tor_multipath_voip/src/algo_lightning.c

309 lines
10 KiB
C
Raw Normal View History

2019-10-07 14:07:35 +00:00
#include <sys/timerfd.h>
#include "algo_utils.h"
#include "utils.h"
#include "url.h"
#include "proxy.h"
#include "timer.h"
#include "proxy.h"
2019-10-09 15:07:51 +00:00
#include "measure.h"
2019-10-07 14:07:35 +00:00
#define HISTORIC_SIZE 256
#define MAX_LINKS 64
enum ooo_state {
IN_ORDER,
OOO_ONGOING,
OOO_DONE
};
2019-10-08 12:47:35 +00:00
struct stat_entry {
uint8_t link_id;
int64_t max_ooo;
};
2019-10-07 14:07:35 +00:00
struct timing_entry {
enum ooo_state state;
struct timespec detected_at;
struct timespec finished_at;
uint8_t link_id;
uint64_t pkt_id;
};
struct light_ctx {
2019-10-07 16:17:44 +00:00
uint8_t prev_links[MAX_LINKS];
2019-10-07 14:07:35 +00:00
struct timing_entry historic[HISTORIC_SIZE];
struct timespec last[MAX_LINKS];
uint64_t pkt_rcv_id;
uint64_t pkt_sent_id;
uint8_t selected_link;
uint8_t total_links;
2019-10-08 12:47:35 +00:00
int fast_count;
2019-10-07 14:07:35 +00:00
int sleep_duration;
2019-10-07 16:17:44 +00:00
int sent_past_links;
2019-10-07 14:07:35 +00:00
struct timespec window;
size_t monit_pkt_size;
2019-10-08 14:48:00 +00:00
uint8_t csv;
2019-10-09 15:07:51 +00:00
uint8_t is_measlat;
2019-10-07 14:07:35 +00:00
};
void algo_lightning_free(void* v) {
struct light_ctx* lightc = v;
free(lightc);
}
void algo_lightning_init(struct evt_core_ctx* ctx, struct algo_ctx* app_ctx, struct algo_params* ap) {
app_ctx->misc = malloc(sizeof(struct light_ctx));
app_ctx->free_misc = algo_lightning_free;
if (app_ctx->misc == NULL) {
perror("malloc failed in algo lightning init");
exit(EXIT_FAILURE);
}
memset(app_ctx->misc, 0, sizeof(struct light_ctx));
struct light_ctx* lightc = app_ctx->misc;
lightc->total_links = app_ctx->ap.links;
lightc->selected_link = lightc->total_links - 1;
2019-10-07 16:17:44 +00:00
lightc->sent_past_links = lightc->total_links;
2019-10-08 12:47:35 +00:00
lightc->fast_count = lightc->total_links / 2;
2019-10-07 14:07:35 +00:00
lightc->sleep_duration = 500;
2019-10-08 14:48:00 +00:00
lightc->csv = 0;
2019-10-07 14:07:35 +00:00
2019-10-08 12:47:35 +00:00
uint64_t window = 2000;
2019-10-07 14:07:35 +00:00
if (ap->algo_specific_params != NULL) {
char *parse_ptr, *token, *params;
for (params = ap->algo_specific_params; ; params = NULL) {
token = strtok_r(params, ",", &parse_ptr);
if (token == NULL) break;
2019-10-08 12:47:35 +00:00
sscanf(token, "fast_count=%d", &lightc->fast_count);
2019-10-07 14:07:35 +00:00
sscanf(token, "recovery=%d", &lightc->sleep_duration);
sscanf(token, "window=%ld", &window);
2019-10-07 16:17:44 +00:00
sscanf(token, "sent_past_links=%d", &lightc->sent_past_links);
2019-10-08 14:48:00 +00:00
sscanf(token, "csv=%c", &lightc->csv);
2019-10-09 15:07:51 +00:00
sscanf(token, "measlat=%c", &lightc->is_measlat);
2019-10-07 14:07:35 +00:00
}
}
2019-10-08 08:54:27 +00:00
for (int i = 0; i < lightc->sent_past_links; i++)
lightc->prev_links[i] = UINT8_MAX;
2019-10-07 14:07:35 +00:00
union abstract_packet m;
2019-10-07 16:17:44 +00:00
lightc->monit_pkt_size = sizeof(m.fmt.headers) + sizeof(m.fmt.content.link_monitoring_lightning) + sizeof(uint8_t) * (lightc->sent_past_links - 1);
2019-10-08 08:22:14 +00:00
timespec_set_unit (&lightc->window, window, MILISEC);
2019-10-07 14:07:35 +00:00
2019-10-08 12:47:35 +00:00
printf("fast_count = %d\n", lightc->fast_count);
2019-10-07 16:17:44 +00:00
printf("recovery = %d ms\n", lightc->sleep_duration);
printf("window check = %ld ms\n", window);
printf("sent_past_links = %d\n", lightc->sent_past_links);
2019-10-09 15:07:51 +00:00
printf("csv = %s\n", lightc->csv ? "yes" : "no");
printf("measlat = %s\n", lightc->is_measlat ? "yes" : "no");
2019-10-07 14:07:35 +00:00
}
2019-10-07 16:17:44 +00:00
void monitoring(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo, struct buffer_packet* bp) {
struct algo_ctx* app_ctx = fdinfo->cat->app_ctx;
struct light_ctx* lightc = app_ctx->misc;
union abstract_packet* ap = (union abstract_packet*) &bp->ip;
while (ap != NULL && ap->fmt.headers.cmd != CMD_LINK_MONITORING_LIGHTNING) ap = ap_next(ap);
if (ap == NULL) {
fprintf(stderr, "[algo_lightning] Unable to find our monitoring information\n");
exit(EXIT_FAILURE);
}
uint8_t *prev_links = &ap->fmt.content.link_monitoring_lightning.prev_links;
int64_t pkt_id = ap->fmt.content.link_monitoring_lightning.id;
int64_t missing = pkt_id - (lightc->pkt_rcv_id + 1);
struct timespec now;
set_now(&now);
// Detect OoO
for (int i = 0; i < missing && i < lightc->sent_past_links; i++) {
uint8_t link_id = prev_links[i];
int64_t miss_id = pkt_id - (i+1);
struct timing_entry *te = &lightc->historic[miss_id % HISTORIC_SIZE];
if (te->pkt_id == miss_id) continue; // Entry already exists
te->state = OOO_ONGOING;
te->detected_at = now;
te->link_id = link_id;
te->pkt_id = miss_id;
}
// Update current packet status
int link_id = url_get_port_int(fdinfo->url) - 7500;
struct timing_entry *te2 = &lightc->historic[pkt_id % HISTORIC_SIZE];
te2->state = te2->pkt_id == pkt_id ? OOO_DONE : IN_ORDER;
te2->pkt_id = pkt_id;
te2->link_id = link_id;
te2->finished_at = now;
}
int deliver(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo, struct buffer_packet* bp) {
char url[256];
struct evt_core_fdinfo *to_fdinfo = NULL;
struct algo_ctx* app_ctx = fdinfo->cat->app_ctx;
2019-10-07 14:07:35 +00:00
union abstract_packet* ap = (union abstract_packet*) &bp->ip;
2019-10-07 16:17:44 +00:00
if (ctx->verbose > 1) fprintf(stderr, " [algo_lightning] 1/2 Find destination\n");
sprintf(url, "udp:write:127.0.0.1:%d", ap->fmt.content.udp_encapsulated.port);
to_fdinfo = evt_core_get_from_url (ctx, url);
if (to_fdinfo == NULL) {
fprintf(stderr, "No fd for URL %s in tcp-read. Dropping packet :( \n", url);
mv_buffer_wtof (&app_ctx->br, fdinfo);
return 1;
}
if (ctx->verbose > 1) fprintf(stderr, " [algo_lightning] 2/2 Move buffer\n");
mv_buffer_rtow (&app_ctx->br, fdinfo, to_fdinfo);
main_on_udp_write(ctx, to_fdinfo);
2019-10-07 14:07:35 +00:00
return 0;
}
2019-10-07 16:17:44 +00:00
int algo_lightning_on_stream(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo, struct buffer_packet* bp) {
monitoring(ctx, fdinfo, bp);
return deliver(ctx, fdinfo, bp);
}
2019-10-08 12:47:35 +00:00
int compare_stat_entry_max(const void *a, const void *b) {
const struct stat_entry *sea = a, *seb = b;
if (sea->max_ooo == -1) return 1;
if (seb->max_ooo == -1) return -1;
return sea->max_ooo - seb->max_ooo;
}
void algo_lightning_update_stats (struct light_ctx *lightc, struct stat_entry *stats) {
2019-10-07 14:07:35 +00:00
struct timespec now, not_before = {0}, temp_time;
set_now(&now);
timespec_diff (&now, &lightc->window, &not_before);
2019-10-08 12:47:35 +00:00
for (int i = 0; i < lightc->total_links; i++) {
stats[i].link_id = i;
stats[i].max_ooo = -1;
}
2019-10-07 14:07:35 +00:00
for (int i = 0; i < HISTORIC_SIZE; i++) {
if (timespec_lt(&lightc->historic[i].finished_at, &not_before)) continue;
2019-10-08 08:54:27 +00:00
uint8_t l = lightc->historic[i].link_id;
if (l >= lightc->total_links) continue;
2019-10-07 14:07:35 +00:00
int64_t delta = 0;
switch (lightc->historic[i].state) {
case IN_ORDER:
break;
case OOO_ONGOING:
timespec_diff(&now, &lightc->historic[i].detected_at, &temp_time);
2019-10-08 08:22:14 +00:00
delta = timespec_get_unit (&temp_time, MILISEC);
2019-10-07 14:07:35 +00:00
break;
case OOO_DONE:
timespec_diff(&lightc->historic[i].finished_at, &lightc->historic[i].detected_at, &temp_time);
2019-10-08 08:22:14 +00:00
delta = timespec_get_unit (&temp_time, MILISEC);
2019-10-07 14:07:35 +00:00
break;
}
2019-10-08 12:47:35 +00:00
stats[l].link_id = l;
stats[l].max_ooo = delta > stats[l].max_ooo ? delta : stats[l].max_ooo;
2019-10-07 14:07:35 +00:00
}
2019-10-08 12:47:35 +00:00
qsort(stats, lightc->total_links, sizeof(struct stat_entry), compare_stat_entry_max);
2019-10-07 14:07:35 +00:00
}
int send_message(struct evt_core_ctx* ctx, struct buffer_packet* bp) {
char url[256];
struct evt_core_cat *cat = evt_core_get_from_cat (ctx, "tcp-write");
if (cat == NULL) {
fprintf(stderr, "[algo_lightning] cat tcp-write not found\n");
exit(EXIT_FAILURE);
}
struct algo_ctx* app_ctx = cat->app_ctx;
struct light_ctx* lightc = app_ctx->misc;
2019-10-08 12:47:35 +00:00
if (lightc->selected_link >= lightc->total_links) return 0;
2019-10-07 14:07:35 +00:00
set_now(&lightc->last[lightc->selected_link]);
sprintf(url, "tcp:write:127.0.0.1:%d", 7500 + lightc->selected_link);
struct evt_core_fdinfo *to_fdinfo = evt_core_get_from_url (ctx, url);
if (to_fdinfo == NULL) return 0;
struct buffer_packet* bp_dup = dup_buffer_tow (&app_ctx->br, bp, to_fdinfo);
2019-10-08 08:54:27 +00:00
lightc->pkt_sent_id++;
2019-10-07 14:07:35 +00:00
union abstract_packet monit = {
.fmt.headers.cmd = CMD_LINK_MONITORING_LIGHTNING,
.fmt.headers.size = lightc->monit_pkt_size,
.fmt.headers.flags = 0,
2019-10-07 16:17:44 +00:00
.fmt.content.link_monitoring_lightning.id = lightc->pkt_sent_id
2019-10-07 14:07:35 +00:00
};
2019-10-07 16:17:44 +00:00
union abstract_packet *ap_buf = buffer_append_ap (bp_dup, &monit);
uint8_t *links = &ap_buf->fmt.content.link_monitoring_lightning.prev_links;
for (int i = 0; i < lightc->sent_past_links; i++) {
2019-10-08 08:10:59 +00:00
links[i] = lightc->prev_links[(lightc->pkt_sent_id - (i + 1)) % MAX_LINKS];
2019-10-07 16:17:44 +00:00
}
lightc->prev_links[lightc->pkt_sent_id % MAX_LINKS] = lightc->selected_link;
2019-10-07 14:07:35 +00:00
if (ctx->verbose > 1) {
dump_buffer_packet(bp_dup);
fprintf(stderr, " [algo_lightning] Will send this info\n");
}
main_on_tcp_write(ctx, to_fdinfo);
return 1;
}
int algo_lightning_on_datagram(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo, struct buffer_packet* bp) {
struct algo_ctx* app_ctx = fdinfo->cat->app_ctx;
struct light_ctx* lightc = app_ctx->misc;
union abstract_packet* ap = (union abstract_packet*) &bp->ip;
// Last step, send packet
2019-10-08 12:47:35 +00:00
struct stat_entry stats[MAX_LINKS] = {0};
2019-10-07 14:07:35 +00:00
algo_lightning_update_stats(lightc, stats);
2019-10-08 12:47:35 +00:00
if (ctx->verbose > 1) {
printf("after sort: ");
for (int i = 0; i < lightc->total_links; i++) {
printf("%d (%ld), ", stats[i].link_id, stats[i].max_ooo);
}
printf("\n");
2019-10-08 08:22:14 +00:00
}
2019-10-08 12:47:35 +00:00
struct timespec now, sel_link_last, temp_time;
2019-10-07 14:07:35 +00:00
set_now(&now);
2019-10-08 14:48:00 +00:00
uint64_t now_timestamp = timespec_get_unit(&now, MILISEC);
2019-10-07 14:07:35 +00:00
2019-10-08 12:47:35 +00:00
// Select fast link
sel_link_last = now;
lightc->selected_link = UINT8_MAX;
for (int i = 0; i < lightc->fast_count; i++) {
if (timespec_lt (&lightc->last[stats[i].link_id], &sel_link_last)) {
lightc->selected_link = stats[i].link_id;
sel_link_last = lightc->last[stats[i].link_id];
2019-10-07 14:07:35 +00:00
}
}
2019-10-08 12:47:35 +00:00
send_message (ctx, bp);
2019-10-08 14:48:00 +00:00
printf("%ld,%d,fast\n", now_timestamp, lightc->selected_link);
2019-10-08 12:47:35 +00:00
// Select slow link
sel_link_last = now;
lightc->selected_link = UINT8_MAX;
for (int i = lightc->fast_count; i < lightc->total_links; i++) {
if (timespec_lt (&lightc->last[stats[i].link_id], &sel_link_last)) {
lightc->selected_link = stats[i].link_id;
sel_link_last = lightc->last[stats[i].link_id];
}
2019-10-08 08:10:59 +00:00
}
2019-10-08 12:47:35 +00:00
timespec_diff (&now, &sel_link_last, &temp_time);
uint64_t elapsed = timespec_get_unit(&temp_time, MILISEC);
2019-10-09 15:07:51 +00:00
if (lightc->is_measlat) {
struct measure_packet *mp = (void*)&ap->fmt.content.udp_encapsulated.payload;
mp->flag = 1;
}
2019-10-08 14:48:00 +00:00
if (elapsed >= lightc->sleep_duration) {
2019-10-08 12:47:35 +00:00
send_message (ctx, bp);
2019-10-08 14:48:00 +00:00
printf("%ld,%d,slow\n", now_timestamp, lightc->selected_link);
}
2019-10-07 14:07:35 +00:00
mv_buffer_rtof (&app_ctx->br, fdinfo);
return 0;
}
int algo_lightning_on_err(struct evt_core_ctx *ctx, struct evt_core_fdinfo *fdinfo) {
return 0;
}