From 22d31a872edd3ac621d34af25c157f6f14c7c6f2 Mon Sep 17 00:00:00 2001 From: Quentin Date: Mon, 23 Sep 2019 16:32:59 +0200 Subject: [PATCH 01/16] Rewrite measlat --- CMakeLists.txt | 2 + r/thunder_configure.R | 4 +- src/meas_lat.c | 130 ++++++++++-------------------------------- src/measure.c | 46 +++++++++++++++ src/measure.h | 22 +++++++ 5 files changed, 103 insertions(+), 101 deletions(-) create mode 100644 src/measure.c create mode 100644 src/measure.h diff --git a/CMakeLists.txt b/CMakeLists.txt index d4a3fa4..ec99347 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,6 +37,8 @@ list(APPEND CSOURCES src/capture_traffic.c src/cap_utils.h src/cap_utils.c + src/measure.h + src/measure.c ) add_executable(donar ${CSOURCES} src/donar.c) diff --git a/r/thunder_configure.R b/r/thunder_configure.R index 668208a..2daf8a5 100644 --- a/r/thunder_configure.R +++ b/r/thunder_configure.R @@ -3,7 +3,7 @@ library(sqldf) library(plyr) library(cowplot) -thunder_ms <- read.csv("thunder_23.csv") +thunder_ms <- read.csv("thunder_22.csv") thunder_ms <- sqldf("select run,ident,jmax,links,latency, CAST(latency as real) / 1000. as lat_ms from thunder_ms") thunder_ms$links <- as.factor(thunder_ms$links) thunder_ms$jmax <- as.factor(thunder_ms$jmax) @@ -61,7 +61,7 @@ thunder_bw$links <- as.factor(thunder_bw$links) v3 <- ggplot(data = thunder_bw, aes(x = jmax, y=sent_ratio, fill=links)) + geom_boxplot(outlier.size=0.1) + #scale_y_log10() + - coord_cartesian(ylim = c(1,2)) + + coord_cartesian(ylim = c(1,4)) + scale_fill_grey() + ylab("bandwidth ratio") + xlab("max allowed jitter") + diff --git a/src/meas_lat.c b/src/meas_lat.c index 43f9c13..4f15215 100644 --- a/src/meas_lat.c +++ b/src/meas_lat.c @@ -8,93 +8,33 @@ #include "net_tools.h" #include "socks5.h" #include "utils.h" +#include "measure.h" struct measlat_ctx { - int count, size, interval, verbose; + struct measure_conf mc; + int verbose; char *host, *port, *transport; }; -struct measure_conf { - uint64_t max_measure; - uint64_t payload_size; - char* payload; - uint64_t counter; -}; - -struct packet_header { - uint64_t counter; - struct timespec emit_time; -}; - -struct measure_conf* create_measure_conf(int max_mes, int plsize) { - struct measure_conf* mc = malloc(sizeof(struct measure_conf)); - if (mc == NULL) { - perror("Malloc failed"); - exit(EXIT_FAILURE); - } - - mc->counter = 0; - mc->max_measure = max_mes; - mc->payload_size = plsize; - mc->payload = malloc(mc->payload_size); - if (mc->payload == NULL) { - perror("malloc failed"); - exit(EXIT_FAILURE); - } - - return mc; -} - -void free_mesure_conf(void* v) { - struct measure_conf* mc = (struct measure_conf*)v; - free(mc->payload); - free(mc); -} - int on_udp_err(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { return 1; } int on_udp(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { ssize_t res; - uint64_t micro_sec; - struct timespec curr; - struct measure_conf* mc = fdinfo->other; - res = read(fdinfo->fd, mc->payload, mc->payload_size); + struct measlat_ctx* mctx = fdinfo->cat->app_ctx; + res = read(fdinfo->fd, mctx->mc.payload, mctx->mc.payload_size); if (res == -1 && errno == EAGAIN) return 1; - if (res != mc->payload_size) { - perror("read error"); - exit(EXIT_FAILURE); - } - struct packet_header* head = (struct packet_header*) mc->payload; - if (clock_gettime(CLOCK_MONOTONIC, &curr) == -1){ - perror("clock_gettime error"); - exit(EXIT_FAILURE); - } - time_t now; - time(&now); - char* ctime_no_newline = strtok(ctime(&now), "\n"); - micro_sec = elapsed_micros (&head->emit_time, &curr); - printf("[%s] Packet %llu latency %luµs\n", ctime_no_newline, (unsigned long long)head->counter, micro_sec); + measure_parse (res, &mctx->mc); - if (mc->max_measure > 0 && head->counter >= mc->max_measure) { - printf("Measurement done\n"); - exit(EXIT_SUCCESS); - } return 0; } -void free_timer_conf(void* v) { - struct measure_conf* mc = v; - free(mc->payload); - free(mc); -} - int on_timer(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { ssize_t s; uint64_t ticks = 0; - struct measure_conf* mc = fdinfo->other; + struct measlat_ctx* mctx = fdinfo->cat->app_ctx; s = read(fdinfo->fd, &ticks, sizeof(uint64_t)); if (s == -1 && errno == EAGAIN) return 1; @@ -106,23 +46,15 @@ int on_timer(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { if (ticks != 1) { fprintf(stderr, "Has ticked %lu times, expected 1 time. This is a bug\n", ticks); } - mc->counter++; + mctx->mc.counter++; - memset(mc->payload, 0, mc->payload_size); - struct packet_header* head = (struct packet_header*)mc->payload; - head->counter = mc->counter; + struct measure_packet* head = (struct measure_packet*)mctx->mc.payload; + head->counter = mctx->mc.counter; if (clock_gettime(CLOCK_MONOTONIC, &head->emit_time) == -1) { perror("clock_gettime error"); exit(EXIT_FAILURE); } - char *my_msg = "Tu n'es pas tout a fait la misere,\nCar les levres les plus pauvres te denoncent\nPar un sourire."; - size_t msg_len = strlen(my_msg); - size_t cursor_msg = 0; - for (size_t i = sizeof(struct packet_header); i < mc->payload_size; i++) { - mc->payload[i] = my_msg[cursor_msg]; - cursor_msg = (cursor_msg + 1) % msg_len; - } struct evt_core_fdinfo* tgtinfo = evt_core_get_first_from_cat (ctx, "udp-read"); if (tgtinfo == NULL) tgtinfo = evt_core_get_first_from_cat (ctx, "tcp-read"); @@ -130,7 +62,7 @@ int on_timer(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { printf("No connection yet\n"); return 1; } - s = send(tgtinfo->fd, mc->payload, mc->payload_size, 0); + s = send(tgtinfo->fd, mctx->mc.payload, mctx->mc.payload_size, 0); if (s < 0) { perror("Send error"); //exit(EXIT_FAILURE); @@ -139,7 +71,7 @@ int on_timer(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { } -void register_timer(struct evt_core_ctx* evts, int udp, int interval, int count, int size) { +void register_timer(struct evt_core_ctx* evts) { struct timespec now; struct itimerspec timer_config; char url[1024]; @@ -148,11 +80,18 @@ void register_timer(struct evt_core_ctx* evts, int udp, int interval, int count, fdinfo.cat = &cat; fdinfo.url = url; + struct evt_core_cat* ucat = evt_core_get_from_cat (evts, "tcp-read"); + if (ucat == NULL) { + fprintf(stderr, "Category udp-read not found\n"); + exit(EXIT_FAILURE); + } + struct measlat_ctx* mctx = ucat->app_ctx; + if (clock_gettime(CLOCK_REALTIME, &now) == -1) { perror("clock_gettime"); exit(EXIT_FAILURE); } - uint64_t micro_sec = interval; + uint64_t micro_sec = mctx->mc.interval; timer_config.it_value.tv_sec = now.tv_sec + 1; timer_config.it_value.tv_nsec = now.tv_nsec; timer_config.it_interval.tv_sec = micro_sec / 1000; @@ -168,9 +107,7 @@ void register_timer(struct evt_core_ctx* evts, int udp, int interval, int count, exit(EXIT_FAILURE); } fdinfo.cat->name = "timer"; - fdinfo.other = create_measure_conf(count, size); - fdinfo.free_other = free_timer_conf; - sprintf(fdinfo.url, "timer:%d:%d", interval, count); + sprintf(fdinfo.url, "timer:%ld:%ld", mctx->mc.interval, mctx->mc.max_measure); evt_core_add_fd (evts, &fdinfo); printf("--- Timer registered\n"); } @@ -192,14 +129,12 @@ int on_socks5_success_measlat(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo_n.fd = dup(fdinfo->fd); fdinfo_n.cat->name = "tcp-read"; - fdinfo_n.other = create_measure_conf (mctx->count, mctx->size); - fdinfo_n.free_other = free_mesure_conf; sprintf(fdinfo_n.url, "tcp:read:%s:%d", s5ctx->addr, s5ctx->port); evt_core_add_fd (ctx, &fdinfo_n); printf("--- Tor socket registered\n"); - register_timer(ctx, fdinfo->fd, mctx->interval, mctx->count, mctx->size); + register_timer(ctx); return 1; } @@ -277,13 +212,13 @@ void spawn_udp_socket(struct evt_core_ctx* evts) { fdinfo.fd = udp_sock; fdinfo.cat->name = "udp-read"; - fdinfo.other = create_measure_conf (mctx->count, mctx->size); - fdinfo.free_other = free_mesure_conf; + fdinfo.other = &mctx->mc; + fdinfo.free_other = NULL; sprintf(fdinfo.url, "udp:read:%s:%s", mctx->host, mctx->port); evt_core_add_fd (evts, &fdinfo); printf("--- UDP socket registered\n"); - register_timer(evts, fdinfo.fd, mctx->interval, mctx->count, mctx->size); + register_timer(evts); } int main(int argc, char** argv) { @@ -310,13 +245,13 @@ int main(int argc, char** argv) { mctx.transport = optarg; break; case 'c': // count - mctx.count = atoi(optarg); + mctx.mc.max_measure = atoi(optarg); break; case 's': // size - payload in bytes - mctx.size = atoi(optarg); + mctx.mc.payload_size = atoi(optarg); break; case 'i': // interval - every ms - mctx.interval = atoi(optarg); + mctx.mc.interval = atoi(optarg); break; default: goto usage; @@ -324,14 +259,11 @@ int main(int argc, char** argv) { } // 2. Check and fix parameters - size_t header_size = sizeof(struct packet_header); - if (mctx.interval <= 0) mctx.interval = 1000; - if (mctx.count <= 0) mctx.count = 1; - if (mctx.size < header_size) mctx.size = header_size; + measure_prepare (&mctx.mc); if (mctx.transport == NULL) mctx.transport = "udp"; if (mctx.host == NULL || mctx.port == NULL) goto usage; - printf("[measlat_conf] host=%s, port=%s, transport=%s, count=%d, size=%d, interval=%d\n", - mctx.host, mctx.port, mctx.transport, mctx.count, mctx.size, mctx.interval); + printf("[measlat_conf] host=%s, port=%s, transport=%s, count=%ld, size=%ld, interval=%ld\n", + mctx.host, mctx.port, mctx.transport, mctx.mc.max_measure, mctx.mc.payload_size, mctx.mc.interval); // 3. Bind events register_categories(&evts, &mctx); diff --git a/src/measure.c b/src/measure.c new file mode 100644 index 0000000..864e687 --- /dev/null +++ b/src/measure.c @@ -0,0 +1,46 @@ +#include "measure.h" + +void measure_parse(int size, struct measure_conf* mc) { + struct timespec curr; + uint64_t micro_sec; + if (size != mc->payload_size) { + perror("read error"); + exit(EXIT_FAILURE); + } + struct measure_packet* head = (struct measure_packet*) mc->payload; + if (clock_gettime(CLOCK_MONOTONIC, &curr) == -1){ + perror("clock_gettime error"); + exit(EXIT_FAILURE); + } + time_t now; + time(&now); + char* ctime_no_newline = strtok(ctime(&now), "\n"); + + micro_sec = elapsed_micros (&head->emit_time, &curr); + printf("[%s] Packet %llu latency %luµs\n", ctime_no_newline, (unsigned long long)head->counter, micro_sec); + + if (mc->max_measure > 0 && head->counter >= mc->max_measure) { + printf("Measurement done\n"); + exit(EXIT_SUCCESS); + } +} + +void measure_prepare(struct measure_conf* mc) { + if (mc->interval <= 0) mc->interval = 1000; + if (mc->max_measure <= 0) mc->max_measure = 1; + if (mc->payload_size < sizeof(struct measure_packet)) mc->payload_size = sizeof(struct measure_packet); + + if ((mc->payload = malloc(sizeof(char) * mc->payload_size)) == NULL) { + perror("payload malloc failed"); + exit(EXIT_FAILURE); + } + memset(mc->payload, 0, mc->payload_size); + + char *my_msg = "Tu n'es pas tout a fait la misere,\nCar les levres les plus pauvres te denoncent\nPar un sourire."; + size_t msg_len = strlen(my_msg); + size_t cursor_msg = 0; + for (size_t i = sizeof(struct measure_packet); i < mc->payload_size; i++) { + mc->payload[i] = my_msg[cursor_msg]; + cursor_msg = (cursor_msg + 1) % msg_len; + } +} diff --git a/src/measure.h b/src/measure.h new file mode 100644 index 0000000..1f97f77 --- /dev/null +++ b/src/measure.h @@ -0,0 +1,22 @@ +#include +#include +#include +#include +#include +#include "utils.h" + +struct measure_conf { + uint64_t max_measure; + uint64_t payload_size; + uint64_t interval; + char* payload; + uint64_t counter; +}; + +struct measure_packet { + uint64_t counter; + struct timespec emit_time; +}; + +void measure_parse(int size, struct measure_conf* mc); +void measure_prepare(struct measure_conf* mc); From 8b04b391b0bee30b081dc639f8428aa1c171b915 Mon Sep 17 00:00:00 2001 From: Quentin Date: Mon, 23 Sep 2019 17:00:02 +0200 Subject: [PATCH 02/16] Now able to log udpecho OWD --- src/measure.c | 2 +- src/measure.h | 1 + src/udp_echo.c | 32 +++++++++++++++++++++++++------- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/measure.c b/src/measure.c index 864e687..060f58d 100644 --- a/src/measure.c +++ b/src/measure.c @@ -19,7 +19,7 @@ void measure_parse(int size, struct measure_conf* mc) { micro_sec = elapsed_micros (&head->emit_time, &curr); printf("[%s] Packet %llu latency %luµs\n", ctime_no_newline, (unsigned long long)head->counter, micro_sec); - if (mc->max_measure > 0 && head->counter >= mc->max_measure) { + if (!mc->inhibit_exit && head->counter >= mc->max_measure) { printf("Measurement done\n"); exit(EXIT_SUCCESS); } diff --git a/src/measure.h b/src/measure.h index 1f97f77..51ff873 100644 --- a/src/measure.h +++ b/src/measure.h @@ -11,6 +11,7 @@ struct measure_conf { uint64_t interval; char* payload; uint64_t counter; + uint8_t inhibit_exit; }; struct measure_packet { diff --git a/src/udp_echo.c b/src/udp_echo.c index df887cc..9294f41 100644 --- a/src/udp_echo.c +++ b/src/udp_echo.c @@ -4,22 +4,30 @@ #include #include "evt_core.h" #include "net_tools.h" +#include "measure.h" + +struct udpecho_ctx { + struct measure_conf mc; + uint8_t is_measlat; +}; int on_udp(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { - char buffer[1500]; ssize_t nread, nwritten; struct sockaddr_in addr; socklen_t addrlen = sizeof(addr); + struct udpecho_ctx *uctx = fdinfo->cat->app_ctx; - nread = recvfrom(fdinfo->fd, buffer, sizeof(buffer), MSG_TRUNC, (struct sockaddr*)&addr, &addrlen); + nread = recvfrom(fdinfo->fd, uctx->mc.payload, uctx->mc.payload_size, MSG_TRUNC, (struct sockaddr*)&addr, &addrlen); if (nread == -1 && errno == EAGAIN) return 1; // Read done - if (nread <= 0 || nread > sizeof(buffer)) { - fprintf(stderr, "Message is either truncated or an error occured. nread=%ld\n", nread); + if (nread <= 0 || nread > uctx->mc.payload_size) { + fprintf(stderr, "Message is either truncated or an error occured. nread=%ld, expected=%ld\n", nread, uctx->mc.payload_size); perror("read errno"); exit(EXIT_FAILURE); } - nwritten = sendto(fdinfo->fd, buffer, nread, 0, (struct sockaddr*)&addr, addrlen); + if (uctx->is_measlat) measure_parse (nread, &uctx->mc); + + nwritten = sendto(fdinfo->fd, uctx->mc.payload, nread, 0, (struct sockaddr*)&addr, addrlen); // @FIXME don't support EAGAIN on write. Could be intended, you don't think so? if (nwritten != nread) { fprintf(stderr, "Didn't write the same number of bytes as read. nread=%ld, nwritten=%ld\n", nread, nwritten); @@ -33,12 +41,15 @@ int main(int argc, char** argv) { setvbuf(stdout, NULL, _IONBF, 0); printf("~ udpecho ~\n"); + struct udpecho_ctx uctx = {0}; int opt, udp_sock, verbose = 0; char *port = NULL, *bindhost = NULL; struct evt_core_ctx evts = {0}; + uctx.mc.inhibit_exit = 1; + uctx.mc.payload_size = 1500; // 1. Parse parameters - while ((opt = getopt(argc, argv, "b:p:v")) != -1) { + while ((opt = getopt(argc, argv, "b:p:vms:")) != -1) { switch(opt) { case 'v': verbose++; @@ -49,15 +60,22 @@ int main(int argc, char** argv) { case 'b': bindhost = optarg; break; + case 'm': + uctx.is_measlat = 1; + break; + case 's': + uctx.mc.payload_size = atoi(optarg); + break; default: goto usage; } } if (bindhost == NULL) bindhost = "127.0.0.1"; + measure_prepare (&uctx.mc); // 2. Register category struct evt_core_cat udp_read = { - .app_ctx = NULL, + .app_ctx = &uctx, .free_app_ctx = NULL, .cb = on_udp, .err_cb = NULL, From c919367e35087f7094c3030f27e337a18a0111ec Mon Sep 17 00:00:00 2001 From: Quentin Date: Mon, 23 Sep 2019 17:19:03 +0200 Subject: [PATCH 03/16] Add tor echo logging feature --- src/tor_echo.c | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/src/tor_echo.c b/src/tor_echo.c index f015ece..2572f96 100644 --- a/src/tor_echo.c +++ b/src/tor_echo.c @@ -6,6 +6,12 @@ #include "evt_core.h" #include "net_tools.h" #include "url.h" +#include "measure.h" + +struct torecho_ctx { + uint8_t is_measlat; + struct measure_conf mc; +}; void te_create_onion_services(struct tor_os_str* tos, struct tor_ctl* tctl, uint16_t* ports, int ports_count, enum TOR_ONION_FLAGS tof) { tor_os_create (tos, "onion_services.pub", "onion_services.txt", ports_count); @@ -55,21 +61,23 @@ co_error: } int te_on_tcp(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { - char buffer[1500]; ssize_t nread, nwritten; struct sockaddr_in addr; socklen_t addrlen = sizeof(addr); + struct torecho_ctx *tctx = fdinfo->cat->app_ctx; - nread = recv(fdinfo->fd, buffer, sizeof(buffer), 0); + nread = recv(fdinfo->fd, tctx->mc.payload, tctx->mc.payload_size, 0); if (nread == -1 && errno == EAGAIN) return 1; // Read done if (nread == 0) { fprintf(stderr, "WARN! Read 0 bytes.\n"); return 1; } - if (nread < 0 || nread > sizeof(buffer)) { - fprintf(stderr, "Message is either truncated or an error occured. nread=%ld\n", nread); + if (nread < 0 || nread > tctx->mc.payload_size) { + fprintf(stderr, "Message is either truncated or an error occured. nread=%ld, expected=%ld\n", nread, tctx->mc.payload_size); perror("read errno"); exit(EXIT_FAILURE); } - nwritten = send(fdinfo->fd, buffer, nread, 0); + if (tctx->is_measlat) measure_parse (nread, &tctx->mc); + + nwritten = send(fdinfo->fd, tctx->mc.payload, nread, 0); // @FIXME don't support EAGAIN on write. Could be intended, you don't think so? if (nwritten != nread) { fprintf(stderr, "Didn't write the same number of bytes as read - not supported. nread=%ld, nwritten=%ld\n", nread, nwritten); @@ -91,13 +99,21 @@ int main(int argc, char** argv) { struct tor_ctl tctl; enum TOR_ONION_FLAGS tof = TOR_ONION_FLAG_NONE; char url[1024]; + struct torecho_ctx tctx = {0}; + tctx.mc.inhibit_exit = 1; + tctx.mc.payload_size = 1500; - - while ((opt = getopt(argc, argv, "n")) != -1) { + while ((opt = getopt(argc, argv, "ns:m")) != -1) { switch(opt) { case 'n': tof |= TOR_ONION_FLAG_NON_ANONYMOUS; break; + case 'm': + tctx.is_measlat = 1; + break; + case 's': + tctx.mc.payload_size = atoi(optarg); + break; default: break; } @@ -105,7 +121,7 @@ int main(int argc, char** argv) { // 1. Register categories struct evt_core_cat tcp_co = { - .app_ctx = NULL, + .app_ctx = &tctx, .free_app_ctx = NULL, .cb = te_on_tcp_co, .err_cb = NULL, @@ -114,7 +130,7 @@ int main(int argc, char** argv) { .socklist = NULL }; struct evt_core_cat tcp_all = { - .app_ctx = NULL, + .app_ctx = &tctx, .free_app_ctx = NULL, .cb = te_on_tcp, .err_cb = NULL, @@ -122,6 +138,8 @@ int main(int argc, char** argv) { .flags = EPOLLIN | EPOLLOUT | EPOLLET | EPOLLRDHUP, .socklist = NULL }; + measure_prepare (&tctx.mc); + evt_core_init(&evts, 0); evt_core_add_cat(&evts, &tcp_co); evt_core_add_cat(&evts, &tcp_all); From 3ebee4ff2e0c40fc0dbda5dd91ed28d1ca9a5ed3 Mon Sep 17 00:00:00 2001 From: Quentin Date: Mon, 23 Sep 2019 17:34:37 +0200 Subject: [PATCH 04/16] Upgrade scripts to print OWD --- scripts/Makefile | 286 ++++++++++++++++++------------------- scripts/dup2-server | 6 +- scripts/orig-server | 6 +- scripts/orig-server-single | 6 +- scripts/thunder-server | 6 +- 5 files changed, 155 insertions(+), 155 deletions(-) diff --git a/scripts/Makefile b/scripts/Makefile index df9cfa8..3ab4e70 100644 --- a/scripts/Makefile +++ b/scripts/Makefile @@ -4,7 +4,7 @@ simple_test: ./run-3 \ 1,simple_test . \ thunder-client 600 100 100 8 150 . \ - thunder-server 8 150 . \ + thunder-server 8 150 100. \ tor3 -f /etc/torrc_simple . \ tor3 -f /etc/torrc_simple @@ -15,10 +15,10 @@ tor_bw_small: orig-client 120 500 100, \ orig-client 600 100 100, \ orig-client 1200 50 100 . \ - orig-server, \ - orig-server, \ - orig-server, \ - orig-server . \ + orig-server 100, \ + orig-server 100, \ + orig-server 100, \ + orig-server 100. \ tor3 -f /etc/torrc_simple, \ tor3 -f /etc/torrc_simple, \ tor3 -f /etc/torrc_simple, \ @@ -40,11 +40,11 @@ tor_bw_full: orig-client 6000 100 100, \ orig-client 12000 50 100, \ orig-client 30000 20 100 . \ - orig-server, \ - orig-server, \ - orig-server, \ - orig-server, \ - orig-server . \ + orig-server 100, \ + orig-server 100, \ + orig-server 100, \ + orig-server 100, \ + orig-server 100. \ tor3 -f /etc/torrc_simple, \ tor3 -f /etc/torrc_simple, \ tor3 -f /etc/torrc_simple, \ @@ -67,10 +67,10 @@ tor_guards_small: dup2-client 600 100 100 8, \ dup2-client 600 100 100 8, \ dup2-client 600 100 100 8 . \ - dup2-server 8, \ - dup2-server 8, \ - dup2-server 8, \ - dup2-server 8 . \ + dup2-server 8 100, \ + dup2-server 8 100, \ + dup2-server 8 100, \ + dup2-server 8 100. \ tor3 -f /etc/torrc_guard_1, \ tor3 -f /etc/torrc_guard_3, \ tor3 -f /etc/torrc_guard_5, \ @@ -92,15 +92,15 @@ tor_guards_full: dup2-client 6000 100 100 8, \ dup2-client 6000 100 100 8, \ dup2-client 6000 100 100 8 . \ - dup2-server 8, \ - dup2-server 8, \ - dup2-server 8, \ - dup2-server 8, \ - dup2-server 8, \ - dup2-server 8, \ - dup2-server 8, \ - dup2-server 8, \ - dup2-server 8 . \ + dup2-server 8 100, \ + dup2-server 8 100, \ + dup2-server 8 100, \ + dup2-server 8 100, \ + dup2-server 8 100, \ + dup2-server 8 100, \ + dup2-server 8 100, \ + dup2-server 8 100, \ + dup2-server 8 100. \ tor3 -f /etc/torrc_guard_1, \ tor3 -f /etc/torrc_guard_2, \ tor3 -f /etc/torrc_guard_3, \ @@ -130,9 +130,9 @@ tor_relays_full: orig-client 6000 100 100, \ orig-client 6000 100 100, \ orig-client 6000 100 100 . \ - orig-server, \ - orig-server, \ - orig-server-single . \ + orig-server 100, \ + orig-server 100, \ + orig-server-single 100. \ tor3 -f /etc/torrc_simple, \ tor2 -f /etc/torrc_simple, \ tor2 -f /etc/torrc_simple . \ @@ -146,9 +146,9 @@ tor_relays_small: orig-client 600 100 100, \ orig-client 600 100 100, \ orig-client 600 100 100 . \ - orig-server, \ - orig-server, \ - orig-server-single . \ + orig-server 100, \ + orig-server 100, \ + orig-server-single 100. \ tor3 -f /etc/torrc_simple, \ tor2 -f /etc/torrc_simple, \ tor2 -f /etc/torrc_simple . \ @@ -170,13 +170,13 @@ thunder_configure_small: thunder-client 600 100 100 $(link_num) 250, \ thunder-client 600 100 100 $(link_num) 300, \ thunder-client 600 100 100 $(link_num) 350. \ - thunder-server $(link_num) 50, \ - thunder-server $(link_num) 100, \ - thunder-server $(link_num) 150, \ - thunder-server $(link_num) 200, \ - thunder-server $(link_num) 250, \ - thunder-server $(link_num) 300, \ - thunder-server $(link_num) 350. \ + thunder-server $(link_num) 50 100, \ + thunder-server $(link_num) 100 100, \ + thunder-server $(link_num) 150 100, \ + thunder-server $(link_num) 200 100, \ + thunder-server $(link_num) 250 100, \ + thunder-server $(link_num) 300 100, \ + thunder-server $(link_num) 350 100. \ tor2 -f /etc/torrc_guard_2, \ tor2 -f /etc/torrc_guard_2, \ tor2 -f /etc/torrc_guard_2, \ @@ -202,13 +202,13 @@ thunder_configure_full: thunder-client 6000 100 100 $(link_num) 250, \ thunder-client 6000 100 100 $(link_num) 300, \ thunder-client 6000 100 100 $(link_num) 350. \ - thunder-server $(link_num) 50, \ - thunder-server $(link_num) 100, \ - thunder-server $(link_num) 150, \ - thunder-server $(link_num) 200, \ - thunder-server $(link_num) 250, \ - thunder-server $(link_num) 300, \ - thunder-server $(link_num) 350. \ + thunder-server $(link_num) 50 100, \ + thunder-server $(link_num) 100 100, \ + thunder-server $(link_num) 150 100, \ + thunder-server $(link_num) 200 100, \ + thunder-server $(link_num) 250 100, \ + thunder-server $(link_num) 300 100, \ + thunder-server $(link_num) 350 100. \ tor2 -f /etc/torrc_guard_2, \ tor2 -f /etc/torrc_guard_2, \ tor2 -f /etc/torrc_guard_2, \ @@ -237,12 +237,12 @@ thunder_configure_2_part: thunder-client 9900 30 100 8 ${jmax}, \ thunder-client 9900 30 100 12 ${jmax}, \ thunder-client 9900 30 100 16 ${jmax}. \ - thunder-server 2 ${jmax}, \ - thunder-server 4 ${jmax}, \ - thunder-server 6 ${jmax}, \ - thunder-server 8 ${jmax}, \ - thunder-server 12 ${jmax}, \ - thunder-server 16 ${jmax}. \ + thunder-server 2 ${jmax} 100, \ + thunder-server 4 ${jmax} 100, \ + thunder-server 6 ${jmax} 100, \ + thunder-server 8 ${jmax} 100, \ + thunder-server 12 ${jmax} 100, \ + thunder-server 16 ${jmax} 100. \ tor2 -f /etc/torrc_simple, \ tor2 -f /etc/torrc_simple, \ tor2 -f /etc/torrc_simple, \ @@ -301,48 +301,48 @@ thunder_configure_2_full: thunder-client 9900 30 100 8 450, \ thunder-client 9900 30 100 8 550, \ thunder-client 9900 30 100 8 650. \ - thunder-server 2 50, \ - thunder-server 2 150, \ - thunder-server 2 250, \ - thunder-server 2 350, \ - thunder-server 2 450, \ - thunder-server 2 550, \ - thunder-server 2 650, \ - thunder-server 4 50, \ - thunder-server 4 150, \ - thunder-server 4 250, \ - thunder-server 4 350, \ - thunder-server 4 450, \ - thunder-server 4 550, \ - thunder-server 4 650, \ - thunder-server 6 50, \ - thunder-server 6 150, \ - thunder-server 6 250, \ - thunder-server 6 350, \ - thunder-server 6 450, \ - thunder-server 6 550, \ - thunder-server 6 650, \ - thunder-server 12 50, \ - thunder-server 12 150, \ - thunder-server 12 250, \ - thunder-server 12 350, \ - thunder-server 12 450, \ - thunder-server 12 550, \ - thunder-server 12 650, \ - thunder-server 16 50, \ - thunder-server 16 150, \ - thunder-server 16 250, \ - thunder-server 16 350, \ - thunder-server 16 450, \ - thunder-server 16 550, \ - thunder-server 16 650, \ - thunder-server 8 50, \ - thunder-server 8 150, \ - thunder-server 8 250, \ - thunder-server 8 350, \ - thunder-server 8 450, \ - thunder-server 8 550, \ - thunder-server 8 650. \ + thunder-server 2 50 100, \ + thunder-server 2 150 100, \ + thunder-server 2 250 100, \ + thunder-server 2 350 100, \ + thunder-server 2 450 100, \ + thunder-server 2 550 100, \ + thunder-server 2 650 100, \ + thunder-server 4 50 100, \ + thunder-server 4 150 100, \ + thunder-server 4 250 100, \ + thunder-server 4 350 100, \ + thunder-server 4 450 100, \ + thunder-server 4 550 100, \ + thunder-server 4 650 100, \ + thunder-server 6 50 100, \ + thunder-server 6 150 100, \ + thunder-server 6 250 100, \ + thunder-server 6 350 100, \ + thunder-server 6 450 100, \ + thunder-server 6 550 100, \ + thunder-server 6 650 100, \ + thunder-server 12 50 100, \ + thunder-server 12 150 100, \ + thunder-server 12 250 100, \ + thunder-server 12 350 100, \ + thunder-server 12 450 100, \ + thunder-server 12 550 100, \ + thunder-server 12 650 100, \ + thunder-server 16 50 100, \ + thunder-server 16 150 100, \ + thunder-server 16 250 100, \ + thunder-server 16 350 100, \ + thunder-server 16 450 100, \ + thunder-server 16 550 100, \ + thunder-server 16 650 100, \ + thunder-server 8 50 100, \ + thunder-server 8 150 100, \ + thunder-server 8 250 100, \ + thunder-server 8 350 100, \ + thunder-server 8 450 100, \ + thunder-server 8 550 100, \ + thunder-server 8 650 100. \ tor2 -f /etc/torrc_simple, \ tor2 -f /etc/torrc_simple, \ tor2 -f /etc/torrc_simple, \ @@ -495,46 +495,46 @@ tor_just_many_latencies: orig-client 9900 30 100, \ orig-client 9900 30 100, \ orig-client 9900 30 100 . \ - orig-server, \ - orig-server, \ - orig-server, \ - orig-server, \ - orig-server, \ - orig-server, \ - orig-server, \ - orig-server, \ - orig-server, \ - orig-server, \ - orig-server, \ - orig-server, \ - orig-server, \ - orig-server, \ - orig-server, \ - orig-server, \ - orig-server, \ - orig-server, \ - orig-server, \ - orig-server, \ - orig-server, \ - orig-server, \ - orig-server, \ - orig-server, \ - orig-server, \ - orig-server, \ - orig-server, \ - orig-server, \ - orig-server, \ - orig-server, \ - orig-server, \ - orig-server, \ - orig-server, \ - orig-server, \ - orig-server, \ - orig-server, \ - orig-server, \ - orig-server, \ - orig-server, \ - orig-server . \ + orig-server 100, \ + orig-server 100, \ + orig-server 100, \ + orig-server 100, \ + orig-server 100, \ + orig-server 100, \ + orig-server 100, \ + orig-server 100, \ + orig-server 100, \ + orig-server 100, \ + orig-server 100, \ + orig-server 100, \ + orig-server 100, \ + orig-server 100, \ + orig-server 100, \ + orig-server 100, \ + orig-server 100, \ + orig-server 100, \ + orig-server 100, \ + orig-server 100, \ + orig-server 100, \ + orig-server 100, \ + orig-server 100, \ + orig-server 100, \ + orig-server 100, \ + orig-server 100, \ + orig-server 100, \ + orig-server 100, \ + orig-server 100, \ + orig-server 100, \ + orig-server 100, \ + orig-server 100, \ + orig-server 100, \ + orig-server 100, \ + orig-server 100, \ + orig-server 100, \ + orig-server 100, \ + orig-server 100, \ + orig-server 100, \ + orig-server 100. \ tor3 -f /etc/torrc_simple, \ tor3 -f /etc/torrc_simple, \ tor3 -f /etc/torrc_simple, \ @@ -659,18 +659,18 @@ thunder_guards: thunder-client 9900 30 100 12 250, \ thunder-client 9900 30 100 12 250, \ thunder-client 9900 30 100 12 250. \ - thunder-server 12 250, \ - thunder-server 12 250, \ - thunder-server 12 250, \ - thunder-server 12 250, \ - thunder-server 12 250, \ - thunder-server 12 250, \ - thunder-server 12 250, \ - thunder-server 12 250, \ - thunder-server 12 250, \ - thunder-server 12 250, \ - thunder-server 12 250, \ - thunder-server 12 250. \ + thunder-server 12 250 100, \ + thunder-server 12 250 100, \ + thunder-server 12 250 100, \ + thunder-server 12 250 100, \ + thunder-server 12 250 100, \ + thunder-server 12 250 100, \ + thunder-server 12 250 100, \ + thunder-server 12 250 100, \ + thunder-server 12 250 100, \ + thunder-server 12 250 100, \ + thunder-server 12 250 100, \ + thunder-server 12 250 100. \ tor2 -f /etc/torrc_guard_2, \ tor2 -f /etc/torrc_guard_4, \ tor2 -f /etc/torrc_guard_6, \ diff --git a/scripts/dup2-server b/scripts/dup2-server index 6272c17..b67b4f3 100755 --- a/scripts/dup2-server +++ b/scripts/dup2-server @@ -1,6 +1,6 @@ #!/bin/bash -mkdir -p $2/{log,shared} -cd $2/shared +mkdir -p $3/{log,shared} +cd $3/shared donar \ -a dup2 \ @@ -10,4 +10,4 @@ donar \ -e 9000 \ > ../log/server-donar-stdout.log 2> ../log/server-donar-stderr.log & -udpecho -p 9000 > ../log/server-udpecho-stdout.log 2> ../log/server-udpecho-stderr.log +udpecho -p 9000 -m -s $2 > ../log/server-udpecho-stdout.log 2> ../log/server-udpecho-stderr.log diff --git a/scripts/orig-server b/scripts/orig-server index dd3cbf5..986ec9f 100755 --- a/scripts/orig-server +++ b/scripts/orig-server @@ -1,4 +1,4 @@ #!/bin/bash -mkdir -p $1/{log,shared} -cd $1/shared -torecho > ../log/server-udpecho-stdout.log 2> ../log/server-udpecho-stderr.log +mkdir -p $2/{log,shared} +cd $2/shared +torecho -m -s $1 > ../log/server-udpecho-stdout.log 2> ../log/server-udpecho-stderr.log diff --git a/scripts/orig-server-single b/scripts/orig-server-single index f785c38..1284c2e 100755 --- a/scripts/orig-server-single +++ b/scripts/orig-server-single @@ -1,4 +1,4 @@ #!/bin/bash -mkdir -p $1/{log,shared} -cd $1/shared -torecho -n > ../log/server-udpecho-stdout.log 2> ../log/server-udpecho-stderr.log +mkdir -p $2/{log,shared} +cd $2/shared +torecho -n -m -s $1 > ../log/server-udpecho-stdout.log 2> ../log/server-udpecho-stderr.log diff --git a/scripts/thunder-server b/scripts/thunder-server index e0a60e3..f7104c5 100755 --- a/scripts/thunder-server +++ b/scripts/thunder-server @@ -1,6 +1,6 @@ #!/bin/bash -mkdir -p $3/{log,shared} -cd $3/shared +mkdir -p $4/{log,shared} +cd $4/shared donar \ -a thunder \ @@ -11,4 +11,4 @@ donar \ -e 9000 \ > ../log/server-donar-stdout.log 2> ../log/server-donar-stderr.log & -udpecho -p 9000 > ../log/server-udpecho-stdout.log 2> ../log/server-udpecho-stderr.log +udpecho -p 9000 -m -s $3 > ../log/server-udpecho-stdout.log 2> ../log/server-udpecho-stderr.log From 55a09eaa3f1e877417a7b352b3bf608b25077978 Mon Sep 17 00:00:00 2001 From: Quentin Date: Mon, 23 Sep 2019 17:36:40 +0200 Subject: [PATCH 05/16] Parameterize many latencies --- scripts/Makefile | 80 ++++++++++++++++++++++++------------------------ 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/scripts/Makefile b/scripts/Makefile index 3ab4e70..cc7ead2 100644 --- a/scripts/Makefile +++ b/scripts/Makefile @@ -455,46 +455,46 @@ thunder_configure_2_full_parse_red: tor_just_many_latencies: ./run-3 \ 2,tor_just_many_latencies . \ - orig-client 9900 30 100, \ - orig-client 9900 30 100, \ - orig-client 9900 30 100, \ - orig-client 9900 30 100, \ - orig-client 9900 30 100, \ - orig-client 9900 30 100, \ - orig-client 9900 30 100, \ - orig-client 9900 30 100, \ - orig-client 9900 30 100, \ - orig-client 9900 30 100, \ - orig-client 9900 30 100, \ - orig-client 9900 30 100, \ - orig-client 9900 30 100, \ - orig-client 9900 30 100, \ - orig-client 9900 30 100, \ - orig-client 9900 30 100, \ - orig-client 9900 30 100, \ - orig-client 9900 30 100, \ - orig-client 9900 30 100, \ - orig-client 9900 30 100, \ - orig-client 9900 30 100, \ - orig-client 9900 30 100, \ - orig-client 9900 30 100, \ - orig-client 9900 30 100, \ - orig-client 9900 30 100, \ - orig-client 9900 30 100, \ - orig-client 9900 30 100, \ - orig-client 9900 30 100, \ - orig-client 9900 30 100, \ - orig-client 9900 30 100, \ - orig-client 9900 30 100, \ - orig-client 9900 30 100, \ - orig-client 9900 30 100, \ - orig-client 9900 30 100, \ - orig-client 9900 30 100, \ - orig-client 9900 30 100, \ - orig-client 9900 30 100, \ - orig-client 9900 30 100, \ - orig-client 9900 30 100, \ - orig-client 9900 30 100 . \ + orig-client ${count} ${interval} 100, \ + orig-client ${count} ${interval} 100, \ + orig-client ${count} ${interval} 100, \ + orig-client ${count} ${interval} 100, \ + orig-client ${count} ${interval} 100, \ + orig-client ${count} ${interval} 100, \ + orig-client ${count} ${interval} 100, \ + orig-client ${count} ${interval} 100, \ + orig-client ${count} ${interval} 100, \ + orig-client ${count} ${interval} 100, \ + orig-client ${count} ${interval} 100, \ + orig-client ${count} ${interval} 100, \ + orig-client ${count} ${interval} 100, \ + orig-client ${count} ${interval} 100, \ + orig-client ${count} ${interval} 100, \ + orig-client ${count} ${interval} 100, \ + orig-client ${count} ${interval} 100, \ + orig-client ${count} ${interval} 100, \ + orig-client ${count} ${interval} 100, \ + orig-client ${count} ${interval} 100, \ + orig-client ${count} ${interval} 100, \ + orig-client ${count} ${interval} 100, \ + orig-client ${count} ${interval} 100, \ + orig-client ${count} ${interval} 100, \ + orig-client ${count} ${interval} 100, \ + orig-client ${count} ${interval} 100, \ + orig-client ${count} ${interval} 100, \ + orig-client ${count} ${interval} 100, \ + orig-client ${count} ${interval} 100, \ + orig-client ${count} ${interval} 100, \ + orig-client ${count} ${interval} 100, \ + orig-client ${count} ${interval} 100, \ + orig-client ${count} ${interval} 100, \ + orig-client ${count} ${interval} 100, \ + orig-client ${count} ${interval} 100, \ + orig-client ${count} ${interval} 100, \ + orig-client ${count} ${interval} 100, \ + orig-client ${count} ${interval} 100, \ + orig-client ${count} ${interval} 100, \ + orig-client ${count} ${interval} 100 . \ orig-server 100, \ orig-server 100, \ orig-server 100, \ From bda1b07b01d3e8c7a167e9d63ad302aef458a311 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Mon, 23 Sep 2019 15:37:24 +0000 Subject: [PATCH 06/16] Activate scheduler --- scripts/thunder-client | 2 +- scripts/thunder-server | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/thunder-client b/scripts/thunder-client index 6705735..0f6075a 100755 --- a/scripts/thunder-client +++ b/scripts/thunder-client @@ -5,7 +5,7 @@ donar \ -c \ -o $6/shared/onion_services.pub \ -l $4 \ - -p jitter=$5 \ + -p jitter=$5,scheduler=1 \ -b \ -r 9000 \ > $6/log/client-donar-stdout.log 2> $6/log/client-donar-stderr.log & diff --git a/scripts/thunder-server b/scripts/thunder-server index e0a60e3..0d28438 100755 --- a/scripts/thunder-server +++ b/scripts/thunder-server @@ -6,7 +6,7 @@ donar \ -a thunder \ -s \ -l $1 \ - -p jitter=$2 \ + -p jitter=$2,scheduler=1 \ -b \ -e 9000 \ > ../log/server-donar-stdout.log 2> ../log/server-donar-stderr.log & From 9d8288e2980304966383c5a758bda9825a7187f0 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Mon, 23 Sep 2019 20:08:04 +0200 Subject: [PATCH 07/16] deltams --- scripts/deltams.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100755 scripts/deltams.py diff --git a/scripts/deltams.py b/scripts/deltams.py new file mode 100755 index 0000000..cac13fe --- /dev/null +++ b/scripts/deltams.py @@ -0,0 +1,31 @@ +#!/usr/bin/python3 +import sys,re + +owd = f"./out/{sys.argv[1]}/log/server-udpecho-stdout.log" +rtt = f"./out/{sys.argv[1]}/res/{sys.argv[2]}.csv" + +agg = {} +last_pkt = 0 + +with open(owd, 'r') as f: + for line in f: + res = re.match(r".*Packet (\d+) latency (\d+)µs", line) + if not res: continue + pkt_id,lat = res.groups() + pkt_id,lat = int(pkt_id), int(lat) + agg[pkt_id] = {"owd": lat } + +with open(rtt, 'r') as f: + for line in f: + res = re.match(r".*Packet (\d+) latency (\d+)µs", line) + if not res: continue + pkt_id,lat = res.groups() + pkt_id,lat = int(pkt_id), int(lat) + agg[pkt_id]["rtt"] = lat + last_pkt = pkt_id + +for i in range(last_pkt): + if i not in agg: continue + if "rtt" not in agg[i]: continue + + print(abs((agg[i]["rtt"] - 2*agg[i]["owd"]) / 1000), agg[i]["owd"] / 1000, (agg[i]["rtt"] - agg[i]["owd"]) / 1000, i) From e1f96bd40c94b8bff7cdd753db564b8afb13e83c Mon Sep 17 00:00:00 2001 From: Quentin Date: Tue, 24 Sep 2019 15:37:12 +0200 Subject: [PATCH 08/16] Upgrade measlat --- src/meas_lat.c | 270 +++++++++++++++++++++++++++++++++++-------------- src/measure.c | 51 +++++++++- src/measure.h | 7 +- src/tor_echo.c | 4 +- src/udp_echo.c | 2 +- 5 files changed, 253 insertions(+), 81 deletions(-) diff --git a/src/meas_lat.c b/src/meas_lat.c index 4f15215..a97a637 100644 --- a/src/meas_lat.c +++ b/src/meas_lat.c @@ -9,28 +9,133 @@ #include "socks5.h" #include "utils.h" #include "measure.h" +#include "url.h" struct measlat_ctx { struct measure_conf mc; - int verbose; + struct sockaddr_in addr; + socklen_t addrlen; + int verbose, is_timer_started, is_from_needed; char *host, *port, *transport; }; -int on_udp_err(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { +void register_timer(struct evt_core_ctx* evts, struct timespec* next_tick) { + struct timespec now; + struct itimerspec timer_config; + char url[1024]; + struct evt_core_cat cat = {0}; + struct evt_core_fdinfo fdinfo = {0}; + fdinfo.cat = &cat; + fdinfo.url = url; + + struct evt_core_cat* ucat = evt_core_get_from_cat (evts, "tcp-read"); + if (ucat == NULL) { + fprintf(stderr, "Category udp-read not found\n"); + exit(EXIT_FAILURE); + } + struct measlat_ctx* mctx = ucat->app_ctx; + mctx->is_timer_started = 1; + + if (clock_gettime(CLOCK_REALTIME, &now) == -1) { + perror("clock_gettime"); + exit(EXIT_FAILURE); + } + uint64_t micro_sec = mctx->mc.interval; + timer_config.it_value.tv_sec = next_tick == NULL ? now.tv_sec + 1 : next_tick->tv_sec; + timer_config.it_value.tv_nsec = next_tick == NULL ? now.tv_nsec : next_tick->tv_nsec; + timer_config.it_interval.tv_sec = micro_sec / 1000; + timer_config.it_interval.tv_nsec = micro_sec % 1000 * 1000000; + + printf("timer_config: sec=%ld nsec=%ld \n", (uint64_t) timer_config.it_value.tv_sec, (uint64_t) timer_config.it_value.tv_nsec); + + fdinfo.fd = timerfd_create(CLOCK_REALTIME, 0); + if (fdinfo.fd == -1) { + perror("Unable to timerfd_create"); + exit(EXIT_FAILURE); + } + if (timerfd_settime (fdinfo.fd, TFD_TIMER_ABSTIME, &timer_config, NULL) == -1) { + perror("Unable to timerfd_time"); + exit(EXIT_FAILURE); + } + fdinfo.cat->name = "timer"; + sprintf(fdinfo.url, "timer:%ld:%ld", mctx->mc.interval, mctx->mc.max_measure); + evt_core_add_fd (evts, &fdinfo); + printf("--- Timer registered\n"); +} + +int on_receive_measure_packet_err(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { return 1; } -int on_udp(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { - ssize_t res; +int on_receive_measure_packet(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { + ssize_t nread; struct measlat_ctx* mctx = fdinfo->cat->app_ctx; - res = read(fdinfo->fd, mctx->mc.payload, mctx->mc.payload_size); - if (res == -1 && errno == EAGAIN) return 1; - measure_parse (res, &mctx->mc); + if (mctx->is_from_needed) nread = recvfrom(fdinfo->fd, mctx->mc.payload, mctx->mc.payload_size, MSG_TRUNC, (struct sockaddr*)&mctx->addr, &mctx->addrlen); + else nread = recv(fdinfo->fd, mctx->mc.payload, mctx->mc.payload_size, MSG_TRUNC); + + if (nread == -1 && errno == EAGAIN) return 1; + // @FIXME logic is wrong for TCP here but would lead (hopefully) to a crash + + // First we parse the packet and exit if needed + measure_parse (nread, &mctx->mc); + + // Old behaviour where we work in a RTT way and send back the packet + if (measure_need_reply (&mctx->mc)) { + ssize_t nwritten; + if (mctx->is_from_needed) nwritten = sendto(fdinfo->fd, mctx->mc.payload, nread, 0, (struct sockaddr*)&mctx->addr, mctx->addrlen); + else nwritten = send(fdinfo->fd, mctx->mc.payload, nread, 0); + + // @FIXME don't support EAGAIN on write. Could be intended, you don't think so? + if (nwritten != nread) { + fprintf(stderr, "Didn't write the same number of bytes as read. nread=%ld, nwritten=%ld\n", nread, nwritten); + perror("write errno"); + exit(EXIT_FAILURE); + } + return 0; + } + + // Used to start sending from the server in sync with client + if (!mctx->is_timer_started) { + struct timespec next_tick = {0}; + measure_next_tick(&mctx->mc, &next_tick); + register_timer (ctx, &next_tick); + } return 0; } + +int on_tcp_co(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { + int conn_sock1, conn_sock2; + struct sockaddr_in addr; + socklen_t in_len; + char url[1024], port[6]; + struct evt_core_cat local_cat = {0}; + struct evt_core_fdinfo to_fdinfo = {0}; + to_fdinfo.cat = &local_cat; + to_fdinfo.url = url; + + in_len = sizeof(addr); + conn_sock1 = accept(fdinfo->fd, (struct sockaddr*)&addr, &in_len); + + if (conn_sock1 == -1 && errno == EAGAIN) return 1; + if (conn_sock1 == -1) goto co_error; + + url_get_port(port, fdinfo->url); + + to_fdinfo.fd = conn_sock1; + to_fdinfo.cat->name = "tcp-read"; + sprintf(to_fdinfo.url, "tcp:read:127.0.0.1:%s", port); + evt_core_add_fd (ctx, &to_fdinfo); + + return 0; + +co_error: + perror("Failed to handle new connection"); + exit(EXIT_FAILURE); +} + int on_timer(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { ssize_t s; uint64_t ticks = 0; @@ -46,15 +151,8 @@ int on_timer(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { if (ticks != 1) { fprintf(stderr, "Has ticked %lu times, expected 1 time. This is a bug\n", ticks); } - mctx->mc.counter++; - - struct measure_packet* head = (struct measure_packet*)mctx->mc.payload; - head->counter = mctx->mc.counter; - if (clock_gettime(CLOCK_MONOTONIC, &head->emit_time) == -1) { - perror("clock_gettime error"); - exit(EXIT_FAILURE); - } + struct measure_packet* head = measure_generate(&mctx->mc); struct evt_core_fdinfo* tgtinfo = evt_core_get_first_from_cat (ctx, "udp-read"); if (tgtinfo == NULL) tgtinfo = evt_core_get_first_from_cat (ctx, "tcp-read"); @@ -62,56 +160,17 @@ int on_timer(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { printf("No connection yet\n"); return 1; } - s = send(tgtinfo->fd, mctx->mc.payload, mctx->mc.payload_size, 0); + if (mctx->is_from_needed) s = sendto(tgtinfo->fd, mctx->mc.payload, mctx->mc.payload_size, 0, (struct sockaddr*)&mctx->addr, mctx->addrlen); + else s = send(tgtinfo->fd, mctx->mc.payload, mctx->mc.payload_size, 0); + if (s < 0) { perror("Send error"); //exit(EXIT_FAILURE); } + return 0; } - -void register_timer(struct evt_core_ctx* evts) { - struct timespec now; - struct itimerspec timer_config; - char url[1024]; - struct evt_core_cat cat = {0}; - struct evt_core_fdinfo fdinfo = {0}; - fdinfo.cat = &cat; - fdinfo.url = url; - - struct evt_core_cat* ucat = evt_core_get_from_cat (evts, "tcp-read"); - if (ucat == NULL) { - fprintf(stderr, "Category udp-read not found\n"); - exit(EXIT_FAILURE); - } - struct measlat_ctx* mctx = ucat->app_ctx; - - if (clock_gettime(CLOCK_REALTIME, &now) == -1) { - perror("clock_gettime"); - exit(EXIT_FAILURE); - } - uint64_t micro_sec = mctx->mc.interval; - timer_config.it_value.tv_sec = now.tv_sec + 1; - timer_config.it_value.tv_nsec = now.tv_nsec; - timer_config.it_interval.tv_sec = micro_sec / 1000; - timer_config.it_interval.tv_nsec = micro_sec % 1000 * 1000000; - - fdinfo.fd = timerfd_create(CLOCK_REALTIME, 0); - if (fdinfo.fd == -1) { - perror("Unable to timerfd_create"); - exit(EXIT_FAILURE); - } - if (timerfd_settime (fdinfo.fd, TFD_TIMER_ABSTIME, &timer_config, NULL) == -1) { - perror("Unable to timerfd_time"); - exit(EXIT_FAILURE); - } - fdinfo.cat->name = "timer"; - sprintf(fdinfo.url, "timer:%ld:%ld", mctx->mc.interval, mctx->mc.max_measure); - evt_core_add_fd (evts, &fdinfo); - printf("--- Timer registered\n"); -} - int on_socks5_success_measlat(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { char url[1024]; struct evt_core_cat cat = {0}; @@ -134,11 +193,11 @@ int on_socks5_success_measlat(struct evt_core_ctx* ctx, struct evt_core_fdinfo* evt_core_add_fd (ctx, &fdinfo_n); printf("--- Tor socket registered\n"); - register_timer(ctx); + register_timer(ctx, NULL); return 1; } -void spawn_tor_socket(struct evt_core_ctx* evts) { +void spawn_tor_client(struct evt_core_ctx* evts) { struct evt_core_cat* ucat = evt_core_get_from_cat (evts, "tcp-read"); if (ucat == NULL) { fprintf(stderr, "Category udp-read not found\n"); @@ -147,11 +206,12 @@ void spawn_tor_socket(struct evt_core_ctx* evts) { struct measlat_ctx* mctx = ucat->app_ctx; socks5_create_dns_client (evts, "127.0.0.1", "9050", mctx->host, atoi(mctx->port)); + printf("--- Tor client SOCKS started\n"); } int on_socks5_failed_measlat(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { evt_core_rm_fd (ctx, fdinfo->fd); - spawn_tor_socket(ctx); + spawn_tor_client(ctx); return 1; } @@ -166,18 +226,24 @@ void register_categories(struct evt_core_ctx* evts, struct measlat_ctx* mctx) { template.flags = EPOLLIN | EPOLLET; evt_core_add_cat(evts, &template); - template.cb = on_udp; // intended but not elegant - template.err_cb = on_udp_err; // intended but not elegant + template.cb = on_receive_measure_packet; // intended but not elegant + template.err_cb = on_receive_measure_packet_err; // intended but not elegant template.name = "tcp-read"; template.flags = EPOLLIN | EPOLLET; evt_core_add_cat(evts, &template); - template.cb = on_udp; - template.err_cb = on_udp_err; + template.cb = on_receive_measure_packet; + template.err_cb = on_receive_measure_packet_err; template.name = "udp-read"; template.flags = EPOLLIN | EPOLLET; evt_core_add_cat(evts, &template); + template.cb = on_tcp_co; + template.err_cb = NULL; + template.name = "tcp-co"; + template.flags = EPOLLIN | EPOLLET; + evt_core_add_cat(evts, &template); + template.cb = on_socks5_success_measlat; template.err_cb = on_socks5_failed_measlat; template.name = "socks5-success"; @@ -195,7 +261,7 @@ void register_categories(struct evt_core_ctx* evts, struct measlat_ctx* mctx) { } -void spawn_udp_socket(struct evt_core_ctx* evts) { +void spawn_udp_client(struct evt_core_ctx* evts) { struct evt_core_cat* ucat = evt_core_get_from_cat (evts, "udp-read"); if (ucat == NULL) { fprintf(stderr, "Category udp-read not found\n"); @@ -216,9 +282,52 @@ void spawn_udp_socket(struct evt_core_ctx* evts) { fdinfo.free_other = NULL; sprintf(fdinfo.url, "udp:read:%s:%s", mctx->host, mctx->port); evt_core_add_fd (evts, &fdinfo); - printf("--- UDP socket registered\n"); + printf("--- UDP client registered\n"); - register_timer(evts); + register_timer(evts, NULL); +} + +void spawn_udp_server(struct evt_core_ctx* evts) { + struct evt_core_cat* ucat = evt_core_get_from_cat (evts, "udp-read"); + if (ucat == NULL) { + fprintf(stderr, "Category udp-read not found\n"); + exit(EXIT_FAILURE); + } + struct measlat_ctx* mctx = ucat->app_ctx; + int udp_sock = create_udp_server (mctx->host, mctx->port); + + char url[1024]; + struct evt_core_cat cat = {0}; + struct evt_core_fdinfo fdinfo = {0}; + fdinfo.cat = &cat; + fdinfo.url = url; + + fdinfo.fd = udp_sock; + sprintf(url, "udp:rw:127.0.0.1:%s", mctx->port); + fdinfo.cat->name = "udp-read"; + evt_core_add_fd(evts, &fdinfo); + printf("--- UDP server is listening\n"); +} + +void spawn_tor_server(struct evt_core_ctx* evts) { + uint16_t ports[] = {7500}; + char buffer[1024]; + int tcp_serv_sock, err; + + sprintf(buffer, "%d", ports[0]); + tcp_serv_sock = create_tcp_server ("0.0.0.0", buffer); + err = listen(tcp_serv_sock, SOMAXCONN); + + struct evt_core_cat cat = {0}; + struct evt_core_fdinfo fdinfo = {0}; + fdinfo.cat = &cat; + fdinfo.url = buffer; + + fdinfo.fd = tcp_serv_sock; + sprintf(buffer, "tcp:co:127.0.0.1:%d", ports[0]); + fdinfo.cat->name = "tcp-co"; + evt_core_add_fd(evts, &fdinfo); + printf("--- TCP server is listening\n"); } int main(int argc, char** argv) { @@ -230,7 +339,7 @@ int main(int argc, char** argv) { struct evt_core_ctx evts = {0}; // 1. Parse parameters - while ((opt = getopt(argc, argv, "vh:p:c:s:i:t:")) != -1) { + while ((opt = getopt(argc, argv, "vh:p:c:s:i:t:lr")) != -1) { switch(opt) { case 'v': mctx.verbose++; @@ -241,6 +350,9 @@ int main(int argc, char** argv) { case 'p': // port mctx.port = optarg; break; + case 'l': + mctx.mc.is_server = 1; + break; case 't': // transport mctx.transport = optarg; break; @@ -250,6 +362,8 @@ int main(int argc, char** argv) { case 's': // size - payload in bytes mctx.mc.payload_size = atoi(optarg); break; + case 'r': + mctx.mc.is_rtt = 1; case 'i': // interval - every ms mctx.mc.interval = atoi(optarg); break; @@ -260,21 +374,27 @@ int main(int argc, char** argv) { // 2. Check and fix parameters measure_prepare (&mctx.mc); + mctx.addrlen = sizeof(mctx.addr); if (mctx.transport == NULL) mctx.transport = "udp"; - if (mctx.host == NULL || mctx.port == NULL) goto usage; - printf("[measlat_conf] host=%s, port=%s, transport=%s, count=%ld, size=%ld, interval=%ld\n", - mctx.host, mctx.port, mctx.transport, mctx.mc.max_measure, mctx.mc.payload_size, mctx.mc.interval); + if (mctx.host == NULL) mctx.host = "127.0.0.1"; + if (mctx.port == NULL) mctx.port = strcmp(mctx.transport, "udp") == 0 ? "9000" : "7500"; + printf("[measlat_conf] host=%s, port=%s, listen=%d, transport=%s, count=%ld, size=%ld, interval=%ld\n", + mctx.host, mctx.port, mctx.mc.is_server, mctx.transport, mctx.mc.max_measure, mctx.mc.payload_size, mctx.mc.interval); // 3. Bind events register_categories(&evts, &mctx); - if (strcmp(mctx.transport, "udp") == 0) spawn_udp_socket(&evts); - else if (strcmp(mctx.transport, "tor") == 0) spawn_tor_socket(&evts); + + if (mctx.mc.is_server && strcmp(mctx.transport, "udp") == 0) { spawn_udp_server (&evts); mctx.is_from_needed = 1; } + else if (mctx.mc.is_server && strcmp(mctx.transport, "tor") == 0) spawn_tor_server(&evts); + else if (strcmp(mctx.transport, "udp") == 0) spawn_udp_client(&evts); + else if (strcmp(mctx.transport, "tor") == 0) spawn_tor_client(&evts); + else exit(EXIT_FAILURE); // 4. Run main loop evt_core_loop(&evts); return 0; usage: - fprintf(stderr, "Usage: %s -h -p [-t ] [-c ] [-i ] [-s ]\n", argv[0]); + fprintf(stderr, "Usage: %s -h -p [-l] [-t ] [-c ] [-i ] [-s ]\n", argv[0]); exit(EXIT_FAILURE); } diff --git a/src/measure.c b/src/measure.c index 060f58d..e7d1fb2 100644 --- a/src/measure.c +++ b/src/measure.c @@ -1,4 +1,5 @@ #include "measure.h" +#define ONE_SEC 1000000000L void measure_parse(int size, struct measure_conf* mc) { struct timespec curr; @@ -8,7 +9,7 @@ void measure_parse(int size, struct measure_conf* mc) { exit(EXIT_FAILURE); } struct measure_packet* head = (struct measure_packet*) mc->payload; - if (clock_gettime(CLOCK_MONOTONIC, &curr) == -1){ + if (clock_gettime(CLOCK_REALTIME, &curr) == -1){ perror("clock_gettime error"); exit(EXIT_FAILURE); } @@ -19,7 +20,7 @@ void measure_parse(int size, struct measure_conf* mc) { micro_sec = elapsed_micros (&head->emit_time, &curr); printf("[%s] Packet %llu latency %luµs\n", ctime_no_newline, (unsigned long long)head->counter, micro_sec); - if (!mc->inhibit_exit && head->counter >= mc->max_measure) { + if (!mc->is_server && head->counter >= mc->max_measure) { printf("Measurement done\n"); exit(EXIT_SUCCESS); } @@ -44,3 +45,49 @@ void measure_prepare(struct measure_conf* mc) { cursor_msg = (cursor_msg + 1) % msg_len; } } +struct measure_packet* measure_generate(struct measure_conf* mc) { + struct measure_packet* head = (struct measure_packet*)mc->payload; + mc->counter++; + + head->counter = mc->counter; + head->is_echo = mc->is_rtt && !mc->is_server; + if (clock_gettime(CLOCK_REALTIME, &head->emit_time) == -1) { + perror("clock_gettime error"); + exit(EXIT_FAILURE); + } + return head; +} + +uint8_t timespec_gt(struct timespec *t1, struct timespec *t2) { + return t1->tv_sec > t2->tv_sec || (t1->tv_sec == t2->tv_sec && t1->tv_nsec > t2->tv_nsec); +} + +void measure_next_tick(struct measure_conf *mc, struct timespec *next) { + struct measure_packet *head = (struct measure_packet*) mc->payload; + struct timespec now, *sent_at = &head->emit_time; + mc->counter = head->counter; + + if (clock_gettime(CLOCK_REALTIME, &now) == -1) { + perror("clock_gettime error"); + exit(EXIT_FAILURE); + } + memcpy(next, sent_at, sizeof(struct timespec)); + + while(!timespec_gt (next, &now)) { + next->tv_nsec += mc->interval * 1000000L; + if (next->tv_nsec > ONE_SEC) { + next->tv_sec += next->tv_nsec / ONE_SEC; + next->tv_nsec = next->tv_nsec % ONE_SEC; + } + mc->counter++; + } + mc->counter--; + printf("interval: %ld\n", mc->interval); + printf("sent_at: sec=%ld nsec=%ld \n", (uint64_t) sent_at->tv_sec, (uint64_t) sent_at->tv_nsec); + printf("now: sec=%ld nsec=%ld \n", (uint64_t) now.tv_sec, (uint64_t) now.tv_nsec); + printf("next: sec=%ld nsec=%ld \n", (uint64_t) next->tv_sec, (uint64_t) next->tv_nsec); +} + +uint8_t measure_need_reply(struct measure_conf* mc) { + return mc->is_server && ((struct measure_packet*)mc->payload)->is_echo; +}; diff --git a/src/measure.h b/src/measure.h index 51ff873..72a53f8 100644 --- a/src/measure.h +++ b/src/measure.h @@ -1,3 +1,4 @@ +#pragma once #include #include #include @@ -11,13 +12,17 @@ struct measure_conf { uint64_t interval; char* payload; uint64_t counter; - uint8_t inhibit_exit; + uint8_t is_server, is_rtt; }; struct measure_packet { uint64_t counter; + uint8_t is_echo; struct timespec emit_time; }; void measure_parse(int size, struct measure_conf* mc); void measure_prepare(struct measure_conf* mc); +struct measure_packet* measure_generate(struct measure_conf* mc); +uint8_t measure_need_reply(struct measure_conf* mc); +void measure_next_tick(struct measure_conf *mc, struct timespec *next); diff --git a/src/tor_echo.c b/src/tor_echo.c index 2572f96..7c33600 100644 --- a/src/tor_echo.c +++ b/src/tor_echo.c @@ -100,7 +100,7 @@ int main(int argc, char** argv) { enum TOR_ONION_FLAGS tof = TOR_ONION_FLAG_NONE; char url[1024]; struct torecho_ctx tctx = {0}; - tctx.mc.inhibit_exit = 1; + tctx.mc.is_server = 1; tctx.mc.payload_size = 1500; while ((opt = getopt(argc, argv, "ns:m")) != -1) { @@ -130,7 +130,7 @@ int main(int argc, char** argv) { .socklist = NULL }; struct evt_core_cat tcp_all = { - .app_ctx = &tctx, + .app_ctx = &tctx , .free_app_ctx = NULL, .cb = te_on_tcp, .err_cb = NULL, diff --git a/src/udp_echo.c b/src/udp_echo.c index 9294f41..92f3e34 100644 --- a/src/udp_echo.c +++ b/src/udp_echo.c @@ -45,7 +45,7 @@ int main(int argc, char** argv) { int opt, udp_sock, verbose = 0; char *port = NULL, *bindhost = NULL; struct evt_core_ctx evts = {0}; - uctx.mc.inhibit_exit = 1; + uctx.mc.is_server = 1; uctx.mc.payload_size = 1500; // 1. Parse parameters From 17a5159b6f088df95507c0049fd63445db75ff53 Mon Sep 17 00:00:00 2001 From: Quentin Date: Tue, 24 Sep 2019 18:11:23 +0200 Subject: [PATCH 09/16] Add doc --- src/meas_lat.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/meas_lat.c b/src/meas_lat.c index a97a637..cdc2e5b 100644 --- a/src/meas_lat.c +++ b/src/meas_lat.c @@ -395,6 +395,6 @@ int main(int argc, char** argv) { return 0; usage: - fprintf(stderr, "Usage: %s -h -p [-l] [-t ] [-c ] [-i ] [-s ]\n", argv[0]); + fprintf(stderr, "Usage: %s -h -p [-l] [-r] [-t ] [-c ] [-i ] [-s ]\n", argv[0]); exit(EXIT_FAILURE); } From bb57457257b48e99227911c626d7ae50e7aa6d91 Mon Sep 17 00:00:00 2001 From: Quentin Date: Tue, 24 Sep 2019 18:33:03 +0200 Subject: [PATCH 10/16] Fix stupid bug --- src/meas_lat.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/meas_lat.c b/src/meas_lat.c index cdc2e5b..4555893 100644 --- a/src/meas_lat.c +++ b/src/meas_lat.c @@ -364,6 +364,7 @@ int main(int argc, char** argv) { break; case 'r': mctx.mc.is_rtt = 1; + break; case 'i': // interval - every ms mctx.mc.interval = atoi(optarg); break; From 7538d2d9e91452b75a4b50d6d0ff93d6088d3730 Mon Sep 17 00:00:00 2001 From: Quentin Date: Tue, 24 Sep 2019 19:48:42 +0200 Subject: [PATCH 11/16] Reworked measlat --- src/meas_lat.c | 60 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 49 insertions(+), 11 deletions(-) diff --git a/src/meas_lat.c b/src/meas_lat.c index 4555893..bfa6fce 100644 --- a/src/meas_lat.c +++ b/src/meas_lat.c @@ -10,12 +10,14 @@ #include "utils.h" #include "measure.h" #include "url.h" +#include "tor_os.h" +#include "tor_ctl.h" struct measlat_ctx { struct measure_conf mc; struct sockaddr_in addr; socklen_t addrlen; - int verbose, is_timer_started, is_from_needed; + int verbose, is_timer_started, is_from_needed, tor_flags; char *host, *port, *transport; }; @@ -72,9 +74,9 @@ int on_receive_measure_packet(struct evt_core_ctx* ctx, struct evt_core_fdinfo* struct measlat_ctx* mctx = fdinfo->cat->app_ctx; if (mctx->is_from_needed) nread = recvfrom(fdinfo->fd, mctx->mc.payload, mctx->mc.payload_size, MSG_TRUNC, (struct sockaddr*)&mctx->addr, &mctx->addrlen); - else nread = recv(fdinfo->fd, mctx->mc.payload, mctx->mc.payload_size, MSG_TRUNC); + else nread = recv(fdinfo->fd, mctx->mc.payload, mctx->mc.payload_size, 0); - if (nread == -1 && errno == EAGAIN) return 1; + if ((nread == -1 && errno == EAGAIN) || nread == 0) return 1; // @FIXME logic is wrong for TCP here but would lead (hopefully) to a crash // First we parse the packet and exit if needed @@ -153,11 +155,15 @@ int on_timer(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { } struct measure_packet* head = measure_generate(&mctx->mc); + //printf("send(id=%ld,is_echo=%d)\n", head->counter, head->is_echo); struct evt_core_fdinfo* tgtinfo = evt_core_get_first_from_cat (ctx, "udp-read"); if (tgtinfo == NULL) tgtinfo = evt_core_get_first_from_cat (ctx, "tcp-read"); if (tgtinfo == NULL) { printf("No connection yet\n"); + struct evt_core_fdinfo* timer_fd = evt_core_get_first_from_cat (ctx, "timer"); + evt_core_rm_fd(ctx, timer_fd->fd); + mctx->is_timer_started = 0; return 1; } if (mctx->is_from_needed) s = sendto(tgtinfo->fd, mctx->mc.payload, mctx->mc.payload_size, 0, (struct sockaddr*)&mctx->addr, mctx->addrlen); @@ -227,7 +233,7 @@ void register_categories(struct evt_core_ctx* evts, struct measlat_ctx* mctx) { evt_core_add_cat(evts, &template); template.cb = on_receive_measure_packet; // intended but not elegant - template.err_cb = on_receive_measure_packet_err; // intended but not elegant + template.err_cb = NULL; // intended but not elegant template.name = "tcp-read"; template.flags = EPOLLIN | EPOLLET; evt_core_add_cat(evts, &template); @@ -309,8 +315,7 @@ void spawn_udp_server(struct evt_core_ctx* evts) { printf("--- UDP server is listening\n"); } -void spawn_tor_server(struct evt_core_ctx* evts) { - uint16_t ports[] = {7500}; +void spawn_tor_server(struct evt_core_ctx* evts, uint16_t *ports) { char buffer[1024]; int tcp_serv_sock, err; @@ -330,6 +335,23 @@ void spawn_tor_server(struct evt_core_ctx* evts) { printf("--- TCP server is listening\n"); } +void measlat_create_onion_services(struct tor_os_str* tos, struct tor_ctl* tctl, uint16_t* ports, int ports_count, enum TOR_ONION_FLAGS tof) { + tor_os_create (tos, "onion_services.pub", "onion_services.txt", ports_count); + tor_os_read (tos); + + int err = 0; + err = tor_ctl_connect (tctl, "127.0.0.1", "9051"); + if (err < 0) { + fprintf(stderr, "Unable to open Tor Socket\n"); + exit(EXIT_FAILURE); + } + err = tor_ctl_add_onion (tctl, tos, ports, tof); + if (err != 0) { + fprintf(stderr, "Unable to create Onion Services (error: %d)\n", err); + exit(EXIT_FAILURE); + } +} + int main(int argc, char** argv) { setvbuf(stdout, NULL, _IONBF, 0); printf("~ measlat ~\n"); @@ -339,7 +361,7 @@ int main(int argc, char** argv) { struct evt_core_ctx evts = {0}; // 1. Parse parameters - while ((opt = getopt(argc, argv, "vh:p:c:s:i:t:lr")) != -1) { + while ((opt = getopt(argc, argv, "vh:p:c:s:i:t:lrn")) != -1) { switch(opt) { case 'v': mctx.verbose++; @@ -365,6 +387,9 @@ int main(int argc, char** argv) { case 'r': mctx.mc.is_rtt = 1; break; + case 'n': + mctx.tor_flags |= TOR_ONION_FLAG_NON_ANONYMOUS; + break; case 'i': // interval - every ms mctx.mc.interval = atoi(optarg); break; @@ -379,14 +404,27 @@ int main(int argc, char** argv) { if (mctx.transport == NULL) mctx.transport = "udp"; if (mctx.host == NULL) mctx.host = "127.0.0.1"; if (mctx.port == NULL) mctx.port = strcmp(mctx.transport, "udp") == 0 ? "9000" : "7500"; - printf("[measlat_conf] host=%s, port=%s, listen=%d, transport=%s, count=%ld, size=%ld, interval=%ld\n", - mctx.host, mctx.port, mctx.mc.is_server, mctx.transport, mctx.mc.max_measure, mctx.mc.payload_size, mctx.mc.interval); + printf("[measlat_conf] host=%s, port=%s, listen=%d, transport=%s, count=%ld, size=%ld, interval=%ld, is_rtt=%d\n", + mctx.host, mctx.port, mctx.mc.is_server, mctx.transport, mctx.mc.max_measure, mctx.mc.payload_size, mctx.mc.interval, mctx.mc.is_rtt); // 3. Bind events register_categories(&evts, &mctx); - if (mctx.mc.is_server && strcmp(mctx.transport, "udp") == 0) { spawn_udp_server (&evts); mctx.is_from_needed = 1; } - else if (mctx.mc.is_server && strcmp(mctx.transport, "tor") == 0) spawn_tor_server(&evts); + struct tor_os_str tos = {0}; + struct tor_ctl tctl = {0}; + uint16_t ports[] = {7500}; + int ports_count = sizeof(ports) / sizeof(ports[0]); + + if (mctx.mc.is_server && strcmp(mctx.transport, "udp") == 0) { + spawn_udp_server (&evts); + mctx.is_from_needed = 1; + + } + else if (mctx.mc.is_server && strcmp(mctx.transport, "tor") == 0) { + spawn_tor_server(&evts, ports); + measlat_create_onion_services (&tos, &tctl, ports, ports_count, mctx.tor_flags); + printf("--- Onion services created\n"); + } else if (strcmp(mctx.transport, "udp") == 0) spawn_udp_client(&evts); else if (strcmp(mctx.transport, "tor") == 0) spawn_tor_client(&evts); else exit(EXIT_FAILURE); From 73b385cd7703db401d4c0fd88e28824e40af1a74 Mon Sep 17 00:00:00 2001 From: Quentin Date: Wed, 25 Sep 2019 09:25:29 +0200 Subject: [PATCH 12/16] Fix scripts --- scripts/orig-client | 4 ++-- scripts/orig-server | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/orig-client b/scripts/orig-client index 14e70bc..346b766 100755 --- a/scripts/orig-client +++ b/scripts/orig-client @@ -5,5 +5,5 @@ pushd $4/shared > /dev/null HS="`cat onion_services.pub | head -n1`.onion" popd > /dev/null -measlat -h $HS -p 7500 -t tor > /dev/null 2> $4/log/client-bootstrap-stderr.log # Used to wait for connections -measlat -h $HS -p 7500 -t tor -c $1 -i $2 -s $3 > $4/res/orig.csv 2> $4/log/client-measlat-stderr.log +measlat -h $HS -p 7500 -t tor -r -s $3 > /dev/null 2> $4/log/client-bootstrap-stderr.log # Used to wait for connections +measlat -h $HS -p 7500 -t tor -c $1 -i $2 -s $3 > $4/log/client-measlat-stdout.log 2> $4/log/client-measlat-stderr.log diff --git a/scripts/orig-server b/scripts/orig-server index 986ec9f..d3d9f28 100755 --- a/scripts/orig-server +++ b/scripts/orig-server @@ -1,4 +1,4 @@ #!/bin/bash mkdir -p $2/{log,shared} cd $2/shared -torecho -m -s $1 > ../log/server-udpecho-stdout.log 2> ../log/server-udpecho-stderr.log +measlat -t tor -p 7500 -l -s $1 > ../log/server-measlat-stdout.log 2> ../log/server-measlat-stderr.log From 404a571873f240175261606aede40223062fabac Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Wed, 25 Sep 2019 07:28:57 +0000 Subject: [PATCH 13/16] Add a simple test pattern --- scripts/Makefile | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/scripts/Makefile b/scripts/Makefile index cc7ead2..27e15c6 100644 --- a/scripts/Makefile +++ b/scripts/Makefile @@ -8,6 +8,14 @@ simple_test: tor3 -f /etc/torrc_simple . \ tor3 -f /etc/torrc_simple +simple_test_2: + ./run-3 \ + 1,simple_test . \ + orig-client 600 100 100 . \ + orig-server 100. \ + tor3 -f /etc/torrc_simple . \ + tor3 -f /etc/torrc_simple + tor_bw_small: ./run-3 \ 1,tor_bw_small . \ From 2d7aa27883fe4c0bbb266c84acfb7fd7c3c40e71 Mon Sep 17 00:00:00 2001 From: Quentin Date: Thu, 26 Sep 2019 09:55:38 +0200 Subject: [PATCH 14/16] Rework parsing script --- scripts/deltams.py | 53 ++++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/scripts/deltams.py b/scripts/deltams.py index cac13fe..2741344 100755 --- a/scripts/deltams.py +++ b/scripts/deltams.py @@ -1,31 +1,34 @@ #!/usr/bin/python3 import sys,re -owd = f"./out/{sys.argv[1]}/log/server-udpecho-stdout.log" -rtt = f"./out/{sys.argv[1]}/res/{sys.argv[2]}.csv" +print("run,packet_id,measure_type,latency,latency_ms") +for i in range(1,len(sys.argv)): + path = sys.argv[i] + client_server = f"{sys.argv[1]}/log/server-measlat-stdout.log" + server_client = f"{sys.argv[1]}/log/client-measlat-stdout.log" + run = path -agg = {} -last_pkt = 0 + agg = {} + with open(client_server, 'r') as f: + for line in f: + res = re.match(r".*Packet (\d+) latency (\d+)µs", line) + if not res: continue + pkt_id,lat = res.groups() + pkt_id,lat = int(pkt_id), int(lat) + agg[pkt_id] = {"client_server": lat } -with open(owd, 'r') as f: - for line in f: - res = re.match(r".*Packet (\d+) latency (\d+)µs", line) - if not res: continue - pkt_id,lat = res.groups() - pkt_id,lat = int(pkt_id), int(lat) - agg[pkt_id] = {"owd": lat } + with open(server_client, 'r') as f: + for line in f: + res = re.match(r".*Packet (\d+) latency (\d+)µs", line) + if not res: continue + pkt_id,lat = res.groups() + pkt_id,lat = int(pkt_id), int(lat) + if not pkt_id in agg: agg[pkt_id] = {} + agg[pkt_id]["server_client"] = lat -with open(rtt, 'r') as f: - for line in f: - res = re.match(r".*Packet (\d+) latency (\d+)µs", line) - if not res: continue - pkt_id,lat = res.groups() - pkt_id,lat = int(pkt_id), int(lat) - agg[pkt_id]["rtt"] = lat - last_pkt = pkt_id - -for i in range(last_pkt): - if i not in agg: continue - if "rtt" not in agg[i]: continue - - print(abs((agg[i]["rtt"] - 2*agg[i]["owd"]) / 1000), agg[i]["owd"] / 1000, (agg[i]["rtt"] - agg[i]["owd"]) / 1000, i) + for packet_id, lats in agg.items(): + if "client_server" not in lats or "server_client" not in lats: continue + print(f"{run},{packet_id},client_server,{lats['client_server']},{lats['client_server'] / 1000}") + print(f"{run},{packet_id},server_client,{lats['server_client']},{lats['server_client'] / 1000}") + print(f"{run},{packet_id},delta,{lats['client_server'] - lats['server_client']},{(lats['client_server'] - lats['server_client'])/1000}") + print(f"{run},{packet_id},delta_abs,{abs(lats['client_server'] - lats['server_client'])},{abs(lats['client_server'] - lats['server_client'])/1000}") From f3d5d234398734be8735be0a2b731f0b9913ba86 Mon Sep 17 00:00:00 2001 From: Quentin Date: Thu, 26 Sep 2019 11:41:07 +0200 Subject: [PATCH 15/16] Fix bug --- scripts/deltams.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/deltams.py b/scripts/deltams.py index 2741344..e7c0c64 100755 --- a/scripts/deltams.py +++ b/scripts/deltams.py @@ -4,8 +4,8 @@ import sys,re print("run,packet_id,measure_type,latency,latency_ms") for i in range(1,len(sys.argv)): path = sys.argv[i] - client_server = f"{sys.argv[1]}/log/server-measlat-stdout.log" - server_client = f"{sys.argv[1]}/log/client-measlat-stdout.log" + client_server = f"{sys.argv[i]}/log/server-measlat-stdout.log" + server_client = f"{sys.argv[i]}/log/client-measlat-stdout.log" run = path agg = {} From d9e4243b4a3fa2d8016ba0900d8a39bf785dabdc Mon Sep 17 00:00:00 2001 From: Quentin Date: Thu, 26 Sep 2019 11:44:51 +0200 Subject: [PATCH 16/16] Add tor owd --- r/tor_owd.R | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 r/tor_owd.R diff --git a/r/tor_owd.R b/r/tor_owd.R new file mode 100644 index 0000000..3922804 --- /dev/null +++ b/r/tor_owd.R @@ -0,0 +1,20 @@ +library(ggplot2) +library(sqldf) +library(dplyr) +library(cowplot) + +tor_owd <- read.csv("tor_owd.csv") + +for (run_id in c(0:39)) { + print(run_id) + sel_owd <- filter(tor_owd, measure_type != "delta", run == paste("./out/nBV7DWLpj8WA6Be3-",run_id,sep="")) + ggplot(data = sel_owd, aes(x = packet_id, y=latency_ms, color=measure_type)) + + geom_line() + + theme_classic() + + ggsave(paste("tor_owd_",run_id,".png", sep = ""), dpi=150, dev='png', height=18, width=32, units="cm") +} + +ggplot(data = sqldf("select * from tor_owd where measure_type='delta_abs'"), aes(x=latency_ms)) + + stat_ecdf(pad = FALSE) + + coord_cartesian(xlim = c(0,500)) + + theme_classic()