9 #include <sys/socket.h>
10 #include <netinet/in.h>
12 unsigned short port = 2007;
14 struct in_addr *destinations = NULL;
15 unsigned num_destinations = 0;
16 unsigned room_destinations = 0;
18 struct in_addr *sources = NULL;
19 unsigned num_sources = 0;
20 unsigned room_sources = 0;
22 unsigned num_senders = 128;
23 unsigned do_listen = 1;
25 unsigned long long total_bytes_received = 0;
26 pthread_mutex_t receive_mutex = PTHREAD_MUTEX_INITIALIZER;
28 unsigned long long total_bytes_sent = 0;
29 pthread_mutex_t send_mutex = PTHREAD_MUTEX_INITIALIZER;
31 const static struct option longopts[] = {
32 { "source-list", required_argument, NULL, 's' },
33 { "destination-list", required_argument, NULL, 'd' },
34 { "num-senders", required_argument, NULL, 'n' },
35 { "port", required_argument, NULL, 'p' },
36 { "sender-only", no_argument, NULL, 'o' },
40 // generates from [0,1>
41 double gen_uniform_random()
43 return rand() / (RAND_MAX+1.0);
46 double gen_pareto_random(double min, double k)
48 double u = gen_uniform_random();
49 return min * pow(u, -1.0 / k);
52 void read_ip_list(char *filename, struct in_addr **addr_list, unsigned *num, unsigned *room)
55 FILE *in = fopen(filename, "r");
66 if (fgets(buf, 256, in) == NULL)
69 ptr = strchr(buf, '\n');
73 ptr = strchr(buf, '\r');
77 ptr = buf + strspn(buf, " \t");
79 if (ptr[0] == '#' || ptr[0] == 0)
82 he = gethostbyname(ptr);
88 // just pick the first for now
89 memcpy(&addr.s_addr, he->h_addr_list[0], sizeof(addr.s_addr));
97 *addr_list = (struct in_addr *)realloc(*addr_list, *room * sizeof(struct in_addr));
100 (*addr_list)[*num] = addr;
107 void parse_options(int argc, char **argv)
109 int option_index = 0;
112 int c = getopt_long(argc, argv, "s:d:n:p:o", longopts, &option_index);
115 read_ip_list(optarg, &sources, &num_sources, &room_sources);
118 read_ip_list(optarg, &destinations, &num_destinations, &room_destinations);
121 num_senders = atoi(optarg);
130 return; // end of argument list
132 fprintf(stderr, "Invalid option\n");
138 void *sender_worker(void *arg)
143 unsigned long long bytes_sent = 0;
145 for (i = 0; i < 65536; ++i)
146 buf[i] = rand() & 0xff;
148 for (i = 0; i < 1000; ++i) {
149 unsigned src_num = (unsigned)(num_sources * gen_uniform_random());
150 unsigned dst_num = (unsigned)(num_destinations * gen_uniform_random());
151 unsigned num_bytes = (unsigned)gen_pareto_random(1048576.0, 1.0);
152 struct sockaddr_in sin;
154 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
160 // FIXME: bind to the right source
162 sin.sin_family = AF_INET;
163 sin.sin_port = htons(port);
164 sin.sin_addr = destinations[dst_num];
166 if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
171 while (num_bytes > 0) {
172 unsigned bytes_to_send = num_bytes;
175 if (bytes_to_send > 65536) {
176 bytes_to_send = 65536;
179 ret = send(sock, buf, bytes_to_send, MSG_NOSIGNAL);
188 // update the central counter after every 1MB (8ms
189 // at gigabit speeds, should be enough) of sent data
190 if (bytes_sent > 1048576) {
191 pthread_mutex_lock(&send_mutex);
192 total_bytes_sent += bytes_sent;
193 pthread_mutex_unlock(&send_mutex);
202 pthread_mutex_lock(&send_mutex);
203 total_bytes_sent += bytes_sent;
204 pthread_mutex_unlock(&send_mutex);
209 void *receiver_worker(void *arg)
213 unsigned long long bytes_received = 0;
216 int ret = read(sock, buf, 65536);
220 bytes_received += ret;
222 // update the central counter after every 1MB (8ms
223 // at gigabit speeds, should be enough) of received data
224 if (bytes_received > 1048576) {
225 pthread_mutex_lock(&receive_mutex);
226 total_bytes_received += bytes_received;
227 pthread_mutex_unlock(&receive_mutex);
233 if (close(sock) == -1) {
238 pthread_mutex_lock(&receive_mutex);
239 total_bytes_received += bytes_received;
240 pthread_mutex_unlock(&receive_mutex);
245 void *receiver_dispatcher(void *arg)
247 int server_sock = (int)arg;
250 // FIXME: these do not really set errno
251 if (pthread_attr_init(&attr) != 0) {
252 perror("pthread_attr_init()");
256 if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + 65536 + 0x4000) != 0) {
257 perror("pthread_attr_setstacksize");
261 if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) {
262 perror("pthread_attr_setdetachstate");
267 * Listen for incoming connections, spawning off one receiver
268 * thread for each (which will just gobble up the data until
272 struct sockaddr_in addr;
273 socklen_t addr_len = sizeof(addr);
276 int sock = accept(server_sock, (struct sockaddr *)&addr, &addr_len);
282 if (pthread_create(&thread, &attr, receiver_worker, (void *)sock) != 0) {
283 perror("pthread_create()");
289 int get_server_socket(unsigned short port)
292 struct sockaddr_in sin;
295 server_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
296 if (server_sock == -1) {
301 if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
302 perror("setsocket(SO_REUSEADDR)");
306 sin.sin_family = AF_INET;
307 sin.sin_port = htons(port);
308 sin.sin_addr.s_addr = INADDR_ANY;
310 if (bind(server_sock, (struct sockaddr *)&sin, sizeof(struct sockaddr)) == -1) {
315 if (listen(server_sock, 255) == -1) {
323 int main(int argc, char **argv)
329 parse_options(argc, argv);
331 if (destinations == NULL || sources == NULL) {
332 fprintf(stderr, "Missing or empty source or destination host list. Aborting.\n");
336 server_sock = get_server_socket(port);
339 printf("Sending data on port %u from %u sources to %u destinations.\n\n",
340 port, num_sources, num_destinations);
342 // FIXME: these do not really set errno
343 if (pthread_attr_init(&attr) != 0) {
344 perror("pthread_attr_init()");
348 if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + 65536 + 0x4000) != 0) {
349 perror("pthread_attr_setstacksize");
353 if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) {
354 perror("pthread_attr_setdetachstate");
358 // Fire off the master receiver.
361 if (pthread_create(&thread, &attr, receiver_dispatcher, (void *)server_sock) != 0) {
362 perror("pthread_create()");
367 printf("Waiting five seconds before starting senders...\n");
370 // Fire off sender workers.
371 for (i = 0; i < num_senders; ++i) {
374 if (pthread_create(&thread, &attr, sender_worker, NULL) != 0) {
375 perror("pthread_create()");
380 // Just stay around collecting statistics until we're done.
382 unsigned long long sent, received;
383 static unsigned long long last_sent = 0, last_received = 0;
384 double recv_rate, send_rate;
386 pthread_mutex_lock(&send_mutex);
387 sent = total_bytes_sent;
388 pthread_mutex_unlock(&send_mutex);
390 pthread_mutex_lock(&receive_mutex);
391 received = total_bytes_received;
392 pthread_mutex_unlock(&receive_mutex);
394 send_rate = (sent - last_sent) * 8.0 / 1048576.0;
395 recv_rate = (received - last_received) * 8.0 / 1048576.0;
397 printf("%12llu %12llu %5.0f Mbit/sec %5.0f Mbit/sec\n", sent, received,
398 send_rate, recv_rate);
401 last_received = received;