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);
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");
282 generate_new_sender(ep_fd);
287 pthread_mutex_lock(&send_mutex);
288 total_bytes_sent += bytes_sent;
289 pthread_mutex_unlock(&send_mutex);
297 void *receiver_worker(void *arg)
301 unsigned long long bytes_received = 0;
304 int ret = read(sock, buf, 65536);
308 bytes_received += ret;
310 // update the central counter after every 1MB (8ms
311 // at gigabit speeds, should be enough) of received data
312 if (bytes_received > 1048576) {
313 pthread_mutex_lock(&receive_mutex);
314 total_bytes_received += bytes_received;
315 pthread_mutex_unlock(&receive_mutex);
321 if (close(sock) == -1) {
326 pthread_mutex_lock(&receive_mutex);
327 total_bytes_received += bytes_received;
328 pthread_mutex_unlock(&receive_mutex);
333 void *receiver_dispatcher(void *arg)
335 int server_sock = (int)arg;
338 // FIXME: these do not really set errno
339 if (pthread_attr_init(&attr) != 0) {
340 perror("pthread_attr_init()");
344 if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + 65536 + 0x4000) != 0) {
345 perror("pthread_attr_setstacksize");
349 if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) {
350 perror("pthread_attr_setdetachstate");
355 * Listen for incoming connections, spawning off one receiver
356 * thread for each (which will just gobble up the data until
360 struct sockaddr_in addr;
361 socklen_t addr_len = sizeof(addr);
364 int sock = accept(server_sock, (struct sockaddr *)&addr, &addr_len);
370 if (pthread_create(&thread, &attr, receiver_worker, (void *)sock) != 0) {
371 perror("pthread_create()");
377 int get_server_socket(unsigned short port)
380 struct sockaddr_in sin;
383 server_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
384 if (server_sock == -1) {
389 if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
390 perror("setsocket(SO_REUSEADDR)");
394 sin.sin_family = AF_INET;
395 sin.sin_port = htons(port);
396 sin.sin_addr.s_addr = INADDR_ANY;
398 if (bind(server_sock, (struct sockaddr *)&sin, sizeof(struct sockaddr)) == -1) {
403 if (listen(server_sock, 255) == -1) {
411 int main(int argc, char **argv)
417 parse_options(argc, argv);
419 if (destinations == NULL || sources == NULL) {
420 fprintf(stderr, "Missing or empty source or destination host list. Aborting.\n");
425 server_sock = get_server_socket(port);
428 printf("Sending data on port %u from %u sources to %u destinations.\n\n",
429 port, num_sources, num_destinations);
431 // FIXME: these do not really set errno
432 if (pthread_attr_init(&attr) != 0) {
433 perror("pthread_attr_init()");
437 if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + 65536 + 0x4000) != 0) {
438 perror("pthread_attr_setstacksize");
442 if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) {
443 perror("pthread_attr_setdetachstate");
447 // Fire off the master receiver.
450 if (pthread_create(&thread, &attr, receiver_dispatcher, (void *)server_sock) != 0) {
451 perror("pthread_create()");
456 printf("Waiting five seconds before starting senders...\n");
459 // Fire off sender workers.
460 for (i = 0; i < num_senders; ++i) {
463 if (pthread_create(&thread, &attr, sender_worker, NULL) != 0) {
464 perror("pthread_create()");
469 // Just stay around collecting statistics until we're done.
471 unsigned long long sent, received;
472 static unsigned long long last_sent = 0, last_received = 0;
473 double recv_rate, send_rate;
475 pthread_mutex_lock(&send_mutex);
476 sent = total_bytes_sent;
477 pthread_mutex_unlock(&send_mutex);
479 pthread_mutex_lock(&receive_mutex);
480 received = total_bytes_received;
481 pthread_mutex_unlock(&receive_mutex);
483 send_rate = (sent - last_sent) * 8.0 / 1048576.0;
484 recv_rate = (received - last_received) * 8.0 / 1048576.0;
486 printf("%12llu %12llu %5.0f Mbit/sec %5.0f Mbit/sec\n", sent, received,
487 send_rate, recv_rate);
490 last_received = received;