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: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");
146 void generate_new_sender(int ep_fd)
149 unsigned src_num, dst_num;
150 unsigned long long num_bytes;
151 struct sockaddr_in sin;
152 struct epoll_event ev;
154 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
160 // find the right parameters
161 src_num = (unsigned)(num_sources * gen_uniform_random());
162 dst_num = (unsigned)(num_destinations * gen_uniform_random());
163 num_bytes = (unsigned)gen_pareto_random(1048576.0, 1.0);
165 // FIXME: bind to the right source
167 if (ioctl(sock, FIONBIO, &one) == -1) {
172 sin.sin_family = AF_INET;
173 sin.sin_port = htons(port);
174 sin.sin_addr = destinations[dst_num];
176 // non-blocking connect (will be detected by epoll later)
177 if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1 && errno != EINPROGRESS) {
182 // stick it in the epoll set (FIXME: make edge-triggered?)
183 ev.events = EPOLLOUT | EPOLLHUP | EPOLLERR;
185 ev.data.u64 = num_bytes;
187 if (epoll_ctl(ep_fd, EPOLL_CTL_ADD, sock, &ev) == -1) {
188 perror("EPOLL_CTL_ADD");
193 void *sender_worker(void *arg)
197 unsigned long long bytes_sent = 0;
199 struct epoll_event *events;
201 ep_fd = epoll_create(num_sockets_per_sender);
203 perror("epoll_create");
207 // malloc, since there might not be enough room on the stack
208 events = (struct epoll_event *)malloc(sizeof(struct epoll_event) * num_sockets_per_sender);
209 if (events == NULL) {
214 // fill the buffer with random junk
215 for (i = 0; i < 65536; ++i)
216 buf[i] = rand() & 0xff;
218 // allocate all the senders
219 for (i = 0; i < num_sockets_per_sender; ++i)
220 generate_new_sender(ep_fd);
223 int num_active = epoll_wait(ep_fd, events, num_sockets_per_sender, -1);
224 if (num_active == -1) {
225 perror("epoll_wait");
229 for (i = 0; i < num_active; ++i) {
230 unsigned bytes_to_send = events[i].data.u64;
233 if (bytes_to_send > 65536) {
234 bytes_to_send = 65536;
237 ret = send(events[i].data.fd, buf, bytes_to_send, MSG_NOSIGNAL);
243 events[i].data.u64 -= ret;
246 // update the central counter after every 1MB (8ms
247 // at gigabit speeds, should be enough) of sent data
248 if (bytes_sent > 1048576) {
249 pthread_mutex_lock(&send_mutex);
250 total_bytes_sent += bytes_sent;
251 pthread_mutex_unlock(&send_mutex);
256 if (events[i].data.u64 == 0) {
257 close(events[i].data.fd);
258 if (epoll_ctl(ep_fd, EPOLL_CTL_DEL, events[i].data.fd, NULL) == -1) {
259 perror("EPOLL_CTL_ADD");
263 generate_new_sender(ep_fd);
268 pthread_mutex_lock(&send_mutex);
269 total_bytes_sent += bytes_sent;
270 pthread_mutex_unlock(&send_mutex);
278 void *receiver_worker(void *arg)
282 unsigned long long bytes_received = 0;
285 int ret = read(sock, buf, 65536);
289 bytes_received += ret;
291 // update the central counter after every 1MB (8ms
292 // at gigabit speeds, should be enough) of received data
293 if (bytes_received > 1048576) {
294 pthread_mutex_lock(&receive_mutex);
295 total_bytes_received += bytes_received;
296 pthread_mutex_unlock(&receive_mutex);
302 if (close(sock) == -1) {
307 pthread_mutex_lock(&receive_mutex);
308 total_bytes_received += bytes_received;
309 pthread_mutex_unlock(&receive_mutex);
314 void *receiver_dispatcher(void *arg)
316 int server_sock = (int)arg;
319 // FIXME: these do not really set errno
320 if (pthread_attr_init(&attr) != 0) {
321 perror("pthread_attr_init()");
325 if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + 65536 + 0x4000) != 0) {
326 perror("pthread_attr_setstacksize");
330 if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) {
331 perror("pthread_attr_setdetachstate");
336 * Listen for incoming connections, spawning off one receiver
337 * thread for each (which will just gobble up the data until
341 struct sockaddr_in addr;
342 socklen_t addr_len = sizeof(addr);
345 int sock = accept(server_sock, (struct sockaddr *)&addr, &addr_len);
351 if (pthread_create(&thread, &attr, receiver_worker, (void *)sock) != 0) {
352 perror("pthread_create()");
358 int get_server_socket(unsigned short port)
361 struct sockaddr_in sin;
364 server_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
365 if (server_sock == -1) {
370 if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
371 perror("setsocket(SO_REUSEADDR)");
375 sin.sin_family = AF_INET;
376 sin.sin_port = htons(port);
377 sin.sin_addr.s_addr = INADDR_ANY;
379 if (bind(server_sock, (struct sockaddr *)&sin, sizeof(struct sockaddr)) == -1) {
384 if (listen(server_sock, 255) == -1) {
392 int main(int argc, char **argv)
398 parse_options(argc, argv);
400 if (destinations == NULL || sources == NULL) {
401 fprintf(stderr, "Missing or empty source or destination host list. Aborting.\n");
406 server_sock = get_server_socket(port);
409 printf("Sending data on port %u from %u sources to %u destinations.\n\n",
410 port, num_sources, num_destinations);
412 // FIXME: these do not really set errno
413 if (pthread_attr_init(&attr) != 0) {
414 perror("pthread_attr_init()");
418 if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + 65536 + 0x4000) != 0) {
419 perror("pthread_attr_setstacksize");
423 if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) {
424 perror("pthread_attr_setdetachstate");
428 // Fire off the master receiver.
431 if (pthread_create(&thread, &attr, receiver_dispatcher, (void *)server_sock) != 0) {
432 perror("pthread_create()");
437 printf("Waiting five seconds before starting senders...\n");
440 // Fire off sender workers.
441 for (i = 0; i < num_senders; ++i) {
444 if (pthread_create(&thread, &attr, sender_worker, NULL) != 0) {
445 perror("pthread_create()");
450 // Just stay around collecting statistics until we're done.
452 unsigned long long sent, received;
453 static unsigned long long last_sent = 0, last_received = 0;
454 double recv_rate, send_rate;
456 pthread_mutex_lock(&send_mutex);
457 sent = total_bytes_sent;
458 pthread_mutex_unlock(&send_mutex);
460 pthread_mutex_lock(&receive_mutex);
461 received = total_bytes_received;
462 pthread_mutex_unlock(&receive_mutex);
464 send_rate = (sent - last_sent) * 8.0 / 1048576.0;
465 recv_rate = (received - last_received) * 8.0 / 1048576.0;
467 printf("%12llu %12llu %5.0f Mbit/sec %5.0f Mbit/sec\n", sent, received,
468 send_rate, recv_rate);
471 last_received = received;