static void try_connect (server_auth_t * auth) { int f; CONNECTION *cli; unsigned int ip; /* attempt a connection. we do this nonblocking so that the server doesn't halt if it takes a long time to connect */ f = make_tcp_connection (auth->name, auth->port, &ip); if (f == -1) return; cli = new_connection (); if (!cli) goto error; cli->fd = f; cli->host = STRDUP (auth->alias ? auth->alias : auth->name); if (!cli->host) { OUTOFMEMORY ("try_connect"); goto error; } cli->server_login = 1; if ((cli->opt.auth = CALLOC (1, sizeof (AUTH))) == 0) { OUTOFMEMORY ("try_connect"); goto error; } cli->opt.auth->nonce = generate_nonce (); if (!cli->opt.auth->nonce) { log_message ("try_connect: could not generate nonce, closing connection"); goto error; } cli->ip = BSWAP32 (ip); cli->port = auth->port; if (add_client (cli, 1/* server connection */)) goto error; return; error: log_message ("try_connect: closing connection"); if (cli) { CLOSE (cli->fd); if (cli->host) FREE (cli->host); if (cli->opt.auth) { if (cli->opt.auth->nonce) FREE (cli->opt.auth->nonce); FREE (cli->opt.auth); } FREE (cli); } }
void stat_server_push(void) { unsigned int ip; if(global.stat_server_fd == INVALID_SOCKET) { /* attempt to make new connection to stats server */ if(!*global.stat_user || !*global.stat_pass || !*global.stat_server) { return; /* nothing defined */ } global.stat_server_fd = make_tcp_connection(global.stat_server, global.stat_server_port, &ip); if(global.stat_server_fd != INVALID_SOCKET) { /* do a nonblocking connect */ add_fd(global.stat_server_fd); set_write(global.stat_server_fd); } return; } snprintf(Buf, sizeof(Buf), "STATS %s %u %u 0 %.0f 0\r\n", global.report_name, global.usersHash->dbsize, global.fileLibCount, global.fileLibSize * 1024); if(WRITE(global.stat_server_fd, Buf, strlen(Buf)) == -1) { log_message_level( LOG_LEVEL_SERVER, "stat_server_push: write: %s (%d)", strerror(N_ERRNO), N_ERRNO); CLOSE(global.stat_server_fd); #if HAVE_POLL remove_fd(global.stat_server_fd); #else FD_CLR(global.stat_server_fd, &global.read_fds); FD_CLR(global.stat_server_fd, &global.write_fds); #endif global.stat_server_fd = INVALID_SOCKET; } }
int main(int argc, char * argv[]) { struct fakeswitch *fakeswitches; char * controller_hostname = myargs_get_default_string(my_options,"controller"); int controller_port = myargs_get_default_integer(my_options, "port"); int n_fakeswitches= myargs_get_default_integer(my_options, "switches"); int total_mac_addresses = myargs_get_default_integer(my_options, "mac-addresses"); int mstestlen = myargs_get_default_integer(my_options, "ms-per-test"); int should_test_range=myargs_get_default_flag(my_options, "ranged-test"); int tests_per_loop = myargs_get_default_integer(my_options, "loops"); int debug = myargs_get_default_flag(my_options, "debug"); int warmup = myargs_get_default_integer(my_options, "warmup"); int cooldown = myargs_get_default_integer(my_options, "cooldown"); int delay = myargs_get_default_integer(my_options, "delay"); int connect_delay = myargs_get_default_integer(my_options, "connect-delay"); int connect_group_size = myargs_get_default_integer(my_options, "connect-group-size"); int learn_dst_macs = myargs_get_default_flag(my_options, "learn-dst-macs"); int dpid_offset = myargs_get_default_integer(my_options, "dpid-offset"); int mode = MODE_LATENCY; int i,j; const struct option * long_opts = myargs_to_long(my_options); char * short_opts = myargs_to_short(my_options); /* parse args here */ while(1) { int c; int option_index=0; c = getopt_long(argc, argv, short_opts, long_opts, &option_index); if (c == -1) break; switch (c) { case 'c' : controller_hostname = strdup(optarg); break; case 'd': debug = 1; break; case 'h': myargs_usage(my_options, PROG_TITLE, "help message", NULL, 1); break; case 'L': if(optarg) learn_dst_macs = ( strcasecmp("true", optarg) == 0 || strcasecmp("on", optarg) == 0 || strcasecmp("1", optarg) == 0); else learn_dst_macs = 1; break; case 'l': tests_per_loop = atoi(optarg); break; case 'M': total_mac_addresses = atoi(optarg); break; case 'm': mstestlen = atoi(optarg); break; case 'r': should_test_range = 1; break; case 'p' : controller_port = atoi(optarg); break; case 's': n_fakeswitches = atoi(optarg); break; case 't': mode = MODE_THROUGHPUT; break; case 'w': warmup = atoi(optarg); break; case 'C': cooldown = atoi(optarg); break; case 'D': delay = atoi(optarg); break; case 'i': connect_delay = atoi(optarg); break; case 'I': connect_group_size = atoi(optarg); break; case 'o': dpid_offset = atoi(optarg); break; default: myargs_usage(my_options, PROG_TITLE, "help message", NULL, 1); } } if(warmup+cooldown >= tests_per_loop) { fprintf(stderr, "Error warmup(%d) + cooldown(%d) >= number of tests (%d)\n", warmup, cooldown, tests_per_loop); exit(1); } fprintf(stderr, "cbench: controller benchmarking tool\n" " running in mode %s\n" " connecting to controller at %s:%d \n" " faking%s %d switches :: %d tests each; %d ms per test\n" " with %d unique source MACs per switch\n" " %s destination mac addresses before the test\n" " starting test with %d ms delay after features_reply\n" " ignoring first %d \"warmup\" and last %d \"cooldown\" loops\n" " connection delay of %dms per %d switch(es)\n" " debugging info is %s\n", mode == MODE_THROUGHPUT? "'throughput'": "'latency'", controller_hostname, controller_port, should_test_range ? " from 1 to": "", n_fakeswitches, tests_per_loop, mstestlen, total_mac_addresses, learn_dst_macs ? "learning" : "NOT learning", delay, warmup,cooldown, connect_delay,connect_group_size, debug == 1 ? "on" : "off"); /* done parsing args */ fakeswitches = malloc(n_fakeswitches * sizeof(struct fakeswitch)); assert(fakeswitches); double *results; double min = DBL_MAX; double max = 0.0; double v; results = malloc(tests_per_loop * sizeof(double)); for( i = 0; i < n_fakeswitches; i++) { int sock; double sum = 0; if (connect_delay != 0 && i != 0 && (i % connect_group_size == 0)) { if(debug) fprintf(stderr,"Delaying connection by %dms...", connect_delay*1000); usleep(connect_delay*1000); } sock = make_tcp_connection(controller_hostname, controller_port,3000, mode!=MODE_THROUGHPUT ); if(sock < 0 ) { fprintf(stderr, "make_nonblock_tcp_connection :: returned %d", sock); exit(1); } if(debug) fprintf(stderr,"Initializing switch %d ... ", i+1); fflush(stderr); fakeswitch_init(&fakeswitches[i],dpid_offset+i,sock,65536, debug, delay, mode, total_mac_addresses, learn_dst_macs); if(debug) fprintf(stderr," :: done.\n"); fflush(stderr); if(count_bits(i+1) == 0) // only test for 1,2,4,8,16 switches continue; if(!should_test_range && ((i+1) != n_fakeswitches)) // only if testing range or this is last continue; for( j = 0; j < tests_per_loop; j ++) { if ( j > 0 ) delay = 0; // only delay on the first run v = 1000.0 * run_test(i+1, fakeswitches, mstestlen, delay); results[j] = v; if(j<warmup || j >= tests_per_loop-cooldown) continue; sum += v; if (v > max) max = v; if (v < min) min = v; } int counted_tests = (tests_per_loop - warmup - cooldown); // compute std dev double avg = sum / counted_tests; sum = 0.0; for (j = warmup; j < tests_per_loop-cooldown; ++j) { sum += pow(results[j] - avg, 2); } sum = sum / (double)(counted_tests); double std_dev = sqrt(sum); printf("RESULT: %d switches %d tests " "min/max/avg/stdev = %.2lf/%.2lf/%.2lf/%.2lf responses/s\n", i+1, counted_tests, min, max, avg, std_dev); } return 0; }