I'm running Linux on x86_64 and in my network configuration eth0 is bind to the ip address 20.20.20.20.
I'm trying to measure a baseline network stack bandwith on my stack for eth0. By baseline network stack bandwith I mean how much MB/s the eth0 is capable to send and receive if the actual wire data transfer limitation is not accounted (purely Linux network stack/driver/rx-tx buffer of the actual device capability to transfer).
I wrote the following code:
server.c:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main(void){
struct sockaddr_in serv_addr;
memset(&serv_addr, '\0', sizeof serv_addr);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr("20.20.20.20");
serv_addr.sin_port = htons(22222);
int socket_fd = socket(AF_INET, SOCK_STREAM, 0);
bind(socket_fd, (struct sockaddr *) &serv_addr, sizeof serv_addr);
listen(socket_fd, 1);
struct sockaddr_in client_addr;
int client_addrlen = sizeof client_addr;
int conn_sock = accept(socket_fd, (struct sockaddr *) &client_addr, &client_addrlen);
char buf[256];
size_t received = 0;
while(received <= 1073741824){ //1GB
received += read(conn_sock, buf, sizeof buf);
}
}
client.c:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main(void){
struct sockaddr_in serv_addr;
memset(&serv_addr, '\0', sizeof serv_addr);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr("20.20.20.20");
serv_addr.sin_port = htons(22222);
int socket_fd = socket(AF_INET, SOCK_STREAM, 0);
connect(socket_fd, (struct sockaddr *) &serv_addr, sizeof serv_addr);
char buf[256];
size_t sent = 0;
while(sent <= 1073741824){ //1GB
sent += write(socket_fd, buf, sizeof buf);;
}
}
So I just run both the client and server under the time command.
I got the average bandwith across multiple measurements: 64MB/s
I basically have 2 questions:
How do I check if the corresponding NIC sends data by wire when performing such benchmarking so the actual wire bandwidth affects the measurement? I tried to measure
lonetwork interface and got the same result from the measurement.Is there any hints to determine in which part of the network stack actual stalls occur? Maybe there's something similar to
Top-Down Microarchitectural Analysis Methodfor network stack optimization?