int main(int argc, char **argv) { cmd_args_t *args; int rc = 0; /* Argument init and parsing */ if ((args = args_init(argc, argv)) == NULL) { rc = -1; goto out; } /* Device specific init */ if ((rc = dev_init())) goto out; /* Generic kernel version string */ if (args->verbose) fprintf(stdout, "%s", zpios_version); print_stats_header(args); rc = run_thread_counts(args); out: if (args != NULL) args_fini(args); dev_fini(); return (rc); }
int main(int argc, char **argv) { struct addrinfo *addrs, *addr; char *host = "127.0.0.1"; char *port = "1337"; int rc; // for checking that the server's up char poke[SHA_LENGTH * 2]; tinymt64_t rando; if (parse_args(&cfg, &host, &port, argc, argv)) { usage(); exit(1); } struct addrinfo hints = { .ai_family = AF_UNSPEC, .ai_socktype = SOCK_STREAM }; if ((rc = getaddrinfo(host, port, &hints, &addrs)) != 0) { const char *msg = gai_strerror(rc); fprintf(stderr, "unable to resolve %s:%s: %s\n", host, port, msg); exit(1); } for (addr = addrs; addr != NULL; addr = addr->ai_next) { int fd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); if (fd == -1) continue; rc = connect(fd, addr->ai_addr, addr->ai_addrlen); tinymt64_init(&rando, time_us()); random_hash(&rando, poke); if (rc == 0 && write(fd, poke, SHA_LENGTH * 2) == SHA_LENGTH * 2) { read(fd, poke, SHA_LENGTH * 2); close(fd); break; } close(fd); } if (addr == NULL) { char *msg = strerror(errno); fprintf(stderr, "unable to connect to %s:%s: %s\n", host, port, msg); exit(1); } signal(SIGPIPE, SIG_IGN); signal(SIGINT, SIG_IGN); cfg.addr = *addr; pthread_mutex_init(&statistics.mutex, NULL); statistics.latency = stats_alloc(SAMPLES); statistics.requests = stats_alloc(SAMPLES); thread *threads = zcalloc(cfg.threads * sizeof(thread)); uint64_t connections = cfg.connections / cfg.threads; uint64_t stop_at = time_us() + (cfg.duration * 1000000); for (uint64_t i = 0; i < cfg.threads; i++) { thread *t = &threads[i]; t->connections = connections; t->stop_at = stop_at; if (pthread_create(&t->thread, NULL, &thread_main, t)) { char *msg = strerror(errno); fprintf(stderr, "unable to create thread %"PRIu64" %s\n", i, msg); exit(2); } } struct sigaction sa = { .sa_handler = handler, .sa_flags = 0, }; sigfillset(&sa.sa_mask); sigaction(SIGINT, &sa, NULL); char *time = format_time_s(cfg.duration); printf("Running %s test @ %s:%s\n", time, host, port); printf(" %"PRIu64" threads and %"PRIu64" connections\n", cfg.threads, cfg.connections); uint64_t start = time_us(); uint64_t complete = 0; uint64_t bytes = 0; errors errors = { 0 }; for (uint64_t i = 0; i < cfg.threads; i++) { thread *t = &threads[i]; pthread_join(t->thread, NULL); complete += t->complete; bytes += t->bytes; errors.connect += t->errors.connect; errors.handshake += t->errors.handshake; errors.read += t->errors.read; errors.validate += t->errors.validate; errors.write += t->errors.write; errors.timeout += t->errors.timeout; } uint64_t runtime_us = time_us() - start; long double runtime_s = runtime_us / 1000000.0; long double req_per_s = complete / runtime_s; long double bytes_per_s = bytes / runtime_s; print_stats_header(); print_stats("Latency", statistics.latency, format_time_us); print_stats("Req/Sec", statistics.requests, format_metric); if (cfg.latency) print_stats_latency(statistics.latency); char *runtime_msg = format_time_us(runtime_us); printf(" %"PRIu64" requests in %s, %sB read\n", complete, runtime_msg, format_binary(bytes)); if (errors.connect || errors.read || errors.write || errors.timeout) { printf(" Socket errors: connect %d, read %d, write %d, timeout %d\n", errors.connect, errors.read, errors.write, errors.timeout); } if (errors.handshake) { printf(" Bad handshakes from server: %d\n", errors.handshake); } if (errors.validate) { printf(" %d proofs failed verification.\n", errors.validate); } printf("Requests/sec: %9.2Lf\n", req_per_s); printf("Transfer/sec: %10sB\n", format_binary(bytes_per_s)); return 0; } void *thread_main(void *arg) { thread *thread = arg; aeEventLoop *loop = aeCreateEventLoop(10 + cfg.connections * 3); thread->cs = zmalloc(thread->connections * sizeof(connection)); thread->loop = loop; tinymt64_init(&thread->rand, time_us()); thread->latency = stats_alloc(100000); connection *c = thread->cs; for (uint64_t i = 0; i < thread->connections; i++, c++) { c->thread = thread; random_hash(&thread->rand, c->hash); connect_socket(thread, c); } aeCreateTimeEvent(loop, CALIBRATE_DELAY_MS, calibrate, thread, NULL); aeCreateTimeEvent(loop, TIMEOUT_INTERVAL_MS, check_timeouts, thread, NULL); thread->start = time_us(); aeMain(loop); aeDeleteEventLoop(loop); zfree(thread->cs); uint64_t max = thread->latency->max; stats_free(thread->latency); pthread_mutex_lock(&statistics.mutex); for (uint64_t i = 0; i < thread->missed; i++) { stats_record(statistics.latency, max); } pthread_mutex_unlock(&statistics.mutex); return NULL; }
int main(int argc, char *argv[]) { signal (SIGINT, signal_handler); //printf("\033[?1049h\033[H"); clear_log(); my_log("Software switch starting..."); my_log(pcap_lib_version()); int option = 0, ret; char c; pthread_t config_thread; char errbuf[PCAP_ERRBUF_SIZE]; p1 = create_port_struct(1); p2 = create_port_struct(2); while ((option = getopt(argc, argv,"h l 1:2: m")) != -1) { switch (option) { case '1' : strcpy(p1->name, optarg); break; case '2' : strcpy(p2->name, optarg); break; case 'l': list_interfaces(); exit(0); case 'm': mock_rule(); break; case 'h': default: print_usage(); exit(EXIT_FAILURE); } } p1->handle = pcap_create(p1->name, errbuf); if ( (ret = pcap_setdirection(p1->handle, PCAP_D_IN)) != 0){ printf("pcap_setdirection returned %i\n", ret); my_log("pcap_setdirection failed"); pcap_perror(p1->handle, 0); //exit(-1); } if ( pcap_set_promisc(p1->handle, 1) != 0){ printf("pcap_set_promisc returned \n%s\n", pcap_geterr(p1->handle)); my_log("pcap_set_promisc failed"); pcap_perror(p1->handle, 0); exit(-1); } if ( pcap_set_immediate_mode(p1->handle, 1) != 0){ printf("pcap_set_immediate_mode returned \n%s\n", pcap_geterr(p1->handle)); my_log("pcap_set_immediate_mode failed"); pcap_perror(p1->handle, 0); exit(-1); } if ( pcap_activate(p1->handle)){ printf("Failed to open interface %s\n", pcap_geterr(p1->handle)); exit(-1); } else { sprintf(log_b, "Handle activated for %s", p1->name); my_log(log_b); } p2->handle = pcap_create(p2->name, errbuf); if ( pcap_setdirection(p2->handle, PCAP_D_OUT) != 0){ my_log("pcap_setdirection failed"); pcap_perror(p2->handle, 0); //exit(-1); } if ( pcap_set_promisc(p2->handle, 1) != 0){ my_log("pcap_set_promisc failed"); pcap_perror(p2->handle, 0); exit(-1); } if ( pcap_set_immediate_mode(p2->handle, 1) != 0){ my_log("pcap_set_immediate_mode failed"); pcap_perror(p2->handle, 0); exit(-1); } if ( pcap_activate(p2->handle)){ printf("Failed to open interface %s\n", pcap_geterr(p2->handle)); exit(-1); } else { sprintf(log_b, "Handle activated for %s", p2->name); my_log(log_b); } //exit(0); my_log("Deleting mac table.."); clear_mac(); sprintf(log_b, "Default action is %s", (DEFAULT_ACTION == R_ALLOW)? "R_ALLOW" : "R_DENY"); my_log(log_b); my_log("Creating threads..."); pthread_mutex_init(&mutex, NULL); if ( pthread_create(&(p1->thread), 0, port_listener, (void *)p1) ){ my_log("Error creating p1 thread"); exit(-1); } if ( pthread_create(&(p2->thread), 0, port_listener, (void *)p2) ){ my_log("Error creating p2 thread"); exit(-1); } pthread_create(&config_thread, 0, config, 0); while (1) { mac_delete_old_entries(5); if(pause_rendering == 1) continue; // render here system("clear"); print_mac(); print_rules(); print_stats_header(); print_stats(p1->in, "1 IN"); print_stats(p1->out, "1 OUT"); print_stats(p2->in, "2 IN"); print_stats(p2->out, "2 OUT"); printf("p1in: %i\tp1out: %i\tp2in: %i\tp2out: %i\n", p1in, p1out, p2in, p2out); sleep(1); } pthread_join(config_thread, 0); pthread_join(p1->thread, 0); pthread_join(p2->thread, 0); //printf("\033[?1049l"); // go back return 0; }