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 ret = epoll_wait(ep_fd, events, num_sockets_per_sender, -1);
224 for (i = 0; i < ret; ++i) {
225 unsigned bytes_to_send = events[i].data.u64;
228 if (bytes_to_send > 65536) {
229 bytes_to_send = 65536;
232 ret = send(events[i].data.fd, buf, bytes_to_send, MSG_NOSIGNAL);
238 events[i].data.u64 -= ret;
241 // update the central counter after every 1MB (8ms
242 // at gigabit speeds, should be enough) of sent data
243 if (bytes_sent > 1048576) {
244 pthread_mutex_lock(&send_mutex);
245 total_bytes_sent += bytes_sent;
246 pthread_mutex_unlock(&send_mutex);
251 if (events[i].data.u64 == 0) {
252 close(events[i].data.fd);
253 if (epoll_ctl(ep_fd, EPOLL_CTL_DEL, events[i].data.fd, NULL) == -1) {
254 perror("EPOLL_CTL_ADD");
258 generate_new_sender(ep_fd);
263 pthread_mutex_lock(&send_mutex);
264 total_bytes_sent += bytes_sent;
265 pthread_mutex_unlock(&send_mutex);
273 void *receiver_worker(void *arg)
277 unsigned long long bytes_received = 0;
280 int ret = read(sock, buf, 65536);
284 bytes_received += ret;
286 // update the central counter after every 1MB (8ms
287 // at gigabit speeds, should be enough) of received data
288 if (bytes_received > 1048576) {
289 pthread_mutex_lock(&receive_mutex);
290 total_bytes_received += bytes_received;
291 pthread_mutex_unlock(&receive_mutex);
297 if (close(sock) == -1) {
302 pthread_mutex_lock(&receive_mutex);
303 total_bytes_received += bytes_received;
304 pthread_mutex_unlock(&receive_mutex);
309 void *receiver_dispatcher(void *arg)
311 int server_sock = (int)arg;
314 // FIXME: these do not really set errno
315 if (pthread_attr_init(&attr) != 0) {
316 perror("pthread_attr_init()");
320 if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + 65536 + 0x4000) != 0) {
321 perror("pthread_attr_setstacksize");
325 if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) {
326 perror("pthread_attr_setdetachstate");
331 * Listen for incoming connections, spawning off one receiver
332 * thread for each (which will just gobble up the data until
336 struct sockaddr_in addr;
337 socklen_t addr_len = sizeof(addr);
340 int sock = accept(server_sock, (struct sockaddr *)&addr, &addr_len);
346 if (pthread_create(&thread, &attr, receiver_worker, (void *)sock) != 0) {
347 perror("pthread_create()");
353 int get_server_socket(unsigned short port)
356 struct sockaddr_in sin;
359 server_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
360 if (server_sock == -1) {
365 if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
366 perror("setsocket(SO_REUSEADDR)");
370 sin.sin_family = AF_INET;
371 sin.sin_port = htons(port);
372 sin.sin_addr.s_addr = INADDR_ANY;
374 if (bind(server_sock, (struct sockaddr *)&sin, sizeof(struct sockaddr)) == -1) {
379 if (listen(server_sock, 255) == -1) {
387 int main(int argc, char **argv)
393 parse_options(argc, argv);
395 if (destinations == NULL || sources == NULL) {
396 fprintf(stderr, "Missing or empty source or destination host list. Aborting.\n");
401 server_sock = get_server_socket(port);
404 printf("Sending data on port %u from %u sources to %u destinations.\n\n",
405 port, num_sources, num_destinations);
407 // FIXME: these do not really set errno
408 if (pthread_attr_init(&attr) != 0) {
409 perror("pthread_attr_init()");
413 if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + 65536 + 0x4000) != 0) {
414 perror("pthread_attr_setstacksize");
418 if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) {
419 perror("pthread_attr_setdetachstate");
423 // Fire off the master receiver.
426 if (pthread_create(&thread, &attr, receiver_dispatcher, (void *)server_sock) != 0) {
427 perror("pthread_create()");
432 printf("Waiting five seconds before starting senders...\n");
435 // Fire off sender workers.
436 for (i = 0; i < num_senders; ++i) {
439 if (pthread_create(&thread, &attr, sender_worker, NULL) != 0) {
440 perror("pthread_create()");
445 // Just stay around collecting statistics until we're done.
447 unsigned long long sent, received;
448 static unsigned long long last_sent = 0, last_received = 0;
449 double recv_rate, send_rate;
451 pthread_mutex_lock(&send_mutex);
452 sent = total_bytes_sent;
453 pthread_mutex_unlock(&send_mutex);
455 pthread_mutex_lock(&receive_mutex);
456 received = total_bytes_received;
457 pthread_mutex_unlock(&receive_mutex);
459 send_rate = (sent - last_sent) * 8.0 / 1048576.0;
460 recv_rate = (received - last_received) * 8.0 / 1048576.0;
462 printf("%12llu %12llu %5.0f Mbit/sec %5.0f Mbit/sec\n", sent, received,
463 send_rate, recv_rate);
466 last_received = received;