From 6400f760c718fefa53956582ad8f671d227841e7 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Tue, 22 Sep 2020 14:05:34 -0700 Subject: [PATCH 01/16] vere: renames ames mug helper for consistency --- pkg/urbit/vere/io/ames.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/pkg/urbit/vere/io/ames.c b/pkg/urbit/vere/io/ames.c index 4d544425d..f4ec98ef3 100644 --- a/pkg/urbit/vere/io/ames.c +++ b/pkg/urbit/vere/io/ames.c @@ -131,13 +131,11 @@ _ames_panc_free(u3_panc* pac_u) { c3_free(pac_u); } -/* _ca_mug_body(): truncated mug hash of bytes +/* _ames_mug_body(): truncated (20 least-significant bits) mug hash of bytes */ static c3_l -_ca_mug_body(c3_w len_w, c3_y* byt_y) +_ames_mug_body(c3_w len_w, c3_y* byt_y) { - // mask off ((1 << 20) - 1) - // return u3r_mug_bytes(byt_y, len_w) & 0xfffff; } @@ -434,7 +432,7 @@ _ames_serialize_packet(u3_panc* pac_u, c3_o dop_o) // if we updated the origin lane, we need to update the mug too // if ( c3y == nal_o ) { - pac_u->hed_u.mug_l = _ca_mug_body(sen_y + rec_y + bod_u->con_w, + pac_u->hed_u.mug_l = _ames_mug_body(sen_y + rec_y + bod_u->con_w, pac_y + 4); } @@ -800,7 +798,7 @@ _ames_recv_cb(uv_udp_t* wax_u, // ensure the mug is valid // if ( c3y == pas_o - && (hed_u.mug_l != _ca_mug_body(nrd_i - 4, bod_y)) ) + && (hed_u.mug_l != _ames_mug_body(nrd_i - 4, bod_y)) ) { pas_o = c3n; From 7c393a1d71af2752643070b60cfab5d9638dae39 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Tue, 22 Sep 2020 14:33:39 -0700 Subject: [PATCH 02/16] vere: factors out ames' packet header parsing/serialization --- pkg/urbit/vere/io/ames.c | 65 ++++++++++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 22 deletions(-) diff --git a/pkg/urbit/vere/io/ames.c b/pkg/urbit/vere/io/ames.c index f4ec98ef3..68ce82c88 100644 --- a/pkg/urbit/vere/io/ames.c +++ b/pkg/urbit/vere/io/ames.c @@ -139,6 +139,46 @@ _ames_mug_body(c3_w len_w, c3_y* byt_y) return u3r_mug_bytes(byt_y, len_w) & 0xfffff; } +/* _ames_sift_head(): parse packet header. +*/ +static void +_ames_sift_head(u3_head* hed_u, c3_y buf_y[4]) +{ + c3_w hed_w = buf_y[0] + | (buf_y[1] << 8) + | (buf_y[2] << 16) + | (buf_y[3] << 24); + + // XX only version 0 currently recognized + // + hed_u->ver_y = hed_w & 0x7; + hed_u->mug_l = (hed_w >> 3) & 0xfffff; // 20 bits + hed_u->sac_y = (hed_w >> 23) & 0x3; + hed_u->rac_y = (hed_w >> 25) & 0x3; + hed_u->enc_o = (hed_w >> 27) & 0x1; +} + +/* _ames_etch_head(): serialize packet header. +*/ +static void +_ames_etch_head(u3_head* hed_u, c3_y buf_y[4]) +{ + c3_w hed_w = hed_u->ver_y + | (hed_u->mug_l << 3) + | (hed_u->sac_y << 23) + | (hed_u->rac_y << 25) + | (hed_u->enc_o << 27); + + // only version 0 currently recognized + // + c3_assert( 0 == hed_u->ver_y ); + + buf_y[0] = hed_w & 0xff; + buf_y[1] = (hed_w >> 8) & 0xff; + buf_y[2] = (hed_w >> 16) & 0xff; + buf_y[3] = (hed_w >> 24) & 0xff; +} + /* _ames_send_cb(): send callback. */ static void @@ -438,16 +478,7 @@ _ames_serialize_packet(u3_panc* pac_u, c3_o dop_o) // now we can serialize the head // - u3_head* hed_u = &pac_u->hed_u; - c3_w hed_w = hed_u->ver_y - | (hed_u->mug_l << 3) - | (hed_u->sac_y << 23) - | (hed_u->rac_y << 25) - | (hed_u->enc_o << 27); - - // XX assumes little-endian - // - memcpy(pac_y, &hed_w, 4); + _ames_etch_head(&pac_u->hed_u, pac_y); pac = u3i_bytes(4 + sen_y + rec_y + bod_u->con_w, pac_y); c3_free(pac_y); @@ -766,20 +797,10 @@ _ames_recv_cb(uv_udp_t* wax_u, if ( 4 >= nrd_i ) { pas_o = c3n; } + // unpack the packet header // - else { - c3_w hed_w = (byt_y[0] << 0) - | (byt_y[1] << 8) - | (byt_y[2] << 16) - | (byt_y[3] << 24); - - hed_u.ver_y = hed_w & 0x7; - hed_u.mug_l = (hed_w >> 3) & 0xfffff; //NOTE ((1 << 20) - 1) - hed_u.sac_y = (hed_w >> 23) & 0x3; - hed_u.rac_y = (hed_w >> 25) & 0x3; - hed_u.enc_o = (hed_w >> 27) & 0x1; - } + _ames_sift_head(&hed_u, byt_y); // ensure the protocol version matches ours // From 6c6b2db0d877a08dde928383179c2aec0cf5a0e6 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Fri, 16 Oct 2020 16:50:29 -0700 Subject: [PATCH 03/16] vere: factors out ames' ship byte-array to chubs-array conversion --- pkg/urbit/vere/io/ames.c | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/pkg/urbit/vere/io/ames.c b/pkg/urbit/vere/io/ames.c index 68ce82c88..a91fe97d1 100644 --- a/pkg/urbit/vere/io/ames.c +++ b/pkg/urbit/vere/io/ames.c @@ -179,6 +179,35 @@ _ames_etch_head(u3_head* hed_u, c3_y buf_y[4]) buf_y[3] = (hed_w >> 24) & 0xff; } +/* _ames_chub_bytes(): c3_y[8] to c3_d +** XX move +*/ +static inline c3_d +_ames_chub_bytes(c3_y byt_y[8]) +{ + return (c3_d)byt_y[0] + | (c3_d)byt_y[1] << 8 + | (c3_d)byt_y[2] << 16 + | (c3_d)byt_y[3] << 24 + | (c3_d)byt_y[4] << 32 + | (c3_d)byt_y[5] << 40 + | (c3_d)byt_y[6] << 48 + | (c3_d)byt_y[7] << 56; +} + +/* _ames_ship_to_chubs(): pack [len_y] bytes into c3_d[2] +** XX move +*/ +static inline void +_ames_ship_to_chubs(c3_d sip_d[2], c3_y len_y, c3_y* buf_y) +{ + c3_y sip_y[16] = {0}; + memcpy(sip_y, buf_y, c3_min(16, len_y)); + + sip_d[0] = _ames_chub_bytes(sip_y); + sip_d[1] = _ames_chub_bytes(sip_y + 8); +} + /* _ames_send_cb(): send callback. */ static void @@ -839,11 +868,8 @@ _ames_recv_cb(uv_udp_t* wax_u, c3_y* con_y = NULL; if ( c3y == pas_o ) { - u3_noun sen = u3i_bytes(sen_y, bod_y); - u3_noun rec = u3i_bytes(rec_y, bod_y + sen_y); - u3r_chubs(0, 2, rec_d, rec); - u3r_chubs(0, 2, sen_d, sen); - u3z(sen); u3z(rec); + _ames_ship_to_chubs(sen_d, sen_y, bod_y); + _ames_ship_to_chubs(rec_d, rec_y, bod_y + sen_y); con_y = c3_malloc(con_w); memcpy(con_y, bod_y + sen_y + rec_y, con_w); From 9b1b303417a9daa83e0df8aa924d541f007e1004 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Fri, 16 Oct 2020 16:52:16 -0700 Subject: [PATCH 04/16] vere: factors out ames' ship chubs-array to byte-array conversion --- pkg/urbit/vere/io/ames.c | 41 +++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/pkg/urbit/vere/io/ames.c b/pkg/urbit/vere/io/ames.c index a91fe97d1..ab1353b8f 100644 --- a/pkg/urbit/vere/io/ames.c +++ b/pkg/urbit/vere/io/ames.c @@ -208,6 +208,36 @@ _ames_ship_to_chubs(c3_d sip_d[2], c3_y len_y, c3_y* buf_y) sip_d[1] = _ames_chub_bytes(sip_y + 8); } +/* _ames_chub_bytes(): c3_d to c3_y[8] +** XX move +*/ +static inline void +_ames_bytes_chub(c3_y byt_y[8], c3_d num_d) +{ + byt_y[0] = num_d & 0xff; + byt_y[1] = (num_d >> 8) & 0xff; + byt_y[2] = (num_d >> 16) & 0xff; + byt_y[3] = (num_d >> 24) & 0xff; + byt_y[4] = (num_d >> 32) & 0xff; + byt_y[5] = (num_d >> 40) & 0xff; + byt_y[6] = (num_d >> 48) & 0xff; + byt_y[7] = (num_d >> 56) & 0xff; +} + +/* _ames_ship_of_chubs(): unpack c3_d[2] into [len_y] bytes. +** XX move +*/ +static inline void +_ames_ship_of_chubs(c3_d sip_d[2], c3_y len_y, c3_y* buf_y) +{ + c3_y sip_y[16] = {0}; + + _ames_bytes_chub(sip_y, sip_d[0]); + _ames_bytes_chub(sip_y + 8, sip_d[1]); + + memcpy(buf_y, sip_y, c3_min(16, len_y)); +} + /* _ames_send_cb(): send callback. */ static void @@ -489,13 +519,10 @@ _ames_serialize_packet(u3_panc* pac_u, c3_o dop_o) // u3_body* bod_u = &pac_u->bod_u; c3_y* pac_y = c3_malloc(4 + sen_y + rec_y + bod_u->con_w); - { - u3_atom sen = u3i_chubs(2, bod_u->sen_d); - u3_atom rec = u3i_chubs(2, bod_u->rec_d); - u3r_bytes(0, sen_y, pac_y + 4, sen); - u3r_bytes(0, rec_y, pac_y + 4 + sen_y, rec); - u3z(sen); u3z(rec); - } + + _ames_ship_of_chubs(bod_u->sen_d, sen_y, pac_y + 4); + _ames_ship_of_chubs(bod_u->rec_d, rec_y, pac_y + 4 + sen_y); + memcpy(pac_y + 4 + sen_y + rec_y, bod_u->con_y, bod_u->con_w); // if we updated the origin lane, we need to update the mug too From acbcc53e49d4b09540f95037aa821d9326098128 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Fri, 16 Oct 2020 16:55:28 -0700 Subject: [PATCH 05/16] vere: refactors ames' packet serialization --- pkg/urbit/vere/io/ames.c | 76 ++++++++++++++++++++++++++-------------- 1 file changed, 49 insertions(+), 27 deletions(-) diff --git a/pkg/urbit/vere/io/ames.c b/pkg/urbit/vere/io/ames.c index ab1353b8f..5db252a8c 100644 --- a/pkg/urbit/vere/io/ames.c +++ b/pkg/urbit/vere/io/ames.c @@ -238,6 +238,45 @@ _ames_ship_of_chubs(c3_d sip_d[2], c3_y len_y, c3_y* buf_y) memcpy(buf_y, sip_y, c3_min(16, len_y)); } +/* _ames_etch_pack(): serialize packet header and body. +*/ +static c3_w +_ames_etch_pack(u3_head* hed_u, + u3_body* bod_u, + c3_o mug_o, + c3_y** out_y) +{ + // start with the body + // + c3_y sen_y = 2 << hed_u->sac_y; // sender len + c3_y rec_y = 2 << hed_u->rac_y; // receiver len + c3_w bod_w = sen_y + rec_y + bod_u->con_w; // body len + c3_w len_w = 4 + bod_w; // packet len + c3_y* pac_y = c3_malloc(len_w); + c3_y* bod_y = pac_y + 4; + + _ames_ship_of_chubs(bod_u->sen_d, sen_y, bod_y); + _ames_ship_of_chubs(bod_u->rec_d, rec_y, bod_y + sen_y); + + { + c3_y* con_y = bod_y + sen_y + rec_y; + memcpy(con_y, bod_u->con_y, bod_u->con_w); + } + + // if we updated the origin lane, we need to update the mug too + // + if ( c3y == mug_o ) { + hed_u->mug_l = _ames_mug_body(bod_w, bod_y); + } + + // now we can serialize the head + // + _ames_etch_head(hed_u, pac_y); + + *out_y = pac_y; + return len_w; +} + /* _ames_send_cb(): send callback. */ static void @@ -467,8 +506,6 @@ _ames_lane_from_cache(u3p(u3h_root) lax_p, u3_noun who) { static u3_noun _ames_serialize_packet(u3_panc* pac_u, c3_o dop_o) { - c3_y sen_y = 2 << pac_u->hed_u.sac_y; - c3_y rec_y = 2 << pac_u->hed_u.rac_y; c3_o nal_o = c3n; // update the body's lane, if desired @@ -513,34 +550,19 @@ _ames_serialize_packet(u3_panc* pac_u, c3_o dop_o) // serialize the packet // - u3_noun pac; + // XX serialize on stack? + // { - // start with the body - // - u3_body* bod_u = &pac_u->bod_u; - c3_y* pac_y = c3_malloc(4 + sen_y + rec_y + bod_u->con_w); - - _ames_ship_of_chubs(bod_u->sen_d, sen_y, pac_y + 4); - _ames_ship_of_chubs(bod_u->rec_d, rec_y, pac_y + 4 + sen_y); - - memcpy(pac_y + 4 + sen_y + rec_y, bod_u->con_y, bod_u->con_w); - - // if we updated the origin lane, we need to update the mug too - // - if ( c3y == nal_o ) { - pac_u->hed_u.mug_l = _ames_mug_body(sen_y + rec_y + bod_u->con_w, - pac_y + 4); - } - - // now we can serialize the head - // - _ames_etch_head(&pac_u->hed_u, pac_y); - - pac = u3i_bytes(4 + sen_y + rec_y + bod_u->con_w, pac_y); + u3_noun pac; + c3_y* pac_y; + c3_w len_w = _ames_etch_pack(&pac_u->hed_u, + &pac_u->bod_u, + nal_o, &pac_y); + pac = u3i_bytes(len_w, pac_y); c3_free(pac_y); - } - return pac; + return pac; + } } /* _ames_czar(): galaxy address resolution. From e2b47e02c2f54286b10f064e253af7add94d8ebd Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Mon, 19 Oct 2020 22:59:57 -0700 Subject: [PATCH 06/16] vere: refactors ames' packet parsing and forwarding --- pkg/urbit/vere/io/ames.c | 370 +++++++++++++++++++++++---------------- 1 file changed, 219 insertions(+), 151 deletions(-) diff --git a/pkg/urbit/vere/io/ames.c b/pkg/urbit/vere/io/ames.c index 5db252a8c..c40b435ef 100644 --- a/pkg/urbit/vere/io/ames.c +++ b/pkg/urbit/vere/io/ames.c @@ -86,6 +86,7 @@ u3_lane ore_u; // origin lane u3_head hed_u; // header u3_body bod_u; // body + void* ptr_v; // buffer (to free) } u3_panc; /* _ames_alloc(): libuv buffer allocator. @@ -116,18 +117,21 @@ _ames_pact_free(u3_pact* pac_u) /* _ames_panc_free(): remove references, lose refcounts and free struct */ static void -_ames_panc_free(u3_panc* pac_u) { +_ames_panc_free(u3_panc* pac_u) +{ if ( 0 != pac_u->nex_u ) { pac_u->nex_u->pre_u = pac_u->pre_u; } + if ( 0 != pac_u->pre_u ) { pac_u->pre_u->nex_u = pac_u->nex_u; - } else { + } + else { c3_assert(pac_u == pac_u->sam_u->pac_u); pac_u->sam_u->pac_u = pac_u->nex_u; } - c3_free(pac_u->bod_u.con_y); + c3_free(pac_u->ptr_v); c3_free(pac_u); } @@ -141,7 +145,7 @@ _ames_mug_body(c3_w len_w, c3_y* byt_y) /* _ames_sift_head(): parse packet header. */ -static void +static c3_o _ames_sift_head(u3_head* hed_u, c3_y buf_y[4]) { c3_w hed_w = buf_y[0] @@ -156,6 +160,7 @@ _ames_sift_head(u3_head* hed_u, c3_y buf_y[4]) hed_u->sac_y = (hed_w >> 23) & 0x3; hed_u->rac_y = (hed_w >> 25) & 0x3; hed_u->enc_o = (hed_w >> 27) & 0x1; + return c3y; } /* _ames_etch_head(): serialize packet header. @@ -238,6 +243,30 @@ _ames_ship_of_chubs(c3_d sip_d[2], c3_y len_y, c3_y* buf_y) memcpy(buf_y, sip_y, c3_min(16, len_y)); } +/* _ames_sift_body(): parse packet body. +*/ +static c3_o +_ames_sift_body(u3_head* hed_u, + u3_body* bod_u, + c3_w len_w, + c3_y* bod_y) +{ + c3_y sen_y = 2 << hed_u->sac_y; + c3_y rec_y = 2 << hed_u->rac_y; + + if ( (sen_y + rec_y) >= len_w ) { + return c3n; + } + else { + _ames_ship_to_chubs(bod_u->sen_d, sen_y, bod_y); + _ames_ship_to_chubs(bod_u->rec_d, rec_y, bod_y + sen_y); + + bod_u->con_w = len_w - sen_y - rec_y; + bod_u->con_y = bod_y + sen_y + rec_y; + return c3y; + } +} + /* _ames_etch_pack(): serialize packet header and body. */ static c3_w @@ -452,17 +481,6 @@ u3_ames_encode_lane(u3_lane lan) { return u3ke_jam(u3nt(c3__ipv4, u3i_words(1, &lan.pip_w), lan.por_s)); } -/* _ames_lane_from_sockaddr(): sockaddr_in to lane struct -*/ -static u3_lane -_ames_lane_from_sockaddr(struct sockaddr_in* add_u) -{ - u3_lane lan_u; - lan_u.por_s = ntohs(add_u->sin_port); - lan_u.pip_w = ntohl(add_u->sin_addr.s_addr); - return lan_u; -} - /* _ames_lane_into_cache(): put las for who into cache, including timestamp */ static void @@ -532,16 +550,27 @@ _ames_serialize_packet(u3_panc* pac_u, c3_o dop_o) // always using the appropriate galaxy lane instead. // if ( u3_nul == lon ) { + c3_w con_w; + c3_y* con_y; + u3z(lon); lon = u3nt(u3_nul, c3n, u3_ames_encode_lane(pac_u->ore_u)); nal_o = c3y; - c3_free(pac_u->bod_u.con_y); - u3_noun jam = u3ke_jam(u3nc(lon, bod)); - pac_u->bod_u.con_w = u3r_met(3, jam); - pac_u->bod_u.con_y = c3_malloc(pac_u->bod_u.con_w); - u3r_bytes(0, pac_u->bod_u.con_w, pac_u->bod_u.con_y, jam); - u3z(jam); + // XX off-loom jam? + // + { + u3_noun jam = u3ke_jam(u3nc(lon, bod)); + con_w = u3r_met(3, jam); + con_y = c3_malloc(con_w); + u3r_bytes(0, con_w, con_y, jam); + u3z(jam); + } + + c3_free(pac_u->ptr_v); + pac_u->ptr_v = con_y; + pac_u->bod_u.con_y = con_y; + pac_u->bod_u.con_w = con_w; } else { u3z(lon); u3z(bod); @@ -855,101 +884,28 @@ _ames_lane_scry_cb(void* vod_p, u3_noun nun) u3z(nun); } -/* _ames_recv_cb(): receive callback. +/* _ames_try_forward(): try to forward a packet for another ship. */ static void -_ames_recv_cb(uv_udp_t* wax_u, - ssize_t nrd_i, - const uv_buf_t * buf_u, - const struct sockaddr* adr_u, - unsigned flg_i) +_ames_try_forward(u3_ames* sam_u, + u3_lane* lan_u, + u3_head* hed_u, + u3_body* bod_u, + c3_y* hun_y) { - u3_ames* sam_u = wax_u->data; - c3_o pas_o = c3y; - c3_y* byt_y = (c3_y*)buf_u->base; - c3_y* bod_y = byt_y + 4; - u3_head hed_u; + u3_weak lac; - // ensure a sane message size + // if the recipient is a galaxy, their lane is always &+~gax // - if ( 4 >= nrd_i ) { - pas_o = c3n; - } - - // unpack the packet header - // - _ames_sift_head(&hed_u, byt_y); - - // ensure the protocol version matches ours - // - if ( c3y == pas_o - && (c3y == sam_u->fit_o) - && (sam_u->ver_y != hed_u.ver_y) ) + if ( (256 > bod_u->rec_d[0]) + && (0 == bod_u->rec_d[1]) ) { - pas_o = c3n; - - sam_u->vet_d++; - if ( 0 == (sam_u->vet_d % 100) ) { - u3l_log("ames: %" PRIu64 " dropped for version mismatch\n", sam_u->vet_d); - } + lac = u3nc(c3y, (c3_y)bod_u->rec_d[0]); } - - // ensure the mug is valid + // otherwise, try to get the lane from cache // - if ( c3y == pas_o - && (hed_u.mug_l != _ames_mug_body(nrd_i - 4, bod_y)) ) - { - pas_o = c3n; - - sam_u->mut_d++; - if ( 0 == (sam_u->mut_d % 100) ) { - u3l_log("ames: %" PRIu64 " dropped for invalid mug\n", sam_u->mut_d); - } - } - - // unpack the body - // - c3_y sen_y = 2 << hed_u.sac_y; - c3_y rec_y = 2 << hed_u.rac_y; - c3_d sen_d[2]; - c3_d rec_d[2]; - c3_w con_w = nrd_i - 4 - sen_y - rec_y; - c3_y* con_y = NULL; - - if ( c3y == pas_o ) { - _ames_ship_to_chubs(sen_d, sen_y, bod_y); - _ames_ship_to_chubs(rec_d, rec_y, bod_y + sen_y); - - con_y = c3_malloc(con_w); - memcpy(con_y, bod_y + sen_y + rec_y, con_w); - - // ensure the content is cue-able - // - pas_o = ur_cue_test_with(sam_u->tes_u, con_w, con_y) ? c3y : c3n; - } - - // if we can scry, - // and we are not the recipient, - // we might want to forward statelessly - // - if ( c3y == pas_o - && c3y == sam_u->see_o - && ( (rec_d[0] != sam_u->pir_u->who_d[0]) - || (rec_d[1] != sam_u->pir_u->who_d[1]) ) ) - { - pas_o = c3n; - - u3_weak lac; - // if the recipient is a galaxy, their lane is always &+~gax - // - if ( (rec_d[1] == 0) && (256 > rec_d[0]) ) { - lac = u3nc(c3y, (c3_y)rec_d[0]); - } - // otherwise, try to get the lane from cache - // - else { - lac = _ames_lane_from_cache(sam_u->lax_p, u3i_chubs(2, rec_d)); - } + else { + lac = _ames_lane_from_cache(sam_u->lax_p, u3i_chubs(2, bod_u->rec_d)); // if we don't know the lane, and the scry queue is full, // just drop the packet @@ -960,70 +916,182 @@ _ames_recv_cb(uv_udp_t* wax_u, // _ames_cap_queue. as such, blocked on u3_lord_peek_cancel or w/e. // if ( (u3_none == lac) && (1000 < sam_u->foq_d) ) { - c3_free(con_y); - sam_u->fod_d++; if ( 0 == (sam_u->fod_d % 10000) ) { u3l_log("ames: dropped %" PRIu64 " forwards total\n", sam_u->fod_d); } + + c3_free(hun_y); + return; } // if we know there's no lane, drop the packet // else if ( u3_nul == lac ) { - c3_free(con_y); - u3z(lac); + c3_free(hun_y); + return; } - // otherwise, proceed with forwarding + } + + // proceed with forwarding + // + { + // store the packet details for later processing + // + // XX allocates unnecessarily when we know the lane + // + u3_panc* pac_u = c3_calloc(sizeof(*pac_u)); + pac_u->sam_u = sam_u; + pac_u->hed_u = *hed_u; + pac_u->bod_u = *bod_u; + pac_u->ore_u = *lan_u; + pac_u->ptr_v = hun_y; + + if ( 0 != sam_u->pac_u ) { + pac_u->nex_u = sam_u->pac_u; + sam_u->pac_u->pre_u = pac_u; + } + sam_u->pac_u = pac_u; + + // if we already know the lane, just forward + // + if ( u3_none != lac ) { + _ames_forward(pac_u, lac); + } + // otherwise, there's space in the scry queue; scry the lane out of ames // else { - // store the packet details for later processing - // - u3_panc* pac_u = c3_calloc(sizeof(*pac_u)); - pac_u->sam_u = sam_u; - pac_u->hed_u = hed_u; - pac_u->bod_u.sen_d[0] = sen_d[0]; - pac_u->bod_u.sen_d[1] = sen_d[1]; - pac_u->bod_u.rec_d[0] = rec_d[0]; - pac_u->bod_u.rec_d[1] = rec_d[1]; - pac_u->bod_u.con_w = con_w; - pac_u->bod_u.con_y = con_y; - pac_u->ore_u = _ames_lane_from_sockaddr((struct sockaddr_in *)adr_u); + sam_u->foq_d++; + u3_noun pax = u3nq(u3i_string("peers"), + u3dc("scot", 'p', u3i_chubs(2, bod_u->rec_d)), + u3i_string("forward-lane"), + u3_nul); + u3_pier_peek_last(sam_u->pir_u, u3_nul, c3__ax, + u3_nul, pax, pac_u, _ames_lane_scry_cb); + } + } +} - if ( 0 != sam_u->pac_u ) { - pac_u->nex_u = sam_u->pac_u; - sam_u->pac_u->pre_u = pac_u; - } - sam_u->pac_u = pac_u; +/* _ames_hear(): parse a (potential packet), dispatch appropriately. +*/ +static void +_ames_hear(u3_ames* sam_u, + u3_lane* lan_u, + c3_w len_w, + c3_y* hun_y) +{ + u3_head hed_u; + u3_body bod_u; - // if we already know the lane, just forward - // - if ( u3_none != lac ) { - _ames_forward(pac_u, lac); + // unpack header, ensuring buffer is large enough + // + if ( (4 > len_w) + || (c3n == _ames_sift_head(&hed_u, hun_y)) ) + { + // XX track stats + // + c3_free(hun_y); + return; + } + + // ensure the protocol version matches ours + // + // XX rethink + // + if ( (c3y == sam_u->fit_o) + && (sam_u->ver_y != hed_u.ver_y) ) + { + sam_u->vet_d++; + if ( 0 == (sam_u->vet_d % 100) ) { + u3l_log("ames: %" PRIu64 " dropped for version mismatch\n", sam_u->vet_d); + } + + c3_free(hun_y); + return; + } + + { + c3_w bod_w = len_w - 4; + c3_y* bod_y = hun_y + 4; + + // ensure the mug is valid + // + if ( _ames_mug_body(bod_w, bod_y) != hed_u.mug_l ) { + sam_u->mut_d++; + if ( 0 == (sam_u->mut_d % 100) ) { + u3l_log("ames: %" PRIu64 " dropped for invalid mug\n", sam_u->mut_d); } - // otherwise, there's space in the scry queue; scry the lane out of ames + + c3_free(hun_y); + return; + } + + // unpack and validate the body + // + if ( (c3n == _ames_sift_body(&hed_u, &bod_u, bod_w, bod_y)) + || !ur_cue_test_with(sam_u->tes_u, bod_u.con_w, bod_u.con_y) ) + { + // XX track stats // - else { - sam_u->foq_d++; - u3_noun pax = u3nq(u3i_string("peers"), - u3dc("scot", 'p', u3i_chubs(2, rec_d)), - u3i_string("forward-lane"), - u3_nul); - u3_pier_peek_last(sam_u->pir_u, u3_nul, c3__ax, - u3_nul, pax, pac_u, _ames_lane_scry_cb); - } + c3_free(hun_y); + return; } } - // if we passed the filter, inject the packet + // if we can scry, + // and we are not the recipient, + // we might want to forward statelessly // - if ( c3y == pas_o ) { - c3_free(con_y); - u3_lane ore_u = _ames_lane_from_sockaddr((struct sockaddr_in *)adr_u); - u3_noun msg = u3i_bytes((c3_w)nrd_i, (c3_y*)buf_u->base); - _ames_put_packet(sam_u, msg, ore_u); + if ( (c3y == sam_u->see_o) + && ( (bod_u.rec_d[0] != sam_u->pir_u->who_d[0]) + || (bod_u.rec_d[1] != sam_u->pir_u->who_d[1]) ) ) + { + _ames_try_forward(sam_u, lan_u, &hed_u, &bod_u, hun_y); } + // otherwise, inject the packet as an event + // + else { + u3_noun msg = u3i_bytes(len_w, hun_y); + c3_free(hun_y); + _ames_put_packet(sam_u, msg, *lan_u); + } +} - c3_free(buf_u->base); +/* _ames_recv_cb(): udp message receive callback. +*/ +static void +_ames_recv_cb(uv_udp_t* wax_u, + ssize_t nrd_i, + const uv_buf_t * buf_u, + const struct sockaddr* adr_u, + unsigned flg_i) +{ + if ( 0 > nrd_i ) { + if ( u3C.wag_w & u3o_verbose ) { + u3l_log("ames: recv: fail: %s\n", uv_strerror(nrd_i)); + } + c3_free(buf_u->base); + } + else if ( 0 == nrd_i ) { + c3_free(buf_u->base); + } + else if ( flg_i & UV_UDP_PARTIAL ) { + if ( u3C.wag_w & u3o_verbose ) { + u3l_log("ames: recv: fail: message truncated\n"); + } + c3_free(buf_u->base); + } + else { + u3_ames* sam_u = wax_u->data; + struct sockaddr_in* add_u = (struct sockaddr_in*)adr_u; + u3_lane lan_u; + + lan_u.por_s = ntohs(add_u->sin_port); + lan_u.pip_w = ntohl(add_u->sin_addr.s_addr); + + // NB: [nrd_i] will never exceed max length from _ames_alloc() + // + _ames_hear(sam_u, &lan_u, (c3_w)nrd_i, (c3_y*)buf_u->base); + } } /* _ames_io_start(): initialize ames I/O. From 0ff8a28ca57e8ea00c8fc786aa091628926b765b Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Tue, 20 Oct 2020 11:40:27 -0700 Subject: [PATCH 07/16] vere: refactors ames' galaxy address resolution --- pkg/urbit/vere/io/ames.c | 294 +++++++++++++++++++++------------------ 1 file changed, 157 insertions(+), 137 deletions(-) diff --git a/pkg/urbit/vere/io/ames.c b/pkg/urbit/vere/io/ames.c index c40b435ef..614a5bd2d 100644 --- a/pkg/urbit/vere/io/ames.c +++ b/pkg/urbit/vere/io/ames.c @@ -14,12 +14,11 @@ #include "vere/vere.h" #include "ur/serial.h" -/* u3_pact: ames packet, coming or going. +/* u3_pact: outbound ames packet. */ typedef struct _u3_pact { uv_udp_send_t snd_u; // udp send request - c3_w pip_w; // target IPv4 address - c3_s por_s; // target port + u3_lane lan_u; // destination lane c3_w len_w; // length in bytes c3_y* hun_y; // packet buffer c3_y imp_y; // galaxy number (optional) @@ -341,8 +340,8 @@ _ames_send(u3_pact* pac_u) memset(&add_u, 0, sizeof(add_u)); add_u.sin_family = AF_INET; - add_u.sin_addr.s_addr = htonl(pac_u->pip_w); - add_u.sin_port = htons(pac_u->por_s); + add_u.sin_addr.s_addr = htonl(pac_u->lan_u.pip_w); + add_u.sin_port = htons(pac_u->lan_u.por_s); { uv_buf_t buf_u = uv_buf_init((c3_c*)pac_u->hun_y, pac_u->len_w); @@ -364,95 +363,6 @@ _ames_send(u3_pact* pac_u) } } -/* _ames_czar_port(): udp port for galaxy. -*/ -static c3_s -_ames_czar_port(c3_y imp_y) -{ - if ( c3n == u3_Host.ops_u.net ) { - return 31337 + imp_y; - } - else { - return 13337 + imp_y; - } -} - -/* _ames_czar_gone(): galaxy address resolution failed. -*/ -static void -_ames_czar_gone(u3_pact* pac_u, time_t now) -{ - u3_ames* sam_u = pac_u->sam_u; - - if ( c3y == sam_u->imp_o[pac_u->imp_y] ) { - u3l_log("ames: czar at %s: not found (b)\n", pac_u->dns_c); - sam_u->imp_o[pac_u->imp_y] = c3n; - } - - if ( (0 == sam_u->imp_w[pac_u->imp_y]) || - (0xffffffff == sam_u->imp_w[pac_u->imp_y]) ) - { - sam_u->imp_w[pac_u->imp_y] = 0xffffffff; - } - - // keep existing ip for 5 more minutes - // - sam_u->imp_t[pac_u->imp_y] = now; - - _ames_pact_free(pac_u); -} - -/* _ames_czar_cb(): galaxy address resolution callback. -*/ -static void -_ames_czar_cb(uv_getaddrinfo_t* adr_u, - c3_i sas_i, - struct addrinfo* aif_u) -{ - u3_pact* pac_u = (u3_pact*)adr_u->data; - u3_ames* sam_u = pac_u->sam_u; - time_t now = time(0); - - struct addrinfo* rai_u = aif_u; - - while ( 1 ) { - if ( !rai_u ) { - _ames_czar_gone(pac_u, now); - break; - } - - if ( (AF_INET == rai_u->ai_family) ) { - struct sockaddr_in* add_u = (struct sockaddr_in *)rai_u->ai_addr; - c3_w old_w = sam_u->imp_w[pac_u->imp_y]; - - sam_u->imp_w[pac_u->imp_y] = ntohl(add_u->sin_addr.s_addr); - sam_u->imp_t[pac_u->imp_y] = now; - sam_u->imp_o[pac_u->imp_y] = c3y; - -#if 1 - if ( sam_u->imp_w[pac_u->imp_y] != old_w - && sam_u->imp_w[pac_u->imp_y] != 0xffffffff ) { - u3_noun wad = u3i_words(1, &sam_u->imp_w[pac_u->imp_y]); - u3_noun nam = u3dc("scot", c3__if, wad); - c3_c* nam_c = u3r_string(nam); - - u3l_log("ames: czar %s: ip %s\n", pac_u->dns_c, nam_c); - - c3_free(nam_c); u3z(nam); - } -#endif - - _ames_send(pac_u); - break; - } - - rai_u = rai_u->ai_next; - } - - c3_free(adr_u); - uv_freeaddrinfo(aif_u); -} - /* u3_ames_decode_lane(): deserialize noun to lane */ u3_lane @@ -594,24 +504,120 @@ _ames_serialize_packet(u3_panc* pac_u, c3_o dop_o) } } -/* _ames_czar(): galaxy address resolution. +/* _ames_czar_port(): udp port for galaxy. +*/ +static c3_s +_ames_czar_port(c3_y imp_y) +{ + if ( c3n == u3_Host.ops_u.net ) { + return 31337 + imp_y; + } + else { + return 13337 + imp_y; + } +} + +/* _ames_czar_gone(): galaxy address resolution failed. */ static void -_ames_czar(u3_pact* pac_u, c3_c* bos_c) +_ames_czar_gone(u3_pact* pac_u, time_t now) { u3_ames* sam_u = pac_u->sam_u; - pac_u->por_s = _ames_czar_port(pac_u->imp_y); + if ( c3y == sam_u->imp_o[pac_u->imp_y] ) { + u3l_log("ames: czar at %s: not found (b)\n", pac_u->dns_c); + sam_u->imp_o[pac_u->imp_y] = c3n; + } + + if ( (0 == sam_u->imp_w[pac_u->imp_y]) || + (0xffffffff == sam_u->imp_w[pac_u->imp_y]) ) + { + sam_u->imp_w[pac_u->imp_y] = 0xffffffff; + } + + // keep existing ip for 5 more minutes + // + sam_u->imp_t[pac_u->imp_y] = now; + + _ames_pact_free(pac_u); +} + +/* _ames_czar_here(): galaxy address resolution succeeded. +*/ +static void +_ames_czar_here(u3_pact* pac_u, time_t now, struct sockaddr_in* add_u) +{ + u3_ames* sam_u = pac_u->sam_u; + c3_w old_w = sam_u->imp_w[pac_u->imp_y]; + c3_w pip_w = ntohl(add_u->sin_addr.s_addr); + + if ( pip_w != old_w ) { + u3_noun nam = u3dc("scot", c3__if, u3i_word(pip_w)); + c3_c* nam_c = u3r_string(nam); + + u3l_log("ames: czar %s: ip %s\n", pac_u->dns_c, nam_c); + + c3_free(nam_c); + u3z(nam); + } + + sam_u->imp_w[pac_u->imp_y] = pip_w; + sam_u->imp_t[pac_u->imp_y] = now; + sam_u->imp_o[pac_u->imp_y] = c3y; + + pac_u->lan_u.pip_w = pip_w; + _ames_send(pac_u); +} + +/* _ames_czar_cb(): galaxy address resolution callback. +*/ +static void +_ames_czar_cb(uv_getaddrinfo_t* adr_u, + c3_i sas_i, + struct addrinfo* aif_u) +{ + { + u3_pact* pac_u = (u3_pact*)adr_u->data; + struct addrinfo* rai_u = aif_u; + time_t now = time(0); + + while ( rai_u ) { + if ( (AF_INET == rai_u->ai_family) ) { + _ames_czar_here(pac_u, now, (struct sockaddr_in *)rai_u->ai_addr); + break; + } + else { + rai_u = rai_u->ai_next; + } + } + + if ( !rai_u ) { + _ames_czar_gone(pac_u, now); + } + } + + c3_free(adr_u); + uv_freeaddrinfo(aif_u); +} + +/* _ames_czar(): galaxy address resolution. +*/ +static void +_ames_czar(u3_pact* pac_u) +{ + u3_ames* sam_u = pac_u->sam_u; + + pac_u->lan_u.por_s = _ames_czar_port(pac_u->imp_y); if ( c3n == u3_Host.ops_u.net ) { - pac_u->pip_w = 0x7f000001; + pac_u->lan_u.pip_w = 0x7f000001; _ames_send(pac_u); return; } // if we don't have a galaxy domain, no-op // - if ( 0 == bos_c ) { + if ( !sam_u->dns_c ) { u3_noun nam = u3dc("scot", 'p', pac_u->imp_y); c3_c* nam_c = u3r_string(nam); u3l_log("ames: no galaxy domain for %s, no-op\r\n", nam_c); @@ -621,48 +627,64 @@ _ames_czar(u3_pact* pac_u, c3_c* bos_c) return; } - time_t now = time(0); - - // backoff - if ( (0xffffffff == sam_u->imp_w[pac_u->imp_y]) && - (now - sam_u->imp_t[pac_u->imp_y]) < 300 ) { - _ames_pact_free(pac_u); - return; - } - - if ( (0 == sam_u->imp_w[pac_u->imp_y]) || - (now - sam_u->imp_t[pac_u->imp_y]) > 300 ) { /* 5 minute TTL */ - u3_noun nam = u3dc("scot", 'p', pac_u->imp_y); - c3_c* nam_c = u3r_string(nam); - // XX remove extra byte for '~' - pac_u->dns_c = c3_malloc(1 + strlen(bos_c) + 1 + strlen(nam_c)); - - snprintf(pac_u->dns_c, 256, "%s.%s", nam_c + 1, bos_c); - // u3l_log("czar %s, dns %s\n", nam_c, pac_u->dns_c); - - c3_free(nam_c); - u3z(nam); + { + c3_w pip_w = sam_u->imp_w[pac_u->imp_y]; + time_t wen = sam_u->imp_t[pac_u->imp_y]; + time_t now = time(0); + // backoff for 5 minutes after failed lookup + // + if ( ( now < wen ) // time shenanigans! + || ( (0xffffffff == pip_w) // sentinal ip address + && ((now - wen) < 300) ) ) { - uv_getaddrinfo_t* adr_u = c3_malloc(sizeof(*adr_u)); - adr_u->data = pac_u; - + _ames_pact_free(pac_u); + return; + } + // cached addresses have a 5 minute TTL + // + else if ( (0 != pip_w) && ((now - wen) < 300) ) { + pac_u->lan_u.pip_w = pip_w; + _ames_send(pac_u); + return; + } + else { c3_i sas_i; - if ( 0 != (sas_i = uv_getaddrinfo(u3L, adr_u, - _ames_czar_cb, - pac_u->dns_c, 0, 0)) ) { - u3l_log("ames: %s\n", uv_strerror(sas_i)); - _ames_czar_gone(pac_u, now); + { + u3_noun nam = u3dc("scot", 'p', pac_u->imp_y); + c3_c* nam_c = u3r_string(nam); + + // NB: . separator not counted, as [nam_c] includes a ~ that we skip + // + pac_u->dns_c = c3_malloc(1 + strlen(nam_c) + strlen(sam_u->dns_c)); + sas_i = snprintf(pac_u->dns_c, 255, "%s.%s", nam_c + 1, sam_u->dns_c); + + c3_free(nam_c); + u3z(nam); + } + + if ( 0 != sas_i ) { + u3l_log("ames: czar: galaxy domain %s truncated\n", sam_u->dns_c); + _ames_pact_free(pac_u); return; } + + { + uv_getaddrinfo_t* adr_u = c3_malloc(sizeof(*adr_u)); + adr_u->data = pac_u; + + if ( 0 != (sas_i = uv_getaddrinfo(u3L, adr_u, + _ames_czar_cb, + pac_u->dns_c, 0, 0)) ) + { + u3l_log("ames: %s\n", uv_strerror(sas_i)); + _ames_czar_gone(pac_u, now); + return; + } + } } } - else { - pac_u->pip_w = sam_u->imp_w[pac_u->imp_y]; - _ames_send(pac_u); - return; - } } /* _ames_ef_send(): send packet to network (v4). @@ -694,7 +716,7 @@ _ames_ef_send(u3_ames* sam_u, u3_noun lan, u3_noun pac) c3_assert( val < 256 ); pac_u->imp_y = val; - _ames_czar(pac_u, sam_u->dns_c); + _ames_czar(pac_u); } // non-galaxy lane // @@ -711,9 +733,7 @@ _ames_ef_send(u3_ames* sam_u, u3_noun lan, u3_noun pac) // otherwise, mutate destination and send packet // else { - pac_u->pip_w = lan_u.pip_w; - pac_u->por_s = lan_u.por_s; - + pac_u->lan_u = lan_u; _ames_send(pac_u); } } From 0e03bea08dfce86d17f7efa717bd8282cd024ae1 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Tue, 20 Oct 2020 12:08:36 -0700 Subject: [PATCH 08/16] vere: groups counters and config in ames' state structure --- pkg/urbit/vere/io/ames.c | 143 ++++++++++++++++++++------------------- 1 file changed, 73 insertions(+), 70 deletions(-) diff --git a/pkg/urbit/vere/io/ames.c b/pkg/urbit/vere/io/ames.c index 614a5bd2d..5134e537d 100644 --- a/pkg/urbit/vere/io/ames.c +++ b/pkg/urbit/vere/io/ames.c @@ -31,30 +31,34 @@ typedef struct _u3_ames { // packet network state u3_auto car_u; // driver u3_pier* pir_u; // pier - union { // + union { // uv udp handle uv_udp_t wax_u; // uv_handle_t had_u; // }; // ur_cue_test_t* tes_u; // cue-test handle u3_cue_xeno* sil_u; // cue handle c3_c* dns_c; // domain XX multiple/fallback - c3_d dop_d; // drop count - c3_d fal_d; // crash count + c3_y ver_y; // protocol version + u3p(u3h_root) lax_p; // lane scry cache + struct _u3_panc* pac_u; // packets pending forwards c3_w imp_w[256]; // imperial IPs time_t imp_t[256]; // imperial IP timestamps c3_o imp_o[256]; // imperial print status - c3_o net_o; // can send - c3_o see_o; // can scry - c3_d saw_d; // successive scry failures - c3_o fit_o; // filtering active - c3_y ver_y; // protocol version - u3p(u3h_root) lax_p; // lane scry cache - c3_d vet_d; // version mismatches filtered - c3_d mut_d; // invalid mugs filtered - struct _u3_panc* pac_u; // packets pending forwards - c3_d foq_d; // forward queue size - c3_d fow_d; // forwarded count - c3_d fod_d; // forwards dropped count + struct { // config: + c3_o net_o; // can send + c3_o see_o; // can scry + c3_o fit_o; // filtering active + } fig_u; // + struct { // stats: + c3_d dop_d; // drop count + c3_d fal_d; // crash count + c3_d saw_d; // successive scry failures + c3_d vet_d; // version mismatches filtered + c3_d mut_d; // invalid mugs filtered + c3_d foq_d; // forward queue size + c3_d fow_d; // forwarded count + c3_d fod_d; // forwards dropped count + } sat_u; // } u3_ames; /* u3_head: ames packet header @@ -313,12 +317,12 @@ _ames_send_cb(uv_udp_send_t* req_u, c3_i sas_i) u3_pact* pac_u = (u3_pact*)req_u; u3_ames* sam_u = pac_u->sam_u; - if ( sas_i && (c3y == sam_u->net_o) ) { + if ( sas_i && (c3y == sam_u->fig_u.net_o) ) { u3l_log("ames: send fail: %s\n", uv_strerror(sas_i)); - sam_u->net_o = c3n; + sam_u->fig_u.net_o = c3n; } else { - sam_u->net_o = c3y; + sam_u->fig_u.net_o = c3y; } _ames_pact_free(pac_u); @@ -352,9 +356,9 @@ _ames_send(u3_pact* pac_u) _ames_send_cb); if ( sas_i ) { - if ( c3y == sam_u->net_o ) { + if ( c3y == sam_u->fig_u.net_o ) { u3l_log("ames: send fail: %s\n", uv_strerror(sas_i)); - sam_u->net_o = c3n; + sam_u->fig_u.net_o = c3n; } _ames_pact_free(pac_u); @@ -752,20 +756,20 @@ _ames_cap_queue(u3_ames* sam_u) if ( c3__hear == u3h(egg_u->cad) ) { u3_auto_drop(&sam_u->car_u, egg_u); - sam_u->dop_d++; + sam_u->sat_u.dop_d++; if ( u3C.wag_w & u3o_verbose ) { - u3l_log("ames: packet dropped (%" PRIu64 " total)\n", sam_u->dop_d); + u3l_log("ames: packet dropped (%" PRIu64 " total)\n", sam_u->sat_u.dop_d); } } egg_u = nex_u; } - if ( (sam_u->dop_d && (0 == (sam_u->dop_d % 1000))) + if ( (sam_u->sat_u.dop_d && (0 == (sam_u->sat_u.dop_d % 1000))) && !(u3C.wag_w & u3o_verbose) ) { - u3l_log("ames: packet dropped (%" PRIu64 " total)\n", sam_u->dop_d); + u3l_log("ames: packet dropped (%" PRIu64 " total)\n", sam_u->sat_u.dop_d); } } @@ -798,19 +802,19 @@ static void _ames_hear_bail(u3_ovum* egg_u, u3_noun lud) { u3_ames* sam_u = (u3_ames*)egg_u->car_u; - sam_u->fal_d++; + sam_u->sat_u.fal_d++; if ( (u3C.wag_w & u3o_verbose) - || (0 == (sam_u->fal_d % 1000)) ) + || (0 == (sam_u->sat_u.fal_d % 1000)) ) { _ames_punt_goof(lud); - u3l_log("ames: packet failed (%" PRIu64 " total)\n\n", sam_u->fal_d); + u3l_log("ames: packet failed (%" PRIu64 " total)\n\n", sam_u->sat_u.fal_d); } else { u3z(lud); - if ( 0 == (sam_u->fal_d % 1000) ) { - u3l_log("ames: packet failed (%" PRIu64 " total)\n\n", sam_u->fal_d); + if ( 0 == (sam_u->sat_u.fal_d % 1000) ) { + u3l_log("ames: packet failed (%" PRIu64 " total)\n\n", sam_u->sat_u.fal_d); } } @@ -840,16 +844,18 @@ _ames_put_packet(u3_ames* sam_u, static void _ames_forward(u3_panc* pac_u, u3_noun las) { - pac_u->sam_u->fow_d++; - if ( 0 == (pac_u->sam_u->fow_d % 1000000) ) { - u3l_log("ames: forwarded %" PRIu64 " total\n", pac_u->sam_u->fow_d); + u3_ames* sam_u = pac_u->sam_u; + + sam_u->sat_u.fow_d++; + if ( 0 == (sam_u->sat_u.fow_d % 1000000) ) { + u3l_log("ames: forwarded %" PRIu64 " total\n", sam_u->sat_u.fow_d); } { u3_noun los = las; u3_noun pac = _ames_serialize_packet(pac_u, c3y); while ( u3_nul != las ) { - _ames_ef_send(pac_u->sam_u, u3k(u3h(las)), u3k(pac)); + _ames_ef_send(sam_u, u3k(u3h(las)), u3k(pac)); las = u3t(las); } u3z(los); u3z(pac); @@ -864,28 +870,27 @@ static void _ames_lane_scry_cb(void* vod_p, u3_noun nun) { u3_panc* pac_u = vod_p; - u3_weak las = u3r_at(7, nun); + u3_ames* sam_u = pac_u->sam_u; + u3_weak las = u3r_at(7, nun); - pac_u->sam_u->foq_d--; + sam_u->sat_u.foq_d--; // if scry fails, remember we can't scry, and just inject the packet // if ( u3_none == las ) { - if ( 5 < ++pac_u->sam_u->saw_d ) { + if ( 5 < ++sam_u->sat_u.saw_d ) { u3l_log("ames: giving up scry\n"); - pac_u->sam_u->see_o = c3n; + sam_u->fig_u.see_o = c3n; } - _ames_put_packet(pac_u->sam_u, - _ames_serialize_packet(pac_u, c3n), - pac_u->ore_u); + _ames_put_packet(sam_u, _ames_serialize_packet(pac_u, c3n), pac_u->ore_u); _ames_panc_free(pac_u); } else { - pac_u->sam_u->saw_d = 0; + sam_u->sat_u.saw_d = 0; // cache the scry result for later use // - _ames_lane_into_cache(pac_u->sam_u->lax_p, + _ames_lane_into_cache(sam_u->lax_p, u3i_chubs(2, pac_u->bod_u.rec_d), u3k(las)); @@ -935,10 +940,10 @@ _ames_try_forward(u3_ames* sam_u, // so can't pluck these out of the event queue like it does in // _ames_cap_queue. as such, blocked on u3_lord_peek_cancel or w/e. // - if ( (u3_none == lac) && (1000 < sam_u->foq_d) ) { - sam_u->fod_d++; - if ( 0 == (sam_u->fod_d % 10000) ) { - u3l_log("ames: dropped %" PRIu64 " forwards total\n", sam_u->fod_d); + if ( (u3_none == lac) && (1000 < sam_u->sat_u.foq_d) ) { + sam_u->sat_u.fod_d++; + if ( 0 == (sam_u->sat_u.fod_d % 10000) ) { + u3l_log("ames: dropped %" PRIu64 " forwards total\n", sam_u->sat_u.fod_d); } c3_free(hun_y); @@ -980,7 +985,7 @@ _ames_try_forward(u3_ames* sam_u, // otherwise, there's space in the scry queue; scry the lane out of ames // else { - sam_u->foq_d++; + sam_u->sat_u.foq_d++; u3_noun pax = u3nq(u3i_string("peers"), u3dc("scot", 'p', u3i_chubs(2, bod_u->rec_d)), u3i_string("forward-lane"), @@ -1017,12 +1022,12 @@ _ames_hear(u3_ames* sam_u, // // XX rethink // - if ( (c3y == sam_u->fit_o) + if ( (c3y == sam_u->fig_u.fit_o) && (sam_u->ver_y != hed_u.ver_y) ) { - sam_u->vet_d++; - if ( 0 == (sam_u->vet_d % 100) ) { - u3l_log("ames: %" PRIu64 " dropped for version mismatch\n", sam_u->vet_d); + sam_u->sat_u.vet_d++; + if ( 0 == (sam_u->sat_u.vet_d % 100) ) { + u3l_log("ames: %" PRIu64 " dropped for version mismatch\n", sam_u->sat_u.vet_d); } c3_free(hun_y); @@ -1036,9 +1041,9 @@ _ames_hear(u3_ames* sam_u, // ensure the mug is valid // if ( _ames_mug_body(bod_w, bod_y) != hed_u.mug_l ) { - sam_u->mut_d++; - if ( 0 == (sam_u->mut_d % 100) ) { - u3l_log("ames: %" PRIu64 " dropped for invalid mug\n", sam_u->mut_d); + sam_u->sat_u.mut_d++; + if ( 0 == (sam_u->sat_u.mut_d % 100) ) { + u3l_log("ames: %" PRIu64 " dropped for invalid mug\n", sam_u->sat_u.mut_d); } c3_free(hun_y); @@ -1061,7 +1066,7 @@ _ames_hear(u3_ames* sam_u, // and we are not the recipient, // we might want to forward statelessly // - if ( (c3y == sam_u->see_o) + if ( (c3y == sam_u->fig_u.see_o) && ( (bod_u.rec_d[0] != sam_u->pir_u->who_d[0]) || (bod_u.rec_d[1] != sam_u->pir_u->who_d[1]) ) ) { @@ -1222,7 +1227,7 @@ static void _ames_prot_scry_cb(void* vod_p, u3_noun nun) { u3_ames* sam_u = vod_p; - u3_weak ver = u3r_at(7, nun); + u3_weak ver = u3r_at(7, nun); if ( u3_none == ver ) { // assume protocol version 0 @@ -1238,7 +1243,7 @@ _ames_prot_scry_cb(void* vod_p, u3_noun nun) sam_u->ver_y = ver; } - sam_u->fit_o = c3y; + sam_u->fig_u.fit_o = c3y; u3z(nun); } @@ -1387,14 +1392,14 @@ static void _ames_io_info(u3_auto* car_u) { u3_ames* sam_u = (u3_ames*)car_u; - u3l_log(" dropped: %" PRIu64 "\n", sam_u->dop_d); - u3l_log(" forwards dropped: %" PRIu64 "\n", sam_u->fod_d); - u3l_log(" forwards pending: %" PRIu64 "\n", sam_u->foq_d); - u3l_log(" forwarded: %" PRIu64 "\n", sam_u->fow_d); - u3l_log(" filtered (ver): %" PRIu64 "\n", sam_u->vet_d); - u3l_log(" filtered (mug): %" PRIu64 "\n", sam_u->mut_d); - u3l_log(" crashed: %" PRIu64 "\n", sam_u->fal_d); - u3l_log(" cached lanes: %u\n", u3to(u3h_root, sam_u->lax_p)->use_w); + u3l_log(" dropped: %" PRIu64 "\n", sam_u->sat_u.dop_d); + u3l_log(" forwards dropped: %" PRIu64 "\n", sam_u->sat_u.fod_d); + u3l_log(" forwards pending: %" PRIu64 "\n", sam_u->sat_u.foq_d); + u3l_log(" forwarded: %" PRIu64 "\n", sam_u->sat_u.fow_d); + u3l_log(" filtered (ver): %" PRIu64 "\n", sam_u->sat_u.vet_d); + u3l_log(" filtered (mug): %" PRIu64 "\n", sam_u->sat_u.mut_d); + u3l_log(" crashed: %" PRIu64 "\n", sam_u->sat_u.fal_d); + u3l_log(" cached lanes: %u\n", u3h_wyt(sam_u->lax_p)); } /* u3_ames_io_init(): initialize ames I/O. @@ -1404,11 +1409,9 @@ u3_ames_io_init(u3_pier* pir_u) { u3_ames* sam_u = c3_calloc(sizeof(*sam_u)); sam_u->pir_u = pir_u; - sam_u->dop_d = 0; - sam_u->net_o = c3y; - sam_u->see_o = c3y; - sam_u->fit_o = c3n; - sam_u->foq_d = 0; + sam_u->fig_u.net_o = c3y; + sam_u->fig_u.see_o = c3y; + sam_u->fig_u.fit_o = c3n; //NOTE some numbers on memory usage for the lane cache // From 7b4e48deb7d892379b8d49896ed4675649d0ee31 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Tue, 20 Oct 2020 15:30:41 -0700 Subject: [PATCH 09/16] vere: updates ames' info handler to also print config --- pkg/urbit/vere/io/ames.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/pkg/urbit/vere/io/ames.c b/pkg/urbit/vere/io/ames.c index 5134e537d..d8e47987e 100644 --- a/pkg/urbit/vere/io/ames.c +++ b/pkg/urbit/vere/io/ames.c @@ -1392,14 +1392,22 @@ static void _ames_io_info(u3_auto* car_u) { u3_ames* sam_u = (u3_ames*)car_u; - u3l_log(" dropped: %" PRIu64 "\n", sam_u->sat_u.dop_d); - u3l_log(" forwards dropped: %" PRIu64 "\n", sam_u->sat_u.fod_d); - u3l_log(" forwards pending: %" PRIu64 "\n", sam_u->sat_u.foq_d); - u3l_log(" forwarded: %" PRIu64 "\n", sam_u->sat_u.fow_d); - u3l_log(" filtered (ver): %" PRIu64 "\n", sam_u->sat_u.vet_d); - u3l_log(" filtered (mug): %" PRIu64 "\n", sam_u->sat_u.mut_d); - u3l_log(" crashed: %" PRIu64 "\n", sam_u->sat_u.fal_d); - u3l_log(" cached lanes: %u\n", u3h_wyt(sam_u->lax_p)); + +# define FLAG(a) ( (c3y == a) ? "&" : "|" ) + + u3l_log(" config:\n"); + u3l_log(" filtering: %s\n", FLAG(sam_u->fig_u.fit_o)); + u3l_log(" can send: %s\n", FLAG(sam_u->fig_u.net_o)); + u3l_log(" can scry: %s\n", FLAG(sam_u->fig_u.see_o)); + u3l_log(" counters:\n"); + u3l_log(" dropped: %" PRIu64 "\n", sam_u->sat_u.dop_d); + u3l_log(" forwards dropped: %" PRIu64 "\n", sam_u->sat_u.fod_d); + u3l_log(" forwards pending: %" PRIu64 "\n", sam_u->sat_u.foq_d); + u3l_log(" forwarded: %" PRIu64 "\n", sam_u->sat_u.fow_d); + u3l_log(" filtered (ver): %" PRIu64 "\n", sam_u->sat_u.vet_d); + u3l_log(" filtered (mug): %" PRIu64 "\n", sam_u->sat_u.mut_d); + u3l_log(" crashed: %" PRIu64 "\n", sam_u->sat_u.fal_d); + u3l_log(" cached lanes: %u\n", u3h_wyt(sam_u->lax_p)); } /* u3_ames_io_init(): initialize ames I/O. From 67f06ce5e5dbd7912ba8a6b9055c2dc28614a411 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Tue, 20 Oct 2020 15:44:58 -0700 Subject: [PATCH 10/16] vere: adds comments discussing protocol-version constraints --- pkg/urbit/vere/io/ames.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/pkg/urbit/vere/io/ames.c b/pkg/urbit/vere/io/ames.c index d8e47987e..163469439 100644 --- a/pkg/urbit/vere/io/ames.c +++ b/pkg/urbit/vere/io/ames.c @@ -1007,6 +1007,19 @@ _ames_hear(u3_ames* sam_u, u3_head hed_u; u3_body bod_u; + // XX packet filtering needs to revised for two protocol-change scenarios + // + // - packets using old protocol versions from our sponsees + // these must be let through, and this is a transitive condition; + // they must also be forwarded where appropriate + // they can be validated, as we know their semantics + // + // - packets using newer protocol versions + // these should probably be let through, or at least + // trigger printfs suggesting upgrade. + // they cannot be filtered, as we do not know their semantics + // + // unpack header, ensuring buffer is large enough // if ( (4 > len_w) @@ -1020,7 +1033,7 @@ _ames_hear(u3_ames* sam_u, // ensure the protocol version matches ours // - // XX rethink + // XX rethink use of [fit_o] here and elsewhere // if ( (c3y == sam_u->fig_u.fit_o) && (sam_u->ver_y != hed_u.ver_y) ) @@ -1243,6 +1256,9 @@ _ames_prot_scry_cb(void* vod_p, u3_noun nun) sam_u->ver_y = ver; } + // XX revise: filtering should probably be disabled if + // we get a protocol version above the latest one we know + // sam_u->fig_u.fit_o = c3y; u3z(nun); } From fcd6968e8ec0ebdd3041a63532fe6bd53ff15a09 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Tue, 20 Oct 2020 17:14:52 -0700 Subject: [PATCH 11/16] vere: adds comment about re-checking ames' protocol version --- pkg/urbit/vere/io/ames.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkg/urbit/vere/io/ames.c b/pkg/urbit/vere/io/ames.c index 163469439..bdc488afa 100644 --- a/pkg/urbit/vere/io/ames.c +++ b/pkg/urbit/vere/io/ames.c @@ -1282,6 +1282,10 @@ _ames_io_talk(u3_auto* car_u) // scry the protocol version out of arvo // + // XX this should be re-triggered periodically, + // or, better yet, %ames should emit a %turf + // (or some other reconfig) effect when it is reset. + // u3_pier_peek_last(car_u->pir_u, u3_nul, c3__ax, u3_nul, u3nt(u3i_string("protocol"), u3i_string("version"), u3_nul), sam_u, _ames_prot_scry_cb); From 7a5d57ce916b58d752a741cfed76e88d2ce54b13 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Thu, 22 Oct 2020 10:51:20 -0700 Subject: [PATCH 12/16] vere: adds ames stats for failed header/body parsing --- pkg/urbit/vere/io/ames.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/pkg/urbit/vere/io/ames.c b/pkg/urbit/vere/io/ames.c index bdc488afa..5e79d000d 100644 --- a/pkg/urbit/vere/io/ames.c +++ b/pkg/urbit/vere/io/ames.c @@ -53,8 +53,10 @@ c3_d dop_d; // drop count c3_d fal_d; // crash count c3_d saw_d; // successive scry failures + c3_d hed_d; // failed to read header c3_d vet_d; // version mismatches filtered c3_d mut_d; // invalid mugs filtered + c3_d bod_d; // failed to read body c3_d foq_d; // forward queue size c3_d fow_d; // forwarded count c3_d fod_d; // forwards dropped count @@ -1025,8 +1027,11 @@ _ames_hear(u3_ames* sam_u, if ( (4 > len_w) || (c3n == _ames_sift_head(&hed_u, hun_y)) ) { - // XX track stats - // + sam_u->sat_u.hed_d++; + if ( 0 == (sam_u->sat_u.hed_d % 100) ) { + u3l_log("ames: %" PRIu64 " dropped, failed to read header\n", sam_u->sat_u.hed_d); + } + c3_free(hun_y); return; } @@ -1068,8 +1073,11 @@ _ames_hear(u3_ames* sam_u, if ( (c3n == _ames_sift_body(&hed_u, &bod_u, bod_w, bod_y)) || !ur_cue_test_with(sam_u->tes_u, bod_u.con_w, bod_u.con_y) ) { - // XX track stats - // + sam_u->sat_u.bod_d++; + if ( 0 == (sam_u->sat_u.bod_d % 100) ) { + u3l_log("ames: %" PRIu64 " dropped, failed to read body\n", sam_u->sat_u.bod_d); + } + c3_free(hun_y); return; } @@ -1424,8 +1432,10 @@ _ames_io_info(u3_auto* car_u) u3l_log(" forwards dropped: %" PRIu64 "\n", sam_u->sat_u.fod_d); u3l_log(" forwards pending: %" PRIu64 "\n", sam_u->sat_u.foq_d); u3l_log(" forwarded: %" PRIu64 "\n", sam_u->sat_u.fow_d); + u3l_log(" filtered (hed): %" PRIu64 "\n", sam_u->sat_u.hed_d); u3l_log(" filtered (ver): %" PRIu64 "\n", sam_u->sat_u.vet_d); u3l_log(" filtered (mug): %" PRIu64 "\n", sam_u->sat_u.mut_d); + u3l_log(" filtered (bod): %" PRIu64 "\n", sam_u->sat_u.bod_d); u3l_log(" crashed: %" PRIu64 "\n", sam_u->sat_u.fal_d); u3l_log(" cached lanes: %u\n", u3h_wyt(sam_u->lax_p)); } From c5870b60d12bbb4d36f9d842b5936ff8e44261f0 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Thu, 22 Oct 2020 10:52:43 -0700 Subject: [PATCH 13/16] vere: updates ames future-refactoring comments for accuracy --- pkg/urbit/vere/io/ames.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pkg/urbit/vere/io/ames.c b/pkg/urbit/vere/io/ames.c index 5e79d000d..7f1186423 100644 --- a/pkg/urbit/vere/io/ames.c +++ b/pkg/urbit/vere/io/ames.c @@ -190,7 +190,7 @@ _ames_etch_head(u3_head* hed_u, c3_y buf_y[4]) } /* _ames_chub_bytes(): c3_y[8] to c3_d -** XX move +** XX factor out, deduplicate with other conversions */ static inline c3_d _ames_chub_bytes(c3_y byt_y[8]) @@ -206,7 +206,6 @@ _ames_chub_bytes(c3_y byt_y[8]) } /* _ames_ship_to_chubs(): pack [len_y] bytes into c3_d[2] -** XX move */ static inline void _ames_ship_to_chubs(c3_d sip_d[2], c3_y len_y, c3_y* buf_y) @@ -219,7 +218,7 @@ _ames_ship_to_chubs(c3_d sip_d[2], c3_y len_y, c3_y* buf_y) } /* _ames_chub_bytes(): c3_d to c3_y[8] -** XX move +** XX factor out, deduplicate with other conversions */ static inline void _ames_bytes_chub(c3_y byt_y[8], c3_d num_d) @@ -235,7 +234,6 @@ _ames_bytes_chub(c3_y byt_y[8], c3_d num_d) } /* _ames_ship_of_chubs(): unpack c3_d[2] into [len_y] bytes. -** XX move */ static inline void _ames_ship_of_chubs(c3_d sip_d[2], c3_y len_y, c3_y* buf_y) From caa21b04cf793b87dff8a7b286590c06cdd424c3 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Thu, 22 Oct 2020 15:37:48 -0700 Subject: [PATCH 14/16] vere: adds (verbose, debug) ames packet forwarding printfs --- pkg/urbit/vere/io/ames.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/pkg/urbit/vere/io/ames.c b/pkg/urbit/vere/io/ames.c index 7f1186423..054a725da 100644 --- a/pkg/urbit/vere/io/ames.c +++ b/pkg/urbit/vere/io/ames.c @@ -851,6 +851,22 @@ _ames_forward(u3_panc* pac_u, u3_noun las) u3l_log("ames: forwarded %" PRIu64 " total\n", sam_u->sat_u.fow_d); } + if ( u3C.wag_w & u3o_verbose ) { + u3_noun sen = u3dc("scot", 'p', u3i_chubs(2, pac_u->bod_u.sen_d)); + u3_noun rec = u3dc("scot", 'p', u3i_chubs(2, pac_u->bod_u.rec_d)); + c3_c* sen_c = u3r_string(sen); + c3_c* rec_c = u3r_string(rec); + c3_y* pip_y = (c3_y*)&pac_u->ore_u.pip_w; + + u3l_log("ames: forwarding for %s to %s from %d.%d.%d.%d:%d\n", + sen_c, rec_c, + pip_y[0], pip_y[1], pip_y[2], pip_y[3], + pac_u->ore_u.por_s); + + c3_free(sen_c); c3_free(rec_c); + u3z(sen); u3z(rec); + } + { u3_noun los = las; u3_noun pac = _ames_serialize_packet(pac_u, c3y); From e59863bded8671a18a6cb8882dc8fc94785879b4 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Thu, 22 Oct 2020 15:38:25 -0700 Subject: [PATCH 15/16] vere: partially prevent forwarding loops by excluding self-galaxy lanes --- pkg/urbit/vere/io/ames.c | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/pkg/urbit/vere/io/ames.c b/pkg/urbit/vere/io/ames.c index 054a725da..3a7242560 100644 --- a/pkg/urbit/vere/io/ames.c +++ b/pkg/urbit/vere/io/ames.c @@ -868,16 +868,46 @@ _ames_forward(u3_panc* pac_u, u3_noun las) } { - u3_noun los = las; u3_noun pac = _ames_serialize_packet(pac_u, c3y); - while ( u3_nul != las ) { - _ames_ef_send(sam_u, u3k(u3h(las)), u3k(pac)); - las = u3t(las); + u3_noun tag, dat, lan, t = las; + + while ( u3_nul != t ) { + u3x_cell(t, &lan, &t); + + // validate lane and skip self if galaxy + // + if ( c3n == u3r_cell(lan, &tag, &dat) ) { + u3l_log("ames: bogus lane\n"); + u3m_p("lan", lan); + } + else { + c3_o sen_o = c3y; + c3_d who_d[2]; + + if ( c3y == tag ) { + u3r_chubs(0, 2, who_d, dat); + + if ( (who_d[0] == sam_u->pir_u->who_d[0]) + && (who_d[1] == sam_u->pir_u->who_d[1]) ) + { + sen_o = c3n; + if ( u3C.wag_w & u3o_verbose ) { + u3l_log("ames: forward skipping self\n"); + } + } + } + + if ( c3y == sen_o ) { + _ames_ef_send(sam_u, u3k(lan), u3k(pac)); + } + } } - u3z(los); u3z(pac); + + u3z(pac); } _ames_panc_free(pac_u); + u3z(las); } /* _ames_lane_scry_cb(): learn lane to forward packet on From 9cc9a308bf217b382d910df9455bb75da1f88742 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Mon, 26 Oct 2020 13:32:56 -0700 Subject: [PATCH 16/16] vere: fix snprintf return-code check in ames --- pkg/urbit/vere/io/ames.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/urbit/vere/io/ames.c b/pkg/urbit/vere/io/ames.c index 3a7242560..ae0656c3f 100644 --- a/pkg/urbit/vere/io/ames.c +++ b/pkg/urbit/vere/io/ames.c @@ -668,7 +668,7 @@ _ames_czar(u3_pact* pac_u) u3z(nam); } - if ( 0 != sas_i ) { + if ( 255 <= sas_i ) { u3l_log("ames: czar: galaxy domain %s truncated\n", sam_u->dns_c); _ames_pact_free(pac_u); return;