9 #include <sys/socket.h>
10 #include <netinet/in.h>
11 #include <sys/epoll.h>
12 #include <sys/ioctl.h>
15 unsigned short port = 2007;
17 struct in_addr *destinations = NULL;
18 unsigned num_destinations = 0;
19 unsigned room_destinations = 0;
21 struct in_addr *sources = NULL;
22 unsigned num_sources = 0;
23 unsigned room_sources = 0;
25 unsigned num_senders = 128;
26 unsigned num_sockets_per_sender = 16;
27 unsigned do_listen = 1;
29 unsigned long long total_bytes_received = 0;
30 pthread_mutex_t receive_mutex = PTHREAD_MUTEX_INITIALIZER;
32 unsigned long long total_bytes_sent = 0;
33 pthread_mutex_t send_mutex = PTHREAD_MUTEX_INITIALIZER;
35 const static struct option longopts[] = {
36 { "source-list", required_argument, NULL, 's' },
37 { "destination-list", required_argument, NULL, 'd' },
38 { "num-senders", required_argument, NULL, 'n' },
39 { "num-sockets-per-sender", required_argument, NULL, 'N' },
40 { "port", required_argument, NULL, 'p' },
41 { "sender-only", no_argument, NULL, 'o' },
45 // generates from [0,1>
46 double gen_uniform_random()
48 return rand() / (RAND_MAX+1.0);
51 double gen_pareto_random(double min, double k)
53 double u = gen_uniform_random();
54 return min * pow(u, -1.0 / k);
57 void read_ip_list(char *filename, struct in_addr **addr_list, unsigned *num, unsigned *room)
60 FILE *in = fopen(filename, "r");
71 if (fgets(buf, 256, in) == NULL)
74 ptr = strchr(buf, '\n');
78 ptr = strchr(buf, '\r');
82 ptr = buf + strspn(buf, " \t");
84 if (ptr[0] == '#' || ptr[0] == 0)
87 he = gethostbyname(ptr);
93 // just pick the first for now
94 memcpy(&addr.s_addr, he->h_addr_list[0], sizeof(addr.s_addr));
102 *addr_list = (struct in_addr *)realloc(*addr_list, *room * sizeof(struct in_addr));
105 (*addr_list)[*num] = addr;
112 void parse_options(int argc, char **argv)
114 int option_index = 0;
117 int c = getopt_long(argc, argv, "s:d:n:N:p:o", longopts, &option_index);
120 read_ip_list(optarg, &sources, &num_sources, &room_sources);
123 read_ip_list(optarg, &destinations, &num_destinations, &room_destinations);
126 num_senders = atoi(optarg);
129 num_sockets_per_sender = atoi(optarg);
138 return; // end of argument list
140 fprintf(stderr, "Invalid option\n");
148 unsigned long long bytes_left;
151 void generate_new_sender(int ep_fd)
154 unsigned src_num, dst_num;
155 unsigned long long num_bytes;
156 struct sockaddr_in sin;
157 struct epoll_event ev;
160 s = (struct sender *)malloc(sizeof(struct sender));
166 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
172 // find the right parameters
173 src_num = (unsigned)(num_sources * gen_uniform_random());
174 dst_num = (unsigned)(num_destinations * gen_uniform_random());
175 num_bytes = (unsigned)gen_pareto_random(1048576.0, 1.0);
177 // FIXME: bind to the right source
179 if (ioctl(sock, FIONBIO, &one) == -1) {
184 sin.sin_family = AF_INET;
185 sin.sin_port = htons(port);
186 sin.sin_addr = destinations[dst_num];
188 // non-blocking connect (will be detected by epoll later)
189 if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1 && errno != EINPROGRESS) {
194 // stick it in the epoll set (FIXME: make edge-triggered?)
195 ev.events = EPOLLOUT | EPOLLHUP | EPOLLERR;
199 s->bytes_left = num_bytes;
201 if (epoll_ctl(ep_fd, EPOLL_CTL_ADD, sock, &ev) == -1) {
202 perror("EPOLL_CTL_ADD");
207 void *sender_worker(void *arg)
211 unsigned long long bytes_sent = 0;
213 struct epoll_event *events;
215 ep_fd = epoll_create(num_sockets_per_sender);
217 perror("epoll_create");
221 // malloc, since there might not be enough room on the stack
222 events = (struct epoll_event *)malloc(sizeof(struct epoll_event) * num_sockets_per_sender);
223 if (events == NULL) {
228 // fill the buffer with random junk
229 for (i = 0; i < 65536; ++i)
230 buf[i] = rand() & 0xff;
232 // allocate all the senders
233 for (i = 0; i < num_sockets_per_sender; ++i)
234 generate_new_sender(ep_fd);
237 int num_active = epoll_wait(ep_fd, events, num_sockets_per_sender, -1);
238 if (num_active == -1) {
239 perror("epoll_wait");
243 for (i = 0; i < num_active; ++i) {
244 struct sender *s = (struct sender *)events[i].data.ptr;
245 unsigned long long bytes_to_send = s->bytes_left;
248 if (bytes_to_send > 65536) {
249 bytes_to_send = 65536;
252 ret = send(s->fd, buf, bytes_to_send, MSG_NOSIGNAL);
258 s->bytes_left -= ret;
261 // update the central counter after every 1MB (8ms
262 // at gigabit speeds, should be enough) of sent data
263 if (bytes_sent > 1048576) {
264 pthread_mutex_lock(&send_mutex);
265 total_bytes_sent += bytes_sent;
266 pthread_mutex_unlock(&send_mutex);
271 if (s->bytes_left == 0) {
272 if (epoll_ctl(ep_fd, EPOLL_CTL_DEL, s->fd, NULL) == -1) {
273 perror("EPOLL_CTL_ADD");
279 generate_new_sender(ep_fd);
284 pthread_mutex_lock(&send_mutex);
285 total_bytes_sent += bytes_sent;
286 pthread_mutex_unlock(&send_mutex);
294 void *receiver_worker(void *arg)
298 unsigned long long bytes_received = 0;
301 int ret = read(sock, buf, 65536);
305 bytes_received += ret;
307 // update the central counter after every 1MB (8ms
308 // at gigabit speeds, should be enough) of received data
309 if (bytes_received > 1048576) {
310 pthread_mutex_lock(&receive_mutex);
311 total_bytes_received += bytes_received;
312 pthread_mutex_unlock(&receive_mutex);
318 if (close(sock) == -1) {
323 pthread_mutex_lock(&receive_mutex);
324 total_bytes_received += bytes_received;
325 pthread_mutex_unlock(&receive_mutex);
330 void *receiver_dispatcher(void *arg)
332 int server_sock = (int)arg;
335 // FIXME: these do not really set errno
336 if (pthread_attr_init(&attr) != 0) {
337 perror("pthread_attr_init()");
341 if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + 65536 + 0x4000) != 0) {
342 perror("pthread_attr_setstacksize");
346 if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) {
347 perror("pthread_attr_setdetachstate");
352 * Listen for incoming connections, spawning off one receiver
353 * thread for each (which will just gobble up the data until
357 struct sockaddr_in addr;
358 socklen_t addr_len = sizeof(addr);
361 int sock = accept(server_sock, (struct sockaddr *)&addr, &addr_len);
367 if (pthread_create(&thread, &attr, receiver_worker, (void *)sock) != 0) {
368 perror("pthread_create()");
374 int get_server_socket(unsigned short port)
377 struct sockaddr_in sin;
380 server_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
381 if (server_sock == -1) {
386 if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
387 perror("setsocket(SO_REUSEADDR)");
391 sin.sin_family = AF_INET;
392 sin.sin_port = htons(port);
393 sin.sin_addr.s_addr = INADDR_ANY;
395 if (bind(server_sock, (struct sockaddr *)&sin, sizeof(struct sockaddr)) == -1) {
400 if (listen(server_sock, 255) == -1) {
408 int main(int argc, char **argv)
414 parse_options(argc, argv);
416 if (destinations == NULL || sources == NULL) {
417 fprintf(stderr, "Missing or empty source or destination host list. Aborting.\n");
422 server_sock = get_server_socket(port);
425 printf("Sending data on port %u from %u sources to %u destinations.\n\n",
426 port, num_sources, num_destinations);
428 // FIXME: these do not really set errno
429 if (pthread_attr_init(&attr) != 0) {
430 perror("pthread_attr_init()");
434 if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + 65536 + 0x4000) != 0) {
435 perror("pthread_attr_setstacksize");
439 if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) {
440 perror("pthread_attr_setdetachstate");
444 // Fire off the master receiver.
447 if (pthread_create(&thread, &attr, receiver_dispatcher, (void *)server_sock) != 0) {
448 perror("pthread_create()");
453 printf("Waiting five seconds before starting senders...\n");
456 // Fire off sender workers.
457 for (i = 0; i < num_senders; ++i) {
460 if (pthread_create(&thread, &attr, sender_worker, NULL) != 0) {
461 perror("pthread_create()");
466 // Just stay around collecting statistics until we're done.
468 unsigned long long sent, received;
469 static unsigned long long last_sent = 0, last_received = 0;
470 double recv_rate, send_rate;
472 pthread_mutex_lock(&send_mutex);
473 sent = total_bytes_sent;
474 pthread_mutex_unlock(&send_mutex);
476 pthread_mutex_lock(&receive_mutex);
477 received = total_bytes_received;
478 pthread_mutex_unlock(&receive_mutex);
480 send_rate = (sent - last_sent) * 8.0 / 1048576.0;
481 recv_rate = (received - last_received) * 8.0 / 1048576.0;
483 printf("%12llu %12llu %5.0f Mbit/sec %5.0f Mbit/sec\n", sent, received,
484 send_rate, recv_rate);
487 last_received = received;