9 #include <sys/socket.h>
10 #include <netinet/in.h>
11 #include <sys/epoll.h>
12 #include <sys/ioctl.h>
14 unsigned short port = 2007;
16 struct in_addr *destinations = NULL;
17 unsigned num_destinations = 0;
18 unsigned room_destinations = 0;
20 struct in_addr *sources = NULL;
21 unsigned num_sources = 0;
22 unsigned room_sources = 0;
24 unsigned num_senders = 128;
25 unsigned num_sockets_per_sender = 16;
26 unsigned do_listen = 1;
28 unsigned long long total_bytes_received = 0;
29 pthread_mutex_t receive_mutex = PTHREAD_MUTEX_INITIALIZER;
31 unsigned long long total_bytes_sent = 0;
32 pthread_mutex_t send_mutex = PTHREAD_MUTEX_INITIALIZER;
34 const static struct option longopts[] = {
35 { "source-list", required_argument, NULL, 's' },
36 { "destination-list", required_argument, NULL, 'd' },
37 { "num-senders", required_argument, NULL, 'n' },
38 { "num-sockets-per-sender", required_argument, NULL, 'N' },
39 { "port", required_argument, NULL, 'p' },
40 { "sender-only", no_argument, NULL, 'o' },
44 // generates from [0,1>
45 double gen_uniform_random()
47 return rand() / (RAND_MAX+1.0);
50 double gen_pareto_random(double min, double k)
52 double u = gen_uniform_random();
53 return min * pow(u, -1.0 / k);
56 void read_ip_list(char *filename, struct in_addr **addr_list, unsigned *num, unsigned *room)
59 FILE *in = fopen(filename, "r");
70 if (fgets(buf, 256, in) == NULL)
73 ptr = strchr(buf, '\n');
77 ptr = strchr(buf, '\r');
81 ptr = buf + strspn(buf, " \t");
83 if (ptr[0] == '#' || ptr[0] == 0)
86 he = gethostbyname(ptr);
92 // just pick the first for now
93 memcpy(&addr.s_addr, he->h_addr_list[0], sizeof(addr.s_addr));
101 *addr_list = (struct in_addr *)realloc(*addr_list, *room * sizeof(struct in_addr));
104 (*addr_list)[*num] = addr;
111 void parse_options(int argc, char **argv)
113 int option_index = 0;
116 int c = getopt_long(argc, argv, "s:d:n:p:o", longopts, &option_index);
119 read_ip_list(optarg, &sources, &num_sources, &room_sources);
122 read_ip_list(optarg, &destinations, &num_destinations, &room_destinations);
125 num_senders = atoi(optarg);
128 num_sockets_per_sender = atoi(optarg);
137 return; // end of argument list
139 fprintf(stderr, "Invalid option\n");
145 void generate_new_sender(int ep_fd)
148 unsigned src_num, dst_num;
149 unsigned long long num_bytes;
150 struct sockaddr_in sin;
151 struct epoll_event ev;
153 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
159 // find the right parameters
160 src_num = (unsigned)(num_sources * gen_uniform_random());
161 dst_num = (unsigned)(num_destinations * gen_uniform_random());
162 num_bytes = (unsigned)gen_pareto_random(1048576.0, 1.0);
164 // FIXME: bind to the right source
166 if (ioctl(sock, FIONBIO, &one) == -1) {
171 sin.sin_family = AF_INET;
172 sin.sin_port = htons(port);
173 sin.sin_addr = destinations[dst_num];
175 // non-blocking connect (will be detected by epoll later)
176 if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
181 // stick it in the epoll set (FIXME: make edge-triggered?)
182 ev.events = EPOLLOUT | EPOLLHUP | EPOLLERR;
184 ev.data.u64 = num_bytes;
186 if (epoll_ctl(ep_fd, EPOLL_CTL_ADD, sock, &ev) == -1) {
187 perror("EPOLL_CTL_ADD");
192 void *sender_worker(void *arg)
196 unsigned long long bytes_sent = 0;
198 struct epoll_event *events;
200 ep_fd = epoll_create(num_sockets_per_sender);
202 perror("epoll_create");
206 // malloc, since there might not be enough room on the stack
207 events = (struct epoll_event *)malloc(sizeof(struct epoll_event) * num_sockets_per_sender);
208 if (events == NULL) {
213 // fill the buffer with random junk
214 for (i = 0; i < 65536; ++i)
215 buf[i] = rand() & 0xff;
217 // allocate all the senders
218 for (i = 0; i < num_sockets_per_sender; ++i)
219 generate_new_sender(ep_fd);
222 int ret = epoll_wait(ep_fd, events, num_sockets_per_sender, -1);
223 for (i = 0; i < ret; ++i) {
224 unsigned bytes_to_send = events[i].data.u64;
227 if (bytes_to_send > 65536) {
228 bytes_to_send = 65536;
231 ret = send(events[i].data.fd, buf, bytes_to_send, MSG_NOSIGNAL);
237 events[i].data.u64 -= ret;
240 // update the central counter after every 1MB (8ms
241 // at gigabit speeds, should be enough) of sent data
242 if (bytes_sent > 1048576) {
243 pthread_mutex_lock(&send_mutex);
244 total_bytes_sent += bytes_sent;
245 pthread_mutex_unlock(&send_mutex);
250 if (events[i].data.u64 == 0) {
251 close(events[i].data.fd);
252 if (epoll_ctl(ep_fd, EPOLL_CTL_DEL, events[i].data.fd, NULL) == -1) {
253 perror("EPOLL_CTL_ADD");
257 generate_new_sender(ep_fd);
262 pthread_mutex_lock(&send_mutex);
263 total_bytes_sent += bytes_sent;
264 pthread_mutex_unlock(&send_mutex);
272 void *receiver_worker(void *arg)
276 unsigned long long bytes_received = 0;
279 int ret = read(sock, buf, 65536);
283 bytes_received += ret;
285 // update the central counter after every 1MB (8ms
286 // at gigabit speeds, should be enough) of received data
287 if (bytes_received > 1048576) {
288 pthread_mutex_lock(&receive_mutex);
289 total_bytes_received += bytes_received;
290 pthread_mutex_unlock(&receive_mutex);
296 if (close(sock) == -1) {
301 pthread_mutex_lock(&receive_mutex);
302 total_bytes_received += bytes_received;
303 pthread_mutex_unlock(&receive_mutex);
308 void *receiver_dispatcher(void *arg)
310 int server_sock = (int)arg;
313 // FIXME: these do not really set errno
314 if (pthread_attr_init(&attr) != 0) {
315 perror("pthread_attr_init()");
319 if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + 65536 + 0x4000) != 0) {
320 perror("pthread_attr_setstacksize");
324 if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) {
325 perror("pthread_attr_setdetachstate");
330 * Listen for incoming connections, spawning off one receiver
331 * thread for each (which will just gobble up the data until
335 struct sockaddr_in addr;
336 socklen_t addr_len = sizeof(addr);
339 int sock = accept(server_sock, (struct sockaddr *)&addr, &addr_len);
345 if (pthread_create(&thread, &attr, receiver_worker, (void *)sock) != 0) {
346 perror("pthread_create()");
352 int get_server_socket(unsigned short port)
355 struct sockaddr_in sin;
358 server_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
359 if (server_sock == -1) {
364 if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
365 perror("setsocket(SO_REUSEADDR)");
369 sin.sin_family = AF_INET;
370 sin.sin_port = htons(port);
371 sin.sin_addr.s_addr = INADDR_ANY;
373 if (bind(server_sock, (struct sockaddr *)&sin, sizeof(struct sockaddr)) == -1) {
378 if (listen(server_sock, 255) == -1) {
386 int main(int argc, char **argv)
392 parse_options(argc, argv);
394 if (destinations == NULL || sources == NULL) {
395 fprintf(stderr, "Missing or empty source or destination host list. Aborting.\n");
399 server_sock = get_server_socket(port);
402 printf("Sending data on port %u from %u sources to %u destinations.\n\n",
403 port, num_sources, num_destinations);
405 // FIXME: these do not really set errno
406 if (pthread_attr_init(&attr) != 0) {
407 perror("pthread_attr_init()");
411 if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + 65536 + 0x4000) != 0) {
412 perror("pthread_attr_setstacksize");
416 if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) {
417 perror("pthread_attr_setdetachstate");
421 // Fire off the master receiver.
424 if (pthread_create(&thread, &attr, receiver_dispatcher, (void *)server_sock) != 0) {
425 perror("pthread_create()");
430 printf("Waiting five seconds before starting senders...\n");
433 // Fire off sender workers.
434 for (i = 0; i < num_senders; ++i) {
437 if (pthread_create(&thread, &attr, sender_worker, NULL) != 0) {
438 perror("pthread_create()");
443 // Just stay around collecting statistics until we're done.
445 unsigned long long sent, received;
446 static unsigned long long last_sent = 0, last_received = 0;
447 double recv_rate, send_rate;
449 pthread_mutex_lock(&send_mutex);
450 sent = total_bytes_sent;
451 pthread_mutex_unlock(&send_mutex);
453 pthread_mutex_lock(&receive_mutex);
454 received = total_bytes_received;
455 pthread_mutex_unlock(&receive_mutex);
457 send_rate = (sent - last_sent) * 8.0 / 1048576.0;
458 recv_rate = (received - last_received) * 8.0 / 1048576.0;
460 printf("%12llu %12llu %5.0f Mbit/sec %5.0f Mbit/sec\n", sent, received,
461 send_rate, recv_rate);
464 last_received = received;