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;
16 unsigned update_frequency = 1048576;
18 struct in_addr *destinations = NULL;
19 unsigned num_destinations = 0;
20 unsigned room_destinations = 0;
22 struct in_addr *sources = NULL;
23 unsigned num_sources = 0;
24 unsigned room_sources = 0;
26 unsigned num_senders = 128;
27 unsigned num_sockets_per_sender = 16;
28 unsigned do_listen = 1;
30 unsigned long long total_bytes_received = 0;
31 pthread_mutex_t receive_mutex = PTHREAD_MUTEX_INITIALIZER;
33 unsigned long long total_bytes_sent = 0;
34 pthread_mutex_t send_mutex = PTHREAD_MUTEX_INITIALIZER;
36 const static struct option longopts[] = {
37 { "source-list", required_argument, NULL, 's' },
38 { "destination-list", required_argument, NULL, 'd' },
39 { "num-senders", required_argument, NULL, 'n' },
40 { "num-sockets-per-sender", required_argument, NULL, 'N' },
41 { "port", required_argument, NULL, 'p' },
42 { "sender-only", no_argument, NULL, 'o' },
46 // generates from [0,1>
47 double gen_uniform_random()
49 return rand() / (RAND_MAX+1.0);
52 double gen_pareto_random(double min, double k)
54 double u = gen_uniform_random();
55 return min * pow(u, -1.0 / k);
58 void read_ip_list(char *filename, struct in_addr **addr_list, unsigned *num, unsigned *room)
61 FILE *in = fopen(filename, "r");
72 if (fgets(buf, 256, in) == NULL)
75 ptr = strchr(buf, '\n');
79 ptr = strchr(buf, '\r');
83 ptr = buf + strspn(buf, " \t");
85 if (ptr[0] == '#' || ptr[0] == 0)
88 he = gethostbyname(ptr);
94 // just pick the first for now
95 memcpy(&addr.s_addr, he->h_addr_list[0], sizeof(addr.s_addr));
103 *addr_list = (struct in_addr *)realloc(*addr_list, *room * sizeof(struct in_addr));
106 (*addr_list)[*num] = addr;
113 void parse_options(int argc, char **argv)
115 int option_index = 0;
118 int c = getopt_long(argc, argv, "s:d:n:N:p:o", longopts, &option_index);
121 read_ip_list(optarg, &sources, &num_sources, &room_sources);
124 read_ip_list(optarg, &destinations, &num_destinations, &room_destinations);
127 num_senders = atoi(optarg);
130 num_sockets_per_sender = atoi(optarg);
139 return; // end of argument list
141 fprintf(stderr, "Invalid option\n");
149 unsigned long long bytes_left;
152 void generate_new_sender(int ep_fd, struct sender *s)
155 unsigned src_num, dst_num;
156 unsigned long long num_bytes;
157 struct sockaddr_in sin;
158 struct epoll_event ev;
160 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
166 // find the right parameters
167 src_num = (unsigned)(num_sources * gen_uniform_random());
168 dst_num = (unsigned)(num_destinations * gen_uniform_random());
169 num_bytes = (unsigned)gen_pareto_random(1048576.0, 1.0);
171 // FIXME: bind to the right source
173 if (ioctl(sock, FIONBIO, &one) == -1) {
178 sin.sin_family = AF_INET;
179 sin.sin_port = htons(port);
180 sin.sin_addr = destinations[dst_num];
182 // non-blocking connect (will be detected by epoll later)
183 if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1 && errno != EINPROGRESS) {
188 // stick it in the epoll set (FIXME: make edge-triggered?)
189 ev.events = EPOLLOUT | EPOLLHUP | EPOLLERR;
193 s->bytes_left = num_bytes;
195 if (epoll_ctl(ep_fd, EPOLL_CTL_ADD, sock, &ev) == -1) {
196 perror("EPOLL_CTL_ADD");
201 void *sender_worker(void *arg)
205 unsigned long long bytes_sent = 0;
207 struct epoll_event *events;
209 ep_fd = epoll_create(num_sockets_per_sender);
211 perror("epoll_create");
215 // malloc, since there might not be enough room on the stack
216 events = (struct epoll_event *)malloc(sizeof(struct epoll_event) * num_sockets_per_sender);
217 if (events == NULL) {
222 // fill the buffer with random junk
223 for (i = 0; i < 65536; ++i)
224 buf[i] = rand() & 0xff;
226 // allocate all the senders
227 for (i = 0; i < num_sockets_per_sender; ++i) {
228 struct sender *s = (struct sender *)malloc(sizeof(struct sender));
234 generate_new_sender(ep_fd, s);
238 int num_active = epoll_wait(ep_fd, events, num_sockets_per_sender, -1);
239 if (num_active == -1) {
240 perror("epoll_wait");
244 for (i = 0; i < num_active; ++i) {
245 struct sender *s = (struct sender *)events[i].data.ptr;
246 unsigned long long bytes_to_send = s->bytes_left;
249 if (bytes_to_send > 65536) {
250 bytes_to_send = 65536;
253 ret = send(s->fd, buf, bytes_to_send, MSG_NOSIGNAL);
262 s->bytes_left -= ret;
265 // update the central counter after every 1MB (8ms
266 // at gigabit speeds, should be enough) of sent data
267 if (bytes_sent > update_frequency) {
268 pthread_mutex_lock(&send_mutex);
269 total_bytes_sent += bytes_sent;
270 pthread_mutex_unlock(&send_mutex);
275 if (s->bytes_left == 0) {
276 if (epoll_ctl(ep_fd, EPOLL_CTL_DEL, s->fd, NULL) == -1) {
277 perror("EPOLL_CTL_ADD");
281 generate_new_sender(ep_fd, s);
286 pthread_mutex_lock(&send_mutex);
287 total_bytes_sent += bytes_sent;
288 pthread_mutex_unlock(&send_mutex);
296 void *receiver_worker(void *arg)
300 unsigned long long bytes_received = 0;
303 int ret = read(sock, buf, 65536);
307 bytes_received += ret;
309 // update the central counter after every 1MB (8ms
310 // at gigabit speeds, should be enough) of received data
311 if (bytes_received > update_frequency) {
312 pthread_mutex_lock(&receive_mutex);
313 total_bytes_received += bytes_received;
314 pthread_mutex_unlock(&receive_mutex);
320 if (close(sock) == -1) {
325 pthread_mutex_lock(&receive_mutex);
326 total_bytes_received += bytes_received;
327 pthread_mutex_unlock(&receive_mutex);
332 void *receiver_dispatcher(void *arg)
334 int server_sock = (int)arg;
337 // FIXME: these do not really set errno
338 if (pthread_attr_init(&attr) != 0) {
339 perror("pthread_attr_init()");
343 if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + 65536 + 0x4000) != 0) {
344 perror("pthread_attr_setstacksize");
348 if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) {
349 perror("pthread_attr_setdetachstate");
354 * Listen for incoming connections, spawning off one receiver
355 * thread for each (which will just gobble up the data until
359 struct sockaddr_in addr;
360 socklen_t addr_len = sizeof(addr);
363 int sock = accept(server_sock, (struct sockaddr *)&addr, &addr_len);
369 if (pthread_create(&thread, &attr, receiver_worker, (void *)sock) != 0) {
370 perror("pthread_create()");
376 int get_server_socket(unsigned short port)
379 struct sockaddr_in sin;
382 server_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
383 if (server_sock == -1) {
388 if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
389 perror("setsocket(SO_REUSEADDR)");
393 sin.sin_family = AF_INET;
394 sin.sin_port = htons(port);
395 sin.sin_addr.s_addr = INADDR_ANY;
397 if (bind(server_sock, (struct sockaddr *)&sin, sizeof(struct sockaddr)) == -1) {
402 if (listen(server_sock, 255) == -1) {
410 int main(int argc, char **argv)
416 parse_options(argc, argv);
418 if (destinations == NULL || sources == NULL) {
419 fprintf(stderr, "Missing or empty source or destination host list. Aborting.\n");
424 server_sock = get_server_socket(port);
427 printf("Sending data on port %u from %u sources to %u destinations.\n\n",
428 port, num_sources, num_destinations);
430 // FIXME: these do not really set errno
431 if (pthread_attr_init(&attr) != 0) {
432 perror("pthread_attr_init()");
436 if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + 65536 + 0x4000) != 0) {
437 perror("pthread_attr_setstacksize");
441 if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) {
442 perror("pthread_attr_setdetachstate");
446 // Fire off the master receiver.
449 if (pthread_create(&thread, &attr, receiver_dispatcher, (void *)server_sock) != 0) {
450 perror("pthread_create()");
455 printf("Waiting five seconds before starting senders...\n");
458 // Fire off sender workers.
459 for (i = 0; i < num_senders; ++i) {
462 if (pthread_create(&thread, &attr, sender_worker, NULL) != 0) {
463 perror("pthread_create()");
468 // Just stay around collecting statistics until we're done.
470 unsigned long long sent, received;
471 static unsigned long long last_sent = 0, last_received = 0;
472 double recv_rate, send_rate;
474 pthread_mutex_lock(&send_mutex);
475 sent = total_bytes_sent;
476 pthread_mutex_unlock(&send_mutex);
478 pthread_mutex_lock(&receive_mutex);
479 received = total_bytes_received;
480 pthread_mutex_unlock(&receive_mutex);
482 send_rate = (sent - last_sent) * 8.0 / 1048576.0;
483 recv_rate = (received - last_received) * 8.0 / 1048576.0;
485 printf("%12llu %12llu %5.0f Mbit/sec %5.0f Mbit/sec\n", sent, received,
486 send_rate, recv_rate);
489 last_received = received;