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, struct sender *s)
154 unsigned src_num, dst_num;
155 unsigned long long num_bytes;
156 struct sockaddr_in sin;
157 struct epoll_event ev;
159 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
165 // find the right parameters
166 src_num = (unsigned)(num_sources * gen_uniform_random());
167 dst_num = (unsigned)(num_destinations * gen_uniform_random());
168 num_bytes = (unsigned)gen_pareto_random(1048576.0, 1.0);
170 // FIXME: bind to the right source
172 if (ioctl(sock, FIONBIO, &one) == -1) {
177 sin.sin_family = AF_INET;
178 sin.sin_port = htons(port);
179 sin.sin_addr = destinations[dst_num];
181 // non-blocking connect (will be detected by epoll later)
182 if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1 && errno != EINPROGRESS) {
187 // stick it in the epoll set (FIXME: make edge-triggered?)
188 ev.events = EPOLLOUT | EPOLLHUP | EPOLLERR;
192 s->bytes_left = num_bytes;
194 if (epoll_ctl(ep_fd, EPOLL_CTL_ADD, sock, &ev) == -1) {
195 perror("EPOLL_CTL_ADD");
200 void *sender_worker(void *arg)
204 unsigned long long bytes_sent = 0;
206 struct epoll_event *events;
208 ep_fd = epoll_create(num_sockets_per_sender);
210 perror("epoll_create");
214 // malloc, since there might not be enough room on the stack
215 events = (struct epoll_event *)malloc(sizeof(struct epoll_event) * num_sockets_per_sender);
216 if (events == NULL) {
221 // fill the buffer with random junk
222 for (i = 0; i < 65536; ++i)
223 buf[i] = rand() & 0xff;
225 // allocate all the senders
226 for (i = 0; i < num_sockets_per_sender; ++i) {
227 struct sender *s = (struct sender *)malloc(sizeof(struct sender));
233 generate_new_sender(ep_fd, s);
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);
261 s->bytes_left -= ret;
264 // update the central counter after every 1MB (8ms
265 // at gigabit speeds, should be enough) of sent data
266 if (bytes_sent > 1048576) {
267 pthread_mutex_lock(&send_mutex);
268 total_bytes_sent += bytes_sent;
269 pthread_mutex_unlock(&send_mutex);
274 if (s->bytes_left == 0) {
275 if (epoll_ctl(ep_fd, EPOLL_CTL_DEL, s->fd, NULL) == -1) {
276 perror("EPOLL_CTL_ADD");
280 generate_new_sender(ep_fd, s);
285 pthread_mutex_lock(&send_mutex);
286 total_bytes_sent += bytes_sent;
287 pthread_mutex_unlock(&send_mutex);
295 void *receiver_worker(void *arg)
299 unsigned long long bytes_received = 0;
302 int ret = read(sock, buf, 65536);
306 bytes_received += ret;
308 // update the central counter after every 1MB (8ms
309 // at gigabit speeds, should be enough) of received data
310 if (bytes_received > 1048576) {
311 pthread_mutex_lock(&receive_mutex);
312 total_bytes_received += bytes_received;
313 pthread_mutex_unlock(&receive_mutex);
319 if (close(sock) == -1) {
324 pthread_mutex_lock(&receive_mutex);
325 total_bytes_received += bytes_received;
326 pthread_mutex_unlock(&receive_mutex);
331 void *receiver_dispatcher(void *arg)
333 int server_sock = (int)arg;
336 // FIXME: these do not really set errno
337 if (pthread_attr_init(&attr) != 0) {
338 perror("pthread_attr_init()");
342 if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + 65536 + 0x4000) != 0) {
343 perror("pthread_attr_setstacksize");
347 if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) {
348 perror("pthread_attr_setdetachstate");
353 * Listen for incoming connections, spawning off one receiver
354 * thread for each (which will just gobble up the data until
358 struct sockaddr_in addr;
359 socklen_t addr_len = sizeof(addr);
362 int sock = accept(server_sock, (struct sockaddr *)&addr, &addr_len);
368 if (pthread_create(&thread, &attr, receiver_worker, (void *)sock) != 0) {
369 perror("pthread_create()");
375 int get_server_socket(unsigned short port)
378 struct sockaddr_in sin;
381 server_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
382 if (server_sock == -1) {
387 if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
388 perror("setsocket(SO_REUSEADDR)");
392 sin.sin_family = AF_INET;
393 sin.sin_port = htons(port);
394 sin.sin_addr.s_addr = INADDR_ANY;
396 if (bind(server_sock, (struct sockaddr *)&sin, sizeof(struct sockaddr)) == -1) {
401 if (listen(server_sock, 255) == -1) {
409 int main(int argc, char **argv)
415 parse_options(argc, argv);
417 if (destinations == NULL || sources == NULL) {
418 fprintf(stderr, "Missing or empty source or destination host list. Aborting.\n");
423 server_sock = get_server_socket(port);
426 printf("Sending data on port %u from %u sources to %u destinations.\n\n",
427 port, num_sources, num_destinations);
429 // FIXME: these do not really set errno
430 if (pthread_attr_init(&attr) != 0) {
431 perror("pthread_attr_init()");
435 if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + 65536 + 0x4000) != 0) {
436 perror("pthread_attr_setstacksize");
440 if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) {
441 perror("pthread_attr_setdetachstate");
445 // Fire off the master receiver.
448 if (pthread_create(&thread, &attr, receiver_dispatcher, (void *)server_sock) != 0) {
449 perror("pthread_create()");
454 printf("Waiting five seconds before starting senders...\n");
457 // Fire off sender workers.
458 for (i = 0; i < num_senders; ++i) {
461 if (pthread_create(&thread, &attr, sender_worker, NULL) != 0) {
462 perror("pthread_create()");
467 // Just stay around collecting statistics until we're done.
469 unsigned long long sent, received;
470 static unsigned long long last_sent = 0, last_received = 0;
471 double recv_rate, send_rate;
473 pthread_mutex_lock(&send_mutex);
474 sent = total_bytes_sent;
475 pthread_mutex_unlock(&send_mutex);
477 pthread_mutex_lock(&receive_mutex);
478 received = total_bytes_received;
479 pthread_mutex_unlock(&receive_mutex);
481 send_rate = (sent - last_sent) * 8.0 / 1048576.0;
482 recv_rate = (received - last_received) * 8.0 / 1048576.0;
484 printf("%12llu %12llu %5.0f Mbit/sec %5.0f Mbit/sec\n", sent, received,
485 send_rate, recv_rate);
488 last_received = received;