From 0d2c6a441f674b2d0760aa65a22335da51c314d6 Mon Sep 17 00:00:00 2001 From: Liam Fitzgerald Date: Tue, 14 Nov 2023 00:55:35 +0000 Subject: [PATCH 001/430] wip --- pkg/c3/motes.h | 1 + pkg/vere/auto.c | 1 + pkg/vere/io/ames.c | 6 +- pkg/vere/io/xmas.c | 782 ++++++++++++++++++++++++++++++++++++++++++ pkg/vere/vere.h | 4 + pkg/vere/xmas_tests.c | 50 +++ 6 files changed, 842 insertions(+), 2 deletions(-) create mode 100644 pkg/vere/io/xmas.c create mode 100644 pkg/vere/xmas_tests.c diff --git a/pkg/c3/motes.h b/pkg/c3/motes.h index 0000d5c593..f76e44dc86 100644 --- a/pkg/c3/motes.h +++ b/pkg/c3/motes.h @@ -1317,6 +1317,7 @@ # define c3__wyp c3_s3('w','y','p') # define c3__wyrd c3_s4('w','y','r','d') # define c3__xray c3_s4('x','r','a','y') +# define c3__xmas c3_s4('x','m','a','s') # define c3__yell c3_s4('y','e','l','l') # define c3__yelp c3_s4('y','e','l','p') # define c3__z c3_s1('z') diff --git a/pkg/vere/auto.c b/pkg/vere/auto.c index 2435458ad4..1aaee81d27 100644 --- a/pkg/vere/auto.c +++ b/pkg/vere/auto.c @@ -439,6 +439,7 @@ u3_auto_init(u3_pier* pir_u) car_u = _auto_link(u3_term_io_init(pir_u), pir_u, car_u); car_u = _auto_link(u3_fore_io_init(pir_u), pir_u, car_u); car_u = _auto_link(u3_lick_io_init(pir_u), pir_u, car_u); + car_u = _auto_link(u3_xmas_io_init(pir_u), pir_u, car_u); return car_u; } diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index 6df308239b..61e9723155 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -2090,10 +2090,12 @@ _ames_io_start(u3_ames* sam_u) htonl(INADDR_ANY) : htonl(INADDR_LOOPBACK); add_u.sin_port = htons(por_s); + u3l_log("ames: skippign port: %u", por_s); - if ( (ret_i = uv_udp_bind(&sam_u->wax_u, + /*if ( (ret_i = uv_udp_bind(&sam_u->wax_u, (const struct sockaddr*)&add_u, 0)) != 0 ) { + u3l_log("ames: port: %u", por_s); u3l_log("ames: bind: %s", uv_strerror(ret_i)); if ( (c3__czar == rac) && @@ -2105,7 +2107,7 @@ _ames_io_start(u3_ames* sam_u) // XX revise // u3_pier_bail(u3_king_stub()); - } + }*/ uv_udp_getsockname(&sam_u->wax_u, (struct sockaddr *)&add_u, &add_i); u3_assert(add_u.sin_port); diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c new file mode 100644 index 0000000000..845cfb602c --- /dev/null +++ b/pkg/vere/io/xmas.c @@ -0,0 +1,782 @@ +/// @file + +#include "vere.h" + +#include "noun.h" +#include "ur.h" + +#define XMAS_DEBUG c3y +#define XMAS_VER 1 +#define FINE_PAGE 4096 // packets per page +#define FINE_FRAG 1024 // bytes per fragment packet +#define FINE_PATH_MAX 384 // longest allowed scry path +#define HEAD_SIZE 4 // header size in bytes + +/* _u3_xmas: next generation networking + */ +typedef struct _u3_xmas { + u3_auto car_u; + u3_pier* pir_u; + union { + uv_udp_t wax_u; + uv_handle_t had_u; + }; + c3_l sev_l; + u3p(u3h_root) lax_p; + c3_w imp_w[256]; + time_t imp_t[256]; // imperial IP timestamps + c3_o imp_o[256]; // imperial print status + u3p(u3h_root) req_p; +} u3_xmas; + +typedef enum _u3_xmas_ptag { + PACT_PEEK = 1, + PACT_POKE = 2, + PACT_PAGE = 3 +} u3_xmas_ptag; + +typedef enum _u3_xmas_rout_tag { + ROUT_GALAXY = 0, + ROUT_OTHER = 1 +} u3_xmas_rout_tag; + +typedef struct _u3_xmas_rout { + u3_xmas_rout_tag typ_y; // type tag + union { + struct { + c3_c* dns_c; + c3_y her_y; + } imp_u; + u3_lane lan_u; + }; +} u3_xmas_rout; + +typedef struct _u3_xmas_head { + c3_y pro_y; // protocol version + u3_xmas_ptag typ_y; // packet type + c3_y ran_y; // publisher rank + c3_w mug_w; // mug checksum + c3_y hop_y; // hopcount +} u3_xmas_head; + + +typedef struct _u3_xmas_req_pact { + uv_udp_send_t snd_u; + struct _u3_xmas* sam_u; + u3_xmas_rout rut_u; + c3_d pub_d[2]; + c3_c* pat_c; +} u3_pact_req; + +static void +_log_head(u3_xmas_head* hed_u) +{ + u3l_log("-- HEADER --"); + u3l_log("protocol: %u", hed_u->pro_y); + u3l_log("packet type: %u", hed_u->typ_y); + u3l_log("mug: 0x%05x", (hed_u->mug_w &0xfffff)); + u3l_log("hopcount: %u", hed_u->hop_y); + u3l_log(""); +} + +static void +_log_buf(c3_y* buf_y, c3_w len_w) +{ + c3_w siz_w = 2*len_w + 1; + c3_c* res_c = c3_calloc(siz_w); + c3_w cur_w = 0; + c3_c tmp_c[3]; + for(c3_w idx_w = 0; idx_w < len_w; idx_w++ ) { + snprintf(res_c + (2*idx_w), siz_w - (2*idx_w), "%02x", buf_y[idx_w]); + } + u3l_log("buffer: %s", res_c); + free(res_c); +} + +static void +_log_pact(u3_pact_req* pac_u) +{ + u3l_log("-- REQUEST PACKET --"); + if( ROUT_GALAXY == pac_u->rut_u.typ_y ) { + u3l_log("-- GALAXY ROUTE --"); + u3_noun her = u3dc("scot", c3__p, u3i_word(pac_u->rut_u.imp_u.her_y)); + c3_c* her_c = u3r_string(her); + u3l_log("ship lane: %s", her_c); + free(her_c); + u3z(her); + } + else { + u3l_log("-- OTHER ROUTE --"); + u3_noun pip = u3dc("scot", c3__if, u3i_word(pac_u->rut_u.lan_u.pip_w)); + c3_c* pip_c = u3r_string(pip); + u3l_log("IP: (%s) port (%u)", pip_c, pac_u->rut_u.lan_u.por_s); + free(pip_c); + u3z(pip); + } + u3_noun pub = u3dc("scot", c3__p, u3i_chubs(2, pac_u->pub_d)); + c3_c* pub_c = u3r_string(pub); + u3l_log("ship: %s", pub_c); + free(pub_c); + u3z(pub); + u3l_log("path: %s", pac_u->pat_c); +} + + +// cut and pasted from ames.c +// +static void +_ames_etch_short(c3_y buf_y[2], c3_s sot_s) +{ + buf_y[0] = sot_s & 0xff; + buf_y[1] = (sot_s >> 8) & 0xff; +} + +static void +_ames_etch_word(c3_y buf_y[4], c3_w wod_w) +{ + buf_y[0] = wod_w & 0xff; + buf_y[1] = (wod_w >> 8) & 0xff; + buf_y[2] = (wod_w >> 16) & 0xff; + buf_y[3] = (wod_w >> 24) & 0xff; +} + +/* _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_alloc(): libuv buffer allocator. +*/ +static void +_ames_alloc(uv_handle_t* had_u, + size_t len_i, + uv_buf_t* buf + ) +{ + // we allocate 2K, which gives us plenty of space + // for a single ames packet (max size 1060 bytes) + // + void* ptr_v = c3_malloc(2048); + *buf = uv_buf_init(ptr_v, 2048); +} + +static inline c3_w +_ames_sift_word(c3_y buf_y[4]) +{ + return (buf_y[3] << 24 | buf_y[2] << 16 | buf_y[1] << 8 | buf_y[0]); +} + +/* u3_xmas_decode_lane(): deserialize noun to lane; 0.0.0.0:0 if invalid +*/ +static u3_lane +u3_xmas_decode_lane(u3_atom lan) { + u3_lane lan_u; + c3_d lan_d; + + if ( c3n == u3r_safe_chub(lan, &lan_d) || (lan_d >> 48) != 0 ) { + return (u3_lane){0, 0}; + } + + u3z(lan); + + lan_u.pip_w = (c3_w)lan_d; + lan_u.por_s = (c3_s)(lan_d >> 32); + // convert incoming localhost to outgoing localhost + // + lan_u.pip_w = ( 0 == lan_u.pip_w ) ? 0x7f000001 : lan_u.pip_w; + + return lan_u; +} + +/* _ames_chub_bytes(): c3_y[8] to c3_d +** XX factor out, deduplicate with other conversions +*/ +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] +*/ +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); +} + + +// END plagariasm zone +// +// +// +// +// +// +// +static c3_o +_xmas_rout_pact(u3_pact_req* pac_u, u3_noun lan) { + u3_noun tag, val; + u3x_cell(lan, &tag, &val); + + + u3_assert( (c3y == tag) || (c3n == tag) ); + c3_o suc_o = c3y; + if ( c3y == tag ) { + u3_assert( c3y == u3a_is_cat(val) ); + u3_assert( val < 256 ); + pac_u->rut_u.typ_y = ROUT_GALAXY; + pac_u->rut_u.imp_u.her_y = val; + } else { + u3_lane lan_u = u3_xmas_decode_lane(u3k(val)); + // if in local-only mode, don't send remote packets + // + if ( (c3n == u3_Host.ops_u.net) && (0x7f000001 != lan_u.pip_w) ) { + suc_o = c3n; + } + // if the lane is uninterpretable, silently drop the packet + // + else if ( 0 == lan_u.por_s ) { + if ( u3C.wag_w & u3o_verbose ) { + u3l_log("ames: inscrutable lane"); + } + suc_o = c3n; + } else { + pac_u->rut_u.typ_y = ROUT_OTHER; + pac_u->rut_u.lan_u = lan_u; + } + } + // todo refcount + // + return suc_o; +} +static c3_y +_ship_meta(u3_noun her) +{ + c3_w met_w = u3r_met(3, her); + switch ( met_w ) { + case 0: + case 1: + case 2: { + return 0; + } + case 3: + case 4: { + return 1; + } + case 5: + case 6: + case 7: + case 8: { + return 2; + } + default: { + return 3; + } + } +} + + +static void _xmas_pact_req_free(u3_pact_req* req_u) { + c3_free(req_u->pat_c); + c3_free(req_u); +} + +static u3_xmas_head +_xmas_head_from_pact_req(u3_pact_req* pac_u, u3_noun her) +{ + u3_xmas_head hed_u; + hed_u.pro_y = XMAS_VER; + hed_u.typ_y = PACT_PEEK; + hed_u.ran_y = _ship_meta(her); + hed_u.hop_y = 0; + hed_u.mug_w = 0; + + return hed_u; +} + +static void +_xmas_sift_head(c3_y buf_y[4], u3_xmas_head* hed_u) +{ + c3_w hed_w = _ames_sift_word(buf_y); + + hed_u->pro_y = (hed_w >> 0) & 0x7; + hed_u->typ_y = (hed_w >> 3) & 0x3; + hed_u->mug_w = (hed_w >> 5) & 0x3FFF; + hed_u->hop_y = (hed_w >> 25) & 0x1F; + + if(c3o((hed_u->typ_y == PACT_PEEK), (hed_u->typ_y == PACT_POKE))) { + hed_u->ran_y = (hed_w >> 30) & 0x3; + } +} + +static void +_xmas_etch_head(u3_xmas_head* hed_u, c3_y buf_y[4]) +{ + if( c3y == XMAS_DEBUG ) { + if( hed_u->pro_y > 7 ) { + u3l_log("xmas: bad protocol version"); + return; + } + if( hed_u->typ_y > 2 ) { + u3l_log("xmas: bad packet type"); + return; + } + } + c3_o req_o = c3o((hed_u->typ_y == PACT_PEEK), (hed_u->typ_y == PACT_POKE)); + c3_y siz_y = req_o ? 5 : 7; + c3_w hed_w = (hed_u->pro_y & 0x7 ) + ^ ((hed_u->typ_y & 0x3 ) << 3) + ^ ((hed_u->mug_w & 0x3FFFF ) << 5) + ^ ((hed_u->hop_y & 0x1F ) << 25); + // XX: we don't expand hopcount if no request. Correct? + + if ( c3y == req_o ) { + hed_w = hed_w ^ ((hed_u->ran_y & 0x3) << 30); + } + u3l_log("head as int: %u", hed_w); + + _ames_etch_word(buf_y, hed_w); +} + +static c3_w +_xmas_etch_pact_req(c3_y** buf_y, u3_pact_req* pac_u, u3_noun her) +{ + u3_xmas_head hed_u = _xmas_head_from_pact_req(pac_u, her); + _log_head(&hed_u); + c3_w str_w = c3_min(328, strlen(pac_u->pat_c)); + if( XMAS_DEBUG ) { + //u3_assert( _(( str_w =< 328)) ); + } + c3_y pub_y = 2 << hed_u.ran_y; + + c3_w len_w = 4 // header; + + pub_y + + str_w + + 1; // tag byte + + *buf_y = c3_calloc(len_w); + c3_y* cur_y = *buf_y; + _xmas_etch_head(&hed_u, *buf_y); + _log_buf(*buf_y, 4); + cur_y += 4; + u3r_bytes(0, pub_y, cur_y, her); + cur_y += pub_y; + cur_y[0] = 0; // tag byte is zeroed + cur_y++; + memcpy(cur_y, pac_u->pat_c, c3_min(328, str_w)); + cur_y[327] = 0; // XX: correct? ensure null terminator + return len_w; +} + +static void +_xmas_send_cb(uv_udp_send_t* req_u, c3_i sas_i) +{ + u3_pact_req* pac_u = (u3_pact_req*)req_u; + u3_xmas* sam_u = pac_u->sam_u; + + if ( sas_i ) { + u3l_log("xmas: send fail_async: %s", uv_strerror(sas_i)); + //sam_u->fig_u.net_o = c3n; + } + else { + //sam_u->fig_u.net_o = c3y; + } + + _xmas_pact_req_free(pac_u); +} + +static void _xmas_send(u3_pact_req* pac_u) +{ + u3_xmas* sam_u = pac_u->sam_u; + + struct sockaddr_in add_u; + + memset(&add_u, 0, sizeof(add_u)); + add_u.sin_family = AF_INET; + c3_s por_s; + c3_w pip_w; + if( pac_u->rut_u.typ_y == ROUT_GALAXY ) { + por_s = _ames_czar_port(pac_u->rut_u.imp_u.her_y); + pip_w = 0x7f000001; + } + else { + por_s = pac_u->rut_u.lan_u.por_s; + pip_w = pac_u->rut_u.lan_u.pip_w; + } + + add_u.sin_addr.s_addr = htonl(pip_w); + add_u.sin_port = htons(por_s); + + //u3l_log("_ames_send %s %u", _str_typ(pac_u->typ_y), + // pac_u->rut_u.lan_u.por_s); + + { + c3_y* buf_y; + c3_w siz_w = _xmas_etch_pact_req(&buf_y, pac_u, u3i_chubs(2, pac_u->pub_d)); + _log_buf(buf_y, 4); + + + uv_buf_t buf_u = uv_buf_init((c3_c*)buf_y, siz_w); + + c3_i sas_i = uv_udp_send(&pac_u->snd_u, + &sam_u->wax_u, + &buf_u, 1, + (const struct sockaddr*)&add_u, + _xmas_send_cb); + + if ( sas_i ) { + /*if ( c3y == sam_u->fig_u.net_o ) { + u3l_log("ames: send fail_sync: %s", uv_strerror(sas_i)); + //sam_u->fig_u.net_o = c3n; + }*/ + + _xmas_pact_req_free(pac_u); + } + } +} + + +static void +_xmas_ef_peek(u3_xmas* sam_u, u3_noun her, u3_noun las, u3_noun pat) +{ + if ( c3n == sam_u->car_u.liv_o ) { + u3l_log("xmas: not yet live, dropping outbound\r"); + u3z(her); u3z(las); u3z(pat); + return; + } + + u3_pact_req* pac_u = c3_calloc(sizeof(*pac_u)); + pac_u->sam_u = sam_u; + u3_noun pas = u3do("spat", pat); + pac_u->pat_c = u3r_string(pas); + u3l_log("pat_c: %s", pac_u->pat_c); + + if( 384 < strlen(pac_u->pat_c) ) { + u3l_log("xmas: path in peek too long"); + _xmas_pact_req_free(pac_u); + u3z(her); u3z(las); u3z(pat); + return; + } + u3r_chubs(0, 2, pac_u->pub_d, her); + + u3_noun lan, t = las; + c3_o suc_o = c3n; + + while ( u3_nul != t ) { + u3x_cell(t, &lan, &t); + + if( c3n == _xmas_rout_pact(pac_u, lan) ) { + u3l_log("xmas: failed to set route"); + //_xmas_pact_req_free(pac_u); + } else { + _log_pact(pac_u); + _xmas_send(pac_u); + suc_o = c3y; + } + } + + if( c3n == suc_o ) { + _xmas_pact_req_free(pac_u); + } + + u3z(lan); +} + + +static c3_o _xmas_kick(u3_xmas* sam_u, u3_noun tag, u3_noun dat) +{ + c3_o ret_o; + + switch ( tag ) { + default: { + ret_o = c3n; + } break; + + // + case c3__peek: { + u3l_log("kick peek"); + u3_noun lan, her, pat; + if ( c3n == u3r_trel(dat, &her, &lan, &pat) ) { + ret_o = c3n; + } else { + _xmas_ef_peek(sam_u, u3k(her), u3k(lan), u3k(pat)); + ret_o = c3y; + } + } break; + } + + u3z(tag); u3z(dat); + return ret_o; +} + +static c3_o _xmas_io_kick(u3_auto* car_u, u3_noun wir, u3_noun cad) +{ + u3_xmas* sam_u = (u3_xmas*)car_u; + + u3_noun tag, dat, i_wir; + c3_o ret_o; + + if( c3n == u3r_cell(cad, &tag, &dat) ) + { + ret_o = c3n; + } + + ret_o = _xmas_kick(sam_u, u3k(tag), u3k(dat)); + + u3z(wir); u3z(cad); + return ret_o; +} + + + + + + + + + +static u3_noun +_xmas_io_info(u3_auto* car_u) +{ + + return u3_nul; +} + +static void +_xmas_io_slog(u3_auto* car_u) { + u3l_log("xmas is online"); +} + +static void +_xmas_exit_cb(uv_handle_t* had_u) +{ + u3_xmas* sam_u = had_u->data; + u3h_free(sam_u->lax_p); + u3h_free(sam_u->req_p); + c3_free(sam_u); +} + +static void +_xmas_io_exit(u3_auto* car_u) +{ + u3_xmas* sam_u = (u3_xmas*)car_u; + uv_close(&sam_u->had_u, _xmas_exit_cb); +} + +static void +_xmas_sift_pact(u3_pact_req* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, c3_w len_w) +{ + c3_y* cur_y = buf_y; + c3_y len_y = 2 << hed_u->ran_y; + _ames_ship_to_chubs(pac_u->pub_d, len_y, buf_y); + cur_y += len_y; + cur_y += 1; // tag byte + pac_u->pat_c = c3_calloc(328); + memcpy(pac_u->pat_c, cur_y, 328); + + _log_pact(pac_u); +} + +static void +_xmas_hear(u3_xmas* sam_u, + u3_lane* lan_u, + c3_w len_w, + c3_y* hun_y) +{ + u3l_log("xmas: heard packet"); + _log_buf(hun_y, 4); + u3_pact_req* pac_u; + c3_w pre_w; + c3_y* cur_y = hun_y; + if ( HEAD_SIZE > len_w ) { + u3l_log("xmas: failed to read header"); + c3_free(hun_y); + return; + } + + pac_u = c3_calloc(sizeof(*pac_u)); + pac_u->sam_u = sam_u; + u3_xmas_head hed_u; + _xmas_sift_head(cur_y, &hed_u); + _log_head(&hed_u); + _xmas_sift_pact(pac_u, &hed_u, hun_y + 4, (len_w - (cur_y - hun_y))); +} + +static void +_xmas_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("xmas: recv: fail: %s", 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("xmas: recv: fail: message truncated"); + } + c3_free(buf_u->base); + } + else { + u3_xmas* 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() + // + _xmas_hear(sam_u, &lan_u, (c3_w)nrd_i, (c3_y*)buf_u->base); + } +} + + + +static void +_xmas_io_talk(u3_auto* car_u) +{ + u3_xmas* sam_u = (u3_xmas*)car_u; + //_xmas_io_start(sam_u); + u3_noun who = u3i_chubs(2, sam_u->pir_u->who_d); + u3_noun rac = u3do("clan:title", u3k(who)); + c3_s por_s = sam_u->pir_u->por_s; + c3_i ret_i; + if ( c3__czar == rac ) { + c3_y num_y = (c3_y)sam_u->pir_u->who_d[0]; + c3_s zar_s = _ames_czar_port(num_y); + + if ( 0 == por_s ) { + por_s = zar_s; + } + else if ( por_s != zar_s ) { + u3l_log("ames: czar: overriding port %d with -p %d", zar_s, por_s); + u3l_log("ames: czar: WARNING: %d required for discoverability", zar_s); + } + } + + + // Bind and stuff. + { + struct sockaddr_in add_u; + c3_i add_i = sizeof(add_u); + + memset(&add_u, 0, sizeof(add_u)); + add_u.sin_family = AF_INET; + add_u.sin_addr.s_addr = _(u3_Host.ops_u.net) ? + htonl(INADDR_ANY) : + htonl(INADDR_LOOPBACK); + add_u.sin_port = htons(por_s); + + if ( (ret_i = uv_udp_bind(&sam_u->wax_u, + (const struct sockaddr*)&add_u, 0)) != 0 ) + { + u3l_log("xmas: bind: %s", uv_strerror(ret_i)); + + /*if ( (c3__czar == rac) && + (UV_EADDRINUSE == ret_i) ) + { + u3l_log(" ...perhaps you've got two copies of vere running?"); + }*/ + + // XX revise + // + u3_pier_bail(u3_king_stub()); + } + + uv_udp_getsockname(&sam_u->wax_u, (struct sockaddr *)&add_u, &add_i); + u3_assert(add_u.sin_port); + + sam_u->pir_u->por_s = ntohs(add_u.sin_port); + } + if ( c3y == u3_Host.ops_u.net ) { + u3l_log("xmas: live on %d", sam_u->pir_u->por_s); + } + else { + u3l_log("xmas: live on %d (localhost only)", sam_u->pir_u->por_s); + } + + uv_udp_recv_start(&sam_u->wax_u, _ames_alloc, _xmas_recv_cb); + + sam_u->car_u.liv_o = c3y; + u3z(rac); u3z(who); +} + +/* _xmas_io_init(): initialize ames I/O. +*/ +u3_auto* +u3_xmas_io_init(u3_pier* pir_u) +{ + u3_xmas* sam_u = c3_calloc(sizeof(*sam_u)); + sam_u->pir_u = pir_u; + + sam_u->lax_p = u3h_new_cache(100000); + sam_u->req_p = u3h_new_cache(100000); + + + u3_assert( !uv_udp_init(u3L, &sam_u->wax_u) ); + sam_u->wax_u.data = sam_u; + + //sam_u->sil_u = u3s_cue_xeno_init(); + //sam_u->tes_u = ur_cue_test_init(); + + // Disable networking for fake ships + // + if ( c3y == sam_u->pir_u->fak_o ) { + u3_Host.ops_u.net = c3n; + } + + u3_auto* car_u = &sam_u->car_u; + car_u->nam_m = c3__xmas; + car_u->liv_o = c3y; + car_u->io.talk_f = _xmas_io_talk; + car_u->io.info_f = _xmas_io_info; + car_u->io.slog_f = _xmas_io_slog; + car_u->io.kick_f = _xmas_io_kick; + car_u->io.exit_f = _xmas_io_exit; + + + /*{ + u3_noun now; + struct timeval tim_u; + gettimeofday(&tim_u, 0); + + now = u3_time_in_tv(&tim_u); + //sam_u->sev_l = u3r_mug(now); + u3z(now); + }*/ + + return car_u; +} + + + + diff --git a/pkg/vere/vere.h b/pkg/vere/vere.h index 9527246e19..b1cd43fd74 100644 --- a/pkg/vere/vere.h +++ b/pkg/vere/vere.h @@ -1215,6 +1215,10 @@ u3_noun u3_ames_encode_lane(u3_lane); + /** xmas + **/ + u3_auto* + u3_xmas_io_init(u3_pier* pir_u); /** Autosave. **/ /* u3_save_ef_chld(): report SIGCHLD. diff --git a/pkg/vere/xmas_tests.c b/pkg/vere/xmas_tests.c new file mode 100644 index 0000000000..373788740f --- /dev/null +++ b/pkg/vere/xmas_tests.c @@ -0,0 +1,50 @@ +/// @file + +#include "noun.h" +#include "vere.h" + +/* _setup(): prepare for tests. +*/ +static void +_setup(void) +{ + u3m_init(1 << 22); + u3m_pave(c3y); +} + +/* _test_ames(): spot check ames helpers +*/ +static void +_test_ames(void) +{ + u3_lane lan_u; + lan_u.pip_w = 0x7f000001; + lan_u.por_s = 12345; + + u3_noun lan = u3_ames_encode_lane(lan_u); + u3_lane nal_u = u3_ames_decode_lane(u3k(lan)); + u3_lane nal_u2 = u3_ames_decode_lane(lan); + + if ( !(lan_u.pip_w == nal_u.pip_w && lan_u.por_s == nal_u.por_s) ) { + fprintf(stderr, "ames: lane fail (a)\r\n"); + fprintf(stderr, "pip: %d, por: %d\r\n", nal_u.pip_w, nal_u.por_s); + exit(1); + } +} + +/* main(): run all test cases. +*/ +int +main(int argc, char* argv[]) +{ + _setup(); + + _test_ames(); + + // GC + // + u3m_grab(u3_none); + + fprintf(stderr, "ames okeedokee\n"); + return 0; +} From 3c4d19353c4af9794502fa76e619b69bfac96d0a Mon Sep 17 00:00:00 2001 From: Liam Fitzgerald Date: Tue, 14 Nov 2023 12:58:33 +0000 Subject: [PATCH 002/430] wip --- pkg/c3/motes.h | 2 + pkg/vere/io/ames.c | 7 +- pkg/vere/io/xmas.c | 597 +++++++++++++++++++++++++++++++++++++-------- 3 files changed, 507 insertions(+), 99 deletions(-) diff --git a/pkg/c3/motes.h b/pkg/c3/motes.h index f76e44dc86..7767439029 100644 --- a/pkg/c3/motes.h +++ b/pkg/c3/motes.h @@ -850,6 +850,7 @@ # define c3__ovum c3_s4('o','v','u','m') # define c3__p c3_s1('p') # define c3__pack c3_s4('p','a','c','k') +# define c3__page c3_s4('p','a','g','e') # define c3__pair c3_s4('p','a','i','r') # define c3__palm c3_s4('p','a','l','m') # define c3__palq c3_s4('p','a','l','q') @@ -1318,6 +1319,7 @@ # define c3__wyrd c3_s4('w','y','r','d') # define c3__xray c3_s4('x','r','a','y') # define c3__xmas c3_s4('x','m','a','s') +# define c3__xx c3_s2('x','x') # define c3__yell c3_s4('y','e','l','l') # define c3__yelp c3_s4('y','e','l','p') # define c3__z c3_s1('z') diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index 61e9723155..eadd403d64 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -856,11 +856,11 @@ _ames_send(u3_pact* pac_u) { uv_buf_t buf_u = uv_buf_init((c3_c*)pac_u->hun_y, pac_u->len_w); - c3_i sas_i = uv_udp_send(&pac_u->snd_u, + c3_i sas_i = 0; /*uv_udp_send(&pac_u->snd_u, &sam_u->wax_u, &buf_u, 1, (const struct sockaddr*)&add_u, - _ames_send_cb); + _ames_send_cb); */ if ( sas_i ) { if ( c3y == sam_u->fig_u.net_o ) { @@ -2129,8 +2129,7 @@ _ames_io_start(u3_ames* sam_u) u3z(who); } -/* _ames_ef_turf(): initialize ames I/O on domain(s). -*/ +/* _ames_ef_turf(): initialize ames I/O on domain(s). */ static void _ames_ef_turf(u3_ames* sam_u, u3_noun tuf) { diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c index 845cfb602c..6e318f40be 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/xmas.c @@ -11,6 +11,7 @@ #define FINE_FRAG 1024 // bytes per fragment packet #define FINE_PATH_MAX 384 // longest allowed scry path #define HEAD_SIZE 4 // header size in bytes +#define PACT_SIZE 1472 /* _u3_xmas: next generation networking */ @@ -22,7 +23,10 @@ typedef struct _u3_xmas { uv_handle_t had_u; }; c3_l sev_l; + ur_cue_test_t* tes_u; // cue-test handle + u3_cue_xeno* sil_u; // cue handle u3p(u3h_root) lax_p; + u3p(u3h_root) pen_p; c3_w imp_w[256]; time_t imp_t[256]; // imperial IP timestamps c3_o imp_o[256]; // imperial print status @@ -60,13 +64,40 @@ typedef struct _u3_xmas_head { } u3_xmas_head; -typedef struct _u3_xmas_req_pact { +typedef struct _u3_xmas_peek_pact { + c3_d pub_d[2]; + c3_s pat_s; + c3_c* pat_c; +} u3_xmas_peek_pact; + +typedef struct _u3_xmas_page_pact { + c3_y hop_y[6]; + c3_s pat_s; + c3_c* pat_c; + c3_w tot_w; + c3_y aut_y[96]; // heap allocate?? + c3_y* dat_y; +} u3_xmas_page_pact; + +typedef struct _u3_xmas_poke_pact { + u3_xmas_peek_pact* pek_u; + c3_w tot_w; + c3_y aut_y[96]; // heap allocate?? + c3_y* dat_y; +} u3_xmas_poke_pact; + +typedef struct _u3_xmas_pact { + u3_xmas_ptag typ_y; uv_udp_send_t snd_u; struct _u3_xmas* sam_u; u3_xmas_rout rut_u; - c3_d pub_d[2]; - c3_c* pat_c; -} u3_pact_req; + union { + u3_xmas_poke_pact pok_u; + u3_xmas_page_pact pag_u; + u3_xmas_peek_pact pek_u; + }; +} u3_xmas_pact; + static void _log_head(u3_xmas_head* hed_u) @@ -94,10 +125,18 @@ _log_buf(c3_y* buf_y, c3_w len_w) } static void -_log_pact(u3_pact_req* pac_u) +_log_peek_pact(u3_xmas_peek_pact* pac_u) +{ + u3l_log("publisher: %s", u3r_string(u3dc("scot", c3__p, u3i_chubs(2, pac_u->pub_d)))); + u3l_log("path len: %u", pac_u->pat_s); + u3l_log("path: %s", pac_u->pat_c); +} + +static void +_log_pact(u3_xmas_peek_pact* pac_u) { u3l_log("-- REQUEST PACKET --"); - if( ROUT_GALAXY == pac_u->rut_u.typ_y ) { + /*if( ROUT_GALAXY == pac_u->rut_u.typ_y ) { u3l_log("-- GALAXY ROUTE --"); u3_noun her = u3dc("scot", c3__p, u3i_word(pac_u->rut_u.imp_u.her_y)); c3_c* her_c = u3r_string(her); @@ -112,12 +151,12 @@ _log_pact(u3_pact_req* pac_u) u3l_log("IP: (%s) port (%u)", pip_c, pac_u->rut_u.lan_u.por_s); free(pip_c); u3z(pip); - } + }*/ u3_noun pub = u3dc("scot", c3__p, u3i_chubs(2, pac_u->pub_d)); c3_c* pub_c = u3r_string(pub); u3l_log("ship: %s", pub_c); free(pub_c); - u3z(pub); + //u3z(pub); u3l_log("path: %s", pac_u->pat_c); } @@ -165,8 +204,14 @@ _ames_alloc(uv_handle_t* had_u, // we allocate 2K, which gives us plenty of space // for a single ames packet (max size 1060 bytes) // - void* ptr_v = c3_malloc(2048); - *buf = uv_buf_init(ptr_v, 2048); + void* ptr_v = c3_malloc(4096); + *buf = uv_buf_init(ptr_v, 4096); +} + +static inline c3_s +_ames_sift_short(c3_y buf_y[2]) +{ + return (buf_y[1] << 8 | buf_y[0]); } static inline c3_w @@ -186,7 +231,7 @@ u3_xmas_decode_lane(u3_atom lan) { return (u3_lane){0, 0}; } - u3z(lan); + //u3z(lan); lan_u.pip_w = (c3_w)lan_d; lan_u.por_s = (c3_s)(lan_d >> 32); @@ -225,6 +270,51 @@ _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 factor out, deduplicate with other conversions +*/ +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; +} + +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)); +} + + +/* u3_xmas_lane_to_chub(): serialize lane to double-word +*/ +static c3_d +u3_xmas_lane_to_chub(u3_lane lan) { + return ((c3_d)lan.por_s << 32) ^ (c3_d)lan.pip_w; +} + +/* u3_xmas_encode_lane(): serialize lane to noun +*/ +static u3_atom +u3_xmas_encode_lane(u3_lane lan) { + // [%| p=@] + // [%& p=@pC] + return u3i_chub(u3_xmas_lane_to_chub(lan)); +} + + // END plagariasm zone // @@ -234,12 +324,17 @@ _ames_ship_to_chubs(c3_d sip_d[2], c3_y len_y, c3_y* buf_y) // // // +static u3_noun _xmas_get_now() { + struct timeval tim_u; + gettimeofday(&tim_u, 0); + return u3_time_in_tv(&tim_u); +} + static c3_o -_xmas_rout_pact(u3_pact_req* pac_u, u3_noun lan) { +_xmas_rout_pact(u3_xmas_pact* pac_u, u3_noun lan) { u3_noun tag, val; u3x_cell(lan, &tag, &val); - u3_assert( (c3y == tag) || (c3n == tag) ); c3_o suc_o = c3y; if ( c3y == tag ) { @@ -270,6 +365,8 @@ _xmas_rout_pact(u3_pact_req* pac_u, u3_noun lan) { // return suc_o; } + + static c3_y _ship_meta(u3_noun her) { @@ -297,17 +394,16 @@ _ship_meta(u3_noun her) } -static void _xmas_pact_req_free(u3_pact_req* req_u) { - c3_free(req_u->pat_c); - c3_free(req_u); +static void _xmas_pact_free(u3_xmas_pact* pac_u) { + // TODO: i'm lazy } static u3_xmas_head -_xmas_head_from_pact_req(u3_pact_req* pac_u, u3_noun her) +_xmas_head_from_pact(u3_xmas_pact* pac_u, u3_noun her) { u3_xmas_head hed_u; hed_u.pro_y = XMAS_VER; - hed_u.typ_y = PACT_PEEK; + hed_u.typ_y = pac_u->typ_y; hed_u.ran_y = _ship_meta(her); hed_u.hop_y = 0; hed_u.mug_w = 0; @@ -338,10 +434,6 @@ _xmas_etch_head(u3_xmas_head* hed_u, c3_y buf_y[4]) u3l_log("xmas: bad protocol version"); return; } - if( hed_u->typ_y > 2 ) { - u3l_log("xmas: bad packet type"); - return; - } } c3_o req_o = c3o((hed_u->typ_y == PACT_PEEK), (hed_u->typ_y == PACT_POKE)); c3_y siz_y = req_o ? 5 : 7; @@ -360,39 +452,113 @@ _xmas_etch_head(u3_xmas_head* hed_u, c3_y buf_y[4]) } static c3_w -_xmas_etch_pact_req(c3_y** buf_y, u3_pact_req* pac_u, u3_noun her) -{ - u3_xmas_head hed_u = _xmas_head_from_pact_req(pac_u, her); - _log_head(&hed_u); - c3_w str_w = c3_min(328, strlen(pac_u->pat_c)); - if( XMAS_DEBUG ) { - //u3_assert( _(( str_w =< 328)) ); - } - c3_y pub_y = 2 << hed_u.ran_y; - - c3_w len_w = 4 // header; - + pub_y - + str_w - + 1; // tag byte - - *buf_y = c3_calloc(len_w); - c3_y* cur_y = *buf_y; - _xmas_etch_head(&hed_u, *buf_y); - _log_buf(*buf_y, 4); - cur_y += 4; - u3r_bytes(0, pub_y, cur_y, her); - cur_y += pub_y; - cur_y[0] = 0; // tag byte is zeroed - cur_y++; - memcpy(cur_y, pac_u->pat_c, c3_min(328, str_w)); - cur_y[327] = 0; // XX: correct? ensure null terminator - return len_w; +_xmas_etch_page_pact(c3_y* buf_y, u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u) +{ + u3l_log("beggining etch"); + c3_w cur_w = 0; + + // hops + memcpy(buf_y, pac_u->hop_y, 6); + cur_w += 6; + + // path length + _ames_etch_short(buf_y + cur_w, pac_u->pat_s); + u3l_log("path length: %u", pac_u->pat_s); + cur_w += 2; + + // path + u3l_log("path : %s", pac_u->pat_c); + memcpy(buf_y + cur_w, pac_u->pat_c, pac_u->pat_s + 1); + cur_w += pac_u->pat_s + 1; + + // total + _ames_etch_word(buf_y + cur_w, pac_u->tot_w); + cur_w += 4; + u3l_log("total: %u", pac_u->tot_w); + + // auth + memcpy(buf_y + cur_w, &pac_u->aut_y, 96); + cur_w += 96; + + // day + memcpy(buf_y + cur_w, pac_u->dat_y, 1024); + cur_w += 1024; + + u3l_log("done: %u", cur_w); + return cur_w; +} + +static c3_w +_xmas_etch_peek_pact(c3_y* buf_y, u3_xmas_peek_pact* pac_u, u3_xmas_head* hed_u, c3_o tag_o) +{ + c3_w cur_w = 0; + c3_y pub_y = 2 << hed_u->ran_y; + _ames_ship_of_chubs(pac_u->pub_d, pub_y, buf_y); + cur_w += pub_y; + + if( tag_o == c3y ) { + buf_y[cur_w] = 0; // Tag byte + cur_w += 1; + } + + _ames_etch_short(buf_y + cur_w, pac_u->pat_s); + cur_w += 2; + + memcpy(buf_y + cur_w, pac_u->pat_c, pac_u->pat_s + 1); + return cur_w + 328; +} + +static c3_w +_xmas_etch_poke_pact(c3_y* buf_y, u3_xmas_poke_pact* pac_u, u3_xmas_head* hed_u) +{ + c3_w cur_w = 0; + cur_w += _xmas_etch_peek_pact(buf_y, pac_u->pek_u, hed_u, c3n); + + // total + _ames_etch_word(buf_y + cur_w, pac_u->tot_w); + cur_w += 4; + + // auth + memcpy(buf_y + cur_w, &pac_u->aut_y, 96); + cur_w += 96; + + // day + memcpy(buf_y + cur_w, pac_u->dat_y, PACT_SIZE - cur_w); + + return PACT_SIZE; +} + +static c3_w +_xmas_etch_pact(c3_y* buf_y, u3_xmas_pact* pac_u, u3_noun her) +{ + c3_w cur_w = 0; + u3_xmas_head hed_u = _xmas_head_from_pact(pac_u, her); + _xmas_etch_head(&hed_u, buf_y + cur_w); + cur_w += 4; + + + switch ( pac_u->typ_y ) { + case PACT_POKE: { + cur_w += _xmas_etch_poke_pact(buf_y + cur_w, &pac_u->pok_u, &hed_u); + } break; + case PACT_PEEK: { + cur_w += _xmas_etch_peek_pact(buf_y + cur_w, &pac_u->pek_u, &hed_u, c3y); + } break; + case PACT_PAGE: { + cur_w += _xmas_etch_page_pact(buf_y + cur_w, &pac_u->pag_u, &hed_u); + } break; + + default: { + u3l_log("bad pact type");//u3m_bail(c3__bail); + } + } + return cur_w; } static void _xmas_send_cb(uv_udp_send_t* req_u, c3_i sas_i) { - u3_pact_req* pac_u = (u3_pact_req*)req_u; + u3_xmas_pact* pac_u = (u3_xmas_pact*)req_u; u3_xmas* sam_u = pac_u->sam_u; if ( sas_i ) { @@ -403,10 +569,10 @@ _xmas_send_cb(uv_udp_send_t* req_u, c3_i sas_i) //sam_u->fig_u.net_o = c3y; } - _xmas_pact_req_free(pac_u); + _xmas_pact_free(pac_u); } -static void _xmas_send(u3_pact_req* pac_u) +static void _xmas_send(u3_xmas_pact* pac_u) { u3_xmas* sam_u = pac_u->sam_u; @@ -416,26 +582,32 @@ static void _xmas_send(u3_pact_req* pac_u) add_u.sin_family = AF_INET; c3_s por_s; c3_w pip_w; + u3l_log("typ_y: %u", pac_u->rut_u.typ_y); if( pac_u->rut_u.typ_y == ROUT_GALAXY ) { por_s = _ames_czar_port(pac_u->rut_u.imp_u.her_y); pip_w = 0x7f000001; } else { + pip_w = c3n == u3_Host.ops_u.net ? pac_u->rut_u.lan_u.pip_w : 0x7f000001; por_s = pac_u->rut_u.lan_u.por_s; - pip_w = pac_u->rut_u.lan_u.pip_w; } + u3l_log("%x", pip_w); + u3l_log("%u", por_s); + add_u.sin_addr.s_addr = htonl(pip_w); add_u.sin_port = htons(por_s); //u3l_log("_ames_send %s %u", _str_typ(pac_u->typ_y), // pac_u->rut_u.lan_u.por_s); - - { - c3_y* buf_y; - c3_w siz_w = _xmas_etch_pact_req(&buf_y, pac_u, u3i_chubs(2, pac_u->pub_d)); - _log_buf(buf_y, 4); - + c3_y* buf_y = c3_calloc(PACT_SIZE); + u3_noun who = 0; + if( pac_u->typ_y == PACT_PEEK ) { + who = u3i_chubs(2, pac_u->pek_u.pub_d); + } + + c3_w siz_w = _xmas_etch_pact(buf_y, pac_u, u3k(who)); + _log_buf(buf_y, siz_w); uv_buf_t buf_u = uv_buf_init((c3_c*)buf_y, siz_w); @@ -445,15 +617,15 @@ static void _xmas_send(u3_pact_req* pac_u) (const struct sockaddr*)&add_u, _xmas_send_cb); + u3l_log("udp sent"); if ( sas_i ) { + u3l_log("ames: send fail_sync: %s", uv_strerror(sas_i)); /*if ( c3y == sam_u->fig_u.net_o ) { - u3l_log("ames: send fail_sync: %s", uv_strerror(sas_i)); //sam_u->fig_u.net_o = c3n; }*/ - _xmas_pact_req_free(pac_u); + _xmas_pact_free(pac_u); } - } } @@ -462,23 +634,24 @@ _xmas_ef_peek(u3_xmas* sam_u, u3_noun her, u3_noun las, u3_noun pat) { if ( c3n == sam_u->car_u.liv_o ) { u3l_log("xmas: not yet live, dropping outbound\r"); - u3z(her); u3z(las); u3z(pat); + //u3z(her); u3z(las); u3z(pat); return; } - u3_pact_req* pac_u = c3_calloc(sizeof(*pac_u)); + u3_xmas_pact* pac_u = c3_calloc(sizeof(*pac_u)); + pac_u->typ_y = PACT_PEEK; pac_u->sam_u = sam_u; u3_noun pas = u3do("spat", pat); - pac_u->pat_c = u3r_string(pas); - u3l_log("pat_c: %s", pac_u->pat_c); + pac_u->pek_u.pat_s = u3r_short(0, u3dc("met", 3, u3k(pas))); + pac_u->pek_u.pat_c = u3r_string(u3k(pas)); - if( 384 < strlen(pac_u->pat_c) ) { + if( 384 < strlen(pac_u->pek_u.pat_c) ) { u3l_log("xmas: path in peek too long"); - _xmas_pact_req_free(pac_u); - u3z(her); u3z(las); u3z(pat); + _xmas_pact_free(pac_u); + //u3z(her); u3z(las); u3z(pat); return; } - u3r_chubs(0, 2, pac_u->pub_d, her); + u3r_chubs(0, 2, pac_u->pek_u.pub_d, her); u3_noun lan, t = las; c3_o suc_o = c3n; @@ -490,17 +663,17 @@ _xmas_ef_peek(u3_xmas* sam_u, u3_noun her, u3_noun las, u3_noun pat) u3l_log("xmas: failed to set route"); //_xmas_pact_req_free(pac_u); } else { - _log_pact(pac_u); + u3l_log("xmas_send"); _xmas_send(pac_u); suc_o = c3y; } } if( c3n == suc_o ) { - _xmas_pact_req_free(pac_u); + _xmas_pact_free(pac_u); } - u3z(lan); + //u3z(lan); } @@ -515,7 +688,6 @@ static c3_o _xmas_kick(u3_xmas* sam_u, u3_noun tag, u3_noun dat) // case c3__peek: { - u3l_log("kick peek"); u3_noun lan, her, pat; if ( c3n == u3r_trel(dat, &her, &lan, &pat) ) { ret_o = c3n; @@ -526,7 +698,7 @@ static c3_o _xmas_kick(u3_xmas* sam_u, u3_noun tag, u3_noun dat) } break; } - u3z(tag); u3z(dat); + //u3z(tag); u3z(dat); return ret_o; } @@ -544,7 +716,7 @@ static c3_o _xmas_io_kick(u3_auto* car_u, u3_noun wir, u3_noun cad) ret_o = _xmas_kick(sam_u, u3k(tag), u3k(dat)); - u3z(wir); u3z(cad); + //u3z(wir); u3z(cad); return ret_o; } @@ -572,6 +744,9 @@ static void _xmas_exit_cb(uv_handle_t* had_u) { u3_xmas* sam_u = had_u->data; + + u3s_cue_xeno_done(sam_u->sil_u); + ur_cue_test_done(sam_u->tes_u); u3h_free(sam_u->lax_p); u3h_free(sam_u->req_p); c3_free(sam_u); @@ -585,17 +760,244 @@ _xmas_io_exit(u3_auto* car_u) } static void -_xmas_sift_pact(u3_pact_req* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, c3_w len_w) +_xmas_sift_page_pact(u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, c3_w len_w) { - c3_y* cur_y = buf_y; - c3_y len_y = 2 << hed_u->ran_y; - _ames_ship_to_chubs(pac_u->pub_d, len_y, buf_y); - cur_y += len_y; - cur_y += 1; // tag byte - pac_u->pat_c = c3_calloc(328); - memcpy(pac_u->pat_c, cur_y, 328); + c3_w cur_w = 0; + + // next hop + memcpy(pac_u->hop_y, buf_y, 6); + cur_w += 6; + + // path length + pac_u->pat_s = _ames_sift_short(buf_y + cur_w); + cur_w += 2; + + // path contents + pac_u->pat_c = c3_calloc(pac_u->pat_s + 1); + memcpy(pac_u->pat_c, buf_y + cur_w, pac_u->pat_s); + pac_u->pat_c[pac_u->pat_s] = '\0'; + cur_w += pac_u->pat_s + 1; - _log_pact(pac_u); + // total fragments + pac_u->tot_w = _ames_sift_word(buf_y + cur_w); + cur_w += 4; + + // Authenticator + memcpy(pac_u->aut_y, buf_y + cur_w, 96); + cur_w += 96; + + memcpy(pac_u->dat_y, buf_y + cur_w, 1024); + cur_w += 1024; +} + +static c3_w +_xmas_sift_peek_pact(u3_xmas_peek_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, c3_w len_w, c3_o tag_o) +{ + c3_y pub_y = 2 << hed_u->ran_y; // Publisher size + c3_w cur_w = 0; + _ames_ship_to_chubs(pac_u->pub_d, pub_y, buf_y); + cur_w += pub_y; + + if ( c3y == tag_o ) { + cur_w += 1; // tag byte + } + + pac_u->pat_s = _ames_sift_short(buf_y + cur_w); // str len + cur_w += 2; + + // if ( pac_u->pek_u.pat_s < 325 ) { TODO: check validity + + pac_u->pat_c = c3_calloc(pac_u->pat_s + 1); + memcpy(pac_u->pat_c, buf_y + cur_w, pac_u->pat_s + 1); + pac_u->pat_c[pac_u->pat_s] = '\0'; + cur_w += 328; + return cur_w; +} + +static void +_xmas_sift_poke_pact(u3_xmas_poke_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, c3_w len_w) +{ + c3_w cur_w = 0; + // Peek portion + pac_u->pek_u = c3_calloc(sizeof(pac_u->pek_u)); + cur_w += _xmas_sift_peek_pact(pac_u->pek_u, hed_u, buf_y, len_w, c3n); + + // Total fragments + pac_u->tot_w = _ames_sift_word(buf_y + cur_w); + cur_w += 4; + + // Authenticator + memcpy(pac_u->aut_y, buf_y + cur_w, 96); + cur_w += 96; + + // Datum + memcpy(pac_u->dat_y, buf_y + cur_w, len_w - cur_w); +} + + + + +static void +_xmas_sift_pact(u3_xmas_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, c3_w len_w) +{ + pac_u->typ_y = hed_u->typ_y; + switch ( hed_u->typ_y ) { + case PACT_PEEK: { + _xmas_sift_peek_pact(&pac_u->pek_u, hed_u, buf_y, len_w, c3y); + _log_peek_pact(&pac_u->pek_u); + } break; + case PACT_PAGE: { + _xmas_sift_page_pact(&pac_u->pag_u, hed_u, buf_y, len_w); + } break; + case PACT_POKE: { + _xmas_sift_poke_pact(&pac_u->pok_u, hed_u, buf_y, len_w); + } break; + } +} + +static u3_weak +_xmas_get_cache(u3_xmas* sam_u, u3_noun pax) +{ + return u3h_git(sam_u->pen_p, u3k(pax)); +} + +static void +_xmas_put_cache(u3_xmas* sam_u, u3_noun pax, u3_noun val) +{ + u3h_put(sam_u->pen_p, u3k(pax), val); +} + + +static void +_xmas_try_forward(u3_xmas_pact* pac_u, u3_noun fra, u3_noun hit) +{ + u3l_log("stubbed forwarding"); +} + +static void +_xmas_respond(u3_xmas_pact* req_u, u3_lane lan_u, u3_noun hit, u3_noun fra) +{ + u3_xmas_pact* res_u = c3_calloc(sizeof(*res_u)); + res_u->typ_y = PACT_PAGE; + res_u->pag_u = (u3_xmas_page_pact) { + .pat_s = req_u->pek_u.pat_s, + .tot_w = u3r_word(0, u3h(hit)), + .dat_y = c3_calloc(FINE_FRAG), + }; + res_u->pag_u.pat_c = c3_calloc(req_u->pek_u.pat_s + 1); + res_u->pag_u.pat_c[req_u->pek_u.pat_s] = '\0'; + memcpy(res_u->pag_u.pat_c, req_u->pek_u.pat_c, req_u->pek_u.pat_s); + u3r_bytes(0, 1024, res_u->pag_u.dat_y, u3dc("snag", fra, u3t(hit))); + res_u->rut_u.lan_u.por_s = lan_u.por_s; + res_u->rut_u.lan_u.pip_w = lan_u.pip_w; + _xmas_send(res_u); +} + +static void +_xmas_serve_cache_hit(u3_xmas_pact* req_u, u3_lane lan_u, u3_noun fra, u3_noun hit) +{ + c3_d her_d[2]; + memcpy(her_d, req_u->pek_u.pub_d, 2); + c3_d our_d[2]; + memcpy(our_d, req_u->sam_u->pir_u->who_d, 2); + if ( (her_d[0] != our_d[0]) + || (her_d[1] != our_d[1]) ) + { + if ( (256 > our_d[0]) + && (0 == our_d[1]) ) + { + // Forward only if requested datum is not ours, and we are a galaxy + _xmas_try_forward(req_u, fra, hit); + } else { + u3l_log("no forward, we are not a galaxy"); + } + } else { + } +} + + +/* + */ +static void +_xmas_page_scry_cb(void* vod_p, u3_noun nun) +{ + u3_xmas_pact* pac_u = vod_p; + u3_xmas* sam_u = pac_u->sam_u; + //u3l_log("scry cb, at path: %s, %u", pac_u->pek_u.pat_c, strlen(pac_u->pek_u.pat_c)); + u3_noun pat = u3do("stab", u3i_string(pac_u->pek_u.pat_c)); + u3_noun fra = u3dc("slav", c3__ud, u3do("rear", u3k(pat))); + u3_noun pax = u3do("snip", u3k(pat)); + u3_weak pas = u3r_at(7, nun); + if( u3_none == pas ) { + // TODO: mark as dead + //u3z(nun); + u3l_log("unbound"); + + return; + } + u3_weak old = _xmas_get_cache(sam_u, u3k(pax)); + if ( old == u3_none ) { + u3l_log("bad"); + } + u3_noun tag; + u3_noun dat; + u3x_cell(old, &tag, &dat); + if ( c3y == u3r_sing(u3i_string("wait"), tag) ) { + u3_noun i; + u3_noun t = u3t(u3k(dat)); + while( u3_nul != t ) { + u3x_cell(t, &i, &t); + u3_lane lan_u = u3_xmas_decode_lane(u3k(i)); + + _xmas_respond(pac_u, lan_u, u3k(pas), u3k(fra)); + + } + } + _xmas_put_cache(sam_u, u3k(pax), u3nc(c3__item, u3k(pas))); +} + + + +static void +_xmas_hear_peek(u3_xmas_pact* pac_u, u3_lane lan_u) +{ + + u3_xmas* sam_u = pac_u->sam_u; + u3_noun pat = u3i_string(pac_u->pek_u.pat_c); + u3_noun pax = u3do("stab", u3k(pat)); + u3_noun key = u3do("snip", u3k(pax)); + u3_noun fra = u3do("rear", u3k(pax)); + u3_weak hit = _xmas_get_cache(sam_u, u3k(key)); + if ( u3_none != hit ) { + u3_noun tag, dat; + u3x_cell(hit, &tag, &dat); + if ( c3y == u3r_sing(u3i_string("wait"), u3k(tag)) ) { + u3_noun liv, las; + u3x_cell(dat, &liv, &las); + u3_noun val = u3nc( + _xmas_get_now(), + u3nc(u3_xmas_encode_lane(lan_u), u3k(las)) + // TODO: a set instead of list? + ); + _xmas_put_cache(sam_u, u3k(key), u3nc(u3i_string("wait"), u3k(val))); + } + else if ( c3y == u3r_sing(u3i_string("item"), u3k(tag)) ) { + _xmas_serve_cache_hit(pac_u, lan_u, u3k(pax), u3k(dat)); + } else { + u3l_log("xmas: weird case in cache, dropping"); + } + } else { + u3_noun sky = u3nc(c3__page, u3k(key)); + u3_pier_peek_last(sam_u->car_u.pir_u, u3_nul, c3__xx, u3_nul, sky, pac_u, _xmas_page_scry_cb); + u3_noun val = u3nc( + _xmas_get_now(), + u3nc(u3_xmas_encode_lane(lan_u), u3_nul) + // TODO: a set instead of list? + ); + + _xmas_put_cache(sam_u, u3k(key), u3nc(u3i_string("wait"), val)); + // TODO: retrieve from namespace + } } static void @@ -604,13 +1006,11 @@ _xmas_hear(u3_xmas* sam_u, c3_w len_w, c3_y* hun_y) { - u3l_log("xmas: heard packet"); - _log_buf(hun_y, 4); - u3_pact_req* pac_u; + + u3_xmas_pact* pac_u; c3_w pre_w; c3_y* cur_y = hun_y; if ( HEAD_SIZE > len_w ) { - u3l_log("xmas: failed to read header"); c3_free(hun_y); return; } @@ -619,8 +1019,16 @@ _xmas_hear(u3_xmas* sam_u, pac_u->sam_u = sam_u; u3_xmas_head hed_u; _xmas_sift_head(cur_y, &hed_u); - _log_head(&hed_u); - _xmas_sift_pact(pac_u, &hed_u, hun_y + 4, (len_w - (cur_y - hun_y))); + _xmas_sift_pact(pac_u, &hed_u, hun_y + 4, (len_w - 4)); + + switch ( pac_u->typ_y ) { + case PACT_PEEK: { + _xmas_hear_peek(pac_u, *lan_u); + } break; + default: { + u3l_log("xmas: unimplemented packet type %u", pac_u->typ_y); + } break; + } } static void @@ -727,7 +1135,7 @@ _xmas_io_talk(u3_auto* car_u) uv_udp_recv_start(&sam_u->wax_u, _ames_alloc, _xmas_recv_cb); sam_u->car_u.liv_o = c3y; - u3z(rac); u3z(who); + //u3z(rac); u3z(who); } /* _xmas_io_init(): initialize ames I/O. @@ -741,12 +1149,11 @@ u3_xmas_io_init(u3_pier* pir_u) sam_u->lax_p = u3h_new_cache(100000); sam_u->req_p = u3h_new_cache(100000); - u3_assert( !uv_udp_init(u3L, &sam_u->wax_u) ); sam_u->wax_u.data = sam_u; - //sam_u->sil_u = u3s_cue_xeno_init(); - //sam_u->tes_u = ur_cue_test_init(); + sam_u->sil_u = u3s_cue_xeno_init(); + sam_u->tes_u = ur_cue_test_init(); // Disable networking for fake ships // From c6eb231fe8bb587a3808f5a54a718a65c57ce025 Mon Sep 17 00:00:00 2001 From: Liam Fitzgerald Date: Tue, 14 Nov 2023 14:38:54 +0000 Subject: [PATCH 003/430] wip --- pkg/vere/io/xmas.c | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c index 6e318f40be..e2adaf14e1 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/xmas.c @@ -446,7 +446,6 @@ _xmas_etch_head(u3_xmas_head* hed_u, c3_y buf_y[4]) if ( c3y == req_o ) { hed_w = hed_w ^ ((hed_u->ran_y & 0x3) << 30); } - u3l_log("head as int: %u", hed_w); _ames_etch_word(buf_y, hed_w); } @@ -454,7 +453,6 @@ _xmas_etch_head(u3_xmas_head* hed_u, c3_y buf_y[4]) static c3_w _xmas_etch_page_pact(c3_y* buf_y, u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u) { - u3l_log("beggining etch"); c3_w cur_w = 0; // hops @@ -463,18 +461,15 @@ _xmas_etch_page_pact(c3_y* buf_y, u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u) // path length _ames_etch_short(buf_y + cur_w, pac_u->pat_s); - u3l_log("path length: %u", pac_u->pat_s); cur_w += 2; // path - u3l_log("path : %s", pac_u->pat_c); memcpy(buf_y + cur_w, pac_u->pat_c, pac_u->pat_s + 1); cur_w += pac_u->pat_s + 1; // total _ames_etch_word(buf_y + cur_w, pac_u->tot_w); cur_w += 4; - u3l_log("total: %u", pac_u->tot_w); // auth memcpy(buf_y + cur_w, &pac_u->aut_y, 96); @@ -484,7 +479,6 @@ _xmas_etch_page_pact(c3_y* buf_y, u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u) memcpy(buf_y + cur_w, pac_u->dat_y, 1024); cur_w += 1024; - u3l_log("done: %u", cur_w); return cur_w; } @@ -582,7 +576,6 @@ static void _xmas_send(u3_xmas_pact* pac_u) add_u.sin_family = AF_INET; c3_s por_s; c3_w pip_w; - u3l_log("typ_y: %u", pac_u->rut_u.typ_y); if( pac_u->rut_u.typ_y == ROUT_GALAXY ) { por_s = _ames_czar_port(pac_u->rut_u.imp_u.her_y); pip_w = 0x7f000001; @@ -591,9 +584,8 @@ static void _xmas_send(u3_xmas_pact* pac_u) pip_w = c3n == u3_Host.ops_u.net ? pac_u->rut_u.lan_u.pip_w : 0x7f000001; por_s = pac_u->rut_u.lan_u.por_s; } + u3l_log("%x,%u", pip_w, por_s); - u3l_log("%x", pip_w); - u3l_log("%u", por_s); add_u.sin_addr.s_addr = htonl(pip_w); add_u.sin_port = htons(por_s); @@ -607,7 +599,6 @@ static void _xmas_send(u3_xmas_pact* pac_u) } c3_w siz_w = _xmas_etch_pact(buf_y, pac_u, u3k(who)); - _log_buf(buf_y, siz_w); uv_buf_t buf_u = uv_buf_init((c3_c*)buf_y, siz_w); @@ -617,7 +608,6 @@ static void _xmas_send(u3_xmas_pact* pac_u) (const struct sockaddr*)&add_u, _xmas_send_cb); - u3l_log("udp sent"); if ( sas_i ) { u3l_log("ames: send fail_sync: %s", uv_strerror(sas_i)); /*if ( c3y == sam_u->fig_u.net_o ) { @@ -879,6 +869,7 @@ _xmas_respond(u3_xmas_pact* req_u, u3_lane lan_u, u3_noun hit, u3_noun fra) { u3_xmas_pact* res_u = c3_calloc(sizeof(*res_u)); res_u->typ_y = PACT_PAGE; + res_u->sam_u = req_u->sam_u; res_u->pag_u = (u3_xmas_page_pact) { .pat_s = req_u->pek_u.pat_s, .tot_w = u3r_word(0, u3h(hit)), @@ -888,6 +879,7 @@ _xmas_respond(u3_xmas_pact* req_u, u3_lane lan_u, u3_noun hit, u3_noun fra) res_u->pag_u.pat_c[req_u->pek_u.pat_s] = '\0'; memcpy(res_u->pag_u.pat_c, req_u->pek_u.pat_c, req_u->pek_u.pat_s); u3r_bytes(0, 1024, res_u->pag_u.dat_y, u3dc("snag", fra, u3t(hit))); + res_u->rut_u.typ_y = ROUT_OTHER; res_u->rut_u.lan_u.por_s = lan_u.por_s; res_u->rut_u.lan_u.pip_w = lan_u.pip_w; _xmas_send(res_u); @@ -1006,6 +998,7 @@ _xmas_hear(u3_xmas* sam_u, c3_w len_w, c3_y* hun_y) { + u3l_log("xmas_hear"); u3_xmas_pact* pac_u; c3_w pre_w; From ca8f450947491a0f84577fa67d77c4834c4ac0c1 Mon Sep 17 00:00:00 2001 From: Liam Fitzgerald Date: Sat, 18 Nov 2023 03:07:53 +0000 Subject: [PATCH 004/430] wip --- bazel/third_party/uv/uv.BUILD | 2 +- pkg/vere/io/xmas.c | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/bazel/third_party/uv/uv.BUILD b/bazel/third_party/uv/uv.BUILD index 1fd2352559..ff7ff944a8 100644 --- a/bazel/third_party/uv/uv.BUILD +++ b/bazel/third_party/uv/uv.BUILD @@ -20,7 +20,7 @@ configure_make( "@//:linux_x86_64": ["--host=x86_64-linux-musl"], "//conditions:default": [], }), - copts = ["-O3"], + copts = ["-g -O0"], lib_source = ":all", out_static_libs = ["libuv.a"], targets = ["install"], diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c index e2adaf14e1..6babafb1cc 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/xmas.c @@ -776,7 +776,7 @@ _xmas_sift_page_pact(u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, memcpy(pac_u->aut_y, buf_y + cur_w, 96); cur_w += 96; - memcpy(pac_u->dat_y, buf_y + cur_w, 1024); + memcpy(pac_u->dat_y, buf_y + cur_w, len_w - cur_w); cur_w += 1024; } @@ -834,7 +834,6 @@ _xmas_sift_pact(u3_xmas_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, c3_w len_ switch ( hed_u->typ_y ) { case PACT_PEEK: { _xmas_sift_peek_pact(&pac_u->pek_u, hed_u, buf_y, len_w, c3y); - _log_peek_pact(&pac_u->pek_u); } break; case PACT_PAGE: { _xmas_sift_page_pact(&pac_u->pag_u, hed_u, buf_y, len_w); From feaca4e049a8c83947399cfa6fc3583692652711 Mon Sep 17 00:00:00 2001 From: Liam Fitzgerald Date: Sat, 18 Nov 2023 04:59:50 +0000 Subject: [PATCH 005/430] more wip --- pkg/c3/motes.h | 2 ++ pkg/vere/io/xmas.c | 77 ++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 69 insertions(+), 10 deletions(-) diff --git a/pkg/c3/motes.h b/pkg/c3/motes.h index 7767439029..417818018d 100644 --- a/pkg/c3/motes.h +++ b/pkg/c3/motes.h @@ -749,6 +749,7 @@ # define c3__memo c3_s4('m','e','m','o') # define c3__menu c3_s4('m','e','n','u') # define c3__mesh c3_s4('m','e','s','h') +# define c3__mess c3_s4('m','e','s','s') # define c3__met c3_s3('m','e','t') # define c3__meta c3_s4('m','e','t','a') # define c3__mill c3_s4('m','i','l','l') @@ -1319,6 +1320,7 @@ # define c3__wyrd c3_s4('w','y','r','d') # define c3__xray c3_s4('x','r','a','y') # define c3__xmas c3_s4('x','m','a','s') +# define c3__x c3_s1('x') # define c3__xx c3_s2('x','x') # define c3__yell c3_s4('y','e','l','l') # define c3__yelp c3_s4('y','e','l','p') diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c index 6babafb1cc..002d73b69a 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/xmas.c @@ -327,7 +327,10 @@ u3_xmas_encode_lane(u3_lane lan) { static u3_noun _xmas_get_now() { struct timeval tim_u; gettimeofday(&tim_u, 0); - return u3_time_in_tv(&tim_u); + u3_noun res = u3_time_in_tv(&tim_u); + u3l_log("now: %s", u3r_string(u3dc("scot", c3__da, u3k(res)))); + return res; + } static c3_o @@ -513,7 +516,7 @@ _xmas_etch_poke_pact(c3_y* buf_y, u3_xmas_poke_pact* pac_u, u3_xmas_head* hed_u) cur_w += 4; // auth - memcpy(buf_y + cur_w, &pac_u->aut_y, 96); + memcpy(buf_y + cur_w, pac_u->aut_y, 96); cur_w += 96; // day @@ -776,8 +779,9 @@ _xmas_sift_page_pact(u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, memcpy(pac_u->aut_y, buf_y + cur_w, 96); cur_w += 96; - memcpy(pac_u->dat_y, buf_y + cur_w, len_w - cur_w); - cur_w += 1024; + c3_w dat_w = 1024; // len_w - cur_w; + pac_u->dat_y = c3_calloc(dat_w); + memcpy(pac_u->dat_y, buf_y + cur_w, dat_w); } static c3_w @@ -847,13 +851,20 @@ _xmas_sift_pact(u3_xmas_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, c3_w len_ static u3_weak _xmas_get_cache(u3_xmas* sam_u, u3_noun pax) { - return u3h_git(sam_u->pen_p, u3k(pax)); + u3m_p("get cache", pax); + u3_weak res = u3h_git(sam_u->pen_p, u3k(pax)); + if( res == u3_none ) { + return res; + } else { + return u3k(res); + } } static void _xmas_put_cache(u3_xmas* sam_u, u3_noun pax, u3_noun val) { - u3h_put(sam_u->pen_p, u3k(pax), val); + + u3h_put(sam_u->pen_p, u3k(pax), u3k(val)); } @@ -894,6 +905,7 @@ _xmas_serve_cache_hit(u3_xmas_pact* req_u, u3_lane lan_u, u3_noun fra, u3_noun h if ( (her_d[0] != our_d[0]) || (her_d[1] != our_d[1]) ) { + u3l_log("publisher is not ours"); if ( (256 > our_d[0]) && (0 == our_d[1]) ) { @@ -903,10 +915,15 @@ _xmas_serve_cache_hit(u3_xmas_pact* req_u, u3_lane lan_u, u3_noun fra, u3_noun h u3l_log("no forward, we are not a galaxy"); } } else { + u3k(hit); + u3l_log("attempting to serve from cache"); + u3l_log("fragment: %u", u3r_word(0, fra)); + u3l_log("lent: %u", u3r_word(0, u3k(u3h(hit)))); + //u3l_log("first: %x", u3r_string(u3dc("scot", c3__uv, u3k(u3h(u3t(hit)))))); + _xmas_respond(req_u, lan_u, fra, hit); } } - /* */ static void @@ -929,7 +946,8 @@ _xmas_page_scry_cb(void* vod_p, u3_noun nun) u3_weak old = _xmas_get_cache(sam_u, u3k(pax)); if ( old == u3_none ) { u3l_log("bad"); - } + } + u3m_p("old", old); u3_noun tag; u3_noun dat; u3x_cell(old, &tag, &dat); @@ -944,10 +962,39 @@ _xmas_page_scry_cb(void* vod_p, u3_noun nun) } } + u3m_p("scry response val", pas); _xmas_put_cache(sam_u, u3k(pax), u3nc(c3__item, u3k(pas))); } +static void +_xmas_hear_bail(u3_ovum* egg_u, u3_noun lud) +{ + u3l_log("xmas: hear bail"); + u3_ovum_free(egg_u); +} + +static void +_xmas_hear_page(u3_xmas_pact* pac_u, u3_lane lan_u) +{ + u3_xmas* sam_u = pac_u->sam_u; + + u3_noun wir = u3nc(c3__xmas, u3_nul); + u3_noun pat = u3do("stab", u3i_string(pac_u->pag_u.pat_c)); + u3_noun mess = u3nq( + pat, + u3i_word(pac_u->pag_u.tot_w), + u3i_bytes(96, pac_u->pag_u.aut_y), + u3i_bytes(1024, pac_u->pag_u.dat_y) + ); + u3_noun cad = u3nt(c3__mess, c3__page, mess); + u3_auto_peer( + u3_auto_plan(&sam_u->car_u, + u3_ovum_init(0, c3__x, wir, cad)), + 0, 0, _xmas_hear_bail); + + +} static void _xmas_hear_peek(u3_xmas_pact* pac_u, u3_lane lan_u) @@ -957,7 +1004,7 @@ _xmas_hear_peek(u3_xmas_pact* pac_u, u3_lane lan_u) u3_noun pat = u3i_string(pac_u->pek_u.pat_c); u3_noun pax = u3do("stab", u3k(pat)); u3_noun key = u3do("snip", u3k(pax)); - u3_noun fra = u3do("rear", u3k(pax)); + u3_noun fra = u3dc("slav", c3__ud, u3do("rear", u3k(pax))); u3_weak hit = _xmas_get_cache(sam_u, u3k(key)); if ( u3_none != hit ) { u3_noun tag, dat; @@ -965,15 +1012,21 @@ _xmas_hear_peek(u3_xmas_pact* pac_u, u3_lane lan_u) if ( c3y == u3r_sing(u3i_string("wait"), u3k(tag)) ) { u3_noun liv, las; u3x_cell(dat, &liv, &las); + u3m_p("lane", las); + u3m_p("live", liv); u3_noun val = u3nc( _xmas_get_now(), u3nc(u3_xmas_encode_lane(lan_u), u3k(las)) // TODO: a set instead of list? ); + u3m_p("wait update key", key); _xmas_put_cache(sam_u, u3k(key), u3nc(u3i_string("wait"), u3k(val))); } else if ( c3y == u3r_sing(u3i_string("item"), u3k(tag)) ) { - _xmas_serve_cache_hit(pac_u, lan_u, u3k(pax), u3k(dat)); + u3l_log("item hit"); + //u3m_p("dat", u3k(dat)); + u3l_log("after dat"); + _xmas_serve_cache_hit(pac_u, lan_u, fra, u3k(dat)); } else { u3l_log("xmas: weird case in cache, dropping"); } @@ -986,6 +1039,7 @@ _xmas_hear_peek(u3_xmas_pact* pac_u, u3_lane lan_u) // TODO: a set instead of list? ); + u3m_p("key new wait", key); _xmas_put_cache(sam_u, u3k(key), u3nc(u3i_string("wait"), val)); // TODO: retrieve from namespace } @@ -1018,6 +1072,9 @@ _xmas_hear(u3_xmas* sam_u, _xmas_hear_peek(pac_u, *lan_u); } break; default: { + case PACT_PAGE: { + _xmas_hear_page(pac_u, *lan_u); + } break; u3l_log("xmas: unimplemented packet type %u", pac_u->typ_y); } break; } From ae2531543ab84af86d5bb18fe3dfce95c1e5967d Mon Sep 17 00:00:00 2001 From: Liam Fitzgerald Date: Fri, 15 Dec 2023 12:32:17 -0500 Subject: [PATCH 006/430] xmas: improve refcounting --- pkg/vere/io/xmas.c | 326 +++++++++++++++++++++------------------ pkg/vere/io/xmas/cubic.c | 89 +++++++++++ 2 files changed, 263 insertions(+), 152 deletions(-) create mode 100644 pkg/vere/io/xmas/cubic.c diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c index 002d73b69a..aae5d36db3 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/xmas.c @@ -4,6 +4,7 @@ #include "noun.h" #include "ur.h" +#include #define XMAS_DEBUG c3y #define XMAS_VER 1 @@ -13,6 +14,9 @@ #define HEAD_SIZE 4 // header size in bytes #define PACT_SIZE 1472 +#define XMAS_ITEM 1 // cache sentinels +#define XMAS_WAIT 2 + /* _u3_xmas: next generation networking */ typedef struct _u3_xmas { @@ -98,6 +102,21 @@ typedef struct _u3_xmas_pact { }; } u3_xmas_pact; +typedef enum _u3_xmas_ctag { + CACE_WAIT = 1, + CACE_ITEM = 2, +} u3_xmas_ctag; + + + +typedef struct _u3_cace_enty { + u3_xmas_ctag typ_y; + union { + // u03_xmas_cace_wait wat_u; + // u3_xmas_cace_item res_u; + }; +} u3_cace_enty; + static void _log_head(u3_xmas_head* hed_u) @@ -231,7 +250,7 @@ u3_xmas_decode_lane(u3_atom lan) { return (u3_lane){0, 0}; } - //u3z(lan); + u3z(lan); lan_u.pip_w = (c3_w)lan_d; lan_u.por_s = (c3_s)(lan_d >> 32); @@ -323,16 +342,27 @@ u3_xmas_encode_lane(u3_lane lan) { // // // +// refcounted // +static u3_noun _xmas_path_with_fra(c3_c* pat_c, c3_s* fra_s) +{ + u3_noun pax = u3do("stab", u3i_string(pat_c)); + u3_noun fra = u3dc("slav", c3__ud, u3do("rear", u3k(pax))); + u3_noun res = u3do("snip", pax); + *fra_s = u3r_short(0, fra); + + u3z(fra); // pax is unncessary bc moved in snip + return res; +} + static u3_noun _xmas_get_now() { struct timeval tim_u; gettimeofday(&tim_u, 0); u3_noun res = u3_time_in_tv(&tim_u); - u3l_log("now: %s", u3r_string(u3dc("scot", c3__da, u3k(res)))); return res; - } +// refcounted static c3_o _xmas_rout_pact(u3_xmas_pact* pac_u, u3_noun lan) { u3_noun tag, val; @@ -364,36 +394,43 @@ _xmas_rout_pact(u3_xmas_pact* pac_u, u3_noun lan) { pac_u->rut_u.lan_u = lan_u; } } - // todo refcount - // + u3z(lan); return suc_o; } +// refcounted +// RETAIN static c3_y _ship_meta(u3_noun her) { c3_w met_w = u3r_met(3, her); + c3_y res_y; switch ( met_w ) { case 0: case 1: case 2: { - return 0; + res_y = 0; + break; } case 3: case 4: { - return 1; + res_y = 1; + break; } case 5: case 6: case 7: case 8: { - return 2; + res_y = 2; + break; } default: { - return 3; + res_y = 3; + break; } } + return res_y; } @@ -407,10 +444,12 @@ _xmas_head_from_pact(u3_xmas_pact* pac_u, u3_noun her) u3_xmas_head hed_u; hed_u.pro_y = XMAS_VER; hed_u.typ_y = pac_u->typ_y; + // ship meta is retain, no u3k necessary hed_u.ran_y = _ship_meta(her); hed_u.hop_y = 0; hed_u.mug_w = 0; + u3z(her); return hed_u; } @@ -490,7 +529,7 @@ _xmas_etch_peek_pact(c3_y* buf_y, u3_xmas_peek_pact* pac_u, u3_xmas_head* hed_u, { c3_w cur_w = 0; c3_y pub_y = 2 << hed_u->ran_y; - _ames_ship_of_chubs(pac_u->pub_d, pub_y, buf_y); + _ames_ship_of_chubs(pac_u->pub_d, pub_y, buf_y); cur_w += pub_y; if( tag_o == c3y ) { @@ -595,78 +634,87 @@ static void _xmas_send(u3_xmas_pact* pac_u) //u3l_log("_ames_send %s %u", _str_typ(pac_u->typ_y), // pac_u->rut_u.lan_u.por_s); - c3_y* buf_y = c3_calloc(PACT_SIZE); + c3_y* buf_y = c3_calloc(PACT_SIZE); + c3_w siz_w; + { u3_noun who = 0; if( pac_u->typ_y == PACT_PEEK ) { who = u3i_chubs(2, pac_u->pek_u.pub_d); } - c3_w siz_w = _xmas_etch_pact(buf_y, pac_u, u3k(who)); - - uv_buf_t buf_u = uv_buf_init((c3_c*)buf_y, siz_w); - - c3_i sas_i = uv_udp_send(&pac_u->snd_u, - &sam_u->wax_u, - &buf_u, 1, - (const struct sockaddr*)&add_u, - _xmas_send_cb); - - if ( sas_i ) { - u3l_log("ames: send fail_sync: %s", uv_strerror(sas_i)); - /*if ( c3y == sam_u->fig_u.net_o ) { - //sam_u->fig_u.net_o = c3n; - }*/ - - _xmas_pact_free(pac_u); - } -} + siz_w = _xmas_etch_pact(buf_y, pac_u, who); + } + uv_buf_t buf_u = uv_buf_init((c3_c*)buf_y, siz_w); -static void -_xmas_ef_peek(u3_xmas* sam_u, u3_noun her, u3_noun las, u3_noun pat) -{ - if ( c3n == sam_u->car_u.liv_o ) { - u3l_log("xmas: not yet live, dropping outbound\r"); - //u3z(her); u3z(las); u3z(pat); - return; - } + c3_i sas_i = uv_udp_send(&pac_u->snd_u, + &sam_u->wax_u, + &buf_u, 1, + (const struct sockaddr*)&add_u, + _xmas_send_cb); - u3_xmas_pact* pac_u = c3_calloc(sizeof(*pac_u)); - pac_u->typ_y = PACT_PEEK; - pac_u->sam_u = sam_u; - u3_noun pas = u3do("spat", pat); - pac_u->pek_u.pat_s = u3r_short(0, u3dc("met", 3, u3k(pas))); - pac_u->pek_u.pat_c = u3r_string(u3k(pas)); + if ( sas_i ) { + u3l_log("ames: send fail_sync: %s", uv_strerror(sas_i)); + /*if ( c3y == sam_u->fig_u.net_o ) { + //sam_u->fig_u.net_o = c3n; + }*/ - if( 384 < strlen(pac_u->pek_u.pat_c) ) { - u3l_log("xmas: path in peek too long"); _xmas_pact_free(pac_u); - //u3z(her); u3z(las); u3z(pat); - return; } - u3r_chubs(0, 2, pac_u->pek_u.pub_d, her); +} - u3_noun lan, t = las; +static c3_o +_xmas_rout_pacs(u3_xmas_pact* pac_u, u3_noun las) +{ c3_o suc_o = c3n; - + u3_noun lan, t = las; while ( u3_nul != t ) { u3x_cell(t, &lan, &t); - if( c3n == _xmas_rout_pact(pac_u, lan) ) { + if( c3n == _xmas_rout_pact(pac_u, u3k(lan)) ) { u3l_log("xmas: failed to set route"); - //_xmas_pact_req_free(pac_u); } else { u3l_log("xmas_send"); _xmas_send(pac_u); suc_o = c3y; } } + u3z(las); + return suc_o; +} - if( c3n == suc_o ) { - _xmas_pact_free(pac_u); - } +static void +_xmas_ef_peek(u3_xmas* sam_u, u3_noun her, u3_noun las, u3_noun pat) +{ + + if ( c3n == sam_u->car_u.liv_o ) { + u3l_log("xmas: not yet live, dropping outbound\r"); + } else { + u3_xmas_pact* pac_u = c3_calloc(sizeof(*pac_u)); + pac_u->typ_y = PACT_PEEK; + pac_u->sam_u = sam_u; + { + u3_noun pas = u3do("spat", pat); + pac_u->pek_u.pat_c = u3r_string(pas); + pac_u->pek_u.pat_s = strlen(pac_u->pek_u.pat_c); + u3z(pas); + } + u3r_chubs(0, 2, pac_u->pek_u.pub_d, her); + + if( 384 < pac_u->pek_u.pat_s ) { + u3l_log("xmas: path in peek too long"); + _xmas_pact_free(pac_u); + } else { + + c3_o rut_o = _xmas_rout_pacs(pac_u, las); - //u3z(lan); + if ( c3n == rut_o ) { + _xmas_pact_free(pac_u); + } + } + } + // do not need to lose pat because moved in spat call + u3z(her); u3z(las); } @@ -678,7 +726,6 @@ static c3_o _xmas_kick(u3_xmas* sam_u, u3_noun tag, u3_noun dat) default: { ret_o = c3n; } break; - // case c3__peek: { u3_noun lan, her, pat; @@ -691,7 +738,9 @@ static c3_o _xmas_kick(u3_xmas* sam_u, u3_noun tag, u3_noun dat) } break; } - //u3z(tag); u3z(dat); + // technically losing tag is unncessary as it always should + // be a direct atom, but better to be strict + u3z(dat); u3z(tag); return ret_o; } @@ -699,7 +748,7 @@ static c3_o _xmas_io_kick(u3_auto* car_u, u3_noun wir, u3_noun cad) { u3_xmas* sam_u = (u3_xmas*)car_u; - u3_noun tag, dat, i_wir; + u3_noun tag, dat; c3_o ret_o; if( c3n == u3r_cell(cad, &tag, &dat) ) @@ -709,18 +758,12 @@ static c3_o _xmas_io_kick(u3_auto* car_u, u3_noun wir, u3_noun cad) ret_o = _xmas_kick(sam_u, u3k(tag), u3k(dat)); - //u3z(wir); u3z(cad); + u3z(wir); u3z(cad); return ret_o; } - - - - - - static u3_noun _xmas_io_info(u3_auto* car_u) { @@ -799,8 +842,6 @@ _xmas_sift_peek_pact(u3_xmas_peek_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, pac_u->pat_s = _ames_sift_short(buf_y + cur_w); // str len cur_w += 2; - // if ( pac_u->pek_u.pat_s < 325 ) { TODO: check validity - pac_u->pat_c = c3_calloc(pac_u->pat_s + 1); memcpy(pac_u->pat_c, buf_y + cur_w, pac_u->pat_s + 1); pac_u->pat_c[pac_u->pat_s] = '\0'; @@ -813,7 +854,7 @@ _xmas_sift_poke_pact(u3_xmas_poke_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, { c3_w cur_w = 0; // Peek portion - pac_u->pek_u = c3_calloc(sizeof(pac_u->pek_u)); + pac_u->pek_u = c3_calloc(sizeof(*(pac_u->pek_u))); cur_w += _xmas_sift_peek_pact(pac_u->pek_u, hed_u, buf_y, len_w, c3n); // Total fragments @@ -848,23 +889,20 @@ _xmas_sift_pact(u3_xmas_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, c3_w len_ } } -static u3_weak + +/* + * RETAIN + */ +static c3_o _xmas_get_cache(u3_xmas* sam_u, u3_noun pax) { - u3m_p("get cache", pax); - u3_weak res = u3h_git(sam_u->pen_p, u3k(pax)); - if( res == u3_none ) { - return res; - } else { - return u3k(res); - } + return u3h_git(sam_u->pen_p, pax); } static void _xmas_put_cache(u3_xmas* sam_u, u3_noun pax, u3_noun val) { - - u3h_put(sam_u->pen_p, u3k(pax), u3k(val)); + u3h_put(sam_u->pen_p, pax, val); } @@ -875,28 +913,24 @@ _xmas_try_forward(u3_xmas_pact* pac_u, u3_noun fra, u3_noun hit) } static void -_xmas_respond(u3_xmas_pact* req_u, u3_lane lan_u, u3_noun hit, u3_noun fra) +_xmas_respond(u3_xmas_pact* req_u, u3_xmas_pact* res_u, u3_noun hit, c3_s fra_s) { - u3_xmas_pact* res_u = c3_calloc(sizeof(*res_u)); res_u->typ_y = PACT_PAGE; res_u->sam_u = req_u->sam_u; res_u->pag_u = (u3_xmas_page_pact) { .pat_s = req_u->pek_u.pat_s, - .tot_w = u3r_word(0, u3h(hit)), + .tot_w = u3r_word(0, u3k(u3h(hit))), .dat_y = c3_calloc(FINE_FRAG), }; res_u->pag_u.pat_c = c3_calloc(req_u->pek_u.pat_s + 1); res_u->pag_u.pat_c[req_u->pek_u.pat_s] = '\0'; memcpy(res_u->pag_u.pat_c, req_u->pek_u.pat_c, req_u->pek_u.pat_s); - u3r_bytes(0, 1024, res_u->pag_u.dat_y, u3dc("snag", fra, u3t(hit))); - res_u->rut_u.typ_y = ROUT_OTHER; - res_u->rut_u.lan_u.por_s = lan_u.por_s; - res_u->rut_u.lan_u.pip_w = lan_u.pip_w; - _xmas_send(res_u); + u3r_bytes(0, 1024, res_u->pag_u.dat_y, u3dc("snag", u3i_word(fra_s), u3k(u3t(hit)))); + u3z(hit); } static void -_xmas_serve_cache_hit(u3_xmas_pact* req_u, u3_lane lan_u, u3_noun fra, u3_noun hit) +_xmas_serve_cache_hit(u3_xmas_pact* req_u, u3_lane lan_u, c3_s fra_s, u3_noun hit) { c3_d her_d[2]; memcpy(her_d, req_u->pek_u.pub_d, 2); @@ -910,18 +944,18 @@ _xmas_serve_cache_hit(u3_xmas_pact* req_u, u3_lane lan_u, u3_noun fra, u3_noun h && (0 == our_d[1]) ) { // Forward only if requested datum is not ours, and we are a galaxy - _xmas_try_forward(req_u, fra, hit); + _xmas_try_forward(req_u, fra_s, hit); } else { u3l_log("no forward, we are not a galaxy"); } } else { - u3k(hit); - u3l_log("attempting to serve from cache"); - u3l_log("fragment: %u", u3r_word(0, fra)); - u3l_log("lent: %u", u3r_word(0, u3k(u3h(hit)))); //u3l_log("first: %x", u3r_string(u3dc("scot", c3__uv, u3k(u3h(u3t(hit)))))); - _xmas_respond(req_u, lan_u, fra, hit); + u3_xmas_pact* res_u = c3_calloc(sizeof(*res_u)); + _xmas_respond(req_u, res_u, fra_s, hit); + _xmas_send(res_u); } + // no lose needed because all nouns are moved in both + // branches of the conditional } /* @@ -931,39 +965,35 @@ _xmas_page_scry_cb(void* vod_p, u3_noun nun) { u3_xmas_pact* pac_u = vod_p; u3_xmas* sam_u = pac_u->sam_u; - //u3l_log("scry cb, at path: %s, %u", pac_u->pek_u.pat_c, strlen(pac_u->pek_u.pat_c)); - u3_noun pat = u3do("stab", u3i_string(pac_u->pek_u.pat_c)); - u3_noun fra = u3dc("slav", c3__ud, u3do("rear", u3k(pat))); - u3_noun pax = u3do("snip", u3k(pat)); - u3_weak pas = u3r_at(7, nun); - if( u3_none == pas ) { + c3_s fra_s; + u3_noun pax = _xmas_path_with_fra(pac_u->pek_u.pat_c, &fra_s); + + u3_weak hit = u3r_at(7, nun); + if ( u3_none == hit ) { // TODO: mark as dead //u3z(nun); u3l_log("unbound"); - return; - } - u3_weak old = _xmas_get_cache(sam_u, u3k(pax)); - if ( old == u3_none ) { - u3l_log("bad"); - } - u3m_p("old", old); - u3_noun tag; - u3_noun dat; - u3x_cell(old, &tag, &dat); - if ( c3y == u3r_sing(u3i_string("wait"), tag) ) { - u3_noun i; - u3_noun t = u3t(u3k(dat)); - while( u3_nul != t ) { - u3x_cell(t, &i, &t); - u3_lane lan_u = u3_xmas_decode_lane(u3k(i)); - - _xmas_respond(pac_u, lan_u, u3k(pas), u3k(fra)); - + } else { + u3_weak old = _xmas_get_cache(sam_u, pax); + if ( old == u3_none ) { + u3l_log("bad"); + } else { + u3_noun tag; + u3_noun dat; + u3x_cell(old, &tag, &dat); + if ( c3y == u3r_sing(u3i_string("wait"), tag) ) { + u3_xmas_pact* res_u = c3_calloc(sizeof(*res_u)); + _xmas_respond(pac_u, res_u, u3k(hit), fra_s); + + _xmas_rout_pacs(res_u, u3k(u3t(dat))); + } + _xmas_put_cache(sam_u, u3k(pax), u3nc(c3__item, u3k(hit))); + u3z(old); } + u3z(hit); } - u3m_p("scry response val", pas); - _xmas_put_cache(sam_u, u3k(pax), u3nc(c3__item, u3k(pas))); + u3z(pax); } static void @@ -993,7 +1023,18 @@ _xmas_hear_page(u3_xmas_pact* pac_u, u3_lane lan_u) u3_ovum_init(0, c3__x, wir, cad)), 0, 0, _xmas_hear_bail); + // no lose required because all nouns are moved +} +static void +_xmas_add_lane_to_cache(u3_xmas* sam_u, u3_noun las, u3_noun pax, u3_lane lan_u) +{ + u3_noun hit = u3nq(XMAS_WAIT, + _xmas_get_now(), + u3_xmas_encode_lane(lan_u), + las); + _xmas_put_cache(sam_u, pax, hit); + u3z(hit); u3z(las); u3z(pax); } static void @@ -1001,48 +1042,29 @@ _xmas_hear_peek(u3_xmas_pact* pac_u, u3_lane lan_u) { u3_xmas* sam_u = pac_u->sam_u; - u3_noun pat = u3i_string(pac_u->pek_u.pat_c); - u3_noun pax = u3do("stab", u3k(pat)); - u3_noun key = u3do("snip", u3k(pax)); - u3_noun fra = u3dc("slav", c3__ud, u3do("rear", u3k(pax))); - u3_weak hit = _xmas_get_cache(sam_u, u3k(key)); + c3_s fra_s; + u3_noun pax = _xmas_path_with_fra(pac_u->pek_u.pat_c, &fra_s); + u3_weak hit = _xmas_get_cache(sam_u, u3k(pax)); if ( u3_none != hit ) { u3_noun tag, dat; u3x_cell(hit, &tag, &dat); - if ( c3y == u3r_sing(u3i_string("wait"), u3k(tag)) ) { - u3_noun liv, las; - u3x_cell(dat, &liv, &las); - u3m_p("lane", las); - u3m_p("live", liv); - u3_noun val = u3nc( - _xmas_get_now(), - u3nc(u3_xmas_encode_lane(lan_u), u3k(las)) - // TODO: a set instead of list? - ); - u3m_p("wait update key", key); - _xmas_put_cache(sam_u, u3k(key), u3nc(u3i_string("wait"), u3k(val))); - } - else if ( c3y == u3r_sing(u3i_string("item"), u3k(tag)) ) { + if ( tag == XMAS_WAIT ) { + _xmas_add_lane_to_cache(sam_u, u3k(u3t(dat)), u3k(pax), lan_u); + } else if ( tag == XMAS_ITEM ) { u3l_log("item hit"); //u3m_p("dat", u3k(dat)); u3l_log("after dat"); - _xmas_serve_cache_hit(pac_u, lan_u, fra, u3k(dat)); + _xmas_serve_cache_hit(pac_u, lan_u, fra_s, u3k(dat)); } else { u3l_log("xmas: weird case in cache, dropping"); } + u3z(hit); } else { - u3_noun sky = u3nc(c3__page, u3k(key)); + u3_noun sky = u3nc(c3__page, u3k(pax)); u3_pier_peek_last(sam_u->car_u.pir_u, u3_nul, c3__xx, u3_nul, sky, pac_u, _xmas_page_scry_cb); - u3_noun val = u3nc( - _xmas_get_now(), - u3nc(u3_xmas_encode_lane(lan_u), u3_nul) - // TODO: a set instead of list? - ); - - u3m_p("key new wait", key); - _xmas_put_cache(sam_u, u3k(key), u3nc(u3i_string("wait"), val)); - // TODO: retrieve from namespace + _xmas_add_lane_to_cache(sam_u, u3_nul, u3k(pax), lan_u); // TODO: retrieve from namespace } + u3z(pax); } static void diff --git a/pkg/vere/io/xmas/cubic.c b/pkg/vere/io/xmas/cubic.c new file mode 100644 index 0000000000..d4830d44f1 --- /dev/null +++ b/pkg/vere/io/xmas/cubic.c @@ -0,0 +1,89 @@ +/// @file +/// +#include "vere.h" + +#include "noun.h" +#include "ur.h" + + + +typedef struct _u3_cbic_stat { + c3_w las_w; // w_last_max + c3_d poc_d; // epoch start + c3_w rig_w // origin point + c3_w del_w; // delay min + c3_w win_w; // w_tcp + c3_w cac_w; // ack count + +} u3_cbic_stat; + +typedef struct _u3_cong { + c3_w rtt_w; // rtt + c3_w rto_w; // rto + c3_w rtv-w; // rttvar + c3_w wnd_w; // cwnd + c3_w wnc_w; // cwnd_cnt + c3_w sst_w; // ssthresh + c3_w con_w; // counter +} u3_cong; + +typedef struct _u3_cbic { + u3_cong con_u; + u3_cbic_stat sat_u; +} u3_cbic; + + +void u3_cbic_reset(u3_cbic* sat_u) +{ + sat_u->las_w = 0; + sat_u->poc_d = 0; + sat_u->rig_w = 0; + sat_u->del_w = 0; + sat_u->win_w = 0; + sat_u->cac_w = 0; + +} + +void u3_cbic_init(u3_cbic_stats* sat_u) +{ + +} + +void u3_cbic_update(u3_cbic* cub_u) +{ + c3_w ack_w += 1; + c3_d tim_d = time(); + if ( 0 == cub_u->sat_w.poc_d ) { + cub_u->sat_w.poc_w = tim_d; + if ( cub_u->con_u.wnd_w + } else { + ifk + + } + +} + + + +void u3_cbic_on_ack(u3_cbic* cub_u) +{ + c3_w rtt_w = cub_u->con_u.rtt_w; // TODO actually update + if ( 0 == cub_u->sat_u.del_w ) { + cub_u->sat_u.del_w = c3_min(cub_u->sat_u.del_w, rtt_w); + } else { + cub_u->sat_u.del_w = rtt_w; + } + + if ( cub_u->con_u.wnd_w <= cub_u->con_u.sst_w ) { + cub_u->sat_u.wnd_w += 1; + } else { + if ( cub_u->con_u.wnc_w > cub_u->con_u.wnd_w ) { + cub_u->con_u.wnd_w += 1; + cub_u->con_u.wnc_w = 0; + } else { + cub_u->con_u.wnc_w += 1; + } + } +} + + From 8624944c85094d863f75a2e9fbc2f9290ac1a47f Mon Sep 17 00:00:00 2001 From: Liam Fitzgerald Date: Fri, 15 Dec 2023 14:08:24 -0500 Subject: [PATCH 007/430] wip --- BUILD.bazel | 2 +- pkg/vere/BUILD.bazel | 13 +++++++++++++ pkg/vere/io/xmas.c | 36 +++++++++++++++++++++++++----------- pkg/vere/xmas_tests.c | 21 +++++++++++++++------ 4 files changed, 54 insertions(+), 18 deletions(-) diff --git a/BUILD.bazel b/BUILD.bazel index ff11949f5f..6220e02702 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -77,7 +77,7 @@ config_setting( string_flag( name = "clang_version", # macOS uses `clang-14.0.3` by default. - build_setting_default = "14.0.3", + build_setting_default = "15.0.0", visibility = ["//visibility:public"], ) diff --git a/pkg/vere/BUILD.bazel b/pkg/vere/BUILD.bazel index f856ce0047..4e58a2c192 100644 --- a/pkg/vere/BUILD.bazel +++ b/pkg/vere/BUILD.bazel @@ -184,6 +184,19 @@ cc_test( deps = [":vere"], ) +cc_test( + name = "xmas_tests", + timeout = "short", + srcs = ["xmas_tests.c", "io/xmas.c"], + features = select({ + "@platforms//os:linux": ["fully_static_link"], + "//conditions:default": [], + }), + visibility = ["//visibility:private"], + deps = [":vere"], +) + + cc_test( name = "boot_tests", timeout = "short", diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c index aae5d36db3..da2adec165 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/xmas.c @@ -67,17 +67,25 @@ typedef struct _u3_xmas_head { c3_y hop_y; // hopcount } u3_xmas_head; +typedef struct _u3_xmas_name { + c3_d her_d[2]; + c3_c* pat_c; + c3_s pat_s; + c3_s boq_s; + c3_s fra_s; +} u3_xmas_name; + typedef struct _u3_xmas_peek_pact { - c3_d pub_d[2]; - c3_s pat_s; - c3_c* pat_c; + c3_d pub_d[2]; + c3_c* pat_c; + c3_s pat_s; } u3_xmas_peek_pact; typedef struct _u3_xmas_page_pact { c3_y hop_y[6]; - c3_s pat_s; c3_c* pat_c; + c3_s pat_s; c3_w tot_w; c3_y aut_y[96]; // heap allocate?? c3_y* dat_y; @@ -449,7 +457,7 @@ _xmas_head_from_pact(u3_xmas_pact* pac_u, u3_noun her) hed_u.hop_y = 0; hed_u.mug_w = 0; - u3z(her); + //u3z(her); return hed_u; } @@ -517,7 +525,7 @@ _xmas_etch_page_pact(c3_y* buf_y, u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u) memcpy(buf_y + cur_w, &pac_u->aut_y, 96); cur_w += 96; - // day + // dat memcpy(buf_y + cur_w, pac_u->dat_y, 1024); cur_w += 1024; @@ -827,6 +835,13 @@ _xmas_sift_page_pact(u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, memcpy(pac_u->dat_y, buf_y + cur_w, dat_w); } +static c3_w +_xmas_sift_name(u3_xmas_name* nam_u, c3_y siz_y, c3_y* buf_y, c3_w len_w) +{ + return 0; +} + + static c3_w _xmas_sift_peek_pact(u3_xmas_peek_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, c3_w len_w, c3_o tag_o) { @@ -869,9 +884,6 @@ _xmas_sift_poke_pact(u3_xmas_poke_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, memcpy(pac_u->dat_y, buf_y + cur_w, len_w - cur_w); } - - - static void _xmas_sift_pact(u3_xmas_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, c3_w len_w) { @@ -1040,6 +1052,7 @@ _xmas_add_lane_to_cache(u3_xmas* sam_u, u3_noun las, u3_noun pax, u3_lane lan_u) static void _xmas_hear_peek(u3_xmas_pact* pac_u, u3_lane lan_u) { + u3l_log("xmas: hear peek"); u3_xmas* sam_u = pac_u->sam_u; c3_s fra_s; @@ -1060,10 +1073,11 @@ _xmas_hear_peek(u3_xmas_pact* pac_u, u3_lane lan_u) } u3z(hit); } else { - u3_noun sky = u3nc(c3__page, u3k(pax)); - u3_pier_peek_last(sam_u->car_u.pir_u, u3_nul, c3__xx, u3_nul, sky, pac_u, _xmas_page_scry_cb); _xmas_add_lane_to_cache(sam_u, u3_nul, u3k(pax), lan_u); // TODO: retrieve from namespace + u3_noun sky = u3nc(c3__page, pax); + u3_pier_peek_last(sam_u->car_u.pir_u, u3_nul, c3__xx, u3_nul, sky, pac_u, _xmas_page_scry_cb); } + u3l_log("xmas: finished hear peek"); u3z(pax); } diff --git a/pkg/vere/xmas_tests.c b/pkg/vere/xmas_tests.c index 373788740f..e799b9f55e 100644 --- a/pkg/vere/xmas_tests.c +++ b/pkg/vere/xmas_tests.c @@ -1,7 +1,6 @@ /// @file -#include "noun.h" -#include "vere.h" +#include "./io/xmas.c" /* _setup(): prepare for tests. */ @@ -15,14 +14,14 @@ _setup(void) /* _test_ames(): spot check ames helpers */ static void -_test_ames(void) +_test_xmas(void) { u3_lane lan_u; lan_u.pip_w = 0x7f000001; lan_u.por_s = 12345; u3_noun lan = u3_ames_encode_lane(lan_u); - u3_lane nal_u = u3_ames_decode_lane(u3k(lan)); + u3_lane nal_u = u3_ames_decode_lane(u3k(lan)); u3_lane nal_u2 = u3_ames_decode_lane(lan); if ( !(lan_u.pip_w == nal_u.pip_w && lan_u.por_s == nal_u.por_s) ) { @@ -30,6 +29,16 @@ _test_ames(void) fprintf(stderr, "pip: %d, por: %d\r\n", nal_u.pip_w, nal_u.por_s); exit(1); } + + + + u3_xmas_name pok_u = (u3_xmas_name) { + .her_d = {43, 0}, + .pat_c = "/foo/bar", + .pat_s = 8, + .boq_s = 13, + .fra_s = 1, + }; } /* main(): run all test cases. @@ -39,12 +48,12 @@ main(int argc, char* argv[]) { _setup(); - _test_ames(); + _test_xmas(); // GC // u3m_grab(u3_none); - fprintf(stderr, "ames okeedokee\n"); + fprintf(stderr, "xmas okeedokee\n"); return 0; } From 59b5909d773050470b8e14d87ee3701f7b1e44c4 Mon Sep 17 00:00:00 2001 From: Liam Fitzgerald Date: Fri, 15 Dec 2023 17:34:10 -0500 Subject: [PATCH 008/430] wip --- pkg/vere/io/xmas.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c index da2adec165..719fa367ec 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/xmas.c @@ -457,7 +457,7 @@ _xmas_head_from_pact(u3_xmas_pact* pac_u, u3_noun her) hed_u.hop_y = 0; hed_u.mug_w = 0; - //u3z(her); + u3z(her); return hed_u; } From c73485e97fa07da349a30ea87524fa364433f959 Mon Sep 17 00:00:00 2001 From: parallels Date: Thu, 4 Jan 2024 10:27:08 -0600 Subject: [PATCH 009/430] wip --- pkg/vere/io/xmas.c | 55 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 17 deletions(-) diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c index 719fa367ec..4dca3111f1 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/xmas.c @@ -619,6 +619,7 @@ _xmas_send_cb(uv_udp_send_t* req_u, c3_i sas_i) static void _xmas_send(u3_xmas_pact* pac_u) { u3_xmas* sam_u = pac_u->sam_u; + u3l_log("xmas sending"); struct sockaddr_in add_u; @@ -676,10 +677,12 @@ _xmas_rout_pacs(u3_xmas_pact* pac_u, u3_noun las) { c3_o suc_o = c3n; u3_noun lan, t = las; - while ( u3_nul != t ) { + while ( t != u3_nul ) { u3x_cell(t, &lan, &t); - - if( c3n == _xmas_rout_pact(pac_u, u3k(lan)) ) { + // if ( c3n == u3r_cell(t, &lan, &t) ) { + // break; + // } + if ( c3n == _xmas_rout_pact(pac_u, u3k(lan)) ) { u3l_log("xmas: failed to set route"); } else { u3l_log("xmas_send"); @@ -714,7 +717,7 @@ _xmas_ef_peek(u3_xmas* sam_u, u3_noun her, u3_noun las, u3_noun pat) _xmas_pact_free(pac_u); } else { - c3_o rut_o = _xmas_rout_pacs(pac_u, las); + c3_o rut_o = _xmas_rout_pacs(pac_u, u3k(las)); if ( c3n == rut_o ) { _xmas_pact_free(pac_u); @@ -905,15 +908,23 @@ _xmas_sift_pact(u3_xmas_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, c3_w len_ /* * RETAIN */ -static c3_o +static u3_weak _xmas_get_cache(u3_xmas* sam_u, u3_noun pax) { - return u3h_git(sam_u->pen_p, pax); + u3_weak res = u3h_git(sam_u->pen_p, pax); + if ( u3_none == res ) { + u3m_p("miss", u3k(pax)); + } else { + u3m_p("hit", u3nc(u3k(pax), u3k(res))); + } + return res; } static void _xmas_put_cache(u3_xmas* sam_u, u3_noun pax, u3_noun val) { + u3m_p("new key", u3k(pax)); + u3m_p("new value", u3k(val)); u3h_put(sam_u->pen_p, pax, val); } @@ -928,6 +939,7 @@ static void _xmas_respond(u3_xmas_pact* req_u, u3_xmas_pact* res_u, u3_noun hit, c3_s fra_s) { res_u->typ_y = PACT_PAGE; + res_u->sam_u = req_u->sam_u; res_u->pag_u = (u3_xmas_page_pact) { .pat_s = req_u->pek_u.pat_s, @@ -944,6 +956,7 @@ _xmas_respond(u3_xmas_pact* req_u, u3_xmas_pact* res_u, u3_noun hit, c3_s fra_s) static void _xmas_serve_cache_hit(u3_xmas_pact* req_u, u3_lane lan_u, c3_s fra_s, u3_noun hit) { + u3l_log("serving cache hit"); c3_d her_d[2]; memcpy(her_d, req_u->pek_u.pub_d, 2); c3_d our_d[2]; @@ -963,7 +976,9 @@ _xmas_serve_cache_hit(u3_xmas_pact* req_u, u3_lane lan_u, c3_s fra_s, u3_noun hi } else { //u3l_log("first: %x", u3r_string(u3dc("scot", c3__uv, u3k(u3h(u3t(hit)))))); u3_xmas_pact* res_u = c3_calloc(sizeof(*res_u)); - _xmas_respond(req_u, res_u, fra_s, hit); + res_u->rut_u.typ_y = ROUT_OTHER; + res_u->rut_u.lan_u = lan_u; + _xmas_respond(req_u, res_u, hit, fra_s); _xmas_send(res_u); } // no lose needed because all nouns are moved in both @@ -993,14 +1008,14 @@ _xmas_page_scry_cb(void* vod_p, u3_noun nun) } else { u3_noun tag; u3_noun dat; - u3x_cell(old, &tag, &dat); - if ( c3y == u3r_sing(u3i_string("wait"), tag) ) { + u3x_cell(u3k(old), &tag, &dat); + if ( XMAS_WAIT == tag ) { u3_xmas_pact* res_u = c3_calloc(sizeof(*res_u)); _xmas_respond(pac_u, res_u, u3k(hit), fra_s); _xmas_rout_pacs(res_u, u3k(u3t(dat))); } - _xmas_put_cache(sam_u, u3k(pax), u3nc(c3__item, u3k(hit))); + _xmas_put_cache(sam_u, u3k(pax), u3nc(XMAS_ITEM, u3k(hit))); u3z(old); } u3z(hit); @@ -1043,29 +1058,31 @@ _xmas_add_lane_to_cache(u3_xmas* sam_u, u3_noun las, u3_noun pax, u3_lane lan_u) { u3_noun hit = u3nq(XMAS_WAIT, _xmas_get_now(), - u3_xmas_encode_lane(lan_u), - las); + u3nc(c3n, u3_xmas_encode_lane(lan_u)), + u3k(las)); _xmas_put_cache(sam_u, pax, hit); - u3z(hit); u3z(las); u3z(pax); + u3z(las); } static void _xmas_hear_peek(u3_xmas_pact* pac_u, u3_lane lan_u) { u3l_log("xmas: hear peek"); - + u3l_log("%hu", lan_u.por_s); u3_xmas* sam_u = pac_u->sam_u; c3_s fra_s; u3_noun pax = _xmas_path_with_fra(pac_u->pek_u.pat_c, &fra_s); + u3l_log("fra: %s", fra_s); u3_weak hit = _xmas_get_cache(sam_u, u3k(pax)); if ( u3_none != hit ) { u3_noun tag, dat; - u3x_cell(hit, &tag, &dat); + u3x_cell(u3k(hit), &tag, &dat); + u3m_p("dat", dat); if ( tag == XMAS_WAIT ) { _xmas_add_lane_to_cache(sam_u, u3k(u3t(dat)), u3k(pax), lan_u); } else if ( tag == XMAS_ITEM ) { u3l_log("item hit"); - //u3m_p("dat", u3k(dat)); + u3m_p("dat", u3k(dat)); u3l_log("after dat"); _xmas_serve_cache_hit(pac_u, lan_u, fra_s, u3k(dat)); } else { @@ -1087,6 +1104,7 @@ _xmas_hear(u3_xmas* sam_u, c3_w len_w, c3_y* hun_y) { + u3l_log("xmas_hear"); u3_xmas_pact* pac_u; @@ -1143,9 +1161,12 @@ _xmas_recv_cb(uv_udp_t* wax_u, struct sockaddr_in* add_u = (struct sockaddr_in*)adr_u; u3_lane lan_u; + u3l_log("test"); + lan_u.por_s = ntohs(add_u->sin_port); + // u3l_log("port: %s", lan_u.por_s); lan_u.pip_w = ntohl(add_u->sin_addr.s_addr); - + // u3l_log("IP: %x", lan_u.pip_w); // NB: [nrd_i] will never exceed max length from _ames_alloc() // _xmas_hear(sam_u, &lan_u, (c3_w)nrd_i, (c3_y*)buf_u->base); From 6d0877cf2a92d1a832f3b62bdd3de38e340baae1 Mon Sep 17 00:00:00 2001 From: Liam Fitzgerald Date: Thu, 11 Jan 2024 11:55:00 -0500 Subject: [PATCH 010/430] wip --- pkg/c3/motes.h | 1 + pkg/vere/io/xmas.c | 622 +++++++++++++++++++++++++++++++++------------ pkg/vere/pier.c | 2 +- pkg/vere/serf.c | 2 +- 4 files changed, 462 insertions(+), 165 deletions(-) diff --git a/pkg/c3/motes.h b/pkg/c3/motes.h index 417818018d..f2d546c013 100644 --- a/pkg/c3/motes.h +++ b/pkg/c3/motes.h @@ -851,6 +851,7 @@ # define c3__ovum c3_s4('o','v','u','m') # define c3__p c3_s1('p') # define c3__pack c3_s4('p','a','c','k') +# define c3__pact c3_s4('p','a','c','t') # define c3__page c3_s4('p','a','g','e') # define c3__pair c3_s4('p','a','i','r') # define c3__palm c3_s4('p','a','l','m') diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c index 4dca3111f1..5fe5289177 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/xmas.c @@ -4,7 +4,9 @@ #include "noun.h" #include "ur.h" +#include #include +#include #define XMAS_DEBUG c3y #define XMAS_VER 1 @@ -14,8 +16,18 @@ #define HEAD_SIZE 4 // header size in bytes #define PACT_SIZE 1472 -#define XMAS_ITEM 1 // cache sentinels -#define XMAS_WAIT 2 +// pending interest sentinels +#define XMAS_ITEM 1 // cached item +#define XMAS_SCRY 2 // waiting on scry +// routing table sentinels +#define XMAS_CZAR 1 // pending dns lookup +#define XMAS_ROUT 2 // have route +// +#define XMAS_COOKIE_LEN 4 +static c3_y XMAS_COOKIE[4] = { 0x5e, 0x1d, 0xad, 0x51 }; + + +#define SIFT_VAR(dest, src, len) dest = 0; for(int i = 0; i < len; i++ ) { dest |= ((src + i) >> (8*i)); } /* _u3_xmas: next generation networking */ @@ -29,18 +41,21 @@ typedef struct _u3_xmas { c3_l sev_l; ur_cue_test_t* tes_u; // cue-test handle u3_cue_xeno* sil_u; // cue handle - u3p(u3h_root) lax_p; + u3p(u3h_root) her_p; // u3p(u3h_root) pen_p; c3_w imp_w[256]; - time_t imp_t[256]; // imperial IP timestamps - c3_o imp_o[256]; // imperial print status + time_t imp_t[256]; // imperial IP timestamps + c3_o imp_o[256]; // imperial print status + c3_c* dns_c; // + // jj u3p(u3h_root) req_p; } u3_xmas; typedef enum _u3_xmas_ptag { + PACT_RESV = 0, PACT_PEEK = 1, - PACT_POKE = 2, - PACT_PAGE = 3 + PACT_PAGE = 2, + PACT_POKE = 3, } u3_xmas_ptag; typedef enum _u3_xmas_rout_tag { @@ -48,52 +63,88 @@ typedef enum _u3_xmas_rout_tag { ROUT_OTHER = 1 } u3_xmas_rout_tag; +typedef enum _u3_xmas_nexh { + NEXH_NONE = 0, + NEXH_SBYT = 1, + NEXH_ONLY = 2, + NEXH_MANY = 3 +} u3_xmas_nexh; + +typedef struct _u3_xmas_name_meta { + // reserved (2 bits) + c3_y ran_y; // rank (2 bits) + c3_y rif_y; // rift-len (2 bits) + c3_o pat_o; // path length length (1bit) + c3_o boq_o; // 0b1 if custom, 0b0 implicitly 13 (1bit) + c3_o fra_y; // fragment number length (2bit) +} u3_xmas_name_meta; + +typedef struct _u3_xmas_name { + u3_xmas_name_meta met_u; + c3_d her_d[2]; + c3_w rif_w; + c3_s pat_s; + c3_c* pat_c; + c3_y boq_y; + c3_w fra_w; +} u3_xmas_name; + typedef struct _u3_xmas_rout { u3_xmas_rout_tag typ_y; // type tag union { - struct { - c3_c* dns_c; - c3_y her_y; - } imp_u; + c3_y imp_y; u3_lane lan_u; }; } u3_xmas_rout; typedef struct _u3_xmas_head { + u3_xmas_nexh nex_y; // next-hop c3_y pro_y; // protocol version u3_xmas_ptag typ_y; // packet type - c3_y ran_y; // publisher rank - c3_w mug_w; // mug checksum c3_y hop_y; // hopcount + c3_w mug_w; // mug checksum } u3_xmas_head; -typedef struct _u3_xmas_name { - c3_d her_d[2]; - c3_c* pat_c; - c3_s pat_s; - c3_s boq_s; - c3_s fra_s; -} u3_xmas_name; +// +// +$ cache +// [%rout lanes=(list lanes)] +// [%pending pacs=(list pact)] typedef struct _u3_xmas_peek_pact { - c3_d pub_d[2]; - c3_c* pat_c; - c3_s pat_s; + u3_xmas_name nam_u; } u3_xmas_peek_pact; +typedef struct _u3_xmas_page_meat_meta { + c3_y tot_y; // total fragments length (2bit) + c3_o aut_o; // authentication len len (1bit) + c3_y len_y; // fragment length (5bit) +} u3_xmas_page_meat_meta; + +typedef enum _u3_xmas_auth_type { + AUTH_SIG = 1, + AUTH_DIG = 2, + AUTH_HMAC = 3, +} u3_xmas_auth_type; + +typedef struct _u3_xmas_page_meat { + u3_xmas_page_meat_meta met_u; // metadata + c3_w tot_w; // total fragments + c3_y aul_y; // authentication length + u3_xmas_auth_type ayp_y; // authentication type + c3_y aut_y[255]; // authentication + c3_d len_d; // fragment length + c3_y* fra_y; // fragment +} u3_xmas_page_meat; + typedef struct _u3_xmas_page_pact { - c3_y hop_y[6]; - c3_c* pat_c; - c3_s pat_s; - c3_w tot_w; - c3_y aut_y[96]; // heap allocate?? - c3_y* dat_y; + u3_xmas_name nam_u; + u3_xmas_page_meat mat_u; } u3_xmas_page_pact; typedef struct _u3_xmas_poke_pact { u3_xmas_peek_pact* pek_u; - c3_w tot_w; + c3_w tot_w; c3_y aut_y[96]; // heap allocate?? c3_y* dat_y; } u3_xmas_poke_pact; @@ -125,14 +176,22 @@ typedef struct _u3_cace_enty { }; } u3_cace_enty; +typedef struct _u3_seal { + uv_udp_send_t snd_u; // udp send request + u3_xmas* sam_u; + c3_w len_w; + c3_y* buf_y; +} u3_seal; + static void _log_head(u3_xmas_head* hed_u) { u3l_log("-- HEADER --"); + u3l_log("next hop: %u", hed_u->nex_y); u3l_log("protocol: %u", hed_u->pro_y); u3l_log("packet type: %u", hed_u->typ_y); - u3l_log("mug: 0x%05x", (hed_u->mug_w &0xfffff)); + u3l_log("mug: 0x%05x", (hed_u->mug_w & 0xfffff)); u3l_log("hopcount: %u", hed_u->hop_y); u3l_log(""); } @@ -154,9 +213,9 @@ _log_buf(c3_y* buf_y, c3_w len_w) static void _log_peek_pact(u3_xmas_peek_pact* pac_u) { - u3l_log("publisher: %s", u3r_string(u3dc("scot", c3__p, u3i_chubs(2, pac_u->pub_d)))); - u3l_log("path len: %u", pac_u->pat_s); - u3l_log("path: %s", pac_u->pat_c); + u3l_log("publisher: %s", u3r_string(u3dc("scot", c3__p, u3i_chubs(2, pac_u->nam_u.her_d)))); + u3l_log("path len: %u", pac_u->nam_u.pat_s); + u3l_log("path: %s", pac_u->nam_u.pat_c); } static void @@ -179,12 +238,12 @@ _log_pact(u3_xmas_peek_pact* pac_u) free(pip_c); u3z(pip); }*/ - u3_noun pub = u3dc("scot", c3__p, u3i_chubs(2, pac_u->pub_d)); + u3_noun pub = u3dc("scot", c3__p, u3i_chubs(2, pac_u->nam_u.her_d)); c3_c* pub_c = u3r_string(pub); u3l_log("ship: %s", pub_c); free(pub_c); //u3z(pub); - u3l_log("path: %s", pac_u->pat_c); + u3l_log("path: %s", pac_u->nam_u.pat_c); } @@ -350,6 +409,11 @@ u3_xmas_encode_lane(u3_lane lan) { // // // +static void _xmas_free_seal(u3_seal* sel_u) +{ + c3_free(sel_u->buf_y); + c3_free(sel_u); +} // refcounted // static u3_noun _xmas_path_with_fra(c3_c* pat_c, c3_s* fra_s) @@ -382,7 +446,7 @@ _xmas_rout_pact(u3_xmas_pact* pac_u, u3_noun lan) { u3_assert( c3y == u3a_is_cat(val) ); u3_assert( val < 256 ); pac_u->rut_u.typ_y = ROUT_GALAXY; - pac_u->rut_u.imp_u.her_y = val; + pac_u->rut_u.imp_y = val; } else { u3_lane lan_u = u3_xmas_decode_lane(u3k(val)); // if in local-only mode, don't send remote packets @@ -453,7 +517,7 @@ _xmas_head_from_pact(u3_xmas_pact* pac_u, u3_noun her) hed_u.pro_y = XMAS_VER; hed_u.typ_y = pac_u->typ_y; // ship meta is retain, no u3k necessary - hed_u.ran_y = _ship_meta(her); + //hed_u.ran_y = _ship_meta(her); hed_u.hop_y = 0; hed_u.mug_w = 0; @@ -461,23 +525,30 @@ _xmas_head_from_pact(u3_xmas_pact* pac_u, u3_noun her) return hed_u; } -static void -_xmas_sift_head(c3_y buf_y[4], u3_xmas_head* hed_u) +static c3_o +_xmas_sift_head(c3_y buf_y[8], u3_xmas_head* hed_u) { + if ( memcmp(buf_y + 4, &XMAS_COOKIE, XMAS_COOKIE_LEN) ) { + return c3n; + + } c3_w hed_w = _ames_sift_word(buf_y); - hed_u->pro_y = (hed_w >> 0) & 0x7; - hed_u->typ_y = (hed_w >> 3) & 0x3; - hed_u->mug_w = (hed_w >> 5) & 0x3FFF; - hed_u->hop_y = (hed_w >> 25) & 0x1F; + hed_u->nex_y = (hed_w >> 2) & 0x3; + hed_u->pro_y = (hed_w >> 4) & 0x7; + hed_u->typ_y = (hed_w >> 7) & 0x3; + hed_u->hop_y = (hed_w >> 9) & 0x7; + hed_u->mug_w = (hed_w >> 12) & 0xFFFFFF; - if(c3o((hed_u->typ_y == PACT_PEEK), (hed_u->typ_y == PACT_POKE))) { + return c3y; + + /*if(c3o((hed_u->typ_y == PACT_PEEK), (hed_u->typ_y == PACT_POKE))) { hed_u->ran_y = (hed_w >> 30) & 0x3; - } + }*/ } static void -_xmas_etch_head(u3_xmas_head* hed_u, c3_y buf_y[4]) +_xmas_etch_head(u3_xmas_head* hed_u, c3_y buf_y[8]) { if( c3y == XMAS_DEBUG ) { if( hed_u->pro_y > 7 ) { @@ -487,23 +558,25 @@ _xmas_etch_head(u3_xmas_head* hed_u, c3_y buf_y[4]) } c3_o req_o = c3o((hed_u->typ_y == PACT_PEEK), (hed_u->typ_y == PACT_POKE)); c3_y siz_y = req_o ? 5 : 7; - c3_w hed_w = (hed_u->pro_y & 0x7 ) - ^ ((hed_u->typ_y & 0x3 ) << 3) - ^ ((hed_u->mug_w & 0x3FFFF ) << 5) - ^ ((hed_u->hop_y & 0x1F ) << 25); + c3_w hed_w = (hed_u->nex_y & 0x3 ) << 2 + ^ (hed_u->pro_y & 0x7 ) << 4 + ^ ((hed_u->typ_y & 0x3 ) << 7) + ^ ((hed_u->hop_y & 0x7 ) << 9) + ^ ((hed_u->mug_w & 0xFFFFFF ) << 12); // XX: we don't expand hopcount if no request. Correct? - - if ( c3y == req_o ) { + // + /*if ( c3y == req_o ) { hed_w = hed_w ^ ((hed_u->ran_y & 0x3) << 30); - } + }*/ _ames_etch_word(buf_y, hed_w); + memcpy(buf_y + 4, XMAS_COOKIE, XMAS_COOKIE_LEN); } static c3_w _xmas_etch_page_pact(c3_y* buf_y, u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u) { - c3_w cur_w = 0; + /*c3_w cur_w = 0; // hops memcpy(buf_y, pac_u->hop_y, 6); @@ -529,27 +602,66 @@ _xmas_etch_page_pact(c3_y* buf_y, u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u) memcpy(buf_y + cur_w, pac_u->dat_y, 1024); cur_w += 1024; - return cur_w; + return cur_w;*/ + return 0; } static c3_w _xmas_etch_peek_pact(c3_y* buf_y, u3_xmas_peek_pact* pac_u, u3_xmas_head* hed_u, c3_o tag_o) { c3_w cur_w = 0; - c3_y pub_y = 2 << hed_u->ran_y; - _ames_ship_of_chubs(pac_u->pub_d, pub_y, buf_y); - cur_w += pub_y; + u3_xmas_name nam_u = pac_u->nam_u; + u3_xmas_name_meta met_u = nam_u.met_u; + + // TODO: double check reserved bits + c3_y met_y = (met_u.ran_y & 0x3) >> 0 + ^ (met_u.rif_y & 0x3) >> 2 + ^ (met_u.pat_o & 0x1) >> 4 + ^ (met_u.boq_o & 0x1) >> 5 + ^ (met_u.fra_y & 0x3) >> 6; + + buf_y[cur_w] = met_y; + + + //shift + cur_w++; + c3_y her_y = 1 << (met_u.ran_y+1); + _ames_ship_of_chubs(nam_u.her_d, her_y, buf_y + cur_w); + cur_w += her_y; + + // rift + c3_y rif_y = met_u.rif_y + 1; + for ( int i = 0; i < rif_y; i++) { + buf_y[cur_w] = (nam_u.rif_w >> (8*i)) & 0xff; + cur_w++; + } - if( tag_o == c3y ) { - buf_y[cur_w] = 0; // Tag byte - cur_w += 1; + // path length + c3_y pat_y = 1 >> met_u.pat_o; + for ( int i = 0; i < pat_y; i++ ) { + buf_y[cur_w] = (nam_u.pat_s >> (8*i)) & 0xff; + cur_w++; } - _ames_etch_short(buf_y + cur_w, pac_u->pat_s); - cur_w += 2; + // path + memcpy(buf_y + cur_w, nam_u.pat_c, nam_u.pat_s); + cur_w += nam_u.pat_s; - memcpy(buf_y + cur_w, pac_u->pat_c, pac_u->pat_s + 1); - return cur_w + 328; + + // possible bloq size + if ( 1 == met_u.boq_o ) { + buf_y[cur_w] = nam_u.boq_y; + cur_w++; + } + + c3_y fra_y = met_u.fra_y + 1; + for( int i = 0; i < met_u.fra_y; i++ ) { + buf_y[cur_w] = (nam_u.fra_w >> (8*i)) & 0xff; + cur_w++; + } + + return cur_w; + //_ames_etch_short(buf_y + cur_w, &met_s); } static c3_w @@ -578,7 +690,7 @@ _xmas_etch_pact(c3_y* buf_y, u3_xmas_pact* pac_u, u3_noun her) c3_w cur_w = 0; u3_xmas_head hed_u = _xmas_head_from_pact(pac_u, her); _xmas_etch_head(&hed_u, buf_y + cur_w); - cur_w += 4; + cur_w += 8; switch ( pac_u->typ_y ) { @@ -602,8 +714,8 @@ _xmas_etch_pact(c3_y* buf_y, u3_xmas_pact* pac_u, u3_noun her) static void _xmas_send_cb(uv_udp_send_t* req_u, c3_i sas_i) { - u3_xmas_pact* pac_u = (u3_xmas_pact*)req_u; - u3_xmas* sam_u = pac_u->sam_u; + u3_seal* sel_u = (u3_seal*)req_u; + u3_xmas* sam_u = sel_u->sam_u; if ( sas_i ) { u3l_log("xmas: send fail_async: %s", uv_strerror(sas_i)); @@ -613,50 +725,33 @@ _xmas_send_cb(uv_udp_send_t* req_u, c3_i sas_i) //sam_u->fig_u.net_o = c3y; } - _xmas_pact_free(pac_u); + _xmas_free_seal(sel_u); } -static void _xmas_send(u3_xmas_pact* pac_u) +static void _xmas_send_buf(u3_xmas* sam_u, u3_lane lan_u, c3_y* buf_y, c3_w len_w) { - u3_xmas* sam_u = pac_u->sam_u; - u3l_log("xmas sending"); - + u3_seal* sel_u = c3_calloc(sizeof(*sel_u)); + sel_u->buf_y = buf_y; + sel_u->len_w = len_w; + sel_u->sam_u = sam_u; struct sockaddr_in add_u; memset(&add_u, 0, sizeof(add_u)); add_u.sin_family = AF_INET; - c3_s por_s; - c3_w pip_w; - if( pac_u->rut_u.typ_y == ROUT_GALAXY ) { - por_s = _ames_czar_port(pac_u->rut_u.imp_u.her_y); - pip_w = 0x7f000001; - } - else { - pip_w = c3n == u3_Host.ops_u.net ? pac_u->rut_u.lan_u.pip_w : 0x7f000001; - por_s = pac_u->rut_u.lan_u.por_s; - } - u3l_log("%x,%u", pip_w, por_s); - + c3_w pip_w = c3n == u3_Host.ops_u.net ? lan_u.pip_w : 0x7f000001; + c3_s por_s = lan_u.por_s; add_u.sin_addr.s_addr = htonl(pip_w); add_u.sin_port = htons(por_s); - //u3l_log("_ames_send %s %u", _str_typ(pac_u->typ_y), - // pac_u->rut_u.lan_u.por_s); - c3_y* buf_y = c3_calloc(PACT_SIZE); - c3_w siz_w; - { - u3_noun who = 0; - if( pac_u->typ_y == PACT_PEEK ) { - who = u3i_chubs(2, pac_u->pek_u.pub_d); - } - - siz_w = _xmas_etch_pact(buf_y, pac_u, who); - } +#ifdef XMAS_DEBUG + c3_c* sip_c = inet_ntoa(add_u.sin_addr); + u3l_log("xmas: sending packet (%s,%u)", sip_c, por_s); +#endif - uv_buf_t buf_u = uv_buf_init((c3_c*)buf_y, siz_w); + uv_buf_t buf_u = uv_buf_init((c3_c*)buf_y, len_w); - c3_i sas_i = uv_udp_send(&pac_u->snd_u, + c3_i sas_i = uv_udp_send(&sel_u->snd_u, &sam_u->wax_u, &buf_u, 1, (const struct sockaddr*)&add_u, @@ -667,9 +762,27 @@ static void _xmas_send(u3_xmas_pact* pac_u) /*if ( c3y == sam_u->fig_u.net_o ) { //sam_u->fig_u.net_o = c3n; }*/ + _xmas_free_seal(sel_u); + } +} - _xmas_pact_free(pac_u); +static void _xmas_send(u3_xmas_pact* pac_u) +{ + u3_xmas* sam_u = pac_u->sam_u; + u3l_log("xmas sending"); + + + //u3l_log("_ames_send %s %u", _str_typ(pac_u->typ_y), + // pac_u->rut_u.lan_u.por_s); + c3_y* buf_y = c3_calloc(PACT_SIZE); + c3_w siz_w; + { + u3_noun who = 0; + + siz_w = _xmas_etch_pact(buf_y, pac_u, who); } + + _xmas_send_buf(sam_u, pac_u->rut_u.lan_u, buf_y, siz_w); } static c3_o @@ -695,28 +808,84 @@ _xmas_rout_pacs(u3_xmas_pact* pac_u, u3_noun las) } static void -_xmas_ef_peek(u3_xmas* sam_u, u3_noun her, u3_noun las, u3_noun pat) +_xmas_ef_pact(u3_xmas* sam_u, u3_noun las, u3_noun pac) { + c3_w len_w = u3r_met(3, pac); + c3_y* buf_y = c3_calloc(len_w); + u3r_bytes(0, len_w, buf_y, pac); + + c3_o suc_o = c3n; + u3_noun lan, t = las; + while ( t != u3_nul ) { + u3x_cell(t, &lan, &t); + // if ( c3n == u3r_cell(t, &lan, &t) ) { + // break; + // } + u3l_log("xmas_send"); + u3_noun tag, val; + u3x_cell(lan, &tag, &val); + u3_assert( (c3y == tag) || (c3n == tag) ); + if ( c3y == tag ) { + u3_assert( c3y == u3a_is_cat(val) ); + u3_assert( val < 256 ); + if ( c3n == u3_Host.ops_u.net ) { + u3_lane lan_u; + lan_u.pip_w = 0x7f000001 ; + lan_u.por_s = _ames_czar_port(val); + _xmas_send_buf(sam_u, lan_u, buf_y, len_w); + } else { + // get galaxy lane or add pending + } + } else { + u3_lane lan_u = u3_xmas_decode_lane(u3k(val)); + // if in local-only mode, don't send remote packets + // + if ( (c3n == u3_Host.ops_u.net) && (0x7f000001 != lan_u.pip_w) ) { + suc_o = c3n; + } + // if the lane is uninterpretable, silently drop the packet + // + else if ( 0 == lan_u.por_s ) { + if ( u3C.wag_w & u3o_verbose ) { + u3l_log("ames: inscrutable lane"); + } + suc_o = c3n; + } else { + _xmas_send_buf(sam_u, lan_u, buf_y, len_w); + } + } + } + u3z(las); u3z(pac); +} + +static void +_xmas_ef_peek(u3_xmas* sam_u, u3_noun las, u3_noun who, u3_noun pat, u3_noun cop) +{ + u3_noun boq, fra, her, rif; + u3x_cell(who, &her, &rif); + u3x_cell(cop, &boq, &fra); if ( c3n == sam_u->car_u.liv_o ) { u3l_log("xmas: not yet live, dropping outbound\r"); + u3z(pat); } else { u3_xmas_pact* pac_u = c3_calloc(sizeof(*pac_u)); pac_u->typ_y = PACT_PEEK; pac_u->sam_u = sam_u; { u3_noun pas = u3do("spat", pat); - pac_u->pek_u.pat_c = u3r_string(pas); - pac_u->pek_u.pat_s = strlen(pac_u->pek_u.pat_c); + pac_u->pek_u.nam_u.pat_c = u3r_string(pas); + pac_u->pek_u.nam_u.pat_s = strlen(pac_u->pek_u.nam_u.pat_c); u3z(pas); } - u3r_chubs(0, 2, pac_u->pek_u.pub_d, her); + u3r_chubs(0, 2, pac_u->pek_u.nam_u.her_d, her); + pac_u->pek_u.nam_u.rif_w = u3r_word(0, rif); + pac_u->pek_u.nam_u.boq_y = u3r_byte(0, boq); - if( 384 < pac_u->pek_u.pat_s ) { + if( 384 < pac_u->pek_u.nam_u.pat_s ) { u3l_log("xmas: path in peek too long"); _xmas_pact_free(pac_u); } else { - c3_o rut_o = _xmas_rout_pacs(pac_u, u3k(las)); if ( c3n == rut_o ) { @@ -725,7 +894,7 @@ _xmas_ef_peek(u3_xmas* sam_u, u3_noun her, u3_noun las, u3_noun pat) } } // do not need to lose pat because moved in spat call - u3z(her); u3z(las); + u3z(who); u3z(cop); } @@ -737,13 +906,22 @@ static c3_o _xmas_kick(u3_xmas* sam_u, u3_noun tag, u3_noun dat) default: { ret_o = c3n; } break; + case c3__pact: { + u3_noun las, pac; + if ( c3n == u3r_cell(dat, &las, &pac) ) { + ret_o = c3n; + } else { + _xmas_ef_pact(sam_u, u3k(las), u3k(pac)); + ret_o = c3y; + } + } break; // case c3__peek: { u3_noun lan, her, pat; if ( c3n == u3r_trel(dat, &her, &lan, &pat) ) { ret_o = c3n; } else { - _xmas_ef_peek(sam_u, u3k(her), u3k(lan), u3k(pat)); + //_xmas_ef_peek(sam_u, u3k(her), u3k(lan), u3k(pat)); ret_o = c3y; } } break; @@ -765,10 +943,10 @@ static c3_o _xmas_io_kick(u3_auto* car_u, u3_noun wir, u3_noun cad) if( c3n == u3r_cell(cad, &tag, &dat) ) { ret_o = c3n; + } else { + ret_o = _xmas_kick(sam_u, u3k(tag), u3k(dat)); } - ret_o = _xmas_kick(sam_u, u3k(tag), u3k(dat)); - u3z(wir); u3z(cad); return ret_o; } @@ -794,7 +972,7 @@ _xmas_exit_cb(uv_handle_t* had_u) u3s_cue_xeno_done(sam_u->sil_u); ur_cue_test_done(sam_u->tes_u); - u3h_free(sam_u->lax_p); + u3h_free(sam_u->her_p); u3h_free(sam_u->req_p); c3_free(sam_u); } @@ -811,7 +989,7 @@ _xmas_sift_page_pact(u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, { c3_w cur_w = 0; - // next hop + /*// next hop memcpy(pac_u->hop_y, buf_y, 6); cur_w += 6; @@ -836,37 +1014,72 @@ _xmas_sift_page_pact(u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, c3_w dat_w = 1024; // len_w - cur_w; pac_u->dat_y = c3_calloc(dat_w); memcpy(pac_u->dat_y, buf_y + cur_w, dat_w); + */ } static c3_w -_xmas_sift_name(u3_xmas_name* nam_u, c3_y siz_y, c3_y* buf_y, c3_w len_w) +_xmas_sift_name(u3_xmas_name* nam_u, c3_y* buf_y) { - return 0; -} - - -static c3_w -_xmas_sift_peek_pact(u3_xmas_peek_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, c3_w len_w, c3_o tag_o) -{ - c3_y pub_y = 2 << hed_u->ran_y; // Publisher size c3_w cur_w = 0; - _ames_ship_to_chubs(pac_u->pub_d, pub_y, buf_y); - cur_w += pub_y; + c3_w met_y = buf_y[cur_w]; + nam_u->met_u.ran_y = (met_y >> 0) & 0x3; + nam_u->met_u.rif_y = (met_y >> 2) & 0x3; + nam_u->met_u.pat_o = (met_y >> 4) & 0x1; + nam_u->met_u.boq_o = (met_y >> 5) & 0x1; + nam_u->met_u.fra_y = (met_y >> 6) & 0x3; + cur_w += 1; + + c3_y her_y = 2 << nam_u->met_u.ran_y; + _ames_ship_to_chubs(nam_u->her_d, her_y, buf_y + cur_w); + cur_w += her_y; + + c3_y rif_y = 2 << nam_u->met_u.rif_y; + //SIFT_VAR(nam_u->rif_w, (buf_y + cur_w), rif_y); + for( int i = 0; i < rif_y; i++ ) { + nam_u->rif_w |= (buf_y[cur_w] << (8*i)); + cur_w++; + } + + c3_y pat_y = 2 << nam_u->met_u.pat_o; + nam_u->pat_s = 0; + for ( int i = 0; i < pat_y; i++ ) { + nam_u->pat_s |= (buf_y[cur_w] << (8*i)); + cur_w++; + } + + nam_u->pat_c = c3_calloc(nam_u->pat_s + 1); // unix string for ease of manipulation + memcpy(nam_u->pat_c, buf_y + cur_w, nam_u->pat_s); + nam_u->pat_c[nam_u->pat_s] = 0; + cur_w += nam_u->pat_s; - if ( c3y == tag_o ) { - cur_w += 1; // tag byte + if ( 1 == nam_u->met_u.boq_o ) { + nam_u->boq_y = buf_y[cur_w]; + cur_w++; + } else { + nam_u->boq_y = 13; } - pac_u->pat_s = _ames_sift_short(buf_y + cur_w); // str len - cur_w += 2; + c3_y fra_y = 2 << nam_u->met_u.fra_y; + nam_u->fra_w = 0; + for( int i = 0; i < fra_y; i++ ) { + nam_u->fra_w |= (buf_y[cur_w] << (8*i)); + + } - pac_u->pat_c = c3_calloc(pac_u->pat_s + 1); - memcpy(pac_u->pat_c, buf_y + cur_w, pac_u->pat_s + 1); - pac_u->pat_c[pac_u->pat_s] = '\0'; - cur_w += 328; return cur_w; } + +static c3_w +_xmas_sift_peek_pact(u3_xmas_peek_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, c3_w len_w, c3_o tag_o) +{ + c3_w siz_w = _xmas_sift_name(&pac_u->nam_u, buf_y); + /*if (siz_w > len_w ) { + u3l_log("xmas: buffer overrun (FIXME)"); + u3m_bail(c3__foul); + }*/ +} + static void _xmas_sift_poke_pact(u3_xmas_poke_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, c3_w len_w) { @@ -904,6 +1117,50 @@ _xmas_sift_pact(u3_xmas_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, c3_w len_ } } +/* + * RETAIN + */ +static u3_weak +_xmas_get_peer(u3_xmas* sam_u, u3_noun her) +{ + return u3h_git(sam_u->her_p, her); +} + +/* + * RETAIN + */ +static void +_xmas_put_sponsee(u3_xmas* sam_u, u3_noun her, u3_noun las) +{ + u3_noun val = u3nc(u3_nul, u3k(las)); + u3h_put(sam_u->her_p, her, val); + u3z(val); +} + +/* + * RETAIN + */ +static c3_o +_xmas_add_galaxy_pend(u3_xmas* sam_u, u3_noun her, u3_noun pen) +{ + u3_weak old = u3h_git(sam_u->her_p, her); + u3_noun pes = u3_nul; + u3_noun wat; + if ( u3_none != old ) { + if ( u3h(old) == XMAS_CZAR ) { + u3x_cell(u3t(old), &pes, &wat); + u3z(old); + } else { + u3l_log("xmas: attempted to resolve resolved czar"); + u3z(old); + return c3n; + } + } + u3_noun val = u3nc(u3nc(u3k(pen), u3k(pes)), c3y); + u3h_put(sam_u->her_p, her, val); + u3z(val); + return _(wat); +} /* * RETAIN @@ -928,10 +1185,38 @@ _xmas_put_cache(u3_xmas* sam_u, u3_noun pax, u3_noun val) u3h_put(sam_u->pen_p, pax, val); } +/* _xmas_czar(): add packet to queue, possibly begin DNS resolution + */ +static void +_xmas_czar(u3_xmas_pact* pac_u) +{ + u3_xmas* sam_u = pac_u->sam_u; +#ifdef XMAS_DEBUG + if ( pac_u->typ_y != PACT_PEEK ) { + u3l_log("xmas: attempted to resolve galaxy for packet that was not peek"); + u3m_bail(c3__oops); + return; + } +#endif + + u3_noun pat = u3i_string(pac_u->pek_u.nam_u.pat_c); + + c3_y her_y = pac_u->rut_u.imp_y; + + u3_weak her = _xmas_get_peer(sam_u, her_y); + + c3_o lok_o = _xmas_add_galaxy_pend(sam_u, her_y, pat); + + + // TODO: actually resolve DNS + u3z(pat); +} + static void _xmas_try_forward(u3_xmas_pact* pac_u, u3_noun fra, u3_noun hit) { + u3l_log(""); u3l_log("stubbed forwarding"); } @@ -941,15 +1226,13 @@ _xmas_respond(u3_xmas_pact* req_u, u3_xmas_pact* res_u, u3_noun hit, c3_s fra_s) res_u->typ_y = PACT_PAGE; res_u->sam_u = req_u->sam_u; - res_u->pag_u = (u3_xmas_page_pact) { - .pat_s = req_u->pek_u.pat_s, - .tot_w = u3r_word(0, u3k(u3h(hit))), - .dat_y = c3_calloc(FINE_FRAG), - }; - res_u->pag_u.pat_c = c3_calloc(req_u->pek_u.pat_s + 1); + + memcpy(&res_u->pag_u.nam_u, &req_u->pek_u.nam_u, sizeof(u3_xmas_name)); + + /*res_u->pag_u.pat_c = c3_calloc(req_u->pek_u.pat_s + 1); res_u->pag_u.pat_c[req_u->pek_u.pat_s] = '\0'; memcpy(res_u->pag_u.pat_c, req_u->pek_u.pat_c, req_u->pek_u.pat_s); - u3r_bytes(0, 1024, res_u->pag_u.dat_y, u3dc("snag", u3i_word(fra_s), u3k(u3t(hit)))); + u3r_bytes(0, 1024, res_u->pag_u.dat_y, u3dc("snag", u3i_word(fra_s), u3k(u3t(hit))));*/ u3z(hit); } @@ -958,7 +1241,7 @@ _xmas_serve_cache_hit(u3_xmas_pact* req_u, u3_lane lan_u, c3_s fra_s, u3_noun hi { u3l_log("serving cache hit"); c3_d her_d[2]; - memcpy(her_d, req_u->pek_u.pub_d, 2); + memcpy(her_d, req_u->pek_u.nam_u.her_d, 2); c3_d our_d[2]; memcpy(our_d, req_u->sam_u->pir_u->who_d, 2); if ( (her_d[0] != our_d[0]) @@ -993,7 +1276,7 @@ _xmas_page_scry_cb(void* vod_p, u3_noun nun) u3_xmas_pact* pac_u = vod_p; u3_xmas* sam_u = pac_u->sam_u; c3_s fra_s; - u3_noun pax = _xmas_path_with_fra(pac_u->pek_u.pat_c, &fra_s); + u3_noun pax = _xmas_path_with_fra(pac_u->pek_u.nam_u.pat_c, &fra_s); u3_weak hit = u3r_at(7, nun); if ( u3_none == hit ) { @@ -1009,7 +1292,7 @@ _xmas_page_scry_cb(void* vod_p, u3_noun nun) u3_noun tag; u3_noun dat; u3x_cell(u3k(old), &tag, &dat); - if ( XMAS_WAIT == tag ) { + if ( XMAS_SCRY == tag ) { u3_xmas_pact* res_u = c3_calloc(sizeof(*res_u)); _xmas_respond(pac_u, res_u, u3k(hit), fra_s); @@ -1034,29 +1317,41 @@ static void _xmas_hear_page(u3_xmas_pact* pac_u, u3_lane lan_u) { u3_xmas* sam_u = pac_u->sam_u; - u3_noun wir = u3nc(c3__xmas, u3_nul); - u3_noun pat = u3do("stab", u3i_string(pac_u->pag_u.pat_c)); - u3_noun mess = u3nq( - pat, - u3i_word(pac_u->pag_u.tot_w), - u3i_bytes(96, pac_u->pag_u.aut_y), - u3i_bytes(1024, pac_u->pag_u.dat_y) - ); - u3_noun cad = u3nt(c3__mess, c3__page, mess); - - u3_auto_peer( - u3_auto_plan(&sam_u->car_u, - u3_ovum_init(0, c3__x, wir, cad)), - 0, 0, _xmas_hear_bail); + c3_s fra_s; + u3_noun pat = _xmas_path_with_fra(pac_u->pag_u.nam_u.pat_c, &fra_s); + + u3_weak cac = _xmas_get_cache(sam_u, pat); + + if ( u3_none != cac ) { + u3_noun tag, dat; + u3x_cell(cac, &tag, &dat); + if ( tag == XMAS_SCRY ) { + _xmas_rout_pacs(pac_u, u3k(u3t(dat))); + } + u3z(cac); + } else { + u3_noun mess = u3nq( + pat, + u3i_word(pac_u->pag_u.mat_u.tot_w), + u3i_bytes(256, pac_u->pag_u.mat_u.aut_y), + u3i_bytes(pac_u->pag_u.mat_u.len_d, pac_u->pag_u.mat_u.fra_y) + ); + u3_noun cad = u3nt(c3__mess, c3__page, mess); + + u3_auto_peer( + u3_auto_plan(&sam_u->car_u, + u3_ovum_init(0, c3__x, wir, cad)), + 0, 0, _xmas_hear_bail); + } // no lose required because all nouns are moved } static void _xmas_add_lane_to_cache(u3_xmas* sam_u, u3_noun las, u3_noun pax, u3_lane lan_u) { - u3_noun hit = u3nq(XMAS_WAIT, + u3_noun hit = u3nq(XMAS_SCRY, _xmas_get_now(), u3nc(c3n, u3_xmas_encode_lane(lan_u)), u3k(las)); @@ -1071,14 +1366,13 @@ _xmas_hear_peek(u3_xmas_pact* pac_u, u3_lane lan_u) u3l_log("%hu", lan_u.por_s); u3_xmas* sam_u = pac_u->sam_u; c3_s fra_s; - u3_noun pax = _xmas_path_with_fra(pac_u->pek_u.pat_c, &fra_s); - u3l_log("fra: %s", fra_s); + u3_noun pax = _xmas_path_with_fra(pac_u->pek_u.nam_u.pat_c, &fra_s); u3_weak hit = _xmas_get_cache(sam_u, u3k(pax)); if ( u3_none != hit ) { u3_noun tag, dat; u3x_cell(u3k(hit), &tag, &dat); u3m_p("dat", dat); - if ( tag == XMAS_WAIT ) { + if ( tag == XMAS_SCRY ) { _xmas_add_lane_to_cache(sam_u, u3k(u3t(dat)), u3k(pax), lan_u); } else if ( tag == XMAS_ITEM ) { u3l_log("item hit"); @@ -1119,16 +1413,17 @@ _xmas_hear(u3_xmas* sam_u, pac_u->sam_u = sam_u; u3_xmas_head hed_u; _xmas_sift_head(cur_y, &hed_u); - _xmas_sift_pact(pac_u, &hed_u, hun_y + 4, (len_w - 4)); + _log_head(&hed_u); + _xmas_sift_pact(pac_u, &hed_u, hun_y + 8, (len_w - 8)); switch ( pac_u->typ_y ) { case PACT_PEEK: { _xmas_hear_peek(pac_u, *lan_u); } break; - default: { case PACT_PAGE: { _xmas_hear_page(pac_u, *lan_u); } break; + default: { u3l_log("xmas: unimplemented packet type %u", pac_u->typ_y); } break; } @@ -1179,6 +1474,7 @@ static void _xmas_io_talk(u3_auto* car_u) { u3_xmas* sam_u = (u3_xmas*)car_u; + sam_u->dns_c = "urbit.org"; // TODO: receive turf //_xmas_io_start(sam_u); u3_noun who = u3i_chubs(2, sam_u->pir_u->who_d); u3_noun rac = u3do("clan:title", u3k(who)); @@ -1252,7 +1548,7 @@ u3_xmas_io_init(u3_pier* pir_u) u3_xmas* sam_u = c3_calloc(sizeof(*sam_u)); sam_u->pir_u = pir_u; - sam_u->lax_p = u3h_new_cache(100000); + sam_u->her_p = u3h_new_cache(100000); sam_u->req_p = u3h_new_cache(100000); u3_assert( !uv_udp_init(u3L, &sam_u->wax_u) ); diff --git a/pkg/vere/pier.c b/pkg/vere/pier.c index 907e33c319..1065b59623 100644 --- a/pkg/vere/pier.c +++ b/pkg/vere/pier.c @@ -815,7 +815,7 @@ _pier_wyrd_card(u3_pier* pir_u) u3_noun kel = u3nl(u3nc(c3__zuse, VERE_ZUSE), // XX from both king and serf? u3nc(c3__lull, VERE_LULL), // XX from both king and serf? u3nc(c3__arvo, 237), // XX from both king and serf? - u3nc(c3__hoon, 139), // god_u->hon_y + u3nc(c3__hoon, 138), // god_u->hon_y u3nc(c3__nock, 4), // god_u->noc_y u3_none); u3_noun wir = u3nc(c3__arvo, u3_nul); diff --git a/pkg/vere/serf.c b/pkg/vere/serf.c index cec8bece55..a99544b447 100644 --- a/pkg/vere/serf.c +++ b/pkg/vere/serf.c @@ -1054,7 +1054,7 @@ u3_serf_init(u3_serf* sef_u) { c3_w pro_w = 1; - c3_y hon_y = 139; + c3_y hon_y = 138; c3_y noc_y = 4; u3_noun ver = u3nt(pro_w, hon_y, noc_y); From b4aefe1b0e58bace0d67424a1ed365cbb3545814 Mon Sep 17 00:00:00 2001 From: Liam Fitzgerald Date: Thu, 11 Jan 2024 12:47:31 -0500 Subject: [PATCH 011/430] wip --- pkg/vere/io/xmas.c | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c index 5fe5289177..841d8dd3ca 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/xmas.c @@ -1078,6 +1078,7 @@ _xmas_sift_peek_pact(u3_xmas_peek_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, u3l_log("xmas: buffer overrun (FIXME)"); u3m_bail(c3__foul); }*/ + return siz_w; } static void From b1790fe038733e0ec847d10724b0b41b21d95d23 Mon Sep 17 00:00:00 2001 From: Liam Fitzgerald Date: Mon, 22 Jan 2024 13:35:13 -0500 Subject: [PATCH 012/430] wip --- ' | 1604 ++++++++++++++++++++++++++++++++++++++ pkg/vere/io/xmas.c | 208 +++-- pkg/vere/io/xmas/cubic.c | 72 +- 3 files changed, 1788 insertions(+), 96 deletions(-) create mode 100644 ' diff --git a/' b/' new file mode 100644 index 0000000000..9041d3e81c --- /dev/null +++ b/' @@ -0,0 +1,1604 @@ +/// @file + +#include "vere.h" + +#include "noun.h" +#include "ur.h" +#include +#include +#include + +#define XMAS_DEBUG c3y +#define XMAS_VER 1 +#define FINE_PAGE 4096 // packets per page +#define FINE_FRAG 1024 // bytes per fragment packet +#define FINE_PATH_MAX 384 // longest allowed scry path +#define HEAD_SIZE 4 // header size in bytes +#define PACT_SIZE 1472 + +// pending interest sentinels +#define XMAS_ITEM 1 // cached item +#define XMAS_SCRY 2 // waiting on scry +// routing table sentinels +#define XMAS_CZAR 1 // pending dns lookup +#define XMAS_ROUT 2 // have route +// +#define XMAS_COOKIE_LEN 4 +static c3_y XMAS_COOKIE[4] = { 0x5e, 0x1d, 0xad, 0x51 }; + + +#define SIFT_VAR(dest, src, len) dest = 0; for(int i = 0; i < len; i++ ) { dest |= ((src + i) >> (8*i)); } + +/* _u3_xmas: next generation networking + */ +typedef struct _u3_xmas { + u3_auto car_u; + u3_pier* pir_u; + union { + uv_udp_t wax_u; + uv_handle_t had_u; + }; + c3_l sev_l; + ur_cue_test_t* tes_u; // cue-test handle + u3_cue_xeno* sil_u; // cue handle + u3p(u3h_root) her_p; // + u3p(u3h_root) pen_p; + c3_w imp_w[256]; + time_t imp_t[256]; // imperial IP timestamps + c3_o imp_o[256]; // imperial print status + c3_c* dns_c; // + // jj + u3p(u3h_root) req_p; +} u3_xmas; + +typedef enum _u3_xmas_ptag { + PACT_RESV = 0, + PACT_PEEK = 1, + PACT_PAGE = 2, + PACT_POKE = 3, +} u3_xmas_ptag; + +typedef enum _u3_xmas_rout_tag { + ROUT_GALAXY = 0, + ROUT_OTHER = 1 +} u3_xmas_rout_tag; + +typedef enum _u3_xmas_nexh { + NEXH_NONE = 0, + NEXH_SBYT = 1, + NEXH_ONLY = 2, + NEXH_MANY = 3 +} u3_xmas_nexh; + +typedef struct _u3_xmas_name_meta { + // reserved (2 bits) + c3_y ran_y; // rank (2 bits) + c3_y rif_y; // rift-len (2 bits) + c3_o pat_o; // path length length (1bit) + c3_o boq_o; // 0b1 if custom, 0b0 implicitly 13 (1bit) + c3_o fra_y; // fragment number length (2bit) +} u3_xmas_name_meta; + +typedef struct _u3_xmas_name { + u3_xmas_name_meta met_u; + c3_d her_d[2]; + c3_w rif_w; + c3_s pat_s; + c3_c* pat_c; + c3_y boq_y; + c3_w fra_w; +} u3_xmas_name; + +typedef struct _u3_xmas_rout { + u3_xmas_rout_tag typ_y; // type tag + union { + c3_y imp_y; + u3_lane lan_u; + }; +} u3_xmas_rout; + +typedef struct _u3_xmas_head { + u3_xmas_nexh nex_y; // next-hop + c3_y pro_y; // protocol version + u3_xmas_ptag typ_y; // packet type + c3_y hop_y; // hopcount + c3_w mug_w; // mug checksum +} u3_xmas_head; + +// +// +$ cache +// [%rout lanes=(list lanes)] +// [%pending pacs=(list pact)] + + +typedef struct _u3_xmas_peek_pact { + u3_xmas_name nam_u; +} u3_xmas_peek_pact; + +typedef struct _u3_xmas_page_meat_meta { + c3_y tot_y; // total fragments length (2bit) + c3_o aut_o; // authentication len len (1bit) + c3_y len_y; // fragment length (5bit) +} u3_xmas_page_meat_meta; + +typedef enum _u3_xmas_auth_type { + AUTH_SIG = 1, + AUTH_DIG = 2, + AUTH_HMAC = 3, +} u3_xmas_auth_type; + +typedef struct _u3_xmas_page_meat { + u3_xmas_page_meat_meta met_u; // metadata + c3_w tot_w; // total fragments + c3_y aul_y; // authentication length + u3_xmas_auth_type ayp_y; // authentication type + c3_y aut_y[255]; // authentication + c3_d len_d; // fragment length + c3_y* fra_y; // fragment +} u3_xmas_page_meat; + +typedef struct _u3_xmas_page_pact { + u3_xmas_name nam_u; + u3_xmas_page_meat mat_u; +} u3_xmas_page_pact; + +typedef struct _u3_xmas_poke_pact { + u3_xmas_peek_pact* pek_u; + c3_w tot_w; + c3_y aut_y[96]; // heap allocate?? + c3_y* dat_y; +} u3_xmas_poke_pact; + +typedef struct _u3_xmas_pact { + u3_xmas_ptag typ_y; + uv_udp_send_t snd_u; + struct _u3_xmas* sam_u; + u3_xmas_rout rut_u; + union { + u3_xmas_poke_pact pok_u; + u3_xmas_page_pact pag_u; + u3_xmas_peek_pact pek_u; + }; +} u3_xmas_pact; + +typedef enum _u3_xmas_ctag { + CACE_WAIT = 1, + CACE_ITEM = 2, +} u3_xmas_ctag; + + + +typedef struct _u3_cace_enty { + u3_xmas_ctag typ_y; + union { + // u03_xmas_cace_wait wat_u; + // u3_xmas_cace_item res_u; + }; +} u3_cace_enty; + +typedef struct _u3_seal { + uv_udp_send_t snd_u; // udp send request + u3_xmas* sam_u; + c3_w len_w; + c3_y* buf_y; +} u3_seal; + + +static void +_log_head(u3_xmas_head* hed_u) +{ + u3l_log("-- HEADER --"); + u3l_log("next hop: %u", hed_u->nex_y); + u3l_log("protocol: %u", hed_u->pro_y); + u3l_log("packet type: %u", hed_u->typ_y); + u3l_log("mug: 0x%05x", (hed_u->mug_w & 0xfffff)); + u3l_log("hopcount: %u", hed_u->hop_y); + u3l_log(""); +} + +static void +_log_buf(c3_y* buf_y, c3_w len_w) +{ + c3_w siz_w = 2*len_w + 1; + c3_c* res_c = c3_calloc(siz_w); + c3_w cur_w = 0; + c3_c tmp_c[3]; + for(c3_w idx_w = 0; idx_w < len_w; idx_w++ ) { + snprintf(res_c + (2*idx_w), siz_w - (2*idx_w), "%02x", buf_y[idx_w]); + } + u3l_log("buffer: %s", res_c); + free(res_c); +} + +static void +_log_peek_pact(u3_xmas_peek_pact* pac_u) +{ + u3l_log("publisher: %s", u3r_string(u3dc("scot", c3__p, u3i_chubs(2, pac_u->nam_u.her_d)))); + u3l_log("path len: %u", pac_u->nam_u.pat_s); + u3l_log("path: %s", pac_u->nam_u.pat_c); +} + +static void +_log_pact(u3_xmas_peek_pact* pac_u) +{ + u3l_log("-- REQUEST PACKET --"); + /*if( ROUT_GALAXY == pac_u->rut_u.typ_y ) { + u3l_log("-- GALAXY ROUTE --"); + u3_noun her = u3dc("scot", c3__p, u3i_word(pac_u->rut_u.imp_u.her_y)); + c3_c* her_c = u3r_string(her); + u3l_log("ship lane: %s", her_c); + free(her_c); + u3z(her); + } + else { + u3l_log("-- OTHER ROUTE --"); + u3_noun pip = u3dc("scot", c3__if, u3i_word(pac_u->rut_u.lan_u.pip_w)); + c3_c* pip_c = u3r_string(pip); + u3l_log("IP: (%s) port (%u)", pip_c, pac_u->rut_u.lan_u.por_s); + free(pip_c); + u3z(pip); + }*/ + u3_noun pub = u3dc("scot", c3__p, u3i_chubs(2, pac_u->nam_u.her_d)); + c3_c* pub_c = u3r_string(pub); + u3l_log("ship: %s", pub_c); + free(pub_c); + //u3z(pub); + u3l_log("path: %s", pac_u->nam_u.pat_c); +} + + +// cut and pasted from ames.c +// +static void +_ames_etch_short(c3_y buf_y[2], c3_s sot_s) +{ + buf_y[0] = sot_s & 0xff; + buf_y[1] = (sot_s >> 8) & 0xff; +} + +static void +_ames_etch_word(c3_y buf_y[4], c3_w wod_w) +{ + buf_y[0] = wod_w & 0xff; + buf_y[1] = (wod_w >> 8) & 0xff; + buf_y[2] = (wod_w >> 16) & 0xff; + buf_y[3] = (wod_w >> 24) & 0xff; +} + +/* _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_alloc(): libuv buffer allocator. +*/ +static void +_ames_alloc(uv_handle_t* had_u, + size_t len_i, + uv_buf_t* buf + ) +{ + // we allocate 2K, which gives us plenty of space + // for a single ames packet (max size 1060 bytes) + // + void* ptr_v = c3_malloc(4096); + *buf = uv_buf_init(ptr_v, 4096); +} + +static inline c3_s +_ames_sift_short(c3_y buf_y[2]) +{ + return (buf_y[1] << 8 | buf_y[0]); +} + +static inline c3_w +_ames_sift_word(c3_y buf_y[4]) +{ + return (buf_y[3] << 24 | buf_y[2] << 16 | buf_y[1] << 8 | buf_y[0]); +} + +/* u3_xmas_decode_lane(): deserialize noun to lane; 0.0.0.0:0 if invalid +*/ +static u3_lane +u3_xmas_decode_lane(u3_atom lan) { + u3_lane lan_u; + c3_d lan_d; + + if ( c3n == u3r_safe_chub(lan, &lan_d) || (lan_d >> 48) != 0 ) { + return (u3_lane){0, 0}; + } + + u3z(lan); + + lan_u.pip_w = (c3_w)lan_d; + lan_u.por_s = (c3_s)(lan_d >> 32); + // convert incoming localhost to outgoing localhost + // + lan_u.pip_w = ( 0 == lan_u.pip_w ) ? 0x7f000001 : lan_u.pip_w; + + return lan_u; +} + +/* _ames_chub_bytes(): c3_y[8] to c3_d +** XX factor out, deduplicate with other conversions +*/ +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] +*/ +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_chub_bytes(): c3_d to c3_y[8] +** XX factor out, deduplicate with other conversions +*/ +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; +} + +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)); +} + + +/* u3_xmas_lane_to_chub(): serialize lane to double-word +*/ +static c3_d +u3_xmas_lane_to_chub(u3_lane lan) { + return ((c3_d)lan.por_s << 32) ^ (c3_d)lan.pip_w; +} + +/* u3_xmas_encode_lane(): serialize lane to noun +*/ +static u3_atom +u3_xmas_encode_lane(u3_lane lan) { + // [%| p=@] + // [%& p=@pC] + return u3i_chub(u3_xmas_lane_to_chub(lan)); +} + + + +// END plagariasm zone +// +// +// +// +// +// +static void _xmas_free_seal(u3_seal* sel_u) +{ + c3_free(sel_u->buf_y); + c3_free(sel_u); +} +// refcounted +// +static u3_noun _xmas_path_with_fra(c3_c* pat_c, c3_s* fra_s) +{ + u3_noun pax = u3do("stab", u3i_string(pat_c)); + u3_noun fra = u3dc("slav", c3__ud, u3do("rear", u3k(pax))); + u3_noun res = u3do("snip", pax); + *fra_s = u3r_short(0, fra); + + u3z(fra); // pax is unncessary bc moved in snip + return res; +} + +static u3_noun _xmas_get_now() { + struct timeval tim_u; + gettimeofday(&tim_u, 0); + u3_noun res = u3_time_in_tv(&tim_u); + return res; +} + +// refcounted +static c3_o +_xmas_rout_pact(u3_xmas_pact* pac_u, u3_noun lan) { + u3_noun tag, val; + u3x_cell(lan, &tag, &val); + + u3_assert( (c3y == tag) || (c3n == tag) ); + c3_o suc_o = c3y; + if ( c3y == tag ) { + u3_assert( c3y == u3a_is_cat(val) ); + u3_assert( val < 256 ); + pac_u->rut_u.typ_y = ROUT_GALAXY; + pac_u->rut_u.imp_y = val; + } else { + u3_lane lan_u = u3_xmas_decode_lane(u3k(val)); + // if in local-only mode, don't send remote packets + // + if ( (c3n == u3_Host.ops_u.net) && (0x7f000001 != lan_u.pip_w) ) { + suc_o = c3n; + } + // if the lane is uninterpretable, silently drop the packet + // + else if ( 0 == lan_u.por_s ) { + if ( u3C.wag_w & u3o_verbose ) { + u3l_log("ames: inscrutable lane"); + } + suc_o = c3n; + } else { + pac_u->rut_u.typ_y = ROUT_OTHER; + pac_u->rut_u.lan_u = lan_u; + } + } + u3z(lan); + return suc_o; +} + + +// refcounted +// RETAIN +static c3_y +_ship_meta(u3_noun her) +{ + c3_w met_w = u3r_met(3, her); + c3_y res_y; + switch ( met_w ) { + case 0: + case 1: + case 2: { + res_y = 0; + break; + } + case 3: + case 4: { + res_y = 1; + break; + } + case 5: + case 6: + case 7: + case 8: { + res_y = 2; + break; + } + default: { + res_y = 3; + break; + } + } + return res_y; +} + + +static void _xmas_pact_free(u3_xmas_pact* pac_u) { + // TODO: i'm lazy +} + +static u3_xmas_head +_xmas_head_from_pact(u3_xmas_pact* pac_u, u3_noun her) +{ + u3_xmas_head hed_u; + hed_u.pro_y = XMAS_VER; + hed_u.typ_y = pac_u->typ_y; + // ship meta is retain, no u3k necessary + //hed_u.ran_y = _ship_meta(her); + hed_u.hop_y = 0; + hed_u.mug_w = 0; + + u3z(her); + return hed_u; +} + +static c3_o +_xmas_sift_head(c3_y buf_y[8], u3_xmas_head* hed_u) +{ + if ( memcmp(buf_y + 4, &XMAS_COOKIE, XMAS_COOKIE_LEN) ) { + return c3n; + + } + c3_w hed_w = _ames_sift_word(buf_y); + + hed_u->nex_y = (hed_w >> 2) & 0x3; + hed_u->pro_y = (hed_w >> 4) & 0x7; + hed_u->typ_y = (hed_w >> 7) & 0x3; + hed_u->hop_y = (hed_w >> 9) & 0x7; + hed_u->mug_w = (hed_w >> 12) & 0xFFFFFF; + + return c3y; + + /*if(c3o((hed_u->typ_y == PACT_PEEK), (hed_u->typ_y == PACT_POKE))) { + hed_u->ran_y = (hed_w >> 30) & 0x3; + }*/ +} + +static void +_xmas_etch_head(u3_xmas_head* hed_u, c3_y buf_y[8]) +{ + if( c3y == XMAS_DEBUG ) { + if( hed_u->pro_y > 7 ) { + u3l_log("xmas: bad protocol version"); + return; + } + } + c3_o req_o = c3o((hed_u->typ_y == PACT_PEEK), (hed_u->typ_y == PACT_POKE)); + c3_y siz_y = req_o ? 5 : 7; + c3_w hed_w = (hed_u->nex_y & 0x3 ) << 2 + ^ (hed_u->pro_y & 0x7 ) << 4 + ^ ((hed_u->typ_y & 0x3 ) << 7) + ^ ((hed_u->hop_y & 0x7 ) << 9) + ^ ((hed_u->mug_w & 0xFFFFFF ) << 12); + // XX: we don't expand hopcount if no request. Correct? + // + /*if ( c3y == req_o ) { + hed_w = hed_w ^ ((hed_u->ran_y & 0x3) << 30); + }*/ + + _ames_etch_word(buf_y, hed_w); + memcpy(buf_y + 4, XMAS_COOKIE, XMAS_COOKIE_LEN); +} + +static c3_w +_xmas_etch_page_pact(c3_y* buf_y, u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u) +{ + /*c3_w cur_w = 0; + + // hops + memcpy(buf_y, pac_u->hop_y, 6); + cur_w += 6; + + // path length + _ames_etch_short(buf_y + cur_w, pac_u->pat_s); + cur_w += 2; + + // path + memcpy(buf_y + cur_w, pac_u->pat_c, pac_u->pat_s + 1); + cur_w += pac_u->pat_s + 1; + + // total + _ames_etch_word(buf_y + cur_w, pac_u->tot_w); + cur_w += 4; + + // auth + memcpy(buf_y + cur_w, &pac_u->aut_y, 96); + cur_w += 96; + + // dat + memcpy(buf_y + cur_w, pac_u->dat_y, 1024); + cur_w += 1024; + + return cur_w;*/ + return 0; +} + +static c3_w +_xmas_etch_peek_pact(c3_y* buf_y, u3_xmas_peek_pact* pac_u, u3_xmas_head* hed_u, c3_o tag_o) +{ + c3_w cur_w = 0; + u3_xmas_name nam_u = pac_u->nam_u; + u3_xmas_name_meta met_u = nam_u.met_u; + + // TODO: double check reserved bits + c3_y met_y = (met_u.ran_y & 0x3) >> 0 + ^ (met_u.rif_y & 0x3) >> 2 + ^ (met_u.pat_o & 0x1) >> 4 + ^ (met_u.boq_o & 0x1) >> 5 + ^ (met_u.fra_y & 0x3) >> 6; + + buf_y[cur_w] = met_y; + + + //shift + cur_w++; + c3_y her_y = 1 << (met_u.ran_y+1); + _ames_ship_of_chubs(nam_u.her_d, her_y, buf_y + cur_w); + cur_w += her_y; + + // rift + c3_y rif_y = met_u.rif_y + 1; + for ( int i = 0; i < rif_y; i++) { + buf_y[cur_w] = (nam_u.rif_w >> (8*i)) & 0xff; + cur_w++; + } + + // path length + c3_y pat_y = 1 >> met_u.pat_o; + for ( int i = 0; i < pat_y; i++ ) { + buf_y[cur_w] = (nam_u.pat_s >> (8*i)) & 0xff; + cur_w++; + } + + // path + memcpy(buf_y + cur_w, nam_u.pat_c, nam_u.pat_s); + cur_w += nam_u.pat_s; + + + // possible bloq size + if ( 1 == met_u.boq_o ) { + buf_y[cur_w] = nam_u.boq_y; + cur_w++; + } + + c3_y fra_y = met_u.fra_y + 1; + for( int i = 0; i < met_u.fra_y; i++ ) { + buf_y[cur_w] = (nam_u.fra_w >> (8*i)) & 0xff; + cur_w++; + } + + return cur_w; + //_ames_etch_short(buf_y + cur_w, &met_s); +} + +static c3_w +_xmas_etch_poke_pact(c3_y* buf_y, u3_xmas_poke_pact* pac_u, u3_xmas_head* hed_u) +{ + c3_w cur_w = 0; + cur_w += _xmas_etch_peek_pact(buf_y, pac_u->pek_u, hed_u, c3n); + + // total + _ames_etch_word(buf_y + cur_w, pac_u->tot_w); + cur_w += 4; + + // auth + memcpy(buf_y + cur_w, pac_u->aut_y, 96); + cur_w += 96; + + // day + memcpy(buf_y + cur_w, pac_u->dat_y, PACT_SIZE - cur_w); + + return PACT_SIZE; +} + +static c3_w +_xmas_etch_pact(c3_y* buf_y, u3_xmas_pact* pac_u, u3_noun her) +{ + c3_w cur_w = 0; + u3_xmas_head hed_u = _xmas_head_from_pact(pac_u, her); + _xmas_etch_head(&hed_u, buf_y + cur_w); + cur_w += 8; + + + switch ( pac_u->typ_y ) { + case PACT_POKE: { + cur_w += _xmas_etch_poke_pact(buf_y + cur_w, &pac_u->pok_u, &hed_u); + } break; + case PACT_PEEK: { + cur_w += _xmas_etch_peek_pact(buf_y + cur_w, &pac_u->pek_u, &hed_u, c3y); + } break; + case PACT_PAGE: { + cur_w += _xmas_etch_page_pact(buf_y + cur_w, &pac_u->pag_u, &hed_u); + } break; + + default: { + u3l_log("bad pact type");//u3m_bail(c3__bail); + } + } + return cur_w; +} + +static void +_xmas_send_cb(uv_udp_send_t* req_u, c3_i sas_i) +{ + u3_seal* sel_u = (u3_seal*)req_u; + u3_xmas* sam_u = sel_u->sam_u; + + if ( sas_i ) { + u3l_log("xmas: send fail_async: %s", uv_strerror(sas_i)); + //sam_u->fig_u.net_o = c3n; + } + else { + //sam_u->fig_u.net_o = c3y; + } + + _xmas_free_seal(sel_u); +} + +static void _xmas_send_buf(u3_xmas* sam_u, u3_lane lan_u, c3_y* buf_y, c3_w len_w) +{ + u3_seal* sel_u = c3_calloc(sizeof(*sel_u)); + sel_u->buf_y = buf_y; + sel_u->len_w = len_w; + sel_u->sam_u = sam_u; + struct sockaddr_in add_u; + + memset(&add_u, 0, sizeof(add_u)); + add_u.sin_family = AF_INET; + c3_w pip_w = c3n == u3_Host.ops_u.net ? lan_u.pip_w : 0x7f000001; + c3_s por_s = lan_u.por_s; + + add_u.sin_addr.s_addr = htonl(pip_w); + add_u.sin_port = htons(por_s); + +#ifdef XMAS_DEBUG + c3_c* sip_c = inet_ntoa(add_u.sin_addr); + u3l_log("xmas: sending packet (%s,%u)", sip_c, por_s); +#endif + + uv_buf_t buf_u = uv_buf_init((c3_c*)buf_y, len_w); + + c3_i sas_i = uv_udp_send(&sel_u->snd_u, + &sam_u->wax_u, + &buf_u, 1, + (const struct sockaddr*)&add_u, + _xmas_send_cb); + + if ( sas_i ) { + u3l_log("ames: send fail_sync: %s", uv_strerror(sas_i)); + /*if ( c3y == sam_u->fig_u.net_o ) { + //sam_u->fig_u.net_o = c3n; + }*/ + _xmas_free_seal(sel_u); + } +} + +static void _xmas_send(u3_xmas_pact* pac_u) +{ + u3_xmas* sam_u = pac_u->sam_u; + u3l_log("xmas sending"); + + + //u3l_log("_ames_send %s %u", _str_typ(pac_u->typ_y), + // pac_u->rut_u.lan_u.por_s); + c3_y* buf_y = c3_calloc(PACT_SIZE); + c3_w siz_w; + { + u3_noun who = 0; + + siz_w = _xmas_etch_pact(buf_y, pac_u, who); + } + + _xmas_send_buf(sam_u, pac_u->rut_u.lan_u, buf_y, siz_w); +} + +static c3_o +_xmas_rout_pacs(u3_xmas_pact* pac_u, u3_noun las) +{ + c3_o suc_o = c3n; + u3_noun lan, t = las; + while ( t != u3_nul ) { + u3x_cell(t, &lan, &t); + // if ( c3n == u3r_cell(t, &lan, &t) ) { + // break; + // } + if ( c3n == _xmas_rout_pact(pac_u, u3k(lan)) ) { + u3l_log("xmas: failed to set route"); + } else { + u3l_log("xmas_send"); + _xmas_send(pac_u); + suc_o = c3y; + } + } + u3z(las); + return suc_o; +} + +static void +_xmas_ef_pact(u3_xmas* sam_u, u3_noun las, u3_noun pac) +{ + u3_noun len, dat; + u3x_cell(pac, &len, &dat); + c3_w len_w = u3r_met(3, pac); + if ( len_w > len ) { +#ifdef XMAS_DEBUG + u3l_log("xmas: vane lying about length"); + return; +#endif + } + c3_y* buf_y = c3_calloc(len); + u3r_bytes(0, len_w, buf_y, pac); + + c3_o suc_o = c3n; + u3_noun lan, t = las; + while ( t != u3_nul ) { + u3x_cell(t, &lan, &t); + // if ( c3n == u3r_cell(t, &lan, &t) ) { + // break; + // } + u3l_log("xmas_send"); + u3_noun tag, val; + u3x_cell(lan, &tag, &val); + u3_assert( (c3y == tag) || (c3n == tag) ); + if ( c3y == tag ) { + u3_assert( c3y == u3a_is_cat(val) ); + u3_assert( val < 256 ); + if ( c3n == u3_Host.ops_u.net ) { + u3_lane lan_u; + lan_u.pip_w = 0x7f000001 ; + lan_u.por_s = _ames_czar_port(val); + _xmas_send_buf(sam_u, lan_u, buf_y, len_w); + } else { + // get galaxy lane or add pending + } + } else { + u3_lane lan_u = u3_xmas_decode_lane(u3k(val)); + // if in local-only mode, don't send remote packets + // + if ( (c3n == u3_Host.ops_u.net) && (0x7f000001 != lan_u.pip_w) ) { + suc_o = c3n; + } + // if the lane is uninterpretable, silently drop the packet + // + else if ( 0 == lan_u.por_s ) { + if ( u3C.wag_w & u3o_verbose ) { + u3l_log("ames: inscrutable lane"); + } + suc_o = c3n; + } else { + _xmas_send_buf(sam_u, lan_u, buf_y, len_w); + } + } + } + u3z(las); u3z(pac); +} + +static void +_xmas_ef_peek(u3_xmas* sam_u, u3_noun las, u3_noun who, u3_noun pat, u3_noun cop) +{ + u3_noun boq, fra, her, rif; + u3x_cell(who, &her, &rif); + u3x_cell(cop, &boq, &fra); + + if ( c3n == sam_u->car_u.liv_o ) { + u3l_log("xmas: not yet live, dropping outbound\r"); + u3z(pat); + } else { + u3_xmas_pact* pac_u = c3_calloc(sizeof(*pac_u)); + pac_u->typ_y = PACT_PEEK; + pac_u->sam_u = sam_u; + { + u3_noun pas = u3do("spat", pat); + pac_u->pek_u.nam_u.pat_c = u3r_string(pas); + pac_u->pek_u.nam_u.pat_s = strlen(pac_u->pek_u.nam_u.pat_c); + u3z(pas); + } + u3r_chubs(0, 2, pac_u->pek_u.nam_u.her_d, her); + pac_u->pek_u.nam_u.rif_w = u3r_word(0, rif); + pac_u->pek_u.nam_u.boq_y = u3r_byte(0, boq); + + if( 384 < pac_u->pek_u.nam_u.pat_s ) { + u3l_log("xmas: path in peek too long"); + _xmas_pact_free(pac_u); + } else { + c3_o rut_o = _xmas_rout_pacs(pac_u, u3k(las)); + + if ( c3n == rut_o ) { + _xmas_pact_free(pac_u); + } + } + } + // do not need to lose pat because moved in spat call + u3z(who); u3z(cop); +} + + +static c3_o _xmas_kick(u3_xmas* sam_u, u3_noun tag, u3_noun dat) +{ + c3_o ret_o; + + switch ( tag ) { + default: { + ret_o = c3n; + } break; + case c3__pact: { + u3_noun las, pac; + if ( c3n == u3r_cell(dat, &las, &pac) ) { + ret_o = c3n; + } else { + _xmas_ef_pact(sam_u, u3k(las), u3k(pac)); + ret_o = c3y; + } + } break; + // + case c3__peek: { + u3_noun lan, her, pat; + if ( c3n == u3r_trel(dat, &her, &lan, &pat) ) { + ret_o = c3n; + } else { + //_xmas_ef_peek(sam_u, u3k(her), u3k(lan), u3k(pat)); + ret_o = c3y; + } + } break; + } + + // technically losing tag is unncessary as it always should + // be a direct atom, but better to be strict + u3z(dat); u3z(tag); + return ret_o; +} + +static c3_o _xmas_io_kick(u3_auto* car_u, u3_noun wir, u3_noun cad) +{ + u3_xmas* sam_u = (u3_xmas*)car_u; + + u3_noun tag, dat; + c3_o ret_o; + + if( c3n == u3r_cell(cad, &tag, &dat) ) + { + ret_o = c3n; + } else { + ret_o = _xmas_kick(sam_u, u3k(tag), u3k(dat)); + } + + u3z(wir); u3z(cad); + return ret_o; +} + + + +static u3_noun +_xmas_io_info(u3_auto* car_u) +{ + + return u3_nul; +} + +static void +_xmas_io_slog(u3_auto* car_u) { + u3l_log("xmas is online"); +} + +static void +_xmas_exit_cb(uv_handle_t* had_u) +{ + u3_xmas* sam_u = had_u->data; + + u3s_cue_xeno_done(sam_u->sil_u); + ur_cue_test_done(sam_u->tes_u); + u3h_free(sam_u->her_p); + u3h_free(sam_u->req_p); + c3_free(sam_u); +} + +static void +_xmas_io_exit(u3_auto* car_u) +{ + u3_xmas* sam_u = (u3_xmas*)car_u; + uv_close(&sam_u->had_u, _xmas_exit_cb); +} + +static void +_xmas_sift_page_pact(u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, c3_w len_w) +{ + c3_w cur_w = 0; + + /*// next hop + memcpy(pac_u->hop_y, buf_y, 6); + cur_w += 6; + + // path length + pac_u->pat_s = _ames_sift_short(buf_y + cur_w); + cur_w += 2; + + // path contents + pac_u->pat_c = c3_calloc(pac_u->pat_s + 1); + memcpy(pac_u->pat_c, buf_y + cur_w, pac_u->pat_s); + pac_u->pat_c[pac_u->pat_s] = '\0'; + cur_w += pac_u->pat_s + 1; + + // total fragments + pac_u->tot_w = _ames_sift_word(buf_y + cur_w); + cur_w += 4; + + // Authenticator + memcpy(pac_u->aut_y, buf_y + cur_w, 96); + cur_w += 96; + + c3_w dat_w = 1024; // len_w - cur_w; + pac_u->dat_y = c3_calloc(dat_w); + memcpy(pac_u->dat_y, buf_y + cur_w, dat_w); + */ +} + +static c3_w +_xmas_sift_name(u3_xmas_name* nam_u, c3_y* buf_y) +{ + c3_w cur_w = 0; + c3_w met_y = buf_y[cur_w]; + nam_u->met_u.ran_y = (met_y >> 0) & 0x3; + nam_u->met_u.rif_y = (met_y >> 2) & 0x3; + nam_u->met_u.pat_o = (met_y >> 4) & 0x1; + nam_u->met_u.boq_o = (met_y >> 5) & 0x1; + nam_u->met_u.fra_y = (met_y >> 6) & 0x3; + cur_w += 1; + + c3_y her_y = 2 << nam_u->met_u.ran_y; + _ames_ship_to_chubs(nam_u->her_d, her_y, buf_y + cur_w); + cur_w += her_y; + + c3_y rif_y = 2 << nam_u->met_u.rif_y; + //SIFT_VAR(nam_u->rif_w, (buf_y + cur_w), rif_y); + for( int i = 0; i < rif_y; i++ ) { + nam_u->rif_w |= (buf_y[cur_w] << (8*i)); + cur_w++; + } + + c3_y pat_y = 2 << nam_u->met_u.pat_o; + nam_u->pat_s = 0; + for ( int i = 0; i < pat_y; i++ ) { + nam_u->pat_s |= (buf_y[cur_w] << (8*i)); + cur_w++; + } + + nam_u->pat_c = c3_calloc(nam_u->pat_s + 1); // unix string for ease of manipulation + memcpy(nam_u->pat_c, buf_y + cur_w, nam_u->pat_s); + nam_u->pat_c[nam_u->pat_s] = 0; + cur_w += nam_u->pat_s; + + if ( 1 == nam_u->met_u.boq_o ) { + nam_u->boq_y = buf_y[cur_w]; + cur_w++; + } else { + nam_u->boq_y = 13; + } + + c3_y fra_y = 2 << nam_u->met_u.fra_y; + nam_u->fra_w = 0; + for( int i = 0; i < fra_y; i++ ) { + nam_u->fra_w |= (buf_y[cur_w] << (8*i)); + + } + + return cur_w; +} + + +static c3_w +_xmas_sift_peek_pact(u3_xmas_peek_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, c3_w len_w, c3_o tag_o) +{ + c3_w siz_w = _xmas_sift_name(&pac_u->nam_u, buf_y); + /*if (siz_w > len_w ) { + u3l_log("xmas: buffer overrun (FIXME)"); + u3m_bail(c3__foul); + }*/ + return siz_w; +} + +static void +_xmas_sift_poke_pact(u3_xmas_poke_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, c3_w len_w) +{ + c3_w cur_w = 0; + // Peek portion + pac_u->pek_u = c3_calloc(sizeof(*(pac_u->pek_u))); + cur_w += _xmas_sift_peek_pact(pac_u->pek_u, hed_u, buf_y, len_w, c3n); + + // Total fragments + pac_u->tot_w = _ames_sift_word(buf_y + cur_w); + cur_w += 4; + + // Authenticator + memcpy(pac_u->aut_y, buf_y + cur_w, 96); + cur_w += 96; + + // Datum + memcpy(pac_u->dat_y, buf_y + cur_w, len_w - cur_w); +} + +static void +_xmas_sift_pact(u3_xmas_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, c3_w len_w) +{ + pac_u->typ_y = hed_u->typ_y; + switch ( hed_u->typ_y ) { + case PACT_PEEK: { + _xmas_sift_peek_pact(&pac_u->pek_u, hed_u, buf_y, len_w, c3y); + } break; + case PACT_PAGE: { + _xmas_sift_page_pact(&pac_u->pag_u, hed_u, buf_y, len_w); + } break; + case PACT_POKE: { + _xmas_sift_poke_pact(&pac_u->pok_u, hed_u, buf_y, len_w); + } break; + default: { + u3l_log("xmas: received unknown packet type"); + break; + } + } +} + +/* + * RETAIN + */ +static u3_weak +_xmas_get_peer(u3_xmas* sam_u, u3_noun her) +{ + return u3h_git(sam_u->her_p, her); +} + +/* + * RETAIN + */ +static void +_xmas_put_sponsee(u3_xmas* sam_u, u3_noun her, u3_noun las) +{ + u3_noun val = u3nc(u3_nul, u3k(las)); + u3h_put(sam_u->her_p, her, val); + u3z(val); +} + +/* + * RETAIN + */ +static c3_o +_xmas_add_galaxy_pend(u3_xmas* sam_u, u3_noun her, u3_noun pen) +{ + u3_weak old = u3h_git(sam_u->her_p, her); + u3_noun pes = u3_nul; + u3_noun wat; + if ( u3_none != old ) { + if ( u3h(old) == XMAS_CZAR ) { + u3x_cell(u3t(old), &pes, &wat); + u3z(old); + } else { + u3l_log("xmas: attempted to resolve resolved czar"); + u3z(old); + return c3n; + } + } + u3_noun val = u3nc(u3nc(u3k(pen), u3k(pes)), c3y); + u3h_put(sam_u->her_p, her, val); + u3z(val); + return _(wat); +} + +/* + * RETAIN + */ +static u3_weak +_xmas_get_cache(u3_xmas* sam_u, u3_noun pax) +{ + u3_weak res = u3h_git(sam_u->pen_p, pax); + if ( u3_none == res ) { + u3m_p("miss", u3k(pax)); + } else { + u3m_p("hit", u3nc(u3k(pax), u3k(res))); + } + return res; +} + +static void +_xmas_put_cache(u3_xmas* sam_u, u3_noun pax, u3_noun val) +{ + u3m_p("new key", u3k(pax)); + u3m_p("new value", u3k(val)); + u3h_put(sam_u->pen_p, pax, val); +} + +/* _xmas_czar(): add packet to queue, possibly begin DNS resolution + */ +static void +_xmas_czar(u3_xmas_pact* pac_u) +{ + u3_xmas* sam_u = pac_u->sam_u; +#ifdef XMAS_DEBUG + if ( pac_u->typ_y != PACT_PEEK ) { + u3l_log("xmas: attempted to resolve galaxy for packet that was not peek"); + u3m_bail(c3__oops); + return; + } +#endif + + u3_noun pat = u3i_string(pac_u->pek_u.nam_u.pat_c); + + c3_y her_y = pac_u->rut_u.imp_y; + + u3_weak her = _xmas_get_peer(sam_u, her_y); + + c3_o lok_o = _xmas_add_galaxy_pend(sam_u, her_y, pat); + + + // TODO: actually resolve DNS + u3z(pat); +} + + +static void +_xmas_try_forward(u3_xmas_pact* pac_u, u3_noun fra, u3_noun hit) +{ + u3l_log(""); + u3l_log("stubbed forwarding"); +} + +static void +_xmas_respond(u3_xmas_pact* req_u, u3_xmas_pact* res_u, u3_noun hit, c3_s fra_s) +{ + res_u->typ_y = PACT_PAGE; + + res_u->sam_u = req_u->sam_u; + + memcpy(&res_u->pag_u.nam_u, &req_u->pek_u.nam_u, sizeof(u3_xmas_name)); + + /*res_u->pag_u.pat_c = c3_calloc(req_u->pek_u.pat_s + 1); + res_u->pag_u.pat_c[req_u->pek_u.pat_s] = '\0'; + memcpy(res_u->pag_u.pat_c, req_u->pek_u.pat_c, req_u->pek_u.pat_s); + u3r_bytes(0, 1024, res_u->pag_u.dat_y, u3dc("snag", u3i_word(fra_s), u3k(u3t(hit))));*/ + u3z(hit); +} + +static void +_xmas_serve_cache_hit(u3_xmas_pact* req_u, u3_lane lan_u, c3_s fra_s, u3_noun hit) +{ + u3l_log("serving cache hit"); + c3_d her_d[2]; + memcpy(her_d, req_u->pek_u.nam_u.her_d, 2); + c3_d our_d[2]; + memcpy(our_d, req_u->sam_u->pir_u->who_d, 2); + if ( (her_d[0] != our_d[0]) + || (her_d[1] != our_d[1]) ) + { + u3l_log("publisher is not ours"); + if ( (256 > our_d[0]) + && (0 == our_d[1]) ) + { + // Forward only if requested datum is not ours, and we are a galaxy + _xmas_try_forward(req_u, fra_s, hit); + } else { + u3l_log("no forward, we are not a galaxy"); + } + } else { + //u3l_log("first: %x", u3r_string(u3dc("scot", c3__uv, u3k(u3h(u3t(hit)))))); + u3_xmas_pact* res_u = c3_calloc(sizeof(*res_u)); + res_u->rut_u.typ_y = ROUT_OTHER; + res_u->rut_u.lan_u = lan_u; + _xmas_respond(req_u, res_u, hit, fra_s); + _xmas_send(res_u); + } + // no lose needed because all nouns are moved in both + // branches of the conditional +} + +/* + */ +static void +_xmas_page_scry_cb(void* vod_p, u3_noun nun) +{ + u3_xmas_pact* pac_u = vod_p; + u3_xmas* sam_u = pac_u->sam_u; + c3_s fra_s; + u3_noun pax = _xmas_path_with_fra(pac_u->pek_u.nam_u.pat_c, &fra_s); + + u3_weak hit = u3r_at(7, nun); + if ( u3_none == hit ) { + // TODO: mark as dead + //u3z(nun); + u3l_log("unbound"); + + } else { + u3_weak old = _xmas_get_cache(sam_u, pax); + if ( old == u3_none ) { + u3l_log("bad"); + } else { + u3_noun tag; + u3_noun dat; + u3x_cell(u3k(old), &tag, &dat); + if ( XMAS_SCRY == tag ) { + u3_xmas_pact* res_u = c3_calloc(sizeof(*res_u)); + _xmas_respond(pac_u, res_u, u3k(hit), fra_s); + + _xmas_rout_pacs(res_u, u3k(u3t(dat))); + } + _xmas_put_cache(sam_u, u3k(pax), u3nc(XMAS_ITEM, u3k(hit))); + u3z(old); + } + u3z(hit); + } + u3z(pax); +} + +static void +_xmas_hear_bail(u3_ovum* egg_u, u3_noun lud) +{ + u3l_log("xmas: hear bail"); + u3_ovum_free(egg_u); +} + +static void +_xmas_hear_page(u3_xmas_pact* pac_u, u3_lane lan_u) +{ + u3_xmas* sam_u = pac_u->sam_u; + u3_noun wir = u3nc(c3__xmas, u3_nul); + c3_s fra_s; + u3_noun pat = _xmas_path_with_fra(pac_u->pag_u.nam_u.pat_c, &fra_s); + + u3_weak cac = _xmas_get_cache(sam_u, pat); + + if ( u3_none != cac ) { + u3_noun tag, dat; + + u3x_cell(cac, &tag, &dat); + if ( tag == XMAS_SCRY ) { + _xmas_rout_pacs(pac_u, u3k(u3t(dat))); + } + u3z(cac); + } else { + u3_noun mess = u3nq( + pat, + u3i_word(pac_u->pag_u.mat_u.tot_w), + u3i_bytes(256, pac_u->pag_u.mat_u.aut_y), + u3i_bytes(pac_u->pag_u.mat_u.len_d, pac_u->pag_u.mat_u.fra_y) + ); + u3_noun cad = u3nt(c3__mess, c3__page, mess); + + u3_auto_peer( + u3_auto_plan(&sam_u->car_u, + u3_ovum_init(0, c3__x, wir, cad)), + 0, 0, _xmas_hear_bail); + } + // no lose required because all nouns are moved +} + +static void +_xmas_add_lane_to_cache(u3_xmas* sam_u, u3_noun las, u3_noun pax, u3_lane lan_u) +{ + u3_noun hit = u3nq(XMAS_SCRY, + _xmas_get_now(), + u3nc(c3n, u3_xmas_encode_lane(lan_u)), + u3k(las)); + _xmas_put_cache(sam_u, pax, hit); + u3z(las); +} + +static void +_xmas_hear_peek(u3_xmas_pact* pac_u, u3_lane lan_u) +{ + u3l_log("xmas: hear peek"); + u3l_log("%hu", lan_u.por_s); + u3_xmas* sam_u = pac_u->sam_u; + c3_s fra_s; + u3_noun pax = _xmas_path_with_fra(pac_u->pek_u.nam_u.pat_c, &fra_s); + u3_weak hit = _xmas_get_cache(sam_u, u3k(pax)); + if ( u3_none != hit ) { + u3_noun tag, dat; + u3x_cell(u3k(hit), &tag, &dat); + u3m_p("dat", dat); + if ( tag == XMAS_SCRY ) { + _xmas_add_lane_to_cache(sam_u, u3k(u3t(dat)), u3k(pax), lan_u); + } else if ( tag == XMAS_ITEM ) { + u3l_log("item hit"); + u3m_p("dat", u3k(dat)); + u3l_log("after dat"); + _xmas_serve_cache_hit(pac_u, lan_u, fra_s, u3k(dat)); + } else { + u3l_log("xmas: weird case in cache, dropping"); + } + u3z(hit); + } else { + _xmas_add_lane_to_cache(sam_u, u3_nul, u3k(pax), lan_u); // TODO: retrieve from namespace + u3_noun sky = u3nc(c3__page, pax); + u3_pier_peek_last(sam_u->car_u.pir_u, u3_nul, c3__xx, u3_nul, sky, pac_u, _xmas_page_scry_cb); + } + u3l_log("xmas: finished hear peek"); + u3z(pax); +} + +static void +_xmas_hear(u3_xmas* sam_u, + u3_lane* lan_u, + c3_w len_w, + c3_y* hun_y) +{ + + u3l_log("xmas_hear"); + + u3_xmas_pact* pac_u; + c3_w pre_w; + c3_y* cur_y = hun_y; + if ( HEAD_SIZE > len_w ) { + c3_free(hun_y); + return; + } + + pac_u = c3_calloc(sizeof(*pac_u)); + pac_u->sam_u = sam_u; + u3_xmas_head hed_u; + _xmas_sift_head(cur_y, &hed_u); + _log_head(&hed_u); + _xmas_sift_pact(pac_u, &hed_u, hun_y + 8, (len_w - 8)); + + switch ( pac_u->typ_y ) { + case PACT_PEEK: { + _xmas_hear_peek(pac_u, *lan_u); + } break; + case PACT_PAGE: { + _xmas_hear_page(pac_u, *lan_u); + } break; + default: { + u3l_log("xmas: unimplemented packet type %u", pac_u->typ_y); + } break; + } +} + +static void +_xmas_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("xmas: recv: fail: %s", 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("xmas: recv: fail: message truncated"); + } + c3_free(buf_u->base); + } + else { + u3_xmas* sam_u = wax_u->data; + struct sockaddr_in* add_u = (struct sockaddr_in*)adr_u; + u3_lane lan_u; + + u3l_log("test"); + + lan_u.por_s = ntohs(add_u->sin_port); + // u3l_log("port: %s", lan_u.por_s); + lan_u.pip_w = ntohl(add_u->sin_addr.s_addr); + // u3l_log("IP: %x", lan_u.pip_w); + // NB: [nrd_i] will never exceed max length from _ames_alloc() + // + _xmas_hear(sam_u, &lan_u, (c3_w)nrd_i, (c3_y*)buf_u->base); + } +} + + + +static void +_xmas_io_talk(u3_auto* car_u) +{ + u3_xmas* sam_u = (u3_xmas*)car_u; + sam_u->dns_c = "urbit.org"; // TODO: receive turf + //_xmas_io_start(sam_u); + u3_noun who = u3i_chubs(2, sam_u->pir_u->who_d); + u3_noun rac = u3do("clan:title", u3k(who)); + c3_s por_s = sam_u->pir_u->por_s; + c3_i ret_i; + if ( c3__czar == rac ) { + c3_y num_y = (c3_y)sam_u->pir_u->who_d[0]; + c3_s zar_s = _ames_czar_port(num_y); + + if ( 0 == por_s ) { + por_s = zar_s; + } + else if ( por_s != zar_s ) { + u3l_log("ames: czar: overriding port %d with -p %d", zar_s, por_s); + u3l_log("ames: czar: WARNING: %d required for discoverability", zar_s); + } + } + + + // Bind and stuff. + { + struct sockaddr_in add_u; + c3_i add_i = sizeof(add_u); + + memset(&add_u, 0, sizeof(add_u)); + add_u.sin_family = AF_INET; + add_u.sin_addr.s_addr = _(u3_Host.ops_u.net) ? + htonl(INADDR_ANY) : + htonl(INADDR_LOOPBACK); + add_u.sin_port = htons(por_s); + + if ( (ret_i = uv_udp_bind(&sam_u->wax_u, + (const struct sockaddr*)&add_u, 0)) != 0 ) + { + u3l_log("xmas: bind: %s", uv_strerror(ret_i)); + + /*if ( (c3__czar == rac) && + (UV_EADDRINUSE == ret_i) ) + { + u3l_log(" ...perhaps you've got two copies of vere running?"); + }*/ + + // XX revise + // + u3_pier_bail(u3_king_stub()); + } + + uv_udp_getsockname(&sam_u->wax_u, (struct sockaddr *)&add_u, &add_i); + u3_assert(add_u.sin_port); + + sam_u->pir_u->por_s = ntohs(add_u.sin_port); + } + if ( c3y == u3_Host.ops_u.net ) { + u3l_log("xmas: live on %d", sam_u->pir_u->por_s); + } + else { + u3l_log("xmas: live on %d (localhost only)", sam_u->pir_u->por_s); + } + + uv_udp_recv_start(&sam_u->wax_u, _ames_alloc, _xmas_recv_cb); + + sam_u->car_u.liv_o = c3y; + //u3z(rac); u3z(who); +} + +/* _xmas_io_init(): initialize ames I/O. +*/ +u3_auto* +u3_xmas_io_init(u3_pier* pir_u) +{ + u3_xmas* sam_u = c3_calloc(sizeof(*sam_u)); + sam_u->pir_u = pir_u; + + sam_u->her_p = u3h_new_cache(100000); + sam_u->req_p = u3h_new_cache(100000); + + u3_assert( !uv_udp_init(u3L, &sam_u->wax_u) ); + sam_u->wax_u.data = sam_u; + + sam_u->sil_u = u3s_cue_xeno_init(); + sam_u->tes_u = ur_cue_test_init(); + + // Disable networking for fake ships + // + if ( c3y == sam_u->pir_u->fak_o ) { + u3_Host.ops_u.net = c3n; + } + + u3_auto* car_u = &sam_u->car_u; + car_u->nam_m = c3__xmas; + car_u->liv_o = c3y; + car_u->io.talk_f = _xmas_io_talk; + car_u->io.info_f = _xmas_io_info; + car_u->io.slog_f = _xmas_io_slog; + car_u->io.kick_f = _xmas_io_kick; + car_u->io.exit_f = _xmas_io_exit; + + + /*{ + u3_noun now; + struct timeval tim_u; + gettimeofday(&tim_u, 0); + + now = u3_time_in_tv(&tim_u); + //sam_u->sev_l = u3r_mug(now); + u3z(now); + }*/ + + return car_u; +} + + + + diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c index 841d8dd3ca..33c3f031d1 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/xmas.c @@ -4,6 +4,7 @@ #include "noun.h" #include "ur.h" +#include #include #include #include @@ -28,6 +29,7 @@ static c3_y XMAS_COOKIE[4] = { 0x5e, 0x1d, 0xad, 0x51 }; #define SIFT_VAR(dest, src, len) dest = 0; for(int i = 0; i < len; i++ ) { dest |= ((src + i) >> (8*i)); } +#define CHECK_BOUNDS(cur) if ( len_w < cur ) { u3l_log("xmas: failed parse (%i,%i) at line %i", len_w, cur, __LINE__); return 0; } /* _u3_xmas: next generation networking */ @@ -53,8 +55,8 @@ typedef struct _u3_xmas { typedef enum _u3_xmas_ptag { PACT_RESV = 0, - PACT_PEEK = 1, - PACT_PAGE = 2, + PACT_PAGE = 1, + PACT_PEEK = 2, PACT_POKE = 3, } u3_xmas_ptag; @@ -216,34 +218,23 @@ _log_peek_pact(u3_xmas_peek_pact* pac_u) u3l_log("publisher: %s", u3r_string(u3dc("scot", c3__p, u3i_chubs(2, pac_u->nam_u.her_d)))); u3l_log("path len: %u", pac_u->nam_u.pat_s); u3l_log("path: %s", pac_u->nam_u.pat_c); + u3l_log("bloq: %u", pac_u->nam_u.boq_y); + u3l_log("frag: %u", pac_u->nam_u.fra_w); } static void -_log_pact(u3_xmas_peek_pact* pac_u) +_log_pact(u3_xmas_pact* pac_u) { - u3l_log("-- REQUEST PACKET --"); - /*if( ROUT_GALAXY == pac_u->rut_u.typ_y ) { - u3l_log("-- GALAXY ROUTE --"); - u3_noun her = u3dc("scot", c3__p, u3i_word(pac_u->rut_u.imp_u.her_y)); - c3_c* her_c = u3r_string(her); - u3l_log("ship lane: %s", her_c); - free(her_c); - u3z(her); + switch ( pac_u->typ_y ) { + case PACT_PEEK: { + _log_peek_pact(&pac_u->pek_u); + break; + }; + default: { + u3l_log("logging not implemented for %i", pac_u->typ_y); + break; + } } - else { - u3l_log("-- OTHER ROUTE --"); - u3_noun pip = u3dc("scot", c3__if, u3i_word(pac_u->rut_u.lan_u.pip_w)); - c3_c* pip_c = u3r_string(pip); - u3l_log("IP: (%s) port (%u)", pip_c, pac_u->rut_u.lan_u.por_s); - free(pip_c); - u3z(pip); - }*/ - u3_noun pub = u3dc("scot", c3__p, u3i_chubs(2, pac_u->nam_u.her_d)); - c3_c* pub_c = u3r_string(pub); - u3l_log("ship: %s", pub_c); - free(pub_c); - //u3z(pub); - u3l_log("path: %s", pac_u->nam_u.pat_c); } @@ -810,9 +801,17 @@ _xmas_rout_pacs(u3_xmas_pact* pac_u, u3_noun las) static void _xmas_ef_pact(u3_xmas* sam_u, u3_noun las, u3_noun pac) { - c3_w len_w = u3r_met(3, pac); - c3_y* buf_y = c3_calloc(len_w); - u3r_bytes(0, len_w, buf_y, pac); + u3_noun len, dat; + u3x_cell(pac, &len, &dat); + c3_w len_w = u3r_met(3, dat); + if ( len_w > len ) { +#ifdef XMAS_DEBUG + u3l_log("xmas: vane lying about length %i", len_w); + return; +#endif + } + c3_y* buf_y = c3_calloc(len); + u3r_bytes(0, len, buf_y, dat); c3_o suc_o = c3n; u3_noun lan, t = las; @@ -832,7 +831,7 @@ _xmas_ef_pact(u3_xmas* sam_u, u3_noun las, u3_noun pac) u3_lane lan_u; lan_u.pip_w = 0x7f000001 ; lan_u.por_s = _ames_czar_port(val); - _xmas_send_buf(sam_u, lan_u, buf_y, len_w); + _xmas_send_buf(sam_u, lan_u, buf_y, len); } else { // get galaxy lane or add pending } @@ -851,7 +850,7 @@ _xmas_ef_pact(u3_xmas* sam_u, u3_noun las, u3_noun pac) } suc_o = c3n; } else { - _xmas_send_buf(sam_u, lan_u, buf_y, len_w); + _xmas_send_buf(sam_u, lan_u, buf_y, len); } } } @@ -984,7 +983,7 @@ _xmas_io_exit(u3_auto* car_u) uv_close(&sam_u->had_u, _xmas_exit_cb); } -static void +static c3_w _xmas_sift_page_pact(u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, c3_w len_w) { c3_w cur_w = 0; @@ -1015,12 +1014,23 @@ _xmas_sift_page_pact(u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, pac_u->dat_y = c3_calloc(dat_w); memcpy(pac_u->dat_y, buf_y + cur_w, dat_w); */ + return 0; } static c3_w -_xmas_sift_name(u3_xmas_name* nam_u, c3_y* buf_y) +_xmas_sift_name(u3_xmas_name* nam_u, c3_y* buf_y, c3_w len_w) { +#ifdef XMAS_DEBUG + u3l_log("xmas: sifting name %i", len_w); + _log_buf(buf_y, len_w); +#endif + + + if( len_w < 1) { + return 0; + } c3_w cur_w = 0; + CHECK_BOUNDS(cur_w + 1); c3_w met_y = buf_y[cur_w]; nam_u->met_u.ran_y = (met_y >> 0) & 0x3; nam_u->met_u.rif_y = (met_y >> 2) & 0x3; @@ -1030,36 +1040,46 @@ _xmas_sift_name(u3_xmas_name* nam_u, c3_y* buf_y) cur_w += 1; c3_y her_y = 2 << nam_u->met_u.ran_y; + CHECK_BOUNDS(cur_w + her_y) _ames_ship_to_chubs(nam_u->her_d, her_y, buf_y + cur_w); cur_w += her_y; c3_y rif_y = 2 << nam_u->met_u.rif_y; //SIFT_VAR(nam_u->rif_w, (buf_y + cur_w), rif_y); + /*CHECK_BOUNDS(cur_w + rif_y) for( int i = 0; i < rif_y; i++ ) { nam_u->rif_w |= (buf_y[cur_w] << (8*i)); cur_w++; - } + }*/ - c3_y pat_y = 2 << nam_u->met_u.pat_o; + c3_y pat_y = 1 << nam_u->met_u.pat_o; + CHECK_BOUNDS(cur_w + pat_y) + _log_buf(buf_y + cur_w, len_w - cur_w); nam_u->pat_s = 0; for ( int i = 0; i < pat_y; i++ ) { nam_u->pat_s |= (buf_y[cur_w] << (8*i)); cur_w++; } + _log_buf(buf_y + cur_w, len_w - cur_w); nam_u->pat_c = c3_calloc(nam_u->pat_s + 1); // unix string for ease of manipulation + CHECK_BOUNDS(nam_u->pat_s + cur_w); memcpy(nam_u->pat_c, buf_y + cur_w, nam_u->pat_s); nam_u->pat_c[nam_u->pat_s] = 0; cur_w += nam_u->pat_s; + _log_buf(buf_y + cur_w, len_w - cur_w); - if ( 1 == nam_u->met_u.boq_o ) { + if ( c3n == nam_u->met_u.boq_o ) { + CHECK_BOUNDS(cur_w + 1); nam_u->boq_y = buf_y[cur_w]; cur_w++; } else { nam_u->boq_y = 13; } - c3_y fra_y = 2 << nam_u->met_u.fra_y; + c3_y fra_y = 1 << nam_u->met_u.fra_y; + _log_buf(buf_y + cur_w, len_w - cur_w); + CHECK_BOUNDS(cur_w + fra_y) nam_u->fra_w = 0; for( int i = 0; i < fra_y; i++ ) { nam_u->fra_w |= (buf_y[cur_w] << (8*i)); @@ -1073,7 +1093,7 @@ _xmas_sift_name(u3_xmas_name* nam_u, c3_y* buf_y) static c3_w _xmas_sift_peek_pact(u3_xmas_peek_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, c3_w len_w, c3_o tag_o) { - c3_w siz_w = _xmas_sift_name(&pac_u->nam_u, buf_y); + c3_w siz_w = _xmas_sift_name(&pac_u->nam_u, buf_y, len_w); /*if (siz_w > len_w ) { u3l_log("xmas: buffer overrun (FIXME)"); u3m_bail(c3__foul); @@ -1081,7 +1101,7 @@ _xmas_sift_peek_pact(u3_xmas_peek_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, return siz_w; } -static void +static c3_w _xmas_sift_poke_pact(u3_xmas_poke_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, c3_w len_w) { c3_w cur_w = 0; @@ -1099,23 +1119,31 @@ _xmas_sift_poke_pact(u3_xmas_poke_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, // Datum memcpy(pac_u->dat_y, buf_y + cur_w, len_w - cur_w); + return cur_w; } -static void +static c3_w _xmas_sift_pact(u3_xmas_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, c3_w len_w) { pac_u->typ_y = hed_u->typ_y; + c3_w res_w = 0; switch ( hed_u->typ_y ) { case PACT_PEEK: { - _xmas_sift_peek_pact(&pac_u->pek_u, hed_u, buf_y, len_w, c3y); + res_w = _xmas_sift_peek_pact(&pac_u->pek_u, hed_u, buf_y, len_w, c3y); } break; case PACT_PAGE: { - _xmas_sift_page_pact(&pac_u->pag_u, hed_u, buf_y, len_w); + res_w = _xmas_sift_page_pact(&pac_u->pag_u, hed_u, buf_y, len_w); } break; case PACT_POKE: { - _xmas_sift_poke_pact(&pac_u->pok_u, hed_u, buf_y, len_w); + res_w = _xmas_sift_poke_pact(&pac_u->pok_u, hed_u, buf_y, len_w); } break; + default: { + u3l_log("xmas: received unknown packet type"); + break; + } } + u3_assert(res_w < len_w ); + return res_w; } /* @@ -1167,23 +1195,28 @@ _xmas_add_galaxy_pend(u3_xmas* sam_u, u3_noun her, u3_noun pen) * RETAIN */ static u3_weak -_xmas_get_cache(u3_xmas* sam_u, u3_noun pax) +_xmas_get_cache(u3_xmas* sam_u, u3_xmas_name nam_u) { + u3_noun pax = u3dt("cat", 3, '/', u3i_string(nam_u.pat_c)); u3_weak res = u3h_git(sam_u->pen_p, pax); if ( u3_none == res ) { u3m_p("miss", u3k(pax)); } else { u3m_p("hit", u3nc(u3k(pax), u3k(res))); } + u3z(pax); return res; } static void -_xmas_put_cache(u3_xmas* sam_u, u3_noun pax, u3_noun val) +_xmas_put_cache(u3_xmas* sam_u, u3_xmas_name nam_u, u3_noun val) { + u3_noun pax = u3dt("cat", 3, '/', u3i_string(nam_u.pat_c)); u3m_p("new key", u3k(pax)); u3m_p("new value", u3k(val)); + u3h_put(sam_u->pen_p, pax, val); + u3z(pax); // TODO: fix refcount } /* _xmas_czar(): add packet to queue, possibly begin DNS resolution @@ -1221,24 +1254,23 @@ _xmas_try_forward(u3_xmas_pact* pac_u, u3_noun fra, u3_noun hit) u3l_log("stubbed forwarding"); } -static void -_xmas_respond(u3_xmas_pact* req_u, u3_xmas_pact* res_u, u3_noun hit, c3_s fra_s) +static c3_w +_xmas_respond(u3_xmas_pact* req_u, c3_y** buf_y, u3_noun hit) { - res_u->typ_y = PACT_PAGE; + u3_noun mat = u3dc("snag", u3i_word(req_u->pek_u.nam_u.fra_w), u3k(u3t(hit))); + + u3_noun len, dat; + u3x_cell(mat, &len, &dat); - res_u->sam_u = req_u->sam_u; - memcpy(&res_u->pag_u.nam_u, &req_u->pek_u.nam_u, sizeof(u3_xmas_name)); + *buf_y = c3_calloc(len); + u3r_bytes(0, len, *buf_y, dat); - /*res_u->pag_u.pat_c = c3_calloc(req_u->pek_u.pat_s + 1); - res_u->pag_u.pat_c[req_u->pek_u.pat_s] = '\0'; - memcpy(res_u->pag_u.pat_c, req_u->pek_u.pat_c, req_u->pek_u.pat_s); - u3r_bytes(0, 1024, res_u->pag_u.dat_y, u3dc("snag", u3i_word(fra_s), u3k(u3t(hit))));*/ u3z(hit); } static void -_xmas_serve_cache_hit(u3_xmas_pact* req_u, u3_lane lan_u, c3_s fra_s, u3_noun hit) +_xmas_serve_cache_hit(u3_xmas_pact* req_u, u3_lane lan_u, u3_noun hit) { u3l_log("serving cache hit"); c3_d her_d[2]; @@ -1253,17 +1285,16 @@ _xmas_serve_cache_hit(u3_xmas_pact* req_u, u3_lane lan_u, c3_s fra_s, u3_noun hi && (0 == our_d[1]) ) { // Forward only if requested datum is not ours, and we are a galaxy - _xmas_try_forward(req_u, fra_s, hit); + //_xmas_try_forward(req_u, fra_s, hit); } else { u3l_log("no forward, we are not a galaxy"); } } else { //u3l_log("first: %x", u3r_string(u3dc("scot", c3__uv, u3k(u3h(u3t(hit)))))); - u3_xmas_pact* res_u = c3_calloc(sizeof(*res_u)); - res_u->rut_u.typ_y = ROUT_OTHER; - res_u->rut_u.lan_u = lan_u; - _xmas_respond(req_u, res_u, hit, fra_s); - _xmas_send(res_u); + c3_y* buf_y; + + c3_w len_w = _xmas_respond(req_u, &buf_y, hit); + _xmas_send_buf(req_u->sam_u, lan_u, buf_y, len_w); } // no lose needed because all nouns are moved in both // branches of the conditional @@ -1277,7 +1308,7 @@ _xmas_page_scry_cb(void* vod_p, u3_noun nun) u3_xmas_pact* pac_u = vod_p; u3_xmas* sam_u = pac_u->sam_u; c3_s fra_s; - u3_noun pax = _xmas_path_with_fra(pac_u->pek_u.nam_u.pat_c, &fra_s); + //u3_noun pax = _xmas_path_with_fra(pac_u->pek_u.nam_u.pat_c, &fra_s); u3_weak hit = u3r_at(7, nun); if ( u3_none == hit ) { @@ -1286,7 +1317,7 @@ _xmas_page_scry_cb(void* vod_p, u3_noun nun) u3l_log("unbound"); } else { - u3_weak old = _xmas_get_cache(sam_u, pax); + u3_weak old = _xmas_get_cache(sam_u, pac_u->pag_u.nam_u); if ( old == u3_none ) { u3l_log("bad"); } else { @@ -1294,17 +1325,18 @@ _xmas_page_scry_cb(void* vod_p, u3_noun nun) u3_noun dat; u3x_cell(u3k(old), &tag, &dat); if ( XMAS_SCRY == tag ) { - u3_xmas_pact* res_u = c3_calloc(sizeof(*res_u)); - _xmas_respond(pac_u, res_u, u3k(hit), fra_s); + c3_y* buf_y; + + c3_w len_w = _xmas_respond(pac_u, &buf_y, hit); - _xmas_rout_pacs(res_u, u3k(u3t(dat))); + //_xmas_rout_pacs(res_u, u3k(u3t(dat))); } - _xmas_put_cache(sam_u, u3k(pax), u3nc(XMAS_ITEM, u3k(hit))); + // _xmas_put_cache(sam_u, u3k(pax), u3nc(XMAS_ITEM, u3k(hit))); u3z(old); } u3z(hit); } - u3z(pax); + // u3z(pax); } static void @@ -1322,7 +1354,7 @@ _xmas_hear_page(u3_xmas_pact* pac_u, u3_lane lan_u) c3_s fra_s; u3_noun pat = _xmas_path_with_fra(pac_u->pag_u.nam_u.pat_c, &fra_s); - u3_weak cac = _xmas_get_cache(sam_u, pat); + u3_weak cac = _xmas_get_cache(sam_u, pac_u->pag_u.nam_u); if ( u3_none != cac ) { u3_noun tag, dat; @@ -1350,47 +1382,49 @@ _xmas_hear_page(u3_xmas_pact* pac_u, u3_lane lan_u) } static void -_xmas_add_lane_to_cache(u3_xmas* sam_u, u3_noun las, u3_noun pax, u3_lane lan_u) +_xmas_add_lane_to_cache(u3_xmas* sam_u, u3_xmas_name nam_u, u3_noun las, u3_lane lan_u) { u3_noun hit = u3nq(XMAS_SCRY, _xmas_get_now(), u3nc(c3n, u3_xmas_encode_lane(lan_u)), u3k(las)); - _xmas_put_cache(sam_u, pax, hit); + _xmas_put_cache(sam_u, nam_u, hit); u3z(las); } static void _xmas_hear_peek(u3_xmas_pact* pac_u, u3_lane lan_u) { +#ifdef XMAS_DEBUG u3l_log("xmas: hear peek"); u3l_log("%hu", lan_u.por_s); + u3_assert(pac_u->typ_y == PACT_PEEK); +#endif + u3_xmas* sam_u = pac_u->sam_u; - c3_s fra_s; - u3_noun pax = _xmas_path_with_fra(pac_u->pek_u.nam_u.pat_c, &fra_s); - u3_weak hit = _xmas_get_cache(sam_u, u3k(pax)); + u3_weak hit = _xmas_get_cache(sam_u, pac_u->pek_u.nam_u); if ( u3_none != hit ) { u3_noun tag, dat; u3x_cell(u3k(hit), &tag, &dat); u3m_p("dat", dat); if ( tag == XMAS_SCRY ) { - _xmas_add_lane_to_cache(sam_u, u3k(u3t(dat)), u3k(pax), lan_u); + // _xmas_add_lane_to_cache(sam_u, u3k(u3t(dat)), u3k(pax), lan_u); } else if ( tag == XMAS_ITEM ) { u3l_log("item hit"); u3m_p("dat", u3k(dat)); u3l_log("after dat"); - _xmas_serve_cache_hit(pac_u, lan_u, fra_s, u3k(dat)); + _xmas_serve_cache_hit(pac_u, lan_u, u3k(dat)); } else { u3l_log("xmas: weird case in cache, dropping"); } u3z(hit); } else { - _xmas_add_lane_to_cache(sam_u, u3_nul, u3k(pax), lan_u); // TODO: retrieve from namespace - u3_noun sky = u3nc(c3__page, pax); - u3_pier_peek_last(sam_u->car_u.pir_u, u3_nul, c3__xx, u3_nul, sky, pac_u, _xmas_page_scry_cb); + //_xmas_add_lane_to_cache(sam_u, u3_nul, u3k(pax), lan_u); // TODO: retrieve from namespace + // u3_noun sky = u3nc(c3__page, pax); + // u3_pier_peek_last(sam_u->car_u.pir_u, u3_nul, c3__xx, u3_nul, sky, pac_u, _xmas_page_scry_cb); } u3l_log("xmas: finished hear peek"); - u3z(pax); + // u3z(pax); } static void @@ -1415,7 +1449,21 @@ _xmas_hear(u3_xmas* sam_u, u3_xmas_head hed_u; _xmas_sift_head(cur_y, &hed_u); _log_head(&hed_u); - _xmas_sift_pact(pac_u, &hed_u, hun_y + 8, (len_w - 8)); + c3_w lin_w = _xmas_sift_pact(pac_u, &hed_u, hun_y + 8, (len_w - 8)); + if( lin_w == 0 ) { +#ifdef XMAS_DEBUG + u3l_log("xmas: failed to parse packet"); + _log_pact(pac_u); +#endif + // TODO free everything + return; + + + } +#ifdef XMAS_DEBUG + _log_peek_pact(&pac_u->pek_u); + u3l_log("xmas: sifted packet"); +#endif switch ( pac_u->typ_y ) { case PACT_PEEK: { diff --git a/pkg/vere/io/xmas/cubic.c b/pkg/vere/io/xmas/cubic.c index d4830d44f1..4ccbf4dfea 100644 --- a/pkg/vere/io/xmas/cubic.c +++ b/pkg/vere/io/xmas/cubic.c @@ -1,26 +1,34 @@ /// @file /// #include "vere.h" +#include #include "noun.h" #include "ur.h" +#define FAST_CONVERGENCE 0 +const float cee_f = 0.4; +const float bet_f = 0.2; typedef struct _u3_cbic_stat { c3_w las_w; // w_last_max c3_d poc_d; // epoch start - c3_w rig_w // origin point + c3_w rig_w; // origin point c3_w del_w; // delay min c3_w win_w; // w_tcp c3_w cac_w; // ack count - + c3_w kay_w; // K + c3_w cnt_w; // cnt + c3_o tcp_o; // tcp friendliness + c3_w tcp_w; // w_tcp + c3_w max_w; // max count } u3_cbic_stat; typedef struct _u3_cong { c3_w rtt_w; // rtt c3_w rto_w; // rto - c3_w rtv-w; // rttvar + c3_w rtv_w; // rttvar c3_w wnd_w; // cwnd c3_w wnc_w; // cwnd_cnt c3_w sst_w; // ssthresh @@ -33,7 +41,7 @@ typedef struct _u3_cbic { } u3_cbic; -void u3_cbic_reset(u3_cbic* sat_u) +void u3_cbic_reset(u3_cbic_stat* sat_u) { sat_u->las_w = 0; sat_u->poc_d = 0; @@ -41,26 +49,58 @@ void u3_cbic_reset(u3_cbic* sat_u) sat_u->del_w = 0; sat_u->win_w = 0; sat_u->cac_w = 0; - } -void u3_cbic_init(u3_cbic_stats* sat_u) +void u3_cbic_init(u3_cbic_stat* sat_u) { } -void u3_cbic_update(u3_cbic* cub_u) +void u3_cbic_tcp(u3_cbic* cub_u) { - c3_w ack_w += 1; - c3_d tim_d = time(); - if ( 0 == cub_u->sat_w.poc_d ) { - cub_u->sat_w.poc_w = tim_d; - if ( cub_u->con_u.wnd_w - } else { - ifk + u3_cbic_stat sat_u = cub_u->sat_u; + u3_cong con_u = cub_u->con_u; + + sat_u.tcp_w = sat_u.tcp_w + ((c3_w)((3*bet_f)/(2-bet_f)) * (sat_u.cac_w/con_u.wnd_w)); + sat_u.cac_w = 0; + if ( sat_u.tcp_w > con_u.wnd_w ) { + sat_u.max_w = con_u.wnd_w/(sat_u.tcp_w-con_u.wnd_w); + if ( sat_u.cnt_w > sat_u.max_w ) { + sat_u.cnt_w = sat_u.max_w; + } + } +} +void u3_cbic_update(u3_cbic* cub_u) +{ + cub_u->sat_u.cac_w += 1; + u3_cong con_u = cub_u->con_u; + u3_cbic_stat sat_u = cub_u->sat_u; + c3_d tim_d = time(NULL); + + // if no epoch start set, then begin now + if ( 0 == sat_u.poc_d ) { + sat_u.poc_d = tim_d; + if ( ( con_u.wnd_w < sat_u.las_w ) && FAST_CONVERGENCE ) { + //sat_u.las_w = con_u.wnd_w * + // set origin point to w last max + } else { + sat_u.kay_w = 0; + sat_u.rig_w = con_u.wnd_w; + } + sat_u.cac_w = 1; + sat_u.win_w = con_u.wnd_w; + } + c3_d tee_d = tim_d + sat_u.del_w - sat_u.poc_d; + c3_d tar_d = sat_u.rig_w + (c3_d)(cee_f * pow((float)tee_d - (float)sat_u.kay_w, 3)); + if ( tar_d > con_u.wnd_w ) { + sat_u.cnt_w = con_u.wnd_w / ( tar_d - con_u.wnd_w ); + } else { + sat_u.cnt_w = con_u.wnd_w * 100; + } + if ( c3y == sat_u.tcp_o ) { + u3_cbic_tcp(cub_u); } - } @@ -75,7 +115,7 @@ void u3_cbic_on_ack(u3_cbic* cub_u) } if ( cub_u->con_u.wnd_w <= cub_u->con_u.sst_w ) { - cub_u->sat_u.wnd_w += 1; + cub_u->con_u.wnd_w += 1; } else { if ( cub_u->con_u.wnc_w > cub_u->con_u.wnd_w ) { cub_u->con_u.wnd_w += 1; From 65e8988f0e41a749f069373a9693f3725557d314 Mon Sep 17 00:00:00 2001 From: Liam Fitzgerald Date: Mon, 22 Jan 2024 13:36:06 -0500 Subject: [PATCH 013/430] wip --- pkg/vere/io/xmas.c | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c index 33c3f031d1..d6cbfd44b5 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/xmas.c @@ -1328,7 +1328,6 @@ _xmas_page_scry_cb(void* vod_p, u3_noun nun) c3_y* buf_y; c3_w len_w = _xmas_respond(pac_u, &buf_y, hit); - //_xmas_rout_pacs(res_u, u3k(u3t(dat))); } // _xmas_put_cache(sam_u, u3k(pax), u3nc(XMAS_ITEM, u3k(hit))); From dd8a9c621ecdb09a21bc6b5f8ee45647bc18c7ad Mon Sep 17 00:00:00 2001 From: Liam Fitzgerald Date: Tue, 23 Jan 2024 11:04:08 -0500 Subject: [PATCH 014/430] wip --- pkg/vere/io/xmas.c | 65 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c index d6cbfd44b5..b2340848aa 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/xmas.c @@ -425,6 +425,46 @@ static u3_noun _xmas_get_now() { return res; } +// refcounted +static u3_lane +_xmas_rout_to_lane(u3_xmas_pact* pac_u, u3_noun lan) { + u3_lane lan_u = {0,0}; + u3_noun tag, val; + u3x_cell(lan, &tag, &val); + + u3_assert( (c3y == tag) || (c3n == tag) ); + if ( c3y == tag ) { + u3_assert( c3y == u3a_is_cat(val) ); + u3_assert( val < 256 ); + if ( c3n == u3_Host.ops_u.net ) { + lan_u.pip_w = 0x7f000001 ; + lan_u.por_s = _ames_czar_port(val); + u3z(lan); + } + + } else { + lan_u = u3_xmas_decode_lane(u3k(val)); + // if in local-only mode, don't send remote packets + // + if ( (c3n == u3_Host.ops_u.net) && (0x7f000001 != lan_u.pip_w) ) { + lan_u = {0,0}; + } + // if the lane is uninterpretable, silently drop the packet + // + else if ( 0 == lan_u.por_s ) { + if ( u3C.wag_w & u3o_verbose ) { + u3l_log("ames: inscrutable lane"); + } + suc_o = c3n; + } else { + pac_u->rut_u.typ_y = ROUT_OTHER; + pac_u->rut_u.lan_u = lan_u; + } + } + u3z(lan); + return suc_o; +} + // refcounted static c3_o _xmas_rout_pact(u3_xmas_pact* pac_u, u3_noun lan) { @@ -776,6 +816,28 @@ static void _xmas_send(u3_xmas_pact* pac_u) _xmas_send_buf(sam_u, pac_u->rut_u.lan_u, buf_y, siz_w); } +static c3_o +_xmas_rout_pacs(c3_y* buf_y, c3_w len_w, u3_noun las) +{ + c3_o suc_o = c3n; + u3_noun lan, t = las; + while ( t != u3_nul ) { + u3x_cell(t, &lan, &t); + // if ( c3n == u3r_cell(t, &lan, &t) ) { + // break; + // } + if ( c3n == _xmas_rout_pact(pac_u, u3k(lan)) ) { + u3l_log("xmas: failed to set route"); + } else { + u3l_log("xmas_send"); + _xmas_send_buf(pac_u); + suc_o = c3y; + } + } + u3z(las); + return suc_o; +} + static c3_o _xmas_rout_pacs(u3_xmas_pact* pac_u, u3_noun las) { @@ -790,7 +852,7 @@ _xmas_rout_pacs(u3_xmas_pact* pac_u, u3_noun las) u3l_log("xmas: failed to set route"); } else { u3l_log("xmas_send"); - _xmas_send(pac_u); + _xmas_send_buf(pac_u); suc_o = c3y; } } @@ -1267,6 +1329,7 @@ _xmas_respond(u3_xmas_pact* req_u, c3_y** buf_y, u3_noun hit) u3r_bytes(0, len, *buf_y, dat); u3z(hit); + return len; } static void From a1519217c47a271077b05da2b8d64d6880c91c2c Mon Sep 17 00:00:00 2001 From: Liam Fitzgerald Date: Tue, 23 Jan 2024 11:05:01 -0500 Subject: [PATCH 015/430] xmas: move seralisation into header --- pkg/vere/io/xmas/serial.h | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 pkg/vere/io/xmas/serial.h diff --git a/pkg/vere/io/xmas/serial.h b/pkg/vere/io/xmas/serial.h new file mode 100644 index 0000000000..e69de29bb2 From fcd0e4cd8daf09302d3c7b7e26243ce1ae173d79 Mon Sep 17 00:00:00 2001 From: Liam Fitzgerald Date: Tue, 23 Jan 2024 14:30:46 -0500 Subject: [PATCH 016/430] add tests --- pkg/vere/BUILD.bazel | 4 +- pkg/vere/io/xmas.c | 272 +++++++++++++++++++++++++++---------------- 2 files changed, 175 insertions(+), 101 deletions(-) diff --git a/pkg/vere/BUILD.bazel b/pkg/vere/BUILD.bazel index 4e58a2c192..d69e49922d 100644 --- a/pkg/vere/BUILD.bazel +++ b/pkg/vere/BUILD.bazel @@ -187,13 +187,13 @@ cc_test( cc_test( name = "xmas_tests", timeout = "short", - srcs = ["xmas_tests.c", "io/xmas.c"], + srcs = ["io/xmas.c"], features = select({ "@platforms//os:linux": ["fully_static_link"], "//conditions:default": [], }), - visibility = ["//visibility:private"], deps = [":vere"], + defines = ["XMAS_TEST"] ) diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c index b2340848aa..c624845a59 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/xmas.c @@ -1,10 +1,13 @@ /// @file #include "vere.h" +#include "ivory.h" #include "noun.h" #include "ur.h" +#include #include +#include #include #include #include @@ -16,6 +19,8 @@ #define FINE_PATH_MAX 384 // longest allowed scry path #define HEAD_SIZE 4 // header size in bytes #define PACT_SIZE 1472 +#define RED_TEXT "\033[0;31m" +#define DEF_TEXT "\033[0m" // pending interest sentinels #define XMAS_ITEM 1 // cached item @@ -30,6 +35,7 @@ static c3_y XMAS_COOKIE[4] = { 0x5e, 0x1d, 0xad, 0x51 }; #define SIFT_VAR(dest, src, len) dest = 0; for(int i = 0; i < len; i++ ) { dest |= ((src + i) >> (8*i)); } #define CHECK_BOUNDS(cur) if ( len_w < cur ) { u3l_log("xmas: failed parse (%i,%i) at line %i", len_w, cur, __LINE__); return 0; } +#define safe_dec(num) num == 0 ? num : num - 1; /* _u3_xmas: next generation networking */ @@ -76,8 +82,8 @@ typedef struct _u3_xmas_name_meta { // reserved (2 bits) c3_y ran_y; // rank (2 bits) c3_y rif_y; // rift-len (2 bits) - c3_o pat_o; // path length length (1bit) - c3_o boq_o; // 0b1 if custom, 0b0 implicitly 13 (1bit) + c3_o pat_b; // path length length (1bit) + c3_o boq_b; // 0b1 if custom, 0b0 implicitly 13 (1bit) c3_o fra_y; // fragment number length (2bit) } u3_xmas_name_meta; @@ -215,6 +221,12 @@ _log_buf(c3_y* buf_y, c3_w len_w) static void _log_peek_pact(u3_xmas_peek_pact* pac_u) { + u3l_log("meta"); + u3l_log("rank: %u", pac_u->nam_u.met_u.ran_y); + u3l_log("rift length: %u", pac_u->nam_u.met_u.rif_y); + u3l_log("custom bloq: %c", pac_u->nam_u.met_u.boq_b == 1 ? 'y' : 'n'); + u3l_log("frag num length: %u", pac_u->nam_u.met_u.fra_y); + u3l_log("path length length: %u", pac_u->nam_u.met_u.pat_b); u3l_log("publisher: %s", u3r_string(u3dc("scot", c3__p, u3i_chubs(2, pac_u->nam_u.her_d)))); u3l_log("path len: %u", pac_u->nam_u.pat_s); u3l_log("path: %s", pac_u->nam_u.pat_c); @@ -425,45 +437,6 @@ static u3_noun _xmas_get_now() { return res; } -// refcounted -static u3_lane -_xmas_rout_to_lane(u3_xmas_pact* pac_u, u3_noun lan) { - u3_lane lan_u = {0,0}; - u3_noun tag, val; - u3x_cell(lan, &tag, &val); - - u3_assert( (c3y == tag) || (c3n == tag) ); - if ( c3y == tag ) { - u3_assert( c3y == u3a_is_cat(val) ); - u3_assert( val < 256 ); - if ( c3n == u3_Host.ops_u.net ) { - lan_u.pip_w = 0x7f000001 ; - lan_u.por_s = _ames_czar_port(val); - u3z(lan); - } - - } else { - lan_u = u3_xmas_decode_lane(u3k(val)); - // if in local-only mode, don't send remote packets - // - if ( (c3n == u3_Host.ops_u.net) && (0x7f000001 != lan_u.pip_w) ) { - lan_u = {0,0}; - } - // if the lane is uninterpretable, silently drop the packet - // - else if ( 0 == lan_u.por_s ) { - if ( u3C.wag_w & u3o_verbose ) { - u3l_log("ames: inscrutable lane"); - } - suc_o = c3n; - } else { - pac_u->rut_u.typ_y = ROUT_OTHER; - pac_u->rut_u.lan_u = lan_u; - } - } - u3z(lan); - return suc_o; -} // refcounted static c3_o @@ -640,54 +613,71 @@ _xmas_etch_page_pact(c3_y* buf_y, u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u) static c3_w _xmas_etch_peek_pact(c3_y* buf_y, u3_xmas_peek_pact* pac_u, u3_xmas_head* hed_u, c3_o tag_o) { +#ifdef XMAS_DEBUG + if ( strlen(pac_u->nam_u.pat_c) != pac_u->nam_u.pat_s ) { + u3l_log( + "xmas: packet validation failure, with path %s expected length %i, got %i", + pac_u->nam_u.pat_c, + pac_u->nam_u.pat_s, + strlen(pac_u->nam_u.pat_c) + ); + u3m_bail(c3__foul); + } + +#endif c3_w cur_w = 0; - u3_xmas_name nam_u = pac_u->nam_u; - u3_xmas_name_meta met_u = nam_u.met_u; + u3_xmas_name* nam_u = &pac_u->nam_u; + nam_u->met_u.ran_y = safe_dec(u3r_met(0, u3r_met(4, u3i_chubs(2, nam_u->her_d)))); + nam_u->met_u.rif_y = safe_dec(u3r_met(3, u3i_word(nam_u->rif_w))); + nam_u->met_u.fra_y = safe_dec(u3r_met(3, u3i_word(nam_u->fra_w))); + nam_u->met_u.boq_b = nam_u->boq_y == 13 ? 0 : 1; + nam_u->met_u.pat_b = nam_u->pat_s > 0xFF ? 1 : 0; // TODO: double check reserved bits - c3_y met_y = (met_u.ran_y & 0x3) >> 0 - ^ (met_u.rif_y & 0x3) >> 2 - ^ (met_u.pat_o & 0x1) >> 4 - ^ (met_u.boq_o & 0x1) >> 5 - ^ (met_u.fra_y & 0x3) >> 6; + c3_y met_y = (nam_u->met_u.ran_y & 0x3) >> 0 + ^ (nam_u->met_u.rif_y & 0x3) >> 2 + ^ (nam_u->met_u.pat_b & 0x1) >> 4 + ^ (nam_u->met_u.boq_b & 0x1) >> 5 + ^ (nam_u->met_u.fra_y & 0x3) >> 6; buf_y[cur_w] = met_y; - //shift + u3_xmas_name_meta met_u = nam_u->met_u; + //ship cur_w++; - c3_y her_y = 1 << (met_u.ran_y+1); - _ames_ship_of_chubs(nam_u.her_d, her_y, buf_y + cur_w); + c3_y her_y = 1 << (met_u.ran_y + 1); + _ames_ship_of_chubs(nam_u->her_d, her_y, buf_y + cur_w); cur_w += her_y; // rift c3_y rif_y = met_u.rif_y + 1; for ( int i = 0; i < rif_y; i++) { - buf_y[cur_w] = (nam_u.rif_w >> (8*i)) & 0xff; + buf_y[cur_w] = (nam_u->rif_w >> (8*i)) & 0xff; cur_w++; } // path length - c3_y pat_y = 1 >> met_u.pat_o; + c3_y pat_y = 1 >> met_u.pat_b; for ( int i = 0; i < pat_y; i++ ) { - buf_y[cur_w] = (nam_u.pat_s >> (8*i)) & 0xff; + buf_y[cur_w] = (nam_u->pat_s >> (8*i)) & 0xff; cur_w++; } // path - memcpy(buf_y + cur_w, nam_u.pat_c, nam_u.pat_s); - cur_w += nam_u.pat_s; + memcpy(buf_y + cur_w, nam_u->pat_c, nam_u->pat_s); + cur_w += nam_u->pat_s; // possible bloq size - if ( 1 == met_u.boq_o ) { - buf_y[cur_w] = nam_u.boq_y; + if ( 1 == met_u.boq_b ) { + buf_y[cur_w] = nam_u->boq_y; cur_w++; } c3_y fra_y = met_u.fra_y + 1; - for( int i = 0; i < met_u.fra_y; i++ ) { - buf_y[cur_w] = (nam_u.fra_w >> (8*i)) & 0xff; + for( int i = 0; i < fra_y; i++ ) { + buf_y[cur_w] = (nam_u->fra_w >> (8*i)) & 0xff; cur_w++; } @@ -817,11 +807,11 @@ static void _xmas_send(u3_xmas_pact* pac_u) } static c3_o -_xmas_rout_pacs(c3_y* buf_y, c3_w len_w, u3_noun las) +_xmas_rout_bufs(c3_y* buf_y, c3_w len_w, u3_noun las) { c3_o suc_o = c3n; u3_noun lan, t = las; - while ( t != u3_nul ) { + /*while ( t != u3_nul ) { u3x_cell(t, &lan, &t); // if ( c3n == u3r_cell(t, &lan, &t) ) { // break; @@ -833,7 +823,7 @@ _xmas_rout_pacs(c3_y* buf_y, c3_w len_w, u3_noun las) _xmas_send_buf(pac_u); suc_o = c3y; } - } + }*/ u3z(las); return suc_o; } @@ -852,7 +842,7 @@ _xmas_rout_pacs(u3_xmas_pact* pac_u, u3_noun las) u3l_log("xmas: failed to set route"); } else { u3l_log("xmas_send"); - _xmas_send_buf(pac_u); + //_xmas_send_buf(pac_u); suc_o = c3y; } } @@ -1046,7 +1036,7 @@ _xmas_io_exit(u3_auto* car_u) } static c3_w -_xmas_sift_page_pact(u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, c3_w len_w) +_xmas_sift_page_pact(u3_xmas_page_pact* pac_u, c3_y* buf_y, c3_w len_w) { c3_w cur_w = 0; @@ -1087,17 +1077,13 @@ _xmas_sift_name(u3_xmas_name* nam_u, c3_y* buf_y, c3_w len_w) _log_buf(buf_y, len_w); #endif - - if( len_w < 1) { - return 0; - } c3_w cur_w = 0; CHECK_BOUNDS(cur_w + 1); - c3_w met_y = buf_y[cur_w]; + c3_y met_y = buf_y[cur_w]; nam_u->met_u.ran_y = (met_y >> 0) & 0x3; nam_u->met_u.rif_y = (met_y >> 2) & 0x3; - nam_u->met_u.pat_o = (met_y >> 4) & 0x1; - nam_u->met_u.boq_o = (met_y >> 5) & 0x1; + nam_u->met_u.pat_b = (met_y >> 4) & 0x1; + nam_u->met_u.boq_b = (met_y >> 5) & 0x1; nam_u->met_u.fra_y = (met_y >> 6) & 0x3; cur_w += 1; @@ -1106,32 +1092,28 @@ _xmas_sift_name(u3_xmas_name* nam_u, c3_y* buf_y, c3_w len_w) _ames_ship_to_chubs(nam_u->her_d, her_y, buf_y + cur_w); cur_w += her_y; - c3_y rif_y = 2 << nam_u->met_u.rif_y; - //SIFT_VAR(nam_u->rif_w, (buf_y + cur_w), rif_y); - /*CHECK_BOUNDS(cur_w + rif_y) + c3_y rif_y = nam_u->met_u.rif_y + 1; + CHECK_BOUNDS(cur_w + rif_y) for( int i = 0; i < rif_y; i++ ) { nam_u->rif_w |= (buf_y[cur_w] << (8*i)); cur_w++; - }*/ + } - c3_y pat_y = 1 << nam_u->met_u.pat_o; + c3_y pat_y = 1 << nam_u->met_u.pat_b; CHECK_BOUNDS(cur_w + pat_y) - _log_buf(buf_y + cur_w, len_w - cur_w); nam_u->pat_s = 0; for ( int i = 0; i < pat_y; i++ ) { nam_u->pat_s |= (buf_y[cur_w] << (8*i)); cur_w++; } - _log_buf(buf_y + cur_w, len_w - cur_w); nam_u->pat_c = c3_calloc(nam_u->pat_s + 1); // unix string for ease of manipulation CHECK_BOUNDS(nam_u->pat_s + cur_w); memcpy(nam_u->pat_c, buf_y + cur_w, nam_u->pat_s); nam_u->pat_c[nam_u->pat_s] = 0; cur_w += nam_u->pat_s; - _log_buf(buf_y + cur_w, len_w - cur_w); - if ( c3n == nam_u->met_u.boq_o ) { + if ( 1 == nam_u->met_u.boq_b ) { CHECK_BOUNDS(cur_w + 1); nam_u->boq_y = buf_y[cur_w]; cur_w++; @@ -1139,13 +1121,17 @@ _xmas_sift_name(u3_xmas_name* nam_u, c3_y* buf_y, c3_w len_w) nam_u->boq_y = 13; } - c3_y fra_y = 1 << nam_u->met_u.fra_y; - _log_buf(buf_y + cur_w, len_w - cur_w); + c3_y fra_y = nam_u->met_u.fra_y + 1; CHECK_BOUNDS(cur_w + fra_y) nam_u->fra_w = 0; for( int i = 0; i < fra_y; i++ ) { nam_u->fra_w |= (buf_y[cur_w] << (8*i)); - + cur_w++; + } + if ( cur_w < len_w ) { + u3l_log("xmas: failed to consume entire packet"); + _log_buf(buf_y + cur_w, len_w - cur_w); + return 0; } return cur_w; @@ -1153,7 +1139,7 @@ _xmas_sift_name(u3_xmas_name* nam_u, c3_y* buf_y, c3_w len_w) static c3_w -_xmas_sift_peek_pact(u3_xmas_peek_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, c3_w len_w, c3_o tag_o) +_xmas_sift_peek_pact(u3_xmas_peek_pact* pac_u, c3_y* buf_y, c3_w len_w) { c3_w siz_w = _xmas_sift_name(&pac_u->nam_u, buf_y, len_w); /*if (siz_w > len_w ) { @@ -1164,12 +1150,12 @@ _xmas_sift_peek_pact(u3_xmas_peek_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, } static c3_w -_xmas_sift_poke_pact(u3_xmas_poke_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, c3_w len_w) +_xmas_sift_poke_pact(u3_xmas_poke_pact* pac_u, c3_y* buf_y, c3_w len_w) { c3_w cur_w = 0; // Peek portion pac_u->pek_u = c3_calloc(sizeof(*(pac_u->pek_u))); - cur_w += _xmas_sift_peek_pact(pac_u->pek_u, hed_u, buf_y, len_w, c3n); + cur_w += _xmas_sift_peek_pact(pac_u->pek_u, buf_y, len_w); // Total fragments pac_u->tot_w = _ames_sift_word(buf_y + cur_w); @@ -1185,26 +1171,34 @@ _xmas_sift_poke_pact(u3_xmas_poke_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, } static c3_w -_xmas_sift_pact(u3_xmas_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, c3_w len_w) +_xmas_sift_pact(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w) { - pac_u->typ_y = hed_u->typ_y; + u3_xmas_head hed_u; + if( len_w < 8 ) { + u3l_log("xmas: attempted to parse overly short packet of size %u", len_w); + } + + _xmas_sift_head(buf_y, &hed_u); + pac_u->typ_y = hed_u.typ_y; c3_w res_w = 0; - switch ( hed_u->typ_y ) { + buf_y += 8; + len_w -= 8; + switch ( pac_u->typ_y ) { case PACT_PEEK: { - res_w = _xmas_sift_peek_pact(&pac_u->pek_u, hed_u, buf_y, len_w, c3y); + res_w = _xmas_sift_peek_pact(&pac_u->pek_u, buf_y, len_w); } break; case PACT_PAGE: { - res_w = _xmas_sift_page_pact(&pac_u->pag_u, hed_u, buf_y, len_w); + res_w = _xmas_sift_page_pact(&pac_u->pag_u, buf_y, len_w); } break; case PACT_POKE: { - res_w = _xmas_sift_poke_pact(&pac_u->pok_u, hed_u, buf_y, len_w); + res_w = _xmas_sift_poke_pact(&pac_u->pok_u, buf_y, len_w); } break; default: { u3l_log("xmas: received unknown packet type"); break; } } - u3_assert(res_w < len_w ); + u3_assert(res_w <= len_w ); return res_w; } @@ -1303,7 +1297,6 @@ _xmas_czar(u3_xmas_pact* pac_u) c3_o lok_o = _xmas_add_galaxy_pend(sam_u, her_y, pat); - // TODO: actually resolve DNS u3z(pat); } @@ -1508,10 +1501,7 @@ _xmas_hear(u3_xmas* sam_u, pac_u = c3_calloc(sizeof(*pac_u)); pac_u->sam_u = sam_u; - u3_xmas_head hed_u; - _xmas_sift_head(cur_y, &hed_u); - _log_head(&hed_u); - c3_w lin_w = _xmas_sift_pact(pac_u, &hed_u, hun_y + 8, (len_w - 8)); + c3_w lin_w = _xmas_sift_pact(pac_u, hun_y, len_w); if( lin_w == 0 ) { #ifdef XMAS_DEBUG u3l_log("xmas: failed to parse packet"); @@ -1697,6 +1687,90 @@ u3_xmas_io_init(u3_pier* pir_u) return car_u; } +#define XMAS_TEST +#ifdef XMAS_TEST + +static void +_test_sift_peek() +{ + u3_xmas_pact pac_u; + memset(&pac_u,0, sizeof(u3_xmas_pact)); + pac_u.typ_y = PACT_PEEK;; + u3l_log("checking sift/etch idempotent"); + u3_xmas_name* nam_u = &pac_u.pek_u.nam_u; + u3_noun her = u3v_wish("~hastuc-dibtux"); + u3r_chubs(0, 2, nam_u->her_d, her); + nam_u->rif_w = 15; + nam_u->pat_c = "foo/bar"; + nam_u->pat_s = strlen(nam_u->pat_c); + nam_u->boq_y = 13; + nam_u->fra_w = 54; + + c3_y* buf_y = c3_calloc(PACT_SIZE); + c3_w len_w =_xmas_etch_pact(buf_y, &pac_u, her); + u3_xmas_pact nex_u; + memset(&nex_u, 0, sizeof(u3_xmas_pact)); + _xmas_sift_pact(&nex_u, buf_y, len_w); + + if ( 0 != memcmp(nex_u.pek_u.nam_u.pat_c, pac_u.pek_u.nam_u.pat_c, pac_u.pek_u.nam_u.pat_s) ) { + u3l_log(RED_TEXT); + u3l_log("path mismatch"); + u3l_log("got"); + _log_pact(&nex_u); + u3l_log("expected"); + _log_pact(&pac_u); + exit(1); + + + } + nex_u.pek_u.nam_u.pat_c = 0; + pac_u.pek_u.nam_u.pat_c = 0; + + if ( 0 != memcmp(&nex_u, &pac_u, sizeof(u3_xmas_pact)) ) { + u3l_log(RED_TEXT); + u3l_log("mismatch"); + u3l_log("got"); + _log_pact(&nex_u); + u3l_log("expected"); + _log_pact(&pac_u); + exit(1); + } +} + + +static void +_setup() +{ + c3_d len_d = u3_Ivory_pill_len; + c3_y* byt_y = u3_Ivory_pill; + u3_cue_xeno* sil_u; + u3_weak pil; + + u3C.wag_w |= u3o_hashless; + u3m_boot_lite(1 << 26); + sil_u = u3s_cue_xeno_init_with(ur_fib27, ur_fib28); + if ( u3_none == (pil = u3s_cue_xeno_with(sil_u, len_d, byt_y)) ) { + printf("*** fail _setup 1\n"); + exit(1); + } + u3s_cue_xeno_done(sil_u); + if ( c3n == u3v_boot_lite(pil) ) { + printf("*** fail _setup 2\n"); + exit(1); + } +} + +int main() +{ + + _setup(); + + _test_sift_peek(); + return 0; +} + +#endif + From 096af8adbfaa4b1fb4d6e93f96e40d3f0a3bd3f4 Mon Sep 17 00:00:00 2001 From: Liam Fitzgerald Date: Thu, 25 Jan 2024 12:07:28 -0500 Subject: [PATCH 017/430] xmas: against jb/dire --- pkg/c3/motes.h | 3 + pkg/vere/io/xmas.c | 594 +++++++++++++++++++++++++-------------------- 2 files changed, 328 insertions(+), 269 deletions(-) diff --git a/pkg/c3/motes.h b/pkg/c3/motes.h index f2d546c013..812767c089 100644 --- a/pkg/c3/motes.h +++ b/pkg/c3/motes.h @@ -68,6 +68,7 @@ # define c3__bcts c3_s4('b','c','t','s') # define c3__bczp c3_s4('b','c','z','p') # define c3__bead c3_s4('b','e','a','d') +# define c3__beam c3_s4('b','e','a','m') # define c3__bean c3_s4('b','e','a','n') # define c3__bear c3_s4('b','e','a','r') # define c3__bede c3_s4('b','e','d','e') @@ -538,6 +539,7 @@ # define c3__head c3_s4('h','e','a','d') # define c3__heal c3_s4('h','e','a','l') # define c3__hear c3_s4('h','e','a','r') +# define c3__heer c3_s4('h','e','e','r') # define c3__hela c3_s4('h','e','l','a') # define c3__helm c3_s4('h','e','l','m') # define c3__helo c3_s4('h','e','l','o') @@ -1014,6 +1016,7 @@ # define c3__sell c3_s4('s','e','l','l') # define c3__semp c3_s4('s','e','m','p') # define c3__send c3_s4('s','e','n','d') +# define c3__sent c3_s4('s','e','n','t') # define c3__seq c3_s3('s','e','q') # define c3__serd c3_s4('s','e','r','d') # define c3__serf c3_s4('s','e','r','f') diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c index c624845a59..caeb43183c 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/xmas.c @@ -5,6 +5,7 @@ #include "noun.h" #include "ur.h" +#include #include #include #include @@ -219,19 +220,33 @@ _log_buf(c3_y* buf_y, c3_w len_w) } static void -_log_peek_pact(u3_xmas_peek_pact* pac_u) +_log_name(u3_xmas_name* nam_u) { u3l_log("meta"); - u3l_log("rank: %u", pac_u->nam_u.met_u.ran_y); - u3l_log("rift length: %u", pac_u->nam_u.met_u.rif_y); - u3l_log("custom bloq: %c", pac_u->nam_u.met_u.boq_b == 1 ? 'y' : 'n'); - u3l_log("frag num length: %u", pac_u->nam_u.met_u.fra_y); - u3l_log("path length length: %u", pac_u->nam_u.met_u.pat_b); - u3l_log("publisher: %s", u3r_string(u3dc("scot", c3__p, u3i_chubs(2, pac_u->nam_u.her_d)))); - u3l_log("path len: %u", pac_u->nam_u.pat_s); - u3l_log("path: %s", pac_u->nam_u.pat_c); - u3l_log("bloq: %u", pac_u->nam_u.boq_y); - u3l_log("frag: %u", pac_u->nam_u.fra_w); + u3l_log("rank: %u", nam_u->met_u.ran_y); + u3l_log("rift length: %u", nam_u->met_u.rif_y); + u3l_log("custom bloq: %c", nam_u->met_u.boq_b == 1 ? 'y' : 'n'); + u3l_log("frag num length: %u", nam_u->met_u.fra_y); + u3l_log("path length length: %u", nam_u->met_u.pat_b); + u3l_log("publisher: %s", u3r_string(u3dc("scot", c3__p, u3i_chubs(2, nam_u->her_d)))); + u3l_log("path len: %u", nam_u->pat_s); + u3l_log("path: %s", nam_u->pat_c); + u3l_log("bloq: %u", nam_u->boq_y); + u3l_log("frag: %u", nam_u->fra_w); +} + + + +static void +_log_peek_pact(u3_xmas_peek_pact* pac_u) +{ + _log_name(&pac_u->nam_u); +} + +static void +_log_page_pact(u3_xmas_page_pact *pac_u) +{ + _log_name(&pac_u->nam_u); } static void @@ -242,6 +257,11 @@ _log_pact(u3_xmas_pact* pac_u) _log_peek_pact(&pac_u->pek_u); break; }; + case PACT_PAGE: { + _log_page_pact(&pac_u->pag_u); + break; + + } default: { u3l_log("logging not implemented for %i", pac_u->typ_y); break; @@ -397,10 +417,10 @@ u3_xmas_lane_to_chub(u3_lane lan) { /* u3_xmas_encode_lane(): serialize lane to noun */ static u3_atom -u3_xmas_encode_lane(u3_lane lan) { +u3_xmas_encode_lane(u3_lane lan_u) { // [%| p=@] // [%& p=@pC] - return u3i_chub(u3_xmas_lane_to_chub(lan)); + return u3nt(c3__if, u3i_word(lan_u.pip_w), lan_u.por_s); } @@ -551,6 +571,184 @@ _xmas_sift_head(c3_y buf_y[8], u3_xmas_head* hed_u) }*/ } +static c3_w +_xmas_sift_name(u3_xmas_name* nam_u, c3_y* buf_y, c3_w len_w) +{ +#ifdef XMAS_DEBUG + u3l_log("xmas: sifting name %i", len_w); + _log_buf(buf_y, len_w); +#endif + + c3_w cur_w = 0; + CHECK_BOUNDS(cur_w + 1); + c3_y met_y = buf_y[cur_w]; + nam_u->met_u.ran_y = (met_y >> 0) & 0x3; + nam_u->met_u.rif_y = (met_y >> 2) & 0x3; + nam_u->met_u.pat_b = (met_y >> 4) & 0x1; + nam_u->met_u.boq_b = (met_y >> 5) & 0x1; + nam_u->met_u.fra_y = (met_y >> 6) & 0x3; + cur_w += 1; + + c3_y her_y = 2 << nam_u->met_u.ran_y; + CHECK_BOUNDS(cur_w + her_y) + _ames_ship_to_chubs(nam_u->her_d, her_y, buf_y + cur_w); + cur_w += her_y; + + c3_y rif_y = nam_u->met_u.rif_y + 1; + CHECK_BOUNDS(cur_w + rif_y) + for( int i = 0; i < rif_y; i++ ) { + nam_u->rif_w |= (buf_y[cur_w] << (8*i)); + cur_w++; + } + + c3_y pat_y = 1 << nam_u->met_u.pat_b; + CHECK_BOUNDS(cur_w + pat_y) + nam_u->pat_s = 0; + for ( int i = 0; i < pat_y; i++ ) { + nam_u->pat_s |= (buf_y[cur_w] << (8*i)); + cur_w++; + } + + nam_u->pat_c = c3_calloc(nam_u->pat_s + 1); // unix string for ease of manipulation + CHECK_BOUNDS(nam_u->pat_s + cur_w); + memcpy(nam_u->pat_c, buf_y + cur_w, nam_u->pat_s); + nam_u->pat_c[nam_u->pat_s] = 0; + cur_w += nam_u->pat_s; + + if ( 1 == nam_u->met_u.boq_b ) { + CHECK_BOUNDS(cur_w + 1); + nam_u->boq_y = buf_y[cur_w]; + cur_w++; + } else { + nam_u->boq_y = 13; + } + + c3_y fra_y = nam_u->met_u.fra_y + 1; + u3l_log("fra_y: %i", fra_y); + CHECK_BOUNDS(cur_w + fra_y) + nam_u->fra_w = 0; + for( int i = 0; i < fra_y; i++ ) { + nam_u->fra_w |= (buf_y[cur_w] << (8*i)); + cur_w++; + } + + return cur_w; +} + + + +static c3_w +_xmas_sift_page_pact(u3_xmas_page_pact* pac_u, c3_y* buf_y, c3_w len_w) +{ + c3_w cur_w = 0; + cur_w += _xmas_sift_name(&pac_u->nam_u, buf_y, len_w); + + CHECK_BOUNDS(cur_w + 1); + + return cur_w; + + /*// next hop + memcpy(pac_u->hop_y, buf_y, 6); + cur_w += 6; + + // path length + pac_u->pat_s = _ames_sift_short(buf_y + cur_w); + cur_w += 2; + + // path contents + pac_u->pat_c = c3_calloc(pac_u->pat_s + 1); + memcpy(pac_u->pat_c, buf_y + cur_w, pac_u->pat_s); + pac_u->pat_c[pac_u->pat_s] = '\0'; + cur_w += pac_u->pat_s + 1; + + // total fragments + pac_u->tot_w = _ames_sift_word(buf_y + cur_w); + cur_w += 4; + + // Authenticator + memcpy(pac_u->aut_y, buf_y + cur_w, 96); + cur_w += 96; + + c3_w dat_w = 1024; // len_w - cur_w; + pac_u->dat_y = c3_calloc(dat_w); + memcpy(pac_u->dat_y, buf_y + cur_w, dat_w); + */ + return 0; +} + + +static c3_w +_xmas_sift_peek_pact(u3_xmas_peek_pact* pac_u, c3_y* buf_y, c3_w len_w) +{ + c3_w siz_w = _xmas_sift_name(&pac_u->nam_u, buf_y, len_w); + if ( siz_w < len_w ) { + u3l_log("xmas: failed to consume entire packet"); + _log_buf(buf_y + siz_w, len_w - siz_w); + return 0; + } + + /*if (siz_w > len_w ) { + u3l_log("xmas: buffer overrun (FIXME)"); + u3m_bail(c3__foul); + }*/ + return siz_w; +} + +static c3_w +_xmas_sift_poke_pact(u3_xmas_poke_pact* pac_u, c3_y* buf_y, c3_w len_w) +{ + c3_w cur_w = 0; + // Peek portion + pac_u->pek_u = c3_calloc(sizeof(*(pac_u->pek_u))); + cur_w += _xmas_sift_peek_pact(pac_u->pek_u, buf_y, len_w); + + // Total fragments + pac_u->tot_w = _ames_sift_word(buf_y + cur_w); + cur_w += 4; + + // Authenticator + memcpy(pac_u->aut_y, buf_y + cur_w, 96); + cur_w += 96; + + // Datum + memcpy(pac_u->dat_y, buf_y + cur_w, len_w - cur_w); + return cur_w; +} + +static c3_w +_xmas_sift_pact(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w) +{ + u3_xmas_head hed_u; + if( len_w < 8 ) { + u3l_log("xmas: attempted to parse overly short packet of size %u", len_w); + } + + _xmas_sift_head(buf_y, &hed_u); + pac_u->typ_y = hed_u.typ_y; + c3_w res_w = 0; + buf_y += 8; + len_w -= 8; + switch ( pac_u->typ_y ) { + case PACT_PEEK: { + res_w = _xmas_sift_peek_pact(&pac_u->pek_u, buf_y, len_w); + } break; + case PACT_PAGE: { + res_w = _xmas_sift_page_pact(&pac_u->pag_u, buf_y, len_w); + } break; + case PACT_POKE: { + res_w = _xmas_sift_poke_pact(&pac_u->pok_u, buf_y, len_w); + } break; + default: { + u3l_log("xmas: received unknown packet type"); + break; + } + } + //u3_assert(res_w <= len_w ); + return res_w; +} + + + static void _xmas_etch_head(u3_xmas_head* hed_u, c3_y buf_y[8]) { @@ -806,24 +1004,54 @@ static void _xmas_send(u3_xmas_pact* pac_u) _xmas_send_buf(sam_u, pac_u->rut_u.lan_u, buf_y, siz_w); } +static u3_lane +_realise_lane(u3_noun lan) { + u3_lane lan_u; + lan_u.por_s = 0; + lan_u.pip_w = 0; + + if ( c3y == u3a_is_cat(lan) ) { + u3_assert( lan < 256 ); + if ( (c3n == u3_Host.ops_u.net) ) { + lan_u.pip_w = 0x7f000001 ; + lan_u.por_s = _ames_czar_port(lan); + } + } else { + u3_noun tag, pip, por; + u3x_trel(lan, &tag, &pip, &por); + if ( tag == c3__if ) { + lan_u.pip_w = u3i_word(pip); + u3_assert( c3y == u3a_is_cat(por) && por <= 0xFFFF); + lan_u.por_s = por; + } else { + u3l_log("xmas: inscrutable lane"); + u3m_p("lane", lan); + } + u3z(lan); + } + return lan_u; +} + static c3_o -_xmas_rout_bufs(c3_y* buf_y, c3_w len_w, u3_noun las) +_xmas_rout_bufs(u3_xmas* sam_u, c3_y* buf_y, c3_w len_w, u3_noun las) { + u3l_log("routing bufs"); c3_o suc_o = c3n; u3_noun lan, t = las; - /*while ( t != u3_nul ) { + while ( t != u3_nul ) { u3x_cell(t, &lan, &t); - // if ( c3n == u3r_cell(t, &lan, &t) ) { - // break; - // } - if ( c3n == _xmas_rout_pact(pac_u, u3k(lan)) ) { - u3l_log("xmas: failed to set route"); - } else { - u3l_log("xmas_send"); - _xmas_send_buf(pac_u); - suc_o = c3y; + u3_lane lan_u = _realise_lane(u3k(lan)); + #ifdef XMAS_DEBUG + u3l_log("sending to ip: %x, port: %u", lan_u.pip_w, lan_u.por_s); + + #endif /* ifdef XMAS_DEBUG + u3l_log("sending to ip: %x, port: %u", lan_u.pip_w, lan_u.por_s); */ + if ( lan_u.por_s == 0 ) { + u3l_log("xmas: failed to realise lane"); + } else { + _xmas_send_buf(sam_u, lan_u, buf_y, len_w); } - }*/ + } u3z(las); return suc_o; } @@ -851,8 +1079,9 @@ _xmas_rout_pacs(u3_xmas_pact* pac_u, u3_noun las) } static void -_xmas_ef_pact(u3_xmas* sam_u, u3_noun las, u3_noun pac) +_xmas_ef_send(u3_xmas* sam_u, u3_noun las, u3_noun pac) { + u3l_log("sending"); u3_noun len, dat; u3x_cell(pac, &len, &dat); c3_w len_w = u3r_met(3, dat); @@ -864,49 +1093,15 @@ _xmas_ef_pact(u3_xmas* sam_u, u3_noun las, u3_noun pac) } c3_y* buf_y = c3_calloc(len); u3r_bytes(0, len, buf_y, dat); + u3_xmas_pact pac_u; + _xmas_sift_pact(&pac_u, buf_y, len); + u3l_log("packet: "); + _log_pact(&pac_u); c3_o suc_o = c3n; u3_noun lan, t = las; - while ( t != u3_nul ) { - u3x_cell(t, &lan, &t); - // if ( c3n == u3r_cell(t, &lan, &t) ) { - // break; - // } - u3l_log("xmas_send"); - u3_noun tag, val; - u3x_cell(lan, &tag, &val); - u3_assert( (c3y == tag) || (c3n == tag) ); - if ( c3y == tag ) { - u3_assert( c3y == u3a_is_cat(val) ); - u3_assert( val < 256 ); - if ( c3n == u3_Host.ops_u.net ) { - u3_lane lan_u; - lan_u.pip_w = 0x7f000001 ; - lan_u.por_s = _ames_czar_port(val); - _xmas_send_buf(sam_u, lan_u, buf_y, len); - } else { - // get galaxy lane or add pending - } - } else { - u3_lane lan_u = u3_xmas_decode_lane(u3k(val)); - // if in local-only mode, don't send remote packets - // - if ( (c3n == u3_Host.ops_u.net) && (0x7f000001 != lan_u.pip_w) ) { - suc_o = c3n; - } - // if the lane is uninterpretable, silently drop the packet - // - else if ( 0 == lan_u.por_s ) { - if ( u3C.wag_w & u3o_verbose ) { - u3l_log("ames: inscrutable lane"); - } - suc_o = c3n; - } else { - _xmas_send_buf(sam_u, lan_u, buf_y, len); - } - } - } - u3z(las); u3z(pac); + _xmas_rout_bufs(sam_u, buf_y, len, las); + u3z(pac); } static void @@ -957,12 +1152,12 @@ static c3_o _xmas_kick(u3_xmas* sam_u, u3_noun tag, u3_noun dat) default: { ret_o = c3n; } break; - case c3__pact: { + case c3__sent: { u3_noun las, pac; if ( c3n == u3r_cell(dat, &las, &pac) ) { ret_o = c3n; } else { - _xmas_ef_pact(sam_u, u3k(las), u3k(pac)); + _xmas_ef_send(sam_u, u3k(las), u3k(pac)); ret_o = c3y; } } break; @@ -995,6 +1190,7 @@ static c3_o _xmas_io_kick(u3_auto* car_u, u3_noun wir, u3_noun cad) { ret_o = c3n; } else { + u3m_p("tag", u3k(tag)); ret_o = _xmas_kick(sam_u, u3k(tag), u3k(dat)); } @@ -1035,173 +1231,6 @@ _xmas_io_exit(u3_auto* car_u) uv_close(&sam_u->had_u, _xmas_exit_cb); } -static c3_w -_xmas_sift_page_pact(u3_xmas_page_pact* pac_u, c3_y* buf_y, c3_w len_w) -{ - c3_w cur_w = 0; - - /*// next hop - memcpy(pac_u->hop_y, buf_y, 6); - cur_w += 6; - - // path length - pac_u->pat_s = _ames_sift_short(buf_y + cur_w); - cur_w += 2; - - // path contents - pac_u->pat_c = c3_calloc(pac_u->pat_s + 1); - memcpy(pac_u->pat_c, buf_y + cur_w, pac_u->pat_s); - pac_u->pat_c[pac_u->pat_s] = '\0'; - cur_w += pac_u->pat_s + 1; - - // total fragments - pac_u->tot_w = _ames_sift_word(buf_y + cur_w); - cur_w += 4; - - // Authenticator - memcpy(pac_u->aut_y, buf_y + cur_w, 96); - cur_w += 96; - - c3_w dat_w = 1024; // len_w - cur_w; - pac_u->dat_y = c3_calloc(dat_w); - memcpy(pac_u->dat_y, buf_y + cur_w, dat_w); - */ - return 0; -} - -static c3_w -_xmas_sift_name(u3_xmas_name* nam_u, c3_y* buf_y, c3_w len_w) -{ -#ifdef XMAS_DEBUG - u3l_log("xmas: sifting name %i", len_w); - _log_buf(buf_y, len_w); -#endif - - c3_w cur_w = 0; - CHECK_BOUNDS(cur_w + 1); - c3_y met_y = buf_y[cur_w]; - nam_u->met_u.ran_y = (met_y >> 0) & 0x3; - nam_u->met_u.rif_y = (met_y >> 2) & 0x3; - nam_u->met_u.pat_b = (met_y >> 4) & 0x1; - nam_u->met_u.boq_b = (met_y >> 5) & 0x1; - nam_u->met_u.fra_y = (met_y >> 6) & 0x3; - cur_w += 1; - - c3_y her_y = 2 << nam_u->met_u.ran_y; - CHECK_BOUNDS(cur_w + her_y) - _ames_ship_to_chubs(nam_u->her_d, her_y, buf_y + cur_w); - cur_w += her_y; - - c3_y rif_y = nam_u->met_u.rif_y + 1; - CHECK_BOUNDS(cur_w + rif_y) - for( int i = 0; i < rif_y; i++ ) { - nam_u->rif_w |= (buf_y[cur_w] << (8*i)); - cur_w++; - } - - c3_y pat_y = 1 << nam_u->met_u.pat_b; - CHECK_BOUNDS(cur_w + pat_y) - nam_u->pat_s = 0; - for ( int i = 0; i < pat_y; i++ ) { - nam_u->pat_s |= (buf_y[cur_w] << (8*i)); - cur_w++; - } - - nam_u->pat_c = c3_calloc(nam_u->pat_s + 1); // unix string for ease of manipulation - CHECK_BOUNDS(nam_u->pat_s + cur_w); - memcpy(nam_u->pat_c, buf_y + cur_w, nam_u->pat_s); - nam_u->pat_c[nam_u->pat_s] = 0; - cur_w += nam_u->pat_s; - - if ( 1 == nam_u->met_u.boq_b ) { - CHECK_BOUNDS(cur_w + 1); - nam_u->boq_y = buf_y[cur_w]; - cur_w++; - } else { - nam_u->boq_y = 13; - } - - c3_y fra_y = nam_u->met_u.fra_y + 1; - CHECK_BOUNDS(cur_w + fra_y) - nam_u->fra_w = 0; - for( int i = 0; i < fra_y; i++ ) { - nam_u->fra_w |= (buf_y[cur_w] << (8*i)); - cur_w++; - } - if ( cur_w < len_w ) { - u3l_log("xmas: failed to consume entire packet"); - _log_buf(buf_y + cur_w, len_w - cur_w); - return 0; - } - - return cur_w; -} - - -static c3_w -_xmas_sift_peek_pact(u3_xmas_peek_pact* pac_u, c3_y* buf_y, c3_w len_w) -{ - c3_w siz_w = _xmas_sift_name(&pac_u->nam_u, buf_y, len_w); - /*if (siz_w > len_w ) { - u3l_log("xmas: buffer overrun (FIXME)"); - u3m_bail(c3__foul); - }*/ - return siz_w; -} - -static c3_w -_xmas_sift_poke_pact(u3_xmas_poke_pact* pac_u, c3_y* buf_y, c3_w len_w) -{ - c3_w cur_w = 0; - // Peek portion - pac_u->pek_u = c3_calloc(sizeof(*(pac_u->pek_u))); - cur_w += _xmas_sift_peek_pact(pac_u->pek_u, buf_y, len_w); - - // Total fragments - pac_u->tot_w = _ames_sift_word(buf_y + cur_w); - cur_w += 4; - - // Authenticator - memcpy(pac_u->aut_y, buf_y + cur_w, 96); - cur_w += 96; - - // Datum - memcpy(pac_u->dat_y, buf_y + cur_w, len_w - cur_w); - return cur_w; -} - -static c3_w -_xmas_sift_pact(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w) -{ - u3_xmas_head hed_u; - if( len_w < 8 ) { - u3l_log("xmas: attempted to parse overly short packet of size %u", len_w); - } - - _xmas_sift_head(buf_y, &hed_u); - pac_u->typ_y = hed_u.typ_y; - c3_w res_w = 0; - buf_y += 8; - len_w -= 8; - switch ( pac_u->typ_y ) { - case PACT_PEEK: { - res_w = _xmas_sift_peek_pact(&pac_u->pek_u, buf_y, len_w); - } break; - case PACT_PAGE: { - res_w = _xmas_sift_page_pact(&pac_u->pag_u, buf_y, len_w); - } break; - case PACT_POKE: { - res_w = _xmas_sift_poke_pact(&pac_u->pok_u, buf_y, len_w); - } break; - default: { - u3l_log("xmas: received unknown packet type"); - break; - } - } - u3_assert(res_w <= len_w ); - return res_w; -} - /* * RETAIN */ @@ -1247,27 +1276,37 @@ _xmas_add_galaxy_pend(u3_xmas* sam_u, u3_noun her, u3_noun pen) return _(wat); } +static u3_noun +_name_to_scry(u3_xmas_name* nam_u) +{ + u3_noun rif = u3dc("scot", c3__ud, nam_u->rif_w); + u3_noun boq = u3dc("scot", c3__ud, nam_u->boq_y); + u3_noun fag = u3dc("scot", c3__ud, nam_u->fra_w); + u3_noun pax = u3do("stab", u3dt("cat", 3, '/', u3i_string(nam_u->pat_c))); + + return u3dc("weld", u3nc(c3__pact, u3nq(rif, boq, fag, u3nc(c3__data, u3_nul))), pax); +} + /* * RETAIN */ static u3_weak -_xmas_get_cache(u3_xmas* sam_u, u3_xmas_name nam_u) +_xmas_get_cache(u3_xmas* sam_u, u3_xmas_name* nam_u) { - u3_noun pax = u3dt("cat", 3, '/', u3i_string(nam_u.pat_c)); + u3_noun pax = _name_to_scry(nam_u); u3_weak res = u3h_git(sam_u->pen_p, pax); if ( u3_none == res ) { u3m_p("miss", u3k(pax)); } else { u3m_p("hit", u3nc(u3k(pax), u3k(res))); } - u3z(pax); return res; } static void -_xmas_put_cache(u3_xmas* sam_u, u3_xmas_name nam_u, u3_noun val) +_xmas_put_cache(u3_xmas* sam_u, u3_xmas_name* nam_u, u3_noun val) { - u3_noun pax = u3dt("cat", 3, '/', u3i_string(nam_u.pat_c)); + u3_noun pax = _name_to_scry(nam_u); u3m_p("new key", u3k(pax)); u3m_p("new value", u3k(val)); @@ -1312,10 +1351,8 @@ _xmas_try_forward(u3_xmas_pact* pac_u, u3_noun fra, u3_noun hit) static c3_w _xmas_respond(u3_xmas_pact* req_u, c3_y** buf_y, u3_noun hit) { - u3_noun mat = u3dc("snag", u3i_word(req_u->pek_u.nam_u.fra_w), u3k(u3t(hit))); - u3_noun len, dat; - u3x_cell(mat, &len, &dat); + u3x_cell(hit, &len, &dat); *buf_y = c3_calloc(len); @@ -1363,7 +1400,6 @@ _xmas_page_scry_cb(void* vod_p, u3_noun nun) { u3_xmas_pact* pac_u = vod_p; u3_xmas* sam_u = pac_u->sam_u; - c3_s fra_s; //u3_noun pax = _xmas_path_with_fra(pac_u->pek_u.nam_u.pat_c, &fra_s); u3_weak hit = u3r_at(7, nun); @@ -1373,7 +1409,7 @@ _xmas_page_scry_cb(void* vod_p, u3_noun nun) u3l_log("unbound"); } else { - u3_weak old = _xmas_get_cache(sam_u, pac_u->pag_u.nam_u); + u3_weak old = _xmas_get_cache(sam_u, &pac_u->pag_u.nam_u); if ( old == u3_none ) { u3l_log("bad"); } else { @@ -1383,10 +1419,12 @@ _xmas_page_scry_cb(void* vod_p, u3_noun nun) if ( XMAS_SCRY == tag ) { c3_y* buf_y; - c3_w len_w = _xmas_respond(pac_u, &buf_y, hit); - //_xmas_rout_pacs(res_u, u3k(u3t(dat))); + u3m_p("hit", hit); + u3m_p("dat", dat); + c3_w len_w = _xmas_respond(pac_u, &buf_y, u3k(hit)); + _xmas_rout_bufs(sam_u, buf_y, len_w, u3k(u3t(dat))); } - // _xmas_put_cache(sam_u, u3k(pax), u3nc(XMAS_ITEM, u3k(hit))); + _xmas_put_cache(sam_u, &pac_u->pek_u.nam_u, u3nc(XMAS_ITEM, u3k(hit))); u3z(old); } u3z(hit); @@ -1398,18 +1436,23 @@ static void _xmas_hear_bail(u3_ovum* egg_u, u3_noun lud) { u3l_log("xmas: hear bail"); + c3_w len_w = u3qb_lent(lud); + u3l_log("len_w: %i", len_w); + if( len_w == 2 ) { + u3_pier_punt_goof("hear", u3k(u3h(lud))); + u3_pier_punt_goof("crud", u3k(u3h(u3t(lud)))); + } u3_ovum_free(egg_u); } static void -_xmas_hear_page(u3_xmas_pact* pac_u, u3_lane lan_u) +_xmas_hear_page(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w, u3_lane lan_u) { u3_xmas* sam_u = pac_u->sam_u; u3_noun wir = u3nc(c3__xmas, u3_nul); c3_s fra_s; - u3_noun pat = _xmas_path_with_fra(pac_u->pag_u.nam_u.pat_c, &fra_s); - u3_weak cac = _xmas_get_cache(sam_u, pac_u->pag_u.nam_u); + u3_weak cac = _xmas_get_cache(sam_u, &pac_u->pag_u.nam_u); if ( u3_none != cac ) { u3_noun tag, dat; @@ -1420,13 +1463,7 @@ _xmas_hear_page(u3_xmas_pact* pac_u, u3_lane lan_u) } u3z(cac); } else { - u3_noun mess = u3nq( - pat, - u3i_word(pac_u->pag_u.mat_u.tot_w), - u3i_bytes(256, pac_u->pag_u.mat_u.aut_y), - u3i_bytes(pac_u->pag_u.mat_u.len_d, pac_u->pag_u.mat_u.fra_y) - ); - u3_noun cad = u3nt(c3__mess, c3__page, mess); + u3_noun cad = u3nt(c3__heer, u3_xmas_encode_lane(lan_u), u3i_bytes(len_w, buf_y)); u3_auto_peer( u3_auto_plan(&sam_u->car_u, @@ -1437,45 +1474,54 @@ _xmas_hear_page(u3_xmas_pact* pac_u, u3_lane lan_u) } static void -_xmas_add_lane_to_cache(u3_xmas* sam_u, u3_xmas_name nam_u, u3_noun las, u3_lane lan_u) +_xmas_add_lane_to_cache(u3_xmas* sam_u, u3_xmas_name* nam_u, u3_noun las, u3_lane lan_u) { u3_noun hit = u3nq(XMAS_SCRY, _xmas_get_now(), - u3nc(c3n, u3_xmas_encode_lane(lan_u)), + u3_xmas_encode_lane(lan_u), u3k(las)); _xmas_put_cache(sam_u, nam_u, hit); u3z(las); } + + static void _xmas_hear_peek(u3_xmas_pact* pac_u, u3_lane lan_u) { #ifdef XMAS_DEBUG u3l_log("xmas: hear peek"); u3l_log("%hu", lan_u.por_s); - u3_assert(pac_u->typ_y == PACT_PEEK); + // u3_assert(pac_u->typ_y == PACT_PEEK); #endif u3_xmas* sam_u = pac_u->sam_u; - u3_weak hit = _xmas_get_cache(sam_u, pac_u->pek_u.nam_u); + u3_weak hit = _xmas_get_cache(sam_u, &pac_u->pek_u.nam_u); if ( u3_none != hit ) { u3_noun tag, dat; u3x_cell(u3k(hit), &tag, &dat); u3m_p("dat", dat); if ( tag == XMAS_SCRY ) { - // _xmas_add_lane_to_cache(sam_u, u3k(u3t(dat)), u3k(pax), lan_u); + _xmas_add_lane_to_cache(sam_u, &pac_u->pek_u.nam_u, u3t(dat), lan_u); } else if ( tag == XMAS_ITEM ) { u3l_log("item hit"); - u3m_p("dat", u3k(dat)); u3l_log("after dat"); - _xmas_serve_cache_hit(pac_u, lan_u, u3k(dat)); + c3_y* buf_y; + + c3_w len_w = _xmas_respond(pac_u, &buf_y, u3k(dat)); + _xmas_send_buf(pac_u->sam_u, lan_u, buf_y, len_w); } else { u3l_log("xmas: weird case in cache, dropping"); } u3z(hit); } else { - //_xmas_add_lane_to_cache(sam_u, u3_nul, u3k(pax), lan_u); // TODO: retrieve from namespace - // u3_noun sky = u3nc(c3__page, pax); + u3l_log("adding lane to cache"); + _xmas_add_lane_to_cache(sam_u, &pac_u->pek_u.nam_u, u3_nul, lan_u); // TODO: retrieve from namespace + u3_noun sky = _name_to_scry(&pac_u->pek_u.nam_u); + + u3_noun our = u3i_chubs(2, sam_u->car_u.pir_u->who_d); + u3_noun bem = u3nc(u3nt(our, u3_nul, u3nc(c3__ud, 1)), sky); + u3_pier_peek(sam_u->car_u.pir_u, u3_nul, u3k(u3nq(1, c3__beam, c3__xx, bem)), pac_u, _xmas_page_scry_cb); // u3_pier_peek_last(sam_u->car_u.pir_u, u3_nul, c3__xx, u3_nul, sky, pac_u, _xmas_page_scry_cb); } u3l_log("xmas: finished hear peek"); @@ -1490,6 +1536,7 @@ _xmas_hear(u3_xmas* sam_u, { u3l_log("xmas_hear"); + _log_buf(hun_y, len_w); u3_xmas_pact* pac_u; c3_w pre_w; @@ -1522,7 +1569,7 @@ _xmas_hear(u3_xmas* sam_u, _xmas_hear_peek(pac_u, *lan_u); } break; case PACT_PAGE: { - _xmas_hear_page(pac_u, *lan_u); + _xmas_hear_page(pac_u, hun_y, len_w, *lan_u); } break; default: { u3l_log("xmas: unimplemented packet type %u", pac_u->typ_y); @@ -1576,6 +1623,16 @@ _xmas_io_talk(u3_auto* car_u) { u3_xmas* sam_u = (u3_xmas*)car_u; sam_u->dns_c = "urbit.org"; // TODO: receive turf + { + // XX remove [sev_l] + // + u3_noun wir = u3nt(c3__xmas, + u3dc("scot", c3__uv, sam_u->sev_l), + u3_nul); + u3_noun cad = u3nc(c3__born, u3_nul); + + u3_auto_plan(car_u, u3_ovum_init(0, c3__x, wir, cad)); + } //_xmas_io_start(sam_u); u3_noun who = u3i_chubs(2, sam_u->pir_u->who_d); u3_noun rac = u3do("clan:title", u3k(who)); @@ -1687,7 +1744,6 @@ u3_xmas_io_init(u3_pier* pir_u) return car_u; } -#define XMAS_TEST #ifdef XMAS_TEST static void From 18437e85cec5573e44859a1150163f1910a63813 Mon Sep 17 00:00:00 2001 From: Liam Fitzgerald Date: Fri, 26 Jan 2024 16:20:23 -0500 Subject: [PATCH 018/430] xmas: working full flow --- pkg/noun/jets/q.h | 1 + pkg/vere/io/xmas.c | 297 +++++++++++++++++++++++++++++++++++++++------ 2 files changed, 259 insertions(+), 39 deletions(-) diff --git a/pkg/noun/jets/q.h b/pkg/noun/jets/q.h index 33c63ef42b..b650c407f3 100644 --- a/pkg/noun/jets/q.h +++ b/pkg/noun/jets/q.h @@ -101,6 +101,7 @@ u3_noun u3qdi_apt(u3_noun); u3_noun u3qdi_bif(u3_noun, u3_noun); + u3_noun u3qdi_del(u3_noun, u3_noun); u3_noun u3qdi_dif(u3_noun, u3_noun); u3_noun u3qdi_gas(u3_noun, u3_noun); u3_noun u3qdi_has(u3_noun, u3_noun); diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c index caeb43183c..5a511a1945 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/xmas.c @@ -9,6 +9,8 @@ #include #include #include +#include +#include #include #include #include @@ -23,13 +25,22 @@ #define RED_TEXT "\033[0;31m" #define DEF_TEXT "\033[0m" +#define IN_FLIGHT 5 + // pending interest sentinels #define XMAS_ITEM 1 // cached item #define XMAS_SCRY 2 // waiting on scry + // routing table sentinels #define XMAS_CZAR 1 // pending dns lookup #define XMAS_ROUT 2 // have route // +// hop enum +#define HOP_NONE 0b0 +#define HOP_SHORT 0b1 +#define HOP_LONG 0b10 +#define HOP_MANY 0b11 + #define XMAS_COOKIE_LEN 4 static c3_y XMAS_COOKIE[4] = { 0x5e, 0x1d, 0xad, 0x51 }; @@ -51,7 +62,7 @@ typedef struct _u3_xmas { ur_cue_test_t* tes_u; // cue-test handle u3_cue_xeno* sil_u; // cue handle u3p(u3h_root) her_p; // - u3p(u3h_root) pen_p; + u3p(u3h_root) pac_p; // pending c3_w imp_w[256]; time_t imp_t[256]; // imperial IP timestamps c3_o imp_o[256]; // imperial print status @@ -126,7 +137,7 @@ typedef struct _u3_xmas_peek_pact { typedef struct _u3_xmas_page_meat_meta { c3_y tot_y; // total fragments length (2bit) - c3_o aut_o; // authentication len len (1bit) + c3_o aut_b; // authentication len len (1bit) c3_y len_y; // fragment length (5bit) } u3_xmas_page_meat_meta; @@ -141,14 +152,29 @@ typedef struct _u3_xmas_page_meat { c3_w tot_w; // total fragments c3_y aul_y; // authentication length u3_xmas_auth_type ayp_y; // authentication type - c3_y aut_y[255]; // authentication + c3_y* aut_y; // authentication c3_d len_d; // fragment length c3_y* fra_y; // fragment } u3_xmas_page_meat; +typedef struct _u3_xmas_hop { + c3_w len_w; + c3_y* dat_y; +} u3_xmas_hop; + +typedef struct _u3_xmas_hops { + c3_w len_w; + u3_xmas_hop** dat_y; +} u3_xmas_hops; + typedef struct _u3_xmas_page_pact { u3_xmas_name nam_u; u3_xmas_page_meat mat_u; + union { + c3_y sot_u[6]; + u3_xmas_hop one_u; + u3_xmas_hops man_u; + }; } u3_xmas_page_pact; typedef struct _u3_xmas_poke_pact { @@ -175,6 +201,10 @@ typedef enum _u3_xmas_ctag { CACE_ITEM = 2, } u3_xmas_ctag; +/* + * typedef u3_xmas_req u3_noun + * [tot=@ waiting=(set @ud) missing=(set @ud) nex=@ud dat=(map @ud @)] + */ typedef struct _u3_cace_enty { @@ -229,6 +259,7 @@ _log_name(u3_xmas_name* nam_u) u3l_log("frag num length: %u", nam_u->met_u.fra_y); u3l_log("path length length: %u", nam_u->met_u.pat_b); u3l_log("publisher: %s", u3r_string(u3dc("scot", c3__p, u3i_chubs(2, nam_u->her_d)))); + u3l_log("rift: %u", nam_u->rif_w); u3l_log("path len: %u", nam_u->pat_s); u3l_log("path: %s", nam_u->pat_c); u3l_log("bloq: %u", nam_u->boq_y); @@ -247,6 +278,15 @@ static void _log_page_pact(u3_xmas_page_pact *pac_u) { _log_name(&pac_u->nam_u); + u3_xmas_page_meat* mat_u = &pac_u->mat_u; + u3l_log("meta"); + u3l_log("total length: %u", mat_u->met_u.tot_y); + u3l_log("auth len length: %u", mat_u->met_u.aut_b); + u3l_log("frag len len: %u", mat_u->met_u.len_y); + u3l_log("actual:"); + u3l_log("total: %u", mat_u->tot_w); + u3l_log("auth len: %u", mat_u->aul_y); + u3l_log("frag len: %u", mat_u->len_d); } static void @@ -494,6 +534,36 @@ _xmas_rout_pact(u3_xmas_pact* pac_u, u3_noun lan) { return suc_o; } +u3_noun _xmas_request_key(u3_xmas_name* nam_u) +{ + u3_noun pax = u3do("stab", u3dt("cat", 3, '/', u3i_string(nam_u->pat_c))); + u3_noun res = u3nt(u3i_chubs(2, nam_u->her_d), u3i_word(nam_u->rif_w), pax); + u3m_p("key", res); + return res; +} + +static u3_weak +_xmas_get_request(u3_xmas* sam_u, u3_xmas_name* nam_u) { + u3_noun key = _xmas_request_key(nam_u); + + u3_weak res = u3h_git(sam_u->req_p, key); + u3z(key); + return res; +} + +/** RETAIN + */ +static void +_xmas_put_request(u3_xmas* sam_u, u3_xmas_name* nam_u, u3_noun val) +{ + u3_noun key = _xmas_request_key(nam_u); + + u3h_put(sam_u->req_p, key, val); + + u3z(key); +} + + // refcounted // RETAIN @@ -576,7 +646,6 @@ _xmas_sift_name(u3_xmas_name* nam_u, c3_y* buf_y, c3_w len_w) { #ifdef XMAS_DEBUG u3l_log("xmas: sifting name %i", len_w); - _log_buf(buf_y, len_w); #endif c3_w cur_w = 0; @@ -595,6 +664,7 @@ _xmas_sift_name(u3_xmas_name* nam_u, c3_y* buf_y, c3_w len_w) cur_w += her_y; c3_y rif_y = nam_u->met_u.rif_y + 1; + nam_u->rif_w = 0; CHECK_BOUNDS(cur_w + rif_y) for( int i = 0; i < rif_y; i++ ) { nam_u->rif_w |= (buf_y[cur_w] << (8*i)); @@ -635,16 +705,113 @@ _xmas_sift_name(u3_xmas_name* nam_u, c3_y* buf_y, c3_w len_w) return cur_w; } +static c3_w +_xmas_sift_hop_long(u3_xmas_hop* hop_u, c3_y* buf_y, c3_w len_w) +{ + c3_w cur_w = 0; + CHECK_BOUNDS(cur_w + 1); + hop_u->len_w = buf_y[cur_w]; + cur_w++; + CHECK_BOUNDS(cur_w + hop_u->len_w); + hop_u->dat_y = c3_calloc(hop_u->len_w); + memcpy(hop_u->dat_y, buf_y + cur_w, hop_u->len_w); + + return cur_w; +} static c3_w -_xmas_sift_page_pact(u3_xmas_page_pact* pac_u, c3_y* buf_y, c3_w len_w) +_xmas_sift_page_pact(u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, c3_w len_w) { c3_w cur_w = 0; - cur_w += _xmas_sift_name(&pac_u->nam_u, buf_y, len_w); + c3_w nam_w = _xmas_sift_name(&pac_u->nam_u, buf_y, len_w); + + if( nam_w == 0 ) { + return 0; + } + cur_w += nam_w; + + u3_xmas_page_meat* mat_u = &pac_u->mat_u; CHECK_BOUNDS(cur_w + 1); + c3_y met_y = buf_y[cur_w]; + mat_u->met_u.tot_y = (met_y >> 0) & 0x3; + mat_u->met_u.aut_b = (met_y >> 2) & 0x1; + mat_u->met_u.len_y = (met_y >> 3) & 0x1F; + cur_w += 1; + + c3_y tot_y = pac_u->mat_u.met_u.tot_y + 1; + CHECK_BOUNDS(cur_w + tot_y); + mat_u->tot_w = 0; + for( int i = 0; i < tot_y; i++ ) { + mat_u->tot_w |= (buf_y[cur_w] << (8*i)); + cur_w++; + } + + mat_u->aul_y = 0; + if ( 1 == mat_u->met_u.aut_b ) { + CHECK_BOUNDS(cur_w + 1); + mat_u->aul_y = buf_y[cur_w]; + cur_w++; + } + + CHECK_BOUNDS(cur_w + mat_u->aul_y); + for ( int i = 0; i < mat_u->aul_y; i++ ) { + mat_u->aut_y = c3_calloc(mat_u->aul_y); + memcpy(mat_u->aut_y, buf_y + cur_w, mat_u->aul_y); + cur_w += mat_u->aul_y; + } + + c3_y len_y = mat_u->met_u.len_y; + CHECK_BOUNDS(cur_w + len_y); + u3_assert( len_y <= 8 ); + mat_u->len_d = 0; + for ( int i = 0; i < len_y; i++ ) { + mat_u->len_d |= (buf_y[cur_w] << (8*i)); + cur_w++; + } + + CHECK_BOUNDS(cur_w + len_y); + mat_u->fra_y = c3_calloc(mat_u->len_d); + memcpy(mat_u->fra_y, buf_y + cur_w, mat_u->len_d); + + switch ( hed_u->nex_y ) { + default: { + u3l_log("xmas: bad hopcount"); + return 0; + } + case HOP_NONE: break; + case HOP_SHORT: { + CHECK_BOUNDS(cur_w + 6); + memcpy(pac_u->sot_u, buf_y + cur_w, 6); + cur_w += 6; + } break; + case HOP_LONG: { + c3_w hop_w = _xmas_sift_hop_long(&pac_u->one_u, buf_y + cur_w, len_w - cur_w); + if( hop_w == 0 ) { + return 0; + } + cur_w += hop_w; + } break; + case HOP_MANY: { + CHECK_BOUNDS(cur_w + 1); + pac_u->man_u.len_w = buf_y[cur_w]; + cur_w++; + + pac_u->man_u.dat_y = c3_calloc(sizeof(u3_xmas_hop*) * pac_u->man_u.len_w); + + for( int i = 0; i < pac_u->man_u.len_w; i++ ) { + pac_u->man_u.dat_y[i] = c3_calloc(sizeof(u3_xmas_hop)); + c3_w hop_w = _xmas_sift_hop_long(pac_u->man_u.dat_y[i], buf_y + cur_w ,len_w - cur_w); + if ( hop_w == 0 ) { + return 0; + } + cur_w += hop_w; + } + } + } + return cur_w; /*// next hop @@ -733,7 +900,7 @@ _xmas_sift_pact(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w) res_w = _xmas_sift_peek_pact(&pac_u->pek_u, buf_y, len_w); } break; case PACT_PAGE: { - res_w = _xmas_sift_page_pact(&pac_u->pag_u, buf_y, len_w); + res_w = _xmas_sift_page_pact(&pac_u->pag_u, &hed_u, buf_y, len_w); } break; case PACT_POKE: { res_w = _xmas_sift_poke_pact(&pac_u->pok_u, buf_y, len_w); @@ -965,7 +1132,7 @@ static void _xmas_send_buf(u3_xmas* sam_u, u3_lane lan_u, c3_y* buf_y, c3_w len_ #ifdef XMAS_DEBUG c3_c* sip_c = inet_ntoa(add_u.sin_addr); - u3l_log("xmas: sending packet (%s,%u)", sip_c, por_s); + // u3l_log("xmas: sending packet (%s,%u)", sip_c, por_s); #endif uv_buf_t buf_u = uv_buf_init((c3_c*)buf_y, len_w); @@ -1078,6 +1245,29 @@ _xmas_rout_pacs(u3_xmas_pact* pac_u, u3_noun las) return suc_o; } +static void +_xmas_update_req_peek(u3_xmas_pact* pac_u) { + u3_weak req = _xmas_get_request(pac_u->sam_u, &pac_u->pek_u.nam_u); + + if ( u3_none == req ) { + u3_assert( 0 == pac_u->pek_u.nam_u.fra_w ); // TODO restart request + _xmas_put_request(pac_u->sam_u, &pac_u->pek_u.nam_u, u3nq(0, u3_nul, u3_nul, u3nc(0, u3_nul))); + } else { + u3_noun tot, wat, mis, nex, dat; + u3x_quil(req, &tot, &wat, &mis, &nex, &dat); + u3_noun fra = u3i_word(pac_u->pek_u.nam_u.fra_w); + if ( c3y == u3r_sing(fra, nex) ) { + nex = fra; + } + + u3_noun nat = u3kdi_put(u3k(wat), u3k(fra)); + + u3_noun new = u3nq(u3k(tot), nat, u3k(mis), u3nc(u3k(nex), u3k(dat))); + _xmas_put_request(pac_u->sam_u, &pac_u->pek_u.nam_u, new); + //u3z(new); u3z(req); + } +} + static void _xmas_ef_send(u3_xmas* sam_u, u3_noun las, u3_noun pac) { @@ -1094,9 +1284,14 @@ _xmas_ef_send(u3_xmas* sam_u, u3_noun las, u3_noun pac) c3_y* buf_y = c3_calloc(len); u3r_bytes(0, len, buf_y, dat); u3_xmas_pact pac_u; + pac_u.sam_u = sam_u; _xmas_sift_pact(&pac_u, buf_y, len); - u3l_log("packet: "); - _log_pact(&pac_u); + + if ( pac_u.typ_y == PACT_PEEK ) { + _xmas_update_req_peek(&pac_u); + } + + c3_o suc_o = c3n; u3_noun lan, t = las; @@ -1161,16 +1356,6 @@ static c3_o _xmas_kick(u3_xmas* sam_u, u3_noun tag, u3_noun dat) ret_o = c3y; } } break; - // - case c3__peek: { - u3_noun lan, her, pat; - if ( c3n == u3r_trel(dat, &her, &lan, &pat) ) { - ret_o = c3n; - } else { - //_xmas_ef_peek(sam_u, u3k(her), u3k(lan), u3k(pat)); - ret_o = c3y; - } - } break; } // technically losing tag is unncessary as it always should @@ -1190,7 +1375,6 @@ static c3_o _xmas_io_kick(u3_auto* car_u, u3_noun wir, u3_noun cad) { ret_o = c3n; } else { - u3m_p("tag", u3k(tag)); ret_o = _xmas_kick(sam_u, u3k(tag), u3k(dat)); } @@ -1294,7 +1478,7 @@ static u3_weak _xmas_get_cache(u3_xmas* sam_u, u3_xmas_name* nam_u) { u3_noun pax = _name_to_scry(nam_u); - u3_weak res = u3h_git(sam_u->pen_p, pax); + u3_weak res = u3h_git(sam_u->pac_p, pax); if ( u3_none == res ) { u3m_p("miss", u3k(pax)); } else { @@ -1310,7 +1494,7 @@ _xmas_put_cache(u3_xmas* sam_u, u3_xmas_name* nam_u, u3_noun val) u3m_p("new key", u3k(pax)); u3m_p("new value", u3k(val)); - u3h_put(sam_u->pen_p, pax, val); + u3h_put(sam_u->pac_p, pax, val); u3z(pax); // TODO: fix refcount } @@ -1448,29 +1632,65 @@ _xmas_hear_bail(u3_ovum* egg_u, u3_noun lud) static void _xmas_hear_page(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w, u3_lane lan_u) { + u3l_log("xmas hear page"); u3_xmas* sam_u = pac_u->sam_u; u3_noun wir = u3nc(c3__xmas, u3_nul); c3_s fra_s; + _log_pact(pac_u); + + u3_noun fra = u3i_bytes(pac_u->pag_u.mat_u.len_d, pac_u->pag_u.mat_u.fra_y) ; - u3_weak cac = _xmas_get_cache(sam_u, &pac_u->pag_u.nam_u); - if ( u3_none != cac ) { - u3_noun tag, dat; + u3_weak req = _xmas_get_request(sam_u, &pac_u->pag_u.nam_u); - u3x_cell(cac, &tag, &dat); - if ( tag == XMAS_SCRY ) { - _xmas_rout_pacs(pac_u, u3k(u3t(dat))); - } - u3z(cac); - } else { - u3_noun cad = u3nt(c3__heer, u3_xmas_encode_lane(lan_u), u3i_bytes(len_w, buf_y)); + if ( u3_none == req ) { + u3l_log("xmas: heard page w/ no request, dropping"); // TODO + return; + } - u3_auto_peer( - u3_auto_plan(&sam_u->car_u, - u3_ovum_init(0, c3__x, wir, cad)), - 0, 0, _xmas_hear_bail); + u3_noun tot, wat, mis, nex, dat; + u3x_quil(req, &tot, &wat, &mis, &nex, &dat); + if ( tot == 0 ) { + tot = u3i_word(pac_u->pag_u.mat_u.tot_w); } - // no lose required because all nouns are moved + c3_w nex_w = u3r_word(0, nex); + u3_noun num = u3i_word(pac_u->pag_u.nam_u.fra_w); + u3_noun wut = u3qdi_del(wat, u3k(num)); + c3_w lat_w = u3r_word(0, u3qdi_wyt(wut)); + + u3_noun dit = u3qdb_put(dat, u3k(num), fra); + u3m_p("dit wyt", u3qdb_wyt(dit)); + + + if ( c3y == u3r_sing(tot, u3qdb_wyt(dit)) ) { + u3l_log("finished"); + return; + } else if ( lat_w < IN_FLIGHT ) { + c3_w sen_w = IN_FLIGHT - lat_w; + u3l_log("send %u", sen_w); + for(int i = 0; i < sen_w; i++) { + u3l_log("sending req fra %u", nex_w + i); + u3_xmas_pact* nex_u = c3_calloc(sizeof(u3_xmas_pact)); + nex_u->sam_u = sam_u; + nex_u->typ_y = PACT_PEEK; + memcpy(&nex_u->pek_u.nam_u, &pac_u->pag_u.nam_u, sizeof(u3_xmas_name)); + nex_u->pek_u.nam_u.fra_w = nex_w + i; + c3_y* buf_y = c3_calloc(PACT_SIZE); + c3_w siz_w =_xmas_etch_pact(buf_y, nex_u, 0); + if( siz_w == 0 ) { + u3l_log("failed to etch"); + u3_assert( 0 ); + + } + // TODO: better route management + _xmas_send_buf(sam_u, lan_u, buf_y, siz_w); + wut = u3qdi_put(wut, u3i_word(nex_u->pek_u.nam_u.fra_w)); + nex = u3dc("add", 1, nex); + } + } + _xmas_put_request(sam_u, &pac_u->pag_u.nam_u, u3nq(u3k(tot), u3k(wut), u3k(mis), u3nc(u3k(nex), u3k(dit)))); + //u3z(req); + } static void @@ -1536,7 +1756,6 @@ _xmas_hear(u3_xmas* sam_u, { u3l_log("xmas_hear"); - _log_buf(hun_y, len_w); u3_xmas_pact* pac_u; c3_w pre_w; From 2eb1ce2a11ffa1bd6534dbc38320de7bad8d2400 Mon Sep 17 00:00:00 2001 From: Liam Fitzgerald Date: Fri, 26 Jan 2024 17:17:26 -0500 Subject: [PATCH 019/430] xmas: end to end transfer, stubbed congestion control --- pkg/vere/io/xmas.c | 68 +++++++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 31 deletions(-) diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c index 5a511a1945..e5688be724 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/xmas.c @@ -15,7 +15,7 @@ #include #include -#define XMAS_DEBUG c3y +//#define XMAS_DEBUG c3y #define XMAS_VER 1 #define FINE_PAGE 4096 // packets per page #define FINE_FRAG 1024 // bytes per fragment packet @@ -223,6 +223,18 @@ typedef struct _u3_seal { } u3_seal; +c3_d _tim_d = 0; + +static c3_d +get_millis() { + struct timeval tp; + + gettimeofday(&tp, NULL); + return (tp.tv_sec * 1000) + (tp.tv_usec / 1000); + // Convert the seconds to milliseconds by multiplying by 1000 + // Convert the microseconds to milliseconds by dividing by 1000 +} + static void _log_head(u3_xmas_head* hed_u) { @@ -538,7 +550,6 @@ u3_noun _xmas_request_key(u3_xmas_name* nam_u) { u3_noun pax = u3do("stab", u3dt("cat", 3, '/', u3i_string(nam_u->pat_c))); u3_noun res = u3nt(u3i_chubs(2, nam_u->her_d), u3i_word(nam_u->rif_w), pax); - u3m_p("key", res); return res; } @@ -645,7 +656,7 @@ static c3_w _xmas_sift_name(u3_xmas_name* nam_u, c3_y* buf_y, c3_w len_w) { #ifdef XMAS_DEBUG - u3l_log("xmas: sifting name %i", len_w); + //u3l_log("xmas: sifting name %i", len_w); #endif c3_w cur_w = 0; @@ -694,7 +705,6 @@ _xmas_sift_name(u3_xmas_name* nam_u, c3_y* buf_y, c3_w len_w) } c3_y fra_y = nam_u->met_u.fra_y + 1; - u3l_log("fra_y: %i", fra_y); CHECK_BOUNDS(cur_w + fra_y) nam_u->fra_w = 0; for( int i = 0; i < fra_y; i++ ) { @@ -919,12 +929,14 @@ _xmas_sift_pact(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w) static void _xmas_etch_head(u3_xmas_head* hed_u, c3_y buf_y[8]) { +#ifdef XMAS_DEBUG if( c3y == XMAS_DEBUG ) { if( hed_u->pro_y > 7 ) { u3l_log("xmas: bad protocol version"); return; } } +#endif c3_o req_o = c3o((hed_u->typ_y == PACT_PEEK), (hed_u->typ_y == PACT_POKE)); c3_y siz_y = req_o ? 5 : 7; c3_w hed_w = (hed_u->nex_y & 0x3 ) << 2 @@ -1202,14 +1214,13 @@ _realise_lane(u3_noun lan) { static c3_o _xmas_rout_bufs(u3_xmas* sam_u, c3_y* buf_y, c3_w len_w, u3_noun las) { - u3l_log("routing bufs"); c3_o suc_o = c3n; u3_noun lan, t = las; while ( t != u3_nul ) { u3x_cell(t, &lan, &t); u3_lane lan_u = _realise_lane(u3k(lan)); #ifdef XMAS_DEBUG - u3l_log("sending to ip: %x, port: %u", lan_u.pip_w, lan_u.por_s); + //u3l_log("sending to ip: %x, port: %u", lan_u.pip_w, lan_u.por_s); #endif /* ifdef XMAS_DEBUG u3l_log("sending to ip: %x, port: %u", lan_u.pip_w, lan_u.por_s); */ @@ -1288,6 +1299,7 @@ _xmas_ef_send(u3_xmas* sam_u, u3_noun las, u3_noun pac) _xmas_sift_pact(&pac_u, buf_y, len); if ( pac_u.typ_y == PACT_PEEK ) { + u3l_log("start: %" PRIu64 "ms", get_millis()); _xmas_update_req_peek(&pac_u); } @@ -1480,9 +1492,9 @@ _xmas_get_cache(u3_xmas* sam_u, u3_xmas_name* nam_u) u3_noun pax = _name_to_scry(nam_u); u3_weak res = u3h_git(sam_u->pac_p, pax); if ( u3_none == res ) { - u3m_p("miss", u3k(pax)); + //u3m_p("miss", u3k(pax)); } else { - u3m_p("hit", u3nc(u3k(pax), u3k(res))); + //u3m_p("hit", u3nc(u3k(pax), u3k(res))); } return res; } @@ -1491,8 +1503,6 @@ static void _xmas_put_cache(u3_xmas* sam_u, u3_xmas_name* nam_u, u3_noun val) { u3_noun pax = _name_to_scry(nam_u); - u3m_p("new key", u3k(pax)); - u3m_p("new value", u3k(val)); u3h_put(sam_u->pac_p, pax, val); u3z(pax); // TODO: fix refcount @@ -1603,8 +1613,6 @@ _xmas_page_scry_cb(void* vod_p, u3_noun nun) if ( XMAS_SCRY == tag ) { c3_y* buf_y; - u3m_p("hit", hit); - u3m_p("dat", dat); c3_w len_w = _xmas_respond(pac_u, &buf_y, u3k(hit)); _xmas_rout_bufs(sam_u, buf_y, len_w, u3k(u3t(dat))); } @@ -1632,11 +1640,12 @@ _xmas_hear_bail(u3_ovum* egg_u, u3_noun lud) static void _xmas_hear_page(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w, u3_lane lan_u) { +#ifdef XMAS_DEBUG u3l_log("xmas hear page"); +#endif u3_xmas* sam_u = pac_u->sam_u; u3_noun wir = u3nc(c3__xmas, u3_nul); c3_s fra_s; - _log_pact(pac_u); u3_noun fra = u3i_bytes(pac_u->pag_u.mat_u.len_d, pac_u->pag_u.mat_u.fra_y) ; @@ -1659,28 +1668,30 @@ _xmas_hear_page(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w, u3_lane lan_u) c3_w lat_w = u3r_word(0, u3qdi_wyt(wut)); u3_noun dit = u3qdb_put(dat, u3k(num), fra); - u3m_p("dit wyt", u3qdb_wyt(dit)); if ( c3y == u3r_sing(tot, u3qdb_wyt(dit)) ) { u3l_log("finished"); + + u3l_log("now: %" PRIu64"ms", get_millis()); + u3l_log("for %u bytes", PACT_SIZE * u3r_word(0, tot)); + _tim_d = 0; + _xmas_put_request(sam_u, &pac_u->pag_u.nam_u, u3_none); return; } else if ( lat_w < IN_FLIGHT ) { - c3_w sen_w = IN_FLIGHT - lat_w; - u3l_log("send %u", sen_w); + c3_w sen_w = IN_FLIGHT; + u3_xmas_pact* nex_u = c3_calloc(sizeof(u3_xmas_pact) * sen_w); for(int i = 0; i < sen_w; i++) { - u3l_log("sending req fra %u", nex_w + i); - u3_xmas_pact* nex_u = c3_calloc(sizeof(u3_xmas_pact)); - nex_u->sam_u = sam_u; - nex_u->typ_y = PACT_PEEK; - memcpy(&nex_u->pek_u.nam_u, &pac_u->pag_u.nam_u, sizeof(u3_xmas_name)); - nex_u->pek_u.nam_u.fra_w = nex_w + i; + //u3l_log("sending req fra %u", nex_w + i); + nex_u[i].sam_u = sam_u; + nex_u[i].typ_y = PACT_PEEK; + memcpy(&nex_u[i].pek_u.nam_u, &pac_u->pag_u.nam_u, sizeof(u3_xmas_name)); + nex_u[i].pek_u.nam_u.fra_w = nex_w + i; c3_y* buf_y = c3_calloc(PACT_SIZE); - c3_w siz_w =_xmas_etch_pact(buf_y, nex_u, 0); + c3_w siz_w =_xmas_etch_pact(buf_y, &nex_u[i], 0); if( siz_w == 0 ) { u3l_log("failed to etch"); u3_assert( 0 ); - } // TODO: better route management _xmas_send_buf(sam_u, lan_u, buf_y, siz_w); @@ -1720,12 +1731,9 @@ _xmas_hear_peek(u3_xmas_pact* pac_u, u3_lane lan_u) if ( u3_none != hit ) { u3_noun tag, dat; u3x_cell(u3k(hit), &tag, &dat); - u3m_p("dat", dat); if ( tag == XMAS_SCRY ) { _xmas_add_lane_to_cache(sam_u, &pac_u->pek_u.nam_u, u3t(dat), lan_u); } else if ( tag == XMAS_ITEM ) { - u3l_log("item hit"); - u3l_log("after dat"); c3_y* buf_y; c3_w len_w = _xmas_respond(pac_u, &buf_y, u3k(dat)); @@ -1735,7 +1743,6 @@ _xmas_hear_peek(u3_xmas_pact* pac_u, u3_lane lan_u) } u3z(hit); } else { - u3l_log("adding lane to cache"); _xmas_add_lane_to_cache(sam_u, &pac_u->pek_u.nam_u, u3_nul, lan_u); // TODO: retrieve from namespace u3_noun sky = _name_to_scry(&pac_u->pek_u.nam_u); @@ -1744,7 +1751,6 @@ _xmas_hear_peek(u3_xmas_pact* pac_u, u3_lane lan_u) u3_pier_peek(sam_u->car_u.pir_u, u3_nul, u3k(u3nq(1, c3__beam, c3__xx, bem)), pac_u, _xmas_page_scry_cb); // u3_pier_peek_last(sam_u->car_u.pir_u, u3_nul, c3__xx, u3_nul, sky, pac_u, _xmas_page_scry_cb); } - u3l_log("xmas: finished hear peek"); // u3z(pax); } @@ -1754,8 +1760,9 @@ _xmas_hear(u3_xmas* sam_u, c3_w len_w, c3_y* hun_y) { - +#ifdef XMAS_DEBUG u3l_log("xmas_hear"); +#endif u3_xmas_pact* pac_u; c3_w pre_w; @@ -1823,7 +1830,6 @@ _xmas_recv_cb(uv_udp_t* wax_u, struct sockaddr_in* add_u = (struct sockaddr_in*)adr_u; u3_lane lan_u; - u3l_log("test"); lan_u.por_s = ntohs(add_u->sin_port); // u3l_log("port: %s", lan_u.por_s); From ab89c0ec1b1812beb89f825aa6f3d7681b4c79a0 Mon Sep 17 00:00:00 2001 From: Liam Fitzgerald Date: Mon, 29 Jan 2024 14:02:02 -0500 Subject: [PATCH 020/430] xmas: refactoring congestion control --- pkg/vere/BUILD.bazel | 1 + pkg/vere/io/cong.h | 32 ++++++++ pkg/vere/io/cubic.c | 130 ++++++++++++++++++++++++++++++++ pkg/vere/io/cubic.h | 33 ++++++++ pkg/vere/io/{xmas => }/serial.h | 0 pkg/vere/io/xmas.c | 3 + pkg/vere/io/xmas/cubic.c | 129 ------------------------------- 7 files changed, 199 insertions(+), 129 deletions(-) create mode 100644 pkg/vere/io/cong.h create mode 100644 pkg/vere/io/cubic.c create mode 100644 pkg/vere/io/cubic.h rename pkg/vere/io/{xmas => }/serial.h (100%) delete mode 100644 pkg/vere/io/xmas/cubic.c diff --git a/pkg/vere/BUILD.bazel b/pkg/vere/BUILD.bazel index 5bc093572c..316a40a93d 100644 --- a/pkg/vere/BUILD.bazel +++ b/pkg/vere/BUILD.bazel @@ -96,6 +96,7 @@ vere_library( "*.h", "db/*.c", "io/*.c", + "io/*.h", ], exclude = [ "main.c", diff --git a/pkg/vere/io/cong.h b/pkg/vere/io/cong.h new file mode 100644 index 0000000000..a8e025469f --- /dev/null +++ b/pkg/vere/io/cong.h @@ -0,0 +1,32 @@ +/// @file +/// +#include "vere.h" +#include +#include + +#include "noun.h" +#include "ur.h" + +typedef struct _u3_gage { + c3_w rtt_w; // rtt + c3_w rto_w; // rto + c3_w rtv_w; // rttvar + c3_w wnd_w; // cwnd + c3_w wnc_w; // cwnd_cnt + c3_w sst_w; // ssthresh + c3_w con_w; // counter + void* alg_u; // algorithm backpointer + c3_c* alg_c; // algorithm name +} u3_gage; + +typedef void (*u3_gage_func)(u3_gage*); + + +typedef struct _u3_gage_alg { + u3_gage_func int_f; //initialise + u3_gage_func don_f; // dispose + u3_gage_func ack_f; // received ack + u3_gage_func los_f; // lost +} u3_gage_alg; + + diff --git a/pkg/vere/io/cubic.c b/pkg/vere/io/cubic.c new file mode 100644 index 0000000000..4cfb35733f --- /dev/null +++ b/pkg/vere/io/cubic.c @@ -0,0 +1,130 @@ +/// @file +/// +#include "vere.h" +#include +#include + +#include "noun.h" +#include "ur.h" +#include "cubic.h" + +#define FAST_CONVERGENCE 0 + +const c3_d cee_d = 13; // (0.4*32) +const c3_d bet_d = 3; // (.2*16) + + +static c3_d +_get_now_micros() +{ + struct timeval tim_u; + gettimeofday(&tim_u, NULL); + return (tim_u.tv_sec * 1000000) + tim_u.tv_usec; +} + +static c3_d +_cube(c3_d val_d) +{ + return val_d * val_d * val_d; + +} + +void u3_cbic_init(u3_gage* gag_u) +{ + gag_u->alg_u = c3_calloc(sizeof(u3_cbic)); + gag_u->alg_c = "CUBIC"; +} + +void u3_cbic_done(u3_gage* gag_u) +{ + free(gag_u->alg_u); +} + + +void u3_cbic_reset(u3_gage* gag_u) +{ + u3_cbic* cub_u = gag_u->alg_u; + cub_u->las_w = 0; + cub_u->poc_d = 0; + cub_u->rig_w = 0; + cub_u->del_w = 0; + cub_u->win_w = 0; + cub_u->cac_w = 0; +} + + +void u3_cbic_tcp(u3_cbic* cub_u) +{ + u3_cbic sat_u = cub_u->sat_u; + u3_cong con_u = cub_u->con_u; + + sat_u.tcp_w = sat_u.tcp_w + ((c3_w)((3*bet_f)/(2-bet_f)) * (sat_u.cac_w/con_u.wnd_w)); + sat_u.cac_w = 0; + if ( sat_u.tcp_w > con_u.wnd_w ) { + sat_u.max_w = con_u.wnd_w/(sat_u.tcp_w-con_u.wnd_w); + if ( sat_u.cnt_w > sat_u.max_w ) { + sat_u.cnt_w = sat_u.max_w; + } + } +} + +void u3_cbic_update(u3_gage* gag_u) +{ + u3_cbic* cub_u = gag_u->alg_u; + cub_u->cac_w += 1; + c3_d tim_d = _get_now_micros(); + + // if no epoch start set, then begin now + if ( 0 == cub_u->poc_d ) { + cub_u->poc_d = tim_d; + if ( ( gag_u->wnd_w < cub_u->las_w ) && FAST_CONVERGENCE ) { + //sat_u.las_w = con_u.wnd_w * + // set origin point to w last max + } else { + cub_u->kay_w = 0; + cub_u->rig_w = gag_u->wnd_w; + } + cub_u->cac_w = 1; + cub_u->win_w = gag_u->wnd_w; + } + c3_d tee_d = tim_d + cub_u->del_w - cub_u->poc_d; + c3_d tar_d = cub_u->rig_w + ((cee_d * _cube(tee_d - cub_u->kay_w)) >> 5); + if ( tar_d > gag_u->wnd_w ) { + cub_u->cnt_w = gag_u->wnd_w / ( tar_d - gag_u->wnd_w ); + } else { + cub_u->cnt_w = gag_u->wnd_w * 100; + } + if ( c3y == cub_u->tcp_o ) { + u3_cbic_tcp(cub_u); + } +} + + +void u3_cbic_on_ack(u3_gage* gag_u) +{ + c3_w rtt_w = gag_u->rtt_w; // TODO actually update + u3_cbic* cub_u = gag_u->alg_u; + if ( 0 == cub_u->del_w ) { + cub_u->del_w = c3_min(cub_u->del_w, rtt_w); + } else { + cub_u->del_w = rtt_w; + } + + if ( gag_u->wnd_w <= gag_u->sst_w ) { + gag_u->wnd_w += 1; + } else { + if ( gag_u->wnc_w > gag_u->wnd_w ) { + gag_u->wnd_w += 1; + gag_u->wnc_w = 0; + } else { + gag_u->wnc_w += 1; + } + } +} + +void u3_cbic_lost(u3_gage* gag_u) +{ + +} + + diff --git a/pkg/vere/io/cubic.h b/pkg/vere/io/cubic.h new file mode 100644 index 0000000000..1b06938ea1 --- /dev/null +++ b/pkg/vere/io/cubic.h @@ -0,0 +1,33 @@ +/// @file +/// +#include "vere.h" +#include +#include + +#include "noun.h" +#include "ur.h" +#include "cong.h" + + +typedef struct _u3_cbic { + c3_w las_w; // w_last_max + c3_d poc_d; // epoch start + c3_w rig_w; // origin point + c3_w del_w; // delay min + c3_w win_w; // w_tcp + c3_w cac_w; // ack count + c3_w kay_w; // K + c3_w cnt_w; // cnt + c3_o tcp_o; // tcp friendliness + c3_w tcp_w; // w_tcp + c3_w max_w; // max count +} u3_cbic; + + +void u3_cbic_init(u3_gage*); + +void u3_cbic_done(u3_gage*); + +void u3_cbic_on_ack(u3_gage*); + +void u3_cbic_lost(u3_gage*); diff --git a/pkg/vere/io/xmas/serial.h b/pkg/vere/io/serial.h similarity index 100% rename from pkg/vere/io/xmas/serial.h rename to pkg/vere/io/serial.h diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c index e5688be724..9f442853cf 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/xmas.c @@ -5,6 +5,8 @@ #include "noun.h" #include "ur.h" +#include "cong.h" +#include "cubic.h" #include #include #include @@ -63,6 +65,7 @@ typedef struct _u3_xmas { u3_cue_xeno* sil_u; // cue handle u3p(u3h_root) her_p; // u3p(u3h_root) pac_p; // pending + u3_gage* gag_u; c3_w imp_w[256]; time_t imp_t[256]; // imperial IP timestamps c3_o imp_o[256]; // imperial print status diff --git a/pkg/vere/io/xmas/cubic.c b/pkg/vere/io/xmas/cubic.c deleted file mode 100644 index 4ccbf4dfea..0000000000 --- a/pkg/vere/io/xmas/cubic.c +++ /dev/null @@ -1,129 +0,0 @@ -/// @file -/// -#include "vere.h" -#include - -#include "noun.h" -#include "ur.h" - -#define FAST_CONVERGENCE 0 - -const float cee_f = 0.4; -const float bet_f = 0.2; - -typedef struct _u3_cbic_stat { - c3_w las_w; // w_last_max - c3_d poc_d; // epoch start - c3_w rig_w; // origin point - c3_w del_w; // delay min - c3_w win_w; // w_tcp - c3_w cac_w; // ack count - c3_w kay_w; // K - c3_w cnt_w; // cnt - c3_o tcp_o; // tcp friendliness - c3_w tcp_w; // w_tcp - c3_w max_w; // max count -} u3_cbic_stat; - -typedef struct _u3_cong { - c3_w rtt_w; // rtt - c3_w rto_w; // rto - c3_w rtv_w; // rttvar - c3_w wnd_w; // cwnd - c3_w wnc_w; // cwnd_cnt - c3_w sst_w; // ssthresh - c3_w con_w; // counter -} u3_cong; - -typedef struct _u3_cbic { - u3_cong con_u; - u3_cbic_stat sat_u; -} u3_cbic; - - -void u3_cbic_reset(u3_cbic_stat* sat_u) -{ - sat_u->las_w = 0; - sat_u->poc_d = 0; - sat_u->rig_w = 0; - sat_u->del_w = 0; - sat_u->win_w = 0; - sat_u->cac_w = 0; -} - -void u3_cbic_init(u3_cbic_stat* sat_u) -{ - -} - -void u3_cbic_tcp(u3_cbic* cub_u) -{ - u3_cbic_stat sat_u = cub_u->sat_u; - u3_cong con_u = cub_u->con_u; - - sat_u.tcp_w = sat_u.tcp_w + ((c3_w)((3*bet_f)/(2-bet_f)) * (sat_u.cac_w/con_u.wnd_w)); - sat_u.cac_w = 0; - if ( sat_u.tcp_w > con_u.wnd_w ) { - sat_u.max_w = con_u.wnd_w/(sat_u.tcp_w-con_u.wnd_w); - if ( sat_u.cnt_w > sat_u.max_w ) { - sat_u.cnt_w = sat_u.max_w; - } - } -} - -void u3_cbic_update(u3_cbic* cub_u) -{ - cub_u->sat_u.cac_w += 1; - u3_cong con_u = cub_u->con_u; - u3_cbic_stat sat_u = cub_u->sat_u; - c3_d tim_d = time(NULL); - - // if no epoch start set, then begin now - if ( 0 == sat_u.poc_d ) { - sat_u.poc_d = tim_d; - if ( ( con_u.wnd_w < sat_u.las_w ) && FAST_CONVERGENCE ) { - //sat_u.las_w = con_u.wnd_w * - // set origin point to w last max - } else { - sat_u.kay_w = 0; - sat_u.rig_w = con_u.wnd_w; - } - sat_u.cac_w = 1; - sat_u.win_w = con_u.wnd_w; - } - c3_d tee_d = tim_d + sat_u.del_w - sat_u.poc_d; - c3_d tar_d = sat_u.rig_w + (c3_d)(cee_f * pow((float)tee_d - (float)sat_u.kay_w, 3)); - if ( tar_d > con_u.wnd_w ) { - sat_u.cnt_w = con_u.wnd_w / ( tar_d - con_u.wnd_w ); - } else { - sat_u.cnt_w = con_u.wnd_w * 100; - } - if ( c3y == sat_u.tcp_o ) { - u3_cbic_tcp(cub_u); - } -} - - - -void u3_cbic_on_ack(u3_cbic* cub_u) -{ - c3_w rtt_w = cub_u->con_u.rtt_w; // TODO actually update - if ( 0 == cub_u->sat_u.del_w ) { - cub_u->sat_u.del_w = c3_min(cub_u->sat_u.del_w, rtt_w); - } else { - cub_u->sat_u.del_w = rtt_w; - } - - if ( cub_u->con_u.wnd_w <= cub_u->con_u.sst_w ) { - cub_u->con_u.wnd_w += 1; - } else { - if ( cub_u->con_u.wnc_w > cub_u->con_u.wnd_w ) { - cub_u->con_u.wnd_w += 1; - cub_u->con_u.wnc_w = 0; - } else { - cub_u->con_u.wnc_w += 1; - } - } -} - - From 6a11be07174f11182d80742cdaf470ea6083b1cd Mon Sep 17 00:00:00 2001 From: Liam Fitzgerald Date: Tue, 30 Jan 2024 12:45:53 -0500 Subject: [PATCH 021/430] xmas: handle request tables better --- pkg/noun/jets/k.h | 5 + pkg/noun/jets/q.h | 1 + pkg/vere/io/cong.h | 2 + pkg/vere/io/cubic.c | 11 +- pkg/vere/io/xmas.c | 365 ++++++++++++++++++++++++++++++++------------ 5 files changed, 278 insertions(+), 106 deletions(-) diff --git a/pkg/noun/jets/k.h b/pkg/noun/jets/k.h index f4587bef58..27733fb5e0 100644 --- a/pkg/noun/jets/k.h +++ b/pkg/noun/jets/k.h @@ -93,6 +93,11 @@ u3_weak u3kdb_put(u3_noun a, u3_noun b, u3_noun c); + /* u3kdb_del(): map del for key `b` + */ + u3_weak + u3kdb_del(u3_noun a, u3_noun b); + /* u3kdb_has(): test for get. */ u3_noun diff --git a/pkg/noun/jets/q.h b/pkg/noun/jets/q.h index 9ed5720db3..a47838676f 100644 --- a/pkg/noun/jets/q.h +++ b/pkg/noun/jets/q.h @@ -87,6 +87,7 @@ u3_noun u3qdb_any(u3_noun, u3_noun); u3_noun u3qdb_apt(u3_noun); u3_noun u3qdb_bif(u3_noun, u3_noun); + u3_noun u3qdb_del(u3_noun, u3_noun); u3_noun u3qdb_dif(u3_noun, u3_noun); u3_noun u3qdb_gas(u3_noun, u3_noun); u3_noun u3qdb_get(u3_noun, u3_noun); diff --git a/pkg/vere/io/cong.h b/pkg/vere/io/cong.h index a8e025469f..3dcf868ad0 100644 --- a/pkg/vere/io/cong.h +++ b/pkg/vere/io/cong.h @@ -17,6 +17,8 @@ typedef struct _u3_gage { c3_w con_w; // counter void* alg_u; // algorithm backpointer c3_c* alg_c; // algorithm name + // + uv_timer_t tim_u; } u3_gage; typedef void (*u3_gage_func)(u3_gage*); diff --git a/pkg/vere/io/cubic.c b/pkg/vere/io/cubic.c index 4cfb35733f..c42087c867 100644 --- a/pkg/vere/io/cubic.c +++ b/pkg/vere/io/cubic.c @@ -53,19 +53,18 @@ void u3_cbic_reset(u3_gage* gag_u) } -void u3_cbic_tcp(u3_cbic* cub_u) +void u3_cbic_tcp(u3_gage* gag_u) { - u3_cbic sat_u = cub_u->sat_u; - u3_cong con_u = cub_u->con_u; + u3_cbic* cub_u = gag_u->alg_u; - sat_u.tcp_w = sat_u.tcp_w + ((c3_w)((3*bet_f)/(2-bet_f)) * (sat_u.cac_w/con_u.wnd_w)); + /*sat_u.tcp_w = sat_u.tcp_w + ((c3_w)((3*bet_f)/(2-bet_f)) * (sat_u.cac_w/con_u.wnd_w)); sat_u.cac_w = 0; if ( sat_u.tcp_w > con_u.wnd_w ) { sat_u.max_w = con_u.wnd_w/(sat_u.tcp_w-con_u.wnd_w); if ( sat_u.cnt_w > sat_u.max_w ) { sat_u.cnt_w = sat_u.max_w; } - } + } */ } void u3_cbic_update(u3_gage* gag_u) @@ -95,7 +94,7 @@ void u3_cbic_update(u3_gage* gag_u) cub_u->cnt_w = gag_u->wnd_w * 100; } if ( c3y == cub_u->tcp_o ) { - u3_cbic_tcp(cub_u); + u3_cbic_tcp(gag_u); } } diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c index 9f442853cf..b25f9564a0 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/xmas.c @@ -5,7 +5,6 @@ #include "noun.h" #include "ur.h" -#include "cong.h" #include "cubic.h" #include #include @@ -17,7 +16,9 @@ #include #include -//#define XMAS_DEBUG c3y +c3_o dop_o = c3n; + +#define XMAS_DEBUG c3y #define XMAS_VER 1 #define FINE_PAGE 4096 // packets per page #define FINE_FRAG 1024 // bytes per fragment packet @@ -51,6 +52,18 @@ static c3_y XMAS_COOKIE[4] = { 0x5e, 0x1d, 0xad, 0x51 }; #define CHECK_BOUNDS(cur) if ( len_w < cur ) { u3l_log("xmas: failed parse (%i,%i) at line %i", len_w, cur, __LINE__); return 0; } #define safe_dec(num) num == 0 ? num : num - 1; +/** typedef u3_xmas_pack_stat u3_noun + * [last=@da tries=@ud] + */ +typedef struct _u3_pend_req { + c3_w nex_w; + c3_w tot_w; + uv_timer_t tim_u; + u3_noun wat; // ((mop @ud packet-state) lte) + u3_noun dat; // ((mop @ud *) lte) +} u3_pend_req; + + /* _u3_xmas: next generation networking */ typedef struct _u3_xmas { @@ -71,7 +84,13 @@ typedef struct _u3_xmas { c3_o imp_o[256]; // imperial print status c3_c* dns_c; // // jj - u3p(u3h_root) req_p; + u3p(u3h_root) req_p; // hashtable of u3_pend_req + struct { + u3_noun put; + u3_noun del; + u3_noun tap; + u3_noun pry; + } mop_u; // pointer to ordered map functions } u3_xmas; typedef enum _u3_xmas_ptag { @@ -226,6 +245,7 @@ typedef struct _u3_seal { } u3_seal; + c3_d _tim_d = 0; static c3_d @@ -304,6 +324,39 @@ _log_page_pact(u3_xmas_page_pact *pac_u) u3l_log("frag len: %u", mat_u->len_d); } +static void +_log_gage(u3_gage* gag_u) +{ + u3l_log("gauge"); + u3l_log("rtt: %u", gag_u->rtt_w); + u3l_log("rto: %u", gag_u->rto_w); + u3l_log("rttvar: %u", gag_u->rtv_w); + u3l_log("cwnd: %u", gag_u->wnd_w); + u3l_log("cwnd cnt: %u", gag_u->wnc_w); + u3l_log("ssthresh: %u", gag_u->sst_w); + u3l_log("counter: %u", gag_u->con_w); + u3l_log("algorithm: %s", gag_u->alg_c); +} + +static void +_log_pend_req(u3_pend_req* req_u) +{ + if( req_u == NULL ) { + u3l_log("pending request was NULL"); + } + u3l_log("next: %u", req_u->nex_w); + u3l_log("total: %u", req_u->tot_w); + u3l_log("total: %u", req_u->tot_w); + u3l_log("timer in: %" PRIu64 " ms", uv_timer_get_due_in(&req_u->tim_u)); + if ( 0 ) { + u3m_p("wat", req_u->wat); + u3m_p("dat", req_u->dat); + } else { + u3l_log("waiting for %u", u3r_word(0, u3qdb_wyt(req_u->wat))); + u3l_log("have %u", u3r_word(0, u3qdb_wyt(req_u->dat))); + } +} + static void _log_pact(u3_xmas_pact* pac_u) { @@ -324,6 +377,158 @@ _log_pact(u3_xmas_pact* pac_u) } } +static c3_d +_get_now_micros() +{ + struct timeval tim_u; + gettimeofday(&tim_u, NULL); + return (tim_u.tv_sec * 1000000) + tim_u.tv_usec; +} + +static c3_d +_abs_dif(c3_d ayy_d, c3_d bee_d) +{ + return ayy_d > bee_d ? ayy_d - bee_d : bee_d - ayy_d; +} + +static c3_d +_clamp_rto(c3_d rto_d) { + return c3_min(c3_max(rto_d, 200000), 25000000); +} + + +u3_noun _xmas_request_key(u3_xmas_name* nam_u) +{ + u3_noun pax = u3do("stab", u3dt("cat", 3, '/', u3i_string(nam_u->pat_c))); + u3_noun res = u3nt(u3i_chubs(2, nam_u->her_d), u3i_word(nam_u->rif_w), pax); + return res; +} + +static u3_pend_req* +_xmas_get_request(u3_xmas* sam_u, u3_xmas_name* nam_u) { + u3_pend_req* ret_u = NULL; + u3_noun key = _xmas_request_key(nam_u); + + u3_weak res = u3h_get(sam_u->req_p, key); + if ( res != u3_none ) { + ret_u = u3to(u3_pend_req, res); + } + + _log_pend_req(ret_u); + return ret_u; +} + +static void +_xmas_put_request(u3_xmas* sam_u, u3_xmas_name* nam_u, u3_pend_req* req_u) { + u3_noun key = _xmas_request_key(nam_u); + u3l_log("put request for name"); + _log_name(nam_u); + u3_pend_req* old_u = _xmas_get_request(sam_u, nam_u); + u3l_log("old"); + _log_pend_req(old_u); + u3_pend_req* new_u = req_u; + if ( old_u == NULL ) { + new_u = c3_calloc(sizeof(u3_pend_req)); + memcpy(new_u, req_u, sizeof(*req_u)); + } else { + memcpy(old_u, new_u, sizeof(*old_u)); + new_u = old_u; + } + _log_pend_req(new_u); + u3_noun val = u3of(u3_pend_req, new_u); + u3h_put(sam_u->req_p, key, val); + u3z(key); +} + +static u3_weak +_xmas_req_pact_done(u3_xmas* sam_u, u3_xmas_name *nam_u, c3_w tot_w, u3_noun fra) +{ + u3_weak ret = u3_none; + c3_d now_d = _get_now_micros(); + u3_pend_req* req_u = _xmas_get_request(sam_u, nam_u); + + if ( NULL != req_u ) { + // TODO: optional assertions? + req_u->dat = u3n_slam_on( + u3k(sam_u->mop_u.put), + u3nt(req_u->dat, u3i_word(nam_u->fra_w), fra) + ); + req_u->wat = u3t(u3n_slam_on( + u3k(sam_u->mop_u.del), + u3nc(req_u->wat, u3i_word(nam_u->fra_w)) + )); + + u3_noun tot = u3i_word(req_u->tot_w); + if ( 0 != req_u->tot_w && c3y == u3r_sing(u3qdb_wyt(req_u->dat), tot) ) { + ret = u3dc("rip", 13, u3n_slam_on(u3k(sam_u->mop_u.tap), req_u->dat)); + //_xmas_put_request_raw(sam_u, nam_u, u3_none); + } else { + _xmas_put_request(sam_u, nam_u, req_u); + } + u3z(tot); + } else { + u3l_log("xmas: attempting to mark packet done, no request"); + u3m_bail(c3__foul); + } + u3z(fra); + return ret; +} + +static c3_w +_xmas_req_get_nex(u3_xmas* sam_u, u3_xmas_name* nam_u) +{ + c3_w res_w = 0; + u3_pend_req* req_u = _xmas_get_request(sam_u, nam_u); + if ( NULL != req_u ) { + res_w = req_u->nex_w; + } + return res_w; +} + +static c3_o +_xmas_req_is_done(u3_xmas* sam_u, u3_xmas_name* nam_u, c3_w tot_w) +{ + c3_o ret_o = c3n; + u3_pend_req* req_u = _xmas_get_request(sam_u, nam_u); + if ( NULL != req_u ) { + u3_noun tot = u3i_word(tot_w); + ret_o = u3r_sing(tot, u3qdb_wyt(req_u->dat)); + u3z(tot); + } + + return ret_o; +} + +static void +_xmas_req_pact_sent(u3_xmas* sam_u, u3_xmas_name* nam_u) +{ + c3_d now_d = _get_now_micros(); + u3_noun pac = u3nt(u3i_chub(now_d), 1, 0); + + u3_pend_req* req_u = _xmas_get_request(sam_u, nam_u); + + if ( NULL != req_u ) { + // TODO: optional assertions? + req_u->wat = u3n_slam_on( + u3k(sam_u->mop_u.put), + u3nt(req_u->dat, u3i_word(nam_u->fra_w), pac) + ); + + } else { + req_u = alloca(sizeof(u3_pend_req)); + u3l_log("TODO: restart requests"); + _log_name(nam_u); + u3_assert( nam_u->fra_w == 0 ); + req_u->nex_w = 1; + req_u->tot_w = 0; + req_u->dat = u3_nul; + req_u->wat = u3n_slam_on( + u3k(sam_u->mop_u.put), + u3nt(u3_nul, u3i_word(nam_u->fra_w), u3k(pac)) + ); + } + _xmas_put_request(sam_u, nam_u, req_u); +} // cut and pasted from ames.c // @@ -549,35 +754,6 @@ _xmas_rout_pact(u3_xmas_pact* pac_u, u3_noun lan) { return suc_o; } -u3_noun _xmas_request_key(u3_xmas_name* nam_u) -{ - u3_noun pax = u3do("stab", u3dt("cat", 3, '/', u3i_string(nam_u->pat_c))); - u3_noun res = u3nt(u3i_chubs(2, nam_u->her_d), u3i_word(nam_u->rif_w), pax); - return res; -} - -static u3_weak -_xmas_get_request(u3_xmas* sam_u, u3_xmas_name* nam_u) { - u3_noun key = _xmas_request_key(nam_u); - - u3_weak res = u3h_git(sam_u->req_p, key); - u3z(key); - return res; -} - -/** RETAIN - */ -static void -_xmas_put_request(u3_xmas* sam_u, u3_xmas_name* nam_u, u3_noun val) -{ - u3_noun key = _xmas_request_key(nam_u); - - u3h_put(sam_u->req_p, key, val); - - u3z(key); -} - - // refcounted // RETAIN @@ -1260,26 +1436,20 @@ _xmas_rout_pacs(u3_xmas_pact* pac_u, u3_noun las) } static void -_xmas_update_req_peek(u3_xmas_pact* pac_u) { - u3_weak req = _xmas_get_request(pac_u->sam_u, &pac_u->pek_u.nam_u); +_xmas_timer_cb(uv_timer_t* tim_u) { + c3_w rto_w = 500; - if ( u3_none == req ) { - u3_assert( 0 == pac_u->pek_u.nam_u.fra_w ); // TODO restart request - _xmas_put_request(pac_u->sam_u, &pac_u->pek_u.nam_u, u3nq(0, u3_nul, u3_nul, u3nc(0, u3_nul))); - } else { - u3_noun tot, wat, mis, nex, dat; - u3x_quil(req, &tot, &wat, &mis, &nex, &dat); - u3_noun fra = u3i_word(pac_u->pek_u.nam_u.fra_w); - if ( c3y == u3r_sing(fra, nex) ) { - nex = fra; - } +} - u3_noun nat = u3kdi_put(u3k(wat), u3k(fra)); +static void +_xmas_update_req_peek(u3_xmas_pact* pac_u) +{ + u3_xmas* sam_u = pac_u->sam_u; + u3l_log("updating req"); + _xmas_req_pact_sent(sam_u, &pac_u->pek_u.nam_u); + u3l_log("updated req"); - u3_noun new = u3nq(u3k(tot), nat, u3k(mis), u3nc(u3k(nex), u3k(dat))); - _xmas_put_request(pac_u->sam_u, &pac_u->pek_u.nam_u, new); - //u3z(new); u3z(req); - } + // TODO: set timers } static void @@ -1421,6 +1591,9 @@ _xmas_exit_cb(uv_handle_t* had_u) u3h_free(sam_u->her_p); u3h_free(sam_u->req_p); c3_free(sam_u); + + u3_cbic_done(sam_u->gag_u); + c3_free(sam_u->gag_u); } static void @@ -1436,7 +1609,7 @@ _xmas_io_exit(u3_auto* car_u) static u3_weak _xmas_get_peer(u3_xmas* sam_u, u3_noun her) { - return u3h_git(sam_u->her_p, her); + return u3h_get(sam_u->her_p, her); } /* @@ -1456,7 +1629,7 @@ _xmas_put_sponsee(u3_xmas* sam_u, u3_noun her, u3_noun las) static c3_o _xmas_add_galaxy_pend(u3_xmas* sam_u, u3_noun her, u3_noun pen) { - u3_weak old = u3h_git(sam_u->her_p, her); + u3_weak old = u3h_get(sam_u->her_p, her); u3_noun pes = u3_nul; u3_noun wat; if ( u3_none != old ) { @@ -1493,7 +1666,7 @@ static u3_weak _xmas_get_cache(u3_xmas* sam_u, u3_xmas_name* nam_u) { u3_noun pax = _name_to_scry(nam_u); - u3_weak res = u3h_git(sam_u->pac_p, pax); + u3_weak res = u3h_get(sam_u->pac_p, pax); if ( u3_none == res ) { //u3m_p("miss", u3k(pax)); } else { @@ -1640,6 +1813,28 @@ _xmas_hear_bail(u3_ovum* egg_u, u3_noun lud) u3_ovum_free(egg_u); } +static void _xmas_handle_ack(u3_xmas* sam_u, u3_noun pas) +{ + u3_noun las, tie, kip; + u3x_cell(pas, &las, &tie); + c3_d las_d = u3r_chub(0, las); + c3_w tie_w = u3r_word(0, tie); + u3_gage* gag_u = sam_u->gag_u; + + gag_u->con_w++; + + c3_d now_d = _get_now_micros(); + c3_d rtt_d = now_d < las_d ? 0 : now_d - las_d; + + c3_d err_d = _abs_dif(rtt_d, gag_u->rtt_w); + + gag_u->rtt_w = (rtt_d + (gag_u->rtt_w * 7)) >> 3; + gag_u->rtv_w = (err_d + (gag_u->rtv_w * 7)) >> 3; + gag_u->rto_w = _clamp_rto(gag_u->rtt_w + (4*gag_u->rtv_w)); + + u3_cbic_on_ack(gag_u); +} + static void _xmas_hear_page(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w, u3_lane lan_u) { @@ -1651,60 +1846,21 @@ _xmas_hear_page(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w, u3_lane lan_u) c3_s fra_s; u3_noun fra = u3i_bytes(pac_u->pag_u.mat_u.len_d, pac_u->pag_u.mat_u.fra_y) ; - - - u3_weak req = _xmas_get_request(sam_u, &pac_u->pag_u.nam_u); - - if ( u3_none == req ) { - u3l_log("xmas: heard page w/ no request, dropping"); // TODO +/* if ( dop_o == c3n && pac_u->pag_u.nam_u.fra_w == 150) { + dop_o = c3y; + u3l_log("simulating dropped packet"); return; - } - - u3_noun tot, wat, mis, nex, dat; - u3x_quil(req, &tot, &wat, &mis, &nex, &dat); - if ( tot == 0 ) { - tot = u3i_word(pac_u->pag_u.mat_u.tot_w); - } - c3_w nex_w = u3r_word(0, nex); - u3_noun num = u3i_word(pac_u->pag_u.nam_u.fra_w); - u3_noun wut = u3qdi_del(wat, u3k(num)); - c3_w lat_w = u3r_word(0, u3qdi_wyt(wut)); - - u3_noun dit = u3qdb_put(dat, u3k(num), fra); + }*/ + u3_weak res = _xmas_req_pact_done(sam_u, &pac_u->pag_u.nam_u, pac_u->pag_u.mat_u.tot_w, fra); - if ( c3y == u3r_sing(tot, u3qdb_wyt(dit)) ) { + if ( u3_none != res ) { u3l_log("finished"); u3l_log("now: %" PRIu64"ms", get_millis()); - u3l_log("for %u bytes", PACT_SIZE * u3r_word(0, tot)); - _tim_d = 0; - _xmas_put_request(sam_u, &pac_u->pag_u.nam_u, u3_none); + u3l_log("for %u bytes", u3r_met(3, res)); return; - } else if ( lat_w < IN_FLIGHT ) { - c3_w sen_w = IN_FLIGHT; - u3_xmas_pact* nex_u = c3_calloc(sizeof(u3_xmas_pact) * sen_w); - for(int i = 0; i < sen_w; i++) { - //u3l_log("sending req fra %u", nex_w + i); - nex_u[i].sam_u = sam_u; - nex_u[i].typ_y = PACT_PEEK; - memcpy(&nex_u[i].pek_u.nam_u, &pac_u->pag_u.nam_u, sizeof(u3_xmas_name)); - nex_u[i].pek_u.nam_u.fra_w = nex_w + i; - c3_y* buf_y = c3_calloc(PACT_SIZE); - c3_w siz_w =_xmas_etch_pact(buf_y, &nex_u[i], 0); - if( siz_w == 0 ) { - u3l_log("failed to etch"); - u3_assert( 0 ); - } - // TODO: better route management - _xmas_send_buf(sam_u, lan_u, buf_y, siz_w); - wut = u3qdi_put(wut, u3i_word(nex_u->pek_u.nam_u.fra_w)); - nex = u3dc("add", 1, nex); - } } - _xmas_put_request(sam_u, &pac_u->pag_u.nam_u, u3nq(u3k(tot), u3k(wut), u3k(mis), u3nc(u3k(nex), u3k(dit)))); - //u3z(req); - } static void @@ -1778,7 +1934,7 @@ _xmas_hear(u3_xmas* sam_u, pac_u = c3_calloc(sizeof(*pac_u)); pac_u->sam_u = sam_u; c3_w lin_w = _xmas_sift_pact(pac_u, hun_y, len_w); - if( lin_w == 0 ) { + if ( lin_w == 0 ) { #ifdef XMAS_DEBUG u3l_log("xmas: failed to parse packet"); _log_pact(pac_u); @@ -1789,7 +1945,7 @@ _xmas_hear(u3_xmas* sam_u, } #ifdef XMAS_DEBUG - _log_peek_pact(&pac_u->pek_u); + //_log_peek_pact(&pac_u->pek_u); u3l_log("xmas: sifted packet"); #endif @@ -1958,6 +2114,15 @@ u3_xmas_io_init(u3_pier* pir_u) car_u->io.kick_f = _xmas_io_kick; car_u->io.exit_f = _xmas_io_exit; + sam_u->gag_u = c3_calloc(sizeof(u3_gage)); + + uv_timer_init(u3L, &sam_u->gag_u->tim_u); + u3_cbic_init(sam_u->gag_u); + + sam_u->mop_u.put = u3v_wish("put:((on @ud *) lte)"); + sam_u->mop_u.del = u3v_wish("del:((on @ud *) lte)"); + sam_u->mop_u.pry = u3v_wish("pry:((on @ud *) lte)"); + sam_u->mop_u.tap = u3v_wish("tap:((on @ud *) lte)"); /*{ u3_noun now; From 0b10563ddfbc7aa7aeea5a4f70c06114f95dcf21 Mon Sep 17 00:00:00 2001 From: Liam Fitzgerald Date: Tue, 30 Jan 2024 14:05:33 -0500 Subject: [PATCH 022/430] wip --- pkg/vere/io/xmas.c | 64 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 54 insertions(+), 10 deletions(-) diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c index b25f9564a0..11f6d9994e 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/xmas.c @@ -343,11 +343,11 @@ _log_pend_req(u3_pend_req* req_u) { if( req_u == NULL ) { u3l_log("pending request was NULL"); + return; } u3l_log("next: %u", req_u->nex_w); u3l_log("total: %u", req_u->tot_w); - u3l_log("total: %u", req_u->tot_w); - u3l_log("timer in: %" PRIu64 " ms", uv_timer_get_due_in(&req_u->tim_u)); + //u3l_log("timer in: %" PRIu64 " ms", uv_timer_get_due_in(&req_u->tim_u)); if ( 0 ) { u3m_p("wat", req_u->wat); u3m_p("dat", req_u->dat); @@ -414,18 +414,13 @@ _xmas_get_request(u3_xmas* sam_u, u3_xmas_name* nam_u) { ret_u = u3to(u3_pend_req, res); } - _log_pend_req(ret_u); return ret_u; } static void _xmas_put_request(u3_xmas* sam_u, u3_xmas_name* nam_u, u3_pend_req* req_u) { u3_noun key = _xmas_request_key(nam_u); - u3l_log("put request for name"); - _log_name(nam_u); u3_pend_req* old_u = _xmas_get_request(sam_u, nam_u); - u3l_log("old"); - _log_pend_req(old_u); u3_pend_req* new_u = req_u; if ( old_u == NULL ) { new_u = c3_calloc(sizeof(u3_pend_req)); @@ -434,7 +429,6 @@ _xmas_put_request(u3_xmas* sam_u, u3_xmas_name* nam_u, u3_pend_req* req_u) { memcpy(old_u, new_u, sizeof(*old_u)); new_u = old_u; } - _log_pend_req(new_u); u3_noun val = u3of(u3_pend_req, new_u); u3h_put(sam_u->req_p, key, val); u3z(key); @@ -458,8 +452,9 @@ _xmas_req_pact_done(u3_xmas* sam_u, u3_xmas_name *nam_u, c3_w tot_w, u3_noun fra u3nc(req_u->wat, u3i_word(nam_u->fra_w)) )); + req_u->tot_w = tot_w; u3_noun tot = u3i_word(req_u->tot_w); - if ( 0 != req_u->tot_w && c3y == u3r_sing(u3qdb_wyt(req_u->dat), tot) ) { + if ( c3y == u3r_sing(u3qdb_wyt(req_u->dat), tot) ) { ret = u3dc("rip", 13, u3n_slam_on(u3k(sam_u->mop_u.tap), req_u->dat)); //_xmas_put_request_raw(sam_u, nam_u, u3_none); } else { @@ -485,6 +480,22 @@ _xmas_req_get_nex(u3_xmas* sam_u, u3_xmas_name* nam_u) return res_w; } +static c3_w +_xmas_req_get_cwnd(u3_xmas* sam_u, u3_xmas_name* nam_u) +{ + c3_w res_w = 0; + u3_pend_req* req_u = _xmas_get_request(sam_u, nam_u); + if ( NULL != req_u ) { + res_w = req_u->nex_w; + } + c3_w liv_w = u3r_word(0, u3qdb_wyt(req_u->wat)); + + if ( liv_w < IN_FLIGHT ) { + return IN_FLIGHT - liv_w; + } + return 0; +} + static c3_o _xmas_req_is_done(u3_xmas* sam_u, u3_xmas_name* nam_u, c3_w tot_w) { @@ -508,12 +519,14 @@ _xmas_req_pact_sent(u3_xmas* sam_u, u3_xmas_name* nam_u) u3_pend_req* req_u = _xmas_get_request(sam_u, nam_u); if ( NULL != req_u ) { + if( req_u->nex_w == nam_u->fra_w ) { + req_u->nex_w++; + } // TODO: optional assertions? req_u->wat = u3n_slam_on( u3k(sam_u->mop_u.put), u3nt(req_u->dat, u3i_word(nam_u->fra_w), pac) ); - } else { req_u = alloca(sizeof(u3_pend_req)); u3l_log("TODO: restart requests"); @@ -522,6 +535,7 @@ _xmas_req_pact_sent(u3_xmas* sam_u, u3_xmas_name* nam_u) req_u->nex_w = 1; req_u->tot_w = 0; req_u->dat = u3_nul; + uv_timer_init(u3L, &req_u->tim_u); req_u->wat = u3n_slam_on( u3k(sam_u->mop_u.put), u3nt(u3_nul, u3i_word(nam_u->fra_w), u3k(pac)) @@ -1852,6 +1866,7 @@ _xmas_hear_page(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w, u3_lane lan_u) return; }*/ + _log_pact(pac_u); u3_weak res = _xmas_req_pact_done(sam_u, &pac_u->pag_u.nam_u, pac_u->pag_u.mat_u.tot_w, fra); if ( u3_none != res ) { @@ -1859,8 +1874,37 @@ _xmas_hear_page(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w, u3_lane lan_u) u3l_log("now: %" PRIu64"ms", get_millis()); u3l_log("for %u bytes", u3r_met(3, res)); + u3z(res); return; } + + + c3_w nex_w = _xmas_req_get_nex(sam_u, &pac_u->pag_u.nam_u); + c3_w win_w = _xmas_req_get_cwnd(sam_u, &pac_u->pag_u.nam_u); + if ( win_w != 0 ) { +#ifdef XMAS_DEBUG + u3l_log("xmas: continuing flow, sending %u packets", win_w); +#endif + u3_xmas_pact* nex_u = c3_calloc(sizeof(u3_xmas_pact) * win_w); + for(int i = 0; i < win_w; i++) { + //u3l_log("sending req fra %u", nex_w + i); + nex_u[i].sam_u = sam_u; + nex_u[i].typ_y = PACT_PEEK; + memcpy(&nex_u[i].pek_u.nam_u, &pac_u->pag_u.nam_u, sizeof(u3_xmas_name)); + nex_u[i].pek_u.nam_u.fra_w = nex_w + i; + c3_y* buf_y = c3_calloc(PACT_SIZE); + c3_w siz_w =_xmas_etch_pact(buf_y, &nex_u[i], 0); + if( siz_w == 0 ) { + u3l_log("failed to etch"); + u3_assert( 0 ); + } + // TODO: better route management + _xmas_send_buf(sam_u, lan_u, buf_y, siz_w); + //_xmas_req_pact_sent(sam_u, &nex_u[i].pek_u.nam_u);; + } + u3l_log("xmas: sent packets"); + free(nex_u); + } } static void From 19068f48cc2f98f13eb2e388b7382a3218af5b4c Mon Sep 17 00:00:00 2001 From: parallels Date: Tue, 30 Jan 2024 16:34:58 -0500 Subject: [PATCH 023/430] wip --- ' | 1604 -------------------------------------------- pkg/vere/io/xmas.c | 63 +- 2 files changed, 37 insertions(+), 1630 deletions(-) delete mode 100644 ' diff --git a/' b/' deleted file mode 100644 index 9041d3e81c..0000000000 --- a/' +++ /dev/null @@ -1,1604 +0,0 @@ -/// @file - -#include "vere.h" - -#include "noun.h" -#include "ur.h" -#include -#include -#include - -#define XMAS_DEBUG c3y -#define XMAS_VER 1 -#define FINE_PAGE 4096 // packets per page -#define FINE_FRAG 1024 // bytes per fragment packet -#define FINE_PATH_MAX 384 // longest allowed scry path -#define HEAD_SIZE 4 // header size in bytes -#define PACT_SIZE 1472 - -// pending interest sentinels -#define XMAS_ITEM 1 // cached item -#define XMAS_SCRY 2 // waiting on scry -// routing table sentinels -#define XMAS_CZAR 1 // pending dns lookup -#define XMAS_ROUT 2 // have route -// -#define XMAS_COOKIE_LEN 4 -static c3_y XMAS_COOKIE[4] = { 0x5e, 0x1d, 0xad, 0x51 }; - - -#define SIFT_VAR(dest, src, len) dest = 0; for(int i = 0; i < len; i++ ) { dest |= ((src + i) >> (8*i)); } - -/* _u3_xmas: next generation networking - */ -typedef struct _u3_xmas { - u3_auto car_u; - u3_pier* pir_u; - union { - uv_udp_t wax_u; - uv_handle_t had_u; - }; - c3_l sev_l; - ur_cue_test_t* tes_u; // cue-test handle - u3_cue_xeno* sil_u; // cue handle - u3p(u3h_root) her_p; // - u3p(u3h_root) pen_p; - c3_w imp_w[256]; - time_t imp_t[256]; // imperial IP timestamps - c3_o imp_o[256]; // imperial print status - c3_c* dns_c; // - // jj - u3p(u3h_root) req_p; -} u3_xmas; - -typedef enum _u3_xmas_ptag { - PACT_RESV = 0, - PACT_PEEK = 1, - PACT_PAGE = 2, - PACT_POKE = 3, -} u3_xmas_ptag; - -typedef enum _u3_xmas_rout_tag { - ROUT_GALAXY = 0, - ROUT_OTHER = 1 -} u3_xmas_rout_tag; - -typedef enum _u3_xmas_nexh { - NEXH_NONE = 0, - NEXH_SBYT = 1, - NEXH_ONLY = 2, - NEXH_MANY = 3 -} u3_xmas_nexh; - -typedef struct _u3_xmas_name_meta { - // reserved (2 bits) - c3_y ran_y; // rank (2 bits) - c3_y rif_y; // rift-len (2 bits) - c3_o pat_o; // path length length (1bit) - c3_o boq_o; // 0b1 if custom, 0b0 implicitly 13 (1bit) - c3_o fra_y; // fragment number length (2bit) -} u3_xmas_name_meta; - -typedef struct _u3_xmas_name { - u3_xmas_name_meta met_u; - c3_d her_d[2]; - c3_w rif_w; - c3_s pat_s; - c3_c* pat_c; - c3_y boq_y; - c3_w fra_w; -} u3_xmas_name; - -typedef struct _u3_xmas_rout { - u3_xmas_rout_tag typ_y; // type tag - union { - c3_y imp_y; - u3_lane lan_u; - }; -} u3_xmas_rout; - -typedef struct _u3_xmas_head { - u3_xmas_nexh nex_y; // next-hop - c3_y pro_y; // protocol version - u3_xmas_ptag typ_y; // packet type - c3_y hop_y; // hopcount - c3_w mug_w; // mug checksum -} u3_xmas_head; - -// -// +$ cache -// [%rout lanes=(list lanes)] -// [%pending pacs=(list pact)] - - -typedef struct _u3_xmas_peek_pact { - u3_xmas_name nam_u; -} u3_xmas_peek_pact; - -typedef struct _u3_xmas_page_meat_meta { - c3_y tot_y; // total fragments length (2bit) - c3_o aut_o; // authentication len len (1bit) - c3_y len_y; // fragment length (5bit) -} u3_xmas_page_meat_meta; - -typedef enum _u3_xmas_auth_type { - AUTH_SIG = 1, - AUTH_DIG = 2, - AUTH_HMAC = 3, -} u3_xmas_auth_type; - -typedef struct _u3_xmas_page_meat { - u3_xmas_page_meat_meta met_u; // metadata - c3_w tot_w; // total fragments - c3_y aul_y; // authentication length - u3_xmas_auth_type ayp_y; // authentication type - c3_y aut_y[255]; // authentication - c3_d len_d; // fragment length - c3_y* fra_y; // fragment -} u3_xmas_page_meat; - -typedef struct _u3_xmas_page_pact { - u3_xmas_name nam_u; - u3_xmas_page_meat mat_u; -} u3_xmas_page_pact; - -typedef struct _u3_xmas_poke_pact { - u3_xmas_peek_pact* pek_u; - c3_w tot_w; - c3_y aut_y[96]; // heap allocate?? - c3_y* dat_y; -} u3_xmas_poke_pact; - -typedef struct _u3_xmas_pact { - u3_xmas_ptag typ_y; - uv_udp_send_t snd_u; - struct _u3_xmas* sam_u; - u3_xmas_rout rut_u; - union { - u3_xmas_poke_pact pok_u; - u3_xmas_page_pact pag_u; - u3_xmas_peek_pact pek_u; - }; -} u3_xmas_pact; - -typedef enum _u3_xmas_ctag { - CACE_WAIT = 1, - CACE_ITEM = 2, -} u3_xmas_ctag; - - - -typedef struct _u3_cace_enty { - u3_xmas_ctag typ_y; - union { - // u03_xmas_cace_wait wat_u; - // u3_xmas_cace_item res_u; - }; -} u3_cace_enty; - -typedef struct _u3_seal { - uv_udp_send_t snd_u; // udp send request - u3_xmas* sam_u; - c3_w len_w; - c3_y* buf_y; -} u3_seal; - - -static void -_log_head(u3_xmas_head* hed_u) -{ - u3l_log("-- HEADER --"); - u3l_log("next hop: %u", hed_u->nex_y); - u3l_log("protocol: %u", hed_u->pro_y); - u3l_log("packet type: %u", hed_u->typ_y); - u3l_log("mug: 0x%05x", (hed_u->mug_w & 0xfffff)); - u3l_log("hopcount: %u", hed_u->hop_y); - u3l_log(""); -} - -static void -_log_buf(c3_y* buf_y, c3_w len_w) -{ - c3_w siz_w = 2*len_w + 1; - c3_c* res_c = c3_calloc(siz_w); - c3_w cur_w = 0; - c3_c tmp_c[3]; - for(c3_w idx_w = 0; idx_w < len_w; idx_w++ ) { - snprintf(res_c + (2*idx_w), siz_w - (2*idx_w), "%02x", buf_y[idx_w]); - } - u3l_log("buffer: %s", res_c); - free(res_c); -} - -static void -_log_peek_pact(u3_xmas_peek_pact* pac_u) -{ - u3l_log("publisher: %s", u3r_string(u3dc("scot", c3__p, u3i_chubs(2, pac_u->nam_u.her_d)))); - u3l_log("path len: %u", pac_u->nam_u.pat_s); - u3l_log("path: %s", pac_u->nam_u.pat_c); -} - -static void -_log_pact(u3_xmas_peek_pact* pac_u) -{ - u3l_log("-- REQUEST PACKET --"); - /*if( ROUT_GALAXY == pac_u->rut_u.typ_y ) { - u3l_log("-- GALAXY ROUTE --"); - u3_noun her = u3dc("scot", c3__p, u3i_word(pac_u->rut_u.imp_u.her_y)); - c3_c* her_c = u3r_string(her); - u3l_log("ship lane: %s", her_c); - free(her_c); - u3z(her); - } - else { - u3l_log("-- OTHER ROUTE --"); - u3_noun pip = u3dc("scot", c3__if, u3i_word(pac_u->rut_u.lan_u.pip_w)); - c3_c* pip_c = u3r_string(pip); - u3l_log("IP: (%s) port (%u)", pip_c, pac_u->rut_u.lan_u.por_s); - free(pip_c); - u3z(pip); - }*/ - u3_noun pub = u3dc("scot", c3__p, u3i_chubs(2, pac_u->nam_u.her_d)); - c3_c* pub_c = u3r_string(pub); - u3l_log("ship: %s", pub_c); - free(pub_c); - //u3z(pub); - u3l_log("path: %s", pac_u->nam_u.pat_c); -} - - -// cut and pasted from ames.c -// -static void -_ames_etch_short(c3_y buf_y[2], c3_s sot_s) -{ - buf_y[0] = sot_s & 0xff; - buf_y[1] = (sot_s >> 8) & 0xff; -} - -static void -_ames_etch_word(c3_y buf_y[4], c3_w wod_w) -{ - buf_y[0] = wod_w & 0xff; - buf_y[1] = (wod_w >> 8) & 0xff; - buf_y[2] = (wod_w >> 16) & 0xff; - buf_y[3] = (wod_w >> 24) & 0xff; -} - -/* _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_alloc(): libuv buffer allocator. -*/ -static void -_ames_alloc(uv_handle_t* had_u, - size_t len_i, - uv_buf_t* buf - ) -{ - // we allocate 2K, which gives us plenty of space - // for a single ames packet (max size 1060 bytes) - // - void* ptr_v = c3_malloc(4096); - *buf = uv_buf_init(ptr_v, 4096); -} - -static inline c3_s -_ames_sift_short(c3_y buf_y[2]) -{ - return (buf_y[1] << 8 | buf_y[0]); -} - -static inline c3_w -_ames_sift_word(c3_y buf_y[4]) -{ - return (buf_y[3] << 24 | buf_y[2] << 16 | buf_y[1] << 8 | buf_y[0]); -} - -/* u3_xmas_decode_lane(): deserialize noun to lane; 0.0.0.0:0 if invalid -*/ -static u3_lane -u3_xmas_decode_lane(u3_atom lan) { - u3_lane lan_u; - c3_d lan_d; - - if ( c3n == u3r_safe_chub(lan, &lan_d) || (lan_d >> 48) != 0 ) { - return (u3_lane){0, 0}; - } - - u3z(lan); - - lan_u.pip_w = (c3_w)lan_d; - lan_u.por_s = (c3_s)(lan_d >> 32); - // convert incoming localhost to outgoing localhost - // - lan_u.pip_w = ( 0 == lan_u.pip_w ) ? 0x7f000001 : lan_u.pip_w; - - return lan_u; -} - -/* _ames_chub_bytes(): c3_y[8] to c3_d -** XX factor out, deduplicate with other conversions -*/ -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] -*/ -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_chub_bytes(): c3_d to c3_y[8] -** XX factor out, deduplicate with other conversions -*/ -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; -} - -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)); -} - - -/* u3_xmas_lane_to_chub(): serialize lane to double-word -*/ -static c3_d -u3_xmas_lane_to_chub(u3_lane lan) { - return ((c3_d)lan.por_s << 32) ^ (c3_d)lan.pip_w; -} - -/* u3_xmas_encode_lane(): serialize lane to noun -*/ -static u3_atom -u3_xmas_encode_lane(u3_lane lan) { - // [%| p=@] - // [%& p=@pC] - return u3i_chub(u3_xmas_lane_to_chub(lan)); -} - - - -// END plagariasm zone -// -// -// -// -// -// -static void _xmas_free_seal(u3_seal* sel_u) -{ - c3_free(sel_u->buf_y); - c3_free(sel_u); -} -// refcounted -// -static u3_noun _xmas_path_with_fra(c3_c* pat_c, c3_s* fra_s) -{ - u3_noun pax = u3do("stab", u3i_string(pat_c)); - u3_noun fra = u3dc("slav", c3__ud, u3do("rear", u3k(pax))); - u3_noun res = u3do("snip", pax); - *fra_s = u3r_short(0, fra); - - u3z(fra); // pax is unncessary bc moved in snip - return res; -} - -static u3_noun _xmas_get_now() { - struct timeval tim_u; - gettimeofday(&tim_u, 0); - u3_noun res = u3_time_in_tv(&tim_u); - return res; -} - -// refcounted -static c3_o -_xmas_rout_pact(u3_xmas_pact* pac_u, u3_noun lan) { - u3_noun tag, val; - u3x_cell(lan, &tag, &val); - - u3_assert( (c3y == tag) || (c3n == tag) ); - c3_o suc_o = c3y; - if ( c3y == tag ) { - u3_assert( c3y == u3a_is_cat(val) ); - u3_assert( val < 256 ); - pac_u->rut_u.typ_y = ROUT_GALAXY; - pac_u->rut_u.imp_y = val; - } else { - u3_lane lan_u = u3_xmas_decode_lane(u3k(val)); - // if in local-only mode, don't send remote packets - // - if ( (c3n == u3_Host.ops_u.net) && (0x7f000001 != lan_u.pip_w) ) { - suc_o = c3n; - } - // if the lane is uninterpretable, silently drop the packet - // - else if ( 0 == lan_u.por_s ) { - if ( u3C.wag_w & u3o_verbose ) { - u3l_log("ames: inscrutable lane"); - } - suc_o = c3n; - } else { - pac_u->rut_u.typ_y = ROUT_OTHER; - pac_u->rut_u.lan_u = lan_u; - } - } - u3z(lan); - return suc_o; -} - - -// refcounted -// RETAIN -static c3_y -_ship_meta(u3_noun her) -{ - c3_w met_w = u3r_met(3, her); - c3_y res_y; - switch ( met_w ) { - case 0: - case 1: - case 2: { - res_y = 0; - break; - } - case 3: - case 4: { - res_y = 1; - break; - } - case 5: - case 6: - case 7: - case 8: { - res_y = 2; - break; - } - default: { - res_y = 3; - break; - } - } - return res_y; -} - - -static void _xmas_pact_free(u3_xmas_pact* pac_u) { - // TODO: i'm lazy -} - -static u3_xmas_head -_xmas_head_from_pact(u3_xmas_pact* pac_u, u3_noun her) -{ - u3_xmas_head hed_u; - hed_u.pro_y = XMAS_VER; - hed_u.typ_y = pac_u->typ_y; - // ship meta is retain, no u3k necessary - //hed_u.ran_y = _ship_meta(her); - hed_u.hop_y = 0; - hed_u.mug_w = 0; - - u3z(her); - return hed_u; -} - -static c3_o -_xmas_sift_head(c3_y buf_y[8], u3_xmas_head* hed_u) -{ - if ( memcmp(buf_y + 4, &XMAS_COOKIE, XMAS_COOKIE_LEN) ) { - return c3n; - - } - c3_w hed_w = _ames_sift_word(buf_y); - - hed_u->nex_y = (hed_w >> 2) & 0x3; - hed_u->pro_y = (hed_w >> 4) & 0x7; - hed_u->typ_y = (hed_w >> 7) & 0x3; - hed_u->hop_y = (hed_w >> 9) & 0x7; - hed_u->mug_w = (hed_w >> 12) & 0xFFFFFF; - - return c3y; - - /*if(c3o((hed_u->typ_y == PACT_PEEK), (hed_u->typ_y == PACT_POKE))) { - hed_u->ran_y = (hed_w >> 30) & 0x3; - }*/ -} - -static void -_xmas_etch_head(u3_xmas_head* hed_u, c3_y buf_y[8]) -{ - if( c3y == XMAS_DEBUG ) { - if( hed_u->pro_y > 7 ) { - u3l_log("xmas: bad protocol version"); - return; - } - } - c3_o req_o = c3o((hed_u->typ_y == PACT_PEEK), (hed_u->typ_y == PACT_POKE)); - c3_y siz_y = req_o ? 5 : 7; - c3_w hed_w = (hed_u->nex_y & 0x3 ) << 2 - ^ (hed_u->pro_y & 0x7 ) << 4 - ^ ((hed_u->typ_y & 0x3 ) << 7) - ^ ((hed_u->hop_y & 0x7 ) << 9) - ^ ((hed_u->mug_w & 0xFFFFFF ) << 12); - // XX: we don't expand hopcount if no request. Correct? - // - /*if ( c3y == req_o ) { - hed_w = hed_w ^ ((hed_u->ran_y & 0x3) << 30); - }*/ - - _ames_etch_word(buf_y, hed_w); - memcpy(buf_y + 4, XMAS_COOKIE, XMAS_COOKIE_LEN); -} - -static c3_w -_xmas_etch_page_pact(c3_y* buf_y, u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u) -{ - /*c3_w cur_w = 0; - - // hops - memcpy(buf_y, pac_u->hop_y, 6); - cur_w += 6; - - // path length - _ames_etch_short(buf_y + cur_w, pac_u->pat_s); - cur_w += 2; - - // path - memcpy(buf_y + cur_w, pac_u->pat_c, pac_u->pat_s + 1); - cur_w += pac_u->pat_s + 1; - - // total - _ames_etch_word(buf_y + cur_w, pac_u->tot_w); - cur_w += 4; - - // auth - memcpy(buf_y + cur_w, &pac_u->aut_y, 96); - cur_w += 96; - - // dat - memcpy(buf_y + cur_w, pac_u->dat_y, 1024); - cur_w += 1024; - - return cur_w;*/ - return 0; -} - -static c3_w -_xmas_etch_peek_pact(c3_y* buf_y, u3_xmas_peek_pact* pac_u, u3_xmas_head* hed_u, c3_o tag_o) -{ - c3_w cur_w = 0; - u3_xmas_name nam_u = pac_u->nam_u; - u3_xmas_name_meta met_u = nam_u.met_u; - - // TODO: double check reserved bits - c3_y met_y = (met_u.ran_y & 0x3) >> 0 - ^ (met_u.rif_y & 0x3) >> 2 - ^ (met_u.pat_o & 0x1) >> 4 - ^ (met_u.boq_o & 0x1) >> 5 - ^ (met_u.fra_y & 0x3) >> 6; - - buf_y[cur_w] = met_y; - - - //shift - cur_w++; - c3_y her_y = 1 << (met_u.ran_y+1); - _ames_ship_of_chubs(nam_u.her_d, her_y, buf_y + cur_w); - cur_w += her_y; - - // rift - c3_y rif_y = met_u.rif_y + 1; - for ( int i = 0; i < rif_y; i++) { - buf_y[cur_w] = (nam_u.rif_w >> (8*i)) & 0xff; - cur_w++; - } - - // path length - c3_y pat_y = 1 >> met_u.pat_o; - for ( int i = 0; i < pat_y; i++ ) { - buf_y[cur_w] = (nam_u.pat_s >> (8*i)) & 0xff; - cur_w++; - } - - // path - memcpy(buf_y + cur_w, nam_u.pat_c, nam_u.pat_s); - cur_w += nam_u.pat_s; - - - // possible bloq size - if ( 1 == met_u.boq_o ) { - buf_y[cur_w] = nam_u.boq_y; - cur_w++; - } - - c3_y fra_y = met_u.fra_y + 1; - for( int i = 0; i < met_u.fra_y; i++ ) { - buf_y[cur_w] = (nam_u.fra_w >> (8*i)) & 0xff; - cur_w++; - } - - return cur_w; - //_ames_etch_short(buf_y + cur_w, &met_s); -} - -static c3_w -_xmas_etch_poke_pact(c3_y* buf_y, u3_xmas_poke_pact* pac_u, u3_xmas_head* hed_u) -{ - c3_w cur_w = 0; - cur_w += _xmas_etch_peek_pact(buf_y, pac_u->pek_u, hed_u, c3n); - - // total - _ames_etch_word(buf_y + cur_w, pac_u->tot_w); - cur_w += 4; - - // auth - memcpy(buf_y + cur_w, pac_u->aut_y, 96); - cur_w += 96; - - // day - memcpy(buf_y + cur_w, pac_u->dat_y, PACT_SIZE - cur_w); - - return PACT_SIZE; -} - -static c3_w -_xmas_etch_pact(c3_y* buf_y, u3_xmas_pact* pac_u, u3_noun her) -{ - c3_w cur_w = 0; - u3_xmas_head hed_u = _xmas_head_from_pact(pac_u, her); - _xmas_etch_head(&hed_u, buf_y + cur_w); - cur_w += 8; - - - switch ( pac_u->typ_y ) { - case PACT_POKE: { - cur_w += _xmas_etch_poke_pact(buf_y + cur_w, &pac_u->pok_u, &hed_u); - } break; - case PACT_PEEK: { - cur_w += _xmas_etch_peek_pact(buf_y + cur_w, &pac_u->pek_u, &hed_u, c3y); - } break; - case PACT_PAGE: { - cur_w += _xmas_etch_page_pact(buf_y + cur_w, &pac_u->pag_u, &hed_u); - } break; - - default: { - u3l_log("bad pact type");//u3m_bail(c3__bail); - } - } - return cur_w; -} - -static void -_xmas_send_cb(uv_udp_send_t* req_u, c3_i sas_i) -{ - u3_seal* sel_u = (u3_seal*)req_u; - u3_xmas* sam_u = sel_u->sam_u; - - if ( sas_i ) { - u3l_log("xmas: send fail_async: %s", uv_strerror(sas_i)); - //sam_u->fig_u.net_o = c3n; - } - else { - //sam_u->fig_u.net_o = c3y; - } - - _xmas_free_seal(sel_u); -} - -static void _xmas_send_buf(u3_xmas* sam_u, u3_lane lan_u, c3_y* buf_y, c3_w len_w) -{ - u3_seal* sel_u = c3_calloc(sizeof(*sel_u)); - sel_u->buf_y = buf_y; - sel_u->len_w = len_w; - sel_u->sam_u = sam_u; - struct sockaddr_in add_u; - - memset(&add_u, 0, sizeof(add_u)); - add_u.sin_family = AF_INET; - c3_w pip_w = c3n == u3_Host.ops_u.net ? lan_u.pip_w : 0x7f000001; - c3_s por_s = lan_u.por_s; - - add_u.sin_addr.s_addr = htonl(pip_w); - add_u.sin_port = htons(por_s); - -#ifdef XMAS_DEBUG - c3_c* sip_c = inet_ntoa(add_u.sin_addr); - u3l_log("xmas: sending packet (%s,%u)", sip_c, por_s); -#endif - - uv_buf_t buf_u = uv_buf_init((c3_c*)buf_y, len_w); - - c3_i sas_i = uv_udp_send(&sel_u->snd_u, - &sam_u->wax_u, - &buf_u, 1, - (const struct sockaddr*)&add_u, - _xmas_send_cb); - - if ( sas_i ) { - u3l_log("ames: send fail_sync: %s", uv_strerror(sas_i)); - /*if ( c3y == sam_u->fig_u.net_o ) { - //sam_u->fig_u.net_o = c3n; - }*/ - _xmas_free_seal(sel_u); - } -} - -static void _xmas_send(u3_xmas_pact* pac_u) -{ - u3_xmas* sam_u = pac_u->sam_u; - u3l_log("xmas sending"); - - - //u3l_log("_ames_send %s %u", _str_typ(pac_u->typ_y), - // pac_u->rut_u.lan_u.por_s); - c3_y* buf_y = c3_calloc(PACT_SIZE); - c3_w siz_w; - { - u3_noun who = 0; - - siz_w = _xmas_etch_pact(buf_y, pac_u, who); - } - - _xmas_send_buf(sam_u, pac_u->rut_u.lan_u, buf_y, siz_w); -} - -static c3_o -_xmas_rout_pacs(u3_xmas_pact* pac_u, u3_noun las) -{ - c3_o suc_o = c3n; - u3_noun lan, t = las; - while ( t != u3_nul ) { - u3x_cell(t, &lan, &t); - // if ( c3n == u3r_cell(t, &lan, &t) ) { - // break; - // } - if ( c3n == _xmas_rout_pact(pac_u, u3k(lan)) ) { - u3l_log("xmas: failed to set route"); - } else { - u3l_log("xmas_send"); - _xmas_send(pac_u); - suc_o = c3y; - } - } - u3z(las); - return suc_o; -} - -static void -_xmas_ef_pact(u3_xmas* sam_u, u3_noun las, u3_noun pac) -{ - u3_noun len, dat; - u3x_cell(pac, &len, &dat); - c3_w len_w = u3r_met(3, pac); - if ( len_w > len ) { -#ifdef XMAS_DEBUG - u3l_log("xmas: vane lying about length"); - return; -#endif - } - c3_y* buf_y = c3_calloc(len); - u3r_bytes(0, len_w, buf_y, pac); - - c3_o suc_o = c3n; - u3_noun lan, t = las; - while ( t != u3_nul ) { - u3x_cell(t, &lan, &t); - // if ( c3n == u3r_cell(t, &lan, &t) ) { - // break; - // } - u3l_log("xmas_send"); - u3_noun tag, val; - u3x_cell(lan, &tag, &val); - u3_assert( (c3y == tag) || (c3n == tag) ); - if ( c3y == tag ) { - u3_assert( c3y == u3a_is_cat(val) ); - u3_assert( val < 256 ); - if ( c3n == u3_Host.ops_u.net ) { - u3_lane lan_u; - lan_u.pip_w = 0x7f000001 ; - lan_u.por_s = _ames_czar_port(val); - _xmas_send_buf(sam_u, lan_u, buf_y, len_w); - } else { - // get galaxy lane or add pending - } - } else { - u3_lane lan_u = u3_xmas_decode_lane(u3k(val)); - // if in local-only mode, don't send remote packets - // - if ( (c3n == u3_Host.ops_u.net) && (0x7f000001 != lan_u.pip_w) ) { - suc_o = c3n; - } - // if the lane is uninterpretable, silently drop the packet - // - else if ( 0 == lan_u.por_s ) { - if ( u3C.wag_w & u3o_verbose ) { - u3l_log("ames: inscrutable lane"); - } - suc_o = c3n; - } else { - _xmas_send_buf(sam_u, lan_u, buf_y, len_w); - } - } - } - u3z(las); u3z(pac); -} - -static void -_xmas_ef_peek(u3_xmas* sam_u, u3_noun las, u3_noun who, u3_noun pat, u3_noun cop) -{ - u3_noun boq, fra, her, rif; - u3x_cell(who, &her, &rif); - u3x_cell(cop, &boq, &fra); - - if ( c3n == sam_u->car_u.liv_o ) { - u3l_log("xmas: not yet live, dropping outbound\r"); - u3z(pat); - } else { - u3_xmas_pact* pac_u = c3_calloc(sizeof(*pac_u)); - pac_u->typ_y = PACT_PEEK; - pac_u->sam_u = sam_u; - { - u3_noun pas = u3do("spat", pat); - pac_u->pek_u.nam_u.pat_c = u3r_string(pas); - pac_u->pek_u.nam_u.pat_s = strlen(pac_u->pek_u.nam_u.pat_c); - u3z(pas); - } - u3r_chubs(0, 2, pac_u->pek_u.nam_u.her_d, her); - pac_u->pek_u.nam_u.rif_w = u3r_word(0, rif); - pac_u->pek_u.nam_u.boq_y = u3r_byte(0, boq); - - if( 384 < pac_u->pek_u.nam_u.pat_s ) { - u3l_log("xmas: path in peek too long"); - _xmas_pact_free(pac_u); - } else { - c3_o rut_o = _xmas_rout_pacs(pac_u, u3k(las)); - - if ( c3n == rut_o ) { - _xmas_pact_free(pac_u); - } - } - } - // do not need to lose pat because moved in spat call - u3z(who); u3z(cop); -} - - -static c3_o _xmas_kick(u3_xmas* sam_u, u3_noun tag, u3_noun dat) -{ - c3_o ret_o; - - switch ( tag ) { - default: { - ret_o = c3n; - } break; - case c3__pact: { - u3_noun las, pac; - if ( c3n == u3r_cell(dat, &las, &pac) ) { - ret_o = c3n; - } else { - _xmas_ef_pact(sam_u, u3k(las), u3k(pac)); - ret_o = c3y; - } - } break; - // - case c3__peek: { - u3_noun lan, her, pat; - if ( c3n == u3r_trel(dat, &her, &lan, &pat) ) { - ret_o = c3n; - } else { - //_xmas_ef_peek(sam_u, u3k(her), u3k(lan), u3k(pat)); - ret_o = c3y; - } - } break; - } - - // technically losing tag is unncessary as it always should - // be a direct atom, but better to be strict - u3z(dat); u3z(tag); - return ret_o; -} - -static c3_o _xmas_io_kick(u3_auto* car_u, u3_noun wir, u3_noun cad) -{ - u3_xmas* sam_u = (u3_xmas*)car_u; - - u3_noun tag, dat; - c3_o ret_o; - - if( c3n == u3r_cell(cad, &tag, &dat) ) - { - ret_o = c3n; - } else { - ret_o = _xmas_kick(sam_u, u3k(tag), u3k(dat)); - } - - u3z(wir); u3z(cad); - return ret_o; -} - - - -static u3_noun -_xmas_io_info(u3_auto* car_u) -{ - - return u3_nul; -} - -static void -_xmas_io_slog(u3_auto* car_u) { - u3l_log("xmas is online"); -} - -static void -_xmas_exit_cb(uv_handle_t* had_u) -{ - u3_xmas* sam_u = had_u->data; - - u3s_cue_xeno_done(sam_u->sil_u); - ur_cue_test_done(sam_u->tes_u); - u3h_free(sam_u->her_p); - u3h_free(sam_u->req_p); - c3_free(sam_u); -} - -static void -_xmas_io_exit(u3_auto* car_u) -{ - u3_xmas* sam_u = (u3_xmas*)car_u; - uv_close(&sam_u->had_u, _xmas_exit_cb); -} - -static void -_xmas_sift_page_pact(u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, c3_w len_w) -{ - c3_w cur_w = 0; - - /*// next hop - memcpy(pac_u->hop_y, buf_y, 6); - cur_w += 6; - - // path length - pac_u->pat_s = _ames_sift_short(buf_y + cur_w); - cur_w += 2; - - // path contents - pac_u->pat_c = c3_calloc(pac_u->pat_s + 1); - memcpy(pac_u->pat_c, buf_y + cur_w, pac_u->pat_s); - pac_u->pat_c[pac_u->pat_s] = '\0'; - cur_w += pac_u->pat_s + 1; - - // total fragments - pac_u->tot_w = _ames_sift_word(buf_y + cur_w); - cur_w += 4; - - // Authenticator - memcpy(pac_u->aut_y, buf_y + cur_w, 96); - cur_w += 96; - - c3_w dat_w = 1024; // len_w - cur_w; - pac_u->dat_y = c3_calloc(dat_w); - memcpy(pac_u->dat_y, buf_y + cur_w, dat_w); - */ -} - -static c3_w -_xmas_sift_name(u3_xmas_name* nam_u, c3_y* buf_y) -{ - c3_w cur_w = 0; - c3_w met_y = buf_y[cur_w]; - nam_u->met_u.ran_y = (met_y >> 0) & 0x3; - nam_u->met_u.rif_y = (met_y >> 2) & 0x3; - nam_u->met_u.pat_o = (met_y >> 4) & 0x1; - nam_u->met_u.boq_o = (met_y >> 5) & 0x1; - nam_u->met_u.fra_y = (met_y >> 6) & 0x3; - cur_w += 1; - - c3_y her_y = 2 << nam_u->met_u.ran_y; - _ames_ship_to_chubs(nam_u->her_d, her_y, buf_y + cur_w); - cur_w += her_y; - - c3_y rif_y = 2 << nam_u->met_u.rif_y; - //SIFT_VAR(nam_u->rif_w, (buf_y + cur_w), rif_y); - for( int i = 0; i < rif_y; i++ ) { - nam_u->rif_w |= (buf_y[cur_w] << (8*i)); - cur_w++; - } - - c3_y pat_y = 2 << nam_u->met_u.pat_o; - nam_u->pat_s = 0; - for ( int i = 0; i < pat_y; i++ ) { - nam_u->pat_s |= (buf_y[cur_w] << (8*i)); - cur_w++; - } - - nam_u->pat_c = c3_calloc(nam_u->pat_s + 1); // unix string for ease of manipulation - memcpy(nam_u->pat_c, buf_y + cur_w, nam_u->pat_s); - nam_u->pat_c[nam_u->pat_s] = 0; - cur_w += nam_u->pat_s; - - if ( 1 == nam_u->met_u.boq_o ) { - nam_u->boq_y = buf_y[cur_w]; - cur_w++; - } else { - nam_u->boq_y = 13; - } - - c3_y fra_y = 2 << nam_u->met_u.fra_y; - nam_u->fra_w = 0; - for( int i = 0; i < fra_y; i++ ) { - nam_u->fra_w |= (buf_y[cur_w] << (8*i)); - - } - - return cur_w; -} - - -static c3_w -_xmas_sift_peek_pact(u3_xmas_peek_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, c3_w len_w, c3_o tag_o) -{ - c3_w siz_w = _xmas_sift_name(&pac_u->nam_u, buf_y); - /*if (siz_w > len_w ) { - u3l_log("xmas: buffer overrun (FIXME)"); - u3m_bail(c3__foul); - }*/ - return siz_w; -} - -static void -_xmas_sift_poke_pact(u3_xmas_poke_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, c3_w len_w) -{ - c3_w cur_w = 0; - // Peek portion - pac_u->pek_u = c3_calloc(sizeof(*(pac_u->pek_u))); - cur_w += _xmas_sift_peek_pact(pac_u->pek_u, hed_u, buf_y, len_w, c3n); - - // Total fragments - pac_u->tot_w = _ames_sift_word(buf_y + cur_w); - cur_w += 4; - - // Authenticator - memcpy(pac_u->aut_y, buf_y + cur_w, 96); - cur_w += 96; - - // Datum - memcpy(pac_u->dat_y, buf_y + cur_w, len_w - cur_w); -} - -static void -_xmas_sift_pact(u3_xmas_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, c3_w len_w) -{ - pac_u->typ_y = hed_u->typ_y; - switch ( hed_u->typ_y ) { - case PACT_PEEK: { - _xmas_sift_peek_pact(&pac_u->pek_u, hed_u, buf_y, len_w, c3y); - } break; - case PACT_PAGE: { - _xmas_sift_page_pact(&pac_u->pag_u, hed_u, buf_y, len_w); - } break; - case PACT_POKE: { - _xmas_sift_poke_pact(&pac_u->pok_u, hed_u, buf_y, len_w); - } break; - default: { - u3l_log("xmas: received unknown packet type"); - break; - } - } -} - -/* - * RETAIN - */ -static u3_weak -_xmas_get_peer(u3_xmas* sam_u, u3_noun her) -{ - return u3h_git(sam_u->her_p, her); -} - -/* - * RETAIN - */ -static void -_xmas_put_sponsee(u3_xmas* sam_u, u3_noun her, u3_noun las) -{ - u3_noun val = u3nc(u3_nul, u3k(las)); - u3h_put(sam_u->her_p, her, val); - u3z(val); -} - -/* - * RETAIN - */ -static c3_o -_xmas_add_galaxy_pend(u3_xmas* sam_u, u3_noun her, u3_noun pen) -{ - u3_weak old = u3h_git(sam_u->her_p, her); - u3_noun pes = u3_nul; - u3_noun wat; - if ( u3_none != old ) { - if ( u3h(old) == XMAS_CZAR ) { - u3x_cell(u3t(old), &pes, &wat); - u3z(old); - } else { - u3l_log("xmas: attempted to resolve resolved czar"); - u3z(old); - return c3n; - } - } - u3_noun val = u3nc(u3nc(u3k(pen), u3k(pes)), c3y); - u3h_put(sam_u->her_p, her, val); - u3z(val); - return _(wat); -} - -/* - * RETAIN - */ -static u3_weak -_xmas_get_cache(u3_xmas* sam_u, u3_noun pax) -{ - u3_weak res = u3h_git(sam_u->pen_p, pax); - if ( u3_none == res ) { - u3m_p("miss", u3k(pax)); - } else { - u3m_p("hit", u3nc(u3k(pax), u3k(res))); - } - return res; -} - -static void -_xmas_put_cache(u3_xmas* sam_u, u3_noun pax, u3_noun val) -{ - u3m_p("new key", u3k(pax)); - u3m_p("new value", u3k(val)); - u3h_put(sam_u->pen_p, pax, val); -} - -/* _xmas_czar(): add packet to queue, possibly begin DNS resolution - */ -static void -_xmas_czar(u3_xmas_pact* pac_u) -{ - u3_xmas* sam_u = pac_u->sam_u; -#ifdef XMAS_DEBUG - if ( pac_u->typ_y != PACT_PEEK ) { - u3l_log("xmas: attempted to resolve galaxy for packet that was not peek"); - u3m_bail(c3__oops); - return; - } -#endif - - u3_noun pat = u3i_string(pac_u->pek_u.nam_u.pat_c); - - c3_y her_y = pac_u->rut_u.imp_y; - - u3_weak her = _xmas_get_peer(sam_u, her_y); - - c3_o lok_o = _xmas_add_galaxy_pend(sam_u, her_y, pat); - - - // TODO: actually resolve DNS - u3z(pat); -} - - -static void -_xmas_try_forward(u3_xmas_pact* pac_u, u3_noun fra, u3_noun hit) -{ - u3l_log(""); - u3l_log("stubbed forwarding"); -} - -static void -_xmas_respond(u3_xmas_pact* req_u, u3_xmas_pact* res_u, u3_noun hit, c3_s fra_s) -{ - res_u->typ_y = PACT_PAGE; - - res_u->sam_u = req_u->sam_u; - - memcpy(&res_u->pag_u.nam_u, &req_u->pek_u.nam_u, sizeof(u3_xmas_name)); - - /*res_u->pag_u.pat_c = c3_calloc(req_u->pek_u.pat_s + 1); - res_u->pag_u.pat_c[req_u->pek_u.pat_s] = '\0'; - memcpy(res_u->pag_u.pat_c, req_u->pek_u.pat_c, req_u->pek_u.pat_s); - u3r_bytes(0, 1024, res_u->pag_u.dat_y, u3dc("snag", u3i_word(fra_s), u3k(u3t(hit))));*/ - u3z(hit); -} - -static void -_xmas_serve_cache_hit(u3_xmas_pact* req_u, u3_lane lan_u, c3_s fra_s, u3_noun hit) -{ - u3l_log("serving cache hit"); - c3_d her_d[2]; - memcpy(her_d, req_u->pek_u.nam_u.her_d, 2); - c3_d our_d[2]; - memcpy(our_d, req_u->sam_u->pir_u->who_d, 2); - if ( (her_d[0] != our_d[0]) - || (her_d[1] != our_d[1]) ) - { - u3l_log("publisher is not ours"); - if ( (256 > our_d[0]) - && (0 == our_d[1]) ) - { - // Forward only if requested datum is not ours, and we are a galaxy - _xmas_try_forward(req_u, fra_s, hit); - } else { - u3l_log("no forward, we are not a galaxy"); - } - } else { - //u3l_log("first: %x", u3r_string(u3dc("scot", c3__uv, u3k(u3h(u3t(hit)))))); - u3_xmas_pact* res_u = c3_calloc(sizeof(*res_u)); - res_u->rut_u.typ_y = ROUT_OTHER; - res_u->rut_u.lan_u = lan_u; - _xmas_respond(req_u, res_u, hit, fra_s); - _xmas_send(res_u); - } - // no lose needed because all nouns are moved in both - // branches of the conditional -} - -/* - */ -static void -_xmas_page_scry_cb(void* vod_p, u3_noun nun) -{ - u3_xmas_pact* pac_u = vod_p; - u3_xmas* sam_u = pac_u->sam_u; - c3_s fra_s; - u3_noun pax = _xmas_path_with_fra(pac_u->pek_u.nam_u.pat_c, &fra_s); - - u3_weak hit = u3r_at(7, nun); - if ( u3_none == hit ) { - // TODO: mark as dead - //u3z(nun); - u3l_log("unbound"); - - } else { - u3_weak old = _xmas_get_cache(sam_u, pax); - if ( old == u3_none ) { - u3l_log("bad"); - } else { - u3_noun tag; - u3_noun dat; - u3x_cell(u3k(old), &tag, &dat); - if ( XMAS_SCRY == tag ) { - u3_xmas_pact* res_u = c3_calloc(sizeof(*res_u)); - _xmas_respond(pac_u, res_u, u3k(hit), fra_s); - - _xmas_rout_pacs(res_u, u3k(u3t(dat))); - } - _xmas_put_cache(sam_u, u3k(pax), u3nc(XMAS_ITEM, u3k(hit))); - u3z(old); - } - u3z(hit); - } - u3z(pax); -} - -static void -_xmas_hear_bail(u3_ovum* egg_u, u3_noun lud) -{ - u3l_log("xmas: hear bail"); - u3_ovum_free(egg_u); -} - -static void -_xmas_hear_page(u3_xmas_pact* pac_u, u3_lane lan_u) -{ - u3_xmas* sam_u = pac_u->sam_u; - u3_noun wir = u3nc(c3__xmas, u3_nul); - c3_s fra_s; - u3_noun pat = _xmas_path_with_fra(pac_u->pag_u.nam_u.pat_c, &fra_s); - - u3_weak cac = _xmas_get_cache(sam_u, pat); - - if ( u3_none != cac ) { - u3_noun tag, dat; - - u3x_cell(cac, &tag, &dat); - if ( tag == XMAS_SCRY ) { - _xmas_rout_pacs(pac_u, u3k(u3t(dat))); - } - u3z(cac); - } else { - u3_noun mess = u3nq( - pat, - u3i_word(pac_u->pag_u.mat_u.tot_w), - u3i_bytes(256, pac_u->pag_u.mat_u.aut_y), - u3i_bytes(pac_u->pag_u.mat_u.len_d, pac_u->pag_u.mat_u.fra_y) - ); - u3_noun cad = u3nt(c3__mess, c3__page, mess); - - u3_auto_peer( - u3_auto_plan(&sam_u->car_u, - u3_ovum_init(0, c3__x, wir, cad)), - 0, 0, _xmas_hear_bail); - } - // no lose required because all nouns are moved -} - -static void -_xmas_add_lane_to_cache(u3_xmas* sam_u, u3_noun las, u3_noun pax, u3_lane lan_u) -{ - u3_noun hit = u3nq(XMAS_SCRY, - _xmas_get_now(), - u3nc(c3n, u3_xmas_encode_lane(lan_u)), - u3k(las)); - _xmas_put_cache(sam_u, pax, hit); - u3z(las); -} - -static void -_xmas_hear_peek(u3_xmas_pact* pac_u, u3_lane lan_u) -{ - u3l_log("xmas: hear peek"); - u3l_log("%hu", lan_u.por_s); - u3_xmas* sam_u = pac_u->sam_u; - c3_s fra_s; - u3_noun pax = _xmas_path_with_fra(pac_u->pek_u.nam_u.pat_c, &fra_s); - u3_weak hit = _xmas_get_cache(sam_u, u3k(pax)); - if ( u3_none != hit ) { - u3_noun tag, dat; - u3x_cell(u3k(hit), &tag, &dat); - u3m_p("dat", dat); - if ( tag == XMAS_SCRY ) { - _xmas_add_lane_to_cache(sam_u, u3k(u3t(dat)), u3k(pax), lan_u); - } else if ( tag == XMAS_ITEM ) { - u3l_log("item hit"); - u3m_p("dat", u3k(dat)); - u3l_log("after dat"); - _xmas_serve_cache_hit(pac_u, lan_u, fra_s, u3k(dat)); - } else { - u3l_log("xmas: weird case in cache, dropping"); - } - u3z(hit); - } else { - _xmas_add_lane_to_cache(sam_u, u3_nul, u3k(pax), lan_u); // TODO: retrieve from namespace - u3_noun sky = u3nc(c3__page, pax); - u3_pier_peek_last(sam_u->car_u.pir_u, u3_nul, c3__xx, u3_nul, sky, pac_u, _xmas_page_scry_cb); - } - u3l_log("xmas: finished hear peek"); - u3z(pax); -} - -static void -_xmas_hear(u3_xmas* sam_u, - u3_lane* lan_u, - c3_w len_w, - c3_y* hun_y) -{ - - u3l_log("xmas_hear"); - - u3_xmas_pact* pac_u; - c3_w pre_w; - c3_y* cur_y = hun_y; - if ( HEAD_SIZE > len_w ) { - c3_free(hun_y); - return; - } - - pac_u = c3_calloc(sizeof(*pac_u)); - pac_u->sam_u = sam_u; - u3_xmas_head hed_u; - _xmas_sift_head(cur_y, &hed_u); - _log_head(&hed_u); - _xmas_sift_pact(pac_u, &hed_u, hun_y + 8, (len_w - 8)); - - switch ( pac_u->typ_y ) { - case PACT_PEEK: { - _xmas_hear_peek(pac_u, *lan_u); - } break; - case PACT_PAGE: { - _xmas_hear_page(pac_u, *lan_u); - } break; - default: { - u3l_log("xmas: unimplemented packet type %u", pac_u->typ_y); - } break; - } -} - -static void -_xmas_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("xmas: recv: fail: %s", 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("xmas: recv: fail: message truncated"); - } - c3_free(buf_u->base); - } - else { - u3_xmas* sam_u = wax_u->data; - struct sockaddr_in* add_u = (struct sockaddr_in*)adr_u; - u3_lane lan_u; - - u3l_log("test"); - - lan_u.por_s = ntohs(add_u->sin_port); - // u3l_log("port: %s", lan_u.por_s); - lan_u.pip_w = ntohl(add_u->sin_addr.s_addr); - // u3l_log("IP: %x", lan_u.pip_w); - // NB: [nrd_i] will never exceed max length from _ames_alloc() - // - _xmas_hear(sam_u, &lan_u, (c3_w)nrd_i, (c3_y*)buf_u->base); - } -} - - - -static void -_xmas_io_talk(u3_auto* car_u) -{ - u3_xmas* sam_u = (u3_xmas*)car_u; - sam_u->dns_c = "urbit.org"; // TODO: receive turf - //_xmas_io_start(sam_u); - u3_noun who = u3i_chubs(2, sam_u->pir_u->who_d); - u3_noun rac = u3do("clan:title", u3k(who)); - c3_s por_s = sam_u->pir_u->por_s; - c3_i ret_i; - if ( c3__czar == rac ) { - c3_y num_y = (c3_y)sam_u->pir_u->who_d[0]; - c3_s zar_s = _ames_czar_port(num_y); - - if ( 0 == por_s ) { - por_s = zar_s; - } - else if ( por_s != zar_s ) { - u3l_log("ames: czar: overriding port %d with -p %d", zar_s, por_s); - u3l_log("ames: czar: WARNING: %d required for discoverability", zar_s); - } - } - - - // Bind and stuff. - { - struct sockaddr_in add_u; - c3_i add_i = sizeof(add_u); - - memset(&add_u, 0, sizeof(add_u)); - add_u.sin_family = AF_INET; - add_u.sin_addr.s_addr = _(u3_Host.ops_u.net) ? - htonl(INADDR_ANY) : - htonl(INADDR_LOOPBACK); - add_u.sin_port = htons(por_s); - - if ( (ret_i = uv_udp_bind(&sam_u->wax_u, - (const struct sockaddr*)&add_u, 0)) != 0 ) - { - u3l_log("xmas: bind: %s", uv_strerror(ret_i)); - - /*if ( (c3__czar == rac) && - (UV_EADDRINUSE == ret_i) ) - { - u3l_log(" ...perhaps you've got two copies of vere running?"); - }*/ - - // XX revise - // - u3_pier_bail(u3_king_stub()); - } - - uv_udp_getsockname(&sam_u->wax_u, (struct sockaddr *)&add_u, &add_i); - u3_assert(add_u.sin_port); - - sam_u->pir_u->por_s = ntohs(add_u.sin_port); - } - if ( c3y == u3_Host.ops_u.net ) { - u3l_log("xmas: live on %d", sam_u->pir_u->por_s); - } - else { - u3l_log("xmas: live on %d (localhost only)", sam_u->pir_u->por_s); - } - - uv_udp_recv_start(&sam_u->wax_u, _ames_alloc, _xmas_recv_cb); - - sam_u->car_u.liv_o = c3y; - //u3z(rac); u3z(who); -} - -/* _xmas_io_init(): initialize ames I/O. -*/ -u3_auto* -u3_xmas_io_init(u3_pier* pir_u) -{ - u3_xmas* sam_u = c3_calloc(sizeof(*sam_u)); - sam_u->pir_u = pir_u; - - sam_u->her_p = u3h_new_cache(100000); - sam_u->req_p = u3h_new_cache(100000); - - u3_assert( !uv_udp_init(u3L, &sam_u->wax_u) ); - sam_u->wax_u.data = sam_u; - - sam_u->sil_u = u3s_cue_xeno_init(); - sam_u->tes_u = ur_cue_test_init(); - - // Disable networking for fake ships - // - if ( c3y == sam_u->pir_u->fak_o ) { - u3_Host.ops_u.net = c3n; - } - - u3_auto* car_u = &sam_u->car_u; - car_u->nam_m = c3__xmas; - car_u->liv_o = c3y; - car_u->io.talk_f = _xmas_io_talk; - car_u->io.info_f = _xmas_io_info; - car_u->io.slog_f = _xmas_io_slog; - car_u->io.kick_f = _xmas_io_kick; - car_u->io.exit_f = _xmas_io_exit; - - - /*{ - u3_noun now; - struct timeval tim_u; - gettimeofday(&tim_u, 0); - - now = u3_time_in_tv(&tim_u); - //sam_u->sev_l = u3r_mug(now); - u3z(now); - }*/ - - return car_u; -} - - - - diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c index 11f6d9994e..fa8def83fe 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/xmas.c @@ -281,7 +281,7 @@ _log_buf(c3_y* buf_y, c3_w len_w) snprintf(res_c + (2*idx_w), siz_w - (2*idx_w), "%02x", buf_y[idx_w]); } u3l_log("buffer: %s", res_c); - free(res_c); + c3_free(res_c); } static void @@ -409,7 +409,7 @@ _xmas_get_request(u3_xmas* sam_u, u3_xmas_name* nam_u) { u3_pend_req* ret_u = NULL; u3_noun key = _xmas_request_key(nam_u); - u3_weak res = u3h_get(sam_u->req_p, key); + u3_weak res = u3h_git(sam_u->req_p, key); if ( res != u3_none ) { ret_u = u3to(u3_pend_req, res); } @@ -423,12 +423,16 @@ _xmas_put_request(u3_xmas* sam_u, u3_xmas_name* nam_u, u3_pend_req* req_u) { u3_pend_req* old_u = _xmas_get_request(sam_u, nam_u); u3_pend_req* new_u = req_u; if ( old_u == NULL ) { - new_u = c3_calloc(sizeof(u3_pend_req)); - memcpy(new_u, req_u, sizeof(*req_u)); + new_u = u3a_calloc(sizeof(u3_pend_req),1); + u3l_log("new request alloc %p", new_u); + memcpy(new_u, req_u, sizeof(u3_pend_req)); } else { - memcpy(old_u, new_u, sizeof(*old_u)); new_u = old_u; + u3l_log("overwriting %p", new_u); + memcpy(new_u, req_u, sizeof(u3_pend_req)); } + + u3_noun val = u3of(u3_pend_req, new_u); u3h_put(sam_u->req_p, key, val); u3z(key); @@ -447,15 +451,18 @@ _xmas_req_pact_done(u3_xmas* sam_u, u3_xmas_name *nam_u, c3_w tot_w, u3_noun fra u3k(sam_u->mop_u.put), u3nt(req_u->dat, u3i_word(nam_u->fra_w), fra) ); - req_u->wat = u3t(u3n_slam_on( + u3_noun wit = u3n_slam_on( u3k(sam_u->mop_u.del), u3nc(req_u->wat, u3i_word(nam_u->fra_w)) - )); + ); + + req_u->wat = u3k(u3t(wit)); + u3z(wit); req_u->tot_w = tot_w; u3_noun tot = u3i_word(req_u->tot_w); if ( c3y == u3r_sing(u3qdb_wyt(req_u->dat), tot) ) { - ret = u3dc("rip", 13, u3n_slam_on(u3k(sam_u->mop_u.tap), req_u->dat)); + ret = u3dc("rap", 13, u3dc("turn", u3n_slam_on(u3k(sam_u->mop_u.tap), req_u->dat), u3v_wish("tail"))); //_xmas_put_request_raw(sam_u, nam_u, u3_none); } else { _xmas_put_request(sam_u, nam_u, req_u); @@ -465,7 +472,6 @@ _xmas_req_pact_done(u3_xmas* sam_u, u3_xmas_name *nam_u, c3_w tot_w, u3_noun fra u3l_log("xmas: attempting to mark packet done, no request"); u3m_bail(c3__foul); } - u3z(fra); return ret; } @@ -477,6 +483,7 @@ _xmas_req_get_nex(u3_xmas* sam_u, u3_xmas_name* nam_u) if ( NULL != req_u ) { res_w = req_u->nex_w; } + u3l_log("nex: %u", res_w); return res_w; } @@ -485,8 +492,8 @@ _xmas_req_get_cwnd(u3_xmas* sam_u, u3_xmas_name* nam_u) { c3_w res_w = 0; u3_pend_req* req_u = _xmas_get_request(sam_u, nam_u); - if ( NULL != req_u ) { - res_w = req_u->nex_w; + if ( NULL == req_u ) { + return 0; } c3_w liv_w = u3r_word(0, u3qdb_wyt(req_u->wat)); @@ -525,7 +532,7 @@ _xmas_req_pact_sent(u3_xmas* sam_u, u3_xmas_name* nam_u) // TODO: optional assertions? req_u->wat = u3n_slam_on( u3k(sam_u->mop_u.put), - u3nt(req_u->dat, u3i_word(nam_u->fra_w), pac) + u3nt(req_u->wat, u3i_word(nam_u->fra_w), pac) ); } else { req_u = alloca(sizeof(u3_pend_req)); @@ -537,9 +544,11 @@ _xmas_req_pact_sent(u3_xmas* sam_u, u3_xmas_name* nam_u) req_u->dat = u3_nul; uv_timer_init(u3L, &req_u->tim_u); req_u->wat = u3n_slam_on( - u3k(sam_u->mop_u.put), + u3k(sam_u->mop_u.put), u3nt(u3_nul, u3i_word(nam_u->fra_w), u3k(pac)) ); + u3m_p("wat", req_u->wat); + u3m_p("dat", req_u->dat); } _xmas_put_request(sam_u, nam_u, req_u); } @@ -1413,7 +1422,7 @@ _xmas_rout_bufs(u3_xmas* sam_u, c3_y* buf_y, c3_w len_w, u3_noun las) u3x_cell(t, &lan, &t); u3_lane lan_u = _realise_lane(u3k(lan)); #ifdef XMAS_DEBUG - //u3l_log("sending to ip: %x, port: %u", lan_u.pip_w, lan_u.por_s); + u3l_log("sending to ip: %x, port: %u", lan_u.pip_w, lan_u.por_s); #endif /* ifdef XMAS_DEBUG u3l_log("sending to ip: %x, port: %u", lan_u.pip_w, lan_u.por_s); */ @@ -1423,7 +1432,7 @@ _xmas_rout_bufs(u3_xmas* sam_u, c3_y* buf_y, c3_w len_w, u3_noun las) _xmas_send_buf(sam_u, lan_u, buf_y, len_w); } } - u3z(las); + // u3z(las); return suc_o; } @@ -1694,7 +1703,7 @@ _xmas_put_cache(u3_xmas* sam_u, u3_xmas_name* nam_u, u3_noun val) { u3_noun pax = _name_to_scry(nam_u); - u3h_put(sam_u->pac_p, pax, val); + u3h_put(sam_u->pac_p, pax, u3k(val)); u3z(pax); // TODO: fix refcount } @@ -1807,9 +1816,9 @@ _xmas_page_scry_cb(void* vod_p, u3_noun nun) _xmas_rout_bufs(sam_u, buf_y, len_w, u3k(u3t(dat))); } _xmas_put_cache(sam_u, &pac_u->pek_u.nam_u, u3nc(XMAS_ITEM, u3k(hit))); - u3z(old); + // u3z(old); } - u3z(hit); + // u3z(hit); } // u3z(pax); } @@ -1860,11 +1869,11 @@ _xmas_hear_page(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w, u3_lane lan_u) c3_s fra_s; u3_noun fra = u3i_bytes(pac_u->pag_u.mat_u.len_d, pac_u->pag_u.mat_u.fra_y) ; -/* if ( dop_o == c3n && pac_u->pag_u.nam_u.fra_w == 150) { + if ( dop_o == c3n && pac_u->pag_u.nam_u.fra_w == 150) { dop_o = c3y; u3l_log("simulating dropped packet"); return; - }*/ + } _log_pact(pac_u); u3_weak res = _xmas_req_pact_done(sam_u, &pac_u->pag_u.nam_u, pac_u->pag_u.mat_u.tot_w, fra); @@ -1873,14 +1882,15 @@ _xmas_hear_page(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w, u3_lane lan_u) u3l_log("finished"); u3l_log("now: %" PRIu64"ms", get_millis()); - u3l_log("for %u bytes", u3r_met(3, res)); - u3z(res); + // u3l_log("for %u bytes", u3r_met(3, res)); + // u3z(res); return; } c3_w nex_w = _xmas_req_get_nex(sam_u, &pac_u->pag_u.nam_u); c3_w win_w = _xmas_req_get_cwnd(sam_u, &pac_u->pag_u.nam_u); + u3l_log("continuing flow nex: %u, win: %u", nex_w, win_w); if ( win_w != 0 ) { #ifdef XMAS_DEBUG u3l_log("xmas: continuing flow, sending %u packets", win_w); @@ -1900,10 +1910,10 @@ _xmas_hear_page(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w, u3_lane lan_u) } // TODO: better route management _xmas_send_buf(sam_u, lan_u, buf_y, siz_w); - //_xmas_req_pact_sent(sam_u, &nex_u[i].pek_u.nam_u);; + _xmas_req_pact_sent(sam_u, &nex_u[i].pek_u.nam_u);; } u3l_log("xmas: sent packets"); - free(nex_u); + c3_free(nex_u); } } @@ -1925,6 +1935,7 @@ _xmas_hear_peek(u3_xmas_pact* pac_u, u3_lane lan_u) { #ifdef XMAS_DEBUG u3l_log("xmas: hear peek"); + _log_name(&pac_u->pek_u.nam_u); u3l_log("%hu", lan_u.por_s); // u3_assert(pac_u->typ_y == PACT_PEEK); #endif @@ -1935,7 +1946,7 @@ _xmas_hear_peek(u3_xmas_pact* pac_u, u3_lane lan_u) u3_noun tag, dat; u3x_cell(u3k(hit), &tag, &dat); if ( tag == XMAS_SCRY ) { - _xmas_add_lane_to_cache(sam_u, &pac_u->pek_u.nam_u, u3t(dat), lan_u); + _xmas_add_lane_to_cache(sam_u, &pac_u->pek_u.nam_u, u3k(u3t(dat)), lan_u); } else if ( tag == XMAS_ITEM ) { c3_y* buf_y; @@ -1944,7 +1955,7 @@ _xmas_hear_peek(u3_xmas_pact* pac_u, u3_lane lan_u) } else { u3l_log("xmas: weird case in cache, dropping"); } - u3z(hit); + // u3z(hit); } else { _xmas_add_lane_to_cache(sam_u, &pac_u->pek_u.nam_u, u3_nul, lan_u); // TODO: retrieve from namespace u3_noun sky = _name_to_scry(&pac_u->pek_u.nam_u); From 952064150d74aea27424626b188663b72fbacfc0 Mon Sep 17 00:00:00 2001 From: Liam Fitzgerald Date: Wed, 31 Jan 2024 10:32:25 -0500 Subject: [PATCH 024/430] xmas: improve noun handling speed --- pkg/vere/io/xmas.c | 130 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 98 insertions(+), 32 deletions(-) diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c index fa8def83fe..e1322cc3cf 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/xmas.c @@ -18,7 +18,7 @@ c3_o dop_o = c3n; -#define XMAS_DEBUG c3y +//#define XMAS_DEBUG c3y #define XMAS_VER 1 #define FINE_PAGE 4096 // packets per page #define FINE_FRAG 1024 // bytes per fragment packet @@ -91,6 +91,7 @@ typedef struct _u3_xmas { u3_noun tap; u3_noun pry; } mop_u; // pointer to ordered map functions + c3_d tim_d; } u3_xmas; typedef enum _u3_xmas_ptag { @@ -246,7 +247,6 @@ typedef struct _u3_seal { -c3_d _tim_d = 0; static c3_d get_millis() { @@ -396,10 +396,52 @@ _clamp_rto(c3_d rto_d) { return c3_min(c3_max(rto_d, 200000), 25000000); } +// presumes already valid path with no leading slash +// +static u3_noun +_xmas_encode_path(c3_w len_w, c3_y* buf_y) +{ + u3_noun pro; + u3_noun* lit = &pro; + + { + u3_noun* hed; + u3_noun* tel; + c3_y* fub_y = buf_y; + c3_y car_y; + c3_w tem_w; + u3i_slab sab_u; + + while ( len_w-- ) { + car_y = *buf_y++; + if ( len_w == 0 ) { + buf_y++; + car_y = 47; + } + + if ( 47 == car_y ) { + tem_w = buf_y - fub_y - 1; + u3i_slab_bare(&sab_u, 3, tem_w); + sab_u.buf_w[sab_u.len_w - 1] = 0; + memcpy(sab_u.buf_y, fub_y, tem_w); + + *lit = u3i_defcons(&hed, &tel); + *hed = u3i_slab_moot(&sab_u); + lit = tel; + fub_y = buf_y; + } + } + } + + *lit = u3_nul; + + return pro; +} + u3_noun _xmas_request_key(u3_xmas_name* nam_u) { - u3_noun pax = u3do("stab", u3dt("cat", 3, '/', u3i_string(nam_u->pat_c))); + u3_noun pax = _xmas_encode_path(nam_u->pat_s, (c3_y*)nam_u->pat_c); u3_noun res = u3nt(u3i_chubs(2, nam_u->her_d), u3i_word(nam_u->rif_w), pax); return res; } @@ -424,11 +466,9 @@ _xmas_put_request(u3_xmas* sam_u, u3_xmas_name* nam_u, u3_pend_req* req_u) { u3_pend_req* new_u = req_u; if ( old_u == NULL ) { new_u = u3a_calloc(sizeof(u3_pend_req),1); - u3l_log("new request alloc %p", new_u); memcpy(new_u, req_u, sizeof(u3_pend_req)); } else { new_u = old_u; - u3l_log("overwriting %p", new_u); memcpy(new_u, req_u, sizeof(u3_pend_req)); } @@ -461,6 +501,7 @@ _xmas_req_pact_done(u3_xmas* sam_u, u3_xmas_name *nam_u, c3_w tot_w, u3_noun fra req_u->tot_w = tot_w; u3_noun tot = u3i_word(req_u->tot_w); + //u3l_log("tot: %u, wyt: %u", req_u->tot_w, u3r_word(0, u3qdb_wyt(req_u->dat))); if ( c3y == u3r_sing(u3qdb_wyt(req_u->dat), tot) ) { ret = u3dc("rap", 13, u3dc("turn", u3n_slam_on(u3k(sam_u->mop_u.tap), req_u->dat), u3v_wish("tail"))); //_xmas_put_request_raw(sam_u, nam_u, u3_none); @@ -470,7 +511,8 @@ _xmas_req_pact_done(u3_xmas* sam_u, u3_xmas_name *nam_u, c3_w tot_w, u3_noun fra u3z(tot); } else { u3l_log("xmas: attempting to mark packet done, no request"); - u3m_bail(c3__foul); + return u3_none; + //u3m_bail(c3__foul); } return ret; } @@ -483,7 +525,6 @@ _xmas_req_get_nex(u3_xmas* sam_u, u3_xmas_name* nam_u) if ( NULL != req_u ) { res_w = req_u->nex_w; } - u3l_log("nex: %u", res_w); return res_w; } @@ -538,7 +579,7 @@ _xmas_req_pact_sent(u3_xmas* sam_u, u3_xmas_name* nam_u) req_u = alloca(sizeof(u3_pend_req)); u3l_log("TODO: restart requests"); _log_name(nam_u); - u3_assert( nam_u->fra_w == 0 ); + //u3_assert( nam_u->fra_w == 0 ); req_u->nex_w = 1; req_u->tot_w = 0; req_u->dat = u3_nul; @@ -1127,6 +1168,14 @@ _xmas_sift_pact(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w) } +static u3_atom +_dire_etch_ud(c3_d num_d) +{ + c3_y hun_y[26]; + c3_y* buf_y = u3s_etch_ud_smol(num_d, hun_y); + c3_w dif_w = (c3_p)buf_y - (c3_p)hun_y; + return u3i_bytes(26 - dif_w, buf_y); // XX known-non-null +} static void _xmas_etch_head(u3_xmas_head* hed_u, c3_y buf_y[8]) @@ -1495,7 +1544,7 @@ _xmas_ef_send(u3_xmas* sam_u, u3_noun las, u3_noun pac) _xmas_sift_pact(&pac_u, buf_y, len); if ( pac_u.typ_y == PACT_PEEK ) { - u3l_log("start: %" PRIu64 "ms", get_millis()); + sam_u->tim_d = get_millis(); _xmas_update_req_peek(&pac_u); } @@ -1674,12 +1723,14 @@ _xmas_add_galaxy_pend(u3_xmas* sam_u, u3_noun her, u3_noun pen) static u3_noun _name_to_scry(u3_xmas_name* nam_u) { - u3_noun rif = u3dc("scot", c3__ud, nam_u->rif_w); - u3_noun boq = u3dc("scot", c3__ud, nam_u->boq_y); - u3_noun fag = u3dc("scot", c3__ud, nam_u->fra_w); - u3_noun pax = u3do("stab", u3dt("cat", 3, '/', u3i_string(nam_u->pat_c))); + u3_noun rif = _dire_etch_ud(nam_u->rif_w); + u3_noun boq = _dire_etch_ud(nam_u->boq_y); + u3_noun fag = _dire_etch_ud(nam_u->fra_w); + u3_noun pax = _xmas_encode_path(nam_u->pat_s, (c3_y*)nam_u->pat_c); + + u3_noun res = u3kb_weld(u3nc(c3__pact, u3nq(rif, boq, fag, u3nc(c3__data, u3_nul))), pax); - return u3dc("weld", u3nc(c3__pact, u3nq(rif, boq, fag, u3nc(c3__data, u3_nul))), pax); + return res; } /* @@ -1869,50 +1920,47 @@ _xmas_hear_page(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w, u3_lane lan_u) c3_s fra_s; u3_noun fra = u3i_bytes(pac_u->pag_u.mat_u.len_d, pac_u->pag_u.mat_u.fra_y) ; - if ( dop_o == c3n && pac_u->pag_u.nam_u.fra_w == 150) { + /*if ( dop_o == c3n && pac_u->pag_u.nam_u.fra_w == 150) { dop_o = c3y; u3l_log("simulating dropped packet"); return; - } + }*/ - _log_pact(pac_u); u3_weak res = _xmas_req_pact_done(sam_u, &pac_u->pag_u.nam_u, pac_u->pag_u.mat_u.tot_w, fra); if ( u3_none != res ) { u3l_log("finished"); - u3l_log("now: %" PRIu64"ms", get_millis()); - // u3l_log("for %u bytes", u3r_met(3, res)); - // u3z(res); + c3_d now_d = get_millis(); + u3l_log("took: %" PRIu64 " ms", now_d - sam_u->tim_d); + u3l_log("for %u bytes", u3r_met(3, res)); + u3z(res); return; } c3_w nex_w = _xmas_req_get_nex(sam_u, &pac_u->pag_u.nam_u); c3_w win_w = _xmas_req_get_cwnd(sam_u, &pac_u->pag_u.nam_u); - u3l_log("continuing flow nex: %u, win: %u", nex_w, win_w); if ( win_w != 0 ) { #ifdef XMAS_DEBUG - u3l_log("xmas: continuing flow, sending %u packets", win_w); + u3l_log("continuing flow nex: %u, win: %u", nex_w, win_w); #endif - u3_xmas_pact* nex_u = c3_calloc(sizeof(u3_xmas_pact) * win_w); + u3_xmas_pact* nex_u = c3_calloc(sizeof(u3_xmas_pact)); + nex_u->sam_u = sam_u; + nex_u->typ_y = PACT_PEEK; + memcpy(&nex_u->pek_u.nam_u, &pac_u->pag_u.nam_u, sizeof(u3_xmas_name)); for(int i = 0; i < win_w; i++) { - //u3l_log("sending req fra %u", nex_w + i); - nex_u[i].sam_u = sam_u; - nex_u[i].typ_y = PACT_PEEK; - memcpy(&nex_u[i].pek_u.nam_u, &pac_u->pag_u.nam_u, sizeof(u3_xmas_name)); - nex_u[i].pek_u.nam_u.fra_w = nex_w + i; + nex_u->pek_u.nam_u.fra_w = nex_w + i; c3_y* buf_y = c3_calloc(PACT_SIZE); - c3_w siz_w =_xmas_etch_pact(buf_y, &nex_u[i], 0); + c3_w siz_w =_xmas_etch_pact(buf_y, nex_u, 0); if( siz_w == 0 ) { u3l_log("failed to etch"); u3_assert( 0 ); } // TODO: better route management _xmas_send_buf(sam_u, lan_u, buf_y, siz_w); - _xmas_req_pact_sent(sam_u, &nex_u[i].pek_u.nam_u);; + _xmas_req_pact_sent(sam_u, &nex_u->pek_u.nam_u); } - u3l_log("xmas: sent packets"); c3_free(nex_u); } } @@ -2241,6 +2289,21 @@ _test_sift_peek() } } +static void +_test_encode_path(c3_c* pat_c) +{ + u3_noun wan = u3do("stab", u3dt("cat", 3, '/', u3i_string(pat_c))); + u3_noun hav = _xmas_encode_path(strlen(pat_c), (c3_y*)pat_c); + + if ( c3n == u3r_sing(wan, hav) ) { + u3l_log(RED_TEXT); + u3l_log("path encoding mismatch"); + u3m_p("want", wan); + u3m_p("have", hav); + exit(1); + } +} + static void _setup() @@ -2269,7 +2332,10 @@ int main() _setup(); - _test_sift_peek(); + //_test_sift_peek(); + + _test_encode_path("foo/bar/baz"); + _test_encode_path("publ/0/xx//1/foo/g"); return 0; } From 66664fe18541ca9bbfc7e8da93219bd701376d8c Mon Sep 17 00:00:00 2001 From: Liam Fitzgerald Date: Fri, 2 Feb 2024 10:05:31 -0500 Subject: [PATCH 025/430] xmas: add more congestion control functionality --- pkg/vere/io/xmas.c | 686 +++++++++++++++++++++++++++++---------------- 1 file changed, 441 insertions(+), 245 deletions(-) diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c index e1322cc3cf..3d685de4fa 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/xmas.c @@ -28,7 +28,7 @@ c3_o dop_o = c3n; #define RED_TEXT "\033[0;31m" #define DEF_TEXT "\033[0m" -#define IN_FLIGHT 5 +#define IN_FLIGHT 10 // pending interest sentinels #define XMAS_ITEM 1 // cached item @@ -52,15 +52,35 @@ static c3_y XMAS_COOKIE[4] = { 0x5e, 0x1d, 0xad, 0x51 }; #define CHECK_BOUNDS(cur) if ( len_w < cur ) { u3l_log("xmas: failed parse (%i,%i) at line %i", len_w, cur, __LINE__); return 0; } #define safe_dec(num) num == 0 ? num : num - 1; +typedef struct _u3_bitset { + c3_w len_w; + c3_y* buf_y; +} u3_bitset; + + + /** typedef u3_xmas_pack_stat u3_noun * [last=@da tries=@ud] */ +struct _u3_xmas_pact; + +typedef struct _u3_pact_stat { + c3_d sen_d; // last sent + c3_y sip_y; // skips + c3_y tie_y; // tries +} u3_pact_stat; + + typedef struct _u3_pend_req { - c3_w nex_w; - c3_w tot_w; - uv_timer_t tim_u; - u3_noun wat; // ((mop @ud packet-state) lte) - u3_noun dat; // ((mop @ud *) lte) + c3_w nex_w; + c3_w tot_w; + uv_timer_t tim_u; + struct _u3_xmas_pact* pac_u; + u3_pact_stat* wat_u; // ((mop @ud packet-state) lte) + u3_bitset was_u; // ((mop @ud packet-state) lte) + c3_y* dat_y; // ((mop @ud *) lte) + c3_w len_w; + c3_w las_w; // frag num of last packet sent } u3_pend_req; @@ -176,7 +196,7 @@ typedef struct _u3_xmas_page_meat { c3_y aul_y; // authentication length u3_xmas_auth_type ayp_y; // authentication type c3_y* aut_y; // authentication - c3_d len_d; // fragment length + c3_d len_w; // fragment length c3_y* fra_y; // fragment } u3_xmas_page_meat; @@ -208,7 +228,7 @@ typedef struct _u3_xmas_poke_pact { } u3_xmas_poke_pact; typedef struct _u3_xmas_pact { - u3_xmas_ptag typ_y; + u3_xmas_head hed_u; uv_udp_send_t snd_u; struct _u3_xmas* sam_u; u3_xmas_rout rut_u; @@ -257,7 +277,6 @@ get_millis() { // Convert the seconds to milliseconds by multiplying by 1000 // Convert the microseconds to milliseconds by dividing by 1000 } - static void _log_head(u3_xmas_head* hed_u) { @@ -321,7 +340,7 @@ _log_page_pact(u3_xmas_page_pact *pac_u) u3l_log("actual:"); u3l_log("total: %u", mat_u->tot_w); u3l_log("auth len: %u", mat_u->aul_y); - u3l_log("frag len: %u", mat_u->len_d); + u3l_log("frag len: %u", mat_u->len_w); } static void @@ -348,19 +367,12 @@ _log_pend_req(u3_pend_req* req_u) u3l_log("next: %u", req_u->nex_w); u3l_log("total: %u", req_u->tot_w); //u3l_log("timer in: %" PRIu64 " ms", uv_timer_get_due_in(&req_u->tim_u)); - if ( 0 ) { - u3m_p("wat", req_u->wat); - u3m_p("dat", req_u->dat); - } else { - u3l_log("waiting for %u", u3r_word(0, u3qdb_wyt(req_u->wat))); - u3l_log("have %u", u3r_word(0, u3qdb_wyt(req_u->dat))); - } } static void _log_pact(u3_xmas_pact* pac_u) { - switch ( pac_u->typ_y ) { + switch ( pac_u->hed_u.typ_y ) { case PACT_PEEK: { _log_peek_pact(&pac_u->pek_u); break; @@ -371,7 +383,7 @@ _log_pact(u3_xmas_pact* pac_u) } default: { - u3l_log("logging not implemented for %i", pac_u->typ_y); + u3l_log("logging not implemented for %i", pac_u->hed_u.typ_y); break; } } @@ -396,8 +408,80 @@ _clamp_rto(c3_d rto_d) { return c3_min(c3_max(rto_d, 200000), 25000000); } -// presumes already valid path with no leading slash -// +static void +_init_bitset(u3_bitset* bit_u, c3_w len_w) +{ + bit_u->buf_y = c3_calloc(len_w >> 3); + bit_u->len_w = len_w; +} + +static c3_w +_wyt_bitset(u3_bitset* bit_u) +{ + c3_w ret_w = 0; + for(int i = 0; i < (bit_u->len_w >> 3); i++ ) { + ret_w += __builtin_popcount(bit_u->buf_y[i]); + } + return ret_w; +} + +static void +_put_bitset(u3_bitset* bit_u, c3_w mem_w) +{ + if (( mem_w > bit_u->len_w )) { + u3l_log("overrun %u, %u", mem_w, bit_u->len_w); + return; + } + c3_w idx_w = mem_w >> 3; + c3_w byt_y = bit_u->buf_y[idx_w]; + c3_y rem_y = mem_w & 0x7; + c3_y mas_y = (1 << rem_y); + bit_u->buf_y[idx_w] = byt_y | mas_y; +} + +static c3_o +_has_bitset(u3_bitset* bit_u, c3_w mem_w) { + if (( mem_w > bit_u->len_w )) { + u3l_log("overrun %u, %u", mem_w, bit_u->len_w); + return c3n; + } + + u3_assert( mem_w < bit_u->len_w ); + c3_w idx_w = mem_w >> 3; + c3_y rem_y = mem_w & 0x7; + return __( (bit_u->buf_y[idx_w] >> rem_y) & 0x1); +} + +static void +_del_bitset(u3_bitset* bit_u, c3_w mem_w) +{ + u3_assert( mem_w < bit_u->len_w ); + c3_w idx_w = mem_w >> 3; + c3_w byt_y = bit_u->buf_y[idx_w]; + c3_y rem_y = mem_w & 0x7; + c3_y mas_y = ~(1 << rem_y); + bit_u->buf_y[idx_w] &= mas_y; +} + + +static void +_log_bitset(u3_bitset* bit_u) +{ + c3_w cur_w = 0; + u3l_log("logging bitset"); + while( cur_w < bit_u->len_w ) { + if ( c3y == _has_bitset(bit_u, cur_w) ) { + u3l_log("%u", cur_w); + } + cur_w++; + } + u3l_log("finished"); +} + + + +/* _xmas_encode_path(): produce buf_y as a parsed path +*/ static u3_noun _xmas_encode_path(c3_w len_w, c3_y* buf_y) { @@ -439,6 +523,8 @@ _xmas_encode_path(c3_w len_w, c3_y* buf_y) } +/* _xmas_request_key(): produce key for request hashtable sam_u->req_p from nam_u +*/ u3_noun _xmas_request_key(u3_xmas_name* nam_u) { u3_noun pax = _xmas_encode_path(nam_u->pat_s, (c3_y*)nam_u->pat_c); @@ -446,22 +532,42 @@ u3_noun _xmas_request_key(u3_xmas_name* nam_u) return res; } +/* _xmas_get_request(): produce pending request state for nam_u + * + * produces a NULL pointer if no pending request exists +*/ static u3_pend_req* _xmas_get_request(u3_xmas* sam_u, u3_xmas_name* nam_u) { u3_pend_req* ret_u = NULL; u3_noun key = _xmas_request_key(nam_u); u3_weak res = u3h_git(sam_u->req_p, key); - if ( res != u3_none ) { + if ( res != u3_none && res != u3_nul ) { ret_u = u3to(u3_pend_req, res); } + if ( ret_u != NULL && ret_u->tot_w != 0 && ret_u->tot_w == ret_u->len_w ) { + ret_u = NULL; + } return ret_u; } +/* _xmas_put_request(): save new pending request state for nam_u + * + * the memory in the hashtable is allocated once in the lifecycle of the + * request. req_u will be copied into the hashtable memory, and so can be + * immediately freed + * +*/ static void _xmas_put_request(u3_xmas* sam_u, u3_xmas_name* nam_u, u3_pend_req* req_u) { u3_noun key = _xmas_request_key(nam_u); + + if ( req_u == NULL) { + u3h_put(sam_u->req_p, key, u3_nul); + u3z(key); + return; + } u3_pend_req* old_u = _xmas_get_request(sam_u, nam_u); u3_pend_req* new_u = req_u; if ( old_u == NULL ) { @@ -478,118 +584,148 @@ _xmas_put_request(u3_xmas* sam_u, u3_xmas_name* nam_u, u3_pend_req* req_u) { u3z(key); } +/* _xmas_packet_timeout(): callback for packet timeout +*/ +static void +_xmas_packet_timeout(uv_timer_t* tim_u) { + u3_pend_req* req_u = (u3_pend_req*)tim_u->data; + u3l_log("%u packet timed out", req_u->las_w); +} + +/* _xmas_req_pact_done(): mark packet as done, possibly producing the result +*/ static u3_weak -_xmas_req_pact_done(u3_xmas* sam_u, u3_xmas_name *nam_u, c3_w tot_w, u3_noun fra) +_xmas_req_pact_done(u3_xmas* sam_u, u3_xmas_name *nam_u, c3_w tot_w, c3_y* buf_y, c3_w len_w) { u3_weak ret = u3_none; c3_d now_d = _get_now_micros(); u3_pend_req* req_u = _xmas_get_request(sam_u, nam_u); - if ( NULL != req_u ) { - // TODO: optional assertions? - req_u->dat = u3n_slam_on( - u3k(sam_u->mop_u.put), - u3nt(req_u->dat, u3i_word(nam_u->fra_w), fra) - ); - u3_noun wit = u3n_slam_on( - u3k(sam_u->mop_u.del), - u3nc(req_u->wat, u3i_word(nam_u->fra_w)) - ); - - req_u->wat = u3k(u3t(wit)); - u3z(wit); - - req_u->tot_w = tot_w; - u3_noun tot = u3i_word(req_u->tot_w); - //u3l_log("tot: %u, wyt: %u", req_u->tot_w, u3r_word(0, u3qdb_wyt(req_u->dat))); - if ( c3y == u3r_sing(u3qdb_wyt(req_u->dat), tot) ) { - ret = u3dc("rap", 13, u3dc("turn", u3n_slam_on(u3k(sam_u->mop_u.tap), req_u->dat), u3v_wish("tail"))); - //_xmas_put_request_raw(sam_u, nam_u, u3_none); - } else { - _xmas_put_request(sam_u, nam_u, req_u); - } - u3z(tot); - } else { + if ( NULL == req_u ) { +#ifdef XMAS_DEBUG u3l_log("xmas: attempting to mark packet done, no request"); +#endif return u3_none; - //u3m_bail(c3__foul); } - return ret; -} + c3_w siz_w = (1 << (nam_u->boq_y - 3)); -static c3_w -_xmas_req_get_nex(u3_xmas* sam_u, u3_xmas_name* nam_u) -{ - c3_w res_w = 0; - u3_pend_req* req_u = _xmas_get_request(sam_u, nam_u); - if ( NULL != req_u ) { - res_w = req_u->nex_w; + // First packet received, instantiate request fully + if ( req_u->tot_w == 0 ) { + u3_assert( siz_w == 1024); // boq_y == 13 + req_u->dat_y = c3_calloc(siz_w * tot_w); + req_u->wat_u = c3_calloc(sizeof(u3_pact_stat) * tot_w); + req_u->tot_w = tot_w; + _init_bitset(&req_u->was_u, tot_w + 1); + req_u->las_w = 0; + } + + // received duplicate + if ( nam_u->fra_w != 0 && c3n == _has_bitset(&req_u->was_u, nam_u->fra_w) ) { +#ifdef XMAS_DEBUG + u3l_log("duplicate page"); +#endif + return u3_none; } - return res_w; + _del_bitset(&req_u->was_u, nam_u->fra_w); + req_u->len_w++; + + + memcpy(req_u->dat_y + (siz_w * nam_u->fra_w), buf_y, len_w); + + // in-order + if (nam_u->fra_w == req_u->las_w ) { + u3l_log("in order %u", nam_u->fra_w); + } else { + // out of order + u3l_log("misordered last: %u have %u", req_u->las_w, nam_u->fra_w); + } + + + if ( req_u->len_w == req_u->tot_w ) { + uv_timer_stop(&req_u->tim_u); + //u3dc("rap", 13, u3dc("turn", u3n_slam_on(u3k(sam_u->mop_u.tap), req_u->dat), u3v_wish("tail"))); + //_xmas_put_request(sam_u, nam_u, u3_none); + return u3i_word(req_u->tot_w); + } else { + _xmas_put_request(sam_u, nam_u, req_u); + } + return u3_none; } +/* + * _xmas_req_get_cwnd(): produce current state of packet pump + * + * saves next fragment number and preallocated pact into the passed pointers. + * Will not do so if returning 0 +*/ static c3_w -_xmas_req_get_cwnd(u3_xmas* sam_u, u3_xmas_name* nam_u) +_xmas_req_get_cwnd_nex(u3_xmas* sam_u, u3_xmas_name* nam_u, c3_w* nex_w, u3_xmas_pact** nex_u) { c3_w res_w = 0; u3_pend_req* req_u = _xmas_get_request(sam_u, nam_u); if ( NULL == req_u ) { return 0; } - c3_w liv_w = u3r_word(0, u3qdb_wyt(req_u->wat)); + *nex_w = req_u->tot_w != 0 ? c3_min(req_u->nex_w, req_u->tot_w - 1) : req_u->nex_w; + *nex_u = req_u->pac_u; + + c3_w liv_w = _wyt_bitset(&req_u->was_u); + c3_w lef_w = req_u->tot_w - (req_u->len_w + liv_w); if ( liv_w < IN_FLIGHT ) { - return IN_FLIGHT - liv_w; + return c3_min(IN_FLIGHT - liv_w, lef_w); } return 0; } -static c3_o -_xmas_req_is_done(u3_xmas* sam_u, u3_xmas_name* nam_u, c3_w tot_w) -{ - c3_o ret_o = c3n; - u3_pend_req* req_u = _xmas_get_request(sam_u, nam_u); - if ( NULL != req_u ) { - u3_noun tot = u3i_word(tot_w); - ret_o = u3r_sing(tot, u3qdb_wyt(req_u->dat)); - u3z(tot); - } - - return ret_o; -} +/* _xmas_req_pact_sent(): mark packet as sent +** +*/ static void _xmas_req_pact_sent(u3_xmas* sam_u, u3_xmas_name* nam_u) { c3_d now_d = _get_now_micros(); - u3_noun pac = u3nt(u3i_chub(now_d), 1, 0); - u3_pend_req* req_u = _xmas_get_request(sam_u, nam_u); + // if we already have pending request if ( NULL != req_u ) { if( req_u->nex_w == nam_u->fra_w ) { req_u->nex_w++; } // TODO: optional assertions? - req_u->wat = u3n_slam_on( - u3k(sam_u->mop_u.put), - u3nt(req_u->wat, u3i_word(nam_u->fra_w), pac) - ); + req_u->wat_u[nam_u->fra_w] = (u3_pact_stat){now_d, 0, 1 }; + _put_bitset(&req_u->was_u, nam_u->fra_w); + if ( nam_u->fra_w == 1 ) { + req_u->las_w = 1; + } + } else { + // instantiate pending request + // stack allocating is ok, because _xmas_put_pend will copy it out anyway req_u = alloca(sizeof(u3_pend_req)); - u3l_log("TODO: restart requests"); - _log_name(nam_u); - //u3_assert( nam_u->fra_w == 0 ); + req_u->pac_u = c3_calloc(sizeof(u3_xmas_pact)); + req_u->pac_u->sam_u = sam_u; + req_u->pac_u->hed_u.typ_y = PACT_PEEK; + memcpy(&req_u->pac_u->pek_u.nam_u, nam_u, sizeof(u3_xmas_name)); + // TODO: handle restart + // u3_assert( nam_u->fra_w == 0 ); req_u->nex_w = 1; req_u->tot_w = 0; - req_u->dat = u3_nul; + req_u->len_w = 0; + req_u->dat_y = NULL; uv_timer_init(u3L, &req_u->tim_u); - req_u->wat = u3n_slam_on( - u3k(sam_u->mop_u.put), - u3nt(u3_nul, u3i_word(nam_u->fra_w), u3k(pac)) - ); - u3m_p("wat", req_u->wat); - u3m_p("dat", req_u->dat); + req_u->wat_u = NULL; + req_u->was_u.buf_y = NULL; + req_u->las_w = 0; + } + + if ( req_u->las_w != 0 && c3n == _has_bitset(&req_u->was_u, req_u->las_w) ) { + while ( req_u->las_w++ < req_u->tot_w ) { + if ( c3y == _has_bitset(&req_u->was_u, req_u->las_w) ) { + break; + } + } } _xmas_put_request(sam_u, nam_u, req_u); } @@ -758,20 +894,8 @@ u3_xmas_encode_lane(u3_lane lan_u) { // static void _xmas_free_seal(u3_seal* sel_u) { - c3_free(sel_u->buf_y); - c3_free(sel_u); -} -// refcounted -// -static u3_noun _xmas_path_with_fra(c3_c* pat_c, c3_s* fra_s) -{ - u3_noun pax = u3do("stab", u3i_string(pat_c)); - u3_noun fra = u3dc("slav", c3__ud, u3do("rear", u3k(pax))); - u3_noun res = u3do("snip", pax); - *fra_s = u3r_short(0, fra); - - u3z(fra); // pax is unncessary bc moved in snip - return res; + //c3_free(sel_u->buf_y); + //c3_free(sel_u); } static u3_noun _xmas_get_now() { @@ -819,60 +943,11 @@ _xmas_rout_pact(u3_xmas_pact* pac_u, u3_noun lan) { } -// refcounted -// RETAIN -static c3_y -_ship_meta(u3_noun her) -{ - c3_w met_w = u3r_met(3, her); - c3_y res_y; - switch ( met_w ) { - case 0: - case 1: - case 2: { - res_y = 0; - break; - } - case 3: - case 4: { - res_y = 1; - break; - } - case 5: - case 6: - case 7: - case 8: { - res_y = 2; - break; - } - default: { - res_y = 3; - break; - } - } - return res_y; -} - static void _xmas_pact_free(u3_xmas_pact* pac_u) { // TODO: i'm lazy } -static u3_xmas_head -_xmas_head_from_pact(u3_xmas_pact* pac_u, u3_noun her) -{ - u3_xmas_head hed_u; - hed_u.pro_y = XMAS_VER; - hed_u.typ_y = pac_u->typ_y; - // ship meta is retain, no u3k necessary - //hed_u.ran_y = _ship_meta(her); - hed_u.hop_y = 0; - hed_u.mug_w = 0; - - u3z(her); - return hed_u; -} - static c3_o _xmas_sift_head(c3_y buf_y[8], u3_xmas_head* hed_u) { @@ -1007,10 +1082,8 @@ _xmas_sift_page_pact(u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, CHECK_BOUNDS(cur_w + 1); mat_u->aul_y = buf_y[cur_w]; cur_w++; - } - CHECK_BOUNDS(cur_w + mat_u->aul_y); - for ( int i = 0; i < mat_u->aul_y; i++ ) { + CHECK_BOUNDS(cur_w + mat_u->aul_y); mat_u->aut_y = c3_calloc(mat_u->aul_y); memcpy(mat_u->aut_y, buf_y + cur_w, mat_u->aul_y); cur_w += mat_u->aul_y; @@ -1019,15 +1092,15 @@ _xmas_sift_page_pact(u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, c3_y len_y = mat_u->met_u.len_y; CHECK_BOUNDS(cur_w + len_y); u3_assert( len_y <= 8 ); - mat_u->len_d = 0; + mat_u->len_w = 0; for ( int i = 0; i < len_y; i++ ) { - mat_u->len_d |= (buf_y[cur_w] << (8*i)); + mat_u->len_w |= (buf_y[cur_w] << (8*i)); cur_w++; } - CHECK_BOUNDS(cur_w + len_y); - mat_u->fra_y = c3_calloc(mat_u->len_d); - memcpy(mat_u->fra_y, buf_y + cur_w, mat_u->len_d); + CHECK_BOUNDS(cur_w + (c3_w)mat_u->len_w); + mat_u->fra_y = c3_calloc(mat_u->len_w); + memcpy(mat_u->fra_y, buf_y + cur_w, mat_u->len_w); switch ( hed_u->nex_y ) { default: { @@ -1144,11 +1217,11 @@ _xmas_sift_pact(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w) } _xmas_sift_head(buf_y, &hed_u); - pac_u->typ_y = hed_u.typ_y; + pac_u->hed_u.typ_y = hed_u.typ_y; c3_w res_w = 0; buf_y += 8; len_w -= 8; - switch ( pac_u->typ_y ) { + switch ( pac_u->hed_u.typ_y ) { case PACT_PEEK: { res_w = _xmas_sift_peek_pact(&pac_u->pek_u, buf_y, len_w); } break; @@ -1206,55 +1279,12 @@ _xmas_etch_head(u3_xmas_head* hed_u, c3_y buf_y[8]) } static c3_w -_xmas_etch_page_pact(c3_y* buf_y, u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u) -{ - /*c3_w cur_w = 0; - - // hops - memcpy(buf_y, pac_u->hop_y, 6); - cur_w += 6; - - // path length - _ames_etch_short(buf_y + cur_w, pac_u->pat_s); - cur_w += 2; - - // path - memcpy(buf_y + cur_w, pac_u->pat_c, pac_u->pat_s + 1); - cur_w += pac_u->pat_s + 1; - - // total - _ames_etch_word(buf_y + cur_w, pac_u->tot_w); - cur_w += 4; - - // auth - memcpy(buf_y + cur_w, &pac_u->aut_y, 96); - cur_w += 96; - - // dat - memcpy(buf_y + cur_w, pac_u->dat_y, 1024); - cur_w += 1024; - - return cur_w;*/ - return 0; -} - -static c3_w -_xmas_etch_peek_pact(c3_y* buf_y, u3_xmas_peek_pact* pac_u, u3_xmas_head* hed_u, c3_o tag_o) +_xmas_etch_name(c3_y* buf_y, u3_xmas_name* nam_u) { #ifdef XMAS_DEBUG - if ( strlen(pac_u->nam_u.pat_c) != pac_u->nam_u.pat_s ) { - u3l_log( - "xmas: packet validation failure, with path %s expected length %i, got %i", - pac_u->nam_u.pat_c, - pac_u->nam_u.pat_s, - strlen(pac_u->nam_u.pat_c) - ); - u3m_bail(c3__foul); - } #endif c3_w cur_w = 0; - u3_xmas_name* nam_u = &pac_u->nam_u; nam_u->met_u.ran_y = safe_dec(u3r_met(0, u3r_met(4, u3i_chubs(2, nam_u->her_d)))); nam_u->met_u.rif_y = safe_dec(u3r_met(3, u3i_word(nam_u->rif_w))); nam_u->met_u.fra_y = safe_dec(u3r_met(3, u3i_word(nam_u->fra_w))); @@ -1262,11 +1292,11 @@ _xmas_etch_peek_pact(c3_y* buf_y, u3_xmas_peek_pact* pac_u, u3_xmas_head* hed_u, nam_u->met_u.pat_b = nam_u->pat_s > 0xFF ? 1 : 0; // TODO: double check reserved bits - c3_y met_y = (nam_u->met_u.ran_y & 0x3) >> 0 - ^ (nam_u->met_u.rif_y & 0x3) >> 2 - ^ (nam_u->met_u.pat_b & 0x1) >> 4 - ^ (nam_u->met_u.boq_b & 0x1) >> 5 - ^ (nam_u->met_u.fra_y & 0x3) >> 6; + c3_y met_y = (nam_u->met_u.ran_y & 0x3) << 0 + ^ (nam_u->met_u.rif_y & 0x3) << 2 + ^ (nam_u->met_u.pat_b & 0x1) << 4 + ^ (nam_u->met_u.boq_b & 0x1) << 5 + ^ (nam_u->met_u.fra_y & 0x3) << 6; buf_y[cur_w] = met_y; @@ -1313,11 +1343,62 @@ _xmas_etch_peek_pact(c3_y* buf_y, u3_xmas_peek_pact* pac_u, u3_xmas_head* hed_u, //_ames_etch_short(buf_y + cur_w, &met_s); } + + +static c3_w +_xmas_etch_page_pact(c3_y* buf_y, u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u) +{ + c3_w cur_w = 0; + { + c3_w siz_w = _xmas_etch_name(buf_y + cur_w, &pac_u->nam_u); + if ( siz_w == 0 ) { + return 0; + } + cur_w += siz_w; + } + + u3_xmas_page_meat* mat_u = &pac_u->mat_u; + + // metabyte TODO fix leak + mat_u->met_u.tot_y = safe_dec(u3r_met(3, u3i_word(mat_u->tot_w))); + mat_u->met_u.aut_b = (mat_u->aul_y != 0) & 0x1; + mat_u->met_u.len_y = u3r_met(3, u3i_chub(mat_u->len_w)); + c3_y met_y = (mat_u->met_u.tot_y & 0x3 ) << 0 + ^ (mat_u->met_u.aut_b & 0x1 ) << 2 + ^ (mat_u->met_u.len_y & 0x1F ) << 3; + + buf_y[cur_w] = met_y; + cur_w++; + + c3_y tot_y = mat_u->met_u.tot_y + 1; + for (int i = 0; i < tot_y; i++ ) { + buf_y[cur_w] = (mat_u->tot_w >> (8 * i)) & 0xFF; + cur_w++; + } + + if ( mat_u->met_u.aut_b == 1 ) { + buf_y[cur_w] = mat_u->aul_y; + cur_w++; + memcpy(buf_y + cur_w, mat_u->aut_y, mat_u->aul_y); + cur_w += mat_u->aul_y; + } + + c3_y len_y = mat_u->met_u.len_y; + for( int i = 0; i < len_y; i++ ) { + buf_y[cur_w] = (mat_u->len_w >> (8 * i)) & 0xFF; + cur_w++; + } + + memcpy(buf_y + cur_w, mat_u->fra_y, mat_u->len_w); + cur_w += mat_u->len_w; + // TODO: hopcount + return cur_w; +} + static c3_w _xmas_etch_poke_pact(c3_y* buf_y, u3_xmas_poke_pact* pac_u, u3_xmas_head* hed_u) { c3_w cur_w = 0; - cur_w += _xmas_etch_peek_pact(buf_y, pac_u->pek_u, hed_u, c3n); // total _ames_etch_word(buf_y + cur_w, pac_u->tot_w); @@ -1337,20 +1418,33 @@ static c3_w _xmas_etch_pact(c3_y* buf_y, u3_xmas_pact* pac_u, u3_noun her) { c3_w cur_w = 0; - u3_xmas_head hed_u = _xmas_head_from_pact(pac_u, her); - _xmas_etch_head(&hed_u, buf_y + cur_w); + u3_xmas_head* hed_u = &pac_u->hed_u; + _xmas_etch_head(hed_u, buf_y + cur_w); cur_w += 8; + c3_w siz_w = 0; - switch ( pac_u->typ_y ) { + switch ( pac_u->hed_u.typ_y ) { case PACT_POKE: { - cur_w += _xmas_etch_poke_pact(buf_y + cur_w, &pac_u->pok_u, &hed_u); + c3_w siz_w = _xmas_etch_poke_pact(buf_y + cur_w, &pac_u->pok_u, hed_u); + if ( siz_w == 0 ) { + return 0; + } + cur_w += siz_w; } break; case PACT_PEEK: { - cur_w += _xmas_etch_peek_pact(buf_y + cur_w, &pac_u->pek_u, &hed_u, c3y); + siz_w = _xmas_etch_name(buf_y + cur_w, &pac_u->pek_u.nam_u); + if ( siz_w == 0 ) { + return 0; + } + cur_w += siz_w; } break; case PACT_PAGE: { - cur_w += _xmas_etch_page_pact(buf_y + cur_w, &pac_u->pag_u, &hed_u); + siz_w = _xmas_etch_page_pact(buf_y + cur_w, &pac_u->pag_u, hed_u); + if ( siz_w == 0 ) { + return 0; + } + cur_w += siz_w; } break; default: { @@ -1418,10 +1512,9 @@ static void _xmas_send_buf(u3_xmas* sam_u, u3_lane lan_u, c3_y* buf_y, c3_w len_ static void _xmas_send(u3_xmas_pact* pac_u) { u3_xmas* sam_u = pac_u->sam_u; - u3l_log("xmas sending"); - //u3l_log("_ames_send %s %u", _str_typ(pac_u->typ_y), + //u3l_log("_ames_send %s %u", _str_typ(pac_u->hed_u.typ_y), // pac_u->rut_u.lan_u.por_s); c3_y* buf_y = c3_calloc(PACT_SIZE); c3_w siz_w; @@ -1543,7 +1636,7 @@ _xmas_ef_send(u3_xmas* sam_u, u3_noun las, u3_noun pac) pac_u.sam_u = sam_u; _xmas_sift_pact(&pac_u, buf_y, len); - if ( pac_u.typ_y == PACT_PEEK ) { + if ( pac_u.hed_u.typ_y == PACT_PEEK ) { sam_u->tim_d = get_millis(); _xmas_update_req_peek(&pac_u); } @@ -1568,7 +1661,7 @@ _xmas_ef_peek(u3_xmas* sam_u, u3_noun las, u3_noun who, u3_noun pat, u3_noun cop u3z(pat); } else { u3_xmas_pact* pac_u = c3_calloc(sizeof(*pac_u)); - pac_u->typ_y = PACT_PEEK; + pac_u->hed_u.typ_y = PACT_PEEK; pac_u->sam_u = sam_u; { u3_noun pas = u3do("spat", pat); @@ -1662,10 +1755,12 @@ _xmas_exit_cb(uv_handle_t* had_u) ur_cue_test_done(sam_u->tes_u); u3h_free(sam_u->her_p); u3h_free(sam_u->req_p); - c3_free(sam_u); u3_cbic_done(sam_u->gag_u); c3_free(sam_u->gag_u); + + + c3_free(sam_u); } static void @@ -1765,7 +1860,7 @@ _xmas_czar(u3_xmas_pact* pac_u) { u3_xmas* sam_u = pac_u->sam_u; #ifdef XMAS_DEBUG - if ( pac_u->typ_y != PACT_PEEK ) { + if ( pac_u->hed_u.typ_y != PACT_PEEK ) { u3l_log("xmas: attempted to resolve galaxy for packet that was not peek"); u3m_bail(c3__oops); return; @@ -1919,49 +2014,45 @@ _xmas_hear_page(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w, u3_lane lan_u) u3_noun wir = u3nc(c3__xmas, u3_nul); c3_s fra_s; - u3_noun fra = u3i_bytes(pac_u->pag_u.mat_u.len_d, pac_u->pag_u.mat_u.fra_y) ; + u3_noun fra = u3i_bytes(pac_u->pag_u.mat_u.len_w, pac_u->pag_u.mat_u.fra_y) ; /*if ( dop_o == c3n && pac_u->pag_u.nam_u.fra_w == 150) { dop_o = c3y; u3l_log("simulating dropped packet"); return; }*/ - u3_weak res = _xmas_req_pact_done(sam_u, &pac_u->pag_u.nam_u, pac_u->pag_u.mat_u.tot_w, fra); + u3_weak res = _xmas_req_pact_done(sam_u, &pac_u->pag_u.nam_u, pac_u->pag_u.mat_u.tot_w, pac_u->pag_u.mat_u.fra_y, pac_u->pag_u.mat_u.len_w); if ( u3_none != res ) { u3l_log("finished"); c3_d now_d = get_millis(); u3l_log("took: %" PRIu64 " ms", now_d - sam_u->tim_d); - u3l_log("for %u bytes", u3r_met(3, res)); - u3z(res); return; } + u3_xmas_pact* nex_u; - c3_w nex_w = _xmas_req_get_nex(sam_u, &pac_u->pag_u.nam_u); - c3_w win_w = _xmas_req_get_cwnd(sam_u, &pac_u->pag_u.nam_u); + c3_w nex_w; + c3_w win_w = _xmas_req_get_cwnd_nex(sam_u, &pac_u->pag_u.nam_u, &nex_w, &nex_u); if ( win_w != 0 ) { #ifdef XMAS_DEBUG u3l_log("continuing flow nex: %u, win: %u", nex_w, win_w); #endif - u3_xmas_pact* nex_u = c3_calloc(sizeof(u3_xmas_pact)); - nex_u->sam_u = sam_u; - nex_u->typ_y = PACT_PEEK; - memcpy(&nex_u->pek_u.nam_u, &pac_u->pag_u.nam_u, sizeof(u3_xmas_name)); + c3_y* buf_y = c3_calloc(PACT_SIZE * win_w); + c3_y* cur_y = buf_y; for(int i = 0; i < win_w; i++) { nex_u->pek_u.nam_u.fra_w = nex_w + i; - c3_y* buf_y = c3_calloc(PACT_SIZE); - c3_w siz_w =_xmas_etch_pact(buf_y, nex_u, 0); + c3_w siz_w =_xmas_etch_pact(cur_y, nex_u, 0); if( siz_w == 0 ) { u3l_log("failed to etch"); u3_assert( 0 ); } // TODO: better route management - _xmas_send_buf(sam_u, lan_u, buf_y, siz_w); + _xmas_send_buf(sam_u, lan_u, cur_y, siz_w); _xmas_req_pact_sent(sam_u, &nex_u->pek_u.nam_u); + cur_y += siz_w; } - c3_free(nex_u); } } @@ -1985,7 +2076,7 @@ _xmas_hear_peek(u3_xmas_pact* pac_u, u3_lane lan_u) u3l_log("xmas: hear peek"); _log_name(&pac_u->pek_u.nam_u); u3l_log("%hu", lan_u.por_s); - // u3_assert(pac_u->typ_y == PACT_PEEK); + // u3_assert(pac_u->hed_u.typ_y == PACT_PEEK); #endif u3_xmas* sam_u = pac_u->sam_u; @@ -2052,7 +2143,7 @@ _xmas_hear(u3_xmas* sam_u, u3l_log("xmas: sifted packet"); #endif - switch ( pac_u->typ_y ) { + switch ( pac_u->hed_u.typ_y ) { case PACT_PEEK: { _xmas_hear_peek(pac_u, *lan_u); } break; @@ -2060,7 +2151,7 @@ _xmas_hear(u3_xmas* sam_u, _xmas_hear_page(pac_u, hun_y, len_w, *lan_u); } break; default: { - u3l_log("xmas: unimplemented packet type %u", pac_u->typ_y); + u3l_log("xmas: unimplemented packet type %u", pac_u->hed_u.typ_y); } break; } } @@ -2242,6 +2333,109 @@ u3_xmas_io_init(u3_pier* pir_u) #ifdef XMAS_TEST +static void +_test_bitset() +{ + u3_bitset bit_u; + _init_bitset(&bit_u, 500); + + _put_bitset(&bit_u, 5); + _put_bitset(&bit_u, 50); + _put_bitset(&bit_u, 100); + + c3_w wyt_w = _wyt_bitset(&bit_u); + if ( 3 != wyt_w ) { + u3l_log("wyt failed have %u expect %u", wyt_w, 3); + exit(1); + } + + if ( c3y == _has_bitset(&bit_u, 3) ) { + u3l_log("false positive for has_bitset"); + exit(1); + } + + if ( c3n == _has_bitset(&bit_u, 50) ) { + u3l_log("false negative for has_bitset"); + exit(1); + } + + _del_bitset(&bit_u, 50); + + if ( c3y == _has_bitset(&bit_u, 50) ) { + u3l_log("false positive for has_bitset"); + exit(1); + } + + wyt_w = _wyt_bitset(&bit_u); + + if ( 2 != wyt_w ) { + u3l_log("wyt failed have %u expect %u", wyt_w, 2); + exit(1); + } +} + +static void +_test_sift_page() +{ + u3_xmas_pact pac_u; + memset(&pac_u,0, sizeof(u3_xmas_pact)); + pac_u.typ_y = PACT_PAGE; + u3l_log("checking sift/etch idempotent"); + u3_xmas_name* nam_u = &pac_u.pag_u.nam_u; + u3_noun her = u3v_wish("~hastuc-dibtux"); + u3r_chubs(0, 2, nam_u->her_d, her); + nam_u->rif_w = 15; + nam_u->pat_c = "foo/bar"; + nam_u->pat_s = strlen(nam_u->pat_c); + nam_u->boq_y = 13; + nam_u->fra_w = 54; + + u3_xmas_page_meat* mat_u = &pac_u.pag_u.mat_u; + mat_u->len_w = 1024; + mat_u->fra_y = c3_calloc(1024); + mat_u->tot_w = 1000; + + c3_y* buf_y = c3_calloc(PACT_SIZE); + c3_w len_w =_xmas_etch_pact(buf_y, &pac_u, her); + u3_xmas_pact nex_u; + memset(&nex_u, 0, sizeof(u3_xmas_pact)); + _xmas_sift_pact(&nex_u, buf_y, len_w); + + if ( 0 != memcmp(nex_u.pag_u.nam_u.pat_c, pac_u.pag_u.nam_u.pat_c, pac_u.pag_u.nam_u.pat_s) ) { + u3l_log(RED_TEXT); + u3l_log("path mismatch"); + u3l_log("got"); + _log_pact(&nex_u); + u3l_log("expected"); + _log_pact(&pac_u); + exit(1); + } + nex_u.pag_u.nam_u.pat_c = 0; + pac_u.pag_u.nam_u.pat_c = 0; + + if ( 0 != memcmp(nex_u.pag_u.mat_u.fra_y, pac_u.pag_u.mat_u.fra_y, pac_u.pag_u.mat_u.len_w) ) { + u3l_log(RED_TEXT); + u3l_log("mismatch"); + u3l_log("got"); + _log_buf(nex_u.pag_u.mat_u.fra_y, nex_u.pag_u.mat_u.len_w); + u3l_log("expected"); + _log_buf(pac_u.pag_u.mat_u.fra_y, pac_u.pag_u.mat_u.len_w); + exit(1); + } + + nex_u.pag_u.mat_u.fra_y = 0; + pac_u.pag_u.mat_u.fra_y = 0; + if ( 0 != memcmp(&nex_u, &pac_u, sizeof(u3_xmas_pact)) ) { + u3l_log(RED_TEXT); + u3l_log("mismatch"); + u3l_log("got"); + _log_pact(&nex_u); + u3l_log("expected"); + _log_pact(&pac_u); + exit(1); + } +} + static void _test_sift_peek() { @@ -2256,7 +2450,7 @@ _test_sift_peek() nam_u->pat_c = "foo/bar"; nam_u->pat_s = strlen(nam_u->pat_c); nam_u->boq_y = 13; - nam_u->fra_w = 54; + nam_u->fra_w = 511; c3_y* buf_y = c3_calloc(PACT_SIZE); c3_w len_w =_xmas_etch_pact(buf_y, &pac_u, her); @@ -2331,8 +2525,10 @@ int main() { _setup(); + _test_bitset(); - //_test_sift_peek(); + _test_sift_peek(); + _test_sift_page(); _test_encode_path("foo/bar/baz"); _test_encode_path("publ/0/xx//1/foo/g"); From 0627d3ffb6b9b99ce2e30da18d0a968a759cc51a Mon Sep 17 00:00:00 2001 From: Liam Fitzgerald Date: Fri, 2 Feb 2024 11:26:17 -0500 Subject: [PATCH 026/430] xmas: move congestion control to per [ship, lane] --- pkg/vere/io/xmas.c | 179 ++++++++++++++++++++++++++++++--------------- 1 file changed, 119 insertions(+), 60 deletions(-) diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c index 3d685de4fa..fb5cab3809 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/xmas.c @@ -81,8 +81,14 @@ typedef struct _u3_pend_req { c3_y* dat_y; // ((mop @ud *) lte) c3_w len_w; c3_w las_w; // frag num of last packet sent + u3_gage* gag_u; } u3_pend_req; +typedef struct _u3_czar_info { + c3_w pip_w; + time_t tim_t; + u3_noun pen; +} u3_czar_info; /* _u3_xmas: next generation networking */ @@ -98,19 +104,10 @@ typedef struct _u3_xmas { u3_cue_xeno* sil_u; // cue handle u3p(u3h_root) her_p; // u3p(u3h_root) pac_p; // pending - u3_gage* gag_u; - c3_w imp_w[256]; - time_t imp_t[256]; // imperial IP timestamps - c3_o imp_o[256]; // imperial print status - c3_c* dns_c; // - // jj + u3p(u3h_root) lan_p; // lanes + u3_czar_info imp_u[256]; u3p(u3h_root) req_p; // hashtable of u3_pend_req - struct { - u3_noun put; - u3_noun del; - u3_noun tap; - u3_noun pry; - } mop_u; // pointer to ordered map functions + c3_c* dns_c; c3_d tim_d; } u3_xmas; @@ -152,6 +149,8 @@ typedef struct _u3_xmas_name { c3_w fra_w; } u3_xmas_name; + + typedef struct _u3_xmas_rout { u3_xmas_rout_tag typ_y; // type tag union { @@ -532,6 +531,69 @@ u3_noun _xmas_request_key(u3_xmas_name* nam_u) return res; } +/* u3_xmas_encode_lane(): serialize lane to noun +*/ +static u3_atom +u3_xmas_encode_lane(u3_lane lan_u) { + // [%if ip=@ port=@] + return u3nt(c3__if, u3i_word(lan_u.pip_w), lan_u.por_s); +} + +static u3_noun +_xmas_lane_key(c3_d her_d[2], u3_lane* lan_u) +{ + return u3nc(u3i_chubs(2,her_d), u3_xmas_encode_lane(*lan_u)); +} + +/* RETAIN +*/ +static u3_gage* +_xmas_get_lane_raw(u3_xmas* sam_u, u3_noun key) +{ + u3_gage* ret_u = NULL; + u3_weak res = u3h_git(sam_u->lan_p, key); + + if ( res != u3_none && res != u3_nul ) { + ret_u = u3to(u3_gage, res); + } + + return ret_u; +} + +/* _xmas_get_lane(): get lane +*/ +static u3_gage* +_xmas_get_lane(u3_xmas* sam_u, c3_d her_d[2], u3_lane* lan_u) { + u3_noun key =_xmas_lane_key(her_d, lan_u); + u3_gage* ret_u = _xmas_get_lane_raw(sam_u, key); + u3z(key); + return ret_u; +} + +/* _xmas_put_lane(): put lane state in state + * + * uses same copying trick as _xmas_put_request() +*/ +static void +_xmas_put_lane(u3_xmas* sam_u, c3_d her_d[2], u3_lane* lan_u, u3_gage* gag_u) +{ + u3_noun key = _xmas_lane_key(her_d, lan_u); + u3_gage* old_u = _xmas_get_lane_raw(sam_u, key); + u3_gage* new_u = gag_u; + + if ( old_u == NULL ) { + new_u = u3a_calloc(sizeof(u3_gage),1); + memcpy(new_u, gag_u, sizeof(u3_gage)); + } else { + new_u = old_u; + memcpy(new_u, gag_u, sizeof(u3_gage)); + } + + u3_noun val = u3of(u3_gage, new_u); + u3h_put(sam_u->req_p, key, val); + u3z(key); +} + /* _xmas_get_request(): produce pending request state for nam_u * * produces a NULL pointer if no pending request exists @@ -549,6 +611,7 @@ _xmas_get_request(u3_xmas* sam_u, u3_xmas_name* nam_u) { ret_u = NULL; } + u3z(key); return ret_u; } @@ -592,10 +655,36 @@ _xmas_packet_timeout(uv_timer_t* tim_u) { u3l_log("%u packet timed out", req_u->las_w); } +static void _init_gage(u3_gage* gag_u) +{ + gag_u->rto_w = 1000000; + gag_u->rtt_w = 1000000; + gag_u->rtv_w = 1000000; + gag_u->con_w = 0; + gag_u->wnd_w = 1; +} + +static void _xmas_handle_ack(u3_gage* gag_u, u3_pact_stat* pat_u) +{ + + gag_u->con_w++; + + c3_d now_d = _get_now_micros(); + c3_d rtt_d = now_d < pat_u->sen_d ? 0 : now_d - pat_u->sen_d; + + c3_d err_d = _abs_dif(rtt_d, gag_u->rtt_w); + + gag_u->rtt_w = (rtt_d + (gag_u->rtt_w * 7)) >> 3; + gag_u->rtv_w = (err_d + (gag_u->rtv_w * 7)) >> 3; + gag_u->rto_w = _clamp_rto(gag_u->rtt_w + (4*gag_u->rtv_w)); + + u3_cbic_on_ack(gag_u); +} + /* _xmas_req_pact_done(): mark packet as done, possibly producing the result */ static u3_weak -_xmas_req_pact_done(u3_xmas* sam_u, u3_xmas_name *nam_u, c3_w tot_w, c3_y* buf_y, c3_w len_w) +_xmas_req_pact_done(u3_xmas* sam_u, u3_xmas_name *nam_u, u3_xmas_page_meat* mat_u, u3_lane* lan_u) { u3_weak ret = u3_none; c3_d now_d = _get_now_micros(); @@ -607,16 +696,25 @@ _xmas_req_pact_done(u3_xmas* sam_u, u3_xmas_name *nam_u, c3_w tot_w, c3_y* buf_y #endif return u3_none; } - c3_w siz_w = (1 << (nam_u->boq_y - 3)); + c3_w siz_w = (1 << (nam_u->boq_y - 3)); // First packet received, instantiate request fully if ( req_u->tot_w == 0 ) { u3_assert( siz_w == 1024); // boq_y == 13 - req_u->dat_y = c3_calloc(siz_w * tot_w); - req_u->wat_u = c3_calloc(sizeof(u3_pact_stat) * tot_w); - req_u->tot_w = tot_w; - _init_bitset(&req_u->was_u, tot_w + 1); + req_u->dat_y = c3_calloc(siz_w * mat_u->tot_w); + req_u->wat_u = c3_calloc(sizeof(u3_pact_stat) * mat_u->tot_w); + req_u->tot_w = mat_u->tot_w; + _init_bitset(&req_u->was_u, mat_u->tot_w + 1); req_u->las_w = 0; + } else { + // handle gauge update + u3_gage* gag_u = _xmas_get_lane(sam_u, nam_u->her_d, lan_u); + + if ( gag_u == NULL ) { + gag_u = alloca(sizeof(u3_gage)); + _init_gage(gag_u); + } + _xmas_handle_ack(gag_u, &req_u->wat_u[nam_u->fra_w]); } // received duplicate @@ -630,7 +728,7 @@ _xmas_req_pact_done(u3_xmas* sam_u, u3_xmas_name *nam_u, c3_w tot_w, c3_y* buf_y req_u->len_w++; - memcpy(req_u->dat_y + (siz_w * nam_u->fra_w), buf_y, len_w); + memcpy(req_u->dat_y + (siz_w * nam_u->fra_w), mat_u->fra_y, mat_u->len_w); // in-order if (nam_u->fra_w == req_u->las_w ) { @@ -874,14 +972,6 @@ u3_xmas_lane_to_chub(u3_lane lan) { return ((c3_d)lan.por_s << 32) ^ (c3_d)lan.pip_w; } -/* u3_xmas_encode_lane(): serialize lane to noun -*/ -static u3_atom -u3_xmas_encode_lane(u3_lane lan_u) { - // [%| p=@] - // [%& p=@pC] - return u3nt(c3__if, u3i_word(lan_u.pip_w), lan_u.por_s); -} @@ -1756,10 +1846,6 @@ _xmas_exit_cb(uv_handle_t* had_u) u3h_free(sam_u->her_p); u3h_free(sam_u->req_p); - u3_cbic_done(sam_u->gag_u); - c3_free(sam_u->gag_u); - - c3_free(sam_u); } @@ -1982,27 +2068,7 @@ _xmas_hear_bail(u3_ovum* egg_u, u3_noun lud) u3_ovum_free(egg_u); } -static void _xmas_handle_ack(u3_xmas* sam_u, u3_noun pas) -{ - u3_noun las, tie, kip; - u3x_cell(pas, &las, &tie); - c3_d las_d = u3r_chub(0, las); - c3_w tie_w = u3r_word(0, tie); - u3_gage* gag_u = sam_u->gag_u; - gag_u->con_w++; - - c3_d now_d = _get_now_micros(); - c3_d rtt_d = now_d < las_d ? 0 : now_d - las_d; - - c3_d err_d = _abs_dif(rtt_d, gag_u->rtt_w); - - gag_u->rtt_w = (rtt_d + (gag_u->rtt_w * 7)) >> 3; - gag_u->rtv_w = (err_d + (gag_u->rtv_w * 7)) >> 3; - gag_u->rto_w = _clamp_rto(gag_u->rtt_w + (4*gag_u->rtv_w)); - - u3_cbic_on_ack(gag_u); -} static void _xmas_hear_page(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w, u3_lane lan_u) @@ -2021,7 +2087,7 @@ _xmas_hear_page(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w, u3_lane lan_u) return; }*/ - u3_weak res = _xmas_req_pact_done(sam_u, &pac_u->pag_u.nam_u, pac_u->pag_u.mat_u.tot_w, pac_u->pag_u.mat_u.fra_y, pac_u->pag_u.mat_u.len_w); + u3_weak res = _xmas_req_pact_done(sam_u, &pac_u->pag_u.nam_u, &pac_u->pag_u.mat_u, &lan_u); if ( u3_none != res ) { u3l_log("finished"); @@ -2286,6 +2352,7 @@ u3_xmas_io_init(u3_pier* pir_u) sam_u->her_p = u3h_new_cache(100000); sam_u->req_p = u3h_new_cache(100000); + sam_u->lan_p = u3h_new_cache(100000); u3_assert( !uv_udp_init(u3L, &sam_u->wax_u) ); sam_u->wax_u.data = sam_u; @@ -2308,15 +2375,7 @@ u3_xmas_io_init(u3_pier* pir_u) car_u->io.kick_f = _xmas_io_kick; car_u->io.exit_f = _xmas_io_exit; - sam_u->gag_u = c3_calloc(sizeof(u3_gage)); - - uv_timer_init(u3L, &sam_u->gag_u->tim_u); - u3_cbic_init(sam_u->gag_u); - sam_u->mop_u.put = u3v_wish("put:((on @ud *) lte)"); - sam_u->mop_u.del = u3v_wish("del:((on @ud *) lte)"); - sam_u->mop_u.pry = u3v_wish("pry:((on @ud *) lte)"); - sam_u->mop_u.tap = u3v_wish("tap:((on @ud *) lte)"); /*{ u3_noun now; From ef7cca34d47da2727c61103370b76afe3324cad5 Mon Sep 17 00:00:00 2001 From: Liam Fitzgerald Date: Fri, 2 Feb 2024 16:57:49 -0500 Subject: [PATCH 027/430] wip --- pkg/vere/io/xmas.c | 1776 ++++++++++++++++++++++++-------------------- 1 file changed, 963 insertions(+), 813 deletions(-) diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c index fb5cab3809..97c11ab467 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/xmas.c @@ -15,6 +15,7 @@ #include #include #include +#include c3_o dop_o = c3n; @@ -27,6 +28,7 @@ c3_o dop_o = c3n; #define PACT_SIZE 1472 #define RED_TEXT "\033[0;31m" #define DEF_TEXT "\033[0m" +#define REORDER_THRESH 5 #define IN_FLIGHT 10 @@ -68,8 +70,11 @@ typedef struct _u3_pact_stat { c3_d sen_d; // last sent c3_y sip_y; // skips c3_y tie_y; // tries + c3_y dup_y; // dupes } u3_pact_stat; +struct _u3_xmas; + typedef struct _u3_pend_req { c3_w nex_w; @@ -80,8 +85,11 @@ typedef struct _u3_pend_req { u3_bitset was_u; // ((mop @ud packet-state) lte) c3_y* dat_y; // ((mop @ud *) lte) c3_w len_w; - c3_w las_w; // frag num of last packet sent - u3_gage* gag_u; + c3_w lef_w; // lowest fragment number currently in flight/pending + c3_w old_w; // frag num of oldest packet sent + c3_w ack_w; // highest acked fragment number + u3_lane lan_u; // last lane heard + u3_gage* gag_u; } u3_pend_req; typedef struct _u3_czar_info { @@ -346,14 +354,14 @@ static void _log_gage(u3_gage* gag_u) { u3l_log("gauge"); - u3l_log("rtt: %u", gag_u->rtt_w); - u3l_log("rto: %u", gag_u->rto_w); - u3l_log("rttvar: %u", gag_u->rtv_w); + u3l_log("rtt: %f", ((double)gag_u->rtt_w / 1000)); + u3l_log("rto: %f", ((double)gag_u->rto_w / 1000)); + u3l_log("rttvar: %f", ((double)gag_u->rtv_w / 1000)); u3l_log("cwnd: %u", gag_u->wnd_w); u3l_log("cwnd cnt: %u", gag_u->wnc_w); u3l_log("ssthresh: %u", gag_u->sst_w); u3l_log("counter: %u", gag_u->con_w); - u3l_log("algorithm: %s", gag_u->alg_c); + //u3l_log("algorithm: %s", gag_u->alg_c); } static void @@ -521,311 +529,365 @@ _xmas_encode_path(c3_w len_w, c3_y* buf_y) return pro; } - -/* _xmas_request_key(): produce key for request hashtable sam_u->req_p from nam_u +/* _ames_chub_bytes(): c3_y[8] to c3_d +** XX factor out, deduplicate with other conversions */ -u3_noun _xmas_request_key(u3_xmas_name* nam_u) +static inline c3_d +_ames_chub_bytes(c3_y byt_y[8]) { - u3_noun pax = _xmas_encode_path(nam_u->pat_s, (c3_y*)nam_u->pat_c); - u3_noun res = u3nt(u3i_chubs(2, nam_u->her_d), u3i_word(nam_u->rif_w), pax); - return res; + 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; } -/* u3_xmas_encode_lane(): serialize lane to noun +/* _ames_ship_to_chubs(): pack [len_y] bytes into c3_d[2] */ -static u3_atom -u3_xmas_encode_lane(u3_lane lan_u) { - // [%if ip=@ port=@] - return u3nt(c3__if, u3i_word(lan_u.pip_w), lan_u.por_s); +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); } -static u3_noun -_xmas_lane_key(c3_d her_d[2], u3_lane* lan_u) +/* _ames_chub_bytes(): c3_d to c3_y[8] +** XX factor out, deduplicate with other conversions +*/ +static inline void +_ames_bytes_chub(c3_y byt_y[8], c3_d num_d) { - return u3nc(u3i_chubs(2,her_d), u3_xmas_encode_lane(*lan_u)); + 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; } -/* RETAIN -*/ -static u3_gage* -_xmas_get_lane_raw(u3_xmas* sam_u, u3_noun key) +static inline void +_ames_ship_of_chubs(c3_d sip_d[2], c3_y len_y, c3_y* buf_y) { - u3_gage* ret_u = NULL; - u3_weak res = u3h_git(sam_u->lan_p, key); + c3_y sip_y[16] = {0}; - if ( res != u3_none && res != u3_nul ) { - ret_u = u3to(u3_gage, res); - } + _ames_bytes_chub(sip_y, sip_d[0]); + _ames_bytes_chub(sip_y + 8, sip_d[1]); - return ret_u; + memcpy(buf_y, sip_y, c3_min(16, len_y)); } -/* _xmas_get_lane(): get lane -*/ -static u3_gage* -_xmas_get_lane(u3_xmas* sam_u, c3_d her_d[2], u3_lane* lan_u) { - u3_noun key =_xmas_lane_key(her_d, lan_u); - u3_gage* ret_u = _xmas_get_lane_raw(sam_u, key); - u3z(key); - return ret_u; + +static inline c3_s +_ames_sift_short(c3_y buf_y[2]) +{ + return (buf_y[1] << 8 | buf_y[0]); } -/* _xmas_put_lane(): put lane state in state - * - * uses same copying trick as _xmas_put_request() -*/ -static void -_xmas_put_lane(u3_xmas* sam_u, c3_d her_d[2], u3_lane* lan_u, u3_gage* gag_u) +static inline c3_w +_ames_sift_word(c3_y buf_y[4]) { - u3_noun key = _xmas_lane_key(her_d, lan_u); - u3_gage* old_u = _xmas_get_lane_raw(sam_u, key); - u3_gage* new_u = gag_u; + return (buf_y[3] << 24 | buf_y[2] << 16 | buf_y[1] << 8 | buf_y[0]); +} + + +static c3_o +_xmas_sift_head(c3_y buf_y[8], u3_xmas_head* hed_u) +{ + if ( memcmp(buf_y + 4, &XMAS_COOKIE, XMAS_COOKIE_LEN) ) { + return c3n; - if ( old_u == NULL ) { - new_u = u3a_calloc(sizeof(u3_gage),1); - memcpy(new_u, gag_u, sizeof(u3_gage)); - } else { - new_u = old_u; - memcpy(new_u, gag_u, sizeof(u3_gage)); } + c3_w hed_w = _ames_sift_word(buf_y); - u3_noun val = u3of(u3_gage, new_u); - u3h_put(sam_u->req_p, key, val); - u3z(key); + hed_u->nex_y = (hed_w >> 2) & 0x3; + hed_u->pro_y = (hed_w >> 4) & 0x7; + hed_u->typ_y = (hed_w >> 7) & 0x3; + hed_u->hop_y = (hed_w >> 9) & 0x7; + hed_u->mug_w = (hed_w >> 12) & 0xFFFFFF; + + return c3y; + + /*if(c3o((hed_u->typ_y == PACT_PEEK), (hed_u->typ_y == PACT_POKE))) { + hed_u->ran_y = (hed_w >> 30) & 0x3; + }*/ } -/* _xmas_get_request(): produce pending request state for nam_u - * - * produces a NULL pointer if no pending request exists -*/ -static u3_pend_req* -_xmas_get_request(u3_xmas* sam_u, u3_xmas_name* nam_u) { - u3_pend_req* ret_u = NULL; - u3_noun key = _xmas_request_key(nam_u); +static c3_w +_xmas_sift_name(u3_xmas_name* nam_u, c3_y* buf_y, c3_w len_w) +{ +#ifdef XMAS_DEBUG + //u3l_log("xmas: sifting name %i", len_w); +#endif - u3_weak res = u3h_git(sam_u->req_p, key); - if ( res != u3_none && res != u3_nul ) { - ret_u = u3to(u3_pend_req, res); + c3_w cur_w = 0; + CHECK_BOUNDS(cur_w + 1); + c3_y met_y = buf_y[cur_w]; + nam_u->met_u.ran_y = (met_y >> 0) & 0x3; + nam_u->met_u.rif_y = (met_y >> 2) & 0x3; + nam_u->met_u.pat_b = (met_y >> 4) & 0x1; + nam_u->met_u.boq_b = (met_y >> 5) & 0x1; + nam_u->met_u.fra_y = (met_y >> 6) & 0x3; + cur_w += 1; + + c3_y her_y = 2 << nam_u->met_u.ran_y; + CHECK_BOUNDS(cur_w + her_y) + _ames_ship_to_chubs(nam_u->her_d, her_y, buf_y + cur_w); + cur_w += her_y; + + c3_y rif_y = nam_u->met_u.rif_y + 1; + nam_u->rif_w = 0; + CHECK_BOUNDS(cur_w + rif_y) + for( int i = 0; i < rif_y; i++ ) { + nam_u->rif_w |= (buf_y[cur_w] << (8*i)); + cur_w++; } - if ( ret_u != NULL && ret_u->tot_w != 0 && ret_u->tot_w == ret_u->len_w ) { - ret_u = NULL; + + c3_y pat_y = 1 << nam_u->met_u.pat_b; + CHECK_BOUNDS(cur_w + pat_y) + nam_u->pat_s = 0; + for ( int i = 0; i < pat_y; i++ ) { + nam_u->pat_s |= (buf_y[cur_w] << (8*i)); + cur_w++; } - u3z(key); - return ret_u; -} - -/* _xmas_put_request(): save new pending request state for nam_u - * - * the memory in the hashtable is allocated once in the lifecycle of the - * request. req_u will be copied into the hashtable memory, and so can be - * immediately freed - * -*/ -static void -_xmas_put_request(u3_xmas* sam_u, u3_xmas_name* nam_u, u3_pend_req* req_u) { - u3_noun key = _xmas_request_key(nam_u); + nam_u->pat_c = c3_calloc(nam_u->pat_s + 1); // unix string for ease of manipulation + CHECK_BOUNDS(nam_u->pat_s + cur_w); + memcpy(nam_u->pat_c, buf_y + cur_w, nam_u->pat_s); + nam_u->pat_c[nam_u->pat_s] = 0; + cur_w += nam_u->pat_s; - if ( req_u == NULL) { - u3h_put(sam_u->req_p, key, u3_nul); - u3z(key); - return; - } - u3_pend_req* old_u = _xmas_get_request(sam_u, nam_u); - u3_pend_req* new_u = req_u; - if ( old_u == NULL ) { - new_u = u3a_calloc(sizeof(u3_pend_req),1); - memcpy(new_u, req_u, sizeof(u3_pend_req)); + if ( 1 == nam_u->met_u.boq_b ) { + CHECK_BOUNDS(cur_w + 1); + nam_u->boq_y = buf_y[cur_w]; + cur_w++; } else { - new_u = old_u; - memcpy(new_u, req_u, sizeof(u3_pend_req)); + nam_u->boq_y = 13; } + c3_y fra_y = nam_u->met_u.fra_y + 1; + CHECK_BOUNDS(cur_w + fra_y) + nam_u->fra_w = 0; + for( int i = 0; i < fra_y; i++ ) { + nam_u->fra_w |= (buf_y[cur_w] << (8*i)); + cur_w++; + } - u3_noun val = u3of(u3_pend_req, new_u); - u3h_put(sam_u->req_p, key, val); - u3z(key); -} - -/* _xmas_packet_timeout(): callback for packet timeout -*/ -static void -_xmas_packet_timeout(uv_timer_t* tim_u) { - u3_pend_req* req_u = (u3_pend_req*)tim_u->data; - u3l_log("%u packet timed out", req_u->las_w); + return cur_w; } -static void _init_gage(u3_gage* gag_u) +static c3_w +_xmas_sift_hop_long(u3_xmas_hop* hop_u, c3_y* buf_y, c3_w len_w) { - gag_u->rto_w = 1000000; - gag_u->rtt_w = 1000000; - gag_u->rtv_w = 1000000; - gag_u->con_w = 0; - gag_u->wnd_w = 1; + c3_w cur_w = 0; + CHECK_BOUNDS(cur_w + 1); + hop_u->len_w = buf_y[cur_w]; + cur_w++; + CHECK_BOUNDS(cur_w + hop_u->len_w); + hop_u->dat_y = c3_calloc(hop_u->len_w); + memcpy(hop_u->dat_y, buf_y + cur_w, hop_u->len_w); + + return cur_w; } -static void _xmas_handle_ack(u3_gage* gag_u, u3_pact_stat* pat_u) + +static c3_w +_xmas_sift_page_pact(u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, c3_w len_w) { + c3_w cur_w = 0; + c3_w nam_w = _xmas_sift_name(&pac_u->nam_u, buf_y, len_w); - gag_u->con_w++; + if( nam_w == 0 ) { + return 0; + } + cur_w += nam_w; - c3_d now_d = _get_now_micros(); - c3_d rtt_d = now_d < pat_u->sen_d ? 0 : now_d - pat_u->sen_d; + u3_xmas_page_meat* mat_u = &pac_u->mat_u; - c3_d err_d = _abs_dif(rtt_d, gag_u->rtt_w); + CHECK_BOUNDS(cur_w + 1); - gag_u->rtt_w = (rtt_d + (gag_u->rtt_w * 7)) >> 3; - gag_u->rtv_w = (err_d + (gag_u->rtv_w * 7)) >> 3; - gag_u->rto_w = _clamp_rto(gag_u->rtt_w + (4*gag_u->rtv_w)); + c3_y met_y = buf_y[cur_w]; + mat_u->met_u.tot_y = (met_y >> 0) & 0x3; + mat_u->met_u.aut_b = (met_y >> 2) & 0x1; + mat_u->met_u.len_y = (met_y >> 3) & 0x1F; + cur_w += 1; - u3_cbic_on_ack(gag_u); -} + c3_y tot_y = pac_u->mat_u.met_u.tot_y + 1; + CHECK_BOUNDS(cur_w + tot_y); + mat_u->tot_w = 0; + for( int i = 0; i < tot_y; i++ ) { + mat_u->tot_w |= (buf_y[cur_w] << (8*i)); + cur_w++; + } -/* _xmas_req_pact_done(): mark packet as done, possibly producing the result -*/ -static u3_weak -_xmas_req_pact_done(u3_xmas* sam_u, u3_xmas_name *nam_u, u3_xmas_page_meat* mat_u, u3_lane* lan_u) -{ - u3_weak ret = u3_none; - c3_d now_d = _get_now_micros(); - u3_pend_req* req_u = _xmas_get_request(sam_u, nam_u); - - if ( NULL == req_u ) { -#ifdef XMAS_DEBUG - u3l_log("xmas: attempting to mark packet done, no request"); -#endif - return u3_none; + mat_u->aul_y = 0; + if ( 1 == mat_u->met_u.aut_b ) { + CHECK_BOUNDS(cur_w + 1); + mat_u->aul_y = buf_y[cur_w]; + cur_w++; + + CHECK_BOUNDS(cur_w + mat_u->aul_y); + mat_u->aut_y = c3_calloc(mat_u->aul_y); + memcpy(mat_u->aut_y, buf_y + cur_w, mat_u->aul_y); + cur_w += mat_u->aul_y; } - c3_w siz_w = (1 << (nam_u->boq_y - 3)); - // First packet received, instantiate request fully - if ( req_u->tot_w == 0 ) { - u3_assert( siz_w == 1024); // boq_y == 13 - req_u->dat_y = c3_calloc(siz_w * mat_u->tot_w); - req_u->wat_u = c3_calloc(sizeof(u3_pact_stat) * mat_u->tot_w); - req_u->tot_w = mat_u->tot_w; - _init_bitset(&req_u->was_u, mat_u->tot_w + 1); - req_u->las_w = 0; - } else { - // handle gauge update - u3_gage* gag_u = _xmas_get_lane(sam_u, nam_u->her_d, lan_u); + c3_y len_y = mat_u->met_u.len_y; + CHECK_BOUNDS(cur_w + len_y); + u3_assert( len_y <= 8 ); + mat_u->len_w = 0; + for ( int i = 0; i < len_y; i++ ) { + mat_u->len_w |= (buf_y[cur_w] << (8*i)); + cur_w++; + } + + CHECK_BOUNDS(cur_w + (c3_w)mat_u->len_w); + mat_u->fra_y = c3_calloc(mat_u->len_w); + memcpy(mat_u->fra_y, buf_y + cur_w, mat_u->len_w); - if ( gag_u == NULL ) { - gag_u = alloca(sizeof(u3_gage)); - _init_gage(gag_u); + switch ( hed_u->nex_y ) { + default: { + u3l_log("xmas: bad hopcount"); + return 0; + } + case HOP_NONE: break; + case HOP_SHORT: { + CHECK_BOUNDS(cur_w + 6); + memcpy(pac_u->sot_u, buf_y + cur_w, 6); + cur_w += 6; + } break; + case HOP_LONG: { + c3_w hop_w = _xmas_sift_hop_long(&pac_u->one_u, buf_y + cur_w, len_w - cur_w); + if( hop_w == 0 ) { + return 0; + } + cur_w += hop_w; + } break; + case HOP_MANY: { + CHECK_BOUNDS(cur_w + 1); + pac_u->man_u.len_w = buf_y[cur_w]; + cur_w++; + + pac_u->man_u.dat_y = c3_calloc(sizeof(u3_xmas_hop*) * pac_u->man_u.len_w); + + for( int i = 0; i < pac_u->man_u.len_w; i++ ) { + pac_u->man_u.dat_y[i] = c3_calloc(sizeof(u3_xmas_hop)); + c3_w hop_w = _xmas_sift_hop_long(pac_u->man_u.dat_y[i], buf_y + cur_w ,len_w - cur_w); + if ( hop_w == 0 ) { + return 0; + } + cur_w += hop_w; + } } - _xmas_handle_ack(gag_u, &req_u->wat_u[nam_u->fra_w]); } - // received duplicate - if ( nam_u->fra_w != 0 && c3n == _has_bitset(&req_u->was_u, nam_u->fra_w) ) { -#ifdef XMAS_DEBUG - u3l_log("duplicate page"); -#endif - return u3_none; - } - _del_bitset(&req_u->was_u, nam_u->fra_w); - req_u->len_w++; + return cur_w; + /*// next hop + memcpy(pac_u->hop_y, buf_y, 6); + cur_w += 6; - memcpy(req_u->dat_y + (siz_w * nam_u->fra_w), mat_u->fra_y, mat_u->len_w); + // path length + pac_u->pat_s = _ames_sift_short(buf_y + cur_w); + cur_w += 2; - // in-order - if (nam_u->fra_w == req_u->las_w ) { - u3l_log("in order %u", nam_u->fra_w); - } else { - // out of order - u3l_log("misordered last: %u have %u", req_u->las_w, nam_u->fra_w); - } + // path contents + pac_u->pat_c = c3_calloc(pac_u->pat_s + 1); + memcpy(pac_u->pat_c, buf_y + cur_w, pac_u->pat_s); + pac_u->pat_c[pac_u->pat_s] = '\0'; + cur_w += pac_u->pat_s + 1; + // total fragments + pac_u->tot_w = _ames_sift_word(buf_y + cur_w); + cur_w += 4; - if ( req_u->len_w == req_u->tot_w ) { - uv_timer_stop(&req_u->tim_u); - //u3dc("rap", 13, u3dc("turn", u3n_slam_on(u3k(sam_u->mop_u.tap), req_u->dat), u3v_wish("tail"))); - //_xmas_put_request(sam_u, nam_u, u3_none); - return u3i_word(req_u->tot_w); - } else { - _xmas_put_request(sam_u, nam_u, req_u); - } - return u3_none; + // Authenticator + memcpy(pac_u->aut_y, buf_y + cur_w, 96); + cur_w += 96; + + c3_w dat_w = 1024; // len_w - cur_w; + pac_u->dat_y = c3_calloc(dat_w); + memcpy(pac_u->dat_y, buf_y + cur_w, dat_w); + */ + return 0; } -/* - * _xmas_req_get_cwnd(): produce current state of packet pump - * - * saves next fragment number and preallocated pact into the passed pointers. - * Will not do so if returning 0 -*/ + static c3_w -_xmas_req_get_cwnd_nex(u3_xmas* sam_u, u3_xmas_name* nam_u, c3_w* nex_w, u3_xmas_pact** nex_u) +_xmas_sift_peek_pact(u3_xmas_peek_pact* pac_u, c3_y* buf_y, c3_w len_w) { - c3_w res_w = 0; - u3_pend_req* req_u = _xmas_get_request(sam_u, nam_u); - if ( NULL == req_u ) { + c3_w siz_w = _xmas_sift_name(&pac_u->nam_u, buf_y, len_w); + if ( siz_w < len_w ) { + u3l_log("xmas: failed to consume entire packet"); + _log_buf(buf_y + siz_w, len_w - siz_w); return 0; } - *nex_w = req_u->tot_w != 0 ? c3_min(req_u->nex_w, req_u->tot_w - 1) : req_u->nex_w; - *nex_u = req_u->pac_u; - - c3_w liv_w = _wyt_bitset(&req_u->was_u); - c3_w lef_w = req_u->tot_w - (req_u->len_w + liv_w); - if ( liv_w < IN_FLIGHT ) { - return c3_min(IN_FLIGHT - liv_w, lef_w); - } - return 0; + /*if (siz_w > len_w ) { + u3l_log("xmas: buffer overrun (FIXME)"); + u3m_bail(c3__foul); + }*/ + return siz_w; } - -/* _xmas_req_pact_sent(): mark packet as sent -** -*/ -static void -_xmas_req_pact_sent(u3_xmas* sam_u, u3_xmas_name* nam_u) +static c3_w +_xmas_sift_poke_pact(u3_xmas_poke_pact* pac_u, c3_y* buf_y, c3_w len_w) { - c3_d now_d = _get_now_micros(); - u3_pend_req* req_u = _xmas_get_request(sam_u, nam_u); + c3_w cur_w = 0; + // Peek portion + pac_u->pek_u = c3_calloc(sizeof(*(pac_u->pek_u))); + cur_w += _xmas_sift_peek_pact(pac_u->pek_u, buf_y, len_w); - // if we already have pending request - if ( NULL != req_u ) { - if( req_u->nex_w == nam_u->fra_w ) { - req_u->nex_w++; - } - // TODO: optional assertions? - req_u->wat_u[nam_u->fra_w] = (u3_pact_stat){now_d, 0, 1 }; - _put_bitset(&req_u->was_u, nam_u->fra_w); - if ( nam_u->fra_w == 1 ) { - req_u->las_w = 1; - } + // Total fragments + pac_u->tot_w = _ames_sift_word(buf_y + cur_w); + cur_w += 4; - } else { - // instantiate pending request - // stack allocating is ok, because _xmas_put_pend will copy it out anyway - req_u = alloca(sizeof(u3_pend_req)); - req_u->pac_u = c3_calloc(sizeof(u3_xmas_pact)); - req_u->pac_u->sam_u = sam_u; - req_u->pac_u->hed_u.typ_y = PACT_PEEK; - memcpy(&req_u->pac_u->pek_u.nam_u, nam_u, sizeof(u3_xmas_name)); - // TODO: handle restart - // u3_assert( nam_u->fra_w == 0 ); - req_u->nex_w = 1; - req_u->tot_w = 0; - req_u->len_w = 0; - req_u->dat_y = NULL; - uv_timer_init(u3L, &req_u->tim_u); - req_u->wat_u = NULL; - req_u->was_u.buf_y = NULL; - req_u->las_w = 0; + // Authenticator + memcpy(pac_u->aut_y, buf_y + cur_w, 96); + cur_w += 96; + + // Datum + memcpy(pac_u->dat_y, buf_y + cur_w, len_w - cur_w); + return cur_w; +} + +static c3_w +_xmas_sift_pact(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w) +{ + u3_xmas_head hed_u; + if( len_w < 8 ) { + u3l_log("xmas: attempted to parse overly short packet of size %u", len_w); } - if ( req_u->las_w != 0 && c3n == _has_bitset(&req_u->was_u, req_u->las_w) ) { - while ( req_u->las_w++ < req_u->tot_w ) { - if ( c3y == _has_bitset(&req_u->was_u, req_u->las_w) ) { - break; - } + _xmas_sift_head(buf_y, &hed_u); + pac_u->hed_u.typ_y = hed_u.typ_y; + c3_w res_w = 0; + buf_y += 8; + len_w -= 8; + switch ( pac_u->hed_u.typ_y ) { + case PACT_PEEK: { + res_w = _xmas_sift_peek_pact(&pac_u->pek_u, buf_y, len_w); + } break; + case PACT_PAGE: { + res_w = _xmas_sift_page_pact(&pac_u->pag_u, &hed_u, buf_y, len_w); + } break; + case PACT_POKE: { + res_w = _xmas_sift_poke_pact(&pac_u->pok_u, buf_y, len_w); + } break; + default: { + u3l_log("xmas: received unknown packet type"); + break; } } - _xmas_put_request(sam_u, nam_u, req_u); + //u3_assert(res_w <= len_w ); + return res_w; } // cut and pasted from ames.c @@ -846,704 +908,605 @@ _ames_etch_word(c3_y buf_y[4], c3_w wod_w) buf_y[3] = (wod_w >> 24) & 0xff; } -/* _ames_czar_port(): udp port for galaxy. -*/ -static c3_s -_ames_czar_port(c3_y imp_y) + +static u3_atom +_dire_etch_ud(c3_d num_d) { - if ( c3n == u3_Host.ops_u.net ) { - return 31337 + imp_y; - } - else { - return 13337 + imp_y; - } + c3_y hun_y[26]; + c3_y* buf_y = u3s_etch_ud_smol(num_d, hun_y); + c3_w dif_w = (c3_p)buf_y - (c3_p)hun_y; + return u3i_bytes(26 - dif_w, buf_y); // XX known-non-null } - -/* _ames_alloc(): libuv buffer allocator. -*/ static void -_ames_alloc(uv_handle_t* had_u, - size_t len_i, - uv_buf_t* buf - ) +_xmas_etch_head(u3_xmas_head* hed_u, c3_y buf_y[8]) { - // we allocate 2K, which gives us plenty of space - // for a single ames packet (max size 1060 bytes) - // - void* ptr_v = c3_malloc(4096); - *buf = uv_buf_init(ptr_v, 4096); -} +#ifdef XMAS_DEBUG + if( c3y == XMAS_DEBUG ) { + if( hed_u->pro_y > 7 ) { + u3l_log("xmas: bad protocol version"); + return; + } + } +#endif + c3_o req_o = c3o((hed_u->typ_y == PACT_PEEK), (hed_u->typ_y == PACT_POKE)); + c3_y siz_y = req_o ? 5 : 7; + c3_w hed_w = (hed_u->nex_y & 0x3 ) << 2 + ^ (hed_u->pro_y & 0x7 ) << 4 + ^ ((hed_u->typ_y & 0x3 ) << 7) + ^ ((hed_u->hop_y & 0x7 ) << 9) + ^ ((hed_u->mug_w & 0xFFFFFF ) << 12); + // XX: we don't expand hopcount if no request. Correct? + // + /*if ( c3y == req_o ) { + hed_w = hed_w ^ ((hed_u->ran_y & 0x3) << 30); + }*/ -static inline c3_s -_ames_sift_short(c3_y buf_y[2]) -{ - return (buf_y[1] << 8 | buf_y[0]); + _ames_etch_word(buf_y, hed_w); + memcpy(buf_y + 4, XMAS_COOKIE, XMAS_COOKIE_LEN); } -static inline c3_w -_ames_sift_word(c3_y buf_y[4]) +static c3_w +_xmas_etch_name(c3_y* buf_y, u3_xmas_name* nam_u) { - return (buf_y[3] << 24 | buf_y[2] << 16 | buf_y[1] << 8 | buf_y[0]); -} +#ifdef XMAS_DEBUG -/* u3_xmas_decode_lane(): deserialize noun to lane; 0.0.0.0:0 if invalid -*/ -static u3_lane -u3_xmas_decode_lane(u3_atom lan) { - u3_lane lan_u; - c3_d lan_d; +#endif + c3_w cur_w = 0; + nam_u->met_u.ran_y = safe_dec(u3r_met(0, u3r_met(4, u3i_chubs(2, nam_u->her_d)))); + nam_u->met_u.rif_y = safe_dec(u3r_met(3, u3i_word(nam_u->rif_w))); + nam_u->met_u.fra_y = safe_dec(u3r_met(3, u3i_word(nam_u->fra_w))); + nam_u->met_u.boq_b = nam_u->boq_y == 13 ? 0 : 1; + nam_u->met_u.pat_b = nam_u->pat_s > 0xFF ? 1 : 0; + + // TODO: double check reserved bits + c3_y met_y = (nam_u->met_u.ran_y & 0x3) << 0 + ^ (nam_u->met_u.rif_y & 0x3) << 2 + ^ (nam_u->met_u.pat_b & 0x1) << 4 + ^ (nam_u->met_u.boq_b & 0x1) << 5 + ^ (nam_u->met_u.fra_y & 0x3) << 6; - if ( c3n == u3r_safe_chub(lan, &lan_d) || (lan_d >> 48) != 0 ) { - return (u3_lane){0, 0}; + buf_y[cur_w] = met_y; + + + u3_xmas_name_meta met_u = nam_u->met_u; + //ship + cur_w++; + c3_y her_y = 1 << (met_u.ran_y + 1); + _ames_ship_of_chubs(nam_u->her_d, her_y, buf_y + cur_w); + cur_w += her_y; + + // rift + c3_y rif_y = met_u.rif_y + 1; + for ( int i = 0; i < rif_y; i++) { + buf_y[cur_w] = (nam_u->rif_w >> (8*i)) & 0xff; + cur_w++; } - u3z(lan); + // path length + c3_y pat_y = 1 >> met_u.pat_b; + for ( int i = 0; i < pat_y; i++ ) { + buf_y[cur_w] = (nam_u->pat_s >> (8*i)) & 0xff; + cur_w++; + } - lan_u.pip_w = (c3_w)lan_d; - lan_u.por_s = (c3_s)(lan_d >> 32); - // convert incoming localhost to outgoing localhost - // - lan_u.pip_w = ( 0 == lan_u.pip_w ) ? 0x7f000001 : lan_u.pip_w; + // path + memcpy(buf_y + cur_w, nam_u->pat_c, nam_u->pat_s); + cur_w += nam_u->pat_s; - return lan_u; -} -/* _ames_chub_bytes(): c3_y[8] to c3_d -** XX factor out, deduplicate with other conversions -*/ -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; -} + // possible bloq size + if ( 1 == met_u.boq_b ) { + buf_y[cur_w] = nam_u->boq_y; + cur_w++; + } -/* _ames_ship_to_chubs(): pack [len_y] bytes into c3_d[2] -*/ -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)); + c3_y fra_y = met_u.fra_y + 1; + for( int i = 0; i < fra_y; i++ ) { + buf_y[cur_w] = (nam_u->fra_w >> (8*i)) & 0xff; + cur_w++; + } - sip_d[0] = _ames_chub_bytes(sip_y); - sip_d[1] = _ames_chub_bytes(sip_y + 8); + return cur_w; + //_ames_etch_short(buf_y + cur_w, &met_s); } -/* _ames_chub_bytes(): c3_d to c3_y[8] -** XX factor out, deduplicate with other conversions -*/ -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; -} -static inline void -_ames_ship_of_chubs(c3_d sip_d[2], c3_y len_y, c3_y* buf_y) + +static c3_w +_xmas_etch_page_pact(c3_y* buf_y, u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u) { - c3_y sip_y[16] = {0}; + c3_w cur_w = 0; + { + c3_w siz_w = _xmas_etch_name(buf_y + cur_w, &pac_u->nam_u); + if ( siz_w == 0 ) { + return 0; + } + cur_w += siz_w; + } - _ames_bytes_chub(sip_y, sip_d[0]); - _ames_bytes_chub(sip_y + 8, sip_d[1]); + u3_xmas_page_meat* mat_u = &pac_u->mat_u; + + // metabyte TODO fix leak + mat_u->met_u.tot_y = safe_dec(u3r_met(3, u3i_word(mat_u->tot_w))); + mat_u->met_u.aut_b = (mat_u->aul_y != 0) & 0x1; + mat_u->met_u.len_y = u3r_met(3, u3i_chub(mat_u->len_w)); + c3_y met_y = (mat_u->met_u.tot_y & 0x3 ) << 0 + ^ (mat_u->met_u.aut_b & 0x1 ) << 2 + ^ (mat_u->met_u.len_y & 0x1F ) << 3; - memcpy(buf_y, sip_y, c3_min(16, len_y)); -} + buf_y[cur_w] = met_y; + cur_w++; + + c3_y tot_y = mat_u->met_u.tot_y + 1; + for (int i = 0; i < tot_y; i++ ) { + buf_y[cur_w] = (mat_u->tot_w >> (8 * i)) & 0xFF; + cur_w++; + } + if ( mat_u->met_u.aut_b == 1 ) { + buf_y[cur_w] = mat_u->aul_y; + cur_w++; + memcpy(buf_y + cur_w, mat_u->aut_y, mat_u->aul_y); + cur_w += mat_u->aul_y; + } -/* u3_xmas_lane_to_chub(): serialize lane to double-word -*/ -static c3_d -u3_xmas_lane_to_chub(u3_lane lan) { - return ((c3_d)lan.por_s << 32) ^ (c3_d)lan.pip_w; + c3_y len_y = mat_u->met_u.len_y; + for( int i = 0; i < len_y; i++ ) { + buf_y[cur_w] = (mat_u->len_w >> (8 * i)) & 0xFF; + cur_w++; + } + + memcpy(buf_y + cur_w, mat_u->fra_y, mat_u->len_w); + cur_w += mat_u->len_w; + // TODO: hopcount + return cur_w; } +static c3_w +_xmas_etch_poke_pact(c3_y* buf_y, u3_xmas_poke_pact* pac_u, u3_xmas_head* hed_u) +{ + c3_w cur_w = 0; + // total + _ames_etch_word(buf_y + cur_w, pac_u->tot_w); + cur_w += 4; + // auth + memcpy(buf_y + cur_w, pac_u->aut_y, 96); + cur_w += 96; -// END plagariasm zone -// -// -// -// -// -// -static void _xmas_free_seal(u3_seal* sel_u) -{ - //c3_free(sel_u->buf_y); - //c3_free(sel_u); -} + // day + memcpy(buf_y + cur_w, pac_u->dat_y, PACT_SIZE - cur_w); -static u3_noun _xmas_get_now() { - struct timeval tim_u; - gettimeofday(&tim_u, 0); - u3_noun res = u3_time_in_tv(&tim_u); - return res; + return PACT_SIZE; } +static c3_w +_xmas_etch_pact(c3_y* buf_y, u3_xmas_pact* pac_u, u3_noun her) +{ + c3_w cur_w = 0; + u3_xmas_head* hed_u = &pac_u->hed_u; + _xmas_etch_head(hed_u, buf_y + cur_w); + cur_w += 8; + c3_w siz_w = 0; -// refcounted -static c3_o -_xmas_rout_pact(u3_xmas_pact* pac_u, u3_noun lan) { - u3_noun tag, val; - u3x_cell(lan, &tag, &val); - u3_assert( (c3y == tag) || (c3n == tag) ); - c3_o suc_o = c3y; - if ( c3y == tag ) { - u3_assert( c3y == u3a_is_cat(val) ); - u3_assert( val < 256 ); - pac_u->rut_u.typ_y = ROUT_GALAXY; - pac_u->rut_u.imp_y = val; - } else { - u3_lane lan_u = u3_xmas_decode_lane(u3k(val)); - // if in local-only mode, don't send remote packets - // - if ( (c3n == u3_Host.ops_u.net) && (0x7f000001 != lan_u.pip_w) ) { - suc_o = c3n; - } - // if the lane is uninterpretable, silently drop the packet - // - else if ( 0 == lan_u.por_s ) { - if ( u3C.wag_w & u3o_verbose ) { - u3l_log("ames: inscrutable lane"); + switch ( pac_u->hed_u.typ_y ) { + case PACT_POKE: { + c3_w siz_w = _xmas_etch_poke_pact(buf_y + cur_w, &pac_u->pok_u, hed_u); + if ( siz_w == 0 ) { + return 0; } - suc_o = c3n; - } else { - pac_u->rut_u.typ_y = ROUT_OTHER; - pac_u->rut_u.lan_u = lan_u; + cur_w += siz_w; + } break; + case PACT_PEEK: { + siz_w = _xmas_etch_name(buf_y + cur_w, &pac_u->pek_u.nam_u); + if ( siz_w == 0 ) { + return 0; + } + cur_w += siz_w; + } break; + case PACT_PAGE: { + siz_w = _xmas_etch_page_pact(buf_y + cur_w, &pac_u->pag_u, hed_u); + if ( siz_w == 0 ) { + return 0; + } + cur_w += siz_w; + } break; + + default: { + u3l_log("bad pact type");//u3m_bail(c3__bail); } } - u3z(lan); - return suc_o; + return cur_w; } - -static void _xmas_pact_free(u3_xmas_pact* pac_u) { - // TODO: i'm lazy +/* _xmas_request_key(): produce key for request hashtable sam_u->req_p from nam_u +*/ +u3_noun _xmas_request_key(u3_xmas_name* nam_u) +{ + u3_noun pax = _xmas_encode_path(nam_u->pat_s, (c3_y*)nam_u->pat_c); + u3_noun res = u3nt(u3i_chubs(2, nam_u->her_d), u3i_word(nam_u->rif_w), pax); + return res; } -static c3_o -_xmas_sift_head(c3_y buf_y[8], u3_xmas_head* hed_u) +static void _init_gage(u3_gage* gag_u) { - if ( memcmp(buf_y + 4, &XMAS_COOKIE, XMAS_COOKIE_LEN) ) { - return c3n; - - } - c3_w hed_w = _ames_sift_word(buf_y); - - hed_u->nex_y = (hed_w >> 2) & 0x3; - hed_u->pro_y = (hed_w >> 4) & 0x7; - hed_u->typ_y = (hed_w >> 7) & 0x3; - hed_u->hop_y = (hed_w >> 9) & 0x7; - hed_u->mug_w = (hed_w >> 12) & 0xFFFFFF; - - return c3y; + gag_u->rto_w = 1000000; + gag_u->rtt_w = 1000000; + gag_u->rtv_w = 1000000; + gag_u->con_w = 0; + gag_u->wnd_w = 1; +} - /*if(c3o((hed_u->typ_y == PACT_PEEK), (hed_u->typ_y == PACT_POKE))) { - hed_u->ran_y = (hed_w >> 30) & 0x3; - }*/ +/* u3_xmas_encode_lane(): serialize lane to noun +*/ +static u3_atom +u3_xmas_encode_lane(u3_lane lan_u) { + // [%if ip=@ port=@] + return u3nt(c3__if, u3i_word(lan_u.pip_w), lan_u.por_s); } -static c3_w -_xmas_sift_name(u3_xmas_name* nam_u, c3_y* buf_y, c3_w len_w) +static u3_noun +_xmas_lane_key(c3_d her_d[2], u3_lane* lan_u) { -#ifdef XMAS_DEBUG - //u3l_log("xmas: sifting name %i", len_w); -#endif - - c3_w cur_w = 0; - CHECK_BOUNDS(cur_w + 1); - c3_y met_y = buf_y[cur_w]; - nam_u->met_u.ran_y = (met_y >> 0) & 0x3; - nam_u->met_u.rif_y = (met_y >> 2) & 0x3; - nam_u->met_u.pat_b = (met_y >> 4) & 0x1; - nam_u->met_u.boq_b = (met_y >> 5) & 0x1; - nam_u->met_u.fra_y = (met_y >> 6) & 0x3; - cur_w += 1; - - c3_y her_y = 2 << nam_u->met_u.ran_y; - CHECK_BOUNDS(cur_w + her_y) - _ames_ship_to_chubs(nam_u->her_d, her_y, buf_y + cur_w); - cur_w += her_y; - - c3_y rif_y = nam_u->met_u.rif_y + 1; - nam_u->rif_w = 0; - CHECK_BOUNDS(cur_w + rif_y) - for( int i = 0; i < rif_y; i++ ) { - nam_u->rif_w |= (buf_y[cur_w] << (8*i)); - cur_w++; - } - - c3_y pat_y = 1 << nam_u->met_u.pat_b; - CHECK_BOUNDS(cur_w + pat_y) - nam_u->pat_s = 0; - for ( int i = 0; i < pat_y; i++ ) { - nam_u->pat_s |= (buf_y[cur_w] << (8*i)); - cur_w++; - } - - nam_u->pat_c = c3_calloc(nam_u->pat_s + 1); // unix string for ease of manipulation - CHECK_BOUNDS(nam_u->pat_s + cur_w); - memcpy(nam_u->pat_c, buf_y + cur_w, nam_u->pat_s); - nam_u->pat_c[nam_u->pat_s] = 0; - cur_w += nam_u->pat_s; + return u3nc(u3i_chubs(2,her_d), u3_xmas_encode_lane(*lan_u)); +} - if ( 1 == nam_u->met_u.boq_b ) { - CHECK_BOUNDS(cur_w + 1); - nam_u->boq_y = buf_y[cur_w]; - cur_w++; - } else { - nam_u->boq_y = 13; - } +/* RETAIN +*/ +static u3_gage* +_xmas_get_lane_raw(u3_xmas* sam_u, u3_noun key) +{ + u3_gage* ret_u = NULL; + u3_weak res = u3h_git(sam_u->lan_p, key); - c3_y fra_y = nam_u->met_u.fra_y + 1; - CHECK_BOUNDS(cur_w + fra_y) - nam_u->fra_w = 0; - for( int i = 0; i < fra_y; i++ ) { - nam_u->fra_w |= (buf_y[cur_w] << (8*i)); - cur_w++; + if ( res != u3_none && res != u3_nul ) { + ret_u = u3to(u3_gage, res); } - return cur_w; + return ret_u; } -static c3_w -_xmas_sift_hop_long(u3_xmas_hop* hop_u, c3_y* buf_y, c3_w len_w) -{ - c3_w cur_w = 0; - CHECK_BOUNDS(cur_w + 1); - hop_u->len_w = buf_y[cur_w]; - cur_w++; - CHECK_BOUNDS(cur_w + hop_u->len_w); - hop_u->dat_y = c3_calloc(hop_u->len_w); - memcpy(hop_u->dat_y, buf_y + cur_w, hop_u->len_w); - - return cur_w; +/* _xmas_get_lane(): get lane +*/ +static u3_gage* +_xmas_get_lane(u3_xmas* sam_u, c3_d her_d[2], u3_lane* lan_u) { + u3_noun key =_xmas_lane_key(her_d, lan_u); + u3_gage* ret_u = _xmas_get_lane_raw(sam_u, key); + u3z(key); + return ret_u; } - -static c3_w -_xmas_sift_page_pact(u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, c3_w len_w) +/* _xmas_put_lane(): put lane state in state + * + * uses same copying trick as _xmas_put_request() +*/ +static void +_xmas_put_lane(u3_xmas* sam_u, c3_d her_d[2], u3_lane* lan_u, u3_gage* gag_u) { - c3_w cur_w = 0; - c3_w nam_w = _xmas_sift_name(&pac_u->nam_u, buf_y, len_w); + u3_noun key = _xmas_lane_key(her_d, lan_u); + u3_gage* old_u = _xmas_get_lane_raw(sam_u, key); + u3_gage* new_u = gag_u; - if( nam_w == 0 ) { - return 0; + if ( old_u == NULL ) { + new_u = u3a_calloc(sizeof(u3_gage),1); + memcpy(new_u, gag_u, sizeof(u3_gage)); + } else { + new_u = old_u; + memcpy(new_u, gag_u, sizeof(u3_gage)); } - cur_w += nam_w; - - u3_xmas_page_meat* mat_u = &pac_u->mat_u; - - CHECK_BOUNDS(cur_w + 1); - c3_y met_y = buf_y[cur_w]; - mat_u->met_u.tot_y = (met_y >> 0) & 0x3; - mat_u->met_u.aut_b = (met_y >> 2) & 0x1; - mat_u->met_u.len_y = (met_y >> 3) & 0x1F; - cur_w += 1; - - c3_y tot_y = pac_u->mat_u.met_u.tot_y + 1; - CHECK_BOUNDS(cur_w + tot_y); - mat_u->tot_w = 0; - for( int i = 0; i < tot_y; i++ ) { - mat_u->tot_w |= (buf_y[cur_w] << (8*i)); - cur_w++; - } + u3_noun val = u3of(u3_gage, new_u); + u3h_put(sam_u->lan_p, key, val); + u3z(key); +} - mat_u->aul_y = 0; - if ( 1 == mat_u->met_u.aut_b ) { - CHECK_BOUNDS(cur_w + 1); - mat_u->aul_y = buf_y[cur_w]; - cur_w++; +/* _xmas_get_request(): produce pending request state for nam_u + * + * produces a NULL pointer if no pending request exists +*/ +static u3_pend_req* +_xmas_get_request(u3_xmas* sam_u, u3_xmas_name* nam_u) { + u3_pend_req* ret_u = NULL; + u3_noun key = _xmas_request_key(nam_u); - CHECK_BOUNDS(cur_w + mat_u->aul_y); - mat_u->aut_y = c3_calloc(mat_u->aul_y); - memcpy(mat_u->aut_y, buf_y + cur_w, mat_u->aul_y); - cur_w += mat_u->aul_y; + u3_weak res = u3h_git(sam_u->req_p, key); + if ( res != u3_none && res != u3_nul ) { + ret_u = u3to(u3_pend_req, res); } - - c3_y len_y = mat_u->met_u.len_y; - CHECK_BOUNDS(cur_w + len_y); - u3_assert( len_y <= 8 ); - mat_u->len_w = 0; - for ( int i = 0; i < len_y; i++ ) { - mat_u->len_w |= (buf_y[cur_w] << (8*i)); - cur_w++; + if ( ret_u != NULL && ret_u->tot_w != 0 && ret_u->tot_w == ret_u->len_w ) { + ret_u = NULL; } - CHECK_BOUNDS(cur_w + (c3_w)mat_u->len_w); - mat_u->fra_y = c3_calloc(mat_u->len_w); - memcpy(mat_u->fra_y, buf_y + cur_w, mat_u->len_w); - - switch ( hed_u->nex_y ) { - default: { - u3l_log("xmas: bad hopcount"); - return 0; - } - case HOP_NONE: break; - case HOP_SHORT: { - CHECK_BOUNDS(cur_w + 6); - memcpy(pac_u->sot_u, buf_y + cur_w, 6); - cur_w += 6; - } break; - case HOP_LONG: { - c3_w hop_w = _xmas_sift_hop_long(&pac_u->one_u, buf_y + cur_w, len_w - cur_w); - if( hop_w == 0 ) { - return 0; - } - cur_w += hop_w; - } break; - case HOP_MANY: { - CHECK_BOUNDS(cur_w + 1); - pac_u->man_u.len_w = buf_y[cur_w]; - cur_w++; + u3z(key); + return ret_u; +} - pac_u->man_u.dat_y = c3_calloc(sizeof(u3_xmas_hop*) * pac_u->man_u.len_w); +/* _xmas_put_request(): save new pending request state for nam_u + * + * the memory in the hashtable is allocated once in the lifecycle of the + * request. req_u will be copied into the hashtable memory, and so can be + * immediately freed + * +*/ +static void +_xmas_put_request(u3_xmas* sam_u, u3_xmas_name* nam_u, u3_pend_req* req_u) { + u3_noun key = _xmas_request_key(nam_u); - for( int i = 0; i < pac_u->man_u.len_w; i++ ) { - pac_u->man_u.dat_y[i] = c3_calloc(sizeof(u3_xmas_hop)); - c3_w hop_w = _xmas_sift_hop_long(pac_u->man_u.dat_y[i], buf_y + cur_w ,len_w - cur_w); - if ( hop_w == 0 ) { - return 0; - } - cur_w += hop_w; - } - } + if ( req_u == NULL) { + u3h_put(sam_u->req_p, key, u3_nul); + u3z(key); + return; + } + u3_pend_req* old_u = _xmas_get_request(sam_u, nam_u); + u3_pend_req* new_u = req_u; + if ( old_u == NULL ) { + new_u = u3a_calloc(sizeof(u3_pend_req),1); + memcpy(new_u, req_u, sizeof(u3_pend_req)); + } else { + new_u = old_u; + memcpy(new_u, req_u, sizeof(u3_pend_req)); } - return cur_w; - - /*// next hop - memcpy(pac_u->hop_y, buf_y, 6); - cur_w += 6; - - // path length - pac_u->pat_s = _ames_sift_short(buf_y + cur_w); - cur_w += 2; - - // path contents - pac_u->pat_c = c3_calloc(pac_u->pat_s + 1); - memcpy(pac_u->pat_c, buf_y + cur_w, pac_u->pat_s); - pac_u->pat_c[pac_u->pat_s] = '\0'; - cur_w += pac_u->pat_s + 1; - - // total fragments - pac_u->tot_w = _ames_sift_word(buf_y + cur_w); - cur_w += 4; - - // Authenticator - memcpy(pac_u->aut_y, buf_y + cur_w, 96); - cur_w += 96; - - c3_w dat_w = 1024; // len_w - cur_w; - pac_u->dat_y = c3_calloc(dat_w); - memcpy(pac_u->dat_y, buf_y + cur_w, dat_w); - */ - return 0; -} + u3_noun val = u3of(u3_pend_req, new_u); + u3h_put(sam_u->req_p, key, val); + u3z(key); +} -static c3_w -_xmas_sift_peek_pact(u3_xmas_peek_pact* pac_u, c3_y* buf_y, c3_w len_w) +/* _xmas_packet_timeout(): callback for packet timeout +*/ +static void +_xmas_packet_timeout(uv_timer_t* tim_u) { + u3_pend_req* req_u = (u3_pend_req*)tim_u->data; + u3l_log("%u packet timed out", req_u->old_w); +} +static void _xmas_handle_ack(u3_gage* gag_u, u3_pact_stat* pat_u) { - c3_w siz_w = _xmas_sift_name(&pac_u->nam_u, buf_y, len_w); - if ( siz_w < len_w ) { - u3l_log("xmas: failed to consume entire packet"); - _log_buf(buf_y + siz_w, len_w - siz_w); - return 0; - } - /*if (siz_w > len_w ) { - u3l_log("xmas: buffer overrun (FIXME)"); - u3m_bail(c3__foul); - }*/ - return siz_w; -} + gag_u->con_w++; -static c3_w -_xmas_sift_poke_pact(u3_xmas_poke_pact* pac_u, c3_y* buf_y, c3_w len_w) -{ - c3_w cur_w = 0; - // Peek portion - pac_u->pek_u = c3_calloc(sizeof(*(pac_u->pek_u))); - cur_w += _xmas_sift_peek_pact(pac_u->pek_u, buf_y, len_w); + c3_d now_d = _get_now_micros(); + c3_d rtt_d = now_d < pat_u->sen_d ? 0 : now_d - pat_u->sen_d; - // Total fragments - pac_u->tot_w = _ames_sift_word(buf_y + cur_w); - cur_w += 4; + c3_d err_d = _abs_dif(rtt_d, gag_u->rtt_w); - // Authenticator - memcpy(pac_u->aut_y, buf_y + cur_w, 96); - cur_w += 96; + gag_u->rtt_w = (rtt_d + (gag_u->rtt_w * 7)) >> 3; + gag_u->rtv_w = (err_d + (gag_u->rtv_w * 7)) >> 3; + gag_u->rto_w = _clamp_rto(gag_u->rtt_w + (4*gag_u->rtv_w)); - // Datum - memcpy(pac_u->dat_y, buf_y + cur_w, len_w - cur_w); - return cur_w; + //u3_cbic_on_ack(gag_u); } +/* + * _xmas_req_get_cwnd(): produce current state of packet pump + * + * saves next fragment number and preallocated pact into the passed pointers. + * Will not do so if returning 0 +*/ static c3_w -_xmas_sift_pact(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w) +_xmas_req_get_cwnd_nex(u3_xmas* sam_u, u3_xmas_name* nam_u, c3_w* nex_w, u3_xmas_pact** nex_u) { - u3_xmas_head hed_u; - if( len_w < 8 ) { - u3l_log("xmas: attempted to parse overly short packet of size %u", len_w); + c3_w res_w = 0; + u3_pend_req* req_u = _xmas_get_request(sam_u, nam_u); + if ( NULL == req_u ) { + return 0; } + *nex_u = req_u->pac_u; + if ( req_u->tot_w == 0 ) { + u3l_log("shouldn't happen"); + *nex_w = 0; + return 1; - _xmas_sift_head(buf_y, &hed_u); - pac_u->hed_u.typ_y = hed_u.typ_y; - c3_w res_w = 0; - buf_y += 8; - len_w -= 8; - switch ( pac_u->hed_u.typ_y ) { - case PACT_PEEK: { - res_w = _xmas_sift_peek_pact(&pac_u->pek_u, buf_y, len_w); - } break; - case PACT_PAGE: { - res_w = _xmas_sift_page_pact(&pac_u->pag_u, &hed_u, buf_y, len_w); - } break; - case PACT_POKE: { - res_w = _xmas_sift_poke_pact(&pac_u->pok_u, buf_y, len_w); - } break; - default: { - u3l_log("xmas: received unknown packet type"); - break; - } } - //u3_assert(res_w <= len_w ); - return res_w; -} + *nex_w = req_u->nex_w; + c3_w liv_w = _wyt_bitset(&req_u->was_u); -static u3_atom -_dire_etch_ud(c3_d num_d) -{ - c3_y hun_y[26]; - c3_y* buf_y = u3s_etch_ud_smol(num_d, hun_y); - c3_w dif_w = (c3_p)buf_y - (c3_p)hun_y; - return u3i_bytes(26 - dif_w, buf_y); // XX known-non-null + c3_w rem_w = req_u->tot_w - req_u->nex_w + 1; + return c3_min(rem_w, IN_FLIGHT - liv_w); } +/* _xmas_req_pact_sent(): mark packet as sent +** +*/ static void -_xmas_etch_head(u3_xmas_head* hed_u, c3_y buf_y[8]) +_xmas_req_pact_resent(u3_xmas* sam_u, u3_xmas_name* nam_u) { -#ifdef XMAS_DEBUG - if( c3y == XMAS_DEBUG ) { - if( hed_u->pro_y > 7 ) { - u3l_log("xmas: bad protocol version"); - return; - } - } -#endif - c3_o req_o = c3o((hed_u->typ_y == PACT_PEEK), (hed_u->typ_y == PACT_POKE)); - c3_y siz_y = req_o ? 5 : 7; - c3_w hed_w = (hed_u->nex_y & 0x3 ) << 2 - ^ (hed_u->pro_y & 0x7 ) << 4 - ^ ((hed_u->typ_y & 0x3 ) << 7) - ^ ((hed_u->hop_y & 0x7 ) << 9) - ^ ((hed_u->mug_w & 0xFFFFFF ) << 12); - // XX: we don't expand hopcount if no request. Correct? - // - /*if ( c3y == req_o ) { - hed_w = hed_w ^ ((hed_u->ran_y & 0x3) << 30); - }*/ + c3_d now_d = _get_now_micros(); + u3_pend_req* req_u = _xmas_get_request(sam_u, nam_u); + // if we already have pending request + if ( NULL == req_u ) { + return; - _ames_etch_word(buf_y, hed_w); - memcpy(buf_y + 4, XMAS_COOKIE, XMAS_COOKIE_LEN); + } + req_u->wat_u[nam_u->fra_w].sen_d = now_d; + req_u->wat_u[nam_u->fra_w].tie_y++; } -static c3_w -_xmas_etch_name(c3_y* buf_y, u3_xmas_name* nam_u) +/* _xmas_req_pact_sent(): mark packet as sent +** +*/ +static void +_xmas_req_pact_sent(u3_xmas* sam_u, u3_xmas_name* nam_u) { -#ifdef XMAS_DEBUG - -#endif - c3_w cur_w = 0; - nam_u->met_u.ran_y = safe_dec(u3r_met(0, u3r_met(4, u3i_chubs(2, nam_u->her_d)))); - nam_u->met_u.rif_y = safe_dec(u3r_met(3, u3i_word(nam_u->rif_w))); - nam_u->met_u.fra_y = safe_dec(u3r_met(3, u3i_word(nam_u->fra_w))); - nam_u->met_u.boq_b = nam_u->boq_y == 13 ? 0 : 1; - nam_u->met_u.pat_b = nam_u->pat_s > 0xFF ? 1 : 0; - - // TODO: double check reserved bits - c3_y met_y = (nam_u->met_u.ran_y & 0x3) << 0 - ^ (nam_u->met_u.rif_y & 0x3) << 2 - ^ (nam_u->met_u.pat_b & 0x1) << 4 - ^ (nam_u->met_u.boq_b & 0x1) << 5 - ^ (nam_u->met_u.fra_y & 0x3) << 6; - - buf_y[cur_w] = met_y; - + c3_d now_d = _get_now_micros(); + u3_pend_req* req_u = _xmas_get_request(sam_u, nam_u); - u3_xmas_name_meta met_u = nam_u->met_u; - //ship - cur_w++; - c3_y her_y = 1 << (met_u.ran_y + 1); - _ames_ship_of_chubs(nam_u->her_d, her_y, buf_y + cur_w); - cur_w += her_y; + // if we already have pending request + if ( NULL != req_u ) { + if( req_u->nex_w == nam_u->fra_w ) { + req_u->nex_w++; + } + // TODO: optional assertions? + req_u->wat_u[nam_u->fra_w] = (u3_pact_stat){now_d, 0, 1, 0 }; + _put_bitset(&req_u->was_u, nam_u->fra_w); + if ( nam_u->fra_w == 1 ) { + req_u->lef_w = 1; + } - // rift - c3_y rif_y = met_u.rif_y + 1; - for ( int i = 0; i < rif_y; i++) { - buf_y[cur_w] = (nam_u->rif_w >> (8*i)) & 0xff; - cur_w++; + } else { + // instantiate pending request + // stack allocating is ok, because _xmas_put_pend will copy it out anyway + req_u = alloca(sizeof(u3_pend_req)); + req_u->pac_u = c3_calloc(sizeof(u3_xmas_pact)); + req_u->pac_u->sam_u = sam_u; + req_u->pac_u->hed_u.typ_y = PACT_PEEK; + req_u->gag_u = c3_calloc(sizeof(u3_gage)); + _init_gage(req_u->gag_u); + memcpy(&req_u->pac_u->pek_u.nam_u, nam_u, sizeof(u3_xmas_name)); + // TODO: handle restart + // u3_assert( nam_u->fra_w == 0 ); + req_u->nex_w = 1; + req_u->tot_w = 0; + req_u->len_w = 0; + req_u->dat_y = NULL; + uv_timer_init(u3L, &req_u->tim_u); + req_u->wat_u = NULL; + req_u->was_u.buf_y = NULL; + req_u->lef_w = 0; + req_u->old_w = 1; } - // path length - c3_y pat_y = 1 >> met_u.pat_b; - for ( int i = 0; i < pat_y; i++ ) { - buf_y[cur_w] = (nam_u->pat_s >> (8*i)) & 0xff; - cur_w++; + if ( req_u->lef_w != 0 && c3n == _has_bitset(&req_u->was_u, req_u->lef_w) ) { + while ( req_u->lef_w++ < req_u->tot_w ) { + if ( c3y == _has_bitset(&req_u->was_u, req_u->lef_w) ) { + break; + } + } } - - // path - memcpy(buf_y + cur_w, nam_u->pat_c, nam_u->pat_s); - cur_w += nam_u->pat_s; + _xmas_put_request(sam_u, nam_u, req_u); +} - // possible bloq size - if ( 1 == met_u.boq_b ) { - buf_y[cur_w] = nam_u->boq_y; - cur_w++; +/* _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; } - - c3_y fra_y = met_u.fra_y + 1; - for( int i = 0; i < fra_y; i++ ) { - buf_y[cur_w] = (nam_u->fra_w >> (8*i)) & 0xff; - cur_w++; + else { + return 13337 + imp_y; } - - return cur_w; - //_ames_etch_short(buf_y + cur_w, &met_s); } +/* _ames_alloc(): libuv buffer allocator. +*/ +static void +_ames_alloc(uv_handle_t* had_u, + size_t len_i, + uv_buf_t* buf + ) +{ + // we allocate 2K, which gives us plenty of space + // for a single ames packet (max size 1060 bytes) + // + void* ptr_v = c3_malloc(4096); + *buf = uv_buf_init(ptr_v, 4096); +} -static c3_w -_xmas_etch_page_pact(c3_y* buf_y, u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u) -{ - c3_w cur_w = 0; - { - c3_w siz_w = _xmas_etch_name(buf_y + cur_w, &pac_u->nam_u); - if ( siz_w == 0 ) { - return 0; - } - cur_w += siz_w; - } - - u3_xmas_page_meat* mat_u = &pac_u->mat_u; - - // metabyte TODO fix leak - mat_u->met_u.tot_y = safe_dec(u3r_met(3, u3i_word(mat_u->tot_w))); - mat_u->met_u.aut_b = (mat_u->aul_y != 0) & 0x1; - mat_u->met_u.len_y = u3r_met(3, u3i_chub(mat_u->len_w)); - c3_y met_y = (mat_u->met_u.tot_y & 0x3 ) << 0 - ^ (mat_u->met_u.aut_b & 0x1 ) << 2 - ^ (mat_u->met_u.len_y & 0x1F ) << 3; - - buf_y[cur_w] = met_y; - cur_w++; +/* u3_xmas_decode_lane(): deserialize noun to lane; 0.0.0.0:0 if invalid +*/ +static u3_lane +u3_xmas_decode_lane(u3_atom lan) { + u3_lane lan_u; + c3_d lan_d; - c3_y tot_y = mat_u->met_u.tot_y + 1; - for (int i = 0; i < tot_y; i++ ) { - buf_y[cur_w] = (mat_u->tot_w >> (8 * i)) & 0xFF; - cur_w++; + if ( c3n == u3r_safe_chub(lan, &lan_d) || (lan_d >> 48) != 0 ) { + return (u3_lane){0, 0}; } - if ( mat_u->met_u.aut_b == 1 ) { - buf_y[cur_w] = mat_u->aul_y; - cur_w++; - memcpy(buf_y + cur_w, mat_u->aut_y, mat_u->aul_y); - cur_w += mat_u->aul_y; - } + u3z(lan); - c3_y len_y = mat_u->met_u.len_y; - for( int i = 0; i < len_y; i++ ) { - buf_y[cur_w] = (mat_u->len_w >> (8 * i)) & 0xFF; - cur_w++; - } + lan_u.pip_w = (c3_w)lan_d; + lan_u.por_s = (c3_s)(lan_d >> 32); + // convert incoming localhost to outgoing localhost + // + lan_u.pip_w = ( 0 == lan_u.pip_w ) ? 0x7f000001 : lan_u.pip_w; - memcpy(buf_y + cur_w, mat_u->fra_y, mat_u->len_w); - cur_w += mat_u->len_w; - // TODO: hopcount - return cur_w; + return lan_u; } -static c3_w -_xmas_etch_poke_pact(c3_y* buf_y, u3_xmas_poke_pact* pac_u, u3_xmas_head* hed_u) -{ - c3_w cur_w = 0; +/* u3_xmas_lane_to_chub(): serialize lane to double-word +*/ +static c3_d +u3_xmas_lane_to_chub(u3_lane lan) { + return ((c3_d)lan.por_s << 32) ^ (c3_d)lan.pip_w; +} - // total - _ames_etch_word(buf_y + cur_w, pac_u->tot_w); - cur_w += 4; - // auth - memcpy(buf_y + cur_w, pac_u->aut_y, 96); - cur_w += 96; - // day - memcpy(buf_y + cur_w, pac_u->dat_y, PACT_SIZE - cur_w); - return PACT_SIZE; +// END plagariasm zone +// +// +// +// +// +// +static void _xmas_free_seal(u3_seal* sel_u) +{ + //c3_free(sel_u->buf_y); + //c3_free(sel_u); } -static c3_w -_xmas_etch_pact(c3_y* buf_y, u3_xmas_pact* pac_u, u3_noun her) -{ - c3_w cur_w = 0; - u3_xmas_head* hed_u = &pac_u->hed_u; - _xmas_etch_head(hed_u, buf_y + cur_w); - cur_w += 8; - c3_w siz_w = 0; +static u3_noun _xmas_get_now() { + struct timeval tim_u; + gettimeofday(&tim_u, 0); + u3_noun res = u3_time_in_tv(&tim_u); + return res; +} - switch ( pac_u->hed_u.typ_y ) { - case PACT_POKE: { - c3_w siz_w = _xmas_etch_poke_pact(buf_y + cur_w, &pac_u->pok_u, hed_u); - if ( siz_w == 0 ) { - return 0; - } - cur_w += siz_w; - } break; - case PACT_PEEK: { - siz_w = _xmas_etch_name(buf_y + cur_w, &pac_u->pek_u.nam_u); - if ( siz_w == 0 ) { - return 0; - } - cur_w += siz_w; - } break; - case PACT_PAGE: { - siz_w = _xmas_etch_page_pact(buf_y + cur_w, &pac_u->pag_u, hed_u); - if ( siz_w == 0 ) { - return 0; - } - cur_w += siz_w; - } break; +// refcounted +static c3_o +_xmas_rout_pact(u3_xmas_pact* pac_u, u3_noun lan) { + u3_noun tag, val; + u3x_cell(lan, &tag, &val); - default: { - u3l_log("bad pact type");//u3m_bail(c3__bail); + u3_assert( (c3y == tag) || (c3n == tag) ); + c3_o suc_o = c3y; + if ( c3y == tag ) { + u3_assert( c3y == u3a_is_cat(val) ); + u3_assert( val < 256 ); + pac_u->rut_u.typ_y = ROUT_GALAXY; + pac_u->rut_u.imp_y = val; + } else { + u3_lane lan_u = u3_xmas_decode_lane(u3k(val)); + // if in local-only mode, don't send remote packets + // + if ( (c3n == u3_Host.ops_u.net) && (0x7f000001 != lan_u.pip_w) ) { + suc_o = c3n; + } + // if the lane is uninterpretable, silently drop the packet + // + else if ( 0 == lan_u.por_s ) { + if ( u3C.wag_w & u3o_verbose ) { + u3l_log("ames: inscrutable lane"); + } + suc_o = c3n; + } else { + pac_u->rut_u.typ_y = ROUT_OTHER; + pac_u->rut_u.lan_u = lan_u; } } - return cur_w; + u3z(lan); + return suc_o; +} + + + +static void _xmas_pact_free(u3_xmas_pact* pac_u) { + // TODO: i'm lazy } + static void _xmas_send_cb(uv_udp_send_t* req_u, c3_i sas_i) { @@ -1599,6 +1562,189 @@ static void _xmas_send_buf(u3_xmas* sam_u, u3_lane lan_u, c3_y* buf_y, c3_w len_ } } +static void +_update_oldest_req(u3_pend_req *req_u, u3_gage* gag_u) +{ + if( req_u->tot_w == 0 ) { + return; + } + // scan in flight packets, find oldest + c3_w idx_w = req_u->lef_w; + c3_d now_d = _get_now_micros(); + c3_d wen_d = now_d; + for ( c3_w i = req_u->lef_w; i < req_u->nex_w; i++ ) { + if ( c3y == _has_bitset(&req_u->was_u, i) && + wen_d > req_u->wat_u[i].sen_d + ) { + wen_d = req_u->wat_u[i].sen_d; + idx_w = i; + } + } + if ( now_d == wen_d ) { + u3l_log("failed to find new oldest"); + _log_bitset(&req_u->was_u); + + } + req_u->old_w = idx_w; + uv_timer_start(&gag_u->tim_u, _xmas_packet_timeout, ((req_u->wat_u[req_u->old_w].sen_d + gag_u->rto_w) / 1000), 0); +} + + +static void +_try_resend(u3_pend_req* req_u) +{ + u3_xmas* sam_u = req_u->pac_u->sam_u; + u3_lane* lan_u = &req_u->lan_u; + if ( req_u->tot_w == 0 || req_u->ack_w <= REORDER_THRESH ) { + return; + } + c3_w ack_w = req_u->ack_w - REORDER_THRESH; + c3_d now_d = _get_now_micros(); + for( int i = req_u->lef_w; i < ack_w; i++ ) { + if ( c3y == _has_bitset(&req_u->was_u, i) ) { + if ( req_u->wat_u[i].tie_y == 1 ) { + c3_y* buf_y = c3_calloc(PACT_SIZE); + req_u->pac_u->pek_u.nam_u.fra_w = req_u->old_w; + u3l_log("fast resending %u ", req_u->old_w); + c3_w siz_w = _xmas_etch_pact(buf_y, req_u->pac_u, 0); + if( siz_w == 0 ) { + u3l_log("failed to etch"); + u3_assert( 0 ); + } + // TODO: better route management + _xmas_send_buf(sam_u, *lan_u, buf_y, siz_w); + _xmas_req_pact_resent(sam_u, &req_u->pac_u->pek_u.nam_u); + + } else if ( (now_d - req_u->wat_u[i].sen_d) < req_u->gag_u->rto_w ) { + c3_y* buf_y = c3_calloc(PACT_SIZE); + req_u->pac_u->pek_u.nam_u.fra_w = req_u->old_w; + u3l_log("slow resending %u ", req_u->old_w); + c3_w siz_w = _xmas_etch_pact(buf_y, req_u->pac_u, 0); + if( siz_w == 0 ) { + u3l_log("failed to etch"); + u3_assert( 0 ); + } + // TODO: better route management + _xmas_send_buf(sam_u, *lan_u, buf_y, siz_w); + _xmas_req_pact_resent(sam_u, &req_u->pac_u->pek_u.nam_u); + + } + } + } +} + +/* _xmas_req_pact_done(): mark packet as done, possibly producing the result +*/ +static u3_weak +_xmas_req_pact_done(u3_xmas* sam_u, u3_xmas_name *nam_u, u3_xmas_page_meat* mat_u, u3_lane* lan_u) +{ + u3_weak ret = u3_none; + c3_d now_d = _get_now_micros(); + u3_pend_req* req_u = _xmas_get_request(sam_u, nam_u); + + req_u->lan_u = *lan_u; + + u3_gage* gag_u = _xmas_get_lane(sam_u, nam_u->her_d, lan_u); + + // first we hear from lane + if ( gag_u == NULL ) { + gag_u = alloca(sizeof(u3_gage)); + _init_gage(gag_u); + } + + + if ( NULL == req_u ) { +#ifdef XMAS_DEBUG + u3l_log("xmas: attempting to mark packet done, no request"); +#endif + return u3_none; + } + + c3_w siz_w = (1 << (nam_u->boq_y - 3)); + // First packet received, instantiate request fully + if ( req_u->tot_w == 0 ) { + u3_assert( siz_w == 1024); // boq_y == 13 + req_u->dat_y = c3_calloc(siz_w * mat_u->tot_w); + req_u->wat_u = c3_calloc(sizeof(u3_pact_stat) * mat_u->tot_w); + req_u->tot_w = mat_u->tot_w; + _init_bitset(&req_u->was_u, mat_u->tot_w + 1); + req_u->lef_w = 2; + } + + if ( mat_u->tot_w <= nam_u->fra_w ) { + u3l_log("xmas: invalid packet (exceeded bounds)"); + return u3_none; + } + + // received duplicate + if ( nam_u->fra_w != 0 && c3n == _has_bitset(&req_u->was_u, nam_u->fra_w) ) { +#ifdef XMAS_DEBUG + u3l_log("duplicate page"); +#endif + + req_u->wat_u[nam_u->fra_w].dup_y++; + u3l_log("duplicate %u", req_u->wat_u[nam_u->fra_w].dup_y); + + return u3_none; + } + + _del_bitset(&req_u->was_u, nam_u->fra_w); + if ( nam_u->fra_w > req_u->ack_w ) { + req_u->ack_w = nam_u->fra_w; + } + if ( nam_u->fra_w != 0 && req_u->wat_u[nam_u->fra_w].tie_y != 1 ) { + u3l_log("received retry %u", nam_u->fra_w); + } + + req_u->len_w++; + if ( req_u->lef_w == nam_u->fra_w ) { + req_u->lef_w++; + } + + if ( nam_u->fra_w != 0 ) { + // handle gauge update + _xmas_handle_ack(gag_u, &req_u->wat_u[nam_u->fra_w]); + if ( (nam_u->fra_w % 10) == 0 ) { + } + } + + + memcpy(req_u->dat_y + (siz_w * nam_u->fra_w), mat_u->fra_y, mat_u->len_w); + c3_d wen_d = _get_now_micros(); + + _try_resend(req_u); + /* + * // in-order + if ( nam_u->fra_w != 0 && ((nam_u->fra_w - req_u->lef_w) > 5) ) { + u3l_log("diff: %u", _abs(nam_u->fra_w - req_u->old_w); + c3_y* buf_y = c3_calloc(PACT_SIZE); + req_u->pac_u->pek_u.nam_u.fra_w = req_u->old_w; + u3l_log("resending %u on num %u", req_u->old_w, nam_u->fra_w); + c3_w siz_w = _xmas_etch_pact(buf_y, req_u->pac_u, 0); + if( siz_w == 0 ) { + u3l_log("failed to etch"); + u3_assert( 0 ); + } + // TODO: better route management + _xmas_send_buf(sam_u, *lan_u, buf_y, siz_w); + _xmas_req_pact_resent(sam_u, &req_u->pac_u->pek_u.nam_u); + }*/ + _update_oldest_req(req_u, gag_u); + + + if ( req_u->len_w == req_u->tot_w ) { + uv_timer_stop(&req_u->tim_u); + //u3dc("rap", 13, u3dc("turn", u3n_slam_on(u3k(sam_u->mop_u.tap), req_u->dat), u3v_wish("tail"))); + //_xmas_put_request(sam_u, nam_u, u3_none); + return u3i_word(req_u->tot_w); + } else { + _xmas_put_request(sam_u, nam_u, req_u); + _xmas_put_lane(sam_u, nam_u->her_d, lan_u, gag_u); + } + return u3_none; +} + + static void _xmas_send(u3_xmas_pact* pac_u) { u3_xmas* sam_u = pac_u->sam_u; @@ -1692,8 +1838,8 @@ _xmas_rout_pacs(u3_xmas_pact* pac_u, u3_noun las) static void _xmas_timer_cb(uv_timer_t* tim_u) { - c3_w rto_w = 500; - + u3_pend_req* req_u = tim_u->data; + _try_resend(req_u); } static void @@ -2088,6 +2234,7 @@ _xmas_hear_page(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w, u3_lane lan_u) }*/ u3_weak res = _xmas_req_pact_done(sam_u, &pac_u->pag_u.nam_u, &pac_u->pag_u.mat_u, &lan_u); + u3l_log("done"); if ( u3_none != res ) { u3l_log("finished"); @@ -2154,7 +2301,10 @@ _xmas_hear_peek(u3_xmas_pact* pac_u, u3_lane lan_u) _xmas_add_lane_to_cache(sam_u, &pac_u->pek_u.nam_u, u3k(u3t(dat)), lan_u); } else if ( tag == XMAS_ITEM ) { c3_y* buf_y; - + if ( rand() % 20 == 0) { + u3l_log("dropping packet %u", pac_u->pek_u.nam_u.fra_w); + return; + } c3_w len_w = _xmas_respond(pac_u, &buf_y, u3k(dat)); _xmas_send_buf(pac_u->sam_u, lan_u, buf_y, len_w); } else { From a3206080dbbf24704d020bea56590ec8b6829102 Mon Sep 17 00:00:00 2001 From: Liam Fitzgerald Date: Mon, 5 Feb 2024 15:50:52 -0500 Subject: [PATCH 028/430] xmas: finalise congestion control --- pkg/vere/io/reno.c | 0 pkg/vere/io/xmas.c | 39 ++++++++++++++++++++++++++++----------- 2 files changed, 28 insertions(+), 11 deletions(-) create mode 100644 pkg/vere/io/reno.c diff --git a/pkg/vere/io/reno.c b/pkg/vere/io/reno.c new file mode 100644 index 0000000000..e69de29bb2 diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c index 97c11ab467..441333c9ef 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/xmas.c @@ -19,7 +19,7 @@ c3_o dop_o = c3n; -//#define XMAS_DEBUG c3y +#define XMAS_DEBUG c3y #define XMAS_VER 1 #define FINE_PAGE 4096 // packets per page #define FINE_FRAG 1024 // bytes per fragment packet @@ -1139,6 +1139,7 @@ static void _init_gage(u3_gage* gag_u) gag_u->rtv_w = 1000000; gag_u->con_w = 0; gag_u->wnd_w = 1; + gag_u->sst_w = 10000; } /* u3_xmas_encode_lane(): serialize lane to noun @@ -1278,6 +1279,14 @@ static void _xmas_handle_ack(u3_gage* gag_u, u3_pact_stat* pat_u) gag_u->rtv_w = (err_d + (gag_u->rtv_w * 7)) >> 3; gag_u->rto_w = _clamp_rto(gag_u->rtt_w + (4*gag_u->rtv_w)); + if ( gag_u->wnd_w < gag_u->sst_w ) { + gag_u->wnd_w++; + } else { + _log_gage(gag_u); + // ?? + //gag_u->wnd_w += (0 == ( + } + //u3_cbic_on_ack(gag_u); } @@ -1307,7 +1316,7 @@ _xmas_req_get_cwnd_nex(u3_xmas* sam_u, u3_xmas_name* nam_u, c3_w* nex_w, u3_xmas c3_w liv_w = _wyt_bitset(&req_u->was_u); c3_w rem_w = req_u->tot_w - req_u->nex_w + 1; - return c3_min(rem_w, IN_FLIGHT - liv_w); + return c3_min(rem_w, req_u->gag_u->wnd_w - liv_w); } /* _xmas_req_pact_sent(): mark packet as sent @@ -1355,8 +1364,7 @@ _xmas_req_pact_sent(u3_xmas* sam_u, u3_xmas_name* nam_u) req_u->pac_u = c3_calloc(sizeof(u3_xmas_pact)); req_u->pac_u->sam_u = sam_u; req_u->pac_u->hed_u.typ_y = PACT_PEEK; - req_u->gag_u = c3_calloc(sizeof(u3_gage)); - _init_gage(req_u->gag_u); + req_u->gag_u = NULL; memcpy(&req_u->pac_u->pek_u.nam_u, nam_u, sizeof(u3_xmas_name)); // TODO: handle restart // u3_assert( nam_u->fra_w == 0 ); @@ -1586,7 +1594,8 @@ _update_oldest_req(u3_pend_req *req_u, u3_gage* gag_u) } req_u->old_w = idx_w; - uv_timer_start(&gag_u->tim_u, _xmas_packet_timeout, ((req_u->wat_u[req_u->old_w].sen_d + gag_u->rto_w) / 1000), 0); + req_u->tim_u.data = req_u; + uv_timer_start(&req_u->tim_u, _xmas_packet_timeout, ((req_u->wat_u[req_u->old_w].sen_d + gag_u->rto_w) / 1000), 0); } @@ -1595,6 +1604,7 @@ _try_resend(u3_pend_req* req_u) { u3_xmas* sam_u = req_u->pac_u->sam_u; u3_lane* lan_u = &req_u->lan_u; + c3_o los_o = c3n; if ( req_u->tot_w == 0 || req_u->ack_w <= REORDER_THRESH ) { return; } @@ -1603,9 +1613,9 @@ _try_resend(u3_pend_req* req_u) for( int i = req_u->lef_w; i < ack_w; i++ ) { if ( c3y == _has_bitset(&req_u->was_u, i) ) { if ( req_u->wat_u[i].tie_y == 1 ) { + los_o = c3y; c3_y* buf_y = c3_calloc(PACT_SIZE); - req_u->pac_u->pek_u.nam_u.fra_w = req_u->old_w; - u3l_log("fast resending %u ", req_u->old_w); + req_u->pac_u->pek_u.nam_u.fra_w = i; c3_w siz_w = _xmas_etch_pact(buf_y, req_u->pac_u, 0); if( siz_w == 0 ) { u3l_log("failed to etch"); @@ -1615,10 +1625,11 @@ _try_resend(u3_pend_req* req_u) _xmas_send_buf(sam_u, *lan_u, buf_y, siz_w); _xmas_req_pact_resent(sam_u, &req_u->pac_u->pek_u.nam_u); - } else if ( (now_d - req_u->wat_u[i].sen_d) < req_u->gag_u->rto_w ) { + } else if ( (now_d - req_u->wat_u[i].sen_d) > req_u->gag_u->rto_w ) { + los_o = c3y; c3_y* buf_y = c3_calloc(PACT_SIZE); - req_u->pac_u->pek_u.nam_u.fra_w = req_u->old_w; - u3l_log("slow resending %u ", req_u->old_w); + req_u->pac_u->pek_u.nam_u.fra_w = i; + u3l_log("slow resending %u ", i); c3_w siz_w = _xmas_etch_pact(buf_y, req_u->pac_u, 0); if( siz_w == 0 ) { u3l_log("failed to etch"); @@ -1627,10 +1638,15 @@ _try_resend(u3_pend_req* req_u) // TODO: better route management _xmas_send_buf(sam_u, *lan_u, buf_y, siz_w); _xmas_req_pact_resent(sam_u, &req_u->pac_u->pek_u.nam_u); - } } } + + if ( c3y == los_o ) { + req_u->gag_u->sst_w = (req_u->gag_u->wnd_w / 2) + 1; + req_u->gag_u->wnd_w = req_u->gag_u->sst_w; + req_u->gag_u->rto_w = _clamp_rto(req_u->gag_u->rto_w * 2); + } } /* _xmas_req_pact_done(): mark packet as done, possibly producing the result @@ -1645,6 +1661,7 @@ _xmas_req_pact_done(u3_xmas* sam_u, u3_xmas_name *nam_u, u3_xmas_page_meat* mat_ req_u->lan_u = *lan_u; u3_gage* gag_u = _xmas_get_lane(sam_u, nam_u->her_d, lan_u); + req_u->gag_u = gag_u; // first we hear from lane if ( gag_u == NULL ) { From 4b9cc43ea3c527b8e6b7aa8fd4529dbf7a2a1be5 Mon Sep 17 00:00:00 2001 From: Liam Fitzgerald Date: Tue, 20 Feb 2024 15:43:39 -0600 Subject: [PATCH 029/430] xmas: hacky first pass at blake --- pkg/vere/BUILD.bazel | 14 ++ pkg/vere/io/blake.c | 586 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 600 insertions(+) create mode 100644 pkg/vere/io/blake.c diff --git a/pkg/vere/BUILD.bazel b/pkg/vere/BUILD.bazel index 316a40a93d..e37be0b603 100644 --- a/pkg/vere/BUILD.bazel +++ b/pkg/vere/BUILD.bazel @@ -196,6 +196,20 @@ cc_test( deps = [":vere"], ) +cc_test( + name = "blake_tests", + timeout = "short", + srcs = ["io/blake.c"], + features = select({ + "@platforms//os:linux": ["fully_static_link"], + "//conditions:default": [], + }), + deps = [":vere"], + defines = ["BLAKE_TEST"] +) + + + cc_test( name = "xmas_tests", timeout = "short", diff --git a/pkg/vere/io/blake.c b/pkg/vere/io/blake.c new file mode 100644 index 0000000000..8352eaeb52 --- /dev/null +++ b/pkg/vere/io/blake.c @@ -0,0 +1,586 @@ +/// @file + + +#include "vere.h" +#include +#include +#include "urcrypt.h" + +#define BLAKE_TEST +#define BLAKE3_BLOCK_LEN 64 + +enum blake3_flags { + CHUNK_START = 1 << 0, + CHUNK_END = 1 << 1, + PARENT = 1 << 2, + ROOT = 1 << 3, + KEYED_HASH = 1 << 4, + DERIVE_KEY_CONTEXT = 1 << 5, + DERIVE_KEY_MATERIAL = 1 << 6, +}; + +typedef struct _blake_node { + c3_w cev_w[8]; + c3_w boq_w[16]; + c3_d con_d; + c3_w len_w; + c3_w fag_w; +} blake_node; + + + + +static c3_w IV[8] = {0x6A09E667UL, 0xBB67AE85UL, 0x3C6EF372UL, 0xA54FF53AUL, 0x510E527FUL, 0x9B05688CUL, 0x1F83D9ABUL, 0x5BE0CD19UL}; +#define ABS_DIFF(x,y) x > y ? x - y : y - x + +#define VEC_SCALING 2 + +typedef struct _u3_raw_vec { + c3_w siz_w; + c3_w len_w; + void** vod_p; +} u3_raw_vec; + +#define u3_vec(type) u3_raw_vec + +static void +_vec_init(u3_raw_vec* vec_u, c3_w siz_w) +{ + vec_u->siz_w = siz_w; + vec_u->vod_p = c3_calloc(siz_w * sizeof(void*)); + vec_u->len_w = 0; +} + +static void +_vec_free(u3_raw_vec* vec_u) +{ + c3_free(vec_u->vod_p); +} + +static u3_raw_vec* +_vec_make(c3_w siz_w) +{ + u3_raw_vec* vec_u = c3_calloc(sizeof(u3_raw_vec)); + _vec_init(vec_u, siz_w); + return vec_u; +} + + + +static c3_w +_vec_len(u3_raw_vec* vec_u) +{ + return vec_u->len_w; +} + +static void* +_vec_raw_get(u3_raw_vec* vec_u, c3_w idx_w) +{ + if ( vec_u->len_w <= idx_w ) { + return NULL; + } + return vec_u->vod_p[idx_w]; +} + +#define _vec_get(typ,vec,idx) ((typ*)_vec_raw_get(vec, idx)) + +static void* +_vec_raw_got(u3_raw_vec* vec_u, c3_w idx_w) +{ + return vec_u->vod_p[idx_w]; +} + +#define _vec_got(typ,vec,idx) ((typ*)_vec_raw_got(vec, idx)) + + +static void +_vec_resize(u3_raw_vec* vec_u, c3_w siz_w) { + u3_assert( vec_u->len_w <= siz_w ); + void* vod_p = c3_calloc(siz_w * sizeof(void*)); + + fprintf(stderr, "new size: %u", siz_w); + memcpy(vod_p, vec_u->vod_p, vec_u->len_w * sizeof(void*)); + c3_free(vec_u->vod_p); + vec_u->vod_p = vod_p; + vec_u->siz_w = siz_w; +} + +static void +_vec_append(u3_raw_vec* vec_u, void* tem_p) +{ + if(vec_u->siz_w == vec_u->len_w ) { + _vec_resize(vec_u, vec_u->siz_w * VEC_SCALING); + } + vec_u->vod_p[vec_u->len_w] = tem_p; + vec_u->len_w++; +} + +static void* +_vec_popf(u3_raw_vec* vec_u) { + if ( vec_u->len_w == 0 ) { + fprintf(stderr, "Failed pop\r\n"); + return NULL; + } + void* hit_p = vec_u->vod_p[0]; + void* vod_p = c3_calloc(vec_u->siz_w); + memcpy(vod_p, vec_u->vod_p + sizeof(void*), vec_u->len_w - 1); + c3_free(vec_u->vod_p); + vec_u->len_w--; + vec_u->vod_p = vod_p; + return hit_p; +} + +static void +_vec_swap(u3_raw_vec* vec_u, c3_w fst_w, c3_w snd_w) { + void* tmp_p = vec_u->vod_p[fst_w]; + vec_u->vod_p[fst_w] = vec_u->vod_p[snd_w]; + vec_u->vod_p[snd_w] = tmp_p; +} + + + +static void _vec_weld_mut(u3_raw_vec* fst_u, u3_raw_vec* snd_u) +{ + if ( fst_u->siz_w <= (fst_u->len_w + snd_u->len_w ) ) { + _vec_resize(fst_u, (fst_u->len_w + snd_u->len_w) * VEC_SCALING ); + } + memcpy(fst_u->vod_p + (fst_u->len_w * sizeof(void*)), snd_u->vod_p, snd_u->len_w ); + fst_u->len_w += snd_u->len_w; +} + +//type Hash [32]byte + +//func (h Hash) String() string { return hex.EncodeToString(h[:]) } + +//type Pair [2][8]uint32 +typedef struct _pair { + c3_w sin_w[8]; + c3_w dex_w[8]; +} pair; + +//type subtree [2]int +typedef struct _subtree { + c3_d sin_d; + c3_d dex_d; +} subtree; + +typedef struct _verifier { + c3_d num_d; + subtree sub_u; + u3_vec(c3_w[8]) que_u; + u3_vec(c3_w[8]) sta_u; +} verifier; + +static c3_d _count_lead_zeros(c3_d bit_d) +{ + return (bit_d == 0) ? 64 : __builtin_clz(bit_d); +} + +static c3_d _bitlen(c3_d bit_d) +{ + return 64 - _count_lead_zeros(bit_d); +} + +static c3_d +_height(subtree* sub_u) +{ + return _bitlen(ABS_DIFF(sub_u->dex_d, sub_u->sin_d)) - 1; +} + +static c3_d +_largest_pow2(c3_d val_d) +{ + return 1 << (_bitlen(val_d-1) - 1); +} + +static void +y_to_w(c3_y* byt_y, c3_w wod_w[16]) +{ + for ( c3_w i_w = 0; i_w < 16; i_w++ ) { + c3_w cur_w = 0; + for ( c3_w j_w = 0; j_w < 4; j_w++ ) { + cur_w |= byt_y[(i_w * 4) + j_w] << (8*j_w); + } + wod_w[i_w] = cur_w; + } +} + +static void +w_to_y(c3_w wod_w[16], c3_y byt_y[64]) +{ + for ( c3_w i_w = 0; i_w < 16; i_w++ ) { + for ( c3_w j_w = 0; j_w < 4; j_w++ ) { + byt_y[(i_w * 4) + j_w] = (wod_w[i_w] >> (8*j_w)) & 0xff; + } + } +} + + + +static void +_make_chain_value(c3_w res_w[8], blake_node* nod_u) +{ + //c3_w* blake3 +} + +static blake_node +_compress_chunk(c3_y* dat_y, c3_w dat_w, c3_w cev_w[8], c3_d con_d, c3_w fag_w) +{ + // A node represents a chunk or parent in the BLAKE3 Merkle tree. + blake_node nod_u; + memcpy(&nod_u.cev_w, cev_w, 8 * sizeof(c3_w)); + nod_u.con_d = con_d; + nod_u.len_w = BLAKE3_BLOCK_LEN; + nod_u.fag_w |= CHUNK_START; + + c3_y* boq_y = alloca(BLAKE3_BLOCK_LEN); + c3_w lef_w = dat_w; + memset(boq_y, 0, BLAKE3_BLOCK_LEN); + while ( lef_w > BLAKE3_BLOCK_LEN ) { + memcpy(boq_y, dat_y, BLAKE3_BLOCK_LEN); + boq_y += BLAKE3_BLOCK_LEN; + lef_w -= BLAKE3_BLOCK_LEN; + y_to_w(boq_y, nod_u.boq_w); + c3_w cev_w[8]; + memset(cev_w, 0, 8 * sizeof(c3_w)); + _make_chain_value(cev_w, &nod_u); + memcpy(nod_u.cev_w, cev_w, 8 * sizeof(c3_w)); + nod_u.fag_w &= ~CHUNK_START; + } + memset(boq_y, 0, BLAKE3_BLOCK_LEN); + nod_u.len_w = lef_w; + memcpy(boq_y, dat_y, lef_w); + y_to_w(boq_y, nod_u.boq_w); + nod_u.fag_w |= CHUNK_END; + + return nod_u; +} + +static +blake_node _leaf_hash(c3_d con_d) +{ + +//_compress_chunk(c3_y* dat_y, c3_w dat_w, c3_w* cev_w, c3_d con_d, c3_w fag_w) + c3_y* dat_y = alloca(1024); + memset(dat_y, 0, 1024); + return _compress_chunk(dat_y, 1024, IV, con_d, 0); +} + +static blake_node +_parent_node(c3_w sin_w[8], c3_w dex_w[8], c3_w key_w[8], c3_w fag_w) +{ + blake_node nod_u; + nod_u.con_d = 0; + memcpy(&nod_u.cev_w, key_w, 8 * sizeof(c3_w)); + nod_u.len_w = BLAKE3_BLOCK_LEN; + nod_u.fag_w |= PARENT; + memcpy(nod_u.boq_w, sin_w, 8 * sizeof(c3_w)); + memcpy(&nod_u.boq_w[8], dex_w, 8 * sizeof(c3_w)); + return nod_u; +} + +static blake_node +_parent_hash(blake_node* sin_u, blake_node* dex_u) +{ + c3_w sin_w[8], dex_w[8]; + memset(sin_w, 0, 8 * sizeof(c3_w)); + memset(dex_w, 0, 8 * sizeof(c3_w)); + _make_chain_value(sin_w, sin_u); + _make_chain_value(dex_w, dex_u); + return _parent_node(sin_w, dex_w, IV, 0); +} + +static void +_compress_node(c3_y out_y[64], blake_node* nod_u) +{ + //urcrypt_blake3_compress(nod_u->cev_w, nod_u->boq_w, nod_u->len_w, nod_u->con_w, nod_u->fag_w, out_y); + + + +} + + +static void +_root_hash(c3_y out_y[16], blake_node* nod_u) +{ + nod_u->fag_w |= ROOT; + +} + +static void recurse_subtree(subtree* sub_u, blake_node* nod_u, u3_vec(pair)* par_u) +{ + if ( _height(sub_u) == 0 ) { + *nod_u = _leaf_hash(sub_u->sin_d); + return; + } + c3_d mid_d = sub_u->sin_d + _largest_pow2(ABS_DIFF(sub_u->dex_d, sub_u->sin_d)); + + subtree sin_u = (subtree){sub_u->sin_d, mid_d}; + blake_node* lod_u = c3_calloc(sizeof(blake_node)); + u3_vec(pair)* lar_u = _vec_make(8); + recurse_subtree(&sin_u, lod_u, lar_u); + + subtree dex_u = (subtree){mid_d, sub_u->dex_d}; + blake_node* rod_u = c3_calloc(sizeof(blake_node)); + u3_vec(pair)* rar_u = _vec_make(8); + recurse_subtree(&dex_u, rod_u, rar_u); + + *nod_u = _parent_hash(lod_u, rod_u); + c3_w* lcv_w = c3_calloc(8* sizeof(c3_w)); + _make_chain_value(lcv_w, lod_u); + c3_w* rcv_w = c3_calloc(8* sizeof(c3_w)); + _make_chain_value(rcv_w, rod_u); + + _vec_append(par_u, lcv_w); + _vec_append(par_u, rcv_w); + _vec_weld_mut(par_u, lar_u); + _vec_weld_mut(par_u, rar_u); +} + +static void +_bao_build(c3_d num_d, c3_y has_y[32], u3_vec(c3_w[8])* pof_u, u3_vec(pair)* par_u) { + subtree sub_u; + memset(&sub_u, 0, sizeof(subtree)); + blake_node* nod_u = c3_calloc(sizeof(blake_node)); + recurse_subtree(&sub_u, nod_u, par_u); + + if ( _vec_len(par_u) != 0 ) { + for ( c3_w i_w = _bitlen(num_d - 1); i_w > 1; i_w-- ) { + _vec_append(pof_u, _vec_get(pair, par_u, 0)->dex_w); + _vec_popf(par_u); + } + pair* fst_u = _vec_get(pair, par_u, 0); + _vec_append(pof_u, fst_u->dex_w); + _vec_append(pof_u, fst_u->sin_w); + _vec_popf(par_u); + for ( c3_w i_w = 0; i_w < (_vec_len(pof_u) >> 1); i_w++ ) { + c3_w jay_w = _vec_len(pof_u) - i_w - 1; + _vec_swap(pof_u, i_w, jay_w); + } + } +} + +static void +_push_leaf(verifier* ver_u, c3_w lef_w[8]) +{ + _vec_append(&ver_u->que_u, lef_w); +} + +static void +_pop_leaf(c3_w out_w[8], verifier* ver_u) +{ + c3_w* res_w = _vec_popf(&ver_u->que_u); + memcpy(out_w, res_w, 8); +} + +static void +_push_parent(verifier* ver_u, c3_w par_w[8]) +{ + _vec_append(&ver_u->sta_u, par_w); +} + +static void +_pop_parent(c3_w out_w[8], verifier* ver_u) +{ + c3_w* res_w = _vec_popf(&ver_u->sta_u); + memcpy(out_w, res_w, 8); +} + +static void +_verifier_next(verifier* ver_u) +{ + if ( _height(&ver_u->sub_u) == 0 ) { + ver_u->sub_u.sin_d += 1; + ver_u->sub_u.dex_d += 1; + if ( ver_u->sub_u.dex_d > ver_u->num_d ) { + ver_u->sub_u.dex_d = ver_u->num_d; + } + subtree sub_u = (subtree){ver_u->sub_u.sin_d, ver_u->sub_u.sin_d + _largest_pow2(ABS_DIFF(ver_u->sub_u.dex_d, ver_u->sub_u.sin_d))}; + ver_u->sub_u = sub_u; + } +} + +static c3_o +_verifier_init(verifier* ver_u, c3_y rot_y[32], u3_vec(c3_w[8])* pof_u) +{ + if ( _vec_len(pof_u) == 0 ) { + return ver_u->num_d <= 1 ? c3y : c3n; + } + blake_node nod_u = _parent_node(_vec_get(c3_w, pof_u, 0), _vec_get(c3_w, pof_u, 1), IV, 0); + for (c3_w i_w = 2; i_w < (_vec_len(pof_u) - 2); i_w++ ) { + c3_w cev_w[8]; + memset(cev_w, 0, 8); + _make_chain_value(cev_w, &nod_u); + c3_w* pof_w = _vec_get(c3_w, pof_u, i_w); + nod_u = _parent_node(cev_w, pof_w, IV, 0); + } + c3_y ron_y[32]; + _root_hash(ron_y, &nod_u); + + if ( memcmp(ron_y, rot_y, 32) != 0 ) { + return c3n; + } + + ver_u->sub_u = (subtree){2,4}; + _vec_init(&ver_u->que_u, 8); + _vec_append(&ver_u->que_u, _vec_get(c3_w, pof_u, 0)); + _vec_append(&ver_u->que_u, _vec_get(c3_w, pof_u, 1)); + + _vec_init(&ver_u->sta_u, 8); + _vec_weld_mut(&ver_u->sta_u, pof_u); + + for ( c3_w i_w = 0; i_w < (_vec_len(&ver_u->sta_u)/2); i_w++ ) { + c3_w jay_w = _vec_len(&ver_u->sta_u) - i_w - 1; + _vec_swap(&ver_u->sta_u, i_w, jay_w); + } + return c3y; +} + +static c3_o +_veri_check_leaf(verifier* ver_u, c3_d lef_d) +{ + if ( _vec_len(&ver_u->sta_u) > 0 ) { + c3_w out_w[8]; + _pop_leaf(out_w, ver_u); + c3_w cav_w[8]; + blake_node nod_u = _leaf_hash(lef_d); + _make_chain_value(cav_w, &nod_u); + return 0 == memcmp(cav_w, out_w, 8 * sizeof(c3_w)) ? c3y : c3n; + } else if ( _vec_len(&ver_u->sta_u) > 0 ) { + c3_w out_w[8]; + _pop_parent(out_w, ver_u); + c3_w cav_w[8]; + blake_node nod_u = _leaf_hash(lef_d); + _make_chain_value(cav_w, &nod_u); + return 0 == memcmp(cav_w, out_w, 8 * sizeof(c3_w)) ? c3y : c3n; + } + return c3n; +} + +static c3_o +_veri_check_pair(verifier* ver_u, pair* par_u) +{ + c3_w out_w[8]; + _pop_parent(out_w, ver_u); + blake_node nod_u = _parent_node(par_u->sin_w, par_u->dex_w, IV, 0); + c3_w cav_w[8]; + _make_chain_value(cav_w, &nod_u); + c3_w par_w[8]; + _pop_parent(par_w, ver_u); + c3_o ret_o = _( _vec_len(&ver_u->sta_u) > 0 && memcmp(par_w, cav_w, 8 * sizeof(c3_w))); + return ret_o; +} + +static void _veri_init(verifier* ver_u, c3_d num_d) +{ + ver_u->num_d = num_d; + ver_u->sub_u = (subtree){0, num_d}; +} + +#ifdef BLAKE_TEST + +static void _test_bao() +{ + fprintf(stderr, "Starting test\r\n"); + for ( c3_w num_w = 1; num_w <= 100; num_w++ ) { + c3_y has_y[32]; + u3_vec(c3_w[8])* pof_u = _vec_make(10); + u3_vec(pair)* par_u = _vec_make(10); + _bao_build(num_w, has_y, pof_u, par_u); + verifier ver_u; + memset(&ver_u, 0, sizeof(verifier)); + _veri_init(&ver_u, num_w); + if ( c3n == _verifier_init(&ver_u, has_y, pof_u) ) { + fprintf(stderr, "Failed on %u\r\n", num_w); + exit(1); + } + + if ( num_w == 1 ) { + c3_y out_y[16]; + blake_node nod_u = _leaf_hash(0); + _root_hash(out_y, &nod_u); + if ( memcmp(out_y, has_y, 32) != 0 ) { + fprintf(stderr, "1 special case\r\n"); + exit(1); + } + for ( c3_w i_w = 0; i_w < num_w; i_w++ ) { + if ( c3n == _veri_check_leaf(&ver_u, i_w) ) { + fprintf(stderr, "Check leaf %u failed for %u\r\n", i_w, num_w); + exit(1); + } + if ( i_w < _vec_len(par_u) ) { + pair* pir_u = _vec_get(pair, par_u, i_w); + if ( _veri_check_pair(&ver_u, pir_u) == c3n ) { + fprintf(stderr, "check pair failed (%u, %u)", i_w, num_w); + exit(1); + } + _verifier_next(&ver_u); + if ( _height(&ver_u.sub_u) == 0 ) { + _push_leaf(&ver_u, pir_u->sin_w); + _push_leaf(&ver_u, pir_u->dex_w); + _verifier_next(&ver_u); + _verifier_next(&ver_u); + } else { + _push_parent(&ver_u, pir_u->sin_w); + _push_parent(&ver_u, pir_u->dex_w); + } + } + } + } + } +} + +static void _test_vec() { + fprintf(stderr, "Making vec"); + u3_vec(c3_w)* vec_u = _vec_make(2); + + c3_w fst = 1; + c3_w snd = 2; + c3_w thd = 3; + _vec_append(vec_u, &fst); + fprintf(stderr, "put one\r\n"); + _vec_append(vec_u, &snd); + fprintf(stderr, "put two\r\n"); + _vec_append(vec_u, &thd); + fprintf(stderr, "put three\r\n"); + + c3_w* one_w = _vec_popf(vec_u); + + if ( *one_w != 1 ) { + fprintf(stderr, "(one) Vec failure\r\n"); + exit(1); + } + fprintf(stderr, "popped %u", *one_w); + + c3_w* two_w = _vec_popf(vec_u); + fprintf(stderr, "two pointer %p, %p\r\n", &snd, two_w); + if ( *two_w != 2 ) { + fprintf(stderr, "(two) Vec failure\r\n"); + exit(1); + } + fprintf(stderr, "popped %u", *two_w); + + c3_w* thr_w = _vec_popf(vec_u); + if ( *thr_w != 3 ) { + fprintf(stderr, "(three) Vec failure\r\n"); + exit(1); + } + + fprintf(stderr, "popped %u", *thr_w); + + if ( 0 != _vec_len(vec_u) ) { + fprintf(stderr, "Vec should be empty\r\n"); + exit(1); + } + _vec_free(vec_u); + c3_free(vec_u); +} + +int main() { + //_test_bao(); + _test_vec(); + + return 0; +} + +#endif From cabf4b318713396e46817c20008e0afb64a7a127 Mon Sep 17 00:00:00 2001 From: Liam Fitzgerald Date: Wed, 21 Feb 2024 08:44:29 -0600 Subject: [PATCH 030/430] blake: fix vec impl --- pkg/vere/io/blake.c | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/pkg/vere/io/blake.c b/pkg/vere/io/blake.c index 8352eaeb52..cdd02efb28 100644 --- a/pkg/vere/io/blake.c +++ b/pkg/vere/io/blake.c @@ -20,11 +20,11 @@ enum blake3_flags { }; typedef struct _blake_node { - c3_w cev_w[8]; - c3_w boq_w[16]; - c3_d con_d; - c3_w len_w; - c3_w fag_w; + c3_w cev_w[8]; + c3_w boq_w[16]; + c3_d con_d; + c3_w len_w; + c3_w fag_w; } blake_node; @@ -116,20 +116,26 @@ _vec_append(u3_raw_vec* vec_u, void* tem_p) } static void* -_vec_popf(u3_raw_vec* vec_u) { - if ( vec_u->len_w == 0 ) { +_vec_pop(u3_raw_vec* vec_u, c3_w idx_w) +{ + if ( vec_u->len_w == 0 || idx_w >= vec_u->siz_w ) { fprintf(stderr, "Failed pop\r\n"); return NULL; } - void* hit_p = vec_u->vod_p[0]; - void* vod_p = c3_calloc(vec_u->siz_w); - memcpy(vod_p, vec_u->vod_p + sizeof(void*), vec_u->len_w - 1); - c3_free(vec_u->vod_p); + void* hit_p = vec_u->vod_p[idx_w]; vec_u->len_w--; - vec_u->vod_p = vod_p; + for ( c3_w i_w = idx_w; i_w < vec_u->len_w; i_w++ ) { + vec_u->vod_p[i_w] = vec_u->vod_p[i_w+1]; + } return hit_p; } +static void* +_vec_popf(u3_raw_vec* vec_u) +{ + return _vec_pop(vec_u, 0); +} + static void _vec_swap(u3_raw_vec* vec_u, c3_w fst_w, c3_w snd_w) { void* tmp_p = vec_u->vod_p[fst_w]; @@ -486,10 +492,13 @@ static void _test_bao() c3_y has_y[32]; u3_vec(c3_w[8])* pof_u = _vec_make(10); u3_vec(pair)* par_u = _vec_make(10); + fprintf(stderr, "Bao building\r\n"); _bao_build(num_w, has_y, pof_u, par_u); + fprintf(stderr, "Bao built\r\n"); verifier ver_u; memset(&ver_u, 0, sizeof(verifier)); _veri_init(&ver_u, num_w); + fprintf(stderr, "veri initted\r\n"); if ( c3n == _verifier_init(&ver_u, has_y, pof_u) ) { fprintf(stderr, "Failed on %u\r\n", num_w); exit(1); @@ -577,8 +586,8 @@ static void _test_vec() { } int main() { - //_test_bao(); _test_vec(); + _test_bao(); return 0; } From caaabdbdeb4631f8947ec93afa5826204a9544a5 Mon Sep 17 00:00:00 2001 From: parallels Date: Wed, 7 Feb 2024 15:41:28 -0800 Subject: [PATCH 031/430] xmas: improve forwarding --- pkg/vere/io/cong.h | 2 +- pkg/vere/io/cubic.c | 6 +- pkg/vere/io/xmas.c | 533 ++++++++++++++++++++++++++++++++------------ 3 files changed, 396 insertions(+), 145 deletions(-) diff --git a/pkg/vere/io/cong.h b/pkg/vere/io/cong.h index 3dcf868ad0..0db5276c1e 100644 --- a/pkg/vere/io/cong.h +++ b/pkg/vere/io/cong.h @@ -12,7 +12,7 @@ typedef struct _u3_gage { c3_w rto_w; // rto c3_w rtv_w; // rttvar c3_w wnd_w; // cwnd - c3_w wnc_w; // cwnd_cnt + c3_w wnf_w; // cwnd fraction c3_w sst_w; // ssthresh c3_w con_w; // counter void* alg_u; // algorithm backpointer diff --git a/pkg/vere/io/cubic.c b/pkg/vere/io/cubic.c index c42087c867..1f9eb33cfd 100644 --- a/pkg/vere/io/cubic.c +++ b/pkg/vere/io/cubic.c @@ -112,11 +112,11 @@ void u3_cbic_on_ack(u3_gage* gag_u) if ( gag_u->wnd_w <= gag_u->sst_w ) { gag_u->wnd_w += 1; } else { - if ( gag_u->wnc_w > gag_u->wnd_w ) { + if ( gag_u->wnf_w > gag_u->wnd_w ) { gag_u->wnd_w += 1; - gag_u->wnc_w = 0; + gag_u->wnf_w = 0; } else { - gag_u->wnc_w += 1; + gag_u->wnf_w += 1; } } } diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c index 441333c9ef..233973a1ce 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/xmas.c @@ -19,7 +19,7 @@ c3_o dop_o = c3n; -#define XMAS_DEBUG c3y +//#define XMAS_DEBUG c3y #define XMAS_VER 1 #define FINE_PAGE 4096 // packets per page #define FINE_FRAG 1024 // bytes per fragment packet @@ -75,6 +75,12 @@ typedef struct _u3_pact_stat { struct _u3_xmas; +typedef struct _u3_peer_last { + c3_d acc_d; // time of last access + c3_d son_d; // time of sponsor check +} u3_peer_last; + + typedef struct _u3_pend_req { c3_w nex_w; @@ -108,6 +114,7 @@ typedef struct _u3_xmas { uv_handle_t had_u; }; c3_l sev_l; + c3_o for_o; // is forwarding ur_cue_test_t* tes_u; // cue-test handle u3_cue_xeno* sil_u; // cue handle u3p(u3h_root) her_p; // @@ -119,6 +126,15 @@ typedef struct _u3_xmas { c3_d tim_d; } u3_xmas; +typedef struct _u3_peer { + u3_peer_last las_u; // last check timestamps + u3_lane dir_u; // direct lane (if any) + u3_lane ind_u; // indirect lane (if any) + c3_y imp_y; // galaxy + u3_xmas* sam_u; // backpointer +} u3_peer; + + typedef enum _u3_xmas_ptag { PACT_RESV = 0, PACT_PAGE = 1, @@ -296,6 +312,13 @@ _log_head(u3_xmas_head* hed_u) u3l_log(""); } +static void +_log_lane(u3_lane* lan_u) +{ + c3_c* sip_c = inet_ntoa(inet_makeaddr(AF_INET, lan_u->pip_w)); + u3l_log("xmas: lane (%s,%u)", sip_c, lan_u->por_s); +} + static void _log_buf(c3_y* buf_y, c3_w len_w) { @@ -358,7 +381,7 @@ _log_gage(u3_gage* gag_u) u3l_log("rto: %f", ((double)gag_u->rto_w / 1000)); u3l_log("rttvar: %f", ((double)gag_u->rtv_w / 1000)); u3l_log("cwnd: %u", gag_u->wnd_w); - u3l_log("cwnd cnt: %u", gag_u->wnc_w); + u3l_log("cwnd fraction: %f", gag_u->wnf_w / (float)gag_u->wnd_w ); u3l_log("ssthresh: %u", gag_u->sst_w); u3l_log("counter: %u", gag_u->con_w); //u3l_log("algorithm: %s", gag_u->alg_c); @@ -415,6 +438,38 @@ _clamp_rto(c3_d rto_d) { return c3_min(c3_max(rto_d, 200000), 25000000); } +static void +_update_hopcount(u3_xmas_head* hed_u) +{ + hed_u->hop_y = c3_max(hed_u->hop_y, 7); +} + +static inline void +_get_her(u3_xmas_pact* pac_u, c3_d* our_d) +{ + switch ( pac_u->hed_u.typ_y ) { + default: { + u3m_bail(c3__foul); + break; + } + case PACT_PAGE: { + memcpy(our_d, pac_u->pag_u.nam_u.her_d,2); + break; + } + case PACT_PEEK: { + memcpy(our_d, pac_u->pek_u.nam_u.her_d,2); + break; + } + case PACT_POKE: { + //memcpy(our_d, pac_u->pok_u.pek_u.nam_u.her_d, 2); + break; + } + } +} + + + + static void _init_bitset(u3_bitset* bit_u, c3_w len_w) { @@ -485,6 +540,15 @@ _log_bitset(u3_bitset* bit_u) u3l_log("finished"); } +// TODO: fix with scry into jael?? +// remove rear call to enable star routing +static u3_noun +_xmas_get_sponsor_list(c3_d her_d[2]) +{ + u3_noun her = u3i_chubs(2, her_d); + u3_noun lis = u3do("rear", u3do("^saxo:title", her)); + return lis; +} /* _xmas_encode_path(): produce buf_y as a parsed path @@ -789,34 +853,6 @@ _xmas_sift_page_pact(u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, } return cur_w; - - /*// next hop - memcpy(pac_u->hop_y, buf_y, 6); - cur_w += 6; - - // path length - pac_u->pat_s = _ames_sift_short(buf_y + cur_w); - cur_w += 2; - - // path contents - pac_u->pat_c = c3_calloc(pac_u->pat_s + 1); - memcpy(pac_u->pat_c, buf_y + cur_w, pac_u->pat_s); - pac_u->pat_c[pac_u->pat_s] = '\0'; - cur_w += pac_u->pat_s + 1; - - // total fragments - pac_u->tot_w = _ames_sift_word(buf_y + cur_w); - cur_w += 4; - - // Authenticator - memcpy(pac_u->aut_y, buf_y + cur_w, 96); - cur_w += 96; - - c3_w dat_w = 1024; // len_w - cur_w; - pac_u->dat_y = c3_calloc(dat_w); - memcpy(pac_u->dat_y, buf_y + cur_w, dat_w); - */ - return 0; } @@ -830,10 +866,6 @@ _xmas_sift_peek_pact(u3_xmas_peek_pact* pac_u, c3_y* buf_y, c3_w len_w) return 0; } - /*if (siz_w > len_w ) { - u3l_log("xmas: buffer overrun (FIXME)"); - u3m_bail(c3__foul); - }*/ return siz_w; } @@ -883,6 +915,7 @@ _xmas_sift_pact(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w) } break; default: { u3l_log("xmas: received unknown packet type"); + _log_buf(buf_y, len_w); break; } } @@ -1008,7 +1041,6 @@ _xmas_etch_name(c3_y* buf_y, u3_xmas_name* nam_u) } return cur_w; - //_ames_etch_short(buf_y + cur_w, &met_s); } @@ -1083,7 +1115,7 @@ _xmas_etch_poke_pact(c3_y* buf_y, u3_xmas_poke_pact* pac_u, u3_xmas_head* hed_u) } static c3_w -_xmas_etch_pact(c3_y* buf_y, u3_xmas_pact* pac_u, u3_noun her) +_xmas_etch_pact(c3_y* buf_y, u3_xmas_pact* pac_u ) { c3_w cur_w = 0; u3_xmas_head* hed_u = &pac_u->hed_u; @@ -1144,7 +1176,7 @@ static void _init_gage(u3_gage* gag_u) /* u3_xmas_encode_lane(): serialize lane to noun */ -static u3_atom +static u3_noun u3_xmas_encode_lane(u3_lane lan_u) { // [%if ip=@ port=@] return u3nt(c3__if, u3i_word(lan_u.pip_w), lan_u.por_s); @@ -1281,13 +1313,10 @@ static void _xmas_handle_ack(u3_gage* gag_u, u3_pact_stat* pat_u) if ( gag_u->wnd_w < gag_u->sst_w ) { gag_u->wnd_w++; - } else { - _log_gage(gag_u); - // ?? - //gag_u->wnd_w += (0 == ( + } else if ( gag_u->wnd_w <= ++gag_u->wnf_w ) { + gag_u->wnd_w++; + gag_u->wnf_w = 0; } - - //u3_cbic_on_ack(gag_u); } /* @@ -1305,12 +1334,12 @@ _xmas_req_get_cwnd_nex(u3_xmas* sam_u, u3_xmas_name* nam_u, c3_w* nex_w, u3_xmas return 0; } *nex_u = req_u->pac_u; - if ( req_u->tot_w == 0 ) { + if ( req_u->tot_w == 0 || req_u->gag_u == NULL ) { u3l_log("shouldn't happen"); *nex_w = 0; return 1; - } + *nex_w = req_u->nex_w; c3_w liv_w = _wyt_bitset(&req_u->was_u); @@ -1327,11 +1356,11 @@ _xmas_req_pact_resent(u3_xmas* sam_u, u3_xmas_name* nam_u) { c3_d now_d = _get_now_micros(); u3_pend_req* req_u = _xmas_get_request(sam_u, nam_u); - // if we already have pending request + // if we dont have pending request noop if ( NULL == req_u ) { return; - } + req_u->wat_u[nam_u->fra_w].sen_d = now_d; req_u->wat_u[nam_u->fra_w].tie_y++; } @@ -1377,6 +1406,7 @@ _xmas_req_pact_sent(u3_xmas* sam_u, u3_xmas_name* nam_u) req_u->was_u.buf_y = NULL; req_u->lef_w = 0; req_u->old_w = 1; + req_u->ack_w = 0; } if ( req_u->lef_w != 0 && c3n == _has_bitset(&req_u->was_u, req_u->lef_w) ) { @@ -1589,15 +1619,26 @@ _update_oldest_req(u3_pend_req *req_u, u3_gage* gag_u) } } if ( now_d == wen_d ) { +#ifdef XMAS_DEBUG u3l_log("failed to find new oldest"); _log_bitset(&req_u->was_u); - +#endif } req_u->old_w = idx_w; req_u->tim_u.data = req_u; uv_timer_start(&req_u->tim_u, _xmas_packet_timeout, ((req_u->wat_u[req_u->old_w].sen_d + gag_u->rto_w) / 1000), 0); } +static void _xmas_send(u3_xmas_pact* pac_u, u3_lane* lan_u) +{ + u3_xmas* sam_u = pac_u->sam_u; + + c3_y* buf_y = c3_calloc(PACT_SIZE); + c3_w siz_w = _xmas_etch_pact(buf_y, pac_u); + + _xmas_send_buf(sam_u, *lan_u, buf_y, siz_w); +} + static void _try_resend(u3_pend_req* req_u) @@ -1612,32 +1653,16 @@ _try_resend(u3_pend_req* req_u) c3_d now_d = _get_now_micros(); for( int i = req_u->lef_w; i < ack_w; i++ ) { if ( c3y == _has_bitset(&req_u->was_u, i) ) { + req_u->pac_u->pek_u.nam_u.fra_w = i; if ( req_u->wat_u[i].tie_y == 1 ) { - los_o = c3y; - c3_y* buf_y = c3_calloc(PACT_SIZE); - req_u->pac_u->pek_u.nam_u.fra_w = i; - c3_w siz_w = _xmas_etch_pact(buf_y, req_u->pac_u, 0); - if( siz_w == 0 ) { - u3l_log("failed to etch"); - u3_assert( 0 ); - } - // TODO: better route management - _xmas_send_buf(sam_u, *lan_u, buf_y, siz_w); - _xmas_req_pact_resent(sam_u, &req_u->pac_u->pek_u.nam_u); - + los_o = c3y; + _xmas_send(req_u->pac_u, lan_u); + _xmas_req_pact_resent(sam_u, &req_u->pac_u->pek_u.nam_u); } else if ( (now_d - req_u->wat_u[i].sen_d) > req_u->gag_u->rto_w ) { - los_o = c3y; - c3_y* buf_y = c3_calloc(PACT_SIZE); - req_u->pac_u->pek_u.nam_u.fra_w = i; - u3l_log("slow resending %u ", i); - c3_w siz_w = _xmas_etch_pact(buf_y, req_u->pac_u, 0); - if( siz_w == 0 ) { - u3l_log("failed to etch"); - u3_assert( 0 ); - } - // TODO: better route management - _xmas_send_buf(sam_u, *lan_u, buf_y, siz_w); - _xmas_req_pact_resent(sam_u, &req_u->pac_u->pek_u.nam_u); + los_o = c3y; + u3l_log("slow resending %u ", i); + _xmas_send(req_u->pac_u, lan_u); + _xmas_req_pact_resent(sam_u, &req_u->pac_u->pek_u.nam_u); } } } @@ -1657,8 +1682,13 @@ _xmas_req_pact_done(u3_xmas* sam_u, u3_xmas_name *nam_u, u3_xmas_page_meat* mat_ u3_weak ret = u3_none; c3_d now_d = _get_now_micros(); u3_pend_req* req_u = _xmas_get_request(sam_u, nam_u); - - req_u->lan_u = *lan_u; + + if ( NULL == req_u ) { +#ifdef XMAS_DEBUG + u3l_log("xmas: attempting to mark packet done, no request"); +#endif + return u3_none; + } u3_gage* gag_u = _xmas_get_lane(sam_u, nam_u->her_d, lan_u); req_u->gag_u = gag_u; @@ -1666,17 +1696,20 @@ _xmas_req_pact_done(u3_xmas* sam_u, u3_xmas_name *nam_u, u3_xmas_page_meat* mat_ // first we hear from lane if ( gag_u == NULL ) { gag_u = alloca(sizeof(u3_gage)); + memset(gag_u, 0, sizeof(u3_gage)); _init_gage(gag_u); } - - - if ( NULL == req_u ) { -#ifdef XMAS_DEBUG - u3l_log("xmas: attempting to mark packet done, no request"); -#endif - return u3_none; + if ( gag_u->wnd_w > 20 ) { + if ( ( rand() % 10 ) == 0 ) { + //u3l_log("overloaded, pretending to drop %u", nam_u->fra_w); + return u3_none; + } } + + + req_u->lan_u = *lan_u; + c3_w siz_w = (1 << (nam_u->boq_y - 3)); // First packet received, instantiate request fully if ( req_u->tot_w == 0 ) { @@ -1730,22 +1763,7 @@ _xmas_req_pact_done(u3_xmas* sam_u, u3_xmas_name *nam_u, u3_xmas_page_meat* mat_ c3_d wen_d = _get_now_micros(); _try_resend(req_u); - /* - * // in-order - if ( nam_u->fra_w != 0 && ((nam_u->fra_w - req_u->lef_w) > 5) ) { - u3l_log("diff: %u", _abs(nam_u->fra_w - req_u->old_w); - c3_y* buf_y = c3_calloc(PACT_SIZE); - req_u->pac_u->pek_u.nam_u.fra_w = req_u->old_w; - u3l_log("resending %u on num %u", req_u->old_w, nam_u->fra_w); - c3_w siz_w = _xmas_etch_pact(buf_y, req_u->pac_u, 0); - if( siz_w == 0 ) { - u3l_log("failed to etch"); - u3_assert( 0 ); - } - // TODO: better route management - _xmas_send_buf(sam_u, *lan_u, buf_y, siz_w); - _xmas_req_pact_resent(sam_u, &req_u->pac_u->pek_u.nam_u); - }*/ + _update_oldest_req(req_u, gag_u); @@ -1762,24 +1780,8 @@ _xmas_req_pact_done(u3_xmas* sam_u, u3_xmas_name *nam_u, u3_xmas_page_meat* mat_ } -static void _xmas_send(u3_xmas_pact* pac_u) -{ - u3_xmas* sam_u = pac_u->sam_u; - //u3l_log("_ames_send %s %u", _str_typ(pac_u->hed_u.typ_y), - // pac_u->rut_u.lan_u.por_s); - c3_y* buf_y = c3_calloc(PACT_SIZE); - c3_w siz_w; - { - u3_noun who = 0; - - siz_w = _xmas_etch_pact(buf_y, pac_u, who); - } - - _xmas_send_buf(sam_u, pac_u->rut_u.lan_u, buf_y, siz_w); -} - static u3_lane _realise_lane(u3_noun lan) { u3_lane lan_u; @@ -1863,9 +1865,7 @@ static void _xmas_update_req_peek(u3_xmas_pact* pac_u) { u3_xmas* sam_u = pac_u->sam_u; - u3l_log("updating req"); _xmas_req_pact_sent(sam_u, &pac_u->pek_u.nam_u); - u3l_log("updated req"); // TODO: set timers } @@ -1873,7 +1873,6 @@ _xmas_update_req_peek(u3_xmas_pact* pac_u) static void _xmas_ef_send(u3_xmas* sam_u, u3_noun las, u3_noun pac) { - u3l_log("sending"); u3_noun len, dat; u3x_cell(pac, &len, &dat); c3_w len_w = u3r_met(3, dat); @@ -1890,12 +1889,10 @@ _xmas_ef_send(u3_xmas* sam_u, u3_noun las, u3_noun pac) _xmas_sift_pact(&pac_u, buf_y, len); if ( pac_u.hed_u.typ_y == PACT_PEEK ) { - sam_u->tim_d = get_millis(); + sam_u->tim_d = _get_now_micros(); _xmas_update_req_peek(&pac_u); } - - c3_o suc_o = c3n; u3_noun lan, t = las; _xmas_rout_bufs(sam_u, buf_y, len, las); @@ -2019,26 +2016,141 @@ _xmas_io_exit(u3_auto* car_u) uv_close(&sam_u->had_u, _xmas_exit_cb); } + +static void +_init_peer(u3_xmas* sam_u, u3_peer* per_u) +{ + per_u->sam_u = sam_u; + per_u->dir_u = (u3_lane){0,0}; + per_u->ind_u = (u3_lane){0,0}; + per_u->las_u = (u3_peer_last){0,0}; +} + +// TODO: all the her_p hashtable functions are not refcounted properly + +static u3_peer* +_xmas_get_peer_raw(u3_xmas* sam_u, u3_noun her) +{ + u3_peer* ret_u = NULL; + u3_weak res = u3h_git(sam_u->her_p, her); + + if ( res != u3_none && res != u3_nul ) { + ret_u = u3to(u3_peer, res); + } + + u3z(her); + return ret_u; + +} /* * RETAIN */ -static u3_weak -_xmas_get_peer(u3_xmas* sam_u, u3_noun her) +static u3_peer* +_xmas_get_peer(u3_xmas* sam_u, c3_d her_d[2]) { - return u3h_get(sam_u->her_p, her); + return _xmas_get_peer_raw(sam_u, u3i_chubs(2, her_d)); } + + /* - * RETAIN */ static void -_xmas_put_sponsee(u3_xmas* sam_u, u3_noun her, u3_noun las) +_xmas_put_peer_raw(u3_xmas* sam_u, u3_noun her, u3_peer* per_u) { - u3_noun val = u3nc(u3_nul, u3k(las)); + u3_peer* old_u = _xmas_get_peer_raw(sam_u, u3k(her)); + u3_peer* new_u = NULL; + + if ( old_u == NULL ) { + new_u = u3a_calloc(sizeof(u3_peer),1); + memcpy(new_u, per_u, sizeof(u3_peer)); + } else if ( new_u != old_u ) { + new_u = old_u; + memcpy(new_u, per_u, sizeof(u3_peer)); + } + + u3_noun val = u3of(u3_peer, new_u); u3h_put(sam_u->her_p, her, val); - u3z(val); + u3z(her); +} + +static void +_xmas_put_peer(u3_xmas* sam_u, c3_d her_d[2], u3_peer* per_u) +{ + _xmas_put_peer_raw(sam_u, u3i_chubs(2, her_d), per_u); } +/* + * RETAIN + */ +static void +_xmas_put_direct_lane(u3_xmas* sam_u, c3_d her_d[2], u3_lane* lan_u) +{ + u3_noun her = u3i_chubs(2, her_d); + u3_peer* per_u = _xmas_get_peer_raw(sam_u, u3k(her)); + if ( per_u == NULL ) { + per_u = alloca(sizeof(u3_peer)); + _init_peer(sam_u, per_u); + } + c3_d now_d = _get_now_micros(); + per_u->las_u.acc_d = now_d; + per_u->dir_u = *lan_u; + + _xmas_put_peer_raw(sam_u, her, per_u); +} + +static u3_lane +_xmas_get_direct_lane_raw(u3_xmas* sam_u, u3_noun her) +{ + if ( c3y == u3a_is_cat(her) && her < 256 ) { + c3_s por_s = _ames_czar_port(her); + return (u3_lane){sam_u->imp_u[her].pip_w, por_s}; + } + u3_peer* per_u = _xmas_get_peer_raw(sam_u, her); + if ( NULL == per_u ) { + return (u3_lane){0,0}; + } + return per_u->dir_u; +} + +static u3_lane +_xmas_get_direct_lane(u3_xmas* sam_u, c3_d her_d[2]) +{ + return _xmas_get_direct_lane_raw(sam_u, u3i_chubs(2, her_d)); +} + + +static u3_lane +_xmas_get_indirect_lane(u3_xmas* sam_u, u3_noun her, u3_noun lan) +{ + if ( c3y == u3a_is_cat(her) && her < 256 ) { + c3_s por_s = _ames_czar_port(her); + return (u3_lane){sam_u->imp_u[her].pip_w, por_s}; + } + u3_peer* per_u = _xmas_get_peer_raw(sam_u, her); + if ( NULL == per_u ) { + return (u3_lane){0,0}; + } + return per_u->ind_u; +} + +static void +_xmas_put_indirect_lane(u3_xmas* sam_u, c3_d her_d[2], u3_lane* lan_u) +{ + u3_noun her = u3i_chubs(2, her_d); + u3_peer* per_u = _xmas_get_peer_raw(sam_u, u3k(her)); + if ( per_u == NULL ) { + per_u = alloca(sizeof(u3_peer)); + _init_peer(sam_u, per_u); + } + c3_d now_d = _get_now_micros(); + per_u->las_u.acc_d = now_d; + per_u->ind_u = *lan_u; + + _xmas_put_peer_raw(sam_u, her, per_u); +} + + /* * RETAIN */ @@ -2105,7 +2217,7 @@ _xmas_put_cache(u3_xmas* sam_u, u3_xmas_name* nam_u, u3_noun val) /* _xmas_czar(): add packet to queue, possibly begin DNS resolution */ static void -_xmas_czar(u3_xmas_pact* pac_u) +_xmas_czar(u3_xmas_pact* pac_u, c3_y her_y) { u3_xmas* sam_u = pac_u->sam_u; #ifdef XMAS_DEBUG @@ -2115,12 +2227,16 @@ _xmas_czar(u3_xmas_pact* pac_u) return; } #endif + u3_czar_info* her_u = &sam_u->imp_u[her_y]; + if ( her_u->pip_w == 0 ) { + c3_y* buf_y = c3_calloc(PACT_SIZE); + c3_w siz_w = _xmas_etch_pact(buf_y, pac_u); + her_u->pen = u3nc(u3i_bytes(siz_w, buf_y), her_u->pen); + } u3_noun pat = u3i_string(pac_u->pek_u.nam_u.pat_c); - c3_y her_y = pac_u->rut_u.imp_y; - - u3_weak her = _xmas_get_peer(sam_u, her_y); + //u3_weak her = _xmas_get_peer(sam_u, her_y); c3_o lok_o = _xmas_add_galaxy_pend(sam_u, her_y, pat); @@ -2232,6 +2348,50 @@ _xmas_hear_bail(u3_ovum* egg_u, u3_noun lud) } +static void +_saxo_cb(void* vod_p, u3_noun nun) +{ + u3_peer* per_u = vod_p; + u3_xmas* sam_u = per_u->sam_u; + u3_weak sax = u3r_at(7, nun); + + if ( sax != u3_none ) { + u3_noun gal = u3do("rear", u3k(sax)); + u3_assert( c3y == u3a_is_cat(gal) && gal < 256 ); + // both atoms guaranteed to be cats, bc we don't call unless forwarding + per_u->imp_y = gal; + } + + u3z(nun); +} + +static void +_meet_peer(u3_xmas* sam_u, u3_peer* per_u, c3_d her_d[2]) +{ + c3_d now_d = _get_now_micros(); + per_u->las_u.son_d = now_d; + + u3_noun her = u3i_chubs(2, her_d); + u3_noun gan = u3nc(u3_nul, u3_nul); + u3_noun pax = u3nc(u3dc("scot", c3__p, her), u3_nul); + u3_pier_peek_last(sam_u->pir_u, gan, c3__j, c3__saxo, pax, per_u, _saxo_cb); +} + +static void +_hear_peer(u3_xmas* sam_u, u3_peer* per_u, u3_lane lan_u, c3_o dir_o) +{ + c3_d now_d = _get_now_micros(); + per_u->las_u.acc_d = now_d; + if ( c3y == dir_o ) { + per_u->dir_u = lan_u; + } else { + per_u->ind_u = lan_u; + } +} + + + + static void _xmas_hear_page(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w, u3_lane lan_u) @@ -2242,6 +2402,26 @@ _xmas_hear_page(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w, u3_lane lan_u) u3_xmas* sam_u = pac_u->sam_u; u3_noun wir = u3nc(c3__xmas, u3_nul); c3_s fra_s; + + u3_peer* per_u = _xmas_get_peer(sam_u, pac_u->pag_u.nam_u.her_d); + c3_o new_o = c3n; + if ( NULL == per_u ) { + new_o = c3y; + per_u = alloca(sizeof(u3_peer)); + _init_peer(sam_u, per_u); + _meet_peer(sam_u, per_u, pac_u->pag_u.nam_u.her_d); + } + + c3_o dir_o = __(pac_u->hed_u.hop_y == 0); + if ( pac_u->hed_u.hop_y == 0 ) { + _hear_peer(sam_u, per_u, lan_u, dir_o); + } else { + u3l_log("received forwarded peek"); + } + if ( new_o == c3y ) { + u3l_log("new lane is direct %c", c3y == dir_o ? 'y' : 'n'); + _log_lane(&lan_u); + } u3_noun fra = u3i_bytes(pac_u->pag_u.mat_u.len_w, pac_u->pag_u.mat_u.fra_y) ; /*if ( dop_o == c3n && pac_u->pag_u.nam_u.fra_w == 150) { @@ -2251,13 +2431,12 @@ _xmas_hear_page(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w, u3_lane lan_u) }*/ u3_weak res = _xmas_req_pact_done(sam_u, &pac_u->pag_u.nam_u, &pac_u->pag_u.mat_u, &lan_u); - u3l_log("done"); if ( u3_none != res ) { u3l_log("finished"); - c3_d now_d = get_millis(); - u3l_log("took: %" PRIu64 " ms", now_d - sam_u->tim_d); + c3_d now_d = _get_now_micros(); + u3l_log("took: %f ms", (now_d - sam_u->tim_d)/1000.0f); return; } @@ -2273,7 +2452,7 @@ _xmas_hear_page(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w, u3_lane lan_u) c3_y* cur_y = buf_y; for(int i = 0; i < win_w; i++) { nex_u->pek_u.nam_u.fra_w = nex_w + i; - c3_w siz_w =_xmas_etch_pact(cur_y, nex_u, 0); + c3_w siz_w =_xmas_etch_pact(cur_y, nex_u); if( siz_w == 0 ) { u3l_log("failed to etch"); u3_assert( 0 ); @@ -2297,8 +2476,31 @@ _xmas_add_lane_to_cache(u3_xmas* sam_u, u3_xmas_name* nam_u, u3_noun las, u3_lan u3z(las); } +static void +_xmas_forward(u3_xmas_pact* pac_u, u3_lane lan_u) +{ + + u3_xmas* sam_u = pac_u->sam_u; + c3_d her_d[2]; + _get_her(pac_u, her_d); + _update_hopcount(&pac_u->hed_u); + + if ( pac_u->hed_u.typ_y == PACT_PAGE ) { + u3l_log("should update next hop"); + } + + u3_peer* per_u = _xmas_get_peer(sam_u, her_d); + if ( NULL != per_u ) { + u3_lane lin_u = _xmas_get_direct_lane(sam_u, her_d); + if ( lin_u.pip_w != 0 ) { + _xmas_send(pac_u, &lin_u); + } + } + _xmas_pact_free(pac_u); +} + static void _xmas_hear_peek(u3_xmas_pact* pac_u, u3_lane lan_u) { @@ -2308,8 +2510,25 @@ _xmas_hear_peek(u3_xmas_pact* pac_u, u3_lane lan_u) u3l_log("%hu", lan_u.por_s); // u3_assert(pac_u->hed_u.typ_y == PACT_PEEK); #endif - u3_xmas* sam_u = pac_u->sam_u; + u3_peer* per_u = _xmas_get_peer(sam_u, pac_u->pek_u.nam_u.her_d); + c3_d her_d[2]; + memcpy(her_d, pac_u->pek_u.nam_u.her_d, 2*sizeof(c3_d)); + + if ( 0 != memcmp(her_d, sam_u->pir_u->who_d, 2*sizeof(c3_d)) ) { + if ( c3y == sam_u->for_o && her_d[0] == per_u->imp_y ) { +//#ifdef XMAS_DEBUG + u3l_log("xmas: forwarding peek"); + u3_lane lin_u = _xmas_get_direct_lane(sam_u, her_d); + _log_lane(&lin_u); +//#endif + _update_hopcount(&pac_u->hed_u); + _xmas_send(pac_u, &lin_u); + } + _xmas_pact_free(pac_u); + return; + } + u3_weak hit = _xmas_get_cache(sam_u, &pac_u->pek_u.nam_u); if ( u3_none != hit ) { u3_noun tag, dat; @@ -2318,10 +2537,10 @@ _xmas_hear_peek(u3_xmas_pact* pac_u, u3_lane lan_u) _xmas_add_lane_to_cache(sam_u, &pac_u->pek_u.nam_u, u3k(u3t(dat)), lan_u); } else if ( tag == XMAS_ITEM ) { c3_y* buf_y; - if ( rand() % 20 == 0) { - u3l_log("dropping packet %u", pac_u->pek_u.nam_u.fra_w); - return; - } +// if ( rand() % 20 == 0) { +// u3l_log("dropping packet %u", pac_u->pek_u.nam_u.fra_w); +// return; +// } c3_w len_w = _xmas_respond(pac_u, &buf_y, u3k(dat)); _xmas_send_buf(pac_u->sam_u, lan_u, buf_y, len_w); } else { @@ -2340,6 +2559,7 @@ _xmas_hear_peek(u3_xmas_pact* pac_u, u3_lane lan_u) // u3z(pax); } + static void _xmas_hear(u3_xmas* sam_u, u3_lane* lan_u, @@ -2374,7 +2594,14 @@ _xmas_hear(u3_xmas* sam_u, #ifdef XMAS_DEBUG //_log_peek_pact(&pac_u->pek_u); u3l_log("xmas: sifted packet"); -#endif +#endif + + c3_d her_d[2]; + _get_her(pac_u, her_d); + + if ( 0 != memcmp(her_d, sam_u->pir_u->who_d, 2*sizeof(c3_d)) ) { + + } switch ( pac_u->hed_u.typ_y ) { case PACT_PEEK: { @@ -2527,12 +2754,36 @@ u3_xmas_io_init(u3_pier* pir_u) sam_u->sil_u = u3s_cue_xeno_init(); sam_u->tes_u = ur_cue_test_init(); + + // Disable networking for fake ships // if ( c3y == sam_u->pir_u->fak_o ) { u3_Host.ops_u.net = c3n; } + sam_u->for_o = c3n; + { + u3_noun her = u3i_chubs(2, pir_u->who_d); + for (int i = 0; i < 256; i++) { + sam_u->imp_u[i].pen = u3_nul; + //sam_u.imp_u[i].tim = 0; + if ( u3_Host.ops_u.net == c3y ) { + sam_u->imp_u[i].pip_w = 0x7f000001; + } else { + sam_u->imp_u[i].pip_w = 0; + + } + } + + if ( c3y == u3a_is_cat(her) && her < 256 ) { + u3l_log("xmas: forwarding enabled"); + sam_u->for_o = c3y; + } + u3z(her); + } + + u3_auto* car_u = &sam_u->car_u; car_u->nam_m = c3__xmas; car_u->liv_o = c3y; @@ -2622,7 +2873,7 @@ _test_sift_page() mat_u->tot_w = 1000; c3_y* buf_y = c3_calloc(PACT_SIZE); - c3_w len_w =_xmas_etch_pact(buf_y, &pac_u, her); + c3_w len_w =_xmas_etch_pact(buf_y, &pac_u); u3_xmas_pact nex_u; memset(&nex_u, 0, sizeof(u3_xmas_pact)); _xmas_sift_pact(&nex_u, buf_y, len_w); From 05fb3305a7d95abfe8dca798f95d33944d0336c9 Mon Sep 17 00:00:00 2001 From: parallels Date: Thu, 8 Feb 2024 16:34:25 -0800 Subject: [PATCH 032/430] xmas: working relaying --- pkg/vere/io/xmas.c | 399 +++++++++++++++++++-------------------------- 1 file changed, 167 insertions(+), 232 deletions(-) diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c index 233973a1ce..c2896e7c63 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/xmas.c @@ -34,7 +34,7 @@ c3_o dop_o = c3n; // pending interest sentinels #define XMAS_ITEM 1 // cached item -#define XMAS_SCRY 2 // waiting on scry +#define XMAS_WAIT 2 // waiting on scry // routing table sentinels #define XMAS_CZAR 1 // pending dns lookup @@ -130,7 +130,7 @@ typedef struct _u3_peer { u3_peer_last las_u; // last check timestamps u3_lane dir_u; // direct lane (if any) u3_lane ind_u; // indirect lane (if any) - c3_y imp_y; // galaxy + c3_s imp_s; // galaxy u3_xmas* sam_u; // backpointer } u3_peer; @@ -244,17 +244,14 @@ typedef struct _u3_xmas_page_pact { } u3_xmas_page_pact; typedef struct _u3_xmas_poke_pact { - u3_xmas_peek_pact* pek_u; - c3_w tot_w; - c3_y aut_y[96]; // heap allocate?? - c3_y* dat_y; + u3_xmas_peek_pact pek_u; + u3_xmas_page_pact pag_u; } u3_xmas_poke_pact; typedef struct _u3_xmas_pact { u3_xmas_head hed_u; uv_udp_send_t snd_u; struct _u3_xmas* sam_u; - u3_xmas_rout rut_u; union { u3_xmas_poke_pact pok_u; u3_xmas_page_pact pag_u; @@ -315,8 +312,7 @@ _log_head(u3_xmas_head* hed_u) static void _log_lane(u3_lane* lan_u) { - c3_c* sip_c = inet_ntoa(inet_makeaddr(AF_INET, lan_u->pip_w)); - u3l_log("xmas: lane (%s,%u)", sip_c, lan_u->por_s); + u3l_log("xmas: lane (%s,%u)", u3r_string(u3dc("scot", c3__if, u3i_word(lan_u->pip_w))), lan_u->por_s); } static void @@ -351,7 +347,6 @@ _log_name(u3_xmas_name* nam_u) } - static void _log_peek_pact(u3_xmas_peek_pact* pac_u) { @@ -373,6 +368,13 @@ _log_page_pact(u3_xmas_page_pact *pac_u) u3l_log("frag len: %u", mat_u->len_w); } +static void +_log_poke_pact(u3_xmas_poke_pact* pac_u) { + _log_peek_pact(&pac_u->pek_u); + _log_page_pact(&pac_u->pag_u); +} + + static void _log_gage(u3_gage* gag_u) { @@ -387,6 +389,19 @@ _log_gage(u3_gage* gag_u) //u3l_log("algorithm: %s", gag_u->alg_c); } +static void _log_peer(u3_peer* per_u) +{ + if ( per_u == NULL ) { + u3l_log("NULL peer"); + return; + } + u3l_log("dir"); + _log_lane(&per_u->dir_u); + u3l_log("ind"); + _log_lane(&per_u->ind_u); + u3l_log("galaxy: %s", u3r_string(u3dc("scot", 'p', per_u->imp_s))); +} + static void _log_pend_req(u3_pend_req* req_u) { @@ -410,10 +425,9 @@ _log_pact(u3_xmas_pact* pac_u) case PACT_PAGE: { _log_page_pact(&pac_u->pag_u); break; - } default: { - u3l_log("logging not implemented for %i", pac_u->hed_u.typ_y); + _log_poke_pact(&pac_u->pok_u); break; } } @@ -441,7 +455,7 @@ _clamp_rto(c3_d rto_d) { static void _update_hopcount(u3_xmas_head* hed_u) { - hed_u->hop_y = c3_max(hed_u->hop_y, 7); + hed_u->hop_y = c3_max(hed_u->hop_y+1, 7); } static inline void @@ -461,7 +475,7 @@ _get_her(u3_xmas_pact* pac_u, c3_d* our_d) break; } case PACT_POKE: { - //memcpy(our_d, pac_u->pok_u.pek_u.nam_u.her_d, 2); + memcpy(our_d, pac_u->pok_u.pag_u.nam_u.her_d, 2); break; } } @@ -870,23 +884,12 @@ _xmas_sift_peek_pact(u3_xmas_peek_pact* pac_u, c3_y* buf_y, c3_w len_w) } static c3_w -_xmas_sift_poke_pact(u3_xmas_poke_pact* pac_u, c3_y* buf_y, c3_w len_w) +_xmas_sift_poke_pact(u3_xmas_poke_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, c3_w len_w) { c3_w cur_w = 0; - // Peek portion - pac_u->pek_u = c3_calloc(sizeof(*(pac_u->pek_u))); - cur_w += _xmas_sift_peek_pact(pac_u->pek_u, buf_y, len_w); - - // Total fragments - pac_u->tot_w = _ames_sift_word(buf_y + cur_w); - cur_w += 4; + cur_w += _xmas_sift_name(&pac_u->pek_u.nam_u, buf_y + cur_w, len_w - cur_w); + cur_w += _xmas_sift_page_pact(&pac_u->pag_u, hed_u, buf_y + cur_w, len_w - cur_w); - // Authenticator - memcpy(pac_u->aut_y, buf_y + cur_w, 96); - cur_w += 96; - - // Datum - memcpy(pac_u->dat_y, buf_y + cur_w, len_w - cur_w); return cur_w; } @@ -911,7 +914,7 @@ _xmas_sift_pact(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w) res_w = _xmas_sift_page_pact(&pac_u->pag_u, &hed_u, buf_y, len_w); } break; case PACT_POKE: { - res_w = _xmas_sift_poke_pact(&pac_u->pok_u, buf_y, len_w); + res_w = _xmas_sift_poke_pact(&pac_u->pok_u, &hed_u, buf_y, len_w); } break; default: { u3l_log("xmas: received unknown packet type"); @@ -1099,19 +1102,19 @@ static c3_w _xmas_etch_poke_pact(c3_y* buf_y, u3_xmas_poke_pact* pac_u, u3_xmas_head* hed_u) { c3_w cur_w = 0; + c3_w siz_w = _xmas_etch_name(buf_y + cur_w, &pac_u->pek_u.nam_u); + if ( siz_w == 0 ) { + return 0; + } + cur_w += siz_w; - // total - _ames_etch_word(buf_y + cur_w, pac_u->tot_w); - cur_w += 4; - - // auth - memcpy(buf_y + cur_w, pac_u->aut_y, 96); - cur_w += 96; - - // day - memcpy(buf_y + cur_w, pac_u->dat_y, PACT_SIZE - cur_w); - - return PACT_SIZE; + siz_w = _xmas_etch_page_pact(buf_y + cur_w, &pac_u->pag_u, hed_u); + if ( siz_w == 0 ) { + return 0; + } + cur_w += siz_w; + + return cur_w; } static c3_w @@ -1502,42 +1505,6 @@ static u3_noun _xmas_get_now() { } -// refcounted -static c3_o -_xmas_rout_pact(u3_xmas_pact* pac_u, u3_noun lan) { - u3_noun tag, val; - u3x_cell(lan, &tag, &val); - - u3_assert( (c3y == tag) || (c3n == tag) ); - c3_o suc_o = c3y; - if ( c3y == tag ) { - u3_assert( c3y == u3a_is_cat(val) ); - u3_assert( val < 256 ); - pac_u->rut_u.typ_y = ROUT_GALAXY; - pac_u->rut_u.imp_y = val; - } else { - u3_lane lan_u = u3_xmas_decode_lane(u3k(val)); - // if in local-only mode, don't send remote packets - // - if ( (c3n == u3_Host.ops_u.net) && (0x7f000001 != lan_u.pip_w) ) { - suc_o = c3n; - } - // if the lane is uninterpretable, silently drop the packet - // - else if ( 0 == lan_u.por_s ) { - if ( u3C.wag_w & u3o_verbose ) { - u3l_log("ames: inscrutable lane"); - } - suc_o = c3n; - } else { - pac_u->rut_u.typ_y = ROUT_OTHER; - pac_u->rut_u.lan_u = lan_u; - } - } - u3z(lan); - return suc_o; -} - static void _xmas_pact_free(u3_xmas_pact* pac_u) { @@ -1699,12 +1666,7 @@ _xmas_req_pact_done(u3_xmas* sam_u, u3_xmas_name *nam_u, u3_xmas_page_meat* mat_ memset(gag_u, 0, sizeof(u3_gage)); _init_gage(gag_u); } - if ( gag_u->wnd_w > 20 ) { - if ( ( rand() % 10 ) == 0 ) { - //u3l_log("overloaded, pretending to drop %u", nam_u->fra_w); - return u3_none; - } - } + //if ( gag_u->wnd_w > 20 ) { } @@ -1826,35 +1788,15 @@ _xmas_rout_bufs(u3_xmas* sam_u, c3_y* buf_y, c3_w len_w, u3_noun las) if ( lan_u.por_s == 0 ) { u3l_log("xmas: failed to realise lane"); } else { - _xmas_send_buf(sam_u, lan_u, buf_y, len_w); + c3_y* sen_y = c3_calloc(len_w); + memcpy(sen_y, buf_y, len_w); + _xmas_send_buf(sam_u, lan_u, sen_y, len_w); } } // u3z(las); return suc_o; } -static c3_o -_xmas_rout_pacs(u3_xmas_pact* pac_u, u3_noun las) -{ - c3_o suc_o = c3n; - u3_noun lan, t = las; - while ( t != u3_nul ) { - u3x_cell(t, &lan, &t); - // if ( c3n == u3r_cell(t, &lan, &t) ) { - // break; - // } - if ( c3n == _xmas_rout_pact(pac_u, u3k(lan)) ) { - u3l_log("xmas: failed to set route"); - } else { - u3l_log("xmas_send"); - //_xmas_send_buf(pac_u); - suc_o = c3y; - } - } - u3z(las); - return suc_o; -} - static void _xmas_timer_cb(uv_timer_t* tim_u) { u3_pend_req* req_u = tim_u->data; @@ -1899,44 +1841,6 @@ _xmas_ef_send(u3_xmas* sam_u, u3_noun las, u3_noun pac) u3z(pac); } -static void -_xmas_ef_peek(u3_xmas* sam_u, u3_noun las, u3_noun who, u3_noun pat, u3_noun cop) -{ - u3_noun boq, fra, her, rif; - u3x_cell(who, &her, &rif); - u3x_cell(cop, &boq, &fra); - - if ( c3n == sam_u->car_u.liv_o ) { - u3l_log("xmas: not yet live, dropping outbound\r"); - u3z(pat); - } else { - u3_xmas_pact* pac_u = c3_calloc(sizeof(*pac_u)); - pac_u->hed_u.typ_y = PACT_PEEK; - pac_u->sam_u = sam_u; - { - u3_noun pas = u3do("spat", pat); - pac_u->pek_u.nam_u.pat_c = u3r_string(pas); - pac_u->pek_u.nam_u.pat_s = strlen(pac_u->pek_u.nam_u.pat_c); - u3z(pas); - } - u3r_chubs(0, 2, pac_u->pek_u.nam_u.her_d, her); - pac_u->pek_u.nam_u.rif_w = u3r_word(0, rif); - pac_u->pek_u.nam_u.boq_y = u3r_byte(0, boq); - - if( 384 < pac_u->pek_u.nam_u.pat_s ) { - u3l_log("xmas: path in peek too long"); - _xmas_pact_free(pac_u); - } else { - c3_o rut_o = _xmas_rout_pacs(pac_u, u3k(las)); - - if ( c3n == rut_o ) { - _xmas_pact_free(pac_u); - } - } - } - // do not need to lose pat because moved in spat call - u3z(who); u3z(cop); -} static c3_o _xmas_kick(u3_xmas* sam_u, u3_noun tag, u3_noun dat) @@ -2021,6 +1925,8 @@ static void _init_peer(u3_xmas* sam_u, u3_peer* per_u) { per_u->sam_u = sam_u; + + per_u->imp_s = 256; per_u->dir_u = (u3_lane){0,0}; per_u->ind_u = (u3_lane){0,0}; per_u->las_u = (u3_peer_last){0,0}; @@ -2080,24 +1986,6 @@ _xmas_put_peer(u3_xmas* sam_u, c3_d her_d[2], u3_peer* per_u) _xmas_put_peer_raw(sam_u, u3i_chubs(2, her_d), per_u); } -/* - * RETAIN - */ -static void -_xmas_put_direct_lane(u3_xmas* sam_u, c3_d her_d[2], u3_lane* lan_u) -{ - u3_noun her = u3i_chubs(2, her_d); - u3_peer* per_u = _xmas_get_peer_raw(sam_u, u3k(her)); - if ( per_u == NULL ) { - per_u = alloca(sizeof(u3_peer)); - _init_peer(sam_u, per_u); - } - c3_d now_d = _get_now_micros(); - per_u->las_u.acc_d = now_d; - per_u->dir_u = *lan_u; - - _xmas_put_peer_raw(sam_u, her, per_u); -} static u3_lane _xmas_get_direct_lane_raw(u3_xmas* sam_u, u3_noun her) @@ -2134,22 +2022,6 @@ _xmas_get_indirect_lane(u3_xmas* sam_u, u3_noun her, u3_noun lan) return per_u->ind_u; } -static void -_xmas_put_indirect_lane(u3_xmas* sam_u, c3_d her_d[2], u3_lane* lan_u) -{ - u3_noun her = u3i_chubs(2, her_d); - u3_peer* per_u = _xmas_get_peer_raw(sam_u, u3k(her)); - if ( per_u == NULL ) { - per_u = alloca(sizeof(u3_peer)); - _init_peer(sam_u, per_u); - } - c3_d now_d = _get_now_micros(); - per_u->las_u.acc_d = now_d; - per_u->ind_u = *lan_u; - - _xmas_put_peer_raw(sam_u, her, per_u); -} - /* * RETAIN @@ -2320,7 +2192,7 @@ _xmas_page_scry_cb(void* vod_p, u3_noun nun) u3_noun tag; u3_noun dat; u3x_cell(u3k(old), &tag, &dat); - if ( XMAS_SCRY == tag ) { + if ( XMAS_WAIT == tag ) { c3_y* buf_y; c3_w len_w = _xmas_respond(pac_u, &buf_y, u3k(hit)); @@ -2352,14 +2224,20 @@ static void _saxo_cb(void* vod_p, u3_noun nun) { u3_peer* per_u = vod_p; - u3_xmas* sam_u = per_u->sam_u; u3_weak sax = u3r_at(7, nun); if ( sax != u3_none ) { + u3_noun her = u3do("head", u3k(sax)); + u3_peer* new_u = _xmas_get_peer_raw(per_u->sam_u, u3k(her)); + if ( new_u != NULL ) { + per_u = new_u; + } + u3_xmas* sam_u = per_u->sam_u; u3_noun gal = u3do("rear", u3k(sax)); u3_assert( c3y == u3a_is_cat(gal) && gal < 256 ); // both atoms guaranteed to be cats, bc we don't call unless forwarding - per_u->imp_y = gal; + per_u->imp_s = gal; + _xmas_put_peer_raw(per_u->sam_u, her, per_u); } u3z(nun); @@ -2374,6 +2252,7 @@ _meet_peer(u3_xmas* sam_u, u3_peer* per_u, c3_d her_d[2]) u3_noun her = u3i_chubs(2, her_d); u3_noun gan = u3nc(u3_nul, u3_nul); u3_noun pax = u3nc(u3dc("scot", c3__p, her), u3_nul); + u3m_p("meet peer", pax); u3_pier_peek_last(sam_u->pir_u, gan, c3__j, c3__saxo, pax, per_u, _saxo_cb); } @@ -2389,8 +2268,47 @@ _hear_peer(u3_xmas* sam_u, u3_peer* per_u, u3_lane lan_u, c3_o dir_o) } } +static void +_xmas_forward(u3_xmas_pact* pac_u, u3_lane lan_u) +{ + + u3_xmas* sam_u = pac_u->sam_u; + c3_d her_d[2]; + _get_her(pac_u, her_d); + _update_hopcount(&pac_u->hed_u); + + if ( pac_u->hed_u.typ_y == PACT_PAGE ) { + //u3l_log("should update next hop"); + u3_weak hit = _xmas_get_cache(sam_u, &pac_u->pag_u.nam_u); + if ( u3_none == hit ) { + u3l_log("xmas: got forward, no interest"); + return; + } + u3_noun tag, dat; + u3x_cell(u3k(hit), &tag, &dat); + if ( tag == XMAS_WAIT ) { + u3m_p("lanes", u3t(dat)); + c3_y* buf_y = c3_calloc(PACT_SIZE); + c3_w len_w = _xmas_etch_pact(buf_y, pac_u); + _xmas_rout_bufs(sam_u, buf_y, len_w, u3k(u3t(dat))); + } else { + u3l_log("xmas: weird pending interest"); + } + } else { + u3_peer* per_u = _xmas_get_peer(sam_u, her_d); + if ( NULL != per_u ) { + u3_lane lin_u = _xmas_get_direct_lane(sam_u, her_d); + if ( lin_u.pip_w != 0 ) { + _xmas_send(pac_u, &lin_u); + } + } + } + + _xmas_pact_free(pac_u); +} + static void @@ -2407,7 +2325,7 @@ _xmas_hear_page(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w, u3_lane lan_u) c3_o new_o = c3n; if ( NULL == per_u ) { new_o = c3y; - per_u = alloca(sizeof(u3_peer)); + per_u = c3_calloc(sizeof(u3_peer)); _init_peer(sam_u, per_u); _meet_peer(sam_u, per_u, pac_u->pag_u.nam_u.her_d); } @@ -2422,6 +2340,14 @@ _xmas_hear_page(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w, u3_lane lan_u) u3l_log("new lane is direct %c", c3y == dir_o ? 'y' : 'n'); _log_lane(&lan_u); } + + _xmas_put_peer(sam_u, pac_u->pag_u.nam_u.her_d, per_u); + u3_weak hit = _xmas_get_cache(sam_u, &pac_u->pag_u.nam_u); + + if ( u3_none != hit ) { + _xmas_forward(pac_u, lan_u); + return; + } u3_noun fra = u3i_bytes(pac_u->pag_u.mat_u.len_w, pac_u->pag_u.mat_u.fra_y) ; /*if ( dop_o == c3n && pac_u->pag_u.nam_u.fra_w == 150) { @@ -2468,7 +2394,7 @@ _xmas_hear_page(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w, u3_lane lan_u) static void _xmas_add_lane_to_cache(u3_xmas* sam_u, u3_xmas_name* nam_u, u3_noun las, u3_lane lan_u) { - u3_noun hit = u3nq(XMAS_SCRY, + u3_noun hit = u3nq(XMAS_WAIT, _xmas_get_now(), u3_xmas_encode_lane(lan_u), u3k(las)); @@ -2476,89 +2402,105 @@ _xmas_add_lane_to_cache(u3_xmas* sam_u, u3_xmas_name* nam_u, u3_noun las, u3_lan u3z(las); } -static void -_xmas_forward(u3_xmas_pact* pac_u, u3_lane lan_u) -{ - - u3_xmas* sam_u = pac_u->sam_u; - c3_d her_d[2]; - _get_her(pac_u, her_d); - _update_hopcount(&pac_u->hed_u); - - if ( pac_u->hed_u.typ_y == PACT_PAGE ) { - u3l_log("should update next hop"); - } - - u3_peer* per_u = _xmas_get_peer(sam_u, her_d); - if ( NULL != per_u ) { - u3_lane lin_u = _xmas_get_direct_lane(sam_u, her_d); - if ( lin_u.pip_w != 0 ) { - _xmas_send(pac_u, &lin_u); - } - } - _xmas_pact_free(pac_u); -} - static void _xmas_hear_peek(u3_xmas_pact* pac_u, u3_lane lan_u) { #ifdef XMAS_DEBUG u3l_log("xmas: hear peek"); - _log_name(&pac_u->pek_u.nam_u); - u3l_log("%hu", lan_u.por_s); // u3_assert(pac_u->hed_u.typ_y == PACT_PEEK); #endif u3_xmas* sam_u = pac_u->sam_u; - u3_peer* per_u = _xmas_get_peer(sam_u, pac_u->pek_u.nam_u.her_d); c3_d her_d[2]; - memcpy(her_d, pac_u->pek_u.nam_u.her_d, 2*sizeof(c3_d)); + her_d[0] = 0; + her_d[1] = 0; + memcpy(her_d, pac_u->pek_u.nam_u.her_d, 2); + c3_o our_o = __( 0 == memcmp(her_d, sam_u->pir_u->who_d, 2) ); + - if ( 0 != memcmp(her_d, sam_u->pir_u->who_d, 2*sizeof(c3_d)) ) { - if ( c3y == sam_u->for_o && her_d[0] == per_u->imp_y ) { + if ( c3n == our_o ) { + u3_peer* per_u = _xmas_get_peer(sam_u, her_d); + if ( per_u == NULL ) { + u3l_log("xmas: alien forward for %s", u3r_string(u3dc("scot", c3__p, u3i_chubs(2, her_d)))); + _xmas_pact_free(pac_u); + return; + } + if ( c3y == sam_u->for_o && sam_u->pir_u->who_d[0] == per_u->imp_s ) { //#ifdef XMAS_DEBUG - u3l_log("xmas: forwarding peek"); u3_lane lin_u = _xmas_get_direct_lane(sam_u, her_d); - _log_lane(&lin_u); //#endif _update_hopcount(&pac_u->hed_u); + u3l_log("sending peek %u", pac_u->pek_u.nam_u.fra_w); _xmas_send(pac_u, &lin_u); } - _xmas_pact_free(pac_u); - return; } u3_weak hit = _xmas_get_cache(sam_u, &pac_u->pek_u.nam_u); + if ( u3_none != hit ) { u3_noun tag, dat; u3x_cell(u3k(hit), &tag, &dat); - if ( tag == XMAS_SCRY ) { + if ( tag == XMAS_WAIT ) { _xmas_add_lane_to_cache(sam_u, &pac_u->pek_u.nam_u, u3k(u3t(dat)), lan_u); - } else if ( tag == XMAS_ITEM ) { + } else if ( c3y == our_o && tag == XMAS_ITEM ) { c3_y* buf_y; // if ( rand() % 20 == 0) { // u3l_log("dropping packet %u", pac_u->pek_u.nam_u.fra_w); // return; // } c3_w len_w = _xmas_respond(pac_u, &buf_y, u3k(dat)); + u3l_log("sending page %u", pac_u->pek_u.nam_u.fra_w); _xmas_send_buf(pac_u->sam_u, lan_u, buf_y, len_w); } else { u3l_log("xmas: weird case in cache, dropping"); } + _xmas_pact_free(pac_u); // u3z(hit); } else { _xmas_add_lane_to_cache(sam_u, &pac_u->pek_u.nam_u, u3_nul, lan_u); // TODO: retrieve from namespace - u3_noun sky = _name_to_scry(&pac_u->pek_u.nam_u); + if ( c3y == our_o ) { + u3_noun sky = _name_to_scry(&pac_u->pek_u.nam_u); - u3_noun our = u3i_chubs(2, sam_u->car_u.pir_u->who_d); - u3_noun bem = u3nc(u3nt(our, u3_nul, u3nc(c3__ud, 1)), sky); - u3_pier_peek(sam_u->car_u.pir_u, u3_nul, u3k(u3nq(1, c3__beam, c3__xx, bem)), pac_u, _xmas_page_scry_cb); - // u3_pier_peek_last(sam_u->car_u.pir_u, u3_nul, c3__xx, u3_nul, sky, pac_u, _xmas_page_scry_cb); + u3_noun our = u3i_chubs(2, sam_u->car_u.pir_u->who_d); + u3_noun bem = u3nc(u3nt(our, u3_nul, u3nc(c3__ud, 1)), sky); + u3_pier_peek(sam_u->car_u.pir_u, u3_nul, u3k(u3nq(1, c3__beam, c3__xx, bem)), pac_u, _xmas_page_scry_cb); + } else { + _xmas_pact_free(pac_u); + } } // u3z(pax); } +// xx: should inject event directly, but vane does not work +// so we just hack it to get +static void +_xmas_hear_poke(u3_xmas_pact* pac_u, u3_lane* lan_u) +{ + u3_xmas* sam_u = pac_u->sam_u; + u3_peer* per_u = _xmas_get_peer(sam_u, pac_u->pok_u.pag_u.nam_u.her_d); + c3_o new_o = c3n; + if ( NULL == per_u ) { + new_o = c3y; + per_u = c3_calloc(sizeof(u3_peer)); + _init_peer(sam_u, per_u); + _meet_peer(sam_u, per_u, pac_u->pok_u.pag_u.nam_u.her_d); + } + + c3_o dir_o = __(pac_u->hed_u.hop_y == 0); + if ( pac_u->hed_u.hop_y == 0 ) { + new_o = c3y; + _hear_peer(sam_u, per_u, *lan_u, dir_o); + u3l_log("learnt lane"); + } else { + u3l_log("received forwarded peek"); + } + if ( new_o == c3y ) { + u3l_log("new lane is direct %c", c3y == dir_o ? 'y' : 'n'); + _log_lane(lan_u); + } + _xmas_put_peer(sam_u, pac_u->pok_u.pag_u.nam_u.her_d, per_u); +} static void _xmas_hear(u3_xmas* sam_u, @@ -2584,7 +2526,7 @@ _xmas_hear(u3_xmas* sam_u, if ( lin_w == 0 ) { #ifdef XMAS_DEBUG u3l_log("xmas: failed to parse packet"); - _log_pact(pac_u); + //_log_pact(pac_u); #endif // TODO free everything return; @@ -2596,13 +2538,6 @@ _xmas_hear(u3_xmas* sam_u, u3l_log("xmas: sifted packet"); #endif - c3_d her_d[2]; - _get_her(pac_u, her_d); - - if ( 0 != memcmp(her_d, sam_u->pir_u->who_d, 2*sizeof(c3_d)) ) { - - } - switch ( pac_u->hed_u.typ_y ) { case PACT_PEEK: { _xmas_hear_peek(pac_u, *lan_u); @@ -2611,7 +2546,7 @@ _xmas_hear(u3_xmas* sam_u, _xmas_hear_page(pac_u, hun_y, len_w, *lan_u); } break; default: { - u3l_log("xmas: unimplemented packet type %u", pac_u->hed_u.typ_y); + _xmas_hear_poke(pac_u, lan_u); } break; } } From 14fa31e738c876e2fe8a21d99ba3894d11ede7bc Mon Sep 17 00:00:00 2001 From: parallels Date: Fri, 9 Feb 2024 11:58:45 -0800 Subject: [PATCH 033/430] hashtable: add working but leaky u3h_del --- pkg/noun/hashtable.c | 123 +++++++++++++++++++++++++++++++++++++ pkg/noun/hashtable.h | 7 +++ pkg/noun/hashtable_tests.c | 54 ++++++++++++++++ pkg/vere/io/xmas.c | 6 +- 4 files changed, 187 insertions(+), 3 deletions(-) diff --git a/pkg/noun/hashtable.c b/pkg/noun/hashtable.c index a21352bf82..d8d57a09c9 100644 --- a/pkg/noun/hashtable.c +++ b/pkg/noun/hashtable.c @@ -270,6 +270,129 @@ u3h_put(u3p(u3h_root) har_p, u3_noun key, u3_noun val) } } +static c3_o _ch_slot_del(u3h_slot*, u3_noun, c3_w, c3_w, c3_w*); + +/* _ch_buck_del(): delete from bucket +*/ +static c3_o +_ch_buck_del(u3h_slot* sot_w, u3_noun key, c3_w *use_w) +{ + u3h_buck* hab_u = u3h_slot_to_node(*sot_w); + c3_w fin_w = hab_u->len_w; + c3_w i_w; + // + // find index of key to be deleted + // + for ( i_w = 0; i_w < hab_u->len_w; i_w++ ) { + u3_noun kov = u3h_slot_to_noun(hab_u->sot_w[i_w]); + if ( c3y == u3r_sing(key, u3h(kov)) ) { + fin_w = i_w; + //u3z(kov); + break; + } + } + + // no key found, no-op + if ( fin_w == hab_u->len_w ) { + return c3n; + } + + { + hab_u->len_w--; + u3_assert(c3y == u3h_slot_is_noun(hab_u->sot_w[fin_w])); + //u3z(u3h_slot_to_noun(hab_u->sot_w[fin_w])); + for ( i_w = fin_w; i_w < hab_u->len_w; i_w++ ) { + hab_u->sot_w[i_w] = hab_u->sot_w[i_w + 1]; + } + + *use_w -= 1; + return c3y; + } +} + +static c3_o _ch_some_del(u3h_slot*, u3_noun, c3_w, c3_w, c3_w*); + +static c3_o +_ch_slot_del(u3h_slot* sot_w, u3_noun key, c3_w lef_w, c3_w rem_w, c3_w* use_w) { + if ( c3y == u3h_slot_is_noun(*sot_w) ) { + u3_noun kev = u3h_slot_to_noun(*sot_w); + *sot_w = 0; + //u3z(kev); + *use_w -= 1; + return c3y; + } else { + return _ch_some_del(sot_w, key, lef_w, rem_w, use_w); + } +} + +static c3_o +_ch_node_del(u3h_slot* sot_w, u3_noun key, c3_w lef_w, c3_w rem_w, c3_w* use_w) +{ + u3h_node* han_u = (u3h_node*) u3h_slot_to_node(*sot_w); + + c3_w bit_w, inx_w, map_w, i_w; + + lef_w -= 5; + bit_w = (rem_w >> lef_w); + rem_w = CUT_END(rem_w, lef_w); + map_w = han_u->map_w; + inx_w = _ch_popcount(CUT_END(map_w, bit_w)); + + // nothing at slot, no-op + if ( !BIT_SET(map_w, bit_w) ) { + return c3n; + } + c3_w ken_w; + u3h_slot kes_w; + + c3_w len_w = _ch_popcount(map_w); + if ( len_w == 2 && ((ken_w = (0 == inx_w) ? 1 : 0), + (kes_w = han_u->sot_w[ken_w]), + (c3y == u3h_slot_is_noun(kes_w))) ) { + *sot_w = kes_w; + u3h_slot don_w = han_u->sot_w[inx_w]; + u3_assert(c3y == u3h_slot_is_noun(don_w)); + //u3z(u3h_slot_to_noun(don_w)); + *use_w -= 1; + //u3a_wfree(han_u) + return c3y; + } + return _ch_slot_del(&han_u->sot_w[inx_w], key, lef_w, rem_w, use_w); +} + + +static c3_o +_ch_some_del(u3h_slot* sot_w, u3_noun key, c3_w lef_w, c3_w rem_w, c3_w* use_w) +{ + if ( 0 == lef_w ) { + return _ch_buck_del(sot_w, key, use_w); + } + //u3h_node* han_u = (u3h_node*)han_v; + //if ( _ch_popcount(han_u)) + return _ch_node_del(sot_w, key, lef_w, rem_w, use_w); +} + + + +/** TODO: this has a bug where the freeing a hamt that this has been called with failes + * most likelly memroy mgmt +*/ +void +u3h_del(u3p(u3h_root) har_p, u3_noun key) +{ + u3h_root* har_u = u3to(u3h_root, har_p); + c3_w mug_w = u3r_mug(key); + c3_w inx_w = (mug_w >> 25); + c3_w rem_w = CUT_END(mug_w, 25); + u3h_slot* sot_w = &(har_u->sot_w[inx_w]); + + if ( c3y == u3h_slot_is_null(*sot_w) ) { + return; + } else { + _ch_slot_del(sot_w, key, 25, rem_w, &(har_u->use_w)); + } +} + /* _ch_uni_with(): key/value callback, put into [*wit] */ static void diff --git a/pkg/noun/hashtable.h b/pkg/noun/hashtable.h index 69ba962dd9..ad04a79f5a 100644 --- a/pkg/noun/hashtable.h +++ b/pkg/noun/hashtable.h @@ -125,6 +125,13 @@ u3_weak u3h_git(u3p(u3h_root) har_p, u3_noun key); + /* u3h_del(); delete from hashtable. + ** + ** `key` is RETAINED + */ + void + u3h_del(u3p(u3h_root) har_p, u3_noun key); + /* u3h_trim_to(): trim to n key-value pairs */ void diff --git a/pkg/noun/hashtable_tests.c b/pkg/noun/hashtable_tests.c index 36afc07b2c..3a5ac09409 100644 --- a/pkg/noun/hashtable_tests.c +++ b/pkg/noun/hashtable_tests.c @@ -14,6 +14,59 @@ _setup(void) u3m_pave(c3y); } +/* _test_put_del(): +*/ +static c3_i +_test_put_del() +{ + u3p(u3h_root) har_p = u3h_new(); + c3_i ret_i = 1; + + c3_w i_w; + for ( i_w = 0; i_w < 5000; i_w++ ) { + u3_noun key = u3i_word(i_w); + u3_noun val = u3k(key); + u3h_put(har_p, key, val); + u3z(key); + } + fprintf(stderr, "inserted"); + + for ( i_w = 0; i_w < 5000; i_w++ ) { + u3_noun key = u3i_word(i_w); + u3_weak val = u3h_get(har_p, key); + if ( val == u3_none ) { + fprintf(stderr, "failed insert\r\n"); + ret_i = 0; + } + u3z(key); + u3z(val); + } + fprintf(stderr, "presence"); + c3_w del_w[4] = {30, 82, 4921, 535}; + + for ( i_w = 0; i_w < 4; i_w++ ) { + u3_noun key = u3i_word(del_w[i_w]); + u3h_del(har_p, key); + u3z(key); + } + fprintf(stderr, "deleted"); + + for ( i_w = 0; i_w < 4; i_w++ ) { + u3_noun key = u3i_word(del_w[i_w]); + u3_weak val = u3h_get(har_p, key); + if ( u3_none != val ) { + fprintf(stderr, "failed delete\r\n"); + ret_i = 0; + break; + } + } + fprintf(stderr, "presence two"); + u3h_free(har_p); + fprintf(stderr, "freed"); + + return ret_i; +} + /* _test_bit_manipulation(): */ static c3_i @@ -220,6 +273,7 @@ _test_hashtable(void) ret_i &= _test_skip_slot(); ret_i &= _test_cache_trimming(); ret_i &= _test_cache_replace_value(); + ret_i &= _test_put_del(); return ret_i; } diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c index c2896e7c63..7e143c4508 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/xmas.c @@ -2791,7 +2791,7 @@ _test_sift_page() { u3_xmas_pact pac_u; memset(&pac_u,0, sizeof(u3_xmas_pact)); - pac_u.typ_y = PACT_PAGE; + pac_u.hed_u.typ_y = PACT_PAGE; u3l_log("checking sift/etch idempotent"); u3_xmas_name* nam_u = &pac_u.pag_u.nam_u; u3_noun her = u3v_wish("~hastuc-dibtux"); @@ -2853,7 +2853,7 @@ _test_sift_peek() { u3_xmas_pact pac_u; memset(&pac_u,0, sizeof(u3_xmas_pact)); - pac_u.typ_y = PACT_PEEK;; + pac_u.hed_u.typ_y = PACT_PEEK;; u3l_log("checking sift/etch idempotent"); u3_xmas_name* nam_u = &pac_u.pek_u.nam_u; u3_noun her = u3v_wish("~hastuc-dibtux"); @@ -2865,7 +2865,7 @@ _test_sift_peek() nam_u->fra_w = 511; c3_y* buf_y = c3_calloc(PACT_SIZE); - c3_w len_w =_xmas_etch_pact(buf_y, &pac_u, her); + c3_w len_w =_xmas_etch_pact(buf_y, &pac_u); u3_xmas_pact nex_u; memset(&nex_u, 0, sizeof(u3_xmas_pact)); _xmas_sift_pact(&nex_u, buf_y, len_w); From 65466f6c173586249f949322fa921b1986ef22c8 Mon Sep 17 00:00:00 2001 From: parallels Date: Fri, 9 Feb 2024 13:49:07 -0800 Subject: [PATCH 034/430] hashtable: fix memory leak --- pkg/noun/hashtable.c | 37 +++++++++++++++++++++---------------- pkg/noun/hashtable_tests.c | 9 +++++---- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/pkg/noun/hashtable.c b/pkg/noun/hashtable.c index d8d57a09c9..3b15938a6c 100644 --- a/pkg/noun/hashtable.c +++ b/pkg/noun/hashtable.c @@ -287,7 +287,7 @@ _ch_buck_del(u3h_slot* sot_w, u3_noun key, c3_w *use_w) u3_noun kov = u3h_slot_to_noun(hab_u->sot_w[i_w]); if ( c3y == u3r_sing(key, u3h(kov)) ) { fin_w = i_w; - //u3z(kov); + u3z(kov); break; } } @@ -317,7 +317,7 @@ _ch_slot_del(u3h_slot* sot_w, u3_noun key, c3_w lef_w, c3_w rem_w, c3_w* use_w) if ( c3y == u3h_slot_is_noun(*sot_w) ) { u3_noun kev = u3h_slot_to_noun(*sot_w); *sot_w = 0; - //u3z(kev); + u3z(kev); *use_w -= 1; return c3y; } else { @@ -344,20 +344,25 @@ _ch_node_del(u3h_slot* sot_w, u3_noun key, c3_w lef_w, c3_w rem_w, c3_w* use_w) } c3_w ken_w; u3h_slot kes_w; - + u3h_slot don_w = han_u->sot_w[inx_w]; c3_w len_w = _ch_popcount(map_w); if ( len_w == 2 && ((ken_w = (0 == inx_w) ? 1 : 0), (kes_w = han_u->sot_w[ken_w]), - (c3y == u3h_slot_is_noun(kes_w))) ) { + (c3y == u3h_slot_is_noun(don_w) && c3y == u3h_slot_is_noun(kes_w))) ) { *sot_w = kes_w; - u3h_slot don_w = han_u->sot_w[inx_w]; - u3_assert(c3y == u3h_slot_is_noun(don_w)); - //u3z(u3h_slot_to_noun(don_w)); + u3z(u3h_slot_to_noun(don_w)); *use_w -= 1; - //u3a_wfree(han_u) + u3a_wfree(han_u); return c3y; } - return _ch_slot_del(&han_u->sot_w[inx_w], key, lef_w, rem_w, use_w); + c3_o ret_o = _ch_slot_del(&han_u->sot_w[inx_w], key, lef_w, rem_w, use_w); + if ( c3y == ret_o && c3y == u3h_slot_is_null(han_u->sot_w[inx_w]) ) { + han_u->map_w &= ~(1 << bit_w); + for ( i_w = inx_w; i_w < len_w; i_w++ ) { + han_u->sot_w[i_w] = han_u->sot_w[i_w + 1]; + } + } + return ret_o; } @@ -790,6 +795,7 @@ u3h_get(u3p(u3h_root) har_p, u3_noun key) static void _ch_free_buck(u3h_buck* hab_u) { + //fprintf(stderr, "free buck\r\n"); c3_w i_w; for ( i_w = 0; i_w < hab_u->len_w; i_w++ ) { @@ -801,7 +807,7 @@ _ch_free_buck(u3h_buck* hab_u) /* _ch_free_node(): free node. */ static void -_ch_free_node(u3h_node* han_u, c3_w lef_w) +_ch_free_node(u3h_node* han_u, c3_w lef_w, c3_o pin_o) { c3_w len_w = _ch_popcount(han_u->map_w); c3_w i_w; @@ -810,17 +816,16 @@ _ch_free_node(u3h_node* han_u, c3_w lef_w) for ( i_w = 0; i_w < len_w; i_w++ ) { c3_w sot_w = han_u->sot_w[i_w]; - - if ( _(u3h_slot_is_noun(sot_w)) ) { + if ( _(u3h_slot_is_null(sot_w))) { + } else if ( _(u3h_slot_is_noun(sot_w)) ) { u3z(u3h_slot_to_noun(sot_w)); - } - else { + } else { void* hav_v = u3h_slot_to_node(sot_w); if ( 0 == lef_w ) { _ch_free_buck(hav_v); } else { - _ch_free_node(hav_v, lef_w); + _ch_free_node(hav_v, lef_w, pin_o); } } } @@ -844,7 +849,7 @@ u3h_free(u3p(u3h_root) har_p) else if ( _(u3h_slot_is_node(sot_w)) ) { u3h_node* han_u = u3h_slot_to_node(sot_w); - _ch_free_node(han_u, 25); + _ch_free_node(han_u, 25, i_w == 57); } } u3a_wfree(har_u); diff --git a/pkg/noun/hashtable_tests.c b/pkg/noun/hashtable_tests.c index 3a5ac09409..dbd039816b 100644 --- a/pkg/noun/hashtable_tests.c +++ b/pkg/noun/hashtable_tests.c @@ -1,6 +1,7 @@ /// @file #include "noun.h" +#define TEST_SIZE 100000 // defined in noun/hashtable.c c3_w _ch_skip_slot(c3_w mug_w, c3_w lef_w); @@ -23,15 +24,15 @@ _test_put_del() c3_i ret_i = 1; c3_w i_w; - for ( i_w = 0; i_w < 5000; i_w++ ) { + for ( i_w = 0; i_w < TEST_SIZE; i_w++ ) { u3_noun key = u3i_word(i_w); - u3_noun val = u3k(key); + u3_noun val = u3nc(u3_nul, u3k(key)); u3h_put(har_p, key, val); u3z(key); } fprintf(stderr, "inserted"); - for ( i_w = 0; i_w < 5000; i_w++ ) { + for ( i_w = 0; i_w < TEST_SIZE; i_w++ ) { u3_noun key = u3i_word(i_w); u3_weak val = u3h_get(har_p, key); if ( val == u3_none ) { @@ -42,7 +43,7 @@ _test_put_del() u3z(val); } fprintf(stderr, "presence"); - c3_w del_w[4] = {30, 82, 4921, 535}; + c3_w del_w[4] = {30, 82, 4921, 535}; for ( i_w = 0; i_w < 4; i_w++ ) { u3_noun key = u3i_word(del_w[i_w]); From 0bd8a4765948e4c45df0ba37bd07d856f448b86e Mon Sep 17 00:00:00 2001 From: parallels Date: Thu, 22 Feb 2024 13:50:01 -0600 Subject: [PATCH 035/430] xmas: better memory mgmt --- pkg/vere/io/xmas.c | 87 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 73 insertions(+), 14 deletions(-) diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c index 7e143c4508..c25bd9385a 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/xmas.c @@ -223,23 +223,30 @@ typedef struct _u3_xmas_page_meat { c3_y* fra_y; // fragment } u3_xmas_page_meat; -typedef struct _u3_xmas_hop { +typedef struct _u3_xmas_hop_once { c3_w len_w; c3_y* dat_y; -} u3_xmas_hop; +} u3_xmas_hop_once; -typedef struct _u3_xmas_hops { +typedef struct _u3_xmas_hop_more { c3_w len_w; - u3_xmas_hop** dat_y; -} u3_xmas_hops; + u3_xmas_hop_once* dat_y; +} u3_xmas_hop_more; + +typedef union { + c3_y sot_u[6]; + u3_xmas_hop_once one_u; + u3_xmas_hop_more man_u; +} u3_xmas_hop; + typedef struct _u3_xmas_page_pact { u3_xmas_name nam_u; u3_xmas_page_meat mat_u; union { c3_y sot_u[6]; - u3_xmas_hop one_u; - u3_xmas_hops man_u; + u3_xmas_hop_once one_u; + u3_xmas_hop_more man_u; }; } u3_xmas_page_pact; @@ -433,6 +440,8 @@ _log_pact(u3_xmas_pact* pac_u) } } + + static c3_d _get_now_micros() { @@ -491,6 +500,12 @@ _init_bitset(u3_bitset* bit_u, c3_w len_w) bit_u->len_w = len_w; } +static void +_free_bitset(u3_bitset* bit_u) +{ + c3_free(bit_u->buf_y); +} + static c3_w _wyt_bitset(u3_bitset* bit_u) { @@ -762,7 +777,7 @@ _xmas_sift_name(u3_xmas_name* nam_u, c3_y* buf_y, c3_w len_w) } static c3_w -_xmas_sift_hop_long(u3_xmas_hop* hop_u, c3_y* buf_y, c3_w len_w) +_xmas_sift_hop_long(u3_xmas_hop_once* hop_u, c3_y* buf_y, c3_w len_w) { c3_w cur_w = 0; CHECK_BOUNDS(cur_w + 1); @@ -853,11 +868,10 @@ _xmas_sift_page_pact(u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, pac_u->man_u.len_w = buf_y[cur_w]; cur_w++; - pac_u->man_u.dat_y = c3_calloc(sizeof(u3_xmas_hop*) * pac_u->man_u.len_w); + pac_u->man_u.dat_y = c3_calloc(sizeof(u3_xmas_hop_once) * pac_u->man_u.len_w); for( int i = 0; i < pac_u->man_u.len_w; i++ ) { - pac_u->man_u.dat_y[i] = c3_calloc(sizeof(u3_xmas_hop)); - c3_w hop_w = _xmas_sift_hop_long(pac_u->man_u.dat_y[i], buf_y + cur_w ,len_w - cur_w); + c3_w hop_w = _xmas_sift_hop_long(&pac_u->man_u.dat_y[i], buf_y + cur_w ,len_w - cur_w); if ( hop_w == 0 ) { return 0; } @@ -1177,6 +1191,28 @@ static void _init_gage(u3_gage* gag_u) gag_u->sst_w = 10000; } +// xx: imcomplete +static void _xmas_pact_free(u3_xmas_pact* pac_u) { + switch ( pac_u->hed_u.typ_y ) { + default: { + u3m_bail(c3__foul); + }; + case PACT_PEEK: { + break; + }; + case PACT_PAGE: { + c3_free(pac_u->pag_u.mat_u.fra_y); + c3_free(pac_u->pag_u.mat_u.aut_y); + break; + }; + case PACT_POKE: { + break; + }; + } + c3_free(pac_u); +} + + /* u3_xmas_encode_lane(): serialize lane to noun */ static u3_noun @@ -1261,6 +1297,25 @@ _xmas_get_request(u3_xmas* sam_u, u3_xmas_name* nam_u) { return ret_u; } +static void +_xmas_del_request(u3_xmas* sam_u, u3_xmas_name* nam_u) { + u3_noun key = _xmas_request_key(nam_u); + + u3_weak req = u3h_get(sam_u->req_p, key); + if ( req == u3_none ) { + u3z(key); + return; + } + u3_pend_req* req_u = u3to(u3_pend_req, req); + _xmas_pact_free(req_u->pac_u); + _free_bitset(&req_u->was_u); + c3_free(req_u->pac_u); + c3_free(req_u->dat_y); + u3a_free(req_u); + u3h_del(sam_u->req_p, key); + u3z(key); +} + /* _xmas_put_request(): save new pending request state for nam_u * * the memory in the hashtable is allocated once in the lifecycle of the @@ -1507,9 +1562,6 @@ static u3_noun _xmas_get_now() { -static void _xmas_pact_free(u3_xmas_pact* pac_u) { - // TODO: i'm lazy -} static void @@ -1731,6 +1783,7 @@ _xmas_req_pact_done(u3_xmas* sam_u, u3_xmas_name *nam_u, u3_xmas_page_meat* mat_ if ( req_u->len_w == req_u->tot_w ) { uv_timer_stop(&req_u->tim_u); + _xmas_del_request(sam_u, nam_u); //u3dc("rap", 13, u3dc("turn", u3n_slam_on(u3k(sam_u->mop_u.tap), req_u->dat), u3v_wish("tail"))); //_xmas_put_request(sam_u, nam_u, u3_none); return u3i_word(req_u->tot_w); @@ -2690,6 +2743,12 @@ u3_xmas_io_init(u3_pier* pir_u) sam_u->tes_u = ur_cue_test_init(); + for ( c3_w i_w = 0; i_w < 5000; i_w++ ) { + u3_noun key = u3i_word(i_w); + u3_noun val = u3k(val); + + u3h_put(sam_u->req_p, key, val); + } // Disable networking for fake ships // From e4073d7060a5aad3f0313c5998cc93b9d83e97ae Mon Sep 17 00:00:00 2001 From: parallels Date: Thu, 22 Feb 2024 13:50:30 -0600 Subject: [PATCH 036/430] xmas: working impl of lss.go --- pkg/vere/io/blake.c | 541 +++++++++++++++++++++++++++++--------------- 1 file changed, 358 insertions(+), 183 deletions(-) diff --git a/pkg/vere/io/blake.c b/pkg/vere/io/blake.c index cdd02efb28..d7dfc0a34a 100644 --- a/pkg/vere/io/blake.c +++ b/pkg/vere/io/blake.c @@ -6,8 +6,12 @@ #include #include "urcrypt.h" -#define BLAKE_TEST #define BLAKE3_BLOCK_LEN 64 +#define BLAKE3_OUT_LEN 32 + +#define pf(label, ...) { fprintf(stderr, "%s (%u)\r\n", label, __LINE__); fprintf(stderr, __VA_ARGS__); } +#define pl { fprintf(stderr, "(%u)", __LINE__); } +#define pll(label) { fprintf(stderr, "%s (%u)\n", label, __LINE__); } enum blake3_flags { CHUNK_START = 1 << 0, @@ -20,17 +24,61 @@ enum blake3_flags { }; typedef struct _blake_node { - c3_w cev_w[8]; - c3_w boq_w[16]; + c3_y cev_y[BLAKE3_OUT_LEN]; + c3_y boq_y[BLAKE3_BLOCK_LEN]; c3_d con_d; - c3_w len_w; - c3_w fag_w; + c3_y len_y; + c3_y fag_y; } blake_node; +static void +_log_buf(c3_c* str_c, c3_y* buf_y, c3_w len_w) +{ + c3_w siz_w = 2*len_w + 1; + c3_c* res_c = c3_calloc(siz_w); + c3_w cur_w = 0; + c3_c tmp_c[3]; + for(c3_w idx_w = 0; idx_w < len_w; idx_w++ ) { + snprintf(res_c + (2*idx_w), siz_w - (2*idx_w), "%02x", buf_y[idx_w]); + } + fprintf(stderr, "%s: %s", str_c, res_c); + fprintf(stderr, "\r\n"); + c3_free(res_c); +} + +static void +_log_words(c3_c* str_c, c3_w* buf_w, c3_w len_w) +{ + fprintf(stderr, "%s\r\n", str_c); + for ( c3_w i_w = 0; i_w < len_w; i_w++ ) { + fprintf(stderr, "%u ", buf_w[i_w]); + } + fprintf(stderr, "\r\n"); +} + +static void +_log_node(blake_node* nod_u) +{ + _log_words("CV", (c3_w*)nod_u->cev_y, 8); + _log_words("block", (c3_w*)nod_u->boq_y, 16); + fprintf(stderr, "counter: %llu\r\n", nod_u->con_d); + fprintf(stderr, "length: %u\r\n", nod_u->len_y); + fprintf(stderr, "flag: %x\r\n", nod_u->fag_y); +} -static c3_w IV[8] = {0x6A09E667UL, 0xBB67AE85UL, 0x3C6EF372UL, 0xA54FF53AUL, 0x510E527FUL, 0x9B05688CUL, 0x1F83D9ABUL, 0x5BE0CD19UL}; +static void print_proof(void* vod_p, c3_w i_w) { + c3_y* pof_y = (c3_y*)vod_p; + fprintf(stderr, "proof: %u", i_w); + _log_words("", pof_y, BLAKE3_OUT_LEN/4); +} + + + + +// static c3_w IV_WORDS[8] = {0x6A09E667UL, 0xBB67AE85UL, 0x3C6EF372UL, 0xA54FF53AUL, 0x510E527FUL, 0x9B05688CUL, 0x1F83D9ABUL, 0x5BE0CD19UL}; +static c3_y IV[BLAKE3_OUT_LEN] = {103, 230, 9, 106, 133, 174, 103, 187, 114, 243, 110, 60, 58, 245, 79, 165, 127, 82, 14, 81, 140, 104, 5, 155, 171, 217, 131, 31, 25, 205, 224, 91}; #define ABS_DIFF(x,y) x > y ? x - y : y - x #define VEC_SCALING 2 @@ -50,6 +98,12 @@ _vec_init(u3_raw_vec* vec_u, c3_w siz_w) vec_u->vod_p = c3_calloc(siz_w * sizeof(void*)); vec_u->len_w = 0; } +static void +_vec_each(u3_raw_vec* vec_u, void (*func)(void*, c3_w)) { + for (c3_w i_w = 0; i_w < vec_u->len_w; i_w++) { + func(vec_u->vod_p[i_w], i_w); + } +} static void _vec_free(u3_raw_vec* vec_u) @@ -98,7 +152,6 @@ _vec_resize(u3_raw_vec* vec_u, c3_w siz_w) { u3_assert( vec_u->len_w <= siz_w ); void* vod_p = c3_calloc(siz_w * sizeof(void*)); - fprintf(stderr, "new size: %u", siz_w); memcpy(vod_p, vec_u->vod_p, vec_u->len_w * sizeof(void*)); c3_free(vec_u->vod_p); vec_u->vod_p = vod_p; @@ -136,6 +189,13 @@ _vec_popf(u3_raw_vec* vec_u) return _vec_pop(vec_u, 0); } +static void* +_vec_popl(u3_raw_vec* vec_u) +{ + return _vec_pop(vec_u, vec_u->len_w - 1); +} + + static void _vec_swap(u3_raw_vec* vec_u, c3_w fst_w, c3_w snd_w) { void* tmp_p = vec_u->vod_p[fst_w]; @@ -150,7 +210,9 @@ static void _vec_weld_mut(u3_raw_vec* fst_u, u3_raw_vec* snd_u) if ( fst_u->siz_w <= (fst_u->len_w + snd_u->len_w ) ) { _vec_resize(fst_u, (fst_u->len_w + snd_u->len_w) * VEC_SCALING ); } - memcpy(fst_u->vod_p + (fst_u->len_w * sizeof(void*)), snd_u->vod_p, snd_u->len_w ); + for ( c3_w i_w = 0; i_w < snd_u->len_w; i_w++ ) { + fst_u->vod_p[i_w + fst_u->len_w] = snd_u->vod_p[i_w]; + } fst_u->len_w += snd_u->len_w; } @@ -160,8 +222,8 @@ static void _vec_weld_mut(u3_raw_vec* fst_u, u3_raw_vec* snd_u) //type Pair [2][8]uint32 typedef struct _pair { - c3_w sin_w[8]; - c3_w dex_w[8]; + c3_y sin_y[BLAKE3_OUT_LEN]; + c3_y dex_y[BLAKE3_OUT_LEN]; } pair; //type subtree [2]int @@ -173,13 +235,56 @@ typedef struct _subtree { typedef struct _verifier { c3_d num_d; subtree sub_u; - u3_vec(c3_w[8]) que_u; - u3_vec(c3_w[8]) sta_u; + u3_vec(c3_y[BLAKE3_OUT_LEN]) que_u; + u3_vec(c3_w[BLAKE3_OUT_LEN]) sta_u; } verifier; -static c3_d _count_lead_zeros(c3_d bit_d) +static void +_log_subtree(subtree* sub_u) +{ + fprintf(stderr, "sub: (%llu, %llu)\r\n", sub_u->sin_d, sub_u->dex_d); +} + +static void +_log_verifier(verifier* ver_u) { - return (bit_d == 0) ? 64 : __builtin_clz(bit_d); + fprintf(stderr, "num: %llu\r\n", ver_u->num_d); + fprintf(stderr, "sub: (%llu, %llu)\r\n", ver_u->sub_u.sin_d, ver_u->sub_u.dex_d); + fprintf(stderr, "queue\r\n"); + _vec_each(&ver_u->que_u, print_proof); + fprintf(stderr, "stack\r\n"); + _vec_each(&ver_u->sta_u, print_proof); +} + +static c3_y +_count_lead_zeros(c3_d val_d) +{ + if (val_d == 0 ) { + return 64; + } + c3_y ret_y = 0; + if ((val_d & 0xFFFFFFFF00000000ULL) == 0) { ret_y += 32; val_d <<= 32; } + if ((val_d & 0xFFFF000000000000ULL) == 0) { ret_y += 16; val_d <<= 16; } + if ((val_d & 0xFF00000000000000ULL) == 0) { ret_y += 8; val_d <<= 8; } + if ((val_d & 0xF000000000000000ULL) == 0) { ret_y += 4; val_d <<= 4; } + if ((val_d & 0xC000000000000000ULL) == 0) { ret_y += 2; val_d <<= 2; } + if ((val_d & 0x8000000000000000ULL) == 0) { ret_y += 1; val_d <<= 1; } + return ret_y; +} + +static c3_y _count_trail_zero(c3_d val_d) +{ + if (val_d == 0 ) { + return 64; + } + c3_y ret_y = 0; + if ((val_d & 0x00000000FFFFFFFFULL) == 0) { ret_y += 32; val_d >>= 32; } + if ((val_d & 0x000000000000FFFFULL) == 0) { ret_y += 16; val_d >>= 16; } + if ((val_d & 0x00000000000000FFULL) == 0) { ret_y += 8; val_d >>= 8; } + if ((val_d & 0x000000000000000FULL) == 0) { ret_y += 4; val_d >>= 4; } + if ((val_d & 0x0000000000000003ULL) == 0) { ret_y += 2; val_d >>= 2; } + if ((val_d & 0x0000000000000001ULL) == 0) { ret_y += 1; val_d >>= 1; } + return ret_y; } static c3_d _bitlen(c3_d bit_d) @@ -190,13 +295,15 @@ static c3_d _bitlen(c3_d bit_d) static c3_d _height(subtree* sub_u) { - return _bitlen(ABS_DIFF(sub_u->dex_d, sub_u->sin_d)) - 1; + c3_d ret_d = _bitlen(ABS_DIFF(sub_u->dex_d, sub_u->sin_d)) - 1; + return ret_d; } static c3_d _largest_pow2(c3_d val_d) { - return 1 << (_bitlen(val_d-1) - 1); + c3_d ret_d = 1 << (_bitlen(val_d-1) - 1); + return ret_d; } static void @@ -222,143 +329,133 @@ w_to_y(c3_w wod_w[16], c3_y byt_y[64]) } +static void +_compress_node(c3_y out_y[BLAKE3_BLOCK_LEN], blake_node* nod_u) +{ + urcrypt_blake3_compress(nod_u->cev_y, + nod_u->boq_y, + nod_u->len_y, + nod_u->con_d, + nod_u->fag_y, + out_y); +} + static void -_make_chain_value(c3_w res_w[8], blake_node* nod_u) -{ - //c3_w* blake3 -} - -static blake_node -_compress_chunk(c3_y* dat_y, c3_w dat_w, c3_w cev_w[8], c3_d con_d, c3_w fag_w) -{ - // A node represents a chunk or parent in the BLAKE3 Merkle tree. - blake_node nod_u; - memcpy(&nod_u.cev_w, cev_w, 8 * sizeof(c3_w)); - nod_u.con_d = con_d; - nod_u.len_w = BLAKE3_BLOCK_LEN; - nod_u.fag_w |= CHUNK_START; - - c3_y* boq_y = alloca(BLAKE3_BLOCK_LEN); - c3_w lef_w = dat_w; - memset(boq_y, 0, BLAKE3_BLOCK_LEN); - while ( lef_w > BLAKE3_BLOCK_LEN ) { - memcpy(boq_y, dat_y, BLAKE3_BLOCK_LEN); - boq_y += BLAKE3_BLOCK_LEN; - lef_w -= BLAKE3_BLOCK_LEN; - y_to_w(boq_y, nod_u.boq_w); - c3_w cev_w[8]; - memset(cev_w, 0, 8 * sizeof(c3_w)); - _make_chain_value(cev_w, &nod_u); - memcpy(nod_u.cev_w, cev_w, 8 * sizeof(c3_w)); - nod_u.fag_w &= ~CHUNK_START; - } - memset(boq_y, 0, BLAKE3_BLOCK_LEN); - nod_u.len_w = lef_w; - memcpy(boq_y, dat_y, lef_w); - y_to_w(boq_y, nod_u.boq_w); - nod_u.fag_w |= CHUNK_END; +_make_chain_value(c3_y out_y[32], blake_node* nod_u) +{ + c3_y tmp_y[64]; + _compress_node(tmp_y, nod_u); + memcpy(out_y, tmp_y, BLAKE3_OUT_LEN); +} +static blake_node* +_compress_chunk(c3_y* dat_y, c3_w dat_w, c3_y cev_y[BLAKE3_OUT_LEN], c3_d con_d, c3_w fag_y) +{ + blake_node* nod_u = c3_calloc(sizeof(blake_node)); + nod_u->con_d = con_d; + nod_u->fag_y = fag_y; + memcpy(nod_u->cev_y, cev_y, BLAKE3_OUT_LEN); + urcrypt_blake3_chunk_output(dat_w, dat_y, nod_u->cev_y, nod_u->boq_y, &nod_u->len_y, &nod_u->con_d, &nod_u->fag_y); return nod_u; } static -blake_node _leaf_hash(c3_d con_d) +blake_node* _leaf_hash(c3_d con_d) { - -//_compress_chunk(c3_y* dat_y, c3_w dat_w, c3_w* cev_w, c3_d con_d, c3_w fag_w) - c3_y* dat_y = alloca(1024); - memset(dat_y, 0, 1024); + c3_y* dat_y = c3_calloc(1024); return _compress_chunk(dat_y, 1024, IV, con_d, 0); } -static blake_node -_parent_node(c3_w sin_w[8], c3_w dex_w[8], c3_w key_w[8], c3_w fag_w) +static blake_node* +_parent_node(c3_y sin_y[BLAKE3_OUT_LEN], c3_y dex_y[BLAKE3_OUT_LEN], c3_y key_y[BLAKE3_OUT_LEN], c3_w fag_y) { - blake_node nod_u; - nod_u.con_d = 0; - memcpy(&nod_u.cev_w, key_w, 8 * sizeof(c3_w)); - nod_u.len_w = BLAKE3_BLOCK_LEN; - nod_u.fag_w |= PARENT; - memcpy(nod_u.boq_w, sin_w, 8 * sizeof(c3_w)); - memcpy(&nod_u.boq_w[8], dex_w, 8 * sizeof(c3_w)); + blake_node* nod_u = c3_calloc(sizeof(blake_node)); + nod_u->con_d = 0; + memcpy(nod_u->cev_y, key_y, BLAKE3_OUT_LEN); + nod_u->len_y = BLAKE3_BLOCK_LEN; + nod_u->fag_y = fag_y | PARENT; + memcpy(nod_u->boq_y, sin_y, BLAKE3_OUT_LEN); + memcpy(nod_u->boq_y + BLAKE3_OUT_LEN, dex_y, BLAKE3_OUT_LEN); return nod_u; } -static blake_node +static blake_node* _parent_hash(blake_node* sin_u, blake_node* dex_u) { - c3_w sin_w[8], dex_w[8]; - memset(sin_w, 0, 8 * sizeof(c3_w)); - memset(dex_w, 0, 8 * sizeof(c3_w)); - _make_chain_value(sin_w, sin_u); - _make_chain_value(dex_w, dex_u); - return _parent_node(sin_w, dex_w, IV, 0); + c3_y* sin_y = c3_calloc(BLAKE3_OUT_LEN); + c3_y* dex_y = c3_calloc(BLAKE3_OUT_LEN); + _make_chain_value(sin_y, sin_u); + _make_chain_value(dex_y, dex_u); + return _parent_node(sin_y, dex_y, IV, 0); } -static void -_compress_node(c3_y out_y[64], blake_node* nod_u) -{ - //urcrypt_blake3_compress(nod_u->cev_w, nod_u->boq_w, nod_u->len_w, nod_u->con_w, nod_u->fag_w, out_y); - - - -} static void -_root_hash(c3_y out_y[16], blake_node* nod_u) +_root_hash(c3_y out_y[64], blake_node* nod_u) { - nod_u->fag_w |= ROOT; + nod_u->fag_y |= ROOT; + _compress_node(out_y, nod_u); +} +void print_pair(void* vod_p, c3_w i_w) { + pair* par_u = (pair*)vod_p; + fprintf(stderr, "pair: %u", i_w); + _log_buf("sin: ", par_u->sin_y, BLAKE3_OUT_LEN); + _log_buf("dex: ", par_u->dex_y, BLAKE3_OUT_LEN); } + static void recurse_subtree(subtree* sub_u, blake_node* nod_u, u3_vec(pair)* par_u) { - if ( _height(sub_u) == 0 ) { - *nod_u = _leaf_hash(sub_u->sin_d); + c3_d hit_d = _height(sub_u); + if ( hit_d == 0 ) { + blake_node* new_u = _leaf_hash(sub_u->sin_d); + memcpy(nod_u, new_u, sizeof(blake_node)); return; } + c3_d mid_d = sub_u->sin_d + _largest_pow2(ABS_DIFF(sub_u->dex_d, sub_u->sin_d)); - subtree sin_u = (subtree){sub_u->sin_d, mid_d}; + subtree* sin_u = c3_calloc(sizeof(subtree)); + *sin_u = (subtree){sub_u->sin_d, mid_d}; blake_node* lod_u = c3_calloc(sizeof(blake_node)); u3_vec(pair)* lar_u = _vec_make(8); - recurse_subtree(&sin_u, lod_u, lar_u); + recurse_subtree(sin_u, lod_u, lar_u); - subtree dex_u = (subtree){mid_d, sub_u->dex_d}; + subtree* dex_u = c3_calloc(sizeof(subtree)); + *dex_u = (subtree){mid_d, sub_u->dex_d}; blake_node* rod_u = c3_calloc(sizeof(blake_node)); u3_vec(pair)* rar_u = _vec_make(8); - recurse_subtree(&dex_u, rod_u, rar_u); + recurse_subtree(dex_u, rod_u, rar_u); - *nod_u = _parent_hash(lod_u, rod_u); - c3_w* lcv_w = c3_calloc(8* sizeof(c3_w)); - _make_chain_value(lcv_w, lod_u); - c3_w* rcv_w = c3_calloc(8* sizeof(c3_w)); - _make_chain_value(rcv_w, rod_u); + pair* new_u = c3_calloc(sizeof(pair)); + _make_chain_value(new_u->sin_y, lod_u); + _make_chain_value(new_u->dex_y, rod_u); + memcpy(nod_u, _parent_hash(lod_u, rod_u), sizeof(blake_node)); - _vec_append(par_u, lcv_w); - _vec_append(par_u, rcv_w); + _vec_append(par_u, new_u); _vec_weld_mut(par_u, lar_u); _vec_weld_mut(par_u, rar_u); } static void -_bao_build(c3_d num_d, c3_y has_y[32], u3_vec(c3_w[8])* pof_u, u3_vec(pair)* par_u) { - subtree sub_u; - memset(&sub_u, 0, sizeof(subtree)); +_bao_build(c3_d num_d, c3_y has_y[64], u3_vec(c3_w[8])* pof_u, u3_vec(pair)* par_u) { + subtree* sub_u = c3_calloc(sizeof(subtree)); + *sub_u = (subtree){0, num_d}; blake_node* nod_u = c3_calloc(sizeof(blake_node)); - recurse_subtree(&sub_u, nod_u, par_u); + recurse_subtree(sub_u, nod_u, par_u); + _root_hash(has_y, nod_u); if ( _vec_len(par_u) != 0 ) { for ( c3_w i_w = _bitlen(num_d - 1); i_w > 1; i_w-- ) { - _vec_append(pof_u, _vec_get(pair, par_u, 0)->dex_w); - _vec_popf(par_u); + pair* pir_u = _vec_popf(par_u); + _vec_append(pof_u, pir_u->dex_y); } - pair* fst_u = _vec_get(pair, par_u, 0); - _vec_append(pof_u, fst_u->dex_w); - _vec_append(pof_u, fst_u->sin_w); - _vec_popf(par_u); + pair* pir_u = _vec_popf(par_u); + _vec_append(pof_u, pir_u->dex_y); + _vec_append(pof_u, pir_u->sin_y); for ( c3_w i_w = 0; i_w < (_vec_len(pof_u) >> 1); i_w++ ) { c3_w jay_w = _vec_len(pof_u) - i_w - 1; _vec_swap(pof_u, i_w, jay_w); @@ -367,29 +464,29 @@ _bao_build(c3_d num_d, c3_y has_y[32], u3_vec(c3_w[8])* pof_u, u3_vec(pair)* par } static void -_push_leaf(verifier* ver_u, c3_w lef_w[8]) +_push_leaf(verifier* ver_u, c3_y lef_y[BLAKE3_OUT_LEN]) { - _vec_append(&ver_u->que_u, lef_w); + _vec_append(&ver_u->que_u, lef_y); } static void -_pop_leaf(c3_w out_w[8], verifier* ver_u) +_pop_leaf(c3_y out_y[BLAKE3_OUT_LEN], verifier* ver_u) { - c3_w* res_w = _vec_popf(&ver_u->que_u); - memcpy(out_w, res_w, 8); + c3_y* res_y = _vec_popf(&ver_u->que_u); + memcpy(out_y, res_y, BLAKE3_OUT_LEN); } static void -_push_parent(verifier* ver_u, c3_w par_w[8]) +_push_parent(verifier* ver_u, c3_y par_y[BLAKE3_OUT_LEN]) { - _vec_append(&ver_u->sta_u, par_w); + _vec_append(&ver_u->sta_u, par_y); } static void -_pop_parent(c3_w out_w[8], verifier* ver_u) +_pop_parent(c3_y out_y[BLAKE3_OUT_LEN], verifier* ver_u) { - c3_w* res_w = _vec_popf(&ver_u->sta_u); - memcpy(out_w, res_w, 8); + c3_y* res_y = _vec_popl(&ver_u->sta_u); + memcpy(out_y, res_y, BLAKE3_OUT_LEN); } static void @@ -397,40 +494,44 @@ _verifier_next(verifier* ver_u) { if ( _height(&ver_u->sub_u) == 0 ) { ver_u->sub_u.sin_d += 1; - ver_u->sub_u.dex_d += 1; + ver_u->sub_u.dex_d += (1 << _count_trail_zero(ver_u->sub_u.dex_d)); if ( ver_u->sub_u.dex_d > ver_u->num_d ) { ver_u->sub_u.dex_d = ver_u->num_d; } - subtree sub_u = (subtree){ver_u->sub_u.sin_d, ver_u->sub_u.sin_d + _largest_pow2(ABS_DIFF(ver_u->sub_u.dex_d, ver_u->sub_u.sin_d))}; - ver_u->sub_u = sub_u; + } else { + ver_u->sub_u = (subtree){ver_u->sub_u.sin_d, ver_u->sub_u.sin_d + _largest_pow2(ABS_DIFF(ver_u->sub_u.dex_d, ver_u->sub_u.sin_d))}; } } + static c3_o -_verifier_init(verifier* ver_u, c3_y rot_y[32], u3_vec(c3_w[8])* pof_u) +_verifier_init(verifier* ver_u, c3_y rot_y[BLAKE3_BLOCK_LEN], u3_vec(c3_y[BLAKE3_OUT_LEN])* pof_u) { if ( _vec_len(pof_u) == 0 ) { return ver_u->num_d <= 1 ? c3y : c3n; } - blake_node nod_u = _parent_node(_vec_get(c3_w, pof_u, 0), _vec_get(c3_w, pof_u, 1), IV, 0); - for (c3_w i_w = 2; i_w < (_vec_len(pof_u) - 2); i_w++ ) { - c3_w cev_w[8]; - memset(cev_w, 0, 8); - _make_chain_value(cev_w, &nod_u); - c3_w* pof_w = _vec_get(c3_w, pof_u, i_w); - nod_u = _parent_node(cev_w, pof_w, IV, 0); + blake_node* nod_u = _parent_node(_vec_get(c3_y, pof_u, 0), _vec_get(c3_y, pof_u, 1), IV, 0); + for (c3_w i_w = 2; i_w < (_vec_len(pof_u)); i_w++ ) { + c3_y* cev_y = c3_calloc(BLAKE3_OUT_LEN); + _make_chain_value(cev_y, nod_u); + c3_y* pof_y = _vec_get(c3_y, pof_u, i_w); + nod_u = _parent_node(cev_y, pof_y, IV, 0); } - c3_y ron_y[32]; - _root_hash(ron_y, &nod_u); + c3_y ron_y[BLAKE3_BLOCK_LEN]; + _root_hash(ron_y, nod_u); - if ( memcmp(ron_y, rot_y, 32) != 0 ) { +// TODO: double check + if ( memcmp(ron_y, rot_y, BLAKE3_BLOCK_LEN) != 0 ) { + fprintf(stderr, "mismatch\r\n"); + _log_buf("ron_y", ron_y, BLAKE3_BLOCK_LEN); return c3n; } ver_u->sub_u = (subtree){2,4}; _vec_init(&ver_u->que_u, 8); - _vec_append(&ver_u->que_u, _vec_get(c3_w, pof_u, 0)); - _vec_append(&ver_u->que_u, _vec_get(c3_w, pof_u, 1)); + + _vec_append(&ver_u->que_u, _vec_popf(pof_u)); + _vec_append(&ver_u->que_u, _vec_popf(pof_u)); _vec_init(&ver_u->sta_u, 8); _vec_weld_mut(&ver_u->sta_u, pof_u); @@ -445,20 +546,21 @@ _verifier_init(verifier* ver_u, c3_y rot_y[32], u3_vec(c3_w[8])* pof_u) static c3_o _veri_check_leaf(verifier* ver_u, c3_d lef_d) { - if ( _vec_len(&ver_u->sta_u) > 0 ) { - c3_w out_w[8]; - _pop_leaf(out_w, ver_u); - c3_w cav_w[8]; - blake_node nod_u = _leaf_hash(lef_d); - _make_chain_value(cav_w, &nod_u); - return 0 == memcmp(cav_w, out_w, 8 * sizeof(c3_w)) ? c3y : c3n; + + if ( _vec_len(&ver_u->que_u) > 0 ) { + c3_y out_y[BLAKE3_OUT_LEN]; + _pop_leaf(out_y, ver_u); + c3_y cav_y[BLAKE3_OUT_LEN]; + blake_node* nod_u = _leaf_hash(lef_d); + _make_chain_value(cav_y, nod_u); + return 0 == memcmp(cav_y, out_y, BLAKE3_OUT_LEN) ? c3y : c3n; } else if ( _vec_len(&ver_u->sta_u) > 0 ) { - c3_w out_w[8]; - _pop_parent(out_w, ver_u); - c3_w cav_w[8]; - blake_node nod_u = _leaf_hash(lef_d); - _make_chain_value(cav_w, &nod_u); - return 0 == memcmp(cav_w, out_w, 8 * sizeof(c3_w)) ? c3y : c3n; + c3_y out_y[BLAKE3_OUT_LEN]; + _pop_parent(out_y, ver_u); + c3_y cav_y[BLAKE3_OUT_LEN]; + blake_node* nod_u = _leaf_hash(lef_d); + _make_chain_value(cav_y, nod_u); + return 0 == memcmp(cav_y, out_y, BLAKE3_OUT_LEN) ? c3y : c3n; } return c3n; } @@ -466,15 +568,17 @@ _veri_check_leaf(verifier* ver_u, c3_d lef_d) static c3_o _veri_check_pair(verifier* ver_u, pair* par_u) { - c3_w out_w[8]; - _pop_parent(out_w, ver_u); - blake_node nod_u = _parent_node(par_u->sin_w, par_u->dex_w, IV, 0); - c3_w cav_w[8]; - _make_chain_value(cav_w, &nod_u); - c3_w par_w[8]; - _pop_parent(par_w, ver_u); - c3_o ret_o = _( _vec_len(&ver_u->sta_u) > 0 && memcmp(par_w, cav_w, 8 * sizeof(c3_w))); - return ret_o; + if ( _vec_len(&ver_u->sta_u) == 0 ) { + fprintf(stderr, "bailing empty stack\r\n"); + return c3n; + } + c3_y par_y[BLAKE3_OUT_LEN]; + // par_y is wrong + _pop_parent(par_y, ver_u); + blake_node* nod_u = _parent_node(par_u->sin_y, par_u->dex_y, IV, 0); + c3_y cav_y[BLAKE3_OUT_LEN]; + _make_chain_value(cav_y, nod_u); + return _(memcmp(par_y, cav_y, BLAKE3_OUT_LEN) == 0); } static void _veri_init(verifier* ver_u, c3_d num_d) @@ -485,60 +589,110 @@ static void _veri_init(verifier* ver_u, c3_d num_d) #ifdef BLAKE_TEST +static void _test_root_hash() +{ + blake_node* nod_u = _leaf_hash(0); + c3_y out_y[BLAKE3_BLOCK_LEN]; + _root_hash(out_y, nod_u); + _log_buf("root", out_y, BLAKE3_BLOCK_LEN); +} + +static void _test_lead_zeros() +{ + #define asrt(x,y) if ( x != y ) { fprintf(stderr, "failed (line: %u) got %llu expected %llu", __LINE__, x,y); exit(1); } + // Test with 0, should be 64 leading zeros + asrt(_count_lead_zeros(0ULL), 64); + + // Test with maximum unsigned 64-bit integer, should be 0 leading zeros + asrt(_count_lead_zeros(0xFFFFFFFFFFFFFFFFULL), 0); + + // Test with 1 (which is 2^0), should be 63 leading zeros + asrt(_count_lead_zeros(1ULL), 63); + + // Test with 2^63, should be 1 leading zero + asrt(_count_lead_zeros(0x4000000000000000ULL), 1); + + // Test with a number in the middle, for example 2^31, should be 32 leading zeros + asrt(_count_lead_zeros(0x0000000080000000ULL), 32); + + // Test with a small number, should still recognize leading zeros correctly + asrt(_count_lead_zeros(0x00000000000000FFULL), 56); + + // Additional tests for edge cases + asrt(_count_lead_zeros(0x2000000000000000ULL), 2); + + // Test with a value just below the halfway mark, should have 33 leading zeros + asrt(_count_lead_zeros(0x000000007FFFFFFFULL), 33); + + asrt(_bitlen(7), 3); + + asrt(_count_trail_zero(3), 0); + #undef asrt +} + static void _test_bao() { - fprintf(stderr, "Starting test\r\n"); for ( c3_w num_w = 1; num_w <= 100; num_w++ ) { - c3_y has_y[32]; - u3_vec(c3_w[8])* pof_u = _vec_make(10); + c3_y has_y[BLAKE3_BLOCK_LEN]; + u3_vec(c3_y[8])* pof_u = _vec_make(10); u3_vec(pair)* par_u = _vec_make(10); - fprintf(stderr, "Bao building\r\n"); _bao_build(num_w, has_y, pof_u, par_u); - fprintf(stderr, "Bao built\r\n"); + verifier ver_u; memset(&ver_u, 0, sizeof(verifier)); _veri_init(&ver_u, num_w); - fprintf(stderr, "veri initted\r\n"); if ( c3n == _verifier_init(&ver_u, has_y, pof_u) ) { fprintf(stderr, "Failed on %u\r\n", num_w); exit(1); } if ( num_w == 1 ) { - c3_y out_y[16]; - blake_node nod_u = _leaf_hash(0); - _root_hash(out_y, &nod_u); - if ( memcmp(out_y, has_y, 32) != 0 ) { + c3_y out_y[BLAKE3_BLOCK_LEN]; + blake_node* nod_u = _leaf_hash(0); + _root_hash(out_y, nod_u); + if ( memcmp(out_y, has_y, BLAKE3_BLOCK_LEN) != 0 ) { + _log_buf("expected", has_y, BLAKE3_BLOCK_LEN); + _log_buf("got", out_y, BLAKE3_BLOCK_LEN); + fprintf(stderr, "1 special case\r\n"); exit(1); } - for ( c3_w i_w = 0; i_w < num_w; i_w++ ) { - if ( c3n == _veri_check_leaf(&ver_u, i_w) ) { - fprintf(stderr, "Check leaf %u failed for %u\r\n", i_w, num_w); + continue; + } + for ( c3_w i_w = 0; i_w < num_w; i_w++ ) { + if ( c3n == _veri_check_leaf(&ver_u, i_w) ) { + fprintf(stderr, "Check leaf %u failed for %u\r\n", i_w, num_w); + exit(1); + } + + // pl + // _log_verifier(&ver_u); + if ( i_w < _vec_len(par_u) ) { + pair* pir_u = _vec_get(pair, par_u, i_w); + if ( _veri_check_pair(&ver_u, pir_u) == c3n ) { + fprintf(stderr, "check pair failed (%u, %u)", i_w, num_w); exit(1); - } - if ( i_w < _vec_len(par_u) ) { - pair* pir_u = _vec_get(pair, par_u, i_w); - if ( _veri_check_pair(&ver_u, pir_u) == c3n ) { - fprintf(stderr, "check pair failed (%u, %u)", i_w, num_w); - exit(1); - } + } + _verifier_next(&ver_u); + if ( _height(&ver_u.sub_u) == 0 ) { + _push_leaf(&ver_u, pir_u->sin_y); + _push_leaf(&ver_u, pir_u->dex_y); + _verifier_next(&ver_u); _verifier_next(&ver_u); - if ( _height(&ver_u.sub_u) == 0 ) { - _push_leaf(&ver_u, pir_u->sin_w); - _push_leaf(&ver_u, pir_u->dex_w); - _verifier_next(&ver_u); - _verifier_next(&ver_u); - } else { - _push_parent(&ver_u, pir_u->sin_w); - _push_parent(&ver_u, pir_u->dex_w); - } + } else { + _push_parent(&ver_u, pir_u->dex_y); + _push_parent(&ver_u, pir_u->sin_y); } } - } + } } } +static void print_int(void* vod_p, c3_w i_w) { + c3_w* int_w = (c3_w*)vod_p; + fprintf(stderr, "int: %u\r\n", *int_w); +} + static void _test_vec() { fprintf(stderr, "Making vec"); u3_vec(c3_w)* vec_u = _vec_make(2); @@ -581,12 +735,33 @@ static void _test_vec() { fprintf(stderr, "Vec should be empty\r\n"); exit(1); } + + _vec_append(vec_u, &fst); + _vec_append(vec_u, &snd); + _vec_append(vec_u, &thd); + c3_w for_w = 4; + c3_w fiv_w = 5; + c3_w six_w = 6; + u3_vec(c3_w)* new_u = _vec_make(4); + _vec_append(new_u, &for_w); + _vec_append(new_u, &fiv_w); + _vec_append(new_u, &six_w); + + _vec_weld_mut(vec_u, new_u); + _vec_each(vec_u, print_int); + + + + + _vec_free(vec_u); c3_free(vec_u); } int main() { - _test_vec(); + //_test_vec(); + //_test_root_hash(); + // _test_lead_zeros(); _test_bao(); return 0; From 7ffd05b5cbaf7e2ace836b202e061089c6ddf0ba Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Thu, 22 Feb 2024 18:14:57 -0500 Subject: [PATCH 037/430] xmas: update $name to new format, misc cleanup --- pkg/vere/io/xmas.c | 264 ++++++++++++++++++++++++++------------------- 1 file changed, 155 insertions(+), 109 deletions(-) diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c index 441333c9ef..6ab913e48c 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/xmas.c @@ -51,8 +51,9 @@ static c3_y XMAS_COOKIE[4] = { 0x5e, 0x1d, 0xad, 0x51 }; #define SIFT_VAR(dest, src, len) dest = 0; for(int i = 0; i < len; i++ ) { dest |= ((src + i) >> (8*i)); } -#define CHECK_BOUNDS(cur) if ( len_w < cur ) { u3l_log("xmas: failed parse (%i,%i) at line %i", len_w, cur, __LINE__); return 0; } -#define safe_dec(num) num == 0 ? num : num - 1; +#define CHECK_BOUNDS(cur) if ( len_w < cur ) { u3l_log("xmas: failed parse (%u,%u) at line %i", len_w, cur, __LINE__); return 0; } +#define safe_dec(num) (num == 0 ? num : num - 1) +#define _xmas_met3_w(a_w) ((c3_bits_word(a_w) + 0x7) >> 3) typedef struct _u3_bitset { c3_w len_w; @@ -139,22 +140,23 @@ typedef enum _u3_xmas_nexh { } u3_xmas_nexh; typedef struct _u3_xmas_name_meta { - // reserved (2 bits) - c3_y ran_y; // rank (2 bits) - c3_y rif_y; // rift-len (2 bits) - c3_o pat_b; // path length length (1bit) - c3_o boq_b; // 0b1 if custom, 0b0 implicitly 13 (1bit) - c3_o fra_y; // fragment number length (2bit) + c3_y ran_y; // rank (2 bits) + c3_y rif_y; // rift-len (2 bits) + c3_y nit_y; // initial overlay (1 bit) + c3_y tau_y; // %data (0) or %auth (1), 0 if !nit_o (1 bit) + c3_y gaf_y; // fragment number length (2bit) } u3_xmas_name_meta; typedef struct _u3_xmas_name { - u3_xmas_name_meta met_u; + // u3_xmas_name_meta met_u; c3_d her_d[2]; c3_w rif_w; - c3_s pat_s; - c3_c* pat_c; c3_y boq_y; + c3_o nit_o; + c3_o aut_o; c3_w fra_w; + c3_s pat_s; + c3_c* pat_c; } u3_xmas_name; @@ -202,9 +204,9 @@ typedef struct _u3_xmas_page_meat { c3_w tot_w; // total fragments c3_y aul_y; // authentication length u3_xmas_auth_type ayp_y; // authentication type - c3_y* aut_y; // authentication - c3_d len_w; // fragment length - c3_y* fra_y; // fragment + c3_y* aut_y; // authentication + c3_w len_w; // fragment length + c3_y* fra_y; // fragment } u3_xmas_page_meat; typedef struct _u3_xmas_hop { @@ -299,32 +301,37 @@ _log_head(u3_xmas_head* hed_u) static void _log_buf(c3_y* buf_y, c3_w len_w) { - c3_w siz_w = 2*len_w + 1; - c3_c* res_c = c3_calloc(siz_w); - c3_w cur_w = 0; - c3_c tmp_c[3]; - for(c3_w idx_w = 0; idx_w < len_w; idx_w++ ) { - snprintf(res_c + (2*idx_w), siz_w - (2*idx_w), "%02x", buf_y[idx_w]); + for( c3_w i_w = 0; i_w < len_w; i_w++ ) { + fprintf(stderr, "%02x", buf_y[i_w]); } - u3l_log("buffer: %s", res_c); - c3_free(res_c); + fprintf(stderr, "\r\n"); } static void _log_name(u3_xmas_name* nam_u) { - u3l_log("meta"); - u3l_log("rank: %u", nam_u->met_u.ran_y); - u3l_log("rift length: %u", nam_u->met_u.rif_y); - u3l_log("custom bloq: %c", nam_u->met_u.boq_b == 1 ? 'y' : 'n'); - u3l_log("frag num length: %u", nam_u->met_u.fra_y); - u3l_log("path length length: %u", nam_u->met_u.pat_b); - u3l_log("publisher: %s", u3r_string(u3dc("scot", c3__p, u3i_chubs(2, nam_u->her_d)))); + // u3l_log("meta"); + // u3l_log("rank: %u", nam_u->met_u.ran_y); + // u3l_log("rift length: %u", nam_u->met_u.rif_y); + // u3l_log("nit: %u", nam_u->met_u.nit_y); + // u3l_log("tau: %u", nam_u->met_u.tau_y); + // u3l_log("frag num length: %u", nam_u->met_u.gaf_y); + + { + u3_noun her = u3dc("scot", c3__p, u3i_chubs(2, nam_u->her_d)); + c3_c* her_c = u3r_string(her); + u3l_log("publisher: %s", her_c); + c3_free(her_c); + u3z(her); + } + u3l_log("rift: %u", nam_u->rif_w); - u3l_log("path len: %u", nam_u->pat_s); - u3l_log("path: %s", nam_u->pat_c); u3l_log("bloq: %u", nam_u->boq_y); + u3l_log("init: %s", (c3y == nam_u->nit_o) ? "&" : "|"); + u3l_log("auth: %s", (c3y == nam_u->aut_o) ? "&" : "|"); u3l_log("frag: %u", nam_u->fra_w); + u3l_log("path len: %u", nam_u->pat_s); + u3l_log("path: %s", nam_u->pat_c); } @@ -628,58 +635,63 @@ _xmas_sift_name(u3_xmas_name* nam_u, c3_y* buf_y, c3_w len_w) #endif c3_w cur_w = 0; + u3_xmas_name_meta met_u; + CHECK_BOUNDS(cur_w + 1); c3_y met_y = buf_y[cur_w]; - nam_u->met_u.ran_y = (met_y >> 0) & 0x3; - nam_u->met_u.rif_y = (met_y >> 2) & 0x3; - nam_u->met_u.pat_b = (met_y >> 4) & 0x1; - nam_u->met_u.boq_b = (met_y >> 5) & 0x1; - nam_u->met_u.fra_y = (met_y >> 6) & 0x3; + met_u.ran_y = (met_y >> 0) & 0x3; + met_u.rif_y = (met_y >> 2) & 0x3; + met_u.nit_y = (met_y >> 4) & 0x1; + met_u.tau_y = (met_y >> 5) & 0x1; + met_u.gaf_y = (met_y >> 6) & 0x3; cur_w += 1; - c3_y her_y = 2 << nam_u->met_u.ran_y; + c3_y her_y = 2 << met_u.ran_y; CHECK_BOUNDS(cur_w + her_y) _ames_ship_to_chubs(nam_u->her_d, her_y, buf_y + cur_w); cur_w += her_y; - c3_y rif_y = nam_u->met_u.rif_y + 1; + c3_y rif_y = met_u.rif_y + 1; nam_u->rif_w = 0; CHECK_BOUNDS(cur_w + rif_y) for( int i = 0; i < rif_y; i++ ) { nam_u->rif_w |= (buf_y[cur_w] << (8*i)); cur_w++; } - - c3_y pat_y = 1 << nam_u->met_u.pat_b; - CHECK_BOUNDS(cur_w + pat_y) - nam_u->pat_s = 0; - for ( int i = 0; i < pat_y; i++ ) { - nam_u->pat_s |= (buf_y[cur_w] << (8*i)); - cur_w++; + + CHECK_BOUNDS(cur_w + 1); + nam_u->boq_y = buf_y[cur_w]; + cur_w++; + + if ( met_u.nit_y ) { + assert( !met_u.tau_y ); + // XX init packet + nam_u->fra_w = 0; + } + else { + c3_y fag_y = met_u.gaf_y + 1; + CHECK_BOUNDS(cur_w + fag_y); + for ( int i = 0; i < fag_y; i++ ) { + nam_u->fra_w |= (buf_y[cur_w] << (8*i)); + cur_w++; + } } + nam_u->nit_o = ( met_u.nit_y ) ? c3y : c3n; + // XX ?:(=(1 tau.c) %auth %data) + nam_u->aut_o = ( met_u.tau_y ) ? c3y : c3n; + + CHECK_BOUNDS(cur_w + 2) + nam_u->pat_s = buf_y[cur_w] + | (buf_y[cur_w + 1] << 8); + cur_w += 2; + nam_u->pat_c = c3_calloc(nam_u->pat_s + 1); // unix string for ease of manipulation - CHECK_BOUNDS(nam_u->pat_s + cur_w); + CHECK_BOUNDS(cur_w + nam_u->pat_s); memcpy(nam_u->pat_c, buf_y + cur_w, nam_u->pat_s); nam_u->pat_c[nam_u->pat_s] = 0; cur_w += nam_u->pat_s; - if ( 1 == nam_u->met_u.boq_b ) { - CHECK_BOUNDS(cur_w + 1); - nam_u->boq_y = buf_y[cur_w]; - cur_w++; - } else { - nam_u->boq_y = 13; - } - - c3_y fra_y = nam_u->met_u.fra_y + 1; - CHECK_BOUNDS(cur_w + fra_y) - nam_u->fra_w = 0; - for( int i = 0; i < fra_y; i++ ) { - nam_u->fra_w |= (buf_y[cur_w] << (8*i)); - cur_w++; - } - return cur_w; } @@ -748,7 +760,7 @@ _xmas_sift_page_pact(u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, cur_w++; } - CHECK_BOUNDS(cur_w + (c3_w)mat_u->len_w); + CHECK_BOUNDS(cur_w + mat_u->len_w); mat_u->fra_y = c3_calloc(mat_u->len_w); memcpy(mat_u->fra_y, buf_y + cur_w, mat_u->len_w); @@ -887,7 +899,7 @@ _xmas_sift_pact(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w) } } //u3_assert(res_w <= len_w ); - return res_w; + return res_w + 8; } // cut and pasted from ames.c @@ -929,13 +941,13 @@ _xmas_etch_head(u3_xmas_head* hed_u, c3_y buf_y[8]) } } #endif - c3_o req_o = c3o((hed_u->typ_y == PACT_PEEK), (hed_u->typ_y == PACT_POKE)); - c3_y siz_y = req_o ? 5 : 7; - c3_w hed_w = (hed_u->nex_y & 0x3 ) << 2 - ^ (hed_u->pro_y & 0x7 ) << 4 - ^ ((hed_u->typ_y & 0x3 ) << 7) - ^ ((hed_u->hop_y & 0x7 ) << 9) - ^ ((hed_u->mug_w & 0xFFFFFF ) << 12); + // c3_o req_o = c3o((hed_u->typ_y == PACT_PEEK), (hed_u->typ_y == PACT_POKE)); + // c3_y siz_y = req_o ? 5 : 7; + c3_w hed_w = (hed_u->nex_y & 0x3) << 2 + ^ (hed_u->pro_y & 0x7) << 4 + ^ (hed_u->typ_y & 0x3) << 7 // XX constant, 1 + ^ (hed_u->hop_y & 0x7) << 9 + ^ (hed_u->mug_w & 0xFFFFFF) << 12; // XX: we don't expand hopcount if no request. Correct? // /*if ( c3y == req_o ) { @@ -946,6 +958,23 @@ _xmas_etch_head(u3_xmas_head* hed_u, c3_y buf_y[8]) memcpy(buf_y + 4, XMAS_COOKIE, XMAS_COOKIE_LEN); } +static c3_y +_xmas_rank(c3_d who_d[2]) +{ + if ( who_d[1] ) { + return 3; + } + else if ( who_d[0] >> 32 ) { + return 2; + } + else if ( who_d[0] >> 16 ) { + return 1; + } + else { + return 0; + } +} + static c3_w _xmas_etch_name(c3_y* buf_y, u3_xmas_name* nam_u) { @@ -953,26 +982,33 @@ _xmas_etch_name(c3_y* buf_y, u3_xmas_name* nam_u) #endif c3_w cur_w = 0; - nam_u->met_u.ran_y = safe_dec(u3r_met(0, u3r_met(4, u3i_chubs(2, nam_u->her_d)))); - nam_u->met_u.rif_y = safe_dec(u3r_met(3, u3i_word(nam_u->rif_w))); - nam_u->met_u.fra_y = safe_dec(u3r_met(3, u3i_word(nam_u->fra_w))); - nam_u->met_u.boq_b = nam_u->boq_y == 13 ? 0 : 1; - nam_u->met_u.pat_b = nam_u->pat_s > 0xFF ? 1 : 0; - - // TODO: double check reserved bits - c3_y met_y = (nam_u->met_u.ran_y & 0x3) << 0 - ^ (nam_u->met_u.rif_y & 0x3) << 2 - ^ (nam_u->met_u.pat_b & 0x1) << 4 - ^ (nam_u->met_u.boq_b & 0x1) << 5 - ^ (nam_u->met_u.fra_y & 0x3) << 6; + u3_xmas_name_meta met_u; - buf_y[cur_w] = met_y; + met_u.ran_y = _xmas_rank(nam_u->her_d); + met_u.rif_y = safe_dec(_xmas_met3_w(nam_u->rif_w)); + if ( c3y == nam_u->nit_o ) { + met_u.nit_y = 1; + met_u.tau_y = 0; + met_u.gaf_y = 0; + } + else { + met_u.nit_y = 0; + met_u.tau_y = (c3y == nam_u->aut_o) ? 1 : 0; + met_u.gaf_y = safe_dec(_xmas_met3_w(nam_u->fra_w)); + } + + c3_y met_y = (met_u.ran_y & 0x3) << 0 + ^ (met_u.rif_y & 0x3) << 2 + ^ (met_u.nit_y & 0x1) << 4 + ^ (met_u.tau_y & 0x1) << 5 + ^ (met_u.gaf_y & 0x3) << 6; + + buf_y[cur_w] = met_y; - u3_xmas_name_meta met_u = nam_u->met_u; //ship cur_w++; - c3_y her_y = 1 << (met_u.ran_y + 1); + c3_y her_y = 2 << met_u.ran_y; // XX confirm _ames_ship_of_chubs(nam_u->her_d, her_y, buf_y + cur_w); cur_w += her_y; @@ -983,8 +1019,17 @@ _xmas_etch_name(c3_y* buf_y, u3_xmas_name* nam_u) cur_w++; } + buf_y[cur_w] = nam_u->boq_y; + cur_w++; + + c3_y fra_y = (c3y == nam_u->nit_o) ? 0 : met_u.gaf_y + 1; + for( int i = 0; i < fra_y; i++ ) { + buf_y[cur_w] = (nam_u->fra_w >> (8*i)) & 0xff; + cur_w++; + } + // path length - c3_y pat_y = 1 >> met_u.pat_b; + c3_y pat_y = 2; for ( int i = 0; i < pat_y; i++ ) { buf_y[cur_w] = (nam_u->pat_s >> (8*i)) & 0xff; cur_w++; @@ -994,19 +1039,6 @@ _xmas_etch_name(c3_y* buf_y, u3_xmas_name* nam_u) memcpy(buf_y + cur_w, nam_u->pat_c, nam_u->pat_s); cur_w += nam_u->pat_s; - - // possible bloq size - if ( 1 == met_u.boq_b ) { - buf_y[cur_w] = nam_u->boq_y; - cur_w++; - } - - c3_y fra_y = met_u.fra_y + 1; - for( int i = 0; i < fra_y; i++ ) { - buf_y[cur_w] = (nam_u->fra_w >> (8*i)) & 0xff; - cur_w++; - } - return cur_w; //_ames_etch_short(buf_y + cur_w, &met_s); } @@ -1027,10 +1059,9 @@ _xmas_etch_page_pact(c3_y* buf_y, u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u) u3_xmas_page_meat* mat_u = &pac_u->mat_u; - // metabyte TODO fix leak - mat_u->met_u.tot_y = safe_dec(u3r_met(3, u3i_word(mat_u->tot_w))); + mat_u->met_u.tot_y = safe_dec(_xmas_met3_w(mat_u->tot_w)); mat_u->met_u.aut_b = (mat_u->aul_y != 0) & 0x1; - mat_u->met_u.len_y = u3r_met(3, u3i_chub(mat_u->len_w)); + mat_u->met_u.len_y = _xmas_met3_w(mat_u->len_w); c3_y met_y = (mat_u->met_u.tot_y & 0x3 ) << 0 ^ (mat_u->met_u.aut_b & 0x1 ) << 2 ^ (mat_u->met_u.len_y & 0x1F ) << 3; @@ -2605,8 +2636,8 @@ _test_sift_page() { u3_xmas_pact pac_u; memset(&pac_u,0, sizeof(u3_xmas_pact)); - pac_u.typ_y = PACT_PAGE; - u3l_log("checking sift/etch idempotent"); + pac_u.hed_u.typ_y = PACT_PAGE; + u3l_log("%%page checking sift/etch idempotent"); u3_xmas_name* nam_u = &pac_u.pag_u.nam_u; u3_noun her = u3v_wish("~hastuc-dibtux"); u3r_chubs(0, 2, nam_u->her_d, her); @@ -2615,6 +2646,7 @@ _test_sift_page() nam_u->pat_s = strlen(nam_u->pat_c); nam_u->boq_y = 13; nam_u->fra_w = 54; + nam_u->nit_o = c3n; u3_xmas_page_meat* mat_u = &pac_u.pag_u.mat_u; mat_u->len_w = 1024; @@ -2623,6 +2655,12 @@ _test_sift_page() c3_y* buf_y = c3_calloc(PACT_SIZE); c3_w len_w =_xmas_etch_pact(buf_y, &pac_u, her); + + if ( !len_w ) { + u3l_log("%%page etch failed"); + exit(1); + } + u3_xmas_pact nex_u; memset(&nex_u, 0, sizeof(u3_xmas_pact)); _xmas_sift_pact(&nex_u, buf_y, len_w); @@ -2641,7 +2679,7 @@ _test_sift_page() if ( 0 != memcmp(nex_u.pag_u.mat_u.fra_y, pac_u.pag_u.mat_u.fra_y, pac_u.pag_u.mat_u.len_w) ) { u3l_log(RED_TEXT); - u3l_log("mismatch"); + u3l_log("mismatch 1"); u3l_log("got"); _log_buf(nex_u.pag_u.mat_u.fra_y, nex_u.pag_u.mat_u.len_w); u3l_log("expected"); @@ -2653,7 +2691,7 @@ _test_sift_page() pac_u.pag_u.mat_u.fra_y = 0; if ( 0 != memcmp(&nex_u, &pac_u, sizeof(u3_xmas_pact)) ) { u3l_log(RED_TEXT); - u3l_log("mismatch"); + u3l_log("mismatch 2"); u3l_log("got"); _log_pact(&nex_u); u3l_log("expected"); @@ -2667,8 +2705,8 @@ _test_sift_peek() { u3_xmas_pact pac_u; memset(&pac_u,0, sizeof(u3_xmas_pact)); - pac_u.typ_y = PACT_PEEK;; - u3l_log("checking sift/etch idempotent"); + pac_u.hed_u.typ_y = PACT_PEEK;; + u3l_log("%%peek checking sift/etch idempotent"); u3_xmas_name* nam_u = &pac_u.pek_u.nam_u; u3_noun her = u3v_wish("~hastuc-dibtux"); u3r_chubs(0, 2, nam_u->her_d, her); @@ -2677,9 +2715,17 @@ _test_sift_peek() nam_u->pat_s = strlen(nam_u->pat_c); nam_u->boq_y = 13; nam_u->fra_w = 511; + nam_u->nit_o = c3n; + nam_u->aut_o = c3n; c3_y* buf_y = c3_calloc(PACT_SIZE); c3_w len_w =_xmas_etch_pact(buf_y, &pac_u, her); + + if ( !len_w ) { + u3l_log("%%peek etch failed"); + exit(1); + } + u3_xmas_pact nex_u; memset(&nex_u, 0, sizeof(u3_xmas_pact)); _xmas_sift_pact(&nex_u, buf_y, len_w); From a42f1eb476d34bff741f8fbcc96c3541a565dce9 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Fri, 23 Feb 2024 11:00:22 -0500 Subject: [PATCH 038/430] xmas: adds $data, updates $page and $poke packets to use it --- pkg/vere/io/xmas.c | 475 ++++++++++++++++++++++++++++----------------- 1 file changed, 294 insertions(+), 181 deletions(-) diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c index 6ab913e48c..1f1052656e 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/xmas.c @@ -159,7 +159,37 @@ typedef struct _u3_xmas_name { c3_c* pat_c; } u3_xmas_name; +typedef struct _u3_xmas_data_meta { + c3_y bot_y; // total-fragments len (2 bits) + c3_y aul_y; // auth-left (message) type (2 bits) + c3_y aur_y; // auth-right (packet) type (2 bits) + c3_y men_y; // fragment length/type (2 bits) +} u3_xmas_data_meta; + +typedef enum { + AUTH_NONE = 0, + AUTH_NEXT = 1, + AUTH_SIGN = 2, + AUTH_HMAC = 3 +} u3_xmas_auth_type; +typedef struct _u3_xmas_data { + // u3_xmas_data_meta met_u; + c3_w tot_w; // total fragments + struct { + u3_xmas_auth_type typ_e; // none, traversal (none), sig, or hmac + union { // + c3_y sig_y[64]; // signature + c3_y mac_y[32]; // hmac + }; + } aum_u; + struct { + c3_y len_y; // number of hashes (0, 1, or 2) + c3_y has_y[2][32]; // hashes + } aup_u; + c3_w len_w; // fragment length + c3_y* fra_y; // fragment +} u3_xmas_data; typedef struct _u3_xmas_rout { u3_xmas_rout_tag typ_y; // type tag @@ -193,12 +223,6 @@ typedef struct _u3_xmas_page_meat_meta { c3_y len_y; // fragment length (5bit) } u3_xmas_page_meat_meta; -typedef enum _u3_xmas_auth_type { - AUTH_SIG = 1, - AUTH_DIG = 2, - AUTH_HMAC = 3, -} u3_xmas_auth_type; - typedef struct _u3_xmas_page_meat { u3_xmas_page_meat_meta met_u; // metadata c3_w tot_w; // total fragments @@ -221,7 +245,7 @@ typedef struct _u3_xmas_hops { typedef struct _u3_xmas_page_pact { u3_xmas_name nam_u; - u3_xmas_page_meat mat_u; + u3_xmas_data dat_u; union { c3_y sot_u[6]; u3_xmas_hop one_u; @@ -230,10 +254,9 @@ typedef struct _u3_xmas_page_pact { } u3_xmas_page_pact; typedef struct _u3_xmas_poke_pact { - u3_xmas_peek_pact* pek_u; - c3_w tot_w; - c3_y aut_y[96]; // heap allocate?? - c3_y* dat_y; + u3_xmas_name nam_u; + u3_xmas_name pay_u; + u3_xmas_data dat_u; } u3_xmas_poke_pact; typedef struct _u3_xmas_pact { @@ -334,6 +357,67 @@ _log_name(u3_xmas_name* nam_u) u3l_log("path: %s", nam_u->pat_c); } +static void +_log_data(u3_xmas_data* dat_u) +{ + u3l_log("total fragments: %u", dat_u->tot_w); + + switch ( dat_u->aum_u.typ_e ) { + case AUTH_NONE: { + if ( dat_u->aup_u.len_y ) { + u3l_log("strange no auth"); + } + else { + u3l_log("no auth"); + } + } break; + + case AUTH_NEXT: { + if ( 2 != dat_u->aup_u.len_y ) { + u3l_log("bad merkle traversal"); + } + else { + u3l_log("merkle traversal:"); + _log_buf(dat_u->aup_u.has_y[0], 32); + _log_buf(dat_u->aup_u.has_y[1], 32); + } + } break; + + case AUTH_SIGN: { + u3l_log("signature:"); + _log_buf(dat_u->aum_u.sig_y, 64); + } break; + + case AUTH_HMAC: { + u3l_log("hmac:"); + _log_buf(dat_u->aum_u.mac_y, 32); + } break; + } + + switch ( dat_u->aum_u.typ_e ) { + case AUTH_SIGN: + case AUTH_HMAC: { + if ( !dat_u->aup_u.len_y ) { + break; + } + + if ( 4 < dat_u->tot_w ) { + u3l_log("strange inline proof"); + } + else { + u3l_log("inline proof"); + } + + for ( int i = 0; i < dat_u->aup_u.len_y; i++ ) { + _log_buf(dat_u->aup_u.has_y[i], 32); + } + } break; + + default: break; + } + + u3l_log("frag len: %u", dat_u->len_w); +} static void @@ -346,15 +430,7 @@ static void _log_page_pact(u3_xmas_page_pact *pac_u) { _log_name(&pac_u->nam_u); - u3_xmas_page_meat* mat_u = &pac_u->mat_u; - u3l_log("meta"); - u3l_log("total length: %u", mat_u->met_u.tot_y); - u3l_log("auth len length: %u", mat_u->met_u.aut_b); - u3l_log("frag len len: %u", mat_u->met_u.len_y); - u3l_log("actual:"); - u3l_log("total: %u", mat_u->tot_w); - u3l_log("auth len: %u", mat_u->aul_y); - u3l_log("frag len: %u", mat_u->len_w); + _log_data(&pac_u->dat_u); } static void @@ -606,7 +682,7 @@ _ames_sift_word(c3_y buf_y[4]) static c3_o -_xmas_sift_head(c3_y buf_y[8], u3_xmas_head* hed_u) +_xmas_sift_head(c3_y buf_y[8], u3_xmas_head* hed_u) { if ( memcmp(buf_y + 4, &XMAS_COOKIE, XMAS_COOKIE_LEN) ) { return c3n; @@ -695,6 +771,71 @@ _xmas_sift_name(u3_xmas_name* nam_u, c3_y* buf_y, c3_w len_w) return cur_w; } +static c3_w +_xmas_sift_data(u3_xmas_data* dat_u, c3_y* buf_y, c3_w len_w) +{ +#ifdef XMAS_DEBUG + //u3l_log("xmas: sifting data %i", len_w); +#endif + + c3_w cur_w = 0; + u3_xmas_data_meta met_u; + + CHECK_BOUNDS(cur_w + 1); + c3_y met_y = buf_y[cur_w]; + met_u.bot_y = (met_y >> 0) & 0x3; + met_u.aul_y = (met_y >> 2) & 0x3; + met_u.aur_y = (met_y >> 4) & 0x3; + met_u.men_y = (met_y >> 6) & 0x3; + cur_w += 1; + + c3_y tot_y = met_u.bot_y + 1; + CHECK_BOUNDS(cur_w + tot_y); + dat_u->tot_w = 0; + for( int i = 0; i < tot_y; i++ ) { + dat_u->tot_w |= (buf_y[cur_w] << (8*i)); + cur_w++; + } + + c3_y aum_y = ( 2 == met_u.aul_y ) ? 64 : + ( 3 == met_u.aul_y ) ? 32 : 0; + CHECK_BOUNDS(cur_w + aum_y); + memcpy(dat_u->aum_u.sig_y, buf_y + cur_w, aum_y); + cur_w += aum_y; + + dat_u->aum_u.typ_e = met_u.aul_y; // XX + + assert( 3 > met_u.aur_y ); + + CHECK_BOUNDS(cur_w + (met_u.aur_y * 32)); + dat_u->aup_u.len_y = met_u.aur_y; + for( int i = 0; i < met_u.aur_y; i++ ) { + memcpy(dat_u->aup_u.has_y[i], buf_y + cur_w, 32); + cur_w += 32; + } + + c3_y nel_y = met_u.men_y; + + if ( 3 == nel_y ) { + CHECK_BOUNDS(cur_w + 1); + nel_y = buf_y[cur_w]; + cur_w++; + } + + CHECK_BOUNDS(cur_w + nel_y); + dat_u->len_w = 0; + for ( int i = 0; i < nel_y; i++ ) { + dat_u->len_w |= (buf_y[cur_w] << (8*i)); + cur_w++; + } + + CHECK_BOUNDS(cur_w + dat_u->len_w); + dat_u->fra_y = c3_calloc(dat_u->len_w); + memcpy(dat_u->fra_y, buf_y + cur_w, dat_u->len_w); + + return cur_w; +} + static c3_w _xmas_sift_hop_long(u3_xmas_hop* hop_u, c3_y* buf_y, c3_w len_w) { @@ -713,56 +854,17 @@ _xmas_sift_hop_long(u3_xmas_hop* hop_u, c3_y* buf_y, c3_w len_w) static c3_w _xmas_sift_page_pact(u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, c3_w len_w) { - c3_w cur_w = 0; - c3_w nam_w = _xmas_sift_name(&pac_u->nam_u, buf_y, len_w); + c3_w cur_w = 0, nex_w; - if( nam_w == 0 ) { + if ( !(nex_w = _xmas_sift_name(&pac_u->nam_u, buf_y + cur_w, len_w)) ) { return 0; } - cur_w += nam_w; - - u3_xmas_page_meat* mat_u = &pac_u->mat_u; - - CHECK_BOUNDS(cur_w + 1); + cur_w += nex_w; - c3_y met_y = buf_y[cur_w]; - mat_u->met_u.tot_y = (met_y >> 0) & 0x3; - mat_u->met_u.aut_b = (met_y >> 2) & 0x1; - mat_u->met_u.len_y = (met_y >> 3) & 0x1F; - cur_w += 1; - - c3_y tot_y = pac_u->mat_u.met_u.tot_y + 1; - CHECK_BOUNDS(cur_w + tot_y); - mat_u->tot_w = 0; - for( int i = 0; i < tot_y; i++ ) { - mat_u->tot_w |= (buf_y[cur_w] << (8*i)); - cur_w++; - } - - mat_u->aul_y = 0; - if ( 1 == mat_u->met_u.aut_b ) { - CHECK_BOUNDS(cur_w + 1); - mat_u->aul_y = buf_y[cur_w]; - cur_w++; - - CHECK_BOUNDS(cur_w + mat_u->aul_y); - mat_u->aut_y = c3_calloc(mat_u->aul_y); - memcpy(mat_u->aut_y, buf_y + cur_w, mat_u->aul_y); - cur_w += mat_u->aul_y; - } - - c3_y len_y = mat_u->met_u.len_y; - CHECK_BOUNDS(cur_w + len_y); - u3_assert( len_y <= 8 ); - mat_u->len_w = 0; - for ( int i = 0; i < len_y; i++ ) { - mat_u->len_w |= (buf_y[cur_w] << (8*i)); - cur_w++; + if ( !(nex_w = _xmas_sift_data(&pac_u->dat_u, buf_y + cur_w, len_w)) ) { + return 0; } - - CHECK_BOUNDS(cur_w + mat_u->len_w); - mat_u->fra_y = c3_calloc(mat_u->len_w); - memcpy(mat_u->fra_y, buf_y + cur_w, mat_u->len_w); + cur_w += nex_w; switch ( hed_u->nex_y ) { default: { @@ -801,34 +903,6 @@ _xmas_sift_page_pact(u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, } return cur_w; - - /*// next hop - memcpy(pac_u->hop_y, buf_y, 6); - cur_w += 6; - - // path length - pac_u->pat_s = _ames_sift_short(buf_y + cur_w); - cur_w += 2; - - // path contents - pac_u->pat_c = c3_calloc(pac_u->pat_s + 1); - memcpy(pac_u->pat_c, buf_y + cur_w, pac_u->pat_s); - pac_u->pat_c[pac_u->pat_s] = '\0'; - cur_w += pac_u->pat_s + 1; - - // total fragments - pac_u->tot_w = _ames_sift_word(buf_y + cur_w); - cur_w += 4; - - // Authenticator - memcpy(pac_u->aut_y, buf_y + cur_w, 96); - cur_w += 96; - - c3_w dat_w = 1024; // len_w - cur_w; - pac_u->dat_y = c3_calloc(dat_w); - memcpy(pac_u->dat_y, buf_y + cur_w, dat_w); - */ - return 0; } @@ -852,21 +926,26 @@ _xmas_sift_peek_pact(u3_xmas_peek_pact* pac_u, c3_y* buf_y, c3_w len_w) static c3_w _xmas_sift_poke_pact(u3_xmas_poke_pact* pac_u, c3_y* buf_y, c3_w len_w) { - c3_w cur_w = 0; - // Peek portion - pac_u->pek_u = c3_calloc(sizeof(*(pac_u->pek_u))); - cur_w += _xmas_sift_peek_pact(pac_u->pek_u, buf_y, len_w); + c3_w cur_w = 0, nex_w; - // Total fragments - pac_u->tot_w = _ames_sift_word(buf_y + cur_w); - cur_w += 4; + // ack path + if ( !(nex_w = _xmas_sift_name(&pac_u->nam_u, buf_y + cur_w, len_w)) ) { + return 0; + } + cur_w += nex_w; - // Authenticator - memcpy(pac_u->aut_y, buf_y + cur_w, 96); - cur_w += 96; + // payload path + if ( !(nex_w = _xmas_sift_name(&pac_u->pay_u, buf_y + cur_w, len_w)) ) { + return 0; + } + cur_w += nex_w; + + // payload + if ( !(nex_w = _xmas_sift_data(&pac_u->dat_u, buf_y + cur_w, len_w)) ) { + return 0; + } + cur_w += nex_w; - // Datum - memcpy(pac_u->dat_y, buf_y + cur_w, len_w - cur_w); return cur_w; } @@ -1043,72 +1122,107 @@ _xmas_etch_name(c3_y* buf_y, u3_xmas_name* nam_u) //_ames_etch_short(buf_y + cur_w, &met_s); } - - static c3_w -_xmas_etch_page_pact(c3_y* buf_y, u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u) +_xmas_etch_data(c3_y* buf_y, u3_xmas_data* dat_u) { +#ifdef XMAS_DEBUG + +#endif c3_w cur_w = 0; - { - c3_w siz_w = _xmas_etch_name(buf_y + cur_w, &pac_u->nam_u); - if ( siz_w == 0 ) { - return 0; - } - cur_w += siz_w; - } + u3_xmas_data_meta met_u; - u3_xmas_page_meat* mat_u = &pac_u->mat_u; - - mat_u->met_u.tot_y = safe_dec(_xmas_met3_w(mat_u->tot_w)); - mat_u->met_u.aut_b = (mat_u->aul_y != 0) & 0x1; - mat_u->met_u.len_y = _xmas_met3_w(mat_u->len_w); - c3_y met_y = (mat_u->met_u.tot_y & 0x3 ) << 0 - ^ (mat_u->met_u.aut_b & 0x1 ) << 2 - ^ (mat_u->met_u.len_y & 0x1F ) << 3; + met_u.bot_y = safe_dec(_xmas_met3_w(dat_u->tot_w)); + // XX + met_u.aul_y = dat_u->aum_u.typ_e; + met_u.aur_y = dat_u->aup_u.len_y; + + c3_y nel_y = _xmas_met3_w(dat_u->len_w); + met_u.men_y = (3 >= nel_y) ? nel_y : 3; + + c3_y met_y = (met_u.bot_y & 0x3) << 0 + ^ (met_u.aul_y & 0x3) << 2 + ^ (met_u.aur_y & 0x3) << 4 + ^ (met_u.men_y & 0x3) << 6; buf_y[cur_w] = met_y; cur_w++; - c3_y tot_y = mat_u->met_u.tot_y + 1; + c3_y tot_y = met_u.bot_y + 1; for (int i = 0; i < tot_y; i++ ) { - buf_y[cur_w] = (mat_u->tot_w >> (8 * i)) & 0xFF; + buf_y[cur_w] = (dat_u->tot_w >> (8 * i)) & 0xFF; cur_w++; } - if ( mat_u->met_u.aut_b == 1 ) { - buf_y[cur_w] = mat_u->aul_y; - cur_w++; - memcpy(buf_y + cur_w, mat_u->aut_y, mat_u->aul_y); - cur_w += mat_u->aul_y; + switch ( dat_u->aum_u.typ_e ) { + case AUTH_SIGN: { + memcpy(buf_y + cur_w, dat_u->aum_u.sig_y, 64); + cur_w += 64; + } break; + + case AUTH_HMAC: { + memcpy(buf_y + cur_w, dat_u->aum_u.mac_y, 32); + cur_w += 32; + } break; + + default: break; + } + + for ( int i = 0; i < dat_u->aup_u.len_y; i++ ) { + memcpy(buf_y + cur_w, dat_u->aup_u.has_y[i], 32); + cur_w += 32; } - c3_y len_y = mat_u->met_u.len_y; - for( int i = 0; i < len_y; i++ ) { - buf_y[cur_w] = (mat_u->len_w >> (8 * i)) & 0xFF; + if ( 3 == met_u.men_y ) { + buf_y[cur_w] = nel_y; cur_w++; } - memcpy(buf_y + cur_w, mat_u->fra_y, mat_u->len_w); - cur_w += mat_u->len_w; - // TODO: hopcount + memcpy(buf_y + cur_w, (c3_y*)&dat_u->len_w, nel_y); + cur_w += nel_y; + + memcpy(buf_y + cur_w, dat_u->fra_y, dat_u->len_w); + cur_w += dat_u->len_w; + + return cur_w; +} + +static c3_w +_xmas_etch_page_pact(c3_y* buf_y, u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u) +{ + c3_w cur_w = 0, nex_w; + + if ( !(nex_w = _xmas_etch_name(buf_y + cur_w, &pac_u->nam_u)) ) { + return 0; + } + cur_w += nex_w; + + if ( !(nex_w = _xmas_etch_data(buf_y + cur_w, &pac_u->dat_u)) ) { + return 0; + } + cur_w += nex_w; + return cur_w; } static c3_w _xmas_etch_poke_pact(c3_y* buf_y, u3_xmas_poke_pact* pac_u, u3_xmas_head* hed_u) { - c3_w cur_w = 0; + c3_w cur_w = 0, nex_w; - // total - _ames_etch_word(buf_y + cur_w, pac_u->tot_w); - cur_w += 4; + if ( !(nex_w = _xmas_etch_name(buf_y + cur_w, &pac_u->nam_u)) ) { + return 0; + } + cur_w += nex_w; - // auth - memcpy(buf_y + cur_w, pac_u->aut_y, 96); - cur_w += 96; + if ( !(nex_w = _xmas_etch_name(buf_y + cur_w, &pac_u->pay_u)) ) { + return 0; + } + cur_w += nex_w; - // day - memcpy(buf_y + cur_w, pac_u->dat_y, PACT_SIZE - cur_w); + if ( !(nex_w = _xmas_etch_data(buf_y + cur_w, &pac_u->dat_u)) ) { + return 0; + } + cur_w += nex_w; return PACT_SIZE; } @@ -1116,40 +1230,38 @@ _xmas_etch_poke_pact(c3_y* buf_y, u3_xmas_poke_pact* pac_u, u3_xmas_head* hed_u) static c3_w _xmas_etch_pact(c3_y* buf_y, u3_xmas_pact* pac_u, u3_noun her) { - c3_w cur_w = 0; + c3_w cur_w = 0, nex_w; u3_xmas_head* hed_u = &pac_u->hed_u; _xmas_etch_head(hed_u, buf_y + cur_w); cur_w += 8; - c3_w siz_w = 0; - switch ( pac_u->hed_u.typ_y ) { case PACT_POKE: { - c3_w siz_w = _xmas_etch_poke_pact(buf_y + cur_w, &pac_u->pok_u, hed_u); - if ( siz_w == 0 ) { - return 0; + if ( !(nex_w = _xmas_etch_poke_pact(buf_y + cur_w, &pac_u->pok_u, hed_u)) ) { + return 0; } - cur_w += siz_w; } break; + case PACT_PEEK: { - siz_w = _xmas_etch_name(buf_y + cur_w, &pac_u->pek_u.nam_u); - if ( siz_w == 0 ) { - return 0; + if ( !(nex_w = _xmas_etch_name(buf_y + cur_w, &pac_u->pek_u.nam_u)) ) { + return 0; } - cur_w += siz_w; } break; + case PACT_PAGE: { - siz_w = _xmas_etch_page_pact(buf_y + cur_w, &pac_u->pag_u, hed_u); - if ( siz_w == 0 ) { - return 0; + if ( !(nex_w = _xmas_etch_page_pact(buf_y + cur_w, &pac_u->pag_u, hed_u)) ) { + return 0; } - cur_w += siz_w; } break; default: { u3l_log("bad pact type");//u3m_bail(c3__bail); + return 0; } } + + cur_w += nex_w; + return cur_w; } @@ -1683,7 +1795,7 @@ _try_resend(u3_pend_req* req_u) /* _xmas_req_pact_done(): mark packet as done, possibly producing the result */ static u3_weak -_xmas_req_pact_done(u3_xmas* sam_u, u3_xmas_name *nam_u, u3_xmas_page_meat* mat_u, u3_lane* lan_u) +_xmas_req_pact_done(u3_xmas* sam_u, u3_xmas_name *nam_u, u3_xmas_data* dat_u, u3_lane* lan_u) { u3_weak ret = u3_none; c3_d now_d = _get_now_micros(); @@ -1712,14 +1824,14 @@ _xmas_req_pact_done(u3_xmas* sam_u, u3_xmas_name *nam_u, u3_xmas_page_meat* mat_ // First packet received, instantiate request fully if ( req_u->tot_w == 0 ) { u3_assert( siz_w == 1024); // boq_y == 13 - req_u->dat_y = c3_calloc(siz_w * mat_u->tot_w); - req_u->wat_u = c3_calloc(sizeof(u3_pact_stat) * mat_u->tot_w); - req_u->tot_w = mat_u->tot_w; - _init_bitset(&req_u->was_u, mat_u->tot_w + 1); + req_u->dat_y = c3_calloc(siz_w * dat_u->tot_w); + req_u->wat_u = c3_calloc(sizeof(u3_pact_stat) * dat_u->tot_w); + req_u->tot_w = dat_u->tot_w; + _init_bitset(&req_u->was_u, dat_u->tot_w + 1); req_u->lef_w = 2; } - if ( mat_u->tot_w <= nam_u->fra_w ) { + if ( dat_u->tot_w <= nam_u->fra_w ) { u3l_log("xmas: invalid packet (exceeded bounds)"); return u3_none; } @@ -1757,7 +1869,7 @@ _xmas_req_pact_done(u3_xmas* sam_u, u3_xmas_name *nam_u, u3_xmas_page_meat* mat_ } - memcpy(req_u->dat_y + (siz_w * nam_u->fra_w), mat_u->fra_y, mat_u->len_w); + memcpy(req_u->dat_y + (siz_w * nam_u->fra_w), dat_u->fra_y, dat_u->len_w); c3_d wen_d = _get_now_micros(); _try_resend(req_u); @@ -2274,14 +2386,14 @@ _xmas_hear_page(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w, u3_lane lan_u) u3_noun wir = u3nc(c3__xmas, u3_nul); c3_s fra_s; - u3_noun fra = u3i_bytes(pac_u->pag_u.mat_u.len_w, pac_u->pag_u.mat_u.fra_y) ; + u3_noun fra = u3i_bytes(pac_u->pag_u.dat_u.len_w, pac_u->pag_u.dat_u.fra_y) ; /*if ( dop_o == c3n && pac_u->pag_u.nam_u.fra_w == 150) { dop_o = c3y; u3l_log("simulating dropped packet"); return; }*/ - u3_weak res = _xmas_req_pact_done(sam_u, &pac_u->pag_u.nam_u, &pac_u->pag_u.mat_u, &lan_u); + u3_weak res = _xmas_req_pact_done(sam_u, &pac_u->pag_u.nam_u, &pac_u->pag_u.dat_u, &lan_u); u3l_log("done"); if ( u3_none != res ) { @@ -2648,10 +2760,11 @@ _test_sift_page() nam_u->fra_w = 54; nam_u->nit_o = c3n; - u3_xmas_page_meat* mat_u = &pac_u.pag_u.mat_u; - mat_u->len_w = 1024; - mat_u->fra_y = c3_calloc(1024); - mat_u->tot_w = 1000; + u3_xmas_data* dat_u = &pac_u.pag_u.dat_u; + dat_u->aum_u.typ_e = AUTH_NONE; + dat_u->tot_w = 1000; + dat_u->len_w = 1024; + dat_u->fra_y = c3_calloc(1024); c3_y* buf_y = c3_calloc(PACT_SIZE); c3_w len_w =_xmas_etch_pact(buf_y, &pac_u, her); @@ -2677,18 +2790,18 @@ _test_sift_page() nex_u.pag_u.nam_u.pat_c = 0; pac_u.pag_u.nam_u.pat_c = 0; - if ( 0 != memcmp(nex_u.pag_u.mat_u.fra_y, pac_u.pag_u.mat_u.fra_y, pac_u.pag_u.mat_u.len_w) ) { + if ( 0 != memcmp(nex_u.pag_u.dat_u.fra_y, pac_u.pag_u.dat_u.fra_y, pac_u.pag_u.dat_u.len_w) ) { u3l_log(RED_TEXT); u3l_log("mismatch 1"); - u3l_log("got"); - _log_buf(nex_u.pag_u.mat_u.fra_y, nex_u.pag_u.mat_u.len_w); - u3l_log("expected"); - _log_buf(pac_u.pag_u.mat_u.fra_y, pac_u.pag_u.mat_u.len_w); + u3l_log("got len %u", nex_u.pag_u.dat_u.len_w); + _log_buf(nex_u.pag_u.dat_u.fra_y, nex_u.pag_u.dat_u.len_w); + u3l_log("expected len %u", pac_u.pag_u.dat_u.len_w); + _log_buf(pac_u.pag_u.dat_u.fra_y, pac_u.pag_u.dat_u.len_w); exit(1); } - nex_u.pag_u.mat_u.fra_y = 0; - pac_u.pag_u.mat_u.fra_y = 0; + nex_u.pag_u.dat_u.fra_y = 0; + pac_u.pag_u.dat_u.fra_y = 0; if ( 0 != memcmp(&nex_u, &pac_u, sizeof(u3_xmas_pact)) ) { u3l_log(RED_TEXT); u3l_log("mismatch 2"); From 5f7e2370151f036c48ecf7c5e2382a20a9487a6e Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Fri, 23 Feb 2024 12:35:48 -0500 Subject: [PATCH 039/430] xmas: remove ship from _xmas_etch_pact() signature --- pkg/vere/io/xmas.c | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c index 1f1052656e..73f5fafb71 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/xmas.c @@ -1228,7 +1228,7 @@ _xmas_etch_poke_pact(c3_y* buf_y, u3_xmas_poke_pact* pac_u, u3_xmas_head* hed_u) } static c3_w -_xmas_etch_pact(c3_y* buf_y, u3_xmas_pact* pac_u, u3_noun her) +_xmas_etch_pact(c3_y* buf_y, u3_xmas_pact* pac_u) { c3_w cur_w = 0, nex_w; u3_xmas_head* hed_u = &pac_u->hed_u; @@ -1759,7 +1759,7 @@ _try_resend(u3_pend_req* req_u) los_o = c3y; c3_y* buf_y = c3_calloc(PACT_SIZE); req_u->pac_u->pek_u.nam_u.fra_w = i; - c3_w siz_w = _xmas_etch_pact(buf_y, req_u->pac_u, 0); + c3_w siz_w = _xmas_etch_pact(buf_y, req_u->pac_u); if( siz_w == 0 ) { u3l_log("failed to etch"); u3_assert( 0 ); @@ -1773,7 +1773,7 @@ _try_resend(u3_pend_req* req_u) c3_y* buf_y = c3_calloc(PACT_SIZE); req_u->pac_u->pek_u.nam_u.fra_w = i; u3l_log("slow resending %u ", i); - c3_w siz_w = _xmas_etch_pact(buf_y, req_u->pac_u, 0); + c3_w siz_w = _xmas_etch_pact(buf_y, req_u->pac_u); if( siz_w == 0 ) { u3l_log("failed to etch"); u3_assert( 0 ); @@ -1880,7 +1880,7 @@ _xmas_req_pact_done(u3_xmas* sam_u, u3_xmas_name *nam_u, u3_xmas_data* dat_u, u3 c3_y* buf_y = c3_calloc(PACT_SIZE); req_u->pac_u->pek_u.nam_u.fra_w = req_u->old_w; u3l_log("resending %u on num %u", req_u->old_w, nam_u->fra_w); - c3_w siz_w = _xmas_etch_pact(buf_y, req_u->pac_u, 0); + c3_w siz_w = _xmas_etch_pact(buf_y, req_u->pac_u); if( siz_w == 0 ) { u3l_log("failed to etch"); u3_assert( 0 ); @@ -1913,12 +1913,7 @@ static void _xmas_send(u3_xmas_pact* pac_u) //u3l_log("_ames_send %s %u", _str_typ(pac_u->hed_u.typ_y), // pac_u->rut_u.lan_u.por_s); c3_y* buf_y = c3_calloc(PACT_SIZE); - c3_w siz_w; - { - u3_noun who = 0; - - siz_w = _xmas_etch_pact(buf_y, pac_u, who); - } + c3_w siz_w = _xmas_etch_pact(buf_y, pac_u); _xmas_send_buf(sam_u, pac_u->rut_u.lan_u, buf_y, siz_w); } @@ -2416,7 +2411,7 @@ _xmas_hear_page(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w, u3_lane lan_u) c3_y* cur_y = buf_y; for(int i = 0; i < win_w; i++) { nex_u->pek_u.nam_u.fra_w = nex_w + i; - c3_w siz_w =_xmas_etch_pact(cur_y, nex_u, 0); + c3_w siz_w =_xmas_etch_pact(cur_y, nex_u); if( siz_w == 0 ) { u3l_log("failed to etch"); u3_assert( 0 ); @@ -2751,8 +2746,12 @@ _test_sift_page() pac_u.hed_u.typ_y = PACT_PAGE; u3l_log("%%page checking sift/etch idempotent"); u3_xmas_name* nam_u = &pac_u.pag_u.nam_u; - u3_noun her = u3v_wish("~hastuc-dibtux"); - u3r_chubs(0, 2, nam_u->her_d, her); + + { + u3_noun her = u3v_wish("~hastuc-dibtux"); + u3r_chubs(0, 2, nam_u->her_d, her); + u3z(her); + } nam_u->rif_w = 15; nam_u->pat_c = "foo/bar"; nam_u->pat_s = strlen(nam_u->pat_c); @@ -2767,7 +2766,7 @@ _test_sift_page() dat_u->fra_y = c3_calloc(1024); c3_y* buf_y = c3_calloc(PACT_SIZE); - c3_w len_w =_xmas_etch_pact(buf_y, &pac_u, her); + c3_w len_w =_xmas_etch_pact(buf_y, &pac_u); if ( !len_w ) { u3l_log("%%page etch failed"); @@ -2821,8 +2820,11 @@ _test_sift_peek() pac_u.hed_u.typ_y = PACT_PEEK;; u3l_log("%%peek checking sift/etch idempotent"); u3_xmas_name* nam_u = &pac_u.pek_u.nam_u; - u3_noun her = u3v_wish("~hastuc-dibtux"); - u3r_chubs(0, 2, nam_u->her_d, her); + { + u3_noun her = u3v_wish("~hastuc-dibtux"); + u3r_chubs(0, 2, nam_u->her_d, her); + u3z(her); + } nam_u->rif_w = 15; nam_u->pat_c = "foo/bar"; nam_u->pat_s = strlen(nam_u->pat_c); @@ -2832,7 +2834,7 @@ _test_sift_peek() nam_u->aut_o = c3n; c3_y* buf_y = c3_calloc(PACT_SIZE); - c3_w len_w =_xmas_etch_pact(buf_y, &pac_u, her); + c3_w len_w =_xmas_etch_pact(buf_y, &pac_u); if ( !len_w ) { u3l_log("%%peek etch failed"); From 445645fcaace5ce2ca5dd762c273996c27d1659d Mon Sep 17 00:00:00 2001 From: parallels Date: Fri, 23 Feb 2024 12:05:04 -0600 Subject: [PATCH 040/430] xmas: add headers for lss --- pkg/vere/io/blake.c | 172 +++++++++++++++++++++++--------------------- pkg/vere/io/blake.h | 69 ++++++++++++++++++ 2 files changed, 158 insertions(+), 83 deletions(-) create mode 100644 pkg/vere/io/blake.h diff --git a/pkg/vere/io/blake.c b/pkg/vere/io/blake.c index d7dfc0a34a..804b4a4bc2 100644 --- a/pkg/vere/io/blake.c +++ b/pkg/vere/io/blake.c @@ -5,9 +5,8 @@ #include #include #include "urcrypt.h" +#include "blake.h" -#define BLAKE3_BLOCK_LEN 64 -#define BLAKE3_OUT_LEN 32 #define pf(label, ...) { fprintf(stderr, "%s (%u)\r\n", label, __LINE__); fprintf(stderr, __VA_ARGS__); } #define pl { fprintf(stderr, "(%u)", __LINE__); } @@ -23,13 +22,6 @@ enum blake3_flags { DERIVE_KEY_MATERIAL = 1 << 6, }; -typedef struct _blake_node { - c3_y cev_y[BLAKE3_OUT_LEN]; - c3_y boq_y[BLAKE3_BLOCK_LEN]; - c3_d con_d; - c3_y len_y; - c3_y fag_y; -} blake_node; @@ -83,13 +75,6 @@ static c3_y IV[BLAKE3_OUT_LEN] = {103, 230, 9, 106, 133, 174, 103, 187, 114, 243 #define VEC_SCALING 2 -typedef struct _u3_raw_vec { - c3_w siz_w; - c3_w len_w; - void** vod_p; -} u3_raw_vec; - -#define u3_vec(type) u3_raw_vec static void _vec_init(u3_raw_vec* vec_u, c3_w siz_w) @@ -221,32 +206,17 @@ static void _vec_weld_mut(u3_raw_vec* fst_u, u3_raw_vec* snd_u) //func (h Hash) String() string { return hex.EncodeToString(h[:]) } //type Pair [2][8]uint32 -typedef struct _pair { - c3_y sin_y[BLAKE3_OUT_LEN]; - c3_y dex_y[BLAKE3_OUT_LEN]; -} pair; - -//type subtree [2]int -typedef struct _subtree { - c3_d sin_d; - c3_d dex_d; -} subtree; - -typedef struct _verifier { - c3_d num_d; - subtree sub_u; - u3_vec(c3_y[BLAKE3_OUT_LEN]) que_u; - u3_vec(c3_w[BLAKE3_OUT_LEN]) sta_u; -} verifier; + + static void -_log_subtree(subtree* sub_u) +_log_blake_subtree(blake_subtree* sub_u) { fprintf(stderr, "sub: (%llu, %llu)\r\n", sub_u->sin_d, sub_u->dex_d); } static void -_log_verifier(verifier* ver_u) +_log_verifier(blake_verifier* ver_u) { fprintf(stderr, "num: %llu\r\n", ver_u->num_d); fprintf(stderr, "sub: (%llu, %llu)\r\n", ver_u->sub_u.sin_d, ver_u->sub_u.dex_d); @@ -293,7 +263,7 @@ static c3_d _bitlen(c3_d bit_d) } static c3_d -_height(subtree* sub_u) +_height(blake_subtree* sub_u) { c3_d ret_d = _bitlen(ABS_DIFF(sub_u->dex_d, sub_u->sin_d)) - 1; return ret_d; @@ -360,11 +330,9 @@ _compress_chunk(c3_y* dat_y, c3_w dat_w, c3_y cev_y[BLAKE3_OUT_LEN], c3_d con_d, return nod_u; } -static -blake_node* _leaf_hash(c3_d con_d) +blake_node* leaf_hash(c3_y* dat_y, c3_w dat_w, c3_d con_d) { - c3_y* dat_y = c3_calloc(1024); - return _compress_chunk(dat_y, 1024, IV, con_d, 0); + return _compress_chunk(dat_y, dat_w, IV, con_d, 0); } static blake_node* @@ -400,37 +368,39 @@ _root_hash(c3_y out_y[64], blake_node* nod_u) } void print_pair(void* vod_p, c3_w i_w) { - pair* par_u = (pair*)vod_p; + blake_pair* par_u = (blake_pair*)vod_p; fprintf(stderr, "pair: %u", i_w); _log_buf("sin: ", par_u->sin_y, BLAKE3_OUT_LEN); _log_buf("dex: ", par_u->dex_y, BLAKE3_OUT_LEN); } +c3_w emp_w = 1024; +static c3_y emp_y[1024] = {0}; -static void recurse_subtree(subtree* sub_u, blake_node* nod_u, u3_vec(pair)* par_u) +static void recurse_blake_subtree(blake_subtree* sub_u, blake_node* nod_u, u3_vec(pair)* par_u) { c3_d hit_d = _height(sub_u); if ( hit_d == 0 ) { - blake_node* new_u = _leaf_hash(sub_u->sin_d); + blake_node* new_u = leaf_hash(emp_y, emp_w, sub_u->sin_d); memcpy(nod_u, new_u, sizeof(blake_node)); return; } c3_d mid_d = sub_u->sin_d + _largest_pow2(ABS_DIFF(sub_u->dex_d, sub_u->sin_d)); - subtree* sin_u = c3_calloc(sizeof(subtree)); - *sin_u = (subtree){sub_u->sin_d, mid_d}; + blake_subtree* sin_u = c3_calloc(sizeof(blake_subtree)); + *sin_u = (blake_subtree){sub_u->sin_d, mid_d}; blake_node* lod_u = c3_calloc(sizeof(blake_node)); u3_vec(pair)* lar_u = _vec_make(8); - recurse_subtree(sin_u, lod_u, lar_u); + recurse_blake_subtree(sin_u, lod_u, lar_u); - subtree* dex_u = c3_calloc(sizeof(subtree)); - *dex_u = (subtree){mid_d, sub_u->dex_d}; + blake_subtree* dex_u = c3_calloc(sizeof(blake_subtree)); + *dex_u = (blake_subtree){mid_d, sub_u->dex_d}; blake_node* rod_u = c3_calloc(sizeof(blake_node)); u3_vec(pair)* rar_u = _vec_make(8); - recurse_subtree(dex_u, rod_u, rar_u); + recurse_blake_subtree(dex_u, rod_u, rar_u); - pair* new_u = c3_calloc(sizeof(pair)); + blake_pair* new_u = c3_calloc(sizeof(blake_pair)); _make_chain_value(new_u->sin_y, lod_u); _make_chain_value(new_u->dex_y, rod_u); memcpy(nod_u, _parent_hash(lod_u, rod_u), sizeof(blake_node)); @@ -442,18 +412,18 @@ static void recurse_subtree(subtree* sub_u, blake_node* nod_u, u3_vec(pair)* par static void _bao_build(c3_d num_d, c3_y has_y[64], u3_vec(c3_w[8])* pof_u, u3_vec(pair)* par_u) { - subtree* sub_u = c3_calloc(sizeof(subtree)); - *sub_u = (subtree){0, num_d}; + blake_subtree* sub_u = c3_calloc(sizeof(blake_subtree)); + *sub_u = (blake_subtree){0, num_d}; blake_node* nod_u = c3_calloc(sizeof(blake_node)); - recurse_subtree(sub_u, nod_u, par_u); + recurse_blake_subtree(sub_u, nod_u, par_u); _root_hash(has_y, nod_u); if ( _vec_len(par_u) != 0 ) { for ( c3_w i_w = _bitlen(num_d - 1); i_w > 1; i_w-- ) { - pair* pir_u = _vec_popf(par_u); + blake_pair* pir_u = _vec_popf(par_u); _vec_append(pof_u, pir_u->dex_y); } - pair* pir_u = _vec_popf(par_u); + blake_pair* pir_u = _vec_popf(par_u); _vec_append(pof_u, pir_u->dex_y); _vec_append(pof_u, pir_u->sin_y); for ( c3_w i_w = 0; i_w < (_vec_len(pof_u) >> 1); i_w++ ) { @@ -464,33 +434,33 @@ _bao_build(c3_d num_d, c3_y has_y[64], u3_vec(c3_w[8])* pof_u, u3_vec(pair)* par } static void -_push_leaf(verifier* ver_u, c3_y lef_y[BLAKE3_OUT_LEN]) +_push_leaf(blake_verifier* ver_u, c3_y lef_y[BLAKE3_OUT_LEN]) { _vec_append(&ver_u->que_u, lef_y); } static void -_pop_leaf(c3_y out_y[BLAKE3_OUT_LEN], verifier* ver_u) +_pop_leaf(c3_y out_y[BLAKE3_OUT_LEN], blake_verifier* ver_u) { c3_y* res_y = _vec_popf(&ver_u->que_u); memcpy(out_y, res_y, BLAKE3_OUT_LEN); } static void -_push_parent(verifier* ver_u, c3_y par_y[BLAKE3_OUT_LEN]) +_push_parent(blake_verifier* ver_u, c3_y par_y[BLAKE3_OUT_LEN]) { _vec_append(&ver_u->sta_u, par_y); } static void -_pop_parent(c3_y out_y[BLAKE3_OUT_LEN], verifier* ver_u) +_pop_parent(c3_y out_y[BLAKE3_OUT_LEN], blake_verifier* ver_u) { c3_y* res_y = _vec_popl(&ver_u->sta_u); memcpy(out_y, res_y, BLAKE3_OUT_LEN); } static void -_verifier_next(verifier* ver_u) +_verifier_next(blake_verifier* ver_u) { if ( _height(&ver_u->sub_u) == 0 ) { ver_u->sub_u.sin_d += 1; @@ -499,13 +469,13 @@ _verifier_next(verifier* ver_u) ver_u->sub_u.dex_d = ver_u->num_d; } } else { - ver_u->sub_u = (subtree){ver_u->sub_u.sin_d, ver_u->sub_u.sin_d + _largest_pow2(ABS_DIFF(ver_u->sub_u.dex_d, ver_u->sub_u.sin_d))}; + ver_u->sub_u = (blake_subtree){ver_u->sub_u.sin_d, ver_u->sub_u.sin_d + _largest_pow2(ABS_DIFF(ver_u->sub_u.dex_d, ver_u->sub_u.sin_d))}; } } static c3_o -_verifier_init(verifier* ver_u, c3_y rot_y[BLAKE3_BLOCK_LEN], u3_vec(c3_y[BLAKE3_OUT_LEN])* pof_u) +_verifier_init(blake_verifier* ver_u, c3_y rot_y[BLAKE3_BLOCK_LEN], u3_vec(c3_y[BLAKE3_OUT_LEN])* pof_u) { if ( _vec_len(pof_u) == 0 ) { return ver_u->num_d <= 1 ? c3y : c3n; @@ -527,7 +497,7 @@ _verifier_init(verifier* ver_u, c3_y rot_y[BLAKE3_BLOCK_LEN], u3_vec(c3_y[BLAKE3 return c3n; } - ver_u->sub_u = (subtree){2,4}; + ver_u->sub_u = (blake_subtree){2,4}; _vec_init(&ver_u->que_u, 8); _vec_append(&ver_u->que_u, _vec_popf(pof_u)); @@ -544,21 +514,21 @@ _verifier_init(verifier* ver_u, c3_y rot_y[BLAKE3_BLOCK_LEN], u3_vec(c3_y[BLAKE3 } static c3_o -_veri_check_leaf(verifier* ver_u, c3_d lef_d) +_veri_check_leaf(blake_verifier* ver_u, c3_d lef_d, c3_y* dat_y, c3_w dat_w) { if ( _vec_len(&ver_u->que_u) > 0 ) { c3_y out_y[BLAKE3_OUT_LEN]; _pop_leaf(out_y, ver_u); c3_y cav_y[BLAKE3_OUT_LEN]; - blake_node* nod_u = _leaf_hash(lef_d); + blake_node* nod_u = leaf_hash(dat_y, dat_w, lef_d); _make_chain_value(cav_y, nod_u); return 0 == memcmp(cav_y, out_y, BLAKE3_OUT_LEN) ? c3y : c3n; } else if ( _vec_len(&ver_u->sta_u) > 0 ) { c3_y out_y[BLAKE3_OUT_LEN]; _pop_parent(out_y, ver_u); c3_y cav_y[BLAKE3_OUT_LEN]; - blake_node* nod_u = _leaf_hash(lef_d); + blake_node* nod_u = leaf_hash(dat_y, dat_w, lef_d); _make_chain_value(cav_y, nod_u); return 0 == memcmp(cav_y, out_y, BLAKE3_OUT_LEN) ? c3y : c3n; } @@ -566,7 +536,7 @@ _veri_check_leaf(verifier* ver_u, c3_d lef_d) } static c3_o -_veri_check_pair(verifier* ver_u, pair* par_u) +_veri_check_pair(blake_verifier* ver_u, blake_pair* par_u) { if ( _vec_len(&ver_u->sta_u) == 0 ) { fprintf(stderr, "bailing empty stack\r\n"); @@ -581,22 +551,56 @@ _veri_check_pair(verifier* ver_u, pair* par_u) return _(memcmp(par_y, cav_y, BLAKE3_OUT_LEN) == 0); } -static void _veri_init(verifier* ver_u, c3_d num_d) +static void _veri_init(blake_verifier* ver_u, c3_d num_d) { ver_u->num_d = num_d; - ver_u->sub_u = (subtree){0, num_d}; + ver_u->sub_u = (blake_subtree){0, num_d}; } - -#ifdef BLAKE_TEST -static void _test_root_hash() + +blake_bao* +blake_bao_make(c3_w num_w, c3_y has_y[BLAKE3_BLOCK_LEN], u3_vec(c3_y[8])* pof_u, u3_vec(blake_pair)* par_u) { - blake_node* nod_u = _leaf_hash(0); - c3_y out_y[BLAKE3_BLOCK_LEN]; - _root_hash(out_y, nod_u); - _log_buf("root", out_y, BLAKE3_BLOCK_LEN); + blake_bao* bao_u = c3_calloc(sizeof(blake_bao)); + // TODO semantics here + bao_u->par_u = *par_u; + _veri_init(&bao_u->ver_u, num_w); + if ( c3n == _verifier_init(&bao_u->ver_u, has_y, pof_u) ) { + return NULL; + } + return bao_u; } +c3_y +bao_ingest(blake_bao* bao_u, c3_w num_w, c3_y* dat_y, c3_w dat_w) +{ + if ( bao_u->con_w != num_w ) { + return BAO_BAD_ORDER; + } + if ( c3n == _veri_check_leaf(&bao_u->ver_u, num_w, dat_y, dat_w) ) { + return BAO_FAILED; + } + if ( num_w < _vec_len(&bao_u->par_u) ) { + blake_pair* pir_u = _vec_get(blake_pair, &bao_u->par_u, num_w); + if ( _veri_check_pair(&bao_u->ver_u, pir_u) == c3n ) { + return BAO_FAILED; + } + _verifier_next(&bao_u->ver_u); + if ( _height(&bao_u->ver_u.sub_u) == 0 ) { + _push_leaf(&bao_u->ver_u, pir_u->sin_y); + _push_leaf(&bao_u->ver_u, pir_u->dex_y); + _verifier_next(&bao_u->ver_u); + _verifier_next(&bao_u->ver_u); + } else { + _push_parent(&bao_u->ver_u, pir_u->dex_y); + _push_parent(&bao_u->ver_u, pir_u->sin_y); + } + } +} + +#ifdef BLAKE_TEST + + static void _test_lead_zeros() { #define asrt(x,y) if ( x != y ) { fprintf(stderr, "failed (line: %u) got %llu expected %llu", __LINE__, x,y); exit(1); } @@ -630,16 +634,19 @@ static void _test_lead_zeros() #undef asrt } + static void _test_bao() { + c3_w dat_w = 1024; + c3_y* dat_y = c3_calloc(dat_w); for ( c3_w num_w = 1; num_w <= 100; num_w++ ) { c3_y has_y[BLAKE3_BLOCK_LEN]; u3_vec(c3_y[8])* pof_u = _vec_make(10); - u3_vec(pair)* par_u = _vec_make(10); + u3_vec(blake_pair)* par_u = _vec_make(10); _bao_build(num_w, has_y, pof_u, par_u); - verifier ver_u; - memset(&ver_u, 0, sizeof(verifier)); + blake_verifier ver_u; + memset(&ver_u, 0, sizeof(blake_verifier)); _veri_init(&ver_u, num_w); if ( c3n == _verifier_init(&ver_u, has_y, pof_u) ) { fprintf(stderr, "Failed on %u\r\n", num_w); @@ -648,19 +655,18 @@ static void _test_bao() if ( num_w == 1 ) { c3_y out_y[BLAKE3_BLOCK_LEN]; - blake_node* nod_u = _leaf_hash(0); + blake_node* nod_u = leaf_hash(dat_y, dat_w, 0); _root_hash(out_y, nod_u); if ( memcmp(out_y, has_y, BLAKE3_BLOCK_LEN) != 0 ) { _log_buf("expected", has_y, BLAKE3_BLOCK_LEN); _log_buf("got", out_y, BLAKE3_BLOCK_LEN); fprintf(stderr, "1 special case\r\n"); - exit(1); } continue; } for ( c3_w i_w = 0; i_w < num_w; i_w++ ) { - if ( c3n == _veri_check_leaf(&ver_u, i_w) ) { + if ( c3n == _veri_check_leaf(&ver_u, i_w, dat_y, dat_w) ) { fprintf(stderr, "Check leaf %u failed for %u\r\n", i_w, num_w); exit(1); } @@ -668,7 +674,7 @@ static void _test_bao() // pl // _log_verifier(&ver_u); if ( i_w < _vec_len(par_u) ) { - pair* pir_u = _vec_get(pair, par_u, i_w); + blake_pair* pir_u = _vec_get(blake_pair, par_u, i_w); if ( _veri_check_pair(&ver_u, pir_u) == c3n ) { fprintf(stderr, "check pair failed (%u, %u)", i_w, num_w); exit(1); diff --git a/pkg/vere/io/blake.h b/pkg/vere/io/blake.h new file mode 100644 index 0000000000..03309bd8a2 --- /dev/null +++ b/pkg/vere/io/blake.h @@ -0,0 +1,69 @@ +#ifndef VERE_BLAKE_H +#define VERE_BLAKE_H + +#include "vere.h" +#include "ivory.h" + +#include "noun.h" +#include "ur.h" + +#define BLAKE3_BLOCK_LEN 64 +#define BLAKE3_OUT_LEN 32 + +enum bao_ingest_result { + BAO_BAD_ORDER = 1, + BAO_FAILED = 2, + BAO_SUCCESS = 3 +}; + + + +typedef struct _blake_pair { + c3_y sin_y[BLAKE3_OUT_LEN]; + c3_y dex_y[BLAKE3_OUT_LEN]; +} blake_pair; + +typedef struct _u3_raw_vec { + c3_w siz_w; + c3_w len_w; + void** vod_p; +} u3_raw_vec; + +#define u3_vec(type) u3_raw_vec + + +typedef struct _blake_node { + c3_y cev_y[BLAKE3_OUT_LEN]; + c3_y boq_y[BLAKE3_BLOCK_LEN]; + c3_d con_d; + c3_y len_y; + c3_y fag_y; +} blake_node; + +typedef struct _blake_subtree { + c3_d sin_d; + c3_d dex_d; +} blake_subtree; + +typedef struct _blake_verifier { + c3_d num_d; + c3_w con_w; + blake_subtree sub_u; + u3_vec(c3_y[BLAKE3_OUT_LEN]) que_u; + u3_vec(c3_w[BLAKE3_OUT_LEN]) sta_u; +} blake_verifier; + +typedef struct _blake_bao { + c3_w con_w; + u3_vec(blake_pair) par_u; + blake_verifier ver_u; +} blake_bao; + + +blake_node* blake_leaf_hash(c3_y* dat_y, c3_w dat_w, c3_d con_d); + +blake_bao* blake_bao_make(c3_w, c3_y[BLAKE3_BLOCK_LEN], u3_vec(c3_y[8])*, u3_vec(blake_pair)*); + +c3_y bao_ingest(blake_bao*, c3_w, c3_y*, c3_w); + +#endif \ No newline at end of file From c80ed98f112c850d50febe4bc41e4c2c5709d7f8 Mon Sep 17 00:00:00 2001 From: parallels Date: Fri, 23 Feb 2024 12:29:45 -0600 Subject: [PATCH 041/430] xmas: fix ingest return values --- pkg/vere/io/blake.c | 5 +++++ pkg/vere/io/blake.h | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/pkg/vere/io/blake.c b/pkg/vere/io/blake.c index 804b4a4bc2..93434b83eb 100644 --- a/pkg/vere/io/blake.c +++ b/pkg/vere/io/blake.c @@ -596,6 +596,11 @@ bao_ingest(blake_bao* bao_u, c3_w num_w, c3_y* dat_y, c3_w dat_w) _push_parent(&bao_u->ver_u, pir_u->sin_y); } } + bao_u->con_w++; + if ( bao_u->con_w == bao_u->ver_u.num_d ) { + return BAO_DONE; + } + return BAO_GOOD; } #ifdef BLAKE_TEST diff --git a/pkg/vere/io/blake.h b/pkg/vere/io/blake.h index 03309bd8a2..32d8953100 100644 --- a/pkg/vere/io/blake.h +++ b/pkg/vere/io/blake.h @@ -13,7 +13,8 @@ enum bao_ingest_result { BAO_BAD_ORDER = 1, BAO_FAILED = 2, - BAO_SUCCESS = 3 + BAO_GOOD = 3, + BAO_DONE = 4, }; From 1546812444d50923dddef5064f7482826a06dab9 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Fri, 23 Feb 2024 14:39:06 -0500 Subject: [PATCH 042/430] xmas: removes old $meat types, fixes cursor in _xmas_sift_data() --- pkg/vere/io/xmas.c | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c index 73f5fafb71..56fc85aa1b 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/xmas.c @@ -217,22 +217,6 @@ typedef struct _u3_xmas_peek_pact { u3_xmas_name nam_u; } u3_xmas_peek_pact; -typedef struct _u3_xmas_page_meat_meta { - c3_y tot_y; // total fragments length (2bit) - c3_o aut_b; // authentication len len (1bit) - c3_y len_y; // fragment length (5bit) -} u3_xmas_page_meat_meta; - -typedef struct _u3_xmas_page_meat { - u3_xmas_page_meat_meta met_u; // metadata - c3_w tot_w; // total fragments - c3_y aul_y; // authentication length - u3_xmas_auth_type ayp_y; // authentication type - c3_y* aut_y; // authentication - c3_w len_w; // fragment length - c3_y* fra_y; // fragment -} u3_xmas_page_meat; - typedef struct _u3_xmas_hop { c3_w len_w; c3_y* dat_y; @@ -832,6 +816,7 @@ _xmas_sift_data(u3_xmas_data* dat_u, c3_y* buf_y, c3_w len_w) CHECK_BOUNDS(cur_w + dat_u->len_w); dat_u->fra_y = c3_calloc(dat_u->len_w); memcpy(dat_u->fra_y, buf_y + cur_w, dat_u->len_w); + cur_w += dat_u->len_w; return cur_w; } From 3ba1d10f5f672da478395d336b6c97e4439e029f Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Fri, 23 Feb 2024 17:01:35 -0500 Subject: [PATCH 043/430] xmas: parse into correct header struct, assert protocol version --- pkg/vere/io/xmas.c | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c index 56fc85aa1b..5489c9b301 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/xmas.c @@ -680,6 +680,8 @@ _xmas_sift_head(c3_y buf_y[8], u3_xmas_head* hed_u) hed_u->hop_y = (hed_w >> 9) & 0x7; hed_u->mug_w = (hed_w >> 12) & 0xFFFFFF; + assert( 1 == hed_u->pro_y ); + return c3y; /*if(c3o((hed_u->typ_y == PACT_PEEK), (hed_u->typ_y == PACT_POKE))) { @@ -837,7 +839,7 @@ _xmas_sift_hop_long(u3_xmas_hop* hop_u, c3_y* buf_y, c3_w len_w) static c3_w -_xmas_sift_page_pact(u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, c3_w len_w) +_xmas_sift_page_pact(u3_xmas_page_pact* pac_u, c3_y nex_y, c3_y* buf_y, c3_w len_w) { c3_w cur_w = 0, nex_w; @@ -851,7 +853,7 @@ _xmas_sift_page_pact(u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u, c3_y* buf_y, } cur_w += nex_w; - switch ( hed_u->nex_y ) { + switch ( nex_y ) { default: { u3l_log("xmas: bad hopcount"); return 0; @@ -937,22 +939,22 @@ _xmas_sift_poke_pact(u3_xmas_poke_pact* pac_u, c3_y* buf_y, c3_w len_w) static c3_w _xmas_sift_pact(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w) { - u3_xmas_head hed_u; - if( len_w < 8 ) { + c3_w res_w = 0; + + if ( len_w < 8 ) { u3l_log("xmas: attempted to parse overly short packet of size %u", len_w); } - _xmas_sift_head(buf_y, &hed_u); - pac_u->hed_u.typ_y = hed_u.typ_y; - c3_w res_w = 0; + _xmas_sift_head(buf_y, &pac_u->hed_u); buf_y += 8; len_w -= 8; + switch ( pac_u->hed_u.typ_y ) { case PACT_PEEK: { res_w = _xmas_sift_peek_pact(&pac_u->pek_u, buf_y, len_w); } break; case PACT_PAGE: { - res_w = _xmas_sift_page_pact(&pac_u->pag_u, &hed_u, buf_y, len_w); + res_w = _xmas_sift_page_pact(&pac_u->pag_u, pac_u->hed_u.nex_y, buf_y, len_w); } break; case PACT_POKE: { res_w = _xmas_sift_poke_pact(&pac_u->pok_u, buf_y, len_w); @@ -1005,11 +1007,14 @@ _xmas_etch_head(u3_xmas_head* hed_u, c3_y buf_y[8]) } } #endif + + assert( 1 == hed_u->pro_y ); + // c3_o req_o = c3o((hed_u->typ_y == PACT_PEEK), (hed_u->typ_y == PACT_POKE)); // c3_y siz_y = req_o ? 5 : 7; c3_w hed_w = (hed_u->nex_y & 0x3) << 2 - ^ (hed_u->pro_y & 0x7) << 4 - ^ (hed_u->typ_y & 0x3) << 7 // XX constant, 1 + ^ (hed_u->pro_y & 0x7) << 4 // XX constant, 1 + ^ (hed_u->typ_y & 0x3) << 7 ^ (hed_u->hop_y & 0x7) << 9 ^ (hed_u->mug_w & 0xFFFFFF) << 12; // XX: we don't expand hopcount if no request. Correct? @@ -1052,6 +1057,7 @@ _xmas_etch_name(c3_y* buf_y, u3_xmas_name* nam_u) met_u.rif_y = safe_dec(_xmas_met3_w(nam_u->rif_w)); if ( c3y == nam_u->nit_o ) { + assert( c3n == nam_u->aut_o ); // XX met_u.nit_y = 1; met_u.tau_y = 0; met_u.gaf_y = 0; @@ -1240,7 +1246,7 @@ _xmas_etch_pact(c3_y* buf_y, u3_xmas_pact* pac_u) } break; default: { - u3l_log("bad pact type");//u3m_bail(c3__bail); + u3l_log("bad pact type %u", pac_u->hed_u.typ_y);//u3m_bail(c3__bail); return 0; } } @@ -2729,6 +2735,7 @@ _test_sift_page() u3_xmas_pact pac_u; memset(&pac_u,0, sizeof(u3_xmas_pact)); pac_u.hed_u.typ_y = PACT_PAGE; + pac_u.hed_u.pro_y = 1; u3l_log("%%page checking sift/etch idempotent"); u3_xmas_name* nam_u = &pac_u.pag_u.nam_u; @@ -2802,7 +2809,8 @@ _test_sift_peek() { u3_xmas_pact pac_u; memset(&pac_u,0, sizeof(u3_xmas_pact)); - pac_u.hed_u.typ_y = PACT_PEEK;; + pac_u.hed_u.typ_y = PACT_PEEK; + pac_u.hed_u.pro_y = 1; u3l_log("%%peek checking sift/etch idempotent"); u3_xmas_name* nam_u = &pac_u.pek_u.nam_u; { From dde5c6e3cdb6724a1ee3a12f085275e00fab4583 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Fri, 23 Feb 2024 17:11:19 -0500 Subject: [PATCH 044/430] xmas: adds generative roundtrip testing ($poke failing, overflow) --- pkg/vere/io/xmas.c | 492 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 411 insertions(+), 81 deletions(-) diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c index 5489c9b301..4a0c536b2a 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/xmas.c @@ -417,6 +417,14 @@ _log_page_pact(u3_xmas_page_pact *pac_u) _log_data(&pac_u->dat_u); } +static void +_log_poke_pact(u3_xmas_poke_pact *pac_u) +{ + _log_name(&pac_u->nam_u); + _log_name(&pac_u->pay_u); + _log_data(&pac_u->dat_u); +} + static void _log_gage(u3_gage* gag_u) { @@ -449,13 +457,16 @@ _log_pact(u3_xmas_pact* pac_u) switch ( pac_u->hed_u.typ_y ) { case PACT_PEEK: { _log_peek_pact(&pac_u->pek_u); - break; - }; + } break; + case PACT_PAGE: { _log_page_pact(&pac_u->pag_u); - break; + } break; + + case PACT_POKE: { + _log_poke_pact(&pac_u->pok_u); + } break; - } default: { u3l_log("logging not implemented for %i", pac_u->hed_u.typ_y); break; @@ -2729,6 +2740,389 @@ _test_bitset() } } +#define cmp_scalar(nam, str, fmt) \ + if ( hav_u->nam != ned_u->nam ) { \ + fprintf(stderr, "xmas test cmp " str " differ:\r\n" \ + " have: " fmt "\r\n" \ + " need: " fmt "\r\n", \ + hav_u->nam, ned_u->nam); \ + ret_i = 1; \ + } + +#define cmp_string(nam, siz, str) \ + if ( memcmp(hav_u->nam, ned_u->nam, siz) ) { \ + fprintf(stderr, "xmas test cmp " str " differ:\r\n" \ + " have: %*.s\r\n" \ + " need: %*.s\r\n", \ + siz, hav_u->nam, siz, ned_u->nam); \ + ret_i = 1; \ + } + +#define cmp_buffer(nam, siz, str) \ + if ( memcmp(hav_u->nam, ned_u->nam, siz) ) { \ + fprintf(stderr, "xmas test cmp " str "\r\n"); \ + ret_i = 1; \ + } + +static c3_i +_test_cmp_head(u3_xmas_head* hav_u, u3_xmas_head* ned_u) +{ + c3_i ret_i = 0; + + cmp_scalar(nex_y, "head: next", "%u"); + cmp_scalar(pro_y, "head: protocol", "%u"); + cmp_scalar(typ_y, "head: type", "%u"); + cmp_scalar(hop_y, "head: hop", "%u"); + cmp_scalar(mug_w, "head: mug", "%u"); + + return ret_i; +} + +static c3_i +_test_cmp_name(u3_xmas_name* hav_u, u3_xmas_name* ned_u) +{ + c3_i ret_i = 0; + + cmp_buffer(her_d, sizeof(ned_u->her_d), "name: ships differ"); + + cmp_scalar(rif_w, "name: rifts", "%u"); + cmp_scalar(boq_y, "name: bloqs", "%u"); + cmp_scalar(nit_o, "name: inits", "%u"); + cmp_scalar(aut_o, "name: auths", "%u"); + cmp_scalar(fra_w, "name: fragments", "%u"); + cmp_scalar(pat_s, "name: path-lengths", "%u"); + + cmp_string(pat_c, ned_u->pat_s, "name: paths"); + + return ret_i; +} + +static c3_i +_test_cmp_data(u3_xmas_data* hav_u, u3_xmas_data* ned_u) +{ + c3_i ret_i = 0; + + cmp_scalar(tot_w, "data: total packets", "%u"); + + cmp_scalar(aum_u.typ_e, "data: auth-types", "%u"); + cmp_buffer(aum_u.sig_y, 64, "data: sig|hmac|null"); + + cmp_scalar(aup_u.len_y, "data: hash-lengths", "%u"); + cmp_buffer(aup_u.has_y, ned_u->aup_u.len_y, "data: hashes"); + + cmp_scalar(len_w, "data: fragments-lengths", "%u"); + cmp_buffer(fra_y, ned_u->len_w, "data: fragments"); + + return ret_i; +} + +static c3_i +_test_pact(u3_xmas_pact* pac_u) +{ + c3_y* buf_y = c3_calloc(PACT_SIZE); + c3_w len_w =_xmas_etch_pact(buf_y, pac_u); + c3_i ret_i = 0; + c3_i bot_i = 0; + c3_w sif_w; + + u3_xmas_pact nex_u; + memset(&nex_u, 0, sizeof(u3_xmas_pact)); + + if ( !len_w ) { + fprintf(stderr, "pact: etch failed\r\n"); + ret_i = 1; goto done; + } + else if ( len_w > PACT_SIZE ) { + fprintf(stderr, "pact: etch overflowed: %u\r\n", len_w); + ret_i = 1; goto done; + } + + if ( len_w != (sif_w = _xmas_sift_pact(&nex_u, buf_y, len_w)) ) { + fprintf(stderr, "pact: sift failed len=%u sif=%u\r\n", len_w, sif_w); + _log_buf(buf_y, len_w); + ret_i = 1; goto done; + } + + bot_i = 1; + + if ( _test_cmp_head(&nex_u.hed_u, &pac_u->hed_u) ) { + fprintf(stderr, "pact: head cmp fail\r\n"); + ret_i = 1; + } + + switch ( pac_u->hed_u.typ_y ) { + case PACT_PEEK: { + if ( _test_cmp_name(&nex_u.pek_u.nam_u, &pac_u->pek_u.nam_u) ) { + fprintf(stderr, "%%peek name cmp fail\r\n"); + ret_i = 1; + } + } break; + + case PACT_PAGE: { + if ( _test_cmp_name(&nex_u.pag_u.nam_u, &pac_u->pag_u.nam_u) ) { + fprintf(stderr, "%%page name cmp fail\r\n"); + ret_i = 1; + } + else if ( _test_cmp_data(&nex_u.pag_u.dat_u, &pac_u->pag_u.dat_u) ) { + fprintf(stderr, "%%page data cmp fail\r\n"); + ret_i = 1; + } + } break; + + case PACT_POKE: { + if ( _test_cmp_name(&nex_u.pok_u.nam_u, &pac_u->pok_u.nam_u) ) { + fprintf(stderr, "%%poke name cmp fail\r\n"); + ret_i = 1; + } + else if ( _test_cmp_name(&nex_u.pok_u.pay_u, &pac_u->pok_u.pay_u) ) { + fprintf(stderr, "%%poke pay-name cmp fail\r\n"); + ret_i = 1; + } + else if ( _test_cmp_data(&nex_u.pok_u.dat_u, &pac_u->pok_u.dat_u) ) { + fprintf(stderr, "%%poke data cmp fail\r\n"); + ret_i = 1; + } + } break; + + default: { + fprintf(stderr, "test: strange packet\r\n"); + ret_i = 1; + } + } + +done: + if ( ret_i ) { + _log_head(&pac_u->hed_u); + _log_pact(pac_u); + _log_buf(buf_y, len_w); + + if ( bot_i ) { + u3l_log(RED_TEXT); + _log_head(&nex_u.hed_u); + _log_pact(&nex_u); + u3l_log(DEF_TEXT); + } + } + + c3_free(buf_y); + + return ret_i; +} + +static c3_y +_test_rand_bit(void* ptr_v) +{ + return rand() & 1; +} + +static c3_y +_test_rand_bits(void* ptr_v, c3_y len_y) +{ + assert( 8 >= len_y ); + return rand() & ((1 << len_y) - 1); +} + +static c3_w +_test_rand_word(void* ptr_v) +{ + c3_w low_w = rand(); + c3_w hig_w = rand(); + return (hig_w << 16) ^ (low_w & ((1 << 16) - 1)); +} + +static c3_y +_test_rand_gulf_y(void* ptr_v, c3_y top_y) +{ + c3_y bit_y = c3_bits_word(top_y); + c3_y res_y = 0; + + if ( !bit_y ) return res_y; + + while ( 1 ) { + res_y = _test_rand_bits(ptr_v, bit_y); + + if ( res_y < top_y ) { + return res_y; + } + } +} + +static c3_w +_test_rand_gulf_w(void* ptr_v, c3_w top_w) +{ + c3_w bit_w = c3_bits_word(top_w); + c3_w res_w = 0; + + if ( !bit_w ) return res_w; + + while ( 1 ) { + res_w = _test_rand_word(ptr_v); + res_w &= (1 << bit_w) - 1; + + if ( res_w < top_w ) { + return res_w; + } + } +} + +static void +_test_rand_bytes(void* ptr_v, c3_w len_w, c3_y* buf_y) +{ + c3_w max_w = len_w / 2; + + while ( max_w-- ) { + c3_w wor_w = rand(); + *buf_y++ = (wor_w >> 0) & 0xff; + *buf_y++ = (wor_w >> 8) & 0xff; + } + + if ( len_w & 1 ) { + *buf_y = rand() & 0xff; + } +} + +const char* ta_c = "-~_.0123456789abcdefghijklmnopqrstuvwxyz"; + +static void +_test_rand_knot(void* ptr_v, c3_w len_w, c3_c* not_c) +{ + for ( c3_w i_w = 0; i_w < len_w; i_w++ ) { + *not_c++ = ta_c[_test_rand_gulf_y(ptr_v, 40)]; + } +} + +static void +_test_rand_path(void* ptr_v, c3_s len_s, c3_c* pat_c) +{ + c3_s not_s = 0; + + while ( len_s ) { + not_s = _test_rand_gulf_w(ptr_v, len_s); + _test_rand_knot(ptr_v, not_s, pat_c); + pat_c[not_s] = '/'; + pat_c += not_s + 1; + len_s -= not_s + 1; + } +} + +static void +_test_make_head(void* ptr_v, u3_xmas_head* hed_u) +{ + hed_u->nex_y = NEXH_NONE; // XX + hed_u->pro_y = 1; + hed_u->typ_y = _test_rand_gulf_y(ptr_v, 3) + 1; + hed_u->hop_y = _test_rand_gulf_y(ptr_v, 8); + hed_u->mug_w = 0; + // XX set mug_w in etch? +} + +static void +_test_make_name(void* ptr_v, u3_xmas_name* nam_u) +{ + _test_rand_bytes(ptr_v, 16, (c3_y*)nam_u->her_d); + nam_u->rif_w = _test_rand_word(ptr_v); + + nam_u->pat_s = _test_rand_gulf_w(ptr_v, 300); + nam_u->pat_c = c3_malloc(nam_u->pat_s + 1); + _test_rand_path(ptr_v, nam_u->pat_s, nam_u->pat_c); + nam_u->pat_c[nam_u->pat_s] = 0; + + nam_u->boq_y = _test_rand_bits(ptr_v, 8); + nam_u->nit_o = _test_rand_bits(ptr_v, 1); + + if ( c3y == nam_u->nit_o ) { + nam_u->aut_o = c3n; + nam_u->fra_w = 0; + } + else { + nam_u->aut_o = _test_rand_bits(ptr_v, 1); + nam_u->fra_w = _test_rand_word(ptr_v); + } +} + +static void +_test_make_data(void* ptr_v, u3_xmas_data* dat_u) +{ + dat_u->tot_w = _test_rand_word(ptr_v); + + memset(dat_u->aum_u.sig_y, 0, 64); + dat_u->aup_u.len_y = 0; + memset(dat_u->aup_u.has_y, 0, sizeof(dat_u->aup_u.has_y)); + + switch ( dat_u->aum_u.typ_e = _test_rand_bits(ptr_v, 2) ) { + case AUTH_NEXT: { + dat_u->aup_u.len_y = 2; + } break; + + case AUTH_SIGN: { + dat_u->aup_u.len_y = _test_rand_gulf_y(ptr_v, 3); + _test_rand_bytes(ptr_v, 64, dat_u->aum_u.sig_y); + } break; + + case AUTH_HMAC: { + dat_u->aup_u.len_y = _test_rand_gulf_y(ptr_v, 3); + _test_rand_bytes(ptr_v, 32, dat_u->aum_u.mac_y); + } break; + + default: break; + } + + for ( c3_w i_w = 0; i_w < dat_u->aup_u.len_y; i_w++ ) { + _test_rand_bytes(ptr_v, 32, dat_u->aup_u.has_y[i_w]); + } + + dat_u->len_w = _test_rand_gulf_w(ptr_v, 1024); + dat_u->fra_y = c3_calloc(dat_u->len_w); + _test_rand_bytes(ptr_v, dat_u->len_w, dat_u->fra_y); +} + +static void +_test_make_pact(void* ptr_v, u3_xmas_pact* pac_u) +{ + _test_make_head(ptr_v, &pac_u->hed_u); + + switch ( pac_u->hed_u.typ_y ) { + case PACT_PEEK: { + _test_make_name(ptr_v, &pac_u->pek_u.nam_u); + } break; + + case PACT_PAGE: { + _test_make_name(ptr_v, &pac_u->pag_u.nam_u); + _test_make_data(ptr_v, &pac_u->pag_u.dat_u); + } break; + + case PACT_POKE: { + _test_make_name(ptr_v, &pac_u->pok_u.nam_u); + _test_make_name(ptr_v, &pac_u->pok_u.pay_u); + _test_make_data(ptr_v, &pac_u->pok_u.dat_u); + } break; + + default: break; // XX + } +} + +static c3_i +_test_rand_pact(c3_w bat_w) +{ + u3_xmas_pact pac_u; + void* ptr_v = 0; + + fprintf(stderr, "pact: test roundtrip %u random packets\r\n", bat_w); + + for ( c3_w i_w = 0; i_w < bat_w; i_w++ ) { + _test_make_pact(ptr_v, &pac_u); + + if ( _test_pact(&pac_u) ) { + fprintf(stderr, RED_TEXT "pact: roundtrip attempt %u failed\r\n", i_w); + exit(1); + } + + // XX free pat_s / buf_y + } + + return 0; +} + static void _test_sift_page() { @@ -2756,50 +3150,10 @@ _test_sift_page() dat_u->tot_w = 1000; dat_u->len_w = 1024; dat_u->fra_y = c3_calloc(1024); + // dat_u->fra_y[1023] = 1; - c3_y* buf_y = c3_calloc(PACT_SIZE); - c3_w len_w =_xmas_etch_pact(buf_y, &pac_u); - - if ( !len_w ) { - u3l_log("%%page etch failed"); - exit(1); - } - - u3_xmas_pact nex_u; - memset(&nex_u, 0, sizeof(u3_xmas_pact)); - _xmas_sift_pact(&nex_u, buf_y, len_w); - - if ( 0 != memcmp(nex_u.pag_u.nam_u.pat_c, pac_u.pag_u.nam_u.pat_c, pac_u.pag_u.nam_u.pat_s) ) { - u3l_log(RED_TEXT); - u3l_log("path mismatch"); - u3l_log("got"); - _log_pact(&nex_u); - u3l_log("expected"); - _log_pact(&pac_u); - exit(1); - } - nex_u.pag_u.nam_u.pat_c = 0; - pac_u.pag_u.nam_u.pat_c = 0; - - if ( 0 != memcmp(nex_u.pag_u.dat_u.fra_y, pac_u.pag_u.dat_u.fra_y, pac_u.pag_u.dat_u.len_w) ) { - u3l_log(RED_TEXT); - u3l_log("mismatch 1"); - u3l_log("got len %u", nex_u.pag_u.dat_u.len_w); - _log_buf(nex_u.pag_u.dat_u.fra_y, nex_u.pag_u.dat_u.len_w); - u3l_log("expected len %u", pac_u.pag_u.dat_u.len_w); - _log_buf(pac_u.pag_u.dat_u.fra_y, pac_u.pag_u.dat_u.len_w); - exit(1); - } - - nex_u.pag_u.dat_u.fra_y = 0; - pac_u.pag_u.dat_u.fra_y = 0; - if ( 0 != memcmp(&nex_u, &pac_u, sizeof(u3_xmas_pact)) ) { - u3l_log(RED_TEXT); - u3l_log("mismatch 2"); - u3l_log("got"); - _log_pact(&nex_u); - u3l_log("expected"); - _log_pact(&pac_u); + if ( _test_pact(&pac_u) ) { + fprintf(stderr, RED_TEXT "%%page failed\r\n"); exit(1); } } @@ -2826,39 +3180,8 @@ _test_sift_peek() nam_u->nit_o = c3n; nam_u->aut_o = c3n; - c3_y* buf_y = c3_calloc(PACT_SIZE); - c3_w len_w =_xmas_etch_pact(buf_y, &pac_u); - - if ( !len_w ) { - u3l_log("%%peek etch failed"); - exit(1); - } - - u3_xmas_pact nex_u; - memset(&nex_u, 0, sizeof(u3_xmas_pact)); - _xmas_sift_pact(&nex_u, buf_y, len_w); - - if ( 0 != memcmp(nex_u.pek_u.nam_u.pat_c, pac_u.pek_u.nam_u.pat_c, pac_u.pek_u.nam_u.pat_s) ) { - u3l_log(RED_TEXT); - u3l_log("path mismatch"); - u3l_log("got"); - _log_pact(&nex_u); - u3l_log("expected"); - _log_pact(&pac_u); - exit(1); - - - } - nex_u.pek_u.nam_u.pat_c = 0; - pac_u.pek_u.nam_u.pat_c = 0; - - if ( 0 != memcmp(&nex_u, &pac_u, sizeof(u3_xmas_pact)) ) { - u3l_log(RED_TEXT); - u3l_log("mismatch"); - u3l_log("got"); - _log_pact(&nex_u); - u3l_log("expected"); - _log_pact(&pac_u); + if ( _test_pact(&pac_u) ) { + fprintf(stderr, RED_TEXT "%%page failed\r\n"); exit(1); } } @@ -2899,17 +3222,24 @@ _setup() printf("*** fail _setup 2\n"); exit(1); } + + { + c3_w pid_w = getpid(); + srand(pid_w); + fprintf(stderr, "test: seeding rand() with pid %u\r\n", pid_w); + } } int main() { - _setup(); _test_bitset(); _test_sift_peek(); _test_sift_page(); + _test_rand_pact(10); + _test_encode_path("foo/bar/baz"); _test_encode_path("publ/0/xx//1/foo/g"); return 0; From b48e08a796f27eb0bc40fb2912b6c2468e5b053d Mon Sep 17 00:00:00 2001 From: parallels Date: Mon, 26 Feb 2024 14:46:11 -0600 Subject: [PATCH 045/430] xmas: better blake3 streaming --- pkg/vere/io/blake.c | 287 +++++++++++++++++++++----------------------- pkg/vere/io/blake.h | 26 ++-- 2 files changed, 148 insertions(+), 165 deletions(-) diff --git a/pkg/vere/io/blake.c b/pkg/vere/io/blake.c index 93434b83eb..30820d5d17 100644 --- a/pkg/vere/io/blake.c +++ b/pkg/vere/io/blake.c @@ -76,8 +76,8 @@ static c3_y IV[BLAKE3_OUT_LEN] = {103, 230, 9, 106, 133, 174, 103, 187, 114, 243 #define VEC_SCALING 2 -static void -_vec_init(u3_raw_vec* vec_u, c3_w siz_w) +void +vec_init(u3_raw_vec* vec_u, c3_w siz_w) { vec_u->siz_w = siz_w; vec_u->vod_p = c3_calloc(siz_w * sizeof(void*)); @@ -96,16 +96,15 @@ _vec_free(u3_raw_vec* vec_u) c3_free(vec_u->vod_p); } -static u3_raw_vec* -_vec_make(c3_w siz_w) +u3_raw_vec* +vec_make(c3_w siz_w) { u3_raw_vec* vec_u = c3_calloc(sizeof(u3_raw_vec)); - _vec_init(vec_u, siz_w); + vec_init(vec_u, siz_w); return vec_u; } - static c3_w _vec_len(u3_raw_vec* vec_u) { @@ -143,8 +142,8 @@ _vec_resize(u3_raw_vec* vec_u, c3_w siz_w) { vec_u->siz_w = siz_w; } -static void -_vec_append(u3_raw_vec* vec_u, void* tem_p) +void +vec_append(u3_raw_vec* vec_u, void* tem_p) { if(vec_u->siz_w == vec_u->len_w ) { _vec_resize(vec_u, vec_u->siz_w * VEC_SCALING); @@ -153,6 +152,16 @@ _vec_append(u3_raw_vec* vec_u, void* tem_p) vec_u->len_w++; } +void +vec_copy(u3_raw_vec* dst_u, u3_raw_vec* src_u) { + if ( dst_u->siz_w < src_u->siz_w ) { + _vec_resize(dst_u, src_u->siz_w); + } + for ( c3_w i_w = 0; i_w < src_u->len_w; i_w++ ) { + dst_u->vod_p[i_w] = src_u->vod_p[i_w]; + } +} + static void* _vec_pop(u3_raw_vec* vec_u, c3_w idx_w) { @@ -168,8 +177,8 @@ _vec_pop(u3_raw_vec* vec_u, c3_w idx_w) return hit_p; } -static void* -_vec_popf(u3_raw_vec* vec_u) +void* +vec_popf(u3_raw_vec* vec_u) { return _vec_pop(vec_u, 0); } @@ -216,14 +225,14 @@ _log_blake_subtree(blake_subtree* sub_u) } static void -_log_verifier(blake_verifier* ver_u) +_log_bao(blake_bao* bao_u) { - fprintf(stderr, "num: %llu\r\n", ver_u->num_d); - fprintf(stderr, "sub: (%llu, %llu)\r\n", ver_u->sub_u.sin_d, ver_u->sub_u.dex_d); + fprintf(stderr, "num: %llu\r\n", bao_u->num_w); + fprintf(stderr, "sub: (%llu, %llu)\r\n", bao_u->sub_u.sin_d, bao_u->sub_u.dex_d); fprintf(stderr, "queue\r\n"); - _vec_each(&ver_u->que_u, print_proof); + _vec_each(&bao_u->que_u, print_proof); fprintf(stderr, "stack\r\n"); - _vec_each(&ver_u->sta_u, print_proof); + _vec_each(&bao_u->sta_u, print_proof); } static c3_y @@ -330,7 +339,7 @@ _compress_chunk(c3_y* dat_y, c3_w dat_w, c3_y cev_y[BLAKE3_OUT_LEN], c3_d con_d, return nod_u; } -blake_node* leaf_hash(c3_y* dat_y, c3_w dat_w, c3_d con_d) +blake_node* blake_leaf_hash(c3_y* dat_y, c3_w dat_w, c3_d con_d) { return _compress_chunk(dat_y, dat_w, IV, con_d, 0); } @@ -391,13 +400,13 @@ static void recurse_blake_subtree(blake_subtree* sub_u, blake_node* nod_u, u3_ve blake_subtree* sin_u = c3_calloc(sizeof(blake_subtree)); *sin_u = (blake_subtree){sub_u->sin_d, mid_d}; blake_node* lod_u = c3_calloc(sizeof(blake_node)); - u3_vec(pair)* lar_u = _vec_make(8); + u3_vec(pair)* lar_u = vec_make(8); recurse_blake_subtree(sin_u, lod_u, lar_u); blake_subtree* dex_u = c3_calloc(sizeof(blake_subtree)); *dex_u = (blake_subtree){mid_d, sub_u->dex_d}; blake_node* rod_u = c3_calloc(sizeof(blake_node)); - u3_vec(pair)* rar_u = _vec_make(8); + u3_vec(pair)* rar_u = vec_make(8); recurse_blake_subtree(dex_u, rod_u, rar_u); blake_pair* new_u = c3_calloc(sizeof(blake_pair)); @@ -405,13 +414,14 @@ static void recurse_blake_subtree(blake_subtree* sub_u, blake_node* nod_u, u3_ve _make_chain_value(new_u->dex_y, rod_u); memcpy(nod_u, _parent_hash(lod_u, rod_u), sizeof(blake_node)); - _vec_append(par_u, new_u); + vec_append(par_u, new_u); _vec_weld_mut(par_u, lar_u); _vec_weld_mut(par_u, rar_u); } static void -_bao_build(c3_d num_d, c3_y has_y[64], u3_vec(c3_w[8])* pof_u, u3_vec(pair)* par_u) { +_bao_build(c3_d num_d, c3_y has_y[64], u3_vec(c3_w[8])* pof_u, u3_vec(pair)* par_u) +{ blake_subtree* sub_u = c3_calloc(sizeof(blake_subtree)); *sub_u = (blake_subtree){0, num_d}; blake_node* nod_u = c3_calloc(sizeof(blake_node)); @@ -420,12 +430,12 @@ _bao_build(c3_d num_d, c3_y has_y[64], u3_vec(c3_w[8])* pof_u, u3_vec(pair)* par if ( _vec_len(par_u) != 0 ) { for ( c3_w i_w = _bitlen(num_d - 1); i_w > 1; i_w-- ) { - blake_pair* pir_u = _vec_popf(par_u); - _vec_append(pof_u, pir_u->dex_y); + blake_pair* pir_u = vec_popf(par_u); + vec_append(pof_u, pir_u->dex_y); } - blake_pair* pir_u = _vec_popf(par_u); - _vec_append(pof_u, pir_u->dex_y); - _vec_append(pof_u, pir_u->sin_y); + blake_pair* pir_u = vec_popf(par_u); + vec_append(pof_u, pir_u->dex_y); + vec_append(pof_u, pir_u->sin_y); for ( c3_w i_w = 0; i_w < (_vec_len(pof_u) >> 1); i_w++ ) { c3_w jay_w = _vec_len(pof_u) - i_w - 1; _vec_swap(pof_u, i_w, jay_w); @@ -434,101 +444,69 @@ _bao_build(c3_d num_d, c3_y has_y[64], u3_vec(c3_w[8])* pof_u, u3_vec(pair)* par } static void -_push_leaf(blake_verifier* ver_u, c3_y lef_y[BLAKE3_OUT_LEN]) +_push_leaf(blake_bao* bao_u, c3_y lef_y[BLAKE3_OUT_LEN]) { - _vec_append(&ver_u->que_u, lef_y); + vec_append(&bao_u->que_u, lef_y); } static void -_pop_leaf(c3_y out_y[BLAKE3_OUT_LEN], blake_verifier* ver_u) +_pop_leaf(c3_y out_y[BLAKE3_OUT_LEN], blake_bao* bao_u) { - c3_y* res_y = _vec_popf(&ver_u->que_u); + c3_y* res_y = vec_popf(&bao_u->que_u); memcpy(out_y, res_y, BLAKE3_OUT_LEN); } static void -_push_parent(blake_verifier* ver_u, c3_y par_y[BLAKE3_OUT_LEN]) +_push_parent(blake_bao* bao_u, c3_y par_y[BLAKE3_OUT_LEN]) { - _vec_append(&ver_u->sta_u, par_y); + vec_append(&bao_u->sta_u, par_y); } static void -_pop_parent(c3_y out_y[BLAKE3_OUT_LEN], blake_verifier* ver_u) +_pop_parent(c3_y out_y[BLAKE3_OUT_LEN], blake_bao* bao_u) { - c3_y* res_y = _vec_popl(&ver_u->sta_u); + c3_y* res_y = _vec_popl(&bao_u->sta_u); memcpy(out_y, res_y, BLAKE3_OUT_LEN); } static void -_verifier_next(blake_verifier* ver_u) +_verifier_next(blake_bao* bao_u) { - if ( _height(&ver_u->sub_u) == 0 ) { - ver_u->sub_u.sin_d += 1; - ver_u->sub_u.dex_d += (1 << _count_trail_zero(ver_u->sub_u.dex_d)); - if ( ver_u->sub_u.dex_d > ver_u->num_d ) { - ver_u->sub_u.dex_d = ver_u->num_d; + if ( _height(&bao_u->sub_u) == 0 ) { + bao_u->sub_u.sin_d += 1; + bao_u->sub_u.dex_d += (1 << _count_trail_zero(bao_u->sub_u.dex_d)); + if ( bao_u->sub_u.dex_d > bao_u->num_w ) { + bao_u->sub_u.dex_d = bao_u->num_w; } } else { - ver_u->sub_u = (blake_subtree){ver_u->sub_u.sin_d, ver_u->sub_u.sin_d + _largest_pow2(ABS_DIFF(ver_u->sub_u.dex_d, ver_u->sub_u.sin_d))}; + bao_u->sub_u = (blake_subtree){bao_u->sub_u.sin_d, bao_u->sub_u.sin_d + _largest_pow2(ABS_DIFF(bao_u->sub_u.dex_d, bao_u->sub_u.sin_d))}; } } - -static c3_o -_verifier_init(blake_verifier* ver_u, c3_y rot_y[BLAKE3_BLOCK_LEN], u3_vec(c3_y[BLAKE3_OUT_LEN])* pof_u) +static void +_push_leaves(blake_bao* bao_u, c3_y lef_y[BLAKE3_OUT_LEN], c3_y rig_y[BLAKE3_OUT_LEN]) { - if ( _vec_len(pof_u) == 0 ) { - return ver_u->num_d <= 1 ? c3y : c3n; - } - blake_node* nod_u = _parent_node(_vec_get(c3_y, pof_u, 0), _vec_get(c3_y, pof_u, 1), IV, 0); - for (c3_w i_w = 2; i_w < (_vec_len(pof_u)); i_w++ ) { - c3_y* cev_y = c3_calloc(BLAKE3_OUT_LEN); - _make_chain_value(cev_y, nod_u); - c3_y* pof_y = _vec_get(c3_y, pof_u, i_w); - nod_u = _parent_node(cev_y, pof_y, IV, 0); - } - c3_y ron_y[BLAKE3_BLOCK_LEN]; - _root_hash(ron_y, nod_u); - -// TODO: double check - if ( memcmp(ron_y, rot_y, BLAKE3_BLOCK_LEN) != 0 ) { - fprintf(stderr, "mismatch\r\n"); - _log_buf("ron_y", ron_y, BLAKE3_BLOCK_LEN); - return c3n; - } - - ver_u->sub_u = (blake_subtree){2,4}; - _vec_init(&ver_u->que_u, 8); - - _vec_append(&ver_u->que_u, _vec_popf(pof_u)); - _vec_append(&ver_u->que_u, _vec_popf(pof_u)); - - _vec_init(&ver_u->sta_u, 8); - _vec_weld_mut(&ver_u->sta_u, pof_u); - - for ( c3_w i_w = 0; i_w < (_vec_len(&ver_u->sta_u)/2); i_w++ ) { - c3_w jay_w = _vec_len(&ver_u->sta_u) - i_w - 1; - _vec_swap(&ver_u->sta_u, i_w, jay_w); - } - return c3y; + _push_leaf(bao_u, lef_y); + _push_leaf(bao_u, rig_y); + _verifier_next(bao_u); + _verifier_next(bao_u); } static c3_o -_veri_check_leaf(blake_verifier* ver_u, c3_d lef_d, c3_y* dat_y, c3_w dat_w) +_veri_check_leaf(blake_bao* bao_u, c3_y* dat_y, c3_w dat_w) { - - if ( _vec_len(&ver_u->que_u) > 0 ) { + if ( _vec_len(&bao_u->que_u) > 0 ) { c3_y out_y[BLAKE3_OUT_LEN]; - _pop_leaf(out_y, ver_u); + _pop_leaf(out_y, bao_u); c3_y cav_y[BLAKE3_OUT_LEN]; - blake_node* nod_u = leaf_hash(dat_y, dat_w, lef_d); + blake_node* nod_u = leaf_hash(dat_y, dat_w, bao_u->con_w); _make_chain_value(cav_y, nod_u); return 0 == memcmp(cav_y, out_y, BLAKE3_OUT_LEN) ? c3y : c3n; - } else if ( _vec_len(&ver_u->sta_u) > 0 ) { + } else if ( _vec_len(&bao_u->sta_u) > 0 ) { c3_y out_y[BLAKE3_OUT_LEN]; - _pop_parent(out_y, ver_u); + _pop_parent(out_y, bao_u); c3_y cav_y[BLAKE3_OUT_LEN]; - blake_node* nod_u = leaf_hash(dat_y, dat_w, lef_d); + blake_node* nod_u = leaf_hash(dat_y, dat_w, bao_u->con_w); _make_chain_value(cav_y, nod_u); return 0 == memcmp(cav_y, out_y, BLAKE3_OUT_LEN) ? c3y : c3n; } @@ -536,68 +514,73 @@ _veri_check_leaf(blake_verifier* ver_u, c3_d lef_d, c3_y* dat_y, c3_w dat_w) } static c3_o -_veri_check_pair(blake_verifier* ver_u, blake_pair* par_u) +_veri_check_pair(blake_bao* bao_u, blake_pair* par_u) { - if ( _vec_len(&ver_u->sta_u) == 0 ) { + if ( _vec_len(&bao_u->sta_u) == 0 ) { fprintf(stderr, "bailing empty stack\r\n"); return c3n; } c3_y par_y[BLAKE3_OUT_LEN]; // par_y is wrong - _pop_parent(par_y, ver_u); + _pop_parent(par_y, bao_u); blake_node* nod_u = _parent_node(par_u->sin_y, par_u->dex_y, IV, 0); c3_y cav_y[BLAKE3_OUT_LEN]; _make_chain_value(cav_y, nod_u); return _(memcmp(par_y, cav_y, BLAKE3_OUT_LEN) == 0); } -static void _veri_init(blake_verifier* ver_u, c3_d num_d) -{ - ver_u->num_d = num_d; - ver_u->sub_u = (blake_subtree){0, num_d}; -} - blake_bao* -blake_bao_make(c3_w num_w, c3_y has_y[BLAKE3_BLOCK_LEN], u3_vec(c3_y[8])* pof_u, u3_vec(blake_pair)* par_u) +blake_bao_make(c3_w num_w, u3_vec(c3_y[BLAKE3_OUT_LEN])* pof_u) { blake_bao* bao_u = c3_calloc(sizeof(blake_bao)); - // TODO semantics here - bao_u->par_u = *par_u; - _veri_init(&bao_u->ver_u, num_w); - if ( c3n == _verifier_init(&bao_u->ver_u, has_y, pof_u) ) { - return NULL; - } + bao_u->num_w = num_w; + bao_u->con_w = 0; + bao_u->sub_u = (blake_subtree){0,1}; + if ( _vec_len(pof_u) == 0 ) { + if ( num_w < 2 ) { + c3_free(bao_u); + return NULL; + } + vec_init(&bao_u->sta_u, 1); + vec_init(&bao_u->que_u, 1); + return bao_u; + } + c3_y* fst_y = vec_popf(pof_u); + c3_y* snd_y = vec_popf(pof_u); + vec_init(&bao_u->sta_u, 8); + vec_init(&bao_u->que_u, 8); + vec_copy(&bao_u->que_u, pof_u); + _push_leaves(bao_u, fst_y, snd_y); return bao_u; } +static void +_push_parents(blake_bao* bao_u, blake_pair* par_u) +{ + _push_parent(bao_u, par_u->dex_y); + _push_parent(bao_u, par_u->sin_y); +} + c3_y -bao_ingest(blake_bao* bao_u, c3_w num_w, c3_y* dat_y, c3_w dat_w) +blake_bao_verify(blake_bao* bao_u, c3_y* dat_y, c3_w dat_w, blake_pair* par_u) { - if ( bao_u->con_w != num_w ) { - return BAO_BAD_ORDER; + if ( c3n == _veri_check_leaf(bao_u, dat_y, dat_w) ) { + return BAO_FAILED; + } + if ( par_u == NULL ) { + return BAO_GOOD; } - if ( c3n == _veri_check_leaf(&bao_u->ver_u, num_w, dat_y, dat_w) ) { + if ( c3n == _veri_check_pair(bao_u, par_u) ) { return BAO_FAILED; } - if ( num_w < _vec_len(&bao_u->par_u) ) { - blake_pair* pir_u = _vec_get(blake_pair, &bao_u->par_u, num_w); - if ( _veri_check_pair(&bao_u->ver_u, pir_u) == c3n ) { - return BAO_FAILED; - } - _verifier_next(&bao_u->ver_u); - if ( _height(&bao_u->ver_u.sub_u) == 0 ) { - _push_leaf(&bao_u->ver_u, pir_u->sin_y); - _push_leaf(&bao_u->ver_u, pir_u->dex_y); - _verifier_next(&bao_u->ver_u); - _verifier_next(&bao_u->ver_u); - } else { - _push_parent(&bao_u->ver_u, pir_u->dex_y); - _push_parent(&bao_u->ver_u, pir_u->sin_y); - } + if ( _height(&bao_u->sub_u) == 0 ) { + _push_leaves(bao_u, par_u->sin_y, par_u->dex_y); + } else { + _push_parents(bao_u, par_u); } bao_u->con_w++; - if ( bao_u->con_w == bao_u->ver_u.num_d ) { + if ( bao_u->con_w == bao_u->num_w ) { return BAO_DONE; } return BAO_GOOD; @@ -646,14 +629,14 @@ static void _test_bao() c3_y* dat_y = c3_calloc(dat_w); for ( c3_w num_w = 1; num_w <= 100; num_w++ ) { c3_y has_y[BLAKE3_BLOCK_LEN]; - u3_vec(c3_y[8])* pof_u = _vec_make(10); - u3_vec(blake_pair)* par_u = _vec_make(10); + u3_vec(c3_y[8])* pof_u = vec_make(10); + u3_vec(blake_pair)* par_u = vec_make(10); _bao_build(num_w, has_y, pof_u, par_u); - blake_verifier ver_u; - memset(&ver_u, 0, sizeof(blake_verifier)); - _veri_init(&ver_u, num_w); - if ( c3n == _verifier_init(&ver_u, has_y, pof_u) ) { + blake_bao bao_u; + memset(&bao_u, 0, sizeof(blake_bao)); + _veri_init(&bao_u, num_w); + if ( c3n == _verifier_init(&bao_u, has_y, pof_u) ) { fprintf(stderr, "Failed on %u\r\n", num_w); exit(1); } @@ -671,28 +654,28 @@ static void _test_bao() continue; } for ( c3_w i_w = 0; i_w < num_w; i_w++ ) { - if ( c3n == _veri_check_leaf(&ver_u, i_w, dat_y, dat_w) ) { + if ( c3n == _veri_check_leaf(&bao_u, i_w, dat_y, dat_w) ) { fprintf(stderr, "Check leaf %u failed for %u\r\n", i_w, num_w); exit(1); } // pl - // _log_verifier(&ver_u); + // _log_verifier(&bao_u); if ( i_w < _vec_len(par_u) ) { blake_pair* pir_u = _vec_get(blake_pair, par_u, i_w); - if ( _veri_check_pair(&ver_u, pir_u) == c3n ) { + if ( _veri_check_pair(&bao_u, pir_u) == c3n ) { fprintf(stderr, "check pair failed (%u, %u)", i_w, num_w); exit(1); } - _verifier_next(&ver_u); - if ( _height(&ver_u.sub_u) == 0 ) { - _push_leaf(&ver_u, pir_u->sin_y); - _push_leaf(&ver_u, pir_u->dex_y); - _verifier_next(&ver_u); - _verifier_next(&ver_u); + _verifier_next(&bao_u); + if ( _height(&bao_u.sub_u) == 0 ) { + _push_leaf(&bao_u, pir_u->sin_y); + _push_leaf(&bao_u, pir_u->dex_y); + _verifier_next(&bao_u); + _verifier_next(&bao_u); } else { - _push_parent(&ver_u, pir_u->dex_y); - _push_parent(&ver_u, pir_u->sin_y); + _push_parent(&bao_u, pir_u->dex_y); + _push_parent(&bao_u, pir_u->sin_y); } } } @@ -706,19 +689,19 @@ static void print_int(void* vod_p, c3_w i_w) { static void _test_vec() { fprintf(stderr, "Making vec"); - u3_vec(c3_w)* vec_u = _vec_make(2); + u3_vec(c3_w)* vec_u = vec_make(2); c3_w fst = 1; c3_w snd = 2; c3_w thd = 3; - _vec_append(vec_u, &fst); + vec_append(vec_u, &fst); fprintf(stderr, "put one\r\n"); - _vec_append(vec_u, &snd); + vec_append(vec_u, &snd); fprintf(stderr, "put two\r\n"); - _vec_append(vec_u, &thd); + vec_append(vec_u, &thd); fprintf(stderr, "put three\r\n"); - c3_w* one_w = _vec_popf(vec_u); + c3_w* one_w = (vec_u); if ( *one_w != 1 ) { fprintf(stderr, "(one) Vec failure\r\n"); @@ -726,7 +709,7 @@ static void _test_vec() { } fprintf(stderr, "popped %u", *one_w); - c3_w* two_w = _vec_popf(vec_u); + c3_w* two_w = (vec_u); fprintf(stderr, "two pointer %p, %p\r\n", &snd, two_w); if ( *two_w != 2 ) { fprintf(stderr, "(two) Vec failure\r\n"); @@ -734,7 +717,7 @@ static void _test_vec() { } fprintf(stderr, "popped %u", *two_w); - c3_w* thr_w = _vec_popf(vec_u); + c3_w* thr_w = (vec_u); if ( *thr_w != 3 ) { fprintf(stderr, "(three) Vec failure\r\n"); exit(1); @@ -747,16 +730,16 @@ static void _test_vec() { exit(1); } - _vec_append(vec_u, &fst); - _vec_append(vec_u, &snd); - _vec_append(vec_u, &thd); + vec_append(vec_u, &fst); + vec_append(vec_u, &snd); + vec_append(vec_u, &thd); c3_w for_w = 4; c3_w fiv_w = 5; c3_w six_w = 6; - u3_vec(c3_w)* new_u = _vec_make(4); - _vec_append(new_u, &for_w); - _vec_append(new_u, &fiv_w); - _vec_append(new_u, &six_w); + u3_vec(c3_w)* new_u = vec_make(4); + vec_append(new_u, &for_w); + vec_append(new_u, &fiv_w); + vec_append(new_u, &six_w); _vec_weld_mut(vec_u, new_u); _vec_each(vec_u, print_int); diff --git a/pkg/vere/io/blake.h b/pkg/vere/io/blake.h index 32d8953100..18dd174bf9 100644 --- a/pkg/vere/io/blake.h +++ b/pkg/vere/io/blake.h @@ -10,6 +10,8 @@ #define BLAKE3_BLOCK_LEN 64 #define BLAKE3_OUT_LEN 32 +#define u3_vec(type) u3_raw_vec + enum bao_ingest_result { BAO_BAD_ORDER = 1, BAO_FAILED = 2, @@ -17,8 +19,6 @@ enum bao_ingest_result { BAO_DONE = 4, }; - - typedef struct _blake_pair { c3_y sin_y[BLAKE3_OUT_LEN]; c3_y dex_y[BLAKE3_OUT_LEN]; @@ -30,7 +30,6 @@ typedef struct _u3_raw_vec { void** vod_p; } u3_raw_vec; -#define u3_vec(type) u3_raw_vec typedef struct _blake_node { @@ -46,25 +45,26 @@ typedef struct _blake_subtree { c3_d dex_d; } blake_subtree; -typedef struct _blake_verifier { - c3_d num_d; +typedef struct _blake_bao { + c3_w num_w; c3_w con_w; blake_subtree sub_u; u3_vec(c3_y[BLAKE3_OUT_LEN]) que_u; u3_vec(c3_w[BLAKE3_OUT_LEN]) sta_u; -} blake_verifier; - -typedef struct _blake_bao { - c3_w con_w; - u3_vec(blake_pair) par_u; - blake_verifier ver_u; } blake_bao; +void* vec_popf(u3_raw_vec*); + +void vec_init(u3_raw_vec*, c3_w); + +void vec_append(u3_raw_vec*, void*); + +u3_raw_vec* vec_make(c3_w); blake_node* blake_leaf_hash(c3_y* dat_y, c3_w dat_w, c3_d con_d); -blake_bao* blake_bao_make(c3_w, c3_y[BLAKE3_BLOCK_LEN], u3_vec(c3_y[8])*, u3_vec(blake_pair)*); +blake_bao* blake_bao_make(c3_w, u3_raw_vec* pof_u); -c3_y bao_ingest(blake_bao*, c3_w, c3_y*, c3_w); +c3_y blake_bao_verify(blake_bao*, c3_y*, c3_w, blake_pair* par_u); #endif \ No newline at end of file From bd1d6caf5666b33323d58b55ff762a2cfba45fc8 Mon Sep 17 00:00:00 2001 From: parallels Date: Mon, 26 Feb 2024 14:46:27 -0600 Subject: [PATCH 046/430] xmas: stub blake3 lss support --- pkg/vere/io/xmas.c | 56 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 47 insertions(+), 9 deletions(-) diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c index ef030d0584..8f15ba9910 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/xmas.c @@ -16,11 +16,12 @@ #include #include #include +#include "blake.h" c3_o dop_o = c3n; //#define XMAS_DEBUG c3y -#define XMAS_TEST +//#define XMAS_TEST #define XMAS_VER 1 #define FINE_PAGE 4096 // packets per page #define FINE_FRAG 1024 // bytes per fragment packet @@ -82,7 +83,10 @@ typedef struct _u3_peer_last { c3_d son_d; // time of sponsor check } u3_peer_last; - +typedef struct _u3_buf { + c3_y* fra_y; + c3_w fra_w; +} u3_buf; typedef struct _u3_pend_req { c3_w nex_w; @@ -98,6 +102,8 @@ typedef struct _u3_pend_req { c3_w ack_w; // highest acked fragment number u3_lane lan_u; // last lane heard u3_gage* gag_u; + u3_vec(u3_buf) mis_u; // misordered blake hash + blake_bao* bao_u; // blake verifier } u3_pend_req; typedef struct _u3_czar_info { @@ -185,8 +191,8 @@ typedef struct _u3_xmas_data_meta { typedef enum { AUTH_NONE = 0, - AUTH_NEXT = 1, - AUTH_SIGN = 2, + AUTH_NEXT = 1, // %1, must be two hash + AUTH_SIGN = 2, // %0, hashes are optional depending on num frag AUTH_HMAC = 3 } u3_xmas_auth_type; @@ -1535,6 +1541,7 @@ _xmas_packet_timeout(uv_timer_t* tim_u) { u3_pend_req* req_u = (u3_pend_req*)tim_u->data; u3l_log("%u packet timed out", req_u->old_w); } + static void _xmas_handle_ack(u3_gage* gag_u, u3_pact_stat* pat_u) { @@ -1628,6 +1635,7 @@ _xmas_req_pact_sent(u3_xmas* sam_u, u3_xmas_name* nam_u) // instantiate pending request // stack allocating is ok, because _xmas_put_pend will copy it out anyway req_u = alloca(sizeof(u3_pend_req)); + memset(req_u, 0, sizeof(u3_pend_req)); req_u->pac_u = c3_calloc(sizeof(u3_xmas_pact)); req_u->pac_u->sam_u = sam_u; req_u->pac_u->hed_u.typ_y = PACT_PEEK; @@ -1890,6 +1898,35 @@ _try_resend(u3_pend_req* req_u) } } +static u3_vec(c3_y[BLAKE3_OUT_LEN])* +_xmas_get_proof_vec(u3_xmas_data* dat_u) +{ + u3_assert( dat_u->aum_u.typ_e == AUTH_SIGN ); + u3_assert( dat_u->aup_u.len_y != 0 ); + u3_vec(c3_y[BLAKE3_OUT_LEN])* pof_u = vec_make(4); + { + c3_y* lef_y = c3_calloc(BLAKE3_OUT_LEN); + blake_node* nod_u = blake_leaf_hash(dat_u->fra_y, dat_u->len_w, 0); + memcpy(lef_y, nod_u->cev_y, BLAKE3_OUT_LEN); + c3_free(nod_u); + vec_append(pof_u, lef_y); + } + + for ( c3_w i_w = 0; i_w < dat_u->aup_u.len_y; i_w++ ) { + c3_y* has_y = c3_calloc(BLAKE3_OUT_LEN); + memcpy(has_y, dat_u->aup_u.has_y[i_w], BLAKE3_OUT_LEN); + vec_append(pof_u, has_y); + } + return pof_u; +} + +static c3_y +_xmas_verify_data(u3_xmas_data* dat_u, blake_bao* bao_u) +{ + blake_pair* par_u = NULL; + return blake_bao_verify(bao_u, dat_u->fra_y, dat_u->len_w, par_u); +} + /* _xmas_req_pact_done(): mark packet as done, possibly producing the result */ static u3_weak @@ -1924,12 +1961,17 @@ _xmas_req_pact_done(u3_xmas* sam_u, u3_xmas_name *nam_u, u3_xmas_data* dat_u, u3 c3_w siz_w = (1 << (nam_u->boq_y - 3)); // First packet received, instantiate request fully if ( req_u->tot_w == 0 ) { - u3_assert( siz_w == 1024); // boq_y == 13 + u3_assert( siz_w == 1024 ); // boq_y == 13 req_u->dat_y = c3_calloc(siz_w * dat_u->tot_w); req_u->wat_u = c3_calloc(sizeof(u3_pact_stat) * dat_u->tot_w); req_u->tot_w = dat_u->tot_w; _init_bitset(&req_u->was_u, dat_u->tot_w + 1); req_u->lef_w = 2; + u3_vec(c3_y[BLAKE3_OUT_LEN])* pof_u = _xmas_get_proof_vec(dat_u); + req_u->bao_u = blake_bao_make(req_u->tot_w, pof_u); + } else { + //blake_bao_verify(req_u->bao_u); + } if ( dat_u->tot_w <= nam_u->fra_w ) { @@ -2536,7 +2578,6 @@ _hear_peer(u3_xmas* sam_u, u3_peer* per_u, u3_lane lan_u, c3_o dir_o) static void _xmas_forward(u3_xmas_pact* pac_u, u3_lane lan_u) { - u3_xmas* sam_u = pac_u->sam_u; c3_d her_d[2]; _get_her(pac_u, her_d); @@ -2568,9 +2609,6 @@ _xmas_forward(u3_xmas_pact* pac_u, u3_lane lan_u) } } } - - - _xmas_pact_free(pac_u); } From c787f9f94cbfa8460d7ba921ac9eefc56a3a448f Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Mon, 26 Feb 2024 15:07:36 -0500 Subject: [PATCH 047/430] xmas: enforce lower path length limits in tests, increase samples --- pkg/vere/io/xmas.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c index 8f15ba9910..ae177572b7 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/xmas.c @@ -3373,12 +3373,12 @@ _test_make_head(void* ptr_v, u3_xmas_head* hed_u) } static void -_test_make_name(void* ptr_v, u3_xmas_name* nam_u) +_test_make_name(void* ptr_v, c3_s pat_s, u3_xmas_name* nam_u) { _test_rand_bytes(ptr_v, 16, (c3_y*)nam_u->her_d); nam_u->rif_w = _test_rand_word(ptr_v); - nam_u->pat_s = _test_rand_gulf_w(ptr_v, 300); + nam_u->pat_s = _test_rand_gulf_w(ptr_v, pat_s); nam_u->pat_c = c3_malloc(nam_u->pat_s + 1); _test_rand_path(ptr_v, nam_u->pat_s, nam_u->pat_c); nam_u->pat_c[nam_u->pat_s] = 0; @@ -3439,17 +3439,17 @@ _test_make_pact(void* ptr_v, u3_xmas_pact* pac_u) switch ( pac_u->hed_u.typ_y ) { case PACT_PEEK: { - _test_make_name(ptr_v, &pac_u->pek_u.nam_u); + _test_make_name(ptr_v, 277, &pac_u->pek_u.nam_u); } break; case PACT_PAGE: { - _test_make_name(ptr_v, &pac_u->pag_u.nam_u); + _test_make_name(ptr_v, 277, &pac_u->pag_u.nam_u); _test_make_data(ptr_v, &pac_u->pag_u.dat_u); } break; case PACT_POKE: { - _test_make_name(ptr_v, &pac_u->pok_u.nam_u); - _test_make_name(ptr_v, &pac_u->pok_u.pay_u); + _test_make_name(ptr_v, 124, &pac_u->pok_u.nam_u); + _test_make_name(ptr_v, 124, &pac_u->pok_u.pay_u); _test_make_data(ptr_v, &pac_u->pok_u.dat_u); } break; @@ -3594,7 +3594,7 @@ int main() _test_sift_peek(); _test_sift_page(); - _test_rand_pact(10); + _test_rand_pact(100000); _test_encode_path("foo/bar/baz"); _test_encode_path("publ/0/xx//1/foo/g"); From f7fa447cfa4b280e7d25903dce86b80c2875e2e0 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Mon, 26 Feb 2024 15:30:34 -0500 Subject: [PATCH 048/430] xmas: adds packet-measurement before etch_*() --- pkg/vere/io/xmas.c | 102 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c index ae177572b7..365394f2ae 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/xmas.c @@ -1294,6 +1294,8 @@ _xmas_etch_page_pact(c3_y* buf_y, u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u) } cur_w += nex_w; + // XX hops + return cur_w; } @@ -1320,9 +1322,107 @@ _xmas_etch_poke_pact(c3_y* buf_y, u3_xmas_poke_pact* pac_u, u3_xmas_head* hed_u) return cur_w; } +static c3_w +_xmas_size_name(u3_xmas_name* nam_u) +{ + c3_w siz_w = 1; + u3_xmas_name_meta met_u; + + met_u.ran_y = _xmas_rank(nam_u->her_d); + met_u.rif_y = safe_dec(_xmas_met3_w(nam_u->rif_w)); + + siz_w += 2 << met_u.ran_y; + siz_w += met_u.rif_y + 1; + siz_w++; // bloq + + if (c3n == nam_u->nit_o ) { + met_u.gaf_y = safe_dec(_xmas_met3_w(nam_u->fra_w)); + siz_w += met_u.gaf_y + 1; + } + + siz_w += 2; // path-length + siz_w += nam_u->pat_s; + + return siz_w; +} + +static c3_w +_xmas_size_data(u3_xmas_data* dat_u) +{ + c3_w siz_w = 1; + u3_xmas_data_meta met_u; + + met_u.bot_y = safe_dec(_xmas_met3_w(dat_u->tot_w)); + + siz_w += met_u.bot_y + 1; + + switch ( dat_u->aum_u.typ_e ) { + case AUTH_SIGN: { + siz_w += 64; + } break; + + case AUTH_HMAC: { + siz_w += 32; + } break; + + default: break; + } + + siz_w += 32 * dat_u->aup_u.len_y; + + c3_y nel_y = _xmas_met3_w(dat_u->len_w); + met_u.men_y = (3 >= nel_y) ? nel_y : 3; + + if ( 3 == met_u.men_y ) { + siz_w++; + } + + siz_w += nel_y; + siz_w += dat_u->len_w; + + return siz_w; +} + +static c3_w +_xmas_size_pact(u3_xmas_pact* pac_u) +{ + c3_w siz_w = 8; // header + cookie; + + switch ( pac_u->hed_u.typ_y ) { + case PACT_PEEK: { + siz_w += _xmas_size_name(&pac_u->pek_u.nam_u); + } break; + + case PACT_PAGE: { + siz_w += _xmas_size_name(&pac_u->pag_u.nam_u); + siz_w += _xmas_size_data(&pac_u->pag_u.dat_u); + // XX hops + } break; + + case PACT_POKE: { + siz_w += _xmas_size_name(&pac_u->pok_u.nam_u); + siz_w += _xmas_size_name(&pac_u->pok_u.pay_u); + siz_w += _xmas_size_data(&pac_u->pok_u.dat_u); + } break; + + default: { + u3l_log("bad pact type %u", pac_u->hed_u.typ_y);//u3m_bail(c3__bail); + return 0; + } + } + + return siz_w; +} + static c3_w _xmas_etch_pact(c3_y* buf_y, u3_xmas_pact* pac_u) { + c3_w siz_w = _xmas_size_pact(pac_u); + if ( siz_w > PACT_SIZE ) { + fprintf(stderr, "etch: would overflow %u\r\n", siz_w); + return 0; + } + c3_w cur_w = 0, nex_w; u3_xmas_head* hed_u = &pac_u->hed_u; _xmas_etch_head(hed_u, buf_y + cur_w); @@ -1355,6 +1455,8 @@ _xmas_etch_pact(c3_y* buf_y, u3_xmas_pact* pac_u) cur_w += nex_w; + assert( siz_w == cur_w ); + return cur_w; } From eb0c72686f4da835716564a79f382e91f7fdfd8d Mon Sep 17 00:00:00 2001 From: parallels Date: Mon, 26 Feb 2024 14:48:37 -0600 Subject: [PATCH 049/430] xmas: fix naming --- pkg/vere/io/blake.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/vere/io/blake.c b/pkg/vere/io/blake.c index 30820d5d17..5bcc4b37de 100644 --- a/pkg/vere/io/blake.c +++ b/pkg/vere/io/blake.c @@ -390,7 +390,7 @@ static void recurse_blake_subtree(blake_subtree* sub_u, blake_node* nod_u, u3_ve { c3_d hit_d = _height(sub_u); if ( hit_d == 0 ) { - blake_node* new_u = leaf_hash(emp_y, emp_w, sub_u->sin_d); + blake_node* new_u = blake_leaf_hash(emp_y, emp_w, sub_u->sin_d); memcpy(nod_u, new_u, sizeof(blake_node)); return; } @@ -499,14 +499,14 @@ _veri_check_leaf(blake_bao* bao_u, c3_y* dat_y, c3_w dat_w) c3_y out_y[BLAKE3_OUT_LEN]; _pop_leaf(out_y, bao_u); c3_y cav_y[BLAKE3_OUT_LEN]; - blake_node* nod_u = leaf_hash(dat_y, dat_w, bao_u->con_w); + blake_node* nod_u = blake_leaf_hash(dat_y, dat_w, bao_u->con_w); _make_chain_value(cav_y, nod_u); return 0 == memcmp(cav_y, out_y, BLAKE3_OUT_LEN) ? c3y : c3n; } else if ( _vec_len(&bao_u->sta_u) > 0 ) { c3_y out_y[BLAKE3_OUT_LEN]; _pop_parent(out_y, bao_u); c3_y cav_y[BLAKE3_OUT_LEN]; - blake_node* nod_u = leaf_hash(dat_y, dat_w, bao_u->con_w); + blake_node* nod_u = blake_leaf_hash(dat_y, dat_w, bao_u->con_w); _make_chain_value(cav_y, nod_u); return 0 == memcmp(cav_y, out_y, BLAKE3_OUT_LEN) ? c3y : c3n; } @@ -643,7 +643,7 @@ static void _test_bao() if ( num_w == 1 ) { c3_y out_y[BLAKE3_BLOCK_LEN]; - blake_node* nod_u = leaf_hash(dat_y, dat_w, 0); + blake_node* nod_u = blake_leaf_hash(dat_y, dat_w, 0); _root_hash(out_y, nod_u); if ( memcmp(out_y, has_y, BLAKE3_BLOCK_LEN) != 0 ) { _log_buf("expected", has_y, BLAKE3_BLOCK_LEN); From e5690487f2ae0f02648083acb14ff4e98cc0fd87 Mon Sep 17 00:00:00 2001 From: Sigilante Date: Wed, 28 Feb 2024 13:25:07 -0600 Subject: [PATCH 050/430] WIP bazel --- WORKSPACE.bazel | 12 +++++- bazel/third_party/softblas/BUILD.bazel | 0 bazel/third_party/softblas/softblas.BUILD | 52 +++++++++++++++++++++++ pkg/noun/BUILD.bazel | 1 + pkg/noun/jets/q.h | 2 + pkg/noun/jets/tree.c | 7 +++ pkg/noun/jets/w.h | 2 + 7 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 bazel/third_party/softblas/BUILD.bazel create mode 100644 bazel/third_party/softblas/softblas.BUILD diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index d3c4848fd1..5553f2357c 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -291,6 +291,14 @@ versioned_http_archive( version = "2.14", ) +versioned_http_archive( + name = "softblas", + build_file = "//bazel/third_party/softblas:softblas.BUILD", + # sha256 = "", + url = "https://github.com/urbit/SoftBLAS/archive/{version}.tar.gz", + version = "bd637fdb23ac4ebd4048eb546633262ddf647b18", +) + versioned_http_archive( name = "softfloat", build_file = "//bazel/third_party/softfloat:softfloat.BUILD", @@ -354,10 +362,10 @@ versioned_http_archive( versioned_http_archive( name = "zlib", build_file = "//bazel/third_party/zlib:zlib.BUILD", - sha256 = "ff0ba4c292013dbc27530b3a81e1f9a813cd39de01ca5e0f8bf355702efa593e", + sha256 = "9a93b2b7dfdac77ceba5a558a580e74667dd6fede4585b91eefb60f03b72df23", strip_prefix = "zlib-{version}", url = "https://www.zlib.net/zlib-{version}.tar.gz", - version = "1.3", + version = "1.3.1", ) # diff --git a/bazel/third_party/softblas/BUILD.bazel b/bazel/third_party/softblas/BUILD.bazel new file mode 100644 index 0000000000..e69de29bb2 diff --git a/bazel/third_party/softblas/softblas.BUILD b/bazel/third_party/softblas/softblas.BUILD new file mode 100644 index 0000000000..8d669a7f77 --- /dev/null +++ b/bazel/third_party/softblas/softblas.BUILD @@ -0,0 +1,52 @@ +# FILEPATH: /home/neal/lagoon/vere/bazel/third_party/softblas/softblas.BUILD + +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") + +cc_library( + name = "softblas", + visibility = ["//visibility:public"], + hdrs = ["include/softblas.h"], + # includes = ["include"], + srcs = ["src/blas/level1/sasum.c", + "src/blas/level1/dasum.c", + "src/blas/level1/hasum.c", + "src/blas/level1/qasum.c", + "src/blas/level1/saxpy.c", + "src/blas/level1/daxpy.c", + "src/blas/level1/haxpy.c", + "src/blas/level1/qaxpy.c", + "src/blas/level1/scopy.c", + "src/blas/level1/dcopy.c", + "src/blas/level1/hcopy.c", + "src/blas/level1/qcopy.c", + "src/blas/level1/sdot.c", + "src/blas/level1/ddot.c", + "src/blas/level1/hdot.c", + "src/blas/level1/qdot.c", + "src/blas/level1/snrm2.c", + "src/blas/level1/dnrm2.c", + "src/blas/level1/hnrm2.c", + "src/blas/level1/qnrm2.c", + "src/blas/level1/sscal.c", + "src/blas/level1/dscal.c", + "src/blas/level1/hscal.c", + "src/blas/level1/qscal.c", + "src/blas/level1/sswap.c", + "src/blas/level1/dswap.c", + "src/blas/level1/hswap.c", + "src/blas/level1/qswap.c", + "src/blas/level1/isamax.c", + "src/blas/level1/idamax.c", + "src/blas/level1/ihamax.c", + "src/blas/level1/iqamax.c", + "src/blas/level2/sgemv.c", + "src/blas/level2/dgemv.c", + "src/blas/level2/hgemv.c", + "src/blas/level2/qgemv.c", + "src/blas/level3/sgemm.c", + "src/blas/level3/dgemm.c", + "src/blas/level3/hgemm.c", + "src/blas/level3/qgemm.c" + ], + deps = ["@softfloat"], +) diff --git a/pkg/noun/BUILD.bazel b/pkg/noun/BUILD.bazel index a6b8de6d7a..1e16d21573 100644 --- a/pkg/noun/BUILD.bazel +++ b/pkg/noun/BUILD.bazel @@ -38,6 +38,7 @@ vere_library( "@openssl", "@pdjson", "@sigsegv", + "@softblas", "@softfloat", "@urcrypt", ] + select({ diff --git a/pkg/noun/jets/q.h b/pkg/noun/jets/q.h index 33c63ef42b..8b99103e56 100644 --- a/pkg/noun/jets/q.h +++ b/pkg/noun/jets/q.h @@ -247,6 +247,8 @@ u3_noun u3qfp_nepo(u3_noun, u3_noun); u3_noun u3qfp_rake(u3_noun); + u3_noun u3qf_la_add_real(u3_noun, u3_noun, u3_noun); + # define u3qfu_van_fan 28 # define u3qfu_van_rib 58 # define u3qfu_van_vet 59 diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index b0264327d1..0499a5202d 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -2147,6 +2147,11 @@ static u3j_core _139_hex_json_d[] = {} }; +/* linear algebra jets +*/ + +static u3j_harm _139_hex_lagoon_add_a[] = {{".2", u3wf_la_add}, {}}; + static u3j_core _139_hex_d[] = { { "lore", 63, _140_hex_lore_a, 0, no_hashes }, { "leer", 63, _140_hex_leer_a, 0, no_hashes }, @@ -2165,6 +2170,8 @@ static u3j_core _139_hex_d[] = { "secp", 6, 0, _140_hex_secp_d, no_hashes }, { "mimes", 31, 0, _140_hex_mimes_d, no_hashes }, { "json", 31, 0, _139_hex_json_d, no_hashes }, + + { "add", 7, _139_hex_lagoon_add_a, 0, no_hashes }, {} }; diff --git a/pkg/noun/jets/w.h b/pkg/noun/jets/w.h index d838416c03..5cc40f1975 100644 --- a/pkg/noun/jets/w.h +++ b/pkg/noun/jets/w.h @@ -331,4 +331,6 @@ u3_noun u3wfu_repo(u3_noun); u3_noun u3wfu_rest(u3_noun); + u3_noun u3wf_la_add(u3_noun); + #endif /* ifndef U3_JETS_W_H */ From 3cf0ad60ffb2a15f965e7819deaacb580447076b Mon Sep 17 00:00:00 2001 From: Matthew LeVan Date: Wed, 28 Feb 2024 14:37:22 -0500 Subject: [PATCH 051/430] WIP bazel builds but `qgemm.c` needs `#include ` --- WORKSPACE.bazel | 1 + bazel/third_party/softblas/softblas.BUILD | 84 ++++++++++++----------- 2 files changed, 44 insertions(+), 41 deletions(-) diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index 5553f2357c..ddb83b8361 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -294,6 +294,7 @@ versioned_http_archive( versioned_http_archive( name = "softblas", build_file = "//bazel/third_party/softblas:softblas.BUILD", + strip_prefix = "SoftBLAS-{version}", # sha256 = "", url = "https://github.com/urbit/SoftBLAS/archive/{version}.tar.gz", version = "bd637fdb23ac4ebd4048eb546633262ddf647b18", diff --git a/bazel/third_party/softblas/softblas.BUILD b/bazel/third_party/softblas/softblas.BUILD index 8d669a7f77..2ca6b46b1a 100644 --- a/bazel/third_party/softblas/softblas.BUILD +++ b/bazel/third_party/softblas/softblas.BUILD @@ -6,47 +6,49 @@ cc_library( name = "softblas", visibility = ["//visibility:public"], hdrs = ["include/softblas.h"], - # includes = ["include"], - srcs = ["src/blas/level1/sasum.c", - "src/blas/level1/dasum.c", - "src/blas/level1/hasum.c", - "src/blas/level1/qasum.c", - "src/blas/level1/saxpy.c", - "src/blas/level1/daxpy.c", - "src/blas/level1/haxpy.c", - "src/blas/level1/qaxpy.c", - "src/blas/level1/scopy.c", - "src/blas/level1/dcopy.c", - "src/blas/level1/hcopy.c", - "src/blas/level1/qcopy.c", - "src/blas/level1/sdot.c", - "src/blas/level1/ddot.c", - "src/blas/level1/hdot.c", - "src/blas/level1/qdot.c", - "src/blas/level1/snrm2.c", - "src/blas/level1/dnrm2.c", - "src/blas/level1/hnrm2.c", - "src/blas/level1/qnrm2.c", - "src/blas/level1/sscal.c", - "src/blas/level1/dscal.c", - "src/blas/level1/hscal.c", - "src/blas/level1/qscal.c", - "src/blas/level1/sswap.c", - "src/blas/level1/dswap.c", - "src/blas/level1/hswap.c", - "src/blas/level1/qswap.c", - "src/blas/level1/isamax.c", - "src/blas/level1/idamax.c", - "src/blas/level1/ihamax.c", - "src/blas/level1/iqamax.c", - "src/blas/level2/sgemv.c", - "src/blas/level2/dgemv.c", - "src/blas/level2/hgemv.c", - "src/blas/level2/qgemv.c", - "src/blas/level3/sgemm.c", - "src/blas/level3/dgemm.c", - "src/blas/level3/hgemm.c", - "src/blas/level3/qgemm.c" + includes = ["include"], + srcs = [ + "include/softblas.h", + "src/blas/level1/sasum.c", + "src/blas/level1/dasum.c", + "src/blas/level1/hasum.c", + "src/blas/level1/qasum.c", + "src/blas/level1/saxpy.c", + "src/blas/level1/daxpy.c", + "src/blas/level1/haxpy.c", + "src/blas/level1/qaxpy.c", + "src/blas/level1/scopy.c", + "src/blas/level1/dcopy.c", + "src/blas/level1/hcopy.c", + "src/blas/level1/qcopy.c", + "src/blas/level1/sdot.c", + "src/blas/level1/ddot.c", + "src/blas/level1/hdot.c", + "src/blas/level1/qdot.c", + "src/blas/level1/snrm2.c", + "src/blas/level1/dnrm2.c", + "src/blas/level1/hnrm2.c", + "src/blas/level1/qnrm2.c", + "src/blas/level1/sscal.c", + "src/blas/level1/dscal.c", + "src/blas/level1/hscal.c", + "src/blas/level1/qscal.c", + "src/blas/level1/sswap.c", + "src/blas/level1/dswap.c", + "src/blas/level1/hswap.c", + "src/blas/level1/qswap.c", + "src/blas/level1/isamax.c", + "src/blas/level1/idamax.c", + "src/blas/level1/ihamax.c", + "src/blas/level1/iqamax.c", + "src/blas/level2/sgemv.c", + "src/blas/level2/dgemv.c", + "src/blas/level2/hgemv.c", + "src/blas/level2/qgemv.c", + "src/blas/level3/sgemm.c", + "src/blas/level3/dgemm.c", + "src/blas/level3/hgemm.c", + "src/blas/level3/qgemm.c" ], deps = ["@softfloat"], ) From 1beb9c35dd9a0d77daac95a5e1ad2b5c02ecb997 Mon Sep 17 00:00:00 2001 From: Sigilante Date: Wed, 28 Feb 2024 13:49:18 -0600 Subject: [PATCH 052/430] Include new commit hash. --- WORKSPACE.bazel | 2 +- bazel/third_party/softblas/softblas.BUILD | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index 5553f2357c..3938734312 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -296,7 +296,7 @@ versioned_http_archive( build_file = "//bazel/third_party/softblas:softblas.BUILD", # sha256 = "", url = "https://github.com/urbit/SoftBLAS/archive/{version}.tar.gz", - version = "bd637fdb23ac4ebd4048eb546633262ddf647b18", + version = "cbf3dfff5882fd03f28a74c7c0c6ef4c27ec176d", ) versioned_http_archive( diff --git a/bazel/third_party/softblas/softblas.BUILD b/bazel/third_party/softblas/softblas.BUILD index 8d669a7f77..101487d86a 100644 --- a/bazel/third_party/softblas/softblas.BUILD +++ b/bazel/third_party/softblas/softblas.BUILD @@ -6,7 +6,7 @@ cc_library( name = "softblas", visibility = ["//visibility:public"], hdrs = ["include/softblas.h"], - # includes = ["include"], + includes = ["include"], srcs = ["src/blas/level1/sasum.c", "src/blas/level1/dasum.c", "src/blas/level1/hasum.c", From e91829d00b15de10c582696548bd2dc5968b24b6 Mon Sep 17 00:00:00 2001 From: Sigilante Date: Thu, 29 Feb 2024 10:30:41 -0600 Subject: [PATCH 053/430] Post jet fundamentals. --- pkg/c3/motes.h | 6 ++ pkg/noun/jets/f/lagoon.c | 157 +++++++++++++++++++++++++++++++++++++++ pkg/noun/jets/q.h | 2 +- pkg/noun/jets/tree.c | 10 ++- 4 files changed, 171 insertions(+), 4 deletions(-) create mode 100644 pkg/noun/jets/f/lagoon.c diff --git a/pkg/c3/motes.h b/pkg/c3/motes.h index db17834057..3cbce8c48c 100644 --- a/pkg/c3/motes.h +++ b/pkg/c3/motes.h @@ -258,6 +258,7 @@ # define c3__corp c3_s4('c','o','r','p') # define c3__corp c3_s4('c','o','r','p') # define c3__cow c3_s3('c','o','w') +# define c3__cplx c3_s3('c','p','l','x') # define c3__cpu c3_s3('c','p','u') # define c3__crad c3_s4('c','r','a','d') # define c3__cram c3_s4('c','r','a','m') @@ -430,6 +431,7 @@ # define c3__fit c3_s3('f','i','t') # define c3__fits c3_s4('f','i','t','s') # define c3__fix c3_s3('f','i','x') +# define c3__fixp c3_s3('f','i','x','p') # define c3__fl c3_s2('f','l') # define c3__flac c3_s4('f','l','a','c') # define c3__flag c3_s4('f','l','a','g') @@ -602,6 +604,7 @@ # define c3__info c3_s4('i','n','f','o') # define c3__init c3_s4('i','n','i','t') # define c3__ins c3_s3('i','n','s') +# define c3__int2 c3_s4('i','n','t','2') # define c3__into c3_s4('i','n','t','o') # define c3__intr c3_s4('i','n','t','r') # define c3__inuk c3_s4('i','n','u','k') @@ -970,6 +973,7 @@ # define c3__rasp c3_s4('r','a','s','p') # define c3__raw c3_s3('r','a','w') # define c3__read c3_s4('r','e','a','d') +# define c3__real c3_s4('r','e','a','l') # define c3__reck c3_s4('r','e','c','k') # define c3__reef c3_s4('r','e','e','f') # define c3__resd c3_s4('r','e','s','d') @@ -1229,11 +1233,13 @@ # define c3__ubin c3_s4('u','b','i','n') # define c3__ubit c3_s4('u','b','i','t') # define c3__ud c3_s2('u','d') +# define c3__uint c3_s4('u','i','n','t') # define c3__ulib c3_s4('u','l','i','b') # define c3__un c3_s2('u','n') # define c3__uniq c3_s4('u','n','i','q') # define c3__unix c3_s4('u','n','i','x') # define c3__unt c3_s3('u','n','t') +# define c3__unum c3_s3('u','n','u','m') # define c3__up c3_s2('u','p') # define c3__url c3_s3('u','r','l') # define c3__urth c3_s4('u','r','t','h') diff --git a/pkg/noun/jets/f/lagoon.c b/pkg/noun/jets/f/lagoon.c new file mode 100644 index 0000000000..e45b1ce124 --- /dev/null +++ b/pkg/noun/jets/f/lagoon.c @@ -0,0 +1,157 @@ +/// @file + +#include "jets/q.h" +#include "jets/w.h" + +#include "noun.h" +#include "softfloat.h" +#include "softblas.h" + +#include + + union half { + float16_t h; + c3_w c; + }; + + union sing { + float32_t s; + c3_w c; + }; + + union doub { + float64_t d; + c3_d c; + }; + + union quad { + float128_t q; + c3_d c[2]; + }; + + static inline void + _set_rounding(c3_w a) + { + switch ( a ) + { + default: + u3m_bail(c3__fail); + break; + case c3__n: + softfloat_roundingMode = softfloat_round_near_even; + break; + case c3__z: + softfloat_roundingMode = softfloat_round_minMag; + break; + case c3__u: + softfloat_roundingMode = softfloat_round_max; + break; + case c3__d: + softfloat_roundingMode = softfloat_round_min; + break; + } + } + +/* add +*/ + u3_noun + u3qf_la_add_real(u3_noun a_data, + u3_noun b_data, + u3_noun shape, + u3_noun bloq, + u3_noun rnd) + { + + fprintf(stderr, ">> u3qf_la_add_real\n"); + + // SoftBLAS needs to be used here. + return u3_none; + + // // Split a into component atoms. + // // (roll shape mul) => 2 x 3 = 6 + // c3_w size = 1; + // u3_atom shp = shape; + // while (u3_nul != shp) { + // shp = u3t(shp); + // size *= shp; + // } + + + + + // return u3i_word(len_w); + + + // union sing c, d, e; + // _set_rounding(r); + // c.c = u3r_word(0, a); + // d.c = u3r_word(0, b); + // e.s = _nan_unify_s(f32_add(c.s, d.s)); + + // return u3i_words(1, &e.c); + } + + u3_noun + u3wf_la_add(u3_noun cor) + { + // Each argument is a ray, [=meta data=@ux] + u3_noun a_meta, a_data, + b_meta, b_data; + + if ( c3n == u3r_mean(cor, + u3x_sam_4, &a_meta, + u3x_sam_5, &a_data, + u3x_sam_6, &b_meta, + u3x_sam_7, &b_data, + 0) || + c3n == u3ud(a_data) || + c3n == u3ud(b_data) ) + { + return u3m_bail(c3__exit); + } else { + u3_noun a_shape, a_bloq, a_kind, + b_shape, b_bloq, b_kind, + rnd, fxp; + if ( c3n == u3r_mean(a_meta, + 2, &a_shape, + 6, &a_bloq, + 7, &a_kind, + 0) || + c3n == u3r_mean(b_meta, + 2, &b_shape, + 6, &b_bloq, + 7, &b_kind, + 0) || + c3n == u3r_sing(a_shape, b_shape) || + c3n == u3r_sing(a_bloq, b_bloq) || + c3n == u3r_sing(a_kind, b_kind) || + c3n == u3r_mean(cor, 60, &rnd, 61, &fxp, 0) + ) + { + return u3m_bail(c3__exit); + } else { + switch (a_kind) { + case c3__real: + return u3qf_la_add_real(a_data, b_data, a_shape, a_bloq, rnd); + + // case c3__int2: + // return u3qf_la_add_int2(a_data, b_data, a_shape, a_bloq); + + // case c3__uint: + // return u3qf_la_add_uint(a_data, b_data, a_shape, a_bloq); + + // case c3__cplx: + // return u3qf_la_add_cplx(a_data, b_data, a_shape, a_bloq, rnd); + + // case c3__unum: + // return u3qf_la_add_unum(a_data, b_data, a_shape, a_bloq); + + // case c3__fixp: + // return u3qf_la_add_fixp(a_data, b_data, a_shape, a_bloq); + + default: + return u3_none; + } + } + } + } diff --git a/pkg/noun/jets/q.h b/pkg/noun/jets/q.h index 8b99103e56..c7cb5ccfd0 100644 --- a/pkg/noun/jets/q.h +++ b/pkg/noun/jets/q.h @@ -247,7 +247,7 @@ u3_noun u3qfp_nepo(u3_noun, u3_noun); u3_noun u3qfp_rake(u3_noun); - u3_noun u3qf_la_add_real(u3_noun, u3_noun, u3_noun); + u3_noun u3qf_la_add_real(u3_noun, u3_noun, u3_noun, u3_noun, u3_noun); # define u3qfu_van_fan 28 # define u3qfu_van_rib 58 diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index 0499a5202d..375f0e1610 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -2148,9 +2148,13 @@ static u3j_core _139_hex_json_d[] = }; /* linear algebra jets + XX move to outer _hep_ core for /lib? */ - -static u3j_harm _139_hex_lagoon_add_a[] = {{".2", u3wf_la_add}, {}}; +static u3j_harm _139_hex__lagoon_add_a[] = {{".2", u3wf_la_add}, {}}; +static u3j_core _139_hex__lagoon_d[] = + { { "add-rays", 7, _139_hex__lagoon_add_a, 0, no_hashes }, + {} + }; static u3j_core _139_hex_d[] = { { "lore", 63, _140_hex_lore_a, 0, no_hashes }, @@ -2171,7 +2175,7 @@ static u3j_core _139_hex_d[] = { "mimes", 31, 0, _140_hex_mimes_d, no_hashes }, { "json", 31, 0, _139_hex_json_d, no_hashes }, - { "add", 7, _139_hex_lagoon_add_a, 0, no_hashes }, + { "lagoon", 31, 0, _139_hex__lagoon_d, no_hashes }, {} }; From 4be17fc8b90eb7c6b68f4510b3541f50f2806647 Mon Sep 17 00:00:00 2001 From: Sigilante Date: Thu, 29 Feb 2024 13:11:05 -0600 Subject: [PATCH 054/430] Hints for Lagoon work. --- pkg/noun/jets/f/lagoon.c | 29 +++++++++++++++++------------ pkg/noun/jets/tree.c | 10 ++++++++-- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/pkg/noun/jets/f/lagoon.c b/pkg/noun/jets/f/lagoon.c index e45b1ce124..5b27ccb31e 100644 --- a/pkg/noun/jets/f/lagoon.c +++ b/pkg/noun/jets/f/lagoon.c @@ -97,6 +97,7 @@ // Each argument is a ray, [=meta data=@ux] u3_noun a_meta, a_data, b_meta, b_data; + fprintf(stderr, "\n>> u3wf_la_add\n"); if ( c3n == u3r_mean(cor, u3x_sam_4, &a_meta, @@ -109,27 +110,31 @@ { return u3m_bail(c3__exit); } else { - u3_noun a_shape, a_bloq, a_kind, - b_shape, b_bloq, b_kind, - rnd, fxp; + u3_noun a_shape, a_bloq, a_kind, a_fxp, + b_shape, b_bloq, b_kind, b_fxp, + rnd; if ( c3n == u3r_mean(a_meta, - 2, &a_shape, - 6, &a_bloq, - 7, &a_kind, - 0) || + 2, &a_shape, + 6, &a_bloq, + 14, &a_kind, + 15, &a_fxp, + 0) || c3n == u3r_mean(b_meta, - 2, &b_shape, - 6, &b_bloq, - 7, &b_kind, - 0) || + 2, &b_shape, + 6, &b_bloq, + 14, &b_kind, + 15, &b_fxp, + 0) || c3n == u3r_sing(a_shape, b_shape) || c3n == u3r_sing(a_bloq, b_bloq) || c3n == u3r_sing(a_kind, b_kind) || - c3n == u3r_mean(cor, 60, &rnd, 61, &fxp, 0) + // fxp does not need to match so no check + c3n == u3r_mean(cor, 31, &rnd, 0) ) { return u3m_bail(c3__exit); } else { + fprintf(stderr, ">> u3wf_la_add: a_kind: %x\n", a_kind); switch (a_kind) { case c3__real: return u3qf_la_add_real(a_data, b_data, a_shape, a_bloq, rnd); diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index 375f0e1610..b6597ffa7f 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -2148,14 +2148,20 @@ static u3j_core _139_hex_json_d[] = }; /* linear algebra jets - XX move to outer _hep_ core for /lib? + XX move to outer _sep_ core for /lib? eventually +static u3j_core _139_sep_d[] = */ static u3j_harm _139_hex__lagoon_add_a[] = {{".2", u3wf_la_add}, {}}; -static u3j_core _139_hex__lagoon_d[] = +static u3j_core _139_hex__la_core_d[] = { { "add-rays", 7, _139_hex__lagoon_add_a, 0, no_hashes }, {} }; +static u3j_core _139_hex__lagoon_d[] = + { { "la-core", 7, 0, _139_hex__la_core_d, no_hashes }, + {} + }; + static u3j_core _139_hex_d[] = { { "lore", 63, _140_hex_lore_a, 0, no_hashes }, { "leer", 63, _140_hex_leer_a, 0, no_hashes }, From 81718e2d75e4e49471be12ae697ca10fe574878e Mon Sep 17 00:00:00 2001 From: Sigilante Date: Thu, 29 Feb 2024 14:04:23 -0600 Subject: [PATCH 055/430] ++add for Lagoon working. --- pkg/noun/jets/f/lagoon.c | 89 +++++++++++++++++++++++++++++++--------- 1 file changed, 70 insertions(+), 19 deletions(-) diff --git a/pkg/noun/jets/f/lagoon.c b/pkg/noun/jets/f/lagoon.c index 5b27ccb31e..3aa27e63d3 100644 --- a/pkg/noun/jets/f/lagoon.c +++ b/pkg/noun/jets/f/lagoon.c @@ -52,6 +52,18 @@ } } +/* shape +*/ + static inline uint64_t _get_shape(u3_noun shape) + { + uint64_t res = 1; + while (u3_nul != shape) { + res = res * u3h(shape); + shape = u3t(shape); + } + return res; + } + /* add */ u3_noun @@ -64,31 +76,72 @@ fprintf(stderr, ">> u3qf_la_add_real\n"); - // SoftBLAS needs to be used here. - return u3_none; + // Unpack the data as a byte array for SoftBLAS. + uint64_t len_a = _get_shape(shape); + uint8_t* a_bytes = (uint8_t*)malloc(len_a*sizeof(uint8_t)); + u3r_bytes(0, len_a, a_bytes, a_data); + uint8_t* b_bytes = (uint8_t*)malloc(len_a*sizeof(uint8_t)); + u3r_bytes(0, len_a, b_bytes, b_data); + + u3_noun r_data; + + // Switch on the block size. + switch (bloq) { + case 4: + haxpy(len_a, (float16_t){SB_REAL16_ONE}, (float16_t*)a_bytes, 1, (float16_t*)b_bytes, 1); + + // Unpack the result back into a noun. + r_data = u3i_bytes(len_a, b_bytes); - // // Split a into component atoms. - // // (roll shape mul) => 2 x 3 = 6 - // c3_w size = 1; - // u3_atom shp = shape; - // while (u3_nul != shp) { - // shp = u3t(shp); - // size *= shp; - // } + // Clean up. + free(a_bytes); + free(b_bytes); + return u3nc(a_data, r_data); + break; + case 5: + saxpy(len_a, (float32_t){SB_REAL32_ONE}, (float32_t*)a_bytes, 1, (float32_t*)b_bytes, 1); + // Unpack the result back into a noun. + r_data = u3i_bytes(len_a, b_bytes); - // return u3i_word(len_w); + // Clean up. + free(a_bytes); + free(b_bytes); + return u3nc(a_data, r_data); + break; - // union sing c, d, e; - // _set_rounding(r); - // c.c = u3r_word(0, a); - // d.c = u3r_word(0, b); - // e.s = _nan_unify_s(f32_add(c.s, d.s)); + case 6: + daxpy(len_a, (float64_t){SB_REAL64_ONE}, (float64_t*)a_bytes, 1, (float64_t*)b_bytes, 1); - // return u3i_words(1, &e.c); + // Unpack the result back into a noun. + r_data = u3i_bytes(len_a, b_bytes); + + // Clean up. + free(a_bytes); + free(b_bytes); + + return u3nc(a_data, r_data); + break; + + case 7: + qaxpy(len_a, (float128_t){SB_REAL128L_ONE,SB_REAL128U_ONE}, (float128_t*)a_bytes, 1, (float128_t*)b_bytes, 1); + + // Unpack the result back into a noun. + r_data = u3i_bytes(len_a, b_bytes); + + // Clean up. + free(a_bytes); + free(b_bytes); + + return u3nc(a_data, r_data); + break; + + default: + return u3_none; + } } u3_noun @@ -97,7 +150,6 @@ // Each argument is a ray, [=meta data=@ux] u3_noun a_meta, a_data, b_meta, b_data; - fprintf(stderr, "\n>> u3wf_la_add\n"); if ( c3n == u3r_mean(cor, u3x_sam_4, &a_meta, @@ -134,7 +186,6 @@ { return u3m_bail(c3__exit); } else { - fprintf(stderr, ">> u3wf_la_add: a_kind: %x\n", a_kind); switch (a_kind) { case c3__real: return u3qf_la_add_real(a_data, b_data, a_shape, a_bloq, rnd); From 7b005d2666849c29c34d91f912faa4c13cfb7f66 Mon Sep 17 00:00:00 2001 From: parallels Date: Mon, 4 Mar 2024 10:27:15 -0500 Subject: [PATCH 056/430] xmas: improved logging --- pkg/vere/io/blake.c | 57 +++-- pkg/vere/io/blake.h | 8 + pkg/vere/io/xmas.c | 516 ++++++++++++++++++++++++-------------------- 3 files changed, 328 insertions(+), 253 deletions(-) diff --git a/pkg/vere/io/blake.c b/pkg/vere/io/blake.c index 5bcc4b37de..824f51005d 100644 --- a/pkg/vere/io/blake.c +++ b/pkg/vere/io/blake.c @@ -50,11 +50,11 @@ _log_words(c3_c* str_c, c3_w* buf_w, c3_w len_w) fprintf(stderr, "\r\n"); } -static void -_log_node(blake_node* nod_u) +void +log_node(blake_node* nod_u) { - _log_words("CV", (c3_w*)nod_u->cev_y, 8); - _log_words("block", (c3_w*)nod_u->boq_y, 16); + _log_buf("CV", (c3_w*)nod_u->cev_y, BLAKE3_OUT_LEN); + _log_buf("block", (c3_w*)nod_u->boq_y, BLAKE3_BLOCK_LEN); fprintf(stderr, "counter: %llu\r\n", nod_u->con_d); fprintf(stderr, "length: %u\r\n", nod_u->len_y); fprintf(stderr, "flag: %x\r\n", nod_u->fag_y); @@ -62,8 +62,8 @@ _log_node(blake_node* nod_u) static void print_proof(void* vod_p, c3_w i_w) { c3_y* pof_y = (c3_y*)vod_p; - fprintf(stderr, "proof: %u", i_w); - _log_words("", pof_y, BLAKE3_OUT_LEN/4); + fprintf(stderr, "proof: %u\r\n", i_w); + _log_buf("", pof_y, BLAKE3_OUT_LEN); } @@ -90,12 +90,19 @@ _vec_each(u3_raw_vec* vec_u, void (*func)(void*, c3_w)) { } } -static void -_vec_free(u3_raw_vec* vec_u) +void +vec_free(u3_raw_vec* vec_u) { c3_free(vec_u->vod_p); } +void +vec_drop(u3_raw_vec* vec_u) +{ + vec_free(vec_u); + c3_free(vec_u); +} + u3_raw_vec* vec_make(c3_w siz_w) { @@ -160,6 +167,7 @@ vec_copy(u3_raw_vec* dst_u, u3_raw_vec* src_u) { for ( c3_w i_w = 0; i_w < src_u->len_w; i_w++ ) { dst_u->vod_p[i_w] = src_u->vod_p[i_w]; } + dst_u->len_w = src_u->len_w; } static void* @@ -224,10 +232,11 @@ _log_blake_subtree(blake_subtree* sub_u) fprintf(stderr, "sub: (%llu, %llu)\r\n", sub_u->sin_d, sub_u->dex_d); } -static void -_log_bao(blake_bao* bao_u) +void +log_bao(blake_bao* bao_u) { fprintf(stderr, "num: %llu\r\n", bao_u->num_w); + fprintf(stderr, "con: %llu\r\n", bao_u->con_w); fprintf(stderr, "sub: (%llu, %llu)\r\n", bao_u->sub_u.sin_d, bao_u->sub_u.dex_d); fprintf(stderr, "queue\r\n"); _vec_each(&bao_u->que_u, print_proof); @@ -320,8 +329,8 @@ _compress_node(c3_y out_y[BLAKE3_BLOCK_LEN], blake_node* nod_u) } -static void -_make_chain_value(c3_y out_y[32], blake_node* nod_u) +void +make_chain_value(c3_y out_y[32], blake_node* nod_u) { c3_y tmp_y[64]; _compress_node(tmp_y, nod_u); @@ -362,8 +371,8 @@ _parent_hash(blake_node* sin_u, blake_node* dex_u) { c3_y* sin_y = c3_calloc(BLAKE3_OUT_LEN); c3_y* dex_y = c3_calloc(BLAKE3_OUT_LEN); - _make_chain_value(sin_y, sin_u); - _make_chain_value(dex_y, dex_u); + make_chain_value(sin_y, sin_u); + make_chain_value(dex_y, dex_u); return _parent_node(sin_y, dex_y, IV, 0); } @@ -410,8 +419,8 @@ static void recurse_blake_subtree(blake_subtree* sub_u, blake_node* nod_u, u3_ve recurse_blake_subtree(dex_u, rod_u, rar_u); blake_pair* new_u = c3_calloc(sizeof(blake_pair)); - _make_chain_value(new_u->sin_y, lod_u); - _make_chain_value(new_u->dex_y, rod_u); + make_chain_value(new_u->sin_y, lod_u); + make_chain_value(new_u->dex_y, rod_u); memcpy(nod_u, _parent_hash(lod_u, rod_u), sizeof(blake_node)); vec_append(par_u, new_u); @@ -500,14 +509,21 @@ _veri_check_leaf(blake_bao* bao_u, c3_y* dat_y, c3_w dat_w) _pop_leaf(out_y, bao_u); c3_y cav_y[BLAKE3_OUT_LEN]; blake_node* nod_u = blake_leaf_hash(dat_y, dat_w, bao_u->con_w); - _make_chain_value(cav_y, nod_u); + make_chain_value(cav_y, nod_u); + if ( bao_u->con_w == 3 ) { + log_node(nod_u); + _log_buf("have", cav_y, 32); + _log_buf("want", out_y, 32); + } + bao_u->con_w++; return 0 == memcmp(cav_y, out_y, BLAKE3_OUT_LEN) ? c3y : c3n; } else if ( _vec_len(&bao_u->sta_u) > 0 ) { c3_y out_y[BLAKE3_OUT_LEN]; _pop_parent(out_y, bao_u); c3_y cav_y[BLAKE3_OUT_LEN]; blake_node* nod_u = blake_leaf_hash(dat_y, dat_w, bao_u->con_w); - _make_chain_value(cav_y, nod_u); + make_chain_value(cav_y, nod_u); + bao_u->con_w++; return 0 == memcmp(cav_y, out_y, BLAKE3_OUT_LEN) ? c3y : c3n; } return c3n; @@ -525,7 +541,7 @@ _veri_check_pair(blake_bao* bao_u, blake_pair* par_u) _pop_parent(par_y, bao_u); blake_node* nod_u = _parent_node(par_u->sin_y, par_u->dex_y, IV, 0); c3_y cav_y[BLAKE3_OUT_LEN]; - _make_chain_value(cav_y, nod_u); + make_chain_value(cav_y, nod_u); return _(memcmp(par_y, cav_y, BLAKE3_OUT_LEN) == 0); } @@ -550,7 +566,7 @@ blake_bao_make(c3_w num_w, u3_vec(c3_y[BLAKE3_OUT_LEN])* pof_u) c3_y* snd_y = vec_popf(pof_u); vec_init(&bao_u->sta_u, 8); vec_init(&bao_u->que_u, 8); - vec_copy(&bao_u->que_u, pof_u); + vec_copy(&bao_u->sta_u, pof_u); _push_leaves(bao_u, fst_y, snd_y); return bao_u; } @@ -579,7 +595,6 @@ blake_bao_verify(blake_bao* bao_u, c3_y* dat_y, c3_w dat_w, blake_pair* par_u) } else { _push_parents(bao_u, par_u); } - bao_u->con_w++; if ( bao_u->con_w == bao_u->num_w ) { return BAO_DONE; } diff --git a/pkg/vere/io/blake.h b/pkg/vere/io/blake.h index 18dd174bf9..ce1c0060ab 100644 --- a/pkg/vere/io/blake.h +++ b/pkg/vere/io/blake.h @@ -61,10 +61,18 @@ void vec_append(u3_raw_vec*, void*); u3_raw_vec* vec_make(c3_w); +void vec_free(u3_raw_vec*); +void vec_drop(u3_raw_vec*); + blake_node* blake_leaf_hash(c3_y* dat_y, c3_w dat_w, c3_d con_d); blake_bao* blake_bao_make(c3_w, u3_raw_vec* pof_u); +void make_chain_value(c3_y*, blake_node*); + +void log_node(blake_node*); +void log_bao(blake_bao*); + c3_y blake_bao_verify(blake_bao*, c3_y*, c3_w, blake_pair* par_u); #endif \ No newline at end of file diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c index 365394f2ae..9bd0880d80 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/xmas.c @@ -20,7 +20,7 @@ c3_o dop_o = c3n; -//#define XMAS_DEBUG c3y +// #define XMAS_DEBUG c3y //#define XMAS_TEST #define XMAS_VER 1 #define FINE_PAGE 4096 // packets per page @@ -32,6 +32,43 @@ c3_o dop_o = c3n; #define DEF_TEXT "\033[0m" #define REORDER_THRESH 5 +// logging and debug symbols +#define XMAS_SYM_DESC(SYM) XMAS_DESC_ ## SYM +#define XMAS_SYM_FIELD(SYM) XMAS_FIELD_ ## SYM +#ifdef XMAS_DEBUG + #define XMAS_LOG(SYM, ...) { sam_u->sat_u.XMAS_SYM_FIELD(SYM)++; u3l_log("xmas: (%u) %s", __LINE__, XMAS_SYM_DESC(SYM)); } +#else + #define XMAS_LOG(SYM, ...) { sam_u->sat_u.XMAS_SYM_FIELD(SYM)++; } +#endif + +typedef struct _u3_xmas_stat { + c3_w dop_w; // dropped for other reasons + c3_w ser_w; // dropped for serialisation + c3_w aut_w; // droppped for auth + c3_w apa_w; // dropped bc no interest + c3_w inv_w; // non-fatal invariant violation + c3_w dup_w; // duplicates +} u3_xmas_stat; + +#define XMAS_DESC_STRANGE "dropped strange packet" +#define XMAS_FIELD_STRANGE dop_w + +#define XMAS_DESC_SERIAL "dropped packet (serialisation)" +#define XMAS_FIELD_SERIAL ser_w + +#define XMAS_DESC_AUTH "dropped packet (authentication)" +#define XMAS_FIELD_AUTH aut_w + +#define XMAS_DESC_INVARIANT "invariant violation" +#define XMAS_FIELD_INVARIANT inv_w + +#define XMAS_DESC_APATHY "dropped packet (no interest)" +#define XMAS_FIELD_APATHY apa_w + +#define XMAS_DESC_DUPE "dropped packet (duplicate)" +#define XMAS_FIELD_DUPE dup_w + + #define IN_FLIGHT 10 // pending interest sentinels @@ -89,10 +126,10 @@ typedef struct _u3_buf { } u3_buf; typedef struct _u3_pend_req { - c3_w nex_w; - c3_w tot_w; - uv_timer_t tim_u; - struct _u3_xmas_pact* pac_u; + c3_w nex_w; // number of the next fragment to be sent + c3_w tot_w; // total number of fragments expected + uv_timer_t tim_u; // timehandler + struct _u3_xmas_pact* pac_u; // preallocated request packet u3_pact_stat* wat_u; // ((mop @ud packet-state) lte) u3_bitset was_u; // ((mop @ud packet-state) lte) c3_y* dat_y; // ((mop @ud *) lte) @@ -101,15 +138,15 @@ typedef struct _u3_pend_req { c3_w old_w; // frag num of oldest packet sent c3_w ack_w; // highest acked fragment number u3_lane lan_u; // last lane heard - u3_gage* gag_u; + u3_gage* gag_u; // congestion control u3_vec(u3_buf) mis_u; // misordered blake hash blake_bao* bao_u; // blake verifier } u3_pend_req; typedef struct _u3_czar_info { - c3_w pip_w; - time_t tim_t; - u3_noun pen; + c3_w pip_w; // IP of galaxy + time_t tim_t; // time of retrieval + u3_noun pen; // (list @) of pending packet } u3_czar_info; /* _u3_xmas: next generation networking @@ -121,17 +158,18 @@ typedef struct _u3_xmas { uv_udp_t wax_u; uv_handle_t had_u; }; - c3_l sev_l; + u3_xmas_stat sat_u; // statistics + c3_l sev_l; // XX: ?? c3_o for_o; // is forwarding ur_cue_test_t* tes_u; // cue-test handle u3_cue_xeno* sil_u; // cue handle - u3p(u3h_root) her_p; // - u3p(u3h_root) pac_p; // pending - u3p(u3h_root) lan_p; // lanes - u3_czar_info imp_u[256]; + u3p(u3h_root) her_p; // (map ship + u3p(u3h_root) pac_p; // packet cache + u3p(u3h_root) lan_p; // lane cache + u3_czar_info imp_u[256]; // galaxy information u3p(u3h_root) req_p; // hashtable of u3_pend_req - c3_c* dns_c; - c3_d tim_d; + c3_c* dns_c; // turf (urb.otrg) + c3_d tim_d; // XX: remove } u3_xmas; typedef struct _u3_peer { @@ -303,6 +341,11 @@ typedef struct _u3_cace_enty { }; } u3_cace_enty; + +// Sent request +// because of lifecycles a u3_xmas_pact may have several libuv +// callbacks associated with it, so we can't those as callback +// instead just alloc new buffer and stick here typedef struct _u3_seal { uv_udp_send_t snd_u; // udp send request u3_xmas* sam_u; @@ -310,9 +353,6 @@ typedef struct _u3_seal { c3_y* buf_y; } u3_seal; - - - static c3_d get_millis() { struct timeval tp; @@ -496,6 +536,7 @@ _log_pend_req(u3_pend_req* req_u) } u3l_log("next: %u", req_u->nex_w); u3l_log("total: %u", req_u->tot_w); + u3l_log("gage: %c", req_u->gag_u == NULL ? 'n' : 'y'); //u3l_log("timer in: %" PRIu64 " ms", uv_timer_get_due_in(&req_u->tim_u)); } @@ -653,6 +694,7 @@ _log_bitset(u3_bitset* bit_u) // TODO: fix with scry into jael?? // remove rear call to enable star routing +// XX: remove static u3_noun _xmas_get_sponsor_list(c3_d her_d[2]) { @@ -947,8 +989,10 @@ _xmas_sift_hop_long(u3_xmas_hop_once* hop_u, c3_y* buf_y, c3_w len_w) static c3_w -_xmas_sift_page_pact(u3_xmas_page_pact* pac_u, c3_y nex_y, c3_y* buf_y, c3_w len_w) +_xmas_sift_page_pact(u3_xmas_pact* pat_u, c3_y nex_y, c3_y* buf_y, c3_w len_w) { + u3_xmas_page_pact* pac_u = &pat_u->pag_u; + u3_xmas* sam_u = pat_u->sam_u; c3_w cur_w = 0, nex_w; if ( !(nex_w = _xmas_sift_name(&pac_u->nam_u, buf_y + cur_w, len_w)) ) { @@ -963,7 +1007,7 @@ _xmas_sift_page_pact(u3_xmas_page_pact* pac_u, c3_y nex_y, c3_y* buf_y, c3_w len switch ( nex_y ) { default: { - u3l_log("xmas: bad hopcount"); + XMAS_LOG(SERIAL); return 0; } case HOP_NONE: break; @@ -1056,7 +1100,7 @@ _xmas_sift_pact(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w) res_w = _xmas_sift_peek_pact(&pac_u->pek_u, buf_y, len_w); } break; case PACT_PAGE: { - res_w = _xmas_sift_page_pact(&pac_u->pag_u, pac_u->hed_u.nex_y, buf_y, len_w); + res_w = _xmas_sift_page_pact(pac_u, pac_u->hed_u.nex_y, buf_y, len_w); } break; case PACT_POKE: { res_w = _xmas_sift_poke_pact(&pac_u->pok_u, buf_y, len_w); @@ -1509,6 +1553,8 @@ u3_xmas_encode_lane(u3_lane lan_u) { return u3nt(c3__if, u3i_word(lan_u.pip_w), lan_u.por_s); } +// lane cache is (map [lane @p] lane-info) +// static u3_noun _xmas_lane_key(c3_d her_d[2], u3_lane* lan_u) { @@ -1577,7 +1623,7 @@ _xmas_get_request(u3_xmas* sam_u, u3_xmas_name* nam_u) { if ( res != u3_none && res != u3_nul ) { ret_u = u3to(u3_pend_req, res); } - if ( ret_u != NULL && ret_u->tot_w != 0 && ret_u->tot_w == ret_u->len_w ) { + if ( ret_u != NULL ) { ret_u = NULL; } @@ -1595,11 +1641,11 @@ _xmas_del_request(u3_xmas* sam_u, u3_xmas_name* nam_u) { return; } u3_pend_req* req_u = u3to(u3_pend_req, req); - _xmas_pact_free(req_u->pac_u); - _free_bitset(&req_u->was_u); - c3_free(req_u->pac_u); - c3_free(req_u->dat_y); - u3a_free(req_u); + // _xmas_pact_free(req_u->pac_u); + // _free_bitset(&req_u->was_u); + // c3_free(req_u->pac_u); + // c3_free(req_u->dat_y); + // u3a_free(req_u); u3h_del(sam_u->req_p, key); u3z(key); } @@ -1644,6 +1690,7 @@ _xmas_packet_timeout(uv_timer_t* tim_u) { u3l_log("%u packet timed out", req_u->old_w); } +// congestion control update static void _xmas_handle_ack(u3_gage* gag_u, u3_pact_stat* pat_u) { @@ -1681,9 +1728,12 @@ _xmas_req_get_cwnd_nex(u3_xmas* sam_u, u3_xmas_name* nam_u, c3_w* nex_w, u3_xmas return 0; } *nex_u = req_u->pac_u; + if ( req_u->tot_w == 0 || req_u->gag_u == NULL ) { u3l_log("shouldn't happen"); *nex_w = 0; + _log_pend_req(req_u); + u3_assert(0); return 1; } @@ -1713,7 +1763,7 @@ _xmas_req_pact_resent(u3_xmas* sam_u, u3_xmas_name* nam_u) } /* _xmas_req_pact_sent(): mark packet as sent -** +** after 1-RT first packet is handled in _xmas_req_pact_init() */ static void _xmas_req_pact_sent(u3_xmas* sam_u, u3_xmas_name* nam_u) @@ -1729,38 +1779,19 @@ _xmas_req_pact_sent(u3_xmas* sam_u, u3_xmas_name* nam_u) // TODO: optional assertions? req_u->wat_u[nam_u->fra_w] = (u3_pact_stat){now_d, 0, 1, 0 }; _put_bitset(&req_u->was_u, nam_u->fra_w); - if ( nam_u->fra_w == 1 ) { - req_u->lef_w = 1; - } + // if ( nam_u->fra_w == 1 ) { + // req_u->lef_w = 1; + // } } else { - // instantiate pending request - // stack allocating is ok, because _xmas_put_pend will copy it out anyway - req_u = alloca(sizeof(u3_pend_req)); - memset(req_u, 0, sizeof(u3_pend_req)); - req_u->pac_u = c3_calloc(sizeof(u3_xmas_pact)); - req_u->pac_u->sam_u = sam_u; - req_u->pac_u->hed_u.typ_y = PACT_PEEK; - req_u->gag_u = NULL; - memcpy(&req_u->pac_u->pek_u.nam_u, nam_u, sizeof(u3_xmas_name)); - // TODO: handle restart - // u3_assert( nam_u->fra_w == 0 ); - req_u->nex_w = 1; - req_u->tot_w = 0; - req_u->len_w = 0; - req_u->dat_y = NULL; - uv_timer_init(u3L, &req_u->tim_u); - req_u->wat_u = NULL; - req_u->was_u.buf_y = NULL; - req_u->lef_w = 0; - req_u->old_w = 1; - req_u->ack_w = 0; + u3l_log("xmas: no req for sent"); + return; } if ( req_u->lef_w != 0 && c3n == _has_bitset(&req_u->was_u, req_u->lef_w) ) { while ( req_u->lef_w++ < req_u->tot_w ) { if ( c3y == _has_bitset(&req_u->was_u, req_u->lef_w) ) { - break; + break; } } } @@ -1819,16 +1850,6 @@ u3_xmas_decode_lane(u3_atom lan) { return lan_u; } -/* u3_xmas_lane_to_chub(): serialize lane to double-word -*/ -static c3_d -u3_xmas_lane_to_chub(u3_lane lan) { - return ((c3_d)lan.por_s << 32) ^ (c3_d)lan.pip_w; -} - - - - // END plagariasm zone // // @@ -1874,6 +1895,7 @@ _xmas_send_cb(uv_udp_send_t* req_u, c3_i sas_i) static void _xmas_send_buf(u3_xmas* sam_u, u3_lane lan_u, c3_y* buf_y, c3_w len_w) { u3_seal* sel_u = c3_calloc(sizeof(*sel_u)); + // this is wrong, need to calloc & memcpy sel_u->buf_y = buf_y; sel_u->len_w = len_w; sel_u->sam_u = sam_u; @@ -1960,39 +1982,39 @@ _try_resend(u3_pend_req* req_u) } c3_w ack_w = req_u->ack_w - REORDER_THRESH; c3_d now_d = _get_now_micros(); - for( int i = req_u->lef_w; i < ack_w; i++ ) { + for ( int i = req_u->lef_w; i < ack_w; i++ ) { if ( c3y == _has_bitset(&req_u->was_u, i) ) { req_u->pac_u->pek_u.nam_u.fra_w = i; if ( req_u->wat_u[i].tie_y == 1 ) { - los_o = c3y; - c3_y* buf_y = c3_calloc(PACT_SIZE); - req_u->pac_u->pek_u.nam_u.fra_w = i; - c3_w siz_w = _xmas_etch_pact(buf_y, req_u->pac_u); - if( siz_w == 0 ) { - u3l_log("failed to etch"); - u3_assert( 0 ); - } - // TODO: better route management - _xmas_send_buf(sam_u, *lan_u, buf_y, siz_w); - _xmas_req_pact_resent(sam_u, &req_u->pac_u->pek_u.nam_u); + los_o = c3y; + c3_y* buf_y = c3_calloc(PACT_SIZE); + req_u->pac_u->pek_u.nam_u.fra_w = i; + c3_w siz_w = _xmas_etch_pact(buf_y, req_u->pac_u); + if( siz_w == 0 ) { + u3l_log("failed to etch"); + u3_assert( 0 ); + } + // TODO: better route management + _xmas_send_buf(sam_u, *lan_u, buf_y, siz_w); + _xmas_req_pact_resent(sam_u, &req_u->pac_u->pek_u.nam_u); } else if ( (now_d - req_u->wat_u[i].sen_d) > req_u->gag_u->rto_w ) { - los_o = c3y; - c3_y* buf_y = c3_calloc(PACT_SIZE); - req_u->pac_u->pek_u.nam_u.fra_w = i; - u3l_log("slow resending %u ", i); - c3_w siz_w = _xmas_etch_pact(buf_y, req_u->pac_u); - if( siz_w == 0 ) { - u3l_log("failed to etch"); - u3_assert( 0 ); - } - // TODO: better route management - _xmas_send_buf(sam_u, *lan_u, buf_y, siz_w); - _xmas_req_pact_resent(sam_u, &req_u->pac_u->pek_u.nam_u); - } - } - } - + los_o = c3y; + c3_y* buf_y = c3_calloc(PACT_SIZE); + req_u->pac_u->pek_u.nam_u.fra_w = i; + u3l_log("slow resending %u ", i); + c3_w siz_w = _xmas_etch_pact(buf_y, req_u->pac_u); + if( siz_w == 0 ) { + u3l_log("failed to etch"); + u3_assert( 0 ); + } + // TODO: better route management + _xmas_send_buf(sam_u, *lan_u, buf_y, siz_w); + _xmas_req_pact_resent(sam_u, &req_u->pac_u->pek_u.nam_u); + } + } + } + if ( c3y == los_o ) { req_u->gag_u->sst_w = (req_u->gag_u->wnd_w / 2) + 1; req_u->gag_u->wnd_w = req_u->gag_u->sst_w; @@ -2000,38 +2022,38 @@ _try_resend(u3_pend_req* req_u) } } -static u3_vec(c3_y[BLAKE3_OUT_LEN])* -_xmas_get_proof_vec(u3_xmas_data* dat_u) -{ - u3_assert( dat_u->aum_u.typ_e == AUTH_SIGN ); - u3_assert( dat_u->aup_u.len_y != 0 ); - u3_vec(c3_y[BLAKE3_OUT_LEN])* pof_u = vec_make(4); - { - c3_y* lef_y = c3_calloc(BLAKE3_OUT_LEN); - blake_node* nod_u = blake_leaf_hash(dat_u->fra_y, dat_u->len_w, 0); - memcpy(lef_y, nod_u->cev_y, BLAKE3_OUT_LEN); - c3_free(nod_u); - vec_append(pof_u, lef_y); - } +// static u3_vec(c3_y[BLAKE3_OUT_LEN])* +// _xmas_get_proof_vec(u3_xmas_data* dat_u) +// { +// u3_assert( dat_u->aum_u.typ_e == AUTH_SIGN ); +// u3_assert( dat_u->aup_u.len_y != 0 ); +// u3_vec(c3_y[BLAKE3_OUT_LEN])* pof_u = vec_make(4); +// { +// c3_y* lef_y = c3_calloc(BLAKE3_OUT_LEN); +// blake_node* nod_u = blake_leaf_hash(dat_u->fra_y, dat_u->len_w, 0); +// memcpy(lef_y, nod_u->cev_y, BLAKE3_OUT_LEN); +// c3_free(nod_u); +// vec_append(pof_u, lef_y); +// } - for ( c3_w i_w = 0; i_w < dat_u->aup_u.len_y; i_w++ ) { - c3_y* has_y = c3_calloc(BLAKE3_OUT_LEN); - memcpy(has_y, dat_u->aup_u.has_y[i_w], BLAKE3_OUT_LEN); - vec_append(pof_u, has_y); - } - return pof_u; -} - -static c3_y -_xmas_verify_data(u3_xmas_data* dat_u, blake_bao* bao_u) -{ - blake_pair* par_u = NULL; - return blake_bao_verify(bao_u, dat_u->fra_y, dat_u->len_w, par_u); -} - -/* _xmas_req_pact_done(): mark packet as done, possibly producing the result +// for ( c3_w i_w = 0; i_w < dat_u->aup_u.len_y; i_w++ ) { +// c3_y* has_y = c3_calloc(BLAKE3_OUT_LEN); +// memcpy(has_y, dat_u->aup_u.has_y[i_w], BLAKE3_OUT_LEN); +// vec_append(pof_u, has_y); +// } +// return pof_u; +// } + +// static c3_y +// _xmas_verify_data(u3_xmas_data* dat_u, blake_bao* bao_u) +// { +// blake_pair* par_u = NULL; +// return blake_bao_verify(bao_u, dat_u->fra_y, dat_u->len_w, par_u); +// } + +/* _xmas_req_pact_done(): mark packet as done, returning if we should continue */ -static u3_weak +static c3_o _xmas_req_pact_done(u3_xmas* sam_u, u3_xmas_name *nam_u, u3_xmas_data* dat_u, u3_lane* lan_u) { u3_weak ret = u3_none; @@ -2039,10 +2061,22 @@ _xmas_req_pact_done(u3_xmas* sam_u, u3_xmas_name *nam_u, u3_xmas_data* dat_u, u3 u3_pend_req* req_u = _xmas_get_request(sam_u, nam_u); if ( NULL == req_u ) { -#ifdef XMAS_DEBUG - u3l_log("xmas: attempting to mark packet done, no request"); -#endif - return u3_none; + XMAS_LOG(APATHY); + return c3n; + } + blake_pair* par_u = NULL; + if ( dat_u->aum_u.typ_e == AUTH_NEXT ) { + par_u = c3_calloc(sizeof(blake_pair)); + memcpy(par_u->sin_y, dat_u->aup_u.has_y[0], BLAKE3_OUT_LEN); + memcpy(par_u->dex_y, dat_u->aup_u.has_y[1], BLAKE3_OUT_LEN); + } + + c3_y ver_y; + // TODO: move to bottom + if( BAO_GOOD != (ver_y = blake_bao_verify(req_u->bao_u, dat_u->fra_y, dat_u->len_w, par_u))) { + // TODO: do we drop the whole request on the floor? + XMAS_LOG(AUTH); + return c3n; } u3_gage* gag_u = _xmas_get_lane(sam_u, nam_u->her_d, lan_u); @@ -2054,43 +2088,23 @@ _xmas_req_pact_done(u3_xmas* sam_u, u3_xmas_name *nam_u, u3_xmas_data* dat_u, u3 memset(gag_u, 0, sizeof(u3_gage)); _init_gage(gag_u); } - //if ( gag_u->wnd_w > 20 ) { } - - req_u->lan_u = *lan_u; c3_w siz_w = (1 << (nam_u->boq_y - 3)); // First packet received, instantiate request fully - if ( req_u->tot_w == 0 ) { - u3_assert( siz_w == 1024 ); // boq_y == 13 - req_u->dat_y = c3_calloc(siz_w * dat_u->tot_w); - req_u->wat_u = c3_calloc(sizeof(u3_pact_stat) * dat_u->tot_w); - req_u->tot_w = dat_u->tot_w; - _init_bitset(&req_u->was_u, dat_u->tot_w + 1); - req_u->lef_w = 2; - u3_vec(c3_y[BLAKE3_OUT_LEN])* pof_u = _xmas_get_proof_vec(dat_u); - req_u->bao_u = blake_bao_make(req_u->tot_w, pof_u); - } else { - //blake_bao_verify(req_u->bao_u); - - } if ( dat_u->tot_w <= nam_u->fra_w ) { - u3l_log("xmas: invalid packet (exceeded bounds)"); - return u3_none; + XMAS_LOG(STRANGE); + // XX: is this sufficient to drop whole request + return c3y; } // received duplicate if ( nam_u->fra_w != 0 && c3n == _has_bitset(&req_u->was_u, nam_u->fra_w) ) { -#ifdef XMAS_DEBUG - u3l_log("duplicate page"); -#endif - + XMAS_LOG(DUPE); req_u->wat_u[nam_u->fra_w].dup_y++; - u3l_log("duplicate %u", req_u->wat_u[nam_u->fra_w].dup_y); - - return u3_none; + return c3y; } _del_bitset(&req_u->was_u, nam_u->fra_w); @@ -2098,7 +2112,9 @@ _xmas_req_pact_done(u3_xmas* sam_u, u3_xmas_name *nam_u, u3_xmas_data* dat_u, u3 req_u->ack_w = nam_u->fra_w; } if ( nam_u->fra_w != 0 && req_u->wat_u[nam_u->fra_w].tie_y != 1 ) { +#ifdef XMAS_DEBUG u3l_log("received retry %u", nam_u->fra_w); +#endif } req_u->len_w++; @@ -2106,48 +2122,20 @@ _xmas_req_pact_done(u3_xmas* sam_u, u3_xmas_name *nam_u, u3_xmas_data* dat_u, u3 req_u->lef_w++; } - if ( nam_u->fra_w != 0 ) { - // handle gauge update - _xmas_handle_ack(gag_u, &req_u->wat_u[nam_u->fra_w]); - if ( (nam_u->fra_w % 10) == 0 ) { - } - } + // handle gauge update + _xmas_handle_ack(gag_u, &req_u->wat_u[nam_u->fra_w]); memcpy(req_u->dat_y + (siz_w * nam_u->fra_w), dat_u->fra_y, dat_u->len_w); c3_d wen_d = _get_now_micros(); _try_resend(req_u); - /* - * // in-order - if ( nam_u->fra_w != 0 && ((nam_u->fra_w - req_u->lef_w) > 5) ) { - u3l_log("diff: %u", _abs(nam_u->fra_w - req_u->old_w); - c3_y* buf_y = c3_calloc(PACT_SIZE); - req_u->pac_u->pek_u.nam_u.fra_w = req_u->old_w; - u3l_log("resending %u on num %u", req_u->old_w, nam_u->fra_w); - c3_w siz_w = _xmas_etch_pact(buf_y, req_u->pac_u); - if( siz_w == 0 ) { - u3l_log("failed to etch"); - u3_assert( 0 ); - } - // TODO: better route management - _xmas_send_buf(sam_u, *lan_u, buf_y, siz_w); - _xmas_req_pact_resent(sam_u, &req_u->pac_u->pek_u.nam_u); - }*/ - _update_oldest_req(req_u, gag_u); + _update_oldest_req(req_u, gag_u); - if ( req_u->len_w == req_u->tot_w ) { - uv_timer_stop(&req_u->tim_u); - _xmas_del_request(sam_u, nam_u); - //u3dc("rap", 13, u3dc("turn", u3n_slam_on(u3k(sam_u->mop_u.tap), req_u->dat), u3v_wish("tail"))); - //_xmas_put_request(sam_u, nam_u, u3_none); - return u3i_word(req_u->tot_w); - } else { - _xmas_put_request(sam_u, nam_u, req_u); - _xmas_put_lane(sam_u, nam_u->her_d, lan_u, gag_u); - } - return u3_none; + _xmas_put_request(sam_u, nam_u, req_u); + _xmas_put_lane(sam_u, nam_u->her_d, lan_u, gag_u); + return c3y; } @@ -2174,7 +2162,6 @@ _realise_lane(u3_noun lan) { lan_u.por_s = por; } else { u3l_log("xmas: inscrutable lane"); - u3m_p("lane", lan); } u3z(lan); } @@ -2212,41 +2199,23 @@ _xmas_timer_cb(uv_timer_t* tim_u) { _try_resend(req_u); } -static void -_xmas_update_req_peek(u3_xmas_pact* pac_u) -{ - u3_xmas* sam_u = pac_u->sam_u; - _xmas_req_pact_sent(sam_u, &pac_u->pek_u.nam_u); - - // TODO: set timers -} static void _xmas_ef_send(u3_xmas* sam_u, u3_noun las, u3_noun pac) { - u3_noun len, dat; - u3x_cell(pac, &len, &dat); - c3_w len_w = u3r_met(3, dat); - if ( len_w > len ) { -#ifdef XMAS_DEBUG - u3l_log("xmas: vane lying about length %i", len_w); - return; -#endif - } - c3_y* buf_y = c3_calloc(len); - u3r_bytes(0, len, buf_y, dat); + c3_w len_w = u3r_met(3, pac); + c3_y* buf_y = c3_calloc(len_w); + u3r_bytes(0, len_w, buf_y, pac); u3_xmas_pact pac_u; pac_u.sam_u = sam_u; - _xmas_sift_pact(&pac_u, buf_y, len); - + _xmas_sift_pact(&pac_u, buf_y, len_w); if ( pac_u.hed_u.typ_y == PACT_PEEK ) { sam_u->tim_d = _get_now_micros(); - _xmas_update_req_peek(&pac_u); } c3_o suc_o = c3n; u3_noun lan, t = las; - _xmas_rout_bufs(sam_u, buf_y, len, las); + _xmas_rout_bufs(sam_u, buf_y, len_w, las); u3z(pac); } @@ -2260,7 +2229,7 @@ static c3_o _xmas_kick(u3_xmas* sam_u, u3_noun tag, u3_noun dat) default: { ret_o = c3n; } break; - case c3__sent: { + case c3__send: { u3_noun las, pac; if ( c3n == u3r_cell(dat, &las, &pac) ) { ret_o = c3n; @@ -2465,7 +2434,11 @@ _name_to_scry(u3_xmas_name* nam_u) u3_noun fag = _dire_etch_ud(nam_u->fra_w); u3_noun pax = _xmas_encode_path(nam_u->pat_s, (c3_y*)nam_u->pat_c); - u3_noun res = u3kb_weld(u3nc(c3__pact, u3nq(rif, boq, fag, u3nc(c3__data, u3_nul))), pax); + u3_noun wer = nam_u->nit_o == c3y + ? u3nc(c3__init, pax) + : u3nt(nam_u->aut_o == c3y ? c3__auth : c3__data, fag, pax); + + u3_noun res = u3nc(c3__mess, u3nq(rif, c3__pact, boq, u3nc(c3__etch, wer))); return res; } @@ -2536,15 +2509,14 @@ _xmas_try_forward(u3_xmas_pact* pac_u, u3_noun fra, u3_noun hit) static c3_w _xmas_respond(u3_xmas_pact* req_u, c3_y** buf_y, u3_noun hit) { - u3_noun len, dat; - u3x_cell(hit, &len, &dat); + c3_w len_w = u3r_met(3, hit); - *buf_y = c3_calloc(len); - u3r_bytes(0, len, *buf_y, dat); + *buf_y = c3_calloc(len_w); + u3r_bytes(0, len_w, *buf_y, hit); u3z(hit); - return len; + return len_w; } static void @@ -2597,6 +2569,7 @@ _xmas_page_scry_cb(void* vod_p, u3_noun nun) u3_weak old = _xmas_get_cache(sam_u, &pac_u->pag_u.nam_u); if ( old == u3_none ) { u3l_log("bad"); + XMAS_LOG(APATHY); } else { u3_noun tag; u3_noun dat; @@ -2661,7 +2634,6 @@ _meet_peer(u3_xmas* sam_u, u3_peer* per_u, c3_d her_d[2]) u3_noun her = u3i_chubs(2, her_d); u3_noun gan = u3nc(u3_nul, u3_nul); u3_noun pax = u3nc(u3dc("scot", c3__p, her), u3_nul); - u3m_p("meet peer", pax); u3_pier_peek_last(sam_u->pir_u, gan, c3__j, c3__saxo, pax, per_u, _saxo_cb); } @@ -2689,7 +2661,7 @@ _xmas_forward(u3_xmas_pact* pac_u, u3_lane lan_u) //u3l_log("should update next hop"); u3_weak hit = _xmas_get_cache(sam_u, &pac_u->pag_u.nam_u); if ( u3_none == hit ) { - u3l_log("xmas: got forward, no interest"); + XMAS_LOG(APATHY); return; } u3_noun tag, dat; @@ -2714,6 +2686,85 @@ _xmas_forward(u3_xmas_pact* pac_u, u3_lane lan_u) _xmas_pact_free(pac_u); } +static c3_o +_xmas_req_pact_init(u3_xmas* sam_u, u3_xmas_pact* pac_u, u3_lane* lan_u) +{ + u3_xmas_name* nam_u = &pac_u->pag_u.nam_u; + u3_xmas_data* dat_u = &pac_u->pag_u.dat_u; + c3_o lin_o = dat_u->tot_w <= 4 ? c3y : c3n; + + + u3_pend_req* req_u = _xmas_get_request(sam_u, nam_u); + if ( NULL != req_u ) { + XMAS_LOG(INVARIANT); + u3_assert(0); + return c3n; + } else { + req_u = alloca(sizeof(u3_pend_req)); + memset(req_u, 0, sizeof(u3_pend_req)); + } + + u3_gage* gag_u = _xmas_get_lane(sam_u, nam_u->her_d, lan_u); + + if ( gag_u == NULL ) { + gag_u = alloca(sizeof(u3_gage)); + _init_gage(gag_u); + // save and re-retrieve so we have persistent pointer + _xmas_put_lane(sam_u, nam_u->her_d, lan_u, gag_u); + gag_u = _xmas_get_lane(sam_u, nam_u->her_d, lan_u); + u3_assert( gag_u != NULL ); + } + + c3_w siz_w = 1 << (pac_u->pag_u.nam_u.boq_y - 3); + u3_assert( siz_w == 1024 ); // boq_y == 13 + req_u->gag_u = gag_u; + req_u->dat_y = c3_calloc(siz_w * dat_u->tot_w); + req_u->wat_u = c3_calloc(sizeof(u3_pact_stat) * dat_u->tot_w); + req_u->tot_w = dat_u->tot_w; + _init_bitset(&req_u->was_u, dat_u->tot_w + 1); + + + req_u->pac_u = c3_calloc(sizeof(u3_xmas_pact)); + req_u->pac_u->sam_u = sam_u; + req_u->pac_u->hed_u.typ_y = PACT_PEEK; + req_u->pac_u->hed_u.pro_y = XMAS_VER; + memcpy(&req_u->pac_u->pek_u.nam_u, nam_u, sizeof(u3_xmas_name)); + req_u->pac_u->pek_u.nam_u.aut_o = c3n; + req_u->pac_u->pek_u.nam_u.nit_o = c3n; + + // TODO: handle restart + // u3_assert( nam_u->fra_w == 0 ); + + req_u->nex_w = c3y == lin_o ? 1 : 0; + req_u->len_w = (c3y == lin_o) ? 1 : 0; + uv_timer_init(u3L, &req_u->tim_u); + req_u->lef_w = 0; + req_u->old_w = 0; + req_u->ack_w = 0; + + u3_vec(c3_y[BLAKE3_OUT_LEN])* pof_u = vec_make(8); + { + blake_node* nod_u = blake_leaf_hash(dat_u->fra_y, dat_u->len_w, 0); + c3_y* lef_y = c3_calloc(BLAKE3_OUT_LEN); + make_chain_value(lef_y, nod_u); + c3_free(nod_u); + vec_append(pof_u, lef_y); + } + + for ( int i = 0; i < pac_u->pag_u.dat_u.aup_u.len_y; i++ ) { + c3_y* pof_y = c3_calloc(BLAKE3_OUT_LEN); + memcpy(pof_y, pac_u->pag_u.dat_u.aup_u.has_y[i], BLAKE3_OUT_LEN); + vec_append(pof_u, pof_y); + } + + req_u->bao_u = blake_bao_make(req_u->tot_w, pof_u); + blake_bao_verify(req_u->bao_u, dat_u->fra_y, dat_u->len_w, NULL); + + _xmas_put_request(sam_u, nam_u, req_u); + return c3y; +} + + static void @@ -2742,11 +2793,13 @@ _xmas_hear_page(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w, u3_lane lan_u) u3l_log("received forwarded peek"); } if ( new_o == c3y ) { - u3l_log("new lane is direct %c", c3y == dir_o ? 'y' : 'n'); - _log_lane(&lan_u); + //u3l_log("new lane is direct %c", c3y == dir_o ? 'y' : 'n'); + //_log_lane(&lan_u); } _xmas_put_peer(sam_u, pac_u->pag_u.nam_u.her_d, per_u); + + u3_weak hit = _xmas_get_cache(sam_u, &pac_u->pag_u.nam_u); if ( u3_none != hit ) { @@ -2754,22 +2807,21 @@ _xmas_hear_page(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w, u3_lane lan_u) return; } - u3_noun fra = u3i_bytes(pac_u->pag_u.dat_u.len_w, pac_u->pag_u.dat_u.fra_y) ; + // u3_noun fra = u3i_bytes(pac_u->pag_u.dat_u.len_w, pac_u->pag_u.dat_u.fra_y) ; /*if ( dop_o == c3n && pac_u->pag_u.nam_u.fra_w == 150) { dop_o = c3y; u3l_log("simulating dropped packet"); return; }*/ - - u3_weak res = _xmas_req_pact_done(sam_u, &pac_u->pag_u.nam_u, &pac_u->pag_u.dat_u, &lan_u); - u3l_log("done"); - - if ( u3_none != res ) { - u3l_log("finished"); - - c3_d now_d = _get_now_micros(); - u3l_log("took: %f ms", (now_d - sam_u->tim_d)/1000.0f); - return; + + if ( pac_u->pag_u.nam_u.fra_w == 0 || pac_u->pag_u.nam_u.aut_o == c3y ) { + _xmas_req_pact_init(sam_u, pac_u, &lan_u); + } else { + c3_o run_o = _xmas_req_pact_done(sam_u, &pac_u->pag_u.nam_u, &pac_u->pag_u.dat_u, &lan_u); + if ( c3n == run_o ) { + // cleanup + return; + } } u3_xmas_pact* nex_u; @@ -2785,7 +2837,7 @@ _xmas_hear_page(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w, u3_lane lan_u) for(int i = 0; i < win_w; i++) { nex_u->pek_u.nam_u.fra_w = nex_w + i; c3_w siz_w =_xmas_etch_pact(cur_y, nex_u); - if( siz_w == 0 ) { + if ( siz_w == 0 ) { u3l_log("failed to etch"); u3_assert( 0 ); } From 246ec2095fa8787f84b6110510e95e753edafd33 Mon Sep 17 00:00:00 2001 From: parallels Date: Mon, 4 Mar 2024 11:45:57 -0500 Subject: [PATCH 057/430] xmas: modularise --- pkg/vere/BUILD.bazel | 2 + pkg/vere/io/xmas.c | 1359 +++---------------------------------- pkg/vere/io/xmas/bitset.c | 147 ++++ pkg/vere/io/xmas/bitset.h | 22 + pkg/vere/io/xmas/pact.c | 953 ++++++++++++++++++++++++++ pkg/vere/io/xmas/xmas.h | 159 +++++ 6 files changed, 1394 insertions(+), 1248 deletions(-) create mode 100644 pkg/vere/io/xmas/bitset.c create mode 100644 pkg/vere/io/xmas/bitset.h create mode 100644 pkg/vere/io/xmas/pact.c create mode 100644 pkg/vere/io/xmas/xmas.h diff --git a/pkg/vere/BUILD.bazel b/pkg/vere/BUILD.bazel index e37be0b603..1ea552a24b 100644 --- a/pkg/vere/BUILD.bazel +++ b/pkg/vere/BUILD.bazel @@ -97,6 +97,8 @@ vere_library( "db/*.c", "io/*.c", "io/*.h", + "io/xmas/*.c", + "io/xmas/*.h", ], exclude = [ "main.c", diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c index 9bd0880d80..085a37d6df 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/xmas.c @@ -6,6 +6,8 @@ #include "noun.h" #include "ur.h" #include "cubic.h" +#include "xmas/xmas.h" +#include "xmas/bitset.h" #include #include #include @@ -22,12 +24,6 @@ c3_o dop_o = c3n; // #define XMAS_DEBUG c3y //#define XMAS_TEST -#define XMAS_VER 1 -#define FINE_PAGE 4096 // packets per page -#define FINE_FRAG 1024 // bytes per fragment packet -#define FINE_PATH_MAX 384 // longest allowed scry path -#define HEAD_SIZE 4 // header size in bytes -#define PACT_SIZE 1472 #define RED_TEXT "\033[0;31m" #define DEF_TEXT "\033[0m" #define REORDER_THRESH 5 @@ -80,13 +76,6 @@ typedef struct _u3_xmas_stat { #define XMAS_ROUT 2 // have route // // hop enum -#define HOP_NONE 0b0 -#define HOP_SHORT 0b1 -#define HOP_LONG 0b10 -#define HOP_MANY 0b11 - -#define XMAS_COOKIE_LEN 4 -static c3_y XMAS_COOKIE[4] = { 0x5e, 0x1d, 0xad, 0x51 }; #define SIFT_VAR(dest, src, len) dest = 0; for(int i = 0; i < len; i++ ) { dest |= ((src + i) >> (8*i)); } @@ -94,10 +83,6 @@ static c3_y XMAS_COOKIE[4] = { 0x5e, 0x1d, 0xad, 0x51 }; #define safe_dec(num) (num == 0 ? num : num - 1) #define _xmas_met3_w(a_w) ((c3_bits_word(a_w) + 0x7) >> 3) -typedef struct _u3_bitset { - c3_w len_w; - c3_y* buf_y; -} u3_bitset; @@ -125,11 +110,19 @@ typedef struct _u3_buf { c3_w fra_w; } u3_buf; +struct _u3_xmas; + +typedef struct _u3_xmas_pict { + uv_udp_send_t snd_u; + struct _u3_xmas* sam_u; + u3_xmas_pact pac_u; +} u3_xmas_pict; + typedef struct _u3_pend_req { c3_w nex_w; // number of the next fragment to be sent c3_w tot_w; // total number of fragments expected uv_timer_t tim_u; // timehandler - struct _u3_xmas_pact* pac_u; // preallocated request packet + u3_xmas_pict* pic_u; // preallocated request packet u3_pact_stat* wat_u; // ((mop @ud packet-state) lte) u3_bitset was_u; // ((mop @ud packet-state) lte) c3_y* dat_y; // ((mop @ud *) lte) @@ -172,6 +165,7 @@ typedef struct _u3_xmas { c3_d tim_d; // XX: remove } u3_xmas; + typedef struct _u3_peer { u3_peer_last las_u; // last check timestamps u3_lane dir_u; // direct lane (if any) @@ -181,146 +175,6 @@ typedef struct _u3_peer { } u3_peer; -typedef enum _u3_xmas_ptag { - PACT_RESV = 0, - PACT_PAGE = 1, - PACT_PEEK = 2, - PACT_POKE = 3, -} u3_xmas_ptag; - -typedef enum _u3_xmas_rout_tag { - ROUT_GALAXY = 0, - ROUT_OTHER = 1 -} u3_xmas_rout_tag; - -typedef enum _u3_xmas_nexh { - NEXH_NONE = 0, - NEXH_SBYT = 1, - NEXH_ONLY = 2, - NEXH_MANY = 3 -} u3_xmas_nexh; - -typedef struct _u3_xmas_name_meta { - c3_y ran_y; // rank (2 bits) - c3_y rif_y; // rift-len (2 bits) - c3_y nit_y; // initial overlay (1 bit) - c3_y tau_y; // %data (0) or %auth (1), 0 if !nit_o (1 bit) - c3_y gaf_y; // fragment number length (2bit) -} u3_xmas_name_meta; - -typedef struct _u3_xmas_name { - // u3_xmas_name_meta met_u; - c3_d her_d[2]; - c3_w rif_w; - c3_y boq_y; - c3_o nit_o; - c3_o aut_o; - c3_w fra_w; - c3_s pat_s; - c3_c* pat_c; -} u3_xmas_name; - -typedef struct _u3_xmas_data_meta { - c3_y bot_y; // total-fragments len (2 bits) - c3_y aul_y; // auth-left (message) type (2 bits) - c3_y aur_y; // auth-right (packet) type (2 bits) - c3_y men_y; // fragment length/type (2 bits) -} u3_xmas_data_meta; - -typedef enum { - AUTH_NONE = 0, - AUTH_NEXT = 1, // %1, must be two hash - AUTH_SIGN = 2, // %0, hashes are optional depending on num frag - AUTH_HMAC = 3 -} u3_xmas_auth_type; - -typedef struct _u3_xmas_data { - // u3_xmas_data_meta met_u; - c3_w tot_w; // total fragments - struct { - u3_xmas_auth_type typ_e; // none, traversal (none), sig, or hmac - union { // - c3_y sig_y[64]; // signature - c3_y mac_y[32]; // hmac - }; - } aum_u; - struct { - c3_y len_y; // number of hashes (0, 1, or 2) - c3_y has_y[2][32]; // hashes - } aup_u; - c3_w len_w; // fragment length - c3_y* fra_y; // fragment -} u3_xmas_data; - -typedef struct _u3_xmas_rout { - u3_xmas_rout_tag typ_y; // type tag - union { - c3_y imp_y; - u3_lane lan_u; - }; -} u3_xmas_rout; - -typedef struct _u3_xmas_head { - u3_xmas_nexh nex_y; // next-hop - c3_y pro_y; // protocol version - u3_xmas_ptag typ_y; // packet type - c3_y hop_y; // hopcount - c3_w mug_w; // mug checksum -} u3_xmas_head; - -// -// +$ cache -// [%rout lanes=(list lanes)] -// [%pending pacs=(list pact)] - - -typedef struct _u3_xmas_peek_pact { - u3_xmas_name nam_u; -} u3_xmas_peek_pact; - -typedef struct _u3_xmas_hop { - c3_w len_w; - c3_y* dat_y; -} u3_xmas_hop_once; - -typedef struct _u3_xmas_hop_more { - c3_w len_w; - u3_xmas_hop_once* dat_y; -} u3_xmas_hop_more; - -typedef union { - c3_y sot_u[6]; - u3_xmas_hop_once one_u; - u3_xmas_hop_more man_u; -} u3_xmas_hop; - - -typedef struct _u3_xmas_page_pact { - u3_xmas_name nam_u; - u3_xmas_data dat_u; - union { - c3_y sot_u[6]; - u3_xmas_hop_once one_u; - u3_xmas_hop_more man_u; - }; -} u3_xmas_page_pact; - -typedef struct _u3_xmas_poke_pact { - u3_xmas_name nam_u; - u3_xmas_name pay_u; - u3_xmas_data dat_u; -} u3_xmas_poke_pact; - -typedef struct _u3_xmas_pact { - u3_xmas_head hed_u; - uv_udp_send_t snd_u; - struct _u3_xmas* sam_u; - union { - u3_xmas_poke_pact pok_u; - u3_xmas_page_pact pag_u; - u3_xmas_peek_pact pek_u; - }; -} u3_xmas_pact; typedef enum _u3_xmas_ctag { CACE_WAIT = 1, @@ -362,143 +216,6 @@ get_millis() { // Convert the seconds to milliseconds by multiplying by 1000 // Convert the microseconds to milliseconds by dividing by 1000 } -static void -_log_head(u3_xmas_head* hed_u) -{ - u3l_log("-- HEADER --"); - u3l_log("next hop: %u", hed_u->nex_y); - u3l_log("protocol: %u", hed_u->pro_y); - u3l_log("packet type: %u", hed_u->typ_y); - u3l_log("mug: 0x%05x", (hed_u->mug_w & 0xfffff)); - u3l_log("hopcount: %u", hed_u->hop_y); - u3l_log(""); -} - -static void -_log_lane(u3_lane* lan_u) -{ - u3l_log("xmas: lane (%s,%u)", u3r_string(u3dc("scot", c3__if, u3i_word(lan_u->pip_w))), lan_u->por_s); -} - -static void -_log_buf(c3_y* buf_y, c3_w len_w) -{ - for( c3_w i_w = 0; i_w < len_w; i_w++ ) { - fprintf(stderr, "%02x", buf_y[i_w]); - } - fprintf(stderr, "\r\n"); -} - -static void -_log_name(u3_xmas_name* nam_u) -{ - // u3l_log("meta"); - // u3l_log("rank: %u", nam_u->met_u.ran_y); - // u3l_log("rift length: %u", nam_u->met_u.rif_y); - // u3l_log("nit: %u", nam_u->met_u.nit_y); - // u3l_log("tau: %u", nam_u->met_u.tau_y); - // u3l_log("frag num length: %u", nam_u->met_u.gaf_y); - - { - u3_noun her = u3dc("scot", c3__p, u3i_chubs(2, nam_u->her_d)); - c3_c* her_c = u3r_string(her); - u3l_log("publisher: %s", her_c); - c3_free(her_c); - u3z(her); - } - - u3l_log("rift: %u", nam_u->rif_w); - u3l_log("bloq: %u", nam_u->boq_y); - u3l_log("init: %s", (c3y == nam_u->nit_o) ? "&" : "|"); - u3l_log("auth: %s", (c3y == nam_u->aut_o) ? "&" : "|"); - u3l_log("frag: %u", nam_u->fra_w); - u3l_log("path len: %u", nam_u->pat_s); - u3l_log("path: %s", nam_u->pat_c); -} - -static void -_log_data(u3_xmas_data* dat_u) -{ - u3l_log("total fragments: %u", dat_u->tot_w); - - switch ( dat_u->aum_u.typ_e ) { - case AUTH_NONE: { - if ( dat_u->aup_u.len_y ) { - u3l_log("strange no auth"); - } - else { - u3l_log("no auth"); - } - } break; - - case AUTH_NEXT: { - if ( 2 != dat_u->aup_u.len_y ) { - u3l_log("bad merkle traversal"); - } - else { - u3l_log("merkle traversal:"); - _log_buf(dat_u->aup_u.has_y[0], 32); - _log_buf(dat_u->aup_u.has_y[1], 32); - } - } break; - - case AUTH_SIGN: { - u3l_log("signature:"); - _log_buf(dat_u->aum_u.sig_y, 64); - } break; - - case AUTH_HMAC: { - u3l_log("hmac:"); - _log_buf(dat_u->aum_u.mac_y, 32); - } break; - } - - switch ( dat_u->aum_u.typ_e ) { - case AUTH_SIGN: - case AUTH_HMAC: { - if ( !dat_u->aup_u.len_y ) { - break; - } - - if ( 4 < dat_u->tot_w ) { - u3l_log("strange inline proof"); - } - else { - u3l_log("inline proof"); - } - - for ( int i = 0; i < dat_u->aup_u.len_y; i++ ) { - _log_buf(dat_u->aup_u.has_y[i], 32); - } - } break; - - default: break; - } - - u3l_log("frag len: %u", dat_u->len_w); -} - -static void -_log_peek_pact(u3_xmas_peek_pact* pac_u) -{ - _log_name(&pac_u->nam_u); -} - -static void -_log_page_pact(u3_xmas_page_pact *pac_u) -{ - _log_name(&pac_u->nam_u); - _log_data(&pac_u->dat_u); -} - -static void -_log_poke_pact(u3_xmas_poke_pact *pac_u) -{ - _log_name(&pac_u->nam_u); - _log_name(&pac_u->pay_u); - _log_data(&pac_u->dat_u); -} - static void _log_gage(u3_gage* gag_u) @@ -514,6 +231,13 @@ _log_gage(u3_gage* gag_u) //u3l_log("algorithm: %s", gag_u->alg_c); } +static void +_log_lane(u3_lane* lan_u) +{ + u3l_log("xmas: lane (%s,%u)", u3r_string(u3dc("scot", c3__if, u3i_word(lan_u->pip_w))), lan_u->por_s); +} + + static void _log_peer(u3_peer* per_u) { if ( per_u == NULL ) { @@ -540,31 +264,6 @@ _log_pend_req(u3_pend_req* req_u) //u3l_log("timer in: %" PRIu64 " ms", uv_timer_get_due_in(&req_u->tim_u)); } -static void -_log_pact(u3_xmas_pact* pac_u) -{ - switch ( pac_u->hed_u.typ_y ) { - case PACT_PEEK: { - _log_peek_pact(&pac_u->pek_u); - } break; - - case PACT_PAGE: { - _log_page_pact(&pac_u->pag_u); - } break; - - case PACT_POKE: { - _log_poke_pact(&pac_u->pok_u); - } break; - - default: { - _log_poke_pact(&pac_u->pok_u); - break; - } - } -} - - - static c3_d _get_now_micros() { @@ -584,12 +283,6 @@ _clamp_rto(c3_d rto_d) { return c3_min(c3_max(rto_d, 200000), 25000000); } -static void -_update_hopcount(u3_xmas_head* hed_u) -{ - hed_u->hop_y = c3_max(hed_u->hop_y+1, 7); -} - static inline void _get_her(u3_xmas_pact* pac_u, c3_d* our_d) { @@ -616,82 +309,6 @@ _get_her(u3_xmas_pact* pac_u, c3_d* our_d) -static void -_init_bitset(u3_bitset* bit_u, c3_w len_w) -{ - bit_u->buf_y = c3_calloc(len_w >> 3); - bit_u->len_w = len_w; -} - -static void -_free_bitset(u3_bitset* bit_u) -{ - c3_free(bit_u->buf_y); -} - -static c3_w -_wyt_bitset(u3_bitset* bit_u) -{ - c3_w ret_w = 0; - for(int i = 0; i < (bit_u->len_w >> 3); i++ ) { - ret_w += __builtin_popcount(bit_u->buf_y[i]); - } - return ret_w; -} - -static void -_put_bitset(u3_bitset* bit_u, c3_w mem_w) -{ - if (( mem_w > bit_u->len_w )) { - u3l_log("overrun %u, %u", mem_w, bit_u->len_w); - return; - } - c3_w idx_w = mem_w >> 3; - c3_w byt_y = bit_u->buf_y[idx_w]; - c3_y rem_y = mem_w & 0x7; - c3_y mas_y = (1 << rem_y); - bit_u->buf_y[idx_w] = byt_y | mas_y; -} - -static c3_o -_has_bitset(u3_bitset* bit_u, c3_w mem_w) { - if (( mem_w > bit_u->len_w )) { - u3l_log("overrun %u, %u", mem_w, bit_u->len_w); - return c3n; - } - - u3_assert( mem_w < bit_u->len_w ); - c3_w idx_w = mem_w >> 3; - c3_y rem_y = mem_w & 0x7; - return __( (bit_u->buf_y[idx_w] >> rem_y) & 0x1); -} - -static void -_del_bitset(u3_bitset* bit_u, c3_w mem_w) -{ - u3_assert( mem_w < bit_u->len_w ); - c3_w idx_w = mem_w >> 3; - c3_w byt_y = bit_u->buf_y[idx_w]; - c3_y rem_y = mem_w & 0x7; - c3_y mas_y = ~(1 << rem_y); - bit_u->buf_y[idx_w] &= mas_y; -} - - -static void -_log_bitset(u3_bitset* bit_u) -{ - c3_w cur_w = 0; - u3l_log("logging bitset"); - while( cur_w < bit_u->len_w ) { - if ( c3y == _has_bitset(bit_u, cur_w) ) { - u3l_log("%u", cur_w); - } - cur_w++; - } - u3l_log("finished"); -} - // TODO: fix with scry into jael?? // remove rear call to enable star routing // XX: remove @@ -762,45 +379,6 @@ _ames_chub_bytes(c3_y byt_y[8]) | (c3_d)byt_y[7] << 56; } -/* _ames_ship_to_chubs(): pack [len_y] bytes into c3_d[2] -*/ -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_chub_bytes(): c3_d to c3_y[8] -** XX factor out, deduplicate with other conversions -*/ -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; -} - -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)); -} - static inline c3_s _ames_sift_short(c3_y buf_y[2]) @@ -815,305 +393,6 @@ _ames_sift_word(c3_y buf_y[4]) } -static c3_o -_xmas_sift_head(c3_y buf_y[8], u3_xmas_head* hed_u) -{ - if ( memcmp(buf_y + 4, &XMAS_COOKIE, XMAS_COOKIE_LEN) ) { - return c3n; - - } - c3_w hed_w = _ames_sift_word(buf_y); - - hed_u->nex_y = (hed_w >> 2) & 0x3; - hed_u->pro_y = (hed_w >> 4) & 0x7; - hed_u->typ_y = (hed_w >> 7) & 0x3; - hed_u->hop_y = (hed_w >> 9) & 0x7; - hed_u->mug_w = (hed_w >> 12) & 0xFFFFFF; - - assert( 1 == hed_u->pro_y ); - - return c3y; - - /*if(c3o((hed_u->typ_y == PACT_PEEK), (hed_u->typ_y == PACT_POKE))) { - hed_u->ran_y = (hed_w >> 30) & 0x3; - }*/ -} - -static c3_w -_xmas_sift_name(u3_xmas_name* nam_u, c3_y* buf_y, c3_w len_w) -{ -#ifdef XMAS_DEBUG - //u3l_log("xmas: sifting name %i", len_w); -#endif - - c3_w cur_w = 0; - u3_xmas_name_meta met_u; - - CHECK_BOUNDS(cur_w + 1); - c3_y met_y = buf_y[cur_w]; - met_u.ran_y = (met_y >> 0) & 0x3; - met_u.rif_y = (met_y >> 2) & 0x3; - met_u.nit_y = (met_y >> 4) & 0x1; - met_u.tau_y = (met_y >> 5) & 0x1; - met_u.gaf_y = (met_y >> 6) & 0x3; - cur_w += 1; - - c3_y her_y = 2 << met_u.ran_y; - CHECK_BOUNDS(cur_w + her_y) - _ames_ship_to_chubs(nam_u->her_d, her_y, buf_y + cur_w); - cur_w += her_y; - - c3_y rif_y = met_u.rif_y + 1; - nam_u->rif_w = 0; - CHECK_BOUNDS(cur_w + rif_y) - for( int i = 0; i < rif_y; i++ ) { - nam_u->rif_w |= (buf_y[cur_w] << (8*i)); - cur_w++; - } - - CHECK_BOUNDS(cur_w + 1); - nam_u->boq_y = buf_y[cur_w]; - cur_w++; - - if ( met_u.nit_y ) { - assert( !met_u.tau_y ); - // XX init packet - nam_u->fra_w = 0; - } - else { - c3_y fag_y = met_u.gaf_y + 1; - CHECK_BOUNDS(cur_w + fag_y); - for ( int i = 0; i < fag_y; i++ ) { - nam_u->fra_w |= (buf_y[cur_w] << (8*i)); - cur_w++; - } - } - - nam_u->nit_o = ( met_u.nit_y ) ? c3y : c3n; - // XX ?:(=(1 tau.c) %auth %data) - nam_u->aut_o = ( met_u.tau_y ) ? c3y : c3n; - - CHECK_BOUNDS(cur_w + 2) - nam_u->pat_s = buf_y[cur_w] - | (buf_y[cur_w + 1] << 8); - cur_w += 2; - - nam_u->pat_c = c3_calloc(nam_u->pat_s + 1); // unix string for ease of manipulation - CHECK_BOUNDS(cur_w + nam_u->pat_s); - memcpy(nam_u->pat_c, buf_y + cur_w, nam_u->pat_s); - nam_u->pat_c[nam_u->pat_s] = 0; - cur_w += nam_u->pat_s; - - return cur_w; -} - -static c3_w -_xmas_sift_data(u3_xmas_data* dat_u, c3_y* buf_y, c3_w len_w) -{ -#ifdef XMAS_DEBUG - //u3l_log("xmas: sifting data %i", len_w); -#endif - - c3_w cur_w = 0; - u3_xmas_data_meta met_u; - - CHECK_BOUNDS(cur_w + 1); - c3_y met_y = buf_y[cur_w]; - met_u.bot_y = (met_y >> 0) & 0x3; - met_u.aul_y = (met_y >> 2) & 0x3; - met_u.aur_y = (met_y >> 4) & 0x3; - met_u.men_y = (met_y >> 6) & 0x3; - cur_w += 1; - - c3_y tot_y = met_u.bot_y + 1; - CHECK_BOUNDS(cur_w + tot_y); - dat_u->tot_w = 0; - for( int i = 0; i < tot_y; i++ ) { - dat_u->tot_w |= (buf_y[cur_w] << (8*i)); - cur_w++; - } - - c3_y aum_y = ( 2 == met_u.aul_y ) ? 64 : - ( 3 == met_u.aul_y ) ? 32 : 0; - CHECK_BOUNDS(cur_w + aum_y); - memcpy(dat_u->aum_u.sig_y, buf_y + cur_w, aum_y); - cur_w += aum_y; - - dat_u->aum_u.typ_e = met_u.aul_y; // XX - - assert( 3 > met_u.aur_y ); - - CHECK_BOUNDS(cur_w + (met_u.aur_y * 32)); - dat_u->aup_u.len_y = met_u.aur_y; - for( int i = 0; i < met_u.aur_y; i++ ) { - memcpy(dat_u->aup_u.has_y[i], buf_y + cur_w, 32); - cur_w += 32; - } - - c3_y nel_y = met_u.men_y; - - if ( 3 == nel_y ) { - CHECK_BOUNDS(cur_w + 1); - nel_y = buf_y[cur_w]; - cur_w++; - } - - CHECK_BOUNDS(cur_w + nel_y); - dat_u->len_w = 0; - for ( int i = 0; i < nel_y; i++ ) { - dat_u->len_w |= (buf_y[cur_w] << (8*i)); - cur_w++; - } - - CHECK_BOUNDS(cur_w + dat_u->len_w); - dat_u->fra_y = c3_calloc(dat_u->len_w); - memcpy(dat_u->fra_y, buf_y + cur_w, dat_u->len_w); - cur_w += dat_u->len_w; - - return cur_w; -} - -static c3_w -_xmas_sift_hop_long(u3_xmas_hop_once* hop_u, c3_y* buf_y, c3_w len_w) -{ - c3_w cur_w = 0; - CHECK_BOUNDS(cur_w + 1); - hop_u->len_w = buf_y[cur_w]; - cur_w++; - CHECK_BOUNDS(cur_w + hop_u->len_w); - hop_u->dat_y = c3_calloc(hop_u->len_w); - memcpy(hop_u->dat_y, buf_y + cur_w, hop_u->len_w); - - return cur_w; -} - - -static c3_w -_xmas_sift_page_pact(u3_xmas_pact* pat_u, c3_y nex_y, c3_y* buf_y, c3_w len_w) -{ - u3_xmas_page_pact* pac_u = &pat_u->pag_u; - u3_xmas* sam_u = pat_u->sam_u; - c3_w cur_w = 0, nex_w; - - if ( !(nex_w = _xmas_sift_name(&pac_u->nam_u, buf_y + cur_w, len_w)) ) { - return 0; - } - cur_w += nex_w; - - if ( !(nex_w = _xmas_sift_data(&pac_u->dat_u, buf_y + cur_w, len_w)) ) { - return 0; - } - cur_w += nex_w; - - switch ( nex_y ) { - default: { - XMAS_LOG(SERIAL); - return 0; - } - case HOP_NONE: break; - case HOP_SHORT: { - CHECK_BOUNDS(cur_w + 6); - memcpy(pac_u->sot_u, buf_y + cur_w, 6); - cur_w += 6; - } break; - case HOP_LONG: { - c3_w hop_w = _xmas_sift_hop_long(&pac_u->one_u, buf_y + cur_w, len_w - cur_w); - if( hop_w == 0 ) { - return 0; - } - cur_w += hop_w; - } break; - case HOP_MANY: { - CHECK_BOUNDS(cur_w + 1); - pac_u->man_u.len_w = buf_y[cur_w]; - cur_w++; - - pac_u->man_u.dat_y = c3_calloc(sizeof(u3_xmas_hop_once) * pac_u->man_u.len_w); - - for( int i = 0; i < pac_u->man_u.len_w; i++ ) { - c3_w hop_w = _xmas_sift_hop_long(&pac_u->man_u.dat_y[i], buf_y + cur_w ,len_w - cur_w); - if ( hop_w == 0 ) { - return 0; - } - cur_w += hop_w; - } - } - } - - return cur_w; -} - - -static c3_w -_xmas_sift_peek_pact(u3_xmas_peek_pact* pac_u, c3_y* buf_y, c3_w len_w) -{ - c3_w siz_w = _xmas_sift_name(&pac_u->nam_u, buf_y, len_w); - if ( siz_w < len_w ) { - u3l_log("xmas: failed to consume entire packet"); - _log_buf(buf_y + siz_w, len_w - siz_w); - return 0; - } - - return siz_w; -} - -static c3_w -_xmas_sift_poke_pact(u3_xmas_poke_pact* pac_u, c3_y* buf_y, c3_w len_w) -{ - c3_w cur_w = 0, nex_w; - // ack path - if ( !(nex_w = _xmas_sift_name(&pac_u->nam_u, buf_y + cur_w, len_w)) ) { - return 0; - } - cur_w += nex_w; - - // payload path - if ( !(nex_w = _xmas_sift_name(&pac_u->pay_u, buf_y + cur_w, len_w)) ) { - return 0; - } - cur_w += nex_w; - - // payload - if ( !(nex_w = _xmas_sift_data(&pac_u->dat_u, buf_y + cur_w, len_w)) ) { - return 0; - } - cur_w += nex_w; - - return cur_w; -} - -static c3_w -_xmas_sift_pact(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w) -{ - c3_w res_w = 0; - - if ( len_w < 8 ) { - u3l_log("xmas: attempted to parse overly short packet of size %u", len_w); - } - - _xmas_sift_head(buf_y, &pac_u->hed_u); - buf_y += 8; - len_w -= 8; - - switch ( pac_u->hed_u.typ_y ) { - case PACT_PEEK: { - res_w = _xmas_sift_peek_pact(&pac_u->pek_u, buf_y, len_w); - } break; - case PACT_PAGE: { - res_w = _xmas_sift_page_pact(pac_u, pac_u->hed_u.nex_y, buf_y, len_w); - } break; - case PACT_POKE: { - res_w = _xmas_sift_poke_pact(&pac_u->pok_u, buf_y, len_w); - } break; - default: { - u3l_log("xmas: received unknown packet type"); - _log_buf(buf_y, len_w); - break; - } - } - //u3_assert(res_w <= len_w ); - return res_w + 8; -} // cut and pasted from ames.c // @@ -1143,366 +422,9 @@ _dire_etch_ud(c3_d num_d) return u3i_bytes(26 - dif_w, buf_y); // XX known-non-null } -static void -_xmas_etch_head(u3_xmas_head* hed_u, c3_y buf_y[8]) -{ -#ifdef XMAS_DEBUG - if( c3y == XMAS_DEBUG ) { - if( hed_u->pro_y > 7 ) { - u3l_log("xmas: bad protocol version"); - return; - } - } -#endif - - assert( 1 == hed_u->pro_y ); - - // c3_o req_o = c3o((hed_u->typ_y == PACT_PEEK), (hed_u->typ_y == PACT_POKE)); - // c3_y siz_y = req_o ? 5 : 7; - c3_w hed_w = (hed_u->nex_y & 0x3) << 2 - ^ (hed_u->pro_y & 0x7) << 4 // XX constant, 1 - ^ (hed_u->typ_y & 0x3) << 7 - ^ (hed_u->hop_y & 0x7) << 9 - ^ (hed_u->mug_w & 0xFFFFFF) << 12; - // XX: we don't expand hopcount if no request. Correct? - // - /*if ( c3y == req_o ) { - hed_w = hed_w ^ ((hed_u->ran_y & 0x3) << 30); - }*/ - - _ames_etch_word(buf_y, hed_w); - memcpy(buf_y + 4, XMAS_COOKIE, XMAS_COOKIE_LEN); -} -static c3_y -_xmas_rank(c3_d who_d[2]) -{ - if ( who_d[1] ) { - return 3; - } - else if ( who_d[0] >> 32 ) { - return 2; - } - else if ( who_d[0] >> 16 ) { - return 1; - } - else { - return 0; - } -} -static c3_w -_xmas_etch_name(c3_y* buf_y, u3_xmas_name* nam_u) -{ -#ifdef XMAS_DEBUG -#endif - c3_w cur_w = 0; - u3_xmas_name_meta met_u; - - met_u.ran_y = _xmas_rank(nam_u->her_d); - met_u.rif_y = safe_dec(_xmas_met3_w(nam_u->rif_w)); - - if ( c3y == nam_u->nit_o ) { - assert( c3n == nam_u->aut_o ); // XX - met_u.nit_y = 1; - met_u.tau_y = 0; - met_u.gaf_y = 0; - } - else { - met_u.nit_y = 0; - met_u.tau_y = (c3y == nam_u->aut_o) ? 1 : 0; - met_u.gaf_y = safe_dec(_xmas_met3_w(nam_u->fra_w)); - } - - c3_y met_y = (met_u.ran_y & 0x3) << 0 - ^ (met_u.rif_y & 0x3) << 2 - ^ (met_u.nit_y & 0x1) << 4 - ^ (met_u.tau_y & 0x1) << 5 - ^ (met_u.gaf_y & 0x3) << 6; - - buf_y[cur_w] = met_y; - - //ship - cur_w++; - c3_y her_y = 2 << met_u.ran_y; // XX confirm - _ames_ship_of_chubs(nam_u->her_d, her_y, buf_y + cur_w); - cur_w += her_y; - - // rift - c3_y rif_y = met_u.rif_y + 1; - for ( int i = 0; i < rif_y; i++) { - buf_y[cur_w] = (nam_u->rif_w >> (8*i)) & 0xff; - cur_w++; - } - - buf_y[cur_w] = nam_u->boq_y; - cur_w++; - - c3_y fra_y = (c3y == nam_u->nit_o) ? 0 : met_u.gaf_y + 1; - for( int i = 0; i < fra_y; i++ ) { - buf_y[cur_w] = (nam_u->fra_w >> (8*i)) & 0xff; - cur_w++; - } - - // path length - c3_y pat_y = 2; - for ( int i = 0; i < pat_y; i++ ) { - buf_y[cur_w] = (nam_u->pat_s >> (8*i)) & 0xff; - cur_w++; - } - - // path - memcpy(buf_y + cur_w, nam_u->pat_c, nam_u->pat_s); - cur_w += nam_u->pat_s; - - return cur_w; -} - -static c3_w -_xmas_etch_data(c3_y* buf_y, u3_xmas_data* dat_u) -{ -#ifdef XMAS_DEBUG - -#endif - c3_w cur_w = 0; - u3_xmas_data_meta met_u; - - met_u.bot_y = safe_dec(_xmas_met3_w(dat_u->tot_w)); - - // XX - met_u.aul_y = dat_u->aum_u.typ_e; - met_u.aur_y = dat_u->aup_u.len_y; - - c3_y nel_y = _xmas_met3_w(dat_u->len_w); - met_u.men_y = (3 >= nel_y) ? nel_y : 3; - - c3_y met_y = (met_u.bot_y & 0x3) << 0 - ^ (met_u.aul_y & 0x3) << 2 - ^ (met_u.aur_y & 0x3) << 4 - ^ (met_u.men_y & 0x3) << 6; - buf_y[cur_w] = met_y; - cur_w++; - - c3_y tot_y = met_u.bot_y + 1; - for (int i = 0; i < tot_y; i++ ) { - buf_y[cur_w] = (dat_u->tot_w >> (8 * i)) & 0xFF; - cur_w++; - } - - switch ( dat_u->aum_u.typ_e ) { - case AUTH_SIGN: { - memcpy(buf_y + cur_w, dat_u->aum_u.sig_y, 64); - cur_w += 64; - } break; - - case AUTH_HMAC: { - memcpy(buf_y + cur_w, dat_u->aum_u.mac_y, 32); - cur_w += 32; - } break; - - default: break; - } - - for ( int i = 0; i < dat_u->aup_u.len_y; i++ ) { - memcpy(buf_y + cur_w, dat_u->aup_u.has_y[i], 32); - cur_w += 32; - } - - if ( 3 == met_u.men_y ) { - buf_y[cur_w] = nel_y; - cur_w++; - } - - memcpy(buf_y + cur_w, (c3_y*)&dat_u->len_w, nel_y); - cur_w += nel_y; - - memcpy(buf_y + cur_w, dat_u->fra_y, dat_u->len_w); - cur_w += dat_u->len_w; - - return cur_w; -} - -static c3_w -_xmas_etch_page_pact(c3_y* buf_y, u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u) -{ - c3_w cur_w = 0, nex_w; - - if ( !(nex_w = _xmas_etch_name(buf_y + cur_w, &pac_u->nam_u)) ) { - return 0; - } - cur_w += nex_w; - - if ( !(nex_w = _xmas_etch_data(buf_y + cur_w, &pac_u->dat_u)) ) { - return 0; - } - cur_w += nex_w; - - // XX hops - - return cur_w; -} - -static c3_w -_xmas_etch_poke_pact(c3_y* buf_y, u3_xmas_poke_pact* pac_u, u3_xmas_head* hed_u) -{ - c3_w cur_w = 0, nex_w; - - if ( !(nex_w = _xmas_etch_name(buf_y + cur_w, &pac_u->nam_u)) ) { - return 0; - } - cur_w += nex_w; - - if ( !(nex_w = _xmas_etch_name(buf_y + cur_w, &pac_u->pay_u)) ) { - return 0; - } - cur_w += nex_w; - - if ( !(nex_w = _xmas_etch_data(buf_y + cur_w, &pac_u->dat_u)) ) { - return 0; - } - cur_w += nex_w; - - return cur_w; -} - -static c3_w -_xmas_size_name(u3_xmas_name* nam_u) -{ - c3_w siz_w = 1; - u3_xmas_name_meta met_u; - - met_u.ran_y = _xmas_rank(nam_u->her_d); - met_u.rif_y = safe_dec(_xmas_met3_w(nam_u->rif_w)); - - siz_w += 2 << met_u.ran_y; - siz_w += met_u.rif_y + 1; - siz_w++; // bloq - - if (c3n == nam_u->nit_o ) { - met_u.gaf_y = safe_dec(_xmas_met3_w(nam_u->fra_w)); - siz_w += met_u.gaf_y + 1; - } - - siz_w += 2; // path-length - siz_w += nam_u->pat_s; - - return siz_w; -} - -static c3_w -_xmas_size_data(u3_xmas_data* dat_u) -{ - c3_w siz_w = 1; - u3_xmas_data_meta met_u; - - met_u.bot_y = safe_dec(_xmas_met3_w(dat_u->tot_w)); - - siz_w += met_u.bot_y + 1; - - switch ( dat_u->aum_u.typ_e ) { - case AUTH_SIGN: { - siz_w += 64; - } break; - - case AUTH_HMAC: { - siz_w += 32; - } break; - - default: break; - } - - siz_w += 32 * dat_u->aup_u.len_y; - - c3_y nel_y = _xmas_met3_w(dat_u->len_w); - met_u.men_y = (3 >= nel_y) ? nel_y : 3; - - if ( 3 == met_u.men_y ) { - siz_w++; - } - - siz_w += nel_y; - siz_w += dat_u->len_w; - - return siz_w; -} - -static c3_w -_xmas_size_pact(u3_xmas_pact* pac_u) -{ - c3_w siz_w = 8; // header + cookie; - - switch ( pac_u->hed_u.typ_y ) { - case PACT_PEEK: { - siz_w += _xmas_size_name(&pac_u->pek_u.nam_u); - } break; - - case PACT_PAGE: { - siz_w += _xmas_size_name(&pac_u->pag_u.nam_u); - siz_w += _xmas_size_data(&pac_u->pag_u.dat_u); - // XX hops - } break; - - case PACT_POKE: { - siz_w += _xmas_size_name(&pac_u->pok_u.nam_u); - siz_w += _xmas_size_name(&pac_u->pok_u.pay_u); - siz_w += _xmas_size_data(&pac_u->pok_u.dat_u); - } break; - - default: { - u3l_log("bad pact type %u", pac_u->hed_u.typ_y);//u3m_bail(c3__bail); - return 0; - } - } - - return siz_w; -} - -static c3_w -_xmas_etch_pact(c3_y* buf_y, u3_xmas_pact* pac_u) -{ - c3_w siz_w = _xmas_size_pact(pac_u); - if ( siz_w > PACT_SIZE ) { - fprintf(stderr, "etch: would overflow %u\r\n", siz_w); - return 0; - } - - c3_w cur_w = 0, nex_w; - u3_xmas_head* hed_u = &pac_u->hed_u; - _xmas_etch_head(hed_u, buf_y + cur_w); - cur_w += 8; - - switch ( pac_u->hed_u.typ_y ) { - case PACT_POKE: { - if ( !(nex_w = _xmas_etch_poke_pact(buf_y + cur_w, &pac_u->pok_u, hed_u)) ) { - return 0; - } - } break; - - case PACT_PEEK: { - if ( !(nex_w = _xmas_etch_name(buf_y + cur_w, &pac_u->pek_u.nam_u)) ) { - return 0; - } - } break; - - case PACT_PAGE: { - if ( !(nex_w = _xmas_etch_page_pact(buf_y + cur_w, &pac_u->pag_u, hed_u)) ) { - return 0; - } - } break; - - default: { - u3l_log("bad pact type %u", pac_u->hed_u.typ_y);//u3m_bail(c3__bail); - return 0; - } - } - - cur_w += nex_w; - - assert( siz_w == cur_w ); - - return cur_w; -} /* _xmas_request_key(): produce key for request hashtable sam_u->req_p from nam_u @@ -1524,26 +446,6 @@ static void _init_gage(u3_gage* gag_u) gag_u->sst_w = 10000; } -// xx: imcomplete -static void _xmas_pact_free(u3_xmas_pact* pac_u) { - switch ( pac_u->hed_u.typ_y ) { - default: { - u3m_bail(c3__foul); - }; - case PACT_PEEK: { - break; - }; - case PACT_PAGE: { - c3_free(pac_u->pag_u.dat_u.fra_y); - break; - }; - case PACT_POKE: { - break; - }; - } - c3_free(pac_u); -} - /* u3_xmas_encode_lane(): serialize lane to noun */ @@ -1641,7 +543,7 @@ _xmas_del_request(u3_xmas* sam_u, u3_xmas_name* nam_u) { return; } u3_pend_req* req_u = u3to(u3_pend_req, req); - // _xmas_pact_free(req_u->pac_u); + // xmas_free_pact(req_u->pac_u); // _free_bitset(&req_u->was_u); // c3_free(req_u->pac_u); // c3_free(req_u->dat_y); @@ -1720,14 +622,14 @@ static void _xmas_handle_ack(u3_gage* gag_u, u3_pact_stat* pat_u) * Will not do so if returning 0 */ static c3_w -_xmas_req_get_cwnd_nex(u3_xmas* sam_u, u3_xmas_name* nam_u, c3_w* nex_w, u3_xmas_pact** nex_u) +_xmas_req_get_cwnd_nex(u3_xmas* sam_u, u3_xmas_name* nam_u, c3_w* nex_w, u3_xmas_pict** nex_u) { c3_w res_w = 0; u3_pend_req* req_u = _xmas_get_request(sam_u, nam_u); if ( NULL == req_u ) { return 0; } - *nex_u = req_u->pac_u; + *nex_u = req_u->pic_u; if ( req_u->tot_w == 0 || req_u->gag_u == NULL ) { u3l_log("shouldn't happen"); @@ -1739,7 +641,7 @@ _xmas_req_get_cwnd_nex(u3_xmas* sam_u, u3_xmas_name* nam_u, c3_w* nex_w, u3_xmas *nex_w = req_u->nex_w; - c3_w liv_w = _wyt_bitset(&req_u->was_u); + c3_w liv_w = bitset_wyt(&req_u->was_u); c3_w rem_w = req_u->tot_w - req_u->nex_w + 1; return c3_min(rem_w, req_u->gag_u->wnd_w - liv_w); @@ -1778,7 +680,7 @@ _xmas_req_pact_sent(u3_xmas* sam_u, u3_xmas_name* nam_u) } // TODO: optional assertions? req_u->wat_u[nam_u->fra_w] = (u3_pact_stat){now_d, 0, 1, 0 }; - _put_bitset(&req_u->was_u, nam_u->fra_w); + bitset_put(&req_u->was_u, nam_u->fra_w); // if ( nam_u->fra_w == 1 ) { // req_u->lef_w = 1; // } @@ -1788,9 +690,9 @@ _xmas_req_pact_sent(u3_xmas* sam_u, u3_xmas_name* nam_u) return; } - if ( req_u->lef_w != 0 && c3n == _has_bitset(&req_u->was_u, req_u->lef_w) ) { + if ( req_u->lef_w != 0 && c3n == bitset_has(&req_u->was_u, req_u->lef_w) ) { while ( req_u->lef_w++ < req_u->tot_w ) { - if ( c3y == _has_bitset(&req_u->was_u, req_u->lef_w) ) { + if ( c3y == bitset_has(&req_u->was_u, req_u->lef_w) ) { break; } } @@ -1942,7 +844,7 @@ _update_oldest_req(u3_pend_req *req_u, u3_gage* gag_u) c3_d now_d = _get_now_micros(); c3_d wen_d = now_d; for ( c3_w i = req_u->lef_w; i < req_u->nex_w; i++ ) { - if ( c3y == _has_bitset(&req_u->was_u, i) && + if ( c3y == bitset_has(&req_u->was_u, i) && wen_d > req_u->wat_u[i].sen_d ) { wen_d = req_u->wat_u[i].sen_d; @@ -1960,12 +862,12 @@ _update_oldest_req(u3_pend_req *req_u, u3_gage* gag_u) uv_timer_start(&req_u->tim_u, _xmas_packet_timeout, ((req_u->wat_u[req_u->old_w].sen_d + gag_u->rto_w) / 1000), 0); } -static void _xmas_send(u3_xmas_pact* pac_u, u3_lane* lan_u) +static void _xmas_send(u3_xmas_pict* pic_u, u3_lane* lan_u) { - u3_xmas* sam_u = pac_u->sam_u; + u3_xmas* sam_u = pic_u->sam_u; c3_y* buf_y = c3_calloc(PACT_SIZE); - c3_w siz_w = _xmas_etch_pact(buf_y, pac_u); + c3_w siz_w = xmas_etch_pact(buf_y, &pic_u->pac_u); _xmas_send_buf(sam_u, *lan_u, buf_y, siz_w); } @@ -1974,7 +876,7 @@ static void _xmas_send(u3_xmas_pact* pac_u, u3_lane* lan_u) static void _try_resend(u3_pend_req* req_u) { - u3_xmas* sam_u = req_u->pac_u->sam_u; + u3_xmas* sam_u = req_u->pic_u->sam_u; u3_lane* lan_u = &req_u->lan_u; c3_o los_o = c3n; if ( req_u->tot_w == 0 || req_u->ack_w <= REORDER_THRESH ) { @@ -1983,34 +885,34 @@ _try_resend(u3_pend_req* req_u) c3_w ack_w = req_u->ack_w - REORDER_THRESH; c3_d now_d = _get_now_micros(); for ( int i = req_u->lef_w; i < ack_w; i++ ) { - if ( c3y == _has_bitset(&req_u->was_u, i) ) { - req_u->pac_u->pek_u.nam_u.fra_w = i; + if ( c3y == bitset_has(&req_u->was_u, i) ) { + req_u->pic_u->pac_u.pek_u.nam_u.fra_w = i; if ( req_u->wat_u[i].tie_y == 1 ) { los_o = c3y; c3_y* buf_y = c3_calloc(PACT_SIZE); - req_u->pac_u->pek_u.nam_u.fra_w = i; - c3_w siz_w = _xmas_etch_pact(buf_y, req_u->pac_u); - if( siz_w == 0 ) { + req_u->pic_u->pac_u.pek_u.nam_u.fra_w = i; + c3_w siz_w = xmas_etch_pact(buf_y, &req_u->pic_u->pac_u); + if ( siz_w == 0 ) { u3l_log("failed to etch"); u3_assert( 0 ); } // TODO: better route management _xmas_send_buf(sam_u, *lan_u, buf_y, siz_w); - _xmas_req_pact_resent(sam_u, &req_u->pac_u->pek_u.nam_u); + _xmas_req_pact_resent(sam_u, &req_u->pic_u->pac_u.pek_u.nam_u); } else if ( (now_d - req_u->wat_u[i].sen_d) > req_u->gag_u->rto_w ) { los_o = c3y; c3_y* buf_y = c3_calloc(PACT_SIZE); - req_u->pac_u->pek_u.nam_u.fra_w = i; + req_u->pic_u->pac_u.pek_u.nam_u.fra_w = i; u3l_log("slow resending %u ", i); - c3_w siz_w = _xmas_etch_pact(buf_y, req_u->pac_u); + c3_w siz_w = xmas_etch_pact(buf_y, &req_u->pic_u->pac_u); if( siz_w == 0 ) { u3l_log("failed to etch"); u3_assert( 0 ); } // TODO: better route management _xmas_send_buf(sam_u, *lan_u, buf_y, siz_w); - _xmas_req_pact_resent(sam_u, &req_u->pac_u->pek_u.nam_u); + _xmas_req_pact_resent(sam_u, &req_u->pic_u->pac_u.pek_u.nam_u); } } } @@ -2101,13 +1003,13 @@ _xmas_req_pact_done(u3_xmas* sam_u, u3_xmas_name *nam_u, u3_xmas_data* dat_u, u3 } // received duplicate - if ( nam_u->fra_w != 0 && c3n == _has_bitset(&req_u->was_u, nam_u->fra_w) ) { + if ( nam_u->fra_w != 0 && c3n == bitset_has(&req_u->was_u, nam_u->fra_w) ) { XMAS_LOG(DUPE); req_u->wat_u[nam_u->fra_w].dup_y++; return c3y; } - _del_bitset(&req_u->was_u, nam_u->fra_w); + bitset_del(&req_u->was_u, nam_u->fra_w); if ( nam_u->fra_w > req_u->ack_w ) { req_u->ack_w = nam_u->fra_w; } @@ -2206,10 +1108,10 @@ _xmas_ef_send(u3_xmas* sam_u, u3_noun las, u3_noun pac) c3_w len_w = u3r_met(3, pac); c3_y* buf_y = c3_calloc(len_w); u3r_bytes(0, len_w, buf_y, pac); - u3_xmas_pact pac_u; - pac_u.sam_u = sam_u; - _xmas_sift_pact(&pac_u, buf_y, len_w); - if ( pac_u.hed_u.typ_y == PACT_PEEK ) { + u3_xmas_pict pic_u; + pic_u.sam_u = sam_u; + xmas_sift_pact(&pic_u.pac_u, buf_y, len_w); + if ( pic_u.pac_u.hed_u.typ_y == PACT_PEEK ) { sam_u->tim_d = _get_now_micros(); } @@ -2471,9 +1373,9 @@ _xmas_put_cache(u3_xmas* sam_u, u3_xmas_name* nam_u, u3_noun val) /* _xmas_czar(): add packet to queue, possibly begin DNS resolution */ static void -_xmas_czar(u3_xmas_pact* pac_u, c3_y her_y) +_xmas_czar(u3_xmas_pict* pic_u, c3_y her_y) { - u3_xmas* sam_u = pac_u->sam_u; + u3_xmas* sam_u = pic_u->sam_u; #ifdef XMAS_DEBUG if ( pac_u->hed_u.typ_y != PACT_PEEK ) { u3l_log("xmas: attempted to resolve galaxy for packet that was not peek"); @@ -2484,11 +1386,11 @@ _xmas_czar(u3_xmas_pact* pac_u, c3_y her_y) u3_czar_info* her_u = &sam_u->imp_u[her_y]; if ( her_u->pip_w == 0 ) { c3_y* buf_y = c3_calloc(PACT_SIZE); - c3_w siz_w = _xmas_etch_pact(buf_y, pac_u); + c3_w siz_w = xmas_etch_pact(buf_y, &pic_u->pac_u); her_u->pen = u3nc(u3i_bytes(siz_w, buf_y), her_u->pen); } - u3_noun pat = u3i_string(pac_u->pek_u.nam_u.pat_c); + u3_noun pat = u3i_string(pic_u->pac_u.pek_u.nam_u.pat_c); //u3_weak her = _xmas_get_peer(sam_u, her_y); @@ -2500,14 +1402,14 @@ _xmas_czar(u3_xmas_pact* pac_u, c3_y her_y) static void -_xmas_try_forward(u3_xmas_pact* pac_u, u3_noun fra, u3_noun hit) +_xmas_try_forward(u3_xmas_pict* pic_u, u3_noun fra, u3_noun hit) { u3l_log(""); u3l_log("stubbed forwarding"); } static c3_w -_xmas_respond(u3_xmas_pact* req_u, c3_y** buf_y, u3_noun hit) +_xmas_respond(u3_xmas_pict* req_u, c3_y** buf_y, u3_noun hit) { c3_w len_w = u3r_met(3, hit); @@ -2520,13 +1422,14 @@ _xmas_respond(u3_xmas_pact* req_u, c3_y** buf_y, u3_noun hit) } static void -_xmas_serve_cache_hit(u3_xmas_pact* req_u, u3_lane lan_u, u3_noun hit) +_xmas_serve_cache_hit(u3_xmas_pict* pic_u, u3_lane lan_u, u3_noun hit) { + u3_xmas_pact* req_u = &pic_u->pac_u; u3l_log("serving cache hit"); c3_d her_d[2]; memcpy(her_d, req_u->pek_u.nam_u.her_d, 2); c3_d our_d[2]; - memcpy(our_d, req_u->sam_u->pir_u->who_d, 2); + memcpy(our_d, pic_u->sam_u->pir_u->who_d, 2); if ( (her_d[0] != our_d[0]) || (her_d[1] != our_d[1]) ) { @@ -2543,8 +1446,8 @@ _xmas_serve_cache_hit(u3_xmas_pact* req_u, u3_lane lan_u, u3_noun hit) //u3l_log("first: %x", u3r_string(u3dc("scot", c3__uv, u3k(u3h(u3t(hit)))))); c3_y* buf_y; - c3_w len_w = _xmas_respond(req_u, &buf_y, hit); - _xmas_send_buf(req_u->sam_u, lan_u, buf_y, len_w); + c3_w len_w = _xmas_respond(pic_u, &buf_y, hit); + _xmas_send_buf(pic_u->sam_u, lan_u, buf_y, len_w); } // no lose needed because all nouns are moved in both // branches of the conditional @@ -2555,8 +1458,9 @@ _xmas_serve_cache_hit(u3_xmas_pact* req_u, u3_lane lan_u, u3_noun hit) static void _xmas_page_scry_cb(void* vod_p, u3_noun nun) { - u3_xmas_pact* pac_u = vod_p; - u3_xmas* sam_u = pac_u->sam_u; + u3_xmas_pict* pic_u = vod_p; + u3_xmas_pact* pac_u = &pic_u->pac_u; + u3_xmas* sam_u = pic_u->sam_u; //u3_noun pax = _xmas_path_with_fra(pac_u->pek_u.nam_u.pat_c, &fra_s); u3_weak hit = u3r_at(7, nun); @@ -2577,7 +1481,7 @@ _xmas_page_scry_cb(void* vod_p, u3_noun nun) if ( XMAS_WAIT == tag ) { c3_y* buf_y; - c3_w len_w = _xmas_respond(pac_u, &buf_y, u3k(hit)); + c3_w len_w = _xmas_respond(pic_u, &buf_y, u3k(hit)); _xmas_rout_bufs(sam_u, buf_y, len_w, u3k(u3t(dat))); } _xmas_put_cache(sam_u, &pac_u->pek_u.nam_u, u3nc(XMAS_ITEM, u3k(hit))); @@ -2650,12 +1554,14 @@ _hear_peer(u3_xmas* sam_u, u3_peer* per_u, u3_lane lan_u, c3_o dir_o) } static void -_xmas_forward(u3_xmas_pact* pac_u, u3_lane lan_u) +_xmas_forward(u3_xmas_pict* pic_u, u3_lane lan_u) { - u3_xmas* sam_u = pac_u->sam_u; + u3_xmas_pact* pac_u = &pic_u->pac_u; + u3_xmas* sam_u = pic_u->sam_u; c3_d her_d[2]; _get_her(pac_u, her_d); - _update_hopcount(&pac_u->hed_u); + // XX: revive + //_update_hopcount(&pac_u->hed_u); if ( pac_u->hed_u.typ_y == PACT_PAGE ) { //u3l_log("should update next hop"); @@ -2669,7 +1575,7 @@ _xmas_forward(u3_xmas_pact* pac_u, u3_lane lan_u) if ( tag == XMAS_WAIT ) { u3m_p("lanes", u3t(dat)); c3_y* buf_y = c3_calloc(PACT_SIZE); - c3_w len_w = _xmas_etch_pact(buf_y, pac_u); + c3_w len_w = xmas_etch_pact(buf_y, pac_u); _xmas_rout_bufs(sam_u, buf_y, len_w, u3k(u3t(dat))); } else { u3l_log("xmas: weird pending interest"); @@ -2679,16 +1585,17 @@ _xmas_forward(u3_xmas_pact* pac_u, u3_lane lan_u) if ( NULL != per_u ) { u3_lane lin_u = _xmas_get_direct_lane(sam_u, her_d); if ( lin_u.pip_w != 0 ) { - _xmas_send(pac_u, &lin_u); + _xmas_send(pic_u, &lin_u); } } } - _xmas_pact_free(pac_u); + xmas_free_pact(pac_u); } static c3_o -_xmas_req_pact_init(u3_xmas* sam_u, u3_xmas_pact* pac_u, u3_lane* lan_u) +_xmas_req_pact_init(u3_xmas* sam_u, u3_xmas_pict* pic_u, u3_lane* lan_u) { + u3_xmas_pact* pac_u = &pic_u->pac_u; u3_xmas_name* nam_u = &pac_u->pag_u.nam_u; u3_xmas_data* dat_u = &pac_u->pag_u.dat_u; c3_o lin_o = dat_u->tot_w <= 4 ? c3y : c3n; @@ -2721,16 +1628,16 @@ _xmas_req_pact_init(u3_xmas* sam_u, u3_xmas_pact* pac_u, u3_lane* lan_u) req_u->dat_y = c3_calloc(siz_w * dat_u->tot_w); req_u->wat_u = c3_calloc(sizeof(u3_pact_stat) * dat_u->tot_w); req_u->tot_w = dat_u->tot_w; - _init_bitset(&req_u->was_u, dat_u->tot_w + 1); + bitset_init(&req_u->was_u, dat_u->tot_w + 1); - req_u->pac_u = c3_calloc(sizeof(u3_xmas_pact)); - req_u->pac_u->sam_u = sam_u; - req_u->pac_u->hed_u.typ_y = PACT_PEEK; - req_u->pac_u->hed_u.pro_y = XMAS_VER; - memcpy(&req_u->pac_u->pek_u.nam_u, nam_u, sizeof(u3_xmas_name)); - req_u->pac_u->pek_u.nam_u.aut_o = c3n; - req_u->pac_u->pek_u.nam_u.nit_o = c3n; + req_u->pic_u = c3_calloc(sizeof(u3_xmas_pact)); + req_u->pic_u->sam_u = sam_u; + req_u->pic_u->pac_u.hed_u.typ_y = PACT_PEEK; + req_u->pic_u->pac_u.hed_u.pro_y = XMAS_VER; + memcpy(&req_u->pic_u->pac_u.pek_u.nam_u, nam_u, sizeof(u3_xmas_name)); + req_u->pic_u->pac_u.pek_u.nam_u.aut_o = c3n; + req_u->pic_u->pac_u.pek_u.nam_u.nit_o = c3n; // TODO: handle restart // u3_assert( nam_u->fra_w == 0 ); @@ -2768,12 +1675,13 @@ _xmas_req_pact_init(u3_xmas* sam_u, u3_xmas_pact* pac_u, u3_lane* lan_u) static void -_xmas_hear_page(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w, u3_lane lan_u) +_xmas_hear_page(u3_xmas_pict* pic_u, c3_y* buf_y, c3_w len_w, u3_lane lan_u) { #ifdef XMAS_DEBUG u3l_log("xmas hear page"); #endif - u3_xmas* sam_u = pac_u->sam_u; + u3_xmas* sam_u = pic_u->sam_u; + u3_xmas_pact* pac_u = &pic_u->pac_u; u3_noun wir = u3nc(c3__xmas, u3_nul); c3_s fra_s; @@ -2803,7 +1711,7 @@ _xmas_hear_page(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w, u3_lane lan_u) u3_weak hit = _xmas_get_cache(sam_u, &pac_u->pag_u.nam_u); if ( u3_none != hit ) { - _xmas_forward(pac_u, lan_u); + _xmas_forward(pic_u, lan_u); return; } @@ -2815,7 +1723,7 @@ _xmas_hear_page(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w, u3_lane lan_u) }*/ if ( pac_u->pag_u.nam_u.fra_w == 0 || pac_u->pag_u.nam_u.aut_o == c3y ) { - _xmas_req_pact_init(sam_u, pac_u, &lan_u); + _xmas_req_pact_init(sam_u, pic_u, &lan_u); } else { c3_o run_o = _xmas_req_pact_done(sam_u, &pac_u->pag_u.nam_u, &pac_u->pag_u.dat_u, &lan_u); if ( c3n == run_o ) { @@ -2824,7 +1732,7 @@ _xmas_hear_page(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w, u3_lane lan_u) } } - u3_xmas_pact* nex_u; + u3_xmas_pict* nex_u; c3_w nex_w; c3_w win_w = _xmas_req_get_cwnd_nex(sam_u, &pac_u->pag_u.nam_u, &nex_w, &nex_u); @@ -2835,15 +1743,15 @@ _xmas_hear_page(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w, u3_lane lan_u) c3_y* buf_y = c3_calloc(PACT_SIZE * win_w); c3_y* cur_y = buf_y; for(int i = 0; i < win_w; i++) { - nex_u->pek_u.nam_u.fra_w = nex_w + i; - c3_w siz_w =_xmas_etch_pact(cur_y, nex_u); + nex_u->pac_u.pek_u.nam_u.fra_w = nex_w + i; + c3_w siz_w = xmas_etch_pact(cur_y, &nex_u->pac_u); if ( siz_w == 0 ) { u3l_log("failed to etch"); u3_assert( 0 ); } // TODO: better route management _xmas_send_buf(sam_u, lan_u, cur_y, siz_w); - _xmas_req_pact_sent(sam_u, &nex_u->pek_u.nam_u); + _xmas_req_pact_sent(sam_u, &nex_u->pac_u.pek_u.nam_u); cur_y += siz_w; } } @@ -2863,13 +1771,14 @@ _xmas_add_lane_to_cache(u3_xmas* sam_u, u3_xmas_name* nam_u, u3_noun las, u3_lan static void -_xmas_hear_peek(u3_xmas_pact* pac_u, u3_lane lan_u) +_xmas_hear_peek(u3_xmas_pict* pic_u, u3_lane lan_u) { #ifdef XMAS_DEBUG u3l_log("xmas: hear peek"); // u3_assert(pac_u->hed_u.typ_y == PACT_PEEK); #endif - u3_xmas* sam_u = pac_u->sam_u; + u3_xmas_pact* pac_u = &pic_u->pac_u; + u3_xmas* sam_u = pic_u->sam_u; c3_d her_d[2]; her_d[0] = 0; her_d[1] = 0; @@ -2881,16 +1790,16 @@ _xmas_hear_peek(u3_xmas_pact* pac_u, u3_lane lan_u) u3_peer* per_u = _xmas_get_peer(sam_u, her_d); if ( per_u == NULL ) { u3l_log("xmas: alien forward for %s", u3r_string(u3dc("scot", c3__p, u3i_chubs(2, her_d)))); - _xmas_pact_free(pac_u); + xmas_free_pact(pac_u); return; } if ( c3y == sam_u->for_o && sam_u->pir_u->who_d[0] == per_u->imp_s ) { //#ifdef XMAS_DEBUG u3_lane lin_u = _xmas_get_direct_lane(sam_u, her_d); //#endif - _update_hopcount(&pac_u->hed_u); + //_update_hopcount(&pac_u->hed_u); u3l_log("sending peek %u", pac_u->pek_u.nam_u.fra_w); - _xmas_send(pac_u, &lin_u); + _xmas_send(pic_u, &lin_u); } } @@ -2907,13 +1816,13 @@ _xmas_hear_peek(u3_xmas_pact* pac_u, u3_lane lan_u) // u3l_log("dropping packet %u", pac_u->pek_u.nam_u.fra_w); // return; // } - c3_w len_w = _xmas_respond(pac_u, &buf_y, u3k(dat)); + c3_w len_w = _xmas_respond(pic_u, &buf_y, u3k(dat)); u3l_log("sending page %u", pac_u->pek_u.nam_u.fra_w); - _xmas_send_buf(pac_u->sam_u, lan_u, buf_y, len_w); + _xmas_send_buf(pic_u->sam_u, lan_u, buf_y, len_w); } else { u3l_log("xmas: weird case in cache, dropping"); } - _xmas_pact_free(pac_u); + xmas_free_pact(pac_u); // u3z(hit); } else { _xmas_add_lane_to_cache(sam_u, &pac_u->pek_u.nam_u, u3_nul, lan_u); // TODO: retrieve from namespace @@ -2924,7 +1833,7 @@ _xmas_hear_peek(u3_xmas_pact* pac_u, u3_lane lan_u) u3_noun bem = u3nc(u3nt(our, u3_nul, u3nc(c3__ud, 1)), sky); u3_pier_peek(sam_u->car_u.pir_u, u3_nul, u3k(u3nq(1, c3__beam, c3__xx, bem)), pac_u, _xmas_page_scry_cb); } else { - _xmas_pact_free(pac_u); + xmas_free_pact(pac_u); } } // u3z(pax); @@ -2933,9 +1842,10 @@ _xmas_hear_peek(u3_xmas_pact* pac_u, u3_lane lan_u) // xx: should inject event directly, but vane does not work // so we just hack it to get static void -_xmas_hear_poke(u3_xmas_pact* pac_u, u3_lane* lan_u) +_xmas_hear_poke(u3_xmas_pict* pic_u, u3_lane* lan_u) { - u3_xmas* sam_u = pac_u->sam_u; + u3_xmas_pact* pac_u = &pic_u->pac_u; + u3_xmas* sam_u = pic_u->sam_u; u3_peer* per_u = _xmas_get_peer(sam_u, pac_u->pok_u.pay_u.her_d); c3_o new_o = c3n; if ( NULL == per_u ) { @@ -2970,7 +1880,7 @@ _xmas_hear(u3_xmas* sam_u, u3l_log("xmas_hear"); #endif - u3_xmas_pact* pac_u; + u3_xmas_pict* pic_u; c3_w pre_w; c3_y* cur_y = hun_y; if ( HEAD_SIZE > len_w ) { @@ -2978,9 +1888,9 @@ _xmas_hear(u3_xmas* sam_u, return; } - pac_u = c3_calloc(sizeof(*pac_u)); - pac_u->sam_u = sam_u; - c3_w lin_w = _xmas_sift_pact(pac_u, hun_y, len_w); + pic_u = c3_calloc(sizeof(u3_xmas_pict)); + pic_u->sam_u = sam_u; + c3_w lin_w = xmas_sift_pact(&pic_u->pac_u, hun_y, len_w); if ( lin_w == 0 ) { #ifdef XMAS_DEBUG u3l_log("xmas: failed to parse packet"); @@ -2988,23 +1898,17 @@ _xmas_hear(u3_xmas* sam_u, #endif // TODO free everything return; - - } -#ifdef XMAS_DEBUG - //_log_peek_pact(&pac_u->pek_u); - u3l_log("xmas: sifted packet"); -#endif - switch ( pac_u->hed_u.typ_y ) { + switch ( pic_u->pac_u.hed_u.typ_y ) { case PACT_PEEK: { - _xmas_hear_peek(pac_u, *lan_u); + _xmas_hear_peek(pic_u, *lan_u); } break; case PACT_PAGE: { - _xmas_hear_page(pac_u, hun_y, len_w, *lan_u); + _xmas_hear_page(pic_u, hun_y, len_w, *lan_u); } break; default: { - _xmas_hear_poke(pac_u, lan_u); + _xmas_hear_poke(pic_u, lan_u); } break; } } @@ -3209,47 +2113,6 @@ u3_xmas_io_init(u3_pier* pir_u) #ifdef XMAS_TEST -static void -_test_bitset() -{ - u3_bitset bit_u; - _init_bitset(&bit_u, 500); - - _put_bitset(&bit_u, 5); - _put_bitset(&bit_u, 50); - _put_bitset(&bit_u, 100); - - c3_w wyt_w = _wyt_bitset(&bit_u); - if ( 3 != wyt_w ) { - u3l_log("wyt failed have %u expect %u", wyt_w, 3); - exit(1); - } - - if ( c3y == _has_bitset(&bit_u, 3) ) { - u3l_log("false positive for has_bitset"); - exit(1); - } - - if ( c3n == _has_bitset(&bit_u, 50) ) { - u3l_log("false negative for has_bitset"); - exit(1); - } - - _del_bitset(&bit_u, 50); - - if ( c3y == _has_bitset(&bit_u, 50) ) { - u3l_log("false positive for has_bitset"); - exit(1); - } - - wyt_w = _wyt_bitset(&bit_u); - - if ( 2 != wyt_w ) { - u3l_log("wyt failed have %u expect %u", wyt_w, 2); - exit(1); - } -} - #define cmp_scalar(nam, str, fmt) \ if ( hav_u->nam != ned_u->nam ) { \ fprintf(stderr, "xmas test cmp " str " differ:\r\n" \ @@ -3330,7 +2193,7 @@ static c3_i _test_pact(u3_xmas_pact* pac_u) { c3_y* buf_y = c3_calloc(PACT_SIZE); - c3_w len_w =_xmas_etch_pact(buf_y, pac_u); + c3_w len_w = xmas_etch_pact(buf_y, pac_u); c3_i ret_i = 0; c3_i bot_i = 0; c3_w sif_w; @@ -3347,7 +2210,7 @@ _test_pact(u3_xmas_pact* pac_u) ret_i = 1; goto done; } - if ( len_w != (sif_w = _xmas_sift_pact(&nex_u, buf_y, len_w)) ) { + if ( len_w != (sif_w = xmas_sift_pact(&nex_u, buf_y, len_w)) ) { fprintf(stderr, "pact: sift failed len=%u sif=%u\r\n", len_w, sif_w); _log_buf(buf_y, len_w); ret_i = 1; goto done; diff --git a/pkg/vere/io/xmas/bitset.c b/pkg/vere/io/xmas/bitset.c new file mode 100644 index 0000000000..11f09c231e --- /dev/null +++ b/pkg/vere/io/xmas/bitset.c @@ -0,0 +1,147 @@ +#include "bitset.h" +#include + + +#define u3_assert(x) \ + do { \ + if (!(x)) { \ + fflush(stderr); \ + fprintf(stderr, "\rAssertion '%s' " \ + "failed in %s:%d\r\n", \ + #x, __FILE__, __LINE__); \ + /*u3m_bail(c3__oops); */ \ + /*abort(); */ \ + } \ + } while(0) + +#define u3l_log(...) fprintf(stderr, __VA_ARGS__) +#define c3_calloc(s) ({ \ + void* rut = calloc(1,s); \ + if ( 0 == rut ) { \ + fprintf(stderr, "c3_calloc(%" PRIu64 ") failed\r\n", \ + (c3_d)s); \ + u3_assert(!"memory lost"); \ + } \ + rut;}) +#define c3_free free + + +void bitset_init(u3_bitset* bit_u, c3_w len_w) +{ + bit_u->len_w = len_w; + bit_u->buf_y = c3_calloc(bit_u->len_w >> 3); +} + +void +bitset_free(u3_bitset* bit_u) +{ + c3_free(bit_u->buf_y); +} + +c3_w +bitset_wyt(u3_bitset* bit_u) +{ + c3_w ret_w = 0; + for(int i = 0; i < (bit_u->len_w >> 3); i++ ) { + ret_w += __builtin_popcount(bit_u->buf_y[i]); + } + return ret_w; +} + +void bitset_put(u3_bitset* bit_u, c3_w mem_w) +{ + if (( mem_w > bit_u->len_w )) { + u3l_log("overrun %u, %u", mem_w, bit_u->len_w); + return; + } + c3_w idx_w = mem_w >> 3; + c3_w byt_y = bit_u->buf_y[idx_w]; + c3_y rem_y = mem_w & 0x7; + c3_y mas_y = (1 << rem_y); + bit_u->buf_y[idx_w] = byt_y | mas_y; +} + +c3_o +bitset_has(u3_bitset* bit_u, c3_w mem_w) { + if (( mem_w > bit_u->len_w )) { + u3l_log("overrun %u, %u", mem_w, bit_u->len_w); + return c3n; + } + + u3_assert( mem_w < bit_u->len_w ); + c3_w idx_w = mem_w >> 3; + c3_y rem_y = mem_w & 0x7; + return __( (bit_u->buf_y[idx_w] >> rem_y) & 0x1); +} + +void +bitset_del(u3_bitset* bit_u, c3_w mem_w) +{ + u3_assert( mem_w < bit_u->len_w ); + c3_w idx_w = mem_w >> 3; + c3_w byt_y = bit_u->buf_y[idx_w]; + c3_y rem_y = mem_w & 0x7; + c3_y mas_y = ~(1 << rem_y); + bit_u->buf_y[idx_w] &= mas_y; +} + + +static void +_log_bitset(u3_bitset* bit_u) +{ + c3_w cur_w = 0; + u3l_log("logging bitset"); + while( cur_w < bit_u->len_w ) { + if ( c3y == bitset_has(bit_u, cur_w) ) { + u3l_log("%u", cur_w); + } + cur_w++; + } + u3l_log("finished"); +} + +#ifdef BITSET_TEST +c3_w main() +{ + u3_bitset bit_u; + bitset_init(&bit_u, 500); + + bitset_put(&bit_u, 5); + bitset_put(&bit_u, 50); + bitset_put(&bit_u, 100); + + c3_w wyt_w = bitset_wyt(&bit_u); + if ( 3 != wyt_w ) { + u3l_log("wyt failed have %u expect %u", wyt_w, 3); + exit(1); + } + + if ( c3y == bitset_has(&bit_u, 3) ) { + u3l_log("false positive for has_bitset"); + exit(1); + } + + if ( c3n == bitset_has(&bit_u, 50) ) { + u3l_log("false negative for has_bitset"); + exit(1); + } + + bitset_del(&bit_u, 50); + + if ( c3y == bitset_has(&bit_u, 50) ) { + u3l_log("false positive for has_bitset"); + exit(1); + } + + wyt_w = bitset_wyt(&bit_u); + + if ( 2 != wyt_w ) { + u3l_log("wyt failed have %u expect %u", wyt_w, 2); + exit(1); + } + return 0; +} + +#endif + + diff --git a/pkg/vere/io/xmas/bitset.h b/pkg/vere/io/xmas/bitset.h new file mode 100644 index 0000000000..f2b2838c75 --- /dev/null +++ b/pkg/vere/io/xmas/bitset.h @@ -0,0 +1,22 @@ +#ifndef VERE_BITSET_H +#define VERE_BITSET_H + +#include "c3.h" + +typedef struct _u3_bitset { + c3_w len_w; + c3_y* buf_y; +} u3_bitset; + +void bitset_init(u3_bitset* bit_u, c3_w len_w); + +void bitset_free(u3_bitset* bit_u); + +c3_w bitset_wyt(u3_bitset* bit_u); + +void bitset_put(u3_bitset* bit_u, c3_w mem_w); + +c3_o bitset_has(u3_bitset* bit_u, c3_w mem_w); + +void bitset_del(u3_bitset* bit_u, c3_w mem_w); +#endif \ No newline at end of file diff --git a/pkg/vere/io/xmas/pact.c b/pkg/vere/io/xmas/pact.c new file mode 100644 index 0000000000..487b8dde8f --- /dev/null +++ b/pkg/vere/io/xmas/pact.c @@ -0,0 +1,953 @@ +#include "xmas.h" +#include + +#define SIFT_VAR(dest, src, len) dest = 0; for(int i = 0; i < len; i++ ) { dest |= ((src + i) >> (8*i)); } +#define CHECK_BOUNDS(cur) if ( len_w < cur ) { u3l_log("xmas: failed parse (%u,%u) at line %i", len_w, cur, __LINE__); return 0; } +#define safe_dec(num) (num == 0 ? num : num - 1) +#define _xmas_met3_w(a_w) ((c3_bits_word(a_w) + 0x7) >> 3) +#define u3_assert(x) \ + do { \ + if (!(x)) { \ + fflush(stderr); \ + fprintf(stderr, "\rAssertion '%s' " \ + "failed in %s:%d\r\n", \ + #x, __FILE__, __LINE__); \ + /*u3m_bail(c3__oops); */ \ + /*abort(); */ \ + } \ + } while(0) + +#define u3l_log(...) fprintf(stderr, __VA_ARGS__) +#define c3_calloc(s) ({ \ + void* rut = calloc(1,s); \ + if ( 0 == rut ) { \ + fprintf(stderr, "c3_calloc(%" PRIu64 ") failed\r\n", \ + (c3_d)s); \ + u3_assert(!"memory lost"); \ + } \ + rut;}) + + +/* Logging functions +*/ + +static void +_log_head(u3_xmas_head* hed_u) +{ + u3l_log("-- HEADER --"); + u3l_log("next hop: %u", hed_u->nex_y); + u3l_log("protocol: %u", hed_u->pro_y); + u3l_log("packet type: %u", hed_u->typ_y); + u3l_log("mug: 0x%05x", (hed_u->mug_w & 0xfffff)); + u3l_log("hopcount: %u", hed_u->hop_y); + u3l_log(""); +} + + +static void +_log_buf(c3_y* buf_y, c3_w len_w) +{ + for( c3_w i_w = 0; i_w < len_w; i_w++ ) { + fprintf(stderr, "%02x", buf_y[i_w]); + } + fprintf(stderr, "\r\n"); +} + +static void +_log_name(u3_xmas_name* nam_u) +{ + // u3l_log("meta"); + // u3l_log("rank: %u", nam_u->met_u.ran_y); + // u3l_log("rift length: %u", nam_u->met_u.rif_y); + // u3l_log("nit: %u", nam_u->met_u.nit_y); + // u3l_log("tau: %u", nam_u->met_u.tau_y); + // u3l_log("frag num length: %u", nam_u->met_u.gaf_y); + + { + // u3_noun her = u3dc("scot", c3__p, u3i_chubs(2, nam_u->her_d)); + // c3_c* her_c = u3r_string(her); + // u3l_log("publisher: %s", her_c); + // c3_free(her_c); + // u3z(her); + } + + u3l_log("rift: %u", nam_u->rif_w); + u3l_log("bloq: %u", nam_u->boq_y); + u3l_log("init: %s", (c3y == nam_u->nit_o) ? "&" : "|"); + u3l_log("auth: %s", (c3y == nam_u->aut_o) ? "&" : "|"); + u3l_log("frag: %u", nam_u->fra_w); + u3l_log("path len: %u", nam_u->pat_s); + u3l_log("path: %s", nam_u->pat_c); +} + +static void +_log_data(u3_xmas_data* dat_u) +{ + u3l_log("total fragments: %u", dat_u->tot_w); + + switch ( dat_u->aum_u.typ_e ) { + case AUTH_NONE: { + if ( dat_u->aup_u.len_y ) { + u3l_log("strange no auth"); + } + else { + u3l_log("no auth"); + } + } break; + + case AUTH_NEXT: { + if ( 2 != dat_u->aup_u.len_y ) { + u3l_log("bad merkle traversal"); + } + else { + u3l_log("merkle traversal:"); + _log_buf(dat_u->aup_u.has_y[0], 32); + _log_buf(dat_u->aup_u.has_y[1], 32); + } + } break; + + case AUTH_SIGN: { + u3l_log("signature:"); + _log_buf(dat_u->aum_u.sig_y, 64); + } break; + + case AUTH_HMAC: { + u3l_log("hmac:"); + _log_buf(dat_u->aum_u.mac_y, 32); + } break; + } + + switch ( dat_u->aum_u.typ_e ) { + case AUTH_SIGN: + case AUTH_HMAC: { + if ( !dat_u->aup_u.len_y ) { + break; + } + + if ( 4 < dat_u->tot_w ) { + u3l_log("strange inline proof"); + } + else { + u3l_log("inline proof"); + } + + for ( int i = 0; i < dat_u->aup_u.len_y; i++ ) { + _log_buf(dat_u->aup_u.has_y[i], 32); + } + } break; + + default: break; + } + + u3l_log("frag len: %u", dat_u->len_w); +} + +static void +_log_peek_pact(u3_xmas_peek_pact* pac_u) +{ + _log_name(&pac_u->nam_u); +} + +static void +_log_page_pact(u3_xmas_page_pact *pac_u) +{ + _log_name(&pac_u->nam_u); + _log_data(&pac_u->dat_u); +} + +static void +_log_poke_pact(u3_xmas_poke_pact *pac_u) +{ + _log_name(&pac_u->nam_u); + _log_name(&pac_u->pay_u); + _log_data(&pac_u->dat_u); +} + +static void +_log_pact(u3_xmas_pact* pac_u) +{ + switch ( pac_u->hed_u.typ_y ) { + case PACT_PEEK: { + _log_peek_pact(&pac_u->pek_u); + } break; + + case PACT_PAGE: { + _log_page_pact(&pac_u->pag_u); + } break; + + case PACT_POKE: { + _log_poke_pact(&pac_u->pok_u); + } break; + + default: { + _log_poke_pact(&pac_u->pok_u); + break; + } + } +} + +/* Helper utilities +*/ +static void +_update_hopcount(u3_xmas_head* hed_u) +{ + hed_u->hop_y = c3_max(hed_u->hop_y+1, 7); +} + +static c3_y +_xmas_rank(c3_d who_d[2]) +{ + if ( who_d[1] ) { + return 3; + } + else if ( who_d[0] >> 32 ) { + return 2; + } + else if ( who_d[0] >> 16 ) { + return 1; + } + else { + return 0; + } +} + +/* lifecycle +*/ +void xmas_free_pact(u3_xmas_pact* pac_u) +{ + switch ( pac_u->hed_u.typ_y ) { + default: { + break; + }; + case PACT_PEEK: { + break; + }; + case PACT_PAGE: { + c3_free(pac_u->pag_u.dat_u.fra_y); + break; + }; + case PACT_POKE: { + break; + }; + } + c3_free(pac_u); +} + +/* deserialisation +*/ + +static void +_ames_etch_word(c3_y buf_y[4], c3_w wod_w) +{ + buf_y[0] = wod_w & 0xff; + buf_y[1] = (wod_w >> 8) & 0xff; + buf_y[2] = (wod_w >> 16) & 0xff; + buf_y[3] = (wod_w >> 24) & 0xff; +} + + + +/* _ames_chub_bytes(): c3_d to c3_y[8] +** XX factor out, deduplicate with other conversions +*/ +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; +} + +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)); +} + +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; +} + +static inline c3_w +_ames_sift_word(c3_y buf_y[4]) +{ + return (buf_y[3] << 24 | buf_y[2] << 16 | buf_y[1] << 8 | buf_y[0]); +} + + +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); +} + +static c3_o +_xmas_sift_head(c3_y buf_y[8], u3_xmas_head* hed_u) +{ + if ( memcmp(buf_y + 4, &XMAS_COOKIE, XMAS_COOKIE_LEN) ) { + return c3n; + + } + c3_w hed_w = _ames_sift_word(buf_y); + + hed_u->nex_y = (hed_w >> 2) & 0x3; + hed_u->pro_y = (hed_w >> 4) & 0x7; + hed_u->typ_y = (hed_w >> 7) & 0x3; + hed_u->hop_y = (hed_w >> 9) & 0x7; + hed_u->mug_w = (hed_w >> 12) & 0xFFFFFF; + + assert( 1 == hed_u->pro_y ); + + return c3y; + + /*if(c3o((hed_u->typ_y == PACT_PEEK), (hed_u->typ_y == PACT_POKE))) { + hed_u->ran_y = (hed_w >> 30) & 0x3; + }*/ +} + +static c3_w +_xmas_sift_name(u3_xmas_name* nam_u, c3_y* buf_y, c3_w len_w) +{ +#ifdef XMAS_DEBUG + //u3l_log("xmas: sifting name %i", len_w); +#endif + + c3_w cur_w = 0; + u3_xmas_name_meta met_u; + + CHECK_BOUNDS(cur_w + 1); + c3_y met_y = buf_y[cur_w]; + met_u.ran_y = (met_y >> 0) & 0x3; + met_u.rif_y = (met_y >> 2) & 0x3; + met_u.nit_y = (met_y >> 4) & 0x1; + met_u.tau_y = (met_y >> 5) & 0x1; + met_u.gaf_y = (met_y >> 6) & 0x3; + cur_w += 1; + + c3_y her_y = 2 << met_u.ran_y; + CHECK_BOUNDS(cur_w + her_y) + _ames_ship_to_chubs(nam_u->her_d, her_y, buf_y + cur_w); + cur_w += her_y; + + c3_y rif_y = met_u.rif_y + 1; + nam_u->rif_w = 0; + CHECK_BOUNDS(cur_w + rif_y) + for( int i = 0; i < rif_y; i++ ) { + nam_u->rif_w |= (buf_y[cur_w] << (8*i)); + cur_w++; + } + + CHECK_BOUNDS(cur_w + 1); + nam_u->boq_y = buf_y[cur_w]; + cur_w++; + + if ( met_u.nit_y ) { + assert( !met_u.tau_y ); + // XX init packet + nam_u->fra_w = 0; + } + else { + c3_y fag_y = met_u.gaf_y + 1; + CHECK_BOUNDS(cur_w + fag_y); + for ( int i = 0; i < fag_y; i++ ) { + nam_u->fra_w |= (buf_y[cur_w] << (8*i)); + cur_w++; + } + } + + nam_u->nit_o = ( met_u.nit_y ) ? c3y : c3n; + // XX ?:(=(1 tau.c) %auth %data) + nam_u->aut_o = ( met_u.tau_y ) ? c3y : c3n; + + CHECK_BOUNDS(cur_w + 2) + nam_u->pat_s = buf_y[cur_w] + | (buf_y[cur_w + 1] << 8); + cur_w += 2; + + nam_u->pat_c = c3_calloc(nam_u->pat_s + 1); // unix string for ease of manipulation + CHECK_BOUNDS(cur_w + nam_u->pat_s); + memcpy(nam_u->pat_c, buf_y + cur_w, nam_u->pat_s); + nam_u->pat_c[nam_u->pat_s] = 0; + cur_w += nam_u->pat_s; + + return cur_w; +} + +static c3_w +_xmas_sift_data(u3_xmas_data* dat_u, c3_y* buf_y, c3_w len_w) +{ +#ifdef XMAS_DEBUG + //u3l_log("xmas: sifting data %i", len_w); +#endif + + c3_w cur_w = 0; + u3_xmas_data_meta met_u; + + CHECK_BOUNDS(cur_w + 1); + c3_y met_y = buf_y[cur_w]; + met_u.bot_y = (met_y >> 0) & 0x3; + met_u.aul_y = (met_y >> 2) & 0x3; + met_u.aur_y = (met_y >> 4) & 0x3; + met_u.men_y = (met_y >> 6) & 0x3; + cur_w += 1; + + c3_y tot_y = met_u.bot_y + 1; + CHECK_BOUNDS(cur_w + tot_y); + dat_u->tot_w = 0; + for( int i = 0; i < tot_y; i++ ) { + dat_u->tot_w |= (buf_y[cur_w] << (8*i)); + cur_w++; + } + + c3_y aum_y = ( 2 == met_u.aul_y ) ? 64 : + ( 3 == met_u.aul_y ) ? 32 : 0; + CHECK_BOUNDS(cur_w + aum_y); + memcpy(dat_u->aum_u.sig_y, buf_y + cur_w, aum_y); + cur_w += aum_y; + + dat_u->aum_u.typ_e = met_u.aul_y; // XX + + assert( 3 > met_u.aur_y ); + + CHECK_BOUNDS(cur_w + (met_u.aur_y * 32)); + dat_u->aup_u.len_y = met_u.aur_y; + for( int i = 0; i < met_u.aur_y; i++ ) { + memcpy(dat_u->aup_u.has_y[i], buf_y + cur_w, 32); + cur_w += 32; + } + + c3_y nel_y = met_u.men_y; + + if ( 3 == nel_y ) { + CHECK_BOUNDS(cur_w + 1); + nel_y = buf_y[cur_w]; + cur_w++; + } + + CHECK_BOUNDS(cur_w + nel_y); + dat_u->len_w = 0; + for ( int i = 0; i < nel_y; i++ ) { + dat_u->len_w |= (buf_y[cur_w] << (8*i)); + cur_w++; + } + + CHECK_BOUNDS(cur_w + dat_u->len_w); + dat_u->fra_y = c3_calloc(dat_u->len_w); + memcpy(dat_u->fra_y, buf_y + cur_w, dat_u->len_w); + cur_w += dat_u->len_w; + + return cur_w; +} + +static c3_w +_xmas_sift_hop_long(u3_xmas_hop_once* hop_u, c3_y* buf_y, c3_w len_w) +{ + c3_w cur_w = 0; + CHECK_BOUNDS(cur_w + 1); + hop_u->len_w = buf_y[cur_w]; + cur_w++; + CHECK_BOUNDS(cur_w + hop_u->len_w); + hop_u->dat_y = c3_calloc(hop_u->len_w); + memcpy(hop_u->dat_y, buf_y + cur_w, hop_u->len_w); + + return cur_w; +} + + +static c3_w +_xmas_sift_page_pact(u3_xmas_pact* pat_u, c3_y nex_y, c3_y* buf_y, c3_w len_w) +{ + u3_xmas_page_pact* pac_u = &pat_u->pag_u; + c3_w cur_w = 0, nex_w; + + if ( !(nex_w = _xmas_sift_name(&pac_u->nam_u, buf_y + cur_w, len_w)) ) { + return 0; + } + cur_w += nex_w; + + if ( !(nex_w = _xmas_sift_data(&pac_u->dat_u, buf_y + cur_w, len_w)) ) { + return 0; + } + cur_w += nex_w; + + switch ( nex_y ) { + default: { + return 0; + } + case HOP_NONE: break; + case HOP_SHORT: { + CHECK_BOUNDS(cur_w + 6); + memcpy(pac_u->sot_u, buf_y + cur_w, 6); + cur_w += 6; + } break; + case HOP_LONG: { + c3_w hop_w = _xmas_sift_hop_long(&pac_u->one_u, buf_y + cur_w, len_w - cur_w); + if( hop_w == 0 ) { + return 0; + } + cur_w += hop_w; + } break; + case HOP_MANY: { + CHECK_BOUNDS(cur_w + 1); + pac_u->man_u.len_w = buf_y[cur_w]; + cur_w++; + + pac_u->man_u.dat_y = c3_calloc(sizeof(u3_xmas_hop_once) * pac_u->man_u.len_w); + + for( int i = 0; i < pac_u->man_u.len_w; i++ ) { + c3_w hop_w = _xmas_sift_hop_long(&pac_u->man_u.dat_y[i], buf_y + cur_w ,len_w - cur_w); + if ( hop_w == 0 ) { + return 0; + } + cur_w += hop_w; + } + } + } + + return cur_w; +} + + +static c3_w +_xmas_sift_peek_pact(u3_xmas_peek_pact* pac_u, c3_y* buf_y, c3_w len_w) +{ + c3_w siz_w = _xmas_sift_name(&pac_u->nam_u, buf_y, len_w); + if ( siz_w < len_w ) { + u3l_log("xmas: failed to consume entire packet"); + _log_buf(buf_y + siz_w, len_w - siz_w); + return 0; + } + + return siz_w; +} + +static c3_w +_xmas_sift_poke_pact(u3_xmas_poke_pact* pac_u, c3_y* buf_y, c3_w len_w) +{ + c3_w cur_w = 0, nex_w; + // ack path + if ( !(nex_w = _xmas_sift_name(&pac_u->nam_u, buf_y + cur_w, len_w)) ) { + return 0; + } + cur_w += nex_w; + + // payload path + if ( !(nex_w = _xmas_sift_name(&pac_u->pay_u, buf_y + cur_w, len_w)) ) { + return 0; + } + cur_w += nex_w; + + // payload + if ( !(nex_w = _xmas_sift_data(&pac_u->dat_u, buf_y + cur_w, len_w)) ) { + return 0; + } + cur_w += nex_w; + + return cur_w; +} + +c3_w +xmas_sift_pact(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w) +{ + c3_w res_w = 0; + + if ( len_w < 8 ) { + u3l_log("xmas: attempted to parse overly short packet of size %u", len_w); + } + + _xmas_sift_head(buf_y, &pac_u->hed_u); + buf_y += 8; + len_w -= 8; + + switch ( pac_u->hed_u.typ_y ) { + case PACT_PEEK: { + res_w = _xmas_sift_peek_pact(&pac_u->pek_u, buf_y, len_w); + } break; + case PACT_PAGE: { + res_w = _xmas_sift_page_pact(pac_u, pac_u->hed_u.nex_y, buf_y, len_w); + } break; + case PACT_POKE: { + res_w = _xmas_sift_poke_pact(&pac_u->pok_u, buf_y, len_w); + } break; + default: { + u3l_log("xmas: received unknown packet type"); + _log_buf(buf_y, len_w); + break; + } + } + //u3_assert(res_w <= len_w ); + return res_w + 8; +} + +/* serialisation +*/ +static void +_xmas_etch_head(u3_xmas_head* hed_u, c3_y buf_y[8]) +{ +#ifdef XMAS_DEBUG + if( c3y == XMAS_DEBUG ) { + if( hed_u->pro_y > 7 ) { + u3l_log("xmas: bad protocol version"); + return; + } + } +#endif + + assert( 1 == hed_u->pro_y ); + + // c3_o req_o = c3o((hed_u->typ_y == PACT_PEEK), (hed_u->typ_y == PACT_POKE)); + // c3_y siz_y = req_o ? 5 : 7; + c3_w hed_w = (hed_u->nex_y & 0x3) << 2 + ^ (hed_u->pro_y & 0x7) << 4 // XX constant, 1 + ^ (hed_u->typ_y & 0x3) << 7 + ^ (hed_u->hop_y & 0x7) << 9 + ^ (hed_u->mug_w & 0xFFFFFF) << 12; + // XX: we don't expand hopcount if no request. Correct? + // + /*if ( c3y == req_o ) { + hed_w = hed_w ^ ((hed_u->ran_y & 0x3) << 30); + }*/ + + _ames_etch_word(buf_y, hed_w); + memcpy(buf_y + 4, XMAS_COOKIE, XMAS_COOKIE_LEN); +} + +static c3_w +_xmas_etch_name(c3_y* buf_y, u3_xmas_name* nam_u) +{ +#ifdef XMAS_DEBUG + +#endif + c3_w cur_w = 0; + u3_xmas_name_meta met_u; + + met_u.ran_y = _xmas_rank(nam_u->her_d); + met_u.rif_y = safe_dec(_xmas_met3_w(nam_u->rif_w)); + + if ( c3y == nam_u->nit_o ) { + assert( c3n == nam_u->aut_o ); // XX + met_u.nit_y = 1; + met_u.tau_y = 0; + met_u.gaf_y = 0; + } + else { + met_u.nit_y = 0; + met_u.tau_y = (c3y == nam_u->aut_o) ? 1 : 0; + met_u.gaf_y = safe_dec(_xmas_met3_w(nam_u->fra_w)); + } + + c3_y met_y = (met_u.ran_y & 0x3) << 0 + ^ (met_u.rif_y & 0x3) << 2 + ^ (met_u.nit_y & 0x1) << 4 + ^ (met_u.tau_y & 0x1) << 5 + ^ (met_u.gaf_y & 0x3) << 6; + + buf_y[cur_w] = met_y; + + //ship + cur_w++; + c3_y her_y = 2 << met_u.ran_y; // XX confirm + _ames_ship_of_chubs(nam_u->her_d, her_y, buf_y + cur_w); + cur_w += her_y; + + // rift + c3_y rif_y = met_u.rif_y + 1; + for ( int i = 0; i < rif_y; i++) { + buf_y[cur_w] = (nam_u->rif_w >> (8*i)) & 0xff; + cur_w++; + } + + buf_y[cur_w] = nam_u->boq_y; + cur_w++; + + c3_y fra_y = (c3y == nam_u->nit_o) ? 0 : met_u.gaf_y + 1; + for( int i = 0; i < fra_y; i++ ) { + buf_y[cur_w] = (nam_u->fra_w >> (8*i)) & 0xff; + cur_w++; + } + + // path length + c3_y pat_y = 2; + for ( int i = 0; i < pat_y; i++ ) { + buf_y[cur_w] = (nam_u->pat_s >> (8*i)) & 0xff; + cur_w++; + } + + // path + memcpy(buf_y + cur_w, nam_u->pat_c, nam_u->pat_s); + cur_w += nam_u->pat_s; + + return cur_w; +} + +static c3_w +_xmas_etch_data(c3_y* buf_y, u3_xmas_data* dat_u) +{ +#ifdef XMAS_DEBUG + +#endif + c3_w cur_w = 0; + u3_xmas_data_meta met_u; + + met_u.bot_y = safe_dec(_xmas_met3_w(dat_u->tot_w)); + + // XX + met_u.aul_y = dat_u->aum_u.typ_e; + met_u.aur_y = dat_u->aup_u.len_y; + + c3_y nel_y = _xmas_met3_w(dat_u->len_w); + met_u.men_y = (3 >= nel_y) ? nel_y : 3; + + c3_y met_y = (met_u.bot_y & 0x3) << 0 + ^ (met_u.aul_y & 0x3) << 2 + ^ (met_u.aur_y & 0x3) << 4 + ^ (met_u.men_y & 0x3) << 6; + buf_y[cur_w] = met_y; + cur_w++; + + c3_y tot_y = met_u.bot_y + 1; + for (int i = 0; i < tot_y; i++ ) { + buf_y[cur_w] = (dat_u->tot_w >> (8 * i)) & 0xFF; + cur_w++; + } + + switch ( dat_u->aum_u.typ_e ) { + case AUTH_SIGN: { + memcpy(buf_y + cur_w, dat_u->aum_u.sig_y, 64); + cur_w += 64; + } break; + + case AUTH_HMAC: { + memcpy(buf_y + cur_w, dat_u->aum_u.mac_y, 32); + cur_w += 32; + } break; + + default: break; + } + + for ( int i = 0; i < dat_u->aup_u.len_y; i++ ) { + memcpy(buf_y + cur_w, dat_u->aup_u.has_y[i], 32); + cur_w += 32; + } + + if ( 3 == met_u.men_y ) { + buf_y[cur_w] = nel_y; + cur_w++; + } + + memcpy(buf_y + cur_w, (c3_y*)&dat_u->len_w, nel_y); + cur_w += nel_y; + + memcpy(buf_y + cur_w, dat_u->fra_y, dat_u->len_w); + cur_w += dat_u->len_w; + + return cur_w; +} + +static c3_w +_xmas_etch_page_pact(c3_y* buf_y, u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u) +{ + c3_w cur_w = 0, nex_w; + + if ( !(nex_w = _xmas_etch_name(buf_y + cur_w, &pac_u->nam_u)) ) { + return 0; + } + cur_w += nex_w; + + if ( !(nex_w = _xmas_etch_data(buf_y + cur_w, &pac_u->dat_u)) ) { + return 0; + } + cur_w += nex_w; + + // XX hops + + return cur_w; +} + +static c3_w +_xmas_etch_poke_pact(c3_y* buf_y, u3_xmas_poke_pact* pac_u, u3_xmas_head* hed_u) +{ + c3_w cur_w = 0, nex_w; + + if ( !(nex_w = _xmas_etch_name(buf_y + cur_w, &pac_u->nam_u)) ) { + return 0; + } + cur_w += nex_w; + + if ( !(nex_w = _xmas_etch_name(buf_y + cur_w, &pac_u->pay_u)) ) { + return 0; + } + cur_w += nex_w; + + if ( !(nex_w = _xmas_etch_data(buf_y + cur_w, &pac_u->dat_u)) ) { + return 0; + } + cur_w += nex_w; + + return cur_w; +} + + + +/* sizing +*/ +static c3_w +_xmas_size_name(u3_xmas_name* nam_u) +{ + c3_w siz_w = 1; + u3_xmas_name_meta met_u; + + met_u.ran_y = _xmas_rank(nam_u->her_d); + met_u.rif_y = safe_dec(_xmas_met3_w(nam_u->rif_w)); + + siz_w += 2 << met_u.ran_y; + siz_w += met_u.rif_y + 1; + siz_w++; // bloq + + if (c3n == nam_u->nit_o ) { + met_u.gaf_y = safe_dec(_xmas_met3_w(nam_u->fra_w)); + siz_w += met_u.gaf_y + 1; + } + + siz_w += 2; // path-length + siz_w += nam_u->pat_s; + + return siz_w; +} + +static c3_w +_xmas_size_data(u3_xmas_data* dat_u) +{ + c3_w siz_w = 1; + u3_xmas_data_meta met_u; + + met_u.bot_y = safe_dec(_xmas_met3_w(dat_u->tot_w)); + + siz_w += met_u.bot_y + 1; + + switch ( dat_u->aum_u.typ_e ) { + case AUTH_SIGN: { + siz_w += 64; + } break; + + case AUTH_HMAC: { + siz_w += 32; + } break; + + default: break; + } + + siz_w += 32 * dat_u->aup_u.len_y; + + c3_y nel_y = _xmas_met3_w(dat_u->len_w); + met_u.men_y = (3 >= nel_y) ? nel_y : 3; + + if ( 3 == met_u.men_y ) { + siz_w++; + } + + siz_w += nel_y; + siz_w += dat_u->len_w; + + return siz_w; +} + +static c3_w +_xmas_size_pact(u3_xmas_pact* pac_u) +{ + c3_w siz_w = 8; // header + cookie; + + switch ( pac_u->hed_u.typ_y ) { + case PACT_PEEK: { + siz_w += _xmas_size_name(&pac_u->pek_u.nam_u); + } break; + + case PACT_PAGE: { + siz_w += _xmas_size_name(&pac_u->pag_u.nam_u); + siz_w += _xmas_size_data(&pac_u->pag_u.dat_u); + // XX hops + } break; + + case PACT_POKE: { + siz_w += _xmas_size_name(&pac_u->pok_u.nam_u); + siz_w += _xmas_size_name(&pac_u->pok_u.pay_u); + siz_w += _xmas_size_data(&pac_u->pok_u.dat_u); + } break; + + default: { + u3l_log("bad pact type %u", pac_u->hed_u.typ_y);//u3m_bail(c3__bail); + return 0; + } + } + + return siz_w; +} + +c3_w +xmas_etch_pact(c3_y* buf_y, u3_xmas_pact* pac_u) +{ + c3_w siz_w = _xmas_size_pact(pac_u); + if ( siz_w > PACT_SIZE ) { + fprintf(stderr, "etch: would overflow %u\r\n", siz_w); + return 0; + } + + c3_w cur_w = 0, nex_w; + u3_xmas_head* hed_u = &pac_u->hed_u; + _xmas_etch_head(hed_u, buf_y + cur_w); + cur_w += 8; + + switch ( pac_u->hed_u.typ_y ) { + case PACT_POKE: { + if ( !(nex_w = _xmas_etch_poke_pact(buf_y + cur_w, &pac_u->pok_u, hed_u)) ) { + return 0; + } + } break; + + case PACT_PEEK: { + if ( !(nex_w = _xmas_etch_name(buf_y + cur_w, &pac_u->pek_u.nam_u)) ) { + return 0; + } + } break; + + case PACT_PAGE: { + if ( !(nex_w = _xmas_etch_page_pact(buf_y + cur_w, &pac_u->pag_u, hed_u)) ) { + return 0; + } + } break; + + default: { + u3l_log("bad pact type %u", pac_u->hed_u.typ_y);//u3m_bail(c3__bail); + return 0; + } + } + + cur_w += nex_w; + + assert( siz_w == cur_w ); + + return cur_w; +} \ No newline at end of file diff --git a/pkg/vere/io/xmas/xmas.h b/pkg/vere/io/xmas/xmas.h new file mode 100644 index 0000000000..ae3290f1b2 --- /dev/null +++ b/pkg/vere/io/xmas/xmas.h @@ -0,0 +1,159 @@ +#ifndef VERE_XMAS_H +#define VERE_XMAS_H + +#include "c3.h" + +#define XMAS_VER 1 +#define FINE_PAGE 4096 // packets per page +#define FINE_FRAG 1024 // bytes per fragment packet +#define FINE_PATH_MAX 384 // longest allowed scry path +#define HEAD_SIZE 4 // header size in bytes +#define PACT_SIZE 1472 + +#define HOP_NONE 0b0 +#define HOP_SHORT 0b1 +#define HOP_LONG 0b10 +#define HOP_MANY 0b11 + +#define XMAS_COOKIE_LEN 4 +static c3_y XMAS_COOKIE[4] = { 0x5e, 0x1d, 0xad, 0x51 }; + + +typedef enum _u3_xmas_ptag { + PACT_RESV = 0, + PACT_PAGE = 1, + PACT_PEEK = 2, + PACT_POKE = 3, +} u3_xmas_ptag; + +typedef enum _u3_xmas_rout_tag { + ROUT_GALAXY = 0, + ROUT_OTHER = 1 +} u3_xmas_rout_tag; + +typedef enum _u3_xmas_nexh { + NEXH_NONE = 0, + NEXH_SBYT = 1, + NEXH_ONLY = 2, + NEXH_MANY = 3 +} u3_xmas_nexh; + +typedef struct _u3_xmas_name_meta { + c3_y ran_y; // rank (2 bits) + c3_y rif_y; // rift-len (2 bits) + c3_y nit_y; // initial overlay (1 bit) + c3_y tau_y; // %data (0) or %auth (1), 0 if !nit_o (1 bit) + c3_y gaf_y; // fragment number length (2bit) +} u3_xmas_name_meta; + +typedef struct _u3_xmas_name { + // u3_xmas_name_meta met_u; + c3_d her_d[2]; + c3_w rif_w; + c3_y boq_y; + c3_o nit_o; + c3_o aut_o; + c3_w fra_w; + c3_s pat_s; + c3_c* pat_c; +} u3_xmas_name; + +typedef struct _u3_xmas_data_meta { + c3_y bot_y; // total-fragments len (2 bits) + c3_y aul_y; // auth-left (message) type (2 bits) + c3_y aur_y; // auth-right (packet) type (2 bits) + c3_y men_y; // fragment length/type (2 bits) +} u3_xmas_data_meta; + +typedef enum { + AUTH_NONE = 0, + AUTH_NEXT = 1, // %1, must be two hash + AUTH_SIGN = 2, // %0, hashes are optional depending on num frag + AUTH_HMAC = 3 +} u3_xmas_auth_type; + +typedef struct _u3_xmas_data { + // u3_xmas_data_meta met_u; + c3_w tot_w; // total fragments + struct { + u3_xmas_auth_type typ_e; // none, traversal (none), sig, or hmac + union { // + c3_y sig_y[64]; // signature + c3_y mac_y[32]; // hmac + }; + } aum_u; + struct { + c3_y len_y; // number of hashes (0, 1, or 2) + c3_y has_y[2][32]; // hashes + } aup_u; + c3_w len_w; // fragment length + c3_y* fra_y; // fragment +} u3_xmas_data; + + +typedef struct _u3_xmas_head { + u3_xmas_nexh nex_y; // next-hop + c3_y pro_y; // protocol version + u3_xmas_ptag typ_y; // packet type + c3_y hop_y; // hopcount + c3_w mug_w; // mug checksum +} u3_xmas_head; + +// +// +$ cache +// [%rout lanes=(list lanes)] +// [%pending pacs=(list pact)] + + +typedef struct _u3_xmas_peek_pact { + u3_xmas_name nam_u; +} u3_xmas_peek_pact; + +typedef struct _u3_xmas_hop { + c3_w len_w; + c3_y* dat_y; +} u3_xmas_hop_once; + +typedef struct _u3_xmas_hop_more { + c3_w len_w; + u3_xmas_hop_once* dat_y; +} u3_xmas_hop_more; + +typedef union { + c3_y sot_u[6]; + u3_xmas_hop_once one_u; + u3_xmas_hop_more man_u; +} u3_xmas_hop; + + +typedef struct _u3_xmas_page_pact { + u3_xmas_name nam_u; + u3_xmas_data dat_u; + union { + c3_y sot_u[6]; + u3_xmas_hop_once one_u; + u3_xmas_hop_more man_u; + }; +} u3_xmas_page_pact; + +typedef struct _u3_xmas_poke_pact { + u3_xmas_name nam_u; + u3_xmas_name pay_u; + u3_xmas_data dat_u; +} u3_xmas_poke_pact; + +typedef struct _u3_xmas_pact { + u3_xmas_head hed_u; + union { + u3_xmas_poke_pact pok_u; + u3_xmas_page_pact pag_u; + u3_xmas_peek_pact pek_u; + }; +} u3_xmas_pact; + +c3_w xmas_sift_pact(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w); +c3_w xmas_etch_pact(c3_y* buf_y, u3_xmas_pact* pac_u); + +void xmas_free_pact(u3_xmas_pact* pac_u); + +#endif \ No newline at end of file From 8939bd5a3ff02a8901fdd6aabbcf4e8b8ffd42e8 Mon Sep 17 00:00:00 2001 From: parallels Date: Mon, 4 Mar 2024 14:55:14 -0500 Subject: [PATCH 058/430] xmas: memory fixes --- pkg/vere/BUILD.bazel | 12 + pkg/vere/io/blake.h | 12 +- pkg/vere/io/xmas.c | 860 +++++++++------------------------------- pkg/vere/io/xmas/pact.c | 536 ++++++++++++++++++++++++- pkg/vere/io/xmas/xmas.h | 2 + 5 files changed, 744 insertions(+), 678 deletions(-) diff --git a/pkg/vere/BUILD.bazel b/pkg/vere/BUILD.bazel index 1ea552a24b..5db129b8f9 100644 --- a/pkg/vere/BUILD.bazel +++ b/pkg/vere/BUILD.bazel @@ -224,6 +224,18 @@ cc_test( defines = ["XMAS_TEST"] ) +cc_test( + name = "pact_tests", + timeout = "short", + srcs = ["io/xmas/pact.c"], + features = select({ + "@platforms//os:linux": ["fully_static_link"], + "//conditions:default": [], + }), + deps = [":vere"], + defines = ["PACT_TEST"] +) + cc_test( name = "boot_tests", diff --git a/pkg/vere/io/blake.h b/pkg/vere/io/blake.h index ce1c0060ab..bb53683d18 100644 --- a/pkg/vere/io/blake.h +++ b/pkg/vere/io/blake.h @@ -30,8 +30,6 @@ typedef struct _u3_raw_vec { void** vod_p; } u3_raw_vec; - - typedef struct _blake_node { c3_y cev_y[BLAKE3_OUT_LEN]; c3_y boq_y[BLAKE3_BLOCK_LEN]; @@ -46,11 +44,11 @@ typedef struct _blake_subtree { } blake_subtree; typedef struct _blake_bao { - c3_w num_w; - c3_w con_w; - blake_subtree sub_u; - u3_vec(c3_y[BLAKE3_OUT_LEN]) que_u; - u3_vec(c3_w[BLAKE3_OUT_LEN]) sta_u; + c3_w num_w; // number of leaves == num of packets + c3_w con_w; // next packet to be ingested + blake_subtree sub_u; // internal cursor + u3_vec(c3_y[BLAKE3_OUT_LEN]) que_u; // internal + u3_vec(c3_w[BLAKE3_OUT_LEN]) sta_u; // interal } blake_bao; void* vec_popf(u3_raw_vec*); diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c index 085a37d6df..8ff3583972 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/xmas.c @@ -22,7 +22,7 @@ c3_o dop_o = c3n; -// #define XMAS_DEBUG c3y +#define XMAS_DEBUG c3y //#define XMAS_TEST #define RED_TEXT "\033[0;31m" #define DEF_TEXT "\033[0m" @@ -138,8 +138,11 @@ typedef struct _u3_pend_req { typedef struct _u3_czar_info { c3_w pip_w; // IP of galaxy + c3_y imp_y; // galaxy number + struct _u3_xmas* sam_u; // backpointer time_t tim_t; // time of retrieval - u3_noun pen; // (list @) of pending packet + c3_c* dns_c; // domain + u3_noun pen; // (list @) of pending packet } u3_czar_info; /* _u3_xmas: next generation networking @@ -251,6 +254,20 @@ static void _log_peer(u3_peer* per_u) u3l_log("galaxy: %s", u3r_string(u3dc("scot", 'p', per_u->imp_s))); } +static void +_log_czar_info(u3_czar_info* zar_u) +{ + { + u3_noun nam = u3dc("scot", 'p', zar_u->imp_y); + u3l_log("czar: %s", u3r_string(nam)); + u3_noun pip = u3dc("scot", c3__if, zar_u->pip_w); + u3l_log("IP: %s", u3r_string(pip)); + u3l_log("time: %u", zar_u->tim_t); + u3l_log("dns: %s", zar_u->dns_c); + u3l_log("pending: %u", u3r_word(0, u3do("lent", zar_u->pen))); + } +} + static void _log_pend_req(u3_pend_req* req_u) { @@ -306,18 +323,22 @@ _get_her(u3_xmas_pact* pac_u, c3_d* our_d) } } +static c3_c* +_xmas_czar_dns(c3_y imp_y, c3_c* zar_c) +{ + u3_noun nam = u3dc("scot", 'p', imp_y); + c3_c* nam_c = u3r_string(nam); + c3_w len_w = 3 + strlen(nam_c) + strlen(zar_c); + u3_assert(len_w <= 256); + c3_c* dns_c = c3_calloc(len_w); + c3_i sas_i = snprintf(dns_c, len_w, "%s.%s.", nam_c + 1, zar_c); + u3_assert(sas_i <= 255); + c3_free(nam_c); + u3z(nam); -// TODO: fix with scry into jael?? -// remove rear call to enable star routing -// XX: remove -static u3_noun -_xmas_get_sponsor_list(c3_d her_d[2]) -{ - u3_noun her = u3i_chubs(2, her_d); - u3_noun lis = u3do("rear", u3do("^saxo:title", her)); - return lis; + return dns_c; } @@ -340,8 +361,8 @@ _xmas_encode_path(c3_w len_w, c3_y* buf_y) while ( len_w-- ) { car_y = *buf_y++; if ( len_w == 0 ) { - buf_y++; - car_y = 47; + buf_y++; + car_y = 47; } if ( 47 == car_y ) { @@ -363,55 +384,6 @@ _xmas_encode_path(c3_w len_w, c3_y* buf_y) return pro; } -/* _ames_chub_bytes(): c3_y[8] to c3_d -** XX factor out, deduplicate with other conversions -*/ -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; -} - - -static inline c3_s -_ames_sift_short(c3_y buf_y[2]) -{ - return (buf_y[1] << 8 | buf_y[0]); -} - -static inline c3_w -_ames_sift_word(c3_y buf_y[4]) -{ - return (buf_y[3] << 24 | buf_y[2] << 16 | buf_y[1] << 8 | buf_y[0]); -} - - - -// cut and pasted from ames.c -// -static void -_ames_etch_short(c3_y buf_y[2], c3_s sot_s) -{ - buf_y[0] = sot_s & 0xff; - buf_y[1] = (sot_s >> 8) & 0xff; -} - -static void -_ames_etch_word(c3_y buf_y[4], c3_w wod_w) -{ - buf_y[0] = wod_w & 0xff; - buf_y[1] = (wod_w >> 8) & 0xff; - buf_y[2] = (wod_w >> 16) & 0xff; - buf_y[3] = (wod_w >> 24) & 0xff; -} - static u3_atom _dire_etch_ud(c3_d num_d) @@ -422,11 +394,6 @@ _dire_etch_ud(c3_d num_d) return u3i_bytes(26 - dif_w, buf_y); // XX known-non-null } - - - - - /* _xmas_request_key(): produce key for request hashtable sam_u->req_p from nam_u */ u3_noun _xmas_request_key(u3_xmas_name* nam_u) @@ -525,9 +492,6 @@ _xmas_get_request(u3_xmas* sam_u, u3_xmas_name* nam_u) { if ( res != u3_none && res != u3_nul ) { ret_u = u3to(u3_pend_req, res); } - if ( ret_u != NULL ) { - ret_u = NULL; - } u3z(key); return ret_u; @@ -627,6 +591,7 @@ _xmas_req_get_cwnd_nex(u3_xmas* sam_u, u3_xmas_name* nam_u, c3_w* nex_w, u3_xmas c3_w res_w = 0; u3_pend_req* req_u = _xmas_get_request(sam_u, nam_u); if ( NULL == req_u ) { + u3l_log("no request"); return 0; } *nex_u = req_u->pic_u; @@ -775,11 +740,10 @@ static u3_noun _xmas_get_now() { - - static void _xmas_send_cb(uv_udp_send_t* req_u, c3_i sas_i) { + u3l_log("sent"); u3_seal* sel_u = (u3_seal*)req_u; u3_xmas* sam_u = sel_u->sam_u; @@ -791,7 +755,7 @@ _xmas_send_cb(uv_udp_send_t* req_u, c3_i sas_i) //sam_u->fig_u.net_o = c3y; } - _xmas_free_seal(sel_u); + //_xmas_free_seal(sel_u); } static void _xmas_send_buf(u3_xmas* sam_u, u3_lane lan_u, c3_y* buf_y, c3_w len_w) @@ -854,7 +818,6 @@ _update_oldest_req(u3_pend_req *req_u, u3_gage* gag_u) if ( now_d == wen_d ) { #ifdef XMAS_DEBUG u3l_log("failed to find new oldest"); - _log_bitset(&req_u->was_u); #endif } req_u->old_w = idx_w; @@ -924,35 +887,6 @@ _try_resend(u3_pend_req* req_u) } } -// static u3_vec(c3_y[BLAKE3_OUT_LEN])* -// _xmas_get_proof_vec(u3_xmas_data* dat_u) -// { -// u3_assert( dat_u->aum_u.typ_e == AUTH_SIGN ); -// u3_assert( dat_u->aup_u.len_y != 0 ); -// u3_vec(c3_y[BLAKE3_OUT_LEN])* pof_u = vec_make(4); -// { -// c3_y* lef_y = c3_calloc(BLAKE3_OUT_LEN); -// blake_node* nod_u = blake_leaf_hash(dat_u->fra_y, dat_u->len_w, 0); -// memcpy(lef_y, nod_u->cev_y, BLAKE3_OUT_LEN); -// c3_free(nod_u); -// vec_append(pof_u, lef_y); -// } - -// for ( c3_w i_w = 0; i_w < dat_u->aup_u.len_y; i_w++ ) { -// c3_y* has_y = c3_calloc(BLAKE3_OUT_LEN); -// memcpy(has_y, dat_u->aup_u.has_y[i_w], BLAKE3_OUT_LEN); -// vec_append(pof_u, has_y); -// } -// return pof_u; -// } - -// static c3_y -// _xmas_verify_data(u3_xmas_data* dat_u, blake_bao* bao_u) -// { -// blake_pair* par_u = NULL; -// return blake_bao_verify(bao_u, dat_u->fra_y, dat_u->len_w, par_u); -// } - /* _xmas_req_pact_done(): mark packet as done, returning if we should continue */ static c3_o @@ -1101,10 +1035,146 @@ _xmas_timer_cb(uv_timer_t* tim_u) { _try_resend(req_u); } +static void +_xmas_czar_here(u3_czar_info* imp_u, time_t now_t, struct sockaddr_in* add_u) +{ + u3_xmas* sam_u = imp_u->sam_u; + c3_y imp_y = imp_u->imp_y; + c3_w pip_w = ntohl(add_u->sin_addr.s_addr); + + if ( imp_u->pip_w != pip_w ) { + u3_noun nam = u3dc("scot", c3__if, u3i_word(pip_w)); + c3_c* nam_c = u3r_string(nam); + + u3l_log("xmas: czar %s: ip %s", imp_u->dns_c, nam_c); + + c3_free(nam_c); + u3z(nam); + } + imp_u->pip_w = pip_w; + imp_u->tim_t = now_t; + + u3_noun pac, t = imp_u->pen; + + while ( t != u3_nul ) { + u3x_cell(t, &pac, &t); + c3_w len_w = u3r_met(3,pac); + c3_y* buf_y = c3_calloc(len_w); + u3r_bytes(0, len_w, buf_y, pac); + u3_lane lan_u = (u3_lane){pip_w, _ames_czar_port(imp_y)}; + _xmas_send_buf(sam_u, lan_u, buf_y, len_w); + } + u3z(imp_u->pen); + imp_u->pen = u3_nul; +} + +static void +_xmas_czar_gone(u3_xmas* sam_u, c3_i sas_i, c3_y imp_y, time_t now_t) +{ + u3_czar_info* imp_u = &sam_u->imp_u[imp_y]; + imp_u->tim_t = now_t; + u3l_log("xmas: %s", uv_strerror(sas_i)); +} + +static void +_xmas_czar_cb(uv_getaddrinfo_t* adr_u, c3_i sas_i, struct addrinfo* aif_u) +{ + u3_czar_info* imp_u = (u3_czar_info*)adr_u->data; + c3_y imp_y = imp_u->imp_y; + u3_xmas* sam_u = imp_u->sam_u; + time_t now_t = time(0); + + if ( 0 == sas_i ) { + // XX: lifetimes for addrinfo, ames does something funny + _xmas_czar_here(imp_u, now_t, (struct sockaddr_in*)aif_u->ai_addr); + } else { + _xmas_czar_gone(sam_u, sas_i, imp_y, now_t); + } + + c3_free(adr_u); + uv_freeaddrinfo(aif_u); + +} + + + +static void +_xmas_resolve_czar(u3_xmas* sam_u, c3_y imp_y, u3_noun pac) +{ + u3_assert( c3y == u3_Host.ops_u.net ); + u3_czar_info* imp_u = &sam_u->imp_u[imp_y]; + time_t now_t = time(0); + time_t wen_t = imp_u->tim_t; + if ( ((now_t - wen_t) < 300) ) { + // XX: confirm validity of drop + return; + } + if ( pac != u3_nul && u3_nul != imp_u->pen ) { + // already pending, add to queue + imp_u->pen = u3nc(pac, imp_u->pen); + return; + } + if ( pac != u3_nul ) { + imp_u->pen = u3nc(pac, imp_u->pen); + } + + if ( !sam_u->dns_c ) { + u3l_log("xmas: no galaxy domain"); + return; + } + imp_u->dns_c = _xmas_czar_dns(imp_y, sam_u->dns_c); + { + uv_getaddrinfo_t* adr_u = c3_calloc(sizeof(*adr_u)); + c3_i sas_i; + adr_u->data = imp_u; + struct addrinfo hints; + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET; + if ( 0 != (sas_i = uv_getaddrinfo(u3L, adr_u, _xmas_czar_cb, imp_u->dns_c, 0, &hints))) { + time_t now_t = time(0); + _xmas_czar_gone(sam_u, sas_i, imp_y, now_t); + return; + } + } + + +} + +static u3_noun +_xmas_queue_czar(u3_xmas* sam_u, u3_noun las, u3_noun pac) +{ + las = u3do("flop", las); + u3_noun lan, t = las; + u3_noun res = u3_nul; + time_t now_t = time(0); + + while ( t != u3_nul ) { + u3x_cell(t, &lan, &t); + if ( (c3y == u3a_is_cat(lan) && lan < 256 ) ) { + u3l_log("imp %u", lan); + u3_czar_info* imp_u = &sam_u->imp_u[lan]; + _log_czar_info(imp_u); + + if ( 0 != imp_u->pip_w ) { + res = u3nc(u3nt(c3__if, u3i_word(imp_u->pip_w), _ames_czar_port(lan)), res); + } + if ( c3y == u3_Host.ops_u.net ) { + _xmas_resolve_czar(sam_u, lan, u3k(pac)); + } + } else { + res = u3nc(u3k(lan), res); + } + } + u3z(las); + u3z(pac); + return res; +} + static void _xmas_ef_send(u3_xmas* sam_u, u3_noun las, u3_noun pac) { + las = _xmas_queue_czar(sam_u, las, u3k(pac)); c3_w len_w = u3r_met(3, pac); c3_y* buf_y = c3_calloc(len_w); u3r_bytes(0, len_w, buf_y, pac); @@ -1116,13 +1186,11 @@ _xmas_ef_send(u3_xmas* sam_u, u3_noun las, u3_noun pac) } c3_o suc_o = c3n; - u3_noun lan, t = las; _xmas_rout_bufs(sam_u, buf_y, len_w, las); u3z(pac); } - static c3_o _xmas_kick(u3_xmas* sam_u, u3_noun tag, u3_noun dat) { c3_o ret_o; @@ -1370,37 +1438,6 @@ _xmas_put_cache(u3_xmas* sam_u, u3_xmas_name* nam_u, u3_noun val) u3z(pax); // TODO: fix refcount } -/* _xmas_czar(): add packet to queue, possibly begin DNS resolution - */ -static void -_xmas_czar(u3_xmas_pict* pic_u, c3_y her_y) -{ - u3_xmas* sam_u = pic_u->sam_u; -#ifdef XMAS_DEBUG - if ( pac_u->hed_u.typ_y != PACT_PEEK ) { - u3l_log("xmas: attempted to resolve galaxy for packet that was not peek"); - u3m_bail(c3__oops); - return; - } -#endif - u3_czar_info* her_u = &sam_u->imp_u[her_y]; - if ( her_u->pip_w == 0 ) { - c3_y* buf_y = c3_calloc(PACT_SIZE); - c3_w siz_w = xmas_etch_pact(buf_y, &pic_u->pac_u); - her_u->pen = u3nc(u3i_bytes(siz_w, buf_y), her_u->pen); - } - - u3_noun pat = u3i_string(pic_u->pac_u.pek_u.nam_u.pat_c); - - //u3_weak her = _xmas_get_peer(sam_u, her_y); - - c3_o lok_o = _xmas_add_galaxy_pend(sam_u, her_y, pat); - - // TODO: actually resolve DNS - u3z(pat); -} - - static void _xmas_try_forward(u3_xmas_pict* pic_u, u3_noun fra, u3_noun hit) { @@ -1417,7 +1454,7 @@ _xmas_respond(u3_xmas_pict* req_u, c3_y** buf_y, u3_noun hit) *buf_y = c3_calloc(len_w); u3r_bytes(0, len_w, *buf_y, hit); - u3z(hit); + //u3z(hit); return len_w; } @@ -1556,6 +1593,7 @@ _hear_peer(u3_xmas* sam_u, u3_peer* per_u, u3_lane lan_u, c3_o dir_o) static void _xmas_forward(u3_xmas_pict* pic_u, u3_lane lan_u) { + u3l_log("forwarding"); u3_xmas_pact* pac_u = &pic_u->pac_u; u3_xmas* sam_u = pic_u->sam_u; c3_d her_d[2]; @@ -1573,7 +1611,6 @@ _xmas_forward(u3_xmas_pict* pic_u, u3_lane lan_u) u3_noun tag, dat; u3x_cell(u3k(hit), &tag, &dat); if ( tag == XMAS_WAIT ) { - u3m_p("lanes", u3t(dat)); c3_y* buf_y = c3_calloc(PACT_SIZE); c3_w len_w = xmas_etch_pact(buf_y, pac_u); _xmas_rout_bufs(sam_u, buf_y, len_w, u3k(u3t(dat))); @@ -1603,8 +1640,7 @@ _xmas_req_pact_init(u3_xmas* sam_u, u3_xmas_pict* pic_u, u3_lane* lan_u) u3_pend_req* req_u = _xmas_get_request(sam_u, nam_u); if ( NULL != req_u ) { - XMAS_LOG(INVARIANT); - u3_assert(0); + // duplicate return c3n; } else { req_u = alloca(sizeof(u3_pend_req)); @@ -1678,7 +1714,7 @@ static void _xmas_hear_page(u3_xmas_pict* pic_u, c3_y* buf_y, c3_w len_w, u3_lane lan_u) { #ifdef XMAS_DEBUG - u3l_log("xmas hear page"); + u3l_log("xmas hear page %u", pic_u->pac_u.pag_u.nam_u); #endif u3_xmas* sam_u = pic_u->sam_u; u3_xmas_pact* pac_u = &pic_u->pac_u; @@ -1723,15 +1759,21 @@ _xmas_hear_page(u3_xmas_pict* pic_u, c3_y* buf_y, c3_w len_w, u3_lane lan_u) }*/ if ( pac_u->pag_u.nam_u.fra_w == 0 || pac_u->pag_u.nam_u.aut_o == c3y ) { - _xmas_req_pact_init(sam_u, pic_u, &lan_u); + u3l_log("initialising request"); + c3_o run_o = _xmas_req_pact_init(sam_u, pic_u, &lan_u); + u3l_log("initialised request %c", run_o == c3y ? 'y' : 'n'); + if ( run_o == c3n ) { + return; + } } else { + u3l_log("continuing request"); c3_o run_o = _xmas_req_pact_done(sam_u, &pac_u->pag_u.nam_u, &pac_u->pag_u.dat_u, &lan_u); if ( c3n == run_o ) { // cleanup return; } } - + u3_xmas_pict* nex_u; c3_w nex_w; @@ -1778,6 +1820,7 @@ _xmas_hear_peek(u3_xmas_pict* pic_u, u3_lane lan_u) // u3_assert(pac_u->hed_u.typ_y == PACT_PEEK); #endif u3_xmas_pact* pac_u = &pic_u->pac_u; + log_pact(pac_u); u3_xmas* sam_u = pic_u->sam_u; c3_d her_d[2]; her_d[0] = 0; @@ -1790,7 +1833,7 @@ _xmas_hear_peek(u3_xmas_pict* pic_u, u3_lane lan_u) u3_peer* per_u = _xmas_get_peer(sam_u, her_d); if ( per_u == NULL ) { u3l_log("xmas: alien forward for %s", u3r_string(u3dc("scot", c3__p, u3i_chubs(2, her_d)))); - xmas_free_pact(pac_u); + //xmas_free_pact(pac_u); return; } if ( c3y == sam_u->for_o && sam_u->pir_u->who_d[0] == per_u->imp_s ) { @@ -1818,11 +1861,11 @@ _xmas_hear_peek(u3_xmas_pict* pic_u, u3_lane lan_u) // } c3_w len_w = _xmas_respond(pic_u, &buf_y, u3k(dat)); u3l_log("sending page %u", pac_u->pek_u.nam_u.fra_w); - _xmas_send_buf(pic_u->sam_u, lan_u, buf_y, len_w); + _xmas_send_buf(sam_u, lan_u, buf_y, len_w); } else { u3l_log("xmas: weird case in cache, dropping"); } - xmas_free_pact(pac_u); + //xmas_free_pact(pac_u); // u3z(hit); } else { _xmas_add_lane_to_cache(sam_u, &pac_u->pek_u.nam_u, u3_nul, lan_u); // TODO: retrieve from namespace @@ -1831,7 +1874,7 @@ _xmas_hear_peek(u3_xmas_pict* pic_u, u3_lane lan_u) u3_noun our = u3i_chubs(2, sam_u->car_u.pir_u->who_d); u3_noun bem = u3nc(u3nt(our, u3_nul, u3nc(c3__ud, 1)), sky); - u3_pier_peek(sam_u->car_u.pir_u, u3_nul, u3k(u3nq(1, c3__beam, c3__xx, bem)), pac_u, _xmas_page_scry_cb); + u3_pier_peek(sam_u->car_u.pir_u, u3_nul, u3k(u3nq(1, c3__beam, c3__xx, bem)), pic_u, _xmas_page_scry_cb); } else { xmas_free_pact(pac_u); } @@ -2052,12 +2095,6 @@ u3_xmas_io_init(u3_pier* pir_u) sam_u->tes_u = ur_cue_test_init(); - for ( c3_w i_w = 0; i_w < 5000; i_w++ ) { - u3_noun key = u3i_word(i_w); - u3_noun val = u3k(val); - - u3h_put(sam_u->req_p, key, val); - } // Disable networking for fake ships // @@ -2070,12 +2107,13 @@ u3_xmas_io_init(u3_pier* pir_u) u3_noun her = u3i_chubs(2, pir_u->who_d); for (int i = 0; i < 256; i++) { sam_u->imp_u[i].pen = u3_nul; + sam_u->imp_u[i].sam_u = sam_u; + sam_u->imp_u[i].imp_y = i; //sam_u.imp_u[i].tim = 0; - if ( u3_Host.ops_u.net == c3y ) { + if ( u3_Host.ops_u.net == c3n ) { sam_u->imp_u[i].pip_w = 0x7f000001; } else { sam_u->imp_u[i].pip_w = 0; - } } @@ -2111,515 +2149,3 @@ u3_xmas_io_init(u3_pier* pir_u) return car_u; } -#ifdef XMAS_TEST - -#define cmp_scalar(nam, str, fmt) \ - if ( hav_u->nam != ned_u->nam ) { \ - fprintf(stderr, "xmas test cmp " str " differ:\r\n" \ - " have: " fmt "\r\n" \ - " need: " fmt "\r\n", \ - hav_u->nam, ned_u->nam); \ - ret_i = 1; \ - } - -#define cmp_string(nam, siz, str) \ - if ( memcmp(hav_u->nam, ned_u->nam, siz) ) { \ - fprintf(stderr, "xmas test cmp " str " differ:\r\n" \ - " have: %*.s\r\n" \ - " need: %*.s\r\n", \ - siz, hav_u->nam, siz, ned_u->nam); \ - ret_i = 1; \ - } - -#define cmp_buffer(nam, siz, str) \ - if ( memcmp(hav_u->nam, ned_u->nam, siz) ) { \ - fprintf(stderr, "xmas test cmp " str "\r\n"); \ - ret_i = 1; \ - } - -static c3_i -_test_cmp_head(u3_xmas_head* hav_u, u3_xmas_head* ned_u) -{ - c3_i ret_i = 0; - - cmp_scalar(nex_y, "head: next", "%u"); - cmp_scalar(pro_y, "head: protocol", "%u"); - cmp_scalar(typ_y, "head: type", "%u"); - cmp_scalar(hop_y, "head: hop", "%u"); - cmp_scalar(mug_w, "head: mug", "%u"); - - return ret_i; -} - -static c3_i -_test_cmp_name(u3_xmas_name* hav_u, u3_xmas_name* ned_u) -{ - c3_i ret_i = 0; - - cmp_buffer(her_d, sizeof(ned_u->her_d), "name: ships differ"); - - cmp_scalar(rif_w, "name: rifts", "%u"); - cmp_scalar(boq_y, "name: bloqs", "%u"); - cmp_scalar(nit_o, "name: inits", "%u"); - cmp_scalar(aut_o, "name: auths", "%u"); - cmp_scalar(fra_w, "name: fragments", "%u"); - cmp_scalar(pat_s, "name: path-lengths", "%u"); - - cmp_string(pat_c, ned_u->pat_s, "name: paths"); - - return ret_i; -} - -static c3_i -_test_cmp_data(u3_xmas_data* hav_u, u3_xmas_data* ned_u) -{ - c3_i ret_i = 0; - - cmp_scalar(tot_w, "data: total packets", "%u"); - - cmp_scalar(aum_u.typ_e, "data: auth-types", "%u"); - cmp_buffer(aum_u.sig_y, 64, "data: sig|hmac|null"); - - cmp_scalar(aup_u.len_y, "data: hash-lengths", "%u"); - cmp_buffer(aup_u.has_y, ned_u->aup_u.len_y, "data: hashes"); - - cmp_scalar(len_w, "data: fragments-lengths", "%u"); - cmp_buffer(fra_y, ned_u->len_w, "data: fragments"); - - return ret_i; -} - -static c3_i -_test_pact(u3_xmas_pact* pac_u) -{ - c3_y* buf_y = c3_calloc(PACT_SIZE); - c3_w len_w = xmas_etch_pact(buf_y, pac_u); - c3_i ret_i = 0; - c3_i bot_i = 0; - c3_w sif_w; - - u3_xmas_pact nex_u; - memset(&nex_u, 0, sizeof(u3_xmas_pact)); - - if ( !len_w ) { - fprintf(stderr, "pact: etch failed\r\n"); - ret_i = 1; goto done; - } - else if ( len_w > PACT_SIZE ) { - fprintf(stderr, "pact: etch overflowed: %u\r\n", len_w); - ret_i = 1; goto done; - } - - if ( len_w != (sif_w = xmas_sift_pact(&nex_u, buf_y, len_w)) ) { - fprintf(stderr, "pact: sift failed len=%u sif=%u\r\n", len_w, sif_w); - _log_buf(buf_y, len_w); - ret_i = 1; goto done; - } - - bot_i = 1; - - if ( _test_cmp_head(&nex_u.hed_u, &pac_u->hed_u) ) { - fprintf(stderr, "pact: head cmp fail\r\n"); - ret_i = 1; - } - - switch ( pac_u->hed_u.typ_y ) { - case PACT_PEEK: { - if ( _test_cmp_name(&nex_u.pek_u.nam_u, &pac_u->pek_u.nam_u) ) { - fprintf(stderr, "%%peek name cmp fail\r\n"); - ret_i = 1; - } - } break; - - case PACT_PAGE: { - if ( _test_cmp_name(&nex_u.pag_u.nam_u, &pac_u->pag_u.nam_u) ) { - fprintf(stderr, "%%page name cmp fail\r\n"); - ret_i = 1; - } - else if ( _test_cmp_data(&nex_u.pag_u.dat_u, &pac_u->pag_u.dat_u) ) { - fprintf(stderr, "%%page data cmp fail\r\n"); - ret_i = 1; - } - } break; - - case PACT_POKE: { - if ( _test_cmp_name(&nex_u.pok_u.nam_u, &pac_u->pok_u.nam_u) ) { - fprintf(stderr, "%%poke name cmp fail\r\n"); - ret_i = 1; - } - else if ( _test_cmp_name(&nex_u.pok_u.pay_u, &pac_u->pok_u.pay_u) ) { - fprintf(stderr, "%%poke pay-name cmp fail\r\n"); - ret_i = 1; - } - else if ( _test_cmp_data(&nex_u.pok_u.dat_u, &pac_u->pok_u.dat_u) ) { - fprintf(stderr, "%%poke data cmp fail\r\n"); - ret_i = 1; - } - } break; - - default: { - fprintf(stderr, "test: strange packet\r\n"); - ret_i = 1; - } - } - -done: - if ( ret_i ) { - _log_head(&pac_u->hed_u); - _log_pact(pac_u); - _log_buf(buf_y, len_w); - - if ( bot_i ) { - u3l_log(RED_TEXT); - _log_head(&nex_u.hed_u); - _log_pact(&nex_u); - u3l_log(DEF_TEXT); - } - } - - c3_free(buf_y); - - return ret_i; -} - -static c3_y -_test_rand_bit(void* ptr_v) -{ - return rand() & 1; -} - -static c3_y -_test_rand_bits(void* ptr_v, c3_y len_y) -{ - assert( 8 >= len_y ); - return rand() & ((1 << len_y) - 1); -} - -static c3_w -_test_rand_word(void* ptr_v) -{ - c3_w low_w = rand(); - c3_w hig_w = rand(); - return (hig_w << 16) ^ (low_w & ((1 << 16) - 1)); -} - -static c3_y -_test_rand_gulf_y(void* ptr_v, c3_y top_y) -{ - c3_y bit_y = c3_bits_word(top_y); - c3_y res_y = 0; - - if ( !bit_y ) return res_y; - - while ( 1 ) { - res_y = _test_rand_bits(ptr_v, bit_y); - - if ( res_y < top_y ) { - return res_y; - } - } -} - -static c3_w -_test_rand_gulf_w(void* ptr_v, c3_w top_w) -{ - c3_w bit_w = c3_bits_word(top_w); - c3_w res_w = 0; - - if ( !bit_w ) return res_w; - - while ( 1 ) { - res_w = _test_rand_word(ptr_v); - res_w &= (1 << bit_w) - 1; - - if ( res_w < top_w ) { - return res_w; - } - } -} - -static void -_test_rand_bytes(void* ptr_v, c3_w len_w, c3_y* buf_y) -{ - c3_w max_w = len_w / 2; - - while ( max_w-- ) { - c3_w wor_w = rand(); - *buf_y++ = (wor_w >> 0) & 0xff; - *buf_y++ = (wor_w >> 8) & 0xff; - } - - if ( len_w & 1 ) { - *buf_y = rand() & 0xff; - } -} - -const char* ta_c = "-~_.0123456789abcdefghijklmnopqrstuvwxyz"; - -static void -_test_rand_knot(void* ptr_v, c3_w len_w, c3_c* not_c) -{ - for ( c3_w i_w = 0; i_w < len_w; i_w++ ) { - *not_c++ = ta_c[_test_rand_gulf_y(ptr_v, 40)]; - } -} - -static void -_test_rand_path(void* ptr_v, c3_s len_s, c3_c* pat_c) -{ - c3_s not_s = 0; - - while ( len_s ) { - not_s = _test_rand_gulf_w(ptr_v, len_s); - _test_rand_knot(ptr_v, not_s, pat_c); - pat_c[not_s] = '/'; - pat_c += not_s + 1; - len_s -= not_s + 1; - } -} - -static void -_test_make_head(void* ptr_v, u3_xmas_head* hed_u) -{ - hed_u->nex_y = NEXH_NONE; // XX - hed_u->pro_y = 1; - hed_u->typ_y = _test_rand_gulf_y(ptr_v, 3) + 1; - hed_u->hop_y = _test_rand_gulf_y(ptr_v, 8); - hed_u->mug_w = 0; - // XX set mug_w in etch? -} - -static void -_test_make_name(void* ptr_v, c3_s pat_s, u3_xmas_name* nam_u) -{ - _test_rand_bytes(ptr_v, 16, (c3_y*)nam_u->her_d); - nam_u->rif_w = _test_rand_word(ptr_v); - - nam_u->pat_s = _test_rand_gulf_w(ptr_v, pat_s); - nam_u->pat_c = c3_malloc(nam_u->pat_s + 1); - _test_rand_path(ptr_v, nam_u->pat_s, nam_u->pat_c); - nam_u->pat_c[nam_u->pat_s] = 0; - - nam_u->boq_y = _test_rand_bits(ptr_v, 8); - nam_u->nit_o = _test_rand_bits(ptr_v, 1); - - if ( c3y == nam_u->nit_o ) { - nam_u->aut_o = c3n; - nam_u->fra_w = 0; - } - else { - nam_u->aut_o = _test_rand_bits(ptr_v, 1); - nam_u->fra_w = _test_rand_word(ptr_v); - } -} - -static void -_test_make_data(void* ptr_v, u3_xmas_data* dat_u) -{ - dat_u->tot_w = _test_rand_word(ptr_v); - - memset(dat_u->aum_u.sig_y, 0, 64); - dat_u->aup_u.len_y = 0; - memset(dat_u->aup_u.has_y, 0, sizeof(dat_u->aup_u.has_y)); - - switch ( dat_u->aum_u.typ_e = _test_rand_bits(ptr_v, 2) ) { - case AUTH_NEXT: { - dat_u->aup_u.len_y = 2; - } break; - - case AUTH_SIGN: { - dat_u->aup_u.len_y = _test_rand_gulf_y(ptr_v, 3); - _test_rand_bytes(ptr_v, 64, dat_u->aum_u.sig_y); - } break; - - case AUTH_HMAC: { - dat_u->aup_u.len_y = _test_rand_gulf_y(ptr_v, 3); - _test_rand_bytes(ptr_v, 32, dat_u->aum_u.mac_y); - } break; - - default: break; - } - - for ( c3_w i_w = 0; i_w < dat_u->aup_u.len_y; i_w++ ) { - _test_rand_bytes(ptr_v, 32, dat_u->aup_u.has_y[i_w]); - } - - dat_u->len_w = _test_rand_gulf_w(ptr_v, 1024); - dat_u->fra_y = c3_calloc(dat_u->len_w); - _test_rand_bytes(ptr_v, dat_u->len_w, dat_u->fra_y); -} - -static void -_test_make_pact(void* ptr_v, u3_xmas_pact* pac_u) -{ - _test_make_head(ptr_v, &pac_u->hed_u); - - switch ( pac_u->hed_u.typ_y ) { - case PACT_PEEK: { - _test_make_name(ptr_v, 277, &pac_u->pek_u.nam_u); - } break; - - case PACT_PAGE: { - _test_make_name(ptr_v, 277, &pac_u->pag_u.nam_u); - _test_make_data(ptr_v, &pac_u->pag_u.dat_u); - } break; - - case PACT_POKE: { - _test_make_name(ptr_v, 124, &pac_u->pok_u.nam_u); - _test_make_name(ptr_v, 124, &pac_u->pok_u.pay_u); - _test_make_data(ptr_v, &pac_u->pok_u.dat_u); - } break; - - default: break; // XX - } -} - -static c3_i -_test_rand_pact(c3_w bat_w) -{ - u3_xmas_pact pac_u; - void* ptr_v = 0; - - fprintf(stderr, "pact: test roundtrip %u random packets\r\n", bat_w); - - for ( c3_w i_w = 0; i_w < bat_w; i_w++ ) { - _test_make_pact(ptr_v, &pac_u); - - if ( _test_pact(&pac_u) ) { - fprintf(stderr, RED_TEXT "pact: roundtrip attempt %u failed\r\n", i_w); - exit(1); - } - - // XX free pat_s / buf_y - } - - return 0; -} - -static void -_test_sift_page() -{ - u3_xmas_pact pac_u; - memset(&pac_u,0, sizeof(u3_xmas_pact)); - pac_u.hed_u.typ_y = PACT_PAGE; - pac_u.hed_u.pro_y = 1; - u3l_log("%%page checking sift/etch idempotent"); - u3_xmas_name* nam_u = &pac_u.pag_u.nam_u; - - { - u3_noun her = u3v_wish("~hastuc-dibtux"); - u3r_chubs(0, 2, nam_u->her_d, her); - u3z(her); - } - nam_u->rif_w = 15; - nam_u->pat_c = "foo/bar"; - nam_u->pat_s = strlen(nam_u->pat_c); - nam_u->boq_y = 13; - nam_u->fra_w = 54; - nam_u->nit_o = c3n; - - u3_xmas_data* dat_u = &pac_u.pag_u.dat_u; - dat_u->aum_u.typ_e = AUTH_NONE; - dat_u->tot_w = 1000; - dat_u->len_w = 1024; - dat_u->fra_y = c3_calloc(1024); - // dat_u->fra_y[1023] = 1; - - if ( _test_pact(&pac_u) ) { - fprintf(stderr, RED_TEXT "%%page failed\r\n"); - exit(1); - } -} - -static void -_test_sift_peek() -{ - u3_xmas_pact pac_u; - memset(&pac_u,0, sizeof(u3_xmas_pact)); - pac_u.hed_u.typ_y = PACT_PEEK; - pac_u.hed_u.pro_y = 1; - u3l_log("%%peek checking sift/etch idempotent"); - u3_xmas_name* nam_u = &pac_u.pek_u.nam_u; - { - u3_noun her = u3v_wish("~hastuc-dibtux"); - u3r_chubs(0, 2, nam_u->her_d, her); - u3z(her); - } - nam_u->rif_w = 15; - nam_u->pat_c = "foo/bar"; - nam_u->pat_s = strlen(nam_u->pat_c); - nam_u->boq_y = 13; - nam_u->fra_w = 511; - nam_u->nit_o = c3n; - nam_u->aut_o = c3n; - - if ( _test_pact(&pac_u) ) { - fprintf(stderr, RED_TEXT "%%page failed\r\n"); - exit(1); - } -} - -static void -_test_encode_path(c3_c* pat_c) -{ - u3_noun wan = u3do("stab", u3dt("cat", 3, '/', u3i_string(pat_c))); - u3_noun hav = _xmas_encode_path(strlen(pat_c), (c3_y*)pat_c); - - if ( c3n == u3r_sing(wan, hav) ) { - u3l_log(RED_TEXT); - u3l_log("path encoding mismatch"); - u3m_p("want", wan); - u3m_p("have", hav); - exit(1); - } -} - - -static void -_setup() -{ - c3_d len_d = u3_Ivory_pill_len; - c3_y* byt_y = u3_Ivory_pill; - u3_cue_xeno* sil_u; - u3_weak pil; - - u3C.wag_w |= u3o_hashless; - u3m_boot_lite(1 << 26); - sil_u = u3s_cue_xeno_init_with(ur_fib27, ur_fib28); - if ( u3_none == (pil = u3s_cue_xeno_with(sil_u, len_d, byt_y)) ) { - printf("*** fail _setup 1\n"); - exit(1); - } - u3s_cue_xeno_done(sil_u); - if ( c3n == u3v_boot_lite(pil) ) { - printf("*** fail _setup 2\n"); - exit(1); - } - - { - c3_w pid_w = getpid(); - srand(pid_w); - fprintf(stderr, "test: seeding rand() with pid %u\r\n", pid_w); - } -} - -int main() -{ - _setup(); - _test_bitset(); - - _test_sift_peek(); - _test_sift_page(); - - _test_rand_pact(100000); - - _test_encode_path("foo/bar/baz"); - _test_encode_path("publ/0/xx//1/foo/g"); - return 0; -} - -#endif - - - - diff --git a/pkg/vere/io/xmas/pact.c b/pkg/vere/io/xmas/pact.c index 487b8dde8f..f93fa57103 100644 --- a/pkg/vere/io/xmas/pact.c +++ b/pkg/vere/io/xmas/pact.c @@ -1,5 +1,12 @@ #include "xmas.h" #include +// only need for tests, can remove +#include "vere.h" +#include "ivory.h" +#include "ur.h" +#define RED_TEXT "\033[0;31m" +#define DEF_TEXT "\033[0m" +// endif tests #define SIFT_VAR(dest, src, len) dest = 0; for(int i = 0; i < len; i++ ) { dest |= ((src + i) >> (8*i)); } #define CHECK_BOUNDS(cur) if ( len_w < cur ) { u3l_log("xmas: failed parse (%u,%u) at line %i", len_w, cur, __LINE__); return 0; } @@ -163,8 +170,8 @@ _log_poke_pact(u3_xmas_poke_pact *pac_u) _log_data(&pac_u->dat_u); } -static void -_log_pact(u3_xmas_pact* pac_u) +void +log_pact(u3_xmas_pact* pac_u) { switch ( pac_u->hed_u.typ_y ) { case PACT_PEEK: { @@ -223,7 +230,7 @@ void xmas_free_pact(u3_xmas_pact* pac_u) break; }; case PACT_PAGE: { - c3_free(pac_u->pag_u.dat_u.fra_y); + //c3_free(pac_u->pag_u.dat_u.fra_y); break; }; case PACT_POKE: { @@ -950,4 +957,525 @@ xmas_etch_pact(c3_y* buf_y, u3_xmas_pact* pac_u) assert( siz_w == cur_w ); return cur_w; -} \ No newline at end of file +} + +#ifdef PACT_TEST + +/* _xmas_encode_path(): produce buf_y as a parsed path +*/ +static u3_noun +_xmas_encode_path(c3_w len_w, c3_y* buf_y) +{ + u3_noun pro; + u3_noun* lit = &pro; + + { + u3_noun* hed; + u3_noun* tel; + c3_y* fub_y = buf_y; + c3_y car_y; + c3_w tem_w; + u3i_slab sab_u; + + while ( len_w-- ) { + car_y = *buf_y++; + if ( len_w == 0 ) { + buf_y++; + car_y = 47; + } + + if ( 47 == car_y ) { + tem_w = buf_y - fub_y - 1; + u3i_slab_bare(&sab_u, 3, tem_w); + sab_u.buf_w[sab_u.len_w - 1] = 0; + memcpy(sab_u.buf_y, fub_y, tem_w); + + *lit = u3i_defcons(&hed, &tel); + *hed = u3i_slab_moot(&sab_u); + lit = tel; + fub_y = buf_y; + } + } + } + + *lit = u3_nul; + + return pro; +} + +#define cmp_scalar(nam, str, fmt) \ + if ( hav_u->nam != ned_u->nam ) { \ + fprintf(stderr, "xmas test cmp " str " differ:\r\n" \ + " have: " fmt "\r\n" \ + " need: " fmt "\r\n", \ + hav_u->nam, ned_u->nam); \ + ret_i = 1; \ + } + +#define cmp_string(nam, siz, str) \ + if ( memcmp(hav_u->nam, ned_u->nam, siz) ) { \ + fprintf(stderr, "xmas test cmp " str " differ:\r\n" \ + " have: %*.s\r\n" \ + " need: %*.s\r\n", \ + siz, hav_u->nam, siz, ned_u->nam); \ + ret_i = 1; \ + } + +#define cmp_buffer(nam, siz, str) \ + if ( memcmp(hav_u->nam, ned_u->nam, siz) ) { \ + fprintf(stderr, "xmas test cmp " str "\r\n"); \ + ret_i = 1; \ + } + +static c3_i +_test_cmp_head(u3_xmas_head* hav_u, u3_xmas_head* ned_u) +{ + c3_i ret_i = 0; + + cmp_scalar(nex_y, "head: next", "%u"); + cmp_scalar(pro_y, "head: protocol", "%u"); + cmp_scalar(typ_y, "head: type", "%u"); + cmp_scalar(hop_y, "head: hop", "%u"); + cmp_scalar(mug_w, "head: mug", "%u"); + + return ret_i; +} + +static c3_i +_test_cmp_name(u3_xmas_name* hav_u, u3_xmas_name* ned_u) +{ + c3_i ret_i = 0; + + cmp_buffer(her_d, sizeof(ned_u->her_d), "name: ships differ"); + + cmp_scalar(rif_w, "name: rifts", "%u"); + cmp_scalar(boq_y, "name: bloqs", "%u"); + cmp_scalar(nit_o, "name: inits", "%u"); + cmp_scalar(aut_o, "name: auths", "%u"); + cmp_scalar(fra_w, "name: fragments", "%u"); + cmp_scalar(pat_s, "name: path-lengths", "%u"); + + cmp_string(pat_c, ned_u->pat_s, "name: paths"); + + return ret_i; +} + +static c3_i +_test_cmp_data(u3_xmas_data* hav_u, u3_xmas_data* ned_u) +{ + c3_i ret_i = 0; + + cmp_scalar(tot_w, "data: total packets", "%u"); + + cmp_scalar(aum_u.typ_e, "data: auth-types", "%u"); + cmp_buffer(aum_u.sig_y, 64, "data: sig|hmac|null"); + + cmp_scalar(aup_u.len_y, "data: hash-lengths", "%u"); + cmp_buffer(aup_u.has_y, ned_u->aup_u.len_y, "data: hashes"); + + cmp_scalar(len_w, "data: fragments-lengths", "%u"); + cmp_buffer(fra_y, ned_u->len_w, "data: fragments"); + + return ret_i; +} + +static c3_i +_test_pact(u3_xmas_pact* pac_u) +{ + c3_y* buf_y = c3_calloc(PACT_SIZE); + c3_w len_w = xmas_etch_pact(buf_y, pac_u); + c3_i ret_i = 0; + c3_i bot_i = 0; + c3_w sif_w; + + u3_xmas_pact nex_u; + memset(&nex_u, 0, sizeof(u3_xmas_pact)); + + if ( !len_w ) { + fprintf(stderr, "pact: etch failed\r\n"); + ret_i = 1; goto done; + } + else if ( len_w > PACT_SIZE ) { + fprintf(stderr, "pact: etch overflowed: %u\r\n", len_w); + ret_i = 1; goto done; + } + + if ( len_w != (sif_w = xmas_sift_pact(&nex_u, buf_y, len_w)) ) { + fprintf(stderr, "pact: sift failed len=%u sif=%u\r\n", len_w, sif_w); + _log_buf(buf_y, len_w); + ret_i = 1; goto done; + } + + bot_i = 1; + + if ( _test_cmp_head(&nex_u.hed_u, &pac_u->hed_u) ) { + fprintf(stderr, "pact: head cmp fail\r\n"); + ret_i = 1; + } + + switch ( pac_u->hed_u.typ_y ) { + case PACT_PEEK: { + if ( _test_cmp_name(&nex_u.pek_u.nam_u, &pac_u->pek_u.nam_u) ) { + fprintf(stderr, "%%peek name cmp fail\r\n"); + ret_i = 1; + } + } break; + + case PACT_PAGE: { + if ( _test_cmp_name(&nex_u.pag_u.nam_u, &pac_u->pag_u.nam_u) ) { + fprintf(stderr, "%%page name cmp fail\r\n"); + ret_i = 1; + } + else if ( _test_cmp_data(&nex_u.pag_u.dat_u, &pac_u->pag_u.dat_u) ) { + fprintf(stderr, "%%page data cmp fail\r\n"); + ret_i = 1; + } + } break; + + case PACT_POKE: { + if ( _test_cmp_name(&nex_u.pok_u.nam_u, &pac_u->pok_u.nam_u) ) { + fprintf(stderr, "%%poke name cmp fail\r\n"); + ret_i = 1; + } + else if ( _test_cmp_name(&nex_u.pok_u.pay_u, &pac_u->pok_u.pay_u) ) { + fprintf(stderr, "%%poke pay-name cmp fail\r\n"); + ret_i = 1; + } + else if ( _test_cmp_data(&nex_u.pok_u.dat_u, &pac_u->pok_u.dat_u) ) { + fprintf(stderr, "%%poke data cmp fail\r\n"); + ret_i = 1; + } + } break; + + default: { + fprintf(stderr, "test: strange packet\r\n"); + ret_i = 1; + } + } + +done: + if ( ret_i ) { + _log_head(&pac_u->hed_u); + _log_pact(pac_u); + _log_buf(buf_y, len_w); + + if ( bot_i ) { + u3l_log(RED_TEXT); + _log_head(&nex_u.hed_u); + _log_pact(&nex_u); + u3l_log(DEF_TEXT); + } + } + + c3_free(buf_y); + + return ret_i; +} + +static c3_y +_test_rand_bit(void* ptr_v) +{ + return rand() & 1; +} + +static c3_y +_test_rand_bits(void* ptr_v, c3_y len_y) +{ + assert( 8 >= len_y ); + return rand() & ((1 << len_y) - 1); +} + +static c3_w +_test_rand_word(void* ptr_v) +{ + c3_w low_w = rand(); + c3_w hig_w = rand(); + return (hig_w << 16) ^ (low_w & ((1 << 16) - 1)); +} + +static c3_y +_test_rand_gulf_y(void* ptr_v, c3_y top_y) +{ + c3_y bit_y = c3_bits_word(top_y); + c3_y res_y = 0; + + if ( !bit_y ) return res_y; + + while ( 1 ) { + res_y = _test_rand_bits(ptr_v, bit_y); + + if ( res_y < top_y ) { + return res_y; + } + } +} + +static c3_w +_test_rand_gulf_w(void* ptr_v, c3_w top_w) +{ + c3_w bit_w = c3_bits_word(top_w); + c3_w res_w = 0; + + if ( !bit_w ) return res_w; + + while ( 1 ) { + res_w = _test_rand_word(ptr_v); + res_w &= (1 << bit_w) - 1; + + if ( res_w < top_w ) { + return res_w; + } + } +} + +static void +_test_rand_bytes(void* ptr_v, c3_w len_w, c3_y* buf_y) +{ + c3_w max_w = len_w / 2; + + while ( max_w-- ) { + c3_w wor_w = rand(); + *buf_y++ = (wor_w >> 0) & 0xff; + *buf_y++ = (wor_w >> 8) & 0xff; + } + + if ( len_w & 1 ) { + *buf_y = rand() & 0xff; + } +} + +const char* ta_c = "-~_.0123456789abcdefghijklmnopqrstuvwxyz"; + +static void +_test_rand_knot(void* ptr_v, c3_w len_w, c3_c* not_c) +{ + for ( c3_w i_w = 0; i_w < len_w; i_w++ ) { + *not_c++ = ta_c[_test_rand_gulf_y(ptr_v, 40)]; + } +} + +static void +_test_rand_path(void* ptr_v, c3_s len_s, c3_c* pat_c) +{ + c3_s not_s = 0; + + while ( len_s ) { + not_s = _test_rand_gulf_w(ptr_v, len_s); + _test_rand_knot(ptr_v, not_s, pat_c); + pat_c[not_s] = '/'; + pat_c += not_s + 1; + len_s -= not_s + 1; + } +} + +static void +_test_make_head(void* ptr_v, u3_xmas_head* hed_u) +{ + hed_u->nex_y = NEXH_NONE; // XX + hed_u->pro_y = 1; + hed_u->typ_y = _test_rand_gulf_y(ptr_v, 3) + 1; + hed_u->hop_y = _test_rand_gulf_y(ptr_v, 8); + hed_u->mug_w = 0; + // XX set mug_w in etch? +} + +static void +_test_make_name(void* ptr_v, c3_s pat_s, u3_xmas_name* nam_u) +{ + _test_rand_bytes(ptr_v, 16, (c3_y*)nam_u->her_d); + nam_u->rif_w = _test_rand_word(ptr_v); + + nam_u->pat_s = _test_rand_gulf_w(ptr_v, pat_s); + nam_u->pat_c = c3_malloc(nam_u->pat_s + 1); + _test_rand_path(ptr_v, nam_u->pat_s, nam_u->pat_c); + nam_u->pat_c[nam_u->pat_s] = 0; + + nam_u->boq_y = _test_rand_bits(ptr_v, 8); + nam_u->nit_o = _test_rand_bits(ptr_v, 1); + + if ( c3y == nam_u->nit_o ) { + nam_u->aut_o = c3n; + nam_u->fra_w = 0; + } + else { + nam_u->aut_o = _test_rand_bits(ptr_v, 1); + nam_u->fra_w = _test_rand_word(ptr_v); + } +} + +static void +_test_make_data(void* ptr_v, u3_xmas_data* dat_u) +{ + dat_u->tot_w = _test_rand_word(ptr_v); + + memset(dat_u->aum_u.sig_y, 0, 64); + dat_u->aup_u.len_y = 0; + memset(dat_u->aup_u.has_y, 0, sizeof(dat_u->aup_u.has_y)); + + switch ( dat_u->aum_u.typ_e = _test_rand_bits(ptr_v, 2) ) { + case AUTH_NEXT: { + dat_u->aup_u.len_y = 2; + } break; + + case AUTH_SIGN: { + dat_u->aup_u.len_y = _test_rand_gulf_y(ptr_v, 3); + _test_rand_bytes(ptr_v, 64, dat_u->aum_u.sig_y); + } break; + + case AUTH_HMAC: { + dat_u->aup_u.len_y = _test_rand_gulf_y(ptr_v, 3); + _test_rand_bytes(ptr_v, 32, dat_u->aum_u.mac_y); + } break; + + default: break; + } + + for ( c3_w i_w = 0; i_w < dat_u->aup_u.len_y; i_w++ ) { + _test_rand_bytes(ptr_v, 32, dat_u->aup_u.has_y[i_w]); + } + + dat_u->len_w = _test_rand_gulf_w(ptr_v, 1024); + dat_u->fra_y = c3_calloc(dat_u->len_w); + _test_rand_bytes(ptr_v, dat_u->len_w, dat_u->fra_y); +} + +static void +_test_make_pact(void* ptr_v, u3_xmas_pact* pac_u) +{ + _test_make_head(ptr_v, &pac_u->hed_u); + + switch ( pac_u->hed_u.typ_y ) { + case PACT_PEEK: { + _test_make_name(ptr_v, 277, &pac_u->pek_u.nam_u); + } break; + + case PACT_PAGE: { + _test_make_name(ptr_v, 277, &pac_u->pag_u.nam_u); + _test_make_data(ptr_v, &pac_u->pag_u.dat_u); + } break; + + case PACT_POKE: { + _test_make_name(ptr_v, 124, &pac_u->pok_u.nam_u); + _test_make_name(ptr_v, 124, &pac_u->pok_u.pay_u); + _test_make_data(ptr_v, &pac_u->pok_u.dat_u); + } break; + + default: break; // XX + } +} + +static c3_i +_test_rand_pact(c3_w bat_w) +{ + u3_xmas_pact pac_u; + void* ptr_v = 0; + + fprintf(stderr, "pact: test roundtrip %u random packets\r\n", bat_w); + + for ( c3_w i_w = 0; i_w < bat_w; i_w++ ) { + _test_make_pact(ptr_v, &pac_u); + + if ( _test_pact(&pac_u) ) { + fprintf(stderr, RED_TEXT "pact: roundtrip attempt %u failed\r\n", i_w); + exit(1); + } + + // XX free pat_s / buf_y + } + + return 0; +} + +static void +_test_sift_page() +{ + u3_xmas_pact pac_u; + memset(&pac_u,0, sizeof(u3_xmas_pact)); + pac_u.hed_u.typ_y = PACT_PAGE; + pac_u.hed_u.pro_y = 1; + u3l_log("%%page checking sift/etch idempotent"); + u3_xmas_name* nam_u = &pac_u.pag_u.nam_u; + + { + u3_noun her = u3v_wish("~hastuc-dibtux"); + u3r_chubs(0, 2, nam_u->her_d, her); + u3z(her); + } + nam_u->rif_w = 15; + nam_u->pat_c = "foo/bar"; + nam_u->pat_s = strlen(nam_u->pat_c); + nam_u->boq_y = 13; + nam_u->fra_w = 54; + nam_u->nit_o = c3n; + + u3_xmas_data* dat_u = &pac_u.pag_u.dat_u; + dat_u->aum_u.typ_e = AUTH_NONE; + dat_u->tot_w = 1000; + dat_u->len_w = 1024; + dat_u->fra_y = c3_calloc(1024); + // dat_u->fra_y[1023] = 1; + + if ( _test_pact(&pac_u) ) { + fprintf(stderr, RED_TEXT "%%page failed\r\n"); + exit(1); + } +} + + +static void +_test_encode_path(c3_c* pat_c) +{ + u3_noun wan = u3do("stab", u3dt("cat", 3, '/', u3i_string(pat_c))); + u3_noun hav = _xmas_encode_path(strlen(pat_c), (c3_y*)pat_c); + + if ( c3n == u3r_sing(wan, hav) ) { + u3l_log(RED_TEXT); + u3l_log("path encoding mismatch"); + u3m_p("want", wan); + u3m_p("have", hav); + exit(1); + } +} + + +static void +_setup() +{ + c3_d len_d = u3_Ivory_pill_len; + c3_y* byt_y = u3_Ivory_pill; + u3_cue_xeno* sil_u; + u3_weak pil; + + u3C.wag_w |= u3o_hashless; + u3m_boot_lite(1 << 26); + sil_u = u3s_cue_xeno_init_with(ur_fib27, ur_fib28); + if ( u3_none == (pil = u3s_cue_xeno_with(sil_u, len_d, byt_y)) ) { + printf("*** fail _setup 1\n"); + exit(1); + } + u3s_cue_xeno_done(sil_u); + if ( c3n == u3v_boot_lite(pil) ) { + printf("*** fail _setup 2\n"); + exit(1); + } + + { + c3_w pid_w = getpid(); + srand(pid_w); + fprintf(stderr, "test: seeding rand() with pid %u\r\n", pid_w); + } +} + +int main() +{ + _setup(); + + _test_rand_pact(100000); + + _test_encode_path("foo/bar/baz"); + _test_encode_path("publ/0/xx//1/foo/g"); + return 0; +} + + +#endif \ No newline at end of file diff --git a/pkg/vere/io/xmas/xmas.h b/pkg/vere/io/xmas/xmas.h index ae3290f1b2..0132b1b0f6 100644 --- a/pkg/vere/io/xmas/xmas.h +++ b/pkg/vere/io/xmas/xmas.h @@ -156,4 +156,6 @@ c3_w xmas_etch_pact(c3_y* buf_y, u3_xmas_pact* pac_u); void xmas_free_pact(u3_xmas_pact* pac_u); +void log_pact(u3_xmas_pact* pac_u); + #endif \ No newline at end of file From 57dbc7ab40c2d23c9e689fca0f2a31d8964dc20a Mon Sep 17 00:00:00 2001 From: parallels Date: Mon, 4 Mar 2024 16:03:30 -0500 Subject: [PATCH 059/430] xmas: blake non-inline support --- pkg/vere/io/blake.c | 26 +++++++++++---- pkg/vere/io/xmas.c | 70 ++++++++++++++++++++++++++++------------- pkg/vere/io/xmas/pact.c | 2 +- 3 files changed, 70 insertions(+), 28 deletions(-) diff --git a/pkg/vere/io/blake.c b/pkg/vere/io/blake.c index 824f51005d..443872be31 100644 --- a/pkg/vere/io/blake.c +++ b/pkg/vere/io/blake.c @@ -96,6 +96,17 @@ vec_free(u3_raw_vec* vec_u) c3_free(vec_u->vod_p); } +void +vec_flop(u3_raw_vec* vec_u) +{ + for (int i = 0; i < (vec_u->len_w/2); i++) { + c3_w j_w = (vec_u->len_w - 1 -i ); + void* vod_p = vec_u->vod_p[i]; + vec_u->vod_p[i] = vec_u->vod_p[j_w]; + vec_u->vod_p[j_w] = vod_p; + } +} + void vec_drop(u3_raw_vec* vec_u) { @@ -510,11 +521,6 @@ _veri_check_leaf(blake_bao* bao_u, c3_y* dat_y, c3_w dat_w) c3_y cav_y[BLAKE3_OUT_LEN]; blake_node* nod_u = blake_leaf_hash(dat_y, dat_w, bao_u->con_w); make_chain_value(cav_y, nod_u); - if ( bao_u->con_w == 3 ) { - log_node(nod_u); - _log_buf("have", cav_y, 32); - _log_buf("want", out_y, 32); - } bao_u->con_w++; return 0 == memcmp(cav_y, out_y, BLAKE3_OUT_LEN) ? c3y : c3n; } else if ( _vec_len(&bao_u->sta_u) > 0 ) { @@ -545,6 +551,12 @@ _veri_check_pair(blake_bao* bao_u, blake_pair* par_u) return _(memcmp(par_y, cav_y, BLAKE3_OUT_LEN) == 0); } +static c3_o +_at_leaf(blake_subtree* sub_u) +{ + return (sub_u->sin_d+1 == sub_u->dex_d) ? c3y : c3n; +} + blake_bao* blake_bao_make(c3_w num_w, u3_vec(c3_y[BLAKE3_OUT_LEN])* pof_u) @@ -567,6 +579,7 @@ blake_bao_make(c3_w num_w, u3_vec(c3_y[BLAKE3_OUT_LEN])* pof_u) vec_init(&bao_u->sta_u, 8); vec_init(&bao_u->que_u, 8); vec_copy(&bao_u->sta_u, pof_u); + vec_flop(&bao_u->sta_u); _push_leaves(bao_u, fst_y, snd_y); return bao_u; } @@ -590,7 +603,8 @@ blake_bao_verify(blake_bao* bao_u, c3_y* dat_y, c3_w dat_w, blake_pair* par_u) if ( c3n == _veri_check_pair(bao_u, par_u) ) { return BAO_FAILED; } - if ( _height(&bao_u->sub_u) == 0 ) { + _verifier_next(bao_u); + if ( _at_leaf(&bao_u->sub_u) == c3y ) { _push_leaves(bao_u, par_u->sin_y, par_u->dex_y); } else { _push_parents(bao_u, par_u); diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c index 8ff3583972..bdda7ed375 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/xmas.c @@ -220,6 +220,16 @@ get_millis() { // Convert the microseconds to milliseconds by dividing by 1000 } +static void +_log_buf(c3_y* buf_y, c3_w len_w) +{ + for( c3_w i_w = 0; i_w < len_w; i_w++ ) { + fprintf(stderr, "%02x", buf_y[i_w]); + } + fprintf(stderr, "\r\n"); +} + + static void _log_gage(u3_gage* gag_u) { @@ -909,10 +919,14 @@ _xmas_req_pact_done(u3_xmas* sam_u, u3_xmas_name *nam_u, u3_xmas_data* dat_u, u3 c3_y ver_y; // TODO: move to bottom - if( BAO_GOOD != (ver_y = blake_bao_verify(req_u->bao_u, dat_u->fra_y, dat_u->len_w, par_u))) { + if ( req_u->bao_u->con_w != nam_u->fra_w ) { + // TODO: queue packet + u3l_log("misordered hash"); + } else if( BAO_GOOD != (ver_y = blake_bao_verify(req_u->bao_u, dat_u->fra_y, dat_u->len_w, par_u))) { // TODO: do we drop the whole request on the floor? XMAS_LOG(AUTH); return c3n; + } else { } u3_gage* gag_u = _xmas_get_lane(sam_u, nam_u->her_d, lan_u); @@ -1641,7 +1655,9 @@ _xmas_req_pact_init(u3_xmas* sam_u, u3_xmas_pict* pic_u, u3_lane* lan_u) u3_pend_req* req_u = _xmas_get_request(sam_u, nam_u); if ( NULL != req_u ) { // duplicate - return c3n; + if ( req_u->tot_w <= 4 ) { + return c3n; + } } else { req_u = alloca(sizeof(u3_pend_req)); memset(req_u, 0, sizeof(u3_pend_req)); @@ -1678,30 +1694,43 @@ _xmas_req_pact_init(u3_xmas* sam_u, u3_xmas_pict* pic_u, u3_lane* lan_u) // TODO: handle restart // u3_assert( nam_u->fra_w == 0 ); - req_u->nex_w = c3y == lin_o ? 1 : 0; + req_u->nex_w = (c3y == lin_o) ? 1 : 0; req_u->len_w = (c3y == lin_o) ? 1 : 0; uv_timer_init(u3L, &req_u->tim_u); req_u->lef_w = 0; req_u->old_w = 0; req_u->ack_w = 0; - u3_vec(c3_y[BLAKE3_OUT_LEN])* pof_u = vec_make(8); - { - blake_node* nod_u = blake_leaf_hash(dat_u->fra_y, dat_u->len_w, 0); - c3_y* lef_y = c3_calloc(BLAKE3_OUT_LEN); - make_chain_value(lef_y, nod_u); - c3_free(nod_u); - vec_append(pof_u, lef_y); - } + if ( c3y == lin_o ) { + u3_vec(c3_y[BLAKE3_OUT_LEN])* pof_u = vec_make(8); + { + blake_node* nod_u = blake_leaf_hash(dat_u->fra_y, dat_u->len_w, 0); + c3_y* lef_y = c3_calloc(BLAKE3_OUT_LEN); + make_chain_value(lef_y, nod_u); + c3_free(nod_u); + vec_append(pof_u, lef_y); + } - for ( int i = 0; i < pac_u->pag_u.dat_u.aup_u.len_y; i++ ) { - c3_y* pof_y = c3_calloc(BLAKE3_OUT_LEN); - memcpy(pof_y, pac_u->pag_u.dat_u.aup_u.has_y[i], BLAKE3_OUT_LEN); - vec_append(pof_u, pof_y); - } + for ( int i = 0; i < pac_u->pag_u.dat_u.aup_u.len_y; i++ ) { + c3_y* pof_y = c3_calloc(BLAKE3_OUT_LEN); + memcpy(pof_y, pac_u->pag_u.dat_u.aup_u.has_y[i], BLAKE3_OUT_LEN); + vec_append(pof_u, pof_y); + } - req_u->bao_u = blake_bao_make(req_u->tot_w, pof_u); - blake_bao_verify(req_u->bao_u, dat_u->fra_y, dat_u->len_w, NULL); + req_u->bao_u = blake_bao_make(req_u->tot_w, pof_u); + blake_bao_verify(req_u->bao_u, dat_u->fra_y, dat_u->len_w, NULL); + } else { + c3_w len_w = dat_u->len_w / BLAKE3_OUT_LEN; + u3_vec(c3_y[BLAKE3_OUT_LEN])* pof_u = vec_make(len_w); + for ( int i = 0; i < len_w; i++ ) { + c3_y* pof_y = c3_calloc(BLAKE3_OUT_LEN); + u3l_log("proof %u", i); + memcpy(pof_y, dat_u->fra_y + (BLAKE3_OUT_LEN*i), BLAKE3_OUT_LEN); + _log_buf(pof_y, BLAKE3_OUT_LEN); + vec_append(pof_u, pof_y); + } + req_u->bao_u = blake_bao_make(req_u->tot_w, pof_u); + } _xmas_put_request(sam_u, nam_u, req_u); return c3y; @@ -1714,7 +1743,7 @@ static void _xmas_hear_page(u3_xmas_pict* pic_u, c3_y* buf_y, c3_w len_w, u3_lane lan_u) { #ifdef XMAS_DEBUG - u3l_log("xmas hear page %u", pic_u->pac_u.pag_u.nam_u); + u3l_log("xmas hear page %u", pic_u->pac_u.pag_u.nam_u.fra_w); #endif u3_xmas* sam_u = pic_u->sam_u; u3_xmas_pact* pac_u = &pic_u->pac_u; @@ -1758,7 +1787,7 @@ _xmas_hear_page(u3_xmas_pict* pic_u, c3_y* buf_y, c3_w len_w, u3_lane lan_u) return; }*/ - if ( pac_u->pag_u.nam_u.fra_w == 0 || pac_u->pag_u.nam_u.aut_o == c3y ) { + if ( pac_u->pek_u.nam_u.nit_o == c3y ) { u3l_log("initialising request"); c3_o run_o = _xmas_req_pact_init(sam_u, pic_u, &lan_u); u3l_log("initialised request %c", run_o == c3y ? 'y' : 'n'); @@ -1820,7 +1849,6 @@ _xmas_hear_peek(u3_xmas_pict* pic_u, u3_lane lan_u) // u3_assert(pac_u->hed_u.typ_y == PACT_PEEK); #endif u3_xmas_pact* pac_u = &pic_u->pac_u; - log_pact(pac_u); u3_xmas* sam_u = pic_u->sam_u; c3_d her_d[2]; her_d[0] = 0; diff --git a/pkg/vere/io/xmas/pact.c b/pkg/vere/io/xmas/pact.c index f93fa57103..3b2bd08894 100644 --- a/pkg/vere/io/xmas/pact.c +++ b/pkg/vere/io/xmas/pact.c @@ -24,7 +24,7 @@ } \ } while(0) -#define u3l_log(...) fprintf(stderr, __VA_ARGS__) +//#define u3l_log(...) fprintf(stderr, __VA_ARGS__) #define c3_calloc(s) ({ \ void* rut = calloc(1,s); \ if ( 0 == rut ) { \ From 1268c90a89c9eb64cb68c68b565e563bb4bda370 Mon Sep 17 00:00:00 2001 From: Sigilante Date: Tue, 5 Mar 2024 15:52:40 -0600 Subject: [PATCH 060/430] Works with SoftBLAS. --- MODULE.bazel | 6 + WORKSPACE.bazel | 2 +- bazel/third_party/softblas/softblas.BUILD | 1 + pkg/noun/jets/f/lagoon.c | 256 ++++++++++++++++++---- pkg/noun/jets/q.h | 3 +- pkg/noun/jets/tree.c | 4 +- pkg/noun/jets/w.h | 1 + 7 files changed, 232 insertions(+), 41 deletions(-) create mode 100644 MODULE.bazel diff --git a/MODULE.bazel b/MODULE.bazel new file mode 100644 index 0000000000..00bb18361f --- /dev/null +++ b/MODULE.bazel @@ -0,0 +1,6 @@ +############################################################################### +# Bazel now uses Bzlmod by default to manage external dependencies. +# Please consider migrating your external dependencies from WORKSPACE to MODULE.bazel. +# +# For more details, please check https://github.com/bazelbuild/bazel/issues/18958 +############################################################################### diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index 85ba2a48b0..3b0296fd0f 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -297,7 +297,7 @@ versioned_http_archive( strip_prefix = "SoftBLAS-{version}", # sha256 = "", url = "https://github.com/urbit/SoftBLAS/archive/{version}.tar.gz", - version = "cbf3dfff5882fd03f28a74c7c0c6ef4c27ec176d", + version = "bace30db3944c0f2bb2b6cac0db9965675ad842e", ) versioned_http_archive( diff --git a/bazel/third_party/softblas/softblas.BUILD b/bazel/third_party/softblas/softblas.BUILD index 2ca6b46b1a..3442c5da45 100644 --- a/bazel/third_party/softblas/softblas.BUILD +++ b/bazel/third_party/softblas/softblas.BUILD @@ -9,6 +9,7 @@ cc_library( includes = ["include"], srcs = [ "include/softblas.h", + "src/softblas_state.c", "src/blas/level1/sasum.c", "src/blas/level1/dasum.c", "src/blas/level1/hasum.c", diff --git a/pkg/noun/jets/f/lagoon.c b/pkg/noun/jets/f/lagoon.c index 3aa27e63d3..3622165141 100644 --- a/pkg/noun/jets/f/lagoon.c +++ b/pkg/noun/jets/f/lagoon.c @@ -7,6 +7,7 @@ #include "softfloat.h" #include "softblas.h" +#include #include union half { @@ -29,59 +30,75 @@ c3_d c[2]; }; + // $?(%n %u %d %z %a) static inline void _set_rounding(c3_w a) { + // We could use SoftBLAS set_rounding() to set the SoftFloat + // mode as well, but it's more explicit to do it here since + // we may use SoftFloat in any given Lagoon jet and we want + // you, dear developer, to see this set here. + fprintf(stderr, "%x %c\n", a, a); switch ( a ) { default: u3m_bail(c3__fail); break; + // %n - near case c3__n: softfloat_roundingMode = softfloat_round_near_even; + softblas_roundingMode = 'n'; break; + // %z - zero case c3__z: softfloat_roundingMode = softfloat_round_minMag; + softblas_roundingMode = 'z'; break; + // %u - up case c3__u: softfloat_roundingMode = softfloat_round_max; + softblas_roundingMode = 'u'; break; + // %d - down case c3__d: softfloat_roundingMode = softfloat_round_min; + softblas_roundingMode = 'd'; + break; + // %a - away + case c3__a: + softfloat_roundingMode = softfloat_round_near_maxMag; + softblas_roundingMode = 'a'; break; } } /* shape */ - static inline uint64_t _get_shape(u3_noun shape) + static inline uint64_t _get_length(u3_noun shape) { - uint64_t res = 1; + uint64_t len = 1; while (u3_nul != shape) { - res = res * u3h(shape); + len = len * u3h(shape); shape = u3t(shape); } - return res; + return len; } -/* add +/* add - axpy = 1*x+y */ u3_noun u3qf_la_add_real(u3_noun a_data, u3_noun b_data, u3_noun shape, - u3_noun bloq, - u3_noun rnd) + u3_noun bloq) { - - fprintf(stderr, ">> u3qf_la_add_real\n"); - - // Unpack the data as a byte array for SoftBLAS. - uint64_t len_a = _get_shape(shape); - uint8_t* a_bytes = (uint8_t*)malloc(len_a*sizeof(uint8_t)); - u3r_bytes(0, len_a, a_bytes, a_data); - uint8_t* b_bytes = (uint8_t*)malloc(len_a*sizeof(uint8_t)); - u3r_bytes(0, len_a, b_bytes, b_data); + // Unpack the data as a byte array. We assume total length < 2**64. + uint64_t len_a = _get_length(shape); + uint64_t siz_a = len_a * bloq; + uint8_t* a_bytes = (uint8_t*)u3a_malloc(siz_a*sizeof(uint8_t)); + u3r_bytes(0, siz_a, a_bytes, a_data); + uint8_t* b_bytes = (uint8_t*)u3a_malloc(siz_a*sizeof(uint8_t)); + u3r_bytes(0, siz_a, b_bytes, b_data); u3_noun r_data; @@ -91,55 +108,145 @@ haxpy(len_a, (float16_t){SB_REAL16_ONE}, (float16_t*)a_bytes, 1, (float16_t*)b_bytes, 1); // Unpack the result back into a noun. - r_data = u3i_bytes(len_a, b_bytes); + r_data = u3i_bytes(siz_a*sizeof(uint8_t), b_bytes); // Clean up. - free(a_bytes); - free(b_bytes); + u3a_free(a_bytes); + u3a_free(b_bytes); - return u3nc(a_data, r_data); - break; + return r_data; case 5: saxpy(len_a, (float32_t){SB_REAL32_ONE}, (float32_t*)a_bytes, 1, (float32_t*)b_bytes, 1); // Unpack the result back into a noun. - r_data = u3i_bytes(len_a, b_bytes); + r_data = u3i_bytes(siz_a*sizeof(uint8_t), b_bytes); // Clean up. - free(a_bytes); - free(b_bytes); + u3a_free(a_bytes); + u3a_free(b_bytes); - return u3nc(a_data, r_data); - break; + return r_data; case 6: daxpy(len_a, (float64_t){SB_REAL64_ONE}, (float64_t*)a_bytes, 1, (float64_t*)b_bytes, 1); // Unpack the result back into a noun. - r_data = u3i_bytes(len_a, b_bytes); + r_data = u3i_bytes(siz_a*sizeof(uint8_t), b_bytes); // Clean up. - free(a_bytes); - free(b_bytes); + u3a_free(a_bytes); + u3a_free(b_bytes); - return u3nc(a_data, r_data); - break; + return r_data; case 7: qaxpy(len_a, (float128_t){SB_REAL128L_ONE,SB_REAL128U_ONE}, (float128_t*)a_bytes, 1, (float128_t*)b_bytes, 1); // Unpack the result back into a noun. - r_data = u3i_bytes(len_a, b_bytes); + r_data = u3i_bytes(siz_a*sizeof(uint8_t), b_bytes); + + // Clean up. + u3a_free(a_bytes); + u3a_free(b_bytes); + + return r_data; + + default: + u3a_free(a_bytes); + u3a_free(b_bytes); + + return u3_none; + } + } + +/* mmul +*/ + u3_noun + u3qf_la_mmul_real(u3_noun a_data, + u3_noun b_data, + u3_noun a_shape, + u3_noun b_shape, + u3_noun bloq) + { + // Unpack the data as a byte array. We assume total length < 2**64. + uint64_t M = u3h(a_shape); + uint64_t Na = u3h(u3t(a_shape)); + uint64_t Nb = u3h(b_shape); + uint64_t P = u3h(u3t(b_shape)); + + assert(u3_nul == u3t(u3t(a_shape))); + assert(Na == Nb); + uint64_t N = Na; + assert(u3_nul == u3t(u3t(b_shape))); + + uint8_t* a_bytes = (uint8_t*)u3a_malloc((M*N)*sizeof(uint8_t)); + u3r_bytes(0, M*N, a_bytes, a_data); + uint8_t* b_bytes = (uint8_t*)u3a_malloc((N*P)*sizeof(uint8_t)); + u3r_bytes(0, N*P, b_bytes, b_data); + uint8_t* c_bytes = (uint8_t*)u3a_malloc((M*P)*sizeof(uint8_t)); + + u3_noun r_data; + + // Switch on the block size. + switch (bloq) { + case 4: + hgemm('N', 'N', M, N, P, (float16_t){SB_REAL16_ONE}, (float16_t*)a_bytes, N, (float16_t*)b_bytes, N, (float16_t){SB_REAL16_ZERO}, (float16_t*)c_bytes, P); + + // Unpack the result back into a noun. + r_data = u3i_bytes(M*P, c_bytes); + + // Clean up. + u3a_free(a_bytes); + u3a_free(b_bytes); + u3a_free(c_bytes); + + return u3nc(u3nq(u3nl(M, P, u3_none), bloq, c3__real, u3_nul), r_data); + + case 5: + sgemm('N', 'N', M, N, P, (float32_t){SB_REAL32_ONE}, (float32_t*)a_bytes, N, (float32_t*)b_bytes, N, (float32_t){SB_REAL32_ZERO}, (float32_t*)c_bytes, P); + + // Unpack the result back into a noun. + r_data = u3i_bytes(M*P, c_bytes); + + // Clean up. + u3a_free(a_bytes); + u3a_free(b_bytes); + u3a_free(c_bytes); + + return u3nc(u3nq(u3nl(M, P, u3_none), bloq, c3__real, u3_nul), r_data); + + case 6: + dgemm('N', 'N', M, N, P, (float64_t){SB_REAL64_ONE}, (float64_t*)a_bytes, N, (float64_t*)b_bytes, N, (float64_t){SB_REAL64_ZERO}, (float64_t*)c_bytes, P); + + // Unpack the result back into a noun. + r_data = u3i_bytes(M*P, c_bytes); // Clean up. - free(a_bytes); - free(b_bytes); + u3a_free(a_bytes); + u3a_free(b_bytes); + u3a_free(c_bytes); - return u3nc(a_data, r_data); - break; + return u3nc(u3nq(u3nl(M, P, u3_none), bloq, c3__real, u3_nul), r_data); + + case 7: + qgemm('N', 'N', M, N, P, (float128_t){SB_REAL128L_ONE,SB_REAL128U_ONE}, (float128_t*)a_bytes, N, (float128_t*)b_bytes, N, (float128_t){SB_REAL128L_ZERO,SB_REAL128U_ZERO}, (float128_t*)c_bytes, P); + + // Unpack the result back into a noun. + r_data = u3i_bytes(M*P, c_bytes); + + // Clean up. + u3a_free(a_bytes); + u3a_free(b_bytes); + u3a_free(c_bytes); + + return u3nc(u3nq(u3nl(M, P, u3_none), bloq, c3__real, u3_nul), r_data); default: + u3a_free(a_bytes); + u3a_free(b_bytes); + u3a_free(c_bytes); + return u3_none; } } @@ -181,14 +288,87 @@ c3n == u3r_sing(a_bloq, b_bloq) || c3n == u3r_sing(a_kind, b_kind) || // fxp does not need to match so no check - c3n == u3r_mean(cor, 31, &rnd, 0) + c3n == u3r_mean(cor, 30, &rnd, 0) + ) + { + return u3m_bail(c3__exit); + } else { + switch (a_kind) { + case c3__real: + _set_rounding(rnd); + u3_noun r_data = u3qf_la_add_real(a_data, b_data, a_shape, a_bloq); + return u3nc(u3nq(a_shape, a_bloq, a_kind, a_fxp), r_data); + break; + + // case c3__int2: + // return u3qf_la_add_int2(a_data, b_data, a_shape, a_bloq); + + // case c3__uint: + // return u3qf_la_add_uint(a_data, b_data, a_shape, a_bloq); + + // case c3__cplx: + // _set_rounding(rnd); + // return u3qf_la_add_cplx(a_data, b_data, a_shape, a_bloq); + + // case c3__unum: + // return u3qf_la_add_unum(a_data, b_data, a_shape, a_bloq); + + // case c3__fixp: + // return u3qf_la_add_fixp(a_data, b_data, a_shape, a_bloq); + + default: + return u3_none; + } + } + } + } + + u3_noun + u3wf_la_mmul(u3_noun cor) + { + // Each argument is a ray, [=meta data=@ux] + u3_noun a_meta, a_data, + b_meta, b_data; + + if ( c3n == u3r_mean(cor, + u3x_sam_4, &a_meta, + u3x_sam_5, &a_data, + u3x_sam_6, &b_meta, + u3x_sam_7, &b_data, + 0) || + c3n == u3ud(a_data) || + c3n == u3ud(b_data) ) + { + return u3m_bail(c3__exit); + } else { + u3_noun a_shape, a_bloq, a_kind, a_fxp, + b_shape, b_bloq, b_kind, b_fxp, + rnd; + if ( c3n == u3r_mean(a_meta, + 2, &a_shape, + 6, &a_bloq, + 14, &a_kind, + 15, &a_fxp, + 0) || + c3n == u3r_mean(b_meta, + 2, &b_shape, + 6, &b_bloq, + 14, &b_kind, + 15, &b_fxp, + 0) || + c3n == u3r_sing(a_bloq, b_bloq) || + c3n == u3r_sing(a_kind, b_kind) || + // fxp does not need to match so no check + c3n == u3r_mean(cor, 30, &rnd, 0) ) { return u3m_bail(c3__exit); } else { switch (a_kind) { case c3__real: - return u3qf_la_add_real(a_data, b_data, a_shape, a_bloq, rnd); + _set_rounding(rnd); + return u3qf_la_mmul_real(a_data, b_data, a_shape, b_shape, a_bloq); + break; // case c3__int2: // return u3qf_la_add_int2(a_data, b_data, a_shape, a_bloq); diff --git a/pkg/noun/jets/q.h b/pkg/noun/jets/q.h index c7cb5ccfd0..ddeed9afcf 100644 --- a/pkg/noun/jets/q.h +++ b/pkg/noun/jets/q.h @@ -247,7 +247,8 @@ u3_noun u3qfp_nepo(u3_noun, u3_noun); u3_noun u3qfp_rake(u3_noun); - u3_noun u3qf_la_add_real(u3_noun, u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qf_la_add_real(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qf_la_mmul_real(u3_noun, u3_noun, u3_noun, u3_noun, u3_noun); # define u3qfu_van_fan 28 # define u3qfu_van_rib 58 diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index b6597ffa7f..e70c4073f5 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -2152,8 +2152,10 @@ static u3j_core _139_hex_json_d[] = static u3j_core _139_sep_d[] = */ static u3j_harm _139_hex__lagoon_add_a[] = {{".2", u3wf_la_add}, {}}; +static u3j_harm _139_hex__lagoon_mmul_a[] = {{".2", u3wf_la_mmul}, {}}; static u3j_core _139_hex__la_core_d[] = - { { "add-rays", 7, _139_hex__lagoon_add_a, 0, no_hashes }, + { { "add-rays", 7, _139_hex__lagoon_add_a, 0, no_hashes }, + { "mmul", 7, _139_hex__lagoon_mmul_a, 0, no_hashes }, {} }; diff --git a/pkg/noun/jets/w.h b/pkg/noun/jets/w.h index 5cc40f1975..23aaab9938 100644 --- a/pkg/noun/jets/w.h +++ b/pkg/noun/jets/w.h @@ -332,5 +332,6 @@ u3_noun u3wfu_rest(u3_noun); u3_noun u3wf_la_add(u3_noun); + u3_noun u3wf_la_mmul(u3_noun); #endif /* ifndef U3_JETS_W_H */ From 171fa5c121c24f5b2b23dfb0818c9f4da534a721 Mon Sep 17 00:00:00 2001 From: Sigilante Date: Tue, 5 Mar 2024 16:24:07 -0600 Subject: [PATCH 061/430] Post ++sub jet. --- pkg/noun/jets/f/lagoon.c | 320 ++++++++++++++++++++++++++++----------- pkg/noun/jets/q.h | 1 + pkg/noun/jets/tree.c | 2 + pkg/noun/jets/w.h | 1 + 4 files changed, 238 insertions(+), 86 deletions(-) diff --git a/pkg/noun/jets/f/lagoon.c b/pkg/noun/jets/f/lagoon.c index 3622165141..07e0e5dd13 100644 --- a/pkg/noun/jets/f/lagoon.c +++ b/pkg/noun/jets/f/lagoon.c @@ -87,74 +87,150 @@ /* add - axpy = 1*x+y */ u3_noun - u3qf_la_add_real(u3_noun a_data, - u3_noun b_data, + u3qf_la_add_real(u3_noun x_data, + u3_noun y_data, u3_noun shape, u3_noun bloq) { // Unpack the data as a byte array. We assume total length < 2**64. uint64_t len_a = _get_length(shape); uint64_t siz_a = len_a * bloq; - uint8_t* a_bytes = (uint8_t*)u3a_malloc(siz_a*sizeof(uint8_t)); - u3r_bytes(0, siz_a, a_bytes, a_data); - uint8_t* b_bytes = (uint8_t*)u3a_malloc(siz_a*sizeof(uint8_t)); - u3r_bytes(0, siz_a, b_bytes, b_data); + uint8_t* x_bytes = (uint8_t*)u3a_malloc(siz_a*sizeof(uint8_t)); + u3r_bytes(0, siz_a, x_bytes, x_data); + uint8_t* y_bytes = (uint8_t*)u3a_malloc(siz_a*sizeof(uint8_t)); + u3r_bytes(0, siz_a, y_bytes, y_data); u3_noun r_data; // Switch on the block size. switch (bloq) { case 4: - haxpy(len_a, (float16_t){SB_REAL16_ONE}, (float16_t*)a_bytes, 1, (float16_t*)b_bytes, 1); + haxpy(len_a, (float16_t){SB_REAL16_ONE}, (float16_t*)x_bytes, 1, (float16_t*)y_bytes, 1); // Unpack the result back into a noun. - r_data = u3i_bytes(siz_a*sizeof(uint8_t), b_bytes); + r_data = u3i_bytes(siz_a*sizeof(uint8_t), y_bytes); // Clean up. - u3a_free(a_bytes); - u3a_free(b_bytes); + u3a_free(x_bytes); + u3a_free(y_bytes); return r_data; case 5: - saxpy(len_a, (float32_t){SB_REAL32_ONE}, (float32_t*)a_bytes, 1, (float32_t*)b_bytes, 1); + saxpy(len_a, (float32_t){SB_REAL32_ONE}, (float32_t*)x_bytes, 1, (float32_t*)y_bytes, 1); // Unpack the result back into a noun. - r_data = u3i_bytes(siz_a*sizeof(uint8_t), b_bytes); + r_data = u3i_bytes(siz_a*sizeof(uint8_t), y_bytes); // Clean up. - u3a_free(a_bytes); - u3a_free(b_bytes); + u3a_free(x_bytes); + u3a_free(y_bytes); return r_data; case 6: - daxpy(len_a, (float64_t){SB_REAL64_ONE}, (float64_t*)a_bytes, 1, (float64_t*)b_bytes, 1); + daxpy(len_a, (float64_t){SB_REAL64_ONE}, (float64_t*)x_bytes, 1, (float64_t*)y_bytes, 1); // Unpack the result back into a noun. - r_data = u3i_bytes(siz_a*sizeof(uint8_t), b_bytes); + r_data = u3i_bytes(siz_a*sizeof(uint8_t), y_bytes); // Clean up. - u3a_free(a_bytes); - u3a_free(b_bytes); + u3a_free(x_bytes); + u3a_free(y_bytes); return r_data; case 7: - qaxpy(len_a, (float128_t){SB_REAL128L_ONE,SB_REAL128U_ONE}, (float128_t*)a_bytes, 1, (float128_t*)b_bytes, 1); + qaxpy(len_a, (float128_t){SB_REAL128L_ONE,SB_REAL128U_ONE}, (float128_t*)x_bytes, 1, (float128_t*)y_bytes, 1); // Unpack the result back into a noun. - r_data = u3i_bytes(siz_a*sizeof(uint8_t), b_bytes); + r_data = u3i_bytes(siz_a*sizeof(uint8_t), y_bytes); // Clean up. - u3a_free(a_bytes); - u3a_free(b_bytes); + u3a_free(x_bytes); + u3a_free(y_bytes); return r_data; default: - u3a_free(a_bytes); - u3a_free(b_bytes); + u3a_free(x_bytes); + u3a_free(y_bytes); + + return u3_none; + } + } + +/* sub - axpy = -1*y+x +*/ + u3_noun + u3qf_la_sub_real(u3_noun x_data, + u3_noun y_data, + u3_noun shape, + u3_noun bloq) + { + // Unpack the data as a byte array. We assume total length < 2**64. + uint64_t len_a = _get_length(shape); + uint64_t siz_a = len_a * bloq; + uint8_t* x_bytes = (uint8_t*)u3a_malloc(siz_a*sizeof(uint8_t)); + u3r_bytes(0, siz_a, x_bytes, y_data); + uint8_t* y_bytes = (uint8_t*)u3a_malloc(siz_a*sizeof(uint8_t)); + u3r_bytes(0, siz_a, y_bytes, x_data); + + u3_noun r_data; + + // Switch on the block size. + switch (bloq) { + case 4: + haxpy(len_a, (float16_t){SB_REAL16_NEGONE}, (float16_t*)x_bytes, 1, (float16_t*)y_bytes, 1); + + // Unpack the result back into a noun. + r_data = u3i_bytes(siz_a*sizeof(uint8_t), y_bytes); + + // Clean up. + u3a_free(x_bytes); + u3a_free(y_bytes); + + return r_data; + + case 5: + saxpy(len_a, (float32_t){SB_REAL32_NEGONE}, (float32_t*)x_bytes, 1, (float32_t*)y_bytes, 1); + + // Unpack the result back into a noun. + r_data = u3i_bytes(siz_a*sizeof(uint8_t), y_bytes); + + // Clean up. + u3a_free(x_bytes); + u3a_free(y_bytes); + + return r_data; + + case 6: + daxpy(len_a, (float64_t){SB_REAL64_NEGONE}, (float64_t*)x_bytes, 1, (float64_t*)y_bytes, 1); + + // Unpack the result back into a noun. + r_data = u3i_bytes(siz_a*sizeof(uint8_t), y_bytes); + + // Clean up. + u3a_free(x_bytes); + u3a_free(y_bytes); + + return r_data; + + case 7: + qaxpy(len_a, (float128_t){SB_REAL128L_NEGONE,SB_REAL128U_NEGONE}, (float128_t*)x_bytes, 1, (float128_t*)y_bytes, 1); + + // Unpack the result back into a noun. + r_data = u3i_bytes(siz_a*sizeof(uint8_t), y_bytes); + + // Clean up. + u3a_free(x_bytes); + u3a_free(y_bytes); + + return r_data; + + default: + u3a_free(x_bytes); + u3a_free(y_bytes); return u3_none; } @@ -163,27 +239,27 @@ /* mmul */ u3_noun - u3qf_la_mmul_real(u3_noun a_data, - u3_noun b_data, - u3_noun a_shape, - u3_noun b_shape, + u3qf_la_mmul_real(u3_noun x_data, + u3_noun y_data, + u3_noun x_shape, + u3_noun y_shape, u3_noun bloq) { // Unpack the data as a byte array. We assume total length < 2**64. - uint64_t M = u3h(a_shape); - uint64_t Na = u3h(u3t(a_shape)); - uint64_t Nb = u3h(b_shape); - uint64_t P = u3h(u3t(b_shape)); + uint64_t M = u3h(x_shape); + uint64_t Na = u3h(u3t(x_shape)); + uint64_t Nb = u3h(y_shape); + uint64_t P = u3h(u3t(y_shape)); - assert(u3_nul == u3t(u3t(a_shape))); + assert(u3_nul == u3t(u3t(x_shape))); assert(Na == Nb); uint64_t N = Na; - assert(u3_nul == u3t(u3t(b_shape))); + assert(u3_nul == u3t(u3t(y_shape))); - uint8_t* a_bytes = (uint8_t*)u3a_malloc((M*N)*sizeof(uint8_t)); - u3r_bytes(0, M*N, a_bytes, a_data); - uint8_t* b_bytes = (uint8_t*)u3a_malloc((N*P)*sizeof(uint8_t)); - u3r_bytes(0, N*P, b_bytes, b_data); + uint8_t* x_bytes = (uint8_t*)u3a_malloc((M*N)*sizeof(uint8_t)); + u3r_bytes(0, M*N, x_bytes, x_data); + uint8_t* y_bytes = (uint8_t*)u3a_malloc((N*P)*sizeof(uint8_t)); + u3r_bytes(0, N*P, y_bytes, y_data); uint8_t* c_bytes = (uint8_t*)u3a_malloc((M*P)*sizeof(uint8_t)); u3_noun r_data; @@ -191,60 +267,60 @@ // Switch on the block size. switch (bloq) { case 4: - hgemm('N', 'N', M, N, P, (float16_t){SB_REAL16_ONE}, (float16_t*)a_bytes, N, (float16_t*)b_bytes, N, (float16_t){SB_REAL16_ZERO}, (float16_t*)c_bytes, P); + hgemm('N', 'N', M, N, P, (float16_t){SB_REAL16_ONE}, (float16_t*)x_bytes, N, (float16_t*)y_bytes, N, (float16_t){SB_REAL16_ZERO}, (float16_t*)c_bytes, P); // Unpack the result back into a noun. r_data = u3i_bytes(M*P, c_bytes); // Clean up. - u3a_free(a_bytes); - u3a_free(b_bytes); + u3a_free(x_bytes); + u3a_free(y_bytes); u3a_free(c_bytes); return u3nc(u3nq(u3nl(M, P, u3_none), bloq, c3__real, u3_nul), r_data); case 5: - sgemm('N', 'N', M, N, P, (float32_t){SB_REAL32_ONE}, (float32_t*)a_bytes, N, (float32_t*)b_bytes, N, (float32_t){SB_REAL32_ZERO}, (float32_t*)c_bytes, P); + sgemm('N', 'N', M, N, P, (float32_t){SB_REAL32_ONE}, (float32_t*)x_bytes, N, (float32_t*)y_bytes, N, (float32_t){SB_REAL32_ZERO}, (float32_t*)c_bytes, P); // Unpack the result back into a noun. r_data = u3i_bytes(M*P, c_bytes); // Clean up. - u3a_free(a_bytes); - u3a_free(b_bytes); + u3a_free(x_bytes); + u3a_free(y_bytes); u3a_free(c_bytes); return u3nc(u3nq(u3nl(M, P, u3_none), bloq, c3__real, u3_nul), r_data); case 6: - dgemm('N', 'N', M, N, P, (float64_t){SB_REAL64_ONE}, (float64_t*)a_bytes, N, (float64_t*)b_bytes, N, (float64_t){SB_REAL64_ZERO}, (float64_t*)c_bytes, P); + dgemm('N', 'N', M, N, P, (float64_t){SB_REAL64_ONE}, (float64_t*)x_bytes, N, (float64_t*)y_bytes, N, (float64_t){SB_REAL64_ZERO}, (float64_t*)c_bytes, P); // Unpack the result back into a noun. r_data = u3i_bytes(M*P, c_bytes); // Clean up. - u3a_free(a_bytes); - u3a_free(b_bytes); + u3a_free(x_bytes); + u3a_free(y_bytes); u3a_free(c_bytes); return u3nc(u3nq(u3nl(M, P, u3_none), bloq, c3__real, u3_nul), r_data); case 7: - qgemm('N', 'N', M, N, P, (float128_t){SB_REAL128L_ONE,SB_REAL128U_ONE}, (float128_t*)a_bytes, N, (float128_t*)b_bytes, N, (float128_t){SB_REAL128L_ZERO,SB_REAL128U_ZERO}, (float128_t*)c_bytes, P); + qgemm('N', 'N', M, N, P, (float128_t){SB_REAL128L_ONE,SB_REAL128U_ONE}, (float128_t*)x_bytes, N, (float128_t*)y_bytes, N, (float128_t){SB_REAL128L_ZERO,SB_REAL128U_ZERO}, (float128_t*)c_bytes, P); // Unpack the result back into a noun. r_data = u3i_bytes(M*P, c_bytes); // Clean up. - u3a_free(a_bytes); - u3a_free(b_bytes); + u3a_free(x_bytes); + u3a_free(y_bytes); u3a_free(c_bytes); return u3nc(u3nq(u3nl(M, P, u3_none), bloq, c3__real, u3_nul), r_data); default: - u3a_free(a_bytes); - u3a_free(b_bytes); + u3a_free(x_bytes); + u3a_free(y_bytes); u3a_free(c_bytes); return u3_none; @@ -255,36 +331,108 @@ u3wf_la_add(u3_noun cor) { // Each argument is a ray, [=meta data=@ux] - u3_noun a_meta, a_data, - b_meta, b_data; + u3_noun a_meta, x_data, + b_meta, y_data; + + if ( c3n == u3r_mean(cor, + u3x_sam_4, &a_meta, + u3x_sam_5, &x_data, + u3x_sam_6, &b_meta, + u3x_sam_7, &y_data, + 0) || + c3n == u3ud(x_data) || + c3n == u3ud(y_data) ) + { + return u3m_bail(c3__exit); + } else { + u3_noun x_shape, a_bloq, a_kind, a_fxp, + y_shape, b_bloq, b_kind, b_fxp, + rnd; + if ( c3n == u3r_mean(a_meta, + 2, &x_shape, + 6, &a_bloq, + 14, &a_kind, + 15, &a_fxp, + 0) || + c3n == u3r_mean(b_meta, + 2, &y_shape, + 6, &b_bloq, + 14, &b_kind, + 15, &b_fxp, + 0) || + c3n == u3r_sing(x_shape, y_shape) || + c3n == u3r_sing(a_bloq, b_bloq) || + c3n == u3r_sing(a_kind, b_kind) || + // fxp does not need to match so no check + c3n == u3r_mean(cor, 30, &rnd, 0) + ) + { + return u3m_bail(c3__exit); + } else { + switch (a_kind) { + case c3__real: + _set_rounding(rnd); + u3_noun r_data = u3qf_la_add_real(x_data, y_data, x_shape, a_bloq); + return u3nc(u3nq(x_shape, a_bloq, a_kind, a_fxp), r_data); + break; + + // case c3__int2: + // return u3qf_la_add_int2(x_data, y_data, x_shape, a_bloq); + + // case c3__uint: + // return u3qf_la_add_uint(x_data, y_data, x_shape, a_bloq); + + // case c3__cplx: + // _set_rounding(rnd); + // return u3qf_la_add_cplx(x_data, y_data, x_shape, a_bloq); + + // case c3__unum: + // return u3qf_la_add_unum(x_data, y_data, x_shape, a_bloq); + + // case c3__fixp: + // return u3qf_la_add_fixp(x_data, y_data, x_shape, a_bloq); + + default: + return u3_none; + } + } + } + } + + u3_noun + u3wf_la_sub(u3_noun cor) + { + // Each argument is a ray, [=meta data=@ux] + u3_noun a_meta, x_data, + b_meta, y_data; if ( c3n == u3r_mean(cor, u3x_sam_4, &a_meta, - u3x_sam_5, &a_data, + u3x_sam_5, &x_data, u3x_sam_6, &b_meta, - u3x_sam_7, &b_data, + u3x_sam_7, &y_data, 0) || - c3n == u3ud(a_data) || - c3n == u3ud(b_data) ) + c3n == u3ud(x_data) || + c3n == u3ud(y_data) ) { return u3m_bail(c3__exit); } else { - u3_noun a_shape, a_bloq, a_kind, a_fxp, - b_shape, b_bloq, b_kind, b_fxp, + u3_noun x_shape, a_bloq, a_kind, a_fxp, + y_shape, b_bloq, b_kind, b_fxp, rnd; if ( c3n == u3r_mean(a_meta, - 2, &a_shape, + 2, &x_shape, 6, &a_bloq, 14, &a_kind, 15, &a_fxp, 0) || c3n == u3r_mean(b_meta, - 2, &b_shape, + 2, &y_shape, 6, &b_bloq, 14, &b_kind, 15, &b_fxp, 0) || - c3n == u3r_sing(a_shape, b_shape) || + c3n == u3r_sing(x_shape, y_shape) || c3n == u3r_sing(a_bloq, b_bloq) || c3n == u3r_sing(a_kind, b_kind) || // fxp does not need to match so no check @@ -296,25 +444,25 @@ switch (a_kind) { case c3__real: _set_rounding(rnd); - u3_noun r_data = u3qf_la_add_real(a_data, b_data, a_shape, a_bloq); - return u3nc(u3nq(a_shape, a_bloq, a_kind, a_fxp), r_data); + u3_noun r_data = u3qf_la_sub_real(x_data, y_data, x_shape, a_bloq); + return u3nc(u3nq(x_shape, a_bloq, a_kind, a_fxp), r_data); break; // case c3__int2: - // return u3qf_la_add_int2(a_data, b_data, a_shape, a_bloq); + // return u3qf_la_sub_int2(x_data, y_data, x_shape, a_bloq); // case c3__uint: - // return u3qf_la_add_uint(a_data, b_data, a_shape, a_bloq); + // return u3qf_la_sub_uint(x_data, y_data, x_shape, a_bloq); // case c3__cplx: // _set_rounding(rnd); - // return u3qf_la_add_cplx(a_data, b_data, a_shape, a_bloq); + // return u3qf_la_sub_cplx(x_data, y_data, x_shape, a_bloq); // case c3__unum: - // return u3qf_la_add_unum(a_data, b_data, a_shape, a_bloq); + // return u3qf_la_sub_unum(x_data, y_data, x_shape, a_bloq); // case c3__fixp: - // return u3qf_la_add_fixp(a_data, b_data, a_shape, a_bloq); + // return u3qf_la_sub_fixp(x_data, y_data, x_shape, a_bloq); default: return u3_none; @@ -327,31 +475,31 @@ u3wf_la_mmul(u3_noun cor) { // Each argument is a ray, [=meta data=@ux] - u3_noun a_meta, a_data, - b_meta, b_data; + u3_noun a_meta, x_data, + b_meta, y_data; if ( c3n == u3r_mean(cor, u3x_sam_4, &a_meta, - u3x_sam_5, &a_data, + u3x_sam_5, &x_data, u3x_sam_6, &b_meta, - u3x_sam_7, &b_data, + u3x_sam_7, &y_data, 0) || - c3n == u3ud(a_data) || - c3n == u3ud(b_data) ) + c3n == u3ud(x_data) || + c3n == u3ud(y_data) ) { return u3m_bail(c3__exit); } else { - u3_noun a_shape, a_bloq, a_kind, a_fxp, - b_shape, b_bloq, b_kind, b_fxp, + u3_noun x_shape, a_bloq, a_kind, a_fxp, + y_shape, b_bloq, b_kind, b_fxp, rnd; if ( c3n == u3r_mean(a_meta, - 2, &a_shape, + 2, &x_shape, 6, &a_bloq, 14, &a_kind, 15, &a_fxp, 0) || c3n == u3r_mean(b_meta, - 2, &b_shape, + 2, &y_shape, 6, &b_bloq, 14, &b_kind, 15, &b_fxp, @@ -367,23 +515,23 @@ switch (a_kind) { case c3__real: _set_rounding(rnd); - return u3qf_la_mmul_real(a_data, b_data, a_shape, b_shape, a_bloq); + return u3qf_la_mmul_real(x_data, y_data, x_shape, y_shape, a_bloq); break; // case c3__int2: - // return u3qf_la_add_int2(a_data, b_data, a_shape, a_bloq); + // return u3qf_la_add_int2(x_data, y_data, x_shape, a_bloq); // case c3__uint: - // return u3qf_la_add_uint(a_data, b_data, a_shape, a_bloq); + // return u3qf_la_add_uint(x_data, y_data, x_shape, a_bloq); // case c3__cplx: - // return u3qf_la_add_cplx(a_data, b_data, a_shape, a_bloq, rnd); + // return u3qf_la_add_cplx(x_data, y_data, x_shape, a_bloq, rnd); // case c3__unum: - // return u3qf_la_add_unum(a_data, b_data, a_shape, a_bloq); + // return u3qf_la_add_unum(x_data, y_data, x_shape, a_bloq); // case c3__fixp: - // return u3qf_la_add_fixp(a_data, b_data, a_shape, a_bloq); + // return u3qf_la_add_fixp(x_data, y_data, x_shape, a_bloq); default: return u3_none; diff --git a/pkg/noun/jets/q.h b/pkg/noun/jets/q.h index ddeed9afcf..371cebb228 100644 --- a/pkg/noun/jets/q.h +++ b/pkg/noun/jets/q.h @@ -248,6 +248,7 @@ u3_noun u3qfp_rake(u3_noun); u3_noun u3qf_la_add_real(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qf_la_sub_real(u3_noun, u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_mmul_real(u3_noun, u3_noun, u3_noun, u3_noun, u3_noun); # define u3qfu_van_fan 28 diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index e70c4073f5..035dad2305 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -2152,9 +2152,11 @@ static u3j_core _139_hex_json_d[] = static u3j_core _139_sep_d[] = */ static u3j_harm _139_hex__lagoon_add_a[] = {{".2", u3wf_la_add}, {}}; +static u3j_harm _139_hex__lagoon_sub_a[] = {{".2", u3wf_la_sub}, {}}; static u3j_harm _139_hex__lagoon_mmul_a[] = {{".2", u3wf_la_mmul}, {}}; static u3j_core _139_hex__la_core_d[] = { { "add-rays", 7, _139_hex__lagoon_add_a, 0, no_hashes }, + { "sub-rays", 7, _139_hex__lagoon_sub_a, 0, no_hashes }, { "mmul", 7, _139_hex__lagoon_mmul_a, 0, no_hashes }, {} }; diff --git a/pkg/noun/jets/w.h b/pkg/noun/jets/w.h index 23aaab9938..f7d4ef6a53 100644 --- a/pkg/noun/jets/w.h +++ b/pkg/noun/jets/w.h @@ -332,6 +332,7 @@ u3_noun u3wfu_rest(u3_noun); u3_noun u3wf_la_add(u3_noun); + u3_noun u3wf_la_sub(u3_noun); u3_noun u3wf_la_mmul(u3_noun); #endif /* ifndef U3_JETS_W_H */ From 271d4e1dcad533e82f60f24d9a1df7f87411a269 Mon Sep 17 00:00:00 2001 From: parallels Date: Wed, 6 Mar 2024 10:43:56 -0500 Subject: [PATCH 062/430] xmas: fix heap corruption --- pkg/vere/io/blake.c | 71 +++++-- pkg/vere/io/blake.h | 8 +- pkg/vere/io/xmas.c | 433 ++++++++++++++++++++------------------ pkg/vere/io/xmas/bitset.c | 51 +++-- pkg/vere/io/xmas/bitset.h | 1 + pkg/vere/io/xmas/pact.c | 40 +--- 6 files changed, 322 insertions(+), 282 deletions(-) diff --git a/pkg/vere/io/blake.c b/pkg/vere/io/blake.c index 443872be31..13b8f41c0f 100644 --- a/pkg/vere/io/blake.c +++ b/pkg/vere/io/blake.c @@ -123,8 +123,8 @@ vec_make(c3_w siz_w) } -static c3_w -_vec_len(u3_raw_vec* vec_u) +c3_w +vec_len(u3_raw_vec* vec_u) { return vec_u->len_w; } @@ -181,11 +181,11 @@ vec_copy(u3_raw_vec* dst_u, u3_raw_vec* src_u) { dst_u->len_w = src_u->len_w; } -static void* -_vec_pop(u3_raw_vec* vec_u, c3_w idx_w) +void* +vec_pop(u3_raw_vec* vec_u, c3_w idx_w) { - if ( vec_u->len_w == 0 || idx_w >= vec_u->siz_w ) { - fprintf(stderr, "Failed pop\r\n"); + if ( vec_u->len_w == 0 || idx_w >= vec_u->len_w ) { + fprintf(stderr, "Failed pop (len: %u, idx: %u)\r\n", vec_u->len_w, idx_w); return NULL; } void* hit_p = vec_u->vod_p[idx_w]; @@ -199,13 +199,13 @@ _vec_pop(u3_raw_vec* vec_u, c3_w idx_w) void* vec_popf(u3_raw_vec* vec_u) { - return _vec_pop(vec_u, 0); + return vec_pop(vec_u, 0); } static void* _vec_popl(u3_raw_vec* vec_u) { - return _vec_pop(vec_u, vec_u->len_w - 1); + return vec_pop(vec_u, vec_u->len_w - 1); } @@ -380,8 +380,8 @@ _parent_node(c3_y sin_y[BLAKE3_OUT_LEN], c3_y dex_y[BLAKE3_OUT_LEN], c3_y key_y[ static blake_node* _parent_hash(blake_node* sin_u, blake_node* dex_u) { - c3_y* sin_y = c3_calloc(BLAKE3_OUT_LEN); - c3_y* dex_y = c3_calloc(BLAKE3_OUT_LEN); + c3_y sin_y[BLAKE3_OUT_LEN]; + c3_y dex_y[BLAKE3_OUT_LEN]; make_chain_value(sin_y, sin_u); make_chain_value(dex_y, dex_u); return _parent_node(sin_y, dex_y, IV, 0); @@ -448,7 +448,7 @@ _bao_build(c3_d num_d, c3_y has_y[64], u3_vec(c3_w[8])* pof_u, u3_vec(pair)* par recurse_blake_subtree(sub_u, nod_u, par_u); _root_hash(has_y, nod_u); - if ( _vec_len(par_u) != 0 ) { + if ( vec_len(par_u) != 0 ) { for ( c3_w i_w = _bitlen(num_d - 1); i_w > 1; i_w-- ) { blake_pair* pir_u = vec_popf(par_u); vec_append(pof_u, pir_u->dex_y); @@ -456,8 +456,8 @@ _bao_build(c3_d num_d, c3_y has_y[64], u3_vec(c3_w[8])* pof_u, u3_vec(pair)* par blake_pair* pir_u = vec_popf(par_u); vec_append(pof_u, pir_u->dex_y); vec_append(pof_u, pir_u->sin_y); - for ( c3_w i_w = 0; i_w < (_vec_len(pof_u) >> 1); i_w++ ) { - c3_w jay_w = _vec_len(pof_u) - i_w - 1; + for ( c3_w i_w = 0; i_w < (vec_len(pof_u) >> 1); i_w++ ) { + c3_w jay_w = vec_len(pof_u) - i_w - 1; _vec_swap(pof_u, i_w, jay_w); } } @@ -474,6 +474,7 @@ _pop_leaf(c3_y out_y[BLAKE3_OUT_LEN], blake_bao* bao_u) { c3_y* res_y = vec_popf(&bao_u->que_u); memcpy(out_y, res_y, BLAKE3_OUT_LEN); + c3_free(res_y); } static void @@ -487,6 +488,7 @@ _pop_parent(c3_y out_y[BLAKE3_OUT_LEN], blake_bao* bao_u) { c3_y* res_y = _vec_popl(&bao_u->sta_u); memcpy(out_y, res_y, BLAKE3_OUT_LEN); + c3_free(res_y); } static void @@ -515,20 +517,22 @@ _push_leaves(blake_bao* bao_u, c3_y lef_y[BLAKE3_OUT_LEN], c3_y rig_y[BLAKE3_OUT static c3_o _veri_check_leaf(blake_bao* bao_u, c3_y* dat_y, c3_w dat_w) { - if ( _vec_len(&bao_u->que_u) > 0 ) { + if ( vec_len(&bao_u->que_u) > 0 ) { c3_y out_y[BLAKE3_OUT_LEN]; _pop_leaf(out_y, bao_u); c3_y cav_y[BLAKE3_OUT_LEN]; blake_node* nod_u = blake_leaf_hash(dat_y, dat_w, bao_u->con_w); make_chain_value(cav_y, nod_u); bao_u->con_w++; + c3_free(nod_u); return 0 == memcmp(cav_y, out_y, BLAKE3_OUT_LEN) ? c3y : c3n; - } else if ( _vec_len(&bao_u->sta_u) > 0 ) { + } else if ( vec_len(&bao_u->sta_u) > 0 ) { c3_y out_y[BLAKE3_OUT_LEN]; _pop_parent(out_y, bao_u); c3_y cav_y[BLAKE3_OUT_LEN]; blake_node* nod_u = blake_leaf_hash(dat_y, dat_w, bao_u->con_w); make_chain_value(cav_y, nod_u); + c3_free(nod_u); bao_u->con_w++; return 0 == memcmp(cav_y, out_y, BLAKE3_OUT_LEN) ? c3y : c3n; } @@ -538,7 +542,7 @@ _veri_check_leaf(blake_bao* bao_u, c3_y* dat_y, c3_w dat_w) static c3_o _veri_check_pair(blake_bao* bao_u, blake_pair* par_u) { - if ( _vec_len(&bao_u->sta_u) == 0 ) { + if ( vec_len(&bao_u->sta_u) == 0 ) { fprintf(stderr, "bailing empty stack\r\n"); return c3n; } @@ -548,6 +552,7 @@ _veri_check_pair(blake_bao* bao_u, blake_pair* par_u) blake_node* nod_u = _parent_node(par_u->sin_y, par_u->dex_y, IV, 0); c3_y cav_y[BLAKE3_OUT_LEN]; make_chain_value(cav_y, nod_u); + c3_free(nod_u); return _(memcmp(par_y, cav_y, BLAKE3_OUT_LEN) == 0); } @@ -565,7 +570,7 @@ blake_bao_make(c3_w num_w, u3_vec(c3_y[BLAKE3_OUT_LEN])* pof_u) bao_u->num_w = num_w; bao_u->con_w = 0; bao_u->sub_u = (blake_subtree){0,1}; - if ( _vec_len(pof_u) == 0 ) { + if ( vec_len(pof_u) == 0 ) { if ( num_w < 2 ) { c3_free(bao_u); return NULL; @@ -584,11 +589,29 @@ blake_bao_make(c3_w num_w, u3_vec(c3_y[BLAKE3_OUT_LEN])* pof_u) return bao_u; } +static void +_free(void* vod_p, c3_w i_w) +{ + c3_free(vod_p); +} + +void +blake_bao_free(blake_bao* bao_u) +{ + _vec_each(&bao_u->sta_u, _free); + _vec_each(&bao_u->que_u, _free); + c3_free(bao_u); +} + static void _push_parents(blake_bao* bao_u, blake_pair* par_u) { - _push_parent(bao_u, par_u->dex_y); - _push_parent(bao_u, par_u->sin_y); + c3_y* fst_y = c3_calloc(BLAKE3_OUT_LEN); + c3_y* snd_y = c3_calloc(BLAKE3_OUT_LEN); + memcpy(fst_y, par_u->dex_y, BLAKE3_OUT_LEN); + memcpy(snd_y, par_u->sin_y, BLAKE3_OUT_LEN); + _push_parent(bao_u, fst_y); + _push_parent(bao_u, snd_y); } c3_y @@ -605,7 +628,11 @@ blake_bao_verify(blake_bao* bao_u, c3_y* dat_y, c3_w dat_w, blake_pair* par_u) } _verifier_next(bao_u); if ( _at_leaf(&bao_u->sub_u) == c3y ) { - _push_leaves(bao_u, par_u->sin_y, par_u->dex_y); + c3_y* fst_y = c3_calloc(BLAKE3_OUT_LEN); + memcpy(fst_y, par_u->sin_y, BLAKE3_OUT_LEN); + c3_y* snd_y = c3_calloc(BLAKE3_OUT_LEN); + memcpy(snd_y, par_u->dex_y, BLAKE3_OUT_LEN); + _push_leaves(bao_u, fst_y, snd_y); } else { _push_parents(bao_u, par_u); } @@ -690,7 +717,7 @@ static void _test_bao() // pl // _log_verifier(&bao_u); - if ( i_w < _vec_len(par_u) ) { + if ( i_w < vec_len(par_u) ) { blake_pair* pir_u = _vec_get(blake_pair, par_u, i_w); if ( _veri_check_pair(&bao_u, pir_u) == c3n ) { fprintf(stderr, "check pair failed (%u, %u)", i_w, num_w); @@ -754,7 +781,7 @@ static void _test_vec() { fprintf(stderr, "popped %u", *thr_w); - if ( 0 != _vec_len(vec_u) ) { + if ( 0 != vec_len(vec_u) ) { fprintf(stderr, "Vec should be empty\r\n"); exit(1); } diff --git a/pkg/vere/io/blake.h b/pkg/vere/io/blake.h index bb53683d18..3fd3d0a3d5 100644 --- a/pkg/vere/io/blake.h +++ b/pkg/vere/io/blake.h @@ -12,12 +12,12 @@ #define u3_vec(type) u3_raw_vec -enum bao_ingest_result { +typedef enum _bao_ingest_result { BAO_BAD_ORDER = 1, BAO_FAILED = 2, BAO_GOOD = 3, BAO_DONE = 4, -}; +} bao_ingest_result; typedef struct _blake_pair { c3_y sin_y[BLAKE3_OUT_LEN]; @@ -52,9 +52,12 @@ typedef struct _blake_bao { } blake_bao; void* vec_popf(u3_raw_vec*); +void* vec_pop(u3_raw_vec*, c3_w); void vec_init(u3_raw_vec*, c3_w); +c3_w vec_len(u3_raw_vec*); + void vec_append(u3_raw_vec*, void*); u3_raw_vec* vec_make(c3_w); @@ -65,6 +68,7 @@ void vec_drop(u3_raw_vec*); blake_node* blake_leaf_hash(c3_y* dat_y, c3_w dat_w, c3_d con_d); blake_bao* blake_bao_make(c3_w, u3_raw_vec* pof_u); +void blake_bao_free(blake_bao*); void make_chain_value(c3_y*, blake_node*); diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/xmas.c index bdda7ed375..196896f2a5 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/xmas.c @@ -3,6 +3,7 @@ #include "vere.h" #include "ivory.h" + #include "noun.h" #include "ur.h" #include "cubic.h" @@ -22,7 +23,7 @@ c3_o dop_o = c3n; -#define XMAS_DEBUG c3y +// #define XMAS_DEBUG c3y //#define XMAS_TEST #define RED_TEXT "\033[0;31m" #define DEF_TEXT "\033[0m" @@ -105,10 +106,12 @@ typedef struct _u3_peer_last { c3_d son_d; // time of sponsor check } u3_peer_last; -typedef struct _u3_buf { +typedef struct _u3_misord_buf { c3_y* fra_y; - c3_w fra_w; -} u3_buf; + c3_w len_w; + c3_w num_w; + blake_pair* par_u; +} u3_misord_buf; struct _u3_xmas; @@ -122,9 +125,6 @@ typedef struct _u3_pend_req { c3_w nex_w; // number of the next fragment to be sent c3_w tot_w; // total number of fragments expected uv_timer_t tim_u; // timehandler - u3_xmas_pict* pic_u; // preallocated request packet - u3_pact_stat* wat_u; // ((mop @ud packet-state) lte) - u3_bitset was_u; // ((mop @ud packet-state) lte) c3_y* dat_y; // ((mop @ud *) lte) c3_w len_w; c3_w lef_w; // lowest fragment number currently in flight/pending @@ -134,6 +134,10 @@ typedef struct _u3_pend_req { u3_gage* gag_u; // congestion control u3_vec(u3_buf) mis_u; // misordered blake hash blake_bao* bao_u; // blake verifier + u3_xmas_pict* pic_u; // preallocated request packet + u3_pact_stat* wat_u; // ((mop @ud packet-state) lte) + u3_bitset was_u; // ((mop @ud packet-state) lte) + c3_y pad_y[64]; } u3_pend_req; typedef struct _u3_czar_info { @@ -273,7 +277,7 @@ _log_czar_info(u3_czar_info* zar_u) u3_noun pip = u3dc("scot", c3__if, zar_u->pip_w); u3l_log("IP: %s", u3r_string(pip)); u3l_log("time: %u", zar_u->tim_t); - u3l_log("dns: %s", zar_u->dns_c); + u3l_log("dns: %s", zar_u->dns_c != NULL ? zar_u->dns_c : "NO DNS"); u3l_log("pending: %u", u3r_word(0, u3do("lent", zar_u->pen))); } } @@ -285,6 +289,7 @@ _log_pend_req(u3_pend_req* req_u) u3l_log("pending request was NULL"); return; } + u3l_log("have: %u", req_u->len_w); u3l_log("next: %u", req_u->nex_w); u3l_log("total: %u", req_u->tot_w); u3l_log("gage: %c", req_u->gag_u == NULL ? 'n' : 'y'); @@ -394,6 +399,12 @@ _xmas_encode_path(c3_w len_w, c3_y* buf_y) return pro; } +static void +_xmas_free_pict(u3_xmas_pict* pic_u) +{ + xmas_free_pact(&pic_u->pac_u); + c3_free(pic_u); +} static u3_atom _dire_etch_ud(c3_d num_d) @@ -517,12 +528,16 @@ _xmas_del_request(u3_xmas* sam_u, u3_xmas_name* nam_u) { return; } u3_pend_req* req_u = u3to(u3_pend_req, req); - // xmas_free_pact(req_u->pac_u); - // _free_bitset(&req_u->was_u); - // c3_free(req_u->pac_u); - // c3_free(req_u->dat_y); - // u3a_free(req_u); + // u3l_log("wat_u %p", req_u->wat_u); + // u3l_log("was_u buf %p", req_u->was_u.buf_y); + uv_timer_stop(&req_u->tim_u); + _xmas_free_pict(req_u->pic_u); + c3_free(req_u->wat_u); + vec_free(&req_u->mis_u); + c3_free(req_u->dat_y); + blake_bao_free(req_u->bao_u); u3h_del(sam_u->req_p, key); + u3a_free(req_u); u3z(key); } @@ -533,19 +548,20 @@ _xmas_del_request(u3_xmas* sam_u, u3_xmas_name* nam_u) { * immediately freed * */ -static void +static u3_pend_req* _xmas_put_request(u3_xmas* sam_u, u3_xmas_name* nam_u, u3_pend_req* req_u) { u3_noun key = _xmas_request_key(nam_u); if ( req_u == NULL) { u3h_put(sam_u->req_p, key, u3_nul); u3z(key); - return; + return req_u; } u3_pend_req* old_u = _xmas_get_request(sam_u, nam_u); u3_pend_req* new_u = req_u; if ( old_u == NULL ) { - new_u = u3a_calloc(sizeof(u3_pend_req),1); + new_u = u3a_calloc(1, sizeof(u3_pend_req)); + // u3l_log("putting fresh req %p", new_u); memcpy(new_u, req_u, sizeof(u3_pend_req)); } else { new_u = old_u; @@ -556,15 +572,10 @@ _xmas_put_request(u3_xmas* sam_u, u3_xmas_name* nam_u, u3_pend_req* req_u) { u3_noun val = u3of(u3_pend_req, new_u); u3h_put(sam_u->req_p, key, val); u3z(key); + return new_u; } -/* _xmas_packet_timeout(): callback for packet timeout -*/ -static void -_xmas_packet_timeout(uv_timer_t* tim_u) { - u3_pend_req* req_u = (u3_pend_req*)tim_u->data; - u3l_log("%u packet timed out", req_u->old_w); -} + // congestion control update static void _xmas_handle_ack(u3_gage* gag_u, u3_pact_stat* pat_u) @@ -590,33 +601,27 @@ static void _xmas_handle_ack(u3_gage* gag_u, u3_pact_stat* pat_u) } /* - * _xmas_req_get_cwnd(): produce current state of packet pump + * _xmas_req_get_cwnd(): produce packets to send * * saves next fragment number and preallocated pact into the passed pointers. * Will not do so if returning 0 */ static c3_w -_xmas_req_get_cwnd_nex(u3_xmas* sam_u, u3_xmas_name* nam_u, c3_w* nex_w, u3_xmas_pict** nex_u) +_xmas_req_get_cwnd(u3_pend_req* req_u) { c3_w res_w = 0; - u3_pend_req* req_u = _xmas_get_request(sam_u, nam_u); - if ( NULL == req_u ) { - u3l_log("no request"); - return 0; - } - *nex_u = req_u->pic_u; if ( req_u->tot_w == 0 || req_u->gag_u == NULL ) { u3l_log("shouldn't happen"); - *nex_w = 0; _log_pend_req(req_u); u3_assert(0); return 1; } - *nex_w = req_u->nex_w; - c3_w liv_w = bitset_wyt(&req_u->was_u); + if ( req_u->nex_w == req_u->tot_w ) { + return 0; + } c3_w rem_w = req_u->tot_w - req_u->nex_w + 1; return c3_min(rem_w, req_u->gag_u->wnd_w - liv_w); @@ -626,10 +631,9 @@ _xmas_req_get_cwnd_nex(u3_xmas* sam_u, u3_xmas_name* nam_u, c3_w* nex_w, u3_xmas ** */ static void -_xmas_req_pact_resent(u3_xmas* sam_u, u3_xmas_name* nam_u) +_xmas_req_pact_resent(u3_pend_req* req_u, u3_xmas_name* nam_u) { c3_d now_d = _get_now_micros(); - u3_pend_req* req_u = _xmas_get_request(sam_u, nam_u); // if we dont have pending request noop if ( NULL == req_u ) { return; @@ -643,11 +647,9 @@ _xmas_req_pact_resent(u3_xmas* sam_u, u3_xmas_name* nam_u) ** after 1-RT first packet is handled in _xmas_req_pact_init() */ static void -_xmas_req_pact_sent(u3_xmas* sam_u, u3_xmas_name* nam_u) +_xmas_req_pact_sent(u3_pend_req* req_u, u3_xmas_name* nam_u) { c3_d now_d = _get_now_micros(); - u3_pend_req* req_u = _xmas_get_request(sam_u, nam_u); - // if we already have pending request if ( NULL != req_u ) { if( req_u->nex_w == nam_u->fra_w ) { @@ -656,10 +658,6 @@ _xmas_req_pact_sent(u3_xmas* sam_u, u3_xmas_name* nam_u) // TODO: optional assertions? req_u->wat_u[nam_u->fra_w] = (u3_pact_stat){now_d, 0, 1, 0 }; bitset_put(&req_u->was_u, nam_u->fra_w); - // if ( nam_u->fra_w == 1 ) { - // req_u->lef_w = 1; - // } - } else { u3l_log("xmas: no req for sent"); return; @@ -672,7 +670,6 @@ _xmas_req_pact_sent(u3_xmas* sam_u, u3_xmas_name* nam_u) } } } - _xmas_put_request(sam_u, nam_u, req_u); } @@ -736,8 +733,8 @@ u3_xmas_decode_lane(u3_atom lan) { // static void _xmas_free_seal(u3_seal* sel_u) { - //c3_free(sel_u->buf_y); - //c3_free(sel_u); + c3_free(sel_u->buf_y); + c3_free(sel_u); } static u3_noun _xmas_get_now() { @@ -753,7 +750,6 @@ static u3_noun _xmas_get_now() { static void _xmas_send_cb(uv_udp_send_t* req_u, c3_i sas_i) { - u3l_log("sent"); u3_seal* sel_u = (u3_seal*)req_u; u3_xmas* sam_u = sel_u->sam_u; @@ -765,7 +761,7 @@ _xmas_send_cb(uv_udp_send_t* req_u, c3_i sas_i) //sam_u->fig_u.net_o = c3y; } - //_xmas_free_seal(sel_u); + _xmas_free_seal(sel_u); } static void _xmas_send_buf(u3_xmas* sam_u, u3_lane lan_u, c3_y* buf_y, c3_w len_w) @@ -807,33 +803,7 @@ static void _xmas_send_buf(u3_xmas* sam_u, u3_lane lan_u, c3_y* buf_y, c3_w len_ } } -static void -_update_oldest_req(u3_pend_req *req_u, u3_gage* gag_u) -{ - if( req_u->tot_w == 0 ) { - return; - } - // scan in flight packets, find oldest - c3_w idx_w = req_u->lef_w; - c3_d now_d = _get_now_micros(); - c3_d wen_d = now_d; - for ( c3_w i = req_u->lef_w; i < req_u->nex_w; i++ ) { - if ( c3y == bitset_has(&req_u->was_u, i) && - wen_d > req_u->wat_u[i].sen_d - ) { - wen_d = req_u->wat_u[i].sen_d; - idx_w = i; - } - } - if ( now_d == wen_d ) { -#ifdef XMAS_DEBUG - u3l_log("failed to find new oldest"); -#endif - } - req_u->old_w = idx_w; - req_u->tim_u.data = req_u; - uv_timer_start(&req_u->tim_u, _xmas_packet_timeout, ((req_u->wat_u[req_u->old_w].sen_d + gag_u->rto_w) / 1000), 0); -} + static void _xmas_send(u3_xmas_pict* pic_u, u3_lane* lan_u) { @@ -861,6 +831,7 @@ _try_resend(u3_pend_req* req_u) if ( c3y == bitset_has(&req_u->was_u, i) ) { req_u->pic_u->pac_u.pek_u.nam_u.fra_w = i; if ( req_u->wat_u[i].tie_y == 1 ) { + u3l_log("fast resend %u", i); los_o = c3y; c3_y* buf_y = c3_calloc(PACT_SIZE); req_u->pic_u->pac_u.pek_u.nam_u.fra_w = i; @@ -871,7 +842,7 @@ _try_resend(u3_pend_req* req_u) } // TODO: better route management _xmas_send_buf(sam_u, *lan_u, buf_y, siz_w); - _xmas_req_pact_resent(sam_u, &req_u->pic_u->pac_u.pek_u.nam_u); + _xmas_req_pact_resent(req_u, &req_u->pic_u->pac_u.pek_u.nam_u); } else if ( (now_d - req_u->wat_u[i].sen_d) > req_u->gag_u->rto_w ) { los_o = c3y; @@ -885,7 +856,7 @@ _try_resend(u3_pend_req* req_u) } // TODO: better route management _xmas_send_buf(sam_u, *lan_u, buf_y, siz_w); - _xmas_req_pact_resent(sam_u, &req_u->pic_u->pac_u.pek_u.nam_u); + _xmas_req_pact_resent(req_u, &req_u->pic_u->pac_u.pek_u.nam_u); } } } @@ -897,9 +868,86 @@ _try_resend(u3_pend_req* req_u) } } +/* _xmas_packet_timeout(): callback for packet timeout +*/ +static void +_xmas_packet_timeout(uv_timer_t* tim_u) { + u3_pend_req* req_u = (u3_pend_req*)tim_u->data; + u3l_log("%u packet timed out", req_u->old_w); + _try_resend(req_u); +} + +static void +_update_oldest_req(u3_pend_req *req_u, u3_gage* gag_u) +{ + if( req_u->tot_w == 0 || req_u->len_w == req_u->tot_w ) { + return; + } + // scan in flight packets, find oldest + c3_w idx_w = req_u->lef_w; + c3_d now_d = _get_now_micros(); + c3_d wen_d = now_d; + for ( c3_w i = req_u->lef_w; i < req_u->nex_w; i++ ) { + // u3l_log("fra %u (%u)", i, __LINE__); + if ( c3y == bitset_has(&req_u->was_u, i) && + wen_d > req_u->wat_u[i].sen_d + ) { + wen_d = req_u->wat_u[i].sen_d; + idx_w = i; + } + } + if ( now_d == wen_d ) { +#ifdef XMAS_DEBUG + u3l_log("failed to find new oldest"); +#endif + } + req_u->old_w = idx_w; + req_u->tim_u.data = req_u; + c3_d gap_d = now_d -req_u->wat_u[idx_w].sen_d; + uv_timer_start(&req_u->tim_u, _xmas_packet_timeout, (gag_u->rto_w - gap_d) / 1000, 0); +} + +static void _xmas_free_misord_buf(u3_misord_buf* buf_u) +{ + c3_free(buf_u->fra_y); + if ( buf_u->par_u != NULL ) { + c3_free(buf_u->par_u); + } + c3_free(buf_u); +} + +static bao_ingest_result +_xmas_burn_misorder_queue(u3_pend_req* req_u) +{ + c3_w wan_w = req_u->bao_u->con_w; + c3_w len_w; + c3_o fon_o; + bao_ingest_result res_y = BAO_GOOD; + while ( (len_w = vec_len(&req_u->mis_u)) != 0 ) { + fon_o = c3n; + for (int i = 0; i < len_w; i++) { + u3_misord_buf* buf_u = (u3_misord_buf*)req_u->mis_u.vod_p[i]; + if ( buf_u->num_w == wan_w ) { + fon_o = c3y; + if ( BAO_GOOD != (res_y = blake_bao_verify(req_u->bao_u, buf_u->fra_y, buf_u->len_w, buf_u->par_u))) { + return res_y; + } else { + _xmas_free_misord_buf((u3_misord_buf*)vec_pop(&req_u->mis_u, i)); + break; + } + } + } + wan_w++; + if ( c3n == fon_o ) { + break; + } + } + return res_y; +} + /* _xmas_req_pact_done(): mark packet as done, returning if we should continue */ -static c3_o +static u3_pend_req* _xmas_req_pact_done(u3_xmas* sam_u, u3_xmas_name *nam_u, u3_xmas_data* dat_u, u3_lane* lan_u) { u3_weak ret = u3_none; @@ -908,25 +956,7 @@ _xmas_req_pact_done(u3_xmas* sam_u, u3_xmas_name *nam_u, u3_xmas_data* dat_u, u3 if ( NULL == req_u ) { XMAS_LOG(APATHY); - return c3n; - } - blake_pair* par_u = NULL; - if ( dat_u->aum_u.typ_e == AUTH_NEXT ) { - par_u = c3_calloc(sizeof(blake_pair)); - memcpy(par_u->sin_y, dat_u->aup_u.has_y[0], BLAKE3_OUT_LEN); - memcpy(par_u->dex_y, dat_u->aup_u.has_y[1], BLAKE3_OUT_LEN); - } - - c3_y ver_y; - // TODO: move to bottom - if ( req_u->bao_u->con_w != nam_u->fra_w ) { - // TODO: queue packet - u3l_log("misordered hash"); - } else if( BAO_GOOD != (ver_y = blake_bao_verify(req_u->bao_u, dat_u->fra_y, dat_u->len_w, par_u))) { - // TODO: do we drop the whole request on the floor? - XMAS_LOG(AUTH); - return c3n; - } else { + return NULL; } u3_gage* gag_u = _xmas_get_lane(sam_u, nam_u->her_d, lan_u); @@ -947,14 +977,14 @@ _xmas_req_pact_done(u3_xmas* sam_u, u3_xmas_name *nam_u, u3_xmas_data* dat_u, u3 if ( dat_u->tot_w <= nam_u->fra_w ) { XMAS_LOG(STRANGE); // XX: is this sufficient to drop whole request - return c3y; + return req_u; } // received duplicate if ( nam_u->fra_w != 0 && c3n == bitset_has(&req_u->was_u, nam_u->fra_w) ) { XMAS_LOG(DUPE); req_u->wat_u[nam_u->fra_w].dup_y++; - return c3y; + return req_u; } bitset_del(&req_u->was_u, nam_u->fra_w); @@ -972,20 +1002,52 @@ _xmas_req_pact_done(u3_xmas* sam_u, u3_xmas_name *nam_u, u3_xmas_data* dat_u, u3 req_u->lef_w++; } + blake_pair* par_u = NULL; + if ( dat_u->aum_u.typ_e == AUTH_NEXT ) { + // needs to be heap allocated bc will be saved if misordered + par_u = c3_calloc(sizeof(blake_pair)); + memcpy(par_u->sin_y, dat_u->aup_u.has_y[0], BLAKE3_OUT_LEN); + memcpy(par_u->dex_y, dat_u->aup_u.has_y[1], BLAKE3_OUT_LEN); + } + + c3_y ver_y; + // TODO: move to bottom + if ( req_u->bao_u->con_w != nam_u->fra_w ) { + // TODO: queue packet + u3_misord_buf* buf_u = c3_calloc(sizeof(u3_misord_buf)); + buf_u->fra_y = c3_calloc(dat_u->len_w); + buf_u->len_w = dat_u->len_w; + memcpy(buf_u->fra_y, dat_u->fra_y, dat_u->len_w); + buf_u->par_u = par_u; + buf_u->num_w = nam_u->fra_w; + vec_append(&req_u->mis_u, buf_u); + } else if ( BAO_GOOD != (ver_y = blake_bao_verify(req_u->bao_u, dat_u->fra_y, dat_u->len_w, par_u)) ) { + c3_free(par_u); + // TODO: do we drop the whole request on the floor? + XMAS_LOG(AUTH); + return req_u; + } else if ( vec_len(&req_u->mis_u) != 0 + && BAO_GOOD != (ver_y = _xmas_burn_misorder_queue(req_u))) { + c3_free(par_u); + XMAS_LOG(AUTH) + return req_u; + } else { + c3_free(par_u); + } + // handle gauge update _xmas_handle_ack(gag_u, &req_u->wat_u[nam_u->fra_w]); memcpy(req_u->dat_y + (siz_w * nam_u->fra_w), dat_u->fra_y, dat_u->len_w); - c3_d wen_d = _get_now_micros(); _try_resend(req_u); _update_oldest_req(req_u, gag_u); - _xmas_put_request(sam_u, nam_u, req_u); - _xmas_put_lane(sam_u, nam_u->her_d, lan_u, gag_u); - return c3y; + // _xmas_put_request(sam_u, nam_u, req_u); + // _xmas_put_lane(sam_u, nam_u->her_d, lan_u, gag_u); + return req_u; } @@ -1165,9 +1227,7 @@ _xmas_queue_czar(u3_xmas* sam_u, u3_noun las, u3_noun pac) while ( t != u3_nul ) { u3x_cell(t, &lan, &t); if ( (c3y == u3a_is_cat(lan) && lan < 256 ) ) { - u3l_log("imp %u", lan); u3_czar_info* imp_u = &sam_u->imp_u[lan]; - _log_czar_info(imp_u); if ( 0 != imp_u->pip_w ) { res = u3nc(u3nt(c3__if, u3i_word(imp_u->pip_w), _ames_czar_port(lan)), res); @@ -1192,15 +1252,12 @@ _xmas_ef_send(u3_xmas* sam_u, u3_noun las, u3_noun pac) c3_w len_w = u3r_met(3, pac); c3_y* buf_y = c3_calloc(len_w); u3r_bytes(0, len_w, buf_y, pac); - u3_xmas_pict pic_u; - pic_u.sam_u = sam_u; - xmas_sift_pact(&pic_u.pac_u, buf_y, len_w); - if ( pic_u.pac_u.hed_u.typ_y == PACT_PEEK ) { - sam_u->tim_d = _get_now_micros(); - } + sam_u->tim_d = _get_now_micros(); c3_o suc_o = c3n; _xmas_rout_bufs(sam_u, buf_y, len_w, las); + + c3_free(buf_y); u3z(pac); } @@ -1472,38 +1529,6 @@ _xmas_respond(u3_xmas_pict* req_u, c3_y** buf_y, u3_noun hit) return len_w; } -static void -_xmas_serve_cache_hit(u3_xmas_pict* pic_u, u3_lane lan_u, u3_noun hit) -{ - u3_xmas_pact* req_u = &pic_u->pac_u; - u3l_log("serving cache hit"); - c3_d her_d[2]; - memcpy(her_d, req_u->pek_u.nam_u.her_d, 2); - c3_d our_d[2]; - memcpy(our_d, pic_u->sam_u->pir_u->who_d, 2); - if ( (her_d[0] != our_d[0]) - || (her_d[1] != our_d[1]) ) - { - u3l_log("publisher is not ours"); - if ( (256 > our_d[0]) - && (0 == our_d[1]) ) - { - // Forward only if requested datum is not ours, and we are a galaxy - //_xmas_try_forward(req_u, fra_s, hit); - } else { - u3l_log("no forward, we are not a galaxy"); - } - } else { - //u3l_log("first: %x", u3r_string(u3dc("scot", c3__uv, u3k(u3h(u3t(hit)))))); - c3_y* buf_y; - - c3_w len_w = _xmas_respond(pic_u, &buf_y, hit); - _xmas_send_buf(pic_u->sam_u, lan_u, buf_y, len_w); - } - // no lose needed because all nouns are moved in both - // branches of the conditional -} - /* */ static void @@ -1643,7 +1668,7 @@ _xmas_forward(u3_xmas_pict* pic_u, u3_lane lan_u) xmas_free_pact(pac_u); } -static c3_o +static u3_pend_req* _xmas_req_pact_init(u3_xmas* sam_u, u3_xmas_pict* pic_u, u3_lane* lan_u) { u3_xmas_pact* pac_u = &pic_u->pac_u; @@ -1651,12 +1676,11 @@ _xmas_req_pact_init(u3_xmas* sam_u, u3_xmas_pict* pic_u, u3_lane* lan_u) u3_xmas_data* dat_u = &pac_u->pag_u.dat_u; c3_o lin_o = dat_u->tot_w <= 4 ? c3y : c3n; - u3_pend_req* req_u = _xmas_get_request(sam_u, nam_u); if ( NULL != req_u ) { // duplicate if ( req_u->tot_w <= 4 ) { - return c3n; + return NULL; } } else { req_u = alloca(sizeof(u3_pend_req)); @@ -1674,16 +1698,7 @@ _xmas_req_pact_init(u3_xmas* sam_u, u3_xmas_pict* pic_u, u3_lane* lan_u) u3_assert( gag_u != NULL ); } - c3_w siz_w = 1 << (pac_u->pag_u.nam_u.boq_y - 3); - u3_assert( siz_w == 1024 ); // boq_y == 13 - req_u->gag_u = gag_u; - req_u->dat_y = c3_calloc(siz_w * dat_u->tot_w); - req_u->wat_u = c3_calloc(sizeof(u3_pact_stat) * dat_u->tot_w); - req_u->tot_w = dat_u->tot_w; - bitset_init(&req_u->was_u, dat_u->tot_w + 1); - - - req_u->pic_u = c3_calloc(sizeof(u3_xmas_pact)); + req_u->pic_u = c3_calloc(sizeof(u3_xmas_pict)); req_u->pic_u->sam_u = sam_u; req_u->pic_u->pac_u.hed_u.typ_y = PACT_PEEK; req_u->pic_u->pac_u.hed_u.pro_y = XMAS_VER; @@ -1691,6 +1706,14 @@ _xmas_req_pact_init(u3_xmas* sam_u, u3_xmas_pict* pic_u, u3_lane* lan_u) req_u->pic_u->pac_u.pek_u.nam_u.aut_o = c3n; req_u->pic_u->pac_u.pek_u.nam_u.nit_o = c3n; + c3_w siz_w = 1 << (pac_u->pag_u.nam_u.boq_y - 3); + u3_assert( siz_w == 1024 ); // boq_y == 13 + req_u->gag_u = gag_u; + req_u->dat_y = c3_calloc(siz_w * dat_u->tot_w); + req_u->wat_u = c3_calloc(sizeof(u3_pact_stat) * dat_u->tot_w + 2 ); + req_u->tot_w = dat_u->tot_w; + bitset_init(&req_u->was_u, dat_u->tot_w); + // TODO: handle restart // u3_assert( nam_u->fra_w == 0 ); @@ -1724,26 +1747,25 @@ _xmas_req_pact_init(u3_xmas* sam_u, u3_xmas_pict* pic_u, u3_lane* lan_u) u3_vec(c3_y[BLAKE3_OUT_LEN])* pof_u = vec_make(len_w); for ( int i = 0; i < len_w; i++ ) { c3_y* pof_y = c3_calloc(BLAKE3_OUT_LEN); - u3l_log("proof %u", i); memcpy(pof_y, dat_u->fra_y + (BLAKE3_OUT_LEN*i), BLAKE3_OUT_LEN); - _log_buf(pof_y, BLAKE3_OUT_LEN); vec_append(pof_u, pof_y); } req_u->bao_u = blake_bao_make(req_u->tot_w, pof_u); } + vec_init(&req_u->mis_u, 8); - _xmas_put_request(sam_u, nam_u, req_u); - return c3y; + req_u = _xmas_put_request(sam_u, nam_u, req_u); + return req_u; } static void -_xmas_hear_page(u3_xmas_pict* pic_u, c3_y* buf_y, c3_w len_w, u3_lane lan_u) +_xmas_hear_page(u3_xmas_pict* pic_u, u3_lane lan_u) { #ifdef XMAS_DEBUG - u3l_log("xmas hear page %u", pic_u->pac_u.pag_u.nam_u.fra_w); + // u3l_log("xmas hear page %u", pic_u->pac_u.pag_u.nam_u.fra_w); #endif u3_xmas* sam_u = pic_u->sam_u; u3_xmas_pact* pac_u = &pic_u->pac_u; @@ -1777,6 +1799,8 @@ _xmas_hear_page(u3_xmas_pict* pic_u, c3_y* buf_y, c3_w len_w, u3_lane lan_u) if ( u3_none != hit ) { _xmas_forward(pic_u, lan_u); + _xmas_free_pict(pic_u); + u3z(hit); return; } @@ -1786,46 +1810,62 @@ _xmas_hear_page(u3_xmas_pict* pic_u, c3_y* buf_y, c3_w len_w, u3_lane lan_u) u3l_log("simulating dropped packet"); return; }*/ + u3_pend_req* req_u; if ( pac_u->pek_u.nam_u.nit_o == c3y ) { - u3l_log("initialising request"); - c3_o run_o = _xmas_req_pact_init(sam_u, pic_u, &lan_u); - u3l_log("initialised request %c", run_o == c3y ? 'y' : 'n'); - if ( run_o == c3n ) { + req_u = _xmas_req_pact_init(sam_u, pic_u, &lan_u); + if ( req_u == NULL ) { + _xmas_free_pict(pic_u); return; } } else { - u3l_log("continuing request"); - c3_o run_o = _xmas_req_pact_done(sam_u, &pac_u->pag_u.nam_u, &pac_u->pag_u.dat_u, &lan_u); - if ( c3n == run_o ) { + req_u = _xmas_req_pact_done(sam_u, &pac_u->pag_u.nam_u, &pac_u->pag_u.dat_u, &lan_u); + if ( req_u == NULL ) { // cleanup + _xmas_free_pict(pic_u); return; } } - - u3_xmas_pict* nex_u; - c3_w nex_w; - c3_w win_w = _xmas_req_get_cwnd_nex(sam_u, &pac_u->pag_u.nam_u, &nex_w, &nex_u); + if ( req_u == NULL ) { + u3_assert(!"invalid"); + return; + } + c3_w win_w = _xmas_req_get_cwnd(req_u); + u3_xmas_pict* nex_u = req_u->pic_u; + c3_w nex_w = req_u->nex_w; if ( win_w != 0 ) { #ifdef XMAS_DEBUG u3l_log("continuing flow nex: %u, win: %u", nex_w, win_w); + u3l_log("in flight %u", bitset_wyt(&req_u->was_u)); #endif - c3_y* buf_y = c3_calloc(PACT_SIZE * win_w); - c3_y* cur_y = buf_y; - for(int i = 0; i < win_w; i++) { + for ( int i = 0; i < win_w; i++ ) { + c3_y* buf_y = c3_calloc(PACT_SIZE); + c3_w fra_w = nex_w + i; + if ( fra_w >= req_u->tot_w ) { + break; + } nex_u->pac_u.pek_u.nam_u.fra_w = nex_w + i; - c3_w siz_w = xmas_etch_pact(cur_y, &nex_u->pac_u); + c3_w siz_w = xmas_etch_pact(buf_y, &nex_u->pac_u); if ( siz_w == 0 ) { u3l_log("failed to etch"); u3_assert( 0 ); } // TODO: better route management - _xmas_send_buf(sam_u, lan_u, cur_y, siz_w); - _xmas_req_pact_sent(sam_u, &nex_u->pac_u.pek_u.nam_u); - cur_y += siz_w; + _xmas_send_buf(sam_u, lan_u, buf_y, siz_w); + _xmas_req_pact_sent(req_u, &nex_u->pac_u.pek_u.nam_u); } } + if ( req_u->len_w == req_u->tot_w ) { + // fprintf(stderr, "finished"); + // u3l_log("queue size %u", req_u->mis_u.len_w); + c3_d now_d = _get_now_micros(); + u3l_log("%u kilobytes took %f ms", req_u->tot_w, (now_d - sam_u->tim_d)/1000.0); + c3_w siz_w = (1 << (pac_u->pag_u.nam_u.boq_y - 3)); + //u3_noun dat = u3i_bytes((siz_w * req_u->tot_w), req_u->dat_y); + _xmas_del_request(sam_u, &pac_u->pag_u.nam_u); + } + _xmas_free_pict(pic_u); } static void @@ -1861,7 +1901,7 @@ _xmas_hear_peek(u3_xmas_pict* pic_u, u3_lane lan_u) u3_peer* per_u = _xmas_get_peer(sam_u, her_d); if ( per_u == NULL ) { u3l_log("xmas: alien forward for %s", u3r_string(u3dc("scot", c3__p, u3i_chubs(2, her_d)))); - //xmas_free_pact(pac_u); + _xmas_free_pict(pic_u); return; } if ( c3y == sam_u->for_o && sam_u->pir_u->who_d[0] == per_u->imp_s ) { @@ -1872,6 +1912,8 @@ _xmas_hear_peek(u3_xmas_pict* pic_u, u3_lane lan_u) u3l_log("sending peek %u", pac_u->pek_u.nam_u.fra_w); _xmas_send(pic_u, &lin_u); } + _xmas_free_pict(pic_u); + return; } u3_weak hit = _xmas_get_cache(sam_u, &pac_u->pek_u.nam_u); @@ -1883,18 +1925,14 @@ _xmas_hear_peek(u3_xmas_pict* pic_u, u3_lane lan_u) _xmas_add_lane_to_cache(sam_u, &pac_u->pek_u.nam_u, u3k(u3t(dat)), lan_u); } else if ( c3y == our_o && tag == XMAS_ITEM ) { c3_y* buf_y; -// if ( rand() % 20 == 0) { -// u3l_log("dropping packet %u", pac_u->pek_u.nam_u.fra_w); -// return; -// } c3_w len_w = _xmas_respond(pic_u, &buf_y, u3k(dat)); - u3l_log("sending page %u", pac_u->pek_u.nam_u.fra_w); _xmas_send_buf(sam_u, lan_u, buf_y, len_w); } else { u3l_log("xmas: weird case in cache, dropping"); } - //xmas_free_pact(pac_u); - // u3z(hit); + _xmas_free_pict(pic_u); + u3z(hit); + return; } else { _xmas_add_lane_to_cache(sam_u, &pac_u->pek_u.nam_u, u3_nul, lan_u); // TODO: retrieve from namespace if ( c3y == our_o ) { @@ -1902,9 +1940,10 @@ _xmas_hear_peek(u3_xmas_pict* pic_u, u3_lane lan_u) u3_noun our = u3i_chubs(2, sam_u->car_u.pir_u->who_d); u3_noun bem = u3nc(u3nt(our, u3_nul, u3nc(c3__ud, 1)), sky); + // only branch where we do not free pic_u u3_pier_peek(sam_u->car_u.pir_u, u3_nul, u3k(u3nq(1, c3__beam, c3__xx, bem)), pic_u, _xmas_page_scry_cb); } else { - xmas_free_pact(pac_u); + _xmas_free_pict(pic_u); } } // u3z(pax); @@ -1947,10 +1986,6 @@ _xmas_hear(u3_xmas* sam_u, c3_w len_w, c3_y* hun_y) { -#ifdef XMAS_DEBUG - u3l_log("xmas_hear"); -#endif - u3_xmas_pict* pic_u; c3_w pre_w; c3_y* cur_y = hun_y; @@ -1962,12 +1997,11 @@ _xmas_hear(u3_xmas* sam_u, pic_u = c3_calloc(sizeof(u3_xmas_pict)); pic_u->sam_u = sam_u; c3_w lin_w = xmas_sift_pact(&pic_u->pac_u, hun_y, len_w); + c3_free(hun_y); if ( lin_w == 0 ) { -#ifdef XMAS_DEBUG - u3l_log("xmas: failed to parse packet"); - //_log_pact(pac_u); -#endif - // TODO free everything + XMAS_LOG(SERIAL) + c3_free(hun_y); + xmas_free_pact(&pic_u->pac_u); return; } @@ -1976,7 +2010,7 @@ _xmas_hear(u3_xmas* sam_u, _xmas_hear_peek(pic_u, *lan_u); } break; case PACT_PAGE: { - _xmas_hear_page(pic_u, hun_y, len_w, *lan_u); + _xmas_hear_page(pic_u, *lan_u); } break; default: { _xmas_hear_poke(pic_u, lan_u); @@ -2039,7 +2073,6 @@ _xmas_io_talk(u3_auto* car_u) u3_auto_plan(car_u, u3_ovum_init(0, c3__x, wir, cad)); } - //_xmas_io_start(sam_u); u3_noun who = u3i_chubs(2, sam_u->pir_u->who_d); u3_noun rac = u3do("clan:title", u3k(who)); c3_s por_s = sam_u->pir_u->por_s; diff --git a/pkg/vere/io/xmas/bitset.c b/pkg/vere/io/xmas/bitset.c index 11f09c231e..d58fd9ba4b 100644 --- a/pkg/vere/io/xmas/bitset.c +++ b/pkg/vere/io/xmas/bitset.c @@ -1,6 +1,8 @@ #include "bitset.h" #include +#include "vere.h" + #define u3_assert(x) \ do { \ @@ -14,22 +16,12 @@ } \ } while(0) -#define u3l_log(...) fprintf(stderr, __VA_ARGS__) -#define c3_calloc(s) ({ \ - void* rut = calloc(1,s); \ - if ( 0 == rut ) { \ - fprintf(stderr, "c3_calloc(%" PRIu64 ") failed\r\n", \ - (c3_d)s); \ - u3_assert(!"memory lost"); \ - } \ - rut;}) -#define c3_free free void bitset_init(u3_bitset* bit_u, c3_w len_w) { bit_u->len_w = len_w; - bit_u->buf_y = c3_calloc(bit_u->len_w >> 3); + bit_u->buf_y = c3_calloc(len_w >> 3); } void @@ -38,12 +30,31 @@ bitset_free(u3_bitset* bit_u) c3_free(bit_u->buf_y); } +static c3_y +_popcnt(c3_y num_y) +{ + return __builtin_popcount(num_y); +} + +static void +_log_bitset(u3_bitset* bit_u) +{ + c3_w cur_w = 0; + while( cur_w < bit_u->len_w ) { + if ( c3y == bitset_has(bit_u, cur_w) ) { + u3l_log("%u", cur_w); + } + cur_w++; + } +} + c3_w bitset_wyt(u3_bitset* bit_u) { c3_w ret_w = 0; - for(int i = 0; i < (bit_u->len_w >> 3); i++ ) { - ret_w += __builtin_popcount(bit_u->buf_y[i]); + c3_w len_w = (bit_u->len_w >> 3); + for(int i = 0; i < len_w; i++ ) { + ret_w += _popcnt(bit_u->buf_y[i]); } return ret_w; } @@ -86,19 +97,7 @@ bitset_del(u3_bitset* bit_u, c3_w mem_w) } -static void -_log_bitset(u3_bitset* bit_u) -{ - c3_w cur_w = 0; - u3l_log("logging bitset"); - while( cur_w < bit_u->len_w ) { - if ( c3y == bitset_has(bit_u, cur_w) ) { - u3l_log("%u", cur_w); - } - cur_w++; - } - u3l_log("finished"); -} + #ifdef BITSET_TEST c3_w main() diff --git a/pkg/vere/io/xmas/bitset.h b/pkg/vere/io/xmas/bitset.h index f2b2838c75..eb76485f89 100644 --- a/pkg/vere/io/xmas/bitset.h +++ b/pkg/vere/io/xmas/bitset.h @@ -19,4 +19,5 @@ void bitset_put(u3_bitset* bit_u, c3_w mem_w); c3_o bitset_has(u3_bitset* bit_u, c3_w mem_w); void bitset_del(u3_bitset* bit_u, c3_w mem_w); + #endif \ No newline at end of file diff --git a/pkg/vere/io/xmas/pact.c b/pkg/vere/io/xmas/pact.c index 3b2bd08894..52fa95374a 100644 --- a/pkg/vere/io/xmas/pact.c +++ b/pkg/vere/io/xmas/pact.c @@ -12,27 +12,6 @@ #define CHECK_BOUNDS(cur) if ( len_w < cur ) { u3l_log("xmas: failed parse (%u,%u) at line %i", len_w, cur, __LINE__); return 0; } #define safe_dec(num) (num == 0 ? num : num - 1) #define _xmas_met3_w(a_w) ((c3_bits_word(a_w) + 0x7) >> 3) -#define u3_assert(x) \ - do { \ - if (!(x)) { \ - fflush(stderr); \ - fprintf(stderr, "\rAssertion '%s' " \ - "failed in %s:%d\r\n", \ - #x, __FILE__, __LINE__); \ - /*u3m_bail(c3__oops); */ \ - /*abort(); */ \ - } \ - } while(0) - -//#define u3l_log(...) fprintf(stderr, __VA_ARGS__) -#define c3_calloc(s) ({ \ - void* rut = calloc(1,s); \ - if ( 0 == rut ) { \ - fprintf(stderr, "c3_calloc(%" PRIu64 ") failed\r\n", \ - (c3_d)s); \ - u3_assert(!"memory lost"); \ - } \ - rut;}) /* Logging functions @@ -220,6 +199,10 @@ _xmas_rank(c3_d who_d[2]) /* lifecycle */ + +/* xmas_free_pact(): free contents of packet. +* Does *not* free pac_u itself +*/ void xmas_free_pact(u3_xmas_pact* pac_u) { switch ( pac_u->hed_u.typ_y ) { @@ -230,14 +213,14 @@ void xmas_free_pact(u3_xmas_pact* pac_u) break; }; case PACT_PAGE: { - //c3_free(pac_u->pag_u.dat_u.fra_y); + c3_free(pac_u->pag_u.dat_u.fra_y); break; }; case PACT_POKE: { + c3_free(pac_u->pok_u.dat_u.fra_y); break; }; } - c3_free(pac_u); } /* deserialisation @@ -614,16 +597,9 @@ xmas_sift_pact(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w) static void _xmas_etch_head(u3_xmas_head* hed_u, c3_y buf_y[8]) { -#ifdef XMAS_DEBUG - if( c3y == XMAS_DEBUG ) { - if( hed_u->pro_y > 7 ) { - u3l_log("xmas: bad protocol version"); - return; - } + if ( 1 != hed_u->pro_y ) { + u3l_log("etching bad head"); } -#endif - - assert( 1 == hed_u->pro_y ); // c3_o req_o = c3o((hed_u->typ_y == PACT_PEEK), (hed_u->typ_y == PACT_POKE)); // c3_y siz_y = req_o ? 5 : 7; From c1b6808dc9c3f0569892bd07691baff03d8d49fb Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Fri, 8 Mar 2024 12:43:29 +0100 Subject: [PATCH 063/430] WIP --- BUILD.bazel | 2 +- pkg/noun/events.c | 6 +- pkg/noun/v3/manage.c | 4 + pkg/noun/version.h | 22 +- pkg/vere/BUILD.bazel | 5 +- pkg/vere/disk.c | 647 +++++++++++++------ pkg/vere/io/ames.c | 4 +- pkg/vere/io/http.c | 4 +- pkg/vere/io/{xmas.c => mesa.c} | 789 +++++++++++------------ pkg/vere/io/{xmas => mesa}/bitset.c | 0 pkg/vere/io/{xmas => mesa}/bitset.h | 0 pkg/vere/io/{xmas/xmas.h => mesa/mesa.h} | 122 ++-- pkg/vere/io/{xmas => mesa}/pact.c | 228 +++---- pkg/vere/main.c | 113 ++-- pkg/vere/mars.c | 7 +- pkg/vere/pier.c | 15 +- pkg/vere/platform/darwin/mach.c | 28 + pkg/vere/vere.h | 43 +- 18 files changed, 1164 insertions(+), 875 deletions(-) rename pkg/vere/io/{xmas.c => mesa.c} (70%) rename pkg/vere/io/{xmas => mesa}/bitset.c (100%) rename pkg/vere/io/{xmas => mesa}/bitset.h (100%) rename pkg/vere/io/{xmas/xmas.h => mesa/mesa.h} (55%) rename pkg/vere/io/{xmas => mesa}/pact.c (82%) create mode 100644 pkg/vere/platform/darwin/mach.c diff --git a/BUILD.bazel b/BUILD.bazel index 6220e02702..ff11949f5f 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -77,7 +77,7 @@ config_setting( string_flag( name = "clang_version", # macOS uses `clang-14.0.3` by default. - build_setting_default = "15.0.0", + build_setting_default = "14.0.3", visibility = ["//visibility:public"], ) diff --git a/pkg/noun/events.c b/pkg/noun/events.c index d4f6c92ed0..d5e0ddac0f 100644 --- a/pkg/noun/events.c +++ b/pkg/noun/events.c @@ -522,10 +522,10 @@ _ce_patch_verify(u3_ce_patch* pat_u) c3_zs ret_zs; c3_o sou_o = c3n; // south seen - if ( U3E_VERLAT != pat_u->con_u->ver_w ) { + if ( U3P_VERLAT != pat_u->con_u->ver_w ) { fprintf(stderr, "loom: patch version mismatch: have %"PRIc3_w", need %u\r\n", pat_u->con_u->ver_w, - U3E_VERLAT); + U3P_VERLAT); return c3n; } @@ -737,7 +737,7 @@ _ce_patch_compose(c3_w nor_w, c3_w sou_w) _ce_patch_create(pat_u); pat_u->con_u = c3_malloc(sizeof(u3e_control) + (pgs_w * sizeof(u3e_line))); - pat_u->con_u->ver_w = U3E_VERLAT; + pat_u->con_u->ver_w = U3P_VERLAT; pgc_w = 0; for ( i_w = 0; i_w < nor_w; i_w++ ) { diff --git a/pkg/noun/v3/manage.c b/pkg/noun/v3/manage.c index a15d5ad9be..16dfbcb2a6 100644 --- a/pkg/noun/v3/manage.c +++ b/pkg/noun/v3/manage.c @@ -36,6 +36,10 @@ u3m_v3_migrate() u3R_v2 = &u3H_v2->rod_u; u3R_v2->cap_p = u3R_v2->mat_p = u3a_v2_outa(u3H_v2); + u3R = (u3a_road*)u3R_v2; + u3H = (u3v_home*)u3H_v2; + u3a_ream(); + // free bytecode caches in old road u3j_v2_reclaim(); u3n_v2_reclaim(); diff --git a/pkg/noun/version.h b/pkg/noun/version.h index 5cc6acb86d..47e43ee336 100644 --- a/pkg/noun/version.h +++ b/pkg/noun/version.h @@ -11,18 +11,30 @@ typedef c3_w u3v_version; #define U3V_VER3 3 #define U3V_VERLAT U3V_VER3 -/* EVENTS +/* PATCHES */ typedef c3_w u3e_version; -#define U3E_VER1 1 -#define U3E_VERLAT U3E_VER1 +#define U3P_VER1 1 +#define U3P_VERLAT U3P_VER1 /* DISK FORMAT + * + * synchronized between: + * - u3_disk->ver_w + * - version key in META table of data.mdb files */ -#define U3D_VER1 1 -#define U3D_VERLAT U3L_VER1 +#define U3D_VER1 1 // <= vere-v2 +#define U3D_VER2 2 // migrating to >= vere-v3 +#define U3D_VER3 3 // >= vere-v3 +#define U3D_VERLAT U3D_VER3 + +/* EPOCH SYSTEM +*/ + +#define U3E_VER1 1 +#define U3E_VERLAT U3E_VER1 #endif /* ifndef U3_VERSION_H */ diff --git a/pkg/vere/BUILD.bazel b/pkg/vere/BUILD.bazel index 5db129b8f9..ee09e201f8 100644 --- a/pkg/vere/BUILD.bazel +++ b/pkg/vere/BUILD.bazel @@ -97,8 +97,8 @@ vere_library( "db/*.c", "io/*.c", "io/*.h", - "io/xmas/*.c", - "io/xmas/*.h", + "io/mesa/*.c", + "io/mesa/*.h", ], exclude = [ "main.c", @@ -113,6 +113,7 @@ vere_library( "@platforms//os:macos": [ "platform/darwin/daemon.c", "platform/darwin/ptty.c", + "platform/darwin/mach.c", ], "@platforms//os:linux": [ "platform/linux/daemon.c", diff --git a/pkg/vere/disk.c b/pkg/vere/disk.c index de141988bd..18fdf31fec 100644 --- a/pkg/vere/disk.c +++ b/pkg/vere/disk.c @@ -573,6 +573,11 @@ u3_disk_walk_init(u3_disk* log_u, eve_d, c3_min(max_d, log_u->dun_d)); + if ( c3n == wok_u->liv_o ) { + c3_free(wok_u); + return 0; + } + return wok_u; } @@ -628,33 +633,35 @@ u3_disk_walk_done(u3_disk_walk* wok_u) /* _disk_save_meta(): serialize atom, save as metadata at [key_c]. */ static c3_o -_disk_save_meta(MDB_env* mdb_u, const c3_c* key_c, u3_atom dat) +_disk_save_meta(MDB_env* mdb_u, const c3_c* key_c, c3_w len_w, c3_y* byt_y) { - c3_w len_w = u3r_met(3, dat); - c3_y* byt_y = c3_malloc(len_w); - u3r_bytes(0, len_w, byt_y, dat); - - { - c3_o ret_o = u3_lmdb_save_meta(mdb_u, key_c, len_w, byt_y); - c3_free(byt_y); - return ret_o; + // strip trailing zeroes. + // + while ( len_w && !byt_y[len_w - 1] ) { + len_w--; } + + return u3_lmdb_save_meta(mdb_u, key_c, len_w, byt_y); } /* u3_disk_save_meta(): save metadata. */ c3_o u3_disk_save_meta(MDB_env* mdb_u, + c3_w ver_w, c3_d who_d[2], c3_o fak_o, c3_w lif_w) { + u3_assert( c3y == u3a_is_cat(ver_w) ); u3_assert( c3y == u3a_is_cat(lif_w) ); - if ( (c3n == _disk_save_meta(mdb_u, "version", 1)) - || (c3n == _disk_save_meta(mdb_u, "who", u3i_chubs(2, who_d))) - || (c3n == _disk_save_meta(mdb_u, "fake", fak_o)) - || (c3n == _disk_save_meta(mdb_u, "life", lif_w)) ) + // XX assumes little-endian + // + if ( (c3n == _disk_save_meta(mdb_u, "version", 4, (c3_y*)&ver_w)) + || (c3n == _disk_save_meta(mdb_u, "who", 16, (c3_y*)who_d)) + || (c3n == _disk_save_meta(mdb_u, "fake", 1, (c3_y*)&fak_o)) + || (c3n == _disk_save_meta(mdb_u, "life", 4, (c3_y*)&lif_w)) ) { return c3n; } @@ -662,6 +669,33 @@ u3_disk_save_meta(MDB_env* mdb_u, return c3y; } + +/* u3_disk_save_meta_meta(): save meta metadata. +*/ +c3_o +u3_disk_save_meta_meta(c3_c* log_c, + c3_d who_d[2], + c3_o fak_o, + c3_w lif_w) +{ + MDB_env* dbm_u; + + if ( 0 == (dbm_u = u3_lmdb_init(log_c, siz_i)) ) { + fprintf(stderr, "disk: failed to initialize meta-lmdb\r\n"); + return c3n; + } + + if ( c3n == u3_disk_save_meta(dbm_u, U3D_VERLAT, who_d, fak_o, lif_w) ) { + fprintf(stderr, "disk: failed to save metadata\r\n"); + return c3n; + } + + u3_lmdb_exit(dbm_u); + + return c3y; +} + + typedef struct { ssize_t hav_i; c3_y buf_y[16]; @@ -688,6 +722,7 @@ _disk_meta_read_cb(void* ptr_v, ssize_t val_i, void* val_v) */ c3_o u3_disk_read_meta(MDB_env* mdb_u, + c3_w* ver_w, c3_d* who_d, c3_o* fak_o, c3_w* lif_w) @@ -698,13 +733,13 @@ u3_disk_read_meta(MDB_env* mdb_u, // u3_lmdb_read_meta(mdb_u, &val_u, "version", _disk_meta_read_cb); - if ( 0 > val_u.hav_i ) { - fprintf(stderr, "disk: read meta: no version\r\n"); + if ( 1 != val_u.hav_i ) { + fprintf(stderr, "disk: read meta: strange version: %zd bytes\r\n", val_u.hav_i); return c3n; } - else if ( (1 != val_u.hav_i) || (1 != *val_u.buf_y) ) { - fprintf(stderr, "disk: read meta: unknown version %u\r\n", *val_u.buf_y); - return c3n; + + if ( ver_w ) { + *ver_w = val_u.buf_y[0]; } // identity @@ -1069,14 +1104,83 @@ _disk_epoc_meta(u3_disk* log_u, return c3y; } -/* u3_disk_epoc_init: create new epoch. +/* u3_disk_epoc_zero: create epoch zero. */ c3_o -u3_disk_epoc_init(u3_disk* log_u, c3_d epo_d) +u3_disk_epoc_zero(u3_disk* log_u) { + // create new epoch directory if it doesn't exist + c3_c epo_c[8193]; + c3_i epo_i; + snprintf(epo_c, sizeof(epo_c), "%s/0i0", log_u->com_u->pax_c); + c3_d ret_d = c3_mkdir(epo_c, 0700); + if ( ( ret_d < 0 ) && ( errno != EEXIST ) ) { + fprintf(stderr, "disk: epoch 0i0 mkdir failed: %s\r\n", strerror(errno)); + return c3n; + } + + // open epoch directory + if ( -1 == (epo_i = c3_open(epo_c, O_RDONLY)) ) { + fprintf(stderr, "disk: open epoch dir 0i0 failed: %s\r\n", strerror(errno)); + goto fail1; + } + + // sync epoch directory + if ( -1 == c3_sync(epo_i) ) { // XX fdatasync on linux? + fprintf(stderr, "disk: sync epoch dir 0i0 failed: %s\r\n", strerror(errno)); + goto fail2; + } + + // create epoch version file, overwriting any existing file + c3_c epv_c[8193]; + snprintf(epv_c, sizeof(epv_c), "%s/epoc.txt", epo_c); + FILE* epv_f = fopen(epv_c, "w"); // XX errors + fprintf(epv_f, "%d", U3E_VERLAT); + fclose(epv_f); + + // create binary version file, overwriting any existing file + c3_c biv_c[8193]; + snprintf(biv_c, sizeof(biv_c), "%s/vere.txt", epo_c); + FILE* biv_f = fopen(biv_c, "w"); // XX errors + fprintf(biv_f, URBIT_VERSION); // XX append a sentinel for auto-rollover? + fclose(biv_f); + + if ( -1 == c3_sync(epo_i) ) { // XX fdatasync on linux? + fprintf(stderr, "disk: sync epoch dir 0i0 failed: %s\r\n", strerror(errno)); + goto fail3; + } + close(epo_i); + + // load new epoch directory and set it in log_u + log_u->epo_d = 0; + log_u->ver_w = U3D_VERLAT; + + // success + return c3y; + +fail3: + c3_unlink(epv_c); + c3_unlink(biv_c); +fail2: + close(epo_i); +fail1: + c3_rmdir(epo_c); + return c3n; +} + +/* _disk_epoc_roll: epoch rollover. +*/ +static c3_o +_disk_epoc_roll(u3_disk* log_u, c3_d epo_d) +{ + u3_assert(epo_d); + // check if any epoch directories exist c3_d lat_d; - c3_o eps_o = u3_disk_epoc_last(log_u, &lat_d); + if ( c3n == u3_disk_epoc_last(log_u, &lat_d) ) { + fprintf(stderr, "disk: failed to get last epoch\r\n"); + return c3n; + } // create new epoch directory if it doesn't exist c3_c epo_c[8193]; @@ -1090,13 +1194,11 @@ u3_disk_epoc_init(u3_disk* log_u, c3_d epo_d) } // copy snapshot files (skip if first epoch) - if ( epo_d > 0 ) { - c3_c chk_c[8193]; - snprintf(chk_c, 8192, "%s/.urb/chk", u3_Host.dir_c); - if ( c3n == u3e_backup(chk_c, epo_c, c3y) ) { - fprintf(stderr, "disk: copy epoch snapshot failed\r\n"); - goto fail1; - } + c3_c chk_c[8193]; + snprintf(chk_c, 8192, "%s/.urb/chk", u3_Host.dir_c); + if ( c3n == u3e_backup(chk_c, epo_c, c3y) ) { + fprintf(stderr, "disk: copy epoch snapshot failed\r\n"); + goto fail1; } if ( -1 == (epo_i = c3_open(epo_c, O_RDONLY)) ) { @@ -1115,7 +1217,7 @@ u3_disk_epoc_init(u3_disk* log_u, c3_d epo_d) c3_c epv_c[8193]; snprintf(epv_c, sizeof(epv_c), "%s/epoc.txt", epo_c); FILE* epv_f = fopen(epv_c, "w"); // XX errors - fprintf(epv_f, "%d", U3D_VER1); + fprintf(epv_f, "%d", U3E_VERLAT); fclose(epv_f); // create binary version file, overwriting any existing file @@ -1125,38 +1227,29 @@ u3_disk_epoc_init(u3_disk* log_u, c3_d epo_d) fprintf(biv_f, URBIT_VERSION); fclose(biv_f); - // get metadata from old epoch or unmigrated event log's db + // get metadata from old log c3_d who_d[2]; c3_o fak_o; c3_w lif_w; - if ( c3y == eps_o ) { // skip if no epochs yet - if ( c3y != u3_disk_read_meta(log_u->mdb_u, who_d, &fak_o, &lif_w) ) { - fprintf(stderr, "disk: failed to read metadata\r\n"); - goto fail3; - } - - u3_lmdb_exit(log_u->mdb_u); - log_u->mdb_u = 0; + if ( c3y != u3_disk_read_meta(log_u->mdb_u, 0, who_d, &fak_o, &lif_w) ) { + fprintf(stderr, "disk: failed to read metadata\r\n"); + goto fail3; } + + u3_lmdb_exit(log_u->mdb_u); + log_u->mdb_u = 0; // initialize db of new epoch - if ( c3y == u3_Host.ops_u.nuu || epo_d > 0 ) { - c3_c dat_c[8193]; - snprintf(dat_c, sizeof(dat_c), "%s/data.mdb", epo_c); - - if ( 0 == (log_u->mdb_u = u3_lmdb_init(epo_c, siz_i)) ) { - fprintf(stderr, "disk: failed to initialize database\r\n"); - c3_free(log_u); - goto fail3; - } + if ( 0 == (log_u->mdb_u = u3_lmdb_init(epo_c, siz_i)) ) { + fprintf(stderr, "disk: failed to initialize database\r\n"); + c3_free(log_u); + goto fail3; } // write the metadata to the database - if ( c3y == eps_o ) { - if ( c3n == u3_disk_save_meta(log_u->mdb_u, who_d, fak_o, lif_w) ) { - fprintf(stderr, "disk: failed to save metadata\r\n"); - goto fail3; - } + if ( c3n == u3_disk_save_meta(log_u->mdb_u, U3D_VERLAT, who_d, fak_o, lif_w) ) { + fprintf(stderr, "disk: failed to save metadata\r\n"); + goto fail3; } if ( -1 == c3_sync(epo_i) ) { // XX fdatasync on linux? @@ -1167,8 +1260,11 @@ u3_disk_epoc_init(u3_disk* log_u, c3_d epo_d) close(epo_i); + fprintf(stderr, "disk: created epoch %" PRIc3_d "\r\n", epo_d); + // load new epoch directory and set it in log_u log_u->epo_d = epo_d; + log_u->ver_w = U3D_VERLAT; // success return c3y; @@ -1183,10 +1279,10 @@ u3_disk_epoc_init(u3_disk* log_u, c3_d epo_d) return c3n; } -/* u3_disk_epoc_kill: delete an epoch. +/* _disk_epoc_kill: delete an epoch. */ -c3_o -u3_disk_epoc_kill(u3_disk* log_u, c3_d epo_d) +static c3_o +_disk_epoc_kill(u3_disk* log_u, c3_d epo_d) { // get epoch directory c3_c epo_c[8193]; @@ -1226,20 +1322,26 @@ u3_disk_epoc_last(u3_disk* log_u, c3_d* lat_d) u3_dire* die_u = u3_foil_folder(log_u->com_u->pax_c); u3_dent* den_u = die_u->dil_u; c3_o ret_o = c3n; - - *lat_d = 0; + c3_d epo_d = 0; + c3_d val_d; + c3_i car_i; while ( den_u ) { - c3_d epo_d = 0; - if ( 1 == sscanf(den_u->nam_c, "0i%" PRIc3_d, &epo_d) ) { + if ( (1 == sscanf(den_u->nam_c, "0i%" SCNu64 "%n", &val_d, &car_i)) + && (0 < car_i) + && ('\0' == *(den_u->nam_c + car_i)) ) + { ret_o = c3y; // NB: returns yes if the directory merely exists - *lat_d = c3_max(epo_d, *lat_d); // update the latest epoch number + epo_d = c3_max(epo_d, val_d); } + den_u = den_u->nex_u; } u3_dire_free(die_u); + *lat_d = epo_d; + return ret_o; } @@ -1251,9 +1353,17 @@ u3_disk_epoc_list(u3_disk* log_u, c3_d* sot_d) u3_dire* ned_u = u3_foil_folder(log_u->com_u->pax_c); u3_dent* den_u = ned_u->dil_u; c3_z len_z = 0; + c3_i car_i; + c3_d val_d; while ( den_u ) { // count epochs - len_z++; + if ( (1 == sscanf(den_u->nam_c, "0i%" SCNu64 "%n", &val_d, &car_i)) + && (0 < car_i) + && ('\0' == *(den_u->nam_c + car_i)) ) + { + len_z++; + } + den_u = den_u->nex_u; } @@ -1266,9 +1376,13 @@ u3_disk_epoc_list(u3_disk* log_u, c3_d* sot_d) den_u = ned_u->dil_u; while ( den_u ) { - if ( 1 == sscanf(den_u->nam_c, "0i%" PRIc3_d, (sot_d + len_z)) ) { - len_z++; + if ( (1 == sscanf(den_u->nam_c, "0i%" SCNu64 "%n", &val_d, &car_i)) + && (0 < car_i) + && ('\0' == *(den_u->nam_c + car_i)) ) + { + sot_d[len_z++] = val_d; } + den_u = den_u->nex_u; } @@ -1289,47 +1403,36 @@ u3_disk_epoc_list(u3_disk* log_u, c3_d* sot_d) return len_z; } -/* _disk_need_migrate: does the disk need to be migrated? -*/ -static c3_o -_disk_need_migrate(u3_disk* log_u) -{ - c3_c dut_c[8193]; - snprintf(dut_c, sizeof(dut_c), "%s/data.mdb", log_u->com_u->pax_c); - - // check if data.mdb is readable in log directory - // - if ( 0 == access(dut_c, F_OK)) { - return c3y; - } - - return c3n; -} - /* _disk_migrate: migrates disk format. */ static c3_o _disk_migrate(u3_disk* log_u, c3_d eve_d) { /* migration steps: - * 0. detect whether we need to migrate or not - * a. if it's a fresh boot via u3_Host.ops_u.nuu -> skip migration - * b. if data.mdb is readable in log directory -> execute migration - * if not -> skip migration (returns yes) - * 1. initialize epoch 0i0 (first call to u3_disk_epoc_init()) - * a. creates epoch directory - * b. creates epoch version file - * c. creates binary version file - * d. initializes database - * e. reads metadata from old database - * f. writes metadata to new database - * g. loads new epoch directory and sets it in log_u + * 1. initialize epoch 0i0 (see u3_disk_epoc_zero) * 2. create hard links to data.mdb and lock.mdb in 0i0/ - * 3. rollover to new epoch (second call to u3_disk_epoc_init()) - * a. same as 1a-g but also copies current snapshot between c/d steps - * 4. delete backup snapshot (c3_unlink() and c3_rmdir() calls) - * 5. delete old data.mdb and lock.mdb files (c3_unlink() calls) + * 3. use scratch space to initialize new log/data.db in log/tmp + * 4. save old metadata to new db in scratch space + * 5. clobber old log/data.mdb with new log/tmp/data.mdb + * 6. open epoch lmdb and set it in log_u */ + + // NB: requires that log_u->mdb_u is initialized to log/data.mdb + // XX: put old log in separate pointer (old_u?)? + + // get metadata from old log + c3_d who_d[2]; + c3_o fak_o; + c3_w lif_w; + + if ( c3y != u3_disk_read_meta(log_u->mdb_u, 0, who_d, &fak_o, &lif_w) ) { + fprintf(stderr, "disk: failed to read metadata\r\n"); + return c3n; + } + + // finish with old log + u3_lmdb_exit(log_u->mdb_u); + log_u->mdb_u = 0; // check if lock.mdb is readable in log directory c3_o luk_o = c3n; @@ -1339,30 +1442,10 @@ _disk_migrate(u3_disk* log_u, c3_d eve_d) luk_o = c3y; } - fprintf(stderr, "disk: migrating disk to v%d format\r\n", U3D_VER1); - - // migrate existing pier which has either: - // - not started the migration, or - // - crashed before completing the migration - - // get first/last event numbers from pre-migrated lmdb - c3_d fir_d, las_d; - if ( c3n == u3_lmdb_gulf(log_u->mdb_u, &fir_d, &las_d) ) { - fprintf(stderr, "disk: failed to get first/last event numbers\r\n"); - return c3n; - } - - // ensure there's a current snapshot - if ( eve_d != las_d ) { - fprintf(stderr, "disk: snapshot is out of date, please " - "start/shutdown your pier gracefully first\r\n"); - fprintf(stderr, "disk: eve_d (%" PRIc3_d ") != las_d (%" PRIc3_d ")\r\n", - eve_d, las_d); - return c3n; - } + fprintf(stderr, "disk: migrating disk to v%d format\r\n", U3D_VERLAT); // initialize first epoch "0i0" - if ( c3n == u3_disk_epoc_init(log_u, 0) ) { + if ( c3n == u3_disk_epoc_zero(log_u) ) { fprintf(stderr, "disk: failed to initialize first epoch\r\n"); return c3n; } @@ -1374,23 +1457,17 @@ _disk_migrate(u3_disk* log_u, c3_d eve_d) snprintf(dat_c, sizeof(dat_c), "%s/data.mdb", epo_c); snprintf(lok_c, sizeof(lok_c), "%s/lock.mdb", epo_c); - if ( 0 < c3_link(dut_c, dat_c) ) { + if ( c3_link(dut_c, dat_c) && (EEXIST != errno) ) { fprintf(stderr, "disk: failed to create data.mdb hard link\r\n"); return c3n; } if ( c3y == luk_o ) { // only link lock.mdb if it exists - if ( 0 < c3_link(luk_c, lok_c) ) { + if ( c3_link(luk_c, lok_c) && (EEXIST != errno) ) { fprintf(stderr, "disk: failed to create lock.mdb hard link\r\n"); return c3n; } } - // rollover to new epoch - if ( c3n == u3_disk_epoc_init(log_u, las_d) ) { - fprintf(stderr, "disk: failed to initialize new epoch\r\n"); - return c3n; - } - // delete backup snapshot c3_c bhk_c[8193], nop_c[8193], sop_c[8193]; snprintf(bhk_c, sizeof(bhk_c), "%s/.urb/bhk", u3_Host.dir_c); @@ -1411,19 +1488,59 @@ _disk_migrate(u3_disk* log_u, c3_d eve_d) } } - // delete old lock.mdb and data.mdb files - if ( c3_unlink(luk_c) ) { - fprintf(stderr, "disk: failed to unlink lock.mdb: %s\r\n", + // use scratch space to initialize new log/data.db + // and clobber old log/data.db + // + c3_c tmp_c[8193]; + snprintf(tmp_c, sizeof(tmp_c), "%s/tmp", log_u->com_u->pax_c); + if ( c3_mkdir(tmp_c, 0700) && (errno != EEXIST) ) { + fprintf(stderr, "disk: failed to create log/tmp: %s\r\n", + strerror(errno)); + return c3n; + } + + if ( 0 == (log_u->mdb_u = u3_lmdb_init(tmp_c, siz_i)) ) { + fprintf(stderr, "disk: failed to initialize database at %s\r\n", + tmp_c); + return c3n; + } + + if ( c3n == u3_disk_save_meta(log_u->mdb_u, U3D_VERLAT, who_d, fak_o, lif_w) ) { + fprintf(stderr, "disk: failed to save metadata\r\n"); + return c3n; + } + + // atomic truncation of old log + // + u3_lmdb_exit(log_u->mdb_u); + log_u->mdb_u = 0; + + c3_c trd_c[8193]; + snprintf(trd_c, sizeof(trd_c), "%s/data.mdb", tmp_c); + if ( c3_rename(trd_c, dut_c) ) { + fprintf(stderr, "disk: failed mv %s to %s: %s\r\n", trd_c, dut_c, + strerror(errno)); + return c3n; + } + + snprintf(trd_c, sizeof(trd_c), "%s/lock.mdb", tmp_c); + if ( c3_unlink(trd_c) && (errno != ENOENT) ) { + fprintf(stderr, "disk: failed to delete log/tmp: %s\r\n", strerror(errno)); } - if ( c3_unlink(dut_c) ) { - fprintf(stderr, "disk: failed to unlink data.mdb: %s\r\n", + if ( c3_rmdir(tmp_c) ) { + fprintf(stderr, "disk: failed to delete log/tmp: %s\r\n", strerror(errno)); - return c3n; // migration succeeds only if we can unlink data.mdb + } + + if ( 0 == (log_u->mdb_u = u3_lmdb_init(epo_c, siz_i)) ) { + fprintf(stderr, "disk: failed to initialize database at %s\r\n", + epo_c); + return c3n; } // success - fprintf(stderr, "disk: migrated disk to v%d format\r\n", U3D_VER1); + fprintf(stderr, "disk: migrated disk to v%d format\r\n", U3D_VERLAT); return c3y; } @@ -1453,18 +1570,119 @@ _disk_vere_diff(u3_disk* log_u) void u3_disk_kindly(u3_disk* log_u, c3_d eve_d) { - if ( c3y == _disk_need_migrate(log_u) ) { - if ( c3n == _disk_migrate(log_u, eve_d) ) { - fprintf(stderr, "disk: failed to migrate event log\r\n"); + // ensure there's a current snapshot + // + if ( eve_d != log_u->dun_d ) { + fprintf(stderr, "disk: snapshot (event %" PRIc3_d ") is out of date\r\n" + " (latest event is %" PRIc3_d "\r\n", + eve_d, log_u->dun_d); + // XX need better instructions + // + fprintf(stderr, "start/shutdown your pier gracefully first\r\n"); + exit(1); + } + + switch ( log_u->ver_w ) { + case U3D_VER1: { + // set version to 2 (migration in progress) + c3_w ver_w = U3D_VER2; + if ( c3n == _disk_save_meta(log_u->mdb_u, "version", 4, (c3_y*)&ver_w) ) { + fprintf(stderr, "disk: failed to set version to 2\r\n"); + exit(1); + } + } // fallthru + + case U3D_VER2: { + if ( c3n == _disk_migrate(log_u, eve_d) ) { + fprintf(stderr, "disk: failed to migrate event log\r\n"); + exit(1); + } + + if ( c3n == _disk_epoc_roll(log_u, eve_d) ) { + fprintf(stderr, "disk: failed to initialize epoch\r\n"); + exit(1); + } + } break; + + case U3D_VER3: { + if ( (0 == log_u->epo_d) || + (c3y == _disk_vere_diff(log_u)) ) + { + if ( c3n == _disk_epoc_roll(log_u, eve_d) ) { + fprintf(stderr, "disk: failed to initialize epoch\r\n"); + exit(1); + } + } + } break; + + default: { + fprintf(stderr, "disk: unknown disk version: %d\r\n", log_u->ver_w); exit(1); } } - else if ( c3y == _disk_vere_diff(log_u) ) { - if ( c3n == u3_disk_epoc_init(log_u, log_u->dun_d) ) { - fprintf(stderr, "disk: failed to initialize epoch\r\n"); +} + +/* u3_disk_chop(): delete all but the latest 2 epocs. +*/ +void +u3_disk_chop(u3_disk* log_u, c3_d eve_d) +{ + c3_z len_z = u3_disk_epoc_list(log_u, 0); + c3_d* sot_d = c3_malloc(len_z * sizeof(c3_d)); + u3_disk_epoc_list(log_u, sot_d); + + if ( len_z <= 2 ) { + fprintf(stderr, "chop: nothing to do, have a great day\r\n"); + exit(0); // enjoy + } + + // delete all but the last two epochs + // + // XX parameterize the number of epochs to chop + // + for ( c3_z i_z = 2; i_z < len_z; i_z++ ) { + fprintf(stderr, "chop: deleting epoch 0i%" PRIu64 "\r\n", + sot_d[i_z]); + if ( c3y != _disk_epoc_kill(log_u, sot_d[i_z]) ) { + fprintf(stderr, "chop: failed to delete epoch 0i%" PRIu64 "\r\n", sot_d[i_z]); exit(1); } } + + // cleanup + c3_free(sot_d); + + // success + fprintf(stderr, "chop: event log truncation complete\r\n"); +} + +/* u3_disk_roll(): rollover to a new epoc. +*/ +void +u3_disk_roll(u3_disk* log_u, c3_d eve_d) +{ + // XX get fir_d from log_u + c3_d fir_d, las_d; + + if ( c3n == u3_lmdb_gulf(log_u->mdb_u, &fir_d, &las_d) ) { + fprintf(stderr, "roll: failed to read first/last event numbers\r\n"); + exit(1); + } + + if ( fir_d == las_d ) { + fprintf(stderr, "roll: latest epoch is empty\r\n"); + exit(0); + } + + if ( (eve_d != las_d) || (eve_d != log_u->dun_d) ) { + fprintf(stderr, "roll: shenanigans!\r\n"); + exit(1); + } + + else if ( c3n == _disk_epoc_roll(log_u, eve_d) ) { + fprintf(stderr, "roll: failed to create new epoch\r\n"); + exit(1); + } } typedef enum { @@ -1485,6 +1703,8 @@ _disk_epoc_load(u3_disk* log_u, c3_d lat_d) { c3_c ver_c[8]; c3_w ver_w; + c3_i car_i; + if ( c3n == _disk_epoc_meta(log_u, lat_d, "epoc", sizeof(ver_c) - 1, ver_c) ) { @@ -1494,18 +1714,19 @@ _disk_epoc_load(u3_disk* log_u, c3_d lat_d) return _epoc_gone; } - if ( 1 != sscanf(ver_c, "%d", &ver_w) ) { + if ( (1 != sscanf(ver_c, "%" SCNu32 "%n", &ver_w, &car_i)) + && (0 < car_i) + && ('\0' == *(ver_c + car_i)) ) + { fprintf(stderr, "disk: failed to parse epoch version: '%s'\r\n", ver_c); return _epoc_fail; } - if ( U3D_VER1 != ver_w ) { + if ( U3E_VERLAT != ver_w ) { fprintf(stderr, "disk: unknown epoch version: '%s', expected '%d'\r\n", - ver_c, U3D_VER1); + ver_c, U3E_VERLAT); return _epoc_late; } - - log_u->ver_w = ver_w; } // set path to latest epoch @@ -1533,7 +1754,7 @@ _disk_epoc_load(u3_disk* log_u, c3_d lat_d) if ( (c3n == u3_Host.ops_u.nuu ) && !fir_d && !las_d - && (c3n == u3_disk_read_meta(log_u->mdb_u, 0, 0, 0)) ) + && (c3n == u3_disk_read_meta(log_u->mdb_u, 0, 0, 0, 0)) ) { u3_lmdb_exit(log_u->mdb_u); log_u->mdb_u = 0; @@ -1541,6 +1762,9 @@ _disk_epoc_load(u3_disk* log_u, c3_d lat_d) } // initialize dun_d/sen_d values + // + // XX save [fir_d] in struct, check on play + // log_u->dun_d = ( 0 != las_d ) ? las_d : lat_d; log_u->sen_d = log_u->dun_d; @@ -1618,63 +1842,98 @@ u3_disk_init(c3_c* pax_c, u3_disk_cb cb_u) return 0; } - // if fresh boot, initialize disk v1 + // check if old data.mdb file exists + c3_c dut_c[8193]; + c3_o exs_o = c3n; + snprintf(dut_c, sizeof(dut_c), "%s/data.mdb", log_c); + if ( 0 == access(dut_c, F_OK) ) { + exs_o = c3y; + } + + // if fresh boot, initialize disk U3D_VERLAT // if ( c3y == u3_Host.ops_u.nuu ) { + // ensure old data.mdb file does not exist + if ( c3y == exs_o ) { + fprintf(stderr, "disk: old data.mdb file exists\r\n"); + c3_free(log_u); + return 0; + } + // initialize first epoch "0i0" - if ( c3n == u3_disk_epoc_init(log_u, 0) ) { + if ( c3n == u3_disk_epoc_zero(log_u) ) { fprintf(stderr, "disk: failed to initialize first epoch\r\n"); + c3_free(log_u); return 0; } - } - else if ( c3y == _disk_need_migrate(log_u) ) { - fprintf(stderr, "disk: loading old format\r\n"); - if ( 0 == (log_u->mdb_u = u3_lmdb_init(log_c, siz_i)) ) { + if ( _epoc_good != _disk_epoc_load(log_u, 0) ) { fprintf(stderr, "disk: failed to initialize lmdb\r\n"); c3_free(log_u); + return 0; } - c3_d fir_d; - if ( c3n == u3_lmdb_gulf(log_u->mdb_u, &fir_d, &log_u->dun_d) ) { - fprintf(stderr, "disk: failed to load latest event from lmdb\r\n"); - // XX dispose mdb_u + return log_u; + } + + if ( c3y == exs_o ) { + // load the old data.mdb file + if ( 0 == (log_u->mdb_u = u3_lmdb_init(log_c, siz_i)) ) { + fprintf(stderr, "disk: failed to initialize lmdb\r\n"); c3_free(log_u); return 0; } - // if a top-level log exists but is empty it's safe to remove. - // for extra safety, we only do this if we also have epochs. - // (the top-level log was most likely created by vere downgrade) - // - c3_d nop_d; - if ( !fir_d - && !log_u->dun_d - && (c3y == u3_disk_epoc_last(log_u, &nop_d)) ) + // read version from old log + if ( c3n == u3_disk_read_meta(log_u->mdb_u, &log_u->ver_w, 0, 0, 0) ) { + fprintf(stderr, "disk: failed to read metadata\r\n"); + goto try_epoc_no; + } + } + else { +try_epoc_no: { - c3_c luk_c[8193]; c3_c dut_c[8193]; - snprintf(dut_c, sizeof(dut_c), "%s/data.mdb", log_c); - snprintf(luk_c, sizeof(luk_c), "%s/lock.mdb", log_u->com_u->pax_c); - - u3_lmdb_exit(log_u->mdb_u); - log_u->mdb_u = 0; - - if ( c3_unlink(dut_c) - || ( (0 == access(luk_c, R_OK)) - && c3_unlink(luk_c) )) - { - fprintf(stderr, "disk: failed to unlink empty, old log\r\n"); + c3_d lat_d; + if ( c3n == u3_disk_epoc_last(log_u, &lat_d) ) { + fprintf(stderr, "disk: no event log anywhere\r\n"); + c3_free(log_u); + return 0; } - - // continue to epoc initialization + // presume pre-release migrated pier + log_u->ver_w = U3D_VERLAT; + exs_o = c3n; } + } + + switch ( log_u->ver_w ) { + case U3D_VER1: + case U3D_VER2: { // XX maybe finish migration eagerly + fprintf(stderr, "disk: loaded old log\r\n"); + c3_d fir_d; + if ( c3n == u3_lmdb_gulf(log_u->mdb_u, &fir_d, &log_u->dun_d) ) { + fprintf(stderr, "disk: failed to load latest event from lmdb\r\n"); + c3_free(log_u); + return 0; + } - else { log_u->sen_d = log_u->dun_d; - log_u->ver_w = 0; return log_u; } + + case U3D_VER3: break; + + default: { + fprintf(stderr, "disk: unknown log version: %d\r\n", log_u->ver_w); + c3_free(log_u); + return 0; + } + } + + // close lmdb + if ( c3y == exs_o ) { + u3_lmdb_exit(log_u->mdb_u); + log_u->mdb_u = 0; } // get latest epoch number @@ -1703,6 +1962,27 @@ u3_disk_init(c3_c* pax_c, u3_disk_cb cb_u) u3t_trace_open(pax_c); #endif + if ( c3n == exs_o ) { + fprintf(stderr, "disk: repairing pre-release pier metadata\r\n"); + + // read metadata from epoch's log + c3_d who_d[2]; + c3_o fak_o; + c3_w lif_w; + if ( c3n == u3_disk_read_meta(log_u->mdb_u, 0, who_d, &fak_o, &lif_w) ) + { + fprintf(stderr, "disk: failed to read metadata\r\n"); + c3_free(log_u); + return 0; + } + + if ( c3n == u3_disk_save_meta_meta(log_c, who_d, fak_o, lif_w) ) { + fprintf(stderr, "disk: failed to save top-level metadata\r\n"); + c3_free(log_u); + return 0; + } + } + return log_u; } break; @@ -1718,7 +1998,9 @@ u3_disk_init(c3_c* pax_c, u3_disk_cb cb_u) return 0; } - c3_z len_z = u3_disk_epoc_list(log_u, 0); + c3_z dir_z = u3_disk_epoc_list(log_u, 0); + c3_d* sot_d = c3_malloc(dir_z * sizeof(*sot_d)); + c3_z len_z = u3_disk_epoc_list(log_u, sot_d); if ( len_z <= 1 ) { fprintf(stderr, "only epoch is bad, bailing out\r\n"); @@ -1726,14 +2008,11 @@ u3_disk_init(c3_c* pax_c, u3_disk_cb cb_u) return 0; } - c3_d* sot_d = c3_malloc(len_z * sizeof(*sot_d)); - u3_disk_epoc_list(log_u, sot_d); - fprintf(stderr, "disk: latest epoch is 0i%" PRIc3_d " is bogus; " "falling back to previous at 0i%" PRIc3_d "\r\n", lat_d, sot_d[1]); - u3_disk_epoc_kill(log_u, lat_d); + _disk_epoc_kill(log_u, lat_d); lat_d = sot_d[1]; try_o = c3y; diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index 5ce7a96a19..7f0de222e4 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -1081,7 +1081,7 @@ _ames_czar_here(u3_pact* pac_u, time_t now, struct sockaddr_in* add_u) 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", pac_u->rut_u.dns_c, nam_c); + u3l_log("ames: czar %s ip %s", pac_u->rut_u.dns_c, nam_c); c3_free(nam_c); u3z(nam); @@ -1107,7 +1107,7 @@ _stun_czar_here(u3_ames* sam_u, time_t now, struct sockaddr_in* add_u) u3_noun nam = u3dc("scot", c3__if, u3i_word(pip_w)); c3_c* nam_c = u3r_string(nam); - u3l_log("stun: czar %s: ip %s", sam_u->dns_c, nam_c); + u3l_log("stun: czar %s ip %s", sam_u->sun_u.dns_c, nam_c); c3_free(nam_c); u3z(nam); diff --git a/pkg/vere/io/http.c b/pkg/vere/io/http.c index b5cb1d12cb..6fb607ad5c 100644 --- a/pkg/vere/io/http.c +++ b/pkg/vere/io/http.c @@ -729,6 +729,8 @@ _http_req_cache(u3_hreq* req_u) u3_noun url = u3dc("scot", 't', _http_vec_to_atom(req_u->rec_u->path)); u3_weak sac = u3h_get(htd_u->sax_p, url); + u3z(url); + if ( u3_none == sac ) { return c3n; } @@ -739,7 +741,7 @@ _http_req_cache(u3_hreq* req_u) req_u->peq_u = c3_malloc(sizeof(*req_u->peq_u)); req_u->peq_u->req_u = req_u; req_u->peq_u->htd_u = htd_u; - req_u->peq_u->pax = sac; + req_u->peq_u->pax = u3k(sac); req_u->sat_e = u3_rsat_peek; diff --git a/pkg/vere/io/xmas.c b/pkg/vere/io/mesa.c similarity index 70% rename from pkg/vere/io/xmas.c rename to pkg/vere/io/mesa.c index 196896f2a5..5786e69f8a 100644 --- a/pkg/vere/io/xmas.c +++ b/pkg/vere/io/mesa.c @@ -3,12 +3,11 @@ #include "vere.h" #include "ivory.h" - #include "noun.h" #include "ur.h" #include "cubic.h" -#include "xmas/xmas.h" -#include "xmas/bitset.h" +#include "mesa/mesa.h" +#include "mesa/bitset.h" #include #include #include @@ -23,83 +22,79 @@ c3_o dop_o = c3n; -// #define XMAS_DEBUG c3y -//#define XMAS_TEST +#define MESA_DEBUG c3y +//#define MESA_TEST #define RED_TEXT "\033[0;31m" #define DEF_TEXT "\033[0m" #define REORDER_THRESH 5 // logging and debug symbols -#define XMAS_SYM_DESC(SYM) XMAS_DESC_ ## SYM -#define XMAS_SYM_FIELD(SYM) XMAS_FIELD_ ## SYM -#ifdef XMAS_DEBUG - #define XMAS_LOG(SYM, ...) { sam_u->sat_u.XMAS_SYM_FIELD(SYM)++; u3l_log("xmas: (%u) %s", __LINE__, XMAS_SYM_DESC(SYM)); } +#define MESA_SYM_DESC(SYM) MESA_DESC_ ## SYM +#define MESA_SYM_FIELD(SYM) MESA_FIELD_ ## SYM +#ifdef MESA_DEBUG + #define MESA_LOG(SYM, ...) { sam_u->sat_u.MESA_SYM_FIELD(SYM)++; u3l_log("mesa: (%u) %s", __LINE__, MESA_SYM_DESC(SYM)); } #else - #define XMAS_LOG(SYM, ...) { sam_u->sat_u.XMAS_SYM_FIELD(SYM)++; } + #define MESA_LOG(SYM, ...) { sam_u->sat_u.MESA_SYM_FIELD(SYM)++; } #endif -typedef struct _u3_xmas_stat { +typedef struct _u3_mesa_stat { c3_w dop_w; // dropped for other reasons c3_w ser_w; // dropped for serialisation c3_w aut_w; // droppped for auth c3_w apa_w; // dropped bc no interest c3_w inv_w; // non-fatal invariant violation c3_w dup_w; // duplicates -} u3_xmas_stat; - -#define XMAS_DESC_STRANGE "dropped strange packet" -#define XMAS_FIELD_STRANGE dop_w +} u3_mesa_stat; -#define XMAS_DESC_SERIAL "dropped packet (serialisation)" -#define XMAS_FIELD_SERIAL ser_w +#define MESA_DESC_STRANGE "dropped strange packet" +#define MESA_FIELD_STRANGE dop_w -#define XMAS_DESC_AUTH "dropped packet (authentication)" -#define XMAS_FIELD_AUTH aut_w +#define MESA_DESC_SERIAL "dropped packet (serialisation)" +#define MESA_FIELD_SERIAL ser_w -#define XMAS_DESC_INVARIANT "invariant violation" -#define XMAS_FIELD_INVARIANT inv_w +#define MESA_DESC_AUTH "dropped packet (authentication)" +#define MESA_FIELD_AUTH aut_w -#define XMAS_DESC_APATHY "dropped packet (no interest)" -#define XMAS_FIELD_APATHY apa_w +#define MESA_DESC_INVARIANT "invariant violation" +#define MESA_FIELD_INVARIANT inv_w -#define XMAS_DESC_DUPE "dropped packet (duplicate)" -#define XMAS_FIELD_DUPE dup_w +#define MESA_DESC_APATHY "dropped packet (no interest)" +#define MESA_FIELD_APATHY apa_w +#define MESA_DESC_DUPE "dropped packet (duplicate)" +#define MESA_FIELD_DUPE dup_w #define IN_FLIGHT 10 // pending interest sentinels -#define XMAS_ITEM 1 // cached item -#define XMAS_WAIT 2 // waiting on scry +#define MESA_ITEM 1 // cached item +#define MESA_WAIT 2 // waiting on scry // routing table sentinels -#define XMAS_CZAR 1 // pending dns lookup -#define XMAS_ROUT 2 // have route +#define MESA_CZAR 1 // pending dns lookup +#define MESA_ROUT 2 // have route // // hop enum - #define SIFT_VAR(dest, src, len) dest = 0; for(int i = 0; i < len; i++ ) { dest |= ((src + i) >> (8*i)); } -#define CHECK_BOUNDS(cur) if ( len_w < cur ) { u3l_log("xmas: failed parse (%u,%u) at line %i", len_w, cur, __LINE__); return 0; } +#define CHECK_BOUNDS(cur) if ( len_w < cur ) { u3l_log("mesa: failed parse (%u,%u) at line %i", len_w, cur, __LINE__); return 0; } #define safe_dec(num) (num == 0 ? num : num - 1) -#define _xmas_met3_w(a_w) ((c3_bits_word(a_w) + 0x7) >> 3) - - +#define _mesa_met3_w(a_w) ((c3_bits_word(a_w) + 0x7) >> 3) -/** typedef u3_xmas_pack_stat u3_noun +/** typedef u3_mesa_pack_stat u3_noun * [last=@da tries=@ud] */ -struct _u3_xmas_pact; +struct _u3_mesa_pact; typedef struct _u3_pact_stat { c3_d sen_d; // last sent - c3_y sip_y; // skips + c3_y sip_y; // skips c3_y tie_y; // tries c3_y dup_y; // dupes } u3_pact_stat; -struct _u3_xmas; +struct _u3_mesa; typedef struct _u3_peer_last { c3_d acc_d; // time of last access @@ -113,13 +108,13 @@ typedef struct _u3_misord_buf { blake_pair* par_u; } u3_misord_buf; -struct _u3_xmas; +struct _u3_mesa; -typedef struct _u3_xmas_pict { +typedef struct _u3_mesa_pict { uv_udp_send_t snd_u; - struct _u3_xmas* sam_u; - u3_xmas_pact pac_u; -} u3_xmas_pict; + struct _u3_mesa* sam_u; + u3_mesa_pact pac_u; +} u3_mesa_pict; typedef struct _u3_pend_req { c3_w nex_w; // number of the next fragment to be sent @@ -134,7 +129,7 @@ typedef struct _u3_pend_req { u3_gage* gag_u; // congestion control u3_vec(u3_buf) mis_u; // misordered blake hash blake_bao* bao_u; // blake verifier - u3_xmas_pict* pic_u; // preallocated request packet + u3_mesa_pict* pic_u; // preallocated request packet u3_pact_stat* wat_u; // ((mop @ud packet-state) lte) u3_bitset was_u; // ((mop @ud packet-state) lte) c3_y pad_y[64]; @@ -143,22 +138,22 @@ typedef struct _u3_pend_req { typedef struct _u3_czar_info { c3_w pip_w; // IP of galaxy c3_y imp_y; // galaxy number - struct _u3_xmas* sam_u; // backpointer + struct _u3_mesa* sam_u; // backpointer time_t tim_t; // time of retrieval c3_c* dns_c; // domain u3_noun pen; // (list @) of pending packet } u3_czar_info; -/* _u3_xmas: next generation networking +/* _u3_mesa: next generation networking */ -typedef struct _u3_xmas { +typedef struct _u3_mesa { u3_auto car_u; u3_pier* pir_u; union { uv_udp_t wax_u; uv_handle_t had_u; }; - u3_xmas_stat sat_u; // statistics + u3_mesa_stat sat_u; // statistics c3_l sev_l; // XX: ?? c3_o for_o; // is forwarding ur_cue_test_t* tes_u; // cue-test handle @@ -170,46 +165,42 @@ typedef struct _u3_xmas { u3p(u3h_root) req_p; // hashtable of u3_pend_req c3_c* dns_c; // turf (urb.otrg) c3_d tim_d; // XX: remove -} u3_xmas; - +} u3_mesa; typedef struct _u3_peer { u3_peer_last las_u; // last check timestamps u3_lane dir_u; // direct lane (if any) u3_lane ind_u; // indirect lane (if any) c3_s imp_s; // galaxy - u3_xmas* sam_u; // backpointer + u3_mesa* sam_u; // backpointer } u3_peer; - - -typedef enum _u3_xmas_ctag { +typedef enum _u3_mesa_ctag { CACE_WAIT = 1, CACE_ITEM = 2, -} u3_xmas_ctag; +} u3_mesa_ctag; /* - * typedef u3_xmas_req u3_noun + * typedef u3_mesa_req u3_noun * [tot=@ waiting=(set @ud) missing=(set @ud) nex=@ud dat=(map @ud @)] */ typedef struct _u3_cace_enty { - u3_xmas_ctag typ_y; + u3_mesa_ctag typ_y; union { - // u03_xmas_cace_wait wat_u; - // u3_xmas_cace_item res_u; + // u03_mesa_cace_wait wat_u; + // u3_mesa_cace_item res_u; }; } u3_cace_enty; - // Sent request -// because of lifecycles a u3_xmas_pact may have several libuv +// because of lifecycles a u3_mesa_pact may have several libuv // callbacks associated with it, so we can't those as callback // instead just alloc new buffer and stick here typedef struct _u3_seal { uv_udp_send_t snd_u; // udp send request - u3_xmas* sam_u; + u3_mesa* sam_u; c3_w len_w; c3_y* buf_y; } u3_seal; @@ -233,7 +224,6 @@ _log_buf(c3_y* buf_y, c3_w len_w) fprintf(stderr, "\r\n"); } - static void _log_gage(u3_gage* gag_u) { @@ -251,10 +241,9 @@ _log_gage(u3_gage* gag_u) static void _log_lane(u3_lane* lan_u) { - u3l_log("xmas: lane (%s,%u)", u3r_string(u3dc("scot", c3__if, u3i_word(lan_u->pip_w))), lan_u->por_s); + u3l_log("mesa: lane (%s,%u)", u3r_string(u3dc("scot", c3__if, u3i_word(lan_u->pip_w))), lan_u->por_s); } - static void _log_peer(u3_peer* per_u) { if ( per_u == NULL ) { @@ -304,7 +293,7 @@ _get_now_micros() return (tim_u.tv_sec * 1000000) + tim_u.tv_usec; } -static c3_d +static c3_d _abs_dif(c3_d ayy_d, c3_d bee_d) { return ayy_d > bee_d ? ayy_d - bee_d : bee_d - ayy_d; @@ -316,7 +305,7 @@ _clamp_rto(c3_d rto_d) { } static inline void -_get_her(u3_xmas_pact* pac_u, c3_d* our_d) +_get_her(u3_mesa_pact* pac_u, c3_d* our_d) { switch ( pac_u->hed_u.typ_y ) { default: { @@ -339,7 +328,7 @@ _get_her(u3_xmas_pact* pac_u, c3_d* our_d) } static c3_c* -_xmas_czar_dns(c3_y imp_y, c3_c* zar_c) +_mesa_czar_dns(c3_y imp_y, c3_c* zar_c) { u3_noun nam = u3dc("scot", 'p', imp_y); c3_c* nam_c = u3r_string(nam); @@ -356,11 +345,10 @@ _xmas_czar_dns(c3_y imp_y, c3_c* zar_c) return dns_c; } - -/* _xmas_encode_path(): produce buf_y as a parsed path +/* _mesa_encode_path(): produce buf_y as a parsed path */ static u3_noun -_xmas_encode_path(c3_w len_w, c3_y* buf_y) +_mesa_encode_path(c3_w len_w, c3_y* buf_y) { u3_noun pro; u3_noun* lit = &pro; @@ -400,9 +388,9 @@ _xmas_encode_path(c3_w len_w, c3_y* buf_y) } static void -_xmas_free_pict(u3_xmas_pict* pic_u) +_mesa_free_pict(u3_mesa_pict* pic_u) { - xmas_free_pact(&pic_u->pac_u); + mesa_free_pact(&pic_u->pac_u); c3_free(pic_u); } @@ -415,11 +403,11 @@ _dire_etch_ud(c3_d num_d) return u3i_bytes(26 - dif_w, buf_y); // XX known-non-null } -/* _xmas_request_key(): produce key for request hashtable sam_u->req_p from nam_u +/* _mesa_request_key(): produce key for request hashtable sam_u->req_p from nam_u */ -u3_noun _xmas_request_key(u3_xmas_name* nam_u) +u3_noun _mesa_request_key(u3_mesa_name* nam_u) { - u3_noun pax = _xmas_encode_path(nam_u->pat_s, (c3_y*)nam_u->pat_c); + u3_noun pax = _mesa_encode_path(nam_u->pat_s, (c3_y*)nam_u->pat_c); u3_noun res = u3nt(u3i_chubs(2, nam_u->her_d), u3i_word(nam_u->rif_w), pax); return res; } @@ -434,27 +422,26 @@ static void _init_gage(u3_gage* gag_u) gag_u->sst_w = 10000; } - -/* u3_xmas_encode_lane(): serialize lane to noun +/* u3_mesa_encode_lane(): serialize lane to noun */ static u3_noun -u3_xmas_encode_lane(u3_lane lan_u) { +u3_mesa_encode_lane(u3_lane lan_u) { // [%if ip=@ port=@] return u3nt(c3__if, u3i_word(lan_u.pip_w), lan_u.por_s); } // lane cache is (map [lane @p] lane-info) -// +// static u3_noun -_xmas_lane_key(c3_d her_d[2], u3_lane* lan_u) +_mesa_lane_key(c3_d her_d[2], u3_lane* lan_u) { - return u3nc(u3i_chubs(2,her_d), u3_xmas_encode_lane(*lan_u)); + return u3nc(u3i_chubs(2,her_d), u3_mesa_encode_lane(*lan_u)); } /* RETAIN */ static u3_gage* -_xmas_get_lane_raw(u3_xmas* sam_u, u3_noun key) +_mesa_get_lane_raw(u3_mesa* sam_u, u3_noun key) { u3_gage* ret_u = NULL; u3_weak res = u3h_git(sam_u->lan_p, key); @@ -466,25 +453,25 @@ _xmas_get_lane_raw(u3_xmas* sam_u, u3_noun key) return ret_u; } -/* _xmas_get_lane(): get lane +/* _mesa_get_lane(): get lane */ static u3_gage* -_xmas_get_lane(u3_xmas* sam_u, c3_d her_d[2], u3_lane* lan_u) { - u3_noun key =_xmas_lane_key(her_d, lan_u); - u3_gage* ret_u = _xmas_get_lane_raw(sam_u, key); +_mesa_get_lane(u3_mesa* sam_u, c3_d her_d[2], u3_lane* lan_u) { + u3_noun key =_mesa_lane_key(her_d, lan_u); + u3_gage* ret_u = _mesa_get_lane_raw(sam_u, key); u3z(key); return ret_u; } -/* _xmas_put_lane(): put lane state in state +/* _mesa_put_lane(): put lane state in state * - * uses same copying trick as _xmas_put_request() + * uses same copying trick as _mesa_put_request() */ static void -_xmas_put_lane(u3_xmas* sam_u, c3_d her_d[2], u3_lane* lan_u, u3_gage* gag_u) +_mesa_put_lane(u3_mesa* sam_u, c3_d her_d[2], u3_lane* lan_u, u3_gage* gag_u) { - u3_noun key = _xmas_lane_key(her_d, lan_u); - u3_gage* old_u = _xmas_get_lane_raw(sam_u, key); + u3_noun key = _mesa_lane_key(her_d, lan_u); + u3_gage* old_u = _mesa_get_lane_raw(sam_u, key); u3_gage* new_u = gag_u; if ( old_u == NULL ) { @@ -500,14 +487,14 @@ _xmas_put_lane(u3_xmas* sam_u, c3_d her_d[2], u3_lane* lan_u, u3_gage* gag_u) u3z(key); } -/* _xmas_get_request(): produce pending request state for nam_u +/* _mesa_get_request(): produce pending request state for nam_u * * produces a NULL pointer if no pending request exists */ static u3_pend_req* -_xmas_get_request(u3_xmas* sam_u, u3_xmas_name* nam_u) { +_mesa_get_request(u3_mesa* sam_u, u3_mesa_name* nam_u) { u3_pend_req* ret_u = NULL; - u3_noun key = _xmas_request_key(nam_u); + u3_noun key = _mesa_request_key(nam_u); u3_weak res = u3h_git(sam_u->req_p, key); if ( res != u3_none && res != u3_nul ) { @@ -519,8 +506,8 @@ _xmas_get_request(u3_xmas* sam_u, u3_xmas_name* nam_u) { } static void -_xmas_del_request(u3_xmas* sam_u, u3_xmas_name* nam_u) { - u3_noun key = _xmas_request_key(nam_u); +_mesa_del_request(u3_mesa* sam_u, u3_mesa_name* nam_u) { + u3_noun key = _mesa_request_key(nam_u); u3_weak req = u3h_get(sam_u->req_p, key); if ( req == u3_none ) { @@ -531,7 +518,7 @@ _xmas_del_request(u3_xmas* sam_u, u3_xmas_name* nam_u) { // u3l_log("wat_u %p", req_u->wat_u); // u3l_log("was_u buf %p", req_u->was_u.buf_y); uv_timer_stop(&req_u->tim_u); - _xmas_free_pict(req_u->pic_u); + _mesa_free_pict(req_u->pic_u); c3_free(req_u->wat_u); vec_free(&req_u->mis_u); c3_free(req_u->dat_y); @@ -541,23 +528,23 @@ _xmas_del_request(u3_xmas* sam_u, u3_xmas_name* nam_u) { u3z(key); } -/* _xmas_put_request(): save new pending request state for nam_u +/* _mesa_put_request(): save new pending request state for nam_u * - * the memory in the hashtable is allocated once in the lifecycle of the + * the memory in the hashtable is allocated once in the lifecycle of the * request. req_u will be copied into the hashtable memory, and so can be * immediately freed * */ static u3_pend_req* -_xmas_put_request(u3_xmas* sam_u, u3_xmas_name* nam_u, u3_pend_req* req_u) { - u3_noun key = _xmas_request_key(nam_u); +_mesa_put_request(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_pend_req* req_u) { + u3_noun key = _mesa_request_key(nam_u); if ( req_u == NULL) { u3h_put(sam_u->req_p, key, u3_nul); u3z(key); return req_u; } - u3_pend_req* old_u = _xmas_get_request(sam_u, nam_u); + u3_pend_req* old_u = _mesa_get_request(sam_u, nam_u); u3_pend_req* new_u = req_u; if ( old_u == NULL ) { new_u = u3a_calloc(1, sizeof(u3_pend_req)); @@ -575,10 +562,8 @@ _xmas_put_request(u3_xmas* sam_u, u3_xmas_name* nam_u, u3_pend_req* req_u) { return new_u; } - - // congestion control update -static void _xmas_handle_ack(u3_gage* gag_u, u3_pact_stat* pat_u) +static void _mesa_handle_ack(u3_gage* gag_u, u3_pact_stat* pat_u) { gag_u->con_w++; @@ -601,13 +586,13 @@ static void _xmas_handle_ack(u3_gage* gag_u, u3_pact_stat* pat_u) } /* - * _xmas_req_get_cwnd(): produce packets to send + * _mesa_req_get_cwnd(): produce packets to send * * saves next fragment number and preallocated pact into the passed pointers. * Will not do so if returning 0 */ static c3_w -_xmas_req_get_cwnd(u3_pend_req* req_u) +_mesa_req_get_cwnd(u3_pend_req* req_u) { c3_w res_w = 0; @@ -627,11 +612,11 @@ _xmas_req_get_cwnd(u3_pend_req* req_u) return c3_min(rem_w, req_u->gag_u->wnd_w - liv_w); } -/* _xmas_req_pact_sent(): mark packet as sent +/* _mesa_req_pact_sent(): mark packet as sent ** */ static void -_xmas_req_pact_resent(u3_pend_req* req_u, u3_xmas_name* nam_u) +_mesa_req_pact_resent(u3_pend_req* req_u, u3_mesa_name* nam_u) { c3_d now_d = _get_now_micros(); // if we dont have pending request noop @@ -643,11 +628,11 @@ _xmas_req_pact_resent(u3_pend_req* req_u, u3_xmas_name* nam_u) req_u->wat_u[nam_u->fra_w].tie_y++; } -/* _xmas_req_pact_sent(): mark packet as sent -** after 1-RT first packet is handled in _xmas_req_pact_init() +/* _mesa_req_pact_sent(): mark packet as sent +** after 1-RT first packet is handled in _mesa_req_pact_init() */ static void -_xmas_req_pact_sent(u3_pend_req* req_u, u3_xmas_name* nam_u) +_mesa_req_pact_sent(u3_pend_req* req_u, u3_mesa_name* nam_u) { c3_d now_d = _get_now_micros(); // if we already have pending request @@ -659,7 +644,7 @@ _xmas_req_pact_sent(u3_pend_req* req_u, u3_xmas_name* nam_u) req_u->wat_u[nam_u->fra_w] = (u3_pact_stat){now_d, 0, 1, 0 }; bitset_put(&req_u->was_u, nam_u->fra_w); } else { - u3l_log("xmas: no req for sent"); + u3l_log("mesa: no req for sent"); return; } @@ -672,7 +657,6 @@ _xmas_req_pact_sent(u3_pend_req* req_u, u3_xmas_name* nam_u) } } - /* _ames_czar_port(): udp port for galaxy. */ static c3_s @@ -686,7 +670,6 @@ _ames_czar_port(c3_y imp_y) } } - /* _ames_alloc(): libuv buffer allocator. */ static void @@ -702,10 +685,10 @@ _ames_alloc(uv_handle_t* had_u, *buf = uv_buf_init(ptr_v, 4096); } -/* u3_xmas_decode_lane(): deserialize noun to lane; 0.0.0.0:0 if invalid +/* u3_mesa_decode_lane(): deserialize noun to lane; 0.0.0.0:0 if invalid */ static u3_lane -u3_xmas_decode_lane(u3_atom lan) { +u3_mesa_decode_lane(u3_atom lan) { u3_lane lan_u; c3_d lan_d; @@ -731,40 +714,37 @@ u3_xmas_decode_lane(u3_atom lan) { // // // -static void _xmas_free_seal(u3_seal* sel_u) +static void _mesa_free_seal(u3_seal* sel_u) { c3_free(sel_u->buf_y); c3_free(sel_u); } -static u3_noun _xmas_get_now() { +static u3_noun _mesa_get_now() { struct timeval tim_u; gettimeofday(&tim_u, 0); u3_noun res = u3_time_in_tv(&tim_u); return res; } - - - static void -_xmas_send_cb(uv_udp_send_t* req_u, c3_i sas_i) +_mesa_send_cb(uv_udp_send_t* req_u, c3_i sas_i) { u3_seal* sel_u = (u3_seal*)req_u; - u3_xmas* sam_u = sel_u->sam_u; + u3_mesa* sam_u = sel_u->sam_u; if ( sas_i ) { - u3l_log("xmas: send fail_async: %s", uv_strerror(sas_i)); + u3l_log("mesa: send fail_async: %s", uv_strerror(sas_i)); //sam_u->fig_u.net_o = c3n; } else { //sam_u->fig_u.net_o = c3y; } - _xmas_free_seal(sel_u); + _mesa_free_seal(sel_u); } -static void _xmas_send_buf(u3_xmas* sam_u, u3_lane lan_u, c3_y* buf_y, c3_w len_w) +static void _mesa_send_buf(u3_mesa* sam_u, u3_lane lan_u, c3_y* buf_y, c3_w len_w) { u3_seal* sel_u = c3_calloc(sizeof(*sel_u)); // this is wrong, need to calloc & memcpy @@ -781,9 +761,9 @@ static void _xmas_send_buf(u3_xmas* sam_u, u3_lane lan_u, c3_y* buf_y, c3_w len_ add_u.sin_addr.s_addr = htonl(pip_w); add_u.sin_port = htons(por_s); -#ifdef XMAS_DEBUG +#ifdef MESA_DEBUG c3_c* sip_c = inet_ntoa(add_u.sin_addr); - // u3l_log("xmas: sending packet (%s,%u)", sip_c, por_s); + // u3l_log("mesa: sending packet (%s,%u)", sip_c, por_s); #endif uv_buf_t buf_u = uv_buf_init((c3_c*)buf_y, len_w); @@ -792,34 +772,31 @@ static void _xmas_send_buf(u3_xmas* sam_u, u3_lane lan_u, c3_y* buf_y, c3_w len_ &sam_u->wax_u, &buf_u, 1, (const struct sockaddr*)&add_u, - _xmas_send_cb); + _mesa_send_cb); if ( sas_i ) { u3l_log("ames: send fail_sync: %s", uv_strerror(sas_i)); /*if ( c3y == sam_u->fig_u.net_o ) { //sam_u->fig_u.net_o = c3n; }*/ - _xmas_free_seal(sel_u); + _mesa_free_seal(sel_u); } } - - -static void _xmas_send(u3_xmas_pict* pic_u, u3_lane* lan_u) +static void _mesa_send(u3_mesa_pict* pic_u, u3_lane* lan_u) { - u3_xmas* sam_u = pic_u->sam_u; + u3_mesa* sam_u = pic_u->sam_u; c3_y* buf_y = c3_calloc(PACT_SIZE); - c3_w siz_w = xmas_etch_pact(buf_y, &pic_u->pac_u); + c3_w siz_w = mesa_etch_pact(buf_y, &pic_u->pac_u); - _xmas_send_buf(sam_u, *lan_u, buf_y, siz_w); + _mesa_send_buf(sam_u, *lan_u, buf_y, siz_w); } - static void _try_resend(u3_pend_req* req_u) { - u3_xmas* sam_u = req_u->pic_u->sam_u; + u3_mesa* sam_u = req_u->pic_u->sam_u; u3_lane* lan_u = &req_u->lan_u; c3_o los_o = c3n; if ( req_u->tot_w == 0 || req_u->ack_w <= REORDER_THRESH ) { @@ -835,32 +812,32 @@ _try_resend(u3_pend_req* req_u) los_o = c3y; c3_y* buf_y = c3_calloc(PACT_SIZE); req_u->pic_u->pac_u.pek_u.nam_u.fra_w = i; - c3_w siz_w = xmas_etch_pact(buf_y, &req_u->pic_u->pac_u); + c3_w siz_w = mesa_etch_pact(buf_y, &req_u->pic_u->pac_u); if ( siz_w == 0 ) { u3l_log("failed to etch"); u3_assert( 0 ); } // TODO: better route management - _xmas_send_buf(sam_u, *lan_u, buf_y, siz_w); - _xmas_req_pact_resent(req_u, &req_u->pic_u->pac_u.pek_u.nam_u); + _mesa_send_buf(sam_u, *lan_u, buf_y, siz_w); + _mesa_req_pact_resent(req_u, &req_u->pic_u->pac_u.pek_u.nam_u); } else if ( (now_d - req_u->wat_u[i].sen_d) > req_u->gag_u->rto_w ) { los_o = c3y; c3_y* buf_y = c3_calloc(PACT_SIZE); req_u->pic_u->pac_u.pek_u.nam_u.fra_w = i; u3l_log("slow resending %u ", i); - c3_w siz_w = xmas_etch_pact(buf_y, &req_u->pic_u->pac_u); + c3_w siz_w = mesa_etch_pact(buf_y, &req_u->pic_u->pac_u); if( siz_w == 0 ) { u3l_log("failed to etch"); u3_assert( 0 ); } // TODO: better route management - _xmas_send_buf(sam_u, *lan_u, buf_y, siz_w); - _xmas_req_pact_resent(req_u, &req_u->pic_u->pac_u.pek_u.nam_u); + _mesa_send_buf(sam_u, *lan_u, buf_y, siz_w); + _mesa_req_pact_resent(req_u, &req_u->pic_u->pac_u.pek_u.nam_u); } } } - + if ( c3y == los_o ) { req_u->gag_u->sst_w = (req_u->gag_u->wnd_w / 2) + 1; req_u->gag_u->wnd_w = req_u->gag_u->sst_w; @@ -868,10 +845,10 @@ _try_resend(u3_pend_req* req_u) } } -/* _xmas_packet_timeout(): callback for packet timeout +/* _mesa_packet_timeout(): callback for packet timeout */ static void -_xmas_packet_timeout(uv_timer_t* tim_u) { +_mesa_packet_timeout(uv_timer_t* tim_u) { u3_pend_req* req_u = (u3_pend_req*)tim_u->data; u3l_log("%u packet timed out", req_u->old_w); _try_resend(req_u); @@ -897,17 +874,17 @@ _update_oldest_req(u3_pend_req *req_u, u3_gage* gag_u) } } if ( now_d == wen_d ) { -#ifdef XMAS_DEBUG +#ifdef MESA_DEBUG u3l_log("failed to find new oldest"); #endif } req_u->old_w = idx_w; req_u->tim_u.data = req_u; c3_d gap_d = now_d -req_u->wat_u[idx_w].sen_d; - uv_timer_start(&req_u->tim_u, _xmas_packet_timeout, (gag_u->rto_w - gap_d) / 1000, 0); + uv_timer_start(&req_u->tim_u, _mesa_packet_timeout, (gag_u->rto_w - gap_d) / 1000, 0); } -static void _xmas_free_misord_buf(u3_misord_buf* buf_u) +static void _mesa_free_misord_buf(u3_misord_buf* buf_u) { c3_free(buf_u->fra_y); if ( buf_u->par_u != NULL ) { @@ -916,8 +893,8 @@ static void _xmas_free_misord_buf(u3_misord_buf* buf_u) c3_free(buf_u); } -static bao_ingest_result -_xmas_burn_misorder_queue(u3_pend_req* req_u) +static bao_ingest_result +_mesa_burn_misorder_queue(u3_pend_req* req_u) { c3_w wan_w = req_u->bao_u->con_w; c3_w len_w; @@ -931,8 +908,8 @@ _xmas_burn_misorder_queue(u3_pend_req* req_u) fon_o = c3y; if ( BAO_GOOD != (res_y = blake_bao_verify(req_u->bao_u, buf_u->fra_y, buf_u->len_w, buf_u->par_u))) { return res_y; - } else { - _xmas_free_misord_buf((u3_misord_buf*)vec_pop(&req_u->mis_u, i)); + } else { + _mesa_free_misord_buf((u3_misord_buf*)vec_pop(&req_u->mis_u, i)); break; } } @@ -942,24 +919,24 @@ _xmas_burn_misorder_queue(u3_pend_req* req_u) break; } } - return res_y; + return res_y; } -/* _xmas_req_pact_done(): mark packet as done, returning if we should continue +/* _mesa_req_pact_done(): mark packet as done, returning if we should continue */ static u3_pend_req* -_xmas_req_pact_done(u3_xmas* sam_u, u3_xmas_name *nam_u, u3_xmas_data* dat_u, u3_lane* lan_u) +_mesa_req_pact_done(u3_mesa* sam_u, u3_mesa_name *nam_u, u3_mesa_data* dat_u, u3_lane* lan_u) { u3_weak ret = u3_none; c3_d now_d = _get_now_micros(); - u3_pend_req* req_u = _xmas_get_request(sam_u, nam_u); - + u3_pend_req* req_u = _mesa_get_request(sam_u, nam_u); + if ( NULL == req_u ) { - XMAS_LOG(APATHY); + MESA_LOG(APATHY); return NULL; } - u3_gage* gag_u = _xmas_get_lane(sam_u, nam_u->her_d, lan_u); + u3_gage* gag_u = _mesa_get_lane(sam_u, nam_u->her_d, lan_u); req_u->gag_u = gag_u; // first we hear from lane @@ -975,24 +952,24 @@ _xmas_req_pact_done(u3_xmas* sam_u, u3_xmas_name *nam_u, u3_xmas_data* dat_u, u3 // First packet received, instantiate request fully if ( dat_u->tot_w <= nam_u->fra_w ) { - XMAS_LOG(STRANGE); + MESA_LOG(STRANGE); // XX: is this sufficient to drop whole request return req_u; } // received duplicate if ( nam_u->fra_w != 0 && c3n == bitset_has(&req_u->was_u, nam_u->fra_w) ) { - XMAS_LOG(DUPE); + MESA_LOG(DUPE); req_u->wat_u[nam_u->fra_w].dup_y++; return req_u; - } + } bitset_del(&req_u->was_u, nam_u->fra_w); if ( nam_u->fra_w > req_u->ack_w ) { req_u->ack_w = nam_u->fra_w; } if ( nam_u->fra_w != 0 && req_u->wat_u[nam_u->fra_w].tie_y != 1 ) { -#ifdef XMAS_DEBUG +#ifdef MESA_DEBUG u3l_log("received retry %u", nam_u->fra_w); #endif } @@ -1012,7 +989,7 @@ _xmas_req_pact_done(u3_xmas* sam_u, u3_xmas_name *nam_u, u3_xmas_data* dat_u, u3 c3_y ver_y; // TODO: move to bottom - if ( req_u->bao_u->con_w != nam_u->fra_w ) { + if ( req_u->bao_u->con_w != nam_u->fra_w ) { // TODO: queue packet u3_misord_buf* buf_u = c3_calloc(sizeof(u3_misord_buf)); buf_u->fra_y = c3_calloc(dat_u->len_w); @@ -1024,19 +1001,19 @@ _xmas_req_pact_done(u3_xmas* sam_u, u3_xmas_name *nam_u, u3_xmas_data* dat_u, u3 } else if ( BAO_GOOD != (ver_y = blake_bao_verify(req_u->bao_u, dat_u->fra_y, dat_u->len_w, par_u)) ) { c3_free(par_u); // TODO: do we drop the whole request on the floor? - XMAS_LOG(AUTH); + MESA_LOG(AUTH); return req_u; - } else if ( vec_len(&req_u->mis_u) != 0 - && BAO_GOOD != (ver_y = _xmas_burn_misorder_queue(req_u))) { + } else if ( vec_len(&req_u->mis_u) != 0 + && BAO_GOOD != (ver_y = _mesa_burn_misorder_queue(req_u))) { c3_free(par_u); - XMAS_LOG(AUTH) + MESA_LOG(AUTH) return req_u; } else { c3_free(par_u); } // handle gauge update - _xmas_handle_ack(gag_u, &req_u->wat_u[nam_u->fra_w]); + _mesa_handle_ack(gag_u, &req_u->wat_u[nam_u->fra_w]); memcpy(req_u->dat_y + (siz_w * nam_u->fra_w), dat_u->fra_y, dat_u->len_w); @@ -1045,14 +1022,11 @@ _xmas_req_pact_done(u3_xmas* sam_u, u3_xmas_name *nam_u, u3_xmas_data* dat_u, u3 _update_oldest_req(req_u, gag_u); - // _xmas_put_request(sam_u, nam_u, req_u); - // _xmas_put_lane(sam_u, nam_u->her_d, lan_u, gag_u); + // _mesa_put_request(sam_u, nam_u, req_u); + // _mesa_put_lane(sam_u, nam_u->her_d, lan_u, gag_u); return req_u; } - - - static u3_lane _realise_lane(u3_noun lan) { u3_lane lan_u; @@ -1073,7 +1047,7 @@ _realise_lane(u3_noun lan) { u3_assert( c3y == u3a_is_cat(por) && por <= 0xFFFF); lan_u.por_s = por; } else { - u3l_log("xmas: inscrutable lane"); + u3l_log("mesa: inscrutable lane"); } u3z(lan); } @@ -1081,40 +1055,45 @@ _realise_lane(u3_noun lan) { } static c3_o -_xmas_rout_bufs(u3_xmas* sam_u, c3_y* buf_y, c3_w len_w, u3_noun las) +_mesa_rout_bufs(u3_mesa* sam_u, c3_y* buf_y, c3_w len_w, u3_noun las) { c3_o suc_o = c3n; u3_noun lan, t = las; + u3m_p("las", las); u3m_p("t", t); + // u3l_log("sending to ip: %x, port: %u", lan_u.pip_w, lan_u.por_s); while ( t != u3_nul ) { + u3m_p("t", t); u3x_cell(t, &lan, &t); u3_lane lan_u = _realise_lane(u3k(lan)); - #ifdef XMAS_DEBUG + u3l_log("sending to ip: %x, port: %u", lan_u.pip_w, lan_u.por_s); + #ifdef MESA_DEBUG u3l_log("sending to ip: %x, port: %u", lan_u.pip_w, lan_u.por_s); - - #endif /* ifdef XMAS_DEBUG + + #endif /* ifdef MESA_DEBUG u3l_log("sending to ip: %x, port: %u", lan_u.pip_w, lan_u.por_s); */ if ( lan_u.por_s == 0 ) { - u3l_log("xmas: failed to realise lane"); - } else { + u3l_log("mesa: failed to realise lane"); + } else { c3_y* sen_y = c3_calloc(len_w); memcpy(sen_y, buf_y, len_w); - _xmas_send_buf(sam_u, lan_u, sen_y, len_w); + _mesa_send_buf(sam_u, lan_u, sen_y, len_w); } } + u3m_p("t", t); // u3z(las); return suc_o; } static void -_xmas_timer_cb(uv_timer_t* tim_u) { +_mesa_timer_cb(uv_timer_t* tim_u) { u3_pend_req* req_u = tim_u->data; _try_resend(req_u); } static void -_xmas_czar_here(u3_czar_info* imp_u, time_t now_t, struct sockaddr_in* add_u) +_mesa_czar_here(u3_czar_info* imp_u, time_t now_t, struct sockaddr_in* add_u) { - u3_xmas* sam_u = imp_u->sam_u; + u3_mesa* sam_u = imp_u->sam_u; c3_y imp_y = imp_u->imp_y; c3_w pip_w = ntohl(add_u->sin_addr.s_addr); @@ -1122,7 +1101,7 @@ _xmas_czar_here(u3_czar_info* imp_u, time_t now_t, struct sockaddr_in* add_u) u3_noun nam = u3dc("scot", c3__if, u3i_word(pip_w)); c3_c* nam_c = u3r_string(nam); - u3l_log("xmas: czar %s: ip %s", imp_u->dns_c, nam_c); + u3l_log("mesa: czar %s: ip %s", imp_u->dns_c, nam_c); c3_free(nam_c); u3z(nam); @@ -1138,33 +1117,33 @@ _xmas_czar_here(u3_czar_info* imp_u, time_t now_t, struct sockaddr_in* add_u) c3_y* buf_y = c3_calloc(len_w); u3r_bytes(0, len_w, buf_y, pac); u3_lane lan_u = (u3_lane){pip_w, _ames_czar_port(imp_y)}; - _xmas_send_buf(sam_u, lan_u, buf_y, len_w); + _mesa_send_buf(sam_u, lan_u, buf_y, len_w); } u3z(imp_u->pen); imp_u->pen = u3_nul; } static void -_xmas_czar_gone(u3_xmas* sam_u, c3_i sas_i, c3_y imp_y, time_t now_t) +_mesa_czar_gone(u3_mesa* sam_u, c3_i sas_i, c3_y imp_y, time_t now_t) { u3_czar_info* imp_u = &sam_u->imp_u[imp_y]; imp_u->tim_t = now_t; - u3l_log("xmas: %s", uv_strerror(sas_i)); + u3l_log("mesa: %s", uv_strerror(sas_i)); } static void -_xmas_czar_cb(uv_getaddrinfo_t* adr_u, c3_i sas_i, struct addrinfo* aif_u) +_mesa_czar_cb(uv_getaddrinfo_t* adr_u, c3_i sas_i, struct addrinfo* aif_u) { u3_czar_info* imp_u = (u3_czar_info*)adr_u->data; c3_y imp_y = imp_u->imp_y; - u3_xmas* sam_u = imp_u->sam_u; + u3_mesa* sam_u = imp_u->sam_u; time_t now_t = time(0); if ( 0 == sas_i ) { // XX: lifetimes for addrinfo, ames does something funny - _xmas_czar_here(imp_u, now_t, (struct sockaddr_in*)aif_u->ai_addr); + _mesa_czar_here(imp_u, now_t, (struct sockaddr_in*)aif_u->ai_addr); } else { - _xmas_czar_gone(sam_u, sas_i, imp_y, now_t); + _mesa_czar_gone(sam_u, sas_i, imp_y, now_t); } c3_free(adr_u); @@ -1172,11 +1151,10 @@ _xmas_czar_cb(uv_getaddrinfo_t* adr_u, c3_i sas_i, struct addrinfo* aif_u) } - - static void -_xmas_resolve_czar(u3_xmas* sam_u, c3_y imp_y, u3_noun pac) +_mesa_resolve_czar(u3_mesa* sam_u, c3_y imp_y, u3_noun pac) { + u3m_p("imp", imp_y); u3_assert( c3y == u3_Host.ops_u.net ); u3_czar_info* imp_u = &sam_u->imp_u[imp_y]; time_t now_t = time(0); @@ -1195,10 +1173,10 @@ _xmas_resolve_czar(u3_xmas* sam_u, c3_y imp_y, u3_noun pac) } if ( !sam_u->dns_c ) { - u3l_log("xmas: no galaxy domain"); + u3l_log("mesa: no galaxy domain"); return; } - imp_u->dns_c = _xmas_czar_dns(imp_y, sam_u->dns_c); + imp_u->dns_c = _mesa_czar_dns(imp_y, sam_u->dns_c); { uv_getaddrinfo_t* adr_u = c3_calloc(sizeof(*adr_u)); c3_i sas_i; @@ -1206,9 +1184,9 @@ _xmas_resolve_czar(u3_xmas* sam_u, c3_y imp_y, u3_noun pac) struct addrinfo hints; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_INET; - if ( 0 != (sas_i = uv_getaddrinfo(u3L, adr_u, _xmas_czar_cb, imp_u->dns_c, 0, &hints))) { + if ( 0 != (sas_i = uv_getaddrinfo(u3L, adr_u, _mesa_czar_cb, imp_u->dns_c, 0, &hints))) { time_t now_t = time(0); - _xmas_czar_gone(sam_u, sas_i, imp_y, now_t); + _mesa_czar_gone(sam_u, sas_i, imp_y, now_t); return; } } @@ -1217,13 +1195,13 @@ _xmas_resolve_czar(u3_xmas* sam_u, c3_y imp_y, u3_noun pac) } static u3_noun -_xmas_queue_czar(u3_xmas* sam_u, u3_noun las, u3_noun pac) +_mesa_queue_czar(u3_mesa* sam_u, u3_noun las, u3_noun pac) { las = u3do("flop", las); u3_noun lan, t = las; u3_noun res = u3_nul; time_t now_t = time(0); - + u3m_p("_mesa_queue_czar", las); while ( t != u3_nul ) { u3x_cell(t, &lan, &t); if ( (c3y == u3a_is_cat(lan) && lan < 256 ) ) { @@ -1231,38 +1209,37 @@ _xmas_queue_czar(u3_xmas* sam_u, u3_noun las, u3_noun pac) if ( 0 != imp_u->pip_w ) { res = u3nc(u3nt(c3__if, u3i_word(imp_u->pip_w), _ames_czar_port(lan)), res); - } + } if ( c3y == u3_Host.ops_u.net ) { - _xmas_resolve_czar(sam_u, lan, u3k(pac)); + _mesa_resolve_czar(sam_u, lan, u3k(pac)); } } else { res = u3nc(u3k(lan), res); } } + u3m_p("_mesa_queue_czar res", res); u3z(las); u3z(pac); return res; } - static void -_xmas_ef_send(u3_xmas* sam_u, u3_noun las, u3_noun pac) +_mesa_ef_send(u3_mesa* sam_u, u3_noun las, u3_noun pac) { - las = _xmas_queue_czar(sam_u, las, u3k(pac)); + las = _mesa_queue_czar(sam_u, las, u3k(pac)); c3_w len_w = u3r_met(3, pac); c3_y* buf_y = c3_calloc(len_w); u3r_bytes(0, len_w, buf_y, pac); sam_u->tim_d = _get_now_micros(); c3_o suc_o = c3n; - _xmas_rout_bufs(sam_u, buf_y, len_w, las); - + _mesa_rout_bufs(sam_u, buf_y, len_w, las); + c3_free(buf_y); u3z(pac); } - -static c3_o _xmas_kick(u3_xmas* sam_u, u3_noun tag, u3_noun dat) +static c3_o _mesa_kick(u3_mesa* sam_u, u3_noun tag, u3_noun dat) { c3_o ret_o; @@ -1273,9 +1250,11 @@ static c3_o _xmas_kick(u3_xmas* sam_u, u3_noun tag, u3_noun dat) case c3__send: { u3_noun las, pac; if ( c3n == u3r_cell(dat, &las, &pac) ) { + u3l_log(" vere: send no"); ret_o = c3n; } else { - _xmas_ef_send(sam_u, u3k(las), u3k(pac)); + u3l_log(" vere: send yes"); + _mesa_ef_send(sam_u, u3k(las), u3k(pac)); ret_o = c3y; } } break; @@ -1284,12 +1263,12 @@ static c3_o _xmas_kick(u3_xmas* sam_u, u3_noun tag, u3_noun dat) // technically losing tag is unncessary as it always should // be a direct atom, but better to be strict u3z(dat); u3z(tag); - return ret_o; + return ret_o; } -static c3_o _xmas_io_kick(u3_auto* car_u, u3_noun wir, u3_noun cad) +static c3_o _mesa_io_kick(u3_auto* car_u, u3_noun wir, u3_noun cad) { - u3_xmas* sam_u = (u3_xmas*)car_u; + u3_mesa* sam_u = (u3_mesa*)car_u; u3_noun tag, dat; c3_o ret_o; @@ -1298,31 +1277,29 @@ static c3_o _xmas_io_kick(u3_auto* car_u, u3_noun wir, u3_noun cad) { ret_o = c3n; } else { - ret_o = _xmas_kick(sam_u, u3k(tag), u3k(dat)); + ret_o = _mesa_kick(sam_u, u3k(tag), u3k(dat)); } u3z(wir); u3z(cad); return ret_o; } - - static u3_noun -_xmas_io_info(u3_auto* car_u) +_mesa_io_info(u3_auto* car_u) { return u3_nul; } static void -_xmas_io_slog(u3_auto* car_u) { - u3l_log("xmas is online"); +_mesa_io_slog(u3_auto* car_u) { + u3l_log("mesa is online"); } static void -_xmas_exit_cb(uv_handle_t* had_u) +_mesa_exit_cb(uv_handle_t* had_u) { - u3_xmas* sam_u = had_u->data; + u3_mesa* sam_u = had_u->data; u3s_cue_xeno_done(sam_u->sil_u); ur_cue_test_done(sam_u->tes_u); @@ -1332,16 +1309,15 @@ _xmas_exit_cb(uv_handle_t* had_u) c3_free(sam_u); } -static void -_xmas_io_exit(u3_auto* car_u) +static void +_mesa_io_exit(u3_auto* car_u) { - u3_xmas* sam_u = (u3_xmas*)car_u; - uv_close(&sam_u->had_u, _xmas_exit_cb); + u3_mesa* sam_u = (u3_mesa*)car_u; + uv_close(&sam_u->had_u, _mesa_exit_cb); } - static void -_init_peer(u3_xmas* sam_u, u3_peer* per_u) +_init_peer(u3_mesa* sam_u, u3_peer* per_u) { per_u->sam_u = sam_u; @@ -1352,9 +1328,8 @@ _init_peer(u3_xmas* sam_u, u3_peer* per_u) } // TODO: all the her_p hashtable functions are not refcounted properly - static u3_peer* -_xmas_get_peer_raw(u3_xmas* sam_u, u3_noun her) +_mesa_get_peer_raw(u3_mesa* sam_u, u3_noun her) { u3_peer* ret_u = NULL; u3_weak res = u3h_git(sam_u->her_p, her); @@ -1367,23 +1342,22 @@ _xmas_get_peer_raw(u3_xmas* sam_u, u3_noun her) return ret_u; } + /* * RETAIN */ static u3_peer* -_xmas_get_peer(u3_xmas* sam_u, c3_d her_d[2]) +_mesa_get_peer(u3_mesa* sam_u, c3_d her_d[2]) { - return _xmas_get_peer_raw(sam_u, u3i_chubs(2, her_d)); + return _mesa_get_peer_raw(sam_u, u3i_chubs(2, her_d)); } - - /* */ static void -_xmas_put_peer_raw(u3_xmas* sam_u, u3_noun her, u3_peer* per_u) +_mesa_put_peer_raw(u3_mesa* sam_u, u3_noun her, u3_peer* per_u) { - u3_peer* old_u = _xmas_get_peer_raw(sam_u, u3k(her)); + u3_peer* old_u = _mesa_get_peer_raw(sam_u, u3k(her)); u3_peer* new_u = NULL; if ( old_u == NULL ) { @@ -1400,20 +1374,19 @@ _xmas_put_peer_raw(u3_xmas* sam_u, u3_noun her, u3_peer* per_u) } static void -_xmas_put_peer(u3_xmas* sam_u, c3_d her_d[2], u3_peer* per_u) +_mesa_put_peer(u3_mesa* sam_u, c3_d her_d[2], u3_peer* per_u) { - _xmas_put_peer_raw(sam_u, u3i_chubs(2, her_d), per_u); + _mesa_put_peer_raw(sam_u, u3i_chubs(2, her_d), per_u); } - static u3_lane -_xmas_get_direct_lane_raw(u3_xmas* sam_u, u3_noun her) +_mesa_get_direct_lane_raw(u3_mesa* sam_u, u3_noun her) { if ( c3y == u3a_is_cat(her) && her < 256 ) { c3_s por_s = _ames_czar_port(her); return (u3_lane){sam_u->imp_u[her].pip_w, por_s}; } - u3_peer* per_u = _xmas_get_peer_raw(sam_u, her); + u3_peer* per_u = _mesa_get_peer_raw(sam_u, her); if ( NULL == per_u ) { return (u3_lane){0,0}; } @@ -1421,42 +1394,40 @@ _xmas_get_direct_lane_raw(u3_xmas* sam_u, u3_noun her) } static u3_lane -_xmas_get_direct_lane(u3_xmas* sam_u, c3_d her_d[2]) +_mesa_get_direct_lane(u3_mesa* sam_u, c3_d her_d[2]) { - return _xmas_get_direct_lane_raw(sam_u, u3i_chubs(2, her_d)); + return _mesa_get_direct_lane_raw(sam_u, u3i_chubs(2, her_d)); } - static u3_lane -_xmas_get_indirect_lane(u3_xmas* sam_u, u3_noun her, u3_noun lan) +_mesa_get_indirect_lane(u3_mesa* sam_u, u3_noun her, u3_noun lan) { if ( c3y == u3a_is_cat(her) && her < 256 ) { c3_s por_s = _ames_czar_port(her); return (u3_lane){sam_u->imp_u[her].pip_w, por_s}; } - u3_peer* per_u = _xmas_get_peer_raw(sam_u, her); + u3_peer* per_u = _mesa_get_peer_raw(sam_u, her); if ( NULL == per_u ) { return (u3_lane){0,0}; } return per_u->ind_u; } - /* * RETAIN */ static c3_o -_xmas_add_galaxy_pend(u3_xmas* sam_u, u3_noun her, u3_noun pen) +_mesa_add_galaxy_pend(u3_mesa* sam_u, u3_noun her, u3_noun pen) { u3_weak old = u3h_get(sam_u->her_p, her); u3_noun pes = u3_nul; u3_noun wat; if ( u3_none != old ) { - if ( u3h(old) == XMAS_CZAR ) { + if ( u3h(old) == MESA_CZAR ) { u3x_cell(u3t(old), &pes, &wat); u3z(old); - } else { - u3l_log("xmas: attempted to resolve resolved czar"); + } else { + u3l_log("mesa: attempted to resolve resolved czar"); u3z(old); return c3n; } @@ -1468,14 +1439,14 @@ _xmas_add_galaxy_pend(u3_xmas* sam_u, u3_noun her, u3_noun pen) } static u3_noun -_name_to_scry(u3_xmas_name* nam_u) +_name_to_scry(u3_mesa_name* nam_u) { u3_noun rif = _dire_etch_ud(nam_u->rif_w); u3_noun boq = _dire_etch_ud(nam_u->boq_y); u3_noun fag = _dire_etch_ud(nam_u->fra_w); - u3_noun pax = _xmas_encode_path(nam_u->pat_s, (c3_y*)nam_u->pat_c); + u3_noun pax = _mesa_encode_path(nam_u->pat_s, (c3_y*)nam_u->pat_c); - u3_noun wer = nam_u->nit_o == c3y + u3_noun wer = nam_u->nit_o == c3y ? u3nc(c3__init, pax) : u3nt(nam_u->aut_o == c3y ? c3__auth : c3__data, fag, pax); @@ -1488,20 +1459,20 @@ _name_to_scry(u3_xmas_name* nam_u) * RETAIN */ static u3_weak -_xmas_get_cache(u3_xmas* sam_u, u3_xmas_name* nam_u) +_mesa_get_cache(u3_mesa* sam_u, u3_mesa_name* nam_u) { u3_noun pax = _name_to_scry(nam_u); u3_weak res = u3h_get(sam_u->pac_p, pax); if ( u3_none == res ) { //u3m_p("miss", u3k(pax)); - } else { + } else { //u3m_p("hit", u3nc(u3k(pax), u3k(res))); } return res; } static void -_xmas_put_cache(u3_xmas* sam_u, u3_xmas_name* nam_u, u3_noun val) +_mesa_put_cache(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_noun val) { u3_noun pax = _name_to_scry(nam_u); @@ -1510,14 +1481,14 @@ _xmas_put_cache(u3_xmas* sam_u, u3_xmas_name* nam_u, u3_noun val) } static void -_xmas_try_forward(u3_xmas_pict* pic_u, u3_noun fra, u3_noun hit) +_mesa_try_forward(u3_mesa_pict* pic_u, u3_noun fra, u3_noun hit) { u3l_log(""); u3l_log("stubbed forwarding"); } static c3_w -_xmas_respond(u3_xmas_pict* req_u, c3_y** buf_y, u3_noun hit) +_mesa_respond(u3_mesa_pict* req_u, c3_y** buf_y, u3_noun hit) { c3_w len_w = u3r_met(3, hit); @@ -1529,15 +1500,15 @@ _xmas_respond(u3_xmas_pict* req_u, c3_y** buf_y, u3_noun hit) return len_w; } -/* +/* */ static void -_xmas_page_scry_cb(void* vod_p, u3_noun nun) +_mesa_page_scry_cb(void* vod_p, u3_noun nun) { - u3_xmas_pict* pic_u = vod_p; - u3_xmas_pact* pac_u = &pic_u->pac_u; - u3_xmas* sam_u = pic_u->sam_u; - //u3_noun pax = _xmas_path_with_fra(pac_u->pek_u.nam_u.pat_c, &fra_s); + u3_mesa_pict* pic_u = vod_p; + u3_mesa_pact* pac_u = &pic_u->pac_u; + u3_mesa* sam_u = pic_u->sam_u; + //u3_noun pax = _mesa_path_with_fra(pac_u->pek_u.nam_u.pat_c, &fra_s); u3_weak hit = u3r_at(7, nun); if ( u3_none == hit ) { @@ -1546,21 +1517,21 @@ _xmas_page_scry_cb(void* vod_p, u3_noun nun) u3l_log("unbound"); } else { - u3_weak old = _xmas_get_cache(sam_u, &pac_u->pag_u.nam_u); + u3_weak old = _mesa_get_cache(sam_u, &pac_u->pag_u.nam_u); if ( old == u3_none ) { u3l_log("bad"); - XMAS_LOG(APATHY); + MESA_LOG(APATHY); } else { u3_noun tag; u3_noun dat; u3x_cell(u3k(old), &tag, &dat); - if ( XMAS_WAIT == tag ) { + if ( MESA_WAIT == tag ) { c3_y* buf_y; - - c3_w len_w = _xmas_respond(pic_u, &buf_y, u3k(hit)); - _xmas_rout_bufs(sam_u, buf_y, len_w, u3k(u3t(dat))); + + c3_w len_w = _mesa_respond(pic_u, &buf_y, u3k(hit)); + _mesa_rout_bufs(sam_u, buf_y, len_w, u3k(u3t(dat))); } - _xmas_put_cache(sam_u, &pac_u->pek_u.nam_u, u3nc(XMAS_ITEM, u3k(hit))); + _mesa_put_cache(sam_u, &pac_u->pek_u.nam_u, u3nc(MESA_ITEM, u3k(hit))); // u3z(old); } // u3z(hit); @@ -1569,9 +1540,9 @@ _xmas_page_scry_cb(void* vod_p, u3_noun nun) } static void -_xmas_hear_bail(u3_ovum* egg_u, u3_noun lud) +_mesa_hear_bail(u3_ovum* egg_u, u3_noun lud) { - u3l_log("xmas: hear bail"); + u3l_log("mesa: hear bail"); c3_w len_w = u3qb_lent(lud); u3l_log("len_w: %i", len_w); if( len_w == 2 ) { @@ -1581,7 +1552,6 @@ _xmas_hear_bail(u3_ovum* egg_u, u3_noun lud) u3_ovum_free(egg_u); } - static void _saxo_cb(void* vod_p, u3_noun nun) { @@ -1590,23 +1560,23 @@ _saxo_cb(void* vod_p, u3_noun nun) if ( sax != u3_none ) { u3_noun her = u3do("head", u3k(sax)); - u3_peer* new_u = _xmas_get_peer_raw(per_u->sam_u, u3k(her)); + u3_peer* new_u = _mesa_get_peer_raw(per_u->sam_u, u3k(her)); if ( new_u != NULL ) { per_u = new_u; } - u3_xmas* sam_u = per_u->sam_u; + u3_mesa* sam_u = per_u->sam_u; u3_noun gal = u3do("rear", u3k(sax)); u3_assert( c3y == u3a_is_cat(gal) && gal < 256 ); // both atoms guaranteed to be cats, bc we don't call unless forwarding per_u->imp_s = gal; - _xmas_put_peer_raw(per_u->sam_u, her, per_u); + _mesa_put_peer_raw(per_u->sam_u, her, per_u); } u3z(nun); } static void -_meet_peer(u3_xmas* sam_u, u3_peer* per_u, c3_d her_d[2]) +_meet_peer(u3_mesa* sam_u, u3_peer* per_u, c3_d her_d[2]) { c3_d now_d = _get_now_micros(); per_u->las_u.son_d = now_d; @@ -1618,7 +1588,7 @@ _meet_peer(u3_xmas* sam_u, u3_peer* per_u, c3_d her_d[2]) } static void -_hear_peer(u3_xmas* sam_u, u3_peer* per_u, u3_lane lan_u, c3_o dir_o) +_hear_peer(u3_mesa* sam_u, u3_peer* per_u, u3_lane lan_u, c3_o dir_o) { c3_d now_d = _get_now_micros(); per_u->las_u.acc_d = now_d; @@ -1630,11 +1600,11 @@ _hear_peer(u3_xmas* sam_u, u3_peer* per_u, u3_lane lan_u, c3_o dir_o) } static void -_xmas_forward(u3_xmas_pict* pic_u, u3_lane lan_u) +_mesa_forward(u3_mesa_pict* pic_u, u3_lane lan_u) { u3l_log("forwarding"); - u3_xmas_pact* pac_u = &pic_u->pac_u; - u3_xmas* sam_u = pic_u->sam_u; + u3_mesa_pact* pac_u = &pic_u->pac_u; + u3_mesa* sam_u = pic_u->sam_u; c3_d her_d[2]; _get_her(pac_u, her_d); // XX: revive @@ -1642,43 +1612,43 @@ _xmas_forward(u3_xmas_pict* pic_u, u3_lane lan_u) if ( pac_u->hed_u.typ_y == PACT_PAGE ) { //u3l_log("should update next hop"); - u3_weak hit = _xmas_get_cache(sam_u, &pac_u->pag_u.nam_u); + u3_weak hit = _mesa_get_cache(sam_u, &pac_u->pag_u.nam_u); if ( u3_none == hit ) { - XMAS_LOG(APATHY); + MESA_LOG(APATHY); return; } u3_noun tag, dat; u3x_cell(u3k(hit), &tag, &dat); - if ( tag == XMAS_WAIT ) { + if ( tag == MESA_WAIT ) { c3_y* buf_y = c3_calloc(PACT_SIZE); - c3_w len_w = xmas_etch_pact(buf_y, pac_u); - _xmas_rout_bufs(sam_u, buf_y, len_w, u3k(u3t(dat))); + c3_w len_w = mesa_etch_pact(buf_y, pac_u); + _mesa_rout_bufs(sam_u, buf_y, len_w, u3k(u3t(dat))); } else { - u3l_log("xmas: weird pending interest"); + u3l_log("mesa: weird pending interest"); } } else { - u3_peer* per_u = _xmas_get_peer(sam_u, her_d); + u3_peer* per_u = _mesa_get_peer(sam_u, her_d); if ( NULL != per_u ) { - u3_lane lin_u = _xmas_get_direct_lane(sam_u, her_d); + u3_lane lin_u = _mesa_get_direct_lane(sam_u, her_d); if ( lin_u.pip_w != 0 ) { - _xmas_send(pic_u, &lin_u); + _mesa_send(pic_u, &lin_u); } } } - xmas_free_pact(pac_u); + mesa_free_pact(pac_u); } static u3_pend_req* -_xmas_req_pact_init(u3_xmas* sam_u, u3_xmas_pict* pic_u, u3_lane* lan_u) +_mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) { - u3_xmas_pact* pac_u = &pic_u->pac_u; - u3_xmas_name* nam_u = &pac_u->pag_u.nam_u; - u3_xmas_data* dat_u = &pac_u->pag_u.dat_u; + u3_mesa_pact* pac_u = &pic_u->pac_u; + u3_mesa_name* nam_u = &pac_u->pag_u.nam_u; + u3_mesa_data* dat_u = &pac_u->pag_u.dat_u; c3_o lin_o = dat_u->tot_w <= 4 ? c3y : c3n; - u3_pend_req* req_u = _xmas_get_request(sam_u, nam_u); + u3_pend_req* req_u = _mesa_get_request(sam_u, nam_u); if ( NULL != req_u ) { - // duplicate + // duplicate if ( req_u->tot_w <= 4 ) { return NULL; } @@ -1687,22 +1657,22 @@ _xmas_req_pact_init(u3_xmas* sam_u, u3_xmas_pict* pic_u, u3_lane* lan_u) memset(req_u, 0, sizeof(u3_pend_req)); } - u3_gage* gag_u = _xmas_get_lane(sam_u, nam_u->her_d, lan_u); - + u3_gage* gag_u = _mesa_get_lane(sam_u, nam_u->her_d, lan_u); + if ( gag_u == NULL ) { gag_u = alloca(sizeof(u3_gage)); _init_gage(gag_u); // save and re-retrieve so we have persistent pointer - _xmas_put_lane(sam_u, nam_u->her_d, lan_u, gag_u); - gag_u = _xmas_get_lane(sam_u, nam_u->her_d, lan_u); + _mesa_put_lane(sam_u, nam_u->her_d, lan_u, gag_u); + gag_u = _mesa_get_lane(sam_u, nam_u->her_d, lan_u); u3_assert( gag_u != NULL ); } - req_u->pic_u = c3_calloc(sizeof(u3_xmas_pict)); + req_u->pic_u = c3_calloc(sizeof(u3_mesa_pict)); req_u->pic_u->sam_u = sam_u; req_u->pic_u->pac_u.hed_u.typ_y = PACT_PEEK; - req_u->pic_u->pac_u.hed_u.pro_y = XMAS_VER; - memcpy(&req_u->pic_u->pac_u.pek_u.nam_u, nam_u, sizeof(u3_xmas_name)); + req_u->pic_u->pac_u.hed_u.pro_y = MESA_VER; + memcpy(&req_u->pic_u->pac_u.pek_u.nam_u, nam_u, sizeof(u3_mesa_name)); req_u->pic_u->pac_u.pek_u.nam_u.aut_o = c3n; req_u->pic_u->pac_u.pek_u.nam_u.nit_o = c3n; @@ -1715,7 +1685,7 @@ _xmas_req_pact_init(u3_xmas* sam_u, u3_xmas_pict* pic_u, u3_lane* lan_u) bitset_init(&req_u->was_u, dat_u->tot_w); // TODO: handle restart - // u3_assert( nam_u->fra_w == 0 ); + // u3_assert( nam_u->fra_w == 0 ); req_u->nex_w = (c3y == lin_o) ? 1 : 0; req_u->len_w = (c3y == lin_o) ? 1 : 0; @@ -1754,25 +1724,22 @@ _xmas_req_pact_init(u3_xmas* sam_u, u3_xmas_pict* pic_u, u3_lane* lan_u) } vec_init(&req_u->mis_u, 8); - req_u = _xmas_put_request(sam_u, nam_u, req_u); + req_u = _mesa_put_request(sam_u, nam_u, req_u); return req_u; } - - - static void -_xmas_hear_page(u3_xmas_pict* pic_u, u3_lane lan_u) +_mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) { -#ifdef XMAS_DEBUG - // u3l_log("xmas hear page %u", pic_u->pac_u.pag_u.nam_u.fra_w); +#ifdef MESA_DEBUG + // u3l_log("mesa hear page %u", pic_u->pac_u.pag_u.nam_u.fra_w); #endif - u3_xmas* sam_u = pic_u->sam_u; - u3_xmas_pact* pac_u = &pic_u->pac_u; - u3_noun wir = u3nc(c3__xmas, u3_nul); + u3_mesa* sam_u = pic_u->sam_u; + u3_mesa_pact* pac_u = &pic_u->pac_u; + u3_noun wir = u3nc(c3__mesa, u3_nul); c3_s fra_s; - u3_peer* per_u = _xmas_get_peer(sam_u, pac_u->pag_u.nam_u.her_d); + u3_peer* per_u = _mesa_get_peer(sam_u, pac_u->pag_u.nam_u.her_d); c3_o new_o = c3n; if ( NULL == per_u ) { new_o = c3y; @@ -1792,18 +1759,18 @@ _xmas_hear_page(u3_xmas_pict* pic_u, u3_lane lan_u) //_log_lane(&lan_u); } - _xmas_put_peer(sam_u, pac_u->pag_u.nam_u.her_d, per_u); + _mesa_put_peer(sam_u, pac_u->pag_u.nam_u.her_d, per_u); - u3_weak hit = _xmas_get_cache(sam_u, &pac_u->pag_u.nam_u); + u3_weak hit = _mesa_get_cache(sam_u, &pac_u->pag_u.nam_u); if ( u3_none != hit ) { - _xmas_forward(pic_u, lan_u); - _xmas_free_pict(pic_u); + _mesa_forward(pic_u, lan_u); + _mesa_free_pict(pic_u); u3z(hit); return; } - + // u3_noun fra = u3i_bytes(pac_u->pag_u.dat_u.len_w, pac_u->pag_u.dat_u.fra_y) ; /*if ( dop_o == c3n && pac_u->pag_u.nam_u.fra_w == 150) { dop_o = c3y; @@ -1811,31 +1778,31 @@ _xmas_hear_page(u3_xmas_pict* pic_u, u3_lane lan_u) return; }*/ u3_pend_req* req_u; - + if ( pac_u->pek_u.nam_u.nit_o == c3y ) { - req_u = _xmas_req_pact_init(sam_u, pic_u, &lan_u); + req_u = _mesa_req_pact_init(sam_u, pic_u, &lan_u); if ( req_u == NULL ) { - _xmas_free_pict(pic_u); + _mesa_free_pict(pic_u); return; } } else { - req_u = _xmas_req_pact_done(sam_u, &pac_u->pag_u.nam_u, &pac_u->pag_u.dat_u, &lan_u); + req_u = _mesa_req_pact_done(sam_u, &pac_u->pag_u.nam_u, &pac_u->pag_u.dat_u, &lan_u); if ( req_u == NULL ) { // cleanup - _xmas_free_pict(pic_u); + _mesa_free_pict(pic_u); return; - } + } } if ( req_u == NULL ) { u3_assert(!"invalid"); return; } - c3_w win_w = _xmas_req_get_cwnd(req_u); - u3_xmas_pict* nex_u = req_u->pic_u; + c3_w win_w = _mesa_req_get_cwnd(req_u); + u3_mesa_pict* nex_u = req_u->pic_u; c3_w nex_w = req_u->nex_w; if ( win_w != 0 ) { -#ifdef XMAS_DEBUG +#ifdef MESA_DEBUG u3l_log("continuing flow nex: %u, win: %u", nex_w, win_w); u3l_log("in flight %u", bitset_wyt(&req_u->was_u)); #endif @@ -1846,14 +1813,14 @@ _xmas_hear_page(u3_xmas_pict* pic_u, u3_lane lan_u) break; } nex_u->pac_u.pek_u.nam_u.fra_w = nex_w + i; - c3_w siz_w = xmas_etch_pact(buf_y, &nex_u->pac_u); + c3_w siz_w = mesa_etch_pact(buf_y, &nex_u->pac_u); if ( siz_w == 0 ) { u3l_log("failed to etch"); u3_assert( 0 ); } // TODO: better route management - _xmas_send_buf(sam_u, lan_u, buf_y, siz_w); - _xmas_req_pact_sent(req_u, &nex_u->pac_u.pek_u.nam_u); + _mesa_send_buf(sam_u, lan_u, buf_y, siz_w); + _mesa_req_pact_sent(req_u, &nex_u->pac_u.pek_u.nam_u); } } if ( req_u->len_w == req_u->tot_w ) { @@ -1863,33 +1830,31 @@ _xmas_hear_page(u3_xmas_pict* pic_u, u3_lane lan_u) u3l_log("%u kilobytes took %f ms", req_u->tot_w, (now_d - sam_u->tim_d)/1000.0); c3_w siz_w = (1 << (pac_u->pag_u.nam_u.boq_y - 3)); //u3_noun dat = u3i_bytes((siz_w * req_u->tot_w), req_u->dat_y); - _xmas_del_request(sam_u, &pac_u->pag_u.nam_u); + _mesa_del_request(sam_u, &pac_u->pag_u.nam_u); } - _xmas_free_pict(pic_u); + _mesa_free_pict(pic_u); } static void -_xmas_add_lane_to_cache(u3_xmas* sam_u, u3_xmas_name* nam_u, u3_noun las, u3_lane lan_u) +_mesa_add_lane_to_cache(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_noun las, u3_lane lan_u) { - u3_noun hit = u3nq(XMAS_WAIT, - _xmas_get_now(), - u3_xmas_encode_lane(lan_u), + u3_noun hit = u3nq(MESA_WAIT, + _mesa_get_now(), + u3_mesa_encode_lane(lan_u), u3k(las)); - _xmas_put_cache(sam_u, nam_u, hit); + _mesa_put_cache(sam_u, nam_u, hit); u3z(las); } - - static void -_xmas_hear_peek(u3_xmas_pict* pic_u, u3_lane lan_u) +_mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) { -#ifdef XMAS_DEBUG - u3l_log("xmas: hear peek"); +#ifdef MESA_DEBUG + u3l_log("mesa: hear peek"); // u3_assert(pac_u->hed_u.typ_y == PACT_PEEK); #endif - u3_xmas_pact* pac_u = &pic_u->pac_u; - u3_xmas* sam_u = pic_u->sam_u; + u3_mesa_pact* pac_u = &pic_u->pac_u; + u3_mesa* sam_u = pic_u->sam_u; c3_d her_d[2]; her_d[0] = 0; her_d[1] = 0; @@ -1898,65 +1863,65 @@ _xmas_hear_peek(u3_xmas_pict* pic_u, u3_lane lan_u) if ( c3n == our_o ) { - u3_peer* per_u = _xmas_get_peer(sam_u, her_d); + u3_peer* per_u = _mesa_get_peer(sam_u, her_d); if ( per_u == NULL ) { - u3l_log("xmas: alien forward for %s", u3r_string(u3dc("scot", c3__p, u3i_chubs(2, her_d)))); - _xmas_free_pict(pic_u); + u3l_log("mesa: alien forward for %s", u3r_string(u3dc("scot", c3__p, u3i_chubs(2, her_d)))); + _mesa_free_pict(pic_u); return; } if ( c3y == sam_u->for_o && sam_u->pir_u->who_d[0] == per_u->imp_s ) { -//#ifdef XMAS_DEBUG - u3_lane lin_u = _xmas_get_direct_lane(sam_u, her_d); +//#ifdef MESA_DEBUG + u3_lane lin_u = _mesa_get_direct_lane(sam_u, her_d); //#endif //_update_hopcount(&pac_u->hed_u); u3l_log("sending peek %u", pac_u->pek_u.nam_u.fra_w); - _xmas_send(pic_u, &lin_u); + _mesa_send(pic_u, &lin_u); } - _xmas_free_pict(pic_u); + _mesa_free_pict(pic_u); return; } - u3_weak hit = _xmas_get_cache(sam_u, &pac_u->pek_u.nam_u); + u3_weak hit = _mesa_get_cache(sam_u, &pac_u->pek_u.nam_u); if ( u3_none != hit ) { u3_noun tag, dat; u3x_cell(u3k(hit), &tag, &dat); - if ( tag == XMAS_WAIT ) { - _xmas_add_lane_to_cache(sam_u, &pac_u->pek_u.nam_u, u3k(u3t(dat)), lan_u); - } else if ( c3y == our_o && tag == XMAS_ITEM ) { + if ( tag == MESA_WAIT ) { + _mesa_add_lane_to_cache(sam_u, &pac_u->pek_u.nam_u, u3k(u3t(dat)), lan_u); + } else if ( c3y == our_o && tag == MESA_ITEM ) { c3_y* buf_y; - c3_w len_w = _xmas_respond(pic_u, &buf_y, u3k(dat)); - _xmas_send_buf(sam_u, lan_u, buf_y, len_w); + c3_w len_w = _mesa_respond(pic_u, &buf_y, u3k(dat)); + _mesa_send_buf(sam_u, lan_u, buf_y, len_w); } else { - u3l_log("xmas: weird case in cache, dropping"); + u3l_log("mesa: weird case in cache, dropping"); } - _xmas_free_pict(pic_u); + _mesa_free_pict(pic_u); u3z(hit); return; } else { - _xmas_add_lane_to_cache(sam_u, &pac_u->pek_u.nam_u, u3_nul, lan_u); // TODO: retrieve from namespace + _mesa_add_lane_to_cache(sam_u, &pac_u->pek_u.nam_u, u3_nul, lan_u); // TODO: retrieve from namespace if ( c3y == our_o ) { - u3_noun sky = _name_to_scry(&pac_u->pek_u.nam_u); + u3_noun sky = _name_to_scry(&pac_u->pek_u.nam_u); u3_noun our = u3i_chubs(2, sam_u->car_u.pir_u->who_d); u3_noun bem = u3nc(u3nt(our, u3_nul, u3nc(c3__ud, 1)), sky); // only branch where we do not free pic_u - u3_pier_peek(sam_u->car_u.pir_u, u3_nul, u3k(u3nq(1, c3__beam, c3__xx, bem)), pic_u, _xmas_page_scry_cb); + u3_pier_peek(sam_u->car_u.pir_u, u3_nul, u3k(u3nq(1, c3__beam, c3__xx, bem)), pic_u, _mesa_page_scry_cb); } else { - _xmas_free_pict(pic_u); + _mesa_free_pict(pic_u); } } // u3z(pax); } // xx: should inject event directly, but vane does not work -// so we just hack it to get +// so we just hack it to get static void -_xmas_hear_poke(u3_xmas_pict* pic_u, u3_lane* lan_u) +_mesa_hear_poke(u3_mesa_pict* pic_u, u3_lane* lan_u) { - u3_xmas_pact* pac_u = &pic_u->pac_u; - u3_xmas* sam_u = pic_u->sam_u; - u3_peer* per_u = _xmas_get_peer(sam_u, pac_u->pok_u.pay_u.her_d); + u3_mesa_pact* pac_u = &pic_u->pac_u; + u3_mesa* sam_u = pic_u->sam_u; + u3_peer* per_u = _mesa_get_peer(sam_u, pac_u->pok_u.pay_u.her_d); c3_o new_o = c3n; if ( NULL == per_u ) { new_o = c3y; @@ -1977,16 +1942,16 @@ _xmas_hear_poke(u3_xmas_pict* pic_u, u3_lane* lan_u) u3l_log("new lane is direct %c", c3y == dir_o ? 'y' : 'n'); _log_lane(lan_u); } - _xmas_put_peer(sam_u, pac_u->pok_u.pay_u.her_d, per_u); + _mesa_put_peer(sam_u, pac_u->pok_u.pay_u.her_d, per_u); } static void -_xmas_hear(u3_xmas* sam_u, +_mesa_hear(u3_mesa* sam_u, u3_lane* lan_u, c3_w len_w, c3_y* hun_y) { - u3_xmas_pict* pic_u; + u3_mesa_pict* pic_u; c3_w pre_w; c3_y* cur_y = hun_y; if ( HEAD_SIZE > len_w ) { @@ -1994,32 +1959,32 @@ _xmas_hear(u3_xmas* sam_u, return; } - pic_u = c3_calloc(sizeof(u3_xmas_pict)); + pic_u = c3_calloc(sizeof(u3_mesa_pict)); pic_u->sam_u = sam_u; - c3_w lin_w = xmas_sift_pact(&pic_u->pac_u, hun_y, len_w); + c3_w lin_w = mesa_sift_pact(&pic_u->pac_u, hun_y, len_w); c3_free(hun_y); if ( lin_w == 0 ) { - XMAS_LOG(SERIAL) + MESA_LOG(SERIAL) c3_free(hun_y); - xmas_free_pact(&pic_u->pac_u); + mesa_free_pact(&pic_u->pac_u); return; } switch ( pic_u->pac_u.hed_u.typ_y ) { case PACT_PEEK: { - _xmas_hear_peek(pic_u, *lan_u); + _mesa_hear_peek(pic_u, *lan_u); } break; case PACT_PAGE: { - _xmas_hear_page(pic_u, *lan_u); + _mesa_hear_page(pic_u, *lan_u); } break; default: { - _xmas_hear_poke(pic_u, lan_u); + _mesa_hear_poke(pic_u, lan_u); } break; } } static void -_xmas_recv_cb(uv_udp_t* wax_u, +_mesa_recv_cb(uv_udp_t* wax_u, ssize_t nrd_i, const uv_buf_t * buf_u, const struct sockaddr* adr_u, @@ -2027,7 +1992,7 @@ _xmas_recv_cb(uv_udp_t* wax_u, { if ( 0 > nrd_i ) { if ( u3C.wag_w & u3o_verbose ) { - u3l_log("xmas: recv: fail: %s", uv_strerror(nrd_i)); + u3l_log("mesa: recv: fail: %s", uv_strerror(nrd_i)); } c3_free(buf_u->base); } @@ -2036,12 +2001,12 @@ _xmas_recv_cb(uv_udp_t* wax_u, } else if ( flg_i & UV_UDP_PARTIAL ) { if ( u3C.wag_w & u3o_verbose ) { - u3l_log("xmas: recv: fail: message truncated"); + u3l_log("mesa: recv: fail: message truncated"); } c3_free(buf_u->base); } else { - u3_xmas* sam_u = wax_u->data; + u3_mesa* sam_u = wax_u->data; struct sockaddr_in* add_u = (struct sockaddr_in*)adr_u; u3_lane lan_u; @@ -2052,26 +2017,24 @@ _xmas_recv_cb(uv_udp_t* wax_u, // u3l_log("IP: %x", lan_u.pip_w); // NB: [nrd_i] will never exceed max length from _ames_alloc() // - _xmas_hear(sam_u, &lan_u, (c3_w)nrd_i, (c3_y*)buf_u->base); + _mesa_hear(sam_u, &lan_u, (c3_w)nrd_i, (c3_y*)buf_u->base); } } - - static void -_xmas_io_talk(u3_auto* car_u) +_mesa_io_talk(u3_auto* car_u) { - u3_xmas* sam_u = (u3_xmas*)car_u; + u3_mesa* sam_u = (u3_mesa*)car_u; sam_u->dns_c = "urbit.org"; // TODO: receive turf { // XX remove [sev_l] // - u3_noun wir = u3nt(c3__xmas, + u3_noun wir = u3nt(c3__mesa, u3dc("scot", c3__uv, sam_u->sev_l), u3_nul); u3_noun cad = u3nc(c3__born, u3_nul); - u3_auto_plan(car_u, u3_ovum_init(0, c3__x, wir, cad)); + u3_auto_plan(car_u, u3_ovum_init(0, c3__m, wir, cad)); } u3_noun who = u3i_chubs(2, sam_u->pir_u->who_d); u3_noun rac = u3do("clan:title", u3k(who)); @@ -2106,7 +2069,7 @@ _xmas_io_talk(u3_auto* car_u) if ( (ret_i = uv_udp_bind(&sam_u->wax_u, (const struct sockaddr*)&add_u, 0)) != 0 ) { - u3l_log("xmas: bind: %s", uv_strerror(ret_i)); + u3l_log("mesa: bind: %s", uv_strerror(ret_i)); /*if ( (c3__czar == rac) && (UV_EADDRINUSE == ret_i) ) @@ -2125,24 +2088,24 @@ _xmas_io_talk(u3_auto* car_u) sam_u->pir_u->por_s = ntohs(add_u.sin_port); } if ( c3y == u3_Host.ops_u.net ) { - u3l_log("xmas: live on %d", sam_u->pir_u->por_s); + u3l_log("mesa: live on %d", sam_u->pir_u->por_s); } else { - u3l_log("xmas: live on %d (localhost only)", sam_u->pir_u->por_s); + u3l_log("mesa: live on %d (localhost only)", sam_u->pir_u->por_s); } - uv_udp_recv_start(&sam_u->wax_u, _ames_alloc, _xmas_recv_cb); + uv_udp_recv_start(&sam_u->wax_u, _ames_alloc, _mesa_recv_cb); sam_u->car_u.liv_o = c3y; //u3z(rac); u3z(who); } -/* _xmas_io_init(): initialize ames I/O. +/* _mesa_io_init(): initialize ames I/O. */ u3_auto* -u3_xmas_io_init(u3_pier* pir_u) +u3_mesa_io_init(u3_pier* pir_u) { - u3_xmas* sam_u = c3_calloc(sizeof(*sam_u)); + u3_mesa* sam_u = c3_calloc(sizeof(*sam_u)); sam_u->pir_u = pir_u; sam_u->her_p = u3h_new_cache(100000); @@ -2179,7 +2142,7 @@ u3_xmas_io_init(u3_pier* pir_u) } if ( c3y == u3a_is_cat(her) && her < 256 ) { - u3l_log("xmas: forwarding enabled"); + u3l_log("mesa: forwarding enabled"); sam_u->for_o = c3y; } u3z(her); @@ -2187,13 +2150,13 @@ u3_xmas_io_init(u3_pier* pir_u) u3_auto* car_u = &sam_u->car_u; - car_u->nam_m = c3__xmas; + car_u->nam_m = c3__mesa; car_u->liv_o = c3y; - car_u->io.talk_f = _xmas_io_talk; - car_u->io.info_f = _xmas_io_info; - car_u->io.slog_f = _xmas_io_slog; - car_u->io.kick_f = _xmas_io_kick; - car_u->io.exit_f = _xmas_io_exit; + car_u->io.talk_f = _mesa_io_talk; + car_u->io.info_f = _mesa_io_info; + car_u->io.slog_f = _mesa_io_slog; + car_u->io.kick_f = _mesa_io_kick; + car_u->io.exit_f = _mesa_io_exit; diff --git a/pkg/vere/io/xmas/bitset.c b/pkg/vere/io/mesa/bitset.c similarity index 100% rename from pkg/vere/io/xmas/bitset.c rename to pkg/vere/io/mesa/bitset.c diff --git a/pkg/vere/io/xmas/bitset.h b/pkg/vere/io/mesa/bitset.h similarity index 100% rename from pkg/vere/io/xmas/bitset.h rename to pkg/vere/io/mesa/bitset.h diff --git a/pkg/vere/io/xmas/xmas.h b/pkg/vere/io/mesa/mesa.h similarity index 55% rename from pkg/vere/io/xmas/xmas.h rename to pkg/vere/io/mesa/mesa.h index 0132b1b0f6..fd4b8860c9 100644 --- a/pkg/vere/io/xmas/xmas.h +++ b/pkg/vere/io/mesa/mesa.h @@ -1,9 +1,9 @@ -#ifndef VERE_XMAS_H -#define VERE_XMAS_H +#ifndef VERE_MESA_H +#define VERE_MESA_H #include "c3.h" -#define XMAS_VER 1 +#define MESA_VER 1 #define FINE_PAGE 4096 // packets per page #define FINE_FRAG 1024 // bytes per fragment packet #define FINE_PATH_MAX 384 // longest allowed scry path @@ -15,39 +15,39 @@ #define HOP_LONG 0b10 #define HOP_MANY 0b11 -#define XMAS_COOKIE_LEN 4 -static c3_y XMAS_COOKIE[4] = { 0x5e, 0x1d, 0xad, 0x51 }; +#define MESA_COOKIE_LEN 4 +static c3_y MESA_COOKIE[4] = { 0x5e, 0x1d, 0xad, 0x51 }; -typedef enum _u3_xmas_ptag { +typedef enum _u3_mesa_ptag { PACT_RESV = 0, PACT_PAGE = 1, PACT_PEEK = 2, PACT_POKE = 3, -} u3_xmas_ptag; +} u3_mesa_ptag; -typedef enum _u3_xmas_rout_tag { +typedef enum _u3_mesa_rout_tag { ROUT_GALAXY = 0, ROUT_OTHER = 1 -} u3_xmas_rout_tag; +} u3_mesa_rout_tag; -typedef enum _u3_xmas_nexh { +typedef enum _u3_mesa_nexh { NEXH_NONE = 0, NEXH_SBYT = 1, NEXH_ONLY = 2, NEXH_MANY = 3 -} u3_xmas_nexh; +} u3_mesa_nexh; -typedef struct _u3_xmas_name_meta { +typedef struct _u3_mesa_name_meta { c3_y ran_y; // rank (2 bits) c3_y rif_y; // rift-len (2 bits) c3_y nit_y; // initial overlay (1 bit) c3_y tau_y; // %data (0) or %auth (1), 0 if !nit_o (1 bit) c3_y gaf_y; // fragment number length (2bit) -} u3_xmas_name_meta; +} u3_mesa_name_meta; -typedef struct _u3_xmas_name { - // u3_xmas_name_meta met_u; +typedef struct _u3_mesa_name { + // u3_mesa_name_meta met_u; c3_d her_d[2]; c3_w rif_w; c3_y boq_y; @@ -56,27 +56,27 @@ typedef struct _u3_xmas_name { c3_w fra_w; c3_s pat_s; c3_c* pat_c; -} u3_xmas_name; +} u3_mesa_name; -typedef struct _u3_xmas_data_meta { +typedef struct _u3_mesa_data_meta { c3_y bot_y; // total-fragments len (2 bits) c3_y aul_y; // auth-left (message) type (2 bits) c3_y aur_y; // auth-right (packet) type (2 bits) c3_y men_y; // fragment length/type (2 bits) -} u3_xmas_data_meta; +} u3_mesa_data_meta; typedef enum { AUTH_NONE = 0, AUTH_NEXT = 1, // %1, must be two hash AUTH_SIGN = 2, // %0, hashes are optional depending on num frag AUTH_HMAC = 3 -} u3_xmas_auth_type; +} u3_mesa_auth_type; -typedef struct _u3_xmas_data { - // u3_xmas_data_meta met_u; +typedef struct _u3_mesa_data { + // u3_mesa_data_meta met_u; c3_w tot_w; // total fragments struct { - u3_xmas_auth_type typ_e; // none, traversal (none), sig, or hmac + u3_mesa_auth_type typ_e; // none, traversal (none), sig, or hmac union { // c3_y sig_y[64]; // signature c3_y mac_y[32]; // hmac @@ -88,74 +88,74 @@ typedef struct _u3_xmas_data { } aup_u; c3_w len_w; // fragment length c3_y* fra_y; // fragment -} u3_xmas_data; +} u3_mesa_data; -typedef struct _u3_xmas_head { - u3_xmas_nexh nex_y; // next-hop +typedef struct _u3_mesa_head { + u3_mesa_nexh nex_y; // next-hop c3_y pro_y; // protocol version - u3_xmas_ptag typ_y; // packet type + u3_mesa_ptag typ_y; // packet type c3_y hop_y; // hopcount c3_w mug_w; // mug checksum -} u3_xmas_head; +} u3_mesa_head; -// +// // +$ cache // [%rout lanes=(list lanes)] // [%pending pacs=(list pact)] -typedef struct _u3_xmas_peek_pact { - u3_xmas_name nam_u; -} u3_xmas_peek_pact; +typedef struct _u3_mesa_peek_pact { + u3_mesa_name nam_u; +} u3_mesa_peek_pact; -typedef struct _u3_xmas_hop { +typedef struct _u3_mesa_hop { c3_w len_w; c3_y* dat_y; -} u3_xmas_hop_once; +} u3_mesa_hop_once; -typedef struct _u3_xmas_hop_more { +typedef struct _u3_mesa_hop_more { c3_w len_w; - u3_xmas_hop_once* dat_y; -} u3_xmas_hop_more; + u3_mesa_hop_once* dat_y; +} u3_mesa_hop_more; typedef union { c3_y sot_u[6]; - u3_xmas_hop_once one_u; - u3_xmas_hop_more man_u; -} u3_xmas_hop; + u3_mesa_hop_once one_u; + u3_mesa_hop_more man_u; +} u3_mesa_hop; -typedef struct _u3_xmas_page_pact { - u3_xmas_name nam_u; - u3_xmas_data dat_u; +typedef struct _u3_mesa_page_pact { + u3_mesa_name nam_u; + u3_mesa_data dat_u; union { c3_y sot_u[6]; - u3_xmas_hop_once one_u; - u3_xmas_hop_more man_u; + u3_mesa_hop_once one_u; + u3_mesa_hop_more man_u; }; -} u3_xmas_page_pact; +} u3_mesa_page_pact; -typedef struct _u3_xmas_poke_pact { - u3_xmas_name nam_u; - u3_xmas_name pay_u; - u3_xmas_data dat_u; -} u3_xmas_poke_pact; +typedef struct _u3_mesa_poke_pact { + u3_mesa_name nam_u; + u3_mesa_name pay_u; + u3_mesa_data dat_u; +} u3_mesa_poke_pact; -typedef struct _u3_xmas_pact { - u3_xmas_head hed_u; +typedef struct _u3_mesa_pact { + u3_mesa_head hed_u; union { - u3_xmas_poke_pact pok_u; - u3_xmas_page_pact pag_u; - u3_xmas_peek_pact pek_u; + u3_mesa_poke_pact pok_u; + u3_mesa_page_pact pag_u; + u3_mesa_peek_pact pek_u; }; -} u3_xmas_pact; +} u3_mesa_pact; -c3_w xmas_sift_pact(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w); -c3_w xmas_etch_pact(c3_y* buf_y, u3_xmas_pact* pac_u); +c3_w mesa_sift_pact(u3_mesa_pact* pac_u, c3_y* buf_y, c3_w len_w); +c3_w mesa_etch_pact(c3_y* buf_y, u3_mesa_pact* pac_u); -void xmas_free_pact(u3_xmas_pact* pac_u); +void mesa_free_pact(u3_mesa_pact* pac_u); -void log_pact(u3_xmas_pact* pac_u); +void log_pact(u3_mesa_pact* pac_u); -#endif \ No newline at end of file +#endif diff --git a/pkg/vere/io/xmas/pact.c b/pkg/vere/io/mesa/pact.c similarity index 82% rename from pkg/vere/io/xmas/pact.c rename to pkg/vere/io/mesa/pact.c index 52fa95374a..04ac1b9fa9 100644 --- a/pkg/vere/io/xmas/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -1,4 +1,4 @@ -#include "xmas.h" +#include "mesa.h" #include // only need for tests, can remove #include "vere.h" @@ -9,16 +9,16 @@ // endif tests #define SIFT_VAR(dest, src, len) dest = 0; for(int i = 0; i < len; i++ ) { dest |= ((src + i) >> (8*i)); } -#define CHECK_BOUNDS(cur) if ( len_w < cur ) { u3l_log("xmas: failed parse (%u,%u) at line %i", len_w, cur, __LINE__); return 0; } +#define CHECK_BOUNDS(cur) if ( len_w < cur ) { u3l_log("mesa: failed parse (%u,%u) at line %i", len_w, cur, __LINE__); return 0; } #define safe_dec(num) (num == 0 ? num : num - 1) -#define _xmas_met3_w(a_w) ((c3_bits_word(a_w) + 0x7) >> 3) +#define _mesa_met3_w(a_w) ((c3_bits_word(a_w) + 0x7) >> 3) /* Logging functions */ static void -_log_head(u3_xmas_head* hed_u) +_log_head(u3_mesa_head* hed_u) { u3l_log("-- HEADER --"); u3l_log("next hop: %u", hed_u->nex_y); @@ -40,7 +40,7 @@ _log_buf(c3_y* buf_y, c3_w len_w) } static void -_log_name(u3_xmas_name* nam_u) +_log_name(u3_mesa_name* nam_u) { // u3l_log("meta"); // u3l_log("rank: %u", nam_u->met_u.ran_y); @@ -67,7 +67,7 @@ _log_name(u3_xmas_name* nam_u) } static void -_log_data(u3_xmas_data* dat_u) +_log_data(u3_mesa_data* dat_u) { u3l_log("total fragments: %u", dat_u->tot_w); @@ -129,20 +129,20 @@ _log_data(u3_xmas_data* dat_u) } static void -_log_peek_pact(u3_xmas_peek_pact* pac_u) +_log_peek_pact(u3_mesa_peek_pact* pac_u) { _log_name(&pac_u->nam_u); } static void -_log_page_pact(u3_xmas_page_pact *pac_u) +_log_page_pact(u3_mesa_page_pact *pac_u) { _log_name(&pac_u->nam_u); _log_data(&pac_u->dat_u); } static void -_log_poke_pact(u3_xmas_poke_pact *pac_u) +_log_poke_pact(u3_mesa_poke_pact *pac_u) { _log_name(&pac_u->nam_u); _log_name(&pac_u->pay_u); @@ -150,7 +150,7 @@ _log_poke_pact(u3_xmas_poke_pact *pac_u) } void -log_pact(u3_xmas_pact* pac_u) +log_pact(u3_mesa_pact* pac_u) { switch ( pac_u->hed_u.typ_y ) { case PACT_PEEK: { @@ -175,13 +175,13 @@ log_pact(u3_xmas_pact* pac_u) /* Helper utilities */ static void -_update_hopcount(u3_xmas_head* hed_u) +_update_hopcount(u3_mesa_head* hed_u) { hed_u->hop_y = c3_max(hed_u->hop_y+1, 7); } static c3_y -_xmas_rank(c3_d who_d[2]) +_mesa_rank(c3_d who_d[2]) { if ( who_d[1] ) { return 3; @@ -200,10 +200,10 @@ _xmas_rank(c3_d who_d[2]) /* lifecycle */ -/* xmas_free_pact(): free contents of packet. +/* mesa_free_pact(): free contents of packet. * Does *not* free pac_u itself */ -void xmas_free_pact(u3_xmas_pact* pac_u) +void mesa_free_pact(u3_mesa_pact* pac_u) { switch ( pac_u->hed_u.typ_y ) { default: { @@ -295,9 +295,9 @@ _ames_ship_to_chubs(c3_d sip_d[2], c3_y len_y, c3_y* buf_y) } static c3_o -_xmas_sift_head(c3_y buf_y[8], u3_xmas_head* hed_u) +_mesa_sift_head(c3_y buf_y[8], u3_mesa_head* hed_u) { - if ( memcmp(buf_y + 4, &XMAS_COOKIE, XMAS_COOKIE_LEN) ) { + if ( memcmp(buf_y + 4, &MESA_COOKIE, MESA_COOKIE_LEN) ) { return c3n; } @@ -319,14 +319,14 @@ _xmas_sift_head(c3_y buf_y[8], u3_xmas_head* hed_u) } static c3_w -_xmas_sift_name(u3_xmas_name* nam_u, c3_y* buf_y, c3_w len_w) +_mesa_sift_name(u3_mesa_name* nam_u, c3_y* buf_y, c3_w len_w) { -#ifdef XMAS_DEBUG - //u3l_log("xmas: sifting name %i", len_w); +#ifdef MESA_DEBUG + //u3l_log("mesa: sifting name %i", len_w); #endif c3_w cur_w = 0; - u3_xmas_name_meta met_u; + u3_mesa_name_meta met_u; CHECK_BOUNDS(cur_w + 1); c3_y met_y = buf_y[cur_w]; @@ -387,14 +387,14 @@ _xmas_sift_name(u3_xmas_name* nam_u, c3_y* buf_y, c3_w len_w) } static c3_w -_xmas_sift_data(u3_xmas_data* dat_u, c3_y* buf_y, c3_w len_w) +_mesa_sift_data(u3_mesa_data* dat_u, c3_y* buf_y, c3_w len_w) { -#ifdef XMAS_DEBUG - //u3l_log("xmas: sifting data %i", len_w); +#ifdef MESA_DEBUG + //u3l_log("mesa: sifting data %i", len_w); #endif c3_w cur_w = 0; - u3_xmas_data_meta met_u; + u3_mesa_data_meta met_u; CHECK_BOUNDS(cur_w + 1); c3_y met_y = buf_y[cur_w]; @@ -453,7 +453,7 @@ _xmas_sift_data(u3_xmas_data* dat_u, c3_y* buf_y, c3_w len_w) } static c3_w -_xmas_sift_hop_long(u3_xmas_hop_once* hop_u, c3_y* buf_y, c3_w len_w) +_mesa_sift_hop_long(u3_mesa_hop_once* hop_u, c3_y* buf_y, c3_w len_w) { c3_w cur_w = 0; CHECK_BOUNDS(cur_w + 1); @@ -468,17 +468,17 @@ _xmas_sift_hop_long(u3_xmas_hop_once* hop_u, c3_y* buf_y, c3_w len_w) static c3_w -_xmas_sift_page_pact(u3_xmas_pact* pat_u, c3_y nex_y, c3_y* buf_y, c3_w len_w) +_mesa_sift_page_pact(u3_mesa_pact* pat_u, c3_y nex_y, c3_y* buf_y, c3_w len_w) { - u3_xmas_page_pact* pac_u = &pat_u->pag_u; + u3_mesa_page_pact* pac_u = &pat_u->pag_u; c3_w cur_w = 0, nex_w; - if ( !(nex_w = _xmas_sift_name(&pac_u->nam_u, buf_y + cur_w, len_w)) ) { + if ( !(nex_w = _mesa_sift_name(&pac_u->nam_u, buf_y + cur_w, len_w)) ) { return 0; } cur_w += nex_w; - if ( !(nex_w = _xmas_sift_data(&pac_u->dat_u, buf_y + cur_w, len_w)) ) { + if ( !(nex_w = _mesa_sift_data(&pac_u->dat_u, buf_y + cur_w, len_w)) ) { return 0; } cur_w += nex_w; @@ -494,7 +494,7 @@ _xmas_sift_page_pact(u3_xmas_pact* pat_u, c3_y nex_y, c3_y* buf_y, c3_w len_w) cur_w += 6; } break; case HOP_LONG: { - c3_w hop_w = _xmas_sift_hop_long(&pac_u->one_u, buf_y + cur_w, len_w - cur_w); + c3_w hop_w = _mesa_sift_hop_long(&pac_u->one_u, buf_y + cur_w, len_w - cur_w); if( hop_w == 0 ) { return 0; } @@ -505,10 +505,10 @@ _xmas_sift_page_pact(u3_xmas_pact* pat_u, c3_y nex_y, c3_y* buf_y, c3_w len_w) pac_u->man_u.len_w = buf_y[cur_w]; cur_w++; - pac_u->man_u.dat_y = c3_calloc(sizeof(u3_xmas_hop_once) * pac_u->man_u.len_w); + pac_u->man_u.dat_y = c3_calloc(sizeof(u3_mesa_hop_once) * pac_u->man_u.len_w); for( int i = 0; i < pac_u->man_u.len_w; i++ ) { - c3_w hop_w = _xmas_sift_hop_long(&pac_u->man_u.dat_y[i], buf_y + cur_w ,len_w - cur_w); + c3_w hop_w = _mesa_sift_hop_long(&pac_u->man_u.dat_y[i], buf_y + cur_w ,len_w - cur_w); if ( hop_w == 0 ) { return 0; } @@ -522,11 +522,11 @@ _xmas_sift_page_pact(u3_xmas_pact* pat_u, c3_y nex_y, c3_y* buf_y, c3_w len_w) static c3_w -_xmas_sift_peek_pact(u3_xmas_peek_pact* pac_u, c3_y* buf_y, c3_w len_w) +_mesa_sift_peek_pact(u3_mesa_peek_pact* pac_u, c3_y* buf_y, c3_w len_w) { - c3_w siz_w = _xmas_sift_name(&pac_u->nam_u, buf_y, len_w); + c3_w siz_w = _mesa_sift_name(&pac_u->nam_u, buf_y, len_w); if ( siz_w < len_w ) { - u3l_log("xmas: failed to consume entire packet"); + u3l_log("mesa: failed to consume entire packet"); _log_buf(buf_y + siz_w, len_w - siz_w); return 0; } @@ -535,23 +535,23 @@ _xmas_sift_peek_pact(u3_xmas_peek_pact* pac_u, c3_y* buf_y, c3_w len_w) } static c3_w -_xmas_sift_poke_pact(u3_xmas_poke_pact* pac_u, c3_y* buf_y, c3_w len_w) +_mesa_sift_poke_pact(u3_mesa_poke_pact* pac_u, c3_y* buf_y, c3_w len_w) { c3_w cur_w = 0, nex_w; // ack path - if ( !(nex_w = _xmas_sift_name(&pac_u->nam_u, buf_y + cur_w, len_w)) ) { + if ( !(nex_w = _mesa_sift_name(&pac_u->nam_u, buf_y + cur_w, len_w)) ) { return 0; } cur_w += nex_w; // payload path - if ( !(nex_w = _xmas_sift_name(&pac_u->pay_u, buf_y + cur_w, len_w)) ) { + if ( !(nex_w = _mesa_sift_name(&pac_u->pay_u, buf_y + cur_w, len_w)) ) { return 0; } cur_w += nex_w; // payload - if ( !(nex_w = _xmas_sift_data(&pac_u->dat_u, buf_y + cur_w, len_w)) ) { + if ( !(nex_w = _mesa_sift_data(&pac_u->dat_u, buf_y + cur_w, len_w)) ) { return 0; } cur_w += nex_w; @@ -560,30 +560,30 @@ _xmas_sift_poke_pact(u3_xmas_poke_pact* pac_u, c3_y* buf_y, c3_w len_w) } c3_w -xmas_sift_pact(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w) +mesa_sift_pact(u3_mesa_pact* pac_u, c3_y* buf_y, c3_w len_w) { c3_w res_w = 0; if ( len_w < 8 ) { - u3l_log("xmas: attempted to parse overly short packet of size %u", len_w); + u3l_log("mesa: attempted to parse overly short packet of size %u", len_w); } - _xmas_sift_head(buf_y, &pac_u->hed_u); + _mesa_sift_head(buf_y, &pac_u->hed_u); buf_y += 8; len_w -= 8; switch ( pac_u->hed_u.typ_y ) { case PACT_PEEK: { - res_w = _xmas_sift_peek_pact(&pac_u->pek_u, buf_y, len_w); + res_w = _mesa_sift_peek_pact(&pac_u->pek_u, buf_y, len_w); } break; case PACT_PAGE: { - res_w = _xmas_sift_page_pact(pac_u, pac_u->hed_u.nex_y, buf_y, len_w); + res_w = _mesa_sift_page_pact(pac_u, pac_u->hed_u.nex_y, buf_y, len_w); } break; case PACT_POKE: { - res_w = _xmas_sift_poke_pact(&pac_u->pok_u, buf_y, len_w); + res_w = _mesa_sift_poke_pact(&pac_u->pok_u, buf_y, len_w); } break; default: { - u3l_log("xmas: received unknown packet type"); + u3l_log("mesa: received unknown packet type"); _log_buf(buf_y, len_w); break; } @@ -595,7 +595,7 @@ xmas_sift_pact(u3_xmas_pact* pac_u, c3_y* buf_y, c3_w len_w) /* serialisation */ static void -_xmas_etch_head(u3_xmas_head* hed_u, c3_y buf_y[8]) +_mesa_etch_head(u3_mesa_head* hed_u, c3_y buf_y[8]) { if ( 1 != hed_u->pro_y ) { u3l_log("etching bad head"); @@ -615,20 +615,20 @@ _xmas_etch_head(u3_xmas_head* hed_u, c3_y buf_y[8]) }*/ _ames_etch_word(buf_y, hed_w); - memcpy(buf_y + 4, XMAS_COOKIE, XMAS_COOKIE_LEN); + memcpy(buf_y + 4, MESA_COOKIE, MESA_COOKIE_LEN); } static c3_w -_xmas_etch_name(c3_y* buf_y, u3_xmas_name* nam_u) +_mesa_etch_name(c3_y* buf_y, u3_mesa_name* nam_u) { -#ifdef XMAS_DEBUG +#ifdef MESA_DEBUG -#endif +#endif c3_w cur_w = 0; - u3_xmas_name_meta met_u; + u3_mesa_name_meta met_u; - met_u.ran_y = _xmas_rank(nam_u->her_d); - met_u.rif_y = safe_dec(_xmas_met3_w(nam_u->rif_w)); + met_u.ran_y = _mesa_rank(nam_u->her_d); + met_u.rif_y = safe_dec(_mesa_met3_w(nam_u->rif_w)); if ( c3y == nam_u->nit_o ) { assert( c3n == nam_u->aut_o ); // XX @@ -639,7 +639,7 @@ _xmas_etch_name(c3_y* buf_y, u3_xmas_name* nam_u) else { met_u.nit_y = 0; met_u.tau_y = (c3y == nam_u->aut_o) ? 1 : 0; - met_u.gaf_y = safe_dec(_xmas_met3_w(nam_u->fra_w)); + met_u.gaf_y = safe_dec(_mesa_met3_w(nam_u->fra_w)); } c3_y met_y = (met_u.ran_y & 0x3) << 0 @@ -687,21 +687,21 @@ _xmas_etch_name(c3_y* buf_y, u3_xmas_name* nam_u) } static c3_w -_xmas_etch_data(c3_y* buf_y, u3_xmas_data* dat_u) +_mesa_etch_data(c3_y* buf_y, u3_mesa_data* dat_u) { -#ifdef XMAS_DEBUG +#ifdef MESA_DEBUG #endif c3_w cur_w = 0; - u3_xmas_data_meta met_u; + u3_mesa_data_meta met_u; - met_u.bot_y = safe_dec(_xmas_met3_w(dat_u->tot_w)); + met_u.bot_y = safe_dec(_mesa_met3_w(dat_u->tot_w)); // XX met_u.aul_y = dat_u->aum_u.typ_e; met_u.aur_y = dat_u->aup_u.len_y; - c3_y nel_y = _xmas_met3_w(dat_u->len_w); + c3_y nel_y = _mesa_met3_w(dat_u->len_w); met_u.men_y = (3 >= nel_y) ? nel_y : 3; c3_y met_y = (met_u.bot_y & 0x3) << 0 @@ -751,16 +751,16 @@ _xmas_etch_data(c3_y* buf_y, u3_xmas_data* dat_u) } static c3_w -_xmas_etch_page_pact(c3_y* buf_y, u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u) +_mesa_etch_page_pact(c3_y* buf_y, u3_mesa_page_pact* pac_u, u3_mesa_head* hed_u) { c3_w cur_w = 0, nex_w; - if ( !(nex_w = _xmas_etch_name(buf_y + cur_w, &pac_u->nam_u)) ) { + if ( !(nex_w = _mesa_etch_name(buf_y + cur_w, &pac_u->nam_u)) ) { return 0; } cur_w += nex_w; - if ( !(nex_w = _xmas_etch_data(buf_y + cur_w, &pac_u->dat_u)) ) { + if ( !(nex_w = _mesa_etch_data(buf_y + cur_w, &pac_u->dat_u)) ) { return 0; } cur_w += nex_w; @@ -771,21 +771,21 @@ _xmas_etch_page_pact(c3_y* buf_y, u3_xmas_page_pact* pac_u, u3_xmas_head* hed_u) } static c3_w -_xmas_etch_poke_pact(c3_y* buf_y, u3_xmas_poke_pact* pac_u, u3_xmas_head* hed_u) +_mesa_etch_poke_pact(c3_y* buf_y, u3_mesa_poke_pact* pac_u, u3_mesa_head* hed_u) { c3_w cur_w = 0, nex_w; - if ( !(nex_w = _xmas_etch_name(buf_y + cur_w, &pac_u->nam_u)) ) { + if ( !(nex_w = _mesa_etch_name(buf_y + cur_w, &pac_u->nam_u)) ) { return 0; } cur_w += nex_w; - if ( !(nex_w = _xmas_etch_name(buf_y + cur_w, &pac_u->pay_u)) ) { + if ( !(nex_w = _mesa_etch_name(buf_y + cur_w, &pac_u->pay_u)) ) { return 0; } cur_w += nex_w; - if ( !(nex_w = _xmas_etch_data(buf_y + cur_w, &pac_u->dat_u)) ) { + if ( !(nex_w = _mesa_etch_data(buf_y + cur_w, &pac_u->dat_u)) ) { return 0; } cur_w += nex_w; @@ -798,20 +798,20 @@ _xmas_etch_poke_pact(c3_y* buf_y, u3_xmas_poke_pact* pac_u, u3_xmas_head* hed_u) /* sizing */ static c3_w -_xmas_size_name(u3_xmas_name* nam_u) +_mesa_size_name(u3_mesa_name* nam_u) { c3_w siz_w = 1; - u3_xmas_name_meta met_u; + u3_mesa_name_meta met_u; - met_u.ran_y = _xmas_rank(nam_u->her_d); - met_u.rif_y = safe_dec(_xmas_met3_w(nam_u->rif_w)); + met_u.ran_y = _mesa_rank(nam_u->her_d); + met_u.rif_y = safe_dec(_mesa_met3_w(nam_u->rif_w)); siz_w += 2 << met_u.ran_y; siz_w += met_u.rif_y + 1; siz_w++; // bloq if (c3n == nam_u->nit_o ) { - met_u.gaf_y = safe_dec(_xmas_met3_w(nam_u->fra_w)); + met_u.gaf_y = safe_dec(_mesa_met3_w(nam_u->fra_w)); siz_w += met_u.gaf_y + 1; } @@ -822,12 +822,12 @@ _xmas_size_name(u3_xmas_name* nam_u) } static c3_w -_xmas_size_data(u3_xmas_data* dat_u) +_mesa_size_data(u3_mesa_data* dat_u) { c3_w siz_w = 1; - u3_xmas_data_meta met_u; + u3_mesa_data_meta met_u; - met_u.bot_y = safe_dec(_xmas_met3_w(dat_u->tot_w)); + met_u.bot_y = safe_dec(_mesa_met3_w(dat_u->tot_w)); siz_w += met_u.bot_y + 1; @@ -845,7 +845,7 @@ _xmas_size_data(u3_xmas_data* dat_u) siz_w += 32 * dat_u->aup_u.len_y; - c3_y nel_y = _xmas_met3_w(dat_u->len_w); + c3_y nel_y = _mesa_met3_w(dat_u->len_w); met_u.men_y = (3 >= nel_y) ? nel_y : 3; if ( 3 == met_u.men_y ) { @@ -859,25 +859,25 @@ _xmas_size_data(u3_xmas_data* dat_u) } static c3_w -_xmas_size_pact(u3_xmas_pact* pac_u) +_mesa_size_pact(u3_mesa_pact* pac_u) { c3_w siz_w = 8; // header + cookie; switch ( pac_u->hed_u.typ_y ) { case PACT_PEEK: { - siz_w += _xmas_size_name(&pac_u->pek_u.nam_u); + siz_w += _mesa_size_name(&pac_u->pek_u.nam_u); } break; case PACT_PAGE: { - siz_w += _xmas_size_name(&pac_u->pag_u.nam_u); - siz_w += _xmas_size_data(&pac_u->pag_u.dat_u); + siz_w += _mesa_size_name(&pac_u->pag_u.nam_u); + siz_w += _mesa_size_data(&pac_u->pag_u.dat_u); // XX hops } break; case PACT_POKE: { - siz_w += _xmas_size_name(&pac_u->pok_u.nam_u); - siz_w += _xmas_size_name(&pac_u->pok_u.pay_u); - siz_w += _xmas_size_data(&pac_u->pok_u.dat_u); + siz_w += _mesa_size_name(&pac_u->pok_u.nam_u); + siz_w += _mesa_size_name(&pac_u->pok_u.pay_u); + siz_w += _mesa_size_data(&pac_u->pok_u.dat_u); } break; default: { @@ -890,34 +890,34 @@ _xmas_size_pact(u3_xmas_pact* pac_u) } c3_w -xmas_etch_pact(c3_y* buf_y, u3_xmas_pact* pac_u) +mesa_etch_pact(c3_y* buf_y, u3_mesa_pact* pac_u) { - c3_w siz_w = _xmas_size_pact(pac_u); + c3_w siz_w = _mesa_size_pact(pac_u); if ( siz_w > PACT_SIZE ) { fprintf(stderr, "etch: would overflow %u\r\n", siz_w); return 0; } c3_w cur_w = 0, nex_w; - u3_xmas_head* hed_u = &pac_u->hed_u; - _xmas_etch_head(hed_u, buf_y + cur_w); + u3_mesa_head* hed_u = &pac_u->hed_u; + _mesa_etch_head(hed_u, buf_y + cur_w); cur_w += 8; switch ( pac_u->hed_u.typ_y ) { case PACT_POKE: { - if ( !(nex_w = _xmas_etch_poke_pact(buf_y + cur_w, &pac_u->pok_u, hed_u)) ) { + if ( !(nex_w = _mesa_etch_poke_pact(buf_y + cur_w, &pac_u->pok_u, hed_u)) ) { return 0; } } break; case PACT_PEEK: { - if ( !(nex_w = _xmas_etch_name(buf_y + cur_w, &pac_u->pek_u.nam_u)) ) { + if ( !(nex_w = _mesa_etch_name(buf_y + cur_w, &pac_u->pek_u.nam_u)) ) { return 0; } } break; case PACT_PAGE: { - if ( !(nex_w = _xmas_etch_page_pact(buf_y + cur_w, &pac_u->pag_u, hed_u)) ) { + if ( !(nex_w = _mesa_etch_page_pact(buf_y + cur_w, &pac_u->pag_u, hed_u)) ) { return 0; } } break; @@ -937,10 +937,10 @@ xmas_etch_pact(c3_y* buf_y, u3_xmas_pact* pac_u) #ifdef PACT_TEST -/* _xmas_encode_path(): produce buf_y as a parsed path +/* _mesa_encode_path(): produce buf_y as a parsed path */ static u3_noun -_xmas_encode_path(c3_w len_w, c3_y* buf_y) +_mesa_encode_path(c3_w len_w, c3_y* buf_y) { u3_noun pro; u3_noun* lit = &pro; @@ -981,7 +981,7 @@ _xmas_encode_path(c3_w len_w, c3_y* buf_y) #define cmp_scalar(nam, str, fmt) \ if ( hav_u->nam != ned_u->nam ) { \ - fprintf(stderr, "xmas test cmp " str " differ:\r\n" \ + fprintf(stderr, "mesa test cmp " str " differ:\r\n" \ " have: " fmt "\r\n" \ " need: " fmt "\r\n", \ hav_u->nam, ned_u->nam); \ @@ -990,7 +990,7 @@ _xmas_encode_path(c3_w len_w, c3_y* buf_y) #define cmp_string(nam, siz, str) \ if ( memcmp(hav_u->nam, ned_u->nam, siz) ) { \ - fprintf(stderr, "xmas test cmp " str " differ:\r\n" \ + fprintf(stderr, "mesa test cmp " str " differ:\r\n" \ " have: %*.s\r\n" \ " need: %*.s\r\n", \ siz, hav_u->nam, siz, ned_u->nam); \ @@ -999,12 +999,12 @@ _xmas_encode_path(c3_w len_w, c3_y* buf_y) #define cmp_buffer(nam, siz, str) \ if ( memcmp(hav_u->nam, ned_u->nam, siz) ) { \ - fprintf(stderr, "xmas test cmp " str "\r\n"); \ + fprintf(stderr, "mesa test cmp " str "\r\n"); \ ret_i = 1; \ } static c3_i -_test_cmp_head(u3_xmas_head* hav_u, u3_xmas_head* ned_u) +_test_cmp_head(u3_mesa_head* hav_u, u3_mesa_head* ned_u) { c3_i ret_i = 0; @@ -1018,7 +1018,7 @@ _test_cmp_head(u3_xmas_head* hav_u, u3_xmas_head* ned_u) } static c3_i -_test_cmp_name(u3_xmas_name* hav_u, u3_xmas_name* ned_u) +_test_cmp_name(u3_mesa_name* hav_u, u3_mesa_name* ned_u) { c3_i ret_i = 0; @@ -1037,7 +1037,7 @@ _test_cmp_name(u3_xmas_name* hav_u, u3_xmas_name* ned_u) } static c3_i -_test_cmp_data(u3_xmas_data* hav_u, u3_xmas_data* ned_u) +_test_cmp_data(u3_mesa_data* hav_u, u3_mesa_data* ned_u) { c3_i ret_i = 0; @@ -1056,16 +1056,16 @@ _test_cmp_data(u3_xmas_data* hav_u, u3_xmas_data* ned_u) } static c3_i -_test_pact(u3_xmas_pact* pac_u) +_test_pact(u3_mesa_pact* pac_u) { c3_y* buf_y = c3_calloc(PACT_SIZE); - c3_w len_w = xmas_etch_pact(buf_y, pac_u); + c3_w len_w = mesa_etch_pact(buf_y, pac_u); c3_i ret_i = 0; c3_i bot_i = 0; c3_w sif_w; - u3_xmas_pact nex_u; - memset(&nex_u, 0, sizeof(u3_xmas_pact)); + u3_mesa_pact nex_u; + memset(&nex_u, 0, sizeof(u3_mesa_pact)); if ( !len_w ) { fprintf(stderr, "pact: etch failed\r\n"); @@ -1076,7 +1076,7 @@ _test_pact(u3_xmas_pact* pac_u) ret_i = 1; goto done; } - if ( len_w != (sif_w = xmas_sift_pact(&nex_u, buf_y, len_w)) ) { + if ( len_w != (sif_w = mesa_sift_pact(&nex_u, buf_y, len_w)) ) { fprintf(stderr, "pact: sift failed len=%u sif=%u\r\n", len_w, sif_w); _log_buf(buf_y, len_w); ret_i = 1; goto done; @@ -1245,7 +1245,7 @@ _test_rand_path(void* ptr_v, c3_s len_s, c3_c* pat_c) } static void -_test_make_head(void* ptr_v, u3_xmas_head* hed_u) +_test_make_head(void* ptr_v, u3_mesa_head* hed_u) { hed_u->nex_y = NEXH_NONE; // XX hed_u->pro_y = 1; @@ -1256,7 +1256,7 @@ _test_make_head(void* ptr_v, u3_xmas_head* hed_u) } static void -_test_make_name(void* ptr_v, c3_s pat_s, u3_xmas_name* nam_u) +_test_make_name(void* ptr_v, c3_s pat_s, u3_mesa_name* nam_u) { _test_rand_bytes(ptr_v, 16, (c3_y*)nam_u->her_d); nam_u->rif_w = _test_rand_word(ptr_v); @@ -1280,7 +1280,7 @@ _test_make_name(void* ptr_v, c3_s pat_s, u3_xmas_name* nam_u) } static void -_test_make_data(void* ptr_v, u3_xmas_data* dat_u) +_test_make_data(void* ptr_v, u3_mesa_data* dat_u) { dat_u->tot_w = _test_rand_word(ptr_v); @@ -1316,7 +1316,7 @@ _test_make_data(void* ptr_v, u3_xmas_data* dat_u) } static void -_test_make_pact(void* ptr_v, u3_xmas_pact* pac_u) +_test_make_pact(void* ptr_v, u3_mesa_pact* pac_u) { _test_make_head(ptr_v, &pac_u->hed_u); @@ -1343,7 +1343,7 @@ _test_make_pact(void* ptr_v, u3_xmas_pact* pac_u) static c3_i _test_rand_pact(c3_w bat_w) { - u3_xmas_pact pac_u; + u3_mesa_pact pac_u; void* ptr_v = 0; fprintf(stderr, "pact: test roundtrip %u random packets\r\n", bat_w); @@ -1365,12 +1365,12 @@ _test_rand_pact(c3_w bat_w) static void _test_sift_page() { - u3_xmas_pact pac_u; - memset(&pac_u,0, sizeof(u3_xmas_pact)); + u3_mesa_pact pac_u; + memset(&pac_u,0, sizeof(u3_mesa_pact)); pac_u.hed_u.typ_y = PACT_PAGE; pac_u.hed_u.pro_y = 1; u3l_log("%%page checking sift/etch idempotent"); - u3_xmas_name* nam_u = &pac_u.pag_u.nam_u; + u3_mesa_name* nam_u = &pac_u.pag_u.nam_u; { u3_noun her = u3v_wish("~hastuc-dibtux"); @@ -1384,7 +1384,7 @@ _test_sift_page() nam_u->fra_w = 54; nam_u->nit_o = c3n; - u3_xmas_data* dat_u = &pac_u.pag_u.dat_u; + u3_mesa_data* dat_u = &pac_u.pag_u.dat_u; dat_u->aum_u.typ_e = AUTH_NONE; dat_u->tot_w = 1000; dat_u->len_w = 1024; @@ -1402,7 +1402,7 @@ static void _test_encode_path(c3_c* pat_c) { u3_noun wan = u3do("stab", u3dt("cat", 3, '/', u3i_string(pat_c))); - u3_noun hav = _xmas_encode_path(strlen(pat_c), (c3_y*)pat_c); + u3_noun hav = _mesa_encode_path(strlen(pat_c), (c3_y*)pat_c); if ( c3n == u3r_sing(wan, hav) ) { u3l_log(RED_TEXT); @@ -1454,4 +1454,4 @@ int main() } -#endif \ No newline at end of file +#endif diff --git a/pkg/vere/main.c b/pkg/vere/main.c index 930d8981ad..ec58b6dafc 100644 --- a/pkg/vere/main.c +++ b/pkg/vere/main.c @@ -1089,6 +1089,9 @@ _cw_serf_commence(c3_i argc, c3_c* argv[]) exit(1); } +#if defined(U3_OS_osx) + darwin_register_mach_exception_handler(); +#endif // XX use named arguments and getopt c3_d eve_d = 0; @@ -1586,6 +1589,23 @@ _cw_info(c3_i argc, c3_c* argv[]) u3_disk_slog(log_u); printf("\n"); + + + { + c3_z len_z = u3_disk_epoc_list(log_u, 0); + c3_d* sot_d = c3_malloc(len_z * sizeof(c3_d)); + u3_disk_epoc_list(log_u, sot_d); + + fprintf(stderr, "epocs:\r\n"); + + while ( len_z-- ) { + fprintf(stderr, " 0i%" PRIu64 "\r\n", sot_d[len_z]); + } + + c3_free(sot_d); + fprintf(stderr, "\r\n"); + } + u3_lmdb_stat(log_u->mdb_u, stdout); u3_disk_exit(log_u); @@ -2198,10 +2218,17 @@ _cw_play_impl(c3_d eve_d, c3_d sap_d, c3_o mel_o, c3_o sof_o, c3_o ful_o) _cw_play_snap(log_u); } - // XX this should check that snapshot is within epoc, - // and load from the epoc / reboot if it is not + u3_Host.eve_d = u3m_boot(u3_Host.dir_c, (size_t)1 << u3_Host.ops_u.lom_y); + + // XX this should load from the epoc snapshot + // but that clobbers chk/ which is risky // - u3m_boot(u3_Host.dir_c, (size_t)1 << u3_Host.ops_u.lom_y); + if ( u3_Host.eve_d < log_u->epo_d ) { + fprintf(stderr, "mars: pier corrupt: " + "snapshot (%" PRIu64 ") out of epoc (%" PRIu64 ")\r\n", + u3_Host.eve_d, log_u->epo_d); + exit(1); + } u3C.slog_f = _cw_play_slog; @@ -2222,6 +2249,7 @@ _cw_play_impl(c3_d eve_d, c3_d sap_d, c3_o mel_o, c3_o sof_o, c3_o ful_o) } u3_disk_exit(log_u); + // NB: loom migrations without replay are not saved u3m_stop(); return pay_d; @@ -2320,7 +2348,7 @@ _cw_play(c3_i argc, c3_c* argv[]) } if ( !_cw_play_impl(eve_d, sap_d, mel_o, sof_o, ful_o) ) { - fprintf(stderr, "mars: nothing to do!"); + fprintf(stderr, "mars: nothing to do!\r\n"); } } @@ -2475,48 +2503,10 @@ _cw_chop(c3_i argc, c3_c* argv[]) u3_disk* log_u = _cw_disk_init(u3_Host.dir_c); u3_disk_kindly(log_u, u3_Host.eve_d); + u3_disk_chop(log_u, u3_Host.eve_d); - // create new epoch - c3_d fir_d, las_d; - if ( c3n == u3_lmdb_gulf(log_u->mdb_u, &fir_d, &las_d) ) { - fprintf(stderr, "chop: failed to get first/last events\r\n"); - exit(1); - } - - // create new epoch if latest isn't empty - if ( (fir_d != las_d) && (c3n == u3_disk_epoc_init(log_u, las_d)) ) { - fprintf(stderr, "chop: failed to create new epoch\r\n"); - exit(1); - } - - c3_z len_z = u3_disk_epoc_list(log_u, 0); - - if ( len_z <= 2 ) { - fprintf(stderr, "chop: nothing to do, have a great day\r\n"); - exit(0); // enjoy - } - - c3_d* sot_d = c3_malloc(len_z * sizeof(c3_d)); - u3_disk_epoc_list(log_u, sot_d); - - // delete all but the last two epochs - // - // XX parameterize the number of epochs to chop - // - for ( c3_z i_z = 2; i_z < len_z; i_z++ ) { - fprintf(stderr, "chop: deleting epoch 0i%" PRIc3_d "\r\n", sot_d[i_z]); - if ( c3y != u3_disk_epoc_kill(log_u, sot_d[i_z]) ) { - fprintf(stderr, "chop: failed to delete epoch 0i%" PRIu64 "\r\n", sot_d[i_z]); - exit(1); - } - } - - // cleanup - c3_free(sot_d); u3_disk_exit(log_u); - - // success - fprintf(stderr, "chop: event log truncation complete\r\n"); + u3m_stop(); } /* _cw_roll(): rollover to new epoch @@ -2574,35 +2564,10 @@ _cw_roll(c3_i argc, c3_c* argv[]) u3_disk* log_u = _cw_disk_init(u3_Host.dir_c); u3_disk_kindly(log_u, u3_Host.eve_d); + u3_disk_roll(log_u, u3_Host.eve_d); - // check if there's a *current* snapshot - if ( log_u->dun_d != u3A->eve_d ) { - fprintf(stderr, "roll: error: snapshot is out of date, please " - "start/shutdown your pier gracefully first\r\n"); - fprintf(stderr, "roll: eve_d: %" PRIc3_d ", dun_d: %" PRIc3_d "\r\n", \ - u3A->eve_d, log_u->dun_d); - exit(1); - } - - // create new epoch - c3_d fir_d, las_d; - if ( c3n == u3_lmdb_gulf(log_u->mdb_u, &fir_d, &las_d) ) { - fprintf(stderr, "roll: failed to get first/last events\r\n"); - exit(1); - } - - if ( fir_d == las_d ) { - fprintf(stderr, "roll: latest epoch already empty\r\n"); - exit(0); - } - else if ( c3n == u3_disk_epoc_init(log_u, las_d) ) { - fprintf(stderr, "roll: failed to create new epoch\r\n"); - exit(1); - } - - // success - c3_d epo_d = log_u->dun_d + 1; - fprintf(stderr, "roll: epoch rollover complete\r\n"); + u3_disk_exit(log_u); + u3m_stop(); } /* _cw_vere(): download vere @@ -2909,6 +2874,10 @@ main(c3_i argc, _main_init(); +#if defined(U3_OS_osx) + darwin_register_mach_exception_handler(); +#endif + c3_c* bin_c = strdup(argv[0]); // parse for subcommands diff --git a/pkg/vere/mars.c b/pkg/vere/mars.c index 5e940182dc..db9056a0ab 100644 --- a/pkg/vere/mars.c +++ b/pkg/vere/mars.c @@ -101,6 +101,11 @@ _mars_play_batch(u3_mars* mar_u, u3_noun dud; u3_weak wen = u3_none; + if ( !wok_u ) { + fprintf(stderr, "play: failed to open event log iterator\r\n"); + return _play_log_e; + } + while ( c3y == u3_disk_walk_live(wok_u) ) { if ( c3n == u3_disk_walk_step(wok_u, &tac_u) ) { u3_disk_walk_done(wok_u); @@ -248,7 +253,7 @@ u3_mars_play(u3_mars* mar_u, c3_d eve_d, c3_d sap_d) if ( !mar_u->dun_d ) { c3_w lif_w; - if ( c3n == u3_disk_read_meta(log_u->mdb_u, 0, 0, &lif_w) ) { + if ( c3n == u3_disk_read_meta(log_u->mdb_u, 0, 0, 0, &lif_w) ) { fprintf(stderr, "mars: disk read meta fail\r\n"); // XX exit code, cb // diff --git a/pkg/vere/pier.c b/pkg/vere/pier.c index 5c5b047f3e..7514e17155 100644 --- a/pkg/vere/pier.c +++ b/pkg/vere/pier.c @@ -1644,7 +1644,7 @@ _pier_init(c3_w wag_w, c3_c* pax_c) return 0; } - u3_assert( U3D_VER1 == pir_u->log_u->ver_w ); + u3_assert( U3D_VERLAT == pir_u->log_u->ver_w ); } // initialize compute @@ -1698,7 +1698,8 @@ u3_pier_stay(c3_w wag_w, u3_noun pax) return 0; } - if ( c3n == u3_disk_read_meta(pir_u->log_u->mdb_u, pir_u->who_d, + if ( c3n == u3_disk_read_meta(pir_u->log_u->mdb_u, + &pir_u->log_u->ver_w, pir_u->who_d, &pir_u->fak_o, &pir_u->lif_w) ) { fprintf(stderr, "pier: disk read meta fail\r\n"); @@ -1895,7 +1896,8 @@ _pier_boot_plan(u3_pier* pir_u, pir_u->lif_w = u3qb_lent(bot_u.bot); } - if ( c3n == u3_disk_save_meta(pir_u->log_u->mdb_u, pir_u->who_d, + if ( c3n == u3_disk_save_meta(pir_u->log_u->mdb_u, + pir_u->log_u->ver_w, pir_u->who_d, pir_u->fak_o, pir_u->lif_w) ) { // XX dispose bot_u @@ -1903,6 +1905,13 @@ _pier_boot_plan(u3_pier* pir_u, return c3n; } + if ( c3n == u3_disk_save_meta_meta(pir_u->log_u->com_u->pax_c, + pir_u->who_d, pir_u->fak_o, pir_u->lif_w) ) + { + fprintf(stderr, "disk: failed to save top-level metadata\r\n"); + return c3n; + } + // insert boot sequence directly // // Note that these are not ovum or (pair @da ovum) events, diff --git a/pkg/vere/platform/darwin/mach.c b/pkg/vere/platform/darwin/mach.c new file mode 100644 index 0000000000..9775882d2e --- /dev/null +++ b/pkg/vere/platform/darwin/mach.c @@ -0,0 +1,28 @@ +#include "log.h" + +#include +#include +#include +#include + + +// lldb does not listen to BSD signals, it only listens to Mach exceptions. +// The Mach exception EXC_BAD_ACCESS corresponds to SIGSEGV, but lldb has +// problems converting between them because of a longstanding macOS kernel bug. +// This means that without this workaround we cannot debug our binaries with +// lldb. The first segfault we hit causes an infinite loop in lldb no matter +// how many times you try to continue. This workaround is implemented in projects +// such as the Go runtime. +// See https://bugs.llvm.org/show_bug.cgi?id=22868#c1 for more details. + +void darwin_register_mach_exception_handler() { + kern_return_t kr = task_set_exception_ports( + mach_task_self(), + EXC_MASK_BAD_ACCESS, // SIGSEGV + MACH_PORT_NULL, + EXCEPTION_STATE_IDENTITY, + MACHINE_THREAD_STATE); + if ( KERN_SUCCESS != kr) { + u3l_log("mach: unable to register exception handler"); + } +} diff --git a/pkg/vere/vere.h b/pkg/vere/vere.h index b1cd43fd74..96ce777e75 100644 --- a/pkg/vere/vere.h +++ b/pkg/vere/vere.h @@ -542,7 +542,7 @@ u3_dire* urb_u; // urbit system data u3_dire* com_u; // log directory c3_o liv_o; // live - c3_w ver_w; // pier version + c3_w ver_w; // version (see version.h) void* mdb_u; // lmdb env of current epoch c3_d sen_d; // commit requested c3_d dun_d; // committed @@ -976,6 +976,7 @@ */ c3_o u3_disk_read_meta(MDB_env* mdb_u, + c3_w* ver_w, c3_d* who_d, c3_o* fak_o, c3_w* lif_w); @@ -984,10 +985,19 @@ */ c3_o u3_disk_save_meta(MDB_env* mdb_u, + c3_w ver_w, c3_d who_d[2], c3_o fak_o, c3_w lif_w); + /* u3_disk_save_meta_meta(): save meta metadata. + */ + c3_o + u3_disk_save_meta_meta(c3_c* log_c, + c3_d who_d[2], + c3_o fak_o, + c3_w lif_w); + /* u3_disk_read(): read [len_d] events starting at [eve_d]. */ void @@ -1008,16 +1018,6 @@ void u3_disk_plan(u3_disk* log_u, u3_fact* tac_u); - /* u3_disk_epoc_init(): create new epoch. - */ - c3_o - u3_disk_epoc_init(u3_disk* log_u, c3_d epo_d); - - /* u3_disk_epoc_kill(): delete an epoch. - */ - c3_o - u3_disk_epoc_kill(u3_disk* log_u, c3_d epo_d); - /* u3_disk_epoc_last(): get latest epoch number. */ c3_o @@ -1033,6 +1033,16 @@ void u3_disk_kindly(u3_disk* log_u, c3_d eve_d); + /* u3_disk_chop(): delete all but the latest 2 epocs. + */ + void + u3_disk_chop(u3_disk* log_u, c3_d epo_d); + + /* u3_disk_roll(): rollover to a new epoc. + */ + void + u3_disk_roll(u3_disk* log_u, c3_d epo_d); + /* u3_disk_read_list(): synchronously read a cons list of events. */ u3_weak @@ -1215,10 +1225,10 @@ u3_noun u3_ames_encode_lane(u3_lane); - /** xmas + /** mesa **/ u3_auto* - u3_xmas_io_init(u3_pier* pir_u); + u3_mesa_io_init(u3_pier* pir_u); /** Autosave. **/ /* u3_save_ef_chld(): report SIGCHLD. @@ -1539,6 +1549,13 @@ void u3_daemon_init(); +#if defined(U3_OS_osx) + /* darwin_register_mach_exception_handler(): make lldb work + */ + void + darwin_register_mach_exception_handler(); +#endif + /* u3_write_fd(): retry interrupts, continue partial writes, assert errors. */ void From 2f829ea2668f309c39e65257b4f552b3c1c3385c Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Fri, 8 Mar 2024 21:03:13 +0100 Subject: [PATCH 064/430] mesa: wip --- pkg/c3/motes.h | 3 +++ pkg/vere/BUILD.bazel | 2 +- pkg/vere/auto.c | 2 +- pkg/vere/io/blake.c | 40 ++++++++++++++++++++-------------------- pkg/vere/io/mesa.c | 11 ++--------- pkg/vere/xmas_tests.c | 2 +- 6 files changed, 28 insertions(+), 32 deletions(-) diff --git a/pkg/c3/motes.h b/pkg/c3/motes.h index f2c52459e3..863a0710b4 100644 --- a/pkg/c3/motes.h +++ b/pkg/c3/motes.h @@ -725,6 +725,8 @@ # define c3__lt c3_s2('l','t') # define c3__lull c3_s4('l','u','l','l') # define c3__lunt c3_s4('l','u','n','t') +# define c3__m c3_s1('m') +# define c3__mx c3_s2('m','x') # define c3__mach c3_s4('m','a','c','h') # define c3__mack c3_s4('m','a','c','k') # define c3__made c3_s4('m','a','d','e') @@ -751,6 +753,7 @@ # define c3__meme c3_s4('m','e','m','e') # define c3__memo c3_s4('m','e','m','o') # define c3__menu c3_s4('m','e','n','u') +# define c3__mesa c3_s4('m','e','s','a') # define c3__mesh c3_s4('m','e','s','h') # define c3__mess c3_s4('m','e','s','s') # define c3__met c3_s3('m','e','t') diff --git a/pkg/vere/BUILD.bazel b/pkg/vere/BUILD.bazel index ee09e201f8..a87045c67e 100644 --- a/pkg/vere/BUILD.bazel +++ b/pkg/vere/BUILD.bazel @@ -216,7 +216,7 @@ cc_test( cc_test( name = "xmas_tests", timeout = "short", - srcs = ["io/xmas.c"], + srcs = ["io/mesa.c"], features = select({ "@platforms//os:linux": ["fully_static_link"], "//conditions:default": [], diff --git a/pkg/vere/auto.c b/pkg/vere/auto.c index 1aaee81d27..b2da91688e 100644 --- a/pkg/vere/auto.c +++ b/pkg/vere/auto.c @@ -439,7 +439,7 @@ u3_auto_init(u3_pier* pir_u) car_u = _auto_link(u3_term_io_init(pir_u), pir_u, car_u); car_u = _auto_link(u3_fore_io_init(pir_u), pir_u, car_u); car_u = _auto_link(u3_lick_io_init(pir_u), pir_u, car_u); - car_u = _auto_link(u3_xmas_io_init(pir_u), pir_u, car_u); + car_u = _auto_link(u3_mesa_io_init(pir_u), pir_u, car_u); return car_u; } diff --git a/pkg/vere/io/blake.c b/pkg/vere/io/blake.c index 13b8f41c0f..2880f21110 100644 --- a/pkg/vere/io/blake.c +++ b/pkg/vere/io/blake.c @@ -33,7 +33,7 @@ _log_buf(c3_c* str_c, c3_y* buf_y, c3_w len_w) c3_w cur_w = 0; c3_c tmp_c[3]; for(c3_w idx_w = 0; idx_w < len_w; idx_w++ ) { - snprintf(res_c + (2*idx_w), siz_w - (2*idx_w), "%02x", buf_y[idx_w]); + snprintf(res_c + (2*idx_w), siz_w - (2*idx_w), "%02x", buf_y[idx_w]); } fprintf(stderr, "%s: %s", str_c, res_c); fprintf(stderr, "\r\n"); @@ -182,7 +182,7 @@ vec_copy(u3_raw_vec* dst_u, u3_raw_vec* src_u) { } void* -vec_pop(u3_raw_vec* vec_u, c3_w idx_w) +vec_pop(u3_raw_vec* vec_u, c3_w idx_w) { if ( vec_u->len_w == 0 || idx_w >= vec_u->len_w ) { fprintf(stderr, "Failed pop (len: %u, idx: %u)\r\n", vec_u->len_w, idx_w); @@ -197,13 +197,13 @@ vec_pop(u3_raw_vec* vec_u, c3_w idx_w) } void* -vec_popf(u3_raw_vec* vec_u) +vec_popf(u3_raw_vec* vec_u) { return vec_pop(vec_u, 0); } static void* -_vec_popl(u3_raw_vec* vec_u) +_vec_popl(u3_raw_vec* vec_u) { return vec_pop(vec_u, vec_u->len_w - 1); } @@ -277,12 +277,12 @@ static c3_y _count_trail_zero(c3_d val_d) return 64; } c3_y ret_y = 0; - if ((val_d & 0x00000000FFFFFFFFULL) == 0) { ret_y += 32; val_d >>= 32; } - if ((val_d & 0x000000000000FFFFULL) == 0) { ret_y += 16; val_d >>= 16; } - if ((val_d & 0x00000000000000FFULL) == 0) { ret_y += 8; val_d >>= 8; } - if ((val_d & 0x000000000000000FULL) == 0) { ret_y += 4; val_d >>= 4; } - if ((val_d & 0x0000000000000003ULL) == 0) { ret_y += 2; val_d >>= 2; } - if ((val_d & 0x0000000000000001ULL) == 0) { ret_y += 1; val_d >>= 1; } + if ((val_d & 0x00000000FFFFFFFFULL) == 0) { ret_y += 32; val_d >>= 32; } + if ((val_d & 0x000000000000FFFFULL) == 0) { ret_y += 16; val_d >>= 16; } + if ((val_d & 0x00000000000000FFULL) == 0) { ret_y += 8; val_d >>= 8; } + if ((val_d & 0x000000000000000FULL) == 0) { ret_y += 4; val_d >>= 4; } + if ((val_d & 0x0000000000000003ULL) == 0) { ret_y += 2; val_d >>= 2; } + if ((val_d & 0x0000000000000001ULL) == 0) { ret_y += 1; val_d >>= 1; } return ret_y; } @@ -299,7 +299,7 @@ _height(blake_subtree* sub_u) } static c3_d -_largest_pow2(c3_d val_d) +_largest_pow2(c3_d val_d) { c3_d ret_d = 1 << (_bitlen(val_d-1) - 1); return ret_d; @@ -389,7 +389,7 @@ _parent_hash(blake_node* sin_u, blake_node* dex_u) -static void +static void _root_hash(c3_y out_y[64], blake_node* nod_u) { nod_u->fag_y |= ROOT; @@ -440,7 +440,7 @@ static void recurse_blake_subtree(blake_subtree* sub_u, blake_node* nod_u, u3_ve } static void -_bao_build(c3_d num_d, c3_y has_y[64], u3_vec(c3_w[8])* pof_u, u3_vec(pair)* par_u) +_bao_build(c3_d num_d, c3_y has_y[64], u3_vec(c3_w[8])* pof_u, u3_vec(pair)* par_u) { blake_subtree* sub_u = c3_calloc(sizeof(blake_subtree)); *sub_u = (blake_subtree){0, num_d}; @@ -464,7 +464,7 @@ _bao_build(c3_d num_d, c3_y has_y[64], u3_vec(c3_w[8])* pof_u, u3_vec(pair)* par } static void -_push_leaf(blake_bao* bao_u, c3_y lef_y[BLAKE3_OUT_LEN]) +_push_leaf(blake_bao* bao_u, c3_y lef_y[BLAKE3_OUT_LEN]) { vec_append(&bao_u->que_u, lef_y); } @@ -492,7 +492,7 @@ _pop_parent(c3_y out_y[BLAKE3_OUT_LEN], blake_bao* bao_u) } static void -_verifier_next(blake_bao* bao_u) +_verifier_next(blake_bao* bao_u) { if ( _height(&bao_u->sub_u) == 0 ) { bao_u->sub_u.sin_d += 1; @@ -573,7 +573,7 @@ blake_bao_make(c3_w num_w, u3_vec(c3_y[BLAKE3_OUT_LEN])* pof_u) if ( vec_len(pof_u) == 0 ) { if ( num_w < 2 ) { c3_free(bao_u); - return NULL; + return NULL; } vec_init(&bao_u->sta_u, 1); vec_init(&bao_u->que_u, 1); @@ -641,7 +641,7 @@ blake_bao_verify(blake_bao* bao_u, c3_y* dat_y, c3_w dat_w, blake_pair* par_u) } return BAO_GOOD; } - + #ifdef BLAKE_TEST @@ -679,7 +679,7 @@ static void _test_lead_zeros() } -static void _test_bao() +static void _test_bao() { c3_w dat_w = 1024; c3_y* dat_y = c3_calloc(dat_w); @@ -688,7 +688,7 @@ static void _test_bao() u3_vec(c3_y[8])* pof_u = vec_make(10); u3_vec(blake_pair)* par_u = vec_make(10); _bao_build(num_w, has_y, pof_u, par_u); - + blake_bao bao_u; memset(&bao_u, 0, sizeof(blake_bao)); _veri_init(&bao_u, num_w); @@ -813,7 +813,7 @@ int main() { //_test_root_hash(); // _test_lead_zeros(); _test_bao(); - + return 0; } diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 5786e69f8a..36c7d29586 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1059,10 +1059,8 @@ _mesa_rout_bufs(u3_mesa* sam_u, c3_y* buf_y, c3_w len_w, u3_noun las) { c3_o suc_o = c3n; u3_noun lan, t = las; - u3m_p("las", las); u3m_p("t", t); // u3l_log("sending to ip: %x, port: %u", lan_u.pip_w, lan_u.por_s); while ( t != u3_nul ) { - u3m_p("t", t); u3x_cell(t, &lan, &t); u3_lane lan_u = _realise_lane(u3k(lan)); u3l_log("sending to ip: %x, port: %u", lan_u.pip_w, lan_u.por_s); @@ -1079,7 +1077,6 @@ _mesa_rout_bufs(u3_mesa* sam_u, c3_y* buf_y, c3_w len_w, u3_noun las) _mesa_send_buf(sam_u, lan_u, sen_y, len_w); } } - u3m_p("t", t); // u3z(las); return suc_o; } @@ -1154,7 +1151,6 @@ _mesa_czar_cb(uv_getaddrinfo_t* adr_u, c3_i sas_i, struct addrinfo* aif_u) static void _mesa_resolve_czar(u3_mesa* sam_u, c3_y imp_y, u3_noun pac) { - u3m_p("imp", imp_y); u3_assert( c3y == u3_Host.ops_u.net ); u3_czar_info* imp_u = &sam_u->imp_u[imp_y]; time_t now_t = time(0); @@ -1201,7 +1197,6 @@ _mesa_queue_czar(u3_mesa* sam_u, u3_noun las, u3_noun pac) u3_noun lan, t = las; u3_noun res = u3_nul; time_t now_t = time(0); - u3m_p("_mesa_queue_czar", las); while ( t != u3_nul ) { u3x_cell(t, &lan, &t); if ( (c3y == u3a_is_cat(lan) && lan < 256 ) ) { @@ -1217,7 +1212,6 @@ _mesa_queue_czar(u3_mesa* sam_u, u3_noun las, u3_noun pac) res = u3nc(u3k(lan), res); } } - u3m_p("_mesa_queue_czar res", res); u3z(las); u3z(pac); return res; @@ -1732,7 +1726,7 @@ static void _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) { #ifdef MESA_DEBUG - // u3l_log("mesa hear page %u", pic_u->pac_u.pag_u.nam_u.fra_w); + u3l_log("mesa hear page %u", pic_u->pac_u.pag_u.nam_u.fra_w); #endif u3_mesa* sam_u = pic_u->sam_u; u3_mesa_pact* pac_u = &pic_u->pac_u; @@ -1861,7 +1855,6 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) memcpy(her_d, pac_u->pek_u.nam_u.her_d, 2); c3_o our_o = __( 0 == memcmp(her_d, sam_u->pir_u->who_d, 2) ); - if ( c3n == our_o ) { u3_peer* per_u = _mesa_get_peer(sam_u, her_d); if ( per_u == NULL ) { @@ -1906,7 +1899,7 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) u3_noun our = u3i_chubs(2, sam_u->car_u.pir_u->who_d); u3_noun bem = u3nc(u3nt(our, u3_nul, u3nc(c3__ud, 1)), sky); // only branch where we do not free pic_u - u3_pier_peek(sam_u->car_u.pir_u, u3_nul, u3k(u3nq(1, c3__beam, c3__xx, bem)), pic_u, _mesa_page_scry_cb); + u3_pier_peek(sam_u->car_u.pir_u, u3_nul, u3k(u3nq(1, c3__beam, c3__mx, bem)), pic_u, _mesa_page_scry_cb); } else { _mesa_free_pict(pic_u); } diff --git a/pkg/vere/xmas_tests.c b/pkg/vere/xmas_tests.c index e799b9f55e..58417b9c1e 100644 --- a/pkg/vere/xmas_tests.c +++ b/pkg/vere/xmas_tests.c @@ -38,7 +38,7 @@ _test_xmas(void) .pat_s = 8, .boq_s = 13, .fra_s = 1, - }; + }; } /* main(): run all test cases. From 04ffdae8ed7a34af957c9a71aa9fc4869c0c8b28 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Wed, 13 Mar 2024 15:02:25 +0100 Subject: [PATCH 065/430] mesa: inject %page response --- pkg/vere/io/mesa.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 36c7d29586..656e9df672 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1706,6 +1706,7 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) req_u->bao_u = blake_bao_make(req_u->tot_w, pof_u); blake_bao_verify(req_u->bao_u, dat_u->fra_y, dat_u->len_w, NULL); + memcpy(req_u->dat_y, dat_u->fra_y, dat_u->len_w); } else { c3_w len_w = dat_u->len_w / BLAKE3_OUT_LEN; u3_vec(c3_y[BLAKE3_OUT_LEN])* pof_u = vec_make(len_w); @@ -1823,8 +1824,17 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) c3_d now_d = _get_now_micros(); u3l_log("%u kilobytes took %f ms", req_u->tot_w, (now_d - sam_u->tim_d)/1000.0); c3_w siz_w = (1 << (pac_u->pag_u.nam_u.boq_y - 3)); - //u3_noun dat = u3i_bytes((siz_w * req_u->tot_w), req_u->dat_y); + u3_noun dat = u3i_bytes((siz_w * req_u->tot_w), req_u->dat_y); _mesa_del_request(sam_u, &pac_u->pag_u.nam_u); + + u3_noun pax = _mesa_encode_path(pac_u->pag_u.nam_u.pat_s, + (c3_y*)(pac_u->pag_u.nam_u.pat_c)); + u3_noun spar = u3nc(u3i_chubs(2, pac_u->pag_u.nam_u.her_d), pax); + // inject $page = [%page =spar auth =gage] + u3_noun cad = u3nt(c3__mess, u3_nul, // XX lane=(unit) + u3nq(c3__page, spar, u3nc(c3y, 0), dat)); + u3_ovum *ovo_u = u3_ovum_init(0, c3__mesa, wir, cad); + u3_auto_plan(&sam_u->car_u, ovo_u); } _mesa_free_pict(pic_u); } From 61e29e8a0fcc3d95dec81f893fedb4cd15879036 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Wed, 13 Mar 2024 16:00:54 -0400 Subject: [PATCH 066/430] mesa: initialize hamt for packet cache, note todos --- pkg/vere/io/ames.c | 2 +- pkg/vere/io/mesa.c | 22 ++++++++++++---------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index 7f0de222e4..90b24781ad 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -2836,7 +2836,7 @@ _ames_io_start(u3_ames* sam_u) htonl(INADDR_ANY) : htonl(INADDR_LOOPBACK); add_u.sin_port = htons(por_s); - u3l_log("ames: skippign port: %u", por_s); + u3l_log("ames: skipping port: %u", por_s); /*if ( (ret_i = uv_udp_bind(&sam_u->wax_u, (const struct sockaddr*)&add_u, 0)) != 0 ) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 656e9df672..118576980a 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -265,7 +265,7 @@ _log_czar_info(u3_czar_info* zar_u) u3l_log("czar: %s", u3r_string(nam)); u3_noun pip = u3dc("scot", c3__if, zar_u->pip_w); u3l_log("IP: %s", u3r_string(pip)); - u3l_log("time: %u", zar_u->tim_t); + u3l_log("time: %" PRIu64, (c3_d)zar_u->tim_t); u3l_log("dns: %s", zar_u->dns_c != NULL ? zar_u->dns_c : "NO DNS"); u3l_log("pending: %u", u3r_word(0, u3do("lent", zar_u->pen))); } @@ -1600,7 +1600,7 @@ _mesa_forward(u3_mesa_pict* pic_u, u3_lane lan_u) u3_mesa_pact* pac_u = &pic_u->pac_u; u3_mesa* sam_u = pic_u->sam_u; c3_d her_d[2]; - _get_her(pac_u, her_d); + _get_her(pac_u, her_d); // XX wrong // XX: revive //_update_hopcount(&pac_u->hed_u); @@ -1731,7 +1731,6 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) #endif u3_mesa* sam_u = pic_u->sam_u; u3_mesa_pact* pac_u = &pic_u->pac_u; - u3_noun wir = u3nc(c3__mesa, u3_nul); c3_s fra_s; u3_peer* per_u = _mesa_get_peer(sam_u, pac_u->pag_u.nam_u.her_d); @@ -1747,7 +1746,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) if ( pac_u->hed_u.hop_y == 0 ) { _hear_peer(sam_u, per_u, lan_u, dir_o); } else { - u3l_log("received forwarded peek"); + u3l_log("received forwarded page"); } if ( new_o == c3y ) { //u3l_log("new lane is direct %c", c3y == dir_o ? 'y' : 'n'); @@ -1831,6 +1830,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) (c3_y*)(pac_u->pag_u.nam_u.pat_c)); u3_noun spar = u3nc(u3i_chubs(2, pac_u->pag_u.nam_u.her_d), pax); // inject $page = [%page =spar auth =gage] + u3_noun wir = u3nc(c3__mesa, u3_nul); u3_noun cad = u3nt(c3__mess, u3_nul, // XX lane=(unit) u3nq(c3__page, spar, u3nc(c3y, 0), dat)); u3_ovum *ovo_u = u3_ovum_init(0, c3__mesa, wir, cad); @@ -1859,15 +1859,14 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) #endif u3_mesa_pact* pac_u = &pic_u->pac_u; u3_mesa* sam_u = pic_u->sam_u; - c3_d her_d[2]; - her_d[0] = 0; - her_d[1] = 0; - memcpy(her_d, pac_u->pek_u.nam_u.her_d, 2); - c3_o our_o = __( 0 == memcmp(her_d, sam_u->pir_u->who_d, 2) ); + c3_d* her_d = pac_u->pek_u.nam_u.her_d; + c3_o our_o = __( 0 == memcmp(her_d, sam_u->pir_u->who_d, sizeof(*her_d) * 2) ); + // XX forwarding wrong, need a PIT entry if ( c3n == our_o ) { u3_peer* per_u = _mesa_get_peer(sam_u, her_d); if ( per_u == NULL ) { + // XX leaks u3l_log("mesa: alien forward for %s", u3r_string(u3dc("scot", c3__p, u3i_chubs(2, her_d)))); _mesa_free_pict(pic_u); return; @@ -1891,7 +1890,7 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) u3x_cell(u3k(hit), &tag, &dat); if ( tag == MESA_WAIT ) { _mesa_add_lane_to_cache(sam_u, &pac_u->pek_u.nam_u, u3k(u3t(dat)), lan_u); - } else if ( c3y == our_o && tag == MESA_ITEM ) { + } else if ( c3y == our_o && tag == MESA_ITEM ) { // XX our_o redundant c3_y* buf_y; c3_w len_w = _mesa_respond(pic_u, &buf_y, u3k(dat)); _mesa_send_buf(sam_u, lan_u, buf_y, len_w); @@ -1911,6 +1910,7 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) // only branch where we do not free pic_u u3_pier_peek(sam_u->car_u.pir_u, u3_nul, u3k(u3nq(1, c3__beam, c3__mx, bem)), pic_u, _mesa_page_scry_cb); } else { + // XX unpossible _mesa_free_pict(pic_u); } } @@ -2111,9 +2111,11 @@ u3_mesa_io_init(u3_pier* pir_u) u3_mesa* sam_u = c3_calloc(sizeof(*sam_u)); sam_u->pir_u = pir_u; + // XX config sam_u->her_p = u3h_new_cache(100000); sam_u->req_p = u3h_new_cache(100000); sam_u->lan_p = u3h_new_cache(100000); + sam_u->pac_p = u3h_new_cache(100000); u3_assert( !uv_udp_init(u3L, &sam_u->wax_u) ); sam_u->wax_u.data = sam_u; From 3428606e771c3926a7e8bd64cb844f6cd735113a Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Wed, 13 Mar 2024 20:37:22 -0400 Subject: [PATCH 067/430] mesa: only attempt to handle our own effects --- pkg/vere/io/mesa.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 118576980a..67ecd84fe9 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1264,13 +1264,16 @@ static c3_o _mesa_io_kick(u3_auto* car_u, u3_noun wir, u3_noun cad) { u3_mesa* sam_u = (u3_mesa*)car_u; - u3_noun tag, dat; + u3_noun tag, dat, i_wir; c3_o ret_o; - if( c3n == u3r_cell(cad, &tag, &dat) ) + if ( (c3n == u3r_cell(wir, &i_wir, 0)) + || (c3__mesa != i_wir) + || (c3n == u3r_cell(cad, &tag, &dat)) ) { ret_o = c3n; - } else { + } + else { ret_o = _mesa_kick(sam_u, u3k(tag), u3k(dat)); } From ed9cb83065aab77914530452b8140dd2e1014a92 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Wed, 13 Mar 2024 22:19:45 -0400 Subject: [PATCH 068/430] mesa: initial, untested %poke handling --- pkg/vere/io/mesa.c | 89 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 88 insertions(+), 1 deletion(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 67ecd84fe9..8b9f1ea675 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1920,13 +1920,62 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) // u3z(pax); } +static void +_mesa_poke_news(u3_ovum* egg_u, u3_ovum_news new_e) +{ + u3_mesa_pict* pic_u = egg_u->ptr_v; + + if ( u3_ovum_done == new_e ) { + // XX success stuff here + u3l_log("mesa: poke success"); + } +} + +static void +_mesa_poke_bail(u3_ovum* egg_u, u3_noun lud) +{ + u3_mesa_pict* pic_u = egg_u->ptr_v; + // XX failure stuff here + u3l_log("mesa: poke failure"); +} + // xx: should inject event directly, but vane does not work // so we just hack it to get static void _mesa_hear_poke(u3_mesa_pict* pic_u, u3_lane* lan_u) { +#ifdef MESA_DEBUG + u3l_log("mesa: hear peek"); + // u3_assert(pac_u->hed_u.typ_y == PACT_POKE); +#endif u3_mesa_pact* pac_u = &pic_u->pac_u; u3_mesa* sam_u = pic_u->sam_u; + c3_d* her_d = pac_u->pek_u.nam_u.her_d; + c3_o our_o = __( 0 == memcmp(her_d, sam_u->pir_u->who_d, sizeof(*her_d) * 2) ); + + // XX forwarding wrong, need a PIT entry + if ( c3n == our_o ) { + u3_peer* per_u = _mesa_get_peer(sam_u, her_d); + if ( per_u == NULL ) { + // XX leaks + u3l_log("mesa: alien forward for %s", u3r_string(u3dc("scot", c3__p, u3i_chubs(2, her_d)))); + _mesa_free_pict(pic_u); + return; + } + if ( c3y == sam_u->for_o && sam_u->pir_u->who_d[0] == per_u->imp_s ) { +//#ifdef MESA_DEBUG + u3_lane lin_u = _mesa_get_direct_lane(sam_u, her_d); +//#endif + //_update_hopcount(&pac_u->hed_u); + u3l_log("sending peek %u", pac_u->pek_u.nam_u.fra_w); + _mesa_send(pic_u, &lin_u); + } + _mesa_free_pict(pic_u); + return; + } + + // XX if this lane management stuff is necessary + // it should be deferred to after successful event processing u3_peer* per_u = _mesa_get_peer(sam_u, pac_u->pok_u.pay_u.her_d); c3_o new_o = c3n; if ( NULL == per_u ) { @@ -1942,13 +1991,51 @@ _mesa_hear_poke(u3_mesa_pict* pic_u, u3_lane* lan_u) _hear_peer(sam_u, per_u, *lan_u, dir_o); u3l_log("learnt lane"); } else { - u3l_log("received forwarded peek"); + u3l_log("received forwarded poke"); } if ( new_o == c3y ) { u3l_log("new lane is direct %c", c3y == dir_o ? 'y' : 'n'); _log_lane(lan_u); } _mesa_put_peer(sam_u, pac_u->pok_u.pay_u.her_d, per_u); + + // XX could check cache for ack (completed duplicate) + + u3_ovum_peer nes_f; + u3_ovum_bail bal_f; + void* ptr_v; + + if ( 1 == pac_u->pok_u.dat_u.tot_w ) { + nes_f = bal_f = ptr_v = NULL; + _mesa_free_pict(pic_u); + } + else { + assert(pac_u->pok_u.dat_u.tot_w); + // XX check request state for *payload* (in-progress duplicate) + nes_f = _mesa_poke_news; + bal_f = _mesa_poke_bail; + ptr_v = pic_u; + } + + // XX create PIT entry for ack + + u3_noun wir = u3nc(c3__mesa, u3_nul); + u3_noun cad; + { + u3_noun lan = u3_mesa_encode_lane(*lan_u); + u3i_slab sab_u; + u3i_slab_init(&sab_u, 3, PACT_SIZE); + + // XX should just preserve input buffer + mesa_etch_pact(sab_u.buf_y, pac_u); + + cad = u3nt(c3__heer, lan, u3i_slab_mint(&sab_u)); + } + + u3_auto_peer( + u3_auto_plan(&sam_u->car_u, + u3_ovum_init(0, c3__mesa, wir, cad)), + ptr_v, nes_f, bal_f); } static void From 99e19c61b6bd53371381dd19b56c0028225f3632 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Wed, 13 Mar 2024 22:39:51 -0400 Subject: [PATCH 069/430] mesa: adds support for single-fragment %page messages --- pkg/vere/io/mesa.c | 62 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 54 insertions(+), 8 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 8b9f1ea675..706f882a9f 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1768,6 +1768,45 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) return; } + if ( 1 == pac_u->pag_u.dat_u.tot_w ) { + u3_noun wir = u3nc(c3__mesa, u3_nul); + u3_noun aut, cad; + + switch ( pac_u->pag_u.dat_u.aum_u.typ_e ) { + case AUTH_SIGN: { + aut = u3nc(c3y, u3i_bytes(64, pac_u->pag_u.dat_u.aum_u.sig_y)); + } break; + + case AUTH_HMAC: { + aut = u3nc(c3n, u3i_bytes(32, pac_u->pag_u.dat_u.aum_u.mac_y)); + } break; + + default: { + u3l_log("page: strange auth"); + _mesa_free_pict(pic_u); + return; + } + } + + { + u3_noun pax = _mesa_encode_path(pac_u->pag_u.nam_u.pat_s, + (c3_y*)(pac_u->pag_u.nam_u.pat_c)); + u3_noun par = u3nc(u3i_chubs(2, pac_u->pag_u.nam_u.her_d), pax); + u3_noun lan = u3nc(u3_nul, u3_mesa_encode_lane(lan_u)); + u3_noun dat = u3i_bytes(pac_u->pag_u.dat_u.len_w, + pac_u->pag_u.dat_u.fra_y); + + cad = u3nt(c3__mess, lan, + u3nq(c3__page, par, aut, dat)); + } + + // XX should put in cache on success + u3_auto_plan(&sam_u->car_u, + u3_ovum_init(0, c3__mesa, wir, cad)); + _mesa_free_pict(pic_u); + return; + } + // u3_noun fra = u3i_bytes(pac_u->pag_u.dat_u.len_w, pac_u->pag_u.dat_u.fra_y) ; /*if ( dop_o == c3n && pac_u->pag_u.nam_u.fra_w == 150) { dop_o = c3y; @@ -1829,16 +1868,23 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) u3_noun dat = u3i_bytes((siz_w * req_u->tot_w), req_u->dat_y); _mesa_del_request(sam_u, &pac_u->pag_u.nam_u); - u3_noun pax = _mesa_encode_path(pac_u->pag_u.nam_u.pat_s, - (c3_y*)(pac_u->pag_u.nam_u.pat_c)); - u3_noun spar = u3nc(u3i_chubs(2, pac_u->pag_u.nam_u.her_d), pax); - // inject $page = [%page =spar auth =gage] u3_noun wir = u3nc(c3__mesa, u3_nul); - u3_noun cad = u3nt(c3__mess, u3_nul, // XX lane=(unit) - u3nq(c3__page, spar, u3nc(c3y, 0), dat)); - u3_ovum *ovo_u = u3_ovum_init(0, c3__mesa, wir, cad); - u3_auto_plan(&sam_u->car_u, ovo_u); + u3_noun cad; + { + u3_noun pax = _mesa_encode_path(pac_u->pag_u.nam_u.pat_s, + (c3_y*)(pac_u->pag_u.nam_u.pat_c)); + u3_noun par = u3nc(u3i_chubs(2, pac_u->pag_u.nam_u.her_d), pax); + u3_noun lan = u3_nul; + u3_noun aut = u3nc(c3y, 0); // XX s/b saved in request state + + cad = u3nt(c3__mess, lan, + u3nq(c3__page, par, aut, dat)); + } + + u3_auto_plan(&sam_u->car_u, + u3_ovum_init(0, c3__mesa, wir, cad)); } + _mesa_free_pict(pic_u); } From 108128ac403bd0a9a2ff5a8f44b28912bff157e5 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Thu, 14 Mar 2024 18:42:40 -0400 Subject: [PATCH 070/430] mesa: get all old xmas tests redefined and running again --- pkg/vere/BUILD.bazel | 6 +++--- pkg/vere/io/blake.c | 34 ++++++++++++++++++---------------- pkg/vere/io/mesa/pact.c | 2 +- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/pkg/vere/BUILD.bazel b/pkg/vere/BUILD.bazel index a87045c67e..c3e6aadeca 100644 --- a/pkg/vere/BUILD.bazel +++ b/pkg/vere/BUILD.bazel @@ -214,7 +214,7 @@ cc_test( cc_test( - name = "xmas_tests", + name = "mesa_tests", timeout = "short", srcs = ["io/mesa.c"], features = select({ @@ -222,13 +222,13 @@ cc_test( "//conditions:default": [], }), deps = [":vere"], - defines = ["XMAS_TEST"] + defines = ["MESA_TEST"] ) cc_test( name = "pact_tests", timeout = "short", - srcs = ["io/xmas/pact.c"], + srcs = ["io/mesa/pact.c"], features = select({ "@platforms//os:linux": ["fully_static_link"], "//conditions:default": [], diff --git a/pkg/vere/io/blake.c b/pkg/vere/io/blake.c index 2880f21110..caff2bacd0 100644 --- a/pkg/vere/io/blake.c +++ b/pkg/vere/io/blake.c @@ -53,9 +53,9 @@ _log_words(c3_c* str_c, c3_w* buf_w, c3_w len_w) void log_node(blake_node* nod_u) { - _log_buf("CV", (c3_w*)nod_u->cev_y, BLAKE3_OUT_LEN); - _log_buf("block", (c3_w*)nod_u->boq_y, BLAKE3_BLOCK_LEN); - fprintf(stderr, "counter: %llu\r\n", nod_u->con_d); + _log_buf("CV", nod_u->cev_y, BLAKE3_OUT_LEN); + _log_buf("block", nod_u->boq_y, BLAKE3_BLOCK_LEN); + fprintf(stderr, "counter: %" PRIu64 "\r\n", nod_u->con_d); fprintf(stderr, "length: %u\r\n", nod_u->len_y); fprintf(stderr, "flag: %x\r\n", nod_u->fag_y); } @@ -246,8 +246,8 @@ _log_blake_subtree(blake_subtree* sub_u) void log_bao(blake_bao* bao_u) { - fprintf(stderr, "num: %llu\r\n", bao_u->num_w); - fprintf(stderr, "con: %llu\r\n", bao_u->con_w); + fprintf(stderr, "num: %u\r\n", bao_u->num_w); + fprintf(stderr, "con: %u\r\n", bao_u->con_w); fprintf(stderr, "sub: (%llu, %llu)\r\n", bao_u->sub_u.sin_d, bao_u->sub_u.dex_d); fprintf(stderr, "queue\r\n"); _vec_each(&bao_u->que_u, print_proof); @@ -286,7 +286,7 @@ static c3_y _count_trail_zero(c3_d val_d) return ret_y; } -static c3_d _bitlen(c3_d bit_d) +static c3_y _bitlen(c3_d bit_d) { return 64 - _count_lead_zeros(bit_d); } @@ -647,7 +647,7 @@ blake_bao_verify(blake_bao* bao_u, c3_y* dat_y, c3_w dat_w, blake_pair* par_u) static void _test_lead_zeros() { - #define asrt(x,y) if ( x != y ) { fprintf(stderr, "failed (line: %u) got %llu expected %llu", __LINE__, x,y); exit(1); } + #define asrt(x,y) if ( x != y ) { fprintf(stderr, "failed (line: %u) got %u expected %u", __LINE__, x,y); exit(1); } // Test with 0, should be 64 leading zeros asrt(_count_lead_zeros(0ULL), 64); @@ -679,6 +679,7 @@ static void _test_lead_zeros() } +#if 0 static void _test_bao() { c3_w dat_w = 1024; @@ -710,7 +711,7 @@ static void _test_bao() continue; } for ( c3_w i_w = 0; i_w < num_w; i_w++ ) { - if ( c3n == _veri_check_leaf(&bao_u, i_w, dat_y, dat_w) ) { + if ( c3n == _veri_check_leaf(&bao_u, dat_y, dat_w) ) { fprintf(stderr, "Check leaf %u failed for %u\r\n", i_w, num_w); exit(1); } @@ -737,6 +738,7 @@ static void _test_bao() } } } +#endif static void print_int(void* vod_p, c3_w i_w) { c3_w* int_w = (c3_w*)vod_p; @@ -757,7 +759,7 @@ static void _test_vec() { vec_append(vec_u, &thd); fprintf(stderr, "put three\r\n"); - c3_w* one_w = (vec_u); + c3_w* one_w = vec_popf(vec_u); if ( *one_w != 1 ) { fprintf(stderr, "(one) Vec failure\r\n"); @@ -765,7 +767,7 @@ static void _test_vec() { } fprintf(stderr, "popped %u", *one_w); - c3_w* two_w = (vec_u); + c3_w* two_w = vec_popf(vec_u); fprintf(stderr, "two pointer %p, %p\r\n", &snd, two_w); if ( *two_w != 2 ) { fprintf(stderr, "(two) Vec failure\r\n"); @@ -773,7 +775,7 @@ static void _test_vec() { } fprintf(stderr, "popped %u", *two_w); - c3_w* thr_w = (vec_u); + c3_w* thr_w = vec_popf(vec_u); if ( *thr_w != 3 ) { fprintf(stderr, "(three) Vec failure\r\n"); exit(1); @@ -804,15 +806,15 @@ static void _test_vec() { - _vec_free(vec_u); + vec_free(vec_u); c3_free(vec_u); } int main() { - //_test_vec(); - //_test_root_hash(); - // _test_lead_zeros(); - _test_bao(); + _test_vec(); + // _test_root_hash(); + _test_lead_zeros(); + // _test_bao(); return 0; } diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index 04ac1b9fa9..d5ebe3889c 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -150,7 +150,7 @@ _log_poke_pact(u3_mesa_poke_pact *pac_u) } void -log_pact(u3_mesa_pact* pac_u) +_log_pact(u3_mesa_pact* pac_u) { switch ( pac_u->hed_u.typ_y ) { case PACT_PEEK: { From 6bfa04fc6c101fb4d62ce367c4865c065abcb37b Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Thu, 14 Mar 2024 18:50:40 -0400 Subject: [PATCH 071/430] mesa: calculate mugs while etching packets --- pkg/vere/io/mesa/mesa.h | 2 +- pkg/vere/io/mesa/pact.c | 15 +++++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/pkg/vere/io/mesa/mesa.h b/pkg/vere/io/mesa/mesa.h index fd4b8860c9..64e8d4dd49 100644 --- a/pkg/vere/io/mesa/mesa.h +++ b/pkg/vere/io/mesa/mesa.h @@ -96,7 +96,7 @@ typedef struct _u3_mesa_head { c3_y pro_y; // protocol version u3_mesa_ptag typ_y; // packet type c3_y hop_y; // hopcount - c3_w mug_w; // mug checksum + c3_w mug_w; // truncated mug checksum } u3_mesa_head; // diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index d5ebe3889c..857cb7833a 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -24,7 +24,7 @@ _log_head(u3_mesa_head* hed_u) u3l_log("next hop: %u", hed_u->nex_y); u3l_log("protocol: %u", hed_u->pro_y); u3l_log("packet type: %u", hed_u->typ_y); - u3l_log("mug: 0x%05x", (hed_u->mug_w & 0xfffff)); + u3l_log("mug: 0x%05x", (hed_u->mug_w & 0xFFFFF)); u3l_log("hopcount: %u", hed_u->hop_y); u3l_log(""); } @@ -307,7 +307,7 @@ _mesa_sift_head(c3_y buf_y[8], u3_mesa_head* hed_u) hed_u->pro_y = (hed_w >> 4) & 0x7; hed_u->typ_y = (hed_w >> 7) & 0x3; hed_u->hop_y = (hed_w >> 9) & 0x7; - hed_u->mug_w = (hed_w >> 12) & 0xFFFFFF; + hed_u->mug_w = (hed_w >> 12) & 0xFFFFF; assert( 1 == hed_u->pro_y ); @@ -607,7 +607,7 @@ _mesa_etch_head(u3_mesa_head* hed_u, c3_y buf_y[8]) ^ (hed_u->pro_y & 0x7) << 4 // XX constant, 1 ^ (hed_u->typ_y & 0x3) << 7 ^ (hed_u->hop_y & 0x7) << 9 - ^ (hed_u->mug_w & 0xFFFFFF) << 12; + ^ (hed_u->mug_w & 0xFFFFF) << 12; // XX: we don't expand hopcount if no request. Correct? // /*if ( c3y == req_o ) { @@ -898,10 +898,9 @@ mesa_etch_pact(c3_y* buf_y, u3_mesa_pact* pac_u) return 0; } - c3_w cur_w = 0, nex_w; u3_mesa_head* hed_u = &pac_u->hed_u; - _mesa_etch_head(hed_u, buf_y + cur_w); - cur_w += 8; + + c3_w nex_w, cur_w = 8; // space for header + cookie switch ( pac_u->hed_u.typ_y ) { case PACT_POKE: { @@ -928,6 +927,10 @@ mesa_etch_pact(c3_y* buf_y, u3_mesa_pact* pac_u) } } + hed_u->mug_w = u3r_mug_bytes(buf_y + cur_w, nex_w); + hed_u->mug_w &= 0xFFFFF; + _mesa_etch_head(hed_u, buf_y); + cur_w += nex_w; assert( siz_w == cur_w ); From cba8fa806816093353c4280bff4c9d874e3e606b Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Thu, 14 Mar 2024 18:53:30 -0400 Subject: [PATCH 072/430] mesa: validate mugs while sifting packets --- pkg/vere/io/mesa/pact.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index 857cb7833a..aa464f5bb4 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -588,6 +588,17 @@ mesa_sift_pact(u3_mesa_pact* pac_u, c3_y* buf_y, c3_w len_w) break; } } + + { + c3_w mug_w = u3r_mug_bytes(buf_y, res_w); + mug_w &= 0xFFFFF; + + if ( mug_w != pac_u->hed_u.mug_w ) { + u3l_log("mesa: failed mug"); + return 0; + } + } + //u3_assert(res_w <= len_w ); return res_w + 8; } From fc3d5b4b1741cf071a80f2fd0445943bd357896d Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Thu, 14 Mar 2024 18:53:44 -0400 Subject: [PATCH 073/430] mesa: correct debug printf --- pkg/vere/io/mesa.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 706f882a9f..e130f98f17 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1991,7 +1991,7 @@ static void _mesa_hear_poke(u3_mesa_pict* pic_u, u3_lane* lan_u) { #ifdef MESA_DEBUG - u3l_log("mesa: hear peek"); + u3l_log("mesa: hear poke"); // u3_assert(pac_u->hed_u.typ_y == PACT_POKE); #endif u3_mesa_pact* pac_u = &pic_u->pac_u; From 315b4f5249326d78e53a8d4a435ee46aaf3c569c Mon Sep 17 00:00:00 2001 From: Sigilante Date: Wed, 27 Mar 2024 10:18:41 -0500 Subject: [PATCH 074/430] Post fixed scalar jets using ?scal. --- pkg/noun/jets/f/lagoon.c | 1257 +++++++++++++++++++++++++++++++------- pkg/noun/jets/q.h | 6 + pkg/noun/jets/tree.c | 12 + pkg/noun/jets/w.h | 6 + 4 files changed, 1051 insertions(+), 230 deletions(-) diff --git a/pkg/noun/jets/f/lagoon.c b/pkg/noun/jets/f/lagoon.c index 07e0e5dd13..ba55c5b4db 100644 --- a/pkg/noun/jets/f/lagoon.c +++ b/pkg/noun/jets/f/lagoon.c @@ -8,6 +8,7 @@ #include "softblas.h" #include +#include // for pow() #include union half { @@ -94,11 +95,11 @@ { // Unpack the data as a byte array. We assume total length < 2**64. uint64_t len_a = _get_length(shape); - uint64_t siz_a = len_a * bloq; + uint64_t siz_a = len_a * pow(2, bloq - 3); uint8_t* x_bytes = (uint8_t*)u3a_malloc(siz_a*sizeof(uint8_t)); u3r_bytes(0, siz_a, x_bytes, x_data); - uint8_t* y_bytes = (uint8_t*)u3a_malloc(siz_a*sizeof(uint8_t)); - u3r_bytes(0, siz_a, y_bytes, y_data); + uint8_t* y_bytes = (uint8_t*)u3a_malloc((siz_a+1)*sizeof(uint8_t)); + u3r_bytes(0, siz_a+1, y_bytes, y_data); u3_noun r_data; @@ -108,7 +109,7 @@ haxpy(len_a, (float16_t){SB_REAL16_ONE}, (float16_t*)x_bytes, 1, (float16_t*)y_bytes, 1); // Unpack the result back into a noun. - r_data = u3i_bytes(siz_a*sizeof(uint8_t), y_bytes); + r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); // Clean up. u3a_free(x_bytes); @@ -120,7 +121,7 @@ saxpy(len_a, (float32_t){SB_REAL32_ONE}, (float32_t*)x_bytes, 1, (float32_t*)y_bytes, 1); // Unpack the result back into a noun. - r_data = u3i_bytes(siz_a*sizeof(uint8_t), y_bytes); + r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); // Clean up. u3a_free(x_bytes); @@ -132,7 +133,7 @@ daxpy(len_a, (float64_t){SB_REAL64_ONE}, (float64_t*)x_bytes, 1, (float64_t*)y_bytes, 1); // Unpack the result back into a noun. - r_data = u3i_bytes(siz_a*sizeof(uint8_t), y_bytes); + r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); // Clean up. u3a_free(x_bytes); @@ -144,7 +145,7 @@ qaxpy(len_a, (float128_t){SB_REAL128L_ONE,SB_REAL128U_ONE}, (float128_t*)x_bytes, 1, (float128_t*)y_bytes, 1); // Unpack the result back into a noun. - r_data = u3i_bytes(siz_a*sizeof(uint8_t), y_bytes); + r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); // Clean up. u3a_free(x_bytes); @@ -170,11 +171,11 @@ { // Unpack the data as a byte array. We assume total length < 2**64. uint64_t len_a = _get_length(shape); - uint64_t siz_a = len_a * bloq; + uint64_t siz_a = len_a * pow(2, bloq - 3); uint8_t* x_bytes = (uint8_t*)u3a_malloc(siz_a*sizeof(uint8_t)); - u3r_bytes(0, siz_a, x_bytes, y_data); - uint8_t* y_bytes = (uint8_t*)u3a_malloc(siz_a*sizeof(uint8_t)); - u3r_bytes(0, siz_a, y_bytes, x_data); + u3r_bytes(0, siz_a, x_bytes, y_data); // XXX + uint8_t* y_bytes = (uint8_t*)u3a_malloc((siz_a+1)*sizeof(uint8_t)); + u3r_bytes(0, siz_a+1, y_bytes, x_data); // XXX u3_noun r_data; @@ -184,7 +185,7 @@ haxpy(len_a, (float16_t){SB_REAL16_NEGONE}, (float16_t*)x_bytes, 1, (float16_t*)y_bytes, 1); // Unpack the result back into a noun. - r_data = u3i_bytes(siz_a*sizeof(uint8_t), y_bytes); + r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); // Clean up. u3a_free(x_bytes); @@ -196,7 +197,7 @@ saxpy(len_a, (float32_t){SB_REAL32_NEGONE}, (float32_t*)x_bytes, 1, (float32_t*)y_bytes, 1); // Unpack the result back into a noun. - r_data = u3i_bytes(siz_a*sizeof(uint8_t), y_bytes); + r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); // Clean up. u3a_free(x_bytes); @@ -208,7 +209,7 @@ daxpy(len_a, (float64_t){SB_REAL64_NEGONE}, (float64_t*)x_bytes, 1, (float64_t*)y_bytes, 1); // Unpack the result back into a noun. - r_data = u3i_bytes(siz_a*sizeof(uint8_t), y_bytes); + r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); // Clean up. u3a_free(x_bytes); @@ -220,7 +221,7 @@ qaxpy(len_a, (float128_t){SB_REAL128L_NEGONE,SB_REAL128U_NEGONE}, (float128_t*)x_bytes, 1, (float128_t*)y_bytes, 1); // Unpack the result back into a noun. - r_data = u3i_bytes(siz_a*sizeof(uint8_t), y_bytes); + r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); // Clean up. u3a_free(x_bytes); @@ -236,302 +237,1098 @@ } } -/* mmul +/* mul - x.*y + elementwise multiplication */ u3_noun - u3qf_la_mmul_real(u3_noun x_data, - u3_noun y_data, - u3_noun x_shape, - u3_noun y_shape, - u3_noun bloq) + u3qf_la_mul_real(u3_noun x_data, + u3_noun y_data, + u3_noun shape, + u3_noun bloq) { // Unpack the data as a byte array. We assume total length < 2**64. - uint64_t M = u3h(x_shape); - uint64_t Na = u3h(u3t(x_shape)); - uint64_t Nb = u3h(y_shape); - uint64_t P = u3h(u3t(y_shape)); - - assert(u3_nul == u3t(u3t(x_shape))); - assert(Na == Nb); - uint64_t N = Na; - assert(u3_nul == u3t(u3t(y_shape))); - - uint8_t* x_bytes = (uint8_t*)u3a_malloc((M*N)*sizeof(uint8_t)); - u3r_bytes(0, M*N, x_bytes, x_data); - uint8_t* y_bytes = (uint8_t*)u3a_malloc((N*P)*sizeof(uint8_t)); - u3r_bytes(0, N*P, y_bytes, y_data); - uint8_t* c_bytes = (uint8_t*)u3a_malloc((M*P)*sizeof(uint8_t)); + uint64_t len_a = _get_length(shape); + uint64_t siz_a = len_a * pow(2, bloq - 3); + uint8_t* x_bytes = (uint8_t*)u3a_malloc(siz_a*sizeof(uint8_t)); + u3r_bytes(0, siz_a, x_bytes, x_data); + uint8_t* y_bytes = (uint8_t*)u3a_malloc((siz_a+1)*sizeof(uint8_t)); + u3r_bytes(0, siz_a+1, y_bytes, y_data); u3_noun r_data; // Switch on the block size. switch (bloq) { case 4: - hgemm('N', 'N', M, N, P, (float16_t){SB_REAL16_ONE}, (float16_t*)x_bytes, N, (float16_t*)y_bytes, N, (float16_t){SB_REAL16_ZERO}, (float16_t*)c_bytes, P); + for (uint64_t i = 0; i < len_a; i++) { + ((float16_t*)y_bytes)[i] = f16_mul(((float16_t*)x_bytes)[i], ((float16_t*)y_bytes)[i]); + } // Unpack the result back into a noun. - r_data = u3i_bytes(M*P, c_bytes); + r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); // Clean up. u3a_free(x_bytes); u3a_free(y_bytes); - u3a_free(c_bytes); - return u3nc(u3nq(u3nl(M, P, u3_none), bloq, c3__real, u3_nul), r_data); + return r_data; case 5: - sgemm('N', 'N', M, N, P, (float32_t){SB_REAL32_ONE}, (float32_t*)x_bytes, N, (float32_t*)y_bytes, N, (float32_t){SB_REAL32_ZERO}, (float32_t*)c_bytes, P); + for (uint64_t i = 0; i < len_a; i++) { + ((float32_t*)y_bytes)[i] = f32_mul(((float32_t*)x_bytes)[i], ((float32_t*)y_bytes)[i]); + } // Unpack the result back into a noun. - r_data = u3i_bytes(M*P, c_bytes); + r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); // Clean up. u3a_free(x_bytes); u3a_free(y_bytes); - u3a_free(c_bytes); - return u3nc(u3nq(u3nl(M, P, u3_none), bloq, c3__real, u3_nul), r_data); + return r_data; case 6: - dgemm('N', 'N', M, N, P, (float64_t){SB_REAL64_ONE}, (float64_t*)x_bytes, N, (float64_t*)y_bytes, N, (float64_t){SB_REAL64_ZERO}, (float64_t*)c_bytes, P); + for (uint64_t i = 0; i < len_a; i++) { + ((float64_t*)y_bytes)[i] = f64_mul(((float64_t*)x_bytes)[i], ((float64_t*)y_bytes)[i]); + } // Unpack the result back into a noun. - r_data = u3i_bytes(M*P, c_bytes); + r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); // Clean up. u3a_free(x_bytes); u3a_free(y_bytes); - u3a_free(c_bytes); - return u3nc(u3nq(u3nl(M, P, u3_none), bloq, c3__real, u3_nul), r_data); + return r_data; case 7: - qgemm('N', 'N', M, N, P, (float128_t){SB_REAL128L_ONE,SB_REAL128U_ONE}, (float128_t*)x_bytes, N, (float128_t*)y_bytes, N, (float128_t){SB_REAL128L_ZERO,SB_REAL128U_ZERO}, (float128_t*)c_bytes, P); + for (uint64_t i = 0; i < len_a; i++) { + f128M_mul(&(((float128_t*)y_bytes)[i]), &(((float128_t*)x_bytes)[i]), &(((float128_t*)y_bytes)[i])); + } // Unpack the result back into a noun. - r_data = u3i_bytes(M*P, c_bytes); + r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); // Clean up. u3a_free(x_bytes); u3a_free(y_bytes); - u3a_free(c_bytes); - return u3nc(u3nq(u3nl(M, P, u3_none), bloq, c3__real, u3_nul), r_data); + return r_data; default: u3a_free(x_bytes); u3a_free(y_bytes); - u3a_free(c_bytes); - + return u3_none; } } +/* div - x/y + elementwise division +*/ u3_noun - u3wf_la_add(u3_noun cor) + u3qf_la_div_real(u3_noun x_data, + u3_noun y_data, + u3_noun shape, + u3_noun bloq) { - // Each argument is a ray, [=meta data=@ux] - u3_noun a_meta, x_data, - b_meta, y_data; - - if ( c3n == u3r_mean(cor, - u3x_sam_4, &a_meta, - u3x_sam_5, &x_data, - u3x_sam_6, &b_meta, - u3x_sam_7, &y_data, - 0) || - c3n == u3ud(x_data) || - c3n == u3ud(y_data) ) - { - return u3m_bail(c3__exit); - } else { - u3_noun x_shape, a_bloq, a_kind, a_fxp, - y_shape, b_bloq, b_kind, b_fxp, - rnd; - if ( c3n == u3r_mean(a_meta, - 2, &x_shape, - 6, &a_bloq, - 14, &a_kind, - 15, &a_fxp, - 0) || - c3n == u3r_mean(b_meta, - 2, &y_shape, - 6, &b_bloq, - 14, &b_kind, - 15, &b_fxp, - 0) || - c3n == u3r_sing(x_shape, y_shape) || - c3n == u3r_sing(a_bloq, b_bloq) || - c3n == u3r_sing(a_kind, b_kind) || - // fxp does not need to match so no check - c3n == u3r_mean(cor, 30, &rnd, 0) - ) - { - return u3m_bail(c3__exit); - } else { - switch (a_kind) { - case c3__real: - _set_rounding(rnd); - u3_noun r_data = u3qf_la_add_real(x_data, y_data, x_shape, a_bloq); - return u3nc(u3nq(x_shape, a_bloq, a_kind, a_fxp), r_data); - break; + // Unpack the data as a byte array. We assume total length < 2**64. + uint64_t len_a = _get_length(shape); + uint64_t siz_a = len_a * pow(2, bloq - 3); + uint8_t* x_bytes = (uint8_t*)u3a_malloc(siz_a*sizeof(uint8_t)); + u3r_bytes(0, siz_a, x_bytes, x_data); + uint8_t* y_bytes = (uint8_t*)u3a_malloc((siz_a+1)*sizeof(uint8_t)); + u3r_bytes(0, siz_a+1, y_bytes, y_data); - // case c3__int2: - // return u3qf_la_add_int2(x_data, y_data, x_shape, a_bloq); + u3_noun r_data; - // case c3__uint: - // return u3qf_la_add_uint(x_data, y_data, x_shape, a_bloq); + // Switch on the block size. + switch (bloq) { + case 4: + for (uint64_t i = 0; i < len_a; i++) { + ((float16_t*)y_bytes)[i] = f16_div(((float16_t*)x_bytes)[i], ((float16_t*)y_bytes)[i]); + } - // case c3__cplx: - // _set_rounding(rnd); - // return u3qf_la_add_cplx(x_data, y_data, x_shape, a_bloq); + // Unpack the result back into a noun. + r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); - // case c3__unum: - // return u3qf_la_add_unum(x_data, y_data, x_shape, a_bloq); + // Clean up. + u3a_free(x_bytes); + u3a_free(y_bytes); - // case c3__fixp: - // return u3qf_la_add_fixp(x_data, y_data, x_shape, a_bloq); + return r_data; - default: - return u3_none; + case 5: + for (uint64_t i = 0; i < len_a; i++) { + ((float32_t*)y_bytes)[i] = f32_div(((float32_t*)x_bytes)[i], ((float32_t*)y_bytes)[i]); } - } - } - } - u3_noun - u3wf_la_sub(u3_noun cor) - { - // Each argument is a ray, [=meta data=@ux] - u3_noun a_meta, x_data, - b_meta, y_data; + // Unpack the result back into a noun. + r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); - if ( c3n == u3r_mean(cor, - u3x_sam_4, &a_meta, - u3x_sam_5, &x_data, - u3x_sam_6, &b_meta, - u3x_sam_7, &y_data, - 0) || - c3n == u3ud(x_data) || - c3n == u3ud(y_data) ) - { - return u3m_bail(c3__exit); - } else { - u3_noun x_shape, a_bloq, a_kind, a_fxp, - y_shape, b_bloq, b_kind, b_fxp, - rnd; - if ( c3n == u3r_mean(a_meta, - 2, &x_shape, - 6, &a_bloq, - 14, &a_kind, - 15, &a_fxp, - 0) || - c3n == u3r_mean(b_meta, - 2, &y_shape, - 6, &b_bloq, - 14, &b_kind, - 15, &b_fxp, - 0) || - c3n == u3r_sing(x_shape, y_shape) || - c3n == u3r_sing(a_bloq, b_bloq) || - c3n == u3r_sing(a_kind, b_kind) || - // fxp does not need to match so no check - c3n == u3r_mean(cor, 30, &rnd, 0) - ) - { - return u3m_bail(c3__exit); - } else { - switch (a_kind) { - case c3__real: - _set_rounding(rnd); - u3_noun r_data = u3qf_la_sub_real(x_data, y_data, x_shape, a_bloq); - return u3nc(u3nq(x_shape, a_bloq, a_kind, a_fxp), r_data); - break; + // Clean up. + u3a_free(x_bytes); + u3a_free(y_bytes); - // case c3__int2: - // return u3qf_la_sub_int2(x_data, y_data, x_shape, a_bloq); + return r_data; - // case c3__uint: - // return u3qf_la_sub_uint(x_data, y_data, x_shape, a_bloq); + case 6: + for (uint64_t i = 0; i < len_a; i++) { + ((float64_t*)y_bytes)[i] = f64_div(((float64_t*)x_bytes)[i], ((float64_t*)y_bytes)[i]); + } - // case c3__cplx: - // _set_rounding(rnd); - // return u3qf_la_sub_cplx(x_data, y_data, x_shape, a_bloq); + // Unpack the result back into a noun. + r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); - // case c3__unum: - // return u3qf_la_sub_unum(x_data, y_data, x_shape, a_bloq); + // Clean up. + u3a_free(x_bytes); + u3a_free(y_bytes); - // case c3__fixp: - // return u3qf_la_sub_fixp(x_data, y_data, x_shape, a_bloq); + return r_data; - default: - return u3_none; + case 7: + for (uint64_t i = 0; i < len_a; i++) { + f128M_div(&(((float128_t*)y_bytes)[i]), &(((float128_t*)x_bytes)[i]), &(((float128_t*)y_bytes)[i])); } - } + + // Unpack the result back into a noun. + r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); + + // Clean up. + u3a_free(x_bytes); + u3a_free(y_bytes); + + return r_data; + + default: + u3a_free(x_bytes); + u3a_free(y_bytes); + + return u3_none; } } +/* adds - axpy = 1*x+n +*/ u3_noun - u3wf_la_mmul(u3_noun cor) + u3qf_la_adds_real(u3_noun x_data, + u3_noun shape, + u3_noun bloq, + u3_noun n) { - // Each argument is a ray, [=meta data=@ux] - u3_noun a_meta, x_data, - b_meta, y_data; + // Unpack the data as a byte array. We assume total length < 2**64. + uint64_t len_a = _get_length(shape); + uint64_t siz_a = len_a * pow(2, bloq - 3); + uint8_t* x_bytes = (uint8_t*)u3a_malloc(siz_a*sizeof(uint8_t)); + u3r_bytes(0, siz_a, x_bytes, x_data); + uint8_t* y_bytes = (uint8_t*)u3a_malloc((siz_a+1)*sizeof(uint8_t)); - if ( c3n == u3r_mean(cor, - u3x_sam_4, &a_meta, - u3x_sam_5, &x_data, - u3x_sam_6, &b_meta, - u3x_sam_7, &y_data, - 0) || - c3n == u3ud(x_data) || - c3n == u3ud(y_data) ) - { - return u3m_bail(c3__exit); - } else { - u3_noun x_shape, a_bloq, a_kind, a_fxp, - y_shape, b_bloq, b_kind, b_fxp, - rnd; - if ( c3n == u3r_mean(a_meta, - 2, &x_shape, - 6, &a_bloq, - 14, &a_kind, - 15, &a_fxp, - 0) || - c3n == u3r_mean(b_meta, - 2, &y_shape, - 6, &b_bloq, - 14, &b_kind, - 15, &b_fxp, - 0) || - c3n == u3r_sing(a_bloq, b_bloq) || - c3n == u3r_sing(a_kind, b_kind) || - // fxp does not need to match so no check - c3n == u3r_mean(cor, 30, &rnd, 0) - ) - { - return u3m_bail(c3__exit); - } else { - switch (a_kind) { - case c3__real: - _set_rounding(rnd); - return u3qf_la_mmul_real(x_data, y_data, x_shape, y_shape, a_bloq); - break; + float16_t n16; + float32_t n32; + float64_t n64; + float128_t n128; - // case c3__int2: - // return u3qf_la_add_int2(x_data, y_data, x_shape, a_bloq); + u3_noun r_data; - // case c3__uint: - // return u3qf_la_add_uint(x_data, y_data, x_shape, a_bloq); + // Switch on the block size. We assume that n fits in the target block size; Hoon typecheck should prevent. + switch (bloq) { + case 4: + u3r_bytes(0, 2, (uint8_t*)&n16, n); + // set y to [n] + for (uint64_t i = 0; i < len_a; i++) { + ((float16_t*)y_bytes)[i] = n16; + } + y_bytes[siz_a] = 1; // pin head + haxpy(len_a, (float16_t){SB_REAL16_ONE}, (float16_t*)x_bytes, 1, (float16_t*)y_bytes, 1); - // case c3__cplx: - // return u3qf_la_add_cplx(x_data, y_data, x_shape, a_bloq, rnd); + // Unpack the result back into a noun. + r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); + + // Clean up. + u3a_free(x_bytes); + u3a_free(y_bytes); + + return r_data; + + case 5: + u3r_bytes(0, 4, (uint8_t*)&n32, n); + // set y to [n] + for (uint64_t i = 0; i < len_a; i++) { + ((float32_t*)y_bytes)[i] = n32; + } + y_bytes[siz_a] = 1; // pin head + saxpy(len_a, (float32_t){SB_REAL32_ONE}, (float32_t*)x_bytes, 1, (float32_t*)y_bytes, 1); + + // Unpack the result back into a noun. + r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); + + // Clean up. + u3a_free(x_bytes); + u3a_free(y_bytes); + + return r_data; + + case 6: + u3r_bytes(0, 8, (uint8_t*)&n64, n); + // set y to [n] + for (uint64_t i = 0; i < len_a; i++) { + ((float64_t*)y_bytes)[i] = n64; + } + y_bytes[siz_a] = 1; // pin head + daxpy(len_a, (float64_t){SB_REAL64_ONE}, (float64_t*)x_bytes, 1, (float64_t*)y_bytes, 1); + + // Unpack the result back into a noun. + r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); + + // Clean up. + u3a_free(x_bytes); + u3a_free(y_bytes); + + return r_data; + + case 7: + u3r_bytes(0, 16, (uint8_t*)&n128, n); + // set y to [n] + for (uint64_t i = 0; i < len_a; i++) { + ((float128_t*)y_bytes)[i] = (float128_t){n128.v[0], n128.v[1]}; + } + y_bytes[siz_a] = 1; // pin head + qaxpy(len_a, (float128_t){SB_REAL128L_ONE,SB_REAL128U_ONE}, (float128_t*)x_bytes, 1, (float128_t*)y_bytes, 1); + + // Unpack the result back into a noun. + r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); + + // Clean up. + u3a_free(x_bytes); + u3a_free(y_bytes); + + return r_data; + + default: + u3a_free(x_bytes); + u3a_free(y_bytes); + + return u3_none; + } + } + +/* subs - axpy = -1*n+x +*/ + u3_noun + u3qf_la_subs_real(u3_noun x_data, + u3_noun shape, + u3_noun bloq, + u3_noun n) + { + // Unpack the data as a byte array. We assume total length < 2**64. + uint64_t len_a = _get_length(shape); + uint64_t siz_a = len_a * pow(2, bloq - 3); + uint8_t* x_bytes = (uint8_t*)u3a_malloc((siz_a+1)*sizeof(uint8_t)); + u3r_bytes(0, siz_a+1, x_bytes, x_data); + uint8_t* y_bytes = (uint8_t*)u3a_malloc(siz_a*sizeof(uint8_t)); + + float16_t n16; + float32_t n32; + float64_t n64; + float128_t n128; + + u3_noun r_data; + + // Switch on the block size. We assume that n fits in the target block size; Hoon typecheck should prevent. + switch (bloq) { + case 4: + u3r_bytes(0, 2, (uint8_t*)&n16, n); + // set y to [n] + for (uint64_t i = 0; i < len_a; i++) { + ((float16_t*)y_bytes)[i] = n16; + } + haxpy(len_a, (float16_t){SB_REAL16_NEGONE}, (float16_t*)y_bytes, 1, (float16_t*)x_bytes, 1); + + // Unpack the result back into a noun. + r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), x_bytes); + + // Clean up. + u3a_free(x_bytes); + u3a_free(y_bytes); + + return r_data; + + case 5: + u3r_bytes(0, 4, (uint8_t*)&n32, n); + // set y to [n] + for (uint64_t i = 0; i < len_a; i++) { + ((float32_t*)y_bytes)[i] = n32; + } + saxpy(len_a, (float32_t){SB_REAL32_NEGONE}, (float32_t*)y_bytes, 1, (float32_t*)x_bytes, 1); + + // Unpack the result back into a noun. + r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), x_bytes); + + // Clean up. + u3a_free(x_bytes); + u3a_free(y_bytes); + + return r_data; + + case 6: + u3r_bytes(0, 8, (uint8_t*)&n64, n); + // set y to [n] + for (uint64_t i = 0; i < len_a; i++) { + ((float64_t*)y_bytes)[i] = n64; + } + daxpy(len_a, (float64_t){SB_REAL64_NEGONE}, (float64_t*)y_bytes, 1, (float64_t*)x_bytes, 1); + + // Unpack the result back into a noun. + r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), x_bytes); + + // Clean up. + u3a_free(x_bytes); + u3a_free(y_bytes); + + return r_data; + + case 7: + u3r_bytes(0, 16, (uint8_t*)&n128, n); + // set y to [n] + for (uint64_t i = 0; i < len_a; i++) { + ((float128_t*)y_bytes)[i] = (float128_t){n128.v[0], n128.v[1]}; + } + qaxpy(len_a, (float128_t){SB_REAL128L_NEGONE,SB_REAL128U_NEGONE}, (float128_t*)y_bytes, 1, (float128_t*)x_bytes, 1); + + // Unpack the result back into a noun. + r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), x_bytes); + + // Clean up. + u3a_free(x_bytes); + u3a_free(y_bytes); + + return r_data; + + default: + u3a_free(x_bytes); + u3a_free(y_bytes); + + return u3_none; + } + } + +/* muls - x.*[n] + elementwise multiplication +*/ + u3_noun + u3qf_la_muls_real(u3_noun x_data, + u3_noun shape, + u3_noun bloq, + u3_noun n) + { + // Unpack the data as a byte array. We assume total length < 2**64. + uint64_t len_a = _get_length(shape); + uint64_t siz_a = len_a * pow(2, bloq - 3); + uint8_t* x_bytes = (uint8_t*)u3a_malloc((siz_a+1)*sizeof(uint8_t)); + u3r_bytes(0, siz_a, x_bytes, x_data); + x_bytes[siz_a] = 1; // pin head + + float16_t n16; + float32_t n32; + float64_t n64; + float128_t n128; + + u3_noun r_data; + + // Switch on the block size. + switch (bloq) { + case 4: + u3r_bytes(0, 2, (uint8_t*)&n16, n); + hscal(len_a, n16, (float16_t*)x_bytes, 1); + + // Unpack the result back into a noun. + r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), x_bytes); + + // Clean up. + u3a_free(x_bytes); + + return r_data; + + case 5: + u3r_bytes(0, 4, (uint8_t*)&n32, n); + sscal(len_a, n32, (float32_t*)x_bytes, 1); + + // Unpack the result back into a noun. + r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), x_bytes); + + // Clean up. + u3a_free(x_bytes); + + return r_data; + + case 6: + u3r_bytes(0, 8, (uint8_t*)&n64, n); + dscal(len_a, n64, (float64_t*)x_bytes, 1); + + // Unpack the result back into a noun. + r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), x_bytes); + + // Clean up. + u3a_free(x_bytes); + + return r_data; + + case 7: + u3r_bytes(0, 16, (uint8_t*)&(n128.v[0]), n); + qscal(len_a, n128, (float128_t*)x_bytes, 1); + + // Unpack the result back into a noun. + r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), x_bytes); + + // Clean up. + u3a_free(x_bytes); + + return r_data; + + default: + u3a_free(x_bytes); + + return u3_none; + } + } + +/* divs - x/[n] + elementwise multiplication +*/ + u3_noun + u3qf_la_divs_real(u3_noun x_data, + u3_noun shape, + u3_noun bloq, + u3_noun n) + { + // Unpack the data as a byte array. We assume total length < 2**64. + uint64_t len_a = _get_length(shape); + uint64_t siz_a = len_a * pow(2, bloq - 3); + uint8_t* x_bytes = (uint8_t*)u3a_malloc((siz_a+1)*sizeof(uint8_t)); + u3r_bytes(0, siz_a, x_bytes, x_data); + x_bytes[siz_a] = 1; // pin head + + float16_t n16; + float32_t n32; + float64_t n64; + float128_t n128; + + u3_noun r_data; + + // Switch on the block size. + switch (bloq) { + case 4: + u3r_bytes(0, 2, (uint8_t*)&n16, n); + n16 = f16_div((float16_t){SB_REAL16_ONE}, n16); + hscal(len_a, n16, (float16_t*)x_bytes, 1); + + // Unpack the result back into a noun. + r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), x_bytes); + + // Clean up. + u3a_free(x_bytes); + + return r_data; + + case 5: + u3r_bytes(0, 4, (uint8_t*)&n32, n); + n32 = f32_div((float32_t){SB_REAL32_ONE}, n32); + sscal(len_a, n32, (float32_t*)x_bytes, 1); + + // Unpack the result back into a noun. + r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), x_bytes); + + // Clean up. + u3a_free(x_bytes); + + return r_data; + + case 6: + u3r_bytes(0, 8, (uint8_t*)&n64, n); + n64 = f64_div((float64_t){SB_REAL64_ONE}, n64); + dscal(len_a, n64, (float64_t*)x_bytes, 1); + + // Unpack the result back into a noun. + r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), x_bytes); + + // Clean up. + u3a_free(x_bytes); + + return r_data; + + case 7: + // u3r_bytes(0, 16, (uint8_t*)&(n128.v[0]), n); + u3l_log("divs: n", n); + u3r_bytes(0, 16, (uint8_t*)&n128, n); + fprintf(stderr, "n128: %lx %lx\r\n", n128.v[0], n128.v[1]); + f128M_div(&((float128_t){SB_REAL128L_ONE,SB_REAL128U_ONE}), &n128, &n128); + fprintf(stderr, "one: %lx %lx\r\n", SB_REAL128L_ONE, SB_REAL128U_ONE); + fprintf(stderr, "n128: %lx %lx\r\n", n128.v[0], n128.v[1]); + qscal(len_a, n128, (float128_t*)x_bytes, 1); + + // Unpack the result back into a noun. + r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), x_bytes); + + // Clean up. + u3a_free(x_bytes); + + return r_data; + + default: + u3a_free(x_bytes); + + return u3_none; + } + } + +/* mmul +*/ + u3_noun + u3qf_la_mmul_real(u3_noun x_data, + u3_noun y_data, + u3_noun x_shape, + u3_noun y_shape, + u3_noun bloq) + { + // Unpack the data as a byte array. We assume total length < 2**64. + uint64_t M = u3h(x_shape); + uint64_t Na = u3h(u3t(x_shape)); + uint64_t Nb = u3h(y_shape); + uint64_t P = u3h(u3t(y_shape)); + + assert(u3_nul == u3t(u3t(x_shape))); + assert(Na == Nb); + uint64_t N = Na; + assert(u3_nul == u3t(u3t(y_shape))); + + uint8_t* x_bytes = (uint8_t*)u3a_malloc((M*N)*sizeof(uint8_t)); + u3r_bytes(0, M*N, x_bytes, x_data); + uint8_t* y_bytes = (uint8_t*)u3a_malloc((N*P)*sizeof(uint8_t)); + u3r_bytes(0, N*P, y_bytes, y_data); + uint8_t* c_bytes = (uint8_t*)u3a_malloc((M*P)*sizeof(uint8_t)); + + u3_noun r_data; + + // Switch on the block size. + switch (bloq) { + case 4: + hgemm('N', 'N', M, N, P, (float16_t){SB_REAL16_ONE}, (float16_t*)x_bytes, N, (float16_t*)y_bytes, N, (float16_t){SB_REAL16_ZERO}, (float16_t*)c_bytes, P); + + // Unpack the result back into a noun. + r_data = u3i_bytes(M*P, c_bytes); + + // Clean up. + u3a_free(x_bytes); + u3a_free(y_bytes); + u3a_free(c_bytes); + + return u3nc(u3nq(u3nl(M, P, u3_none), bloq, c3__real, u3_nul), r_data); + + case 5: + sgemm('N', 'N', M, N, P, (float32_t){SB_REAL32_ONE}, (float32_t*)x_bytes, N, (float32_t*)y_bytes, N, (float32_t){SB_REAL32_ZERO}, (float32_t*)c_bytes, P); + + // Unpack the result back into a noun. + r_data = u3i_bytes(M*P, c_bytes); + + // Clean up. + u3a_free(x_bytes); + u3a_free(y_bytes); + u3a_free(c_bytes); + + return u3nc(u3nq(u3nl(M, P, u3_none), bloq, c3__real, u3_nul), r_data); + + case 6: + dgemm('N', 'N', M, N, P, (float64_t){SB_REAL64_ONE}, (float64_t*)x_bytes, N, (float64_t*)y_bytes, N, (float64_t){SB_REAL64_ZERO}, (float64_t*)c_bytes, P); + + // Unpack the result back into a noun. + r_data = u3i_bytes(M*P, c_bytes); + + // Clean up. + u3a_free(x_bytes); + u3a_free(y_bytes); + u3a_free(c_bytes); + + return u3nc(u3nq(u3nl(M, P, u3_none), bloq, c3__real, u3_nul), r_data); + + case 7: + qgemm('N', 'N', M, N, P, (float128_t){SB_REAL128L_ONE,SB_REAL128U_ONE}, (float128_t*)x_bytes, N, (float128_t*)y_bytes, N, (float128_t){SB_REAL128L_ZERO,SB_REAL128U_ZERO}, (float128_t*)c_bytes, P); + + // Unpack the result back into a noun. + r_data = u3i_bytes(M*P, c_bytes); + + // Clean up. + u3a_free(x_bytes); + u3a_free(y_bytes); + u3a_free(c_bytes); + + return u3nc(u3nq(u3nl(M, P, u3_none), bloq, c3__real, u3_nul), r_data); + + default: + u3a_free(x_bytes); + u3a_free(y_bytes); + u3a_free(c_bytes); + + return u3_none; + } + } + + u3_noun + u3wf_la_add(u3_noun cor) + { + // Each argument is a ray, [=meta data=@ux] + u3_noun a_meta, x_data, + b_meta, y_data; + + if ( c3n == u3r_mean(cor, + u3x_sam_4, &a_meta, + u3x_sam_5, &x_data, + u3x_sam_6, &b_meta, + u3x_sam_7, &y_data, + 0) || + c3n == u3ud(x_data) || + c3n == u3ud(y_data) ) + { + return u3m_bail(c3__exit); + } else { + u3_noun x_shape, a_bloq, a_kind, a_fxp, + y_shape, b_bloq, b_kind, b_fxp, + rnd; + if ( c3n == u3r_mean(a_meta, + 2, &x_shape, + 6, &a_bloq, + 14, &a_kind, + 15, &a_fxp, + 0) || + c3n == u3r_mean(b_meta, + 2, &y_shape, + 6, &b_bloq, + 14, &b_kind, + 15, &b_fxp, + 0) || + c3n == u3r_sing(x_shape, y_shape) || + c3n == u3r_sing(a_bloq, b_bloq) || + c3n == u3r_sing(a_kind, b_kind) || + // fxp does not need to match so no check + c3n == u3r_mean(cor, 30, &rnd, 0) + ) + { + return u3m_bail(c3__exit); + } else { + switch (a_kind) { + case c3__real: + _set_rounding(rnd); + u3_noun r_data = u3qf_la_add_real(x_data, y_data, x_shape, a_bloq); + return u3nc(u3nq(x_shape, a_bloq, a_kind, a_fxp), r_data); + break; + + default: + return u3_none; + } + } + } + } + + u3_noun + u3wf_la_sub(u3_noun cor) + { + // Each argument is a ray, [=meta data=@ux] + u3_noun a_meta, x_data, + b_meta, y_data; + + if ( c3n == u3r_mean(cor, + u3x_sam_4, &a_meta, + u3x_sam_5, &x_data, + u3x_sam_6, &b_meta, + u3x_sam_7, &y_data, + 0) || + c3n == u3ud(x_data) || + c3n == u3ud(y_data) ) + { + return u3m_bail(c3__exit); + } else { + u3_noun x_shape, a_bloq, a_kind, a_fxp, + y_shape, b_bloq, b_kind, b_fxp, + rnd; + if ( c3n == u3r_mean(a_meta, + 2, &x_shape, + 6, &a_bloq, + 14, &a_kind, + 15, &a_fxp, + 0) || + c3n == u3r_mean(b_meta, + 2, &y_shape, + 6, &b_bloq, + 14, &b_kind, + 15, &b_fxp, + 0) || + c3n == u3r_sing(x_shape, y_shape) || + c3n == u3r_sing(a_bloq, b_bloq) || + c3n == u3r_sing(a_kind, b_kind) || + // fxp does not need to match so no check + c3n == u3r_mean(cor, 30, &rnd, 0) + ) + { + return u3m_bail(c3__exit); + } else { + switch (a_kind) { + case c3__real: + _set_rounding(rnd); + u3_noun r_data = u3qf_la_sub_real(x_data, y_data, x_shape, a_bloq); + return u3nc(u3nq(x_shape, a_bloq, a_kind, a_fxp), r_data); + break; + + default: + return u3_none; + } + } + } + } + + u3_noun + u3wf_la_mul(u3_noun cor) + { + // Each argument is a ray, [=meta data=@ux] + u3_noun a_meta, x_data, + b_meta, y_data; + + if ( c3n == u3r_mean(cor, + u3x_sam_4, &a_meta, + u3x_sam_5, &x_data, + u3x_sam_6, &b_meta, + u3x_sam_7, &y_data, + 0) || + c3n == u3ud(x_data) || + c3n == u3ud(y_data) ) + { + return u3m_bail(c3__exit); + } else { + u3_noun x_shape, a_bloq, a_kind, a_fxp, + y_shape, b_bloq, b_kind, b_fxp, + rnd; + if ( c3n == u3r_mean(a_meta, + 2, &x_shape, + 6, &a_bloq, + 14, &a_kind, + 15, &a_fxp, + 0) || + c3n == u3r_mean(b_meta, + 2, &y_shape, + 6, &b_bloq, + 14, &b_kind, + 15, &b_fxp, + 0) || + c3n == u3r_sing(x_shape, y_shape) || + c3n == u3r_sing(a_bloq, b_bloq) || + c3n == u3r_sing(a_kind, b_kind) || + // fxp does not need to match so no check + c3n == u3r_mean(cor, 30, &rnd, 0) + ) + { + return u3m_bail(c3__exit); + } else { + switch (a_kind) { + case c3__real: + _set_rounding(rnd); + u3_noun r_data = u3qf_la_mul_real(x_data, y_data, x_shape, a_bloq); + return u3nc(u3nq(x_shape, a_bloq, a_kind, a_fxp), r_data); + break; + + default: + return u3_none; + } + } + } + } + + u3_noun + u3wf_la_div(u3_noun cor) + { + // Each argument is a ray, [=meta data=@ux] + u3_noun a_meta, x_data, + b_meta, y_data; + + if ( c3n == u3r_mean(cor, + u3x_sam_4, &a_meta, + u3x_sam_5, &x_data, + u3x_sam_6, &b_meta, + u3x_sam_7, &y_data, + 0) || + c3n == u3ud(x_data) || + c3n == u3ud(y_data) ) + { + return u3m_bail(c3__exit); + } else { + u3_noun x_shape, a_bloq, a_kind, a_fxp, + y_shape, b_bloq, b_kind, b_fxp, + rnd; + if ( c3n == u3r_mean(a_meta, + 2, &x_shape, + 6, &a_bloq, + 14, &a_kind, + 15, &a_fxp, + 0) || + c3n == u3r_mean(b_meta, + 2, &y_shape, + 6, &b_bloq, + 14, &b_kind, + 15, &b_fxp, + 0) || + c3n == u3r_sing(x_shape, y_shape) || + c3n == u3r_sing(a_bloq, b_bloq) || + c3n == u3r_sing(a_kind, b_kind) || + // fxp does not need to match so no check + c3n == u3r_mean(cor, 30, &rnd, 0) + ) + { + return u3m_bail(c3__exit); + } else { + switch (a_kind) { + case c3__real: + _set_rounding(rnd); + u3_noun r_data = u3qf_la_div_real(x_data, y_data, x_shape, a_bloq); + return u3nc(u3nq(x_shape, a_bloq, a_kind, a_fxp), r_data); + break; + + default: + return u3_none; + } + } + } + } + + u3_noun + u3wf_la_adds(u3_noun cor) + { + // Each argument is a ray, [=meta data=@ux] + u3_noun a_meta, x_data, n; + + if ( c3n == u3r_mean(cor, + u3x_sam_4, &a_meta, + u3x_sam_5, &x_data, + u3x_sam_3, &n, + 0) || + c3n == u3ud(x_data) || + c3n == u3ud(n) ) + { + return u3m_bail(c3__exit); + } else { + u3_noun x_shape, a_bloq, a_kind, a_fxp, + rnd; + if ( c3n == u3r_mean(a_meta, + 2, &x_shape, + 6, &a_bloq, + 14, &a_kind, + 15, &a_fxp, + 0) || + // shape does not matter so no check + // bloq does not matter so no check + // kind does not matter so no check + // fxp does not need to match so no check + c3n == u3r_mean(cor, 30, &rnd, 0) + ) + { + return u3m_bail(c3__exit); + } else { + switch (a_kind) { + case c3__real: + _set_rounding(rnd); + u3_noun r_data = u3qf_la_adds_real(x_data, x_shape, a_bloq, n); + return u3nc(u3nq(x_shape, a_bloq, a_kind, a_fxp), r_data); + break; + + default: + return u3_none; + } + } + } + } + + u3_noun + u3wf_la_subs(u3_noun cor) + { + // Each argument is a ray, [=meta data=@ux] + u3_noun a_meta, x_data, n; + + if ( c3n == u3r_mean(cor, + u3x_sam_4, &a_meta, + u3x_sam_5, &x_data, + u3x_sam_3, &n, + 0) || + c3n == u3ud(x_data) || + c3n == u3ud(n) ) + { + return u3m_bail(c3__exit); + } else { + u3_noun x_shape, a_bloq, a_kind, a_fxp, + rnd; + if ( c3n == u3r_mean(a_meta, + 2, &x_shape, + 6, &a_bloq, + 14, &a_kind, + 15, &a_fxp, + 0) || + // shape does not matter so no check + // bloq does not matter so no check + // kind does not matter so no check + // fxp does not need to match so no check + c3n == u3r_mean(cor, 30, &rnd, 0) + ) + { + return u3m_bail(c3__exit); + } else { + switch (a_kind) { + case c3__real: + _set_rounding(rnd); + u3_noun r_data = u3qf_la_subs_real(x_data, x_shape, a_bloq, n); + return u3nc(u3nq(x_shape, a_bloq, a_kind, a_fxp), r_data); + break; + + default: + return u3_none; + } + } + } + } + + u3_noun + u3wf_la_muls(u3_noun cor) + { + // Each argument is a ray, [=meta data=@ux] + u3_noun a_meta, x_data, n; + + if ( c3n == u3r_mean(cor, + u3x_sam_4, &a_meta, + u3x_sam_5, &x_data, + u3x_sam_3, &n, + 0) || + c3n == u3ud(x_data) || + c3n == u3ud(n) ) + { + return u3m_bail(c3__exit); + } else { + u3_noun x_shape, a_bloq, a_kind, a_fxp, + rnd; + if ( c3n == u3r_mean(a_meta, + 2, &x_shape, + 6, &a_bloq, + 14, &a_kind, + 15, &a_fxp, + 0) || + // shape does not matter so no check + // bloq does not matter so no check + // kind does not matter so no check + // fxp does not need to match so no check + c3n == u3r_mean(cor, 30, &rnd, 0) + ) + { + return u3m_bail(c3__exit); + } else { + switch (a_kind) { + case c3__real: + _set_rounding(rnd); + u3_noun r_data = u3qf_la_muls_real(x_data, x_shape, a_bloq, n); + return u3nc(u3nq(x_shape, a_bloq, a_kind, a_fxp), r_data); + break; + + default: + return u3_none; + } + } + } + } + + u3_noun + u3wf_la_divs(u3_noun cor) + { + // Each argument is a ray, [=meta data=@ux] + u3_noun a_meta, x_data, n; + + if ( c3n == u3r_mean(cor, + u3x_sam_4, &a_meta, + u3x_sam_5, &x_data, + u3x_sam_3, &n, + 0) || + c3n == u3ud(x_data) || + c3n == u3ud(n) ) + { + return u3m_bail(c3__exit); + } else { + u3_noun x_shape, a_bloq, a_kind, a_fxp, + rnd; + if ( c3n == u3r_mean(a_meta, + 2, &x_shape, + 6, &a_bloq, + 14, &a_kind, + 15, &a_fxp, + 0) || + // shape does not matter so no check + // bloq does not matter so no check + // kind does not matter so no check + // fxp does not need to match so no check + c3n == u3r_mean(cor, 30, &rnd, 0) + ) + { + return u3m_bail(c3__exit); + } else { + switch (a_kind) { + case c3__real: + _set_rounding(rnd); + u3_noun r_data = u3qf_la_divs_real(x_data, x_shape, a_bloq, n); + return u3nc(u3nq(x_shape, a_bloq, a_kind, a_fxp), r_data); + break; + + default: + return u3_none; + } + } + } + } - // case c3__unum: - // return u3qf_la_add_unum(x_data, y_data, x_shape, a_bloq); + u3_noun + u3wf_la_mmul(u3_noun cor) + { + // Each argument is a ray, [=meta data=@ux] + u3_noun a_meta, x_data, + b_meta, y_data; - // case c3__fixp: - // return u3qf_la_add_fixp(x_data, y_data, x_shape, a_bloq); + if ( c3n == u3r_mean(cor, + u3x_sam_4, &a_meta, + u3x_sam_5, &x_data, + u3x_sam_6, &b_meta, + u3x_sam_7, &y_data, + 0) || + c3n == u3ud(x_data) || + c3n == u3ud(y_data) ) + { + return u3m_bail(c3__exit); + } else { + u3_noun x_shape, a_bloq, a_kind, a_fxp, + y_shape, b_bloq, b_kind, b_fxp, + rnd; + if ( c3n == u3r_mean(a_meta, + 2, &x_shape, + 6, &a_bloq, + 14, &a_kind, + 15, &a_fxp, + 0) || + c3n == u3r_mean(b_meta, + 2, &y_shape, + 6, &b_bloq, + 14, &b_kind, + 15, &b_fxp, + 0) || + c3n == u3r_sing(a_bloq, b_bloq) || + c3n == u3r_sing(a_kind, b_kind) || + // fxp does not need to match so no check + c3n == u3r_mean(cor, 30, &rnd, 0) + ) + { + return u3m_bail(c3__exit); + } else { + switch (a_kind) { + case c3__real: + _set_rounding(rnd); + return u3qf_la_mmul_real(x_data, y_data, x_shape, y_shape, a_bloq); + break; default: return u3_none; diff --git a/pkg/noun/jets/q.h b/pkg/noun/jets/q.h index 371cebb228..807f7bdcff 100644 --- a/pkg/noun/jets/q.h +++ b/pkg/noun/jets/q.h @@ -249,6 +249,12 @@ u3_noun u3qf_la_add_real(u3_noun, u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_sub_real(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qf_la_mul_real(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qf_la_div_real(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qf_la_adds_real(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qf_la_subs_real(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qf_la_muls_real(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qf_la_divs_real(u3_noun, u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_mmul_real(u3_noun, u3_noun, u3_noun, u3_noun, u3_noun); # define u3qfu_van_fan 28 diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index 035dad2305..cf82124dac 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -2153,10 +2153,22 @@ static u3j_core _139_sep_d[] = */ static u3j_harm _139_hex__lagoon_add_a[] = {{".2", u3wf_la_add}, {}}; static u3j_harm _139_hex__lagoon_sub_a[] = {{".2", u3wf_la_sub}, {}}; +static u3j_harm _139_hex__lagoon_mul_a[] = {{".2", u3wf_la_mul}, {}}; +static u3j_harm _139_hex__lagoon_div_a[] = {{".2", u3wf_la_div}, {}}; +static u3j_harm _139_hex__lagoon_adds_a[] = {{".2", u3wf_la_adds}, {}}; +static u3j_harm _139_hex__lagoon_subs_a[] = {{".2", u3wf_la_subs}, {}}; +static u3j_harm _139_hex__lagoon_muls_a[] = {{".2", u3wf_la_muls}, {}}; +static u3j_harm _139_hex__lagoon_divs_a[] = {{".2", u3wf_la_divs}, {}}; static u3j_harm _139_hex__lagoon_mmul_a[] = {{".2", u3wf_la_mmul}, {}}; static u3j_core _139_hex__la_core_d[] = { { "add-rays", 7, _139_hex__lagoon_add_a, 0, no_hashes }, { "sub-rays", 7, _139_hex__lagoon_sub_a, 0, no_hashes }, + { "mul-rays", 7, _139_hex__lagoon_mul_a, 0, no_hashes }, + { "div-rays", 7, _139_hex__lagoon_div_a, 0, no_hashes }, + { "add-scal", 7, _139_hex__lagoon_adds_a, 0, no_hashes }, + { "sub-scal", 7, _139_hex__lagoon_subs_a, 0, no_hashes }, + { "mul-scal", 7, _139_hex__lagoon_muls_a, 0, no_hashes }, + { "div-scal", 7, _139_hex__lagoon_divs_a, 0, no_hashes }, { "mmul", 7, _139_hex__lagoon_mmul_a, 0, no_hashes }, {} }; diff --git a/pkg/noun/jets/w.h b/pkg/noun/jets/w.h index f7d4ef6a53..b833d2ca49 100644 --- a/pkg/noun/jets/w.h +++ b/pkg/noun/jets/w.h @@ -333,6 +333,12 @@ u3_noun u3wf_la_add(u3_noun); u3_noun u3wf_la_sub(u3_noun); + u3_noun u3wf_la_mul(u3_noun); + u3_noun u3wf_la_div(u3_noun); + u3_noun u3wf_la_adds(u3_noun); + u3_noun u3wf_la_subs(u3_noun); + u3_noun u3wf_la_muls(u3_noun); + u3_noun u3wf_la_divs(u3_noun); u3_noun u3wf_la_mmul(u3_noun); #endif /* ifndef U3_JETS_W_H */ From 0a42728a19bbb866f373cf4b93f8ad96dd2415bf Mon Sep 17 00:00:00 2001 From: Sigilante Date: Wed, 27 Mar 2024 12:10:48 -0500 Subject: [PATCH 075/430] Add trace/diag/dot --- pkg/noun/jets/f/lagoon.c | 299 +++++++++++++++++++++++++++++++++++++-- pkg/noun/jets/q.h | 3 + pkg/noun/jets/tree.c | 14 +- pkg/noun/jets/w.h | 3 + 4 files changed, 303 insertions(+), 16 deletions(-) diff --git a/pkg/noun/jets/f/lagoon.c b/pkg/noun/jets/f/lagoon.c index ba55c5b4db..b6db643273 100644 --- a/pkg/noun/jets/f/lagoon.c +++ b/pkg/noun/jets/f/lagoon.c @@ -85,6 +85,23 @@ return len; } + static inline uint64_t* _get_dims(u3_noun shape) + { + uint64_t len = u3kb_lent(shape); + uint64_t* dims = (uint64_t*)u3a_malloc(len*sizeof(uint64_t)); + for (uint64_t i = 0; i < len; i++) { + dims[i] = u3h(shape); + shape = u3t(shape); + } + return dims; + } + + static inline size_t _get_array_length(uint64_t* array) + { + size_t n = sizeof(array)/sizeof(array[0]); + return n; + } + /* add - axpy = 1*x+y */ u3_noun @@ -611,7 +628,7 @@ } } -/* muls - x.*[n] +/* muls - ?scal n * x elementwise multiplication */ u3_noun @@ -691,8 +708,8 @@ } } -/* divs - x/[n] - elementwise multiplication +/* divs - ?scal 1/n * x + elementwise division */ u3_noun u3qf_la_divs_real(u3_noun x_data, @@ -717,7 +734,7 @@ // Switch on the block size. switch (bloq) { case 4: - u3r_bytes(0, 2, (uint8_t*)&n16, n); + u3r_bytes(0, 2, (uint8_t*)&(n16.v), n); n16 = f16_div((float16_t){SB_REAL16_ONE}, n16); hscal(len_a, n16, (float16_t*)x_bytes, 1); @@ -730,7 +747,7 @@ return r_data; case 5: - u3r_bytes(0, 4, (uint8_t*)&n32, n); + u3r_bytes(0, 4, (uint8_t*)&(n32.v), n); n32 = f32_div((float32_t){SB_REAL32_ONE}, n32); sscal(len_a, n32, (float32_t*)x_bytes, 1); @@ -743,7 +760,7 @@ return r_data; case 6: - u3r_bytes(0, 8, (uint8_t*)&n64, n); + u3r_bytes(0, 8, (uint8_t*)&(n64.v), n); n64 = f64_div((float64_t){SB_REAL64_ONE}, n64); dscal(len_a, n64, (float64_t*)x_bytes, 1); @@ -756,13 +773,8 @@ return r_data; case 7: - // u3r_bytes(0, 16, (uint8_t*)&(n128.v[0]), n); - u3l_log("divs: n", n); - u3r_bytes(0, 16, (uint8_t*)&n128, n); - fprintf(stderr, "n128: %lx %lx\r\n", n128.v[0], n128.v[1]); + u3r_bytes(0, 16, (uint8_t*)&(n128.v[0]), n); f128M_div(&((float128_t){SB_REAL128L_ONE,SB_REAL128U_ONE}), &n128, &n128); - fprintf(stderr, "one: %lx %lx\r\n", SB_REAL128L_ONE, SB_REAL128U_ONE); - fprintf(stderr, "n128: %lx %lx\r\n", n128.v[0], n128.v[1]); qscal(len_a, n128, (float128_t*)x_bytes, 1); // Unpack the result back into a noun. @@ -780,6 +792,130 @@ } } +/* dot - ?dot = x · y +*/ + u3_noun + u3qf_la_dot_real(u3_noun x_data, + u3_noun y_data, + u3_noun shape, + u3_noun bloq) + { + // Unpack the data as a byte array. We assume total length < 2**64. + uint64_t len_a = _get_length(shape); + uint64_t siz_a = len_a * pow(2, bloq - 3); + uint8_t* x_bytes = (uint8_t*)u3a_malloc(siz_a*sizeof(uint8_t)); + u3r_bytes(0, siz_a, x_bytes, x_data); + uint8_t* y_bytes = (uint8_t*)u3a_malloc((siz_a+1)*sizeof(uint8_t)); + u3r_bytes(0, siz_a+1, y_bytes, y_data); + + u3_noun r_data; + + // Switch on the block size. + switch (bloq) { + case 4: + hdot(len_a, (float16_t*)x_bytes, 1, (float16_t*)y_bytes, 1); + + // Unpack the result back into a noun. + r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); + + // Clean up. + u3a_free(x_bytes); + u3a_free(y_bytes); + + return r_data; + + case 5: + sdot(len_a, (float32_t*)x_bytes, 1, (float32_t*)y_bytes, 1); + + // Unpack the result back into a noun. + r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); + + // Clean up. + u3a_free(x_bytes); + u3a_free(y_bytes); + + return r_data; + + case 6: + ddot(len_a, (float64_t*)x_bytes, 1, (float64_t*)y_bytes, 1); + + // Unpack the result back into a noun. + r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); + + // Clean up. + u3a_free(x_bytes); + u3a_free(y_bytes); + + return r_data; + + case 7: + qdot(len_a, (float128_t*)x_bytes, 1, (float128_t*)y_bytes, 1); + + // Unpack the result back into a noun. + r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); + + // Clean up. + u3a_free(x_bytes); + u3a_free(y_bytes); + + return r_data; + + default: + u3a_free(x_bytes); + u3a_free(y_bytes); + + return u3_none; + } + } + +/* diag - diag(x) +*/ + u3_noun + u3qf_la_diag(u3_noun x_data, + u3_noun shape, + u3_noun bloq) + { + // Unpack shape into an array of dimensions. + uint64_t* dims = _get_dims(shape); + // Assert length of dims is 2. + assert(dims[0] == dims[1]); + assert(_get_array_length(dims) == 2); + + // Unpack the data as a byte array. We assume total length < 2**64. + uint64_t len_a = _get_length(shape); + uint64_t siz_a = len_a * pow(2, bloq - 3); + uint8_t* x_bytes = (uint8_t*)u3a_malloc((siz_a+1)*sizeof(uint8_t)); + u3r_bytes(0, siz_a, x_bytes, x_data); + uint8_t* y_bytes = (uint8_t*)u3a_malloc((dims[0]*dims[1]+1)*sizeof(uint8_t)); + + u3_noun r_data; + + for (uint64_t i = 0; i < dims[0]; i++) { + y_bytes[i] = x_bytes[i*dims[0] + i]; + } + y_bytes[dims[0]*dims[1]] = 1; // pin head + + // Unpack the result back into a noun. + r_data = u3i_bytes((dims[0]*dims[1]+1)*sizeof(uint8_t), y_bytes); + + u3a_free(x_bytes); + u3a_free(y_bytes); + u3a_free(dims); + + return r_data; + } + +/* trace - tr(x) +*/ + u3_noun + u3qf_la_trace_real(u3_noun x_data, + u3_noun shape, + u3_noun bloq) + { + u3_noun diag_data = u3qf_la_diag(x_data, shape, bloq); + return u3qf_la_dot_real(diag_data, diag_data, shape, bloq); + } + /* mmul */ u3_noun @@ -1283,6 +1419,145 @@ } } + u3_noun + u3wf_la_dot(u3_noun cor) + { + // Each argument is a ray, [=meta data=@ux] + u3_noun a_meta, x_data, + b_meta, y_data; + + if ( c3n == u3r_mean(cor, + u3x_sam_4, &a_meta, + u3x_sam_5, &x_data, + u3x_sam_6, &b_meta, + u3x_sam_7, &y_data, + 0) || + c3n == u3ud(x_data) || + c3n == u3ud(y_data) ) + { + return u3m_bail(c3__exit); + } else { + u3_noun x_shape, a_bloq, a_kind, a_fxp, + y_shape, b_bloq, b_kind, b_fxp, + rnd; + if ( c3n == u3r_mean(a_meta, + 2, &x_shape, + 6, &a_bloq, + 14, &a_kind, + 15, &a_fxp, + 0) || + c3n == u3r_mean(b_meta, + 2, &y_shape, + 6, &b_bloq, + 14, &b_kind, + 15, &b_fxp, + 0) || + c3n == u3r_sing(x_shape, y_shape) || + c3n == u3r_sing(a_bloq, b_bloq) || + c3n == u3r_sing(a_kind, b_kind) || + c3n == u3r_sing(a_fxp, b_fxp) || + c3n == u3r_mean(cor, 30, &rnd, 0) + ) + { + return u3m_bail(c3__exit); + } else { + switch (a_kind) { + case c3__real: + _set_rounding(rnd); + u3_noun r_data = u3qf_la_dot_real(x_data, y_data, x_shape, a_bloq); + return u3nc(u3nq(x_shape, a_bloq, a_kind, a_fxp), r_data); + break; + + default: + return u3_none; + } + } + } + } + + u3_noun + u3wf_la_diag(u3_noun cor) + { + // Each argument is a ray, [=meta data=@ux] + u3_noun a_meta, x_data; + + if ( c3n == u3r_mean(cor, + u3x_sam_4, &a_meta, + u3x_sam_5, &x_data, + 0) || + c3n == u3ud(x_data) ) + { + return u3m_bail(c3__exit); + } else { + u3_noun x_shape, a_bloq, a_kind, a_fxp, + rnd; + if ( c3n == u3r_mean(a_meta, + 2, &x_shape, + 6, &a_bloq, + 14, &a_kind, + 15, &a_fxp, + 0) + // c3n == u3r_sing(x_shape, y_shape) || + // c3n == u3r_sing(a_bloq, b_bloq) || + // c3n == u3r_sing(a_kind, b_kind) || + // c3n == u3r_sing(a_fxp, b_fxp) || + // c3n == u3r_mean(cor, 30, &rnd, 0) + ) + { + return u3m_bail(c3__exit); + } else { + u3_noun r_data = u3qf_la_diag(x_data, x_shape, a_bloq); + return u3nc(u3nq(x_shape, a_bloq, a_kind, a_fxp), r_data); + } + } + } + + u3_noun + u3wf_la_trace(u3_noun cor) + { + // Each argument is a ray, [=meta data=@ux] + u3_noun a_meta, x_data; + + if ( c3n == u3r_mean(cor, + u3x_sam_4, &a_meta, + u3x_sam_5, &x_data, + 0) || + c3n == u3ud(x_data) ) + { + return u3m_bail(c3__exit); + } else { + u3_noun x_shape, a_bloq, a_kind, a_fxp, + rnd; + if ( c3n == u3r_mean(a_meta, + 2, &x_shape, + 6, &a_bloq, + 14, &a_kind, + 15, &a_fxp, + 0) + // c3n == u3r_sing(x_shape, y_shape) || + // c3n == u3r_sing(a_bloq, b_bloq) || + // c3n == u3r_sing(a_kind, b_kind) || + // c3n == u3r_sing(a_fxp, b_fxp) || + // c3n == u3r_mean(cor, 30, &rnd, 0) + ) + { + return u3m_bail(c3__exit); + } else { + switch (a_kind) { + case c3__real: + _set_rounding(rnd); + u3_noun r_data = u3qf_la_trace_real(x_data, x_shape, a_bloq); + uint64_t len_x0 = _get_dims(x_shape)[0]; + return u3nc(u3nq(len_x0, a_bloq, a_kind, a_fxp), r_data); + break; + + default: + return u3_none; + } + } + } + } + u3_noun u3wf_la_mmul(u3_noun cor) { diff --git a/pkg/noun/jets/q.h b/pkg/noun/jets/q.h index 807f7bdcff..db84af39d1 100644 --- a/pkg/noun/jets/q.h +++ b/pkg/noun/jets/q.h @@ -255,6 +255,9 @@ u3_noun u3qf_la_subs_real(u3_noun, u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_muls_real(u3_noun, u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_divs_real(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qf_la_dot_real(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qf_la_diag(u3_noun, u3_noun, u3_noun); + u3_noun u3qf_la_trace_real(u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_mmul_real(u3_noun, u3_noun, u3_noun, u3_noun, u3_noun); # define u3qfu_van_fan 28 diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index cf82124dac..084ba01e98 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -2151,14 +2151,17 @@ static u3j_core _139_hex_json_d[] = XX move to outer _sep_ core for /lib? eventually static u3j_core _139_sep_d[] = */ -static u3j_harm _139_hex__lagoon_add_a[] = {{".2", u3wf_la_add}, {}}; -static u3j_harm _139_hex__lagoon_sub_a[] = {{".2", u3wf_la_sub}, {}}; -static u3j_harm _139_hex__lagoon_mul_a[] = {{".2", u3wf_la_mul}, {}}; -static u3j_harm _139_hex__lagoon_div_a[] = {{".2", u3wf_la_div}, {}}; +static u3j_harm _139_hex__lagoon_add_a[] = {{".2", u3wf_la_add}, {}}; +static u3j_harm _139_hex__lagoon_sub_a[] = {{".2", u3wf_la_sub}, {}}; +static u3j_harm _139_hex__lagoon_mul_a[] = {{".2", u3wf_la_mul}, {}}; +static u3j_harm _139_hex__lagoon_div_a[] = {{".2", u3wf_la_div}, {}}; static u3j_harm _139_hex__lagoon_adds_a[] = {{".2", u3wf_la_adds}, {}}; static u3j_harm _139_hex__lagoon_subs_a[] = {{".2", u3wf_la_subs}, {}}; static u3j_harm _139_hex__lagoon_muls_a[] = {{".2", u3wf_la_muls}, {}}; static u3j_harm _139_hex__lagoon_divs_a[] = {{".2", u3wf_la_divs}, {}}; +static u3j_harm _139_hex__lagoon_dot_a[] = {{".2", u3wf_la_dot}, {}}; +static u3j_harm _139_hex__lagoon_diag_a[] = {{".2", u3wf_la_diag}, {}}; +static u3j_harm _139_hex__lagoon_trace_a[]= {{".2", u3wf_la_trace}, {}}; static u3j_harm _139_hex__lagoon_mmul_a[] = {{".2", u3wf_la_mmul}, {}}; static u3j_core _139_hex__la_core_d[] = { { "add-rays", 7, _139_hex__lagoon_add_a, 0, no_hashes }, @@ -2169,6 +2172,9 @@ static u3j_core _139_hex__la_core_d[] = { "sub-scal", 7, _139_hex__lagoon_subs_a, 0, no_hashes }, { "mul-scal", 7, _139_hex__lagoon_muls_a, 0, no_hashes }, { "div-scal", 7, _139_hex__lagoon_divs_a, 0, no_hashes }, + { "dot", 7, _139_hex__lagoon_dot_a, 0, no_hashes }, + { "diag", 7, _139_hex__lagoon_diag_a, 0, no_hashes }, + { "trace", 7, _139_hex__lagoon_trace_a,0, no_hashes }, { "mmul", 7, _139_hex__lagoon_mmul_a, 0, no_hashes }, {} }; diff --git a/pkg/noun/jets/w.h b/pkg/noun/jets/w.h index b833d2ca49..2bd87e6551 100644 --- a/pkg/noun/jets/w.h +++ b/pkg/noun/jets/w.h @@ -339,6 +339,9 @@ u3_noun u3wf_la_subs(u3_noun); u3_noun u3wf_la_muls(u3_noun); u3_noun u3wf_la_divs(u3_noun); + u3_noun u3wf_la_dot(u3_noun); + u3_noun u3wf_la_diag(u3_noun); + u3_noun u3wf_la_trace(u3_noun); u3_noun u3wf_la_mmul(u3_noun); #endif /* ifndef U3_JETS_W_H */ From ebe6dd381397b909cf29f28954dda1d11ac7a737 Mon Sep 17 00:00:00 2001 From: Sigilante Date: Wed, 27 Mar 2024 20:36:30 -0500 Subject: [PATCH 076/430] WIP shape+diag error --- pkg/noun/jets/f/lagoon.c | 72 +++++++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 27 deletions(-) diff --git a/pkg/noun/jets/f/lagoon.c b/pkg/noun/jets/f/lagoon.c index b6db643273..baaac63331 100644 --- a/pkg/noun/jets/f/lagoon.c +++ b/pkg/noun/jets/f/lagoon.c @@ -73,32 +73,42 @@ } } -/* shape +/* length of shape = x * y * z * w * ... */ - static inline uint64_t _get_length(u3_noun shape) + static inline c3_d _get_length(u3_noun shape) { - uint64_t len = 1; + c3_d len = 1; while (u3_nul != shape) { - len = len * u3h(shape); + len = len * u3x_atom(u3h(shape)); shape = u3t(shape); } return len; } - static inline uint64_t* _get_dims(u3_noun shape) +/* get dims from shape as array [x y z w ...] +*/ + static inline c3_d* _get_dims(u3_noun shape) { - uint64_t len = u3kb_lent(shape); - uint64_t* dims = (uint64_t*)u3a_malloc(len*sizeof(uint64_t)); - for (uint64_t i = 0; i < len; i++) { - dims[i] = u3h(shape); + u3_atom len = u3qb_lent(shape); + c3_d len_d = u3r_chub(0, len); + c3_d* dims = (c3_d*)u3a_malloc(len_d*sizeof(c3_d)); + for (uint64_t i = 0; i < len_d; i++) { + dims[i] = u3r_chub(0, u3x_atom(u3h(shape))); shape = u3t(shape); } + u3z(len); return dims; } +/* +*/ static inline size_t _get_array_length(uint64_t* array) { size_t n = sizeof(array)/sizeof(array[0]); + for (size_t i = 0; i < n; i++) { + fprintf(stderr, "%x ", array[i]); + } + fprintf(stderr, " => %x \n", n); return n; } @@ -434,7 +444,9 @@ { // Unpack the data as a byte array. We assume total length < 2**64. uint64_t len_a = _get_length(shape); + fprintf(stderr, "len_a: %d 0x%x units\r\n", len_a, len_a); uint64_t siz_a = len_a * pow(2, bloq - 3); + fprintf(stderr, "siz_a: %d 0x%x bytes\r\n", siz_a, siz_a); uint8_t* x_bytes = (uint8_t*)u3a_malloc(siz_a*sizeof(uint8_t)); u3r_bytes(0, siz_a, x_bytes, x_data); uint8_t* y_bytes = (uint8_t*)u3a_malloc((siz_a+1)*sizeof(uint8_t)); @@ -875,28 +887,33 @@ u3_noun shape, u3_noun bloq) { - // Unpack shape into an array of dimensions. - uint64_t* dims = _get_dims(shape); // Assert length of dims is 2. + assert(u3qb_lent(shape) == 2); + // Unpack shape into an array of dimensions. + uint64_t *dims = _get_dims(shape); assert(dims[0] == dims[1]); - assert(_get_array_length(dims) == 2); // Unpack the data as a byte array. We assume total length < 2**64. uint64_t len_a = _get_length(shape); uint64_t siz_a = len_a * pow(2, bloq - 3); + uint64_t stride = dims[0] * pow(2, bloq - 3); uint8_t* x_bytes = (uint8_t*)u3a_malloc((siz_a+1)*sizeof(uint8_t)); - u3r_bytes(0, siz_a, x_bytes, x_data); - uint8_t* y_bytes = (uint8_t*)u3a_malloc((dims[0]*dims[1]+1)*sizeof(uint8_t)); + u3r_bytes(0, siz_a+1, x_bytes, x_data); + uint64_t siz_b = stride * dims[1]; + uint8_t* y_bytes = (uint8_t*)u3a_malloc((siz_b+1)*sizeof(uint8_t)); u3_noun r_data; - for (uint64_t i = 0; i < dims[0]; i++) { - y_bytes[i] = x_bytes[i*dims[0] + i]; + for (uint64_t i = 0; i < dims[1]; i++) { + for (uint64_t j = 0; j < stride; j++) { + fprintf(stderr, "i*s+j = %d*%d+%d = %d // x_bytes[i]: %lx\r\n", i, stride, j, i*stride+j, x_bytes[i*stride+j + i]); + y_bytes[i*stride+j] = x_bytes[i*stride+j + i]; + } } - y_bytes[dims[0]*dims[1]] = 1; // pin head + y_bytes[siz_b] = 1; // pin head // Unpack the result back into a noun. - r_data = u3i_bytes((dims[0]*dims[1]+1)*sizeof(uint8_t), y_bytes); + r_data = u3i_bytes((siz_b+1)*sizeof(uint8_t), y_bytes); u3a_free(x_bytes); u3a_free(y_bytes); @@ -913,7 +930,8 @@ u3_noun bloq) { u3_noun diag_data = u3qf_la_diag(x_data, shape, bloq); - return u3qf_la_dot_real(diag_data, diag_data, shape, bloq); + uint64_t len_x0 = _get_dims(shape)[0]; + return u3qf_la_dot_real(diag_data, diag_data, u3nt(len_x0, 0x1, u3_nul), bloq); } /* mmul @@ -1053,7 +1071,7 @@ case c3__real: _set_rounding(rnd); u3_noun r_data = u3qf_la_add_real(x_data, y_data, x_shape, a_bloq); - return u3nc(u3nq(x_shape, a_bloq, a_kind, a_fxp), r_data); + return u3nc(u3nq(y_shape, a_bloq, a_kind, a_fxp), r_data); break; default: @@ -1482,8 +1500,8 @@ u3_noun a_meta, x_data; if ( c3n == u3r_mean(cor, - u3x_sam_4, &a_meta, - u3x_sam_5, &x_data, + u3x_sam_2, &a_meta, + u3x_sam_3, &x_data, 0) || c3n == u3ud(x_data) ) { @@ -1507,7 +1525,8 @@ return u3m_bail(c3__exit); } else { u3_noun r_data = u3qf_la_diag(x_data, x_shape, a_bloq); - return u3nc(u3nq(x_shape, a_bloq, a_kind, a_fxp), r_data); + uint64_t len_x0 = _get_dims(x_shape)[0]; + return u3nc(u3nq(u3nt(len_x0, 0x1, u3_nul), a_bloq, a_kind, a_fxp), r_data); } } } @@ -1519,8 +1538,8 @@ u3_noun a_meta, x_data; if ( c3n == u3r_mean(cor, - u3x_sam_4, &a_meta, - u3x_sam_5, &x_data, + u3x_sam_2, &a_meta, + u3x_sam_3, &x_data, 0) || c3n == u3ud(x_data) ) { @@ -1547,8 +1566,7 @@ case c3__real: _set_rounding(rnd); u3_noun r_data = u3qf_la_trace_real(x_data, x_shape, a_bloq); - uint64_t len_x0 = _get_dims(x_shape)[0]; - return u3nc(u3nq(len_x0, a_bloq, a_kind, a_fxp), r_data); + return u3nc(u3nq(u3nt(0x1, 0x1, u3_nul), a_bloq, a_kind, a_fxp), r_data); break; default: From 7e01cccb89388084d8d5a1de666d3dc591fbecdb Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Wed, 3 Apr 2024 13:46:03 +0200 Subject: [PATCH 077/430] mesa: wip --- pkg/c3/motes.h | 1 + pkg/vere/io/mesa.c | 100 ++++++++++++++++++++++++++-------------- pkg/vere/io/mesa/pact.c | 44 +++++++++++------- 3 files changed, 95 insertions(+), 50 deletions(-) diff --git a/pkg/c3/motes.h b/pkg/c3/motes.h index 863a0710b4..e1fbb7707e 100644 --- a/pkg/c3/motes.h +++ b/pkg/c3/motes.h @@ -756,6 +756,7 @@ # define c3__mesa c3_s4('m','e','s','a') # define c3__mesh c3_s4('m','e','s','h') # define c3__mess c3_s4('m','e','s','s') +# define c3__mess_ser c3_s8('m','e','s','s','-', 's', 'e','r') # define c3__met c3_s3('m','e','t') # define c3__meta c3_s4('m','e','t','a') # define c3__mill c3_s4('m','i','l','l') diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index e130f98f17..8cf5a3a2d6 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -285,6 +285,14 @@ _log_pend_req(u3_pend_req* req_u) //u3l_log("timer in: %" PRIu64 " ms", uv_timer_get_due_in(&req_u->tim_u)); } +static void +_log_mesa_data(u3_mesa_data dat_u) +{ + u3l_log("total frag: %u", dat_u.tot_w); + u3l_log("frag len: %u", dat_u.len_w); + // u3l_log("frag: %xxx", dat_u.fra_y); +} + static c3_d _get_now_micros() { @@ -1034,7 +1042,7 @@ _realise_lane(u3_noun lan) { lan_u.pip_w = 0; if ( c3y == u3a_is_cat(lan) ) { - u3_assert( lan < 256 ); + // u3_assert( lan < 256 ); if ( (c3n == u3_Host.ops_u.net) ) { lan_u.pip_w = 0x7f000001 ; lan_u.por_s = _ames_czar_port(lan); @@ -1220,6 +1228,7 @@ _mesa_queue_czar(u3_mesa* sam_u, u3_noun las, u3_noun pac) static void _mesa_ef_send(u3_mesa* sam_u, u3_noun las, u3_noun pac) { + u3m_p("pac", pac); las = _mesa_queue_czar(sam_u, las, u3k(pac)); c3_w len_w = u3r_met(3, pac); c3_y* buf_y = c3_calloc(len_w); @@ -1758,7 +1767,6 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) _mesa_put_peer(sam_u, pac_u->pag_u.nam_u.her_d, per_u); - u3_weak hit = _mesa_get_cache(sam_u, &pac_u->pag_u.nam_u); if ( u3_none != hit ) { @@ -1789,15 +1797,25 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) } { - u3_noun pax = _mesa_encode_path(pac_u->pag_u.nam_u.pat_s, - (c3_y*)(pac_u->pag_u.nam_u.pat_c)); - u3_noun par = u3nc(u3i_chubs(2, pac_u->pag_u.nam_u.her_d), pax); - u3_noun lan = u3nc(u3_nul, u3_mesa_encode_lane(lan_u)); - u3_noun dat = u3i_bytes(pac_u->pag_u.dat_u.len_w, - pac_u->pag_u.dat_u.fra_y); - - cad = u3nt(c3__mess, lan, - u3nq(c3__page, par, aut, dat)); + // u3_noun pax = _mesa_encode_path(pac_u->pag_u.nam_u.pat_s, + // (c3_y*)(pac_u->pag_u.nam_u.pat_c)); + // u3_noun par = u3nc(u3i_chubs(2, pac_u->pag_u.nam_u.her_d), pax); + // u3_noun lan = u3nc(u3_nul, u3_mesa_encode_lane(lan_u)); + // u3_noun dat = u3i_bytes(pac_u->pag_u.dat_u.len_w, + // pac_u->pag_u.dat_u.fra_y); + + // cad = u3nt(c3__mess_ser, lan, + // u3nq(c3__page, par, aut, dat)); + + u3_noun lan = u3_mesa_encode_lane(lan_u); + u3i_slab sab_u; + u3i_slab_init(&sab_u, 3, PACT_SIZE); + + // XX should just preserve input buffer + mesa_etch_pact(sab_u.buf_y, pac_u); + + cad = u3nt(c3__heer, lan, u3i_slab_mint(&sab_u)); + } // XX should put in cache on success @@ -1871,14 +1889,24 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) u3_noun wir = u3nc(c3__mesa, u3_nul); u3_noun cad; { - u3_noun pax = _mesa_encode_path(pac_u->pag_u.nam_u.pat_s, - (c3_y*)(pac_u->pag_u.nam_u.pat_c)); - u3_noun par = u3nc(u3i_chubs(2, pac_u->pag_u.nam_u.her_d), pax); + // u3_noun pax = _mesa_encode_path(pac_u->pag_u.nam_u.pat_s, + // (c3_y*)(pac_u->pag_u.nam_u.pat_c)); + // u3_noun par = u3nc(u3i_chubs(2, pac_u->pag_u.nam_u.her_d), pax); + // u3_noun lan = u3_nul; + // u3_noun aut = u3nc(c3y, 0); // XX s/b saved in request state + + // cad = u3nt(c3__mess_ser, lan, + // u3nq(c3__page, par, aut, dat)); u3_noun lan = u3_nul; - u3_noun aut = u3nc(c3y, 0); // XX s/b saved in request state - - cad = u3nt(c3__mess, lan, - u3nq(c3__page, par, aut, dat)); + u3i_slab sab_u; + u3i_slab_init(&sab_u, 3, (siz_w * req_u->tot_w) + 135); + pac_u->pag_u.dat_u.len_w = (siz_w * req_u->tot_w); + // memcpy(pac_u->pag_u.dat_u.fra_y, req_u->dat_y, pac_u->pag_u.dat_u.len_w); + pac_u->pag_u.dat_u.fra_y = req_u->dat_y; + // XX should just preserve input buffer + mesa_etch_pact(sab_u.buf_y, pac_u); + + cad = u3nt(c3__heer, lan, u3i_slab_mint(&sab_u)); } u3_auto_plan(&sam_u->car_u, @@ -1990,15 +2018,17 @@ _mesa_poke_bail(u3_ovum* egg_u, u3_noun lud) static void _mesa_hear_poke(u3_mesa_pict* pic_u, u3_lane* lan_u) { -#ifdef MESA_DEBUG - u3l_log("mesa: hear poke"); - // u3_assert(pac_u->hed_u.typ_y == PACT_POKE); -#endif + u3_mesa_pact* pac_u = &pic_u->pac_u; u3_mesa* sam_u = pic_u->sam_u; c3_d* her_d = pac_u->pek_u.nam_u.her_d; c3_o our_o = __( 0 == memcmp(her_d, sam_u->pir_u->who_d, sizeof(*her_d) * 2) ); +#ifdef MESA_DEBUG + u3l_log("mesa: hear poke"); + u3_assert(pac_u->hed_u.typ_y == PACT_POKE); +#endif + // XX forwarding wrong, need a PIT entry if ( c3n == our_o ) { u3_peer* per_u = _mesa_get_peer(sam_u, her_d); @@ -2051,18 +2081,6 @@ _mesa_hear_poke(u3_mesa_pict* pic_u, u3_lane* lan_u) u3_ovum_bail bal_f; void* ptr_v; - if ( 1 == pac_u->pok_u.dat_u.tot_w ) { - nes_f = bal_f = ptr_v = NULL; - _mesa_free_pict(pic_u); - } - else { - assert(pac_u->pok_u.dat_u.tot_w); - // XX check request state for *payload* (in-progress duplicate) - nes_f = _mesa_poke_news; - bal_f = _mesa_poke_bail; - ptr_v = pic_u; - } - // XX create PIT entry for ack u3_noun wir = u3nc(c3__mesa, u3_nul); @@ -2078,6 +2096,18 @@ _mesa_hear_poke(u3_mesa_pict* pic_u, u3_lane* lan_u) cad = u3nt(c3__heer, lan, u3i_slab_mint(&sab_u)); } + if ( 1 == pac_u->pok_u.dat_u.tot_w ) { + nes_f = bal_f = ptr_v = NULL; + _mesa_free_pict(pic_u); + } + else { + assert(pac_u->pok_u.dat_u.tot_w); + // XX check request state for *payload* (in-progress duplicate) + nes_f = _mesa_poke_news; + bal_f = _mesa_poke_bail; + ptr_v = pic_u; + } + u3_auto_peer( u3_auto_plan(&sam_u->car_u, u3_ovum_init(0, c3__mesa, wir, cad)), @@ -2100,7 +2130,9 @@ _mesa_hear(u3_mesa* sam_u, pic_u = c3_calloc(sizeof(u3_mesa_pict)); pic_u->sam_u = sam_u; + c3_w lin_w = mesa_sift_pact(&pic_u->pac_u, hun_y, len_w); + u3l_log("_mesa_hear len_w %u lin_w %u", len_w, lin_w); c3_free(hun_y); if ( lin_w == 0 ) { MESA_LOG(SERIAL) diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index aa464f5bb4..d06290578c 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -302,6 +302,7 @@ _mesa_sift_head(c3_y buf_y[8], u3_mesa_head* hed_u) } c3_w hed_w = _ames_sift_word(buf_y); + u3l_log("hed_w %x", hed_w); hed_u->nex_y = (hed_w >> 2) & 0x3; hed_u->pro_y = (hed_w >> 4) & 0x7; @@ -389,9 +390,9 @@ _mesa_sift_name(u3_mesa_name* nam_u, c3_y* buf_y, c3_w len_w) static c3_w _mesa_sift_data(u3_mesa_data* dat_u, c3_y* buf_y, c3_w len_w) { -#ifdef MESA_DEBUG - //u3l_log("mesa: sifting data %i", len_w); -#endif +// #ifdef MESA_DEBUG + u3l_log("mesa: sifting data %i", len_w); +// #endif c3_w cur_w = 0; u3_mesa_data_meta met_u; @@ -540,22 +541,27 @@ _mesa_sift_poke_pact(u3_mesa_poke_pact* pac_u, c3_y* buf_y, c3_w len_w) c3_w cur_w = 0, nex_w; // ack path if ( !(nex_w = _mesa_sift_name(&pac_u->nam_u, buf_y + cur_w, len_w)) ) { + u3l_log("_mesa_sift_poke_pact nex_w %u", nex_w); return 0; } cur_w += nex_w; // payload path if ( !(nex_w = _mesa_sift_name(&pac_u->pay_u, buf_y + cur_w, len_w)) ) { + u3l_log("_mesa_sift_poke_pact nex_w %u", nex_w); return 0; } cur_w += nex_w; // payload if ( !(nex_w = _mesa_sift_data(&pac_u->dat_u, buf_y + cur_w, len_w)) ) { + u3l_log("_mesa_sift_poke_pact nex_w %u", nex_w); return 0; } cur_w += nex_w; + u3l_log("_mesa_sift_poke_pact cur_w %u", cur_w); + return cur_w; } @@ -572,6 +578,8 @@ mesa_sift_pact(u3_mesa_pact* pac_u, c3_y* buf_y, c3_w len_w) buf_y += 8; len_w -= 8; + u3l_log("pac_u->hed_u.typ_y typ_y %u", pac_u->hed_u.typ_y); + switch ( pac_u->hed_u.typ_y ) { case PACT_PEEK: { res_w = _mesa_sift_peek_pact(&pac_u->pek_u, buf_y, len_w); @@ -707,27 +715,27 @@ _mesa_etch_data(c3_y* buf_y, u3_mesa_data* dat_u) u3_mesa_data_meta met_u; met_u.bot_y = safe_dec(_mesa_met3_w(dat_u->tot_w)); - + u3l_log("_mesa_etch_data tot_w %u", dat_u->tot_w); // XX met_u.aul_y = dat_u->aum_u.typ_e; met_u.aur_y = dat_u->aup_u.len_y; - + u3l_log("_mesa_etch_data tot_w %u", dat_u->tot_w); c3_y nel_y = _mesa_met3_w(dat_u->len_w); met_u.men_y = (3 >= nel_y) ? nel_y : 3; - + u3l_log("_mesa_etch_data tot_w %u", dat_u->tot_w); c3_y met_y = (met_u.bot_y & 0x3) << 0 ^ (met_u.aul_y & 0x3) << 2 ^ (met_u.aur_y & 0x3) << 4 ^ (met_u.men_y & 0x3) << 6; buf_y[cur_w] = met_y; cur_w++; - + u3l_log("_mesa_etch_data tot_w %u", dat_u->tot_w); c3_y tot_y = met_u.bot_y + 1; for (int i = 0; i < tot_y; i++ ) { buf_y[cur_w] = (dat_u->tot_w >> (8 * i)) & 0xFF; cur_w++; } - + u3l_log("_mesa_etch_data tot_w %u", dat_u->tot_w); switch ( dat_u->aum_u.typ_e ) { case AUTH_SIGN: { memcpy(buf_y + cur_w, dat_u->aum_u.sig_y, 64); @@ -741,7 +749,7 @@ _mesa_etch_data(c3_y* buf_y, u3_mesa_data* dat_u) default: break; } - + u3l_log("_mesa_etch_data tot_w %u", dat_u->tot_w); for ( int i = 0; i < dat_u->aup_u.len_y; i++ ) { memcpy(buf_y + cur_w, dat_u->aup_u.has_y[i], 32); cur_w += 32; @@ -751,12 +759,14 @@ _mesa_etch_data(c3_y* buf_y, u3_mesa_data* dat_u) buf_y[cur_w] = nel_y; cur_w++; } - + u3l_log("_mesa_etch_data cur_w %u", cur_w); memcpy(buf_y + cur_w, (c3_y*)&dat_u->len_w, nel_y); cur_w += nel_y; + u3l_log("_mesa_etch_data cur_w %u", cur_w); memcpy(buf_y + cur_w, dat_u->fra_y, dat_u->len_w); cur_w += dat_u->len_w; + u3l_log("_mesa_etch_data cur_w %u", cur_w); return cur_w; } @@ -775,7 +785,7 @@ _mesa_etch_page_pact(c3_y* buf_y, u3_mesa_page_pact* pac_u, u3_mesa_head* hed_u) return 0; } cur_w += nex_w; - + u3l_log("_mesa_etch_page_pact cur_w %u", cur_w); // XX hops return cur_w; @@ -904,10 +914,11 @@ c3_w mesa_etch_pact(c3_y* buf_y, u3_mesa_pact* pac_u) { c3_w siz_w = _mesa_size_pact(pac_u); - if ( siz_w > PACT_SIZE ) { - fprintf(stderr, "etch: would overflow %u\r\n", siz_w); - return 0; - } + u3l_log("mesa_etch_pact siz_w %u", siz_w); + // if ( siz_w > PACT_SIZE ) { + // fprintf(stderr, "etch: would overflow %u\r\n", siz_w); + // return 0; + // } u3_mesa_head* hed_u = &pac_u->hed_u; @@ -940,10 +951,11 @@ mesa_etch_pact(c3_y* buf_y, u3_mesa_pact* pac_u) hed_u->mug_w = u3r_mug_bytes(buf_y + cur_w, nex_w); hed_u->mug_w &= 0xFFFFF; + u3l_log("mesa_etch_pact mug_w %u", hed_u->mug_w);//u3m_bail(c3__bail); _mesa_etch_head(hed_u, buf_y); cur_w += nex_w; - + u3l_log("mesa_etch_pact cur_w %u", cur_w);//u3m_bail(c3__bail); assert( siz_w == cur_w ); return cur_w; From 42ecff5fa467241ab00234522c71bbb23e37ae86 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Thu, 4 Apr 2024 14:00:53 +0200 Subject: [PATCH 078/430] mesa: wip inject jumbo packet --- pkg/vere/io/mesa.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 8cf5a3a2d6..26b54cf7f3 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1228,7 +1228,7 @@ _mesa_queue_czar(u3_mesa* sam_u, u3_noun las, u3_noun pac) static void _mesa_ef_send(u3_mesa* sam_u, u3_noun las, u3_noun pac) { - u3m_p("pac", pac); + // u3m_p("pac", pac); las = _mesa_queue_czar(sam_u, las, u3k(pac)); c3_w len_w = u3r_met(3, pac); c3_y* buf_y = c3_calloc(len_w); @@ -1884,7 +1884,6 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) u3l_log("%u kilobytes took %f ms", req_u->tot_w, (now_d - sam_u->tim_d)/1000.0); c3_w siz_w = (1 << (pac_u->pag_u.nam_u.boq_y - 3)); u3_noun dat = u3i_bytes((siz_w * req_u->tot_w), req_u->dat_y); - _mesa_del_request(sam_u, &pac_u->pag_u.nam_u); u3_noun wir = u3nc(c3__mesa, u3_nul); u3_noun cad; @@ -1899,16 +1898,21 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) // u3nq(c3__page, par, aut, dat)); u3_noun lan = u3_nul; u3i_slab sab_u; - u3i_slab_init(&sab_u, 3, (siz_w * req_u->tot_w) + 135); + // u3i_slab_init(&sab_u, 3, PACT_SIZE); + // u3i_slab_init(&sab_u, 3, (siz_w * req_u->tot_w) + 135); + u3i_slab_init(&sab_u, 3, (PACT_SIZE - pac_u->pag_u.dat_u.len_w) + (siz_w * req_u->tot_w)); pac_u->pag_u.dat_u.len_w = (siz_w * req_u->tot_w); - // memcpy(pac_u->pag_u.dat_u.fra_y, req_u->dat_y, pac_u->pag_u.dat_u.len_w); - pac_u->pag_u.dat_u.fra_y = req_u->dat_y; + pac_u->pag_u.dat_u.fra_y = c3_realloc(pac_u->pag_u.dat_u.fra_y, pac_u->pag_u.dat_u.len_w); + memcpy(pac_u->pag_u.dat_u.fra_y, req_u->dat_y, pac_u->pag_u.dat_u.len_w); + // pac_u->pag_u.dat_u.fra_y = req_u->dat_y; // XX should just preserve input buffer mesa_etch_pact(sab_u.buf_y, pac_u); cad = u3nt(c3__heer, lan, u3i_slab_mint(&sab_u)); } + _mesa_del_request(sam_u, &pac_u->pag_u.nam_u); + u3_auto_plan(&sam_u->car_u, u3_ovum_init(0, c3__mesa, wir, cad)); } From 1113971a18327bc2a57726bca7af80c813b71da3 Mon Sep 17 00:00:00 2001 From: pkova Date: Sat, 6 Apr 2024 14:59:00 +0300 Subject: [PATCH 079/430] mesa: fix segfault caused by incorrect uv_timer_init --- pkg/vere/io/mesa.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 26b54cf7f3..7e3b8e8400 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -556,8 +556,9 @@ _mesa_put_request(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_pend_req* req_u) { u3_pend_req* new_u = req_u; if ( old_u == NULL ) { new_u = u3a_calloc(1, sizeof(u3_pend_req)); - // u3l_log("putting fresh req %p", new_u); + u3l_log("putting fresh req %p", new_u); memcpy(new_u, req_u, sizeof(u3_pend_req)); + uv_timer_init(u3L, &new_u->tim_u); } else { new_u = old_u; memcpy(new_u, req_u, sizeof(u3_pend_req)); @@ -1695,7 +1696,6 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) req_u->nex_w = (c3y == lin_o) ? 1 : 0; req_u->len_w = (c3y == lin_o) ? 1 : 0; - uv_timer_init(u3L, &req_u->tim_u); req_u->lef_w = 0; req_u->old_w = 0; req_u->ack_w = 0; From 3b1738ed0e9953d492d6c11fbcfe916c5c24b269 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Mon, 8 Apr 2024 14:58:24 -0400 Subject: [PATCH 080/430] vere: decrements/rectifies kelvins --- pkg/noun/jets/tree.c | 1 + pkg/vere/pier.c | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index e40fe03846..c51859a928 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -2621,6 +2621,7 @@ static u3j_core _d[] = { { "k140", 0, 0, _k140_d, _k140_ha, 0, (u3j_core*) 140, 0 }, { "k139", 0, 0, _k139_d, no_hashes, 0, (u3j_core*) 139, 0 }, { "k138", 0, 0, _k138_d, no_hashes, 0, (u3j_core*) 138, 0 }, + { "k137", 0, 0, _k138_d, no_hashes, 0, (u3j_core*) 137, 0 }, { "a50", 0, 0, _a50_d, _k140_ha, 0, (u3j_core*) c3__a50, 0 }, {} }; diff --git a/pkg/vere/pier.c b/pkg/vere/pier.c index 2894b9bf84..5f7b4593f5 100644 --- a/pkg/vere/pier.c +++ b/pkg/vere/pier.c @@ -675,7 +675,7 @@ _pier_wyrd_fail(u3_pier* pir_u, u3_ovum* egg_u, u3_noun lud) // XX organizing version constants // #define VERE_NAME "vere" -#define VERE_ZUSE 411 +#define VERE_ZUSE 410 #define VERE_LULL 322 /* _pier_wyrd_aver(): check for %wend effect and version downgrade. RETAIN @@ -815,7 +815,7 @@ _pier_wyrd_card(u3_pier* pir_u) u3_noun kel = u3nl(u3nc(c3__zuse, VERE_ZUSE), // XX from both king and serf? u3nc(c3__lull, VERE_LULL), // XX from both king and serf? u3nc(c3__arvo, 236), // XX from both king and serf? - u3nc(c3__hoon, 138), // god_u->hon_y + u3nc(c3__hoon, 137), // god_u->hon_y u3nc(c3__nock, 4), // god_u->noc_y u3_none); u3_noun wir = u3nc(c3__arvo, u3_nul); From 64ce5cf6f8871d77152e8597d605df5cbb311e71 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Mon, 8 Apr 2024 16:10:51 -0400 Subject: [PATCH 081/430] c3: adds macros for builtin bit counting --- pkg/c3/defs.h | 15 ++++++++++++++- pkg/noun/hashtable.c | 2 +- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/pkg/c3/defs.h b/pkg/c3/defs.h index bbf8d91ef7..6985edcf28 100644 --- a/pkg/c3/defs.h +++ b/pkg/c3/defs.h @@ -5,6 +5,7 @@ #include "portable.h" #include "types.h" +#include #include @@ -51,7 +52,19 @@ /* Bit counting. */ -# define c3_bits_word(w) ((w) ? (32 - __builtin_clz(w)) : 0) +#if (32 == (CHAR_BIT * __SIZEOF_INT__)) +# define c3_lz_w __builtin_clz +# define c3_tz_w __builtin_ctz +# define c3_pc_w __builtin_popcount +#elif (32 == (CHAR_BIT * __SIZEOF_LONG__)) +# define c3_lz_w __builtin_clzl +# define c3_tz_w __builtin_ctzl +# define c3_pc_w __builtin_popcountl +#else +# error "port me" +#endif + +# define c3_bits_word(w) ((w) ? (32 - c3_lz_w(w)) : 0) /* Min and max. */ diff --git a/pkg/noun/hashtable.c b/pkg/noun/hashtable.c index bc5d751b8c..a230286e89 100644 --- a/pkg/noun/hashtable.c +++ b/pkg/noun/hashtable.c @@ -54,7 +54,7 @@ u3h_new(void) static c3_w _ch_popcount(c3_w num_w) { - return __builtin_popcount(num_w); + return c3_pc_w(num_w); } /* _ch_buck_new(): create new bucket. From 1c3c415379a0cb64771c156fe64ba3c4f554baaf Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Tue, 9 Apr 2024 22:58:27 -0400 Subject: [PATCH 082/430] jets: stage new registrations for hoon 137 --- pkg/noun/jets/tree.c | 91 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 90 insertions(+), 1 deletion(-) diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index c51859a928..42f9f7dae6 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -2552,6 +2552,95 @@ u3j_core _k138_d[] = }; +static u3j_core _137_two_d[] = +{ { "tri", 3, 0, _138_tri_d, no_hashes, _140_tri_ho }, + + { "find", 7, _140_two_find_a, 0, no_hashes }, + { "flop", 7, _140_two_flop_a, 0, no_hashes }, + { "lent", 7, _140_two_lent_a, 0, no_hashes }, + { "levy", 7, _140_two_levy_a, 0, no_hashes }, + { "lien", 7, _140_two_lien_a, 0, no_hashes }, + { "murn", 7, _140_two_murn_a, 0, no_hashes }, + { "need", 7, _140_two_need_a, 0, no_hashes }, + { "mate", 7, _138_two_mate_a, 0, no_hashes }, + { "reap", 7, _140_two_reap_a, 0, no_hashes }, + { "reel", 7, _140_two_reel_a, 0, no_hashes }, + { "roll", 7, _140_two_roll_a, 0, no_hashes }, + { "skid", 7, _140_two_skid_a, 0, no_hashes }, + { "skim", 7, _140_two_skim_a, 0, no_hashes }, + { "skip", 7, _140_two_skip_a, 0, no_hashes }, + { "scag", 7, _140_two_scag_a, 0, no_hashes }, + { "slag", 7, _140_two_slag_a, 0, no_hashes }, + { "snag", 7, _140_two_snag_a, 0, no_hashes }, + { "sort", 7, _140_two_sort_a, 0, no_hashes }, + { "turn", 7, _140_two_turn_a, 0, no_hashes }, + { "weld", 7, _140_two_weld_a, 0, no_hashes }, + { "welp", 7, _140_two_welp_a, 0, no_hashes }, + { "zing", 7, _140_two_zing_a, 0, no_hashes }, + + { "bex", 7, _140_two_bex_a, 0, no_hashes }, + { "cat", 7, _140_two_cat_a, 0, no_hashes }, + { "can", 7, _140_two_can_a, 0, no_hashes }, + { "con", 7, _140_two_con_a, 0, no_hashes }, + { "cue", 7, _140_two_cue_a, 0, no_hashes }, + { "cut", 7, _140_two_cut_a, 0, no_hashes }, + { "dis", 7, _140_two_dis_a, 0, no_hashes }, + { "dor", 7, _140_two_dor_a, 0, no_hashes }, + { "end", 7, _140_two_end_a, 0, no_hashes }, + { "gor", 7, _140_two_gor_a, 0, no_hashes }, + { "jam", 7, _140_two_jam_a, 0, no_hashes }, + { "lsh", 7, _140_two_lsh_a, 0, no_hashes }, + { "mat", 7, _140_two_mat_a, 0, no_hashes }, + { "met", 7, _140_two_met_a, 0, no_hashes }, + { "mix", 7, _140_two_mix_a, 0, no_hashes }, + { "mor", 7, _140_two_mor_a, 0, no_hashes }, + { "mug", 7, _140_two_mug_a, 0, no_hashes }, + { "muk", 59, _140_two_muk_a, 0, no_hashes }, + { "rap", 7, _140_two_rap_a, 0, no_hashes }, + { "rep", 7, _140_two_rep_a, 0, no_hashes }, + { "rev", 7, _140_two_rev_a, 0, no_hashes }, + { "rip", 7, _140_two_rip_a, 0, no_hashes }, + { "rsh", 7, _140_two_rsh_a, 0, no_hashes }, + { "swp", 7, _140_two_swp_a, 0, no_hashes }, + { "rub", 7, _140_two_rub_a, 0, no_hashes }, + { "pow", 7, _140_two_pow_a, 0, no_hashes }, + { "sqt", 7, _140_two_sqt_a, 0, no_hashes }, + { "xeb", 7, _140_two_xeb_a, 0, no_hashes }, + + { "by", 7, 0, _138_two__by_d, no_hashes }, + { "in", 7, 0, _139_two__in_d, no_hashes }, + {} +}; + +static u3j_core _137_one_d[] = +{ { "two", 3, 0, _137_two_d, no_hashes }, + + { "add", 7, _140_one_add_a, 0, no_hashes }, + { "dec", 7, _140_one_dec_a, 0, no_hashes }, + { "div", 7, _140_one_div_a, 0, no_hashes }, + { "dvr", 7, _140_one_dvr_a, 0, no_hashes }, + { "gte", 7, _140_one_gte_a, 0, no_hashes }, + { "gth", 7, _140_one_gth_a, 0, no_hashes }, + { "lte", 7, _140_one_lte_a, 0, no_hashes }, + { "lth", 7, _140_one_lth_a, 0, no_hashes }, + { "max", 7, _140_one_max_a, 0, no_hashes }, + { "min", 7, _140_one_min_a, 0, no_hashes }, + { "mod", 7, _140_one_mod_a, 0, no_hashes }, + { "mul", 7, _140_one_mul_a, 0, no_hashes }, + { "sub", 7, _140_one_sub_a, 0, no_hashes }, + + { "cap", 7, _140_one_cap_a, 0, no_hashes }, + { "mas", 7, _140_one_mas_a, 0, no_hashes }, + { "peg", 7, _140_one_peg_a, 0, no_hashes }, + {} +}; + +u3j_core _k137_d[] = +{ { "one", 3, 0, _137_one_d, no_hashes }, + {} +}; + + // TODO: probably need different ha hashes static u3j_core _a50_two__by_d[] = @@ -2621,7 +2710,7 @@ static u3j_core _d[] = { { "k140", 0, 0, _k140_d, _k140_ha, 0, (u3j_core*) 140, 0 }, { "k139", 0, 0, _k139_d, no_hashes, 0, (u3j_core*) 139, 0 }, { "k138", 0, 0, _k138_d, no_hashes, 0, (u3j_core*) 138, 0 }, - { "k137", 0, 0, _k138_d, no_hashes, 0, (u3j_core*) 137, 0 }, + { "k137", 0, 0, _k137_d, no_hashes, 0, (u3j_core*) 137, 0 }, { "a50", 0, 0, _a50_d, _k140_ha, 0, (u3j_core*) c3__a50, 0 }, {} }; From 40d61d6d998b1d1d99f762e95fc9b458c3f6a7cb Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Mon, 8 Apr 2024 15:09:59 -0400 Subject: [PATCH 083/430] jets: add +rig --- pkg/noun/jets/c/rig.c | 82 +++++++++++++++++++++++++++++++++++++++++++ pkg/noun/jets/q.h | 3 ++ pkg/noun/jets/tree.c | 3 ++ pkg/noun/jets/w.h | 1 + 4 files changed, 89 insertions(+) create mode 100644 pkg/noun/jets/c/rig.c diff --git a/pkg/noun/jets/c/rig.c b/pkg/noun/jets/c/rig.c new file mode 100644 index 0000000000..7e5e7b958a --- /dev/null +++ b/pkg/noun/jets/c/rig.c @@ -0,0 +1,82 @@ +/// @file + +#include "jets/q.h" +#include "jets/w.h" + +#include "noun.h" + +c3_d +u3qc_rig_s(c3_g foq_g, + c3_w sep_w, + c3_g toq_g) +{ + c3_d sep_d = sep_w; + + if ( foq_g >= toq_g ) { + return sep_d << (foq_g - toq_g); + } + else { + c3_g dif_g = toq_g - foq_g; + + sep_d += (1 << dif_g) - 1; + return sep_d >> dif_g; + } +} + +u3_noun +u3qc_rig(u3_atom foq, + u3_atom sep, + u3_atom toq) +{ + if ( c3y == u3r_sing(foq, toq) ) { + return u3k(sep); + } + + if ( (c3y == u3a_is_cat(foq)) && (foq < 32) + && (c3y == u3a_is_cat(toq)) && (toq < 32) + && (c3y == u3a_is_cat(sep)) ) + { + c3_d sep_d = u3qc_rig_s((c3_g)foq, (c3_w)sep, (c3_g)toq); + return u3i_chub(sep_d); + } + + if ( c3y == u3qa_gth(foq, toq) ) { + u3_atom d = u3qa_sub(foq, toq); + u3_atom e = u3qc_lsh(0, d, sep); + u3z(d); + return e; + } + else { + u3_atom d = u3qa_sub(toq, foq); + u3_atom e = u3qc_rsh(0, d, sep); + u3_atom f = u3qc_end(0, d, sep); + + if ( f ) { + e = u3i_vint(e); + u3z(f); + } + + u3z(d); + return e; + } +} + +u3_noun +u3wc_rig(u3_noun cor) +{ + u3_atom boq, sep, vat; + { + u3_noun sam = u3h(u3t(cor)); + u3_noun bit = u3h(sam); + + if ( c3y == u3a_is_atom(bit) ) { + return 0; + } + + boq = u3x_atom(u3h(bit)); + sep = u3x_atom(u3t(bit)); + vat = u3x_atom(u3t(sam)); + } + + return u3qc_rig(boq, sep, vat); +} diff --git a/pkg/noun/jets/q.h b/pkg/noun/jets/q.h index fd7c5981fb..6ab31a29ab 100644 --- a/pkg/noun/jets/q.h +++ b/pkg/noun/jets/q.h @@ -71,11 +71,14 @@ u3_noun u3qc_rap(u3_atom, u3_noun); u3_noun u3qc_rep(u3_atom, u3_atom, u3_noun); u3_noun u3qc_rev(u3_atom, u3_atom, u3_atom); + u3_noun u3qc_rig(u3_atom, u3_atom, u3_atom); u3_noun u3qc_rip(u3_atom, u3_atom, u3_atom); u3_noun u3qc_rsh(u3_atom, u3_atom, u3_atom); u3_noun u3qc_swp(u3_atom, u3_atom); u3_noun u3qc_sqt(u3_atom); + c3_d u3qc_rig_s(c3_g, c3_w, c3_g); + u3_noun u3_po_find_prefix(c3_y one, c3_y two, c3_y three); u3_noun u3_po_find_suffix(c3_y one, c3_y two, c3_y three); void u3_po_to_prefix(u3_noun id, c3_y* a, c3_y* b, c3_y* c); diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index 42f9f7dae6..9f80a0a8a5 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -2552,6 +2552,8 @@ u3j_core _k138_d[] = }; +static u3j_harm _137_two_rig_a[] = {{".2", u3wc_rig, c3y}, {}}; + static u3j_core _137_two_d[] = { { "tri", 3, 0, _138_tri_d, no_hashes, _140_tri_ho }, @@ -2599,6 +2601,7 @@ static u3j_core _137_two_d[] = { "rap", 7, _140_two_rap_a, 0, no_hashes }, { "rep", 7, _140_two_rep_a, 0, no_hashes }, { "rev", 7, _140_two_rev_a, 0, no_hashes }, + { "rig", 7, _137_two_rig_a, 0, no_hashes }, { "rip", 7, _140_two_rip_a, 0, no_hashes }, { "rsh", 7, _140_two_rsh_a, 0, no_hashes }, { "swp", 7, _140_two_swp_a, 0, no_hashes }, diff --git a/pkg/noun/jets/w.h b/pkg/noun/jets/w.h index 34716971bd..ecec4f75bd 100644 --- a/pkg/noun/jets/w.h +++ b/pkg/noun/jets/w.h @@ -74,6 +74,7 @@ u3_noun u3wc_rap(u3_noun); u3_noun u3wc_rep(u3_noun); u3_noun u3wc_rev(u3_noun); + u3_noun u3wc_rig(u3_noun); u3_noun u3wc_rip(u3_noun); u3_noun u3wc_rsh(u3_noun); u3_noun u3wc_swp(u3_noun); From 26c6d41e0e524785250796573b2caa974fc9d4d5 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Mon, 8 Apr 2024 16:17:03 -0400 Subject: [PATCH 084/430] jets: add +clz --- pkg/noun/jets/c/clz.c | 75 +++++++++++++++++++++++++++++++++++++++++++ pkg/noun/jets/q.h | 1 + pkg/noun/jets/tree.c | 2 ++ pkg/noun/jets/w.h | 1 + 4 files changed, 79 insertions(+) create mode 100644 pkg/noun/jets/c/clz.c diff --git a/pkg/noun/jets/c/clz.c b/pkg/noun/jets/c/clz.c new file mode 100644 index 0000000000..f8da45a3ce --- /dev/null +++ b/pkg/noun/jets/c/clz.c @@ -0,0 +1,75 @@ +/// @file + +#include "jets/q.h" +#include "jets/w.h" + +#include "noun.h" + +u3_atom +u3qc_clz(u3_atom boq, u3_atom sep, u3_atom a) +{ + if ( !_(u3a_is_cat(boq)) || (boq >= 32) ) { + return u3m_bail(c3__fail); + } + + if ( !_(u3a_is_cat(sep)) ) { + return u3m_bail(c3__fail); + } + + c3_g boq_g = boq; + c3_w sep_w = sep; + c3_w tot_w = sep_w << boq_g; + + if ( (tot_w >> boq_g) != sep_w ) { + return u3m_bail(c3__fail); + } + + c3_w met_w = u3r_met(0, a); + + if ( met_w <= tot_w ) { + tot_w -= met_w; + return u3i_word(tot_w); + } + else { + c3_w wid_w = tot_w >> 5; + c3_w bit_w = tot_w & 31; + c3_w wor_w; + + if ( bit_w ) { + wor_w = u3r_word(wid_w, a); + wor_w &= (1 << bit_w) - 1; + + if ( wor_w ) { + return bit_w - (32 - c3_lz_w(wor_w)); + } + } + + while ( wid_w-- ) { + wor_w = u3r_word(wid_w, a); + + if ( wor_w ) { + bit_w += c3_lz_w(wor_w); + break; + } + + bit_w += 32; + } + + return u3i_word(bit_w); + } +} + +u3_noun +u3wc_clz(u3_noun cor) +{ + u3_atom boq, sep, vat; + { + u3_noun sam = u3h(u3t(cor)); + u3_noun bit = u3h(sam); + + u3x_bite(bit, &boq, &sep); + vat = u3x_atom(u3t(sam)); + } + + return u3qc_clz(boq, sep, vat); +} diff --git a/pkg/noun/jets/q.h b/pkg/noun/jets/q.h index 6ab31a29ab..2100653bd2 100644 --- a/pkg/noun/jets/q.h +++ b/pkg/noun/jets/q.h @@ -53,6 +53,7 @@ u3_noun u3qc_can(u3_atom, u3_noun); u3_noun u3qc_cap(u3_atom); u3_noun u3qc_cat(u3_atom, u3_atom, u3_atom); + u3_noun u3qc_clz(u3_atom, u3_atom, u3_atom); u3_noun u3qc_con(u3_atom, u3_atom); u3_noun u3qc_cut(u3_atom, u3_atom, u3_atom, u3_atom); u3_noun u3qc_dis(u3_atom, u3_atom); diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index 9f80a0a8a5..2f34c03095 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -2552,6 +2552,7 @@ u3j_core _k138_d[] = }; +static u3j_harm _137_two_clz_a[] = {{".2", u3wc_clz, c3y}, {}}; static u3j_harm _137_two_rig_a[] = {{".2", u3wc_rig, c3y}, {}}; static u3j_core _137_two_d[] = @@ -2583,6 +2584,7 @@ static u3j_core _137_two_d[] = { "bex", 7, _140_two_bex_a, 0, no_hashes }, { "cat", 7, _140_two_cat_a, 0, no_hashes }, { "can", 7, _140_two_can_a, 0, no_hashes }, + { "clz", 7, _137_two_clz_a, 0, no_hashes }, { "con", 7, _140_two_con_a, 0, no_hashes }, { "cue", 7, _140_two_cue_a, 0, no_hashes }, { "cut", 7, _140_two_cut_a, 0, no_hashes }, diff --git a/pkg/noun/jets/w.h b/pkg/noun/jets/w.h index ecec4f75bd..a406f616cd 100644 --- a/pkg/noun/jets/w.h +++ b/pkg/noun/jets/w.h @@ -55,6 +55,7 @@ u3_noun u3wc_can(u3_noun); u3_noun u3wc_cap(u3_noun); u3_noun u3wc_cat(u3_noun); + u3_noun u3wc_clz(u3_noun); u3_noun u3wc_con(u3_noun); u3_noun u3wc_cut(u3_noun); u3_noun u3wc_dis(u3_noun); From 4261fa708a5346e3846f207acd6a7b28f8275c64 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Mon, 8 Apr 2024 16:39:59 -0400 Subject: [PATCH 085/430] jets: add +ctz --- pkg/noun/jets/c/ctz.c | 34 ++++++++++++++++++++++++++++++++++ pkg/noun/jets/q.h | 1 + pkg/noun/jets/tree.c | 2 ++ pkg/noun/jets/w.h | 1 + 4 files changed, 38 insertions(+) create mode 100644 pkg/noun/jets/c/ctz.c diff --git a/pkg/noun/jets/c/ctz.c b/pkg/noun/jets/c/ctz.c new file mode 100644 index 0000000000..ff478902a5 --- /dev/null +++ b/pkg/noun/jets/c/ctz.c @@ -0,0 +1,34 @@ +/// @file + +#include "jets/q.h" +#include "jets/w.h" + +#include "noun.h" + +u3_atom +u3qc_ctz(u3_atom a) +{ + c3_w wor_w, i_w; + + if ( 0 == a ) { + return 0; + } + + do { + wor_w = u3r_word(i_w++, a); + } + while ( !wor_w ); + + { + c3_w bit_d = i_w - 1; + bit_d *= 32; + bit_d += c3_tz_w(wor_w); + return u3i_chub(bit_d); + } +} + +u3_noun +u3wc_ctz(u3_noun cor) +{ + return u3qc_ctz(u3x_atom(u3h(u3t(cor)))); +} diff --git a/pkg/noun/jets/q.h b/pkg/noun/jets/q.h index 2100653bd2..a542841250 100644 --- a/pkg/noun/jets/q.h +++ b/pkg/noun/jets/q.h @@ -55,6 +55,7 @@ u3_noun u3qc_cat(u3_atom, u3_atom, u3_atom); u3_noun u3qc_clz(u3_atom, u3_atom, u3_atom); u3_noun u3qc_con(u3_atom, u3_atom); + u3_noun u3qc_ctz(u3_atom); u3_noun u3qc_cut(u3_atom, u3_atom, u3_atom, u3_atom); u3_noun u3qc_dis(u3_atom, u3_atom); u3_noun u3qc_dor(u3_atom, u3_atom); diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index 2f34c03095..a50530cb3a 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -2553,6 +2553,7 @@ u3j_core _k138_d[] = static u3j_harm _137_two_clz_a[] = {{".2", u3wc_clz, c3y}, {}}; +static u3j_harm _137_two_ctz_a[] = {{".2", u3wc_ctz, c3y}, {}}; static u3j_harm _137_two_rig_a[] = {{".2", u3wc_rig, c3y}, {}}; static u3j_core _137_two_d[] = @@ -2586,6 +2587,7 @@ static u3j_core _137_two_d[] = { "can", 7, _140_two_can_a, 0, no_hashes }, { "clz", 7, _137_two_clz_a, 0, no_hashes }, { "con", 7, _140_two_con_a, 0, no_hashes }, + { "ctz", 7, _137_two_ctz_a, 0, no_hashes }, { "cue", 7, _140_two_cue_a, 0, no_hashes }, { "cut", 7, _140_two_cut_a, 0, no_hashes }, { "dis", 7, _140_two_dis_a, 0, no_hashes }, diff --git a/pkg/noun/jets/w.h b/pkg/noun/jets/w.h index a406f616cd..ed736dda11 100644 --- a/pkg/noun/jets/w.h +++ b/pkg/noun/jets/w.h @@ -57,6 +57,7 @@ u3_noun u3wc_cat(u3_noun); u3_noun u3wc_clz(u3_noun); u3_noun u3wc_con(u3_noun); + u3_noun u3wc_ctz(u3_noun); u3_noun u3wc_cut(u3_noun); u3_noun u3wc_dis(u3_noun); u3_noun u3wc_dor(u3_noun); From 2b4054247930cd1660b3e0a9fdfd69053fdb8ed7 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Mon, 8 Apr 2024 16:41:54 -0400 Subject: [PATCH 086/430] jets: add +ham --- pkg/noun/jets/c/ham.c | 27 +++++++++++++++++++++++++++ pkg/noun/jets/q.h | 1 + pkg/noun/jets/tree.c | 2 ++ pkg/noun/jets/w.h | 1 + 4 files changed, 31 insertions(+) create mode 100644 pkg/noun/jets/c/ham.c diff --git a/pkg/noun/jets/c/ham.c b/pkg/noun/jets/c/ham.c new file mode 100644 index 0000000000..776cf0cd6e --- /dev/null +++ b/pkg/noun/jets/c/ham.c @@ -0,0 +1,27 @@ +/// @file + +#include "jets/q.h" +#include "jets/w.h" + +#include "noun.h" + +u3_atom +u3qc_ham(u3_atom a) +{ + c3_w len_w = u3r_met(5, a); + c3_d pop_d = 0; + c3_w wor_w; + + for ( c3_w i_w = 0; i_w < len_w; i_w++ ) { + wor_w = u3r_word(i_w, a); + pop_d += c3_pc_w(wor_w); + } + + return u3i_chub(pop_d); +} + +u3_noun +u3wc_ham(u3_noun cor) +{ + return u3qc_ham(u3x_atom(u3h(u3t(cor)))); +} diff --git a/pkg/noun/jets/q.h b/pkg/noun/jets/q.h index a542841250..d78c4b2659 100644 --- a/pkg/noun/jets/q.h +++ b/pkg/noun/jets/q.h @@ -62,6 +62,7 @@ u3_noun u3qc_dvr(u3_atom, u3_atom); u3_noun u3qc_end(u3_atom, u3_atom, u3_atom); u3_noun u3qc_gor(u3_atom, u3_atom); + u3_noun u3qc_ham(u3_atom); u3_noun u3qc_lsh(u3_atom, u3_atom, u3_atom); u3_noun u3qc_mas(u3_atom); u3_noun u3qc_met(u3_atom, u3_atom); diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index a50530cb3a..731ecad609 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -2554,6 +2554,7 @@ u3j_core _k138_d[] = static u3j_harm _137_two_clz_a[] = {{".2", u3wc_clz, c3y}, {}}; static u3j_harm _137_two_ctz_a[] = {{".2", u3wc_ctz, c3y}, {}}; +static u3j_harm _137_two_ham_a[] = {{".2", u3wc_ham, c3y}, {}}; static u3j_harm _137_two_rig_a[] = {{".2", u3wc_rig, c3y}, {}}; static u3j_core _137_two_d[] = @@ -2594,6 +2595,7 @@ static u3j_core _137_two_d[] = { "dor", 7, _140_two_dor_a, 0, no_hashes }, { "end", 7, _140_two_end_a, 0, no_hashes }, { "gor", 7, _140_two_gor_a, 0, no_hashes }, + { "ham", 7, _137_two_ham_a, 0, no_hashes }, { "jam", 7, _140_two_jam_a, 0, no_hashes }, { "lsh", 7, _140_two_lsh_a, 0, no_hashes }, { "mat", 7, _140_two_mat_a, 0, no_hashes }, diff --git a/pkg/noun/jets/w.h b/pkg/noun/jets/w.h index ed736dda11..b5ea2f6a95 100644 --- a/pkg/noun/jets/w.h +++ b/pkg/noun/jets/w.h @@ -64,6 +64,7 @@ u3_noun u3wc_dvr(u3_noun); u3_noun u3wc_end(u3_noun); u3_noun u3wc_gor(u3_noun); + u3_noun u3wc_ham(u3_noun); u3_noun u3wc_lsh(u3_noun); u3_noun u3wc_mas(u3_noun); u3_noun u3wc_met(u3_noun); From acbf56da43d2aec3834a52b23d153d82a16b95f8 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Mon, 8 Apr 2024 20:12:23 -0400 Subject: [PATCH 087/430] jets: add +hew --- pkg/noun/jets/c/hew.c | 87 +++++++++++++++++++++++++++++++++++++++++++ pkg/noun/jets/q.h | 1 + pkg/noun/jets/tree.c | 8 ++++ pkg/noun/jets/w.h | 1 + 4 files changed, 97 insertions(+) create mode 100644 pkg/noun/jets/c/hew.c diff --git a/pkg/noun/jets/c/hew.c b/pkg/noun/jets/c/hew.c new file mode 100644 index 0000000000..be6b7a76bd --- /dev/null +++ b/pkg/noun/jets/c/hew.c @@ -0,0 +1,87 @@ +/// @file + +#include "jets/q.h" +#include "jets/w.h" + +#include "noun.h" + +static c3_w +_hew_in(c3_g a_g, + c3_w pos_w, + u3_atom vat, + u3_noun sam, + u3_noun* out) +{ + u3_noun h, t, *l, *r; + + while ( c3y == u3r_cell(sam, &h, &t) ) { + *out = u3i_defcons(&l, &r); + pos_w = _hew_in(a_g, pos_w, vat, h, l); + sam = t; + out = r; + } + + if ( !_(u3a_is_cat(sam)) ) { + return u3m_bail(c3__fail); + } + + if ( !sam ) { + *out = 0; + return pos_w; + } + else { + c3_w wid_w = (c3_w)sam; + c3_w new_w = pos_w + wid_w; + u3i_slab sab_u; + + if ( new_w < pos_w ) { + return u3m_bail(c3__fail); + } + + u3i_slab_init(&sab_u, a_g, wid_w); + u3r_chop(a_g, pos_w, wid_w, 0, sab_u.buf_w, vat); + + *out = u3i_slab_mint(&sab_u); + return new_w; + } +} + +u3_noun +u3qc_hew(u3_atom boq, + u3_atom sep, + u3_atom vat, + u3_noun sam) +{ + if ( !_(u3a_is_cat(boq)) || (boq >= 32) ) { + return u3m_bail(c3__fail); + } + + if ( !_(u3a_is_cat(sep)) ) { + return u3m_bail(c3__fail); + } + + u3_noun pro; + c3_w pos_w = _hew_in((c3_g)boq, (c3_w)sep, vat, sam, &pro); + return u3nt(pro, boq, u3i_word(pos_w)); +} + +u3_noun +u3wc_hew(u3_noun cor) +{ + u3_atom boq, sep, vat; + u3_noun sam; + { + u3_noun pay = u3t(cor); + u3_noun con = u3t(pay); + u3_noun gat = u3t(con); // outer gate + u3_noun cam = u3h(u3t(gat)); // outer sample + u3_noun d = u3h(con); + + boq = u3x_atom(u3h(d)); + sep = u3x_atom(u3t(d)); + vat = u3x_atom(u3t(cam)); + sam = u3h(pay); + } + + return u3qc_hew(boq, sep, vat, sam); +} diff --git a/pkg/noun/jets/q.h b/pkg/noun/jets/q.h index d78c4b2659..7be3ca4788 100644 --- a/pkg/noun/jets/q.h +++ b/pkg/noun/jets/q.h @@ -63,6 +63,7 @@ u3_noun u3qc_end(u3_atom, u3_atom, u3_atom); u3_noun u3qc_gor(u3_atom, u3_atom); u3_noun u3qc_ham(u3_atom); + u3_noun u3qc_hew(u3_atom, u3_atom, u3_atom, u3_noun); u3_noun u3qc_lsh(u3_atom, u3_atom, u3_atom); u3_noun u3qc_mas(u3_atom); u3_noun u3qc_met(u3_atom, u3_atom); diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index 731ecad609..5cc95d4124 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -2555,6 +2555,13 @@ u3j_core _k138_d[] = static u3j_harm _137_two_clz_a[] = {{".2", u3wc_clz, c3y}, {}}; static u3j_harm _137_two_ctz_a[] = {{".2", u3wc_ctz, c3y}, {}}; static u3j_harm _137_two_ham_a[] = {{".2", u3wc_ham, c3y}, {}}; + +static u3j_harm _137_two__hew_fun_a[] = {{".2", u3wc_hew, c3y}, {}}; +static u3j_core _137_two__hew_d[] = + { { "fun", 15, _137_two__hew_fun_a, 0, no_hashes }, + {} + }; + static u3j_harm _137_two_rig_a[] = {{".2", u3wc_rig, c3y}, {}}; static u3j_core _137_two_d[] = @@ -2596,6 +2603,7 @@ static u3j_core _137_two_d[] = { "end", 7, _140_two_end_a, 0, no_hashes }, { "gor", 7, _140_two_gor_a, 0, no_hashes }, { "ham", 7, _137_two_ham_a, 0, no_hashes }, + { "hew", 7, 0, _137_two__hew_d, no_hashes }, { "jam", 7, _140_two_jam_a, 0, no_hashes }, { "lsh", 7, _140_two_lsh_a, 0, no_hashes }, { "mat", 7, _140_two_mat_a, 0, no_hashes }, diff --git a/pkg/noun/jets/w.h b/pkg/noun/jets/w.h index b5ea2f6a95..bbb9d99a36 100644 --- a/pkg/noun/jets/w.h +++ b/pkg/noun/jets/w.h @@ -65,6 +65,7 @@ u3_noun u3wc_end(u3_noun); u3_noun u3wc_gor(u3_noun); u3_noun u3wc_ham(u3_noun); + u3_noun u3wc_hew(u3_noun); u3_noun u3wc_lsh(u3_noun); u3_noun u3wc_mas(u3_noun); u3_noun u3wc_met(u3_noun); From e08fca6aa0df82f53157dce07285cd78a745e6b2 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Mon, 8 Apr 2024 22:37:24 -0400 Subject: [PATCH 088/430] TMP set test flag on new jets --- pkg/noun/jets/tree.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index 5cc95d4124..fadd606352 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -2552,17 +2552,17 @@ u3j_core _k138_d[] = }; -static u3j_harm _137_two_clz_a[] = {{".2", u3wc_clz, c3y}, {}}; -static u3j_harm _137_two_ctz_a[] = {{".2", u3wc_ctz, c3y}, {}}; -static u3j_harm _137_two_ham_a[] = {{".2", u3wc_ham, c3y}, {}}; +static u3j_harm _137_two_clz_a[] = {{".2", u3wc_clz, c3n}, {}}; +static u3j_harm _137_two_ctz_a[] = {{".2", u3wc_ctz, c3n}, {}}; +static u3j_harm _137_two_ham_a[] = {{".2", u3wc_ham, c3n}, {}}; -static u3j_harm _137_two__hew_fun_a[] = {{".2", u3wc_hew, c3y}, {}}; +static u3j_harm _137_two__hew_fun_a[] = {{".2", u3wc_hew, c3n}, {}}; static u3j_core _137_two__hew_d[] = { { "fun", 15, _137_two__hew_fun_a, 0, no_hashes }, {} }; -static u3j_harm _137_two_rig_a[] = {{".2", u3wc_rig, c3y}, {}}; +static u3j_harm _137_two_rig_a[] = {{".2", u3wc_rig, c3n}, {}}; static u3j_core _137_two_d[] = { { "tri", 3, 0, _138_tri_d, no_hashes, _140_tri_ho }, From 545ecfdb710724b600aefe5da99e9803a9e76252 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Wed, 10 Apr 2024 15:22:49 +0200 Subject: [PATCH 089/430] mesa: wip batch --- pkg/vere/io/mesa.c | 87 +++++++++++++++++++++++++++++++++++++++-- pkg/vere/io/mesa/pact.c | 17 ++------ 2 files changed, 87 insertions(+), 17 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 7e3b8e8400..7c6b1b35a3 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -66,6 +66,9 @@ typedef struct _u3_mesa_stat { #define IN_FLIGHT 10 +// XX +#define MESA_HUNK 4096 + // pending interest sentinels #define MESA_ITEM 1 // cached item #define MESA_WAIT 2 // waiting on scry @@ -1462,6 +1465,28 @@ _name_to_scry(u3_mesa_name* nam_u) return res; } +static u3_noun +_name_to_batch_scry(u3_mesa_name* nam_u, c3_w lop_w, c3_w len_w) +{ + u3_noun rif = _dire_etch_ud(nam_u->rif_w); + u3_noun boq = _dire_etch_ud(nam_u->boq_y); + u3_noun fag = _dire_etch_ud(nam_u->fra_w); + u3_noun pax = _mesa_encode_path(nam_u->pat_s, (c3_y*)nam_u->pat_c); + + u3_noun lop = _dire_etch_ud(lop_w); + u3_noun len = _dire_etch_ud(len_w); + + u3_noun wer = nam_u->nit_o == c3y + ? u3nc(c3__init, pax) + : u3nt(nam_u->aut_o == c3y ? c3__auth : c3__data, fag, pax); + + u3_noun res = u3nc(c3__mess, u3nq(rif, c3__pact, boq, u3nc(c3__etch, wer))); + // [%hunk lop=@t len=@t pat=*] + u3_noun bat = u3nq(c3__hunk, lop, len, res); + + return bat; +} + /* * RETAIN */ @@ -1546,6 +1571,57 @@ _mesa_page_scry_cb(void* vod_p, u3_noun nun) // u3z(pax); } +/* + */ +static void +_mesa_page_scry_hunk_cb(void* vod_p, u3_noun nun) +{ + u3_mesa_pict* pic_u = vod_p; + u3_mesa_pact* pac_u = &pic_u->pac_u; + u3_mesa* sam_u = pic_u->sam_u; + //u3_noun pax = _mesa_path_with_fra(pac_u->pek_u.nam_u.pat_c, &fra_s); + + u3_weak hit = u3r_at(7, nun); + if ( u3_none == hit ) { + // TODO: mark as dead + //u3z(nun); + u3l_log("unbound"); + + } else { + u3_weak old = _mesa_get_cache(sam_u, &pac_u->pag_u.nam_u); + if ( old == u3_none ) { + u3l_log("bad"); + MESA_LOG(APATHY); + } else { + u3_noun tag; + u3_noun dat; + u3x_cell(u3k(old), &tag, &dat); + if ( MESA_WAIT == tag ) { + c3_y* buf_y; + // u3m_p("hit", u3a_is_cell(hit)); + c3_w len_w = _mesa_respond(pic_u, &buf_y, u3k(u3h(hit))); + _mesa_rout_bufs(sam_u, buf_y, len_w, u3k(u3t(dat))); + } + + c3_w len_w = pac_u->pek_u.nam_u.fra_w; + while ( u3_nul != hit ) { + // u3_noun key = u3nc(u3k(pax), u3i_word(lop_w)); + // u3h_put(sam_u->fin_s.sac_p, key, u3k(u3h(lis))); + + _mesa_put_cache(sam_u, &pac_u->pek_u.nam_u, u3nc(MESA_ITEM, u3k(u3h(hit)))); + // u3z(key); + + hit = u3t(hit); + len_w++; + pac_u->pek_u.nam_u.fra_w = len_w; + } + // u3z(old); + } + // u3z(hit); + } + // u3z(pax); +} + static void _mesa_hear_bail(u3_ovum* egg_u, u3_noun lud) { @@ -1812,8 +1888,8 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) u3i_slab_init(&sab_u, 3, PACT_SIZE); // XX should just preserve input buffer - mesa_etch_pact(sab_u.buf_y, pac_u); - + c3_w cur_w = mesa_etch_pact(sab_u.buf_y, pac_u); + // _log_buf(sab_u.buf_y, cur_w); cad = u3nt(c3__heer, lan, u3i_slab_mint(&sab_u)); } @@ -1868,6 +1944,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) } nex_u->pac_u.pek_u.nam_u.fra_w = nex_w + i; c3_w siz_w = mesa_etch_pact(buf_y, &nex_u->pac_u); + // _log_buf(buf_y, siz_w); if ( siz_w == 0 ) { u3l_log("failed to etch"); u3_assert( 0 ); @@ -1974,7 +2051,9 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) } else if ( c3y == our_o && tag == MESA_ITEM ) { // XX our_o redundant c3_y* buf_y; c3_w len_w = _mesa_respond(pic_u, &buf_y, u3k(dat)); + // _log_buf(buf_y, len_w); _mesa_send_buf(sam_u, lan_u, buf_y, len_w); + } else { u3l_log("mesa: weird case in cache, dropping"); } @@ -1984,12 +2063,12 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) } else { _mesa_add_lane_to_cache(sam_u, &pac_u->pek_u.nam_u, u3_nul, lan_u); // TODO: retrieve from namespace if ( c3y == our_o ) { - u3_noun sky = _name_to_scry(&pac_u->pek_u.nam_u); + u3_noun sky = _name_to_batch_scry(&pac_u->pek_u.nam_u, pac_u->pek_u.nam_u.fra_w, pac_u->pek_u.nam_u.fra_w + MESA_HUNK); u3_noun our = u3i_chubs(2, sam_u->car_u.pir_u->who_d); u3_noun bem = u3nc(u3nt(our, u3_nul, u3nc(c3__ud, 1)), sky); // only branch where we do not free pic_u - u3_pier_peek(sam_u->car_u.pir_u, u3_nul, u3k(u3nq(1, c3__beam, c3__mx, bem)), pic_u, _mesa_page_scry_cb); + u3_pier_peek(sam_u->car_u.pir_u, u3_nul, u3k(u3nq(1, c3__beam, c3__mx, bem)), pic_u, _mesa_page_scry_hunk_cb); } else { // XX unpossible _mesa_free_pict(pic_u); diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index d06290578c..1449a5e785 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -715,27 +715,22 @@ _mesa_etch_data(c3_y* buf_y, u3_mesa_data* dat_u) u3_mesa_data_meta met_u; met_u.bot_y = safe_dec(_mesa_met3_w(dat_u->tot_w)); - u3l_log("_mesa_etch_data tot_w %u", dat_u->tot_w); // XX met_u.aul_y = dat_u->aum_u.typ_e; met_u.aur_y = dat_u->aup_u.len_y; - u3l_log("_mesa_etch_data tot_w %u", dat_u->tot_w); c3_y nel_y = _mesa_met3_w(dat_u->len_w); met_u.men_y = (3 >= nel_y) ? nel_y : 3; - u3l_log("_mesa_etch_data tot_w %u", dat_u->tot_w); c3_y met_y = (met_u.bot_y & 0x3) << 0 ^ (met_u.aul_y & 0x3) << 2 ^ (met_u.aur_y & 0x3) << 4 ^ (met_u.men_y & 0x3) << 6; buf_y[cur_w] = met_y; cur_w++; - u3l_log("_mesa_etch_data tot_w %u", dat_u->tot_w); c3_y tot_y = met_u.bot_y + 1; for (int i = 0; i < tot_y; i++ ) { buf_y[cur_w] = (dat_u->tot_w >> (8 * i)) & 0xFF; cur_w++; } - u3l_log("_mesa_etch_data tot_w %u", dat_u->tot_w); switch ( dat_u->aum_u.typ_e ) { case AUTH_SIGN: { memcpy(buf_y + cur_w, dat_u->aum_u.sig_y, 64); @@ -749,7 +744,6 @@ _mesa_etch_data(c3_y* buf_y, u3_mesa_data* dat_u) default: break; } - u3l_log("_mesa_etch_data tot_w %u", dat_u->tot_w); for ( int i = 0; i < dat_u->aup_u.len_y; i++ ) { memcpy(buf_y + cur_w, dat_u->aup_u.has_y[i], 32); cur_w += 32; @@ -759,14 +753,11 @@ _mesa_etch_data(c3_y* buf_y, u3_mesa_data* dat_u) buf_y[cur_w] = nel_y; cur_w++; } - u3l_log("_mesa_etch_data cur_w %u", cur_w); memcpy(buf_y + cur_w, (c3_y*)&dat_u->len_w, nel_y); cur_w += nel_y; - u3l_log("_mesa_etch_data cur_w %u", cur_w); memcpy(buf_y + cur_w, dat_u->fra_y, dat_u->len_w); cur_w += dat_u->len_w; - u3l_log("_mesa_etch_data cur_w %u", cur_w); return cur_w; } @@ -785,7 +776,7 @@ _mesa_etch_page_pact(c3_y* buf_y, u3_mesa_page_pact* pac_u, u3_mesa_head* hed_u) return 0; } cur_w += nex_w; - u3l_log("_mesa_etch_page_pact cur_w %u", cur_w); + // XX hops return cur_w; @@ -914,7 +905,7 @@ c3_w mesa_etch_pact(c3_y* buf_y, u3_mesa_pact* pac_u) { c3_w siz_w = _mesa_size_pact(pac_u); - u3l_log("mesa_etch_pact siz_w %u", siz_w); + // if ( siz_w > PACT_SIZE ) { // fprintf(stderr, "etch: would overflow %u\r\n", siz_w); // return 0; @@ -951,11 +942,11 @@ mesa_etch_pact(c3_y* buf_y, u3_mesa_pact* pac_u) hed_u->mug_w = u3r_mug_bytes(buf_y + cur_w, nex_w); hed_u->mug_w &= 0xFFFFF; - u3l_log("mesa_etch_pact mug_w %u", hed_u->mug_w);//u3m_bail(c3__bail); + _mesa_etch_head(hed_u, buf_y); cur_w += nex_w; - u3l_log("mesa_etch_pact cur_w %u", cur_w);//u3m_bail(c3__bail); + assert( siz_w == cur_w ); return cur_w; From 82afa964f15aef99d4004395dbe376399a8f1b19 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Wed, 10 Apr 2024 14:52:04 -0400 Subject: [PATCH 090/430] jets: stage the rest of the core declarations for %137 --- pkg/noun/jets/tree.c | 101 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 100 insertions(+), 1 deletion(-) diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index fadd606352..c7482d7c5f 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -2552,6 +2552,105 @@ u3j_core _k138_d[] = }; +static u3j_core _137_lull_d[] = + { {} + }; + +static u3j_core _137_hex_d[] = +{ { "lull", 3, 0, _137_lull_d, no_hashes }, + + { "lore", 63, _140_hex_lore_a, 0, no_hashes }, + { "leer", 63, _140_hex_leer_a, 0, no_hashes }, + { "loss", 63, _140_hex_loss_a, 0, no_hashes }, + { "lune", 127, _140_hex_lune_a, 0, no_hashes }, + + { "coed", 63, 0, _140_hex_coed_d, no_hashes }, + { "aes", 31, 0, _140_hex_aes_d, no_hashes }, + + { "hmac", 63, 0, _140_hex_hmac_d, no_hashes }, + { "argon", 31, 0, _140_hex_argon_d, no_hashes }, + { "blake", 31, 0, _138_hex_blake_d, no_hashes }, + { "kecc", 31, 0, _140_hex_kecc_d, no_hashes }, + { "ripemd", 31, 0, _140_hex_ripe_d, no_hashes }, + { "scr", 31, 0, _140_hex_scr_d, no_hashes }, + { "secp", 6, 0, _140_hex_secp_d, no_hashes }, + { "mimes", 31, 0, _140_hex_mimes_d, no_hashes }, + { "json", 31, 0, _139_hex_json_d, no_hashes }, + {} +}; + +static u3j_core _137_pen_d[] = +{ { "hex", 7, 0, _137_hex_d, no_hashes }, + + { "cell", 7, _140_pen_cell_a, 0, no_hashes }, + { "comb", 7, _140_pen_comb_a, 0, no_hashes }, + { "cons", 7, _140_pen_cons_a, 0, no_hashes }, + { "core", 7, _140_pen_core_a, 0, no_hashes }, + { "face", 7, _140_pen_face_a, 0, no_hashes }, + { "fitz", 7, _140_pen_fitz_a, 0, no_hashes }, + { "fork", 7, _140_pen_fork_a, 0, no_hashes }, + { "look", 7, _140_pen_look_a, 0, no_hashes }, + { "loot", 7, _140_pen_loot_a, 0, no_hashes }, + { "ut", 15, 0, _140_pen__ut_d, no_hashes, _140_pen__ut_ho }, + {} +}; + +static u3j_core _137_qua_d[] = +{ { "pen", 3, 0, _137_pen_d, no_hashes, _140_pen_ho }, + + { "po", 7, 0, _140_qua__po_d, no_hashes }, + + { "trip", 7, _140_qua_trip_a, 0, no_hashes }, + + { "bend", 7, 0, _140_qua__bend_d, no_hashes }, + { "cold", 7, 0, _140_qua__cold_d, no_hashes }, + { "comp", 7, 0, _140_qua__comp_d, no_hashes }, + { "cook", 7, 0, _140_qua__cook_d, no_hashes }, + { "easy", 7, 0, _140_qua__easy_d, no_hashes }, + { "glue", 7, 0, _140_qua__glue_d, no_hashes }, + { "here", 7, 0, _140_qua__here_d, no_hashes }, + { "just", 7, 0, _140_qua__just_d, no_hashes }, + { "mask", 7, 0, _140_qua__mask_d, no_hashes }, + { "shim", 7, 0, _140_qua__shim_d, no_hashes }, + { "stag", 7, 0, _140_qua__stag_d, no_hashes }, + { "stew", 7, 0, _140_qua__stew_d, no_hashes }, + { "stir", 7, 0, _140_qua__stir_d, no_hashes }, + + { "pfix", 7, _140_qua_pfix_a, 0, no_hashes }, + { "plug", 7, _140_qua_plug_a, 0, no_hashes }, + { "pose", 7, _140_qua_pose_a, 0, no_hashes }, + { "sfix", 7, _140_qua_sfix_a, 0, no_hashes }, + + { "mink", 7, _140_qua_mink_a, 0, no_hashes }, + { "mole", 7, _140_qua_mole_a, 0, no_hashes }, + { "mule", 7, _140_qua_mule_a, 0, no_hashes }, + + { "scot", 7, _140_qua_scot_a, 0, no_hashes }, + { "scow", 7, _140_qua_scow_a, 0, no_hashes }, + { "slaw", 7, _140_qua_slaw_a, 0, no_hashes }, + {} +}; + +static u3j_core _137_tri_d[] = +{ { "qua", 3, 0, _137_qua_d, no_hashes, _140_qua_ho }, + + { "cofl", 7, 0, _140_tri__cofl_d, no_hashes }, + { "rd", 7, 0, _140_tri__rd_d, no_hashes }, + { "rs", 7, 0, _140_tri__rs_d, no_hashes }, + { "rq", 7, 0, _140_tri__rq_d, no_hashes }, + { "rh", 7, 0, _140_tri__rh_d, no_hashes }, + { "og", 7, 0, _140_tri__og_d, no_hashes }, + + { "sha", 7, 0, _140_tri__sha_d, no_hashes }, + { "shax", 7, _140_tri_shax_a, 0, no_hashes }, + { "shay", 7, _140_tri_shay_a, 0, no_hashes }, + { "shas", 7, _140_tri_shas_a, 0, no_hashes }, + { "shal", 7, _140_tri_shal_a, 0, no_hashes }, + + { "ob", 3, 0, _140_ob_d, no_hashes, _140_ob_ho }, + {} +}; + static u3j_harm _137_two_clz_a[] = {{".2", u3wc_clz, c3n}, {}}; static u3j_harm _137_two_ctz_a[] = {{".2", u3wc_ctz, c3n}, {}}; static u3j_harm _137_two_ham_a[] = {{".2", u3wc_ham, c3n}, {}}; @@ -2565,7 +2664,7 @@ static u3j_core _137_two__hew_d[] = static u3j_harm _137_two_rig_a[] = {{".2", u3wc_rig, c3n}, {}}; static u3j_core _137_two_d[] = -{ { "tri", 3, 0, _138_tri_d, no_hashes, _140_tri_ho }, +{ { "tri", 3, 0, _137_tri_d, no_hashes, _140_tri_ho }, { "find", 7, _140_two_find_a, 0, no_hashes }, { "flop", 7, _140_two_flop_a, 0, no_hashes }, From bf1ba91eb5ad0d2c316c2ee0dc100fa71cacaa95 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Mon, 8 Apr 2024 20:34:24 -0400 Subject: [PATCH 091/430] jets: adds $plot (+fax and +mes) --- pkg/noun/jets/g/plot.c | 364 +++++++++++++++++++++++++++++++++++++++++ pkg/noun/jets/q.h | 6 + pkg/noun/jets/tree.c | 12 +- pkg/noun/jets/w.h | 5 + 4 files changed, 386 insertions(+), 1 deletion(-) create mode 100644 pkg/noun/jets/g/plot.c diff --git a/pkg/noun/jets/g/plot.c b/pkg/noun/jets/g/plot.c new file mode 100644 index 0000000000..18c8ab03b7 --- /dev/null +++ b/pkg/noun/jets/g/plot.c @@ -0,0 +1,364 @@ +/// @file + +#include "jets/q.h" +#include "jets/w.h" + +#include "noun.h" + +// XX optimize +// +static c3_w +_met_plat_m(c3_g a_g, c3_w fum_w, c3_w met_w, u3_atom vat) +{ + c3_w len_w, wor_w; + + { + u3i_slab sab_u; + u3i_slab_init(&sab_u, a_g, met_w); + u3r_chop(a_g, fum_w, met_w, 0, sab_u.buf_w, vat); + + len_w = sab_u.len_w; + + while ( len_w && !sab_u.buf_w[len_w - 1] ) { + len_w--; + } + + wor_w = !len_w ? 0 : sab_u.buf_w[len_w - 1]; + + u3i_slab_free(&sab_u); + } + + + if ( !len_w ) { + return 0; + } + + { + c3_w gal_w = len_w - 1; + c3_w daz_w = wor_w; + c3_y a_y = a_g; + + // inlined from u3r_met + if (a_y < 5) { + c3_y max_y = (1 << a_y) - 1; + c3_y gow_y = 5 - a_y; + + if (gal_w > ((UINT32_MAX - (32 + max_y)) >> gow_y)) { + return u3m_bail(c3__fail); + } + + return (gal_w << gow_y) + + ((c3_bits_word(daz_w) + max_y) + >> a_y); + } + + { + c3_y gow_y = (a_y - 5); + return ((gal_w + 1) + ((1 << gow_y) - 1)) >> gow_y; + } + } +} + +static c3_w +_met_list(c3_g a_g, + c3_w sep_w, + u3_noun b_p); + +static c3_w +_met_pair(c3_g* las_g, + c3_w sep_w, + u3_noun a_p, + u3_noun b_p, + c3_g* new_g) +{ + c3_g res_g, a_g; + + while ( c3y == u3a_is_cell(a_p) ) { + sep_w = _met_pair(las_g, sep_w, u3h(a_p), u3t(a_p), &res_g); + las_g = &res_g; + a_p = u3h(b_p); + b_p = u3t(b_p); + } + + if ( !_(u3a_is_cat(a_p)) || (a_p >= 32) ) { + return u3m_bail(c3__fail); + } + + a_g = (c3_g)a_p; + + if ( las_g && (a_g != *las_g) ) { + sep_w = u3qc_rig_s(*las_g, sep_w, a_g); // XX overflow + } + + *new_g = a_g; + return _met_list(a_g, sep_w, b_p); +} + +static c3_w +_met_list(c3_g a_g, + c3_w sep_w, + u3_noun b_p) +{ + if ( u3_nul != b_p ) { + c3_w met_w; + u3_noun i, t = b_p; + + do { + u3x_cell(t, &i, &t); + + // ?@ i.b.p + if ( c3y == u3a_is_atom(i) ) { + met_w = u3r_met(a_g, i); + sep_w += met_w; // XX overflow + } + else { + u3_noun i_i, t_i; + u3x_cell(i, &i_i, &t_i); + + // ?=(@ -.i.b.p) + if ( c3y == u3a_is_atom(i_i) ) { + if ( !_(u3a_is_cat(i_i)) ) { + return u3m_bail(c3__fail); + } + + met_w = (c3_w)i_i; + sep_w += met_w; // XX overflow + } + else { + u3_noun l, r; + c3_o cel_o = u3r_cell(i_i, &l, &r); + + // ?=([%c ~] -.i.b.p) + if ( (c3y == cel_o) && ('c' == l) && (u3_nul == r) ) { + u3_atom p_p_t_i = u3x_atom(u3h(u3h(t_i))); + u3_atom q_p_t_i = u3x_atom(u3t(u3h(t_i))); + u3_atom q_t_i = u3x_atom(u3t(t_i)); + + if ( !_(u3a_is_cat(p_p_t_i)) + || !_(u3a_is_cat(q_p_t_i)) + || !_(u3a_is_cat(q_t_i)) ) + { + return u3m_bail(c3__fail); + } + + met_w = (c3_w)q_p_t_i; + sep_w += met_w; // XX overflow + } + // ?=([%m ~] -.i.b.p) + else if ( (c3y == cel_o) && ('m' == l) && (u3_nul == r) ) { + u3_atom p_p_t_i = u3x_atom(u3h(u3h(t_i))); + u3_atom q_p_t_i = u3x_atom(u3t(u3h(t_i))); + u3_atom q_t_i = u3x_atom(u3t(t_i)); + + if ( !_(u3a_is_cat(p_p_t_i)) + || !_(u3a_is_cat(q_p_t_i)) + || !_(u3a_is_cat(q_t_i)) ) + { + return u3m_bail(c3__fail); + } + + met_w = _met_plat_m(a_g, (c3_w)p_p_t_i, (c3_w)q_p_t_i, q_t_i); + sep_w += met_w; // XX overflow + } + // ?=([%s ~] -.i.b.p) (assumed) + else { + c3_g new_g; + u3x_cell(t_i, &l, &r); + + sep_w = _met_pair(&a_g, sep_w, l, r, &new_g); + + if ( new_g != a_g ) { + sep_w = u3qc_rig_s(new_g, sep_w, a_g); // XX overflow + } + } + } + } + } + while ( u3_nul != t ); + } + + return sep_w; +} + +static c3_w +_fax_list(u3i_slab* sab_u, + c3_g a_g, + c3_w sep_w, + u3_noun b_p); + +static c3_w +_fax_pair(u3i_slab* sab_u, + c3_g* las_g, + c3_w sep_w, + u3_noun a_p, + u3_noun b_p, + c3_g* new_g) +{ + c3_g res_g, a_g; + + while ( c3y == u3a_is_cell(a_p) ) { + sep_w = _fax_pair(sab_u, las_g, sep_w, u3h(a_p), u3t(a_p), &res_g); + las_g = &res_g; + a_p = u3h(b_p); + b_p = u3t(b_p); + } + + if ( !_(u3a_is_cat(a_p)) || (a_p >= 32) ) { + return u3m_bail(c3__fail); + } + + a_g = (c3_g)a_p; + + if ( las_g && (a_g != *las_g) ) { + sep_w = u3qc_rig_s(*las_g, sep_w, a_g); // XX overflow + } + + *new_g = a_g; + return _fax_list(sab_u, a_g, sep_w, b_p); +} + +static c3_w +_fax_list(u3i_slab* sab_u, + c3_g a_g, + c3_w sep_w, + u3_noun b_p) +{ + if ( u3_nul != b_p ) { + c3_w met_w; + u3_noun i, t = b_p; + + do { + u3x_cell(t, &i, &t); + + // ?@ i.b.p + if ( c3y == u3a_is_atom(i) ) { + met_w = u3r_met(a_g, i); + + u3r_chop(a_g, 0, met_w, sep_w, sab_u->buf_w, i); + + sep_w += met_w; // XX overflow + } + else { + u3_noun i_i, t_i; + u3x_cell(i, &i_i, &t_i); + + // ?=(@ -.i.b.p) + if ( c3y == u3a_is_atom(i_i) ) { + if ( !_(u3a_is_cat(i_i)) ) { + return u3m_bail(c3__fail); + } + + met_w = (c3_w)i_i; + + u3r_chop(a_g, 0, met_w, sep_w, sab_u->buf_w, u3x_atom(t_i)); + + sep_w += met_w; // XX overflow + } + else { + u3_noun l, r; + c3_o cel_o = u3r_cell(i_i, &l, &r); + + // ?=([%c ~] -.i.b.p) + if ( (c3y == cel_o) && ('c' == l) && (u3_nul == r) ) { + u3_atom p_p_t_i = u3x_atom(u3h(u3h(t_i))); + u3_atom q_p_t_i = u3x_atom(u3t(u3h(t_i))); + u3_atom q_t_i = u3x_atom(u3t(t_i)); + + if ( !_(u3a_is_cat(p_p_t_i)) + || !_(u3a_is_cat(q_p_t_i)) + || !_(u3a_is_cat(q_t_i)) ) + { + return u3m_bail(c3__fail); + } + + met_w = (c3_w)q_p_t_i; + + u3r_chop(a_g, (c3_w)p_p_t_i, met_w, sep_w, sab_u->buf_w, q_t_i); + + sep_w += met_w; // XX overflow + } + // ?=([%m ~] -.i.b.p) + else if ( (c3y == cel_o) && ('m' == l) && (u3_nul == r) ) { + u3_atom p_p_t_i = u3x_atom(u3h(u3h(t_i))); + u3_atom q_p_t_i = u3x_atom(u3t(u3h(t_i))); + u3_atom q_t_i = u3x_atom(u3t(t_i)); + + if ( !_(u3a_is_cat(p_p_t_i)) + || !_(u3a_is_cat(q_p_t_i)) + || !_(u3a_is_cat(q_t_i)) ) + { + return u3m_bail(c3__fail); + } + + met_w = _met_plat_m(a_g, (c3_w)p_p_t_i, (c3_w)q_p_t_i, q_t_i); + + u3r_chop(a_g, (c3_w)p_p_t_i, met_w, sep_w, sab_u->buf_w, q_t_i); + + sep_w += met_w; // XX overflow + } + // ?=([%s ~] -.i.b.p) (assumed) + else { + c3_g new_g; + u3x_cell(t_i, &l, &r); + + sep_w = _fax_pair(sab_u, &a_g, sep_w, l, r, &new_g); + + if ( new_g != a_g ) { + sep_w = u3qc_rig_s(new_g, sep_w, a_g); // XX overflow + } + } + } + } + } + while ( u3_nul != t ); + } + + return sep_w; +} + +u3_noun +u3qg_plot_met(u3_noun a_p, u3_noun b_p) +{ + c3_g out_g; + c3_w sep_w = _met_pair(NULL, 0, a_p, b_p, &out_g); + + return u3nc(out_g, u3i_word(sep_w)); +} + +u3_noun +u3wg_plot_met(u3_noun cor) +{ + u3_noun a_p, b_p; + { + u3_noun sam = u3h(u3t(cor)); + a_p = u3h(sam); + b_p = u3t(sam); + } + return u3qg_plot_met(a_p, b_p); +} + +u3_noun +u3qg_plot_fax(u3_noun a_p, u3_noun b_p) +{ + c3_g out_g; + c3_w sep_w = _met_pair(NULL, 0, a_p, b_p, &out_g); + u3i_slab sab_u; + + u3i_slab_init(&sab_u, out_g, sep_w); + + _fax_pair(&sab_u, NULL, 0, a_p, b_p, &out_g); + + return u3nt(u3i_slab_mint(&sab_u), out_g, u3i_word(sep_w)); +} + +u3_noun +u3wg_plot_fax(u3_noun cor) +{ + u3_noun a_p, b_p; + { + u3_noun sam = u3h(u3t(cor)); + a_p = u3h(sam); + b_p = u3t(sam); + } + return u3qg_plot_fax(a_p, b_p); +} diff --git a/pkg/noun/jets/q.h b/pkg/noun/jets/q.h index 7be3ca4788..53fed774e1 100644 --- a/pkg/noun/jets/q.h +++ b/pkg/noun/jets/q.h @@ -258,5 +258,11 @@ void u3qf_test(const c3_c*, u3_noun); + + /** Tier 6. + **/ + u3_noun u3qg_plot_fax(u3_noun, u3_noun); + u3_noun u3qg_plot_met(u3_noun, u3_noun); + #endif /* ifndef U3_JETS_Q_H */ diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index c7482d7c5f..a4eef0526e 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -2552,8 +2552,18 @@ u3j_core _k138_d[] = }; +static u3j_harm _137_lull_plot_fax_a[] = {{".2", u3wg_plot_fax, c3y}, {}}; +static u3j_harm _137_lull_plot_met_a[] = {{".2", u3wg_plot_met, c3y}, {}}; + +static u3j_core _137_lull_plot_d[] = + { { "fax", 7, _137_lull_plot_fax_a, 0, no_hashes }, + { "met", 7, _137_lull_plot_met_a, 0, no_hashes }, + {} + }; + static u3j_core _137_lull_d[] = - { {} + { { "plot", 31, 0, _137_lull_plot_d, no_hashes }, + {} }; static u3j_core _137_hex_d[] = diff --git a/pkg/noun/jets/w.h b/pkg/noun/jets/w.h index bbb9d99a36..16fe3f609d 100644 --- a/pkg/noun/jets/w.h +++ b/pkg/noun/jets/w.h @@ -340,5 +340,10 @@ u3_noun u3wfu_repo(u3_noun); u3_noun u3wfu_rest(u3_noun); + /** Tier 7. + **/ + u3_noun u3wg_plot_fax(u3_noun); + u3_noun u3wg_plot_met(u3_noun); + #endif /* ifndef U3_JETS_W_H */ From 20db29b10c6d391acb26448deff46f363241773c Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Fri, 12 Apr 2024 10:17:28 +0200 Subject: [PATCH 092/430] mesa: rewrite fragment in jumbo packet to last --- pkg/vere/io/mesa.c | 22 ++++++++++++++-------- pkg/vere/io/mesa/pact.c | 13 ++++++------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 7c6b1b35a3..0aebc28e58 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1978,10 +1978,16 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) // u3i_slab_init(&sab_u, 3, PACT_SIZE); // u3i_slab_init(&sab_u, 3, (siz_w * req_u->tot_w) + 135); u3i_slab_init(&sab_u, 3, (PACT_SIZE - pac_u->pag_u.dat_u.len_w) + (siz_w * req_u->tot_w)); + pac_u->pag_u.dat_u.len_w = (siz_w * req_u->tot_w); pac_u->pag_u.dat_u.fra_y = c3_realloc(pac_u->pag_u.dat_u.fra_y, pac_u->pag_u.dat_u.len_w); memcpy(pac_u->pag_u.dat_u.fra_y, req_u->dat_y, pac_u->pag_u.dat_u.len_w); - // pac_u->pag_u.dat_u.fra_y = req_u->dat_y; + + + u3l_log("last frag %u", pic_u->pac_u.pag_u.nam_u.fra_w); + // this could be a retry, so we just rewrite the fragment to be the last one + // + pic_u->pac_u.pag_u.nam_u.fra_w = req_u->tot_w - 1; // XX should just preserve input buffer mesa_etch_pact(sab_u.buf_y, pac_u); @@ -2012,7 +2018,7 @@ static void _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) { #ifdef MESA_DEBUG - u3l_log("mesa: hear peek"); + // u3l_log("mesa: hear peek"); // u3_assert(pac_u->hed_u.typ_y == PACT_PEEK); #endif u3_mesa_pact* pac_u = &pic_u->pac_u; @@ -2108,7 +2114,7 @@ _mesa_hear_poke(u3_mesa_pict* pic_u, u3_lane* lan_u) c3_o our_o = __( 0 == memcmp(her_d, sam_u->pir_u->who_d, sizeof(*her_d) * 2) ); #ifdef MESA_DEBUG - u3l_log("mesa: hear poke"); + // u3l_log("mesa: hear poke"); u3_assert(pac_u->hed_u.typ_y == PACT_POKE); #endif @@ -2148,13 +2154,13 @@ _mesa_hear_poke(u3_mesa_pict* pic_u, u3_lane* lan_u) if ( pac_u->hed_u.hop_y == 0 ) { new_o = c3y; _hear_peer(sam_u, per_u, *lan_u, dir_o); - u3l_log("learnt lane"); + // u3l_log("learnt lane"); } else { - u3l_log("received forwarded poke"); + // u3l_log("received forwarded poke"); } if ( new_o == c3y ) { - u3l_log("new lane is direct %c", c3y == dir_o ? 'y' : 'n'); - _log_lane(lan_u); + // u3l_log("new lane is direct %c", c3y == dir_o ? 'y' : 'n'); + // _log_lane(lan_u); } _mesa_put_peer(sam_u, pac_u->pok_u.pay_u.her_d, per_u); @@ -2215,7 +2221,7 @@ _mesa_hear(u3_mesa* sam_u, pic_u->sam_u = sam_u; c3_w lin_w = mesa_sift_pact(&pic_u->pac_u, hun_y, len_w); - u3l_log("_mesa_hear len_w %u lin_w %u", len_w, lin_w); + c3_free(hun_y); if ( lin_w == 0 ) { MESA_LOG(SERIAL) diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index 1449a5e785..9f16e70a5c 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -302,7 +302,6 @@ _mesa_sift_head(c3_y buf_y[8], u3_mesa_head* hed_u) } c3_w hed_w = _ames_sift_word(buf_y); - u3l_log("hed_w %x", hed_w); hed_u->nex_y = (hed_w >> 2) & 0x3; hed_u->pro_y = (hed_w >> 4) & 0x7; @@ -391,7 +390,7 @@ static c3_w _mesa_sift_data(u3_mesa_data* dat_u, c3_y* buf_y, c3_w len_w) { // #ifdef MESA_DEBUG - u3l_log("mesa: sifting data %i", len_w); + // u3l_log("mesa: sifting data %i", len_w); // #endif c3_w cur_w = 0; @@ -541,26 +540,26 @@ _mesa_sift_poke_pact(u3_mesa_poke_pact* pac_u, c3_y* buf_y, c3_w len_w) c3_w cur_w = 0, nex_w; // ack path if ( !(nex_w = _mesa_sift_name(&pac_u->nam_u, buf_y + cur_w, len_w)) ) { - u3l_log("_mesa_sift_poke_pact nex_w %u", nex_w); + // u3l_log("_mesa_sift_poke_pact nex_w %u", nex_w); return 0; } cur_w += nex_w; // payload path if ( !(nex_w = _mesa_sift_name(&pac_u->pay_u, buf_y + cur_w, len_w)) ) { - u3l_log("_mesa_sift_poke_pact nex_w %u", nex_w); + // u3l_log("_mesa_sift_poke_pact nex_w %u", nex_w); return 0; } cur_w += nex_w; // payload if ( !(nex_w = _mesa_sift_data(&pac_u->dat_u, buf_y + cur_w, len_w)) ) { - u3l_log("_mesa_sift_poke_pact nex_w %u", nex_w); + // u3l_log("_mesa_sift_poke_pact nex_w %u", nex_w); return 0; } cur_w += nex_w; - u3l_log("_mesa_sift_poke_pact cur_w %u", cur_w); + // u3l_log("_mesa_sift_poke_pact cur_w %u", cur_w); return cur_w; } @@ -578,7 +577,7 @@ mesa_sift_pact(u3_mesa_pact* pac_u, c3_y* buf_y, c3_w len_w) buf_y += 8; len_w -= 8; - u3l_log("pac_u->hed_u.typ_y typ_y %u", pac_u->hed_u.typ_y); + // u3l_log("pac_u->hed_u.typ_y typ_y %u", pac_u->hed_u.typ_y); switch ( pac_u->hed_u.typ_y ) { case PACT_PEEK: { From b59f33f9870373dce53c38628943faa219e6aa90 Mon Sep 17 00:00:00 2001 From: Sigilante Date: Fri, 12 Apr 2024 17:10:08 -0500 Subject: [PATCH 093/430] Bump SoftBLAS to fix memory error and add progress on jets. --- WORKSPACE.bazel | 2 +- pkg/noun/jets/f/lagoon.c | 1557 ++++++++++++++++++-------------------- pkg/noun/jets/q.h | 1 + pkg/noun/jets/tree.c | 2 + pkg/noun/jets/w.h | 1 + 5 files changed, 761 insertions(+), 802 deletions(-) diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index 3b0296fd0f..de3564b253 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -297,7 +297,7 @@ versioned_http_archive( strip_prefix = "SoftBLAS-{version}", # sha256 = "", url = "https://github.com/urbit/SoftBLAS/archive/{version}.tar.gz", - version = "bace30db3944c0f2bb2b6cac0db9965675ad842e", + version = "3af44d8cbf0d61e31946af9127099257160d0451", ) versioned_http_archive( diff --git a/pkg/noun/jets/f/lagoon.c b/pkg/noun/jets/f/lagoon.c index baaac63331..84c58bd191 100644 --- a/pkg/noun/jets/f/lagoon.c +++ b/pkg/noun/jets/f/lagoon.c @@ -38,8 +38,7 @@ // We could use SoftBLAS set_rounding() to set the SoftFloat // mode as well, but it's more explicit to do it here since // we may use SoftFloat in any given Lagoon jet and we want - // you, dear developer, to see this set here. - fprintf(stderr, "%x %c\n", a, a); + // you, dear developer, to see it set here. switch ( a ) { default: @@ -92,7 +91,7 @@ u3_atom len = u3qb_lent(shape); c3_d len_d = u3r_chub(0, len); c3_d* dims = (c3_d*)u3a_malloc(len_d*sizeof(c3_d)); - for (uint64_t i = 0; i < len_d; i++) { + for (c3_d i = 0; i < len_d; i++) { dims[i] = u3r_chub(0, u3x_atom(u3h(shape))); shape = u3t(shape); } @@ -102,7 +101,7 @@ /* */ - static inline size_t _get_array_length(uint64_t* array) + static inline size_t _get_array_length(c3_d* array) { size_t n = sizeof(array)/sizeof(array[0]); for (size_t i = 0; i < n; i++) { @@ -118,74 +117,56 @@ u3qf_la_add_real(u3_noun x_data, u3_noun y_data, u3_noun shape, - u3_noun bloq) + u3_noun bloq + ) { + // Fence on valid bloq size. + if (bloq < 4 || bloq > 7) { + return u3_none; + } + // Unpack the data as a byte array. We assume total length < 2**64. - uint64_t len_a = _get_length(shape); - uint64_t siz_a = len_a * pow(2, bloq - 3); - uint8_t* x_bytes = (uint8_t*)u3a_malloc(siz_a*sizeof(uint8_t)); - u3r_bytes(0, siz_a, x_bytes, x_data); - uint8_t* y_bytes = (uint8_t*)u3a_malloc((siz_a+1)*sizeof(uint8_t)); - u3r_bytes(0, siz_a+1, y_bytes, y_data); + // len_x is length in base units + c3_d len_x = _get_length(shape); - u3_noun r_data; + // siz_x is length in bytes + c3_d siz_x = len_x * pow(2, bloq-3); + // x_bytes is the data array (w/o leading 0x1) + c3_y* x_bytes = (c3_y*)u3a_malloc(siz_x*sizeof(c3_y)); + u3r_bytes(0, siz_x, x_bytes, x_data); + + // y_bytes is the data array (w/ leading 0x1, skipped by ?axpy) + c3_y* y_bytes = (c3_y*)u3a_malloc((siz_x+1)*sizeof(c3_y)); + u3r_bytes(0, siz_x+1, y_bytes, y_data); + // Switch on the block size. switch (bloq) { case 4: - haxpy(len_a, (float16_t){SB_REAL16_ONE}, (float16_t*)x_bytes, 1, (float16_t*)y_bytes, 1); - - // Unpack the result back into a noun. - r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); - - // Clean up. - u3a_free(x_bytes); - u3a_free(y_bytes); - - return r_data; + haxpy(len_x, (float16_t){SB_REAL16_ONE}, (float16_t*)x_bytes, 1, (float16_t*)y_bytes, 1); + break; case 5: - saxpy(len_a, (float32_t){SB_REAL32_ONE}, (float32_t*)x_bytes, 1, (float32_t*)y_bytes, 1); - - // Unpack the result back into a noun. - r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); - - // Clean up. - u3a_free(x_bytes); - u3a_free(y_bytes); - - return r_data; + saxpy(len_x, (float32_t){SB_REAL32_ONE}, (float32_t*)x_bytes, 1, (float32_t*)y_bytes, 1); + break; case 6: - daxpy(len_a, (float64_t){SB_REAL64_ONE}, (float64_t*)x_bytes, 1, (float64_t*)y_bytes, 1); - - // Unpack the result back into a noun. - r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); - - // Clean up. - u3a_free(x_bytes); - u3a_free(y_bytes); - - return r_data; + daxpy(len_x, (float64_t){SB_REAL64_ONE}, (float64_t*)x_bytes, 1, (float64_t*)y_bytes, 1); + break; case 7: - qaxpy(len_a, (float128_t){SB_REAL128L_ONE,SB_REAL128U_ONE}, (float128_t*)x_bytes, 1, (float128_t*)y_bytes, 1); - - // Unpack the result back into a noun. - r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); - - // Clean up. - u3a_free(x_bytes); - u3a_free(y_bytes); + qaxpy(len_x, (float128_t){SB_REAL128L_ONE,SB_REAL128U_ONE}, (float128_t*)x_bytes, 1, (float128_t*)y_bytes, 1); + break; + } - return r_data; + // r_data is the result noun of [data] + u3_noun r_data = u3i_bytes((siz_x+1)*sizeof(c3_y), y_bytes); - default: - u3a_free(x_bytes); - u3a_free(y_bytes); + // Clean up and return. + u3a_free(x_bytes); + u3a_free(y_bytes); - return u3_none; - } + return r_data; } /* sub - axpy = -1*y+x @@ -194,76 +175,59 @@ u3qf_la_sub_real(u3_noun x_data, u3_noun y_data, u3_noun shape, - u3_noun bloq) + u3_noun bloq + ) { + // Fence on valid bloq size. + if (bloq < 4 || bloq > 7) { + return u3_none; + } + // Unpack the data as a byte array. We assume total length < 2**64. - uint64_t len_a = _get_length(shape); - uint64_t siz_a = len_a * pow(2, bloq - 3); - uint8_t* x_bytes = (uint8_t*)u3a_malloc(siz_a*sizeof(uint8_t)); - u3r_bytes(0, siz_a, x_bytes, y_data); // XXX - uint8_t* y_bytes = (uint8_t*)u3a_malloc((siz_a+1)*sizeof(uint8_t)); - u3r_bytes(0, siz_a+1, y_bytes, x_data); // XXX + // len_x is length in base units + c3_d len_x = _get_length(shape); - u3_noun r_data; + // siz_x is length in bytes + c3_d siz_x = len_x * pow(2, bloq-3); + // x_bytes is the data array (w/o leading 0x1) + c3_y* x_bytes = (c3_y*)u3a_malloc(siz_x*sizeof(c3_y)); + u3r_bytes(0, siz_x, x_bytes, x_data); + + // y_bytes is the data array (w/ leading 0x1, skipped by ?axpy) + c3_y* y_bytes = (c3_y*)u3a_malloc((siz_x+1)*sizeof(c3_y)); + u3r_bytes(0, siz_x+1, y_bytes, y_data); + // Switch on the block size. switch (bloq) { case 4: - haxpy(len_a, (float16_t){SB_REAL16_NEGONE}, (float16_t*)x_bytes, 1, (float16_t*)y_bytes, 1); - - // Unpack the result back into a noun. - r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); - - // Clean up. - u3a_free(x_bytes); - u3a_free(y_bytes); - - return r_data; + haxpy(len_x, (float16_t){SB_REAL16_NEGONE}, (float16_t*)x_bytes, 1, (float16_t*)y_bytes, 1); + break; case 5: - saxpy(len_a, (float32_t){SB_REAL32_NEGONE}, (float32_t*)x_bytes, 1, (float32_t*)y_bytes, 1); - - // Unpack the result back into a noun. - r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); - - // Clean up. - u3a_free(x_bytes); - u3a_free(y_bytes); - - return r_data; + saxpy(len_x, (float32_t){SB_REAL32_NEGONE}, (float32_t*)x_bytes, 1, (float32_t*)y_bytes, 1); + break; case 6: - daxpy(len_a, (float64_t){SB_REAL64_NEGONE}, (float64_t*)x_bytes, 1, (float64_t*)y_bytes, 1); - - // Unpack the result back into a noun. - r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); - - // Clean up. - u3a_free(x_bytes); - u3a_free(y_bytes); - - return r_data; + daxpy(len_x, (float64_t){SB_REAL64_NEGONE}, (float64_t*)x_bytes, 1, (float64_t*)y_bytes, 1); + break; case 7: - qaxpy(len_a, (float128_t){SB_REAL128L_NEGONE,SB_REAL128U_NEGONE}, (float128_t*)x_bytes, 1, (float128_t*)y_bytes, 1); - - // Unpack the result back into a noun. - r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); - - // Clean up. - u3a_free(x_bytes); - u3a_free(y_bytes); + qaxpy(len_x, (float128_t){SB_REAL128L_NEGONE,SB_REAL128U_NEGONE}, (float128_t*)x_bytes, 1, (float128_t*)y_bytes, 1); + break; + } - return r_data; + // r_data is the result noun of [data] + u3_noun r_data = u3i_bytes((siz_x+1)*sizeof(c3_y), y_bytes); - default: - u3a_free(x_bytes); - u3a_free(y_bytes); + // Clean up and return. + u3a_free(x_bytes); + u3a_free(y_bytes); - return u3_none; - } + return r_data; } + /* mul - x.*y elementwise multiplication */ @@ -273,80 +237,61 @@ u3_noun shape, u3_noun bloq) { + // Fence on valid bloq size. + if (bloq < 4 || bloq > 7) { + return u3_none; + } + // Unpack the data as a byte array. We assume total length < 2**64. - uint64_t len_a = _get_length(shape); - uint64_t siz_a = len_a * pow(2, bloq - 3); - uint8_t* x_bytes = (uint8_t*)u3a_malloc(siz_a*sizeof(uint8_t)); - u3r_bytes(0, siz_a, x_bytes, x_data); - uint8_t* y_bytes = (uint8_t*)u3a_malloc((siz_a+1)*sizeof(uint8_t)); - u3r_bytes(0, siz_a+1, y_bytes, y_data); + // len_x is length in base units + c3_d len_x = _get_length(shape); - u3_noun r_data; + // siz_x is length in bytes + c3_d siz_x = len_x * pow(2, bloq-3); + + // x_bytes is the data array (w/o leading 0x1) + c3_y* x_bytes = (c3_y*)u3a_malloc(siz_x*sizeof(c3_y)); + u3r_bytes(0, siz_x, x_bytes, x_data); + + // y_bytes is the data array (w/ leading 0x1, skipped by ?axpy) + c3_y* y_bytes = (c3_y*)u3a_malloc((siz_x+1)*sizeof(c3_y)); + u3r_bytes(0, siz_x+1, y_bytes, y_data); // Switch on the block size. switch (bloq) { case 4: - for (uint64_t i = 0; i < len_a; i++) { + for (c3_d i = 0; i < len_x; i++) { ((float16_t*)y_bytes)[i] = f16_mul(((float16_t*)x_bytes)[i], ((float16_t*)y_bytes)[i]); } - - // Unpack the result back into a noun. - r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); - - // Clean up. - u3a_free(x_bytes); - u3a_free(y_bytes); - - return r_data; + break; case 5: - for (uint64_t i = 0; i < len_a; i++) { + for (c3_d i = 0; i < len_x; i++) { ((float32_t*)y_bytes)[i] = f32_mul(((float32_t*)x_bytes)[i], ((float32_t*)y_bytes)[i]); } - - // Unpack the result back into a noun. - r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); - - // Clean up. - u3a_free(x_bytes); - u3a_free(y_bytes); - - return r_data; + break; case 6: - for (uint64_t i = 0; i < len_a; i++) { + for (c3_d i = 0; i < len_x; i++) { ((float64_t*)y_bytes)[i] = f64_mul(((float64_t*)x_bytes)[i], ((float64_t*)y_bytes)[i]); } - - // Unpack the result back into a noun. - r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); - - // Clean up. - u3a_free(x_bytes); - u3a_free(y_bytes); - - return r_data; + break; case 7: - for (uint64_t i = 0; i < len_a; i++) { + for (c3_d i = 0; i < len_x; i++) { f128M_mul(&(((float128_t*)y_bytes)[i]), &(((float128_t*)x_bytes)[i]), &(((float128_t*)y_bytes)[i])); } + break; + } - // Unpack the result back into a noun. - r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); - - // Clean up. - u3a_free(x_bytes); - u3a_free(y_bytes); - - return r_data; + // r_data is the result noun of [data] + u3_noun r_data = u3i_bytes((siz_x+1)*sizeof(c3_y), y_bytes); - default: - u3a_free(x_bytes); - u3a_free(y_bytes); + // Clean up and return. + u3a_free(x_bytes); + u3a_free(y_bytes); - return u3_none; - } + return r_data; } /* div - x/y @@ -358,286 +303,347 @@ u3_noun shape, u3_noun bloq) { + // Fence on valid bloq size. + if (bloq < 4 || bloq > 7) { + return u3_none; + } + // Unpack the data as a byte array. We assume total length < 2**64. - uint64_t len_a = _get_length(shape); - uint64_t siz_a = len_a * pow(2, bloq - 3); - uint8_t* x_bytes = (uint8_t*)u3a_malloc(siz_a*sizeof(uint8_t)); - u3r_bytes(0, siz_a, x_bytes, x_data); - uint8_t* y_bytes = (uint8_t*)u3a_malloc((siz_a+1)*sizeof(uint8_t)); - u3r_bytes(0, siz_a+1, y_bytes, y_data); + // len_x is length in base units + c3_d len_x = _get_length(shape); - u3_noun r_data; + // siz_x is length in bytes + c3_d siz_x = len_x * pow(2, bloq-3); + + // x_bytes is the data array (w/o leading 0x1) + c3_y* x_bytes = (c3_y*)u3a_malloc(siz_x*sizeof(c3_y)); + u3r_bytes(0, siz_x, x_bytes, x_data); + + // y_bytes is the data array (w/ leading 0x1, skipped by ?axpy) + c3_y* y_bytes = (c3_y*)u3a_malloc((siz_x+1)*sizeof(c3_y)); + u3r_bytes(0, siz_x+1, y_bytes, y_data); // Switch on the block size. switch (bloq) { case 4: - for (uint64_t i = 0; i < len_a; i++) { + for (c3_d i = 0; i < len_x; i++) { ((float16_t*)y_bytes)[i] = f16_div(((float16_t*)x_bytes)[i], ((float16_t*)y_bytes)[i]); } - - // Unpack the result back into a noun. - r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); - - // Clean up. - u3a_free(x_bytes); - u3a_free(y_bytes); - - return r_data; + break; case 5: - for (uint64_t i = 0; i < len_a; i++) { + for (c3_d i = 0; i < len_x; i++) { ((float32_t*)y_bytes)[i] = f32_div(((float32_t*)x_bytes)[i], ((float32_t*)y_bytes)[i]); } - - // Unpack the result back into a noun. - r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); - - // Clean up. - u3a_free(x_bytes); - u3a_free(y_bytes); - - return r_data; + break; case 6: - for (uint64_t i = 0; i < len_a; i++) { + for (c3_d i = 0; i < len_x; i++) { ((float64_t*)y_bytes)[i] = f64_div(((float64_t*)x_bytes)[i], ((float64_t*)y_bytes)[i]); } - - // Unpack the result back into a noun. - r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); - - // Clean up. - u3a_free(x_bytes); - u3a_free(y_bytes); - - return r_data; + break; case 7: - for (uint64_t i = 0; i < len_a; i++) { + for (c3_d i = 0; i < len_x; i++) { f128M_div(&(((float128_t*)y_bytes)[i]), &(((float128_t*)x_bytes)[i]), &(((float128_t*)y_bytes)[i])); } + break; + } - // Unpack the result back into a noun. - r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); + // r_data is the result noun of [data] + u3_noun r_data = u3i_bytes((siz_x+1)*sizeof(c3_y), y_bytes); - // Clean up. - u3a_free(x_bytes); - u3a_free(y_bytes); + // Clean up and return. + u3a_free(x_bytes); + u3a_free(y_bytes); - return r_data; + return r_data; + } - default: - u3a_free(x_bytes); - u3a_free(y_bytes); +/* mod - x % y = x - r*floor(x/r) + remainder after division +*/ + u3_noun + u3qf_la_mod_real(u3_noun x_data, + u3_noun y_data, + u3_noun shape, + u3_noun bloq, + u3_noun rnd) + { + // Fence on valid bloq size. + if (bloq < 4 || bloq > 7) { + return u3_none; + } - return u3_none; + // Unpack the data as a byte array. We assume total length < 2**64. + // len_x is length in base units + c3_d len_x = _get_length(shape); + + // siz_x is length in bytes + c3_d siz_x = len_x * pow(2, bloq-3); + + // x_bytes is the data array (w/o leading 0x1) + c3_y* x_bytes = (c3_y*)u3a_malloc(siz_x*sizeof(c3_y)); + u3r_bytes(0, siz_x, x_bytes, x_data); + + // y_bytes is the data array (w/ leading 0x1, skipped by ?axpy) + c3_y* y_bytes = (c3_y*)u3a_malloc((siz_x+1)*sizeof(c3_y)); + u3r_bytes(0, siz_x+1, y_bytes, y_data); + + // Switch on the block size. + switch (bloq) { + case 4: + for (c3_d i = 0; i < len_x; i++) { + float16_t x_val16 = ((float16_t*)x_bytes)[i]; + float16_t y_val16 = ((float16_t*)y_bytes)[i]; + // Perform division x/n + float16_t div_result16 = f16_div(x_val16, y_val16); + // Compute floor of the division result + int64_t floor_result16 = f16_to_i64(div_result16, rnd, false); + float16_t floor_float16 = i64_to_f16(floor_result16); + // Multiply n by floor(x/n) + float16_t mult_result16 = f16_mul(y_val16, floor_float16); + // Compute remainder: x - n * floor(x/n) + ((float16_t*)y_bytes)[i] = f16_sub(x_val16, mult_result16); + } + break; + + case 5: + for (c3_d i = 0; i < len_x; i++) { + float32_t x_val32 = ((float32_t*)x_bytes)[i]; + float32_t y_val32 = ((float32_t*)y_bytes)[i]; + // Perform division x/n + float32_t div_result32 = f32_div(x_val32, y_val32); + // Compute floor of the division result + int64_t floor_result32 = f32_to_i64(div_result32, rnd, false); + float32_t floor_float32 = i64_to_f32(floor_result32); + // Multiply n by floor(x/n) + float32_t mult_result32 = f32_mul(y_val32, floor_float32); + // Compute remainder: x - n * floor(x/n) + ((float32_t*)y_bytes)[i] = f32_sub(x_val32, mult_result32); + } + break; + + case 6: + for (c3_d i = 0; i < len_x; i++) { + float64_t x_val64 = ((float64_t*)x_bytes)[i]; + float64_t y_val64 = ((float64_t*)y_bytes)[i]; + // Perform division x/n + float64_t div_result64 = f64_div(x_val64, y_val64); + // Compute floor of the division result + int64_t floor_result64 = f64_to_i64(div_result64, rnd, false); + float64_t floor_float64 = i64_to_f64(floor_result64); + // Multiply n by floor(x/n) + float64_t mult_result64 = f64_mul(y_val64, floor_float64); + // Compute remainder: x - n * floor(x/n) + ((float64_t*)y_bytes)[i] = f64_sub(x_val64, mult_result64); + } + break; + + case 7: + for (c3_d i = 0; i < len_x; i++) { + float128_t x_val128 = ((float128_t*)x_bytes)[i]; + float128_t y_val128 = ((float128_t*)y_bytes)[i]; + fprintf(stderr, "x_val128: %llx %llx\r\n", x_val128.v[0], x_val128.v[1]); + fprintf(stderr, "y_val128: %llx %llx\r\n", y_val128.v[0], y_val128.v[1]); + // Perform division x/n + float128_t div_result128; + // float128_t div_result128 = f128_div(x_val128, y_val128); + f128M_div((float128_t*)&x_val128, (float128_t*)&y_val128, (float128_t*)&div_result128); + fprintf(stderr, "div_result128: %llx %llx\r\n", div_result128.v[0], div_result128.v[1]); + // Compute floor of the division result + int64_t floor_result128 = f128_to_i64(div_result128, softfloat_round_minMag, false); + fprintf(stderr, "floor_result128: %llx\r\n", floor_result128); + float128_t floor_float128 = i64_to_f128(floor_result128); + fprintf(stderr, "floor_float128: %llx %llx\r\n", floor_float128.v[0], floor_float128.v[1]); + // Multiply n by floor(x/n) + float128_t mult_result128; + // float128_t mult_result128 = f128_mul(y_val128, floor_float128); + f128M_mul(((float128_t*)&y_val128), ((float128_t*)&floor_float128), ((float128_t*)&mult_result128)); + fprintf(stderr, "mult_result128: %llx %llx\r\n", mult_result128.v[0], mult_result128.v[1]); + // Compute remainder: x - n * floor(x/n) + // ((float128_t*)y_bytes)[i] = f128_sub(x_val128, mult_result128); + f128M_div(((float128_t*)&x_val128), ((float128_t*)&mult_result128), &(((float128_t*)y_bytes)[i])); + fprintf(stderr, "y_bytes: %llx %llx\r\n", ((float128_t*)y_bytes)[i].v[0], ((float128_t*)y_bytes)[i].v[1]); + } + // for (c3_d i = 0; i < len_x; i++) { + // f128M_div(&(((float128_t*)y_bytes)[i]), &(((float128_t*)x_bytes)[i]), &(((float128_t*)y_bytes)[i])); + // } + break; } + + // r_data is the result noun of [data] + u3_noun r_data = u3i_bytes((siz_x+1)*sizeof(c3_y), y_bytes); + + // Clean up and return. + u3a_free(x_bytes); + u3a_free(y_bytes); + + return r_data; } -/* adds - axpy = 1*x+n +/* adds - axpy = 1*x+[n] */ u3_noun u3qf_la_adds_real(u3_noun x_data, + u3_noun n, u3_noun shape, - u3_noun bloq, - u3_noun n) + u3_noun bloq) { + // Fence on valid bloq size. + if (bloq < 4 || bloq > 7) { + return u3_none; + } + // Unpack the data as a byte array. We assume total length < 2**64. - uint64_t len_a = _get_length(shape); - fprintf(stderr, "len_a: %d 0x%x units\r\n", len_a, len_a); - uint64_t siz_a = len_a * pow(2, bloq - 3); - fprintf(stderr, "siz_a: %d 0x%x bytes\r\n", siz_a, siz_a); - uint8_t* x_bytes = (uint8_t*)u3a_malloc(siz_a*sizeof(uint8_t)); - u3r_bytes(0, siz_a, x_bytes, x_data); - uint8_t* y_bytes = (uint8_t*)u3a_malloc((siz_a+1)*sizeof(uint8_t)); + // len_x is length in base units + c3_d len_x = _get_length(shape); + + // siz_x is length in bytes + c3_d siz_x = len_x * pow(2, bloq-3); + + // x_bytes is the data array (w/o leading 0x1) + c3_y* x_bytes = (c3_y*)u3a_malloc(siz_x*sizeof(c3_y)); + u3r_bytes(0, siz_x, x_bytes, x_data); + + // y_bytes is the data array (w/ leading 0x1, skipped by ?axpy) + c3_y* y_bytes = (c3_y*)u3a_malloc((siz_x+1)*sizeof(c3_y)); float16_t n16; float32_t n32; float64_t n64; float128_t n128; - u3_noun r_data; - // Switch on the block size. We assume that n fits in the target block size; Hoon typecheck should prevent. switch (bloq) { case 4: - u3r_bytes(0, 2, (uint8_t*)&n16, n); + u3r_bytes(0, 2, (c3_y*)&n16, n); // set y to [n] - for (uint64_t i = 0; i < len_a; i++) { + for (c3_d i = 0; i < len_x; i++) { ((float16_t*)y_bytes)[i] = n16; } - y_bytes[siz_a] = 1; // pin head - haxpy(len_a, (float16_t){SB_REAL16_ONE}, (float16_t*)x_bytes, 1, (float16_t*)y_bytes, 1); - - // Unpack the result back into a noun. - r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); - - // Clean up. - u3a_free(x_bytes); - u3a_free(y_bytes); - - return r_data; + haxpy(len_x, (float16_t){SB_REAL16_ONE}, (float16_t*)x_bytes, 1, (float16_t*)y_bytes, 1); + break; case 5: - u3r_bytes(0, 4, (uint8_t*)&n32, n); + u3r_bytes(0, 4, (c3_y*)&n32, n); // set y to [n] - for (uint64_t i = 0; i < len_a; i++) { + for (c3_d i = 0; i < len_x; i++) { ((float32_t*)y_bytes)[i] = n32; } - y_bytes[siz_a] = 1; // pin head - saxpy(len_a, (float32_t){SB_REAL32_ONE}, (float32_t*)x_bytes, 1, (float32_t*)y_bytes, 1); - - // Unpack the result back into a noun. - r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); - - // Clean up. - u3a_free(x_bytes); - u3a_free(y_bytes); - - return r_data; + saxpy(len_x, (float32_t){SB_REAL32_ONE}, (float32_t*)x_bytes, 1, (float32_t*)y_bytes, 1); + break; case 6: - u3r_bytes(0, 8, (uint8_t*)&n64, n); + u3r_bytes(0, 8, (c3_y*)&n64, n); // set y to [n] - for (uint64_t i = 0; i < len_a; i++) { + for (c3_d i = 0; i < len_x; i++) { ((float64_t*)y_bytes)[i] = n64; } - y_bytes[siz_a] = 1; // pin head - daxpy(len_a, (float64_t){SB_REAL64_ONE}, (float64_t*)x_bytes, 1, (float64_t*)y_bytes, 1); - - // Unpack the result back into a noun. - r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); - - // Clean up. - u3a_free(x_bytes); - u3a_free(y_bytes); - - return r_data; + daxpy(len_x, (float64_t){SB_REAL64_ONE}, (float64_t*)x_bytes, 1, (float64_t*)y_bytes, 1); + break; case 7: - u3r_bytes(0, 16, (uint8_t*)&n128, n); + u3r_bytes(0, 16, (c3_y*)&n128, n); // set y to [n] - for (uint64_t i = 0; i < len_a; i++) { + for (c3_d i = 0; i < len_x; i++) { ((float128_t*)y_bytes)[i] = (float128_t){n128.v[0], n128.v[1]}; } - y_bytes[siz_a] = 1; // pin head - qaxpy(len_a, (float128_t){SB_REAL128L_ONE,SB_REAL128U_ONE}, (float128_t*)x_bytes, 1, (float128_t*)y_bytes, 1); - - // Unpack the result back into a noun. - r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); - - // Clean up. - u3a_free(x_bytes); - u3a_free(y_bytes); + qaxpy(len_x, (float128_t){SB_REAL128L_ONE,SB_REAL128U_ONE}, (float128_t*)x_bytes, 1, (float128_t*)y_bytes, 1); + break; + } - return r_data; + // r_data is the result noun of [data] + y_bytes[siz_x] = 1; // pin head + u3_noun r_data = u3i_bytes((siz_x+1)*sizeof(c3_y), y_bytes); - default: - u3a_free(x_bytes); - u3a_free(y_bytes); + // Clean up and return. + u3a_free(x_bytes); + u3a_free(y_bytes); - return u3_none; - } + return r_data; } -/* subs - axpy = -1*n+x +/* subs - axpy = -1*[n]+x */ u3_noun u3qf_la_subs_real(u3_noun x_data, + u3_noun n, u3_noun shape, - u3_noun bloq, - u3_noun n) + u3_noun bloq) { + // Fence on valid bloq size. + if (bloq < 4 || bloq > 7) { + return u3_none; + } + // Unpack the data as a byte array. We assume total length < 2**64. - uint64_t len_a = _get_length(shape); - uint64_t siz_a = len_a * pow(2, bloq - 3); - uint8_t* x_bytes = (uint8_t*)u3a_malloc((siz_a+1)*sizeof(uint8_t)); - u3r_bytes(0, siz_a+1, x_bytes, x_data); - uint8_t* y_bytes = (uint8_t*)u3a_malloc(siz_a*sizeof(uint8_t)); + // len_x is length in base units + c3_d len_x = _get_length(shape); + + // siz_x is length in bytes + c3_d siz_x = len_x * pow(2, bloq-3); + + // x_bytes is the data array (w/ leading 0x1, skipped by ?axpy) + c3_y* x_bytes = (c3_y*)u3a_malloc((siz_x+1)*sizeof(c3_y)); + u3r_bytes(0, siz_x, x_bytes, x_data); + + // y_bytes is the data array (w/o leading 0x1) + c3_y* y_bytes = (c3_y*)u3a_malloc(siz_x*sizeof(c3_y)); float16_t n16; float32_t n32; float64_t n64; float128_t n128; - u3_noun r_data; - // Switch on the block size. We assume that n fits in the target block size; Hoon typecheck should prevent. switch (bloq) { case 4: - u3r_bytes(0, 2, (uint8_t*)&n16, n); + u3r_bytes(0, 2, (c3_y*)&n16, n); // set y to [n] - for (uint64_t i = 0; i < len_a; i++) { + for (c3_d i = 0; i < len_x; i++) { ((float16_t*)y_bytes)[i] = n16; } - haxpy(len_a, (float16_t){SB_REAL16_NEGONE}, (float16_t*)y_bytes, 1, (float16_t*)x_bytes, 1); - - // Unpack the result back into a noun. - r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), x_bytes); - - // Clean up. - u3a_free(x_bytes); - u3a_free(y_bytes); - - return r_data; + haxpy(len_x, (float16_t){SB_REAL16_NEGONE}, (float16_t*)y_bytes, 1, (float16_t*)x_bytes, 1); + break; case 5: - u3r_bytes(0, 4, (uint8_t*)&n32, n); + u3r_bytes(0, 4, (c3_y*)&n32, n); // set y to [n] - for (uint64_t i = 0; i < len_a; i++) { + for (c3_d i = 0; i < len_x; i++) { ((float32_t*)y_bytes)[i] = n32; } - saxpy(len_a, (float32_t){SB_REAL32_NEGONE}, (float32_t*)y_bytes, 1, (float32_t*)x_bytes, 1); - - // Unpack the result back into a noun. - r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), x_bytes); - - // Clean up. - u3a_free(x_bytes); - u3a_free(y_bytes); - - return r_data; + saxpy(len_x, (float32_t){SB_REAL32_NEGONE}, (float32_t*)y_bytes, 1, (float32_t*)x_bytes, 1); + break; case 6: - u3r_bytes(0, 8, (uint8_t*)&n64, n); + u3r_bytes(0, 8, (c3_y*)&n64, n); // set y to [n] - for (uint64_t i = 0; i < len_a; i++) { + for (c3_d i = 0; i < len_x; i++) { ((float64_t*)y_bytes)[i] = n64; } - daxpy(len_a, (float64_t){SB_REAL64_NEGONE}, (float64_t*)y_bytes, 1, (float64_t*)x_bytes, 1); - - // Unpack the result back into a noun. - r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), x_bytes); - - // Clean up. - u3a_free(x_bytes); - u3a_free(y_bytes); - - return r_data; + daxpy(len_x, (float64_t){SB_REAL64_NEGONE}, (float64_t*)y_bytes, 1, (float64_t*)x_bytes, 1); + break; case 7: - u3r_bytes(0, 16, (uint8_t*)&n128, n); + u3r_bytes(0, 16, (c3_y*)&n128, n); // set y to [n] - for (uint64_t i = 0; i < len_a; i++) { + for (c3_d i = 0; i < len_x; i++) { ((float128_t*)y_bytes)[i] = (float128_t){n128.v[0], n128.v[1]}; } - qaxpy(len_a, (float128_t){SB_REAL128L_NEGONE,SB_REAL128U_NEGONE}, (float128_t*)y_bytes, 1, (float128_t*)x_bytes, 1); - - // Unpack the result back into a noun. - r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), x_bytes); - - // Clean up. - u3a_free(x_bytes); - u3a_free(y_bytes); + qaxpy(len_x, (float128_t){SB_REAL128L_NEGONE,SB_REAL128U_NEGONE}, (float128_t*)y_bytes, 1, (float128_t*)x_bytes, 1); + break; + } - return r_data; + // r_data is the result noun of [data] + x_bytes[siz_x] = 1; // pin head + u3_noun r_data = u3i_bytes((siz_x+1)*sizeof(c3_y), x_bytes); - default: - u3a_free(x_bytes); - u3a_free(y_bytes); + // Clean up and return. + u3a_free(x_bytes); + u3a_free(y_bytes); - return u3_none; - } + return r_data; } /* muls - ?scal n * x @@ -645,79 +651,62 @@ */ u3_noun u3qf_la_muls_real(u3_noun x_data, + u3_noun n, u3_noun shape, - u3_noun bloq, - u3_noun n) + u3_noun bloq) { + // Fence on valid bloq size. + if (bloq < 4 || bloq > 7) { + return u3_none; + } + // Unpack the data as a byte array. We assume total length < 2**64. - uint64_t len_a = _get_length(shape); - uint64_t siz_a = len_a * pow(2, bloq - 3); - uint8_t* x_bytes = (uint8_t*)u3a_malloc((siz_a+1)*sizeof(uint8_t)); - u3r_bytes(0, siz_a, x_bytes, x_data); - x_bytes[siz_a] = 1; // pin head + // len_x is length in base units + c3_d len_x = _get_length(shape); + + // siz_x is length in bytes + c3_d siz_x = len_x * pow(2, bloq-3); + + // x_bytes is the data array (w/ leading 0x1, skipped by ?axpy) + c3_y* x_bytes = (c3_y*)u3a_malloc((siz_x+1)*sizeof(c3_y)); + u3r_bytes(0, siz_x, x_bytes, x_data); + x_bytes[siz_x] = 1; // pin head float16_t n16; float32_t n32; float64_t n64; float128_t n128; - u3_noun r_data; - // Switch on the block size. switch (bloq) { case 4: - u3r_bytes(0, 2, (uint8_t*)&n16, n); - hscal(len_a, n16, (float16_t*)x_bytes, 1); - - // Unpack the result back into a noun. - r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), x_bytes); - - // Clean up. - u3a_free(x_bytes); - - return r_data; + u3r_bytes(0, 2, (c3_y*)&n16, n); + hscal(len_x, n16, (float16_t*)x_bytes, 1); + break; case 5: - u3r_bytes(0, 4, (uint8_t*)&n32, n); - sscal(len_a, n32, (float32_t*)x_bytes, 1); - - // Unpack the result back into a noun. - r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), x_bytes); - - // Clean up. - u3a_free(x_bytes); - - return r_data; + u3r_bytes(0, 4, (c3_y*)&n32, n); + sscal(len_x, n32, (float32_t*)x_bytes, 1); + break; case 6: - u3r_bytes(0, 8, (uint8_t*)&n64, n); - dscal(len_a, n64, (float64_t*)x_bytes, 1); - - // Unpack the result back into a noun. - r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), x_bytes); - - // Clean up. - u3a_free(x_bytes); - - return r_data; + u3r_bytes(0, 8, (c3_y*)&n64, n); + dscal(len_x, n64, (float64_t*)x_bytes, 1); + break; case 7: - u3r_bytes(0, 16, (uint8_t*)&(n128.v[0]), n); - qscal(len_a, n128, (float128_t*)x_bytes, 1); - - // Unpack the result back into a noun. - r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), x_bytes); - - // Clean up. - u3a_free(x_bytes); + u3r_bytes(0, 16, (c3_y*)&(n128.v[0]), n); + qscal(len_x, n128, (float128_t*)x_bytes, 1); + break; + } - return r_data; + // r_data is the result noun of [data] + u3_noun r_data = u3i_bytes((siz_x+1)*sizeof(c3_y), x_bytes); - default: - u3a_free(x_bytes); + // Clean up and return. + u3a_free(x_bytes); - return u3_none; - } + return r_data; } /* divs - ?scal 1/n * x @@ -725,83 +714,66 @@ */ u3_noun u3qf_la_divs_real(u3_noun x_data, + u3_noun n, u3_noun shape, - u3_noun bloq, - u3_noun n) + u3_noun bloq) { + // Fence on valid bloq size. + if (bloq < 4 || bloq > 7) { + return u3_none; + } + // Unpack the data as a byte array. We assume total length < 2**64. - uint64_t len_a = _get_length(shape); - uint64_t siz_a = len_a * pow(2, bloq - 3); - uint8_t* x_bytes = (uint8_t*)u3a_malloc((siz_a+1)*sizeof(uint8_t)); - u3r_bytes(0, siz_a, x_bytes, x_data); - x_bytes[siz_a] = 1; // pin head + // len_x is length in base units + c3_d len_x = _get_length(shape); + + // siz_x is length in bytes + c3_d siz_x = len_x * pow(2, bloq-3); + + // x_bytes is the data array (w/ leading 0x1, skipped by ?axpy) + c3_y* x_bytes = (c3_y*)u3a_malloc((siz_x+1)*sizeof(c3_y)); + u3r_bytes(0, siz_x, x_bytes, x_data); + x_bytes[siz_x] = 1; // pin head float16_t n16; float32_t n32; float64_t n64; float128_t n128; - u3_noun r_data; - // Switch on the block size. switch (bloq) { case 4: - u3r_bytes(0, 2, (uint8_t*)&(n16.v), n); + u3r_bytes(0, 2, (c3_y*)&(n16.v), n); n16 = f16_div((float16_t){SB_REAL16_ONE}, n16); - hscal(len_a, n16, (float16_t*)x_bytes, 1); - - // Unpack the result back into a noun. - r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), x_bytes); - - // Clean up. - u3a_free(x_bytes); - - return r_data; + hscal(len_x, n16, (float16_t*)x_bytes, 1); + break; case 5: - u3r_bytes(0, 4, (uint8_t*)&(n32.v), n); + u3r_bytes(0, 4, (c3_y*)&(n32.v), n); n32 = f32_div((float32_t){SB_REAL32_ONE}, n32); - sscal(len_a, n32, (float32_t*)x_bytes, 1); - - // Unpack the result back into a noun. - r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), x_bytes); - - // Clean up. - u3a_free(x_bytes); - - return r_data; + sscal(len_x, n32, (float32_t*)x_bytes, 1); + break; case 6: - u3r_bytes(0, 8, (uint8_t*)&(n64.v), n); + u3r_bytes(0, 8, (c3_y*)&(n64.v), n); n64 = f64_div((float64_t){SB_REAL64_ONE}, n64); - dscal(len_a, n64, (float64_t*)x_bytes, 1); - - // Unpack the result back into a noun. - r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), x_bytes); - - // Clean up. - u3a_free(x_bytes); - - return r_data; + dscal(len_x, n64, (float64_t*)x_bytes, 1); + break; case 7: - u3r_bytes(0, 16, (uint8_t*)&(n128.v[0]), n); + u3r_bytes(0, 16, (c3_y*)&(n128.v[0]), n); f128M_div(&((float128_t){SB_REAL128L_ONE,SB_REAL128U_ONE}), &n128, &n128); - qscal(len_a, n128, (float128_t*)x_bytes, 1); - - // Unpack the result back into a noun. - r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), x_bytes); - - // Clean up. - u3a_free(x_bytes); + qscal(len_x, n128, (float128_t*)x_bytes, 1); + break; + } - return r_data; + // r_data is the result noun of [data] + u3_noun r_data = u3i_bytes((siz_x+1)*sizeof(c3_y), x_bytes); - default: - u3a_free(x_bytes); + // Clean up and return. + u3a_free(x_bytes); - return u3_none; - } + return r_data; } /* dot - ?dot = x · y @@ -812,72 +784,53 @@ u3_noun shape, u3_noun bloq) { + // Fence on valid bloq size. + if (bloq < 4 || bloq > 7) { + return u3_none; + } + // Unpack the data as a byte array. We assume total length < 2**64. - uint64_t len_a = _get_length(shape); - uint64_t siz_a = len_a * pow(2, bloq - 3); - uint8_t* x_bytes = (uint8_t*)u3a_malloc(siz_a*sizeof(uint8_t)); - u3r_bytes(0, siz_a, x_bytes, x_data); - uint8_t* y_bytes = (uint8_t*)u3a_malloc((siz_a+1)*sizeof(uint8_t)); - u3r_bytes(0, siz_a+1, y_bytes, y_data); + // len_x is length in base units + c3_d len_x = _get_length(shape); - u3_noun r_data; + // siz_x is length in bytes + c3_d siz_x = len_x * pow(2, bloq-3); + + // x_bytes is the data array (w/o leading 0x1) + c3_y* x_bytes = (c3_y*)u3a_malloc(siz_x*sizeof(c3_y)); + u3r_bytes(0, siz_x, x_bytes, x_data); + + // y_bytes is the data array (w/ leading 0x1, skipped by ?axpy) + c3_y* y_bytes = (c3_y*)u3a_malloc((siz_x+1)*sizeof(c3_y)); + u3r_bytes(0, siz_x+1, y_bytes, y_data); // Switch on the block size. switch (bloq) { case 4: - hdot(len_a, (float16_t*)x_bytes, 1, (float16_t*)y_bytes, 1); - - // Unpack the result back into a noun. - r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); - - // Clean up. - u3a_free(x_bytes); - u3a_free(y_bytes); - - return r_data; + hdot(len_x, (float16_t*)x_bytes, 1, (float16_t*)y_bytes, 1); + break; case 5: - sdot(len_a, (float32_t*)x_bytes, 1, (float32_t*)y_bytes, 1); - - // Unpack the result back into a noun. - r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); - - // Clean up. - u3a_free(x_bytes); - u3a_free(y_bytes); - - return r_data; + sdot(len_x, (float32_t*)x_bytes, 1, (float32_t*)y_bytes, 1); + break; case 6: - ddot(len_a, (float64_t*)x_bytes, 1, (float64_t*)y_bytes, 1); - - // Unpack the result back into a noun. - r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); - - // Clean up. - u3a_free(x_bytes); - u3a_free(y_bytes); - - return r_data; + ddot(len_x, (float64_t*)x_bytes, 1, (float64_t*)y_bytes, 1); + break; case 7: - qdot(len_a, (float128_t*)x_bytes, 1, (float128_t*)y_bytes, 1); - - // Unpack the result back into a noun. - r_data = u3i_bytes((siz_a+1)*sizeof(uint8_t), y_bytes); - - // Clean up. - u3a_free(x_bytes); - u3a_free(y_bytes); + qdot(len_x, (float128_t*)x_bytes, 1, (float128_t*)y_bytes, 1); + break; + } - return r_data; + // r_data is the result noun of [data] + u3_noun r_data = u3i_bytes((siz_x+1)*sizeof(c3_y), y_bytes); - default: - u3a_free(x_bytes); - u3a_free(y_bytes); + // Clean up and return. + u3a_free(x_bytes); + u3a_free(y_bytes); - return u3_none; - } + return r_data; } /* diag - diag(x) @@ -890,30 +843,30 @@ // Assert length of dims is 2. assert(u3qb_lent(shape) == 2); // Unpack shape into an array of dimensions. - uint64_t *dims = _get_dims(shape); + c3_d *dims = _get_dims(shape); assert(dims[0] == dims[1]); // Unpack the data as a byte array. We assume total length < 2**64. - uint64_t len_a = _get_length(shape); - uint64_t siz_a = len_a * pow(2, bloq - 3); - uint64_t stride = dims[0] * pow(2, bloq - 3); - uint8_t* x_bytes = (uint8_t*)u3a_malloc((siz_a+1)*sizeof(uint8_t)); - u3r_bytes(0, siz_a+1, x_bytes, x_data); - uint64_t siz_b = stride * dims[1]; - uint8_t* y_bytes = (uint8_t*)u3a_malloc((siz_b+1)*sizeof(uint8_t)); + c3_d len_x = _get_length(shape); + c3_d siz_x = len_x * pow(2, bloq - 3); + c3_d stride = dims[0] * pow(2, bloq - 3); + c3_y* x_bytes = (c3_y*)u3a_malloc((siz_x+1)*sizeof(c3_y)); + u3r_bytes(0, siz_x+1, x_bytes, x_data); + c3_d siz_y = stride * dims[1]; + c3_y* y_bytes = (c3_y*)u3a_malloc((siz_y+1)*sizeof(c3_y)); u3_noun r_data; - for (uint64_t i = 0; i < dims[1]; i++) { - for (uint64_t j = 0; j < stride; j++) { + for (c3_d i = 0; i < dims[1]; i++) { + for (c3_d j = 0; j < stride; j++) { fprintf(stderr, "i*s+j = %d*%d+%d = %d // x_bytes[i]: %lx\r\n", i, stride, j, i*stride+j, x_bytes[i*stride+j + i]); y_bytes[i*stride+j] = x_bytes[i*stride+j + i]; } } - y_bytes[siz_b] = 1; // pin head + y_bytes[siz_y] = 1; // pin head // Unpack the result back into a noun. - r_data = u3i_bytes((siz_b+1)*sizeof(uint8_t), y_bytes); + r_data = u3i_bytes((siz_y+1)*sizeof(c3_y), y_bytes); u3a_free(x_bytes); u3a_free(y_bytes); @@ -930,7 +883,7 @@ u3_noun bloq) { u3_noun diag_data = u3qf_la_diag(x_data, shape, bloq); - uint64_t len_x0 = _get_dims(shape)[0]; + c3_d len_x0 = _get_dims(shape)[0]; return u3qf_la_dot_real(diag_data, diag_data, u3nt(len_x0, 0x1, u3_nul), bloq); } @@ -944,21 +897,21 @@ u3_noun bloq) { // Unpack the data as a byte array. We assume total length < 2**64. - uint64_t M = u3h(x_shape); - uint64_t Na = u3h(u3t(x_shape)); - uint64_t Nb = u3h(y_shape); - uint64_t P = u3h(u3t(y_shape)); + c3_d M = u3h(x_shape); + c3_d Na = u3h(u3t(x_shape)); + c3_d Nb = u3h(y_shape); + c3_d P = u3h(u3t(y_shape)); assert(u3_nul == u3t(u3t(x_shape))); assert(Na == Nb); - uint64_t N = Na; + c3_d N = Na; assert(u3_nul == u3t(u3t(y_shape))); - uint8_t* x_bytes = (uint8_t*)u3a_malloc((M*N)*sizeof(uint8_t)); + c3_y* x_bytes = (c3_y*)u3a_malloc((M*N)*sizeof(c3_y)); u3r_bytes(0, M*N, x_bytes, x_data); - uint8_t* y_bytes = (uint8_t*)u3a_malloc((N*P)*sizeof(uint8_t)); + c3_y* y_bytes = (c3_y*)u3a_malloc((N*P)*sizeof(c3_y)); u3r_bytes(0, N*P, y_bytes, y_data); - uint8_t* c_bytes = (uint8_t*)u3a_malloc((M*P)*sizeof(uint8_t)); + c3_y* c_bytes = (c3_y*)u3a_malloc((M*P)*sizeof(c3_y)); u3_noun r_data; @@ -1029,13 +982,13 @@ u3wf_la_add(u3_noun cor) { // Each argument is a ray, [=meta data=@ux] - u3_noun a_meta, x_data, - b_meta, y_data; + u3_noun x_meta, x_data, + y_meta, y_data; if ( c3n == u3r_mean(cor, - u3x_sam_4, &a_meta, + u3x_sam_4, &x_meta, u3x_sam_5, &x_data, - u3x_sam_6, &b_meta, + u3x_sam_6, &y_meta, u3x_sam_7, &y_data, 0) || c3n == u3ud(x_data) || @@ -1043,36 +996,35 @@ { return u3m_bail(c3__exit); } else { - u3_noun x_shape, a_bloq, a_kind, a_fxp, - y_shape, b_bloq, b_kind, b_fxp, + u3_noun x_shape, x_bloq, x_kind, x_fxp, + y_shape, y_bloq, y_kind, y_fxp, rnd; - if ( c3n == u3r_mean(a_meta, - 2, &x_shape, - 6, &a_bloq, - 14, &a_kind, - 15, &a_fxp, - 0) || - c3n == u3r_mean(b_meta, - 2, &y_shape, - 6, &b_bloq, - 14, &b_kind, - 15, &b_fxp, - 0) || + x_shape = u3h(x_meta); // 2 + x_bloq = u3h(u3t(x_meta)); // 6 + x_kind = u3h(u3t(u3t(x_meta))); // 14 + x_fxp = u3t(u3t(u3t(x_meta))); // 15 + y_shape = u3h(y_meta); // 2 + y_bloq = u3h(u3t(y_meta)); // 6 + y_kind = u3h(u3t(u3t(y_meta))); // 14 + y_fxp = u3t(u3t(u3t(y_meta))); // 15 + rnd = u3h(u3t(u3t(u3t(cor)))); // 30 + if ( c3n == u3ud(x_bloq) || + c3n == u3ud(y_bloq) || + c3n == u3ud(x_kind) || + c3n == u3ud(y_kind) || c3n == u3r_sing(x_shape, y_shape) || - c3n == u3r_sing(a_bloq, b_bloq) || - c3n == u3r_sing(a_kind, b_kind) || - // fxp does not need to match so no check - c3n == u3r_mean(cor, 30, &rnd, 0) + c3n == u3r_sing(x_bloq, y_bloq) || + c3n == u3r_sing(x_kind, y_kind) + // fxp does not need to match here so no check ) { return u3m_bail(c3__exit); } else { - switch (a_kind) { + switch (x_kind) { case c3__real: _set_rounding(rnd); - u3_noun r_data = u3qf_la_add_real(x_data, y_data, x_shape, a_bloq); - return u3nc(u3nq(y_shape, a_bloq, a_kind, a_fxp), r_data); - break; + u3_noun r_data = u3qf_la_add_real(x_data, y_data, x_shape, x_bloq); + return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: return u3_none; @@ -1084,14 +1036,14 @@ u3_noun u3wf_la_sub(u3_noun cor) { - // Each argument is a ray, [=meta data=@ux] - u3_noun a_meta, x_data, - b_meta, y_data; + // Each argument is a ray, [=meta data=@ux] + u3_noun x_meta, x_data, + y_meta, y_data; if ( c3n == u3r_mean(cor, - u3x_sam_4, &a_meta, + u3x_sam_4, &x_meta, u3x_sam_5, &x_data, - u3x_sam_6, &b_meta, + u3x_sam_6, &y_meta, u3x_sam_7, &y_data, 0) || c3n == u3ud(x_data) || @@ -1099,36 +1051,35 @@ { return u3m_bail(c3__exit); } else { - u3_noun x_shape, a_bloq, a_kind, a_fxp, - y_shape, b_bloq, b_kind, b_fxp, + u3_noun x_shape, x_bloq, x_kind, x_fxp, + y_shape, y_bloq, y_kind, y_fxp, rnd; - if ( c3n == u3r_mean(a_meta, - 2, &x_shape, - 6, &a_bloq, - 14, &a_kind, - 15, &a_fxp, - 0) || - c3n == u3r_mean(b_meta, - 2, &y_shape, - 6, &b_bloq, - 14, &b_kind, - 15, &b_fxp, - 0) || + x_shape = u3h(x_meta); // 2 + x_bloq = u3h(u3t(x_meta)); // 6 + x_kind = u3h(u3t(u3t(x_meta))); // 14 + x_fxp = u3t(u3t(u3t(x_meta))); // 15 + y_shape = u3h(y_meta); // 2 + y_bloq = u3h(u3t(y_meta)); // 6 + y_kind = u3h(u3t(u3t(y_meta))); // 14 + y_fxp = u3t(u3t(u3t(y_meta))); // 15 + rnd = u3h(u3t(u3t(u3t(cor)))); // 30 + if ( c3n == u3ud(x_bloq) || + c3n == u3ud(y_bloq) || + c3n == u3ud(x_kind) || + c3n == u3ud(y_kind) || c3n == u3r_sing(x_shape, y_shape) || - c3n == u3r_sing(a_bloq, b_bloq) || - c3n == u3r_sing(a_kind, b_kind) || - // fxp does not need to match so no check - c3n == u3r_mean(cor, 30, &rnd, 0) + c3n == u3r_sing(x_bloq, y_bloq) || + c3n == u3r_sing(x_kind, y_kind) + // fxp does not need to match here so no check ) { return u3m_bail(c3__exit); } else { - switch (a_kind) { + switch (x_kind) { case c3__real: _set_rounding(rnd); - u3_noun r_data = u3qf_la_sub_real(x_data, y_data, x_shape, a_bloq); - return u3nc(u3nq(x_shape, a_bloq, a_kind, a_fxp), r_data); - break; + u3_noun r_data = u3qf_la_sub_real(x_data, y_data, x_shape, x_bloq); + return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: return u3_none; @@ -1140,14 +1091,14 @@ u3_noun u3wf_la_mul(u3_noun cor) { - // Each argument is a ray, [=meta data=@ux] - u3_noun a_meta, x_data, - b_meta, y_data; + // Each argument is a ray, [=meta data=@ux] + u3_noun x_meta, x_data, + y_meta, y_data; if ( c3n == u3r_mean(cor, - u3x_sam_4, &a_meta, + u3x_sam_4, &x_meta, u3x_sam_5, &x_data, - u3x_sam_6, &b_meta, + u3x_sam_6, &y_meta, u3x_sam_7, &y_data, 0) || c3n == u3ud(x_data) || @@ -1155,36 +1106,35 @@ { return u3m_bail(c3__exit); } else { - u3_noun x_shape, a_bloq, a_kind, a_fxp, - y_shape, b_bloq, b_kind, b_fxp, + u3_noun x_shape, x_bloq, x_kind, x_fxp, + y_shape, y_bloq, y_kind, y_fxp, rnd; - if ( c3n == u3r_mean(a_meta, - 2, &x_shape, - 6, &a_bloq, - 14, &a_kind, - 15, &a_fxp, - 0) || - c3n == u3r_mean(b_meta, - 2, &y_shape, - 6, &b_bloq, - 14, &b_kind, - 15, &b_fxp, - 0) || + x_shape = u3h(x_meta); // 2 + x_bloq = u3h(u3t(x_meta)); // 6 + x_kind = u3h(u3t(u3t(x_meta))); // 14 + x_fxp = u3t(u3t(u3t(x_meta))); // 15 + y_shape = u3h(y_meta); // 2 + y_bloq = u3h(u3t(y_meta)); // 6 + y_kind = u3h(u3t(u3t(y_meta))); // 14 + y_fxp = u3t(u3t(u3t(y_meta))); // 15 + rnd = u3h(u3t(u3t(u3t(cor)))); // 30 + if ( c3n == u3ud(x_bloq) || + c3n == u3ud(y_bloq) || + c3n == u3ud(x_kind) || + c3n == u3ud(y_kind) || c3n == u3r_sing(x_shape, y_shape) || - c3n == u3r_sing(a_bloq, b_bloq) || - c3n == u3r_sing(a_kind, b_kind) || - // fxp does not need to match so no check - c3n == u3r_mean(cor, 30, &rnd, 0) + c3n == u3r_sing(x_bloq, y_bloq) || + c3n == u3r_sing(x_kind, y_kind) + // fxp does not need to match here so no check ) { return u3m_bail(c3__exit); } else { - switch (a_kind) { + switch (x_kind) { case c3__real: _set_rounding(rnd); - u3_noun r_data = u3qf_la_mul_real(x_data, y_data, x_shape, a_bloq); - return u3nc(u3nq(x_shape, a_bloq, a_kind, a_fxp), r_data); - break; + u3_noun r_data = u3qf_la_mul_real(x_data, y_data, x_shape, x_bloq); + return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: return u3_none; @@ -1196,14 +1146,14 @@ u3_noun u3wf_la_div(u3_noun cor) { - // Each argument is a ray, [=meta data=@ux] - u3_noun a_meta, x_data, - b_meta, y_data; + // Each argument is a ray, [=meta data=@ux] + u3_noun x_meta, x_data, + y_meta, y_data; if ( c3n == u3r_mean(cor, - u3x_sam_4, &a_meta, + u3x_sam_4, &x_meta, u3x_sam_5, &x_data, - u3x_sam_6, &b_meta, + u3x_sam_6, &y_meta, u3x_sam_7, &y_data, 0) || c3n == u3ud(x_data) || @@ -1211,36 +1161,35 @@ { return u3m_bail(c3__exit); } else { - u3_noun x_shape, a_bloq, a_kind, a_fxp, - y_shape, b_bloq, b_kind, b_fxp, + u3_noun x_shape, x_bloq, x_kind, x_fxp, + y_shape, y_bloq, y_kind, y_fxp, rnd; - if ( c3n == u3r_mean(a_meta, - 2, &x_shape, - 6, &a_bloq, - 14, &a_kind, - 15, &a_fxp, - 0) || - c3n == u3r_mean(b_meta, - 2, &y_shape, - 6, &b_bloq, - 14, &b_kind, - 15, &b_fxp, - 0) || + x_shape = u3h(x_meta); // 2 + x_bloq = u3h(u3t(x_meta)); // 6 + x_kind = u3h(u3t(u3t(x_meta))); // 14 + x_fxp = u3t(u3t(u3t(x_meta))); // 15 + y_shape = u3h(y_meta); // 2 + y_bloq = u3h(u3t(y_meta)); // 6 + y_kind = u3h(u3t(u3t(y_meta))); // 14 + y_fxp = u3t(u3t(u3t(y_meta))); // 15 + rnd = u3h(u3t(u3t(u3t(cor)))); // 30 + if ( c3n == u3ud(x_bloq) || + c3n == u3ud(y_bloq) || + c3n == u3ud(x_kind) || + c3n == u3ud(y_kind) || c3n == u3r_sing(x_shape, y_shape) || - c3n == u3r_sing(a_bloq, b_bloq) || - c3n == u3r_sing(a_kind, b_kind) || - // fxp does not need to match so no check - c3n == u3r_mean(cor, 30, &rnd, 0) + c3n == u3r_sing(x_bloq, y_bloq) || + c3n == u3r_sing(x_kind, y_kind) + // fxp does not need to match here so no check ) { return u3m_bail(c3__exit); } else { - switch (a_kind) { + switch (x_kind) { case c3__real: _set_rounding(rnd); - u3_noun r_data = u3qf_la_div_real(x_data, y_data, x_shape, a_bloq); - return u3nc(u3nq(x_shape, a_bloq, a_kind, a_fxp), r_data); - break; + u3_noun r_data = u3qf_la_div_real(x_data, y_data, x_shape, x_bloq); + return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: return u3_none; @@ -1250,44 +1199,52 @@ } u3_noun - u3wf_la_adds(u3_noun cor) + u3wf_la_mod(u3_noun cor) { - // Each argument is a ray, [=meta data=@ux] - u3_noun a_meta, x_data, n; + // Each argument is a ray, [=meta data=@ux] + u3_noun x_meta, x_data, + y_meta, y_data; if ( c3n == u3r_mean(cor, - u3x_sam_4, &a_meta, + u3x_sam_4, &x_meta, u3x_sam_5, &x_data, - u3x_sam_3, &n, + u3x_sam_6, &y_meta, + u3x_sam_7, &y_data, 0) || c3n == u3ud(x_data) || - c3n == u3ud(n) ) + c3n == u3ud(y_data) ) { return u3m_bail(c3__exit); } else { - u3_noun x_shape, a_bloq, a_kind, a_fxp, + u3_noun x_shape, x_bloq, x_kind, x_fxp, + y_shape, y_bloq, y_kind, y_fxp, rnd; - if ( c3n == u3r_mean(a_meta, - 2, &x_shape, - 6, &a_bloq, - 14, &a_kind, - 15, &a_fxp, - 0) || - // shape does not matter so no check - // bloq does not matter so no check - // kind does not matter so no check - // fxp does not need to match so no check - c3n == u3r_mean(cor, 30, &rnd, 0) + x_shape = u3h(x_meta); // 2 + x_bloq = u3h(u3t(x_meta)); // 6 + x_kind = u3h(u3t(u3t(x_meta))); // 14 + x_fxp = u3t(u3t(u3t(x_meta))); // 15 + y_shape = u3h(y_meta); // 2 + y_bloq = u3h(u3t(y_meta)); // 6 + y_kind = u3h(u3t(u3t(y_meta))); // 14 + y_fxp = u3t(u3t(u3t(y_meta))); // 15 + rnd = u3h(u3t(u3t(u3t(cor)))); // 30 + if ( c3n == u3ud(x_bloq) || + c3n == u3ud(y_bloq) || + c3n == u3ud(x_kind) || + c3n == u3ud(y_kind) || + c3n == u3r_sing(x_shape, y_shape) || + c3n == u3r_sing(x_bloq, y_bloq) || + c3n == u3r_sing(x_kind, y_kind) + // fxp does not need to match here so no check ) { return u3m_bail(c3__exit); } else { - switch (a_kind) { - case c3__real: - _set_rounding(rnd); - u3_noun r_data = u3qf_la_adds_real(x_data, x_shape, a_bloq, n); - return u3nc(u3nq(x_shape, a_bloq, a_kind, a_fxp), r_data); - break; + switch (x_kind) { + case c3__real: ; // XX satisfy label + // Global rounding mode is ignored by SoftFloat conversions so we pass it in. + u3_noun r_data = u3qf_la_mod_real(x_data, y_data, x_shape, x_bloq, rnd); + return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: return u3_none; @@ -1297,13 +1254,13 @@ } u3_noun - u3wf_la_subs(u3_noun cor) + u3wf_la_adds(u3_noun cor) { // Each argument is a ray, [=meta data=@ux] - u3_noun a_meta, x_data, n; + u3_noun x_meta, x_data, n; if ( c3n == u3r_mean(cor, - u3x_sam_4, &a_meta, + u3x_sam_4, &x_meta, u3x_sam_5, &x_data, u3x_sam_3, &n, 0) || @@ -1312,33 +1269,56 @@ { return u3m_bail(c3__exit); } else { - u3_noun x_shape, a_bloq, a_kind, a_fxp, + u3_noun x_shape, x_bloq, x_kind, x_fxp, rnd; - if ( c3n == u3r_mean(a_meta, - 2, &x_shape, - 6, &a_bloq, - 14, &a_kind, - 15, &a_fxp, - 0) || - // shape does not matter so no check - // bloq does not matter so no check - // kind does not matter so no check - // fxp does not need to match so no check - c3n == u3r_mean(cor, 30, &rnd, 0) - ) - { - return u3m_bail(c3__exit); - } else { - switch (a_kind) { - case c3__real: - _set_rounding(rnd); - u3_noun r_data = u3qf_la_subs_real(x_data, x_shape, a_bloq, n); - return u3nc(u3nq(x_shape, a_bloq, a_kind, a_fxp), r_data); - break; + x_shape = u3h(x_meta); // 2 + x_bloq = u3h(u3t(x_meta)); // 6 + x_kind = u3h(u3t(u3t(x_meta))); // 14 + x_fxp = u3t(u3t(u3t(x_meta))); // 15 + rnd = u3h(u3t(u3t(u3t(cor)))); // 30 + switch (x_kind) { + case c3__real: + _set_rounding(rnd); + u3_noun r_data = u3qf_la_adds_real(x_data, n, x_shape, x_bloq); + return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); + + default: + return u3_none; + } + } + } - default: - return u3_none; - } + u3_noun + u3wf_la_subs(u3_noun cor) + { + // Each argument is a ray, [=meta data=@ux] + u3_noun x_meta, x_data, n; + + if ( c3n == u3r_mean(cor, + u3x_sam_4, &x_meta, + u3x_sam_5, &x_data, + u3x_sam_3, &n, + 0) || + c3n == u3ud(x_data) || + c3n == u3ud(n) ) + { + return u3m_bail(c3__exit); + } else { + u3_noun x_shape, x_bloq, x_kind, x_fxp, + rnd; + x_shape = u3h(x_meta); // 2 + x_bloq = u3h(u3t(x_meta)); // 6 + x_kind = u3h(u3t(u3t(x_meta))); // 14 + x_fxp = u3t(u3t(u3t(x_meta))); // 15 + rnd = u3h(u3t(u3t(u3t(cor)))); // 30 + switch (x_kind) { + case c3__real: + _set_rounding(rnd); + u3_noun r_data = u3qf_la_subs_real(x_data, n, x_shape, x_bloq); + return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); + + default: + return u3_none; } } } @@ -1347,10 +1327,10 @@ u3wf_la_muls(u3_noun cor) { // Each argument is a ray, [=meta data=@ux] - u3_noun a_meta, x_data, n; + u3_noun x_meta, x_data, n; if ( c3n == u3r_mean(cor, - u3x_sam_4, &a_meta, + u3x_sam_4, &x_meta, u3x_sam_5, &x_data, u3x_sam_3, &n, 0) || @@ -1359,33 +1339,21 @@ { return u3m_bail(c3__exit); } else { - u3_noun x_shape, a_bloq, a_kind, a_fxp, + u3_noun x_shape, x_bloq, x_kind, x_fxp, rnd; - if ( c3n == u3r_mean(a_meta, - 2, &x_shape, - 6, &a_bloq, - 14, &a_kind, - 15, &a_fxp, - 0) || - // shape does not matter so no check - // bloq does not matter so no check - // kind does not matter so no check - // fxp does not need to match so no check - c3n == u3r_mean(cor, 30, &rnd, 0) - ) - { - return u3m_bail(c3__exit); - } else { - switch (a_kind) { - case c3__real: - _set_rounding(rnd); - u3_noun r_data = u3qf_la_muls_real(x_data, x_shape, a_bloq, n); - return u3nc(u3nq(x_shape, a_bloq, a_kind, a_fxp), r_data); - break; - - default: - return u3_none; - } + x_shape = u3h(x_meta); // 2 + x_bloq = u3h(u3t(x_meta)); // 6 + x_kind = u3h(u3t(u3t(x_meta))); // 14 + x_fxp = u3t(u3t(u3t(x_meta))); // 15 + rnd = u3h(u3t(u3t(u3t(cor)))); // 30 + switch (x_kind) { + case c3__real: + _set_rounding(rnd); + u3_noun r_data = u3qf_la_muls_real(x_data, n, x_shape, x_bloq); + return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); + + default: + return u3_none; } } } @@ -1394,10 +1362,10 @@ u3wf_la_divs(u3_noun cor) { // Each argument is a ray, [=meta data=@ux] - u3_noun a_meta, x_data, n; + u3_noun x_meta, x_data, n; if ( c3n == u3r_mean(cor, - u3x_sam_4, &a_meta, + u3x_sam_4, &x_meta, u3x_sam_5, &x_data, u3x_sam_3, &n, 0) || @@ -1406,33 +1374,21 @@ { return u3m_bail(c3__exit); } else { - u3_noun x_shape, a_bloq, a_kind, a_fxp, + u3_noun x_shape, x_bloq, x_kind, x_fxp, rnd; - if ( c3n == u3r_mean(a_meta, - 2, &x_shape, - 6, &a_bloq, - 14, &a_kind, - 15, &a_fxp, - 0) || - // shape does not matter so no check - // bloq does not matter so no check - // kind does not matter so no check - // fxp does not need to match so no check - c3n == u3r_mean(cor, 30, &rnd, 0) - ) - { - return u3m_bail(c3__exit); - } else { - switch (a_kind) { - case c3__real: - _set_rounding(rnd); - u3_noun r_data = u3qf_la_divs_real(x_data, x_shape, a_bloq, n); - return u3nc(u3nq(x_shape, a_bloq, a_kind, a_fxp), r_data); - break; - - default: - return u3_none; - } + x_shape = u3h(x_meta); // 2 + x_bloq = u3h(u3t(x_meta)); // 6 + x_kind = u3h(u3t(u3t(x_meta))); // 14 + x_fxp = u3t(u3t(u3t(x_meta))); // 15 + rnd = u3h(u3t(u3t(u3t(cor)))); // 30 + switch (x_kind) { + case c3__real: + _set_rounding(rnd); + u3_noun r_data = u3qf_la_divs_real(x_data, n, x_shape, x_bloq); + return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); + + default: + return u3_none; } } } @@ -1441,13 +1397,13 @@ u3wf_la_dot(u3_noun cor) { // Each argument is a ray, [=meta data=@ux] - u3_noun a_meta, x_data, - b_meta, y_data; + u3_noun x_meta, x_data, + y_meta, y_data; if ( c3n == u3r_mean(cor, - u3x_sam_4, &a_meta, + u3x_sam_4, &x_meta, u3x_sam_5, &x_data, - u3x_sam_6, &b_meta, + u3x_sam_6, &y_meta, u3x_sam_7, &y_data, 0) || c3n == u3ud(x_data) || @@ -1455,36 +1411,35 @@ { return u3m_bail(c3__exit); } else { - u3_noun x_shape, a_bloq, a_kind, a_fxp, - y_shape, b_bloq, b_kind, b_fxp, + u3_noun x_shape, x_bloq, x_kind, x_fxp, + y_shape, y_bloq, y_kind, y_fxp, rnd; - if ( c3n == u3r_mean(a_meta, - 2, &x_shape, - 6, &a_bloq, - 14, &a_kind, - 15, &a_fxp, - 0) || - c3n == u3r_mean(b_meta, - 2, &y_shape, - 6, &b_bloq, - 14, &b_kind, - 15, &b_fxp, - 0) || + x_shape = u3h(x_meta); // 2 + x_bloq = u3h(u3t(x_meta)); // 6 + x_kind = u3h(u3t(u3t(x_meta))); // 14 + x_fxp = u3t(u3t(u3t(x_meta))); // 15 + y_shape = u3h(y_meta); // 2 + y_bloq = u3h(u3t(y_meta)); // 6 + y_kind = u3h(u3t(u3t(y_meta))); // 14 + y_fxp = u3t(u3t(u3t(y_meta))); // 15 + rnd = u3h(u3t(u3t(u3t(cor)))); // 30 + if ( c3n == u3ud(x_bloq) || + c3n == u3ud(y_bloq) || + c3n == u3ud(x_kind) || + c3n == u3ud(y_kind) || c3n == u3r_sing(x_shape, y_shape) || - c3n == u3r_sing(a_bloq, b_bloq) || - c3n == u3r_sing(a_kind, b_kind) || - c3n == u3r_sing(a_fxp, b_fxp) || - c3n == u3r_mean(cor, 30, &rnd, 0) + c3n == u3r_sing(x_bloq, y_bloq) || + c3n == u3r_sing(x_kind, y_kind) || + c3n == u3r_sing(x_fxp, y_fxp) ) { return u3m_bail(c3__exit); } else { - switch (a_kind) { + switch (x_kind) { case c3__real: _set_rounding(rnd); - u3_noun r_data = u3qf_la_dot_real(x_data, y_data, x_shape, a_bloq); - return u3nc(u3nq(x_shape, a_bloq, a_kind, a_fxp), r_data); - break; + u3_noun r_data = u3qf_la_dot_real(x_data, y_data, x_shape, x_bloq); + return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: return u3_none; @@ -1497,36 +1452,36 @@ u3wf_la_diag(u3_noun cor) { // Each argument is a ray, [=meta data=@ux] - u3_noun a_meta, x_data; + u3_noun x_meta, x_data; if ( c3n == u3r_mean(cor, - u3x_sam_2, &a_meta, + u3x_sam_2, &x_meta, u3x_sam_3, &x_data, 0) || c3n == u3ud(x_data) ) { return u3m_bail(c3__exit); } else { - u3_noun x_shape, a_bloq, a_kind, a_fxp, + u3_noun x_shape, x_bloq, x_kind, x_fxp, rnd; - if ( c3n == u3r_mean(a_meta, + if ( c3n == u3r_mean(x_meta, 2, &x_shape, - 6, &a_bloq, - 14, &a_kind, - 15, &a_fxp, + 6, &x_bloq, + 14, &x_kind, + 15, &x_fxp, 0) // c3n == u3r_sing(x_shape, y_shape) || - // c3n == u3r_sing(a_bloq, b_bloq) || - // c3n == u3r_sing(a_kind, b_kind) || - // c3n == u3r_sing(a_fxp, b_fxp) || - // c3n == u3r_mean(cor, 30, &rnd, 0) + // c3n == u3r_sing(x_bloq, y_bloq) || + // c3n == u3r_sing(x_kind, y_kind) || + // c3n == u3r_sing(x_fxp, y_fxp) || + // c3n == u3r_mean(cor, u3x_con_sam, &rnd, 0) ) { return u3m_bail(c3__exit); } else { - u3_noun r_data = u3qf_la_diag(x_data, x_shape, a_bloq); - uint64_t len_x0 = _get_dims(x_shape)[0]; - return u3nc(u3nq(u3nt(len_x0, 0x1, u3_nul), a_bloq, a_kind, a_fxp), r_data); + u3_noun r_data = u3qf_la_diag(x_data, x_shape, x_bloq); + c3_d len_x0 = _get_dims(x_shape)[0]; + return u3nc(u3nq(u3nt(len_x0, 0x1, u3_nul), x_bloq, x_kind, x_fxp), r_data); } } } @@ -1535,38 +1490,38 @@ u3wf_la_trace(u3_noun cor) { // Each argument is a ray, [=meta data=@ux] - u3_noun a_meta, x_data; + u3_noun x_meta, x_data; if ( c3n == u3r_mean(cor, - u3x_sam_2, &a_meta, + u3x_sam_2, &x_meta, u3x_sam_3, &x_data, 0) || c3n == u3ud(x_data) ) { return u3m_bail(c3__exit); } else { - u3_noun x_shape, a_bloq, a_kind, a_fxp, + u3_noun x_shape, x_bloq, x_kind, x_fxp, rnd; - if ( c3n == u3r_mean(a_meta, + if ( c3n == u3r_mean(x_meta, 2, &x_shape, - 6, &a_bloq, - 14, &a_kind, - 15, &a_fxp, + 6, &x_bloq, + 14, &x_kind, + 15, &x_fxp, 0) // c3n == u3r_sing(x_shape, y_shape) || - // c3n == u3r_sing(a_bloq, b_bloq) || - // c3n == u3r_sing(a_kind, b_kind) || - // c3n == u3r_sing(a_fxp, b_fxp) || - // c3n == u3r_mean(cor, 30, &rnd, 0) + // c3n == u3r_sing(x_bloq, y_bloq) || + // c3n == u3r_sing(x_kind, y_kind) || + // c3n == u3r_sing(x_fxp, y_fxp) || + // c3n == u3r_mean(cor, u3x_con_sam, &rnd, 0) ) { return u3m_bail(c3__exit); } else { - switch (a_kind) { + switch (x_kind) { case c3__real: _set_rounding(rnd); - u3_noun r_data = u3qf_la_trace_real(x_data, x_shape, a_bloq); - return u3nc(u3nq(u3nt(0x1, 0x1, u3_nul), a_bloq, a_kind, a_fxp), r_data); + u3_noun r_data = u3qf_la_trace_real(x_data, x_shape, x_bloq); + return u3nc(u3nq(u3nt(0x1, 0x1, u3_nul), x_bloq, x_kind, x_fxp), r_data); break; default: @@ -1580,13 +1535,13 @@ u3wf_la_mmul(u3_noun cor) { // Each argument is a ray, [=meta data=@ux] - u3_noun a_meta, x_data, - b_meta, y_data; + u3_noun x_meta, x_data, + y_meta, y_data; if ( c3n == u3r_mean(cor, - u3x_sam_4, &a_meta, + u3x_sam_4, &x_meta, u3x_sam_5, &x_data, - u3x_sam_6, &b_meta, + u3x_sam_6, &y_meta, u3x_sam_7, &y_data, 0) || c3n == u3ud(x_data) || @@ -1594,33 +1549,33 @@ { return u3m_bail(c3__exit); } else { - u3_noun x_shape, a_bloq, a_kind, a_fxp, - y_shape, b_bloq, b_kind, b_fxp, + u3_noun x_shape, x_bloq, x_kind, x_fxp, + y_shape, y_bloq, y_kind, y_fxp, rnd; - if ( c3n == u3r_mean(a_meta, + if ( c3n == u3r_mean(x_meta, 2, &x_shape, - 6, &a_bloq, - 14, &a_kind, - 15, &a_fxp, + 6, &x_bloq, + 14, &x_kind, + 15, &x_fxp, 0) || - c3n == u3r_mean(b_meta, + c3n == u3r_mean(y_meta, 2, &y_shape, - 6, &b_bloq, - 14, &b_kind, - 15, &b_fxp, + 6, &y_bloq, + 14, &y_kind, + 15, &y_fxp, 0) || - c3n == u3r_sing(a_bloq, b_bloq) || - c3n == u3r_sing(a_kind, b_kind) || + c3n == u3r_sing(x_bloq, y_bloq) || + c3n == u3r_sing(x_kind, y_kind) || // fxp does not need to match so no check - c3n == u3r_mean(cor, 30, &rnd, 0) + c3n == u3r_mean(cor, u3x_con_sam, &rnd, 0) ) { return u3m_bail(c3__exit); } else { - switch (a_kind) { + switch (x_kind) { case c3__real: _set_rounding(rnd); - return u3qf_la_mmul_real(x_data, y_data, x_shape, y_shape, a_bloq); + return u3qf_la_mmul_real(x_data, y_data, x_shape, y_shape, x_bloq); break; default: diff --git a/pkg/noun/jets/q.h b/pkg/noun/jets/q.h index db84af39d1..63d0bb724e 100644 --- a/pkg/noun/jets/q.h +++ b/pkg/noun/jets/q.h @@ -251,6 +251,7 @@ u3_noun u3qf_la_sub_real(u3_noun, u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_mul_real(u3_noun, u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_div_real(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qf_la_mod_real(u3_noun, u3_noun, u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_adds_real(u3_noun, u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_subs_real(u3_noun, u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_muls_real(u3_noun, u3_noun, u3_noun, u3_noun); diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index 084ba01e98..d8375dc4fc 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -2155,6 +2155,7 @@ static u3j_harm _139_hex__lagoon_add_a[] = {{".2", u3wf_la_add}, {}}; static u3j_harm _139_hex__lagoon_sub_a[] = {{".2", u3wf_la_sub}, {}}; static u3j_harm _139_hex__lagoon_mul_a[] = {{".2", u3wf_la_mul}, {}}; static u3j_harm _139_hex__lagoon_div_a[] = {{".2", u3wf_la_div}, {}}; +static u3j_harm _139_hex__lagoon_mod_a[] = {{".2", u3wf_la_mod}, {}}; static u3j_harm _139_hex__lagoon_adds_a[] = {{".2", u3wf_la_adds}, {}}; static u3j_harm _139_hex__lagoon_subs_a[] = {{".2", u3wf_la_subs}, {}}; static u3j_harm _139_hex__lagoon_muls_a[] = {{".2", u3wf_la_muls}, {}}; @@ -2168,6 +2169,7 @@ static u3j_core _139_hex__la_core_d[] = { "sub-rays", 7, _139_hex__lagoon_sub_a, 0, no_hashes }, { "mul-rays", 7, _139_hex__lagoon_mul_a, 0, no_hashes }, { "div-rays", 7, _139_hex__lagoon_div_a, 0, no_hashes }, + { "mod-rays", 7, _139_hex__lagoon_mod_a, 0, no_hashes }, { "add-scal", 7, _139_hex__lagoon_adds_a, 0, no_hashes }, { "sub-scal", 7, _139_hex__lagoon_subs_a, 0, no_hashes }, { "mul-scal", 7, _139_hex__lagoon_muls_a, 0, no_hashes }, diff --git a/pkg/noun/jets/w.h b/pkg/noun/jets/w.h index 2bd87e6551..0d8dadbf2f 100644 --- a/pkg/noun/jets/w.h +++ b/pkg/noun/jets/w.h @@ -335,6 +335,7 @@ u3_noun u3wf_la_sub(u3_noun); u3_noun u3wf_la_mul(u3_noun); u3_noun u3wf_la_div(u3_noun); + u3_noun u3wf_la_mod(u3_noun); u3_noun u3wf_la_adds(u3_noun); u3_noun u3wf_la_subs(u3_noun); u3_noun u3wf_la_muls(u3_noun); From ee9d1e10f19852a242a964b3974a0df51fd9f394 Mon Sep 17 00:00:00 2001 From: Sigilante Date: Fri, 12 Apr 2024 19:27:09 -0500 Subject: [PATCH 094/430] Posting some jets. --- pkg/noun/jets/f/lagoon.c | 29 ++--------------------------- 1 file changed, 2 insertions(+), 27 deletions(-) diff --git a/pkg/noun/jets/f/lagoon.c b/pkg/noun/jets/f/lagoon.c index 84c58bd191..9edb40095a 100644 --- a/pkg/noun/jets/f/lagoon.c +++ b/pkg/noun/jets/f/lagoon.c @@ -99,18 +99,6 @@ return dims; } -/* -*/ - static inline size_t _get_array_length(c3_d* array) - { - size_t n = sizeof(array)/sizeof(array[0]); - for (size_t i = 0; i < n; i++) { - fprintf(stderr, "%x ", array[i]); - } - fprintf(stderr, " => %x \n", n); - return n; - } - /* add - axpy = 1*x+y */ u3_noun @@ -444,31 +432,18 @@ for (c3_d i = 0; i < len_x; i++) { float128_t x_val128 = ((float128_t*)x_bytes)[i]; float128_t y_val128 = ((float128_t*)y_bytes)[i]; - fprintf(stderr, "x_val128: %llx %llx\r\n", x_val128.v[0], x_val128.v[1]); - fprintf(stderr, "y_val128: %llx %llx\r\n", y_val128.v[0], y_val128.v[1]); // Perform division x/n float128_t div_result128; - // float128_t div_result128 = f128_div(x_val128, y_val128); f128M_div((float128_t*)&x_val128, (float128_t*)&y_val128, (float128_t*)&div_result128); - fprintf(stderr, "div_result128: %llx %llx\r\n", div_result128.v[0], div_result128.v[1]); // Compute floor of the division result - int64_t floor_result128 = f128_to_i64(div_result128, softfloat_round_minMag, false); - fprintf(stderr, "floor_result128: %llx\r\n", floor_result128); + int64_t floor_result128 = f128_to_i64(div_result128, rnd, false); float128_t floor_float128 = i64_to_f128(floor_result128); - fprintf(stderr, "floor_float128: %llx %llx\r\n", floor_float128.v[0], floor_float128.v[1]); // Multiply n by floor(x/n) float128_t mult_result128; - // float128_t mult_result128 = f128_mul(y_val128, floor_float128); f128M_mul(((float128_t*)&y_val128), ((float128_t*)&floor_float128), ((float128_t*)&mult_result128)); - fprintf(stderr, "mult_result128: %llx %llx\r\n", mult_result128.v[0], mult_result128.v[1]); // Compute remainder: x - n * floor(x/n) - // ((float128_t*)y_bytes)[i] = f128_sub(x_val128, mult_result128); - f128M_div(((float128_t*)&x_val128), ((float128_t*)&mult_result128), &(((float128_t*)y_bytes)[i])); - fprintf(stderr, "y_bytes: %llx %llx\r\n", ((float128_t*)y_bytes)[i].v[0], ((float128_t*)y_bytes)[i].v[1]); + f128M_sub(((float128_t*)&x_val128), ((float128_t*)&mult_result128), &(((float128_t*)y_bytes)[i])); } - // for (c3_d i = 0; i < len_x; i++) { - // f128M_div(&(((float128_t*)y_bytes)[i]), &(((float128_t*)x_bytes)[i]), &(((float128_t*)y_bytes)[i])); - // } break; } From a5c79a519c476edf0ec040567e9542618c063da8 Mon Sep 17 00:00:00 2001 From: Sigilante Date: Sat, 13 Apr 2024 12:37:46 -0500 Subject: [PATCH 095/430] Post mod/mods jets. --- pkg/noun/jets/f/lagoon.c | 217 ++++++++++++++++++++++++++++++++++----- pkg/noun/jets/q.h | 3 +- pkg/noun/jets/tree.c | 2 + pkg/noun/jets/w.h | 1 + 4 files changed, 198 insertions(+), 25 deletions(-) diff --git a/pkg/noun/jets/f/lagoon.c b/pkg/noun/jets/f/lagoon.c index 9edb40095a..8ae1e77e03 100644 --- a/pkg/noun/jets/f/lagoon.c +++ b/pkg/noun/jets/f/lagoon.c @@ -355,8 +355,7 @@ u3qf_la_mod_real(u3_noun x_data, u3_noun y_data, u3_noun shape, - u3_noun bloq, - u3_noun rnd) + u3_noun bloq) { // Fence on valid bloq size. if (bloq < 4 || bloq > 7) { @@ -387,7 +386,7 @@ // Perform division x/n float16_t div_result16 = f16_div(x_val16, y_val16); // Compute floor of the division result - int64_t floor_result16 = f16_to_i64(div_result16, rnd, false); + int64_t floor_result16 = f16_to_i64(div_result16, softfloat_round_minMag, false); float16_t floor_float16 = i64_to_f16(floor_result16); // Multiply n by floor(x/n) float16_t mult_result16 = f16_mul(y_val16, floor_float16); @@ -403,7 +402,7 @@ // Perform division x/n float32_t div_result32 = f32_div(x_val32, y_val32); // Compute floor of the division result - int64_t floor_result32 = f32_to_i64(div_result32, rnd, false); + int64_t floor_result32 = f32_to_i64(div_result32, softfloat_round_minMag, false); float32_t floor_float32 = i64_to_f32(floor_result32); // Multiply n by floor(x/n) float32_t mult_result32 = f32_mul(y_val32, floor_float32); @@ -419,7 +418,7 @@ // Perform division x/n float64_t div_result64 = f64_div(x_val64, y_val64); // Compute floor of the division result - int64_t floor_result64 = f64_to_i64(div_result64, rnd, false); + int64_t floor_result64 = f64_to_i64(div_result64, softfloat_round_minMag, false); float64_t floor_float64 = i64_to_f64(floor_result64); // Multiply n by floor(x/n) float64_t mult_result64 = f64_mul(y_val64, floor_float64); @@ -436,7 +435,7 @@ float128_t div_result128; f128M_div((float128_t*)&x_val128, (float128_t*)&y_val128, (float128_t*)&div_result128); // Compute floor of the division result - int64_t floor_result128 = f128_to_i64(div_result128, rnd, false); + int64_t floor_result128 = f128_to_i64(div_result128, softfloat_round_minMag, false); float128_t floor_float128 = i64_to_f128(floor_result128); // Multiply n by floor(x/n) float128_t mult_result128; @@ -710,35 +709,170 @@ u3r_bytes(0, siz_x, x_bytes, x_data); x_bytes[siz_x] = 1; // pin head - float16_t n16; - float32_t n32; - float64_t n64; - float128_t n128; + float16_t in16; + float32_t in32; + float64_t in64; + float128_t in128; // Switch on the block size. switch (bloq) { case 4: - u3r_bytes(0, 2, (c3_y*)&(n16.v), n); - n16 = f16_div((float16_t){SB_REAL16_ONE}, n16); - hscal(len_x, n16, (float16_t*)x_bytes, 1); + // XX note that in16 is doing double duty here + u3r_bytes(0, 2, (c3_y*)&(in16.v), n); + in16 = f16_div((float16_t){SB_REAL16_ONE}, in16); + hscal(len_x, in16, (float16_t*)x_bytes, 1); break; case 5: + // XX note that in32 is doing double duty here + u3r_bytes(0, 4, (c3_y*)&(in32.v), n); + in32 = f32_div((float32_t){SB_REAL32_ONE}, in32); + sscal(len_x, in32, (float32_t*)x_bytes, 1); + break; + + case 6: + // XX note that in64 is doing double duty here + u3r_bytes(0, 8, (c3_y*)&(in64.v), n); + in64 = f64_div((float64_t){SB_REAL64_ONE}, in64); + dscal(len_x, in64, (float64_t*)x_bytes, 1); + break; + + case 7: + // XX note that in128 is doing double duty here + u3r_bytes(0, 16, (c3_y*)&(in128.v[0]), n); + f128M_div(&((float128_t){SB_REAL128L_ONE,SB_REAL128U_ONE}), &in128, &in128); + qscal(len_x, in128, (float128_t*)x_bytes, 1); + break; + } + + // r_data is the result noun of [data] + u3_noun r_data = u3i_bytes((siz_x+1)*sizeof(c3_y), x_bytes); + + // Clean up and return. + u3a_free(x_bytes); + + return r_data; + } + +/* mods - x % [n] = x - r*floor(x/r) + remainder after scalar division +*/ + u3_noun + u3qf_la_mods_real(u3_noun x_data, + u3_noun n, + u3_noun shape, + u3_noun bloq) + { + // Fence on valid bloq size. + if (bloq < 4 || bloq > 7) { + return u3_none; + } + + // Unpack the data as a byte array. We assume total length < 2**64. + // len_x is length in base units + c3_d len_x = _get_length(shape); + fprintf(stderr, "len_x: %ld\r\n", len_x); + + // siz_x is length in bytes + c3_d siz_x = len_x * pow(2, bloq-3); + fprintf(stderr, "siz_x: %ld\r\n", siz_x); + + // x_bytes is the data array (w/ leading 0x1, skipped by ?axpy) + // we reuse it for results for parsimony + c3_y* x_bytes = (c3_y*)u3a_malloc((siz_x+1)*sizeof(c3_y)); + u3r_bytes(0, siz_x+1, x_bytes, x_data); + for (c3_d i = 0; i < siz_x+1; i++) { + fprintf(stderr, "x_bytes[%ld]: %x\r\n", i, x_bytes[i]); + } + + float16_t n16, in16; + float32_t n32, in32; + float64_t n64, in64; + float128_t n128, in128; + + // Switch on the block size. + switch (bloq) { + case 4: + u3r_bytes(0, 2, (c3_y*)&n16, n); + in16 = f16_div((float16_t){SB_REAL16_ONE}, n16); + + for (c3_d i = 0; i < len_x; i++) { + float16_t x_val16 = ((float16_t*)x_bytes)[i]; + // Perform division x/n + float16_t div_result16 = f16_mul(in16, x_val16); + // Compute floor of the division result + int64_t floor_result16 = f16_to_i64(div_result16, softfloat_round_minMag, false); + float16_t floor_float16 = i64_to_f16(floor_result16); + // Multiply n by floor(x/n) + float16_t mult_result16 = f16_mul(n16, floor_float16); + // Compute remainder: x - n * floor(x/n) + ((float16_t*)x_bytes)[i] = f16_sub(x_val16, mult_result16); + } + break; + + case 5: + u3l_log("n: %x", n); u3r_bytes(0, 4, (c3_y*)&(n32.v), n); - n32 = f32_div((float32_t){SB_REAL32_ONE}, n32); - sscal(len_x, n32, (float32_t*)x_bytes, 1); + fprintf(stderr, "n32: %f\r\n", n32.v); + in32 = f32_div((float32_t){SB_REAL32_ONE}, n32); + fprintf(stderr, "in32: %f\r\n", in32); + + for (c3_d i = 0; i < len_x; i++) { + float32_t x_val32 = ((float32_t*)x_bytes)[i]; + fprintf(stderr, "x_val32: %f\r\n", (float32_t)x_val32); + // Perform division x/n + float32_t div_result32 = f32_mul((float32_t)in32, (float32_t)x_val32); + fprintf(stderr, "div_result32: %f\r\n", div_result32); + // Compute floor of the division result + int64_t floor_result32 = f32_to_i64(div_result32, softfloat_round_minMag, false); + fprintf(stderr, "floor_result32: %ld\r\n", floor_result32); + float32_t floor_float32 = i64_to_f32(floor_result32); + fprintf(stderr, "floor_float32: %f\r\n", floor_float32); + // Multiply n by floor(x/n) + float32_t mult_result32 = f32_mul(n32, floor_float32); + fprintf(stderr, "mult_result32: %f\r\n", mult_result32); + // Compute remainder: x - n * floor(x/n) + ((float32_t*)x_bytes)[i] = f32_sub(x_val32, mult_result32); + fprintf(stderr, "x_bytes[i]: %f\r\n\r\n", ((float32_t*)x_bytes)[i]); + } break; case 6: - u3r_bytes(0, 8, (c3_y*)&(n64.v), n); - n64 = f64_div((float64_t){SB_REAL64_ONE}, n64); - dscal(len_x, n64, (float64_t*)x_bytes, 1); + u3r_bytes(0, 8, (c3_y*)&n64, n); + in64 = f64_div((float64_t){SB_REAL64_ONE}, n64); + + for (c3_d i = 0; i < len_x; i++) { + float64_t x_val64 = ((float64_t*)x_bytes)[i]; + // Perform division x/n + float64_t div_result64 = f64_mul(in64, x_val64); + // Compute floor of the division result + int64_t floor_result64 = f64_to_i64(div_result64, softfloat_round_minMag, false); + float64_t floor_float64 = i64_to_f64(floor_result64); + // Multiply n by floor(x/n) + float64_t mult_result64 = f64_mul(n64, floor_float64); + // Compute remainder: x - n * floor(x/n) + ((float64_t*)x_bytes)[i] = f64_sub(x_val64, mult_result64); + } break; case 7: - u3r_bytes(0, 16, (c3_y*)&(n128.v[0]), n); - f128M_div(&((float128_t){SB_REAL128L_ONE,SB_REAL128U_ONE}), &n128, &n128); - qscal(len_x, n128, (float128_t*)x_bytes, 1); + u3r_bytes(0, 16, (c3_y*)&n128, n); + f128M_div(&((float128_t){SB_REAL128L_ONE,SB_REAL128U_ZERO}), &n128, &in128); + + for (c3_d i = 0; i < len_x; i++) { + float128_t x_val128 = ((float128_t*)x_bytes)[i]; + // Perform division x/n + float128_t div_result128; + f128M_mul((float128_t*)&in128, (float128_t*)&x_val128, (float128_t*)&div_result128); + // Compute floor of the division result + int64_t floor_result128 = f128_to_i64(div_result128, softfloat_round_minMag, false); + float128_t floor_float128 = i64_to_f128(floor_result128); + // Multiply n by floor(x/n) + float128_t mult_result128; + f128M_mul(((float128_t*)&n128), ((float128_t*)&floor_float128), ((float128_t*)&mult_result128)); + // Compute remainder: x - n * floor(x/n) + f128M_sub(((float128_t*)&x_val128), ((float128_t*)&mult_result128), &(((float128_t*)x_bytes)[i])); + } break; } @@ -1216,9 +1350,9 @@ return u3m_bail(c3__exit); } else { switch (x_kind) { - case c3__real: ; // XX satisfy label - // Global rounding mode is ignored by SoftFloat conversions so we pass it in. - u3_noun r_data = u3qf_la_mod_real(x_data, y_data, x_shape, x_bloq, rnd); + case c3__real: + _set_rounding(rnd); + u3_noun r_data = u3qf_la_mod_real(x_data, y_data, x_shape, x_bloq); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -1368,6 +1502,41 @@ } } + u3_noun + u3wf_la_mods(u3_noun cor) + { + // Each argument is a ray, [=meta data=@ux] + u3_noun x_meta, x_data, n; + + if ( c3n == u3r_mean(cor, + u3x_sam_4, &x_meta, + u3x_sam_5, &x_data, + u3x_sam_3, &n, + 0) || + c3n == u3ud(x_data) || + c3n == u3ud(n) ) + { + return u3m_bail(c3__exit); + } else { + u3_noun x_shape, x_bloq, x_kind, x_fxp, + rnd; + x_shape = u3h(x_meta); // 2 + x_bloq = u3h(u3t(x_meta)); // 6 + x_kind = u3h(u3t(u3t(x_meta))); // 14 + x_fxp = u3t(u3t(u3t(x_meta))); // 15 + rnd = u3h(u3t(u3t(u3t(cor)))); // 30 + switch (x_kind) { + case c3__real: + _set_rounding(rnd); + u3_noun r_data = u3qf_la_mods_real(x_data, n, x_shape, x_bloq); + return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); + + default: + return u3_none; + } + } + } + u3_noun u3wf_la_dot(u3_noun cor) { diff --git a/pkg/noun/jets/q.h b/pkg/noun/jets/q.h index 63d0bb724e..0b504b185e 100644 --- a/pkg/noun/jets/q.h +++ b/pkg/noun/jets/q.h @@ -251,11 +251,12 @@ u3_noun u3qf_la_sub_real(u3_noun, u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_mul_real(u3_noun, u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_div_real(u3_noun, u3_noun, u3_noun, u3_noun); - u3_noun u3qf_la_mod_real(u3_noun, u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qf_la_mod_real(u3_noun, u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_adds_real(u3_noun, u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_subs_real(u3_noun, u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_muls_real(u3_noun, u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_divs_real(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qf_la_mods_real(u3_noun, u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_dot_real(u3_noun, u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_diag(u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_trace_real(u3_noun, u3_noun, u3_noun); diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index d8375dc4fc..c6c148d83f 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -2160,6 +2160,7 @@ static u3j_harm _139_hex__lagoon_adds_a[] = {{".2", u3wf_la_adds}, {}}; static u3j_harm _139_hex__lagoon_subs_a[] = {{".2", u3wf_la_subs}, {}}; static u3j_harm _139_hex__lagoon_muls_a[] = {{".2", u3wf_la_muls}, {}}; static u3j_harm _139_hex__lagoon_divs_a[] = {{".2", u3wf_la_divs}, {}}; +static u3j_harm _139_hex__lagoon_mods_a[] = {{".2", u3wf_la_mods}, {}}; static u3j_harm _139_hex__lagoon_dot_a[] = {{".2", u3wf_la_dot}, {}}; static u3j_harm _139_hex__lagoon_diag_a[] = {{".2", u3wf_la_diag}, {}}; static u3j_harm _139_hex__lagoon_trace_a[]= {{".2", u3wf_la_trace}, {}}; @@ -2174,6 +2175,7 @@ static u3j_core _139_hex__la_core_d[] = { "sub-scal", 7, _139_hex__lagoon_subs_a, 0, no_hashes }, { "mul-scal", 7, _139_hex__lagoon_muls_a, 0, no_hashes }, { "div-scal", 7, _139_hex__lagoon_divs_a, 0, no_hashes }, + { "mod-scal", 7, _139_hex__lagoon_mods_a, 0, no_hashes }, { "dot", 7, _139_hex__lagoon_dot_a, 0, no_hashes }, { "diag", 7, _139_hex__lagoon_diag_a, 0, no_hashes }, { "trace", 7, _139_hex__lagoon_trace_a,0, no_hashes }, diff --git a/pkg/noun/jets/w.h b/pkg/noun/jets/w.h index 0d8dadbf2f..f36e490e2c 100644 --- a/pkg/noun/jets/w.h +++ b/pkg/noun/jets/w.h @@ -340,6 +340,7 @@ u3_noun u3wf_la_subs(u3_noun); u3_noun u3wf_la_muls(u3_noun); u3_noun u3wf_la_divs(u3_noun); + u3_noun u3wf_la_mods(u3_noun); u3_noun u3wf_la_dot(u3_noun); u3_noun u3wf_la_diag(u3_noun); u3_noun u3wf_la_trace(u3_noun); From 110580373116fb645e4bf47ff8436171ff675b8d Mon Sep 17 00:00:00 2001 From: Sigilante Date: Mon, 15 Apr 2024 15:00:36 -0500 Subject: [PATCH 096/430] Add transpose and fixed other jets. --- pkg/noun/jets/f/lagoon.c | 208 +++++++++++++++++++++++++-------------- pkg/noun/jets/q.h | 1 + pkg/noun/jets/tree.c | 2 + pkg/noun/jets/w.h | 1 + 4 files changed, 140 insertions(+), 72 deletions(-) diff --git a/pkg/noun/jets/f/lagoon.c b/pkg/noun/jets/f/lagoon.c index 8ae1e77e03..1318b532a2 100644 --- a/pkg/noun/jets/f/lagoon.c +++ b/pkg/noun/jets/f/lagoon.c @@ -491,7 +491,7 @@ // Switch on the block size. We assume that n fits in the target block size; Hoon typecheck should prevent. switch (bloq) { case 4: - u3r_bytes(0, 2, (c3_y*)&n16, n); + u3r_bytes(0, 2, (c3_y*)&(n16.v), n); // set y to [n] for (c3_d i = 0; i < len_x; i++) { ((float16_t*)y_bytes)[i] = n16; @@ -500,7 +500,7 @@ break; case 5: - u3r_bytes(0, 4, (c3_y*)&n32, n); + u3r_bytes(0, 4, (c3_y*)&(n32.v), n); // set y to [n] for (c3_d i = 0; i < len_x; i++) { ((float32_t*)y_bytes)[i] = n32; @@ -509,7 +509,7 @@ break; case 6: - u3r_bytes(0, 8, (c3_y*)&n64, n); + u3r_bytes(0, 8, (c3_y*)&(n64.v), n); // set y to [n] for (c3_d i = 0; i < len_x; i++) { ((float64_t*)y_bytes)[i] = n64; @@ -518,7 +518,7 @@ break; case 7: - u3r_bytes(0, 16, (c3_y*)&n128, n); + u3r_bytes(0, 16, (c3_y*)&(n128.v[0]), n); // set y to [n] for (c3_d i = 0; i < len_x; i++) { ((float128_t*)y_bytes)[i] = (float128_t){n128.v[0], n128.v[1]}; @@ -573,7 +573,7 @@ // Switch on the block size. We assume that n fits in the target block size; Hoon typecheck should prevent. switch (bloq) { case 4: - u3r_bytes(0, 2, (c3_y*)&n16, n); + u3r_bytes(0, 2, (c3_y*)&(n16.v), n); // set y to [n] for (c3_d i = 0; i < len_x; i++) { ((float16_t*)y_bytes)[i] = n16; @@ -582,7 +582,7 @@ break; case 5: - u3r_bytes(0, 4, (c3_y*)&n32, n); + u3r_bytes(0, 4, (c3_y*)&(n32.v), n); // set y to [n] for (c3_d i = 0; i < len_x; i++) { ((float32_t*)y_bytes)[i] = n32; @@ -591,7 +591,7 @@ break; case 6: - u3r_bytes(0, 8, (c3_y*)&n64, n); + u3r_bytes(0, 8, (c3_y*)&(n64.v), n); // set y to [n] for (c3_d i = 0; i < len_x; i++) { ((float64_t*)y_bytes)[i] = n64; @@ -600,7 +600,7 @@ break; case 7: - u3r_bytes(0, 16, (c3_y*)&n128, n); + u3r_bytes(0, 16, (c3_y*)&(n128.v[0]), n); // set y to [n] for (c3_d i = 0; i < len_x; i++) { ((float128_t*)y_bytes)[i] = (float128_t){n128.v[0], n128.v[1]}; @@ -654,17 +654,17 @@ // Switch on the block size. switch (bloq) { case 4: - u3r_bytes(0, 2, (c3_y*)&n16, n); + u3r_bytes(0, 2, (c3_y*)&(n16.v), n); hscal(len_x, n16, (float16_t*)x_bytes, 1); break; case 5: - u3r_bytes(0, 4, (c3_y*)&n32, n); + u3r_bytes(0, 4, (c3_y*)&(n32.v), n); sscal(len_x, n32, (float32_t*)x_bytes, 1); break; case 6: - u3r_bytes(0, 8, (c3_y*)&n64, n); + u3r_bytes(0, 8, (c3_y*)&(n64.v), n); dscal(len_x, n64, (float64_t*)x_bytes, 1); break; @@ -771,19 +771,14 @@ // Unpack the data as a byte array. We assume total length < 2**64. // len_x is length in base units c3_d len_x = _get_length(shape); - fprintf(stderr, "len_x: %ld\r\n", len_x); // siz_x is length in bytes c3_d siz_x = len_x * pow(2, bloq-3); - fprintf(stderr, "siz_x: %ld\r\n", siz_x); // x_bytes is the data array (w/ leading 0x1, skipped by ?axpy) // we reuse it for results for parsimony c3_y* x_bytes = (c3_y*)u3a_malloc((siz_x+1)*sizeof(c3_y)); u3r_bytes(0, siz_x+1, x_bytes, x_data); - for (c3_d i = 0; i < siz_x+1; i++) { - fprintf(stderr, "x_bytes[%ld]: %x\r\n", i, x_bytes[i]); - } float16_t n16, in16; float32_t n32, in32; @@ -793,7 +788,7 @@ // Switch on the block size. switch (bloq) { case 4: - u3r_bytes(0, 2, (c3_y*)&n16, n); + u3r_bytes(0, 2, (c3_y*)&(n16.v), n); in16 = f16_div((float16_t){SB_REAL16_ONE}, n16); for (c3_d i = 0; i < len_x; i++) { @@ -811,34 +806,25 @@ break; case 5: - u3l_log("n: %x", n); u3r_bytes(0, 4, (c3_y*)&(n32.v), n); - fprintf(stderr, "n32: %f\r\n", n32.v); in32 = f32_div((float32_t){SB_REAL32_ONE}, n32); - fprintf(stderr, "in32: %f\r\n", in32); for (c3_d i = 0; i < len_x; i++) { float32_t x_val32 = ((float32_t*)x_bytes)[i]; - fprintf(stderr, "x_val32: %f\r\n", (float32_t)x_val32); // Perform division x/n float32_t div_result32 = f32_mul((float32_t)in32, (float32_t)x_val32); - fprintf(stderr, "div_result32: %f\r\n", div_result32); // Compute floor of the division result int64_t floor_result32 = f32_to_i64(div_result32, softfloat_round_minMag, false); - fprintf(stderr, "floor_result32: %ld\r\n", floor_result32); float32_t floor_float32 = i64_to_f32(floor_result32); - fprintf(stderr, "floor_float32: %f\r\n", floor_float32); // Multiply n by floor(x/n) float32_t mult_result32 = f32_mul(n32, floor_float32); - fprintf(stderr, "mult_result32: %f\r\n", mult_result32); // Compute remainder: x - n * floor(x/n) ((float32_t*)x_bytes)[i] = f32_sub(x_val32, mult_result32); - fprintf(stderr, "x_bytes[i]: %f\r\n\r\n", ((float32_t*)x_bytes)[i]); } break; case 6: - u3r_bytes(0, 8, (c3_y*)&n64, n); + u3r_bytes(0, 8, (c3_y*)&(n64.v), n); in64 = f64_div((float64_t){SB_REAL64_ONE}, n64); for (c3_d i = 0; i < len_x; i++) { @@ -856,7 +842,7 @@ break; case 7: - u3r_bytes(0, 16, (c3_y*)&n128, n); + u3r_bytes(0, 16, (c3_y*)&(n128.v[0]), n); f128M_div(&((float128_t){SB_REAL128L_ONE,SB_REAL128U_ZERO}), &n128, &in128); for (c3_d i = 0; i < len_x; i++) { @@ -913,28 +899,39 @@ c3_y* y_bytes = (c3_y*)u3a_malloc((siz_x+1)*sizeof(c3_y)); u3r_bytes(0, siz_x+1, y_bytes, y_data); + u3_noun r_data; + // Switch on the block size. switch (bloq) { - case 4: - hdot(len_x, (float16_t*)x_bytes, 1, (float16_t*)y_bytes, 1); + case 4: ; + float16_t r16[2]; + r16[0] = hdot(len_x, (float16_t*)x_bytes, 1, (float16_t*)y_bytes, 1); + r16[1].v = 0x1; + r_data = u3i_bytes((2+1)*sizeof(c3_y), (c3_y*)r16); break; - case 5: - sdot(len_x, (float32_t*)x_bytes, 1, (float32_t*)y_bytes, 1); + case 5: ; + float32_t r32[2]; + r32[0] = sdot(len_x, (float32_t*)x_bytes, 1, (float32_t*)y_bytes, 1); + r32[1].v = 0x1; + r_data = u3i_bytes((4+1)*sizeof(c3_y), (c3_y*)r32); break; - case 6: - ddot(len_x, (float64_t*)x_bytes, 1, (float64_t*)y_bytes, 1); + case 6: ; + float64_t r64[2]; + r64[0] = ddot(len_x, (float64_t*)x_bytes, 1, (float64_t*)y_bytes, 1); + r64[1].v = 0x1; + r_data = u3i_bytes((8+1)*sizeof(c3_y), (c3_y*)r64); break; - case 7: - qdot(len_x, (float128_t*)x_bytes, 1, (float128_t*)y_bytes, 1); + case 7: ; + float128_t r128[2]; + r128[0] = qdot(len_x, (float128_t*)x_bytes, 1, (float128_t*)y_bytes, 1); + r128[1] = (float128_t){0x1, 0x0}; + r_data = u3i_bytes((16+1)*sizeof(c3_y), (c3_y*)r128); break; } - // r_data is the result noun of [data] - u3_noun r_data = u3i_bytes((siz_x+1)*sizeof(c3_y), y_bytes); - // Clean up and return. u3a_free(x_bytes); u3a_free(y_bytes); @@ -949,6 +946,10 @@ u3_noun shape, u3_noun bloq) { + // Fence on valid bloq size. + if (bloq < 4 || bloq > 7) { + return u3_none; + } // Assert length of dims is 2. assert(u3qb_lent(shape) == 2); // Unpack shape into an array of dimensions. @@ -958,18 +959,19 @@ // Unpack the data as a byte array. We assume total length < 2**64. c3_d len_x = _get_length(shape); c3_d siz_x = len_x * pow(2, bloq - 3); - c3_d stride = dims[0] * pow(2, bloq - 3); + c3_d wyd = pow(2, bloq - 3); c3_y* x_bytes = (c3_y*)u3a_malloc((siz_x+1)*sizeof(c3_y)); u3r_bytes(0, siz_x+1, x_bytes, x_data); - c3_d siz_y = stride * dims[1]; + c3_d siz_y = wyd * dims[1]; c3_y* y_bytes = (c3_y*)u3a_malloc((siz_y+1)*sizeof(c3_y)); u3_noun r_data; + // Grab the index at i*n_x+j in bytes; put it at j. for (c3_d i = 0; i < dims[1]; i++) { - for (c3_d j = 0; j < stride; j++) { - fprintf(stderr, "i*s+j = %d*%d+%d = %d // x_bytes[i]: %lx\r\n", i, stride, j, i*stride+j, x_bytes[i*stride+j + i]); - y_bytes[i*stride+j] = x_bytes[i*stride+j + i]; + // Scan across whole field width. + for (c3_y k = 0; k < wyd; k++) { + y_bytes[i*wyd+k] = x_bytes[(i*dims[0]+i)*wyd+k]; } } y_bytes[siz_y] = 1; // pin head @@ -984,6 +986,49 @@ return r_data; } +/* transpose - x' +*/ + u3_noun + u3qf_la_transpose(u3_noun x_data, + u3_noun shape, + u3_noun bloq) + { + // Assert length of dims is 2. + assert(u3qb_lent(shape) == 2); + // Unpack shape into an array of dimensions. + c3_d *dims = _get_dims(shape); + + // Unpack the data as a byte array. We assume total length < 2**64. + c3_d len_x = _get_length(shape); + c3_d siz_x = len_x * pow(2, bloq - 3); + c3_d wyd = pow(2, bloq - 3); + c3_y* x_bytes = (c3_y*)u3a_malloc((siz_x+1)*sizeof(c3_y)); + u3r_bytes(0, siz_x+1, x_bytes, x_data); + c3_y* y_bytes = (c3_y*)u3a_malloc((siz_x+1)*sizeof(c3_y)); + + u3_noun r_data; + + // Grab the index at i*n_x+j in bytes; put it at j. + for (c3_d i = 0; i < dims[1]; i++) { + for (c3_d j = 0; j < dims[0]; j++) { + // Scan across whole field width. + for (c3_y k = 0; k < wyd; k++) { + y_bytes[(j*dims[1]+i)*wyd+k] = x_bytes[(i*dims[0]+j)*wyd+k]; + } + } + } + y_bytes[siz_x] = 1; // pin head + + // Unpack the result back into a noun. + r_data = u3i_bytes((siz_x+1)*sizeof(c3_y), y_bytes); + + u3a_free(x_bytes); + u3a_free(y_bytes); + u3a_free(dims); + + return r_data; + } + /* trace - tr(x) */ u3_noun @@ -991,9 +1036,10 @@ u3_noun shape, u3_noun bloq) { - u3_noun diag_data = u3qf_la_diag(x_data, shape, bloq); + u3_noun d_data = u3qf_la_diag(x_data, shape, bloq); c3_d len_x0 = _get_dims(shape)[0]; - return u3qf_la_dot_real(diag_data, diag_data, u3nt(len_x0, 0x1, u3_nul), bloq); + u3_noun r_data = u3qf_la_dot_real(d_data, d_data, u3nt(len_x0, 0x1, u3_nul), bloq); + return r_data; } /* mmul @@ -1583,7 +1629,8 @@ case c3__real: _set_rounding(rnd); u3_noun r_data = u3qf_la_dot_real(x_data, y_data, x_shape, x_bloq); - return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); + c3_d len_x0 = _get_dims(x_shape)[0]; + return u3nc(u3nq(u3nt(len_x0, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: return u3_none; @@ -1606,26 +1653,51 @@ { return u3m_bail(c3__exit); } else { - u3_noun x_shape, x_bloq, x_kind, x_fxp, - rnd; - if ( c3n == u3r_mean(x_meta, - 2, &x_shape, - 6, &x_bloq, - 14, &x_kind, - 15, &x_fxp, - 0) - // c3n == u3r_sing(x_shape, y_shape) || - // c3n == u3r_sing(x_bloq, y_bloq) || - // c3n == u3r_sing(x_kind, y_kind) || - // c3n == u3r_sing(x_fxp, y_fxp) || - // c3n == u3r_mean(cor, u3x_con_sam, &rnd, 0) + u3_noun x_shape, x_bloq, x_kind, x_fxp; + x_shape = u3h(x_meta); // 2 + x_bloq = u3h(u3t(x_meta)); // 6 + x_kind = u3h(u3t(u3t(x_meta))); // 14 + x_fxp = u3t(u3t(u3t(x_meta))); // 15 + if ( c3n == u3ud(x_bloq) || + c3n == u3ud(x_kind) ) { return u3m_bail(c3__exit); } else { u3_noun r_data = u3qf_la_diag(x_data, x_shape, x_bloq); c3_d len_x0 = _get_dims(x_shape)[0]; - return u3nc(u3nq(u3nt(len_x0, 0x1, u3_nul), x_bloq, x_kind, x_fxp), r_data); + return u3nc(u3nq(u3nt(len_x0, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); + } + } + } + + u3_noun + u3wf_la_transpose(u3_noun cor) + { + // Each argument is a ray, [=meta data=@ux] + u3_noun x_meta, x_data; + + if ( c3n == u3r_mean(cor, + u3x_sam_2, &x_meta, + u3x_sam_3, &x_data, + 0) || + c3n == u3ud(x_data) ) + { + return u3m_bail(c3__exit); + } else { + u3_noun x_shape, x_bloq, x_kind, x_fxp; + x_shape = u3h(x_meta); // 2 + x_bloq = u3h(u3t(x_meta)); // 6 + x_kind = u3h(u3t(u3t(x_meta))); // 14 + x_fxp = u3t(u3t(u3t(x_meta))); // 15 + if ( c3n == u3ud(x_bloq) || + c3n == u3ud(x_kind) + ) + { + return u3m_bail(c3__exit); + } else { + u3_noun r_data = u3qf_la_transpose(x_data, x_shape, x_bloq); + return u3nc(u3nq(u3nt(u3k(u3h(x_shape)), u3k(u3h(u3t(x_shape))), u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); } } } @@ -1644,29 +1716,21 @@ { return u3m_bail(c3__exit); } else { - u3_noun x_shape, x_bloq, x_kind, x_fxp, - rnd; + u3_noun x_shape, x_bloq, x_kind, x_fxp; if ( c3n == u3r_mean(x_meta, 2, &x_shape, 6, &x_bloq, 14, &x_kind, 15, &x_fxp, 0) - // c3n == u3r_sing(x_shape, y_shape) || - // c3n == u3r_sing(x_bloq, y_bloq) || - // c3n == u3r_sing(x_kind, y_kind) || - // c3n == u3r_sing(x_fxp, y_fxp) || - // c3n == u3r_mean(cor, u3x_con_sam, &rnd, 0) ) { return u3m_bail(c3__exit); } else { switch (x_kind) { - case c3__real: - _set_rounding(rnd); + case c3__real: ; u3_noun r_data = u3qf_la_trace_real(x_data, x_shape, x_bloq); - return u3nc(u3nq(u3nt(0x1, 0x1, u3_nul), x_bloq, x_kind, x_fxp), r_data); - break; + return u3nc(u3nq(u3nt(0x1, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: return u3_none; diff --git a/pkg/noun/jets/q.h b/pkg/noun/jets/q.h index 0b504b185e..37f4ccc127 100644 --- a/pkg/noun/jets/q.h +++ b/pkg/noun/jets/q.h @@ -259,6 +259,7 @@ u3_noun u3qf_la_mods_real(u3_noun, u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_dot_real(u3_noun, u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_diag(u3_noun, u3_noun, u3_noun); + u3_noun u3qf_la_transpose(u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_trace_real(u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_mmul_real(u3_noun, u3_noun, u3_noun, u3_noun, u3_noun); diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index c6c148d83f..db84df3780 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -2162,6 +2162,7 @@ static u3j_harm _139_hex__lagoon_muls_a[] = {{".2", u3wf_la_muls}, {}}; static u3j_harm _139_hex__lagoon_divs_a[] = {{".2", u3wf_la_divs}, {}}; static u3j_harm _139_hex__lagoon_mods_a[] = {{".2", u3wf_la_mods}, {}}; static u3j_harm _139_hex__lagoon_dot_a[] = {{".2", u3wf_la_dot}, {}}; +static u3j_harm _139_hex__lagoon_trans_a[] ={{".2", u3wf_la_transpose}, {}}; static u3j_harm _139_hex__lagoon_diag_a[] = {{".2", u3wf_la_diag}, {}}; static u3j_harm _139_hex__lagoon_trace_a[]= {{".2", u3wf_la_trace}, {}}; static u3j_harm _139_hex__lagoon_mmul_a[] = {{".2", u3wf_la_mmul}, {}}; @@ -2177,6 +2178,7 @@ static u3j_core _139_hex__la_core_d[] = { "div-scal", 7, _139_hex__lagoon_divs_a, 0, no_hashes }, { "mod-scal", 7, _139_hex__lagoon_mods_a, 0, no_hashes }, { "dot", 7, _139_hex__lagoon_dot_a, 0, no_hashes }, + { "transpose",7, _139_hex__lagoon_trans_a, 0, no_hashes }, { "diag", 7, _139_hex__lagoon_diag_a, 0, no_hashes }, { "trace", 7, _139_hex__lagoon_trace_a,0, no_hashes }, { "mmul", 7, _139_hex__lagoon_mmul_a, 0, no_hashes }, diff --git a/pkg/noun/jets/w.h b/pkg/noun/jets/w.h index f36e490e2c..36d66c21ba 100644 --- a/pkg/noun/jets/w.h +++ b/pkg/noun/jets/w.h @@ -343,6 +343,7 @@ u3_noun u3wf_la_mods(u3_noun); u3_noun u3wf_la_dot(u3_noun); u3_noun u3wf_la_diag(u3_noun); + u3_noun u3wf_la_transpose(u3_noun); u3_noun u3wf_la_trace(u3_noun); u3_noun u3wf_la_mmul(u3_noun); From 7eab0d7830494e5171200b0c31558da49a54f00e Mon Sep 17 00:00:00 2001 From: Sigilante Date: Tue, 16 Apr 2024 15:52:41 -0500 Subject: [PATCH 097/430] Add +abs etc. --- pkg/noun/jets/f/lagoon.c | 972 ++++++++++++++++++++++++++++++++++----- pkg/noun/jets/q.h | 9 + pkg/noun/jets/tree.c | 30 ++ pkg/noun/jets/w.h | 9 + 4 files changed, 899 insertions(+), 121 deletions(-) diff --git a/pkg/noun/jets/f/lagoon.c b/pkg/noun/jets/f/lagoon.c index 1318b532a2..7ffc7fd7ad 100644 --- a/pkg/noun/jets/f/lagoon.c +++ b/pkg/noun/jets/f/lagoon.c @@ -117,16 +117,16 @@ // len_x is length in base units c3_d len_x = _get_length(shape); - // siz_x is length in bytes - c3_d siz_x = len_x * pow(2, bloq-3); + // syz_x is length in bytes + c3_d syz_x = len_x * pow(2, bloq-3); // x_bytes is the data array (w/o leading 0x1) - c3_y* x_bytes = (c3_y*)u3a_malloc(siz_x*sizeof(c3_y)); - u3r_bytes(0, siz_x, x_bytes, x_data); + c3_y* x_bytes = (c3_y*)u3a_malloc(syz_x*sizeof(c3_y)); + u3r_bytes(0, syz_x, x_bytes, x_data); // y_bytes is the data array (w/ leading 0x1, skipped by ?axpy) - c3_y* y_bytes = (c3_y*)u3a_malloc((siz_x+1)*sizeof(c3_y)); - u3r_bytes(0, siz_x+1, y_bytes, y_data); + c3_y* y_bytes = (c3_y*)u3a_malloc((syz_x+1)*sizeof(c3_y)); + u3r_bytes(0, syz_x+1, y_bytes, y_data); // Switch on the block size. switch (bloq) { @@ -148,7 +148,7 @@ } // r_data is the result noun of [data] - u3_noun r_data = u3i_bytes((siz_x+1)*sizeof(c3_y), y_bytes); + u3_noun r_data = u3i_bytes((syz_x+1)*sizeof(c3_y), y_bytes); // Clean up and return. u3a_free(x_bytes); @@ -175,16 +175,16 @@ // len_x is length in base units c3_d len_x = _get_length(shape); - // siz_x is length in bytes - c3_d siz_x = len_x * pow(2, bloq-3); + // syz_x is length in bytes + c3_d syz_x = len_x * pow(2, bloq-3); // x_bytes is the data array (w/o leading 0x1) - c3_y* x_bytes = (c3_y*)u3a_malloc(siz_x*sizeof(c3_y)); - u3r_bytes(0, siz_x, x_bytes, x_data); + c3_y* x_bytes = (c3_y*)u3a_malloc(syz_x*sizeof(c3_y)); + u3r_bytes(0, syz_x, x_bytes, x_data); // y_bytes is the data array (w/ leading 0x1, skipped by ?axpy) - c3_y* y_bytes = (c3_y*)u3a_malloc((siz_x+1)*sizeof(c3_y)); - u3r_bytes(0, siz_x+1, y_bytes, y_data); + c3_y* y_bytes = (c3_y*)u3a_malloc((syz_x+1)*sizeof(c3_y)); + u3r_bytes(0, syz_x+1, y_bytes, y_data); // Switch on the block size. switch (bloq) { @@ -206,7 +206,7 @@ } // r_data is the result noun of [data] - u3_noun r_data = u3i_bytes((siz_x+1)*sizeof(c3_y), y_bytes); + u3_noun r_data = u3i_bytes((syz_x+1)*sizeof(c3_y), y_bytes); // Clean up and return. u3a_free(x_bytes); @@ -234,16 +234,16 @@ // len_x is length in base units c3_d len_x = _get_length(shape); - // siz_x is length in bytes - c3_d siz_x = len_x * pow(2, bloq-3); + // syz_x is length in bytes + c3_d syz_x = len_x * pow(2, bloq-3); // x_bytes is the data array (w/o leading 0x1) - c3_y* x_bytes = (c3_y*)u3a_malloc(siz_x*sizeof(c3_y)); - u3r_bytes(0, siz_x, x_bytes, x_data); + c3_y* x_bytes = (c3_y*)u3a_malloc(syz_x*sizeof(c3_y)); + u3r_bytes(0, syz_x, x_bytes, x_data); // y_bytes is the data array (w/ leading 0x1, skipped by ?axpy) - c3_y* y_bytes = (c3_y*)u3a_malloc((siz_x+1)*sizeof(c3_y)); - u3r_bytes(0, siz_x+1, y_bytes, y_data); + c3_y* y_bytes = (c3_y*)u3a_malloc((syz_x+1)*sizeof(c3_y)); + u3r_bytes(0, syz_x+1, y_bytes, y_data); // Switch on the block size. switch (bloq) { @@ -273,7 +273,7 @@ } // r_data is the result noun of [data] - u3_noun r_data = u3i_bytes((siz_x+1)*sizeof(c3_y), y_bytes); + u3_noun r_data = u3i_bytes((syz_x+1)*sizeof(c3_y), y_bytes); // Clean up and return. u3a_free(x_bytes); @@ -300,16 +300,16 @@ // len_x is length in base units c3_d len_x = _get_length(shape); - // siz_x is length in bytes - c3_d siz_x = len_x * pow(2, bloq-3); + // syz_x is length in bytes + c3_d syz_x = len_x * pow(2, bloq-3); // x_bytes is the data array (w/o leading 0x1) - c3_y* x_bytes = (c3_y*)u3a_malloc(siz_x*sizeof(c3_y)); - u3r_bytes(0, siz_x, x_bytes, x_data); + c3_y* x_bytes = (c3_y*)u3a_malloc(syz_x*sizeof(c3_y)); + u3r_bytes(0, syz_x, x_bytes, x_data); // y_bytes is the data array (w/ leading 0x1, skipped by ?axpy) - c3_y* y_bytes = (c3_y*)u3a_malloc((siz_x+1)*sizeof(c3_y)); - u3r_bytes(0, siz_x+1, y_bytes, y_data); + c3_y* y_bytes = (c3_y*)u3a_malloc((syz_x+1)*sizeof(c3_y)); + u3r_bytes(0, syz_x+1, y_bytes, y_data); // Switch on the block size. switch (bloq) { @@ -339,7 +339,7 @@ } // r_data is the result noun of [data] - u3_noun r_data = u3i_bytes((siz_x+1)*sizeof(c3_y), y_bytes); + u3_noun r_data = u3i_bytes((syz_x+1)*sizeof(c3_y), y_bytes); // Clean up and return. u3a_free(x_bytes); @@ -366,16 +366,16 @@ // len_x is length in base units c3_d len_x = _get_length(shape); - // siz_x is length in bytes - c3_d siz_x = len_x * pow(2, bloq-3); + // syz_x is length in bytes + c3_d syz_x = len_x * pow(2, bloq-3); // x_bytes is the data array (w/o leading 0x1) - c3_y* x_bytes = (c3_y*)u3a_malloc(siz_x*sizeof(c3_y)); - u3r_bytes(0, siz_x, x_bytes, x_data); + c3_y* x_bytes = (c3_y*)u3a_malloc(syz_x*sizeof(c3_y)); + u3r_bytes(0, syz_x, x_bytes, x_data); // y_bytes is the data array (w/ leading 0x1, skipped by ?axpy) - c3_y* y_bytes = (c3_y*)u3a_malloc((siz_x+1)*sizeof(c3_y)); - u3r_bytes(0, siz_x+1, y_bytes, y_data); + c3_y* y_bytes = (c3_y*)u3a_malloc((syz_x+1)*sizeof(c3_y)); + u3r_bytes(0, syz_x+1, y_bytes, y_data); // Switch on the block size. switch (bloq) { @@ -447,7 +447,358 @@ } // r_data is the result noun of [data] - u3_noun r_data = u3i_bytes((siz_x+1)*sizeof(c3_y), y_bytes); + u3_noun r_data = u3i_bytes((syz_x+1)*sizeof(c3_y), y_bytes); + + // Clean up and return. + u3a_free(x_bytes); + u3a_free(y_bytes); + + return r_data; + } + +/* abs - |x| +*/ + u3_noun + u3qf_la_abs_real(u3_noun x_data, + u3_noun shape, + u3_noun bloq) + { + // Fence on valid bloq size. + if (bloq < 4 || bloq > 7) { + return u3_none; + } + + // Unpack the data as a byte array. We assume total length < 2**64. + // len_x is length in base units + c3_d len_x = _get_length(shape); + + // syz_x is length in bytes + c3_d syz_x = len_x * pow(2, bloq-3); + + // x_bytes is the data array (w/ leading 0x1, skipped by for range) + c3_y* x_bytes = (c3_y*)u3a_malloc((syz_x+1)*sizeof(c3_y)); + u3r_bytes(0, syz_x+1, x_bytes, x_data); + + // Switch on the block size. + switch (bloq) { + case 4: + for (c3_d i = 0; i < len_x; i++) { + ((float16_t*)x_bytes)[i] = f16_abs(((float16_t*)x_bytes)[i]); + } + break; + + case 5: + for (c3_d i = 0; i < len_x; i++) { + ((float32_t*)x_bytes)[i] = f32_abs(((float32_t*)x_bytes)[i]); + } + break; + + case 6: + for (c3_d i = 0; i < len_x; i++) { + ((float64_t*)x_bytes)[i] = f64_abs(((float64_t*)x_bytes)[i]); + } + break; + + case 7: + for (c3_d i = 0; i < len_x; i++) { + ((float128_t*)x_bytes)[i] = f128_abs(((float128_t*)x_bytes)[i]); + } + break; + } + + // r_data is the result noun of [data] + u3_noun r_data = u3i_bytes((syz_x+1)*sizeof(c3_y), x_bytes); + + // Clean up and return. + u3a_free(x_bytes); + + return r_data; + } + +/* gth - x > y +*/ + u3_noun + u3qf_la_gth_real(u3_noun x_data, + u3_noun y_data, + u3_noun shape, + u3_noun bloq) + { + // Fence on valid bloq size. + if (bloq < 4 || bloq > 7) { + return u3_none; + } + + // Unpack the data as a byte array. We assume total length < 2**64. + // len_x is length in base units + c3_d len_x = _get_length(shape); + + // syz_x is length in bytes + c3_d syz_x = len_x * pow(2, bloq-3); + + // x_bytes is the data array (w/o leading 0x1) + c3_y* x_bytes = (c3_y*)u3a_malloc(syz_x*sizeof(c3_y)); + u3r_bytes(0, syz_x, x_bytes, x_data); + + // y_bytes is the data array (w/ leading 0x1, skipped by ?axpy) + c3_y* y_bytes = (c3_y*)u3a_malloc((syz_x+1)*sizeof(c3_y)); + u3r_bytes(0, syz_x+1, y_bytes, y_data); + + // Switch on the block size. + switch (bloq) { + case 4: + for (c3_d i = 0; i < len_x; i++) { + float16_t x_val16 = ((float16_t*)x_bytes)[i]; + float16_t y_val16 = ((float16_t*)y_bytes)[i]; + ((float16_t*)y_bytes)[i] = f16_gt(x_val16, y_val16) ? (float16_t){SB_REAL16_ONE} : (float16_t){SB_REAL16_ZERO}; + } + break; + + case 5: + for (c3_d i = 0; i < len_x; i++) { + float32_t x_val32 = ((float32_t*)x_bytes)[i]; + float32_t y_val32 = ((float32_t*)y_bytes)[i]; + ((float32_t*)y_bytes)[i] = f32_gt(x_val32, y_val32) ? (float32_t){SB_REAL32_ONE} : (float32_t){SB_REAL32_ZERO}; + } + break; + + case 6: + for (c3_d i = 0; i < len_x; i++) { + float64_t x_val64 = ((float64_t*)x_bytes)[i]; + float64_t y_val64 = ((float64_t*)y_bytes)[i]; + ((float64_t*)y_bytes)[i] = f64_gt(x_val64, y_val64) ? (float64_t){SB_REAL64_ONE} : (float64_t){SB_REAL64_ZERO}; + } + break; + + case 7: + for (c3_d i = 0; i < len_x; i++) { + float128_t x_val128 = ((float128_t*)x_bytes)[i]; + float128_t y_val128 = ((float128_t*)y_bytes)[i]; + ((float128_t*)y_bytes)[i] = f128M_gt(((float128_t*)&x_val128), ((float128_t*)&y_val128)) ? (float128_t){SB_REAL128L_ONE, SB_REAL128U_ONE} : (float128_t){SB_REAL128L_ZERO, SB_REAL128U_ZERO}; + } + break; + } + + // r_data is the result noun of [data] + u3_noun r_data = u3i_bytes((syz_x+1)*sizeof(c3_y), y_bytes); + + // Clean up and return. + u3a_free(x_bytes); + u3a_free(y_bytes); + + return r_data; + } + +/* gte - x > y +*/ + u3_noun + u3qf_la_gte_real(u3_noun x_data, + u3_noun y_data, + u3_noun shape, + u3_noun bloq) + { + // Fence on valid bloq size. + if (bloq < 4 || bloq > 7) { + return u3_none; + } + + // Unpack the data as a byte array. We assume total length < 2**64. + // len_x is length in base units + c3_d len_x = _get_length(shape); + + // syz_x is length in bytes + c3_d syz_x = len_x * pow(2, bloq-3); + + // x_bytes is the data array (w/o leading 0x1) + c3_y* x_bytes = (c3_y*)u3a_malloc(syz_x*sizeof(c3_y)); + u3r_bytes(0, syz_x, x_bytes, x_data); + + // y_bytes is the data array (w/ leading 0x1, skipped by ?axpy) + c3_y* y_bytes = (c3_y*)u3a_malloc((syz_x+1)*sizeof(c3_y)); + u3r_bytes(0, syz_x+1, y_bytes, y_data); + + // Switch on the block size. + switch (bloq) { + case 4: + for (c3_d i = 0; i < len_x; i++) { + float16_t x_val16 = ((float16_t*)x_bytes)[i]; + float16_t y_val16 = ((float16_t*)y_bytes)[i]; + ((float16_t*)y_bytes)[i] = f16_ge(x_val16, y_val16) ? (float16_t){SB_REAL16_ONE} : (float16_t){SB_REAL16_ZERO}; + } + break; + + case 5: + for (c3_d i = 0; i < len_x; i++) { + float32_t x_val32 = ((float32_t*)x_bytes)[i]; + float32_t y_val32 = ((float32_t*)y_bytes)[i]; + ((float32_t*)y_bytes)[i] = f32_ge(x_val32, y_val32) ? (float32_t){SB_REAL32_ONE} : (float32_t){SB_REAL32_ZERO}; + } + break; + + case 6: + for (c3_d i = 0; i < len_x; i++) { + float64_t x_val64 = ((float64_t*)x_bytes)[i]; + float64_t y_val64 = ((float64_t*)y_bytes)[i]; + ((float64_t*)y_bytes)[i] = f64_ge(x_val64, y_val64) ? (float64_t){SB_REAL64_ONE} : (float64_t){SB_REAL64_ZERO}; + } + break; + + case 7: + for (c3_d i = 0; i < len_x; i++) { + float128_t x_val128 = ((float128_t*)x_bytes)[i]; + float128_t y_val128 = ((float128_t*)y_bytes)[i]; + ((float128_t*)y_bytes)[i] = f128M_ge(((float128_t*)&x_val128), ((float128_t*)&y_val128)) ? (float128_t){SB_REAL128L_ONE, SB_REAL128U_ONE} : (float128_t){SB_REAL128L_ZERO, SB_REAL128U_ZERO}; + } + break; + } + + // r_data is the result noun of [data] + u3_noun r_data = u3i_bytes((syz_x+1)*sizeof(c3_y), y_bytes); + + // Clean up and return. + u3a_free(x_bytes); + u3a_free(y_bytes); + + return r_data; + } + +/* lth - x > y +*/ + u3_noun + u3qf_la_lth_real(u3_noun x_data, + u3_noun y_data, + u3_noun shape, + u3_noun bloq) + { + // Fence on valid bloq size. + if (bloq < 4 || bloq > 7) { + return u3_none; + } + + // Unpack the data as a byte array. We assume total length < 2**64. + // len_x is length in base units + c3_d len_x = _get_length(shape); + + // syz_x is length in bytes + c3_d syz_x = len_x * pow(2, bloq-3); + + // x_bytes is the data array (w/o leading 0x1) + c3_y* x_bytes = (c3_y*)u3a_malloc(syz_x*sizeof(c3_y)); + u3r_bytes(0, syz_x, x_bytes, x_data); + + // y_bytes is the data array (w/ leading 0x1, skipped by ?axpy) + c3_y* y_bytes = (c3_y*)u3a_malloc((syz_x+1)*sizeof(c3_y)); + u3r_bytes(0, syz_x+1, y_bytes, y_data); + + // Switch on the block size. + switch (bloq) { + case 4: + for (c3_d i = 0; i < len_x; i++) { + float16_t x_val16 = ((float16_t*)x_bytes)[i]; + float16_t y_val16 = ((float16_t*)y_bytes)[i]; + ((float16_t*)y_bytes)[i] = f16_lt(x_val16, y_val16) ? (float16_t){SB_REAL16_ONE} : (float16_t){SB_REAL16_ZERO}; + } + break; + + case 5: + for (c3_d i = 0; i < len_x; i++) { + float32_t x_val32 = ((float32_t*)x_bytes)[i]; + float32_t y_val32 = ((float32_t*)y_bytes)[i]; + ((float32_t*)y_bytes)[i] = f32_lt(x_val32, y_val32) ? (float32_t){SB_REAL32_ONE} : (float32_t){SB_REAL32_ZERO}; + } + break; + + case 6: + for (c3_d i = 0; i < len_x; i++) { + float64_t x_val64 = ((float64_t*)x_bytes)[i]; + float64_t y_val64 = ((float64_t*)y_bytes)[i]; + ((float64_t*)y_bytes)[i] = f64_lt(x_val64, y_val64) ? (float64_t){SB_REAL64_ONE} : (float64_t){SB_REAL64_ZERO}; + } + break; + + case 7: + for (c3_d i = 0; i < len_x; i++) { + float128_t x_val128 = ((float128_t*)x_bytes)[i]; + float128_t y_val128 = ((float128_t*)y_bytes)[i]; + ((float128_t*)y_bytes)[i] = f128M_lt(((float128_t*)&x_val128), ((float128_t*)&y_val128)) ? (float128_t){SB_REAL128L_ONE, SB_REAL128U_ONE} : (float128_t){SB_REAL128L_ZERO, SB_REAL128U_ZERO}; + } + break; + } + + // r_data is the result noun of [data] + u3_noun r_data = u3i_bytes((syz_x+1)*sizeof(c3_y), y_bytes); + + // Clean up and return. + u3a_free(x_bytes); + u3a_free(y_bytes); + + return r_data; + } + +/* lte - x > y +*/ + u3_noun + u3qf_la_lte_real(u3_noun x_data, + u3_noun y_data, + u3_noun shape, + u3_noun bloq) + { + // Fence on valid bloq size. + if (bloq < 4 || bloq > 7) { + return u3_none; + } + + // Unpack the data as a byte array. We assume total length < 2**64. + // len_x is length in base units + c3_d len_x = _get_length(shape); + + // syz_x is length in bytes + c3_d syz_x = len_x * pow(2, bloq-3); + + // x_bytes is the data array (w/o leading 0x1) + c3_y* x_bytes = (c3_y*)u3a_malloc(syz_x*sizeof(c3_y)); + u3r_bytes(0, syz_x, x_bytes, x_data); + + // y_bytes is the data array (w/ leading 0x1, skipped by ?axpy) + c3_y* y_bytes = (c3_y*)u3a_malloc((syz_x+1)*sizeof(c3_y)); + u3r_bytes(0, syz_x+1, y_bytes, y_data); + + // Switch on the block size. + switch (bloq) { + case 4: + for (c3_d i = 0; i < len_x; i++) { + float16_t x_val16 = ((float16_t*)x_bytes)[i]; + float16_t y_val16 = ((float16_t*)y_bytes)[i]; + ((float16_t*)y_bytes)[i] = f16_le(x_val16, y_val16) ? (float16_t){SB_REAL16_ONE} : (float16_t){SB_REAL16_ZERO}; + } + break; + + case 5: + for (c3_d i = 0; i < len_x; i++) { + float32_t x_val32 = ((float32_t*)x_bytes)[i]; + float32_t y_val32 = ((float32_t*)y_bytes)[i]; + ((float32_t*)y_bytes)[i] = f32_le(x_val32, y_val32) ? (float32_t){SB_REAL32_ONE} : (float32_t){SB_REAL32_ZERO}; + } + break; + + case 6: + for (c3_d i = 0; i < len_x; i++) { + float64_t x_val64 = ((float64_t*)x_bytes)[i]; + float64_t y_val64 = ((float64_t*)y_bytes)[i]; + ((float64_t*)y_bytes)[i] = f64_le(x_val64, y_val64) ? (float64_t){SB_REAL64_ONE} : (float64_t){SB_REAL64_ZERO}; + } + break; + + case 7: + for (c3_d i = 0; i < len_x; i++) { + float128_t x_val128 = ((float128_t*)x_bytes)[i]; + float128_t y_val128 = ((float128_t*)y_bytes)[i]; + ((float128_t*)y_bytes)[i] = f128M_le(((float128_t*)&x_val128), ((float128_t*)&y_val128)) ? (float128_t){SB_REAL128L_ONE, SB_REAL128U_ONE} : (float128_t){SB_REAL128L_ZERO, SB_REAL128U_ZERO}; + } + break; + } + + // r_data is the result noun of [data] + u3_noun r_data = u3i_bytes((syz_x+1)*sizeof(c3_y), y_bytes); // Clean up and return. u3a_free(x_bytes); @@ -473,15 +824,15 @@ // len_x is length in base units c3_d len_x = _get_length(shape); - // siz_x is length in bytes - c3_d siz_x = len_x * pow(2, bloq-3); + // syz_x is length in bytes + c3_d syz_x = len_x * pow(2, bloq-3); // x_bytes is the data array (w/o leading 0x1) - c3_y* x_bytes = (c3_y*)u3a_malloc(siz_x*sizeof(c3_y)); - u3r_bytes(0, siz_x, x_bytes, x_data); + c3_y* x_bytes = (c3_y*)u3a_malloc(syz_x*sizeof(c3_y)); + u3r_bytes(0, syz_x, x_bytes, x_data); // y_bytes is the data array (w/ leading 0x1, skipped by ?axpy) - c3_y* y_bytes = (c3_y*)u3a_malloc((siz_x+1)*sizeof(c3_y)); + c3_y* y_bytes = (c3_y*)u3a_malloc((syz_x+1)*sizeof(c3_y)); float16_t n16; float32_t n32; @@ -528,8 +879,8 @@ } // r_data is the result noun of [data] - y_bytes[siz_x] = 1; // pin head - u3_noun r_data = u3i_bytes((siz_x+1)*sizeof(c3_y), y_bytes); + y_bytes[syz_x] = 1; // pin head + u3_noun r_data = u3i_bytes((syz_x+1)*sizeof(c3_y), y_bytes); // Clean up and return. u3a_free(x_bytes); @@ -555,15 +906,15 @@ // len_x is length in base units c3_d len_x = _get_length(shape); - // siz_x is length in bytes - c3_d siz_x = len_x * pow(2, bloq-3); + // syz_x is length in bytes + c3_d syz_x = len_x * pow(2, bloq-3); // x_bytes is the data array (w/ leading 0x1, skipped by ?axpy) - c3_y* x_bytes = (c3_y*)u3a_malloc((siz_x+1)*sizeof(c3_y)); - u3r_bytes(0, siz_x, x_bytes, x_data); + c3_y* x_bytes = (c3_y*)u3a_malloc((syz_x+1)*sizeof(c3_y)); + u3r_bytes(0, syz_x, x_bytes, x_data); // y_bytes is the data array (w/o leading 0x1) - c3_y* y_bytes = (c3_y*)u3a_malloc(siz_x*sizeof(c3_y)); + c3_y* y_bytes = (c3_y*)u3a_malloc(syz_x*sizeof(c3_y)); float16_t n16; float32_t n32; @@ -610,8 +961,8 @@ } // r_data is the result noun of [data] - x_bytes[siz_x] = 1; // pin head - u3_noun r_data = u3i_bytes((siz_x+1)*sizeof(c3_y), x_bytes); + x_bytes[syz_x] = 1; // pin head + u3_noun r_data = u3i_bytes((syz_x+1)*sizeof(c3_y), x_bytes); // Clean up and return. u3a_free(x_bytes); @@ -638,13 +989,13 @@ // len_x is length in base units c3_d len_x = _get_length(shape); - // siz_x is length in bytes - c3_d siz_x = len_x * pow(2, bloq-3); + // syz_x is length in bytes + c3_d syz_x = len_x * pow(2, bloq-3); // x_bytes is the data array (w/ leading 0x1, skipped by ?axpy) - c3_y* x_bytes = (c3_y*)u3a_malloc((siz_x+1)*sizeof(c3_y)); - u3r_bytes(0, siz_x, x_bytes, x_data); - x_bytes[siz_x] = 1; // pin head + c3_y* x_bytes = (c3_y*)u3a_malloc((syz_x+1)*sizeof(c3_y)); + u3r_bytes(0, syz_x, x_bytes, x_data); + x_bytes[syz_x] = 1; // pin head float16_t n16; float32_t n32; @@ -675,7 +1026,7 @@ } // r_data is the result noun of [data] - u3_noun r_data = u3i_bytes((siz_x+1)*sizeof(c3_y), x_bytes); + u3_noun r_data = u3i_bytes((syz_x+1)*sizeof(c3_y), x_bytes); // Clean up and return. u3a_free(x_bytes); @@ -701,13 +1052,13 @@ // len_x is length in base units c3_d len_x = _get_length(shape); - // siz_x is length in bytes - c3_d siz_x = len_x * pow(2, bloq-3); + // syz_x is length in bytes + c3_d syz_x = len_x * pow(2, bloq-3); // x_bytes is the data array (w/ leading 0x1, skipped by ?axpy) - c3_y* x_bytes = (c3_y*)u3a_malloc((siz_x+1)*sizeof(c3_y)); - u3r_bytes(0, siz_x, x_bytes, x_data); - x_bytes[siz_x] = 1; // pin head + c3_y* x_bytes = (c3_y*)u3a_malloc((syz_x+1)*sizeof(c3_y)); + u3r_bytes(0, syz_x, x_bytes, x_data); + x_bytes[syz_x] = 1; // pin head float16_t in16; float32_t in32; @@ -746,7 +1097,7 @@ } // r_data is the result noun of [data] - u3_noun r_data = u3i_bytes((siz_x+1)*sizeof(c3_y), x_bytes); + u3_noun r_data = u3i_bytes((syz_x+1)*sizeof(c3_y), x_bytes); // Clean up and return. u3a_free(x_bytes); @@ -772,13 +1123,13 @@ // len_x is length in base units c3_d len_x = _get_length(shape); - // siz_x is length in bytes - c3_d siz_x = len_x * pow(2, bloq-3); + // syz_x is length in bytes + c3_d syz_x = len_x * pow(2, bloq-3); // x_bytes is the data array (w/ leading 0x1, skipped by ?axpy) // we reuse it for results for parsimony - c3_y* x_bytes = (c3_y*)u3a_malloc((siz_x+1)*sizeof(c3_y)); - u3r_bytes(0, siz_x+1, x_bytes, x_data); + c3_y* x_bytes = (c3_y*)u3a_malloc((syz_x+1)*sizeof(c3_y)); + u3r_bytes(0, syz_x+1, x_bytes, x_data); float16_t n16, in16; float32_t n32, in32; @@ -863,7 +1214,7 @@ } // r_data is the result noun of [data] - u3_noun r_data = u3i_bytes((siz_x+1)*sizeof(c3_y), x_bytes); + u3_noun r_data = u3i_bytes((syz_x+1)*sizeof(c3_y), x_bytes); // Clean up and return. u3a_free(x_bytes); @@ -888,16 +1239,16 @@ // len_x is length in base units c3_d len_x = _get_length(shape); - // siz_x is length in bytes - c3_d siz_x = len_x * pow(2, bloq-3); + // syz_x is length in bytes + c3_d syz_x = len_x * pow(2, bloq-3); // x_bytes is the data array (w/o leading 0x1) - c3_y* x_bytes = (c3_y*)u3a_malloc(siz_x*sizeof(c3_y)); - u3r_bytes(0, siz_x, x_bytes, x_data); + c3_y* x_bytes = (c3_y*)u3a_malloc(syz_x*sizeof(c3_y)); + u3r_bytes(0, syz_x, x_bytes, x_data); // y_bytes is the data array (w/ leading 0x1, skipped by ?axpy) - c3_y* y_bytes = (c3_y*)u3a_malloc((siz_x+1)*sizeof(c3_y)); - u3r_bytes(0, siz_x+1, y_bytes, y_data); + c3_y* y_bytes = (c3_y*)u3a_malloc((syz_x+1)*sizeof(c3_y)); + u3r_bytes(0, syz_x+1, y_bytes, y_data); u3_noun r_data; @@ -958,12 +1309,12 @@ // Unpack the data as a byte array. We assume total length < 2**64. c3_d len_x = _get_length(shape); - c3_d siz_x = len_x * pow(2, bloq - 3); + c3_d syz_x = len_x * pow(2, bloq - 3); c3_d wyd = pow(2, bloq - 3); - c3_y* x_bytes = (c3_y*)u3a_malloc((siz_x+1)*sizeof(c3_y)); - u3r_bytes(0, siz_x+1, x_bytes, x_data); - c3_d siz_y = wyd * dims[1]; - c3_y* y_bytes = (c3_y*)u3a_malloc((siz_y+1)*sizeof(c3_y)); + c3_y* x_bytes = (c3_y*)u3a_malloc((syz_x+1)*sizeof(c3_y)); + u3r_bytes(0, syz_x+1, x_bytes, x_data); + c3_d syz_y = wyd * dims[1]; + c3_y* y_bytes = (c3_y*)u3a_malloc((syz_y+1)*sizeof(c3_y)); u3_noun r_data; @@ -974,10 +1325,10 @@ y_bytes[i*wyd+k] = x_bytes[(i*dims[0]+i)*wyd+k]; } } - y_bytes[siz_y] = 1; // pin head + y_bytes[syz_y] = 1; // pin head // Unpack the result back into a noun. - r_data = u3i_bytes((siz_y+1)*sizeof(c3_y), y_bytes); + r_data = u3i_bytes((syz_y+1)*sizeof(c3_y), y_bytes); u3a_free(x_bytes); u3a_free(y_bytes); @@ -1000,11 +1351,11 @@ // Unpack the data as a byte array. We assume total length < 2**64. c3_d len_x = _get_length(shape); - c3_d siz_x = len_x * pow(2, bloq - 3); + c3_d syz_x = len_x * pow(2, bloq - 3); c3_d wyd = pow(2, bloq - 3); - c3_y* x_bytes = (c3_y*)u3a_malloc((siz_x+1)*sizeof(c3_y)); - u3r_bytes(0, siz_x+1, x_bytes, x_data); - c3_y* y_bytes = (c3_y*)u3a_malloc((siz_x+1)*sizeof(c3_y)); + c3_y* x_bytes = (c3_y*)u3a_malloc((syz_x+1)*sizeof(c3_y)); + u3r_bytes(0, syz_x+1, x_bytes, x_data); + c3_y* y_bytes = (c3_y*)u3a_malloc((syz_x+1)*sizeof(c3_y)); u3_noun r_data; @@ -1017,10 +1368,10 @@ } } } - y_bytes[siz_x] = 1; // pin head + y_bytes[syz_x] = 1; // pin head // Unpack the result back into a noun. - r_data = u3i_bytes((siz_x+1)*sizeof(c3_y), y_bytes); + r_data = u3i_bytes((syz_x+1)*sizeof(c3_y), y_bytes); u3a_free(x_bytes); u3a_free(y_bytes); @@ -1029,6 +1380,97 @@ return r_data; } +/* linspace - [a a+(b-a)/n ... b] +*/ + u3_noun + u3qf_la_linspace_real(u3_noun a, + u3_noun b, + u3_noun n, + u3_noun bloq) + { + // Fence on valid bloq size. + if (bloq < 4 || bloq > 7) { + return u3_none; + } + + u3_noun r_data; + + switch (bloq) { + case 4: ; + float16_t a16, b16; + u3r_bytes(0, 2, (c3_y*)&(a16.v), a); + u3r_bytes(0, 2, (c3_y*)&(b16.v), b); + float16_t span16 = f16_sub(b16, a16); + float16_t interval16 = f16_div(span16, i32_to_f16(n)); + c3_y* x_bytes16 = (c3_y*)u3a_malloc(((n+1)*2+1)*sizeof(c3_y)); + for (c3_d i = 1; i <= n; i++) { + ((float16_t*)x_bytes16)[n-i] = f16_add(a16, f16_mul(i32_to_f16(i), interval16)); + } + ((float16_t*)x_bytes16)[n] = b16; + x_bytes16[(n+1)*2] = 1; // pin head + r_data = u3i_bytes(((n+1)*2+1)*sizeof(c3_y), x_bytes16); + u3a_free(x_bytes16); + break; + + case 5: ; + float32_t a32, b32; + u3r_bytes(0, 4, (c3_y*)&(a32.v), a); + u3r_bytes(0, 4, (c3_y*)&(b32.v), b); + float32_t span32 = f32_sub(b32, a32); + float32_t interval32 = f32_div(span32, i32_to_f32(n)); + c3_y* x_bytes32 = (c3_y*)u3a_malloc(((n+1)*4+1)*sizeof(c3_y)); + for (c3_d i = 1; i <= n; i++) { + ((float32_t*)x_bytes32)[n-i] = f32_add(a32, f32_mul(i32_to_f32(i), interval32)); + } + ((float32_t*)x_bytes32)[n] = b32; + x_bytes32[(n+1)*4] = 1; // pin head + r_data = u3i_bytes(((n+1)*4+1)*sizeof(c3_y), x_bytes32); + u3a_free(x_bytes32); + break; + + case 6: ; + float64_t a64, b64; + u3r_bytes(0, 8, (c3_y*)&(a64.v), a); + u3r_bytes(0, 8, (c3_y*)&(b64.v), b); + float64_t span64 = f64_sub(b64, a64); + float64_t interval64 = f64_div(span64, i32_to_f64(n)); + c3_y* x_bytes64 = (c3_y*)u3a_malloc(((n+1)*8+1)*sizeof(c3_y)); + for (c3_d i = 1; i < n; i++) { + ((float64_t*)x_bytes64)[n-i] = f64_add(a64, f64_mul(i32_to_f64(i), interval64)); + } + ((float64_t*)x_bytes64)[n] = b64; + x_bytes64[(n+1)*8] = 1; // pin head + r_data = u3i_bytes(((n+1)*8+1)*sizeof(c3_y), x_bytes64); + u3a_free(x_bytes64); + break; + + case 7: ; + float128_t a128, b128; + u3r_bytes(0, 16, (c3_y*)&(a128.v[0]), a); + u3r_bytes(0, 16, (c3_y*)&(b128.v[0]), b); + float128_t span128; + f128M_sub(&b128, &a128, &span128); + float128_t interval128; + float128_t n128; + i32_to_f128M(n, &n128); + f128M_div(&span128, &n128, &interval128); + c3_y* x_bytes128 = (c3_y*)u3a_malloc(((n+1)*16+1)*sizeof(c3_y)); + float128_t i128; + for (c3_d i = 1; i < n; i++) { + i32_to_f128M(i, &i128); + f128M_mul(&i128, &interval128, &((float128_t*)x_bytes128)[n-i]); + f128M_add(&a128, &((float128_t*)x_bytes128)[n-i], &((float128_t*)x_bytes128)[n-i]); + } + ((float128_t*)x_bytes128)[n] = b128; + x_bytes128[(n+1)*16] = 1; // pin head + r_data = u3i_bytes(((n+1)*16+1)*sizeof(c3_y), x_bytes128); + u3a_free(x_bytes128); + break; + } + + return r_data; + } + /* trace - tr(x) */ u3_noun @@ -1124,19 +1566,129 @@ return u3nc(u3nq(u3nl(M, P, u3_none), bloq, c3__real, u3_nul), r_data); - default: - u3a_free(x_bytes); - u3a_free(y_bytes); - u3a_free(c_bytes); - - return u3_none; + default: + u3a_free(x_bytes); + u3a_free(y_bytes); + u3a_free(c_bytes); + + return u3_none; + } + } + + u3_noun + u3wf_la_add(u3_noun cor) + { + // Each argument is a ray, [=meta data=@ux] + u3_noun x_meta, x_data, + y_meta, y_data; + + if ( c3n == u3r_mean(cor, + u3x_sam_4, &x_meta, + u3x_sam_5, &x_data, + u3x_sam_6, &y_meta, + u3x_sam_7, &y_data, + 0) || + c3n == u3ud(x_data) || + c3n == u3ud(y_data) ) + { + return u3m_bail(c3__exit); + } else { + u3_noun x_shape, x_bloq, x_kind, x_fxp, + y_shape, y_bloq, y_kind, y_fxp, + rnd; + x_shape = u3h(x_meta); // 2 + x_bloq = u3h(u3t(x_meta)); // 6 + x_kind = u3h(u3t(u3t(x_meta))); // 14 + x_fxp = u3t(u3t(u3t(x_meta))); // 15 + y_shape = u3h(y_meta); // 2 + y_bloq = u3h(u3t(y_meta)); // 6 + y_kind = u3h(u3t(u3t(y_meta))); // 14 + y_fxp = u3t(u3t(u3t(y_meta))); // 15 + rnd = u3h(u3t(u3t(u3t(cor)))); // 30 + if ( c3n == u3ud(x_bloq) || + c3n == u3ud(y_bloq) || + c3n == u3ud(x_kind) || + c3n == u3ud(y_kind) || + c3n == u3r_sing(x_shape, y_shape) || + c3n == u3r_sing(x_bloq, y_bloq) || + c3n == u3r_sing(x_kind, y_kind) + // fxp does not need to match here so no check + ) + { + return u3m_bail(c3__exit); + } else { + switch (x_kind) { + case c3__real: + _set_rounding(rnd); + u3_noun r_data = u3qf_la_add_real(x_data, y_data, x_shape, x_bloq); + return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); + + default: + return u3_none; + } + } + } + } + + u3_noun + u3wf_la_sub(u3_noun cor) + { + // Each argument is a ray, [=meta data=@ux] + u3_noun x_meta, x_data, + y_meta, y_data; + + if ( c3n == u3r_mean(cor, + u3x_sam_4, &x_meta, + u3x_sam_5, &x_data, + u3x_sam_6, &y_meta, + u3x_sam_7, &y_data, + 0) || + c3n == u3ud(x_data) || + c3n == u3ud(y_data) ) + { + return u3m_bail(c3__exit); + } else { + u3_noun x_shape, x_bloq, x_kind, x_fxp, + y_shape, y_bloq, y_kind, y_fxp, + rnd; + x_shape = u3h(x_meta); // 2 + x_bloq = u3h(u3t(x_meta)); // 6 + x_kind = u3h(u3t(u3t(x_meta))); // 14 + x_fxp = u3t(u3t(u3t(x_meta))); // 15 + y_shape = u3h(y_meta); // 2 + y_bloq = u3h(u3t(y_meta)); // 6 + y_kind = u3h(u3t(u3t(y_meta))); // 14 + y_fxp = u3t(u3t(u3t(y_meta))); // 15 + rnd = u3h(u3t(u3t(u3t(cor)))); // 30 + if ( c3n == u3ud(x_bloq) || + c3n == u3ud(y_bloq) || + c3n == u3ud(x_kind) || + c3n == u3ud(y_kind) || + c3n == u3r_sing(x_shape, y_shape) || + c3n == u3r_sing(x_bloq, y_bloq) || + c3n == u3r_sing(x_kind, y_kind) + // fxp does not need to match here so no check + ) + { + return u3m_bail(c3__exit); + } else { + switch (x_kind) { + case c3__real: + _set_rounding(rnd); + u3_noun r_data = u3qf_la_sub_real(x_data, y_data, x_shape, x_bloq); + return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); + + default: + return u3_none; + } + } } } u3_noun - u3wf_la_add(u3_noun cor) + u3wf_la_mul(u3_noun cor) { - // Each argument is a ray, [=meta data=@ux] + // Each argument is a ray, [=meta data=@ux] u3_noun x_meta, x_data, y_meta, y_data; @@ -1178,7 +1730,7 @@ switch (x_kind) { case c3__real: _set_rounding(rnd); - u3_noun r_data = u3qf_la_add_real(x_data, y_data, x_shape, x_bloq); + u3_noun r_data = u3qf_la_mul_real(x_data, y_data, x_shape, x_bloq); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -1189,7 +1741,7 @@ } u3_noun - u3wf_la_sub(u3_noun cor) + u3wf_la_div(u3_noun cor) { // Each argument is a ray, [=meta data=@ux] u3_noun x_meta, x_data, @@ -1233,7 +1785,7 @@ switch (x_kind) { case c3__real: _set_rounding(rnd); - u3_noun r_data = u3qf_la_sub_real(x_data, y_data, x_shape, x_bloq); + u3_noun r_data = u3qf_la_div_real(x_data, y_data, x_shape, x_bloq); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -1244,7 +1796,7 @@ } u3_noun - u3wf_la_mul(u3_noun cor) + u3wf_la_mod(u3_noun cor) { // Each argument is a ray, [=meta data=@ux] u3_noun x_meta, x_data, @@ -1288,7 +1840,7 @@ switch (x_kind) { case c3__real: _set_rounding(rnd); - u3_noun r_data = u3qf_la_mul_real(x_data, y_data, x_shape, x_bloq); + u3_noun r_data = u3qf_la_mod_real(x_data, y_data, x_shape, x_bloq); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -1299,9 +1851,47 @@ } u3_noun - u3wf_la_div(u3_noun cor) + u3wf_la_abs(u3_noun cor) { - // Each argument is a ray, [=meta data=@ux] + // Each argument is a ray, [=meta data=@ux] + u3_noun x_meta, x_data; + + if ( c3n == u3r_mean(cor, + u3x_sam_2, &x_meta, + u3x_sam_3, &x_data, + 0) || + c3n == u3ud(x_data) ) + { + return u3m_bail(c3__exit); + } else { + u3_noun x_shape, x_bloq, x_kind, x_fxp; + x_shape = u3h(x_meta); // 2 + x_bloq = u3h(u3t(x_meta)); // 6 + x_kind = u3h(u3t(u3t(x_meta))); // 14 + x_fxp = u3t(u3t(u3t(x_meta))); // 15 + if ( c3n == u3ud(x_bloq) || + c3n == u3ud(x_kind) + ) + { + return u3m_bail(c3__exit); + } else { + switch (x_kind) { + case c3__real: ; + u3_noun r_data = u3qf_la_abs_real(x_data, x_shape, x_bloq); + fprintf(stderr, "abs: %lx\n", x_bloq); + return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); + + default: + return u3_none; + } + } + } + } + + u3_noun + u3wf_la_gth(u3_noun cor) + { + // Each argument is a ray, [=meta data=@ux] u3_noun x_meta, x_data, y_meta, y_data; @@ -1327,23 +1917,21 @@ y_bloq = u3h(u3t(y_meta)); // 6 y_kind = u3h(u3t(u3t(y_meta))); // 14 y_fxp = u3t(u3t(u3t(y_meta))); // 15 - rnd = u3h(u3t(u3t(u3t(cor)))); // 30 if ( c3n == u3ud(x_bloq) || c3n == u3ud(y_bloq) || c3n == u3ud(x_kind) || c3n == u3ud(y_kind) || c3n == u3r_sing(x_shape, y_shape) || c3n == u3r_sing(x_bloq, y_bloq) || - c3n == u3r_sing(x_kind, y_kind) - // fxp does not need to match here so no check + c3n == u3r_sing(x_kind, y_kind) || + c3n == u3r_sing(x_fxp, y_fxp) ) { return u3m_bail(c3__exit); } else { switch (x_kind) { - case c3__real: - _set_rounding(rnd); - u3_noun r_data = u3qf_la_div_real(x_data, y_data, x_shape, x_bloq); + case c3__real: ; + u3_noun r_data = u3qf_la_gth_real(x_data, y_data, x_shape, x_bloq); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -1354,9 +1942,9 @@ } u3_noun - u3wf_la_mod(u3_noun cor) + u3wf_la_gte(u3_noun cor) { - // Each argument is a ray, [=meta data=@ux] + // Each argument is a ray, [=meta data=@ux] u3_noun x_meta, x_data, y_meta, y_data; @@ -1382,23 +1970,127 @@ y_bloq = u3h(u3t(y_meta)); // 6 y_kind = u3h(u3t(u3t(y_meta))); // 14 y_fxp = u3t(u3t(u3t(y_meta))); // 15 - rnd = u3h(u3t(u3t(u3t(cor)))); // 30 if ( c3n == u3ud(x_bloq) || c3n == u3ud(y_bloq) || c3n == u3ud(x_kind) || c3n == u3ud(y_kind) || c3n == u3r_sing(x_shape, y_shape) || c3n == u3r_sing(x_bloq, y_bloq) || - c3n == u3r_sing(x_kind, y_kind) - // fxp does not need to match here so no check + c3n == u3r_sing(x_kind, y_kind) || + c3n == u3r_sing(x_fxp, y_fxp) ) { return u3m_bail(c3__exit); } else { switch (x_kind) { - case c3__real: - _set_rounding(rnd); - u3_noun r_data = u3qf_la_mod_real(x_data, y_data, x_shape, x_bloq); + case c3__real: ; + u3_noun r_data = u3qf_la_gte_real(x_data, y_data, x_shape, x_bloq); + return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); + + default: + return u3_none; + } + } + } + } + + u3_noun + u3wf_la_lth(u3_noun cor) + { + // Each argument is a ray, [=meta data=@ux] + u3_noun x_meta, x_data, + y_meta, y_data; + + if ( c3n == u3r_mean(cor, + u3x_sam_4, &x_meta, + u3x_sam_5, &x_data, + u3x_sam_6, &y_meta, + u3x_sam_7, &y_data, + 0) || + c3n == u3ud(x_data) || + c3n == u3ud(y_data) ) + { + return u3m_bail(c3__exit); + } else { + u3_noun x_shape, x_bloq, x_kind, x_fxp, + y_shape, y_bloq, y_kind, y_fxp, + rnd; + x_shape = u3h(x_meta); // 2 + x_bloq = u3h(u3t(x_meta)); // 6 + x_kind = u3h(u3t(u3t(x_meta))); // 14 + x_fxp = u3t(u3t(u3t(x_meta))); // 15 + y_shape = u3h(y_meta); // 2 + y_bloq = u3h(u3t(y_meta)); // 6 + y_kind = u3h(u3t(u3t(y_meta))); // 14 + y_fxp = u3t(u3t(u3t(y_meta))); // 15 + if ( c3n == u3ud(x_bloq) || + c3n == u3ud(y_bloq) || + c3n == u3ud(x_kind) || + c3n == u3ud(y_kind) || + c3n == u3r_sing(x_shape, y_shape) || + c3n == u3r_sing(x_bloq, y_bloq) || + c3n == u3r_sing(x_kind, y_kind) || + c3n == u3r_sing(x_fxp, y_fxp) + ) + { + return u3m_bail(c3__exit); + } else { + switch (x_kind) { + case c3__real: ; + u3_noun r_data = u3qf_la_lth_real(x_data, y_data, x_shape, x_bloq); + return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); + + default: + return u3_none; + } + } + } + } + + u3_noun + u3wf_la_lte(u3_noun cor) + { + // Each argument is a ray, [=meta data=@ux] + u3_noun x_meta, x_data, + y_meta, y_data; + + if ( c3n == u3r_mean(cor, + u3x_sam_4, &x_meta, + u3x_sam_5, &x_data, + u3x_sam_6, &y_meta, + u3x_sam_7, &y_data, + 0) || + c3n == u3ud(x_data) || + c3n == u3ud(y_data) ) + { + return u3m_bail(c3__exit); + } else { + u3_noun x_shape, x_bloq, x_kind, x_fxp, + y_shape, y_bloq, y_kind, y_fxp, + rnd; + x_shape = u3h(x_meta); // 2 + x_bloq = u3h(u3t(x_meta)); // 6 + x_kind = u3h(u3t(u3t(x_meta))); // 14 + x_fxp = u3t(u3t(u3t(x_meta))); // 15 + y_shape = u3h(y_meta); // 2 + y_bloq = u3h(u3t(y_meta)); // 6 + y_kind = u3h(u3t(u3t(y_meta))); // 14 + y_fxp = u3t(u3t(u3t(y_meta))); // 15 + if ( c3n == u3ud(x_bloq) || + c3n == u3ud(y_bloq) || + c3n == u3ud(x_kind) || + c3n == u3ud(y_kind) || + c3n == u3r_sing(x_shape, y_shape) || + c3n == u3r_sing(x_bloq, y_bloq) || + c3n == u3r_sing(x_kind, y_kind) || + c3n == u3r_sing(x_fxp, y_fxp) + ) + { + return u3m_bail(c3__exit); + } else { + switch (x_kind) { + case c3__real: ; + u3_noun r_data = u3qf_la_lte_real(x_data, y_data, x_shape, x_bloq); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -1640,7 +2332,7 @@ } u3_noun - u3wf_la_diag(u3_noun cor) + u3wf_la_transpose(u3_noun cor) { // Each argument is a ray, [=meta data=@ux] u3_noun x_meta, x_data; @@ -1664,15 +2356,52 @@ { return u3m_bail(c3__exit); } else { - u3_noun r_data = u3qf_la_diag(x_data, x_shape, x_bloq); - c3_d len_x0 = _get_dims(x_shape)[0]; - return u3nc(u3nq(u3nt(len_x0, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); + u3_noun r_data = u3qf_la_transpose(x_data, x_shape, x_bloq); + return u3nc(u3nq(u3nt(u3k(u3h(x_shape)), u3k(u3h(u3t(x_shape))), u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); } } } u3_noun - u3wf_la_transpose(u3_noun cor) + u3wf_la_linspace(u3_noun cor) + { + u3_noun x_meta, a, b, n; + + if ( c3n == u3r_mean(cor, + u3x_sam_2, &x_meta, + u3x_sam_12, &a, + u3x_sam_13, &b, + u3x_sam_7, &n, + 0)) + { + return u3m_bail(c3__exit); + } else { + u3_noun x_shape, x_bloq, x_kind, x_fxp; + x_shape = u3h(x_meta); // 2 + x_bloq = u3h(u3t(x_meta)); // 6 + x_kind = u3h(u3t(u3t(x_meta))); // 14 + x_fxp = u3t(u3t(u3t(x_meta))); // 15 + if ( c3n == u3ud(x_bloq) || + c3n == u3ud(x_kind) + ) + { + return u3m_bail(c3__exit); + } else { + switch (x_kind) { + case c3__real: ; + u3_noun r_data = u3qf_la_linspace_real(a, b, n, x_bloq); + x_shape = u3nc(u3x_atom(n)+1, u3_nul); + return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); + + default: + return u3_none; + } + } + } + } + + u3_noun + u3wf_la_diag(u3_noun cor) { // Each argument is a ray, [=meta data=@ux] u3_noun x_meta, x_data; @@ -1696,8 +2425,9 @@ { return u3m_bail(c3__exit); } else { - u3_noun r_data = u3qf_la_transpose(x_data, x_shape, x_bloq); - return u3nc(u3nq(u3nt(u3k(u3h(x_shape)), u3k(u3h(u3t(x_shape))), u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); + u3_noun r_data = u3qf_la_diag(x_data, x_shape, x_bloq); + c3_d len_x0 = _get_dims(x_shape)[0]; + return u3nc(u3nq(u3nt(len_x0, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); } } } diff --git a/pkg/noun/jets/q.h b/pkg/noun/jets/q.h index 37f4ccc127..de853bfe94 100644 --- a/pkg/noun/jets/q.h +++ b/pkg/noun/jets/q.h @@ -260,6 +260,15 @@ u3_noun u3qf_la_dot_real(u3_noun, u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_diag(u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_transpose(u3_noun, u3_noun, u3_noun); + + u3_noun u3qf_la_linspace_real(u3_noun, u3_noun, u3_noun, u3_noun); + + u3_noun u3qf_la_abs_real(u3_noun, u3_noun, u3_noun); + u3_noun u3qf_la_gth_real(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qf_la_gte_real(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qf_la_lth_real(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qf_la_lte_real(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qf_la_trace_real(u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_mmul_real(u3_noun, u3_noun, u3_noun, u3_noun, u3_noun); diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index db84df3780..6252c2fa63 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -2163,6 +2163,21 @@ static u3j_harm _139_hex__lagoon_divs_a[] = {{".2", u3wf_la_divs}, {}}; static u3j_harm _139_hex__lagoon_mods_a[] = {{".2", u3wf_la_mods}, {}}; static u3j_harm _139_hex__lagoon_dot_a[] = {{".2", u3wf_la_dot}, {}}; static u3j_harm _139_hex__lagoon_trans_a[] ={{".2", u3wf_la_transpose}, {}}; +// static u3j_harm _139_hex__lagoon_stack_a[] ={{".2", u3wf_la_stack}, {}}; +// static u3j_harm _139_hex__lagoon_cumsum_a[]={{".2", u3wf_la_cumsum}, {}}; +// static u3j_harm _139_hex__lagoon_argmin_a[]={{".2", u3wf_la_argmin}, {}}; +// static u3j_harm _139_hex__lagoon_argmax_a[]={{".2", u3wf_la_argmax}, {}}; +// static u3j_harm _139_hex__lagoon_ravel_a[]={{".2", u3wf_la_ravel}, {}}; +// static u3j_harm _139_hex__lagoon_min_a[]={{".2", u3wf_la_min}, {}}; +// static u3j_harm _139_hex__lagoon_max_a[]={{".2", u3wf_la_max}, {}}; +static u3j_harm _139_hex__lagoon_linspace_a[]={{".2", u3wf_la_linspace}, {}}; +// static u3j_harm _139_hex__lagoon_range_a[]={{".2", u3wf_la_range}, {}}; +// static u3j_harm _139_hex__lagoon_submatrix_a[]={{".2", u3wf_la_submatrix}, {}}; +static u3j_harm _139_hex__lagoon_abs_a[]={{".2", u3wf_la_abs}, {}}; +static u3j_harm _139_hex__lagoon_gth_a[]={{".2", u3wf_la_gth}, {}}; +static u3j_harm _139_hex__lagoon_gte_a[]={{".2", u3wf_la_gte}, {}}; +static u3j_harm _139_hex__lagoon_lth_a[]={{".2", u3wf_la_lth}, {}}; +static u3j_harm _139_hex__lagoon_lte_a[]={{".2", u3wf_la_lte}, {}}; static u3j_harm _139_hex__lagoon_diag_a[] = {{".2", u3wf_la_diag}, {}}; static u3j_harm _139_hex__lagoon_trace_a[]= {{".2", u3wf_la_trace}, {}}; static u3j_harm _139_hex__lagoon_mmul_a[] = {{".2", u3wf_la_mmul}, {}}; @@ -2179,6 +2194,21 @@ static u3j_core _139_hex__la_core_d[] = { "mod-scal", 7, _139_hex__lagoon_mods_a, 0, no_hashes }, { "dot", 7, _139_hex__lagoon_dot_a, 0, no_hashes }, { "transpose",7, _139_hex__lagoon_trans_a, 0, no_hashes }, + // { "stack", 7, _139_hex__lagoon_stack_a, 0, no_hashes }, + // { "cumsum", 7, _139_hex__lagoon_cumsum_a, 0, no_hashes }, + // { "argmin", 7, _139_hex__lagoon_argmin_a, 0, no_hashes }, + // { "argmax", 7, _139_hex__lagoon_argmax_a, 0, no_hashes }, + // { "ravel", 7, _139_hex__lagoon_ravel_a, 0, no_hashes }, + // { "min", 7, _139_hex__lagoon_min_a, 0, no_hashes }, + // { "max", 7, _139_hex__lagoon_max_a, 0, no_hashes }, + { "linspace", 7, _139_hex__lagoon_linspace_a, 0, no_hashes }, + // { "range", 7, _139_hex__lagoon_range_a, 0, no_hashes }, + // { "submatrix",7, _139_hex__lagoon_submatrix_a, 0, no_hashes }, + { "abs", 7, _139_hex__lagoon_abs_a, 0, no_hashes }, + { "gth", 7, _139_hex__lagoon_gth_a, 0, no_hashes }, + { "gte", 7, _139_hex__lagoon_gte_a, 0, no_hashes }, + { "lth", 7, _139_hex__lagoon_lth_a, 0, no_hashes }, + { "lte", 7, _139_hex__lagoon_lte_a, 0, no_hashes }, { "diag", 7, _139_hex__lagoon_diag_a, 0, no_hashes }, { "trace", 7, _139_hex__lagoon_trace_a,0, no_hashes }, { "mmul", 7, _139_hex__lagoon_mmul_a, 0, no_hashes }, diff --git a/pkg/noun/jets/w.h b/pkg/noun/jets/w.h index 36d66c21ba..6ab0518f67 100644 --- a/pkg/noun/jets/w.h +++ b/pkg/noun/jets/w.h @@ -344,6 +344,15 @@ u3_noun u3wf_la_dot(u3_noun); u3_noun u3wf_la_diag(u3_noun); u3_noun u3wf_la_transpose(u3_noun); + + u3_noun u3wf_la_linspace(u3_noun); + + u3_noun u3wf_la_abs(u3_noun); + u3_noun u3wf_la_gth(u3_noun); + u3_noun u3wf_la_gte(u3_noun); + u3_noun u3wf_la_lth(u3_noun); + u3_noun u3wf_la_lte(u3_noun); + u3_noun u3wf_la_trace(u3_noun); u3_noun u3wf_la_mmul(u3_noun); From 118b28103bd04b60e90a1460fe019b329fe8f589 Mon Sep 17 00:00:00 2001 From: Sigilante Date: Tue, 16 Apr 2024 20:35:17 -0500 Subject: [PATCH 098/430] Add min/max jets. --- WORKSPACE.bazel | 2 +- pkg/noun/jets/f/lagoon.c | 230 +++++++++++++++++++++++++++++++++++++++ pkg/noun/jets/q.h | 2 + pkg/noun/jets/tree.c | 8 +- pkg/noun/jets/w.h | 2 + 5 files changed, 239 insertions(+), 5 deletions(-) diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index de3564b253..11cb3c7f58 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -297,7 +297,7 @@ versioned_http_archive( strip_prefix = "SoftBLAS-{version}", # sha256 = "", url = "https://github.com/urbit/SoftBLAS/archive/{version}.tar.gz", - version = "3af44d8cbf0d61e31946af9127099257160d0451", + version = "afeccbabaf43b7d0fde6f3d2809b9c811b91641e", ) versioned_http_archive( diff --git a/pkg/noun/jets/f/lagoon.c b/pkg/noun/jets/f/lagoon.c index 7ffc7fd7ad..aec62641ba 100644 --- a/pkg/noun/jets/f/lagoon.c +++ b/pkg/noun/jets/f/lagoon.c @@ -456,6 +456,162 @@ return r_data; } +/* min - min(x,y) +*/ + u3_noun + u3qf_la_min_real(u3_noun x_data, + u3_noun shape, + u3_noun bloq) + { + // Fence on valid bloq size. + if (bloq < 4 || bloq > 7) { + return u3_none; + } + + // Unpack the data as a byte array. We assume total length < 2**64. + // len_x is length in base units + c3_d len_x = _get_length(shape); + + // syz_x is length in bytes + c3_d syz_x = len_x * pow(2, bloq-3); + + // x_bytes is the data array (w/ leading 0x1, skipped by for range) + c3_y* x_bytes = (c3_y*)u3a_malloc((syz_x+1)*sizeof(c3_y)); + u3r_bytes(0, syz_x+1, x_bytes, x_data); + + u3_noun r_data; + + // Switch on the block size. + switch (bloq) { + case 4: ; + float16_t min_val16 = ((float16_t*)x_bytes)[0]; + for (c3_d i = 0; i < len_x; i++) { + min_val16 = f16_min(min_val16, ((float16_t*)x_bytes)[i]); + } + float16_t r16[2]; + r16[0] = min_val16; + r16[1].v = 0x1; + r_data = u3i_bytes((2+1)*sizeof(c3_y), (c3_y*)r16); + break; + + case 5: ; + float32_t min_val32 = ((float32_t*)x_bytes)[0]; + for (c3_d i = 0; i < len_x; i++) { + min_val32 = f32_min(min_val32, ((float32_t*)x_bytes)[i]); + } + float32_t r32[2]; + r32[0] = min_val32; + r32[1].v = 0x1; + r_data = u3i_bytes((4+1)*sizeof(c3_y), (c3_y*)r32); + break; + + case 6: ; + float64_t min_val64 = ((float64_t*)x_bytes)[0]; + for (c3_d i = 0; i < len_x; i++) { + min_val64 = f64_min(min_val64, ((float64_t*)x_bytes)[i]); + } + float64_t r64[2]; + r64[0] = min_val64; + r64[1].v = 0x1; + r_data = u3i_bytes((8+1)*sizeof(c3_y), (c3_y*)r64); + break; + + case 7: ; + float128_t min_val128 = ((float128_t*)x_bytes)[0]; + for (c3_d i = 0; i < len_x; i++) { + min_val128 = *f128M_min(&min_val128, &((float128_t*)x_bytes)[i]); + } + float128_t r128[2]; + r128[0] = min_val128; + r128[1] = (float128_t){0x1, 0x0}; + r_data = u3i_bytes((16+1)*sizeof(c3_y), (c3_y*)r128); + break; + } + + // Clean up and return. + u3a_free(x_bytes); + + return r_data; + } + +/* max - max(x,y) +*/ + u3_noun + u3qf_la_max_real(u3_noun x_data, + u3_noun shape, + u3_noun bloq) + { + // Fence on valid bloq size. + if (bloq < 4 || bloq > 7) { + return u3_none; + } + + // Unpack the data as a byte array. We assume total length < 2**64. + // len_x is length in base units + c3_d len_x = _get_length(shape); + + // syz_x is length in bytes + c3_d syz_x = len_x * pow(2, bloq-3); + + // x_bytes is the data array (w/ leading 0x1, skipped by for range) + c3_y* x_bytes = (c3_y*)u3a_malloc((syz_x+1)*sizeof(c3_y)); + u3r_bytes(0, syz_x+1, x_bytes, x_data); + + u3_noun r_data; + + // Switch on the block size. + switch (bloq) { + case 4: ; + float16_t max_val16 = ((float16_t*)x_bytes)[0]; + for (c3_d i = 0; i < len_x; i++) { + max_val16 = f16_max(max_val16, ((float16_t*)x_bytes)[i]); + } + float16_t r16[2]; + r16[0] = max_val16; + r16[1].v = 0x1; + r_data = u3i_bytes((2+1)*sizeof(c3_y), (c3_y*)r16); + break; + + case 5: ; + float32_t max_val32 = ((float32_t*)x_bytes)[0]; + for (c3_d i = 0; i < len_x; i++) { + max_val32 = f32_max(max_val32, ((float32_t*)x_bytes)[i]); + } + float32_t r32[2]; + r32[0] = max_val32; + r32[1].v = 0x1; + r_data = u3i_bytes((4+1)*sizeof(c3_y), (c3_y*)r32); + break; + + case 6: ; + float64_t max_val64 = ((float64_t*)x_bytes)[0]; + for (c3_d i = 0; i < len_x; i++) { + max_val64 = f64_max(max_val64, ((float64_t*)x_bytes)[i]); + } + float64_t r64[2]; + r64[0] = max_val64; + r64[1].v = 0x1; + r_data = u3i_bytes((8+1)*sizeof(c3_y), (c3_y*)r64); + break; + + case 7: ; + float128_t max_val128 = ((float128_t*)x_bytes)[0]; + for (c3_d i = 0; i < len_x; i++) { + max_val128 = *f128M_max(&max_val128, &((float128_t*)x_bytes)[i]); + } + float128_t r128[2]; + r128[0] = max_val128; + r128[1] = (float128_t){0x1, 0x0}; + r_data = u3i_bytes((16+1)*sizeof(c3_y), (c3_y*)r128); + break; + } + + // Clean up and return. + u3a_free(x_bytes); + + return r_data; + } + /* abs - |x| */ u3_noun @@ -1850,6 +2006,80 @@ } } + u3_noun + u3wf_la_min(u3_noun cor) + { + // Each argument is a ray, [=meta data=@ux] + u3_noun x_meta, x_data; + + if ( c3n == u3r_mean(cor, + u3x_sam_2, &x_meta, + u3x_sam_3, &x_data, + 0) || + c3n == u3ud(x_data) ) + { + return u3m_bail(c3__exit); + } else { + u3_noun x_shape, x_bloq, x_kind, x_fxp; + x_shape = u3h(x_meta); // 2 + x_bloq = u3h(u3t(x_meta)); // 6 + x_kind = u3h(u3t(u3t(x_meta))); // 14 + x_fxp = u3t(u3t(u3t(x_meta))); // 15 + if ( c3n == u3ud(x_bloq) || + c3n == u3ud(x_kind) + ) + { + return u3m_bail(c3__exit); + } else { + switch (x_kind) { + case c3__real: ; + u3_noun r_data = u3qf_la_min_real(x_data, x_shape, x_bloq); + return u3nc(u3nq(u3nt(0x1, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); + + default: + return u3_none; + } + } + } + } + + u3_noun + u3wf_la_max(u3_noun cor) + { + // Each argument is a ray, [=meta data=@ux] + u3_noun x_meta, x_data; + + if ( c3n == u3r_mean(cor, + u3x_sam_2, &x_meta, + u3x_sam_3, &x_data, + 0) || + c3n == u3ud(x_data) ) + { + return u3m_bail(c3__exit); + } else { + u3_noun x_shape, x_bloq, x_kind, x_fxp; + x_shape = u3h(x_meta); // 2 + x_bloq = u3h(u3t(x_meta)); // 6 + x_kind = u3h(u3t(u3t(x_meta))); // 14 + x_fxp = u3t(u3t(u3t(x_meta))); // 15 + if ( c3n == u3ud(x_bloq) || + c3n == u3ud(x_kind) + ) + { + return u3m_bail(c3__exit); + } else { + switch (x_kind) { + case c3__real: ; + u3_noun r_data = u3qf_la_max_real(x_data, x_shape, x_bloq); + return u3nc(u3nq(u3nt(0x1, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); + + default: + return u3_none; + } + } + } + } + u3_noun u3wf_la_abs(u3_noun cor) { diff --git a/pkg/noun/jets/q.h b/pkg/noun/jets/q.h index de853bfe94..17fdb2a646 100644 --- a/pkg/noun/jets/q.h +++ b/pkg/noun/jets/q.h @@ -261,6 +261,8 @@ u3_noun u3qf_la_diag(u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_transpose(u3_noun, u3_noun, u3_noun); + u3_noun u3qf_la_min_real(u3_noun, u3_noun, u3_noun); + u3_noun u3qf_la_max_real(u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_linspace_real(u3_noun, u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_abs_real(u3_noun, u3_noun, u3_noun); diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index 6252c2fa63..bc482ced0a 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -2168,8 +2168,8 @@ static u3j_harm _139_hex__lagoon_trans_a[] ={{".2", u3wf_la_transpose}, {}}; // static u3j_harm _139_hex__lagoon_argmin_a[]={{".2", u3wf_la_argmin}, {}}; // static u3j_harm _139_hex__lagoon_argmax_a[]={{".2", u3wf_la_argmax}, {}}; // static u3j_harm _139_hex__lagoon_ravel_a[]={{".2", u3wf_la_ravel}, {}}; -// static u3j_harm _139_hex__lagoon_min_a[]={{".2", u3wf_la_min}, {}}; -// static u3j_harm _139_hex__lagoon_max_a[]={{".2", u3wf_la_max}, {}}; +static u3j_harm _139_hex__lagoon_min_a[]={{".2", u3wf_la_min}, {}}; +static u3j_harm _139_hex__lagoon_max_a[]={{".2", u3wf_la_max}, {}}; static u3j_harm _139_hex__lagoon_linspace_a[]={{".2", u3wf_la_linspace}, {}}; // static u3j_harm _139_hex__lagoon_range_a[]={{".2", u3wf_la_range}, {}}; // static u3j_harm _139_hex__lagoon_submatrix_a[]={{".2", u3wf_la_submatrix}, {}}; @@ -2199,8 +2199,8 @@ static u3j_core _139_hex__la_core_d[] = // { "argmin", 7, _139_hex__lagoon_argmin_a, 0, no_hashes }, // { "argmax", 7, _139_hex__lagoon_argmax_a, 0, no_hashes }, // { "ravel", 7, _139_hex__lagoon_ravel_a, 0, no_hashes }, - // { "min", 7, _139_hex__lagoon_min_a, 0, no_hashes }, - // { "max", 7, _139_hex__lagoon_max_a, 0, no_hashes }, + { "min", 7, _139_hex__lagoon_min_a, 0, no_hashes }, + { "max", 7, _139_hex__lagoon_max_a, 0, no_hashes }, { "linspace", 7, _139_hex__lagoon_linspace_a, 0, no_hashes }, // { "range", 7, _139_hex__lagoon_range_a, 0, no_hashes }, // { "submatrix",7, _139_hex__lagoon_submatrix_a, 0, no_hashes }, diff --git a/pkg/noun/jets/w.h b/pkg/noun/jets/w.h index 6ab0518f67..1e250bb79c 100644 --- a/pkg/noun/jets/w.h +++ b/pkg/noun/jets/w.h @@ -345,6 +345,8 @@ u3_noun u3wf_la_diag(u3_noun); u3_noun u3wf_la_transpose(u3_noun); + u3_noun u3wf_la_min(u3_noun); + u3_noun u3wf_la_max(u3_noun); u3_noun u3wf_la_linspace(u3_noun); u3_noun u3wf_la_abs(u3_noun); From c617a8297be21c9a0cf2f70041527cf3268a5039 Mon Sep 17 00:00:00 2001 From: Sigilante Date: Wed, 17 Apr 2024 09:58:33 -0500 Subject: [PATCH 099/430] Add min/max arg jets. --- WORKSPACE.bazel | 2 +- pkg/noun/jets/f/lagoon.c | 222 +++++++++++++++++++++++++++++++++++++++ pkg/noun/jets/q.h | 3 + pkg/noun/jets/tree.c | 8 +- pkg/noun/jets/w.h | 3 + 5 files changed, 233 insertions(+), 5 deletions(-) diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index 11cb3c7f58..c904a63eff 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -297,7 +297,7 @@ versioned_http_archive( strip_prefix = "SoftBLAS-{version}", # sha256 = "", url = "https://github.com/urbit/SoftBLAS/archive/{version}.tar.gz", - version = "afeccbabaf43b7d0fde6f3d2809b9c811b91641e", + version = "7d05697aea5363dcf5f877a9c8b464e9c352d3d4", ) versioned_http_archive( diff --git a/pkg/noun/jets/f/lagoon.c b/pkg/noun/jets/f/lagoon.c index aec62641ba..4f724d4514 100644 --- a/pkg/noun/jets/f/lagoon.c +++ b/pkg/noun/jets/f/lagoon.c @@ -456,6 +456,152 @@ return r_data; } +/* argmin - argmin(x) +*/ + u3_noun + u3qf_la_argmin_real(u3_noun x_data, + u3_noun shape, + u3_noun bloq) + { + // Fence on valid bloq size. + if (bloq < 4 || bloq > 7) { + return u3_none; + } + + // Unpack the data as a byte array. We assume total length < 2**64. + // len_x is length in base units + c3_d len_x = _get_length(shape); + + // syz_x is length in bytes + c3_d syz_x = len_x * pow(2, bloq-3); + + // x_bytes is the data array (w/o leading 0x1, which doesn't matter here) + c3_y* x_bytes = (c3_y*)u3a_malloc(syz_x*sizeof(c3_y)); + u3r_bytes(0, syz_x, x_bytes, x_data); + + c3_w min_idx = 0; + + // Switch on the block size. + switch (bloq) { + case 4: ; + float16_t min_val16 = ((float16_t*)x_bytes)[0]; + for (c3_d i = 0; i < len_x; i++) { + if(f16_lt(((float16_t*)x_bytes)[i], min_val16)) { + min_val16 = ((float16_t*)x_bytes)[i]; + min_idx = (len_x - i); + } + } + break; + + case 5: ; + float32_t min_val32 = ((float32_t*)x_bytes)[0]; + for (c3_d i = 0; i < len_x; i++) { + if(f32_lt(((float32_t*)x_bytes)[i], min_val32)) { + min_val32 = ((float32_t*)x_bytes)[i]; + min_idx = (len_x - i); + } + } + break; + + case 6: ; + float64_t min_val64 = ((float64_t*)x_bytes)[0]; + for (c3_d i = 0; i < len_x; i++) { + if(f64_lt(((float64_t*)x_bytes)[i], min_val64)) { + min_val64 = ((float64_t*)x_bytes)[i]; + min_idx = (len_x - i); + } + } + break; + + case 7: ; + float128_t min_val128 = ((float128_t*)x_bytes)[0]; + for (c3_d i = 0; i < len_x; i++) { + if(f128_lt(((float128_t*)x_bytes)[i], min_val128)) { + min_val128 = *f128M_min(&min_val128, &((float128_t*)x_bytes)[i]); + min_idx = (len_x - i); + } + } + break; + } + + u3_noun r_data = u3i_chub(min_idx); + + return r_data; + } + +/* argmax - argmax(x) +*/ + u3_noun + u3qf_la_argmax_real(u3_noun x_data, + u3_noun shape, + u3_noun bloq) + { + // Fence on valid bloq size. + if (bloq < 4 || bloq > 7) { + return u3_none; + } + + // Unpack the data as a byte array. We assume total length < 2**64. + // len_x is length in base units + c3_d len_x = _get_length(shape); + + // syz_x is length in bytes + c3_d syz_x = len_x * pow(2, bloq-3); + + // x_bytes is the data array (w/o leading 0x1, which doesn't matter here) + c3_y* x_bytes = (c3_y*)u3a_malloc(syz_x*sizeof(c3_y)); + u3r_bytes(0, syz_x, x_bytes, x_data); + + c3_w max_idx = 0; + + // Switch on the block size. + switch (bloq) { + case 4: ; + float16_t max_val16 = ((float16_t*)x_bytes)[0]; + for (c3_d i = 0; i < len_x; i++) { + if(f16_gt(((float16_t*)x_bytes)[i], max_val16)) { + max_val16 = ((float16_t*)x_bytes)[i]; + max_idx = (len_x - i); + } + } + break; + + case 5: ; + float32_t max_val32 = ((float32_t*)x_bytes)[0]; + for (c3_d i = 0; i < len_x; i++) { + if(f32_gt(((float32_t*)x_bytes)[i], max_val32)) { + max_val32 = ((float32_t*)x_bytes)[i]; + max_idx = (len_x - i); + } + } + break; + + case 6: ; + float64_t max_val64 = ((float64_t*)x_bytes)[0]; + for (c3_d i = 0; i < len_x; i++) { + if(f64_gt(((float64_t*)x_bytes)[i], max_val64)) { + max_val64 = ((float64_t*)x_bytes)[i]; + max_idx = (len_x - i); + } + } + break; + + case 7: ; + float128_t max_val128 = ((float128_t*)x_bytes)[0]; + for (c3_d i = 0; i < len_x; i++) { + if(f128_gt(((float128_t*)x_bytes)[i], max_val128)) { + max_val128 = *f128M_max(&max_val128, &((float128_t*)x_bytes)[i]); + max_idx = (len_x - i); + } + } + break; + } + + u3_noun r_data = u3i_chub(max_idx); + + return r_data; + } + /* min - min(x,y) */ u3_noun @@ -2006,6 +2152,82 @@ } } + u3_noun + u3wf_la_argmin(u3_noun cor) + { + // Each argument is a ray, [=meta data=@ux] + u3_noun x_meta, x_data; + + if ( c3n == u3r_mean(cor, + u3x_sam_2, &x_meta, + u3x_sam_3, &x_data, + 0) || + c3n == u3ud(x_data) ) + { + return u3m_bail(c3__exit); + } else { + u3_noun x_shape, x_bloq, x_kind, x_fxp; + x_shape = u3h(x_meta); // 2 + x_bloq = u3h(u3t(x_meta)); // 6 + x_kind = u3h(u3t(u3t(x_meta))); // 14 + x_fxp = u3t(u3t(u3t(x_meta))); // 15 + if ( c3n == u3ud(x_bloq) || + c3n == u3ud(x_kind) + ) + { + return u3m_bail(c3__exit); + } else { + switch (x_kind) { + case c3__real: ; + u3_noun r_data = u3qf_la_argmin_real(x_data, x_shape, x_bloq); + // bare atom (@ index) + return r_data; + + default: + return u3_none; + } + } + } + } + + u3_noun + u3wf_la_argmax(u3_noun cor) + { + // Each argument is a ray, [=meta data=@ux] + u3_noun x_meta, x_data; + + if ( c3n == u3r_mean(cor, + u3x_sam_2, &x_meta, + u3x_sam_3, &x_data, + 0) || + c3n == u3ud(x_data) ) + { + return u3m_bail(c3__exit); + } else { + u3_noun x_shape, x_bloq, x_kind, x_fxp; + x_shape = u3h(x_meta); // 2 + x_bloq = u3h(u3t(x_meta)); // 6 + x_kind = u3h(u3t(u3t(x_meta))); // 14 + x_fxp = u3t(u3t(u3t(x_meta))); // 15 + if ( c3n == u3ud(x_bloq) || + c3n == u3ud(x_kind) + ) + { + return u3m_bail(c3__exit); + } else { + switch (x_kind) { + case c3__real: ; + u3_noun r_data = u3qf_la_argmax_real(x_data, x_shape, x_bloq); + // bare atom (@ index) + return r_data; + + default: + return u3_none; + } + } + } + } + u3_noun u3wf_la_min(u3_noun cor) { diff --git a/pkg/noun/jets/q.h b/pkg/noun/jets/q.h index 17fdb2a646..cf25093573 100644 --- a/pkg/noun/jets/q.h +++ b/pkg/noun/jets/q.h @@ -261,6 +261,9 @@ u3_noun u3qf_la_diag(u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_transpose(u3_noun, u3_noun, u3_noun); + u3_noun u3qf_la_argmin_real(u3_noun, u3_noun, u3_noun); + u3_noun u3qf_la_argmax_real(u3_noun, u3_noun, u3_noun); + u3_noun u3qf_la_min_real(u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_max_real(u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_linspace_real(u3_noun, u3_noun, u3_noun, u3_noun); diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index bc482ced0a..1b876597fc 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -2165,8 +2165,8 @@ static u3j_harm _139_hex__lagoon_dot_a[] = {{".2", u3wf_la_dot}, {}}; static u3j_harm _139_hex__lagoon_trans_a[] ={{".2", u3wf_la_transpose}, {}}; // static u3j_harm _139_hex__lagoon_stack_a[] ={{".2", u3wf_la_stack}, {}}; // static u3j_harm _139_hex__lagoon_cumsum_a[]={{".2", u3wf_la_cumsum}, {}}; -// static u3j_harm _139_hex__lagoon_argmin_a[]={{".2", u3wf_la_argmin}, {}}; -// static u3j_harm _139_hex__lagoon_argmax_a[]={{".2", u3wf_la_argmax}, {}}; +static u3j_harm _139_hex__lagoon_argmin_a[]={{".2", u3wf_la_argmin}, {}}; +static u3j_harm _139_hex__lagoon_argmax_a[]={{".2", u3wf_la_argmax}, {}}; // static u3j_harm _139_hex__lagoon_ravel_a[]={{".2", u3wf_la_ravel}, {}}; static u3j_harm _139_hex__lagoon_min_a[]={{".2", u3wf_la_min}, {}}; static u3j_harm _139_hex__lagoon_max_a[]={{".2", u3wf_la_max}, {}}; @@ -2196,8 +2196,8 @@ static u3j_core _139_hex__la_core_d[] = { "transpose",7, _139_hex__lagoon_trans_a, 0, no_hashes }, // { "stack", 7, _139_hex__lagoon_stack_a, 0, no_hashes }, // { "cumsum", 7, _139_hex__lagoon_cumsum_a, 0, no_hashes }, - // { "argmin", 7, _139_hex__lagoon_argmin_a, 0, no_hashes }, - // { "argmax", 7, _139_hex__lagoon_argmax_a, 0, no_hashes }, + { "argmin", 7, _139_hex__lagoon_argmin_a, 0, no_hashes }, + { "argmax", 7, _139_hex__lagoon_argmax_a, 0, no_hashes }, // { "ravel", 7, _139_hex__lagoon_ravel_a, 0, no_hashes }, { "min", 7, _139_hex__lagoon_min_a, 0, no_hashes }, { "max", 7, _139_hex__lagoon_max_a, 0, no_hashes }, diff --git a/pkg/noun/jets/w.h b/pkg/noun/jets/w.h index 1e250bb79c..d8b887c896 100644 --- a/pkg/noun/jets/w.h +++ b/pkg/noun/jets/w.h @@ -345,6 +345,9 @@ u3_noun u3wf_la_diag(u3_noun); u3_noun u3wf_la_transpose(u3_noun); + u3_noun u3wf_la_argmin(u3_noun); + u3_noun u3wf_la_argmax(u3_noun); + u3_noun u3wf_la_min(u3_noun); u3_noun u3wf_la_max(u3_noun); u3_noun u3wf_la_linspace(u3_noun); From 44f1013fc460c3fbe426297a1a98f92e5c892ef8 Mon Sep 17 00:00:00 2001 From: Sigilante Date: Wed, 17 Apr 2024 10:17:38 -0500 Subject: [PATCH 100/430] Add cumsum jet. --- pkg/noun/jets/f/lagoon.c | 126 ++++++++++++++++++++++++++++++++++++--- pkg/noun/jets/q.h | 1 + pkg/noun/jets/tree.c | 2 +- pkg/noun/jets/w.h | 1 + 4 files changed, 121 insertions(+), 9 deletions(-) diff --git a/pkg/noun/jets/f/lagoon.c b/pkg/noun/jets/f/lagoon.c index 4f724d4514..4235daec55 100644 --- a/pkg/noun/jets/f/lagoon.c +++ b/pkg/noun/jets/f/lagoon.c @@ -456,6 +456,76 @@ return r_data; } +/* cumsum - x[0] + x[1] + ... x[n] +*/ + u3_noun + u3qf_la_cumsum_real(u3_noun x_data, + u3_noun shape, + u3_noun bloq) + { + // Fence on valid bloq size. + if (bloq < 4 || bloq > 7) { + return u3_none; + } + + // Unpack the data as a byte array. We assume total length < 2**64. + // len_x is length in base units + c3_d len_x = _get_length(shape); + + // syz_x is length in bytes + c3_d syz_x = len_x * pow(2, bloq-3); + + // y_bytes is the data array (w/ leading 0x1, skipped by for range) + c3_y* x_bytes = (c3_y*)u3a_malloc((syz_x+1)*sizeof(c3_y)); + u3r_bytes(0, syz_x+1, x_bytes, x_data); + + u3_noun r_data; + + // Switch on the block size. + switch (bloq) { + case 4: ; + float16_t sum16[2]; + for (c3_d i = 0; i < len_x; i++) { + sum16[0] = f16_add(sum16[0], ((float16_t*)x_bytes)[i]); + } + sum16[1].v = 0x1; + r_data = u3i_bytes((2+1)*sizeof(c3_y), (c3_y*)sum16); + break; + + case 5: ; + float32_t sum32[2]; + for (c3_d i = 0; i < len_x; i++) { + sum32[0] = f32_add(sum32[0], ((float32_t*)x_bytes)[i]); + } + sum32[1].v = 0x1; + r_data = u3i_bytes((4+1)*sizeof(c3_y), (c3_y*)sum32); + break; + + case 6: ; + float64_t sum64[2]; + for (c3_d i = 0; i < len_x; i++) { + sum64[0] = f64_add(sum64[0], ((float64_t*)x_bytes)[i]); + } + sum64[1].v = 0x1; + r_data = u3i_bytes((8+1)*sizeof(c3_y), (c3_y*)sum64); + break; + + case 7: ; + float128_t sum128[2]; + for (c3_d i = 0; i < len_x; i++) { + f128M_add(&(sum128[0]), &(((float128_t*)x_bytes)[i]), &(sum128[0])); + } + sum128[1] = (float128_t){0x1, 0x0}; + r_data = u3i_bytes((16+1)*sizeof(c3_y), (c3_y*)sum128); + break; + } + + // Clean up and return. + u3a_free(x_bytes); + + return r_data; + } + /* argmin - argmin(x) */ u3_noun @@ -488,7 +558,7 @@ for (c3_d i = 0; i < len_x; i++) { if(f16_lt(((float16_t*)x_bytes)[i], min_val16)) { min_val16 = ((float16_t*)x_bytes)[i]; - min_idx = (len_x - i); + min_idx = (len_x - i - 1); } } break; @@ -498,7 +568,7 @@ for (c3_d i = 0; i < len_x; i++) { if(f32_lt(((float32_t*)x_bytes)[i], min_val32)) { min_val32 = ((float32_t*)x_bytes)[i]; - min_idx = (len_x - i); + min_idx = (len_x - i - 1); } } break; @@ -508,7 +578,7 @@ for (c3_d i = 0; i < len_x; i++) { if(f64_lt(((float64_t*)x_bytes)[i], min_val64)) { min_val64 = ((float64_t*)x_bytes)[i]; - min_idx = (len_x - i); + min_idx = (len_x - i - 1); } } break; @@ -518,7 +588,7 @@ for (c3_d i = 0; i < len_x; i++) { if(f128_lt(((float128_t*)x_bytes)[i], min_val128)) { min_val128 = *f128M_min(&min_val128, &((float128_t*)x_bytes)[i]); - min_idx = (len_x - i); + min_idx = (len_x - i - 1); } } break; @@ -561,7 +631,7 @@ for (c3_d i = 0; i < len_x; i++) { if(f16_gt(((float16_t*)x_bytes)[i], max_val16)) { max_val16 = ((float16_t*)x_bytes)[i]; - max_idx = (len_x - i); + max_idx = (len_x - i - 1); } } break; @@ -571,7 +641,7 @@ for (c3_d i = 0; i < len_x; i++) { if(f32_gt(((float32_t*)x_bytes)[i], max_val32)) { max_val32 = ((float32_t*)x_bytes)[i]; - max_idx = (len_x - i); + max_idx = (len_x - i - 1); } } break; @@ -581,7 +651,7 @@ for (c3_d i = 0; i < len_x; i++) { if(f64_gt(((float64_t*)x_bytes)[i], max_val64)) { max_val64 = ((float64_t*)x_bytes)[i]; - max_idx = (len_x - i); + max_idx = (len_x - i - 1); } } break; @@ -591,7 +661,7 @@ for (c3_d i = 0; i < len_x; i++) { if(f128_gt(((float128_t*)x_bytes)[i], max_val128)) { max_val128 = *f128M_max(&max_val128, &((float128_t*)x_bytes)[i]); - max_idx = (len_x - i); + max_idx = (len_x - i - 1); } } break; @@ -2152,6 +2222,46 @@ } } + u3_noun + u3wf_la_cumsum(u3_noun cor) + { + // Each argument is a ray, [=meta data=@ux] + u3_noun x_meta, x_data; + + if ( c3n == u3r_mean(cor, + u3x_sam_2, &x_meta, + u3x_sam_3, &x_data, + 0) || + c3n == u3ud(x_data) ) + { + return u3m_bail(c3__exit); + } else { + u3_noun x_shape, x_bloq, x_kind, x_fxp, + rnd; + x_shape = u3h(x_meta); // 2 + x_bloq = u3h(u3t(x_meta)); // 6 + x_kind = u3h(u3t(u3t(x_meta))); // 14 + x_fxp = u3t(u3t(u3t(x_meta))); // 15 + rnd = u3h(u3t(u3t(u3t(cor)))); // 30 + if ( c3n == u3ud(x_bloq) || + c3n == u3ud(x_kind) + ) + { + return u3m_bail(c3__exit); + } else { + switch (x_kind) { + case c3__real: + _set_rounding(rnd); + u3_noun r_data = u3qf_la_cumsum_real(x_data, x_shape, x_bloq); + return u3nc(u3nq(u3nt(0x1, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); + + default: + return u3_none; + } + } + } + } + u3_noun u3wf_la_argmin(u3_noun cor) { diff --git a/pkg/noun/jets/q.h b/pkg/noun/jets/q.h index cf25093573..34a6b7dca7 100644 --- a/pkg/noun/jets/q.h +++ b/pkg/noun/jets/q.h @@ -261,6 +261,7 @@ u3_noun u3qf_la_diag(u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_transpose(u3_noun, u3_noun, u3_noun); + u3_noun u3qf_la_cumsum_real(u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_argmin_real(u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_argmax_real(u3_noun, u3_noun, u3_noun); diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index 1b876597fc..1738f75339 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -2164,7 +2164,7 @@ static u3j_harm _139_hex__lagoon_mods_a[] = {{".2", u3wf_la_mods}, {}}; static u3j_harm _139_hex__lagoon_dot_a[] = {{".2", u3wf_la_dot}, {}}; static u3j_harm _139_hex__lagoon_trans_a[] ={{".2", u3wf_la_transpose}, {}}; // static u3j_harm _139_hex__lagoon_stack_a[] ={{".2", u3wf_la_stack}, {}}; -// static u3j_harm _139_hex__lagoon_cumsum_a[]={{".2", u3wf_la_cumsum}, {}}; +static u3j_harm _139_hex__lagoon_cumsum_a[]={{".2", u3wf_la_cumsum}, {}}; static u3j_harm _139_hex__lagoon_argmin_a[]={{".2", u3wf_la_argmin}, {}}; static u3j_harm _139_hex__lagoon_argmax_a[]={{".2", u3wf_la_argmax}, {}}; // static u3j_harm _139_hex__lagoon_ravel_a[]={{".2", u3wf_la_ravel}, {}}; diff --git a/pkg/noun/jets/w.h b/pkg/noun/jets/w.h index d8b887c896..2b4626f324 100644 --- a/pkg/noun/jets/w.h +++ b/pkg/noun/jets/w.h @@ -345,6 +345,7 @@ u3_noun u3wf_la_diag(u3_noun); u3_noun u3wf_la_transpose(u3_noun); + u3_noun u3wf_la_cumsum(u3_noun); u3_noun u3wf_la_argmin(u3_noun); u3_noun u3wf_la_argmax(u3_noun); From c1f651d85ff5e3e24480cf210e87d1c2e6d34235 Mon Sep 17 00:00:00 2001 From: Sigilante Date: Wed, 17 Apr 2024 10:41:24 -0500 Subject: [PATCH 101/430] Add ravel jet. --- pkg/noun/jets/f/lagoon.c | 102 +++++++++++++++++++++++++++++++++++++++ pkg/noun/jets/q.h | 2 +- pkg/noun/jets/tree.c | 2 +- pkg/noun/jets/w.h | 2 +- 4 files changed, 105 insertions(+), 3 deletions(-) diff --git a/pkg/noun/jets/f/lagoon.c b/pkg/noun/jets/f/lagoon.c index 4235daec55..09962d0a8f 100644 --- a/pkg/noun/jets/f/lagoon.c +++ b/pkg/noun/jets/f/lagoon.c @@ -672,6 +672,71 @@ return r_data; } +/* ravel - x -> ~[x[0], x[1], ... x[n]] + entire nd-array busted out as a linear list +*/ + u3_noun + u3qf_la_ravel_real(u3_noun x_data, + u3_noun shape, + u3_noun bloq) + { + // Fence on valid bloq size. + if (bloq < 4 || bloq > 7) { + return u3_none; + } + + // Unpack the data as a byte array. We assume total length < 2**64. + // len_x is length in base units + c3_d len_x = _get_length(shape); + + // syz_x is length in bytes + c3_d syz_x = len_x * pow(2, bloq-3); + + // x_bytes is the data array (w/o leading 0x1) + c3_y* x_bytes = (c3_y*)u3a_malloc(syz_x*sizeof(c3_y)); + u3r_bytes(0, syz_x, x_bytes, x_data); + + // r_data is the result noun of [data] + u3_noun r_data; + + // Switch on the block size. + switch (bloq) { + case 4: + for (c3_d i = 0; i < len_x; i++) { + float16_t x_val16 = ((float16_t*)x_bytes)[i]; + r_data = u3nc(u3i_word(x_val16.v), r_data); + } + break; + + case 5: + for (c3_d i = 0; i < len_x; i++) { + float32_t x_val32 = ((float32_t*)x_bytes)[i]; + r_data = u3nc(u3i_word(x_val32.v), r_data); + } + break; + + case 6: + for (c3_d i = 0; i < len_x; i++) { + float64_t x_val64 = ((float64_t*)x_bytes)[i]; + r_data = u3nc(u3i_chub(x_val64.v), r_data); + } + break; + + case 7: + for (c3_d i = 0; i < len_x; i++) { + float128_t x_val128 = ((float128_t*)x_bytes)[i]; + r_data = u3nc(u3i_chubs(2, (c3_d*)&(x_val128.v)), r_data); + } + break; + } + + // Clean up and return. + u3a_free(x_bytes); + + // return u3qb_flop(r_data); + return r_data; + } + /* min - min(x,y) */ u3_noun @@ -2300,6 +2365,43 @@ } } + u3_noun + u3wf_la_ravel(u3_noun cor) + { + // Each argument is a ray, [=meta data=@ux] + u3_noun x_meta, x_data; + + if ( c3n == u3r_mean(cor, + u3x_sam_2, &x_meta, + u3x_sam_3, &x_data, + 0) || + c3n == u3ud(x_data) ) + { + return u3m_bail(c3__exit); + } else { + u3_noun x_shape, x_bloq, x_kind, x_fxp; + x_shape = u3h(x_meta); // 2 + x_bloq = u3h(u3t(x_meta)); // 6 + x_kind = u3h(u3t(u3t(x_meta))); // 14 + if ( c3n == u3ud(x_bloq) || + c3n == u3ud(x_kind) + ) + { + return u3m_bail(c3__exit); + } else { + switch (x_kind) { + case c3__real: ; + u3_noun r_data = u3qf_la_ravel_real(x_data, x_shape, x_bloq); + // (list @) + return r_data; + + default: + return u3_none; + } + } + } + } + u3_noun u3wf_la_argmax(u3_noun cor) { diff --git a/pkg/noun/jets/q.h b/pkg/noun/jets/q.h index 34a6b7dca7..0f38bb3a96 100644 --- a/pkg/noun/jets/q.h +++ b/pkg/noun/jets/q.h @@ -264,7 +264,7 @@ u3_noun u3qf_la_cumsum_real(u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_argmin_real(u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_argmax_real(u3_noun, u3_noun, u3_noun); - + u3_noun u3qf_la_ravel_real(u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_min_real(u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_max_real(u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_linspace_real(u3_noun, u3_noun, u3_noun, u3_noun); diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index 1738f75339..9dea54bcfc 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -2167,7 +2167,7 @@ static u3j_harm _139_hex__lagoon_trans_a[] ={{".2", u3wf_la_transpose}, {}}; static u3j_harm _139_hex__lagoon_cumsum_a[]={{".2", u3wf_la_cumsum}, {}}; static u3j_harm _139_hex__lagoon_argmin_a[]={{".2", u3wf_la_argmin}, {}}; static u3j_harm _139_hex__lagoon_argmax_a[]={{".2", u3wf_la_argmax}, {}}; -// static u3j_harm _139_hex__lagoon_ravel_a[]={{".2", u3wf_la_ravel}, {}}; +static u3j_harm _139_hex__lagoon_ravel_a[]={{".2", u3wf_la_ravel}, {}}; static u3j_harm _139_hex__lagoon_min_a[]={{".2", u3wf_la_min}, {}}; static u3j_harm _139_hex__lagoon_max_a[]={{".2", u3wf_la_max}, {}}; static u3j_harm _139_hex__lagoon_linspace_a[]={{".2", u3wf_la_linspace}, {}}; diff --git a/pkg/noun/jets/w.h b/pkg/noun/jets/w.h index 2b4626f324..666b611bc5 100644 --- a/pkg/noun/jets/w.h +++ b/pkg/noun/jets/w.h @@ -348,7 +348,7 @@ u3_noun u3wf_la_cumsum(u3_noun); u3_noun u3wf_la_argmin(u3_noun); u3_noun u3wf_la_argmax(u3_noun); - + u3_noun u3wf_la_ravel(u3_noun); u3_noun u3wf_la_min(u3_noun); u3_noun u3wf_la_max(u3_noun); u3_noun u3wf_la_linspace(u3_noun); From 692b58d19b2ae8ba4e74489d2a8df8e562c26717 Mon Sep 17 00:00:00 2001 From: Sigilante Date: Wed, 17 Apr 2024 13:41:40 -0500 Subject: [PATCH 102/430] Post "final" jet roster. --- pkg/noun/jets/f/lagoon.c | 210 +++++++++++++++++++++++++++++++++++---- pkg/noun/jets/q.h | 4 +- pkg/noun/jets/tree.c | 12 +-- pkg/noun/jets/w.h | 3 +- 4 files changed, 197 insertions(+), 32 deletions(-) diff --git a/pkg/noun/jets/f/lagoon.c b/pkg/noun/jets/f/lagoon.c index 09962d0a8f..8dc6144d2e 100644 --- a/pkg/noun/jets/f/lagoon.c +++ b/pkg/noun/jets/f/lagoon.c @@ -7,7 +7,6 @@ #include "softfloat.h" #include "softblas.h" -#include #include // for pow() #include @@ -733,7 +732,6 @@ // Clean up and return. u3a_free(x_bytes); - // return u3qb_flop(r_data); return r_data; } @@ -1739,10 +1737,14 @@ return u3_none; } // Assert length of dims is 2. - assert(u3qb_lent(shape) == 2); + if (u3qb_lent(shape) != 2) { + return u3m_bail(c3__exit); + } // Unpack shape into an array of dimensions. c3_d *dims = _get_dims(shape); - assert(dims[0] == dims[1]); + if (dims[0] != dims[1]) { + return u3m_bail(c3__exit); + } // Unpack the data as a byte array. We assume total length < 2**64. c3_d len_x = _get_length(shape); @@ -1782,7 +1784,9 @@ u3_noun bloq) { // Assert length of dims is 2. - assert(u3qb_lent(shape) == 2); + if (u3qb_lent(shape) != 2) { + return u3m_bail(c3__exit); + } // Unpack shape into an array of dimensions. c3_d *dims = _get_dims(shape); @@ -1838,12 +1842,13 @@ u3r_bytes(0, 2, (c3_y*)&(a16.v), a); u3r_bytes(0, 2, (c3_y*)&(b16.v), b); float16_t span16 = f16_sub(b16, a16); - float16_t interval16 = f16_div(span16, i32_to_f16(n)); + float16_t interval16 = f16_div(span16, i32_to_f16(n-1)); c3_y* x_bytes16 = (c3_y*)u3a_malloc(((n+1)*2+1)*sizeof(c3_y)); for (c3_d i = 1; i <= n; i++) { ((float16_t*)x_bytes16)[n-i] = f16_add(a16, f16_mul(i32_to_f16(i), interval16)); } - ((float16_t*)x_bytes16)[n] = b16; + ((float16_t*)x_bytes16)[n] = a16; + ((float16_t*)x_bytes16)[0] = b16; x_bytes16[(n+1)*2] = 1; // pin head r_data = u3i_bytes(((n+1)*2+1)*sizeof(c3_y), x_bytes16); u3a_free(x_bytes16); @@ -1854,12 +1859,13 @@ u3r_bytes(0, 4, (c3_y*)&(a32.v), a); u3r_bytes(0, 4, (c3_y*)&(b32.v), b); float32_t span32 = f32_sub(b32, a32); - float32_t interval32 = f32_div(span32, i32_to_f32(n)); + float32_t interval32 = f32_div(span32, i32_to_f32(n-1)); c3_y* x_bytes32 = (c3_y*)u3a_malloc(((n+1)*4+1)*sizeof(c3_y)); for (c3_d i = 1; i <= n; i++) { ((float32_t*)x_bytes32)[n-i] = f32_add(a32, f32_mul(i32_to_f32(i), interval32)); } - ((float32_t*)x_bytes32)[n] = b32; + ((float32_t*)x_bytes32)[n] = a32; + ((float32_t*)x_bytes32)[0] = b32; x_bytes32[(n+1)*4] = 1; // pin head r_data = u3i_bytes(((n+1)*4+1)*sizeof(c3_y), x_bytes32); u3a_free(x_bytes32); @@ -1870,12 +1876,13 @@ u3r_bytes(0, 8, (c3_y*)&(a64.v), a); u3r_bytes(0, 8, (c3_y*)&(b64.v), b); float64_t span64 = f64_sub(b64, a64); - float64_t interval64 = f64_div(span64, i32_to_f64(n)); + float64_t interval64 = f64_div(span64, i32_to_f64(n-1)); c3_y* x_bytes64 = (c3_y*)u3a_malloc(((n+1)*8+1)*sizeof(c3_y)); for (c3_d i = 1; i < n; i++) { ((float64_t*)x_bytes64)[n-i] = f64_add(a64, f64_mul(i32_to_f64(i), interval64)); } - ((float64_t*)x_bytes64)[n] = b64; + ((float64_t*)x_bytes64)[n] = a64; + ((float64_t*)x_bytes64)[0] = b64; x_bytes64[(n+1)*8] = 1; // pin head r_data = u3i_bytes(((n+1)*8+1)*sizeof(c3_y), x_bytes64); u3a_free(x_bytes64); @@ -1889,7 +1896,7 @@ f128M_sub(&b128, &a128, &span128); float128_t interval128; float128_t n128; - i32_to_f128M(n, &n128); + i32_to_f128M(n-1, &n128); f128M_div(&span128, &n128, &interval128); c3_y* x_bytes128 = (c3_y*)u3a_malloc(((n+1)*16+1)*sizeof(c3_y)); float128_t i128; @@ -1898,7 +1905,8 @@ f128M_mul(&i128, &interval128, &((float128_t*)x_bytes128)[n-i]); f128M_add(&a128, &((float128_t*)x_bytes128)[n-i], &((float128_t*)x_bytes128)[n-i]); } - ((float128_t*)x_bytes128)[n] = b128; + ((float128_t*)x_bytes128)[n] = a128; + ((float128_t*)x_bytes128)[0] = b128; x_bytes128[(n+1)*16] = 1; // pin head r_data = u3i_bytes(((n+1)*16+1)*sizeof(c3_y), x_bytes128); u3a_free(x_bytes128); @@ -1908,6 +1916,97 @@ return r_data; } +/* range - [a a+d ... b] +*/ + u3_noun + u3qf_la_range_real(u3_noun a, + u3_noun b, + u3_noun d, + u3_noun bloq) + { + // Fence on valid bloq size. + if (bloq < 4 || bloq > 7) { + return u3_none; + } + + u3_noun r_data; + + switch (bloq) { + case 4: ; + float16_t a16, b16, interval16; + u3r_bytes(0, 2, (c3_y*)&(a16.v), a); + u3r_bytes(0, 2, (c3_y*)&(b16.v), b); + u3r_bytes(0, 2, (c3_y*)&(interval16.v), d); + c3_d n16 = f16_to_i64(f16_div(f16_sub(b16, a16), interval16), softfloat_round_minMag, false); + c3_y* x_bytes16 = (c3_y*)u3a_malloc(((n16+1)*2+1)*sizeof(c3_y)); + for (c3_d i = 1; i <= n16; i++) { + ((float16_t*)x_bytes16)[n16-i] = f16_add(a16, f16_mul(i32_to_f16(i), interval16)); + } + ((float16_t*)x_bytes16)[n16] = a16; + // ((float16_t*)x_bytes16)[0] = b16; + x_bytes16[(n16+1)*2] = 1; // pin head + r_data = u3i_bytes(((n16+1)*2+1)*sizeof(c3_y), x_bytes16); + u3a_free(x_bytes16); + break; + + case 5: ; + float32_t a32, b32, interval32; + u3r_bytes(0, 4, (c3_y*)&(a32.v), a); + u3r_bytes(0, 4, (c3_y*)&(b32.v), b); + u3r_bytes(0, 4, (c3_y*)&(interval32.v), d); + c3_d n32 = f32_to_i64(f32_div(f32_sub(b32, a32), interval32), softfloat_round_minMag, false); + c3_y* x_bytes32 = (c3_y*)u3a_malloc(((n32+1)*4+1)*sizeof(c3_y)); + for (c3_d i = 1; i <= n32; i++) { + ((float32_t*)x_bytes32)[n32-i] = f32_add(a32, f32_mul(i32_to_f32(i), interval32)); + } + ((float32_t*)x_bytes32)[n32] = a32; + // ((float32_t*)x_bytes32)[0] = b32; + x_bytes32[(n32+1)*4] = 1; // pin head + r_data = u3i_bytes(((n32+1)*4+1)*sizeof(c3_y), x_bytes32); + u3a_free(x_bytes32); + break; + + case 6: ; + float64_t a64, b64, interval64; + u3r_bytes(0, 8, (c3_y*)&(a64.v), a); + u3r_bytes(0, 8, (c3_y*)&(b64.v), b); + u3r_bytes(0, 8, (c3_y*)&(interval64.v), d); + c3_d n64 = f64_to_i64(f64_div(f64_sub(b64, a64), interval64), softfloat_round_minMag, false); + c3_y* x_bytes64 = (c3_y*)u3a_malloc(((n64+1)*8+1)*sizeof(c3_y)); + for (c3_d i = 1; i < n64; i++) { + ((float64_t*)x_bytes64)[n64-i] = f64_add(a64, f64_mul(i32_to_f64(i), interval64)); + } + ((float64_t*)x_bytes64)[n64] = a64; + // ((float64_t*)x_bytes64)[0] = b64; + x_bytes64[(n64+1)*8] = 1; // pin head + r_data = u3i_bytes(((n64+1)*8+1)*sizeof(c3_y), x_bytes64); + u3a_free(x_bytes64); + break; + + case 7: ; + float128_t a128, b128, interval128; + u3r_bytes(0, 16, (c3_y*)&(a128.v[0]), a); + u3r_bytes(0, 16, (c3_y*)&(b128.v[0]), b); + u3r_bytes(0, 16, (c3_y*)&(interval128.v), d); + c3_d n128 = f128_to_i64(f128_div(f128_sub(b128, a128), interval128), softfloat_round_minMag, false); + c3_y* x_bytes128 = (c3_y*)u3a_malloc(((n128+1)*16+1)*sizeof(c3_y)); + float128_t i128; + for (c3_d i = 1; i < n128; i++) { + i32_to_f128M(i, &i128); + f128M_mul(&i128, &interval128, &((float128_t*)x_bytes128)[n128-i]); + f128M_add(&a128, &((float128_t*)x_bytes128)[n128-i], &((float128_t*)x_bytes128)[n128-i]); + } + ((float128_t*)x_bytes128)[n128] = a128; + // ((float128_t*)x_bytes128)[0] = b128; + x_bytes128[(n128+1)*16] = 1; // pin head + r_data = u3i_bytes(((n128+1)*16+1)*sizeof(c3_y), x_bytes128); + u3a_free(x_bytes128); + break; + } + + return r_data; + } + /* trace - tr(x) */ u3_noun @@ -1936,10 +2035,12 @@ c3_d Nb = u3h(y_shape); c3_d P = u3h(u3t(y_shape)); - assert(u3_nul == u3t(u3t(x_shape))); - assert(Na == Nb); + if ((u3_nul != u3t(u3t(x_shape))) || + (u3_nul != u3t(u3t(y_shape))) || + (Na != Nb)) { + return u3m_bail(c3__exit); + } c3_d N = Na; - assert(u3_nul == u3t(u3t(y_shape))); c3_y* x_bytes = (c3_y*)u3a_malloc((M*N)*sizeof(c3_y)); u3r_bytes(0, M*N, x_bytes, x_data); @@ -3029,7 +3130,7 @@ u3_noun u3wf_la_linspace(u3_noun cor) { - u3_noun x_meta, a, b, n; + u3_noun x_meta, a, b, n, rnd; if ( c3n == u3r_mean(cor, u3x_sam_2, &x_meta, @@ -3045,6 +3146,7 @@ x_bloq = u3h(u3t(x_meta)); // 6 x_kind = u3h(u3t(u3t(x_meta))); // 14 x_fxp = u3t(u3t(u3t(x_meta))); // 15 + rnd = u3h(u3t(u3t(u3t(cor)))); // 30 if ( c3n == u3ud(x_bloq) || c3n == u3ud(x_kind) ) @@ -3052,9 +3154,79 @@ return u3m_bail(c3__exit); } else { switch (x_kind) { - case c3__real: ; + case c3__real: + _set_rounding(rnd); u3_noun r_data = u3qf_la_linspace_real(a, b, n, x_bloq); - x_shape = u3nc(u3x_atom(n)+1, u3_nul); + x_shape = u3nt(u3x_atom(n), 0x1, u3_nul); + return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); + + default: + return u3_none; + } + } + } + } + + u3_noun + u3wf_la_range(u3_noun cor) + { + u3_noun x_meta, a, b, d, rnd; + + if ( c3n == u3r_mean(cor, + u3x_sam_2, &x_meta, + u3x_sam_12, &a, + u3x_sam_13, &b, + u3x_sam_7, &d, + 0)) + { + return u3m_bail(c3__exit); + } else { + u3_noun x_shape, x_bloq, x_kind, x_fxp; + x_shape = u3h(x_meta); // 2 + x_bloq = u3h(u3t(x_meta)); // 6 + x_kind = u3h(u3t(u3t(x_meta))); // 14 + x_fxp = u3t(u3t(u3t(x_meta))); // 15 + rnd = u3h(u3t(u3t(u3t(cor)))); // 30 + if ( c3n == u3ud(x_bloq) || + c3n == u3ud(x_kind) + ) + { + return u3m_bail(c3__exit); + } else { + switch (x_kind) { + case c3__real: + _set_rounding(rnd); + u3_noun r_data = u3qf_la_range_real(a, b, d, x_bloq); + c3_d a_, b_, d_; + c3_ds n_; + switch (x_bloq) { + case 4: + u3r_bytes(0, 2, (c3_y*)&a_, a); + u3r_bytes(0, 2, (c3_y*)&b_, b); + u3r_bytes(0, 2, (c3_y*)&d_, d); + n_ = f16_to_i64(f16_div(f16_sub((float16_t){b_}, (float16_t){a_}), (float16_t){d_}), softfloat_round_minMag, false); + break; + case 5: + u3r_bytes(0, 4, (c3_y*)&a_, a); + u3r_bytes(0, 4, (c3_y*)&b_, b); + u3r_bytes(0, 4, (c3_y*)&d_, d); + n_ = f32_to_i64(f32_div(f32_sub((float32_t){b_}, (float32_t){a_}), (float32_t){d_}), softfloat_round_minMag, false); + break; + case 6: + u3r_bytes(0, 8, (c3_y*)&a_, a); + u3r_bytes(0, 8, (c3_y*)&b_, b); + u3r_bytes(0, 8, (c3_y*)&d_, d); + n_ = f64_to_i64(f64_div(f64_sub((float64_t){b_}, (float64_t){a_}), (float64_t){d_}), softfloat_round_minMag, false); + break; + case 7: + u3r_bytes(0, 16, (c3_y*)&a_, a); + u3r_bytes(0, 16, (c3_y*)&b_, b); + u3r_bytes(0, 16, (c3_y*)&d_, d); + n_ = f128_to_i64(f128_div(f128_sub((float128_t){b_}, (float128_t){a_}), (float128_t){d_}), softfloat_round_minMag, false); + break; + } + u3_noun n = u3i_chub(n_+1); + x_shape = u3nt(u3k(n), 0x1, u3_nul); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: diff --git a/pkg/noun/jets/q.h b/pkg/noun/jets/q.h index 0f38bb3a96..b5e5cbc8e1 100644 --- a/pkg/noun/jets/q.h +++ b/pkg/noun/jets/q.h @@ -260,7 +260,6 @@ u3_noun u3qf_la_dot_real(u3_noun, u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_diag(u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_transpose(u3_noun, u3_noun, u3_noun); - u3_noun u3qf_la_cumsum_real(u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_argmin_real(u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_argmax_real(u3_noun, u3_noun, u3_noun); @@ -268,13 +267,12 @@ u3_noun u3qf_la_min_real(u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_max_real(u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_linspace_real(u3_noun, u3_noun, u3_noun, u3_noun); - + u3_noun u3qf_la_range_real(u3_noun, u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_abs_real(u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_gth_real(u3_noun, u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_gte_real(u3_noun, u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_lth_real(u3_noun, u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_lte_real(u3_noun, u3_noun, u3_noun, u3_noun); - u3_noun u3qf_la_trace_real(u3_noun, u3_noun, u3_noun); u3_noun u3qf_la_mmul_real(u3_noun, u3_noun, u3_noun, u3_noun, u3_noun); diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index 9dea54bcfc..dc27123a4d 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -2163,7 +2163,6 @@ static u3j_harm _139_hex__lagoon_divs_a[] = {{".2", u3wf_la_divs}, {}}; static u3j_harm _139_hex__lagoon_mods_a[] = {{".2", u3wf_la_mods}, {}}; static u3j_harm _139_hex__lagoon_dot_a[] = {{".2", u3wf_la_dot}, {}}; static u3j_harm _139_hex__lagoon_trans_a[] ={{".2", u3wf_la_transpose}, {}}; -// static u3j_harm _139_hex__lagoon_stack_a[] ={{".2", u3wf_la_stack}, {}}; static u3j_harm _139_hex__lagoon_cumsum_a[]={{".2", u3wf_la_cumsum}, {}}; static u3j_harm _139_hex__lagoon_argmin_a[]={{".2", u3wf_la_argmin}, {}}; static u3j_harm _139_hex__lagoon_argmax_a[]={{".2", u3wf_la_argmax}, {}}; @@ -2171,8 +2170,7 @@ static u3j_harm _139_hex__lagoon_ravel_a[]={{".2", u3wf_la_ravel}, {}}; static u3j_harm _139_hex__lagoon_min_a[]={{".2", u3wf_la_min}, {}}; static u3j_harm _139_hex__lagoon_max_a[]={{".2", u3wf_la_max}, {}}; static u3j_harm _139_hex__lagoon_linspace_a[]={{".2", u3wf_la_linspace}, {}}; -// static u3j_harm _139_hex__lagoon_range_a[]={{".2", u3wf_la_range}, {}}; -// static u3j_harm _139_hex__lagoon_submatrix_a[]={{".2", u3wf_la_submatrix}, {}}; +static u3j_harm _139_hex__lagoon_range_a[]={{".2", u3wf_la_range}, {}}; static u3j_harm _139_hex__lagoon_abs_a[]={{".2", u3wf_la_abs}, {}}; static u3j_harm _139_hex__lagoon_gth_a[]={{".2", u3wf_la_gth}, {}}; static u3j_harm _139_hex__lagoon_gte_a[]={{".2", u3wf_la_gte}, {}}; @@ -2194,16 +2192,14 @@ static u3j_core _139_hex__la_core_d[] = { "mod-scal", 7, _139_hex__lagoon_mods_a, 0, no_hashes }, { "dot", 7, _139_hex__lagoon_dot_a, 0, no_hashes }, { "transpose",7, _139_hex__lagoon_trans_a, 0, no_hashes }, - // { "stack", 7, _139_hex__lagoon_stack_a, 0, no_hashes }, - // { "cumsum", 7, _139_hex__lagoon_cumsum_a, 0, no_hashes }, + { "cumsum", 7, _139_hex__lagoon_cumsum_a, 0, no_hashes }, { "argmin", 7, _139_hex__lagoon_argmin_a, 0, no_hashes }, { "argmax", 7, _139_hex__lagoon_argmax_a, 0, no_hashes }, - // { "ravel", 7, _139_hex__lagoon_ravel_a, 0, no_hashes }, + { "ravel", 7, _139_hex__lagoon_ravel_a, 0, no_hashes }, { "min", 7, _139_hex__lagoon_min_a, 0, no_hashes }, { "max", 7, _139_hex__lagoon_max_a, 0, no_hashes }, { "linspace", 7, _139_hex__lagoon_linspace_a, 0, no_hashes }, - // { "range", 7, _139_hex__lagoon_range_a, 0, no_hashes }, - // { "submatrix",7, _139_hex__lagoon_submatrix_a, 0, no_hashes }, + { "range", 7, _139_hex__lagoon_range_a, 0, no_hashes }, { "abs", 7, _139_hex__lagoon_abs_a, 0, no_hashes }, { "gth", 7, _139_hex__lagoon_gth_a, 0, no_hashes }, { "gte", 7, _139_hex__lagoon_gte_a, 0, no_hashes }, diff --git a/pkg/noun/jets/w.h b/pkg/noun/jets/w.h index 666b611bc5..e7976ba3b7 100644 --- a/pkg/noun/jets/w.h +++ b/pkg/noun/jets/w.h @@ -344,7 +344,6 @@ u3_noun u3wf_la_dot(u3_noun); u3_noun u3wf_la_diag(u3_noun); u3_noun u3wf_la_transpose(u3_noun); - u3_noun u3wf_la_cumsum(u3_noun); u3_noun u3wf_la_argmin(u3_noun); u3_noun u3wf_la_argmax(u3_noun); @@ -352,7 +351,7 @@ u3_noun u3wf_la_min(u3_noun); u3_noun u3wf_la_max(u3_noun); u3_noun u3wf_la_linspace(u3_noun); - + u3_noun u3wf_la_range(u3_noun); u3_noun u3wf_la_abs(u3_noun); u3_noun u3wf_la_gth(u3_noun); u3_noun u3wf_la_gte(u3_noun); From 081ee1dad4a743247439fb86c8fa0e0853c2df31 Mon Sep 17 00:00:00 2001 From: lukechampine Date: Wed, 17 Apr 2024 17:58:17 -0400 Subject: [PATCH 103/430] jets: jet chacha --- WORKSPACE.bazel | 4 +-- pkg/noun/jets/e/chacha.c | 67 ++++++++++++++++++++++++++++++++++++++++ pkg/noun/jets/tree.c | 61 ++++++++++++++++++++---------------- pkg/noun/jets/w.h | 3 ++ 4 files changed, 107 insertions(+), 28 deletions(-) create mode 100644 pkg/noun/jets/e/chacha.c diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index 39ddfe03e9..4509e425cb 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -327,10 +327,10 @@ versioned_http_file( versioned_http_archive( name = "urcrypt", build_file = "//bazel/third_party/urcrypt:urcrypt.BUILD", - sha256 = "d27ec04d3854da7c479dd815af92ffef986616bc7ff400022e2dfb7971853d86", + sha256 = "6f054aeca23081265e9c7ed76dd156ac051b64b49d82375f761163d4988fef62", strip_prefix = "urcrypt-{version}", url = "https://github.com/urbit/urcrypt/archive/{version}.tar.gz", - version = "9ae5d604528bc54ae48430f55ebbb17b1ad7956c", + version = "c6052b52645d1097602b15cc590ccf05495117ba", ) versioned_http_archive( diff --git a/pkg/noun/jets/e/chacha.c b/pkg/noun/jets/e/chacha.c new file mode 100644 index 0000000000..779e08a15e --- /dev/null +++ b/pkg/noun/jets/e/chacha.c @@ -0,0 +1,67 @@ +/// @file + +#include "jets/q.h" +#include "jets/w.h" + +#include "noun.h" +#include "urcrypt.h" + + static u3_atom + _cqe_chacha_crypt(u3_atom rounds, u3_atom key, u3_atom nonce, u3_atom counter, u3_atom wid, u3_atom dat) + { + c3_w rounds_w, wid_w; + c3_d counter_d; + if ( !u3r_word_fit(&rounds_w, rounds) || !u3r_word_fit(&wid_w, wid) || c3n == u3r_safe_chub(counter, &counter_d) ) { + return u3m_bail(c3__fail); + } + else { + c3_y key_y[32], nonce_y[8]; + u3r_bytes(0, 32, key_y, key); + c3_y *dat_y = u3r_bytes_alloc(0, wid_w, dat); + urcrypt_chacha_crypt(rounds, key_y, nonce_y, counter, wid, dat_y); + u3_noun cry = u3i_bytes(wid_w, dat_y); + u3a_free(dat_y); + return u3i_cell(wid, cry); + } + } + + u3_noun + u3we_chacha_crypt(u3_noun cor) + { + u3_noun sam = u3x_at(u3x_sam, cor); + u3_noun rounds, key, nonce, counter, msg; + u3_noun wid, dat; + + if ( u3r_quil(sam, &rounds, &key, &nonce, &counter, &msg) || + u3ud(rounds) || u3ud(key) || u3ud(nonce) || u3ud(counter) || u3r_cell(msg, &wid, &dat) ) + { + return u3m_bail(c3__exit); + } else { + return u3l_punt("chacha_crypt", _cqe_chacha_crypt(rounds, key, nonce, counter, wid, dat)); + } + } + + + static u3_noun + _cqe_chacha_xchacha(u3_atom key, u3_atom nonce) + { + c3_y key_y[32], nonce_y[64], xkey_y[32], xnonce_y[8]; + u3r_bytes(0, 32, key_y, key); + u3r_bytes(0, 24, nonce_y, nonce); + urcrypt_chacha_xchacha(key_y, nonce_y, xkey_y, xnonce_y); + return u3i_cell(u3i_bytes(32, xkey_y), u3i_bytes(8, xnonce_y)); + } + + u3_noun + u3we_chacha_xchacha(u3_noun cor) + { + u3_noun key, nonce; + if ( c3n == u3r_mean(cor, u3x_sam_2, &key, u3x_sam_3, &nonce, 0) || + c3n == u3ud(key) || + c3n == u3ud(nonce) ) + { + return u3m_bail(c3__exit); + } else { + return u3l_punt("chacha_xchacha", _cqe_chacha_xchacha(key, nonce)); + } + } diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index e40fe03846..da57da0195 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -495,14 +495,14 @@ static u3j_core _140_hex_d[] = { "coed", 63, 0, _140_hex_coed_d, _140_hex_coed_ha }, { "aes", 31, 0, _140_hex_aes_d, _140_hex_aes_ha }, - { "hmac", 63, 0, _140_hex_hmac_d, _140_hex_hmac_ha }, - { "argon", 31, 0, _140_hex_argon_d, _140_hex_argon_ha }, - { "blake", 31, 0, _140_hex_blake_d, _140_hex_blake_ha }, - { "kecc", 31, 0, _140_hex_kecc_d, no_hashes }, - { "ripemd", 31, 0, _140_hex_ripe_d, _140_hex_ripe_ha }, - { "scr", 31, 0, _140_hex_scr_d, _140_hex_scr_ha }, - { "secp", 6, 0, _140_hex_secp_d, _140_hex_secp_ha }, - { "mimes", 31, 0, _140_hex_mimes_d, _140_hex_mimes_ha }, + { "hmac", 63, 0, _140_hex_hmac_d, _140_hex_hmac_ha }, + { "argon", 31, 0, _140_hex_argon_d, _140_hex_argon_ha }, + { "blake", 31, 0, _140_hex_blake_d, _140_hex_blake_ha }, + { "kecc", 31, 0, _140_hex_kecc_d, no_hashes }, + { "ripemd", 31, 0, _140_hex_ripe_d, _140_hex_ripe_ha }, + { "scr", 31, 0, _140_hex_scr_d, _140_hex_scr_ha }, + { "secp", 6, 0, _140_hex_secp_d, _140_hex_secp_ha }, + { "mimes", 31, 0, _140_hex_mimes_d, _140_hex_mimes_ha }, {} }; static c3_c* _140_hex_ha[] = { @@ -2129,15 +2129,15 @@ static u3j_core _139_hex_d[] = { "coed", 63, 0, _140_hex_coed_d, no_hashes }, { "aes", 31, 0, _140_hex_aes_d, no_hashes }, - { "hmac", 63, 0, _140_hex_hmac_d, no_hashes }, - { "argon", 31, 0, _140_hex_argon_d, no_hashes }, - { "blake", 31, 0, _140_hex_blake_d, no_hashes }, - { "kecc", 31, 0, _140_hex_kecc_d, no_hashes }, - { "ripemd", 31, 0, _140_hex_ripe_d, no_hashes }, - { "scr", 31, 0, _140_hex_scr_d, no_hashes }, - { "secp", 6, 0, _140_hex_secp_d, no_hashes }, - { "mimes", 31, 0, _140_hex_mimes_d, no_hashes }, - { "json", 31, 0, _139_hex_json_d, no_hashes }, + { "hmac", 63, 0, _140_hex_hmac_d, no_hashes }, + { "argon", 31, 0, _140_hex_argon_d, no_hashes }, + { "blake", 31, 0, _140_hex_blake_d, no_hashes }, + { "kecc", 31, 0, _140_hex_kecc_d, no_hashes }, + { "ripemd", 31, 0, _140_hex_ripe_d, no_hashes }, + { "scr", 31, 0, _140_hex_scr_d, no_hashes }, + { "secp", 6, 0, _140_hex_secp_d, no_hashes }, + { "mimes", 31, 0, _140_hex_mimes_d, no_hashes }, + { "json", 31, 0, _139_hex_json_d, no_hashes }, {} }; @@ -2341,6 +2341,14 @@ static u3j_core _138_hex_blake_d[] = {} }; + static u3j_harm _138_hex_chacha_crypt_a[] = {{".2", u3we_chacha_crypt, c3y}, {}}; + static u3j_harm _138_hex_chacha_xchacha_a[] = {{".2", u3we_chacha_xchacha, c3y}, {}}; +static u3j_core _138_hex_chacha_d[] = + { { "crypt", 7, _138_hex_chacha_crypt_a, 0, no_hashes }, + { "xchacha", 7, _138_hex_chacha_xchacha_a, 0, no_hashes }, + {} + }; + static u3j_core _138_hex_d[] = { { "lore", 63, _140_hex_lore_a, 0, no_hashes }, { "leer", 63, _140_hex_leer_a, 0, no_hashes }, @@ -2350,15 +2358,16 @@ static u3j_core _138_hex_d[] = { "coed", 63, 0, _140_hex_coed_d, no_hashes }, { "aes", 31, 0, _140_hex_aes_d, no_hashes }, - { "hmac", 63, 0, _140_hex_hmac_d, no_hashes }, - { "argon", 31, 0, _140_hex_argon_d, no_hashes }, - { "blake", 31, 0, _138_hex_blake_d, no_hashes }, - { "kecc", 31, 0, _140_hex_kecc_d, no_hashes }, - { "ripemd", 31, 0, _140_hex_ripe_d, no_hashes }, - { "scr", 31, 0, _140_hex_scr_d, no_hashes }, - { "secp", 6, 0, _140_hex_secp_d, no_hashes }, - { "mimes", 31, 0, _140_hex_mimes_d, no_hashes }, - { "json", 31, 0, _139_hex_json_d, no_hashes }, + { "hmac", 63, 0, _140_hex_hmac_d, no_hashes }, + { "argon", 31, 0, _140_hex_argon_d, no_hashes }, + { "blake", 31, 0, _138_hex_blake_d, no_hashes }, + { "chacha", 31, 0, _138_hex_chacha_d, no_hashes }, + { "kecc", 31, 0, _140_hex_kecc_d, no_hashes }, + { "ripemd", 31, 0, _140_hex_ripe_d, no_hashes }, + { "scr", 31, 0, _140_hex_scr_d, no_hashes }, + { "secp", 6, 0, _140_hex_secp_d, no_hashes }, + { "mimes", 31, 0, _140_hex_mimes_d, no_hashes }, + { "json", 31, 0, _139_hex_json_d, no_hashes }, {} }; diff --git a/pkg/noun/jets/w.h b/pkg/noun/jets/w.h index 34716971bd..9d49bce0f8 100644 --- a/pkg/noun/jets/w.h +++ b/pkg/noun/jets/w.h @@ -211,6 +211,9 @@ u3_noun u3we_blake3_chunk_output(u3_noun); u3_noun u3we_blake3_compress(u3_noun); + u3_noun u3we_chacha_crypt(u3_noun); + u3_noun u3we_chacha_xchacha(u3_noun); + u3_noun u3we_ripe(u3_noun); u3_noun u3we_make(u3_noun); From 338154c13e913c8248ba3d810af08db7429c89f3 Mon Sep 17 00:00:00 2001 From: Sigilante Date: Wed, 17 Apr 2024 17:02:46 -0500 Subject: [PATCH 104/430] Post all jets. --- pkg/noun/jets/f/lagoon.c | 159 +++++++++++++++++++-------------------- 1 file changed, 79 insertions(+), 80 deletions(-) diff --git a/pkg/noun/jets/f/lagoon.c b/pkg/noun/jets/f/lagoon.c index 8dc6144d2e..9e5215e387 100644 --- a/pkg/noun/jets/f/lagoon.c +++ b/pkg/noun/jets/f/lagoon.c @@ -128,7 +128,7 @@ u3r_bytes(0, syz_x+1, y_bytes, y_data); // Switch on the block size. - switch (bloq) { + switch (u3x_atom(bloq)) { case 4: haxpy(len_x, (float16_t){SB_REAL16_ONE}, (float16_t*)x_bytes, 1, (float16_t*)y_bytes, 1); break; @@ -186,7 +186,7 @@ u3r_bytes(0, syz_x+1, y_bytes, y_data); // Switch on the block size. - switch (bloq) { + switch (u3x_atom(bloq)) { case 4: haxpy(len_x, (float16_t){SB_REAL16_NEGONE}, (float16_t*)x_bytes, 1, (float16_t*)y_bytes, 1); break; @@ -245,7 +245,7 @@ u3r_bytes(0, syz_x+1, y_bytes, y_data); // Switch on the block size. - switch (bloq) { + switch (u3x_atom(bloq)) { case 4: for (c3_d i = 0; i < len_x; i++) { ((float16_t*)y_bytes)[i] = f16_mul(((float16_t*)x_bytes)[i], ((float16_t*)y_bytes)[i]); @@ -311,7 +311,7 @@ u3r_bytes(0, syz_x+1, y_bytes, y_data); // Switch on the block size. - switch (bloq) { + switch (u3x_atom(bloq)) { case 4: for (c3_d i = 0; i < len_x; i++) { ((float16_t*)y_bytes)[i] = f16_div(((float16_t*)x_bytes)[i], ((float16_t*)y_bytes)[i]); @@ -377,7 +377,7 @@ u3r_bytes(0, syz_x+1, y_bytes, y_data); // Switch on the block size. - switch (bloq) { + switch (u3x_atom(bloq)) { case 4: for (c3_d i = 0; i < len_x; i++) { float16_t x_val16 = ((float16_t*)x_bytes)[i]; @@ -466,6 +466,7 @@ if (bloq < 4 || bloq > 7) { return u3_none; } + fprintf(stderr, "function rounding mode: %lx\r\n", softfloat_roundingMode); // Unpack the data as a byte array. We assume total length < 2**64. // len_x is length in base units @@ -481,7 +482,7 @@ u3_noun r_data; // Switch on the block size. - switch (bloq) { + switch (u3x_atom(bloq)) { case 4: ; float16_t sum16[2]; for (c3_d i = 0; i < len_x; i++) { @@ -512,7 +513,10 @@ case 7: ; float128_t sum128[2]; for (c3_d i = 0; i < len_x; i++) { + fprintf(stderr, " sum128[%d] = %lx %lx\r\n", i, sum128[i].v[0], sum128[i].v[1]); + fprintf(stderr, "x_bytes[%d] = %lx %lx\r\n", i, ((float128_t*)x_bytes)[i].v[0], ((float128_t*)x_bytes)[i].v[1]); f128M_add(&(sum128[0]), &(((float128_t*)x_bytes)[i]), &(sum128[0])); + fprintf(stderr, " equals[%d] = %lx %lx\r\n", i, sum128[i].v[0], sum128[i].v[1]); } sum128[1] = (float128_t){0x1, 0x0}; r_data = u3i_bytes((16+1)*sizeof(c3_y), (c3_y*)sum128); @@ -551,7 +555,7 @@ c3_w min_idx = 0; // Switch on the block size. - switch (bloq) { + switch (u3x_atom(bloq)) { case 4: ; float16_t min_val16 = ((float16_t*)x_bytes)[0]; for (c3_d i = 0; i < len_x; i++) { @@ -624,7 +628,7 @@ c3_w max_idx = 0; // Switch on the block size. - switch (bloq) { + switch (u3x_atom(bloq)) { case 4: ; float16_t max_val16 = ((float16_t*)x_bytes)[0]; for (c3_d i = 0; i < len_x; i++) { @@ -696,10 +700,10 @@ u3r_bytes(0, syz_x, x_bytes, x_data); // r_data is the result noun of [data] - u3_noun r_data; + u3_noun r_data = u3_nul; // Switch on the block size. - switch (bloq) { + switch (u3x_atom(bloq)) { case 4: for (c3_d i = 0; i < len_x; i++) { float16_t x_val16 = ((float16_t*)x_bytes)[i]; @@ -761,7 +765,7 @@ u3_noun r_data; // Switch on the block size. - switch (bloq) { + switch (u3x_atom(bloq)) { case 4: ; float16_t min_val16 = ((float16_t*)x_bytes)[0]; for (c3_d i = 0; i < len_x; i++) { @@ -839,7 +843,7 @@ u3_noun r_data; // Switch on the block size. - switch (bloq) { + switch (u3x_atom(bloq)) { case 4: ; float16_t max_val16 = ((float16_t*)x_bytes)[0]; for (c3_d i = 0; i < len_x; i++) { @@ -915,7 +919,7 @@ u3r_bytes(0, syz_x+1, x_bytes, x_data); // Switch on the block size. - switch (bloq) { + switch (u3x_atom(bloq)) { case 4: for (c3_d i = 0; i < len_x; i++) { ((float16_t*)x_bytes)[i] = f16_abs(((float16_t*)x_bytes)[i]); @@ -979,7 +983,7 @@ u3r_bytes(0, syz_x+1, y_bytes, y_data); // Switch on the block size. - switch (bloq) { + switch (u3x_atom(bloq)) { case 4: for (c3_d i = 0; i < len_x; i++) { float16_t x_val16 = ((float16_t*)x_bytes)[i]; @@ -1052,7 +1056,7 @@ u3r_bytes(0, syz_x+1, y_bytes, y_data); // Switch on the block size. - switch (bloq) { + switch (u3x_atom(bloq)) { case 4: for (c3_d i = 0; i < len_x; i++) { float16_t x_val16 = ((float16_t*)x_bytes)[i]; @@ -1125,7 +1129,7 @@ u3r_bytes(0, syz_x+1, y_bytes, y_data); // Switch on the block size. - switch (bloq) { + switch (u3x_atom(bloq)) { case 4: for (c3_d i = 0; i < len_x; i++) { float16_t x_val16 = ((float16_t*)x_bytes)[i]; @@ -1198,7 +1202,7 @@ u3r_bytes(0, syz_x+1, y_bytes, y_data); // Switch on the block size. - switch (bloq) { + switch (u3x_atom(bloq)) { case 4: for (c3_d i = 0; i < len_x; i++) { float16_t x_val16 = ((float16_t*)x_bytes)[i]; @@ -1275,7 +1279,7 @@ float128_t n128; // Switch on the block size. We assume that n fits in the target block size; Hoon typecheck should prevent. - switch (bloq) { + switch (u3x_atom(bloq)) { case 4: u3r_bytes(0, 2, (c3_y*)&(n16.v), n); // set y to [n] @@ -1357,7 +1361,7 @@ float128_t n128; // Switch on the block size. We assume that n fits in the target block size; Hoon typecheck should prevent. - switch (bloq) { + switch (u3x_atom(bloq)) { case 4: u3r_bytes(0, 2, (c3_y*)&(n16.v), n); // set y to [n] @@ -1438,7 +1442,7 @@ float128_t n128; // Switch on the block size. - switch (bloq) { + switch (u3x_atom(bloq)) { case 4: u3r_bytes(0, 2, (c3_y*)&(n16.v), n); hscal(len_x, n16, (float16_t*)x_bytes, 1); @@ -1501,7 +1505,7 @@ float128_t in128; // Switch on the block size. - switch (bloq) { + switch (u3x_atom(bloq)) { case 4: // XX note that in16 is doing double duty here u3r_bytes(0, 2, (c3_y*)&(in16.v), n); @@ -1572,7 +1576,7 @@ float128_t n128, in128; // Switch on the block size. - switch (bloq) { + switch (u3x_atom(bloq)) { case 4: u3r_bytes(0, 2, (c3_y*)&(n16.v), n); in16 = f16_div((float16_t){SB_REAL16_ONE}, n16); @@ -1688,7 +1692,7 @@ u3_noun r_data; // Switch on the block size. - switch (bloq) { + switch (u3x_atom(bloq)) { case 4: ; float16_t r16[2]; r16[0] = hdot(len_x, (float16_t*)x_bytes, 1, (float16_t*)y_bytes, 1); @@ -1836,7 +1840,7 @@ u3_noun r_data; - switch (bloq) { + switch (u3x_atom(bloq)) { case 4: ; float16_t a16, b16; u3r_bytes(0, 2, (c3_y*)&(a16.v), a); @@ -1931,7 +1935,7 @@ u3_noun r_data; - switch (bloq) { + switch (u3x_atom(bloq)) { case 4: ; float16_t a16, b16, interval16; u3r_bytes(0, 2, (c3_y*)&(a16.v), a); @@ -2042,75 +2046,66 @@ } c3_d N = Na; - c3_y* x_bytes = (c3_y*)u3a_malloc((M*N)*sizeof(c3_y)); - u3r_bytes(0, M*N, x_bytes, x_data); - c3_y* y_bytes = (c3_y*)u3a_malloc((N*P)*sizeof(c3_y)); - u3r_bytes(0, N*P, y_bytes, y_data); - c3_y* c_bytes = (c3_y*)u3a_malloc((M*P)*sizeof(c3_y)); - - u3_noun r_data; - - // Switch on the block size. - switch (bloq) { - case 4: - hgemm('N', 'N', M, N, P, (float16_t){SB_REAL16_ONE}, (float16_t*)x_bytes, N, (float16_t*)y_bytes, N, (float16_t){SB_REAL16_ZERO}, (float16_t*)c_bytes, P); + // Unpack the data as a byte array. We assume total length < 2**64. + // len_x is length in base units + c3_d len_x = _get_length(x_shape); // M*N - // Unpack the result back into a noun. - r_data = u3i_bytes(M*P, c_bytes); + // syz_x is length in bytes + c3_d syz_x = len_x * pow(2, bloq-3); // M*N - // Clean up. - u3a_free(x_bytes); - u3a_free(y_bytes); - u3a_free(c_bytes); + // x_bytes is the data array (w/o leading 0x1) + c3_y* x_bytes = (c3_y*)u3a_malloc(syz_x*pow(2,bloq-3)*sizeof(c3_y)); + u3r_bytes(0, syz_x, x_bytes, x_data); - return u3nc(u3nq(u3nl(M, P, u3_none), bloq, c3__real, u3_nul), r_data); + // len_x is length in base units + c3_d len_y = _get_length(y_shape); // N*P - case 5: - sgemm('N', 'N', M, N, P, (float32_t){SB_REAL32_ONE}, (float32_t*)x_bytes, N, (float32_t*)y_bytes, N, (float32_t){SB_REAL32_ZERO}, (float32_t*)c_bytes, P); + // syz_x is length in bytes + c3_d syz_y = len_x * pow(2, bloq-3); // N*P - // Unpack the result back into a noun. - r_data = u3i_bytes(M*P, c_bytes); + // y_bytes is the data array (w/o leading 0x1) + c3_y* y_bytes = (c3_y*)u3a_malloc(syz_y*pow(2,bloq-3)*sizeof(c3_y)); + u3r_bytes(0, syz_y, y_bytes, y_data); - // Clean up. - u3a_free(x_bytes); - u3a_free(y_bytes); - u3a_free(c_bytes); + // syz_r is length in bytes + c3_d syz_r = (M*P) * pow(2, bloq-3); // M*P + + // len_r is length in base units + c3_d len_r = M*P; // M*P - return u3nc(u3nq(u3nl(M, P, u3_none), bloq, c3__real, u3_nul), r_data); + // r_bytes is the result array + c3_y* r_bytes = (c3_y*)u3a_malloc((syz_r*pow(2,bloq-3)+1)*sizeof(c3_y)); + r_bytes[syz_r] = 1; // pin head - case 6: - dgemm('N', 'N', M, N, P, (float64_t){SB_REAL64_ONE}, (float64_t*)x_bytes, N, (float64_t*)y_bytes, N, (float64_t){SB_REAL64_ZERO}, (float64_t*)c_bytes, P); + u3_noun r_data; - // Unpack the result back into a noun. - r_data = u3i_bytes(M*P, c_bytes); + // Switch on the block size. + switch (u3x_atom(bloq)) { + case 4: + hgemm('N', 'N', M, N, P, (float16_t){SB_REAL16_ONE}, (float16_t*)x_bytes, N, (float16_t*)y_bytes, N, (float16_t){SB_REAL16_ZERO}, (float16_t*)r_bytes, P); + break; - // Clean up. - u3a_free(x_bytes); - u3a_free(y_bytes); - u3a_free(c_bytes); + case 5: + sgemm('N', 'N', M, N, P, (float32_t){SB_REAL32_ONE}, (float32_t*)x_bytes, N, (float32_t*)y_bytes, N, (float32_t){SB_REAL32_ZERO}, (float32_t*)r_bytes, P); + break; - return u3nc(u3nq(u3nl(M, P, u3_none), bloq, c3__real, u3_nul), r_data); + case 6: + dgemm('N', 'N', M, N, P, (float64_t){SB_REAL64_ONE}, (float64_t*)x_bytes, N, (float64_t*)y_bytes, N, (float64_t){SB_REAL64_ZERO}, (float64_t*)r_bytes, P); + break; case 7: - qgemm('N', 'N', M, N, P, (float128_t){SB_REAL128L_ONE,SB_REAL128U_ONE}, (float128_t*)x_bytes, N, (float128_t*)y_bytes, N, (float128_t){SB_REAL128L_ZERO,SB_REAL128U_ZERO}, (float128_t*)c_bytes, P); - - // Unpack the result back into a noun. - r_data = u3i_bytes(M*P, c_bytes); + qgemm('N', 'N', M, N, P, (float128_t){SB_REAL128L_ONE,SB_REAL128U_ONE}, (float128_t*)x_bytes, N, (float128_t*)y_bytes, N, (float128_t){SB_REAL128L_ZERO,SB_REAL128U_ZERO}, (float128_t*)r_bytes, P); + break; + } - // Clean up. - u3a_free(x_bytes); - u3a_free(y_bytes); - u3a_free(c_bytes); + // Unpack the result back into a noun. + r_data = u3i_bytes(syz_r*pow(2,bloq-3)+1, r_bytes); - return u3nc(u3nq(u3nl(M, P, u3_none), bloq, c3__real, u3_nul), r_data); + u3a_free(x_bytes); + u3a_free(y_bytes); + u3a_free(r_bytes); - default: - u3a_free(x_bytes); - u3a_free(y_bytes); - u3a_free(c_bytes); - - return u3_none; - } + return u3nc(u3nq(u3nt(u3k(M), u3k(P), u3_nul), u3k(bloq), c3__real, u3_nul), u3k(r_data)); } u3_noun @@ -2419,6 +2414,8 @@ case c3__real: _set_rounding(rnd); u3_noun r_data = u3qf_la_cumsum_real(x_data, x_shape, x_bloq); + fprintf(stderr, "desired rounding mode: %lx\r\n", rnd); + fprintf(stderr, "apparent rounding mode: %lx\r\n", softfloat_roundingMode); return u3nc(u3nq(u3nt(0x1, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -3349,8 +3346,10 @@ switch (x_kind) { case c3__real: _set_rounding(rnd); - return u3qf_la_mmul_real(x_data, y_data, x_shape, y_shape, x_bloq); - break; + u3_noun r_data; + r_data = u3qf_la_mmul_real(x_data, y_data, x_shape, y_shape, x_bloq); + // result is already [meta data] + return u3k(r_data); default: return u3_none; From 6f2fc23c6d97f4b206cc239e7bb156f906f30ce5 Mon Sep 17 00:00:00 2001 From: Sigilante Date: Wed, 17 Apr 2024 19:40:06 -0500 Subject: [PATCH 105/430] Fix cumsum. --- pkg/noun/jets/f/lagoon.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/pkg/noun/jets/f/lagoon.c b/pkg/noun/jets/f/lagoon.c index 9e5215e387..0d16f70eb5 100644 --- a/pkg/noun/jets/f/lagoon.c +++ b/pkg/noun/jets/f/lagoon.c @@ -485,6 +485,7 @@ switch (u3x_atom(bloq)) { case 4: ; float16_t sum16[2]; + sum16[0] = (float16_t){SB_REAL16_ZERO}; for (c3_d i = 0; i < len_x; i++) { sum16[0] = f16_add(sum16[0], ((float16_t*)x_bytes)[i]); } @@ -494,6 +495,7 @@ case 5: ; float32_t sum32[2]; + sum32[0] = (float32_t){SB_REAL32_ZERO}; for (c3_d i = 0; i < len_x; i++) { sum32[0] = f32_add(sum32[0], ((float32_t*)x_bytes)[i]); } @@ -503,6 +505,7 @@ case 6: ; float64_t sum64[2]; + sum64[0] = (float64_t){SB_REAL64_ZERO}; for (c3_d i = 0; i < len_x; i++) { sum64[0] = f64_add(sum64[0], ((float64_t*)x_bytes)[i]); } @@ -512,11 +515,9 @@ case 7: ; float128_t sum128[2]; + sum128[0] = (float128_t){SB_REAL128L_ZERO, SB_REAL128U_ZERO}; for (c3_d i = 0; i < len_x; i++) { - fprintf(stderr, " sum128[%d] = %lx %lx\r\n", i, sum128[i].v[0], sum128[i].v[1]); - fprintf(stderr, "x_bytes[%d] = %lx %lx\r\n", i, ((float128_t*)x_bytes)[i].v[0], ((float128_t*)x_bytes)[i].v[1]); f128M_add(&(sum128[0]), &(((float128_t*)x_bytes)[i]), &(sum128[0])); - fprintf(stderr, " equals[%d] = %lx %lx\r\n", i, sum128[i].v[0], sum128[i].v[1]); } sum128[1] = (float128_t){0x1, 0x0}; r_data = u3i_bytes((16+1)*sizeof(c3_y), (c3_y*)sum128); @@ -2414,8 +2415,6 @@ case c3__real: _set_rounding(rnd); u3_noun r_data = u3qf_la_cumsum_real(x_data, x_shape, x_bloq); - fprintf(stderr, "desired rounding mode: %lx\r\n", rnd); - fprintf(stderr, "apparent rounding mode: %lx\r\n", softfloat_roundingMode); return u3nc(u3nq(u3nt(0x1, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2640,7 +2639,6 @@ switch (x_kind) { case c3__real: ; u3_noun r_data = u3qf_la_abs_real(x_data, x_shape, x_bloq); - fprintf(stderr, "abs: %lx\n", x_bloq); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: From 90014967eb162dafdf62ebc778339bf8d44929b8 Mon Sep 17 00:00:00 2001 From: Sigilante Date: Thu, 18 Apr 2024 14:26:51 -0500 Subject: [PATCH 106/430] Post working version of all jets for reals to date. --- pkg/noun/jets/f/lagoon.c | 106 +++++++++++++++++++-------------------- pkg/noun/jets/tree.c | 20 ++++---- 2 files changed, 62 insertions(+), 64 deletions(-) diff --git a/pkg/noun/jets/f/lagoon.c b/pkg/noun/jets/f/lagoon.c index 0d16f70eb5..8fd95be2ab 100644 --- a/pkg/noun/jets/f/lagoon.c +++ b/pkg/noun/jets/f/lagoon.c @@ -466,7 +466,6 @@ if (bloq < 4 || bloq > 7) { return u3_none; } - fprintf(stderr, "function rounding mode: %lx\r\n", softfloat_roundingMode); // Unpack the data as a byte array. We assume total length < 2**64. // len_x is length in base units @@ -1319,7 +1318,7 @@ } // r_data is the result noun of [data] - y_bytes[syz_x] = 1; // pin head + y_bytes[syz_x] = 0x1; // pin head u3_noun r_data = u3i_bytes((syz_x+1)*sizeof(c3_y), y_bytes); // Clean up and return. @@ -1401,7 +1400,7 @@ } // r_data is the result noun of [data] - x_bytes[syz_x] = 1; // pin head + x_bytes[syz_x] = 0x1; // pin head u3_noun r_data = u3i_bytes((syz_x+1)*sizeof(c3_y), x_bytes); // Clean up and return. @@ -1435,7 +1434,7 @@ // x_bytes is the data array (w/ leading 0x1, skipped by ?axpy) c3_y* x_bytes = (c3_y*)u3a_malloc((syz_x+1)*sizeof(c3_y)); u3r_bytes(0, syz_x, x_bytes, x_data); - x_bytes[syz_x] = 1; // pin head + x_bytes[syz_x] = 0x1; // pin head float16_t n16; float32_t n32; @@ -1498,7 +1497,7 @@ // x_bytes is the data array (w/ leading 0x1, skipped by ?axpy) c3_y* x_bytes = (c3_y*)u3a_malloc((syz_x+1)*sizeof(c3_y)); u3r_bytes(0, syz_x, x_bytes, x_data); - x_bytes[syz_x] = 1; // pin head + x_bytes[syz_x] = 0x1; // pin head float16_t in16; float32_t in32; @@ -1769,7 +1768,7 @@ y_bytes[i*wyd+k] = x_bytes[(i*dims[0]+i)*wyd+k]; } } - y_bytes[syz_y] = 1; // pin head + y_bytes[syz_y] = 0x1; // pin head // Unpack the result back into a noun. r_data = u3i_bytes((syz_y+1)*sizeof(c3_y), y_bytes); @@ -1814,7 +1813,7 @@ } } } - y_bytes[syz_x] = 1; // pin head + y_bytes[syz_x] = 0x1; // pin head // Unpack the result back into a noun. r_data = u3i_bytes((syz_x+1)*sizeof(c3_y), y_bytes); @@ -1854,7 +1853,7 @@ } ((float16_t*)x_bytes16)[n] = a16; ((float16_t*)x_bytes16)[0] = b16; - x_bytes16[(n+1)*2] = 1; // pin head + x_bytes16[(n+1)*2] = 0x1; // pin head r_data = u3i_bytes(((n+1)*2+1)*sizeof(c3_y), x_bytes16); u3a_free(x_bytes16); break; @@ -1871,7 +1870,7 @@ } ((float32_t*)x_bytes32)[n] = a32; ((float32_t*)x_bytes32)[0] = b32; - x_bytes32[(n+1)*4] = 1; // pin head + x_bytes32[(n+1)*4] = 0x1; // pin head r_data = u3i_bytes(((n+1)*4+1)*sizeof(c3_y), x_bytes32); u3a_free(x_bytes32); break; @@ -1888,7 +1887,7 @@ } ((float64_t*)x_bytes64)[n] = a64; ((float64_t*)x_bytes64)[0] = b64; - x_bytes64[(n+1)*8] = 1; // pin head + x_bytes64[(n+1)*8] = 0x1; // pin head r_data = u3i_bytes(((n+1)*8+1)*sizeof(c3_y), x_bytes64); u3a_free(x_bytes64); break; @@ -1912,7 +1911,7 @@ } ((float128_t*)x_bytes128)[n] = a128; ((float128_t*)x_bytes128)[0] = b128; - x_bytes128[(n+1)*16] = 1; // pin head + x_bytes128[(n+1)*16] = 0x1; // pin head r_data = u3i_bytes(((n+1)*16+1)*sizeof(c3_y), x_bytes128); u3a_free(x_bytes128); break; @@ -1949,7 +1948,7 @@ } ((float16_t*)x_bytes16)[n16] = a16; // ((float16_t*)x_bytes16)[0] = b16; - x_bytes16[(n16+1)*2] = 1; // pin head + x_bytes16[(n16+1)*2] = 0x1; // pin head r_data = u3i_bytes(((n16+1)*2+1)*sizeof(c3_y), x_bytes16); u3a_free(x_bytes16); break; @@ -1966,7 +1965,7 @@ } ((float32_t*)x_bytes32)[n32] = a32; // ((float32_t*)x_bytes32)[0] = b32; - x_bytes32[(n32+1)*4] = 1; // pin head + x_bytes32[(n32+1)*4] = 0x1; // pin head r_data = u3i_bytes(((n32+1)*4+1)*sizeof(c3_y), x_bytes32); u3a_free(x_bytes32); break; @@ -1983,7 +1982,7 @@ } ((float64_t*)x_bytes64)[n64] = a64; // ((float64_t*)x_bytes64)[0] = b64; - x_bytes64[(n64+1)*8] = 1; // pin head + x_bytes64[(n64+1)*8] = 0x1; // pin head r_data = u3i_bytes(((n64+1)*8+1)*sizeof(c3_y), x_bytes64); u3a_free(x_bytes64); break; @@ -2003,7 +2002,7 @@ } ((float128_t*)x_bytes128)[n128] = a128; // ((float128_t*)x_bytes128)[0] = b128; - x_bytes128[(n128+1)*16] = 1; // pin head + x_bytes128[(n128+1)*16] = 0x1; // pin head r_data = u3i_bytes(((n128+1)*16+1)*sizeof(c3_y), x_bytes128); u3a_free(x_bytes128); break; @@ -2021,7 +2020,7 @@ { u3_noun d_data = u3qf_la_diag(x_data, shape, bloq); c3_d len_x0 = _get_dims(shape)[0]; - u3_noun r_data = u3qf_la_dot_real(d_data, d_data, u3nt(len_x0, 0x1, u3_nul), bloq); + u3_noun r_data = u3qf_la_dot_real(d_data, d_data, u3nt(len_x0, 0x1, u3_nul), u3k(bloq)); return r_data; } @@ -2035,10 +2034,10 @@ u3_noun bloq) { // Unpack the data as a byte array. We assume total length < 2**64. - c3_d M = u3h(x_shape); - c3_d Na = u3h(u3t(x_shape)); - c3_d Nb = u3h(y_shape); - c3_d P = u3h(u3t(y_shape)); + c3_d M = u3x_atom(u3h(x_shape)); + c3_d Na= u3x_atom(u3h(u3t(x_shape))); + c3_d Nb= u3x_atom(u3h(y_shape)); + c3_d P = u3x_atom(u3h(u3t(y_shape))); if ((u3_nul != u3t(u3t(x_shape))) || (u3_nul != u3t(u3t(y_shape))) || @@ -2055,58 +2054,62 @@ c3_d syz_x = len_x * pow(2, bloq-3); // M*N // x_bytes is the data array (w/o leading 0x1) - c3_y* x_bytes = (c3_y*)u3a_malloc(syz_x*pow(2,bloq-3)*sizeof(c3_y)); + c3_y* x_bytes = (c3_y*)u3a_malloc(syz_x*sizeof(c3_y)); u3r_bytes(0, syz_x, x_bytes, x_data); // len_x is length in base units c3_d len_y = _get_length(y_shape); // N*P // syz_x is length in bytes - c3_d syz_y = len_x * pow(2, bloq-3); // N*P + c3_d syz_y = len_y * pow(2, bloq-3); // N*P // y_bytes is the data array (w/o leading 0x1) - c3_y* y_bytes = (c3_y*)u3a_malloc(syz_y*pow(2,bloq-3)*sizeof(c3_y)); + c3_y* y_bytes = (c3_y*)u3a_malloc(syz_y*sizeof(c3_y)); u3r_bytes(0, syz_y, y_bytes, y_data); - - // syz_r is length in bytes - c3_d syz_r = (M*P) * pow(2, bloq-3); // M*P // len_r is length in base units c3_d len_r = M*P; // M*P - // r_bytes is the result array - c3_y* r_bytes = (c3_y*)u3a_malloc((syz_r*pow(2,bloq-3)+1)*sizeof(c3_y)); - r_bytes[syz_r] = 1; // pin head + // syz_r is length in bytes + c3_d syz_r = len_r * pow(2, bloq-3); // M*P - u3_noun r_data; + // r_bytes is the result array + c3_y* r_bytes = (c3_y*)u3a_malloc((syz_r+1)*sizeof(c3_y)); + r_bytes[syz_r] = 0x1; // pin head + // initialize with 0x0s + for (c3_d i = 0; i < syz_r; i++) { + r_bytes[i] = 0x0; + } // Switch on the block size. switch (u3x_atom(bloq)) { case 4: - hgemm('N', 'N', M, N, P, (float16_t){SB_REAL16_ONE}, (float16_t*)x_bytes, N, (float16_t*)y_bytes, N, (float16_t){SB_REAL16_ZERO}, (float16_t*)r_bytes, P); + hgemm('N', 'N', M, N, P, (float16_t){SB_REAL16_ONE}, (float16_t*)x_bytes, N, (float16_t*)y_bytes, P, (float16_t){SB_REAL16_ZERO}, (float16_t*)r_bytes, P); break; case 5: - sgemm('N', 'N', M, N, P, (float32_t){SB_REAL32_ONE}, (float32_t*)x_bytes, N, (float32_t*)y_bytes, N, (float32_t){SB_REAL32_ZERO}, (float32_t*)r_bytes, P); + sgemm('N', 'N', M, N, P, (float32_t){SB_REAL32_ONE}, (float32_t*)x_bytes, N, (float32_t*)y_bytes, P, (float32_t){SB_REAL32_ZERO}, (float32_t*)r_bytes, P); break; case 6: - dgemm('N', 'N', M, N, P, (float64_t){SB_REAL64_ONE}, (float64_t*)x_bytes, N, (float64_t*)y_bytes, N, (float64_t){SB_REAL64_ZERO}, (float64_t*)r_bytes, P); + dgemm('N', 'N', M, N, P, (float64_t){SB_REAL64_ONE}, (float64_t*)x_bytes, N, (float64_t*)y_bytes, P, (float64_t){SB_REAL64_ZERO}, (float64_t*)r_bytes, P); break; case 7: - qgemm('N', 'N', M, N, P, (float128_t){SB_REAL128L_ONE,SB_REAL128U_ONE}, (float128_t*)x_bytes, N, (float128_t*)y_bytes, N, (float128_t){SB_REAL128L_ZERO,SB_REAL128U_ZERO}, (float128_t*)r_bytes, P); + qgemm('N', 'N', M, N, P, (float128_t){SB_REAL128L_ONE,SB_REAL128U_ONE}, (float128_t*)x_bytes, N, (float128_t*)y_bytes, P, (float128_t){SB_REAL128L_ZERO,SB_REAL128U_ZERO}, (float128_t*)r_bytes, P); break; } // Unpack the result back into a noun. - r_data = u3i_bytes(syz_r*pow(2,bloq-3)+1, r_bytes); + u3_noun r_data = u3i_bytes(syz_r+1, r_bytes); + u3_noun M_ = u3i_chub(M); + u3_noun P_ = u3i_chub(P); u3a_free(x_bytes); u3a_free(y_bytes); u3a_free(r_bytes); - return u3nc(u3nq(u3nt(u3k(M), u3k(P), u3_nul), u3k(bloq), c3__real, u3_nul), u3k(r_data)); + return u3nc(u3nq(u3nt(M_, P_, u3_nul), u3k(bloq), c3__real, u3_nul), r_data); } u3_noun @@ -3321,22 +3324,18 @@ u3_noun x_shape, x_bloq, x_kind, x_fxp, y_shape, y_bloq, y_kind, y_fxp, rnd; - if ( c3n == u3r_mean(x_meta, - 2, &x_shape, - 6, &x_bloq, - 14, &x_kind, - 15, &x_fxp, - 0) || - c3n == u3r_mean(y_meta, - 2, &y_shape, - 6, &y_bloq, - 14, &y_kind, - 15, &y_fxp, - 0) || - c3n == u3r_sing(x_bloq, y_bloq) || - c3n == u3r_sing(x_kind, y_kind) || + x_shape = u3h(x_meta); // 2 + x_bloq = u3h(u3t(x_meta)); // 6 + x_kind = u3h(u3t(u3t(x_meta))); // 14 + x_fxp = u3t(u3t(u3t(x_meta))); // 15 + y_shape = u3h(y_meta); // 2 + y_bloq = u3h(u3t(y_meta)); // 6 + y_kind = u3h(u3t(u3t(y_meta))); // 14 + y_fxp = u3t(u3t(u3t(y_meta))); // 15 + rnd = u3h(u3t(u3t(u3t(cor)))); // 30 + if ( c3n == u3r_sing(x_bloq, y_bloq) || + c3n == u3r_sing(x_kind, y_kind) // fxp does not need to match so no check - c3n == u3r_mean(cor, u3x_con_sam, &rnd, 0) ) { return u3m_bail(c3__exit); @@ -3344,10 +3343,9 @@ switch (x_kind) { case c3__real: _set_rounding(rnd); - u3_noun r_data; - r_data = u3qf_la_mmul_real(x_data, y_data, x_shape, y_shape, x_bloq); + u3_noun r_data = u3qf_la_mmul_real(x_data, y_data, x_shape, y_shape, x_bloq); // result is already [meta data] - return u3k(r_data); + return r_data; default: return u3_none; diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index dc27123a4d..35573a42e6 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -2148,8 +2148,8 @@ static u3j_core _139_hex_json_d[] = }; /* linear algebra jets - XX move to outer _sep_ core for /lib? eventually -static u3j_core _139_sep_d[] = + XX move to outer _hep_ core for /lib? eventually +static u3j_core _139_hep_d[] = */ static u3j_harm _139_hex__lagoon_add_a[] = {{".2", u3wf_la_add}, {}}; static u3j_harm _139_hex__lagoon_sub_a[] = {{".2", u3wf_la_sub}, {}}; @@ -2167,15 +2167,15 @@ static u3j_harm _139_hex__lagoon_cumsum_a[]={{".2", u3wf_la_cumsum}, {}}; static u3j_harm _139_hex__lagoon_argmin_a[]={{".2", u3wf_la_argmin}, {}}; static u3j_harm _139_hex__lagoon_argmax_a[]={{".2", u3wf_la_argmax}, {}}; static u3j_harm _139_hex__lagoon_ravel_a[]={{".2", u3wf_la_ravel}, {}}; -static u3j_harm _139_hex__lagoon_min_a[]={{".2", u3wf_la_min}, {}}; -static u3j_harm _139_hex__lagoon_max_a[]={{".2", u3wf_la_max}, {}}; +static u3j_harm _139_hex__lagoon_min_a[] = {{".2", u3wf_la_min}, {}}; +static u3j_harm _139_hex__lagoon_max_a[] = {{".2", u3wf_la_max}, {}}; static u3j_harm _139_hex__lagoon_linspace_a[]={{".2", u3wf_la_linspace}, {}}; -static u3j_harm _139_hex__lagoon_range_a[]={{".2", u3wf_la_range}, {}}; -static u3j_harm _139_hex__lagoon_abs_a[]={{".2", u3wf_la_abs}, {}}; -static u3j_harm _139_hex__lagoon_gth_a[]={{".2", u3wf_la_gth}, {}}; -static u3j_harm _139_hex__lagoon_gte_a[]={{".2", u3wf_la_gte}, {}}; -static u3j_harm _139_hex__lagoon_lth_a[]={{".2", u3wf_la_lth}, {}}; -static u3j_harm _139_hex__lagoon_lte_a[]={{".2", u3wf_la_lte}, {}}; +static u3j_harm _139_hex__lagoon_range_a[]= {{".2", u3wf_la_range}, {}}; +static u3j_harm _139_hex__lagoon_abs_a[] = {{".2", u3wf_la_abs}, {}}; +static u3j_harm _139_hex__lagoon_gth_a[] = {{".2", u3wf_la_gth}, {}}; +static u3j_harm _139_hex__lagoon_gte_a[] = {{".2", u3wf_la_gte}, {}}; +static u3j_harm _139_hex__lagoon_lth_a[] = {{".2", u3wf_la_lth}, {}}; +static u3j_harm _139_hex__lagoon_lte_a[] = {{".2", u3wf_la_lte}, {}}; static u3j_harm _139_hex__lagoon_diag_a[] = {{".2", u3wf_la_diag}, {}}; static u3j_harm _139_hex__lagoon_trace_a[]= {{".2", u3wf_la_trace}, {}}; static u3j_harm _139_hex__lagoon_mmul_a[] = {{".2", u3wf_la_mmul}, {}}; From 392d6ebd0aacd0eca338d81648803a8cb952960c Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Sat, 20 Apr 2024 05:54:39 +0200 Subject: [PATCH 107/430] ames, mesa: unification ames.c doesn't send an %born task to ames.hoon, instead ames uses mesa.c to set the unix-duct --- pkg/vere/io/ames.c | 20 ++++++++++---------- pkg/vere/io/mesa.c | 25 ++++++++++++------------- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index 90b24781ad..dcf00340d5 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -2963,16 +2963,16 @@ _ames_io_talk(u3_auto* car_u) // send born event // - { - // XX remove [sev_l] - // - u3_noun wir = u3nt(c3__newt, - u3dc("scot", c3__uv, sam_u->sev_l), - u3_nul); - u3_noun cad = u3nc(c3__born, u3_nul); - - u3_auto_plan(car_u, u3_ovum_init(0, c3__a, wir, cad)); - } + // { + // // XX remove [sev_l] + // // + // u3_noun wir = u3nt(c3__newt, + // u3dc("scot", c3__uv, sam_u->sev_l), + // u3_nul); + // u3_noun cad = u3nc(c3__born, u3_nul); + + // u3_auto_plan(car_u, u3_ovum_init(0, c3__a, wir, cad)); + // } // scry the protocol version out of arvo // diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 0aebc28e58..ec657a2d0e 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1249,7 +1249,7 @@ _mesa_ef_send(u3_mesa* sam_u, u3_noun las, u3_noun pac) static c3_o _mesa_kick(u3_mesa* sam_u, u3_noun tag, u3_noun dat) { c3_o ret_o; - + u3l_log("blabla"); switch ( tag ) { default: { ret_o = c3n; @@ -1279,9 +1279,8 @@ static c3_o _mesa_io_kick(u3_auto* car_u, u3_noun wir, u3_noun cad) u3_noun tag, dat, i_wir; c3_o ret_o; - if ( (c3n == u3r_cell(wir, &i_wir, 0)) - || (c3__mesa != i_wir) + || (c3__ames != i_wir) || (c3n == u3r_cell(cad, &tag, &dat)) ) { ret_o = c3n; @@ -1853,7 +1852,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) } if ( 1 == pac_u->pag_u.dat_u.tot_w ) { - u3_noun wir = u3nc(c3__mesa, u3_nul); + u3_noun wir = u3nc(c3__ames, u3_nul); u3_noun aut, cad; switch ( pac_u->pag_u.dat_u.aum_u.typ_e ) { @@ -1896,7 +1895,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) // XX should put in cache on success u3_auto_plan(&sam_u->car_u, - u3_ovum_init(0, c3__mesa, wir, cad)); + u3_ovum_init(0, c3__ames, wir, cad)); _mesa_free_pict(pic_u); return; } @@ -1962,7 +1961,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) c3_w siz_w = (1 << (pac_u->pag_u.nam_u.boq_y - 3)); u3_noun dat = u3i_bytes((siz_w * req_u->tot_w), req_u->dat_y); - u3_noun wir = u3nc(c3__mesa, u3_nul); + u3_noun wir = u3nc(c3__ames, u3_nul); u3_noun cad; { // u3_noun pax = _mesa_encode_path(pac_u->pag_u.nam_u.pat_s, @@ -1997,7 +1996,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) _mesa_del_request(sam_u, &pac_u->pag_u.nam_u); u3_auto_plan(&sam_u->car_u, - u3_ovum_init(0, c3__mesa, wir, cad)); + u3_ovum_init(0, c3__ames, wir, cad)); } _mesa_free_pict(pic_u); @@ -2074,7 +2073,7 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) u3_noun our = u3i_chubs(2, sam_u->car_u.pir_u->who_d); u3_noun bem = u3nc(u3nt(our, u3_nul, u3nc(c3__ud, 1)), sky); // only branch where we do not free pic_u - u3_pier_peek(sam_u->car_u.pir_u, u3_nul, u3k(u3nq(1, c3__beam, c3__mx, bem)), pic_u, _mesa_page_scry_hunk_cb); + u3_pier_peek(sam_u->car_u.pir_u, u3_nul, u3k(u3nq(1, c3__beam, c3__ax, bem)), pic_u, _mesa_page_scry_hunk_cb); } else { // XX unpossible _mesa_free_pict(pic_u); @@ -2172,7 +2171,7 @@ _mesa_hear_poke(u3_mesa_pict* pic_u, u3_lane* lan_u) // XX create PIT entry for ack - u3_noun wir = u3nc(c3__mesa, u3_nul); + u3_noun wir = u3nc(c3__ames, u3_nul); u3_noun cad; { u3_noun lan = u3_mesa_encode_lane(*lan_u); @@ -2199,7 +2198,7 @@ _mesa_hear_poke(u3_mesa_pict* pic_u, u3_lane* lan_u) u3_auto_peer( u3_auto_plan(&sam_u->car_u, - u3_ovum_init(0, c3__mesa, wir, cad)), + u3_ovum_init(0, c3__ames, wir, cad)), ptr_v, nes_f, bal_f); } @@ -2289,12 +2288,12 @@ _mesa_io_talk(u3_auto* car_u) { // XX remove [sev_l] // - u3_noun wir = u3nt(c3__mesa, + u3_noun wir = u3nt(c3__ames, u3dc("scot", c3__uv, sam_u->sev_l), u3_nul); u3_noun cad = u3nc(c3__born, u3_nul); - u3_auto_plan(car_u, u3_ovum_init(0, c3__m, wir, cad)); + u3_auto_plan(car_u, u3_ovum_init(0, c3__a, wir, cad)); } u3_noun who = u3i_chubs(2, sam_u->pir_u->who_d); u3_noun rac = u3do("clan:title", u3k(who)); @@ -2412,7 +2411,7 @@ u3_mesa_io_init(u3_pier* pir_u) u3_auto* car_u = &sam_u->car_u; - car_u->nam_m = c3__mesa; + car_u->nam_m = c3__ames; car_u->liv_o = c3y; car_u->io.talk_f = _mesa_io_talk; car_u->io.info_f = _mesa_io_info; From e43d2101790208d23c82f679a8a590d4a4139028 Mon Sep 17 00:00:00 2001 From: Sigilante Date: Wed, 24 Apr 2024 10:51:09 -0500 Subject: [PATCH 108/430] Post with things moved and support for ARM. --- bazel/third_party/softblas/softblas.BUILD | 62 ++++++++ pkg/noun/jets/{f => i}/lagoon.c | 172 +++++++++++----------- pkg/noun/jets/q.h | 56 +++---- pkg/noun/jets/tree.c | 132 +++++++++-------- pkg/noun/jets/w.h | 58 ++++---- 5 files changed, 273 insertions(+), 207 deletions(-) rename pkg/noun/jets/{f => i}/lagoon.c (96%) diff --git a/bazel/third_party/softblas/softblas.BUILD b/bazel/third_party/softblas/softblas.BUILD index 3442c5da45..34c80c93e6 100644 --- a/bazel/third_party/softblas/softblas.BUILD +++ b/bazel/third_party/softblas/softblas.BUILD @@ -5,6 +5,68 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") cc_library( name = "softblas", visibility = ["//visibility:public"], + deps = select({ + "@platforms//cpu:aarch64": [":softblas_aarch64"], + "@platforms//cpu:x86_64": [":softblas_x86_64"], + "//conditions:default": [], + }), +) + +cc_library( + name = "softblas_aarch64", + visibility = ["//visibility:public"], + hdrs = ["include/softblas.h"], + includes = ["include"], + srcs = [ + "include/softblas.h", + "src/softblas_state.c", + "src/blas/level1/sasum.c", + "src/blas/level1/dasum.c", + "src/blas/level1/hasum.c", + "src/blas/level1/qasum.c", + "src/blas/level1/saxpy.c", + "src/blas/level1/daxpy.c", + "src/blas/level1/haxpy.c", + "src/blas/level1/qaxpy.c", + "src/blas/level1/scopy.c", + "src/blas/level1/dcopy.c", + "src/blas/level1/hcopy.c", + "src/blas/level1/qcopy.c", + "src/blas/level1/sdot.c", + "src/blas/level1/ddot.c", + "src/blas/level1/hdot.c", + "src/blas/level1/qdot.c", + "src/blas/level1/snrm2.c", + "src/blas/level1/dnrm2.c", + "src/blas/level1/hnrm2.c", + "src/blas/level1/qnrm2.c", + "src/blas/level1/sscal.c", + "src/blas/level1/dscal.c", + "src/blas/level1/hscal.c", + "src/blas/level1/qscal.c", + "src/blas/level1/sswap.c", + "src/blas/level1/dswap.c", + "src/blas/level1/hswap.c", + "src/blas/level1/qswap.c", + "src/blas/level1/isamax.c", + "src/blas/level1/idamax.c", + "src/blas/level1/ihamax.c", + "src/blas/level1/iqamax.c", + "src/blas/level2/sgemv.c", + "src/blas/level2/dgemv.c", + "src/blas/level2/hgemv.c", + "src/blas/level2/qgemv.c", + "src/blas/level3/sgemm.c", + "src/blas/level3/dgemm.c", + "src/blas/level3/hgemm.c", + "src/blas/level3/qgemm.c" + ], + deps = ["@softfloat"], +) + +cc_library( + name = "softblas_x86_64", + visibility = ["//visibility:public"], hdrs = ["include/softblas.h"], includes = ["include"], srcs = [ diff --git a/pkg/noun/jets/f/lagoon.c b/pkg/noun/jets/i/lagoon.c similarity index 96% rename from pkg/noun/jets/f/lagoon.c rename to pkg/noun/jets/i/lagoon.c index 8fd95be2ab..be9b8f6791 100644 --- a/pkg/noun/jets/f/lagoon.c +++ b/pkg/noun/jets/i/lagoon.c @@ -101,7 +101,7 @@ /* add - axpy = 1*x+y */ u3_noun - u3qf_la_add_real(u3_noun x_data, + u3qi_la_add_real(u3_noun x_data, u3_noun y_data, u3_noun shape, u3_noun bloq @@ -159,7 +159,7 @@ /* sub - axpy = -1*y+x */ u3_noun - u3qf_la_sub_real(u3_noun x_data, + u3qi_la_sub_real(u3_noun x_data, u3_noun y_data, u3_noun shape, u3_noun bloq @@ -219,7 +219,7 @@ elementwise multiplication */ u3_noun - u3qf_la_mul_real(u3_noun x_data, + u3qi_la_mul_real(u3_noun x_data, u3_noun y_data, u3_noun shape, u3_noun bloq) @@ -285,7 +285,7 @@ elementwise division */ u3_noun - u3qf_la_div_real(u3_noun x_data, + u3qi_la_div_real(u3_noun x_data, u3_noun y_data, u3_noun shape, u3_noun bloq) @@ -351,7 +351,7 @@ remainder after division */ u3_noun - u3qf_la_mod_real(u3_noun x_data, + u3qi_la_mod_real(u3_noun x_data, u3_noun y_data, u3_noun shape, u3_noun bloq) @@ -458,7 +458,7 @@ /* cumsum - x[0] + x[1] + ... x[n] */ u3_noun - u3qf_la_cumsum_real(u3_noun x_data, + u3qi_la_cumsum_real(u3_noun x_data, u3_noun shape, u3_noun bloq) { @@ -532,7 +532,7 @@ /* argmin - argmin(x) */ u3_noun - u3qf_la_argmin_real(u3_noun x_data, + u3qi_la_argmin_real(u3_noun x_data, u3_noun shape, u3_noun bloq) { @@ -605,7 +605,7 @@ /* argmax - argmax(x) */ u3_noun - u3qf_la_argmax_real(u3_noun x_data, + u3qi_la_argmax_real(u3_noun x_data, u3_noun shape, u3_noun bloq) { @@ -679,7 +679,7 @@ entire nd-array busted out as a linear list */ u3_noun - u3qf_la_ravel_real(u3_noun x_data, + u3qi_la_ravel_real(u3_noun x_data, u3_noun shape, u3_noun bloq) { @@ -742,7 +742,7 @@ /* min - min(x,y) */ u3_noun - u3qf_la_min_real(u3_noun x_data, + u3qi_la_min_real(u3_noun x_data, u3_noun shape, u3_noun bloq) { @@ -820,7 +820,7 @@ /* max - max(x,y) */ u3_noun - u3qf_la_max_real(u3_noun x_data, + u3qi_la_max_real(u3_noun x_data, u3_noun shape, u3_noun bloq) { @@ -898,7 +898,7 @@ /* abs - |x| */ u3_noun - u3qf_la_abs_real(u3_noun x_data, + u3qi_la_abs_real(u3_noun x_data, u3_noun shape, u3_noun bloq) { @@ -957,7 +957,7 @@ /* gth - x > y */ u3_noun - u3qf_la_gth_real(u3_noun x_data, + u3qi_la_gth_real(u3_noun x_data, u3_noun y_data, u3_noun shape, u3_noun bloq) @@ -1030,7 +1030,7 @@ /* gte - x > y */ u3_noun - u3qf_la_gte_real(u3_noun x_data, + u3qi_la_gte_real(u3_noun x_data, u3_noun y_data, u3_noun shape, u3_noun bloq) @@ -1103,7 +1103,7 @@ /* lth - x > y */ u3_noun - u3qf_la_lth_real(u3_noun x_data, + u3qi_la_lth_real(u3_noun x_data, u3_noun y_data, u3_noun shape, u3_noun bloq) @@ -1176,7 +1176,7 @@ /* lte - x > y */ u3_noun - u3qf_la_lte_real(u3_noun x_data, + u3qi_la_lte_real(u3_noun x_data, u3_noun y_data, u3_noun shape, u3_noun bloq) @@ -1249,7 +1249,7 @@ /* adds - axpy = 1*x+[n] */ u3_noun - u3qf_la_adds_real(u3_noun x_data, + u3qi_la_adds_real(u3_noun x_data, u3_noun n, u3_noun shape, u3_noun bloq) @@ -1331,7 +1331,7 @@ /* subs - axpy = -1*[n]+x */ u3_noun - u3qf_la_subs_real(u3_noun x_data, + u3qi_la_subs_real(u3_noun x_data, u3_noun n, u3_noun shape, u3_noun bloq) @@ -1414,7 +1414,7 @@ elementwise multiplication */ u3_noun - u3qf_la_muls_real(u3_noun x_data, + u3qi_la_muls_real(u3_noun x_data, u3_noun n, u3_noun shape, u3_noun bloq) @@ -1477,7 +1477,7 @@ elementwise division */ u3_noun - u3qf_la_divs_real(u3_noun x_data, + u3qi_la_divs_real(u3_noun x_data, u3_noun n, u3_noun shape, u3_noun bloq) @@ -1548,7 +1548,7 @@ remainder after scalar division */ u3_noun - u3qf_la_mods_real(u3_noun x_data, + u3qi_la_mods_real(u3_noun x_data, u3_noun n, u3_noun shape, u3_noun bloq) @@ -1664,7 +1664,7 @@ /* dot - ?dot = x · y */ u3_noun - u3qf_la_dot_real(u3_noun x_data, + u3qi_la_dot_real(u3_noun x_data, u3_noun y_data, u3_noun shape, u3_noun bloq) @@ -1732,7 +1732,7 @@ /* diag - diag(x) */ u3_noun - u3qf_la_diag(u3_noun x_data, + u3qi_la_diag(u3_noun x_data, u3_noun shape, u3_noun bloq) { @@ -1783,7 +1783,7 @@ /* transpose - x' */ u3_noun - u3qf_la_transpose(u3_noun x_data, + u3qi_la_transpose(u3_noun x_data, u3_noun shape, u3_noun bloq) { @@ -1828,7 +1828,7 @@ /* linspace - [a a+(b-a)/n ... b] */ u3_noun - u3qf_la_linspace_real(u3_noun a, + u3qi_la_linspace_real(u3_noun a, u3_noun b, u3_noun n, u3_noun bloq) @@ -1923,7 +1923,7 @@ /* range - [a a+d ... b] */ u3_noun - u3qf_la_range_real(u3_noun a, + u3qi_la_range_real(u3_noun a, u3_noun b, u3_noun d, u3_noun bloq) @@ -2014,20 +2014,20 @@ /* trace - tr(x) */ u3_noun - u3qf_la_trace_real(u3_noun x_data, + u3qi_la_trace_real(u3_noun x_data, u3_noun shape, u3_noun bloq) { - u3_noun d_data = u3qf_la_diag(x_data, shape, bloq); + u3_noun d_data = u3qi_la_diag(x_data, shape, bloq); c3_d len_x0 = _get_dims(shape)[0]; - u3_noun r_data = u3qf_la_dot_real(d_data, d_data, u3nt(len_x0, 0x1, u3_nul), u3k(bloq)); + u3_noun r_data = u3qi_la_dot_real(d_data, d_data, u3nt(len_x0, 0x1, u3_nul), u3k(bloq)); return r_data; } /* mmul */ u3_noun - u3qf_la_mmul_real(u3_noun x_data, + u3qi_la_mmul_real(u3_noun x_data, u3_noun y_data, u3_noun x_shape, u3_noun y_shape, @@ -2113,7 +2113,7 @@ } u3_noun - u3wf_la_add(u3_noun cor) + u3wi_la_add(u3_noun cor) { // Each argument is a ray, [=meta data=@ux] u3_noun x_meta, x_data, @@ -2157,7 +2157,7 @@ switch (x_kind) { case c3__real: _set_rounding(rnd); - u3_noun r_data = u3qf_la_add_real(x_data, y_data, x_shape, x_bloq); + u3_noun r_data = u3qi_la_add_real(x_data, y_data, x_shape, x_bloq); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2168,7 +2168,7 @@ } u3_noun - u3wf_la_sub(u3_noun cor) + u3wi_la_sub(u3_noun cor) { // Each argument is a ray, [=meta data=@ux] u3_noun x_meta, x_data, @@ -2212,7 +2212,7 @@ switch (x_kind) { case c3__real: _set_rounding(rnd); - u3_noun r_data = u3qf_la_sub_real(x_data, y_data, x_shape, x_bloq); + u3_noun r_data = u3qi_la_sub_real(x_data, y_data, x_shape, x_bloq); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2223,7 +2223,7 @@ } u3_noun - u3wf_la_mul(u3_noun cor) + u3wi_la_mul(u3_noun cor) { // Each argument is a ray, [=meta data=@ux] u3_noun x_meta, x_data, @@ -2267,7 +2267,7 @@ switch (x_kind) { case c3__real: _set_rounding(rnd); - u3_noun r_data = u3qf_la_mul_real(x_data, y_data, x_shape, x_bloq); + u3_noun r_data = u3qi_la_mul_real(x_data, y_data, x_shape, x_bloq); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2278,7 +2278,7 @@ } u3_noun - u3wf_la_div(u3_noun cor) + u3wi_la_div(u3_noun cor) { // Each argument is a ray, [=meta data=@ux] u3_noun x_meta, x_data, @@ -2322,7 +2322,7 @@ switch (x_kind) { case c3__real: _set_rounding(rnd); - u3_noun r_data = u3qf_la_div_real(x_data, y_data, x_shape, x_bloq); + u3_noun r_data = u3qi_la_div_real(x_data, y_data, x_shape, x_bloq); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2333,7 +2333,7 @@ } u3_noun - u3wf_la_mod(u3_noun cor) + u3wi_la_mod(u3_noun cor) { // Each argument is a ray, [=meta data=@ux] u3_noun x_meta, x_data, @@ -2377,7 +2377,7 @@ switch (x_kind) { case c3__real: _set_rounding(rnd); - u3_noun r_data = u3qf_la_mod_real(x_data, y_data, x_shape, x_bloq); + u3_noun r_data = u3qi_la_mod_real(x_data, y_data, x_shape, x_bloq); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2388,7 +2388,7 @@ } u3_noun - u3wf_la_cumsum(u3_noun cor) + u3wi_la_cumsum(u3_noun cor) { // Each argument is a ray, [=meta data=@ux] u3_noun x_meta, x_data; @@ -2417,7 +2417,7 @@ switch (x_kind) { case c3__real: _set_rounding(rnd); - u3_noun r_data = u3qf_la_cumsum_real(x_data, x_shape, x_bloq); + u3_noun r_data = u3qi_la_cumsum_real(x_data, x_shape, x_bloq); return u3nc(u3nq(u3nt(0x1, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2428,7 +2428,7 @@ } u3_noun - u3wf_la_argmin(u3_noun cor) + u3wi_la_argmin(u3_noun cor) { // Each argument is a ray, [=meta data=@ux] u3_noun x_meta, x_data; @@ -2454,7 +2454,7 @@ } else { switch (x_kind) { case c3__real: ; - u3_noun r_data = u3qf_la_argmin_real(x_data, x_shape, x_bloq); + u3_noun r_data = u3qi_la_argmin_real(x_data, x_shape, x_bloq); // bare atom (@ index) return r_data; @@ -2466,7 +2466,7 @@ } u3_noun - u3wf_la_ravel(u3_noun cor) + u3wi_la_ravel(u3_noun cor) { // Each argument is a ray, [=meta data=@ux] u3_noun x_meta, x_data; @@ -2491,7 +2491,7 @@ } else { switch (x_kind) { case c3__real: ; - u3_noun r_data = u3qf_la_ravel_real(x_data, x_shape, x_bloq); + u3_noun r_data = u3qi_la_ravel_real(x_data, x_shape, x_bloq); // (list @) return r_data; @@ -2503,7 +2503,7 @@ } u3_noun - u3wf_la_argmax(u3_noun cor) + u3wi_la_argmax(u3_noun cor) { // Each argument is a ray, [=meta data=@ux] u3_noun x_meta, x_data; @@ -2529,7 +2529,7 @@ } else { switch (x_kind) { case c3__real: ; - u3_noun r_data = u3qf_la_argmax_real(x_data, x_shape, x_bloq); + u3_noun r_data = u3qi_la_argmax_real(x_data, x_shape, x_bloq); // bare atom (@ index) return r_data; @@ -2541,7 +2541,7 @@ } u3_noun - u3wf_la_min(u3_noun cor) + u3wi_la_min(u3_noun cor) { // Each argument is a ray, [=meta data=@ux] u3_noun x_meta, x_data; @@ -2567,7 +2567,7 @@ } else { switch (x_kind) { case c3__real: ; - u3_noun r_data = u3qf_la_min_real(x_data, x_shape, x_bloq); + u3_noun r_data = u3qi_la_min_real(x_data, x_shape, x_bloq); return u3nc(u3nq(u3nt(0x1, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2578,7 +2578,7 @@ } u3_noun - u3wf_la_max(u3_noun cor) + u3wi_la_max(u3_noun cor) { // Each argument is a ray, [=meta data=@ux] u3_noun x_meta, x_data; @@ -2604,7 +2604,7 @@ } else { switch (x_kind) { case c3__real: ; - u3_noun r_data = u3qf_la_max_real(x_data, x_shape, x_bloq); + u3_noun r_data = u3qi_la_max_real(x_data, x_shape, x_bloq); return u3nc(u3nq(u3nt(0x1, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2615,7 +2615,7 @@ } u3_noun - u3wf_la_abs(u3_noun cor) + u3wi_la_abs(u3_noun cor) { // Each argument is a ray, [=meta data=@ux] u3_noun x_meta, x_data; @@ -2641,7 +2641,7 @@ } else { switch (x_kind) { case c3__real: ; - u3_noun r_data = u3qf_la_abs_real(x_data, x_shape, x_bloq); + u3_noun r_data = u3qi_la_abs_real(x_data, x_shape, x_bloq); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2652,7 +2652,7 @@ } u3_noun - u3wf_la_gth(u3_noun cor) + u3wi_la_gth(u3_noun cor) { // Each argument is a ray, [=meta data=@ux] u3_noun x_meta, x_data, @@ -2694,7 +2694,7 @@ } else { switch (x_kind) { case c3__real: ; - u3_noun r_data = u3qf_la_gth_real(x_data, y_data, x_shape, x_bloq); + u3_noun r_data = u3qi_la_gth_real(x_data, y_data, x_shape, x_bloq); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2705,7 +2705,7 @@ } u3_noun - u3wf_la_gte(u3_noun cor) + u3wi_la_gte(u3_noun cor) { // Each argument is a ray, [=meta data=@ux] u3_noun x_meta, x_data, @@ -2747,7 +2747,7 @@ } else { switch (x_kind) { case c3__real: ; - u3_noun r_data = u3qf_la_gte_real(x_data, y_data, x_shape, x_bloq); + u3_noun r_data = u3qi_la_gte_real(x_data, y_data, x_shape, x_bloq); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2758,7 +2758,7 @@ } u3_noun - u3wf_la_lth(u3_noun cor) + u3wi_la_lth(u3_noun cor) { // Each argument is a ray, [=meta data=@ux] u3_noun x_meta, x_data, @@ -2800,7 +2800,7 @@ } else { switch (x_kind) { case c3__real: ; - u3_noun r_data = u3qf_la_lth_real(x_data, y_data, x_shape, x_bloq); + u3_noun r_data = u3qi_la_lth_real(x_data, y_data, x_shape, x_bloq); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2811,7 +2811,7 @@ } u3_noun - u3wf_la_lte(u3_noun cor) + u3wi_la_lte(u3_noun cor) { // Each argument is a ray, [=meta data=@ux] u3_noun x_meta, x_data, @@ -2853,7 +2853,7 @@ } else { switch (x_kind) { case c3__real: ; - u3_noun r_data = u3qf_la_lte_real(x_data, y_data, x_shape, x_bloq); + u3_noun r_data = u3qi_la_lte_real(x_data, y_data, x_shape, x_bloq); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2864,7 +2864,7 @@ } u3_noun - u3wf_la_adds(u3_noun cor) + u3wi_la_adds(u3_noun cor) { // Each argument is a ray, [=meta data=@ux] u3_noun x_meta, x_data, n; @@ -2889,7 +2889,7 @@ switch (x_kind) { case c3__real: _set_rounding(rnd); - u3_noun r_data = u3qf_la_adds_real(x_data, n, x_shape, x_bloq); + u3_noun r_data = u3qi_la_adds_real(x_data, n, x_shape, x_bloq); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2899,7 +2899,7 @@ } u3_noun - u3wf_la_subs(u3_noun cor) + u3wi_la_subs(u3_noun cor) { // Each argument is a ray, [=meta data=@ux] u3_noun x_meta, x_data, n; @@ -2924,7 +2924,7 @@ switch (x_kind) { case c3__real: _set_rounding(rnd); - u3_noun r_data = u3qf_la_subs_real(x_data, n, x_shape, x_bloq); + u3_noun r_data = u3qi_la_subs_real(x_data, n, x_shape, x_bloq); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2934,7 +2934,7 @@ } u3_noun - u3wf_la_muls(u3_noun cor) + u3wi_la_muls(u3_noun cor) { // Each argument is a ray, [=meta data=@ux] u3_noun x_meta, x_data, n; @@ -2959,7 +2959,7 @@ switch (x_kind) { case c3__real: _set_rounding(rnd); - u3_noun r_data = u3qf_la_muls_real(x_data, n, x_shape, x_bloq); + u3_noun r_data = u3qi_la_muls_real(x_data, n, x_shape, x_bloq); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2969,7 +2969,7 @@ } u3_noun - u3wf_la_divs(u3_noun cor) + u3wi_la_divs(u3_noun cor) { // Each argument is a ray, [=meta data=@ux] u3_noun x_meta, x_data, n; @@ -2994,7 +2994,7 @@ switch (x_kind) { case c3__real: _set_rounding(rnd); - u3_noun r_data = u3qf_la_divs_real(x_data, n, x_shape, x_bloq); + u3_noun r_data = u3qi_la_divs_real(x_data, n, x_shape, x_bloq); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -3004,7 +3004,7 @@ } u3_noun - u3wf_la_mods(u3_noun cor) + u3wi_la_mods(u3_noun cor) { // Each argument is a ray, [=meta data=@ux] u3_noun x_meta, x_data, n; @@ -3029,7 +3029,7 @@ switch (x_kind) { case c3__real: _set_rounding(rnd); - u3_noun r_data = u3qf_la_mods_real(x_data, n, x_shape, x_bloq); + u3_noun r_data = u3qi_la_mods_real(x_data, n, x_shape, x_bloq); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -3039,7 +3039,7 @@ } u3_noun - u3wf_la_dot(u3_noun cor) + u3wi_la_dot(u3_noun cor) { // Each argument is a ray, [=meta data=@ux] u3_noun x_meta, x_data, @@ -3083,7 +3083,7 @@ switch (x_kind) { case c3__real: _set_rounding(rnd); - u3_noun r_data = u3qf_la_dot_real(x_data, y_data, x_shape, x_bloq); + u3_noun r_data = u3qi_la_dot_real(x_data, y_data, x_shape, x_bloq); c3_d len_x0 = _get_dims(x_shape)[0]; return u3nc(u3nq(u3nt(len_x0, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); @@ -3095,7 +3095,7 @@ } u3_noun - u3wf_la_transpose(u3_noun cor) + u3wi_la_transpose(u3_noun cor) { // Each argument is a ray, [=meta data=@ux] u3_noun x_meta, x_data; @@ -3119,14 +3119,14 @@ { return u3m_bail(c3__exit); } else { - u3_noun r_data = u3qf_la_transpose(x_data, x_shape, x_bloq); + u3_noun r_data = u3qi_la_transpose(x_data, x_shape, x_bloq); return u3nc(u3nq(u3nt(u3k(u3h(x_shape)), u3k(u3h(u3t(x_shape))), u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); } } } u3_noun - u3wf_la_linspace(u3_noun cor) + u3wi_la_linspace(u3_noun cor) { u3_noun x_meta, a, b, n, rnd; @@ -3154,7 +3154,7 @@ switch (x_kind) { case c3__real: _set_rounding(rnd); - u3_noun r_data = u3qf_la_linspace_real(a, b, n, x_bloq); + u3_noun r_data = u3qi_la_linspace_real(a, b, n, x_bloq); x_shape = u3nt(u3x_atom(n), 0x1, u3_nul); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); @@ -3166,7 +3166,7 @@ } u3_noun - u3wf_la_range(u3_noun cor) + u3wi_la_range(u3_noun cor) { u3_noun x_meta, a, b, d, rnd; @@ -3194,7 +3194,7 @@ switch (x_kind) { case c3__real: _set_rounding(rnd); - u3_noun r_data = u3qf_la_range_real(a, b, d, x_bloq); + u3_noun r_data = u3qi_la_range_real(a, b, d, x_bloq); c3_d a_, b_, d_; c3_ds n_; switch (x_bloq) { @@ -3235,7 +3235,7 @@ } u3_noun - u3wf_la_diag(u3_noun cor) + u3wi_la_diag(u3_noun cor) { // Each argument is a ray, [=meta data=@ux] u3_noun x_meta, x_data; @@ -3259,7 +3259,7 @@ { return u3m_bail(c3__exit); } else { - u3_noun r_data = u3qf_la_diag(x_data, x_shape, x_bloq); + u3_noun r_data = u3qi_la_diag(x_data, x_shape, x_bloq); c3_d len_x0 = _get_dims(x_shape)[0]; return u3nc(u3nq(u3nt(len_x0, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); } @@ -3267,7 +3267,7 @@ } u3_noun - u3wf_la_trace(u3_noun cor) + u3wi_la_trace(u3_noun cor) { // Each argument is a ray, [=meta data=@ux] u3_noun x_meta, x_data; @@ -3293,7 +3293,7 @@ } else { switch (x_kind) { case c3__real: ; - u3_noun r_data = u3qf_la_trace_real(x_data, x_shape, x_bloq); + u3_noun r_data = u3qi_la_trace_real(x_data, x_shape, x_bloq); return u3nc(u3nq(u3nt(0x1, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -3304,7 +3304,7 @@ } u3_noun - u3wf_la_mmul(u3_noun cor) + u3wi_la_mmul(u3_noun cor) { // Each argument is a ray, [=meta data=@ux] u3_noun x_meta, x_data, @@ -3343,7 +3343,7 @@ switch (x_kind) { case c3__real: _set_rounding(rnd); - u3_noun r_data = u3qf_la_mmul_real(x_data, y_data, x_shape, y_shape, x_bloq); + u3_noun r_data = u3qi_la_mmul_real(x_data, y_data, x_shape, y_shape, x_bloq); // result is already [meta data] return r_data; diff --git a/pkg/noun/jets/q.h b/pkg/noun/jets/q.h index b5e5cbc8e1..14a4cac1e3 100644 --- a/pkg/noun/jets/q.h +++ b/pkg/noun/jets/q.h @@ -247,34 +247,34 @@ u3_noun u3qfp_nepo(u3_noun, u3_noun); u3_noun u3qfp_rake(u3_noun); - u3_noun u3qf_la_add_real(u3_noun, u3_noun, u3_noun, u3_noun); - u3_noun u3qf_la_sub_real(u3_noun, u3_noun, u3_noun, u3_noun); - u3_noun u3qf_la_mul_real(u3_noun, u3_noun, u3_noun, u3_noun); - u3_noun u3qf_la_div_real(u3_noun, u3_noun, u3_noun, u3_noun); - u3_noun u3qf_la_mod_real(u3_noun, u3_noun, u3_noun, u3_noun); - u3_noun u3qf_la_adds_real(u3_noun, u3_noun, u3_noun, u3_noun); - u3_noun u3qf_la_subs_real(u3_noun, u3_noun, u3_noun, u3_noun); - u3_noun u3qf_la_muls_real(u3_noun, u3_noun, u3_noun, u3_noun); - u3_noun u3qf_la_divs_real(u3_noun, u3_noun, u3_noun, u3_noun); - u3_noun u3qf_la_mods_real(u3_noun, u3_noun, u3_noun, u3_noun); - u3_noun u3qf_la_dot_real(u3_noun, u3_noun, u3_noun, u3_noun); - u3_noun u3qf_la_diag(u3_noun, u3_noun, u3_noun); - u3_noun u3qf_la_transpose(u3_noun, u3_noun, u3_noun); - u3_noun u3qf_la_cumsum_real(u3_noun, u3_noun, u3_noun); - u3_noun u3qf_la_argmin_real(u3_noun, u3_noun, u3_noun); - u3_noun u3qf_la_argmax_real(u3_noun, u3_noun, u3_noun); - u3_noun u3qf_la_ravel_real(u3_noun, u3_noun, u3_noun); - u3_noun u3qf_la_min_real(u3_noun, u3_noun, u3_noun); - u3_noun u3qf_la_max_real(u3_noun, u3_noun, u3_noun); - u3_noun u3qf_la_linspace_real(u3_noun, u3_noun, u3_noun, u3_noun); - u3_noun u3qf_la_range_real(u3_noun, u3_noun, u3_noun, u3_noun); - u3_noun u3qf_la_abs_real(u3_noun, u3_noun, u3_noun); - u3_noun u3qf_la_gth_real(u3_noun, u3_noun, u3_noun, u3_noun); - u3_noun u3qf_la_gte_real(u3_noun, u3_noun, u3_noun, u3_noun); - u3_noun u3qf_la_lth_real(u3_noun, u3_noun, u3_noun, u3_noun); - u3_noun u3qf_la_lte_real(u3_noun, u3_noun, u3_noun, u3_noun); - u3_noun u3qf_la_trace_real(u3_noun, u3_noun, u3_noun); - u3_noun u3qf_la_mmul_real(u3_noun, u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_add_real(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_sub_real(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_mul_real(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_div_real(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_mod_real(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_adds_real(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_subs_real(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_muls_real(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_divs_real(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_mods_real(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_dot_real(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_diag(u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_transpose(u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_cumsum_real(u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_argmin_real(u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_argmax_real(u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_ravel_real(u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_min_real(u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_max_real(u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_linspace_real(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_range_real(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_abs_real(u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_gth_real(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_gte_real(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_lth_real(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_lte_real(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_trace_real(u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_mmul_real(u3_noun, u3_noun, u3_noun, u3_noun, u3_noun); # define u3qfu_van_fan 28 # define u3qfu_van_rib 58 diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index 35573a42e6..c9118c9a4b 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -2147,77 +2147,82 @@ static u3j_core _139_hex_json_d[] = {} }; -/* linear algebra jets - XX move to outer _hep_ core for /lib? eventually -static u3j_core _139_hep_d[] = +/* /lib jets in non core */ -static u3j_harm _139_hex__lagoon_add_a[] = {{".2", u3wf_la_add}, {}}; -static u3j_harm _139_hex__lagoon_sub_a[] = {{".2", u3wf_la_sub}, {}}; -static u3j_harm _139_hex__lagoon_mul_a[] = {{".2", u3wf_la_mul}, {}}; -static u3j_harm _139_hex__lagoon_div_a[] = {{".2", u3wf_la_div}, {}}; -static u3j_harm _139_hex__lagoon_mod_a[] = {{".2", u3wf_la_mod}, {}}; -static u3j_harm _139_hex__lagoon_adds_a[] = {{".2", u3wf_la_adds}, {}}; -static u3j_harm _139_hex__lagoon_subs_a[] = {{".2", u3wf_la_subs}, {}}; -static u3j_harm _139_hex__lagoon_muls_a[] = {{".2", u3wf_la_muls}, {}}; -static u3j_harm _139_hex__lagoon_divs_a[] = {{".2", u3wf_la_divs}, {}}; -static u3j_harm _139_hex__lagoon_mods_a[] = {{".2", u3wf_la_mods}, {}}; -static u3j_harm _139_hex__lagoon_dot_a[] = {{".2", u3wf_la_dot}, {}}; -static u3j_harm _139_hex__lagoon_trans_a[] ={{".2", u3wf_la_transpose}, {}}; -static u3j_harm _139_hex__lagoon_cumsum_a[]={{".2", u3wf_la_cumsum}, {}}; -static u3j_harm _139_hex__lagoon_argmin_a[]={{".2", u3wf_la_argmin}, {}}; -static u3j_harm _139_hex__lagoon_argmax_a[]={{".2", u3wf_la_argmax}, {}}; -static u3j_harm _139_hex__lagoon_ravel_a[]={{".2", u3wf_la_ravel}, {}}; -static u3j_harm _139_hex__lagoon_min_a[] = {{".2", u3wf_la_min}, {}}; -static u3j_harm _139_hex__lagoon_max_a[] = {{".2", u3wf_la_max}, {}}; -static u3j_harm _139_hex__lagoon_linspace_a[]={{".2", u3wf_la_linspace}, {}}; -static u3j_harm _139_hex__lagoon_range_a[]= {{".2", u3wf_la_range}, {}}; -static u3j_harm _139_hex__lagoon_abs_a[] = {{".2", u3wf_la_abs}, {}}; -static u3j_harm _139_hex__lagoon_gth_a[] = {{".2", u3wf_la_gth}, {}}; -static u3j_harm _139_hex__lagoon_gte_a[] = {{".2", u3wf_la_gte}, {}}; -static u3j_harm _139_hex__lagoon_lth_a[] = {{".2", u3wf_la_lth}, {}}; -static u3j_harm _139_hex__lagoon_lte_a[] = {{".2", u3wf_la_lte}, {}}; -static u3j_harm _139_hex__lagoon_diag_a[] = {{".2", u3wf_la_diag}, {}}; -static u3j_harm _139_hex__lagoon_trace_a[]= {{".2", u3wf_la_trace}, {}}; -static u3j_harm _139_hex__lagoon_mmul_a[] = {{".2", u3wf_la_mmul}, {}}; -static u3j_core _139_hex__la_core_d[] = - { { "add-rays", 7, _139_hex__lagoon_add_a, 0, no_hashes }, - { "sub-rays", 7, _139_hex__lagoon_sub_a, 0, no_hashes }, - { "mul-rays", 7, _139_hex__lagoon_mul_a, 0, no_hashes }, - { "div-rays", 7, _139_hex__lagoon_div_a, 0, no_hashes }, - { "mod-rays", 7, _139_hex__lagoon_mod_a, 0, no_hashes }, - { "add-scal", 7, _139_hex__lagoon_adds_a, 0, no_hashes }, - { "sub-scal", 7, _139_hex__lagoon_subs_a, 0, no_hashes }, - { "mul-scal", 7, _139_hex__lagoon_muls_a, 0, no_hashes }, - { "div-scal", 7, _139_hex__lagoon_divs_a, 0, no_hashes }, - { "mod-scal", 7, _139_hex__lagoon_mods_a, 0, no_hashes }, - { "dot", 7, _139_hex__lagoon_dot_a, 0, no_hashes }, - { "transpose",7, _139_hex__lagoon_trans_a, 0, no_hashes }, - { "cumsum", 7, _139_hex__lagoon_cumsum_a, 0, no_hashes }, - { "argmin", 7, _139_hex__lagoon_argmin_a, 0, no_hashes }, - { "argmax", 7, _139_hex__lagoon_argmax_a, 0, no_hashes }, - { "ravel", 7, _139_hex__lagoon_ravel_a, 0, no_hashes }, - { "min", 7, _139_hex__lagoon_min_a, 0, no_hashes }, - { "max", 7, _139_hex__lagoon_max_a, 0, no_hashes }, - { "linspace", 7, _139_hex__lagoon_linspace_a, 0, no_hashes }, - { "range", 7, _139_hex__lagoon_range_a, 0, no_hashes }, - { "abs", 7, _139_hex__lagoon_abs_a, 0, no_hashes }, - { "gth", 7, _139_hex__lagoon_gth_a, 0, no_hashes }, - { "gte", 7, _139_hex__lagoon_gte_a, 0, no_hashes }, - { "lth", 7, _139_hex__lagoon_lth_a, 0, no_hashes }, - { "lte", 7, _139_hex__lagoon_lte_a, 0, no_hashes }, - { "diag", 7, _139_hex__lagoon_diag_a, 0, no_hashes }, - { "trace", 7, _139_hex__lagoon_trace_a,0, no_hashes }, - { "mmul", 7, _139_hex__lagoon_mmul_a, 0, no_hashes }, +static u3j_harm _139_non__lagoon_add_a[] = {{".2", u3wi_la_add, c3n}, {}}; +static u3j_harm _139_non__lagoon_sub_a[] = {{".2", u3wi_la_sub, c3n}, {}}; +static u3j_harm _139_non__lagoon_mul_a[] = {{".2", u3wi_la_mul, c3n}, {}}; +static u3j_harm _139_non__lagoon_div_a[] = {{".2", u3wi_la_div, c3n}, {}}; +static u3j_harm _139_non__lagoon_mod_a[] = {{".2", u3wi_la_mod, c3n}, {}}; +static u3j_harm _139_non__lagoon_adds_a[] = {{".2", u3wi_la_adds, c3n}, {}}; +static u3j_harm _139_non__lagoon_subs_a[] = {{".2", u3wi_la_subs, c3n}, {}}; +static u3j_harm _139_non__lagoon_muls_a[] = {{".2", u3wi_la_muls, c3n}, {}}; +static u3j_harm _139_non__lagoon_divs_a[] = {{".2", u3wi_la_divs, c3n}, {}}; +static u3j_harm _139_non__lagoon_mods_a[] = {{".2", u3wi_la_mods, c3n}, {}}; +static u3j_harm _139_non__lagoon_dot_a[] = {{".2", u3wi_la_dot, c3n}, {}}; +static u3j_harm _139_non__lagoon_trans_a[] ={{".2", u3wi_la_transpose, c3n}, {}}; +static u3j_harm _139_non__lagoon_cumsum_a[]={{".2", u3wi_la_cumsum, c3n}, {}}; +static u3j_harm _139_non__lagoon_argmin_a[]={{".2", u3wi_la_argmin, c3n}, {}}; +static u3j_harm _139_non__lagoon_argmax_a[]={{".2", u3wi_la_argmax, c3n}, {}}; +static u3j_harm _139_non__lagoon_ravel_a[]={{".2", u3wi_la_ravel, c3n}, {}}; +static u3j_harm _139_non__lagoon_min_a[] = {{".2", u3wi_la_min, c3n}, {}}; +static u3j_harm _139_non__lagoon_max_a[] = {{".2", u3wi_la_max, c3n}, {}}; +static u3j_harm _139_non__lagoon_linspace_a[]={{".2", u3wi_la_linspace, c3n}, {}}; +static u3j_harm _139_non__lagoon_range_a[]= {{".2", u3wi_la_range, c3n}, {}}; +static u3j_harm _139_non__lagoon_abs_a[] = {{".2", u3wi_la_abs, c3n}, {}}; +static u3j_harm _139_non__lagoon_gth_a[] = {{".2", u3wi_la_gth, c3n}, {}}; +static u3j_harm _139_non__lagoon_gte_a[] = {{".2", u3wi_la_gte, c3n}, {}}; +static u3j_harm _139_non__lagoon_lth_a[] = {{".2", u3wi_la_lth, c3n}, {}}; +static u3j_harm _139_non__lagoon_lte_a[] = {{".2", u3wi_la_lte, c3n}, {}}; +static u3j_harm _139_non__lagoon_diag_a[] = {{".2", u3wi_la_diag, c3n}, {}}; +static u3j_harm _139_non__lagoon_trace_a[]= {{".2", u3wi_la_trace, c3n}, {}}; +static u3j_harm _139_non__lagoon_mmul_a[] = {{".2", u3wi_la_mmul, c3n}, {}}; +static u3j_core _139_non__la_core_d[] = + { { "add-rays", 7, _139_non__lagoon_add_a, 0, no_hashes }, + { "sub-rays", 7, _139_non__lagoon_sub_a, 0, no_hashes }, + { "mul-rays", 7, _139_non__lagoon_mul_a, 0, no_hashes }, + { "div-rays", 7, _139_non__lagoon_div_a, 0, no_hashes }, + { "mod-rays", 7, _139_non__lagoon_mod_a, 0, no_hashes }, + { "add-scal", 7, _139_non__lagoon_adds_a, 0, no_hashes }, + { "sub-scal", 7, _139_non__lagoon_subs_a, 0, no_hashes }, + { "mul-scal", 7, _139_non__lagoon_muls_a, 0, no_hashes }, + { "div-scal", 7, _139_non__lagoon_divs_a, 0, no_hashes }, + { "mod-scal", 7, _139_non__lagoon_mods_a, 0, no_hashes }, + { "dot", 7, _139_non__lagoon_dot_a, 0, no_hashes }, + { "transpose",7, _139_non__lagoon_trans_a, 0, no_hashes }, + { "cumsum", 7, _139_non__lagoon_cumsum_a, 0, no_hashes }, + { "argmin", 7, _139_non__lagoon_argmin_a, 0, no_hashes }, + { "argmax", 7, _139_non__lagoon_argmax_a, 0, no_hashes }, + { "ravel", 7, _139_non__lagoon_ravel_a, 0, no_hashes }, + { "min", 7, _139_non__lagoon_min_a, 0, no_hashes }, + { "max", 7, _139_non__lagoon_max_a, 0, no_hashes }, + { "linspace", 7, _139_non__lagoon_linspace_a, 0, no_hashes }, + { "range", 7, _139_non__lagoon_range_a, 0, no_hashes }, + { "abs", 7, _139_non__lagoon_abs_a, 0, no_hashes }, + { "gth", 7, _139_non__lagoon_gth_a, 0, no_hashes }, + { "gte", 7, _139_non__lagoon_gte_a, 0, no_hashes }, + { "lth", 7, _139_non__lagoon_lth_a, 0, no_hashes }, + { "lte", 7, _139_non__lagoon_lte_a, 0, no_hashes }, + { "diag", 7, _139_non__lagoon_diag_a, 0, no_hashes }, + { "trace", 7, _139_non__lagoon_trace_a,0, no_hashes }, + { "mmul", 7, _139_non__lagoon_mmul_a, 0, no_hashes }, {} }; -static u3j_core _139_hex__lagoon_d[] = - { { "la-core", 7, 0, _139_hex__la_core_d, no_hashes }, +static u3j_core _139_non__lagoon_d[] = + { { "la-core", 7, 0, _139_non__la_core_d, no_hashes }, + {} + }; + +static u3j_core _139_non_d[] = + { { "lagoon", 6, 0, _139_non__lagoon_d, no_hashes }, {} }; static u3j_core _139_hex_d[] = -{ { "lore", 63, _140_hex_lore_a, 0, no_hashes }, +{ { "sep", 7, 0, _139_non_d, no_hashes }, + + { "lore", 63, _140_hex_lore_a, 0, no_hashes }, { "leer", 63, _140_hex_leer_a, 0, no_hashes }, { "loss", 63, _140_hex_loss_a, 0, no_hashes }, { "lune", 127, _140_hex_lune_a, 0, no_hashes }, @@ -2235,7 +2240,6 @@ static u3j_core _139_hex_d[] = { "mimes", 31, 0, _140_hex_mimes_d, no_hashes }, { "json", 31, 0, _139_hex_json_d, no_hashes }, - { "lagoon", 31, 0, _139_hex__lagoon_d, no_hashes }, {} }; diff --git a/pkg/noun/jets/w.h b/pkg/noun/jets/w.h index e7976ba3b7..01948a851e 100644 --- a/pkg/noun/jets/w.h +++ b/pkg/noun/jets/w.h @@ -331,34 +331,34 @@ u3_noun u3wfu_repo(u3_noun); u3_noun u3wfu_rest(u3_noun); - u3_noun u3wf_la_add(u3_noun); - u3_noun u3wf_la_sub(u3_noun); - u3_noun u3wf_la_mul(u3_noun); - u3_noun u3wf_la_div(u3_noun); - u3_noun u3wf_la_mod(u3_noun); - u3_noun u3wf_la_adds(u3_noun); - u3_noun u3wf_la_subs(u3_noun); - u3_noun u3wf_la_muls(u3_noun); - u3_noun u3wf_la_divs(u3_noun); - u3_noun u3wf_la_mods(u3_noun); - u3_noun u3wf_la_dot(u3_noun); - u3_noun u3wf_la_diag(u3_noun); - u3_noun u3wf_la_transpose(u3_noun); - u3_noun u3wf_la_cumsum(u3_noun); - u3_noun u3wf_la_argmin(u3_noun); - u3_noun u3wf_la_argmax(u3_noun); - u3_noun u3wf_la_ravel(u3_noun); - u3_noun u3wf_la_min(u3_noun); - u3_noun u3wf_la_max(u3_noun); - u3_noun u3wf_la_linspace(u3_noun); - u3_noun u3wf_la_range(u3_noun); - u3_noun u3wf_la_abs(u3_noun); - u3_noun u3wf_la_gth(u3_noun); - u3_noun u3wf_la_gte(u3_noun); - u3_noun u3wf_la_lth(u3_noun); - u3_noun u3wf_la_lte(u3_noun); - - u3_noun u3wf_la_trace(u3_noun); - u3_noun u3wf_la_mmul(u3_noun); + u3_noun u3wi_la_add(u3_noun); + u3_noun u3wi_la_sub(u3_noun); + u3_noun u3wi_la_mul(u3_noun); + u3_noun u3wi_la_div(u3_noun); + u3_noun u3wi_la_mod(u3_noun); + u3_noun u3wi_la_adds(u3_noun); + u3_noun u3wi_la_subs(u3_noun); + u3_noun u3wi_la_muls(u3_noun); + u3_noun u3wi_la_divs(u3_noun); + u3_noun u3wi_la_mods(u3_noun); + u3_noun u3wi_la_dot(u3_noun); + u3_noun u3wi_la_diag(u3_noun); + u3_noun u3wi_la_transpose(u3_noun); + u3_noun u3wi_la_cumsum(u3_noun); + u3_noun u3wi_la_argmin(u3_noun); + u3_noun u3wi_la_argmax(u3_noun); + u3_noun u3wi_la_ravel(u3_noun); + u3_noun u3wi_la_min(u3_noun); + u3_noun u3wi_la_max(u3_noun); + u3_noun u3wi_la_linspace(u3_noun); + u3_noun u3wi_la_range(u3_noun); + u3_noun u3wi_la_abs(u3_noun); + u3_noun u3wi_la_gth(u3_noun); + u3_noun u3wi_la_gte(u3_noun); + u3_noun u3wi_la_lth(u3_noun); + u3_noun u3wi_la_lte(u3_noun); + + u3_noun u3wi_la_trace(u3_noun); + u3_noun u3wi_la_mmul(u3_noun); #endif /* ifndef U3_JETS_W_H */ From fe485ea43e4106840c90c3692ffb30947302e98c Mon Sep 17 00:00:00 2001 From: Sigilante Date: Wed, 24 Apr 2024 10:57:47 -0500 Subject: [PATCH 109/430] Attempt ARM build with same files as x86_64. --- bazel/third_party/softfloat/softfloat.BUILD | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/bazel/third_party/softfloat/softfloat.BUILD b/bazel/third_party/softfloat/softfloat.BUILD index 0d57ba5d7f..09f7b21370 100644 --- a/bazel/third_party/softfloat/softfloat.BUILD +++ b/bazel/third_party/softfloat/softfloat.BUILD @@ -235,6 +235,25 @@ cc_library( "source/extF80M_eq_signaling.c", "source/extF80M_le_quiet.c", "source/extF80M_lt_quiet.c", + "source/f128_to_f16.c", + "source/f128_to_f32.c", + "source/f128_to_extF80.c", + "source/f128_to_f64.c", + "source/f128_roundToInt.c", + "source/f128_add.c", + "source/f128_sub.c", + "source/f128_mul.c", + "source/f128_mulAdd.c", + "source/f128_div.c", + "source/f128_rem.c", + "source/f128_sqrt.c", + "source/f128_eq.c", + "source/f128_le.c", + "source/f128_lt.c", + "source/f128_eq_signaling.c", + "source/f128_le_quiet.c", + "source/f128_lt_quiet.c", + "source/f128_isSignalingNaN.c", "source/f128M_to_ui32.c", "source/f128M_to_ui64.c", "source/f128M_to_i32.c", From bf13e281cdc815bcfe5bfef9538f3a6104435efb Mon Sep 17 00:00:00 2001 From: Sigilante Date: Wed, 24 Apr 2024 11:04:24 -0500 Subject: [PATCH 110/430] Attempt ARM build with same files as x86_64. --- bazel/third_party/softfloat/softfloat.BUILD | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bazel/third_party/softfloat/softfloat.BUILD b/bazel/third_party/softfloat/softfloat.BUILD index 09f7b21370..cf01a9dcf2 100644 --- a/bazel/third_party/softfloat/softfloat.BUILD +++ b/bazel/third_party/softfloat/softfloat.BUILD @@ -20,6 +20,9 @@ cc_library( srcs = [ # See `OBJS_PRIMITIVES` in `build/Linux-ARM-VFPv2-GCC/Makefile` in the # `softfloat` repo. + "source/s_eq128.c", + "source/s_le128.c", + "source/s_lt128.c", "source/s_compare96M.c", "source/s_compare128M.c", "source/s_shortShiftLeft64To96M.c", From 4832fbe06cf1c7201090f7b5fe28b3d763b5f4a5 Mon Sep 17 00:00:00 2001 From: Sigilante Date: Wed, 24 Apr 2024 11:46:51 -0500 Subject: [PATCH 111/430] Swap out SoftFloat fns. --- bazel/third_party/softfloat/softfloat.BUILD | 22 ----------------- pkg/noun/jets/i/lagoon.c | 26 +++++++++++++-------- 2 files changed, 16 insertions(+), 32 deletions(-) diff --git a/bazel/third_party/softfloat/softfloat.BUILD b/bazel/third_party/softfloat/softfloat.BUILD index cf01a9dcf2..0d57ba5d7f 100644 --- a/bazel/third_party/softfloat/softfloat.BUILD +++ b/bazel/third_party/softfloat/softfloat.BUILD @@ -20,9 +20,6 @@ cc_library( srcs = [ # See `OBJS_PRIMITIVES` in `build/Linux-ARM-VFPv2-GCC/Makefile` in the # `softfloat` repo. - "source/s_eq128.c", - "source/s_le128.c", - "source/s_lt128.c", "source/s_compare96M.c", "source/s_compare128M.c", "source/s_shortShiftLeft64To96M.c", @@ -238,25 +235,6 @@ cc_library( "source/extF80M_eq_signaling.c", "source/extF80M_le_quiet.c", "source/extF80M_lt_quiet.c", - "source/f128_to_f16.c", - "source/f128_to_f32.c", - "source/f128_to_extF80.c", - "source/f128_to_f64.c", - "source/f128_roundToInt.c", - "source/f128_add.c", - "source/f128_sub.c", - "source/f128_mul.c", - "source/f128_mulAdd.c", - "source/f128_div.c", - "source/f128_rem.c", - "source/f128_sqrt.c", - "source/f128_eq.c", - "source/f128_le.c", - "source/f128_lt.c", - "source/f128_eq_signaling.c", - "source/f128_le_quiet.c", - "source/f128_lt_quiet.c", - "source/f128_isSignalingNaN.c", "source/f128M_to_ui32.c", "source/f128M_to_ui64.c", "source/f128M_to_i32.c", diff --git a/pkg/noun/jets/i/lagoon.c b/pkg/noun/jets/i/lagoon.c index be9b8f6791..f0906cd20a 100644 --- a/pkg/noun/jets/i/lagoon.c +++ b/pkg/noun/jets/i/lagoon.c @@ -385,7 +385,7 @@ // Perform division x/n float16_t div_result16 = f16_div(x_val16, y_val16); // Compute floor of the division result - int64_t floor_result16 = f16_to_i64(div_result16, softfloat_round_minMag, false); + c3_ds floor_result16 = f16_to_i64(div_result16, softfloat_round_minMag, false); float16_t floor_float16 = i64_to_f16(floor_result16); // Multiply n by floor(x/n) float16_t mult_result16 = f16_mul(y_val16, floor_float16); @@ -401,7 +401,7 @@ // Perform division x/n float32_t div_result32 = f32_div(x_val32, y_val32); // Compute floor of the division result - int64_t floor_result32 = f32_to_i64(div_result32, softfloat_round_minMag, false); + c3_ds floor_result32 = f32_to_i64(div_result32, softfloat_round_minMag, false); float32_t floor_float32 = i64_to_f32(floor_result32); // Multiply n by floor(x/n) float32_t mult_result32 = f32_mul(y_val32, floor_float32); @@ -417,7 +417,7 @@ // Perform division x/n float64_t div_result64 = f64_div(x_val64, y_val64); // Compute floor of the division result - int64_t floor_result64 = f64_to_i64(div_result64, softfloat_round_minMag, false); + c3_ds floor_result64 = f64_to_i64(div_result64, softfloat_round_minMag, false); float64_t floor_float64 = i64_to_f64(floor_result64); // Multiply n by floor(x/n) float64_t mult_result64 = f64_mul(y_val64, floor_float64); @@ -434,7 +434,7 @@ float128_t div_result128; f128M_div((float128_t*)&x_val128, (float128_t*)&y_val128, (float128_t*)&div_result128); // Compute floor of the division result - int64_t floor_result128 = f128_to_i64(div_result128, softfloat_round_minMag, false); + c3_ds floor_result128 = f128M_to_i64(&div_result128, softfloat_round_minMag, false); float128_t floor_float128 = i64_to_f128(floor_result128); // Multiply n by floor(x/n) float128_t mult_result128; @@ -1586,7 +1586,7 @@ // Perform division x/n float16_t div_result16 = f16_mul(in16, x_val16); // Compute floor of the division result - int64_t floor_result16 = f16_to_i64(div_result16, softfloat_round_minMag, false); + c3_ds floor_result16 = f16_to_i64(div_result16, softfloat_round_minMag, false); float16_t floor_float16 = i64_to_f16(floor_result16); // Multiply n by floor(x/n) float16_t mult_result16 = f16_mul(n16, floor_float16); @@ -1604,7 +1604,7 @@ // Perform division x/n float32_t div_result32 = f32_mul((float32_t)in32, (float32_t)x_val32); // Compute floor of the division result - int64_t floor_result32 = f32_to_i64(div_result32, softfloat_round_minMag, false); + c3_ds floor_result32 = f32_to_i64(div_result32, softfloat_round_minMag, false); float32_t floor_float32 = i64_to_f32(floor_result32); // Multiply n by floor(x/n) float32_t mult_result32 = f32_mul(n32, floor_float32); @@ -1622,7 +1622,7 @@ // Perform division x/n float64_t div_result64 = f64_mul(in64, x_val64); // Compute floor of the division result - int64_t floor_result64 = f64_to_i64(div_result64, softfloat_round_minMag, false); + c3_ds floor_result64 = f64_to_i64(div_result64, softfloat_round_minMag, false); float64_t floor_float64 = i64_to_f64(floor_result64); // Multiply n by floor(x/n) float64_t mult_result64 = f64_mul(n64, floor_float64); @@ -1641,7 +1641,7 @@ float128_t div_result128; f128M_mul((float128_t*)&in128, (float128_t*)&x_val128, (float128_t*)&div_result128); // Compute floor of the division result - int64_t floor_result128 = f128_to_i64(div_result128, softfloat_round_minMag, false); + c3_ds floor_result128 = f128M_to_i64(&div_result128, softfloat_round_minMag, false); float128_t floor_float128 = i64_to_f128(floor_result128); // Multiply n by floor(x/n) float128_t mult_result128; @@ -1992,7 +1992,10 @@ u3r_bytes(0, 16, (c3_y*)&(a128.v[0]), a); u3r_bytes(0, 16, (c3_y*)&(b128.v[0]), b); u3r_bytes(0, 16, (c3_y*)&(interval128.v), d); - c3_d n128 = f128_to_i64(f128_div(f128_sub(b128, a128), interval128), softfloat_round_minMag, false); + float128_t tmp; + f128M_sub(&b128, &a128, &tmp); + f128M_div(&tmp, &interval128, &interval128); + c3_d n128 = f128M_to_i64(&tmp, softfloat_round_minMag, false); c3_y* x_bytes128 = (c3_y*)u3a_malloc(((n128+1)*16+1)*sizeof(c3_y)); float128_t i128; for (c3_d i = 1; i < n128; i++) { @@ -3220,7 +3223,10 @@ u3r_bytes(0, 16, (c3_y*)&a_, a); u3r_bytes(0, 16, (c3_y*)&b_, b); u3r_bytes(0, 16, (c3_y*)&d_, d); - n_ = f128_to_i64(f128_div(f128_sub((float128_t){b_}, (float128_t){a_}), (float128_t){d_}), softfloat_round_minMag, false); + float128_t tmp; + f128M_sub((float128_t*){&b_}, (float128_t*){&a_}, &tmp); + f128M_div(&tmp, (float128_t*){&d_}, &tmp); + n_ = f128M_to_i64(&tmp, softfloat_round_minMag, false); break; } u3_noun n = u3i_chub(n_+1); From 04a33f757043b75ffd46236b646a7782c044eec9 Mon Sep 17 00:00:00 2001 From: Sigilante Date: Wed, 24 Apr 2024 11:51:00 -0500 Subject: [PATCH 112/430] Swap out SoftFloat fns. --- pkg/noun/jets/i/lagoon.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pkg/noun/jets/i/lagoon.c b/pkg/noun/jets/i/lagoon.c index f0906cd20a..b5fe75f298 100644 --- a/pkg/noun/jets/i/lagoon.c +++ b/pkg/noun/jets/i/lagoon.c @@ -435,7 +435,8 @@ f128M_div((float128_t*)&x_val128, (float128_t*)&y_val128, (float128_t*)&div_result128); // Compute floor of the division result c3_ds floor_result128 = f128M_to_i64(&div_result128, softfloat_round_minMag, false); - float128_t floor_float128 = i64_to_f128(floor_result128); + float128_t floor_float128; + i64_to_f128M(floor_result128, &floor_float128); // Multiply n by floor(x/n) float128_t mult_result128; f128M_mul(((float128_t*)&y_val128), ((float128_t*)&floor_float128), ((float128_t*)&mult_result128)); @@ -1642,7 +1643,8 @@ f128M_mul((float128_t*)&in128, (float128_t*)&x_val128, (float128_t*)&div_result128); // Compute floor of the division result c3_ds floor_result128 = f128M_to_i64(&div_result128, softfloat_round_minMag, false); - float128_t floor_float128 = i64_to_f128(floor_result128); + float128_t floor_float128; + i64_to_f128M(floor_result128, &floor_float128); // Multiply n by floor(x/n) float128_t mult_result128; f128M_mul(((float128_t*)&n128), ((float128_t*)&floor_float128), ((float128_t*)&mult_result128)); From 8b76ac5ab77292080312b1516afb74e3117075b2 Mon Sep 17 00:00:00 2001 From: Sigilante Date: Wed, 24 Apr 2024 11:55:46 -0500 Subject: [PATCH 113/430] Swap out SoftFloat fns. --- pkg/noun/jets/i/lagoon.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/noun/jets/i/lagoon.c b/pkg/noun/jets/i/lagoon.c index b5fe75f298..502cc6af2e 100644 --- a/pkg/noun/jets/i/lagoon.c +++ b/pkg/noun/jets/i/lagoon.c @@ -590,7 +590,7 @@ case 7: ; float128_t min_val128 = ((float128_t*)x_bytes)[0]; for (c3_d i = 0; i < len_x; i++) { - if(f128_lt(((float128_t*)x_bytes)[i], min_val128)) { + if(f128M_lt(&(((float128_t*)x_bytes)[i]), &min_val128)) { min_val128 = *f128M_min(&min_val128, &((float128_t*)x_bytes)[i]); min_idx = (len_x - i - 1); } From fdd6951712135afbcafab3afa94cd2e654f183ed Mon Sep 17 00:00:00 2001 From: Sigilante Date: Wed, 24 Apr 2024 11:59:29 -0500 Subject: [PATCH 114/430] Swap out SoftFloat fns. --- pkg/noun/jets/i/lagoon.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/noun/jets/i/lagoon.c b/pkg/noun/jets/i/lagoon.c index 502cc6af2e..2bfe195b3c 100644 --- a/pkg/noun/jets/i/lagoon.c +++ b/pkg/noun/jets/i/lagoon.c @@ -663,7 +663,7 @@ case 7: ; float128_t max_val128 = ((float128_t*)x_bytes)[0]; for (c3_d i = 0; i < len_x; i++) { - if(f128_gt(((float128_t*)x_bytes)[i], max_val128)) { + if(f128M_gt(&(((float128_t*)x_bytes)[i]), &max_val128)) { max_val128 = *f128M_max(&max_val128, &((float128_t*)x_bytes)[i]); max_idx = (len_x - i - 1); } From ffccc3f3cb113bc13050a18cb9efb95a0852ea36 Mon Sep 17 00:00:00 2001 From: Pyry Kovanen Date: Thu, 25 Apr 2024 18:51:30 +0300 Subject: [PATCH 115/430] jets: fix unintialized nonce in chacha jet --- pkg/noun/jets/e/chacha.c | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/noun/jets/e/chacha.c b/pkg/noun/jets/e/chacha.c index 779e08a15e..1deadc6b25 100644 --- a/pkg/noun/jets/e/chacha.c +++ b/pkg/noun/jets/e/chacha.c @@ -17,6 +17,7 @@ else { c3_y key_y[32], nonce_y[8]; u3r_bytes(0, 32, key_y, key); + u3r_bytes(0, 8, nonce_y, nonce); c3_y *dat_y = u3r_bytes_alloc(0, wid_w, dat); urcrypt_chacha_crypt(rounds, key_y, nonce_y, counter, wid, dat_y); u3_noun cry = u3i_bytes(wid_w, dat_y); From ce235e720eab9013c9512493e82ab9ef76b4c182 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Thu, 25 Apr 2024 12:36:59 -0400 Subject: [PATCH 116/430] chacha: corrects scalar/noun confusion --- pkg/noun/jets/e/chacha.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/noun/jets/e/chacha.c b/pkg/noun/jets/e/chacha.c index 1deadc6b25..f95c8f6117 100644 --- a/pkg/noun/jets/e/chacha.c +++ b/pkg/noun/jets/e/chacha.c @@ -19,7 +19,7 @@ u3r_bytes(0, 32, key_y, key); u3r_bytes(0, 8, nonce_y, nonce); c3_y *dat_y = u3r_bytes_alloc(0, wid_w, dat); - urcrypt_chacha_crypt(rounds, key_y, nonce_y, counter, wid, dat_y); + urcrypt_chacha_crypt(rounds_w, key_y, nonce_y, counter_d, wid_w, dat_y); u3_noun cry = u3i_bytes(wid_w, dat_y); u3a_free(dat_y); return u3i_cell(wid, cry); From d078a314d24464fd3d51e604f8a96609a2576cf5 Mon Sep 17 00:00:00 2001 From: Pyry Kovanen Date: Thu, 25 Apr 2024 19:37:33 +0300 Subject: [PATCH 117/430] ci: install pkg-config in linux-aarch64 --- .github/workflows/shared.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/shared.yml b/.github/workflows/shared.yml index aee47884a5..bd8044e580 100644 --- a/.github/workflows/shared.yml +++ b/.github/workflows/shared.yml @@ -113,7 +113,7 @@ jobs: run: | case "${{ matrix.target }}" in "linux-aarch64") - sudo apt-get -y install autoconf-archive + sudo apt-get -y install autoconf-archive pkg-config bazel run //bazel/toolchain:aarch64-linux-musl-gcc ;; "linux-x86_64") From f171b19c8be54a737df2f5acd9e41b4c78da5f02 Mon Sep 17 00:00:00 2001 From: Pyry Kovanen Date: Wed, 1 May 2024 19:47:32 +0300 Subject: [PATCH 118/430] ctx: fix uninitialized stack variable bug --- pkg/noun/jets/c/ctz.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/noun/jets/c/ctz.c b/pkg/noun/jets/c/ctz.c index ff478902a5..ff74e244c3 100644 --- a/pkg/noun/jets/c/ctz.c +++ b/pkg/noun/jets/c/ctz.c @@ -8,7 +8,7 @@ u3_atom u3qc_ctz(u3_atom a) { - c3_w wor_w, i_w; + c3_w wor_w, i_w = 0; if ( 0 == a ) { return 0; From 59619bc4bea6a079c2ab4ccd1f914b867e6635b8 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Thu, 2 May 2024 15:37:48 +0200 Subject: [PATCH 119/430] wip --- pkg/vere/io/mesa.c | 74 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 51 insertions(+), 23 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index ec657a2d0e..1e080312f2 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -67,7 +67,7 @@ typedef struct _u3_mesa_stat { #define IN_FLIGHT 10 // XX -#define MESA_HUNK 4096 +#define MESA_HUNK 4096 // 184 // pending interest sentinels #define MESA_ITEM 1 // cached item @@ -296,6 +296,18 @@ _log_mesa_data(u3_mesa_data dat_u) // u3l_log("frag: %xxx", dat_u.fra_y); } +/* _mesa_lop(): find beginning of page containing fra_w +*/ +static inline c3_w +_mesa_lop(c3_w fra_w) +{ + if ( fra_w == 0 ) { + return 1; + } else { + return 1 + ((( fra_w - 1) / MESA_HUNK) * MESA_HUNK); + } +} + static c3_d _get_now_micros() { @@ -621,6 +633,7 @@ _mesa_req_get_cwnd(u3_pend_req* req_u) } c3_w rem_w = req_u->tot_w - req_u->nex_w + 1; + u3l_log("rem_w: %u", rem_w); return c3_min(rem_w, req_u->gag_u->wnd_w - liv_w); } @@ -846,9 +859,9 @@ _try_resend(u3_pend_req* req_u) // TODO: better route management _mesa_send_buf(sam_u, *lan_u, buf_y, siz_w); _mesa_req_pact_resent(req_u, &req_u->pic_u->pac_u.pek_u.nam_u); - } - } - } + } + } + } if ( c3y == los_o ) { req_u->gag_u->sst_w = (req_u->gag_u->wnd_w / 2) + 1; @@ -1481,6 +1494,8 @@ _name_to_batch_scry(u3_mesa_name* nam_u, c3_w lop_w, c3_w len_w) u3_noun res = u3nc(c3__mess, u3nq(rif, c3__pact, boq, u3nc(c3__etch, wer))); // [%hunk lop=@t len=@t pat=*] + u3l_log("lop_w %u len_w %u", lop_w, len_w); + u3m_p("len", len); u3_noun bat = u3nq(c3__hunk, lop, len, res); return bat; @@ -1585,24 +1600,24 @@ _mesa_page_scry_hunk_cb(void* vod_p, u3_noun nun) // TODO: mark as dead //u3z(nun); u3l_log("unbound"); - } else { - u3_weak old = _mesa_get_cache(sam_u, &pac_u->pag_u.nam_u); - if ( old == u3_none ) { - u3l_log("bad"); - MESA_LOG(APATHY); - } else { - u3_noun tag; - u3_noun dat; - u3x_cell(u3k(old), &tag, &dat); - if ( MESA_WAIT == tag ) { - c3_y* buf_y; - // u3m_p("hit", u3a_is_cell(hit)); - c3_w len_w = _mesa_respond(pic_u, &buf_y, u3k(u3h(hit))); - _mesa_rout_bufs(sam_u, buf_y, len_w, u3k(u3t(dat))); - } + // u3_weak old = _mesa_get_cache(sam_u, &pac_u->pag_u.nam_u); + // if ( old == u3_none ) { + // u3l_log("bad"); + // MESA_LOG(APATHY); + // } else { + // u3_noun tag; + // u3_noun dat; + // u3x_cell(u3k(old), &tag, &dat); + // if ( MESA_WAIT == tag ) { + // c3_y* buf_y; + // // u3m_p("hit", u3a_is_cell(hit)); + // c3_w len_w = _mesa_respond(pic_u, &buf_y, u3k(u3h(hit))); + // _mesa_rout_bufs(sam_u, buf_y, len_w, u3k(u3t(dat))); + // } c3_w len_w = pac_u->pek_u.nam_u.fra_w; + c3_w i = 0; while ( u3_nul != hit ) { // u3_noun key = u3nc(u3k(pax), u3i_word(lop_w)); // u3h_put(sam_u->fin_s.sac_p, key, u3k(u3h(lis))); @@ -1612,12 +1627,13 @@ _mesa_page_scry_hunk_cb(void* vod_p, u3_noun nun) hit = u3t(hit); len_w++; + i++; pac_u->pek_u.nam_u.fra_w = len_w; } + u3l_log("i %u", i); // u3z(old); } // u3z(hit); - } // u3z(pax); } @@ -2045,8 +2061,14 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) _mesa_free_pict(pic_u); return; } + c3_w bat_w = _mesa_lop(pac_u->pek_u.nam_u.fra_w); + c3_w fra_w = pac_u->pek_u.nam_u.fra_w; + pac_u->pek_u.nam_u.fra_w = bat_w; u3_weak hit = _mesa_get_cache(sam_u, &pac_u->pek_u.nam_u); + pac_u->pek_u.nam_u.fra_w = fra_w; + + u3l_log("peek fra %u bat %u", fra_w, bat_w); if ( u3_none != hit ) { u3_noun tag, dat; @@ -2055,10 +2077,14 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) _mesa_add_lane_to_cache(sam_u, &pac_u->pek_u.nam_u, u3k(u3t(dat)), lan_u); } else if ( c3y == our_o && tag == MESA_ITEM ) { // XX our_o redundant c3_y* buf_y; - c3_w len_w = _mesa_respond(pic_u, &buf_y, u3k(dat)); + u3_weak hit_2 = _mesa_get_cache(sam_u, &pac_u->pek_u.nam_u); + u3_noun tag_2, dat_2; + u3x_cell(u3k(hit_2), &tag_2, &dat_2); + u3l_log("cache hit %u bat %u", fra_w, bat_w); + c3_w len_w = _mesa_respond(pic_u, &buf_y, u3k(dat_2)); // _log_buf(buf_y, len_w); _mesa_send_buf(sam_u, lan_u, buf_y, len_w); - + u3z(hit_2); } else { u3l_log("mesa: weird case in cache, dropping"); } @@ -2068,7 +2094,9 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) } else { _mesa_add_lane_to_cache(sam_u, &pac_u->pek_u.nam_u, u3_nul, lan_u); // TODO: retrieve from namespace if ( c3y == our_o ) { - u3_noun sky = _name_to_batch_scry(&pac_u->pek_u.nam_u, pac_u->pek_u.nam_u.fra_w, pac_u->pek_u.nam_u.fra_w + MESA_HUNK); + u3_noun sky = _name_to_batch_scry(&pac_u->pek_u.nam_u, + pac_u->pek_u.nam_u.fra_w, + pac_u->pek_u.nam_u.fra_w + MESA_HUNK); u3_noun our = u3i_chubs(2, sam_u->car_u.pir_u->who_d); u3_noun bem = u3nc(u3nt(our, u3_nul, u3nc(c3__ud, 1)), sky); From 7c984a77dab2a1fe6f84c4cfc8a65e3eafeea24f Mon Sep 17 00:00:00 2001 From: pkova Date: Fri, 10 May 2024 14:41:56 +0300 Subject: [PATCH 120/430] mesa: fix bitset init being too small --- pkg/vere/io/mesa/bitset.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/vere/io/mesa/bitset.c b/pkg/vere/io/mesa/bitset.c index d58fd9ba4b..a706c6074b 100644 --- a/pkg/vere/io/mesa/bitset.c +++ b/pkg/vere/io/mesa/bitset.c @@ -21,7 +21,7 @@ void bitset_init(u3_bitset* bit_u, c3_w len_w) { bit_u->len_w = len_w; - bit_u->buf_y = c3_calloc(len_w >> 3); + bit_u->buf_y = c3_calloc((len_w >> 3) + 1); } void From d44aca92b1a97eed2acc4d5aa55b0c51465d653e Mon Sep 17 00:00:00 2001 From: pkova Date: Fri, 10 May 2024 14:42:17 +0300 Subject: [PATCH 121/430] mesa: misc fixes and debug logging --- pkg/vere/io/mesa.c | 128 ++++++++++++++++++++++++++++----------------- 1 file changed, 81 insertions(+), 47 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 1e080312f2..97cfb13c4a 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -301,11 +301,11 @@ _log_mesa_data(u3_mesa_data dat_u) static inline c3_w _mesa_lop(c3_w fra_w) { - if ( fra_w == 0 ) { + c3_w res_w = ((( fra_w ) / MESA_HUNK) * MESA_HUNK); + if (res_w == 0) { return 1; - } else { - return 1 + ((( fra_w - 1) / MESA_HUNK) * MESA_HUNK); } + return res_w; } static c3_d @@ -324,6 +324,7 @@ _abs_dif(c3_d ayy_d, c3_d bee_d) static c3_d _clamp_rto(c3_d rto_d) { + /* u3l_log("clamp rto %llu", rto_d); */ return c3_min(c3_max(rto_d, 200000), 25000000); } @@ -666,7 +667,13 @@ _mesa_req_pact_sent(u3_pend_req* req_u, u3_mesa_name* nam_u) req_u->nex_w++; } // TODO: optional assertions? - req_u->wat_u[nam_u->fra_w] = (u3_pact_stat){now_d, 0, 1, 0 }; + /* req_u->wat_u[nam_u->fra_w] = (u3_pact_stat){now_d, 0, 1, 0 }; */ + req_u->wat_u[nam_u->fra_w].sen_d = now_d; + req_u->wat_u[nam_u->fra_w].sip_y = 0; + req_u->wat_u[nam_u->fra_w].tie_y = 1; + req_u->wat_u[nam_u->fra_w].dup_y = 0; + + /* u3l_log("bitset_put %u", nam_u->fra_w); */ bitset_put(&req_u->was_u, nam_u->fra_w); } else { u3l_log("mesa: no req for sent"); @@ -825,15 +832,17 @@ _try_resend(u3_pend_req* req_u) u3_lane* lan_u = &req_u->lan_u; c3_o los_o = c3n; if ( req_u->tot_w == 0 || req_u->ack_w <= REORDER_THRESH ) { + u3l_log("reorder thresh too low %u", req_u->ack_w); return; } c3_w ack_w = req_u->ack_w - REORDER_THRESH; c3_d now_d = _get_now_micros(); - for ( int i = req_u->lef_w; i < ack_w; i++ ) { + /* u3l_log("lef_w %u, nex_w %u, len_w %u, tot_w %u", req_u->lef_w, req_u->nex_w, req_u->len_w, req_u->tot_w); */ + for ( int i = req_u->lef_w; i < req_u->nex_w; i++ ) { if ( c3y == bitset_has(&req_u->was_u, i) ) { req_u->pic_u->pac_u.pek_u.nam_u.fra_w = i; if ( req_u->wat_u[i].tie_y == 1 ) { - u3l_log("fast resend %u", i); + /* u3l_log("fast resend %u", i); */ los_o = c3y; c3_y* buf_y = c3_calloc(PACT_SIZE); req_u->pic_u->pac_u.pek_u.nam_u.fra_w = i; @@ -850,7 +859,7 @@ _try_resend(u3_pend_req* req_u) los_o = c3y; c3_y* buf_y = c3_calloc(PACT_SIZE); req_u->pic_u->pac_u.pek_u.nam_u.fra_w = i; - u3l_log("slow resending %u ", i); + /* u3l_log("slow resending %u ", i); */ c3_w siz_w = mesa_etch_pact(buf_y, &req_u->pic_u->pac_u); if( siz_w == 0 ) { u3l_log("failed to etch"); @@ -859,6 +868,8 @@ _try_resend(u3_pend_req* req_u) // TODO: better route management _mesa_send_buf(sam_u, *lan_u, buf_y, siz_w); _mesa_req_pact_resent(req_u, &req_u->pic_u->pac_u.pek_u.nam_u); + } else { + /* u3l_log("did nothing %u %llu %u", req_u->wat_u[i].tie_y, (now_d - req_u->wat_u[i].sen_d), req_u->gag_u->rto_w); */ } } } @@ -870,19 +881,14 @@ _try_resend(u3_pend_req* req_u) } } -/* _mesa_packet_timeout(): callback for packet timeout -*/ static void -_mesa_packet_timeout(uv_timer_t* tim_u) { - u3_pend_req* req_u = (u3_pend_req*)tim_u->data; - u3l_log("%u packet timed out", req_u->old_w); - _try_resend(req_u); -} +_mesa_packet_timeout(uv_timer_t* tim_u); static void _update_oldest_req(u3_pend_req *req_u, u3_gage* gag_u) { if( req_u->tot_w == 0 || req_u->len_w == req_u->tot_w ) { + u3l_log("bad condition"); return; } // scan in flight packets, find oldest @@ -905,10 +911,21 @@ _update_oldest_req(u3_pend_req *req_u, u3_gage* gag_u) } req_u->old_w = idx_w; req_u->tim_u.data = req_u; - c3_d gap_d = now_d -req_u->wat_u[idx_w].sen_d; + c3_d gap_d = req_u->wat_u[idx_w].sen_d - now_d; + /* u3l_log("timeout %llu", (gag_u->rto_w - gap_d) / 1000); */ uv_timer_start(&req_u->tim_u, _mesa_packet_timeout, (gag_u->rto_w - gap_d) / 1000, 0); } +/* _mesa_packet_timeout(): callback for packet timeout +*/ +static void +_mesa_packet_timeout(uv_timer_t* tim_u) { + u3_pend_req* req_u = (u3_pend_req*)tim_u->data; + /* u3l_log("%u packet timed out", req_u->old_w); */ + _try_resend(req_u); + _update_oldest_req(req_u, req_u->gag_u); +} + static void _mesa_free_misord_buf(u3_misord_buf* buf_u) { c3_free(buf_u->fra_y); @@ -984,22 +1001,24 @@ _mesa_req_pact_done(u3_mesa* sam_u, u3_mesa_name *nam_u, u3_mesa_data* dat_u, u3 // received duplicate if ( nam_u->fra_w != 0 && c3n == bitset_has(&req_u->was_u, nam_u->fra_w) ) { - MESA_LOG(DUPE); + /* MESA_LOG(DUPE); */ req_u->wat_u[nam_u->fra_w].dup_y++; return req_u; } + /* u3l_log("bitset_del %u", nam_u->fra_w); */ bitset_del(&req_u->was_u, nam_u->fra_w); if ( nam_u->fra_w > req_u->ack_w ) { req_u->ack_w = nam_u->fra_w; } if ( nam_u->fra_w != 0 && req_u->wat_u[nam_u->fra_w].tie_y != 1 ) { #ifdef MESA_DEBUG - u3l_log("received retry %u", nam_u->fra_w); + /* u3l_log("received retry %u", nam_u->fra_w); */ #endif } req_u->len_w++; + /* u3l_log("fragment %u len %u", nam_u->fra_w, req_u->len_w); */ if ( req_u->lef_w == nam_u->fra_w ) { req_u->lef_w++; } @@ -1026,6 +1045,7 @@ _mesa_req_pact_done(u3_mesa* sam_u, u3_mesa_name *nam_u, u3_mesa_data* dat_u, u3 } else if ( BAO_GOOD != (ver_y = blake_bao_verify(req_u->bao_u, dat_u->fra_y, dat_u->len_w, par_u)) ) { c3_free(par_u); // TODO: do we drop the whole request on the floor? + u3l_log("auth fail frag %u", nam_u->fra_w); MESA_LOG(AUTH); return req_u; } else if ( vec_len(&req_u->mis_u) != 0 @@ -1088,9 +1108,9 @@ _mesa_rout_bufs(u3_mesa* sam_u, c3_y* buf_y, c3_w len_w, u3_noun las) while ( t != u3_nul ) { u3x_cell(t, &lan, &t); u3_lane lan_u = _realise_lane(u3k(lan)); - u3l_log("sending to ip: %x, port: %u", lan_u.pip_w, lan_u.por_s); + /* u3l_log("sending to ip: %x, port: %u", lan_u.pip_w, lan_u.por_s); */ #ifdef MESA_DEBUG - u3l_log("sending to ip: %x, port: %u", lan_u.pip_w, lan_u.por_s); + /* u3l_log("sending to ip: %x, port: %u", lan_u.pip_w, lan_u.por_s); */ #endif /* ifdef MESA_DEBUG u3l_log("sending to ip: %x, port: %u", lan_u.pip_w, lan_u.por_s); */ @@ -1262,7 +1282,7 @@ _mesa_ef_send(u3_mesa* sam_u, u3_noun las, u3_noun pac) static c3_o _mesa_kick(u3_mesa* sam_u, u3_noun tag, u3_noun dat) { c3_o ret_o; - u3l_log("blabla"); + /* u3l_log("blabla"); */ switch ( tag ) { default: { ret_o = c3n; @@ -1494,8 +1514,6 @@ _name_to_batch_scry(u3_mesa_name* nam_u, c3_w lop_w, c3_w len_w) u3_noun res = u3nc(c3__mess, u3nq(rif, c3__pact, boq, u3nc(c3__etch, wer))); // [%hunk lop=@t len=@t pat=*] - u3l_log("lop_w %u len_w %u", lop_w, len_w); - u3m_p("len", len); u3_noun bat = u3nq(c3__hunk, lop, len, res); return bat; @@ -1601,27 +1619,34 @@ _mesa_page_scry_hunk_cb(void* vod_p, u3_noun nun) //u3z(nun); u3l_log("unbound"); } else { - // u3_weak old = _mesa_get_cache(sam_u, &pac_u->pag_u.nam_u); - // if ( old == u3_none ) { - // u3l_log("bad"); - // MESA_LOG(APATHY); - // } else { - // u3_noun tag; - // u3_noun dat; - // u3x_cell(u3k(old), &tag, &dat); - // if ( MESA_WAIT == tag ) { - // c3_y* buf_y; - // // u3m_p("hit", u3a_is_cell(hit)); - // c3_w len_w = _mesa_respond(pic_u, &buf_y, u3k(u3h(hit))); - // _mesa_rout_bufs(sam_u, buf_y, len_w, u3k(u3t(dat))); - // } - - c3_w len_w = pac_u->pek_u.nam_u.fra_w; + c3_w fra_w = pac_u->pek_u.nam_u.fra_w; + c3_w bat_w = _mesa_lop(fra_w); + pac_u->pek_u.nam_u.fra_w = bat_w; + u3_weak old = _mesa_get_cache(sam_u, &pac_u->pag_u.nam_u); + if ( old == u3_none ) { + u3l_log("bad"); + MESA_LOG(APATHY); + } else { + u3_noun tag; + u3_noun dat; + u3x_cell(u3k(old), &tag, &dat); + c3_y* buf_y; + // u3m_p("hit", u3a_is_cell(hit)); + + c3_w len_w = fra_w == 0 ? 0 : bat_w; + /* u3l_log("path %s", pac_u->pek_u.nam_u.pat_c); */ c3_w i = 0; while ( u3_nul != hit ) { // u3_noun key = u3nc(u3k(pax), u3i_word(lop_w)); // u3h_put(sam_u->fin_s.sac_p, key, u3k(u3h(lis))); + if (fra_w == len_w && tag == MESA_WAIT) { + c3_w lun_w = _mesa_respond(pic_u, &buf_y, u3k(u3h(hit))); + pac_u->pek_u.nam_u.fra_w = fra_w; + _mesa_rout_bufs(sam_u, buf_y, lun_w, u3k(u3t(dat))); + } + pac_u->pek_u.nam_u.fra_w = len_w; + /* u3l_log("putting %u", pac_u->pek_u.nam_u.fra_w); */ _mesa_put_cache(sam_u, &pac_u->pek_u.nam_u, u3nc(MESA_ITEM, u3k(u3h(hit)))); // u3z(key); @@ -1633,6 +1658,7 @@ _mesa_page_scry_hunk_cb(void* vod_p, u3_noun nun) u3l_log("i %u", i); // u3z(old); } + } // u3z(hit); // u3z(pax); } @@ -1823,6 +1849,7 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) vec_init(&req_u->mis_u, 8); req_u = _mesa_put_request(sam_u, nam_u, req_u); + _update_oldest_req(req_u, gag_u); return req_u; } @@ -1830,7 +1857,7 @@ static void _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) { #ifdef MESA_DEBUG - u3l_log("mesa hear page %u", pic_u->pac_u.pag_u.nam_u.fra_w); + /* u3l_log("mesa hear page %u", pic_u->pac_u.pag_u.nam_u.fra_w); */ #endif u3_mesa* sam_u = pic_u->sam_u; u3_mesa_pact* pac_u = &pic_u->pac_u; @@ -1992,6 +2019,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) u3i_slab sab_u; // u3i_slab_init(&sab_u, 3, PACT_SIZE); // u3i_slab_init(&sab_u, 3, (siz_w * req_u->tot_w) + 135); + u3l_log("slab size %u", (PACT_SIZE - pac_u->pag_u.dat_u.len_w) + (siz_w * req_u->tot_w)); u3i_slab_init(&sab_u, 3, (PACT_SIZE - pac_u->pag_u.dat_u.len_w) + (siz_w * req_u->tot_w)); pac_u->pag_u.dat_u.len_w = (siz_w * req_u->tot_w); @@ -2004,7 +2032,8 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) // pic_u->pac_u.pag_u.nam_u.fra_w = req_u->tot_w - 1; // XX should just preserve input buffer - mesa_etch_pact(sab_u.buf_y, pac_u); + c3_w res = mesa_etch_pact(sab_u.buf_y, pac_u); + u3l_log("slab len_w %u mesa_etch_pact %u", sab_u.len_w, res); cad = u3nt(c3__heer, lan, u3i_slab_mint(&sab_u)); } @@ -2061,26 +2090,29 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) _mesa_free_pict(pic_u); return; } - c3_w bat_w = _mesa_lop(pac_u->pek_u.nam_u.fra_w); c3_w fra_w = pac_u->pek_u.nam_u.fra_w; + c3_w bat_w = _mesa_lop(fra_w); + pac_u->pek_u.nam_u.fra_w = bat_w; u3_weak hit = _mesa_get_cache(sam_u, &pac_u->pek_u.nam_u); - pac_u->pek_u.nam_u.fra_w = fra_w; - u3l_log("peek fra %u bat %u", fra_w, bat_w); + /* u3l_log("peek fra %u hit %u", fra_w, hit != u3_none); */ + /* u3l_log("peek fra %u bat %u hit %u", fra_w, bat_w, hit != u3_none); */ if ( u3_none != hit ) { u3_noun tag, dat; u3x_cell(u3k(hit), &tag, &dat); if ( tag == MESA_WAIT ) { + /* u3l_log("MESA_WAIT for %u", bat_w); */ _mesa_add_lane_to_cache(sam_u, &pac_u->pek_u.nam_u, u3k(u3t(dat)), lan_u); } else if ( c3y == our_o && tag == MESA_ITEM ) { // XX our_o redundant + pac_u->pek_u.nam_u.fra_w = fra_w; c3_y* buf_y; u3_weak hit_2 = _mesa_get_cache(sam_u, &pac_u->pek_u.nam_u); u3_noun tag_2, dat_2; - u3x_cell(u3k(hit_2), &tag_2, &dat_2); - u3l_log("cache hit %u bat %u", fra_w, bat_w); + u3x_cell(hit_2, &tag_2, &dat_2); + /* u3l_log("cache hit %u bat %u", fra_w, bat_w); */ c3_w len_w = _mesa_respond(pic_u, &buf_y, u3k(dat_2)); // _log_buf(buf_y, len_w); _mesa_send_buf(sam_u, lan_u, buf_y, len_w); @@ -2095,8 +2127,10 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) _mesa_add_lane_to_cache(sam_u, &pac_u->pek_u.nam_u, u3_nul, lan_u); // TODO: retrieve from namespace if ( c3y == our_o ) { u3_noun sky = _name_to_batch_scry(&pac_u->pek_u.nam_u, - pac_u->pek_u.nam_u.fra_w, - pac_u->pek_u.nam_u.fra_w + MESA_HUNK); + fra_w == 0 ? 0 : bat_w, + fra_w == 0 ? 4096 : bat_w + MESA_HUNK); + + pac_u->pek_u.nam_u.fra_w = fra_w; u3_noun our = u3i_chubs(2, sam_u->car_u.pir_u->who_d); u3_noun bem = u3nc(u3nt(our, u3_nul, u3nc(c3__ud, 1)), sky); From 43eb6d3352ab9ee5af913b81760894de360012a8 Mon Sep 17 00:00:00 2001 From: pkova Date: Fri, 10 May 2024 19:43:03 +0300 Subject: [PATCH 122/430] serf: make trace work with peeks --- pkg/vere/serf.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/pkg/vere/serf.c b/pkg/vere/serf.c index a99544b447..0e667d7379 100644 --- a/pkg/vere/serf.c +++ b/pkg/vere/serf.c @@ -5,6 +5,7 @@ #include "vere.h" #include "ivory.h" #include "ur.h" +#include /* |% @@ -752,9 +753,32 @@ u3_serf_play(u3_serf* sef_u, c3_d eve_d, u3_noun lit) u3_noun u3_serf_peek(u3_serf* sef_u, c3_w mil_w, u3_noun sam) { + c3_t tac_t = !!( u3C.wag_w & u3o_trace ); + c3_c lab_c[2056]; + + // XX refactor tracing + // + if ( tac_t ) { + c3_c* foo_c = u3m_pretty(u3t(sam)); + + { + snprintf(lab_c, 2056, "peek %s", foo_c); + c3_free(foo_c); + } + + u3t_event_trace(lab_c, 'B'); + } + + u3_noun gon = u3m_soft(mil_w, u3v_peek, sam); u3_noun pro; + if ( tac_t ) { + u3t_event_trace(lab_c, 'E'); + } + + + { u3_noun tag, dat; u3x_cell(gon, &tag, &dat); From 5090eaaded76a63209c3e1c919a6d461d7af2c02 Mon Sep 17 00:00:00 2001 From: pkova Date: Tue, 14 May 2024 13:59:00 +0300 Subject: [PATCH 123/430] mesa: send -> push --- pkg/vere/io/mesa.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 97cfb13c4a..c9b9ab8bee 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1287,7 +1287,7 @@ static c3_o _mesa_kick(u3_mesa* sam_u, u3_noun tag, u3_noun dat) default: { ret_o = c3n; } break; - case c3__send: { + case c3__push: { u3_noun las, pac; if ( c3n == u3r_cell(dat, &las, &pac) ) { u3l_log(" vere: send no"); From 8feafc42e3c2dca34e419cfdc20f51fb28dfa7fd Mon Sep 17 00:00:00 2001 From: pkova Date: Mon, 20 May 2024 17:13:49 +0300 Subject: [PATCH 124/430] mesa: zero pad blake_bao_verify buffer --- pkg/vere/io/mesa.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index c9b9ab8bee..191d0f316d 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1033,27 +1033,36 @@ _mesa_req_pact_done(u3_mesa* sam_u, u3_mesa_name *nam_u, u3_mesa_data* dat_u, u3 c3_y ver_y; // TODO: move to bottom + + c3_y buf_y[1024]; + memcpy(buf_y, dat_u->fra_y, dat_u->len_w); + memset(buf_y + dat_u->len_w, 0, 1024 - dat_u->len_w); + c3_w len_w = (nam_u->fra_w + 1 == dat_u->tot_w) ? dat_u->len_w : 1024; + if ( req_u->bao_u->con_w != nam_u->fra_w ) { // TODO: queue packet u3_misord_buf* buf_u = c3_calloc(sizeof(u3_misord_buf)); - buf_u->fra_y = c3_calloc(dat_u->len_w); - buf_u->len_w = dat_u->len_w; - memcpy(buf_u->fra_y, dat_u->fra_y, dat_u->len_w); + buf_u->fra_y = c3_calloc(len_w); + buf_u->len_w = len_w; + memcpy(buf_u->fra_y, buf_y, len_w); buf_u->par_u = par_u; buf_u->num_w = nam_u->fra_w; vec_append(&req_u->mis_u, buf_u); - } else if ( BAO_GOOD != (ver_y = blake_bao_verify(req_u->bao_u, dat_u->fra_y, dat_u->len_w, par_u)) ) { + } + else if ( BAO_GOOD != (ver_y = blake_bao_verify(req_u->bao_u, buf_y, len_w, par_u)) ) { c3_free(par_u); // TODO: do we drop the whole request on the floor? u3l_log("auth fail frag %u", nam_u->fra_w); MESA_LOG(AUTH); return req_u; - } else if ( vec_len(&req_u->mis_u) != 0 + } + else if ( vec_len(&req_u->mis_u) != 0 && BAO_GOOD != (ver_y = _mesa_burn_misorder_queue(req_u))) { c3_free(par_u); MESA_LOG(AUTH) return req_u; - } else { + } + else { c3_free(par_u); } From 1a36dfad9a4b060061bb13a4997264a48d6a8f04 Mon Sep 17 00:00:00 2001 From: lukechampine Date: Tue, 21 May 2024 00:29:33 -0400 Subject: [PATCH 125/430] jets: add rounds parameter to xchacha --- WORKSPACE.bazel | 4 ++-- pkg/noun/jets/e/chacha.c | 16 +++++++++++----- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index 4509e425cb..5f0d710221 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -327,10 +327,10 @@ versioned_http_file( versioned_http_archive( name = "urcrypt", build_file = "//bazel/third_party/urcrypt:urcrypt.BUILD", - sha256 = "6f054aeca23081265e9c7ed76dd156ac051b64b49d82375f761163d4988fef62", + sha256 = "3c66dd6acdb4703c68f24958c820e79b6a56285ea5d0ba769f60eafe3f0cc0db", strip_prefix = "urcrypt-{version}", url = "https://github.com/urbit/urcrypt/archive/{version}.tar.gz", - version = "c6052b52645d1097602b15cc590ccf05495117ba", + version = "a17279dcbf38d812c7019a3e71d25bdfdb8842ef", ) versioned_http_archive( diff --git a/pkg/noun/jets/e/chacha.c b/pkg/noun/jets/e/chacha.c index f95c8f6117..e42f8e221c 100644 --- a/pkg/noun/jets/e/chacha.c +++ b/pkg/noun/jets/e/chacha.c @@ -44,25 +44,31 @@ static u3_noun - _cqe_chacha_xchacha(u3_atom key, u3_atom nonce) + _cqe_chacha_xchacha(u3_atom rounds, u3_atom key, u3_atom nonce) { + c3_w rounds_w; + if ( !u3r_word_fit(&rounds_w, rounds) ) { + return u3m_bail(c3__fail); + } c3_y key_y[32], nonce_y[64], xkey_y[32], xnonce_y[8]; u3r_bytes(0, 32, key_y, key); u3r_bytes(0, 24, nonce_y, nonce); - urcrypt_chacha_xchacha(key_y, nonce_y, xkey_y, xnonce_y); + urcrypt_chacha_xchacha(rounds, key_y, nonce_y, xkey_y, xnonce_y); return u3i_cell(u3i_bytes(32, xkey_y), u3i_bytes(8, xnonce_y)); } u3_noun u3we_chacha_xchacha(u3_noun cor) { - u3_noun key, nonce; - if ( c3n == u3r_mean(cor, u3x_sam_2, &key, u3x_sam_3, &nonce, 0) || + u3_noun sam = u3x_at(u3x_sam, cor); + u3_noun rounds, key, nonce; + if ( c3n == u3r_trel(sam, &rounds, &key, &nonce) || + c3n == u3ud(rounds) || c3n == u3ud(key) || c3n == u3ud(nonce) ) { return u3m_bail(c3__exit); } else { - return u3l_punt("chacha_xchacha", _cqe_chacha_xchacha(key, nonce)); + return u3l_punt("chacha_xchacha", _cqe_chacha_xchacha(rounds, key, nonce)); } } From e9647a4924bbe1041e95c488d7cbd5b7f00f2fb2 Mon Sep 17 00:00:00 2001 From: nathanlever Date: Wed, 14 Feb 2024 17:00:55 +0200 Subject: [PATCH 126/430] Add crc32 jet --- pkg/noun/jets/e/crc32.c | 51 +++++++++++++++++++++++++++++++++++++++++ pkg/noun/jets/tree.c | 13 +++++++++++ pkg/noun/jets/w.h | 3 +++ 3 files changed, 67 insertions(+) create mode 100644 pkg/noun/jets/e/crc32.c diff --git a/pkg/noun/jets/e/crc32.c b/pkg/noun/jets/e/crc32.c new file mode 100644 index 0000000000..08afcc0a19 --- /dev/null +++ b/pkg/noun/jets/e/crc32.c @@ -0,0 +1,51 @@ +/// @file + +#include +#include +#include "zlib.h" + +#include "jets/w.h" + +#include "noun.h" + +u3_noun +u3qe_crc32(u3_noun input_octs) +{ + u3_atom tail = u3t(input_octs); + c3_w len_w = u3r_met(3, tail); + c3_y* input; + + if (c3y == u3a_is_cat(tail)) { + input = &tail; + } + else { + u3a_atom* vat_u = u3a_to_ptr(tail); + input = (c3_y*)vat_u->buf_w; + } + + u3_atom head = u3h(input_octs); + c3_w leading_zeros = head - len_w; + c3_w crc = 0L; + + while (leading_zeros > 0) { + c3_y lz_input = 0; + crc = crc32(crc, &lz_input, 1); + leading_zeros--; + } + + crc = crc32(crc, input, len_w); + return u3i_word(crc); +} + +u3_noun +u3we_crc32(u3_noun cor) +{ + u3_noun a = u3r_at(u3x_sam, cor); + + if ( (u3du(a) == c3y) && (u3ud(u3h(a))) == c3y && (u3ud(u3t(a))) == c3y) { + return u3qe_crc32(a); + } + else { + return u3m_bail(c3__exit); + } +} diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index c51859a928..e4933cf048 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -17,6 +17,7 @@ ``` */ + #include "c3.h" #include "jets.h" #include "jets/w.h" @@ -270,6 +271,14 @@ static c3_c* _140_hex_lune_ha[] = { 0 }; + +/* static u3j_harm _139_hex__unzip_gzip_a[] = {{".2", u3we_unzip_gzip}, {}}; */ +static u3j_harm _137_hex__crc32_a[] = {{".2", u3we_crc32}, {}}; + +/* static u3j_core _139_hex__unzip_d[] = {{"gzip", 7, _139_hex__unzip_gzip_a, 0, no_hashes }, {}}; */ +static u3j_core _137_hex__crc_d[] = {{"crc32", 7, _137_hex__crc32_a, 0, no_hashes }, {}}; + + static u3j_harm _140_hex_coed__ed_puck_a[] = {{".2", u3wee_puck}, {}}; static c3_c* _140_hex_coed__ed_puck_ha[] = { "1bc694675842345c50b0e20a2193bb5bcbb42f163fc832431a3d1822a81e4c98", @@ -2126,6 +2135,9 @@ static u3j_core _139_hex_d[] = { "loss", 63, _140_hex_loss_a, 0, no_hashes }, { "lune", 127, _140_hex_lune_a, 0, no_hashes }, + /* { "unzip", 31, 0, _139_hex__unzip_d, no_hashes }, */ + /* { "crc", 31, 0, _137_hex__crc_d, no_hashes }, */ + { "coed", 63, 0, _140_hex_coed_d, no_hashes }, { "aes", 31, 0, _140_hex_aes_d, no_hashes }, @@ -2346,6 +2358,7 @@ static u3j_core _138_hex_d[] = { "leer", 63, _140_hex_leer_a, 0, no_hashes }, { "loss", 63, _140_hex_loss_a, 0, no_hashes }, { "lune", 127, _140_hex_lune_a, 0, no_hashes }, + { "crc", 31, 0, _137_hex__crc_d, no_hashes }, { "coed", 63, 0, _140_hex_coed_d, no_hashes }, { "aes", 31, 0, _140_hex_aes_d, no_hashes }, diff --git a/pkg/noun/jets/w.h b/pkg/noun/jets/w.h index 34716971bd..d7a624c8bb 100644 --- a/pkg/noun/jets/w.h +++ b/pkg/noun/jets/w.h @@ -291,6 +291,9 @@ u3_noun u3wes_gte(u3_noun); u3_noun u3wes_gth(u3_noun); + /* u3_noun u3we_unzip_gzip(u3_noun); */ + u3_noun u3we_crc32(u3_noun); + /** Tier 6. **/ u3_noun u3wf_bull(u3_noun); From d7bb2c9161de6458d0e6f99f31aa90859ba6040e Mon Sep 17 00:00:00 2001 From: nathanlever Date: Thu, 23 May 2024 15:41:11 +0300 Subject: [PATCH 127/430] add zlib to dependencies --- pkg/noun/BUILD.bazel | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/noun/BUILD.bazel b/pkg/noun/BUILD.bazel index a6b8de6d7a..fd6963cb82 100644 --- a/pkg/noun/BUILD.bazel +++ b/pkg/noun/BUILD.bazel @@ -40,6 +40,7 @@ vere_library( "@sigsegv", "@softfloat", "@urcrypt", + "@zlib", ] + select({ "@platforms//os:macos": ["//pkg/noun/platform/darwin"], "@platforms//os:linux": ["//pkg/noun/platform/linux"], From 2d10d98a92631cf74925753de26862212002078e Mon Sep 17 00:00:00 2001 From: nathanlever Date: Thu, 23 May 2024 16:36:59 +0300 Subject: [PATCH 128/430] remove old jet registration comments, correct crc32 parentheses --- pkg/noun/jets/e/crc32.c | 5 ++--- pkg/noun/jets/tree.c | 6 ------ pkg/noun/jets/w.h | 1 - 3 files changed, 2 insertions(+), 10 deletions(-) diff --git a/pkg/noun/jets/e/crc32.c b/pkg/noun/jets/e/crc32.c index 08afcc0a19..84e91ae86e 100644 --- a/pkg/noun/jets/e/crc32.c +++ b/pkg/noun/jets/e/crc32.c @@ -42,10 +42,9 @@ u3we_crc32(u3_noun cor) { u3_noun a = u3r_at(u3x_sam, cor); - if ( (u3du(a) == c3y) && (u3ud(u3h(a))) == c3y && (u3ud(u3t(a))) == c3y) { + if ( (u3du(a) == c3y) && (u3ud(u3h(a)) == c3y) && (u3ud(u3t(a)) == c3y) ) { return u3qe_crc32(a); - } - else { + } else { return u3m_bail(c3__exit); } } diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index e4933cf048..8ad0d8f45c 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -17,7 +17,6 @@ ``` */ - #include "c3.h" #include "jets.h" #include "jets/w.h" @@ -272,10 +271,8 @@ static c3_c* _140_hex_lune_ha[] = { }; -/* static u3j_harm _139_hex__unzip_gzip_a[] = {{".2", u3we_unzip_gzip}, {}}; */ static u3j_harm _137_hex__crc32_a[] = {{".2", u3we_crc32}, {}}; -/* static u3j_core _139_hex__unzip_d[] = {{"gzip", 7, _139_hex__unzip_gzip_a, 0, no_hashes }, {}}; */ static u3j_core _137_hex__crc_d[] = {{"crc32", 7, _137_hex__crc32_a, 0, no_hashes }, {}}; @@ -2135,9 +2132,6 @@ static u3j_core _139_hex_d[] = { "loss", 63, _140_hex_loss_a, 0, no_hashes }, { "lune", 127, _140_hex_lune_a, 0, no_hashes }, - /* { "unzip", 31, 0, _139_hex__unzip_d, no_hashes }, */ - /* { "crc", 31, 0, _137_hex__crc_d, no_hashes }, */ - { "coed", 63, 0, _140_hex_coed_d, no_hashes }, { "aes", 31, 0, _140_hex_aes_d, no_hashes }, diff --git a/pkg/noun/jets/w.h b/pkg/noun/jets/w.h index d7a624c8bb..07e8498344 100644 --- a/pkg/noun/jets/w.h +++ b/pkg/noun/jets/w.h @@ -291,7 +291,6 @@ u3_noun u3wes_gte(u3_noun); u3_noun u3wes_gth(u3_noun); - /* u3_noun u3we_unzip_gzip(u3_noun); */ u3_noun u3we_crc32(u3_noun); /** Tier 6. From 8b3122b0ab9ea09aa91d00b1163d2bb046e69905 Mon Sep 17 00:00:00 2001 From: Sigilante Date: Fri, 24 May 2024 15:11:13 -0500 Subject: [PATCH 129/430] Update for SoftBLAS version. --- WORKSPACE.bazel | 2 +- pkg/noun/jets/i/lagoon.c | 224 +++++++++++++++++++++------------------ pkg/noun/jets/tree.c | 2 +- 3 files changed, 124 insertions(+), 104 deletions(-) diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index c904a63eff..ec6a637114 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -297,7 +297,7 @@ versioned_http_archive( strip_prefix = "SoftBLAS-{version}", # sha256 = "", url = "https://github.com/urbit/SoftBLAS/archive/{version}.tar.gz", - version = "7d05697aea5363dcf5f877a9c8b464e9c352d3d4", + version = "29daa2f2fd0ad5070e405ad287f3623804f8fc67", ) versioned_http_archive( diff --git a/pkg/noun/jets/i/lagoon.c b/pkg/noun/jets/i/lagoon.c index 2bfe195b3c..460058ecc7 100644 --- a/pkg/noun/jets/i/lagoon.c +++ b/pkg/noun/jets/i/lagoon.c @@ -98,6 +98,16 @@ return dims; } +/* soft check on u3_none return from q jet +*/ + static inline u3_noun _soft_run(u3_noun a) + { + if (u3_none == a) { + u3m_bail(c3__fail); + } + return a; + } + /* add - axpy = 1*x+y */ u3_noun @@ -125,7 +135,8 @@ // y_bytes is the data array (w/ leading 0x1, skipped by ?axpy) c3_y* y_bytes = (c3_y*)u3a_malloc((syz_x+1)*sizeof(c3_y)); - u3r_bytes(0, syz_x+1, y_bytes, y_data); + u3r_bytes(0, syz_x, y_bytes, y_data); + y_bytes[syz_x] = 0x1; // Switch on the block size. switch (u3x_atom(bloq)) { @@ -183,8 +194,9 @@ // y_bytes is the data array (w/ leading 0x1, skipped by ?axpy) c3_y* y_bytes = (c3_y*)u3a_malloc((syz_x+1)*sizeof(c3_y)); - u3r_bytes(0, syz_x+1, y_bytes, y_data); - + u3r_bytes(0, syz_x, y_bytes, y_data); + y_bytes[syz_x] = 0x1; + // Switch on the block size. switch (u3x_atom(bloq)) { case 4: @@ -242,7 +254,8 @@ // y_bytes is the data array (w/ leading 0x1, skipped by ?axpy) c3_y* y_bytes = (c3_y*)u3a_malloc((syz_x+1)*sizeof(c3_y)); - u3r_bytes(0, syz_x+1, y_bytes, y_data); + u3r_bytes(0, syz_x, y_bytes, y_data); + y_bytes[syz_x] = 0x1; // Switch on the block size. switch (u3x_atom(bloq)) { @@ -308,7 +321,8 @@ // y_bytes is the data array (w/ leading 0x1, skipped by ?axpy) c3_y* y_bytes = (c3_y*)u3a_malloc((syz_x+1)*sizeof(c3_y)); - u3r_bytes(0, syz_x+1, y_bytes, y_data); + u3r_bytes(0, syz_x, y_bytes, y_data); + y_bytes[syz_x] = 0x1; // Switch on the block size. switch (u3x_atom(bloq)) { @@ -374,7 +388,8 @@ // y_bytes is the data array (w/ leading 0x1, skipped by ?axpy) c3_y* y_bytes = (c3_y*)u3a_malloc((syz_x+1)*sizeof(c3_y)); - u3r_bytes(0, syz_x+1, y_bytes, y_data); + u3r_bytes(0, syz_x, y_bytes, y_data); + y_bytes[syz_x] = 0x1; // Switch on the block size. switch (u3x_atom(bloq)) { @@ -557,44 +572,44 @@ // Switch on the block size. switch (u3x_atom(bloq)) { - case 4: ; + case 4: { float16_t min_val16 = ((float16_t*)x_bytes)[0]; for (c3_d i = 0; i < len_x; i++) { if(f16_lt(((float16_t*)x_bytes)[i], min_val16)) { min_val16 = ((float16_t*)x_bytes)[i]; min_idx = (len_x - i - 1); } - } + } } break; - case 5: ; + case 5: { float32_t min_val32 = ((float32_t*)x_bytes)[0]; for (c3_d i = 0; i < len_x; i++) { if(f32_lt(((float32_t*)x_bytes)[i], min_val32)) { min_val32 = ((float32_t*)x_bytes)[i]; min_idx = (len_x - i - 1); } - } + } } break; - case 6: ; + case 6: { float64_t min_val64 = ((float64_t*)x_bytes)[0]; for (c3_d i = 0; i < len_x; i++) { if(f64_lt(((float64_t*)x_bytes)[i], min_val64)) { min_val64 = ((float64_t*)x_bytes)[i]; min_idx = (len_x - i - 1); } - } + } } break; - case 7: ; + case 7: { float128_t min_val128 = ((float128_t*)x_bytes)[0]; for (c3_d i = 0; i < len_x; i++) { if(f128M_lt(&(((float128_t*)x_bytes)[i]), &min_val128)) { min_val128 = *f128M_min(&min_val128, &((float128_t*)x_bytes)[i]); min_idx = (len_x - i - 1); } - } + } } break; } @@ -630,44 +645,44 @@ // Switch on the block size. switch (u3x_atom(bloq)) { - case 4: ; + case 4: { float16_t max_val16 = ((float16_t*)x_bytes)[0]; for (c3_d i = 0; i < len_x; i++) { if(f16_gt(((float16_t*)x_bytes)[i], max_val16)) { max_val16 = ((float16_t*)x_bytes)[i]; max_idx = (len_x - i - 1); } - } + } } break; - case 5: ; + case 5: { float32_t max_val32 = ((float32_t*)x_bytes)[0]; for (c3_d i = 0; i < len_x; i++) { if(f32_gt(((float32_t*)x_bytes)[i], max_val32)) { max_val32 = ((float32_t*)x_bytes)[i]; max_idx = (len_x - i - 1); } - } + } } break; - case 6: ; + case 6: { float64_t max_val64 = ((float64_t*)x_bytes)[0]; for (c3_d i = 0; i < len_x; i++) { if(f64_gt(((float64_t*)x_bytes)[i], max_val64)) { max_val64 = ((float64_t*)x_bytes)[i]; max_idx = (len_x - i - 1); } - } + } } break; - case 7: ; + case 7: { float128_t max_val128 = ((float128_t*)x_bytes)[0]; for (c3_d i = 0; i < len_x; i++) { if(f128M_gt(&(((float128_t*)x_bytes)[i]), &max_val128)) { max_val128 = *f128M_max(&max_val128, &((float128_t*)x_bytes)[i]); max_idx = (len_x - i - 1); } - } + } } break; } @@ -1744,12 +1759,12 @@ } // Assert length of dims is 2. if (u3qb_lent(shape) != 2) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } // Unpack shape into an array of dimensions. c3_d *dims = _get_dims(shape); if (dims[0] != dims[1]) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } // Unpack the data as a byte array. We assume total length < 2**64. @@ -1791,7 +1806,7 @@ { // Assert length of dims is 2. if (u3qb_lent(shape) != 2) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } // Unpack shape into an array of dimensions. c3_d *dims = _get_dims(shape); @@ -2044,10 +2059,15 @@ c3_d Nb= u3x_atom(u3h(y_shape)); c3_d P = u3x_atom(u3h(u3t(y_shape))); + // Fence on valid bloq size. + if (bloq < 4 || bloq > 7) { + return u3_none; + } + if ((u3_nul != u3t(u3t(x_shape))) || (u3_nul != u3t(u3t(y_shape))) || (Na != Nb)) { - return u3m_bail(c3__exit); + return u3_none; } c3_d N = Na; @@ -2133,7 +2153,7 @@ c3n == u3ud(x_data) || c3n == u3ud(y_data) ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp, y_shape, y_bloq, y_kind, y_fxp, @@ -2157,12 +2177,12 @@ // fxp does not need to match here so no check ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { switch (x_kind) { case c3__real: _set_rounding(rnd); - u3_noun r_data = u3qi_la_add_real(x_data, y_data, x_shape, x_bloq); + u3_noun r_data = _soft_run(u3qi_la_add_real(x_data, y_data, x_shape, x_bloq)); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2188,7 +2208,7 @@ c3n == u3ud(x_data) || c3n == u3ud(y_data) ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp, y_shape, y_bloq, y_kind, y_fxp, @@ -2212,12 +2232,12 @@ // fxp does not need to match here so no check ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { switch (x_kind) { case c3__real: _set_rounding(rnd); - u3_noun r_data = u3qi_la_sub_real(x_data, y_data, x_shape, x_bloq); + u3_noun r_data = _soft_run(u3qi_la_sub_real(x_data, y_data, x_shape, x_bloq)); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2243,7 +2263,7 @@ c3n == u3ud(x_data) || c3n == u3ud(y_data) ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp, y_shape, y_bloq, y_kind, y_fxp, @@ -2267,12 +2287,12 @@ // fxp does not need to match here so no check ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { switch (x_kind) { case c3__real: _set_rounding(rnd); - u3_noun r_data = u3qi_la_mul_real(x_data, y_data, x_shape, x_bloq); + u3_noun r_data = _soft_run(u3qi_la_mul_real(x_data, y_data, x_shape, x_bloq)); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2298,7 +2318,7 @@ c3n == u3ud(x_data) || c3n == u3ud(y_data) ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp, y_shape, y_bloq, y_kind, y_fxp, @@ -2322,12 +2342,12 @@ // fxp does not need to match here so no check ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { switch (x_kind) { case c3__real: _set_rounding(rnd); - u3_noun r_data = u3qi_la_div_real(x_data, y_data, x_shape, x_bloq); + u3_noun r_data = _soft_run(u3qi_la_div_real(x_data, y_data, x_shape, x_bloq)); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2353,7 +2373,7 @@ c3n == u3ud(x_data) || c3n == u3ud(y_data) ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp, y_shape, y_bloq, y_kind, y_fxp, @@ -2377,12 +2397,12 @@ // fxp does not need to match here so no check ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { switch (x_kind) { case c3__real: _set_rounding(rnd); - u3_noun r_data = u3qi_la_mod_real(x_data, y_data, x_shape, x_bloq); + u3_noun r_data = _soft_run(u3qi_la_mod_real(x_data, y_data, x_shape, x_bloq)); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2404,7 +2424,7 @@ 0) || c3n == u3ud(x_data) ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp, rnd; @@ -2417,12 +2437,12 @@ c3n == u3ud(x_kind) ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { switch (x_kind) { case c3__real: _set_rounding(rnd); - u3_noun r_data = u3qi_la_cumsum_real(x_data, x_shape, x_bloq); + u3_noun r_data = _soft_run(u3qi_la_cumsum_real(x_data, x_shape, x_bloq)); return u3nc(u3nq(u3nt(0x1, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2444,7 +2464,7 @@ 0) || c3n == u3ud(x_data) ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp; x_shape = u3h(x_meta); // 2 @@ -2455,7 +2475,7 @@ c3n == u3ud(x_kind) ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { switch (x_kind) { case c3__real: ; @@ -2482,7 +2502,7 @@ 0) || c3n == u3ud(x_data) ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp; x_shape = u3h(x_meta); // 2 @@ -2492,11 +2512,11 @@ c3n == u3ud(x_kind) ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { switch (x_kind) { case c3__real: ; - u3_noun r_data = u3qi_la_ravel_real(x_data, x_shape, x_bloq); + u3_noun r_data = _soft_run(u3qi_la_ravel_real(x_data, x_shape, x_bloq)); // (list @) return r_data; @@ -2519,7 +2539,7 @@ 0) || c3n == u3ud(x_data) ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp; x_shape = u3h(x_meta); // 2 @@ -2530,7 +2550,7 @@ c3n == u3ud(x_kind) ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { switch (x_kind) { case c3__real: ; @@ -2557,7 +2577,7 @@ 0) || c3n == u3ud(x_data) ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp; x_shape = u3h(x_meta); // 2 @@ -2568,11 +2588,11 @@ c3n == u3ud(x_kind) ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { switch (x_kind) { case c3__real: ; - u3_noun r_data = u3qi_la_min_real(x_data, x_shape, x_bloq); + u3_noun r_data = _soft_run(u3qi_la_min_real(x_data, x_shape, x_bloq)); return u3nc(u3nq(u3nt(0x1, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2594,7 +2614,7 @@ 0) || c3n == u3ud(x_data) ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp; x_shape = u3h(x_meta); // 2 @@ -2605,11 +2625,11 @@ c3n == u3ud(x_kind) ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { switch (x_kind) { case c3__real: ; - u3_noun r_data = u3qi_la_max_real(x_data, x_shape, x_bloq); + u3_noun r_data = _soft_run(u3qi_la_max_real(x_data, x_shape, x_bloq)); return u3nc(u3nq(u3nt(0x1, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2631,7 +2651,7 @@ 0) || c3n == u3ud(x_data) ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp; x_shape = u3h(x_meta); // 2 @@ -2642,11 +2662,11 @@ c3n == u3ud(x_kind) ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { switch (x_kind) { case c3__real: ; - u3_noun r_data = u3qi_la_abs_real(x_data, x_shape, x_bloq); + u3_noun r_data = _soft_run(u3qi_la_abs_real(x_data, x_shape, x_bloq)); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2672,7 +2692,7 @@ c3n == u3ud(x_data) || c3n == u3ud(y_data) ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp, y_shape, y_bloq, y_kind, y_fxp, @@ -2695,11 +2715,11 @@ c3n == u3r_sing(x_fxp, y_fxp) ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { switch (x_kind) { case c3__real: ; - u3_noun r_data = u3qi_la_gth_real(x_data, y_data, x_shape, x_bloq); + u3_noun r_data = _soft_run(u3qi_la_gth_real(x_data, y_data, x_shape, x_bloq)); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2725,7 +2745,7 @@ c3n == u3ud(x_data) || c3n == u3ud(y_data) ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp, y_shape, y_bloq, y_kind, y_fxp, @@ -2748,11 +2768,11 @@ c3n == u3r_sing(x_fxp, y_fxp) ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { switch (x_kind) { case c3__real: ; - u3_noun r_data = u3qi_la_gte_real(x_data, y_data, x_shape, x_bloq); + u3_noun r_data = _soft_run(u3qi_la_gte_real(x_data, y_data, x_shape, x_bloq)); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2778,7 +2798,7 @@ c3n == u3ud(x_data) || c3n == u3ud(y_data) ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp, y_shape, y_bloq, y_kind, y_fxp, @@ -2801,11 +2821,11 @@ c3n == u3r_sing(x_fxp, y_fxp) ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { switch (x_kind) { case c3__real: ; - u3_noun r_data = u3qi_la_lth_real(x_data, y_data, x_shape, x_bloq); + u3_noun r_data = _soft_run(u3qi_la_lth_real(x_data, y_data, x_shape, x_bloq)); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2831,7 +2851,7 @@ c3n == u3ud(x_data) || c3n == u3ud(y_data) ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp, y_shape, y_bloq, y_kind, y_fxp, @@ -2854,11 +2874,11 @@ c3n == u3r_sing(x_fxp, y_fxp) ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { switch (x_kind) { case c3__real: ; - u3_noun r_data = u3qi_la_lte_real(x_data, y_data, x_shape, x_bloq); + u3_noun r_data = _soft_run(u3qi_la_lte_real(x_data, y_data, x_shape, x_bloq)); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2882,7 +2902,7 @@ c3n == u3ud(x_data) || c3n == u3ud(n) ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp, rnd; @@ -2894,7 +2914,7 @@ switch (x_kind) { case c3__real: _set_rounding(rnd); - u3_noun r_data = u3qi_la_adds_real(x_data, n, x_shape, x_bloq); + u3_noun r_data = _soft_run(u3qi_la_adds_real(x_data, n, x_shape, x_bloq)); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2917,7 +2937,7 @@ c3n == u3ud(x_data) || c3n == u3ud(n) ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp, rnd; @@ -2929,7 +2949,7 @@ switch (x_kind) { case c3__real: _set_rounding(rnd); - u3_noun r_data = u3qi_la_subs_real(x_data, n, x_shape, x_bloq); + u3_noun r_data = _soft_run(u3qi_la_subs_real(x_data, n, x_shape, x_bloq)); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2952,7 +2972,7 @@ c3n == u3ud(x_data) || c3n == u3ud(n) ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp, rnd; @@ -2964,7 +2984,7 @@ switch (x_kind) { case c3__real: _set_rounding(rnd); - u3_noun r_data = u3qi_la_muls_real(x_data, n, x_shape, x_bloq); + u3_noun r_data = _soft_run(u3qi_la_muls_real(x_data, n, x_shape, x_bloq)); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2987,7 +3007,7 @@ c3n == u3ud(x_data) || c3n == u3ud(n) ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp, rnd; @@ -2999,7 +3019,7 @@ switch (x_kind) { case c3__real: _set_rounding(rnd); - u3_noun r_data = u3qi_la_divs_real(x_data, n, x_shape, x_bloq); + u3_noun r_data = _soft_run(u3qi_la_divs_real(x_data, n, x_shape, x_bloq)); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -3022,7 +3042,7 @@ c3n == u3ud(x_data) || c3n == u3ud(n) ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp, rnd; @@ -3034,7 +3054,7 @@ switch (x_kind) { case c3__real: _set_rounding(rnd); - u3_noun r_data = u3qi_la_mods_real(x_data, n, x_shape, x_bloq); + u3_noun r_data = _soft_run(u3qi_la_mods_real(x_data, n, x_shape, x_bloq)); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -3059,7 +3079,7 @@ c3n == u3ud(x_data) || c3n == u3ud(y_data) ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp, y_shape, y_bloq, y_kind, y_fxp, @@ -3083,12 +3103,12 @@ c3n == u3r_sing(x_fxp, y_fxp) ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { switch (x_kind) { case c3__real: _set_rounding(rnd); - u3_noun r_data = u3qi_la_dot_real(x_data, y_data, x_shape, x_bloq); + u3_noun r_data = _soft_run(u3qi_la_dot_real(x_data, y_data, x_shape, x_bloq)); c3_d len_x0 = _get_dims(x_shape)[0]; return u3nc(u3nq(u3nt(len_x0, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); @@ -3111,7 +3131,7 @@ 0) || c3n == u3ud(x_data) ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp; x_shape = u3h(x_meta); // 2 @@ -3122,7 +3142,7 @@ c3n == u3ud(x_kind) ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { u3_noun r_data = u3qi_la_transpose(x_data, x_shape, x_bloq); return u3nc(u3nq(u3nt(u3k(u3h(x_shape)), u3k(u3h(u3t(x_shape))), u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); @@ -3142,7 +3162,7 @@ u3x_sam_7, &n, 0)) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp; x_shape = u3h(x_meta); // 2 @@ -3154,12 +3174,12 @@ c3n == u3ud(x_kind) ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { switch (x_kind) { case c3__real: _set_rounding(rnd); - u3_noun r_data = u3qi_la_linspace_real(a, b, n, x_bloq); + u3_noun r_data = _soft_run(u3qi_la_linspace_real(a, b, n, x_bloq)); x_shape = u3nt(u3x_atom(n), 0x1, u3_nul); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); @@ -3182,7 +3202,7 @@ u3x_sam_7, &d, 0)) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp; x_shape = u3h(x_meta); // 2 @@ -3194,12 +3214,12 @@ c3n == u3ud(x_kind) ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { switch (x_kind) { case c3__real: _set_rounding(rnd); - u3_noun r_data = u3qi_la_range_real(a, b, d, x_bloq); + u3_noun r_data = _soft_run(u3qi_la_range_real(a, b, d, x_bloq)); c3_d a_, b_, d_; c3_ds n_; switch (x_bloq) { @@ -3254,7 +3274,7 @@ 0) || c3n == u3ud(x_data) ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp; x_shape = u3h(x_meta); // 2 @@ -3265,9 +3285,9 @@ c3n == u3ud(x_kind) ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { - u3_noun r_data = u3qi_la_diag(x_data, x_shape, x_bloq); + u3_noun r_data = _soft_run(u3qi_la_diag(x_data, x_shape, x_bloq)); c3_d len_x0 = _get_dims(x_shape)[0]; return u3nc(u3nq(u3nt(len_x0, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); } @@ -3286,7 +3306,7 @@ 0) || c3n == u3ud(x_data) ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp; if ( c3n == u3r_mean(x_meta, @@ -3297,11 +3317,11 @@ 0) ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { switch (x_kind) { case c3__real: ; - u3_noun r_data = u3qi_la_trace_real(x_data, x_shape, x_bloq); + u3_noun r_data = _soft_run(u3qi_la_trace_real(x_data, x_shape, x_bloq)); return u3nc(u3nq(u3nt(0x1, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -3327,7 +3347,7 @@ c3n == u3ud(x_data) || c3n == u3ud(y_data) ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp, y_shape, y_bloq, y_kind, y_fxp, @@ -3346,12 +3366,12 @@ // fxp does not need to match so no check ) { - return u3m_bail(c3__exit); + u3m_bail(c3__exit); } else { switch (x_kind) { case c3__real: _set_rounding(rnd); - u3_noun r_data = u3qi_la_mmul_real(x_data, y_data, x_shape, y_shape, x_bloq); + u3_noun r_data = _soft_run(u3qi_la_mmul_real(x_data, y_data, x_shape, y_shape, x_bloq)); // result is already [meta data] return r_data; diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index c9118c9a4b..48ef0db9f6 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -2220,7 +2220,7 @@ static u3j_core _139_non_d[] = }; static u3j_core _139_hex_d[] = -{ { "sep", 7, 0, _139_non_d, no_hashes }, +{ { "non", 7, 0, _139_non_d, no_hashes }, { "lore", 63, _140_hex_lore_a, 0, no_hashes }, { "leer", 63, _140_hex_leer_a, 0, no_hashes }, From 3f5c6d613aaea345d0fc08105fc2cef4eac4869c Mon Sep 17 00:00:00 2001 From: Quodss Date: Wed, 8 Nov 2023 16:48:09 +0100 Subject: [PATCH 130/430] modernize mass --- pkg/vere/lord.c | 31 +++++++++++++++++++++++++++++++ pkg/vere/main.c | 5 ++++- pkg/vere/serf.c | 20 ++++++++++++++++---- pkg/vere/serf.h | 2 +- 4 files changed, 52 insertions(+), 6 deletions(-) diff --git a/pkg/vere/lord.c b/pkg/vere/lord.c index a63824ae9b..da739f838d 100644 --- a/pkg/vere/lord.c +++ b/pkg/vere/lord.c @@ -521,6 +521,32 @@ _lord_plea_play(u3_lord* god_u, u3_noun dat) u3z(dat); } +/* _lord_plea_mass(): inject mass report + */ +static void +_lord_plea_mass(u3_lord* god_u, u3_noun dat) +{ + u3_noun cad = u3nc(c3__quac, dat); + u3_noun wir = u3nc(c3__quac, u3_nul); + u3_ovum* egg_u = u3_ovum_init(0, c3__k, wir, cad); + + u3_pier* pir_u = god_u->cb_u.ptr_v; + u3_auto* car_u = c3_calloc(sizeof(*car_u)); + u3_noun ovo; + + car_u->pir_u = pir_u; + car_u->nam_m = c3__quac; // is that right? I did it by analogy w/ smth + + u3_auto_plan(car_u, egg_u); + + u3_assert( u3_auto_next(car_u, &ovo) == egg_u ); + + { + struct timeval tim_tv; + gettimeofday(&tim_tv, 0); + u3_lord_work(god_u, egg_u, u3nc(u3_time_in_tv(&tim_tv), ovo)); + } +} /* _lord_work_spin(): update spinner if more work is in progress. */ static void @@ -742,6 +768,11 @@ _lord_on_plea(void* ptr_v, c3_d len_d, c3_y* byt_y) case c3__ripe: { _lord_plea_ripe(god_u, u3k(dat)); } break; + + case c3__quac: { + _lord_plea_mass(god_u, u3k(dat)); + + } break; } u3z(jar); diff --git a/pkg/vere/main.c b/pkg/vere/main.c index 2222f87dd1..012f4afdfa 100644 --- a/pkg/vere/main.c +++ b/pkg/vere/main.c @@ -1041,7 +1041,10 @@ _cw_serf_writ(void* vod_p, c3_d len_d, c3_y* byt_y) // all references must now be counted, and all roots recorded // - u3_serf_post(&u3V); + u3_weak serf_post_out = u3_serf_post(&u3V); + if (serf_post_out != u3_none) { + _cw_serf_send(u3nc(c3__quac, serf_post_out)); + } } } diff --git a/pkg/vere/serf.c b/pkg/vere/serf.c index cec8bece55..2304a926da 100644 --- a/pkg/vere/serf.c +++ b/pkg/vere/serf.c @@ -32,6 +32,7 @@ :: next steps: +$ plea $% [%live ~] [%ripe [pro=%1 hon=@ nok=@] eve=@ mug=@] + [%quac p=(unit *)] [%slog pri=@ tank] [%flog cord] $: %peek @@ -72,13 +73,15 @@ enum { /* _serf_grab(): garbage collect, checking for profiling. RETAIN. */ -static void +static u3_weak _serf_grab(u3_noun sac) { + u3_noun out = u3_none; if ( u3_nul == sac) { if ( u3C.wag_w & (u3o_debug_ram | u3o_check_corrupt) ) { u3m_grab(sac, u3_none); } + return u3_none; } else { c3_w tot_w = 0; @@ -134,6 +137,8 @@ _serf_grab(u3_noun sac) u3z(sac); u3l_log(""); + + return u3i_word(tot_w * 4); } } @@ -174,11 +179,13 @@ u3_serf_grab(void) } fprintf(stderr, "serf: measuring memory:\r\n"); - + fprintf(stderr, "BEFORE sac FORK:\r\n"); if ( u3_nul != sac ) { + printf("enter _serf_grab\r\n"); _serf_grab(sac); } else { + fprintf(stderr, "sac is empty\r\n"); u3a_print_memory(stderr, "total marked", u3m_mark(stderr)); u3a_print_memory(stderr, "free lists", u3a_idle(u3R)); u3a_print_memory(stderr, "sweep", u3a_sweep()); @@ -190,9 +197,10 @@ u3_serf_grab(void) /* u3_serf_post(): update serf state post-writ. */ -void +u3_weak u3_serf_post(u3_serf* sef_u) { + u3_noun out = u3_none; if ( sef_u->fag_w & _serf_fag_hit1 ) { if ( u3C.wag_w & u3o_verbose ) { u3l_log("serf: threshold 1: %u", u3h_wyt(u3R->cax.per_p)); @@ -213,8 +221,11 @@ u3_serf_post(u3_serf* sef_u) // XX this runs on replay too, |mass s/b elsewhere // if ( sef_u->fag_w & _serf_fag_mute ) { - _serf_grab(sef_u->sac); + u3_weak grab_mass = _serf_grab(sef_u->sac); sef_u->sac = u3_nul; + if (grab_mass != u3_none) { + out = u3nc(u3_nul, grab_mass); + } } if ( sef_u->fag_w & _serf_fag_hit0 ) { @@ -232,6 +243,7 @@ u3_serf_post(u3_serf* sef_u) } sef_u->fag_w = _serf_fag_none; + return out; } /* _serf_curb(): check for memory threshold diff --git a/pkg/vere/serf.h b/pkg/vere/serf.h index 7cd2ca47d0..bfd86e5765 100644 --- a/pkg/vere/serf.h +++ b/pkg/vere/serf.h @@ -51,7 +51,7 @@ /* u3_serf_post(): update serf state post-writ. */ - void + u3_weak u3_serf_post(u3_serf* sef_u); /* u3_serf_grab(): garbage collect. From b53f86af59d1481ea95bbb1560ad3f3bb695bd2b Mon Sep 17 00:00:00 2001 From: pkova Date: Thu, 30 May 2024 16:48:54 +0300 Subject: [PATCH 131/430] mesa: do not send anything in hunk callback --- pkg/vere/io/mesa.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 191d0f316d..cbc369a5dd 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1648,11 +1648,11 @@ _mesa_page_scry_hunk_cb(void* vod_p, u3_noun nun) while ( u3_nul != hit ) { // u3_noun key = u3nc(u3k(pax), u3i_word(lop_w)); // u3h_put(sam_u->fin_s.sac_p, key, u3k(u3h(lis))); - if (fra_w == len_w && tag == MESA_WAIT) { - c3_w lun_w = _mesa_respond(pic_u, &buf_y, u3k(u3h(hit))); - pac_u->pek_u.nam_u.fra_w = fra_w; - _mesa_rout_bufs(sam_u, buf_y, lun_w, u3k(u3t(dat))); - } + /* if (fra_w == len_w && tag == MESA_WAIT) { */ + /* c3_w lun_w = _mesa_respond(pic_u, &buf_y, u3k(u3h(hit))); */ + /* pac_u->pek_u.nam_u.fra_w = fra_w; */ + /* _mesa_rout_bufs(sam_u, buf_y, lun_w, u3k(u3t(dat))); */ + /* } */ pac_u->pek_u.nam_u.fra_w = len_w; /* u3l_log("putting %u", pac_u->pek_u.nam_u.fra_w); */ From 4fa51b44c3093e9b70a501590ca13fb9387d8bd8 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Mon, 3 Jun 2024 15:47:50 +0200 Subject: [PATCH 132/430] ames, mesa: unify driver complete --- pkg/vere/io/ames.c | 67 +++++++++++++++++++++++++---------------- pkg/vere/io/mesa.c | 44 ++++++++++++++++++++------- pkg/vere/io/mesa/mesa.h | 2 +- pkg/vere/vere.h | 1 + 4 files changed, 76 insertions(+), 38 deletions(-) diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index 0e369eb649..4fbdcfdd3f 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -1422,23 +1422,29 @@ _stun_make_response(const c3_y req_y[20], } static void -_stun_on_request(u3_ames* sam_u, - const c3_y* req_y, - const struct sockaddr* adr_u) +_stun_on_request(u3_ames* sam_u, + const c3_y* req_y, + u3_lane lan_u) { _stun_send* snd_u = c3_malloc(sizeof(*snd_u) + 40); snd_u->sam_u = sam_u; - struct sockaddr_in* add_u = (struct sockaddr_in*)adr_u; - u3_lane lan_u = { - .por_s = ntohs(add_u->sin_port), - .pip_w = ntohl(add_u->sin_addr.s_addr) - }; + struct sockaddr_in add_u; + memset(&add_u, 0, sizeof(add_u)); + add_u.sin_family = AF_INET; + add_u.sin_addr.s_addr = htonl(lan_u.pip_w); + add_u.sin_port = htons(lan_u.por_s); + + // struct sockaddr_in* add_u = (struct sockaddr_in*)adr_u; + // u3_lane lan_u = { + // .por_s = ntohs(add_u->sin_port), + // .pip_w = ntohl(add_u->sin_addr.s_addr) + // }; _stun_make_response(req_y, &lan_u, snd_u->hun_y); uv_buf_t buf_u = uv_buf_init((c3_c*)snd_u->hun_y, 40); c3_i sas_i = uv_udp_send(&snd_u->req_u, &sam_u->wax_u, - &buf_u, 1, adr_u, _stun_send_response_cb); + &buf_u, 1, (struct sockaddr *)&add_u, _stun_send_response_cb); if ( sas_i != 0 ) { u3l_log("stun: send response fail_sync: %s", uv_strerror(sas_i)); @@ -2591,12 +2597,31 @@ _ames_try_forward(u3_pact* pac_u) ** trigger printfs suggesting upgrade. ** they cannot be filtered, as we do not know their semantics */ -static void +void _ames_hear(u3_ames* sam_u, u3_lane* lan_u, c3_w len_w, c3_y* hun_y) { + + // XX reorg, check if a STUN req/resp can look like an ames packet + // check the mug hash of the body of the packet, if not check if STUN + // otherwise , invalid packet, log failure + // check ames first, assume that STUN could maybe (not likely) overlap with ames + // for next protocol version, have an urbit cookie + // + if (_stun_is_request(hun_y, len_w) == c3y) { + _stun_on_request(sam_u, hun_y, *lan_u); + c3_free(hun_y); + return; + } + else if (_stun_is_our_response(hun_y, sam_u->sun_u.tid_y, len_w) + == c3y) { + _stun_on_response(sam_u, hun_y, len_w); + c3_free(hun_y); + return; + } + u3_pact* pac_u; c3_w pre_w; c3_w cur_w = 0; // cursor: how many bytes we've read from hun_y @@ -2737,21 +2762,6 @@ _ames_recv_cb(uv_udp_t* wax_u, } c3_free(buf_u->base); } - // XX reorg, check if a STUN req/resp can look like an ames packet - // check the mug hash of the body of the packet, if not check if STUN - // otherwise , invalid packet, log failure - // check ames first, assume that STUN could maybe (not likely) overlap with ames - // for next protocol version, have an urbit cookie - // - else if (_stun_is_request((c3_y*)buf_u->base, nrd_i) == c3y) { - _stun_on_request(sam_u, (c3_y *)buf_u->base, adr_u); - c3_free(buf_u->base); - } - else if (_stun_is_our_response((c3_y*)buf_u->base, sam_u->sun_u.tid_y, nrd_i) - == c3y) { - _stun_on_response(sam_u, (c3_y*)buf_u->base, nrd_i); - c3_free(buf_u->base); - } else { struct sockaddr_in* add_u = (struct sockaddr_in*)adr_u; u3_lane lan_u = { @@ -2994,6 +3004,8 @@ _ames_io_talk(u3_auto* car_u) // (or some other reconfig) effect when it is reset. // u3_noun gang = u3nc(u3_nul, u3_nul); + // XX drop this; done at another level + // u3_pier_peek_last(car_u->pir_u, gang, c3__ax, u3_nul, u3nt(u3i_string("protocol"), u3i_string("version"), u3_nul), sam_u, _ames_prot_scry_cb); @@ -3001,7 +3013,7 @@ _ames_io_talk(u3_auto* car_u) /* _ames_kick_newt(): apply packet network outputs. */ -static c3_o +c3_o _ames_kick_newt(u3_ames* sam_u, u3_noun tag, u3_noun dat) { c3_o ret_o; @@ -3035,6 +3047,7 @@ _ames_kick_newt(u3_ames* sam_u, u3_noun tag, u3_noun dat) sam_u->nal_o = c3y; ret_o = c3y; } break; + } u3z(tag); u3z(dat); @@ -3290,5 +3303,7 @@ u3_ames_io_init(u3_pier* pir_u) u3z(now); } + // XX declare void pointer to u3_host and add sam_u in it + u3_Host.sam_u = sam_u; return car_u; } diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index cbc369a5dd..54b546ec66 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -882,7 +882,7 @@ _try_resend(u3_pend_req* req_u) } static void -_mesa_packet_timeout(uv_timer_t* tim_u); +_mesa_packet_timeout(uv_timer_t* tim_u); static void _update_oldest_req(u3_pend_req *req_u, u3_gage* gag_u) @@ -1288,6 +1288,9 @@ _mesa_ef_send(u3_mesa* sam_u, u3_noun las, u3_noun pac) u3z(pac); } +c3_o +_ames_kick_newt(void* sam_u, u3_noun tag, u3_noun dat); + static c3_o _mesa_kick(u3_mesa* sam_u, u3_noun tag, u3_noun dat) { c3_o ret_o; @@ -1307,6 +1310,13 @@ static c3_o _mesa_kick(u3_mesa* sam_u, u3_noun tag, u3_noun dat) ret_o = c3y; } } break; + case c3__send: + case c3__turf: + case c3__saxo: + case c3__nail: { + + ret_o = _ames_kick_newt(u3_Host.sam_u, u3k(tag), u3k(dat)); + } break; } // technically losing tag is unncessary as it always should @@ -1535,6 +1545,7 @@ static u3_weak _mesa_get_cache(u3_mesa* sam_u, u3_mesa_name* nam_u) { u3_noun pax = _name_to_scry(nam_u); + u3m_p("_mesa_get_cache pax", pax); u3_weak res = u3h_get(sam_u->pac_p, pax); if ( u3_none == res ) { //u3m_p("miss", u3k(pax)); @@ -1548,7 +1559,7 @@ static void _mesa_put_cache(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_noun val) { u3_noun pax = _name_to_scry(nam_u); - + u3m_p("_mesa_put_cache pax", pax); u3h_put(sam_u->pac_p, pax, u3k(val)); u3z(pax); // TODO: fix refcount } @@ -1648,14 +1659,16 @@ _mesa_page_scry_hunk_cb(void* vod_p, u3_noun nun) while ( u3_nul != hit ) { // u3_noun key = u3nc(u3k(pax), u3i_word(lop_w)); // u3h_put(sam_u->fin_s.sac_p, key, u3k(u3h(lis))); - /* if (fra_w == len_w && tag == MESA_WAIT) { */ - /* c3_w lun_w = _mesa_respond(pic_u, &buf_y, u3k(u3h(hit))); */ - /* pac_u->pek_u.nam_u.fra_w = fra_w; */ - /* _mesa_rout_bufs(sam_u, buf_y, lun_w, u3k(u3t(dat))); */ - /* } */ + // if (fra_w == len_w && tag == MESA_WAIT) { + // c3_w lun_w = _mesa_respond(pic_u, &buf_y, u3k(u3h(hit))); + // pac_u->pek_u.nam_u.fra_w = fra_w; + // _mesa_rout_bufs(sam_u, buf_y, lun_w, u3k(u3t(dat))); + // } pac_u->pek_u.nam_u.fra_w = len_w; /* u3l_log("putting %u", pac_u->pek_u.nam_u.fra_w); */ + _log_pact(pac_u); + u3l_log("_mesa_put_cache loop %s", pac_u->pek_u.nam_u.pat_c); _mesa_put_cache(sam_u, &pac_u->pek_u.nam_u, u3nc(MESA_ITEM, u3k(u3h(hit)))); // u3z(key); @@ -1866,7 +1879,7 @@ static void _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) { #ifdef MESA_DEBUG - /* u3l_log("mesa hear page %u", pic_u->pac_u.pag_u.nam_u.fra_w); */ + u3l_log("mesa hear page %u", pic_u->pac_u.pag_u.nam_u.fra_w); #endif u3_mesa* sam_u = pic_u->sam_u; u3_mesa_pact* pac_u = &pic_u->pac_u; @@ -1961,6 +1974,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) u3_pend_req* req_u; if ( pac_u->pek_u.nam_u.nit_o == c3y ) { + u3l_log("_mesa_req_pact_init NIT"); req_u = _mesa_req_pact_init(sam_u, pic_u, &lan_u); if ( req_u == NULL ) { _mesa_free_pict(pic_u); @@ -2103,7 +2117,8 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) c3_w bat_w = _mesa_lop(fra_w); pac_u->pek_u.nam_u.fra_w = bat_w; - + _log_pact(pac_u); + u3l_log("_mesa_hear_peek %s", pac_u->pek_u.nam_u.pat_c); u3_weak hit = _mesa_get_cache(sam_u, &pac_u->pek_u.nam_u); /* u3l_log("peek fra %u hit %u", fra_w, hit != u3_none); */ @@ -2273,6 +2288,12 @@ _mesa_hear_poke(u3_mesa_pict* pic_u, u3_lane* lan_u) ptr_v, nes_f, bal_f); } +void +_ames_hear(void* sam_u, + u3_lane* lan_u, + c3_w len_w, + c3_y* hun_y); + static void _mesa_hear(u3_mesa* sam_u, u3_lane* lan_u, @@ -2294,9 +2315,10 @@ _mesa_hear(u3_mesa* sam_u, c3_free(hun_y); if ( lin_w == 0 ) { - MESA_LOG(SERIAL) - c3_free(hun_y); + // MESA_LOG(SERIAL) + // c3_free(hun_y); mesa_free_pact(&pic_u->pac_u); + _ames_hear(u3_Host.sam_u, lan_u, len_w, hun_y); return; } diff --git a/pkg/vere/io/mesa/mesa.h b/pkg/vere/io/mesa/mesa.h index 64e8d4dd49..52c7a65cda 100644 --- a/pkg/vere/io/mesa/mesa.h +++ b/pkg/vere/io/mesa/mesa.h @@ -156,6 +156,6 @@ c3_w mesa_etch_pact(c3_y* buf_y, u3_mesa_pact* pac_u); void mesa_free_pact(u3_mesa_pact* pac_u); -void log_pact(u3_mesa_pact* pac_u); +void _log_pact(u3_mesa_pact* pac_u); #endif diff --git a/pkg/vere/vere.h b/pkg/vere/vere.h index 3916859a78..a81e2d91ed 100644 --- a/pkg/vere/vere.h +++ b/pkg/vere/vere.h @@ -340,6 +340,7 @@ c3_o pep_o; // prep for upgrade c3_i xit_i; // exit code for shutdown void (*bot_f)(); // call when chis is up + void* sam_u; // XX } u3_host; // host == computer == process /** Pier system. From c635c1d7d96ba1819990465526273f38299b885c Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Tue, 4 Jun 2024 05:59:27 +0200 Subject: [PATCH 133/430] ames, mesa: call uv_udp_send in _ames_send --- pkg/vere/io/ames.c | 6 +++--- pkg/vere/io/mesa.c | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index 4fbdcfdd3f..a50054ff00 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -904,17 +904,17 @@ _ames_send(u3_pact* pac_u) add_u.sin_addr.s_addr = htonl(pac_u->rut_u.lan_u.pip_w); add_u.sin_port = htons(pac_u->rut_u.lan_u.por_s); - //u3l_log("_ames_send %s %u", _str_typ(pac_u->typ_y), + // u3l_log("_ames_send %s %u", _str_typ(pac_u->typ_y), // pac_u->rut_u.lan_u.por_s); { uv_buf_t buf_u = uv_buf_init((c3_c*)pac_u->hun_y, pac_u->len_w); - c3_i sas_i = 0; /*uv_udp_send(&pac_u->snd_u, + c3_i sas_i = uv_udp_send(&pac_u->snd_u, &sam_u->wax_u, &buf_u, 1, (const struct sockaddr*)&add_u, - _ames_send_cb); */ + _ames_send_cb); if ( sas_i ) { if ( c3y == sam_u->fig_u.net_o ) { diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 54b546ec66..3173f96d93 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1302,10 +1302,10 @@ static c3_o _mesa_kick(u3_mesa* sam_u, u3_noun tag, u3_noun dat) case c3__push: { u3_noun las, pac; if ( c3n == u3r_cell(dat, &las, &pac) ) { - u3l_log(" vere: send no"); + u3l_log(" mesa: send no"); ret_o = c3n; } else { - u3l_log(" vere: send yes"); + u3l_log(" mesa: send yes"); _mesa_ef_send(sam_u, u3k(las), u3k(pac)); ret_o = c3y; } @@ -1314,7 +1314,6 @@ static c3_o _mesa_kick(u3_mesa* sam_u, u3_noun tag, u3_noun dat) case c3__turf: case c3__saxo: case c3__nail: { - ret_o = _ames_kick_newt(u3_Host.sam_u, u3k(tag), u3k(dat)); } break; } @@ -2313,7 +2312,6 @@ _mesa_hear(u3_mesa* sam_u, c3_w lin_w = mesa_sift_pact(&pic_u->pac_u, hun_y, len_w); - c3_free(hun_y); if ( lin_w == 0 ) { // MESA_LOG(SERIAL) // c3_free(hun_y); @@ -2322,6 +2320,8 @@ _mesa_hear(u3_mesa* sam_u, return; } + c3_free(hun_y); + switch ( pic_u->pac_u.hed_u.typ_y ) { case PACT_PEEK: { _mesa_hear_peek(pic_u, *lan_u); From bfd29c0791e5a523fee7639680c6fd2310c2184f Mon Sep 17 00:00:00 2001 From: nathanlever Date: Tue, 4 Jun 2024 19:02:50 +0300 Subject: [PATCH 134/430] fix leading-zero placement --- pkg/noun/jets/e/crc32.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/noun/jets/e/crc32.c b/pkg/noun/jets/e/crc32.c index 84e91ae86e..efc865d603 100644 --- a/pkg/noun/jets/e/crc32.c +++ b/pkg/noun/jets/e/crc32.c @@ -27,13 +27,14 @@ u3qe_crc32(u3_noun input_octs) c3_w leading_zeros = head - len_w; c3_w crc = 0L; + crc = crc32(crc, input, len_w); + while (leading_zeros > 0) { c3_y lz_input = 0; crc = crc32(crc, &lz_input, 1); leading_zeros--; } - crc = crc32(crc, input, len_w); return u3i_word(crc); } From 04b29a12e4bd6770b67dff73725ada013fd0a8d0 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Wed, 5 Jun 2024 12:44:38 +0200 Subject: [PATCH 135/430] mesa: correct ip in _mesa_send_buf --- pkg/vere/io/mesa.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 3173f96d93..09342a4074 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -787,15 +787,15 @@ static void _mesa_send_buf(u3_mesa* sam_u, u3_lane lan_u, c3_y* buf_y, c3_w len_ memset(&add_u, 0, sizeof(add_u)); add_u.sin_family = AF_INET; - c3_w pip_w = c3n == u3_Host.ops_u.net ? lan_u.pip_w : 0x7f000001; + c3_w pip_w = c3y == u3_Host.ops_u.net ? lan_u.pip_w : 0x7f000001; c3_s por_s = lan_u.por_s; - + u3l_log("sending to ip : %x, port: %u", pip_w, por_s); add_u.sin_addr.s_addr = htonl(pip_w); add_u.sin_port = htons(por_s); #ifdef MESA_DEBUG c3_c* sip_c = inet_ntoa(add_u.sin_addr); - // u3l_log("mesa: sending packet (%s,%u)", sip_c, por_s); + u3l_log("mesa: sending packet (%s,%u)", sip_c, por_s); #endif uv_buf_t buf_u = uv_buf_init((c3_c*)buf_y, len_w); @@ -1096,6 +1096,9 @@ _realise_lane(u3_noun lan) { } else { u3_noun tag, pip, por; u3x_trel(lan, &tag, &pip, &por); + u3m_p("tag",tag); + u3m_p("pip", pip); + u3m_p("por", por); if ( tag == c3__if ) { lan_u.pip_w = u3i_word(pip); u3_assert( c3y == u3a_is_cat(por) && por <= 0xFFFF); @@ -1117,7 +1120,7 @@ _mesa_rout_bufs(u3_mesa* sam_u, c3_y* buf_y, c3_w len_w, u3_noun las) while ( t != u3_nul ) { u3x_cell(t, &lan, &t); u3_lane lan_u = _realise_lane(u3k(lan)); - /* u3l_log("sending to ip: %x, port: %u", lan_u.pip_w, lan_u.por_s); */ + u3l_log("sending to ip : %x, port: %u", lan_u.pip_w, lan_u.por_s); #ifdef MESA_DEBUG /* u3l_log("sending to ip: %x, port: %u", lan_u.pip_w, lan_u.por_s); */ @@ -1275,7 +1278,9 @@ static void _mesa_ef_send(u3_mesa* sam_u, u3_noun las, u3_noun pac) { // u3m_p("pac", pac); + u3m_p("las", las); las = _mesa_queue_czar(sam_u, las, u3k(pac)); + u3m_p("las", las); c3_w len_w = u3r_met(3, pac); c3_y* buf_y = c3_calloc(len_w); u3r_bytes(0, len_w, buf_y, pac); @@ -1884,6 +1889,17 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) u3_mesa_pact* pac_u = &pic_u->pac_u; c3_s fra_s; + + c3_d* her_d = pac_u->pek_u.nam_u.her_d; + c3_o our_o = __( 0 == memcmp(her_d, sam_u->pir_u->who_d, sizeof(*her_d) * 2) ); + + // forwarding wrong, need a PIT entry + // if ( c3n == our_o ) { + // _mesa_forward(pic_u, lan_u); + // _mesa_free_pict(pic_u); + // return; + // } + u3_peer* per_u = _mesa_get_peer(sam_u, pac_u->pag_u.nam_u.her_d); c3_o new_o = c3n; if ( NULL == per_u ) { @@ -1908,6 +1924,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) u3_weak hit = _mesa_get_cache(sam_u, &pac_u->pag_u.nam_u); + // XX better check to forward pages? if ( u3_none != hit ) { _mesa_forward(pic_u, lan_u); _mesa_free_pict(pic_u); From 0b58dc239a8d294a6fc95bbdcac90b55caf4bd98 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Wed, 5 Jun 2024 13:49:51 +0200 Subject: [PATCH 136/430] mesa: get the right pip_w --- pkg/vere/io/mesa.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 09342a4074..3a54e694e2 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1100,7 +1100,7 @@ _realise_lane(u3_noun lan) { u3m_p("pip", pip); u3m_p("por", por); if ( tag == c3__if ) { - lan_u.pip_w = u3i_word(pip); + lan_u.pip_w = u3r_word(0, pip); u3_assert( c3y == u3a_is_cat(por) && por <= 0xFFFF); lan_u.por_s = por; } else { @@ -2359,6 +2359,12 @@ _mesa_recv_cb(uv_udp_t* wax_u, const struct sockaddr* adr_u, unsigned flg_i) { + + struct sockaddr_in* add_u = (struct sockaddr_in*)adr_u; + + c3_c* sip_c = inet_ntoa(add_u->sin_addr); + u3l_log("mesa: hear packet (%s:%u)", sip_c, add_u->sin_port); + if ( 0 > nrd_i ) { if ( u3C.wag_w & u3o_verbose ) { u3l_log("mesa: recv: fail: %s", uv_strerror(nrd_i)); @@ -2376,7 +2382,7 @@ _mesa_recv_cb(uv_udp_t* wax_u, } else { u3_mesa* sam_u = wax_u->data; - struct sockaddr_in* add_u = (struct sockaddr_in*)adr_u; + // struct sockaddr_in* add_u = (struct sockaddr_in*)adr_u; u3_lane lan_u; From b65a965d89e6e631efe59abc6463b28ce7c7d2a9 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Fri, 7 Jun 2024 07:15:27 +0200 Subject: [PATCH 137/430] mesa: remove prints --- pkg/vere/io/mesa.c | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 3a54e694e2..b13ffa16f4 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -789,7 +789,6 @@ static void _mesa_send_buf(u3_mesa* sam_u, u3_lane lan_u, c3_y* buf_y, c3_w len_ add_u.sin_family = AF_INET; c3_w pip_w = c3y == u3_Host.ops_u.net ? lan_u.pip_w : 0x7f000001; c3_s por_s = lan_u.por_s; - u3l_log("sending to ip : %x, port: %u", pip_w, por_s); add_u.sin_addr.s_addr = htonl(pip_w); add_u.sin_port = htons(por_s); @@ -1096,9 +1095,6 @@ _realise_lane(u3_noun lan) { } else { u3_noun tag, pip, por; u3x_trel(lan, &tag, &pip, &por); - u3m_p("tag",tag); - u3m_p("pip", pip); - u3m_p("por", por); if ( tag == c3__if ) { lan_u.pip_w = u3r_word(0, pip); u3_assert( c3y == u3a_is_cat(por) && por <= 0xFFFF); @@ -1120,7 +1116,7 @@ _mesa_rout_bufs(u3_mesa* sam_u, c3_y* buf_y, c3_w len_w, u3_noun las) while ( t != u3_nul ) { u3x_cell(t, &lan, &t); u3_lane lan_u = _realise_lane(u3k(lan)); - u3l_log("sending to ip : %x, port: %u", lan_u.pip_w, lan_u.por_s); + #ifdef MESA_DEBUG /* u3l_log("sending to ip: %x, port: %u", lan_u.pip_w, lan_u.por_s); */ @@ -1549,7 +1545,6 @@ static u3_weak _mesa_get_cache(u3_mesa* sam_u, u3_mesa_name* nam_u) { u3_noun pax = _name_to_scry(nam_u); - u3m_p("_mesa_get_cache pax", pax); u3_weak res = u3h_get(sam_u->pac_p, pax); if ( u3_none == res ) { //u3m_p("miss", u3k(pax)); @@ -1563,7 +1558,6 @@ static void _mesa_put_cache(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_noun val) { u3_noun pax = _name_to_scry(nam_u); - u3m_p("_mesa_put_cache pax", pax); u3h_put(sam_u->pac_p, pax, u3k(val)); u3z(pax); // TODO: fix refcount } @@ -1672,7 +1666,6 @@ _mesa_page_scry_hunk_cb(void* vod_p, u3_noun nun) pac_u->pek_u.nam_u.fra_w = len_w; /* u3l_log("putting %u", pac_u->pek_u.nam_u.fra_w); */ _log_pact(pac_u); - u3l_log("_mesa_put_cache loop %s", pac_u->pek_u.nam_u.pat_c); _mesa_put_cache(sam_u, &pac_u->pek_u.nam_u, u3nc(MESA_ITEM, u3k(u3h(hit)))); // u3z(key); @@ -2359,12 +2352,6 @@ _mesa_recv_cb(uv_udp_t* wax_u, const struct sockaddr* adr_u, unsigned flg_i) { - - struct sockaddr_in* add_u = (struct sockaddr_in*)adr_u; - - c3_c* sip_c = inet_ntoa(add_u->sin_addr); - u3l_log("mesa: hear packet (%s:%u)", sip_c, add_u->sin_port); - if ( 0 > nrd_i ) { if ( u3C.wag_w & u3o_verbose ) { u3l_log("mesa: recv: fail: %s", uv_strerror(nrd_i)); @@ -2382,7 +2369,7 @@ _mesa_recv_cb(uv_udp_t* wax_u, } else { u3_mesa* sam_u = wax_u->data; - // struct sockaddr_in* add_u = (struct sockaddr_in*)adr_u; + struct sockaddr_in* add_u = (struct sockaddr_in*)adr_u; u3_lane lan_u; From 9094664ff0aa55b0ba6d1d5358a986140b6b6ce0 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Mon, 10 Jun 2024 06:41:57 +0200 Subject: [PATCH 138/430] mesa: don't inkect nul lane --- pkg/vere/io/mesa.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index b13ffa16f4..ab9beb7619 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -2047,7 +2047,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) // cad = u3nt(c3__mess_ser, lan, // u3nq(c3__page, par, aut, dat)); - u3_noun lan = u3_nul; + u3_noun lan = u3_mesa_encode_lane(lan_u); u3i_slab sab_u; // u3i_slab_init(&sab_u, 3, PACT_SIZE); // u3i_slab_init(&sab_u, 3, (siz_w * req_u->tot_w) + 135); From 310eef7b5f6e8701705097b358f6cb111c6b52d7 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Mon, 10 Jun 2024 10:32:37 +0200 Subject: [PATCH 139/430] mesa: use MESA_HUNK macro --- pkg/vere/io/mesa.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index ab9beb7619..eb9e57d951 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -2161,7 +2161,7 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) if ( c3y == our_o ) { u3_noun sky = _name_to_batch_scry(&pac_u->pek_u.nam_u, fra_w == 0 ? 0 : bat_w, - fra_w == 0 ? 4096 : bat_w + MESA_HUNK); + fra_w == 0 ? MESA_HUNK : bat_w + MESA_HUNK); pac_u->pek_u.nam_u.fra_w = fra_w; From 3e7c09921c0e822f20ea477a5935c5eef2e66f99 Mon Sep 17 00:00:00 2001 From: pkova Date: Mon, 10 Jun 2024 23:07:34 +0300 Subject: [PATCH 140/430] mesa: send scry results immediately, fix memory corruption bug --- pkg/vere/io/mesa.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index eb9e57d951..70884f9561 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -999,7 +999,7 @@ _mesa_req_pact_done(u3_mesa* sam_u, u3_mesa_name *nam_u, u3_mesa_data* dat_u, u3 } // received duplicate - if ( nam_u->fra_w != 0 && c3n == bitset_has(&req_u->was_u, nam_u->fra_w) ) { + if ( c3n == bitset_has(&req_u->was_u, nam_u->fra_w) ) { /* MESA_LOG(DUPE); */ req_u->wat_u[nam_u->fra_w].dup_y++; return req_u; @@ -1657,11 +1657,11 @@ _mesa_page_scry_hunk_cb(void* vod_p, u3_noun nun) while ( u3_nul != hit ) { // u3_noun key = u3nc(u3k(pax), u3i_word(lop_w)); // u3h_put(sam_u->fin_s.sac_p, key, u3k(u3h(lis))); - // if (fra_w == len_w && tag == MESA_WAIT) { - // c3_w lun_w = _mesa_respond(pic_u, &buf_y, u3k(u3h(hit))); - // pac_u->pek_u.nam_u.fra_w = fra_w; - // _mesa_rout_bufs(sam_u, buf_y, lun_w, u3k(u3t(dat))); - // } + if (fra_w == len_w && tag == MESA_WAIT) { + c3_w lun_w = _mesa_respond(pic_u, &buf_y, u3k(u3h(hit))); + pac_u->pek_u.nam_u.fra_w = fra_w; + _mesa_rout_bufs(sam_u, buf_y, lun_w, u3k(u3t(dat))); + } pac_u->pek_u.nam_u.fra_w = len_w; /* u3l_log("putting %u", pac_u->pek_u.nam_u.fra_w); */ From 8ffd66db0b7459c1f8d573859379e73f141a6909 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Wed, 12 Jun 2024 00:45:29 -0400 Subject: [PATCH 141/430] ames: add a further hack to ensure only one bound udp socket --- pkg/vere/io/ames.c | 12 ++++++++---- pkg/vere/vere.h | 1 + 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index a50054ff00..8645c50028 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -911,7 +911,7 @@ _ames_send(u3_pact* pac_u) uv_buf_t buf_u = uv_buf_init((c3_c*)pac_u->hun_y, pac_u->len_w); c3_i sas_i = uv_udp_send(&pac_u->snd_u, - &sam_u->wax_u, + u3_Host.wax_u, &buf_u, 1, (const struct sockaddr*)&add_u, _ames_send_cb); @@ -1443,7 +1443,7 @@ _stun_on_request(u3_ames* sam_u, _stun_make_response(req_y, &lan_u, snd_u->hun_y); uv_buf_t buf_u = uv_buf_init((c3_c*)snd_u->hun_y, 40); - c3_i sas_i = uv_udp_send(&snd_u->req_u, &sam_u->wax_u, + c3_i sas_i = uv_udp_send(&snd_u->req_u, u3_Host.wax_u, &buf_u, 1, (struct sockaddr *)&add_u, _stun_send_response_cb); if ( sas_i != 0 ) { @@ -1567,7 +1567,7 @@ _stun_send_request(u3_ames* sam_u) add_u.sin_port = htons(sam_u->sun_u.lan_u.por_s); uv_buf_t buf_u = uv_buf_init((c3_c*)snd_u->hun_y, 28); - c3_i sas_i = uv_udp_send(&snd_u->req_u, &sam_u->wax_u, &buf_u, 1, + c3_i sas_i = uv_udp_send(&snd_u->req_u, u3_Host.wax_u, &buf_u, 1, (const struct sockaddr*)&add_u, _stun_send_request_cb); if ( sas_i != 0) { @@ -2745,7 +2745,7 @@ _ames_recv_cb(uv_udp_t* wax_u, const struct sockaddr* adr_u, unsigned flg_i) { - u3_ames* sam_u = wax_u->data; + u3_ames* sam_u = u3_Host.sam_u; // wax_u->data; if ( 0 > nrd_i ) { if ( u3C.wag_w & u3o_verbose ) { @@ -2878,10 +2878,12 @@ _ames_io_start(u3_ames* sam_u) u3_pier_bail(u3_king_stub()); }*/ + /* uv_udp_getsockname(&sam_u->wax_u, (struct sockaddr *)&add_u, &add_i); u3_assert(add_u.sin_port); sam_u->pir_u->por_s = ntohs(add_u.sin_port); + */ } if ( c3y == u3_Host.ops_u.net ) { @@ -2900,7 +2902,9 @@ _ames_io_start(u3_ames* sam_u) c3_free(our_s); } + /* uv_udp_recv_start(&sam_u->wax_u, _ames_alloc, _ames_recv_cb); + */ sam_u->car_u.liv_o = c3y; u3z(who); diff --git a/pkg/vere/vere.h b/pkg/vere/vere.h index a81e2d91ed..9dc6cd1b9f 100644 --- a/pkg/vere/vere.h +++ b/pkg/vere/vere.h @@ -341,6 +341,7 @@ c3_i xit_i; // exit code for shutdown void (*bot_f)(); // call when chis is up void* sam_u; // XX + uv_udp_t* wax_u; // XX } u3_host; // host == computer == process /** Pier system. From ddcaee95ccc9a9c9da6214d15bb15988e1de6ecf Mon Sep 17 00:00:00 2001 From: Pyry Kovanen Date: Mon, 27 May 2024 16:02:16 +0300 Subject: [PATCH 142/430] main: restore SIGTSTP handler after replay --- pkg/vere/main.c | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/vere/main.c b/pkg/vere/main.c index 2222f87dd1..37de90f882 100644 --- a/pkg/vere/main.c +++ b/pkg/vere/main.c @@ -3111,6 +3111,7 @@ main(c3_i argc, // validate whether we can execute disk migration if ( u3_Host.ops_u.nuu == c3n ) { _cw_play_impl(0, 0, c3n, c3n, c3n); + signal(SIGTSTP, _stop_exit); // XX unmap loom, else parts of the snapshot could be left in memory } From 295f1e224903e8431e7f9c4919b8fe11df848ab4 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Wed, 12 Jun 2024 08:54:05 -0400 Subject: [PATCH 143/430] ames: check for no udp handle before sending --- pkg/vere/io/ames.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index 8645c50028..f8f22b302b 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -896,6 +896,10 @@ _ames_send(u3_pact* pac_u) u3l_log("ames: _ames_send null"); _ames_pact_free(pac_u); } + else if ( !u3_Host.wax_u ) { + u3l_log("ames: send: no handle"); + _ames_pact_free(pac_u); + } else { struct sockaddr_in add_u; @@ -1426,6 +1430,11 @@ _stun_on_request(u3_ames* sam_u, const c3_y* req_y, u3_lane lan_u) { + if ( !u3_Host.wax_u ) { + u3l_log("stun: response: no handle"); + return; + } + _stun_send* snd_u = c3_malloc(sizeof(*snd_u) + 40); snd_u->sam_u = sam_u; @@ -1555,6 +1564,11 @@ _stun_send_request(u3_ames* sam_u) { u3_assert( STUN_OFF != sam_u->sun_u.sat_y ); + if ( !u3_Host.wax_u ) { + u3l_log("stun: request: no handle"); + return; + } + _stun_send* snd_u = c3_malloc(sizeof(*snd_u) + 28); snd_u->sam_u = sam_u; From 3720ecda23ee1ba31ed531fa63e685b71d76250b Mon Sep 17 00:00:00 2001 From: pkova Date: Wed, 12 Jun 2024 17:48:44 +0300 Subject: [PATCH 144/430] ames/mesa: make the shared handle work --- pkg/vere/io/ames.c | 21 ++++----------------- pkg/vere/io/mesa.c | 9 +++++---- pkg/vere/vere.h | 2 +- 3 files changed, 10 insertions(+), 22 deletions(-) diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index f8f22b302b..4926db2657 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -896,10 +896,6 @@ _ames_send(u3_pact* pac_u) u3l_log("ames: _ames_send null"); _ames_pact_free(pac_u); } - else if ( !u3_Host.wax_u ) { - u3l_log("ames: send: no handle"); - _ames_pact_free(pac_u); - } else { struct sockaddr_in add_u; @@ -915,7 +911,7 @@ _ames_send(u3_pact* pac_u) uv_buf_t buf_u = uv_buf_init((c3_c*)pac_u->hun_y, pac_u->len_w); c3_i sas_i = uv_udp_send(&pac_u->snd_u, - u3_Host.wax_u, + &u3_Host.wax_u, &buf_u, 1, (const struct sockaddr*)&add_u, _ames_send_cb); @@ -1430,11 +1426,6 @@ _stun_on_request(u3_ames* sam_u, const c3_y* req_y, u3_lane lan_u) { - if ( !u3_Host.wax_u ) { - u3l_log("stun: response: no handle"); - return; - } - _stun_send* snd_u = c3_malloc(sizeof(*snd_u) + 40); snd_u->sam_u = sam_u; @@ -1452,7 +1443,7 @@ _stun_on_request(u3_ames* sam_u, _stun_make_response(req_y, &lan_u, snd_u->hun_y); uv_buf_t buf_u = uv_buf_init((c3_c*)snd_u->hun_y, 40); - c3_i sas_i = uv_udp_send(&snd_u->req_u, u3_Host.wax_u, + c3_i sas_i = uv_udp_send(&snd_u->req_u, &u3_Host.wax_u, &buf_u, 1, (struct sockaddr *)&add_u, _stun_send_response_cb); if ( sas_i != 0 ) { @@ -1564,11 +1555,6 @@ _stun_send_request(u3_ames* sam_u) { u3_assert( STUN_OFF != sam_u->sun_u.sat_y ); - if ( !u3_Host.wax_u ) { - u3l_log("stun: request: no handle"); - return; - } - _stun_send* snd_u = c3_malloc(sizeof(*snd_u) + 28); snd_u->sam_u = sam_u; @@ -1581,7 +1567,7 @@ _stun_send_request(u3_ames* sam_u) add_u.sin_port = htons(sam_u->sun_u.lan_u.por_s); uv_buf_t buf_u = uv_buf_init((c3_c*)snd_u->hun_y, 28); - c3_i sas_i = uv_udp_send(&snd_u->req_u, u3_Host.wax_u, &buf_u, 1, + c3_i sas_i = uv_udp_send(&snd_u->req_u, &u3_Host.wax_u, &buf_u, 1, (const struct sockaddr*)&add_u, _stun_send_request_cb); if ( sas_i != 0) { @@ -3289,6 +3275,7 @@ u3_ames_io_init(u3_pier* pir_u) sam_u->lax_p = u3h_new_cache(500000); u3_assert( !uv_udp_init(u3L, &sam_u->wax_u) ); + uv_udp_init(u3L, &u3_Host.wax_u); sam_u->wax_u.data = sam_u; sam_u->sil_u = u3s_cue_xeno_init(); diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 70884f9561..737ee92800 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -800,7 +800,7 @@ static void _mesa_send_buf(u3_mesa* sam_u, u3_lane lan_u, c3_y* buf_y, c3_w len_ uv_buf_t buf_u = uv_buf_init((c3_c*)buf_y, len_w); c3_i sas_i = uv_udp_send(&sel_u->snd_u, - &sam_u->wax_u, + &u3_Host.wax_u, &buf_u, 1, (const struct sockaddr*)&add_u, _mesa_send_cb); @@ -2428,7 +2428,7 @@ _mesa_io_talk(u3_auto* car_u) htonl(INADDR_LOOPBACK); add_u.sin_port = htons(por_s); - if ( (ret_i = uv_udp_bind(&sam_u->wax_u, + if ( (ret_i = uv_udp_bind(&u3_Host.wax_u, (const struct sockaddr*)&add_u, 0)) != 0 ) { u3l_log("mesa: bind: %s", uv_strerror(ret_i)); @@ -2444,7 +2444,7 @@ _mesa_io_talk(u3_auto* car_u) u3_pier_bail(u3_king_stub()); } - uv_udp_getsockname(&sam_u->wax_u, (struct sockaddr *)&add_u, &add_i); + uv_udp_getsockname(&u3_Host.wax_u, (struct sockaddr *)&add_u, &add_i); u3_assert(add_u.sin_port); sam_u->pir_u->por_s = ntohs(add_u.sin_port); @@ -2456,7 +2456,8 @@ _mesa_io_talk(u3_auto* car_u) u3l_log("mesa: live on %d (localhost only)", sam_u->pir_u->por_s); } - uv_udp_recv_start(&sam_u->wax_u, _ames_alloc, _mesa_recv_cb); + u3_Host.wax_u.data = sam_u; + uv_udp_recv_start(&u3_Host.wax_u, _ames_alloc, _mesa_recv_cb); sam_u->car_u.liv_o = c3y; //u3z(rac); u3z(who); diff --git a/pkg/vere/vere.h b/pkg/vere/vere.h index 9dc6cd1b9f..870dff5e89 100644 --- a/pkg/vere/vere.h +++ b/pkg/vere/vere.h @@ -341,7 +341,7 @@ c3_i xit_i; // exit code for shutdown void (*bot_f)(); // call when chis is up void* sam_u; // XX - uv_udp_t* wax_u; // XX + uv_udp_t wax_u; // XX } u3_host; // host == computer == process /** Pier system. From a9448abeee15450c8b813c89807ab6ca5a2b7395 Mon Sep 17 00:00:00 2001 From: pkova Date: Thu, 13 Jun 2024 14:50:37 +0300 Subject: [PATCH 145/430] mesa: remove dumb prints --- pkg/vere/io/mesa.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 737ee92800..07123626cd 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -793,8 +793,8 @@ static void _mesa_send_buf(u3_mesa* sam_u, u3_lane lan_u, c3_y* buf_y, c3_w len_ add_u.sin_port = htons(por_s); #ifdef MESA_DEBUG - c3_c* sip_c = inet_ntoa(add_u.sin_addr); - u3l_log("mesa: sending packet (%s,%u)", sip_c, por_s); + /* c3_c* sip_c = inet_ntoa(add_u.sin_addr); */ + /* u3l_log("mesa: sending packet (%s,%u)", sip_c, por_s); */ #endif uv_buf_t buf_u = uv_buf_init((c3_c*)buf_y, len_w); @@ -950,6 +950,8 @@ _mesa_burn_misorder_queue(u3_pend_req* req_u) if ( BAO_GOOD != (res_y = blake_bao_verify(req_u->bao_u, buf_u->fra_y, buf_u->len_w, buf_u->par_u))) { return res_y; } else { + /* u3l_log("burn %u", buf_u->num_w); */ + /* memcpy(req_u->dat_y + (1024 * buf_u->num_w), buf_u->fra_y, buf_u->len_w); */ _mesa_free_misord_buf((u3_misord_buf*)vec_pop(&req_u->mis_u, i)); break; } @@ -1665,7 +1667,7 @@ _mesa_page_scry_hunk_cb(void* vod_p, u3_noun nun) pac_u->pek_u.nam_u.fra_w = len_w; /* u3l_log("putting %u", pac_u->pek_u.nam_u.fra_w); */ - _log_pact(pac_u); + /* _log_pact(pac_u); */ _mesa_put_cache(sam_u, &pac_u->pek_u.nam_u, u3nc(MESA_ITEM, u3k(u3h(hit)))); // u3z(key); @@ -1876,7 +1878,7 @@ static void _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) { #ifdef MESA_DEBUG - u3l_log("mesa hear page %u", pic_u->pac_u.pag_u.nam_u.fra_w); + /* u3l_log("mesa hear page %u", pic_u->pac_u.pag_u.nam_u.fra_w); */ #endif u3_mesa* sam_u = pic_u->sam_u; u3_mesa_pact* pac_u = &pic_u->pac_u; @@ -2126,8 +2128,8 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) c3_w bat_w = _mesa_lop(fra_w); pac_u->pek_u.nam_u.fra_w = bat_w; - _log_pact(pac_u); - u3l_log("_mesa_hear_peek %s", pac_u->pek_u.nam_u.pat_c); + /* _log_pact(pac_u); */ + /* u3l_log("_mesa_hear_peek %s", pac_u->pek_u.nam_u.pat_c); */ u3_weak hit = _mesa_get_cache(sam_u, &pac_u->pek_u.nam_u); /* u3l_log("peek fra %u hit %u", fra_w, hit != u3_none); */ From 6ece43fb0fa84f225778069f86849c8747dd64e5 Mon Sep 17 00:00:00 2001 From: pkova Date: Thu, 13 Jun 2024 15:11:21 +0300 Subject: [PATCH 146/430] mesa: remove more dumb prints --- pkg/vere/io/mesa/pact.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index 9f16e70a5c..3ad2188d67 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -590,8 +590,8 @@ mesa_sift_pact(u3_mesa_pact* pac_u, c3_y* buf_y, c3_w len_w) res_w = _mesa_sift_poke_pact(&pac_u->pok_u, buf_y, len_w); } break; default: { - u3l_log("mesa: received unknown packet type"); - _log_buf(buf_y, len_w); + /* u3l_log("mesa: received unknown packet type"); */ + /* _log_buf(buf_y, len_w); */ break; } } @@ -600,8 +600,12 @@ mesa_sift_pact(u3_mesa_pact* pac_u, c3_y* buf_y, c3_w len_w) c3_w mug_w = u3r_mug_bytes(buf_y, res_w); mug_w &= 0xFFFFF; + if ( mug_w != pac_u->hed_u.mug_w ) { - u3l_log("mesa: failed mug"); + /* u3l_log("mesa: failed mug"); */ + /* _log_buf(buf_y, res_w); */ + /* u3l_log("res_w %u", res_w); */ + /* u3l_log("frag %u", pac_u->pag_u.nam_u.fra_w); */ return 0; } } @@ -939,6 +943,7 @@ mesa_etch_pact(c3_y* buf_y, u3_mesa_pact* pac_u) } } + hed_u->mug_w = u3r_mug_bytes(buf_y + cur_w, nex_w); hed_u->mug_w &= 0xFFFFF; From a69ae72ec405e7b213cf36f5f6ac4f198102f26b Mon Sep 17 00:00:00 2001 From: pkova Date: Thu, 13 Jun 2024 15:45:05 +0300 Subject: [PATCH 147/430] mesa: and even more dumb prints out --- pkg/vere/io/mesa.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 07123626cd..cf8eb5d72d 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -572,7 +572,7 @@ _mesa_put_request(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_pend_req* req_u) { u3_pend_req* new_u = req_u; if ( old_u == NULL ) { new_u = u3a_calloc(1, sizeof(u3_pend_req)); - u3l_log("putting fresh req %p", new_u); + /* u3l_log("putting fresh req %p", new_u); */ memcpy(new_u, req_u, sizeof(u3_pend_req)); uv_timer_init(u3L, &new_u->tim_u); } else { @@ -634,7 +634,7 @@ _mesa_req_get_cwnd(u3_pend_req* req_u) } c3_w rem_w = req_u->tot_w - req_u->nex_w + 1; - u3l_log("rem_w: %u", rem_w); + /* u3l_log("rem_w: %u", rem_w); */ return c3_min(rem_w, req_u->gag_u->wnd_w - liv_w); } @@ -831,7 +831,7 @@ _try_resend(u3_pend_req* req_u) u3_lane* lan_u = &req_u->lan_u; c3_o los_o = c3n; if ( req_u->tot_w == 0 || req_u->ack_w <= REORDER_THRESH ) { - u3l_log("reorder thresh too low %u", req_u->ack_w); + /* u3l_log("reorder thresh too low %u", req_u->ack_w); */ return; } c3_w ack_w = req_u->ack_w - REORDER_THRESH; @@ -887,7 +887,7 @@ static void _update_oldest_req(u3_pend_req *req_u, u3_gage* gag_u) { if( req_u->tot_w == 0 || req_u->len_w == req_u->tot_w ) { - u3l_log("bad condition"); + /* u3l_log("bad condition"); */ return; } // scan in flight packets, find oldest @@ -905,7 +905,7 @@ _update_oldest_req(u3_pend_req *req_u, u3_gage* gag_u) } if ( now_d == wen_d ) { #ifdef MESA_DEBUG - u3l_log("failed to find new oldest"); + /* u3l_log("failed to find new oldest"); */ #endif } req_u->old_w = idx_w; @@ -975,7 +975,7 @@ _mesa_req_pact_done(u3_mesa* sam_u, u3_mesa_name *nam_u, u3_mesa_data* dat_u, u3 u3_pend_req* req_u = _mesa_get_request(sam_u, nam_u); if ( NULL == req_u ) { - MESA_LOG(APATHY); + /* MESA_LOG(APATHY); */ return NULL; } @@ -1276,9 +1276,9 @@ static void _mesa_ef_send(u3_mesa* sam_u, u3_noun las, u3_noun pac) { // u3m_p("pac", pac); - u3m_p("las", las); + /* u3m_p("las", las); */ las = _mesa_queue_czar(sam_u, las, u3k(pac)); - u3m_p("las", las); + /* u3m_p("las", las); */ c3_w len_w = u3r_met(3, pac); c3_y* buf_y = c3_calloc(len_w); u3r_bytes(0, len_w, buf_y, pac); @@ -1308,7 +1308,7 @@ static c3_o _mesa_kick(u3_mesa* sam_u, u3_noun tag, u3_noun dat) u3l_log(" mesa: send no"); ret_o = c3n; } else { - u3l_log(" mesa: send yes"); + /* u3l_log(" mesa: send yes"); */ _mesa_ef_send(sam_u, u3k(las), u3k(pac)); ret_o = c3y; } @@ -1985,7 +1985,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) u3_pend_req* req_u; if ( pac_u->pek_u.nam_u.nit_o == c3y ) { - u3l_log("_mesa_req_pact_init NIT"); + /* u3l_log("_mesa_req_pact_init NIT"); */ req_u = _mesa_req_pact_init(sam_u, pic_u, &lan_u); if ( req_u == NULL ) { _mesa_free_pict(pic_u); @@ -2009,8 +2009,8 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) c3_w nex_w = req_u->nex_w; if ( win_w != 0 ) { #ifdef MESA_DEBUG - u3l_log("continuing flow nex: %u, win: %u", nex_w, win_w); - u3l_log("in flight %u", bitset_wyt(&req_u->was_u)); + /* u3l_log("continuing flow nex: %u, win: %u", nex_w, win_w); */ + /* u3l_log("in flight %u", bitset_wyt(&req_u->was_u)); */ #endif for ( int i = 0; i < win_w; i++ ) { c3_y* buf_y = c3_calloc(PACT_SIZE); @@ -2053,7 +2053,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) u3i_slab sab_u; // u3i_slab_init(&sab_u, 3, PACT_SIZE); // u3i_slab_init(&sab_u, 3, (siz_w * req_u->tot_w) + 135); - u3l_log("slab size %u", (PACT_SIZE - pac_u->pag_u.dat_u.len_w) + (siz_w * req_u->tot_w)); + /* u3l_log("slab size %u", (PACT_SIZE - pac_u->pag_u.dat_u.len_w) + (siz_w * req_u->tot_w)); */ u3i_slab_init(&sab_u, 3, (PACT_SIZE - pac_u->pag_u.dat_u.len_w) + (siz_w * req_u->tot_w)); pac_u->pag_u.dat_u.len_w = (siz_w * req_u->tot_w); @@ -2067,7 +2067,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) pic_u->pac_u.pag_u.nam_u.fra_w = req_u->tot_w - 1; // XX should just preserve input buffer c3_w res = mesa_etch_pact(sab_u.buf_y, pac_u); - u3l_log("slab len_w %u mesa_etch_pact %u", sab_u.len_w, res); + /* u3l_log("slab len_w %u mesa_etch_pact %u", sab_u.len_w, res); */ cad = u3nt(c3__heer, lan, u3i_slab_mint(&sab_u)); } From 560d7f8d6b82712ac1d5143abd83f059cd5e6adf Mon Sep 17 00:00:00 2001 From: pkova Date: Thu, 13 Jun 2024 19:46:22 +0300 Subject: [PATCH 148/430] mesa: MESA_HUNK to 16k --- pkg/vere/io/mesa.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index cf8eb5d72d..944652f0c7 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -67,7 +67,7 @@ typedef struct _u3_mesa_stat { #define IN_FLIGHT 10 // XX -#define MESA_HUNK 4096 // 184 +#define MESA_HUNK 16384 // 184 // pending interest sentinels #define MESA_ITEM 1 // cached item From f18879e40f03a9074f144b47dcf544c1d7b3b3d8 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Fri, 14 Jun 2024 13:49:15 +0200 Subject: [PATCH 149/430] mesa: get MESA_HUNK fragments for %init request --- pkg/vere/io/mesa.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 944652f0c7..961f32fe11 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -302,9 +302,6 @@ static inline c3_w _mesa_lop(c3_w fra_w) { c3_w res_w = ((( fra_w ) / MESA_HUNK) * MESA_HUNK); - if (res_w == 0) { - return 1; - } return res_w; } @@ -1653,12 +1650,21 @@ _mesa_page_scry_hunk_cb(void* vod_p, u3_noun nun) c3_y* buf_y; // u3m_p("hit", u3a_is_cell(hit)); - c3_w len_w = fra_w == 0 ? 0 : bat_w; + c3_w len_w = bat_w; /* u3l_log("path %s", pac_u->pek_u.nam_u.pat_c); */ c3_w i = 0; while ( u3_nul != hit ) { // u3_noun key = u3nc(u3k(pax), u3i_word(lop_w)); // u3h_put(sam_u->fin_s.sac_p, key, u3k(u3h(lis))); + + if ( (bat_w == 0) && (i == 0) ) { + pac_u->pek_u.nam_u.nit_o = c3y; + pac_u->pek_u.nam_u.aut_o = c3n; + } else { + pac_u->pek_u.nam_u.nit_o = c3n; + pac_u->pek_u.nam_u.aut_o = c3n; + } + if (fra_w == len_w && tag == MESA_WAIT) { c3_w lun_w = _mesa_respond(pic_u, &buf_y, u3k(u3h(hit))); pac_u->pek_u.nam_u.fra_w = fra_w; @@ -1668,11 +1674,16 @@ _mesa_page_scry_hunk_cb(void* vod_p, u3_noun nun) pac_u->pek_u.nam_u.fra_w = len_w; /* u3l_log("putting %u", pac_u->pek_u.nam_u.fra_w); */ /* _log_pact(pac_u); */ + _mesa_put_cache(sam_u, &pac_u->pek_u.nam_u, u3nc(MESA_ITEM, u3k(u3h(hit)))); // u3z(key); hit = u3t(hit); - len_w++; + + if (!( (bat_w == 0) && (i == 0) )) { + len_w++; + } + i++; pac_u->pek_u.nam_u.fra_w = len_w; } @@ -2162,8 +2173,8 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) _mesa_add_lane_to_cache(sam_u, &pac_u->pek_u.nam_u, u3_nul, lan_u); // TODO: retrieve from namespace if ( c3y == our_o ) { u3_noun sky = _name_to_batch_scry(&pac_u->pek_u.nam_u, - fra_w == 0 ? 0 : bat_w, - fra_w == 0 ? MESA_HUNK : bat_w + MESA_HUNK); + bat_w, + bat_w + MESA_HUNK); pac_u->pek_u.nam_u.fra_w = fra_w; From a5aee1541bd089aedfcb560a6be64bb9c4490e4f Mon Sep 17 00:00:00 2001 From: lukechampine Date: Thu, 13 Jun 2024 23:54:22 -0400 Subject: [PATCH 150/430] mesa: add lackman LSS verifier --- pkg/vere/io/lss.c | 127 +++++++++++++++++++++++++++++++++++++++++++++ pkg/vere/io/lss.h | 28 ++++++++++ pkg/vere/io/mesa.c | 77 +++++++++++++-------------- 3 files changed, 192 insertions(+), 40 deletions(-) create mode 100644 pkg/vere/io/lss.c create mode 100644 pkg/vere/io/lss.h diff --git a/pkg/vere/io/lss.c b/pkg/vere/io/lss.c new file mode 100644 index 0000000000..7c3980dee6 --- /dev/null +++ b/pkg/vere/io/lss.c @@ -0,0 +1,127 @@ +/// @file + +#include "vere.h" +#include +#include +#include "urcrypt.h" +#include "lss.h" + +static c3_y IV[32] = {103, 230, 9, 106, 133, 174, 103, 187, 114, 243, 110, 60, 58, 245, 79, 165, 127, 82, 14, 81, 140, 104, 5, 155, 171, 217, 131, 31, 25, 205, 224, 91}; + +static void _leaf_hash(c3_y out[32], c3_y* leaf, c3_w len, c3_d counter) +{ + c3_y cv[32]; + memcpy(cv, IV, 32); + c3_y block[64] = {0}; + c3_y block_len = 0; + c3_y flags = 0; + urcrypt_blake3_chunk_output(len, leaf, cv, block, &block_len, &counter, &flags); + urcrypt_blake3_compress(cv, block, block_len, counter, flags, out); +} + +static void _parent_hash(c3_y out[32], c3_y left[32], c3_y right[32]) +{ + c3_y cv[32]; + memcpy(cv, IV, 32); + c3_y block[64]; + memcpy(block, left, 32); + memcpy(block + 32, right, 32); + c3_y block_len = 64; + c3_y flags = 1 << 2; // PARENT + urcrypt_blake3_compress(cv, block, block_len, 0, flags, out); +} + +static c3_y bits_ctz64(c3_d val_d) +{ + if (val_d == 0 ) { + return 64; + } + c3_y ret_y = 0; + if ((val_d & 0x00000000FFFFFFFFULL) == 0) { ret_y += 32; val_d >>= 32; } + if ((val_d & 0x000000000000FFFFULL) == 0) { ret_y += 16; val_d >>= 16; } + if ((val_d & 0x00000000000000FFULL) == 0) { ret_y += 8; val_d >>= 8; } + if ((val_d & 0x000000000000000FULL) == 0) { ret_y += 4; val_d >>= 4; } + if ((val_d & 0x0000000000000003ULL) == 0) { ret_y += 2; val_d >>= 2; } + if ((val_d & 0x0000000000000001ULL) == 0) { ret_y += 1; val_d >>= 1; } + return ret_y; +} + +static c3_y bits_len64(c3_d val_d) +{ + if (val_d == 0) { + return 0; + } + c3_y ret_y = 64; + if ((val_d & 0xFFFFFFFF00000000ULL) == 0) { ret_y -= 32; val_d <<= 32; } + if ((val_d & 0xFFFF000000000000ULL) == 0) { ret_y -= 16; val_d <<= 16; } + if ((val_d & 0xFF00000000000000ULL) == 0) { ret_y -= 8; val_d <<= 8; } + if ((val_d & 0xF000000000000000ULL) == 0) { ret_y -= 4; val_d <<= 4; } + if ((val_d & 0xC000000000000000ULL) == 0) { ret_y -= 2; val_d <<= 2; } + if ((val_d & 0x8000000000000000ULL) == 0) { ret_y -= 1; val_d <<= 1; } + return ret_y; +} + +static c3_o lss_find_pair(lss_verifier* los_u, c3_w height, c3_b sel, c3_y h[32]) +{ + if (memcmp(los_u->pairs[height][sel], h, 32) == 0) { + return c3y; + } + // look for "irregular" pairs, which will always be on the right + c3_w max_height = bits_len64(los_u->leaves); + for (height++; height < max_height; height++) { + if (memcmp(los_u->pairs[height][1], h, 32) == 0) { + return c3y; + } + } + return c3n; +} + +c3_o lss_verifier_ingest(lss_verifier* los_u, c3_y* leaf_y, c3_w leaf_w, lss_pair* pair) { + // verify leaf + c3_y leaf_hash[32]; + _leaf_hash(leaf_hash, leaf_y, leaf_w, los_u->counter); + if (!(lss_find_pair(los_u, 0, los_u->counter&1, leaf_hash))) { + return c3n; + } + // check whether pair is expected + c3_w height = bits_ctz64(los_u->counter) + 1; + if ((pair != NULL) != ((los_u->counter == 0) || (los_u->counter + (1 << height) < los_u->leaves))) { + return c3n; + } else if (pair == NULL) { + return c3y; + } + // verify and insert pair + c3_b sel = ~(los_u->counter >> height) & 1; + c3_y parent_hash[32]; + _parent_hash(parent_hash, (*pair)[0], (*pair)[1]); + if (!lss_find_pair(los_u, height, sel, parent_hash)) { + return c3n; + } + los_u->pairs[height-1] = pair; + return c3y; +} + +c3_o lss_verifier_init(lss_verifier* los_u, c3_w leaves, lss_hash* proof, c3_w proof_w) { + if (proof_w < 2 || proof_w > bits_len64(leaves)) { + return c3n; + } + los_u->leaves = leaves; + los_u->counter = 0; + los_u->pairs = c3_calloc(bits_len64(leaves) * sizeof(lss_pair*)); + memcpy(los_u->pairs[0][0], proof[0], 32); + for (c3_w i = 1; i < proof_w; i++) { + memcpy(los_u->pairs[i-1][1], proof[i], 32); + } + return c3y; +} + +void lss_verifier_free(lss_verifier* los_u) { + c3_free(los_u->pairs); + c3_free(los_u); +} + +void lss_complete_inline_proof(lss_hash* proof, c3_y* leaf_y, c3_w leaf_w) { + c3_y leaf_hash[32]; + _leaf_hash(leaf_hash, leaf_y, leaf_w, 0); + memcpy(proof, leaf_hash, 32); +} diff --git a/pkg/vere/io/lss.h b/pkg/vere/io/lss.h new file mode 100644 index 0000000000..37a8d95f18 --- /dev/null +++ b/pkg/vere/io/lss.h @@ -0,0 +1,28 @@ +#ifndef VERE_LSS_H +#define VERE_LSS_H + +#include "vere.h" +#include "ivory.h" + +#include "noun.h" +#include "ur.h" + +typedef c3_y lss_hash[32]; + +typedef lss_hash lss_pair[2]; + +typedef struct _lss_verifier { + c3_w leaves; + c3_w counter; + lss_pair** pairs; +} lss_verifier; + +c3_o lss_verifier_ingest(lss_verifier* ver_u, c3_y* leaf_y, c3_w leaf_w, lss_pair* pair); + +c3_o lss_verifier_init(lss_verifier* ver_u, c3_w leaves, lss_hash* proof, c3_w proof_w); + +void lss_verifier_free(lss_verifier* ver_u); + +void lss_complete_inline_proof(lss_hash* proof, c3_y* leaf_y, c3_w leaf_w); + +#endif diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 737ee92800..f0868b84ba 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -19,6 +19,7 @@ #include #include #include "blake.h" +#include "lss.h" c3_o dop_o = c3n; @@ -105,10 +106,10 @@ typedef struct _u3_peer_last { } u3_peer_last; typedef struct _u3_misord_buf { - c3_y* fra_y; - c3_w len_w; - c3_w num_w; - blake_pair* par_u; + c3_y* fra_y; + c3_w len_w; + c3_w num_w; + lss_pair* par_u; } u3_misord_buf; struct _u3_mesa; @@ -130,8 +131,8 @@ typedef struct _u3_pend_req { c3_w ack_w; // highest acked fragment number u3_lane lan_u; // last lane heard u3_gage* gag_u; // congestion control - u3_vec(u3_buf) mis_u; // misordered blake hash - blake_bao* bao_u; // blake verifier + u3_vec(u3_buf) mis_u; // misordered packets + lss_verifier* los_u; // Lockstep verifier u3_mesa_pict* pic_u; // preallocated request packet u3_pact_stat* wat_u; // ((mop @ud packet-state) lte) u3_bitset was_u; // ((mop @ud packet-state) lte) @@ -546,7 +547,7 @@ _mesa_del_request(u3_mesa* sam_u, u3_mesa_name* nam_u) { c3_free(req_u->wat_u); vec_free(&req_u->mis_u); c3_free(req_u->dat_y); - blake_bao_free(req_u->bao_u); + lss_verifier_free(req_u->los_u); u3h_del(sam_u->req_p, key); u3a_free(req_u); u3z(key); @@ -934,20 +935,20 @@ static void _mesa_free_misord_buf(u3_misord_buf* buf_u) c3_free(buf_u); } -static bao_ingest_result +static c3_o _mesa_burn_misorder_queue(u3_pend_req* req_u) { - c3_w wan_w = req_u->bao_u->con_w; + c3_w wan_w = req_u->los_u->counter; c3_w len_w; c3_o fon_o; - bao_ingest_result res_y = BAO_GOOD; + c3_o res_y = c3y; while ( (len_w = vec_len(&req_u->mis_u)) != 0 ) { fon_o = c3n; for (int i = 0; i < len_w; i++) { u3_misord_buf* buf_u = (u3_misord_buf*)req_u->mis_u.vod_p[i]; if ( buf_u->num_w == wan_w ) { fon_o = c3y; - if ( BAO_GOOD != (res_y = blake_bao_verify(req_u->bao_u, buf_u->fra_y, buf_u->len_w, buf_u->par_u))) { + if ( c3y != (res_y = lss_verifier_ingest(req_u->los_u, buf_u->fra_y, buf_u->len_w, buf_u->par_u))) { return res_y; } else { _mesa_free_misord_buf((u3_misord_buf*)vec_pop(&req_u->mis_u, i)); @@ -1022,12 +1023,12 @@ _mesa_req_pact_done(u3_mesa* sam_u, u3_mesa_name *nam_u, u3_mesa_data* dat_u, u3 req_u->lef_w++; } - blake_pair* par_u = NULL; + lss_pair* par_u = NULL; if ( dat_u->aum_u.typ_e == AUTH_NEXT ) { // needs to be heap allocated bc will be saved if misordered - par_u = c3_calloc(sizeof(blake_pair)); - memcpy(par_u->sin_y, dat_u->aup_u.has_y[0], BLAKE3_OUT_LEN); - memcpy(par_u->dex_y, dat_u->aup_u.has_y[1], BLAKE3_OUT_LEN); + par_u = c3_calloc(sizeof(lss_pair)); + memcpy((*par_u)[0], dat_u->aup_u.has_y[0], sizeof(dat_u->aup_u.has_y[0])); + memcpy((*par_u)[1], dat_u->aup_u.has_y[1], sizeof(dat_u->aup_u.has_y[1])); } c3_y ver_y; @@ -1038,7 +1039,7 @@ _mesa_req_pact_done(u3_mesa* sam_u, u3_mesa_name *nam_u, u3_mesa_data* dat_u, u3 memset(buf_y + dat_u->len_w, 0, 1024 - dat_u->len_w); c3_w len_w = (nam_u->fra_w + 1 == dat_u->tot_w) ? dat_u->len_w : 1024; - if ( req_u->bao_u->con_w != nam_u->fra_w ) { + if ( req_u->los_u->counter != nam_u->fra_w ) { // TODO: queue packet u3_misord_buf* buf_u = c3_calloc(sizeof(u3_misord_buf)); buf_u->fra_y = c3_calloc(len_w); @@ -1048,7 +1049,7 @@ _mesa_req_pact_done(u3_mesa* sam_u, u3_mesa_name *nam_u, u3_mesa_data* dat_u, u3 buf_u->num_w = nam_u->fra_w; vec_append(&req_u->mis_u, buf_u); } - else if ( BAO_GOOD != (ver_y = blake_bao_verify(req_u->bao_u, buf_y, len_w, par_u)) ) { + else if ( c3y != (ver_y = lss_verifier_ingest(req_u->los_u, buf_y, len_w, par_u)) ) { c3_free(par_u); // TODO: do we drop the whole request on the floor? u3l_log("auth fail frag %u", nam_u->fra_w); @@ -1056,7 +1057,7 @@ _mesa_req_pact_done(u3_mesa* sam_u, u3_mesa_name *nam_u, u3_mesa_data* dat_u, u3 return req_u; } else if ( vec_len(&req_u->mis_u) != 0 - && BAO_GOOD != (ver_y = _mesa_burn_misorder_queue(req_u))) { + && c3y != (ver_y = _mesa_burn_misorder_queue(req_u))) { c3_free(par_u); MESA_LOG(AUTH) return req_u; @@ -1837,33 +1838,29 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) req_u->ack_w = 0; if ( c3y == lin_o ) { - u3_vec(c3_y[BLAKE3_OUT_LEN])* pof_u = vec_make(8); - { - blake_node* nod_u = blake_leaf_hash(dat_u->fra_y, dat_u->len_w, 0); - c3_y* lef_y = c3_calloc(BLAKE3_OUT_LEN); - make_chain_value(lef_y, nod_u); - c3_free(nod_u); - vec_append(pof_u, lef_y); - } - - for ( int i = 0; i < pac_u->pag_u.dat_u.aup_u.len_y; i++ ) { - c3_y* pof_y = c3_calloc(BLAKE3_OUT_LEN); - memcpy(pof_y, pac_u->pag_u.dat_u.aup_u.has_y[i], BLAKE3_OUT_LEN); - vec_append(pof_u, pof_y); + // complete the proof by computing the first leaf hash + c3_w len_w = pac_u->pag_u.dat_u.aup_u.len_y + 1; + lss_hash* pof_u = c3_calloc(len_w * 32); + for ( int i = 1; i < len_w; i++ ) { + memcpy(pof_u + (i * 32), pac_u->pag_u.dat_u.aup_u.has_y[i-1], 32); } + lss_complete_inline_proof(pof_u, dat_u->fra_y, dat_u->len_w); - req_u->bao_u = blake_bao_make(req_u->tot_w, pof_u); - blake_bao_verify(req_u->bao_u, dat_u->fra_y, dat_u->len_w, NULL); + // TODO: check return values + req_u->los_u = c3_calloc(sizeof(lss_verifier)); + lss_verifier_init(req_u->los_u, req_u->tot_w, pof_u, len_w); + c3_free(pof_u); + lss_verifier_ingest(req_u->los_u, dat_u->fra_y, dat_u->len_w, NULL); memcpy(req_u->dat_y, dat_u->fra_y, dat_u->len_w); } else { - c3_w len_w = dat_u->len_w / BLAKE3_OUT_LEN; - u3_vec(c3_y[BLAKE3_OUT_LEN])* pof_u = vec_make(len_w); - for ( int i = 0; i < len_w; i++ ) { - c3_y* pof_y = c3_calloc(BLAKE3_OUT_LEN); - memcpy(pof_y, dat_u->fra_y + (BLAKE3_OUT_LEN*i), BLAKE3_OUT_LEN); - vec_append(pof_u, pof_y); + // TODO: cast directly instead of copying? + lss_hash* pof_u = c3_calloc(dat_u->len_w * 32); + for ( int i = 0; i < dat_u->len_w; i++ ) { + memcpy(pof_u[i], dat_u->fra_y + (i * 32), 32); } - req_u->bao_u = blake_bao_make(req_u->tot_w, pof_u); + req_u->los_u = c3_calloc(sizeof(lss_verifier)); + lss_verifier_init(req_u->los_u, req_u->tot_w, pof_u, dat_u->len_w/32); + c3_free(pof_u); } vec_init(&req_u->mis_u, 8); From f6df09fe0eaf2958b5a083f8a60901664cac0faa Mon Sep 17 00:00:00 2001 From: pkova Date: Fri, 14 Jun 2024 22:46:50 +0300 Subject: [PATCH 151/430] mesa: demo nonsense --- pkg/vere/io/mesa.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 961f32fe11..5d4bf1fa11 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1657,21 +1657,19 @@ _mesa_page_scry_hunk_cb(void* vod_p, u3_noun nun) // u3_noun key = u3nc(u3k(pax), u3i_word(lop_w)); // u3h_put(sam_u->fin_s.sac_p, key, u3k(u3h(lis))); + pac_u->pek_u.nam_u.fra_w = len_w; + if ( (bat_w == 0) && (i == 0) ) { pac_u->pek_u.nam_u.nit_o = c3y; - pac_u->pek_u.nam_u.aut_o = c3n; + pac_u->pek_u.nam_u.aut_o = c3y; } else { pac_u->pek_u.nam_u.nit_o = c3n; pac_u->pek_u.nam_u.aut_o = c3n; } - if (fra_w == len_w && tag == MESA_WAIT) { - c3_w lun_w = _mesa_respond(pic_u, &buf_y, u3k(u3h(hit))); - pac_u->pek_u.nam_u.fra_w = fra_w; - _mesa_rout_bufs(sam_u, buf_y, lun_w, u3k(u3t(dat))); - } + c3_w lun_w = _mesa_respond(pic_u, &buf_y, u3k(u3h(hit))); + _mesa_rout_bufs(sam_u, buf_y, lun_w, u3k(u3t(dat))); - pac_u->pek_u.nam_u.fra_w = len_w; /* u3l_log("putting %u", pac_u->pek_u.nam_u.fra_w); */ /* _log_pact(pac_u); */ @@ -1685,7 +1683,6 @@ _mesa_page_scry_hunk_cb(void* vod_p, u3_noun nun) } i++; - pac_u->pek_u.nam_u.fra_w = len_w; } u3l_log("i %u", i); // u3z(old); @@ -2083,7 +2080,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) cad = u3nt(c3__heer, lan, u3i_slab_mint(&sab_u)); } - _mesa_del_request(sam_u, &pac_u->pag_u.nam_u); + /* _mesa_del_request(sam_u, &pac_u->pag_u.nam_u); */ u3_auto_plan(&sam_u->car_u, u3_ovum_init(0, c3__ames, wir, cad)); From b7c88b9eb8c300178094496f596ff7b25618426f Mon Sep 17 00:00:00 2001 From: midden-fabler Date: Fri, 14 Jun 2024 18:22:46 -0400 Subject: [PATCH 152/430] http: streaming wip --- pkg/vere/io/http.c | 216 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 213 insertions(+), 3 deletions(-) diff --git a/pkg/vere/io/http.c b/pkg/vere/io/http.c index 63812a4792..5f229da1ed 100644 --- a/pkg/vere/io/http.c +++ b/pkg/vere/io/http.c @@ -7,6 +7,7 @@ #include "openssl/err.h" #include "openssl/ssl.h" #include "version.h" +#include typedef struct _u3_h2o_serv { h2o_globalconf_t fig_u; // h2o global config @@ -44,7 +45,8 @@ typedef struct _u3_h2o_serv { typedef struct _u3_preq { struct _u3_hreq* req_u; // originating request (nullable) struct _u3_httd* htd_u; // device backpointer - u3_noun pax; // partial scry path + u3_noun pax; // partial scry path + c3_o las_o; // was scry at now } u3_preq; /* u3_hcon: incoming http connection. @@ -638,8 +640,55 @@ _http_seq_new(u3_hcon* hon_u, h2o_req_t* rec_u) return req_u; } +/* _http_foo_cb() +*/ +static void +// _http_cache_respond(u3_hreq* req_u, u3_noun nun); +_http_scry_respond(u3_hreq* req_u, u3_noun nun); + +static void +_http_foo_cb(void* vod_p, u3_noun nun) +{ + u3_preq* peq_u = vod_p; // TODO + u3_httd* htd_u = peq_u->htd_u; + u3_hreq* req_u = peq_u->req_u; + + if ( req_u ) { + u3_assert(u3_rsat_peek == req_u->sat_e); + req_u->peq_u = 0; + _http_scry_respond(req_u, u3k(nun)); + // _http_cache_respond(req_u, u3k(nun)); + } + + u3h_put(htd_u->nax_p, peq_u->pax, nun); + u3z(peq_u->pax); + c3_free(peq_u); +} + +static c3_c* +_find_tis_fas(void* txt, size_t len) +{ + c3_c* tis = memchr(txt, '=', len); + c3_c* fas = memchr(txt, '/', len); + + if ( tis && fas ) { + return c3_min(tis, fas); + } + else if ( tis ) { + return tis; + } + else { + return fas; + } +} /* _http_req_dispatch(): dispatch http request to %eyre */ +// TODO +// [x] don't blow up on bad paths +// [x] authentication +// [ ] caching +// [ ] range header +// static void _http_req_dispatch(u3_hreq* req_u, u3_noun req) { @@ -652,10 +701,13 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) u3_noun wir = _http_req_to_duct(req_u); u3_noun cad; + c3_c* base = req_u->rec_u->input.path.base; + c3_w len = req_u->rec_u->input.path.len; + { u3_noun adr = u3nc(c3__ipv4, u3i_words(1, &req_u->hon_u->ipf_w)); // XX loopback automatically secure too? - // + // u3_noun dat = u3nt(htp_u->sec, adr, req); cad = ( c3y == req_u->hon_u->htp_u->lop ) @@ -663,8 +715,166 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) : u3nc(u3i_string("request"), dat); } - u3_auto_plan(&htd_u->car_u, u3_ovum_init(0, c3__e, wir, cad)); + if ( (len >= 5) && (base[1] == '_') && (base[2] == '~') && (base[3] == '_') && (base[4] == '/')) { + base = base + 4; // retain '/' after /_~_ + len = len - 4; + + req_u->peq_u = c3_malloc(sizeof(*req_u->peq_u)); + req_u->peq_u->req_u = req_u; + req_u->peq_u->htd_u = htd_u; + req_u->sat_e = u3_rsat_peek; + + u3_hfig* fig_u = &req_u->hon_u->htp_u->htd_u->fig_u; + h2o_req_t* rec_u = req_u->rec_u; + + u3_noun our = u3dc("scot", 'p', u3i_chubs(2, htd_u->car_u.pir_u->who_d)); + + // set gang to [~ ~] or ~ + u3_noun gang; + c3_o auth = _http_req_is_auth(fig_u, rec_u); + if ( auth == c3y ) { + gang = u3nc(u3_nul, u3_nul); + } + else { + gang = u3_nul; + } + + u3_noun who; + u3_noun des; + u3_noun cas; + c3_o last = c3n; + + size_t i; + + // get beak from path + // + for (i = 0; i < 3; ++i) { + u3_noun* where; + if ( i == 0 ) { + where = &who; + } + else if ( i == 1 ) { + where = &des; + } + else { + where = &cas; + } + + // find '//' + if ( len >= 2 && base[0] == '/' && base[1] == '/' ) { + *where = u3_nul; + base++; + len--; + } + // skip '/' + else if ( len > 0 && base[0] == '/' ) { + base++; + len--; + } + // '=' + if ( len > 0 && base[0] == '=' ) { + if ( i == 0 ) { + *where = our; + } + else if ( i == 1 ) { + *where = u3i_string("base"); + } + else { + last = c3y; + } + base++; + len--; + } + // slice cord + else { + c3_c* nex = _find_tis_fas(base, len); + if ( !nex ) { + c3_c* msg_c = "bad beam"; + h2o_send_error_generic(req_u->rec_u, 400, msg_c, msg_c, 0); + return; + } + else { + c3_d mylen = nex - base; + *where = u3i_bytes(mylen, base); + base = nex; + len = len - mylen; + } + } + } + + u3_noun spur = u3dc("rush", u3i_bytes(len, (const c3_y*)base), u3v_wish("stap")); + + if ( spur == u3_nul ) { + h2o_send_error_generic(req_u->rec_u, 400, "bad spur", "bad spur", 0); + } + else { + if ( who != our ) { + c3_c* msg_c = "who != our"; + h2o_send_error_generic(req_u->rec_u, 400, msg_c, msg_c, 0); + return; + } + if ( c3y == last ) { + // DON'T CACHE + u3_pier_peek_last(htd_u->car_u.pir_u, gang, c3__ex, + des, u3t(spur), req_u->peq_u, _http_foo_cb); + } + + else { + u3_noun bem = u3nq(our, des, cas, u3t(spur)); + // TODO try to serve from cache + u3_pier_peek(htd_u->car_u.pir_u, gang, u3k(u3nt(0, c3__ex, bem)), + req_u->peq_u, _http_foo_cb); + } + } + } + + else { + // inject to arvo + u3_auto_plan(&htd_u->car_u, u3_ovum_init(0, c3__e, wir, cad)); + } + } +} + +static void +_http_scry_respond(u3_hreq* req_u, u3_noun nun) { + h2o_req_t* rec_u = req_u->rec_u; + u3_httd* htd_u = req_u->hon_u->htp_u->htd_u; + + if ( u3_nul == nun ) { + u3_weak req = _http_rec_to_httq(rec_u); + if ( u3_none == req ) { + if ( (u3C.wag_w & u3o_verbose) ) { + u3l_log("strange %.*s request", (c3_i)rec_u->method.len, + rec_u->method.base); + } + c3_c* msg_c = "bad request"; + h2o_send_error_generic(rec_u, 400, msg_c, msg_c, 0); + } + else { + h2o_send_error_500(rec_u, "Internal Server Error", "scry failed", 0); + } } + else if ( u3_none == u3r_at(15, nun) ) { + h2o_send_error_500(rec_u, "Internal Server Error", "scry failed", 0); + } + else { + u3_noun auth, response_header, data; + u3x_qual(u3k(u3t(u3t(nun))), &auth, 0, &response_header, &data); + u3_noun status, headers; + u3x_cell(response_header, &status, &headers); + + // check auth + if ( (c3y == auth) + && (c3n == _http_req_is_auth(&htd_u->fig_u, rec_u)) ) + { + h2o_send_error_403(rec_u, "Unauthorized", "unauthorized", 0); + } + else { + req_u->sat_e = u3_rsat_plan; + _http_start_respond(req_u, u3k(status), u3k(headers), u3k(data), c3y); + } + } + u3z(nun); } /* _http_cache_respond(): respond with a simple-payload:http From 91096b3653f5f4dd230bd82eaa1d1bb6e989a354 Mon Sep 17 00:00:00 2001 From: pkova Date: Sat, 15 Jun 2024 18:12:57 +0300 Subject: [PATCH 153/430] mesa: more demo nonsense --- pkg/vere/io/mesa.c | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 5d4bf1fa11..893bffb4ce 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -827,10 +827,10 @@ _try_resend(u3_pend_req* req_u) u3_mesa* sam_u = req_u->pic_u->sam_u; u3_lane* lan_u = &req_u->lan_u; c3_o los_o = c3n; - if ( req_u->tot_w == 0 || req_u->ack_w <= REORDER_THRESH ) { - /* u3l_log("reorder thresh too low %u", req_u->ack_w); */ - return; - } + /* if ( req_u->tot_w == 0 || req_u->ack_w <= REORDER_THRESH ) { */ + /* u3l_log("reorder thresh too low %u", req_u->ack_w); */ + /* return; */ + /* } */ c3_w ack_w = req_u->ack_w - REORDER_THRESH; c3_d now_d = _get_now_micros(); /* u3l_log("lef_w %u, nex_w %u, len_w %u, tot_w %u", req_u->lef_w, req_u->nex_w, req_u->len_w, req_u->tot_w); */ @@ -1661,12 +1661,18 @@ _mesa_page_scry_hunk_cb(void* vod_p, u3_noun nun) if ( (bat_w == 0) && (i == 0) ) { pac_u->pek_u.nam_u.nit_o = c3y; - pac_u->pek_u.nam_u.aut_o = c3y; + /* pac_u->pek_u.nam_u.aut_o = c3y; */ } else { pac_u->pek_u.nam_u.nit_o = c3n; pac_u->pek_u.nam_u.aut_o = c3n; } + /* if (len_w == 0) { */ + /* c3_w lun_w = _mesa_respond(pic_u, &buf_y, u3k(u3h(hit))); */ + /* /\* pac_u->pek_u.nam_u.fra_w = fra_w; *\/ */ + /* _mesa_rout_bufs(sam_u, buf_y, lun_w, u3k(u3t(dat))); */ + /* } */ + c3_w lun_w = _mesa_respond(pic_u, &buf_y, u3k(u3h(hit))); _mesa_rout_bufs(sam_u, buf_y, lun_w, u3k(u3t(dat))); @@ -1805,6 +1811,7 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) if ( req_u->tot_w <= 4 ) { return NULL; } + return NULL; } else { req_u = alloca(sizeof(u3_pend_req)); memset(req_u, 0, sizeof(u3_pend_req)); @@ -1992,6 +1999,18 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) }*/ u3_pend_req* req_u; + /* req_u = _mesa_req_pact_init(sam_u, pic_u, &lan_u); */ + /* if ( req_u == NULL ) { */ + /* req_u = _mesa_req_pact_done(sam_u, &pac_u->pag_u.nam_u, &pac_u->pag_u.dat_u, &lan_u); */ + /* if ( req_u == NULL ) { */ + /* // cleanup */ + /* /\* u3l_log("wrong"); *\/ */ + /* _log_pact(pac_u); */ + /* _mesa_free_pict(pic_u); */ + /* return; */ + /* } */ + /* } */ + if ( pac_u->pek_u.nam_u.nit_o == c3y ) { /* u3l_log("_mesa_req_pact_init NIT"); */ req_u = _mesa_req_pact_init(sam_u, pic_u, &lan_u); @@ -2003,9 +2022,13 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) req_u = _mesa_req_pact_done(sam_u, &pac_u->pag_u.nam_u, &pac_u->pag_u.dat_u, &lan_u); if ( req_u == NULL ) { // cleanup + /* u3l_log("wrong"); */ + /* _log_pact(pac_u); */ _mesa_free_pict(pic_u); return; } + /* u3l_log("right"); */ + /* _log_pact(pac_u); */ } if ( req_u == NULL ) { @@ -2013,6 +2036,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) return; } c3_w win_w = _mesa_req_get_cwnd(req_u); + /* c3_w win_w = 100000; */ u3_mesa_pict* nex_u = req_u->pic_u; c3_w nex_w = req_u->nex_w; if ( win_w != 0 ) { @@ -2069,7 +2093,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) memcpy(pac_u->pag_u.dat_u.fra_y, req_u->dat_y, pac_u->pag_u.dat_u.len_w); - u3l_log("last frag %u", pic_u->pac_u.pag_u.nam_u.fra_w); + /* u3l_log("last frag %u", pic_u->pac_u.pag_u.nam_u.fra_w); */ // this could be a retry, so we just rewrite the fragment to be the last one // pic_u->pac_u.pag_u.nam_u.fra_w = req_u->tot_w - 1; @@ -2080,7 +2104,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) cad = u3nt(c3__heer, lan, u3i_slab_mint(&sab_u)); } - /* _mesa_del_request(sam_u, &pac_u->pag_u.nam_u); */ + _mesa_del_request(sam_u, &pac_u->pag_u.nam_u); u3_auto_plan(&sam_u->car_u, u3_ovum_init(0, c3__ames, wir, cad)); @@ -2194,7 +2218,7 @@ _mesa_poke_news(u3_ovum* egg_u, u3_ovum_news new_e) if ( u3_ovum_done == new_e ) { // XX success stuff here - u3l_log("mesa: poke success"); + /* u3l_log("mesa: poke success"); */ } } From 5c603f45612d1a28ae0bb0400e084a8055847dd7 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Wed, 19 Jun 2024 11:06:13 +0200 Subject: [PATCH 154/430] mesa: inject hopcount with %heer --- pkg/vere/io/mesa.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 893bffb4ce..6195b1d148 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -2303,14 +2303,15 @@ _mesa_hear_poke(u3_mesa_pict* pic_u, u3_lane* lan_u) u3_noun wir = u3nc(c3__ames, u3_nul); u3_noun cad; { - u3_noun lan = u3_mesa_encode_lane(*lan_u); - u3i_slab sab_u; + u3_noun lan = u3_mesa_encode_lane(*lan_u); + u3_noun hop = u3i_chub(pac_u->hed_u.hop_y); // XX hop_w ? + u3i_slab sab_u; u3i_slab_init(&sab_u, 3, PACT_SIZE); // XX should just preserve input buffer mesa_etch_pact(sab_u.buf_y, pac_u); - cad = u3nt(c3__heer, lan, u3i_slab_mint(&sab_u)); + cad = u3nq(c3__heer, hop, lan, u3i_slab_mint(&sab_u)); } if ( 1 == pac_u->pok_u.dat_u.tot_w ) { From d9d9bc872e84ca54afdd6a3e58b1515e3f74d7a1 Mon Sep 17 00:00:00 2001 From: midden-fabler Date: Mon, 24 Jun 2024 12:34:35 -0400 Subject: [PATCH 155/430] http: serve from cache --- pkg/vere/io/http.c | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/pkg/vere/io/http.c b/pkg/vere/io/http.c index 5f229da1ed..69fd954328 100644 --- a/pkg/vere/io/http.c +++ b/pkg/vere/io/http.c @@ -640,16 +640,18 @@ _http_seq_new(u3_hcon* hon_u, h2o_req_t* rec_u) return req_u; } -/* _http_foo_cb() -*/ static void -// _http_cache_respond(u3_hreq* req_u, u3_noun nun); +_http_cache_respond(u3_hreq* req_u, u3_noun nun); + +static void _http_scry_respond(u3_hreq* req_u, u3_noun nun); +/* _http_foo_cb() +*/ static void _http_foo_cb(void* vod_p, u3_noun nun) { - u3_preq* peq_u = vod_p; // TODO + u3_preq* peq_u = vod_p; u3_httd* htd_u = peq_u->htd_u; u3_hreq* req_u = peq_u->req_u; @@ -657,10 +659,11 @@ _http_foo_cb(void* vod_p, u3_noun nun) u3_assert(u3_rsat_peek == req_u->sat_e); req_u->peq_u = 0; _http_scry_respond(req_u, u3k(nun)); - // _http_cache_respond(req_u, u3k(nun)); } - u3h_put(htd_u->nax_p, peq_u->pax, nun); + if ( peq_u->las_o == c3n ) { + u3h_put(htd_u->nax_p, peq_u->pax, nun); + } u3z(peq_u->pax); c3_free(peq_u); } @@ -686,7 +689,8 @@ _find_tis_fas(void* txt, size_t len) // TODO // [x] don't blow up on bad paths // [x] authentication -// [ ] caching +// [x] caching +// [ ] insert mime in path // [ ] range header // static void @@ -821,9 +825,17 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) else { u3_noun bem = u3nq(our, des, cas, u3t(spur)); - // TODO try to serve from cache - u3_pier_peek(htd_u->car_u.pir_u, gang, u3k(u3nt(0, c3__ex, bem)), - req_u->peq_u, _http_foo_cb); + u3_weak nac = u3h_get(htd_u->nax_p, bem); + + if ( u3_none == nac ) { + req_u->peq_u->las_o = c3n; + req_u->peq_u->pax = u3k(bem); + u3_pier_peek(htd_u->car_u.pir_u, gang, u3k(u3nt(0, c3__ex, bem)), + req_u->peq_u, _http_foo_cb); + } + else { + _http_cache_respond(req_u, nac); + } } } } From b3987e2542afb4d19e3689090b4572ad0c1e8da3 Mon Sep 17 00:00:00 2001 From: Pyry Kovanen Date: Tue, 25 Jun 2024 18:21:20 +0300 Subject: [PATCH 156/430] crc: tidy up the crc32 jet --- pkg/noun/jets/e/crc32.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/pkg/noun/jets/e/crc32.c b/pkg/noun/jets/e/crc32.c index efc865d603..192af8678f 100644 --- a/pkg/noun/jets/e/crc32.c +++ b/pkg/noun/jets/e/crc32.c @@ -11,8 +11,10 @@ u3_noun u3qe_crc32(u3_noun input_octs) { + u3_atom head = u3h(input_octs); u3_atom tail = u3t(input_octs); - c3_w len_w = u3r_met(3, tail); + c3_w tel_w = u3r_met(3, tail); + c3_w hed_w = u3r_met(3, head); c3_y* input; if (c3y == u3a_is_cat(tail)) { @@ -23,19 +25,22 @@ u3qe_crc32(u3_noun input_octs) input = (c3_y*)vat_u->buf_w; } - u3_atom head = u3h(input_octs); - c3_w leading_zeros = head - len_w; - c3_w crc = 0L; + if ( tel_w > hed_w ) { + return u3m_error("subtract-underflow"); + } + + c3_w led_w = hed_w - tel_w; + c3_w crc_w = 0; - crc = crc32(crc, input, len_w); + crc_w = crc32(crc, input, tel_w); - while (leading_zeros > 0) { - c3_y lz_input = 0; - crc = crc32(crc, &lz_input, 1); - leading_zeros--; + while ( led_w > 0 ) { + c3_y byt_y = 0; + crc_w = crc32(crc, &byt_y, 1); + led_w--; } - return u3i_word(crc); + return u3i_word(crc_w); } u3_noun From a6eac431ae74aa01a328c4f43c65cb08051e96ad Mon Sep 17 00:00:00 2001 From: Pyry Kovanen Date: Tue, 25 Jun 2024 18:24:37 +0300 Subject: [PATCH 157/430] crc: crc -> crc_w --- pkg/noun/jets/e/crc32.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/noun/jets/e/crc32.c b/pkg/noun/jets/e/crc32.c index 192af8678f..56a7532eff 100644 --- a/pkg/noun/jets/e/crc32.c +++ b/pkg/noun/jets/e/crc32.c @@ -32,11 +32,11 @@ u3qe_crc32(u3_noun input_octs) c3_w led_w = hed_w - tel_w; c3_w crc_w = 0; - crc_w = crc32(crc, input, tel_w); + crc_w = crc32(crc_w, input, tel_w); while ( led_w > 0 ) { c3_y byt_y = 0; - crc_w = crc32(crc, &byt_y, 1); + crc_w = crc32(crc_w, &byt_y, 1); led_w--; } From 1e6253d3c49371c835d7b79e386bdf0087a0cdf8 Mon Sep 17 00:00:00 2001 From: Pyry Kovanen Date: Tue, 25 Jun 2024 19:28:25 +0300 Subject: [PATCH 158/430] crc: fix bogus head --- pkg/noun/jets/e/crc32.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pkg/noun/jets/e/crc32.c b/pkg/noun/jets/e/crc32.c index 56a7532eff..1afc473a30 100644 --- a/pkg/noun/jets/e/crc32.c +++ b/pkg/noun/jets/e/crc32.c @@ -14,7 +14,10 @@ u3qe_crc32(u3_noun input_octs) u3_atom head = u3h(input_octs); u3_atom tail = u3t(input_octs); c3_w tel_w = u3r_met(3, tail); - c3_w hed_w = u3r_met(3, head); + c3_w hed_w; + if ( c3n == u3r_safe_word(head, &hed_w) ) { + return u3m_bail(c3__fail); + } c3_y* input; if (c3y == u3a_is_cat(tail)) { From badaac4cbdfed31ecfab1376126cc4554fcd84e4 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Wed, 26 Jun 2024 13:14:44 +0200 Subject: [PATCH 159/430] Revert "mesa: inject hopcount with %heer" This reverts commit 5c603f45612d1a28ae0bb0400e084a8055847dd7. --- pkg/vere/io/mesa.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 6195b1d148..893bffb4ce 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -2303,15 +2303,14 @@ _mesa_hear_poke(u3_mesa_pict* pic_u, u3_lane* lan_u) u3_noun wir = u3nc(c3__ames, u3_nul); u3_noun cad; { - u3_noun lan = u3_mesa_encode_lane(*lan_u); - u3_noun hop = u3i_chub(pac_u->hed_u.hop_y); // XX hop_w ? - u3i_slab sab_u; + u3_noun lan = u3_mesa_encode_lane(*lan_u); + u3i_slab sab_u; u3i_slab_init(&sab_u, 3, PACT_SIZE); // XX should just preserve input buffer mesa_etch_pact(sab_u.buf_y, pac_u); - cad = u3nq(c3__heer, hop, lan, u3i_slab_mint(&sab_u)); + cad = u3nt(c3__heer, lan, u3i_slab_mint(&sab_u)); } if ( 1 == pac_u->pok_u.dat_u.tot_w ) { From 55df4bb0942e1ffb6bb80666b84a89a5417ba6f7 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Wed, 26 Jun 2024 14:08:09 +0200 Subject: [PATCH 160/430] lss: add segafault comment --- pkg/vere/io/lss.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/vere/io/lss.c b/pkg/vere/io/lss.c index 7c3980dee6..1ca242fdc2 100644 --- a/pkg/vere/io/lss.c +++ b/pkg/vere/io/lss.c @@ -80,7 +80,7 @@ c3_o lss_verifier_ingest(lss_verifier* los_u, c3_y* leaf_y, c3_w leaf_w, lss_pai // verify leaf c3_y leaf_hash[32]; _leaf_hash(leaf_hash, leaf_y, leaf_w, los_u->counter); - if (!(lss_find_pair(los_u, 0, los_u->counter&1, leaf_hash))) { + if (!(lss_find_pair(los_u, 0, los_u->counter&1, leaf_hash))) { // XX segfaults return c3n; } // check whether pair is expected From 9e10ece92314a3612e19beefc410f0d3bbc47f10 Mon Sep 17 00:00:00 2001 From: Pyry Kovanen Date: Wed, 26 Jun 2024 18:08:19 +0300 Subject: [PATCH 161/430] crc: add comment to move the jet to 137 --- pkg/noun/jets/tree.c | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index 8ad0d8f45c..e4fc71643c 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -2352,6 +2352,7 @@ static u3j_core _138_hex_d[] = { "leer", 63, _140_hex_leer_a, 0, no_hashes }, { "loss", 63, _140_hex_loss_a, 0, no_hashes }, { "lune", 127, _140_hex_lune_a, 0, no_hashes }, +// XX move me to 137 when it exists { "crc", 31, 0, _137_hex__crc_d, no_hashes }, { "coed", 63, 0, _140_hex_coed_d, no_hashes }, From 1da4c5914615e2755eb1e757eeef0f33ccbcc828 Mon Sep 17 00:00:00 2001 From: lukechampine Date: Wed, 26 Jun 2024 13:25:05 -0400 Subject: [PATCH 162/430] lss: add tests and various fixes --- pkg/vere/BUILD.bazel | 12 ++++ pkg/vere/io/lss.c | 139 ++++++++++++++++++++++++++++++++++++------- pkg/vere/io/lss.h | 2 + pkg/vere/io/mesa.c | 17 +++++- 4 files changed, 146 insertions(+), 24 deletions(-) diff --git a/pkg/vere/BUILD.bazel b/pkg/vere/BUILD.bazel index a2b6623156..465505ce22 100644 --- a/pkg/vere/BUILD.bazel +++ b/pkg/vere/BUILD.bazel @@ -231,6 +231,18 @@ cc_test( defines = ["MESA_TEST"] ) +cc_test( + name = "lss_tests", + timeout = "short", + srcs = ["io/lss.c"], + features = select({ + "@platforms//os:linux": ["fully_static_link"], + "//conditions:default": [], + }), + deps = [":vere"], + defines = ["LSS_TEST"] +) + cc_test( name = "pact_tests", timeout = "short", diff --git a/pkg/vere/io/lss.c b/pkg/vere/io/lss.c index 1ca242fdc2..1444f5e24a 100644 --- a/pkg/vere/io/lss.c +++ b/pkg/vere/io/lss.c @@ -8,7 +8,7 @@ static c3_y IV[32] = {103, 230, 9, 106, 133, 174, 103, 187, 114, 243, 110, 60, 58, 245, 79, 165, 127, 82, 14, 81, 140, 104, 5, 155, 171, 217, 131, 31, 25, 205, 224, 91}; -static void _leaf_hash(c3_y out[32], c3_y* leaf, c3_w len, c3_d counter) +static void _leaf_hash(lss_hash out, c3_y* leaf, c3_w len, c3_d counter) { c3_y cv[32]; memcpy(cv, IV, 32); @@ -16,10 +16,11 @@ static void _leaf_hash(c3_y out[32], c3_y* leaf, c3_w len, c3_d counter) c3_y block_len = 0; c3_y flags = 0; urcrypt_blake3_chunk_output(len, leaf, cv, block, &block_len, &counter, &flags); - urcrypt_blake3_compress(cv, block, block_len, counter, flags, out); + urcrypt_blake3_compress(cv, block, block_len, counter, flags, block); + memcpy(out, block, 32); } -static void _parent_hash(c3_y out[32], c3_y left[32], c3_y right[32]) +static void _parent_hash(lss_hash out, lss_hash left, lss_hash right) { c3_y cv[32]; memcpy(cv, IV, 32); @@ -28,7 +29,8 @@ static void _parent_hash(c3_y out[32], c3_y left[32], c3_y right[32]) memcpy(block + 32, right, 32); c3_y block_len = 64; c3_y flags = 1 << 2; // PARENT - urcrypt_blake3_compress(cv, block, block_len, 0, flags, out); + urcrypt_blake3_compress(cv, block, block_len, 0, flags, block); + memcpy(out, block, 32); } static c3_y bits_ctz64(c3_d val_d) @@ -61,15 +63,15 @@ static c3_y bits_len64(c3_d val_d) return ret_y; } -static c3_o lss_find_pair(lss_verifier* los_u, c3_w height, c3_b sel, c3_y h[32]) +static c3_o lss_find_pair(lss_verifier* los_u, c3_w height, c3_b sel, lss_hash h) { - if (memcmp(los_u->pairs[height][sel], h, 32) == 0) { + if (memcmp((*los_u->pairs[height])[sel], h, 32) == 0) { return c3y; } // look for "irregular" pairs, which will always be on the right c3_w max_height = bits_len64(los_u->leaves); for (height++; height < max_height; height++) { - if (memcmp(los_u->pairs[height][1], h, 32) == 0) { + if (memcmp((*los_u->pairs[height])[1], h, 32) == 0) { return c3y; } } @@ -78,39 +80,42 @@ static c3_o lss_find_pair(lss_verifier* los_u, c3_w height, c3_b sel, c3_y h[32] c3_o lss_verifier_ingest(lss_verifier* los_u, c3_y* leaf_y, c3_w leaf_w, lss_pair* pair) { // verify leaf - c3_y leaf_hash[32]; + lss_hash leaf_hash; _leaf_hash(leaf_hash, leaf_y, leaf_w, los_u->counter); - if (!(lss_find_pair(los_u, 0, los_u->counter&1, leaf_hash))) { // XX segfaults + if ( c3n == lss_find_pair(los_u, 0, los_u->counter&1, leaf_hash) ) { return c3n; } // check whether pair is expected c3_w height = bits_ctz64(los_u->counter) + 1; - if ((pair != NULL) != ((los_u->counter == 0) || (los_u->counter + (1 << height) < los_u->leaves))) { + if ((pair != NULL) != ((los_u->counter != 0) && (los_u->counter + (1 << height) < los_u->leaves))) { return c3n; } else if (pair == NULL) { + los_u->counter++; return c3y; } // verify and insert pair c3_b sel = ~(los_u->counter >> height) & 1; - c3_y parent_hash[32]; + lss_hash parent_hash; _parent_hash(parent_hash, (*pair)[0], (*pair)[1]); - if (!lss_find_pair(los_u, height, sel, parent_hash)) { + if ( c3n == lss_find_pair(los_u, height, sel, parent_hash) ) { return c3n; } los_u->pairs[height-1] = pair; + los_u->counter++; return c3y; } c3_o lss_verifier_init(lss_verifier* los_u, c3_w leaves, lss_hash* proof, c3_w proof_w) { - if (proof_w < 2 || proof_w > bits_len64(leaves)) { - return c3n; - } los_u->leaves = leaves; los_u->counter = 0; - los_u->pairs = c3_calloc(bits_len64(leaves) * sizeof(lss_pair*)); - memcpy(los_u->pairs[0][0], proof[0], 32); + c3_w pairs_w = bits_len64(leaves); + los_u->pairs = c3_calloc(pairs_w * sizeof(lss_pair*)); + for (c3_w i = 0; i < proof_w-1; i++) { + los_u->pairs[i] = c3_calloc(sizeof(lss_pair)); + } + memcpy((*los_u->pairs[0])[0], proof[0], 32); for (c3_w i = 1; i < proof_w; i++) { - memcpy(los_u->pairs[i-1][1], proof[i], 32); + memcpy((*los_u->pairs[i-1])[1], proof[i], 32); } return c3y; } @@ -121,7 +126,99 @@ void lss_verifier_free(lss_verifier* los_u) { } void lss_complete_inline_proof(lss_hash* proof, c3_y* leaf_y, c3_w leaf_w) { - c3_y leaf_hash[32]; - _leaf_hash(leaf_hash, leaf_y, leaf_w, 0); - memcpy(proof, leaf_hash, 32); + _leaf_hash(proof[0], leaf_y, leaf_w, 0); +} + +void lss_root(lss_hash root, lss_hash* proof, c3_w proof_w) { + memcpy(root, proof[0], sizeof(lss_hash)); + for (c3_w i = 1; i < proof_w; i++) { + _parent_hash(root, root, proof[i]); + } +} + +#ifdef LSS_TEST + +static void _test_bits_ctz64() +{ + #define asrt(x,y) if ( x != y ) { fprintf(stderr, "failed (line: %u) got %u expected %u", __LINE__, x,y); exit(1); } + asrt(bits_ctz64(0ULL), 64); + asrt(bits_ctz64(0x0000000000000001ULL), 0); + asrt(bits_ctz64(0x0000000000000002ULL), 1); + asrt(bits_ctz64(0x0000000000000100ULL), 8); + asrt(bits_ctz64(0x0000000080000000ULL), 31); + asrt(bits_ctz64(0x0000000100000000ULL), 32); + asrt(bits_ctz64(0x4000000000000000ULL), 62); + asrt(bits_ctz64(0x8000000000000000ULL), 63); + #undef asrt +} + +static lss_pair* _make_pair(lss_hash left, lss_hash right) +{ + lss_pair* pair = c3_malloc(sizeof(lss_pair)); + memcpy((*pair)[0], left, 32); + memcpy((*pair)[1], right, 32); + return pair; } + +static void _test_lss_8() +{ + #define asrt_ok(y) if ( c3y != y ) { fprintf(stderr, "failed (line: %u)", __LINE__); exit(1); } + + c3_w dat_w = 1024 * 7 + 1; + c3_y* dat_y = c3_calloc(dat_w); + + c3_y* leaves_y[8]; + c3_w leaves_w[8]; + lss_hash leaves_h[8]; + lss_pair *pairs[8]; + + // construct leaves + for ( c3_w i = 0; i < 8; i++ ) { + leaves_y[i] = dat_y + 1024 * i; + leaves_w[i] = i < 7 ? 1024 : 1; + _leaf_hash(leaves_h[i], leaves_y[i], leaves_w[i], i); + pairs[i] = NULL; + } + // construct pairs + pairs[1] = _make_pair(leaves_h[2], leaves_h[3]); + pairs[3] = _make_pair(leaves_h[4], leaves_h[5]); + pairs[5] = _make_pair(leaves_h[6], leaves_h[7]); + lss_hash pair3_hash; + _parent_hash(pair3_hash, (*pairs[3])[0], (*pairs[3])[1]); + lss_hash pair5_hash; + _parent_hash(pair5_hash, (*pairs[5])[0], (*pairs[5])[1]); + pairs[2] = _make_pair(pair3_hash, pair5_hash); + + // construct proof + lss_hash *proof = c3_calloc(4 * sizeof(lss_hash)); + memcpy(proof[0], leaves_h[0], 32); + memcpy(proof[1], leaves_h[1], 32); + _parent_hash(proof[2], leaves_h[2], leaves_h[3]); + _parent_hash(proof[3], (*pairs[2])[0], (*pairs[2])[1]); + + // verify + lss_verifier lss_u; + memset(&lss_u, 0, sizeof(lss_verifier)); + asrt_ok(lss_verifier_init(&lss_u, 8, proof, 4)) + // for ( c3_w i = 0; i < 8; i++ ) { + // asrt_ok(lss_verifier_ingest(&lss_u, leaves_y[i], leaves_w[i], pairs[i])) + // } + asrt_ok(lss_verifier_ingest(&lss_u, leaves_y[0], leaves_w[0], pairs[0])) + asrt_ok(lss_verifier_ingest(&lss_u, leaves_y[1], leaves_w[1], pairs[1])) + asrt_ok(lss_verifier_ingest(&lss_u, leaves_y[2], leaves_w[2], pairs[2])) + asrt_ok(lss_verifier_ingest(&lss_u, leaves_y[3], leaves_w[3], pairs[3])) + asrt_ok(lss_verifier_ingest(&lss_u, leaves_y[4], leaves_w[4], pairs[4])) + asrt_ok(lss_verifier_ingest(&lss_u, leaves_y[5], leaves_w[5], pairs[5])) + asrt_ok(lss_verifier_ingest(&lss_u, leaves_y[6], leaves_w[6], pairs[6])) + asrt_ok(lss_verifier_ingest(&lss_u, leaves_y[7], leaves_w[7], pairs[7])) + + #undef asrt_ok +} + +int main() { + _test_bits_ctz64(); + _test_lss_8(); + return 0; +} + +#endif diff --git a/pkg/vere/io/lss.h b/pkg/vere/io/lss.h index 37a8d95f18..570969c537 100644 --- a/pkg/vere/io/lss.h +++ b/pkg/vere/io/lss.h @@ -25,4 +25,6 @@ void lss_verifier_free(lss_verifier* ver_u); void lss_complete_inline_proof(lss_hash* proof, c3_y* leaf_y, c3_w leaf_w); +void lss_root(lss_hash root, lss_hash* proof, c3_w proof_w); + #endif diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 802a79220e..ab2008d8ab 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1862,12 +1862,19 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) memcpy(pof_u + (i * 32), pac_u->pag_u.dat_u.aup_u.has_y[i-1], 32); } lss_complete_inline_proof(pof_u, dat_u->fra_y, dat_u->len_w); + // TODO: authenticate root + // lss_root root; + // lss_root(root, pof_u, len_w); + - // TODO: check return values req_u->los_u = c3_calloc(sizeof(lss_verifier)); - lss_verifier_init(req_u->los_u, req_u->tot_w, pof_u, len_w); + if ( c3n == lss_verifier_init(req_u->los_u, req_u->tot_w, pof_u, len_w) ) { + return NULL; // XX ??? + } c3_free(pof_u); - lss_verifier_ingest(req_u->los_u, dat_u->fra_y, dat_u->len_w, NULL); + if ( c3n == lss_verifier_ingest(req_u->los_u, dat_u->fra_y, dat_u->len_w, NULL) ) { + return NULL; // XX ??? + } memcpy(req_u->dat_y, dat_u->fra_y, dat_u->len_w); } else { // TODO: cast directly instead of copying? @@ -1875,6 +1882,10 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) for ( int i = 0; i < dat_u->len_w; i++ ) { memcpy(pof_u[i], dat_u->fra_y + (i * 32), 32); } + // TODO: authenticate root + // lss_root root; + // lss_root(root, pof_u, dat_u->len_w/32); + req_u->los_u = c3_calloc(sizeof(lss_verifier)); lss_verifier_init(req_u->los_u, req_u->tot_w, pof_u, dat_u->len_w/32); c3_free(pof_u); From 3e5f9aa30c4f88a1e11b9ce71cbd8dbf95093fd7 Mon Sep 17 00:00:00 2001 From: lukechampine Date: Wed, 26 Jun 2024 13:58:47 -0400 Subject: [PATCH 163/430] lss: tweak verifier struct --- pkg/vere/io/lss.c | 16 +++++++--------- pkg/vere/io/lss.h | 6 +++--- pkg/vere/io/mesa.c | 17 ++++++++--------- 3 files changed, 18 insertions(+), 21 deletions(-) diff --git a/pkg/vere/io/lss.c b/pkg/vere/io/lss.c index 1444f5e24a..d6b845025c 100644 --- a/pkg/vere/io/lss.c +++ b/pkg/vere/io/lss.c @@ -65,13 +65,13 @@ static c3_y bits_len64(c3_d val_d) static c3_o lss_find_pair(lss_verifier* los_u, c3_w height, c3_b sel, lss_hash h) { - if (memcmp((*los_u->pairs[height])[sel], h, 32) == 0) { + if (memcmp(los_u->pairs[height][sel], h, 32) == 0) { return c3y; } // look for "irregular" pairs, which will always be on the right c3_w max_height = bits_len64(los_u->leaves); for (height++; height < max_height; height++) { - if (memcmp((*los_u->pairs[height])[1], h, 32) == 0) { + if (memcmp(los_u->pairs[height][1], h, 32) == 0) { return c3y; } } @@ -100,7 +100,8 @@ c3_o lss_verifier_ingest(lss_verifier* los_u, c3_y* leaf_y, c3_w leaf_w, lss_pai if ( c3n == lss_find_pair(los_u, height, sel, parent_hash) ) { return c3n; } - los_u->pairs[height-1] = pair; + memcpy(los_u->pairs[height-1][0], (*pair)[0], sizeof(lss_hash)); + memcpy(los_u->pairs[height-1][1], (*pair)[1], sizeof(lss_hash)); los_u->counter++; return c3y; } @@ -109,13 +110,10 @@ c3_o lss_verifier_init(lss_verifier* los_u, c3_w leaves, lss_hash* proof, c3_w p los_u->leaves = leaves; los_u->counter = 0; c3_w pairs_w = bits_len64(leaves); - los_u->pairs = c3_calloc(pairs_w * sizeof(lss_pair*)); - for (c3_w i = 0; i < proof_w-1; i++) { - los_u->pairs[i] = c3_calloc(sizeof(lss_pair)); - } - memcpy((*los_u->pairs[0])[0], proof[0], 32); + los_u->pairs = c3_calloc(pairs_w * sizeof(lss_pair)); + memcpy(los_u->pairs[0][0], proof[0], sizeof(lss_hash)); for (c3_w i = 1; i < proof_w; i++) { - memcpy((*los_u->pairs[i-1])[1], proof[i], 32); + memcpy(los_u->pairs[i-1][1], proof[i], sizeof(lss_hash)); } return c3y; } diff --git a/pkg/vere/io/lss.h b/pkg/vere/io/lss.h index 570969c537..e7e7f59e38 100644 --- a/pkg/vere/io/lss.h +++ b/pkg/vere/io/lss.h @@ -12,9 +12,9 @@ typedef c3_y lss_hash[32]; typedef lss_hash lss_pair[2]; typedef struct _lss_verifier { - c3_w leaves; - c3_w counter; - lss_pair** pairs; + c3_w leaves; + c3_w counter; + lss_pair* pairs; } lss_verifier; c3_o lss_verifier_ingest(lss_verifier* ver_u, c3_y* leaf_y, c3_w leaf_w, lss_pair* pair); diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index ab2008d8ab..f93d9da102 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1026,8 +1026,8 @@ _mesa_req_pact_done(u3_mesa* sam_u, u3_mesa_name *nam_u, u3_mesa_data* dat_u, u3 if ( dat_u->aum_u.typ_e == AUTH_NEXT ) { // needs to be heap allocated bc will be saved if misordered par_u = c3_calloc(sizeof(lss_pair)); - memcpy((*par_u)[0], dat_u->aup_u.has_y[0], sizeof(dat_u->aup_u.has_y[0])); - memcpy((*par_u)[1], dat_u->aup_u.has_y[1], sizeof(dat_u->aup_u.has_y[1])); + memcpy((*par_u)[0], dat_u->aup_u.has_y[0], sizeof(lss_hash)); + memcpy((*par_u)[1], dat_u->aup_u.has_y[1], sizeof(lss_hash)); } c3_y ver_y; @@ -1857,30 +1857,29 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) if ( c3y == lin_o ) { // complete the proof by computing the first leaf hash c3_w len_w = pac_u->pag_u.dat_u.aup_u.len_y + 1; - lss_hash* pof_u = c3_calloc(len_w * 32); + lss_hash* pof_u = c3_calloc(len_w * sizeof(lss_hash)); for ( int i = 1; i < len_w; i++ ) { - memcpy(pof_u + (i * 32), pac_u->pag_u.dat_u.aup_u.has_y[i-1], 32); + memcpy(pof_u[i], pac_u->pag_u.dat_u.aup_u.has_y[i-1], sizeof(lss_hash)); } lss_complete_inline_proof(pof_u, dat_u->fra_y, dat_u->len_w); // TODO: authenticate root // lss_root root; // lss_root(root, pof_u, len_w); - req_u->los_u = c3_calloc(sizeof(lss_verifier)); - if ( c3n == lss_verifier_init(req_u->los_u, req_u->tot_w, pof_u, len_w) ) { + if ( c3y != lss_verifier_init(req_u->los_u, req_u->tot_w, pof_u, len_w) ) { return NULL; // XX ??? } c3_free(pof_u); - if ( c3n == lss_verifier_ingest(req_u->los_u, dat_u->fra_y, dat_u->len_w, NULL) ) { + if ( c3y != lss_verifier_ingest(req_u->los_u, dat_u->fra_y, dat_u->len_w, NULL) ) { return NULL; // XX ??? } memcpy(req_u->dat_y, dat_u->fra_y, dat_u->len_w); } else { // TODO: cast directly instead of copying? - lss_hash* pof_u = c3_calloc(dat_u->len_w * 32); + lss_hash* pof_u = c3_calloc(dat_u->len_w * sizeof(lss_hash)); for ( int i = 0; i < dat_u->len_w; i++ ) { - memcpy(pof_u[i], dat_u->fra_y + (i * 32), 32); + memcpy(pof_u[i], dat_u->fra_y + (i * sizeof(lss_hash)), sizeof(lss_hash)); } // TODO: authenticate root // lss_root root; From 0c007582897729fbbdb54fd63dc88d0d27e8b57d Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Thu, 27 Jun 2024 15:41:47 +0200 Subject: [PATCH 164/430] mesa: wip jumbo frames --- pkg/vere/io/mesa.c | 94 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 90 insertions(+), 4 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index f93d9da102..c45d247ab6 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1306,7 +1306,7 @@ static c3_o _mesa_kick(u3_mesa* sam_u, u3_noun tag, u3_noun dat) u3l_log(" mesa: send no"); ret_o = c3n; } else { - /* u3l_log(" mesa: send yes"); */ + u3l_log(" mesa: send yes"); _mesa_ef_send(sam_u, u3k(las), u3k(pac)); ret_o = c3y; } @@ -1538,6 +1538,23 @@ _name_to_batch_scry(u3_mesa_name* nam_u, c3_w lop_w, c3_w len_w) return bat; } +static u3_noun +_name_to_jumbo_scry(u3_mesa_name* nam_u, c3_w lop_w, c3_w len_w) +{ + u3_noun rif = _dire_etch_ud(nam_u->rif_w); + u3_noun boq = _dire_etch_ud(31); + u3_noun fag = _dire_etch_ud(0); // XX 1 + u3_noun pax = _mesa_encode_path(nam_u->pat_s, (c3_y*)nam_u->pat_c); + + u3_noun wer = nam_u->nit_o == c3y + ? u3nc(c3__init, pax) + : u3nt(nam_u->aut_o == c3y ? c3__auth : c3__data, fag, pax); + + u3_noun res = u3nc(c3__mess, u3nq(rif, c3__pact, boq, u3nc(c3__etch, wer))); + + return res; +} + /* * RETAIN */ @@ -1574,7 +1591,6 @@ _mesa_respond(u3_mesa_pict* req_u, c3_y** buf_y, u3_noun hit) { c3_w len_w = u3r_met(3, hit); - *buf_y = c3_calloc(len_w); u3r_bytes(0, len_w, *buf_y, hit); @@ -1699,6 +1715,72 @@ _mesa_page_scry_hunk_cb(void* vod_p, u3_noun nun) // u3z(pax); } +static void +_mesa_page_scry_jumbo_cb(void* vod_p, u3_noun nun) +{ + u3_mesa_pict* pic_u = vod_p; + u3_mesa_pact* pac_u = &pic_u->pac_u; + u3_mesa* sam_u = pic_u->sam_u; + //u3_noun pax = _mesa_path_with_fra(pac_u->pek_u.nam_u.pat_c, &fra_s); + + u3_weak hit = u3r_at(7, nun); + if ( u3_none == hit ) { + // TODO: mark as dead + //u3z(nun); + u3l_log("unbound"); + } else { + c3_w fra_w = pac_u->pek_u.nam_u.fra_w; + u3_weak old = _mesa_get_cache(sam_u, &pac_u->pag_u.nam_u); + if ( old == u3_none ) { + u3l_log("bad"); + MESA_LOG(APATHY); + } else { + u3_noun tag; + u3_noun dat; + u3x_cell(u3k(old), &tag, &dat); + c3_w siz = u3r_met(13, hit); + c3_y* buf_y; + + /* u3l_log("path %s", pac_u->pek_u.nam_u.pat_c); */ + u3_mesa_pact* fac_u; + + c3_w len_w = u3r_met(3, hit); + c3_y* puf_y = c3_calloc(len_w); + u3r_bytes(0, len_w, puf_y, hit); + + // mesa_sift_pact(fac_u, puf_y, len_w); + // _log_buf(puf_y, len_w); + + u3_atom lin = u3qc_cut(13, 0, 1, hit); + + c3_w lin_w = u3r_met(3, lin); + c3_y* pef_y = c3_calloc(lin_w); + u3r_bytes(0, len_w, pef_y, lin); + mesa_sift_pact(fac_u, pef_y, lin_w); + + for (c3_w i=0; i < siz; i++) { + // (cut 3 [wid 1] dat.byts)) + u3_atom lin = u3qc_cut(13, i, 1, hit); + + pac_u->pek_u.nam_u.fra_w = i; + + _mesa_put_cache(sam_u, &pac_u->pek_u.nam_u, u3nc(MESA_ITEM, u3k(lin))); + + c3_w lun_w = _mesa_respond(pic_u, &buf_y, u3k(lin)); + _mesa_rout_bufs(sam_u, buf_y, lun_w, u3k(u3t(dat))); + + /* u3l_log("putting %u", pac_u->pek_u.nam_u.fra_w); */ + /* _log_pact(pac_u); */ + + } + + // u3z(old); + } + } + // u3z(hit); + // u3z(pax); +} + static void _mesa_hear_bail(u3_ovum* egg_u, u3_noun lud) { @@ -2200,7 +2282,11 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) } else { _mesa_add_lane_to_cache(sam_u, &pac_u->pek_u.nam_u, u3_nul, lan_u); // TODO: retrieve from namespace if ( c3y == our_o ) { - u3_noun sky = _name_to_batch_scry(&pac_u->pek_u.nam_u, + // u3_noun sky = _name_to_batch_scry(&pac_u->pek_u.nam_u, + // bat_w, + // bat_w + MESA_HUNK); + + u3_noun sky = _name_to_jumbo_scry(&pac_u->pek_u.nam_u, bat_w, bat_w + MESA_HUNK); @@ -2209,7 +2295,7 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) u3_noun our = u3i_chubs(2, sam_u->car_u.pir_u->who_d); u3_noun bem = u3nc(u3nt(our, u3_nul, u3nc(c3__ud, 1)), sky); // only branch where we do not free pic_u - u3_pier_peek(sam_u->car_u.pir_u, u3_nul, u3k(u3nq(1, c3__beam, c3__ax, bem)), pic_u, _mesa_page_scry_hunk_cb); + u3_pier_peek(sam_u->car_u.pir_u, u3_nul, u3k(u3nq(1, c3__beam, c3__ax, bem)), pic_u, _mesa_page_scry_jumbo_cb); } else { // XX unpossible _mesa_free_pict(pic_u); From 2cffa44b419235585e38e60ca19f714c58b5793a Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Fri, 28 Jun 2024 17:56:54 +0200 Subject: [PATCH 165/430] mesa: allocate tac_u on the stack --- pkg/vere/io/mesa.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index c45d247ab6..8e271eb08e 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1742,21 +1742,15 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun nun) c3_y* buf_y; /* u3l_log("path %s", pac_u->pek_u.nam_u.pat_c); */ - u3_mesa_pact* fac_u; + u3_mesa_pact tac_u; c3_w len_w = u3r_met(3, hit); + u3l_log("len_w %u", len_w); c3_y* puf_y = c3_calloc(len_w); u3r_bytes(0, len_w, puf_y, hit); - // mesa_sift_pact(fac_u, puf_y, len_w); - // _log_buf(puf_y, len_w); - - u3_atom lin = u3qc_cut(13, 0, 1, hit); - - c3_w lin_w = u3r_met(3, lin); - c3_y* pef_y = c3_calloc(lin_w); - u3r_bytes(0, len_w, pef_y, lin); - mesa_sift_pact(fac_u, pef_y, lin_w); + mesa_sift_pact(&tac_u, puf_y, len_w); + _log_pact(&tac_u); for (c3_w i=0; i < siz; i++) { // (cut 3 [wid 1] dat.byts)) From 3d9e45ddb8fa96d036c7fa85c7d72860a369cc30 Mon Sep 17 00:00:00 2001 From: Santeri Hannula Date: Thu, 30 May 2024 19:30:02 +0300 Subject: [PATCH 166/430] pier: double boot protection --- pkg/vere/pier.c | 206 ++++++++++++++++++++++++++++++++++++++++++++++-- pkg/vere/vere.h | 1 + 2 files changed, 202 insertions(+), 5 deletions(-) diff --git a/pkg/vere/pier.c b/pkg/vere/pier.c index 5f7b4593f5..f7115a466b 100644 --- a/pkg/vere/pier.c +++ b/pkg/vere/pier.c @@ -6,6 +6,7 @@ #include "pace.h" #include "vere.h" #include "version.h" +#include "curl/curl.h" #define PIER_READ_BATCH 1000ULL #define PIER_PLAY_BATCH 500ULL @@ -529,6 +530,181 @@ _pier_on_scry_done(void* ptr_v, u3_noun nun) u3z(nun); } +static size_t +_dawn_curl_alloc(void* dat_v, size_t uni_t, size_t mem_t, void* buf_v) +{ + uv_buf_t* buf_u = buf_v; + + size_t siz_t = uni_t * mem_t; + buf_u->base = c3_realloc(buf_u->base, 1 + siz_t + buf_u->len); + + memcpy(buf_u->base + buf_u->len, dat_v, siz_t); + buf_u->len += siz_t; + buf_u->base[buf_u->len] = 0; + + return siz_t; +} + +struct HttpRes { + c3_i cod_i; + c3_c* res_c; +}; + +static struct HttpRes http_get(c3_c* url_c) { + CURL *curl; + CURLcode result; + long cod_l; + + uv_buf_t buf_u = uv_buf_init(c3_malloc(1), 0); + + if ( !(curl = curl_easy_init()) ) { + u3l_log("failed to initialize libcurl"); + exit(1); + } + + u3K.ssl_curl_f(curl); + curl_easy_setopt(curl, CURLOPT_URL, url_c); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _dawn_curl_alloc); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&buf_u); + curl_easy_setopt(curl, CURLOPT_SERVER_RESPONSE_TIMEOUT, 30); + + result = curl_easy_perform(curl); + curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &cod_l); + + if ( CURLE_OK != result ) { + u3l_log("failed to fetch %s: %s", url_c, curl_easy_strerror(result)); + /* exit(1); */ + } + if ( 300 <= cod_l && 404 != cod_l ) { + u3l_log("error fetching %s: HTTP %ld", url_c, cod_l); + /* exit(1); */ + } + + curl_easy_cleanup(curl); + + struct HttpRes r = { cod_l, buf_u.base }; + return r; +} + +static c3_i fetch_ping_ack(c3_c* czar, c3_c* who, c3_i bon) { + c3_c url[256]; + sprintf(url, "https://%s.urbit.org/~/boot/%s/%d", czar, who, bon + 1); + struct HttpRes res = http_get(url); + if ( 200 != res.cod_i) { + return -1; + } + c3_i last_acked = strtoumax(res.res_c, NULL, 10); + return last_acked; +} + +static void +_boot_scry_cb(void* vod_p, u3_noun nun) { + u3_work* wok_u = (u3_work*)vod_p; + u3_atom who = u3dc("scot", c3__p, u3i_chubs(2, wok_u->pir_u->who_d)); + c3_c* who_c = u3r_string(who); + u3_noun nul, rem, typ, glx, ryf, lyf, bon, nex; + c3_w glx_w, ryf_w, bon_w, nex_w; + if (c3y == u3r_cell(nun, &nul, &rem) && + c3y == u3r_hext(rem, &typ, &glx, &ryf, &lyf, &bon, &nex)) { + /* + * Boot scry succeeded. Proceed to cross reference networking state against + * sponsoring galaxy. + */ + glx_w = u3r_word(0, glx); ryf_w = u3r_word(0, ryf); + bon_w = u3r_word(0, bon); nex_w = u3r_word(0, nex); + u3_atom czar = u3dc("scot", c3__p, glx_w); + c3_c* czar_c = u3r_string(czar); + c3_c url[256]; + sprintf(url, "https://%s.urbit.org/~/boot/%s", czar_c+1, who_c); + struct HttpRes res = http_get(url); + if (res.cod_i == 200) { + u3_noun jammed_atom = + u3dc("slav", c3__ud, u3do("crip", u3i_tape(res.res_c))); + u3_noun unpacked = u3ke_cue(u3k(jammed_atom)); + u3_noun czar_glx, czar_ryf, czar_lyf; + c3_w czar_ryf_w; + u3x_trel(unpacked, &czar_glx, &czar_ryf, &czar_lyf); + czar_ryf_w = u3r_word(0, czar_ryf); + if (czar_ryf_w == ryf_w) { + c3_i last_ack = fetch_ping_ack(czar_c, who_c, bon_w); + if (last_ack == -1) { + u3l_log("boot: peer state unvailable on czar, cannot protect from double boot"); + _pier_work(wok_u); + } else if (last_ack == nex_w - 1 || last_ack - 1 == nex_w - 1) { + _pier_work(wok_u); + } else { + u3l_log("boot: failed: czar last ack: %d, ship last ack: %d", + last_ack, nex_w - 1); + exit(1); + } + } else { + // Trying to boot old ship after breach + u3l_log("boot: failed: rift in czar peer state: %d, current rift: %d", + czar_ryf_w, ryf_w); + exit(1); + } + u3z(jammed_atom); u3z(unpacked); + } else { + u3l_log("boot: %%boot state unvailable on czar, cannot protect from double boot"); + _pier_work(wok_u); + } + u3z(czar); + c3_free(czar_c); + } else if (c3y == u3r_trel(nun, &nul, &typ, &rem) && + rem == 1634628973 /* %muna */) { + /* + * Data not available for boot scry. Check against sponsoring galaxy. + * If peer state exists exit(1) unless ship has breached, + * otherwise continue boot. + */ + c3_i sponsor = wok_u->pir_u->who_d[0] & ((1 << 8) - 1); + u3_noun czar = u3dc("scot", 'p', u3k(sponsor)); + c3_c* czar_c = u3r_string(u3k(czar)); + c3_c url[256]; + sprintf(url, "https://%s.urbit.org/~/sponsor/%s", czar_c+1, who_c); + struct HttpRes res = http_get(url); + if (res.cod_i == 200) { + czar_c = res.res_c; + } + sprintf(url, "https://%s.urbit.org/~/boot/%s", czar_c+1, who_c); + res = http_get(url); + if (res.cod_i == 200) { + // Peer state found under czar + u3_noun jammed_atom = + u3dc("slav", c3__ud, u3do("crip", u3i_tape(res.res_c))); + u3_noun unpacked = u3ke_cue(jammed_atom); + u3_noun czar_glx, czar_ryf, czar_lyf; + c3_w czar_ryf_w; + u3x_trel(unpacked, &czar_glx, &czar_ryf, &czar_lyf); + czar_ryf_w = u3r_word(0, czar_ryf); + u3_weak kf_ryf = wok_u->pir_u->ryf; + if (kf_ryf == u3_none) { + u3l_log("boot: keyfile rift unavailable, cannot protect from double boot"); + _pier_work(wok_u); + } else if (kf_ryf > czar_ryf_w) { + // Ship has breached, continue boot + _pier_work(wok_u); + } else { + u3l_log("boot: failed: ship not in sync with czar, double boot"); + exit(1); + } + u3z(czar); u3z(jammed_atom); u3z(unpacked); + c3_free(czar_c); + } else { + _pier_work(wok_u); + } + } else { + /* + * Boot scry endpoint doesn't exists. Most likely old arvo. + * Continue boot and hope for the best. + */ + u3l_log("boot: %%boot scry endpoint doesn't exist, cannot protect from double boot"); + _pier_work(wok_u); + } + u3z(nun); u3z(who); + c3_free(who_c); +} + /* _pier_work_init(): begin processing new events */ static void @@ -615,7 +791,20 @@ _pier_work_init(u3_pier* pir_u) u3_auto_talk(wok_u->car_u); } - _pier_work(wok_u); + c3_i pi_i = wok_u->pir_u->who_d[0]; + c3_i pt_i = wok_u->pir_u->who_d[1]; + + if ((pi_i < 256 && pt_i == 0) || c3n == u3_Host.ops_u.net) { + // Skip double boot protection for galaxies and local mode ships + // + _pier_work(wok_u); + } else { + // Double boot protection + // + u3_noun pex = u3nc(u3i_string("boot"), u3_nul); + u3_pier_peek_last(pir_u, u3nc(u3_nul, u3_nul), c3__ax, u3_nul, pex, + pir_u->wok_u, _boot_scry_cb); + } } /* _pier_wyrd_good(): %wyrd version negotation succeeded. @@ -1609,7 +1798,7 @@ u3_pier_slog(u3_pier* pir_u) /* _pier_init(): create a pier, loading existing. */ static u3_pier* -_pier_init(c3_w wag_w, c3_c* pax_c) +_pier_init(c3_w wag_w, c3_c* pax_c, u3_weak ryf) { // create pier // @@ -1618,6 +1807,7 @@ _pier_init(c3_w wag_w, c3_c* pax_c) pir_u->pax_c = pax_c; pir_u->sat_e = u3_psat_init; pir_u->liv_o = c3n; + pir_u->ryf = ryf; // XX remove // @@ -1691,8 +1881,9 @@ u3_pier* u3_pier_stay(c3_w wag_w, u3_noun pax) { u3_pier* pir_u; + u3_weak rift = u3_none; - if ( !(pir_u = _pier_init(wag_w, u3r_string(pax))) ) { + if ( !(pir_u = _pier_init(wag_w, u3r_string(pax), rift)) ) { fprintf(stderr, "pier: stay: init fail\r\n"); u3_king_bail(); return 0; @@ -1848,7 +2039,7 @@ _pier_boot_make(u3_noun who, // include additional key configuration events if we have multiple keys // - if ( (u3_none != fed) && (c3y == u3du(u3h(fed))) ) { + if ( (u3_none != fed) && (c3y == u3du(u3h(fed))) && (u3h(u3h(fed))) == 1) { u3_noun wir = u3nt(c3__j, c3__seed, u3_nul); u3_noun tag = u3i_string("rekey"); u3_noun kyz = u3t(u3t(fed)); @@ -2027,8 +2218,13 @@ u3_pier_boot(c3_w wag_w, // config flags u3_noun mor) // extra boot sequence props { u3_pier* pir_u; + u3_weak rift = u3_none; + if (fed != u3_none && c3y == u3du(u3h(fed)) && u3h(u3h(fed)) == 2) { + rift = u3h(u3t(u3t(fed))); + u3k(rift); + } - if ( !(pir_u = _pier_init(wag_w, u3r_string(pax))) ) { + if ( !(pir_u = _pier_init(wag_w, u3r_string(pax), rift)) ) { fprintf(stderr, "pier: boot: init fail\r\n"); u3_king_bail(); return 0; diff --git a/pkg/vere/vere.h b/pkg/vere/vere.h index f97a76f142..42c9024603 100644 --- a/pkg/vere/vere.h +++ b/pkg/vere/vere.h @@ -661,6 +661,7 @@ u3_disk* log_u; // event log u3_lord* god_u; // computer u3_psat sat_e; // type-tagged + u3_weak ryf; // rift union { // u3_boot* bot_u; // bootstrap u3_play* pay_u; // recompute From d0e6e4964322b15c21b8348186056a8115ffb039 Mon Sep 17 00:00:00 2001 From: Santeri Hannula Date: Thu, 13 Jun 2024 13:13:35 +0300 Subject: [PATCH 167/430] king: king_curl_alloc to header --- pkg/vere/dawn.c | 22 ++-------------------- pkg/vere/king.c | 8 ++++---- pkg/vere/pier.c | 17 +---------------- pkg/vere/vere.h | 5 +++++ 4 files changed, 12 insertions(+), 40 deletions(-) diff --git a/pkg/vere/dawn.c b/pkg/vere/dawn.c index ba474d10d4..37758e553e 100644 --- a/pkg/vere/dawn.c +++ b/pkg/vere/dawn.c @@ -41,24 +41,6 @@ _dawn_buf_to_oct(uv_buf_t buf_u) return u3nc(len, u3i_bytes(buf_u.len, (const c3_y*)buf_u.base)); } - -/* _dawn_curl_alloc(): allocate a response buffer for curl -*/ -static size_t -_dawn_curl_alloc(void* dat_v, size_t uni_t, size_t mem_t, void* buf_v) -{ - uv_buf_t* buf_u = buf_v; - - size_t siz_t = uni_t * mem_t; - buf_u->base = c3_realloc(buf_u->base, 1 + siz_t + buf_u->len); - - memcpy(buf_u->base + buf_u->len, dat_v, siz_t); - buf_u->len += siz_t; - buf_u->base[buf_u->len] = 0; - - return siz_t; -} - /* _dawn_post_json(): POST JSON to url_c */ static uv_buf_t @@ -84,7 +66,7 @@ _dawn_post_json(c3_c* url_c, uv_buf_t lod_u) // u3K.ssl_curl_f(curl); curl_easy_setopt(curl, CURLOPT_URL, url_c); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _dawn_curl_alloc); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, king_curl_alloc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&buf_u); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, hed_u); @@ -131,7 +113,7 @@ _dawn_get_jam(c3_c* url_c) // u3K.ssl_curl_f(curl); curl_easy_setopt(curl, CURLOPT_URL, url_c); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _dawn_curl_alloc); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, king_curl_alloc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&buf_u); result = curl_easy_perform(curl); diff --git a/pkg/vere/king.c b/pkg/vere/king.c index 66181c6f6a..03095afc83 100644 --- a/pkg/vere/king.c +++ b/pkg/vere/king.c @@ -270,11 +270,11 @@ _king_pier(u3_noun pier) u3z(pier); } -/* _king_curl_alloc(): allocate a response buffer for curl +/* king_curl_alloc(): allocate a response buffer for curl ** XX deduplicate with dawn.c */ -static size_t -_king_curl_alloc(void* dat_v, size_t uni_t, size_t mem_t, void* buf_v) +size_t +king_curl_alloc(void* dat_v, size_t uni_t, size_t mem_t, void* buf_v) { uv_buf_t* buf_u = buf_v; @@ -308,7 +308,7 @@ _king_curl_bytes(c3_c* url_c, c3_w* len_w, c3_y** hun_y, c3_t veb_t) u3K.ssl_curl_f(cul_u); curl_easy_setopt(cul_u, CURLOPT_URL, url_c); - curl_easy_setopt(cul_u, CURLOPT_WRITEFUNCTION, _king_curl_alloc); + curl_easy_setopt(cul_u, CURLOPT_WRITEFUNCTION, king_curl_alloc); curl_easy_setopt(cul_u, CURLOPT_WRITEDATA, (void*)&buf_u); while ( 5 > try_y ) { diff --git a/pkg/vere/pier.c b/pkg/vere/pier.c index f7115a466b..0405d41b22 100644 --- a/pkg/vere/pier.c +++ b/pkg/vere/pier.c @@ -530,21 +530,6 @@ _pier_on_scry_done(void* ptr_v, u3_noun nun) u3z(nun); } -static size_t -_dawn_curl_alloc(void* dat_v, size_t uni_t, size_t mem_t, void* buf_v) -{ - uv_buf_t* buf_u = buf_v; - - size_t siz_t = uni_t * mem_t; - buf_u->base = c3_realloc(buf_u->base, 1 + siz_t + buf_u->len); - - memcpy(buf_u->base + buf_u->len, dat_v, siz_t); - buf_u->len += siz_t; - buf_u->base[buf_u->len] = 0; - - return siz_t; -} - struct HttpRes { c3_i cod_i; c3_c* res_c; @@ -564,7 +549,7 @@ static struct HttpRes http_get(c3_c* url_c) { u3K.ssl_curl_f(curl); curl_easy_setopt(curl, CURLOPT_URL, url_c); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _dawn_curl_alloc); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, king_curl_alloc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&buf_u); curl_easy_setopt(curl, CURLOPT_SERVER_RESPONSE_TIMEOUT, 30); diff --git a/pkg/vere/vere.h b/pkg/vere/vere.h index 42c9024603..7494a636e0 100644 --- a/pkg/vere/vere.h +++ b/pkg/vere/vere.h @@ -1565,6 +1565,11 @@ darwin_register_mach_exception_handler(); #endif + /* king_curl_alloc(): allocate a response buffer for curl + */ + size_t + king_curl_alloc(void* dat_v, size_t uni_t, size_t mem_t, void* buf_v); + /* u3_write_fd(): retry interrupts, continue partial writes, assert errors. */ void From d9c49be98a34b87547924d73e4f9fa2ab1d81f4b Mon Sep 17 00:00:00 2001 From: Santeri Hannula Date: Thu, 13 Jun 2024 15:38:45 +0300 Subject: [PATCH 168/430] pier: type changes and fix double boot galaxy check --- pkg/vere/pier.c | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/pkg/vere/pier.c b/pkg/vere/pier.c index 0405d41b22..9ed13e41d3 100644 --- a/pkg/vere/pier.c +++ b/pkg/vere/pier.c @@ -531,11 +531,11 @@ _pier_on_scry_done(void* ptr_v, u3_noun nun) } struct HttpRes { - c3_i cod_i; + c3_w cod_w; c3_c* res_c; }; -static struct HttpRes http_get(c3_c* url_c) { +static struct HttpRes _http_get(c3_c* url_c) { CURL *curl; CURLcode result; long cod_l; @@ -558,11 +558,9 @@ static struct HttpRes http_get(c3_c* url_c) { if ( CURLE_OK != result ) { u3l_log("failed to fetch %s: %s", url_c, curl_easy_strerror(result)); - /* exit(1); */ } if ( 300 <= cod_l && 404 != cod_l ) { u3l_log("error fetching %s: HTTP %ld", url_c, cod_l); - /* exit(1); */ } curl_easy_cleanup(curl); @@ -571,14 +569,15 @@ static struct HttpRes http_get(c3_c* url_c) { return r; } -static c3_i fetch_ping_ack(c3_c* czar, c3_c* who, c3_i bon) { +static c3_w _fetch_ping_ack(c3_c* czar, c3_c* who, c3_w bon) { c3_c url[256]; sprintf(url, "https://%s.urbit.org/~/boot/%s/%d", czar, who, bon + 1); - struct HttpRes res = http_get(url); - if ( 200 != res.cod_i) { - return -1; + u3l_log("dbug: fetxh: %s", url); + struct HttpRes res = _http_get(url); + if ( 200 != res.cod_w) { + return 0xFFFFFFFF; } - c3_i last_acked = strtoumax(res.res_c, NULL, 10); + c3_w last_acked = strtoumax(res.res_c, NULL, 10); return last_acked; } @@ -601,8 +600,8 @@ _boot_scry_cb(void* vod_p, u3_noun nun) { c3_c* czar_c = u3r_string(czar); c3_c url[256]; sprintf(url, "https://%s.urbit.org/~/boot/%s", czar_c+1, who_c); - struct HttpRes res = http_get(url); - if (res.cod_i == 200) { + struct HttpRes res = _http_get(url); + if (res.cod_w == 200) { u3_noun jammed_atom = u3dc("slav", c3__ud, u3do("crip", u3i_tape(res.res_c))); u3_noun unpacked = u3ke_cue(u3k(jammed_atom)); @@ -611,8 +610,8 @@ _boot_scry_cb(void* vod_p, u3_noun nun) { u3x_trel(unpacked, &czar_glx, &czar_ryf, &czar_lyf); czar_ryf_w = u3r_word(0, czar_ryf); if (czar_ryf_w == ryf_w) { - c3_i last_ack = fetch_ping_ack(czar_c, who_c, bon_w); - if (last_ack == -1) { + c3_w last_ack = _fetch_ping_ack(czar_c, who_c, bon_w); + if (last_ack == 0xFFFFFFFF) { u3l_log("boot: peer state unvailable on czar, cannot protect from double boot"); _pier_work(wok_u); } else if (last_ack == nex_w - 1 || last_ack - 1 == nex_w - 1) { @@ -642,18 +641,18 @@ _boot_scry_cb(void* vod_p, u3_noun nun) { * If peer state exists exit(1) unless ship has breached, * otherwise continue boot. */ - c3_i sponsor = wok_u->pir_u->who_d[0] & ((1 << 8) - 1); + c3_w sponsor = wok_u->pir_u->who_d[0] & ((1 << 8) - 1); u3_noun czar = u3dc("scot", 'p', u3k(sponsor)); c3_c* czar_c = u3r_string(u3k(czar)); c3_c url[256]; sprintf(url, "https://%s.urbit.org/~/sponsor/%s", czar_c+1, who_c); - struct HttpRes res = http_get(url); - if (res.cod_i == 200) { + struct HttpRes res = _http_get(url); + if (res.cod_w == 200) { czar_c = res.res_c; } sprintf(url, "https://%s.urbit.org/~/boot/%s", czar_c+1, who_c); - res = http_get(url); - if (res.cod_i == 200) { + res = _http_get(url); + if (res.cod_w == 200) { // Peer state found under czar u3_noun jammed_atom = u3dc("slav", c3__ud, u3do("crip", u3i_tape(res.res_c))); @@ -776,10 +775,10 @@ _pier_work_init(u3_pier* pir_u) u3_auto_talk(wok_u->car_u); } - c3_i pi_i = wok_u->pir_u->who_d[0]; - c3_i pt_i = wok_u->pir_u->who_d[1]; + c3_d pi_d = wok_u->pir_u->who_d[0]; + c3_d pt_d = wok_u->pir_u->who_d[1]; - if ((pi_i < 256 && pt_i == 0) || c3n == u3_Host.ops_u.net) { + if ((pi_d < 256 && pt_d == 0) || c3n == u3_Host.ops_u.net) { // Skip double boot protection for galaxies and local mode ships // _pier_work(wok_u); From c150827f8877b806c5ba5c8890f67b9ec66d12c5 Mon Sep 17 00:00:00 2001 From: Santeri Hannula Date: Fri, 14 Jun 2024 13:48:39 +0300 Subject: [PATCH 169/430] king: king_curl_bytes to header --- pkg/vere/king.c | 13 +++++++------ pkg/vere/vere.h | 5 +++++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/pkg/vere/king.c b/pkg/vere/king.c index 03095afc83..4e0dd7937f 100644 --- a/pkg/vere/king.c +++ b/pkg/vere/king.c @@ -288,11 +288,11 @@ king_curl_alloc(void* dat_v, size_t uni_t, size_t mem_t, void* buf_v) return siz_t; } -/* _king_curl_bytes(): HTTP GET url_c, produce response body bytes. +/* king_curl_bytes(): HTTP GET url_c, produce response body bytes. ** XX deduplicate with dawn.c */ -static c3_i -_king_curl_bytes(c3_c* url_c, c3_w* len_w, c3_y** hun_y, c3_t veb_t) +c3_i +king_curl_bytes(c3_c* url_c, c3_w* len_w, c3_y** hun_y, c3_t veb_t) { c3_i ret_i = 0; CURL *cul_u; @@ -310,6 +310,7 @@ _king_curl_bytes(c3_c* url_c, c3_w* len_w, c3_y** hun_y, c3_t veb_t) curl_easy_setopt(cul_u, CURLOPT_URL, url_c); curl_easy_setopt(cul_u, CURLOPT_WRITEFUNCTION, king_curl_alloc); curl_easy_setopt(cul_u, CURLOPT_WRITEDATA, (void*)&buf_u); + curl_easy_setopt(cul_u, CURLOPT_SERVER_RESPONSE_TIMEOUT, 30); while ( 5 > try_y ) { sleep(try_y++); @@ -351,7 +352,7 @@ _king_get_atom(c3_c* url_c) c3_y* hun_y; u3_noun pro; - if ( _king_curl_bytes(url_c, &len_w, &hun_y, 1) ) { + if ( king_curl_bytes(url_c, &len_w, &hun_y, 1) ) { u3_king_bail(); exit(1); } @@ -431,7 +432,7 @@ u3_king_next(c3_c* pac_c, c3_c** out_c) // skip printfs on failed requests (/next is usually not present) //REVIEW new retry logic means this case will take longer. make retries optional? // - if ( _king_curl_bytes(url_c, &len_w, &hun_y, 0) ) { + if ( king_curl_bytes(url_c, &len_w, &hun_y, 0) ) { c3_free(url_c); ret_i = asprintf(&url_c, "%s/%s/last", ver_hos_c, pac_c); @@ -440,7 +441,7 @@ u3_king_next(c3_c* pac_c, c3_c** out_c) // enable printfs on failed requests (/last must be present) // XX support channel redirections // - if ( _king_curl_bytes(url_c, &len_w, &hun_y, 1) ) + if ( king_curl_bytes(url_c, &len_w, &hun_y, 1) ) { c3_free(url_c); return -2; diff --git a/pkg/vere/vere.h b/pkg/vere/vere.h index 7494a636e0..00ab6b9737 100644 --- a/pkg/vere/vere.h +++ b/pkg/vere/vere.h @@ -1570,6 +1570,11 @@ size_t king_curl_alloc(void* dat_v, size_t uni_t, size_t mem_t, void* buf_v); + /* king_curl_bytes(): HTTP GET url_c, produce response body bytes. + */ + c3_i + king_curl_bytes(c3_c* url_c, c3_w* len_w, c3_y** hun_y, c3_t veb_t); + /* u3_write_fd(): retry interrupts, continue partial writes, assert errors. */ void From 0e6f2f39f490d9fbb69c864536e3fbd452066238 Mon Sep 17 00:00:00 2001 From: Santeri Hannula Date: Fri, 14 Jun 2024 18:48:03 +0300 Subject: [PATCH 170/430] pier: refactor double boot protection --- pkg/vere/pier.c | 167 ++++++++++++++++++++++++------------------------ 1 file changed, 85 insertions(+), 82 deletions(-) diff --git a/pkg/vere/pier.c b/pkg/vere/pier.c index 9ed13e41d3..b56328676a 100644 --- a/pkg/vere/pier.c +++ b/pkg/vere/pier.c @@ -530,64 +530,87 @@ _pier_on_scry_done(void* ptr_v, u3_noun nun) u3z(nun); } -struct HttpRes { - c3_w cod_w; - c3_c* res_c; -}; +static c3_c* +_resolve_czar(u3_work* wok_u, c3_c* who_c) +{ + u3_noun czar = u3dc("scot", 'p', u3k(wok_u->pir_u->who_d[0] & ((1 << 8) - 1))); + c3_c* czar_c = u3r_string(u3k(czar)); -static struct HttpRes _http_get(c3_c* url_c) { - CURL *curl; - CURLcode result; - long cod_l; + c3_c url[256]; + c3_w len_w; + c3_y* hun_y; - uv_buf_t buf_u = uv_buf_init(c3_malloc(1), 0); + sprintf(url, "https://%s.urbit.org/~/sponsor/%s", czar_c+1, who_c); - if ( !(curl = curl_easy_init()) ) { - u3l_log("failed to initialize libcurl"); - exit(1); + c3_i ret_i = king_curl_bytes(url, &len_w, &hun_y, 1); + if (!ret_i) { + c3_free(czar_c); + czar_c = (c3_c*)hun_y; } - u3K.ssl_curl_f(curl); - curl_easy_setopt(curl, CURLOPT_URL, url_c); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, king_curl_alloc); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&buf_u); - curl_easy_setopt(curl, CURLOPT_SERVER_RESPONSE_TIMEOUT, 30); + return czar_c; +} - result = curl_easy_perform(curl); - curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &cod_l); +static c3_w +_czar_peer_rift(c3_c* czar_c, c3_c* who_c) +{ + c3_w czar_ryf_w = 0xFFFFFFFF; + c3_c url[256]; + c3_w len_w; + c3_y* hun_y; - if ( CURLE_OK != result ) { - u3l_log("failed to fetch %s: %s", url_c, curl_easy_strerror(result)); - } - if ( 300 <= cod_l && 404 != cod_l ) { - u3l_log("error fetching %s: HTTP %ld", url_c, cod_l); - } + sprintf(url, "https://%s.urbit.org/~/boot/%s", czar_c+1, who_c); - curl_easy_cleanup(curl); + c3_i ret_i = king_curl_bytes(url, &len_w, &hun_y, 1); + if (!ret_i) { + u3_noun jamd = + u3dc("slav", c3__ud, u3do("crip", u3i_tape((c3_c*)hun_y))); + u3_noun cued = u3ke_cue(jamd); - struct HttpRes r = { cod_l, buf_u.base }; - return r; + u3_noun czar_glx, czar_ryf, czar_lyf; + u3x_trel(cued, &czar_glx, &czar_ryf, &czar_lyf); + czar_ryf_w = u3r_word(0, czar_ryf); + + u3z(jamd); + u3z(cued); + c3_free(hun_y); + } + + return czar_ryf_w; } -static c3_w _fetch_ping_ack(c3_c* czar, c3_c* who, c3_w bon) { +static c3_w +_czar_last_ack(c3_c* czar_c, + c3_c* who_c, + c3_w bone_w) +{ + c3_w czar_last_ack_w = 0xFFFFFFFF; c3_c url[256]; - sprintf(url, "https://%s.urbit.org/~/boot/%s/%d", czar, who, bon + 1); - u3l_log("dbug: fetxh: %s", url); - struct HttpRes res = _http_get(url); - if ( 200 != res.cod_w) { - return 0xFFFFFFFF; + c3_w len_w; + c3_y* hun_y; + + sprintf(url, "https://%s.urbit.org/~/boot/%s/%d", czar_c+1, who_c, bone_w + 1); + + c3_i ret_i = king_curl_bytes(url, &len_w, &hun_y, 1); + if (!ret_i) { + czar_last_ack_w = strtoumax((c3_c*)hun_y, NULL, 10); } - c3_w last_acked = strtoumax(res.res_c, NULL, 10); - return last_acked; + + c3_free(hun_y); + return czar_last_ack_w; } static void -_boot_scry_cb(void* vod_p, u3_noun nun) { +_boot_scry_cb(void* vod_p, u3_noun nun) +{ u3_work* wok_u = (u3_work*)vod_p; + u3_atom who = u3dc("scot", c3__p, u3i_chubs(2, wok_u->pir_u->who_d)); c3_c* who_c = u3r_string(who); + u3_noun nul, rem, typ, glx, ryf, lyf, bon, nex; c3_w glx_w, ryf_w, bon_w, nex_w; + if (c3y == u3r_cell(nun, &nul, &rem) && c3y == u3r_hext(rem, &typ, &glx, &ryf, &lyf, &bon, &nex)) { /* @@ -596,42 +619,37 @@ _boot_scry_cb(void* vod_p, u3_noun nun) { */ glx_w = u3r_word(0, glx); ryf_w = u3r_word(0, ryf); bon_w = u3r_word(0, bon); nex_w = u3r_word(0, nex); + u3_atom czar = u3dc("scot", c3__p, glx_w); c3_c* czar_c = u3r_string(czar); - c3_c url[256]; - sprintf(url, "https://%s.urbit.org/~/boot/%s", czar_c+1, who_c); - struct HttpRes res = _http_get(url); - if (res.cod_w == 200) { - u3_noun jammed_atom = - u3dc("slav", c3__ud, u3do("crip", u3i_tape(res.res_c))); - u3_noun unpacked = u3ke_cue(u3k(jammed_atom)); - u3_noun czar_glx, czar_ryf, czar_lyf; - c3_w czar_ryf_w; - u3x_trel(unpacked, &czar_glx, &czar_ryf, &czar_lyf); - czar_ryf_w = u3r_word(0, czar_ryf); + c3_w czar_ryf_w = _czar_peer_rift(czar_c, who_c); + + if (czar_ryf_w == 0xFFFFFFFF) { + u3l_log("boot: peer-state unvailable on czar, cannot protect from double boot"); + _pier_work(wok_u); + } else { if (czar_ryf_w == ryf_w) { - c3_w last_ack = _fetch_ping_ack(czar_c, who_c, bon_w); - if (last_ack == 0xFFFFFFFF) { - u3l_log("boot: peer state unvailable on czar, cannot protect from double boot"); + c3_w czar_ack_w = _czar_last_ack(czar_c, who_c, bon_w); + c3_w ack_w = nex_w - 1; + if (czar_ack_w == 0xFFFFFFFF) { + // This codepath should never be hit + u3l_log("boot: message-sink-state unvailable on czar, cannot protect from double boot"); _pier_work(wok_u); - } else if (last_ack == nex_w - 1 || last_ack - 1 == nex_w - 1) { + } else if (czar_ack_w == ack_w || czar_ack_w - 1 == ack_w) { _pier_work(wok_u); } else { u3l_log("boot: failed: czar last ack: %d, ship last ack: %d", - last_ack, nex_w - 1); + czar_ack_w, ack_w); exit(1); } } else { // Trying to boot old ship after breach - u3l_log("boot: failed: rift in czar peer state: %d, current rift: %d", + u3l_log("boot: failed: rift in czar peer-state: %d, current rift: %d", czar_ryf_w, ryf_w); exit(1); } - u3z(jammed_atom); u3z(unpacked); - } else { - u3l_log("boot: %%boot state unvailable on czar, cannot protect from double boot"); - _pier_work(wok_u); } + u3z(czar); c3_free(czar_c); } else if (c3y == u3r_trel(nun, &nul, &typ, &rem) && @@ -641,26 +659,14 @@ _boot_scry_cb(void* vod_p, u3_noun nun) { * If peer state exists exit(1) unless ship has breached, * otherwise continue boot. */ - c3_w sponsor = wok_u->pir_u->who_d[0] & ((1 << 8) - 1); - u3_noun czar = u3dc("scot", 'p', u3k(sponsor)); - c3_c* czar_c = u3r_string(u3k(czar)); - c3_c url[256]; - sprintf(url, "https://%s.urbit.org/~/sponsor/%s", czar_c+1, who_c); - struct HttpRes res = _http_get(url); - if (res.cod_w == 200) { - czar_c = res.res_c; - } - sprintf(url, "https://%s.urbit.org/~/boot/%s", czar_c+1, who_c); - res = _http_get(url); - if (res.cod_w == 200) { + c3_c* czar_c = _resolve_czar(wok_u, who_c); + c3_w czar_ryf_w = _czar_peer_rift(czar_c, who_c); + c3_free(czar_c); + + if (czar_ryf_w == 0xFFFFFFFF) { + _pier_work(wok_u); + } else { // Peer state found under czar - u3_noun jammed_atom = - u3dc("slav", c3__ud, u3do("crip", u3i_tape(res.res_c))); - u3_noun unpacked = u3ke_cue(jammed_atom); - u3_noun czar_glx, czar_ryf, czar_lyf; - c3_w czar_ryf_w; - u3x_trel(unpacked, &czar_glx, &czar_ryf, &czar_lyf); - czar_ryf_w = u3r_word(0, czar_ryf); u3_weak kf_ryf = wok_u->pir_u->ryf; if (kf_ryf == u3_none) { u3l_log("boot: keyfile rift unavailable, cannot protect from double boot"); @@ -669,13 +675,10 @@ _boot_scry_cb(void* vod_p, u3_noun nun) { // Ship has breached, continue boot _pier_work(wok_u); } else { - u3l_log("boot: failed: ship not in sync with czar, double boot"); + u3l_log("boot: failed: rift in czar peer state: %d, keyfile rift: %d", + czar_ryf_w, kf_ryf); exit(1); } - u3z(czar); u3z(jammed_atom); u3z(unpacked); - c3_free(czar_c); - } else { - _pier_work(wok_u); } } else { /* From 88b7a384ea7dbe91d772ec610b96fa907c3f75b6 Mon Sep 17 00:00:00 2001 From: Santeri Hannula Date: Mon, 24 Jun 2024 16:22:01 +0300 Subject: [PATCH 171/430] pier: boot scry return value to unit --- pkg/vere/pier.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/pkg/vere/pier.c b/pkg/vere/pier.c index b56328676a..5868386fa6 100644 --- a/pkg/vere/pier.c +++ b/pkg/vere/pier.c @@ -608,11 +608,11 @@ _boot_scry_cb(void* vod_p, u3_noun nun) u3_atom who = u3dc("scot", c3__p, u3i_chubs(2, wok_u->pir_u->who_d)); c3_c* who_c = u3r_string(who); - u3_noun nul, rem, typ, glx, ryf, lyf, bon, nex; + u3_noun nul, typ, rem, nil, glx, ryf, lyf, bon, nex; c3_w glx_w, ryf_w, bon_w, nex_w; - if (c3y == u3r_cell(nun, &nul, &rem) && - c3y == u3r_hext(rem, &typ, &glx, &ryf, &lyf, &bon, &nex)) { + if (c3y == u3r_trel(nun, &nul, &typ, &rem) && + c3y == u3r_hext(rem, &nil, &glx, &ryf, &lyf, &bon, &nex)) { /* * Boot scry succeeded. Proceed to cross reference networking state against * sponsoring galaxy. @@ -652,8 +652,7 @@ _boot_scry_cb(void* vod_p, u3_noun nun) u3z(czar); c3_free(czar_c); - } else if (c3y == u3r_trel(nun, &nul, &typ, &rem) && - rem == 1634628973 /* %muna */) { + } else if (c3y == u3r_trel(nun, &nul, &typ, &rem) && rem == 0) { /* * Data not available for boot scry. Check against sponsoring galaxy. * If peer state exists exit(1) unless ship has breached, From d8f21d40f87a1b70226633f5346d2fda9626423d Mon Sep 17 00:00:00 2001 From: Santeri Hannula Date: Tue, 25 Jun 2024 16:35:08 +0300 Subject: [PATCH 172/430] king: break king_curl_bytes retry loop on 404 --- pkg/vere/king.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pkg/vere/king.c b/pkg/vere/king.c index 4e0dd7937f..7e87e8b622 100644 --- a/pkg/vere/king.c +++ b/pkg/vere/king.c @@ -329,6 +329,9 @@ king_curl_bytes(c3_c* url_c, c3_w* len_w, c3_y** hun_y, c3_t veb_t) u3l_log("curl: error fetching %s: HTTP %ld", url_c, cod_i); } ret_i = -2; + if ( cod_i == 404 ) { + break; + } } else { *len_w = buf_u.len; From e8e04571d1eb819a99ead314a05e13a55ef74bd6 Mon Sep 17 00:00:00 2001 From: Santeri Hannula Date: Tue, 25 Jun 2024 16:36:19 +0300 Subject: [PATCH 173/430] pier: add packet-pump-state length to %boot scry return value --- pkg/vere/pier.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/pkg/vere/pier.c b/pkg/vere/pier.c index 5868386fa6..cd06543a33 100644 --- a/pkg/vere/pier.c +++ b/pkg/vere/pier.c @@ -608,17 +608,18 @@ _boot_scry_cb(void* vod_p, u3_noun nun) u3_atom who = u3dc("scot", c3__p, u3i_chubs(2, wok_u->pir_u->who_d)); c3_c* who_c = u3r_string(who); - u3_noun nul, typ, rem, nil, glx, ryf, lyf, bon, nex; - c3_w glx_w, ryf_w, bon_w, nex_w; + u3_noun nul, typ, nil, rem, glx, ryf, lyf, bon, nex, liv; + c3_w glx_w, ryf_w, bon_w, nex_w, liv_w; - if (c3y == u3r_trel(nun, &nul, &typ, &rem) && - c3y == u3r_hext(rem, &nil, &glx, &ryf, &lyf, &bon, &nex)) { + if (c3y == u3r_qual(nun, &nul, &typ, &nil, &rem) && + c3y == u3r_hext(rem, &glx, &ryf, &lyf, &bon, &nex, &liv)) { /* * Boot scry succeeded. Proceed to cross reference networking state against * sponsoring galaxy. */ glx_w = u3r_word(0, glx); ryf_w = u3r_word(0, ryf); bon_w = u3r_word(0, bon); nex_w = u3r_word(0, nex); + liv_w = u3r_word(0, liv); u3_atom czar = u3dc("scot", c3__p, glx_w); c3_c* czar_c = u3r_string(czar); @@ -635,7 +636,8 @@ _boot_scry_cb(void* vod_p, u3_noun nun) // This codepath should never be hit u3l_log("boot: message-sink-state unvailable on czar, cannot protect from double boot"); _pier_work(wok_u); - } else if (czar_ack_w == ack_w || czar_ack_w - 1 == ack_w) { + } else if (czar_ack_w == ack_w || + (liv_w > 0 && czar_ack_w - 1 == ack_w)) { _pier_work(wok_u); } else { u3l_log("boot: failed: czar last ack: %d, ship last ack: %d", @@ -652,7 +654,7 @@ _boot_scry_cb(void* vod_p, u3_noun nun) u3z(czar); c3_free(czar_c); - } else if (c3y == u3r_trel(nun, &nul, &typ, &rem) && rem == 0) { + } else if (c3y == u3r_trel(nun, &nul, &typ, &nil) && nil == 0) { /* * Data not available for boot scry. Check against sponsoring galaxy. * If peer state exists exit(1) unless ship has breached, From 62cc27a7869f0a86c3feb3f8ec8d4c11cf557ea3 Mon Sep 17 00:00:00 2001 From: Santeri Hannula Date: Wed, 26 Jun 2024 19:43:34 +0300 Subject: [PATCH 174/430] pier: remove unnecessary variables from _boot_scry_cb --- pkg/vere/pier.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/vere/pier.c b/pkg/vere/pier.c index cd06543a33..41bd36e2ac 100644 --- a/pkg/vere/pier.c +++ b/pkg/vere/pier.c @@ -608,11 +608,11 @@ _boot_scry_cb(void* vod_p, u3_noun nun) u3_atom who = u3dc("scot", c3__p, u3i_chubs(2, wok_u->pir_u->who_d)); c3_c* who_c = u3r_string(who); - u3_noun nul, typ, nil, rem, glx, ryf, lyf, bon, nex, liv; + u3_noun rem, glx, ryf, bon, nex, liv; c3_w glx_w, ryf_w, bon_w, nex_w, liv_w; - if (c3y == u3r_qual(nun, &nul, &typ, &nil, &rem) && - c3y == u3r_hext(rem, &glx, &ryf, &lyf, &bon, &nex, &liv)) { + if (c3y == u3r_qual(nun, 0, 0, 0, &rem) && + c3y == u3r_hext(rem, &glx, &ryf, 0, &bon, &nex, &liv)) { /* * Boot scry succeeded. Proceed to cross reference networking state against * sponsoring galaxy. @@ -654,7 +654,7 @@ _boot_scry_cb(void* vod_p, u3_noun nun) u3z(czar); c3_free(czar_c); - } else if (c3y == u3r_trel(nun, &nul, &typ, &nil) && nil == 0) { + } else if (c3y == u3r_trel(nun, 0, 0, &rem) && rem == 0) { /* * Data not available for boot scry. Check against sponsoring galaxy. * If peer state exists exit(1) unless ship has breached, From c642d9552bec589792778d10c5a506552a1a5bc4 Mon Sep 17 00:00:00 2001 From: Santeri Hannula Date: Thu, 27 Jun 2024 16:56:15 +0300 Subject: [PATCH 175/430] pier: change number of live packets to next ack in %boot scry --- pkg/vere/pier.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pkg/vere/pier.c b/pkg/vere/pier.c index 41bd36e2ac..580c64fb66 100644 --- a/pkg/vere/pier.c +++ b/pkg/vere/pier.c @@ -608,18 +608,18 @@ _boot_scry_cb(void* vod_p, u3_noun nun) u3_atom who = u3dc("scot", c3__p, u3i_chubs(2, wok_u->pir_u->who_d)); c3_c* who_c = u3r_string(who); - u3_noun rem, glx, ryf, bon, nex, liv; - c3_w glx_w, ryf_w, bon_w, nex_w, liv_w; + u3_noun rem, glx, ryf, bon, cur, nex; + c3_w glx_w, ryf_w, bon_w, cur_w, nex_w; if (c3y == u3r_qual(nun, 0, 0, 0, &rem) && - c3y == u3r_hext(rem, &glx, &ryf, 0, &bon, &nex, &liv)) { + c3y == u3r_hext(rem, &glx, &ryf, 0, &bon, &cur, &nex)) { /* * Boot scry succeeded. Proceed to cross reference networking state against * sponsoring galaxy. */ glx_w = u3r_word(0, glx); ryf_w = u3r_word(0, ryf); - bon_w = u3r_word(0, bon); nex_w = u3r_word(0, nex); - liv_w = u3r_word(0, liv); + bon_w = u3r_word(0, bon); cur_w = u3r_word(0, cur); + nex_w = u3r_word(0, nex); u3_atom czar = u3dc("scot", c3__p, glx_w); c3_c* czar_c = u3r_string(czar); @@ -631,13 +631,13 @@ _boot_scry_cb(void* vod_p, u3_noun nun) } else { if (czar_ryf_w == ryf_w) { c3_w czar_ack_w = _czar_last_ack(czar_c, who_c, bon_w); - c3_w ack_w = nex_w - 1; + c3_w ack_w = cur_w - 1; if (czar_ack_w == 0xFFFFFFFF) { // This codepath should never be hit u3l_log("boot: message-sink-state unvailable on czar, cannot protect from double boot"); _pier_work(wok_u); } else if (czar_ack_w == ack_w || - (liv_w > 0 && czar_ack_w - 1 == ack_w)) { + (nex_w > cur_w && czar_ack_w - 1 == ack_w)) { _pier_work(wok_u); } else { u3l_log("boot: failed: czar last ack: %d, ship last ack: %d", From 827bcac6d4c85558f82375b6ee73286b7a7c703f Mon Sep 17 00:00:00 2001 From: Santeri Hannula Date: Thu, 27 Jun 2024 17:06:35 +0300 Subject: [PATCH 176/430] pier: change boot response type from @ud to @uw --- pkg/vere/pier.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/vere/pier.c b/pkg/vere/pier.c index 580c64fb66..8bd882ec02 100644 --- a/pkg/vere/pier.c +++ b/pkg/vere/pier.c @@ -563,8 +563,7 @@ _czar_peer_rift(c3_c* czar_c, c3_c* who_c) c3_i ret_i = king_curl_bytes(url, &len_w, &hun_y, 1); if (!ret_i) { - u3_noun jamd = - u3dc("slav", c3__ud, u3do("crip", u3i_tape((c3_c*)hun_y))); + u3_noun jamd = u3dc("slav", c3__uw, u3do("crip", u3i_tape((c3_c*)hun_y))); u3_noun cued = u3ke_cue(jamd); u3_noun czar_glx, czar_ryf, czar_lyf; From e7129222f06a981dce1e87818309fc8fb897f113 Mon Sep 17 00:00:00 2001 From: Santeri Hannula Date: Thu, 27 Jun 2024 17:15:55 +0300 Subject: [PATCH 177/430] pier: cleanup _czar_peer_rift --- pkg/vere/pier.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/vere/pier.c b/pkg/vere/pier.c index 8bd882ec02..a0611b770d 100644 --- a/pkg/vere/pier.c +++ b/pkg/vere/pier.c @@ -563,8 +563,8 @@ _czar_peer_rift(c3_c* czar_c, c3_c* who_c) c3_i ret_i = king_curl_bytes(url, &len_w, &hun_y, 1); if (!ret_i) { - u3_noun jamd = u3dc("slav", c3__uw, u3do("crip", u3i_tape((c3_c*)hun_y))); - u3_noun cued = u3ke_cue(jamd); + u3_noun jamd = u3dc("slav", c3__uw, u3i_bytes(len_w, hun_y)); + u3_noun cued = u3qe_cue(jamd); u3_noun czar_glx, czar_ryf, czar_lyf; u3x_trel(cued, &czar_glx, &czar_ryf, &czar_lyf); From fa97c3a754e17a63e8c24f1e605bb6969680504f Mon Sep 17 00:00:00 2001 From: midden-fabler Date: Fri, 28 Jun 2024 19:47:22 -0400 Subject: [PATCH 178/430] http: cache, range WIP --- pkg/vere/io/http.c | 99 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 83 insertions(+), 16 deletions(-) diff --git a/pkg/vere/io/http.c b/pkg/vere/io/http.c index 69fd954328..dc3dbdc870 100644 --- a/pkg/vere/io/http.c +++ b/pkg/vere/io/http.c @@ -655,6 +655,7 @@ _http_foo_cb(void* vod_p, u3_noun nun) u3_httd* htd_u = peq_u->htd_u; u3_hreq* req_u = peq_u->req_u; + // TODO range if ( req_u ) { u3_assert(u3_rsat_peek == req_u->sat_e); req_u->peq_u = 0; @@ -669,7 +670,7 @@ _http_foo_cb(void* vod_p, u3_noun nun) } static c3_c* -_find_tis_fas(void* txt, size_t len) +_find_tis_fas(void* txt, c3_w len) { c3_c* tis = memchr(txt, '=', len); c3_c* fas = memchr(txt, '/', len); @@ -684,15 +685,47 @@ _find_tis_fas(void* txt, size_t len) return fas; } } -/* _http_req_dispatch(): dispatch http request to %eyre -*/ + // TODO // [x] don't blow up on bad paths // [x] authentication // [x] caching -// [ ] insert mime in path -// [ ] range header +// [x] insert mime in path +// [x] range header +// [x] u3qc_cut +// [ ] _http_range_respond? +// [ ] 216 +// [ ] better range header parsing +// [ ] better slicing +// [ ] multipart ranges // +typedef struct _range_header { + c3_o ok; + c3_w begin; + c3_w end; +} range_header; + +static range_header +_get_range(void* txt, c3_w len) +{ + c3_c* hep = memchr(txt, '-', len); + range_header slice; + + if ( hep ) { + // XX sscanf safety - u3i_bytes first? + sscanf(txt, "%" SCNu32, &slice.begin); + sscanf(hep+1, "%" SCNu32, &slice.end); + slice.ok = c3y; + return slice; + } + else { + slice.ok = c3n; + return slice; + } +} + +/* _http_req_dispatch(): dispatch http request to %eyre +*/ static void _http_req_dispatch(u3_hreq* req_u, u3_noun req) { @@ -748,7 +781,7 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) u3_noun cas; c3_o last = c3n; - size_t i; + c3_w i; // get beak from path // @@ -808,23 +841,23 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) u3_noun spur = u3dc("rush", u3i_bytes(len, (const c3_y*)base), u3v_wish("stap")); - if ( spur == u3_nul ) { - h2o_send_error_generic(req_u->rec_u, 400, "bad spur", "bad spur", 0); + if ( (who != our) || (spur == u3_nul) ) { + c3_c* msg_c = "bad scry path"; + h2o_send_error_generic(req_u->rec_u, 400, msg_c, msg_c, 0); + return; } + else { - if ( who != our ) { - c3_c* msg_c = "who != our"; - h2o_send_error_generic(req_u->rec_u, 400, msg_c, msg_c, 0); - return; - } + spur = u3nc(u3i_string("mime"), u3t(spur)); if ( c3y == last ) { // DON'T CACHE + req_u->peq_u->las_o = c3y; u3_pier_peek_last(htd_u->car_u.pir_u, gang, c3__ex, - des, u3t(spur), req_u->peq_u, _http_foo_cb); + des, spur, req_u->peq_u, _http_foo_cb); } else { - u3_noun bem = u3nq(our, des, cas, u3t(spur)); + u3_noun bem = u3nq(our, des, cas, spur); u3_weak nac = u3h_get(htd_u->nax_p, bem); if ( u3_none == nac ) { @@ -834,7 +867,41 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) req_u->peq_u, _http_foo_cb); } else { - _http_cache_respond(req_u, nac); + // TODO range + h2o_headers_t req_headers = req_u->rec_u->headers; + c3_w idx = h2o_find_header(&req_headers, H2O_TOKEN_RANGE, -1); + + if (idx != UINT32_MAX) { + if ( (req_headers.entries[idx].value.len > 6) && + (memcmp("bytes=", req_headers.entries[idx].value.base, 6) == 0 )) { + c3_w rest_len = req_headers.entries[idx].value.len - 6; + range_header res = _get_range(req_headers.entries[idx].value.base + 6, rest_len); + u3l_log("struct begin %u, end %u", res.begin, res.end); + if ( res.ok == c3n ) { + c3_c* msg_c = "Requested Range Not Satisfiable"; + h2o_send_error_generic(req_u->rec_u, 416, msg_c, msg_c, 0); + } + else { + u3_noun octs = u3r_at(127, nac); + + if ( octs == u3_none) { + c3_c* msg_c = "Requested Range Not Satisfiable"; + h2o_send_error_generic(req_u->rec_u, 416, msg_c, msg_c, 0); + return; + } + + // TODO check range vs size + u3_noun piece = u3qc_cut(3, res.begin, res.end + 1, u3t(octs)); + u3_noun result = u3nc(res.end + 1 - res.begin, piece); + u3m_p("result", result); + u3_noun res = u3i_edit(nac, 127, result); + _http_cache_respond(req_u, res); + } + } + } + else { + _http_cache_respond(req_u, nac); + } } } } From 466c7699d2bdc239b7a3de58b60685b83fa1d8bf Mon Sep 17 00:00:00 2001 From: midden-fabler Date: Mon, 1 Jul 2024 12:46:22 -0400 Subject: [PATCH 179/430] http: better header parsing --- pkg/vere/io/http.c | 77 +++++++++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 38 deletions(-) diff --git a/pkg/vere/io/http.c b/pkg/vere/io/http.c index dc3dbdc870..c1008048a9 100644 --- a/pkg/vere/io/http.c +++ b/pkg/vere/io/http.c @@ -7,7 +7,9 @@ #include "openssl/err.h" #include "openssl/ssl.h" #include "version.h" +#include #include +#include typedef struct _u3_h2o_serv { h2o_globalconf_t fig_u; // h2o global config @@ -693,35 +695,42 @@ _find_tis_fas(void* txt, c3_w len) // [x] insert mime in path // [x] range header // [x] u3qc_cut -// [ ] _http_range_respond? -// [ ] 216 -// [ ] better range header parsing +// [x] better range header parsing // [ ] better slicing +// [ ] 216 +// [ ] _http_range_respond? // [ ] multipart ranges // typedef struct _range_header { - c3_o ok; - c3_w begin; - c3_w end; + c3_z start_z; + c3_z end_z; } range_header; static range_header -_get_range(void* txt, c3_w len) +_get_range(c3_c* txt_c, c3_w len_w) { - c3_c* hep = memchr(txt, '-', len); + c3_c* hep_c = memchr(txt_c, '-', len_w); + c3_c* txt_two_c = txt_c; range_header slice; - - if ( hep ) { - // XX sscanf safety - u3i_bytes first? - sscanf(txt, "%" SCNu32, &slice.begin); - sscanf(hep+1, "%" SCNu32, &slice.end); - slice.ok = c3y; - return slice; - } - else { - slice.ok = c3n; - return slice; + slice.start_z = SIZE_MAX; + slice.end_z = SIZE_MAX; + + // - + // - + // -, - + // -, -, - + // - + + if ( hep_c ) { + slice.start_z = h2o_strtosizefwd(&txt_two_c, hep_c - txt_c); + if ( slice.start_z != SIZE_MAX ) { + u3_assert(txt_two_c == hep_c); + } + txt_two_c = hep_c + 1; + slice.end_z = h2o_strtosizefwd(&txt_two_c, len_w - (txt_two_c - txt_c)); } + + return slice; } /* _http_req_dispatch(): dispatch http request to %eyre @@ -875,28 +884,20 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) if ( (req_headers.entries[idx].value.len > 6) && (memcmp("bytes=", req_headers.entries[idx].value.base, 6) == 0 )) { c3_w rest_len = req_headers.entries[idx].value.len - 6; - range_header res = _get_range(req_headers.entries[idx].value.base + 6, rest_len); - u3l_log("struct begin %u, end %u", res.begin, res.end); - if ( res.ok == c3n ) { + range_header rng_hed = _get_range(req_headers.entries[idx].value.base + 6, rest_len); + u3l_log("struct start %lu, end %lu", rng_hed.start_z, rng_hed.end_z); + u3_noun octs = u3r_at(127, nac); + if ( octs == u3_none) { c3_c* msg_c = "Requested Range Not Satisfiable"; h2o_send_error_generic(req_u->rec_u, 416, msg_c, msg_c, 0); + return; } - else { - u3_noun octs = u3r_at(127, nac); - - if ( octs == u3_none) { - c3_c* msg_c = "Requested Range Not Satisfiable"; - h2o_send_error_generic(req_u->rec_u, 416, msg_c, msg_c, 0); - return; - } - - // TODO check range vs size - u3_noun piece = u3qc_cut(3, res.begin, res.end + 1, u3t(octs)); - u3_noun result = u3nc(res.end + 1 - res.begin, piece); - u3m_p("result", result); - u3_noun res = u3i_edit(nac, 127, result); - _http_cache_respond(req_u, res); - } + // TODO check range vs size + u3_noun piece = u3qc_cut(3, rng_hed.start_z, rng_hed.end_z + 1, u3t(octs)); + u3_noun result = u3nc(rng_hed.end_z + 1 - rng_hed.start_z, piece); + u3m_p("result", result); + u3_noun res = u3i_edit(nac, 127, result); + _http_cache_respond(req_u, res); } } else { From 08c3992e4b553ee6cb7e5391055289978dd1e794 Mon Sep 17 00:00:00 2001 From: lukechampine Date: Mon, 1 Jul 2024 13:49:07 -0400 Subject: [PATCH 180/430] lss: add lss_builder --- pkg/vere/io/lss.c | 182 +++++++++++++++++++++++++++++++++++---------- pkg/vere/io/lss.h | 21 +++++- pkg/vere/io/mesa.c | 22 ++++-- 3 files changed, 178 insertions(+), 47 deletions(-) diff --git a/pkg/vere/io/lss.c b/pkg/vere/io/lss.c index d6b845025c..be95a767af 100644 --- a/pkg/vere/io/lss.c +++ b/pkg/vere/io/lss.c @@ -33,7 +33,7 @@ static void _parent_hash(lss_hash out, lss_hash left, lss_hash right) memcpy(out, block, 32); } -static c3_y bits_ctz64(c3_d val_d) +static c3_y _bits_ctz64(c3_d val_d) { if (val_d == 0 ) { return 64; @@ -48,7 +48,7 @@ static c3_y bits_ctz64(c3_d val_d) return ret_y; } -static c3_y bits_len64(c3_d val_d) +static c3_y _bits_len64(c3_d val_d) { if (val_d == 0) { return 0; @@ -63,13 +63,87 @@ static c3_y bits_len64(c3_d val_d) return ret_y; } -static c3_o lss_find_pair(lss_verifier* los_u, c3_w height, c3_b sel, lss_hash h) +c3_w lss_proof_size(c3_w leaves) { + return 1 + _bits_len64(leaves-1); +} + +c3_o _lss_expect_pair(c3_w leaves, c3_w i) { + if ( ((i != 0) && (i + (1 << (1+_bits_ctz64(i))) < leaves)) ) { + return c3y; + } + return c3n; +} + +static void _lss_builder_merge(lss_builder* bil_u, c3_w height, lss_hash l, lss_hash r) { + // whenever two subtrees are merged, insert them into the pairs array; + // if the merged tree is part of the left-side "spine" of the tree, + // instead add the right subtree to the initial proof + if (height+1 < _bits_len64(bil_u->counter)) { + c3_w c = (bil_u->counter&~((1<<(height+1))-1)) - (1<pairs[c][0], l, sizeof(lss_hash)); + memcpy(bil_u->pairs[c][1], r, sizeof(lss_hash)); + } else { + if (height == 0) { + memcpy(bil_u->proof[0], l, sizeof(lss_hash)); // proof always begins with [0, 1) + } + memcpy(bil_u->proof[height+1], r, sizeof(lss_hash)); + } +} + +void lss_builder_ingest(lss_builder* bil_u, c3_y* leaf_y, c3_w leaf_w) { + lss_hash h; + _leaf_hash(h, leaf_y, leaf_w, bil_u->counter); + c3_w height = 0; + while ( bil_u->counter&(1<trees[height], h); + _parent_hash(h, bil_u->trees[height], h); + height++; + } + memcpy(bil_u->trees[height], h, sizeof(lss_hash)); + bil_u->counter++; +} + +lss_hash* lss_builder_finalize(lss_builder* bil_u) { + if ( bil_u->counter != 0 ) { + c3_w height = _bits_ctz64(bil_u->counter); + lss_hash h; + memcpy(h, bil_u->trees[height], sizeof(lss_hash)); + for (height++; height < 32; height++) { + if ( bil_u->counter&(1<trees[height], h); + _parent_hash(h, bil_u->trees[height], h); + } + } + } + return bil_u->proof; +} + +lss_pair* lss_builder_pair(lss_builder* bil_u, c3_w i) { + if ( c3y == _lss_expect_pair(bil_u->counter, i) ) { + return &bil_u->pairs[i]; + } + return NULL; +} + +void lss_builder_init(lss_builder* bil_u, c3_w leaves) { + bil_u->counter = 0; + bil_u->proof = c3_calloc(lss_proof_size(leaves) * sizeof(lss_hash)); + bil_u->pairs = c3_calloc(leaves * sizeof(lss_pair)); +} + +void lss_builder_free(lss_builder* bil_u) { + c3_free(bil_u->proof); + c3_free(bil_u->pairs); + c3_free(bil_u); +} + +static c3_o _lss_verifier_find_pair(lss_verifier* los_u, c3_w height, c3_b sel, lss_hash h) { if (memcmp(los_u->pairs[height][sel], h, 32) == 0) { return c3y; } // look for "irregular" pairs, which will always be on the right - c3_w max_height = bits_len64(los_u->leaves); + c3_w max_height = _bits_len64(los_u->leaves); for (height++; height < max_height; height++) { if (memcmp(los_u->pairs[height][1], h, 32) == 0) { return c3y; @@ -80,14 +154,14 @@ static c3_o lss_find_pair(lss_verifier* los_u, c3_w height, c3_b sel, lss_hash h c3_o lss_verifier_ingest(lss_verifier* los_u, c3_y* leaf_y, c3_w leaf_w, lss_pair* pair) { // verify leaf - lss_hash leaf_hash; - _leaf_hash(leaf_hash, leaf_y, leaf_w, los_u->counter); - if ( c3n == lss_find_pair(los_u, 0, los_u->counter&1, leaf_hash) ) { + lss_hash h; + _leaf_hash(h, leaf_y, leaf_w, los_u->counter); + if ( c3n == _lss_verifier_find_pair(los_u, 0, los_u->counter&1, h) ) { return c3n; } // check whether pair is expected - c3_w height = bits_ctz64(los_u->counter) + 1; - if ((pair != NULL) != ((los_u->counter != 0) && (los_u->counter + (1 << height) < los_u->leaves))) { + c3_w height = _bits_ctz64(los_u->counter) + 1; + if ( (pair != NULL) != (c3y == _lss_expect_pair(los_u->leaves, los_u->counter)) ) { return c3n; } else if (pair == NULL) { los_u->counter++; @@ -97,7 +171,7 @@ c3_o lss_verifier_ingest(lss_verifier* los_u, c3_y* leaf_y, c3_w leaf_w, lss_pai c3_b sel = ~(los_u->counter >> height) & 1; lss_hash parent_hash; _parent_hash(parent_hash, (*pair)[0], (*pair)[1]); - if ( c3n == lss_find_pair(los_u, height, sel, parent_hash) ) { + if ( c3n == _lss_verifier_find_pair(los_u, height, sel, parent_hash) ) { return c3n; } memcpy(los_u->pairs[height-1][0], (*pair)[0], sizeof(lss_hash)); @@ -106,10 +180,11 @@ c3_o lss_verifier_ingest(lss_verifier* los_u, c3_y* leaf_y, c3_w leaf_w, lss_pai return c3y; } -c3_o lss_verifier_init(lss_verifier* los_u, c3_w leaves, lss_hash* proof, c3_w proof_w) { +c3_o lss_verifier_init(lss_verifier* los_u, c3_w leaves, lss_hash* proof) { + c3_w proof_w = lss_proof_size(leaves); + c3_w pairs_w = _bits_len64(leaves); los_u->leaves = leaves; los_u->counter = 0; - c3_w pairs_w = bits_len64(leaves); los_u->pairs = c3_calloc(pairs_w * sizeof(lss_pair)); memcpy(los_u->pairs[0][0], proof[0], sizeof(lss_hash)); for (c3_w i = 1; i < proof_w; i++) { @@ -136,17 +211,17 @@ void lss_root(lss_hash root, lss_hash* proof, c3_w proof_w) { #ifdef LSS_TEST -static void _test_bits_ctz64() +static void _test__bits_ctz64() { - #define asrt(x,y) if ( x != y ) { fprintf(stderr, "failed (line: %u) got %u expected %u", __LINE__, x,y); exit(1); } - asrt(bits_ctz64(0ULL), 64); - asrt(bits_ctz64(0x0000000000000001ULL), 0); - asrt(bits_ctz64(0x0000000000000002ULL), 1); - asrt(bits_ctz64(0x0000000000000100ULL), 8); - asrt(bits_ctz64(0x0000000080000000ULL), 31); - asrt(bits_ctz64(0x0000000100000000ULL), 32); - asrt(bits_ctz64(0x4000000000000000ULL), 62); - asrt(bits_ctz64(0x8000000000000000ULL), 63); + #define asrt(x,y) if ( x != y ) { fprintf(stderr, "failed at %s:%u: got %u, expected %u", __FILE__, __LINE__, x,y); exit(1); } + asrt(_bits_ctz64(0ULL), 64); + asrt(_bits_ctz64(0x0000000000000001ULL), 0); + asrt(_bits_ctz64(0x0000000000000002ULL), 1); + asrt(_bits_ctz64(0x0000000000000100ULL), 8); + asrt(_bits_ctz64(0x0000000080000000ULL), 31); + asrt(_bits_ctz64(0x0000000100000000ULL), 32); + asrt(_bits_ctz64(0x4000000000000000ULL), 62); + asrt(_bits_ctz64(0x8000000000000000ULL), 63); #undef asrt } @@ -158,9 +233,9 @@ static lss_pair* _make_pair(lss_hash left, lss_hash right) return pair; } -static void _test_lss_8() +static void _test_lss_manual_verify_8() { - #define asrt_ok(y) if ( c3y != y ) { fprintf(stderr, "failed (line: %u)", __LINE__); exit(1); } + #define asrt_ok(y) if ( c3y != y ) { fprintf(stderr, "failed at %s:%u\n", __FILE__, __LINE__); exit(1); } c3_w dat_w = 1024 * 7 + 1; c3_y* dat_y = c3_calloc(dat_w); @@ -197,25 +272,56 @@ static void _test_lss_8() // verify lss_verifier lss_u; memset(&lss_u, 0, sizeof(lss_verifier)); - asrt_ok(lss_verifier_init(&lss_u, 8, proof, 4)) - // for ( c3_w i = 0; i < 8; i++ ) { - // asrt_ok(lss_verifier_ingest(&lss_u, leaves_y[i], leaves_w[i], pairs[i])) - // } - asrt_ok(lss_verifier_ingest(&lss_u, leaves_y[0], leaves_w[0], pairs[0])) - asrt_ok(lss_verifier_ingest(&lss_u, leaves_y[1], leaves_w[1], pairs[1])) - asrt_ok(lss_verifier_ingest(&lss_u, leaves_y[2], leaves_w[2], pairs[2])) - asrt_ok(lss_verifier_ingest(&lss_u, leaves_y[3], leaves_w[3], pairs[3])) - asrt_ok(lss_verifier_ingest(&lss_u, leaves_y[4], leaves_w[4], pairs[4])) - asrt_ok(lss_verifier_ingest(&lss_u, leaves_y[5], leaves_w[5], pairs[5])) - asrt_ok(lss_verifier_ingest(&lss_u, leaves_y[6], leaves_w[6], pairs[6])) - asrt_ok(lss_verifier_ingest(&lss_u, leaves_y[7], leaves_w[7], pairs[7])) + asrt_ok(lss_verifier_init(&lss_u, 8, proof)) + for ( c3_w i = 0; i < 8; i++ ) { + asrt_ok(lss_verifier_ingest(&lss_u, leaves_y[i], leaves_w[i], pairs[i])) + } + + #undef asrt_ok +} + +static void _test_lss_build_verify(c3_w dat_w) +{ + #define asrt_ok(y) if ( c3y != y ) { fprintf(stderr, "failed at %s:%u\n", __FILE__, __LINE__); exit(1); } + + c3_y* dat_y = c3_calloc(dat_w); + for ( c3_w i = 0; i < dat_w; i++ ) { + dat_y[i] = i; + } + c3_w leaves_w = (dat_w + 1023) / 1024; + + // build + lss_builder bil_u; + lss_builder_init(&bil_u, leaves_w); + for ( c3_w i = 0; i < leaves_w; i++ ) { + c3_y* leaf_y = dat_y + (i*1024); + c3_w leaf_w = (i < leaves_w - 1) ? 1024 : dat_w % 1024; + lss_builder_ingest(&bil_u, leaf_y, leaf_w); + } + lss_hash* proof = lss_builder_finalize(&bil_u); + + // verify + lss_verifier lss_u; + asrt_ok(lss_verifier_init(&lss_u, leaves_w, proof)) + for ( c3_w i = 0; i < leaves_w; i++ ) { + c3_y* leaf_y = dat_y + (i*1024); + c3_w leaf_w = (i < leaves_w - 1) ? 1024 : dat_w % 1024; + lss_pair* pair = lss_builder_pair(&bil_u, i); + asrt_ok(lss_verifier_ingest(&lss_u, leaf_y, leaf_w, pair)); + } #undef asrt_ok } int main() { - _test_bits_ctz64(); - _test_lss_8(); + _test__bits_ctz64(); + _test_lss_manual_verify_8(); + _test_lss_build_verify(2 * 1024); + _test_lss_build_verify(2 * 1024 + 2); + _test_lss_build_verify(3 * 1024); + _test_lss_build_verify(5 * 1024 + 5); + _test_lss_build_verify(17 * 1024 + 512); + _test_lss_build_verify(128 * 1024); return 0; } diff --git a/pkg/vere/io/lss.h b/pkg/vere/io/lss.h index e7e7f59e38..bafffd3944 100644 --- a/pkg/vere/io/lss.h +++ b/pkg/vere/io/lss.h @@ -7,10 +7,29 @@ #include "noun.h" #include "ur.h" +c3_w lss_proof_size(c3_w leaves); + typedef c3_y lss_hash[32]; typedef lss_hash lss_pair[2]; +typedef struct _lss_builder { + lss_hash trees[32]; + c3_w counter; + lss_hash *proof; + lss_pair *pairs; +} lss_builder; + +void lss_builder_ingest(lss_builder* bil_u, c3_y* leaf_y, c3_w leaf_w); + +lss_hash* lss_builder_finalize(lss_builder* bil_u); + +lss_pair* lss_builder_pair(lss_builder* bil_u, c3_w i); + +void lss_builder_init(lss_builder* bil_u, c3_w leaves); + +void lss_builder_free(lss_builder* bil_u); + typedef struct _lss_verifier { c3_w leaves; c3_w counter; @@ -19,7 +38,7 @@ typedef struct _lss_verifier { c3_o lss_verifier_ingest(lss_verifier* ver_u, c3_y* leaf_y, c3_w leaf_w, lss_pair* pair); -c3_o lss_verifier_init(lss_verifier* ver_u, c3_w leaves, lss_hash* proof, c3_w proof_w); +c3_o lss_verifier_init(lss_verifier* ver_u, c3_w leaves, lss_hash* proof); void lss_verifier_free(lss_verifier* ver_u); diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 8e271eb08e..dd44d9df20 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1932,18 +1932,21 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) if ( c3y == lin_o ) { // complete the proof by computing the first leaf hash - c3_w len_w = pac_u->pag_u.dat_u.aup_u.len_y + 1; - lss_hash* pof_u = c3_calloc(len_w * sizeof(lss_hash)); - for ( int i = 1; i < len_w; i++ ) { + c3_w pof_w = pac_u->pag_u.dat_u.aup_u.len_y + 1; + if ( pof_w != lss_proof_size(req_u->tot_w) ) { + return NULL; // XX ??? + } + lss_hash* pof_u = c3_calloc(pof_w * sizeof(lss_hash)); + for ( int i = 1; i < pof_w; i++ ) { memcpy(pof_u[i], pac_u->pag_u.dat_u.aup_u.has_y[i-1], sizeof(lss_hash)); } lss_complete_inline_proof(pof_u, dat_u->fra_y, dat_u->len_w); // TODO: authenticate root - // lss_root root; - // lss_root(root, pof_u, len_w); + // lss_hash root; + // lss_root(root, pof_u, pof_w); req_u->los_u = c3_calloc(sizeof(lss_verifier)); - if ( c3y != lss_verifier_init(req_u->los_u, req_u->tot_w, pof_u, len_w) ) { + if ( c3y != lss_verifier_init(req_u->los_u, req_u->tot_w, pof_u) ) { return NULL; // XX ??? } c3_free(pof_u); @@ -1952,17 +1955,20 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) } memcpy(req_u->dat_y, dat_u->fra_y, dat_u->len_w); } else { + if ( dat_u->len_w != lss_proof_size(req_u->tot_w) ) { + return NULL; // XX ??? + } // TODO: cast directly instead of copying? lss_hash* pof_u = c3_calloc(dat_u->len_w * sizeof(lss_hash)); for ( int i = 0; i < dat_u->len_w; i++ ) { memcpy(pof_u[i], dat_u->fra_y + (i * sizeof(lss_hash)), sizeof(lss_hash)); } // TODO: authenticate root - // lss_root root; + // lss_hash root; // lss_root(root, pof_u, dat_u->len_w/32); req_u->los_u = c3_calloc(sizeof(lss_verifier)); - lss_verifier_init(req_u->los_u, req_u->tot_w, pof_u, dat_u->len_w/32); + lss_verifier_init(req_u->los_u, req_u->tot_w, pof_u); c3_free(pof_u); } vec_init(&req_u->mis_u, 8); From 3e35656f3b2e916875118ea500343bf42aa0a4d9 Mon Sep 17 00:00:00 2001 From: lukechampine Date: Mon, 1 Jul 2024 21:35:30 -0400 Subject: [PATCH 181/430] lss: eliminate loop in _lss_verifier_find_pair --- pkg/vere/io/lss.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/pkg/vere/io/lss.c b/pkg/vere/io/lss.c index be95a767af..13b6efe623 100644 --- a/pkg/vere/io/lss.c +++ b/pkg/vere/io/lss.c @@ -142,14 +142,16 @@ static c3_o _lss_verifier_find_pair(lss_verifier* los_u, c3_w height, c3_b sel, if (memcmp(los_u->pairs[height][sel], h, 32) == 0) { return c3y; } - // look for "irregular" pairs, which will always be on the right - c3_w max_height = _bits_len64(los_u->leaves); - for (height++; height < max_height; height++) { - if (memcmp(los_u->pairs[height][1], h, 32) == 0) { - return c3y; - } - } - return c3n; + // "Unbalanced" pairs have two children on the left and one on the right. This + // results in the right child being emplaced at a greater height than it + // "should" be, so the standard check (above) misses it. + // + // The locations of these single children are fully determined by the size of + // the tree. To find the adjusted height, we simply "climb up" however many + // unbalanced pairs are directly above us. + c3_w unbalanced = (1<<_bits_len64(los_u->leaves-1)) - los_u->leaves; + height += _bits_ctz64(~(mask >> height)); + return _(memcmp(los_u->pairs[height][1], h, 32) == 0); } c3_o lss_verifier_ingest(lss_verifier* los_u, c3_y* leaf_y, c3_w leaf_w, lss_pair* pair) { @@ -160,7 +162,6 @@ c3_o lss_verifier_ingest(lss_verifier* los_u, c3_y* leaf_y, c3_w leaf_w, lss_pai return c3n; } // check whether pair is expected - c3_w height = _bits_ctz64(los_u->counter) + 1; if ( (pair != NULL) != (c3y == _lss_expect_pair(los_u->leaves, los_u->counter)) ) { return c3n; } else if (pair == NULL) { @@ -168,6 +169,7 @@ c3_o lss_verifier_ingest(lss_verifier* los_u, c3_y* leaf_y, c3_w leaf_w, lss_pai return c3y; } // verify and insert pair + c3_w height = _bits_ctz64(los_u->counter) + 1; c3_b sel = ~(los_u->counter >> height) & 1; lss_hash parent_hash; _parent_hash(parent_hash, (*pair)[0], (*pair)[1]); From 260f1af6bd1a1775ab66cb2292f634956744c87a Mon Sep 17 00:00:00 2001 From: lukechampine Date: Mon, 1 Jul 2024 23:14:07 -0400 Subject: [PATCH 182/430] lss: fix typo --- pkg/vere/io/lss.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/vere/io/lss.c b/pkg/vere/io/lss.c index 13b6efe623..d617a5b735 100644 --- a/pkg/vere/io/lss.c +++ b/pkg/vere/io/lss.c @@ -150,7 +150,7 @@ static c3_o _lss_verifier_find_pair(lss_verifier* los_u, c3_w height, c3_b sel, // the tree. To find the adjusted height, we simply "climb up" however many // unbalanced pairs are directly above us. c3_w unbalanced = (1<<_bits_len64(los_u->leaves-1)) - los_u->leaves; - height += _bits_ctz64(~(mask >> height)); + height += _bits_ctz64(~(unbalanced >> height)); return _(memcmp(los_u->pairs[height][1], h, 32) == 0); } From ab002786e807483bfc682a804be75c2348c97dc9 Mon Sep 17 00:00:00 2001 From: pkova Date: Tue, 2 Jul 2024 11:01:37 +0300 Subject: [PATCH 183/430] wip: mesa jumbo frames --- pkg/vere/io/mesa.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 8e271eb08e..dad751862d 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1724,6 +1724,9 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun nun) //u3_noun pax = _mesa_path_with_fra(pac_u->pek_u.nam_u.pat_c, &fra_s); u3_weak hit = u3r_at(7, nun); + u3_noun proof = u3h(hit); + hit = u3t(hit); + if ( u3_none == hit ) { // TODO: mark as dead //u3z(nun); @@ -1754,7 +1757,21 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun nun) for (c3_w i=0; i < siz; i++) { // (cut 3 [wid 1] dat.byts)) - u3_atom lin = u3qc_cut(13, i, 1, hit); + /* u3_atom lin = u3qc_cut(13, i, 1, hit); */ + tac_u.pag_u.nam_u.fra_w = i; + + u3_noun pair = u3h(proof); + + if (pair == u3_nul) { + tac_u.pag_u.dat_u.aup_u.len_y = 0; + } else { + tac_u.pag_u.dat_u.aup_u.len_y = 2; + tac_u.pag_u.dat_u.aup_u. + } + + c3_y* buf_y[PACT_SIZE]; + + c3_w len_w = mesa_etch_pact(buf_y, &tac_u); pac_u->pek_u.nam_u.fra_w = i; From c4489aa6de6c1d6e6efbee5d74d10cbd61a2f893 Mon Sep 17 00:00:00 2001 From: Santeri Hannula Date: Fri, 28 Jun 2024 19:01:55 +0300 Subject: [PATCH 184/430] pier: change boot endpoint response type --- pkg/vere/pier.c | 80 +++++++++++++++++++++++++------------------------ 1 file changed, 41 insertions(+), 39 deletions(-) diff --git a/pkg/vere/pier.c b/pkg/vere/pier.c index a0611b770d..772812f895 100644 --- a/pkg/vere/pier.c +++ b/pkg/vere/pier.c @@ -551,52 +551,49 @@ _resolve_czar(u3_work* wok_u, c3_c* who_c) return czar_c; } -static c3_w -_czar_peer_rift(c3_c* czar_c, c3_c* who_c) +static c3_o +_czar_boot_data(c3_c* czar_c, + c3_c* who_c, + c3_w* bone_w, + c3_w* czar_glx_w, + c3_w* czar_ryf_w, + c3_w* czar_lyf_w, + c3_w* czar_bon_w, + c3_w* czar_ack_w) { - c3_w czar_ryf_w = 0xFFFFFFFF; c3_c url[256]; c3_w len_w; - c3_y* hun_y; + c3_y* hun_y = 0; - sprintf(url, "https://%s.urbit.org/~/boot/%s", czar_c+1, who_c); + if (bone_w != NULL) { + sprintf(url, "https://%s.urbit.org/~/boot/%s/%d", + czar_c+1, who_c, *bone_w + 1); + } else { + sprintf(url, "https://%s.urbit.org/~/boot/%s", czar_c+1, who_c); + } c3_i ret_i = king_curl_bytes(url, &len_w, &hun_y, 1); if (!ret_i) { u3_noun jamd = u3dc("slav", c3__uw, u3i_bytes(len_w, hun_y)); u3_noun cued = u3qe_cue(jamd); - u3_noun czar_glx, czar_ryf, czar_lyf; - u3x_trel(cued, &czar_glx, &czar_ryf, &czar_lyf); - czar_ryf_w = u3r_word(0, czar_ryf); + u3_noun czar_glx, czar_ryf, czar_lyf, czar_bon, czar_ack; + u3x_hext(cued, 0, &czar_glx, &czar_ryf, &czar_lyf, &czar_bon, &czar_ack); + + *czar_glx_w = u3r_word(0, czar_glx); + *czar_ryf_w = u3r_word(0, czar_ryf); + *czar_lyf_w = u3r_word(0, czar_lyf); + if (czar_bon != 0) *czar_bon_w = u3r_word(0, u3t(czar_bon)); + if (czar_ack != 0) *czar_ack_w = u3r_word(0, u3t(czar_ack)); u3z(jamd); u3z(cued); c3_free(hun_y); - } - - return czar_ryf_w; -} - -static c3_w -_czar_last_ack(c3_c* czar_c, - c3_c* who_c, - c3_w bone_w) -{ - c3_w czar_last_ack_w = 0xFFFFFFFF; - c3_c url[256]; - c3_w len_w; - c3_y* hun_y; - sprintf(url, "https://%s.urbit.org/~/boot/%s/%d", czar_c+1, who_c, bone_w + 1); - - c3_i ret_i = king_curl_bytes(url, &len_w, &hun_y, 1); - if (!ret_i) { - czar_last_ack_w = strtoumax((c3_c*)hun_y, NULL, 10); + return c3y; } - c3_free(hun_y); - return czar_last_ack_w; + return c3n; } static void @@ -604,11 +601,13 @@ _boot_scry_cb(void* vod_p, u3_noun nun) { u3_work* wok_u = (u3_work*)vod_p; - u3_atom who = u3dc("scot", c3__p, u3i_chubs(2, wok_u->pir_u->who_d)); - c3_c* who_c = u3r_string(who); + u3_atom who = u3dc("scot", c3__p, u3i_chubs(2, wok_u->pir_u->who_d)); + c3_c* who_c = u3r_string(who); - u3_noun rem, glx, ryf, bon, cur, nex; - c3_w glx_w, ryf_w, bon_w, cur_w, nex_w; + u3_noun rem, glx, ryf, bon, cur, nex; + c3_w glx_w, ryf_w, bon_w, cur_w, nex_w; + + c3_w czar_glx_w, czar_ryf_w, czar_lyf_w, czar_bon_w, czar_ack_w = 0xFFFFFFFF; if (c3y == u3r_qual(nun, 0, 0, 0, &rem) && c3y == u3r_hext(rem, &glx, &ryf, 0, &bon, &cur, &nex)) { @@ -622,14 +621,15 @@ _boot_scry_cb(void* vod_p, u3_noun nun) u3_atom czar = u3dc("scot", c3__p, glx_w); c3_c* czar_c = u3r_string(czar); - c3_w czar_ryf_w = _czar_peer_rift(czar_c, who_c); - if (czar_ryf_w == 0xFFFFFFFF) { + if (c3n == _czar_boot_data(czar_c, who_c, &bon_w, + &czar_glx_w, &czar_ryf_w, + &czar_lyf_w, &czar_bon_w, + &czar_ack_w)) { u3l_log("boot: peer-state unvailable on czar, cannot protect from double boot"); _pier_work(wok_u); } else { if (czar_ryf_w == ryf_w) { - c3_w czar_ack_w = _czar_last_ack(czar_c, who_c, bon_w); c3_w ack_w = cur_w - 1; if (czar_ack_w == 0xFFFFFFFF) { // This codepath should never be hit @@ -660,13 +660,15 @@ _boot_scry_cb(void* vod_p, u3_noun nun) * otherwise continue boot. */ c3_c* czar_c = _resolve_czar(wok_u, who_c); - c3_w czar_ryf_w = _czar_peer_rift(czar_c, who_c); - c3_free(czar_c); - if (czar_ryf_w == 0xFFFFFFFF) { + if (c3n == _czar_boot_data(czar_c, who_c, 0, + &czar_glx_w, &czar_ryf_w, + &czar_lyf_w, 0, 0)) { + c3_free(czar_c); _pier_work(wok_u); } else { // Peer state found under czar + c3_free(czar_c); u3_weak kf_ryf = wok_u->pir_u->ryf; if (kf_ryf == u3_none) { u3l_log("boot: keyfile rift unavailable, cannot protect from double boot"); From 69455993bb7f8d0a2b8fc1ddcf244465eb3d8c85 Mon Sep 17 00:00:00 2001 From: Santeri Hannula Date: Fri, 28 Jun 2024 19:12:48 +0300 Subject: [PATCH 185/430] king: break king_curl_bytes retry loop on client side errors --- pkg/vere/king.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/vere/king.c b/pkg/vere/king.c index 7e87e8b622..e0d4bbac00 100644 --- a/pkg/vere/king.c +++ b/pkg/vere/king.c @@ -329,7 +329,7 @@ king_curl_bytes(c3_c* url_c, c3_w* len_w, c3_y** hun_y, c3_t veb_t) u3l_log("curl: error fetching %s: HTTP %ld", url_c, cod_i); } ret_i = -2; - if ( cod_i == 404 ) { + if ( 400 <= cod_i && cod_i < 500 ) { break; } } From 46575f870d3c15c837d2828b247fc33010ff115e Mon Sep 17 00:00:00 2001 From: Santeri Hannula Date: Tue, 2 Jul 2024 15:34:52 +0300 Subject: [PATCH 186/430] pier: `_resolve_czar`, remove redundant `u3k`s & and free czar noun --- pkg/vere/pier.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pkg/vere/pier.c b/pkg/vere/pier.c index 772812f895..e3a9cf53e3 100644 --- a/pkg/vere/pier.c +++ b/pkg/vere/pier.c @@ -533,8 +533,8 @@ _pier_on_scry_done(void* ptr_v, u3_noun nun) static c3_c* _resolve_czar(u3_work* wok_u, c3_c* who_c) { - u3_noun czar = u3dc("scot", 'p', u3k(wok_u->pir_u->who_d[0] & ((1 << 8) - 1))); - c3_c* czar_c = u3r_string(u3k(czar)); + u3_noun czar = u3dc("scot", 'p', wok_u->pir_u->who_d[0] & ((1 << 8) - 1)); + c3_c* czar_c = u3r_string(czar); c3_c url[256]; c3_w len_w; @@ -548,6 +548,7 @@ _resolve_czar(u3_work* wok_u, c3_c* who_c) czar_c = (c3_c*)hun_y; } + u3z(czar); return czar_c; } From 8f9d0b29b99dcb2ca80ba45dce445635ef294743 Mon Sep 17 00:00:00 2001 From: Santeri Hannula Date: Tue, 2 Jul 2024 15:42:29 +0300 Subject: [PATCH 187/430] pier: fix styling of double boot protection if-statements --- pkg/vere/pier.c | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/pkg/vere/pier.c b/pkg/vere/pier.c index e3a9cf53e3..9e88d97818 100644 --- a/pkg/vere/pier.c +++ b/pkg/vere/pier.c @@ -566,7 +566,7 @@ _czar_boot_data(c3_c* czar_c, c3_w len_w; c3_y* hun_y = 0; - if (bone_w != NULL) { + if ( bone_w != NULL ) { sprintf(url, "https://%s.urbit.org/~/boot/%s/%d", czar_c+1, who_c, *bone_w + 1); } else { @@ -574,7 +574,7 @@ _czar_boot_data(c3_c* czar_c, } c3_i ret_i = king_curl_bytes(url, &len_w, &hun_y, 1); - if (!ret_i) { + if ( !ret_i ) { u3_noun jamd = u3dc("slav", c3__uw, u3i_bytes(len_w, hun_y)); u3_noun cued = u3qe_cue(jamd); @@ -584,8 +584,8 @@ _czar_boot_data(c3_c* czar_c, *czar_glx_w = u3r_word(0, czar_glx); *czar_ryf_w = u3r_word(0, czar_ryf); *czar_lyf_w = u3r_word(0, czar_lyf); - if (czar_bon != 0) *czar_bon_w = u3r_word(0, u3t(czar_bon)); - if (czar_ack != 0) *czar_ack_w = u3r_word(0, u3t(czar_ack)); + if ( czar_bon != 0 ) *czar_bon_w = u3r_word(0, u3t(czar_bon)); + if ( czar_ack != 0 ) *czar_ack_w = u3r_word(0, u3t(czar_ack)); u3z(jamd); u3z(cued); @@ -610,8 +610,8 @@ _boot_scry_cb(void* vod_p, u3_noun nun) c3_w czar_glx_w, czar_ryf_w, czar_lyf_w, czar_bon_w, czar_ack_w = 0xFFFFFFFF; - if (c3y == u3r_qual(nun, 0, 0, 0, &rem) && - c3y == u3r_hext(rem, &glx, &ryf, 0, &bon, &cur, &nex)) { + if ( (c3y == u3r_qual(nun, 0, 0, 0, &rem)) && + (c3y == u3r_hext(rem, &glx, &ryf, 0, &bon, &cur, &nex)) ) { /* * Boot scry succeeded. Proceed to cross reference networking state against * sponsoring galaxy. @@ -623,21 +623,21 @@ _boot_scry_cb(void* vod_p, u3_noun nun) u3_atom czar = u3dc("scot", c3__p, glx_w); c3_c* czar_c = u3r_string(czar); - if (c3n == _czar_boot_data(czar_c, who_c, &bon_w, - &czar_glx_w, &czar_ryf_w, - &czar_lyf_w, &czar_bon_w, - &czar_ack_w)) { + if ( c3n == _czar_boot_data(czar_c, who_c, &bon_w, + &czar_glx_w, &czar_ryf_w, + &czar_lyf_w, &czar_bon_w, + &czar_ack_w) ) { u3l_log("boot: peer-state unvailable on czar, cannot protect from double boot"); _pier_work(wok_u); } else { - if (czar_ryf_w == ryf_w) { + if ( czar_ryf_w == ryf_w ) { c3_w ack_w = cur_w - 1; - if (czar_ack_w == 0xFFFFFFFF) { + if ( czar_ack_w == 0xFFFFFFFF ) { // This codepath should never be hit u3l_log("boot: message-sink-state unvailable on czar, cannot protect from double boot"); _pier_work(wok_u); - } else if (czar_ack_w == ack_w || - (nex_w > cur_w && czar_ack_w - 1 == ack_w)) { + } else if ( (czar_ack_w == ack_w) || + ((nex_w > cur_w) && (czar_ack_w - 1 == ack_w)) ) { _pier_work(wok_u); } else { u3l_log("boot: failed: czar last ack: %d, ship last ack: %d", @@ -654,7 +654,7 @@ _boot_scry_cb(void* vod_p, u3_noun nun) u3z(czar); c3_free(czar_c); - } else if (c3y == u3r_trel(nun, 0, 0, &rem) && rem == 0) { + } else if ( c3y == u3r_trel(nun, 0, 0, &rem) && rem == 0 ) { /* * Data not available for boot scry. Check against sponsoring galaxy. * If peer state exists exit(1) unless ship has breached, @@ -662,19 +662,19 @@ _boot_scry_cb(void* vod_p, u3_noun nun) */ c3_c* czar_c = _resolve_czar(wok_u, who_c); - if (c3n == _czar_boot_data(czar_c, who_c, 0, - &czar_glx_w, &czar_ryf_w, - &czar_lyf_w, 0, 0)) { + if ( c3n == _czar_boot_data(czar_c, who_c, 0, + &czar_glx_w, &czar_ryf_w, + &czar_lyf_w, 0, 0) ) { c3_free(czar_c); _pier_work(wok_u); } else { // Peer state found under czar c3_free(czar_c); u3_weak kf_ryf = wok_u->pir_u->ryf; - if (kf_ryf == u3_none) { + if ( kf_ryf == u3_none ) { u3l_log("boot: keyfile rift unavailable, cannot protect from double boot"); _pier_work(wok_u); - } else if (kf_ryf > czar_ryf_w) { + } else if ( kf_ryf > czar_ryf_w ) { // Ship has breached, continue boot _pier_work(wok_u); } else { @@ -784,7 +784,7 @@ _pier_work_init(u3_pier* pir_u) c3_d pi_d = wok_u->pir_u->who_d[0]; c3_d pt_d = wok_u->pir_u->who_d[1]; - if ((pi_d < 256 && pt_d == 0) || c3n == u3_Host.ops_u.net) { + if ( (pi_d < 256 && pt_d == 0) || (c3n == u3_Host.ops_u.net) ) { // Skip double boot protection for galaxies and local mode ships // _pier_work(wok_u); From 600e0f322f6ae44ae1c7405294d2c8647d0890ee Mon Sep 17 00:00:00 2001 From: Santeri Hannula Date: Tue, 2 Jul 2024 15:45:27 +0300 Subject: [PATCH 188/430] pier: safely destructure %boot http response --- pkg/vere/pier.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/pkg/vere/pier.c b/pkg/vere/pier.c index 9e88d97818..9fa19e1a42 100644 --- a/pkg/vere/pier.c +++ b/pkg/vere/pier.c @@ -573,28 +573,32 @@ _czar_boot_data(c3_c* czar_c, sprintf(url, "https://%s.urbit.org/~/boot/%s", czar_c+1, who_c); } + c3_o ret_o = c3n; c3_i ret_i = king_curl_bytes(url, &len_w, &hun_y, 1); if ( !ret_i ) { u3_noun jamd = u3dc("slav", c3__uw, u3i_bytes(len_w, hun_y)); u3_noun cued = u3qe_cue(jamd); u3_noun czar_glx, czar_ryf, czar_lyf, czar_bon, czar_ack; - u3x_hext(cued, 0, &czar_glx, &czar_ryf, &czar_lyf, &czar_bon, &czar_ack); - *czar_glx_w = u3r_word(0, czar_glx); - *czar_ryf_w = u3r_word(0, czar_ryf); - *czar_lyf_w = u3r_word(0, czar_lyf); - if ( czar_bon != 0 ) *czar_bon_w = u3r_word(0, u3t(czar_bon)); - if ( czar_ack != 0 ) *czar_ack_w = u3r_word(0, u3t(czar_ack)); + if ( (c3y == u3r_hext(cued, 0, &czar_glx, &czar_ryf, + &czar_lyf, &czar_bon, &czar_ack)) && + (c3y == u3r_safe_word(czar_glx, czar_glx_w)) && + (c3y == u3r_safe_word(czar_ryf, czar_ryf_w)) && + (c3y == u3r_safe_word(czar_lyf, czar_lyf_w)) && + (c3y == u3du(czar_bon)) && + (c3y == u3r_safe_word(u3t(czar_bon), czar_bon_w)) && + (c3y == u3du(czar_ack)) && + (c3y == u3r_safe_word(u3t(czar_ack), czar_ack_w)) ) { + ret_o = c3y; + } u3z(jamd); u3z(cued); c3_free(hun_y); - - return c3y; } - return c3n; + return ret_o; } static void From 92a8b3247c486d9769eaaaf01a3814793aaf0bb1 Mon Sep 17 00:00:00 2001 From: Santeri Hannula Date: Tue, 2 Jul 2024 15:49:39 +0300 Subject: [PATCH 189/430] pier: use `u3_king_bail` instead of `exit` --- pkg/vere/pier.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/vere/pier.c b/pkg/vere/pier.c index 9fa19e1a42..916fbdd08d 100644 --- a/pkg/vere/pier.c +++ b/pkg/vere/pier.c @@ -646,13 +646,13 @@ _boot_scry_cb(void* vod_p, u3_noun nun) } else { u3l_log("boot: failed: czar last ack: %d, ship last ack: %d", czar_ack_w, ack_w); - exit(1); + u3_king_bail(); } } else { // Trying to boot old ship after breach u3l_log("boot: failed: rift in czar peer-state: %d, current rift: %d", czar_ryf_w, ryf_w); - exit(1); + u3_king_bail(); } } @@ -684,7 +684,7 @@ _boot_scry_cb(void* vod_p, u3_noun nun) } else { u3l_log("boot: failed: rift in czar peer state: %d, keyfile rift: %d", czar_ryf_w, kf_ryf); - exit(1); + u3_king_bail(); } } } else { From fad9af63ccb6927dc4a5b66fb340a11ac438f317 Mon Sep 17 00:00:00 2001 From: Santeri Hannula Date: Tue, 2 Jul 2024 16:31:31 +0300 Subject: [PATCH 190/430] pier: change `%boot` response bodies into binary data --- pkg/vere/pier.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/vere/pier.c b/pkg/vere/pier.c index 916fbdd08d..4dab2fbd3c 100644 --- a/pkg/vere/pier.c +++ b/pkg/vere/pier.c @@ -576,7 +576,7 @@ _czar_boot_data(c3_c* czar_c, c3_o ret_o = c3n; c3_i ret_i = king_curl_bytes(url, &len_w, &hun_y, 1); if ( !ret_i ) { - u3_noun jamd = u3dc("slav", c3__uw, u3i_bytes(len_w, hun_y)); + u3_noun jamd = u3i_bytes(len_w, hun_y); u3_noun cued = u3qe_cue(jamd); u3_noun czar_glx, czar_ryf, czar_lyf, czar_bon, czar_ack; From ccc97560046b3166da55900a06e5a087a95e6b95 Mon Sep 17 00:00:00 2001 From: lukechampine Date: Tue, 2 Jul 2024 12:17:17 -0400 Subject: [PATCH 191/430] lss: further optimize _lss_verifier_find_pair --- pkg/vere/io/lss.c | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/pkg/vere/io/lss.c b/pkg/vere/io/lss.c index d617a5b735..1d5cf40073 100644 --- a/pkg/vere/io/lss.c +++ b/pkg/vere/io/lss.c @@ -137,28 +137,20 @@ void lss_builder_free(lss_builder* bil_u) { c3_free(bil_u); } -static c3_o _lss_verifier_find_pair(lss_verifier* los_u, c3_w height, c3_b sel, lss_hash h) +static c3_o _lss_verifier_find_pair(lss_verifier* los_u, c3_w i, c3_w height, lss_hash h) { - if (memcmp(los_u->pairs[height][sel], h, 32) == 0) { - return c3y; - } - // "Unbalanced" pairs have two children on the left and one on the right. This - // results in the right child being emplaced at a greater height than it - // "should" be, so the standard check (above) misses it. - // - // The locations of these single children are fully determined by the size of - // the tree. To find the adjusted height, we simply "climb up" however many - // unbalanced pairs are directly above us. - c3_w unbalanced = (1<<_bits_len64(los_u->leaves-1)) - los_u->leaves; - height += _bits_ctz64(~(unbalanced >> height)); - return _(memcmp(los_u->pairs[height][1], h, 32) == 0); + c3_w odd = (1<<_bits_len64(los_u->leaves-1)) - los_u->leaves; + c3_w mask = (1<<_bits_len64(i ^ (los_u->leaves-1))) - 1; + height += _bits_ctz64(~((odd&~mask) >> height)); + c3_b parity = (i >> height) & 1; + return _(memcmp(los_u->pairs[height][parity], h, 32) == 0); } c3_o lss_verifier_ingest(lss_verifier* los_u, c3_y* leaf_y, c3_w leaf_w, lss_pair* pair) { // verify leaf lss_hash h; _leaf_hash(h, leaf_y, leaf_w, los_u->counter); - if ( c3n == _lss_verifier_find_pair(los_u, 0, los_u->counter&1, h) ) { + if ( c3n == _lss_verifier_find_pair(los_u, los_u->counter, 0, h) ) { return c3n; } // check whether pair is expected @@ -169,15 +161,15 @@ c3_o lss_verifier_ingest(lss_verifier* los_u, c3_y* leaf_y, c3_w leaf_w, lss_pai return c3y; } // verify and insert pair - c3_w height = _bits_ctz64(los_u->counter) + 1; - c3_b sel = ~(los_u->counter >> height) & 1; + c3_w height = _bits_ctz64(los_u->counter); + c3_w pairStart = los_u->counter + (1 << height); // first leaf "covered" by this pair lss_hash parent_hash; _parent_hash(parent_hash, (*pair)[0], (*pair)[1]); - if ( c3n == _lss_verifier_find_pair(los_u, height, sel, parent_hash) ) { + if ( c3n == _lss_verifier_find_pair(los_u, pairStart, height+1, parent_hash) ) { return c3n; } - memcpy(los_u->pairs[height-1][0], (*pair)[0], sizeof(lss_hash)); - memcpy(los_u->pairs[height-1][1], (*pair)[1], sizeof(lss_hash)); + memcpy(los_u->pairs[height][0], (*pair)[0], sizeof(lss_hash)); + memcpy(los_u->pairs[height][1], (*pair)[1], sizeof(lss_hash)); los_u->counter++; return c3y; } From 05a5db9f88de0764e03f02fe0d4c4d75e41d30eb Mon Sep 17 00:00:00 2001 From: midden-fabler Date: Tue, 2 Jul 2024 12:25:30 -0400 Subject: [PATCH 192/430] http: better mime slicing, header parsing --- pkg/vere/io/http.c | 85 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 63 insertions(+), 22 deletions(-) diff --git a/pkg/vere/io/http.c b/pkg/vere/io/http.c index c1008048a9..7b5d27590d 100644 --- a/pkg/vere/io/http.c +++ b/pkg/vere/io/http.c @@ -696,8 +696,9 @@ _find_tis_fas(void* txt, c3_w len) // [x] range header // [x] u3qc_cut // [x] better range header parsing -// [ ] better slicing -// [ ] 216 +// [x] better slicing +// [ ] 206 +// [ ] 200 // [ ] _http_range_respond? // [ ] multipart ranges // @@ -706,28 +707,62 @@ typedef struct _range_header { c3_z end_z; } range_header; +static u3_noun +_slice_mime(range_header rng, u3_noun octs) +{ + c3_w lent_w = u3h(octs); + c3_w oct_w = u3t(octs); + u3_noun out; + + if ( rng.start_z == SIZE_MAX ) { + if ( rng.end_z == SIZE_MAX) { + // [~ ~] + out = u3_nul; + } + else { + // [~ @] + if ( rng.end_z > lent_w ) { + out = u3_nul; + } + else { + // slice last bytes + out = u3nc( rng.end_z, + u3qc_cut(3, lent_w - rng.end_z, rng.end_z, oct_w)); + } + } + } + else if ( rng.end_z == SIZE_MAX ) { + // [@ ~] + if ( rng.start_z > lent_w ) { + out = u3_nul; + } + else { + out = u3nc( lent_w - rng.start_z, + u3qc_cut(3, rng.start_z, lent_w, oct_w)); + } + } + else if (rng.end_z > lent_w) { + out = u3_nul; + } + else { + // [@ @] + out = u3nc( (rng.end_z - rng.start_z) + 1, + u3qc_cut(3, rng.start_z, (rng.end_z - rng.start_z) + 1, oct_w)); + } + return out; +} + static range_header _get_range(c3_c* txt_c, c3_w len_w) { c3_c* hep_c = memchr(txt_c, '-', len_w); - c3_c* txt_two_c = txt_c; range_header slice; slice.start_z = SIZE_MAX; slice.end_z = SIZE_MAX; - // - - // - - // -, - - // -, -, - - // - - if ( hep_c ) { - slice.start_z = h2o_strtosizefwd(&txt_two_c, hep_c - txt_c); - if ( slice.start_z != SIZE_MAX ) { - u3_assert(txt_two_c == hep_c); - } - txt_two_c = hep_c + 1; - slice.end_z = h2o_strtosizefwd(&txt_two_c, len_w - (txt_two_c - txt_c)); + slice.start_z = h2o_strtosize(txt_c, hep_c - txt_c); + slice.end_z = h2o_strtosize(hep_c + 1, len_w - ((hep_c + 1) - txt_c)); } return slice; @@ -876,26 +911,32 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) req_u->peq_u, _http_foo_cb); } else { - // TODO range h2o_headers_t req_headers = req_u->rec_u->headers; c3_w idx = h2o_find_header(&req_headers, H2O_TOKEN_RANGE, -1); if (idx != UINT32_MAX) { - if ( (req_headers.entries[idx].value.len > 6) && + if ( (req_headers.entries[idx].value.len >= 6) && (memcmp("bytes=", req_headers.entries[idx].value.base, 6) == 0 )) { c3_w rest_len = req_headers.entries[idx].value.len - 6; + if ( rest_len == 0) { + c3_c* msg_c = "Requested Range Not Satisfiable"; + h2o_send_error_generic(req_u->rec_u, 416, msg_c, msg_c, 0); + return; + } range_header rng_hed = _get_range(req_headers.entries[idx].value.base + 6, rest_len); - u3l_log("struct start %lu, end %lu", rng_hed.start_z, rng_hed.end_z); u3_noun octs = u3r_at(127, nac); if ( octs == u3_none) { c3_c* msg_c = "Requested Range Not Satisfiable"; h2o_send_error_generic(req_u->rec_u, 416, msg_c, msg_c, 0); return; } - // TODO check range vs size - u3_noun piece = u3qc_cut(3, rng_hed.start_z, rng_hed.end_z + 1, u3t(octs)); - u3_noun result = u3nc(rng_hed.end_z + 1 - rng_hed.start_z, piece); - u3m_p("result", result); + u3_noun result = _slice_mime(rng_hed, octs); + + if ( result == u3_nul) { + c3_c* msg_c = "Requested Range Not Satisfiable"; + h2o_send_error_generic(req_u->rec_u, 416, msg_c, msg_c, 0); + return; + } u3_noun res = u3i_edit(nac, 127, result); _http_cache_respond(req_u, res); } From 8d93ef6404771e3c360a5522ff871018cbf601b0 Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Tue, 2 Jul 2024 14:43:39 -0400 Subject: [PATCH 193/430] mesa: WIP adding route switching, some cleanup --- pkg/vere/io/mesa.c | 57 ++++++++++++++-------------------------------- 1 file changed, 17 insertions(+), 40 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index dd44d9df20..8c457c2c07 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -95,14 +95,14 @@ typedef struct _u3_pact_stat { c3_d sen_d; // last sent c3_y sip_y; // skips c3_y tie_y; // tries - c3_y dup_y; // dupes } u3_pact_stat; struct _u3_mesa; typedef struct _u3_peer_last { - c3_d acc_d; // time of last access - c3_d son_d; // time of sponsor check + c3_d her_d; // time we last heard from the direct route + c3_d dir_d; // time we last sent a packet on the direct route + c3_d ind_d; // time we last sent a packet on the indirect route } u3_peer_last; typedef struct _u3_misord_buf { @@ -135,7 +135,7 @@ typedef struct _u3_pend_req { lss_verifier* los_u; // Lockstep verifier u3_mesa_pict* pic_u; // preallocated request packet u3_pact_stat* wat_u; // ((mop @ud packet-state) lte) - u3_bitset was_u; // ((mop @ud packet-state) lte) + u3_bitset was_u; // ((mop @ud ?) lte) c3_y pad_y[64]; } u3_pend_req; @@ -669,7 +669,6 @@ _mesa_req_pact_sent(u3_pend_req* req_u, u3_mesa_name* nam_u) req_u->wat_u[nam_u->fra_w].sen_d = now_d; req_u->wat_u[nam_u->fra_w].sip_y = 0; req_u->wat_u[nam_u->fra_w].tie_y = 1; - req_u->wat_u[nam_u->fra_w].dup_y = 0; /* u3l_log("bitset_put %u", nam_u->fra_w); */ bitset_put(&req_u->was_u, nam_u->fra_w); @@ -822,51 +821,34 @@ static void _mesa_send(u3_mesa_pict* pic_u, u3_lane* lan_u) _mesa_send_buf(sam_u, *lan_u, buf_y, siz_w); } +// TODO: check whether direct, decide what to send static void _try_resend(u3_pend_req* req_u) { u3_mesa* sam_u = req_u->pic_u->sam_u; u3_lane* lan_u = &req_u->lan_u; c3_o los_o = c3n; - /* if ( req_u->tot_w == 0 || req_u->ack_w <= REORDER_THRESH ) { */ - /* u3l_log("reorder thresh too low %u", req_u->ack_w); */ - /* return; */ - /* } */ - c3_w ack_w = req_u->ack_w - REORDER_THRESH; c3_d now_d = _get_now_micros(); - /* u3l_log("lef_w %u, nex_w %u, len_w %u, tot_w %u", req_u->lef_w, req_u->nex_w, req_u->len_w, req_u->tot_w); */ + u3_mesa_pact *pac_u = &req_u->pic_u->pac_u; + u3_mesa_name *nam_u = &pac_u->pek_u.nam_u; + for ( int i = req_u->lef_w; i < req_u->nex_w; i++ ) { + // TODO: make fast recovery different from slow + // TODO: track skip count but not dupes, since dupes are meaningless if ( c3y == bitset_has(&req_u->was_u, i) ) { - req_u->pic_u->pac_u.pek_u.nam_u.fra_w = i; - if ( req_u->wat_u[i].tie_y == 1 ) { - /* u3l_log("fast resend %u", i); */ - los_o = c3y; - c3_y* buf_y = c3_calloc(PACT_SIZE); - req_u->pic_u->pac_u.pek_u.nam_u.fra_w = i; - c3_w siz_w = mesa_etch_pact(buf_y, &req_u->pic_u->pac_u); - if ( siz_w == 0 ) { - u3l_log("failed to etch"); - u3_assert( 0 ); - } - // TODO: better route management - _mesa_send_buf(sam_u, *lan_u, buf_y, siz_w); - _mesa_req_pact_resent(req_u, &req_u->pic_u->pac_u.pek_u.nam_u); - - } else if ( (now_d - req_u->wat_u[i].sen_d) > req_u->gag_u->rto_w ) { + nam_u->fra_w = i; + if ( ( req_u->wat_u[i].tie_y == 1 ) || // TODO wth? + ( (now_d - req_u->wat_u[i].sen_d) > req_u->gag_u->rto_w ) ) { los_o = c3y; c3_y* buf_y = c3_calloc(PACT_SIZE); - req_u->pic_u->pac_u.pek_u.nam_u.fra_w = i; - /* u3l_log("slow resending %u ", i); */ - c3_w siz_w = mesa_etch_pact(buf_y, &req_u->pic_u->pac_u); - if( siz_w == 0 ) { + c3_w siz_w = mesa_etch_pact(buf_y, pac_u); + if ( 0 == siz_w ) { u3l_log("failed to etch"); u3_assert( 0 ); } // TODO: better route management _mesa_send_buf(sam_u, *lan_u, buf_y, siz_w); - _mesa_req_pact_resent(req_u, &req_u->pic_u->pac_u.pek_u.nam_u); - } else { - /* u3l_log("did nothing %u %llu %u", req_u->wat_u[i].tie_y, (now_d - req_u->wat_u[i].sen_d), req_u->gag_u->rto_w); */ + _mesa_req_pact_resent(req_u, nam_u); } } } @@ -1001,7 +983,6 @@ _mesa_req_pact_done(u3_mesa* sam_u, u3_mesa_name *nam_u, u3_mesa_data* dat_u, u3 // received duplicate if ( c3n == bitset_has(&req_u->was_u, nam_u->fra_w) ) { /* MESA_LOG(DUPE); */ - req_u->wat_u[nam_u->fra_w].dup_y++; return req_u; } @@ -1814,9 +1795,6 @@ _saxo_cb(void* vod_p, u3_noun nun) static void _meet_peer(u3_mesa* sam_u, u3_peer* per_u, c3_d her_d[2]) { - c3_d now_d = _get_now_micros(); - per_u->las_u.son_d = now_d; - u3_noun her = u3i_chubs(2, her_d); u3_noun gan = u3nc(u3_nul, u3_nul); u3_noun pax = u3nc(u3dc("scot", c3__p, her), u3_nul); @@ -1826,10 +1804,9 @@ _meet_peer(u3_mesa* sam_u, u3_peer* per_u, c3_d her_d[2]) static void _hear_peer(u3_mesa* sam_u, u3_peer* per_u, u3_lane lan_u, c3_o dir_o) { - c3_d now_d = _get_now_micros(); - per_u->las_u.acc_d = now_d; if ( c3y == dir_o ) { per_u->dir_u = lan_u; + per_u->las_u.her_d = _get_now_micros(); } else { per_u->ind_u = lan_u; } From ee82216555c54f08f6a67af27eaf7856c237d326 Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Tue, 2 Jul 2024 14:48:32 -0400 Subject: [PATCH 194/430] mesa: WIP rectified try_resend() conditional --- pkg/vere/io/mesa.c | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 8c457c2c07..7eb03b7d29 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -830,26 +830,23 @@ _try_resend(u3_pend_req* req_u) c3_o los_o = c3n; c3_d now_d = _get_now_micros(); u3_mesa_pact *pac_u = &req_u->pic_u->pac_u; - u3_mesa_name *nam_u = &pac_u->pek_u.nam_u; for ( int i = req_u->lef_w; i < req_u->nex_w; i++ ) { // TODO: make fast recovery different from slow // TODO: track skip count but not dupes, since dupes are meaningless - if ( c3y == bitset_has(&req_u->was_u, i) ) { - nam_u->fra_w = i; - if ( ( req_u->wat_u[i].tie_y == 1 ) || // TODO wth? - ( (now_d - req_u->wat_u[i].sen_d) > req_u->gag_u->rto_w ) ) { - los_o = c3y; - c3_y* buf_y = c3_calloc(PACT_SIZE); - c3_w siz_w = mesa_etch_pact(buf_y, pac_u); - if ( 0 == siz_w ) { - u3l_log("failed to etch"); - u3_assert( 0 ); - } - // TODO: better route management - _mesa_send_buf(sam_u, *lan_u, buf_y, siz_w); - _mesa_req_pact_resent(req_u, nam_u); + if ( ( c3y == bitset_has(&req_u->was_u, i) ) && + ( (now_d - req_u->wat_u[i].sen_d) > req_u->gag_u->rto_w ) ) { + pac_u->pek_u.nam_u.fra_w = i; + los_o = c3y; + c3_y* buf_y = c3_calloc(PACT_SIZE); + c3_w siz_w = mesa_etch_pact(buf_y, pac_u); + if ( 0 == siz_w ) { + u3l_log("failed to etch"); + u3_assert( 0 ); } + // TODO: better route management + _mesa_send_buf(sam_u, *lan_u, buf_y, siz_w); + _mesa_req_pact_resent(req_u, &pac_u->pek_u.nam_u); } } From 8389cb85fb10a9a841b51b89d8c6298ec849dfe5 Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Tue, 2 Jul 2024 16:10:14 -0400 Subject: [PATCH 195/430] mesa: minor try_resend() cleanup --- pkg/vere/io/mesa.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 7eb03b7d29..dd74030032 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -825,8 +825,6 @@ static void _mesa_send(u3_mesa_pict* pic_u, u3_lane* lan_u) static void _try_resend(u3_pend_req* req_u) { - u3_mesa* sam_u = req_u->pic_u->sam_u; - u3_lane* lan_u = &req_u->lan_u; c3_o los_o = c3n; c3_d now_d = _get_now_micros(); u3_mesa_pact *pac_u = &req_u->pic_u->pac_u; @@ -836,8 +834,9 @@ _try_resend(u3_pend_req* req_u) // TODO: track skip count but not dupes, since dupes are meaningless if ( ( c3y == bitset_has(&req_u->was_u, i) ) && ( (now_d - req_u->wat_u[i].sen_d) > req_u->gag_u->rto_w ) ) { - pac_u->pek_u.nam_u.fra_w = i; los_o = c3y; + + pac_u->pek_u.nam_u.fra_w = i; c3_y* buf_y = c3_calloc(PACT_SIZE); c3_w siz_w = mesa_etch_pact(buf_y, pac_u); if ( 0 == siz_w ) { @@ -845,7 +844,7 @@ _try_resend(u3_pend_req* req_u) u3_assert( 0 ); } // TODO: better route management - _mesa_send_buf(sam_u, *lan_u, buf_y, siz_w); + _mesa_send_buf(req_u->pic_u->sam_u, req_u->lan_u, buf_y, siz_w); _mesa_req_pact_resent(req_u, &pac_u->pek_u.nam_u); } } From 05ec0eabe0aea6ff70af0a633eca2e60fdf8c404 Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Tue, 2 Jul 2024 16:10:34 -0400 Subject: [PATCH 196/430] mesa: add proposed state as hoon comment --- pkg/vere/io/mesa.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index dd74030032..43b8652ba1 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -148,6 +148,25 @@ typedef struct _u3_czar_info { u3_noun pen; // (list @) of pending packet } u3_czar_info; +/* +|% ++$ lane-state + $: =lane + last-sent=@da + last-heard=@da + rtt=@ + rttvar=@ + == ++$ full-state (map ship peer-state) ++$ peer-state + $: [direct=lane-state indirect=lane-state] + galaxy=@p + backpointer :: for callbacks + requests=(map [rift path] u3_pend_req) :: pend_req without lan_u + == +-- +*/ + /* _u3_mesa: next generation networking */ typedef struct _u3_mesa { From 6d0df92a13d300b11c130f5158e067c74cdb846d Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Tue, 2 Jul 2024 17:04:24 -0400 Subject: [PATCH 197/430] mesa: WIP refactoring state --- pkg/vere/io/mesa.c | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 43b8652ba1..2919fc0821 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -151,22 +151,28 @@ typedef struct _u3_czar_info { /* |% +$ lane-state - $: =lane - last-sent=@da + $: last-sent=@da last-heard=@da rtt=@ rttvar=@ == +$ full-state (map ship peer-state) +$ peer-state - $: [direct=lane-state indirect=lane-state] - galaxy=@p + $: direct=[=lane lane-state] + indirect=[galaxy=@p lane-state] backpointer :: for callbacks requests=(map [rift path] u3_pend_req) :: pend_req without lan_u == -- */ +typedef struct _u3_lane_state { + c3_d sen_d; // last sent date + c3_d her_d; // last heard date + c3_d rtt_d; // round-trip time + c3_d rtv_d; // round-trip time variance +} u3_lane_state; + /* _u3_mesa: next generation networking */ typedef struct _u3_mesa { @@ -181,7 +187,7 @@ typedef struct _u3_mesa { c3_o for_o; // is forwarding ur_cue_test_t* tes_u; // cue-test handle u3_cue_xeno* sil_u; // cue handle - u3p(u3h_root) her_p; // (map ship + u3p(u3h_root) her_p; // (map ship u3_peer) u3p(u3h_root) pac_p; // packet cache u3p(u3h_root) lan_p; // lane cache u3_czar_info imp_u[256]; // galaxy information @@ -191,11 +197,12 @@ typedef struct _u3_mesa { } u3_mesa; typedef struct _u3_peer { - u3_peer_last las_u; // last check timestamps - u3_lane dir_u; // direct lane (if any) - u3_lane ind_u; // indirect lane (if any) - c3_s imp_s; // galaxy - u3_mesa* sam_u; // backpointer + u3_mesa* sam_u; // backpointer + u3_lane dan_u; // direct lane (nullable) + u3_lane_state dir_u; // direct lane state + c3_y imp_y; // galaxy @p + u3_lane_state ind_u; // indirect lane state + // TODO HAMT for (map [rift path] u3_pend_req) } u3_peer; typedef enum _u3_mesa_ctag { From b5b0a9e4d2fc59c3f095840288575c6aba9d79b6 Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Wed, 3 Jul 2024 00:17:02 -0400 Subject: [PATCH 198/430] mesa: WIP new driver state compiles --- pkg/vere/io/mesa.c | 401 +++++++++++++++++++++++---------------------- 1 file changed, 203 insertions(+), 198 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 2919fc0821..5f331986a9 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -99,12 +99,6 @@ typedef struct _u3_pact_stat { struct _u3_mesa; -typedef struct _u3_peer_last { - c3_d her_d; // time we last heard from the direct route - c3_d dir_d; // time we last sent a packet on the direct route - c3_d ind_d; // time we last sent a packet on the indirect route -} u3_peer_last; - typedef struct _u3_misord_buf { c3_y* fra_y; c3_w len_w; @@ -136,7 +130,9 @@ typedef struct _u3_pend_req { u3_mesa_pict* pic_u; // preallocated request packet u3_pact_stat* wat_u; // ((mop @ud packet-state) lte) u3_bitset was_u; // ((mop @ud ?) lte) - c3_y pad_y[64]; + c3_y pad_y[64]; + // stats TODO: use + c3_d beg_d; // date when request began } u3_pend_req; typedef struct _u3_czar_info { @@ -148,29 +144,11 @@ typedef struct _u3_czar_info { u3_noun pen; // (list @) of pending packet } u3_czar_info; -/* -|% -+$ lane-state - $: last-sent=@da - last-heard=@da - rtt=@ - rttvar=@ - == -+$ full-state (map ship peer-state) -+$ peer-state - $: direct=[=lane lane-state] - indirect=[galaxy=@p lane-state] - backpointer :: for callbacks - requests=(map [rift path] u3_pend_req) :: pend_req without lan_u - == --- -*/ - typedef struct _u3_lane_state { c3_d sen_d; // last sent date c3_d her_d; // last heard date - c3_d rtt_d; // round-trip time - c3_d rtv_d; // round-trip time variance + c3_w rtt_w; // round-trip time + c3_w rtv_w; // round-trip time variance } u3_lane_state; /* _u3_mesa: next generation networking @@ -182,27 +160,27 @@ typedef struct _u3_mesa { uv_udp_t wax_u; uv_handle_t had_u; }; - u3_mesa_stat sat_u; // statistics - c3_l sev_l; // XX: ?? - c3_o for_o; // is forwarding - ur_cue_test_t* tes_u; // cue-test handle - u3_cue_xeno* sil_u; // cue handle - u3p(u3h_root) her_p; // (map ship u3_peer) - u3p(u3h_root) pac_p; // packet cache - u3p(u3h_root) lan_p; // lane cache - u3_czar_info imp_u[256]; // galaxy information - u3p(u3h_root) req_p; // hashtable of u3_pend_req - c3_c* dns_c; // turf (urb.otrg) - c3_d tim_d; // XX: remove + u3_mesa_stat sat_u; // statistics + c3_l sev_l; // XX: ?? + c3_o for_o; // is forwarding + ur_cue_test_t* tes_u; // cue-test handle + u3_cue_xeno* sil_u; // cue handle + u3p(u3h_root) her_p; // (map ship u3_peer) + u3p(u3h_root) pac_p; // packet cache + u3p(u3h_root) lan_p; // lane cache + u3_czar_info imp_u[256]; // galaxy information + c3_c* dns_c; // turf (urb.otrg) + c3_d tim_d; // XX: remove } u3_mesa; typedef struct _u3_peer { - u3_mesa* sam_u; // backpointer - u3_lane dan_u; // direct lane (nullable) - u3_lane_state dir_u; // direct lane state - c3_y imp_y; // galaxy @p - u3_lane_state ind_u; // indirect lane state - // TODO HAMT for (map [rift path] u3_pend_req) + u3_mesa* sam_u; // backpointer + c3_o ful_o; // has this been initialized? + u3_lane dan_u; // direct lane (nullable) + u3_lane_state dir_u; // direct lane state + c3_y imp_y; // galaxy @p + u3_lane_state ind_u; // indirect lane state + u3p(u3h_root) req_p; // (map [rift path] u3_pend_req) } u3_peer; typedef enum _u3_mesa_ctag { @@ -281,10 +259,8 @@ static void _log_peer(u3_peer* per_u) return; } u3l_log("dir"); - _log_lane(&per_u->dir_u); - u3l_log("ind"); - _log_lane(&per_u->ind_u); - u3l_log("galaxy: %s", u3r_string(u3dc("scot", 'p', per_u->imp_s))); + _log_lane(&per_u->dan_u); + u3l_log("galaxy: %s", u3r_string(u3dc("scot", 'p', per_u->imp_y))); } static void @@ -456,7 +432,7 @@ _dire_etch_ud(c3_d num_d) u3_noun _mesa_request_key(u3_mesa_name* nam_u) { u3_noun pax = _mesa_encode_path(nam_u->pat_s, (c3_y*)nam_u->pat_c); - u3_noun res = u3nt(u3i_chubs(2, nam_u->her_d), u3i_word(nam_u->rif_w), pax); + u3_noun res = u3nc(u3i_word(nam_u->rif_w), pax); return res; } @@ -486,53 +462,55 @@ _mesa_lane_key(c3_d her_d[2], u3_lane* lan_u) return u3nc(u3i_chubs(2,her_d), u3_mesa_encode_lane(*lan_u)); } -/* RETAIN -*/ -static u3_gage* -_mesa_get_lane_raw(u3_mesa* sam_u, u3_noun key) +// TODO: all the her_p hashtable functions are not refcounted properly +static u3_peer* +_mesa_get_peer_raw(u3_mesa* sam_u, u3_noun her) { - u3_gage* ret_u = NULL; - u3_weak res = u3h_git(sam_u->lan_p, key); + u3_peer* ret_u = NULL; + u3_weak res = u3h_git(sam_u->her_p, her); if ( res != u3_none && res != u3_nul ) { - ret_u = u3to(u3_gage, res); + ret_u = u3to(u3_peer, res); } + u3z(her); return ret_u; } -/* _mesa_get_lane(): get lane -*/ -static u3_gage* -_mesa_get_lane(u3_mesa* sam_u, c3_d her_d[2], u3_lane* lan_u) { - u3_noun key =_mesa_lane_key(her_d, lan_u); - u3_gage* ret_u = _mesa_get_lane_raw(sam_u, key); - u3z(key); - return ret_u; +/* + * RETAIN + */ +static u3_peer* +_mesa_get_peer(u3_mesa* sam_u, c3_d her_d[2]) +{ + return _mesa_get_peer_raw(sam_u, u3i_chubs(2, her_d)); } -/* _mesa_put_lane(): put lane state in state - * - * uses same copying trick as _mesa_put_request() -*/ +/* + */ static void -_mesa_put_lane(u3_mesa* sam_u, c3_d her_d[2], u3_lane* lan_u, u3_gage* gag_u) +_mesa_put_peer_raw(u3_mesa* sam_u, u3_noun her, u3_peer* per_u) { - u3_noun key = _mesa_lane_key(her_d, lan_u); - u3_gage* old_u = _mesa_get_lane_raw(sam_u, key); - u3_gage* new_u = gag_u; + u3_peer* old_u = _mesa_get_peer_raw(sam_u, u3k(her)); + u3_peer* new_u = NULL; if ( old_u == NULL ) { - new_u = u3a_calloc(sizeof(u3_gage),1); - memcpy(new_u, gag_u, sizeof(u3_gage)); - } else { + new_u = u3a_calloc(sizeof(u3_peer),1); + memcpy(new_u, per_u, sizeof(u3_peer)); + } else if ( new_u != old_u ) { new_u = old_u; - memcpy(new_u, gag_u, sizeof(u3_gage)); + memcpy(new_u, per_u, sizeof(u3_peer)); } - u3_noun val = u3of(u3_gage, new_u); - u3h_put(sam_u->lan_p, key, val); - u3z(key); + u3_noun val = u3of(u3_peer, new_u); + u3h_put(sam_u->her_p, her, val); + u3z(her); +} + +static void +_mesa_put_peer(u3_mesa* sam_u, c3_d her_d[2], u3_peer* per_u) +{ + _mesa_put_peer_raw(sam_u, u3i_chubs(2, her_d), per_u); } /* _mesa_get_request(): produce pending request state for nam_u @@ -542,9 +520,14 @@ _mesa_put_lane(u3_mesa* sam_u, c3_d her_d[2], u3_lane* lan_u, u3_gage* gag_u) static u3_pend_req* _mesa_get_request(u3_mesa* sam_u, u3_mesa_name* nam_u) { u3_pend_req* ret_u = NULL; + + u3_peer* per_u = _mesa_get_peer(sam_u, nam_u->her_d); + if ( !per_u ) { + return ret_u; + } u3_noun key = _mesa_request_key(nam_u); + u3_weak res = u3h_git(per_u->req_p, key); - u3_weak res = u3h_git(sam_u->req_p, key); if ( res != u3_none && res != u3_nul ) { ret_u = u3to(u3_pend_req, res); } @@ -555,9 +538,13 @@ _mesa_get_request(u3_mesa* sam_u, u3_mesa_name* nam_u) { static void _mesa_del_request(u3_mesa* sam_u, u3_mesa_name* nam_u) { + u3_peer* per_u = _mesa_get_peer(sam_u, nam_u->her_d); + if ( !per_u ) { + return; + } u3_noun key = _mesa_request_key(nam_u); - u3_weak req = u3h_get(sam_u->req_p, key); + u3_weak req = u3h_get(per_u->req_p, key); if ( req == u3_none ) { u3z(key); return; @@ -571,7 +558,7 @@ _mesa_del_request(u3_mesa* sam_u, u3_mesa_name* nam_u) { vec_free(&req_u->mis_u); c3_free(req_u->dat_y); lss_verifier_free(req_u->los_u); - u3h_del(sam_u->req_p, key); + u3h_del(per_u->req_p, key); u3a_free(req_u); u3z(key); } @@ -585,10 +572,14 @@ _mesa_del_request(u3_mesa* sam_u, u3_mesa_name* nam_u) { */ static u3_pend_req* _mesa_put_request(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_pend_req* req_u) { + u3_peer* per_u = _mesa_get_peer(sam_u, nam_u->her_d); + if ( !per_u ) { + return NULL; + } u3_noun key = _mesa_request_key(nam_u); if ( req_u == NULL) { - u3h_put(sam_u->req_p, key, u3_nul); + u3h_put(per_u->req_p, key, u3_nul); u3z(key); return req_u; } @@ -604,13 +595,114 @@ _mesa_put_request(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_pend_req* req_u) { memcpy(new_u, req_u, sizeof(u3_pend_req)); } - u3_noun val = u3of(u3_pend_req, new_u); - u3h_put(sam_u->req_p, key, val); + u3h_put(per_u->req_p, key, val); u3z(key); return new_u; } +/* _ames_czar_port(): udp port for galaxy. + XX copied from io/ames.c +*/ +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; + } +} + +static u3_lane +_mesa_get_direct_lane_raw(u3_mesa* sam_u, u3_noun her) +{ + if ( (c3y == u3a_is_cat(her)) && (her < 256) ) { + c3_s por_s = _ames_czar_port(her); + return (u3_lane){sam_u->imp_u[her].pip_w, por_s}; + } + u3_peer* per_u = _mesa_get_peer_raw(sam_u, her); + if ( !per_u ) { + return (u3_lane){0,0}; + } + return per_u->dan_u; +} + +static u3_lane +_mesa_get_direct_lane(u3_mesa* sam_u, c3_d her_d[2]) +{ + return _mesa_get_direct_lane_raw(sam_u, u3i_chubs(2, her_d)); +} + +static u3_lane +_mesa_get_czar_lane(u3_mesa* sam_u, c3_y imp_y) +{ + c3_s por_s = _ames_czar_port(imp_y); + return (u3_lane){sam_u->imp_u[imp_y].pip_w, por_s}; +} + +static u3_lane +_mesa_get_indirect_lane(u3_mesa* sam_u, u3_noun her, u3_noun lan) +{ + if ( (c3y == u3a_is_cat(her)) && (her < 256) ) { + return _mesa_get_czar_lane(sam_u, her); + } + u3_peer* per_u = _mesa_get_peer_raw(sam_u, her); + if ( !per_u ) { + return (u3_lane){0,0}; + } + return _mesa_get_czar_lane(sam_u, per_u->imp_y); +} + +/* RETAIN +*/ +static u3_gage* +_mesa_get_lane_raw(u3_mesa* sam_u, u3_noun key) +{ + u3_gage* ret_u = NULL; + u3_weak res = u3h_git(sam_u->lan_p, key); + + if ( res != u3_none && res != u3_nul ) { + ret_u = u3to(u3_gage, res); + } + + return ret_u; +} + +/* _mesa_get_lane(): get lane +*/ +static u3_gage* +_mesa_get_lane(u3_mesa* sam_u, c3_d her_d[2], u3_lane* lan_u) { + u3_noun key =_mesa_lane_key(her_d, lan_u); + u3_gage* ret_u = _mesa_get_lane_raw(sam_u, key); + u3z(key); + return ret_u; +} + +/* _mesa_put_lane(): put lane state in state + * + * uses same copying trick as _mesa_put_request() +*/ +static void +_mesa_put_lane(u3_mesa* sam_u, c3_d her_d[2], u3_lane* lan_u, u3_gage* gag_u) +{ + u3_noun key = _mesa_lane_key(her_d, lan_u); + u3_gage* old_u = _mesa_get_lane_raw(sam_u, key); + u3_gage* new_u = gag_u; + + if ( old_u == NULL ) { + new_u = u3a_calloc(sizeof(u3_gage),1); + memcpy(new_u, gag_u, sizeof(u3_gage)); + } else { + new_u = old_u; + memcpy(new_u, gag_u, sizeof(u3_gage)); + } + + u3_noun val = u3of(u3_gage, new_u); + u3h_put(sam_u->lan_p, key, val); + u3z(key); +} // congestion control update static void _mesa_handle_ack(u3_gage* gag_u, u3_pact_stat* pat_u) { @@ -712,19 +804,6 @@ _mesa_req_pact_sent(u3_pend_req* req_u, u3_mesa_name* nam_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_alloc(): libuv buffer allocator. */ static void @@ -1367,8 +1446,8 @@ _mesa_exit_cb(uv_handle_t* had_u) u3s_cue_xeno_done(sam_u->sil_u); ur_cue_test_done(sam_u->tes_u); + // TODO free each hashtable in sam_u->her_p first u3h_free(sam_u->her_p); - u3h_free(sam_u->req_p); c3_free(sam_u); } @@ -1380,101 +1459,27 @@ _mesa_io_exit(u3_auto* car_u) uv_close(&sam_u->had_u, _mesa_exit_cb); } -static void -_init_peer(u3_mesa* sam_u, u3_peer* per_u) +static u3_lane_state +_init_lane_state() { - per_u->sam_u = sam_u; - - per_u->imp_s = 256; - per_u->dir_u = (u3_lane){0,0}; - per_u->ind_u = (u3_lane){0,0}; - per_u->las_u = (u3_peer_last){0,0}; + u3_lane_state sat_u; + sat_u.sen_d = 0; + sat_u.her_d = 0; + sat_u.rtt_w = 1000000; + sat_u.rtv_w = 1000000; + return sat_u; } -// TODO: all the her_p hashtable functions are not refcounted properly -static u3_peer* -_mesa_get_peer_raw(u3_mesa* sam_u, u3_noun her) -{ - u3_peer* ret_u = NULL; - u3_weak res = u3h_git(sam_u->her_p, her); - - if ( res != u3_none && res != u3_nul ) { - ret_u = u3to(u3_peer, res); - } - - u3z(her); - return ret_u; - -} - -/* - * RETAIN - */ -static u3_peer* -_mesa_get_peer(u3_mesa* sam_u, c3_d her_d[2]) -{ - return _mesa_get_peer_raw(sam_u, u3i_chubs(2, her_d)); -} - -/* - */ static void -_mesa_put_peer_raw(u3_mesa* sam_u, u3_noun her, u3_peer* per_u) -{ - u3_peer* old_u = _mesa_get_peer_raw(sam_u, u3k(her)); - u3_peer* new_u = NULL; - - if ( old_u == NULL ) { - new_u = u3a_calloc(sizeof(u3_peer),1); - memcpy(new_u, per_u, sizeof(u3_peer)); - } else if ( new_u != old_u ) { - new_u = old_u; - memcpy(new_u, per_u, sizeof(u3_peer)); - } - - u3_noun val = u3of(u3_peer, new_u); - u3h_put(sam_u->her_p, her, val); - u3z(her); -} - -static void -_mesa_put_peer(u3_mesa* sam_u, c3_d her_d[2], u3_peer* per_u) -{ - _mesa_put_peer_raw(sam_u, u3i_chubs(2, her_d), per_u); -} - -static u3_lane -_mesa_get_direct_lane_raw(u3_mesa* sam_u, u3_noun her) -{ - if ( c3y == u3a_is_cat(her) && her < 256 ) { - c3_s por_s = _ames_czar_port(her); - return (u3_lane){sam_u->imp_u[her].pip_w, por_s}; - } - u3_peer* per_u = _mesa_get_peer_raw(sam_u, her); - if ( NULL == per_u ) { - return (u3_lane){0,0}; - } - return per_u->dir_u; -} - -static u3_lane -_mesa_get_direct_lane(u3_mesa* sam_u, c3_d her_d[2]) -{ - return _mesa_get_direct_lane_raw(sam_u, u3i_chubs(2, her_d)); -} - -static u3_lane -_mesa_get_indirect_lane(u3_mesa* sam_u, u3_noun her, u3_noun lan) +_init_peer(u3_mesa* sam_u, u3_peer* per_u) { - if ( c3y == u3a_is_cat(her) && her < 256 ) { - c3_s por_s = _ames_czar_port(her); - return (u3_lane){sam_u->imp_u[her].pip_w, por_s}; - } - u3_peer* per_u = _mesa_get_peer_raw(sam_u, her); - if ( NULL == per_u ) { - return (u3_lane){0,0}; - } - return per_u->ind_u; + per_u->sam_u = sam_u; + per_u->ful_o = c3n; + per_u->dan_u = (u3_lane){0,0}; + per_u->dir_u = _init_lane_state(); + per_u->imp_y = 0; + per_u->ind_u = _init_lane_state(); + per_u->req_p = u3h_new(); } /* @@ -1807,7 +1812,8 @@ _saxo_cb(void* vod_p, u3_noun nun) u3_noun gal = u3do("rear", u3k(sax)); u3_assert( c3y == u3a_is_cat(gal) && gal < 256 ); // both atoms guaranteed to be cats, bc we don't call unless forwarding - per_u->imp_s = gal; + per_u->ful_o = c3y; + per_u->imp_y = gal; _mesa_put_peer_raw(per_u->sam_u, her, per_u); } @@ -1827,10 +1833,10 @@ static void _hear_peer(u3_mesa* sam_u, u3_peer* per_u, u3_lane lan_u, c3_o dir_o) { if ( c3y == dir_o ) { - per_u->dir_u = lan_u; - per_u->las_u.her_d = _get_now_micros(); + per_u->dan_u = lan_u; + per_u->dir_u.her_d = _get_now_micros(); } else { - per_u->ind_u = lan_u; + per_u->ind_u.her_d = _get_now_micros(); } } @@ -2233,7 +2239,7 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) _mesa_free_pict(pic_u); return; } - if ( c3y == sam_u->for_o && sam_u->pir_u->who_d[0] == per_u->imp_s ) { + if ( c3y == sam_u->for_o && sam_u->pir_u->who_d[0] == per_u->imp_y ) { //#ifdef MESA_DEBUG u3_lane lin_u = _mesa_get_direct_lane(sam_u, her_d); //#endif @@ -2347,12 +2353,12 @@ _mesa_hear_poke(u3_mesa_pict* pic_u, u3_lane* lan_u) _mesa_free_pict(pic_u); return; } - if ( c3y == sam_u->for_o && sam_u->pir_u->who_d[0] == per_u->imp_s ) { + if ( c3y == sam_u->for_o && sam_u->pir_u->who_d[0] == per_u->imp_y ) { //#ifdef MESA_DEBUG u3_lane lin_u = _mesa_get_direct_lane(sam_u, her_d); //#endif - //_update_hopcount(&pac_u->hed_u); u3l_log("sending peek %u", pac_u->pek_u.nam_u.fra_w); + //_update_hopcount(&pac_u->hed_u); _mesa_send(pic_u, &lin_u); } _mesa_free_pict(pic_u); @@ -2599,7 +2605,6 @@ u3_mesa_io_init(u3_pier* pir_u) // XX config sam_u->her_p = u3h_new_cache(100000); - sam_u->req_p = u3h_new_cache(100000); sam_u->lan_p = u3h_new_cache(100000); sam_u->pac_p = u3h_new_cache(100000); From 5d28cabd4cd974a585c605de7bbaa593aacbdc3e Mon Sep 17 00:00:00 2001 From: pkova Date: Wed, 3 Jul 2024 16:10:40 +0300 Subject: [PATCH 199/430] mesa: more jumbo frame wip --- pkg/vere/io/mesa.c | 56 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 12 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index dad751862d..9059a2969d 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1741,11 +1741,11 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun nun) u3_noun tag; u3_noun dat; u3x_cell(u3k(old), &tag, &dat); - c3_w siz = u3r_met(13, hit); - c3_y* buf_y; + c3_y* out_y; /* u3l_log("path %s", pac_u->pek_u.nam_u.pat_c); */ u3_mesa_pact tac_u; + u3_mesa_pact muna; c3_w len_w = u3r_met(3, hit); u3l_log("len_w %u", len_w); @@ -1753,32 +1753,63 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun nun) u3r_bytes(0, len_w, puf_y, hit); mesa_sift_pact(&tac_u, puf_y, len_w); + + u3l_log("jumbo frame"); _log_pact(&tac_u); - for (c3_w i=0; i < siz; i++) { + + u3_atom jumbo = u3i_bytes(len_w, tac_u.pag_u.dat_u.fra_y); + c3_w siz_w = u3r_met(13, jumbo); + + tac_u.pag_u.nam_u.boq_y = 13; + tac_u.pag_u.dat_u.tot_w = siz_w; + + /* c3_w off_w = 0; */ + + for (c3_w i=0; i < siz_w; i++) { // (cut 3 [wid 1] dat.byts)) - /* u3_atom lin = u3qc_cut(13, i, 1, hit); */ + u3_atom lin = u3qc_cut(13, i, 1, jumbo); + tac_u.pag_u.dat_u.len_w = u3r_met(3, lin); tac_u.pag_u.nam_u.fra_w = i; + tac_u.pag_u.dat_u.fra_y = c3_malloc(tac_u.pag_u.dat_u.len_w); + /* memcpy(jum_y + off_w, tac_u.pag_u.dat_u.fra_y, tac_u.pag_u.dat_u.len_w ); */ + /* off_w += tac_u.pag_u.dat_u.len_w; */ + + u3r_bytes(0, tac_u.pag_u.dat_u.len_w, tac_u.pag_u.dat_u.fra_y, lin); u3_noun pair = u3h(proof); if (pair == u3_nul) { tac_u.pag_u.dat_u.aup_u.len_y = 0; + tac_u.pag_u.dat_u.aum_u.typ_e = AUTH_NONE; } else { tac_u.pag_u.dat_u.aup_u.len_y = 2; - tac_u.pag_u.dat_u.aup_u. + tac_u.pag_u.dat_u.aum_u.typ_e = AUTH_NEXT; + u3r_bytes(0, 32, tac_u.pag_u.dat_u.aup_u.has_y[0], u3h(u3t(pair))); + u3r_bytes(0, 32, tac_u.pag_u.dat_u.aup_u.has_y[1], u3t(u3t(pair))); } - c3_y* buf_y[PACT_SIZE]; + u3l_log("i %u", i); + /* _log_pact(&tac_u); */ + _log_buf(tac_u.pag_u.dat_u.fra_y, tac_u.pag_u.dat_u.len_w); + + c3_y buf_y[PACT_SIZE]; c3_w len_w = mesa_etch_pact(buf_y, &tac_u); - pac_u->pek_u.nam_u.fra_w = i; + u3_atom lan = u3i_bytes(len_w, buf_y); - _mesa_put_cache(sam_u, &pac_u->pek_u.nam_u, u3nc(MESA_ITEM, u3k(lin))); + _mesa_put_cache(sam_u, &tac_u.pag_u.nam_u, u3nc(MESA_ITEM, u3k(lan))); - c3_w lun_w = _mesa_respond(pic_u, &buf_y, u3k(lin)); - _mesa_rout_bufs(sam_u, buf_y, lun_w, u3k(u3t(dat))); + c3_w lun_w = _mesa_respond(pic_u, &out_y, u3k(lan)); + + u3l_log("out_y"); + mesa_sift_pact(&muna, out_y, lun_w); + + _log_buf(out_y, tac_u.pag_u.dat_u.len_w); + + _mesa_rout_bufs(sam_u, out_y, lun_w, u3k(u3t(dat))); + proof = u3t(proof); /* u3l_log("putting %u", pac_u->pek_u.nam_u.fra_w); */ /* _log_pact(pac_u); */ @@ -1993,8 +2024,9 @@ static void _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) { #ifdef MESA_DEBUG - /* u3l_log("mesa hear page %u", pic_u->pac_u.pag_u.nam_u.fra_w); */ + u3l_log("mesa hear page %u", pic_u->pac_u.pag_u.nam_u.fra_w); #endif + _log_pact(&pic_u->pac_u); u3_mesa* sam_u = pic_u->sam_u; u3_mesa_pact* pac_u = &pic_u->pac_u; c3_s fra_s; @@ -2261,7 +2293,7 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) pac_u->pek_u.nam_u.fra_w = bat_w; /* _log_pact(pac_u); */ - /* u3l_log("_mesa_hear_peek %s", pac_u->pek_u.nam_u.pat_c); */ + u3l_log("_mesa_hear_peek %s", pac_u->pek_u.nam_u.pat_c); u3_weak hit = _mesa_get_cache(sam_u, &pac_u->pek_u.nam_u); /* u3l_log("peek fra %u hit %u", fra_w, hit != u3_none); */ From b0e3f3f0b492aa28bb62dd034720f83a6728bb58 Mon Sep 17 00:00:00 2001 From: midden-fabler Date: Wed, 3 Jul 2024 09:50:44 -0400 Subject: [PATCH 200/430] http: return 200, 206, clean up range header edge cases --- pkg/vere/io/http.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/pkg/vere/io/http.c b/pkg/vere/io/http.c index 7b5d27590d..f4dbb4182e 100644 --- a/pkg/vere/io/http.c +++ b/pkg/vere/io/http.c @@ -697,9 +697,10 @@ _find_tis_fas(void* txt, c3_w len) // [x] u3qc_cut // [x] better range header parsing // [x] better slicing -// [ ] 206 -// [ ] 200 -// [ ] _http_range_respond? +// [x] 206 +// [x] 200 +// [x] open range vs error +// [ ] test stream // [ ] multipart ranges // typedef struct _range_header { @@ -763,6 +764,12 @@ _get_range(c3_c* txt_c, c3_w len_w) if ( hep_c ) { slice.start_z = h2o_strtosize(txt_c, hep_c - txt_c); slice.end_z = h2o_strtosize(hep_c + 1, len_w - ((hep_c + 1) - txt_c)); + // strange -> [SIZE_MAX SIZE_MAX] so we return u3_nul in _slice_mime + if ( ((hep_c != txt_c) && slice.start_z == SIZE_MAX ) || + ( len_w - ((hep_c + 1) - txt_c) > 0 ) && slice.end_z == SIZE_MAX ) { + slice.start_z = SIZE_MAX; + slice.end_z = SIZE_MAX; + } } return slice; @@ -937,8 +944,16 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) h2o_send_error_generic(req_u->rec_u, 416, msg_c, msg_c, 0); return; } - u3_noun res = u3i_edit(nac, 127, result); - _http_cache_respond(req_u, res); + if ( u3r_sing(result, octs) == c3y) { + // 200 + _http_cache_respond(req_u, nac); + } + else { + // 206 + u3_noun tmp = u3i_edit(nac, 124, 206); + u3_noun res = u3i_edit(tmp, 127, result); + _http_cache_respond(req_u, res); + } } } else { From 6e493629b05da1eba64926535509b0da312f6375 Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Wed, 3 Jul 2024 11:37:14 -0400 Subject: [PATCH 201/430] mesa: WIP doesn't compile, but working on making new state work --- pkg/vere/io/mesa.c | 59 ++++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 5f331986a9..e8270346da 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -85,10 +85,6 @@ typedef struct _u3_mesa_stat { #define safe_dec(num) (num == 0 ? num : num - 1) #define _mesa_met3_w(a_w) ((c3_bits_word(a_w) + 0x7) >> 3) - -/** typedef u3_mesa_pack_stat u3_noun - * [last=@da tries=@ud] - */ struct _u3_mesa_pact; typedef struct _u3_pact_stat { @@ -114,27 +110,6 @@ typedef struct _u3_mesa_pict { u3_mesa_pact pac_u; } u3_mesa_pict; -typedef struct _u3_pend_req { - c3_w nex_w; // number of the next fragment to be sent - c3_w tot_w; // total number of fragments expected - uv_timer_t tim_u; // timehandler - c3_y* dat_y; // ((mop @ud *) lte) - c3_w len_w; - c3_w lef_w; // lowest fragment number currently in flight/pending - c3_w old_w; // frag num of oldest packet sent - c3_w ack_w; // highest acked fragment number - u3_lane lan_u; // last lane heard - u3_gage* gag_u; // congestion control - u3_vec(u3_buf) mis_u; // misordered packets - lss_verifier* los_u; // Lockstep verifier - u3_mesa_pict* pic_u; // preallocated request packet - u3_pact_stat* wat_u; // ((mop @ud packet-state) lte) - u3_bitset was_u; // ((mop @ud ?) lte) - c3_y pad_y[64]; - // stats TODO: use - c3_d beg_d; // date when request began -} u3_pend_req; - typedef struct _u3_czar_info { c3_w pip_w; // IP of galaxy c3_y imp_y; // galaxy number @@ -183,6 +158,26 @@ typedef struct _u3_peer { u3p(u3h_root) req_p; // (map [rift path] u3_pend_req) } u3_peer; +typedef struct _u3_pend_req { + c3_w nex_w; // number of the next fragment to be sent + c3_w tot_w; // total number of fragments expected + uv_timer_t tim_u; // timehandler + c3_y* dat_y; // ((mop @ud *) lte) + c3_w len_w; + c3_w lef_w; // lowest fragment number currently in flight/pending + c3_w old_w; // frag num of oldest packet sent + c3_w ack_w; // highest acked fragment number + u3_gage* gag_u; // congestion control + u3_vec(u3_buf) mis_u; // misordered packets + lss_verifier* los_u; // Lockstep verifier + u3_mesa_pict* pic_u; // preallocated request packet + u3_pact_stat* wat_u; // ((mop @ud packet-state) lte) + u3_bitset was_u; // ((mop @ud ?) lte) + c3_y pad_y[64]; + // stats TODO: use + c3_d beg_d; // date when request began +} u3_pend_req; + typedef enum _u3_mesa_ctag { CACE_WAIT = 1, CACE_ITEM = 2, @@ -486,8 +481,6 @@ _mesa_get_peer(u3_mesa* sam_u, c3_d her_d[2]) return _mesa_get_peer_raw(sam_u, u3i_chubs(2, her_d)); } -/* - */ static void _mesa_put_peer_raw(u3_mesa* sam_u, u3_noun her, u3_peer* per_u) { @@ -949,6 +942,8 @@ _try_resend(u3_pend_req* req_u) u3_assert( 0 ); } // TODO: better route management + // it needs to be more ergonomic to access the u3_peer + // could do a backpointer to the u3_peer from the u3_pend_req _mesa_send_buf(req_u->pic_u->sam_u, req_u->lan_u, buf_y, siz_w); _mesa_req_pact_resent(req_u, &pac_u->pek_u.nam_u); } @@ -1070,6 +1065,7 @@ _mesa_req_pact_done(u3_mesa* sam_u, u3_mesa_name *nam_u, u3_mesa_data* dat_u, u3 _init_gage(gag_u); } + // TODO: fix this req_u->lan_u = *lan_u; c3_w siz_w = (1 << (nam_u->boq_y - 3)); @@ -1439,6 +1435,13 @@ _mesa_io_slog(u3_auto* car_u) { u3l_log("mesa is online"); } +static void +_mesa_free_peer(u3_noun per) +{ + u3_peer* per_u = u3to(u3_peer, per); + u3h_free(per_u->req_p); +} + static void _mesa_exit_cb(uv_handle_t* had_u) { @@ -1446,7 +1449,7 @@ _mesa_exit_cb(uv_handle_t* had_u) u3s_cue_xeno_done(sam_u->sil_u); ur_cue_test_done(sam_u->tes_u); - // TODO free each hashtable in sam_u->her_p first + u3h_walk(sam_u->her_p, _mesa_free_peer); u3h_free(sam_u->her_p); c3_free(sam_u); From e0919c1b1b89a7e2749bed1e442ccfdf16b6147e Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Wed, 3 Jul 2024 18:53:39 -0400 Subject: [PATCH 202/430] mesa: removed pluggable congestion control; compiles --- pkg/vere/io/cong.h | 34 ------------ pkg/vere/io/cubic.c | 129 -------------------------------------------- pkg/vere/io/cubic.h | 33 ------------ pkg/vere/io/mesa.c | 84 +++++++++++++++++++++-------- pkg/vere/io/reno.c | 0 5 files changed, 62 insertions(+), 218 deletions(-) delete mode 100644 pkg/vere/io/cong.h delete mode 100644 pkg/vere/io/cubic.c delete mode 100644 pkg/vere/io/cubic.h delete mode 100644 pkg/vere/io/reno.c diff --git a/pkg/vere/io/cong.h b/pkg/vere/io/cong.h deleted file mode 100644 index 0db5276c1e..0000000000 --- a/pkg/vere/io/cong.h +++ /dev/null @@ -1,34 +0,0 @@ -/// @file -/// -#include "vere.h" -#include -#include - -#include "noun.h" -#include "ur.h" - -typedef struct _u3_gage { - c3_w rtt_w; // rtt - c3_w rto_w; // rto - c3_w rtv_w; // rttvar - c3_w wnd_w; // cwnd - c3_w wnf_w; // cwnd fraction - c3_w sst_w; // ssthresh - c3_w con_w; // counter - void* alg_u; // algorithm backpointer - c3_c* alg_c; // algorithm name - // - uv_timer_t tim_u; -} u3_gage; - -typedef void (*u3_gage_func)(u3_gage*); - - -typedef struct _u3_gage_alg { - u3_gage_func int_f; //initialise - u3_gage_func don_f; // dispose - u3_gage_func ack_f; // received ack - u3_gage_func los_f; // lost -} u3_gage_alg; - - diff --git a/pkg/vere/io/cubic.c b/pkg/vere/io/cubic.c deleted file mode 100644 index 1f9eb33cfd..0000000000 --- a/pkg/vere/io/cubic.c +++ /dev/null @@ -1,129 +0,0 @@ -/// @file -/// -#include "vere.h" -#include -#include - -#include "noun.h" -#include "ur.h" -#include "cubic.h" - -#define FAST_CONVERGENCE 0 - -const c3_d cee_d = 13; // (0.4*32) -const c3_d bet_d = 3; // (.2*16) - - -static c3_d -_get_now_micros() -{ - struct timeval tim_u; - gettimeofday(&tim_u, NULL); - return (tim_u.tv_sec * 1000000) + tim_u.tv_usec; -} - -static c3_d -_cube(c3_d val_d) -{ - return val_d * val_d * val_d; - -} - -void u3_cbic_init(u3_gage* gag_u) -{ - gag_u->alg_u = c3_calloc(sizeof(u3_cbic)); - gag_u->alg_c = "CUBIC"; -} - -void u3_cbic_done(u3_gage* gag_u) -{ - free(gag_u->alg_u); -} - - -void u3_cbic_reset(u3_gage* gag_u) -{ - u3_cbic* cub_u = gag_u->alg_u; - cub_u->las_w = 0; - cub_u->poc_d = 0; - cub_u->rig_w = 0; - cub_u->del_w = 0; - cub_u->win_w = 0; - cub_u->cac_w = 0; -} - - -void u3_cbic_tcp(u3_gage* gag_u) -{ - u3_cbic* cub_u = gag_u->alg_u; - - /*sat_u.tcp_w = sat_u.tcp_w + ((c3_w)((3*bet_f)/(2-bet_f)) * (sat_u.cac_w/con_u.wnd_w)); - sat_u.cac_w = 0; - if ( sat_u.tcp_w > con_u.wnd_w ) { - sat_u.max_w = con_u.wnd_w/(sat_u.tcp_w-con_u.wnd_w); - if ( sat_u.cnt_w > sat_u.max_w ) { - sat_u.cnt_w = sat_u.max_w; - } - } */ -} - -void u3_cbic_update(u3_gage* gag_u) -{ - u3_cbic* cub_u = gag_u->alg_u; - cub_u->cac_w += 1; - c3_d tim_d = _get_now_micros(); - - // if no epoch start set, then begin now - if ( 0 == cub_u->poc_d ) { - cub_u->poc_d = tim_d; - if ( ( gag_u->wnd_w < cub_u->las_w ) && FAST_CONVERGENCE ) { - //sat_u.las_w = con_u.wnd_w * - // set origin point to w last max - } else { - cub_u->kay_w = 0; - cub_u->rig_w = gag_u->wnd_w; - } - cub_u->cac_w = 1; - cub_u->win_w = gag_u->wnd_w; - } - c3_d tee_d = tim_d + cub_u->del_w - cub_u->poc_d; - c3_d tar_d = cub_u->rig_w + ((cee_d * _cube(tee_d - cub_u->kay_w)) >> 5); - if ( tar_d > gag_u->wnd_w ) { - cub_u->cnt_w = gag_u->wnd_w / ( tar_d - gag_u->wnd_w ); - } else { - cub_u->cnt_w = gag_u->wnd_w * 100; - } - if ( c3y == cub_u->tcp_o ) { - u3_cbic_tcp(gag_u); - } -} - - -void u3_cbic_on_ack(u3_gage* gag_u) -{ - c3_w rtt_w = gag_u->rtt_w; // TODO actually update - u3_cbic* cub_u = gag_u->alg_u; - if ( 0 == cub_u->del_w ) { - cub_u->del_w = c3_min(cub_u->del_w, rtt_w); - } else { - cub_u->del_w = rtt_w; - } - - if ( gag_u->wnd_w <= gag_u->sst_w ) { - gag_u->wnd_w += 1; - } else { - if ( gag_u->wnf_w > gag_u->wnd_w ) { - gag_u->wnd_w += 1; - gag_u->wnf_w = 0; - } else { - gag_u->wnf_w += 1; - } - } -} - -void u3_cbic_lost(u3_gage* gag_u) -{ - -} - - diff --git a/pkg/vere/io/cubic.h b/pkg/vere/io/cubic.h deleted file mode 100644 index 1b06938ea1..0000000000 --- a/pkg/vere/io/cubic.h +++ /dev/null @@ -1,33 +0,0 @@ -/// @file -/// -#include "vere.h" -#include -#include - -#include "noun.h" -#include "ur.h" -#include "cong.h" - - -typedef struct _u3_cbic { - c3_w las_w; // w_last_max - c3_d poc_d; // epoch start - c3_w rig_w; // origin point - c3_w del_w; // delay min - c3_w win_w; // w_tcp - c3_w cac_w; // ack count - c3_w kay_w; // K - c3_w cnt_w; // cnt - c3_o tcp_o; // tcp friendliness - c3_w tcp_w; // w_tcp - c3_w max_w; // max count -} u3_cbic; - - -void u3_cbic_init(u3_gage*); - -void u3_cbic_done(u3_gage*); - -void u3_cbic_on_ack(u3_gage*); - -void u3_cbic_lost(u3_gage*); diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index e8270346da..cbaecff7a5 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -5,7 +5,6 @@ #include "noun.h" #include "ur.h" -#include "cubic.h" #include "mesa/mesa.h" #include "mesa/bitset.h" #include @@ -29,6 +28,8 @@ c3_o dop_o = c3n; #define DEF_TEXT "\033[0m" #define REORDER_THRESH 5 +#define DIRECT_ROUTE_TIMEOUT_MICROS 5000000 + // logging and debug symbols #define MESA_SYM_DESC(SYM) MESA_DESC_ ## SYM #define MESA_SYM_FIELD(SYM) MESA_FIELD_ ## SYM @@ -102,6 +103,18 @@ typedef struct _u3_misord_buf { lss_pair* par_u; } u3_misord_buf; +typedef struct _u3_gage { + c3_w rtt_w; // rtt + c3_w rto_w; // rto + c3_w rtv_w; // rttvar + c3_w wnd_w; // cwnd + c3_w wnf_w; // cwnd fraction + c3_w sst_w; // ssthresh + c3_w con_w; // counter + // + uv_timer_t tim_u; +} u3_gage; + struct _u3_mesa; typedef struct _u3_mesa_pict { @@ -159,6 +172,7 @@ typedef struct _u3_peer { } u3_peer; typedef struct _u3_pend_req { + u3_peer* per_u; // backpointer c3_w nex_w; // number of the next fragment to be sent c3_w tot_w; // total number of fragments expected uv_timer_t tim_u; // timehandler @@ -323,6 +337,13 @@ _clamp_rto(c3_d rto_d) { return c3_min(c3_max(rto_d, 200000), 25000000); } +static c3_o +_mesa_is_direct_mode(u3_peer* per_u) +{ + c3_d now_d = _get_now_micros(); + return __(per_u->dir_u.her_d + DIRECT_ROUTE_TIMEOUT_MICROS > now_d); +} + static inline void _get_her(u3_mesa_pact* pac_u, c3_d* our_d) { @@ -582,6 +603,7 @@ _mesa_put_request(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_pend_req* req_u) { new_u = u3a_calloc(1, sizeof(u3_pend_req)); /* u3l_log("putting fresh req %p", new_u); */ memcpy(new_u, req_u, sizeof(u3_pend_req)); + new_u->per_u = per_u; uv_timer_init(u3L, &new_u->tim_u); } else { new_u = old_u; @@ -699,7 +721,6 @@ _mesa_put_lane(u3_mesa* sam_u, c3_d her_d[2], u3_lane* lan_u, u3_gage* gag_u) // congestion control update static void _mesa_handle_ack(u3_gage* gag_u, u3_pact_stat* pat_u) { - gag_u->con_w++; c3_d now_d = _get_now_micros(); @@ -944,7 +965,7 @@ _try_resend(u3_pend_req* req_u) // TODO: better route management // it needs to be more ergonomic to access the u3_peer // could do a backpointer to the u3_peer from the u3_pend_req - _mesa_send_buf(req_u->pic_u->sam_u, req_u->lan_u, buf_y, siz_w); + _mesa_send_buf(req_u->pic_u->sam_u, req_u->per_u->dan_u, buf_y, siz_w); _mesa_req_pact_resent(req_u, &pac_u->pek_u.nam_u); } } @@ -960,7 +981,7 @@ static void _mesa_packet_timeout(uv_timer_t* tim_u); static void -_update_oldest_req(u3_pend_req *req_u, u3_gage* gag_u) +_update_oldest_req(u3_pend_req *req_u) { if( req_u->tot_w == 0 || req_u->len_w == req_u->tot_w ) { /* u3l_log("bad condition"); */ @@ -988,7 +1009,8 @@ _update_oldest_req(u3_pend_req *req_u, u3_gage* gag_u) req_u->tim_u.data = req_u; c3_d gap_d = req_u->wat_u[idx_w].sen_d - now_d; /* u3l_log("timeout %llu", (gag_u->rto_w - gap_d) / 1000); */ - uv_timer_start(&req_u->tim_u, _mesa_packet_timeout, (gag_u->rto_w - gap_d) / 1000, 0); + c3_w dur_w = (req_u->gag_u->rto_w - gap_d) / 1000; + uv_timer_start(&req_u->tim_u, _mesa_packet_timeout, dur_w, 0); } /* _mesa_packet_timeout(): callback for packet timeout @@ -998,7 +1020,7 @@ _mesa_packet_timeout(uv_timer_t* tim_u) { u3_pend_req* req_u = (u3_pend_req*)tim_u->data; /* u3l_log("%u packet timed out", req_u->old_w); */ _try_resend(req_u); - _update_oldest_req(req_u, req_u->gag_u); + _update_oldest_req(req_u); } static void _mesa_free_misord_buf(u3_misord_buf* buf_u) @@ -1044,7 +1066,11 @@ _mesa_burn_misorder_queue(u3_pend_req* req_u) /* _mesa_req_pact_done(): mark packet as done, returning if we should continue */ static u3_pend_req* -_mesa_req_pact_done(u3_mesa* sam_u, u3_mesa_name *nam_u, u3_mesa_data* dat_u, u3_lane* lan_u) +_mesa_req_pact_done(u3_mesa* sam_u, + u3_mesa_name* nam_u, + u3_mesa_data* dat_u, + c3_y hop_y, + u3_lane lan_u) { u3_weak ret = u3_none; c3_d now_d = _get_now_micros(); @@ -1055,18 +1081,20 @@ _mesa_req_pact_done(u3_mesa* sam_u, u3_mesa_name *nam_u, u3_mesa_data* dat_u, u3 return NULL; } - u3_gage* gag_u = _mesa_get_lane(sam_u, nam_u->her_d, lan_u); - req_u->gag_u = gag_u; - - // first we hear from lane - if ( gag_u == NULL ) { - gag_u = alloca(sizeof(u3_gage)); - memset(gag_u, 0, sizeof(u3_gage)); - _init_gage(gag_u); + // TODO: if hop count is nonzero, + // then write down the next hop as the candidate direct route. + // We also need to branch on that to figure out which + // congestion control state to update. + // TODO: when we change candidate direct routes, zero out the + // direct-route congestion control state. + u3_lane_state sat_u; + if ( 0 == hop_y ) { + req_u->per_u->dan_u = lan_u; + sat_u = req_u->per_u->dir_u; + } + else { + sat_u = req_u->per_u->ind_u; } - - // TODO: fix this - req_u->lan_u = *lan_u; c3_w siz_w = (1 << (nam_u->boq_y - 3)); // First packet received, instantiate request fully @@ -1144,14 +1172,14 @@ _mesa_req_pact_done(u3_mesa* sam_u, u3_mesa_name *nam_u, u3_mesa_data* dat_u, u3 } // handle gauge update - _mesa_handle_ack(gag_u, &req_u->wat_u[nam_u->fra_w]); + _mesa_handle_ack(req_u->gag_u, &req_u->wat_u[nam_u->fra_w]); memcpy(req_u->dat_y + (siz_w * nam_u->fra_w), dat_u->fra_y, dat_u->len_w); _try_resend(req_u); - _update_oldest_req(req_u, gag_u); + _update_oldest_req(req_u); // _mesa_put_request(sam_u, nam_u, req_u); // _mesa_put_lane(sam_u, nam_u->her_d, lan_u, gag_u); @@ -1982,7 +2010,7 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) vec_init(&req_u->mis_u, 8); req_u = _mesa_put_request(sam_u, nam_u, req_u); - _update_oldest_req(req_u, gag_u); + _update_oldest_req(req_u); return req_u; } @@ -2116,7 +2144,19 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) return; } } else { - req_u = _mesa_req_pact_done(sam_u, &pac_u->pag_u.nam_u, &pac_u->pag_u.dat_u, &lan_u); + u3_lane lon_u; + if ( HOP_SHORT == pac_u->hed_u.nex_y ) { + lon_u.pip_w = *(c3_w*)pac_u->pag_u.sot_u; + lon_u.por_s = *(c3_s*)&pac_u->pag_u.sot_u[4]; + } + else { + lon_u = lan_u; + } + req_u = _mesa_req_pact_done(sam_u, + &pac_u->pag_u.nam_u, + &pac_u->pag_u.dat_u, + pac_u->hed_u.hop_y, + lon_u); if ( req_u == NULL ) { // cleanup /* u3l_log("wrong"); */ diff --git a/pkg/vere/io/reno.c b/pkg/vere/io/reno.c deleted file mode 100644 index e69de29bb2..0000000000 From b2faab5806245bebd688e0f5bcfcf54a36221ea7 Mon Sep 17 00:00:00 2001 From: pkova Date: Thu, 4 Jul 2024 15:49:44 +0300 Subject: [PATCH 203/430] mesa: jumbo frames work but lss_verifier_ingest fails --- pkg/vere/io/mesa.c | 55 +++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 9059a2969d..51c4172222 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -302,8 +302,7 @@ _log_mesa_data(u3_mesa_data dat_u) static inline c3_w _mesa_lop(c3_w fra_w) { - c3_w res_w = ((( fra_w ) / MESA_HUNK) * MESA_HUNK); - return res_w; + return 0; } static c3_d @@ -1048,19 +1047,19 @@ _mesa_req_pact_done(u3_mesa* sam_u, u3_mesa_name *nam_u, u3_mesa_data* dat_u, u3 buf_u->num_w = nam_u->fra_w; vec_append(&req_u->mis_u, buf_u); } - else if ( c3y != (ver_y = lss_verifier_ingest(req_u->los_u, buf_y, len_w, par_u)) ) { - c3_free(par_u); - // TODO: do we drop the whole request on the floor? - u3l_log("auth fail frag %u", nam_u->fra_w); - MESA_LOG(AUTH); - return req_u; - } - else if ( vec_len(&req_u->mis_u) != 0 - && c3y != (ver_y = _mesa_burn_misorder_queue(req_u))) { - c3_free(par_u); - MESA_LOG(AUTH) - return req_u; - } + /* else if ( c3y != (ver_y = lss_verifier_ingest(req_u->los_u, buf_y, len_w, par_u)) ) { */ + /* c3_free(par_u); */ + /* // TODO: do we drop the whole request on the floor? */ + /* u3l_log("auth fail frag %u", nam_u->fra_w); */ + /* MESA_LOG(AUTH); */ + /* return req_u; */ + /* } */ + /* else if ( vec_len(&req_u->mis_u) != 0 */ + /* && c3y != (ver_y = _mesa_burn_misorder_queue(req_u))) { */ + /* c3_free(par_u); */ + /* MESA_LOG(AUTH) */ + /* return req_u; */ + /* } */ else { c3_free(par_u); } @@ -1725,6 +1724,7 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun nun) u3_weak hit = u3r_at(7, nun); u3_noun proof = u3h(hit); + /* u3m_p("proof", proof); */ hit = u3t(hit); if ( u3_none == hit ) { @@ -1754,8 +1754,8 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun nun) mesa_sift_pact(&tac_u, puf_y, len_w); - u3l_log("jumbo frame"); - _log_pact(&tac_u); + /* u3l_log("jumbo frame"); */ + /* _log_pact(&tac_u); */ u3_atom jumbo = u3i_bytes(len_w, tac_u.pag_u.dat_u.fra_y); @@ -1789,9 +1789,9 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun nun) u3r_bytes(0, 32, tac_u.pag_u.dat_u.aup_u.has_y[1], u3t(u3t(pair))); } - u3l_log("i %u", i); + /* u3l_log("i %u", i); */ /* _log_pact(&tac_u); */ - _log_buf(tac_u.pag_u.dat_u.fra_y, tac_u.pag_u.dat_u.len_w); + /* _log_buf(tac_u.pag_u.dat_u.fra_y, tac_u.pag_u.dat_u.len_w); */ c3_y buf_y[PACT_SIZE]; @@ -1801,14 +1801,14 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun nun) _mesa_put_cache(sam_u, &tac_u.pag_u.nam_u, u3nc(MESA_ITEM, u3k(lan))); - c3_w lun_w = _mesa_respond(pic_u, &out_y, u3k(lan)); + /* c3_w lun_w = _mesa_respond(pic_u, &out_y, u3k(lan)); */ - u3l_log("out_y"); - mesa_sift_pact(&muna, out_y, lun_w); + /* u3l_log("out_y"); */ + /* mesa_sift_pact(&muna, out_y, lun_w); */ - _log_buf(out_y, tac_u.pag_u.dat_u.len_w); + /* _log_buf(out_y, tac_u.pag_u.dat_u.len_w); */ - _mesa_rout_bufs(sam_u, out_y, lun_w, u3k(u3t(dat))); + _mesa_rout_bufs(sam_u, buf_y, len_w, u3k(u3t(dat))); proof = u3t(proof); /* u3l_log("putting %u", pac_u->pek_u.nam_u.fra_w); */ @@ -2024,9 +2024,10 @@ static void _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) { #ifdef MESA_DEBUG - u3l_log("mesa hear page %u", pic_u->pac_u.pag_u.nam_u.fra_w); + /* u3l_log("mesa hear page %u", pic_u->pac_u.pag_u.nam_u.fra_w); */ #endif - _log_pact(&pic_u->pac_u); + + /* _log_buf(pic_u->pac_u.pag_u.dat_u.fra_y, pic_u->pac_u.pag_u.dat_u.len_w); */ u3_mesa* sam_u = pic_u->sam_u; u3_mesa_pact* pac_u = &pic_u->pac_u; c3_s fra_s; @@ -2293,7 +2294,7 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) pac_u->pek_u.nam_u.fra_w = bat_w; /* _log_pact(pac_u); */ - u3l_log("_mesa_hear_peek %s", pac_u->pek_u.nam_u.pat_c); + /* u3l_log("_mesa_hear_peek %s", pac_u->pek_u.nam_u.pat_c); */ u3_weak hit = _mesa_get_cache(sam_u, &pac_u->pek_u.nam_u); /* u3l_log("peek fra %u hit %u", fra_w, hit != u3_none); */ From 6a15cbf7170fae9a0ca107040399ae52329d1d47 Mon Sep 17 00:00:00 2001 From: pkova Date: Thu, 4 Jul 2024 19:28:24 +0300 Subject: [PATCH 204/430] mesa: jumbo frames work, verifier not yet --- pkg/vere/io/mesa.c | 43 ++++++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 51c4172222..593488bb6a 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1538,7 +1538,7 @@ _name_to_batch_scry(u3_mesa_name* nam_u, c3_w lop_w, c3_w len_w) } static u3_noun -_name_to_jumbo_scry(u3_mesa_name* nam_u, c3_w lop_w, c3_w len_w) +_name_to_jumbo_scry(u3_mesa_name* nam_u) { u3_noun rif = _dire_etch_ud(nam_u->rif_w); u3_noun boq = _dire_etch_ud(31); @@ -1555,8 +1555,22 @@ _name_to_jumbo_scry(u3_mesa_name* nam_u, c3_w lop_w, c3_w len_w) } /* - * RETAIN - */ + * RETAIN */ +static u3_weak +_mesa_get_jumbo_cache(u3_mesa* sam_u, u3_mesa_name* nam_u) +{ + u3_noun pax = _name_to_jumbo_scry(nam_u); + u3_weak res = u3h_get(sam_u->pac_p, pax); + if ( u3_none == res ) { + //u3m_p("miss", u3k(pax)); + } else { + //u3m_p("hit", u3nc(u3k(pax), u3k(res))); + } + return res; +} + +/* + * RETAIN */ static u3_weak _mesa_get_cache(u3_mesa* sam_u, u3_mesa_name* nam_u) { @@ -1578,6 +1592,14 @@ _mesa_put_cache(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_noun val) u3z(pax); // TODO: fix refcount } +static void +_mesa_put_jumbo_cache(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_noun val) +{ + u3_noun pax = _name_to_jumbo_scry(nam_u); + u3h_put(sam_u->pac_p, pax, u3k(val)); + u3z(pax); // TODO: fix refcount +} + static void _mesa_try_forward(u3_mesa_pict* pic_u, u3_noun fra, u3_noun hit) { @@ -1733,7 +1755,7 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun nun) u3l_log("unbound"); } else { c3_w fra_w = pac_u->pek_u.nam_u.fra_w; - u3_weak old = _mesa_get_cache(sam_u, &pac_u->pag_u.nam_u); + u3_weak old = _mesa_get_jumbo_cache(sam_u, &pac_u->pag_u.nam_u); if ( old == u3_none ) { u3l_log("bad"); MESA_LOG(APATHY); @@ -1754,6 +1776,7 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun nun) mesa_sift_pact(&tac_u, puf_y, len_w); + _mesa_put_jumbo_cache(sam_u, &tac_u.pag_u.nam_u, u3nc(MESA_ITEM, 0)); /* u3l_log("jumbo frame"); */ /* _log_pact(&tac_u); */ @@ -1789,7 +1812,6 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun nun) u3r_bytes(0, 32, tac_u.pag_u.dat_u.aup_u.has_y[1], u3t(u3t(pair))); } - /* u3l_log("i %u", i); */ /* _log_pact(&tac_u); */ /* _log_buf(tac_u.pag_u.dat_u.fra_y, tac_u.pag_u.dat_u.len_w); */ @@ -1808,6 +1830,7 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun nun) /* _log_buf(out_y, tac_u.pag_u.dat_u.len_w); */ + _mesa_rout_bufs(sam_u, buf_y, len_w, u3k(u3t(dat))); proof = u3t(proof); @@ -2253,7 +2276,7 @@ _mesa_add_lane_to_cache(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_noun las, u3_lan _mesa_get_now(), u3_mesa_encode_lane(lan_u), u3k(las)); - _mesa_put_cache(sam_u, nam_u, hit); + _mesa_put_jumbo_cache(sam_u, nam_u, hit); u3z(las); } @@ -2295,7 +2318,7 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) pac_u->pek_u.nam_u.fra_w = bat_w; /* _log_pact(pac_u); */ /* u3l_log("_mesa_hear_peek %s", pac_u->pek_u.nam_u.pat_c); */ - u3_weak hit = _mesa_get_cache(sam_u, &pac_u->pek_u.nam_u); + u3_weak hit = _mesa_get_jumbo_cache(sam_u, &pac_u->pek_u.nam_u); /* u3l_log("peek fra %u hit %u", fra_w, hit != u3_none); */ /* u3l_log("peek fra %u bat %u hit %u", fra_w, bat_w, hit != u3_none); */ @@ -2330,9 +2353,7 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) // bat_w, // bat_w + MESA_HUNK); - u3_noun sky = _name_to_jumbo_scry(&pac_u->pek_u.nam_u, - bat_w, - bat_w + MESA_HUNK); + u3_noun sky = _name_to_jumbo_scry(&pac_u->pek_u.nam_u); pac_u->pek_u.nam_u.fra_w = fra_w; @@ -2646,7 +2667,7 @@ u3_mesa_io_init(u3_pier* pir_u) sam_u->her_p = u3h_new_cache(100000); sam_u->req_p = u3h_new_cache(100000); sam_u->lan_p = u3h_new_cache(100000); - sam_u->pac_p = u3h_new_cache(100000); + sam_u->pac_p = u3h_new_cache(300000); u3_assert( !uv_udp_init(u3L, &sam_u->wax_u) ); sam_u->wax_u.data = sam_u; From f6471e0a42719ed13fe1ce3eb5aa4335e0bcac0e Mon Sep 17 00:00:00 2001 From: lukechampine Date: Wed, 3 Jul 2024 00:38:33 -0400 Subject: [PATCH 205/430] lss: explain bithacking --- pkg/vere/io/lss.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/pkg/vere/io/lss.c b/pkg/vere/io/lss.c index 1d5cf40073..ebd7e161d5 100644 --- a/pkg/vere/io/lss.c +++ b/pkg/vere/io/lss.c @@ -137,20 +137,33 @@ void lss_builder_free(lss_builder* bil_u) { c3_free(bil_u); } -static c3_o _lss_verifier_find_pair(lss_verifier* los_u, c3_w i, c3_w height, lss_hash h) +static c3_o _lss_verifier_check_hash(lss_verifier* los_u, c3_w i, c3_w height, lss_hash h) { + // Binary numeral trees are composed of a set of perfect binary trees of + // unique heights. Unless the set consists of a single tree, there will be + // pairs that span two trees. We call these pairs "odd" because the heights of + // their children are uneven. Specifically, the left child of an odd pair has + // a greater height than the right child. + // + // When such a pair is inserted by lss_verifier_ingest, both children are + // inserted at the same height. This means that the right child will be in the + // "wrong" place. The abstruse bithacking below corrects for this. First, it + // calculates the positions of the odd pairs within a tree of this size. Then + // it determines how many odd pairs are directly above us, and increments the + // height accordingly. A mask is used to ensure that we only perform this + // adjustment when necessary. c3_w odd = (1<<_bits_len64(los_u->leaves-1)) - los_u->leaves; c3_w mask = (1<<_bits_len64(i ^ (los_u->leaves-1))) - 1; height += _bits_ctz64(~((odd&~mask) >> height)); c3_b parity = (i >> height) & 1; - return _(memcmp(los_u->pairs[height][parity], h, 32) == 0); + return _(memcmp(los_u->pairs[height][parity], h, sizeof(lss_hash)) == 0); } c3_o lss_verifier_ingest(lss_verifier* los_u, c3_y* leaf_y, c3_w leaf_w, lss_pair* pair) { // verify leaf lss_hash h; _leaf_hash(h, leaf_y, leaf_w, los_u->counter); - if ( c3n == _lss_verifier_find_pair(los_u, los_u->counter, 0, h) ) { + if ( c3n == _lss_verifier_check_hash(los_u, los_u->counter, 0, h) ) { return c3n; } // check whether pair is expected @@ -162,10 +175,10 @@ c3_o lss_verifier_ingest(lss_verifier* los_u, c3_y* leaf_y, c3_w leaf_w, lss_pai } // verify and insert pair c3_w height = _bits_ctz64(los_u->counter); - c3_w pairStart = los_u->counter + (1 << height); // first leaf "covered" by this pair + c3_w start = los_u->counter + (1 << height); // first leaf "covered" by this pair lss_hash parent_hash; _parent_hash(parent_hash, (*pair)[0], (*pair)[1]); - if ( c3n == _lss_verifier_find_pair(los_u, pairStart, height+1, parent_hash) ) { + if ( c3n == _lss_verifier_check_hash(los_u, start, height+1, parent_hash) ) { return c3n; } memcpy(los_u->pairs[height][0], (*pair)[0], sizeof(lss_hash)); From ab52c9c1c5993e400f987b72e945988367a5be2e Mon Sep 17 00:00:00 2001 From: lukechampine Date: Wed, 3 Jul 2024 15:46:22 -0400 Subject: [PATCH 206/430] lss: replace len and clz functions with builtins --- pkg/vere/io/lss.c | 83 ++++++++++------------------------------------ pkg/vere/io/lss.h | 2 +- pkg/vere/io/mesa.c | 4 +-- 3 files changed, 19 insertions(+), 70 deletions(-) diff --git a/pkg/vere/io/lss.c b/pkg/vere/io/lss.c index ebd7e161d5..2e9b0b118d 100644 --- a/pkg/vere/io/lss.c +++ b/pkg/vere/io/lss.c @@ -33,55 +33,22 @@ static void _parent_hash(lss_hash out, lss_hash left, lss_hash right) memcpy(out, block, 32); } -static c3_y _bits_ctz64(c3_d val_d) -{ - if (val_d == 0 ) { - return 64; - } - c3_y ret_y = 0; - if ((val_d & 0x00000000FFFFFFFFULL) == 0) { ret_y += 32; val_d >>= 32; } - if ((val_d & 0x000000000000FFFFULL) == 0) { ret_y += 16; val_d >>= 16; } - if ((val_d & 0x00000000000000FFULL) == 0) { ret_y += 8; val_d >>= 8; } - if ((val_d & 0x000000000000000FULL) == 0) { ret_y += 4; val_d >>= 4; } - if ((val_d & 0x0000000000000003ULL) == 0) { ret_y += 2; val_d >>= 2; } - if ((val_d & 0x0000000000000001ULL) == 0) { ret_y += 1; val_d >>= 1; } - return ret_y; -} - -static c3_y _bits_len64(c3_d val_d) -{ - if (val_d == 0) { - return 0; - } - c3_y ret_y = 64; - if ((val_d & 0xFFFFFFFF00000000ULL) == 0) { ret_y -= 32; val_d <<= 32; } - if ((val_d & 0xFFFF000000000000ULL) == 0) { ret_y -= 16; val_d <<= 16; } - if ((val_d & 0xFF00000000000000ULL) == 0) { ret_y -= 8; val_d <<= 8; } - if ((val_d & 0xF000000000000000ULL) == 0) { ret_y -= 4; val_d <<= 4; } - if ((val_d & 0xC000000000000000ULL) == 0) { ret_y -= 2; val_d <<= 2; } - if ((val_d & 0x8000000000000000ULL) == 0) { ret_y -= 1; val_d <<= 1; } - return ret_y; -} - c3_w lss_proof_size(c3_w leaves) { - return 1 + _bits_len64(leaves-1); + return 1 + c3_bits_word(leaves-1); } c3_o _lss_expect_pair(c3_w leaves, c3_w i) { - if ( ((i != 0) && (i + (1 << (1+_bits_ctz64(i))) < leaves)) ) { - return c3y; - } - return c3n; + return __((i != 0) && ((i + (1 << (1+c3_tz_w(i)))) < leaves)); } static void _lss_builder_merge(lss_builder* bil_u, c3_w height, lss_hash l, lss_hash r) { // whenever two subtrees are merged, insert them into the pairs array; // if the merged tree is part of the left-side "spine" of the tree, // instead add the right subtree to the initial proof - if (height+1 < _bits_len64(bil_u->counter)) { - c3_w c = (bil_u->counter&~((1<<(height+1))-1)) - (1<pairs[c][0], l, sizeof(lss_hash)); - memcpy(bil_u->pairs[c][1], r, sizeof(lss_hash)); + if ( bil_u->counter >> (height+1) ) { + c3_w i = (bil_u->counter&~((1<<(height+1))-1)) - (1<pairs[i][0], l, sizeof(lss_hash)); + memcpy(bil_u->pairs[i][1], r, sizeof(lss_hash)); } else { if (height == 0) { memcpy(bil_u->proof[0], l, sizeof(lss_hash)); // proof always begins with [0, 1) @@ -105,10 +72,10 @@ void lss_builder_ingest(lss_builder* bil_u, c3_y* leaf_y, c3_w leaf_w) { lss_hash* lss_builder_finalize(lss_builder* bil_u) { if ( bil_u->counter != 0 ) { - c3_w height = _bits_ctz64(bil_u->counter); + c3_w height = c3_tz_w(bil_u->counter); lss_hash h; memcpy(h, bil_u->trees[height], sizeof(lss_hash)); - for (height++; height < 32; height++) { + for (height++; height < sizeof(bil_u->trees)/sizeof(lss_hash); height++) { if ( bil_u->counter&(1<trees[height], h); _parent_hash(h, bil_u->trees[height], h); @@ -152,11 +119,11 @@ static c3_o _lss_verifier_check_hash(lss_verifier* los_u, c3_w i, c3_w height, l // it determines how many odd pairs are directly above us, and increments the // height accordingly. A mask is used to ensure that we only perform this // adjustment when necessary. - c3_w odd = (1<<_bits_len64(los_u->leaves-1)) - los_u->leaves; - c3_w mask = (1<<_bits_len64(i ^ (los_u->leaves-1))) - 1; - height += _bits_ctz64(~((odd&~mask) >> height)); + c3_w odd = (1<leaves-1)) - los_u->leaves; + c3_w mask = (1<leaves-1))) - 1; + height += c3_tz_w(~((odd&~mask) >> height)); c3_b parity = (i >> height) & 1; - return _(memcmp(los_u->pairs[height][parity], h, sizeof(lss_hash)) == 0); + return __(memcmp(los_u->pairs[height][parity], h, sizeof(lss_hash)) == 0); } c3_o lss_verifier_ingest(lss_verifier* los_u, c3_y* leaf_y, c3_w leaf_w, lss_pair* pair) { @@ -174,7 +141,7 @@ c3_o lss_verifier_ingest(lss_verifier* los_u, c3_y* leaf_y, c3_w leaf_w, lss_pai return c3y; } // verify and insert pair - c3_w height = _bits_ctz64(los_u->counter); + c3_w height = c3_tz_w(los_u->counter); c3_w start = los_u->counter + (1 << height); // first leaf "covered" by this pair lss_hash parent_hash; _parent_hash(parent_hash, (*pair)[0], (*pair)[1]); @@ -187,9 +154,9 @@ c3_o lss_verifier_ingest(lss_verifier* los_u, c3_y* leaf_y, c3_w leaf_w, lss_pai return c3y; } -c3_o lss_verifier_init(lss_verifier* los_u, c3_w leaves, lss_hash* proof) { +void lss_verifier_init(lss_verifier* los_u, c3_w leaves, lss_hash* proof) { c3_w proof_w = lss_proof_size(leaves); - c3_w pairs_w = _bits_len64(leaves); + c3_w pairs_w = c3_bits_word(leaves); los_u->leaves = leaves; los_u->counter = 0; los_u->pairs = c3_calloc(pairs_w * sizeof(lss_pair)); @@ -197,7 +164,6 @@ c3_o lss_verifier_init(lss_verifier* los_u, c3_w leaves, lss_hash* proof) { for (c3_w i = 1; i < proof_w; i++) { memcpy(los_u->pairs[i-1][1], proof[i], sizeof(lss_hash)); } - return c3y; } void lss_verifier_free(lss_verifier* los_u) { @@ -218,20 +184,6 @@ void lss_root(lss_hash root, lss_hash* proof, c3_w proof_w) { #ifdef LSS_TEST -static void _test__bits_ctz64() -{ - #define asrt(x,y) if ( x != y ) { fprintf(stderr, "failed at %s:%u: got %u, expected %u", __FILE__, __LINE__, x,y); exit(1); } - asrt(_bits_ctz64(0ULL), 64); - asrt(_bits_ctz64(0x0000000000000001ULL), 0); - asrt(_bits_ctz64(0x0000000000000002ULL), 1); - asrt(_bits_ctz64(0x0000000000000100ULL), 8); - asrt(_bits_ctz64(0x0000000080000000ULL), 31); - asrt(_bits_ctz64(0x0000000100000000ULL), 32); - asrt(_bits_ctz64(0x4000000000000000ULL), 62); - asrt(_bits_ctz64(0x8000000000000000ULL), 63); - #undef asrt -} - static lss_pair* _make_pair(lss_hash left, lss_hash right) { lss_pair* pair = c3_malloc(sizeof(lss_pair)); @@ -279,7 +231,7 @@ static void _test_lss_manual_verify_8() // verify lss_verifier lss_u; memset(&lss_u, 0, sizeof(lss_verifier)); - asrt_ok(lss_verifier_init(&lss_u, 8, proof)) + lss_verifier_init(&lss_u, 8, proof); for ( c3_w i = 0; i < 8; i++ ) { asrt_ok(lss_verifier_ingest(&lss_u, leaves_y[i], leaves_w[i], pairs[i])) } @@ -309,7 +261,7 @@ static void _test_lss_build_verify(c3_w dat_w) // verify lss_verifier lss_u; - asrt_ok(lss_verifier_init(&lss_u, leaves_w, proof)) + lss_verifier_init(&lss_u, leaves_w, proof); for ( c3_w i = 0; i < leaves_w; i++ ) { c3_y* leaf_y = dat_y + (i*1024); c3_w leaf_w = (i < leaves_w - 1) ? 1024 : dat_w % 1024; @@ -321,7 +273,6 @@ static void _test_lss_build_verify(c3_w dat_w) } int main() { - _test__bits_ctz64(); _test_lss_manual_verify_8(); _test_lss_build_verify(2 * 1024); _test_lss_build_verify(2 * 1024 + 2); diff --git a/pkg/vere/io/lss.h b/pkg/vere/io/lss.h index bafffd3944..dfd8b5bad5 100644 --- a/pkg/vere/io/lss.h +++ b/pkg/vere/io/lss.h @@ -38,7 +38,7 @@ typedef struct _lss_verifier { c3_o lss_verifier_ingest(lss_verifier* ver_u, c3_y* leaf_y, c3_w leaf_w, lss_pair* pair); -c3_o lss_verifier_init(lss_verifier* ver_u, c3_w leaves, lss_hash* proof); +void lss_verifier_init(lss_verifier* ver_u, c3_w leaves, lss_hash* proof); void lss_verifier_free(lss_verifier* ver_u); diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index d02109749c..172c53ce3b 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -2017,9 +2017,7 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) // lss_root(root, pof_u, pof_w); req_u->los_u = c3_calloc(sizeof(lss_verifier)); - if ( c3y != lss_verifier_init(req_u->los_u, req_u->tot_w, pof_u) ) { - return NULL; // XX ??? - } + lss_verifier_init(req_u->los_u, req_u->tot_w, pof_u); c3_free(pof_u); if ( c3y != lss_verifier_ingest(req_u->los_u, dat_u->fra_y, dat_u->len_w, NULL) ) { return NULL; // XX ??? From dfdef6b5c6fda741a975bbb25aa8b42ff1a034f7 Mon Sep 17 00:00:00 2001 From: lukechampine Date: Sun, 7 Jul 2024 23:39:27 -0400 Subject: [PATCH 207/430] lss: implement transceiver --- pkg/vere/io/lss.c | 120 +++++++++++++++++++++++++++++++++++++++++---- pkg/vere/io/lss.h | 3 +- pkg/vere/io/mesa.c | 4 +- 3 files changed, 115 insertions(+), 12 deletions(-) diff --git a/pkg/vere/io/lss.c b/pkg/vere/io/lss.c index 2e9b0b118d..9d2734ba55 100644 --- a/pkg/vere/io/lss.c +++ b/pkg/vere/io/lss.c @@ -8,15 +8,15 @@ static c3_y IV[32] = {103, 230, 9, 106, 133, 174, 103, 187, 114, 243, 110, 60, 58, 245, 79, 165, 127, 82, 14, 81, 140, 104, 5, 155, 171, 217, 131, 31, 25, 205, 224, 91}; -static void _leaf_hash(lss_hash out, c3_y* leaf, c3_w len, c3_d counter) +static void _leaf_hash(lss_hash out, c3_y* leaf_y, c3_w leaf_w, c3_d counter_d) { c3_y cv[32]; memcpy(cv, IV, 32); c3_y block[64] = {0}; c3_y block_len = 0; c3_y flags = 0; - urcrypt_blake3_chunk_output(len, leaf, cv, block, &block_len, &counter, &flags); - urcrypt_blake3_compress(cv, block, block_len, counter, flags, block); + urcrypt_blake3_chunk_output(leaf_w, leaf_y, cv, block, &block_len, &counter_d, &flags); + urcrypt_blake3_compress(cv, block, block_len, counter_d, flags, block); memcpy(out, block, 32); } @@ -33,6 +33,20 @@ static void _parent_hash(lss_hash out, lss_hash left, lss_hash right) memcpy(out, block, 32); } +static void _subtree_root(lss_hash out, c3_y* leaf_y, c3_w leaf_w, c3_d counter_d) +{ + if ( leaf_w <= 1024 ) { + _leaf_hash(out, leaf_y, leaf_w, counter_d); + return; + } + c3_w leaves_w = (leaf_w + 1023) / 1024; + c3_w mid_w = 1 << (c3_bits_word(leaves_w-1) - 1); + lss_hash l, r; + _subtree_root(l, leaf_y, (mid_w * 1024), counter_d); + _subtree_root(r, leaf_y + (mid_w * 1024), leaf_w - (mid_w * 1024), counter_d + mid_w); + _parent_hash(out, l, r); +} + c3_w lss_proof_size(c3_w leaves) { return 1 + c3_bits_word(leaves-1); } @@ -70,6 +84,20 @@ void lss_builder_ingest(lss_builder* bil_u, c3_y* leaf_y, c3_w leaf_w) { bil_u->counter++; } +void lss_builder_transceive(lss_builder* bil_u, c3_w steps, c3_y* jumbo_y, c3_w jumbo_w, lss_pair* pair) { + if ( pair != NULL ) { + c3_w i = bil_u->counter; + memcpy(bil_u->pairs[i][0], (*pair)[0], sizeof(lss_hash)); + memcpy(bil_u->pairs[i][1], (*pair)[1], sizeof(lss_hash)); + } + for (c3_w i = 0; (i < (1< 0); i++) { + c3_w leaf_w = c3_min(jumbo_w, 1024); + lss_builder_ingest(bil_u, jumbo_y, leaf_w); + jumbo_y += leaf_w; + jumbo_w -= leaf_w; + } +} + lss_hash* lss_builder_finalize(lss_builder* bil_u) { if ( bil_u->counter != 0 ) { c3_w height = c3_tz_w(bil_u->counter); @@ -104,6 +132,13 @@ void lss_builder_free(lss_builder* bil_u) { c3_free(bil_u); } +lss_hash* lss_transceive_proof(lss_hash* proof, c3_w steps) { + for (c3_w i = 0; i < steps; i++) { + _parent_hash(proof[i+1], proof[i], proof[i+1]); + } + return proof + steps; +} + static c3_o _lss_verifier_check_hash(lss_verifier* los_u, c3_w i, c3_w height, lss_hash h) { // Binary numeral trees are composed of a set of perfect binary trees of @@ -113,8 +148,8 @@ static c3_o _lss_verifier_check_hash(lss_verifier* los_u, c3_w i, c3_w height, l // a greater height than the right child. // // When such a pair is inserted by lss_verifier_ingest, both children are - // inserted at the same height. This means that the right child will be in the - // "wrong" place. The abstruse bithacking below corrects for this. First, it + // inserted at the same height, causing right child to be in the "wrong" + // place. The abstruse bithacking below corrects for this. First, it // calculates the positions of the odd pairs within a tree of this size. Then // it determines how many odd pairs are directly above us, and increments the // height accordingly. A mask is used to ensure that we only perform this @@ -129,7 +164,7 @@ static c3_o _lss_verifier_check_hash(lss_verifier* los_u, c3_w i, c3_w height, l c3_o lss_verifier_ingest(lss_verifier* los_u, c3_y* leaf_y, c3_w leaf_w, lss_pair* pair) { // verify leaf lss_hash h; - _leaf_hash(h, leaf_y, leaf_w, los_u->counter); + _subtree_root(h, leaf_y, leaf_w, los_u->counter << los_u->steps); if ( c3n == _lss_verifier_check_hash(los_u, los_u->counter, 0, h) ) { return c3n; } @@ -154,9 +189,10 @@ c3_o lss_verifier_ingest(lss_verifier* los_u, c3_y* leaf_y, c3_w leaf_w, lss_pai return c3y; } -void lss_verifier_init(lss_verifier* los_u, c3_w leaves, lss_hash* proof) { +void lss_verifier_init(lss_verifier* los_u, c3_w steps, c3_w leaves, lss_hash* proof) { c3_w proof_w = lss_proof_size(leaves); c3_w pairs_w = c3_bits_word(leaves); + los_u->steps = steps; los_u->leaves = leaves; los_u->counter = 0; los_u->pairs = c3_calloc(pairs_w * sizeof(lss_pair)); @@ -231,7 +267,7 @@ static void _test_lss_manual_verify_8() // verify lss_verifier lss_u; memset(&lss_u, 0, sizeof(lss_verifier)); - lss_verifier_init(&lss_u, 8, proof); + lss_verifier_init(&lss_u, 0, 8, proof); for ( c3_w i = 0; i < 8; i++ ) { asrt_ok(lss_verifier_ingest(&lss_u, leaves_y[i], leaves_w[i], pairs[i])) } @@ -261,7 +297,7 @@ static void _test_lss_build_verify(c3_w dat_w) // verify lss_verifier lss_u; - lss_verifier_init(&lss_u, leaves_w, proof); + lss_verifier_init(&lss_u, 0, leaves_w, proof); for ( c3_w i = 0; i < leaves_w; i++ ) { c3_y* leaf_y = dat_y + (i*1024); c3_w leaf_w = (i < leaves_w - 1) ? 1024 : dat_w % 1024; @@ -272,14 +308,80 @@ static void _test_lss_build_verify(c3_w dat_w) #undef asrt_ok } +static void _test_lss_build_verify_jumbo(c3_w steps, c3_w dat_w) +{ + #define asrt_ok(y) if ( c3y != y ) { fprintf(stderr, "failed at %s:%u\n", __FILE__, __LINE__); exit(1); } + + c3_y* dat_y = c3_calloc(dat_w); + for ( c3_w i = 0; i < dat_w; i++ ) { + dat_y[i] = i; + } + c3_w leaves_w = (dat_w + 1023) / 1024; + + // build + lss_builder bil_u; + lss_builder_init(&bil_u, leaves_w); + for ( c3_w i = 0; i < leaves_w; i++ ) { + c3_y* leaf_y = dat_y + (i*1024); + c3_w leaf_w = c3_min(dat_w - (i*1024), 1024); + lss_builder_ingest(&bil_u, leaf_y, leaf_w); + } + lss_hash* proof = lss_builder_finalize(&bil_u); + + // transceive up + c3_w jumbo_leaf_w = 1024 << steps; + c3_w jumbo_leaves_w = (dat_w + jumbo_leaf_w - 1) / jumbo_leaf_w; + + // verify (if possible) + if ( jumbo_leaves_w > 1 ) { + lss_verifier lss_u; + lss_verifier_init(&lss_u, steps, jumbo_leaves_w, lss_transceive_proof(proof, steps)); + for ( c3_w i = 0; i < jumbo_leaves_w; i++ ) { + c3_y* leaf_y = dat_y + (i*(1024<los_u = c3_calloc(sizeof(lss_verifier)); - lss_verifier_init(req_u->los_u, req_u->tot_w, pof_u); + lss_verifier_init(req_u->los_u, 0, req_u->tot_w, pof_u); c3_free(pof_u); if ( c3y != lss_verifier_ingest(req_u->los_u, dat_u->fra_y, dat_u->len_w, NULL) ) { return NULL; // XX ??? @@ -2037,7 +2037,7 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) // lss_root(root, pof_u, dat_u->len_w/32); req_u->los_u = c3_calloc(sizeof(lss_verifier)); - lss_verifier_init(req_u->los_u, req_u->tot_w, pof_u); + lss_verifier_init(req_u->los_u, 0, req_u->tot_w, pof_u); c3_free(pof_u); } vec_init(&req_u->mis_u, 8); From 6f561afb5b4ed4410a713f769c7293a512dabdfb Mon Sep 17 00:00:00 2001 From: midden-fabler Date: Mon, 8 Jul 2024 14:38:09 -0400 Subject: [PATCH 208/430] http: add content-range header --- pkg/vere/io/http.c | 82 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 72 insertions(+), 10 deletions(-) diff --git a/pkg/vere/io/http.c b/pkg/vere/io/http.c index f4dbb4182e..577a23db79 100644 --- a/pkg/vere/io/http.c +++ b/pkg/vere/io/http.c @@ -700,8 +700,10 @@ _find_tis_fas(void* txt, c3_w len) // [x] 206 // [x] 200 // [x] open range vs error -// [ ] test stream -// [ ] multipart ranges +// [x] test stream +// [x] content range function +// [ ] video controls +// [ ] multipart ranges? // typedef struct _range_header { c3_z start_z; @@ -774,6 +776,64 @@ _get_range(c3_c* txt_c, c3_w len_w) return slice; } +static u3_noun +_content_rng(range_header rng, c3_w lent_w) +{ + u3_noun out; + u3_atom start; + u3_atom end; + + if ( rng.start_z == SIZE_MAX ) { + if ( rng.end_z == SIZE_MAX) { + // [~ ~] + out = u3_nul; + } + else { + // [~ @] + if ( rng.end_z > lent_w ) { + out = u3_nul; + return out; + } + else { + // last bytes + start = lent_w - rng.end_z; + end = rng.end_z; + } + } + } + else if ( rng.end_z == SIZE_MAX ) { + // [@ ~] + if ( rng.start_z > lent_w ) { + out = u3_nul; + return out; + } + else { + start = rng.start_z; + end = lent_w; + } + } + else if (rng.end_z > lent_w) { + out = u3_nul; + return out; + } + else { + // [@ @] + start = rng.start_z; + end = rng.end_z; + } + + u3_noun lin = u3i_list(u3i_string("bytes "), + u3do("crip", u3do("a-co:co", start)), + c3_s1('-'), + u3do("crip", u3do("a-co:co", end)), + c3_s1('/'), + // XX ++? + u3do("crip", u3do("a-co:co", lent_w)), + u3_none); + u3_atom dat = u3qc_rap(3, lin); + out = u3nc(u3i_string("Content-Range"), dat); + return out; +} /* _http_req_dispatch(): dispatch http request to %eyre */ @@ -944,16 +1004,18 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) h2o_send_error_generic(req_u->rec_u, 416, msg_c, msg_c, 0); return; } - if ( u3r_sing(result, octs) == c3y) { - // 200 - _http_cache_respond(req_u, nac); - } - else { + // if ( u3r_sing(result, octs) == c3y) { + // // 200 + // _http_cache_respond(req_u, nac); + // } + // else { // 206 - u3_noun tmp = u3i_edit(nac, 124, 206); - u3_noun res = u3i_edit(tmp, 127, result); + u3_noun con_rng_hed = _content_rng(rng_hed, u3h(octs)); + u3_noun res = u3i_edit(nac, 127, result); + res = u3i_edit(res, 124, 206); + res = u3i_edit(res, 125, u3nc(con_rng_hed, u3r_at(125, res))); _http_cache_respond(req_u, res); - } + // } } } else { From 150bc2fba38080248585d29fdacd775740c7b198 Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Mon, 8 Jul 2024 17:33:45 -0400 Subject: [PATCH 209/430] mesa: route switching compiles --- pkg/vere/io/mesa.c | 91 ++++++++++++++++++++++++++-------------------- 1 file changed, 52 insertions(+), 39 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index cbaecff7a5..56caf0d92d 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -29,6 +29,7 @@ c3_o dop_o = c3n; #define REORDER_THRESH 5 #define DIRECT_ROUTE_TIMEOUT_MICROS 5000000 +#define DIRECT_ROUTE_RETRY_MICROS 1000000 // logging and debug symbols #define MESA_SYM_DESC(SYM) MESA_DESC_ ## SYM @@ -940,19 +941,18 @@ static void _mesa_send(u3_mesa_pict* pic_u, u3_lane* lan_u) _mesa_send_buf(sam_u, *lan_u, buf_y, siz_w); } -// TODO: check whether direct, decide what to send static void -_try_resend(u3_pend_req* req_u) +_try_resend(u3_pend_req* req_u, c3_w ack_w) { c3_o los_o = c3n; c3_d now_d = _get_now_micros(); u3_mesa_pact *pac_u = &req_u->pic_u->pac_u; - for ( int i = req_u->lef_w; i < req_u->nex_w; i++ ) { + for ( int i = req_u->lef_w; i < ack_w; i++ ) { // TODO: make fast recovery different from slow // TODO: track skip count but not dupes, since dupes are meaningless - if ( ( c3y == bitset_has(&req_u->was_u, i) ) && - ( (now_d - req_u->wat_u[i].sen_d) > req_u->gag_u->rto_w ) ) { + if ( (c3y == bitset_has(&req_u->was_u, i)) && + (now_d - req_u->wat_u[i].sen_d > req_u->gag_u->rto_w) ) { los_o = c3y; pac_u->pek_u.nam_u.fra_w = i; @@ -962,10 +962,23 @@ _try_resend(u3_pend_req* req_u) u3l_log("failed to etch"); u3_assert( 0 ); } - // TODO: better route management - // it needs to be more ergonomic to access the u3_peer - // could do a backpointer to the u3_peer from the u3_pend_req - _mesa_send_buf(req_u->pic_u->sam_u, req_u->per_u->dan_u, buf_y, siz_w); + + u3_peer* per_u = req_u->per_u; + u3_mesa* sam_u = per_u->sam_u; + if ( c3y == _mesa_is_direct_mode(per_u) ) { + _mesa_send_buf(sam_u, per_u->dan_u, buf_y, siz_w); + per_u->dir_u.sen_d = now_d; + } + else { + u3_lane imp_u = _mesa_get_czar_lane(sam_u, per_u->imp_y); + _mesa_send_buf(sam_u, imp_u, buf_y, siz_w); + per_u->ind_u.sen_d = now_d; + + if ( per_u->dir_u.sen_d + DIRECT_ROUTE_RETRY_MICROS > now_d ) { + _mesa_send_buf(sam_u, per_u->dan_u, buf_y, siz_w); + per_u->dir_u.sen_d = now_d; + } + } _mesa_req_pact_resent(req_u, &pac_u->pek_u.nam_u); } } @@ -980,6 +993,7 @@ _try_resend(u3_pend_req* req_u) static void _mesa_packet_timeout(uv_timer_t* tim_u); +// TODO rename to indicate it sets a timer static void _update_oldest_req(u3_pend_req *req_u) { @@ -1019,7 +1033,7 @@ static void _mesa_packet_timeout(uv_timer_t* tim_u) { u3_pend_req* req_u = (u3_pend_req*)tim_u->data; /* u3l_log("%u packet timed out", req_u->old_w); */ - _try_resend(req_u); + _try_resend(req_u, req_u->nex_w); _update_oldest_req(req_u); } @@ -1063,6 +1077,8 @@ _mesa_burn_misorder_queue(u3_pend_req* req_u) return res_y; } +static void _init_lane_state(u3_lane_state*); + /* _mesa_req_pact_done(): mark packet as done, returning if we should continue */ static u3_pend_req* @@ -1081,24 +1097,24 @@ _mesa_req_pact_done(u3_mesa* sam_u, return NULL; } - // TODO: if hop count is nonzero, - // then write down the next hop as the candidate direct route. - // We also need to branch on that to figure out which - // congestion control state to update. - // TODO: when we change candidate direct routes, zero out the - // direct-route congestion control state. - u3_lane_state sat_u; + u3_lane_state* sat_u; if ( 0 == hop_y ) { + c3_i new_i = ( + (lan_u.pip_w != req_u->per_u->dan_u.pip_w) || + (lan_u.por_s != req_u->per_u->dan_u.por_s) + ); req_u->per_u->dan_u = lan_u; - sat_u = req_u->per_u->dir_u; + sat_u = &req_u->per_u->dir_u; + if ( new_i ) { + _init_lane_state(sat_u); + } } else { - sat_u = req_u->per_u->ind_u; + sat_u = &req_u->per_u->ind_u; } + sat_u->her_d = now_d; - c3_w siz_w = (1 << (nam_u->boq_y - 3)); - // First packet received, instantiate request fully - + // received past the end of the message if ( dat_u->tot_w <= nam_u->fra_w ) { MESA_LOG(STRANGE); // XX: is this sufficient to drop whole request @@ -1111,16 +1127,16 @@ _mesa_req_pact_done(u3_mesa* sam_u, return req_u; } - /* u3l_log("bitset_del %u", nam_u->fra_w); */ bitset_del(&req_u->was_u, nam_u->fra_w); if ( nam_u->fra_w > req_u->ack_w ) { req_u->ack_w = nam_u->fra_w; } - if ( nam_u->fra_w != 0 && req_u->wat_u[nam_u->fra_w].tie_y != 1 ) { + #ifdef MESA_DEBUG + if ( nam_u->fra_w != 0 && req_u->wat_u[nam_u->fra_w].tie_y != 1 ) { /* u3l_log("received retry %u", nam_u->fra_w); */ -#endif } +#endif req_u->len_w++; /* u3l_log("fragment %u len %u", nam_u->fra_w, req_u->len_w); */ @@ -1174,11 +1190,10 @@ _mesa_req_pact_done(u3_mesa* sam_u, // handle gauge update _mesa_handle_ack(req_u->gag_u, &req_u->wat_u[nam_u->fra_w]); - + c3_w siz_w = (1 << (nam_u->boq_y - 3)); memcpy(req_u->dat_y + (siz_w * nam_u->fra_w), dat_u->fra_y, dat_u->len_w); - _try_resend(req_u); - + _try_resend(req_u, nam_u->fra_w); _update_oldest_req(req_u); // _mesa_put_request(sam_u, nam_u, req_u); @@ -1243,7 +1258,7 @@ _mesa_rout_bufs(u3_mesa* sam_u, c3_y* buf_y, c3_w len_w, u3_noun las) static void _mesa_timer_cb(uv_timer_t* tim_u) { u3_pend_req* req_u = tim_u->data; - _try_resend(req_u); + _try_resend(req_u, req_u->nex_w); } static void @@ -1490,15 +1505,13 @@ _mesa_io_exit(u3_auto* car_u) uv_close(&sam_u->had_u, _mesa_exit_cb); } -static u3_lane_state -_init_lane_state() +static void +_init_lane_state(u3_lane_state* sat_u) { - u3_lane_state sat_u; - sat_u.sen_d = 0; - sat_u.her_d = 0; - sat_u.rtt_w = 1000000; - sat_u.rtv_w = 1000000; - return sat_u; + sat_u->sen_d = 0; + sat_u->her_d = 0; + sat_u->rtt_w = 1000000; + sat_u->rtv_w = 1000000; } static void @@ -1507,9 +1520,9 @@ _init_peer(u3_mesa* sam_u, u3_peer* per_u) per_u->sam_u = sam_u; per_u->ful_o = c3n; per_u->dan_u = (u3_lane){0,0}; - per_u->dir_u = _init_lane_state(); + _init_lane_state(&per_u->dir_u); per_u->imp_y = 0; - per_u->ind_u = _init_lane_state(); + _init_lane_state(&per_u->ind_u); per_u->req_p = u3h_new(); } From 59adc332a6a88f0f73c86d8442cb8de397ad879d Mon Sep 17 00:00:00 2001 From: midden-fabler Date: Mon, 8 Jul 2024 23:32:07 -0400 Subject: [PATCH 210/430] http: fix content-range size --- pkg/vere/io/http.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pkg/vere/io/http.c b/pkg/vere/io/http.c index 577a23db79..3262a899c5 100644 --- a/pkg/vere/io/http.c +++ b/pkg/vere/io/http.c @@ -702,7 +702,7 @@ _find_tis_fas(void* txt, c3_w len) // [x] open range vs error // [x] test stream // [x] content range function -// [ ] video controls +// [x] video controls // [ ] multipart ranges? // typedef struct _range_header { @@ -827,8 +827,7 @@ _content_rng(range_header rng, c3_w lent_w) c3_s1('-'), u3do("crip", u3do("a-co:co", end)), c3_s1('/'), - // XX ++? - u3do("crip", u3do("a-co:co", lent_w)), + u3do("crip", u3do("a-co:co", ++lent_w )), u3_none); u3_atom dat = u3qc_rap(3, lin); out = u3nc(u3i_string("Content-Range"), dat); From 9d00bed42b37439d3a572262402687c68c58ccb7 Mon Sep 17 00:00:00 2001 From: pkova Date: Tue, 9 Jul 2024 11:07:26 +0300 Subject: [PATCH 211/430] mesa: wip tranceiver stuff --- pkg/vere/io/mesa.c | 96 +++++++++++++++++++++++++++++++--------------- 1 file changed, 65 insertions(+), 31 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index c0c6f6c128..23ee645c08 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -967,12 +967,13 @@ _mesa_burn_misorder_queue(u3_pend_req* req_u) static u3_pend_req* _mesa_req_pact_done(u3_mesa* sam_u, u3_mesa_name *nam_u, u3_mesa_data* dat_u, u3_lane* lan_u) { + u3l_log("rec_pact_done"); u3_weak ret = u3_none; c3_d now_d = _get_now_micros(); u3_pend_req* req_u = _mesa_get_request(sam_u, nam_u); if ( NULL == req_u ) { - /* MESA_LOG(APATHY); */ + MESA_LOG(APATHY); return NULL; } @@ -992,6 +993,7 @@ _mesa_req_pact_done(u3_mesa* sam_u, u3_mesa_name *nam_u, u3_mesa_data* dat_u, u3 // First packet received, instantiate request fully if ( dat_u->tot_w <= nam_u->fra_w ) { + u3l_log("strange tot_w %u fra_w %u req_u %u", dat_u->tot_w, nam_u->fra_w, req_u->len_w); MESA_LOG(STRANGE); // XX: is this sufficient to drop whole request return req_u; @@ -999,7 +1001,7 @@ _mesa_req_pact_done(u3_mesa* sam_u, u3_mesa_name *nam_u, u3_mesa_data* dat_u, u3 // received duplicate if ( c3n == bitset_has(&req_u->was_u, nam_u->fra_w) ) { - /* MESA_LOG(DUPE); */ + MESA_LOG(DUPE); req_u->wat_u[nam_u->fra_w].dup_y++; return req_u; } @@ -1016,7 +1018,7 @@ _mesa_req_pact_done(u3_mesa* sam_u, u3_mesa_name *nam_u, u3_mesa_data* dat_u, u3 } req_u->len_w++; - /* u3l_log("fragment %u len %u", nam_u->fra_w, req_u->len_w); */ + u3l_log("fragment %u len %u", nam_u->fra_w, req_u->len_w); if ( req_u->lef_w == nam_u->fra_w ) { req_u->lef_w++; } @@ -1047,19 +1049,19 @@ _mesa_req_pact_done(u3_mesa* sam_u, u3_mesa_name *nam_u, u3_mesa_data* dat_u, u3 buf_u->num_w = nam_u->fra_w; vec_append(&req_u->mis_u, buf_u); } - /* else if ( c3y != (ver_y = lss_verifier_ingest(req_u->los_u, buf_y, len_w, par_u)) ) { */ - /* c3_free(par_u); */ - /* // TODO: do we drop the whole request on the floor? */ - /* u3l_log("auth fail frag %u", nam_u->fra_w); */ - /* MESA_LOG(AUTH); */ - /* return req_u; */ - /* } */ - /* else if ( vec_len(&req_u->mis_u) != 0 */ - /* && c3y != (ver_y = _mesa_burn_misorder_queue(req_u))) { */ - /* c3_free(par_u); */ - /* MESA_LOG(AUTH) */ - /* return req_u; */ - /* } */ + else if ( c3y != (ver_y = lss_verifier_ingest(req_u->los_u, buf_y, len_w, par_u)) ) { + c3_free(par_u); + // TODO: do we drop the whole request on the floor? + u3l_log("auth fail frag %u", nam_u->fra_w); + MESA_LOG(AUTH); + return req_u; + } + else if ( vec_len(&req_u->mis_u) != 0 + && c3y != (ver_y = _mesa_burn_misorder_queue(req_u))) { + c3_free(par_u); + MESA_LOG(AUTH) + return req_u; + } else { c3_free(par_u); } @@ -1745,7 +1747,7 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun nun) //u3_noun pax = _mesa_path_with_fra(pac_u->pek_u.nam_u.pat_c, &fra_s); u3_weak hit = u3r_at(7, nun); - u3_noun proof = u3h(hit); + /* u3_noun proof = u3h(hit); */ /* u3m_p("proof", proof); */ hit = u3t(hit); @@ -1766,8 +1768,7 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun nun) c3_y* out_y; /* u3l_log("path %s", pac_u->pek_u.nam_u.pat_c); */ - u3_mesa_pact tac_u; - u3_mesa_pact muna; + u3_mesa_pact tac_u = {0}; c3_w len_w = u3r_met(3, hit); u3l_log("len_w %u", len_w); @@ -1780,14 +1781,42 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun nun) /* u3l_log("jumbo frame"); */ /* _log_pact(&tac_u); */ - u3_atom jumbo = u3i_bytes(len_w, tac_u.pag_u.dat_u.fra_y); c3_w siz_w = u3r_met(13, jumbo); + c3_w leaves_w = siz_w; + lss_builder bil_u; + lss_builder_init(&bil_u, leaves_w); + + for ( c3_w i = 0; i < leaves_w; i++ ) { + c3_y* leaf_y = puf_y + (i*1024); + c3_w leaf_w = (i < leaves_w - 1) ? 1024 : len_w % 1024; + lss_builder_ingest(&bil_u, leaf_y, leaf_w); + } + + lss_hash* proof = lss_builder_finalize(&bil_u); + c3_w saz_w = lss_proof_size(leaves_w); + + tac_u.pag_u.nam_u.boq_y = 13; tac_u.pag_u.dat_u.tot_w = siz_w; - /* c3_w off_w = 0; */ + if ( c3y == tac_u.pag_u.nam_u.nit_o && leaves_w > 4) { + memcpy(tac_u.pag_u.dat_u.fra_y, proof, saz_w*32); + tac_u.pag_u.dat_u.len_w = saz_w*32; + /* c3_w off_w = 0; */ + + c3_y baf_y[PACT_SIZE]; + + c3_w lon_w = mesa_etch_pact(baf_y, &tac_u); + + u3_atom lon = u3i_bytes(len_w, baf_y); + + _mesa_put_cache(sam_u, &tac_u.pag_u.nam_u, u3nc(MESA_ITEM, u3k(lon))); + + _mesa_rout_bufs(sam_u, baf_y, lon_w, u3k(u3t(dat))); + } + for (c3_w i=0; i < siz_w; i++) { // (cut 3 [wid 1] dat.byts)) @@ -1800,16 +1829,18 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun nun) u3r_bytes(0, tac_u.pag_u.dat_u.len_w, tac_u.pag_u.dat_u.fra_y, lin); - u3_noun pair = u3h(proof); + /* u3_noun pair = u3h(proof); */ + - if (pair == u3_nul) { + lss_pair* pair = lss_builder_pair(&bil_u, i); + if ( NULL == pair ) { tac_u.pag_u.dat_u.aup_u.len_y = 0; tac_u.pag_u.dat_u.aum_u.typ_e = AUTH_NONE; } else { tac_u.pag_u.dat_u.aup_u.len_y = 2; tac_u.pag_u.dat_u.aum_u.typ_e = AUTH_NEXT; - u3r_bytes(0, 32, tac_u.pag_u.dat_u.aup_u.has_y[0], u3h(u3t(pair))); - u3r_bytes(0, 32, tac_u.pag_u.dat_u.aup_u.has_y[1], u3t(u3t(pair))); + memcpy(tac_u.pag_u.dat_u.aup_u.has_y[0], (*pair)[0], 32); + memcpy(tac_u.pag_u.dat_u.aup_u.has_y[1], (*pair)[1], 32); } /* _log_pact(&tac_u); */ @@ -1830,9 +1861,8 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun nun) /* _log_buf(out_y, tac_u.pag_u.dat_u.len_w); */ - _mesa_rout_bufs(sam_u, buf_y, len_w, u3k(u3t(dat))); - proof = u3t(proof); + /* proof = u3t(proof); */ /* u3l_log("putting %u", pac_u->pek_u.nam_u.fra_w); */ /* _log_pact(pac_u); */ @@ -2005,6 +2035,7 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) // complete the proof by computing the first leaf hash c3_w pof_w = pac_u->pag_u.dat_u.aup_u.len_y + 1; if ( pof_w != lss_proof_size(req_u->tot_w) ) { + u3l_log("hello"); return NULL; // XX ??? } lss_hash* pof_u = c3_calloc(pof_w * sizeof(lss_hash)); @@ -2020,13 +2051,16 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) lss_verifier_init(req_u->los_u, 0, req_u->tot_w, pof_u); c3_free(pof_u); if ( c3y != lss_verifier_ingest(req_u->los_u, dat_u->fra_y, dat_u->len_w, NULL) ) { + u3l_log("hello2"); return NULL; // XX ??? } memcpy(req_u->dat_y, dat_u->fra_y, dat_u->len_w); } else { - if ( dat_u->len_w != lss_proof_size(req_u->tot_w) ) { - return NULL; // XX ??? - } + /* if ( dat_u->len_w != lss_proof_size(req_u->tot_w) ) { */ + /* u3l_log("len real: %u expected %u", dat_u->len_w, lss_proof_size(req_u->tot_w)); */ + /* u3l_log("hello3"); */ + /* return NULL; // XX ??? */ + /* } */ // TODO: cast directly instead of copying? lss_hash* pof_u = c3_calloc(dat_u->len_w * sizeof(lss_hash)); for ( int i = 0; i < dat_u->len_w; i++ ) { @@ -2051,7 +2085,7 @@ static void _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) { #ifdef MESA_DEBUG - /* u3l_log("mesa hear page %u", pic_u->pac_u.pag_u.nam_u.fra_w); */ + u3l_log("mesa hear page %u", pic_u->pac_u.pag_u.nam_u.fra_w); #endif /* _log_buf(pic_u->pac_u.pag_u.dat_u.fra_y, pic_u->pac_u.pag_u.dat_u.len_w); */ From a7d8ccaf268838d18c759d8df5d81bb251ec1ec7 Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Tue, 9 Jul 2024 14:11:36 -0400 Subject: [PATCH 212/430] mesa: more route switching cleanup --- pkg/vere/io/mesa.c | 88 ++++++++++++++++++++++++---------------------- 1 file changed, 45 insertions(+), 43 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 56caf0d92d..818ef1d1f9 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -338,6 +338,12 @@ _clamp_rto(c3_d rto_d) { return c3_min(c3_max(rto_d, 200000), 25000000); } +static inline c3_o +_mesa_is_lane_zero(u3_lane* lan_u) +{ + return __(0 == lan_u->pip_w && 0 == lan_u->por_s); +} + static c3_o _mesa_is_direct_mode(u3_peer* per_u) { @@ -803,7 +809,7 @@ _mesa_req_pact_sent(u3_pend_req* req_u, u3_mesa_name* nam_u) req_u->wat_u[nam_u->fra_w].sip_y = 0; req_u->wat_u[nam_u->fra_w].tie_y = 1; - /* u3l_log("bitset_put %u", nam_u->fra_w); */ + u3l_log("bitset_put %u", nam_u->fra_w); bitset_put(&req_u->was_u, nam_u->fra_w); } else { u3l_log("mesa: no req for sent"); @@ -910,8 +916,8 @@ static void _mesa_send_buf(u3_mesa* sam_u, u3_lane lan_u, c3_y* buf_y, c3_w len_ add_u.sin_port = htons(por_s); #ifdef MESA_DEBUG - /* c3_c* sip_c = inet_ntoa(add_u.sin_addr); */ - /* u3l_log("mesa: sending packet (%s,%u)", sip_c, por_s); */ + c3_c* sip_c = inet_ntoa(add_u.sin_addr); + u3l_log("mesa: sending packet (%s,%u)", sip_c, por_s); #endif uv_buf_t buf_u = uv_buf_init((c3_c*)buf_y, len_w); @@ -966,6 +972,7 @@ _try_resend(u3_pend_req* req_u, c3_w ack_w) u3_peer* per_u = req_u->per_u; u3_mesa* sam_u = per_u->sam_u; if ( c3y == _mesa_is_direct_mode(per_u) ) { + u3l_log("mesa: direct"); _mesa_send_buf(sam_u, per_u->dan_u, buf_y, siz_w); per_u->dir_u.sen_d = now_d; } @@ -973,8 +980,10 @@ _try_resend(u3_pend_req* req_u, c3_w ack_w) u3_lane imp_u = _mesa_get_czar_lane(sam_u, per_u->imp_y); _mesa_send_buf(sam_u, imp_u, buf_y, siz_w); per_u->ind_u.sen_d = now_d; + u3l_log("mesa: indirect"); - if ( per_u->dir_u.sen_d + DIRECT_ROUTE_RETRY_MICROS > now_d ) { + if ( (c3n == _mesa_is_lane_zero(&per_u->dan_u)) && + (per_u->dir_u.sen_d + DIRECT_ROUTE_RETRY_MICROS > now_d)) { _mesa_send_buf(sam_u, per_u->dan_u, buf_y, siz_w); per_u->dir_u.sen_d = now_d; } @@ -998,7 +1007,7 @@ static void _update_oldest_req(u3_pend_req *req_u) { if( req_u->tot_w == 0 || req_u->len_w == req_u->tot_w ) { - /* u3l_log("bad condition"); */ + u3l_log("bad condition"); return; } // scan in flight packets, find oldest @@ -1093,7 +1102,7 @@ _mesa_req_pact_done(u3_mesa* sam_u, u3_pend_req* req_u = _mesa_get_request(sam_u, nam_u); if ( NULL == req_u ) { - /* MESA_LOG(APATHY); */ + MESA_LOG(APATHY); return NULL; } @@ -1123,7 +1132,7 @@ _mesa_req_pact_done(u3_mesa* sam_u, // received duplicate if ( c3n == bitset_has(&req_u->was_u, nam_u->fra_w) ) { - /* MESA_LOG(DUPE); */ + MESA_LOG(DUPE); return req_u; } @@ -1134,12 +1143,12 @@ _mesa_req_pact_done(u3_mesa* sam_u, #ifdef MESA_DEBUG if ( nam_u->fra_w != 0 && req_u->wat_u[nam_u->fra_w].tie_y != 1 ) { - /* u3l_log("received retry %u", nam_u->fra_w); */ + u3l_log("received retry %u", nam_u->fra_w); } #endif req_u->len_w++; - /* u3l_log("fragment %u len %u", nam_u->fra_w, req_u->len_w); */ + u3l_log("fragment %u len %u", nam_u->fra_w, req_u->len_w); if ( req_u->lef_w == nam_u->fra_w ) { req_u->lef_w++; } @@ -2274,6 +2283,30 @@ _mesa_add_lane_to_cache(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_noun las, u3_lan u3z(las); } +// XX forwarding wrong, need a PIT entry +static void +_mesa_forward_request(u3_mesa* sam_u, u3_mesa_pict* pic_u) +{ + u3_mesa_pact* pac_u = &pic_u->pac_u; + c3_d* her_d = pac_u->pek_u.nam_u.her_d; + u3_peer* per_u = _mesa_get_peer(sam_u, her_d); + if ( per_u == NULL ) { + // XX leaks + u3l_log("mesa: alien forward for %s", u3r_string(u3dc("scot", c3__p, u3i_chubs(2, her_d)))); + _mesa_free_pict(pic_u); + return; + } + if ( c3y == sam_u->for_o && sam_u->pir_u->who_d[0] == per_u->imp_y ) { +//#ifdef MESA_DEBUG + u3_lane lin_u = _mesa_get_direct_lane(sam_u, her_d); +//#endif + //_update_hopcount(&pac_u->hed_u); + u3l_log("sending peek %u", pic_u->pac_u.pek_u.nam_u.fra_w); + _mesa_send(pic_u, &lin_u); + } + _mesa_free_pict(pic_u); +} + static void _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) { @@ -2286,24 +2319,8 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) c3_d* her_d = pac_u->pek_u.nam_u.her_d; c3_o our_o = __( 0 == memcmp(her_d, sam_u->pir_u->who_d, sizeof(*her_d) * 2) ); - // XX forwarding wrong, need a PIT entry if ( c3n == our_o ) { - u3_peer* per_u = _mesa_get_peer(sam_u, her_d); - if ( per_u == NULL ) { - // XX leaks - u3l_log("mesa: alien forward for %s", u3r_string(u3dc("scot", c3__p, u3i_chubs(2, her_d)))); - _mesa_free_pict(pic_u); - return; - } - if ( c3y == sam_u->for_o && sam_u->pir_u->who_d[0] == per_u->imp_y ) { -//#ifdef MESA_DEBUG - u3_lane lin_u = _mesa_get_direct_lane(sam_u, her_d); -//#endif - //_update_hopcount(&pac_u->hed_u); - u3l_log("sending peek %u", pac_u->pek_u.nam_u.fra_w); - _mesa_send(pic_u, &lin_u); - } - _mesa_free_pict(pic_u); + _mesa_forward_request(sam_u, pic_u); return; } c3_w fra_w = pac_u->pek_u.nam_u.fra_w; @@ -2400,24 +2417,8 @@ _mesa_hear_poke(u3_mesa_pict* pic_u, u3_lane* lan_u) u3_assert(pac_u->hed_u.typ_y == PACT_POKE); #endif - // XX forwarding wrong, need a PIT entry if ( c3n == our_o ) { - u3_peer* per_u = _mesa_get_peer(sam_u, her_d); - if ( per_u == NULL ) { - // XX leaks - u3l_log("mesa: alien forward for %s", u3r_string(u3dc("scot", c3__p, u3i_chubs(2, her_d)))); - _mesa_free_pict(pic_u); - return; - } - if ( c3y == sam_u->for_o && sam_u->pir_u->who_d[0] == per_u->imp_y ) { -//#ifdef MESA_DEBUG - u3_lane lin_u = _mesa_get_direct_lane(sam_u, her_d); -//#endif - u3l_log("sending peek %u", pac_u->pek_u.nam_u.fra_w); - //_update_hopcount(&pac_u->hed_u); - _mesa_send(pic_u, &lin_u); - } - _mesa_free_pict(pic_u); + _mesa_forward_request(sam_u, pic_u); return; } @@ -2656,6 +2657,7 @@ _mesa_io_talk(u3_auto* car_u) u3_auto* u3_mesa_io_init(u3_pier* pir_u) { + u3l_log("mesa: INIT"); u3_mesa* sam_u = c3_calloc(sizeof(*sam_u)); sam_u->pir_u = pir_u; From 2c4f58519a78e741f1e15c46f4727ffa26c6c59f Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Tue, 9 Jul 2024 16:48:51 -0400 Subject: [PATCH 213/430] mesa: more incremental routing cleanup; still needs next hop in pages --- pkg/vere/io/mesa.c | 118 ++++++++++++++++++++++------------------ pkg/vere/io/mesa/mesa.h | 3 + pkg/vere/io/mesa/pact.c | 11 ++-- 3 files changed, 74 insertions(+), 58 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 818ef1d1f9..522089c162 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -651,6 +651,12 @@ _mesa_get_direct_lane_raw(u3_mesa* sam_u, u3_noun her) return per_u->dan_u; } +static c3_o +_mesa_lanes_equal(u3_lane* lan_u, u3_lane* lon_u) +{ + return __((lan_u->pip_w == lon_u->pip_w) && (lan_u->por_s == lon_u->por_s)); +} + static u3_lane _mesa_get_direct_lane(u3_mesa* sam_u, c3_d her_d[2]) { @@ -1237,10 +1243,13 @@ _realise_lane(u3_noun lan) { return lan_u; } -static c3_o -_mesa_rout_bufs(u3_mesa* sam_u, c3_y* buf_y, c3_w len_w, u3_noun las) +static void +_mesa_rout_bufs(u3_mesa* sam_u, + u3_peer* per_u, // null for response packets + c3_y* buf_y, + c3_w len_w, + u3_noun las) { - c3_o suc_o = c3n; u3_noun lan, t = las; // u3l_log("sending to ip: %x, port: %u", lan_u.pip_w, lan_u.por_s); while ( t != u3_nul ) { @@ -1258,10 +1267,12 @@ _mesa_rout_bufs(u3_mesa* sam_u, c3_y* buf_y, c3_w len_w, u3_noun las) c3_y* sen_y = c3_calloc(len_w); memcpy(sen_y, buf_y, len_w); _mesa_send_buf(sam_u, lan_u, sen_y, len_w); + if ( per_u && (c3y == _mesa_lanes_equal(&lan_u, &per_u->dan_u)) ) { + per_u->dir_u.sen_d = _get_now_micros(); + } } } - // u3z(las); - return suc_o; + // u3z(las);// TODO is this ok? } static void @@ -1403,17 +1414,28 @@ _mesa_queue_czar(u3_mesa* sam_u, u3_noun las, u3_noun pac) static void _mesa_ef_send(u3_mesa* sam_u, u3_noun las, u3_noun pac) { - // u3m_p("pac", pac); - /* u3m_p("las", las); */ las = _mesa_queue_czar(sam_u, las, u3k(pac)); - /* u3m_p("las", las); */ c3_w len_w = u3r_met(3, pac); c3_y* buf_y = c3_calloc(len_w); u3r_bytes(0, len_w, buf_y, pac); + + u3_mesa_head hed_u; + mesa_sift_head(buf_y, &hed_u); + + u3_peer* per_u; + if ( (PACT_PEEK == hed_u.typ_y) || (PACT_POKE == hed_u.typ_y) ) { + u3_mesa_pact pac_u; + c3_w res_w = mesa_sift_pact(&pac_u, buf_y, len_w); + c3_d her_d[2]; + _get_her(&pac_u, her_d); + per_u = _mesa_get_peer(sam_u, her_d); + } else { // treat PACT_RESV as an opaque response + per_u = NULL; + } + sam_u->tim_d = _get_now_micros(); - c3_o suc_o = c3n; - _mesa_rout_bufs(sam_u, buf_y, len_w, las); + _mesa_rout_bufs(sam_u, per_u, buf_y, len_w, las); c3_free(buf_y); u3z(pac); @@ -1648,7 +1670,7 @@ _mesa_try_forward(u3_mesa_pict* pic_u, u3_noun fra, u3_noun hit) } static c3_w -_mesa_respond(u3_mesa_pict* req_u, c3_y** buf_y, u3_noun hit) +_mesa_fill_buf(u3_mesa_pict* req_u, c3_y** buf_y, u3_noun hit) { c3_w len_w = u3r_met(3, hit); @@ -1683,12 +1705,12 @@ _mesa_page_scry_cb(void* vod_p, u3_noun nun) } else { u3_noun tag; u3_noun dat; - u3x_cell(u3k(old), &tag, &dat); + u3x_cell(old, &tag, &dat); if ( MESA_WAIT == tag ) { c3_y* buf_y; - c3_w len_w = _mesa_respond(pic_u, &buf_y, u3k(hit)); - _mesa_rout_bufs(sam_u, buf_y, len_w, u3k(u3t(dat))); + c3_w len_w = _mesa_fill_buf(pic_u, &buf_y, u3k(hit)); + _mesa_rout_bufs(sam_u, NULL, buf_y, len_w, u3k(u3t(dat))); } _mesa_put_cache(sam_u, &pac_u->pek_u.nam_u, u3nc(MESA_ITEM, u3k(hit))); // u3z(old); @@ -1724,7 +1746,7 @@ _mesa_page_scry_hunk_cb(void* vod_p, u3_noun nun) } else { u3_noun tag; u3_noun dat; - u3x_cell(u3k(old), &tag, &dat); + u3x_cell(old, &tag, &dat); c3_y* buf_y; // u3m_p("hit", u3a_is_cell(hit)); @@ -1746,13 +1768,13 @@ _mesa_page_scry_hunk_cb(void* vod_p, u3_noun nun) } /* if (len_w == 0) { */ - /* c3_w lun_w = _mesa_respond(pic_u, &buf_y, u3k(u3h(hit))); */ + /* c3_w lun_w = _mesa_fill_buf(pic_u, &buf_y, u3k(u3h(hit))); */ /* /\* pac_u->pek_u.nam_u.fra_w = fra_w; *\/ */ /* _mesa_rout_bufs(sam_u, buf_y, lun_w, u3k(u3t(dat))); */ /* } */ - c3_w lun_w = _mesa_respond(pic_u, &buf_y, u3k(u3h(hit))); - _mesa_rout_bufs(sam_u, buf_y, lun_w, u3k(u3t(dat))); + c3_w lun_w = _mesa_fill_buf(pic_u, &buf_y, u3k(u3h(hit))); + _mesa_rout_bufs(sam_u, NULL, buf_y, lun_w, u3k(u3t(dat))); /* u3l_log("putting %u", pac_u->pek_u.nam_u.fra_w); */ /* _log_pact(pac_u); */ @@ -1821,8 +1843,8 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun nun) _mesa_put_cache(sam_u, &pac_u->pek_u.nam_u, u3nc(MESA_ITEM, u3k(lin))); - c3_w lun_w = _mesa_respond(pic_u, &buf_y, u3k(lin)); - _mesa_rout_bufs(sam_u, buf_y, lun_w, u3k(u3t(dat))); + c3_w lun_w = _mesa_fill_buf(pic_u, &buf_y, u3k(lin)); + _mesa_rout_bufs(sam_u, NULL, buf_y, lun_w, u3k(u3t(dat))); /* u3l_log("putting %u", pac_u->pek_u.nam_u.fra_w); */ /* _log_pact(pac_u); */ @@ -1894,40 +1916,32 @@ _hear_peer(u3_mesa* sam_u, u3_peer* per_u, u3_lane lan_u, c3_o dir_o) } static void -_mesa_forward(u3_mesa_pict* pic_u, u3_lane lan_u) +_mesa_forward_response(u3_mesa_pict* pic_u, u3_lane lan_u) { u3l_log("forwarding"); u3_mesa_pact* pac_u = &pic_u->pac_u; u3_mesa* sam_u = pic_u->sam_u; - c3_d her_d[2]; - _get_her(pac_u, her_d); // XX wrong - // XX: revive - //_update_hopcount(&pac_u->hed_u); - - if ( pac_u->hed_u.typ_y == PACT_PAGE ) { - //u3l_log("should update next hop"); - u3_weak hit = _mesa_get_cache(sam_u, &pac_u->pag_u.nam_u); - if ( u3_none == hit ) { - MESA_LOG(APATHY); - return; - } - u3_noun tag, dat; - u3x_cell(u3k(hit), &tag, &dat); - if ( tag == MESA_WAIT ) { - c3_y* buf_y = c3_calloc(PACT_SIZE); - c3_w len_w = mesa_etch_pact(buf_y, pac_u); - _mesa_rout_bufs(sam_u, buf_y, len_w, u3k(u3t(dat))); - } else { - u3l_log("mesa: weird pending interest"); - } + update_hopcount(&pac_u->hed_u); + + if ( PACT_PAGE != pac_u->hed_u.typ_y ) { + MESA_LOG(STRANGE); + return; + } + //u3l_log("should update next hop"); + u3_weak hit = _mesa_get_cache(sam_u, &pac_u->pag_u.nam_u); + if ( u3_none == hit ) { + MESA_LOG(APATHY); + return; + } + u3_noun tag, dat; + u3x_cell(hit, &tag, &dat); + if ( tag == MESA_WAIT ) { + c3_y* buf_y = c3_calloc(PACT_SIZE); + // TODO actually stick next hop in packet + c3_w len_w = mesa_etch_pact(buf_y, pac_u); + _mesa_rout_bufs(sam_u, NULL, buf_y, len_w, u3k(u3t(dat))); } else { - u3_peer* per_u = _mesa_get_peer(sam_u, her_d); - if ( NULL != per_u ) { - u3_lane lin_u = _mesa_get_direct_lane(sam_u, her_d); - if ( lin_u.pip_w != 0 ) { - _mesa_send(pic_u, &lin_u); - } - } + u3l_log("mesa: weird pending interest"); } mesa_free_pact(pac_u); } @@ -2052,7 +2066,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) // forwarding wrong, need a PIT entry // if ( c3n == our_o ) { - // _mesa_forward(pic_u, lan_u); + // _mesa_forward_response(pic_u, lan_u); // _mesa_free_pict(pic_u); // return; // } @@ -2083,7 +2097,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) // XX better check to forward pages? if ( u3_none != hit ) { - _mesa_forward(pic_u, lan_u); + _mesa_forward_response(pic_u, lan_u); _mesa_free_pict(pic_u); u3z(hit); return; @@ -2347,7 +2361,7 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) u3_noun tag_2, dat_2; u3x_cell(hit_2, &tag_2, &dat_2); /* u3l_log("cache hit %u bat %u", fra_w, bat_w); */ - c3_w len_w = _mesa_respond(pic_u, &buf_y, u3k(dat_2)); + c3_w len_w = _mesa_fill_buf(pic_u, &buf_y, u3k(dat_2)); // _log_buf(buf_y, len_w); _mesa_send_buf(sam_u, lan_u, buf_y, len_w); u3z(hit_2); diff --git a/pkg/vere/io/mesa/mesa.h b/pkg/vere/io/mesa/mesa.h index 52c7a65cda..a5e08292cf 100644 --- a/pkg/vere/io/mesa/mesa.h +++ b/pkg/vere/io/mesa/mesa.h @@ -151,9 +151,12 @@ typedef struct _u3_mesa_pact { }; } u3_mesa_pact; +c3_o mesa_sift_head(c3_y buf_y[8], u3_mesa_head* hed_u); c3_w mesa_sift_pact(u3_mesa_pact* pac_u, c3_y* buf_y, c3_w len_w); c3_w mesa_etch_pact(c3_y* buf_y, u3_mesa_pact* pac_u); +void update_hopcount(u3_mesa_head*); + void mesa_free_pact(u3_mesa_pact* pac_u); void _log_pact(u3_mesa_pact* pac_u); diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index 3ad2188d67..88e175bb5f 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -174,8 +174,8 @@ _log_pact(u3_mesa_pact* pac_u) /* Helper utilities */ -static void -_update_hopcount(u3_mesa_head* hed_u) +void +update_hopcount(u3_mesa_head* hed_u) // TODO rename, _inc_hopcount()? { hed_u->hop_y = c3_max(hed_u->hop_y+1, 7); } @@ -294,12 +294,11 @@ _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); } -static c3_o -_mesa_sift_head(c3_y buf_y[8], u3_mesa_head* hed_u) +c3_o +mesa_sift_head(c3_y buf_y[8], u3_mesa_head* hed_u) { if ( memcmp(buf_y + 4, &MESA_COOKIE, MESA_COOKIE_LEN) ) { return c3n; - } c3_w hed_w = _ames_sift_word(buf_y); @@ -573,7 +572,7 @@ mesa_sift_pact(u3_mesa_pact* pac_u, c3_y* buf_y, c3_w len_w) u3l_log("mesa: attempted to parse overly short packet of size %u", len_w); } - _mesa_sift_head(buf_y, &pac_u->hed_u); + mesa_sift_head(buf_y, &pac_u->hed_u); buf_y += 8; len_w -= 8; From 852a1fba0bee15a0769839e12af785734c016adf Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Tue, 9 Jul 2024 18:09:06 -0400 Subject: [PATCH 214/430] mesa: WIP refactoring, separating PIT and cache; compiles but not sane --- pkg/vere/io/mesa.c | 103 +++++++++++++++++++++------------------------ 1 file changed, 49 insertions(+), 54 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 522089c162..e36578640d 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -157,6 +157,7 @@ typedef struct _u3_mesa { u3p(u3h_root) her_p; // (map ship u3_peer) u3p(u3h_root) pac_p; // packet cache u3p(u3h_root) lan_p; // lane cache + u3p(u3h_root) pit_p; // (map path [our=? las=(set lane)]) u3_czar_info imp_u[256]; // galaxy information c3_c* dns_c; // turf (urb.otrg) c3_d tim_d; // XX: remove @@ -1638,35 +1639,42 @@ _name_to_jumbo_scry(u3_mesa_name* nam_u, c3_w lop_w, c3_w len_w) return res; } -/* - * RETAIN - */ static u3_weak -_mesa_get_cache(u3_mesa* sam_u, u3_mesa_name* nam_u) +_mesa_get_pit(u3_mesa* sam_u, u3_mesa_name* nam_u) +{ + return u3h_get(sam_u->pit_p, _name_to_scry((nam_u))); +} + +// RETAIN +static void +_mesa_put_pit(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_noun val) { u3_noun pax = _name_to_scry(nam_u); - u3_weak res = u3h_get(sam_u->pac_p, pax); - if ( u3_none == res ) { - //u3m_p("miss", u3k(pax)); - } else { - //u3m_p("hit", u3nc(u3k(pax), u3k(res))); - } - return res; + u3h_put(sam_u->pit_p, pax, u3k(val)); + u3z(pax); } static void -_mesa_put_cache(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_noun val) +_mesa_del_pit(u3_mesa* sam_u, u3_mesa_name* nam_u) { u3_noun pax = _name_to_scry(nam_u); - u3h_put(sam_u->pac_p, pax, u3k(val)); - u3z(pax); // TODO: fix refcount + u3h_del(sam_u->pit_p, pax); + u3z(pax); } +static u3_weak +_mesa_get_cache(u3_mesa* sam_u, u3_mesa_name* nam_u) +{ + return u3h_get(sam_u->pac_p, _name_to_scry(nam_u)); +} + +// RETAIN static void -_mesa_try_forward(u3_mesa_pict* pic_u, u3_noun fra, u3_noun hit) +_mesa_put_cache(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_noun val) { - u3l_log(""); - u3l_log("stubbed forwarding"); + u3_noun pax = _name_to_scry(nam_u); + u3h_put(sam_u->pac_p, pax, u3k(val)); + u3z(pax); } static c3_w @@ -1677,64 +1685,52 @@ _mesa_fill_buf(u3_mesa_pict* req_u, c3_y** buf_y, u3_noun hit) *buf_y = c3_calloc(len_w); u3r_bytes(0, len_w, *buf_y, hit); - //u3z(hit); + u3z(hit); return len_w; } -/* - */ static void -_mesa_page_scry_cb(void* vod_p, u3_noun nun) +_mesa_page_scry_cb(void* vod_p, u3_noun res) { u3_mesa_pict* pic_u = vod_p; u3_mesa_pact* pac_u = &pic_u->pac_u; u3_mesa* sam_u = pic_u->sam_u; - //u3_noun pax = _mesa_path_with_fra(pac_u->pek_u.nam_u.pat_c, &fra_s); - u3_weak hit = u3r_at(7, nun); - if ( u3_none == hit ) { - // TODO: mark as dead - //u3z(nun); + u3_weak pag = u3r_at(7, res); + if ( u3_none == pag ) { u3l_log("unbound"); - + _mesa_del_pit(sam_u, &pac_u->pag_u.nam_u); } else { - u3_weak old = _mesa_get_cache(sam_u, &pac_u->pag_u.nam_u); - if ( old == u3_none ) { - u3l_log("bad"); + u3_weak pit = _mesa_get_pit(sam_u, &pac_u->pag_u.nam_u); + if ( pit == u3_none ) { + u3l_log("no pit"); MESA_LOG(APATHY); } else { - u3_noun tag; - u3_noun dat; - u3x_cell(old, &tag, &dat); - if ( MESA_WAIT == tag ) { - c3_y* buf_y; - - c3_w len_w = _mesa_fill_buf(pic_u, &buf_y, u3k(hit)); - _mesa_rout_bufs(sam_u, NULL, buf_y, len_w, u3k(u3t(dat))); - } - _mesa_put_cache(sam_u, &pac_u->pek_u.nam_u, u3nc(MESA_ITEM, u3k(hit))); - // u3z(old); + c3_y* buf_y; + c3_w len_w = _mesa_fill_buf(pic_u, &buf_y, u3k(pag)); + u3_noun our, las; + u3x_cell(pit, &our, &las); + _mesa_rout_bufs(sam_u, NULL, buf_y, len_w, u3k(las)); + _mesa_put_cache(sam_u, &pac_u->pek_u.nam_u, u3k(pag)); } - // u3z(hit); + u3z(pit); } - // u3z(pax); + u3z(res); } /* */ static void -_mesa_page_scry_hunk_cb(void* vod_p, u3_noun nun) +_mesa_page_scry_hunk_cb(void* vod_p, u3_noun res) { u3_mesa_pict* pic_u = vod_p; u3_mesa_pact* pac_u = &pic_u->pac_u; u3_mesa* sam_u = pic_u->sam_u; - //u3_noun pax = _mesa_path_with_fra(pac_u->pek_u.nam_u.pat_c, &fra_s); - u3_weak hit = u3r_at(7, nun); - if ( u3_none == hit ) { - // TODO: mark as dead - //u3z(nun); - u3l_log("unbound"); + u3_weak pag = u3r_at(7, res); + if ( u3_none == pag ) { + u3l_log("unbound hunk"); + _mesa_del_pit(sam_u, &pac_u->pag_u.nam_u); } else { c3_w fra_w = pac_u->pek_u.nam_u.fra_w; c3_w bat_w = _mesa_lop(fra_w); @@ -1753,7 +1749,7 @@ _mesa_page_scry_hunk_cb(void* vod_p, u3_noun nun) c3_w len_w = bat_w; /* u3l_log("path %s", pac_u->pek_u.nam_u.pat_c); */ c3_w i = 0; - while ( u3_nul != hit ) { + while ( u3_nul != pag ) { // u3_noun key = u3nc(u3k(pax), u3i_word(lop_w)); // u3h_put(sam_u->fin_s.sac_p, key, u3k(u3h(lis))); @@ -2675,10 +2671,11 @@ u3_mesa_io_init(u3_pier* pir_u) u3_mesa* sam_u = c3_calloc(sizeof(*sam_u)); sam_u->pir_u = pir_u; - // XX config + // XX tune cache sizes sam_u->her_p = u3h_new_cache(100000); sam_u->lan_p = u3h_new_cache(100000); sam_u->pac_p = u3h_new_cache(100000); + sam_u->pit_p = u3h_new_cache(10000); u3_assert( !uv_udp_init(u3L, &sam_u->wax_u) ); sam_u->wax_u.data = sam_u; @@ -2686,8 +2683,6 @@ u3_mesa_io_init(u3_pier* pir_u) sam_u->sil_u = u3s_cue_xeno_init(); sam_u->tes_u = ur_cue_test_init(); - - // Disable networking for fake ships // if ( c3y == sam_u->pir_u->fak_o ) { From 695d3684023f7f0b0ae9430407f2cbe8db3fc9e5 Mon Sep 17 00:00:00 2001 From: midden-fabler Date: Tue, 9 Jul 2024 18:39:18 -0400 Subject: [PATCH 215/430] http: consolidate slicing & content-range logic --- pkg/vere/io/http.c | 111 ++++++++++++++++++--------------------------- 1 file changed, 44 insertions(+), 67 deletions(-) diff --git a/pkg/vere/io/http.c b/pkg/vere/io/http.c index 3262a899c5..f22b86679e 100644 --- a/pkg/vere/io/http.c +++ b/pkg/vere/io/http.c @@ -7,9 +7,6 @@ #include "openssl/err.h" #include "openssl/ssl.h" #include "version.h" -#include -#include -#include typedef struct _u3_h2o_serv { h2o_globalconf_t fig_u; // h2o global config @@ -703,6 +700,7 @@ _find_tis_fas(void* txt, c3_w len) // [x] test stream // [x] content range function // [x] video controls +// [ ] fix repeated 1 byte requests // [ ] multipart ranges? // typedef struct _range_header { @@ -710,47 +708,67 @@ typedef struct _range_header { c3_z end_z; } range_header; -static u3_noun +typedef struct _content { + c3_z start_z; + c3_z end_z; + u3_noun dat; +} content; + +static content _slice_mime(range_header rng, u3_noun octs) { c3_w lent_w = u3h(octs); c3_w oct_w = u3t(octs); - u3_noun out; + content out; if ( rng.start_z == SIZE_MAX ) { - if ( rng.end_z == SIZE_MAX) { + if ( rng.end_z == SIZE_MAX ) { // [~ ~] - out = u3_nul; + out.start_z = SIZE_MAX; + out.end_z = SIZE_MAX; + out.dat = u3_nul; } else { // [~ @] if ( rng.end_z > lent_w ) { - out = u3_nul; + out.start_z = SIZE_MAX; + out.end_z = SIZE_MAX; + out.dat = u3_nul; } else { // slice last bytes - out = u3nc( rng.end_z, - u3qc_cut(3, lent_w - rng.end_z, rng.end_z, oct_w)); + out.start_z = lent_w - rng.end_z; + out.end_z = rng.end_z; + out.dat = u3nc(rng.end_z, + u3qc_cut(3, out.start_z, out.end_z, oct_w)); } } } else if ( rng.end_z == SIZE_MAX ) { // [@ ~] if ( rng.start_z > lent_w ) { - out = u3_nul; + out.start_z = SIZE_MAX; + out.end_z = SIZE_MAX; + out.dat = u3_nul; } else { - out = u3nc( lent_w - rng.start_z, - u3qc_cut(3, rng.start_z, lent_w, oct_w)); + out.start_z = rng.start_z; + out.end_z = lent_w; + out.dat = u3nc(lent_w - rng.start_z, + u3qc_cut(3, rng.start_z, rng.end_z, oct_w)); } } else if (rng.end_z > lent_w) { - out = u3_nul; + out.start_z = SIZE_MAX; + out.end_z = SIZE_MAX; + out.dat = u3_nul; } else { // [@ @] - out = u3nc( (rng.end_z - rng.start_z) + 1, - u3qc_cut(3, rng.start_z, (rng.end_z - rng.start_z) + 1, oct_w)); + out.start_z = rng.start_z; + out.end_z = (rng.end_z - rng.start_z) + 1; + out.dat = u3nc((rng.end_z - rng.start_z) + 1, + u3qc_cut(3, out.start_z, out.end_z, oct_w)); } return out; } @@ -767,65 +785,24 @@ _get_range(c3_c* txt_c, c3_w len_w) slice.start_z = h2o_strtosize(txt_c, hep_c - txt_c); slice.end_z = h2o_strtosize(hep_c + 1, len_w - ((hep_c + 1) - txt_c)); // strange -> [SIZE_MAX SIZE_MAX] so we return u3_nul in _slice_mime - if ( ((hep_c != txt_c) && slice.start_z == SIZE_MAX ) || - ( len_w - ((hep_c + 1) - txt_c) > 0 ) && slice.end_z == SIZE_MAX ) { + if ( ((hep_c != txt_c) && (slice.start_z == SIZE_MAX)) || + ((len_w - ((hep_c + 1) - txt_c) > 0) && (slice.end_z == SIZE_MAX)) ) { slice.start_z = SIZE_MAX; slice.end_z = SIZE_MAX; } } - return slice; } + static u3_noun -_content_rng(range_header rng, c3_w lent_w) +_content_rng(c3_z start_z, c3_z end_z, c3_w lent_w) { u3_noun out; - u3_atom start; - u3_atom end; - - if ( rng.start_z == SIZE_MAX ) { - if ( rng.end_z == SIZE_MAX) { - // [~ ~] - out = u3_nul; - } - else { - // [~ @] - if ( rng.end_z > lent_w ) { - out = u3_nul; - return out; - } - else { - // last bytes - start = lent_w - rng.end_z; - end = rng.end_z; - } - } - } - else if ( rng.end_z == SIZE_MAX ) { - // [@ ~] - if ( rng.start_z > lent_w ) { - out = u3_nul; - return out; - } - else { - start = rng.start_z; - end = lent_w; - } - } - else if (rng.end_z > lent_w) { - out = u3_nul; - return out; - } - else { - // [@ @] - start = rng.start_z; - end = rng.end_z; - } u3_noun lin = u3i_list(u3i_string("bytes "), - u3do("crip", u3do("a-co:co", start)), + u3do("crip", u3do("a-co:co", start_z)), c3_s1('-'), - u3do("crip", u3do("a-co:co", end)), + u3do("crip", u3do("a-co:co", end_z)), c3_s1('/'), u3do("crip", u3do("a-co:co", ++lent_w )), u3_none); @@ -996,9 +973,9 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) h2o_send_error_generic(req_u->rec_u, 416, msg_c, msg_c, 0); return; } - u3_noun result = _slice_mime(rng_hed, octs); + content result = _slice_mime(rng_hed, octs); - if ( result == u3_nul) { + if ( result.dat == u3_nul) { c3_c* msg_c = "Requested Range Not Satisfiable"; h2o_send_error_generic(req_u->rec_u, 416, msg_c, msg_c, 0); return; @@ -1009,8 +986,8 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) // } // else { // 206 - u3_noun con_rng_hed = _content_rng(rng_hed, u3h(octs)); - u3_noun res = u3i_edit(nac, 127, result); + u3_noun con_rng_hed = _content_rng(result.start_z, result.end_z, u3h(octs)); + u3_noun res = u3i_edit(nac, 127, result.dat); res = u3i_edit(res, 124, 206); res = u3i_edit(res, 125, u3nc(con_rng_hed, u3r_at(125, res))); _http_cache_respond(req_u, res); From d7af826dcc33f3ba6ddc3aeaa4edce3be0c9dc38 Mon Sep 17 00:00:00 2001 From: pkova Date: Wed, 10 Jul 2024 15:16:45 +0300 Subject: [PATCH 216/430] mesa: working jumbo frames --- pkg/vere/io/mesa.c | 71 ++++++++++++++++++++++++++-------------------- 1 file changed, 41 insertions(+), 30 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 23ee645c08..a58dd012cf 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -967,13 +967,12 @@ _mesa_burn_misorder_queue(u3_pend_req* req_u) static u3_pend_req* _mesa_req_pact_done(u3_mesa* sam_u, u3_mesa_name *nam_u, u3_mesa_data* dat_u, u3_lane* lan_u) { - u3l_log("rec_pact_done"); u3_weak ret = u3_none; c3_d now_d = _get_now_micros(); u3_pend_req* req_u = _mesa_get_request(sam_u, nam_u); if ( NULL == req_u ) { - MESA_LOG(APATHY); + /* MESA_LOG(APATHY); */ return NULL; } @@ -1001,7 +1000,7 @@ _mesa_req_pact_done(u3_mesa* sam_u, u3_mesa_name *nam_u, u3_mesa_data* dat_u, u3 // received duplicate if ( c3n == bitset_has(&req_u->was_u, nam_u->fra_w) ) { - MESA_LOG(DUPE); + /* MESA_LOG(DUPE); */ req_u->wat_u[nam_u->fra_w].dup_y++; return req_u; } @@ -1018,7 +1017,6 @@ _mesa_req_pact_done(u3_mesa* sam_u, u3_mesa_name *nam_u, u3_mesa_data* dat_u, u3 } req_u->len_w++; - u3l_log("fragment %u len %u", nam_u->fra_w, req_u->len_w); if ( req_u->lef_w == nam_u->fra_w ) { req_u->lef_w++; } @@ -1640,8 +1638,7 @@ _mesa_page_scry_cb(void* vod_p, u3_noun nun) } else { u3_weak old = _mesa_get_cache(sam_u, &pac_u->pag_u.nam_u); if ( old == u3_none ) { - u3l_log("bad"); - MESA_LOG(APATHY); + /* MESA_LOG(APATHY); */ } else { u3_noun tag; u3_noun dat; @@ -1679,10 +1676,9 @@ _mesa_page_scry_hunk_cb(void* vod_p, u3_noun nun) c3_w fra_w = pac_u->pek_u.nam_u.fra_w; c3_w bat_w = _mesa_lop(fra_w); pac_u->pek_u.nam_u.fra_w = bat_w; - u3_weak old = _mesa_get_cache(sam_u, &pac_u->pag_u.nam_u); + u3_weak old = _mesa_get_jumbo_cache(sam_u, &pac_u->pag_u.nam_u); if ( old == u3_none ) { - u3l_log("bad"); - MESA_LOG(APATHY); + /* MESA_LOG(APATHY); */ } else { u3_noun tag; u3_noun dat; @@ -1693,6 +1689,7 @@ _mesa_page_scry_hunk_cb(void* vod_p, u3_noun nun) c3_w len_w = bat_w; /* u3l_log("path %s", pac_u->pek_u.nam_u.pat_c); */ c3_w i = 0; + _mesa_put_jumbo_cache(sam_u, &pac_u->pek_u.nam_u, u3nc(MESA_ITEM, 0)); while ( u3_nul != hit ) { // u3_noun key = u3nc(u3k(pax), u3i_word(lop_w)); // u3h_put(sam_u->fin_s.sac_p, key, u3k(u3h(lis))); @@ -1700,7 +1697,15 @@ _mesa_page_scry_hunk_cb(void* vod_p, u3_noun nun) pac_u->pek_u.nam_u.fra_w = len_w; if ( (bat_w == 0) && (i == 0) ) { + u3_mesa_pact tac_u = {0}; + + c3_w lun_w = u3r_met(3, u3h(hit)); + c3_y* baf_y = c3_calloc(lun_w); + + u3r_bytes(0, lun_w, baf_y, u3h(hit)); pac_u->pek_u.nam_u.nit_o = c3y; + mesa_sift_pact(&tac_u, baf_y, lun_w); + /* pac_u->pek_u.nam_u.aut_o = c3y; */ } else { pac_u->pek_u.nam_u.nit_o = c3n; @@ -1714,6 +1719,7 @@ _mesa_page_scry_hunk_cb(void* vod_p, u3_noun nun) /* } */ c3_w lun_w = _mesa_respond(pic_u, &buf_y, u3k(u3h(hit))); + _mesa_rout_bufs(sam_u, buf_y, lun_w, u3k(u3t(dat))); /* u3l_log("putting %u", pac_u->pek_u.nam_u.fra_w); */ @@ -1749,7 +1755,7 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun nun) u3_weak hit = u3r_at(7, nun); /* u3_noun proof = u3h(hit); */ /* u3m_p("proof", proof); */ - hit = u3t(hit); + /* hit = u3t(hit); */ if ( u3_none == hit ) { // TODO: mark as dead @@ -1759,64 +1765,74 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun nun) c3_w fra_w = pac_u->pek_u.nam_u.fra_w; u3_weak old = _mesa_get_jumbo_cache(sam_u, &pac_u->pag_u.nam_u); if ( old == u3_none ) { - u3l_log("bad"); - MESA_LOG(APATHY); + /* MESA_LOG(APATHY); */ } else { u3_noun tag; u3_noun dat; u3x_cell(u3k(old), &tag, &dat); - c3_y* out_y; /* u3l_log("path %s", pac_u->pek_u.nam_u.pat_c); */ u3_mesa_pact tac_u = {0}; c3_w len_w = u3r_met(3, hit); u3l_log("len_w %u", len_w); + c3_y* puf_y = c3_calloc(len_w); u3r_bytes(0, len_w, puf_y, hit); mesa_sift_pact(&tac_u, puf_y, len_w); _mesa_put_jumbo_cache(sam_u, &tac_u.pag_u.nam_u, u3nc(MESA_ITEM, 0)); + + if (len_w <= 1024) { + tac_u.pag_u.nam_u.boq_y = 13; + c3_y baf_y[PACT_SIZE]; + c3_w lon_w = mesa_etch_pact(baf_y, &tac_u); + u3_atom lon = u3i_bytes(lon_w, baf_y); + _mesa_put_cache(sam_u, &tac_u.pag_u.nam_u, u3nc(MESA_ITEM, u3k(lon))); + _mesa_rout_bufs(sam_u, baf_y, lon_w, u3k(u3t(dat))); + return; + } + + /* u3l_log("jumbo frame"); */ /* _log_pact(&tac_u); */ u3_atom jumbo = u3i_bytes(len_w, tac_u.pag_u.dat_u.fra_y); c3_w siz_w = u3r_met(13, jumbo); + c3_w leaves_w = siz_w; lss_builder bil_u; lss_builder_init(&bil_u, leaves_w); for ( c3_w i = 0; i < leaves_w; i++ ) { - c3_y* leaf_y = puf_y + (i*1024); - c3_w leaf_w = (i < leaves_w - 1) ? 1024 : len_w % 1024; + c3_y* leaf_y = tac_u.pag_u.dat_u.fra_y + (i*1024); + c3_w leaf_w = (i < leaves_w - 1) ? 1024 : tac_u.pag_u.dat_u.len_w % 1024; lss_builder_ingest(&bil_u, leaf_y, leaf_w); } lss_hash* proof = lss_builder_finalize(&bil_u); c3_w saz_w = lss_proof_size(leaves_w); - tac_u.pag_u.nam_u.boq_y = 13; tac_u.pag_u.dat_u.tot_w = siz_w; if ( c3y == tac_u.pag_u.nam_u.nit_o && leaves_w > 4) { memcpy(tac_u.pag_u.dat_u.fra_y, proof, saz_w*32); tac_u.pag_u.dat_u.len_w = saz_w*32; - /* c3_w off_w = 0; */ - c3_y baf_y[PACT_SIZE]; c3_w lon_w = mesa_etch_pact(baf_y, &tac_u); - u3_atom lon = u3i_bytes(len_w, baf_y); + u3_atom lon = u3i_bytes(lon_w, baf_y); _mesa_put_cache(sam_u, &tac_u.pag_u.nam_u, u3nc(MESA_ITEM, u3k(lon))); _mesa_rout_bufs(sam_u, baf_y, lon_w, u3k(u3t(dat))); } + tac_u.pag_u.nam_u.nit_o = c3n; for (c3_w i=0; i < siz_w; i++) { // (cut 3 [wid 1] dat.byts)) @@ -1854,10 +1870,6 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun nun) _mesa_put_cache(sam_u, &tac_u.pag_u.nam_u, u3nc(MESA_ITEM, u3k(lan))); - /* c3_w lun_w = _mesa_respond(pic_u, &out_y, u3k(lan)); */ - - /* u3l_log("out_y"); */ - /* mesa_sift_pact(&muna, out_y, lun_w); */ /* _log_buf(out_y, tac_u.pag_u.dat_u.len_w); */ @@ -1951,7 +1963,7 @@ _mesa_forward(u3_mesa_pict* pic_u, u3_lane lan_u) //u3l_log("should update next hop"); u3_weak hit = _mesa_get_cache(sam_u, &pac_u->pag_u.nam_u); if ( u3_none == hit ) { - MESA_LOG(APATHY); + /* MESA_LOG(APATHY); */ return; } u3_noun tag, dat; @@ -2085,7 +2097,7 @@ static void _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) { #ifdef MESA_DEBUG - u3l_log("mesa hear page %u", pic_u->pac_u.pag_u.nam_u.fra_w); + /* u3l_log("mesa hear page %u", pic_u->pac_u.pag_u.nam_u.fra_w); */ #endif /* _log_buf(pic_u->pac_u.pag_u.dat_u.fra_y, pic_u->pac_u.pag_u.dat_u.len_w); */ @@ -2387,9 +2399,9 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) } else { _mesa_add_lane_to_cache(sam_u, &pac_u->pek_u.nam_u, u3_nul, lan_u); // TODO: retrieve from namespace if ( c3y == our_o ) { - // u3_noun sky = _name_to_batch_scry(&pac_u->pek_u.nam_u, - // bat_w, - // bat_w + MESA_HUNK); + /* u3_noun sky = _name_to_batch_scry(&pac_u->pek_u.nam_u, */ + /* bat_w, */ + /* bat_w + MESA_HUNK); */ u3_noun sky = _name_to_jumbo_scry(&pac_u->pek_u.nam_u); @@ -2575,8 +2587,7 @@ _mesa_hear(u3_mesa* sam_u, } } -static void -_mesa_recv_cb(uv_udp_t* wax_u, +static void _mesa_recv_cb(uv_udp_t* wax_u, ssize_t nrd_i, const uv_buf_t * buf_u, const struct sockaddr* adr_u, From 402593daefa8a86d353547637a60b4cac4e0485f Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Wed, 10 Jul 2024 12:57:19 -0400 Subject: [PATCH 217/430] mesa: actually compiles, still not sane --- pkg/vere/io/mesa.c | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index e36578640d..528da1a921 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1695,13 +1695,14 @@ _mesa_page_scry_cb(void* vod_p, u3_noun res) u3_mesa_pict* pic_u = vod_p; u3_mesa_pact* pac_u = &pic_u->pac_u; u3_mesa* sam_u = pic_u->sam_u; + u3_mesa_name* nam_u = &pac_u->pek_u.nam_u; u3_weak pag = u3r_at(7, res); if ( u3_none == pag ) { u3l_log("unbound"); - _mesa_del_pit(sam_u, &pac_u->pag_u.nam_u); + _mesa_del_pit(sam_u, nam_u); } else { - u3_weak pit = _mesa_get_pit(sam_u, &pac_u->pag_u.nam_u); + u3_weak pit = _mesa_get_pit(sam_u, nam_u); if ( pit == u3_none ) { u3l_log("no pit"); MESA_LOG(APATHY); @@ -1711,7 +1712,8 @@ _mesa_page_scry_cb(void* vod_p, u3_noun res) u3_noun our, las; u3x_cell(pit, &our, &las); _mesa_rout_bufs(sam_u, NULL, buf_y, len_w, u3k(las)); - _mesa_put_cache(sam_u, &pac_u->pek_u.nam_u, u3k(pag)); + _mesa_del_pit(sam_u, nam_u); + _mesa_put_cache(sam_u, nam_u, u3k(pag)); } u3z(pit); } @@ -1726,23 +1728,23 @@ _mesa_page_scry_hunk_cb(void* vod_p, u3_noun res) u3_mesa_pict* pic_u = vod_p; u3_mesa_pact* pac_u = &pic_u->pac_u; u3_mesa* sam_u = pic_u->sam_u; + u3_mesa_name* nam_u = &pac_u->pag_u.nam_u; u3_weak pag = u3r_at(7, res); if ( u3_none == pag ) { u3l_log("unbound hunk"); - _mesa_del_pit(sam_u, &pac_u->pag_u.nam_u); + _mesa_del_pit(sam_u, nam_u); } else { - c3_w fra_w = pac_u->pek_u.nam_u.fra_w; + c3_w fra_w = nam_u->fra_w; c3_w bat_w = _mesa_lop(fra_w); pac_u->pek_u.nam_u.fra_w = bat_w; - u3_weak old = _mesa_get_cache(sam_u, &pac_u->pag_u.nam_u); - if ( old == u3_none ) { + u3_weak pit = _mesa_get_pit(sam_u, nam_u); // TODO iterate through pit entries? + if ( pit == u3_none ) { u3l_log("bad"); MESA_LOG(APATHY); } else { - u3_noun tag; - u3_noun dat; - u3x_cell(old, &tag, &dat); + u3_noun our, las; + u3x_cell(pit, &our, &las); c3_y* buf_y; // u3m_p("hit", u3a_is_cell(hit)); @@ -1753,14 +1755,14 @@ _mesa_page_scry_hunk_cb(void* vod_p, u3_noun res) // u3_noun key = u3nc(u3k(pax), u3i_word(lop_w)); // u3h_put(sam_u->fin_s.sac_p, key, u3k(u3h(lis))); - pac_u->pek_u.nam_u.fra_w = len_w; + nam_u->fra_w = len_w; if ( (bat_w == 0) && (i == 0) ) { - pac_u->pek_u.nam_u.nit_o = c3y; + nam_u->nit_o = c3y; /* pac_u->pek_u.nam_u.aut_o = c3y; */ } else { - pac_u->pek_u.nam_u.nit_o = c3n; - pac_u->pek_u.nam_u.aut_o = c3n; + nam_u->nit_o = c3n; + nam_u->aut_o = c3n; } /* if (len_w == 0) { */ @@ -1769,16 +1771,17 @@ _mesa_page_scry_hunk_cb(void* vod_p, u3_noun res) /* _mesa_rout_bufs(sam_u, buf_y, lun_w, u3k(u3t(dat))); */ /* } */ - c3_w lun_w = _mesa_fill_buf(pic_u, &buf_y, u3k(u3h(hit))); - _mesa_rout_bufs(sam_u, NULL, buf_y, lun_w, u3k(u3t(dat))); + c3_w lun_w = _mesa_fill_buf(pic_u, &buf_y, u3k(u3h(pag))); + _mesa_rout_bufs(sam_u, NULL, buf_y, lun_w, u3k(las)); + _mesa_del_pit(sam_u, nam_u); /* u3l_log("putting %u", pac_u->pek_u.nam_u.fra_w); */ /* _log_pact(pac_u); */ - _mesa_put_cache(sam_u, &pac_u->pek_u.nam_u, u3nc(MESA_ITEM, u3k(u3h(hit)))); + _mesa_put_cache(sam_u, nam_u, u3nc(MESA_ITEM, u3k(u3h(pag)))); // u3z(key); - hit = u3t(hit); + pag = u3t(pag); if (!( (bat_w == 0) && (i == 0) )) { len_w++; @@ -1787,11 +1790,10 @@ _mesa_page_scry_hunk_cb(void* vod_p, u3_noun res) i++; } u3l_log("i %u", i); - // u3z(old); + u3z(pit); } } - // u3z(hit); - // u3z(pax); + u3z(res); } static void From fd6297abba66f3ae333adb7cc3bd569c7a3cfa4d Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Wed, 10 Jul 2024 14:01:47 -0400 Subject: [PATCH 218/430] mesa: compiles again, but merge still needs work --- pkg/vere/io/mesa.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 5bb88bf055..de5b7ab3aa 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1786,10 +1786,10 @@ _mesa_page_scry_hunk_cb(void* vod_p, u3_noun res) if ( (bat_w == 0) && (i == 0) ) { u3_mesa_pact tac_u = {0}; - c3_w lun_w = u3r_met(3, u3h(hit)); + c3_w lun_w = u3r_met(3, u3h(pag)); c3_y* baf_y = c3_calloc(lun_w); - u3r_bytes(0, lun_w, baf_y, u3h(hit)); + u3r_bytes(0, lun_w, baf_y, u3h(pag)); nam_u->nit_o = c3y; mesa_sift_pact(&tac_u, baf_y, lun_w); @@ -1876,7 +1876,7 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun nun) c3_w lon_w = mesa_etch_pact(baf_y, &tac_u); u3_atom lon = u3i_bytes(lon_w, baf_y); _mesa_put_cache(sam_u, &tac_u.pag_u.nam_u, u3nc(MESA_ITEM, u3k(lon))); - _mesa_rout_bufs(sam_u, baf_y, lon_w, u3k(u3t(dat))); + _mesa_rout_bufs(sam_u, NULL, baf_y, lon_w, u3k(u3t(dat))); return; } @@ -1915,7 +1915,7 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun nun) _mesa_put_cache(sam_u, &tac_u.pag_u.nam_u, u3nc(MESA_ITEM, u3k(lon))); - _mesa_rout_bufs(sam_u, baf_y, lon_w, u3k(u3t(dat))); + _mesa_rout_bufs(sam_u, NULL, baf_y, lon_w, u3k(u3t(dat))); } tac_u.pag_u.nam_u.nit_o = c3n; From 33fef3a5d396b8cf2a3c82e8389d8270e392c936 Mon Sep 17 00:00:00 2001 From: lukechampine Date: Mon, 8 Jul 2024 11:00:37 -0400 Subject: [PATCH 219/430] lss: fix proof handling in non-inline case --- pkg/vere/io/mesa.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index a58dd012cf..e564d522ca 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -2056,8 +2056,8 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) } lss_complete_inline_proof(pof_u, dat_u->fra_y, dat_u->len_w); // TODO: authenticate root - // lss_hash root; - // lss_root(root, pof_u, pof_w); + lss_hash root; + lss_root(root, pof_u, pof_w); req_u->los_u = c3_calloc(sizeof(lss_verifier)); lss_verifier_init(req_u->los_u, 0, req_u->tot_w, pof_u); @@ -2068,19 +2068,18 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) } memcpy(req_u->dat_y, dat_u->fra_y, dat_u->len_w); } else { - /* if ( dat_u->len_w != lss_proof_size(req_u->tot_w) ) { */ - /* u3l_log("len real: %u expected %u", dat_u->len_w, lss_proof_size(req_u->tot_w)); */ - /* u3l_log("hello3"); */ - /* return NULL; // XX ??? */ - /* } */ + c3_w pof_w = lss_proof_size(req_u->tot_w); + if ( dat_u->len_w != pof_w*sizeof(lss_hash) ) { + return NULL; // XX ??? + } // TODO: cast directly instead of copying? - lss_hash* pof_u = c3_calloc(dat_u->len_w * sizeof(lss_hash)); - for ( int i = 0; i < dat_u->len_w; i++ ) { + lss_hash* pof_u = c3_calloc(pof_w * sizeof(lss_hash)); + for ( int i = 0; i < pof_w; i++ ) { memcpy(pof_u[i], dat_u->fra_y + (i * sizeof(lss_hash)), sizeof(lss_hash)); } // TODO: authenticate root - // lss_hash root; - // lss_root(root, pof_u, dat_u->len_w/32); + lss_hash root; + lss_root(root, pof_u, pof_w); req_u->los_u = c3_calloc(sizeof(lss_verifier)); lss_verifier_init(req_u->los_u, 0, req_u->tot_w, pof_u); From 2799bdb6c2aa16e5a31c9d8c9eb39fcd95accfc7 Mon Sep 17 00:00:00 2001 From: lukechampine Date: Mon, 8 Jul 2024 15:01:00 -0400 Subject: [PATCH 220/430] lss: return ready pairs from transceive --- pkg/vere/io/lss.c | 11 ++++++++--- pkg/vere/io/lss.h | 3 +++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/pkg/vere/io/lss.c b/pkg/vere/io/lss.c index 9d2734ba55..0a824a648b 100644 --- a/pkg/vere/io/lss.c +++ b/pkg/vere/io/lss.c @@ -84,7 +84,7 @@ void lss_builder_ingest(lss_builder* bil_u, c3_y* leaf_y, c3_w leaf_w) { bil_u->counter++; } -void lss_builder_transceive(lss_builder* bil_u, c3_w steps, c3_y* jumbo_y, c3_w jumbo_w, lss_pair* pair) { +c3_w lss_builder_transceive(lss_builder* bil_u, c3_w steps, c3_y* jumbo_y, c3_w jumbo_w, lss_pair* pair) { if ( pair != NULL ) { c3_w i = bil_u->counter; memcpy(bil_u->pairs[i][0], (*pair)[0], sizeof(lss_hash)); @@ -96,6 +96,7 @@ void lss_builder_transceive(lss_builder* bil_u, c3_w steps, c3_y* jumbo_y, c3_w jumbo_y += leaf_w; jumbo_w -= leaf_w; } + return c3_min(bil_u->counter - (1<leaves); } lss_hash* lss_builder_finalize(lss_builder* bil_u) { @@ -114,13 +115,14 @@ lss_hash* lss_builder_finalize(lss_builder* bil_u) { } lss_pair* lss_builder_pair(lss_builder* bil_u, c3_w i) { - if ( c3y == _lss_expect_pair(bil_u->counter, i) ) { + if ( c3y == _lss_expect_pair(bil_u->leaves, i) ) { return &bil_u->pairs[i]; } return NULL; } void lss_builder_init(lss_builder* bil_u, c3_w leaves) { + bil_u->leaves = leaves; bil_u->counter = 0; bil_u->proof = c3_calloc(lss_proof_size(leaves) * sizeof(lss_hash)); bil_u->pairs = c3_calloc(leaves * sizeof(lss_pair)); @@ -351,7 +353,10 @@ static void _test_lss_build_verify_jumbo(c3_w steps, c3_w dat_w) c3_y* leaf_y = dat_y + (i*jumbo_leaf_w); c3_w leaf_w = c3_min(dat_w - (i*jumbo_leaf_w), jumbo_leaf_w); lss_pair* pair = lss_builder_pair(&bil_u, i< Date: Mon, 8 Jul 2024 15:35:49 -0400 Subject: [PATCH 221/430] lss: document API --- pkg/vere/io/lss.c | 2 +- pkg/vere/io/lss.h | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/pkg/vere/io/lss.c b/pkg/vere/io/lss.c index 0a824a648b..c492730f6e 100644 --- a/pkg/vere/io/lss.c +++ b/pkg/vere/io/lss.c @@ -210,7 +210,7 @@ void lss_verifier_free(lss_verifier* los_u) { } void lss_complete_inline_proof(lss_hash* proof, c3_y* leaf_y, c3_w leaf_w) { - _leaf_hash(proof[0], leaf_y, leaf_w, 0); + _subtree_root(proof[0], leaf_y, leaf_w, 0); } void lss_root(lss_hash root, lss_hash* proof, c3_w proof_w) { diff --git a/pkg/vere/io/lss.h b/pkg/vere/io/lss.h index 3f28253eaa..a687a59794 100644 --- a/pkg/vere/io/lss.h +++ b/pkg/vere/io/lss.h @@ -23,16 +23,25 @@ typedef struct _lss_builder { void lss_builder_ingest(lss_builder* bil_u, c3_y* leaf_y, c3_w leaf_w); +// Ingests a transceived leaf (consisting of 1< Date: Wed, 10 Jul 2024 19:32:43 -0400 Subject: [PATCH 222/430] mesa: use fixed-size queue for mis_u --- pkg/vere/io/blake.c | 822 -------------------------------------------- pkg/vere/io/blake.h | 80 ----- pkg/vere/io/mesa.c | 89 ++--- 3 files changed, 31 insertions(+), 960 deletions(-) delete mode 100644 pkg/vere/io/blake.c delete mode 100644 pkg/vere/io/blake.h diff --git a/pkg/vere/io/blake.c b/pkg/vere/io/blake.c deleted file mode 100644 index caff2bacd0..0000000000 --- a/pkg/vere/io/blake.c +++ /dev/null @@ -1,822 +0,0 @@ -/// @file - - -#include "vere.h" -#include -#include -#include "urcrypt.h" -#include "blake.h" - - -#define pf(label, ...) { fprintf(stderr, "%s (%u)\r\n", label, __LINE__); fprintf(stderr, __VA_ARGS__); } -#define pl { fprintf(stderr, "(%u)", __LINE__); } -#define pll(label) { fprintf(stderr, "%s (%u)\n", label, __LINE__); } - -enum blake3_flags { - CHUNK_START = 1 << 0, - CHUNK_END = 1 << 1, - PARENT = 1 << 2, - ROOT = 1 << 3, - KEYED_HASH = 1 << 4, - DERIVE_KEY_CONTEXT = 1 << 5, - DERIVE_KEY_MATERIAL = 1 << 6, -}; - - - - -static void -_log_buf(c3_c* str_c, c3_y* buf_y, c3_w len_w) -{ - c3_w siz_w = 2*len_w + 1; - c3_c* res_c = c3_calloc(siz_w); - c3_w cur_w = 0; - c3_c tmp_c[3]; - for(c3_w idx_w = 0; idx_w < len_w; idx_w++ ) { - snprintf(res_c + (2*idx_w), siz_w - (2*idx_w), "%02x", buf_y[idx_w]); - } - fprintf(stderr, "%s: %s", str_c, res_c); - fprintf(stderr, "\r\n"); - c3_free(res_c); -} - -static void -_log_words(c3_c* str_c, c3_w* buf_w, c3_w len_w) -{ - fprintf(stderr, "%s\r\n", str_c); - for ( c3_w i_w = 0; i_w < len_w; i_w++ ) { - fprintf(stderr, "%u ", buf_w[i_w]); - } - fprintf(stderr, "\r\n"); -} - -void -log_node(blake_node* nod_u) -{ - _log_buf("CV", nod_u->cev_y, BLAKE3_OUT_LEN); - _log_buf("block", nod_u->boq_y, BLAKE3_BLOCK_LEN); - fprintf(stderr, "counter: %" PRIu64 "\r\n", nod_u->con_d); - fprintf(stderr, "length: %u\r\n", nod_u->len_y); - fprintf(stderr, "flag: %x\r\n", nod_u->fag_y); -} - -static void print_proof(void* vod_p, c3_w i_w) { - c3_y* pof_y = (c3_y*)vod_p; - fprintf(stderr, "proof: %u\r\n", i_w); - _log_buf("", pof_y, BLAKE3_OUT_LEN); -} - - - - -// static c3_w IV_WORDS[8] = {0x6A09E667UL, 0xBB67AE85UL, 0x3C6EF372UL, 0xA54FF53AUL, 0x510E527FUL, 0x9B05688CUL, 0x1F83D9ABUL, 0x5BE0CD19UL}; -static c3_y IV[BLAKE3_OUT_LEN] = {103, 230, 9, 106, 133, 174, 103, 187, 114, 243, 110, 60, 58, 245, 79, 165, 127, 82, 14, 81, 140, 104, 5, 155, 171, 217, 131, 31, 25, 205, 224, 91}; -#define ABS_DIFF(x,y) x > y ? x - y : y - x - -#define VEC_SCALING 2 - - -void -vec_init(u3_raw_vec* vec_u, c3_w siz_w) -{ - vec_u->siz_w = siz_w; - vec_u->vod_p = c3_calloc(siz_w * sizeof(void*)); - vec_u->len_w = 0; -} -static void -_vec_each(u3_raw_vec* vec_u, void (*func)(void*, c3_w)) { - for (c3_w i_w = 0; i_w < vec_u->len_w; i_w++) { - func(vec_u->vod_p[i_w], i_w); - } -} - -void -vec_free(u3_raw_vec* vec_u) -{ - c3_free(vec_u->vod_p); -} - -void -vec_flop(u3_raw_vec* vec_u) -{ - for (int i = 0; i < (vec_u->len_w/2); i++) { - c3_w j_w = (vec_u->len_w - 1 -i ); - void* vod_p = vec_u->vod_p[i]; - vec_u->vod_p[i] = vec_u->vod_p[j_w]; - vec_u->vod_p[j_w] = vod_p; - } -} - -void -vec_drop(u3_raw_vec* vec_u) -{ - vec_free(vec_u); - c3_free(vec_u); -} - -u3_raw_vec* -vec_make(c3_w siz_w) -{ - u3_raw_vec* vec_u = c3_calloc(sizeof(u3_raw_vec)); - vec_init(vec_u, siz_w); - return vec_u; -} - - -c3_w -vec_len(u3_raw_vec* vec_u) -{ - return vec_u->len_w; -} - -static void* -_vec_raw_get(u3_raw_vec* vec_u, c3_w idx_w) -{ - if ( vec_u->len_w <= idx_w ) { - return NULL; - } - return vec_u->vod_p[idx_w]; -} - -#define _vec_get(typ,vec,idx) ((typ*)_vec_raw_get(vec, idx)) - -static void* -_vec_raw_got(u3_raw_vec* vec_u, c3_w idx_w) -{ - return vec_u->vod_p[idx_w]; -} - -#define _vec_got(typ,vec,idx) ((typ*)_vec_raw_got(vec, idx)) - - -static void -_vec_resize(u3_raw_vec* vec_u, c3_w siz_w) { - u3_assert( vec_u->len_w <= siz_w ); - void* vod_p = c3_calloc(siz_w * sizeof(void*)); - - memcpy(vod_p, vec_u->vod_p, vec_u->len_w * sizeof(void*)); - c3_free(vec_u->vod_p); - vec_u->vod_p = vod_p; - vec_u->siz_w = siz_w; -} - -void -vec_append(u3_raw_vec* vec_u, void* tem_p) -{ - if(vec_u->siz_w == vec_u->len_w ) { - _vec_resize(vec_u, vec_u->siz_w * VEC_SCALING); - } - vec_u->vod_p[vec_u->len_w] = tem_p; - vec_u->len_w++; -} - -void -vec_copy(u3_raw_vec* dst_u, u3_raw_vec* src_u) { - if ( dst_u->siz_w < src_u->siz_w ) { - _vec_resize(dst_u, src_u->siz_w); - } - for ( c3_w i_w = 0; i_w < src_u->len_w; i_w++ ) { - dst_u->vod_p[i_w] = src_u->vod_p[i_w]; - } - dst_u->len_w = src_u->len_w; -} - -void* -vec_pop(u3_raw_vec* vec_u, c3_w idx_w) -{ - if ( vec_u->len_w == 0 || idx_w >= vec_u->len_w ) { - fprintf(stderr, "Failed pop (len: %u, idx: %u)\r\n", vec_u->len_w, idx_w); - return NULL; - } - void* hit_p = vec_u->vod_p[idx_w]; - vec_u->len_w--; - for ( c3_w i_w = idx_w; i_w < vec_u->len_w; i_w++ ) { - vec_u->vod_p[i_w] = vec_u->vod_p[i_w+1]; - } - return hit_p; -} - -void* -vec_popf(u3_raw_vec* vec_u) -{ - return vec_pop(vec_u, 0); -} - -static void* -_vec_popl(u3_raw_vec* vec_u) -{ - return vec_pop(vec_u, vec_u->len_w - 1); -} - - -static void -_vec_swap(u3_raw_vec* vec_u, c3_w fst_w, c3_w snd_w) { - void* tmp_p = vec_u->vod_p[fst_w]; - vec_u->vod_p[fst_w] = vec_u->vod_p[snd_w]; - vec_u->vod_p[snd_w] = tmp_p; -} - - - -static void _vec_weld_mut(u3_raw_vec* fst_u, u3_raw_vec* snd_u) -{ - if ( fst_u->siz_w <= (fst_u->len_w + snd_u->len_w ) ) { - _vec_resize(fst_u, (fst_u->len_w + snd_u->len_w) * VEC_SCALING ); - } - for ( c3_w i_w = 0; i_w < snd_u->len_w; i_w++ ) { - fst_u->vod_p[i_w + fst_u->len_w] = snd_u->vod_p[i_w]; - } - fst_u->len_w += snd_u->len_w; -} - -//type Hash [32]byte - -//func (h Hash) String() string { return hex.EncodeToString(h[:]) } - -//type Pair [2][8]uint32 - - - -static void -_log_blake_subtree(blake_subtree* sub_u) -{ - fprintf(stderr, "sub: (%llu, %llu)\r\n", sub_u->sin_d, sub_u->dex_d); -} - -void -log_bao(blake_bao* bao_u) -{ - fprintf(stderr, "num: %u\r\n", bao_u->num_w); - fprintf(stderr, "con: %u\r\n", bao_u->con_w); - fprintf(stderr, "sub: (%llu, %llu)\r\n", bao_u->sub_u.sin_d, bao_u->sub_u.dex_d); - fprintf(stderr, "queue\r\n"); - _vec_each(&bao_u->que_u, print_proof); - fprintf(stderr, "stack\r\n"); - _vec_each(&bao_u->sta_u, print_proof); -} - -static c3_y -_count_lead_zeros(c3_d val_d) -{ - if (val_d == 0 ) { - return 64; - } - c3_y ret_y = 0; - if ((val_d & 0xFFFFFFFF00000000ULL) == 0) { ret_y += 32; val_d <<= 32; } - if ((val_d & 0xFFFF000000000000ULL) == 0) { ret_y += 16; val_d <<= 16; } - if ((val_d & 0xFF00000000000000ULL) == 0) { ret_y += 8; val_d <<= 8; } - if ((val_d & 0xF000000000000000ULL) == 0) { ret_y += 4; val_d <<= 4; } - if ((val_d & 0xC000000000000000ULL) == 0) { ret_y += 2; val_d <<= 2; } - if ((val_d & 0x8000000000000000ULL) == 0) { ret_y += 1; val_d <<= 1; } - return ret_y; -} - -static c3_y _count_trail_zero(c3_d val_d) -{ - if (val_d == 0 ) { - return 64; - } - c3_y ret_y = 0; - if ((val_d & 0x00000000FFFFFFFFULL) == 0) { ret_y += 32; val_d >>= 32; } - if ((val_d & 0x000000000000FFFFULL) == 0) { ret_y += 16; val_d >>= 16; } - if ((val_d & 0x00000000000000FFULL) == 0) { ret_y += 8; val_d >>= 8; } - if ((val_d & 0x000000000000000FULL) == 0) { ret_y += 4; val_d >>= 4; } - if ((val_d & 0x0000000000000003ULL) == 0) { ret_y += 2; val_d >>= 2; } - if ((val_d & 0x0000000000000001ULL) == 0) { ret_y += 1; val_d >>= 1; } - return ret_y; -} - -static c3_y _bitlen(c3_d bit_d) -{ - return 64 - _count_lead_zeros(bit_d); -} - -static c3_d -_height(blake_subtree* sub_u) -{ - c3_d ret_d = _bitlen(ABS_DIFF(sub_u->dex_d, sub_u->sin_d)) - 1; - return ret_d; -} - -static c3_d -_largest_pow2(c3_d val_d) -{ - c3_d ret_d = 1 << (_bitlen(val_d-1) - 1); - return ret_d; -} - -static void -y_to_w(c3_y* byt_y, c3_w wod_w[16]) -{ - for ( c3_w i_w = 0; i_w < 16; i_w++ ) { - c3_w cur_w = 0; - for ( c3_w j_w = 0; j_w < 4; j_w++ ) { - cur_w |= byt_y[(i_w * 4) + j_w] << (8*j_w); - } - wod_w[i_w] = cur_w; - } -} - -static void -w_to_y(c3_w wod_w[16], c3_y byt_y[64]) -{ - for ( c3_w i_w = 0; i_w < 16; i_w++ ) { - for ( c3_w j_w = 0; j_w < 4; j_w++ ) { - byt_y[(i_w * 4) + j_w] = (wod_w[i_w] >> (8*j_w)) & 0xff; - } - } -} - - -static void -_compress_node(c3_y out_y[BLAKE3_BLOCK_LEN], blake_node* nod_u) -{ - urcrypt_blake3_compress(nod_u->cev_y, - nod_u->boq_y, - nod_u->len_y, - nod_u->con_d, - nod_u->fag_y, - out_y); -} - - -void -make_chain_value(c3_y out_y[32], blake_node* nod_u) -{ - c3_y tmp_y[64]; - _compress_node(tmp_y, nod_u); - memcpy(out_y, tmp_y, BLAKE3_OUT_LEN); -} - -static blake_node* -_compress_chunk(c3_y* dat_y, c3_w dat_w, c3_y cev_y[BLAKE3_OUT_LEN], c3_d con_d, c3_w fag_y) -{ - blake_node* nod_u = c3_calloc(sizeof(blake_node)); - nod_u->con_d = con_d; - nod_u->fag_y = fag_y; - memcpy(nod_u->cev_y, cev_y, BLAKE3_OUT_LEN); - urcrypt_blake3_chunk_output(dat_w, dat_y, nod_u->cev_y, nod_u->boq_y, &nod_u->len_y, &nod_u->con_d, &nod_u->fag_y); - return nod_u; -} - -blake_node* blake_leaf_hash(c3_y* dat_y, c3_w dat_w, c3_d con_d) -{ - return _compress_chunk(dat_y, dat_w, IV, con_d, 0); -} - -static blake_node* -_parent_node(c3_y sin_y[BLAKE3_OUT_LEN], c3_y dex_y[BLAKE3_OUT_LEN], c3_y key_y[BLAKE3_OUT_LEN], c3_w fag_y) -{ - blake_node* nod_u = c3_calloc(sizeof(blake_node)); - nod_u->con_d = 0; - memcpy(nod_u->cev_y, key_y, BLAKE3_OUT_LEN); - nod_u->len_y = BLAKE3_BLOCK_LEN; - nod_u->fag_y = fag_y | PARENT; - memcpy(nod_u->boq_y, sin_y, BLAKE3_OUT_LEN); - memcpy(nod_u->boq_y + BLAKE3_OUT_LEN, dex_y, BLAKE3_OUT_LEN); - return nod_u; -} - -static blake_node* -_parent_hash(blake_node* sin_u, blake_node* dex_u) -{ - c3_y sin_y[BLAKE3_OUT_LEN]; - c3_y dex_y[BLAKE3_OUT_LEN]; - make_chain_value(sin_y, sin_u); - make_chain_value(dex_y, dex_u); - return _parent_node(sin_y, dex_y, IV, 0); -} - - - -static void -_root_hash(c3_y out_y[64], blake_node* nod_u) -{ - nod_u->fag_y |= ROOT; - _compress_node(out_y, nod_u); -} - -void print_pair(void* vod_p, c3_w i_w) { - blake_pair* par_u = (blake_pair*)vod_p; - fprintf(stderr, "pair: %u", i_w); - _log_buf("sin: ", par_u->sin_y, BLAKE3_OUT_LEN); - _log_buf("dex: ", par_u->dex_y, BLAKE3_OUT_LEN); -} - -c3_w emp_w = 1024; -static c3_y emp_y[1024] = {0}; - -static void recurse_blake_subtree(blake_subtree* sub_u, blake_node* nod_u, u3_vec(pair)* par_u) -{ - c3_d hit_d = _height(sub_u); - if ( hit_d == 0 ) { - blake_node* new_u = blake_leaf_hash(emp_y, emp_w, sub_u->sin_d); - memcpy(nod_u, new_u, sizeof(blake_node)); - return; - } - - c3_d mid_d = sub_u->sin_d + _largest_pow2(ABS_DIFF(sub_u->dex_d, sub_u->sin_d)); - - blake_subtree* sin_u = c3_calloc(sizeof(blake_subtree)); - *sin_u = (blake_subtree){sub_u->sin_d, mid_d}; - blake_node* lod_u = c3_calloc(sizeof(blake_node)); - u3_vec(pair)* lar_u = vec_make(8); - recurse_blake_subtree(sin_u, lod_u, lar_u); - - blake_subtree* dex_u = c3_calloc(sizeof(blake_subtree)); - *dex_u = (blake_subtree){mid_d, sub_u->dex_d}; - blake_node* rod_u = c3_calloc(sizeof(blake_node)); - u3_vec(pair)* rar_u = vec_make(8); - recurse_blake_subtree(dex_u, rod_u, rar_u); - - blake_pair* new_u = c3_calloc(sizeof(blake_pair)); - make_chain_value(new_u->sin_y, lod_u); - make_chain_value(new_u->dex_y, rod_u); - memcpy(nod_u, _parent_hash(lod_u, rod_u), sizeof(blake_node)); - - vec_append(par_u, new_u); - _vec_weld_mut(par_u, lar_u); - _vec_weld_mut(par_u, rar_u); -} - -static void -_bao_build(c3_d num_d, c3_y has_y[64], u3_vec(c3_w[8])* pof_u, u3_vec(pair)* par_u) -{ - blake_subtree* sub_u = c3_calloc(sizeof(blake_subtree)); - *sub_u = (blake_subtree){0, num_d}; - blake_node* nod_u = c3_calloc(sizeof(blake_node)); - recurse_blake_subtree(sub_u, nod_u, par_u); - _root_hash(has_y, nod_u); - - if ( vec_len(par_u) != 0 ) { - for ( c3_w i_w = _bitlen(num_d - 1); i_w > 1; i_w-- ) { - blake_pair* pir_u = vec_popf(par_u); - vec_append(pof_u, pir_u->dex_y); - } - blake_pair* pir_u = vec_popf(par_u); - vec_append(pof_u, pir_u->dex_y); - vec_append(pof_u, pir_u->sin_y); - for ( c3_w i_w = 0; i_w < (vec_len(pof_u) >> 1); i_w++ ) { - c3_w jay_w = vec_len(pof_u) - i_w - 1; - _vec_swap(pof_u, i_w, jay_w); - } - } -} - -static void -_push_leaf(blake_bao* bao_u, c3_y lef_y[BLAKE3_OUT_LEN]) -{ - vec_append(&bao_u->que_u, lef_y); -} - -static void -_pop_leaf(c3_y out_y[BLAKE3_OUT_LEN], blake_bao* bao_u) -{ - c3_y* res_y = vec_popf(&bao_u->que_u); - memcpy(out_y, res_y, BLAKE3_OUT_LEN); - c3_free(res_y); -} - -static void -_push_parent(blake_bao* bao_u, c3_y par_y[BLAKE3_OUT_LEN]) -{ - vec_append(&bao_u->sta_u, par_y); -} - -static void -_pop_parent(c3_y out_y[BLAKE3_OUT_LEN], blake_bao* bao_u) -{ - c3_y* res_y = _vec_popl(&bao_u->sta_u); - memcpy(out_y, res_y, BLAKE3_OUT_LEN); - c3_free(res_y); -} - -static void -_verifier_next(blake_bao* bao_u) -{ - if ( _height(&bao_u->sub_u) == 0 ) { - bao_u->sub_u.sin_d += 1; - bao_u->sub_u.dex_d += (1 << _count_trail_zero(bao_u->sub_u.dex_d)); - if ( bao_u->sub_u.dex_d > bao_u->num_w ) { - bao_u->sub_u.dex_d = bao_u->num_w; - } - } else { - bao_u->sub_u = (blake_subtree){bao_u->sub_u.sin_d, bao_u->sub_u.sin_d + _largest_pow2(ABS_DIFF(bao_u->sub_u.dex_d, bao_u->sub_u.sin_d))}; - } -} - -static void -_push_leaves(blake_bao* bao_u, c3_y lef_y[BLAKE3_OUT_LEN], c3_y rig_y[BLAKE3_OUT_LEN]) -{ - _push_leaf(bao_u, lef_y); - _push_leaf(bao_u, rig_y); - _verifier_next(bao_u); - _verifier_next(bao_u); -} - -static c3_o -_veri_check_leaf(blake_bao* bao_u, c3_y* dat_y, c3_w dat_w) -{ - if ( vec_len(&bao_u->que_u) > 0 ) { - c3_y out_y[BLAKE3_OUT_LEN]; - _pop_leaf(out_y, bao_u); - c3_y cav_y[BLAKE3_OUT_LEN]; - blake_node* nod_u = blake_leaf_hash(dat_y, dat_w, bao_u->con_w); - make_chain_value(cav_y, nod_u); - bao_u->con_w++; - c3_free(nod_u); - return 0 == memcmp(cav_y, out_y, BLAKE3_OUT_LEN) ? c3y : c3n; - } else if ( vec_len(&bao_u->sta_u) > 0 ) { - c3_y out_y[BLAKE3_OUT_LEN]; - _pop_parent(out_y, bao_u); - c3_y cav_y[BLAKE3_OUT_LEN]; - blake_node* nod_u = blake_leaf_hash(dat_y, dat_w, bao_u->con_w); - make_chain_value(cav_y, nod_u); - c3_free(nod_u); - bao_u->con_w++; - return 0 == memcmp(cav_y, out_y, BLAKE3_OUT_LEN) ? c3y : c3n; - } - return c3n; -} - -static c3_o -_veri_check_pair(blake_bao* bao_u, blake_pair* par_u) -{ - if ( vec_len(&bao_u->sta_u) == 0 ) { - fprintf(stderr, "bailing empty stack\r\n"); - return c3n; - } - c3_y par_y[BLAKE3_OUT_LEN]; - // par_y is wrong - _pop_parent(par_y, bao_u); - blake_node* nod_u = _parent_node(par_u->sin_y, par_u->dex_y, IV, 0); - c3_y cav_y[BLAKE3_OUT_LEN]; - make_chain_value(cav_y, nod_u); - c3_free(nod_u); - return _(memcmp(par_y, cav_y, BLAKE3_OUT_LEN) == 0); -} - -static c3_o -_at_leaf(blake_subtree* sub_u) -{ - return (sub_u->sin_d+1 == sub_u->dex_d) ? c3y : c3n; -} - - -blake_bao* -blake_bao_make(c3_w num_w, u3_vec(c3_y[BLAKE3_OUT_LEN])* pof_u) -{ - blake_bao* bao_u = c3_calloc(sizeof(blake_bao)); - bao_u->num_w = num_w; - bao_u->con_w = 0; - bao_u->sub_u = (blake_subtree){0,1}; - if ( vec_len(pof_u) == 0 ) { - if ( num_w < 2 ) { - c3_free(bao_u); - return NULL; - } - vec_init(&bao_u->sta_u, 1); - vec_init(&bao_u->que_u, 1); - return bao_u; - } - c3_y* fst_y = vec_popf(pof_u); - c3_y* snd_y = vec_popf(pof_u); - vec_init(&bao_u->sta_u, 8); - vec_init(&bao_u->que_u, 8); - vec_copy(&bao_u->sta_u, pof_u); - vec_flop(&bao_u->sta_u); - _push_leaves(bao_u, fst_y, snd_y); - return bao_u; -} - -static void -_free(void* vod_p, c3_w i_w) -{ - c3_free(vod_p); -} - -void -blake_bao_free(blake_bao* bao_u) -{ - _vec_each(&bao_u->sta_u, _free); - _vec_each(&bao_u->que_u, _free); - c3_free(bao_u); -} - -static void -_push_parents(blake_bao* bao_u, blake_pair* par_u) -{ - c3_y* fst_y = c3_calloc(BLAKE3_OUT_LEN); - c3_y* snd_y = c3_calloc(BLAKE3_OUT_LEN); - memcpy(fst_y, par_u->dex_y, BLAKE3_OUT_LEN); - memcpy(snd_y, par_u->sin_y, BLAKE3_OUT_LEN); - _push_parent(bao_u, fst_y); - _push_parent(bao_u, snd_y); -} - -c3_y -blake_bao_verify(blake_bao* bao_u, c3_y* dat_y, c3_w dat_w, blake_pair* par_u) -{ - if ( c3n == _veri_check_leaf(bao_u, dat_y, dat_w) ) { - return BAO_FAILED; - } - if ( par_u == NULL ) { - return BAO_GOOD; - } - if ( c3n == _veri_check_pair(bao_u, par_u) ) { - return BAO_FAILED; - } - _verifier_next(bao_u); - if ( _at_leaf(&bao_u->sub_u) == c3y ) { - c3_y* fst_y = c3_calloc(BLAKE3_OUT_LEN); - memcpy(fst_y, par_u->sin_y, BLAKE3_OUT_LEN); - c3_y* snd_y = c3_calloc(BLAKE3_OUT_LEN); - memcpy(snd_y, par_u->dex_y, BLAKE3_OUT_LEN); - _push_leaves(bao_u, fst_y, snd_y); - } else { - _push_parents(bao_u, par_u); - } - if ( bao_u->con_w == bao_u->num_w ) { - return BAO_DONE; - } - return BAO_GOOD; -} - -#ifdef BLAKE_TEST - - -static void _test_lead_zeros() -{ - #define asrt(x,y) if ( x != y ) { fprintf(stderr, "failed (line: %u) got %u expected %u", __LINE__, x,y); exit(1); } - // Test with 0, should be 64 leading zeros - asrt(_count_lead_zeros(0ULL), 64); - - // Test with maximum unsigned 64-bit integer, should be 0 leading zeros - asrt(_count_lead_zeros(0xFFFFFFFFFFFFFFFFULL), 0); - - // Test with 1 (which is 2^0), should be 63 leading zeros - asrt(_count_lead_zeros(1ULL), 63); - - // Test with 2^63, should be 1 leading zero - asrt(_count_lead_zeros(0x4000000000000000ULL), 1); - - // Test with a number in the middle, for example 2^31, should be 32 leading zeros - asrt(_count_lead_zeros(0x0000000080000000ULL), 32); - - // Test with a small number, should still recognize leading zeros correctly - asrt(_count_lead_zeros(0x00000000000000FFULL), 56); - - // Additional tests for edge cases - asrt(_count_lead_zeros(0x2000000000000000ULL), 2); - - // Test with a value just below the halfway mark, should have 33 leading zeros - asrt(_count_lead_zeros(0x000000007FFFFFFFULL), 33); - - asrt(_bitlen(7), 3); - - asrt(_count_trail_zero(3), 0); - #undef asrt -} - - -#if 0 -static void _test_bao() -{ - c3_w dat_w = 1024; - c3_y* dat_y = c3_calloc(dat_w); - for ( c3_w num_w = 1; num_w <= 100; num_w++ ) { - c3_y has_y[BLAKE3_BLOCK_LEN]; - u3_vec(c3_y[8])* pof_u = vec_make(10); - u3_vec(blake_pair)* par_u = vec_make(10); - _bao_build(num_w, has_y, pof_u, par_u); - - blake_bao bao_u; - memset(&bao_u, 0, sizeof(blake_bao)); - _veri_init(&bao_u, num_w); - if ( c3n == _verifier_init(&bao_u, has_y, pof_u) ) { - fprintf(stderr, "Failed on %u\r\n", num_w); - exit(1); - } - - if ( num_w == 1 ) { - c3_y out_y[BLAKE3_BLOCK_LEN]; - blake_node* nod_u = blake_leaf_hash(dat_y, dat_w, 0); - _root_hash(out_y, nod_u); - if ( memcmp(out_y, has_y, BLAKE3_BLOCK_LEN) != 0 ) { - _log_buf("expected", has_y, BLAKE3_BLOCK_LEN); - _log_buf("got", out_y, BLAKE3_BLOCK_LEN); - - fprintf(stderr, "1 special case\r\n"); - } - continue; - } - for ( c3_w i_w = 0; i_w < num_w; i_w++ ) { - if ( c3n == _veri_check_leaf(&bao_u, dat_y, dat_w) ) { - fprintf(stderr, "Check leaf %u failed for %u\r\n", i_w, num_w); - exit(1); - } - - // pl - // _log_verifier(&bao_u); - if ( i_w < vec_len(par_u) ) { - blake_pair* pir_u = _vec_get(blake_pair, par_u, i_w); - if ( _veri_check_pair(&bao_u, pir_u) == c3n ) { - fprintf(stderr, "check pair failed (%u, %u)", i_w, num_w); - exit(1); - } - _verifier_next(&bao_u); - if ( _height(&bao_u.sub_u) == 0 ) { - _push_leaf(&bao_u, pir_u->sin_y); - _push_leaf(&bao_u, pir_u->dex_y); - _verifier_next(&bao_u); - _verifier_next(&bao_u); - } else { - _push_parent(&bao_u, pir_u->dex_y); - _push_parent(&bao_u, pir_u->sin_y); - } - } - } - } -} -#endif - -static void print_int(void* vod_p, c3_w i_w) { - c3_w* int_w = (c3_w*)vod_p; - fprintf(stderr, "int: %u\r\n", *int_w); -} - -static void _test_vec() { - fprintf(stderr, "Making vec"); - u3_vec(c3_w)* vec_u = vec_make(2); - - c3_w fst = 1; - c3_w snd = 2; - c3_w thd = 3; - vec_append(vec_u, &fst); - fprintf(stderr, "put one\r\n"); - vec_append(vec_u, &snd); - fprintf(stderr, "put two\r\n"); - vec_append(vec_u, &thd); - fprintf(stderr, "put three\r\n"); - - c3_w* one_w = vec_popf(vec_u); - - if ( *one_w != 1 ) { - fprintf(stderr, "(one) Vec failure\r\n"); - exit(1); - } - fprintf(stderr, "popped %u", *one_w); - - c3_w* two_w = vec_popf(vec_u); - fprintf(stderr, "two pointer %p, %p\r\n", &snd, two_w); - if ( *two_w != 2 ) { - fprintf(stderr, "(two) Vec failure\r\n"); - exit(1); - } - fprintf(stderr, "popped %u", *two_w); - - c3_w* thr_w = vec_popf(vec_u); - if ( *thr_w != 3 ) { - fprintf(stderr, "(three) Vec failure\r\n"); - exit(1); - } - - fprintf(stderr, "popped %u", *thr_w); - - if ( 0 != vec_len(vec_u) ) { - fprintf(stderr, "Vec should be empty\r\n"); - exit(1); - } - - vec_append(vec_u, &fst); - vec_append(vec_u, &snd); - vec_append(vec_u, &thd); - c3_w for_w = 4; - c3_w fiv_w = 5; - c3_w six_w = 6; - u3_vec(c3_w)* new_u = vec_make(4); - vec_append(new_u, &for_w); - vec_append(new_u, &fiv_w); - vec_append(new_u, &six_w); - - _vec_weld_mut(vec_u, new_u); - _vec_each(vec_u, print_int); - - - - - - vec_free(vec_u); - c3_free(vec_u); -} - -int main() { - _test_vec(); - // _test_root_hash(); - _test_lead_zeros(); - // _test_bao(); - - return 0; -} - -#endif diff --git a/pkg/vere/io/blake.h b/pkg/vere/io/blake.h deleted file mode 100644 index 3fd3d0a3d5..0000000000 --- a/pkg/vere/io/blake.h +++ /dev/null @@ -1,80 +0,0 @@ -#ifndef VERE_BLAKE_H -#define VERE_BLAKE_H - -#include "vere.h" -#include "ivory.h" - -#include "noun.h" -#include "ur.h" - -#define BLAKE3_BLOCK_LEN 64 -#define BLAKE3_OUT_LEN 32 - -#define u3_vec(type) u3_raw_vec - -typedef enum _bao_ingest_result { - BAO_BAD_ORDER = 1, - BAO_FAILED = 2, - BAO_GOOD = 3, - BAO_DONE = 4, -} bao_ingest_result; - -typedef struct _blake_pair { - c3_y sin_y[BLAKE3_OUT_LEN]; - c3_y dex_y[BLAKE3_OUT_LEN]; -} blake_pair; - -typedef struct _u3_raw_vec { - c3_w siz_w; - c3_w len_w; - void** vod_p; -} u3_raw_vec; - -typedef struct _blake_node { - c3_y cev_y[BLAKE3_OUT_LEN]; - c3_y boq_y[BLAKE3_BLOCK_LEN]; - c3_d con_d; - c3_y len_y; - c3_y fag_y; -} blake_node; - -typedef struct _blake_subtree { - c3_d sin_d; - c3_d dex_d; -} blake_subtree; - -typedef struct _blake_bao { - c3_w num_w; // number of leaves == num of packets - c3_w con_w; // next packet to be ingested - blake_subtree sub_u; // internal cursor - u3_vec(c3_y[BLAKE3_OUT_LEN]) que_u; // internal - u3_vec(c3_w[BLAKE3_OUT_LEN]) sta_u; // interal -} blake_bao; - -void* vec_popf(u3_raw_vec*); -void* vec_pop(u3_raw_vec*, c3_w); - -void vec_init(u3_raw_vec*, c3_w); - -c3_w vec_len(u3_raw_vec*); - -void vec_append(u3_raw_vec*, void*); - -u3_raw_vec* vec_make(c3_w); - -void vec_free(u3_raw_vec*); -void vec_drop(u3_raw_vec*); - -blake_node* blake_leaf_hash(c3_y* dat_y, c3_w dat_w, c3_d con_d); - -blake_bao* blake_bao_make(c3_w, u3_raw_vec* pof_u); -void blake_bao_free(blake_bao*); - -void make_chain_value(c3_y*, blake_node*); - -void log_node(blake_node*); -void log_bao(blake_bao*); - -c3_y blake_bao_verify(blake_bao*, c3_y*, c3_w, blake_pair* par_u); - -#endif \ No newline at end of file diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index e564d522ca..689504e57b 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -18,7 +18,6 @@ #include #include #include -#include "blake.h" #include "lss.h" c3_o dop_o = c3n; @@ -77,14 +76,6 @@ typedef struct _u3_mesa_stat { // routing table sentinels #define MESA_CZAR 1 // pending dns lookup #define MESA_ROUT 2 // have route -// -// hop enum - -#define SIFT_VAR(dest, src, len) dest = 0; for(int i = 0; i < len; i++ ) { dest |= ((src + i) >> (8*i)); } -#define CHECK_BOUNDS(cur) if ( len_w < cur ) { u3l_log("mesa: failed parse (%u,%u) at line %i", len_w, cur, __LINE__); return 0; } -#define safe_dec(num) (num == 0 ? num : num - 1) -#define _mesa_met3_w(a_w) ((c3_bits_word(a_w) + 0x7) >> 3) - /** typedef u3_mesa_pack_stat u3_noun * [last=@da tries=@ud] @@ -108,7 +99,6 @@ typedef struct _u3_peer_last { typedef struct _u3_misord_buf { c3_y* fra_y; c3_w len_w; - c3_w num_w; lss_pair* par_u; } u3_misord_buf; @@ -131,7 +121,7 @@ typedef struct _u3_pend_req { c3_w ack_w; // highest acked fragment number u3_lane lan_u; // last lane heard u3_gage* gag_u; // congestion control - u3_vec(u3_buf) mis_u; // misordered packets + u3_misord_buf mis_u[8]; // misordered packets lss_verifier* los_u; // Lockstep verifier u3_mesa_pict* pic_u; // preallocated request packet u3_pact_stat* wat_u; // ((mop @ud packet-state) lte) @@ -541,7 +531,6 @@ _mesa_del_request(u3_mesa* sam_u, u3_mesa_name* nam_u) { uv_timer_stop(&req_u->tim_u); _mesa_free_pict(req_u->pic_u); c3_free(req_u->wat_u); - vec_free(&req_u->mis_u); c3_free(req_u->dat_y); lss_verifier_free(req_u->los_u); u3h_del(sam_u->req_p, key); @@ -922,44 +911,27 @@ _mesa_packet_timeout(uv_timer_t* tim_u) { _update_oldest_req(req_u, req_u->gag_u); } -static void _mesa_free_misord_buf(u3_misord_buf* buf_u) -{ - c3_free(buf_u->fra_y); - if ( buf_u->par_u != NULL ) { - c3_free(buf_u->par_u); - } - c3_free(buf_u); -} - static c3_o _mesa_burn_misorder_queue(u3_pend_req* req_u) { - c3_w wan_w = req_u->los_u->counter; - c3_w len_w; - c3_o fon_o; - c3_o res_y = c3y; - while ( (len_w = vec_len(&req_u->mis_u)) != 0 ) { - fon_o = c3n; - for (int i = 0; i < len_w; i++) { - u3_misord_buf* buf_u = (u3_misord_buf*)req_u->mis_u.vod_p[i]; - if ( buf_u->num_w == wan_w ) { - fon_o = c3y; - if ( c3y != (res_y = lss_verifier_ingest(req_u->los_u, buf_u->fra_y, buf_u->len_w, buf_u->par_u))) { - return res_y; - } else { - /* u3l_log("burn %u", buf_u->num_w); */ - /* memcpy(req_u->dat_y + (1024 * buf_u->num_w), buf_u->fra_y, buf_u->len_w); */ - _mesa_free_misord_buf((u3_misord_buf*)vec_pop(&req_u->mis_u, i)); - break; - } - } + c3_w num_w; + c3_w max_w = sizeof(req_u->mis_u) / sizeof(u3_misord_buf); + c3_o res_o = c3y; + for ( num_w = 0; num_w < max_w; num_w++ ) { + u3_misord_buf* buf_u = &req_u->mis_u[num_w]; + if ( buf_u->len_w == 0 ) { + break; } - wan_w++; - if ( c3n == fon_o ) { + if ( c3y != lss_verifier_ingest(req_u->los_u, buf_u->fra_y, buf_u->len_w, buf_u->par_u) ) { + res_o = c3n; break; } } - return res_y; + // ratchet forward + num_w++; // account for the packet processed in _mesa_req_pact_done + memcpy(req_u->mis_u, req_u->mis_u + num_w, max_w - num_w); + memset(req_u->mis_u + max_w - num_w, 0, num_w * sizeof(u3_misord_buf)); + return res_o; } /* _mesa_req_pact_done(): mark packet as done, returning if we should continue @@ -1029,7 +1001,6 @@ _mesa_req_pact_done(u3_mesa* sam_u, u3_mesa_name *nam_u, u3_mesa_data* dat_u, u3 memcpy((*par_u)[1], dat_u->aup_u.has_y[1], sizeof(lss_hash)); } - c3_y ver_y; // TODO: move to bottom c3_y buf_y[1024]; @@ -1038,24 +1009,27 @@ _mesa_req_pact_done(u3_mesa* sam_u, u3_mesa_name *nam_u, u3_mesa_data* dat_u, u3 c3_w len_w = (nam_u->fra_w + 1 == dat_u->tot_w) ? dat_u->len_w : 1024; if ( req_u->los_u->counter != nam_u->fra_w ) { - // TODO: queue packet - u3_misord_buf* buf_u = c3_calloc(sizeof(u3_misord_buf)); - buf_u->fra_y = c3_calloc(len_w); - buf_u->len_w = len_w; - memcpy(buf_u->fra_y, buf_y, len_w); - buf_u->par_u = par_u; - buf_u->num_w = nam_u->fra_w; - vec_append(&req_u->mis_u, buf_u); - } - else if ( c3y != (ver_y = lss_verifier_ingest(req_u->los_u, buf_y, len_w, par_u)) ) { + if ( nam_u->fra_w < req_u->los_u->counter ) { + u3l_log("fragment number too low (%u)", nam_u->fra_w); + } else if ( nam_u->fra_w >= req_u->los_u->counter + (sizeof(req_u->mis_u)/sizeof(u3_misord_buf)) ) { + u3l_log("fragment number too high (%u)", nam_u->fra_w); + } else { + // insert into misordered queue + u3_misord_buf* buf_u = &req_u->mis_u[nam_u->fra_w - req_u->los_u->counter - 1]; + buf_u->fra_y = c3_calloc(len_w); + buf_u->len_w = len_w; + memcpy(buf_u->fra_y, buf_y, len_w); + buf_u->par_u = par_u; + } + } + else if ( c3y != lss_verifier_ingest(req_u->los_u, buf_y, len_w, par_u) ) { c3_free(par_u); // TODO: do we drop the whole request on the floor? u3l_log("auth fail frag %u", nam_u->fra_w); MESA_LOG(AUTH); return req_u; } - else if ( vec_len(&req_u->mis_u) != 0 - && c3y != (ver_y = _mesa_burn_misorder_queue(req_u))) { + else if ( c3y != _mesa_burn_misorder_queue(req_u) ) { c3_free(par_u); MESA_LOG(AUTH) return req_u; @@ -1067,7 +1041,6 @@ _mesa_req_pact_done(u3_mesa* sam_u, u3_mesa_name *nam_u, u3_mesa_data* dat_u, u3 // handle gauge update _mesa_handle_ack(gag_u, &req_u->wat_u[nam_u->fra_w]); - memcpy(req_u->dat_y + (siz_w * nam_u->fra_w), dat_u->fra_y, dat_u->len_w); _try_resend(req_u); @@ -2085,7 +2058,7 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) lss_verifier_init(req_u->los_u, 0, req_u->tot_w, pof_u); c3_free(pof_u); } - vec_init(&req_u->mis_u, 8); + memset(req_u->mis_u, 0, sizeof(req_u->mis_u)); req_u = _mesa_put_request(sam_u, nam_u, req_u); _update_oldest_req(req_u, gag_u); From 11439ebbb0f64f87ce6801b52ce233c34c67c1dd Mon Sep 17 00:00:00 2001 From: Tinnus Napbus Date: Fri, 31 May 2024 21:30:46 +1200 Subject: [PATCH 223/430] mass: return memory report to arvo --- pkg/noun/allocate.c | 186 +++++++++++++++++++++++++++++++++----------- pkg/noun/allocate.h | 19 +++-- pkg/noun/jets.c | 60 ++++++++++---- pkg/noun/jets.h | 4 +- pkg/noun/manage.c | 19 ++--- pkg/noun/manage.h | 14 +++- pkg/noun/nock.c | 27 +++++-- pkg/noun/nock.h | 4 +- pkg/noun/vortex.c | 30 +++++-- pkg/noun/vortex.h | 4 +- pkg/vere/io/term.c | 24 ++++++ pkg/vere/king.c | 30 +++++-- pkg/vere/lord.c | 61 ++++++--------- pkg/vere/main.c | 8 +- pkg/vere/serf.c | 172 ++++++++++++++++++++++++++++++---------- pkg/vere/serf.h | 10 ++- pkg/vere/vere.h | 13 +++- 17 files changed, 501 insertions(+), 184 deletions(-) diff --git a/pkg/noun/allocate.c b/pkg/noun/allocate.c index 8ca5085397..620d5fac5c 100644 --- a/pkg/noun/allocate.c +++ b/pkg/noun/allocate.c @@ -9,6 +9,8 @@ #include "retrieve.h" #include "trace.h" #include "vortex.h" +#include "noun.h" +#include "defs.h" u3_road* u3a_Road; @@ -2005,9 +2007,8 @@ u3a_maid(FILE* fil_u, c3_c* cap_c, c3_w wor_w) /* _ca_print_memory(): un-captioned u3a_print_memory(). */ static void -_ca_print_memory(FILE* fil_u, c3_w wor_w) +_ca_print_memory(FILE* fil_u, c3_w byt_w) { - c3_w byt_w = (wor_w * 4); c3_w gib_w = (byt_w / 1000000000); c3_w mib_w = (byt_w % 1000000000) / 1000000; c3_w kib_w = (byt_w % 1000000) / 1000; @@ -2028,43 +2029,54 @@ _ca_print_memory(FILE* fil_u, c3_w wor_w) } } +/* u3a_quac_free: free quac memory. +*/ +void +u3a_quac_free(quac* qua_u) +{ + for ( c3_w i_w = 0; i_w < qua_u->len_w; i_w++ ) { + u3a_quac_free(qua_u->qua_u[i_w]); + } + c3_free(qua_u->nam_c); + c3_free(qua_u->qua_u); + c3_free(qua_u); +} + /* u3a_prof(): mark/measure/print memory profile. RETAIN. */ -c3_w -u3a_prof(FILE* fil_u, c3_w den_w, u3_noun mas) +quac* +u3a_prof(FILE* fil_u, u3_noun mas) { - c3_w tot_w = 0; +// c3_w tot_w = 0; + quac* pro_u = c3_calloc(sizeof(*pro_u)); u3_noun h_mas, t_mas; if ( c3n == u3r_cell(mas, &h_mas, &t_mas) ) { - fprintf(fil_u, "%.*smistyped mass\r\n", den_w, ""); - return tot_w; + fprintf(fil_u, "mistyped mass\r\n"); + c3_free(pro_u); + return NULL; } - else if ( _(u3du(h_mas)) ) { - fprintf(fil_u, "%.*smistyped mass head\r\n", den_w, ""); + else if ( c3y == u3du(h_mas) ) { + fprintf(fil_u, "mistyped mass head\r\n"); { c3_c* lab_c = u3m_pretty(h_mas); fprintf(fil_u, "h_mas: %s", lab_c); c3_free(lab_c); } - return tot_w; + c3_free(pro_u); + return NULL; } else { - { - c3_c* lab_c = u3m_pretty(h_mas); - fprintf(fil_u, "%*s%s: ", den_w, "", lab_c); - c3_free(lab_c); - } u3_noun it_mas, tt_mas; if ( c3n == u3r_cell(t_mas, &it_mas, &tt_mas) ) { - fprintf(fil_u, "%*smistyped mass tail\r\n", den_w, ""); - return tot_w; + fprintf(fil_u, "mistyped mass tail\r\n"); + c3_free(pro_u); + return NULL; } else if ( c3y == it_mas ) { - tot_w += u3a_mark_noun(tt_mas); - _ca_print_memory(fil_u, tot_w); + c3_w siz_w = u3a_mark_noun(tt_mas); #if 1 /* The basic issue here is that tt_mas is included in .sac @@ -2075,7 +2087,7 @@ u3a_prof(FILE* fil_u, c3_w den_w, u3_noun mas) * * see u3a_mark_ptr(). */ - if ( _(u3a_is_dog(tt_mas)) ) { + if ( c3y == u3a_is_dog(tt_mas) ) { u3a_box* box_u = u3a_botox(u3a_to_ptr(tt_mas)); #ifdef U3_MEMORY_DEBUG if ( 1 == box_u->eus_w ) { @@ -2094,45 +2106,131 @@ u3a_prof(FILE* fil_u, c3_w den_w, u3_noun mas) #endif } #endif + pro_u->nam_c = u3r_string(h_mas); + pro_u->siz_w = siz_w*4; + pro_u->len_w = 0; + pro_u->qua_u = 0; + return pro_u; - return tot_w; } else if ( c3n == it_mas ) { fprintf(fil_u, "\r\n"); - - while ( _(u3du(tt_mas)) ) { - tot_w += u3a_prof(fil_u, den_w+2, u3h(tt_mas)); + pro_u->qua_u = c3_malloc(sizeof(pro_u->qua_u)); + pro_u->len_w = 0; + c3_w i_w = 2; + c3_t bad_t = 0; + while ( c3y == u3du(tt_mas) ) { + quac* new_u = u3a_prof(fil_u, u3h(tt_mas)); + if ( NULL == new_u ) { + c3_free(new_u); + bad_t = 1; + } else { + pro_u->qua_u = c3_realloc(pro_u->qua_u, i_w*sizeof(pro_u->qua_u)); + pro_u->siz_w += new_u->siz_w; + pro_u->qua_u[pro_u->len_w] = new_u; + pro_u->len_w++; + } tt_mas = u3t(tt_mas); + i_w++; + } + + if ( bad_t ) { + for ( i_w = 0; i_w < pro_u->len_w ; i_w++ ) { + u3a_quac_free(pro_u->qua_u[i_w]); + } + c3_free(pro_u->qua_u); + c3_free(pro_u); + return NULL; + } else { + pro_u->nam_c = u3r_string(h_mas); + return pro_u; } + } + else { + fprintf(fil_u, "mistyped (strange) mass tail\r\n"); + c3_free(pro_u); + return NULL; + } + } +} - fprintf(fil_u, "%*s--", den_w, ""); - _ca_print_memory(fil_u, tot_w); - return tot_w; +/* u3a_print_quac: print a quac memory report. +*/ - } - else { - fprintf(fil_u, "%*smistyped (strange) mass tail\r\n", den_w, ""); - return tot_w; +void +u3a_print_quac(FILE* fil_u, c3_w den_w, quac* mas_u) +{ + u3_assert( 0 != fil_u ); + + if ( mas_u->siz_w ) { + fprintf(fil_u, "%*s%s: ", den_w, "", mas_u->nam_c); + + if ( mas_u->len_w == 0) { + _ca_print_memory(fil_u, mas_u->siz_w); + } else { + fprintf(fil_u, "\r\n"); + c3_w i_w; + for ( i_w = 0; i_w < mas_u->len_w; i_w++ ) { + u3a_print_quac(fil_u, den_w+2, mas_u->qua_u[i_w]); + } + fprintf(fil_u, "%*s--", den_w, ""); + _ca_print_memory(fil_u, mas_u->siz_w); } } } /* u3a_mark_road(): mark ad-hoc persistent road structures. */ -c3_w -u3a_mark_road(FILE* fil_u) -{ - c3_w tot_w = 0; - tot_w += u3a_maid(fil_u, " namespace", u3a_mark_noun(u3R->ski.gul)); - tot_w += u3a_maid(fil_u, " trace stack", u3a_mark_noun(u3R->bug.tax)); - tot_w += u3a_maid(fil_u, " trace buffer", u3a_mark_noun(u3R->bug.mer)); - tot_w += u3a_maid(fil_u, " profile batteries", u3a_mark_noun(u3R->pro.don)); - tot_w += u3a_maid(fil_u, " profile doss", u3a_mark_noun(u3R->pro.day)); - tot_w += u3a_maid(fil_u, " new profile trace", u3a_mark_noun(u3R->pro.trace)); - tot_w += u3a_maid(fil_u, " transient memoization cache", u3h_mark(u3R->cax.har_p)); - tot_w += u3a_maid(fil_u, " persistent memoization cache", u3h_mark(u3R->cax.per_p)); - return u3a_maid(fil_u, "total road stuff", tot_w); +quac* +u3a_mark_road() +{ + quac** qua_u = c3_malloc(sizeof(*qua_u)*8); + + qua_u[0] = c3_calloc(sizeof(*qua_u[0])); + qua_u[0]->nam_c = strdup("namespace"); + qua_u[0]->siz_w = u3a_mark_noun(u3R->ski.gul)*4; + + qua_u[1] = c3_calloc(sizeof(*qua_u[1])); + qua_u[1]->nam_c = strdup("trace stack"); + qua_u[1]->siz_w = u3a_mark_noun(u3R->ski.gul)*4; + + qua_u[2] = c3_calloc(sizeof(*qua_u[2])); + qua_u[2]->nam_c = strdup("trace buffer"); + qua_u[2]->siz_w = u3a_mark_noun(u3R->bug.mer)*4; + + qua_u[3] = c3_calloc(sizeof(*qua_u[3])); + qua_u[3]->nam_c = strdup("profile batteries"); + qua_u[3]->siz_w = u3a_mark_noun(u3R->pro.don)*4; + + qua_u[4] = c3_calloc(sizeof(*qua_u[4])); + qua_u[4]->nam_c = strdup("profile doss"); + qua_u[4]->siz_w = u3a_mark_noun(u3R->pro.day)*4; + + qua_u[5] = c3_calloc(sizeof(*qua_u[5])); + qua_u[5]->nam_c = strdup("new profile trace"); + qua_u[5]->siz_w = u3a_mark_noun(u3R->pro.trace)*4; + + qua_u[6] = c3_calloc(sizeof(*qua_u[6])); + qua_u[6]->nam_c = strdup("transient memoization cache"); + qua_u[6]->siz_w = u3h_mark(u3R->cax.har_p)*4; + + qua_u[7] = c3_calloc(sizeof(*qua_u[7])); + qua_u[7]->nam_c = strdup("persistent memoization cache"); + qua_u[7]->siz_w = u3h_mark(u3R->cax.per_p)*4; + + c3_w sum_w = 0; + for (c3_w i_w = 0; i_w < 8; i_w++) { + sum_w += qua_u[i_w]->siz_w; + } + + quac* tot_u = c3_malloc(sizeof(*tot_u)); + tot_u->nam_c = strdup("total road stuff"); + tot_u->siz_w = sum_w; + tot_u->len_w = 8; + tot_u->qua_u = qua_u; + + return tot_u; } /* u3a_reclaim(): clear ad-hoc persistent caches to reclaim memory. diff --git a/pkg/noun/allocate.h b/pkg/noun/allocate.h index b851b64c98..7763fd3183 100644 --- a/pkg/noun/allocate.h +++ b/pkg/noun/allocate.h @@ -606,8 +606,8 @@ /* u3a_mark_road(): mark ad-hoc persistent road structures. */ - c3_w - u3a_mark_road(FILE* fil_u); + quac* + u3a_mark_road(); /* u3a_reclaim(): clear ad-hoc persistent caches to reclaim memory. */ @@ -702,21 +702,30 @@ void u3a_print_time(c3_c* str_c, c3_c* cap_c, c3_d mic_d); + /* u3a_print_quac: print a quac memory report. + */ + void + u3a_print_quac(FILE* fil_u, c3_w den_w, quac* mas_u); + /* u3a_print_memory(): print memory amount. */ void u3a_print_memory(FILE* fil_u, c3_c* cap_c, c3_w wor_w); - /* u3a_prof(): mark/measure/print memory profile. RETAIN. */ - c3_w - u3a_prof(FILE* fil_u, c3_w den_w, u3_noun mas); + quac* + u3a_prof(FILE* fil_u, u3_noun mas); /* u3a_maid(): maybe print memory. */ c3_w u3a_maid(FILE* fil_u, c3_c* cap_c, c3_w wor_w); + /* u3a_uncap_print_memory(): un-captioned print memory amount. + */ + void + u3a_uncap_print_memory(FILE* fil_u, c3_w byt_w); + /* u3a_deadbeef(): write 0xdeadbeef from hat to cap. */ void diff --git a/pkg/noun/jets.c b/pkg/noun/jets.c index 45e7e8144a..e64ce26c8a 100644 --- a/pkg/noun/jets.c +++ b/pkg/noun/jets.c @@ -2305,27 +2305,59 @@ _cj_mark_hank(u3_noun kev, void* dat) /* u3j_mark(): mark jet state for gc. */ -c3_w -u3j_mark(FILE* fil_u) +quac* +u3j_mark() { - c3_w tot_w = 0; + quac** qua_u = c3_malloc(sizeof(*qua_u)*6); - tot_w += u3a_maid(fil_u, " warm jet state", u3h_mark(u3R->jed.war_p)); - tot_w += u3a_maid(fil_u, " cold jet state", u3h_mark(u3R->jed.cod_p)); - tot_w += u3a_maid(fil_u, " hank cache", u3h_mark(u3R->jed.han_p)); - tot_w += u3a_maid(fil_u, " battery hash cache", u3h_mark(u3R->jed.bas_p)); + qua_u[0] = c3_calloc(sizeof(*qua_u[0])); + qua_u[0]->nam_c = strdup("warm jet state"); + qua_u[0]->siz_w = u3h_mark(u3R->jed.war_p)*4; - { - c3_w han_w = 0; - u3h_walk_with(u3R->jed.han_p, _cj_mark_hank, &han_w); - tot_w += u3a_maid(fil_u, " call site cache", han_w); + qua_u[1] = c3_calloc(sizeof(*qua_u[1])); + qua_u[1]->nam_c = strdup("cold jet state"); + qua_u[1]->siz_w = u3h_mark(u3R->jed.cod_p)*4; + + qua_u[2] = c3_calloc(sizeof(*qua_u[2])); + qua_u[2]->nam_c = strdup("hank cache"); + qua_u[2]->siz_w = u3h_mark(u3R->jed.han_p)*4; + + qua_u[3] = c3_calloc(sizeof(*qua_u[3])); + qua_u[3]->nam_c = strdup("battery hash cache"); + qua_u[3]->siz_w = u3h_mark(u3R->jed.bas_p)*4; + + qua_u[4] = c3_calloc(sizeof(*qua_u[4])); + qua_u[4]->nam_c = strdup("call site cache"); + u3h_walk_with(u3R->jed.han_p, _cj_mark_hank, &qua_u[4]->siz_w); + qua_u[4]->siz_w = qua_u[4]->siz_w*4; + + c3_w sum_w = 0; + for ( c3_w i_w = 0; i_w < 5; i_w++ ) { + sum_w += qua_u[i_w]->siz_w; } + quac* tot_u = c3_calloc(sizeof(*tot_u)); + tot_u->nam_c = strdup("total jet stuff"); + if ( u3R == &(u3H->rod_u) ) { - tot_w += u3a_maid(fil_u, " hot jet state", u3h_mark(u3R->jed.hot_p)); - } + qua_u[5] = c3_calloc(sizeof(*qua_u[5])); + qua_u[5]->nam_c = strdup("hot jet state"); + qua_u[5]->siz_w = u3h_mark(u3R->jed.hot_p)*4; + + sum_w += qua_u[5]->siz_w; + + tot_u->siz_w = sum_w; + tot_u->len_w = 6; + tot_u->qua_u = qua_u; - return u3a_maid(fil_u, "total jet stuff", tot_w); + return tot_u; + } else { + tot_u->siz_w = sum_w; + tot_u->len_w = 5; + tot_u->qua_u = qua_u; + + return tot_u; + } } /* u3j_free_hank(): free an entry from the hank cache. diff --git a/pkg/noun/jets.h b/pkg/noun/jets.h index 1440f7558a..02c2c54950 100644 --- a/pkg/noun/jets.h +++ b/pkg/noun/jets.h @@ -296,8 +296,8 @@ /* u3j_mark(): mark jet state for gc. */ - c3_w - u3j_mark(FILE* fil_u); + quac* + u3j_mark(); /* u3j_free(): free jet state. */ diff --git a/pkg/noun/manage.c b/pkg/noun/manage.c index d880a42032..7726ce6c2b 100644 --- a/pkg/noun/manage.c +++ b/pkg/noun/manage.c @@ -459,15 +459,16 @@ u3m_file(c3_c* pas_c) /* u3m_mark(): mark all nouns in the road. */ -c3_w -u3m_mark(FILE* fil_u) -{ - c3_w tot_w = 0; - tot_w += u3v_mark(fil_u); - tot_w += u3j_mark(fil_u); - tot_w += u3n_mark(fil_u); - tot_w += u3a_mark_road(fil_u); - return tot_w; +quac** +u3m_mark() +{ + quac** qua_u = c3_malloc(sizeof(*qua_u)*4); + qua_u[0] = u3v_mark(); + qua_u[1] = u3j_mark(); + qua_u[2] = u3n_mark(); + qua_u[3] = u3a_mark_road(); + + return qua_u; } /* _pave_parts(): build internal tables. diff --git a/pkg/noun/manage.h b/pkg/noun/manage.h index 14f9cc503d..ce1e66a86b 100644 --- a/pkg/noun/manage.h +++ b/pkg/noun/manage.h @@ -148,10 +148,20 @@ u3_noun u3m_soft_esc(u3_noun ref, u3_noun sam); + + /* quac: memory report. + */ + typedef struct _quac { + c3_c* nam_c; + c3_w siz_w; + c3_w len_w; + struct _quac** qua_u; + } quac; + /* u3m_mark(): mark all nouns in the road. */ - c3_w - u3m_mark(FILE* fil_u); + quac** + u3m_mark(); /* u3m_grab(): garbage-collect the world, plus extra roots. */ diff --git a/pkg/noun/nock.c b/pkg/noun/nock.c index 452b04c309..60f713f831 100644 --- a/pkg/noun/nock.c +++ b/pkg/noun/nock.c @@ -3047,16 +3047,29 @@ _n_bam(u3_noun kev, void* dat) /* u3n_mark(): mark the bytecode cache for gc. */ -c3_w -u3n_mark(FILE* fil_u) +quac* +u3n_mark() { - c3_w bam_w = 0, har_w = 0; + quac** qua_u = c3_malloc(sizeof(*qua_u)*2); + + qua_u[0] = c3_calloc(sizeof(*qua_u[0])); + qua_u[0]->nam_c = strdup("bytecode programs"); + u3p(u3h_root) har_p = u3R->byc.har_p; - u3h_walk_with(har_p, _n_bam, &bam_w); + u3h_walk_with(har_p, _n_bam, &qua_u[0]->siz_w); + qua_u[0]->siz_w = qua_u[0]->siz_w*4; + + qua_u[1] = c3_calloc(sizeof(*qua_u[1])); + qua_u[1]->nam_c = strdup("bytecode cache"); + qua_u[1]->siz_w = u3h_mark(har_p)*4; + + quac* tot_u = c3_malloc(sizeof(*tot_u)); + tot_u->nam_c = strdup("total nock stuff"); + tot_u->siz_w = qua_u[0]->siz_w + qua_u[1]->siz_w; + tot_u->len_w = 2; + tot_u->qua_u = qua_u; - bam_w = u3a_maid(fil_u, " bytecode programs", bam_w); - har_w = u3a_maid(fil_u, " bytecode cache", u3h_mark(har_p)); - return u3a_maid(fil_u, "total nock stuff", bam_w + har_w); + return tot_u; } /* u3n_reclaim(): clear ad-hoc persistent caches to reclaim memory. diff --git a/pkg/noun/nock.h b/pkg/noun/nock.h index 7baf7351d8..19489d304c 100644 --- a/pkg/noun/nock.h +++ b/pkg/noun/nock.h @@ -123,8 +123,8 @@ /* u3n_mark(): mark bytecode cache. */ - c3_w - u3n_mark(FILE* fil_u); + quac* + u3n_mark(); /* u3n_reclaim(): clear ad-hoc persistent caches to reclaim memory. */ diff --git a/pkg/noun/vortex.c b/pkg/noun/vortex.c index 0bb9af8c29..8de5b2bd88 100644 --- a/pkg/noun/vortex.c +++ b/pkg/noun/vortex.c @@ -392,16 +392,32 @@ u3v_sway(u3_noun blu, c3_l tab_l, u3_noun tax) /* u3v_mark(): mark arvo kernel. */ -c3_w -u3v_mark(FILE* fil_u) +quac* +u3v_mark() { u3v_arvo* arv_u = &(u3H->arv_u); - c3_w tot_w = 0; - tot_w += u3a_maid(fil_u, " kernel", u3a_mark_noun(arv_u->roc)); - tot_w += u3a_maid(fil_u, " date", u3a_mark_noun(arv_u->now)); - tot_w += u3a_maid(fil_u, " wish cache", u3a_mark_noun(arv_u->yot)); - return u3a_maid(fil_u, "total arvo stuff", tot_w); + quac** qua_u = c3_malloc(sizeof(*qua_u)*3); + + qua_u[0] = c3_calloc(sizeof(*qua_u[0])); + qua_u[0]->nam_c = strdup("kernel"); + qua_u[0]->siz_w = u3a_mark_noun(arv_u->roc)*4; + + qua_u[1] = c3_calloc(sizeof(*qua_u[1])); + qua_u[1]->nam_c = strdup("date"); + qua_u[1]->siz_w = u3a_mark_noun(arv_u->now)*4; + + qua_u[2] = c3_calloc(sizeof(*qua_u[2])); + qua_u[2]->nam_c = strdup("wish cache"); + qua_u[2]->siz_w = u3a_mark_noun(arv_u->yot)*4; + + quac* tot_u = c3_malloc(sizeof(*tot_u)); + tot_u->nam_c = strdup("total arvo stuff"); + tot_u->siz_w = qua_u[0]->siz_w + qua_u[1]->siz_w + qua_u[2]->siz_w; + tot_u->len_w = 3; + tot_u->qua_u = qua_u; + + return tot_u; } /* u3v_reclaim(): clear ad-hoc persistent caches to reclaim memory. diff --git a/pkg/noun/vortex.h b/pkg/noun/vortex.h index 2d202e831c..c254b32625 100644 --- a/pkg/noun/vortex.h +++ b/pkg/noun/vortex.h @@ -130,8 +130,8 @@ /* u3v_mark(): mark arvo kernel. */ - c3_w - u3v_mark(FILE* fil_u); + quac* + u3v_mark(); /* u3v_reclaim(): clear ad-hoc persistent caches to reclaim memory. */ diff --git a/pkg/vere/io/term.c b/pkg/vere/io/term.c index ff3f11efd2..927116bc62 100644 --- a/pkg/vere/io/term.c +++ b/pkg/vere/io/term.c @@ -1639,6 +1639,17 @@ _reck_orchid(u3_noun fot, u3_noun txt, c3_l* tid_l) } } +/* _term_io_quiz(): handle quiz (query to serf). +*/ +static void +_term_io_quiz(void* vod_p, u3_noun res) +{ + u3_auto* car_u = (u3_auto*)vod_p; + u3_noun wir = u3nt(c3__term, '1', u3_nul); + u3_noun cad = u3k(res); + u3_auto_plan(car_u, u3_ovum_init(0, c3__d, wir, cad)); +} + /* _term_io_kick(): apply effects. */ static c3_o @@ -1727,6 +1738,19 @@ _term_io_kick(u3_auto* car_u, u3_noun wir, u3_noun cad) ret_o = c3y; u3_pier_pack(car_u->pir_u); } break; + + case c3__quac: { + ret_o = c3y; + // construct and send writ here + u3_writ* wit_u = c3_calloc(sizeof(*wit_u)); + wit_u->typ_e = u3_writ_quiz; + wit_u->qui_u.ptr_v = car_u; + u3l_log("car_u: %p", car_u); + wit_u->qui_u.quiz_f = _term_io_quiz; + + lord_writ_plan(u3K.pir_u->god_u, wit_u); + + } break; } } } diff --git a/pkg/vere/king.c b/pkg/vere/king.c index 66181c6f6a..c1ddfd9d04 100644 --- a/pkg/vere/king.c +++ b/pkg/vere/king.c @@ -1653,7 +1653,6 @@ u3_king_bail(void) void u3_king_grab(void* vod_p) { - c3_w tot_w = 0; FILE* fil_u; u3_assert( u3R == &(u3H->rod_u) ); @@ -1689,11 +1688,32 @@ u3_king_grab(void* vod_p) } #endif - tot_w += u3m_mark(fil_u); - tot_w += u3_pier_mark(fil_u); + quac** all_u = c3_malloc(sizeof(*all_u)*6); - u3a_print_memory(fil_u, "total marked", tot_w); - u3a_print_memory(fil_u, "sweep", u3a_sweep()); + quac** var_u = u3m_mark(); + all_u[0] = var_u[0]; + all_u[1] = var_u[1]; + all_u[2] = var_u[2]; + all_u[3] = var_u[3]; + c3_free(var_u); + + c3_w tot_w = all_u[0]->siz_w + all_u[1]->siz_w + + all_u[2]->siz_w + all_u[3]->siz_w; + + all_u[4] = c3_calloc(sizeof(*all_u[4])); + all_u[4]->nam_c = "total marked"; + all_u[4]->siz_w = tot_w; + + all_u[5] = c3_calloc(sizeof(*all_u[5])); + all_u[5]->nam_c = "sweep"; + all_u[5]->siz_w = u3a_sweep(); + + for ( c3_w i_w = 0; i_w < 6; i_w++ ) { + u3a_print_quac(fil_u, 0, all_u[i_w]); + u3a_quac_free(all_u[i_w]); + } + + c3_free(all_u); #ifdef U3_MEMORY_LOG { diff --git a/pkg/vere/lord.c b/pkg/vere/lord.c index da739f838d..565dab2baf 100644 --- a/pkg/vere/lord.c +++ b/pkg/vere/lord.c @@ -23,6 +23,7 @@ [%peek mil=@ sam=*] :: gang (each path $%([%once @tas @tas path] [%beam @tas beam])) [%play eve=@ lit=(list ?((pair @da ovum) *))] [%work mil=@ job=(pair @da ovum)] + [%quiz $%([%quac ~])] == :: +plea: from serf to king :: @@ -31,6 +32,7 @@ [%ripe [pro=%1 hon=@ nok=@] eve=@ mug=@] [%slog pri=@ tank] [%flog cord] + [%quiz $%([%quac p=*])] $: %peek $% [%done dat=(unit (cask))] [%bail dud=goof] @@ -521,32 +523,16 @@ _lord_plea_play(u3_lord* god_u, u3_noun dat) u3z(dat); } -/* _lord_plea_mass(): inject mass report +/* _lord_plea_quiz(): handle quiz (query to serf). */ static void -_lord_plea_mass(u3_lord* god_u, u3_noun dat) +_lord_plea_quiz(u3_lord* god_u, u3_noun dat) { - u3_noun cad = u3nc(c3__quac, dat); - u3_noun wir = u3nc(c3__quac, u3_nul); - u3_ovum* egg_u = u3_ovum_init(0, c3__k, wir, cad); - - u3_pier* pir_u = god_u->cb_u.ptr_v; - u3_auto* car_u = c3_calloc(sizeof(*car_u)); - u3_noun ovo; - - car_u->pir_u = pir_u; - car_u->nam_m = c3__quac; // is that right? I did it by analogy w/ smth - - u3_auto_plan(car_u, egg_u); - - u3_assert( u3_auto_next(car_u, &ovo) == egg_u ); - - { - struct timeval tim_tv; - gettimeofday(&tim_tv, 0); - u3_lord_work(god_u, egg_u, u3nc(u3_time_in_tv(&tim_tv), ovo)); - } + u3_writ* wit_u = _lord_writ_need(god_u, u3_writ_quiz); + wit_u->qui_u.quiz_f(wit_u->qui_u.ptr_v, dat); + u3z(dat); } + /* _lord_work_spin(): update spinner if more work is in progress. */ static void @@ -769,9 +755,8 @@ _lord_on_plea(void* ptr_v, c3_d len_d, c3_y* byt_y) _lord_plea_ripe(god_u, u3k(dat)); } break; - case c3__quac: { - _lord_plea_mass(god_u, u3k(dat)); - + case c3__quiz: { + _lord_plea_quiz(god_u, u3k(dat)); } break; } @@ -843,6 +828,10 @@ _lord_writ_make(u3_lord* god_u, u3_writ* wit_u) // msg = u3nt(c3__live, c3__exit, 0); } break; + + case u3_writ_quiz: { + msg = u3nt(c3__quiz, c3__quac, u3_nul); + } break; } return msg; @@ -880,10 +869,10 @@ _lord_writ_send(u3_lord* god_u, u3_writ* wit_u) } } -/* _lord_writ_plan(): enqueue a writ and send. +/* lord_writ_plan(): enqueue a writ and send. */ -static void -_lord_writ_plan(u3_lord* god_u, u3_writ* wit_u) +void +lord_writ_plan(u3_lord* god_u, u3_writ* wit_u) { if ( !god_u->ent_u ) { u3_assert( !god_u->ext_u ); @@ -936,7 +925,7 @@ u3_lord_peek(u3_lord* god_u, u3_pico* pic_u) // XX cache check, unless last // - _lord_writ_plan(god_u, wit_u); + lord_writ_plan(god_u, wit_u); } /* u3_lord_play(): recompute batch. @@ -952,7 +941,7 @@ u3_lord_play(u3_lord* god_u, u3_info fon_u) // // u3_assert( !pay_u.ent_u->nex_u ); - _lord_writ_plan(god_u, wit_u); + lord_writ_plan(god_u, wit_u); } /* u3_lord_work(): attempt work. @@ -974,7 +963,7 @@ u3_lord_work(u3_lord* god_u, u3_ovum* egg_u, u3_noun job) god_u->pin_o = c3y; } - _lord_writ_plan(god_u, wit_u); + lord_writ_plan(god_u, wit_u); } /* u3_lord_save(): save a snapshot. @@ -988,7 +977,7 @@ u3_lord_save(u3_lord* god_u) else { u3_writ* wit_u = _lord_writ_new(god_u); wit_u->typ_e = u3_writ_save; - _lord_writ_plan(god_u, wit_u); + lord_writ_plan(god_u, wit_u); return c3y; } } @@ -1004,7 +993,7 @@ u3_lord_cram(u3_lord* god_u) else { u3_writ* wit_u = _lord_writ_new(god_u); wit_u->typ_e = u3_writ_cram; - _lord_writ_plan(god_u, wit_u); + lord_writ_plan(god_u, wit_u); return c3y; } } @@ -1016,7 +1005,7 @@ u3_lord_meld(u3_lord* god_u) { u3_writ* wit_u = _lord_writ_new(god_u); wit_u->typ_e = u3_writ_meld; - _lord_writ_plan(god_u, wit_u); + lord_writ_plan(god_u, wit_u); } /* u3_lord_pack(): defragment persistent state. @@ -1026,7 +1015,7 @@ u3_lord_pack(u3_lord* god_u) { u3_writ* wit_u = _lord_writ_new(god_u); wit_u->typ_e = u3_writ_pack; - _lord_writ_plan(god_u, wit_u); + lord_writ_plan(god_u, wit_u); } /* u3_lord_exit(): shutdown gracefully. @@ -1036,7 +1025,7 @@ u3_lord_exit(u3_lord* god_u) { u3_writ* wit_u = _lord_writ_new(god_u); wit_u->typ_e = u3_writ_exit; - _lord_writ_plan(god_u, wit_u); + lord_writ_plan(god_u, wit_u); // XX set timer, then halt } diff --git a/pkg/vere/main.c b/pkg/vere/main.c index 012f4afdfa..dd92507f81 100644 --- a/pkg/vere/main.c +++ b/pkg/vere/main.c @@ -1041,10 +1041,7 @@ _cw_serf_writ(void* vod_p, c3_d len_d, c3_y* byt_y) // all references must now be counted, and all roots recorded // - u3_weak serf_post_out = u3_serf_post(&u3V); - if (serf_post_out != u3_none) { - _cw_serf_send(u3nc(c3__quac, serf_post_out)); - } + u3_serf_post(&u3V); } } @@ -1734,7 +1731,7 @@ _cw_grab(c3_i argc, c3_c* argv[]) u3m_boot(u3_Host.dir_c, (size_t)1 << u3_Host.ops_u.lom_y); u3C.wag_w |= u3o_hashless; - u3_serf_grab(); + u3z(u3_serf_grab(c3y)); u3m_stop(); } @@ -3114,6 +3111,7 @@ main(c3_i argc, // validate whether we can execute disk migration if ( u3_Host.ops_u.nuu == c3n ) { _cw_play_impl(0, 0, c3n, c3n, c3n); + signal(SIGTSTP, _stop_exit); // XX unmap loom, else parts of the snapshot could be left in memory } diff --git a/pkg/vere/serf.c b/pkg/vere/serf.c index 2304a926da..658a0b9ff3 100644 --- a/pkg/vere/serf.c +++ b/pkg/vere/serf.c @@ -26,13 +26,14 @@ :: next steps: [%peek mil=@ sam=*] :: gang (each path $%([%once @tas @tas path] [beam @tas beam])) [%play eve=@ lit=(list ?((pair @da ovum) *))] [%work mil=@ job=(pair @da ovum)] + [%quiz $%([%quac ~])] == :: +plea: from serf to king :: +$ plea $% [%live ~] [%ripe [pro=%1 hon=@ nok=@] eve=@ mug=@] - [%quac p=(unit *)] + [%quiz $%([%quac p=*])] [%slog pri=@ tank] [%flog cord] $: %peek @@ -71,20 +72,61 @@ enum { _serf_fag_vega = 1 << 4 // kernel reset }; +/* _serf_quac: convert a quac to a noun. +*/ +u3_noun +_serf_quac(quac* mas_u) +{ + u3_noun list = u3_nul; + for ( c3_w i_w = 0; i_w < mas_u->len_w; i_w++ ) { + list = u3nc(_serf_quac(mas_u->qua_u[i_w]), list); + } + list = u3kb_flop(list); + + u3_noun mas = u3nt(u3i_string(mas_u->nam_c), u3i_word(mas_u->siz_w), list); + + c3_free(mas_u->nam_c); + c3_free(mas_u->qua_u); + c3_free(mas_u); + + return mas; +} + +/* _serf_quacs: convert an array of quacs to a noun list. +*/ +u3_noun +_serf_quacs(c3_w len_w, quac** all_u) +{ + u3_noun list = u3_nul; + for ( c3_w i_w = 0; i_w < len_w; i_w++ ) { + list = u3nc(_serf_quac(all_u[i_w]), list); + } + c3_free(all_u); + return u3kb_flop(list); +} + +/* _serf_print_quacs: print an array of quacs. +*/ +void +_serf_print_quacs(FILE* fil_u, c3_w len_w, quac** all_u) +{ + for ( c3_w i_w = 0; i_w < len_w; i_w++) { + u3a_print_quac(fil_u, 0, all_u[i_w]); + } +} + /* _serf_grab(): garbage collect, checking for profiling. RETAIN. */ -static u3_weak -_serf_grab(u3_noun sac) +static u3_noun +_serf_grab(u3_noun sac, c3_o pri_o) { - u3_noun out = u3_none; if ( u3_nul == sac) { if ( u3C.wag_w & (u3o_debug_ram | u3o_check_corrupt) ) { u3m_grab(sac, u3_none); } - return u3_none; + return u3_nul; } else { - c3_w tot_w = 0; FILE* fil_u; #ifdef U3_MEMORY_LOG @@ -116,38 +158,70 @@ _serf_grab(u3_noun sac) #endif u3_assert( u3R == &(u3H->rod_u) ); - fprintf(fil_u, "\r\n"); - tot_w += u3a_maid(fil_u, "total userspace", u3a_prof(fil_u, 0, sac)); - tot_w += u3m_mark(fil_u); - tot_w += u3a_maid(fil_u, "space profile", u3a_mark_noun(sac)); - - u3a_print_memory(fil_u, "total marked", tot_w); - u3a_print_memory(fil_u, "free lists", u3a_idle(u3R)); - u3a_print_memory(fil_u, "sweep", u3a_sweep()); - - fflush(fil_u); + quac* pro_u = u3a_prof(fil_u, sac); + if ( NULL == pro_u ) { + fflush(fil_u); + u3z(sac); + return u3_nul; + } else { + quac** all_u = c3_malloc(sizeof(*all_u)*9); + all_u[0] = pro_u; + + quac** var_u = u3m_mark(); + all_u[1] = var_u[0]; + all_u[2] = var_u[1]; + all_u[3] = var_u[2]; + all_u[4] = var_u[3]; + c3_free(var_u); + + c3_w tot_w = all_u[0]->siz_w + all_u[1]->siz_w + all_u[2]->siz_w + + all_u[3]->siz_w + all_u[4]->siz_w; + + all_u[5] = c3_calloc(sizeof(*all_u[5])); + all_u[5]->nam_c = strdup("space profile"); + all_u[5]->siz_w = u3a_mark_noun(sac)*4; + + tot_w += all_u[5]->siz_w; + + all_u[6] = c3_calloc(sizeof(*all_u[6])); + all_u[6]->nam_c = strdup("total marked"); + all_u[6]->siz_w = tot_w; + + all_u[7] = c3_calloc(sizeof(*all_u[7])); + all_u[7]->nam_c = strdup("free lists"); + all_u[7]->siz_w = u3a_idle(u3R)*4; + + all_u[8] = c3_calloc(sizeof(*all_u[8])); + all_u[8]->nam_c = strdup("sweep"); + all_u[8]->siz_w = u3a_sweep()*4; + + if ( c3y == pri_o ) { + _serf_print_quacs(fil_u, 9, all_u); + } + fflush(fil_u); #ifdef U3_MEMORY_LOG - { - fclose(fil_u); - } + { + fclose(fil_u); + } #endif - u3z(sac); - - u3l_log(""); + u3_noun mas = _serf_quacs(9, all_u); + u3z(sac); - return u3i_word(tot_w * 4); + return mas; + } } } /* u3_serf_grab(): garbage collect. */ -void -u3_serf_grab(void) +u3_noun +u3_serf_grab(c3_o pri_o) { u3_noun sac = u3_nul; + u3_noun res = u3_nul; u3_assert( u3R == &(u3H->rod_u) ); @@ -177,30 +251,40 @@ u3_serf_grab(void) u3z(gon); } - + fprintf(stderr, "serf: measuring memory:\r\n"); - fprintf(stderr, "BEFORE sac FORK:\r\n"); if ( u3_nul != sac ) { - printf("enter _serf_grab\r\n"); - _serf_grab(sac); + res = _serf_grab(sac, pri_o); } else { fprintf(stderr, "sac is empty\r\n"); - u3a_print_memory(stderr, "total marked", u3m_mark(stderr)); + quac** var_u = u3m_mark(); + + c3_w tot_w; + tot_w = var_u[0]->siz_w + var_u[1]->siz_w + + var_u[2]->siz_w + var_u[3]->siz_w; + + for ( c3_w i_w = 0; i_w < 4; i_w++ ) { + u3a_quac_free(var_u[i_w]); + } + c3_free(var_u); + + u3a_print_memory(stderr, "total marked", tot_w/4); u3a_print_memory(stderr, "free lists", u3a_idle(u3R)); u3a_print_memory(stderr, "sweep", u3a_sweep()); fprintf(stderr, "\r\n"); } - + fflush(stderr); + + return res; } /* u3_serf_post(): update serf state post-writ. */ -u3_weak +void u3_serf_post(u3_serf* sef_u) { - u3_noun out = u3_none; if ( sef_u->fag_w & _serf_fag_hit1 ) { if ( u3C.wag_w & u3o_verbose ) { u3l_log("serf: threshold 1: %u", u3h_wyt(u3R->cax.per_p)); @@ -221,11 +305,8 @@ u3_serf_post(u3_serf* sef_u) // XX this runs on replay too, |mass s/b elsewhere // if ( sef_u->fag_w & _serf_fag_mute ) { - u3_weak grab_mass = _serf_grab(sef_u->sac); + u3z(_serf_grab(sef_u->sac, c3y)); sef_u->sac = u3_nul; - if (grab_mass != u3_none) { - out = u3nc(u3_nul, grab_mass); - } } if ( sef_u->fag_w & _serf_fag_hit0 ) { @@ -243,7 +324,6 @@ u3_serf_post(u3_serf* sef_u) } sef_u->fag_w = _serf_fag_none; - return out; } /* _serf_curb(): check for memory threshold @@ -918,7 +998,7 @@ u3_serf_live(u3_serf* sef_u, u3_noun com, u3_noun* ret) } u3m_save(); - u3_serf_grab(); + u3_serf_grab(c3y); *ret = u3nc(c3__live, u3_nul); return c3y; @@ -1036,10 +1116,22 @@ u3_serf_writ(u3_serf* sef_u, u3_noun wit, u3_noun* pel) ret_o = c3y; } } break; + case c3__quiz: { + u3z(wit); + u3_noun res = u3_serf_grab(c3n); + if ( u3_none == res ) { + ret_o = c3n; + } else { + *pel = u3nt(c3__quiz, c3__quac, res); + ret_o = c3y; + } + } break; } } - u3z(wit); + if ( tag != c3__quiz ) { + u3z(wit); + } return ret_o; } diff --git a/pkg/vere/serf.h b/pkg/vere/serf.h index bfd86e5765..973538f4f1 100644 --- a/pkg/vere/serf.h +++ b/pkg/vere/serf.h @@ -51,12 +51,18 @@ /* u3_serf_post(): update serf state post-writ. */ - u3_weak + void u3_serf_post(u3_serf* sef_u); /* u3_serf_grab(): garbage collect. + */ + u3_noun + u3_serf_grab(c3_o pri_o); + + /* u3_quac_free(): free quac memory. */ void - u3_serf_grab(void); + u3a_quac_free(quac* qua_u); + #endif /* ifndef U3_VERE_SERF_H */ diff --git a/pkg/vere/vere.h b/pkg/vere/vere.h index f97a76f142..9d4cfdb5aa 100644 --- a/pkg/vere/vere.h +++ b/pkg/vere/vere.h @@ -456,7 +456,8 @@ u3_writ_cram = 4, u3_writ_meld = 5, u3_writ_pack = 6, - u3_writ_exit = 7 + u3_writ_exit = 7, + u3_writ_quiz = 8 } u3_writ_type; /* u3_writ: ipc message from king to serf @@ -472,6 +473,10 @@ u3_peek* pek_u; // peek u3_info fon_u; // recompute c3_d eve_d; // save/pack at + struct { // serf query: + void* ptr_v; // driver + void (*quiz_f)(void*, u3_noun); // callback + } qui_u; // }; } u3_writ; @@ -765,7 +770,11 @@ u3_atom u3_time_t_in_ts(time_t tim); #endif - + /* lord_writ_plan(): enqueue a writ and send. + */ + void + lord_writ_plan(u3_lord* god_u, u3_writ* wit_u); + /* u3_time_out_ts(): struct timespec from urbit time. */ void From a6a4d0995a30b3ff0320c8c49d4b2e78e1154516 Mon Sep 17 00:00:00 2001 From: midden-fabler Date: Thu, 11 Jul 2024 14:07:02 -0400 Subject: [PATCH 224/430] http: cleanup ranges, content range header --- pkg/vere/io/http.c | 100 ++++++++++++++++++++++++++------------------- 1 file changed, 59 insertions(+), 41 deletions(-) diff --git a/pkg/vere/io/http.c b/pkg/vere/io/http.c index f22b86679e..18e4928e3c 100644 --- a/pkg/vere/io/http.c +++ b/pkg/vere/io/http.c @@ -645,6 +645,23 @@ _http_cache_respond(u3_hreq* req_u, u3_noun nun); static void _http_scry_respond(u3_hreq* req_u, u3_noun nun); +static u3_noun +_content_rng(c3_z start_z, c3_z end_z, c3_w total_w) +{ + u3_noun out; + + u3_noun lin = u3i_list(u3i_string("bytes "), + u3do("crip", u3do("a-co:co", start_z)), + c3_s1('-'), + u3do("crip", u3do("a-co:co", end_z)), + c3_s1('/'), + u3do("crip", u3do("a-co:co", total_w)), + u3_none); + u3_atom dat = u3qc_rap(3, lin); + out = u3nc(u3i_string("Content-Range"), dat); + return out; +} + /* _http_foo_cb() */ static void @@ -654,11 +671,18 @@ _http_foo_cb(void* vod_p, u3_noun nun) u3_httd* htd_u = peq_u->htd_u; u3_hreq* req_u = peq_u->req_u; - // TODO range if ( req_u ) { u3_assert(u3_rsat_peek == req_u->sat_e); req_u->peq_u = 0; - _http_scry_respond(req_u, u3k(nun)); + if ( u3_nul != nun ) { + u3_atom lent = u3r_at(254, nun); + u3_noun con_rng_hed = _content_rng(0, (lent - 1), lent); + u3_noun mun = u3i_edit(u3k(nun), 125, u3nc(con_rng_hed, u3r_at(125, nun))); + _http_scry_respond(req_u, u3k(mun)); + } + else { + _http_scry_respond(req_u, u3k(nun)); + } } if ( peq_u->las_o == c3n ) { @@ -700,8 +724,12 @@ _find_tis_fas(void* txt, c3_w len) // [x] test stream // [x] content range function // [x] video controls -// [ ] fix repeated 1 byte requests -// [ ] multipart ranges? +// [x] fix repeated 1 byte requests +// [ ] disappearing headers +// [ ] fix 200 +// [ ] content-length +// [ ] mite +// [ ] don't crash, check multipart ranges // typedef struct _range_header { c3_z start_z; @@ -721,19 +749,19 @@ _slice_mime(range_header rng, u3_noun octs) c3_w oct_w = u3t(octs); content out; + out.start_z = SIZE_MAX; + out.end_z = SIZE_MAX; + out.dat = u3_nul; + if ( rng.start_z == SIZE_MAX ) { if ( rng.end_z == SIZE_MAX ) { // [~ ~] - out.start_z = SIZE_MAX; - out.end_z = SIZE_MAX; - out.dat = u3_nul; + return out; } else { // [~ @] if ( rng.end_z > lent_w ) { - out.start_z = SIZE_MAX; - out.end_z = SIZE_MAX; - out.dat = u3_nul; + return out; } else { // slice last bytes @@ -747,28 +775,24 @@ _slice_mime(range_header rng, u3_noun octs) else if ( rng.end_z == SIZE_MAX ) { // [@ ~] if ( rng.start_z > lent_w ) { - out.start_z = SIZE_MAX; - out.end_z = SIZE_MAX; - out.dat = u3_nul; + return out; } else { out.start_z = rng.start_z; - out.end_z = lent_w; - out.dat = u3nc(lent_w - rng.start_z, - u3qc_cut(3, rng.start_z, rng.end_z, oct_w)); + out.end_z = lent_w - 1; + out.dat = u3nc((out.end_z - out.start_z) + 1, + u3qc_cut(3, out.start_z, (out.end_z + 1) - out.start_z, oct_w)); } } else if (rng.end_z > lent_w) { - out.start_z = SIZE_MAX; - out.end_z = SIZE_MAX; - out.dat = u3_nul; + return out; } else { // [@ @] out.start_z = rng.start_z; - out.end_z = (rng.end_z - rng.start_z) + 1; - out.dat = u3nc((rng.end_z - rng.start_z) + 1, - u3qc_cut(3, out.start_z, out.end_z, oct_w)); + out.end_z = rng.end_z; + out.dat = u3nc((out.end_z - out.start_z) + 1, + u3qc_cut(3, out.start_z, (out.end_z + 1) - out.start_z, oct_w)); } return out; } @@ -794,23 +818,6 @@ _get_range(c3_c* txt_c, c3_w len_w) return slice; } -static u3_noun -_content_rng(c3_z start_z, c3_z end_z, c3_w lent_w) -{ - u3_noun out; - - u3_noun lin = u3i_list(u3i_string("bytes "), - u3do("crip", u3do("a-co:co", start_z)), - c3_s1('-'), - u3do("crip", u3do("a-co:co", end_z)), - c3_s1('/'), - u3do("crip", u3do("a-co:co", ++lent_w )), - u3_none); - u3_atom dat = u3qc_rap(3, lin); - out = u3nc(u3i_string("Content-Range"), dat); - return out; -} - /* _http_req_dispatch(): dispatch http request to %eyre */ static void @@ -980,12 +987,19 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) h2o_send_error_generic(req_u->rec_u, 416, msg_c, msg_c, 0); return; } - // if ( u3r_sing(result, octs) == c3y) { + + // if ( u3r_sing(result.dat, octs) == c3y) { // // 200 - // _http_cache_respond(req_u, nac); + // u3l_log("sending 200"); + // u3_atom lent = u3r_at(254, nac); + // u3_noun con_rng_hed = _content_rng(0, lent, lent); + // u3_noun mac = u3i_edit(nac, 125, u3nc(con_rng_hed, u3r_at(125, nac))); + // _http_cache_respond(req_u, mac); + // // _http_cache_respond(req_u, nac); // } // else { // 206 + u3l_log("sending 206"); u3_noun con_rng_hed = _content_rng(result.start_z, result.end_z, u3h(octs)); u3_noun res = u3i_edit(nac, 127, result.dat); res = u3i_edit(res, 124, 206); @@ -1033,7 +1047,9 @@ _http_scry_respond(u3_hreq* req_u, u3_noun nun) { } else { u3_noun auth, response_header, data; + // XX check: looks good u3x_qual(u3k(u3t(u3t(nun))), &auth, 0, &response_header, &data); + // u3m_p("res-header", response_header); u3_noun status, headers; u3x_cell(response_header, &status, &headers); @@ -1255,6 +1271,7 @@ _http_start_respond(u3_hreq* req_u, u3_noun data, u3_noun complete) { + u3m_p("start_respond headers", headers); if ( u3_rsat_plan != req_u->sat_e ) { u3l_log("http: %%start not sane"); u3z(status); u3z(headers); u3z(data); u3z(complete); @@ -1280,6 +1297,7 @@ _http_start_respond(u3_hreq* req_u, c3_i has_len_i = 0; while ( 0 != hed_u ) { + // u3l_log("start_respond header: %s", hed_u->nam_c); if ( 0x200 <= rec_u->version ) { h2o_strtolower(hed_u->nam_c, hed_u->nam_w); From 485f75c560c975d1ad7bae20478542e54961ff67 Mon Sep 17 00:00:00 2001 From: midden-fabler Date: Thu, 11 Jul 2024 15:10:37 -0400 Subject: [PATCH 225/430] http: cleanup slice_mime --- pkg/vere/io/http.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/vere/io/http.c b/pkg/vere/io/http.c index 18e4928e3c..c3f742fe03 100644 --- a/pkg/vere/io/http.c +++ b/pkg/vere/io/http.c @@ -766,9 +766,7 @@ _slice_mime(range_header rng, u3_noun octs) else { // slice last bytes out.start_z = lent_w - rng.end_z; - out.end_z = rng.end_z; - out.dat = u3nc(rng.end_z, - u3qc_cut(3, out.start_z, out.end_z, oct_w)); + out.end_z = lent_w - 1; } } } @@ -780,8 +778,6 @@ _slice_mime(range_header rng, u3_noun octs) else { out.start_z = rng.start_z; out.end_z = lent_w - 1; - out.dat = u3nc((out.end_z - out.start_z) + 1, - u3qc_cut(3, out.start_z, (out.end_z + 1) - out.start_z, oct_w)); } } else if (rng.end_z > lent_w) { @@ -791,6 +787,10 @@ _slice_mime(range_header rng, u3_noun octs) // [@ @] out.start_z = rng.start_z; out.end_z = rng.end_z; + } + if ( (out.start_z <= lent_w) && + (out.end_z <= lent_w) && + (out.start_z <= out.end_z) ) { out.dat = u3nc((out.end_z - out.start_z) + 1, u3qc_cut(3, out.start_z, (out.end_z + 1) - out.start_z, oct_w)); } From 3698ec77720156c05937dc30f45cb290837a36c0 Mon Sep 17 00:00:00 2001 From: midden-fabler Date: Thu, 11 Jul 2024 15:19:31 -0400 Subject: [PATCH 226/430] http: slice_mime lth not lte --- pkg/vere/io/http.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/vere/io/http.c b/pkg/vere/io/http.c index c3f742fe03..9afc340eec 100644 --- a/pkg/vere/io/http.c +++ b/pkg/vere/io/http.c @@ -788,8 +788,8 @@ _slice_mime(range_header rng, u3_noun octs) out.start_z = rng.start_z; out.end_z = rng.end_z; } - if ( (out.start_z <= lent_w) && - (out.end_z <= lent_w) && + if ( (out.start_z < lent_w) && + (out.end_z < lent_w) && (out.start_z <= out.end_z) ) { out.dat = u3nc((out.end_z - out.start_z) + 1, u3qc_cut(3, out.start_z, (out.end_z + 1) - out.start_z, oct_w)); From 5f92f2501e085567f53f0f683f8f6fca842d6dc3 Mon Sep 17 00:00:00 2001 From: midden-fabler Date: Fri, 12 Jul 2024 10:37:33 -0400 Subject: [PATCH 227/430] http: fix headers: working --- pkg/vere/io/http.c | 47 +++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/pkg/vere/io/http.c b/pkg/vere/io/http.c index 9afc340eec..c0e7db1ae8 100644 --- a/pkg/vere/io/http.c +++ b/pkg/vere/io/http.c @@ -646,9 +646,11 @@ static void _http_scry_respond(u3_hreq* req_u, u3_noun nun); static u3_noun -_content_rng(c3_z start_z, c3_z end_z, c3_w total_w) +_content_headers(c3_z start_z, c3_z end_z, c3_w total_w) { u3_noun out; + u3_noun con_rng; + u3_noun con_len; u3_noun lin = u3i_list(u3i_string("bytes "), u3do("crip", u3do("a-co:co", start_z)), @@ -658,7 +660,11 @@ _content_rng(c3_z start_z, c3_z end_z, c3_w total_w) u3do("crip", u3do("a-co:co", total_w)), u3_none); u3_atom dat = u3qc_rap(3, lin); - out = u3nc(u3i_string("Content-Range"), dat); + con_rng = u3nc(u3i_string("Content-Range"), dat); + con_len = u3nc(u3i_string("Content-Length"), + u3do("crip", u3do("a-co:co", (end_z - start_z) + 1))); + + out = u3i_list(con_rng, con_len, u3_none); return out; } @@ -676,8 +682,8 @@ _http_foo_cb(void* vod_p, u3_noun nun) req_u->peq_u = 0; if ( u3_nul != nun ) { u3_atom lent = u3r_at(254, nun); - u3_noun con_rng_hed = _content_rng(0, (lent - 1), lent); - u3_noun mun = u3i_edit(u3k(nun), 125, u3nc(con_rng_hed, u3r_at(125, nun))); + u3_noun cont_heds = _content_headers(0, (lent - 1), lent); + u3_noun mun = u3i_edit(u3k(nun), 125, u3qb_weld(cont_heds, u3r_at(125, nun))); _http_scry_respond(req_u, u3k(mun)); } else { @@ -725,9 +731,9 @@ _find_tis_fas(void* txt, c3_w len) // [x] content range function // [x] video controls // [x] fix repeated 1 byte requests -// [ ] disappearing headers -// [ ] fix 200 -// [ ] content-length +// [x] disappearing headers +// [x] fix 200 +// [x] content-length // [ ] mite // [ ] don't crash, check multipart ranges // @@ -988,24 +994,21 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) return; } - // if ( u3r_sing(result.dat, octs) == c3y) { - // // 200 - // u3l_log("sending 200"); - // u3_atom lent = u3r_at(254, nac); - // u3_noun con_rng_hed = _content_rng(0, lent, lent); - // u3_noun mac = u3i_edit(nac, 125, u3nc(con_rng_hed, u3r_at(125, nac))); - // _http_cache_respond(req_u, mac); - // // _http_cache_respond(req_u, nac); - // } - // else { + if ( u3r_sing(result.dat, octs) == c3y) { + // 200 + u3_atom lent = u3r_at(254, nac); + u3_noun cont_heds = _content_headers(0, (lent - 1), lent); + u3_noun mac = u3i_edit(nac, 125, u3qb_weld(cont_heds, u3r_at(125, nac))); + _http_cache_respond(req_u, mac); + } + else { // 206 - u3l_log("sending 206"); - u3_noun con_rng_hed = _content_rng(result.start_z, result.end_z, u3h(octs)); + u3_noun cont_heds = _content_headers(result.start_z, result.end_z, u3h(octs)); u3_noun res = u3i_edit(nac, 127, result.dat); res = u3i_edit(res, 124, 206); - res = u3i_edit(res, 125, u3nc(con_rng_hed, u3r_at(125, res))); + res = u3i_edit(res, 125, u3qb_weld(cont_heds, u3r_at(125, res))); _http_cache_respond(req_u, res); - // } + } } } else { @@ -1271,7 +1274,6 @@ _http_start_respond(u3_hreq* req_u, u3_noun data, u3_noun complete) { - u3m_p("start_respond headers", headers); if ( u3_rsat_plan != req_u->sat_e ) { u3l_log("http: %%start not sane"); u3z(status); u3z(headers); u3z(data); u3z(complete); @@ -1297,7 +1299,6 @@ _http_start_respond(u3_hreq* req_u, c3_i has_len_i = 0; while ( 0 != hed_u ) { - // u3l_log("start_respond header: %s", hed_u->nam_c); if ( 0x200 <= rec_u->version ) { h2o_strtolower(hed_u->nam_c, hed_u->nam_w); From 7eb6cbaf4d58789a2343ba8a0ea4e65ef95403bd Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Fri, 12 Jul 2024 10:58:51 -0400 Subject: [PATCH 228/430] mesa: WIP redesigning PIT --- pkg/vere/io/mesa.c | 915 +++++++++++++++------------------------- pkg/vere/io/mesa/mesa.h | 3 +- pkg/vere/io/mesa/pact.c | 58 +-- 3 files changed, 380 insertions(+), 596 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index de5b7ab3aa..48969a329d 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -753,6 +753,12 @@ static void _mesa_handle_ack(u3_gage* gag_u, u3_pact_stat* pat_u) } } +static inline c3_w +_mesa_req_get_remaining(u3_pend_req* req_u) +{ + return req_u->tot_w - req_u->nex_w; +} + /* * _mesa_req_get_cwnd(): produce packets to send * @@ -762,22 +768,8 @@ static void _mesa_handle_ack(u3_gage* gag_u, u3_pact_stat* pat_u) static c3_w _mesa_req_get_cwnd(u3_pend_req* req_u) { - c3_w res_w = 0; - - if ( req_u->tot_w == 0 || req_u->gag_u == NULL ) { - u3l_log("shouldn't happen"); - _log_pend_req(req_u); - u3_assert(0); - return 1; - } - c3_w liv_w = bitset_wyt(&req_u->was_u); - if ( req_u->nex_w == req_u->tot_w ) { - return 0; - } - - c3_w rem_w = req_u->tot_w - req_u->nex_w + 1; - /* u3l_log("rem_w: %u", rem_w); */ + c3_w rem_w = _mesa_req_get_remaining(req_u); return c3_min(rem_w, req_u->gag_u->wnd_w - liv_w); } @@ -923,7 +915,7 @@ static void _mesa_send_buf(u3_mesa* sam_u, u3_lane lan_u, c3_y* buf_y, c3_w len_ #ifdef MESA_DEBUG c3_c* sip_c = inet_ntoa(add_u.sin_addr); - u3l_log("mesa: sending packet (%s,%u)", sip_c, por_s); + u3l_log("mesa: sending packet to %s:%u", sip_c, por_s); #endif uv_buf_t buf_u = uv_buf_init((c3_c*)buf_y, len_w); @@ -1010,7 +1002,7 @@ _mesa_packet_timeout(uv_timer_t* tim_u); // TODO rename to indicate it sets a timer static void -_update_oldest_req(u3_pend_req *req_u) +_update_resend_timer(u3_pend_req *req_u) { if( req_u->tot_w == 0 || req_u->len_w == req_u->tot_w ) { u3l_log("bad condition"); @@ -1049,7 +1041,7 @@ _mesa_packet_timeout(uv_timer_t* tim_u) { u3_pend_req* req_u = (u3_pend_req*)tim_u->data; /* u3l_log("%u packet timed out", req_u->old_w); */ _try_resend(req_u, req_u->nex_w); - _update_oldest_req(req_u); + _update_resend_timer(req_u); } static void _mesa_free_misord_buf(u3_misord_buf* buf_u) @@ -1094,23 +1086,17 @@ _mesa_burn_misorder_queue(u3_pend_req* req_u) static void _init_lane_state(u3_lane_state*); -/* _mesa_req_pact_done(): mark packet as done, returning if we should continue +/* _mesa_req_pact_done(): mark packet as done */ -static u3_pend_req* -_mesa_req_pact_done(u3_mesa* sam_u, +static void +_mesa_req_pact_done(u3_pend_req* req_u, u3_mesa_name* nam_u, u3_mesa_data* dat_u, c3_y hop_y, u3_lane lan_u) { - u3_weak ret = u3_none; + u3_mesa* sam_u = req_u->per_u->sam_u; // needed for the MESA_LOG macro c3_d now_d = _get_now_micros(); - u3_pend_req* req_u = _mesa_get_request(sam_u, nam_u); - - if ( NULL == req_u ) { - MESA_LOG(APATHY); - return NULL; - } u3_lane_state* sat_u; if ( 0 == hop_y ) { @@ -1134,13 +1120,13 @@ _mesa_req_pact_done(u3_mesa* sam_u, u3l_log("strange tot_w %u fra_w %u req_u %u", dat_u->tot_w, nam_u->fra_w, req_u->len_w); MESA_LOG(STRANGE); // XX: is this sufficient to drop whole request - return req_u; + return; } // received duplicate if ( c3n == bitset_has(&req_u->was_u, nam_u->fra_w) ) { MESA_LOG(DUPE); - return req_u; + return; } bitset_del(&req_u->was_u, nam_u->fra_w); @@ -1191,13 +1177,13 @@ _mesa_req_pact_done(u3_mesa* sam_u, // TODO: do we drop the whole request on the floor? u3l_log("auth fail frag %u", nam_u->fra_w); MESA_LOG(AUTH); - return req_u; + return; } else if ( vec_len(&req_u->mis_u) != 0 && c3y != (ver_y = _mesa_burn_misorder_queue(req_u))) { c3_free(par_u); MESA_LOG(AUTH) - return req_u; + return; } else { c3_free(par_u); @@ -1210,11 +1196,11 @@ _mesa_req_pact_done(u3_mesa* sam_u, memcpy(req_u->dat_y + (siz_w * nam_u->fra_w), dat_u->fra_y, dat_u->len_w); _try_resend(req_u, nam_u->fra_w); - _update_oldest_req(req_u); + _update_resend_timer(req_u); // _mesa_put_request(sam_u, nam_u, req_u); // _mesa_put_lane(sam_u, nam_u->her_d, lan_u, gag_u); - return req_u; + return; } static u3_lane @@ -1245,35 +1231,30 @@ _realise_lane(u3_noun lan) { } static void -_mesa_rout_bufs(u3_mesa* sam_u, +_mesa_send_bufs(u3_mesa* sam_u, u3_peer* per_u, // null for response packets c3_y* buf_y, c3_w len_w, u3_noun las) { u3_noun lan, t = las; - // u3l_log("sending to ip: %x, port: %u", lan_u.pip_w, lan_u.por_s); while ( t != u3_nul ) { u3x_cell(t, &lan, &t); u3_lane lan_u = _realise_lane(u3k(lan)); - #ifdef MESA_DEBUG - /* u3l_log("sending to ip: %x, port: %u", lan_u.pip_w, lan_u.por_s); */ - - #endif /* ifdef MESA_DEBUG - u3l_log("sending to ip: %x, port: %u", lan_u.pip_w, lan_u.por_s); */ - if ( lan_u.por_s == 0 ) { + if ( !lan_u.por_s ) { u3l_log("mesa: failed to realise lane"); } else { c3_y* sen_y = c3_calloc(len_w); memcpy(sen_y, buf_y, len_w); _mesa_send_buf(sam_u, lan_u, sen_y, len_w); if ( per_u && (c3y == _mesa_lanes_equal(&lan_u, &per_u->dan_u)) ) { + u3l_log(" direct send"); per_u->dir_u.sen_d = _get_now_micros(); } } } - // u3z(las);// TODO is this ok? + u3z(las); } static void @@ -1412,6 +1393,8 @@ _mesa_queue_czar(u3_mesa* sam_u, u3_noun las, u3_noun pac) return res; } +static void _mesa_add_our_to_pit(u3_mesa*, u3_mesa_name*); + static void _mesa_ef_send(u3_mesa* sam_u, u3_noun las, u3_noun pac) { @@ -1423,6 +1406,13 @@ _mesa_ef_send(u3_mesa* sam_u, u3_noun las, u3_noun pac) u3_mesa_head hed_u; mesa_sift_head(buf_y, &hed_u); + #ifdef MESA_DEBUG + c3_c* typ_c = (PACT_PEEK == hed_u.typ_y) ? "PEEK" : + (PACT_POKE == hed_u.typ_y) ? "POKE" : + (PACT_PAGE == hed_u.typ_y) ? "PAGE" : "RESV"; + u3l_log("mesa: ef_send() %s", typ_c); + #endif + u3_peer* per_u; if ( (PACT_PEEK == hed_u.typ_y) || (PACT_POKE == hed_u.typ_y) ) { u3_mesa_pact pac_u; @@ -1430,16 +1420,19 @@ _mesa_ef_send(u3_mesa* sam_u, u3_noun las, u3_noun pac) c3_d her_d[2]; _get_her(&pac_u, her_d); per_u = _mesa_get_peer(sam_u, her_d); + + _mesa_add_our_to_pit(sam_u, &pac_u.pek_u.nam_u); } else { // treat PACT_RESV as an opaque response per_u = NULL; } sam_u->tim_d = _get_now_micros(); - _mesa_rout_bufs(sam_u, per_u, buf_y, len_w, las); + _mesa_send_bufs(sam_u, per_u, buf_y, len_w, u3k(las)); c3_free(buf_y); u3z(pac); + u3z(las); } c3_o @@ -1448,7 +1441,6 @@ _ames_kick_newt(void* sam_u, u3_noun tag, u3_noun dat); static c3_o _mesa_kick(u3_mesa* sam_u, u3_noun tag, u3_noun dat) { c3_o ret_o; - /* u3l_log("blabla"); */ switch ( tag ) { default: { ret_o = c3n; @@ -1456,10 +1448,10 @@ static c3_o _mesa_kick(u3_mesa* sam_u, u3_noun tag, u3_noun dat) case c3__push: { u3_noun las, pac; if ( c3n == u3r_cell(dat, &las, &pac) ) { - u3l_log(" mesa: send no"); + // u3l_log(" mesa: send old"); ret_o = c3n; } else { - u3l_log(" mesa: send yes"); + // u3l_log(" mesa: send new"); _mesa_ef_send(sam_u, u3k(las), u3k(pac)); ret_o = c3y; } @@ -1647,10 +1639,13 @@ _mesa_get_jumbo_cache(u3_mesa* sam_u, u3_mesa_name* nam_u) u3_noun pax = _name_to_jumbo_scry(nam_u); u3_weak res = u3h_get(sam_u->pac_p, pax); if ( u3_none == res ) { - //u3m_p("miss", u3k(pax)); + u3m_p("miss", pax); } else { - //u3m_p("hit", u3nc(u3k(pax), u3k(res))); + u3_noun kev = u3nc(u3k(pax), u3k(res)); + u3m_p("hit", kev); + u3z(kev); } + u3z(pax); return res; } @@ -1662,13 +1657,29 @@ _mesa_get_pit(u3_mesa* sam_u, u3_mesa_name* nam_u) return u3h_get(sam_u->pit_p, _name_to_scry((nam_u))); } -// RETAIN +/* +ld: Undefined symbols: + _log_name, referenced from: + __mesa_put_pit in + _log_pact in + _log_pact in + _log_pact in + _log_pact in +clang: error: linker command failed with exit code 1 (use -v to see invocation) +Target //pkg/vere:urbit failed to build +*/ + static void _mesa_put_pit(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_noun val) { u3_noun pax = _name_to_scry(nam_u); u3h_put(sam_u->pit_p, pax, u3k(val)); + c3_c* our_c = (c3y == u3h(val))? "&" : "|"; + c3_c* las_c = (u3_nul == u3t(val))? "~" : "..."; + u3l_log("mesa: put_pit(our %s, las %s)", our_c, las_c); + log_name(nam_u); u3z(pax); + u3z(val); } static void @@ -1679,12 +1690,6 @@ _mesa_del_pit(u3_mesa* sam_u, u3_mesa_name* nam_u) u3z(pax); } -static u3_weak -_mesa_get_cache(u3_mesa* sam_u, u3_mesa_name* nam_u) -{ - return u3h_get(sam_u->pac_p, _name_to_scry(nam_u)); -} - static void _mesa_put_jumbo_cache(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_noun val) { @@ -1693,285 +1698,129 @@ _mesa_put_jumbo_cache(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_noun val) u3z(pax); // TODO: fix refcount } -// RETAIN static void -_mesa_put_cache(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_noun val) -{ - u3_noun pax = _name_to_scry(nam_u); - u3h_put(sam_u->pac_p, pax, u3k(val)); - u3z(pax); -} - -static c3_w -_mesa_fill_buf(u3_mesa_pict* req_u, c3_y** buf_y, u3_noun hit) -{ - c3_w len_w = u3r_met(3, hit); - - *buf_y = c3_calloc(len_w); - u3r_bytes(0, len_w, *buf_y, hit); - - u3z(hit); - return len_w; +_mesa_send_pact(u3_mesa* sam_u, + u3_noun las, + u3_peer* per_u, // null for response packets + u3_mesa_pact* tac_u) +{ + c3_y buf_y[PACT_SIZE]; + c3_w len_w = mesa_etch_pact(buf_y, tac_u); + _mesa_send_bufs(sam_u, per_u, buf_y, len_w, u3k(las)); + u3z(las); } static void -_mesa_page_scry_cb(void* vod_p, u3_noun res) -{ - u3_mesa_pict* pic_u = vod_p; - u3_mesa_pact* pac_u = &pic_u->pac_u; - u3_mesa* sam_u = pic_u->sam_u; - u3_mesa_name* nam_u = &pac_u->pek_u.nam_u; - - u3_weak pag = u3r_at(7, res); - if ( u3_none == pag ) { - u3l_log("unbound"); - _mesa_del_pit(sam_u, nam_u); - } else { - u3_weak pit = _mesa_get_pit(sam_u, nam_u); - if ( pit == u3_none ) { - u3l_log("no pit"); - MESA_LOG(APATHY); - } else { - c3_y* buf_y; - c3_w len_w = _mesa_fill_buf(pic_u, &buf_y, u3k(pag)); - u3_noun our, las; - u3x_cell(pit, &our, &las); - _mesa_rout_bufs(sam_u, NULL, buf_y, len_w, u3k(las)); - _mesa_del_pit(sam_u, nam_u); - _mesa_put_cache(sam_u, nam_u, u3k(pag)); +_mesa_send_jumbo_pieces(u3_mesa* sam_u, u3_noun pag) +{ + #ifdef MESA_DEBUG + u3l_log("mesa: send_jumbo_pieces()"); + #endif + // parse packet + u3_mesa_pact tac_u = {0}; + c3_w jumbo_w = u3r_met(3, pag); + c3_y* jumbo_y = c3_calloc(jumbo_w); + u3r_bytes(0, jumbo_w, jumbo_y, pag); + u3z(pag); + mesa_sift_pact(&tac_u, jumbo_y, jumbo_w); + + #ifdef MESA_DEBUG + log_pact(&tac_u); + #endif + + // compute LSS data + // + // TODO: this assumes we have the entire message. Should be switched to use + // lss_builder_transceive instead. + c3_w leaves_w = (jumbo_w + 1023) / 1024; + lss_builder bil_u; + lss_builder_init(&bil_u, leaves_w); + for ( c3_w i = 0; i < leaves_w; i++ ) { + c3_y* leaf_y = jumbo_y + (i*1024); + c3_w leaf_w = (i < leaves_w - 1) ? 1024 : jumbo_w % 1024; + lss_builder_ingest(&bil_u, leaf_y, leaf_w); + } + lss_hash* proof = lss_builder_finalize(&bil_u); + c3_w proof_len = lss_proof_size(leaves_w); + + u3_mesa_name nam_u = tac_u.pag_u.nam_u; + u3_mesa_data dat_u = tac_u.pag_u.dat_u; + nam_u.boq_y = 13; + dat_u.tot_w = leaves_w; + + if ( c3y == nam_u.nit_o && leaves_w > 4) { + #ifdef MESA_DEBUG + u3l_log(" sending proof packet"); + #endif + + // send proof packet + tac_u.pag_u.dat_u.len_w = proof_len*sizeof(lss_hash); + memcpy(tac_u.pag_u.dat_u.fra_y, proof, tac_u.pag_u.dat_u.len_w); + u3_weak pin = _mesa_get_pit(sam_u, &nam_u); + if ( u3_none != pin ) { + _mesa_send_pact(sam_u, u3k(u3t(pin)), NULL, &tac_u); + _mesa_del_pit(sam_u, &nam_u); + u3z(pin); } - u3z(pit); } - u3z(res); -} - -/* - */ -static void -_mesa_page_scry_hunk_cb(void* vod_p, u3_noun res) -{ - u3_mesa_pict* pic_u = vod_p; - u3_mesa_pact* pac_u = &pic_u->pac_u; - u3_mesa* sam_u = pic_u->sam_u; - u3_mesa_name* nam_u = &pac_u->pag_u.nam_u; - - u3_weak pag = u3r_at(7, res); - if ( u3_none == pag ) { - u3l_log("unbound hunk"); - _mesa_del_pit(sam_u, nam_u); - } else { - c3_w fra_w = nam_u->fra_w; - c3_w bat_w = _mesa_lop(fra_w); - pac_u->pek_u.nam_u.fra_w = bat_w; - u3_weak pit = _mesa_get_pit(sam_u, nam_u); // TODO iterate through pit entries? - if ( pit == u3_none ) { - u3l_log("bad"); - MESA_LOG(APATHY); - } else { - u3_noun our, las; - u3x_cell(pit, &our, &las); - c3_y* buf_y; - // u3m_p("hit", u3a_is_cell(hit)); - - c3_w len_w = bat_w; - /* u3l_log("path %s", pac_u->pek_u.nam_u.pat_c); */ - c3_w i = 0; - _mesa_put_jumbo_cache(sam_u, nam_u, u3nc(MESA_ITEM, 0)); - while ( u3_nul != pag ) { - // u3_noun key = u3nc(u3k(pax), u3i_word(lop_w)); - // u3h_put(sam_u->fin_s.sac_p, key, u3k(u3h(lis))); - - nam_u->fra_w = len_w; - - if ( (bat_w == 0) && (i == 0) ) { - u3_mesa_pact tac_u = {0}; - c3_w lun_w = u3r_met(3, u3h(pag)); - c3_y* baf_y = c3_calloc(lun_w); + #ifdef MESA_DEBUG + u3l_log(" sending up to %u leaf packets", leaves_w); + #endif + // send leaf packets + tac_u.pag_u.nam_u.nit_o = c3n; - u3r_bytes(0, lun_w, baf_y, u3h(pag)); - nam_u->nit_o = c3y; - mesa_sift_pact(&tac_u, baf_y, lun_w); + for (c3_w i = 0; i < leaves_w; i++) { + nam_u.fra_w = i; + dat_u.fra_y = jumbo_y + (i*1024); + dat_u.len_w = c3_min(jumbo_w - (i*1024), 1024); - /* pac_u->pek_u.nam_u.aut_o = c3y; */ - } else { - nam_u->nit_o = c3n; - nam_u->aut_o = c3n; - } - - /* if (len_w == 0) { */ - /* c3_w lun_w = _mesa_fill_buf(pic_u, &buf_y, u3k(u3h(hit))); */ - /* /\* pac_u->pek_u.nam_u.fra_w = fra_w; *\/ */ - /* _mesa_rout_bufs(sam_u, buf_y, lun_w, u3k(u3t(dat))); */ - /* } */ - - c3_w lun_w = _mesa_fill_buf(pic_u, &buf_y, u3k(u3h(pag))); - _mesa_rout_bufs(sam_u, NULL, buf_y, lun_w, u3k(las)); - _mesa_del_pit(sam_u, nam_u); - - /* u3l_log("putting %u", pac_u->pek_u.nam_u.fra_w); */ - /* _log_pact(pac_u); */ - - _mesa_put_cache(sam_u, nam_u, u3nc(MESA_ITEM, u3k(u3h(pag)))); - // u3z(key); - - pag = u3t(pag); - - if (!( (bat_w == 0) && (i == 0) )) { - len_w++; - } - - i++; - } - u3l_log("i %u", i); - u3z(pit); + lss_pair* pair = lss_builder_pair(&bil_u, i); + if ( NULL == pair ) { + dat_u.aup_u.len_y = 0; + dat_u.aum_u.typ_e = AUTH_NONE; + } else { + dat_u.aup_u.len_y = 2; + dat_u.aum_u.typ_e = AUTH_NEXT; + memcpy(dat_u.aup_u.has_y[0], (*pair)[0], sizeof(lss_hash)); + memcpy(dat_u.aup_u.has_y[1], (*pair)[1], sizeof(lss_hash)); + } + u3_weak pin = _mesa_get_pit(sam_u, &nam_u); + if ( u3_none != pin) { + #ifdef MESA_DEBUG + u3l_log(" sending leaf packet, fra_w: %u", nam_u.fra_w); + #endif + _mesa_send_pact(sam_u, u3k(u3t(pin)), NULL, &tac_u); + _mesa_del_pit(sam_u, &nam_u); + u3k(pin); } } - u3z(res); } static void -_mesa_page_scry_jumbo_cb(void* vod_p, u3_noun nun) +_mesa_page_scry_jumbo_cb(void* vod_p, u3_noun res) { u3_mesa_pict* pic_u = vod_p; + u3_mesa* sam_u = pic_u->sam_u; u3_mesa_pact* pac_u = &pic_u->pac_u; - u3_mesa* sam_u = pic_u->sam_u; - //u3_noun pax = _mesa_path_with_fra(pac_u->pek_u.nam_u.pat_c, &fra_s); + u3_mesa_name* nam_u = &pac_u->pag_u.nam_u; - u3_weak hit = u3r_at(7, nun); - /* u3_noun proof = u3h(hit); */ - /* u3m_p("proof", proof); */ - /* hit = u3t(hit); */ + u3_weak pag = u3r_at(7, res); - if ( u3_none == hit ) { + if ( u3_none == pag ) { // TODO: mark as dead - //u3z(nun); - u3l_log("unbound"); - } else { - c3_w fra_w = pac_u->pek_u.nam_u.fra_w; - u3_weak old = _mesa_get_jumbo_cache(sam_u, &pac_u->pag_u.nam_u); - if ( old == u3_none ) { - /* MESA_LOG(APATHY); */ - } else { - u3_noun tag; - u3_noun dat; - u3x_cell(u3k(old), &tag, &dat); - - /* u3l_log("path %s", pac_u->pek_u.nam_u.pat_c); */ - u3_mesa_pact tac_u = {0}; - - c3_w len_w = u3r_met(3, hit); - u3l_log("len_w %u", len_w); - - c3_y* puf_y = c3_calloc(len_w); - u3r_bytes(0, len_w, puf_y, hit); - - mesa_sift_pact(&tac_u, puf_y, len_w); - - _mesa_put_jumbo_cache(sam_u, &tac_u.pag_u.nam_u, u3nc(MESA_ITEM, 0)); - - if (len_w <= 1024) { - tac_u.pag_u.nam_u.boq_y = 13; - c3_y baf_y[PACT_SIZE]; - c3_w lon_w = mesa_etch_pact(baf_y, &tac_u); - u3_atom lon = u3i_bytes(lon_w, baf_y); - _mesa_put_cache(sam_u, &tac_u.pag_u.nam_u, u3nc(MESA_ITEM, u3k(lon))); - _mesa_rout_bufs(sam_u, NULL, baf_y, lon_w, u3k(u3t(dat))); - return; - } - - - /* u3l_log("jumbo frame"); */ - /* _log_pact(&tac_u); */ - - u3_atom jumbo = u3i_bytes(len_w, tac_u.pag_u.dat_u.fra_y); - c3_w siz_w = u3r_met(13, jumbo); - - - c3_w leaves_w = siz_w; - lss_builder bil_u; - lss_builder_init(&bil_u, leaves_w); - - for ( c3_w i = 0; i < leaves_w; i++ ) { - c3_y* leaf_y = tac_u.pag_u.dat_u.fra_y + (i*1024); - c3_w leaf_w = (i < leaves_w - 1) ? 1024 : tac_u.pag_u.dat_u.len_w % 1024; - lss_builder_ingest(&bil_u, leaf_y, leaf_w); - } - - lss_hash* proof = lss_builder_finalize(&bil_u); - c3_w saz_w = lss_proof_size(leaves_w); - - tac_u.pag_u.nam_u.boq_y = 13; - tac_u.pag_u.dat_u.tot_w = siz_w; - - if ( c3y == tac_u.pag_u.nam_u.nit_o && leaves_w > 4) { - memcpy(tac_u.pag_u.dat_u.fra_y, proof, saz_w*32); - tac_u.pag_u.dat_u.len_w = saz_w*32; - c3_y baf_y[PACT_SIZE]; - - c3_w lon_w = mesa_etch_pact(baf_y, &tac_u); - - u3_atom lon = u3i_bytes(lon_w, baf_y); - - _mesa_put_cache(sam_u, &tac_u.pag_u.nam_u, u3nc(MESA_ITEM, u3k(lon))); - - _mesa_rout_bufs(sam_u, NULL, baf_y, lon_w, u3k(u3t(dat))); - } - - tac_u.pag_u.nam_u.nit_o = c3n; - - for (c3_w i=0; i < siz_w; i++) { - // (cut 3 [wid 1] dat.byts)) - u3_atom lin = u3qc_cut(13, i, 1, jumbo); - tac_u.pag_u.dat_u.len_w = u3r_met(3, lin); - tac_u.pag_u.nam_u.fra_w = i; - tac_u.pag_u.dat_u.fra_y = c3_malloc(tac_u.pag_u.dat_u.len_w); - /* memcpy(jum_y + off_w, tac_u.pag_u.dat_u.fra_y, tac_u.pag_u.dat_u.len_w ); */ - /* off_w += tac_u.pag_u.dat_u.len_w; */ - - u3r_bytes(0, tac_u.pag_u.dat_u.len_w, tac_u.pag_u.dat_u.fra_y, lin); - - /* u3_noun pair = u3h(proof); */ - - - lss_pair* pair = lss_builder_pair(&bil_u, i); - if ( NULL == pair ) { - tac_u.pag_u.dat_u.aup_u.len_y = 0; - tac_u.pag_u.dat_u.aum_u.typ_e = AUTH_NONE; - } else { - tac_u.pag_u.dat_u.aup_u.len_y = 2; - tac_u.pag_u.dat_u.aum_u.typ_e = AUTH_NEXT; - memcpy(tac_u.pag_u.dat_u.aup_u.has_y[0], (*pair)[0], 32); - memcpy(tac_u.pag_u.dat_u.aup_u.has_y[1], (*pair)[1], 32); - } - - /* _log_pact(&tac_u); */ - /* _log_buf(tac_u.pag_u.dat_u.fra_y, tac_u.pag_u.dat_u.len_w); */ - - c3_y buf_y[PACT_SIZE]; - - c3_w len_w = mesa_etch_pact(buf_y, &tac_u); - - u3_atom lan = u3i_bytes(len_w, buf_y); - - _mesa_put_cache(sam_u, &tac_u.pag_u.nam_u, u3nc(MESA_ITEM, u3k(lan))); - - - /* _log_buf(out_y, tac_u.pag_u.dat_u.len_w); */ - - _mesa_rout_bufs(sam_u, NULL, buf_y, len_w, u3k(u3t(dat))); - /* proof = u3t(proof); */ - - /* u3l_log("putting %u", pac_u->pek_u.nam_u.fra_w); */ - /* _log_pact(pac_u); */ - - } - - // u3z(old); - } + u3z(res); + u3l_log("mesa: jumbo frame missing"); + log_pact(pac_u); + return; } - // u3z(hit); - // u3z(pax); + #ifdef MESA_DEBUG + u3l_log("mesa: scry_jumbo_cb()"); + log_pact(pac_u); + #endif + _mesa_put_jumbo_cache(sam_u, nam_u, u3nc(MESA_ITEM, u3k(pag))); + _mesa_send_jumbo_pieces(sam_u, u3k(pag)); + u3z(pag); } static void @@ -2031,37 +1880,6 @@ _hear_peer(u3_mesa* sam_u, u3_peer* per_u, u3_lane lan_u, c3_o dir_o) } } -static void -_mesa_forward_response(u3_mesa_pict* pic_u, u3_lane lan_u) -{ - u3l_log("forwarding"); - u3_mesa_pact* pac_u = &pic_u->pac_u; - u3_mesa* sam_u = pic_u->sam_u; - update_hopcount(&pac_u->hed_u); - - if ( PACT_PAGE != pac_u->hed_u.typ_y ) { - MESA_LOG(STRANGE); - return; - } - //u3l_log("should update next hop"); - u3_weak hit = _mesa_get_cache(sam_u, &pac_u->pag_u.nam_u); - if ( u3_none == hit ) { - MESA_LOG(APATHY); - return; - } - u3_noun tag, dat; - u3x_cell(hit, &tag, &dat); - if ( tag == MESA_WAIT ) { - c3_y* buf_y = c3_calloc(PACT_SIZE); - // TODO actually stick next hop in packet - c3_w len_w = mesa_etch_pact(buf_y, pac_u); - _mesa_rout_bufs(sam_u, NULL, buf_y, len_w, u3k(u3t(dat))); - } else { - u3l_log("mesa: weird pending interest"); - } - mesa_free_pact(pac_u); -} - static u3_pend_req* _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) { @@ -2164,24 +1982,51 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) vec_init(&req_u->mis_u, 8); req_u = _mesa_put_request(sam_u, nam_u, req_u); - _update_oldest_req(req_u); + _update_resend_timer(req_u); return req_u; } +static void +_mesa_request_next_fragments(u3_mesa* sam_u, + u3_pend_req* req_u, + u3_lane lan_u) +{ + c3_w win_w = _mesa_req_get_cwnd(req_u); + u3_mesa_pict* nex_u = req_u->pic_u; + c3_w nex_w = req_u->nex_w; + for ( int i = 0; i < win_w; i++ ) { + c3_y* buf_y = c3_calloc(PACT_SIZE); + c3_w fra_w = nex_w + i; + if ( fra_w >= req_u->tot_w ) { + break; + } + nex_u->pac_u.pek_u.nam_u.fra_w = nex_w + i; + c3_w siz_w = mesa_etch_pact(buf_y, &nex_u->pac_u); + if ( siz_w == 0 ) { + u3l_log("failed to etch"); + u3_assert( 0 ); + } + // TODO: better route management + _mesa_send_buf(sam_u, lan_u, buf_y, siz_w); + _mesa_req_pact_sent(req_u, &nex_u->pac_u.pek_u.nam_u); + } +} + static void _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) { -#ifdef MESA_DEBUG - /* u3l_log("mesa hear page %u", pic_u->pac_u.pag_u.nam_u.fra_w); */ -#endif + #ifdef MESA_DEBUG + u3l_log("mesa: hear_page()"); + log_pact(&pic_u->pac_u); + u3_assert( PACT_PAGE == pic_u->pac_u.hed_u.typ_y ); + #endif - /* _log_buf(pic_u->pac_u.pag_u.dat_u.fra_y, pic_u->pac_u.pag_u.dat_u.len_w); */ u3_mesa* sam_u = pic_u->sam_u; u3_mesa_pact* pac_u = &pic_u->pac_u; + u3_mesa_name* nam_u = &pac_u->pek_u.nam_u; c3_s fra_s; - - c3_d* her_d = pac_u->pek_u.nam_u.her_d; + c3_d* her_d = nam_u->her_d; c3_o our_o = __( 0 == memcmp(her_d, sam_u->pir_u->who_d, sizeof(*her_d) * 2) ); // forwarding wrong, need a PIT entry @@ -2191,115 +2036,103 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) // return; // } - u3_peer* per_u = _mesa_get_peer(sam_u, pac_u->pag_u.nam_u.her_d); + u3_peer* per_u = _mesa_get_peer(sam_u, nam_u->her_d); c3_o new_o = c3n; if ( NULL == per_u ) { new_o = c3y; per_u = c3_calloc(sizeof(u3_peer)); _init_peer(sam_u, per_u); - _meet_peer(sam_u, per_u, pac_u->pag_u.nam_u.her_d); + _meet_peer(sam_u, per_u, nam_u->her_d); } c3_o dir_o = __(pac_u->hed_u.hop_y == 0); if ( pac_u->hed_u.hop_y == 0 ) { _hear_peer(sam_u, per_u, lan_u, dir_o); } else { - u3l_log("received forwarded page"); + u3l_log(" received forwarded page"); } if ( new_o == c3y ) { //u3l_log("new lane is direct %c", c3y == dir_o ? 'y' : 'n'); //_log_lane(&lan_u); } - _mesa_put_peer(sam_u, pac_u->pag_u.nam_u.her_d, per_u); + _mesa_put_peer(sam_u, nam_u->her_d, per_u); - u3_weak hit = _mesa_get_cache(sam_u, &pac_u->pag_u.nam_u); + u3_weak pin = _mesa_get_pit(sam_u, nam_u); - // XX better check to forward pages? - if ( u3_none != hit ) { - _mesa_forward_response(pic_u, lan_u); + if ( u3_none == pin ) { + #ifdef MESA_DEBUG + u3l_log(" no PIT entry"); + #endif + return; + } + u3_noun our, las; + u3x_cell(pin, &our, &las); + if ( u3_nul != las ) { + #ifdef MESA_DEBUG + u3l_log(" forwarding"); + #endif + // update_hopcount(&pac_u->hed_u); // TODO reinstate + // TODO actually stick next hop in packet + _mesa_send_pact(sam_u, u3k(las), per_u, pac_u); + _mesa_del_pit(sam_u, nam_u); + mesa_free_pact(pac_u); _mesa_free_pict(pic_u); - u3z(hit); + u3z(pin); + } + if ( c3n == our ) { + // TODO: free pact and pict + u3z(pin); return; } - if ( 1 == pac_u->pag_u.dat_u.tot_w ) { - u3_noun wir = u3nc(c3__ames, u3_nul); - u3_noun aut, cad; - - switch ( pac_u->pag_u.dat_u.aum_u.typ_e ) { - case AUTH_SIGN: { - aut = u3nc(c3y, u3i_bytes(64, pac_u->pag_u.dat_u.aum_u.sig_y)); - } break; - - case AUTH_HMAC: { - aut = u3nc(c3n, u3i_bytes(32, pac_u->pag_u.dat_u.aum_u.mac_y)); - } break; - - default: { - u3l_log("page: strange auth"); - _mesa_free_pict(pic_u); - return; - } - } + // process incoming response to ourselves + // TODO: use jumbo frames + // TODO: memory management, maybe free pict and pact + // if single-fragment message, inject directly into Arvo + if ( 1 == pac_u->pag_u.dat_u.tot_w ) { + u3_noun cad; { - // u3_noun pax = _mesa_encode_path(pac_u->pag_u.nam_u.pat_s, - // (c3_y*)(pac_u->pag_u.nam_u.pat_c)); - // u3_noun par = u3nc(u3i_chubs(2, pac_u->pag_u.nam_u.her_d), pax); - // u3_noun lan = u3nc(u3_nul, u3_mesa_encode_lane(lan_u)); - // u3_noun dat = u3i_bytes(pac_u->pag_u.dat_u.len_w, - // pac_u->pag_u.dat_u.fra_y); - - // cad = u3nt(c3__mess_ser, lan, - // u3nq(c3__page, par, aut, dat)); - u3_noun lan = u3_mesa_encode_lane(lan_u); u3i_slab sab_u; u3i_slab_init(&sab_u, 3, PACT_SIZE); // XX should just preserve input buffer c3_w cur_w = mesa_etch_pact(sab_u.buf_y, pac_u); - // _log_buf(sab_u.buf_y, cur_w); cad = u3nt(c3__heer, lan, u3i_slab_mint(&sab_u)); - } // XX should put in cache on success u3_auto_plan(&sam_u->car_u, - u3_ovum_init(0, c3__ames, wir, cad)); + u3_ovum_init(0, c3__ames, u3nc(c3__ames, u3_nul), cad)); _mesa_free_pict(pic_u); + u3z(pin); return; } - // u3_noun fra = u3i_bytes(pac_u->pag_u.dat_u.len_w, pac_u->pag_u.dat_u.fra_y) ; - /*if ( dop_o == c3n && pac_u->pag_u.nam_u.fra_w == 150) { - dop_o = c3y; - u3l_log("simulating dropped packet"); - return; - }*/ u3_pend_req* req_u; - /* req_u = _mesa_req_pact_init(sam_u, pic_u, &lan_u); */ - /* if ( req_u == NULL ) { */ - /* req_u = _mesa_req_pact_done(sam_u, &pac_u->pag_u.nam_u, &pac_u->pag_u.dat_u, &lan_u); */ - /* if ( req_u == NULL ) { */ - /* // cleanup */ - /* /\* u3l_log("wrong"); *\/ */ - /* _log_pact(pac_u); */ - /* _mesa_free_pict(pic_u); */ - /* return; */ - /* } */ - /* } */ - - if ( pac_u->pek_u.nam_u.nit_o == c3y ) { - /* u3l_log("_mesa_req_pact_init NIT"); */ + if ( c3y == nam_u->nit_o ) { + #ifdef MESA_LOG + u3l_log(" NIT"); + #endif req_u = _mesa_req_pact_init(sam_u, pic_u, &lan_u); if ( req_u == NULL ) { + #ifdef MESA_LOG + u3l_log(" init fail"); + #endif _mesa_free_pict(pic_u); return; } } else { + req_u = _mesa_get_request(sam_u, nam_u); + if ( !req_u ) { + _mesa_free_pict(pic_u); + // TODO free pin, other things too? + return; + } + u3_lane lon_u; if ( HOP_SHORT == pac_u->hed_u.nex_y ) { lon_u.pip_w = *(c3_w*)pac_u->pag_u.sot_u; @@ -2308,113 +2141,80 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) else { lon_u = lan_u; } - req_u = _mesa_req_pact_done(sam_u, - &pac_u->pag_u.nam_u, - &pac_u->pag_u.dat_u, - pac_u->hed_u.hop_y, - lon_u); - if ( req_u == NULL ) { - // cleanup - /* u3l_log("wrong"); */ - /* _log_pact(pac_u); */ - _mesa_free_pict(pic_u); - return; - } - /* u3l_log("right"); */ - /* _log_pact(pac_u); */ - } - - if ( req_u == NULL ) { - u3_assert(!"invalid"); - return; - } - c3_w win_w = _mesa_req_get_cwnd(req_u); - /* c3_w win_w = 100000; */ - u3_mesa_pict* nex_u = req_u->pic_u; - c3_w nex_w = req_u->nex_w; - if ( win_w != 0 ) { -#ifdef MESA_DEBUG - /* u3l_log("continuing flow nex: %u, win: %u", nex_w, win_w); */ - /* u3l_log("in flight %u", bitset_wyt(&req_u->was_u)); */ -#endif - for ( int i = 0; i < win_w; i++ ) { - c3_y* buf_y = c3_calloc(PACT_SIZE); - c3_w fra_w = nex_w + i; - if ( fra_w >= req_u->tot_w ) { - break; - } - nex_u->pac_u.pek_u.nam_u.fra_w = nex_w + i; - c3_w siz_w = mesa_etch_pact(buf_y, &nex_u->pac_u); - // _log_buf(buf_y, siz_w); - if ( siz_w == 0 ) { - u3l_log("failed to etch"); - u3_assert( 0 ); - } - // TODO: better route management - _mesa_send_buf(sam_u, lan_u, buf_y, siz_w); - _mesa_req_pact_sent(req_u, &nex_u->pac_u.pek_u.nam_u); - } + _mesa_req_pact_done(req_u, + nam_u, + &pac_u->pag_u.dat_u, + pac_u->hed_u.hop_y, + lon_u); } - if ( req_u->len_w == req_u->tot_w ) { - // fprintf(stderr, "finished"); - // u3l_log("queue size %u", req_u->mis_u.len_w); - c3_d now_d = _get_now_micros(); - u3l_log("%u kilobytes took %f ms", req_u->tot_w, (now_d - sam_u->tim_d)/1000.0); - c3_w siz_w = (1 << (pac_u->pag_u.nam_u.boq_y - 3)); - u3_noun dat = u3i_bytes((siz_w * req_u->tot_w), req_u->dat_y); - u3_noun wir = u3nc(c3__ames, u3_nul); + c3_o done_with_jumbo_frame = __(req_u->len_w == req_u->tot_w); // TODO: fix for non-message-sized jumbo frames + if ( c3y == done_with_jumbo_frame ) { u3_noun cad; { - // u3_noun pax = _mesa_encode_path(pac_u->pag_u.nam_u.pat_s, - // (c3_y*)(pac_u->pag_u.nam_u.pat_c)); - // u3_noun par = u3nc(u3i_chubs(2, pac_u->pag_u.nam_u.her_d), pax); - // u3_noun lan = u3_nul; - // u3_noun aut = u3nc(c3y, 0); // XX s/b saved in request state - - // cad = u3nt(c3__mess_ser, lan, - // u3nq(c3__page, par, aut, dat)); - u3_noun lan = u3_mesa_encode_lane(lan_u); - u3i_slab sab_u; - // u3i_slab_init(&sab_u, 3, PACT_SIZE); - // u3i_slab_init(&sab_u, 3, (siz_w * req_u->tot_w) + 135); - /* u3l_log("slab size %u", (PACT_SIZE - pac_u->pag_u.dat_u.len_w) + (siz_w * req_u->tot_w)); */ - u3i_slab_init(&sab_u, 3, (PACT_SIZE - pac_u->pag_u.dat_u.len_w) + (siz_w * req_u->tot_w)); - - pac_u->pag_u.dat_u.len_w = (siz_w * req_u->tot_w); - pac_u->pag_u.dat_u.fra_y = c3_realloc(pac_u->pag_u.dat_u.fra_y, pac_u->pag_u.dat_u.len_w); - memcpy(pac_u->pag_u.dat_u.fra_y, req_u->dat_y, pac_u->pag_u.dat_u.len_w); + // construct jumbo frame + c3_w jumbo_len_w = (1024 * (req_u->tot_w - 1)) + pac_u->pag_u.dat_u.len_w; + pac_u->pag_u.nam_u.boq_y = 31; // TODO: use actual jumbo bloq + pac_u->pag_u.dat_u.len_w = jumbo_len_w; + pac_u->pag_u.dat_u.fra_y = c3_realloc(pac_u->pag_u.dat_u.fra_y, jumbo_len_w); + memcpy(pac_u->pag_u.dat_u.fra_y, req_u->dat_y, jumbo_len_w); + pic_u->pac_u.pag_u.nam_u.fra_w = 0; + u3_noun lan = u3_mesa_encode_lane(lan_u); - /* u3l_log("last frag %u", pic_u->pac_u.pag_u.nam_u.fra_w); */ - // this could be a retry, so we just rewrite the fragment to be the last one - // - pic_u->pac_u.pag_u.nam_u.fra_w = req_u->tot_w - 1; - // XX should just preserve input buffer - c3_w res = mesa_etch_pact(sab_u.buf_y, pac_u); - /* u3l_log("slab len_w %u mesa_etch_pact %u", sab_u.len_w, res); */ - - cad = u3nt(c3__heer, lan, u3i_slab_mint(&sab_u)); + c3_y* buf_y = c3_calloc((PACT_SIZE - 1024) + jumbo_len_w); + c3_w res_w = mesa_etch_pact(buf_y, pac_u); + cad = u3nt(c3__heer, lan, u3i_bytes(res_w, buf_y)); + c3_free(buf_y); } _mesa_del_request(sam_u, &pac_u->pag_u.nam_u); u3_auto_plan(&sam_u->car_u, - u3_ovum_init(0, c3__ames, wir, cad)); + u3_ovum_init(0, c3__ames, u3nc(c3__ames, u3_nul), cad)); + } + if ( req_u->len_w < req_u->tot_w ) { + _mesa_request_next_fragments(sam_u, req_u, lan_u); + } + else { + u3l_log(" received last packet, tot_w: %u", req_u->tot_w); + c3_d now_d = _get_now_micros(); + u3l_log("%u kilobytes took %f ms", req_u->tot_w, (now_d - sam_u->tim_d)/1000.0); + // clean up pend_req + _mesa_free_pict(pic_u); } +} - _mesa_free_pict(pic_u); +static void +_mesa_add_lane_to_pit(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_lane lan_u) +{ + // TODO: prevent duplicate lane from being added + u3_noun lan = u3_mesa_encode_lane(lan_u); + u3_weak pin = _mesa_get_pit(sam_u, nam_u); + if ( u3_none == pin ) { + pin = u3nt(c3n, u3k(lan), u3_nul); + } + else { + pin = u3nt(u3k(u3h(pin)), u3k(lan), u3k(u3t(pin))); + } + _mesa_put_pit(sam_u, nam_u, u3k(pin)); + u3z(lan); u3z(pin); + return; } static void -_mesa_add_lane_to_cache(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_noun las, u3_lane lan_u) +_mesa_add_our_to_pit(u3_mesa* sam_u, u3_mesa_name* nam_u) { - u3_noun hit = u3nq(MESA_WAIT, - _mesa_get_now(), - u3_mesa_encode_lane(lan_u), - u3k(las)); - _mesa_put_jumbo_cache(sam_u, nam_u, hit); - u3z(las); + u3_weak pin = _mesa_get_pit(sam_u, nam_u); + if ( u3_none == pin ) { + pin = u3nc(c3y, u3_nul); + } + else { + pin = u3nc(c3y, u3k(u3t(pin))); + } + _mesa_put_pit(sam_u, nam_u, u3k(pin)); + u3z(pin); + return; } // XX forwarding wrong, need a PIT entry @@ -2424,18 +2224,19 @@ _mesa_forward_request(u3_mesa* sam_u, u3_mesa_pict* pic_u) u3_mesa_pact* pac_u = &pic_u->pac_u; c3_d* her_d = pac_u->pek_u.nam_u.her_d; u3_peer* per_u = _mesa_get_peer(sam_u, her_d); - if ( per_u == NULL ) { + if ( !per_u ) { // XX leaks u3l_log("mesa: alien forward for %s", u3r_string(u3dc("scot", c3__p, u3i_chubs(2, her_d)))); _mesa_free_pict(pic_u); return; } if ( c3y == sam_u->for_o && sam_u->pir_u->who_d[0] == per_u->imp_y ) { -//#ifdef MESA_DEBUG u3_lane lin_u = _mesa_get_direct_lane(sam_u, her_d); -//#endif - //_update_hopcount(&pac_u->hed_u); - u3l_log("sending peek %u", pic_u->pac_u.pek_u.nam_u.fra_w); + //_update_hopcount(&pac_u->hed_u); // TODO reinstate + #ifdef MESA_DEBUG + u3l_log("mesa: forward_request()"); + log_pact(pac_u); + #endif _mesa_send(pic_u, &lin_u); } _mesa_free_pict(pic_u); @@ -2444,16 +2245,18 @@ _mesa_forward_request(u3_mesa* sam_u, u3_mesa_pict* pic_u) static void _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) { -#ifdef MESA_DEBUG - // u3l_log("mesa: hear peek"); - // u3_assert(pac_u->hed_u.typ_y == PACT_PEEK); -#endif + #ifdef MESA_DEBUG + u3l_log("mesa: hear_peek()\r\n\r\n\r\n\r\n\r\n"); + u3_assert( PACT_PEEK == pic_u->pac_u.hed_u.typ_y ); + #endif + u3_mesa_pact* pac_u = &pic_u->pac_u; u3_mesa* sam_u = pic_u->sam_u; c3_d* her_d = pac_u->pek_u.nam_u.her_d; c3_o our_o = __( 0 == memcmp(her_d, sam_u->pir_u->who_d, sizeof(*her_d) * 2) ); if ( c3n == our_o ) { + u3l_log(" forwarding\r\n"); _mesa_forward_request(sam_u, pic_u); return; } @@ -2461,61 +2264,41 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) c3_w bat_w = _mesa_lop(fra_w); pac_u->pek_u.nam_u.fra_w = bat_w; - /* _log_pact(pac_u); */ - /* u3l_log("_mesa_hear_peek %s", pac_u->pek_u.nam_u.pat_c); */ - u3_weak hit = _mesa_get_jumbo_cache(sam_u, &pac_u->pek_u.nam_u); - - /* u3l_log("peek fra %u hit %u", fra_w, hit != u3_none); */ - /* u3l_log("peek fra %u bat %u hit %u", fra_w, bat_w, hit != u3_none); */ + + // record interest + u3l_log(" adding lane to pit\r\n"); + _mesa_add_lane_to_pit(sam_u, &pac_u->pek_u.nam_u, lan_u); + u3l_log(" added\r\n"); + // if we have the page, send it + u3_weak hit = _mesa_get_jumbo_cache(sam_u, &pac_u->pek_u.nam_u); if ( u3_none != hit ) { + u3l_log(" hit\r\n"); u3_noun tag, dat; - u3x_cell(u3k(hit), &tag, &dat); - if ( tag == MESA_WAIT ) { - /* u3l_log("MESA_WAIT for %u", bat_w); */ - _mesa_add_lane_to_cache(sam_u, &pac_u->pek_u.nam_u, u3k(u3t(dat)), lan_u); - } else if ( c3y == our_o && tag == MESA_ITEM ) { // XX our_o redundant - pac_u->pek_u.nam_u.fra_w = fra_w; - c3_y* buf_y; - u3_weak hit_2 = _mesa_get_cache(sam_u, &pac_u->pek_u.nam_u); - u3_noun tag_2, dat_2; - u3x_cell(hit_2, &tag_2, &dat_2); - /* u3l_log("cache hit %u bat %u", fra_w, bat_w); */ - c3_w len_w = _mesa_fill_buf(pic_u, &buf_y, u3k(dat_2)); - // _log_buf(buf_y, len_w); - _mesa_send_buf(sam_u, lan_u, buf_y, len_w); - u3z(hit_2); - } else { - u3l_log("mesa: weird case in cache, dropping"); + u3x_cell(hit, &tag, &dat); + if ( MESA_ITEM == tag ) { + _mesa_send_jumbo_pieces(sam_u, u3k(dat)); + _mesa_free_pict(pic_u); } - _mesa_free_pict(pic_u); u3z(hit); return; - } else { - _mesa_add_lane_to_cache(sam_u, &pac_u->pek_u.nam_u, u3_nul, lan_u); // TODO: retrieve from namespace - if ( c3y == our_o ) { - /* u3_noun sky = _name_to_batch_scry(&pac_u->pek_u.nam_u, */ - /* bat_w, */ - /* bat_w + MESA_HUNK); */ - - u3_noun sky = _name_to_jumbo_scry(&pac_u->pek_u.nam_u); - - pac_u->pek_u.nam_u.fra_w = fra_w; - - u3_noun our = u3i_chubs(2, sam_u->car_u.pir_u->who_d); - u3_noun bem = u3nc(u3nt(our, u3_nul, u3nc(c3__ud, 1)), sky); - // only branch where we do not free pic_u - u3_pier_peek(sam_u->car_u.pir_u, u3_nul, u3k(u3nq(1, c3__beam, c3__ax, bem)), pic_u, _mesa_page_scry_jumbo_cb); - } else { - // XX unpossible - _mesa_free_pict(pic_u); - } } - // u3z(pax); + // otherwise, scry + u3l_log(" got hit\r\n"); + _mesa_put_jumbo_cache(sam_u, &pac_u->pek_u.nam_u, u3nc(MESA_WAIT, 0)); + u3_noun sky = _name_to_jumbo_scry(&pac_u->pek_u.nam_u); + pac_u->pek_u.nam_u.fra_w = fra_w; + u3_noun our = u3i_chubs(2, sam_u->car_u.pir_u->who_d); + u3_noun bem = u3nc(u3nt(our, u3_nul, u3nc(c3__ud, 1)), sky); + // NOTE: pic_u not freed + u3l_log(" about to peek\r\n"); + u3_pier_peek(sam_u->car_u.pir_u, u3_nul, u3k(u3nq(1, c3__beam, c3__ax, bem)), pic_u, _mesa_page_scry_jumbo_cb); + u3z(hit); + u3l_log(" ran\r\n"); } static void -_mesa_poke_news(u3_ovum* egg_u, u3_ovum_news new_e) +_mesa_poke_news_cb(u3_ovum* egg_u, u3_ovum_news new_e) { u3_mesa_pict* pic_u = egg_u->ptr_v; @@ -2526,7 +2309,7 @@ _mesa_poke_news(u3_ovum* egg_u, u3_ovum_news new_e) } static void -_mesa_poke_bail(u3_ovum* egg_u, u3_noun lud) +_mesa_poke_bail_cb(u3_ovum* egg_u, u3_noun lud) { u3_mesa_pict* pic_u = egg_u->ptr_v; // XX failure stuff here @@ -2538,16 +2321,15 @@ _mesa_poke_bail(u3_ovum* egg_u, u3_noun lud) static void _mesa_hear_poke(u3_mesa_pict* pic_u, u3_lane* lan_u) { - u3_mesa_pact* pac_u = &pic_u->pac_u; u3_mesa* sam_u = pic_u->sam_u; c3_d* her_d = pac_u->pek_u.nam_u.her_d; c3_o our_o = __( 0 == memcmp(her_d, sam_u->pir_u->who_d, sizeof(*her_d) * 2) ); -#ifdef MESA_DEBUG - // u3l_log("mesa: hear poke"); - u3_assert(pac_u->hed_u.typ_y == PACT_POKE); -#endif + #ifdef MESA_DEBUG + u3l_log("mesa: hear_poke()"); + u3_assert( PACT_POKE == pac_u->hed_u.typ_y ); + #endif if ( c3n == our_o ) { _mesa_forward_request(sam_u, pic_u); @@ -2580,13 +2362,12 @@ _mesa_hear_poke(u3_mesa_pict* pic_u, u3_lane* lan_u) _mesa_put_peer(sam_u, pac_u->pok_u.pay_u.her_d, per_u); // XX could check cache for ack (completed duplicate) + _mesa_add_lane_to_pit(sam_u, &pac_u->pek_u.nam_u, *lan_u); u3_ovum_peer nes_f; u3_ovum_bail bal_f; void* ptr_v; - // XX create PIT entry for ack - u3_noun wir = u3nc(c3__ames, u3_nul); u3_noun cad; { @@ -2607,8 +2388,8 @@ _mesa_hear_poke(u3_mesa_pict* pic_u, u3_lane* lan_u) else { assert(pac_u->pok_u.dat_u.tot_w); // XX check request state for *payload* (in-progress duplicate) - nes_f = _mesa_poke_news; - bal_f = _mesa_poke_bail; + nes_f = _mesa_poke_news_cb; + bal_f = _mesa_poke_bail_cb; ptr_v = pic_u; } diff --git a/pkg/vere/io/mesa/mesa.h b/pkg/vere/io/mesa/mesa.h index a5e08292cf..7dd60b5ad6 100644 --- a/pkg/vere/io/mesa/mesa.h +++ b/pkg/vere/io/mesa/mesa.h @@ -159,6 +159,7 @@ void update_hopcount(u3_mesa_head*); void mesa_free_pact(u3_mesa_pact* pac_u); -void _log_pact(u3_mesa_pact* pac_u); +void log_pact(u3_mesa_pact* pac_u); +void log_name(u3_mesa_name* nam_u); #endif diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index 88e175bb5f..59c78eb73f 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -39,8 +39,8 @@ _log_buf(c3_y* buf_y, c3_w len_w) fprintf(stderr, "\r\n"); } -static void -_log_name(u3_mesa_name* nam_u) +void +log_name(u3_mesa_name* nam_u) { // u3l_log("meta"); // u3l_log("rank: %u", nam_u->met_u.ran_y); @@ -49,27 +49,28 @@ _log_name(u3_mesa_name* nam_u) // u3l_log("tau: %u", nam_u->met_u.tau_y); // u3l_log("frag num length: %u", nam_u->met_u.gaf_y); + c3_c* her_c; { - // u3_noun her = u3dc("scot", c3__p, u3i_chubs(2, nam_u->her_d)); - // c3_c* her_c = u3r_string(her); - // u3l_log("publisher: %s", her_c); - // c3_free(her_c); - // u3z(her); - } - - u3l_log("rift: %u", nam_u->rif_w); - u3l_log("bloq: %u", nam_u->boq_y); - u3l_log("init: %s", (c3y == nam_u->nit_o) ? "&" : "|"); - u3l_log("auth: %s", (c3y == nam_u->aut_o) ? "&" : "|"); - u3l_log("frag: %u", nam_u->fra_w); - u3l_log("path len: %u", nam_u->pat_s); - u3l_log("path: %s", nam_u->pat_c); + u3_noun her = u3dc("scot", c3__p, u3i_chubs(2, nam_u->her_d)); + her_c = u3r_string(her); + u3z(her); + } + + u3l_log("%s: /%s", her_c, nam_u->pat_c); + u3l_log(" rift: %u bloq: %u init: %s auth: %s frag: %u", + nam_u->rif_w, + nam_u->boq_y, + (c3y == nam_u->nit_o) ? "&" : "|", + (c3y == nam_u->aut_o) ? "&" : "|", + nam_u->fra_w + ); + c3_free(her_c); } static void _log_data(u3_mesa_data* dat_u) { - u3l_log("total fragments: %u", dat_u->tot_w); + fprintf(stderr, "tot_w: %u len_w: %u ", dat_u->tot_w, dat_u->len_w); switch ( dat_u->aum_u.typ_e ) { case AUTH_NONE: { @@ -93,12 +94,12 @@ _log_data(u3_mesa_data* dat_u) } break; case AUTH_SIGN: { - u3l_log("signature:"); + fprintf(stderr, "signature: "); _log_buf(dat_u->aum_u.sig_y, 64); } break; case AUTH_HMAC: { - u3l_log("hmac:"); + fprintf(stderr, "hmac: "); _log_buf(dat_u->aum_u.mac_y, 32); } break; } @@ -124,33 +125,33 @@ _log_data(u3_mesa_data* dat_u) default: break; } - - u3l_log("frag len: %u", dat_u->len_w); + fprintf(stderr, "\r\n"); } + static void _log_peek_pact(u3_mesa_peek_pact* pac_u) { - _log_name(&pac_u->nam_u); + log_name(&pac_u->nam_u); } static void _log_page_pact(u3_mesa_page_pact *pac_u) { - _log_name(&pac_u->nam_u); + log_name(&pac_u->nam_u); _log_data(&pac_u->dat_u); } static void _log_poke_pact(u3_mesa_poke_pact *pac_u) { - _log_name(&pac_u->nam_u); - _log_name(&pac_u->pay_u); + log_name(&pac_u->nam_u); + log_name(&pac_u->pay_u); _log_data(&pac_u->dat_u); } void -_log_pact(u3_mesa_pact* pac_u) +log_pact(u3_mesa_pact* pac_u) { switch ( pac_u->hed_u.typ_y ) { case PACT_PEEK: { @@ -205,6 +206,7 @@ _mesa_rank(c3_d who_d[2]) */ void mesa_free_pact(u3_mesa_pact* pac_u) { + // TODO free pat_c in the name switch ( pac_u->hed_u.typ_y ) { default: { break; @@ -1152,13 +1154,13 @@ _test_pact(u3_mesa_pact* pac_u) done: if ( ret_i ) { _log_head(&pac_u->hed_u); - _log_pact(pac_u); + log_pact(pac_u); _log_buf(buf_y, len_w); if ( bot_i ) { u3l_log(RED_TEXT); _log_head(&nex_u.hed_u); - _log_pact(&nex_u); + log_pact(&nex_u); u3l_log(DEF_TEXT); } } From 717bae5daed175fc74dd34448892923e15bc0dbb Mon Sep 17 00:00:00 2001 From: Matthew LeVan Date: Fri, 12 Jul 2024 12:48:37 -0500 Subject: [PATCH 229/430] mass: `quac` cleanup --- pkg/noun/allocate.c | 24 +++++++++++------------- pkg/noun/allocate.h | 11 ++++++++--- pkg/noun/jets.c | 10 +++++----- pkg/noun/jets.h | 2 +- pkg/noun/manage.c | 6 +++--- pkg/noun/manage.h | 10 +++++----- pkg/noun/nock.c | 6 +++--- pkg/noun/nock.h | 2 +- pkg/noun/vortex.c | 7 +++---- pkg/noun/vortex.h | 2 +- pkg/vere/king.c | 6 +++--- pkg/vere/serf.c | 30 +++++++++++++++--------------- pkg/vere/serf.h | 5 ----- 13 files changed, 59 insertions(+), 62 deletions(-) diff --git a/pkg/noun/allocate.c b/pkg/noun/allocate.c index 620d5fac5c..f17392f4f0 100644 --- a/pkg/noun/allocate.c +++ b/pkg/noun/allocate.c @@ -9,8 +9,6 @@ #include "retrieve.h" #include "trace.h" #include "vortex.h" -#include "noun.h" -#include "defs.h" u3_road* u3a_Road; @@ -2032,7 +2030,7 @@ _ca_print_memory(FILE* fil_u, c3_w byt_w) /* u3a_quac_free: free quac memory. */ void -u3a_quac_free(quac* qua_u) +u3a_quac_free(u3m_quac* qua_u) { for ( c3_w i_w = 0; i_w < qua_u->len_w; i_w++ ) { u3a_quac_free(qua_u->qua_u[i_w]); @@ -2044,11 +2042,11 @@ u3a_quac_free(quac* qua_u) /* u3a_prof(): mark/measure/print memory profile. RETAIN. */ -quac* +u3m_quac* u3a_prof(FILE* fil_u, u3_noun mas) { // c3_w tot_w = 0; - quac* pro_u = c3_calloc(sizeof(*pro_u)); + u3m_quac* pro_u = c3_calloc(sizeof(*pro_u)); u3_noun h_mas, t_mas; if ( c3n == u3r_cell(mas, &h_mas, &t_mas) ) { @@ -2120,7 +2118,7 @@ u3a_prof(FILE* fil_u, u3_noun mas) c3_w i_w = 2; c3_t bad_t = 0; while ( c3y == u3du(tt_mas) ) { - quac* new_u = u3a_prof(fil_u, u3h(tt_mas)); + u3m_quac* new_u = u3a_prof(fil_u, u3h(tt_mas)); if ( NULL == new_u ) { c3_free(new_u); bad_t = 1; @@ -2133,7 +2131,7 @@ u3a_prof(FILE* fil_u, u3_noun mas) tt_mas = u3t(tt_mas); i_w++; } - + if ( bad_t ) { for ( i_w = 0; i_w < pro_u->len_w ; i_w++ ) { u3a_quac_free(pro_u->qua_u[i_w]); @@ -2155,14 +2153,14 @@ u3a_prof(FILE* fil_u, u3_noun mas) } -/* u3a_print_quac: print a quac memory report. +/* u3a_print_quac: print a memory report. */ void -u3a_print_quac(FILE* fil_u, c3_w den_w, quac* mas_u) +u3a_print_quac(FILE* fil_u, c3_w den_w, u3m_quac* mas_u) { u3_assert( 0 != fil_u ); - + if ( mas_u->siz_w ) { fprintf(fil_u, "%*s%s: ", den_w, "", mas_u->nam_c); @@ -2182,10 +2180,10 @@ u3a_print_quac(FILE* fil_u, c3_w den_w, quac* mas_u) /* u3a_mark_road(): mark ad-hoc persistent road structures. */ -quac* +u3m_quac* u3a_mark_road() { - quac** qua_u = c3_malloc(sizeof(*qua_u)*8); + u3m_quac** qua_u = c3_malloc(sizeof(*qua_u)*8); qua_u[0] = c3_calloc(sizeof(*qua_u[0])); qua_u[0]->nam_c = strdup("namespace"); @@ -2224,7 +2222,7 @@ u3a_mark_road() sum_w += qua_u[i_w]->siz_w; } - quac* tot_u = c3_malloc(sizeof(*tot_u)); + u3m_quac* tot_u = c3_malloc(sizeof(*tot_u)); tot_u->nam_c = strdup("total road stuff"); tot_u->siz_w = sum_w; tot_u->len_w = 8; diff --git a/pkg/noun/allocate.h b/pkg/noun/allocate.h index 7763fd3183..d0954f9656 100644 --- a/pkg/noun/allocate.h +++ b/pkg/noun/allocate.h @@ -606,7 +606,7 @@ /* u3a_mark_road(): mark ad-hoc persistent road structures. */ - quac* + u3m_quac* u3a_mark_road(); /* u3a_reclaim(): clear ad-hoc persistent caches to reclaim memory. @@ -705,7 +705,7 @@ /* u3a_print_quac: print a quac memory report. */ void - u3a_print_quac(FILE* fil_u, c3_w den_w, quac* mas_u); + u3a_print_quac(FILE* fil_u, c3_w den_w, u3m_quac* mas_u); /* u3a_print_memory(): print memory amount. */ @@ -713,7 +713,7 @@ u3a_print_memory(FILE* fil_u, c3_c* cap_c, c3_w wor_w); /* u3a_prof(): mark/measure/print memory profile. RETAIN. */ - quac* + u3m_quac* u3a_prof(FILE* fil_u, u3_noun mas); /* u3a_maid(): maybe print memory. @@ -721,6 +721,11 @@ c3_w u3a_maid(FILE* fil_u, c3_c* cap_c, c3_w wor_w); + /* u3a_quac_free(): free quac memory. + */ + void + u3a_quac_free(u3m_quac* qua_u); + /* u3a_uncap_print_memory(): un-captioned print memory amount. */ void diff --git a/pkg/noun/jets.c b/pkg/noun/jets.c index e64ce26c8a..2f1dc3a0df 100644 --- a/pkg/noun/jets.c +++ b/pkg/noun/jets.c @@ -2305,10 +2305,10 @@ _cj_mark_hank(u3_noun kev, void* dat) /* u3j_mark(): mark jet state for gc. */ -quac* +u3m_quac* u3j_mark() { - quac** qua_u = c3_malloc(sizeof(*qua_u)*6); + u3m_quac** qua_u = c3_malloc(sizeof(*qua_u)*6); qua_u[0] = c3_calloc(sizeof(*qua_u[0])); qua_u[0]->nam_c = strdup("warm jet state"); @@ -2325,18 +2325,18 @@ u3j_mark() qua_u[3] = c3_calloc(sizeof(*qua_u[3])); qua_u[3]->nam_c = strdup("battery hash cache"); qua_u[3]->siz_w = u3h_mark(u3R->jed.bas_p)*4; - + qua_u[4] = c3_calloc(sizeof(*qua_u[4])); qua_u[4]->nam_c = strdup("call site cache"); u3h_walk_with(u3R->jed.han_p, _cj_mark_hank, &qua_u[4]->siz_w); qua_u[4]->siz_w = qua_u[4]->siz_w*4; - + c3_w sum_w = 0; for ( c3_w i_w = 0; i_w < 5; i_w++ ) { sum_w += qua_u[i_w]->siz_w; } - quac* tot_u = c3_calloc(sizeof(*tot_u)); + u3m_quac* tot_u = c3_calloc(sizeof(*tot_u)); tot_u->nam_c = strdup("total jet stuff"); if ( u3R == &(u3H->rod_u) ) { diff --git a/pkg/noun/jets.h b/pkg/noun/jets.h index 02c2c54950..81301d1199 100644 --- a/pkg/noun/jets.h +++ b/pkg/noun/jets.h @@ -296,7 +296,7 @@ /* u3j_mark(): mark jet state for gc. */ - quac* + u3m_quac* u3j_mark(); /* u3j_free(): free jet state. diff --git a/pkg/noun/manage.c b/pkg/noun/manage.c index 7726ce6c2b..3a462ef9ea 100644 --- a/pkg/noun/manage.c +++ b/pkg/noun/manage.c @@ -459,15 +459,15 @@ u3m_file(c3_c* pas_c) /* u3m_mark(): mark all nouns in the road. */ -quac** +u3m_quac** u3m_mark() { - quac** qua_u = c3_malloc(sizeof(*qua_u)*4); + u3m_quac** qua_u = c3_malloc(sizeof(*qua_u)*4); qua_u[0] = u3v_mark(); qua_u[1] = u3j_mark(); qua_u[2] = u3n_mark(); qua_u[3] = u3a_mark_road(); - + return qua_u; } diff --git a/pkg/noun/manage.h b/pkg/noun/manage.h index ce1e66a86b..33c75b4ee4 100644 --- a/pkg/noun/manage.h +++ b/pkg/noun/manage.h @@ -149,18 +149,18 @@ u3m_soft_esc(u3_noun ref, u3_noun sam); - /* quac: memory report. + /* u3m_quac: memory report. */ - typedef struct _quac { + typedef struct _u3m_quac { c3_c* nam_c; c3_w siz_w; c3_w len_w; - struct _quac** qua_u; - } quac; + struct _u3m_quac** qua_u; + } u3m_quac; /* u3m_mark(): mark all nouns in the road. */ - quac** + u3m_quac** u3m_mark(); /* u3m_grab(): garbage-collect the world, plus extra roots. diff --git a/pkg/noun/nock.c b/pkg/noun/nock.c index 60f713f831..e7b1ab64be 100644 --- a/pkg/noun/nock.c +++ b/pkg/noun/nock.c @@ -3047,10 +3047,10 @@ _n_bam(u3_noun kev, void* dat) /* u3n_mark(): mark the bytecode cache for gc. */ -quac* +u3m_quac* u3n_mark() { - quac** qua_u = c3_malloc(sizeof(*qua_u)*2); + u3m_quac** qua_u = c3_malloc(sizeof(*qua_u)*2); qua_u[0] = c3_calloc(sizeof(*qua_u[0])); qua_u[0]->nam_c = strdup("bytecode programs"); @@ -3063,7 +3063,7 @@ u3n_mark() qua_u[1]->nam_c = strdup("bytecode cache"); qua_u[1]->siz_w = u3h_mark(har_p)*4; - quac* tot_u = c3_malloc(sizeof(*tot_u)); + u3m_quac* tot_u = c3_malloc(sizeof(*tot_u)); tot_u->nam_c = strdup("total nock stuff"); tot_u->siz_w = qua_u[0]->siz_w + qua_u[1]->siz_w; tot_u->len_w = 2; diff --git a/pkg/noun/nock.h b/pkg/noun/nock.h index 19489d304c..266438119d 100644 --- a/pkg/noun/nock.h +++ b/pkg/noun/nock.h @@ -123,7 +123,7 @@ /* u3n_mark(): mark bytecode cache. */ - quac* + u3m_quac* u3n_mark(); /* u3n_reclaim(): clear ad-hoc persistent caches to reclaim memory. diff --git a/pkg/noun/vortex.c b/pkg/noun/vortex.c index 8de5b2bd88..c49d73780f 100644 --- a/pkg/noun/vortex.c +++ b/pkg/noun/vortex.c @@ -392,12 +392,12 @@ u3v_sway(u3_noun blu, c3_l tab_l, u3_noun tax) /* u3v_mark(): mark arvo kernel. */ -quac* +u3m_quac* u3v_mark() { u3v_arvo* arv_u = &(u3H->arv_u); - quac** qua_u = c3_malloc(sizeof(*qua_u)*3); + u3m_quac** qua_u = c3_malloc(sizeof(*qua_u)*3); qua_u[0] = c3_calloc(sizeof(*qua_u[0])); qua_u[0]->nam_c = strdup("kernel"); @@ -411,7 +411,7 @@ u3v_mark() qua_u[2]->nam_c = strdup("wish cache"); qua_u[2]->siz_w = u3a_mark_noun(arv_u->yot)*4; - quac* tot_u = c3_malloc(sizeof(*tot_u)); + u3m_quac* tot_u = c3_malloc(sizeof(*tot_u)); tot_u->nam_c = strdup("total arvo stuff"); tot_u->siz_w = qua_u[0]->siz_w + qua_u[1]->siz_w + qua_u[2]->siz_w; tot_u->len_w = 3; @@ -450,4 +450,3 @@ u3v_rewrite_compact() arv_u->now = u3a_rewritten_noun(arv_u->now); arv_u->yot = u3a_rewritten_noun(arv_u->yot); } - diff --git a/pkg/noun/vortex.h b/pkg/noun/vortex.h index c254b32625..e2377fb9be 100644 --- a/pkg/noun/vortex.h +++ b/pkg/noun/vortex.h @@ -130,7 +130,7 @@ /* u3v_mark(): mark arvo kernel. */ - quac* + u3m_quac* u3v_mark(); /* u3v_reclaim(): clear ad-hoc persistent caches to reclaim memory. diff --git a/pkg/vere/king.c b/pkg/vere/king.c index c1ddfd9d04..668237245a 100644 --- a/pkg/vere/king.c +++ b/pkg/vere/king.c @@ -1688,9 +1688,9 @@ u3_king_grab(void* vod_p) } #endif - quac** all_u = c3_malloc(sizeof(*all_u)*6); + u3m_quac** all_u = c3_malloc(sizeof(*all_u)*6); - quac** var_u = u3m_mark(); + u3m_quac** var_u = u3m_mark(); all_u[0] = var_u[0]; all_u[1] = var_u[1]; all_u[2] = var_u[2]; @@ -1707,7 +1707,7 @@ u3_king_grab(void* vod_p) all_u[5] = c3_calloc(sizeof(*all_u[5])); all_u[5]->nam_c = "sweep"; all_u[5]->siz_w = u3a_sweep(); - + for ( c3_w i_w = 0; i_w < 6; i_w++ ) { u3a_print_quac(fil_u, 0, all_u[i_w]); u3a_quac_free(all_u[i_w]); diff --git a/pkg/vere/serf.c b/pkg/vere/serf.c index 658a0b9ff3..bf0d4ae949 100644 --- a/pkg/vere/serf.c +++ b/pkg/vere/serf.c @@ -75,7 +75,7 @@ enum { /* _serf_quac: convert a quac to a noun. */ u3_noun -_serf_quac(quac* mas_u) +_serf_quac(u3m_quac* mas_u) { u3_noun list = u3_nul; for ( c3_w i_w = 0; i_w < mas_u->len_w; i_w++ ) { @@ -84,7 +84,7 @@ _serf_quac(quac* mas_u) list = u3kb_flop(list); u3_noun mas = u3nt(u3i_string(mas_u->nam_c), u3i_word(mas_u->siz_w), list); - + c3_free(mas_u->nam_c); c3_free(mas_u->qua_u); c3_free(mas_u); @@ -95,7 +95,7 @@ _serf_quac(quac* mas_u) /* _serf_quacs: convert an array of quacs to a noun list. */ u3_noun -_serf_quacs(c3_w len_w, quac** all_u) +_serf_quacs(c3_w len_w, u3m_quac** all_u) { u3_noun list = u3_nul; for ( c3_w i_w = 0; i_w < len_w; i_w++ ) { @@ -108,7 +108,7 @@ _serf_quacs(c3_w len_w, quac** all_u) /* _serf_print_quacs: print an array of quacs. */ void -_serf_print_quacs(FILE* fil_u, c3_w len_w, quac** all_u) +_serf_print_quacs(FILE* fil_u, c3_w len_w, u3m_quac** all_u) { for ( c3_w i_w = 0; i_w < len_w; i_w++) { u3a_print_quac(fil_u, 0, all_u[i_w]); @@ -159,25 +159,25 @@ _serf_grab(u3_noun sac, c3_o pri_o) u3_assert( u3R == &(u3H->rod_u) ); - quac* pro_u = u3a_prof(fil_u, sac); + u3m_quac* pro_u = u3a_prof(fil_u, sac); if ( NULL == pro_u ) { fflush(fil_u); u3z(sac); return u3_nul; } else { - quac** all_u = c3_malloc(sizeof(*all_u)*9); + u3m_quac** all_u = c3_malloc(sizeof(*all_u)*9); all_u[0] = pro_u; - quac** var_u = u3m_mark(); + u3m_quac** var_u = u3m_mark(); all_u[1] = var_u[0]; all_u[2] = var_u[1]; all_u[3] = var_u[2]; all_u[4] = var_u[3]; c3_free(var_u); - + c3_w tot_w = all_u[0]->siz_w + all_u[1]->siz_w + all_u[2]->siz_w + all_u[3]->siz_w + all_u[4]->siz_w; - + all_u[5] = c3_calloc(sizeof(*all_u[5])); all_u[5]->nam_c = strdup("space profile"); all_u[5]->siz_w = u3a_mark_noun(sac)*4; @@ -195,7 +195,7 @@ _serf_grab(u3_noun sac, c3_o pri_o) all_u[8] = c3_calloc(sizeof(*all_u[8])); all_u[8]->nam_c = strdup("sweep"); all_u[8]->siz_w = u3a_sweep()*4; - + if ( c3y == pri_o ) { _serf_print_quacs(fil_u, 9, all_u); } @@ -251,14 +251,14 @@ u3_serf_grab(c3_o pri_o) u3z(gon); } - + fprintf(stderr, "serf: measuring memory:\r\n"); if ( u3_nul != sac ) { res = _serf_grab(sac, pri_o); } else { fprintf(stderr, "sac is empty\r\n"); - quac** var_u = u3m_mark(); + u3m_quac** var_u = u3m_mark(); c3_w tot_w; tot_w = var_u[0]->siz_w + var_u[1]->siz_w @@ -274,9 +274,9 @@ u3_serf_grab(c3_o pri_o) u3a_print_memory(stderr, "sweep", u3a_sweep()); fprintf(stderr, "\r\n"); } - + fflush(stderr); - + return res; } @@ -1120,7 +1120,7 @@ u3_serf_writ(u3_serf* sef_u, u3_noun wit, u3_noun* pel) u3z(wit); u3_noun res = u3_serf_grab(c3n); if ( u3_none == res ) { - ret_o = c3n; + ret_o = c3n; } else { *pel = u3nt(c3__quiz, c3__quac, res); ret_o = c3y; diff --git a/pkg/vere/serf.h b/pkg/vere/serf.h index 973538f4f1..0645434015 100644 --- a/pkg/vere/serf.h +++ b/pkg/vere/serf.h @@ -59,10 +59,5 @@ u3_noun u3_serf_grab(c3_o pri_o); - /* u3_quac_free(): free quac memory. - */ - void - u3a_quac_free(quac* qua_u); - #endif /* ifndef U3_VERE_SERF_H */ From 89b7f30aa306fc049465e190412ce2be58f80102 Mon Sep 17 00:00:00 2001 From: midden-fabler Date: Fri, 12 Jul 2024 14:08:48 -0400 Subject: [PATCH 230/430] http: cleanup wip --- pkg/vere/io/http.c | 218 ++++++++++++++++++++------------------------- 1 file changed, 99 insertions(+), 119 deletions(-) diff --git a/pkg/vere/io/http.c b/pkg/vere/io/http.c index c0e7db1ae8..4ca232bd2e 100644 --- a/pkg/vere/io/http.c +++ b/pkg/vere/io/http.c @@ -645,26 +645,28 @@ _http_cache_respond(u3_hreq* req_u, u3_noun nun); static void _http_scry_respond(u3_hreq* req_u, u3_noun nun); +/* _content_headers: create content headers for response +*/ static u3_noun -_content_headers(c3_z start_z, c3_z end_z, c3_w total_w) +_content_headers(c3_z beg_z, c3_z end_z, c3_w tot_w) { u3_noun out; - u3_noun con_rng; - u3_noun con_len; + u3_noun rng; + u3_noun len; u3_noun lin = u3i_list(u3i_string("bytes "), - u3do("crip", u3do("a-co:co", start_z)), + u3do("crip", u3do("a-co:co", beg_z)), c3_s1('-'), u3do("crip", u3do("a-co:co", end_z)), c3_s1('/'), - u3do("crip", u3do("a-co:co", total_w)), + u3do("crip", u3do("a-co:co", tot_w)), u3_none); u3_atom dat = u3qc_rap(3, lin); - con_rng = u3nc(u3i_string("Content-Range"), dat); - con_len = u3nc(u3i_string("Content-Length"), - u3do("crip", u3do("a-co:co", (end_z - start_z) + 1))); + rng = u3nc(u3i_string("Content-Range"), dat); + len = u3nc(u3i_string("Content-Length"), + u3do("crip", u3do("a-co:co", (end_z - beg_z) + 1))); - out = u3i_list(con_rng, con_len, u3_none); + out = u3i_list(rng, len, u3_none); return out; } @@ -681,9 +683,9 @@ _http_foo_cb(void* vod_p, u3_noun nun) u3_assert(u3_rsat_peek == req_u->sat_e); req_u->peq_u = 0; if ( u3_nul != nun ) { - u3_atom lent = u3r_at(254, nun); - u3_noun cont_heds = _content_headers(0, (lent - 1), lent); - u3_noun mun = u3i_edit(u3k(nun), 125, u3qb_weld(cont_heds, u3r_at(125, nun))); + u3_atom len = u3r_at(254, nun); + u3_noun hez = _content_headers(0, (len - 1), len); + u3_noun mun = u3i_edit(u3k(nun), 125, u3qb_weld(hez, u3r_at(125, nun))); _http_scry_respond(req_u, u3k(mun)); } else { @@ -698,52 +700,13 @@ _http_foo_cb(void* vod_p, u3_noun nun) c3_free(peq_u); } -static c3_c* -_find_tis_fas(void* txt, c3_w len) -{ - c3_c* tis = memchr(txt, '=', len); - c3_c* fas = memchr(txt, '/', len); - - if ( tis && fas ) { - return c3_min(tis, fas); - } - else if ( tis ) { - return tis; - } - else { - return fas; - } -} - -// TODO -// [x] don't blow up on bad paths -// [x] authentication -// [x] caching -// [x] insert mime in path -// [x] range header -// [x] u3qc_cut -// [x] better range header parsing -// [x] better slicing -// [x] 206 -// [x] 200 -// [x] open range vs error -// [x] test stream -// [x] content range function -// [x] video controls -// [x] fix repeated 1 byte requests -// [x] disappearing headers -// [x] fix 200 -// [x] content-length -// [ ] mite -// [ ] don't crash, check multipart ranges -// typedef struct _range_header { - c3_z start_z; + c3_z beg_z; c3_z end_z; } range_header; typedef struct _content { - c3_z start_z; + c3_z beg_z; c3_z end_z; u3_noun dat; } content; @@ -751,54 +714,55 @@ typedef struct _content { static content _slice_mime(range_header rng, u3_noun octs) { - c3_w lent_w = u3h(octs); + c3_w len_w = u3h(octs); c3_w oct_w = u3t(octs); content out; - out.start_z = SIZE_MAX; + out.beg_z = SIZE_MAX; out.end_z = SIZE_MAX; out.dat = u3_nul; - if ( rng.start_z == SIZE_MAX ) { + if ( rng.beg_z == SIZE_MAX ) { if ( rng.end_z == SIZE_MAX ) { // [~ ~] return out; } else { // [~ @] - if ( rng.end_z > lent_w ) { + if ( rng.end_z > len_w ) { return out; } else { // slice last bytes - out.start_z = lent_w - rng.end_z; - out.end_z = lent_w - 1; + out.beg_z = len_w - rng.end_z; + out.end_z = len_w - 1; } } } else if ( rng.end_z == SIZE_MAX ) { // [@ ~] - if ( rng.start_z > lent_w ) { + if ( rng.beg_z > len_w ) { return out; } else { - out.start_z = rng.start_z; - out.end_z = lent_w - 1; + out.beg_z = rng.beg_z; + out.end_z = len_w - 1; } } - else if (rng.end_z > lent_w) { + else if (rng.end_z > len_w) { return out; } else { // [@ @] - out.start_z = rng.start_z; + out.beg_z = rng.beg_z; out.end_z = rng.end_z; } - if ( (out.start_z < lent_w) && - (out.end_z < lent_w) && - (out.start_z <= out.end_z) ) { - out.dat = u3nc((out.end_z - out.start_z) + 1, - u3qc_cut(3, out.start_z, (out.end_z + 1) - out.start_z, oct_w)); + if ( (out.beg_z < len_w) + && (out.end_z < len_w) + && (out.beg_z <= out.end_z) ) + { + out.dat = u3nc((out.end_z - out.beg_z) + 1, + u3qc_cut(3, out.beg_z, (out.end_z + 1) - out.beg_z, oct_w)); } return out; } @@ -808,16 +772,16 @@ _get_range(c3_c* txt_c, c3_w len_w) { c3_c* hep_c = memchr(txt_c, '-', len_w); range_header slice; - slice.start_z = SIZE_MAX; + slice.beg_z = SIZE_MAX; slice.end_z = SIZE_MAX; if ( hep_c ) { - slice.start_z = h2o_strtosize(txt_c, hep_c - txt_c); + slice.beg_z = h2o_strtosize(txt_c, hep_c - txt_c); slice.end_z = h2o_strtosize(hep_c + 1, len_w - ((hep_c + 1) - txt_c)); // strange -> [SIZE_MAX SIZE_MAX] so we return u3_nul in _slice_mime - if ( ((hep_c != txt_c) && (slice.start_z == SIZE_MAX)) || + if ( ((hep_c != txt_c) && (slice.beg_z == SIZE_MAX)) || ((len_w - ((hep_c + 1) - txt_c) > 0) && (slice.end_z == SIZE_MAX)) ) { - slice.start_z = SIZE_MAX; + slice.beg_z = SIZE_MAX; slice.end_z = SIZE_MAX; } } @@ -838,8 +802,8 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) u3_noun wir = _http_req_to_duct(req_u); u3_noun cad; - c3_c* base = req_u->rec_u->input.path.base; - c3_w len = req_u->rec_u->input.path.len; + c3_c* bas_c = req_u->rec_u->input.path.base; + c3_w len_w = req_u->rec_u->input.path.len; { u3_noun adr = u3nc(c3__ipv4, u3i_words(1, &req_u->hon_u->ipf_w)); @@ -852,9 +816,14 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) : u3nc(u3i_string("request"), dat); } - if ( (len >= 5) && (base[1] == '_') && (base[2] == '~') && (base[3] == '_') && (base[4] == '/')) { - base = base + 4; // retain '/' after /_~_ - len = len - 4; + if ( (len_w >= 5) + && (bas_c[1] == '_') + && (bas_c[2] == '~') + && (bas_c[3] == '_') + && (bas_c[4] == '/') ) + { + bas_c = bas_c + 4; // retain '/' after /_~_ + len_w = len_w - 4; req_u->peq_u = c3_malloc(sizeof(*req_u->peq_u)); req_u->peq_u->req_u = req_u; @@ -881,65 +850,78 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) u3_noun cas; c3_o last = c3n; - c3_w i; - // get beak from path // - for (i = 0; i < 3; ++i) { - u3_noun* where; - if ( i == 0 ) { - where = &who; + for ( c3_w i_w = 0; i_w < 3; ++i_w ) { + u3_noun* wer; + if ( 0 == i_w ) { + wer = &who; } - else if ( i == 1 ) { - where = &des; + else if ( 1 == i_w ) { + wer = &des; } else { - where = &cas; + wer = &cas; } // find '//' - if ( len >= 2 && base[0] == '/' && base[1] == '/' ) { - *where = u3_nul; - base++; - len--; + if ( (len_w >= 2) + && ('/' == bas_c[0]) + && ('/' == bas_c[1]) ) + { + *wer = u3_nul; + bas_c++; + len_w--; } // skip '/' - else if ( len > 0 && base[0] == '/' ) { - base++; - len--; + else if ( (len_w > 0) && ('/' == bas_c[0]) ) { + bas_c++; + len_w--; } // '=' - if ( len > 0 && base[0] == '=' ) { - if ( i == 0 ) { - *where = our; + if ( (len_w > 0) && ('=' == bas_c[0]) ) { + if ( 0 == i_w ) { + *wer = our; } - else if ( i == 1 ) { - *where = u3i_string("base"); + else if ( 1 == i_w ) { + *wer = u3i_string("base"); } else { last = c3y; } - base++; - len--; + bas_c++; + len_w--; } // slice cord else { - c3_c* nex = _find_tis_fas(base, len); - if ( !nex ) { + c3_c* nex_c; + c3_c* tis_c = memchr(bas_c, '=', len_w); + c3_c* fas_c = memchr(bas_c, '/', len_w); + if ( tis_c && fas_c ) { + nex_c = c3_min(tis_c, fas_c); + } + else if ( tis_c ) { + nex_c = tis_c; + } + else { + nex_c = fas_c; + } + + if ( !nex_c ) { c3_c* msg_c = "bad beam"; h2o_send_error_generic(req_u->rec_u, 400, msg_c, msg_c, 0); return; } else { - c3_d mylen = nex - base; - *where = u3i_bytes(mylen, base); - base = nex; - len = len - mylen; + c3_d len_d = nex_c - bas_c; + *wer = u3i_bytes(len_d, (const c3_y*)bas_c); + bas_c = nex_c; + len_w = len_w - len_d; } } } - u3_noun spur = u3dc("rush", u3i_bytes(len, (const c3_y*)base), u3v_wish("stap")); + u3_noun spur = u3dc("rush", u3i_bytes(len_w, (const c3_y*)bas_c), u3v_wish("stap")); if ( (who != our) || (spur == u3_nul) ) { c3_c* msg_c = "bad scry path"; @@ -972,23 +954,23 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) if (idx != UINT32_MAX) { if ( (req_headers.entries[idx].value.len >= 6) && - (memcmp("bytes=", req_headers.entries[idx].value.base, 6) == 0 )) { + (0 == memcmp("bytes=", req_headers.entries[idx].value.base, 6)) ) { c3_w rest_len = req_headers.entries[idx].value.len - 6; - if ( rest_len == 0) { + if ( 0 == rest_len ) { c3_c* msg_c = "Requested Range Not Satisfiable"; h2o_send_error_generic(req_u->rec_u, 416, msg_c, msg_c, 0); return; } range_header rng_hed = _get_range(req_headers.entries[idx].value.base + 6, rest_len); u3_noun octs = u3r_at(127, nac); - if ( octs == u3_none) { + if ( u3_none == octs ) { c3_c* msg_c = "Requested Range Not Satisfiable"; h2o_send_error_generic(req_u->rec_u, 416, msg_c, msg_c, 0); return; } content result = _slice_mime(rng_hed, octs); - if ( result.dat == u3_nul) { + if ( u3_nul == result.dat ) { c3_c* msg_c = "Requested Range Not Satisfiable"; h2o_send_error_generic(req_u->rec_u, 416, msg_c, msg_c, 0); return; @@ -996,17 +978,17 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) if ( u3r_sing(result.dat, octs) == c3y) { // 200 - u3_atom lent = u3r_at(254, nac); - u3_noun cont_heds = _content_headers(0, (lent - 1), lent); - u3_noun mac = u3i_edit(nac, 125, u3qb_weld(cont_heds, u3r_at(125, nac))); + u3_atom len = u3r_at(254, nac); + u3_noun hez = _content_headers(0, (len - 1), len); + u3_noun mac = u3i_edit(nac, 125, u3qb_weld(hez, u3r_at(125, nac))); _http_cache_respond(req_u, mac); } else { // 206 - u3_noun cont_heds = _content_headers(result.start_z, result.end_z, u3h(octs)); + u3_noun hez = _content_headers(result.beg_z, result.end_z, u3h(octs)); u3_noun res = u3i_edit(nac, 127, result.dat); res = u3i_edit(res, 124, 206); - res = u3i_edit(res, 125, u3qb_weld(cont_heds, u3r_at(125, res))); + res = u3i_edit(res, 125, u3qb_weld(hez, u3r_at(125, res))); _http_cache_respond(req_u, res); } } @@ -1050,9 +1032,7 @@ _http_scry_respond(u3_hreq* req_u, u3_noun nun) { } else { u3_noun auth, response_header, data; - // XX check: looks good u3x_qual(u3k(u3t(u3t(nun))), &auth, 0, &response_header, &data); - // u3m_p("res-header", response_header); u3_noun status, headers; u3x_cell(response_header, &status, &headers); From 627fc6c43a5d66befd60d29c9f8c997b55f18af4 Mon Sep 17 00:00:00 2001 From: lukechampine Date: Mon, 15 Jul 2024 00:49:56 -0400 Subject: [PATCH 231/430] mesa: handle inline proofs in _mesa_send_jumbo_pieces --- pkg/vere/io/mesa.c | 58 +++++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 8a024502c5..1777a8b762 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1725,12 +1725,12 @@ _mesa_send_jumbo_pieces(u3_mesa* sam_u, u3_noun pag) lss_hash* proof = lss_builder_finalize(&bil_u); c3_w proof_len = lss_proof_size(leaves_w); - u3_mesa_name nam_u = tac_u.pag_u.nam_u; - u3_mesa_data dat_u = tac_u.pag_u.dat_u; - nam_u.boq_y = 13; - dat_u.tot_w = leaves_w; + u3_mesa_name* nam_u = &tac_u.pag_u.nam_u; + u3_mesa_data* dat_u = &tac_u.pag_u.dat_u; + nam_u->boq_y = 13; + dat_u->tot_w = leaves_w; - if ( c3y == nam_u.nit_o && leaves_w > 4) { + if ( c3y == nam_u->nit_o && leaves_w > 4) { #ifdef MESA_DEBUG u3l_log(" sending proof packet"); #endif @@ -1738,42 +1738,46 @@ _mesa_send_jumbo_pieces(u3_mesa* sam_u, u3_noun pag) // send proof packet tac_u.pag_u.dat_u.len_w = proof_len*sizeof(lss_hash); memcpy(tac_u.pag_u.dat_u.fra_y, proof, tac_u.pag_u.dat_u.len_w); - u3_weak pin = _mesa_get_pit(sam_u, &nam_u); + u3_weak pin = _mesa_get_pit(sam_u, nam_u); if ( u3_none != pin ) { _mesa_send_pact(sam_u, u3k(u3t(pin)), NULL, &tac_u); - _mesa_del_pit(sam_u, &nam_u); + _mesa_del_pit(sam_u, nam_u); u3z(pin); } } - #ifdef MESA_DEBUG - u3l_log(" sending up to %u leaf packets", leaves_w); - #endif // send leaf packets - tac_u.pag_u.nam_u.nit_o = c3n; - for (c3_w i = 0; i < leaves_w; i++) { - nam_u.fra_w = i; - dat_u.fra_y = jumbo_y + (i*1024); - dat_u.len_w = c3_min(jumbo_w - (i*1024), 1024); - - lss_pair* pair = lss_builder_pair(&bil_u, i); - if ( NULL == pair ) { - dat_u.aup_u.len_y = 0; - dat_u.aum_u.typ_e = AUTH_NONE; + tac_u.pag_u.nam_u.nit_o = __(i == 0); + nam_u->fra_w = i; + dat_u->fra_y = jumbo_y + (i*1024); + dat_u->len_w = c3_min(jumbo_w - (i*1024), 1024); + + if ( (c3y == tac_u.pag_u.nam_u.nit_o) && (leaves_w <= 4) ) { + // inline proof; omit first leaf + proof_len--; + proof++; + dat_u->aup_u.len_y = proof_len; + memcpy(dat_u->aup_u.has_y, proof, proof_len*sizeof(lss_hash)); } else { - dat_u.aup_u.len_y = 2; - dat_u.aum_u.typ_e = AUTH_NEXT; - memcpy(dat_u.aup_u.has_y[0], (*pair)[0], sizeof(lss_hash)); - memcpy(dat_u.aup_u.has_y[1], (*pair)[1], sizeof(lss_hash)); + lss_pair* pair = lss_builder_pair(&bil_u, i); + if ( NULL == pair ) { + dat_u->aup_u.len_y = 0; + dat_u->aum_u.typ_e = AUTH_NONE; + } else { + dat_u->aup_u.len_y = 2; + dat_u->aum_u.typ_e = AUTH_NEXT; + memcpy(dat_u->aup_u.has_y[0], (*pair)[0], sizeof(lss_hash)); + memcpy(dat_u->aup_u.has_y[1], (*pair)[1], sizeof(lss_hash)); + } } - u3_weak pin = _mesa_get_pit(sam_u, &nam_u); + u3_weak pin = _mesa_get_pit(sam_u, nam_u); if ( u3_none != pin) { #ifdef MESA_DEBUG - u3l_log(" sending leaf packet, fra_w: %u", nam_u.fra_w); + u3l_log(" sending leaf packet, fra_w: %u", nam_u->fra_w); #endif _mesa_send_pact(sam_u, u3k(u3t(pin)), NULL, &tac_u); - _mesa_del_pit(sam_u, &nam_u); + _mesa_del_pit(sam_u, nam_u); u3k(pin); } } From dd547c7780430ffe544d3a840dc9eaec36f33bc7 Mon Sep 17 00:00:00 2001 From: lukechampine Date: Mon, 15 Jul 2024 00:52:20 -0400 Subject: [PATCH 232/430] mesa: register our interest in _mesa_request_next_fragments --- pkg/vere/io/mesa.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 1777a8b762..1cd5b859a1 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1980,19 +1980,13 @@ _mesa_request_next_fragments(u3_mesa* sam_u, u3_mesa_pict* nex_u = req_u->pic_u; c3_w nex_w = req_u->nex_w; for ( int i = 0; i < win_w; i++ ) { - c3_y* buf_y = c3_calloc(PACT_SIZE); c3_w fra_w = nex_w + i; if ( fra_w >= req_u->tot_w ) { break; } nex_u->pac_u.pek_u.nam_u.fra_w = nex_w + i; - c3_w siz_w = mesa_etch_pact(buf_y, &nex_u->pac_u); - if ( siz_w == 0 ) { - u3l_log("failed to etch"); - u3_assert( 0 ); - } - // TODO: better route management - _mesa_send_buf(sam_u, lan_u, buf_y, siz_w); + _mesa_add_our_to_pit(sam_u, &nex_u->pac_u.pek_u.nam_u); + _mesa_send(nex_u, &lan_u); _mesa_req_pact_sent(req_u, &nex_u->pac_u.pek_u.nam_u); } } From fcec150d37fd7e36cd143032de56b5d4896698e2 Mon Sep 17 00:00:00 2001 From: lukechampine Date: Mon, 15 Jul 2024 00:56:22 -0400 Subject: [PATCH 233/430] mesa: fix jumbo scrying, with a workaround --- pkg/vere/io/mesa.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 1cd5b859a1..0a84ea1ce4 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -2239,20 +2239,22 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) _mesa_forward_request(sam_u, pic_u); return; } + // record interest + _mesa_add_lane_to_pit(sam_u, &pac_u->pek_u.nam_u, lan_u); + c3_w fra_w = pac_u->pek_u.nam_u.fra_w; c3_w bat_w = _mesa_lop(fra_w); pac_u->pek_u.nam_u.fra_w = bat_w; - - // record interest - u3l_log(" adding lane to pit\r\n"); - _mesa_add_lane_to_pit(sam_u, &pac_u->pek_u.nam_u, lan_u); + // XX HACK: shouldn't be necessary to change data 0 to init, but + // for some reason it's changing the data returned by the scry + if ( pac_u->pek_u.nam_u.fra_w == 0 ) { + pac_u->pek_u.nam_u.nit_o = c3y; + } - u3l_log(" added\r\n"); // if we have the page, send it u3_weak hit = _mesa_get_jumbo_cache(sam_u, &pac_u->pek_u.nam_u); if ( u3_none != hit ) { - u3l_log(" hit\r\n"); u3_noun tag, dat; u3x_cell(hit, &tag, &dat); if ( MESA_ITEM == tag ) { @@ -2263,17 +2265,13 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) return; } // otherwise, scry - u3l_log(" got hit\r\n"); _mesa_put_jumbo_cache(sam_u, &pac_u->pek_u.nam_u, u3nc(MESA_WAIT, 0)); u3_noun sky = _name_to_jumbo_scry(&pac_u->pek_u.nam_u); - pac_u->pek_u.nam_u.fra_w = fra_w; u3_noun our = u3i_chubs(2, sam_u->car_u.pir_u->who_d); u3_noun bem = u3nc(u3nt(our, u3_nul, u3nc(c3__ud, 1)), sky); // NOTE: pic_u not freed - u3l_log(" about to peek\r\n"); u3_pier_peek(sam_u->car_u.pir_u, u3_nul, u3k(u3nq(1, c3__beam, c3__ax, bem)), pic_u, _mesa_page_scry_jumbo_cb); u3z(hit); - u3l_log(" ran\r\n"); } static void From 70d58423964b46a8d5a689dc2ce9fa240a2b7862 Mon Sep 17 00:00:00 2001 From: lukechampine Date: Mon, 15 Jul 2024 00:57:35 -0400 Subject: [PATCH 234/430] mesa: remove erroneous PIT addition in _mesa_hear_poke --- pkg/vere/io/mesa.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 0a84ea1ce4..a5887c3355 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -2338,9 +2338,6 @@ _mesa_hear_poke(u3_mesa_pict* pic_u, u3_lane* lan_u) } _mesa_put_peer(sam_u, pac_u->pok_u.pay_u.her_d, per_u); - // XX could check cache for ack (completed duplicate) - _mesa_add_lane_to_pit(sam_u, &pac_u->pek_u.nam_u, *lan_u); - u3_ovum_peer nes_f; u3_ovum_bail bal_f; void* ptr_v; From 1dc91ef8efd7d4b39c792c1510b8872e1e5e40dc Mon Sep 17 00:00:00 2001 From: lukechampine Date: Mon, 15 Jul 2024 00:59:53 -0400 Subject: [PATCH 235/430] mesa: preserve sig/hmac when transceiving --- pkg/vere/io/mesa.c | 6 +++++- pkg/vere/io/mesa/mesa.h | 16 +++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index a5887c3355..91351e7f7d 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -175,6 +175,7 @@ typedef struct _u3_pend_req { u3_peer* per_u; // backpointer c3_w nex_w; // number of the next fragment to be sent c3_w tot_w; // total number of fragments expected + u3_auth_data aum_u; // message authenticator uv_timer_t tim_u; // timehandler c3_y* dat_y; // ((mop @ud *) lte) c3_w len_w; @@ -1904,6 +1905,7 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) memcpy(&req_u->pic_u->pac_u.pek_u.nam_u, nam_u, sizeof(u3_mesa_name)); req_u->pic_u->pac_u.pek_u.nam_u.aut_o = c3n; req_u->pic_u->pac_u.pek_u.nam_u.nit_o = c3n; + req_u->aum_u = pac_u->pag_u.dat_u.aum_u; c3_w siz_w = 1 << (pac_u->pag_u.nam_u.boq_y - 3); u3_assert( siz_w == 1024 ); // boq_y == 13 @@ -2134,10 +2136,12 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) // construct jumbo frame c3_w jumbo_len_w = (1024 * (req_u->tot_w - 1)) + pac_u->pag_u.dat_u.len_w; pac_u->pag_u.nam_u.boq_y = 31; // TODO: use actual jumbo bloq + pac_u->pag_u.dat_u.tot_w = 1; + pac_u->pag_u.nam_u.fra_w = 0; pac_u->pag_u.dat_u.len_w = jumbo_len_w; pac_u->pag_u.dat_u.fra_y = c3_realloc(pac_u->pag_u.dat_u.fra_y, jumbo_len_w); memcpy(pac_u->pag_u.dat_u.fra_y, req_u->dat_y, jumbo_len_w); - pic_u->pac_u.pag_u.nam_u.fra_w = 0; + pac_u->pag_u.dat_u.aum_u = req_u->aum_u; u3_noun lan = u3_mesa_encode_lane(lan_u); diff --git a/pkg/vere/io/mesa/mesa.h b/pkg/vere/io/mesa/mesa.h index 7dd60b5ad6..252234136e 100644 --- a/pkg/vere/io/mesa/mesa.h +++ b/pkg/vere/io/mesa/mesa.h @@ -72,16 +72,18 @@ typedef enum { AUTH_HMAC = 3 } u3_mesa_auth_type; +typedef struct _u3_auth_data { + u3_mesa_auth_type typ_e; // none, traversal (none), sig, or hmac + union { // + c3_y sig_y[64]; // signature + c3_y mac_y[32]; // hmac + }; +} u3_auth_data; + typedef struct _u3_mesa_data { // u3_mesa_data_meta met_u; c3_w tot_w; // total fragments - struct { - u3_mesa_auth_type typ_e; // none, traversal (none), sig, or hmac - union { // - c3_y sig_y[64]; // signature - c3_y mac_y[32]; // hmac - }; - } aum_u; + u3_auth_data aum_u; struct { c3_y len_y; // number of hashes (0, 1, or 2) c3_y has_y[2][32]; // hashes From 75d2e495ccfc5ce87cb0dddd87984f0b2a87f4d7 Mon Sep 17 00:00:00 2001 From: lukechampine Date: Mon, 15 Jul 2024 09:41:10 -0400 Subject: [PATCH 236/430] mesa: use correct buffer in _mesa_send_jumbo_pieces --- pkg/vere/io/mesa.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 91351e7f7d..21736a38dd 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1711,26 +1711,26 @@ _mesa_send_jumbo_pieces(u3_mesa* sam_u, u3_noun pag) log_pact(&tac_u); #endif + u3_mesa_name* nam_u = &tac_u.pag_u.nam_u; + u3_mesa_data* dat_u = &tac_u.pag_u.dat_u; + nam_u->boq_y = 13; + dat_u->tot_w = (jumbo_w + 1023) / 1024; + // compute LSS data // // TODO: this assumes we have the entire message. Should be switched to use // lss_builder_transceive instead. - c3_w leaves_w = (jumbo_w + 1023) / 1024; + c3_w leaves_w = dat_u->tot_w; lss_builder bil_u; lss_builder_init(&bil_u, leaves_w); for ( c3_w i = 0; i < leaves_w; i++ ) { - c3_y* leaf_y = jumbo_y + (i*1024); + c3_y* leaf_y = tac_u.pag_u.dat_u.fra_y + (i*1024); c3_w leaf_w = (i < leaves_w - 1) ? 1024 : jumbo_w % 1024; lss_builder_ingest(&bil_u, leaf_y, leaf_w); } lss_hash* proof = lss_builder_finalize(&bil_u); c3_w proof_len = lss_proof_size(leaves_w); - u3_mesa_name* nam_u = &tac_u.pag_u.nam_u; - u3_mesa_data* dat_u = &tac_u.pag_u.dat_u; - nam_u->boq_y = 13; - dat_u->tot_w = leaves_w; - if ( c3y == nam_u->nit_o && leaves_w > 4) { #ifdef MESA_DEBUG u3l_log(" sending proof packet"); @@ -1751,7 +1751,7 @@ _mesa_send_jumbo_pieces(u3_mesa* sam_u, u3_noun pag) for (c3_w i = 0; i < leaves_w; i++) { tac_u.pag_u.nam_u.nit_o = __(i == 0); nam_u->fra_w = i; - dat_u->fra_y = jumbo_y + (i*1024); + dat_u->fra_y = tac_u.pag_u.dat_u.fra_y + (i*1024); dat_u->len_w = c3_min(jumbo_w - (i*1024), 1024); if ( (c3y == tac_u.pag_u.nam_u.nit_o) && (leaves_w <= 4) ) { From 2cfd7a02d408148115f915dd1725e9cab96e669a Mon Sep 17 00:00:00 2001 From: lukechampine Date: Mon, 15 Jul 2024 09:58:09 -0400 Subject: [PATCH 237/430] mesa: don't set %init for messages w/out inlined proofs --- pkg/vere/io/mesa.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 21736a38dd..2e1fff494b 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1749,12 +1749,12 @@ _mesa_send_jumbo_pieces(u3_mesa* sam_u, u3_noun pag) // send leaf packets for (c3_w i = 0; i < leaves_w; i++) { - tac_u.pag_u.nam_u.nit_o = __(i == 0); + nam_u->nit_o = __((i == 0) && (leaves_w <= 4)); nam_u->fra_w = i; dat_u->fra_y = tac_u.pag_u.dat_u.fra_y + (i*1024); dat_u->len_w = c3_min(jumbo_w - (i*1024), 1024); - if ( (c3y == tac_u.pag_u.nam_u.nit_o) && (leaves_w <= 4) ) { + if ( c3y == nam_u->nit_o ) { // inline proof; omit first leaf proof_len--; proof++; From df9139efec7ee48f41d8591711e8ae9a07edb82c Mon Sep 17 00:00:00 2001 From: lukechampine Date: Mon, 15 Jul 2024 10:08:08 -0400 Subject: [PATCH 238/430] mesa: use correct buffer length in _mesa_send_jumbo_pieces --- pkg/vere/io/mesa.c | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 2e1fff494b..895c1f11d0 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1701,44 +1701,46 @@ _mesa_send_jumbo_pieces(u3_mesa* sam_u, u3_noun pag) #endif // parse packet u3_mesa_pact tac_u = {0}; - c3_w jumbo_w = u3r_met(3, pag); - c3_y* jumbo_y = c3_calloc(jumbo_w); - u3r_bytes(0, jumbo_w, jumbo_y, pag); - u3z(pag); - mesa_sift_pact(&tac_u, jumbo_y, jumbo_w); - - #ifdef MESA_DEBUG - log_pact(&tac_u); - #endif - - u3_mesa_name* nam_u = &tac_u.pag_u.nam_u; - u3_mesa_data* dat_u = &tac_u.pag_u.dat_u; - nam_u->boq_y = 13; - dat_u->tot_w = (jumbo_w + 1023) / 1024; + { + c3_w jumbo_w = u3r_met(3, pag); + c3_y* jumbo_y = c3_calloc(jumbo_w); + u3r_bytes(0, jumbo_w, jumbo_y, pag); + u3z(pag); + mesa_sift_pact(&tac_u, jumbo_y, jumbo_w); + } + c3_w jumbo_pact_w = tac_u.pag_u.dat_u.len_w; + c3_y* jumbo_pact_y = tac_u.pag_u.dat_u.fra_y; // compute LSS data // // TODO: this assumes we have the entire message. Should be switched to use // lss_builder_transceive instead. - c3_w leaves_w = dat_u->tot_w; + c3_w leaves_w = (jumbo_pact_w + 1023) / 1024; lss_builder bil_u; lss_builder_init(&bil_u, leaves_w); for ( c3_w i = 0; i < leaves_w; i++ ) { - c3_y* leaf_y = tac_u.pag_u.dat_u.fra_y + (i*1024); - c3_w leaf_w = (i < leaves_w - 1) ? 1024 : jumbo_w % 1024; + c3_y* leaf_y = jumbo_pact_y + (i*1024); + c3_w leaf_w = (i < leaves_w - 1) ? 1024 : jumbo_pact_w % 1024; lss_builder_ingest(&bil_u, leaf_y, leaf_w); } lss_hash* proof = lss_builder_finalize(&bil_u); c3_w proof_len = lss_proof_size(leaves_w); + + // send packets + u3_mesa_name* nam_u = &tac_u.pag_u.nam_u; + u3_mesa_data* dat_u = &tac_u.pag_u.dat_u; + nam_u->boq_y = 13; + dat_u->tot_w = leaves_w; + if ( c3y == nam_u->nit_o && leaves_w > 4) { #ifdef MESA_DEBUG u3l_log(" sending proof packet"); #endif // send proof packet - tac_u.pag_u.dat_u.len_w = proof_len*sizeof(lss_hash); - memcpy(tac_u.pag_u.dat_u.fra_y, proof, tac_u.pag_u.dat_u.len_w); + dat_u->len_w = proof_len*sizeof(lss_hash); + memcpy(dat_u->fra_y, proof, dat_u->len_w); u3_weak pin = _mesa_get_pit(sam_u, nam_u); if ( u3_none != pin ) { _mesa_send_pact(sam_u, u3k(u3t(pin)), NULL, &tac_u); @@ -1751,8 +1753,8 @@ _mesa_send_jumbo_pieces(u3_mesa* sam_u, u3_noun pag) for (c3_w i = 0; i < leaves_w; i++) { nam_u->nit_o = __((i == 0) && (leaves_w <= 4)); nam_u->fra_w = i; - dat_u->fra_y = tac_u.pag_u.dat_u.fra_y + (i*1024); - dat_u->len_w = c3_min(jumbo_w - (i*1024), 1024); + dat_u->fra_y = jumbo_pact_y + (i*1024); + dat_u->len_w = c3_min(jumbo_pact_w - (i*1024), 1024); if ( c3y == nam_u->nit_o ) { // inline proof; omit first leaf From 448715a16ee52d5637cdb7756a7a312cd02365d1 Mon Sep 17 00:00:00 2001 From: lukechampine Date: Mon, 15 Jul 2024 11:12:32 -0400 Subject: [PATCH 239/430] mesa: don't clobber jumbo payload with proof data --- pkg/vere/io/mesa.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 895c1f11d0..752aa43d7d 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1734,15 +1734,15 @@ _mesa_send_jumbo_pieces(u3_mesa* sam_u, u3_noun pag) dat_u->tot_w = leaves_w; if ( c3y == nam_u->nit_o && leaves_w > 4) { - #ifdef MESA_DEBUG - u3l_log(" sending proof packet"); - #endif - - // send proof packet - dat_u->len_w = proof_len*sizeof(lss_hash); - memcpy(dat_u->fra_y, proof, dat_u->len_w); u3_weak pin = _mesa_get_pit(sam_u, nam_u); if ( u3_none != pin ) { + #ifdef MESA_DEBUG + u3l_log(" sending proof packet"); + #endif + dat_u->len_w = proof_len*sizeof(lss_hash); + c3_y* proof_y = c3_malloc(dat_u->len_w); + memcpy(proof_y, proof, dat_u->len_w); + dat_u->fra_y = proof_y; _mesa_send_pact(sam_u, u3k(u3t(pin)), NULL, &tac_u); _mesa_del_pit(sam_u, nam_u); u3z(pin); From bed1f55f5907b983a2facf52860afce2ac91c993 Mon Sep 17 00:00:00 2001 From: lukechampine Date: Mon, 15 Jul 2024 11:17:26 -0400 Subject: [PATCH 240/430] mesa: eliminate buffer copy in _mesa_req_pact_done --- pkg/vere/io/mesa.c | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 752aa43d7d..0883e0be96 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1135,13 +1135,6 @@ _mesa_req_pact_done(u3_pend_req* req_u, memcpy((*par_u)[1], dat_u->aup_u.has_y[1], sizeof(lss_hash)); } - // TODO: move to bottom - - c3_y buf_y[1024]; - memcpy(buf_y, dat_u->fra_y, dat_u->len_w); - memset(buf_y + dat_u->len_w, 0, 1024 - dat_u->len_w); - c3_w len_w = (nam_u->fra_w + 1 == dat_u->tot_w) ? dat_u->len_w : 1024; - if ( req_u->los_u->counter != nam_u->fra_w ) { if ( nam_u->fra_w < req_u->los_u->counter ) { u3l_log("fragment number too low (%u)", nam_u->fra_w); @@ -1150,13 +1143,13 @@ _mesa_req_pact_done(u3_pend_req* req_u, } else { // insert into misordered queue u3_misord_buf* buf_u = &req_u->mis_u[nam_u->fra_w - req_u->los_u->counter - 1]; - buf_u->fra_y = c3_calloc(len_w); - buf_u->len_w = len_w; - memcpy(buf_u->fra_y, buf_y, len_w); + buf_u->fra_y = c3_calloc(dat_u->len_w); + buf_u->len_w = dat_u->len_w; + memcpy(buf_u->fra_y, dat_u->fra_y, dat_u->len_w); buf_u->par_u = par_u; } } - else if ( c3y != lss_verifier_ingest(req_u->los_u, buf_y, len_w, par_u) ) { + else if ( c3y != lss_verifier_ingest(req_u->los_u, dat_u->fra_y, dat_u->len_w, par_u) ) { c3_free(par_u); // TODO: do we drop the whole request on the floor? u3l_log("auth fail frag %u", nam_u->fra_w); From 5b3c230d5376f1366af1ba3096dbfef42e68fc28 Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Mon, 15 Jul 2024 15:06:03 -0400 Subject: [PATCH 241/430] mesa: put more prints behind #ifdef MESA_DEBUG --- pkg/vere/io/mesa.c | 50 +++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 0883e0be96..89d0a92a2c 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -805,7 +805,9 @@ _mesa_req_pact_sent(u3_pend_req* req_u, u3_mesa_name* nam_u) req_u->wat_u[nam_u->fra_w].sip_y = 0; req_u->wat_u[nam_u->fra_w].tie_y = 1; - u3l_log("bitset_put %u", nam_u->fra_w); + #ifdef MESA_DEBUG + u3l_log("bitset_put %u", nam_u->fra_w); + #endif bitset_put(&req_u->was_u, nam_u->fra_w); } else { u3l_log("mesa: no req for sent"); @@ -1003,7 +1005,8 @@ static void _update_resend_timer(u3_pend_req *req_u) { if( req_u->tot_w == 0 || req_u->len_w == req_u->tot_w ) { - u3l_log("bad condition"); + u3l_log("bad condition tot_w: %u len_w: %u", + req_u->tot_w, req_u->len_w); return; } // scan in flight packets, find oldest @@ -1115,14 +1118,17 @@ _mesa_req_pact_done(u3_pend_req* req_u, req_u->ack_w = nam_u->fra_w; } -#ifdef MESA_DEBUG - if ( nam_u->fra_w != 0 && req_u->wat_u[nam_u->fra_w].tie_y != 1 ) { - u3l_log("received retry %u", nam_u->fra_w); - } -#endif + #ifdef MESA_DEBUG + if ( nam_u->fra_w != 0 && req_u->wat_u[nam_u->fra_w].tie_y != 1 ) { + u3l_log("received retry %u", nam_u->fra_w); + } + #endif req_u->len_w++; - u3l_log("fragment %u len %u", nam_u->fra_w, req_u->len_w); + + #ifdef MESA_DEBUG + u3l_log("fragment %u len %u", nam_u->fra_w, req_u->len_w); + #endif if ( req_u->lef_w == nam_u->fra_w ) { req_u->lef_w++; } @@ -1614,14 +1620,16 @@ _mesa_get_jumbo_cache(u3_mesa* sam_u, u3_mesa_name* nam_u) { u3_noun pax = _name_to_jumbo_scry(nam_u); u3_weak res = u3h_get(sam_u->pac_p, pax); - if ( u3_none == res ) { - u3m_p("miss", pax); - } else { - u3_noun kev = u3nc(u3k(pax), u3k(res)); - u3m_p("hit", kev); - u3z(kev); - } - u3z(pax); + #ifdef MESA_DEBUG + if ( u3_none == res ) { + u3m_p("miss", pax); + } else { + u3_noun kev = u3nc(u3k(pax), u3k(res)); + u3m_p("hit", kev); + u3z(kev); + } + u3z(pax); + #endif return res; } @@ -1650,10 +1658,12 @@ _mesa_put_pit(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_noun val) { u3_noun pax = _name_to_scry(nam_u); u3h_put(sam_u->pit_p, pax, u3k(val)); - c3_c* our_c = (c3y == u3h(val))? "&" : "|"; - c3_c* las_c = (u3_nul == u3t(val))? "~" : "..."; - u3l_log("mesa: put_pit(our %s, las %s)", our_c, las_c); - log_name(nam_u); + #ifdef MESA_DEBUG + c3_c* our_c = (c3y == u3h(val))? "&" : "|"; + c3_c* las_c = (u3_nul == u3t(val))? "~" : "..."; + u3l_log("mesa: put_pit(our %s, las %s)", our_c, las_c); + log_name(nam_u); + #endif u3z(pax); u3z(val); } From e0a961a26b4de0458d052fa970af8087109c224b Mon Sep 17 00:00:00 2001 From: midden-fabler Date: Mon, 15 Jul 2024 23:33:28 -0400 Subject: [PATCH 242/430] http: cleanup, headers --- pkg/vere/io/http.c | 75 +++++++++++++++++++++++----------------------- 1 file changed, 38 insertions(+), 37 deletions(-) diff --git a/pkg/vere/io/http.c b/pkg/vere/io/http.c index 4ca232bd2e..c8496e90d0 100644 --- a/pkg/vere/io/http.c +++ b/pkg/vere/io/http.c @@ -651,6 +651,7 @@ static u3_noun _content_headers(c3_z beg_z, c3_z end_z, c3_w tot_w) { u3_noun out; + u3_noun byz; u3_noun rng; u3_noun len; @@ -662,8 +663,8 @@ _content_headers(c3_z beg_z, c3_z end_z, c3_w tot_w) u3do("crip", u3do("a-co:co", tot_w)), u3_none); u3_atom dat = u3qc_rap(3, lin); - rng = u3nc(u3i_string("Content-Range"), dat); - len = u3nc(u3i_string("Content-Length"), + rng = u3nc(u3i_string("content-range"), dat); + len = u3nc(u3i_string("content-length"), u3do("crip", u3do("a-co:co", (end_z - beg_z) + 1))); out = u3i_list(rng, len, u3_none); @@ -685,25 +686,26 @@ _http_foo_cb(void* vod_p, u3_noun nun) if ( u3_nul != nun ) { u3_atom len = u3r_at(254, nun); u3_noun hez = _content_headers(0, (len - 1), len); - u3_noun mun = u3i_edit(u3k(nun), 125, u3qb_weld(hez, u3r_at(125, nun))); - _http_scry_respond(req_u, u3k(mun)); + u3_noun res = u3i_edit(u3k(nun), 125, u3qb_weld(hez, u3r_at(125, nun))); + _http_scry_respond(req_u, u3k(res)); } else { _http_scry_respond(req_u, u3k(nun)); } } - if ( peq_u->las_o == c3n ) { + // cache only if peek was not at now + if ( c3n == peq_u->las_o ) { u3h_put(htd_u->nax_p, peq_u->pax, nun); } u3z(peq_u->pax); c3_free(peq_u); } -typedef struct _range_header { +typedef struct _range_request { c3_z beg_z; c3_z end_z; -} range_header; +} range_request; typedef struct _content { c3_z beg_z; @@ -712,7 +714,7 @@ typedef struct _content { } content; static content -_slice_mime(range_header rng, u3_noun octs) +_slice_mime(range_request rng, u3_noun octs) { c3_w len_w = u3h(octs); c3_w oct_w = u3t(octs); @@ -722,8 +724,8 @@ _slice_mime(range_header rng, u3_noun octs) out.end_z = SIZE_MAX; out.dat = u3_nul; - if ( rng.beg_z == SIZE_MAX ) { - if ( rng.end_z == SIZE_MAX ) { + if ( SIZE_MAX == rng.beg_z ) { + if ( SIZE_MAX == rng.end_z ) { // [~ ~] return out; } @@ -739,7 +741,7 @@ _slice_mime(range_header rng, u3_noun octs) } } } - else if ( rng.end_z == SIZE_MAX ) { + else if ( SIZE_MAX == rng.end_z ) { // [@ ~] if ( rng.beg_z > len_w ) { return out; @@ -767,25 +769,26 @@ _slice_mime(range_header rng, u3_noun octs) return out; } -static range_header -_get_range(c3_c* txt_c, c3_w len_w) +static range_request +_parse_range(c3_c* txt_c, c3_w len_w) { c3_c* hep_c = memchr(txt_c, '-', len_w); - range_header slice; - slice.beg_z = SIZE_MAX; - slice.end_z = SIZE_MAX; + range_request cut; + cut.beg_z = SIZE_MAX; + cut.end_z = SIZE_MAX; if ( hep_c ) { - slice.beg_z = h2o_strtosize(txt_c, hep_c - txt_c); - slice.end_z = h2o_strtosize(hep_c + 1, len_w - ((hep_c + 1) - txt_c)); + cut.beg_z = h2o_strtosize(txt_c, hep_c - txt_c); + cut.end_z = h2o_strtosize(hep_c + 1, len_w - ((hep_c + 1) - txt_c)); // strange -> [SIZE_MAX SIZE_MAX] so we return u3_nul in _slice_mime - if ( ((hep_c != txt_c) && (slice.beg_z == SIZE_MAX)) || - ((len_w - ((hep_c + 1) - txt_c) > 0) && (slice.end_z == SIZE_MAX)) ) { - slice.beg_z = SIZE_MAX; - slice.end_z = SIZE_MAX; + if ( ((SIZE_MAX == cut.beg_z) && (hep_c != txt_c)) + || ((SIZE_MAX == cut.end_z) && (len_w - ((hep_c + 1) - txt_c) > 0)) ) + { + cut.beg_z = SIZE_MAX; + cut.end_z = SIZE_MAX; } } - return slice; + return cut; } /* _http_req_dispatch(): dispatch http request to %eyre @@ -817,10 +820,10 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) } if ( (len_w >= 5) - && (bas_c[1] == '_') - && (bas_c[2] == '~') - && (bas_c[3] == '_') - && (bas_c[4] == '/') ) + && ('_' == bas_c[1]) + && ('~' == bas_c[2]) + && ('_' == bas_c[3]) + && ('/' == bas_c[4]) ) { bas_c = bas_c + 4; // retain '/' after /_~_ len_w = len_w - 4; @@ -828,6 +831,7 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) req_u->peq_u = c3_malloc(sizeof(*req_u->peq_u)); req_u->peq_u->req_u = req_u; req_u->peq_u->htd_u = htd_u; + req_u->peq_u->las_o = c3n; req_u->sat_e = u3_rsat_peek; u3_hfig* fig_u = &req_u->hon_u->htp_u->htd_u->fig_u; @@ -848,7 +852,6 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) u3_noun who; u3_noun des; u3_noun cas; - c3_o last = c3n; // get beak from path // @@ -887,7 +890,7 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) *wer = u3i_string("base"); } else { - last = c3y; + req_u->peq_u->las_o = c3y; } bas_c++; len_w--; @@ -923,7 +926,7 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) u3_noun spur = u3dc("rush", u3i_bytes(len_w, (const c3_y*)bas_c), u3v_wish("stap")); - if ( (who != our) || (spur == u3_nul) ) { + if ( (who != our) || (u3_nul == spur) ) { c3_c* msg_c = "bad scry path"; h2o_send_error_generic(req_u->rec_u, 400, msg_c, msg_c, 0); return; @@ -931,9 +934,7 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) else { spur = u3nc(u3i_string("mime"), u3t(spur)); - if ( c3y == last ) { - // DON'T CACHE - req_u->peq_u->las_o = c3y; + if ( c3y == req_u->peq_u->las_o ) { u3_pier_peek_last(htd_u->car_u.pir_u, gang, c3__ex, des, spur, req_u->peq_u, _http_foo_cb); } @@ -961,14 +962,14 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) h2o_send_error_generic(req_u->rec_u, 416, msg_c, msg_c, 0); return; } - range_header rng_hed = _get_range(req_headers.entries[idx].value.base + 6, rest_len); + range_request rng_req = _parse_range(req_headers.entries[idx].value.base + 6, rest_len); u3_noun octs = u3r_at(127, nac); if ( u3_none == octs ) { c3_c* msg_c = "Requested Range Not Satisfiable"; h2o_send_error_generic(req_u->rec_u, 416, msg_c, msg_c, 0); return; } - content result = _slice_mime(rng_hed, octs); + content result = _slice_mime(rng_req, octs); if ( u3_nul == result.dat ) { c3_c* msg_c = "Requested Range Not Satisfiable"; @@ -980,8 +981,8 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) // 200 u3_atom len = u3r_at(254, nac); u3_noun hez = _content_headers(0, (len - 1), len); - u3_noun mac = u3i_edit(nac, 125, u3qb_weld(hez, u3r_at(125, nac))); - _http_cache_respond(req_u, mac); + u3_noun res = u3i_edit(nac, 125, u3qb_weld(hez, u3r_at(125, nac))); + _http_cache_respond(req_u, res); } else { // 206 From 10687827f81310dfd7039d924acbfa2d1785e8c8 Mon Sep 17 00:00:00 2001 From: Tinnus Napbus Date: Tue, 16 Jul 2024 18:24:57 +1200 Subject: [PATCH 243/430] mass: cleanup per matthew-levan feedback - init len_w in quac, use null term'd list instead - add spaces between arithmetic operators - publicise _lord_writ_new() as u3_lord_writ_new() - rename lord_writ_plan as u3_lord_writ_plan - remove superfluous printfs - misc tidying --- pkg/noun/allocate.c | 51 +++++++++++++++++++++++---------------------- pkg/noun/jets.c | 20 ++++++++++-------- pkg/noun/manage.c | 3 ++- pkg/noun/manage.h | 1 - pkg/noun/nock.c | 9 ++++---- pkg/noun/vortex.c | 11 +++++----- pkg/vere/io/term.c | 6 ++---- pkg/vere/lord.c | 42 ++++++++++++++++++------------------- pkg/vere/serf.c | 49 ++++++++++++++++++++++++++----------------- pkg/vere/vere.h | 9 ++++++-- 10 files changed, 110 insertions(+), 91 deletions(-) diff --git a/pkg/noun/allocate.c b/pkg/noun/allocate.c index f17392f4f0..60332b5b52 100644 --- a/pkg/noun/allocate.c +++ b/pkg/noun/allocate.c @@ -2032,8 +2032,10 @@ _ca_print_memory(FILE* fil_u, c3_w byt_w) void u3a_quac_free(u3m_quac* qua_u) { - for ( c3_w i_w = 0; i_w < qua_u->len_w; i_w++ ) { + c3_w i_w = 0; + while ( qua_u->qua_u[i_w] != NULL ) { u3a_quac_free(qua_u->qua_u[i_w]); + i_w++; } c3_free(qua_u->nam_c); c3_free(qua_u->qua_u); @@ -2045,7 +2047,6 @@ u3a_quac_free(u3m_quac* qua_u) u3m_quac* u3a_prof(FILE* fil_u, u3_noun mas) { -// c3_w tot_w = 0; u3m_quac* pro_u = c3_calloc(sizeof(*pro_u)); u3_noun h_mas, t_mas; @@ -2106,35 +2107,33 @@ u3a_prof(FILE* fil_u, u3_noun mas) #endif pro_u->nam_c = u3r_string(h_mas); pro_u->siz_w = siz_w*4; - pro_u->len_w = 0; - pro_u->qua_u = 0; + pro_u->qua_u = NULL; return pro_u; } else if ( c3n == it_mas ) { - fprintf(fil_u, "\r\n"); pro_u->qua_u = c3_malloc(sizeof(pro_u->qua_u)); - pro_u->len_w = 0; - c3_w i_w = 2; + c3_w i_w = 0; c3_t bad_t = 0; while ( c3y == u3du(tt_mas) ) { u3m_quac* new_u = u3a_prof(fil_u, u3h(tt_mas)); if ( NULL == new_u ) { - c3_free(new_u); bad_t = 1; } else { - pro_u->qua_u = c3_realloc(pro_u->qua_u, i_w*sizeof(pro_u->qua_u)); + pro_u->qua_u = c3_realloc(pro_u->qua_u, (i_w + 2) * sizeof(pro_u->qua_u)); pro_u->siz_w += new_u->siz_w; - pro_u->qua_u[pro_u->len_w] = new_u; - pro_u->len_w++; + pro_u->qua_u[i_w] = new_u; } tt_mas = u3t(tt_mas); i_w++; } + pro_u->qua_u[i_w] = NULL; if ( bad_t ) { - for ( i_w = 0; i_w < pro_u->len_w ; i_w++ ) { + i_w = 0; + while ( pro_u->qua_u[i_w] != NULL ) { u3a_quac_free(pro_u->qua_u[i_w]); + i_w++; } c3_free(pro_u->qua_u); c3_free(pro_u); @@ -2164,13 +2163,14 @@ u3a_print_quac(FILE* fil_u, c3_w den_w, u3m_quac* mas_u) if ( mas_u->siz_w ) { fprintf(fil_u, "%*s%s: ", den_w, "", mas_u->nam_c); - if ( mas_u->len_w == 0) { + if ( mas_u->qua_u == NULL ) { _ca_print_memory(fil_u, mas_u->siz_w); } else { fprintf(fil_u, "\r\n"); - c3_w i_w; - for ( i_w = 0; i_w < mas_u->len_w; i_w++ ) { + c3_w i_w = 0; + while ( mas_u->qua_u[i_w] != NULL ) { u3a_print_quac(fil_u, den_w+2, mas_u->qua_u[i_w]); + i_w++; } fprintf(fil_u, "%*s--", den_w, ""); _ca_print_memory(fil_u, mas_u->siz_w); @@ -2183,39 +2183,41 @@ u3a_print_quac(FILE* fil_u, c3_w den_w, u3m_quac* mas_u) u3m_quac* u3a_mark_road() { - u3m_quac** qua_u = c3_malloc(sizeof(*qua_u)*8); + u3m_quac** qua_u = c3_malloc(sizeof(*qua_u) * 9); qua_u[0] = c3_calloc(sizeof(*qua_u[0])); qua_u[0]->nam_c = strdup("namespace"); - qua_u[0]->siz_w = u3a_mark_noun(u3R->ski.gul)*4; + qua_u[0]->siz_w = u3a_mark_noun(u3R->ski.gul) * 4; qua_u[1] = c3_calloc(sizeof(*qua_u[1])); qua_u[1]->nam_c = strdup("trace stack"); - qua_u[1]->siz_w = u3a_mark_noun(u3R->ski.gul)*4; + qua_u[1]->siz_w = u3a_mark_noun(u3R->ski.gul) * 4; qua_u[2] = c3_calloc(sizeof(*qua_u[2])); qua_u[2]->nam_c = strdup("trace buffer"); - qua_u[2]->siz_w = u3a_mark_noun(u3R->bug.mer)*4; + qua_u[2]->siz_w = u3a_mark_noun(u3R->bug.mer) * 4; qua_u[3] = c3_calloc(sizeof(*qua_u[3])); qua_u[3]->nam_c = strdup("profile batteries"); - qua_u[3]->siz_w = u3a_mark_noun(u3R->pro.don)*4; + qua_u[3]->siz_w = u3a_mark_noun(u3R->pro.don) * 4; qua_u[4] = c3_calloc(sizeof(*qua_u[4])); qua_u[4]->nam_c = strdup("profile doss"); - qua_u[4]->siz_w = u3a_mark_noun(u3R->pro.day)*4; + qua_u[4]->siz_w = u3a_mark_noun(u3R->pro.day) * 4; qua_u[5] = c3_calloc(sizeof(*qua_u[5])); qua_u[5]->nam_c = strdup("new profile trace"); - qua_u[5]->siz_w = u3a_mark_noun(u3R->pro.trace)*4; + qua_u[5]->siz_w = u3a_mark_noun(u3R->pro.trace) * 4; qua_u[6] = c3_calloc(sizeof(*qua_u[6])); qua_u[6]->nam_c = strdup("transient memoization cache"); - qua_u[6]->siz_w = u3h_mark(u3R->cax.har_p)*4; + qua_u[6]->siz_w = u3h_mark(u3R->cax.har_p) * 4; qua_u[7] = c3_calloc(sizeof(*qua_u[7])); qua_u[7]->nam_c = strdup("persistent memoization cache"); - qua_u[7]->siz_w = u3h_mark(u3R->cax.per_p)*4; + qua_u[7]->siz_w = u3h_mark(u3R->cax.per_p) * 4; + + qua_u[8] = NULL; c3_w sum_w = 0; for (c3_w i_w = 0; i_w < 8; i_w++) { @@ -2225,7 +2227,6 @@ u3a_mark_road() u3m_quac* tot_u = c3_malloc(sizeof(*tot_u)); tot_u->nam_c = strdup("total road stuff"); tot_u->siz_w = sum_w; - tot_u->len_w = 8; tot_u->qua_u = qua_u; return tot_u; diff --git a/pkg/noun/jets.c b/pkg/noun/jets.c index 2f1dc3a0df..c0b17142ef 100644 --- a/pkg/noun/jets.c +++ b/pkg/noun/jets.c @@ -2308,28 +2308,28 @@ _cj_mark_hank(u3_noun kev, void* dat) u3m_quac* u3j_mark() { - u3m_quac** qua_u = c3_malloc(sizeof(*qua_u)*6); + u3m_quac** qua_u = c3_malloc(sizeof(*qua_u) * 7); qua_u[0] = c3_calloc(sizeof(*qua_u[0])); qua_u[0]->nam_c = strdup("warm jet state"); - qua_u[0]->siz_w = u3h_mark(u3R->jed.war_p)*4; + qua_u[0]->siz_w = u3h_mark(u3R->jed.war_p) * 4; qua_u[1] = c3_calloc(sizeof(*qua_u[1])); qua_u[1]->nam_c = strdup("cold jet state"); - qua_u[1]->siz_w = u3h_mark(u3R->jed.cod_p)*4; + qua_u[1]->siz_w = u3h_mark(u3R->jed.cod_p) * 4; qua_u[2] = c3_calloc(sizeof(*qua_u[2])); qua_u[2]->nam_c = strdup("hank cache"); - qua_u[2]->siz_w = u3h_mark(u3R->jed.han_p)*4; + qua_u[2]->siz_w = u3h_mark(u3R->jed.han_p) * 4; qua_u[3] = c3_calloc(sizeof(*qua_u[3])); qua_u[3]->nam_c = strdup("battery hash cache"); - qua_u[3]->siz_w = u3h_mark(u3R->jed.bas_p)*4; + qua_u[3]->siz_w = u3h_mark(u3R->jed.bas_p) * 4; qua_u[4] = c3_calloc(sizeof(*qua_u[4])); qua_u[4]->nam_c = strdup("call site cache"); u3h_walk_with(u3R->jed.han_p, _cj_mark_hank, &qua_u[4]->siz_w); - qua_u[4]->siz_w = qua_u[4]->siz_w*4; + qua_u[4]->siz_w *= 4; c3_w sum_w = 0; for ( c3_w i_w = 0; i_w < 5; i_w++ ) { @@ -2342,18 +2342,20 @@ u3j_mark() if ( u3R == &(u3H->rod_u) ) { qua_u[5] = c3_calloc(sizeof(*qua_u[5])); qua_u[5]->nam_c = strdup("hot jet state"); - qua_u[5]->siz_w = u3h_mark(u3R->jed.hot_p)*4; + qua_u[5]->siz_w = u3h_mark(u3R->jed.hot_p) * 4; sum_w += qua_u[5]->siz_w; + qua_u[6] = NULL; + tot_u->siz_w = sum_w; - tot_u->len_w = 6; tot_u->qua_u = qua_u; return tot_u; } else { + qua_u[5] = NULL; + tot_u->siz_w = sum_w; - tot_u->len_w = 5; tot_u->qua_u = qua_u; return tot_u; diff --git a/pkg/noun/manage.c b/pkg/noun/manage.c index 3a462ef9ea..3f94f34055 100644 --- a/pkg/noun/manage.c +++ b/pkg/noun/manage.c @@ -462,11 +462,12 @@ u3m_file(c3_c* pas_c) u3m_quac** u3m_mark() { - u3m_quac** qua_u = c3_malloc(sizeof(*qua_u)*4); + u3m_quac** qua_u = c3_malloc(sizeof(*qua_u) * 5); qua_u[0] = u3v_mark(); qua_u[1] = u3j_mark(); qua_u[2] = u3n_mark(); qua_u[3] = u3a_mark_road(); + qua_u[4] = NULL; return qua_u; } diff --git a/pkg/noun/manage.h b/pkg/noun/manage.h index 33c75b4ee4..974c5948f6 100644 --- a/pkg/noun/manage.h +++ b/pkg/noun/manage.h @@ -154,7 +154,6 @@ typedef struct _u3m_quac { c3_c* nam_c; c3_w siz_w; - c3_w len_w; struct _u3m_quac** qua_u; } u3m_quac; diff --git a/pkg/noun/nock.c b/pkg/noun/nock.c index e7b1ab64be..7530191c01 100644 --- a/pkg/noun/nock.c +++ b/pkg/noun/nock.c @@ -3050,23 +3050,24 @@ _n_bam(u3_noun kev, void* dat) u3m_quac* u3n_mark() { - u3m_quac** qua_u = c3_malloc(sizeof(*qua_u)*2); + u3m_quac** qua_u = c3_malloc(sizeof(*qua_u) * 3); qua_u[0] = c3_calloc(sizeof(*qua_u[0])); qua_u[0]->nam_c = strdup("bytecode programs"); u3p(u3h_root) har_p = u3R->byc.har_p; u3h_walk_with(har_p, _n_bam, &qua_u[0]->siz_w); - qua_u[0]->siz_w = qua_u[0]->siz_w*4; + qua_u[0]->siz_w = qua_u[0]->siz_w * 4; qua_u[1] = c3_calloc(sizeof(*qua_u[1])); qua_u[1]->nam_c = strdup("bytecode cache"); - qua_u[1]->siz_w = u3h_mark(har_p)*4; + qua_u[1]->siz_w = u3h_mark(har_p) * 4; + + qua_u[2] = NULL; u3m_quac* tot_u = c3_malloc(sizeof(*tot_u)); tot_u->nam_c = strdup("total nock stuff"); tot_u->siz_w = qua_u[0]->siz_w + qua_u[1]->siz_w; - tot_u->len_w = 2; tot_u->qua_u = qua_u; return tot_u; diff --git a/pkg/noun/vortex.c b/pkg/noun/vortex.c index c49d73780f..287be49e8c 100644 --- a/pkg/noun/vortex.c +++ b/pkg/noun/vortex.c @@ -397,24 +397,25 @@ u3v_mark() { u3v_arvo* arv_u = &(u3H->arv_u); - u3m_quac** qua_u = c3_malloc(sizeof(*qua_u)*3); + u3m_quac** qua_u = c3_malloc(sizeof(*qua_u) * 4); qua_u[0] = c3_calloc(sizeof(*qua_u[0])); qua_u[0]->nam_c = strdup("kernel"); - qua_u[0]->siz_w = u3a_mark_noun(arv_u->roc)*4; + qua_u[0]->siz_w = u3a_mark_noun(arv_u->roc) * 4; qua_u[1] = c3_calloc(sizeof(*qua_u[1])); qua_u[1]->nam_c = strdup("date"); - qua_u[1]->siz_w = u3a_mark_noun(arv_u->now)*4; + qua_u[1]->siz_w = u3a_mark_noun(arv_u->now) * 4; qua_u[2] = c3_calloc(sizeof(*qua_u[2])); qua_u[2]->nam_c = strdup("wish cache"); - qua_u[2]->siz_w = u3a_mark_noun(arv_u->yot)*4; + qua_u[2]->siz_w = u3a_mark_noun(arv_u->yot) * 4; + + qua_u[3] = NULL; u3m_quac* tot_u = c3_malloc(sizeof(*tot_u)); tot_u->nam_c = strdup("total arvo stuff"); tot_u->siz_w = qua_u[0]->siz_w + qua_u[1]->siz_w + qua_u[2]->siz_w; - tot_u->len_w = 3; tot_u->qua_u = qua_u; return tot_u; diff --git a/pkg/vere/io/term.c b/pkg/vere/io/term.c index 927116bc62..bf9789aabc 100644 --- a/pkg/vere/io/term.c +++ b/pkg/vere/io/term.c @@ -1741,14 +1741,12 @@ _term_io_kick(u3_auto* car_u, u3_noun wir, u3_noun cad) case c3__quac: { ret_o = c3y; - // construct and send writ here - u3_writ* wit_u = c3_calloc(sizeof(*wit_u)); + u3_writ* wit_u = u3_lord_writ_new(u3K.pir_u->god_u); wit_u->typ_e = u3_writ_quiz; wit_u->qui_u.ptr_v = car_u; - u3l_log("car_u: %p", car_u); wit_u->qui_u.quiz_f = _term_io_quiz; - lord_writ_plan(u3K.pir_u->god_u, wit_u); + u3_lord_writ_plan(u3K.pir_u->god_u, wit_u); } break; } diff --git a/pkg/vere/lord.c b/pkg/vere/lord.c index 565dab2baf..3553aa402f 100644 --- a/pkg/vere/lord.c +++ b/pkg/vere/lord.c @@ -763,10 +763,10 @@ _lord_on_plea(void* ptr_v, c3_d len_d, c3_y* byt_y) u3z(jar); } -/* _lord_writ_new(): allocate a new writ. +/* u3_lord_writ_new(): allocate a new writ. */ -static u3_writ* -_lord_writ_new(u3_lord* god_u) +u3_writ* +u3_lord_writ_new(u3_lord* god_u) { u3_writ* wit_u = c3_calloc(sizeof(*wit_u)); return wit_u; @@ -869,10 +869,10 @@ _lord_writ_send(u3_lord* god_u, u3_writ* wit_u) } } -/* lord_writ_plan(): enqueue a writ and send. +/* u3_lord_writ_plan(): enqueue a writ and send. */ void -lord_writ_plan(u3_lord* god_u, u3_writ* wit_u) +u3_lord_writ_plan(u3_lord* god_u, u3_writ* wit_u) { if ( !god_u->ent_u ) { u3_assert( !god_u->ext_u ); @@ -894,7 +894,7 @@ lord_writ_plan(u3_lord* god_u, u3_writ* wit_u) void u3_lord_peek(u3_lord* god_u, u3_pico* pic_u) { - u3_writ* wit_u = _lord_writ_new(god_u); + u3_writ* wit_u = u3_lord_writ_new(god_u); wit_u->typ_e = u3_writ_peek; wit_u->pek_u = c3_calloc(sizeof(*wit_u->pek_u)); wit_u->pek_u->ptr_v = pic_u->ptr_v; @@ -925,7 +925,7 @@ u3_lord_peek(u3_lord* god_u, u3_pico* pic_u) // XX cache check, unless last // - lord_writ_plan(god_u, wit_u); + u3_lord_writ_plan(god_u, wit_u); } /* u3_lord_play(): recompute batch. @@ -933,7 +933,7 @@ u3_lord_peek(u3_lord* god_u, u3_pico* pic_u) void u3_lord_play(u3_lord* god_u, u3_info fon_u) { - u3_writ* wit_u = _lord_writ_new(god_u); + u3_writ* wit_u = u3_lord_writ_new(god_u); wit_u->typ_e = u3_writ_play; wit_u->fon_u = fon_u; @@ -941,7 +941,7 @@ u3_lord_play(u3_lord* god_u, u3_info fon_u) // // u3_assert( !pay_u.ent_u->nex_u ); - lord_writ_plan(god_u, wit_u); + u3_lord_writ_plan(god_u, wit_u); } /* u3_lord_work(): attempt work. @@ -949,7 +949,7 @@ u3_lord_play(u3_lord* god_u, u3_info fon_u) void u3_lord_work(u3_lord* god_u, u3_ovum* egg_u, u3_noun job) { - u3_writ* wit_u = _lord_writ_new(god_u); + u3_writ* wit_u = u3_lord_writ_new(god_u); wit_u->typ_e = u3_writ_work; wit_u->wok_u.egg_u = egg_u; wit_u->wok_u.job = job; @@ -963,7 +963,7 @@ u3_lord_work(u3_lord* god_u, u3_ovum* egg_u, u3_noun job) god_u->pin_o = c3y; } - lord_writ_plan(god_u, wit_u); + u3_lord_writ_plan(god_u, wit_u); } /* u3_lord_save(): save a snapshot. @@ -975,9 +975,9 @@ u3_lord_save(u3_lord* god_u) return c3n; } else { - u3_writ* wit_u = _lord_writ_new(god_u); + u3_writ* wit_u = u3_lord_writ_new(god_u); wit_u->typ_e = u3_writ_save; - lord_writ_plan(god_u, wit_u); + u3_lord_writ_plan(god_u, wit_u); return c3y; } } @@ -991,9 +991,9 @@ u3_lord_cram(u3_lord* god_u) return c3n; } else { - u3_writ* wit_u = _lord_writ_new(god_u); + u3_writ* wit_u = u3_lord_writ_new(god_u); wit_u->typ_e = u3_writ_cram; - lord_writ_plan(god_u, wit_u); + u3_lord_writ_plan(god_u, wit_u); return c3y; } } @@ -1003,9 +1003,9 @@ u3_lord_cram(u3_lord* god_u) void u3_lord_meld(u3_lord* god_u) { - u3_writ* wit_u = _lord_writ_new(god_u); + u3_writ* wit_u = u3_lord_writ_new(god_u); wit_u->typ_e = u3_writ_meld; - lord_writ_plan(god_u, wit_u); + u3_lord_writ_plan(god_u, wit_u); } /* u3_lord_pack(): defragment persistent state. @@ -1013,9 +1013,9 @@ u3_lord_meld(u3_lord* god_u) void u3_lord_pack(u3_lord* god_u) { - u3_writ* wit_u = _lord_writ_new(god_u); + u3_writ* wit_u = u3_lord_writ_new(god_u); wit_u->typ_e = u3_writ_pack; - lord_writ_plan(god_u, wit_u); + u3_lord_writ_plan(god_u, wit_u); } /* u3_lord_exit(): shutdown gracefully. @@ -1023,9 +1023,9 @@ u3_lord_pack(u3_lord* god_u) void u3_lord_exit(u3_lord* god_u) { - u3_writ* wit_u = _lord_writ_new(god_u); + u3_writ* wit_u = u3_lord_writ_new(god_u); wit_u->typ_e = u3_writ_exit; - lord_writ_plan(god_u, wit_u); + u3_lord_writ_plan(god_u, wit_u); // XX set timer, then halt } diff --git a/pkg/vere/serf.c b/pkg/vere/serf.c index bf0d4ae949..fd28f47f08 100644 --- a/pkg/vere/serf.c +++ b/pkg/vere/serf.c @@ -78,8 +78,12 @@ u3_noun _serf_quac(u3m_quac* mas_u) { u3_noun list = u3_nul; - for ( c3_w i_w = 0; i_w < mas_u->len_w; i_w++ ) { - list = u3nc(_serf_quac(mas_u->qua_u[i_w]), list); + c3_w i_w = 0; + if ( mas_u->qua_u != NULL ) { + while ( mas_u->qua_u[i_w] != NULL ) { + list = u3nc(_serf_quac(mas_u->qua_u[i_w]), list); + i_w++; + } } list = u3kb_flop(list); @@ -95,11 +99,13 @@ _serf_quac(u3m_quac* mas_u) /* _serf_quacs: convert an array of quacs to a noun list. */ u3_noun -_serf_quacs(c3_w len_w, u3m_quac** all_u) +_serf_quacs(u3m_quac** all_u) { u3_noun list = u3_nul; - for ( c3_w i_w = 0; i_w < len_w; i_w++ ) { + c3_w i_w = 0; + while ( all_u[i_w] != NULL ) { list = u3nc(_serf_quac(all_u[i_w]), list); + i_w++; } c3_free(all_u); return u3kb_flop(list); @@ -108,10 +114,13 @@ _serf_quacs(c3_w len_w, u3m_quac** all_u) /* _serf_print_quacs: print an array of quacs. */ void -_serf_print_quacs(FILE* fil_u, c3_w len_w, u3m_quac** all_u) +_serf_print_quacs(FILE* fil_u, u3m_quac** all_u) { - for ( c3_w i_w = 0; i_w < len_w; i_w++) { + fprintf(fil_u, "\r\n"); + c3_w i_w = 0; + while ( all_u[i_w] != NULL ) { u3a_print_quac(fil_u, 0, all_u[i_w]); + i_w++; } } @@ -160,12 +169,13 @@ _serf_grab(u3_noun sac, c3_o pri_o) u3_assert( u3R == &(u3H->rod_u) ); u3m_quac* pro_u = u3a_prof(fil_u, sac); + if ( NULL == pro_u ) { fflush(fil_u); u3z(sac); return u3_nul; } else { - u3m_quac** all_u = c3_malloc(sizeof(*all_u)*9); + u3m_quac** all_u = c3_malloc(sizeof(*all_u) * 10); all_u[0] = pro_u; u3m_quac** var_u = u3m_mark(); @@ -180,7 +190,7 @@ _serf_grab(u3_noun sac, c3_o pri_o) all_u[5] = c3_calloc(sizeof(*all_u[5])); all_u[5]->nam_c = strdup("space profile"); - all_u[5]->siz_w = u3a_mark_noun(sac)*4; + all_u[5]->siz_w = u3a_mark_noun(sac) * 4; tot_w += all_u[5]->siz_w; @@ -190,14 +200,16 @@ _serf_grab(u3_noun sac, c3_o pri_o) all_u[7] = c3_calloc(sizeof(*all_u[7])); all_u[7]->nam_c = strdup("free lists"); - all_u[7]->siz_w = u3a_idle(u3R)*4; + all_u[7]->siz_w = u3a_idle(u3R) * 4; all_u[8] = c3_calloc(sizeof(*all_u[8])); all_u[8]->nam_c = strdup("sweep"); - all_u[8]->siz_w = u3a_sweep()*4; + all_u[8]->siz_w = u3a_sweep() * 4; + + all_u[9] = NULL; if ( c3y == pri_o ) { - _serf_print_quacs(fil_u, 9, all_u); + _serf_print_quacs(fil_u, all_u); } fflush(fil_u); @@ -207,7 +219,7 @@ _serf_grab(u3_noun sac, c3_o pri_o) } #endif - u3_noun mas = _serf_quacs(9, all_u); + u3_noun mas = _serf_quacs( all_u); u3z(sac); return mas; @@ -252,7 +264,6 @@ u3_serf_grab(c3_o pri_o) u3z(gon); } - fprintf(stderr, "serf: measuring memory:\r\n"); if ( u3_nul != sac ) { res = _serf_grab(sac, pri_o); } @@ -260,16 +271,16 @@ u3_serf_grab(c3_o pri_o) fprintf(stderr, "sac is empty\r\n"); u3m_quac** var_u = u3m_mark(); - c3_w tot_w; - tot_w = var_u[0]->siz_w + var_u[1]->siz_w - + var_u[2]->siz_w + var_u[3]->siz_w; - - for ( c3_w i_w = 0; i_w < 4; i_w++ ) { + c3_w tot_w = 0; + c3_w i_w = 0; + while ( var_u[i_w] != NULL ) { + tot_w += var_u[i_w]->siz_w; u3a_quac_free(var_u[i_w]); + i_w++; } c3_free(var_u); - u3a_print_memory(stderr, "total marked", tot_w/4); + u3a_print_memory(stderr, "total marked", tot_w / 4); u3a_print_memory(stderr, "free lists", u3a_idle(u3R)); u3a_print_memory(stderr, "sweep", u3a_sweep()); fprintf(stderr, "\r\n"); diff --git a/pkg/vere/vere.h b/pkg/vere/vere.h index 9d4cfdb5aa..59644518f5 100644 --- a/pkg/vere/vere.h +++ b/pkg/vere/vere.h @@ -770,10 +770,15 @@ u3_atom u3_time_t_in_ts(time_t tim); #endif - /* lord_writ_plan(): enqueue a writ and send. + /* u3_lord_writ_new(): allocate a new writ. + */ + u3_writ* + u3_lord_writ_new(u3_lord* god_u); + + /* u3_lord_writ_plan(): enqueue a writ and send. */ void - lord_writ_plan(u3_lord* god_u, u3_writ* wit_u); + u3_lord_writ_plan(u3_lord* god_u, u3_writ* wit_u); /* u3_time_out_ts(): struct timespec from urbit time. */ From b68c670e533b948b99ffd9d777dde503593c09e9 Mon Sep 17 00:00:00 2001 From: Tinnus Napbus Date: Tue, 16 Jul 2024 18:44:11 +1200 Subject: [PATCH 244/430] mass: include loom size in report --- pkg/vere/serf.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pkg/vere/serf.c b/pkg/vere/serf.c index fd28f47f08..c410212db9 100644 --- a/pkg/vere/serf.c +++ b/pkg/vere/serf.c @@ -175,7 +175,7 @@ _serf_grab(u3_noun sac, c3_o pri_o) u3z(sac); return u3_nul; } else { - u3m_quac** all_u = c3_malloc(sizeof(*all_u) * 10); + u3m_quac** all_u = c3_malloc(sizeof(*all_u) * 11); all_u[0] = pro_u; u3m_quac** var_u = u3m_mark(); @@ -205,8 +205,12 @@ _serf_grab(u3_noun sac, c3_o pri_o) all_u[8] = c3_calloc(sizeof(*all_u[8])); all_u[8]->nam_c = strdup("sweep"); all_u[8]->siz_w = u3a_sweep() * 4; + + all_u[9] = c3_calloc(sizeof(*all_u[9])); + all_u[9]->nam_c = strdup("loom"); + all_u[9]->siz_w = u3C.wor_i * 4; - all_u[9] = NULL; + all_u[10] = NULL; if ( c3y == pri_o ) { _serf_print_quacs(fil_u, all_u); From 06c6d9749c5ad89472403a72fe69c1066748c0f9 Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Tue, 16 Jul 2024 11:44:30 -0400 Subject: [PATCH 245/430] mesa: adding a next hop compiles, runs, doesn't handle indirect yet --- pkg/vere/io/mesa.c | 15 +++++-- pkg/vere/io/mesa/mesa.h | 22 ++++------ pkg/vere/io/mesa/pact.c | 96 +++++++++++++++++++++++++---------------- 3 files changed, 78 insertions(+), 55 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 89d0a92a2c..da8a71f568 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1442,6 +1442,11 @@ static c3_o _mesa_kick(u3_mesa* sam_u, u3_noun tag, u3_noun dat) case c3__turf: case c3__saxo: case c3__nail: { + #ifdef MESA_DEBUG + c3_c* tag_c = u3r_string(tag); + u3l_log("mesa: send old %s", tag_c); + c3_free(tag_c); + #endif ret_o = _ames_kick_newt(u3_Host.sam_u, u3k(tag), u3k(dat)); } break; } @@ -2058,7 +2063,11 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) #ifdef MESA_DEBUG u3l_log(" forwarding"); #endif - // update_hopcount(&pac_u->hed_u); // TODO reinstate + + update_hopcount(&pac_u->hed_u); + c3_etch_word(pac_u->pag_u.sot_u, lan_u.pip_w); + c3_etch_short(pac_u->pag_u.sot_u + 4, lan_u.por_s); + // TODO actually stick next hop in packet _mesa_send_pact(sam_u, u3k(las), per_u, pac_u); _mesa_del_pit(sam_u, nam_u); @@ -2121,8 +2130,8 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) u3_lane lon_u; if ( HOP_SHORT == pac_u->hed_u.nex_y ) { - lon_u.pip_w = *(c3_w*)pac_u->pag_u.sot_u; - lon_u.por_s = *(c3_s*)&pac_u->pag_u.sot_u[4]; + lon_u.pip_w = c3_sift_word(pac_u->pag_u.sot_u); + lon_u.por_s = c3_sift_short(pac_u->pag_u.sot_u + 4); } else { lon_u = lan_u; diff --git a/pkg/vere/io/mesa/mesa.h b/pkg/vere/io/mesa/mesa.h index 252234136e..1d5f0cfe76 100644 --- a/pkg/vere/io/mesa/mesa.h +++ b/pkg/vere/io/mesa/mesa.h @@ -10,15 +10,9 @@ #define HEAD_SIZE 4 // header size in bytes #define PACT_SIZE 1472 -#define HOP_NONE 0b0 -#define HOP_SHORT 0b1 -#define HOP_LONG 0b10 -#define HOP_MANY 0b11 - #define MESA_COOKIE_LEN 4 static c3_y MESA_COOKIE[4] = { 0x5e, 0x1d, 0xad, 0x51 }; - typedef enum _u3_mesa_ptag { PACT_RESV = 0, PACT_PAGE = 1, @@ -31,12 +25,12 @@ typedef enum _u3_mesa_rout_tag { ROUT_OTHER = 1 } u3_mesa_rout_tag; -typedef enum _u3_mesa_nexh { - NEXH_NONE = 0, - NEXH_SBYT = 1, - NEXH_ONLY = 2, - NEXH_MANY = 3 -} u3_mesa_nexh; +typedef enum _u3_mesa_hop_type { + HOP_NONE = 0, + HOP_SHORT = 1, + HOP_LONG = 2, + HOP_MANY = 3 +} u3_mesa_hop_type; typedef struct _u3_mesa_name_meta { c3_y ran_y; // rank (2 bits) @@ -94,7 +88,7 @@ typedef struct _u3_mesa_data { typedef struct _u3_mesa_head { - u3_mesa_nexh nex_y; // next-hop + u3_mesa_hop_type nex_y; // next-hop c3_y pro_y; // protocol version u3_mesa_ptag typ_y; // packet type c3_y hop_y; // hopcount @@ -111,7 +105,7 @@ typedef struct _u3_mesa_peek_pact { u3_mesa_name nam_u; } u3_mesa_peek_pact; -typedef struct _u3_mesa_hop { +typedef struct _u3_mesa_hop_once { c3_w len_w; c3_y* dat_y; } u3_mesa_hop_once; diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index 59c78eb73f..439f5c382e 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -464,9 +464,42 @@ _mesa_sift_hop_long(u3_mesa_hop_once* hop_u, c3_y* buf_y, c3_w len_w) hop_u->dat_y = c3_calloc(hop_u->len_w); memcpy(hop_u->dat_y, buf_y + cur_w, hop_u->len_w); - return cur_w; + return cur_w + hop_u->len_w; } +static c3_w +_mesa_sift_hops(u3_mesa_page_pact* pac_u, c3_y nex_y, c3_y* buf_y, c3_w len_w) +{ + switch ( nex_y ) { + default: { + u3_assert(!"mesa: sift invalid hop type"); + } + case HOP_NONE: return 0; + case HOP_SHORT: { + CHECK_BOUNDS(6); + memcpy(pac_u->sot_u, buf_y, 6); + return 6; + } + case HOP_LONG: { + return _mesa_sift_hop_long(&pac_u->one_u, buf_y, len_w); + } + case HOP_MANY: { + c3_w siz_w = 0; + CHECK_BOUNDS(siz_w + 1); + pac_u->man_u.len_w = buf_y[0]; + siz_w++; + + pac_u->man_u.dat_y = c3_calloc(sizeof(u3_mesa_hop_once) * pac_u->man_u.len_w); + + for( c3_w i = 0; i < pac_u->man_u.len_w; i++ ) { + siz_w += _mesa_sift_hop_long(&pac_u->man_u.dat_y[i], + buf_y + siz_w, + len_w - siz_w); + } + return siz_w; + } + } +} static c3_w _mesa_sift_page_pact(u3_mesa_pact* pat_u, c3_y nex_y, c3_y* buf_y, c3_w len_w) @@ -484,44 +517,11 @@ _mesa_sift_page_pact(u3_mesa_pact* pat_u, c3_y nex_y, c3_y* buf_y, c3_w len_w) } cur_w += nex_w; - switch ( nex_y ) { - default: { - return 0; - } - case HOP_NONE: break; - case HOP_SHORT: { - CHECK_BOUNDS(cur_w + 6); - memcpy(pac_u->sot_u, buf_y + cur_w, 6); - cur_w += 6; - } break; - case HOP_LONG: { - c3_w hop_w = _mesa_sift_hop_long(&pac_u->one_u, buf_y + cur_w, len_w - cur_w); - if( hop_w == 0 ) { - return 0; - } - cur_w += hop_w; - } break; - case HOP_MANY: { - CHECK_BOUNDS(cur_w + 1); - pac_u->man_u.len_w = buf_y[cur_w]; - cur_w++; - - pac_u->man_u.dat_y = c3_calloc(sizeof(u3_mesa_hop_once) * pac_u->man_u.len_w); - - for( int i = 0; i < pac_u->man_u.len_w; i++ ) { - c3_w hop_w = _mesa_sift_hop_long(&pac_u->man_u.dat_y[i], buf_y + cur_w ,len_w - cur_w); - if ( hop_w == 0 ) { - return 0; - } - cur_w += hop_w; - } - } - } + cur_w += _mesa_sift_hops(pac_u, nex_y, buf_y + cur_w, len_w); return cur_w; } - static c3_w _mesa_sift_peek_pact(u3_mesa_peek_pact* pac_u, c3_y* buf_y, c3_w len_w) { @@ -601,7 +601,6 @@ mesa_sift_pact(u3_mesa_pact* pac_u, c3_y* buf_y, c3_w len_w) c3_w mug_w = u3r_mug_bytes(buf_y, res_w); mug_w &= 0xFFFFF; - if ( mug_w != pac_u->hed_u.mug_w ) { /* u3l_log("mesa: failed mug"); */ /* _log_buf(buf_y, res_w); */ @@ -874,6 +873,28 @@ _mesa_size_data(u3_mesa_data* dat_u) return siz_w; } +static c3_w +_mesa_size_hops(u3_mesa_pact* pac_u) +{ + if ( PACT_PAGE != pac_u->hed_u.typ_y ) { + return 0; + } + + switch ( pac_u->hed_u.nex_y ) { + case HOP_NONE: return 0; + case HOP_SHORT: return 6; + case HOP_LONG: return 1 + pac_u->pag_u.one_u.len_w; + case HOP_MANY: { + c3_w siz_w = 0; + for( c3_w i = 0; i < pac_u->pag_u.man_u.len_w; i++ ) { + siz_w += 1 + pac_u->pag_u.man_u.dat_y[i].len_w; + } + return siz_w; + } + default: u3_assert(!"mesa: invalid hop type"); + } +} + static c3_w _mesa_size_pact(u3_mesa_pact* pac_u) { @@ -887,7 +908,7 @@ _mesa_size_pact(u3_mesa_pact* pac_u) case PACT_PAGE: { siz_w += _mesa_size_name(&pac_u->pag_u.nam_u); siz_w += _mesa_size_data(&pac_u->pag_u.dat_u); - // XX hops + siz_w += _mesa_size_hops(pac_u); } break; case PACT_POKE: { @@ -944,7 +965,6 @@ mesa_etch_pact(c3_y* buf_y, u3_mesa_pact* pac_u) } } - hed_u->mug_w = u3r_mug_bytes(buf_y + cur_w, nex_w); hed_u->mug_w &= 0xFFFFF; From 8d567d423f680e306e48137735d6d0355b72f218 Mon Sep 17 00:00:00 2001 From: midden-fabler Date: Tue, 16 Jul 2024 18:21:26 -0400 Subject: [PATCH 246/430] http: additional range spec compliance --- pkg/vere/io/http.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/pkg/vere/io/http.c b/pkg/vere/io/http.c index c8496e90d0..19367144e6 100644 --- a/pkg/vere/io/http.c +++ b/pkg/vere/io/http.c @@ -732,7 +732,9 @@ _slice_mime(range_request rng, u3_noun octs) else { // [~ @] if ( rng.end_z > len_w ) { - return out; + // -9000/42 + out.beg_z = 0; + out.end_z = len_w - 1; } else { // slice last bytes @@ -752,7 +754,9 @@ _slice_mime(range_request rng, u3_noun octs) } } else if (rng.end_z > len_w) { - return out; + // 12-9000/42 + out.beg_z = rng.beg_z; + out.end_z = len_w - 1; } else { // [@ @] @@ -944,6 +948,7 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) u3_weak nac = u3h_get(htd_u->nax_p, bem); if ( u3_none == nac ) { + // cache, then serve subsequent range requests from cache req_u->peq_u->las_o = c3n; req_u->peq_u->pax = u3k(bem); u3_pier_peek(htd_u->car_u.pir_u, gang, u3k(u3nt(0, c3__ex, bem)), From 7988ca9d7d45cf6caa154af6be23b58fe64e2b86 Mon Sep 17 00:00:00 2001 From: midden-fabler Date: Wed, 17 Jul 2024 23:15:24 -0400 Subject: [PATCH 247/430] http: cleanup slice_mime --- pkg/vere/io/http.c | 35 ++++++++++------------------------- 1 file changed, 10 insertions(+), 25 deletions(-) diff --git a/pkg/vere/io/http.c b/pkg/vere/io/http.c index 19367144e6..8170d07ef3 100644 --- a/pkg/vere/io/http.c +++ b/pkg/vere/io/http.c @@ -731,41 +731,23 @@ _slice_mime(range_request rng, u3_noun octs) } else { // [~ @] - if ( rng.end_z > len_w ) { - // -9000/42 - out.beg_z = 0; - out.end_z = len_w - 1; - } - else { - // slice last bytes - out.beg_z = len_w - rng.end_z; - out.end_z = len_w - 1; - } + out.beg_z = len_w - c3_min(rng.end_z, len_w); + out.end_z = len_w - 1; } } else if ( SIZE_MAX == rng.end_z ) { // [@ ~] - if ( rng.beg_z > len_w ) { - return out; - } - else { - out.beg_z = rng.beg_z; - out.end_z = len_w - 1; - } - } - else if (rng.end_z > len_w) { - // 12-9000/42 out.beg_z = rng.beg_z; out.end_z = len_w - 1; } else { // [@ @] out.beg_z = rng.beg_z; - out.end_z = rng.end_z; + out.end_z = c3_min(rng.end_z, len_w - 1); } - if ( (out.beg_z < len_w) - && (out.end_z < len_w) - && (out.beg_z <= out.end_z) ) + if ( (out.beg_z < len_w) + && (out.end_z < len_w) + && (out.beg_z <= out.end_z) ) { out.dat = u3nc((out.end_z - out.beg_z) + 1, u3qc_cut(3, out.beg_z, (out.end_z + 1) - out.beg_z, oct_w)); @@ -795,7 +777,7 @@ _parse_range(c3_c* txt_c, c3_w len_w) return cut; } -/* _http_req_dispatch(): dispatch http request to %eyre +/* _http_req_dispatch(): dispatch http request */ static void _http_req_dispatch(u3_hreq* req_u, u3_noun req) @@ -823,6 +805,7 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) : u3nc(u3i_string("request"), dat); } + // XX make _is_http_req function if ( (len_w >= 5) && ('_' == bas_c[1]) && ('~' == bas_c[2]) @@ -955,6 +938,7 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) req_u->peq_u, _http_foo_cb); } else { + // XX gang / auth h2o_headers_t req_headers = req_u->rec_u->headers; c3_w idx = h2o_find_header(&req_headers, H2O_TOKEN_RANGE, -1); @@ -1008,6 +992,7 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) } else { + // XX move to first branch // inject to arvo u3_auto_plan(&htd_u->car_u, u3_ovum_init(0, c3__e, wir, cad)); } From 7bf5043d828125994574c18b0f29f54c5ecfad3b Mon Sep 17 00:00:00 2001 From: midden-fabler Date: Thu, 18 Jul 2024 13:10:50 -0400 Subject: [PATCH 248/430] http: auth & cache --- pkg/vere/io/http.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/pkg/vere/io/http.c b/pkg/vere/io/http.c index 8170d07ef3..c74e3a7741 100644 --- a/pkg/vere/io/http.c +++ b/pkg/vere/io/http.c @@ -694,8 +694,10 @@ _http_foo_cb(void* vod_p, u3_noun nun) } } - // cache only if peek was not at now - if ( c3n == peq_u->las_o ) { + // cache only if peek was not at now, and nun isn't u3_nul + if ( (c3n == peq_u->las_o) + && (u3_nul != nun) ) + { u3h_put(htd_u->nax_p, peq_u->pax, nun); } u3z(peq_u->pax); @@ -930,15 +932,18 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) u3_noun bem = u3nq(our, des, cas, spur); u3_weak nac = u3h_get(htd_u->nax_p, bem); - if ( u3_none == nac ) { - // cache, then serve subsequent range requests from cache + if ( (u3_none == nac) + || (u3_nul == nac) + || ((u3_nul == gang) && (c3y == u3r_at(14, nac))) ) + { + // maybe cache, then serve subsequent range requests from cache req_u->peq_u->las_o = c3n; req_u->peq_u->pax = u3k(bem); u3_pier_peek(htd_u->car_u.pir_u, gang, u3k(u3nt(0, c3__ex, bem)), req_u->peq_u, _http_foo_cb); } else { - // XX gang / auth + h2o_headers_t req_headers = req_u->rec_u->headers; c3_w idx = h2o_find_header(&req_headers, H2O_TOKEN_RANGE, -1); From 77bdfbc0993a9214aecd0f281471dd2ab9cfa364 Mon Sep 17 00:00:00 2001 From: midden-fabler Date: Thu, 18 Jul 2024 22:38:06 -0400 Subject: [PATCH 249/430] http: better slice_mime --- pkg/vere/io/http.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/pkg/vere/io/http.c b/pkg/vere/io/http.c index c74e3a7741..b40261702f 100644 --- a/pkg/vere/io/http.c +++ b/pkg/vere/io/http.c @@ -726,27 +726,29 @@ _slice_mime(range_request rng, u3_noun octs) out.end_z = SIZE_MAX; out.dat = u3_nul; - if ( SIZE_MAX == rng.beg_z ) { - if ( SIZE_MAX == rng.end_z ) { - // [~ ~] - return out; - } - else { - // [~ @] - out.beg_z = len_w - c3_min(rng.end_z, len_w); - out.end_z = len_w - 1; - } + if ( (SIZE_MAX == rng.beg_z) + && (SIZE_MAX == rng.end_z) ) + { + // [~ ~] + return out; } - else if ( SIZE_MAX == rng.end_z ) { + + if ( SIZE_MAX == rng.end_z ) { // [@ ~] out.beg_z = rng.beg_z; out.end_z = len_w - 1; } + else if ( SIZE_MAX == rng.beg_z ) { + // [~ @] + out.beg_z = len_w - c3_min(rng.end_z, len_w); + out.end_z = len_w - 1; + } else { // [@ @] out.beg_z = rng.beg_z; out.end_z = c3_min(rng.end_z, len_w - 1); } + if ( (out.beg_z < len_w) && (out.end_z < len_w) && (out.beg_z <= out.end_z) ) From 1321e3f06d953e201edaf01c322204a4e68a55ac Mon Sep 17 00:00:00 2001 From: midden-fabler Date: Fri, 19 Jul 2024 09:48:20 -0400 Subject: [PATCH 250/430] http: move heavier branches down --- pkg/vere/io/http.c | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/pkg/vere/io/http.c b/pkg/vere/io/http.c index b40261702f..210b657802 100644 --- a/pkg/vere/io/http.c +++ b/pkg/vere/io/http.c @@ -809,13 +809,13 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) : u3nc(u3i_string("request"), dat); } - // XX make _is_http_req function - if ( (len_w >= 5) - && ('_' == bas_c[1]) - && ('~' == bas_c[2]) - && ('_' == bas_c[3]) - && ('/' == bas_c[4]) ) + if ( (len_w < 6) + || (0 != memcmp("/_~_/", bas_c, 5)) ) { + // inject to arvo + u3_auto_plan(&htd_u->car_u, u3_ovum_init(0, c3__e, wir, cad)); + } + else { bas_c = bas_c + 4; // retain '/' after /_~_ len_w = len_w - 4; @@ -949,7 +949,10 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) h2o_headers_t req_headers = req_u->rec_u->headers; c3_w idx = h2o_find_header(&req_headers, H2O_TOKEN_RANGE, -1); - if (idx != UINT32_MAX) { + if (idx == UINT32_MAX) { + _http_cache_respond(req_u, nac); + } + else { if ( (req_headers.entries[idx].value.len >= 6) && (0 == memcmp("bytes=", req_headers.entries[idx].value.base, 6)) ) { c3_w rest_len = req_headers.entries[idx].value.len - 6; @@ -990,19 +993,10 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) } } } - else { - _http_cache_respond(req_u, nac); - } } } } } - - else { - // XX move to first branch - // inject to arvo - u3_auto_plan(&htd_u->car_u, u3_ovum_init(0, c3__e, wir, cad)); - } } } From aba8da490dbbcfada119e0054f2017d1f6a47218 Mon Sep 17 00:00:00 2001 From: lukechampine Date: Fri, 19 Jul 2024 15:31:07 -0400 Subject: [PATCH 251/430] jets: switch ed25519 jets to use octs --- pkg/noun/jets/e/ed_sign.c | 34 ++++++++++++++++++---------------- pkg/noun/jets/e/ed_veri.c | 35 +++++++++++++++++++---------------- pkg/noun/jets/tree.c | 12 ++++++------ pkg/noun/jets/w.h | 4 ++-- 4 files changed, 45 insertions(+), 40 deletions(-) diff --git a/pkg/noun/jets/e/ed_sign.c b/pkg/noun/jets/e/ed_sign.c index 2728905d92..4aad1c25fd 100644 --- a/pkg/noun/jets/e/ed_sign.c +++ b/pkg/noun/jets/e/ed_sign.c @@ -5,37 +5,39 @@ #include "noun.h" #include "urcrypt.h" +#include +#include static u3_atom - _cqee_sign(u3_noun a, - u3_noun b) + _cqee_sign_octs(u3_noun len, u3_noun dat, u3_noun key) { - c3_y sed_y[32]; - - if ( 0 != u3r_bytes_fit(32, sed_y, b) ) { + c3_y key_y[32]; + c3_w len_w; + if ( 0 != u3r_bytes_fit(32, key_y, key) ) { // hoon calls suck, which calls puck, which crashes return u3m_bail(c3__exit); } + else if ( !u3r_word_fit(&len_w, len) ) { + return u3m_bail(c3__fail); + } else { c3_y sig_y[64]; - c3_w met_w; - c3_y* mes_y = u3r_bytes_all(&met_w, a); - - urcrypt_ed_sign(mes_y, met_w, sed_y, sig_y); - u3a_free(mes_y); - + c3_y* dat_y = u3r_bytes_alloc(0, len_w, dat); + urcrypt_ed_sign(dat_y, len_w, key_y, sig_y); + u3a_free(dat_y); return u3i_bytes(64, sig_y); } } u3_noun - u3wee_sign(u3_noun cor) + u3wee_sign_octs(u3_noun cor) { - u3_noun a, b; - if ( c3n == u3r_mean(cor, - u3x_sam_2, &a, u3x_sam_3, &b, 0) ) { + u3_noun msg, key; + u3_noun len, dat; + if ( c3n == u3r_mean(cor, u3x_sam_2, &msg, u3x_sam_3, &key, 0) || + c3n == u3r_cell(msg, &len, &dat) ) { return u3m_bail(c3__fail); } else { - return _cqee_sign(a, b); + return _cqee_sign_octs(len, dat, key); } } diff --git a/pkg/noun/jets/e/ed_veri.c b/pkg/noun/jets/e/ed_veri.c index 742469f075..42b8f30d69 100644 --- a/pkg/noun/jets/e/ed_veri.c +++ b/pkg/noun/jets/e/ed_veri.c @@ -7,36 +7,39 @@ #include "urcrypt.h" static u3_atom - _cqee_veri(u3_noun s, - u3_noun m, - u3_noun pk) + _cqee_veri_octs(u3_noun sig, + u3_noun len, + u3_noun dat, + u3_noun pub) { - c3_y sig_y[64], pub_y[32]; - - if ( (0 != u3r_bytes_fit(64, sig_y, s)) || - (0 != u3r_bytes_fit(32, pub_y, pk)) ) { + c3_y sig_y[64], pub_y[32]; + c3_w len_w; + if ( (0 != u3r_bytes_fit(64, sig_y, sig)) || + (0 != u3r_bytes_fit(32, pub_y, pub)) || + !u3r_word_fit(&len_w, len) ) { // hoon checks sizes, but weirdly and without crashes return u3_none; } else { - c3_w met_w; - c3_y* mes_y = u3r_bytes_all(&met_w, m); - c3_t val_t = urcrypt_ed_veri(mes_y, met_w, pub_y, sig_y); - u3a_free(mes_y); + c3_y* dat_y = u3r_bytes_alloc(0, len_w, dat); + c3_t val_t = urcrypt_ed_veri(dat_y, len_w, pub_y, sig_y); + u3a_free(dat_y); return val_t ? c3y : c3n; } } u3_noun - u3wee_veri(u3_noun cor) + u3wee_veri_octs(u3_noun cor) { - u3_noun a, b, c; + u3_noun sig, msg, pub; + u3_noun len, dat; if ( c3n == u3r_mean(cor, - u3x_sam_2, &a, u3x_sam_6, &b, - u3x_sam_7, &c, 0) ) { + u3x_sam_2, &sig, u3x_sam_6, &msg, + u3x_sam_7, &pub, 0) || + c3n == u3r_cell(msg, &len, &dat) ){ return u3m_bail(c3__fail); } else { - return u3l_punt("veri", _cqee_veri(a, b, c)); + return u3l_punt("veri-octs", _cqee_veri_octs(sig, len, dat, pub)); } } diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index f4c8727a9f..6212235991 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -275,13 +275,13 @@ static c3_c* _140_hex_coed__ed_puck_ha[] = { "1bc694675842345c50b0e20a2193bb5bcbb42f163fc832431a3d1822a81e4c98", 0 }; -static u3j_harm _140_hex_coed__ed_sign_a[] = {{".2", u3wee_sign}, {}}; -static c3_c* _140_hex_coed__ed_sign_ha[] = { +static u3j_harm _140_hex_coed__ed_sign_octs_a[] = {{".2", u3wee_sign_octs}, {}}; +static c3_c* _140_hex_coed__ed_sign_octs_ha[] = { "34ad749bf8443611cbf1f7de90a066318bd12be36f2f7f6f55281f6f7ed79754", 0 }; -static u3j_harm _140_hex_coed__ed_veri_a[] = {{".2", u3wee_veri}, {}}; -static c3_c* _140_hex_coed__ed_veri_ha[] = { +static u3j_harm _140_hex_coed__ed_veri_octs_a[] = {{".2", u3wee_veri_octs}, {}}; +static c3_c* _140_hex_coed__ed_veri_octs_ha[] = { "047a7eeccb2e68aeeee631b6db86e11a5a3aa9e179660553eca6304327612dcf", 0 }; @@ -323,9 +323,9 @@ static c3_c* _140_hex_coed__ed_shar_ha[] = { }; static u3j_core _140_hex_coed__ed_d[] = - { { "sign", 7, _140_hex_coed__ed_sign_a, 0, _140_hex_coed__ed_sign_ha }, + { { "sign-octs", 7, _140_hex_coed__ed_sign_octs_a, 0, _140_hex_coed__ed_sign_octs_ha }, { "puck", 7, _140_hex_coed__ed_puck_a, 0, _140_hex_coed__ed_puck_ha }, - { "veri", 7, _140_hex_coed__ed_veri_a, 0, _140_hex_coed__ed_veri_ha }, + { "veri-octs", 7, _140_hex_coed__ed_veri_octs_a, 0, _140_hex_coed__ed_veri_octs_ha }, { "shar", 7, _140_hex_coed__ed_shar_a, 0, _140_hex_coed__ed_shar_ha }, { "point-add", 7, _140_hex_coed__ed_point_add_a, 0, 0 }, { "scalarmult", 7, _140_hex_coed__ed_scalarmult_a, 0, diff --git a/pkg/noun/jets/w.h b/pkg/noun/jets/w.h index 41c441aeac..0edeeca95d 100644 --- a/pkg/noun/jets/w.h +++ b/pkg/noun/jets/w.h @@ -193,8 +193,8 @@ u3_noun u3weo_raw(u3_noun); u3_noun u3wee_puck(u3_noun); - u3_noun u3wee_sign(u3_noun); - u3_noun u3wee_veri(u3_noun); + u3_noun u3wee_sign_octs(u3_noun); + u3_noun u3wee_veri_octs(u3_noun); u3_noun u3wee_shar(u3_noun); u3_noun u3wee_point_add(u3_noun); u3_noun u3wee_scalarmult(u3_noun); From 1bebd6ec1af2ff53e2b10a7328bfca7772983cd3 Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Fri, 19 Jul 2024 16:18:21 -0400 Subject: [PATCH 252/430] add u3_ship library; use in meas --- pkg/vere/io/mesa.c | 151 +++++++++++++++++++++++++--------------- pkg/vere/io/mesa/mesa.h | 3 +- pkg/vere/io/mesa/pact.c | 93 +++++-------------------- 3 files changed, 116 insertions(+), 131 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index da8a71f568..c240da628c 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -5,6 +5,7 @@ #include "noun.h" #include "ur.h" +#include "ship.h" #include "mesa/mesa.h" #include "mesa/bitset.h" #include @@ -165,6 +166,7 @@ typedef struct _u3_peer { u3_mesa* sam_u; // backpointer c3_o ful_o; // has this been initialized? u3_lane dan_u; // direct lane (nullable) + u3_lane may_u; // provisional direct lane (nullable) u3_lane_state dir_u; // direct lane state c3_y imp_y; // galaxy @p u3_lane_state ind_u; // indirect lane state @@ -359,15 +361,15 @@ _get_her(u3_mesa_pact* pac_u, c3_d* our_d) break; } case PACT_PAGE: { - memcpy(our_d, pac_u->pag_u.nam_u.her_d,2); + memcpy(our_d, pac_u->pag_u.nam_u.her_u,2); break; } case PACT_PEEK: { - memcpy(our_d, pac_u->pek_u.nam_u.her_d,2); + memcpy(our_d, pac_u->pek_u.nam_u.her_u,2); break; } case PACT_POKE: { - memcpy(our_d, pac_u->pok_u.pay_u.her_d, 2); + memcpy(our_d, pac_u->pok_u.pay_u.her_u, 2); break; } } @@ -479,9 +481,9 @@ u3_mesa_encode_lane(u3_lane lan_u) { // lane cache is (map [lane @p] lane-info) // static u3_noun -_mesa_lane_key(c3_d her_d[2], u3_lane* lan_u) +_mesa_lane_key(u3_ship her_u, u3_lane* lan_u) { - return u3nc(u3i_chubs(2,her_d), u3_mesa_encode_lane(*lan_u)); + return u3nc(u3_ship_to_noun(her_u), u3_mesa_encode_lane(*lan_u)); } // TODO: all the her_p hashtable functions are not refcounted properly @@ -503,9 +505,9 @@ _mesa_get_peer_raw(u3_mesa* sam_u, u3_noun her) * RETAIN */ static u3_peer* -_mesa_get_peer(u3_mesa* sam_u, c3_d her_d[2]) +_mesa_get_peer(u3_mesa* sam_u, u3_ship her_u) { - return _mesa_get_peer_raw(sam_u, u3i_chubs(2, her_d)); + return _mesa_get_peer_raw(sam_u, u3_ship_to_noun(her_u)); } static void @@ -528,9 +530,9 @@ _mesa_put_peer_raw(u3_mesa* sam_u, u3_noun her, u3_peer* per_u) } static void -_mesa_put_peer(u3_mesa* sam_u, c3_d her_d[2], u3_peer* per_u) +_mesa_put_peer(u3_mesa* sam_u, u3_ship her_u, u3_peer* per_u) { - _mesa_put_peer_raw(sam_u, u3i_chubs(2, her_d), per_u); + _mesa_put_peer_raw(sam_u, u3_ship_to_noun(her_u), per_u); } /* _mesa_get_request(): produce pending request state for nam_u @@ -541,7 +543,7 @@ static u3_pend_req* _mesa_get_request(u3_mesa* sam_u, u3_mesa_name* nam_u) { u3_pend_req* ret_u = NULL; - u3_peer* per_u = _mesa_get_peer(sam_u, nam_u->her_d); + u3_peer* per_u = _mesa_get_peer(sam_u, nam_u->her_u); if ( !per_u ) { return ret_u; } @@ -558,7 +560,7 @@ _mesa_get_request(u3_mesa* sam_u, u3_mesa_name* nam_u) { static void _mesa_del_request(u3_mesa* sam_u, u3_mesa_name* nam_u) { - u3_peer* per_u = _mesa_get_peer(sam_u, nam_u->her_d); + u3_peer* per_u = _mesa_get_peer(sam_u, nam_u->her_u); if ( !per_u ) { return; } @@ -591,7 +593,7 @@ _mesa_del_request(u3_mesa* sam_u, u3_mesa_name* nam_u) { */ static u3_pend_req* _mesa_put_request(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_pend_req* req_u) { - u3_peer* per_u = _mesa_get_peer(sam_u, nam_u->her_d); + u3_peer* per_u = _mesa_get_peer(sam_u, nam_u->her_u); if ( !per_u ) { return NULL; } @@ -656,9 +658,9 @@ _mesa_lanes_equal(u3_lane* lan_u, u3_lane* lon_u) } static u3_lane -_mesa_get_direct_lane(u3_mesa* sam_u, c3_d her_d[2]) +_mesa_get_direct_lane(u3_mesa* sam_u, u3_ship her_u) { - return _mesa_get_direct_lane_raw(sam_u, u3i_chubs(2, her_d)); + return _mesa_get_direct_lane_raw(sam_u, u3_ship_to_noun(her_u)); } static u3_lane @@ -699,8 +701,8 @@ _mesa_get_lane_raw(u3_mesa* sam_u, u3_noun key) /* _mesa_get_lane(): get lane */ static u3_gage* -_mesa_get_lane(u3_mesa* sam_u, c3_d her_d[2], u3_lane* lan_u) { - u3_noun key =_mesa_lane_key(her_d, lan_u); +_mesa_get_lane(u3_mesa* sam_u, u3_ship her_u, u3_lane* lan_u) { + u3_noun key =_mesa_lane_key(her_u, lan_u); u3_gage* ret_u = _mesa_get_lane_raw(sam_u, key); u3z(key); return ret_u; @@ -711,9 +713,9 @@ _mesa_get_lane(u3_mesa* sam_u, c3_d her_d[2], u3_lane* lan_u) { * uses same copying trick as _mesa_put_request() */ static void -_mesa_put_lane(u3_mesa* sam_u, c3_d her_d[2], u3_lane* lan_u, u3_gage* gag_u) +_mesa_put_lane(u3_mesa* sam_u, u3_ship her_u, u3_lane* lan_u, u3_gage* gag_u) { - u3_noun key = _mesa_lane_key(her_d, lan_u); + u3_noun key = _mesa_lane_key(her_u, lan_u); u3_gage* old_u = _mesa_get_lane_raw(sam_u, key); u3_gage* new_u = gag_u; @@ -1181,7 +1183,7 @@ _mesa_req_pact_done(u3_pend_req* req_u, _update_resend_timer(req_u); // _mesa_put_request(sam_u, nam_u, req_u); - // _mesa_put_lane(sam_u, nam_u->her_d, lan_u, gag_u); + // _mesa_put_lane(sam_u, nam_u->her_u, lan_u, gag_u); return; } @@ -1399,9 +1401,9 @@ _mesa_ef_send(u3_mesa* sam_u, u3_noun las, u3_noun pac) if ( (PACT_PEEK == hed_u.typ_y) || (PACT_POKE == hed_u.typ_y) ) { u3_mesa_pact pac_u; c3_w res_w = mesa_sift_pact(&pac_u, buf_y, len_w); - c3_d her_d[2]; - _get_her(&pac_u, her_d); - per_u = _mesa_get_peer(sam_u, her_d); + u3_ship her_u; + _get_her(&pac_u, her_u); + per_u = _mesa_get_peer(sam_u, her_u); _mesa_add_our_to_pit(sam_u, &pac_u.pek_u.nam_u); } else { // treat PACT_RESV as an opaque response @@ -1531,6 +1533,7 @@ _init_peer(u3_mesa* sam_u, u3_peer* per_u) per_u->sam_u = sam_u; per_u->ful_o = c3n; per_u->dan_u = (u3_lane){0,0}; + per_u->may_u = (u3_lane){0,0}; _init_lane_state(&per_u->dir_u); per_u->imp_y = 0; _init_lane_state(&per_u->ind_u); @@ -1858,9 +1861,9 @@ _saxo_cb(void* vod_p, u3_noun nun) } static void -_meet_peer(u3_mesa* sam_u, u3_peer* per_u, c3_d her_d[2]) +_meet_peer(u3_mesa* sam_u, u3_peer* per_u, u3_ship her_u) { - u3_noun her = u3i_chubs(2, her_d); + u3_noun her = u3_ship_to_noun(her_u); u3_noun gan = u3nc(u3_nul, u3_nul); u3_noun pax = u3nc(u3dc("scot", c3__p, her), u3_nul); u3_pier_peek_last(sam_u->pir_u, gan, c3__j, c3__saxo, pax, per_u, _saxo_cb); @@ -1897,14 +1900,14 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) memset(req_u, 0, sizeof(u3_pend_req)); } - u3_gage* gag_u = _mesa_get_lane(sam_u, nam_u->her_d, lan_u); + u3_gage* gag_u = _mesa_get_lane(sam_u, nam_u->her_u, lan_u); if ( gag_u == NULL ) { gag_u = alloca(sizeof(u3_gage)); _init_gage(gag_u); // save and re-retrieve so we have persistent pointer - _mesa_put_lane(sam_u, nam_u->her_d, lan_u, gag_u); - gag_u = _mesa_get_lane(sam_u, nam_u->her_d, lan_u); + _mesa_put_lane(sam_u, nam_u->her_u, lan_u, gag_u); + gag_u = _mesa_get_lane(sam_u, nam_u->her_u, lan_u); u3_assert( gag_u != NULL ); } @@ -2003,6 +2006,41 @@ _mesa_request_next_fragments(u3_mesa* sam_u, } } +typedef struct _u3_mesa_lane_cb_data { + u3_ship her_u; + u3_lane lan_u; + u3_peer* per_u; +} u3_mesa_lane_cb_data; + +static void +_mesa_page_news_cb(u3_ovum* egg_u, u3_ovum_news new_e) +{ + if ( u3_ovum_done != new_e ) { + return; + } + u3_mesa_lane_cb_data* dat_u = egg_u->ptr_v; + u3_peer* per_u = dat_u->per_u; + per_u->dan_u.pip_w = per_u->may_u.pip_w; + per_u->dan_u.por_s = per_u->may_u.por_s; + + u3_noun wir = u3nc(c3__ames, u3_nul); + u3_noun cad; + { + u3_noun her = u3_ship_to_noun(dat_u->her_u); + u3_noun lan = u3_mesa_encode_lane(per_u->dan_u); + cad = u3nt(c3__dear, her, lan); + } + u3_auto_plan(&per_u->sam_u->car_u, u3_ovum_init(0, c3__ames, wir, cad)); +} + +static void +_mesa_page_bail_cb(u3_ovum* egg_u, u3_ovum_news new_e) +{ + #ifdef MESA_DEBUG + u3l_log("mesa: arvo page event failed"); + #endif +} + static void _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) { @@ -2017,7 +2055,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) u3_mesa_name* nam_u = &pac_u->pek_u.nam_u; c3_s fra_s; - c3_d* her_d = nam_u->her_d; + c3_d* her_d = nam_u->her_u; c3_o our_o = __( 0 == memcmp(her_d, sam_u->pir_u->who_d, sizeof(*her_d) * 2) ); // forwarding wrong, need a PIT entry @@ -2027,13 +2065,17 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) // return; // } - u3_peer* per_u = _mesa_get_peer(sam_u, nam_u->her_d); + u3_peer* per_u = _mesa_get_peer(sam_u, nam_u->her_u); c3_o new_o = c3n; if ( NULL == per_u ) { new_o = c3y; per_u = c3_calloc(sizeof(u3_peer)); _init_peer(sam_u, per_u); - _meet_peer(sam_u, per_u, nam_u->her_d); + _meet_peer(sam_u, per_u, nam_u->her_u); + } + if ( !pac_u->hed_u.hop_y && !_mesa_lanes_equal(&per_u->dan_u, &lan_u) ) { + per_u->may_u.pip_w = lan_u.pip_w; + per_u->may_u.por_s = lan_u.por_s; } c3_o dir_o = __(pac_u->hed_u.hop_y == 0); @@ -2047,7 +2089,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) //_log_lane(&lan_u); } - _mesa_put_peer(sam_u, nam_u->her_d, per_u); + _mesa_put_peer(sam_u, nam_u->her_u, per_u); u3_weak pin = _mesa_get_pit(sam_u, nam_u); @@ -2099,8 +2141,11 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) } // XX should put in cache on success - u3_auto_plan(&sam_u->car_u, - u3_ovum_init(0, c3__ames, u3nc(c3__ames, u3_nul), cad)); + u3_auto_peer( + u3_auto_plan(&sam_u->car_u, + u3_ovum_init(0, c3__ames, u3nc(c3__ames, u3_nul), cad)), + per_u, _mesa_page_news_cb, _mesa_page_bail_cb); + _mesa_free_pict(pic_u); u3z(pin); return; @@ -2219,16 +2264,18 @@ static void _mesa_forward_request(u3_mesa* sam_u, u3_mesa_pict* pic_u) { u3_mesa_pact* pac_u = &pic_u->pac_u; - c3_d* her_d = pac_u->pek_u.nam_u.her_d; - u3_peer* per_u = _mesa_get_peer(sam_u, her_d); + u3_peer* per_u = _mesa_get_peer(sam_u, pac_u->pek_u.nam_u.her_u); if ( !per_u ) { - // XX leaks - u3l_log("mesa: alien forward for %s", u3r_string(u3dc("scot", c3__p, u3i_chubs(2, her_d)))); + #ifdef MESA_DEBUG + c3_c* mes = u3_ship_to_string(pac_u->pek_u.nam_u.her_u); + u3l_log("mesa: alien forward for %s", mes); + c3_free(mes); + #endif _mesa_free_pict(pic_u); return; } if ( c3y == sam_u->for_o && sam_u->pir_u->who_d[0] == per_u->imp_y ) { - u3_lane lin_u = _mesa_get_direct_lane(sam_u, her_d); + u3_lane lin_u = _mesa_get_direct_lane(sam_u, pac_u->pek_u.nam_u.her_u); //_update_hopcount(&pac_u->hed_u); // TODO reinstate #ifdef MESA_DEBUG u3l_log("mesa: forward_request()"); @@ -2249,8 +2296,7 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) u3_mesa_pact* pac_u = &pic_u->pac_u; u3_mesa* sam_u = pic_u->sam_u; - c3_d* her_d = pac_u->pek_u.nam_u.her_d; - c3_o our_o = __( 0 == memcmp(her_d, sam_u->pir_u->who_d, sizeof(*her_d) * 2) ); + c3_o our_o = u3_ships_equal(pac_u->pek_u.nam_u.her_u, sam_u->pir_u->who_d); if ( c3n == our_o ) { u3l_log(" forwarding\r\n"); @@ -2318,14 +2364,14 @@ _mesa_hear_poke(u3_mesa_pict* pic_u, u3_lane* lan_u) { u3_mesa_pact* pac_u = &pic_u->pac_u; u3_mesa* sam_u = pic_u->sam_u; - c3_d* her_d = pac_u->pek_u.nam_u.her_d; - c3_o our_o = __( 0 == memcmp(her_d, sam_u->pir_u->who_d, sizeof(*her_d) * 2) ); #ifdef MESA_DEBUG u3l_log("mesa: hear_poke()"); u3_assert( PACT_POKE == pac_u->hed_u.typ_y ); #endif + c3_o our_o = u3_ships_equal(pac_u->pek_u.nam_u.her_u, sam_u->pir_u->who_d); + if ( c3n == our_o ) { _mesa_forward_request(sam_u, pic_u); return; @@ -2333,13 +2379,13 @@ _mesa_hear_poke(u3_mesa_pict* pic_u, u3_lane* lan_u) // XX if this lane management stuff is necessary // it should be deferred to after successful event processing - u3_peer* per_u = _mesa_get_peer(sam_u, pac_u->pok_u.pay_u.her_d); + u3_peer* per_u = _mesa_get_peer(sam_u, pac_u->pok_u.pay_u.her_u); c3_o new_o = c3n; if ( NULL == per_u ) { new_o = c3y; per_u = c3_calloc(sizeof(u3_peer)); _init_peer(sam_u, per_u); - _meet_peer(sam_u, per_u, pac_u->pok_u.pay_u.her_d); + _meet_peer(sam_u, per_u, pac_u->pok_u.pay_u.her_u); } c3_o dir_o = __(pac_u->hed_u.hop_y == 0); @@ -2354,7 +2400,7 @@ _mesa_hear_poke(u3_mesa_pict* pic_u, u3_lane* lan_u) // u3l_log("new lane is direct %c", c3y == dir_o ? 'y' : 'n'); // _log_lane(lan_u); } - _mesa_put_peer(sam_u, pac_u->pok_u.pay_u.her_d, per_u); + _mesa_put_peer(sam_u, pac_u->pok_u.pay_u.her_u, per_u); u3_ovum_peer nes_f; u3_ovum_bail bal_f; @@ -2373,22 +2419,17 @@ _mesa_hear_poke(u3_mesa_pict* pic_u, u3_lane* lan_u) cad = u3nt(c3__heer, lan, u3i_slab_mint(&sab_u)); } + u3_ovum* ovo = u3_ovum_init(0, c3__ames, wir, cad); + ovo = u3_auto_plan(&sam_u->car_u, ovo); + if ( 1 == pac_u->pok_u.dat_u.tot_w ) { - nes_f = bal_f = ptr_v = NULL; _mesa_free_pict(pic_u); } else { - assert(pac_u->pok_u.dat_u.tot_w); // XX check request state for *payload* (in-progress duplicate) - nes_f = _mesa_poke_news_cb; - bal_f = _mesa_poke_bail_cb; - ptr_v = pic_u; + assert(pac_u->pok_u.dat_u.tot_w); + u3_auto_peer(ovo, pic_u, _mesa_poke_news_cb, _mesa_poke_bail_cb); } - - u3_auto_peer( - u3_auto_plan(&sam_u->car_u, - u3_ovum_init(0, c3__ames, wir, cad)), - ptr_v, nes_f, bal_f); } void diff --git a/pkg/vere/io/mesa/mesa.h b/pkg/vere/io/mesa/mesa.h index 1d5f0cfe76..4ca7fe57c5 100644 --- a/pkg/vere/io/mesa/mesa.h +++ b/pkg/vere/io/mesa/mesa.h @@ -2,6 +2,7 @@ #define VERE_MESA_H #include "c3.h" +#include "ship.h" #define MESA_VER 1 #define FINE_PAGE 4096 // packets per page @@ -42,7 +43,7 @@ typedef struct _u3_mesa_name_meta { typedef struct _u3_mesa_name { // u3_mesa_name_meta met_u; - c3_d her_d[2]; + u3_ship her_u; c3_w rif_w; c3_y boq_y; c3_o nit_o; diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index 439f5c382e..6ff4bf038a 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -4,6 +4,7 @@ #include "vere.h" #include "ivory.h" #include "ur.h" +#include "ship.h" #define RED_TEXT "\033[0;31m" #define DEF_TEXT "\033[0m" // endif tests @@ -13,7 +14,6 @@ #define safe_dec(num) (num == 0 ? num : num - 1) #define _mesa_met3_w(a_w) ((c3_bits_word(a_w) + 0x7) >> 3) - /* Logging functions */ @@ -51,7 +51,7 @@ log_name(u3_mesa_name* nam_u) c3_c* her_c; { - u3_noun her = u3dc("scot", c3__p, u3i_chubs(2, nam_u->her_d)); + u3_noun her = u3dc("scot", c3__p, u3_ship_to_noun(nam_u->her_u)); her_c = u3r_string(her); u3z(her); } @@ -182,20 +182,16 @@ update_hopcount(u3_mesa_head* hed_u) // TODO rename, _inc_hopcount()? } static c3_y -_mesa_rank(c3_d who_d[2]) +_mesa_rank(u3_ship who_u) { - if ( who_d[1] ) { - return 3; - } - else if ( who_d[0] >> 32 ) { - return 2; - } - else if ( who_d[0] >> 16 ) { - return 1; - } - else { - return 0; - } + switch ( u3_ship_rank(who_u) ) { + case c3__pawn: return 3; + case c3__earl: return 2; + case c3__duke: return 1; + case c3__king: return 0; + case c3__czar: return 0; + default: u3_assert(!"unreachable"); + }; } /* lifecycle @@ -237,65 +233,12 @@ _ames_etch_word(c3_y buf_y[4], c3_w wod_w) buf_y[3] = (wod_w >> 24) & 0xff; } - - -/* _ames_chub_bytes(): c3_d to c3_y[8] -** XX factor out, deduplicate with other conversions -*/ -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; -} - -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)); -} - -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; -} - static inline c3_w _ames_sift_word(c3_y buf_y[4]) { return (buf_y[3] << 24 | buf_y[2] << 16 | buf_y[1] << 8 | buf_y[0]); } - -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); -} - c3_o mesa_sift_head(c3_y buf_y[8], u3_mesa_head* hed_u) { @@ -340,7 +283,7 @@ _mesa_sift_name(u3_mesa_name* nam_u, c3_y* buf_y, c3_w len_w) c3_y her_y = 2 << met_u.ran_y; CHECK_BOUNDS(cur_w + her_y) - _ames_ship_to_chubs(nam_u->her_d, her_y, buf_y + cur_w); + u3_ship_of_bytes(nam_u->her_u, her_y, buf_y + cur_w); cur_w += her_y; c3_y rif_y = met_u.rif_y + 1; @@ -649,7 +592,7 @@ _mesa_etch_name(c3_y* buf_y, u3_mesa_name* nam_u) c3_w cur_w = 0; u3_mesa_name_meta met_u; - met_u.ran_y = _mesa_rank(nam_u->her_d); + met_u.ran_y = _mesa_rank(nam_u->her_u); met_u.rif_y = safe_dec(_mesa_met3_w(nam_u->rif_w)); if ( c3y == nam_u->nit_o ) { @@ -675,7 +618,7 @@ _mesa_etch_name(c3_y* buf_y, u3_mesa_name* nam_u) //ship cur_w++; c3_y her_y = 2 << met_u.ran_y; // XX confirm - _ames_ship_of_chubs(nam_u->her_d, her_y, buf_y + cur_w); + u3_ship_to_bytes(nam_u->her_u, her_y, buf_y + cur_w); cur_w += her_y; // rift @@ -818,7 +761,7 @@ _mesa_size_name(u3_mesa_name* nam_u) c3_w siz_w = 1; u3_mesa_name_meta met_u; - met_u.ran_y = _mesa_rank(nam_u->her_d); + met_u.ran_y = _mesa_rank(nam_u->her_u); met_u.rif_y = safe_dec(_mesa_met3_w(nam_u->rif_w)); siz_w += 2 << met_u.ran_y; @@ -1064,7 +1007,7 @@ _test_cmp_name(u3_mesa_name* hav_u, u3_mesa_name* ned_u) { c3_i ret_i = 0; - cmp_buffer(her_d, sizeof(ned_u->her_d), "name: ships differ"); + cmp_buffer(her_d, sizeof(ned_u->her_u), "name: ships differ"); cmp_scalar(rif_w, "name: rifts", "%u"); cmp_scalar(boq_y, "name: bloqs", "%u"); @@ -1300,7 +1243,7 @@ _test_make_head(void* ptr_v, u3_mesa_head* hed_u) static void _test_make_name(void* ptr_v, c3_s pat_s, u3_mesa_name* nam_u) { - _test_rand_bytes(ptr_v, 16, (c3_y*)nam_u->her_d); + _test_rand_bytes(ptr_v, 16, (c3_y*)nam_u->her_u); nam_u->rif_w = _test_rand_word(ptr_v); nam_u->pat_s = _test_rand_gulf_w(ptr_v, pat_s); @@ -1416,7 +1359,7 @@ _test_sift_page() { u3_noun her = u3v_wish("~hastuc-dibtux"); - u3r_chubs(0, 2, nam_u->her_d, her); + u3r_chubs(0, 2, nam_u->her_u, her); u3z(her); } nam_u->rif_w = 15; From d616041f5a96f76f205628869a2ab819ca56f84a Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Fri, 19 Jul 2024 16:25:06 -0400 Subject: [PATCH 253/430] noun: actually add the ship library files, jeez --- pkg/noun/ship.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++++ pkg/noun/ship.h | 54 +++++++++++++++++++++++++ 2 files changed, 156 insertions(+) create mode 100644 pkg/noun/ship.c create mode 100644 pkg/noun/ship.h diff --git a/pkg/noun/ship.c b/pkg/noun/ship.c new file mode 100644 index 0000000000..85684c3de9 --- /dev/null +++ b/pkg/noun/ship.c @@ -0,0 +1,102 @@ +#include "c3.h" +#include "types.h" +#include "imprison.h" +#include "retrieve.h" +#include "vortex.h" +#include "ship.h" + +static inline void +_s_chub_to_bytes(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; +} + +static inline c3_d +_s_bytes_to_chub(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; +} + +void +u3_ship_to_bytes(u3_ship who_u, c3_y len_y, c3_y* buf_y) +{ + c3_y sip_y[16] = {0}; + + _s_chub_to_bytes(sip_y, who_u[0]); + _s_chub_to_bytes(sip_y + 8, who_u[1]); + + memcpy(buf_y, sip_y, c3_min(16, len_y)); +} + +void +u3_ship_of_bytes(u3_ship who_u, c3_y len_y, c3_y* buf_y) +{ + c3_y sip_y[16] = {0}; + memcpy(sip_y, buf_y, c3_min(16, len_y)); + + who_u[0] = _s_bytes_to_chub(sip_y); + who_u[1] = _s_bytes_to_chub(sip_y + 8); +} + +u3_atom +u3_ship_to_noun(u3_ship who_u) +{ + return u3i_chubs(2, who_u); +} + +c3_c* +u3_ship_to_string(u3_ship who_u) +{ + u3_noun ser = u3dc("scot", c3__p, u3_ship_to_noun(who_u)); + c3_c* who_c = u3r_string(ser); + u3z(ser); + return who_c; +} + +void +u3_ship_of_noun(u3_ship who_u, u3_noun who) +{ + u3r_chubs(0, 2, who_u, who); +} + +c3_o +u3_ships_equal(u3_ship sip_u, u3_ship sap_u) +{ + return __((sip_u[0] == sap_u[0]) && (sip_u[1] == sap_u[1])); +} + +c3_l +u3_ship_rank(u3_ship who_u) +{ + if ( who_u[1] ) return c3__pawn; + else if ( who_u[0] >> 32 ) return c3__earl; + else if ( who_u[0] >> 16 ) return c3__duke; + else if ( who_u[0] >> 8 ) return c3__king; + else return c3__czar; +} + +c3_y +u3_ship_czar(u3_ship who_u) { return who_u[0] & 0xFF; } + +c3_s +u3_ship_king(u3_ship who_u) { return who_u[0] & 0xffff; } + +c3_w +u3_ship_duke(u3_ship who_u) { return who_u[0] & 0xffffffff; } + +c3_d +u3_ship_earl(u3_ship who_u) { return who_u[0]; } \ No newline at end of file diff --git a/pkg/noun/ship.h b/pkg/noun/ship.h new file mode 100644 index 0000000000..5ddabd1021 --- /dev/null +++ b/pkg/noun/ship.h @@ -0,0 +1,54 @@ +#ifndef U3_SHIP_H +#define U3_SHIP_H + +#include "c3.h" +#include "types.h" + +typedef c3_d u3_ship[2]; + +void +u3_ship_to_bytes(c3_d sip_d[2], c3_y len_y, c3_y* buf_y); + +void +u3_ship_of_bytes(u3_ship who_u, c3_y len_y, c3_y* buf_y); + +u3_atom +u3_ship_to_noun(u3_ship who_u); + +void +u3_ship_of_noun(u3_ship who_u, u3_noun who); + +c3_c* +u3_ship_to_string(u3_ship who_u); + +c3_o +u3_ships_equal(u3_ship sip_u, u3_ship sap_u); + +c3_l +u3_ship_rank(u3_ship who_u); + +/** +* Returns a ship's galaxy byte prefix. +*/ +c3_y +u3_ship_czar(u3_ship who_u); + +/** +* Returns a ship's star prefix. +*/ +c3_s +u3_ship_king(u3_ship who_u); + +/** +* Returns a ship's planet prefix. +*/ +c3_w +u3_ship_duke(u3_ship who_u); + +/** +* Returns a ship's moon prefix. +*/ +c3_d +u3_ship_earl(u3_ship who_u); + +#endif /* ifndef U3_SHIP_H */ \ No newline at end of file From 5ab79d0d51bca75454e3c341490d4c1a0b88ab25 Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Fri, 19 Jul 2024 16:27:43 -0400 Subject: [PATCH 254/430] gitignore MODULE.bazel and lock --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index da5bf8e6c8..fe7281c2cc 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,8 @@ /.user.bazelrc /bazel-* /urbit +MODULE.bazel +MODULE.bazel.lock # Swap files. *.swo From 8a4f086b3918581667fb49ceb3881b2773d55700 Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Fri, 19 Jul 2024 16:53:59 -0400 Subject: [PATCH 255/430] mesa: inject %dear on page success in the single-fragment case --- pkg/vere/io/mesa.c | 56 +++++++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index c240da628c..584c41b9df 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -166,7 +166,6 @@ typedef struct _u3_peer { u3_mesa* sam_u; // backpointer c3_o ful_o; // has this been initialized? u3_lane dan_u; // direct lane (nullable) - u3_lane may_u; // provisional direct lane (nullable) u3_lane_state dir_u; // direct lane state c3_y imp_y; // galaxy @p u3_lane_state ind_u; // indirect lane state @@ -1533,7 +1532,6 @@ _init_peer(u3_mesa* sam_u, u3_peer* per_u) per_u->sam_u = sam_u; per_u->ful_o = c3n; per_u->dan_u = (u3_lane){0,0}; - per_u->may_u = (u3_lane){0,0}; _init_lane_state(&per_u->dir_u); per_u->imp_y = 0; _init_lane_state(&per_u->ind_u); @@ -1652,11 +1650,11 @@ _mesa_get_pit(u3_mesa* sam_u, u3_mesa_name* nam_u) /* ld: Undefined symbols: _log_name, referenced from: - __mesa_put_pit in - _log_pact in - _log_pact in - _log_pact in - _log_pact in + __mesa_put_pit in + _log_pact in + _log_pact in + _log_pact in + _log_pact in clang: error: linker command failed with exit code 1 (use -v to see invocation) Target //pkg/vere:urbit failed to build */ @@ -2016,12 +2014,20 @@ static void _mesa_page_news_cb(u3_ovum* egg_u, u3_ovum_news new_e) { if ( u3_ovum_done != new_e ) { + #ifdef MESA_DEBUG + u3l_log("mesa: arvo page event was not a success"); + #endif return; } u3_mesa_lane_cb_data* dat_u = egg_u->ptr_v; u3_peer* per_u = dat_u->per_u; - per_u->dan_u.pip_w = per_u->may_u.pip_w; - per_u->dan_u.por_s = per_u->may_u.por_s; + + #ifdef MESA_DEBUG + c3_c* her_c = u3_ship_to_string(dat_u->her_u); + u3l_log("mesa: %%dear %s", her_c); + _log_lane(&dat_u->lan_u); + c3_free(her_c); + #endif u3_noun wir = u3nc(c3__ames, u3_nul); u3_noun cad; @@ -2031,6 +2037,8 @@ _mesa_page_news_cb(u3_ovum* egg_u, u3_ovum_news new_e) cad = u3nt(c3__dear, her, lan); } u3_auto_plan(&per_u->sam_u->car_u, u3_ovum_init(0, c3__ames, wir, cad)); + + c3_free(dat_u); } static void @@ -2039,6 +2047,7 @@ _mesa_page_bail_cb(u3_ovum* egg_u, u3_ovum_news new_e) #ifdef MESA_DEBUG u3l_log("mesa: arvo page event failed"); #endif + c3_free(egg_u->ptr_v); } static void @@ -2073,10 +2082,6 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) _init_peer(sam_u, per_u); _meet_peer(sam_u, per_u, nam_u->her_u); } - if ( !pac_u->hed_u.hop_y && !_mesa_lanes_equal(&per_u->dan_u, &lan_u) ) { - per_u->may_u.pip_w = lan_u.pip_w; - per_u->may_u.por_s = lan_u.por_s; - } c3_o dir_o = __(pac_u->hed_u.hop_y == 0); if ( pac_u->hed_u.hop_y == 0 ) { @@ -2094,7 +2099,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) u3_weak pin = _mesa_get_pit(sam_u, nam_u); if ( u3_none == pin ) { - #ifdef MESA_DEBUG + #ifdef MESA_DEBUG u3l_log(" no PIT entry"); #endif return; @@ -2140,11 +2145,22 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) cad = u3nt(c3__heer, lan, u3i_slab_mint(&sab_u)); } - // XX should put in cache on success - u3_auto_peer( - u3_auto_plan(&sam_u->car_u, - u3_ovum_init(0, c3__ames, u3nc(c3__ames, u3_nul), cad)), - per_u, _mesa_page_news_cb, _mesa_page_bail_cb); + u3_noun wir = u3nc(c3__ames, u3_nul); + + u3_ovum* ovo = u3_ovum_init(0, c3__ames, wir, cad); + ovo = u3_auto_plan(&sam_u->car_u, ovo); + + if ( !pac_u->hed_u.hop_y && !_mesa_lanes_equal(&per_u->dan_u, &lan_u) ) { + // XX should put in cache on success + u3_mesa_lane_cb_data* dat_u = c3_malloc(sizeof(u3_mesa_lane_cb_data)); + { + memcpy(dat_u->her_u, nam_u->her_u, 16); + dat_u->lan_u.pip_w = lan_u.pip_w; + dat_u->lan_u.por_s = lan_u.por_s; + dat_u->per_u = per_u; + } + u3_auto_peer(ovo, dat_u, _mesa_page_news_cb, _mesa_page_bail_cb); + } _mesa_free_pict(pic_u); u3z(pin); @@ -2421,7 +2437,7 @@ _mesa_hear_poke(u3_mesa_pict* pic_u, u3_lane* lan_u) u3_ovum* ovo = u3_ovum_init(0, c3__ames, wir, cad); ovo = u3_auto_plan(&sam_u->car_u, ovo); - + if ( 1 == pac_u->pok_u.dat_u.tot_w ) { _mesa_free_pict(pic_u); } From b11be88171ec3cfdbe84684f7938afd2c45b1e8f Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Fri, 19 Jul 2024 17:18:34 -0400 Subject: [PATCH 256/430] mesa: inject %dear on successful LSS packet processing --- pkg/vere/io/mesa.c | 53 +++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 584c41b9df..fabaebdb51 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1071,6 +1071,19 @@ _mesa_burn_misorder_queue(u3_pend_req* req_u) static void _init_lane_state(u3_lane_state*); +static u3_ovum* +_mesa_dear(u3_auto* car_u, u3_ship her_u, u3_lane lan_u) +{ + u3_noun wir = u3nc(c3__ames, u3_nul); + u3_noun cad; + { + u3_noun her = u3_ship_to_noun(her_u); + u3_noun lan = u3_mesa_encode_lane(lan_u); + cad = u3nt(c3__dear, her, lan); + } + return u3_auto_plan(car_u, u3_ovum_init(0, c3__ames, wir, cad)); +} + /* _mesa_req_pact_done(): mark packet as done */ static void @@ -1081,24 +1094,6 @@ _mesa_req_pact_done(u3_pend_req* req_u, u3_lane lan_u) { u3_mesa* sam_u = req_u->per_u->sam_u; // needed for the MESA_LOG macro - c3_d now_d = _get_now_micros(); - - u3_lane_state* sat_u; - if ( 0 == hop_y ) { - c3_i new_i = ( - (lan_u.pip_w != req_u->per_u->dan_u.pip_w) || - (lan_u.por_s != req_u->per_u->dan_u.por_s) - ); - req_u->per_u->dan_u = lan_u; - sat_u = &req_u->per_u->dir_u; - if ( new_i ) { - _init_lane_state(sat_u); - } - } - else { - sat_u = &req_u->per_u->ind_u; - } - sat_u->her_d = now_d; // received past the end of the message if ( dat_u->tot_w <= nam_u->fra_w ) { @@ -1172,6 +1167,19 @@ _mesa_req_pact_done(u3_pend_req* req_u, c3_free(par_u); } + u3_lane_state* sat_u; + if ( 0 == hop_y && (c3n == _mesa_lanes_equal(&lan_u, &req_u->per_u->dan_u)) ) { + req_u->per_u->dan_u = lan_u; + sat_u = &req_u->per_u->dir_u; + _init_lane_state(sat_u); + + _mesa_dear(&sam_u->car_u, nam_u->her_u, lan_u); + } + else { + sat_u = &req_u->per_u->ind_u; + } + sat_u->her_d = _get_now_micros(); + // handle gauge update _mesa_handle_ack(req_u->gag_u, &req_u->wat_u[nam_u->fra_w]); @@ -2029,14 +2037,7 @@ _mesa_page_news_cb(u3_ovum* egg_u, u3_ovum_news new_e) c3_free(her_c); #endif - u3_noun wir = u3nc(c3__ames, u3_nul); - u3_noun cad; - { - u3_noun her = u3_ship_to_noun(dat_u->her_u); - u3_noun lan = u3_mesa_encode_lane(per_u->dan_u); - cad = u3nt(c3__dear, her, lan); - } - u3_auto_plan(&per_u->sam_u->car_u, u3_ovum_init(0, c3__ames, wir, cad)); + _mesa_dear(&per_u->sam_u->car_u, dat_u->her_u, per_u->dan_u); c3_free(dat_u); } From 1054a980cd9dccccbbdf8f1076e2361bc41b3e2b Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Mon, 22 Jul 2024 09:12:44 -0400 Subject: [PATCH 257/430] mesa: add lane to pit when forwarding requests --- pkg/vere/io/mesa.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index fabaebdb51..7a35368960 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -2278,7 +2278,7 @@ _mesa_add_our_to_pit(u3_mesa* sam_u, u3_mesa_name* nam_u) // XX forwarding wrong, need a PIT entry static void -_mesa_forward_request(u3_mesa* sam_u, u3_mesa_pict* pic_u) +_mesa_forward_request(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane lan_u) { u3_mesa_pact* pac_u = &pic_u->pac_u; u3_peer* per_u = _mesa_get_peer(sam_u, pac_u->pek_u.nam_u.her_u); @@ -2293,11 +2293,17 @@ _mesa_forward_request(u3_mesa* sam_u, u3_mesa_pict* pic_u) } if ( c3y == sam_u->for_o && sam_u->pir_u->who_d[0] == per_u->imp_y ) { u3_lane lin_u = _mesa_get_direct_lane(sam_u, pac_u->pek_u.nam_u.her_u); + u3_lane zer_u = {0, 0}; + if ( _mesa_lanes_equal(&zer_u, &lin_u)) { + _mesa_free_pict(pic_u); + return; + } //_update_hopcount(&pac_u->hed_u); // TODO reinstate #ifdef MESA_DEBUG u3l_log("mesa: forward_request()"); log_pact(pac_u); #endif + _mesa_add_lane_to_pit(sam_u, &pac_u->pek_u.nam_u, lan_u); _mesa_send(pic_u, &lin_u); } _mesa_free_pict(pic_u); @@ -2317,7 +2323,7 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) if ( c3n == our_o ) { u3l_log(" forwarding\r\n"); - _mesa_forward_request(sam_u, pic_u); + _mesa_forward_request(sam_u, pic_u, lan_u); return; } // record interest @@ -2390,7 +2396,7 @@ _mesa_hear_poke(u3_mesa_pict* pic_u, u3_lane* lan_u) c3_o our_o = u3_ships_equal(pac_u->pek_u.nam_u.her_u, sam_u->pir_u->who_d); if ( c3n == our_o ) { - _mesa_forward_request(sam_u, pic_u); + _mesa_forward_request(sam_u, pic_u, *lan_u); return; } From 8b6a86bc6626e35fc8cc48962a52111ce2b4e6c7 Mon Sep 17 00:00:00 2001 From: midden-fabler Date: Mon, 22 Jul 2024 13:55:15 -0400 Subject: [PATCH 258/430] http: cleanup, notes from review --- pkg/vere/io/http.c | 80 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 56 insertions(+), 24 deletions(-) diff --git a/pkg/vere/io/http.c b/pkg/vere/io/http.c index 210b657802..39bbbe5538 100644 --- a/pkg/vere/io/http.c +++ b/pkg/vere/io/http.c @@ -671,11 +671,12 @@ _content_headers(c3_z beg_z, c3_z end_z, c3_w tot_w) return out; } -/* _http_foo_cb() +/* _http_scry_cb() */ static void -_http_foo_cb(void* vod_p, u3_noun nun) +_http_scry_cb(void* vod_p, u3_noun nun) { + // XX slice here u3_preq* peq_u = vod_p; u3_httd* htd_u = peq_u->htd_u; u3_hreq* req_u = peq_u->req_u; @@ -684,8 +685,16 @@ _http_foo_cb(void* vod_p, u3_noun nun) u3_assert(u3_rsat_peek == req_u->sat_e); req_u->peq_u = 0; if ( u3_nul != nun ) { - u3_atom len = u3r_at(254, nun); - u3_noun hez = _content_headers(0, (len - 1), len); + u3_noun len = u3r_at(254, nun); + c3_w len_w; + if ( c3n == u3r_safe_word(len, &len_w) ) { + u3z(nun); + _http_scry_respond(req_u, u3_nul); + u3z(peq_u->pax); + c3_free(peq_u); + return; + } + u3_noun hez = _content_headers(0, (len_w - 1), len_w); u3_noun res = u3i_edit(u3k(nun), 125, u3qb_weld(hez, u3r_at(125, nun))); _http_scry_respond(req_u, u3k(res)); } @@ -698,6 +707,8 @@ _http_foo_cb(void* vod_p, u3_noun nun) if ( (c3n == peq_u->las_o) && (u3_nul != nun) ) { + // XX pair of auth & path for key + // check ~watter-parter's u3h_put(htd_u->nax_p, peq_u->pax, nun); } u3z(peq_u->pax); @@ -712,20 +723,25 @@ typedef struct _range_request { typedef struct _content { c3_z beg_z; c3_z end_z; - u3_noun dat; + u3_noun dat; // XX free } content; +/* _slice_mime: given a valid range, slice a section of octs +*/ static content _slice_mime(range_request rng, u3_noun octs) { - c3_w len_w = u3h(octs); - c3_w oct_w = u3t(octs); content out; - out.beg_z = SIZE_MAX; out.end_z = SIZE_MAX; out.dat = u3_nul; + u3_noun len = u3h(octs); + c3_w len_w; + if ( c3n == u3r_safe_word(len, &len_w) ) { + return out; + } + if ( (SIZE_MAX == rng.beg_z) && (SIZE_MAX == rng.end_z) ) { @@ -754,11 +770,13 @@ _slice_mime(range_request rng, u3_noun octs) && (out.beg_z <= out.end_z) ) { out.dat = u3nc((out.end_z - out.beg_z) + 1, - u3qc_cut(3, out.beg_z, (out.end_z + 1) - out.beg_z, oct_w)); + u3qc_cut(3, out.beg_z, (out.end_z + 1) - out.beg_z, u3t(octs))); } return out; } +/* _parse_range: get a range from '-' delimited text +*/ static range_request _parse_range(c3_c* txt_c, c3_w len_w) { @@ -809,13 +827,15 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) : u3nc(u3i_string("request"), dat); } + // check if base url starts with '/_~_/' if ( (len_w < 6) || (0 != memcmp("/_~_/", bas_c, 5)) ) { - // inject to arvo + // no: inject to arvo u3_auto_plan(&htd_u->car_u, u3_ovum_init(0, c3__e, wir, cad)); } else { + // '/_~_/' found bas_c = bas_c + 4; // retain '/' after /_~_ len_w = len_w - 4; @@ -844,6 +864,7 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) u3_noun des; u3_noun cas; + // XX move to function // get beak from path // for ( c3_w i_w = 0; i_w < 3; ++i_w ) { @@ -907,17 +928,19 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) return; } else { - c3_d len_d = nex_c - bas_c; - *wer = u3i_bytes(len_d, (const c3_y*)bas_c); + c3_w dif_w = (c3_p)(nex_c - bas_c); + *wer = u3i_bytes(dif_w, (const c3_y*)bas_c); bas_c = nex_c; - len_w = len_w - len_d; + len_w = len_w - dif_w; } } } u3_noun spur = u3dc("rush", u3i_bytes(len_w, (const c3_y*)bas_c), u3v_wish("stap")); - if ( (who != our) || (u3_nul == spur) ) { + if ( (u3_nul == spur) + || (c3n == u3r_sing(our, who)) ) + { c3_c* msg_c = "bad scry path"; h2o_send_error_generic(req_u->rec_u, 400, msg_c, msg_c, 0); return; @@ -927,7 +950,7 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) spur = u3nc(u3i_string("mime"), u3t(spur)); if ( c3y == req_u->peq_u->las_o ) { u3_pier_peek_last(htd_u->car_u.pir_u, gang, c3__ex, - des, spur, req_u->peq_u, _http_foo_cb); + des, spur, req_u->peq_u, _http_scry_cb); } else { @@ -939,13 +962,13 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) || ((u3_nul == gang) && (c3y == u3r_at(14, nac))) ) { // maybe cache, then serve subsequent range requests from cache - req_u->peq_u->las_o = c3n; - req_u->peq_u->pax = u3k(bem); - u3_pier_peek(htd_u->car_u.pir_u, gang, u3k(u3nt(0, c3__ex, bem)), - req_u->peq_u, _http_foo_cb); + req_u->peq_u->pax = bem; + u3_pier_peek(htd_u->car_u.pir_u, gang, u3nt(0, c3__ex, u3k(bem)), + req_u->peq_u, _http_scry_cb); } else { + // XX function h2o_headers_t req_headers = req_u->rec_u->headers; c3_w idx = h2o_find_header(&req_headers, H2O_TOKEN_RANGE, -1); @@ -959,27 +982,35 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) if ( 0 == rest_len ) { c3_c* msg_c = "Requested Range Not Satisfiable"; h2o_send_error_generic(req_u->rec_u, 416, msg_c, msg_c, 0); + // XX leaks return; } range_request rng_req = _parse_range(req_headers.entries[idx].value.base + 6, rest_len); u3_noun octs = u3r_at(127, nac); if ( u3_none == octs ) { - c3_c* msg_c = "Requested Range Not Satisfiable"; - h2o_send_error_generic(req_u->rec_u, 416, msg_c, msg_c, 0); + h2o_send_error_500(rec_u, "Internal Server Error", "scry failed", 0); return; } content result = _slice_mime(rng_req, octs); if ( u3_nul == result.dat ) { c3_c* msg_c = "Requested Range Not Satisfiable"; + u3z(result.dat); h2o_send_error_generic(req_u->rec_u, 416, msg_c, msg_c, 0); return; } - if ( u3r_sing(result.dat, octs) == c3y) { + if ( c3y == u3r_sing(result.dat, octs) ) { // 200 - u3_atom len = u3r_at(254, nac); - u3_noun hez = _content_headers(0, (len - 1), len); + u3z(result.dat); + u3_noun len = u3r_at(254, nac); + c3_w len_w; + if ( c3n == u3r_safe_word(len, &len_w) ) { + u3z(nac); + _http_scry_respond(req_u, u3_nul); + return; + } + u3_noun hez = _content_headers(0, (len_w - 1), len_w); u3_noun res = u3i_edit(nac, 125, u3qb_weld(hez, u3r_at(125, nac))); _http_cache_respond(req_u, res); } @@ -1102,6 +1133,7 @@ _http_cache_scry_cb(void* vod_p, u3_noun nun) _http_cache_respond(req_u, u3k(nun)); } + // XX pair of auth & path for key u3h_put(htd_u->nax_p, peq_u->pax, nun); u3z(peq_u->pax); c3_free(peq_u); From 529f399a1ac1535767eb22ce0fe4328a2cc68b70 Mon Sep 17 00:00:00 2001 From: Sigilante Date: Mon, 22 Jul 2024 16:33:39 -0500 Subject: [PATCH 259/430] Post with i754 mote. --- pkg/c3/motes.h | 1 + pkg/noun/jets/i/lagoon.c | 468 +++++++++++++++++++-------------------- pkg/noun/jets/q.h | 52 ++--- 3 files changed, 258 insertions(+), 263 deletions(-) diff --git a/pkg/c3/motes.h b/pkg/c3/motes.h index 277761185f..6c1217dc2f 100644 --- a/pkg/c3/motes.h +++ b/pkg/c3/motes.h @@ -613,6 +613,7 @@ # define c3__is c3_s2('i','s') # define c3__item c3_s4('i','t','e','m') # define c3__ix c3_s2('i','x') +# define c3__i754 c3_s4('i','7','5','4') # define c3__j c3_s1('j') # define c3__jack c3_s4('j','a','c','k') # define c3__jam c3_s3('j','a','m') diff --git a/pkg/noun/jets/i/lagoon.c b/pkg/noun/jets/i/lagoon.c index 460058ecc7..9ead70d14a 100644 --- a/pkg/noun/jets/i/lagoon.c +++ b/pkg/noun/jets/i/lagoon.c @@ -30,6 +30,16 @@ c3_d c[2]; }; +/* soft check on u3_none return from q jet +*/ + static inline u3_noun _soft_run(u3_noun a) + { + if (u3_none == a) { + u3m_bail(c3__fail); + } + return a; + } + // $?(%n %u %d %z %a) static inline void _set_rounding(c3_w a) @@ -98,20 +108,10 @@ return dims; } -/* soft check on u3_none return from q jet -*/ - static inline u3_noun _soft_run(u3_noun a) - { - if (u3_none == a) { - u3m_bail(c3__fail); - } - return a; - } - /* add - axpy = 1*x+y */ u3_noun - u3qi_la_add_real(u3_noun x_data, + u3qi_la_add_i754(u3_noun x_data, u3_noun y_data, u3_noun shape, u3_noun bloq @@ -121,6 +121,7 @@ if (bloq < 4 || bloq > 7) { return u3_none; } + fprintf(stderr, ">>> u3qi_la_add\n\r"); // Unpack the data as a byte array. We assume total length < 2**64. // len_x is length in base units @@ -135,8 +136,7 @@ // y_bytes is the data array (w/ leading 0x1, skipped by ?axpy) c3_y* y_bytes = (c3_y*)u3a_malloc((syz_x+1)*sizeof(c3_y)); - u3r_bytes(0, syz_x, y_bytes, y_data); - y_bytes[syz_x] = 0x1; + u3r_bytes(0, syz_x+1, y_bytes, y_data); // Switch on the block size. switch (u3x_atom(bloq)) { @@ -170,7 +170,7 @@ /* sub - axpy = -1*y+x */ u3_noun - u3qi_la_sub_real(u3_noun x_data, + u3qi_la_sub_i754(u3_noun x_data, u3_noun y_data, u3_noun shape, u3_noun bloq @@ -194,9 +194,8 @@ // y_bytes is the data array (w/ leading 0x1, skipped by ?axpy) c3_y* y_bytes = (c3_y*)u3a_malloc((syz_x+1)*sizeof(c3_y)); - u3r_bytes(0, syz_x, y_bytes, y_data); - y_bytes[syz_x] = 0x1; - + u3r_bytes(0, syz_x+1, y_bytes, y_data); + // Switch on the block size. switch (u3x_atom(bloq)) { case 4: @@ -231,7 +230,7 @@ elementwise multiplication */ u3_noun - u3qi_la_mul_real(u3_noun x_data, + u3qi_la_mul_i754(u3_noun x_data, u3_noun y_data, u3_noun shape, u3_noun bloq) @@ -254,8 +253,7 @@ // y_bytes is the data array (w/ leading 0x1, skipped by ?axpy) c3_y* y_bytes = (c3_y*)u3a_malloc((syz_x+1)*sizeof(c3_y)); - u3r_bytes(0, syz_x, y_bytes, y_data); - y_bytes[syz_x] = 0x1; + u3r_bytes(0, syz_x+1, y_bytes, y_data); // Switch on the block size. switch (u3x_atom(bloq)) { @@ -298,7 +296,7 @@ elementwise division */ u3_noun - u3qi_la_div_real(u3_noun x_data, + u3qi_la_div_i754(u3_noun x_data, u3_noun y_data, u3_noun shape, u3_noun bloq) @@ -321,8 +319,7 @@ // y_bytes is the data array (w/ leading 0x1, skipped by ?axpy) c3_y* y_bytes = (c3_y*)u3a_malloc((syz_x+1)*sizeof(c3_y)); - u3r_bytes(0, syz_x, y_bytes, y_data); - y_bytes[syz_x] = 0x1; + u3r_bytes(0, syz_x+1, y_bytes, y_data); // Switch on the block size. switch (u3x_atom(bloq)) { @@ -365,7 +362,7 @@ remainder after division */ u3_noun - u3qi_la_mod_real(u3_noun x_data, + u3qi_la_mod_i754(u3_noun x_data, u3_noun y_data, u3_noun shape, u3_noun bloq) @@ -388,8 +385,7 @@ // y_bytes is the data array (w/ leading 0x1, skipped by ?axpy) c3_y* y_bytes = (c3_y*)u3a_malloc((syz_x+1)*sizeof(c3_y)); - u3r_bytes(0, syz_x, y_bytes, y_data); - y_bytes[syz_x] = 0x1; + u3r_bytes(0, syz_x+1, y_bytes, y_data); // Switch on the block size. switch (u3x_atom(bloq)) { @@ -474,7 +470,7 @@ /* cumsum - x[0] + x[1] + ... x[n] */ u3_noun - u3qi_la_cumsum_real(u3_noun x_data, + u3qi_la_cumsum_i754(u3_noun x_data, u3_noun shape, u3_noun bloq) { @@ -498,7 +494,7 @@ // Switch on the block size. switch (u3x_atom(bloq)) { - case 4: ; + case 4: { float16_t sum16[2]; sum16[0] = (float16_t){SB_REAL16_ZERO}; for (c3_d i = 0; i < len_x; i++) { @@ -506,9 +502,9 @@ } sum16[1].v = 0x1; r_data = u3i_bytes((2+1)*sizeof(c3_y), (c3_y*)sum16); - break; + break;} - case 5: ; + case 5: { float32_t sum32[2]; sum32[0] = (float32_t){SB_REAL32_ZERO}; for (c3_d i = 0; i < len_x; i++) { @@ -516,9 +512,9 @@ } sum32[1].v = 0x1; r_data = u3i_bytes((4+1)*sizeof(c3_y), (c3_y*)sum32); - break; + break;} - case 6: ; + case 6: { float64_t sum64[2]; sum64[0] = (float64_t){SB_REAL64_ZERO}; for (c3_d i = 0; i < len_x; i++) { @@ -526,9 +522,9 @@ } sum64[1].v = 0x1; r_data = u3i_bytes((8+1)*sizeof(c3_y), (c3_y*)sum64); - break; + break;} - case 7: ; + case 7: { float128_t sum128[2]; sum128[0] = (float128_t){SB_REAL128L_ZERO, SB_REAL128U_ZERO}; for (c3_d i = 0; i < len_x; i++) { @@ -536,7 +532,7 @@ } sum128[1] = (float128_t){0x1, 0x0}; r_data = u3i_bytes((16+1)*sizeof(c3_y), (c3_y*)sum128); - break; + break;} } // Clean up and return. @@ -548,7 +544,7 @@ /* argmin - argmin(x) */ u3_noun - u3qi_la_argmin_real(u3_noun x_data, + u3qi_la_argmin_i754(u3_noun x_data, u3_noun shape, u3_noun bloq) { @@ -579,8 +575,8 @@ min_val16 = ((float16_t*)x_bytes)[i]; min_idx = (len_x - i - 1); } - } } - break; + } + break;} case 5: { float32_t min_val32 = ((float32_t*)x_bytes)[0]; @@ -589,8 +585,8 @@ min_val32 = ((float32_t*)x_bytes)[i]; min_idx = (len_x - i - 1); } - } } - break; + } + break;} case 6: { float64_t min_val64 = ((float64_t*)x_bytes)[0]; @@ -599,8 +595,8 @@ min_val64 = ((float64_t*)x_bytes)[i]; min_idx = (len_x - i - 1); } - } } - break; + } + break;} case 7: { float128_t min_val128 = ((float128_t*)x_bytes)[0]; @@ -609,8 +605,8 @@ min_val128 = *f128M_min(&min_val128, &((float128_t*)x_bytes)[i]); min_idx = (len_x - i - 1); } - } } - break; + } + break;} } u3_noun r_data = u3i_chub(min_idx); @@ -621,7 +617,7 @@ /* argmax - argmax(x) */ u3_noun - u3qi_la_argmax_real(u3_noun x_data, + u3qi_la_argmax_i754(u3_noun x_data, u3_noun shape, u3_noun bloq) { @@ -652,8 +648,8 @@ max_val16 = ((float16_t*)x_bytes)[i]; max_idx = (len_x - i - 1); } - } } - break; + } + break;} case 5: { float32_t max_val32 = ((float32_t*)x_bytes)[0]; @@ -662,8 +658,8 @@ max_val32 = ((float32_t*)x_bytes)[i]; max_idx = (len_x - i - 1); } - } } - break; + } + break;} case 6: { float64_t max_val64 = ((float64_t*)x_bytes)[0]; @@ -672,8 +668,8 @@ max_val64 = ((float64_t*)x_bytes)[i]; max_idx = (len_x - i - 1); } - } } - break; + } + break;} case 7: { float128_t max_val128 = ((float128_t*)x_bytes)[0]; @@ -682,8 +678,8 @@ max_val128 = *f128M_max(&max_val128, &((float128_t*)x_bytes)[i]); max_idx = (len_x - i - 1); } - } } - break; + } + break;} } u3_noun r_data = u3i_chub(max_idx); @@ -695,7 +691,7 @@ entire nd-array busted out as a linear list */ u3_noun - u3qi_la_ravel_real(u3_noun x_data, + u3qi_la_ravel_i754(u3_noun x_data, u3_noun shape, u3_noun bloq) { @@ -758,7 +754,7 @@ /* min - min(x,y) */ u3_noun - u3qi_la_min_real(u3_noun x_data, + u3qi_la_min_i754(u3_noun x_data, u3_noun shape, u3_noun bloq) { @@ -782,7 +778,7 @@ // Switch on the block size. switch (u3x_atom(bloq)) { - case 4: ; + case 4: { float16_t min_val16 = ((float16_t*)x_bytes)[0]; for (c3_d i = 0; i < len_x; i++) { min_val16 = f16_min(min_val16, ((float16_t*)x_bytes)[i]); @@ -791,9 +787,9 @@ r16[0] = min_val16; r16[1].v = 0x1; r_data = u3i_bytes((2+1)*sizeof(c3_y), (c3_y*)r16); - break; + break;} - case 5: ; + case 5: { float32_t min_val32 = ((float32_t*)x_bytes)[0]; for (c3_d i = 0; i < len_x; i++) { min_val32 = f32_min(min_val32, ((float32_t*)x_bytes)[i]); @@ -802,9 +798,9 @@ r32[0] = min_val32; r32[1].v = 0x1; r_data = u3i_bytes((4+1)*sizeof(c3_y), (c3_y*)r32); - break; + break;} - case 6: ; + case 6: { float64_t min_val64 = ((float64_t*)x_bytes)[0]; for (c3_d i = 0; i < len_x; i++) { min_val64 = f64_min(min_val64, ((float64_t*)x_bytes)[i]); @@ -813,9 +809,9 @@ r64[0] = min_val64; r64[1].v = 0x1; r_data = u3i_bytes((8+1)*sizeof(c3_y), (c3_y*)r64); - break; + break;} - case 7: ; + case 7: { float128_t min_val128 = ((float128_t*)x_bytes)[0]; for (c3_d i = 0; i < len_x; i++) { min_val128 = *f128M_min(&min_val128, &((float128_t*)x_bytes)[i]); @@ -824,7 +820,7 @@ r128[0] = min_val128; r128[1] = (float128_t){0x1, 0x0}; r_data = u3i_bytes((16+1)*sizeof(c3_y), (c3_y*)r128); - break; + break;} } // Clean up and return. @@ -836,7 +832,7 @@ /* max - max(x,y) */ u3_noun - u3qi_la_max_real(u3_noun x_data, + u3qi_la_max_i754(u3_noun x_data, u3_noun shape, u3_noun bloq) { @@ -860,7 +856,7 @@ // Switch on the block size. switch (u3x_atom(bloq)) { - case 4: ; + case 4: { float16_t max_val16 = ((float16_t*)x_bytes)[0]; for (c3_d i = 0; i < len_x; i++) { max_val16 = f16_max(max_val16, ((float16_t*)x_bytes)[i]); @@ -869,9 +865,9 @@ r16[0] = max_val16; r16[1].v = 0x1; r_data = u3i_bytes((2+1)*sizeof(c3_y), (c3_y*)r16); - break; + break;} - case 5: ; + case 5: { float32_t max_val32 = ((float32_t*)x_bytes)[0]; for (c3_d i = 0; i < len_x; i++) { max_val32 = f32_max(max_val32, ((float32_t*)x_bytes)[i]); @@ -880,9 +876,9 @@ r32[0] = max_val32; r32[1].v = 0x1; r_data = u3i_bytes((4+1)*sizeof(c3_y), (c3_y*)r32); - break; + break;} - case 6: ; + case 6: { float64_t max_val64 = ((float64_t*)x_bytes)[0]; for (c3_d i = 0; i < len_x; i++) { max_val64 = f64_max(max_val64, ((float64_t*)x_bytes)[i]); @@ -891,9 +887,9 @@ r64[0] = max_val64; r64[1].v = 0x1; r_data = u3i_bytes((8+1)*sizeof(c3_y), (c3_y*)r64); - break; + break;} - case 7: ; + case 7: { float128_t max_val128 = ((float128_t*)x_bytes)[0]; for (c3_d i = 0; i < len_x; i++) { max_val128 = *f128M_max(&max_val128, &((float128_t*)x_bytes)[i]); @@ -902,7 +898,7 @@ r128[0] = max_val128; r128[1] = (float128_t){0x1, 0x0}; r_data = u3i_bytes((16+1)*sizeof(c3_y), (c3_y*)r128); - break; + break;} } // Clean up and return. @@ -914,7 +910,7 @@ /* abs - |x| */ u3_noun - u3qi_la_abs_real(u3_noun x_data, + u3qi_la_abs_i754(u3_noun x_data, u3_noun shape, u3_noun bloq) { @@ -973,7 +969,7 @@ /* gth - x > y */ u3_noun - u3qi_la_gth_real(u3_noun x_data, + u3qi_la_gth_i754(u3_noun x_data, u3_noun y_data, u3_noun shape, u3_noun bloq) @@ -1046,7 +1042,7 @@ /* gte - x > y */ u3_noun - u3qi_la_gte_real(u3_noun x_data, + u3qi_la_gte_i754(u3_noun x_data, u3_noun y_data, u3_noun shape, u3_noun bloq) @@ -1119,7 +1115,7 @@ /* lth - x > y */ u3_noun - u3qi_la_lth_real(u3_noun x_data, + u3qi_la_lth_i754(u3_noun x_data, u3_noun y_data, u3_noun shape, u3_noun bloq) @@ -1192,7 +1188,7 @@ /* lte - x > y */ u3_noun - u3qi_la_lte_real(u3_noun x_data, + u3qi_la_lte_i754(u3_noun x_data, u3_noun y_data, u3_noun shape, u3_noun bloq) @@ -1265,7 +1261,7 @@ /* adds - axpy = 1*x+[n] */ u3_noun - u3qi_la_adds_real(u3_noun x_data, + u3qi_la_adds_i754(u3_noun x_data, u3_noun n, u3_noun shape, u3_noun bloq) @@ -1347,7 +1343,7 @@ /* subs - axpy = -1*[n]+x */ u3_noun - u3qi_la_subs_real(u3_noun x_data, + u3qi_la_subs_i754(u3_noun x_data, u3_noun n, u3_noun shape, u3_noun bloq) @@ -1430,7 +1426,7 @@ elementwise multiplication */ u3_noun - u3qi_la_muls_real(u3_noun x_data, + u3qi_la_muls_i754(u3_noun x_data, u3_noun n, u3_noun shape, u3_noun bloq) @@ -1493,7 +1489,7 @@ elementwise division */ u3_noun - u3qi_la_divs_real(u3_noun x_data, + u3qi_la_divs_i754(u3_noun x_data, u3_noun n, u3_noun shape, u3_noun bloq) @@ -1564,7 +1560,7 @@ remainder after scalar division */ u3_noun - u3qi_la_mods_real(u3_noun x_data, + u3qi_la_mods_i754(u3_noun x_data, u3_noun n, u3_noun shape, u3_noun bloq) @@ -1681,7 +1677,7 @@ /* dot - ?dot = x · y */ u3_noun - u3qi_la_dot_real(u3_noun x_data, + u3qi_la_dot_i754(u3_noun x_data, u3_noun y_data, u3_noun shape, u3_noun bloq) @@ -1710,33 +1706,33 @@ // Switch on the block size. switch (u3x_atom(bloq)) { - case 4: ; + case 4: { float16_t r16[2]; r16[0] = hdot(len_x, (float16_t*)x_bytes, 1, (float16_t*)y_bytes, 1); r16[1].v = 0x1; r_data = u3i_bytes((2+1)*sizeof(c3_y), (c3_y*)r16); - break; + break;} - case 5: ; + case 5: { float32_t r32[2]; r32[0] = sdot(len_x, (float32_t*)x_bytes, 1, (float32_t*)y_bytes, 1); r32[1].v = 0x1; r_data = u3i_bytes((4+1)*sizeof(c3_y), (c3_y*)r32); - break; + break;} - case 6: ; + case 6: { float64_t r64[2]; r64[0] = ddot(len_x, (float64_t*)x_bytes, 1, (float64_t*)y_bytes, 1); r64[1].v = 0x1; r_data = u3i_bytes((8+1)*sizeof(c3_y), (c3_y*)r64); - break; + break;} - case 7: ; + case 7: { float128_t r128[2]; r128[0] = qdot(len_x, (float128_t*)x_bytes, 1, (float128_t*)y_bytes, 1); r128[1] = (float128_t){0x1, 0x0}; r_data = u3i_bytes((16+1)*sizeof(c3_y), (c3_y*)r128); - break; + break;} } // Clean up and return. @@ -1759,12 +1755,12 @@ } // Assert length of dims is 2. if (u3qb_lent(shape) != 2) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } // Unpack shape into an array of dimensions. c3_d *dims = _get_dims(shape); if (dims[0] != dims[1]) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } // Unpack the data as a byte array. We assume total length < 2**64. @@ -1806,7 +1802,7 @@ { // Assert length of dims is 2. if (u3qb_lent(shape) != 2) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } // Unpack shape into an array of dimensions. c3_d *dims = _get_dims(shape); @@ -1845,7 +1841,7 @@ /* linspace - [a a+(b-a)/n ... b] */ u3_noun - u3qi_la_linspace_real(u3_noun a, + u3qi_la_linspace_i754(u3_noun a, u3_noun b, u3_noun n, u3_noun bloq) @@ -1858,7 +1854,7 @@ u3_noun r_data; switch (u3x_atom(bloq)) { - case 4: ; + case 4: { float16_t a16, b16; u3r_bytes(0, 2, (c3_y*)&(a16.v), a); u3r_bytes(0, 2, (c3_y*)&(b16.v), b); @@ -1873,9 +1869,9 @@ x_bytes16[(n+1)*2] = 0x1; // pin head r_data = u3i_bytes(((n+1)*2+1)*sizeof(c3_y), x_bytes16); u3a_free(x_bytes16); - break; + break;} - case 5: ; + case 5: { float32_t a32, b32; u3r_bytes(0, 4, (c3_y*)&(a32.v), a); u3r_bytes(0, 4, (c3_y*)&(b32.v), b); @@ -1890,9 +1886,9 @@ x_bytes32[(n+1)*4] = 0x1; // pin head r_data = u3i_bytes(((n+1)*4+1)*sizeof(c3_y), x_bytes32); u3a_free(x_bytes32); - break; + break;} - case 6: ; + case 6: { float64_t a64, b64; u3r_bytes(0, 8, (c3_y*)&(a64.v), a); u3r_bytes(0, 8, (c3_y*)&(b64.v), b); @@ -1907,9 +1903,9 @@ x_bytes64[(n+1)*8] = 0x1; // pin head r_data = u3i_bytes(((n+1)*8+1)*sizeof(c3_y), x_bytes64); u3a_free(x_bytes64); - break; + break;} - case 7: ; + case 7: { float128_t a128, b128; u3r_bytes(0, 16, (c3_y*)&(a128.v[0]), a); u3r_bytes(0, 16, (c3_y*)&(b128.v[0]), b); @@ -1931,7 +1927,7 @@ x_bytes128[(n+1)*16] = 0x1; // pin head r_data = u3i_bytes(((n+1)*16+1)*sizeof(c3_y), x_bytes128); u3a_free(x_bytes128); - break; + break;} } return r_data; @@ -1940,7 +1936,7 @@ /* range - [a a+d ... b] */ u3_noun - u3qi_la_range_real(u3_noun a, + u3qi_la_range_i754(u3_noun a, u3_noun b, u3_noun d, u3_noun bloq) @@ -1953,7 +1949,7 @@ u3_noun r_data; switch (u3x_atom(bloq)) { - case 4: ; + case 4: { float16_t a16, b16, interval16; u3r_bytes(0, 2, (c3_y*)&(a16.v), a); u3r_bytes(0, 2, (c3_y*)&(b16.v), b); @@ -1968,9 +1964,9 @@ x_bytes16[(n16+1)*2] = 0x1; // pin head r_data = u3i_bytes(((n16+1)*2+1)*sizeof(c3_y), x_bytes16); u3a_free(x_bytes16); - break; + break;} - case 5: ; + case 5: { float32_t a32, b32, interval32; u3r_bytes(0, 4, (c3_y*)&(a32.v), a); u3r_bytes(0, 4, (c3_y*)&(b32.v), b); @@ -1985,9 +1981,9 @@ x_bytes32[(n32+1)*4] = 0x1; // pin head r_data = u3i_bytes(((n32+1)*4+1)*sizeof(c3_y), x_bytes32); u3a_free(x_bytes32); - break; + break;} - case 6: ; + case 6: { float64_t a64, b64, interval64; u3r_bytes(0, 8, (c3_y*)&(a64.v), a); u3r_bytes(0, 8, (c3_y*)&(b64.v), b); @@ -2002,9 +1998,9 @@ x_bytes64[(n64+1)*8] = 0x1; // pin head r_data = u3i_bytes(((n64+1)*8+1)*sizeof(c3_y), x_bytes64); u3a_free(x_bytes64); - break; + break;} - case 7: ; + case 7: { float128_t a128, b128, interval128; u3r_bytes(0, 16, (c3_y*)&(a128.v[0]), a); u3r_bytes(0, 16, (c3_y*)&(b128.v[0]), b); @@ -2025,7 +2021,7 @@ x_bytes128[(n128+1)*16] = 0x1; // pin head r_data = u3i_bytes(((n128+1)*16+1)*sizeof(c3_y), x_bytes128); u3a_free(x_bytes128); - break; + break;} } return r_data; @@ -2034,20 +2030,20 @@ /* trace - tr(x) */ u3_noun - u3qi_la_trace_real(u3_noun x_data, + u3qi_la_trace_i754(u3_noun x_data, u3_noun shape, u3_noun bloq) { u3_noun d_data = u3qi_la_diag(x_data, shape, bloq); c3_d len_x0 = _get_dims(shape)[0]; - u3_noun r_data = u3qi_la_dot_real(d_data, d_data, u3nt(len_x0, 0x1, u3_nul), u3k(bloq)); + u3_noun r_data = u3qi_la_dot_i754(d_data, d_data, u3nt(len_x0, 0x1, u3_nul), u3k(bloq)); return r_data; } /* mmul */ u3_noun - u3qi_la_mmul_real(u3_noun x_data, + u3qi_la_mmul_i754(u3_noun x_data, u3_noun y_data, u3_noun x_shape, u3_noun y_shape, @@ -2059,15 +2055,10 @@ c3_d Nb= u3x_atom(u3h(y_shape)); c3_d P = u3x_atom(u3h(u3t(y_shape))); - // Fence on valid bloq size. - if (bloq < 4 || bloq > 7) { - return u3_none; - } - if ((u3_nul != u3t(u3t(x_shape))) || (u3_nul != u3t(u3t(y_shape))) || (Na != Nb)) { - return u3_none; + return u3m_bail(c3__exit); } c3_d N = Na; @@ -2134,7 +2125,7 @@ u3a_free(y_bytes); u3a_free(r_bytes); - return u3nc(u3nq(u3nt(M_, P_, u3_nul), u3k(bloq), c3__real, u3_nul), r_data); + return u3nc(u3nq(u3nt(M_, P_, u3_nul), u3k(bloq), c3__i754, u3_nul), r_data); } u3_noun @@ -2143,6 +2134,7 @@ // Each argument is a ray, [=meta data=@ux] u3_noun x_meta, x_data, y_meta, y_data; + fprintf(stderr, "> u3wi_la_add\n\r"); if ( c3n == u3r_mean(cor, u3x_sam_4, &x_meta, @@ -2153,7 +2145,7 @@ c3n == u3ud(x_data) || c3n == u3ud(y_data) ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp, y_shape, y_bloq, y_kind, y_fxp, @@ -2177,12 +2169,14 @@ // fxp does not need to match here so no check ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { + fprintf(stderr, "> u3wi_la_add\n\r"); switch (x_kind) { - case c3__real: + case c3__i754: _set_rounding(rnd); - u3_noun r_data = _soft_run(u3qi_la_add_real(x_data, y_data, x_shape, x_bloq)); + fprintf(stderr, ">> u3wi_la_add\n\r"); + u3_noun r_data = _soft_run(u3qi_la_add_i754(x_data, y_data, x_shape, x_bloq)); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2208,7 +2202,7 @@ c3n == u3ud(x_data) || c3n == u3ud(y_data) ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp, y_shape, y_bloq, y_kind, y_fxp, @@ -2232,12 +2226,12 @@ // fxp does not need to match here so no check ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { switch (x_kind) { - case c3__real: + case c3__i754: _set_rounding(rnd); - u3_noun r_data = _soft_run(u3qi_la_sub_real(x_data, y_data, x_shape, x_bloq)); + u3_noun r_data = _soft_run(u3qi_la_sub_i754(x_data, y_data, x_shape, x_bloq)); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2263,7 +2257,7 @@ c3n == u3ud(x_data) || c3n == u3ud(y_data) ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp, y_shape, y_bloq, y_kind, y_fxp, @@ -2287,12 +2281,12 @@ // fxp does not need to match here so no check ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { switch (x_kind) { - case c3__real: + case c3__i754: _set_rounding(rnd); - u3_noun r_data = _soft_run(u3qi_la_mul_real(x_data, y_data, x_shape, x_bloq)); + u3_noun r_data = _soft_run(u3qi_la_mul_i754(x_data, y_data, x_shape, x_bloq)); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2318,7 +2312,7 @@ c3n == u3ud(x_data) || c3n == u3ud(y_data) ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp, y_shape, y_bloq, y_kind, y_fxp, @@ -2342,12 +2336,12 @@ // fxp does not need to match here so no check ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { switch (x_kind) { - case c3__real: + case c3__i754: _set_rounding(rnd); - u3_noun r_data = _soft_run(u3qi_la_div_real(x_data, y_data, x_shape, x_bloq)); + u3_noun r_data = _soft_run(u3qi_la_div_i754(x_data, y_data, x_shape, x_bloq)); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2373,7 +2367,7 @@ c3n == u3ud(x_data) || c3n == u3ud(y_data) ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp, y_shape, y_bloq, y_kind, y_fxp, @@ -2397,12 +2391,12 @@ // fxp does not need to match here so no check ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { switch (x_kind) { - case c3__real: + case c3__i754: _set_rounding(rnd); - u3_noun r_data = _soft_run(u3qi_la_mod_real(x_data, y_data, x_shape, x_bloq)); + u3_noun r_data = _soft_run(u3qi_la_mod_i754(x_data, y_data, x_shape, x_bloq)); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2424,7 +2418,7 @@ 0) || c3n == u3ud(x_data) ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp, rnd; @@ -2437,12 +2431,12 @@ c3n == u3ud(x_kind) ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { switch (x_kind) { - case c3__real: + case c3__i754: _set_rounding(rnd); - u3_noun r_data = _soft_run(u3qi_la_cumsum_real(x_data, x_shape, x_bloq)); + u3_noun r_data = _soft_run(u3qi_la_cumsum_i754(x_data, x_shape, x_bloq)); return u3nc(u3nq(u3nt(0x1, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2464,7 +2458,7 @@ 0) || c3n == u3ud(x_data) ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp; x_shape = u3h(x_meta); // 2 @@ -2475,13 +2469,13 @@ c3n == u3ud(x_kind) ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { switch (x_kind) { - case c3__real: ; - u3_noun r_data = u3qi_la_argmin_real(x_data, x_shape, x_bloq); + case c3__i754: { + u3_noun r_data = _soft_run(u3qi_la_argmin_i754(x_data, x_shape, x_bloq)); // bare atom (@ index) - return r_data; + return r_data;} default: return u3_none; @@ -2502,7 +2496,7 @@ 0) || c3n == u3ud(x_data) ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp; x_shape = u3h(x_meta); // 2 @@ -2512,13 +2506,13 @@ c3n == u3ud(x_kind) ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { switch (x_kind) { - case c3__real: ; - u3_noun r_data = _soft_run(u3qi_la_ravel_real(x_data, x_shape, x_bloq)); + case c3__i754: { + u3_noun r_data = _soft_run(u3qi_la_ravel_i754(x_data, x_shape, x_bloq)); // (list @) - return r_data; + return r_data;} default: return u3_none; @@ -2539,7 +2533,7 @@ 0) || c3n == u3ud(x_data) ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp; x_shape = u3h(x_meta); // 2 @@ -2550,13 +2544,13 @@ c3n == u3ud(x_kind) ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { switch (x_kind) { - case c3__real: ; - u3_noun r_data = u3qi_la_argmax_real(x_data, x_shape, x_bloq); + case c3__i754: { + u3_noun r_data = _soft_run(u3qi_la_argmax_i754(x_data, x_shape, x_bloq)); // bare atom (@ index) - return r_data; + return r_data;} default: return u3_none; @@ -2577,7 +2571,7 @@ 0) || c3n == u3ud(x_data) ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp; x_shape = u3h(x_meta); // 2 @@ -2588,12 +2582,12 @@ c3n == u3ud(x_kind) ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { switch (x_kind) { - case c3__real: ; - u3_noun r_data = _soft_run(u3qi_la_min_real(x_data, x_shape, x_bloq)); - return u3nc(u3nq(u3nt(0x1, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); + case c3__i754: { + u3_noun r_data = _soft_run(u3qi_la_min_i754(x_data, x_shape, x_bloq)); + return u3nc(u3nq(u3nt(0x1, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data);} default: return u3_none; @@ -2614,7 +2608,7 @@ 0) || c3n == u3ud(x_data) ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp; x_shape = u3h(x_meta); // 2 @@ -2625,12 +2619,12 @@ c3n == u3ud(x_kind) ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { switch (x_kind) { - case c3__real: ; - u3_noun r_data = _soft_run(u3qi_la_max_real(x_data, x_shape, x_bloq)); - return u3nc(u3nq(u3nt(0x1, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); + case c3__i754: { + u3_noun r_data = _soft_run(u3qi_la_max_i754(x_data, x_shape, x_bloq)); + return u3nc(u3nq(u3nt(0x1, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data);} default: return u3_none; @@ -2651,7 +2645,7 @@ 0) || c3n == u3ud(x_data) ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp; x_shape = u3h(x_meta); // 2 @@ -2662,12 +2656,12 @@ c3n == u3ud(x_kind) ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { switch (x_kind) { - case c3__real: ; - u3_noun r_data = _soft_run(u3qi_la_abs_real(x_data, x_shape, x_bloq)); - return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); + case c3__i754: { + u3_noun r_data = _soft_run(u3qi_la_abs_i754(x_data, x_shape, x_bloq)); + return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data);} default: return u3_none; @@ -2692,7 +2686,7 @@ c3n == u3ud(x_data) || c3n == u3ud(y_data) ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp, y_shape, y_bloq, y_kind, y_fxp, @@ -2715,12 +2709,12 @@ c3n == u3r_sing(x_fxp, y_fxp) ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { switch (x_kind) { - case c3__real: ; - u3_noun r_data = _soft_run(u3qi_la_gth_real(x_data, y_data, x_shape, x_bloq)); - return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); + case c3__i754: { + u3_noun r_data = _soft_run(u3qi_la_gth_i754(x_data, y_data, x_shape, x_bloq)); + return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data);} default: return u3_none; @@ -2745,7 +2739,7 @@ c3n == u3ud(x_data) || c3n == u3ud(y_data) ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp, y_shape, y_bloq, y_kind, y_fxp, @@ -2768,12 +2762,12 @@ c3n == u3r_sing(x_fxp, y_fxp) ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { switch (x_kind) { - case c3__real: ; - u3_noun r_data = _soft_run(u3qi_la_gte_real(x_data, y_data, x_shape, x_bloq)); - return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); + case c3__i754: { + u3_noun r_data = _soft_run(u3qi_la_gte_i754(x_data, y_data, x_shape, x_bloq)); + return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data);} default: return u3_none; @@ -2798,7 +2792,7 @@ c3n == u3ud(x_data) || c3n == u3ud(y_data) ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp, y_shape, y_bloq, y_kind, y_fxp, @@ -2821,12 +2815,12 @@ c3n == u3r_sing(x_fxp, y_fxp) ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { switch (x_kind) { - case c3__real: ; - u3_noun r_data = _soft_run(u3qi_la_lth_real(x_data, y_data, x_shape, x_bloq)); - return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); + case c3__i754: { + u3_noun r_data = _soft_run(u3qi_la_lth_i754(x_data, y_data, x_shape, x_bloq)); + return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data);} default: return u3_none; @@ -2851,7 +2845,7 @@ c3n == u3ud(x_data) || c3n == u3ud(y_data) ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp, y_shape, y_bloq, y_kind, y_fxp, @@ -2874,12 +2868,12 @@ c3n == u3r_sing(x_fxp, y_fxp) ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { switch (x_kind) { - case c3__real: ; - u3_noun r_data = _soft_run(u3qi_la_lte_real(x_data, y_data, x_shape, x_bloq)); - return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); + case c3__i754: { + u3_noun r_data = _soft_run(u3qi_la_lte_i754(x_data, y_data, x_shape, x_bloq)); + return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data);} default: return u3_none; @@ -2902,7 +2896,7 @@ c3n == u3ud(x_data) || c3n == u3ud(n) ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp, rnd; @@ -2912,9 +2906,9 @@ x_fxp = u3t(u3t(u3t(x_meta))); // 15 rnd = u3h(u3t(u3t(u3t(cor)))); // 30 switch (x_kind) { - case c3__real: + case c3__i754: _set_rounding(rnd); - u3_noun r_data = _soft_run(u3qi_la_adds_real(x_data, n, x_shape, x_bloq)); + u3_noun r_data = _soft_run(u3qi_la_adds_i754(x_data, n, x_shape, x_bloq)); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2937,7 +2931,7 @@ c3n == u3ud(x_data) || c3n == u3ud(n) ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp, rnd; @@ -2947,9 +2941,9 @@ x_fxp = u3t(u3t(u3t(x_meta))); // 15 rnd = u3h(u3t(u3t(u3t(cor)))); // 30 switch (x_kind) { - case c3__real: + case c3__i754: _set_rounding(rnd); - u3_noun r_data = _soft_run(u3qi_la_subs_real(x_data, n, x_shape, x_bloq)); + u3_noun r_data = _soft_run(u3qi_la_subs_i754(x_data, n, x_shape, x_bloq)); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2972,7 +2966,7 @@ c3n == u3ud(x_data) || c3n == u3ud(n) ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp, rnd; @@ -2982,9 +2976,9 @@ x_fxp = u3t(u3t(u3t(x_meta))); // 15 rnd = u3h(u3t(u3t(u3t(cor)))); // 30 switch (x_kind) { - case c3__real: + case c3__i754: _set_rounding(rnd); - u3_noun r_data = _soft_run(u3qi_la_muls_real(x_data, n, x_shape, x_bloq)); + u3_noun r_data = _soft_run(u3qi_la_muls_i754(x_data, n, x_shape, x_bloq)); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -3007,7 +3001,7 @@ c3n == u3ud(x_data) || c3n == u3ud(n) ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp, rnd; @@ -3017,9 +3011,9 @@ x_fxp = u3t(u3t(u3t(x_meta))); // 15 rnd = u3h(u3t(u3t(u3t(cor)))); // 30 switch (x_kind) { - case c3__real: + case c3__i754: _set_rounding(rnd); - u3_noun r_data = _soft_run(u3qi_la_divs_real(x_data, n, x_shape, x_bloq)); + u3_noun r_data = _soft_run(u3qi_la_divs_i754(x_data, n, x_shape, x_bloq)); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -3042,7 +3036,7 @@ c3n == u3ud(x_data) || c3n == u3ud(n) ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp, rnd; @@ -3052,9 +3046,9 @@ x_fxp = u3t(u3t(u3t(x_meta))); // 15 rnd = u3h(u3t(u3t(u3t(cor)))); // 30 switch (x_kind) { - case c3__real: + case c3__i754: _set_rounding(rnd); - u3_noun r_data = _soft_run(u3qi_la_mods_real(x_data, n, x_shape, x_bloq)); + u3_noun r_data = _soft_run(u3qi_la_mods_i754(x_data, n, x_shape, x_bloq)); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -3079,7 +3073,7 @@ c3n == u3ud(x_data) || c3n == u3ud(y_data) ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp, y_shape, y_bloq, y_kind, y_fxp, @@ -3103,12 +3097,12 @@ c3n == u3r_sing(x_fxp, y_fxp) ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { switch (x_kind) { - case c3__real: + case c3__i754: _set_rounding(rnd); - u3_noun r_data = _soft_run(u3qi_la_dot_real(x_data, y_data, x_shape, x_bloq)); + u3_noun r_data = _soft_run(u3qi_la_dot_i754(x_data, y_data, x_shape, x_bloq)); c3_d len_x0 = _get_dims(x_shape)[0]; return u3nc(u3nq(u3nt(len_x0, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); @@ -3131,7 +3125,7 @@ 0) || c3n == u3ud(x_data) ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp; x_shape = u3h(x_meta); // 2 @@ -3142,9 +3136,9 @@ c3n == u3ud(x_kind) ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { - u3_noun r_data = u3qi_la_transpose(x_data, x_shape, x_bloq); + u3_noun r_data = _soft_run(u3qi_la_transpose(x_data, x_shape, x_bloq)); return u3nc(u3nq(u3nt(u3k(u3h(x_shape)), u3k(u3h(u3t(x_shape))), u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); } } @@ -3162,7 +3156,7 @@ u3x_sam_7, &n, 0)) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp; x_shape = u3h(x_meta); // 2 @@ -3174,12 +3168,12 @@ c3n == u3ud(x_kind) ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { switch (x_kind) { - case c3__real: + case c3__i754: _set_rounding(rnd); - u3_noun r_data = _soft_run(u3qi_la_linspace_real(a, b, n, x_bloq)); + u3_noun r_data = _soft_run(u3qi_la_linspace_i754(a, b, n, x_bloq)); x_shape = u3nt(u3x_atom(n), 0x1, u3_nul); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); @@ -3202,7 +3196,7 @@ u3x_sam_7, &d, 0)) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp; x_shape = u3h(x_meta); // 2 @@ -3214,12 +3208,12 @@ c3n == u3ud(x_kind) ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { switch (x_kind) { - case c3__real: + case c3__i754: _set_rounding(rnd); - u3_noun r_data = _soft_run(u3qi_la_range_real(a, b, d, x_bloq)); + u3_noun r_data = _soft_run(u3qi_la_range_i754(a, b, d, x_bloq)); c3_d a_, b_, d_; c3_ds n_; switch (x_bloq) { @@ -3274,7 +3268,7 @@ 0) || c3n == u3ud(x_data) ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp; x_shape = u3h(x_meta); // 2 @@ -3285,7 +3279,7 @@ c3n == u3ud(x_kind) ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { u3_noun r_data = _soft_run(u3qi_la_diag(x_data, x_shape, x_bloq)); c3_d len_x0 = _get_dims(x_shape)[0]; @@ -3306,7 +3300,7 @@ 0) || c3n == u3ud(x_data) ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp; if ( c3n == u3r_mean(x_meta, @@ -3317,12 +3311,12 @@ 0) ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { switch (x_kind) { - case c3__real: ; - u3_noun r_data = _soft_run(u3qi_la_trace_real(x_data, x_shape, x_bloq)); - return u3nc(u3nq(u3nt(0x1, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); + case c3__i754: { + u3_noun r_data = _soft_run(u3qi_la_trace_i754(x_data, x_shape, x_bloq)); + return u3nc(u3nq(u3nt(0x1, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data);} default: return u3_none; @@ -3347,7 +3341,7 @@ c3n == u3ud(x_data) || c3n == u3ud(y_data) ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { u3_noun x_shape, x_bloq, x_kind, x_fxp, y_shape, y_bloq, y_kind, y_fxp, @@ -3366,12 +3360,12 @@ // fxp does not need to match so no check ) { - u3m_bail(c3__exit); + return u3m_bail(c3__exit); } else { switch (x_kind) { - case c3__real: + case c3__i754: _set_rounding(rnd); - u3_noun r_data = _soft_run(u3qi_la_mmul_real(x_data, y_data, x_shape, y_shape, x_bloq)); + u3_noun r_data = _soft_run(u3qi_la_mmul_i754(x_data, y_data, x_shape, y_shape, x_bloq)); // result is already [meta data] return r_data; diff --git a/pkg/noun/jets/q.h b/pkg/noun/jets/q.h index bb366844e2..720cd8a2de 100644 --- a/pkg/noun/jets/q.h +++ b/pkg/noun/jets/q.h @@ -245,34 +245,34 @@ u3_noun u3qfp_nepo(u3_noun, u3_noun); u3_noun u3qfp_rake(u3_noun); - u3_noun u3qi_la_add_real(u3_noun, u3_noun, u3_noun, u3_noun); - u3_noun u3qi_la_sub_real(u3_noun, u3_noun, u3_noun, u3_noun); - u3_noun u3qi_la_mul_real(u3_noun, u3_noun, u3_noun, u3_noun); - u3_noun u3qi_la_div_real(u3_noun, u3_noun, u3_noun, u3_noun); - u3_noun u3qi_la_mod_real(u3_noun, u3_noun, u3_noun, u3_noun); - u3_noun u3qi_la_adds_real(u3_noun, u3_noun, u3_noun, u3_noun); - u3_noun u3qi_la_subs_real(u3_noun, u3_noun, u3_noun, u3_noun); - u3_noun u3qi_la_muls_real(u3_noun, u3_noun, u3_noun, u3_noun); - u3_noun u3qi_la_divs_real(u3_noun, u3_noun, u3_noun, u3_noun); - u3_noun u3qi_la_mods_real(u3_noun, u3_noun, u3_noun, u3_noun); - u3_noun u3qi_la_dot_real(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_add_i754(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_sub_i754(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_mul_i754(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_div_i754(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_mod_i754(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_adds_i754(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_subs_i754(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_muls_i754(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_divs_i754(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_mods_i754(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_dot_i754(u3_noun, u3_noun, u3_noun, u3_noun); u3_noun u3qi_la_diag(u3_noun, u3_noun, u3_noun); u3_noun u3qi_la_transpose(u3_noun, u3_noun, u3_noun); - u3_noun u3qi_la_cumsum_real(u3_noun, u3_noun, u3_noun); - u3_noun u3qi_la_argmin_real(u3_noun, u3_noun, u3_noun); - u3_noun u3qi_la_argmax_real(u3_noun, u3_noun, u3_noun); - u3_noun u3qi_la_ravel_real(u3_noun, u3_noun, u3_noun); - u3_noun u3qi_la_min_real(u3_noun, u3_noun, u3_noun); - u3_noun u3qi_la_max_real(u3_noun, u3_noun, u3_noun); - u3_noun u3qi_la_linspace_real(u3_noun, u3_noun, u3_noun, u3_noun); - u3_noun u3qi_la_range_real(u3_noun, u3_noun, u3_noun, u3_noun); - u3_noun u3qi_la_abs_real(u3_noun, u3_noun, u3_noun); - u3_noun u3qi_la_gth_real(u3_noun, u3_noun, u3_noun, u3_noun); - u3_noun u3qi_la_gte_real(u3_noun, u3_noun, u3_noun, u3_noun); - u3_noun u3qi_la_lth_real(u3_noun, u3_noun, u3_noun, u3_noun); - u3_noun u3qi_la_lte_real(u3_noun, u3_noun, u3_noun, u3_noun); - u3_noun u3qi_la_trace_real(u3_noun, u3_noun, u3_noun); - u3_noun u3qi_la_mmul_real(u3_noun, u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_cumsum_i754(u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_argmin_i754(u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_argmax_i754(u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_ravel_i754(u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_min_i754(u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_max_i754(u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_linspace_i754(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_range_i754(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_abs_i754(u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_gth_i754(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_gte_i754(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_lth_i754(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_lte_i754(u3_noun, u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_trace_i754(u3_noun, u3_noun, u3_noun); + u3_noun u3qi_la_mmul_i754(u3_noun, u3_noun, u3_noun, u3_noun, u3_noun); # define u3qfu_van_fan 28 # define u3qfu_van_rib 58 From 3fe1a0cd1154ea98f48a5e1d8d473b2ed6bba11b Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Tue, 23 Jul 2024 15:10:15 +0200 Subject: [PATCH 260/430] mesa: fix memory and refcounting errors --- pkg/vere/io/mesa.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 7a35368960..7f4ccb3d7a 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -2119,9 +2119,9 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) // TODO actually stick next hop in packet _mesa_send_pact(sam_u, u3k(las), per_u, pac_u); _mesa_del_pit(sam_u, nam_u); - mesa_free_pact(pac_u); _mesa_free_pict(pic_u); u3z(pin); + return; } if ( c3n == our ) { // TODO: free pact and pict From 8a15c7f8cd2dcdc973409f6e4a872ea23e906b5d Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Tue, 23 Jul 2024 15:11:01 +0200 Subject: [PATCH 261/430] mesa: remove lane from %heer --- pkg/vere/io/mesa.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 7f4ccb3d7a..99cefdc413 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -2143,7 +2143,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) // XX should just preserve input buffer c3_w cur_w = mesa_etch_pact(sab_u.buf_y, pac_u); - cad = u3nt(c3__heer, lan, u3i_slab_mint(&sab_u)); + cad = u3nc(c3__heer, u3i_slab_mint(&sab_u)); } u3_noun wir = u3nc(c3__ames, u3_nul); @@ -2223,7 +2223,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) c3_y* buf_y = c3_calloc((PACT_SIZE - 1024) + jumbo_len_w); c3_w res_w = mesa_etch_pact(buf_y, pac_u); - cad = u3nt(c3__heer, lan, u3i_bytes(res_w, buf_y)); + cad = u3nc(c3__heer, u3i_bytes(res_w, buf_y)); c3_free(buf_y); } @@ -2439,7 +2439,7 @@ _mesa_hear_poke(u3_mesa_pict* pic_u, u3_lane* lan_u) // XX should just preserve input buffer mesa_etch_pact(sab_u.buf_y, pac_u); - cad = u3nt(c3__heer, lan, u3i_slab_mint(&sab_u)); + cad = u3nc(c3__heer, u3i_slab_mint(&sab_u)); } u3_ovum* ovo = u3_ovum_init(0, c3__ames, wir, cad); From d9c4393b938796b72c33d6c55313ffe442f50902 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Tue, 23 Jul 2024 15:11:28 +0200 Subject: [PATCH 262/430] mesa: fix loobean check --- pkg/vere/io/mesa.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 99cefdc413..7b186e6457 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -2294,7 +2294,7 @@ _mesa_forward_request(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane lan_u) if ( c3y == sam_u->for_o && sam_u->pir_u->who_d[0] == per_u->imp_y ) { u3_lane lin_u = _mesa_get_direct_lane(sam_u, pac_u->pek_u.nam_u.her_u); u3_lane zer_u = {0, 0}; - if ( _mesa_lanes_equal(&zer_u, &lin_u)) { + if ( _mesa_lanes_equal(&zer_u, &lin_u) == c3y) { _mesa_free_pict(pic_u); return; } From fa8a8273ace1b3719e55ec7638023e3ab227afb4 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Tue, 23 Jul 2024 15:12:15 +0200 Subject: [PATCH 263/430] mesa: implement _mesa_poke_news_cb --- pkg/vere/io/mesa.c | 48 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 7b186e6457..5a09608a0d 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -2364,12 +2364,28 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) static void _mesa_poke_news_cb(u3_ovum* egg_u, u3_ovum_news new_e) { - u3_mesa_pict* pic_u = egg_u->ptr_v; - if ( u3_ovum_done == new_e ) { - // XX success stuff here - /* u3l_log("mesa: poke success"); */ + if ( u3_ovum_done != new_e ) { + #ifdef MESA_DEBUG + u3l_log("mesa: arvo poke event was not a success"); + #endif + return; } + + u3_mesa_lane_cb_data* dat_u = egg_u->ptr_v; + u3_peer* per_u = dat_u->per_u; + + #ifdef MESA_DEBUG + c3_c* her_c = u3_ship_to_string(dat_u->her_u); + u3l_log("mesa: %%dear %s", her_c); + _log_lane(&dat_u->lan_u); + c3_free(her_c); + #endif + + // XX tame old routes, and then dear? + _mesa_dear(&per_u->sam_u->car_u, dat_u->her_u, per_u->dan_u); + + c3_free(dat_u); } static void @@ -2380,8 +2396,6 @@ _mesa_poke_bail_cb(u3_ovum* egg_u, u3_noun lud) u3l_log("mesa: poke failure"); } -// xx: should inject event directly, but vane does not work -// so we just hack it to get static void _mesa_hear_poke(u3_mesa_pict* pic_u, u3_lane* lan_u) { @@ -2445,14 +2459,24 @@ _mesa_hear_poke(u3_mesa_pict* pic_u, u3_lane* lan_u) u3_ovum* ovo = u3_ovum_init(0, c3__ames, wir, cad); ovo = u3_auto_plan(&sam_u->car_u, ovo); - if ( 1 == pac_u->pok_u.dat_u.tot_w ) { - _mesa_free_pict(pic_u); - } - else { + // if ( 1 == pac_u->pok_u.dat_u.tot_w ) { + // u3l_log("free poke"); + // _mesa_free_pict(pic_u); + // } + // else { + // u3l_log("inject poke"); + // XX check request state for *payload* (in-progress duplicate) assert(pac_u->pok_u.dat_u.tot_w); - u3_auto_peer(ovo, pic_u, _mesa_poke_news_cb, _mesa_poke_bail_cb); - } + u3_mesa_lane_cb_data* dat_u = c3_malloc(sizeof(u3_mesa_lane_cb_data)); + { + memcpy(dat_u->her_u, pac_u->pok_u.pay_u.her_u, 16); + dat_u->lan_u.pip_w = lan_u->pip_w; + dat_u->lan_u.por_s = lan_u->por_s; + dat_u->per_u = per_u; + } + u3_auto_peer(ovo, dat_u, _mesa_poke_news_cb, _mesa_poke_bail_cb); + // } } void From 22181027d584fdc50fa4084a9e0ded4aeb345569 Mon Sep 17 00:00:00 2001 From: midden-fabler Date: Tue, 23 Jul 2024 09:43:41 -0400 Subject: [PATCH 264/430] http: create function for beam logic (WIP) --- pkg/vere/io/http.c | 187 ++++++++++++++++++++++++--------------------- 1 file changed, 102 insertions(+), 85 deletions(-) diff --git a/pkg/vere/io/http.c b/pkg/vere/io/http.c index 39bbbe5538..ba4c64d5f9 100644 --- a/pkg/vere/io/http.c +++ b/pkg/vere/io/http.c @@ -7,6 +7,7 @@ #include "openssl/err.h" #include "openssl/ssl.h" #include "version.h" +#include typedef struct _u3_h2o_serv { h2o_globalconf_t fig_u; // h2o global config @@ -799,6 +800,97 @@ _parse_range(c3_c* txt_c, c3_w len_w) return cut; } +typedef struct _beam { + u3_noun who; + u3_noun des; + u3_noun cas; + u3_noun pur; +} beam; + +/* _get_beam: url to beam +*/ +static beam +_get_beam(u3_hreq* req_u, c3_c* txt_c, c3_w len_w) +{ + beam bem; + u3_http* htp_u = req_u->hon_u->htp_u; + u3_httd* htd_u = htp_u->htd_u; + u3_noun our = u3dc("scot", 'p', u3i_chubs(2, htd_u->car_u.pir_u->who_d)); + // get beak from path + // + for ( c3_w i_w = 0; i_w < 3; ++i_w ) { + u3_noun* wer; + if ( 0 == i_w ) { + wer = &bem.who; + } + else if ( 1 == i_w ) { + wer = &bem.des; + } + else { + wer = &bem.cas; + } + + // find '//' + if ( (len_w >= 2) + && ('/' == txt_c[0]) + && ('/' == txt_c[1]) ) + { + *wer = u3_nul; + txt_c++; + len_w--; + } + // skip '/' + else if ( (len_w > 0) && ('/' == txt_c[0]) ) { + txt_c++; + len_w--; + } + // '=' + if ( (len_w > 0) && ('=' == txt_c[0]) ) { + if ( 0 == i_w ) { + *wer = our; + } + else if ( 1 == i_w ) { + *wer = u3i_string("base"); + } + else { + req_u->peq_u->las_o = c3y; + } + txt_c++; + len_w--; + } + // slice cord + else { + c3_c* nex_c; + c3_c* tis_c = memchr(txt_c, '=', len_w); + c3_c* fas_c = memchr(txt_c, '/', len_w); + if ( tis_c && fas_c ) { + nex_c = c3_min(tis_c, fas_c); + } + else if ( tis_c ) { + nex_c = tis_c; + } + else { + nex_c = fas_c; + } + if ( !nex_c ) { + c3_c* msg_c = "bad beam"; + // h2o_send_error_generic(req_u->rec_u, 400, msg_c, msg_c, 0); + // return; + } + else { + c3_w dif_w = (c3_p)(nex_c - txt_c); + *wer = u3i_bytes(dif_w, (const c3_y*)txt_c); + txt_c = nex_c; + len_w = len_w - dif_w; + } + } + } + + bem.pur = u3dc("rush", u3i_bytes(len_w, (const c3_y*)txt_c), u3v_wish("stap")); + + return bem; +} + /* _http_req_dispatch(): dispatch http request */ static void @@ -860,86 +952,11 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) gang = u3_nul; } - u3_noun who; - u3_noun des; - u3_noun cas; - - // XX move to function - // get beak from path - // - for ( c3_w i_w = 0; i_w < 3; ++i_w ) { - u3_noun* wer; - if ( 0 == i_w ) { - wer = &who; - } - else if ( 1 == i_w ) { - wer = &des; - } - else { - wer = &cas; - } - - // find '//' - if ( (len_w >= 2) - && ('/' == bas_c[0]) - && ('/' == bas_c[1]) ) - { - *wer = u3_nul; - bas_c++; - len_w--; - } - // skip '/' - else if ( (len_w > 0) && ('/' == bas_c[0]) ) { - bas_c++; - len_w--; - } - // '=' - if ( (len_w > 0) && ('=' == bas_c[0]) ) { - if ( 0 == i_w ) { - *wer = our; - } - else if ( 1 == i_w ) { - *wer = u3i_string("base"); - } - else { - req_u->peq_u->las_o = c3y; - } - bas_c++; - len_w--; - } - // slice cord - else { - c3_c* nex_c; - c3_c* tis_c = memchr(bas_c, '=', len_w); - c3_c* fas_c = memchr(bas_c, '/', len_w); - if ( tis_c && fas_c ) { - nex_c = c3_min(tis_c, fas_c); - } - else if ( tis_c ) { - nex_c = tis_c; - } - else { - nex_c = fas_c; - } - - if ( !nex_c ) { - c3_c* msg_c = "bad beam"; - h2o_send_error_generic(req_u->rec_u, 400, msg_c, msg_c, 0); - return; - } - else { - c3_w dif_w = (c3_p)(nex_c - bas_c); - *wer = u3i_bytes(dif_w, (const c3_y*)bas_c); - bas_c = nex_c; - len_w = len_w - dif_w; - } - } - } - - u3_noun spur = u3dc("rush", u3i_bytes(len_w, (const c3_y*)bas_c), u3v_wish("stap")); + beam bem = _get_beam(req_u, bas_c, len_w); - if ( (u3_nul == spur) - || (c3n == u3r_sing(our, who)) ) + // XX necessary? + if ( (u3_nul == bem.pur) + || (c3n == u3r_sing(our, bem.who)) ) { c3_c* msg_c = "bad scry path"; h2o_send_error_generic(req_u->rec_u, 400, msg_c, msg_c, 0); @@ -947,23 +964,23 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) } else { - spur = u3nc(u3i_string("mime"), u3t(spur)); + u3_noun spur = u3nc(u3i_string("mime"), u3t(bem.pur)); if ( c3y == req_u->peq_u->las_o ) { u3_pier_peek_last(htd_u->car_u.pir_u, gang, c3__ex, - des, spur, req_u->peq_u, _http_scry_cb); + bem.des, spur, req_u->peq_u, _http_scry_cb); } else { - u3_noun bem = u3nq(our, des, cas, spur); - u3_weak nac = u3h_get(htd_u->nax_p, bem); + u3_noun bam = u3nq(bem.who, bem.des, bem.cas, spur); + u3_weak nac = u3h_get(htd_u->nax_p, bam); if ( (u3_none == nac) || (u3_nul == nac) || ((u3_nul == gang) && (c3y == u3r_at(14, nac))) ) { // maybe cache, then serve subsequent range requests from cache - req_u->peq_u->pax = bem; - u3_pier_peek(htd_u->car_u.pir_u, gang, u3nt(0, c3__ex, u3k(bem)), + req_u->peq_u->pax = bam; + u3_pier_peek(htd_u->car_u.pir_u, gang, u3nt(0, c3__ex, u3k(bam)), req_u->peq_u, _http_scry_cb); } else { From 864289f8da3b9150226603aa8de0822bb8f8d1af Mon Sep 17 00:00:00 2001 From: lukechampine Date: Tue, 23 Jul 2024 10:51:46 -0400 Subject: [PATCH 265/430] mesa: verify packet auth via scry --- pkg/c3/motes.h | 3 + pkg/vere/io/mesa.c | 213 ++++++++++++++++++++++++--------------------- 2 files changed, 117 insertions(+), 99 deletions(-) diff --git a/pkg/c3/motes.h b/pkg/c3/motes.h index 25d593fdf4..9d34561c09 100644 --- a/pkg/c3/motes.h +++ b/pkg/c3/motes.h @@ -557,6 +557,7 @@ # define c3__hind c3_s4('h','i','n','d') # define c3__hint c3_s4('h','i','n','t') # define c3__hit c3_s3('h','i','t') +# define c3__hmac c3_s4('h','m','a','c') # define c3__hmal c3_s4('h','m','a','l') # define c3__hold c3_s4('h','o','l','d') # define c3__hole c3_s4('h','o','l','e') @@ -1066,6 +1067,7 @@ # define c3__shut c3_s4('s','h','u','t') # define c3__sibl c3_s4('s','i','b','l') # define c3__sift c3_s4('s','i','f','t') +# define c3__sign c3_s4('s','i','g','n') # define c3__sing c3_s4('s','i','n','g') # define c3__sist c3_s4('s','i','s','t') # define c3__site c3_s4('s','i','t','e') @@ -1268,6 +1270,7 @@ # define c3__vent c3_s4('v','e','n','t') # define c3__verb c3_s4('v','e','r','b') # define c3__vere c3_s4('v','e','r','e') +# define c3__veri c3_s4('v','e','r','i') # define c3__vern c3_s4('v','e','r','n') # define c3__very c3_s4('v','e','r','y') # define c3__view c3_s4('v','i','e','w') diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 5a09608a0d..02f4afbc0f 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1886,28 +1886,62 @@ _hear_peer(u3_mesa* sam_u, u3_peer* per_u, u3_lane lan_u, c3_o dir_o) } } -static u3_pend_req* +static void +_mesa_request_next_fragments(u3_mesa* sam_u, + u3_pend_req* req_u, + u3_lane lan_u) +{ + c3_w win_w = _mesa_req_get_cwnd(req_u); + u3_mesa_pict* nex_u = req_u->pic_u; + c3_w nex_w = req_u->nex_w; + for ( int i = 0; i < win_w; i++ ) { + c3_w fra_w = nex_w + i; + if ( fra_w >= req_u->tot_w ) { + break; + } + nex_u->pac_u.pek_u.nam_u.fra_w = nex_w + i; + _mesa_add_our_to_pit(sam_u, &nex_u->pac_u.pek_u.nam_u); + _mesa_send(nex_u, &lan_u); + _mesa_req_pact_sent(req_u, &nex_u->pac_u.pek_u.nam_u); + } +} + +typedef struct _u3_mesa_veri_cb_data { + u3_mesa* sam_u; + u3_mesa_name nam_u; + u3_lane lan_u; +} u3_mesa_veri_cb_data; + +static void +_mesa_veri_scry_cb(void* vod_p, u3_noun nun) +{ + u3_mesa_veri_cb_data* ver_u = vod_p; + u3_pend_req* req_u = _mesa_get_request(ver_u->sam_u, &ver_u->nam_u); + if ( !req_u ) { + return; + } + else if ( c3y == nun ) { + // TODO: also call _mesa_dear? + _mesa_request_next_fragments(ver_u->sam_u, req_u, ver_u->lan_u); + } + else if ( c3n == nun ) { + u3l_log("mesa: packet auth failed verification"); + // TODO: wipe request state? (If this was an imposter, + // we don't want to punish the real peer.) + } + else { + u3l_log("mesa: %%veri returned strange value"); + } +} + +static void _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) { u3_mesa_pact* pac_u = &pic_u->pac_u; u3_mesa_name* nam_u = &pac_u->pag_u.nam_u; u3_mesa_data* dat_u = &pac_u->pag_u.dat_u; - c3_o lin_o = dat_u->tot_w <= 4 ? c3y : c3n; - - u3_pend_req* req_u = _mesa_get_request(sam_u, nam_u); - if ( NULL != req_u ) { - // duplicate - if ( req_u->tot_w <= 4 ) { - return NULL; - } - return NULL; - } else { - req_u = alloca(sizeof(u3_pend_req)); - memset(req_u, 0, sizeof(u3_pend_req)); - } u3_gage* gag_u = _mesa_get_lane(sam_u, nam_u->her_u, lan_u); - if ( gag_u == NULL ) { gag_u = alloca(sizeof(u3_gage)); _init_gage(gag_u); @@ -1917,6 +1951,8 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) u3_assert( gag_u != NULL ); } + u3_pend_req* req_u = alloca(sizeof(u3_pend_req)); + memset(req_u, 0, sizeof(u3_pend_req)); req_u->pic_u = c3_calloc(sizeof(u3_mesa_pict)); req_u->pic_u->sam_u = sam_u; req_u->pic_u->pac_u.hed_u.typ_y = PACT_PEEK; @@ -1924,7 +1960,7 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) memcpy(&req_u->pic_u->pac_u.pek_u.nam_u, nam_u, sizeof(u3_mesa_name)); req_u->pic_u->pac_u.pek_u.nam_u.aut_o = c3n; req_u->pic_u->pac_u.pek_u.nam_u.nit_o = c3n; - req_u->aum_u = pac_u->pag_u.dat_u.aum_u; + req_u->aum_u = dat_u->aum_u; c3_w siz_w = 1 << (pac_u->pag_u.nam_u.boq_y - 3); u3_assert( siz_w == 1024 ); // boq_y == 13 @@ -1937,79 +1973,71 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) // TODO: handle restart // u3_assert( nam_u->fra_w == 0 ); + c3_o lin_o = dat_u->tot_w <= 4 ? c3y : c3n; req_u->nex_w = (c3y == lin_o) ? 1 : 0; req_u->len_w = (c3y == lin_o) ? 1 : 0; req_u->lef_w = 0; req_u->old_w = 0; req_u->ack_w = 0; + c3_w pof_w = lss_proof_size(req_u->tot_w); + lss_hash* pof_u = c3_calloc(pof_w * sizeof(lss_hash)); if ( c3y == lin_o ) { - // complete the proof by computing the first leaf hash - c3_w pof_w = pac_u->pag_u.dat_u.aup_u.len_y + 1; - if ( pof_w != lss_proof_size(req_u->tot_w) ) { - u3l_log("hello"); - return NULL; // XX ??? + if ( pof_w != (dat_u->aup_u.len_y + 1) ) { + return; // TODO: handle like other auth failures } - lss_hash* pof_u = c3_calloc(pof_w * sizeof(lss_hash)); for ( int i = 1; i < pof_w; i++ ) { - memcpy(pof_u[i], pac_u->pag_u.dat_u.aup_u.has_y[i-1], sizeof(lss_hash)); + memcpy(pof_u[i], dat_u->aup_u.has_y[i-1], sizeof(lss_hash)); } + // complete the proof by computing the first leaf hash lss_complete_inline_proof(pof_u, dat_u->fra_y, dat_u->len_w); - // TODO: authenticate root - lss_hash root; - lss_root(root, pof_u, pof_w); - - req_u->los_u = c3_calloc(sizeof(lss_verifier)); - lss_verifier_init(req_u->los_u, 0, req_u->tot_w, pof_u); - c3_free(pof_u); - if ( c3y != lss_verifier_ingest(req_u->los_u, dat_u->fra_y, dat_u->len_w, NULL) ) { - u3l_log("hello2"); - return NULL; // XX ??? - } - memcpy(req_u->dat_y, dat_u->fra_y, dat_u->len_w); } else { - c3_w pof_w = lss_proof_size(req_u->tot_w); if ( dat_u->len_w != pof_w*sizeof(lss_hash) ) { - return NULL; // XX ??? + return; // TODO: handle like other auth failures } - // TODO: cast directly instead of copying? - lss_hash* pof_u = c3_calloc(pof_w * sizeof(lss_hash)); for ( int i = 0; i < pof_w; i++ ) { memcpy(pof_u[i], dat_u->fra_y + (i * sizeof(lss_hash)), sizeof(lss_hash)); } - // TODO: authenticate root - lss_hash root; - lss_root(root, pof_u, pof_w); + } + lss_hash root; + lss_root(root, pof_u, pof_w); + req_u->los_u = c3_calloc(sizeof(lss_verifier)); + lss_verifier_init(req_u->los_u, 0, req_u->tot_w, pof_u); + c3_free(pof_u); - req_u->los_u = c3_calloc(sizeof(lss_verifier)); - lss_verifier_init(req_u->los_u, 0, req_u->tot_w, pof_u); - c3_free(pof_u); + if ( c3y == lin_o ) { + if ( c3y != lss_verifier_ingest(req_u->los_u, dat_u->fra_y, dat_u->len_w, NULL) ) { + return; // TODO: handle like other auth failures + } + memcpy(req_u->dat_y, dat_u->fra_y, dat_u->len_w); } - memset(req_u->mis_u, 0, sizeof(req_u->mis_u)); req_u = _mesa_put_request(sam_u, nam_u, req_u); _update_resend_timer(req_u); - return req_u; -} -static void -_mesa_request_next_fragments(u3_mesa* sam_u, - u3_pend_req* req_u, - u3_lane lan_u) -{ - c3_w win_w = _mesa_req_get_cwnd(req_u); - u3_mesa_pict* nex_u = req_u->pic_u; - c3_w nex_w = req_u->nex_w; - for ( int i = 0; i < win_w; i++ ) { - c3_w fra_w = nex_w + i; - if ( fra_w >= req_u->tot_w ) { - break; - } - nex_u->pac_u.pek_u.nam_u.fra_w = nex_w + i; - _mesa_add_our_to_pit(sam_u, &nex_u->pac_u.pek_u.nam_u); - _mesa_send(nex_u, &lan_u); - _mesa_req_pact_sent(req_u, &nex_u->pac_u.pek_u.nam_u); - } + // scry to verify auth + u3_noun typ, aut; + switch ( dat_u->aum_u.typ_e ) { + case AUTH_SIGN: + typ = c3__sign; + aut = u3dc("scot", c3__uv, u3i_bytes(64, dat_u->aum_u.sig_y)); + break; + case AUTH_HMAC: + typ = c3__hmac; + aut = u3dc("scot", c3__uv, u3i_bytes(32, dat_u->aum_u.mac_y)); + break; + default: + return; // TODO: handle like other auth failures + } + u3_noun her = u3dc("scot", c3__p, u3_ship_to_noun(nam_u->her_u)); + u3_noun rut = u3dc("scot", c3__uv, u3i_bytes(32, root)); + u3_noun pax = _mesa_encode_path(nam_u->pat_s, (c3_y*)nam_u->pat_c); + u3_noun sky = u3i_list(typ, her, aut, rut, pax, u3_none); + u3_mesa_veri_cb_data* ver_u = c3_malloc(sizeof(u3_mesa_veri_cb_data)); + ver_u->sam_u = sam_u; + memcpy(&ver_u->nam_u, nam_u, sizeof(u3_mesa_name)); + ver_u->lan_u = *lan_u; + u3_pier_peek_last(sam_u->pir_u, u3_nul, c3__a, c3__veri, sky, ver_u, _mesa_veri_scry_cb); } typedef struct _u3_mesa_lane_cb_data { @@ -2168,42 +2196,29 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) return; } - u3_pend_req* req_u; - - if ( c3y == nam_u->nit_o ) { - #ifdef MESA_LOG - u3l_log(" NIT"); - #endif - req_u = _mesa_req_pact_init(sam_u, pic_u, &lan_u); - if ( req_u == NULL ) { - #ifdef MESA_LOG - u3l_log(" init fail"); - #endif - _mesa_free_pict(pic_u); - return; - } - } else { - req_u = _mesa_get_request(sam_u, nam_u); - if ( !req_u ) { - _mesa_free_pict(pic_u); - // TODO free pin, other things too? - return; + u3_pend_req* req_u = _mesa_get_request(sam_u, nam_u); + if ( !req_u ) { + if ( c3y == nam_u->nit_o ) { + _mesa_req_pact_init(sam_u, pic_u, &lan_u); } + _mesa_free_pict(pic_u); + // TODO free pin, other things too? + return; + } - u3_lane lon_u; - if ( HOP_SHORT == pac_u->hed_u.nex_y ) { - lon_u.pip_w = c3_sift_word(pac_u->pag_u.sot_u); - lon_u.por_s = c3_sift_short(pac_u->pag_u.sot_u + 4); - } - else { - lon_u = lan_u; - } - _mesa_req_pact_done(req_u, - nam_u, - &pac_u->pag_u.dat_u, - pac_u->hed_u.hop_y, - lon_u); + u3_lane lon_u; + if ( HOP_SHORT == pac_u->hed_u.nex_y ) { + lon_u.pip_w = c3_sift_word(pac_u->pag_u.sot_u); + lon_u.por_s = c3_sift_short(pac_u->pag_u.sot_u + 4); + } + else { + lon_u = lan_u; } + _mesa_req_pact_done(req_u, + nam_u, + &pac_u->pag_u.dat_u, + pac_u->hed_u.hop_y, + lon_u); c3_o done_with_jumbo_frame = __(req_u->len_w == req_u->tot_w); // TODO: fix for non-message-sized jumbo frames if ( c3y == done_with_jumbo_frame ) { From b80e99a3906b2f85a25398424997744866cc8c08 Mon Sep 17 00:00:00 2001 From: Sigilante Date: Tue, 23 Jul 2024 10:01:31 -0500 Subject: [PATCH 266/430] WIP jet hint call stack correct --- pkg/noun/jets/i/lagoon.c | 7 +++---- pkg/noun/jets/tree.c | 7 +------ 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/pkg/noun/jets/i/lagoon.c b/pkg/noun/jets/i/lagoon.c index 9ead70d14a..6bf53d4a1e 100644 --- a/pkg/noun/jets/i/lagoon.c +++ b/pkg/noun/jets/i/lagoon.c @@ -121,7 +121,6 @@ if (bloq < 4 || bloq > 7) { return u3_none; } - fprintf(stderr, ">>> u3qi_la_add\n\r"); // Unpack the data as a byte array. We assume total length < 2**64. // len_x is length in base units @@ -2134,7 +2133,6 @@ // Each argument is a ray, [=meta data=@ux] u3_noun x_meta, x_data, y_meta, y_data; - fprintf(stderr, "> u3wi_la_add\n\r"); if ( c3n == u3r_mean(cor, u3x_sam_4, &x_meta, @@ -2159,6 +2157,7 @@ y_kind = u3h(u3t(u3t(y_meta))); // 14 y_fxp = u3t(u3t(u3t(y_meta))); // 15 rnd = u3h(u3t(u3t(u3t(cor)))); // 30 + fprintf(stderr, "> u3wi_la_add\r\n"); if ( c3n == u3ud(x_bloq) || c3n == u3ud(y_bloq) || c3n == u3ud(x_kind) || @@ -2171,15 +2170,15 @@ { return u3m_bail(c3__exit); } else { - fprintf(stderr, "> u3wi_la_add\n\r"); + fprintf(stderr, "x_bloq: %x\r\n", x_kind); switch (x_kind) { case c3__i754: _set_rounding(rnd); - fprintf(stderr, ">> u3wi_la_add\n\r"); u3_noun r_data = _soft_run(u3qi_la_add_i754(x_data, y_data, x_shape, x_bloq)); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: + fprintf(stderr, "default\r\n"); return u3_none; } } diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index 8ee3037fae..7b6224ba97 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -2185,13 +2185,8 @@ static u3j_core _139_non__la_core_d[] = {} }; -static u3j_core _139_non__lagoon_d[] = - { { "la-core", 7, 0, _139_non__la_core_d, no_hashes }, - {} - }; - static u3j_core _139_non_d[] = - { { "lagoon", 6, 0, _139_non__lagoon_d, no_hashes }, + { { "lagoon", 7, 0, _139_non__la_core_d, no_hashes }, {} }; From 439fdf335a1ace4496796e702bd8eb169718e5c3 Mon Sep 17 00:00:00 2001 From: Sigilante Date: Tue, 23 Jul 2024 10:57:41 -0500 Subject: [PATCH 267/430] WIP debugging u3_none path --- pkg/noun/jets/i/lagoon.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/pkg/noun/jets/i/lagoon.c b/pkg/noun/jets/i/lagoon.c index 6bf53d4a1e..eb92dc447f 100644 --- a/pkg/noun/jets/i/lagoon.c +++ b/pkg/noun/jets/i/lagoon.c @@ -2157,7 +2157,6 @@ y_kind = u3h(u3t(u3t(y_meta))); // 14 y_fxp = u3t(u3t(u3t(y_meta))); // 15 rnd = u3h(u3t(u3t(u3t(cor)))); // 30 - fprintf(stderr, "> u3wi_la_add\r\n"); if ( c3n == u3ud(x_bloq) || c3n == u3ud(y_bloq) || c3n == u3ud(x_kind) || @@ -2170,7 +2169,7 @@ { return u3m_bail(c3__exit); } else { - fprintf(stderr, "x_bloq: %x\r\n", x_kind); + fprintf(stderr, "\r\nx_kind: %x\r\n", x_kind); switch (x_kind) { case c3__i754: _set_rounding(rnd); @@ -2178,7 +2177,7 @@ return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: - fprintf(stderr, "default\r\n"); + fprintf(stderr, "uint\r\n"); return u3_none; } } @@ -3234,15 +3233,16 @@ u3r_bytes(0, 8, (c3_y*)&d_, d); n_ = f64_to_i64(f64_div(f64_sub((float64_t){b_}, (float64_t){a_}), (float64_t){d_}), softfloat_round_minMag, false); break; - case 7: - u3r_bytes(0, 16, (c3_y*)&a_, a); - u3r_bytes(0, 16, (c3_y*)&b_, b); - u3r_bytes(0, 16, (c3_y*)&d_, d); + case 7: { + c3_d a__[2], b__[2], d__[2]; + u3r_bytes(0, 16, (c3_y*)&a__, a); + u3r_bytes(0, 16, (c3_y*)&b__, b); + u3r_bytes(0, 16, (c3_y*)&d__, d); float128_t tmp; - f128M_sub((float128_t*){&b_}, (float128_t*){&a_}, &tmp); - f128M_div(&tmp, (float128_t*){&d_}, &tmp); + f128M_sub((float128_t*){&b__}, (float128_t*){&a__}, &tmp); + f128M_div(&tmp, (float128_t*){&d__}, &tmp); n_ = f128M_to_i64(&tmp, softfloat_round_minMag, false); - break; + break;} } u3_noun n = u3i_chub(n_+1); x_shape = u3nt(u3k(n), 0x1, u3_nul); From 470ebe9edd9ea24188ac711b91433bcd8b42d4bb Mon Sep 17 00:00:00 2001 From: midden-fabler Date: Wed, 24 Jul 2024 09:34:56 -0400 Subject: [PATCH 268/430] http: add _get_range, slice in _http_scry_cb (WIP) --- pkg/vere/io/http.c | 253 ++++++++++++++++++++++++++------------------- 1 file changed, 145 insertions(+), 108 deletions(-) diff --git a/pkg/vere/io/http.c b/pkg/vere/io/http.c index ba4c64d5f9..cdfc2a2094 100644 --- a/pkg/vere/io/http.c +++ b/pkg/vere/io/http.c @@ -7,7 +7,6 @@ #include "openssl/err.h" #include "openssl/ssl.h" #include "version.h" -#include typedef struct _u3_h2o_serv { h2o_globalconf_t fig_u; // h2o global config @@ -672,54 +671,57 @@ _content_headers(c3_z beg_z, c3_z end_z, c3_w tot_w) return out; } -/* _http_scry_cb() +typedef struct _range_request { + c3_z beg_z; + c3_z end_z; +} range_request; + +/* _parse_range: get a range from '-' delimited text */ -static void -_http_scry_cb(void* vod_p, u3_noun nun) +static range_request +_parse_range(c3_c* txt_c, c3_w len_w) { - // XX slice here - u3_preq* peq_u = vod_p; - u3_httd* htd_u = peq_u->htd_u; - u3_hreq* req_u = peq_u->req_u; + c3_c* hep_c = memchr(txt_c, '-', len_w); + range_request cut; + cut.beg_z = SIZE_MAX; + cut.end_z = SIZE_MAX; - if ( req_u ) { - u3_assert(u3_rsat_peek == req_u->sat_e); - req_u->peq_u = 0; - if ( u3_nul != nun ) { - u3_noun len = u3r_at(254, nun); - c3_w len_w; - if ( c3n == u3r_safe_word(len, &len_w) ) { - u3z(nun); - _http_scry_respond(req_u, u3_nul); - u3z(peq_u->pax); - c3_free(peq_u); - return; - } - u3_noun hez = _content_headers(0, (len_w - 1), len_w); - u3_noun res = u3i_edit(u3k(nun), 125, u3qb_weld(hez, u3r_at(125, nun))); - _http_scry_respond(req_u, u3k(res)); - } - else { - _http_scry_respond(req_u, u3k(nun)); + if ( hep_c ) { + cut.beg_z = h2o_strtosize(txt_c, hep_c - txt_c); + cut.end_z = h2o_strtosize(hep_c + 1, len_w - ((hep_c + 1) - txt_c)); + // strange -> [SIZE_MAX SIZE_MAX] so we return u3_nul in _slice_octs + if ( ((SIZE_MAX == cut.beg_z) && (hep_c != txt_c)) + || ((SIZE_MAX == cut.end_z) && (len_w - ((hep_c + 1) - txt_c) > 0)) ) + { + cut.beg_z = SIZE_MAX; + cut.end_z = SIZE_MAX; } } + return cut; +} - // cache only if peek was not at now, and nun isn't u3_nul - if ( (c3n == peq_u->las_o) - && (u3_nul != nun) ) +static c3_o +_get_range(h2o_headers_t req_headers, range_request* rng_req) +{ + rng_req->beg_z = SIZE_MAX; + rng_req->end_z = SIZE_MAX; + + c3_w idx = h2o_find_header(&req_headers, H2O_TOKEN_RANGE, -1); + if (idx == UINT32_MAX) { + return c3n; + } + + if ( (req_headers.entries[idx].value.len >= 6) + && (0 == memcmp("bytes=", req_headers.entries[idx].value.base, 6)) ) { - // XX pair of auth & path for key - // check ~watter-parter's - u3h_put(htd_u->nax_p, peq_u->pax, nun); + range_request tmp = _parse_range(req_headers.entries[idx].value.base + 6, + req_headers.entries[idx].value.len - 6); + rng_req->beg_z = tmp.beg_z; + rng_req->end_z = tmp.end_z; } - u3z(peq_u->pax); - c3_free(peq_u); -} -typedef struct _range_request { - c3_z beg_z; - c3_z end_z; -} range_request; + return c3y; +} typedef struct _content { c3_z beg_z; @@ -727,10 +729,10 @@ typedef struct _content { u3_noun dat; // XX free } content; -/* _slice_mime: given a valid range, slice a section of octs +/* _slice_octs: given a valid range, slice a section of octs */ static content -_slice_mime(range_request rng, u3_noun octs) +_slice_octs(range_request rng, u3_noun octs) { content out; out.beg_z = SIZE_MAX; @@ -776,28 +778,74 @@ _slice_mime(range_request rng, u3_noun octs) return out; } -/* _parse_range: get a range from '-' delimited text +/* _http_scry_cb() */ -static range_request -_parse_range(c3_c* txt_c, c3_w len_w) +static void +_http_scry_cb(void* vod_p, u3_noun nun) { - c3_c* hep_c = memchr(txt_c, '-', len_w); - range_request cut; - cut.beg_z = SIZE_MAX; - cut.end_z = SIZE_MAX; + // XX free + u3_preq* peq_u = vod_p; + u3_httd* htd_u = peq_u->htd_u; + u3_hreq* req_u = peq_u->req_u; - if ( hep_c ) { - cut.beg_z = h2o_strtosize(txt_c, hep_c - txt_c); - cut.end_z = h2o_strtosize(hep_c + 1, len_w - ((hep_c + 1) - txt_c)); - // strange -> [SIZE_MAX SIZE_MAX] so we return u3_nul in _slice_mime - if ( ((SIZE_MAX == cut.beg_z) && (hep_c != txt_c)) - || ((SIZE_MAX == cut.end_z) && (len_w - ((hep_c + 1) - txt_c) > 0)) ) - { - cut.beg_z = SIZE_MAX; - cut.end_z = SIZE_MAX; + if ( req_u ) { + u3_assert(u3_rsat_peek == req_u->sat_e); + req_u->peq_u = 0; + if ( u3_nul == nun ) { + _http_scry_respond(req_u, u3k(nun)); + } + else { + h2o_headers_t req_headers = req_u->rec_u->headers; + range_request rng_req; + c3_o rng_o = _get_range(req_headers, &rng_req); + + if (c3n == rng_o ) { + // XX review + u3_noun len = u3r_at(254, nun); + c3_w len_w; + if ( c3n == u3r_safe_word(len, &len_w) ) { + u3z(nun); + _http_scry_respond(req_u, u3_nul); + u3z(peq_u->pax); + c3_free(peq_u); + return; + } + u3_noun hez = _content_headers(0, (len_w - 1), len_w); + u3_noun res = u3i_edit(u3k(nun), 125, u3qb_weld(hez, u3r_at(125, nun))); + _http_scry_respond(req_u, u3k(res)); + } + else { + u3_noun octs = u3r_at(127, nun); + if ( u3_none == octs ) { + h2o_send_error_500(req_u->rec_u, "Internal Server Error", "scry failed", 0); + } + content result = _slice_octs(rng_req, octs); + if ( u3_nul == result.dat ) { + c3_c* msg_c = "Requested Range Not Satisfiable"; + u3z(result.dat); + h2o_send_error_generic(req_u->rec_u, 416, msg_c, msg_c, 0); + } + else { + u3_noun hez = _content_headers(result.beg_z, result.end_z, u3h(octs)); + u3_noun res = u3i_edit(nun, 127, result.dat); + res = u3i_edit(res, 124, 206); + res = u3i_edit(res, 125, u3qb_weld(hez, u3r_at(125, res))); + _http_scry_respond(req_u, res); + } + } } } - return cut; + + // cache only if peek was not at now, and nun isn't u3_nul + if ( (c3n == peq_u->las_o) + && (u3_nul != nun) ) + { + // XX pair of auth & path for key + // check ~watter-parter's + u3h_put(htd_u->nax_p, peq_u->pax, nun); + } + u3z(peq_u->pax); + c3_free(peq_u); } typedef struct _beam { @@ -807,7 +855,7 @@ typedef struct _beam { u3_noun pur; } beam; -/* _get_beam: url to beam +/* _get_beam: path to beam */ static beam _get_beam(u3_hreq* req_u, c3_c* txt_c, c3_w len_w) @@ -967,7 +1015,7 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) u3_noun spur = u3nc(u3i_string("mime"), u3t(bem.pur)); if ( c3y == req_u->peq_u->las_o ) { u3_pier_peek_last(htd_u->car_u.pir_u, gang, c3__ex, - bem.des, spur, req_u->peq_u, _http_scry_cb); + bem.des, spur, req_u->peq_u, _http_scry_cb); } else { @@ -981,64 +1029,53 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) // maybe cache, then serve subsequent range requests from cache req_u->peq_u->pax = bam; u3_pier_peek(htd_u->car_u.pir_u, gang, u3nt(0, c3__ex, u3k(bam)), - req_u->peq_u, _http_scry_cb); + req_u->peq_u, _http_scry_cb); } else { - // XX function h2o_headers_t req_headers = req_u->rec_u->headers; - c3_w idx = h2o_find_header(&req_headers, H2O_TOKEN_RANGE, -1); + range_request rng_req; + c3_o rng_o = _get_range(req_headers, &rng_req); - if (idx == UINT32_MAX) { + if ( c3n == rng_o) { _http_cache_respond(req_u, nac); } else { - if ( (req_headers.entries[idx].value.len >= 6) && - (0 == memcmp("bytes=", req_headers.entries[idx].value.base, 6)) ) { - c3_w rest_len = req_headers.entries[idx].value.len - 6; - if ( 0 == rest_len ) { - c3_c* msg_c = "Requested Range Not Satisfiable"; - h2o_send_error_generic(req_u->rec_u, 416, msg_c, msg_c, 0); - // XX leaks - return; - } - range_request rng_req = _parse_range(req_headers.entries[idx].value.base + 6, rest_len); - u3_noun octs = u3r_at(127, nac); - if ( u3_none == octs ) { - h2o_send_error_500(rec_u, "Internal Server Error", "scry failed", 0); - return; - } - content result = _slice_mime(rng_req, octs); + u3_noun octs = u3r_at(127, nac); + if ( u3_none == octs ) { + h2o_send_error_500(rec_u, "Internal Server Error", "scry failed", 0); + return; + } + content result = _slice_octs(rng_req, octs); - if ( u3_nul == result.dat ) { - c3_c* msg_c = "Requested Range Not Satisfiable"; - u3z(result.dat); - h2o_send_error_generic(req_u->rec_u, 416, msg_c, msg_c, 0); - return; - } + if ( u3_nul == result.dat ) { + c3_c* msg_c = "Requested Range Not Satisfiable"; + u3z(result.dat); + h2o_send_error_generic(req_u->rec_u, 416, msg_c, msg_c, 0); + return; + } - if ( c3y == u3r_sing(result.dat, octs) ) { - // 200 - u3z(result.dat); - u3_noun len = u3r_at(254, nac); - c3_w len_w; - if ( c3n == u3r_safe_word(len, &len_w) ) { - u3z(nac); - _http_scry_respond(req_u, u3_nul); - return; - } - u3_noun hez = _content_headers(0, (len_w - 1), len_w); - u3_noun res = u3i_edit(nac, 125, u3qb_weld(hez, u3r_at(125, nac))); - _http_cache_respond(req_u, res); - } - else { - // 206 - u3_noun hez = _content_headers(result.beg_z, result.end_z, u3h(octs)); - u3_noun res = u3i_edit(nac, 127, result.dat); - res = u3i_edit(res, 124, 206); - res = u3i_edit(res, 125, u3qb_weld(hez, u3r_at(125, res))); - _http_cache_respond(req_u, res); + if ( c3y == u3r_sing(result.dat, octs) ) { + // 200 + u3z(result.dat); + u3_noun len = u3r_at(254, nac); + c3_w len_w; + if ( c3n == u3r_safe_word(len, &len_w) ) { + u3z(nac); + _http_scry_respond(req_u, u3_nul); + return; } + u3_noun hez = _content_headers(0, (len_w - 1), len_w); + u3_noun res = u3i_edit(nac, 125, u3qb_weld(hez, u3r_at(125, nac))); + _http_cache_respond(req_u, res); + } + else { + // 206 + u3_noun hez = _content_headers(result.beg_z, result.end_z, u3h(octs)); + u3_noun res = u3i_edit(nac, 127, result.dat); + res = u3i_edit(res, 124, 206); + res = u3i_edit(res, 125, u3qb_weld(hez, u3r_at(125, res))); + _http_cache_respond(req_u, res); } } } From 93ae4cc01eec0216344e025aa4620e5b6a7eb3b6 Mon Sep 17 00:00:00 2001 From: lukechampine Date: Wed, 24 Jul 2024 12:42:49 -0400 Subject: [PATCH 269/430] mesa: make all macro parameters explicit --- pkg/vere/io/mesa.c | 14 +++++++------- pkg/vere/io/mesa/pact.c | 39 +++++++++++++++++++-------------------- 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 02f4afbc0f..68e377db7b 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -35,9 +35,9 @@ c3_o dop_o = c3n; #define MESA_SYM_DESC(SYM) MESA_DESC_ ## SYM #define MESA_SYM_FIELD(SYM) MESA_FIELD_ ## SYM #ifdef MESA_DEBUG - #define MESA_LOG(SYM, ...) { sam_u->sat_u.MESA_SYM_FIELD(SYM)++; u3l_log("mesa: (%u) %s", __LINE__, MESA_SYM_DESC(SYM)); } + #define MESA_LOG(SAM_U, SYM, ...) { SAM_U->sat_u.MESA_SYM_FIELD(SYM)++; u3l_log("mesa: (%u) %s", __LINE__, MESA_SYM_DESC(SYM)); } #else - #define MESA_LOG(SYM, ...) { sam_u->sat_u.MESA_SYM_FIELD(SYM)++; } + #define MESA_LOG(SAM_U, SYM, ...) { SAM_U->sat_u.MESA_SYM_FIELD(SYM)++; } #endif typedef struct _u3_mesa_stat { @@ -1098,14 +1098,14 @@ _mesa_req_pact_done(u3_pend_req* req_u, // received past the end of the message if ( dat_u->tot_w <= nam_u->fra_w ) { u3l_log("strange tot_w %u fra_w %u req_u %u", dat_u->tot_w, nam_u->fra_w, req_u->len_w); - MESA_LOG(STRANGE); + MESA_LOG(sam_u, STRANGE); // XX: is this sufficient to drop whole request return; } // received duplicate if ( c3n == bitset_has(&req_u->was_u, nam_u->fra_w) ) { - MESA_LOG(DUPE); + MESA_LOG(sam_u, DUPE); return; } @@ -1155,12 +1155,12 @@ _mesa_req_pact_done(u3_pend_req* req_u, c3_free(par_u); // TODO: do we drop the whole request on the floor? u3l_log("auth fail frag %u", nam_u->fra_w); - MESA_LOG(AUTH); + MESA_LOG(sam_u, AUTH); return; } else if ( c3y != _mesa_burn_misorder_queue(req_u) ) { c3_free(par_u); - MESA_LOG(AUTH) + MESA_LOG(sam_u, AUTH) return; } else { @@ -2520,7 +2520,7 @@ _mesa_hear(u3_mesa* sam_u, c3_w lin_w = mesa_sift_pact(&pic_u->pac_u, hun_y, len_w); if ( lin_w == 0 ) { - // MESA_LOG(SERIAL) + // MESA_LOG(sam_u, SERIAL) // c3_free(hun_y); mesa_free_pact(&pic_u->pac_u); _ames_hear(u3_Host.sam_u, lan_u, len_w, hun_y); diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index 6ff4bf038a..c959e01e7e 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -9,8 +9,7 @@ #define DEF_TEXT "\033[0m" // endif tests -#define SIFT_VAR(dest, src, len) dest = 0; for(int i = 0; i < len; i++ ) { dest |= ((src + i) >> (8*i)); } -#define CHECK_BOUNDS(cur) if ( len_w < cur ) { u3l_log("mesa: failed parse (%u,%u) at line %i", len_w, cur, __LINE__); return 0; } +#define CHECK_BOUNDS(len_w, cur) if ( len_w < cur ) { u3l_log("mesa: failed parse (%u,%u) at line %i", len_w, cur, __LINE__); return 0; }; #define safe_dec(num) (num == 0 ? num : num - 1) #define _mesa_met3_w(a_w) ((c3_bits_word(a_w) + 0x7) >> 3) @@ -272,7 +271,7 @@ _mesa_sift_name(u3_mesa_name* nam_u, c3_y* buf_y, c3_w len_w) c3_w cur_w = 0; u3_mesa_name_meta met_u; - CHECK_BOUNDS(cur_w + 1); + CHECK_BOUNDS(len_w, cur_w + 1); c3_y met_y = buf_y[cur_w]; met_u.ran_y = (met_y >> 0) & 0x3; met_u.rif_y = (met_y >> 2) & 0x3; @@ -282,19 +281,19 @@ _mesa_sift_name(u3_mesa_name* nam_u, c3_y* buf_y, c3_w len_w) cur_w += 1; c3_y her_y = 2 << met_u.ran_y; - CHECK_BOUNDS(cur_w + her_y) + CHECK_BOUNDS(len_w, cur_w + her_y); u3_ship_of_bytes(nam_u->her_u, her_y, buf_y + cur_w); cur_w += her_y; c3_y rif_y = met_u.rif_y + 1; nam_u->rif_w = 0; - CHECK_BOUNDS(cur_w + rif_y) + CHECK_BOUNDS(len_w, cur_w + rif_y); for( int i = 0; i < rif_y; i++ ) { nam_u->rif_w |= (buf_y[cur_w] << (8*i)); cur_w++; } - CHECK_BOUNDS(cur_w + 1); + CHECK_BOUNDS(len_w, cur_w + 1); nam_u->boq_y = buf_y[cur_w]; cur_w++; @@ -305,7 +304,7 @@ _mesa_sift_name(u3_mesa_name* nam_u, c3_y* buf_y, c3_w len_w) } else { c3_y fag_y = met_u.gaf_y + 1; - CHECK_BOUNDS(cur_w + fag_y); + CHECK_BOUNDS(len_w, cur_w + fag_y); for ( int i = 0; i < fag_y; i++ ) { nam_u->fra_w |= (buf_y[cur_w] << (8*i)); cur_w++; @@ -316,13 +315,13 @@ _mesa_sift_name(u3_mesa_name* nam_u, c3_y* buf_y, c3_w len_w) // XX ?:(=(1 tau.c) %auth %data) nam_u->aut_o = ( met_u.tau_y ) ? c3y : c3n; - CHECK_BOUNDS(cur_w + 2) + CHECK_BOUNDS(len_w, cur_w + 2); nam_u->pat_s = buf_y[cur_w] | (buf_y[cur_w + 1] << 8); cur_w += 2; nam_u->pat_c = c3_calloc(nam_u->pat_s + 1); // unix string for ease of manipulation - CHECK_BOUNDS(cur_w + nam_u->pat_s); + CHECK_BOUNDS(len_w, cur_w + nam_u->pat_s); memcpy(nam_u->pat_c, buf_y + cur_w, nam_u->pat_s); nam_u->pat_c[nam_u->pat_s] = 0; cur_w += nam_u->pat_s; @@ -340,7 +339,7 @@ _mesa_sift_data(u3_mesa_data* dat_u, c3_y* buf_y, c3_w len_w) c3_w cur_w = 0; u3_mesa_data_meta met_u; - CHECK_BOUNDS(cur_w + 1); + CHECK_BOUNDS(len_w, cur_w + 1); c3_y met_y = buf_y[cur_w]; met_u.bot_y = (met_y >> 0) & 0x3; met_u.aul_y = (met_y >> 2) & 0x3; @@ -349,7 +348,7 @@ _mesa_sift_data(u3_mesa_data* dat_u, c3_y* buf_y, c3_w len_w) cur_w += 1; c3_y tot_y = met_u.bot_y + 1; - CHECK_BOUNDS(cur_w + tot_y); + CHECK_BOUNDS(len_w, cur_w + tot_y); dat_u->tot_w = 0; for( int i = 0; i < tot_y; i++ ) { dat_u->tot_w |= (buf_y[cur_w] << (8*i)); @@ -358,7 +357,7 @@ _mesa_sift_data(u3_mesa_data* dat_u, c3_y* buf_y, c3_w len_w) c3_y aum_y = ( 2 == met_u.aul_y ) ? 64 : ( 3 == met_u.aul_y ) ? 32 : 0; - CHECK_BOUNDS(cur_w + aum_y); + CHECK_BOUNDS(len_w, cur_w + aum_y); memcpy(dat_u->aum_u.sig_y, buf_y + cur_w, aum_y); cur_w += aum_y; @@ -366,7 +365,7 @@ _mesa_sift_data(u3_mesa_data* dat_u, c3_y* buf_y, c3_w len_w) assert( 3 > met_u.aur_y ); - CHECK_BOUNDS(cur_w + (met_u.aur_y * 32)); + CHECK_BOUNDS(len_w, cur_w + (met_u.aur_y * 32)); dat_u->aup_u.len_y = met_u.aur_y; for( int i = 0; i < met_u.aur_y; i++ ) { memcpy(dat_u->aup_u.has_y[i], buf_y + cur_w, 32); @@ -376,19 +375,19 @@ _mesa_sift_data(u3_mesa_data* dat_u, c3_y* buf_y, c3_w len_w) c3_y nel_y = met_u.men_y; if ( 3 == nel_y ) { - CHECK_BOUNDS(cur_w + 1); + CHECK_BOUNDS(len_w, cur_w + 1); nel_y = buf_y[cur_w]; cur_w++; } - CHECK_BOUNDS(cur_w + nel_y); + CHECK_BOUNDS(len_w, cur_w + nel_y); dat_u->len_w = 0; for ( int i = 0; i < nel_y; i++ ) { dat_u->len_w |= (buf_y[cur_w] << (8*i)); cur_w++; } - CHECK_BOUNDS(cur_w + dat_u->len_w); + CHECK_BOUNDS(len_w, cur_w + dat_u->len_w); dat_u->fra_y = c3_calloc(dat_u->len_w); memcpy(dat_u->fra_y, buf_y + cur_w, dat_u->len_w); cur_w += dat_u->len_w; @@ -400,10 +399,10 @@ static c3_w _mesa_sift_hop_long(u3_mesa_hop_once* hop_u, c3_y* buf_y, c3_w len_w) { c3_w cur_w = 0; - CHECK_BOUNDS(cur_w + 1); + CHECK_BOUNDS(len_w, cur_w + 1); hop_u->len_w = buf_y[cur_w]; cur_w++; - CHECK_BOUNDS(cur_w + hop_u->len_w); + CHECK_BOUNDS(len_w, cur_w + hop_u->len_w); hop_u->dat_y = c3_calloc(hop_u->len_w); memcpy(hop_u->dat_y, buf_y + cur_w, hop_u->len_w); @@ -419,7 +418,7 @@ _mesa_sift_hops(u3_mesa_page_pact* pac_u, c3_y nex_y, c3_y* buf_y, c3_w len_w) } case HOP_NONE: return 0; case HOP_SHORT: { - CHECK_BOUNDS(6); + CHECK_BOUNDS(len_w, 6); memcpy(pac_u->sot_u, buf_y, 6); return 6; } @@ -428,7 +427,7 @@ _mesa_sift_hops(u3_mesa_page_pact* pac_u, c3_y nex_y, c3_y* buf_y, c3_w len_w) } case HOP_MANY: { c3_w siz_w = 0; - CHECK_BOUNDS(siz_w + 1); + CHECK_BOUNDS(len_w, siz_w + 1); pac_u->man_u.len_w = buf_y[0]; siz_w++; From 19fba9c6583660d3a6ee2130a7b78a19de678c1e Mon Sep 17 00:00:00 2001 From: lukechampine Date: Wed, 24 Jul 2024 12:51:43 -0400 Subject: [PATCH 270/430] mesa: remove duplicate u3_assert macro --- pkg/vere/io/mesa/bitset.c | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/pkg/vere/io/mesa/bitset.c b/pkg/vere/io/mesa/bitset.c index a706c6074b..22f687b25b 100644 --- a/pkg/vere/io/mesa/bitset.c +++ b/pkg/vere/io/mesa/bitset.c @@ -3,21 +3,6 @@ #include "vere.h" - -#define u3_assert(x) \ - do { \ - if (!(x)) { \ - fflush(stderr); \ - fprintf(stderr, "\rAssertion '%s' " \ - "failed in %s:%d\r\n", \ - #x, __FILE__, __LINE__); \ - /*u3m_bail(c3__oops); */ \ - /*abort(); */ \ - } \ - } while(0) - - - void bitset_init(u3_bitset* bit_u, c3_w len_w) { bit_u->len_w = len_w; From ecbaac3a2a0e1e8e1b0dee1714aa8edcd08c920c Mon Sep 17 00:00:00 2001 From: pkova Date: Wed, 24 Jul 2024 17:33:52 +0300 Subject: [PATCH 271/430] bazel: change openssl mirror to github since openssl.org is down --- WORKSPACE.bazel | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index 166915649e..66c04eed85 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -264,7 +264,7 @@ versioned_http_archive( build_file = "//bazel/third_party/openssl:openssl.BUILD", sha256 = "cf3098950cb4d853ad95c0841f1f9c6d3dc102dccfcacd521d93925208b76ac8", strip_prefix = "openssl-{version}", - url = "https://www.openssl.org/source/openssl-{version}.tar.gz", + url = "https://github.com/openssl/openssl/releases/download/OpenSSL_1_1_1w/openssl-{version}.tar.gz", version = "1.1.1w", ) From 24c23c1293fddfef54c810f17e3aa88b82bdd3d2 Mon Sep 17 00:00:00 2001 From: pkova Date: Mon, 22 Jul 2024 16:47:13 +0300 Subject: [PATCH 272/430] bazel: change mirror for libnatpmp because it's always down --- WORKSPACE.bazel | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index 66c04eed85..b045bd6d91 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -222,7 +222,7 @@ versioned_http_archive( build_file = "//bazel/third_party/natpmp:natpmp.BUILD", sha256 = "0684ed2c8406437e7519a1bd20ea83780db871b3a3a5d752311ba3e889dbfc70", strip_prefix = "libnatpmp-{version}", - url = "http://miniupnp.free.fr/files/libnatpmp-{version}.tar.gz", + url = "http://download.openpkg.org/components/cache/libnatpmp/libnatpmp-{version}.tar.gz", version = "20230423", ) From 20f30fc10ecef907375b1ca5f78313a5b9141a6f Mon Sep 17 00:00:00 2001 From: midden-fabler Date: Wed, 24 Jul 2024 13:53:57 -0400 Subject: [PATCH 273/430] http: add _http_range_respond --- pkg/vere/io/http.c | 114 +++++++++++++++++++++------------------------ 1 file changed, 52 insertions(+), 62 deletions(-) diff --git a/pkg/vere/io/http.c b/pkg/vere/io/http.c index cdfc2a2094..525ad353c4 100644 --- a/pkg/vere/io/http.c +++ b/pkg/vere/io/http.c @@ -778,12 +778,52 @@ _slice_octs(range_request rng, u3_noun octs) return out; } +static void +_http_range_respond(u3_hreq* req_u, u3_noun nac, range_request rng_req) +{ + u3_noun octs = u3r_at(127, nac); + if ( u3_none == octs ) { + h2o_send_error_500(req_u->rec_u, "Internal Server Error", "scry failed", 0); + return; + } + content result = _slice_octs(rng_req, octs); + + if ( u3_nul == result.dat ) { + c3_c* msg_c = "Requested Range Not Satisfiable"; + u3z(result.dat); + h2o_send_error_generic(req_u->rec_u, 416, msg_c, msg_c, 0); + return; + } + + if ( c3y == u3r_sing(result.dat, octs) ) { + // 200 + u3z(result.dat); + u3_noun len = u3r_at(254, nac); + c3_w len_w; + if ( c3n == u3r_safe_word(len, &len_w) ) { + u3z(nac); + _http_scry_respond(req_u, u3_nul); + return; + } + u3_noun hez = _content_headers(0, (len_w - 1), len_w); + u3_noun res = u3i_edit(nac, 125, u3qb_weld(hez, u3r_at(125, nac))); + _http_cache_respond(req_u, res); + } + else { + // 206 + u3_noun hez = _content_headers(result.beg_z, result.end_z, u3h(octs)); + u3_noun res = u3i_edit(nac, 127, result.dat); + res = u3i_edit(res, 124, 206); + res = u3i_edit(res, 125, u3qb_weld(hez, u3r_at(125, res))); + _http_cache_respond(req_u, res); + } +} + /* _http_scry_cb() */ static void _http_scry_cb(void* vod_p, u3_noun nun) { - // XX free u3_preq* peq_u = vod_p; u3_httd* htd_u = peq_u->htd_u; u3_hreq* req_u = peq_u->req_u; @@ -799,7 +839,10 @@ _http_scry_cb(void* vod_p, u3_noun nun) range_request rng_req; c3_o rng_o = _get_range(req_headers, &rng_req); - if (c3n == rng_o ) { + if (c3y == rng_o ) { + _http_range_respond(req_u, nun, rng_req); + } + else { // XX review u3_noun len = u3r_at(254, nun); c3_w len_w; @@ -814,25 +857,6 @@ _http_scry_cb(void* vod_p, u3_noun nun) u3_noun res = u3i_edit(u3k(nun), 125, u3qb_weld(hez, u3r_at(125, nun))); _http_scry_respond(req_u, u3k(res)); } - else { - u3_noun octs = u3r_at(127, nun); - if ( u3_none == octs ) { - h2o_send_error_500(req_u->rec_u, "Internal Server Error", "scry failed", 0); - } - content result = _slice_octs(rng_req, octs); - if ( u3_nul == result.dat ) { - c3_c* msg_c = "Requested Range Not Satisfiable"; - u3z(result.dat); - h2o_send_error_generic(req_u->rec_u, 416, msg_c, msg_c, 0); - } - else { - u3_noun hez = _content_headers(result.beg_z, result.end_z, u3h(octs)); - u3_noun res = u3i_edit(nun, 127, result.dat); - res = u3i_edit(res, 124, 206); - res = u3i_edit(res, 125, u3qb_weld(hez, u3r_at(125, res))); - _http_scry_respond(req_u, res); - } - } } } @@ -1032,51 +1056,15 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) req_u->peq_u, _http_scry_cb); } else { - // XX function h2o_headers_t req_headers = req_u->rec_u->headers; range_request rng_req; c3_o rng_o = _get_range(req_headers, &rng_req); - if ( c3n == rng_o) { - _http_cache_respond(req_u, nac); + if ( c3y == rng_o) { + _http_range_respond(req_u, nac, rng_req); } else { - u3_noun octs = u3r_at(127, nac); - if ( u3_none == octs ) { - h2o_send_error_500(rec_u, "Internal Server Error", "scry failed", 0); - return; - } - content result = _slice_octs(rng_req, octs); - - if ( u3_nul == result.dat ) { - c3_c* msg_c = "Requested Range Not Satisfiable"; - u3z(result.dat); - h2o_send_error_generic(req_u->rec_u, 416, msg_c, msg_c, 0); - return; - } - - if ( c3y == u3r_sing(result.dat, octs) ) { - // 200 - u3z(result.dat); - u3_noun len = u3r_at(254, nac); - c3_w len_w; - if ( c3n == u3r_safe_word(len, &len_w) ) { - u3z(nac); - _http_scry_respond(req_u, u3_nul); - return; - } - u3_noun hez = _content_headers(0, (len_w - 1), len_w); - u3_noun res = u3i_edit(nac, 125, u3qb_weld(hez, u3r_at(125, nac))); - _http_cache_respond(req_u, res); - } - else { - // 206 - u3_noun hez = _content_headers(result.beg_z, result.end_z, u3h(octs)); - u3_noun res = u3i_edit(nac, 127, result.dat); - res = u3i_edit(res, 124, 206); - res = u3i_edit(res, 125, u3qb_weld(hez, u3r_at(125, res))); - _http_cache_respond(req_u, res); - } + _http_cache_respond(req_u, nac); } } } @@ -1086,7 +1074,8 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) } static void -_http_scry_respond(u3_hreq* req_u, u3_noun nun) { +_http_scry_respond(u3_hreq* req_u, u3_noun nun) +{ h2o_req_t* rec_u = req_u->rec_u; u3_httd* htd_u = req_u->hon_u->htp_u->htd_u; @@ -1130,7 +1119,8 @@ _http_scry_respond(u3_hreq* req_u, u3_noun nun) { /* _http_cache_respond(): respond with a simple-payload:http */ static void -_http_cache_respond(u3_hreq* req_u, u3_noun nun) { +_http_cache_respond(u3_hreq* req_u, u3_noun nun) +{ h2o_req_t* rec_u = req_u->rec_u; u3_httd* htd_u = req_u->hon_u->htp_u->htd_u; From fbc504dd03a620a11780d2be17928a4bc683daa0 Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Wed, 24 Jul 2024 16:44:57 -0400 Subject: [PATCH 274/430] mesa: new lane management --- pkg/vere/io/mesa.c | 130 ++++++++++++++++++---------------------- pkg/vere/io/mesa/mesa.h | 6 +- pkg/vere/io/mesa/pact.c | 13 ++-- 3 files changed, 65 insertions(+), 84 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 68e377db7b..bd89d53962 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -946,6 +946,33 @@ static void _mesa_send(u3_mesa_pict* pic_u, u3_lane* lan_u) _mesa_send_buf(sam_u, *lan_u, buf_y, siz_w); } +/* _mesa_send_modal(): send packet on lanes based on direct vs. indirect mode +*/ +static void +_mesa_send_modal(u3_peer* per_u, c3_y* buf_y, c3_w siz_w) +{ + u3_mesa* sam_u = per_u->sam_u; + c3_d now_d = _get_now_micros(); + + if ( c3y == _mesa_is_direct_mode(per_u) ) { + u3l_log("mesa: direct"); + _mesa_send_buf(sam_u, per_u->dan_u, buf_y, siz_w); + per_u->dir_u.sen_d = now_d; + } + else { + u3l_log("mesa: indirect"); + u3_lane imp_u = _mesa_get_czar_lane(sam_u, per_u->imp_y); + _mesa_send_buf(sam_u, imp_u, buf_y, siz_w); + per_u->ind_u.sen_d = now_d; + + if ( (c3n == _mesa_is_lane_zero(&per_u->dan_u)) && + (per_u->dir_u.sen_d + DIRECT_ROUTE_RETRY_MICROS > now_d)) { + _mesa_send_buf(sam_u, per_u->dan_u, buf_y, siz_w); + per_u->dir_u.sen_d = now_d; + } + } +} + static void _try_resend(u3_pend_req* req_u, c3_w ack_w) { @@ -953,6 +980,7 @@ _try_resend(u3_pend_req* req_u, c3_w ack_w) c3_d now_d = _get_now_micros(); u3_mesa_pact *pac_u = &req_u->pic_u->pac_u; + c3_y* buf_y = c3_calloc(PACT_SIZE); for ( int i = req_u->lef_w; i < ack_w; i++ ) { // TODO: make fast recovery different from slow // TODO: track skip count but not dupes, since dupes are meaningless @@ -961,35 +989,14 @@ _try_resend(u3_pend_req* req_u, c3_w ack_w) los_o = c3y; pac_u->pek_u.nam_u.fra_w = i; - c3_y* buf_y = c3_calloc(PACT_SIZE); c3_w siz_w = mesa_etch_pact(buf_y, pac_u); if ( 0 == siz_w ) { - u3l_log("failed to etch"); - u3_assert( 0 ); + u3_assert(!"failed to etch"); } - - u3_peer* per_u = req_u->per_u; - u3_mesa* sam_u = per_u->sam_u; - if ( c3y == _mesa_is_direct_mode(per_u) ) { - u3l_log("mesa: direct"); - _mesa_send_buf(sam_u, per_u->dan_u, buf_y, siz_w); - per_u->dir_u.sen_d = now_d; - } - else { - u3_lane imp_u = _mesa_get_czar_lane(sam_u, per_u->imp_y); - _mesa_send_buf(sam_u, imp_u, buf_y, siz_w); - per_u->ind_u.sen_d = now_d; - u3l_log("mesa: indirect"); - - if ( (c3n == _mesa_is_lane_zero(&per_u->dan_u)) && - (per_u->dir_u.sen_d + DIRECT_ROUTE_RETRY_MICROS > now_d)) { - _mesa_send_buf(sam_u, per_u->dan_u, buf_y, siz_w); - per_u->dir_u.sen_d = now_d; - } - } - _mesa_req_pact_resent(req_u, &pac_u->pek_u.nam_u); + _mesa_send_modal(req_u->per_u, buf_y, siz_w); } } + c3_free(buf_y); if ( c3y == los_o ) { req_u->gag_u->sst_w = (req_u->gag_u->wnd_w / 2) + 1; @@ -1069,20 +1076,8 @@ _mesa_burn_misorder_queue(u3_pend_req* req_u) return res_o; } -static void _init_lane_state(u3_lane_state*); - -static u3_ovum* -_mesa_dear(u3_auto* car_u, u3_ship her_u, u3_lane lan_u) -{ - u3_noun wir = u3nc(c3__ames, u3_nul); - u3_noun cad; - { - u3_noun her = u3_ship_to_noun(her_u); - u3_noun lan = u3_mesa_encode_lane(lan_u); - cad = u3nt(c3__dear, her, lan); - } - return u3_auto_plan(car_u, u3_ovum_init(0, c3__ames, wir, cad)); -} +static void +_init_lane_state(u3_lane_state* sat_u); /* _mesa_req_pact_done(): mark packet as done */ @@ -1172,8 +1167,6 @@ _mesa_req_pact_done(u3_pend_req* req_u, req_u->per_u->dan_u = lan_u; sat_u = &req_u->per_u->dir_u; _init_lane_state(sat_u); - - _mesa_dear(&sam_u->car_u, nam_u->her_u, lan_u); } else { sat_u = &req_u->per_u->ind_u; @@ -1188,10 +1181,6 @@ _mesa_req_pact_done(u3_pend_req* req_u, _try_resend(req_u, nam_u->fra_w); _update_resend_timer(req_u); - - // _mesa_put_request(sam_u, nam_u, req_u); - // _mesa_put_lane(sam_u, nam_u->her_u, lan_u, gag_u); - return; } static u3_lane @@ -1404,22 +1393,27 @@ _mesa_ef_send(u3_mesa* sam_u, u3_noun las, u3_noun pac) u3l_log("mesa: ef_send() %s", typ_c); #endif - u3_peer* per_u; - if ( (PACT_PEEK == hed_u.typ_y) || (PACT_POKE == hed_u.typ_y) ) { - u3_mesa_pact pac_u; - c3_w res_w = mesa_sift_pact(&pac_u, buf_y, len_w); + u3_mesa_pact pac_u; + c3_w res_w = mesa_sift_pact(&pac_u, buf_y, len_w); + u3_peer* per_u; + { u3_ship her_u; _get_her(&pac_u, her_u); per_u = _mesa_get_peer(sam_u, her_u); + } + if ( (PACT_PEEK == hed_u.typ_y) || (PACT_POKE == hed_u.typ_y) ) { _mesa_add_our_to_pit(sam_u, &pac_u.pek_u.nam_u); - } else { // treat PACT_RESV as an opaque response - per_u = NULL; } - sam_u->tim_d = _get_now_micros(); + if ( per_u ) { + _mesa_send_modal(per_u, buf_y, len_w); + } + else { + _mesa_send_bufs(sam_u, NULL, buf_y, len_w, u3k(las)); + } - _mesa_send_bufs(sam_u, per_u, buf_y, len_w, u3k(las)); + sam_u->tim_d = _get_now_micros(); c3_free(buf_y); u3z(pac); @@ -1921,7 +1915,6 @@ _mesa_veri_scry_cb(void* vod_p, u3_noun nun) return; } else if ( c3y == nun ) { - // TODO: also call _mesa_dear? _mesa_request_next_fragments(ver_u->sam_u, req_u, ver_u->lan_u); } else if ( c3n == nun ) { @@ -2065,8 +2058,6 @@ _mesa_page_news_cb(u3_ovum* egg_u, u3_ovum_news new_e) c3_free(her_c); #endif - _mesa_dear(&per_u->sam_u->car_u, dat_u->her_u, per_u->dan_u); - c3_free(dat_u); } @@ -2140,7 +2131,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) u3l_log(" forwarding"); #endif - update_hopcount(&pac_u->hed_u); + inc_hopcount(&pac_u->hed_u); c3_etch_word(pac_u->pag_u.sot_u, lan_u.pip_w); c3_etch_short(pac_u->pag_u.sot_u + 4, lan_u.por_s); @@ -2149,7 +2140,6 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) _mesa_del_pit(sam_u, nam_u); _mesa_free_pict(pic_u); u3z(pin); - return; } if ( c3n == our ) { // TODO: free pact and pict @@ -2158,7 +2148,6 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) } // process incoming response to ourselves - // TODO: use jumbo frames // TODO: memory management, maybe free pict and pact // if single-fragment message, inject directly into Arvo @@ -2166,12 +2155,13 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) u3_noun cad; { u3_noun lan = u3_mesa_encode_lane(lan_u); - u3i_slab sab_u; - u3i_slab_init(&sab_u, 3, PACT_SIZE); // XX should just preserve input buffer + u3i_slab sab_u; + u3i_slab_init(&sab_u, 3, PACT_SIZE); c3_w cur_w = mesa_etch_pact(sab_u.buf_y, pac_u); - cad = u3nc(c3__heer, u3i_slab_mint(&sab_u)); + + cad = u3nt(c3__heer, lan, u3i_slab_mint(&sab_u)); } u3_noun wir = u3nc(c3__ames, u3_nul); @@ -2219,26 +2209,26 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) &pac_u->pag_u.dat_u, pac_u->hed_u.hop_y, lon_u); + // TODO: check return value before continuing? c3_o done_with_jumbo_frame = __(req_u->len_w == req_u->tot_w); // TODO: fix for non-message-sized jumbo frames if ( c3y == done_with_jumbo_frame ) { u3_noun cad; { // construct jumbo frame - c3_w jumbo_len_w = (1024 * (req_u->tot_w - 1)) + pac_u->pag_u.dat_u.len_w; pac_u->pag_u.nam_u.boq_y = 31; // TODO: use actual jumbo bloq pac_u->pag_u.dat_u.tot_w = 1; pac_u->pag_u.nam_u.fra_w = 0; + c3_w jumbo_len_w = (1024 * (req_u->tot_w - 1)) + pac_u->pag_u.dat_u.len_w; pac_u->pag_u.dat_u.len_w = jumbo_len_w; - pac_u->pag_u.dat_u.fra_y = c3_realloc(pac_u->pag_u.dat_u.fra_y, jumbo_len_w); - memcpy(pac_u->pag_u.dat_u.fra_y, req_u->dat_y, jumbo_len_w); + pac_u->pag_u.dat_u.fra_y = req_u->dat_y; pac_u->pag_u.dat_u.aum_u = req_u->aum_u; u3_noun lan = u3_mesa_encode_lane(lan_u); - c3_y* buf_y = c3_calloc((PACT_SIZE - 1024) + jumbo_len_w); + c3_y* buf_y = c3_calloc(mesa_size_pact(pac_u)); c3_w res_w = mesa_etch_pact(buf_y, pac_u); - cad = u3nc(c3__heer, u3i_bytes(res_w, buf_y)); + cad = u3nt(c3__heer, lan, u3i_bytes(res_w, buf_y)); c3_free(buf_y); } @@ -2291,7 +2281,6 @@ _mesa_add_our_to_pit(u3_mesa* sam_u, u3_mesa_name* nam_u) return; } -// XX forwarding wrong, need a PIT entry static void _mesa_forward_request(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane lan_u) { @@ -2313,7 +2302,7 @@ _mesa_forward_request(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane lan_u) _mesa_free_pict(pic_u); return; } - //_update_hopcount(&pac_u->hed_u); // TODO reinstate + inc_hopcount(&pac_u->hed_u); #ifdef MESA_DEBUG u3l_log("mesa: forward_request()"); log_pact(pac_u); @@ -2397,9 +2386,6 @@ _mesa_poke_news_cb(u3_ovum* egg_u, u3_ovum_news new_e) c3_free(her_c); #endif - // XX tame old routes, and then dear? - _mesa_dear(&per_u->sam_u->car_u, dat_u->her_u, per_u->dan_u); - c3_free(dat_u); } @@ -2468,7 +2454,7 @@ _mesa_hear_poke(u3_mesa_pict* pic_u, u3_lane* lan_u) // XX should just preserve input buffer mesa_etch_pact(sab_u.buf_y, pac_u); - cad = u3nc(c3__heer, u3i_slab_mint(&sab_u)); + cad = u3nt(c3__heer, lan, u3i_slab_mint(&sab_u)); } u3_ovum* ovo = u3_ovum_init(0, c3__ames, wir, cad); diff --git a/pkg/vere/io/mesa/mesa.h b/pkg/vere/io/mesa/mesa.h index 4ca7fe57c5..43ed733ebd 100644 --- a/pkg/vere/io/mesa/mesa.h +++ b/pkg/vere/io/mesa/mesa.h @@ -148,14 +148,14 @@ typedef struct _u3_mesa_pact { }; } u3_mesa_pact; +c3_w mesa_size_pact(u3_mesa_pact* pac_u); c3_o mesa_sift_head(c3_y buf_y[8], u3_mesa_head* hed_u); c3_w mesa_sift_pact(u3_mesa_pact* pac_u, c3_y* buf_y, c3_w len_w); c3_w mesa_etch_pact(c3_y* buf_y, u3_mesa_pact* pac_u); - -void update_hopcount(u3_mesa_head*); - void mesa_free_pact(u3_mesa_pact* pac_u); +void inc_hopcount(u3_mesa_head*); + void log_pact(u3_mesa_pact* pac_u); void log_name(u3_mesa_name* nam_u); diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index c959e01e7e..cd41e7fdbb 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -175,7 +175,7 @@ log_pact(u3_mesa_pact* pac_u) /* Helper utilities */ void -update_hopcount(u3_mesa_head* hed_u) // TODO rename, _inc_hopcount()? +inc_hopcount(u3_mesa_head* hed_u) { hed_u->hop_y = c3_max(hed_u->hop_y+1, 7); } @@ -837,8 +837,8 @@ _mesa_size_hops(u3_mesa_pact* pac_u) } } -static c3_w -_mesa_size_pact(u3_mesa_pact* pac_u) +c3_w +mesa_size_pact(u3_mesa_pact* pac_u) { c3_w siz_w = 8; // header + cookie; @@ -871,12 +871,7 @@ _mesa_size_pact(u3_mesa_pact* pac_u) c3_w mesa_etch_pact(c3_y* buf_y, u3_mesa_pact* pac_u) { - c3_w siz_w = _mesa_size_pact(pac_u); - - // if ( siz_w > PACT_SIZE ) { - // fprintf(stderr, "etch: would overflow %u\r\n", siz_w); - // return 0; - // } + c3_w siz_w = mesa_size_pact(pac_u); u3_mesa_head* hed_u = &pac_u->hed_u; From 5007af484f9d4284a4ccb2a92722c540aa0b4a9c Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Thu, 25 Jul 2024 10:18:36 +0200 Subject: [PATCH 275/430] mesa: serialize next hops in hed_u->nex_y --- pkg/vere/io/mesa.c | 33 +++++++++++++++++++++++++++++++-- pkg/vere/io/mesa/mesa.h | 1 - pkg/vere/io/mesa/pact.c | 35 +++++++++++++++++++++++++++++++++-- 3 files changed, 64 insertions(+), 5 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index bd89d53962..5fa9e3c5cb 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -2070,6 +2070,32 @@ _mesa_page_bail_cb(u3_ovum* egg_u, u3_ovum_news new_e) c3_free(egg_u->ptr_v); } +static void +_mesa_add_hop(c3_y hop_y, u3_mesa_head* hed_u, u3_mesa_page_pact* pag_u, u3_lane lan_u) +{ + if ( 1 == hop_y ) { + c3_etch_word(pag_u->sot_u, lan_u.pip_w); + c3_etch_short(pag_u->sot_u + 4, lan_u.por_s); + hed_u->nex_y = HOP_SHORT; + return; + } + + hed_u->nex_y = HOP_MANY; + + u3_mesa_hop_once* lan_y = c3_calloc(sizeof(u3_mesa_hop_once)); + + c3_etch_word(lan_y->dat_y, lan_u.pip_w); + c3_etch_short(lan_y->dat_y, lan_u.por_s); + + lan_y->len_w = 6; + + c3_realloc(&pag_u->man_u, pag_u->man_u.len_w + 8); + pag_u->man_u.dat_y[pag_u->man_u.len_w] = *lan_y; + + pag_u->man_u.len_w++; + +} + static void _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) { @@ -2135,7 +2161,9 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) c3_etch_word(pac_u->pag_u.sot_u, lan_u.pip_w); c3_etch_short(pac_u->pag_u.sot_u + 4, lan_u.por_s); - // TODO actually stick next hop in packet + // stick next hop in packet + _mesa_add_hop(pac_u->hed_u.hop_y, &pac_u->hed_u ,&pac_u->pag_u, lan_u); + _mesa_send_pact(sam_u, u3k(las), per_u, pac_u); _mesa_del_pit(sam_u, nam_u); _mesa_free_pict(pic_u); @@ -2169,7 +2197,8 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) u3_ovum* ovo = u3_ovum_init(0, c3__ames, wir, cad); ovo = u3_auto_plan(&sam_u->car_u, ovo); - if ( !pac_u->hed_u.hop_y && !_mesa_lanes_equal(&per_u->dan_u, &lan_u) ) { + // XX only inject dear if we hear a different, direct lane + if ( pac_u->hed_u.hop_y == 0 && _mesa_lanes_equal(&per_u->dan_u, &lan_u) == c3n) { // XX should put in cache on success u3_mesa_lane_cb_data* dat_u = c3_malloc(sizeof(u3_mesa_lane_cb_data)); { diff --git a/pkg/vere/io/mesa/mesa.h b/pkg/vere/io/mesa/mesa.h index 43ed733ebd..8961f6751e 100644 --- a/pkg/vere/io/mesa/mesa.h +++ b/pkg/vere/io/mesa/mesa.h @@ -122,7 +122,6 @@ typedef union { u3_mesa_hop_more man_u; } u3_mesa_hop; - typedef struct _u3_mesa_page_pact { u3_mesa_name nam_u; u3_mesa_data dat_u; diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index cd41e7fdbb..7275abab00 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -177,7 +177,7 @@ log_pact(u3_mesa_pact* pac_u) void inc_hopcount(u3_mesa_head* hed_u) { - hed_u->hop_y = c3_max(hed_u->hop_y+1, 7); + hed_u->hop_y = c3_min(hed_u->hop_y+1, 7); } static c3_y @@ -707,6 +707,35 @@ _mesa_etch_data(c3_y* buf_y, u3_mesa_data* dat_u) return cur_w; } +static c3_w +_mesa_etch_next(c3_y* buf_y, u3_mesa_hop_type nex_y, u3_mesa_page_pact* pac_u) +{ + + switch ( nex_y ) { + case HOP_SHORT: { + memcpy(buf_y, pac_u->sot_u, 6); + return 6; + } break; + case HOP_MANY: { + // buf_y[0] = pac_u->man_u.len_w; // XX + c3_w cur_w =0; + memcpy(buf_y + cur_w, &pac_u->man_u.len_w, 4); + for( c3_w i = 0; i < pac_u->man_u.len_w; i++ ) { + struct _u3_mesa_hop_once hop_u = pac_u->man_u.dat_y[i]; + memcpy(buf_y + cur_w, &hop_u.len_w, 4); + cur_w += 4; + memcpy(buf_y + cur_w, hop_u.dat_y, hop_u.len_w); + cur_w += hop_u.len_w; + } + return cur_w; + } break; + default: { + return 0; + } break; + } + +} + static c3_w _mesa_etch_page_pact(c3_y* buf_y, u3_mesa_page_pact* pac_u, u3_mesa_head* hed_u) { @@ -722,7 +751,9 @@ _mesa_etch_page_pact(c3_y* buf_y, u3_mesa_page_pact* pac_u, u3_mesa_head* hed_u) } cur_w += nex_w; - // XX hops + nex_w = _mesa_etch_next(buf_y + cur_w, hed_u->nex_y, pac_u); + + cur_w += nex_w; return cur_w; } From 51f664b3b945b713874dec9046483b5831d9ea41 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Thu, 25 Jul 2024 15:38:56 +0200 Subject: [PATCH 276/430] mesa: fix wrong size in _get_her --- pkg/vere/io/mesa.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 5fa9e3c5cb..0aba85d6b8 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -360,15 +360,15 @@ _get_her(u3_mesa_pact* pac_u, c3_d* our_d) break; } case PACT_PAGE: { - memcpy(our_d, pac_u->pag_u.nam_u.her_u,2); + memcpy(our_d, pac_u->pag_u.nam_u.her_u, 16); break; } case PACT_PEEK: { - memcpy(our_d, pac_u->pek_u.nam_u.her_u,2); + memcpy(our_d, pac_u->pek_u.nam_u.her_u, 16); break; } case PACT_POKE: { - memcpy(our_d, pac_u->pok_u.pay_u.her_u, 2); + memcpy(our_d, pac_u->pok_u.pay_u.her_u, 16); break; } } From 9b068d8bba1d2a33b5d3f5fa96f56490137db4c0 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Thu, 25 Jul 2024 15:40:47 +0200 Subject: [PATCH 277/430] mesa: get PIT entry when producing %page --- pkg/vere/io/mesa.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 0aba85d6b8..526298e810 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1406,7 +1406,25 @@ _mesa_ef_send(u3_mesa* sam_u, u3_noun las, u3_noun pac) _mesa_add_our_to_pit(sam_u, &pac_u.pek_u.nam_u); } - if ( per_u ) { + if ( PACT_PAGE == hed_u.typ_y ) { + u3_weak pin = _mesa_get_pit(sam_u, &pac_u.pek_u.nam_u); + + if ( u3_none == pin ) { + #ifdef MESA_DEBUG + u3l_log(" no PIT entry"); + #endif + return; + } + + u3_noun our, las; + u3x_cell(pin, &our, &las); + u3m_p("lane", las); + if ( u3_nul != las ) { + _mesa_send_bufs(sam_u, NULL, buf_y, len_w, u3k(las)); + _mesa_del_pit(sam_u, &pac_u.pek_u.nam_u); + u3z(pin); + } + }else if ( per_u ) { _mesa_send_modal(per_u, buf_y, len_w); } else { @@ -2168,6 +2186,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) _mesa_del_pit(sam_u, nam_u); _mesa_free_pict(pic_u); u3z(pin); + return; } if ( c3n == our ) { // TODO: free pact and pict @@ -2444,6 +2463,8 @@ _mesa_hear_poke(u3_mesa_pict* pic_u, u3_lane* lan_u) return; } + _mesa_add_lane_to_pit(sam_u, &pac_u->pek_u.nam_u, *lan_u); + // XX if this lane management stuff is necessary // it should be deferred to after successful event processing u3_peer* per_u = _mesa_get_peer(sam_u, pac_u->pok_u.pay_u.her_u); From 253db2d36a385ef286a7cfc73658a5bbd7cfed35 Mon Sep 17 00:00:00 2001 From: lukechampine Date: Thu, 25 Jul 2024 10:41:24 -0400 Subject: [PATCH 278/430] mesa: reorder some definitions --- pkg/vere/io/mesa.c | 194 +++++++++++++++++++-------------------------- 1 file changed, 80 insertions(+), 114 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 526298e810..614833f44c 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1375,6 +1375,86 @@ _mesa_queue_czar(u3_mesa* sam_u, u3_noun las, u3_noun pac) static void _mesa_add_our_to_pit(u3_mesa*, u3_mesa_name*); +static u3_noun +_name_to_scry(u3_mesa_name* nam_u) +{ + u3_noun rif = _dire_etch_ud(nam_u->rif_w); + u3_noun boq = _dire_etch_ud(nam_u->boq_y); + u3_noun fag = _dire_etch_ud(nam_u->fra_w); + u3_noun pax = _mesa_encode_path(nam_u->pat_s, (c3_y*)nam_u->pat_c); + + u3_noun wer = nam_u->nit_o == c3y + ? u3nc(c3__init, pax) + : u3nt(nam_u->aut_o == c3y ? c3__auth : c3__data, fag, pax); + + u3_noun res = u3nc(c3__mess, u3nq(rif, c3__pact, boq, u3nc(c3__etch, wer))); + + return res; +} + +/* + * RETAIN */ +static u3_weak +_mesa_get_pit(u3_mesa* sam_u, u3_mesa_name* nam_u) +{ + return u3h_get(sam_u->pit_p, _name_to_scry((nam_u))); +} + +static void +_mesa_put_pit(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_noun val) +{ + u3_noun pax = _name_to_scry(nam_u); + u3h_put(sam_u->pit_p, pax, u3k(val)); + #ifdef MESA_DEBUG + c3_c* our_c = (c3y == u3h(val))? "&" : "|"; + c3_c* las_c = (u3_nul == u3t(val))? "~" : "..."; + u3l_log("mesa: put_pit(our %s, las %s)", our_c, las_c); + log_name(nam_u); + #endif + u3z(pax); + u3z(val); +} + +static void +_mesa_del_pit(u3_mesa* sam_u, u3_mesa_name* nam_u) +{ + u3_noun pax = _name_to_scry(nam_u); + u3h_del(sam_u->pit_p, pax); + u3z(pax); +} + +static void +_mesa_add_lane_to_pit(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_lane lan_u) +{ + // TODO: prevent duplicate lane from being added + u3_noun lan = u3_mesa_encode_lane(lan_u); + u3_weak pin = _mesa_get_pit(sam_u, nam_u); + if ( u3_none == pin ) { + pin = u3nt(c3n, u3k(lan), u3_nul); + } + else { + pin = u3nt(u3k(u3h(pin)), u3k(lan), u3k(u3t(pin))); + } + _mesa_put_pit(sam_u, nam_u, u3k(pin)); + u3z(lan); u3z(pin); + return; +} + +static void +_mesa_add_our_to_pit(u3_mesa* sam_u, u3_mesa_name* nam_u) +{ + u3_weak pin = _mesa_get_pit(sam_u, nam_u); + if ( u3_none == pin ) { + pin = u3nc(c3y, u3_nul); + } + else { + pin = u3nc(c3y, u3k(u3t(pin))); + } + _mesa_put_pit(sam_u, nam_u, u3k(pin)); + u3z(pin); + return; +} + static void _mesa_ef_send(u3_mesa* sam_u, u3_noun las, u3_noun pac) { @@ -1583,45 +1663,6 @@ _mesa_add_galaxy_pend(u3_mesa* sam_u, u3_noun her, u3_noun pen) return _(wat); } -static u3_noun -_name_to_scry(u3_mesa_name* nam_u) -{ - u3_noun rif = _dire_etch_ud(nam_u->rif_w); - u3_noun boq = _dire_etch_ud(nam_u->boq_y); - u3_noun fag = _dire_etch_ud(nam_u->fra_w); - u3_noun pax = _mesa_encode_path(nam_u->pat_s, (c3_y*)nam_u->pat_c); - - u3_noun wer = nam_u->nit_o == c3y - ? u3nc(c3__init, pax) - : u3nt(nam_u->aut_o == c3y ? c3__auth : c3__data, fag, pax); - - u3_noun res = u3nc(c3__mess, u3nq(rif, c3__pact, boq, u3nc(c3__etch, wer))); - - return res; -} - -static u3_noun -_name_to_batch_scry(u3_mesa_name* nam_u, c3_w lop_w, c3_w len_w) -{ - u3_noun rif = _dire_etch_ud(nam_u->rif_w); - u3_noun boq = _dire_etch_ud(nam_u->boq_y); - u3_noun fag = _dire_etch_ud(nam_u->fra_w); - u3_noun pax = _mesa_encode_path(nam_u->pat_s, (c3_y*)nam_u->pat_c); - - u3_noun lop = _dire_etch_ud(lop_w); - u3_noun len = _dire_etch_ud(len_w); - - u3_noun wer = nam_u->nit_o == c3y - ? u3nc(c3__init, pax) - : u3nt(nam_u->aut_o == c3y ? c3__auth : c3__data, fag, pax); - - u3_noun res = u3nc(c3__mess, u3nq(rif, c3__pact, boq, u3nc(c3__etch, wer))); - // [%hunk lop=@t len=@t pat=*] - u3_noun bat = u3nq(c3__hunk, lop, len, res); - - return bat; -} - static u3_noun _name_to_jumbo_scry(u3_mesa_name* nam_u) { @@ -1659,49 +1700,6 @@ _mesa_get_jumbo_cache(u3_mesa* sam_u, u3_mesa_name* nam_u) return res; } -/* - * RETAIN */ -static u3_weak -_mesa_get_pit(u3_mesa* sam_u, u3_mesa_name* nam_u) -{ - return u3h_get(sam_u->pit_p, _name_to_scry((nam_u))); -} - -/* -ld: Undefined symbols: - _log_name, referenced from: - __mesa_put_pit in - _log_pact in - _log_pact in - _log_pact in - _log_pact in -clang: error: linker command failed with exit code 1 (use -v to see invocation) -Target //pkg/vere:urbit failed to build -*/ - -static void -_mesa_put_pit(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_noun val) -{ - u3_noun pax = _name_to_scry(nam_u); - u3h_put(sam_u->pit_p, pax, u3k(val)); - #ifdef MESA_DEBUG - c3_c* our_c = (c3y == u3h(val))? "&" : "|"; - c3_c* las_c = (u3_nul == u3t(val))? "~" : "..."; - u3l_log("mesa: put_pit(our %s, las %s)", our_c, las_c); - log_name(nam_u); - #endif - u3z(pax); - u3z(val); -} - -static void -_mesa_del_pit(u3_mesa* sam_u, u3_mesa_name* nam_u) -{ - u3_noun pax = _name_to_scry(nam_u); - u3h_del(sam_u->pit_p, pax); - u3z(pax); -} - static void _mesa_put_jumbo_cache(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_noun val) { @@ -2297,38 +2295,6 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) } } -static void -_mesa_add_lane_to_pit(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_lane lan_u) -{ - // TODO: prevent duplicate lane from being added - u3_noun lan = u3_mesa_encode_lane(lan_u); - u3_weak pin = _mesa_get_pit(sam_u, nam_u); - if ( u3_none == pin ) { - pin = u3nt(c3n, u3k(lan), u3_nul); - } - else { - pin = u3nt(u3k(u3h(pin)), u3k(lan), u3k(u3t(pin))); - } - _mesa_put_pit(sam_u, nam_u, u3k(pin)); - u3z(lan); u3z(pin); - return; -} - -static void -_mesa_add_our_to_pit(u3_mesa* sam_u, u3_mesa_name* nam_u) -{ - u3_weak pin = _mesa_get_pit(sam_u, nam_u); - if ( u3_none == pin ) { - pin = u3nc(c3y, u3_nul); - } - else { - pin = u3nc(c3y, u3k(u3t(pin))); - } - _mesa_put_pit(sam_u, nam_u, u3k(pin)); - u3z(pin); - return; -} - static void _mesa_forward_request(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane lan_u) { From 1a2d6597a277ef9eb6e152bec13981fdb289fba8 Mon Sep 17 00:00:00 2001 From: midden-fabler Date: Thu, 25 Jul 2024 12:47:13 -0400 Subject: [PATCH 279/430] http: put auth in cache key, cleanup --- pkg/vere/io/http.c | 44 +++++++++++++++++++------------------------- 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/pkg/vere/io/http.c b/pkg/vere/io/http.c index 525ad353c4..af0dbf2ee9 100644 --- a/pkg/vere/io/http.c +++ b/pkg/vere/io/http.c @@ -783,12 +783,15 @@ _http_range_respond(u3_hreq* req_u, u3_noun nac, range_request rng_req) { u3_noun octs = u3r_at(127, nac); if ( u3_none == octs ) { - h2o_send_error_500(req_u->rec_u, "Internal Server Error", "scry failed", 0); + // 400 + c3_c* msg_c = "bad request"; + h2o_send_error_generic(req_u->rec_u, 400, msg_c, msg_c, 0); return; } content result = _slice_octs(rng_req, octs); if ( u3_nul == result.dat ) { + // 416 c3_c* msg_c = "Requested Range Not Satisfiable"; u3z(result.dat); h2o_send_error_generic(req_u->rec_u, 416, msg_c, msg_c, 0); @@ -796,7 +799,7 @@ _http_range_respond(u3_hreq* req_u, u3_noun nac, range_request rng_req) } if ( c3y == u3r_sing(result.dat, octs) ) { - // 200 + // 200 u3z(result.dat); u3_noun len = u3r_at(254, nac); c3_w len_w; @@ -810,7 +813,7 @@ _http_range_respond(u3_hreq* req_u, u3_noun nac, range_request rng_req) _http_cache_respond(req_u, res); } else { - // 206 + // 206 u3_noun hez = _content_headers(result.beg_z, result.end_z, u3h(octs)); u3_noun res = u3i_edit(nac, 127, result.dat); res = u3i_edit(res, 124, 206); @@ -827,6 +830,8 @@ _http_scry_cb(void* vod_p, u3_noun nun) u3_preq* peq_u = vod_p; u3_httd* htd_u = peq_u->htd_u; u3_hreq* req_u = peq_u->req_u; + u3_hfig* fig_u = &req_u->hon_u->htp_u->htd_u->fig_u; + c3_o auth = _http_req_is_auth(fig_u, req_u->rec_u); if ( req_u ) { u3_assert(u3_rsat_peek == req_u->sat_e); @@ -843,30 +848,18 @@ _http_scry_cb(void* vod_p, u3_noun nun) _http_range_respond(req_u, nun, rng_req); } else { - // XX review - u3_noun len = u3r_at(254, nun); - c3_w len_w; - if ( c3n == u3r_safe_word(len, &len_w) ) { - u3z(nun); - _http_scry_respond(req_u, u3_nul); - u3z(peq_u->pax); - c3_free(peq_u); - return; - } - u3_noun hez = _content_headers(0, (len_w - 1), len_w); - u3_noun res = u3i_edit(u3k(nun), 125, u3qb_weld(hez, u3r_at(125, nun))); - _http_scry_respond(req_u, u3k(res)); + _http_cache_respond(req_u, u3k(nun)); } } } // cache only if peek was not at now, and nun isn't u3_nul - if ( (c3n == peq_u->las_o) - && (u3_nul != nun) ) - { + if ( c3n == peq_u->las_o ) { // XX pair of auth & path for key // check ~watter-parter's - u3h_put(htd_u->nax_p, peq_u->pax, nun); + u3_noun key = u3nc(auth, u3k(peq_u->pax)); + u3h_put(htd_u->nax_p, key, nun); + u3z(key); } u3z(peq_u->pax); c3_free(peq_u); @@ -1044,15 +1037,17 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) else { u3_noun bam = u3nq(bem.who, bem.des, bem.cas, spur); - u3_weak nac = u3h_get(htd_u->nax_p, bam); + u3_noun key = u3nc(auth, u3k(bam)); + u3_weak nac = u3h_get(htd_u->nax_p, key); + u3z(key); if ( (u3_none == nac) || (u3_nul == nac) || ((u3_nul == gang) && (c3y == u3r_at(14, nac))) ) { - // maybe cache, then serve subsequent range requests from cache - req_u->peq_u->pax = bam; - u3_pier_peek(htd_u->car_u.pir_u, gang, u3nt(0, c3__ex, u3k(bam)), + // cache, then serve subsequent range requests from cache + req_u->peq_u->pax = u3k(bam); + u3_pier_peek(htd_u->car_u.pir_u, gang, u3nt(0, c3__ex, bam), req_u->peq_u, _http_scry_cb); } else { @@ -1177,7 +1172,6 @@ _http_cache_scry_cb(void* vod_p, u3_noun nun) _http_cache_respond(req_u, u3k(nun)); } - // XX pair of auth & path for key u3h_put(htd_u->nax_p, peq_u->pax, nun); u3z(peq_u->pax); c3_free(peq_u); From cc174349fbdc04fd2e204dbd37e2196c53fc2e16 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Fri, 26 Jul 2024 10:13:04 +0200 Subject: [PATCH 280/430] mesa: for pokes, get ship from ack path --- pkg/vere/io/mesa.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 614833f44c..23fa1bfe54 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -368,7 +368,7 @@ _get_her(u3_mesa_pact* pac_u, c3_d* our_d) break; } case PACT_POKE: { - memcpy(our_d, pac_u->pok_u.pay_u.her_u, 16); + memcpy(our_d, pac_u->pok_u.nam_u.her_u, 16); break; } } From f268f47bc694515cf703341aeb1afb5a83377e78 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Fri, 26 Jul 2024 10:13:22 +0200 Subject: [PATCH 281/430] mesa: fix memory error --- pkg/vere/io/mesa.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 23fa1bfe54..49f22e49a5 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1513,7 +1513,7 @@ _mesa_ef_send(u3_mesa* sam_u, u3_noun las, u3_noun pac) sam_u->tim_d = _get_now_micros(); - c3_free(buf_y); + // c3_free(buf_y); u3z(pac); u3z(las); } From be993b8d3f5c6ff344a96ec02de667821f0d944f Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Fri, 26 Jul 2024 14:32:55 +0200 Subject: [PATCH 282/430] mesa: add comment about indirect lane lifecycle --- pkg/vere/io/mesa.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 49f22e49a5..8d04dd950d 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -960,7 +960,14 @@ _mesa_send_modal(u3_peer* per_u, c3_y* buf_y, c3_w siz_w) per_u->dir_u.sen_d = now_d; } else { - u3l_log("mesa: indirect"); + u3l_log("mesa: indirect to %u", per_u->imp_y); + // XX if we have lanes in arvo, send it also there? + // otherwise after a peer turns indirect because we haven't contacted them, + // we never achieve a direct route since we only send + // to the sponsor, and all pages will come forwarded, and per_u->dir_u.her_d + // only gets updated when pages come directly, or if it's the first time + // after a restart of the driver + // u3_lane imp_u = _mesa_get_czar_lane(sam_u, per_u->imp_y); _mesa_send_buf(sam_u, imp_u, buf_y, siz_w); per_u->ind_u.sen_d = now_d; From 305e73643c39a72160f4175f9ad501b2489e0402 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Fri, 26 Jul 2024 14:33:32 +0200 Subject: [PATCH 283/430] mesa: meet peer on nail (note: this updates the peer cache to avoid stale sponsorship data) --- pkg/vere/io/mesa.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 8d04dd950d..ca3015d712 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1528,6 +1528,8 @@ _mesa_ef_send(u3_mesa* sam_u, u3_noun las, u3_noun pac) c3_o _ames_kick_newt(void* sam_u, u3_noun tag, u3_noun dat); +static void _meet_peer(u3_mesa* sam_u, u3_peer* per_u, u3_ship her_u); + static c3_o _mesa_kick(u3_mesa* sam_u, u3_noun tag, u3_noun dat) { c3_o ret_o; @@ -1548,8 +1550,7 @@ static c3_o _mesa_kick(u3_mesa* sam_u, u3_noun tag, u3_noun dat) } break; case c3__send: case c3__turf: - case c3__saxo: - case c3__nail: { + case c3__saxo: { #ifdef MESA_DEBUG c3_c* tag_c = u3r_string(tag); u3l_log("mesa: send old %s", tag_c); @@ -1557,6 +1558,18 @@ static c3_o _mesa_kick(u3_mesa* sam_u, u3_noun tag, u3_noun dat) #endif ret_o = _ames_kick_newt(u3_Host.sam_u, u3k(tag), u3k(dat)); } break; + case c3__nail: { + u3m_p("data", dat); + u3_noun who = u3k(u3h(dat)); + u3_peer* per_u = _mesa_get_peer_raw(sam_u, who); + c3_o new_o = c3n; + + u3_ship who_u; + u3_ship_of_noun(who_u ,who); + _meet_peer(sam_u, per_u, who_u); + + ret_o = _ames_kick_newt(u3_Host.sam_u, u3k(tag), u3k(dat)); + } break; } // technically losing tag is unncessary as it always should From a64e29c096a3862719934a3053359a631fb788c0 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Fri, 26 Jul 2024 14:35:12 +0200 Subject: [PATCH 284/430] mesa: fix crash when exiting binary --- pkg/vere/io/mesa.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index ca3015d712..d2e06d8053 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1614,7 +1614,7 @@ static void _mesa_free_peer(u3_noun per) { u3_peer* per_u = u3to(u3_peer, per); - u3h_free(per_u->req_p); + // u3h_free(per_u->req_p); XX FIXME; refcounts wrong } static void From 5d7651e171129d1f02bd247166c1b0efd50dd03f Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Fri, 26 Jul 2024 14:36:54 +0200 Subject: [PATCH 285/430] mesa: fix memory leak --- pkg/vere/io/mesa.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index d2e06d8053..321cbc8d4c 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -2302,17 +2302,16 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) u3_auto_plan(&sam_u->car_u, u3_ovum_init(0, c3__ames, u3nc(c3__ames, u3_nul), cad)); - } - if ( req_u->len_w < req_u->tot_w ) { - _mesa_request_next_fragments(sam_u, req_u, lan_u); - } - else { + u3l_log(" received last packet, tot_w: %u", req_u->tot_w); c3_d now_d = _get_now_micros(); u3l_log("%u kilobytes took %f ms", req_u->tot_w, (now_d - sam_u->tim_d)/1000.0); - // clean up pend_req - _mesa_free_pict(pic_u); } + + if ( req_u->len_w < req_u->tot_w ) { + _mesa_request_next_fragments(sam_u, req_u, lan_u); + } + } static void From ab03d62d4d431f06608d6a17d65824458aeec613 Mon Sep 17 00:00:00 2001 From: Sigilante Date: Fri, 26 Jul 2024 12:53:23 -0500 Subject: [PATCH 286/430] Turn off Nock verification. --- pkg/noun/jets/i/lagoon.c | 2 -- pkg/noun/jets/tree.c | 58 ++++++++++++++++++++-------------------- 2 files changed, 29 insertions(+), 31 deletions(-) diff --git a/pkg/noun/jets/i/lagoon.c b/pkg/noun/jets/i/lagoon.c index eb92dc447f..4466018f48 100644 --- a/pkg/noun/jets/i/lagoon.c +++ b/pkg/noun/jets/i/lagoon.c @@ -2169,7 +2169,6 @@ { return u3m_bail(c3__exit); } else { - fprintf(stderr, "\r\nx_kind: %x\r\n", x_kind); switch (x_kind) { case c3__i754: _set_rounding(rnd); @@ -2177,7 +2176,6 @@ return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: - fprintf(stderr, "uint\r\n"); return u3_none; } } diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index 7b6224ba97..19335435f7 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -2125,34 +2125,34 @@ static u3j_core _139_hex_json_d[] = /* /lib jets in non core */ -static u3j_harm _139_non__lagoon_add_a[] = {{".2", u3wi_la_add, c3n}, {}}; -static u3j_harm _139_non__lagoon_sub_a[] = {{".2", u3wi_la_sub, c3n}, {}}; -static u3j_harm _139_non__lagoon_mul_a[] = {{".2", u3wi_la_mul, c3n}, {}}; -static u3j_harm _139_non__lagoon_div_a[] = {{".2", u3wi_la_div, c3n}, {}}; -static u3j_harm _139_non__lagoon_mod_a[] = {{".2", u3wi_la_mod, c3n}, {}}; -static u3j_harm _139_non__lagoon_adds_a[] = {{".2", u3wi_la_adds, c3n}, {}}; -static u3j_harm _139_non__lagoon_subs_a[] = {{".2", u3wi_la_subs, c3n}, {}}; -static u3j_harm _139_non__lagoon_muls_a[] = {{".2", u3wi_la_muls, c3n}, {}}; -static u3j_harm _139_non__lagoon_divs_a[] = {{".2", u3wi_la_divs, c3n}, {}}; -static u3j_harm _139_non__lagoon_mods_a[] = {{".2", u3wi_la_mods, c3n}, {}}; -static u3j_harm _139_non__lagoon_dot_a[] = {{".2", u3wi_la_dot, c3n}, {}}; -static u3j_harm _139_non__lagoon_trans_a[] ={{".2", u3wi_la_transpose, c3n}, {}}; -static u3j_harm _139_non__lagoon_cumsum_a[]={{".2", u3wi_la_cumsum, c3n}, {}}; -static u3j_harm _139_non__lagoon_argmin_a[]={{".2", u3wi_la_argmin, c3n}, {}}; -static u3j_harm _139_non__lagoon_argmax_a[]={{".2", u3wi_la_argmax, c3n}, {}}; -static u3j_harm _139_non__lagoon_ravel_a[]={{".2", u3wi_la_ravel, c3n}, {}}; -static u3j_harm _139_non__lagoon_min_a[] = {{".2", u3wi_la_min, c3n}, {}}; -static u3j_harm _139_non__lagoon_max_a[] = {{".2", u3wi_la_max, c3n}, {}}; -static u3j_harm _139_non__lagoon_linspace_a[]={{".2", u3wi_la_linspace, c3n}, {}}; -static u3j_harm _139_non__lagoon_range_a[]= {{".2", u3wi_la_range, c3n}, {}}; -static u3j_harm _139_non__lagoon_abs_a[] = {{".2", u3wi_la_abs, c3n}, {}}; -static u3j_harm _139_non__lagoon_gth_a[] = {{".2", u3wi_la_gth, c3n}, {}}; -static u3j_harm _139_non__lagoon_gte_a[] = {{".2", u3wi_la_gte, c3n}, {}}; -static u3j_harm _139_non__lagoon_lth_a[] = {{".2", u3wi_la_lth, c3n}, {}}; -static u3j_harm _139_non__lagoon_lte_a[] = {{".2", u3wi_la_lte, c3n}, {}}; -static u3j_harm _139_non__lagoon_diag_a[] = {{".2", u3wi_la_diag, c3n}, {}}; -static u3j_harm _139_non__lagoon_trace_a[]= {{".2", u3wi_la_trace, c3n}, {}}; -static u3j_harm _139_non__lagoon_mmul_a[] = {{".2", u3wi_la_mmul, c3n}, {}}; +static u3j_harm _139_non__lagoon_add_a[] = {{".2", u3wi_la_add}, {}}; +static u3j_harm _139_non__lagoon_sub_a[] = {{".2", u3wi_la_sub}, {}}; +static u3j_harm _139_non__lagoon_mul_a[] = {{".2", u3wi_la_mul}, {}}; +static u3j_harm _139_non__lagoon_div_a[] = {{".2", u3wi_la_div}, {}}; +static u3j_harm _139_non__lagoon_mod_a[] = {{".2", u3wi_la_mod}, {}}; +static u3j_harm _139_non__lagoon_adds_a[] = {{".2", u3wi_la_adds}, {}}; +static u3j_harm _139_non__lagoon_subs_a[] = {{".2", u3wi_la_subs}, {}}; +static u3j_harm _139_non__lagoon_muls_a[] = {{".2", u3wi_la_muls}, {}}; +static u3j_harm _139_non__lagoon_divs_a[] = {{".2", u3wi_la_divs}, {}}; +static u3j_harm _139_non__lagoon_mods_a[] = {{".2", u3wi_la_mods}, {}}; +static u3j_harm _139_non__lagoon_dot_a[] = {{".2", u3wi_la_dot}, {}}; +static u3j_harm _139_non__lagoon_trans_a[] ={{".2", u3wi_la_transpose}, {}}; +static u3j_harm _139_non__lagoon_cumsum_a[]={{".2", u3wi_la_cumsum}, {}}; +static u3j_harm _139_non__lagoon_argmin_a[]={{".2", u3wi_la_argmin}, {}}; +static u3j_harm _139_non__lagoon_argmax_a[]={{".2", u3wi_la_argmax}, {}}; +static u3j_harm _139_non__lagoon_ravel_a[]={{".2", u3wi_la_ravel}, {}}; +static u3j_harm _139_non__lagoon_min_a[] = {{".2", u3wi_la_min}, {}}; +static u3j_harm _139_non__lagoon_max_a[] = {{".2", u3wi_la_max}, {}}; +static u3j_harm _139_non__lagoon_linspace_a[]={{".2", u3wi_la_linspace}, {}}; +static u3j_harm _139_non__lagoon_range_a[]= {{".2", u3wi_la_range}, {}}; +static u3j_harm _139_non__lagoon_abs_a[] = {{".2", u3wi_la_abs}, {}}; +static u3j_harm _139_non__lagoon_gth_a[] = {{".2", u3wi_la_gth}, {}}; +static u3j_harm _139_non__lagoon_gte_a[] = {{".2", u3wi_la_gte}, {}}; +static u3j_harm _139_non__lagoon_lth_a[] = {{".2", u3wi_la_lth}, {}}; +static u3j_harm _139_non__lagoon_lte_a[] = {{".2", u3wi_la_lte}, {}}; +static u3j_harm _139_non__lagoon_diag_a[] = {{".2", u3wi_la_diag}, {}}; +static u3j_harm _139_non__lagoon_trace_a[]= {{".2", u3wi_la_trace}, {}}; +static u3j_harm _139_non__lagoon_mmul_a[] = {{".2", u3wi_la_mmul}, {}}; static u3j_core _139_non__la_core_d[] = { { "add-rays", 7, _139_non__lagoon_add_a, 0, no_hashes }, { "sub-rays", 7, _139_non__lagoon_sub_a, 0, no_hashes }, @@ -2169,7 +2169,7 @@ static u3j_core _139_non__la_core_d[] = { "cumsum", 7, _139_non__lagoon_cumsum_a, 0, no_hashes }, { "argmin", 7, _139_non__lagoon_argmin_a, 0, no_hashes }, { "argmax", 7, _139_non__lagoon_argmax_a, 0, no_hashes }, - { "ravel", 7, _139_non__lagoon_ravel_a, 0, no_hashes }, + // { "ravel", 7, _139_non__lagoon_ravel_a, 0, no_hashes }, { "min", 7, _139_non__lagoon_min_a, 0, no_hashes }, { "max", 7, _139_non__lagoon_max_a, 0, no_hashes }, { "linspace", 7, _139_non__lagoon_linspace_a, 0, no_hashes }, From c7bb5ec543e13b17bb77d39133a0f78dfda196c2 Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Fri, 26 Jul 2024 17:27:02 -0400 Subject: [PATCH 287/430] mesa: retry outgoing requests on a timer --- pkg/vere/io/mesa.c | 128 +++++++++++++++++++++++++++++++++------- pkg/vere/io/mesa/pact.c | 2 +- 2 files changed, 109 insertions(+), 21 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 321cbc8d4c..57fb104817 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -946,17 +946,47 @@ static void _mesa_send(u3_mesa_pict* pic_u, u3_lane* lan_u) _mesa_send_buf(sam_u, *lan_u, buf_y, siz_w); } -/* _mesa_send_modal(): send packet on lanes based on direct vs. indirect mode -*/ +typedef struct _u3_mesa_request_data { + u3_mesa* sam_u; + u3_ship her_u; + u3_mesa_name* nam_u; + c3_y* buf_y; + c3_w len_w; + u3_noun las; +} u3_mesa_request_data; + +typedef struct _u3_mesa_resend_data { + uv_timer_t tim_u; + u3_mesa_request_data dat_u; + c3_y ret_y; // number of remaining retries +} u3_mesa_resend_data; + +static void +_mesa_free_request_data(u3_mesa_request_data* dat_u) +{ + c3_free(dat_u->nam_u->pat_c); + c3_free(dat_u->nam_u); + c3_free(dat_u->buf_y); + u3z(dat_u->las); + c3_free(dat_u); +} + +static void +_mesa_send_bufs(u3_mesa* sam_u, + u3_peer* per_u, + c3_y* buf_y, + c3_w len_w, + u3_noun las); + static void -_mesa_send_modal(u3_peer* per_u, c3_y* buf_y, c3_w siz_w) +_mesa_send_modal(u3_peer* per_u, c3_y* buf_y, c3_w len_w) { u3_mesa* sam_u = per_u->sam_u; c3_d now_d = _get_now_micros(); if ( c3y == _mesa_is_direct_mode(per_u) ) { u3l_log("mesa: direct"); - _mesa_send_buf(sam_u, per_u->dan_u, buf_y, siz_w); + _mesa_send_buf(sam_u, per_u->dan_u, buf_y, len_w); per_u->dir_u.sen_d = now_d; } else { @@ -969,17 +999,34 @@ _mesa_send_modal(u3_peer* per_u, c3_y* buf_y, c3_w siz_w) // after a restart of the driver // u3_lane imp_u = _mesa_get_czar_lane(sam_u, per_u->imp_y); - _mesa_send_buf(sam_u, imp_u, buf_y, siz_w); + _mesa_send_buf(sam_u, imp_u, buf_y, len_w); per_u->ind_u.sen_d = now_d; if ( (c3n == _mesa_is_lane_zero(&per_u->dan_u)) && - (per_u->dir_u.sen_d + DIRECT_ROUTE_RETRY_MICROS > now_d)) { - _mesa_send_buf(sam_u, per_u->dan_u, buf_y, siz_w); + (per_u->dir_u.sen_d + DIRECT_ROUTE_RETRY_MICROS > now_d)) { + _mesa_send_buf(sam_u, per_u->dan_u, buf_y, len_w); per_u->dir_u.sen_d = now_d; } } } +static void +_mesa_send_request(u3_mesa_request_data* dat_u) +{ + u3_peer* per_u = _mesa_get_peer(dat_u->sam_u, dat_u->her_u); + if ( !per_u ) { + _mesa_send_bufs(dat_u->sam_u, + NULL, + dat_u->buf_y, + dat_u->len_w, + u3k(dat_u->las)); + } + else { + _mesa_send_modal(per_u, dat_u->buf_y, dat_u->len_w); + } + u3z(dat_u->las); +} + static void _try_resend(u3_pend_req* req_u, c3_w ack_w) { @@ -1461,6 +1508,45 @@ _mesa_add_our_to_pit(u3_mesa* sam_u, u3_mesa_name* nam_u) u3z(pin); return; } +static void +_mesa_free_resend_data(u3_mesa_resend_data* res_u) +{ + uv_timer_stop(&res_u->tim_u); + _mesa_free_request_data(&res_u->dat_u); +} + +static void +_mesa_set_resend_timer(u3_mesa_resend_data* dat_u); + +static void +_mesa_resend_timer_cb(uv_timer_t* tim_u) +{ + u3_mesa_resend_data* res_u = (u3_mesa_resend_data*)tim_u; + u3_mesa_request_data* dat_u = &res_u->dat_u; + res_u->ret_y--; + + u3_weak pit = _mesa_get_pit(dat_u->sam_u, dat_u->nam_u); + if ( u3_none == pit ) { + _mesa_free_resend_data(res_u); + return; + } + u3z(pit); + + _mesa_send_request(dat_u); + + if ( res_u->ret_y ) { + _mesa_set_resend_timer(res_u); + } + else { + _mesa_free_resend_data(res_u); + } +} + +static void +_mesa_set_resend_timer(u3_mesa_resend_data* dat_u) +{ + uv_timer_start(&dat_u->tim_u, _mesa_resend_timer_cb, 1, 0); +} static void _mesa_ef_send(u3_mesa* sam_u, u3_noun las, u3_noun pac) @@ -1482,16 +1568,6 @@ _mesa_ef_send(u3_mesa* sam_u, u3_noun las, u3_noun pac) u3_mesa_pact pac_u; c3_w res_w = mesa_sift_pact(&pac_u, buf_y, len_w); - u3_peer* per_u; - { - u3_ship her_u; - _get_her(&pac_u, her_u); - per_u = _mesa_get_peer(sam_u, her_u); - } - - if ( (PACT_PEEK == hed_u.typ_y) || (PACT_POKE == hed_u.typ_y) ) { - _mesa_add_our_to_pit(sam_u, &pac_u.pek_u.nam_u); - } if ( PACT_PAGE == hed_u.typ_y ) { u3_weak pin = _mesa_get_pit(sam_u, &pac_u.pek_u.nam_u); @@ -1511,11 +1587,23 @@ _mesa_ef_send(u3_mesa* sam_u, u3_noun las, u3_noun pac) _mesa_del_pit(sam_u, &pac_u.pek_u.nam_u); u3z(pin); } - }else if ( per_u ) { - _mesa_send_modal(per_u, buf_y, len_w); } else { - _mesa_send_bufs(sam_u, NULL, buf_y, len_w, u3k(las)); + u3_mesa_name* nam_u = &pac_u.pek_u.nam_u; + u3_mesa_resend_data* res_u = c3_malloc(sizeof(u3_mesa_resend_data)); + u3_mesa_request_data* dat_u = &res_u->dat_u; + { + dat_u->sam_u = sam_u; + _get_her(&pac_u, dat_u->her_u); + dat_u->nam_u = nam_u; + } + { + res_u->ret_y = 4; + uv_timer_init(u3L, &res_u->tim_u); + } + _mesa_add_our_to_pit(sam_u, nam_u); + _mesa_send_request(dat_u); + _mesa_set_resend_timer(res_u); } sam_u->tim_d = _get_now_micros(); diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index 7275abab00..881ec3afc6 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -201,7 +201,7 @@ _mesa_rank(u3_ship who_u) */ void mesa_free_pact(u3_mesa_pact* pac_u) { - // TODO free pat_c in the name + c3_free(pac_u->pek_u.nam_u.pat_c); switch ( pac_u->hed_u.typ_y ) { default: { break; From a3e7b428eb2d0add6a03d3ed213582bc84e3ccd9 Mon Sep 17 00:00:00 2001 From: Sigilante Date: Fri, 26 Jul 2024 19:44:23 -0500 Subject: [PATCH 288/430] Post working jets. --- pkg/noun/jets/i/lagoon.c | 138 +++++++++++++++++++++++++++------------ pkg/noun/jets/tree.c | 2 +- 2 files changed, 97 insertions(+), 43 deletions(-) diff --git a/pkg/noun/jets/i/lagoon.c b/pkg/noun/jets/i/lagoon.c index 4466018f48..01e8e9954a 100644 --- a/pkg/noun/jets/i/lagoon.c +++ b/pkg/noun/jets/i/lagoon.c @@ -108,6 +108,26 @@ return dims; } +/* check consistency of array shape and bloq size + |= =ray + ^- ? + .= (roll shape.meta.ray ^mul) + (dec (met bloq.meta.ray data.ray)) +*/ + static inline c3_o _check(u3_noun ray) + { + // Calculate expected size. + u3_atom shp = u3h(u3h(ray)); // (reported) shape of ray, +4 + u3_atom blq = u3h(u3t(u3h(ray))); // block size of ray, +10 + u3_atom sin = _get_length(shp); // calculated length of ray + + // Calculate actual size. + u3_atom len = u3r_met(blq, u3t(ray)); // length of ray + u3_atom dex = u3qa_dec(len); // decrement length b/c of pinned 1 + + return __(sin == dex); + } + /* add - axpy = 1*x+y */ u3_noun @@ -1859,14 +1879,14 @@ u3r_bytes(0, 2, (c3_y*)&(b16.v), b); float16_t span16 = f16_sub(b16, a16); float16_t interval16 = f16_div(span16, i32_to_f16(n-1)); - c3_y* x_bytes16 = (c3_y*)u3a_malloc(((n+1)*2+1)*sizeof(c3_y)); - for (c3_d i = 1; i <= n; i++) { - ((float16_t*)x_bytes16)[n-i] = f16_add(a16, f16_mul(i32_to_f16(i), interval16)); - } - ((float16_t*)x_bytes16)[n] = a16; - ((float16_t*)x_bytes16)[0] = b16; - x_bytes16[(n+1)*2] = 0x1; // pin head - r_data = u3i_bytes(((n+1)*2+1)*sizeof(c3_y), x_bytes16); + c3_y* x_bytes16 = (c3_y*)u3a_malloc((n*2+1)*sizeof(c3_y)); + for (c3_d i = 1; i < n-1; i++) { + ((float16_t*)x_bytes16)[i] = f16_add(a16, f16_mul(i32_to_f16(i), interval16)); + } + ((float16_t*)x_bytes16)[0] = a16; + ((float16_t*)x_bytes16)[n-1] = b16; + x_bytes16[n*2] = 0x1; // pin head + r_data = u3i_bytes((n*2+1)*sizeof(c3_y), x_bytes16); u3a_free(x_bytes16); break;} @@ -1876,14 +1896,14 @@ u3r_bytes(0, 4, (c3_y*)&(b32.v), b); float32_t span32 = f32_sub(b32, a32); float32_t interval32 = f32_div(span32, i32_to_f32(n-1)); - c3_y* x_bytes32 = (c3_y*)u3a_malloc(((n+1)*4+1)*sizeof(c3_y)); - for (c3_d i = 1; i <= n; i++) { - ((float32_t*)x_bytes32)[n-i] = f32_add(a32, f32_mul(i32_to_f32(i), interval32)); - } - ((float32_t*)x_bytes32)[n] = a32; - ((float32_t*)x_bytes32)[0] = b32; - x_bytes32[(n+1)*4] = 0x1; // pin head - r_data = u3i_bytes(((n+1)*4+1)*sizeof(c3_y), x_bytes32); + c3_y* x_bytes32 = (c3_y*)u3a_malloc((n*4+1)*sizeof(c3_y)); + for (c3_d i = 1; i < n-1; i++) { + ((float32_t*)x_bytes32)[i] = f32_add(a32, f32_mul(i32_to_f32(i), interval32)); + } + ((float32_t*)x_bytes32)[0] = a32; + ((float32_t*)x_bytes32)[n-1] = b32; + x_bytes32[n*4] = 0x1; // pin head + r_data = u3i_bytes((n*4+1)*sizeof(c3_y), x_bytes32); u3a_free(x_bytes32); break;} @@ -1893,14 +1913,14 @@ u3r_bytes(0, 8, (c3_y*)&(b64.v), b); float64_t span64 = f64_sub(b64, a64); float64_t interval64 = f64_div(span64, i32_to_f64(n-1)); - c3_y* x_bytes64 = (c3_y*)u3a_malloc(((n+1)*8+1)*sizeof(c3_y)); - for (c3_d i = 1; i < n; i++) { - ((float64_t*)x_bytes64)[n-i] = f64_add(a64, f64_mul(i32_to_f64(i), interval64)); - } - ((float64_t*)x_bytes64)[n] = a64; - ((float64_t*)x_bytes64)[0] = b64; - x_bytes64[(n+1)*8] = 0x1; // pin head - r_data = u3i_bytes(((n+1)*8+1)*sizeof(c3_y), x_bytes64); + c3_y* x_bytes64 = (c3_y*)u3a_malloc((n*8+1)*sizeof(c3_y)); + for (c3_d i = 1; i < n-1; i++) { + ((float64_t*)x_bytes64)[i] = f64_add(a64, f64_mul(i32_to_f64(i), interval64)); + } + ((float64_t*)x_bytes64)[0] = a64; + ((float64_t*)x_bytes64)[n-1] = b64; + x_bytes64[n*8] = 0x1; // pin head + r_data = u3i_bytes((n*8+1)*sizeof(c3_y), x_bytes64); u3a_free(x_bytes64); break;} @@ -1914,17 +1934,17 @@ float128_t n128; i32_to_f128M(n-1, &n128); f128M_div(&span128, &n128, &interval128); - c3_y* x_bytes128 = (c3_y*)u3a_malloc(((n+1)*16+1)*sizeof(c3_y)); + c3_y* x_bytes128 = (c3_y*)u3a_malloc((n*16+1)*sizeof(c3_y)); float128_t i128; - for (c3_d i = 1; i < n; i++) { + for (c3_d i = 1; i < n-1; i++) { i32_to_f128M(i, &i128); - f128M_mul(&i128, &interval128, &((float128_t*)x_bytes128)[n-i]); - f128M_add(&a128, &((float128_t*)x_bytes128)[n-i], &((float128_t*)x_bytes128)[n-i]); + f128M_mul(&i128, &interval128, &((float128_t*)x_bytes128)[i]); + f128M_add(&a128, &((float128_t*)x_bytes128)[i], &((float128_t*)x_bytes128)[i]); } - ((float128_t*)x_bytes128)[n] = a128; - ((float128_t*)x_bytes128)[0] = b128; - x_bytes128[(n+1)*16] = 0x1; // pin head - r_data = u3i_bytes(((n+1)*16+1)*sizeof(c3_y), x_bytes128); + ((float128_t*)x_bytes128)[0] = a128; + ((float128_t*)x_bytes128)[n-1] = b128; + x_bytes128[n*16] = 0x1; // pin head + r_data = u3i_bytes((n*16+1)*sizeof(c3_y), x_bytes128); u3a_free(x_bytes128); break;} } @@ -2173,6 +2193,7 @@ case c3__i754: _set_rounding(rnd); u3_noun r_data = _soft_run(u3qi_la_add_i754(x_data, y_data, x_shape, x_bloq)); + if (r_data == u3_none) { return u3_none; } return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2228,6 +2249,7 @@ case c3__i754: _set_rounding(rnd); u3_noun r_data = _soft_run(u3qi_la_sub_i754(x_data, y_data, x_shape, x_bloq)); + if (r_data == u3_none) { return u3_none; } return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2283,6 +2305,7 @@ case c3__i754: _set_rounding(rnd); u3_noun r_data = _soft_run(u3qi_la_mul_i754(x_data, y_data, x_shape, x_bloq)); + if (r_data == u3_none) { return u3_none; } return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2338,6 +2361,7 @@ case c3__i754: _set_rounding(rnd); u3_noun r_data = _soft_run(u3qi_la_div_i754(x_data, y_data, x_shape, x_bloq)); + if (r_data == u3_none) { return u3_none; } return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2393,6 +2417,7 @@ case c3__i754: _set_rounding(rnd); u3_noun r_data = _soft_run(u3qi_la_mod_i754(x_data, y_data, x_shape, x_bloq)); + if (r_data == u3_none) { return u3_none; } return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2424,7 +2449,8 @@ x_fxp = u3t(u3t(u3t(x_meta))); // 15 rnd = u3h(u3t(u3t(u3t(cor)))); // 30 if ( c3n == u3ud(x_bloq) || - c3n == u3ud(x_kind) + c3n == u3ud(x_kind) || + c3n == _check(u3nc(x_meta, x_data)) ) { return u3m_bail(c3__exit); @@ -2433,6 +2459,7 @@ case c3__i754: _set_rounding(rnd); u3_noun r_data = _soft_run(u3qi_la_cumsum_i754(x_data, x_shape, x_bloq)); + if (r_data == u3_none) { return u3_none; } return u3nc(u3nq(u3nt(0x1, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2462,7 +2489,8 @@ x_kind = u3h(u3t(u3t(x_meta))); // 14 x_fxp = u3t(u3t(u3t(x_meta))); // 15 if ( c3n == u3ud(x_bloq) || - c3n == u3ud(x_kind) + c3n == u3ud(x_kind) || + c3n == _check(u3nc(x_meta, x_data)) ) { return u3m_bail(c3__exit); @@ -2499,7 +2527,8 @@ x_bloq = u3h(u3t(x_meta)); // 6 x_kind = u3h(u3t(u3t(x_meta))); // 14 if ( c3n == u3ud(x_bloq) || - c3n == u3ud(x_kind) + c3n == u3ud(x_kind) || + c3n == _check(u3nc(x_meta, x_data)) ) { return u3m_bail(c3__exit); @@ -2537,7 +2566,8 @@ x_kind = u3h(u3t(u3t(x_meta))); // 14 x_fxp = u3t(u3t(u3t(x_meta))); // 15 if ( c3n == u3ud(x_bloq) || - c3n == u3ud(x_kind) + c3n == u3ud(x_kind) || + c3n == _check(u3nc(x_meta, x_data)) ) { return u3m_bail(c3__exit); @@ -2575,7 +2605,8 @@ x_kind = u3h(u3t(u3t(x_meta))); // 14 x_fxp = u3t(u3t(u3t(x_meta))); // 15 if ( c3n == u3ud(x_bloq) || - c3n == u3ud(x_kind) + c3n == u3ud(x_kind) || + c3n == _check(u3nc(x_meta, x_data)) ) { return u3m_bail(c3__exit); @@ -2583,6 +2614,7 @@ switch (x_kind) { case c3__i754: { u3_noun r_data = _soft_run(u3qi_la_min_i754(x_data, x_shape, x_bloq)); + if (r_data == u3_none) { return u3_none; } return u3nc(u3nq(u3nt(0x1, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data);} default: @@ -2612,7 +2644,8 @@ x_kind = u3h(u3t(u3t(x_meta))); // 14 x_fxp = u3t(u3t(u3t(x_meta))); // 15 if ( c3n == u3ud(x_bloq) || - c3n == u3ud(x_kind) + c3n == u3ud(x_kind) || + c3n == _check(u3nc(x_meta, x_data)) ) { return u3m_bail(c3__exit); @@ -2620,6 +2653,7 @@ switch (x_kind) { case c3__i754: { u3_noun r_data = _soft_run(u3qi_la_max_i754(x_data, x_shape, x_bloq)); + if (r_data == u3_none) { return u3_none; } return u3nc(u3nq(u3nt(0x1, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data);} default: @@ -2657,6 +2691,7 @@ switch (x_kind) { case c3__i754: { u3_noun r_data = _soft_run(u3qi_la_abs_i754(x_data, x_shape, x_bloq)); + if (r_data == u3_none) { return u3_none; } return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data);} default: @@ -2710,6 +2745,7 @@ switch (x_kind) { case c3__i754: { u3_noun r_data = _soft_run(u3qi_la_gth_i754(x_data, y_data, x_shape, x_bloq)); + if (r_data == u3_none) { return u3_none; } return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data);} default: @@ -2763,6 +2799,7 @@ switch (x_kind) { case c3__i754: { u3_noun r_data = _soft_run(u3qi_la_gte_i754(x_data, y_data, x_shape, x_bloq)); + if (r_data == u3_none) { return u3_none; } return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data);} default: @@ -2816,6 +2853,7 @@ switch (x_kind) { case c3__i754: { u3_noun r_data = _soft_run(u3qi_la_lth_i754(x_data, y_data, x_shape, x_bloq)); + if (r_data == u3_none) { return u3_none; } return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data);} default: @@ -2869,6 +2907,7 @@ switch (x_kind) { case c3__i754: { u3_noun r_data = _soft_run(u3qi_la_lte_i754(x_data, y_data, x_shape, x_bloq)); + if (r_data == u3_none) { return u3_none; } return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data);} default: @@ -2905,6 +2944,7 @@ case c3__i754: _set_rounding(rnd); u3_noun r_data = _soft_run(u3qi_la_adds_i754(x_data, n, x_shape, x_bloq)); + if (r_data == u3_none) { return u3_none; } return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2940,6 +2980,7 @@ case c3__i754: _set_rounding(rnd); u3_noun r_data = _soft_run(u3qi_la_subs_i754(x_data, n, x_shape, x_bloq)); + if (r_data == u3_none) { return u3_none; } return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -2975,6 +3016,7 @@ case c3__i754: _set_rounding(rnd); u3_noun r_data = _soft_run(u3qi_la_muls_i754(x_data, n, x_shape, x_bloq)); + if (r_data == u3_none) { return u3_none; } return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -3010,6 +3052,7 @@ case c3__i754: _set_rounding(rnd); u3_noun r_data = _soft_run(u3qi_la_divs_i754(x_data, n, x_shape, x_bloq)); + if (r_data == u3_none) { return u3_none; } return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -3045,6 +3088,7 @@ case c3__i754: _set_rounding(rnd); u3_noun r_data = _soft_run(u3qi_la_mods_i754(x_data, n, x_shape, x_bloq)); + if (r_data == u3_none) { return u3_none; } return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -3099,6 +3143,7 @@ case c3__i754: _set_rounding(rnd); u3_noun r_data = _soft_run(u3qi_la_dot_i754(x_data, y_data, x_shape, x_bloq)); + if (r_data == u3_none) { return u3_none; } c3_d len_x0 = _get_dims(x_shape)[0]; return u3nc(u3nq(u3nt(len_x0, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); @@ -3129,12 +3174,14 @@ x_kind = u3h(u3t(u3t(x_meta))); // 14 x_fxp = u3t(u3t(u3t(x_meta))); // 15 if ( c3n == u3ud(x_bloq) || - c3n == u3ud(x_kind) + c3n == u3ud(x_kind) || + c3n == _check(cor) ) { return u3m_bail(c3__exit); } else { u3_noun r_data = _soft_run(u3qi_la_transpose(x_data, x_shape, x_bloq)); + if (r_data == u3_none) { return u3_none; } return u3nc(u3nq(u3nt(u3k(u3h(x_shape)), u3k(u3h(u3t(x_shape))), u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); } } @@ -3170,7 +3217,8 @@ case c3__i754: _set_rounding(rnd); u3_noun r_data = _soft_run(u3qi_la_linspace_i754(a, b, n, x_bloq)); - x_shape = u3nt(u3x_atom(n), 0x1, u3_nul); + if (r_data == u3_none) { return u3_none; } + x_shape = u3nc(u3x_atom(n), u3_nul); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: @@ -3210,6 +3258,7 @@ case c3__i754: _set_rounding(rnd); u3_noun r_data = _soft_run(u3qi_la_range_i754(a, b, d, x_bloq)); + if (r_data == u3_none) { return u3_none; } c3_d a_, b_, d_; c3_ds n_; switch (x_bloq) { @@ -3273,12 +3322,14 @@ x_kind = u3h(u3t(u3t(x_meta))); // 14 x_fxp = u3t(u3t(u3t(x_meta))); // 15 if ( c3n == u3ud(x_bloq) || - c3n == u3ud(x_kind) + c3n == u3ud(x_kind) || + c3n == _check(cor) ) { return u3m_bail(c3__exit); } else { u3_noun r_data = _soft_run(u3qi_la_diag(x_data, x_shape, x_bloq)); + if (r_data == u3_none) { return u3_none; } c3_d len_x0 = _get_dims(x_shape)[0]; return u3nc(u3nq(u3nt(len_x0, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); } @@ -3313,6 +3364,7 @@ switch (x_kind) { case c3__i754: { u3_noun r_data = _soft_run(u3qi_la_trace_i754(x_data, x_shape, x_bloq)); + if (r_data == u3_none) { return u3_none; } return u3nc(u3nq(u3nt(0x1, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data);} default: @@ -3353,7 +3405,9 @@ y_fxp = u3t(u3t(u3t(y_meta))); // 15 rnd = u3h(u3t(u3t(u3t(cor)))); // 30 if ( c3n == u3r_sing(x_bloq, y_bloq) || - c3n == u3r_sing(x_kind, y_kind) + c3n == u3r_sing(x_kind, y_kind) || + c3n == _check(u3nc(x_meta, x_data)) || + c3n == _check(u3nc(y_meta, y_data)) // fxp does not need to match so no check ) { diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index 19335435f7..7e790b21f5 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -2169,7 +2169,7 @@ static u3j_core _139_non__la_core_d[] = { "cumsum", 7, _139_non__lagoon_cumsum_a, 0, no_hashes }, { "argmin", 7, _139_non__lagoon_argmin_a, 0, no_hashes }, { "argmax", 7, _139_non__lagoon_argmax_a, 0, no_hashes }, - // { "ravel", 7, _139_non__lagoon_ravel_a, 0, no_hashes }, + { "ravel", 7, _139_non__lagoon_ravel_a, 0, no_hashes }, { "min", 7, _139_non__lagoon_min_a, 0, no_hashes }, { "max", 7, _139_non__lagoon_max_a, 0, no_hashes }, { "linspace", 7, _139_non__lagoon_linspace_a, 0, no_hashes }, From 1d8792832e7c6a6c9dd4fb21a9d31cb622bdc786 Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Mon, 29 Jul 2024 10:39:40 -0400 Subject: [PATCH 289/430] mesa: fix crash in request retry --- pkg/vere/io/mesa.c | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 57fb104817..b680fc55f4 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -968,7 +968,15 @@ _mesa_free_request_data(u3_mesa_request_data* dat_u) c3_free(dat_u->nam_u); c3_free(dat_u->buf_y); u3z(dat_u->las); - c3_free(dat_u); + // does not free dat_u itself, since it could be in a u3_mesa_resend_data +} + +static void +_mesa_free_resend_data(u3_mesa_resend_data* res_u) +{ + uv_timer_stop(&res_u->tim_u); + _mesa_free_request_data(&res_u->dat_u); + c3_free(res_u); } static void @@ -1508,15 +1516,6 @@ _mesa_add_our_to_pit(u3_mesa* sam_u, u3_mesa_name* nam_u) u3z(pin); return; } -static void -_mesa_free_resend_data(u3_mesa_resend_data* res_u) -{ - uv_timer_stop(&res_u->tim_u); - _mesa_free_request_data(&res_u->dat_u); -} - -static void -_mesa_set_resend_timer(u3_mesa_resend_data* dat_u); static void _mesa_resend_timer_cb(uv_timer_t* tim_u) @@ -1527,27 +1526,29 @@ _mesa_resend_timer_cb(uv_timer_t* tim_u) u3_weak pit = _mesa_get_pit(dat_u->sam_u, dat_u->nam_u); if ( u3_none == pit ) { + #ifdef MESA_DEBUG + u3l_log("mesa: resend PIT entry gone %u", res_u->ret_y); + #endif _mesa_free_resend_data(res_u); return; } + else { + #ifdef MESA_DEBUG + u3l_log("mesa: resend %u", res_u->ret_y); + #endif + } u3z(pit); _mesa_send_request(dat_u); if ( res_u->ret_y ) { - _mesa_set_resend_timer(res_u); + uv_timer_start(&res_u->tim_u, _mesa_resend_timer_cb, 1000, 0); } else { _mesa_free_resend_data(res_u); } } -static void -_mesa_set_resend_timer(u3_mesa_resend_data* dat_u) -{ - uv_timer_start(&dat_u->tim_u, _mesa_resend_timer_cb, 1, 0); -} - static void _mesa_ef_send(u3_mesa* sam_u, u3_noun las, u3_noun pac) { @@ -1589,7 +1590,8 @@ _mesa_ef_send(u3_mesa* sam_u, u3_noun las, u3_noun pac) } } else { - u3_mesa_name* nam_u = &pac_u.pek_u.nam_u; + u3_mesa_name* nam_u = c3_malloc(sizeof(u3_mesa_name)); + memcpy(nam_u, &pac_u.pek_u.nam_u, sizeof(u3_mesa_name)); u3_mesa_resend_data* res_u = c3_malloc(sizeof(u3_mesa_resend_data)); u3_mesa_request_data* dat_u = &res_u->dat_u; { @@ -1603,12 +1605,12 @@ _mesa_ef_send(u3_mesa* sam_u, u3_noun las, u3_noun pac) } _mesa_add_our_to_pit(sam_u, nam_u); _mesa_send_request(dat_u); - _mesa_set_resend_timer(res_u); + uv_timer_start(&res_u->tim_u, _mesa_resend_timer_cb, 1000, 0); } sam_u->tim_d = _get_now_micros(); - // c3_free(buf_y); + // TODO: free pac_u or any fields in pac_u? u3z(pac); u3z(las); } From 23033f8ec0e0d49d6f3da1f0cc74da0bbf6d6413 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Mon, 29 Jul 2024 18:52:18 +0200 Subject: [PATCH 290/430] mesa: fix memory bug --- pkg/vere/io/mesa.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index b680fc55f4..29d987ae45 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -902,6 +902,7 @@ static void _mesa_send_buf(u3_mesa* sam_u, u3_lane lan_u, c3_y* buf_y, c3_w len_ { u3_seal* sel_u = c3_calloc(sizeof(*sel_u)); // this is wrong, need to calloc & memcpy + // XX calloc & memcpy done before the _mesa_send_buf callsite sel_u->buf_y = buf_y; sel_u->len_w = len_w; sel_u->sam_u = sam_u; @@ -992,9 +993,12 @@ _mesa_send_modal(u3_peer* per_u, c3_y* buf_y, c3_w len_w) u3_mesa* sam_u = per_u->sam_u; c3_d now_d = _get_now_micros(); + c3_y* sen_y = c3_calloc(len_w); + memcpy(sen_y, buf_y, len_w); + if ( c3y == _mesa_is_direct_mode(per_u) ) { u3l_log("mesa: direct"); - _mesa_send_buf(sam_u, per_u->dan_u, buf_y, len_w); + _mesa_send_buf(sam_u, per_u->dan_u, sen_y, len_w); per_u->dir_u.sen_d = now_d; } else { @@ -1007,12 +1011,12 @@ _mesa_send_modal(u3_peer* per_u, c3_y* buf_y, c3_w len_w) // after a restart of the driver // u3_lane imp_u = _mesa_get_czar_lane(sam_u, per_u->imp_y); - _mesa_send_buf(sam_u, imp_u, buf_y, len_w); + _mesa_send_buf(sam_u, imp_u, sen_y, len_w); per_u->ind_u.sen_d = now_d; if ( (c3n == _mesa_is_lane_zero(&per_u->dan_u)) && - (per_u->dir_u.sen_d + DIRECT_ROUTE_RETRY_MICROS > now_d)) { - _mesa_send_buf(sam_u, per_u->dan_u, buf_y, len_w); + (per_u->dir_u.sen_d + DIRECT_ROUTE_RETRY_MICROS > now_d)) { + _mesa_send_buf(sam_u, per_u->dan_u, sen_y, len_w); per_u->dir_u.sen_d = now_d; } } From b00825e9786517e01f10336cb067dfb7acab46ad Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Mon, 29 Jul 2024 11:16:16 -0400 Subject: [PATCH 291/430] u3: corrects recursive deletion in _ch_node_del(), refactors --- pkg/noun/hashtable.c | 102 ++++++++++++++++++++++++------------------- 1 file changed, 58 insertions(+), 44 deletions(-) diff --git a/pkg/noun/hashtable.c b/pkg/noun/hashtable.c index 4da9ab757a..0de127c4a8 100644 --- a/pkg/noun/hashtable.c +++ b/pkg/noun/hashtable.c @@ -270,12 +270,10 @@ u3h_put(u3p(u3h_root) har_p, u3_noun key, u3_noun val) } } -static c3_o _ch_slot_del(u3h_slot*, u3_noun, c3_w, c3_w, c3_w*); - /* _ch_buck_del(): delete from bucket */ static c3_o -_ch_buck_del(u3h_slot* sot_w, u3_noun key, c3_w *use_w) +_ch_buck_del(u3h_slot* sot_w, u3_noun key) { u3h_buck* hab_u = u3h_slot_to_node(*sot_w); c3_w fin_w = hab_u->len_w; @@ -299,37 +297,41 @@ _ch_buck_del(u3h_slot* sot_w, u3_noun key, c3_w *use_w) { hab_u->len_w--; - u3_assert(c3y == u3h_slot_is_noun(hab_u->sot_w[fin_w])); - //u3z(u3h_slot_to_noun(hab_u->sot_w[fin_w])); + // u3_assert(c3y == u3h_slot_is_noun(hab_u->sot_w[fin_w])); for ( i_w = fin_w; i_w < hab_u->len_w; i_w++ ) { hab_u->sot_w[i_w] = hab_u->sot_w[i_w + 1]; } - *use_w -= 1; return c3y; } } -static c3_o _ch_some_del(u3h_slot*, u3_noun, c3_w, c3_w, c3_w*); +static c3_o _ch_some_del(u3h_slot*, u3_noun, c3_w, c3_w); +/* _ch_slot_del(): delete from slot +*/ static c3_o -_ch_slot_del(u3h_slot* sot_w, u3_noun key, c3_w lef_w, c3_w rem_w, c3_w* use_w) { +_ch_slot_del(u3h_slot* sot_w, u3_noun key, c3_w lef_w, c3_w rem_w) +{ if ( c3y == u3h_slot_is_noun(*sot_w) ) { u3_noun kev = u3h_slot_to_noun(*sot_w); *sot_w = 0; u3z(kev); - *use_w -= 1; return c3y; - } else { - return _ch_some_del(sot_w, key, lef_w, rem_w, use_w); + } + else { + return _ch_some_del(sot_w, key, lef_w, rem_w); } } +/* _ch_slot_del(): delete from node +*/ static c3_o -_ch_node_del(u3h_slot* sot_w, u3_noun key, c3_w lef_w, c3_w rem_w, c3_w* use_w) +_ch_node_del(u3h_slot* sot_w, u3_noun key, c3_w lef_w, c3_w rem_w) { u3h_node* han_u = (u3h_node*) u3h_slot_to_node(*sot_w); - + u3h_slot* tos_w; + c3_w bit_w, inx_w, map_w, i_w; lef_w -= 5; @@ -338,49 +340,61 @@ _ch_node_del(u3h_slot* sot_w, u3_noun key, c3_w lef_w, c3_w rem_w, c3_w* use_w) map_w = han_u->map_w; inx_w = _ch_popcount(CUT_END(map_w, bit_w)); + tos_w = &(han_u->sot_w[inx_w]); + // nothing at slot, no-op if ( !BIT_SET(map_w, bit_w) ) { return c3n; } - c3_w ken_w; - u3h_slot kes_w; - u3h_slot don_w = han_u->sot_w[inx_w]; - c3_w len_w = _ch_popcount(map_w); - if ( len_w == 2 && ((ken_w = (0 == inx_w) ? 1 : 0), - (kes_w = han_u->sot_w[ken_w]), - (c3y == u3h_slot_is_noun(don_w) && c3y == u3h_slot_is_noun(kes_w))) ) { - *sot_w = kes_w; - u3z(u3h_slot_to_noun(don_w)); - *use_w -= 1; - u3a_wfree(han_u); + + if ( c3n == _ch_slot_del(tos_w, key, lef_w, rem_w) ) { + // nothing deleted + return c3n; + } + else if ( 0 != *tos_w ) { + // something deleted, but slot still has value return c3y; } - c3_o ret_o = _ch_slot_del(&han_u->sot_w[inx_w], key, lef_w, rem_w, use_w); - if ( c3y == ret_o && c3y == u3h_slot_is_null(han_u->sot_w[inx_w]) ) { - han_u->map_w &= ~(1 << bit_w); - for ( i_w = inx_w; i_w < len_w; i_w++ ) { - han_u->sot_w[i_w] = han_u->sot_w[i_w + 1]; + else { + // shrink! + c3_w i_w, ken_w, len_w = _ch_popcount(map_w); + u3h_slot kes_w; + + if ( 2 == len_w && ((ken_w = (0 == inx_w) ? 1 : 0), + (kes_w = han_u->sot_w[ken_w]), + (c3y == u3h_slot_is_noun(kes_w))) ) + { + // only one side left, and the other is a noun. debucketize. + *sot_w = kes_w; + u3a_wfree(han_u); + } + else { + // shrink node in place; don't reallocate, we could be low on memory + // + han_u->map_w &= ~(1 << bit_w); + --len_w; + + for ( i_w = inx_w; i_w < len_w; i_w++ ) { + han_u->sot_w[i_w] = han_u->sot_w[i_w + 1]; + } } + return c3y; } - return ret_o; } - +/* _ch_some_del(): delete from node or buck +*/ static c3_o -_ch_some_del(u3h_slot* sot_w, u3_noun key, c3_w lef_w, c3_w rem_w, c3_w* use_w) +_ch_some_del(u3h_slot* sot_w, u3_noun key, c3_w lef_w, c3_w rem_w) { if ( 0 == lef_w ) { - return _ch_buck_del(sot_w, key, use_w); + return _ch_buck_del(sot_w, key); } - //u3h_node* han_u = (u3h_node*)han_v; - //if ( _ch_popcount(han_u)) - return _ch_node_del(sot_w, key, lef_w, rem_w, use_w); -} - + return _ch_node_del(sot_w, key, lef_w, rem_w); +} -/** TODO: this has a bug where the freeing a hamt that this has been called with failes - * most likelly memroy mgmt +/* u3h_del(); delete from hashtable. */ void u3h_del(u3p(u3h_root) har_p, u3_noun key) @@ -391,10 +405,10 @@ u3h_del(u3p(u3h_root) har_p, u3_noun key) c3_w rem_w = CUT_END(mug_w, 25); u3h_slot* sot_w = &(har_u->sot_w[inx_w]); - if ( c3y == u3h_slot_is_null(*sot_w) ) { - return; - } else { - _ch_slot_del(sot_w, key, 25, rem_w, &(har_u->use_w)); + if ( (c3n == u3h_slot_is_null(*sot_w)) + && (c3y == _ch_slot_del(sot_w, key, 25, rem_w)) ) + { + har_u->use_w--; } } From 2a068451aa39f4816c39ab0defc98408ce52d4e2 Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Mon, 29 Jul 2024 16:58:27 -0400 Subject: [PATCH 292/430] add lldbinit.txt --- lldbinit.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 lldbinit.txt diff --git a/lldbinit.txt b/lldbinit.txt new file mode 100644 index 0000000000..da15d87afc --- /dev/null +++ b/lldbinit.txt @@ -0,0 +1,3 @@ +pro hand -p true -s false -n false SIGBUS SIGSEGV +p (void)darwin_register_mach_exception_handler() +b u3m_bail \ No newline at end of file From cbd8898ab056d0c5c51144adc7d3f448b4a466e9 Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Mon, 29 Jul 2024 20:08:35 -0400 Subject: [PATCH 293/430] mesa: fixed crashes during basic operation --- pkg/vere/io/mesa.c | 111 +++++++++++++++++++++++++++++++-------------- 1 file changed, 77 insertions(+), 34 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 29d987ae45..915cf2260a 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -434,6 +434,13 @@ _mesa_encode_path(c3_w len_w, c3_y* buf_y) return pro; } +static void +_mesa_free_name(u3_mesa_name* nam_u) +{ + c3_free(nam_u->pat_c); + c3_free(nam_u); +} + static void _mesa_free_pict(u3_mesa_pict* pic_u) { @@ -901,8 +908,6 @@ _mesa_send_cb(uv_udp_send_t* req_u, c3_i sas_i) static void _mesa_send_buf(u3_mesa* sam_u, u3_lane lan_u, c3_y* buf_y, c3_w len_w) { u3_seal* sel_u = c3_calloc(sizeof(*sel_u)); - // this is wrong, need to calloc & memcpy - // XX calloc & memcpy done before the _mesa_send_buf callsite sel_u->buf_y = buf_y; sel_u->len_w = len_w; sel_u->sam_u = sam_u; @@ -965,8 +970,7 @@ typedef struct _u3_mesa_resend_data { static void _mesa_free_request_data(u3_mesa_request_data* dat_u) { - c3_free(dat_u->nam_u->pat_c); - c3_free(dat_u->nam_u); + _mesa_free_name(dat_u->nam_u); c3_free(dat_u->buf_y); u3z(dat_u->las); // does not free dat_u itself, since it could be in a u3_mesa_resend_data @@ -1002,7 +1006,13 @@ _mesa_send_modal(u3_peer* per_u, c3_y* buf_y, c3_w len_w) per_u->dir_u.sen_d = now_d; } else { - u3l_log("mesa: indirect to %u", per_u->imp_y); + #ifdef MESA_DEBUG + u3_ship gal_u = {0}; + gal_u[0] = per_u->imp_y; + c3_c* gal_c = u3_ship_to_string(gal_u); + u3l_log("mesa: sending to %s", gal_c); + c3_free(gal_c); + #endif // XX if we have lanes in arvo, send it also there? // otherwise after a peer turns indirect because we haven't contacted them, // we never achieve a direct route since we only send @@ -1022,11 +1032,13 @@ _mesa_send_modal(u3_peer* per_u, c3_y* buf_y, c3_w len_w) } } +// RETAIN on dat_u->las static void _mesa_send_request(u3_mesa_request_data* dat_u) { u3_peer* per_u = _mesa_get_peer(dat_u->sam_u, dat_u->her_u); if ( !per_u ) { + u3l_log("mesa: send_bufs()"); _mesa_send_bufs(dat_u->sam_u, NULL, dat_u->buf_y, @@ -1034,9 +1046,9 @@ _mesa_send_request(u3_mesa_request_data* dat_u) u3k(dat_u->las)); } else { + u3l_log("mesa: send_modal()"); _mesa_send_modal(per_u, dat_u->buf_y, dat_u->len_w); } - u3z(dat_u->las); } static void @@ -1298,6 +1310,11 @@ _mesa_send_bufs(u3_mesa* sam_u, u3l_log(" direct send"); per_u->dir_u.sen_d = _get_now_micros(); } + else { + #ifdef MESA_DEBUG + u3l_log("mesa: send indirect"); + #endif + } } } u3z(las); @@ -1542,7 +1559,7 @@ _mesa_resend_timer_cb(uv_timer_t* tim_u) #endif } u3z(pit); - + _mesa_send_request(dat_u); if ( res_u->ret_y ) { @@ -1594,14 +1611,20 @@ _mesa_ef_send(u3_mesa* sam_u, u3_noun las, u3_noun pac) } } else { + // TODO this is too much allocation and copying u3_mesa_name* nam_u = c3_malloc(sizeof(u3_mesa_name)); memcpy(nam_u, &pac_u.pek_u.nam_u, sizeof(u3_mesa_name)); + nam_u->pat_c = c3_malloc(nam_u->pat_s); + memcpy(nam_u->pat_c, pac_u.pek_u.nam_u.pat_c, nam_u->pat_s); u3_mesa_resend_data* res_u = c3_malloc(sizeof(u3_mesa_resend_data)); u3_mesa_request_data* dat_u = &res_u->dat_u; { dat_u->sam_u = sam_u; _get_her(&pac_u, dat_u->her_u); dat_u->nam_u = nam_u; + dat_u->las = u3k(las); + dat_u->buf_y = buf_y; + dat_u->len_w = len_w; } { res_u->ret_y = 4; @@ -2164,31 +2187,49 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) } typedef struct _u3_mesa_lane_cb_data { - u3_ship her_u; - u3_lane lan_u; - u3_peer* per_u; + u3_lane lan_u; + u3_peer* per_u; + u3_mesa_name* nam_u; } u3_mesa_lane_cb_data; +static void +_mesa_free_lane_cb_data(u3_mesa_lane_cb_data* dat_u) +{ + u3l_log("free_lane_cb_data %s", dat_u->nam_u? "non-null" : "null"); + _mesa_free_name(dat_u->nam_u); + c3_free(dat_u); +} + +// TODO should put in cache on success static void _mesa_page_news_cb(u3_ovum* egg_u, u3_ovum_news new_e) { + u3_mesa_lane_cb_data* dat_u = egg_u->ptr_v; + if ( u3_ovum_done != new_e ) { + /* #ifdef MESA_DEBUG - u3l_log("mesa: arvo page event was not a success"); + u3l_log("mesa: arvo page event was not a success, %s", + (u3_ovum_drop == new_e)? "DROP" : + (u3_ovum_work == new_e)? "WORK" : "WACK" + ); #endif + */ return; } - u3_mesa_lane_cb_data* dat_u = egg_u->ptr_v; - u3_peer* per_u = dat_u->per_u; + + u3_assert(dat_u); // TODO remove + u3_assert(dat_u->nam_u); + u3_assert(dat_u->nam_u->pat_s); + u3_assert(dat_u->nam_u->pat_c); #ifdef MESA_DEBUG - c3_c* her_c = u3_ship_to_string(dat_u->her_u); - u3l_log("mesa: %%dear %s", her_c); - _log_lane(&dat_u->lan_u); + c3_c* her_c = u3_ship_to_string(dat_u->nam_u->her_u); + u3l_log("mesa: %%page %s", her_c); c3_free(her_c); #endif - c3_free(dat_u); + _mesa_free_lane_cb_data(dat_u); } static void @@ -2197,7 +2238,7 @@ _mesa_page_bail_cb(u3_ovum* egg_u, u3_ovum_news new_e) #ifdef MESA_DEBUG u3l_log("mesa: arvo page event failed"); #endif - c3_free(egg_u->ptr_v); + _mesa_free_lane_cb_data((u3_mesa_lane_cb_data*)egg_u->ptr_v); } static void @@ -2241,7 +2282,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) c3_s fra_s; c3_d* her_d = nam_u->her_u; - c3_o our_o = __( 0 == memcmp(her_d, sam_u->pir_u->who_d, sizeof(*her_d) * 2) ); + c3_o our_o = u3_ships_equal(her_d, sam_u->pir_u->who_d); // forwarding wrong, need a PIT entry // if ( c3n == our_o ) { @@ -2328,18 +2369,17 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) u3_ovum* ovo = u3_ovum_init(0, c3__ames, wir, cad); ovo = u3_auto_plan(&sam_u->car_u, ovo); - // XX only inject dear if we hear a different, direct lane - if ( pac_u->hed_u.hop_y == 0 && _mesa_lanes_equal(&per_u->dan_u, &lan_u) == c3n) { - // XX should put in cache on success - u3_mesa_lane_cb_data* dat_u = c3_malloc(sizeof(u3_mesa_lane_cb_data)); - { - memcpy(dat_u->her_u, nam_u->her_u, 16); - dat_u->lan_u.pip_w = lan_u.pip_w; - dat_u->lan_u.por_s = lan_u.por_s; - dat_u->per_u = per_u; - } - u3_auto_peer(ovo, dat_u, _mesa_page_news_cb, _mesa_page_bail_cb); + u3_mesa_lane_cb_data* dat_u = c3_calloc(sizeof(u3_mesa_lane_cb_data)); + { + dat_u->per_u = per_u; + dat_u->lan_u.pip_w = lan_u.pip_w; + dat_u->lan_u.por_s = lan_u.por_s; + dat_u->nam_u = c3_calloc(sizeof(u3_mesa_name)); + memcpy(dat_u->nam_u, nam_u, sizeof(u3_mesa_name)); + dat_u->nam_u->pat_c = c3_calloc(nam_u->pat_s); + memcpy(dat_u->nam_u->pat_c, nam_u->pat_c, nam_u->pat_s); } + u3_auto_peer(ovo, dat_u, _mesa_page_news_cb, _mesa_page_bail_cb); _mesa_free_pict(pic_u); u3z(pin); @@ -2495,11 +2535,12 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) static void _mesa_poke_news_cb(u3_ovum* egg_u, u3_ovum_news new_e) { - if ( u3_ovum_done != new_e ) { + /* #ifdef MESA_DEBUG u3l_log("mesa: arvo poke event was not a success"); #endif + */ return; } @@ -2507,8 +2548,8 @@ _mesa_poke_news_cb(u3_ovum* egg_u, u3_ovum_news new_e) u3_peer* per_u = dat_u->per_u; #ifdef MESA_DEBUG - c3_c* her_c = u3_ship_to_string(dat_u->her_u); - u3l_log("mesa: %%dear %s", her_c); + c3_c* her_c = u3_ship_to_string(dat_u->nam_u->her_u); + u3l_log("mesa: %%poke processed from %s", her_c); _log_lane(&dat_u->lan_u); c3_free(her_c); #endif @@ -2600,7 +2641,8 @@ _mesa_hear_poke(u3_mesa_pict* pic_u, u3_lane* lan_u) assert(pac_u->pok_u.dat_u.tot_w); u3_mesa_lane_cb_data* dat_u = c3_malloc(sizeof(u3_mesa_lane_cb_data)); { - memcpy(dat_u->her_u, pac_u->pok_u.pay_u.her_u, 16); + dat_u->nam_u = c3_malloc(sizeof(u3_mesa_name)); + memcpy(dat_u->nam_u, &pac_u->pek_u.nam_u, sizeof(u3_mesa_name)); dat_u->lan_u.pip_w = lan_u->pip_w; dat_u->lan_u.por_s = lan_u->por_s; dat_u->per_u = per_u; @@ -2621,6 +2663,7 @@ _mesa_hear(u3_mesa* sam_u, c3_w len_w, c3_y* hun_y) { + u3l_log("mesa_hear()"); u3_mesa_pict* pic_u; c3_w pre_w; c3_y* cur_y = hun_y; From 6fce2a4d2f302bf816b488fc20e7365fcf0dbd01 Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Mon, 29 Jul 2024 20:18:38 -0400 Subject: [PATCH 294/430] mesa: stop retrying packet after it gets acked --- pkg/vere/io/mesa.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 915cf2260a..6fe1cfa71e 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -2195,7 +2195,6 @@ typedef struct _u3_mesa_lane_cb_data { static void _mesa_free_lane_cb_data(u3_mesa_lane_cb_data* dat_u) { - u3l_log("free_lane_cb_data %s", dat_u->nam_u? "non-null" : "null"); _mesa_free_name(dat_u->nam_u); c3_free(dat_u); } @@ -2218,17 +2217,13 @@ _mesa_page_news_cb(u3_ovum* egg_u, u3_ovum_news new_e) return; } - u3_assert(dat_u); // TODO remove - u3_assert(dat_u->nam_u); - u3_assert(dat_u->nam_u->pat_s); - u3_assert(dat_u->nam_u->pat_c); - #ifdef MESA_DEBUG c3_c* her_c = u3_ship_to_string(dat_u->nam_u->her_u); - u3l_log("mesa: %%page %s", her_c); + u3l_log("mesa: processed %%page from %s", her_c); c3_free(her_c); #endif + _mesa_del_pit(dat_u->per_u->sam_u, dat_u->nam_u); _mesa_free_lane_cb_data(dat_u); } @@ -2663,7 +2658,6 @@ _mesa_hear(u3_mesa* sam_u, c3_w len_w, c3_y* hun_y) { - u3l_log("mesa_hear()"); u3_mesa_pict* pic_u; c3_w pre_w; c3_y* cur_y = hun_y; From 874a884ced9e940ff0faa4a1881a19fc3bf57853 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Tue, 30 Jul 2024 13:19:19 +0200 Subject: [PATCH 295/430] mesa: fix uninitialized peer on %nail --- pkg/vere/io/mesa.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 6fe1cfa71e..599bf5b0b1 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1646,6 +1646,7 @@ c3_o _ames_kick_newt(void* sam_u, u3_noun tag, u3_noun dat); static void _meet_peer(u3_mesa* sam_u, u3_peer* per_u, u3_ship her_u); +static void _init_peer(u3_mesa* sam_u, u3_peer* per_u); static c3_o _mesa_kick(u3_mesa* sam_u, u3_noun tag, u3_noun dat) { @@ -1679,8 +1680,11 @@ static c3_o _mesa_kick(u3_mesa* sam_u, u3_noun tag, u3_noun dat) u3m_p("data", dat); u3_noun who = u3k(u3h(dat)); u3_peer* per_u = _mesa_get_peer_raw(sam_u, who); - c3_o new_o = c3n; + if ( NULL == per_u ) { + per_u = c3_calloc(sizeof(u3_peer)); + _init_peer(sam_u, per_u); + } u3_ship who_u; u3_ship_of_noun(who_u ,who); _meet_peer(sam_u, per_u, who_u); From 3e081bddd0e9ab379c8d7251dfbe5377f5dc5ed1 Mon Sep 17 00:00:00 2001 From: Santeri Hannula Date: Tue, 30 Jul 2024 13:25:32 +0300 Subject: [PATCH 296/430] pier: fix double boot protection on fresh boot --- pkg/vere/pier.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/pkg/vere/pier.c b/pkg/vere/pier.c index 4dab2fbd3c..9c8ac0a4ec 100644 --- a/pkg/vere/pier.c +++ b/pkg/vere/pier.c @@ -585,11 +585,9 @@ _czar_boot_data(c3_c* czar_c, &czar_lyf, &czar_bon, &czar_ack)) && (c3y == u3r_safe_word(czar_glx, czar_glx_w)) && (c3y == u3r_safe_word(czar_ryf, czar_ryf_w)) && - (c3y == u3r_safe_word(czar_lyf, czar_lyf_w)) && - (c3y == u3du(czar_bon)) && - (c3y == u3r_safe_word(u3t(czar_bon), czar_bon_w)) && - (c3y == u3du(czar_ack)) && - (c3y == u3r_safe_word(u3t(czar_ack), czar_ack_w)) ) { + (c3y == u3r_safe_word(czar_lyf, czar_lyf_w)) ) { + if ( c3y == u3du(czar_bon) ) u3r_safe_word(u3t(czar_bon), czar_bon_w); + if ( c3y == u3du(czar_ack) ) u3r_safe_word(u3t(czar_ack), czar_ack_w); ret_o = c3y; } From a3363eafdb8dada91464195516668ce6120e69fb Mon Sep 17 00:00:00 2001 From: Santeri Hannula Date: Tue, 30 Jul 2024 13:45:25 +0300 Subject: [PATCH 297/430] pier: tweak double-boot protection messages --- pkg/vere/pier.c | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/pkg/vere/pier.c b/pkg/vere/pier.c index 9c8ac0a4ec..bd3190c8b1 100644 --- a/pkg/vere/pier.c +++ b/pkg/vere/pier.c @@ -629,27 +629,32 @@ _boot_scry_cb(void* vod_p, u3_noun nun) &czar_glx_w, &czar_ryf_w, &czar_lyf_w, &czar_bon_w, &czar_ack_w) ) { - u3l_log("boot: peer-state unvailable on czar, cannot protect from double boot"); + u3l_log("boot: peer-state unvailable on czar, cannot protect from double-boot"); _pier_work(wok_u); } else { if ( czar_ryf_w == ryf_w ) { c3_w ack_w = cur_w - 1; if ( czar_ack_w == 0xFFFFFFFF ) { // This codepath should never be hit - u3l_log("boot: message-sink-state unvailable on czar, cannot protect from double boot"); + u3l_log("boot: message-sink-state unvailable on czar, cannot protect from double-boot"); _pier_work(wok_u); } else if ( (czar_ack_w == ack_w) || ((nex_w > cur_w) && (czar_ack_w - 1 == ack_w)) ) { _pier_work(wok_u); } else { - u3l_log("boot: failed: czar last ack: %d, ship last ack: %d", - czar_ack_w, ack_w); + u3l_log("boot: failed: double-boot detected, refusing to boot %s\r\n" + "this pier is an old copy, boot the latest pier or breach\r\n" + "read more: https://docs.urbit.org/glossary/double-boot", + who_c); u3_king_bail(); } } else { // Trying to boot old ship after breach - u3l_log("boot: failed: rift in czar peer-state: %d, current rift: %d", - czar_ryf_w, ryf_w); + u3l_log("boot: failed: double-boot detected, refusing to boot %s\r\n" + "this ship has been breached since its initialization, " + "boot the latest pier or breach again\r\n" + "read more: https://docs.urbit.org/glossary/double-boot", + who_c); u3_king_bail(); } } @@ -674,14 +679,17 @@ _boot_scry_cb(void* vod_p, u3_noun nun) c3_free(czar_c); u3_weak kf_ryf = wok_u->pir_u->ryf; if ( kf_ryf == u3_none ) { - u3l_log("boot: keyfile rift unavailable, cannot protect from double boot"); + u3l_log("boot: keyfile rift unavailable, cannot protect from double-boot"); _pier_work(wok_u); } else if ( kf_ryf > czar_ryf_w ) { // Ship has breached, continue boot _pier_work(wok_u); } else { - u3l_log("boot: failed: rift in czar peer state: %d, keyfile rift: %d", - czar_ryf_w, kf_ryf); + u3l_log("boot: failed: double-boot detected, refusing to boot %s\r\n" + "this ship has already been booted elsewere, " + "boot the existing pier or breach\r\n" + "read more: https://docs.urbit.org/glossary/double-boot", + who_c); u3_king_bail(); } } @@ -690,7 +698,7 @@ _boot_scry_cb(void* vod_p, u3_noun nun) * Boot scry endpoint doesn't exists. Most likely old arvo. * Continue boot and hope for the best. */ - u3l_log("boot: %%boot scry endpoint doesn't exist, cannot protect from double boot"); + u3l_log("boot: %%boot scry endpoint doesn't exist, cannot protect from double-boot"); _pier_work(wok_u); } u3z(nun); u3z(who); From 6a8439b89127109b624895ecf976fa5895dbc44f Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Tue, 30 Jul 2024 16:46:38 +0200 Subject: [PATCH 298/430] mesa: fix memory bug --- pkg/vere/io/mesa.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 599bf5b0b1..54b3f48729 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -2071,7 +2071,7 @@ _mesa_veri_scry_cb(void* vod_p, u3_noun nun) if ( !req_u ) { return; } - else if ( c3y == nun ) { + else if ( c3y == nun ) { // XX _mesa_request_next_fragments(ver_u->sam_u, req_u, ver_u->lan_u); } else if ( c3n == nun ) { @@ -2107,7 +2107,7 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) req_u->pic_u->sam_u = sam_u; req_u->pic_u->pac_u.hed_u.typ_y = PACT_PEEK; req_u->pic_u->pac_u.hed_u.pro_y = MESA_VER; - memcpy(&req_u->pic_u->pac_u.pek_u.nam_u, nam_u, sizeof(u3_mesa_name)); + memcpy(&req_u->pic_u->pac_u.pek_u.nam_u, nam_u, sizeof(u3_mesa_name)); // XX req_u->pic_u->pac_u.pek_u.nam_u.aut_o = c3n; req_u->pic_u->pac_u.pek_u.nam_u.nit_o = c3n; req_u->aum_u = dat_u->aum_u; @@ -2390,7 +2390,8 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) if ( c3y == nam_u->nit_o ) { _mesa_req_pact_init(sam_u, pic_u, &lan_u); } - _mesa_free_pict(pic_u); + // _mesa_free_pict(pic_u); // XX leaks packet + // TODO free pin, other things too? return; } From 0c3ed2a5da49123567ecf759072c39ffb48cc5f1 Mon Sep 17 00:00:00 2001 From: midden-fabler Date: Tue, 30 Jul 2024 18:14:52 -0400 Subject: [PATCH 299/430] http: slice in arvo, simplify --- pkg/vere/io/http.c | 228 +++++---------------------------------------- 1 file changed, 25 insertions(+), 203 deletions(-) diff --git a/pkg/vere/io/http.c b/pkg/vere/io/http.c index af0dbf2ee9..a5b0ae99a8 100644 --- a/pkg/vere/io/http.c +++ b/pkg/vere/io/http.c @@ -642,35 +642,6 @@ _http_seq_new(u3_hcon* hon_u, h2o_req_t* rec_u) static void _http_cache_respond(u3_hreq* req_u, u3_noun nun); -static void -_http_scry_respond(u3_hreq* req_u, u3_noun nun); - -/* _content_headers: create content headers for response -*/ -static u3_noun -_content_headers(c3_z beg_z, c3_z end_z, c3_w tot_w) -{ - u3_noun out; - u3_noun byz; - u3_noun rng; - u3_noun len; - - u3_noun lin = u3i_list(u3i_string("bytes "), - u3do("crip", u3do("a-co:co", beg_z)), - c3_s1('-'), - u3do("crip", u3do("a-co:co", end_z)), - c3_s1('/'), - u3do("crip", u3do("a-co:co", tot_w)), - u3_none); - u3_atom dat = u3qc_rap(3, lin); - rng = u3nc(u3i_string("content-range"), dat); - len = u3nc(u3i_string("content-length"), - u3do("crip", u3do("a-co:co", (end_z - beg_z) + 1))); - - out = u3i_list(rng, len, u3_none); - return out; -} - typedef struct _range_request { c3_z beg_z; c3_z end_z; @@ -689,7 +660,7 @@ _parse_range(c3_c* txt_c, c3_w len_w) if ( hep_c ) { cut.beg_z = h2o_strtosize(txt_c, hep_c - txt_c); cut.end_z = h2o_strtosize(hep_c + 1, len_w - ((hep_c + 1) - txt_c)); - // strange -> [SIZE_MAX SIZE_MAX] so we return u3_nul in _slice_octs + // strange -> [SIZE_MAX SIZE_MAX] so we return u3_nul if ( ((SIZE_MAX == cut.beg_z) && (hep_c != txt_c)) || ((SIZE_MAX == cut.end_z) && (len_w - ((hep_c + 1) - txt_c) > 0)) ) { @@ -723,105 +694,6 @@ _get_range(h2o_headers_t req_headers, range_request* rng_req) return c3y; } -typedef struct _content { - c3_z beg_z; - c3_z end_z; - u3_noun dat; // XX free -} content; - -/* _slice_octs: given a valid range, slice a section of octs -*/ -static content -_slice_octs(range_request rng, u3_noun octs) -{ - content out; - out.beg_z = SIZE_MAX; - out.end_z = SIZE_MAX; - out.dat = u3_nul; - - u3_noun len = u3h(octs); - c3_w len_w; - if ( c3n == u3r_safe_word(len, &len_w) ) { - return out; - } - - if ( (SIZE_MAX == rng.beg_z) - && (SIZE_MAX == rng.end_z) ) - { - // [~ ~] - return out; - } - - if ( SIZE_MAX == rng.end_z ) { - // [@ ~] - out.beg_z = rng.beg_z; - out.end_z = len_w - 1; - } - else if ( SIZE_MAX == rng.beg_z ) { - // [~ @] - out.beg_z = len_w - c3_min(rng.end_z, len_w); - out.end_z = len_w - 1; - } - else { - // [@ @] - out.beg_z = rng.beg_z; - out.end_z = c3_min(rng.end_z, len_w - 1); - } - - if ( (out.beg_z < len_w) - && (out.end_z < len_w) - && (out.beg_z <= out.end_z) ) - { - out.dat = u3nc((out.end_z - out.beg_z) + 1, - u3qc_cut(3, out.beg_z, (out.end_z + 1) - out.beg_z, u3t(octs))); - } - return out; -} - -static void -_http_range_respond(u3_hreq* req_u, u3_noun nac, range_request rng_req) -{ - u3_noun octs = u3r_at(127, nac); - if ( u3_none == octs ) { - // 400 - c3_c* msg_c = "bad request"; - h2o_send_error_generic(req_u->rec_u, 400, msg_c, msg_c, 0); - return; - } - content result = _slice_octs(rng_req, octs); - - if ( u3_nul == result.dat ) { - // 416 - c3_c* msg_c = "Requested Range Not Satisfiable"; - u3z(result.dat); - h2o_send_error_generic(req_u->rec_u, 416, msg_c, msg_c, 0); - return; - } - - if ( c3y == u3r_sing(result.dat, octs) ) { - // 200 - u3z(result.dat); - u3_noun len = u3r_at(254, nac); - c3_w len_w; - if ( c3n == u3r_safe_word(len, &len_w) ) { - u3z(nac); - _http_scry_respond(req_u, u3_nul); - return; - } - u3_noun hez = _content_headers(0, (len_w - 1), len_w); - u3_noun res = u3i_edit(nac, 125, u3qb_weld(hez, u3r_at(125, nac))); - _http_cache_respond(req_u, res); - } - else { - // 206 - u3_noun hez = _content_headers(result.beg_z, result.end_z, u3h(octs)); - u3_noun res = u3i_edit(nac, 127, result.dat); - res = u3i_edit(res, 124, 206); - res = u3i_edit(res, 125, u3qb_weld(hez, u3r_at(125, res))); - _http_cache_respond(req_u, res); - } -} - /* _http_scry_cb() */ static void @@ -836,31 +708,18 @@ _http_scry_cb(void* vod_p, u3_noun nun) if ( req_u ) { u3_assert(u3_rsat_peek == req_u->sat_e); req_u->peq_u = 0; - if ( u3_nul == nun ) { - _http_scry_respond(req_u, u3k(nun)); - } - else { - h2o_headers_t req_headers = req_u->rec_u->headers; - range_request rng_req; - c3_o rng_o = _get_range(req_headers, &rng_req); - - if (c3y == rng_o ) { - _http_range_respond(req_u, nun, rng_req); - } - else { - _http_cache_respond(req_u, u3k(nun)); - } - } + _http_cache_respond(req_u, u3k(nun)); } // cache only if peek was not at now, and nun isn't u3_nul - if ( c3n == peq_u->las_o ) { - // XX pair of auth & path for key - // check ~watter-parter's + if ( (c3n == peq_u->las_o) + && (u3_nul != nun) ) + { u3_noun key = u3nc(auth, u3k(peq_u->pax)); u3h_put(htd_u->nax_p, key, nun); u3z(key); } + u3z(peq_u->pax); c3_free(peq_u); } @@ -1029,12 +888,27 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) } else { - u3_noun spur = u3nc(u3i_string("mime"), u3t(bem.pur)); + h2o_headers_t req_headers = req_u->rec_u->headers; + range_request rng_req; + c3_o rng_o = _get_range(req_headers, &rng_req); + + u3_noun spur; + if ( c3n == rng_o ) { + spur = u3nq(u3i_string("range"), c3_s1('0'), u3_blip, u3t(bem.pur)); + } + else { + u3_atom beg = ( SIZE_MAX == rng_req.beg_z) ? + u3_blip : u3dc("scot", c3__ud, u3i_chub(rng_req.beg_z)); + u3_atom end = ( SIZE_MAX == rng_req.end_z) ? + u3_blip : u3dc("scot", c3__ud, u3i_chub(rng_req.end_z)); + + spur = u3nq(u3i_string("range"), beg, end, u3t(bem.pur)); + } + if ( c3y == req_u->peq_u->las_o ) { u3_pier_peek_last(htd_u->car_u.pir_u, gang, c3__ex, bem.des, spur, req_u->peq_u, _http_scry_cb); } - else { u3_noun bam = u3nq(bem.who, bem.des, bem.cas, spur); u3_noun key = u3nc(auth, u3k(bam)); @@ -1045,22 +919,13 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) || (u3_nul == nac) || ((u3_nul == gang) && (c3y == u3r_at(14, nac))) ) { - // cache, then serve subsequent range requests from cache + // maybe cache, then serve subsequent range requests from cache req_u->peq_u->pax = u3k(bam); u3_pier_peek(htd_u->car_u.pir_u, gang, u3nt(0, c3__ex, bam), req_u->peq_u, _http_scry_cb); } else { - h2o_headers_t req_headers = req_u->rec_u->headers; - range_request rng_req; - c3_o rng_o = _get_range(req_headers, &rng_req); - - if ( c3y == rng_o) { - _http_range_respond(req_u, nac, rng_req); - } - else { - _http_cache_respond(req_u, nac); - } + _http_cache_respond(req_u, nac); } } } @@ -1068,49 +933,6 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) } } -static void -_http_scry_respond(u3_hreq* req_u, u3_noun nun) -{ - h2o_req_t* rec_u = req_u->rec_u; - u3_httd* htd_u = req_u->hon_u->htp_u->htd_u; - - if ( u3_nul == nun ) { - u3_weak req = _http_rec_to_httq(rec_u); - if ( u3_none == req ) { - if ( (u3C.wag_w & u3o_verbose) ) { - u3l_log("strange %.*s request", (c3_i)rec_u->method.len, - rec_u->method.base); - } - c3_c* msg_c = "bad request"; - h2o_send_error_generic(rec_u, 400, msg_c, msg_c, 0); - } - else { - h2o_send_error_500(rec_u, "Internal Server Error", "scry failed", 0); - } - } - else if ( u3_none == u3r_at(15, nun) ) { - h2o_send_error_500(rec_u, "Internal Server Error", "scry failed", 0); - } - else { - u3_noun auth, response_header, data; - u3x_qual(u3k(u3t(u3t(nun))), &auth, 0, &response_header, &data); - u3_noun status, headers; - u3x_cell(response_header, &status, &headers); - - // check auth - if ( (c3y == auth) - && (c3n == _http_req_is_auth(&htd_u->fig_u, rec_u)) ) - { - h2o_send_error_403(rec_u, "Unauthorized", "unauthorized", 0); - } - else { - req_u->sat_e = u3_rsat_plan; - _http_start_respond(req_u, u3k(status), u3k(headers), u3k(data), c3y); - } - } - u3z(nun); -} - /* _http_cache_respond(): respond with a simple-payload:http */ static void From f7bf65355ed6d66da69f33a3178597eba12a45e1 Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Tue, 30 Jul 2024 19:14:32 -0400 Subject: [PATCH 300/430] mesa: WIP writing new cache line data structure; also added functions to copy u3_mesa_name's --- pkg/vere/io/mesa.c | 64 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 17 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 54b3f48729..6c7877ca00 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -72,10 +72,6 @@ typedef struct _u3_mesa_stat { // XX #define MESA_HUNK 16384 // 184 -// pending interest sentinels -#define MESA_ITEM 1 // cached item -#define MESA_WAIT 2 // waiting on scry - // routing table sentinels #define MESA_CZAR 1 // pending dns lookup #define MESA_ROUT 2 // have route @@ -195,16 +191,27 @@ typedef struct _u3_pend_req { } u3_pend_req; typedef enum _u3_mesa_ctag { - CACE_WAIT = 1, - CACE_ITEM = 2, + CTAG_WAIT = 1, + CTAG_ITEM = 2, } u3_mesa_ctag; +// jumbo frame cache value +// +typedef struct _u3_mesa_line { + u3_mesa_ctag typ_y; // pending or present? + u3_mesa_name nam_u; // full name for data, ready to serialize + u3_auth_data aum_u; // message authenticator + c3_y* tip_y; // initial Merkle spine, nullable + c3_y* dat_y; // message data (1024 bytes per fragment) + c3_w dat_w; // message data size in bytes + c3_y* haz_y; // hash pairs (64 bytes per fragment) +} u3_mesa_line; + /* * typedef u3_mesa_req u3_noun * [tot=@ waiting=(set @ud) missing=(set @ud) nex=@ud dat=(map @ud @)] */ - typedef struct _u3_cace_enty { u3_mesa_ctag typ_y; union { @@ -434,6 +441,22 @@ _mesa_encode_path(c3_w len_w, c3_y* buf_y) return pro; } +static void +_mesa_copy_name(u3_mesa_name* des_u, u3_mesa_name* src_u) +{ + memcpy(des_u, src_u, sizeof(u3_mesa_name)); + des_u->pat_c = c3_calloc(src_u->pat_s); + memcpy(des_u->pat_c, src_u->pat_c, src_u->pat_s); +} + +static u3_mesa_name* +_mesa_copy_name_alloc(u3_mesa_name* src_u) +{ + u3_mesa_name* des_u = c3_calloc(sizeof(u3_mesa_name)); + _mesa_copy_name(des_u, src_u); + return des_u; +} + static void _mesa_free_name(u3_mesa_name* nam_u) { @@ -457,16 +480,29 @@ _dire_etch_ud(c3_d num_d) return u3i_bytes(26 - dif_w, buf_y); // XX known-non-null } +static u3_mesa_line* +_mesa_new_line(u3_mesa_name* nam_u) +{ + u3_mesa_line* lin_u = c3_calloc(sizeof(u3_mesa_line)); + lin_u->typ_y = CTAG_ITEM; + lin_u->nam_u = *nam_u; + memcpy(lin_u->nam_u.pat_c, nam_u->pat_c, nam_u->pat_s); + + return lin_u; +} + /* _mesa_request_key(): produce key for request hashtable sam_u->req_p from nam_u */ -u3_noun _mesa_request_key(u3_mesa_name* nam_u) +u3_noun +_mesa_request_key(u3_mesa_name* nam_u) { u3_noun pax = _mesa_encode_path(nam_u->pat_s, (c3_y*)nam_u->pat_c); u3_noun res = u3nc(u3i_word(nam_u->rif_w), pax); return res; } -static void _init_gage(u3_gage* gag_u) +static void +_init_gage(u3_gage* gag_u) { gag_u->rto_w = 1000000; gag_u->rtt_w = 1000000; @@ -1612,10 +1648,7 @@ _mesa_ef_send(u3_mesa* sam_u, u3_noun las, u3_noun pac) } else { // TODO this is too much allocation and copying - u3_mesa_name* nam_u = c3_malloc(sizeof(u3_mesa_name)); - memcpy(nam_u, &pac_u.pek_u.nam_u, sizeof(u3_mesa_name)); - nam_u->pat_c = c3_malloc(nam_u->pat_s); - memcpy(nam_u->pat_c, pac_u.pek_u.nam_u.pat_c, nam_u->pat_s); + u3_mesa_name* nam_u = _mesa_copy_name_alloc(&pac_u.pek_u.nam_u); u3_mesa_resend_data* res_u = c3_malloc(sizeof(u3_mesa_resend_data)); u3_mesa_request_data* dat_u = &res_u->dat_u; { @@ -2373,10 +2406,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) dat_u->per_u = per_u; dat_u->lan_u.pip_w = lan_u.pip_w; dat_u->lan_u.por_s = lan_u.por_s; - dat_u->nam_u = c3_calloc(sizeof(u3_mesa_name)); - memcpy(dat_u->nam_u, nam_u, sizeof(u3_mesa_name)); - dat_u->nam_u->pat_c = c3_calloc(nam_u->pat_s); - memcpy(dat_u->nam_u->pat_c, nam_u->pat_c, nam_u->pat_s); + dat_u->nam_u = _mesa_copy_name_alloc(nam_u); } u3_auto_peer(ovo, dat_u, _mesa_page_news_cb, _mesa_page_bail_cb); From 64ecf0cf7e74c4e80bd709617d800b4f26bd2932 Mon Sep 17 00:00:00 2001 From: Tinnus Napbus Date: Wed, 31 Jul 2024 22:26:19 +1200 Subject: [PATCH 301/430] dill: send %born event on start --- pkg/vere/io/term.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/pkg/vere/io/term.c b/pkg/vere/io/term.c index bf9789aabc..b5e75e2569 100644 --- a/pkg/vere/io/term.c +++ b/pkg/vere/io/term.c @@ -1590,7 +1590,19 @@ u3_term_wall(u3_noun wol) static void _term_io_talk(u3_auto* car_u) { + //TODO reevaluate wrt dill sessions + // + u3_noun wir = u3nt(c3__term, '1', u3_nul); + u3_noun cad; + + // send born event + { + cad = u3nc(c3__born, u3_nul); + _term_ovum_plan(car_u, u3k(wir), cad); + } + if ( c3n == u3_Host.ops_u.tem ) { + u3z(wir); u3_utty* uty_u = _term_main(); uv_read_start((uv_stream_t*)&(uty_u->pin_u), @@ -1598,11 +1610,6 @@ _term_io_talk(u3_auto* car_u) _term_read_cb); } - //TODO reevaluate wrt dill sessions - // - u3_noun wir = u3nt(c3__term, '1', u3_nul); - u3_noun cad; - // send terminal dimensions // { From 13e08dbcac9e51f25bff0e347aecaf8386afd27e Mon Sep 17 00:00:00 2001 From: Sigilante Date: Wed, 31 Jul 2024 12:40:42 -0500 Subject: [PATCH 302/430] Post corrected traversal order. --- pkg/noun/jets/i/lagoon.c | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/pkg/noun/jets/i/lagoon.c b/pkg/noun/jets/i/lagoon.c index 01e8e9954a..64447176fb 100644 --- a/pkg/noun/jets/i/lagoon.c +++ b/pkg/noun/jets/i/lagoon.c @@ -516,8 +516,8 @@ case 4: { float16_t sum16[2]; sum16[0] = (float16_t){SB_REAL16_ZERO}; - for (c3_d i = 0; i < len_x; i++) { - sum16[0] = f16_add(sum16[0], ((float16_t*)x_bytes)[i]); + for (c3_d i = len_x; i > 0; i--) { + sum16[0] = f16_add(sum16[0], ((float16_t*)x_bytes)[i-1]); } sum16[1].v = 0x1; r_data = u3i_bytes((2+1)*sizeof(c3_y), (c3_y*)sum16); @@ -526,8 +526,8 @@ case 5: { float32_t sum32[2]; sum32[0] = (float32_t){SB_REAL32_ZERO}; - for (c3_d i = 0; i < len_x; i++) { - sum32[0] = f32_add(sum32[0], ((float32_t*)x_bytes)[i]); + for (c3_d i = len_x; i > 0; i--) { + sum32[0] = f32_add(sum32[0], ((float32_t*)x_bytes)[i-1]); } sum32[1].v = 0x1; r_data = u3i_bytes((4+1)*sizeof(c3_y), (c3_y*)sum32); @@ -536,8 +536,8 @@ case 6: { float64_t sum64[2]; sum64[0] = (float64_t){SB_REAL64_ZERO}; - for (c3_d i = 0; i < len_x; i++) { - sum64[0] = f64_add(sum64[0], ((float64_t*)x_bytes)[i]); + for (c3_d i = len_x; i > 0; i--) { + sum64[0] = f64_add(sum64[0], ((float64_t*)x_bytes)[i-1]); } sum64[1].v = 0x1; r_data = u3i_bytes((8+1)*sizeof(c3_y), (c3_y*)sum64); @@ -546,8 +546,8 @@ case 7: { float128_t sum128[2]; sum128[0] = (float128_t){SB_REAL128L_ZERO, SB_REAL128U_ZERO}; - for (c3_d i = 0; i < len_x; i++) { - f128M_add(&(sum128[0]), &(((float128_t*)x_bytes)[i]), &(sum128[0])); + for (c3_d i = len_x; i > 0; i--) { + f128M_add(&(sum128[0]), &(((float128_t*)x_bytes)[i-1]), &(sum128[0])); } sum128[1] = (float128_t){0x1, 0x0}; r_data = u3i_bytes((16+1)*sizeof(c3_y), (c3_y*)sum128); @@ -1883,8 +1883,9 @@ for (c3_d i = 1; i < n-1; i++) { ((float16_t*)x_bytes16)[i] = f16_add(a16, f16_mul(i32_to_f16(i), interval16)); } - ((float16_t*)x_bytes16)[0] = a16; + // Assign in reverse order so that n=1 case is correctly left-hand bound. ((float16_t*)x_bytes16)[n-1] = b16; + ((float16_t*)x_bytes16)[0] = a16; x_bytes16[n*2] = 0x1; // pin head r_data = u3i_bytes((n*2+1)*sizeof(c3_y), x_bytes16); u3a_free(x_bytes16); @@ -1900,8 +1901,8 @@ for (c3_d i = 1; i < n-1; i++) { ((float32_t*)x_bytes32)[i] = f32_add(a32, f32_mul(i32_to_f32(i), interval32)); } - ((float32_t*)x_bytes32)[0] = a32; ((float32_t*)x_bytes32)[n-1] = b32; + ((float32_t*)x_bytes32)[0] = a32; x_bytes32[n*4] = 0x1; // pin head r_data = u3i_bytes((n*4+1)*sizeof(c3_y), x_bytes32); u3a_free(x_bytes32); @@ -1917,8 +1918,8 @@ for (c3_d i = 1; i < n-1; i++) { ((float64_t*)x_bytes64)[i] = f64_add(a64, f64_mul(i32_to_f64(i), interval64)); } - ((float64_t*)x_bytes64)[0] = a64; ((float64_t*)x_bytes64)[n-1] = b64; + ((float64_t*)x_bytes64)[0] = a64; x_bytes64[n*8] = 0x1; // pin head r_data = u3i_bytes((n*8+1)*sizeof(c3_y), x_bytes64); u3a_free(x_bytes64); @@ -1941,8 +1942,8 @@ f128M_mul(&i128, &interval128, &((float128_t*)x_bytes128)[i]); f128M_add(&a128, &((float128_t*)x_bytes128)[i], &((float128_t*)x_bytes128)[i]); } - ((float128_t*)x_bytes128)[0] = a128; ((float128_t*)x_bytes128)[n-1] = b128; + ((float128_t*)x_bytes128)[0] = a128; x_bytes128[n*16] = 0x1; // pin head r_data = u3i_bytes((n*16+1)*sizeof(c3_y), x_bytes128); u3a_free(x_bytes128); @@ -2460,7 +2461,7 @@ _set_rounding(rnd); u3_noun r_data = _soft_run(u3qi_la_cumsum_i754(x_data, x_shape, x_bloq)); if (r_data == u3_none) { return u3_none; } - return u3nc(u3nq(u3nt(0x1, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); + return u3nc(u3nq(u3nc(0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); default: return u3_none; @@ -3208,7 +3209,9 @@ x_fxp = u3t(u3t(u3t(x_meta))); // 15 rnd = u3h(u3t(u3t(u3t(cor)))); // 30 if ( c3n == u3ud(x_bloq) || - c3n == u3ud(x_kind) + c3n == u3ud(x_kind) || + c3n == u3ud(n) || + (n < 1) // crash on zero size ) { return u3m_bail(c3__exit); From e2351396f48191286d566324894f9b4ed043cd40 Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Wed, 31 Jul 2024 18:21:43 -0400 Subject: [PATCH 303/430] main: add --jumbo-bloq command-line argument to set the jumbo frame bloq size --- pkg/vere/main.c | 14 +++++++++++++- pkg/vere/vere.h | 1 + 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/pkg/vere/main.c b/pkg/vere/main.c index 361f8ad304..22e4dd4bc4 100644 --- a/pkg/vere/main.c +++ b/pkg/vere/main.c @@ -194,6 +194,7 @@ _main_init(void) u3_Host.ops_u.sap_w = 120; /* aka 2 minutes */ u3_Host.ops_u.lut_y = 31; /* aka 2G */ u3_Host.ops_u.lom_y = 31; + u3_Host.ops_u.jum_y = 23; /* aka 1MB */ u3_Host.ops_u.siz_i = #if (defined(U3_CPU_aarch64) && defined(U3_OS_linux)) @@ -269,6 +270,7 @@ _main_getopt(c3_i argc, c3_c** argv) { "import", required_argument, NULL, 'i' }, { "ivory-pill", required_argument, NULL, 'J' }, { "json-trace", no_argument, NULL, 'j' }, + { "jumbo-bloq", required_argument, NULL, c3__bloq }, { "kernel-stage", required_argument, NULL, 'K' }, { "key-file", required_argument, NULL, 'k' }, { "loom", required_argument, NULL, c3__loom }, @@ -365,6 +367,15 @@ _main_getopt(c3_i argc, c3_c** argv) } // special args // + case c3__bloq: { + if (_main_readw(optarg, 30, &arg_w)) { + return c3n; + } else u3_Host.ops_u.jum_y = arg_w; + if ( 13 > u3_Host.ops_u.jum_y ) { + return c3n; + } + break; + } case c3__loom: { if (_main_readw_loom("loom", &u3_Host.ops_u.lom_y)) { return c3n; @@ -844,10 +855,11 @@ u3_ve_usage(c3_i argc, c3_c** argv) "-i, --import FILE Import pier state from jamfile\n", "-J, --ivory-pill PILL Use custom ivory pill\n", "-j, --json-trace Create json trace file in .urb/put/trace\n", + " --jumbo-bloq BLOQ Set Ames jumbo frame bloq size\n", "-K, --kernel-stage STAGE Start at Hoon kernel version stage\n", "-k, --key-file KEYS Private key file (see also -G)\n", "-L, --local Local networking only\n", - " --loom Set loom to binary exponent (31 == 2GB)\n" + " --loom EXPO Set loom to binary exponent (31 == 2GB)\n" "-l, --lite-boot Most-minimal startup\n", "-M, --keep-cache-limit LIMIT Set persistent memo cache max size; 0 means default\n", "-n, --replay-to NUMBER Replay up to event\n", diff --git a/pkg/vere/vere.h b/pkg/vere/vere.h index 77f2a99913..80100905ec 100644 --- a/pkg/vere/vere.h +++ b/pkg/vere/vere.h @@ -321,6 +321,7 @@ c3_o beb; // --behn-allow-blocked c3_z siz_i; // --lmdb-map-size + c3_y jum_y; // jumbo frame size, TODO parser } u3_opts; /* u3_host: entire host. From 0ac53bdeaeda4a8cddb16cbcff14d99c46eb00dc Mon Sep 17 00:00:00 2001 From: midden-fabler Date: Wed, 31 Jul 2024 20:17:29 -0400 Subject: [PATCH 304/430] http: scry & cache chunks, cleanup --- pkg/vere/io/http.c | 109 +++++++++++++++++++++++++++++---------------- 1 file changed, 70 insertions(+), 39 deletions(-) diff --git a/pkg/vere/io/http.c b/pkg/vere/io/http.c index a5b0ae99a8..482fb976d7 100644 --- a/pkg/vere/io/http.c +++ b/pkg/vere/io/http.c @@ -642,53 +642,71 @@ _http_seq_new(u3_hcon* hon_u, h2o_req_t* rec_u) static void _http_cache_respond(u3_hreq* req_u, u3_noun nun); -typedef struct _range_request { +typedef struct _byte_range { c3_z beg_z; c3_z end_z; -} range_request; +} byte_range; -/* _parse_range: get a range from '-' delimited text +/* _chunk_align(): align range to a nearby chunk */ -static range_request +static void +_chunk_align(byte_range* rng_u) +{ + c3_z siz_z = 4194304; // 4MiB + + if ( SIZE_MAX != rng_u->beg_z ) { + c3_z tmp_z = rng_u->beg_z / siz_z; + rng_u->beg_z = tmp_z * siz_z; + rng_u->end_z = ((tmp_z + 1) * siz_z) - 1; + } + else if ( SIZE_MAX != rng_u->end_z ) { + // round to multiple of siz_z + rng_u->end_z = siz_z * ((rng_u->end_z / siz_z) + 1); + } +} + +/* _parse_range(): get a range from '-' delimited text +*/ +static byte_range _parse_range(c3_c* txt_c, c3_w len_w) { c3_c* hep_c = memchr(txt_c, '-', len_w); - range_request cut; - cut.beg_z = SIZE_MAX; - cut.end_z = SIZE_MAX; + byte_range rng_u; + rng_u.beg_z = SIZE_MAX; + rng_u.end_z = SIZE_MAX; if ( hep_c ) { - cut.beg_z = h2o_strtosize(txt_c, hep_c - txt_c); - cut.end_z = h2o_strtosize(hep_c + 1, len_w - ((hep_c + 1) - txt_c)); - // strange -> [SIZE_MAX SIZE_MAX] so we return u3_nul - if ( ((SIZE_MAX == cut.beg_z) && (hep_c != txt_c)) - || ((SIZE_MAX == cut.end_z) && (len_w - ((hep_c + 1) - txt_c) > 0)) ) + rng_u.beg_z = h2o_strtosize(txt_c, hep_c - txt_c); + rng_u.end_z = h2o_strtosize(hep_c + 1, len_w - ((hep_c + 1) - txt_c)); + // strange -> [SIZE_MAX SIZE_MAX] + if ( ((SIZE_MAX == rng_u.beg_z) && (hep_c != txt_c)) + || ((SIZE_MAX == rng_u.end_z) && (len_w - ((hep_c + 1) - txt_c) > 0)) ) { - cut.beg_z = SIZE_MAX; - cut.end_z = SIZE_MAX; + rng_u.beg_z = SIZE_MAX; + rng_u.end_z = SIZE_MAX; } } - return cut; + return rng_u; } static c3_o -_get_range(h2o_headers_t req_headers, range_request* rng_req) +_get_range(h2o_headers_t req_headers, byte_range* rng_u) { - rng_req->beg_z = SIZE_MAX; - rng_req->end_z = SIZE_MAX; + rng_u->beg_z = SIZE_MAX; + rng_u->end_z = SIZE_MAX; - c3_w idx = h2o_find_header(&req_headers, H2O_TOKEN_RANGE, -1); - if (idx == UINT32_MAX) { + c3_w inx_w = h2o_find_header(&req_headers, H2O_TOKEN_RANGE, -1); + if (inx_w == UINT32_MAX) { return c3n; } - if ( (req_headers.entries[idx].value.len >= 6) - && (0 == memcmp("bytes=", req_headers.entries[idx].value.base, 6)) ) + if ( (req_headers.entries[inx_w].value.len >= 6) + && (0 == memcmp("bytes=", req_headers.entries[inx_w].value.base, 6)) ) { - range_request tmp = _parse_range(req_headers.entries[idx].value.base + 6, - req_headers.entries[idx].value.len - 6); - rng_req->beg_z = tmp.beg_z; - rng_req->end_z = tmp.end_z; + byte_range tmp_u = _parse_range(req_headers.entries[inx_w].value.base + 6, + req_headers.entries[inx_w].value.len - 6); + rng_u->beg_z = tmp_u.beg_z; + rng_u->end_z = tmp_u.end_z; } return c3y; @@ -797,9 +815,8 @@ _get_beam(u3_hreq* req_u, c3_c* txt_c, c3_w len_w) nex_c = fas_c; } if ( !nex_c ) { - c3_c* msg_c = "bad beam"; - // h2o_send_error_generic(req_u->rec_u, 400, msg_c, msg_c, 0); - // return; + // XX bad beam + *wer = u3_nul; } else { c3_w dif_w = (c3_p)(nex_c - txt_c); @@ -864,8 +881,6 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) u3_hfig* fig_u = &req_u->hon_u->htp_u->htd_u->fig_u; h2o_req_t* rec_u = req_u->rec_u; - u3_noun our = u3dc("scot", 'p', u3i_chubs(2, htd_u->car_u.pir_u->who_d)); - // set gang to [~ ~] or ~ u3_noun gang; c3_o auth = _http_req_is_auth(fig_u, rec_u); @@ -876,35 +891,51 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) gang = u3_nul; } + u3_noun our = u3dc("scot", 'p', u3i_chubs(2, htd_u->car_u.pir_u->who_d)); + // XX weak beam? beam bem = _get_beam(req_u, bas_c, len_w); - // XX necessary? if ( (u3_nul == bem.pur) || (c3n == u3r_sing(our, bem.who)) ) { - c3_c* msg_c = "bad scry path"; + c3_c* msg_c = "bad request"; h2o_send_error_generic(req_u->rec_u, 400, msg_c, msg_c, 0); return; } else { h2o_headers_t req_headers = req_u->rec_u->headers; - range_request rng_req; - c3_o rng_o = _get_range(req_headers, &rng_req); + byte_range rng_u; + c3_o rng_o = _get_range(req_headers, &rng_u); + // prepare spur for eyre range scry + // u3_noun spur; if ( c3n == rng_o ) { + // full range: '/range/0//foo' spur = u3nq(u3i_string("range"), c3_s1('0'), u3_blip, u3t(bem.pur)); } else { - u3_atom beg = ( SIZE_MAX == rng_req.beg_z) ? - u3_blip : u3dc("scot", c3__ud, u3i_chub(rng_req.beg_z)); - u3_atom end = ( SIZE_MAX == rng_req.end_z) ? - u3_blip : u3dc("scot", c3__ud, u3i_chub(rng_req.end_z)); + if ( (SIZE_MAX == rng_u.beg_z) + && (SIZE_MAX == rng_u.end_z) ) + { + c3_c* msg_c = "Requested Range Not Satisfiable"; + h2o_send_error_generic(req_u->rec_u, 416, msg_c, msg_c, 0); + return; + } + + _chunk_align(&rng_u); + + u3_atom beg = ( SIZE_MAX == rng_u.beg_z) ? + u3_blip : u3dc("scot", c3__ud, u3i_chub(rng_u.beg_z)); + u3_atom end = ( SIZE_MAX == rng_u.end_z) ? + u3_blip : u3dc("scot", c3__ud, u3i_chub(rng_u.end_z)); spur = u3nq(u3i_string("range"), beg, end, u3t(bem.pur)); } + // peek or respond from cache + // if ( c3y == req_u->peq_u->las_o ) { u3_pier_peek_last(htd_u->car_u.pir_u, gang, c3__ex, bem.des, spur, req_u->peq_u, _http_scry_cb); From b07bf086fd2af2693867b7c33efbacfec0bcb6be Mon Sep 17 00:00:00 2001 From: Tinnus Napbus Date: Thu, 1 Aug 2024 17:48:46 +1200 Subject: [PATCH 305/430] dill: revise born event logic --- pkg/vere/io/term.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/pkg/vere/io/term.c b/pkg/vere/io/term.c index b5e75e2569..60fa6a68fa 100644 --- a/pkg/vere/io/term.c +++ b/pkg/vere/io/term.c @@ -1590,26 +1590,26 @@ u3_term_wall(u3_noun wol) static void _term_io_talk(u3_auto* car_u) { + if ( c3n == u3_Host.ops_u.tem ) { + u3_utty* uty_u = _term_main(); + + uv_read_start((uv_stream_t*)&(uty_u->pin_u), + _term_alloc, + _term_read_cb); + } + //TODO reevaluate wrt dill sessions // u3_noun wir = u3nt(c3__term, '1', u3_nul); u3_noun cad; // send born event + // { cad = u3nc(c3__born, u3_nul); _term_ovum_plan(car_u, u3k(wir), cad); } - if ( c3n == u3_Host.ops_u.tem ) { - u3z(wir); - u3_utty* uty_u = _term_main(); - - uv_read_start((uv_stream_t*)&(uty_u->pin_u), - _term_alloc, - _term_read_cb); - } - // send terminal dimensions // { From b68619c61c5fb762ab98e3777f8613984c4ddde5 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Thu, 1 Aug 2024 15:50:21 +0200 Subject: [PATCH 306/430] mesa: fix refcounting error in _mesa_get_pit --- pkg/vere/io/mesa.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 54b3f48729..39c8808632 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1480,7 +1480,10 @@ _name_to_scry(u3_mesa_name* nam_u) static u3_weak _mesa_get_pit(u3_mesa* sam_u, u3_mesa_name* nam_u) { - return u3h_get(sam_u->pit_p, _name_to_scry((nam_u))); + u3_noun pax = _name_to_scry(nam_u); + u3_weak res = u3h_get(sam_u->pit_p, pax); + u3z(pax); + return res; } static void From 9e72c92d0b3ca181ec813adaf036910c6d718104 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Thu, 1 Aug 2024 15:51:13 +0200 Subject: [PATCH 307/430] ames: free jumbo_y --- pkg/vere/io/mesa.c | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 39c8808632..ca51b0335a 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1878,6 +1878,7 @@ _mesa_send_jumbo_pieces(u3_mesa* sam_u, u3_noun pag) u3r_bytes(0, jumbo_w, jumbo_y, pag); u3z(pag); mesa_sift_pact(&tac_u, jumbo_y, jumbo_w); + c3_free(jumbo_y); } c3_w jumbo_pact_w = tac_u.pag_u.dat_u.len_w; c3_y* jumbo_pact_y = tac_u.pag_u.dat_u.fra_y; From 2c4519ef72d5c8cb7c4af926bd43146a74cde502 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Thu, 1 Aug 2024 15:52:19 +0200 Subject: [PATCH 308/430] mesa: allocate lss_builder on the heap --- pkg/vere/io/mesa.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index ca51b0335a..1780112d07 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1888,17 +1888,17 @@ _mesa_send_jumbo_pieces(u3_mesa* sam_u, u3_noun pag) // TODO: this assumes we have the entire message. Should be switched to use // lss_builder_transceive instead. c3_w leaves_w = (jumbo_pact_w + 1023) / 1024; - lss_builder bil_u; - lss_builder_init(&bil_u, leaves_w); + lss_builder* bil_u = c3_calloc(sizeof(lss_builder)); + + lss_builder_init(bil_u, leaves_w); for ( c3_w i = 0; i < leaves_w; i++ ) { c3_y* leaf_y = jumbo_pact_y + (i*1024); c3_w leaf_w = (i < leaves_w - 1) ? 1024 : jumbo_pact_w % 1024; - lss_builder_ingest(&bil_u, leaf_y, leaf_w); + lss_builder_ingest(bil_u, leaf_y, leaf_w); } - lss_hash* proof = lss_builder_finalize(&bil_u); + lss_hash* proof = lss_builder_finalize(bil_u); c3_w proof_len = lss_proof_size(leaves_w); - // send packets u3_mesa_name* nam_u = &tac_u.pag_u.nam_u; u3_mesa_data* dat_u = &tac_u.pag_u.dat_u; @@ -1917,6 +1917,7 @@ _mesa_send_jumbo_pieces(u3_mesa* sam_u, u3_noun pag) dat_u->fra_y = proof_y; _mesa_send_pact(sam_u, u3k(u3t(pin)), NULL, &tac_u); _mesa_del_pit(sam_u, nam_u); + c3_free(proof_y); u3z(pin); } } @@ -1935,7 +1936,7 @@ _mesa_send_jumbo_pieces(u3_mesa* sam_u, u3_noun pag) dat_u->aup_u.len_y = proof_len; memcpy(dat_u->aup_u.has_y, proof, proof_len*sizeof(lss_hash)); } else { - lss_pair* pair = lss_builder_pair(&bil_u, i); + lss_pair* pair = lss_builder_pair(bil_u, i); if ( NULL == pair ) { dat_u->aup_u.len_y = 0; dat_u->aum_u.typ_e = AUTH_NONE; @@ -1953,9 +1954,11 @@ _mesa_send_jumbo_pieces(u3_mesa* sam_u, u3_noun pag) #endif _mesa_send_pact(sam_u, u3k(u3t(pin)), NULL, &tac_u); _mesa_del_pit(sam_u, nam_u); - u3k(pin); + u3z(pin); } } + // mesa_free_pact(&tac_u); // XX ?? + lss_builder_free(bil_u); } static void From c69e12a645243fe20265b824f1be13d93de564cb Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Thu, 1 Aug 2024 15:56:53 +0200 Subject: [PATCH 309/430] mesa: fix deletion of PIT entries on hear_page --- pkg/vere/io/mesa.c | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 1780112d07..527bd67e84 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1504,9 +1504,16 @@ _mesa_put_pit(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_noun val) static void _mesa_del_pit(u3_mesa* sam_u, u3_mesa_name* nam_u) { + u3_weak pin = _mesa_get_pit(sam_u, nam_u); u3_noun pax = _name_to_scry(nam_u); - u3h_del(sam_u->pit_p, pax); - u3z(pax); + // u3m_p("_mesa_del_pit pax", pax); + if ( pin != u3_none ) { + u3h_del(sam_u->pit_p, pax); + } else { + u3l_log("deleting non existent key in PIT"); + } + + u3z(pin); u3z(pax); } static void @@ -2234,7 +2241,8 @@ _mesa_page_news_cb(u3_ovum* egg_u, u3_ovum_news new_e) c3_free(her_c); #endif - _mesa_del_pit(dat_u->per_u->sam_u, dat_u->nam_u); + // XX do early delete instead, to avoid injecting retries + // _mesa_del_pit(dat_u->per_u->sam_u, dat_u->nam_u); _mesa_free_lane_cb_data(dat_u); } @@ -2385,6 +2393,10 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) dat_u->nam_u->pat_c = c3_calloc(nam_u->pat_s); memcpy(dat_u->nam_u->pat_c, nam_u->pat_c, nam_u->pat_s); } + // early deletion, to avoid injecting retries, + // (in case of failure, the retry timer will add it again to the PIT) + // + _mesa_del_pit(dat_u->per_u->sam_u, dat_u->nam_u); u3_auto_peer(ovo, dat_u, _mesa_page_news_cb, _mesa_page_bail_cb); _mesa_free_pict(pic_u); @@ -2398,7 +2410,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) _mesa_req_pact_init(sam_u, pic_u, &lan_u); } // _mesa_free_pict(pic_u); // XX leaks packet - + _mesa_del_pit(sam_u, nam_u); // TODO free pin, other things too? return; } @@ -2418,9 +2430,19 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) lon_u); // TODO: check return value before continuing? + _mesa_del_pit(sam_u, nam_u); + c3_o done_with_jumbo_frame = __(req_u->len_w == req_u->tot_w); // TODO: fix for non-message-sized jumbo frames if ( c3y == done_with_jumbo_frame ) { u3_noun cad; + + u3l_log(" received last packet, tot_w: %u", req_u->tot_w); + c3_d now_d = _get_now_micros(); + u3l_log("%u kilobytes took %f ms", req_u->tot_w, (now_d - sam_u->tim_d)/1000.0); + + + _mesa_del_request(sam_u, &pac_u->pag_u.nam_u, pac_u->pag_u.dat_u.tot_w); + { // construct jumbo frame pac_u->pag_u.nam_u.boq_y = 31; // TODO: use actual jumbo bloq @@ -2439,17 +2461,9 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) c3_free(buf_y); } - _mesa_del_request(sam_u, &pac_u->pag_u.nam_u); - u3_auto_plan(&sam_u->car_u, u3_ovum_init(0, c3__ames, u3nc(c3__ames, u3_nul), cad)); - - u3l_log(" received last packet, tot_w: %u", req_u->tot_w); - c3_d now_d = _get_now_micros(); - u3l_log("%u kilobytes took %f ms", req_u->tot_w, (now_d - sam_u->tim_d)/1000.0); - } - - if ( req_u->len_w < req_u->tot_w ) { + } else if ( req_u->len_w < req_u->tot_w ) { _mesa_request_next_fragments(sam_u, req_u, lan_u); } From 95b2e285bde364a3e911f4aa2c1f1f2e48775d77 Mon Sep 17 00:00:00 2001 From: midden-fabler Date: Thu, 1 Aug 2024 13:47:11 -0400 Subject: [PATCH 310/430] http: weak spur, refs, cleanup --- pkg/vere/io/http.c | 205 +++++++++++++++++++++++++++------------------ 1 file changed, 125 insertions(+), 80 deletions(-) diff --git a/pkg/vere/io/http.c b/pkg/vere/io/http.c index 482fb976d7..5bf8fcc169 100644 --- a/pkg/vere/io/http.c +++ b/pkg/vere/io/http.c @@ -642,6 +642,9 @@ _http_seq_new(u3_hcon* hon_u, h2o_req_t* rec_u) static void _http_cache_respond(u3_hreq* req_u, u3_noun nun); +static void +_http_scry_respond(u3_hreq* req_u, u3_noun nun); + typedef struct _byte_range { c3_z beg_z; c3_z end_z; @@ -655,12 +658,11 @@ _chunk_align(byte_range* rng_u) c3_z siz_z = 4194304; // 4MiB if ( SIZE_MAX != rng_u->beg_z ) { - c3_z tmp_z = rng_u->beg_z / siz_z; - rng_u->beg_z = tmp_z * siz_z; - rng_u->end_z = ((tmp_z + 1) * siz_z) - 1; + rng_u->beg_z = (rng_u->beg_z / siz_z) * siz_z; + rng_u->end_z = (rng_u->beg_z + siz_z) - 1; } else if ( SIZE_MAX != rng_u->end_z ) { - // round to multiple of siz_z + // round up to multiple of siz_z rng_u->end_z = siz_z * ((rng_u->end_z / siz_z) + 1); } } @@ -696,7 +698,7 @@ _get_range(h2o_headers_t req_headers, byte_range* rng_u) rng_u->end_z = SIZE_MAX; c3_w inx_w = h2o_find_header(&req_headers, H2O_TOKEN_RANGE, -1); - if (inx_w == UINT32_MAX) { + if ( UINT32_MAX == inx_w) { return c3n; } @@ -726,7 +728,7 @@ _http_scry_cb(void* vod_p, u3_noun nun) if ( req_u ) { u3_assert(u3_rsat_peek == req_u->sat_e); req_u->peq_u = 0; - _http_cache_respond(req_u, u3k(nun)); + _http_scry_respond(req_u, u3k(nun)); } // cache only if peek was not at now, and nun isn't u3_nul @@ -737,6 +739,9 @@ _http_scry_cb(void* vod_p, u3_noun nun) u3h_put(htd_u->nax_p, key, nun); u3z(key); } + else { + u3z(nun); + } u3z(peq_u->pax); c3_free(peq_u); @@ -746,18 +751,25 @@ typedef struct _beam { u3_noun who; u3_noun des; u3_noun cas; - u3_noun pur; + u3_weak pur; } beam; +static void +_free_beam(beam* bem) +{ + u3z(bem->who); + u3z(bem->des); + u3z(bem->cas); + u3z(bem->pur); +} + /* _get_beam: path to beam */ static beam _get_beam(u3_hreq* req_u, c3_c* txt_c, c3_w len_w) { beam bem; - u3_http* htp_u = req_u->hon_u->htp_u; - u3_httd* htd_u = htp_u->htd_u; - u3_noun our = u3dc("scot", 'p', u3i_chubs(2, htd_u->car_u.pir_u->who_d)); + // get beak from path // for ( c3_w i_w = 0; i_w < 3; ++i_w ) { @@ -789,10 +801,12 @@ _get_beam(u3_hreq* req_u, c3_c* txt_c, c3_w len_w) // '=' if ( (len_w > 0) && ('=' == txt_c[0]) ) { if ( 0 == i_w ) { - *wer = our; + u3_http* htp_u = req_u->hon_u->htp_u; + u3_httd* htd_u = htp_u->htd_u; + *wer = u3dc("scot", 'p', u3i_chubs(2, htd_u->car_u.pir_u->who_d)); } else if ( 1 == i_w ) { - *wer = u3i_string("base"); + *wer = c3__base; } else { req_u->peq_u->las_o = c3y; @@ -805,15 +819,14 @@ _get_beam(u3_hreq* req_u, c3_c* txt_c, c3_w len_w) c3_c* nex_c; c3_c* tis_c = memchr(txt_c, '=', len_w); c3_c* fas_c = memchr(txt_c, '/', len_w); + if ( tis_c && fas_c ) { nex_c = c3_min(tis_c, fas_c); } - else if ( tis_c ) { - nex_c = tis_c; - } else { - nex_c = fas_c; + nex_c = ( tis_c ) ? tis_c : fas_c; } + if ( !nex_c ) { // XX bad beam *wer = u3_nul; @@ -827,7 +840,9 @@ _get_beam(u3_hreq* req_u, c3_c* txt_c, c3_w len_w) } } - bem.pur = u3dc("rush", u3i_bytes(len_w, (const c3_y*)txt_c), u3v_wish("stap")); + u3_noun tmp = u3dc("rush", u3i_bytes(len_w, (const c3_y*)txt_c), u3v_wish("stap")); + bem.pur = ( u3_nul == tmp ) ? u3_none : u3k(u3t(tmp)); + u3z(tmp); return bem; } @@ -843,13 +858,17 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) { u3_http* htp_u = req_u->hon_u->htp_u; u3_httd* htd_u = htp_u->htd_u; - u3_noun wir = _http_req_to_duct(req_u); - u3_noun cad; c3_c* bas_c = req_u->rec_u->input.path.base; c3_w len_w = req_u->rec_u->input.path.len; + // check if base url starts with '/_~_/' + if ( (len_w < 6) + || (0 != memcmp("/_~_/", bas_c, 5)) ) { + // no: inject to arvo + u3_noun wir = _http_req_to_duct(req_u); + u3_noun cad; u3_noun adr = u3nc(c3__ipv4, u3i_words(1, &req_u->hon_u->ipf_w)); // XX loopback automatically secure too? // @@ -858,13 +877,6 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) cad = ( c3y == req_u->hon_u->htp_u->lop ) ? u3nc(u3i_string("request-local"), dat) : u3nc(u3i_string("request"), dat); - } - - // check if base url starts with '/_~_/' - if ( (len_w < 6) - || (0 != memcmp("/_~_/", bas_c, 5)) ) - { - // no: inject to arvo u3_auto_plan(&htd_u->car_u, u3_ovum_init(0, c3__e, wir, cad)); } else { @@ -891,75 +903,65 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) gang = u3_nul; } - u3_noun our = u3dc("scot", 'p', u3i_chubs(2, htd_u->car_u.pir_u->who_d)); - // XX weak beam? beam bem = _get_beam(req_u, bas_c, len_w); - - if ( (u3_nul == bem.pur) - || (c3n == u3r_sing(our, bem.who)) ) - { + if ( u3_none == bem.pur ) { c3_c* msg_c = "bad request"; h2o_send_error_generic(req_u->rec_u, 400, msg_c, msg_c, 0); + u3z(gang); + _free_beam(&bem); return; } - else { - h2o_headers_t req_headers = req_u->rec_u->headers; - byte_range rng_u; - c3_o rng_o = _get_range(req_headers, &rng_u); + h2o_headers_t req_headers = req_u->rec_u->headers; + byte_range rng_u; + c3_o rng_o = _get_range(req_headers, &rng_u); - // prepare spur for eyre range scry - // - u3_noun spur; - if ( c3n == rng_o ) { - // full range: '/range/0//foo' - spur = u3nq(u3i_string("range"), c3_s1('0'), u3_blip, u3t(bem.pur)); - } - else { - if ( (SIZE_MAX == rng_u.beg_z) - && (SIZE_MAX == rng_u.end_z) ) - { - c3_c* msg_c = "Requested Range Not Satisfiable"; - h2o_send_error_generic(req_u->rec_u, 416, msg_c, msg_c, 0); - return; - } + // prepare spur for eyre range scry + // + u3_noun spur; + if ( c3n == rng_o ) { + // full range: '/range/0//foo' + spur = u3nq(u3i_string("range"), c3_s1('0'), u3_blip, u3k(bem.pur)); + } + else { + _chunk_align(&rng_u); - _chunk_align(&rng_u); + u3_atom beg = ( SIZE_MAX == rng_u.beg_z) ? + u3_blip : u3dc("scot", c3__ud, u3i_chub(rng_u.beg_z)); + u3_atom end = ( SIZE_MAX == rng_u.end_z) ? + u3_blip : u3dc("scot", c3__ud, u3i_chub(rng_u.end_z)); - u3_atom beg = ( SIZE_MAX == rng_u.beg_z) ? - u3_blip : u3dc("scot", c3__ud, u3i_chub(rng_u.beg_z)); - u3_atom end = ( SIZE_MAX == rng_u.end_z) ? - u3_blip : u3dc("scot", c3__ud, u3i_chub(rng_u.end_z)); + spur = u3nq(u3i_string("range"), beg, end, u3k(bem.pur)); + } - spur = u3nq(u3i_string("range"), beg, end, u3t(bem.pur)); - } + // peek or respond from cache + // + if ( c3y == req_u->peq_u->las_o ) { + u3_pier_peek_last(htd_u->car_u.pir_u, gang, c3__ex, + u3k(bem.des), spur, req_u->peq_u, _http_scry_cb); + } + else { + u3_noun bam = u3nq(u3k(bem.who), u3k(bem.des), u3k(bem.cas), spur); + u3_noun key = u3nc(auth, u3k(bam)); + u3_weak nac = u3h_get(htd_u->nax_p, key); + u3z(key); - // peek or respond from cache - // - if ( c3y == req_u->peq_u->las_o ) { - u3_pier_peek_last(htd_u->car_u.pir_u, gang, c3__ex, - bem.des, spur, req_u->peq_u, _http_scry_cb); + if ( (u3_none == nac) + || ((u3_nul == gang) && (c3y == u3r_at(14, nac))) ) + { + // maybe cache, then serve subsequent range requests from cache + req_u->peq_u->pax = u3k(bam); + u3_pier_peek(htd_u->car_u.pir_u, gang, u3nt(0, c3__ex, bam), + req_u->peq_u, _http_scry_cb); + u3z(nac); } else { - u3_noun bam = u3nq(bem.who, bem.des, bem.cas, spur); - u3_noun key = u3nc(auth, u3k(bam)); - u3_weak nac = u3h_get(htd_u->nax_p, key); - u3z(key); - - if ( (u3_none == nac) - || (u3_nul == nac) - || ((u3_nul == gang) && (c3y == u3r_at(14, nac))) ) - { - // maybe cache, then serve subsequent range requests from cache - req_u->peq_u->pax = u3k(bam); - u3_pier_peek(htd_u->car_u.pir_u, gang, u3nt(0, c3__ex, bam), - req_u->peq_u, _http_scry_cb); - } - else { - _http_cache_respond(req_u, nac); - } + _http_scry_respond(req_u, nac); + u3z(bam); + u3z(gang); } } + _free_beam(&bem); } } } @@ -1010,6 +1012,49 @@ _http_cache_respond(u3_hreq* req_u, u3_noun nun) u3z(nun); } +static void +_http_scry_respond(u3_hreq* req_u, u3_noun nun) +{ + h2o_req_t* rec_u = req_u->rec_u; + u3_httd* htd_u = req_u->hon_u->htp_u->htd_u; + + if ( u3_nul == nun ) { + u3_weak req = _http_rec_to_httq(rec_u); + if ( u3_none == req ) { + if ( (u3C.wag_w & u3o_verbose) ) { + u3l_log("strange %.*s request", (c3_i)rec_u->method.len, + rec_u->method.base); + } + c3_c* msg_c = "bad request"; + h2o_send_error_generic(rec_u, 400, msg_c, msg_c, 0); + } + else { + h2o_send_error_500(rec_u, "Internal Server Error", "scry failed", 0); + } + } + else if ( u3_none == u3r_at(7, nun) ) { + h2o_send_error_500(rec_u, "Internal Server Error", "scry failed", 0); + } + else { + u3_noun auth, response_header, data; + u3x_qual(u3k(u3t(u3t(nun))), &auth, 0, &response_header, &data); + u3_noun status, headers; + u3x_cell(response_header, &status, &headers); + + // check auth + if ( (c3y == auth) + && (c3n == _http_req_is_auth(&htd_u->fig_u, rec_u)) ) + { + h2o_send_error_403(rec_u, "Unauthorized", "unauthorized", 0); + } + else { + req_u->sat_e = u3_rsat_plan; + _http_start_respond(req_u, u3k(status), u3k(headers), u3k(data), c3y); + } + } + u3z(nun); +} + /* _http_cache_scry_cb(): insert scry result into noun cache */ static void From 2fd7030d0da8d7d9f4f0338c02d3c0a17483f3bb Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Thu, 1 Aug 2024 16:06:41 -0400 Subject: [PATCH 311/430] mesa: new cache line; compiles, untested --- pkg/vere/io/mesa.c | 215 +++++++++++++++++++++++++-------------------- 1 file changed, 122 insertions(+), 93 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 6c7877ca00..95386577f4 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -69,9 +69,6 @@ typedef struct _u3_mesa_stat { #define IN_FLIGHT 10 -// XX -#define MESA_HUNK 16384 // 184 - // routing table sentinels #define MESA_CZAR 1 // pending dns lookup #define MESA_ROUT 2 // have route @@ -86,9 +83,9 @@ typedef struct _u3_mesa_stat { struct _u3_mesa_pact; typedef struct _u3_pact_stat { + c3_y tie_y; // tries c3_d sen_d; // last sent c3_y sip_y; // skips - c3_y tie_y; // tries } u3_pact_stat; struct _u3_mesa; @@ -200,11 +197,13 @@ typedef enum _u3_mesa_ctag { typedef struct _u3_mesa_line { u3_mesa_ctag typ_y; // pending or present? u3_mesa_name nam_u; // full name for data, ready to serialize - u3_auth_data aum_u; // message authenticator + u3_auth_data aum_u; // message authenticator, none if not initial frame + c3_w tot_w; // number of fragments in message + c3_w dat_w; // size in bytes of dat_y + c3_w len_w; // total allocated size, in bytes c3_y* tip_y; // initial Merkle spine, nullable - c3_y* dat_y; // message data (1024 bytes per fragment) - c3_w dat_w; // message data size in bytes - c3_y* haz_y; // hash pairs (64 bytes per fragment) + c3_y* dat_y; // fragment data (1024 bytes per fragment) + c3_y* haz_y; // hash pairs (64 bytes per fragment) } u3_mesa_line; /* @@ -1856,29 +1855,23 @@ _name_to_jumbo_scry(u3_mesa_name* nam_u) /* * RETAIN */ -static u3_weak +static u3_mesa_line* _mesa_get_jumbo_cache(u3_mesa* sam_u, u3_mesa_name* nam_u) { u3_noun pax = _name_to_jumbo_scry(nam_u); u3_weak res = u3h_get(sam_u->pac_p, pax); #ifdef MESA_DEBUG - if ( u3_none == res ) { - u3m_p("miss", pax); - } else { - u3_noun kev = u3nc(u3k(pax), u3k(res)); - u3m_p("hit", kev); - u3z(kev); - } - u3z(pax); + u3m_p((u3_none == res)? "mesa: cache miss" : "mesa: cache hit ", pax); #endif - return res; + u3z(pax); + return ( u3_none == res ) ? NULL : u3a_into(res); } static void -_mesa_put_jumbo_cache(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_noun val) +_mesa_put_jumbo_cache(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_mesa_line* lin_u) { u3_noun pax = _name_to_jumbo_scry(nam_u); - u3h_put(sam_u->pac_p, pax, u3k(val)); + u3h_put(sam_u->pac_p, pax, u3a_outa(lin_u)); u3z(pax); // TODO: fix refcount } @@ -1895,77 +1888,75 @@ _mesa_send_pact(u3_mesa* sam_u, } static void -_mesa_send_jumbo_pieces(u3_mesa* sam_u, u3_noun pag) +_mesa_send_jumbo_pieces(u3_mesa* sam_u, u3_mesa_line* lin_u) { #ifdef MESA_DEBUG u3l_log("mesa: send_jumbo_pieces()"); #endif - // parse packet - u3_mesa_pact tac_u = {0}; + + u3_mesa_pact pac_u = {0}; + + u3_mesa_head* hed_u = &pac_u.hed_u; { - c3_w jumbo_w = u3r_met(3, pag); - c3_y* jumbo_y = c3_calloc(jumbo_w); - u3r_bytes(0, jumbo_w, jumbo_y, pag); - u3z(pag); - mesa_sift_pact(&tac_u, jumbo_y, jumbo_w); + hed_u->nex_y = HOP_NONE; + hed_u->pro_y = 1; + hed_u->typ_y = PACT_PAGE; + hed_u->hop_y = 0; + // mug_w varies by fragment } - c3_w jumbo_pact_w = tac_u.pag_u.dat_u.len_w; - c3_y* jumbo_pact_y = tac_u.pag_u.dat_u.fra_y; - // compute LSS data - // - // TODO: this assumes we have the entire message. Should be switched to use - // lss_builder_transceive instead. - c3_w leaves_w = (jumbo_pact_w + 1023) / 1024; - lss_builder bil_u; - lss_builder_init(&bil_u, leaves_w); - for ( c3_w i = 0; i < leaves_w; i++ ) { - c3_y* leaf_y = jumbo_pact_y + (i*1024); - c3_w leaf_w = (i < leaves_w - 1) ? 1024 : jumbo_pact_w % 1024; - lss_builder_ingest(&bil_u, leaf_y, leaf_w); - } - lss_hash* proof = lss_builder_finalize(&bil_u); - c3_w proof_len = lss_proof_size(leaves_w); - - - // send packets - u3_mesa_name* nam_u = &tac_u.pag_u.nam_u; - u3_mesa_data* dat_u = &tac_u.pag_u.dat_u; - nam_u->boq_y = 13; - dat_u->tot_w = leaves_w; - - if ( c3y == nam_u->nit_o && leaves_w > 4) { + u3_mesa_name* nam_u = &pac_u.pag_u.nam_u; + { + _mesa_copy_name(nam_u, &lin_u->nam_u); + nam_u->boq_y = 13; + nam_u->fra_w *= (1 << u3_Host.ops_u.jum_y); + } + + u3_mesa_data* dat_u = &pac_u.pag_u.dat_u; + { + dat_u->tot_w = lin_u->tot_w; + dat_u->aum_u = lin_u->aum_u; + // aup_u, len_w, and fra_y vary by fragment + } + c3_w pro_w = lss_proof_size(lin_u->tot_w); + + if ( c3y == nam_u->nit_o && lin_u->tot_w > 4) { u3_weak pin = _mesa_get_pit(sam_u, nam_u); if ( u3_none != pin ) { - #ifdef MESA_DEBUG - u3l_log(" sending proof packet"); - #endif - dat_u->len_w = proof_len*sizeof(lss_hash); - c3_y* proof_y = c3_malloc(dat_u->len_w); - memcpy(proof_y, proof, dat_u->len_w); - dat_u->fra_y = proof_y; - _mesa_send_pact(sam_u, u3k(u3t(pin)), NULL, &tac_u); - _mesa_del_pit(sam_u, nam_u); - u3z(pin); + #ifdef MESA_DEBUG + u3l_log(" sending proof packet"); + #endif + dat_u->len_w = pro_w * sizeof(lss_hash); + c3_y* pro_y = c3_malloc(dat_u->len_w); + memcpy(pro_y, lin_u->tip_y, dat_u->len_w); + dat_u->fra_y = pro_y; + _mesa_send_pact(sam_u, u3k(u3t(pin)), NULL, &pac_u); + _mesa_del_pit(sam_u, nam_u); + u3z(pin); } } // send leaf packets - for (c3_w i = 0; i < leaves_w; i++) { - nam_u->nit_o = __((i == 0) && (leaves_w <= 4)); - nam_u->fra_w = i; - dat_u->fra_y = jumbo_pact_y + (i*1024); - dat_u->len_w = c3_min(jumbo_pact_w - (i*1024), 1024); + c3_w lev_w = lin_u->tot_w - nam_u->fra_w; + c3_w fir_w = nam_u->fra_w; + c3_y* pro_y = lin_u->tip_y; + for (c3_w i_w = 0; i_w < lev_w; i_w++) { + c3_w fra_w = i_w + fir_w; + nam_u->nit_o = __((fra_w == 0) && (lev_w <= 4)); + nam_u->fra_w = fra_w; + c3_w cur_w = i_w * 1024; + dat_u->fra_y = lin_u->dat_y + cur_w; + dat_u->len_w = c3_min(lin_u->dat_w - cur_w, 1024); if ( c3y == nam_u->nit_o ) { // inline proof; omit first leaf - proof_len--; - proof++; - dat_u->aup_u.len_y = proof_len; - memcpy(dat_u->aup_u.has_y, proof, proof_len*sizeof(lss_hash)); + pro_w--; + pro_y++; + dat_u->aup_u.len_y = pro_w; + memcpy(dat_u->aup_u.has_y, pro_y, pro_w * sizeof(lss_hash)); } else { - lss_pair* pair = lss_builder_pair(&bil_u, i); - if ( NULL == pair ) { + lss_pair* pair = ((lss_pair*)lin_u->haz_y) + i_w; + if ( 0 == memcmp(pair, &(lss_pair){0}, sizeof(lss_pair)) ) { dat_u->aup_u.len_y = 0; dat_u->aum_u.typ_e = AUTH_NONE; } else { @@ -1980,7 +1971,7 @@ _mesa_send_jumbo_pieces(u3_mesa* sam_u, u3_noun pag) #ifdef MESA_DEBUG u3l_log(" sending leaf packet, fra_w: %u", nam_u->fra_w); #endif - _mesa_send_pact(sam_u, u3k(u3t(pin)), NULL, &tac_u); + _mesa_send_pact(sam_u, u3k(u3t(pin)), NULL, &pac_u); _mesa_del_pit(sam_u, nam_u); u3k(pin); } @@ -2004,13 +1995,52 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun res) log_pact(pac_u); return; } + u3_noun pac, pas; + if ( c3n == u3r_cell(pag, &pac, &pas) || + c3n == u3a_is_pug(pac) ) { + u3l_log("mesa: jumbo frame misshapen"); + log_pact(pac_u); + u3z(res); + return; + } + #ifdef MESA_DEBUG u3l_log("mesa: scry_jumbo_cb()"); log_pact(pac_u); #endif - _mesa_put_jumbo_cache(sam_u, nam_u, u3nc(MESA_ITEM, u3k(pag))); - _mesa_send_jumbo_pieces(sam_u, u3k(pag)); - u3z(pag); + + u3_mesa_line* lin_u; + { + u3a_atom* pat_u = u3a_to_ptr(pac); + u3_mesa_pact jum_u; + mesa_sift_pact(&jum_u, (c3_y*)pat_u->buf_w, pat_u->len_w); + u3_mesa_data* dat_u = &jum_u.pag_u.dat_u; + + c3_w tip_w = // bytes in Merkle spine + (c3n == jum_u.pag_u.nam_u.nit_o)? 0 : lss_proof_size(dat_u->tot_w); + c3_w dat_w = dat_u->len_w; // bytes in fragment data in this jumbo frame + c3_w haz_w = (lin_u->len_w + 1023) / 1024 * 2 * 32; // bytes in hash pairs + c3_w len_w = tip_w + dat_w + haz_w; + + lin_u = u3a_malloc(sizeof(u3_mesa_line)); + lin_u->typ_y = CTAG_ITEM; + _mesa_copy_name(&lin_u->nam_u, &jum_u.pek_u.nam_u); + lin_u->aum_u = dat_u->aum_u; + lin_u->tot_w = dat_u->tot_w; + lin_u->dat_w = dat_w; + lin_u->len_w = len_w; + lin_u->tip_y = c3_malloc(len_w); // note: off-loom + lin_u->dat_y = lin_u->tip_y + tip_w; + lin_u->haz_y = lin_u->dat_y + haz_w; + memcpy(lin_u->tip_y, dat_u->fra_y, dat_u->len_w); + u3r_bytes(0, haz_w, lin_u->haz_y, pas); + mesa_free_pact(&jum_u); + } + + _mesa_put_jumbo_cache(sam_u, nam_u, lin_u); + _mesa_send_jumbo_pieces(sam_u, lin_u); + u3z(res); + return; } static void @@ -2146,7 +2176,7 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) req_u->aum_u = dat_u->aum_u; c3_w siz_w = 1 << (pac_u->pag_u.nam_u.boq_y - 3); - u3_assert( siz_w == 1024 ); // boq_y == 13 + u3_assert( 1024 == siz_w ); // boq_y == 13 req_u->gag_u = gag_u; req_u->dat_y = c3_calloc(siz_w * dat_u->tot_w); req_u->wat_u = c3_calloc(sizeof(u3_pact_stat) * dat_u->tot_w + 2 ); @@ -2441,14 +2471,15 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) lon_u); // TODO: check return value before continuing? - c3_o done_with_jumbo_frame = __(req_u->len_w == req_u->tot_w); // TODO: fix for non-message-sized jumbo frames + c3_y boq_y = u3_Host.ops_u.jum_y; + c3_o done_with_jumbo_frame = __(0 == req_u->len_w % boq_y); if ( c3y == done_with_jumbo_frame ) { u3_noun cad; { // construct jumbo frame - pac_u->pag_u.nam_u.boq_y = 31; // TODO: use actual jumbo bloq - pac_u->pag_u.dat_u.tot_w = 1; - pac_u->pag_u.nam_u.fra_w = 0; + pac_u->pag_u.nam_u.boq_y = boq_y; + pac_u->pag_u.dat_u.tot_w = req_u->tot_w; + pac_u->pag_u.nam_u.fra_w = (req_u->len_w >> boq_y); c3_w jumbo_len_w = (1024 * (req_u->tot_w - 1)) + pac_u->pag_u.dat_u.len_w; pac_u->pag_u.dat_u.len_w = jumbo_len_w; pac_u->pag_u.dat_u.fra_y = req_u->dat_y; @@ -2541,25 +2572,23 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) } // if we have the page, send it - u3_weak hit = _mesa_get_jumbo_cache(sam_u, &pac_u->pek_u.nam_u); - if ( u3_none != hit ) { - u3_noun tag, dat; - u3x_cell(hit, &tag, &dat); - if ( MESA_ITEM == tag ) { - _mesa_send_jumbo_pieces(sam_u, u3k(dat)); + u3_mesa_line* lin_u = _mesa_get_jumbo_cache(sam_u, &pac_u->pek_u.nam_u); + if ( NULL != lin_u ) { + if ( CTAG_ITEM == lin_u->typ_y ) { + _mesa_send_jumbo_pieces(sam_u, lin_u); _mesa_free_pict(pic_u); } - u3z(hit); return; } // otherwise, scry - _mesa_put_jumbo_cache(sam_u, &pac_u->pek_u.nam_u, u3nc(MESA_WAIT, 0)); + lin_u = c3_calloc(sizeof(u3_mesa_line)); + lin_u->typ_y = CTAG_WAIT; + _mesa_put_jumbo_cache(sam_u, &pac_u->pek_u.nam_u, lin_u); u3_noun sky = _name_to_jumbo_scry(&pac_u->pek_u.nam_u); u3_noun our = u3i_chubs(2, sam_u->car_u.pir_u->who_d); u3_noun bem = u3nc(u3nt(our, u3_nul, u3nc(c3__ud, 1)), sky); // NOTE: pic_u not freed u3_pier_peek(sam_u->car_u.pir_u, u3_nul, u3k(u3nq(1, c3__beam, c3__ax, bem)), pic_u, _mesa_page_scry_jumbo_cb); - u3z(hit); } static void @@ -2858,7 +2887,7 @@ u3_mesa_io_init(u3_pier* pir_u) // XX tune cache sizes sam_u->her_p = u3h_new_cache(100000); sam_u->lan_p = u3h_new_cache(100000); - sam_u->pac_p = u3h_new_cache(300000); + sam_u->pac_p = u3h_new_cache(10000); sam_u->pit_p = u3h_new_cache(10000); u3_assert( !uv_udp_init(u3L, &sam_u->wax_u) ); From 98bcfaaa9d8f5c13ee32ba27914e220bf5247ad8 Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Thu, 1 Aug 2024 16:49:51 -0400 Subject: [PATCH 312/430] hashtable: add u3h_put_get() --- pkg/noun/hashtable.c | 69 +++++++++++++++++++++++++------------------- pkg/noun/hashtable.h | 7 +++++ 2 files changed, 47 insertions(+), 29 deletions(-) diff --git a/pkg/noun/hashtable.c b/pkg/noun/hashtable.c index 0de127c4a8..cc7c1bff5d 100644 --- a/pkg/noun/hashtable.c +++ b/pkg/noun/hashtable.c @@ -15,9 +15,12 @@ */ #define BIT_SET(a_w, b_w) ((a_w) & (1 << (b_w))) -static c3_o +static u3_weak _ch_trim_slot(u3h_root* har_u, u3h_slot *sot_w, c3_w lef_w, c3_w rem_w); +static u3_weak +_ch_trim_root(u3h_root* har_u); + c3_w _ch_skip_slot(c3_w mug_w, c3_w lef_w); @@ -243,12 +246,12 @@ _ch_slot_put(u3h_slot* sot_w, u3_noun kev, c3_w lef_w, c3_w rem_w, c3_w* use_w) } } -/* u3h_put(): insert in hashtable. +/* u3h_put_get(): insert in caching hashtable, returning deleted key-value pair ** ** `key` is RETAINED; `val` is transferred. */ -void -u3h_put(u3p(u3h_root) har_p, u3_noun key, u3_noun val) +u3_weak +u3h_put_get(u3p(u3h_root) har_p, u3_noun key, u3_noun val) { u3h_root* har_u = u3to(u3h_root, har_p); u3_noun kev = u3nc(u3k(key), val); @@ -266,8 +269,23 @@ u3h_put(u3p(u3h_root) har_p, u3_noun key, u3_noun val) } if ( har_u->max_w > 0 ) { - u3h_trim_to(har_p, har_u->max_w); + return _ch_trim_root(har_u); + } + else { + return u3_none; } + +} + +/* u3h_put(): insert in hashtable. +** +** `key` is RETAINED; `val` is transferred. +*/ +void +u3h_put(u3p(u3h_root) har_p, u3_noun key, u3_noun val) +{ + u3_weak del = u3h_put_get(har_p, key, val); + u3z(del); } /* _ch_buck_del(): delete from bucket @@ -434,7 +452,7 @@ u3h_uni(u3p(u3h_root) har_p, u3p(u3h_root) rah_p) /* _ch_trim_node(): trim one entry from a node slot or its children */ -static c3_o +static u3_weak _ch_trim_node(u3h_root* har_u, u3h_slot* sot_w, c3_w lef_w, c3_w rem_w) { c3_w bit_w, map_w, inx_w; @@ -454,15 +472,8 @@ _ch_trim_node(u3h_root* har_u, u3h_slot* sot_w, c3_w lef_w, c3_w rem_w) inx_w = _ch_popcount(CUT_END(map_w, bit_w)); tos_w = &(han_u->sot_w[inx_w]); - if ( c3n == _ch_trim_slot(har_u, tos_w, lef_w, rem_w) ) { - // nothing trimmed - return c3n; - } - else if ( 0 != *tos_w ) { - // something trimmed, but slot still has value - return c3y; - } - else { + u3_weak ret = _ch_trim_slot(har_u, tos_w, lef_w, rem_w); + if ( (u3_none != ret) && (0 == *tos_w) ) { // shrink! c3_w i_w, ken_w, len_w = _ch_popcount(map_w); u3h_slot kes_w; @@ -484,30 +495,29 @@ _ch_trim_node(u3h_root* har_u, u3h_slot* sot_w, c3_w lef_w, c3_w rem_w) han_u->sot_w[i_w] = han_u->sot_w[i_w + 1]; } } - return c3y; } + return ret; } /* _ch_trim_kev(): trim a single entry slot */ -static c3_o +static u3_weak _ch_trim_kev(u3h_slot *sot_w) { if ( _(u3h_slot_is_warm(*sot_w)) ) { *sot_w = u3h_noun_be_cold(*sot_w); - return c3n; + return u3_none; } else { u3_noun kev = u3h_slot_to_noun(*sot_w); *sot_w = 0; - u3z(kev); - return c3y; + return kev; } } /* _ch_trim_node(): trim one entry from a bucket slot */ -static c3_o +static u3_weak _ch_trim_buck(u3h_root* har_u, u3h_slot* sot_w) { c3_w i_w, len_w; @@ -517,7 +527,8 @@ _ch_trim_buck(u3h_root* har_u, u3h_slot* sot_w) har_u->arm_u.inx_w < len_w; har_u->arm_u.inx_w += 1 ) { - if ( c3y == _ch_trim_kev(&(hab_u->sot_w[har_u->arm_u.inx_w])) ) { + u3_weak ret = _ch_trim_kev(&(hab_u->sot_w[har_u->arm_u.inx_w])); + if ( u3_none != ret ) { if ( 2 == len_w ) { // 2 things in bucket: debucketize to key-value pair, the next // run will point at this pair (same mug_w, no longer in bucket) @@ -535,18 +546,18 @@ _ch_trim_buck(u3h_root* har_u, u3h_slot* sot_w) // leave the arm pointing at the next index in the bucket ++(har_u->arm_u.inx_w); } - return c3y; + return ret; } } har_u->arm_u.mug_w = (har_u->arm_u.mug_w + 1) & 0x7FFFFFFF; // modulo 2^31 har_u->arm_u.inx_w = 0; - return c3n; + return u3_none; } /* _ch_trim_some(): trim one entry from a bucket or node slot */ -static c3_o +static u3_weak _ch_trim_some(u3h_root* har_u, u3h_slot* sot_w, c3_w lef_w, c3_w rem_w) { if ( 0 == lef_w ) { @@ -569,7 +580,7 @@ _ch_skip_slot(c3_w mug_w, c3_w lef_w) /* _ch_trim_slot(): trim one entry from a non-bucket slot */ -static c3_o +static u3_weak _ch_trim_slot(u3h_root* har_u, u3h_slot *sot_w, c3_w lef_w, c3_w rem_w) { if ( c3y == u3h_slot_is_noun(*sot_w) ) { @@ -583,7 +594,7 @@ _ch_trim_slot(u3h_root* har_u, u3h_slot *sot_w, c3_w lef_w, c3_w rem_w) /* _ch_trim_root(): trim one entry from a hashtable */ -static c3_o +static u3_weak _ch_trim_root(u3h_root* har_u) { c3_w mug_w = har_u->arm_u.mug_w; @@ -592,7 +603,7 @@ _ch_trim_root(u3h_root* har_u) if ( c3y == u3h_slot_is_null(*sot_w) ) { har_u->arm_u.mug_w = _ch_skip_slot(har_u->arm_u.mug_w, 25); - return c3n; + return u3_none; } return _ch_trim_slot(har_u, sot_w, 25, CUT_END(mug_w, 25)); @@ -606,7 +617,7 @@ u3h_trim_to(u3p(u3h_root) har_p, c3_w n_w) u3h_root* har_u = u3to(u3h_root, har_p); while ( har_u->use_w > n_w ) { - if ( c3y == _ch_trim_root(har_u) ) { + if ( u3_none != _ch_trim_root(har_u) ) { har_u->use_w -= 1; } } diff --git a/pkg/noun/hashtable.h b/pkg/noun/hashtable.h index 80deb9e2f7..775716deb8 100644 --- a/pkg/noun/hashtable.h +++ b/pkg/noun/hashtable.h @@ -106,6 +106,13 @@ void u3h_put(u3p(u3h_root) har_p, u3_noun key, u3_noun val); + /* u3h_put_get(): insert in caching hashtable, returning deleted entry + ** + ** `key` is RETAINED; `val` is transferred. + */ + u3_weak + u3h_put_get(u3p(u3h_root) har_p, u3_noun key, u3_noun val); + /* u3h_uni(): unify hashtables, copying [rah_p] into [har_p] */ void From f152ec506d6641dcf91fb0b4667e2c2aae55d9b3 Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Thu, 1 Aug 2024 17:01:06 -0400 Subject: [PATCH 313/430] hashtable: add u3h_trim_with(), fix u3z(null) in u3h_put() --- pkg/noun/hashtable.c | 16 ++++++++++++++-- pkg/noun/hashtable.h | 5 +++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/pkg/noun/hashtable.c b/pkg/noun/hashtable.c index cc7c1bff5d..58d852a0c5 100644 --- a/pkg/noun/hashtable.c +++ b/pkg/noun/hashtable.c @@ -285,7 +285,9 @@ void u3h_put(u3p(u3h_root) har_p, u3_noun key, u3_noun val) { u3_weak del = u3h_put_get(har_p, key, val); - u3z(del); + if ( u3_none != del ) { + u3z(del); + } } /* _ch_buck_del(): delete from bucket @@ -613,12 +615,22 @@ _ch_trim_root(u3h_root* har_u) */ void u3h_trim_to(u3p(u3h_root) har_p, c3_w n_w) +{ + u3h_trim_with(har_p, n_w, NULL); +} + +/* u3h_trim_to(): trim to n key-value pairs +*/ +void +u3h_trim_with(u3p(u3h_root) har_p, c3_w n_w, void (*del_cb)(u3_noun)) { u3h_root* har_u = u3to(u3h_root, har_p); while ( har_u->use_w > n_w ) { - if ( u3_none != _ch_trim_root(har_u) ) { + u3_weak del = _ch_trim_root(har_u); + if ( u3_none != del ) { har_u->use_w -= 1; + if (del_cb) del_cb(del); } } } diff --git a/pkg/noun/hashtable.h b/pkg/noun/hashtable.h index 775716deb8..2326ec89b9 100644 --- a/pkg/noun/hashtable.h +++ b/pkg/noun/hashtable.h @@ -144,6 +144,11 @@ void u3h_trim_to(u3p(u3h_root) har_p, c3_w n_w); + /* u3h_trim_with(): trim to n key-value pairs, with deletion callback + */ + void + u3h_trim_with(u3p(u3h_root) har_p, c3_w n_w, void (*del_cb)(u3_noun)); + /* u3h_free(): free hashtable. */ void From 7098cbe9a9a6d1539d53f4098f1ad284e010c41b Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Thu, 1 Aug 2024 17:04:15 -0400 Subject: [PATCH 314/430] hashtable: @joemfb review suggestion --- pkg/noun/hashtable.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/noun/hashtable.c b/pkg/noun/hashtable.c index 58d852a0c5..264d8218b1 100644 --- a/pkg/noun/hashtable.c +++ b/pkg/noun/hashtable.c @@ -616,7 +616,7 @@ _ch_trim_root(u3h_root* har_u) void u3h_trim_to(u3p(u3h_root) har_p, c3_w n_w) { - u3h_trim_with(har_p, n_w, NULL); + u3h_trim_with(har_p, n_w, u3a_lose); } /* u3h_trim_to(): trim to n key-value pairs @@ -630,7 +630,7 @@ u3h_trim_with(u3p(u3h_root) har_p, c3_w n_w, void (*del_cb)(u3_noun)) u3_weak del = _ch_trim_root(har_u); if ( u3_none != del ) { har_u->use_w -= 1; - if (del_cb) del_cb(del); + del_cb(del); } } } From e481d04ea124c0dc48773b2c0dae6204ab7daac7 Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Thu, 1 Aug 2024 17:15:34 -0400 Subject: [PATCH 315/430] mesa: use u3h_put_get() for jumbo frame cache --- pkg/vere/io/mesa.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 95386577f4..44bfa0440e 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -440,6 +440,14 @@ _mesa_encode_path(c3_w len_w, c3_y* buf_y) return pro; } +// does not free the line itself, only its contents +static void +_mesa_free_line(u3_mesa_line* lin_u) +{ + c3_free(lin_u->nam_u.pat_c); + c3_free(lin_u->tip_y); +} + static void _mesa_copy_name(u3_mesa_name* des_u, u3_mesa_name* src_u) { @@ -1871,8 +1879,12 @@ static void _mesa_put_jumbo_cache(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_mesa_line* lin_u) { u3_noun pax = _name_to_jumbo_scry(nam_u); - u3h_put(sam_u->pac_p, pax, u3a_outa(lin_u)); - u3z(pax); // TODO: fix refcount + u3_weak del = u3h_put_get(sam_u->pac_p, pax, u3a_outa(lin_u)); + if ( u3_none != del ) { + _mesa_free_line(u3a_into(del)); + u3z(del); + } + u3z(pax); } static void From 840760d003b66602a640f45308610fca302e2847 Mon Sep 17 00:00:00 2001 From: Sigilante Date: Thu, 1 Aug 2024 18:16:11 -0500 Subject: [PATCH 316/430] Post second review tweaks. --- pkg/noun/jets/i/lagoon.c | 374 +++++++++++++-------------------------- 1 file changed, 127 insertions(+), 247 deletions(-) diff --git a/pkg/noun/jets/i/lagoon.c b/pkg/noun/jets/i/lagoon.c index 64447176fb..5da333ecf0 100644 --- a/pkg/noun/jets/i/lagoon.c +++ b/pkg/noun/jets/i/lagoon.c @@ -30,16 +30,6 @@ c3_d c[2]; }; -/* soft check on u3_none return from q jet -*/ - static inline u3_noun _soft_run(u3_noun a) - { - if (u3_none == a) { - u3m_bail(c3__fail); - } - return a; - } - // $?(%n %u %d %z %a) static inline void _set_rounding(c3_w a) @@ -2161,31 +2151,22 @@ u3x_sam_6, &y_meta, u3x_sam_7, &y_data, 0) || + c3n == u3r_sing(x_meta, y_meta) || c3n == u3ud(x_data) || c3n == u3ud(y_data) ) { return u3m_bail(c3__exit); } else { - u3_noun x_shape, x_bloq, x_kind, x_fxp, - y_shape, y_bloq, y_kind, y_fxp, + u3_noun x_shape, x_bloq, x_kind, x_tail, rnd; x_shape = u3h(x_meta); // 2 x_bloq = u3h(u3t(x_meta)); // 6 x_kind = u3h(u3t(u3t(x_meta))); // 14 - x_fxp = u3t(u3t(u3t(x_meta))); // 15 - y_shape = u3h(y_meta); // 2 - y_bloq = u3h(u3t(y_meta)); // 6 - y_kind = u3h(u3t(u3t(y_meta))); // 14 - y_fxp = u3t(u3t(u3t(y_meta))); // 15 + x_tail = u3t(u3t(u3t(x_meta))); // 15 rnd = u3h(u3t(u3t(u3t(cor)))); // 30 if ( c3n == u3ud(x_bloq) || - c3n == u3ud(y_bloq) || c3n == u3ud(x_kind) || - c3n == u3ud(y_kind) || - c3n == u3r_sing(x_shape, y_shape) || - c3n == u3r_sing(x_bloq, y_bloq) || - c3n == u3r_sing(x_kind, y_kind) - // fxp does not need to match here so no check + c3n == u3ud(rnd) ) { return u3m_bail(c3__exit); @@ -2193,9 +2174,9 @@ switch (x_kind) { case c3__i754: _set_rounding(rnd); - u3_noun r_data = _soft_run(u3qi_la_add_i754(x_data, y_data, x_shape, x_bloq)); + u3_noun r_data = u3qi_la_add_i754(x_data, y_data, x_shape, x_bloq); if (r_data == u3_none) { return u3_none; } - return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); + return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_tail)), r_data); default: return u3_none; @@ -2217,31 +2198,22 @@ u3x_sam_6, &y_meta, u3x_sam_7, &y_data, 0) || + c3n == u3r_sing(x_meta, y_meta) || c3n == u3ud(x_data) || c3n == u3ud(y_data) ) { return u3m_bail(c3__exit); } else { - u3_noun x_shape, x_bloq, x_kind, x_fxp, - y_shape, y_bloq, y_kind, y_fxp, + u3_noun x_shape, x_bloq, x_kind, x_tail, rnd; x_shape = u3h(x_meta); // 2 x_bloq = u3h(u3t(x_meta)); // 6 x_kind = u3h(u3t(u3t(x_meta))); // 14 - x_fxp = u3t(u3t(u3t(x_meta))); // 15 - y_shape = u3h(y_meta); // 2 - y_bloq = u3h(u3t(y_meta)); // 6 - y_kind = u3h(u3t(u3t(y_meta))); // 14 - y_fxp = u3t(u3t(u3t(y_meta))); // 15 + x_tail = u3t(u3t(u3t(x_meta))); // 15 rnd = u3h(u3t(u3t(u3t(cor)))); // 30 if ( c3n == u3ud(x_bloq) || - c3n == u3ud(y_bloq) || c3n == u3ud(x_kind) || - c3n == u3ud(y_kind) || - c3n == u3r_sing(x_shape, y_shape) || - c3n == u3r_sing(x_bloq, y_bloq) || - c3n == u3r_sing(x_kind, y_kind) - // fxp does not need to match here so no check + c3n == u3ud(rnd) ) { return u3m_bail(c3__exit); @@ -2249,9 +2221,9 @@ switch (x_kind) { case c3__i754: _set_rounding(rnd); - u3_noun r_data = _soft_run(u3qi_la_sub_i754(x_data, y_data, x_shape, x_bloq)); + u3_noun r_data = u3qi_la_sub_i754(x_data, y_data, x_shape, x_bloq); if (r_data == u3_none) { return u3_none; } - return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); + return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_tail)), r_data); default: return u3_none; @@ -2273,31 +2245,22 @@ u3x_sam_6, &y_meta, u3x_sam_7, &y_data, 0) || + c3n == u3r_sing(x_meta, y_meta) || c3n == u3ud(x_data) || c3n == u3ud(y_data) ) { return u3m_bail(c3__exit); } else { - u3_noun x_shape, x_bloq, x_kind, x_fxp, - y_shape, y_bloq, y_kind, y_fxp, + u3_noun x_shape, x_bloq, x_kind, x_tail, rnd; x_shape = u3h(x_meta); // 2 x_bloq = u3h(u3t(x_meta)); // 6 x_kind = u3h(u3t(u3t(x_meta))); // 14 - x_fxp = u3t(u3t(u3t(x_meta))); // 15 - y_shape = u3h(y_meta); // 2 - y_bloq = u3h(u3t(y_meta)); // 6 - y_kind = u3h(u3t(u3t(y_meta))); // 14 - y_fxp = u3t(u3t(u3t(y_meta))); // 15 + x_tail = u3t(u3t(u3t(x_meta))); // 15 rnd = u3h(u3t(u3t(u3t(cor)))); // 30 if ( c3n == u3ud(x_bloq) || - c3n == u3ud(y_bloq) || c3n == u3ud(x_kind) || - c3n == u3ud(y_kind) || - c3n == u3r_sing(x_shape, y_shape) || - c3n == u3r_sing(x_bloq, y_bloq) || - c3n == u3r_sing(x_kind, y_kind) - // fxp does not need to match here so no check + c3n == u3ud(rnd) ) { return u3m_bail(c3__exit); @@ -2305,9 +2268,9 @@ switch (x_kind) { case c3__i754: _set_rounding(rnd); - u3_noun r_data = _soft_run(u3qi_la_mul_i754(x_data, y_data, x_shape, x_bloq)); + u3_noun r_data = u3qi_la_mul_i754(x_data, y_data, x_shape, x_bloq); if (r_data == u3_none) { return u3_none; } - return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); + return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_tail)), r_data); default: return u3_none; @@ -2329,31 +2292,22 @@ u3x_sam_6, &y_meta, u3x_sam_7, &y_data, 0) || + c3n == u3r_sing(x_meta, y_meta) || c3n == u3ud(x_data) || c3n == u3ud(y_data) ) { return u3m_bail(c3__exit); } else { - u3_noun x_shape, x_bloq, x_kind, x_fxp, - y_shape, y_bloq, y_kind, y_fxp, + u3_noun x_shape, x_bloq, x_kind, x_tail, rnd; x_shape = u3h(x_meta); // 2 x_bloq = u3h(u3t(x_meta)); // 6 x_kind = u3h(u3t(u3t(x_meta))); // 14 - x_fxp = u3t(u3t(u3t(x_meta))); // 15 - y_shape = u3h(y_meta); // 2 - y_bloq = u3h(u3t(y_meta)); // 6 - y_kind = u3h(u3t(u3t(y_meta))); // 14 - y_fxp = u3t(u3t(u3t(y_meta))); // 15 + x_tail = u3t(u3t(u3t(x_meta))); // 15 rnd = u3h(u3t(u3t(u3t(cor)))); // 30 if ( c3n == u3ud(x_bloq) || - c3n == u3ud(y_bloq) || c3n == u3ud(x_kind) || - c3n == u3ud(y_kind) || - c3n == u3r_sing(x_shape, y_shape) || - c3n == u3r_sing(x_bloq, y_bloq) || - c3n == u3r_sing(x_kind, y_kind) - // fxp does not need to match here so no check + c3n == u3ud(rnd) ) { return u3m_bail(c3__exit); @@ -2361,9 +2315,9 @@ switch (x_kind) { case c3__i754: _set_rounding(rnd); - u3_noun r_data = _soft_run(u3qi_la_div_i754(x_data, y_data, x_shape, x_bloq)); + u3_noun r_data = u3qi_la_div_i754(x_data, y_data, x_shape, x_bloq); if (r_data == u3_none) { return u3_none; } - return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); + return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_tail)), r_data); default: return u3_none; @@ -2385,31 +2339,22 @@ u3x_sam_6, &y_meta, u3x_sam_7, &y_data, 0) || + c3n == u3r_sing(x_meta, y_meta) || c3n == u3ud(x_data) || c3n == u3ud(y_data) ) { return u3m_bail(c3__exit); } else { - u3_noun x_shape, x_bloq, x_kind, x_fxp, - y_shape, y_bloq, y_kind, y_fxp, + u3_noun x_shape, x_bloq, x_kind, x_tail, rnd; x_shape = u3h(x_meta); // 2 x_bloq = u3h(u3t(x_meta)); // 6 x_kind = u3h(u3t(u3t(x_meta))); // 14 - x_fxp = u3t(u3t(u3t(x_meta))); // 15 - y_shape = u3h(y_meta); // 2 - y_bloq = u3h(u3t(y_meta)); // 6 - y_kind = u3h(u3t(u3t(y_meta))); // 14 - y_fxp = u3t(u3t(u3t(y_meta))); // 15 + x_tail = u3t(u3t(u3t(x_meta))); // 15 rnd = u3h(u3t(u3t(u3t(cor)))); // 30 if ( c3n == u3ud(x_bloq) || - c3n == u3ud(y_bloq) || c3n == u3ud(x_kind) || - c3n == u3ud(y_kind) || - c3n == u3r_sing(x_shape, y_shape) || - c3n == u3r_sing(x_bloq, y_bloq) || - c3n == u3r_sing(x_kind, y_kind) - // fxp does not need to match here so no check + c3n == u3ud(rnd) ) { return u3m_bail(c3__exit); @@ -2417,9 +2362,9 @@ switch (x_kind) { case c3__i754: _set_rounding(rnd); - u3_noun r_data = _soft_run(u3qi_la_mod_i754(x_data, y_data, x_shape, x_bloq)); + u3_noun r_data = u3qi_la_mod_i754(x_data, y_data, x_shape, x_bloq); if (r_data == u3_none) { return u3_none; } - return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); + return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_tail)), r_data); default: return u3_none; @@ -2442,12 +2387,12 @@ { return u3m_bail(c3__exit); } else { - u3_noun x_shape, x_bloq, x_kind, x_fxp, + u3_noun x_shape, x_bloq, x_kind, x_tail, rnd; x_shape = u3h(x_meta); // 2 x_bloq = u3h(u3t(x_meta)); // 6 x_kind = u3h(u3t(u3t(x_meta))); // 14 - x_fxp = u3t(u3t(u3t(x_meta))); // 15 + x_tail = u3t(u3t(u3t(x_meta))); // 15 rnd = u3h(u3t(u3t(u3t(cor)))); // 30 if ( c3n == u3ud(x_bloq) || c3n == u3ud(x_kind) || @@ -2459,9 +2404,9 @@ switch (x_kind) { case c3__i754: _set_rounding(rnd); - u3_noun r_data = _soft_run(u3qi_la_cumsum_i754(x_data, x_shape, x_bloq)); + u3_noun r_data = u3qi_la_cumsum_i754(x_data, x_shape, x_bloq); if (r_data == u3_none) { return u3_none; } - return u3nc(u3nq(u3nc(0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); + return u3nc(u3nq(u3nc(0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_tail)), r_data); default: return u3_none; @@ -2484,11 +2429,10 @@ { return u3m_bail(c3__exit); } else { - u3_noun x_shape, x_bloq, x_kind, x_fxp; + u3_noun x_shape, x_bloq, x_kind; x_shape = u3h(x_meta); // 2 x_bloq = u3h(u3t(x_meta)); // 6 x_kind = u3h(u3t(u3t(x_meta))); // 14 - x_fxp = u3t(u3t(u3t(x_meta))); // 15 if ( c3n == u3ud(x_bloq) || c3n == u3ud(x_kind) || c3n == _check(u3nc(x_meta, x_data)) @@ -2498,7 +2442,7 @@ } else { switch (x_kind) { case c3__i754: { - u3_noun r_data = _soft_run(u3qi_la_argmin_i754(x_data, x_shape, x_bloq)); + u3_noun r_data = u3qi_la_argmin_i754(x_data, x_shape, x_bloq); // bare atom (@ index) return r_data;} @@ -2523,7 +2467,7 @@ { return u3m_bail(c3__exit); } else { - u3_noun x_shape, x_bloq, x_kind, x_fxp; + u3_noun x_shape, x_bloq, x_kind; x_shape = u3h(x_meta); // 2 x_bloq = u3h(u3t(x_meta)); // 6 x_kind = u3h(u3t(u3t(x_meta))); // 14 @@ -2536,7 +2480,7 @@ } else { switch (x_kind) { case c3__i754: { - u3_noun r_data = _soft_run(u3qi_la_ravel_i754(x_data, x_shape, x_bloq)); + u3_noun r_data = u3qi_la_ravel_i754(x_data, x_shape, x_bloq); // (list @) return r_data;} @@ -2561,11 +2505,10 @@ { return u3m_bail(c3__exit); } else { - u3_noun x_shape, x_bloq, x_kind, x_fxp; + u3_noun x_shape, x_bloq, x_kind; x_shape = u3h(x_meta); // 2 x_bloq = u3h(u3t(x_meta)); // 6 x_kind = u3h(u3t(u3t(x_meta))); // 14 - x_fxp = u3t(u3t(u3t(x_meta))); // 15 if ( c3n == u3ud(x_bloq) || c3n == u3ud(x_kind) || c3n == _check(u3nc(x_meta, x_data)) @@ -2575,7 +2518,7 @@ } else { switch (x_kind) { case c3__i754: { - u3_noun r_data = _soft_run(u3qi_la_argmax_i754(x_data, x_shape, x_bloq)); + u3_noun r_data = u3qi_la_argmax_i754(x_data, x_shape, x_bloq); // bare atom (@ index) return r_data;} @@ -2600,11 +2543,11 @@ { return u3m_bail(c3__exit); } else { - u3_noun x_shape, x_bloq, x_kind, x_fxp; + u3_noun x_shape, x_bloq, x_kind, x_tail; x_shape = u3h(x_meta); // 2 x_bloq = u3h(u3t(x_meta)); // 6 x_kind = u3h(u3t(u3t(x_meta))); // 14 - x_fxp = u3t(u3t(u3t(x_meta))); // 15 + x_tail = u3t(u3t(u3t(x_meta))); // 15 if ( c3n == u3ud(x_bloq) || c3n == u3ud(x_kind) || c3n == _check(u3nc(x_meta, x_data)) @@ -2614,9 +2557,9 @@ } else { switch (x_kind) { case c3__i754: { - u3_noun r_data = _soft_run(u3qi_la_min_i754(x_data, x_shape, x_bloq)); + u3_noun r_data = u3qi_la_min_i754(x_data, x_shape, x_bloq); if (r_data == u3_none) { return u3_none; } - return u3nc(u3nq(u3nt(0x1, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data);} + return u3nc(u3nq(u3nt(0x1, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_tail)), r_data);} default: return u3_none; @@ -2639,11 +2582,11 @@ { return u3m_bail(c3__exit); } else { - u3_noun x_shape, x_bloq, x_kind, x_fxp; + u3_noun x_shape, x_bloq, x_kind, x_tail; x_shape = u3h(x_meta); // 2 x_bloq = u3h(u3t(x_meta)); // 6 x_kind = u3h(u3t(u3t(x_meta))); // 14 - x_fxp = u3t(u3t(u3t(x_meta))); // 15 + x_tail = u3t(u3t(u3t(x_meta))); // 15 if ( c3n == u3ud(x_bloq) || c3n == u3ud(x_kind) || c3n == _check(u3nc(x_meta, x_data)) @@ -2653,9 +2596,9 @@ } else { switch (x_kind) { case c3__i754: { - u3_noun r_data = _soft_run(u3qi_la_max_i754(x_data, x_shape, x_bloq)); + u3_noun r_data = u3qi_la_max_i754(x_data, x_shape, x_bloq); if (r_data == u3_none) { return u3_none; } - return u3nc(u3nq(u3nt(0x1, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data);} + return u3nc(u3nq(u3nt(0x1, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_tail)), r_data);} default: return u3_none; @@ -2678,11 +2621,11 @@ { return u3m_bail(c3__exit); } else { - u3_noun x_shape, x_bloq, x_kind, x_fxp; + u3_noun x_shape, x_bloq, x_kind, x_tail; x_shape = u3h(x_meta); // 2 x_bloq = u3h(u3t(x_meta)); // 6 x_kind = u3h(u3t(u3t(x_meta))); // 14 - x_fxp = u3t(u3t(u3t(x_meta))); // 15 + x_tail = u3t(u3t(u3t(x_meta))); // 15 if ( c3n == u3ud(x_bloq) || c3n == u3ud(x_kind) ) @@ -2691,9 +2634,9 @@ } else { switch (x_kind) { case c3__i754: { - u3_noun r_data = _soft_run(u3qi_la_abs_i754(x_data, x_shape, x_bloq)); + u3_noun r_data = u3qi_la_abs_i754(x_data, x_shape, x_bloq); if (r_data == u3_none) { return u3_none; } - return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data);} + return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_tail)), r_data);} default: return u3_none; @@ -2715,39 +2658,27 @@ u3x_sam_6, &y_meta, u3x_sam_7, &y_data, 0) || + c3n == u3r_sing(x_meta, y_meta) || c3n == u3ud(x_data) || c3n == u3ud(y_data) ) { return u3m_bail(c3__exit); } else { - u3_noun x_shape, x_bloq, x_kind, x_fxp, - y_shape, y_bloq, y_kind, y_fxp, - rnd; + u3_noun x_shape, x_bloq, x_kind; x_shape = u3h(x_meta); // 2 x_bloq = u3h(u3t(x_meta)); // 6 x_kind = u3h(u3t(u3t(x_meta))); // 14 - x_fxp = u3t(u3t(u3t(x_meta))); // 15 - y_shape = u3h(y_meta); // 2 - y_bloq = u3h(u3t(y_meta)); // 6 - y_kind = u3h(u3t(u3t(y_meta))); // 14 - y_fxp = u3t(u3t(u3t(y_meta))); // 15 if ( c3n == u3ud(x_bloq) || - c3n == u3ud(y_bloq) || - c3n == u3ud(x_kind) || - c3n == u3ud(y_kind) || - c3n == u3r_sing(x_shape, y_shape) || - c3n == u3r_sing(x_bloq, y_bloq) || - c3n == u3r_sing(x_kind, y_kind) || - c3n == u3r_sing(x_fxp, y_fxp) + c3n == u3ud(x_kind) ) { return u3m_bail(c3__exit); } else { switch (x_kind) { case c3__i754: { - u3_noun r_data = _soft_run(u3qi_la_gth_i754(x_data, y_data, x_shape, x_bloq)); + u3_noun r_data = u3qi_la_gth_i754(x_data, y_data, x_shape, x_bloq); if (r_data == u3_none) { return u3_none; } - return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data);} + return u3nc(u3k(x_meta), r_data);} default: return u3_none; @@ -2769,39 +2700,27 @@ u3x_sam_6, &y_meta, u3x_sam_7, &y_data, 0) || + c3n == u3r_sing(x_meta, y_meta) || c3n == u3ud(x_data) || c3n == u3ud(y_data) ) { return u3m_bail(c3__exit); } else { - u3_noun x_shape, x_bloq, x_kind, x_fxp, - y_shape, y_bloq, y_kind, y_fxp, - rnd; + u3_noun x_shape, x_bloq, x_kind; x_shape = u3h(x_meta); // 2 x_bloq = u3h(u3t(x_meta)); // 6 x_kind = u3h(u3t(u3t(x_meta))); // 14 - x_fxp = u3t(u3t(u3t(x_meta))); // 15 - y_shape = u3h(y_meta); // 2 - y_bloq = u3h(u3t(y_meta)); // 6 - y_kind = u3h(u3t(u3t(y_meta))); // 14 - y_fxp = u3t(u3t(u3t(y_meta))); // 15 if ( c3n == u3ud(x_bloq) || - c3n == u3ud(y_bloq) || - c3n == u3ud(x_kind) || - c3n == u3ud(y_kind) || - c3n == u3r_sing(x_shape, y_shape) || - c3n == u3r_sing(x_bloq, y_bloq) || - c3n == u3r_sing(x_kind, y_kind) || - c3n == u3r_sing(x_fxp, y_fxp) + c3n == u3ud(x_kind) ) { return u3m_bail(c3__exit); } else { switch (x_kind) { case c3__i754: { - u3_noun r_data = _soft_run(u3qi_la_gte_i754(x_data, y_data, x_shape, x_bloq)); + u3_noun r_data = u3qi_la_gte_i754(x_data, y_data, x_shape, x_bloq); if (r_data == u3_none) { return u3_none; } - return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data);} + return u3nc(u3k(x_meta), r_data);} default: return u3_none; @@ -2823,39 +2742,27 @@ u3x_sam_6, &y_meta, u3x_sam_7, &y_data, 0) || + c3n == u3r_sing(x_meta, y_meta) || c3n == u3ud(x_data) || c3n == u3ud(y_data) ) { return u3m_bail(c3__exit); } else { - u3_noun x_shape, x_bloq, x_kind, x_fxp, - y_shape, y_bloq, y_kind, y_fxp, - rnd; + u3_noun x_shape, x_bloq, x_kind; x_shape = u3h(x_meta); // 2 x_bloq = u3h(u3t(x_meta)); // 6 x_kind = u3h(u3t(u3t(x_meta))); // 14 - x_fxp = u3t(u3t(u3t(x_meta))); // 15 - y_shape = u3h(y_meta); // 2 - y_bloq = u3h(u3t(y_meta)); // 6 - y_kind = u3h(u3t(u3t(y_meta))); // 14 - y_fxp = u3t(u3t(u3t(y_meta))); // 15 if ( c3n == u3ud(x_bloq) || - c3n == u3ud(y_bloq) || - c3n == u3ud(x_kind) || - c3n == u3ud(y_kind) || - c3n == u3r_sing(x_shape, y_shape) || - c3n == u3r_sing(x_bloq, y_bloq) || - c3n == u3r_sing(x_kind, y_kind) || - c3n == u3r_sing(x_fxp, y_fxp) + c3n == u3ud(x_kind) ) { return u3m_bail(c3__exit); } else { switch (x_kind) { case c3__i754: { - u3_noun r_data = _soft_run(u3qi_la_lth_i754(x_data, y_data, x_shape, x_bloq)); + u3_noun r_data = u3qi_la_lth_i754(x_data, y_data, x_shape, x_bloq); if (r_data == u3_none) { return u3_none; } - return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data);} + return u3nc(u3k(x_meta), r_data);} default: return u3_none; @@ -2877,39 +2784,27 @@ u3x_sam_6, &y_meta, u3x_sam_7, &y_data, 0) || + c3n == u3r_sing(x_meta, y_meta) || c3n == u3ud(x_data) || c3n == u3ud(y_data) ) { return u3m_bail(c3__exit); } else { - u3_noun x_shape, x_bloq, x_kind, x_fxp, - y_shape, y_bloq, y_kind, y_fxp, - rnd; + u3_noun x_shape, x_bloq, x_kind; x_shape = u3h(x_meta); // 2 x_bloq = u3h(u3t(x_meta)); // 6 x_kind = u3h(u3t(u3t(x_meta))); // 14 - x_fxp = u3t(u3t(u3t(x_meta))); // 15 - y_shape = u3h(y_meta); // 2 - y_bloq = u3h(u3t(y_meta)); // 6 - y_kind = u3h(u3t(u3t(y_meta))); // 14 - y_fxp = u3t(u3t(u3t(y_meta))); // 15 if ( c3n == u3ud(x_bloq) || - c3n == u3ud(y_bloq) || - c3n == u3ud(x_kind) || - c3n == u3ud(y_kind) || - c3n == u3r_sing(x_shape, y_shape) || - c3n == u3r_sing(x_bloq, y_bloq) || - c3n == u3r_sing(x_kind, y_kind) || - c3n == u3r_sing(x_fxp, y_fxp) + c3n == u3ud(x_kind) ) { return u3m_bail(c3__exit); } else { switch (x_kind) { case c3__i754: { - u3_noun r_data = _soft_run(u3qi_la_lte_i754(x_data, y_data, x_shape, x_bloq)); + u3_noun r_data = u3qi_la_lte_i754(x_data, y_data, x_shape, x_bloq); if (r_data == u3_none) { return u3_none; } - return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data);} + return u3nc(u3k(x_meta), r_data);} default: return u3_none; @@ -2934,19 +2829,19 @@ { return u3m_bail(c3__exit); } else { - u3_noun x_shape, x_bloq, x_kind, x_fxp, + u3_noun x_shape, x_bloq, x_kind, x_tail, rnd; x_shape = u3h(x_meta); // 2 x_bloq = u3h(u3t(x_meta)); // 6 x_kind = u3h(u3t(u3t(x_meta))); // 14 - x_fxp = u3t(u3t(u3t(x_meta))); // 15 + x_tail = u3t(u3t(u3t(x_meta))); // 15 rnd = u3h(u3t(u3t(u3t(cor)))); // 30 switch (x_kind) { case c3__i754: _set_rounding(rnd); - u3_noun r_data = _soft_run(u3qi_la_adds_i754(x_data, n, x_shape, x_bloq)); + u3_noun r_data = u3qi_la_adds_i754(x_data, n, x_shape, x_bloq); if (r_data == u3_none) { return u3_none; } - return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); + return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_tail)), r_data); default: return u3_none; @@ -2970,19 +2865,19 @@ { return u3m_bail(c3__exit); } else { - u3_noun x_shape, x_bloq, x_kind, x_fxp, + u3_noun x_shape, x_bloq, x_kind, x_tail, rnd; x_shape = u3h(x_meta); // 2 x_bloq = u3h(u3t(x_meta)); // 6 x_kind = u3h(u3t(u3t(x_meta))); // 14 - x_fxp = u3t(u3t(u3t(x_meta))); // 15 + x_tail = u3t(u3t(u3t(x_meta))); // 15 rnd = u3h(u3t(u3t(u3t(cor)))); // 30 switch (x_kind) { case c3__i754: _set_rounding(rnd); - u3_noun r_data = _soft_run(u3qi_la_subs_i754(x_data, n, x_shape, x_bloq)); + u3_noun r_data = u3qi_la_subs_i754(x_data, n, x_shape, x_bloq); if (r_data == u3_none) { return u3_none; } - return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); + return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_tail)), r_data); default: return u3_none; @@ -3006,19 +2901,19 @@ { return u3m_bail(c3__exit); } else { - u3_noun x_shape, x_bloq, x_kind, x_fxp, + u3_noun x_shape, x_bloq, x_kind, x_tail, rnd; x_shape = u3h(x_meta); // 2 x_bloq = u3h(u3t(x_meta)); // 6 x_kind = u3h(u3t(u3t(x_meta))); // 14 - x_fxp = u3t(u3t(u3t(x_meta))); // 15 + x_tail = u3t(u3t(u3t(x_meta))); // 15 rnd = u3h(u3t(u3t(u3t(cor)))); // 30 switch (x_kind) { case c3__i754: _set_rounding(rnd); - u3_noun r_data = _soft_run(u3qi_la_muls_i754(x_data, n, x_shape, x_bloq)); + u3_noun r_data = u3qi_la_muls_i754(x_data, n, x_shape, x_bloq); if (r_data == u3_none) { return u3_none; } - return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); + return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_tail)), r_data); default: return u3_none; @@ -3042,19 +2937,19 @@ { return u3m_bail(c3__exit); } else { - u3_noun x_shape, x_bloq, x_kind, x_fxp, + u3_noun x_shape, x_bloq, x_kind, x_tail, rnd; x_shape = u3h(x_meta); // 2 x_bloq = u3h(u3t(x_meta)); // 6 x_kind = u3h(u3t(u3t(x_meta))); // 14 - x_fxp = u3t(u3t(u3t(x_meta))); // 15 + x_tail = u3t(u3t(u3t(x_meta))); // 15 rnd = u3h(u3t(u3t(u3t(cor)))); // 30 switch (x_kind) { case c3__i754: _set_rounding(rnd); - u3_noun r_data = _soft_run(u3qi_la_divs_i754(x_data, n, x_shape, x_bloq)); + u3_noun r_data = u3qi_la_divs_i754(x_data, n, x_shape, x_bloq); if (r_data == u3_none) { return u3_none; } - return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); + return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_tail)), r_data); default: return u3_none; @@ -3078,19 +2973,19 @@ { return u3m_bail(c3__exit); } else { - u3_noun x_shape, x_bloq, x_kind, x_fxp, + u3_noun x_shape, x_bloq, x_kind, x_tail, rnd; x_shape = u3h(x_meta); // 2 x_bloq = u3h(u3t(x_meta)); // 6 x_kind = u3h(u3t(u3t(x_meta))); // 14 - x_fxp = u3t(u3t(u3t(x_meta))); // 15 + x_tail = u3t(u3t(u3t(x_meta))); // 15 rnd = u3h(u3t(u3t(u3t(cor)))); // 30 switch (x_kind) { case c3__i754: _set_rounding(rnd); - u3_noun r_data = _soft_run(u3qi_la_mods_i754(x_data, n, x_shape, x_bloq)); + u3_noun r_data = u3qi_la_mods_i754(x_data, n, x_shape, x_bloq); if (r_data == u3_none) { return u3_none; } - return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); + return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_tail)), r_data); default: return u3_none; @@ -3111,31 +3006,21 @@ u3x_sam_6, &y_meta, u3x_sam_7, &y_data, 0) || + c3n == u3r_sing(x_meta, y_meta) || c3n == u3ud(x_data) || c3n == u3ud(y_data) ) { return u3m_bail(c3__exit); } else { - u3_noun x_shape, x_bloq, x_kind, x_fxp, - y_shape, y_bloq, y_kind, y_fxp, + u3_noun x_shape, x_bloq, x_kind, x_tail, rnd; x_shape = u3h(x_meta); // 2 x_bloq = u3h(u3t(x_meta)); // 6 x_kind = u3h(u3t(u3t(x_meta))); // 14 - x_fxp = u3t(u3t(u3t(x_meta))); // 15 - y_shape = u3h(y_meta); // 2 - y_bloq = u3h(u3t(y_meta)); // 6 - y_kind = u3h(u3t(u3t(y_meta))); // 14 - y_fxp = u3t(u3t(u3t(y_meta))); // 15 + x_tail = u3t(u3t(u3t(x_meta))); // 15 rnd = u3h(u3t(u3t(u3t(cor)))); // 30 if ( c3n == u3ud(x_bloq) || - c3n == u3ud(y_bloq) || - c3n == u3ud(x_kind) || - c3n == u3ud(y_kind) || - c3n == u3r_sing(x_shape, y_shape) || - c3n == u3r_sing(x_bloq, y_bloq) || - c3n == u3r_sing(x_kind, y_kind) || - c3n == u3r_sing(x_fxp, y_fxp) + c3n == u3ud(x_kind) ) { return u3m_bail(c3__exit); @@ -3143,10 +3028,10 @@ switch (x_kind) { case c3__i754: _set_rounding(rnd); - u3_noun r_data = _soft_run(u3qi_la_dot_i754(x_data, y_data, x_shape, x_bloq)); + u3_noun r_data = u3qi_la_dot_i754(x_data, y_data, x_shape, x_bloq); if (r_data == u3_none) { return u3_none; } c3_d len_x0 = _get_dims(x_shape)[0]; - return u3nc(u3nq(u3nt(len_x0, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); + return u3nc(u3nq(u3nt(len_x0, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_tail)), r_data); default: return u3_none; @@ -3169,11 +3054,11 @@ { return u3m_bail(c3__exit); } else { - u3_noun x_shape, x_bloq, x_kind, x_fxp; + u3_noun x_shape, x_bloq, x_kind, x_tail; x_shape = u3h(x_meta); // 2 x_bloq = u3h(u3t(x_meta)); // 6 x_kind = u3h(u3t(u3t(x_meta))); // 14 - x_fxp = u3t(u3t(u3t(x_meta))); // 15 + x_tail = u3t(u3t(u3t(x_meta))); // 15 if ( c3n == u3ud(x_bloq) || c3n == u3ud(x_kind) || c3n == _check(cor) @@ -3181,9 +3066,9 @@ { return u3m_bail(c3__exit); } else { - u3_noun r_data = _soft_run(u3qi_la_transpose(x_data, x_shape, x_bloq)); + u3_noun r_data = u3qi_la_transpose(x_data, x_shape, x_bloq); if (r_data == u3_none) { return u3_none; } - return u3nc(u3nq(u3nt(u3k(u3h(x_shape)), u3k(u3h(u3t(x_shape))), u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); + return u3nc(u3nq(u3nt(u3k(u3h(x_shape)), u3k(u3h(u3t(x_shape))), u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_tail)), r_data); } } } @@ -3202,11 +3087,11 @@ { return u3m_bail(c3__exit); } else { - u3_noun x_shape, x_bloq, x_kind, x_fxp; + u3_noun x_shape, x_bloq, x_kind, x_tail; x_shape = u3h(x_meta); // 2 x_bloq = u3h(u3t(x_meta)); // 6 x_kind = u3h(u3t(u3t(x_meta))); // 14 - x_fxp = u3t(u3t(u3t(x_meta))); // 15 + x_tail = u3t(u3t(u3t(x_meta))); // 15 rnd = u3h(u3t(u3t(u3t(cor)))); // 30 if ( c3n == u3ud(x_bloq) || c3n == u3ud(x_kind) || @@ -3219,10 +3104,10 @@ switch (x_kind) { case c3__i754: _set_rounding(rnd); - u3_noun r_data = _soft_run(u3qi_la_linspace_i754(a, b, n, x_bloq)); + u3_noun r_data = u3qi_la_linspace_i754(a, b, n, x_bloq); if (r_data == u3_none) { return u3_none; } x_shape = u3nc(u3x_atom(n), u3_nul); - return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); + return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_tail)), r_data); default: return u3_none; @@ -3245,11 +3130,11 @@ { return u3m_bail(c3__exit); } else { - u3_noun x_shape, x_bloq, x_kind, x_fxp; + u3_noun x_shape, x_bloq, x_kind, x_tail; x_shape = u3h(x_meta); // 2 x_bloq = u3h(u3t(x_meta)); // 6 x_kind = u3h(u3t(u3t(x_meta))); // 14 - x_fxp = u3t(u3t(u3t(x_meta))); // 15 + x_tail = u3t(u3t(u3t(x_meta))); // 15 rnd = u3h(u3t(u3t(u3t(cor)))); // 30 if ( c3n == u3ud(x_bloq) || c3n == u3ud(x_kind) @@ -3260,7 +3145,7 @@ switch (x_kind) { case c3__i754: _set_rounding(rnd); - u3_noun r_data = _soft_run(u3qi_la_range_i754(a, b, d, x_bloq)); + u3_noun r_data = u3qi_la_range_i754(a, b, d, x_bloq); if (r_data == u3_none) { return u3_none; } c3_d a_, b_, d_; c3_ds n_; @@ -3289,14 +3174,14 @@ u3r_bytes(0, 16, (c3_y*)&b__, b); u3r_bytes(0, 16, (c3_y*)&d__, d); float128_t tmp; - f128M_sub((float128_t*){&b__}, (float128_t*){&a__}, &tmp); - f128M_div(&tmp, (float128_t*){&d__}, &tmp); + f128M_sub((float128_t*)&b__, (float128_t*)&a__, &tmp); + f128M_div(&tmp, (float128_t*)&d__, &tmp); n_ = f128M_to_i64(&tmp, softfloat_round_minMag, false); break;} } u3_noun n = u3i_chub(n_+1); x_shape = u3nt(u3k(n), 0x1, u3_nul); - return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); + return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_tail)), r_data); default: return u3_none; @@ -3319,11 +3204,11 @@ { return u3m_bail(c3__exit); } else { - u3_noun x_shape, x_bloq, x_kind, x_fxp; + u3_noun x_shape, x_bloq, x_kind, x_tail; x_shape = u3h(x_meta); // 2 x_bloq = u3h(u3t(x_meta)); // 6 x_kind = u3h(u3t(u3t(x_meta))); // 14 - x_fxp = u3t(u3t(u3t(x_meta))); // 15 + x_tail = u3t(u3t(u3t(x_meta))); // 15 if ( c3n == u3ud(x_bloq) || c3n == u3ud(x_kind) || c3n == _check(cor) @@ -3331,10 +3216,10 @@ { return u3m_bail(c3__exit); } else { - u3_noun r_data = _soft_run(u3qi_la_diag(x_data, x_shape, x_bloq)); + u3_noun r_data = u3qi_la_diag(x_data, x_shape, x_bloq); if (r_data == u3_none) { return u3_none; } c3_d len_x0 = _get_dims(x_shape)[0]; - return u3nc(u3nq(u3nt(len_x0, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data); + return u3nc(u3nq(u3nt(len_x0, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_tail)), r_data); } } } @@ -3353,12 +3238,12 @@ { return u3m_bail(c3__exit); } else { - u3_noun x_shape, x_bloq, x_kind, x_fxp; + u3_noun x_shape, x_bloq, x_kind, x_tail; if ( c3n == u3r_mean(x_meta, 2, &x_shape, 6, &x_bloq, 14, &x_kind, - 15, &x_fxp, + 15, &x_tail, 0) ) { @@ -3366,9 +3251,9 @@ } else { switch (x_kind) { case c3__i754: { - u3_noun r_data = _soft_run(u3qi_la_trace_i754(x_data, x_shape, x_bloq)); + u3_noun r_data = u3qi_la_trace_i754(x_data, x_shape, x_bloq); if (r_data == u3_none) { return u3_none; } - return u3nc(u3nq(u3nt(0x1, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_fxp)), r_data);} + return u3nc(u3nq(u3nt(0x1, 0x1, u3_nul), u3k(x_bloq), u3k(x_kind), u3k(x_tail)), r_data);} default: return u3_none; @@ -3383,7 +3268,7 @@ // Each argument is a ray, [=meta data=@ux] u3_noun x_meta, x_data, y_meta, y_data; - + fprintf(stderr, "mmul 1\n"); if ( c3n == u3r_mean(cor, u3x_sam_4, &x_meta, u3x_sam_5, &x_data, @@ -3395,31 +3280,26 @@ { return u3m_bail(c3__exit); } else { - u3_noun x_shape, x_bloq, x_kind, x_fxp, - y_shape, y_bloq, y_kind, y_fxp, + u3_noun x_shape, x_bloq, x_kind, x_tail, + y_shape, rnd; x_shape = u3h(x_meta); // 2 x_bloq = u3h(u3t(x_meta)); // 6 x_kind = u3h(u3t(u3t(x_meta))); // 14 - x_fxp = u3t(u3t(u3t(x_meta))); // 15 + x_tail = u3t(u3t(u3t(x_meta))); // 15 y_shape = u3h(y_meta); // 2 - y_bloq = u3h(u3t(y_meta)); // 6 - y_kind = u3h(u3t(u3t(y_meta))); // 14 - y_fxp = u3t(u3t(u3t(y_meta))); // 15 rnd = u3h(u3t(u3t(u3t(cor)))); // 30 - if ( c3n == u3r_sing(x_bloq, y_bloq) || - c3n == u3r_sing(x_kind, y_kind) || - c3n == _check(u3nc(x_meta, x_data)) || + if ( c3n == _check(u3nc(x_meta, x_data)) || c3n == _check(u3nc(y_meta, y_data)) - // fxp does not need to match so no check ) { return u3m_bail(c3__exit); } else { switch (x_kind) { case c3__i754: + fprintf(stderr, "mmul 2\n"); _set_rounding(rnd); - u3_noun r_data = _soft_run(u3qi_la_mmul_i754(x_data, y_data, x_shape, y_shape, x_bloq)); + u3_noun r_data = u3qi_la_mmul_i754(x_data, y_data, x_shape, y_shape, x_bloq); // result is already [meta data] return r_data; From c4030eb38031e6aede98f58edf3709a895a3a6bb Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Fri, 2 Aug 2024 10:12:00 +0200 Subject: [PATCH 317/430] mesa: reorder call to _mesa_del_request --- pkg/vere/io/mesa.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 527bd67e84..02aad19774 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -2440,9 +2440,6 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) c3_d now_d = _get_now_micros(); u3l_log("%u kilobytes took %f ms", req_u->tot_w, (now_d - sam_u->tim_d)/1000.0); - - _mesa_del_request(sam_u, &pac_u->pag_u.nam_u, pac_u->pag_u.dat_u.tot_w); - { // construct jumbo frame pac_u->pag_u.nam_u.boq_y = 31; // TODO: use actual jumbo bloq @@ -2461,6 +2458,8 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) c3_free(buf_y); } + _mesa_del_request(sam_u, &pac_u->pag_u.nam_u); + u3_auto_plan(&sam_u->car_u, u3_ovum_init(0, c3__ames, u3nc(c3__ames, u3_nul), cad)); } else if ( req_u->len_w < req_u->tot_w ) { From 4594a3816a15eef2d275804f5089b5c66a8cf038 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Fri, 2 Aug 2024 10:15:07 +0200 Subject: [PATCH 318/430] mesa: always release refcount to lan in _realise_lane --- pkg/vere/io/mesa.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 02aad19774..4e3592d3dc 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1283,8 +1283,9 @@ _realise_lane(u3_noun lan) { } else { u3l_log("mesa: inscrutable lane"); } - u3z(lan); + } + u3z(lan); return lan_u; } From 5cea76a641f28fe2ced780abeb7eddc6c67806c0 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Fri, 2 Aug 2024 16:15:38 +0200 Subject: [PATCH 319/430] stun: fix incorrect buffer length --- pkg/vere/io/ames/stun.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/vere/io/ames/stun.c b/pkg/vere/io/ames/stun.c index e1f314ef07..491db66720 100644 --- a/pkg/vere/io/ames/stun.c +++ b/pkg/vere/io/ames/stun.c @@ -21,10 +21,10 @@ _stun_add_fingerprint(c3_y *message, c3_w index) } static c3_o -_stun_has_fingerprint(c3_y* buf_y, c3_w len_w) +_stun_has_fingerprint(c3_y* buf_y, c3_w buf_len_w) { c3_y ned_y[4] = {0x80, 0x28, 0x00, 0x04}; - if ( len_w < 28 ) { // At least STUN header and FINGERPRINT + if ( buf_len_w < 28 ) { // At least STUN header and FINGERPRINT return c3n; } @@ -32,14 +32,14 @@ _stun_has_fingerprint(c3_y* buf_y, c3_w len_w) c3_y* fin_y = 0; c3_w i = 20; // start after the header - fin_y = memmem(buf_y + i, len_w - i, ned_y, sizeof(ned_y)); + fin_y = memmem(buf_y + i, buf_len_w - i, ned_y, sizeof(ned_y)); if ( fin_y != 0 ) { c3_w len_w = fin_y - buf_y; // Skip attribute type and length c3_w fingerprint = c3_sift_word(fin_y + sizeof(ned_y)); c3_w init = crc32(0L, Z_NULL, 0); c3_w crc = htonl(crc32(init, buf_y, len_w) ^ 0x5354554e); - if ((fingerprint == crc) && (fin_y - buf_y + 8) == len_w) { + if ((fingerprint == crc) && (fin_y - buf_y + 8) == buf_len_w) { return c3y; } } From 354852befb6be54dd77182f8617b376727c3542e Mon Sep 17 00:00:00 2001 From: midden-fabler Date: Fri, 2 Aug 2024 19:37:57 -0400 Subject: [PATCH 320/430] http: weaker spur, chunk edge case, cleanup --- pkg/vere/io/http.c | 52 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 13 deletions(-) diff --git a/pkg/vere/io/http.c b/pkg/vere/io/http.c index 5bf8fcc169..834843effd 100644 --- a/pkg/vere/io/http.c +++ b/pkg/vere/io/http.c @@ -658,8 +658,19 @@ _chunk_align(byte_range* rng_u) c3_z siz_z = 4194304; // 4MiB if ( SIZE_MAX != rng_u->beg_z ) { - rng_u->beg_z = (rng_u->beg_z / siz_z) * siz_z; - rng_u->end_z = (rng_u->beg_z + siz_z) - 1; + if ( rng_u->beg_z > rng_u->end_z ) { + rng_u->beg_z = SIZE_MAX; + rng_u->end_z = SIZE_MAX; + } + else { + // XX an out-of-bounds request could be aligned to in-bounds + // resulting in a 200 or 206 response instead of 416. + // browsers should have the total length from content-range, + // and send reasonable range requests. + // + rng_u->beg_z = (rng_u->beg_z / siz_z) * siz_z; + rng_u->end_z = (rng_u->beg_z + siz_z) - 1; + } } else if ( SIZE_MAX != rng_u->end_z ) { // round up to multiple of siz_z @@ -682,7 +693,8 @@ _parse_range(c3_c* txt_c, c3_w len_w) rng_u.end_z = h2o_strtosize(hep_c + 1, len_w - ((hep_c + 1) - txt_c)); // strange -> [SIZE_MAX SIZE_MAX] if ( ((SIZE_MAX == rng_u.beg_z) && (hep_c != txt_c)) - || ((SIZE_MAX == rng_u.end_z) && (len_w - ((hep_c + 1) - txt_c) > 0)) ) + || ((SIZE_MAX == rng_u.end_z) && (len_w - ((hep_c + 1) - txt_c) > 0)) + || ((SIZE_MAX != rng_u.beg_z) && (rng_u.beg_z > rng_u.end_z)) ) { rng_u.beg_z = SIZE_MAX; rng_u.end_z = SIZE_MAX; @@ -691,6 +703,8 @@ _parse_range(c3_c* txt_c, c3_w len_w) return rng_u; } +/* _get_range(): get a _byte_range from headers +*/ static c3_o _get_range(h2o_headers_t req_headers, byte_range* rng_u) { @@ -714,7 +728,7 @@ _get_range(h2o_headers_t req_headers, byte_range* rng_u) return c3y; } -/* _http_scry_cb() +/* _http_scry_cb(): respond and maybe cache scry result */ static void _http_scry_cb(void* vod_p, u3_noun nun) @@ -747,13 +761,17 @@ _http_scry_cb(void* vod_p, u3_noun nun) c3_free(peq_u); } +/* _beam: ship desk case spur +*/ typedef struct _beam { - u3_noun who; - u3_noun des; - u3_noun cas; + u3_weak who; + u3_weak des; + u3_weak cas; u3_weak pur; } beam; +/* _free_beam(): free a beam +*/ static void _free_beam(beam* bem) { @@ -763,14 +781,14 @@ _free_beam(beam* bem) u3z(bem->pur); } -/* _get_beam: path to beam +/* _get_beam(): get a _beam from url */ static beam _get_beam(u3_hreq* req_u, c3_c* txt_c, c3_w len_w) { beam bem; - // get beak from path + // get beak // for ( c3_w i_w = 0; i_w < 3; ++i_w ) { u3_noun* wer; @@ -798,6 +816,7 @@ _get_beam(u3_hreq* req_u, c3_c* txt_c, c3_w len_w) txt_c++; len_w--; } + // '=' if ( (len_w > 0) && ('=' == txt_c[0]) ) { if ( 0 == i_w ) { @@ -828,8 +847,8 @@ _get_beam(u3_hreq* req_u, c3_c* txt_c, c3_w len_w) } if ( !nex_c ) { - // XX bad beam - *wer = u3_nul; + *wer = u3_none; + return bem; } else { c3_w dif_w = (c3_p)(nex_c - txt_c); @@ -840,6 +859,7 @@ _get_beam(u3_hreq* req_u, c3_c* txt_c, c3_w len_w) } } + // get spur u3_noun tmp = u3dc("rush", u3i_bytes(len_w, (const c3_y*)txt_c), u3v_wish("stap")); bem.pur = ( u3_nul == tmp ) ? u3_none : u3k(u3t(tmp)); u3z(tmp); @@ -867,7 +887,7 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) || (0 != memcmp("/_~_/", bas_c, 5)) ) { // no: inject to arvo - u3_noun wir = _http_req_to_duct(req_u); + u3_noun wir = _http_req_to_duct(req_u); u3_noun cad; u3_noun adr = u3nc(c3__ipv4, u3i_words(1, &req_u->hon_u->ipf_w)); // XX loopback automatically secure too? @@ -904,7 +924,11 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) } beam bem = _get_beam(req_u, bas_c, len_w); - if ( u3_none == bem.pur ) { + if ( (u3_none == bem.who) + || (u3_none == bem.des) + || (u3_none == bem.cas) + || (u3_none == bem.pur) ) + { c3_c* msg_c = "bad request"; h2o_send_error_generic(req_u->rec_u, 400, msg_c, msg_c, 0); u3z(gang); @@ -1012,6 +1036,8 @@ _http_cache_respond(u3_hreq* req_u, u3_noun nun) u3z(nun); } +/* _http_scry_respond(): respond with a simple-payload:http +*/ static void _http_scry_respond(u3_hreq* req_u, u3_noun nun) { From 9b984a0c28073ceffc11325eb30a8d1dad9381a7 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Mon, 5 Aug 2024 10:07:28 +0200 Subject: [PATCH 321/430] stun: add success response header --- pkg/vere/io/ames/stun.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pkg/vere/io/ames/stun.c b/pkg/vere/io/ames/stun.c index 491db66720..e22bd4cdac 100644 --- a/pkg/vere/io/ames/stun.c +++ b/pkg/vere/io/ames/stun.c @@ -126,6 +126,9 @@ u3_stun_make_response(const c3_y req_y[20], // memcpy(buf_y, req_y, cur_w); + buf_y[0] = 0x01; buf_y[1] = 0x01; // 0x0101 SUCCESS RESPONSE + buf_y[2] = 0x00; buf_y[3] = 0x14; // Length: 20 bytes + memset(buf_y + cur_w, 0, cur_w); // XOR-MAPPED-ADDRESS From e7898244bf6f88db4cb3b3a60f19c3aa8dd9aa96 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Mon, 5 Aug 2024 10:23:41 +0200 Subject: [PATCH 322/430] ames. mesa: support STUN --- pkg/vere/io/ames.c | 235 +++++++++++++++++++++++---------------------- pkg/vere/io/mesa.c | 34 ++++--- 2 files changed, 138 insertions(+), 131 deletions(-) diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index 73ba0b2a46..9c55bf14f8 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -1039,7 +1039,7 @@ _stun_on_request(u3_ames* sam_u, u3_stun_make_response(req_y, &lan_u, snd_u->hun_y); uv_buf_t buf_u = uv_buf_init((c3_c*)snd_u->hun_y, 40); - c3_i sas_i = uv_udp_send(&snd_u->req_u, &sam_u->wax_u, + c3_i sas_i = uv_udp_send(&snd_u->req_u, &u3_Host.wax_u, &buf_u, 1, adr_u, _stun_send_cb); if ( sas_i ) { @@ -1116,7 +1116,7 @@ _stun_send_request(u3_ames* sam_u) add_u.sin_port = htons(sam_u->sun_u.lan_u.por_s); uv_buf_t buf_u = uv_buf_init((c3_c*)snd_u->hun_y, 28); - c3_i sas_i = uv_udp_send(&snd_u->req_u, &sam_u->wax_u, &buf_u, 1, + c3_i sas_i = uv_udp_send(&snd_u->req_u, &u3_Host.wax_u, &buf_u, 1, (const struct sockaddr*)&add_u, _stun_send_cb); if ( sas_i ) { @@ -2013,7 +2013,7 @@ _ames_try_forward(u3_pact* pac_u) */ void _ames_hear(u3_ames* sam_u, - u3_lane* lan_u, + const struct sockaddr* adr_u, c3_w len_w, c3_y* hun_y) { @@ -2021,111 +2021,136 @@ _ames_hear(u3_ames* sam_u, c3_w pre_w; c3_w cur_w = 0; // cursor: how many bytes we've read from hun_y - // make sure packet is big enough to have a header - // - if ( HEAD_SIZE > len_w ) { - sam_u->sat_u.hed_d++; - if ( 0 == (sam_u->sat_u.hed_d % 100000) ) { - u3l_log("ames: %" PRIu64 " dropped, failed to read header", - sam_u->sat_u.hed_d); - } + // XX reorg, check if a STUN req/resp can look like an ames packet + // check the mug hash of the body of the packet, if not check if STUN + // otherwise , invalid packet, log failure + // check ames first, assume that STUN could maybe (not likely) overlap with ames + // for next protocol version, have an urbit cookie + // + if ( c3y == u3_stun_is_request(hun_y, len_w) ) { + _stun_on_request(sam_u, hun_y, adr_u); + c3_free(hun_y); + } + else if ( c3y == u3_stun_is_our_response(hun_y, + sam_u->sun_u.tid_y, len_w) ) + { + _stun_on_response(sam_u, hun_y, len_w); c3_free(hun_y); - return; } + else { + struct sockaddr_in* add_u = (struct sockaddr_in*)adr_u; + u3_lane lan_u = { + .por_s = ntohs(add_u->sin_port), + .pip_w = ntohl(add_u->sin_addr.s_addr) + }; - pac_u = c3_calloc(sizeof(*pac_u)); - pac_u->sam_u = sam_u; - pac_u->len_w = len_w; - pac_u->hun_y = hun_y; - pac_u->lan_u = *lan_u; - cur_w = 0; - - // parse the header - // - _ames_sift_head(&pac_u->hed_u, pac_u->hun_y); - cur_w += HEAD_SIZE; - - pac_u->typ_y = _ames_pact_typ(&pac_u->hed_u); + // make sure packet is big enough to have a header + // + if ( HEAD_SIZE > len_w ) { + sam_u->sat_u.hed_d++; + if ( 0 == (sam_u->sat_u.hed_d % 100000) ) { + u3l_log("ames: %" PRIu64 " dropped, failed to read header", + sam_u->sat_u.hed_d); + } - // ensure the protocol version matches ours - // - // XX rethink use of [fit_o] here and elsewhere - // - if ( (c3y == sam_u->fig_u.fit_o) - && (sam_u->ver_y != pac_u->hed_u.ver_y) ) - { - sam_u->sat_u.vet_d++; - if ( 0 == (sam_u->sat_u.vet_d % 100000) ) { - u3l_log("ames: %" PRIu64 " dropped for version mismatch", - sam_u->sat_u.vet_d); + c3_free(hun_y); + return; } - _ames_pact_free(pac_u); - return; - } - // check contents match mug in header - // - if ( c3n == _ames_check_mug(pac_u) ) { - // _log_head(&pac_u->hed_u); - sam_u->sat_u.mut_d++; - if ( 0 == (sam_u->sat_u.mut_d % 100000) ) { - u3l_log("ames: %" PRIu64 " dropped for invalid mug", - sam_u->sat_u.mut_d); - } - _ames_pact_free(pac_u); - return; - } + pac_u = c3_calloc(sizeof(*pac_u)); + pac_u->sam_u = sam_u; + pac_u->len_w = len_w; + pac_u->hun_y = hun_y; + pac_u->lan_u = lan_u; + cur_w = 0; - // check that packet is big enough for prelude - // - pre_w = _ames_prel_size(&pac_u->hed_u); - if ( len_w < cur_w + pre_w ) { - sam_u->sat_u.pre_d++; - if ( 0 == (sam_u->sat_u.pre_d % 100000) ) { - u3l_log("ames: %" PRIu64 " dropped, failed to read prelude", - sam_u->sat_u.pre_d); - } - _ames_pact_free(pac_u); - return; - } + // parse the header + // + _ames_sift_head(&pac_u->hed_u, pac_u->hun_y); + cur_w += HEAD_SIZE; - // parse prelude - // - _ames_sift_prel(&pac_u->hed_u, &pac_u->pre_u, pac_u->hun_y + cur_w); - cur_w += pre_w; + pac_u->typ_y = _ames_pact_typ(&pac_u->hed_u); - // if we can scry for lanes, - // and we are not the recipient, - // we might want to forward statelessly - // - if ( (c3y == sam_u->fig_u.see_o) - && ( (pac_u->pre_u.rec_d[0] != sam_u->pir_u->who_d[0]) - || (pac_u->pre_u.rec_d[1] != sam_u->pir_u->who_d[1]) ) ) - { - if ( c3y == sam_u->sat_u.for_o ) { - _ames_try_forward(pac_u); + // ensure the protocol version matches ours + // + // XX rethink use of [fit_o] here and elsewhere + // + if ( (c3y == sam_u->fig_u.fit_o) + && (sam_u->ver_y != pac_u->hed_u.ver_y) ) + { + sam_u->sat_u.vet_d++; + if ( 0 == (sam_u->sat_u.vet_d % 100000) ) { + u3l_log("ames: %" PRIu64 " dropped for version mismatch", + sam_u->sat_u.vet_d); + } + _ames_pact_free(pac_u); + return; } - } - else { - // enter protocol-specific packet handling + + // check contents match mug in header // - switch ( pac_u->typ_y ) { - case PACT_WAIL: { - _fine_hear_request(pac_u, cur_w); - } break; + if ( c3n == _ames_check_mug(pac_u) ) { + // _log_head(&pac_u->hed_u); + sam_u->sat_u.mut_d++; + if ( 0 == (sam_u->sat_u.mut_d % 100000) ) { + u3l_log("ames: %" PRIu64 " dropped for invalid mug", + sam_u->sat_u.mut_d); + } + _ames_pact_free(pac_u); + return; + } - case PACT_PURR: { - _fine_hear_response(pac_u, cur_w); - } break; + // check that packet is big enough for prelude + // + pre_w = _ames_prel_size(&pac_u->hed_u); + if ( len_w < cur_w + pre_w ) { + sam_u->sat_u.pre_d++; + if ( 0 == (sam_u->sat_u.pre_d % 100000) ) { + u3l_log("ames: %" PRIu64 " dropped, failed to read prelude", + sam_u->sat_u.pre_d); + } + _ames_pact_free(pac_u); + return; + } - case PACT_AMES: { - _ames_hear_ames(pac_u, cur_w); - } break; + // parse prelude + // + _ames_sift_prel(&pac_u->hed_u, &pac_u->pre_u, pac_u->hun_y + cur_w); + cur_w += pre_w; - default: { - u3l_log("ames_hear: bad packet type %d", pac_u->typ_y); - u3_pier_bail(u3_king_stub()); + // if we can scry for lanes, + // and we are not the recipient, + // we might want to forward statelessly + // + if ( (c3y == sam_u->fig_u.see_o) + && ( (pac_u->pre_u.rec_d[0] != sam_u->pir_u->who_d[0]) + || (pac_u->pre_u.rec_d[1] != sam_u->pir_u->who_d[1]) ) ) + { + if ( c3y == sam_u->sat_u.for_o ) { + _ames_try_forward(pac_u); + } + } + else { + // enter protocol-specific packet handling + // + switch ( pac_u->typ_y ) { + case PACT_WAIL: { + _fine_hear_request(pac_u, cur_w); + } break; + + case PACT_PURR: { + _fine_hear_response(pac_u, cur_w); + } break; + + case PACT_AMES: { + _ames_hear_ames(pac_u, cur_w); + } break; + + default: { + u3l_log("ames_hear: bad packet type %d", pac_u->typ_y); + u3_pier_bail(u3_king_stub()); + } } } } @@ -2157,32 +2182,10 @@ _ames_recv_cb(uv_udp_t* wax_u, } c3_free(buf_u->base); } - // XX reorg, check if a STUN req/resp can look like an ames packet - // check the mug hash of the body of the packet, if not check if STUN - // otherwise , invalid packet, log failure - // check ames first, assume that STUN could maybe (not likely) overlap with ames - // for next protocol version, have an urbit cookie - // - else if ( c3y == u3_stun_is_request((c3_y*)buf_u->base, nrd_i) ) { - _stun_on_request(sam_u, (c3_y *)buf_u->base, adr_u); - c3_free(buf_u->base); - } - else if ( c3y == u3_stun_is_our_response((c3_y*)buf_u->base, - sam_u->sun_u.tid_y, nrd_i) ) - { - _stun_on_response(sam_u, (c3_y*)buf_u->base, nrd_i); - c3_free(buf_u->base); - } else { - struct sockaddr_in* add_u = (struct sockaddr_in*)adr_u; - u3_lane lan_u = { - .por_s = ntohs(add_u->sin_port), - .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_hear(sam_u, adr_u, (c3_w)nrd_i, (c3_y*)buf_u->base); } } diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 4e3592d3dc..55dd84a0cb 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -6,6 +6,7 @@ #include "noun.h" #include "ur.h" #include "ship.h" +#include "io/ames/stun.h" #include "mesa/mesa.h" #include "mesa/bitset.h" #include @@ -2674,13 +2675,13 @@ _mesa_hear_poke(u3_mesa_pict* pic_u, u3_lane* lan_u) void _ames_hear(void* sam_u, - u3_lane* lan_u, + const struct sockaddr* adr_u, c3_w len_w, c3_y* hun_y); static void _mesa_hear(u3_mesa* sam_u, - u3_lane* lan_u, + const struct sockaddr* adr_u, c3_w len_w, c3_y* hun_y) { @@ -2701,21 +2702,29 @@ _mesa_hear(u3_mesa* sam_u, // MESA_LOG(sam_u, SERIAL) // c3_free(hun_y); mesa_free_pact(&pic_u->pac_u); - _ames_hear(u3_Host.sam_u, lan_u, len_w, hun_y); + _ames_hear(u3_Host.sam_u, adr_u, len_w, hun_y); return; } c3_free(hun_y); + struct sockaddr_in* add_u = (struct sockaddr_in*)adr_u; + u3_lane lan_u; + + lan_u.por_s = ntohs(add_u->sin_port); + // u3l_log("port: %s", lan_u.por_s); + lan_u.pip_w = ntohl(add_u->sin_addr.s_addr); + + switch ( pic_u->pac_u.hed_u.typ_y ) { case PACT_PEEK: { - _mesa_hear_peek(pic_u, *lan_u); + _mesa_hear_peek(pic_u, lan_u); } break; case PACT_PAGE: { - _mesa_hear_page(pic_u, *lan_u); + _mesa_hear_page(pic_u, lan_u); } break; default: { - _mesa_hear_poke(pic_u, lan_u); + _mesa_hear_poke(pic_u, &lan_u); } break; } } @@ -2742,18 +2751,13 @@ static void _mesa_recv_cb(uv_udp_t* wax_u, c3_free(buf_u->base); } else { - u3_mesa* 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); - // u3l_log("port: %s", lan_u.por_s); - lan_u.pip_w = ntohl(add_u->sin_addr.s_addr); + u3_mesa* mes_u = wax_u->data; // u3l_log("IP: %x", lan_u.pip_w); // NB: [nrd_i] will never exceed max length from _ames_alloc() // - _mesa_hear(sam_u, &lan_u, (c3_w)nrd_i, (c3_y*)buf_u->base); + + _mesa_hear(mes_u, adr_u, (c3_w)nrd_i, (c3_y*)buf_u->base); + } } From f55fbf8d6336b498ce17e4c670c31406b5338d6a Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Mon, 5 Aug 2024 10:28:18 +0200 Subject: [PATCH 323/430] mesa: free jumbo_pact_y cursor --- pkg/vere/io/mesa.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 55dd84a0cb..698967a502 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1966,7 +1966,7 @@ _mesa_send_jumbo_pieces(u3_mesa* sam_u, u3_noun pag) u3z(pin); } } - // mesa_free_pact(&tac_u); // XX ?? + c3_free(jumbo_pact_y); // XX lss_builder_free(bil_u); } From 633b5a29beb4aeef97ed89b16ad7db253f9fc2cf Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Mon, 5 Aug 2024 14:13:44 +0200 Subject: [PATCH 324/430] mesa: don't send to ourselves in indirect mode ...send directly instead. the sponsee will eventually ping and _hear_peer will update the direct lane --- pkg/vere/io/mesa.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 698967a502..74e2118848 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1001,15 +1001,20 @@ _mesa_send_modal(u3_peer* per_u, c3_y* buf_y, c3_w len_w) c3_y* sen_y = c3_calloc(len_w); memcpy(sen_y, buf_y, len_w); - if ( c3y == _mesa_is_direct_mode(per_u) ) { + u3_ship gal_u = {0}; + gal_u[0] = per_u->imp_y; + c3_o our_o = u3_ships_equal(gal_u, sam_u->pir_u->who_d); + + if ( ( c3y == _mesa_is_direct_mode(per_u) ) || + // if we are the sponsor of the ship, don't send to ourselves + (our_o == c3y) ) { u3l_log("mesa: direct"); _mesa_send_buf(sam_u, per_u->dan_u, sen_y, len_w); per_u->dir_u.sen_d = now_d; } else { + #ifdef MESA_DEBUG - u3_ship gal_u = {0}; - gal_u[0] = per_u->imp_y; c3_c* gal_c = u3_ship_to_string(gal_u); u3l_log("mesa: sending to %s", gal_c); c3_free(gal_c); From 9ab726c22f7e051c73b71f8137aa61346e2ea927 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Mon, 5 Aug 2024 15:39:48 +0200 Subject: [PATCH 325/430] mesa: fix build --- pkg/vere/io/mesa.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 93caf11968..5036bf1380 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -2006,8 +2006,8 @@ _mesa_send_jumbo_pieces(u3_mesa* sam_u, u3_mesa_line* lin_u) u3z(pin); } } - c3_free(jumbo_pact_y); // XX - lss_builder_free(bil_u); + // c3_free(jumbo_pact_y); // XX + // lss_builder_free(bil_u); } static void From a0fd92197b29d62a2c20d834d212d2379a35cafe Mon Sep 17 00:00:00 2001 From: midden-fabler Date: Mon, 5 Aug 2024 12:30:48 -0400 Subject: [PATCH 326/430] http: fix possible bail foul --- pkg/vere/io/http.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pkg/vere/io/http.c b/pkg/vere/io/http.c index 834843effd..0584930e36 100644 --- a/pkg/vere/io/http.c +++ b/pkg/vere/io/http.c @@ -909,6 +909,7 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) req_u->peq_u->htd_u = htd_u; req_u->peq_u->las_o = c3n; req_u->sat_e = u3_rsat_peek; + req_u->peq_u->pax = u3_nul; u3_hfig* fig_u = &req_u->hon_u->htp_u->htd_u->fig_u; h2o_req_t* rec_u = req_u->rec_u; @@ -932,6 +933,7 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) c3_c* msg_c = "bad request"; h2o_send_error_generic(req_u->rec_u, 400, msg_c, msg_c, 0); u3z(gang); + u3z(req_u->peq_u->pax); _free_beam(&bem); return; } @@ -974,6 +976,7 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) || ((u3_nul == gang) && (c3y == u3r_at(14, nac))) ) { // maybe cache, then serve subsequent range requests from cache + u3z(req_u->peq_u->pax); req_u->peq_u->pax = u3k(bam); u3_pier_peek(htd_u->car_u.pir_u, gang, u3nt(0, c3__ex, bam), req_u->peq_u, _http_scry_cb); @@ -985,7 +988,7 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) u3z(gang); } } - _free_beam(&bem); + _free_beam(&bem); } } } From f13c5c4f44548d011eb9355e1c94497e8abdda3d Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Mon, 5 Aug 2024 14:28:26 -0400 Subject: [PATCH 327/430] pact.c: propagate packet parse failures --- pkg/vere/io/mesa/pact.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index 881ec3afc6..db4a57a041 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -538,6 +538,9 @@ mesa_sift_pact(u3_mesa_pact* pac_u, c3_y* buf_y, c3_w len_w) break; } } + if ( 0 == res_w ) { + return 0; + } { c3_w mug_w = u3r_mug_bytes(buf_y, res_w); From 403d67f32b7e0c733abb856fe05b0380748b112d Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Mon, 5 Aug 2024 14:28:49 -0400 Subject: [PATCH 328/430] pact: remove cruft --- pkg/vere/io/mesa/pact.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index db4a57a041..f3dce48798 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -332,10 +332,6 @@ _mesa_sift_name(u3_mesa_name* nam_u, c3_y* buf_y, c3_w len_w) static c3_w _mesa_sift_data(u3_mesa_data* dat_u, c3_y* buf_y, c3_w len_w) { -// #ifdef MESA_DEBUG - // u3l_log("mesa: sifting data %i", len_w); -// #endif - c3_w cur_w = 0; u3_mesa_data_meta met_u; From 9ffeec8e2ea9f4966a094b0ba91136d7a00e1761 Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Mon, 5 Aug 2024 14:29:13 -0400 Subject: [PATCH 329/430] mesa: fix crash in _mesa_page_scry_jumbo_cb() --- pkg/vere/io/mesa.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 44bfa0440e..aad6116628 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -2025,13 +2025,23 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun res) { u3a_atom* pat_u = u3a_to_ptr(pac); u3_mesa_pact jum_u; - mesa_sift_pact(&jum_u, (c3_y*)pat_u->buf_w, pat_u->len_w); + if ( 0 == mesa_sift_pact(&jum_u, + (c3_y*)pat_u->buf_w, + pat_u->len_w << 2) ) { + u3l_log("mesa: jumbo frame parse failure"); + log_pact(pac_u); + u3z(res); + return; + } u3_mesa_data* dat_u = &jum_u.pag_u.dat_u; c3_w tip_w = // bytes in Merkle spine - (c3n == jum_u.pag_u.nam_u.nit_o)? 0 : lss_proof_size(dat_u->tot_w); + (c3n == jum_u.pag_u.nam_u.nit_o)? + 0 : + lss_proof_size(dat_u->tot_w); c3_w dat_w = dat_u->len_w; // bytes in fragment data in this jumbo frame - c3_w haz_w = (lin_u->len_w + 1023) / 1024 * 2 * 32; // bytes in hash pairs + c3_w lev_w = (dat_w + 1023) / 1024; // number of leaves in this frame + c3_w haz_w = lev_w * sizeof(lss_pair); // bytes in hash pairs c3_w len_w = tip_w + dat_w + haz_w; lin_u = u3a_malloc(sizeof(u3_mesa_line)); From 47c6be8f70ea2903f9127fec26cb8cd4a8e9ed42 Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Mon, 5 Aug 2024 18:36:06 -0400 Subject: [PATCH 330/430] mesa: use total message bytes in message format; compiles --- pkg/vere/io/mesa.c | 185 ++++++++++++++++++++-------------------- pkg/vere/io/mesa/mesa.h | 3 +- pkg/vere/io/mesa/pact.c | 49 ++++++++--- 3 files changed, 131 insertions(+), 106 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index aad6116628..a4d2902935 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -167,15 +167,15 @@ typedef struct _u3_peer { typedef struct _u3_pend_req { u3_peer* per_u; // backpointer - c3_w nex_w; // number of the next fragment to be sent - c3_w tot_w; // total number of fragments expected + c3_d nex_d; // number of the next fragment to be sent + c3_d tot_d; // total number of expected bytes u3_auth_data aum_u; // message authenticator uv_timer_t tim_u; // timehandler c3_y* dat_y; // ((mop @ud *) lte) - c3_w len_w; - c3_w lef_w; // lowest fragment number currently in flight/pending - c3_w old_w; // frag num of oldest packet sent - c3_w ack_w; // highest acked fragment number + c3_d hav_d; // how many fragments we've received + c3_d lef_d; // lowest fragment number currently in flight/pending + c3_d old_d; // frag num of oldest packet sent + c3_d ack_d; // highest acked fragment number u3_gage* gag_u; // congestion control u3_misord_buf mis_u[8]; // misordered packets lss_verifier* los_u; // Lockstep verifier @@ -198,7 +198,7 @@ typedef struct _u3_mesa_line { u3_mesa_ctag typ_y; // pending or present? u3_mesa_name nam_u; // full name for data, ready to serialize u3_auth_data aum_u; // message authenticator, none if not initial frame - c3_w tot_w; // number of fragments in message + c3_w tot_d; // number of bytes in whole message c3_w dat_w; // size in bytes of dat_y c3_w len_w; // total allocated size, in bytes c3_y* tip_y; // initial Merkle spine, nullable @@ -301,9 +301,9 @@ _log_pend_req(u3_pend_req* req_u) u3l_log("pending request was NULL"); return; } - u3l_log("have: %u", req_u->len_w); - u3l_log("next: %u", req_u->nex_w); - u3l_log("total: %u", req_u->tot_w); + u3l_log("have: %"PRIu64, req_u->hav_d); + u3l_log("next: %"PRIu64, req_u->nex_d); + u3l_log("total: %" PRIu64, req_u->tot_d); u3l_log("gage: %c", req_u->gag_u == NULL ? 'n' : 'y'); //u3l_log("timer in: %" PRIu64 " ms", uv_timer_get_due_in(&req_u->tim_u)); } @@ -311,7 +311,7 @@ _log_pend_req(u3_pend_req* req_u) static void _log_mesa_data(u3_mesa_data dat_u) { - u3l_log("total frag: %u", dat_u.tot_w); + u3l_log("total frag: %" PRIu64, dat_u.tot_d); u3l_log("frag len: %u", dat_u.len_w); // u3l_log("frag: %xxx", dat_u.fra_y); } @@ -802,10 +802,10 @@ static void _mesa_handle_ack(u3_gage* gag_u, u3_pact_stat* pat_u) } } -static inline c3_w +static inline c3_d _mesa_req_get_remaining(u3_pend_req* req_u) { - return req_u->tot_w - req_u->nex_w; + return req_u->tot_d - req_u->nex_d; } /* @@ -847,8 +847,8 @@ _mesa_req_pact_sent(u3_pend_req* req_u, u3_mesa_name* nam_u) c3_d now_d = _get_now_micros(); // if we already have pending request if ( NULL != req_u ) { - if( req_u->nex_w == nam_u->fra_w ) { - req_u->nex_w++; + if( req_u->nex_d == nam_u->fra_w ) { + req_u->nex_d++; } // TODO: optional assertions? /* req_u->wat_u[nam_u->fra_w] = (u3_pact_stat){now_d, 0, 1, 0 }; */ @@ -865,11 +865,12 @@ _mesa_req_pact_sent(u3_pend_req* req_u, u3_mesa_name* nam_u) return; } - if ( req_u->lef_w != 0 && c3n == bitset_has(&req_u->was_u, req_u->lef_w) ) { - while ( req_u->lef_w++ < req_u->tot_w ) { - if ( c3y == bitset_has(&req_u->was_u, req_u->lef_w) ) { + if ( req_u->lef_d != 0 && c3n == bitset_has(&req_u->was_u, req_u->lef_d) ) { + while (req_u->lef_d < mesa_num_leaves(req_u->tot_d)) { + if ( c3y == bitset_has(&req_u->was_u, req_u->lef_d) ) { break; } + req_u->lef_d++; } } } @@ -1095,21 +1096,21 @@ _mesa_send_request(u3_mesa_request_data* dat_u) } static void -_try_resend(u3_pend_req* req_u, c3_w ack_w) +_try_resend(u3_pend_req* req_u, c3_d ack_d) { c3_o los_o = c3n; c3_d now_d = _get_now_micros(); u3_mesa_pact *pac_u = &req_u->pic_u->pac_u; c3_y* buf_y = c3_calloc(PACT_SIZE); - for ( int i = req_u->lef_w; i < ack_w; i++ ) { + for ( c3_d i_d = req_u->lef_d; i_d < ack_d; i_d++ ) { // TODO: make fast recovery different from slow // TODO: track skip count but not dupes, since dupes are meaningless - if ( (c3y == bitset_has(&req_u->was_u, i)) && - (now_d - req_u->wat_u[i].sen_d > req_u->gag_u->rto_w) ) { + if ( (c3y == bitset_has(&req_u->was_u, i_d)) && + (now_d - req_u->wat_u[i_d].sen_d > req_u->gag_u->rto_w) ) { los_o = c3y; - pac_u->pek_u.nam_u.fra_w = i; + pac_u->pek_u.nam_u.fra_w = i_d; c3_w siz_w = mesa_etch_pact(buf_y, pac_u); if ( 0 == siz_w ) { u3_assert(!"failed to etch"); @@ -1133,22 +1134,22 @@ _mesa_packet_timeout(uv_timer_t* tim_u); static void _update_resend_timer(u3_pend_req *req_u) { - if( req_u->tot_w == 0 || req_u->len_w == req_u->tot_w ) { - u3l_log("bad condition tot_w: %u len_w: %u", - req_u->tot_w, req_u->len_w); + if( req_u->tot_d == 0 || req_u->hav_d == req_u->tot_d ) { + u3l_log("bad condition tot_d: %"PRIu64" hav_d: %"PRIu64, + req_u->tot_d, req_u->hav_d); return; } // scan in flight packets, find oldest - c3_w idx_w = req_u->lef_w; + c3_w idx_d = req_u->lef_d; c3_d now_d = _get_now_micros(); c3_d wen_d = now_d; - for ( c3_w i = req_u->lef_w; i < req_u->nex_w; i++ ) { + for ( c3_d i = req_u->lef_d; i < req_u->nex_d; i++ ) { // u3l_log("fra %u (%u)", i, __LINE__); if ( c3y == bitset_has(&req_u->was_u, i) && wen_d > req_u->wat_u[i].sen_d ) { wen_d = req_u->wat_u[i].sen_d; - idx_w = i; + idx_d = i; } } if ( now_d == wen_d ) { @@ -1156,9 +1157,9 @@ _update_resend_timer(u3_pend_req *req_u) /* u3l_log("failed to find new oldest"); */ #endif } - req_u->old_w = idx_w; + req_u->old_d = idx_d; req_u->tim_u.data = req_u; - c3_d gap_d = req_u->wat_u[idx_w].sen_d - now_d; + c3_d gap_d = req_u->wat_u[idx_d].sen_d - now_d; /* u3l_log("timeout %llu", (gag_u->rto_w - gap_d) / 1000); */ c3_w dur_w = (req_u->gag_u->rto_w - gap_d) / 1000; uv_timer_start(&req_u->tim_u, _mesa_packet_timeout, dur_w, 0); @@ -1169,8 +1170,8 @@ _update_resend_timer(u3_pend_req *req_u) static void _mesa_packet_timeout(uv_timer_t* tim_u) { u3_pend_req* req_u = (u3_pend_req*)tim_u->data; - /* u3l_log("%u packet timed out", req_u->old_w); */ - _try_resend(req_u, req_u->nex_w); + /* u3l_log("%"PRIu64" packet timed out", req_u->old_d); */ + _try_resend(req_u, req_u->nex_d); _update_resend_timer(req_u); } @@ -1212,8 +1213,9 @@ _mesa_req_pact_done(u3_pend_req* req_u, u3_mesa* sam_u = req_u->per_u->sam_u; // needed for the MESA_LOG macro // received past the end of the message - if ( dat_u->tot_w <= nam_u->fra_w ) { - u3l_log("strange tot_w %u fra_w %u req_u %u", dat_u->tot_w, nam_u->fra_w, req_u->len_w); + if ( dat_u->tot_d <= nam_u->fra_w ) { + u3l_log("strange tot_d %"PRIu64" fra_w %u req_u %"PRIu64, + dat_u->tot_d, nam_u->fra_w, req_u->hav_d); MESA_LOG(sam_u, STRANGE); // XX: is this sufficient to drop whole request return; @@ -1226,8 +1228,8 @@ _mesa_req_pact_done(u3_pend_req* req_u, } bitset_del(&req_u->was_u, nam_u->fra_w); - if ( nam_u->fra_w > req_u->ack_w ) { - req_u->ack_w = nam_u->fra_w; + if ( nam_u->fra_w > req_u->ack_d ) { + req_u->ack_d = nam_u->fra_w; } #ifdef MESA_DEBUG @@ -1236,13 +1238,13 @@ _mesa_req_pact_done(u3_pend_req* req_u, } #endif - req_u->len_w++; + req_u->hav_d++; #ifdef MESA_DEBUG - u3l_log("fragment %u len %u", nam_u->fra_w, req_u->len_w); + u3l_log("fragment %u len %"PRIu64, nam_u->fra_w, req_u->hav_d); #endif - if ( req_u->lef_w == nam_u->fra_w ) { - req_u->lef_w++; + if ( req_u->lef_d == nam_u->fra_w ) { + req_u->lef_d++; } lss_pair* par_u = NULL; @@ -1366,7 +1368,7 @@ _mesa_send_bufs(u3_mesa* sam_u, static void _mesa_timer_cb(uv_timer_t* tim_u) { u3_pend_req* req_u = tim_u->data; - _try_resend(req_u, req_u->nex_w); + _try_resend(req_u, req_u->nex_d); } static void @@ -1926,13 +1928,14 @@ _mesa_send_jumbo_pieces(u3_mesa* sam_u, u3_mesa_line* lin_u) u3_mesa_data* dat_u = &pac_u.pag_u.dat_u; { - dat_u->tot_w = lin_u->tot_w; + dat_u->tot_d = lin_u->tot_d; dat_u->aum_u = lin_u->aum_u; // aup_u, len_w, and fra_y vary by fragment } - c3_w pro_w = lss_proof_size(lin_u->tot_w); + c3_d mev_d = mesa_num_leaves(dat_u->tot_d); + c3_w pro_w = lss_proof_size(mev_d); - if ( c3y == nam_u->nit_o && lin_u->tot_w > 4) { + if ( c3y == nam_u->nit_o && mev_d > 4) { u3_weak pin = _mesa_get_pit(sam_u, nam_u); if ( u3_none != pin ) { #ifdef MESA_DEBUG @@ -1949,7 +1952,7 @@ _mesa_send_jumbo_pieces(u3_mesa* sam_u, u3_mesa_line* lin_u) } // send leaf packets - c3_w lev_w = lin_u->tot_w - nam_u->fra_w; + c3_w lev_w = mesa_num_leaves(lin_u->dat_w); c3_w fir_w = nam_u->fra_w; c3_y* pro_y = lin_u->tip_y; for (c3_w i_w = 0; i_w < lev_w; i_w++) { @@ -1983,6 +1986,7 @@ _mesa_send_jumbo_pieces(u3_mesa* sam_u, u3_mesa_line* lin_u) #ifdef MESA_DEBUG u3l_log(" sending leaf packet, fra_w: %u", nam_u->fra_w); #endif + log_pact(&pac_u); _mesa_send_pact(sam_u, u3k(u3t(pin)), NULL, &pac_u); _mesa_del_pit(sam_u, nam_u); u3k(pin); @@ -2035,10 +2039,11 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun res) } u3_mesa_data* dat_u = &jum_u.pag_u.dat_u; + c3_d mev_d = (dat_u->tot_d + 1023) / 1024; // leaves in message c3_w tip_w = // bytes in Merkle spine (c3n == jum_u.pag_u.nam_u.nit_o)? 0 : - lss_proof_size(dat_u->tot_w); + lss_proof_size(mev_d); c3_w dat_w = dat_u->len_w; // bytes in fragment data in this jumbo frame c3_w lev_w = (dat_w + 1023) / 1024; // number of leaves in this frame c3_w haz_w = lev_w * sizeof(lss_pair); // bytes in hash pairs @@ -2048,7 +2053,7 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun res) lin_u->typ_y = CTAG_ITEM; _mesa_copy_name(&lin_u->nam_u, &jum_u.pek_u.nam_u); lin_u->aum_u = dat_u->aum_u; - lin_u->tot_w = dat_u->tot_w; + lin_u->tot_d = dat_u->tot_d; lin_u->dat_w = dat_w; lin_u->len_w = len_w; lin_u->tip_y = c3_malloc(len_w); // note: off-loom @@ -2129,13 +2134,13 @@ _mesa_request_next_fragments(u3_mesa* sam_u, { c3_w win_w = _mesa_req_get_cwnd(req_u); u3_mesa_pict* nex_u = req_u->pic_u; - c3_w nex_w = req_u->nex_w; + c3_w nex_d = req_u->nex_d; for ( int i = 0; i < win_w; i++ ) { - c3_w fra_w = nex_w + i; - if ( fra_w >= req_u->tot_w ) { + c3_w fra_w = nex_d + i; + if ( fra_w >= req_u->tot_d ) { break; } - nex_u->pac_u.pek_u.nam_u.fra_w = nex_w + i; + nex_u->pac_u.pek_u.nam_u.fra_w = nex_d + i; _mesa_add_our_to_pit(sam_u, &nex_u->pac_u.pek_u.nam_u); _mesa_send(nex_u, &lan_u); _mesa_req_pact_sent(req_u, &nex_u->pac_u.pek_u.nam_u); @@ -2200,22 +2205,22 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) c3_w siz_w = 1 << (pac_u->pag_u.nam_u.boq_y - 3); u3_assert( 1024 == siz_w ); // boq_y == 13 req_u->gag_u = gag_u; - req_u->dat_y = c3_calloc(siz_w * dat_u->tot_w); - req_u->wat_u = c3_calloc(sizeof(u3_pact_stat) * dat_u->tot_w + 2 ); - req_u->tot_w = dat_u->tot_w; - bitset_init(&req_u->was_u, dat_u->tot_w); + req_u->dat_y = c3_calloc(siz_w * dat_u->tot_d); + req_u->wat_u = c3_calloc(sizeof(u3_pact_stat) * dat_u->tot_d + 2 ); + req_u->tot_d = mesa_num_leaves(dat_u->tot_d); + bitset_init(&req_u->was_u, req_u->tot_d); // TODO: handle restart // u3_assert( nam_u->fra_w == 0 ); - c3_o lin_o = dat_u->tot_w <= 4 ? c3y : c3n; - req_u->nex_w = (c3y == lin_o) ? 1 : 0; - req_u->len_w = (c3y == lin_o) ? 1 : 0; - req_u->lef_w = 0; - req_u->old_w = 0; - req_u->ack_w = 0; + c3_o lin_o = req_u->tot_d <= 4 ? c3y : c3n; + req_u->nex_d = (c3y == lin_o) ? 1 : 0; + req_u->hav_d = (c3y == lin_o) ? 1 : 0; + req_u->lef_d = 0; + req_u->old_d = 0; + req_u->ack_d = 0; - c3_w pof_w = lss_proof_size(req_u->tot_w); + c3_w pof_w = lss_proof_size(req_u->tot_d); lss_hash* pof_u = c3_calloc(pof_w * sizeof(lss_hash)); if ( c3y == lin_o ) { if ( pof_w != (dat_u->aup_u.len_y + 1) ) { @@ -2237,7 +2242,7 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) lss_hash root; lss_root(root, pof_u, pof_w); req_u->los_u = c3_calloc(sizeof(lss_verifier)); - lss_verifier_init(req_u->los_u, 0, req_u->tot_w, pof_u); + lss_verifier_init(req_u->los_u, 0, req_u->tot_d, pof_u); c3_free(pof_u); if ( c3y == lin_o ) { @@ -2435,7 +2440,8 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) // TODO: memory management, maybe free pict and pact // if single-fragment message, inject directly into Arvo - if ( 1 == pac_u->pag_u.dat_u.tot_w ) { + c3_d lev_d = mesa_num_leaves(pac_u->pag_u.dat_u.tot_d); + if ( 1 == lev_d ) { u3_noun cad; { u3_noun lan = u3_mesa_encode_lane(lan_u); @@ -2494,25 +2500,27 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) // TODO: check return value before continuing? c3_y boq_y = u3_Host.ops_u.jum_y; - c3_o done_with_jumbo_frame = __(0 == req_u->len_w % boq_y); + c3_o done_with_jumbo_frame = __(0 == req_u->hav_d % boq_y); if ( c3y == done_with_jumbo_frame ) { u3_noun cad; { // construct jumbo frame - pac_u->pag_u.nam_u.boq_y = boq_y; - pac_u->pag_u.dat_u.tot_w = req_u->tot_w; - pac_u->pag_u.nam_u.fra_w = (req_u->len_w >> boq_y); - c3_w jumbo_len_w = (1024 * (req_u->tot_w - 1)) + pac_u->pag_u.dat_u.len_w; - pac_u->pag_u.dat_u.len_w = jumbo_len_w; - pac_u->pag_u.dat_u.fra_y = req_u->dat_y; - pac_u->pag_u.dat_u.aum_u = req_u->aum_u; - u3_noun lan = u3_mesa_encode_lane(lan_u); - - c3_y* buf_y = c3_calloc(mesa_size_pact(pac_u)); - c3_w res_w = mesa_etch_pact(buf_y, pac_u); - cad = u3nt(c3__heer, lan, u3i_bytes(res_w, buf_y)); - c3_free(buf_y); + u3_noun pac; + { + pac_u->pag_u.nam_u.boq_y = boq_y; + pac_u->pag_u.dat_u.tot_d = req_u->tot_d; + pac_u->pag_u.nam_u.fra_w = (req_u->hav_d >> boq_y); + pac_u->pag_u.dat_u.len_w += (1024 * (lev_d - 1)); + pac_u->pag_u.dat_u.fra_y = req_u->dat_y; + pac_u->pag_u.dat_u.aum_u = req_u->aum_u; + + c3_y* buf_y = c3_calloc(mesa_size_pact(pac_u)); + c3_w res_w = mesa_etch_pact(buf_y, pac_u); + pac = u3i_bytes(res_w, buf_y); + c3_free(buf_y); + } + cad = u3nt(c3__heer, lan, pac); } _mesa_del_request(sam_u, &pac_u->pag_u.nam_u); @@ -2520,15 +2528,15 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) u3_auto_plan(&sam_u->car_u, u3_ovum_init(0, c3__ames, u3nc(c3__ames, u3_nul), cad)); - u3l_log(" received last packet, tot_w: %u", req_u->tot_w); + u3l_log(" received last of %"PRIu64" total fragments", lev_d); c3_d now_d = _get_now_micros(); - u3l_log("%u kilobytes took %f ms", req_u->tot_w, (now_d - sam_u->tim_d)/1000.0); + c3_d dur_d = (now_d - sam_u->tim_d) / 1000; + u3l_log("%"PRIu64" bytes took %"PRIu64" ms", req_u->tot_d, dur_d); } - if ( req_u->len_w < req_u->tot_w ) { + if ( req_u->hav_d < lev_d ) { _mesa_request_next_fragments(sam_u, req_u, lan_u); } - } static void @@ -2663,7 +2671,7 @@ _mesa_hear_poke(u3_mesa_pict* pic_u, u3_lane* lan_u) _mesa_forward_request(sam_u, pic_u, *lan_u); return; } - + // TODO check if lane already in pit, drop dupes _mesa_add_lane_to_pit(sam_u, &pac_u->pek_u.nam_u, *lan_u); // XX if this lane management stuff is necessary @@ -2711,15 +2719,8 @@ _mesa_hear_poke(u3_mesa_pict* pic_u, u3_lane* lan_u) u3_ovum* ovo = u3_ovum_init(0, c3__ames, wir, cad); ovo = u3_auto_plan(&sam_u->car_u, ovo); - // if ( 1 == pac_u->pok_u.dat_u.tot_w ) { - // u3l_log("free poke"); - // _mesa_free_pict(pic_u); - // } - // else { - // u3l_log("inject poke"); - // XX check request state for *payload* (in-progress duplicate) - assert(pac_u->pok_u.dat_u.tot_w); + assert(pac_u->pok_u.dat_u.tot_d); u3_mesa_lane_cb_data* dat_u = c3_malloc(sizeof(u3_mesa_lane_cb_data)); { dat_u->nam_u = c3_malloc(sizeof(u3_mesa_name)); diff --git a/pkg/vere/io/mesa/mesa.h b/pkg/vere/io/mesa/mesa.h index 8961f6751e..0d22bf0b94 100644 --- a/pkg/vere/io/mesa/mesa.h +++ b/pkg/vere/io/mesa/mesa.h @@ -77,7 +77,7 @@ typedef struct _u3_auth_data { typedef struct _u3_mesa_data { // u3_mesa_data_meta met_u; - c3_w tot_w; // total fragments + c3_d tot_d; // total bytes in message u3_auth_data aum_u; struct { c3_y len_y; // number of hashes (0, 1, or 2) @@ -147,6 +147,7 @@ typedef struct _u3_mesa_pact { }; } u3_mesa_pact; +c3_d mesa_num_leaves(c3_d tot_d); c3_w mesa_size_pact(u3_mesa_pact* pac_u); c3_o mesa_sift_head(c3_y buf_y[8], u3_mesa_head* hed_u); c3_w mesa_sift_pact(u3_mesa_pact* pac_u, c3_y* buf_y, c3_w len_w); diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index f3dce48798..5cdeab890a 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -69,7 +69,8 @@ log_name(u3_mesa_name* nam_u) static void _log_data(u3_mesa_data* dat_u) { - fprintf(stderr, "tot_w: %u len_w: %u ", dat_u->tot_w, dat_u->len_w); + fprintf(stderr, "tot_d: %" PRIu64 " len_w: %u ", + dat_u->tot_d, dat_u->len_w); switch ( dat_u->aum_u.typ_e ) { case AUTH_NONE: { @@ -110,7 +111,7 @@ _log_data(u3_mesa_data* dat_u) break; } - if ( 4 < dat_u->tot_w ) { + if ( 4 < dat_u->tot_d ) { u3l_log("strange inline proof"); } else { @@ -154,14 +155,17 @@ log_pact(u3_mesa_pact* pac_u) { switch ( pac_u->hed_u.typ_y ) { case PACT_PEEK: { + fprintf(stderr, "PEEK "); _log_peek_pact(&pac_u->pek_u); } break; case PACT_PAGE: { + fprintf(stderr, "PAGE "); _log_page_pact(&pac_u->pag_u); } break; case PACT_POKE: { + fprintf(stderr, "POKE "); _log_poke_pact(&pac_u->pok_u); } break; @@ -174,6 +178,12 @@ log_pact(u3_mesa_pact* pac_u) /* Helper utilities */ +c3_d +mesa_num_leaves(c3_d tot_d) +{ + return (tot_d + 1023) / 1024; +} + void inc_hopcount(u3_mesa_head* hed_u) { @@ -193,6 +203,21 @@ _mesa_rank(u3_ship who_u) }; } +static c3_y +_mesa_size_tot(c3_d tot_d) +{ + return (tot_d <= 0xff)? 0b00 : + (tot_d <= 0xffff)? 0b01 : + (tot_d <= 0xffffffff)? 0b10 : + 0b11; +} + +static c3_y +_mesa_tot_bytes(c3_y tot_y) +{ + return 1 << tot_y; +} + /* lifecycle */ @@ -343,11 +368,11 @@ _mesa_sift_data(u3_mesa_data* dat_u, c3_y* buf_y, c3_w len_w) met_u.men_y = (met_y >> 6) & 0x3; cur_w += 1; - c3_y tot_y = met_u.bot_y + 1; + c3_y tot_y = _mesa_tot_bytes(met_u.bot_y); CHECK_BOUNDS(len_w, cur_w + tot_y); - dat_u->tot_w = 0; + dat_u->tot_d = 0; for( int i = 0; i < tot_y; i++ ) { - dat_u->tot_w |= (buf_y[cur_w] << (8*i)); + dat_u->tot_d |= (buf_y[cur_w] << (8*i)); cur_w++; } @@ -652,13 +677,9 @@ _mesa_etch_name(c3_y* buf_y, u3_mesa_name* nam_u) static c3_w _mesa_etch_data(c3_y* buf_y, u3_mesa_data* dat_u) { -#ifdef MESA_DEBUG - -#endif c3_w cur_w = 0; u3_mesa_data_meta met_u; - met_u.bot_y = safe_dec(_mesa_met3_w(dat_u->tot_w)); // XX met_u.aul_y = dat_u->aum_u.typ_e; met_u.aur_y = dat_u->aup_u.len_y; @@ -670,9 +691,10 @@ _mesa_etch_data(c3_y* buf_y, u3_mesa_data* dat_u) ^ (met_u.men_y & 0x3) << 6; buf_y[cur_w] = met_y; cur_w++; - c3_y tot_y = met_u.bot_y + 1; + + c3_y tot_y = _mesa_size_tot(dat_u->tot_d); for (int i = 0; i < tot_y; i++ ) { - buf_y[cur_w] = (dat_u->tot_w >> (8 * i)) & 0xFF; + buf_y[cur_w] = (dat_u->tot_d >> (8 * i)) & 0xFF; cur_w++; } switch ( dat_u->aum_u.typ_e ) { @@ -814,9 +836,9 @@ _mesa_size_data(u3_mesa_data* dat_u) c3_w siz_w = 1; u3_mesa_data_meta met_u; - met_u.bot_y = safe_dec(_mesa_met3_w(dat_u->tot_w)); + met_u.bot_y = _mesa_size_tot(dat_u->tot_d); - siz_w += met_u.bot_y + 1; + siz_w += _mesa_tot_bytes(met_u.bot_y); switch ( dat_u->aum_u.typ_e ) { case AUTH_SIGN: { @@ -939,6 +961,7 @@ mesa_etch_pact(c3_y* buf_y, u3_mesa_pact* pac_u) cur_w += nex_w; + u3l_log("siz_w %u cur_w %u", siz_w, cur_w); assert( siz_w == cur_w ); return cur_w; From 17c5ccfed78b9cf65d0e184931b7144163a5aefa Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Mon, 5 Aug 2024 19:26:01 -0400 Subject: [PATCH 331/430] mesa: 64-bit fragment number --- pkg/vere/io/mesa.c | 104 ++++++++++++++++++++-------------------- pkg/vere/io/mesa/mesa.h | 2 +- pkg/vere/io/mesa/pact.c | 59 ++++++++++------------- 3 files changed, 79 insertions(+), 86 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index a4d2902935..0c689a05b0 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -316,12 +316,12 @@ _log_mesa_data(u3_mesa_data dat_u) // u3l_log("frag: %xxx", dat_u.fra_y); } -/* _mesa_lop(): find beginning of page containing fra_w +/* _mesa_lop(): find beginning of page containing fra_d */ -static inline c3_w -_mesa_lop(c3_w fra_w) +static inline c3_d +_mesa_lop(c3_d fra_d) { - return 0; + return fra_d & ~((1 << u3_Host.ops_u.jum_y) - 1); } static c3_d @@ -771,6 +771,7 @@ _mesa_put_lane(u3_mesa* sam_u, u3_ship her_u, u3_lane* lan_u, u3_gage* gag_u) if ( old_u == NULL ) { new_u = u3a_calloc(sizeof(u3_gage),1); memcpy(new_u, gag_u, sizeof(u3_gage)); + } else { new_u = old_u; memcpy(new_u, gag_u, sizeof(u3_gage)); @@ -834,8 +835,8 @@ _mesa_req_pact_resent(u3_pend_req* req_u, u3_mesa_name* nam_u) return; } - req_u->wat_u[nam_u->fra_w].sen_d = now_d; - req_u->wat_u[nam_u->fra_w].tie_y++; + req_u->wat_u[nam_u->fra_d].sen_d = now_d; + req_u->wat_u[nam_u->fra_d].tie_y++; } /* _mesa_req_pact_sent(): mark packet as sent @@ -847,19 +848,19 @@ _mesa_req_pact_sent(u3_pend_req* req_u, u3_mesa_name* nam_u) c3_d now_d = _get_now_micros(); // if we already have pending request if ( NULL != req_u ) { - if( req_u->nex_d == nam_u->fra_w ) { + if( req_u->nex_d == nam_u->fra_d ) { req_u->nex_d++; } // TODO: optional assertions? /* req_u->wat_u[nam_u->fra_w] = (u3_pact_stat){now_d, 0, 1, 0 }; */ - req_u->wat_u[nam_u->fra_w].sen_d = now_d; - req_u->wat_u[nam_u->fra_w].sip_y = 0; - req_u->wat_u[nam_u->fra_w].tie_y = 1; + req_u->wat_u[nam_u->fra_d].sen_d = now_d; + req_u->wat_u[nam_u->fra_d].sip_y = 0; + req_u->wat_u[nam_u->fra_d].tie_y = 1; #ifdef MESA_DEBUG - u3l_log("bitset_put %u", nam_u->fra_w); + u3l_log("bitset_put %"PRIu64, nam_u->fra_d); #endif - bitset_put(&req_u->was_u, nam_u->fra_w); + bitset_put(&req_u->was_u, nam_u->fra_d); } else { u3l_log("mesa: no req for sent"); return; @@ -1110,7 +1111,7 @@ _try_resend(u3_pend_req* req_u, c3_d ack_d) (now_d - req_u->wat_u[i_d].sen_d > req_u->gag_u->rto_w) ) { los_o = c3y; - pac_u->pek_u.nam_u.fra_w = i_d; + pac_u->pek_u.nam_u.fra_d = i_d; c3_w siz_w = mesa_etch_pact(buf_y, pac_u); if ( 0 == siz_w ) { u3_assert(!"failed to etch"); @@ -1213,37 +1214,37 @@ _mesa_req_pact_done(u3_pend_req* req_u, u3_mesa* sam_u = req_u->per_u->sam_u; // needed for the MESA_LOG macro // received past the end of the message - if ( dat_u->tot_d <= nam_u->fra_w ) { - u3l_log("strange tot_d %"PRIu64" fra_w %u req_u %"PRIu64, - dat_u->tot_d, nam_u->fra_w, req_u->hav_d); + if ( dat_u->tot_d <= nam_u->fra_d ) { + u3l_log("strange tot_d %"PRIu64" fra_d %"PRIu64" req_u %"PRIu64, + dat_u->tot_d, nam_u->fra_d, req_u->hav_d); MESA_LOG(sam_u, STRANGE); // XX: is this sufficient to drop whole request return; } // received duplicate - if ( c3n == bitset_has(&req_u->was_u, nam_u->fra_w) ) { + if ( c3n == bitset_has(&req_u->was_u, nam_u->fra_d) ) { MESA_LOG(sam_u, DUPE); return; } - bitset_del(&req_u->was_u, nam_u->fra_w); - if ( nam_u->fra_w > req_u->ack_d ) { - req_u->ack_d = nam_u->fra_w; + bitset_del(&req_u->was_u, nam_u->fra_d); + if ( nam_u->fra_d > req_u->ack_d ) { + req_u->ack_d = nam_u->fra_d; } #ifdef MESA_DEBUG - if ( nam_u->fra_w != 0 && req_u->wat_u[nam_u->fra_w].tie_y != 1 ) { - u3l_log("received retry %u", nam_u->fra_w); + if ( nam_u->fra_d != 0 && req_u->wat_u[nam_u->fra_d].tie_y != 1 ) { + u3l_log("received retry %"PRIu64, nam_u->fra_d); } #endif req_u->hav_d++; #ifdef MESA_DEBUG - u3l_log("fragment %u len %"PRIu64, nam_u->fra_w, req_u->hav_d); + u3l_log("fragment %"PRIu64" len %"PRIu64, nam_u->fra_d, req_u->hav_d); #endif - if ( req_u->lef_d == nam_u->fra_w ) { + if ( req_u->lef_d == nam_u->fra_d ) { req_u->lef_d++; } @@ -1255,14 +1256,14 @@ _mesa_req_pact_done(u3_pend_req* req_u, memcpy((*par_u)[1], dat_u->aup_u.has_y[1], sizeof(lss_hash)); } - if ( req_u->los_u->counter != nam_u->fra_w ) { - if ( nam_u->fra_w < req_u->los_u->counter ) { - u3l_log("fragment number too low (%u)", nam_u->fra_w); - } else if ( nam_u->fra_w >= req_u->los_u->counter + (sizeof(req_u->mis_u)/sizeof(u3_misord_buf)) ) { - u3l_log("fragment number too high (%u)", nam_u->fra_w); + if ( req_u->los_u->counter != nam_u->fra_d ) { + if ( nam_u->fra_d < req_u->los_u->counter ) { + u3l_log("fragment number too low: %"PRIu64, nam_u->fra_d); + } else if ( nam_u->fra_d >= req_u->los_u->counter + (sizeof(req_u->mis_u)/sizeof(u3_misord_buf)) ) { + u3l_log("fragment number too high: %"PRIu64, nam_u->fra_d); } else { // insert into misordered queue - u3_misord_buf* buf_u = &req_u->mis_u[nam_u->fra_w - req_u->los_u->counter - 1]; + u3_misord_buf* buf_u = &req_u->mis_u[nam_u->fra_d - req_u->los_u->counter - 1]; buf_u->fra_y = c3_calloc(dat_u->len_w); buf_u->len_w = dat_u->len_w; memcpy(buf_u->fra_y, dat_u->fra_y, dat_u->len_w); @@ -1272,7 +1273,7 @@ _mesa_req_pact_done(u3_pend_req* req_u, else if ( c3y != lss_verifier_ingest(req_u->los_u, dat_u->fra_y, dat_u->len_w, par_u) ) { c3_free(par_u); // TODO: do we drop the whole request on the floor? - u3l_log("auth fail frag %u", nam_u->fra_w); + u3l_log("auth fail frag %"PRIu64, nam_u->fra_d); MESA_LOG(sam_u, AUTH); return; } @@ -1297,12 +1298,12 @@ _mesa_req_pact_done(u3_pend_req* req_u, sat_u->her_d = _get_now_micros(); // handle gauge update - _mesa_handle_ack(req_u->gag_u, &req_u->wat_u[nam_u->fra_w]); + _mesa_handle_ack(req_u->gag_u, &req_u->wat_u[nam_u->fra_d]); c3_w siz_w = (1 << (nam_u->boq_y - 3)); - memcpy(req_u->dat_y + (siz_w * nam_u->fra_w), dat_u->fra_y, dat_u->len_w); + memcpy(req_u->dat_y + (siz_w * nam_u->fra_d), dat_u->fra_y, dat_u->len_w); - _try_resend(req_u, nam_u->fra_w); + _try_resend(req_u, nam_u->fra_d); _update_resend_timer(req_u); } @@ -1508,7 +1509,7 @@ _name_to_scry(u3_mesa_name* nam_u) { u3_noun rif = _dire_etch_ud(nam_u->rif_w); u3_noun boq = _dire_etch_ud(nam_u->boq_y); - u3_noun fag = _dire_etch_ud(nam_u->fra_w); + u3_noun fag = _dire_etch_ud(nam_u->fra_d); u3_noun pax = _mesa_encode_path(nam_u->pat_s, (c3_y*)nam_u->pat_c); u3_noun wer = nam_u->nit_o == c3y @@ -1923,7 +1924,7 @@ _mesa_send_jumbo_pieces(u3_mesa* sam_u, u3_mesa_line* lin_u) { _mesa_copy_name(nam_u, &lin_u->nam_u); nam_u->boq_y = 13; - nam_u->fra_w *= (1 << u3_Host.ops_u.jum_y); + nam_u->fra_d *= (1 << u3_Host.ops_u.jum_y); } u3_mesa_data* dat_u = &pac_u.pag_u.dat_u; @@ -1952,14 +1953,14 @@ _mesa_send_jumbo_pieces(u3_mesa* sam_u, u3_mesa_line* lin_u) } // send leaf packets - c3_w lev_w = mesa_num_leaves(lin_u->dat_w); - c3_w fir_w = nam_u->fra_w; + c3_d lev_d = mesa_num_leaves(lin_u->dat_w); + c3_d fir_d = nam_u->fra_d; c3_y* pro_y = lin_u->tip_y; - for (c3_w i_w = 0; i_w < lev_w; i_w++) { - c3_w fra_w = i_w + fir_w; - nam_u->nit_o = __((fra_w == 0) && (lev_w <= 4)); - nam_u->fra_w = fra_w; - c3_w cur_w = i_w * 1024; + for (c3_d i_d = 0; i_d < lev_d; i_d++) { + c3_d fra_d = i_d + fir_d; + nam_u->nit_o = __((fra_d == 0) && (lev_d <= 4)); + nam_u->fra_d = fra_d; + c3_w cur_w = i_d * 1024; dat_u->fra_y = lin_u->dat_y + cur_w; dat_u->len_w = c3_min(lin_u->dat_w - cur_w, 1024); @@ -1970,7 +1971,7 @@ _mesa_send_jumbo_pieces(u3_mesa* sam_u, u3_mesa_line* lin_u) dat_u->aup_u.len_y = pro_w; memcpy(dat_u->aup_u.has_y, pro_y, pro_w * sizeof(lss_hash)); } else { - lss_pair* pair = ((lss_pair*)lin_u->haz_y) + i_w; + lss_pair* pair = ((lss_pair*)lin_u->haz_y) + i_d; if ( 0 == memcmp(pair, &(lss_pair){0}, sizeof(lss_pair)) ) { dat_u->aup_u.len_y = 0; dat_u->aum_u.typ_e = AUTH_NONE; @@ -1984,7 +1985,7 @@ _mesa_send_jumbo_pieces(u3_mesa* sam_u, u3_mesa_line* lin_u) u3_weak pin = _mesa_get_pit(sam_u, nam_u); if ( u3_none != pin) { #ifdef MESA_DEBUG - u3l_log(" sending leaf packet, fra_w: %u", nam_u->fra_w); + u3l_log(" sending leaf packet, fra_d: %"PRIu64, nam_u->fra_d); #endif log_pact(&pac_u); _mesa_send_pact(sam_u, u3k(u3t(pin)), NULL, &pac_u); @@ -2140,7 +2141,7 @@ _mesa_request_next_fragments(u3_mesa* sam_u, if ( fra_w >= req_u->tot_d ) { break; } - nex_u->pac_u.pek_u.nam_u.fra_w = nex_d + i; + nex_u->pac_u.pek_u.nam_u.fra_d = nex_d + i; _mesa_add_our_to_pit(sam_u, &nex_u->pac_u.pek_u.nam_u); _mesa_send(nex_u, &lan_u); _mesa_req_pact_sent(req_u, &nex_u->pac_u.pek_u.nam_u); @@ -2510,7 +2511,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) { pac_u->pag_u.nam_u.boq_y = boq_y; pac_u->pag_u.dat_u.tot_d = req_u->tot_d; - pac_u->pag_u.nam_u.fra_w = (req_u->hav_d >> boq_y); + pac_u->pag_u.nam_u.fra_d = (req_u->hav_d >> boq_y); pac_u->pag_u.dat_u.len_w += (1024 * (lev_d - 1)); pac_u->pag_u.dat_u.fra_y = req_u->dat_y; pac_u->pag_u.dat_u.aum_u = req_u->aum_u; @@ -2591,13 +2592,13 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) // record interest _mesa_add_lane_to_pit(sam_u, &pac_u->pek_u.nam_u, lan_u); - c3_w fra_w = pac_u->pek_u.nam_u.fra_w; - c3_w bat_w = _mesa_lop(fra_w); + c3_d fra_d = pac_u->pek_u.nam_u.fra_d; + c3_d bat_d = _mesa_lop(fra_d); - pac_u->pek_u.nam_u.fra_w = bat_w; + pac_u->pek_u.nam_u.fra_d = bat_d; // XX HACK: shouldn't be necessary to change data 0 to init, but // for some reason it's changing the data returned by the scry - if ( pac_u->pek_u.nam_u.fra_w == 0 ) { + if ( pac_u->pek_u.nam_u.fra_d == 0 ) { pac_u->pek_u.nam_u.nit_o = c3y; } @@ -2605,6 +2606,7 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) u3_mesa_line* lin_u = _mesa_get_jumbo_cache(sam_u, &pac_u->pek_u.nam_u); if ( NULL != lin_u ) { if ( CTAG_ITEM == lin_u->typ_y ) { + // TODO only respond to this fragment, don't iterate _mesa_send_jumbo_pieces(sam_u, lin_u); _mesa_free_pict(pic_u); } diff --git a/pkg/vere/io/mesa/mesa.h b/pkg/vere/io/mesa/mesa.h index 0d22bf0b94..18b11f2774 100644 --- a/pkg/vere/io/mesa/mesa.h +++ b/pkg/vere/io/mesa/mesa.h @@ -48,7 +48,7 @@ typedef struct _u3_mesa_name { c3_y boq_y; c3_o nit_o; c3_o aut_o; - c3_w fra_w; + c3_d fra_d; c3_s pat_s; c3_c* pat_c; } u3_mesa_name; diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index 5cdeab890a..2260a6e347 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -56,12 +56,12 @@ log_name(u3_mesa_name* nam_u) } u3l_log("%s: /%s", her_c, nam_u->pat_c); - u3l_log(" rift: %u bloq: %u init: %s auth: %s frag: %u", + u3l_log(" rift: %u bloq: %u init: %s auth: %s frag: %"PRIu64, nam_u->rif_w, nam_u->boq_y, (c3y == nam_u->nit_o) ? "&" : "|", (c3y == nam_u->aut_o) ? "&" : "|", - nam_u->fra_w + nam_u->fra_d ); c3_free(her_c); } @@ -325,13 +325,13 @@ _mesa_sift_name(u3_mesa_name* nam_u, c3_y* buf_y, c3_w len_w) if ( met_u.nit_y ) { assert( !met_u.tau_y ); // XX init packet - nam_u->fra_w = 0; + nam_u->fra_d = 0; } else { c3_y fag_y = met_u.gaf_y + 1; CHECK_BOUNDS(len_w, cur_w + fag_y); for ( int i = 0; i < fag_y; i++ ) { - nam_u->fra_w |= (buf_y[cur_w] << (8*i)); + nam_u->fra_d |= (buf_y[cur_w] << (8*i)); cur_w++; } } @@ -609,9 +609,6 @@ _mesa_etch_head(u3_mesa_head* hed_u, c3_y buf_y[8]) static c3_w _mesa_etch_name(c3_y* buf_y, u3_mesa_name* nam_u) { -#ifdef MESA_DEBUG - -#endif c3_w cur_w = 0; u3_mesa_name_meta met_u; @@ -627,7 +624,7 @@ _mesa_etch_name(c3_y* buf_y, u3_mesa_name* nam_u) else { met_u.nit_y = 0; met_u.tau_y = (c3y == nam_u->aut_o) ? 1 : 0; - met_u.gaf_y = safe_dec(_mesa_met3_w(nam_u->fra_w)); + met_u.gaf_y = _mesa_size_tot(nam_u->fra_d); } c3_y met_y = (met_u.ran_y & 0x3) << 0 @@ -656,7 +653,7 @@ _mesa_etch_name(c3_y* buf_y, u3_mesa_name* nam_u) c3_y fra_y = (c3y == nam_u->nit_o) ? 0 : met_u.gaf_y + 1; for( int i = 0; i < fra_y; i++ ) { - buf_y[cur_w] = (nam_u->fra_w >> (8*i)) & 0xff; + buf_y[cur_w] = (nam_u->fra_d >> (8*i)) & 0xff; cur_w++; } @@ -731,7 +728,6 @@ _mesa_etch_data(c3_y* buf_y, u3_mesa_data* dat_u) static c3_w _mesa_etch_next(c3_y* buf_y, u3_mesa_hop_type nex_y, u3_mesa_page_pact* pac_u) { - switch ( nex_y ) { case HOP_SHORT: { memcpy(buf_y, pac_u->sot_u, 6); @@ -754,7 +750,6 @@ _mesa_etch_next(c3_y* buf_y, u3_mesa_hop_type nex_y, u3_mesa_page_pact* pac_u) return 0; } break; } - } static c3_w @@ -820,8 +815,8 @@ _mesa_size_name(u3_mesa_name* nam_u) siz_w++; // bloq if (c3n == nam_u->nit_o ) { - met_u.gaf_y = safe_dec(_mesa_met3_w(nam_u->fra_w)); - siz_w += met_u.gaf_y + 1; + met_u.gaf_y = _mesa_size_tot(nam_u->fra_d); + siz_w += _mesa_tot_bytes(met_u.gaf_y); } siz_w += 2; // path-length @@ -923,36 +918,29 @@ mesa_size_pact(u3_mesa_pact* pac_u) c3_w mesa_etch_pact(c3_y* buf_y, u3_mesa_pact* pac_u) { - c3_w siz_w = mesa_size_pact(pac_u); - + c3_w cur_w = 8; // space for header + cookie + c3_w nex_w = 0; u3_mesa_head* hed_u = &pac_u->hed_u; - - c3_w nex_w, cur_w = 8; // space for header + cookie - - switch ( pac_u->hed_u.typ_y ) { + switch ( hed_u->typ_y ) { case PACT_POKE: { - if ( !(nex_w = _mesa_etch_poke_pact(buf_y + cur_w, &pac_u->pok_u, hed_u)) ) { - return 0; - } + nex_w = _mesa_etch_poke_pact(buf_y + cur_w, &pac_u->pok_u, hed_u); } break; case PACT_PEEK: { - if ( !(nex_w = _mesa_etch_name(buf_y + cur_w, &pac_u->pek_u.nam_u)) ) { - return 0; - } + nex_w = _mesa_etch_name(buf_y + cur_w, &pac_u->pek_u.nam_u); } break; case PACT_PAGE: { - if ( !(nex_w = _mesa_etch_page_pact(buf_y + cur_w, &pac_u->pag_u, hed_u)) ) { - return 0; - } + nex_w = _mesa_etch_page_pact(buf_y + cur_w, &pac_u->pag_u, hed_u); } break; default: { - u3l_log("bad pact type %u", pac_u->hed_u.typ_y);//u3m_bail(c3__bail); - return 0; + u3l_log("bad pact type %u", pac_u->hed_u.typ_y); } } + if ( 0 == nex_w ) { + return 0; + } hed_u->mug_w = u3r_mug_bytes(buf_y + cur_w, nex_w); hed_u->mug_w &= 0xFFFFF; @@ -961,10 +949,13 @@ mesa_etch_pact(c3_y* buf_y, u3_mesa_pact* pac_u) cur_w += nex_w; - u3l_log("siz_w %u cur_w %u", siz_w, cur_w); - assert( siz_w == cur_w ); - - return cur_w; + if ( mesa_size_pact(pac_u) == cur_w ) { + return cur_w; + } + else { + u3l_log("mesa: size %u cur_w %u", mesa_size_pact(pac_u), cur_w); + return 0; + } } #ifdef PACT_TEST From da63bc9a1e20d7eeba5e1602bf9112168697b571 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Tue, 6 Aug 2024 12:55:41 +0200 Subject: [PATCH 332/430] mesa: fix merge conflicts --- pkg/vere/io/mesa.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 70fdaba89e..044ad6dcc7 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -2526,14 +2526,14 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) // TODO: check return value before continuing? c3_y boq_y = u3_Host.ops_u.jum_y; - c3_o done_with_jumbo_frame = __(0 == req_u->len_w % boq_y); + c3_o done_with_jumbo_frame = __(0 == req_u->hav_d % boq_y); _mesa_del_pit(sam_u, nam_u); if ( c3y == done_with_jumbo_frame ) { u3_noun cad; - u3l_log(" received last packet, tot_w: %u", req_u->tot_w); + u3l_log(" received last packet, tot_d: %llu", req_u->tot_d); c3_d now_d = _get_now_micros(); - u3l_log("%u kilobytes took %f ms", req_u->tot_w, (now_d - sam_u->tim_d)/1000.0); + u3l_log("%llu kilobytes took %f ms", req_u->tot_d, (now_d - sam_u->tim_d)/1000.0); { // construct jumbo frame From 512e6148f2fea05b6c3a9550245cecf6b99d49d8 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Tue, 6 Aug 2024 16:08:04 +0200 Subject: [PATCH 333/430] mesa: fix missing met_u.bot_y initialization --- pkg/vere/io/mesa/pact.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index 2260a6e347..9570c14c0e 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -682,14 +682,16 @@ _mesa_etch_data(c3_y* buf_y, u3_mesa_data* dat_u) met_u.aur_y = dat_u->aup_u.len_y; c3_y nel_y = _mesa_met3_w(dat_u->len_w); met_u.men_y = (3 >= nel_y) ? nel_y : 3; + met_u.bot_y = _mesa_size_tot(dat_u->tot_d); + c3_y met_y = (met_u.bot_y & 0x3) << 0 ^ (met_u.aul_y & 0x3) << 2 ^ (met_u.aur_y & 0x3) << 4 ^ (met_u.men_y & 0x3) << 6; buf_y[cur_w] = met_y; cur_w++; + c3_y tot_y = _mesa_tot_bytes(met_u.bot_y); - c3_y tot_y = _mesa_size_tot(dat_u->tot_d); for (int i = 0; i < tot_y; i++ ) { buf_y[cur_w] = (dat_u->tot_d >> (8 * i)) & 0xFF; cur_w++; From bee8af3c7adc0b431d9f8fd9a7990421d806b6fc Mon Sep 17 00:00:00 2001 From: midden-fabler Date: Tue, 6 Aug 2024 10:53:58 -0400 Subject: [PATCH 334/430] http: fix bail meme --- pkg/vere/io/http.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/vere/io/http.c b/pkg/vere/io/http.c index 0584930e36..9f5b937511 100644 --- a/pkg/vere/io/http.c +++ b/pkg/vere/io/http.c @@ -1021,7 +1021,7 @@ _http_cache_respond(u3_hreq* req_u, u3_noun nun) } else { u3_noun auth, response_header, data; - u3x_qual(u3k(u3t(u3t(nun))), &auth, 0, &response_header, &data); + u3x_qual(u3t(u3t(nun)), &auth, 0, &response_header, &data); u3_noun status, headers; u3x_cell(response_header, &status, &headers); @@ -1066,7 +1066,7 @@ _http_scry_respond(u3_hreq* req_u, u3_noun nun) } else { u3_noun auth, response_header, data; - u3x_qual(u3k(u3t(u3t(nun))), &auth, 0, &response_header, &data); + u3x_qual(u3t(u3t(nun)), &auth, 0, &response_header, &data); u3_noun status, headers; u3x_cell(response_header, &status, &headers); From 5c83b9b23d09e9ded29a10fbc8285ab723a3af22 Mon Sep 17 00:00:00 2001 From: lukechampine Date: Mon, 5 Aug 2024 18:50:05 -0400 Subject: [PATCH 335/430] jets: update for urcrypt blake3 changes --- WORKSPACE.bazel | 4 ++-- pkg/noun/jets/e/blake.c | 19 ++++++++++--------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index 67da72413c..1a73ad4f18 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -354,10 +354,10 @@ versioned_http_file( versioned_http_archive( name = "urcrypt", build_file = "//bazel/third_party/urcrypt:urcrypt.BUILD", - sha256 = "3c66dd6acdb4703c68f24958c820e79b6a56285ea5d0ba769f60eafe3f0cc0db", + sha256 = "cfd4d6c510cef168489c9a162c319d160a20698f51b9f84c3644763f3a19e50e", strip_prefix = "urcrypt-{version}", url = "https://github.com/urbit/urcrypt/archive/{version}.tar.gz", - version = "a17279dcbf38d812c7019a3e71d25bdfdb8842ef", + version = "e77a0998809f7552c455cf7bf8c1b68c141cce18", ) versioned_http_archive( diff --git a/pkg/noun/jets/e/blake.c b/pkg/noun/jets/e/blake.c index f21f229ceb..cac4c481ca 100644 --- a/pkg/noun/jets/e/blake.c +++ b/pkg/noun/jets/e/blake.c @@ -59,20 +59,21 @@ static u3_atom _cqe_blake3_hash(u3_atom wid, u3_atom dat, - u3_atom key, u3_atom out) + u3_atom key, u3_atom flags, u3_atom out) { c3_w wid_w, out_w; if ( !u3r_word_fit(&wid_w, wid) || !u3r_word_fit(&out_w, out) ) { return u3m_bail(c3__fail); } else { - c3_y key_y[32]; + c3_y key_y[32]; u3r_bytes(0, 32, key_y, key); + c3_y flags_y = u3r_byte(0, flags); c3_y *dat_y = u3r_bytes_alloc(0, wid_w, dat); u3i_slab sab_u; u3i_slab_bare(&sab_u, 3, out_w); c3_y* out_y = sab_u.buf_y; - urcrypt_blake3_hash(wid_w, dat_y, key_y, out, out_y); + urcrypt_blake3_hash(wid_w, dat_y, key_y, flags_y, out, out_y); u3a_free(dat_y); return u3i_slab_mint(&sab_u); } @@ -81,20 +82,20 @@ u3_noun u3we_blake3_hash(u3_noun cor) { - u3_noun out, msg, // arguments - wid, dat, // destructured msg - key; // context + u3_noun out, msg, // arguments + wid, dat, // destructured msg + sam, key, flags; // context if ( c3n == u3r_mean(cor, u3x_sam_2, &out, u3x_sam_3, &msg, - u3x_con_sam_2, &key, 0) || + u3x_con_sam, &sam, 0) || u3ud(out) || u3r_cell(msg, &wid, &dat) || u3ud(wid) || u3ud(dat) || - u3ud(key)) + u3r_cell(sam, &key, &flags) || u3ud(key) || u3ud(flags) ) { return u3m_bail(c3__exit); } else { - return u3l_punt("blake3_hash", _cqe_blake3_hash(wid, dat, key, out)); + return u3l_punt("blake3_hash", _cqe_blake3_hash(wid, dat, key, flags, out)); } } From 269a1015b6a279c617f68f427a20097bf5104bca Mon Sep 17 00:00:00 2001 From: lukechampine Date: Tue, 6 Aug 2024 00:13:17 -0400 Subject: [PATCH 336/430] mesa: outline WIP --- pkg/vere/io/mesa.c | 110 +++++++-------------- pkg/vere/io/mesa/mesa.h | 29 +++--- pkg/vere/io/mesa/pact.c | 211 +++++++++++++++++----------------------- 3 files changed, 138 insertions(+), 212 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 044ad6dcc7..e8aba5329a 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -170,7 +170,7 @@ typedef struct _u3_pend_req { u3_peer* per_u; // backpointer c3_d nex_d; // number of the next fragment to be sent c3_d tot_d; // total number of expected bytes - u3_auth_data aum_u; // message authenticator + u3_auth_data aut_u; // message authenticator uv_timer_t tim_u; // timehandler c3_y* dat_y; // ((mop @ud *) lte) c3_d hav_d; // how many fragments we've received @@ -198,7 +198,7 @@ typedef enum _u3_mesa_ctag { typedef struct _u3_mesa_line { u3_mesa_ctag typ_y; // pending or present? u3_mesa_name nam_u; // full name for data, ready to serialize - u3_auth_data aum_u; // message authenticator, none if not initial frame + u3_auth_data aut_u; // message authenticator c3_w tot_d; // number of bytes in whole message c3_w dat_w; // size in bytes of dat_y c3_w len_w; // total allocated size, in bytes @@ -1255,11 +1255,11 @@ _mesa_req_pact_done(u3_pend_req* req_u, } lss_pair* par_u = NULL; - if ( dat_u->aum_u.typ_e == AUTH_NEXT ) { + if ( dat_u->aut_u.typ_e == AUTH_PAIR ) { // needs to be heap allocated bc will be saved if misordered par_u = c3_calloc(sizeof(lss_pair)); - memcpy((*par_u)[0], dat_u->aup_u.has_y[0], sizeof(lss_hash)); - memcpy((*par_u)[1], dat_u->aup_u.has_y[1], sizeof(lss_hash)); + memcpy((*par_u)[0], dat_u->aut_u.has_y[0], sizeof(lss_hash)); + memcpy((*par_u)[1], dat_u->aut_u.has_y[1], sizeof(lss_hash)); } if ( req_u->los_u->counter != nam_u->fra_d ) { @@ -1519,9 +1519,7 @@ _name_to_scry(u3_mesa_name* nam_u) u3_noun fag = _dire_etch_ud(nam_u->fra_d); u3_noun pax = _mesa_encode_path(nam_u->pat_s, (c3_y*)nam_u->pat_c); - u3_noun wer = nam_u->nit_o == c3y - ? u3nc(c3__init, pax) - : u3nt(nam_u->aut_o == c3y ? c3__auth : c3__data, fag, pax); + u3_noun wer = u3nt(nam_u->aut_o == c3y ? c3__auth : c3__data, fag, pax); u3_noun res = u3nc(c3__mess, u3nq(rif, c3__pact, boq, u3nc(c3__etch, wer))); @@ -1868,13 +1866,11 @@ static u3_noun _name_to_jumbo_scry(u3_mesa_name* nam_u) { u3_noun rif = _dire_etch_ud(nam_u->rif_w); - u3_noun boq = _dire_etch_ud(31); + u3_noun boq = _dire_etch_ud(31); // XX make configurable u3_noun fag = _dire_etch_ud(0); // XX 1 u3_noun pax = _mesa_encode_path(nam_u->pat_s, (c3_y*)nam_u->pat_c); - - u3_noun wer = nam_u->nit_o == c3y - ? u3nc(c3__init, pax) - : u3nt(nam_u->aut_o == c3y ? c3__auth : c3__data, fag, pax); + // XX TODO: scry for %auth when necessary + u3_noun wer = u3nt(c3__data, fag, pax); u3_noun res = u3nc(c3__mess, u3nq(rif, c3__pact, boq, u3nc(c3__etch, wer))); @@ -1947,13 +1943,13 @@ _mesa_send_jumbo_pieces(u3_mesa* sam_u, u3_mesa_line* lin_u) u3_mesa_data* dat_u = &pac_u.pag_u.dat_u; { dat_u->tot_d = lin_u->tot_d; - dat_u->aum_u = lin_u->aum_u; - // aup_u, len_w, and fra_y vary by fragment + dat_u->aut_u = lin_u->aut_u; + // aut_u, len_w, and fra_y vary by fragment } c3_d mev_d = mesa_num_leaves(dat_u->tot_d); c3_w pro_w = lss_proof_size(mev_d); - if ( c3y == nam_u->nit_o && mev_d > 4) { + if ( pro_w > 0 ) { u3_weak pin = _mesa_get_pit(sam_u, nam_u); if ( u3_none != pin ) { #ifdef MESA_DEBUG @@ -1970,35 +1966,23 @@ _mesa_send_jumbo_pieces(u3_mesa* sam_u, u3_mesa_line* lin_u) } } - // send leaf packets + // send leaf packet(s) c3_d lev_d = mesa_num_leaves(lin_u->dat_w); c3_d fir_d = nam_u->fra_d; c3_y* pro_y = lin_u->tip_y; for (c3_d i_d = 0; i_d < lev_d; i_d++) { c3_d fra_d = i_d + fir_d; - nam_u->nit_o = __((fra_d == 0) && (lev_d <= 4)); nam_u->fra_d = fra_d; c3_w cur_w = i_d * 1024; dat_u->fra_y = lin_u->dat_y + cur_w; dat_u->len_w = c3_min(lin_u->dat_w - cur_w, 1024); - if ( c3y == nam_u->nit_o ) { - // inline proof; omit first leaf - pro_w--; - pro_y++; - dat_u->aup_u.len_y = pro_w; - memcpy(dat_u->aup_u.has_y, pro_y, pro_w * sizeof(lss_hash)); + lss_pair* pair = ((lss_pair*)lin_u->haz_y) + i_d; + if ( 0 == memcmp(pair, &(lss_pair){0}, sizeof(lss_pair)) ) { + dat_u->aut_u.typ_e = AUTH_NONE; } else { - lss_pair* pair = ((lss_pair*)lin_u->haz_y) + i_d; - if ( 0 == memcmp(pair, &(lss_pair){0}, sizeof(lss_pair)) ) { - dat_u->aup_u.len_y = 0; - dat_u->aum_u.typ_e = AUTH_NONE; - } else { - dat_u->aup_u.len_y = 2; - dat_u->aum_u.typ_e = AUTH_NEXT; - memcpy(dat_u->aup_u.has_y[0], (*pair)[0], sizeof(lss_hash)); - memcpy(dat_u->aup_u.has_y[1], (*pair)[1], sizeof(lss_hash)); - } + dat_u->aut_u.typ_e = AUTH_PAIR; + memcpy(dat_u->aut_u.has_y, pair, sizeof(lss_pair)); } u3_weak pin = _mesa_get_pit(sam_u, nam_u); if ( u3_none != pin) { @@ -2062,7 +2046,7 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun res) c3_d mev_d = (dat_u->tot_d + 1023) / 1024; // leaves in message c3_w tip_w = // bytes in Merkle spine - (c3n == jum_u.pag_u.nam_u.nit_o)? + (0 == jum_u.pag_u.nam_u.fra_d)? 0 : lss_proof_size(mev_d); c3_w dat_w = dat_u->len_w; // bytes in fragment data in this jumbo frame @@ -2073,7 +2057,7 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun res) lin_u = u3a_malloc(sizeof(u3_mesa_line)); lin_u->typ_y = CTAG_ITEM; _mesa_copy_name(&lin_u->nam_u, &jum_u.pek_u.nam_u); - lin_u->aum_u = dat_u->aum_u; + lin_u->aut_u = dat_u->aut_u; lin_u->tot_d = dat_u->tot_d; lin_u->dat_w = dat_w; lin_u->len_w = len_w; @@ -2220,8 +2204,7 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) req_u->pic_u->pac_u.hed_u.pro_y = MESA_VER; memcpy(&req_u->pic_u->pac_u.pek_u.nam_u, nam_u, sizeof(u3_mesa_name)); // XX req_u->pic_u->pac_u.pek_u.nam_u.aut_o = c3n; - req_u->pic_u->pac_u.pek_u.nam_u.nit_o = c3n; - req_u->aum_u = dat_u->aum_u; + req_u->aut_u = dat_u->aut_u; c3_w siz_w = 1 << (pac_u->pag_u.nam_u.boq_y - 3); u3_assert( 1024 == siz_w ); // boq_y == 13 @@ -2234,31 +2217,19 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) // TODO: handle restart // u3_assert( nam_u->fra_w == 0 ); - c3_o lin_o = req_u->tot_d <= 4 ? c3y : c3n; - req_u->nex_d = (c3y == lin_o) ? 1 : 0; - req_u->hav_d = (c3y == lin_o) ? 1 : 0; + req_u->nex_d = 0; + req_u->hav_d = 0; req_u->lef_d = 0; req_u->old_d = 0; req_u->ack_d = 0; c3_w pof_w = lss_proof_size(req_u->tot_d); lss_hash* pof_u = c3_calloc(pof_w * sizeof(lss_hash)); - if ( c3y == lin_o ) { - if ( pof_w != (dat_u->aup_u.len_y + 1) ) { - return; // TODO: handle like other auth failures - } - for ( int i = 1; i < pof_w; i++ ) { - memcpy(pof_u[i], dat_u->aup_u.has_y[i-1], sizeof(lss_hash)); - } - // complete the proof by computing the first leaf hash - lss_complete_inline_proof(pof_u, dat_u->fra_y, dat_u->len_w); - } else { - if ( dat_u->len_w != pof_w*sizeof(lss_hash) ) { - return; // TODO: handle like other auth failures - } - for ( int i = 0; i < pof_w; i++ ) { - memcpy(pof_u[i], dat_u->fra_y + (i * sizeof(lss_hash)), sizeof(lss_hash)); - } + if ( dat_u->len_w != pof_w*sizeof(lss_hash) ) { + return; // TODO: handle like other auth failures + } + for ( int i = 0; i < pof_w; i++ ) { + memcpy(pof_u[i], dat_u->fra_y + (i * sizeof(lss_hash)), sizeof(lss_hash)); } lss_hash root; lss_root(root, pof_u, pof_w); @@ -2266,26 +2237,19 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) lss_verifier_init(req_u->los_u, 0, req_u->tot_d, pof_u); c3_free(pof_u); - if ( c3y == lin_o ) { - if ( c3y != lss_verifier_ingest(req_u->los_u, dat_u->fra_y, dat_u->len_w, NULL) ) { - return; // TODO: handle like other auth failures - } - memcpy(req_u->dat_y, dat_u->fra_y, dat_u->len_w); - } - req_u = _mesa_put_request(sam_u, nam_u, req_u); _update_resend_timer(req_u); // scry to verify auth u3_noun typ, aut; - switch ( dat_u->aum_u.typ_e ) { + switch ( dat_u->aut_u.typ_e ) { case AUTH_SIGN: typ = c3__sign; - aut = u3dc("scot", c3__uv, u3i_bytes(64, dat_u->aum_u.sig_y)); + aut = u3dc("scot", c3__uv, u3i_bytes(64, dat_u->aut_u.sig_y)); break; case AUTH_HMAC: typ = c3__hmac; - aut = u3dc("scot", c3__uv, u3i_bytes(32, dat_u->aum_u.mac_y)); + aut = u3dc("scot", c3__uv, u3i_bytes(32, dat_u->aut_u.mac_y)); break; default: return; // TODO: handle like other auth failures @@ -2501,7 +2465,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) u3_pend_req* req_u = _mesa_get_request(sam_u, nam_u); if ( !req_u ) { - if ( c3y == nam_u->nit_o ) { + if ( 0 == nam_u->fra_d ) { _mesa_req_pact_init(sam_u, pic_u, &lan_u); } // _mesa_free_pict(pic_u); // XX leaks packet @@ -2545,7 +2509,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) pac_u->pag_u.nam_u.fra_d = (req_u->hav_d >> boq_y); pac_u->pag_u.dat_u.len_w += (1024 * (lev_d - 1)); pac_u->pag_u.dat_u.fra_y = req_u->dat_y; - pac_u->pag_u.dat_u.aum_u = req_u->aum_u; + pac_u->pag_u.dat_u.aut_u = req_u->aut_u; c3_y* buf_y = c3_calloc(mesa_size_pact(pac_u)); c3_w res_w = mesa_etch_pact(buf_y, pac_u); @@ -2620,11 +2584,6 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) c3_d bat_d = _mesa_lop(fra_d); pac_u->pek_u.nam_u.fra_d = bat_d; - // XX HACK: shouldn't be necessary to change data 0 to init, but - // for some reason it's changing the data returned by the scry - if ( pac_u->pek_u.nam_u.fra_d == 0 ) { - pac_u->pek_u.nam_u.nit_o = c3y; - } // if we have the page, send it u3_mesa_line* lin_u = _mesa_get_jumbo_cache(sam_u, &pac_u->pek_u.nam_u); @@ -2771,10 +2730,12 @@ _mesa_hear(u3_mesa* sam_u, c3_w len_w, c3_y* hun_y) { + u3l_log("mesa_hear() len_w %u", len_w); u3_mesa_pict* pic_u; c3_w pre_w; c3_y* cur_y = hun_y; if ( HEAD_SIZE > len_w ) { + u3l_log(" HEAD_SIZE %u", len_w); c3_free(hun_y); return; } @@ -2787,6 +2748,7 @@ _mesa_hear(u3_mesa* sam_u, if ( lin_w == 0 ) { // MESA_LOG(sam_u, SERIAL) // c3_free(hun_y); + u3l_log(" lin_w 0"); mesa_free_pact(&pic_u->pac_u); _ames_hear(u3_Host.sam_u, adr_u, len_w, hun_y); return; diff --git a/pkg/vere/io/mesa/mesa.h b/pkg/vere/io/mesa/mesa.h index 18b11f2774..c65ddc634d 100644 --- a/pkg/vere/io/mesa/mesa.h +++ b/pkg/vere/io/mesa/mesa.h @@ -46,7 +46,6 @@ typedef struct _u3_mesa_name { u3_ship her_u; c3_w rif_w; c3_y boq_y; - c3_o nit_o; c3_o aut_o; c3_d fra_d; c3_s pat_s; @@ -55,34 +54,30 @@ typedef struct _u3_mesa_name { typedef struct _u3_mesa_data_meta { c3_y bot_y; // total-fragments len (2 bits) - c3_y aul_y; // auth-left (message) type (2 bits) - c3_y aur_y; // auth-right (packet) type (2 bits) + c3_o aut_o; // auth tag (c3y for message, c3n for pair) + c3_o auv_o; // auth value (c3y for sig/no-pair, c3n for hmac/pair) c3_y men_y; // fragment length/type (2 bits) } u3_mesa_data_meta; typedef enum { - AUTH_NONE = 0, - AUTH_NEXT = 1, // %1, must be two hash - AUTH_SIGN = 2, // %0, hashes are optional depending on num frag - AUTH_HMAC = 3 + AUTH_SIGN = 0, + AUTH_HMAC = 1, + AUTH_NONE = 2, + AUTH_PAIR = 3, } u3_mesa_auth_type; typedef struct _u3_auth_data { - u3_mesa_auth_type typ_e; // none, traversal (none), sig, or hmac - union { // - c3_y sig_y[64]; // signature - c3_y mac_y[32]; // hmac + u3_mesa_auth_type typ_e; // none, traversal (none), sig, or hmac + union { // + c3_y sig_y[64]; // signature + c3_y mac_y[32]; // hmac + c3_y has_y[2][32]; // hashes }; } u3_auth_data; typedef struct _u3_mesa_data { - // u3_mesa_data_meta met_u; c3_d tot_d; // total bytes in message - u3_auth_data aum_u; - struct { - c3_y len_y; // number of hashes (0, 1, or 2) - c3_y has_y[2][32]; // hashes - } aup_u; + u3_auth_data aut_u; // authentication c3_w len_w; // fragment length c3_y* fra_y; // fragment } u3_mesa_data; diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index 9570c14c0e..a6e151caf0 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -56,11 +56,10 @@ log_name(u3_mesa_name* nam_u) } u3l_log("%s: /%s", her_c, nam_u->pat_c); - u3l_log(" rift: %u bloq: %u init: %s auth: %s frag: %"PRIu64, + u3l_log(" rift: %u bloq: %u auth/data: %s frag: %"PRIu64, nam_u->rif_w, nam_u->boq_y, - (c3y == nam_u->nit_o) ? "&" : "|", - (c3y == nam_u->aut_o) ? "&" : "|", + (c3y == nam_u->aut_o) ? "auth" : "data", nam_u->fra_d ); c3_free(her_c); @@ -72,58 +71,26 @@ _log_data(u3_mesa_data* dat_u) fprintf(stderr, "tot_d: %" PRIu64 " len_w: %u ", dat_u->tot_d, dat_u->len_w); - switch ( dat_u->aum_u.typ_e ) { - case AUTH_NONE: { - if ( dat_u->aup_u.len_y ) { - u3l_log("strange no auth"); - } - else { - u3l_log("no auth"); - } - } break; - - case AUTH_NEXT: { - if ( 2 != dat_u->aup_u.len_y ) { - u3l_log("bad merkle traversal"); - } - else { - u3l_log("merkle traversal:"); - _log_buf(dat_u->aup_u.has_y[0], 32); - _log_buf(dat_u->aup_u.has_y[1], 32); - } - } break; - + switch ( dat_u->aut_u.typ_e ) { case AUTH_SIGN: { fprintf(stderr, "signature: "); - _log_buf(dat_u->aum_u.sig_y, 64); + _log_buf(dat_u->aut_u.sig_y, 64); } break; case AUTH_HMAC: { fprintf(stderr, "hmac: "); - _log_buf(dat_u->aum_u.mac_y, 32); + _log_buf(dat_u->aut_u.mac_y, 32); } break; - } - - switch ( dat_u->aum_u.typ_e ) { - case AUTH_SIGN: - case AUTH_HMAC: { - if ( !dat_u->aup_u.len_y ) { - break; - } - - if ( 4 < dat_u->tot_d ) { - u3l_log("strange inline proof"); - } - else { - u3l_log("inline proof"); - } - for ( int i = 0; i < dat_u->aup_u.len_y; i++ ) { - _log_buf(dat_u->aup_u.has_y[i], 32); - } + case AUTH_NONE: { + u3l_log("merkle traversal: "); } break; - default: break; + case AUTH_PAIR: { + u3l_log("merkle traversal:"); + _log_buf(dat_u->aut_u.has_y[0], 32); + _log_buf(dat_u->aut_u.has_y[1], 32); + } break; } fprintf(stderr, "\r\n"); } @@ -336,7 +303,6 @@ _mesa_sift_name(u3_mesa_name* nam_u, c3_y* buf_y, c3_w len_w) } } - nam_u->nit_o = ( met_u.nit_y ) ? c3y : c3n; // XX ?:(=(1 tau.c) %auth %data) nam_u->aut_o = ( met_u.tau_y ) ? c3y : c3n; @@ -363,8 +329,8 @@ _mesa_sift_data(u3_mesa_data* dat_u, c3_y* buf_y, c3_w len_w) CHECK_BOUNDS(len_w, cur_w + 1); c3_y met_y = buf_y[cur_w]; met_u.bot_y = (met_y >> 0) & 0x3; - met_u.aul_y = (met_y >> 2) & 0x3; - met_u.aur_y = (met_y >> 4) & 0x3; + met_u.aut_o = (met_y >> 2) & 0x1; + met_u.auv_o = (met_y >> 3) & 0x1; met_u.men_y = (met_y >> 6) & 0x3; cur_w += 1; @@ -376,21 +342,29 @@ _mesa_sift_data(u3_mesa_data* dat_u, c3_y* buf_y, c3_w len_w) cur_w++; } - c3_y aum_y = ( 2 == met_u.aul_y ) ? 64 : - ( 3 == met_u.aul_y ) ? 32 : 0; - CHECK_BOUNDS(len_w, cur_w + aum_y); - memcpy(dat_u->aum_u.sig_y, buf_y + cur_w, aum_y); - cur_w += aum_y; - - dat_u->aum_u.typ_e = met_u.aul_y; // XX - - assert( 3 > met_u.aur_y ); - - CHECK_BOUNDS(len_w, cur_w + (met_u.aur_y * 32)); - dat_u->aup_u.len_y = met_u.aur_y; - for( int i = 0; i < met_u.aur_y; i++ ) { - memcpy(dat_u->aup_u.has_y[i], buf_y + cur_w, 32); - cur_w += 32; + if ( c3y == met_u.aut_o ) { + if ( c3y == met_u.auv_o ) { + CHECK_BOUNDS(len_w, cur_w + 64); + memcpy(dat_u->aut_u.sig_y, buf_y + cur_w, 64); + cur_w += 64; + dat_u->aut_u.typ_e = AUTH_SIGN; + } + else { + CHECK_BOUNDS(len_w, cur_w + 32); + memcpy(dat_u->aut_u.mac_y, buf_y + cur_w, 32); + cur_w += 32; + dat_u->aut_u.typ_e = AUTH_HMAC; + } + } + else { + if ( c3y == met_u.auv_o ) { + dat_u->aut_u.typ_e = AUTH_NONE; + } else { + CHECK_BOUNDS(len_w, cur_w + 64); + memcpy(dat_u->aut_u.has_y, buf_y + cur_w, 64); + cur_w += 64; + dat_u->aut_u.typ_e = AUTH_PAIR; + } } c3_y nel_y = met_u.men_y; @@ -615,17 +589,9 @@ _mesa_etch_name(c3_y* buf_y, u3_mesa_name* nam_u) met_u.ran_y = _mesa_rank(nam_u->her_u); met_u.rif_y = safe_dec(_mesa_met3_w(nam_u->rif_w)); - if ( c3y == nam_u->nit_o ) { - assert( c3n == nam_u->aut_o ); // XX - met_u.nit_y = 1; - met_u.tau_y = 0; - met_u.gaf_y = 0; - } - else { - met_u.nit_y = 0; - met_u.tau_y = (c3y == nam_u->aut_o) ? 1 : 0; - met_u.gaf_y = _mesa_size_tot(nam_u->fra_d); - } + met_u.nit_y = 0; + met_u.tau_y = (c3y == nam_u->aut_o) ? 1 : 0; + met_u.gaf_y = _mesa_size_tot(nam_u->fra_d); c3_y met_y = (met_u.ran_y & 0x3) << 0 ^ (met_u.rif_y & 0x3) << 2 @@ -651,7 +617,7 @@ _mesa_etch_name(c3_y* buf_y, u3_mesa_name* nam_u) buf_y[cur_w] = nam_u->boq_y; cur_w++; - c3_y fra_y = (c3y == nam_u->nit_o) ? 0 : met_u.gaf_y + 1; + c3_y fra_y = met_u.gaf_y + 1; for( int i = 0; i < fra_y; i++ ) { buf_y[cur_w] = (nam_u->fra_d >> (8*i)) & 0xff; cur_w++; @@ -675,46 +641,48 @@ static c3_w _mesa_etch_data(c3_y* buf_y, u3_mesa_data* dat_u) { c3_w cur_w = 0; - u3_mesa_data_meta met_u; - // XX - met_u.aul_y = dat_u->aum_u.typ_e; - met_u.aur_y = dat_u->aup_u.len_y; + c3_y aut_y = 0; + switch ( dat_u->aut_u.typ_e ) { + case AUTH_SIGN: aut_y = 0; break; + case AUTH_HMAC: aut_y = 1; break; + case AUTH_NONE: aut_y = 2; break; + case AUTH_PAIR: aut_y = 3; break; + } + c3_y bot_y = _mesa_size_tot(dat_u->tot_d); c3_y nel_y = _mesa_met3_w(dat_u->len_w); - met_u.men_y = (3 >= nel_y) ? nel_y : 3; - met_u.bot_y = _mesa_size_tot(dat_u->tot_d); - - c3_y met_y = (met_u.bot_y & 0x3) << 0 - ^ (met_u.aul_y & 0x3) << 2 - ^ (met_u.aur_y & 0x3) << 4 - ^ (met_u.men_y & 0x3) << 6; - buf_y[cur_w] = met_y; + c3_y men_y = (3 >= nel_y) ? nel_y : 3; + buf_y[cur_w] = (bot_y & 0x3) << 0 + | (aut_y & 0x3) << 2 + | (men_y & 0x3) << 6; cur_w++; - c3_y tot_y = _mesa_tot_bytes(met_u.bot_y); + c3_y tot_y = _mesa_tot_bytes(bot_y); for (int i = 0; i < tot_y; i++ ) { buf_y[cur_w] = (dat_u->tot_d >> (8 * i)) & 0xFF; cur_w++; } - switch ( dat_u->aum_u.typ_e ) { + switch ( dat_u->aut_u.typ_e ) { case AUTH_SIGN: { - memcpy(buf_y + cur_w, dat_u->aum_u.sig_y, 64); + memcpy(buf_y + cur_w, dat_u->aut_u.sig_y, 64); cur_w += 64; } break; case AUTH_HMAC: { - memcpy(buf_y + cur_w, dat_u->aum_u.mac_y, 32); + memcpy(buf_y + cur_w, dat_u->aut_u.mac_y, 32); cur_w += 32; } break; - default: break; - } - for ( int i = 0; i < dat_u->aup_u.len_y; i++ ) { - memcpy(buf_y + cur_w, dat_u->aup_u.has_y[i], 32); - cur_w += 32; + case AUTH_NONE: { + } break; + + case AUTH_PAIR: { + memcpy(buf_y + cur_w, dat_u->aut_u.has_y, 64); + cur_w += 32; + } break; } - if ( 3 == met_u.men_y ) { + if ( 3 == men_y ) { buf_y[cur_w] = nel_y; cur_w++; } @@ -816,10 +784,8 @@ _mesa_size_name(u3_mesa_name* nam_u) siz_w += met_u.rif_y + 1; siz_w++; // bloq - if (c3n == nam_u->nit_o ) { - met_u.gaf_y = _mesa_size_tot(nam_u->fra_d); - siz_w += _mesa_tot_bytes(met_u.gaf_y); - } + met_u.gaf_y = _mesa_size_tot(nam_u->fra_d); + siz_w += _mesa_tot_bytes(met_u.gaf_y); siz_w += 2; // path-length siz_w += nam_u->pat_s; @@ -837,7 +803,7 @@ _mesa_size_data(u3_mesa_data* dat_u) siz_w += _mesa_tot_bytes(met_u.bot_y); - switch ( dat_u->aum_u.typ_e ) { + switch ( dat_u->aut_u.typ_e ) { case AUTH_SIGN: { siz_w += 64; } break; @@ -846,10 +812,13 @@ _mesa_size_data(u3_mesa_data* dat_u) siz_w += 32; } break; - default: break; - } + case AUTH_NONE: { + } break; - siz_w += 32 * dat_u->aup_u.len_y; + case AUTH_PAIR: { + siz_w += 64; + } break; + } c3_y nel_y = _mesa_met3_w(dat_u->len_w); met_u.men_y = (3 >= nel_y) ? nel_y : 3; @@ -1068,11 +1037,11 @@ _test_cmp_data(u3_mesa_data* hav_u, u3_mesa_data* ned_u) cmp_scalar(tot_w, "data: total packets", "%u"); - cmp_scalar(aum_u.typ_e, "data: auth-types", "%u"); - cmp_buffer(aum_u.sig_y, 64, "data: sig|hmac|null"); + cmp_scalar(aut_u.typ_e, "data: auth-types", "%u"); + cmp_buffer(aut_u.sig_y, 64, "data: sig|hmac|null"); - cmp_scalar(aup_u.len_y, "data: hash-lengths", "%u"); - cmp_buffer(aup_u.has_y, ned_u->aup_u.len_y, "data: hashes"); + cmp_scalar(aut_u.len_y, "data: hash-lengths", "%u"); + cmp_buffer(aut_u.has_y, ned_u->aut_u.len_y, "data: hashes"); cmp_scalar(len_w, "data: fragments-lengths", "%u"); cmp_buffer(fra_y, ned_u->len_w, "data: fragments"); @@ -1309,30 +1278,30 @@ _test_make_data(void* ptr_v, u3_mesa_data* dat_u) { dat_u->tot_w = _test_rand_word(ptr_v); - memset(dat_u->aum_u.sig_y, 0, 64); - dat_u->aup_u.len_y = 0; - memset(dat_u->aup_u.has_y, 0, sizeof(dat_u->aup_u.has_y)); + memset(dat_u->aut_u.sig_y, 0, 64); + dat_u->aut_u.len_y = 0; + memset(dat_u->aut_u.has_y, 0, sizeof(dat_u->aut_u.has_y)); - switch ( dat_u->aum_u.typ_e = _test_rand_bits(ptr_v, 2) ) { + switch ( dat_u->aut_u.typ_e = _test_rand_bits(ptr_v, 2) ) { case AUTH_NEXT: { - dat_u->aup_u.len_y = 2; + dat_u->aut_u.len_y = 2; } break; case AUTH_SIGN: { - dat_u->aup_u.len_y = _test_rand_gulf_y(ptr_v, 3); - _test_rand_bytes(ptr_v, 64, dat_u->aum_u.sig_y); + dat_u->aut_u.len_y = _test_rand_gulf_y(ptr_v, 3); + _test_rand_bytes(ptr_v, 64, dat_u->aut_u.sig_y); } break; case AUTH_HMAC: { - dat_u->aup_u.len_y = _test_rand_gulf_y(ptr_v, 3); - _test_rand_bytes(ptr_v, 32, dat_u->aum_u.mac_y); + dat_u->aut_u.len_y = _test_rand_gulf_y(ptr_v, 3); + _test_rand_bytes(ptr_v, 32, dat_u->aut_u.mac_y); } break; default: break; } - for ( c3_w i_w = 0; i_w < dat_u->aup_u.len_y; i_w++ ) { - _test_rand_bytes(ptr_v, 32, dat_u->aup_u.has_y[i_w]); + for ( c3_w i_w = 0; i_w < dat_u->aut_u.len_y; i_w++ ) { + _test_rand_bytes(ptr_v, 32, dat_u->aut_u.has_y[i_w]); } dat_u->len_w = _test_rand_gulf_w(ptr_v, 1024); @@ -1410,7 +1379,7 @@ _test_sift_page() nam_u->nit_o = c3n; u3_mesa_data* dat_u = &pac_u.pag_u.dat_u; - dat_u->aum_u.typ_e = AUTH_NONE; + dat_u->aut_u.typ_e = AUTH_NONE; dat_u->tot_w = 1000; dat_u->len_w = 1024; dat_u->fra_y = c3_calloc(1024); From 1640177c7ca9ed44d3ed8ed2b4cc82628aef8228 Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Tue, 6 Aug 2024 17:53:52 -0400 Subject: [PATCH 337/430] mesa: only send one response fragment on cache hit --- pkg/vere/io/mesa.c | 74 +++++++++++++++++++++++++++------------------- 1 file changed, 44 insertions(+), 30 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index e8aba5329a..420a275f86 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1916,7 +1916,40 @@ _mesa_send_pact(u3_mesa* sam_u, } static void -_mesa_send_jumbo_pieces(u3_mesa* sam_u, u3_mesa_line* lin_u) +_mesa_send_leaf(u3_mesa* sam_u, + u3_mesa_line* lin_u, + u3_mesa_pact* pac_u, // scratchpad + c3_d fra_d) +{ + u3_mesa_name* nam_u = &pac_u->pag_u.nam_u; + u3_mesa_data* dat_u = &pac_u->pag_u.dat_u; + nam_u->fra_d = fra_d; + c3_d i_d = fra_d - (lin_u->nam_u.fra_d * (1 << u3_Host.ops_u.jum_y)); + c3_w cur_w = i_d * 1024; + dat_u->fra_y = lin_u->dat_y + cur_w; + dat_u->len_w = c3_min(lin_u->dat_w - cur_w, 1024); + + lss_pair* pair = ((lss_pair*)lin_u->haz_y) + i_d; + if ( 0 == memcmp(pair, &(lss_pair){0}, sizeof(lss_pair)) ) { + dat_u->aut_u.typ_e = AUTH_NONE; + } else { + dat_u->aut_u.typ_e = AUTH_PAIR; + memcpy(dat_u->aut_u.has_y, pair, sizeof(lss_pair)); + } + u3_weak pin = _mesa_get_pit(sam_u, nam_u); + if ( u3_none != pin) { + #ifdef MESA_DEBUG + u3l_log(" sending leaf packet, fra_d: %"PRIu64, nam_u->fra_d); + #endif + log_pact(pac_u); + _mesa_send_pact(sam_u, u3k(u3t(pin)), NULL, pac_u); + _mesa_del_pit(sam_u, nam_u); + u3z(pin); + } +} + +static void +_mesa_send_jumbo_pieces(u3_mesa* sam_u, u3_mesa_line* lin_u, c3_d* fra_d) { #ifdef MESA_DEBUG u3l_log("mesa: send_jumbo_pieces()"); @@ -1969,34 +2002,16 @@ _mesa_send_jumbo_pieces(u3_mesa* sam_u, u3_mesa_line* lin_u) // send leaf packet(s) c3_d lev_d = mesa_num_leaves(lin_u->dat_w); c3_d fir_d = nam_u->fra_d; - c3_y* pro_y = lin_u->tip_y; - for (c3_d i_d = 0; i_d < lev_d; i_d++) { - c3_d fra_d = i_d + fir_d; - nam_u->fra_d = fra_d; - c3_w cur_w = i_d * 1024; - dat_u->fra_y = lin_u->dat_y + cur_w; - dat_u->len_w = c3_min(lin_u->dat_w - cur_w, 1024); - - lss_pair* pair = ((lss_pair*)lin_u->haz_y) + i_d; - if ( 0 == memcmp(pair, &(lss_pair){0}, sizeof(lss_pair)) ) { - dat_u->aut_u.typ_e = AUTH_NONE; - } else { - dat_u->aut_u.typ_e = AUTH_PAIR; - memcpy(dat_u->aut_u.has_y, pair, sizeof(lss_pair)); - } - u3_weak pin = _mesa_get_pit(sam_u, nam_u); - if ( u3_none != pin) { - #ifdef MESA_DEBUG - u3l_log(" sending leaf packet, fra_d: %"PRIu64, nam_u->fra_d); - #endif - log_pact(&pac_u); - _mesa_send_pact(sam_u, u3k(u3t(pin)), NULL, &pac_u); - _mesa_del_pit(sam_u, nam_u); - u3z(pin); + c3_d las_d = fir_d + lev_d; + if ( NULL == fra_d ) { + for (c3_d fra_d = fir_d; fra_d < las_d; fra_d++) { + _mesa_send_leaf(sam_u, lin_u, &pac_u, fra_d); } } - // c3_free(jumbo_pact_y); // XX - // lss_builder_free(bil_u); + else { + _mesa_send_leaf(sam_u, lin_u, &pac_u, *fra_d); + } + mesa_free_pact(&pac_u); } static void @@ -2070,7 +2085,7 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun res) } _mesa_put_jumbo_cache(sam_u, nam_u, lin_u); - _mesa_send_jumbo_pieces(sam_u, lin_u); + _mesa_send_jumbo_pieces(sam_u, lin_u, NULL); u3z(res); return; } @@ -2589,8 +2604,7 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) u3_mesa_line* lin_u = _mesa_get_jumbo_cache(sam_u, &pac_u->pek_u.nam_u); if ( NULL != lin_u ) { if ( CTAG_ITEM == lin_u->typ_y ) { - // TODO only respond to this fragment, don't iterate - _mesa_send_jumbo_pieces(sam_u, lin_u); + _mesa_send_jumbo_pieces(sam_u, lin_u, &fra_d); _mesa_free_pict(pic_u); } return; From 8287c68f8cb7e5df915a2ee3d1350ea5f4280ffd Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Tue, 6 Aug 2024 18:23:40 -0400 Subject: [PATCH 338/430] mesa: fix fragment-num encoding bug --- pkg/vere/io/mesa.c | 2 +- pkg/vere/io/mesa/pact.c | 29 +++++++++++++++++------------ 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 420a275f86..993fcd2876 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -2579,7 +2579,7 @@ static void _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) { #ifdef MESA_DEBUG - u3l_log("mesa: hear_peek()\r\n\r\n\r\n\r\n\r\n"); + u3l_log("mesa: hear_peek()"); u3_assert( PACT_PEEK == pic_u->pac_u.hed_u.typ_y ); #endif diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index a6e151caf0..21bf7e31ab 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -170,8 +170,11 @@ _mesa_rank(u3_ship who_u) }; } +/* +** _mesa_make_chub_tag(): make a 2-bit tag for a chub length +*/ static c3_y -_mesa_size_tot(c3_d tot_d) +_mesa_make_chub_tag(c3_d tot_d) { return (tot_d <= 0xff)? 0b00 : (tot_d <= 0xffff)? 0b01 : @@ -179,8 +182,11 @@ _mesa_size_tot(c3_d tot_d) 0b11; } +/* +** _mesa_bytes_of_chub_tag(): how many bytes does a chub tag mean +*/ static c3_y -_mesa_tot_bytes(c3_y tot_y) +_mesa_bytes_of_chub_tag(c3_y tot_y) { return 1 << tot_y; } @@ -295,7 +301,7 @@ _mesa_sift_name(u3_mesa_name* nam_u, c3_y* buf_y, c3_w len_w) nam_u->fra_d = 0; } else { - c3_y fag_y = met_u.gaf_y + 1; + c3_y fag_y = _mesa_bytes_of_chub_tag(met_u.gaf_y); CHECK_BOUNDS(len_w, cur_w + fag_y); for ( int i = 0; i < fag_y; i++ ) { nam_u->fra_d |= (buf_y[cur_w] << (8*i)); @@ -334,7 +340,7 @@ _mesa_sift_data(u3_mesa_data* dat_u, c3_y* buf_y, c3_w len_w) met_u.men_y = (met_y >> 6) & 0x3; cur_w += 1; - c3_y tot_y = _mesa_tot_bytes(met_u.bot_y); + c3_y tot_y = _mesa_bytes_of_chub_tag(met_u.bot_y); CHECK_BOUNDS(len_w, cur_w + tot_y); dat_u->tot_d = 0; for( int i = 0; i < tot_y; i++ ) { @@ -591,7 +597,7 @@ _mesa_etch_name(c3_y* buf_y, u3_mesa_name* nam_u) met_u.nit_y = 0; met_u.tau_y = (c3y == nam_u->aut_o) ? 1 : 0; - met_u.gaf_y = _mesa_size_tot(nam_u->fra_d); + met_u.gaf_y = _mesa_make_chub_tag(nam_u->fra_d); c3_y met_y = (met_u.ran_y & 0x3) << 0 ^ (met_u.rif_y & 0x3) << 2 @@ -649,14 +655,14 @@ _mesa_etch_data(c3_y* buf_y, u3_mesa_data* dat_u) case AUTH_NONE: aut_y = 2; break; case AUTH_PAIR: aut_y = 3; break; } - c3_y bot_y = _mesa_size_tot(dat_u->tot_d); + c3_y bot_y = _mesa_make_chub_tag(dat_u->tot_d); c3_y nel_y = _mesa_met3_w(dat_u->len_w); c3_y men_y = (3 >= nel_y) ? nel_y : 3; buf_y[cur_w] = (bot_y & 0x3) << 0 | (aut_y & 0x3) << 2 | (men_y & 0x3) << 6; cur_w++; - c3_y tot_y = _mesa_tot_bytes(bot_y); + c3_y tot_y = _mesa_bytes_of_chub_tag(bot_y); for (int i = 0; i < tot_y; i++ ) { buf_y[cur_w] = (dat_u->tot_d >> (8 * i)) & 0xFF; @@ -784,8 +790,8 @@ _mesa_size_name(u3_mesa_name* nam_u) siz_w += met_u.rif_y + 1; siz_w++; // bloq - met_u.gaf_y = _mesa_size_tot(nam_u->fra_d); - siz_w += _mesa_tot_bytes(met_u.gaf_y); + met_u.gaf_y = _mesa_make_chub_tag(nam_u->fra_d); + siz_w += _mesa_bytes_of_chub_tag(met_u.gaf_y); siz_w += 2; // path-length siz_w += nam_u->pat_s; @@ -799,9 +805,8 @@ _mesa_size_data(u3_mesa_data* dat_u) c3_w siz_w = 1; u3_mesa_data_meta met_u; - met_u.bot_y = _mesa_size_tot(dat_u->tot_d); - - siz_w += _mesa_tot_bytes(met_u.bot_y); + met_u.bot_y = _mesa_make_chub_tag(dat_u->tot_d); + siz_w += _mesa_bytes_of_chub_tag(met_u.bot_y); switch ( dat_u->aut_u.typ_e ) { case AUTH_SIGN: { From 88f92eeddbcb3f217ca7162baa4a03d2d5e8e63d Mon Sep 17 00:00:00 2001 From: midden-fabler Date: Tue, 6 Aug 2024 23:21:56 -0400 Subject: [PATCH 339/430] http: check ship for scry at now --- pkg/vere/io/http.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/pkg/vere/io/http.c b/pkg/vere/io/http.c index 9f5b937511..094c813af6 100644 --- a/pkg/vere/io/http.c +++ b/pkg/vere/io/http.c @@ -963,8 +963,19 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) // peek or respond from cache // if ( c3y == req_u->peq_u->las_o ) { - u3_pier_peek_last(htd_u->car_u.pir_u, gang, c3__ex, - u3k(bem.des), spur, req_u->peq_u, _http_scry_cb); + u3_noun our = u3dc("scot", 'p', u3i_chubs(2, htd_u->car_u.pir_u->who_d)); + if ( our == bem.who ) { + u3_pier_peek_last(htd_u->car_u.pir_u, gang, c3__ex, + u3k(bem.des), spur, req_u->peq_u, _http_scry_cb); + } + else { + c3_c* msg_c = "bad request"; + h2o_send_error_generic(req_u->rec_u, 400, msg_c, msg_c, 0); + u3z(gang); + u3z(spur); + u3z(req_u->peq_u->pax); + } + u3z(our); } else { u3_noun bam = u3nq(u3k(bem.who), u3k(bem.des), u3k(bem.cas), spur); From 650f8d99a266b57855e309bcebe3ea1e88c49dd0 Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Thu, 8 Aug 2024 23:52:17 -0400 Subject: [PATCH 340/430] mesa: WIP debugging packet parsing after protocol change --- pkg/vere/io/mesa.c | 6 +-- pkg/vere/io/mesa/pact.c | 96 +++++++++++++++++++++++++++++------------ 2 files changed, 71 insertions(+), 31 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 993fcd2876..fd0784b57c 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -453,7 +453,7 @@ static void _mesa_copy_name(u3_mesa_name* des_u, u3_mesa_name* src_u) { memcpy(des_u, src_u, sizeof(u3_mesa_name)); - des_u->pat_c = c3_calloc(src_u->pat_s); + des_u->pat_c = c3_calloc(src_u->pat_s + 1); // null-terminate memcpy(des_u->pat_c, src_u->pat_c, src_u->pat_s); } @@ -493,9 +493,7 @@ _mesa_new_line(u3_mesa_name* nam_u) { u3_mesa_line* lin_u = c3_calloc(sizeof(u3_mesa_line)); lin_u->typ_y = CTAG_ITEM; - lin_u->nam_u = *nam_u; - memcpy(lin_u->nam_u.pat_c, nam_u->pat_c, nam_u->pat_s); - + _mesa_copy_name(&lin_u->nam_u, nam_u); return lin_u; } diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index 21bf7e31ab..59fcab737a 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -262,9 +262,9 @@ mesa_sift_head(c3_y buf_y[8], u3_mesa_head* hed_u) static c3_w _mesa_sift_name(u3_mesa_name* nam_u, c3_y* buf_y, c3_w len_w) { -#ifdef MESA_DEBUG - //u3l_log("mesa: sifting name %i", len_w); -#endif +//#ifdef MESA_DEBUG + u3l_log("mesa: sifting name %i", len_w); +//#endif c3_w cur_w = 0; u3_mesa_name_meta met_u; @@ -299,6 +299,7 @@ _mesa_sift_name(u3_mesa_name* nam_u, c3_y* buf_y, c3_w len_w) assert( !met_u.tau_y ); // XX init packet nam_u->fra_d = 0; + u3l_log("name: init"); } else { c3_y fag_y = _mesa_bytes_of_chub_tag(met_u.gaf_y); @@ -307,6 +308,7 @@ _mesa_sift_name(u3_mesa_name* nam_u, c3_y* buf_y, c3_w len_w) nam_u->fra_d |= (buf_y[cur_w] << (8*i)); cur_w++; } + u3l_log("name: fag_y: %u nam_u->fra_d %"PRIu64, fag_y, nam_u->fra_d); } // XX ?:(=(1 tau.c) %auth %data) @@ -316,6 +318,7 @@ _mesa_sift_name(u3_mesa_name* nam_u, c3_y* buf_y, c3_w len_w) nam_u->pat_s = buf_y[cur_w] | (buf_y[cur_w + 1] << 8); cur_w += 2; + u3l_log("nam_u->pat_s %u", nam_u->pat_s); nam_u->pat_c = c3_calloc(nam_u->pat_s + 1); // unix string for ease of manipulation CHECK_BOUNDS(len_w, cur_w + nam_u->pat_s); @@ -347,6 +350,7 @@ _mesa_sift_data(u3_mesa_data* dat_u, c3_y* buf_y, c3_w len_w) dat_u->tot_d |= (buf_y[cur_w] << (8*i)); cur_w++; } + u3l_log("dat_u->tot_d %"PRIu64, dat_u->tot_d); if ( c3y == met_u.aut_o ) { if ( c3y == met_u.auv_o ) { @@ -354,22 +358,26 @@ _mesa_sift_data(u3_mesa_data* dat_u, c3_y* buf_y, c3_w len_w) memcpy(dat_u->aut_u.sig_y, buf_y + cur_w, 64); cur_w += 64; dat_u->aut_u.typ_e = AUTH_SIGN; + u3l_log("AUTH_SIGN"); } else { CHECK_BOUNDS(len_w, cur_w + 32); memcpy(dat_u->aut_u.mac_y, buf_y + cur_w, 32); cur_w += 32; dat_u->aut_u.typ_e = AUTH_HMAC; + u3l_log("AUTH_HMAC"); } } else { if ( c3y == met_u.auv_o ) { dat_u->aut_u.typ_e = AUTH_NONE; + u3l_log("AUTH_NONE"); } else { CHECK_BOUNDS(len_w, cur_w + 64); memcpy(dat_u->aut_u.has_y, buf_y + cur_w, 64); cur_w += 64; dat_u->aut_u.typ_e = AUTH_PAIR; + u3l_log("AUTH_PAIR"); } } @@ -379,15 +387,18 @@ _mesa_sift_data(u3_mesa_data* dat_u, c3_y* buf_y, c3_w len_w) CHECK_BOUNDS(len_w, cur_w + 1); nel_y = buf_y[cur_w]; cur_w++; + u3l_log("nel 3"); } CHECK_BOUNDS(len_w, cur_w + nel_y); dat_u->len_w = 0; + u3l_log("nel_y %u", nel_y); for ( int i = 0; i < nel_y; i++ ) { dat_u->len_w |= (buf_y[cur_w] << (8*i)); cur_w++; } + u3l_log("dat_u->len_w %u cur_w %u", dat_u->len_w, cur_w); CHECK_BOUNDS(len_w, cur_w + dat_u->len_w); dat_u->fra_y = c3_calloc(dat_u->len_w); memcpy(dat_u->fra_y, buf_y + cur_w, dat_u->len_w); @@ -447,27 +458,41 @@ _mesa_sift_hops(u3_mesa_page_pact* pac_u, c3_y nex_y, c3_y* buf_y, c3_w len_w) static c3_w _mesa_sift_page_pact(u3_mesa_pact* pat_u, c3_y nex_y, c3_y* buf_y, c3_w len_w) { + u3l_log("mesa: sift page pact"); u3_mesa_page_pact* pac_u = &pat_u->pag_u; - c3_w cur_w = 0, nex_w; + c3_w cur_w = 0, res_w = len_w, nex_w; - if ( !(nex_w = _mesa_sift_name(&pac_u->nam_u, buf_y + cur_w, len_w)) ) { + if ( !(nex_w = _mesa_sift_name(&pac_u->nam_u, buf_y + cur_w, res_w)) ) { + u3l_log("name fail"); return 0; } cur_w += nex_w; + res_w -= nex_w; - if ( !(nex_w = _mesa_sift_data(&pac_u->dat_u, buf_y + cur_w, len_w)) ) { + if ( !(nex_w = _mesa_sift_data(&pac_u->dat_u, buf_y + cur_w, res_w)) ) { + u3l_log("data fail"); return 0; } cur_w += nex_w; + res_w -= nex_w; - cur_w += _mesa_sift_hops(pac_u, nex_y, buf_y + cur_w, len_w); + nex_w = _mesa_sift_hops(pac_u, nex_y, buf_y + cur_w, res_w); + cur_w += nex_w; + res_w -= nex_w; - return cur_w; + if ( cur_w > len_w ) { + u3l_log("mesa: %%page too big"); + return 0; + } + else { + return cur_w; + } } static c3_w _mesa_sift_peek_pact(u3_mesa_peek_pact* pac_u, c3_y* buf_y, c3_w len_w) { + u3l_log("mesa: sift peek pact"); c3_w siz_w = _mesa_sift_name(&pac_u->nam_u, buf_y, len_w); if ( siz_w < len_w ) { u3l_log("mesa: failed to consume entire packet"); @@ -481,57 +506,67 @@ _mesa_sift_peek_pact(u3_mesa_peek_pact* pac_u, c3_y* buf_y, c3_w len_w) static c3_w _mesa_sift_poke_pact(u3_mesa_poke_pact* pac_u, c3_y* buf_y, c3_w len_w) { - c3_w cur_w = 0, nex_w; + u3l_log("mesa: sift poke pact"); + c3_w cur_w = 0, res_w = len_w, nex_w; // ack path - if ( !(nex_w = _mesa_sift_name(&pac_u->nam_u, buf_y + cur_w, len_w)) ) { + if ( !(nex_w = _mesa_sift_name(&pac_u->nam_u, buf_y + cur_w, res_w)) ) { + u3l_log("name fail"); // u3l_log("_mesa_sift_poke_pact nex_w %u", nex_w); return 0; } cur_w += nex_w; + res_w -= nex_w; // payload path - if ( !(nex_w = _mesa_sift_name(&pac_u->pay_u, buf_y + cur_w, len_w)) ) { - // u3l_log("_mesa_sift_poke_pact nex_w %u", nex_w); + if ( !(nex_w = _mesa_sift_name(&pac_u->pay_u, buf_y + cur_w, res_w)) ) { + u3l_log("payload name fail"); return 0; } cur_w += nex_w; + res_w -= nex_w; // payload - if ( !(nex_w = _mesa_sift_data(&pac_u->dat_u, buf_y + cur_w, len_w)) ) { + if ( !(nex_w = _mesa_sift_data(&pac_u->dat_u, buf_y + cur_w, res_w)) ) { + u3l_log("data fail"); // u3l_log("_mesa_sift_poke_pact nex_w %u", nex_w); return 0; } cur_w += nex_w; + res_w -= nex_w; // u3l_log("_mesa_sift_poke_pact cur_w %u", cur_w); - + if ( cur_w > len_w ) { + u3l_log("mesa: %%poke too big"); + return 0; + } return cur_w; } c3_w mesa_sift_pact(u3_mesa_pact* pac_u, c3_y* buf_y, c3_w len_w) { - c3_w res_w = 0; + c3_w cur_w = 0, res_w = len_w; - if ( len_w < 8 ) { + if ( len_w < HEAD_SIZE ) { u3l_log("mesa: attempted to parse overly short packet of size %u", len_w); } mesa_sift_head(buf_y, &pac_u->hed_u); - buf_y += 8; - len_w -= 8; + buf_y += HEAD_SIZE; + cur_w += HEAD_SIZE; + res_w -= HEAD_SIZE; // u3l_log("pac_u->hed_u.typ_y typ_y %u", pac_u->hed_u.typ_y); switch ( pac_u->hed_u.typ_y ) { case PACT_PEEK: { - res_w = _mesa_sift_peek_pact(&pac_u->pek_u, buf_y, len_w); + cur_w += _mesa_sift_peek_pact(&pac_u->pek_u, buf_y, res_w); } break; case PACT_PAGE: { - res_w = _mesa_sift_page_pact(pac_u, pac_u->hed_u.nex_y, buf_y, len_w); + cur_w += _mesa_sift_page_pact(pac_u, pac_u->hed_u.nex_y, buf_y, res_w); } break; case PACT_POKE: { - res_w = _mesa_sift_poke_pact(&pac_u->pok_u, buf_y, len_w); + cur_w += _mesa_sift_poke_pact(&pac_u->pok_u, buf_y, res_w); } break; default: { /* u3l_log("mesa: received unknown packet type"); */ @@ -539,25 +574,32 @@ mesa_sift_pact(u3_mesa_pact* pac_u, c3_y* buf_y, c3_w len_w) break; } } - if ( 0 == res_w ) { + if ( 0 == cur_w ) { return 0; } { - c3_w mug_w = u3r_mug_bytes(buf_y, res_w); + c3_w mug_w = u3r_mug_bytes(buf_y, cur_w); mug_w &= 0xFFFFF; if ( mug_w != pac_u->hed_u.mug_w ) { - /* u3l_log("mesa: failed mug"); */ + u3l_log("mesa: failed mug, mug_w %u " + "pac_u->hed_u.mug_w %u " + "cur_w %u " + "len_w %u", + mug_w, pac_u->hed_u.mug_w, cur_w, len_w); /* _log_buf(buf_y, res_w); */ - /* u3l_log("res_w %u", res_w); */ /* u3l_log("frag %u", pac_u->pag_u.nam_u.fra_w); */ return 0; } } - //u3_assert(res_w <= len_w ); - return res_w + 8; + if ( cur_w > len_w ) { + return 0; + } + else { + return cur_w; + } } /* serialisation From 174ee94fb9242f3e27f269e1e321eb0f3d4a41b6 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Fri, 9 Aug 2024 11:17:36 +0200 Subject: [PATCH 341/430] mesa: account for cookie length in mesa_sift_pact --- pkg/vere/io/mesa/pact.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index 59fcab737a..1541eb5039 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -546,15 +546,16 @@ c3_w mesa_sift_pact(u3_mesa_pact* pac_u, c3_y* buf_y, c3_w len_w) { c3_w cur_w = 0, res_w = len_w; + c3_w pre_w = (HEAD_SIZE + MESA_COOKIE_LEN); // prelude: header + cookie - if ( len_w < HEAD_SIZE ) { + if ( len_w < pre_w ) { u3l_log("mesa: attempted to parse overly short packet of size %u", len_w); } mesa_sift_head(buf_y, &pac_u->hed_u); - buf_y += HEAD_SIZE; - cur_w += HEAD_SIZE; - res_w -= HEAD_SIZE; + buf_y += pre_w; + // cur_w += pre_w; // XX add the prelude later + res_w -= pre_w; // u3l_log("pac_u->hed_u.typ_y typ_y %u", pac_u->hed_u.typ_y); @@ -598,7 +599,7 @@ mesa_sift_pact(u3_mesa_pact* pac_u, c3_y* buf_y, c3_w len_w) return 0; } else { - return cur_w; + return cur_w + pre_w; } } From cdcf5c7200c0ad14b21495ab83779cb99fcaaebb Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Fri, 9 Aug 2024 16:08:31 +0200 Subject: [PATCH 342/430] mesa: WIP bugfixes --- pkg/vere/io/mesa.c | 7 ++++++- pkg/vere/io/mesa/pact.c | 14 +++++++------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index fd0784b57c..52503b26d7 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1909,6 +1909,11 @@ _mesa_send_pact(u3_mesa* sam_u, { c3_y buf_y[PACT_SIZE]; c3_w len_w = mesa_etch_pact(buf_y, tac_u); + u3_mesa_pact paco; + // XX siftting after etching fails + // + u3l_log("THE LENGTH %u", len_w); + mesa_sift_pact(&paco, buf_y, len_w); _mesa_send_bufs(sam_u, per_u, buf_y, len_w, u3k(las)); u3z(las); } @@ -2009,7 +2014,7 @@ _mesa_send_jumbo_pieces(u3_mesa* sam_u, u3_mesa_line* lin_u, c3_d* fra_d) else { _mesa_send_leaf(sam_u, lin_u, &pac_u, *fra_d); } - mesa_free_pact(&pac_u); + // mesa_free_pact(&pac_u); } static void diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index 1541eb5039..e691218613 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -302,7 +302,7 @@ _mesa_sift_name(u3_mesa_name* nam_u, c3_y* buf_y, c3_w len_w) u3l_log("name: init"); } else { - c3_y fag_y = _mesa_bytes_of_chub_tag(met_u.gaf_y); + c3_y fag_y = _mesa_bytes_of_chub_tag(met_u.gaf_y) << 3; CHECK_BOUNDS(len_w, cur_w + fag_y); for ( int i = 0; i < fag_y; i++ ) { nam_u->fra_d |= (buf_y[cur_w] << (8*i)); @@ -338,8 +338,8 @@ _mesa_sift_data(u3_mesa_data* dat_u, c3_y* buf_y, c3_w len_w) CHECK_BOUNDS(len_w, cur_w + 1); c3_y met_y = buf_y[cur_w]; met_u.bot_y = (met_y >> 0) & 0x3; - met_u.aut_o = (met_y >> 2) & 0x1; - met_u.auv_o = (met_y >> 3) & 0x1; + met_u.auv_o = (met_y >> 2) & 0x1; + met_u.aut_o = (met_y >> 3) & 0x1; met_u.men_y = (met_y >> 6) & 0x3; cur_w += 1; @@ -554,7 +554,7 @@ mesa_sift_pact(u3_mesa_pact* pac_u, c3_y* buf_y, c3_w len_w) mesa_sift_head(buf_y, &pac_u->hed_u); buf_y += pre_w; - // cur_w += pre_w; // XX add the prelude later + cur_w += pre_w; res_w -= pre_w; // u3l_log("pac_u->hed_u.typ_y typ_y %u", pac_u->hed_u.typ_y); @@ -580,7 +580,7 @@ mesa_sift_pact(u3_mesa_pact* pac_u, c3_y* buf_y, c3_w len_w) } { - c3_w mug_w = u3r_mug_bytes(buf_y, cur_w); + c3_w mug_w = u3r_mug_bytes(buf_y, cur_w - pre_w); // XX mug_w &= 0xFFFFF; if ( mug_w != pac_u->hed_u.mug_w ) { @@ -599,7 +599,7 @@ mesa_sift_pact(u3_mesa_pact* pac_u, c3_y* buf_y, c3_w len_w) return 0; } else { - return cur_w + pre_w; + return cur_w; } } @@ -666,7 +666,7 @@ _mesa_etch_name(c3_y* buf_y, u3_mesa_name* nam_u) buf_y[cur_w] = nam_u->boq_y; cur_w++; - c3_y fra_y = met_u.gaf_y + 1; + c3_y fra_y = ( met_u.gaf_y + 1 ) << 3; for( int i = 0; i < fra_y; i++ ) { buf_y[cur_w] = (nam_u->fra_d >> (8*i)) & 0xff; cur_w++; From 84c4c647d918deef162a48e254d99147048e4b47 Mon Sep 17 00:00:00 2001 From: lukechampine Date: Fri, 9 Aug 2024 13:52:54 -0400 Subject: [PATCH 343/430] mesa: distinguish between total bytes and total fragments --- pkg/vere/io/mesa.c | 72 +++++++++++++++++++++-------------------- pkg/vere/io/mesa/mesa.h | 2 +- pkg/vere/io/mesa/pact.c | 16 ++++----- 3 files changed, 46 insertions(+), 44 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 52503b26d7..9802cc563d 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -169,7 +169,8 @@ typedef struct _u3_peer { typedef struct _u3_pend_req { u3_peer* per_u; // backpointer c3_d nex_d; // number of the next fragment to be sent - c3_d tot_d; // total number of expected bytes + c3_d tof_d; // total number of expected fragments + c3_d tob_d; // total number of expected bytes u3_auth_data aut_u; // message authenticator uv_timer_t tim_u; // timehandler c3_y* dat_y; // ((mop @ud *) lte) @@ -199,7 +200,7 @@ typedef struct _u3_mesa_line { u3_mesa_ctag typ_y; // pending or present? u3_mesa_name nam_u; // full name for data, ready to serialize u3_auth_data aut_u; // message authenticator - c3_w tot_d; // number of bytes in whole message + c3_w tob_d; // number of bytes in whole message c3_w dat_w; // size in bytes of dat_y c3_w len_w; // total allocated size, in bytes c3_y* tip_y; // initial Merkle spine, nullable @@ -304,7 +305,7 @@ _log_pend_req(u3_pend_req* req_u) } u3l_log("have: %"PRIu64, req_u->hav_d); u3l_log("next: %"PRIu64, req_u->nex_d); - u3l_log("total: %" PRIu64, req_u->tot_d); + u3l_log("total: %" PRIu64, req_u->tof_d); u3l_log("gage: %c", req_u->gag_u == NULL ? 'n' : 'y'); //u3l_log("timer in: %" PRIu64 " ms", uv_timer_get_due_in(&req_u->tim_u)); } @@ -312,7 +313,7 @@ _log_pend_req(u3_pend_req* req_u) static void _log_mesa_data(u3_mesa_data dat_u) { - u3l_log("total frag: %" PRIu64, dat_u.tot_d); + u3l_log("total bytes: %" PRIu64, dat_u.tob_d); u3l_log("frag len: %u", dat_u.len_w); // u3l_log("frag: %xxx", dat_u.fra_y); } @@ -805,7 +806,7 @@ static void _mesa_handle_ack(u3_gage* gag_u, u3_pact_stat* pat_u) static inline c3_d _mesa_req_get_remaining(u3_pend_req* req_u) { - return req_u->tot_d - req_u->nex_d; + return req_u->tof_d - req_u->nex_d; } /* @@ -866,7 +867,7 @@ _mesa_req_pact_sent(u3_pend_req* req_u, u3_mesa_name* nam_u) } if ( req_u->lef_d != 0 && c3n == bitset_has(&req_u->was_u, req_u->lef_d) ) { - while (req_u->lef_d < mesa_num_leaves(req_u->tot_d)) { + while (req_u->lef_d < req_u->tof_d) { if ( c3y == bitset_has(&req_u->was_u, req_u->lef_d) ) { break; } @@ -1139,9 +1140,9 @@ _mesa_packet_timeout(uv_timer_t* tim_u); static void _update_resend_timer(u3_pend_req *req_u) { - if( req_u->tot_d == 0 || req_u->hav_d == req_u->tot_d ) { - u3l_log("bad condition tot_d: %"PRIu64" hav_d: %"PRIu64, - req_u->tot_d, req_u->hav_d); + if( req_u->tof_d == 0 || req_u->hav_d == req_u->tof_d ) { + u3l_log("bad condition tof_d: %"PRIu64" hav_d: %"PRIu64, + req_u->tof_d, req_u->hav_d); return; } // scan in flight packets, find oldest @@ -1218,9 +1219,9 @@ _mesa_req_pact_done(u3_pend_req* req_u, u3_mesa* sam_u = req_u->per_u->sam_u; // needed for the MESA_LOG macro // received past the end of the message - if ( dat_u->tot_d <= nam_u->fra_d ) { - u3l_log("strange tot_d %"PRIu64" fra_d %"PRIu64" req_u %"PRIu64, - dat_u->tot_d, nam_u->fra_d, req_u->hav_d); + if ( mesa_num_leaves(dat_u->tob_d) <= nam_u->fra_d ) { + u3l_log("strange tob_d %"PRIu64" fra_d %"PRIu64" req_u %"PRIu64, + dat_u->tob_d, nam_u->fra_d, req_u->hav_d); MESA_LOG(sam_u, STRANGE); // XX: is this sufficient to drop whole request return; @@ -1978,11 +1979,11 @@ _mesa_send_jumbo_pieces(u3_mesa* sam_u, u3_mesa_line* lin_u, c3_d* fra_d) u3_mesa_data* dat_u = &pac_u.pag_u.dat_u; { - dat_u->tot_d = lin_u->tot_d; + dat_u->tob_d = lin_u->tob_d; dat_u->aut_u = lin_u->aut_u; // aut_u, len_w, and fra_y vary by fragment } - c3_d mev_d = mesa_num_leaves(dat_u->tot_d); + c3_d mev_d = mesa_num_leaves(dat_u->tob_d); c3_w pro_w = lss_proof_size(mev_d); if ( pro_w > 0 ) { @@ -2062,13 +2063,13 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun res) } u3_mesa_data* dat_u = &jum_u.pag_u.dat_u; - c3_d mev_d = (dat_u->tot_d + 1023) / 1024; // leaves in message + c3_d mev_d = mesa_num_leaves(dat_u->tob_d); // leaves in message c3_w tip_w = // bytes in Merkle spine - (0 == jum_u.pag_u.nam_u.fra_d)? - 0 : - lss_proof_size(mev_d); + (mev_d > 1 && jum_u.pag_u.nam_u.fra_d == 0)? + lss_proof_size(mev_d) : + 0; c3_w dat_w = dat_u->len_w; // bytes in fragment data in this jumbo frame - c3_w lev_w = (dat_w + 1023) / 1024; // number of leaves in this frame + c3_w lev_w = mesa_num_leaves(dat_w); // number of leaves in this frame c3_w haz_w = lev_w * sizeof(lss_pair); // bytes in hash pairs c3_w len_w = tip_w + dat_w + haz_w; @@ -2076,7 +2077,7 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun res) lin_u->typ_y = CTAG_ITEM; _mesa_copy_name(&lin_u->nam_u, &jum_u.pek_u.nam_u); lin_u->aut_u = dat_u->aut_u; - lin_u->tot_d = dat_u->tot_d; + lin_u->tob_d = dat_u->tob_d; lin_u->dat_w = dat_w; lin_u->len_w = len_w; lin_u->tip_y = c3_malloc(len_w); // note: off-loom @@ -2160,7 +2161,7 @@ _mesa_request_next_fragments(u3_mesa* sam_u, c3_w nex_d = req_u->nex_d; for ( int i = 0; i < win_w; i++ ) { c3_w fra_w = nex_d + i; - if ( fra_w >= req_u->tot_d ) { + if ( fra_w >= req_u->tof_d ) { break; } nex_u->pac_u.pek_u.nam_u.fra_d = nex_d + i; @@ -2224,13 +2225,14 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) req_u->pic_u->pac_u.pek_u.nam_u.aut_o = c3n; req_u->aut_u = dat_u->aut_u; - c3_w siz_w = 1 << (pac_u->pag_u.nam_u.boq_y - 3); - u3_assert( 1024 == siz_w ); // boq_y == 13 + u3_assert( pac_u->pag_u.nam_u.boq_y == 13 ); req_u->gag_u = gag_u; - req_u->dat_y = c3_calloc(siz_w * dat_u->tot_d); - req_u->wat_u = c3_calloc(sizeof(u3_pact_stat) * dat_u->tot_d + 2 ); - req_u->tot_d = mesa_num_leaves(dat_u->tot_d); - bitset_init(&req_u->was_u, req_u->tot_d); + req_u->tob_d = dat_u->tob_d; + req_u->tof_d = mesa_num_leaves(dat_u->tob_d); // NOTE: only correct for bloq 13! + assert( req_u->tof_d != 1 ); // these should be injected directly by _mesa_hear_page + req_u->dat_y = c3_calloc(dat_u->tob_d); + req_u->wat_u = c3_calloc(sizeof(u3_pact_stat) * req_u->tof_d + 2 ); + bitset_init(&req_u->was_u, req_u->tof_d); // TODO: handle restart // u3_assert( nam_u->fra_w == 0 ); @@ -2241,7 +2243,7 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) req_u->old_d = 0; req_u->ack_d = 0; - c3_w pof_w = lss_proof_size(req_u->tot_d); + c3_w pof_w = lss_proof_size(req_u->tof_d); lss_hash* pof_u = c3_calloc(pof_w * sizeof(lss_hash)); if ( dat_u->len_w != pof_w*sizeof(lss_hash) ) { return; // TODO: handle like other auth failures @@ -2252,7 +2254,7 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) lss_hash root; lss_root(root, pof_u, pof_w); req_u->los_u = c3_calloc(sizeof(lss_verifier)); - lss_verifier_init(req_u->los_u, 0, req_u->tot_d, pof_u); + lss_verifier_init(req_u->los_u, 0, req_u->tof_d, pof_u); c3_free(pof_u); req_u = _mesa_put_request(sam_u, nam_u, req_u); @@ -2443,8 +2445,8 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) // process incoming response to ourselves // TODO: memory management, maybe free pict and pact - // if single-fragment message, inject directly into Arvo - c3_d lev_d = mesa_num_leaves(pac_u->pag_u.dat_u.tot_d); + // if single-leaf message, inject directly into Arvo + c3_d lev_d = mesa_num_leaves(pac_u->pag_u.dat_u.tob_d); if ( 1 == lev_d ) { u3_noun cad; { @@ -2513,9 +2515,9 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) if ( c3y == done_with_jumbo_frame ) { u3_noun cad; - u3l_log(" received last packet, tot_d: %llu", req_u->tot_d); + u3l_log(" received last packet, tot_d: %llu", req_u->tof_d); c3_d now_d = _get_now_micros(); - u3l_log("%llu kilobytes took %f ms", req_u->tot_d, (now_d - sam_u->tim_d)/1000.0); + u3l_log("%llu kilobytes took %f ms", req_u->tof_d, (now_d - sam_u->tim_d)/1000.0); { // construct jumbo frame @@ -2523,7 +2525,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) u3_noun pac; { pac_u->pag_u.nam_u.boq_y = boq_y; - pac_u->pag_u.dat_u.tot_d = req_u->tot_d; + pac_u->pag_u.dat_u.tob_d = req_u->tob_d; pac_u->pag_u.nam_u.fra_d = (req_u->hav_d >> boq_y); pac_u->pag_u.dat_u.len_w += (1024 * (lev_d - 1)); pac_u->pag_u.dat_u.fra_y = req_u->dat_y; @@ -2722,7 +2724,7 @@ _mesa_hear_poke(u3_mesa_pict* pic_u, u3_lane* lan_u) ovo = u3_auto_plan(&sam_u->car_u, ovo); // XX check request state for *payload* (in-progress duplicate) - assert(pac_u->pok_u.dat_u.tot_d); + assert(pac_u->pok_u.dat_u.tob_d); u3_mesa_lane_cb_data* dat_u = c3_malloc(sizeof(u3_mesa_lane_cb_data)); { dat_u->nam_u = c3_malloc(sizeof(u3_mesa_name)); diff --git a/pkg/vere/io/mesa/mesa.h b/pkg/vere/io/mesa/mesa.h index c65ddc634d..afead2567a 100644 --- a/pkg/vere/io/mesa/mesa.h +++ b/pkg/vere/io/mesa/mesa.h @@ -76,7 +76,7 @@ typedef struct _u3_auth_data { } u3_auth_data; typedef struct _u3_mesa_data { - c3_d tot_d; // total bytes in message + c3_d tob_d; // total bytes in message u3_auth_data aut_u; // authentication c3_w len_w; // fragment length c3_y* fra_y; // fragment diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index e691218613..d726092e81 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -68,8 +68,8 @@ log_name(u3_mesa_name* nam_u) static void _log_data(u3_mesa_data* dat_u) { - fprintf(stderr, "tot_d: %" PRIu64 " len_w: %u ", - dat_u->tot_d, dat_u->len_w); + fprintf(stderr, "tob_d: %" PRIu64 " len_w: %u ", + dat_u->tob_d, dat_u->len_w); switch ( dat_u->aut_u.typ_e ) { case AUTH_SIGN: { @@ -345,12 +345,12 @@ _mesa_sift_data(u3_mesa_data* dat_u, c3_y* buf_y, c3_w len_w) c3_y tot_y = _mesa_bytes_of_chub_tag(met_u.bot_y); CHECK_BOUNDS(len_w, cur_w + tot_y); - dat_u->tot_d = 0; + dat_u->tob_d = 0; for( int i = 0; i < tot_y; i++ ) { - dat_u->tot_d |= (buf_y[cur_w] << (8*i)); + dat_u->tob_d |= (buf_y[cur_w] << (8*i)); cur_w++; } - u3l_log("dat_u->tot_d %"PRIu64, dat_u->tot_d); + u3l_log("dat_u->tob_d %"PRIu64, dat_u->tob_d); if ( c3y == met_u.aut_o ) { if ( c3y == met_u.auv_o ) { @@ -698,7 +698,7 @@ _mesa_etch_data(c3_y* buf_y, u3_mesa_data* dat_u) case AUTH_NONE: aut_y = 2; break; case AUTH_PAIR: aut_y = 3; break; } - c3_y bot_y = _mesa_make_chub_tag(dat_u->tot_d); + c3_y bot_y = _mesa_make_chub_tag(dat_u->tob_d); c3_y nel_y = _mesa_met3_w(dat_u->len_w); c3_y men_y = (3 >= nel_y) ? nel_y : 3; buf_y[cur_w] = (bot_y & 0x3) << 0 @@ -708,7 +708,7 @@ _mesa_etch_data(c3_y* buf_y, u3_mesa_data* dat_u) c3_y tot_y = _mesa_bytes_of_chub_tag(bot_y); for (int i = 0; i < tot_y; i++ ) { - buf_y[cur_w] = (dat_u->tot_d >> (8 * i)) & 0xFF; + buf_y[cur_w] = (dat_u->tob_d >> (8 * i)) & 0xFF; cur_w++; } switch ( dat_u->aut_u.typ_e ) { @@ -848,7 +848,7 @@ _mesa_size_data(u3_mesa_data* dat_u) c3_w siz_w = 1; u3_mesa_data_meta met_u; - met_u.bot_y = _mesa_make_chub_tag(dat_u->tot_d); + met_u.bot_y = _mesa_make_chub_tag(dat_u->tob_d); siz_w += _mesa_bytes_of_chub_tag(met_u.bot_y); switch ( dat_u->aut_u.typ_e ) { From bfc69c79b3ca869258845c478fced44dc4bd443d Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Fri, 9 Aug 2024 19:01:37 -0400 Subject: [PATCH 344/430] mesa: add packet serialization roundtrip checks --- pkg/vere/io/mesa.c | 172 +++++++++++++++++++++++++++++++++++++--- pkg/vere/io/mesa/pact.c | 22 +++-- 2 files changed, 176 insertions(+), 18 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 9802cc563d..357b210355 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -13,10 +13,12 @@ #include #include #include +#include #include #include #include #include +#include #include #include #include "lss.h" @@ -24,6 +26,7 @@ c3_o dop_o = c3n; #define MESA_DEBUG c3y +#define MESA_ROUNDTRIP c3y //#define MESA_TEST #define RED_TEXT "\033[0;31m" #define DEF_TEXT "\033[0m" @@ -242,6 +245,131 @@ get_millis() { // Convert the microseconds to milliseconds by dividing by 1000 } +// assertion helpers named by aura tag +static void +_assert_eq_f(c3_o a, c3_o b) +{ + if ( a != b ) { + u3l_log("%s != %s", __(a)? "&" : "|", __(b)? "&" : "|"); + u3m_bail(c3__oops); + } +} + +static void +_assert_eq_udG(c3_d a, c3_d b) +{ + if ( a != b ) { + u3l_log("%"PRIu64" != %"PRIu64, a, b); + u3m_bail(c3__oops); + } +} + +static void +_assert_eq_uxF(c3_w a, c3_w b) +{ + if ( a != b ) { + u3l_log("0x%08x != 0x%08x", a, b); + u3m_bail(c3__oops); + } +} + +static void +_assert_eq_uxG(c3_d a, c3_d b) +{ + if ( a != b ) { + u3l_log("0x%016llx != 0x%016llx", a, b); + u3m_bail(c3__oops); + } +} + +static void +_mesa_check_heads_equal(u3_mesa_head* hed_u, u3_mesa_head* hod_u) +{ + _assert_eq_udG(hed_u->hop_y, hed_u->hop_y); + _assert_eq_uxF(hed_u->mug_w, hed_u->mug_w); + _assert_eq_udG(hed_u->nex_y, hed_u->nex_y); + _assert_eq_udG(hed_u->pro_y, hed_u->pro_y); + _assert_eq_udG(hed_u->typ_y, hed_u->typ_y); +} + +static void +_mesa_check_names_equal(u3_mesa_name* nam_u, u3_mesa_name* nom_u) +{ + u3_assert( u3_ships_equal(nam_u->her_u, nom_u->her_u) ); + _assert_eq_udG(nam_u->rif_w, nom_u->rif_w); + _assert_eq_udG(nam_u->boq_y, nom_u->boq_y); + _assert_eq_f(nam_u->nit_o, nom_u->nit_o); + _assert_eq_f(nam_u->aut_o, nom_u->aut_o); + _assert_eq_udG(nam_u->fra_d, nom_u->fra_d); + _assert_eq_udG(nam_u->pat_s, nom_u->pat_s); + u3_assert( 0 == memcmp(nam_u->pat_c, nom_u->pat_c, nam_u->pat_s + 1) ); +} + +static void +_mesa_check_auth_datas_equal(u3_auth_data* aut_u, u3_auth_data* aot_u) +{ + _assert_eq_udG(aut_u->typ_e, aot_u->typ_e); + switch ( aut_u->typ_e ) { + case AUTH_SIGN: { + u3_assert( 0 == memcmp(aut_u->sig_y, aot_u->sig_y, 64) ); + } break; + case AUTH_HMAC: { + u3_assert( 0 == memcmp(aut_u->mac_y, aot_u->mac_y, 32) ); + } break; + case AUTH_NONE: {} break; + case AUTH_PAIR: { + u3_assert( 0 == memcmp(aut_u->has_y, aot_u->has_y, 64) ); + } break; + default: u3_assert(!"unreachable"); + } +} + +static void +_mesa_check_datas_equal(u3_mesa_data* dat_u, u3_mesa_data* dot_u) +{ + _assert_eq_udG(dat_u->tob_d, dot_u->tob_d); + _mesa_check_auth_datas_equal(&dat_u->aut_u, &dot_u->aut_u); + _assert_eq_udG(dat_u->len_w, dot_u->len_w); + u3_assert( 0 == memcmp(dat_u->fra_y, dot_u->fra_y, dat_u->len_w) ); +} + +static void +_mesa_check_etch(u3_mesa_pact* pac_u, c3_y* buf_y, c3_w len_w) +{ + u3_mesa_pact poc_u; + c3_w lon_w = mesa_sift_pact(&poc_u, buf_y, len_w); + _assert_eq_udG(lon_w, len_w); + + _mesa_check_heads_equal(&poc_u.hed_u, &pac_u->hed_u); + + switch ( poc_u.hed_u.typ_y ) { + case PACT_PEEK: { + _mesa_check_names_equal(&poc_u.pek_u.nam_u, &pac_u->pek_u.nam_u); + } break; + case PACT_POKE: { + _mesa_check_names_equal(&poc_u.pok_u.nam_u, &pac_u->pok_u.nam_u); + _mesa_check_names_equal(&poc_u.pok_u.pay_u, &pac_u->pok_u.pay_u); + _mesa_check_datas_equal(&poc_u.pok_u.dat_u, &pac_u->pok_u.dat_u); + } break; + case PACT_PAGE: { + _mesa_check_names_equal(&poc_u.pag_u.nam_u, &pac_u->pag_u.nam_u); + _mesa_check_datas_equal(&poc_u.pag_u.dat_u, &pac_u->pag_u.dat_u); + } break; + default: u3_assert(!"unreachable"); + } + mesa_free_pact(&poc_u); +} + +static void +_mesa_check_sift(u3_mesa_pact* pac_u, c3_y* buf_y, c3_w len_w) +{ + c3_y* bof_y = c3_calloc(len_w); + c3_w lon_w = mesa_etch_pact(bof_y, pac_u); + _assert_eq_udG( lon_w, len_w ); + u3_assert( 0 == memcmp(bof_y, buf_y, len_w) ); + c3_free(bof_y); +} + static void _log_buf(c3_y* buf_y, c3_w len_w) { @@ -993,6 +1121,9 @@ static void _mesa_send(u3_mesa_pict* pic_u, u3_lane* lan_u) c3_y* buf_y = c3_calloc(PACT_SIZE); c3_w siz_w = mesa_etch_pact(buf_y, &pic_u->pac_u); + #ifdef MESA_ROUNDTRIP + _mesa_check_etch(&pic_u->pac_u, buf_y, siz_w); + #endif _mesa_send_buf(sam_u, *lan_u, buf_y, siz_w); } @@ -1118,6 +1249,9 @@ _try_resend(u3_pend_req* req_u, c3_d ack_d) pac_u->pek_u.nam_u.fra_d = i_d; c3_w siz_w = mesa_etch_pact(buf_y, pac_u); + #ifdef MESA_ROUNDTRIP + _mesa_check_etch(pac_u, buf_y, siz_w); + #endif if ( 0 == siz_w ) { u3_assert(!"failed to etch"); } @@ -1650,6 +1784,9 @@ _mesa_ef_send(u3_mesa* sam_u, u3_noun las, u3_noun pac) u3_mesa_pact pac_u; c3_w res_w = mesa_sift_pact(&pac_u, buf_y, len_w); + #ifdef MESA_DEBUG + _mesa_check_sift(&pac_u, buf_y, res_w); + #endif if ( PACT_PAGE == hed_u.typ_y ) { u3_weak pin = _mesa_get_pit(sam_u, &pac_u.pek_u.nam_u); @@ -1910,11 +2047,10 @@ _mesa_send_pact(u3_mesa* sam_u, { c3_y buf_y[PACT_SIZE]; c3_w len_w = mesa_etch_pact(buf_y, tac_u); - u3_mesa_pact paco; - // XX siftting after etching fails - // - u3l_log("THE LENGTH %u", len_w); - mesa_sift_pact(&paco, buf_y, len_w); + + #ifdef MESA_DEBUG + _mesa_check_etch(tac_u, buf_y, len_w); + #endif _mesa_send_bufs(sam_u, per_u, buf_y, len_w, u3k(las)); u3z(las); } @@ -2053,14 +2189,18 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun res) { u3a_atom* pat_u = u3a_to_ptr(pac); u3_mesa_pact jum_u; - if ( 0 == mesa_sift_pact(&jum_u, - (c3_y*)pat_u->buf_w, - pat_u->len_w << 2) ) { + c3_w sif_w = mesa_sift_pact(&jum_u, + (c3_y*)pat_u->buf_w, + pat_u->len_w << 2); + if ( 0 == sif_w ) { u3l_log("mesa: jumbo frame parse failure"); log_pact(pac_u); u3z(res); return; } + #ifdef MESA_ROUNDTRIP + _mesa_check_sift(&jum_u, (c3_y*)pat_u->buf_w, sif_w); + #endif u3_mesa_data* dat_u = &jum_u.pag_u.dat_u; c3_d mev_d = mesa_num_leaves(dat_u->tob_d); // leaves in message @@ -2455,7 +2595,10 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) // XX should just preserve input buffer u3i_slab sab_u; u3i_slab_init(&sab_u, 3, PACT_SIZE); - c3_w cur_w = mesa_etch_pact(sab_u.buf_y, pac_u); + c3_w res_w = mesa_etch_pact(sab_u.buf_y, pac_u); + #ifdef MESA_ROUNDTRIP + _mesa_check_etch(pac_u, sab_u.buf_y, res_w); + #endif cad = u3nt(c3__heer, lan, u3i_slab_mint(&sab_u)); } @@ -2533,6 +2676,9 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) c3_y* buf_y = c3_calloc(mesa_size_pact(pac_u)); c3_w res_w = mesa_etch_pact(buf_y, pac_u); + #ifdef MESA_ROUNDTRIP + _mesa_check_etch(pac_u, buf_y, res_w); + #endif pac = u3i_bytes(res_w, buf_y); c3_free(buf_y); } @@ -2715,7 +2861,10 @@ _mesa_hear_poke(u3_mesa_pict* pic_u, u3_lane* lan_u) u3i_slab_init(&sab_u, 3, PACT_SIZE); // XX should just preserve input buffer - mesa_etch_pact(sab_u.buf_y, pac_u); + c3_w res_w = mesa_etch_pact(sab_u.buf_y, pac_u); + #ifdef MESA_ROUNDTRIP + _mesa_check_etch(pac_u, sab_u.buf_y, res_w); + #endif cad = u3nt(c3__heer, lan, u3i_slab_mint(&sab_u)); } @@ -2763,6 +2912,9 @@ _mesa_hear(u3_mesa* sam_u, pic_u->sam_u = sam_u; c3_w lin_w = mesa_sift_pact(&pic_u->pac_u, hun_y, len_w); + #ifdef MESA_ROUNDTRIP + _mesa_check_sift(&pic_u->pac_u, hun_y, lin_w); + #endif if ( lin_w == 0 ) { // MESA_LOG(sam_u, SERIAL) diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index d726092e81..3fc2e4b776 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -1,4 +1,5 @@ #include "mesa.h" +#include #include // only need for tests, can remove #include "vere.h" @@ -278,6 +279,9 @@ _mesa_sift_name(u3_mesa_name* nam_u, c3_y* buf_y, c3_w len_w) met_u.gaf_y = (met_y >> 6) & 0x3; cur_w += 1; + // XX ?:(=(1 tau.c) %auth %data) + nam_u->aut_o = ( met_u.tau_y ) ? c3y : c3n; + c3_y her_y = 2 << met_u.ran_y; CHECK_BOUNDS(len_w, cur_w + her_y); u3_ship_of_bytes(nam_u->her_u, her_y, buf_y + cur_w); @@ -302,7 +306,7 @@ _mesa_sift_name(u3_mesa_name* nam_u, c3_y* buf_y, c3_w len_w) u3l_log("name: init"); } else { - c3_y fag_y = _mesa_bytes_of_chub_tag(met_u.gaf_y) << 3; + c3_y fag_y = _mesa_bytes_of_chub_tag(met_u.gaf_y); CHECK_BOUNDS(len_w, cur_w + fag_y); for ( int i = 0; i < fag_y; i++ ) { nam_u->fra_d |= (buf_y[cur_w] << (8*i)); @@ -311,9 +315,6 @@ _mesa_sift_name(u3_mesa_name* nam_u, c3_y* buf_y, c3_w len_w) u3l_log("name: fag_y: %u nam_u->fra_d %"PRIu64, fag_y, nam_u->fra_d); } - // XX ?:(=(1 tau.c) %auth %data) - nam_u->aut_o = ( met_u.tau_y ) ? c3y : c3n; - CHECK_BOUNDS(len_w, cur_w + 2); nam_u->pat_s = buf_y[cur_w] | (buf_y[cur_w + 1] << 8); @@ -666,10 +667,15 @@ _mesa_etch_name(c3_y* buf_y, u3_mesa_name* nam_u) buf_y[cur_w] = nam_u->boq_y; cur_w++; - c3_y fra_y = ( met_u.gaf_y + 1 ) << 3; - for( int i = 0; i < fra_y; i++ ) { - buf_y[cur_w] = (nam_u->fra_d >> (8*i)) & 0xff; - cur_w++; + if ( met_u.nit_y ) { + // init packet + } + else { + c3_y fag_y = _mesa_bytes_of_chub_tag(met_u.gaf_y); + for( int i = 0; i < fag_y; i++ ) { + buf_y[cur_w] = (nam_u->fra_d >> (8*i)) & 0xff; + cur_w++; + } } // path length From cbd91cdefedcf5408448e2e16b307174d93591eb Mon Sep 17 00:00:00 2001 From: lukechampine Date: Fri, 9 Aug 2024 19:02:45 -0400 Subject: [PATCH 345/430] mesa: restore nit_o field --- pkg/vere/io/mesa.c | 13 +++++++++---- pkg/vere/io/mesa/mesa.h | 1 + pkg/vere/io/mesa/pact.c | 14 ++++++++++---- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 357b210355..5dcc38f6d0 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1652,7 +1652,9 @@ _name_to_scry(u3_mesa_name* nam_u) u3_noun fag = _dire_etch_ud(nam_u->fra_d); u3_noun pax = _mesa_encode_path(nam_u->pat_s, (c3_y*)nam_u->pat_c); - u3_noun wer = u3nt(nam_u->aut_o == c3y ? c3__auth : c3__data, fag, pax); + u3_noun wer = nam_u->nit_o == c3y + ? u3nc(c3__init, pax) + : u3nt(nam_u->aut_o == c3y ? c3__auth : c3__data, fag, pax); u3_noun res = u3nc(c3__mess, u3nq(rif, c3__pact, boq, u3nc(c3__etch, wer))); @@ -2005,8 +2007,9 @@ _name_to_jumbo_scry(u3_mesa_name* nam_u) u3_noun boq = _dire_etch_ud(31); // XX make configurable u3_noun fag = _dire_etch_ud(0); // XX 1 u3_noun pax = _mesa_encode_path(nam_u->pat_s, (c3_y*)nam_u->pat_c); - // XX TODO: scry for %auth when necessary - u3_noun wer = u3nt(c3__data, fag, pax); + u3_noun wer = nam_u->nit_o == c3y + ? u3nc(c3__init, pax) + : u3nt(nam_u->aut_o == c3y ? c3__auth : c3__data, fag, pax); u3_noun res = u3nc(c3__mess, u3nq(rif, c3__pact, boq, u3nc(c3__etch, wer))); @@ -2063,6 +2066,7 @@ _mesa_send_leaf(u3_mesa* sam_u, { u3_mesa_name* nam_u = &pac_u->pag_u.nam_u; u3_mesa_data* dat_u = &pac_u->pag_u.dat_u; + nam_u->nit_o = __(fra_d == 0); nam_u->fra_d = fra_d; c3_d i_d = fra_d - (lin_u->nam_u.fra_d * (1 << u3_Host.ops_u.jum_y)); c3_w cur_w = i_d * 1024; @@ -2122,7 +2126,7 @@ _mesa_send_jumbo_pieces(u3_mesa* sam_u, u3_mesa_line* lin_u, c3_d* fra_d) c3_d mev_d = mesa_num_leaves(dat_u->tob_d); c3_w pro_w = lss_proof_size(mev_d); - if ( pro_w > 0 ) { + if ( c3y == nam_u->nit_o && pro_w > 0 ) { u3_weak pin = _mesa_get_pit(sam_u, nam_u); if ( u3_none != pin ) { #ifdef MESA_DEBUG @@ -2363,6 +2367,7 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) req_u->pic_u->pac_u.hed_u.pro_y = MESA_VER; memcpy(&req_u->pic_u->pac_u.pek_u.nam_u, nam_u, sizeof(u3_mesa_name)); // XX req_u->pic_u->pac_u.pek_u.nam_u.aut_o = c3n; + req_u->pic_u->pac_u.pek_u.nam_u.nit_o = c3n; req_u->aut_u = dat_u->aut_u; u3_assert( pac_u->pag_u.nam_u.boq_y == 13 ); diff --git a/pkg/vere/io/mesa/mesa.h b/pkg/vere/io/mesa/mesa.h index afead2567a..3d9da0754a 100644 --- a/pkg/vere/io/mesa/mesa.h +++ b/pkg/vere/io/mesa/mesa.h @@ -46,6 +46,7 @@ typedef struct _u3_mesa_name { u3_ship her_u; c3_w rif_w; c3_y boq_y; + c3_o nit_o; c3_o aut_o; c3_d fra_d; c3_s pat_s; diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index 3fc2e4b776..d40a796782 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -639,10 +639,16 @@ _mesa_etch_name(c3_y* buf_y, u3_mesa_name* nam_u) met_u.ran_y = _mesa_rank(nam_u->her_u); met_u.rif_y = safe_dec(_mesa_met3_w(nam_u->rif_w)); - met_u.nit_y = 0; - met_u.tau_y = (c3y == nam_u->aut_o) ? 1 : 0; - met_u.gaf_y = _mesa_make_chub_tag(nam_u->fra_d); - + if ( c3y == nam_u->nit_o ) { + met_u.nit_y = 1; + met_u.tau_y = 0; + met_u.gaf_y = 0; + } + else { + met_u.nit_y = 0; + met_u.tau_y = (c3y == nam_u->aut_o) ? 1 : 0; + met_u.gaf_y = _mesa_make_chub_tag(nam_u->fra_d); + } c3_y met_y = (met_u.ran_y & 0x3) << 0 ^ (met_u.rif_y & 0x3) << 2 ^ (met_u.nit_y & 0x1) << 4 From e4cc13d79c73d1ba989b72aef662d820a97bd8d1 Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Fri, 9 Aug 2024 22:59:31 -0400 Subject: [PATCH 346/430] mesa: fix serialization roundtripping, -hi goes through again --- pkg/vere/io/mesa/pact.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index d40a796782..8b137ad2a1 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -279,7 +279,7 @@ _mesa_sift_name(u3_mesa_name* nam_u, c3_y* buf_y, c3_w len_w) met_u.gaf_y = (met_y >> 6) & 0x3; cur_w += 1; - // XX ?:(=(1 tau.c) %auth %data) + nam_u->nit_o = ( met_u.nit_y ) ? c3y : c3n; nam_u->aut_o = ( met_u.tau_y ) ? c3y : c3n; c3_y her_y = 2 << met_u.ran_y; @@ -299,10 +299,10 @@ _mesa_sift_name(u3_mesa_name* nam_u, c3_y* buf_y, c3_w len_w) nam_u->boq_y = buf_y[cur_w]; cur_w++; + nam_u->fra_d = 0; if ( met_u.nit_y ) { assert( !met_u.tau_y ); // XX init packet - nam_u->fra_d = 0; u3l_log("name: init"); } else { From 547e533928b1cacd50ac9b8ec40b00655b566255 Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Fri, 9 Aug 2024 23:17:06 -0400 Subject: [PATCH 347/430] mesa: cleanup, remove failing assertion that we sifted the same bytes as we etched --- pkg/vere/io/mesa.c | 118 +++++++++++++++++++++------------------- pkg/vere/io/mesa/pact.c | 22 +++++--- 2 files changed, 75 insertions(+), 65 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 5dcc38f6d0..181a9bc823 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -71,17 +71,12 @@ typedef struct _u3_mesa_stat { #define MESA_DESC_DUPE "dropped packet (duplicate)" #define MESA_FIELD_DUPE dup_w -#define IN_FLIGHT 10 - // routing table sentinels #define MESA_CZAR 1 // pending dns lookup #define MESA_ROUT 2 // have route // // hop enum -#define SIFT_VAR(dest, src, len) dest = 0; for(int i = 0; i < len; i++ ) { dest |= ((src + i) >> (8*i)); } -#define CHECK_BOUNDS(cur) if ( len_w < cur ) { u3l_log("mesa: failed parse (%u,%u) at line %i", len_w, cur, __LINE__); return 0; } -#define safe_dec(num) (num == 0 ? num : num - 1) #define _mesa_met3_w(a_w) ((c3_bits_word(a_w) + 0x7) >> 3) struct _u3_mesa_pact; @@ -246,69 +241,65 @@ get_millis() { } // assertion helpers named by aura tag -static void -_assert_eq_f(c3_o a, c3_o b) -{ - if ( a != b ) { - u3l_log("%s != %s", __(a)? "&" : "|", __(b)? "&" : "|"); - u3m_bail(c3__oops); +// +#define _assert_eq_f(a, b) \ + if ( a != b ) { \ + u3l_log("mesa.c:%u %s != %s", __LINE__, \ + __(a)? "&" : "|", \ + __(b)? "&" : "|"); \ + u3m_bail(c3__oops); \ + } +#define _assert_eq_udF(a, b) \ + if ( a != b ) { \ + u3l_log("mesa.c:%u %u != %u", __LINE__, a, b); \ + u3m_bail(c3__oops); \ } -} -static void -_assert_eq_udG(c3_d a, c3_d b) -{ - if ( a != b ) { - u3l_log("%"PRIu64" != %"PRIu64, a, b); - u3m_bail(c3__oops); +#define _assert_eq_udG(a, b) \ + if ( a != b ) { \ + u3l_log("mesa.c:%u %"PRIu64" != %"PRIu64, __LINE__, a, b); \ + u3m_bail(c3__oops); \ } -} -static void -_assert_eq_uxF(c3_w a, c3_w b) -{ - if ( a != b ) { - u3l_log("0x%08x != 0x%08x", a, b); - u3m_bail(c3__oops); +#define _assert_eq_uxF(a, b) \ + if ( a != b ) { \ + u3l_log("mesa.c: %u 0x%08x != 0x%08x", __LINE__, a, b); \ + u3m_bail(c3__oops); \ } -} -static void -_assert_eq_uxG(c3_d a, c3_d b) -{ - if ( a != b ) { - u3l_log("0x%016llx != 0x%016llx", a, b); - u3m_bail(c3__oops); +#define _assert_eq_uxG(a, b) \ + if ( a != b ) { \ + u3l_log("mesa.c: %u 0x%016llx != 0x%016llx", __LINE__, a, b); \ + u3m_bail(c3__oops); \ } -} static void _mesa_check_heads_equal(u3_mesa_head* hed_u, u3_mesa_head* hod_u) { - _assert_eq_udG(hed_u->hop_y, hed_u->hop_y); + _assert_eq_udF(hed_u->hop_y, hed_u->hop_y); _assert_eq_uxF(hed_u->mug_w, hed_u->mug_w); - _assert_eq_udG(hed_u->nex_y, hed_u->nex_y); - _assert_eq_udG(hed_u->pro_y, hed_u->pro_y); - _assert_eq_udG(hed_u->typ_y, hed_u->typ_y); + _assert_eq_udF(hed_u->nex_y, hed_u->nex_y); + _assert_eq_udF(hed_u->pro_y, hed_u->pro_y); + _assert_eq_udF(hed_u->typ_y, hed_u->typ_y); } static void _mesa_check_names_equal(u3_mesa_name* nam_u, u3_mesa_name* nom_u) { - u3_assert( u3_ships_equal(nam_u->her_u, nom_u->her_u) ); - _assert_eq_udG(nam_u->rif_w, nom_u->rif_w); - _assert_eq_udG(nam_u->boq_y, nom_u->boq_y); + u3_assert( __(u3_ships_equal(nam_u->her_u, nom_u->her_u)) ); + _assert_eq_udF(nam_u->rif_w, nom_u->rif_w); + _assert_eq_udF(nam_u->boq_y, nom_u->boq_y); _assert_eq_f(nam_u->nit_o, nom_u->nit_o); _assert_eq_f(nam_u->aut_o, nom_u->aut_o); _assert_eq_udG(nam_u->fra_d, nom_u->fra_d); - _assert_eq_udG(nam_u->pat_s, nom_u->pat_s); + _assert_eq_udF(nam_u->pat_s, nom_u->pat_s); u3_assert( 0 == memcmp(nam_u->pat_c, nom_u->pat_c, nam_u->pat_s + 1) ); } static void _mesa_check_auth_datas_equal(u3_auth_data* aut_u, u3_auth_data* aot_u) { - _assert_eq_udG(aut_u->typ_e, aot_u->typ_e); + _assert_eq_udF(aut_u->typ_e, aot_u->typ_e); switch ( aut_u->typ_e ) { case AUTH_SIGN: { u3_assert( 0 == memcmp(aut_u->sig_y, aot_u->sig_y, 64) ); @@ -329,45 +320,58 @@ _mesa_check_datas_equal(u3_mesa_data* dat_u, u3_mesa_data* dot_u) { _assert_eq_udG(dat_u->tob_d, dot_u->tob_d); _mesa_check_auth_datas_equal(&dat_u->aut_u, &dot_u->aut_u); - _assert_eq_udG(dat_u->len_w, dot_u->len_w); + _assert_eq_udF(dat_u->len_w, dot_u->len_w); u3_assert( 0 == memcmp(dat_u->fra_y, dot_u->fra_y, dat_u->len_w) ); } static void -_mesa_check_etch(u3_mesa_pact* pac_u, c3_y* buf_y, c3_w len_w) +_mesa_check_pacts_equal(u3_mesa_pact* pac_u, u3_mesa_pact* poc_u) { - u3_mesa_pact poc_u; - c3_w lon_w = mesa_sift_pact(&poc_u, buf_y, len_w); - _assert_eq_udG(lon_w, len_w); + _mesa_check_heads_equal(&poc_u->hed_u, &pac_u->hed_u); - _mesa_check_heads_equal(&poc_u.hed_u, &pac_u->hed_u); - - switch ( poc_u.hed_u.typ_y ) { + switch ( poc_u->hed_u.typ_y ) { case PACT_PEEK: { - _mesa_check_names_equal(&poc_u.pek_u.nam_u, &pac_u->pek_u.nam_u); + _mesa_check_names_equal(&poc_u->pek_u.nam_u, &pac_u->pek_u.nam_u); } break; case PACT_POKE: { - _mesa_check_names_equal(&poc_u.pok_u.nam_u, &pac_u->pok_u.nam_u); - _mesa_check_names_equal(&poc_u.pok_u.pay_u, &pac_u->pok_u.pay_u); - _mesa_check_datas_equal(&poc_u.pok_u.dat_u, &pac_u->pok_u.dat_u); + _mesa_check_names_equal(&poc_u->pok_u.nam_u, &pac_u->pok_u.nam_u); + _mesa_check_names_equal(&poc_u->pok_u.pay_u, &pac_u->pok_u.pay_u); + _mesa_check_datas_equal(&poc_u->pok_u.dat_u, &pac_u->pok_u.dat_u); } break; case PACT_PAGE: { - _mesa_check_names_equal(&poc_u.pag_u.nam_u, &pac_u->pag_u.nam_u); - _mesa_check_datas_equal(&poc_u.pag_u.dat_u, &pac_u->pag_u.dat_u); + _mesa_check_names_equal(&poc_u->pag_u.nam_u, &pac_u->pag_u.nam_u); + _mesa_check_datas_equal(&poc_u->pag_u.dat_u, &pac_u->pag_u.dat_u); } break; default: u3_assert(!"unreachable"); } +} + +static void +_mesa_check_etch(u3_mesa_pact* pac_u, c3_y* buf_y, c3_w len_w) +{ + u3_mesa_pact poc_u; + c3_w lon_w = mesa_sift_pact(&poc_u, buf_y, len_w); + _assert_eq_udF(lon_w, len_w); + _mesa_check_pacts_equal(&poc_u, pac_u); mesa_free_pact(&poc_u); + u3l_log("mesa: checked etch"); } static void _mesa_check_sift(u3_mesa_pact* pac_u, c3_y* buf_y, c3_w len_w) { + u3l_log("checking sift"); c3_y* bof_y = c3_calloc(len_w); c3_w lon_w = mesa_etch_pact(bof_y, pac_u); - _assert_eq_udG( lon_w, len_w ); + //_assert_eq_udF( lon_w, len_w ); + u3_mesa_pact poc_u; + u3l_log("re-sifting"); + mesa_sift_pact(&poc_u, bof_y, PACT_SIZE); // TODO jumbo frames + _mesa_check_pacts_equal(&poc_u, pac_u); + u3_assert( 0 == memcmp(bof_y, buf_y, len_w) ); c3_free(bof_y); + u3l_log("mesa: checked sift"); } static void diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index 8b137ad2a1..61345101c9 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -39,16 +39,20 @@ _log_buf(c3_y* buf_y, c3_w len_w) fprintf(stderr, "\r\n"); } +static void +_log_name_meta(u3_mesa_name_meta* met_u) +{ + u3l_log("meta"); + u3l_log("rank: %u", met_u->ran_y); + u3l_log("rift length: %u", met_u->rif_y); + u3l_log("nit: %u", met_u->nit_y); + u3l_log("tau: %u", met_u->tau_y); + u3l_log("frag num length: %u", met_u->gaf_y); +} + void log_name(u3_mesa_name* nam_u) { - // u3l_log("meta"); - // u3l_log("rank: %u", nam_u->met_u.ran_y); - // u3l_log("rift length: %u", nam_u->met_u.rif_y); - // u3l_log("nit: %u", nam_u->met_u.nit_y); - // u3l_log("tau: %u", nam_u->met_u.tau_y); - // u3l_log("frag num length: %u", nam_u->met_u.gaf_y); - c3_c* her_c; { u3_noun her = u3dc("scot", c3__p, u3_ship_to_noun(nam_u->her_u)); @@ -64,6 +68,7 @@ log_name(u3_mesa_name* nam_u) nam_u->fra_d ); c3_free(her_c); + fflush(stderr); } static void @@ -94,6 +99,7 @@ _log_data(u3_mesa_data* dat_u) } break; } fprintf(stderr, "\r\n"); + fflush(stderr); } @@ -264,7 +270,7 @@ static c3_w _mesa_sift_name(u3_mesa_name* nam_u, c3_y* buf_y, c3_w len_w) { //#ifdef MESA_DEBUG - u3l_log("mesa: sifting name %i", len_w); + u3l_log("mesa: sifting name %u", len_w); //#endif c3_w cur_w = 0; From 04ef40ff2f09dde9a12b093e529711f6a5cf1314 Mon Sep 17 00:00:00 2001 From: lukechampine Date: Sat, 10 Aug 2024 00:00:45 -0400 Subject: [PATCH 348/430] mesa: hmacs are 128 bits! --- pkg/vere/io/mesa.c | 4 ++-- pkg/vere/io/mesa/mesa.h | 2 +- pkg/vere/io/mesa/pact.c | 16 ++++++++-------- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 181a9bc823..bef0e8b1f9 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -305,7 +305,7 @@ _mesa_check_auth_datas_equal(u3_auth_data* aut_u, u3_auth_data* aot_u) u3_assert( 0 == memcmp(aut_u->sig_y, aot_u->sig_y, 64) ); } break; case AUTH_HMAC: { - u3_assert( 0 == memcmp(aut_u->mac_y, aot_u->mac_y, 32) ); + u3_assert( 0 == memcmp(aut_u->mac_y, aot_u->mac_y, 16) ); } break; case AUTH_NONE: {} break; case AUTH_PAIR: { @@ -2418,7 +2418,7 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) break; case AUTH_HMAC: typ = c3__hmac; - aut = u3dc("scot", c3__uv, u3i_bytes(32, dat_u->aut_u.mac_y)); + aut = u3dc("scot", c3__uv, u3i_bytes(16, dat_u->aut_u.mac_y)); break; default: return; // TODO: handle like other auth failures diff --git a/pkg/vere/io/mesa/mesa.h b/pkg/vere/io/mesa/mesa.h index 3d9da0754a..eca9f67b14 100644 --- a/pkg/vere/io/mesa/mesa.h +++ b/pkg/vere/io/mesa/mesa.h @@ -71,7 +71,7 @@ typedef struct _u3_auth_data { u3_mesa_auth_type typ_e; // none, traversal (none), sig, or hmac union { // c3_y sig_y[64]; // signature - c3_y mac_y[32]; // hmac + c3_y mac_y[16]; // hmac c3_y has_y[2][32]; // hashes }; } u3_auth_data; diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index 61345101c9..99db82ca89 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -85,7 +85,7 @@ _log_data(u3_mesa_data* dat_u) case AUTH_HMAC: { fprintf(stderr, "hmac: "); - _log_buf(dat_u->aut_u.mac_y, 32); + _log_buf(dat_u->aut_u.mac_y, 16); } break; case AUTH_NONE: { @@ -368,9 +368,9 @@ _mesa_sift_data(u3_mesa_data* dat_u, c3_y* buf_y, c3_w len_w) u3l_log("AUTH_SIGN"); } else { - CHECK_BOUNDS(len_w, cur_w + 32); - memcpy(dat_u->aut_u.mac_y, buf_y + cur_w, 32); - cur_w += 32; + CHECK_BOUNDS(len_w, cur_w + 16); + memcpy(dat_u->aut_u.mac_y, buf_y + cur_w, 16); + cur_w += 16; dat_u->aut_u.typ_e = AUTH_HMAC; u3l_log("AUTH_HMAC"); } @@ -736,8 +736,8 @@ _mesa_etch_data(c3_y* buf_y, u3_mesa_data* dat_u) } break; case AUTH_HMAC: { - memcpy(buf_y + cur_w, dat_u->aut_u.mac_y, 32); - cur_w += 32; + memcpy(buf_y + cur_w, dat_u->aut_u.mac_y, 16); + cur_w += 16; } break; case AUTH_NONE: { @@ -745,7 +745,7 @@ _mesa_etch_data(c3_y* buf_y, u3_mesa_data* dat_u) case AUTH_PAIR: { memcpy(buf_y + cur_w, dat_u->aut_u.has_y, 64); - cur_w += 32; + cur_w += 64; } break; } @@ -875,7 +875,7 @@ _mesa_size_data(u3_mesa_data* dat_u) } break; case AUTH_HMAC: { - siz_w += 32; + siz_w += 16; } break; case AUTH_NONE: { From ef3a96591741689b5153df78d5dad851bc9bad62 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Mon, 12 Aug 2024 19:16:14 +0200 Subject: [PATCH 349/430] mesa: reorder request updates after early returns --- pkg/vere/io/mesa.c | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index bef0e8b1f9..8f760eccae 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1371,26 +1371,6 @@ _mesa_req_pact_done(u3_pend_req* req_u, return; } - bitset_del(&req_u->was_u, nam_u->fra_d); - if ( nam_u->fra_d > req_u->ack_d ) { - req_u->ack_d = nam_u->fra_d; - } - - #ifdef MESA_DEBUG - if ( nam_u->fra_d != 0 && req_u->wat_u[nam_u->fra_d].tie_y != 1 ) { - u3l_log("received retry %"PRIu64, nam_u->fra_d); - } - #endif - - req_u->hav_d++; - - #ifdef MESA_DEBUG - u3l_log("fragment %"PRIu64" len %"PRIu64, nam_u->fra_d, req_u->hav_d); - #endif - if ( req_u->lef_d == nam_u->fra_d ) { - req_u->lef_d++; - } - lss_pair* par_u = NULL; if ( dat_u->aut_u.typ_e == AUTH_PAIR ) { // needs to be heap allocated bc will be saved if misordered @@ -1429,6 +1409,26 @@ _mesa_req_pact_done(u3_pend_req* req_u, c3_free(par_u); } + bitset_del(&req_u->was_u, nam_u->fra_d); + if ( nam_u->fra_d > req_u->ack_d ) { + req_u->ack_d = nam_u->fra_d; + } + + #ifdef MESA_DEBUG + if ( nam_u->fra_d != 0 && req_u->wat_u[nam_u->fra_d].tie_y != 1 ) { + u3l_log("received retry %"PRIu64, nam_u->fra_d); + } + #endif + + req_u->hav_d++; + + #ifdef MESA_DEBUG + u3l_log("fragment %"PRIu64" len %"PRIu64, nam_u->fra_d, req_u->hav_d); + #endif + if ( req_u->lef_d == nam_u->fra_d ) { + req_u->lef_d++; + } + u3_lane_state* sat_u; if ( 0 == hop_y && (c3n == _mesa_lanes_equal(&lan_u, &req_u->per_u->dan_u)) ) { req_u->per_u->dan_u = lan_u; From 4fad3e5887b6e756baf229dd4db4706c6d8590ad Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Mon, 12 Aug 2024 19:16:35 +0200 Subject: [PATCH 350/430] mesa: add assertion --- pkg/vere/io/mesa/pact.c | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index 99db82ca89..e13c302ed4 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -308,6 +308,7 @@ _mesa_sift_name(u3_mesa_name* nam_u, c3_y* buf_y, c3_w len_w) nam_u->fra_d = 0; if ( met_u.nit_y ) { assert( !met_u.tau_y ); + assert( !met_u.gaf_y ); // XX init packet u3l_log("name: init"); } From f06315b7cf9bbb40aa8c2a7fb71334dbb8c64935 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Mon, 12 Aug 2024 19:18:01 +0200 Subject: [PATCH 351/430] mesa: don't account for fragments in init packet size --- pkg/vere/io/mesa/pact.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index e13c302ed4..a4cb44dcf8 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -852,8 +852,10 @@ _mesa_size_name(u3_mesa_name* nam_u) siz_w += met_u.rif_y + 1; siz_w++; // bloq - met_u.gaf_y = _mesa_make_chub_tag(nam_u->fra_d); - siz_w += _mesa_bytes_of_chub_tag(met_u.gaf_y); + if ( nam_u->nit_o == c3n ) { + met_u.gaf_y = _mesa_make_chub_tag(nam_u->fra_d); + siz_w += _mesa_bytes_of_chub_tag(met_u.gaf_y); + } siz_w += 2; // path-length siz_w += nam_u->pat_s; From eae994fa79bbaafe38912a701cd4d21cfaefb645 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Mon, 12 Aug 2024 19:20:21 +0200 Subject: [PATCH 352/430] mesa: use actual length of the etched pact in _mesa_check_sift --- pkg/vere/io/mesa.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 8f760eccae..9b7b7e435e 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -366,7 +366,8 @@ _mesa_check_sift(u3_mesa_pact* pac_u, c3_y* buf_y, c3_w len_w) //_assert_eq_udF( lon_w, len_w ); u3_mesa_pact poc_u; u3l_log("re-sifting"); - mesa_sift_pact(&poc_u, bof_y, PACT_SIZE); // TODO jumbo frames + mesa_sift_pact(&poc_u, bof_y, lon_w); // TODO jumbo frames + // mesa_sift_pact(&poc_u, bof_y, PACT_SIZE); // TODO jumbo frames _mesa_check_pacts_equal(&poc_u, pac_u); u3_assert( 0 == memcmp(bof_y, buf_y, len_w) ); From dc9d4f24a0372a299868dfd54bede658d494fdef Mon Sep 17 00:00:00 2001 From: lukechampine Date: Mon, 12 Aug 2024 18:33:33 -0400 Subject: [PATCH 353/430] mesa: refactor etching/sifting --- pkg/vere/io/mesa.c | 305 ++---------- pkg/vere/io/mesa/mesa.h | 27 +- pkg/vere/io/mesa/pact.c | 1038 +++++++++++++++++++++------------------ 3 files changed, 613 insertions(+), 757 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 9b7b7e435e..819fd438ac 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -26,7 +26,6 @@ c3_o dop_o = c3n; #define MESA_DEBUG c3y -#define MESA_ROUNDTRIP c3y //#define MESA_TEST #define RED_TEXT "\033[0;31m" #define DEF_TEXT "\033[0m" @@ -240,141 +239,6 @@ get_millis() { // Convert the microseconds to milliseconds by dividing by 1000 } -// assertion helpers named by aura tag -// -#define _assert_eq_f(a, b) \ - if ( a != b ) { \ - u3l_log("mesa.c:%u %s != %s", __LINE__, \ - __(a)? "&" : "|", \ - __(b)? "&" : "|"); \ - u3m_bail(c3__oops); \ - } -#define _assert_eq_udF(a, b) \ - if ( a != b ) { \ - u3l_log("mesa.c:%u %u != %u", __LINE__, a, b); \ - u3m_bail(c3__oops); \ - } - -#define _assert_eq_udG(a, b) \ - if ( a != b ) { \ - u3l_log("mesa.c:%u %"PRIu64" != %"PRIu64, __LINE__, a, b); \ - u3m_bail(c3__oops); \ - } - -#define _assert_eq_uxF(a, b) \ - if ( a != b ) { \ - u3l_log("mesa.c: %u 0x%08x != 0x%08x", __LINE__, a, b); \ - u3m_bail(c3__oops); \ - } - -#define _assert_eq_uxG(a, b) \ - if ( a != b ) { \ - u3l_log("mesa.c: %u 0x%016llx != 0x%016llx", __LINE__, a, b); \ - u3m_bail(c3__oops); \ - } - -static void -_mesa_check_heads_equal(u3_mesa_head* hed_u, u3_mesa_head* hod_u) -{ - _assert_eq_udF(hed_u->hop_y, hed_u->hop_y); - _assert_eq_uxF(hed_u->mug_w, hed_u->mug_w); - _assert_eq_udF(hed_u->nex_y, hed_u->nex_y); - _assert_eq_udF(hed_u->pro_y, hed_u->pro_y); - _assert_eq_udF(hed_u->typ_y, hed_u->typ_y); -} - -static void -_mesa_check_names_equal(u3_mesa_name* nam_u, u3_mesa_name* nom_u) -{ - u3_assert( __(u3_ships_equal(nam_u->her_u, nom_u->her_u)) ); - _assert_eq_udF(nam_u->rif_w, nom_u->rif_w); - _assert_eq_udF(nam_u->boq_y, nom_u->boq_y); - _assert_eq_f(nam_u->nit_o, nom_u->nit_o); - _assert_eq_f(nam_u->aut_o, nom_u->aut_o); - _assert_eq_udG(nam_u->fra_d, nom_u->fra_d); - _assert_eq_udF(nam_u->pat_s, nom_u->pat_s); - u3_assert( 0 == memcmp(nam_u->pat_c, nom_u->pat_c, nam_u->pat_s + 1) ); -} - -static void -_mesa_check_auth_datas_equal(u3_auth_data* aut_u, u3_auth_data* aot_u) -{ - _assert_eq_udF(aut_u->typ_e, aot_u->typ_e); - switch ( aut_u->typ_e ) { - case AUTH_SIGN: { - u3_assert( 0 == memcmp(aut_u->sig_y, aot_u->sig_y, 64) ); - } break; - case AUTH_HMAC: { - u3_assert( 0 == memcmp(aut_u->mac_y, aot_u->mac_y, 16) ); - } break; - case AUTH_NONE: {} break; - case AUTH_PAIR: { - u3_assert( 0 == memcmp(aut_u->has_y, aot_u->has_y, 64) ); - } break; - default: u3_assert(!"unreachable"); - } -} - -static void -_mesa_check_datas_equal(u3_mesa_data* dat_u, u3_mesa_data* dot_u) -{ - _assert_eq_udG(dat_u->tob_d, dot_u->tob_d); - _mesa_check_auth_datas_equal(&dat_u->aut_u, &dot_u->aut_u); - _assert_eq_udF(dat_u->len_w, dot_u->len_w); - u3_assert( 0 == memcmp(dat_u->fra_y, dot_u->fra_y, dat_u->len_w) ); -} - -static void -_mesa_check_pacts_equal(u3_mesa_pact* pac_u, u3_mesa_pact* poc_u) -{ - _mesa_check_heads_equal(&poc_u->hed_u, &pac_u->hed_u); - - switch ( poc_u->hed_u.typ_y ) { - case PACT_PEEK: { - _mesa_check_names_equal(&poc_u->pek_u.nam_u, &pac_u->pek_u.nam_u); - } break; - case PACT_POKE: { - _mesa_check_names_equal(&poc_u->pok_u.nam_u, &pac_u->pok_u.nam_u); - _mesa_check_names_equal(&poc_u->pok_u.pay_u, &pac_u->pok_u.pay_u); - _mesa_check_datas_equal(&poc_u->pok_u.dat_u, &pac_u->pok_u.dat_u); - } break; - case PACT_PAGE: { - _mesa_check_names_equal(&poc_u->pag_u.nam_u, &pac_u->pag_u.nam_u); - _mesa_check_datas_equal(&poc_u->pag_u.dat_u, &pac_u->pag_u.dat_u); - } break; - default: u3_assert(!"unreachable"); - } -} - -static void -_mesa_check_etch(u3_mesa_pact* pac_u, c3_y* buf_y, c3_w len_w) -{ - u3_mesa_pact poc_u; - c3_w lon_w = mesa_sift_pact(&poc_u, buf_y, len_w); - _assert_eq_udF(lon_w, len_w); - _mesa_check_pacts_equal(&poc_u, pac_u); - mesa_free_pact(&poc_u); - u3l_log("mesa: checked etch"); -} - -static void -_mesa_check_sift(u3_mesa_pact* pac_u, c3_y* buf_y, c3_w len_w) -{ - u3l_log("checking sift"); - c3_y* bof_y = c3_calloc(len_w); - c3_w lon_w = mesa_etch_pact(bof_y, pac_u); - //_assert_eq_udF( lon_w, len_w ); - u3_mesa_pact poc_u; - u3l_log("re-sifting"); - mesa_sift_pact(&poc_u, bof_y, lon_w); // TODO jumbo frames - // mesa_sift_pact(&poc_u, bof_y, PACT_SIZE); // TODO jumbo frames - _mesa_check_pacts_equal(&poc_u, pac_u); - - u3_assert( 0 == memcmp(bof_y, buf_y, len_w) ); - c3_free(bof_y); - u3l_log("mesa: checked sift"); -} - static void _log_buf(c3_y* buf_y, c3_w len_w) { @@ -1123,14 +987,9 @@ static void _mesa_send_buf(u3_mesa* sam_u, u3_lane lan_u, c3_y* buf_y, c3_w len_ static void _mesa_send(u3_mesa_pict* pic_u, u3_lane* lan_u) { u3_mesa* sam_u = pic_u->sam_u; - - c3_y* buf_y = c3_calloc(PACT_SIZE); - c3_w siz_w = mesa_etch_pact(buf_y, &pic_u->pac_u); - #ifdef MESA_ROUNDTRIP - _mesa_check_etch(&pic_u->pac_u, buf_y, siz_w); - #endif - - _mesa_send_buf(sam_u, *lan_u, buf_y, siz_w); + c3_y buf_y[PACT_SIZE]; + c3_w len_w = mesa_etch_pact_to_buf(buf_y, &pic_u->pac_u); + _mesa_send_buf(sam_u, *lan_u, buf_y, len_w); } typedef struct _u3_mesa_request_data { @@ -1244,7 +1103,7 @@ _try_resend(u3_pend_req* req_u, c3_d ack_d) c3_d now_d = _get_now_micros(); u3_mesa_pact *pac_u = &req_u->pic_u->pac_u; - c3_y* buf_y = c3_calloc(PACT_SIZE); + c3_y buf_y[PACT_SIZE]; for ( c3_d i_d = req_u->lef_d; i_d < ack_d; i_d++ ) { // TODO: make fast recovery different from slow // TODO: track skip count but not dupes, since dupes are meaningless @@ -1253,17 +1112,10 @@ _try_resend(u3_pend_req* req_u, c3_d ack_d) los_o = c3y; pac_u->pek_u.nam_u.fra_d = i_d; - c3_w siz_w = mesa_etch_pact(buf_y, pac_u); - #ifdef MESA_ROUNDTRIP - _mesa_check_etch(pac_u, buf_y, siz_w); - #endif - if ( 0 == siz_w ) { - u3_assert(!"failed to etch"); - } - _mesa_send_modal(req_u->per_u, buf_y, siz_w); + c3_w len_w = mesa_etch_pact_to_buf(buf_y, pac_u); + _mesa_send_modal(req_u->per_u, buf_y, len_w); } } - c3_free(buf_y); if ( c3y == los_o ) { req_u->gag_u->sst_w = (req_u->gag_u->wnd_w / 2) + 1; @@ -1498,14 +1350,8 @@ _mesa_send_bufs(u3_mesa* sam_u, memcpy(sen_y, buf_y, len_w); _mesa_send_buf(sam_u, lan_u, sen_y, len_w); if ( per_u && (c3y == _mesa_lanes_equal(&lan_u, &per_u->dan_u)) ) { - u3l_log(" direct send"); per_u->dir_u.sen_d = _get_now_micros(); } - else { - #ifdef MESA_DEBUG - u3l_log("mesa: send indirect"); - #endif - } } } u3z(las); @@ -1682,12 +1528,6 @@ _mesa_put_pit(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_noun val) { u3_noun pax = _name_to_scry(nam_u); u3h_put(sam_u->pit_p, pax, u3k(val)); - #ifdef MESA_DEBUG - c3_c* our_c = (c3y == u3h(val))? "&" : "|"; - c3_c* las_c = (u3_nul == u3t(val))? "~" : "..."; - u3l_log("mesa: put_pit(our %s, las %s)", our_c, las_c); - log_name(nam_u); - #endif u3z(pax); u3z(val); } @@ -1779,23 +1619,14 @@ _mesa_ef_send(u3_mesa* sam_u, u3_noun las, u3_noun pac) c3_y* buf_y = c3_calloc(len_w); u3r_bytes(0, len_w, buf_y, pac); - u3_mesa_head hed_u; - mesa_sift_head(buf_y, &hed_u); - - #ifdef MESA_DEBUG - c3_c* typ_c = (PACT_PEEK == hed_u.typ_y) ? "PEEK" : - (PACT_POKE == hed_u.typ_y) ? "POKE" : - (PACT_PAGE == hed_u.typ_y) ? "PAGE" : "RESV"; - u3l_log("mesa: ef_send() %s", typ_c); - #endif - u3_mesa_pact pac_u; - c3_w res_w = mesa_sift_pact(&pac_u, buf_y, len_w); - #ifdef MESA_DEBUG - _mesa_check_sift(&pac_u, buf_y, res_w); - #endif + c3_c* err_c = mesa_sift_pact_from_buf(&pac_u, buf_y, len_w); + if ( err_c ) { + u3l_log("mesa: ef_send: sift failed: %u %s", len_w, err_c); + return; + } - if ( PACT_PAGE == hed_u.typ_y ) { + if ( PACT_PAGE == pac_u.hed_u.typ_y ) { u3_weak pin = _mesa_get_pit(sam_u, &pac_u.pek_u.nam_u); if ( u3_none == pin ) { @@ -1807,7 +1638,6 @@ _mesa_ef_send(u3_mesa* sam_u, u3_noun las, u3_noun pac) u3_noun our, las; u3x_cell(pin, &our, &las); - u3m_p("lane", las); if ( u3_nul != las ) { _mesa_send_bufs(sam_u, NULL, buf_y, len_w, u3k(las)); _mesa_del_pit(sam_u, &pac_u.pek_u.nam_u); @@ -2054,11 +1884,7 @@ _mesa_send_pact(u3_mesa* sam_u, u3_mesa_pact* tac_u) { c3_y buf_y[PACT_SIZE]; - c3_w len_w = mesa_etch_pact(buf_y, tac_u); - - #ifdef MESA_DEBUG - _mesa_check_etch(tac_u, buf_y, len_w); - #endif + c3_w len_w = mesa_etch_pact_to_buf(buf_y, tac_u); _mesa_send_bufs(sam_u, per_u, buf_y, len_w, u3k(las)); u3z(las); } @@ -2198,18 +2024,15 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun res) { u3a_atom* pat_u = u3a_to_ptr(pac); u3_mesa_pact jum_u; - c3_w sif_w = mesa_sift_pact(&jum_u, - (c3_y*)pat_u->buf_w, - pat_u->len_w << 2); - if ( 0 == sif_w ) { - u3l_log("mesa: jumbo frame parse failure"); + c3_c* err_c = mesa_sift_pact_from_buf(&jum_u, + (c3_y*)pat_u->buf_w, + pat_u->len_w << 2); + if ( err_c ) { + u3l_log("mesa: jumbo frame parse failure: %s", err_c); log_pact(pac_u); u3z(res); return; } - #ifdef MESA_ROUNDTRIP - _mesa_check_sift(&jum_u, (c3_y*)pat_u->buf_w, sif_w); - #endif u3_mesa_data* dat_u = &jum_u.pag_u.dat_u; c3_d mev_d = mesa_num_leaves(dat_u->tob_d); // leaves in message @@ -2605,11 +2428,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) // XX should just preserve input buffer u3i_slab sab_u; u3i_slab_init(&sab_u, 3, PACT_SIZE); - c3_w res_w = mesa_etch_pact(sab_u.buf_y, pac_u); - #ifdef MESA_ROUNDTRIP - _mesa_check_etch(pac_u, sab_u.buf_y, res_w); - #endif - + mesa_etch_pact_to_buf(sab_u.buf_y, pac_u); cad = u3nt(c3__heer, lan, u3i_slab_mint(&sab_u)); } @@ -2685,10 +2504,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) pac_u->pag_u.dat_u.aut_u = req_u->aut_u; c3_y* buf_y = c3_calloc(mesa_size_pact(pac_u)); - c3_w res_w = mesa_etch_pact(buf_y, pac_u); - #ifdef MESA_ROUNDTRIP - _mesa_check_etch(pac_u, buf_y, res_w); - #endif + c3_w res_w = mesa_etch_pact_to_buf(buf_y, pac_u); pac = u3i_bytes(res_w, buf_y); c3_free(buf_y); } @@ -2749,7 +2565,6 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) c3_o our_o = u3_ships_equal(pac_u->pek_u.nam_u.her_u, sam_u->pir_u->who_d); if ( c3n == our_o ) { - u3l_log(" forwarding\r\n"); _mesa_forward_request(sam_u, pic_u, lan_u); return; } @@ -2785,24 +2600,9 @@ static void _mesa_poke_news_cb(u3_ovum* egg_u, u3_ovum_news new_e) { if ( u3_ovum_done != new_e ) { - /* - #ifdef MESA_DEBUG - u3l_log("mesa: arvo poke event was not a success"); - #endif - */ return; } - u3_mesa_lane_cb_data* dat_u = egg_u->ptr_v; - u3_peer* per_u = dat_u->per_u; - - #ifdef MESA_DEBUG - c3_c* her_c = u3_ship_to_string(dat_u->nam_u->her_u); - u3l_log("mesa: %%poke processed from %s", her_c); - _log_lane(&dat_u->lan_u); - c3_free(her_c); - #endif - c3_free(dat_u); } @@ -2871,29 +2671,24 @@ _mesa_hear_poke(u3_mesa_pict* pic_u, u3_lane* lan_u) u3i_slab_init(&sab_u, 3, PACT_SIZE); // XX should just preserve input buffer - c3_w res_w = mesa_etch_pact(sab_u.buf_y, pac_u); - #ifdef MESA_ROUNDTRIP - _mesa_check_etch(pac_u, sab_u.buf_y, res_w); - #endif - + mesa_etch_pact_to_buf(sab_u.buf_y, pac_u); cad = u3nt(c3__heer, lan, u3i_slab_mint(&sab_u)); } u3_ovum* ovo = u3_ovum_init(0, c3__ames, wir, cad); ovo = u3_auto_plan(&sam_u->car_u, ovo); - // XX check request state for *payload* (in-progress duplicate) - assert(pac_u->pok_u.dat_u.tob_d); - u3_mesa_lane_cb_data* dat_u = c3_malloc(sizeof(u3_mesa_lane_cb_data)); - { - dat_u->nam_u = c3_malloc(sizeof(u3_mesa_name)); - memcpy(dat_u->nam_u, &pac_u->pek_u.nam_u, sizeof(u3_mesa_name)); - dat_u->lan_u.pip_w = lan_u->pip_w; - dat_u->lan_u.por_s = lan_u->por_s; - dat_u->per_u = per_u; - } - u3_auto_peer(ovo, dat_u, _mesa_poke_news_cb, _mesa_poke_bail_cb); - // } + // XX check request state for *payload* (in-progress duplicate) + assert(pac_u->pok_u.dat_u.tob_d); + u3_mesa_lane_cb_data* dat_u = c3_malloc(sizeof(u3_mesa_lane_cb_data)); + { + dat_u->nam_u = c3_malloc(sizeof(u3_mesa_name)); + memcpy(dat_u->nam_u, &pac_u->pek_u.nam_u, sizeof(u3_mesa_name)); + dat_u->lan_u.pip_w = lan_u->pip_w; + dat_u->lan_u.por_s = lan_u->por_s; + dat_u->per_u = per_u; + } + u3_auto_peer(ovo, dat_u, _mesa_poke_news_cb, _mesa_poke_bail_cb); } void @@ -2908,40 +2703,26 @@ _mesa_hear(u3_mesa* sam_u, c3_w len_w, c3_y* hun_y) { - u3l_log("mesa_hear() len_w %u", len_w); - u3_mesa_pict* pic_u; - c3_w pre_w; - c3_y* cur_y = hun_y; - if ( HEAD_SIZE > len_w ) { - u3l_log(" HEAD_SIZE %u", len_w); - c3_free(hun_y); + if ( c3n == mesa_is_new_pact(hun_y, len_w) ) { + u3l_log("mesa: old hear"); + _ames_hear(u3_Host.sam_u, adr_u, len_w, hun_y); return; } - pic_u = c3_calloc(sizeof(u3_mesa_pict)); + u3_mesa_pict* pic_u = c3_calloc(sizeof(u3_mesa_pict)); pic_u->sam_u = sam_u; - - c3_w lin_w = mesa_sift_pact(&pic_u->pac_u, hun_y, len_w); - #ifdef MESA_ROUNDTRIP - _mesa_check_sift(&pic_u->pac_u, hun_y, lin_w); - #endif - - if ( lin_w == 0 ) { - // MESA_LOG(sam_u, SERIAL) - // c3_free(hun_y); - u3l_log(" lin_w 0"); + c3_c* err_c = mesa_sift_pact_from_buf(&pic_u->pac_u, hun_y, len_w); + if ( err_c ) { + u3l_log("mesa: hear: sift failed: %s", err_c); mesa_free_pact(&pic_u->pac_u); - _ames_hear(u3_Host.sam_u, adr_u, len_w, hun_y); return; } - c3_free(hun_y); struct sockaddr_in* add_u = (struct sockaddr_in*)adr_u; u3_lane lan_u; lan_u.por_s = ntohs(add_u->sin_port); - // u3l_log("port: %s", lan_u.por_s); lan_u.pip_w = ntohl(add_u->sin_addr.s_addr); @@ -2980,13 +2761,7 @@ static void _mesa_recv_cb(uv_udp_t* wax_u, c3_free(buf_u->base); } else { - u3_mesa* mes_u = wax_u->data; - // u3l_log("IP: %x", lan_u.pip_w); - // NB: [nrd_i] will never exceed max length from _ames_alloc() - // - - _mesa_hear(mes_u, adr_u, (c3_w)nrd_i, (c3_y*)buf_u->base); - + _mesa_hear(wax_u->data, adr_u, (c3_w)nrd_i, (c3_y*)buf_u->base); } } diff --git a/pkg/vere/io/mesa/mesa.h b/pkg/vere/io/mesa/mesa.h index eca9f67b14..a52e75979e 100644 --- a/pkg/vere/io/mesa/mesa.h +++ b/pkg/vere/io/mesa/mesa.h @@ -11,8 +11,7 @@ #define HEAD_SIZE 4 // header size in bytes #define PACT_SIZE 1472 -#define MESA_COOKIE_LEN 4 -static c3_y MESA_COOKIE[4] = { 0x5e, 0x1d, 0xad, 0x51 }; +static c3_w MESA_COOKIE = 0x51ad1d5e; typedef enum _u3_mesa_ptag { PACT_RESV = 0, @@ -143,13 +142,31 @@ typedef struct _u3_mesa_pact { }; } u3_mesa_pact; +typedef struct _u3_etcher { + c3_y* buf_y; + c3_w len_w; +} u3_etcher; + +typedef struct _u3_sifter { + c3_y* buf_y; + c3_w rem_w; + c3_c* err_c; +} u3_sifter; + c3_d mesa_num_leaves(c3_d tot_d); c3_w mesa_size_pact(u3_mesa_pact* pac_u); -c3_o mesa_sift_head(c3_y buf_y[8], u3_mesa_head* hed_u); -c3_w mesa_sift_pact(u3_mesa_pact* pac_u, c3_y* buf_y, c3_w len_w); -c3_w mesa_etch_pact(c3_y* buf_y, u3_mesa_pact* pac_u); +c3_o mesa_is_new_pact(c3_y* buf_y, c3_w len_w); + +void etcher_init(u3_etcher* ech_u, c3_y* buf_y); +void sifter_init(u3_sifter* sif_u, c3_y* buf_y, c3_w len_w); +void mesa_sift_head(u3_sifter* sif_u, u3_mesa_head* hed_u); +void mesa_sift_pact(u3_sifter* sif_u, u3_mesa_pact* pac_u); +void mesa_etch_pact(u3_etcher* ech_u, u3_mesa_pact* pac_u); void mesa_free_pact(u3_mesa_pact* pac_u); +c3_w mesa_etch_pact_to_buf(c3_y* buf_y, u3_mesa_pact *pac_u); +c3_c* mesa_sift_pact_from_buf(u3_mesa_pact *pac_u, c3_y* buf_y, c3_w len_w); + void inc_hopcount(u3_mesa_head*); void log_pact(u3_mesa_pact* pac_u); diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index a4cb44dcf8..0e4156a2cf 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -1,4 +1,5 @@ #include "mesa.h" +#include #include #include // only need for tests, can remove @@ -10,10 +11,115 @@ #define DEF_TEXT "\033[0m" // endif tests -#define CHECK_BOUNDS(len_w, cur) if ( len_w < cur ) { u3l_log("mesa: failed parse (%u,%u) at line %i", len_w, cur, __LINE__); return 0; }; #define safe_dec(num) (num == 0 ? num : num - 1) #define _mesa_met3_w(a_w) ((c3_bits_word(a_w) + 0x7) >> 3) +// assertions for roundtrip tests +#define MESA_ROUNDTRIP c3y +#define _assert_eq_f(a, b) \ + if ( a != b ) { \ + u3l_log("mesa.c:%u %s != %s", __LINE__, \ + __(a)? "&" : "|", \ + __(b)? "&" : "|"); \ + u3m_bail(c3__oops); \ + } +#define _assert_eq_udF(a, b) \ + if ( a != b ) { \ + u3l_log("mesa.c:%u %u != %u", __LINE__, a, b); \ + u3m_bail(c3__oops); \ + } + +#define _assert_eq_udG(a, b) \ + if ( a != b ) { \ + u3l_log("mesa.c:%u %"PRIu64" != %"PRIu64, __LINE__, a, b); \ + u3m_bail(c3__oops); \ + } + +#define _assert_eq_uxF(a, b) \ + if ( a != b ) { \ + u3l_log("mesa.c: %u 0x%08x != 0x%08x", __LINE__, a, b); \ + u3m_bail(c3__oops); \ + } + +#define _assert_eq_uxG(a, b) \ + if ( a != b ) { \ + u3l_log("mesa.c: %u 0x%016llx != 0x%016llx", __LINE__, a, b); \ + u3m_bail(c3__oops); \ + } + +static void +_mesa_check_heads_equal(u3_mesa_head* hed_u, u3_mesa_head* hod_u) +{ + _assert_eq_udF(hed_u->hop_y, hed_u->hop_y); + _assert_eq_uxF(hed_u->mug_w, hed_u->mug_w); + _assert_eq_udF(hed_u->nex_y, hed_u->nex_y); + _assert_eq_udF(hed_u->pro_y, hed_u->pro_y); + _assert_eq_udF(hed_u->typ_y, hed_u->typ_y); +} + +static void +_mesa_check_names_equal(u3_mesa_name* nam_u, u3_mesa_name* nom_u) +{ + u3_assert( __(u3_ships_equal(nam_u->her_u, nom_u->her_u)) ); + _assert_eq_udF(nam_u->rif_w, nom_u->rif_w); + _assert_eq_udF(nam_u->boq_y, nom_u->boq_y); + _assert_eq_f(nam_u->nit_o, nom_u->nit_o); + _assert_eq_f(nam_u->aut_o, nom_u->aut_o); + _assert_eq_udG(nam_u->fra_d, nom_u->fra_d); + _assert_eq_udF(nam_u->pat_s, nom_u->pat_s); + u3_assert( 0 == memcmp(nam_u->pat_c, nom_u->pat_c, nam_u->pat_s + 1) ); +} + +static void +_mesa_check_auth_datas_equal(u3_auth_data* aut_u, u3_auth_data* aot_u) +{ + _assert_eq_udF(aut_u->typ_e, aot_u->typ_e); + switch ( aut_u->typ_e ) { + case AUTH_SIGN: { + u3_assert( 0 == memcmp(aut_u->sig_y, aot_u->sig_y, 64) ); + } break; + case AUTH_HMAC: { + u3_assert( 0 == memcmp(aut_u->mac_y, aot_u->mac_y, 16) ); + } break; + case AUTH_NONE: {} break; + case AUTH_PAIR: { + u3_assert( 0 == memcmp(aut_u->has_y, aot_u->has_y, 64) ); + } break; + default: u3_assert(!"unreachable"); + } +} + +static void +_mesa_check_datas_equal(u3_mesa_data* dat_u, u3_mesa_data* dot_u) +{ + _assert_eq_udG(dat_u->tob_d, dot_u->tob_d); + _mesa_check_auth_datas_equal(&dat_u->aut_u, &dot_u->aut_u); + _assert_eq_udF(dat_u->len_w, dot_u->len_w); + u3_assert( 0 == memcmp(dat_u->fra_y, dot_u->fra_y, dat_u->len_w) ); +} + +static void +_mesa_check_pacts_equal(u3_mesa_pact* pac_u, u3_mesa_pact* poc_u) +{ + _mesa_check_heads_equal(&poc_u->hed_u, &pac_u->hed_u); + + switch ( poc_u->hed_u.typ_y ) { + case PACT_PEEK: { + _mesa_check_names_equal(&poc_u->pek_u.nam_u, &pac_u->pek_u.nam_u); + } break; + case PACT_POKE: { + _mesa_check_names_equal(&poc_u->pok_u.nam_u, &pac_u->pok_u.nam_u); + _mesa_check_names_equal(&poc_u->pok_u.pay_u, &pac_u->pok_u.pay_u); + _mesa_check_datas_equal(&poc_u->pok_u.dat_u, &pac_u->pok_u.dat_u); + } break; + case PACT_PAGE: { + _mesa_check_names_equal(&poc_u->pag_u.nam_u, &pac_u->pag_u.nam_u); + _mesa_check_datas_equal(&poc_u->pag_u.dat_u, &pac_u->pag_u.dat_u); + } break; + default: u3_assert(!"unreachable"); + } +} + /* Logging functions */ @@ -29,7 +135,6 @@ _log_head(u3_mesa_head* hed_u) u3l_log(""); } - static void _log_buf(c3_y* buf_y, c3_w len_w) { @@ -225,424 +330,250 @@ void mesa_free_pact(u3_mesa_pact* pac_u) } } -/* deserialisation +/* serialisation */ -static void -_ames_etch_word(c3_y buf_y[4], c3_w wod_w) +void +etcher_init(u3_etcher* ech_u, c3_y* buf_y) { - buf_y[0] = wod_w & 0xff; - buf_y[1] = (wod_w >> 8) & 0xff; - buf_y[2] = (wod_w >> 16) & 0xff; - buf_y[3] = (wod_w >> 24) & 0xff; + ech_u->buf_y = buf_y; + ech_u->len_w = 0; } -static inline c3_w -_ames_sift_word(c3_y buf_y[4]) +void +sifter_init(u3_sifter* sif_u, c3_y* buf_y, c3_w len_w) { - return (buf_y[3] << 24 | buf_y[2] << 16 | buf_y[1] << 8 | buf_y[0]); + sif_u->buf_y = buf_y; + sif_u->rem_w = len_w; + sif_u->err_c = NULL; } -c3_o -mesa_sift_head(c3_y buf_y[8], u3_mesa_head* hed_u) +static void +_sift_fail(u3_sifter* sif_u, c3_c* msg_c) { - if ( memcmp(buf_y + 4, &MESA_COOKIE, MESA_COOKIE_LEN) ) { - return c3n; - } - c3_w hed_w = _ames_sift_word(buf_y); - - hed_u->nex_y = (hed_w >> 2) & 0x3; - hed_u->pro_y = (hed_w >> 4) & 0x7; - hed_u->typ_y = (hed_w >> 7) & 0x3; - hed_u->hop_y = (hed_w >> 9) & 0x7; - hed_u->mug_w = (hed_w >> 12) & 0xFFFFF; - - assert( 1 == hed_u->pro_y ); - - return c3y; - - /*if(c3o((hed_u->typ_y == PACT_PEEK), (hed_u->typ_y == PACT_POKE))) { - hed_u->ran_y = (hed_w >> 30) & 0x3; - }*/ + if ( sif_u->err_c ) { + return; + } + sif_u->err_c = msg_c; + #ifdef MESA_ROUNDTRIP + u3l_log(RED_TEXT "sift fail: %s" DEF_TEXT, msg_c); + assert(!"sift fail"); + #endif } -static c3_w -_mesa_sift_name(u3_mesa_name* nam_u, c3_y* buf_y, c3_w len_w) +static c3_y* +_etch_next(u3_etcher* ech_u, c3_w len_w) { -//#ifdef MESA_DEBUG - u3l_log("mesa: sifting name %u", len_w); -//#endif - - c3_w cur_w = 0; - u3_mesa_name_meta met_u; - - CHECK_BOUNDS(len_w, cur_w + 1); - c3_y met_y = buf_y[cur_w]; - met_u.ran_y = (met_y >> 0) & 0x3; - met_u.rif_y = (met_y >> 2) & 0x3; - met_u.nit_y = (met_y >> 4) & 0x1; - met_u.tau_y = (met_y >> 5) & 0x1; - met_u.gaf_y = (met_y >> 6) & 0x3; - cur_w += 1; - - nam_u->nit_o = ( met_u.nit_y ) ? c3y : c3n; - nam_u->aut_o = ( met_u.tau_y ) ? c3y : c3n; - - c3_y her_y = 2 << met_u.ran_y; - CHECK_BOUNDS(len_w, cur_w + her_y); - u3_ship_of_bytes(nam_u->her_u, her_y, buf_y + cur_w); - cur_w += her_y; - - c3_y rif_y = met_u.rif_y + 1; - nam_u->rif_w = 0; - CHECK_BOUNDS(len_w, cur_w + rif_y); - for( int i = 0; i < rif_y; i++ ) { - nam_u->rif_w |= (buf_y[cur_w] << (8*i)); - cur_w++; - } - - CHECK_BOUNDS(len_w, cur_w + 1); - nam_u->boq_y = buf_y[cur_w]; - cur_w++; + c3_y *res_y = ech_u->buf_y + ech_u->len_w; + ech_u->len_w += len_w; + return res_y; +} - nam_u->fra_d = 0; - if ( met_u.nit_y ) { - assert( !met_u.tau_y ); - assert( !met_u.gaf_y ); - // XX init packet - u3l_log("name: init"); +static c3_y* +_sift_next(u3_sifter* sif_u, c3_w len_w) +{ + if ( sif_u->err_c ) { + return NULL; } - else { - c3_y fag_y = _mesa_bytes_of_chub_tag(met_u.gaf_y); - CHECK_BOUNDS(len_w, cur_w + fag_y); - for ( int i = 0; i < fag_y; i++ ) { - nam_u->fra_d |= (buf_y[cur_w] << (8*i)); - cur_w++; - } - u3l_log("name: fag_y: %u nam_u->fra_d %"PRIu64, fag_y, nam_u->fra_d); + else if ( len_w > sif_u->rem_w ) { + _sift_fail(sif_u, "unexpected eof"); } - - CHECK_BOUNDS(len_w, cur_w + 2); - nam_u->pat_s = buf_y[cur_w] - | (buf_y[cur_w + 1] << 8); - cur_w += 2; - u3l_log("nam_u->pat_s %u", nam_u->pat_s); - - nam_u->pat_c = c3_calloc(nam_u->pat_s + 1); // unix string for ease of manipulation - CHECK_BOUNDS(len_w, cur_w + nam_u->pat_s); - memcpy(nam_u->pat_c, buf_y + cur_w, nam_u->pat_s); - nam_u->pat_c[nam_u->pat_s] = 0; - cur_w += nam_u->pat_s; - - return cur_w; + c3_y *res_y = sif_u->buf_y; + sif_u->buf_y += len_w; + sif_u->rem_w -= len_w; + return res_y; } -static c3_w -_mesa_sift_data(u3_mesa_data* dat_u, c3_y* buf_y, c3_w len_w) +static void +_etch_bytes(u3_etcher* ech_u, c3_y *buf_y, c3_w len_w) { - c3_w cur_w = 0; - u3_mesa_data_meta met_u; - - CHECK_BOUNDS(len_w, cur_w + 1); - c3_y met_y = buf_y[cur_w]; - met_u.bot_y = (met_y >> 0) & 0x3; - met_u.auv_o = (met_y >> 2) & 0x1; - met_u.aut_o = (met_y >> 3) & 0x1; - met_u.men_y = (met_y >> 6) & 0x3; - cur_w += 1; + c3_y *res_y = _etch_next(ech_u, len_w); + memcpy(res_y, buf_y, len_w); +} - c3_y tot_y = _mesa_bytes_of_chub_tag(met_u.bot_y); - CHECK_BOUNDS(len_w, cur_w + tot_y); - dat_u->tob_d = 0; - for( int i = 0; i < tot_y; i++ ) { - dat_u->tob_d |= (buf_y[cur_w] << (8*i)); - cur_w++; +static void +_sift_bytes(u3_sifter* sif_u, c3_y *buf_y, c3_w len_w) +{ + c3_y *res_y = _sift_next(sif_u, len_w); + if ( NULL == res_y ) { + memset(buf_y, 0, len_w); + return; } - u3l_log("dat_u->tob_d %"PRIu64, dat_u->tob_d); + memcpy(buf_y, res_y, len_w); +} - if ( c3y == met_u.aut_o ) { - if ( c3y == met_u.auv_o ) { - CHECK_BOUNDS(len_w, cur_w + 64); - memcpy(dat_u->aut_u.sig_y, buf_y + cur_w, 64); - cur_w += 64; - dat_u->aut_u.typ_e = AUTH_SIGN; - u3l_log("AUTH_SIGN"); - } - else { - CHECK_BOUNDS(len_w, cur_w + 16); - memcpy(dat_u->aut_u.mac_y, buf_y + cur_w, 16); - cur_w += 16; - dat_u->aut_u.typ_e = AUTH_HMAC; - u3l_log("AUTH_HMAC"); - } - } - else { - if ( c3y == met_u.auv_o ) { - dat_u->aut_u.typ_e = AUTH_NONE; - u3l_log("AUTH_NONE"); - } else { - CHECK_BOUNDS(len_w, cur_w + 64); - memcpy(dat_u->aut_u.has_y, buf_y + cur_w, 64); - cur_w += 64; - dat_u->aut_u.typ_e = AUTH_PAIR; - u3l_log("AUTH_PAIR"); - } - } +static c3_y* +_sift_bytes_alloc(u3_sifter* sif_u, c3_w len_w) +{ + c3_y *buf_y = c3_calloc(len_w); + _sift_bytes(sif_u, buf_y, len_w); + return buf_y; +} - c3_y nel_y = met_u.men_y; +static void +_etch_byte(u3_etcher* ech_u, c3_y val_y) +{ + _etch_next(ech_u, 1)[0] = val_y; +} - if ( 3 == nel_y ) { - CHECK_BOUNDS(len_w, cur_w + 1); - nel_y = buf_y[cur_w]; - cur_w++; - u3l_log("nel 3"); - } +static c3_y +_sift_byte(u3_sifter* sif_u) +{ + c3_y *res_y = _sift_next(sif_u, 1); + return ( NULL == res_y ) ? 0 : res_y[0]; +} - CHECK_BOUNDS(len_w, cur_w + nel_y); - dat_u->len_w = 0; - u3l_log("nel_y %u", nel_y); - for ( int i = 0; i < nel_y; i++ ) { - dat_u->len_w |= (buf_y[cur_w] << (8*i)); - cur_w++; - } +static void +_etch_short(u3_etcher* ech_u, c3_s val_s) +{ + c3_etch_short(_etch_next(ech_u, 2), val_s); +} - u3l_log("dat_u->len_w %u cur_w %u", dat_u->len_w, cur_w); - CHECK_BOUNDS(len_w, cur_w + dat_u->len_w); - dat_u->fra_y = c3_calloc(dat_u->len_w); - memcpy(dat_u->fra_y, buf_y + cur_w, dat_u->len_w); - cur_w += dat_u->len_w; +static c3_y +_sift_short(u3_sifter* sif_u) +{ + c3_y *res_y = _sift_next(sif_u, 2); + return ( NULL == res_y ) ? 0 : c3_sift_short(res_y); +} - return cur_w; +static void +_etch_word(u3_etcher* ech_u, c3_w val_w) +{ + c3_etch_word(_etch_next(ech_u, 4), val_w); } static c3_w -_mesa_sift_hop_long(u3_mesa_hop_once* hop_u, c3_y* buf_y, c3_w len_w) +_sift_word(u3_sifter* sif_u) { - c3_w cur_w = 0; - CHECK_BOUNDS(len_w, cur_w + 1); - hop_u->len_w = buf_y[cur_w]; - cur_w++; - CHECK_BOUNDS(len_w, cur_w + hop_u->len_w); - hop_u->dat_y = c3_calloc(hop_u->len_w); - memcpy(hop_u->dat_y, buf_y + cur_w, hop_u->len_w); - - return cur_w + hop_u->len_w; + c3_y *res_y = _sift_next(sif_u, 4); + return ( NULL == res_y ) ? 0 : c3_sift_word(res_y); } -static c3_w -_mesa_sift_hops(u3_mesa_page_pact* pac_u, c3_y nex_y, c3_y* buf_y, c3_w len_w) +static void +_etch_chub(u3_etcher* ech_u, c3_d val_d) { - switch ( nex_y ) { - default: { - u3_assert(!"mesa: sift invalid hop type"); - } - case HOP_NONE: return 0; - case HOP_SHORT: { - CHECK_BOUNDS(len_w, 6); - memcpy(pac_u->sot_u, buf_y, 6); - return 6; - } - case HOP_LONG: { - return _mesa_sift_hop_long(&pac_u->one_u, buf_y, len_w); - } - case HOP_MANY: { - c3_w siz_w = 0; - CHECK_BOUNDS(len_w, siz_w + 1); - pac_u->man_u.len_w = buf_y[0]; - siz_w++; + c3_etch_chub(_etch_next(ech_u, 8), val_d); +} - pac_u->man_u.dat_y = c3_calloc(sizeof(u3_mesa_hop_once) * pac_u->man_u.len_w); +static c3_d +_sift_chub(u3_sifter* sif_u) +{ + c3_y *res_y = _sift_next(sif_u, 8); + return ( NULL == res_y ) ? 0 : c3_sift_chub(res_y); +} - for( c3_w i = 0; i < pac_u->man_u.len_w; i++ ) { - siz_w += _mesa_sift_hop_long(&pac_u->man_u.dat_y[i], - buf_y + siz_w, - len_w - siz_w); - } - return siz_w; - } +static void +_etch_var_word(u3_etcher* ech_u, c3_w val_w, c3_w len_w) +{ + assert ( len_w <= 4 ); + c3_y *buf_y = _etch_next(ech_u, len_w); + for( int i = 0; i < len_w; i++ ) { + buf_y[i] |= (val_w >> (8*i)) & 0xFF; } } static c3_w -_mesa_sift_page_pact(u3_mesa_pact* pat_u, c3_y nex_y, c3_y* buf_y, c3_w len_w) +_sift_var_word(u3_sifter* sif_u, c3_w len_w) { - u3l_log("mesa: sift page pact"); - u3_mesa_page_pact* pac_u = &pat_u->pag_u; - c3_w cur_w = 0, res_w = len_w, nex_w; - - if ( !(nex_w = _mesa_sift_name(&pac_u->nam_u, buf_y + cur_w, res_w)) ) { - u3l_log("name fail"); + assert ( len_w <= 4 ); + c3_y *res_y = _sift_next(sif_u, len_w); + if ( NULL == res_y ) { return 0; } - cur_w += nex_w; - res_w -= nex_w; - - if ( !(nex_w = _mesa_sift_data(&pac_u->dat_u, buf_y + cur_w, res_w)) ) { - u3l_log("data fail"); - return 0; - } - cur_w += nex_w; - res_w -= nex_w; - - nex_w = _mesa_sift_hops(pac_u, nex_y, buf_y + cur_w, res_w); - cur_w += nex_w; - res_w -= nex_w; - - if ( cur_w > len_w ) { - u3l_log("mesa: %%page too big"); - return 0; - } - else { - return cur_w; + c3_w val_w = 0; + for( int i = 0; i < len_w; i++ ) { + val_w |= (res_y[i] << (8*i)); } + return val_w; } -static c3_w -_mesa_sift_peek_pact(u3_mesa_peek_pact* pac_u, c3_y* buf_y, c3_w len_w) +static void +_etch_var_chub(u3_etcher* ech_u, c3_d val_d, c3_w len_w) { - u3l_log("mesa: sift peek pact"); - c3_w siz_w = _mesa_sift_name(&pac_u->nam_u, buf_y, len_w); - if ( siz_w < len_w ) { - u3l_log("mesa: failed to consume entire packet"); - _log_buf(buf_y + siz_w, len_w - siz_w); - return 0; + assert ( len_w <= 8 ); + c3_y *buf_y = _etch_next(ech_u, len_w); + for( int i = 0; i < len_w; i++ ) { + buf_y[i] |= (val_d >> (8*i)) & 0xFF; } - - return siz_w; } -static c3_w -_mesa_sift_poke_pact(u3_mesa_poke_pact* pac_u, c3_y* buf_y, c3_w len_w) -{ - u3l_log("mesa: sift poke pact"); - c3_w cur_w = 0, res_w = len_w, nex_w; - // ack path - if ( !(nex_w = _mesa_sift_name(&pac_u->nam_u, buf_y + cur_w, res_w)) ) { - u3l_log("name fail"); - // u3l_log("_mesa_sift_poke_pact nex_w %u", nex_w); - return 0; - } - cur_w += nex_w; - res_w -= nex_w; - - // payload path - if ( !(nex_w = _mesa_sift_name(&pac_u->pay_u, buf_y + cur_w, res_w)) ) { - u3l_log("payload name fail"); +static c3_d +_sift_var_chub(u3_sifter* sif_u, c3_w len_w) +{ + assert ( len_w <= 8 ); + c3_y *res_y = _sift_next(sif_u, len_w); + if ( NULL == res_y ) { return 0; } - cur_w += nex_w; - res_w -= nex_w; - - // payload - if ( !(nex_w = _mesa_sift_data(&pac_u->dat_u, buf_y + cur_w, res_w)) ) { - u3l_log("data fail"); - // u3l_log("_mesa_sift_poke_pact nex_w %u", nex_w); - return 0; + c3_d val_d = 0; + for( int i = 0; i < len_w; i++ ) { + val_d |= (res_y[i] << (8*i)); } - cur_w += nex_w; - res_w -= nex_w; - - // u3l_log("_mesa_sift_poke_pact cur_w %u", cur_w); - if ( cur_w > len_w ) { - u3l_log("mesa: %%poke too big"); - return 0; - } - return cur_w; + return val_d; } -c3_w -mesa_sift_pact(u3_mesa_pact* pac_u, c3_y* buf_y, c3_w len_w) +static void +_etch_ship(u3_etcher* ech_u, u3_ship who_u, c3_y len_y) { - c3_w cur_w = 0, res_w = len_w; - c3_w pre_w = (HEAD_SIZE + MESA_COOKIE_LEN); // prelude: header + cookie - - if ( len_w < pre_w ) { - u3l_log("mesa: attempted to parse overly short packet of size %u", len_w); - } - - mesa_sift_head(buf_y, &pac_u->hed_u); - buf_y += pre_w; - cur_w += pre_w; - res_w -= pre_w; - - // u3l_log("pac_u->hed_u.typ_y typ_y %u", pac_u->hed_u.typ_y); - - switch ( pac_u->hed_u.typ_y ) { - case PACT_PEEK: { - cur_w += _mesa_sift_peek_pact(&pac_u->pek_u, buf_y, res_w); - } break; - case PACT_PAGE: { - cur_w += _mesa_sift_page_pact(pac_u, pac_u->hed_u.nex_y, buf_y, res_w); - } break; - case PACT_POKE: { - cur_w += _mesa_sift_poke_pact(&pac_u->pok_u, buf_y, res_w); - } break; - default: { - /* u3l_log("mesa: received unknown packet type"); */ - /* _log_buf(buf_y, len_w); */ - break; - } - } - if ( 0 == cur_w ) { - return 0; - } - - { - c3_w mug_w = u3r_mug_bytes(buf_y, cur_w - pre_w); // XX - mug_w &= 0xFFFFF; - - if ( mug_w != pac_u->hed_u.mug_w ) { - u3l_log("mesa: failed mug, mug_w %u " - "pac_u->hed_u.mug_w %u " - "cur_w %u " - "len_w %u", - mug_w, pac_u->hed_u.mug_w, cur_w, len_w); - /* _log_buf(buf_y, res_w); */ - /* u3l_log("frag %u", pac_u->pag_u.nam_u.fra_w); */ - return 0; - } - } + assert ( len_y <= 16 ); + u3_ship_to_bytes(who_u, len_y, _etch_next(ech_u, len_y)); +} - if ( cur_w > len_w ) { - return 0; - } - else { - return cur_w; +static void +_sift_ship(u3_sifter* sif_u, u3_ship who_u, c3_y len_y) +{ + assert ( len_y <= 16 ); + c3_y *res_y = _sift_next(sif_u, len_y); + if ( NULL == res_y ) { + who_u[0] = who_u[1] = 0; + return; } + u3_ship_of_bytes(who_u, len_y, res_y); } -/* serialisation -*/ static void -_mesa_etch_head(u3_mesa_head* hed_u, c3_y buf_y[8]) +_mesa_etch_head(u3_etcher* ech_u, u3_mesa_head* hed_u) { if ( 1 != hed_u->pro_y ) { u3l_log("etching bad head"); } - - // c3_o req_o = c3o((hed_u->typ_y == PACT_PEEK), (hed_u->typ_y == PACT_POKE)); - // c3_y siz_y = req_o ? 5 : 7; c3_w hed_w = (hed_u->nex_y & 0x3) << 2 ^ (hed_u->pro_y & 0x7) << 4 // XX constant, 1 ^ (hed_u->typ_y & 0x3) << 7 ^ (hed_u->hop_y & 0x7) << 9 ^ (hed_u->mug_w & 0xFFFFF) << 12; // XX: we don't expand hopcount if no request. Correct? - // - /*if ( c3y == req_o ) { - hed_w = hed_w ^ ((hed_u->ran_y & 0x3) << 30); - }*/ + _etch_word(ech_u, hed_w); + _etch_word(ech_u, MESA_COOKIE); +} - _ames_etch_word(buf_y, hed_w); - memcpy(buf_y + 4, MESA_COOKIE, MESA_COOKIE_LEN); +c3_o +mesa_is_new_pact(c3_y* buf_y, c3_w len_w) +{ + return __((len_w >= 8) && c3_sift_word(buf_y + 4) == MESA_COOKIE); } -static c3_w -_mesa_etch_name(c3_y* buf_y, u3_mesa_name* nam_u) +void +mesa_sift_head(u3_sifter* sif_u, u3_mesa_head* hed_u) { - c3_w cur_w = 0; - u3_mesa_name_meta met_u; + c3_w hed_w = _sift_word(sif_u); + hed_u->nex_y = (hed_w >> 2) & 0x3; + hed_u->pro_y = (hed_w >> 4) & 0x7; + hed_u->typ_y = (hed_w >> 7) & 0x3; + hed_u->hop_y = (hed_w >> 9) & 0x7; + hed_u->mug_w = (hed_w >> 12) & 0xFFFFF; + if ( 1 != hed_u->pro_y ) { + _sift_fail(sif_u, "bad protocol"); + } + if ( _sift_word(sif_u) != MESA_COOKIE ) { + _sift_fail(sif_u, "bad cookie"); + } +} +static void +_mesa_etch_name(u3_etcher *ech_u, u3_mesa_name* nam_u) +{ + u3_mesa_name_meta met_u; met_u.ran_y = _mesa_rank(nam_u->her_u); met_u.rif_y = safe_dec(_mesa_met3_w(nam_u->rif_w)); @@ -662,54 +593,62 @@ _mesa_etch_name(c3_y* buf_y, u3_mesa_name* nam_u) ^ (met_u.tau_y & 0x1) << 5 ^ (met_u.gaf_y & 0x3) << 6; - buf_y[cur_w] = met_y; - - //ship - cur_w++; + _etch_byte(ech_u, met_y); c3_y her_y = 2 << met_u.ran_y; // XX confirm - u3_ship_to_bytes(nam_u->her_u, her_y, buf_y + cur_w); - cur_w += her_y; - - // rift - c3_y rif_y = met_u.rif_y + 1; - for ( int i = 0; i < rif_y; i++) { - buf_y[cur_w] = (nam_u->rif_w >> (8*i)) & 0xff; - cur_w++; - } - - buf_y[cur_w] = nam_u->boq_y; - cur_w++; + _etch_ship(ech_u, nam_u->her_u, her_y); + _etch_var_word(ech_u, nam_u->rif_w, met_u.rif_y + 1); + _etch_byte(ech_u, nam_u->boq_y); if ( met_u.nit_y ) { // init packet } else { - c3_y fag_y = _mesa_bytes_of_chub_tag(met_u.gaf_y); - for( int i = 0; i < fag_y; i++ ) { - buf_y[cur_w] = (nam_u->fra_d >> (8*i)) & 0xff; - cur_w++; - } + _etch_var_word(ech_u, nam_u->fra_d, 1 << met_u.gaf_y); } - // path length - c3_y pat_y = 2; - for ( int i = 0; i < pat_y; i++ ) { - buf_y[cur_w] = (nam_u->pat_s >> (8*i)) & 0xff; - cur_w++; + _etch_short(ech_u, nam_u->pat_s); + _etch_bytes(ech_u, (c3_y*)nam_u->pat_c, nam_u->pat_s); +} + +static void +_mesa_sift_name(u3_sifter* sif_u, u3_mesa_name* nam_u) +{ + u3_mesa_name_meta met_u; + + c3_y met_y = _sift_byte(sif_u); + met_u.ran_y = (met_y >> 0) & 0x3; + met_u.rif_y = (met_y >> 2) & 0x3; + met_u.nit_y = (met_y >> 4) & 0x1; + met_u.tau_y = (met_y >> 5) & 0x1; + met_u.gaf_y = (met_y >> 6) & 0x3; + + nam_u->nit_o = ( met_u.nit_y ) ? c3y : c3n; + nam_u->aut_o = ( met_u.tau_y ) ? c3y : c3n; + + _sift_ship(sif_u, nam_u->her_u, 2 << met_u.ran_y); + nam_u->rif_w = _sift_var_word(sif_u, met_u.rif_y + 1); + nam_u->boq_y = _sift_byte(sif_u); + + nam_u->fra_d = 0; + if ( met_u.nit_y ) { + assert( !met_u.tau_y ); + assert( !met_u.gaf_y ); + // XX init packet + } + else { + nam_u->fra_d = _sift_var_word(sif_u, 1 << met_u.gaf_y); } - // path - memcpy(buf_y + cur_w, nam_u->pat_c, nam_u->pat_s); - cur_w += nam_u->pat_s; + nam_u->pat_s = _sift_short(sif_u); - return cur_w; + nam_u->pat_c = c3_calloc(nam_u->pat_s + 1); + _sift_bytes(sif_u, (c3_y*)nam_u->pat_c, nam_u->pat_s); + nam_u->pat_c[nam_u->pat_s] = 0; } -static c3_w -_mesa_etch_data(c3_y* buf_y, u3_mesa_data* dat_u) +static void +_mesa_etch_data(u3_etcher* ech_u, u3_mesa_data* dat_u) { - c3_w cur_w = 0; - c3_y aut_y = 0; switch ( dat_u->aut_u.typ_e ) { case AUTH_SIGN: aut_y = 0; break; @@ -720,122 +659,290 @@ _mesa_etch_data(c3_y* buf_y, u3_mesa_data* dat_u) c3_y bot_y = _mesa_make_chub_tag(dat_u->tob_d); c3_y nel_y = _mesa_met3_w(dat_u->len_w); c3_y men_y = (3 >= nel_y) ? nel_y : 3; - buf_y[cur_w] = (bot_y & 0x3) << 0 - | (aut_y & 0x3) << 2 - | (men_y & 0x3) << 6; - cur_w++; - c3_y tot_y = _mesa_bytes_of_chub_tag(bot_y); - - for (int i = 0; i < tot_y; i++ ) { - buf_y[cur_w] = (dat_u->tob_d >> (8 * i)) & 0xFF; - cur_w++; - } + _etch_byte(ech_u, (bot_y & 0x3) << 0 + | (aut_y & 0x3) << 2 + | (men_y & 0x3) << 6); + _etch_var_chub(ech_u, dat_u->tob_d, 1 << bot_y); switch ( dat_u->aut_u.typ_e ) { case AUTH_SIGN: { - memcpy(buf_y + cur_w, dat_u->aut_u.sig_y, 64); - cur_w += 64; + _etch_bytes(ech_u, dat_u->aut_u.sig_y, 64); } break; case AUTH_HMAC: { - memcpy(buf_y + cur_w, dat_u->aut_u.mac_y, 16); - cur_w += 16; + _etch_bytes(ech_u, dat_u->aut_u.mac_y, 16); } break; case AUTH_NONE: { } break; case AUTH_PAIR: { - memcpy(buf_y + cur_w, dat_u->aut_u.has_y, 64); - cur_w += 64; + _etch_bytes(ech_u, dat_u->aut_u.has_y[0], 32); + _etch_bytes(ech_u, dat_u->aut_u.has_y[1], 32); } break; } if ( 3 == men_y ) { - buf_y[cur_w] = nel_y; - cur_w++; + _etch_byte(ech_u, nel_y); } - memcpy(buf_y + cur_w, (c3_y*)&dat_u->len_w, nel_y); - cur_w += nel_y; + _etch_var_word(ech_u, dat_u->len_w, nel_y); + _etch_bytes(ech_u, dat_u->fra_y, dat_u->len_w); +} - memcpy(buf_y + cur_w, dat_u->fra_y, dat_u->len_w); - cur_w += dat_u->len_w; +static void +_mesa_sift_data(u3_sifter* sif_u, u3_mesa_data* dat_u) +{ + u3_mesa_data_meta met_u; - return cur_w; + c3_y met_y = _sift_byte(sif_u); + met_u.bot_y = (met_y >> 0) & 0x3; + met_u.auv_o = (met_y >> 2) & 0x1; + met_u.aut_o = (met_y >> 3) & 0x1; + met_u.men_y = (met_y >> 6) & 0x3; + + dat_u->tob_d = _sift_var_chub(sif_u, 1<aut_u.sig_y, 64); + dat_u->aut_u.typ_e = AUTH_SIGN; + } + else { + _sift_bytes(sif_u, dat_u->aut_u.mac_y, 16); + dat_u->aut_u.typ_e = AUTH_HMAC; + } + } + else { + if ( c3y == met_u.auv_o ) { + dat_u->aut_u.typ_e = AUTH_NONE; + } else { + _sift_bytes(sif_u, dat_u->aut_u.has_y[0], 32); + _sift_bytes(sif_u, dat_u->aut_u.has_y[1], 32); + dat_u->aut_u.typ_e = AUTH_PAIR; + } + } + + c3_y nel_y = (met_u.men_y < 3) ? met_u.men_y : _sift_byte(sif_u); + dat_u->len_w = _sift_var_word(sif_u, nel_y); + dat_u->fra_y = _sift_bytes_alloc(sif_u, dat_u->len_w); } -static c3_w -_mesa_etch_next(c3_y* buf_y, u3_mesa_hop_type nex_y, u3_mesa_page_pact* pac_u) +static void +_mesa_etch_hop_long(u3_etcher* ech_u, u3_mesa_hop_once* hop_u) { - switch ( nex_y ) { + _etch_byte(ech_u, hop_u->len_w); + _etch_bytes(ech_u, hop_u->dat_y, hop_u->len_w); +} + +static void +_mesa_sift_hop_long(u3_sifter* sif_u, u3_mesa_hop_once* hop_u) +{ + hop_u->len_w = _sift_byte(sif_u); + hop_u->dat_y = _sift_bytes_alloc(sif_u, hop_u->len_w); +} + +static void +_mesa_etch_page_pact(u3_etcher* ech_u, u3_mesa_page_pact* pag_u, u3_mesa_head* hed_u) +{ + _mesa_etch_name(ech_u, &pag_u->nam_u); + _mesa_etch_data(ech_u, &pag_u->dat_u); + + switch ( hed_u->nex_y ) { + case HOP_NONE: { + } break; case HOP_SHORT: { - memcpy(buf_y, pac_u->sot_u, 6); - return 6; + _etch_bytes(ech_u, pag_u->sot_u, 6); + } break; + case HOP_LONG: { + _mesa_etch_hop_long(ech_u, &pag_u->one_u); } break; case HOP_MANY: { - // buf_y[0] = pac_u->man_u.len_w; // XX - c3_w cur_w =0; - memcpy(buf_y + cur_w, &pac_u->man_u.len_w, 4); - for( c3_w i = 0; i < pac_u->man_u.len_w; i++ ) { - struct _u3_mesa_hop_once hop_u = pac_u->man_u.dat_y[i]; - memcpy(buf_y + cur_w, &hop_u.len_w, 4); - cur_w += 4; - memcpy(buf_y + cur_w, hop_u.dat_y, hop_u.len_w); - cur_w += hop_u.len_w; + _etch_byte(ech_u, pag_u->man_u.len_w); + for( c3_w i = 0; i < pag_u->man_u.len_w; i++ ) { + _mesa_etch_hop_long(ech_u, &pag_u->man_u.dat_y[i]); } - return cur_w; } break; default: { - return 0; + return; } break; } } -static c3_w -_mesa_etch_page_pact(c3_y* buf_y, u3_mesa_page_pact* pac_u, u3_mesa_head* hed_u) +static void +_mesa_sift_page_pact(u3_sifter* sif_u, u3_mesa_page_pact* pag_u, c3_y nex_y) { - c3_w cur_w = 0, nex_w; + _mesa_sift_name(sif_u, &pag_u->nam_u); + _mesa_sift_data(sif_u, &pag_u->dat_u); - if ( !(nex_w = _mesa_etch_name(buf_y + cur_w, &pac_u->nam_u)) ) { - return 0; + switch ( nex_y ) { + default: { + u3_assert(!"mesa: sift invalid hop type"); + } + case HOP_NONE: return; + case HOP_SHORT: { + _sift_bytes(sif_u, pag_u->sot_u, 6); + return; + } + case HOP_LONG: { + _mesa_sift_hop_long(sif_u, &pag_u->one_u); + return; + } + case HOP_MANY: { + pag_u->man_u.len_w = _sift_byte(sif_u); + pag_u->man_u.dat_y = c3_calloc(sizeof(u3_mesa_hop_once) * pag_u->man_u.len_w); + for( c3_w i = 0; i < pag_u->man_u.len_w; i++ ) { + _mesa_sift_hop_long(sif_u, &pag_u->man_u.dat_y[i]); + } + return; + } } - cur_w += nex_w; +} - if ( !(nex_w = _mesa_etch_data(buf_y + cur_w, &pac_u->dat_u)) ) { - return 0; - } - cur_w += nex_w; +static void +_mesa_etch_peek_pact(u3_etcher* ech_u, u3_mesa_peek_pact* pac_u) +{ + _mesa_etch_name(ech_u, &pac_u->nam_u); +} - nex_w = _mesa_etch_next(buf_y + cur_w, hed_u->nex_y, pac_u); +static void +_mesa_sift_peek_pact(u3_sifter* sif_u, u3_mesa_peek_pact* pac_u) +{ + _mesa_sift_name(sif_u, &pac_u->nam_u); +} - cur_w += nex_w; +static void +_mesa_etch_poke_pact(u3_etcher* ech_u, u3_mesa_poke_pact* pac_u) +{ + _mesa_etch_name(ech_u, &pac_u->nam_u); + _mesa_etch_name(ech_u, &pac_u->pay_u); + _mesa_etch_data(ech_u, &pac_u->dat_u); +} - return cur_w; +static void +_mesa_sift_poke_pact(u3_sifter* sif_u, u3_mesa_poke_pact* pac_u) +{ + _mesa_sift_name(sif_u, &pac_u->nam_u); + _mesa_sift_name(sif_u, &pac_u->pay_u); + _mesa_sift_data(sif_u, &pac_u->dat_u); } -static c3_w -_mesa_etch_poke_pact(c3_y* buf_y, u3_mesa_poke_pact* pac_u, u3_mesa_head* hed_u) +void +mesa_etch_pact(u3_etcher* ech_u, u3_mesa_pact* pac_u) { - c3_w cur_w = 0, nex_w; + // use a separate etcher to make computing the mug easier + u3_etcher pec_u; + etcher_init(&pec_u, ech_u->buf_y + ech_u->len_w + 8); - if ( !(nex_w = _mesa_etch_name(buf_y + cur_w, &pac_u->nam_u)) ) { - return 0; + switch ( pac_u->hed_u.typ_y ) { + case PACT_POKE: { + _mesa_etch_poke_pact(&pec_u, &pac_u->pok_u); + } break; + case PACT_PAGE: { + _mesa_etch_page_pact(&pec_u, &pac_u->pag_u, &pac_u->hed_u); + } break; + case PACT_PEEK: { + _mesa_etch_peek_pact(&pec_u, &pac_u->pek_u); + } break; + default: { + u3l_log("bad pact type %u", pac_u->hed_u.typ_y); + } } - cur_w += nex_w; + // now we can compute the mug and write the header + pac_u->hed_u.mug_w = u3r_mug_bytes(pec_u.buf_y, pec_u.len_w) + & 0xFFFFF; + _mesa_etch_head(ech_u, &pac_u->hed_u); - if ( !(nex_w = _mesa_etch_name(buf_y + cur_w, &pac_u->pay_u)) ) { - return 0; + // the payload is already written into the correct spot, so we just need to + // adjust the length + ech_u->len_w += pec_u.len_w; +} + +void +mesa_sift_pact(u3_sifter* sif_u, u3_mesa_pact* pac_u) +{ + mesa_sift_head(sif_u, &pac_u->hed_u); + + // for mug, later + c3_y *mug_y = sif_u->buf_y; + c3_w pre_w = sif_u->rem_w; + + switch ( pac_u->hed_u.typ_y ) { + case PACT_PEEK: { + _mesa_sift_peek_pact(sif_u, &pac_u->pek_u); + } break; + case PACT_PAGE: { + _mesa_sift_page_pact(sif_u, &pac_u->pag_u, pac_u->hed_u.nex_y); + } break; + case PACT_POKE: { + _mesa_sift_poke_pact(sif_u, &pac_u->pok_u); + } break; + default: { + /* u3l_log("mesa: received unknown packet type"); */ + break; + } } - cur_w += nex_w; - if ( !(nex_w = _mesa_etch_data(buf_y + cur_w, &pac_u->dat_u)) ) { - return 0; + { + c3_w mug_w = u3r_mug_bytes(mug_y, pre_w - sif_u->rem_w) + & 0xFFFFF; + if ( mug_w != pac_u->hed_u.mug_w ) { + _sift_fail(sif_u, "bad mug"); + return; + } } - cur_w += nex_w; +} - return cur_w; +/* packet etch/sift, with roundtrip tests */ + +c3_w +mesa_etch_pact_to_buf(c3_y* buf_y, u3_mesa_pact *pac_u) { + u3_etcher ech_u; + etcher_init(&ech_u, buf_y); + mesa_etch_pact(&ech_u, pac_u); + + #ifdef MESA_ROUNDTRIP + u3_mesa_pact poc_u; + u3_sifter sif_u; + sifter_init(&sif_u, ech_u.buf_y, ech_u.len_w); + mesa_sift_pact(&sif_u, &poc_u); + if ( sif_u.rem_w && !sif_u.err_c ) { + u3l_log("mesa: etch roundtrip failed: %u trailing bytes", sif_u.rem_w); + _sift_fail(&sif_u, "trailing bytes"); + } + if ( sif_u.err_c ) { + u3l_log("mesa: roundtrip failed: %s", sif_u.err_c); + assert(!"roundtrip failed"); + } + _mesa_check_pacts_equal(&poc_u, pac_u); + mesa_free_pact(&poc_u); + #endif + + return ech_u.len_w; } +c3_c* +mesa_sift_pact_from_buf(u3_mesa_pact *pac_u, c3_y* buf_y, c3_w len_w) { + u3_sifter sif_u; + sifter_init(&sif_u, buf_y, len_w); + mesa_sift_pact(&sif_u, pac_u); + if ( sif_u.rem_w && !sif_u.err_c ) { + u3l_log("mesa: sift roundtrip failed: %u trailing bytes", sif_u.rem_w); + _sift_fail(&sif_u, "trailing bytes"); + } + + #ifdef MESA_ROUNDTRIP + c3_y* bof_y = c3_calloc(len_w); + u3_etcher ech_u; + etcher_init(&ech_u, bof_y); + mesa_etch_pact(&ech_u, pac_u); + if ( memcmp(bof_y, buf_y, len_w) ) { + u3l_log("mesa: roundtrip failed: len_w %u ech_u %u", len_w, ech_u.len_w); + } + u3_assert( 0 == memcmp(bof_y, buf_y, len_w) ); + c3_free(bof_y); + #endif + return sif_u.err_c; +} /* sizing */ @@ -955,49 +1062,6 @@ mesa_size_pact(u3_mesa_pact* pac_u) return siz_w; } -c3_w -mesa_etch_pact(c3_y* buf_y, u3_mesa_pact* pac_u) -{ - c3_w cur_w = 8; // space for header + cookie - c3_w nex_w = 0; - u3_mesa_head* hed_u = &pac_u->hed_u; - switch ( hed_u->typ_y ) { - case PACT_POKE: { - nex_w = _mesa_etch_poke_pact(buf_y + cur_w, &pac_u->pok_u, hed_u); - } break; - - case PACT_PEEK: { - nex_w = _mesa_etch_name(buf_y + cur_w, &pac_u->pek_u.nam_u); - } break; - - case PACT_PAGE: { - nex_w = _mesa_etch_page_pact(buf_y + cur_w, &pac_u->pag_u, hed_u); - } break; - - default: { - u3l_log("bad pact type %u", pac_u->hed_u.typ_y); - } - } - if ( 0 == nex_w ) { - return 0; - } - - hed_u->mug_w = u3r_mug_bytes(buf_y + cur_w, nex_w); - hed_u->mug_w &= 0xFFFFF; - - _mesa_etch_head(hed_u, buf_y); - - cur_w += nex_w; - - if ( mesa_size_pact(pac_u) == cur_w ) { - return cur_w; - } - else { - u3l_log("mesa: size %u cur_w %u", mesa_size_pact(pac_u), cur_w); - return 0; - } -} - #ifdef PACT_TEST /* _mesa_encode_path(): produce buf_y as a parsed path From 17ae179bf3b2a3735b63472015c39c1e6eb68f13 Mon Sep 17 00:00:00 2001 From: lukechampine Date: Mon, 12 Aug 2024 22:13:59 -0400 Subject: [PATCH 354/430] mesa: add _etch_bits and _sift_bits --- pkg/vere/io/mesa/mesa.h | 4 ++ pkg/vere/io/mesa/pact.c | 129 +++++++++++++++++++++++----------------- 2 files changed, 80 insertions(+), 53 deletions(-) diff --git a/pkg/vere/io/mesa/mesa.h b/pkg/vere/io/mesa/mesa.h index a52e75979e..dfe09e0c89 100644 --- a/pkg/vere/io/mesa/mesa.h +++ b/pkg/vere/io/mesa/mesa.h @@ -145,11 +145,15 @@ typedef struct _u3_mesa_pact { typedef struct _u3_etcher { c3_y* buf_y; c3_w len_w; + c3_d bit_d; // for _etch_bits + c3_y off_y; // for _etch_bits } u3_etcher; typedef struct _u3_sifter { c3_y* buf_y; c3_w rem_w; + c3_d bit_d; // for _etch_bits + c3_y off_y; // for _etch_bits c3_c* err_c; } u3_sifter; diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index 0e4156a2cf..eb5e204ecd 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -338,6 +338,8 @@ etcher_init(u3_etcher* ech_u, c3_y* buf_y) { ech_u->buf_y = buf_y; ech_u->len_w = 0; + ech_u->bit_d = 0; + ech_u->off_y = 0; } void @@ -345,6 +347,8 @@ sifter_init(u3_sifter* sif_u, c3_y* buf_y, c3_w len_w) { sif_u->buf_y = buf_y; sif_u->rem_w = len_w; + sif_u->bit_d = 0; + sif_u->off_y = 0; sif_u->err_c = NULL; } @@ -364,6 +368,7 @@ _sift_fail(u3_sifter* sif_u, c3_c* msg_c) static c3_y* _etch_next(u3_etcher* ech_u, c3_w len_w) { + assert ( ech_u->off_y == 0 ); // ensure all bits were etched c3_y *res_y = ech_u->buf_y + ech_u->len_w; ech_u->len_w += len_w; return res_y; @@ -372,6 +377,7 @@ _etch_next(u3_etcher* ech_u, c3_w len_w) static c3_y* _sift_next(u3_sifter* sif_u, c3_w len_w) { + assert ( sif_u->off_y == 0 ); // ensure all bits were sifted if ( sif_u->err_c ) { return NULL; } @@ -512,6 +518,37 @@ _sift_var_chub(u3_sifter* sif_u, c3_w len_w) return val_d; } +static void +_etch_bits(u3_etcher* ech_u, c3_w wid_w, c3_d val_d) +{ + assert ( ech_u->off_y + wid_w <= 64 ); + ech_u->bit_d |= ((val_d&((1 << wid_w) - 1)) << ech_u->off_y); + ech_u->off_y += wid_w; + while ( ech_u->off_y >= 8 ) { + ech_u->buf_y[ech_u->len_w] = ech_u->bit_d & 0xFF; + ech_u->bit_d >>= 8; + ech_u->len_w += 1; + ech_u->off_y -= 8; + } +} + +static c3_d +_sift_bits(u3_sifter* sif_u, c3_w wid_w) +{ + assert ( wid_w <= 64 ); + while ( sif_u->off_y < wid_w ) { + c3_d byt_d = (sif_u->rem_w > 0) ? sif_u->buf_y[0] : 0; + sif_u->buf_y += 1; + sif_u->rem_w -= 1; + sif_u->bit_d |= (byt_d << sif_u->off_y); + sif_u->off_y += 8; + } + c3_d res_d = sif_u->bit_d & ((1 << wid_w) - 1); + sif_u->bit_d >>= wid_w; + sif_u->off_y -= wid_w; + return res_d; +} + static void _etch_ship(u3_etcher* ech_u, u3_ship who_u, c3_y len_y) { @@ -537,13 +574,12 @@ _mesa_etch_head(u3_etcher* ech_u, u3_mesa_head* hed_u) if ( 1 != hed_u->pro_y ) { u3l_log("etching bad head"); } - c3_w hed_w = (hed_u->nex_y & 0x3) << 2 - ^ (hed_u->pro_y & 0x7) << 4 // XX constant, 1 - ^ (hed_u->typ_y & 0x3) << 7 - ^ (hed_u->hop_y & 0x7) << 9 - ^ (hed_u->mug_w & 0xFFFFF) << 12; - // XX: we don't expand hopcount if no request. Correct? - _etch_word(ech_u, hed_w); + _etch_bits(ech_u, 2, 0); // unused + _etch_bits(ech_u, 2, hed_u->nex_y); + _etch_bits(ech_u, 3, hed_u->pro_y); + _etch_bits(ech_u, 2, hed_u->typ_y); + _etch_bits(ech_u, 3, hed_u->hop_y); + _etch_bits(ech_u, 20, hed_u->mug_w); _etch_word(ech_u, MESA_COOKIE); } @@ -556,12 +592,12 @@ mesa_is_new_pact(c3_y* buf_y, c3_w len_w) void mesa_sift_head(u3_sifter* sif_u, u3_mesa_head* hed_u) { - c3_w hed_w = _sift_word(sif_u); - hed_u->nex_y = (hed_w >> 2) & 0x3; - hed_u->pro_y = (hed_w >> 4) & 0x7; - hed_u->typ_y = (hed_w >> 7) & 0x3; - hed_u->hop_y = (hed_w >> 9) & 0x7; - hed_u->mug_w = (hed_w >> 12) & 0xFFFFF; + _sift_bits(sif_u, 2); // unused + hed_u->nex_y = _sift_bits(sif_u, 2); + hed_u->pro_y = _sift_bits(sif_u, 3); + hed_u->typ_y = _sift_bits(sif_u, 2); + hed_u->hop_y = _sift_bits(sif_u, 3); + hed_u->mug_w = _sift_bits(sif_u, 20); if ( 1 != hed_u->pro_y ) { _sift_fail(sif_u, "bad protocol"); } @@ -587,13 +623,12 @@ _mesa_etch_name(u3_etcher *ech_u, u3_mesa_name* nam_u) met_u.tau_y = (c3y == nam_u->aut_o) ? 1 : 0; met_u.gaf_y = _mesa_make_chub_tag(nam_u->fra_d); } - c3_y met_y = (met_u.ran_y & 0x3) << 0 - ^ (met_u.rif_y & 0x3) << 2 - ^ (met_u.nit_y & 0x1) << 4 - ^ (met_u.tau_y & 0x1) << 5 - ^ (met_u.gaf_y & 0x3) << 6; - _etch_byte(ech_u, met_y); + _etch_bits(ech_u, 2, met_u.ran_y); + _etch_bits(ech_u, 2, met_u.rif_y); + _etch_bits(ech_u, 1, met_u.nit_y); + _etch_bits(ech_u, 1, met_u.tau_y); + _etch_bits(ech_u, 2, met_u.gaf_y); c3_y her_y = 2 << met_u.ran_y; // XX confirm _etch_ship(ech_u, nam_u->her_u, her_y); _etch_var_word(ech_u, nam_u->rif_w, met_u.rif_y + 1); @@ -614,13 +649,11 @@ static void _mesa_sift_name(u3_sifter* sif_u, u3_mesa_name* nam_u) { u3_mesa_name_meta met_u; - - c3_y met_y = _sift_byte(sif_u); - met_u.ran_y = (met_y >> 0) & 0x3; - met_u.rif_y = (met_y >> 2) & 0x3; - met_u.nit_y = (met_y >> 4) & 0x1; - met_u.tau_y = (met_y >> 5) & 0x1; - met_u.gaf_y = (met_y >> 6) & 0x3; + met_u.ran_y = _sift_bits(sif_u, 2); + met_u.rif_y = _sift_bits(sif_u, 2); + met_u.nit_y = _sift_bits(sif_u, 1); + met_u.tau_y = _sift_bits(sif_u, 1); + met_u.gaf_y = _sift_bits(sif_u, 2); nam_u->nit_o = ( met_u.nit_y ) ? c3y : c3n; nam_u->aut_o = ( met_u.tau_y ) ? c3y : c3n; @@ -649,39 +682,34 @@ _mesa_sift_name(u3_sifter* sif_u, u3_mesa_name* nam_u) static void _mesa_etch_data(u3_etcher* ech_u, u3_mesa_data* dat_u) { - c3_y aut_y = 0; - switch ( dat_u->aut_u.typ_e ) { - case AUTH_SIGN: aut_y = 0; break; - case AUTH_HMAC: aut_y = 1; break; - case AUTH_NONE: aut_y = 2; break; - case AUTH_PAIR: aut_y = 3; break; - } - c3_y bot_y = _mesa_make_chub_tag(dat_u->tob_d); + u3_mesa_data_meta met_u; + met_u.bot_y = _mesa_make_chub_tag(dat_u->tob_d); + met_u.aut_o = __(dat_u->aut_u.typ_e == AUTH_SIGN || dat_u->aut_u.typ_e == AUTH_HMAC); + met_u.auv_o = __(dat_u->aut_u.typ_e == AUTH_SIGN || dat_u->aut_u.typ_e == AUTH_NONE); c3_y nel_y = _mesa_met3_w(dat_u->len_w); - c3_y men_y = (3 >= nel_y) ? nel_y : 3; - _etch_byte(ech_u, (bot_y & 0x3) << 0 - | (aut_y & 0x3) << 2 - | (men_y & 0x3) << 6); - _etch_var_chub(ech_u, dat_u->tob_d, 1 << bot_y); + met_u.men_y = c3_min(nel_y, 3); + _etch_bits(ech_u, 2, met_u.bot_y); + _etch_bits(ech_u, 1, met_u.auv_o); + _etch_bits(ech_u, 1, met_u.aut_o); + _etch_bits(ech_u, 2, 0); // unused + _etch_bits(ech_u, 2, met_u.men_y); + _etch_var_chub(ech_u, dat_u->tob_d, 1 << met_u.bot_y); switch ( dat_u->aut_u.typ_e ) { case AUTH_SIGN: { _etch_bytes(ech_u, dat_u->aut_u.sig_y, 64); } break; - case AUTH_HMAC: { _etch_bytes(ech_u, dat_u->aut_u.mac_y, 16); } break; - case AUTH_NONE: { } break; - case AUTH_PAIR: { _etch_bytes(ech_u, dat_u->aut_u.has_y[0], 32); _etch_bytes(ech_u, dat_u->aut_u.has_y[1], 32); } break; } - if ( 3 == men_y ) { + if ( 3 == met_u.men_y ) { _etch_byte(ech_u, nel_y); } _etch_var_word(ech_u, dat_u->len_w, nel_y); @@ -692,12 +720,11 @@ static void _mesa_sift_data(u3_sifter* sif_u, u3_mesa_data* dat_u) { u3_mesa_data_meta met_u; - - c3_y met_y = _sift_byte(sif_u); - met_u.bot_y = (met_y >> 0) & 0x3; - met_u.auv_o = (met_y >> 2) & 0x1; - met_u.aut_o = (met_y >> 3) & 0x1; - met_u.men_y = (met_y >> 6) & 0x3; + met_u.bot_y = _sift_bits(sif_u, 2); + met_u.auv_o = _sift_bits(sif_u, 1); + met_u.aut_o = _sift_bits(sif_u, 1); + _sift_bits(sif_u, 2); // unused + met_u.men_y = _sift_bits(sif_u, 2); dat_u->tob_d = _sift_var_chub(sif_u, 1< Date: Tue, 13 Aug 2024 11:34:03 -0400 Subject: [PATCH 355/430] mesa: swap aut/auv order --- pkg/vere/io/mesa/pact.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index eb5e204ecd..d1520b6b53 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -689,8 +689,8 @@ _mesa_etch_data(u3_etcher* ech_u, u3_mesa_data* dat_u) c3_y nel_y = _mesa_met3_w(dat_u->len_w); met_u.men_y = c3_min(nel_y, 3); _etch_bits(ech_u, 2, met_u.bot_y); - _etch_bits(ech_u, 1, met_u.auv_o); _etch_bits(ech_u, 1, met_u.aut_o); + _etch_bits(ech_u, 1, met_u.auv_o); _etch_bits(ech_u, 2, 0); // unused _etch_bits(ech_u, 2, met_u.men_y); _etch_var_chub(ech_u, dat_u->tob_d, 1 << met_u.bot_y); @@ -721,8 +721,8 @@ _mesa_sift_data(u3_sifter* sif_u, u3_mesa_data* dat_u) { u3_mesa_data_meta met_u; met_u.bot_y = _sift_bits(sif_u, 2); - met_u.auv_o = _sift_bits(sif_u, 1); met_u.aut_o = _sift_bits(sif_u, 1); + met_u.auv_o = _sift_bits(sif_u, 1); _sift_bits(sif_u, 2); // unused met_u.men_y = _sift_bits(sif_u, 2); From 3d462d3fa5354ef210493a9943a6b2e7b8ab8bc8 Mon Sep 17 00:00:00 2001 From: Sigilante Date: Tue, 13 Aug 2024 12:03:51 -0500 Subject: [PATCH 356/430] Add corrected range jets. --- WORKSPACE.bazel | 2 +- pkg/noun/jets/i/lagoon.c | 85 +++++++++++++++++++++------------------- 2 files changed, 45 insertions(+), 42 deletions(-) diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index ec6a637114..37d6659125 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -297,7 +297,7 @@ versioned_http_archive( strip_prefix = "SoftBLAS-{version}", # sha256 = "", url = "https://github.com/urbit/SoftBLAS/archive/{version}.tar.gz", - version = "29daa2f2fd0ad5070e405ad287f3623804f8fc67", + version = "cbffb33f19ea02f9ffbd184d445123c57929ec53", ) versioned_http_archive( diff --git a/pkg/noun/jets/i/lagoon.c b/pkg/noun/jets/i/lagoon.c index 5da333ecf0..13c0f2a138 100644 --- a/pkg/noun/jets/i/lagoon.c +++ b/pkg/noun/jets/i/lagoon.c @@ -10,6 +10,11 @@ #include // for pow() #include +#define f16_ceil(a) f16_roundToInt( a, softfloat_round_max, false ) +#define f32_ceil(a) f32_roundToInt( a, softfloat_round_max, false ) +#define f64_ceil(a) f64_roundToInt( a, softfloat_round_max, false ) +#define f128M_ceil(a, b) f128M_roundToInt( a, softfloat_round_max, false, b ) + union half { float16_t h; c3_w c; @@ -1964,15 +1969,14 @@ u3r_bytes(0, 2, (c3_y*)&(a16.v), a); u3r_bytes(0, 2, (c3_y*)&(b16.v), b); u3r_bytes(0, 2, (c3_y*)&(interval16.v), d); - c3_d n16 = f16_to_i64(f16_div(f16_sub(b16, a16), interval16), softfloat_round_minMag, false); - c3_y* x_bytes16 = (c3_y*)u3a_malloc(((n16+1)*2+1)*sizeof(c3_y)); - for (c3_d i = 1; i <= n16; i++) { - ((float16_t*)x_bytes16)[n16-i] = f16_add(a16, f16_mul(i32_to_f16(i), interval16)); - } - ((float16_t*)x_bytes16)[n16] = a16; - // ((float16_t*)x_bytes16)[0] = b16; - x_bytes16[(n16+1)*2] = 0x1; // pin head - r_data = u3i_bytes(((n16+1)*2+1)*sizeof(c3_y), x_bytes16); + c3_d n16 = f16_to_i64(f16_ceil(f16_div(f16_sub(b16, a16), interval16)), softfloat_round_minMag, false); + c3_y* x_bytes16 = (c3_y*)u3a_malloc(((n16+1)*2)*sizeof(c3_y)); + ((float16_t*)x_bytes16)[0] = a16; + for (c3_d i = 1; i < n16; i++) { + ((float16_t*)x_bytes16)[i] = f16_add(a16, f16_mul(i32_to_f16(i), interval16)); + } + ((float16_t*)x_bytes16)[n16].v = 0x1; // pin head + r_data = u3i_bytes(((n16+1)*2)*sizeof(c3_y), x_bytes16); u3a_free(x_bytes16); break;} @@ -1981,15 +1985,14 @@ u3r_bytes(0, 4, (c3_y*)&(a32.v), a); u3r_bytes(0, 4, (c3_y*)&(b32.v), b); u3r_bytes(0, 4, (c3_y*)&(interval32.v), d); - c3_d n32 = f32_to_i64(f32_div(f32_sub(b32, a32), interval32), softfloat_round_minMag, false); - c3_y* x_bytes32 = (c3_y*)u3a_malloc(((n32+1)*4+1)*sizeof(c3_y)); - for (c3_d i = 1; i <= n32; i++) { - ((float32_t*)x_bytes32)[n32-i] = f32_add(a32, f32_mul(i32_to_f32(i), interval32)); - } - ((float32_t*)x_bytes32)[n32] = a32; - // ((float32_t*)x_bytes32)[0] = b32; - x_bytes32[(n32+1)*4] = 0x1; // pin head - r_data = u3i_bytes(((n32+1)*4+1)*sizeof(c3_y), x_bytes32); + c3_d n32 = f32_to_i64(f32_ceil(f32_div(f32_sub(b32, a32), interval32)), softfloat_round_minMag, false); + c3_y* x_bytes32 = (c3_y*)u3a_malloc(((n32+1)*4)*sizeof(c3_y)); + ((float32_t*)x_bytes32)[0] = a32; + for (c3_d i = 1; i < n32; i++) { + ((float32_t*)x_bytes32)[i] = f32_add(a32, f32_mul(i32_to_f32(i), interval32)); + } + ((float32_t*)x_bytes32)[n32].v = 0x1; // pin head + r_data = u3i_bytes(((n32+1)*4)*sizeof(c3_y), x_bytes32); u3a_free(x_bytes32); break;} @@ -1998,15 +2001,14 @@ u3r_bytes(0, 8, (c3_y*)&(a64.v), a); u3r_bytes(0, 8, (c3_y*)&(b64.v), b); u3r_bytes(0, 8, (c3_y*)&(interval64.v), d); - c3_d n64 = f64_to_i64(f64_div(f64_sub(b64, a64), interval64), softfloat_round_minMag, false); - c3_y* x_bytes64 = (c3_y*)u3a_malloc(((n64+1)*8+1)*sizeof(c3_y)); + c3_d n64 = f64_to_i64(f64_ceil(f64_div(f64_sub(b64, a64), interval64)), softfloat_round_minMag, false); + c3_y* x_bytes64 = (c3_y*)u3a_malloc(((n64+1)*8)*sizeof(c3_y)); + ((float64_t*)x_bytes64)[0] = a64; for (c3_d i = 1; i < n64; i++) { - ((float64_t*)x_bytes64)[n64-i] = f64_add(a64, f64_mul(i32_to_f64(i), interval64)); + ((float64_t*)x_bytes64)[i] = f64_add(a64, f64_mul(i32_to_f64(i), interval64)); } - ((float64_t*)x_bytes64)[n64] = a64; - // ((float64_t*)x_bytes64)[0] = b64; - x_bytes64[(n64+1)*8] = 0x1; // pin head - r_data = u3i_bytes(((n64+1)*8+1)*sizeof(c3_y), x_bytes64); + ((float64_t*)x_bytes64)[n64].v = 0x1; // pin head + r_data = u3i_bytes(((n64+1)*8)*sizeof(c3_y), x_bytes64); u3a_free(x_bytes64); break;} @@ -2014,22 +2016,23 @@ float128_t a128, b128, interval128; u3r_bytes(0, 16, (c3_y*)&(a128.v[0]), a); u3r_bytes(0, 16, (c3_y*)&(b128.v[0]), b); - u3r_bytes(0, 16, (c3_y*)&(interval128.v), d); + u3r_bytes(0, 16, (c3_y*)&(interval128.v[0]), d); float128_t tmp; f128M_sub(&b128, &a128, &tmp); - f128M_div(&tmp, &interval128, &interval128); + f128M_div(&tmp, &interval128, &tmp); + f128M_ceil(&tmp, &tmp); c3_d n128 = f128M_to_i64(&tmp, softfloat_round_minMag, false); - c3_y* x_bytes128 = (c3_y*)u3a_malloc(((n128+1)*16+1)*sizeof(c3_y)); + c3_y* x_bytes128 = (c3_y*)u3a_malloc(((n128+1)*16)*sizeof(c3_y)); float128_t i128; + ((float128_t*)x_bytes128)[0] = a128; for (c3_d i = 1; i < n128; i++) { i32_to_f128M(i, &i128); - f128M_mul(&i128, &interval128, &((float128_t*)x_bytes128)[n128-i]); - f128M_add(&a128, &((float128_t*)x_bytes128)[n128-i], &((float128_t*)x_bytes128)[n128-i]); + f128M_mul(&i128, &interval128, &((float128_t*)x_bytes128)[i]); + f128M_add(&a128, &((float128_t*)x_bytes128)[i], &((float128_t*)x_bytes128)[i]); } - ((float128_t*)x_bytes128)[n128] = a128; - // ((float128_t*)x_bytes128)[0] = b128; - x_bytes128[(n128+1)*16] = 0x1; // pin head - r_data = u3i_bytes(((n128+1)*16+1)*sizeof(c3_y), x_bytes128); + ((float128_t*)x_bytes128)[n128].v[0] = 0x1; // pin head + ((float128_t*)x_bytes128)[n128].v[1] = 0x0; // pin head + r_data = u3i_bytes(((n128+1)*16)*sizeof(c3_y), x_bytes128); u3a_free(x_bytes128); break;} } @@ -3154,19 +3157,19 @@ u3r_bytes(0, 2, (c3_y*)&a_, a); u3r_bytes(0, 2, (c3_y*)&b_, b); u3r_bytes(0, 2, (c3_y*)&d_, d); - n_ = f16_to_i64(f16_div(f16_sub((float16_t){b_}, (float16_t){a_}), (float16_t){d_}), softfloat_round_minMag, false); + n_ = f16_to_i64(f16_ceil(f16_div(f16_sub((float16_t){b_}, (float16_t){a_}), (float16_t){d_})), softfloat_round_minMag, false) - 1; break; case 5: u3r_bytes(0, 4, (c3_y*)&a_, a); u3r_bytes(0, 4, (c3_y*)&b_, b); u3r_bytes(0, 4, (c3_y*)&d_, d); - n_ = f32_to_i64(f32_div(f32_sub((float32_t){b_}, (float32_t){a_}), (float32_t){d_}), softfloat_round_minMag, false); + n_ = f32_to_i64(f32_ceil(f32_div(f32_sub((float32_t){b_}, (float32_t){a_}), (float32_t){d_})), softfloat_round_minMag, false) - 1; break; case 6: u3r_bytes(0, 8, (c3_y*)&a_, a); u3r_bytes(0, 8, (c3_y*)&b_, b); u3r_bytes(0, 8, (c3_y*)&d_, d); - n_ = f64_to_i64(f64_div(f64_sub((float64_t){b_}, (float64_t){a_}), (float64_t){d_}), softfloat_round_minMag, false); + n_ = f64_to_i64(f64_ceil(f64_div(f64_sub((float64_t){b_}, (float64_t){a_}), (float64_t){d_})), softfloat_round_minMag, false) - 1; break; case 7: { c3_d a__[2], b__[2], d__[2]; @@ -3176,11 +3179,12 @@ float128_t tmp; f128M_sub((float128_t*)&b__, (float128_t*)&a__, &tmp); f128M_div(&tmp, (float128_t*)&d__, &tmp); - n_ = f128M_to_i64(&tmp, softfloat_round_minMag, false); + f128M_ceil(&tmp, &tmp); + n_ = f128M_to_i64(&tmp, softfloat_round_minMag, false) - 1; break;} } u3_noun n = u3i_chub(n_+1); - x_shape = u3nt(u3k(n), 0x1, u3_nul); + x_shape = u3nc(u3k(n), u3_nul); return u3nc(u3nq(u3k(x_shape), u3k(x_bloq), u3k(x_kind), u3k(x_tail)), r_data); default: @@ -3268,7 +3272,7 @@ // Each argument is a ray, [=meta data=@ux] u3_noun x_meta, x_data, y_meta, y_data; - fprintf(stderr, "mmul 1\n"); + if ( c3n == u3r_mean(cor, u3x_sam_4, &x_meta, u3x_sam_5, &x_data, @@ -3297,7 +3301,6 @@ } else { switch (x_kind) { case c3__i754: - fprintf(stderr, "mmul 2\n"); _set_rounding(rnd); u3_noun r_data = u3qi_la_mmul_i754(x_data, y_data, x_shape, y_shape, x_bloq); // result is already [meta data] From d1291e67d9ab71e3d6b78a98afd772f94f5ed6e7 Mon Sep 17 00:00:00 2001 From: pkova Date: Wed, 14 Aug 2024 19:57:17 +0300 Subject: [PATCH 357/430] manage: u3m_mark takes no arguments --- pkg/noun/manage.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/noun/manage.c b/pkg/noun/manage.c index 71a1c86542..1ff748d528 100644 --- a/pkg/noun/manage.c +++ b/pkg/noun/manage.c @@ -460,7 +460,7 @@ u3m_file(c3_c* pas_c) /* u3m_mark(): mark all nouns in the road. */ u3m_quac** -u3m_mark() +u3m_mark(void) { u3m_quac** qua_u = c3_malloc(sizeof(*qua_u) * 5); qua_u[0] = u3v_mark(); @@ -1368,7 +1368,7 @@ u3m_grab(u3_noun som, ...) // terminate with u3_none // u3h_free(u3R->cax.har_p); // u3R->cax.har_p = u3h_new(); - u3m_mark(0); + u3m_mark(); { va_list vap; u3_noun tur; From cccfcd27512e0204eb220ade7fd14e5fcc48f007 Mon Sep 17 00:00:00 2001 From: pkova Date: Wed, 14 Aug 2024 19:57:37 +0300 Subject: [PATCH 358/430] allocate: mark the trace stack in u3a_mark_road --- pkg/noun/allocate.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/noun/allocate.c b/pkg/noun/allocate.c index 60332b5b52..829356a96a 100644 --- a/pkg/noun/allocate.c +++ b/pkg/noun/allocate.c @@ -2191,7 +2191,7 @@ u3a_mark_road() qua_u[1] = c3_calloc(sizeof(*qua_u[1])); qua_u[1]->nam_c = strdup("trace stack"); - qua_u[1]->siz_w = u3a_mark_noun(u3R->ski.gul) * 4; + qua_u[1]->siz_w = u3a_mark_noun(u3R->bug.tax) * 4; qua_u[2] = c3_calloc(sizeof(*qua_u[2])); qua_u[2]->nam_c = strdup("trace buffer"); From 2d1d5eed6cd7f4580d741bb046b29b1ed17f13e9 Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Wed, 14 Aug 2024 16:46:13 -0400 Subject: [PATCH 359/430] mesa: WIP trying to fix jumbo frames; still crashes in Ames on validating paths --- pkg/noun/ship.c | 7 ++ pkg/noun/ship.h | 3 + pkg/vere/io/mesa.c | 148 ++++++++++++++++++++++++++-------------- pkg/vere/io/mesa/pact.c | 12 ++-- 4 files changed, 114 insertions(+), 56 deletions(-) diff --git a/pkg/noun/ship.c b/pkg/noun/ship.c index 85684c3de9..73fa53bd72 100644 --- a/pkg/noun/ship.c +++ b/pkg/noun/ship.c @@ -79,6 +79,13 @@ u3_ships_equal(u3_ship sip_u, u3_ship sap_u) return __((sip_u[0] == sap_u[0]) && (sip_u[1] == sap_u[1])); } +void +u3_ship_copy(u3_ship des_u, u3_ship src_u) +{ + des_u[0] = src_u[0]; + des_u[1] = src_u[1]; +} + c3_l u3_ship_rank(u3_ship who_u) { diff --git a/pkg/noun/ship.h b/pkg/noun/ship.h index 5ddabd1021..d1e4696f4b 100644 --- a/pkg/noun/ship.h +++ b/pkg/noun/ship.h @@ -27,6 +27,9 @@ u3_ships_equal(u3_ship sip_u, u3_ship sap_u); c3_l u3_ship_rank(u3_ship who_u); +void +u3_ship_copy(u3_ship des_u, u3_ship src_u); + /** * Returns a ship's galaxy byte prefix. */ diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 819fd438ac..3f8ca8bc33 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -356,29 +356,6 @@ _mesa_is_direct_mode(u3_peer* per_u) return __(per_u->dir_u.her_d + DIRECT_ROUTE_TIMEOUT_MICROS > now_d); } -static inline void -_get_her(u3_mesa_pact* pac_u, c3_d* our_d) -{ - switch ( pac_u->hed_u.typ_y ) { - default: { - u3m_bail(c3__foul); - break; - } - case PACT_PAGE: { - memcpy(our_d, pac_u->pag_u.nam_u.her_u, 16); - break; - } - case PACT_PEEK: { - memcpy(our_d, pac_u->pek_u.nam_u.her_u, 16); - break; - } - case PACT_POKE: { - memcpy(our_d, pac_u->pok_u.nam_u.her_u, 16); - break; - } - } -} - static c3_c* _mesa_czar_dns(c3_y imp_y, c3_c* zar_c) { @@ -391,6 +368,7 @@ _mesa_czar_dns(c3_y imp_y, c3_c* zar_c) c3_i sas_i = snprintf(dns_c, len_w, "%s.%s.", nam_c + 1, zar_c); u3_assert(sas_i <= 255); + u3l_log("about to free nam_c"); c3_free(nam_c); u3z(nam); @@ -447,10 +425,31 @@ _mesa_free_line(u3_mesa_line* lin_u) c3_free(lin_u->tip_y); } +static void +_mesa_copy_auth_data(u3_auth_data* des_u, u3_auth_data* src_u) +{ + des_u->typ_e = src_u->typ_e; + switch ( des_u->typ_e ) { + case AUTH_HMAC: { + memcpy(des_u->mac_y, src_u->mac_y, 16); + } break; + case AUTH_SIGN: { + memcpy(des_u->sig_y, src_u->sig_y, 64); + } break; + case AUTH_PAIR: { + memcpy(des_u->has_y, src_u->has_y, 64); + } break; + case AUTH_NONE: { + } break; + default: u3_assert(!"unreachable"); + } +} + static void _mesa_copy_name(u3_mesa_name* des_u, u3_mesa_name* src_u) { memcpy(des_u, src_u, sizeof(u3_mesa_name)); + u3_ship_copy(des_u->her_u, src_u->her_u); des_u->pat_c = c3_calloc(src_u->pat_s + 1); // null-terminate memcpy(des_u->pat_c, src_u->pat_c, src_u->pat_s); } @@ -621,11 +620,15 @@ _mesa_del_request(u3_mesa* sam_u, u3_mesa_name* nam_u) { // u3l_log("wat_u %p", req_u->wat_u); // u3l_log("was_u buf %p", req_u->was_u.buf_y); uv_timer_stop(&req_u->tim_u); + u3l_log("about to free_pict in del_request"); _mesa_free_pict(req_u->pic_u); + u3l_log("about to free wat_u in del_request"); c3_free(req_u->wat_u); + u3l_log("about to free dat_y in del_request"); c3_free(req_u->dat_y); lss_verifier_free(req_u->los_u); u3h_del(per_u->req_p, key); + u3l_log("about to free req_u in del_request"); u3a_free(req_u); u3z(key); } @@ -944,6 +947,7 @@ _mesa_send_cb(uv_udp_send_t* req_u, c3_i sas_i) //sam_u->fig_u.net_o = c3y; } + u3l_log("about to free seal"); _mesa_free_seal(sel_u); } @@ -1247,18 +1251,20 @@ _mesa_req_pact_done(u3_pend_req* req_u, } } else if ( c3y != lss_verifier_ingest(req_u->los_u, dat_u->fra_y, dat_u->len_w, par_u) ) { + u3l_log("auth fail frag %"PRIu64, nam_u->fra_d); c3_free(par_u); // TODO: do we drop the whole request on the floor? - u3l_log("auth fail frag %"PRIu64, nam_u->fra_d); MESA_LOG(sam_u, AUTH); return; } else if ( c3y != _mesa_burn_misorder_queue(req_u) ) { + u3l_log("about to misorder free"); c3_free(par_u); MESA_LOG(sam_u, AUTH) return; } else { + u3l_log("about to other free"); c3_free(par_u); } @@ -1419,6 +1425,7 @@ _mesa_czar_cb(uv_getaddrinfo_t* adr_u, c3_i sas_i, struct addrinfo* aif_u) _mesa_czar_gone(sam_u, sas_i, imp_y, now_t); } + u3l_log("about to free adr_u"); c3_free(adr_u); uv_freeaddrinfo(aif_u); @@ -1591,6 +1598,7 @@ _mesa_resend_timer_cb(uv_timer_t* tim_u) #ifdef MESA_DEBUG u3l_log("mesa: resend PIT entry gone %u", res_u->ret_y); #endif + u3l_log("about to free resend data in resend-timer-cb"); _mesa_free_resend_data(res_u); return; } @@ -1607,6 +1615,7 @@ _mesa_resend_timer_cb(uv_timer_t* tim_u) uv_timer_start(&res_u->tim_u, _mesa_resend_timer_cb, 1000, 0); } else { + u3l_log("about to free resend data in resend-timer-cb 2"); _mesa_free_resend_data(res_u); } } @@ -1651,7 +1660,7 @@ _mesa_ef_send(u3_mesa* sam_u, u3_noun las, u3_noun pac) u3_mesa_request_data* dat_u = &res_u->dat_u; { dat_u->sam_u = sam_u; - _get_her(&pac_u, dat_u->her_u); + u3_ship_copy(dat_u->her_u, nam_u->her_u); dat_u->nam_u = nam_u; dat_u->las = u3k(las); dat_u->buf_y = buf_y; @@ -1871,6 +1880,7 @@ _mesa_put_jumbo_cache(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_mesa_line* lin_u) u3_noun pax = _name_to_jumbo_scry(nam_u); u3_weak del = u3h_put_get(sam_u->pac_p, pax, u3a_outa(lin_u)); if ( u3_none != del ) { + u3l_log("about to free line"); _mesa_free_line(u3a_into(del)); u3z(del); } @@ -1892,12 +1902,12 @@ _mesa_send_pact(u3_mesa* sam_u, static void _mesa_send_leaf(u3_mesa* sam_u, u3_mesa_line* lin_u, - u3_mesa_pact* pac_u, // scratchpad + u3_mesa_pact* pac_u, // scratchpad c3_d fra_d) { u3_mesa_name* nam_u = &pac_u->pag_u.nam_u; u3_mesa_data* dat_u = &pac_u->pag_u.dat_u; - nam_u->nit_o = __(fra_d == 0); + nam_u->nit_o = __(0 == fra_d); nam_u->fra_d = fra_d; c3_d i_d = fra_d - (lin_u->nam_u.fra_d * (1 << u3_Host.ops_u.jum_y)); c3_w cur_w = i_d * 1024; @@ -1905,7 +1915,10 @@ _mesa_send_leaf(u3_mesa* sam_u, dat_u->len_w = c3_min(lin_u->dat_w - cur_w, 1024); lss_pair* pair = ((lss_pair*)lin_u->haz_y) + i_d; - if ( 0 == memcmp(pair, &(lss_pair){0}, sizeof(lss_pair)) ) { + if ( 0 == nam_u->fra_d ) { + _mesa_copy_auth_data(&dat_u->aut_u, &lin_u->aut_u); + } + else if ( 0 == memcmp(pair, &(lss_pair){0}, sizeof(lss_pair)) ) { dat_u->aut_u.typ_e = AUTH_NONE; } else { dat_u->aut_u.typ_e = AUTH_PAIR; @@ -1924,7 +1937,7 @@ _mesa_send_leaf(u3_mesa* sam_u, } static void -_mesa_send_jumbo_pieces(u3_mesa* sam_u, u3_mesa_line* lin_u, c3_d* fra_d) +_mesa_send_jumbo_pieces(u3_mesa* sam_u, u3_mesa_line* lin_u, c3_d* fra_u) { #ifdef MESA_DEBUG u3l_log("mesa: send_jumbo_pieces()"); @@ -1957,36 +1970,52 @@ _mesa_send_jumbo_pieces(u3_mesa* sam_u, u3_mesa_line* lin_u, c3_d* fra_d) c3_d mev_d = mesa_num_leaves(dat_u->tob_d); c3_w pro_w = lss_proof_size(mev_d); - if ( c3y == nam_u->nit_o && pro_w > 0 ) { + // send response packet for %init request + if ( 0 == lin_u->nam_u.fra_d ) { + nam_u->nit_o = c3y; u3_weak pin = _mesa_get_pit(sam_u, nam_u); if ( u3_none != pin ) { - #ifdef MESA_DEBUG - u3l_log(" sending proof packet"); - #endif - dat_u->len_w = pro_w * sizeof(lss_hash); - c3_y* pro_y = c3_malloc(dat_u->len_w); - memcpy(pro_y, lin_u->tip_y, dat_u->len_w); - dat_u->fra_y = pro_y; - _mesa_send_pact(sam_u, u3k(u3t(pin)), NULL, &pac_u); - _mesa_del_pit(sam_u, nam_u); - c3_free(pro_y); - u3z(pin); + // if the initial Merkle proof is nonzero, this is a multifragment + // message; send auth response to the %init request + if ( pro_w > 0 ) { + dat_u->len_w = pro_w * sizeof(lss_hash); + c3_y* pro_y = c3_malloc(dat_u->len_w); + memcpy(pro_y, lin_u->tip_y, dat_u->len_w); + dat_u->fra_y = pro_y; + _mesa_send_pact(sam_u, u3k(u3t(pin)), NULL, &pac_u); + _mesa_del_pit(sam_u, nam_u); + u3l_log("about to free pro_y"); + c3_free(pro_y); + u3z(pin); + } + // single-fragment message; just send the one data fragment + else if ( ( NULL == fra_u) || (0 == *fra_u) ) { + _mesa_send_leaf(sam_u, lin_u, &pac_u, *fra_u); + } + else { + u3l_log("mesa: weird fragment number %"PRIu64, *fra_u); + } } } + // Now send responses to non-%init requests. + // note: we might re-send the first data fragment, but only if there is an + // outstanding non-%init request for it, which would be strange but not + // illegal + nam_u->nit_o = c3n; // send leaf packet(s) c3_d lev_d = mesa_num_leaves(lin_u->dat_w); c3_d fir_d = nam_u->fra_d; c3_d las_d = fir_d + lev_d; - if ( NULL == fra_d ) { + if ( NULL == fra_u ) { for (c3_d fra_d = fir_d; fra_d < las_d; fra_d++) { _mesa_send_leaf(sam_u, lin_u, &pac_u, fra_d); } } else { - _mesa_send_leaf(sam_u, lin_u, &pac_u, *fra_d); + _mesa_send_leaf(sam_u, lin_u, &pac_u, *fra_u); } - // mesa_free_pact(&pac_u); + // mesa_free_pact(&pac_u); // TODO reinstate } static void @@ -2006,8 +2035,8 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun res) log_pact(pac_u); return; } - u3_noun pac, pas; - if ( c3n == u3r_cell(pag, &pac, &pas) || + u3_noun pac, pas, pof; + if ( c3n == u3r_trel(pag, &pac, &pas, &pof) || c3n == u3a_is_pug(pac) ) { u3l_log("mesa: jumbo frame misshapen"); log_pact(pac_u); @@ -2038,7 +2067,7 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun res) c3_d mev_d = mesa_num_leaves(dat_u->tob_d); // leaves in message c3_w tip_w = // bytes in Merkle spine (mev_d > 1 && jum_u.pag_u.nam_u.fra_d == 0)? - lss_proof_size(mev_d) : + lss_proof_size(mev_d) * sizeof(lss_hash): 0; c3_w dat_w = dat_u->len_w; // bytes in fragment data in this jumbo frame c3_w lev_w = mesa_num_leaves(dat_w); // number of leaves in this frame @@ -2055,13 +2084,16 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun res) lin_u->tip_y = c3_malloc(len_w); // note: off-loom lin_u->dat_y = lin_u->tip_y + tip_w; lin_u->haz_y = lin_u->dat_y + haz_w; - memcpy(lin_u->tip_y, dat_u->fra_y, dat_u->len_w); + u3r_bytes(0, tip_w, lin_u->tip_y, pof); + memcpy(lin_u->dat_y, dat_u->fra_y, dat_u->len_w); u3r_bytes(0, haz_w, lin_u->haz_y, pas); + u3l_log("about to mesa_free_pact(&jum_u)"); mesa_free_pact(&jum_u); } _mesa_put_jumbo_cache(sam_u, nam_u, lin_u); _mesa_send_jumbo_pieces(sam_u, lin_u, NULL); + u3z(res); return; } @@ -2228,6 +2260,7 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) lss_root(root, pof_u, pof_w); req_u->los_u = c3_calloc(sizeof(lss_verifier)); lss_verifier_init(req_u->los_u, 0, req_u->tof_d, pof_u); + u3l_log("about to c3_free(pof_u)"); c3_free(pof_u); req_u = _mesa_put_request(sam_u, nam_u, req_u); @@ -2297,6 +2330,7 @@ _mesa_page_news_cb(u3_ovum* egg_u, u3_ovum_news new_e) // XX do early delete instead, to avoid injecting retries // _mesa_del_pit(dat_u->per_u->sam_u, dat_u->nam_u); + u3l_log("about to free lane cb data"); _mesa_free_lane_cb_data(dat_u); } @@ -2306,6 +2340,7 @@ _mesa_page_bail_cb(u3_ovum* egg_u, u3_ovum_news new_e) #ifdef MESA_DEBUG u3l_log("mesa: arvo page event failed"); #endif + u3l_log("about to free lane cb data 2"); _mesa_free_lane_cb_data((u3_mesa_lane_cb_data*)egg_u->ptr_v); } @@ -2349,8 +2384,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) u3_mesa_name* nam_u = &pac_u->pek_u.nam_u; c3_s fra_s; - c3_d* her_d = nam_u->her_u; - c3_o our_o = u3_ships_equal(her_d, sam_u->pir_u->who_d); + c3_o our_o = u3_ships_equal(nam_u->her_u, sam_u->pir_u->who_d); // forwarding wrong, need a PIT entry // if ( c3n == our_o ) { @@ -2405,6 +2439,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) _mesa_send_pact(sam_u, u3k(las), per_u, pac_u); _mesa_del_pit(sam_u, nam_u); + u3l_log("about to free_pict() in hear_page()"); _mesa_free_pict(pic_u); u3z(pin); return; @@ -2449,7 +2484,8 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) // _mesa_del_pit(dat_u->per_u->sam_u, dat_u->nam_u); u3_auto_peer(ovo, dat_u, _mesa_page_news_cb, _mesa_page_bail_cb); - + + u3l_log("about to free_pict() 2 in hear_page()"); _mesa_free_pict(pic_u); u3z(pin); return; @@ -2482,7 +2518,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) // TODO: check return value before continuing? c3_y boq_y = u3_Host.ops_u.jum_y; - c3_o done_with_jumbo_frame = __(0 == req_u->hav_d % boq_y); + c3_o done_with_jumbo_frame = __(0 == (req_u->hav_d % (1 << (boq_y - 13)))); _mesa_del_pit(sam_u, nam_u); if ( c3y == done_with_jumbo_frame ) { u3_noun cad; @@ -2531,6 +2567,7 @@ _mesa_forward_request(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane lan_u) u3l_log("mesa: alien forward for %s", mes); c3_free(mes); #endif + u3l_log("about to free_pict() in forward_request()"); _mesa_free_pict(pic_u); return; } @@ -2538,6 +2575,7 @@ _mesa_forward_request(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane lan_u) u3_lane lin_u = _mesa_get_direct_lane(sam_u, pac_u->pek_u.nam_u.her_u); u3_lane zer_u = {0, 0}; if ( _mesa_lanes_equal(&zer_u, &lin_u) == c3y) { + u3l_log("about to free_pict() in forward_request() lanes_equal"); _mesa_free_pict(pic_u); return; } @@ -2549,6 +2587,7 @@ _mesa_forward_request(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane lan_u) _mesa_add_lane_to_pit(sam_u, &pac_u->pek_u.nam_u, lan_u); _mesa_send(pic_u, &lin_u); } + u3l_log("about to free_pict() in forward_request() end"); _mesa_free_pict(pic_u); } @@ -2581,6 +2620,7 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) if ( NULL != lin_u ) { if ( CTAG_ITEM == lin_u->typ_y ) { _mesa_send_jumbo_pieces(sam_u, lin_u, &fra_d); + u3l_log("about to free_pict after jumbo pieces"); _mesa_free_pict(pic_u); } return; @@ -2603,6 +2643,7 @@ _mesa_poke_news_cb(u3_ovum* egg_u, u3_ovum_news new_e) return; } u3_mesa_lane_cb_data* dat_u = egg_u->ptr_v; + u3l_log("free(dat_u) in poke_news_cb"); c3_free(dat_u); } @@ -2717,6 +2758,7 @@ _mesa_hear(u3_mesa* sam_u, mesa_free_pact(&pic_u->pac_u); return; } + u3l_log("about to free hun_y"); c3_free(hun_y); struct sockaddr_in* add_u = (struct sockaddr_in*)adr_u; @@ -2752,12 +2794,14 @@ static void _mesa_recv_cb(uv_udp_t* wax_u, c3_free(buf_u->base); } else if ( 0 == nrd_i ) { + u3l_log("about to free buf_u->base"); c3_free(buf_u->base); } else if ( flg_i & UV_UDP_PARTIAL ) { if ( u3C.wag_w & u3o_verbose ) { u3l_log("mesa: recv: fail: message truncated"); } + u3l_log("about to free buf_u->base 2"); c3_free(buf_u->base); } else { diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index d1520b6b53..18aa411d3c 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -166,10 +166,11 @@ log_name(u3_mesa_name* nam_u) } u3l_log("%s: /%s", her_c, nam_u->pat_c); - u3l_log(" rift: %u bloq: %u auth/data: %s frag: %"PRIu64, + u3l_log(" rift: %u bloq: %u auth/data: %s init: %s frag: %"PRIu64, nam_u->rif_w, nam_u->boq_y, (c3y == nam_u->aut_o) ? "auth" : "data", + (c3y == nam_u->nit_o) ? "init" : "nope", nam_u->fra_d ); c3_free(her_c); @@ -257,10 +258,13 @@ log_pact(u3_mesa_pact* pac_u) /* Helper utilities */ + +/* mesa_num_leaves(): calculate how many leaves, given total bytes +*/ c3_d -mesa_num_leaves(c3_d tot_d) +mesa_num_leaves(c3_d tob_d) { - return (tot_d + 1023) / 1024; + return (tob_d + 1023) / 1024; } void @@ -1450,7 +1454,7 @@ _test_make_data(void* ptr_v, u3_mesa_data* dat_u) case AUTH_HMAC: { dat_u->aut_u.len_y = _test_rand_gulf_y(ptr_v, 3); - _test_rand_bytes(ptr_v, 32, dat_u->aut_u.mac_y); + _test_rand_bytes(ptr_v, 16, dat_u->aut_u.mac_y); } break; default: break; From 23b4ebfcd699141f552168e8295fed198499e2a3 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Wed, 14 Aug 2024 15:44:26 +0200 Subject: [PATCH 360/430] mesa: wip --- pkg/vere/io/mesa.c | 15 +++++++++------ pkg/vere/io/mesa/pact.c | 2 +- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 3f8ca8bc33..055077547f 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1907,7 +1907,7 @@ _mesa_send_leaf(u3_mesa* sam_u, { u3_mesa_name* nam_u = &pac_u->pag_u.nam_u; u3_mesa_data* dat_u = &pac_u->pag_u.dat_u; - nam_u->nit_o = __(0 == fra_d); + // nam_u->nit_o = __(fra_d == 0); nam_u->fra_d = fra_d; c3_d i_d = fra_d - (lin_u->nam_u.fra_d * (1 << u3_Host.ops_u.jum_y)); c3_w cur_w = i_d * 1024; @@ -2084,10 +2084,12 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun res) lin_u->tip_y = c3_malloc(len_w); // note: off-loom lin_u->dat_y = lin_u->tip_y + tip_w; lin_u->haz_y = lin_u->dat_y + haz_w; - u3r_bytes(0, tip_w, lin_u->tip_y, pof); memcpy(lin_u->dat_y, dat_u->fra_y, dat_u->len_w); - u3r_bytes(0, haz_w, lin_u->haz_y, pas); - u3l_log("about to mesa_free_pact(&jum_u)"); + // u3r_bytes(0, haz_w, lin_u->haz_y, pas); + if ( tip_w > 0 ) { + u3r_bytes(0, tip_w, lin_u->tip_y, pof); + } + mesa_free_pact(&jum_u); } @@ -2517,8 +2519,9 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) lon_u); // TODO: check return value before continuing? - c3_y boq_y = u3_Host.ops_u.jum_y; - c3_o done_with_jumbo_frame = __(0 == (req_u->hav_d % (1 << (boq_y - 13)))); + c3_y boq_y = 31; + // c3_o done_with_jumbo_frame = __(0 == req_u->hav_d % boq_y); + c3_o done_with_jumbo_frame = __(req_u->hav_d == req_u->tof_d); // TODO: fix for non-message-sized jumbo frames _mesa_del_pit(sam_u, nam_u); if ( c3y == done_with_jumbo_frame ) { u3_noun cad; diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index 18aa411d3c..c20b744b8c 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -166,7 +166,7 @@ log_name(u3_mesa_name* nam_u) } u3l_log("%s: /%s", her_c, nam_u->pat_c); - u3l_log(" rift: %u bloq: %u auth/data: %s init: %s frag: %"PRIu64, + u3l_log(" rift: %u bloq: %u auth/data: %s init: %s frag: %"PRIu64, nam_u->rif_w, nam_u->boq_y, (c3y == nam_u->aut_o) ? "auth" : "data", From 8a05de1d3e7ea1504aa240195d0bcfa96d14be46 Mon Sep 17 00:00:00 2001 From: pkova Date: Wed, 14 Aug 2024 16:53:16 +0300 Subject: [PATCH 361/430] mesa: remove unnecessary conditional --- pkg/vere/io/mesa.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 055077547f..0435a3095f 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -2086,9 +2086,7 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun res) lin_u->haz_y = lin_u->dat_y + haz_w; memcpy(lin_u->dat_y, dat_u->fra_y, dat_u->len_w); // u3r_bytes(0, haz_w, lin_u->haz_y, pas); - if ( tip_w > 0 ) { - u3r_bytes(0, tip_w, lin_u->tip_y, pof); - } + u3r_bytes(0, tip_w, lin_u->tip_y, pof); mesa_free_pact(&jum_u); } From 253bed8a03b75322be68878ee09f813204dd4f72 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Thu, 15 Aug 2024 12:06:05 +0200 Subject: [PATCH 362/430] mesa: send responses for init/non-init requests --- pkg/vere/io/mesa.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 0435a3095f..c03f9a90c1 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -2015,7 +2015,7 @@ _mesa_send_jumbo_pieces(u3_mesa* sam_u, u3_mesa_line* lin_u, c3_d* fra_u) else { _mesa_send_leaf(sam_u, lin_u, &pac_u, *fra_u); } - // mesa_free_pact(&pac_u); // TODO reinstate + // mesa_free_pact(&pac_u); // TODO reinstate } static void From fa68bf38d286a4f782b7c0dd4efa3ce4ee5e1f8a Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Thu, 15 Aug 2024 14:46:26 +0200 Subject: [PATCH 363/430] mesa: fix poonter math --- pkg/vere/io/mesa.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index c03f9a90c1..d2a4d0ca1c 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1907,7 +1907,7 @@ _mesa_send_leaf(u3_mesa* sam_u, { u3_mesa_name* nam_u = &pac_u->pag_u.nam_u; u3_mesa_data* dat_u = &pac_u->pag_u.dat_u; - // nam_u->nit_o = __(fra_d == 0); + nam_u->fra_d = fra_d; c3_d i_d = fra_d - (lin_u->nam_u.fra_d * (1 << u3_Host.ops_u.jum_y)); c3_w cur_w = i_d * 1024; @@ -1915,12 +1915,12 @@ _mesa_send_leaf(u3_mesa* sam_u, dat_u->len_w = c3_min(lin_u->dat_w - cur_w, 1024); lss_pair* pair = ((lss_pair*)lin_u->haz_y) + i_d; - if ( 0 == nam_u->fra_d ) { - _mesa_copy_auth_data(&dat_u->aut_u, &lin_u->aut_u); - } - else if ( 0 == memcmp(pair, &(lss_pair){0}, sizeof(lss_pair)) ) { + + if ( 0 == memcmp(pair, &(lss_pair){0}, sizeof(lss_pair)) ) { dat_u->aut_u.typ_e = AUTH_NONE; - } else { + } else if ( 0 == nam_u->fra_d ) { + _mesa_copy_auth_data(&dat_u->aut_u, &lin_u->aut_u); + }else { dat_u->aut_u.typ_e = AUTH_PAIR; memcpy(dat_u->aut_u.has_y, pair, sizeof(lss_pair)); } @@ -2052,10 +2052,12 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun res) u3_mesa_line* lin_u; { u3a_atom* pat_u = u3a_to_ptr(pac); + u3_noun siz = u3r_met(3, pac); // XX refcount u3_mesa_pact jum_u; c3_c* err_c = mesa_sift_pact_from_buf(&jum_u, - (c3_y*)pat_u->buf_w, - pat_u->len_w << 2); + (c3_y*)pat_u->buf_w, + // pat_u->len_w << 2); + siz); if ( err_c ) { u3l_log("mesa: jumbo frame parse failure: %s", err_c); log_pact(pac_u); @@ -2083,10 +2085,11 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun res) lin_u->len_w = len_w; lin_u->tip_y = c3_malloc(len_w); // note: off-loom lin_u->dat_y = lin_u->tip_y + tip_w; - lin_u->haz_y = lin_u->dat_y + haz_w; + lin_u->haz_y = lin_u->dat_y + dat_w; memcpy(lin_u->dat_y, dat_u->fra_y, dat_u->len_w); - // u3r_bytes(0, haz_w, lin_u->haz_y, pas); + u3r_bytes(0, tip_w, lin_u->tip_y, pof); + u3r_bytes(0, haz_w, lin_u->haz_y, pas); mesa_free_pact(&jum_u); } From 279540e52357ebb86fd0efc40bef71960a6a2063 Mon Sep 17 00:00:00 2001 From: lukechampine Date: Thu, 15 Aug 2024 16:10:44 -0400 Subject: [PATCH 364/430] mesa: wip --- pkg/vere/io/mesa.c | 53 ++++++++--------------- pkg/vere/io/mesa/mesa.h | 22 +--------- pkg/vere/io/mesa/pact.c | 96 ++++++++++++++++++++++++++++------------- 3 files changed, 86 insertions(+), 85 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index d2a4d0ca1c..1290e9d654 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include "lss.h" @@ -368,7 +369,6 @@ _mesa_czar_dns(c3_y imp_y, c3_c* zar_c) c3_i sas_i = snprintf(dns_c, len_w, "%s.%s.", nam_c + 1, zar_c); u3_assert(sas_i <= 255); - u3l_log("about to free nam_c"); c3_free(nam_c); u3z(nam); @@ -620,15 +620,11 @@ _mesa_del_request(u3_mesa* sam_u, u3_mesa_name* nam_u) { // u3l_log("wat_u %p", req_u->wat_u); // u3l_log("was_u buf %p", req_u->was_u.buf_y); uv_timer_stop(&req_u->tim_u); - u3l_log("about to free_pict in del_request"); _mesa_free_pict(req_u->pic_u); - u3l_log("about to free wat_u in del_request"); c3_free(req_u->wat_u); - u3l_log("about to free dat_y in del_request"); c3_free(req_u->dat_y); lss_verifier_free(req_u->los_u); u3h_del(per_u->req_p, key); - u3l_log("about to free req_u in del_request"); u3a_free(req_u); u3z(key); } @@ -947,7 +943,6 @@ _mesa_send_cb(uv_udp_send_t* req_u, c3_i sas_i) //sam_u->fig_u.net_o = c3y; } - u3l_log("about to free seal"); _mesa_free_seal(sel_u); } @@ -992,7 +987,7 @@ static void _mesa_send(u3_mesa_pict* pic_u, u3_lane* lan_u) { u3_mesa* sam_u = pic_u->sam_u; c3_y buf_y[PACT_SIZE]; - c3_w len_w = mesa_etch_pact_to_buf(buf_y, &pic_u->pac_u); + c3_w len_w = mesa_etch_pact_to_buf(buf_y, PACT_SIZE, &pic_u->pac_u); _mesa_send_buf(sam_u, *lan_u, buf_y, len_w); } @@ -1116,7 +1111,7 @@ _try_resend(u3_pend_req* req_u, c3_d ack_d) los_o = c3y; pac_u->pek_u.nam_u.fra_d = i_d; - c3_w len_w = mesa_etch_pact_to_buf(buf_y, pac_u); + c3_w len_w = mesa_etch_pact_to_buf(buf_y, PACT_SIZE, pac_u); _mesa_send_modal(req_u->per_u, buf_y, len_w); } } @@ -1425,7 +1420,6 @@ _mesa_czar_cb(uv_getaddrinfo_t* adr_u, c3_i sas_i, struct addrinfo* aif_u) _mesa_czar_gone(sam_u, sas_i, imp_y, now_t); } - u3l_log("about to free adr_u"); c3_free(adr_u); uv_freeaddrinfo(aif_u); @@ -1598,7 +1592,6 @@ _mesa_resend_timer_cb(uv_timer_t* tim_u) #ifdef MESA_DEBUG u3l_log("mesa: resend PIT entry gone %u", res_u->ret_y); #endif - u3l_log("about to free resend data in resend-timer-cb"); _mesa_free_resend_data(res_u); return; } @@ -1615,7 +1608,6 @@ _mesa_resend_timer_cb(uv_timer_t* tim_u) uv_timer_start(&res_u->tim_u, _mesa_resend_timer_cb, 1000, 0); } else { - u3l_log("about to free resend data in resend-timer-cb 2"); _mesa_free_resend_data(res_u); } } @@ -1629,6 +1621,7 @@ _mesa_ef_send(u3_mesa* sam_u, u3_noun las, u3_noun pac) u3r_bytes(0, len_w, buf_y, pac); u3_mesa_pact pac_u; + memset(&pac_u, 0x11, sizeof(pac_u)); c3_c* err_c = mesa_sift_pact_from_buf(&pac_u, buf_y, len_w); if ( err_c ) { u3l_log("mesa: ef_send: sift failed: %u %s", len_w, err_c); @@ -1880,7 +1873,6 @@ _mesa_put_jumbo_cache(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_mesa_line* lin_u) u3_noun pax = _name_to_jumbo_scry(nam_u); u3_weak del = u3h_put_get(sam_u->pac_p, pax, u3a_outa(lin_u)); if ( u3_none != del ) { - u3l_log("about to free line"); _mesa_free_line(u3a_into(del)); u3z(del); } @@ -1894,7 +1886,7 @@ _mesa_send_pact(u3_mesa* sam_u, u3_mesa_pact* tac_u) { c3_y buf_y[PACT_SIZE]; - c3_w len_w = mesa_etch_pact_to_buf(buf_y, tac_u); + c3_w len_w = mesa_etch_pact_to_buf(buf_y, PACT_SIZE, tac_u); _mesa_send_bufs(sam_u, per_u, buf_y, len_w, u3k(las)); u3z(las); } @@ -1944,6 +1936,7 @@ _mesa_send_jumbo_pieces(u3_mesa* sam_u, u3_mesa_line* lin_u, c3_d* fra_u) #endif u3_mesa_pact pac_u = {0}; + memset(&pac_u, 0x11, sizeof(pac_u)); u3_mesa_head* hed_u = &pac_u.hed_u; { @@ -1958,7 +1951,7 @@ _mesa_send_jumbo_pieces(u3_mesa* sam_u, u3_mesa_line* lin_u, c3_d* fra_u) { _mesa_copy_name(nam_u, &lin_u->nam_u); nam_u->boq_y = 13; - nam_u->fra_d *= (1 << u3_Host.ops_u.jum_y); + nam_u->fra_d *= (1 << u3_Host.ops_u.jum_y); // XX looks sus } u3_mesa_data* dat_u = &pac_u.pag_u.dat_u; @@ -1984,7 +1977,6 @@ _mesa_send_jumbo_pieces(u3_mesa* sam_u, u3_mesa_line* lin_u, c3_d* fra_u) dat_u->fra_y = pro_y; _mesa_send_pact(sam_u, u3k(u3t(pin)), NULL, &pac_u); _mesa_del_pit(sam_u, nam_u); - u3l_log("about to free pro_y"); c3_free(pro_y); u3z(pin); } @@ -2051,13 +2043,14 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun res) u3_mesa_line* lin_u; { - u3a_atom* pat_u = u3a_to_ptr(pac); - u3_noun siz = u3r_met(3, pac); // XX refcount + c3_w jumbo_w = u3r_met(3, pac); + c3_y* jumbo_y = c3_calloc(jumbo_w); + u3r_bytes(0, jumbo_w, jumbo_y, pac); + u3z(pac); + + u3l_log("sifting jumbo packet len=%u", jumbo_w); u3_mesa_pact jum_u; - c3_c* err_c = mesa_sift_pact_from_buf(&jum_u, - (c3_y*)pat_u->buf_w, - // pat_u->len_w << 2); - siz); + c3_c* err_c = mesa_sift_pact_from_buf(&jum_u, jumbo_y, jumbo_w); if ( err_c ) { u3l_log("mesa: jumbo frame parse failure: %s", err_c); log_pact(pac_u); @@ -2066,6 +2059,7 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun res) } u3_mesa_data* dat_u = &jum_u.pag_u.dat_u; + c3_d mev_d = mesa_num_leaves(dat_u->tob_d); // leaves in message c3_w tip_w = // bytes in Merkle spine (mev_d > 1 && jum_u.pag_u.nam_u.fra_d == 0)? @@ -2333,7 +2327,6 @@ _mesa_page_news_cb(u3_ovum* egg_u, u3_ovum_news new_e) // XX do early delete instead, to avoid injecting retries // _mesa_del_pit(dat_u->per_u->sam_u, dat_u->nam_u); - u3l_log("about to free lane cb data"); _mesa_free_lane_cb_data(dat_u); } @@ -2343,7 +2336,6 @@ _mesa_page_bail_cb(u3_ovum* egg_u, u3_ovum_news new_e) #ifdef MESA_DEBUG u3l_log("mesa: arvo page event failed"); #endif - u3l_log("about to free lane cb data 2"); _mesa_free_lane_cb_data((u3_mesa_lane_cb_data*)egg_u->ptr_v); } @@ -2442,7 +2434,6 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) _mesa_send_pact(sam_u, u3k(las), per_u, pac_u); _mesa_del_pit(sam_u, nam_u); - u3l_log("about to free_pict() in hear_page()"); _mesa_free_pict(pic_u); u3z(pin); return; @@ -2466,7 +2457,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) // XX should just preserve input buffer u3i_slab sab_u; u3i_slab_init(&sab_u, 3, PACT_SIZE); - mesa_etch_pact_to_buf(sab_u.buf_y, pac_u); + mesa_etch_pact_to_buf(sab_u.buf_y, PACT_SIZE, pac_u); cad = u3nt(c3__heer, lan, u3i_slab_mint(&sab_u)); } @@ -2488,7 +2479,6 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) _mesa_del_pit(dat_u->per_u->sam_u, dat_u->nam_u); u3_auto_peer(ovo, dat_u, _mesa_page_news_cb, _mesa_page_bail_cb); - u3l_log("about to free_pict() 2 in hear_page()"); _mesa_free_pict(pic_u); u3z(pin); return; @@ -2544,7 +2534,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) pac_u->pag_u.dat_u.aut_u = req_u->aut_u; c3_y* buf_y = c3_calloc(mesa_size_pact(pac_u)); - c3_w res_w = mesa_etch_pact_to_buf(buf_y, pac_u); + c3_w res_w = mesa_etch_pact_to_buf(buf_y, mesa_size_pact(pac_u), pac_u); pac = u3i_bytes(res_w, buf_y); c3_free(buf_y); } @@ -2571,7 +2561,6 @@ _mesa_forward_request(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane lan_u) u3l_log("mesa: alien forward for %s", mes); c3_free(mes); #endif - u3l_log("about to free_pict() in forward_request()"); _mesa_free_pict(pic_u); return; } @@ -2579,7 +2568,6 @@ _mesa_forward_request(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane lan_u) u3_lane lin_u = _mesa_get_direct_lane(sam_u, pac_u->pek_u.nam_u.her_u); u3_lane zer_u = {0, 0}; if ( _mesa_lanes_equal(&zer_u, &lin_u) == c3y) { - u3l_log("about to free_pict() in forward_request() lanes_equal"); _mesa_free_pict(pic_u); return; } @@ -2591,7 +2579,6 @@ _mesa_forward_request(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane lan_u) _mesa_add_lane_to_pit(sam_u, &pac_u->pek_u.nam_u, lan_u); _mesa_send(pic_u, &lin_u); } - u3l_log("about to free_pict() in forward_request() end"); _mesa_free_pict(pic_u); } @@ -2624,7 +2611,6 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) if ( NULL != lin_u ) { if ( CTAG_ITEM == lin_u->typ_y ) { _mesa_send_jumbo_pieces(sam_u, lin_u, &fra_d); - u3l_log("about to free_pict after jumbo pieces"); _mesa_free_pict(pic_u); } return; @@ -2716,7 +2702,7 @@ _mesa_hear_poke(u3_mesa_pict* pic_u, u3_lane* lan_u) u3i_slab_init(&sab_u, 3, PACT_SIZE); // XX should just preserve input buffer - mesa_etch_pact_to_buf(sab_u.buf_y, pac_u); + mesa_etch_pact_to_buf(sab_u.buf_y, PACT_SIZE, pac_u); cad = u3nt(c3__heer, lan, u3i_slab_mint(&sab_u)); } @@ -2762,7 +2748,6 @@ _mesa_hear(u3_mesa* sam_u, mesa_free_pact(&pic_u->pac_u); return; } - u3l_log("about to free hun_y"); c3_free(hun_y); struct sockaddr_in* add_u = (struct sockaddr_in*)adr_u; @@ -2798,14 +2783,12 @@ static void _mesa_recv_cb(uv_udp_t* wax_u, c3_free(buf_u->base); } else if ( 0 == nrd_i ) { - u3l_log("about to free buf_u->base"); c3_free(buf_u->base); } else if ( flg_i & UV_UDP_PARTIAL ) { if ( u3C.wag_w & u3o_verbose ) { u3l_log("mesa: recv: fail: message truncated"); } - u3l_log("about to free buf_u->base 2"); c3_free(buf_u->base); } else { diff --git a/pkg/vere/io/mesa/mesa.h b/pkg/vere/io/mesa/mesa.h index dfe09e0c89..d9fa43856a 100644 --- a/pkg/vere/io/mesa/mesa.h +++ b/pkg/vere/io/mesa/mesa.h @@ -142,33 +142,13 @@ typedef struct _u3_mesa_pact { }; } u3_mesa_pact; -typedef struct _u3_etcher { - c3_y* buf_y; - c3_w len_w; - c3_d bit_d; // for _etch_bits - c3_y off_y; // for _etch_bits -} u3_etcher; - -typedef struct _u3_sifter { - c3_y* buf_y; - c3_w rem_w; - c3_d bit_d; // for _etch_bits - c3_y off_y; // for _etch_bits - c3_c* err_c; -} u3_sifter; - c3_d mesa_num_leaves(c3_d tot_d); c3_w mesa_size_pact(u3_mesa_pact* pac_u); c3_o mesa_is_new_pact(c3_y* buf_y, c3_w len_w); -void etcher_init(u3_etcher* ech_u, c3_y* buf_y); -void sifter_init(u3_sifter* sif_u, c3_y* buf_y, c3_w len_w); -void mesa_sift_head(u3_sifter* sif_u, u3_mesa_head* hed_u); -void mesa_sift_pact(u3_sifter* sif_u, u3_mesa_pact* pac_u); -void mesa_etch_pact(u3_etcher* ech_u, u3_mesa_pact* pac_u); void mesa_free_pact(u3_mesa_pact* pac_u); -c3_w mesa_etch_pact_to_buf(c3_y* buf_y, u3_mesa_pact *pac_u); +c3_w mesa_etch_pact_to_buf(c3_y* buf_y, c3_w cap_w, u3_mesa_pact *pac_u); c3_c* mesa_sift_pact_from_buf(u3_mesa_pact *pac_u, c3_y* buf_y, c3_w len_w); void inc_hopcount(u3_mesa_head*); diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index c20b744b8c..d6f8a4c460 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -1,6 +1,7 @@ #include "mesa.h" #include #include +#include #include // only need for tests, can remove #include "vere.h" @@ -138,10 +139,11 @@ _log_head(u3_mesa_head* hed_u) static void _log_buf(c3_y* buf_y, c3_w len_w) { + c3_c *buf_c = c3_malloc(2 * len_w + 1); for( c3_w i_w = 0; i_w < len_w; i_w++ ) { - fprintf(stderr, "%02x", buf_y[i_w]); + sprintf(buf_c + (i_w*2), "%02x", buf_y[i_w]); } - fprintf(stderr, "\r\n"); + u3l_log("%s", buf_c); } static void @@ -180,17 +182,17 @@ log_name(u3_mesa_name* nam_u) static void _log_data(u3_mesa_data* dat_u) { - fprintf(stderr, "tob_d: %" PRIu64 " len_w: %u ", + u3l_log("tob_d: %" PRIu64 " len_w: %u ", dat_u->tob_d, dat_u->len_w); switch ( dat_u->aut_u.typ_e ) { case AUTH_SIGN: { - fprintf(stderr, "signature: "); + u3l_log("signature: "); _log_buf(dat_u->aut_u.sig_y, 64); } break; case AUTH_HMAC: { - fprintf(stderr, "hmac: "); + u3l_log("hmac: "); _log_buf(dat_u->aut_u.mac_y, 16); } break; @@ -204,8 +206,6 @@ _log_data(u3_mesa_data* dat_u) _log_buf(dat_u->aut_u.has_y[1], 32); } break; } - fprintf(stderr, "\r\n"); - fflush(stderr); } @@ -337,16 +337,33 @@ void mesa_free_pact(u3_mesa_pact* pac_u) /* serialisation */ -void -etcher_init(u3_etcher* ech_u, c3_y* buf_y) +typedef struct _u3_etcher { + c3_y* buf_y; + c3_w len_w; + c3_w cap_w; + c3_d bit_d; // for _etch_bits + c3_y off_y; // for _etch_bits +} u3_etcher; + +typedef struct _u3_sifter { + c3_y* buf_y; + c3_w rem_w; + c3_d bit_d; // for _etch_bits + c3_y off_y; // for _etch_bits + c3_c* err_c; +} u3_sifter; + +static void +etcher_init(u3_etcher* ech_u, c3_y* buf_y, c3_w cap_w) { ech_u->buf_y = buf_y; ech_u->len_w = 0; + ech_u->cap_w = cap_w; ech_u->bit_d = 0; ech_u->off_y = 0; } -void +static void sifter_init(u3_sifter* sif_u, c3_y* buf_y, c3_w len_w) { sif_u->buf_y = buf_y; @@ -365,6 +382,7 @@ _sift_fail(u3_sifter* sif_u, c3_c* msg_c) sif_u->err_c = msg_c; #ifdef MESA_ROUNDTRIP u3l_log(RED_TEXT "sift fail: %s" DEF_TEXT, msg_c); + u3m_bail(c3__czar); assert(!"sift fail"); #endif } @@ -373,6 +391,7 @@ static c3_y* _etch_next(u3_etcher* ech_u, c3_w len_w) { assert ( ech_u->off_y == 0 ); // ensure all bits were etched + assert ( ech_u->len_w + len_w <= ech_u->cap_w ); // ensure buffer is big enough c3_y *res_y = ech_u->buf_y + ech_u->len_w; ech_u->len_w += len_w; return res_y; @@ -477,7 +496,7 @@ _etch_var_word(u3_etcher* ech_u, c3_w val_w, c3_w len_w) { assert ( len_w <= 4 ); c3_y *buf_y = _etch_next(ech_u, len_w); - for( int i = 0; i < len_w; i++ ) { + for( c3_w i = 0; i < len_w; i++ ) { buf_y[i] |= (val_w >> (8*i)) & 0xFF; } } @@ -491,7 +510,7 @@ _sift_var_word(u3_sifter* sif_u, c3_w len_w) return 0; } c3_w val_w = 0; - for( int i = 0; i < len_w; i++ ) { + for( c3_w i = 0; i < len_w; i++ ) { val_w |= (res_y[i] << (8*i)); } return val_w; @@ -633,6 +652,8 @@ _mesa_etch_name(u3_etcher *ech_u, u3_mesa_name* nam_u) _etch_bits(ech_u, 1, met_u.nit_y); _etch_bits(ech_u, 1, met_u.tau_y); _etch_bits(ech_u, 2, met_u.gaf_y); + + c3_y her_y = 2 << met_u.ran_y; // XX confirm _etch_ship(ech_u, nam_u->her_u, her_y); _etch_var_word(ech_u, nam_u->rif_w, met_u.rif_y + 1); @@ -659,18 +680,18 @@ _mesa_sift_name(u3_sifter* sif_u, u3_mesa_name* nam_u) met_u.tau_y = _sift_bits(sif_u, 1); met_u.gaf_y = _sift_bits(sif_u, 2); - nam_u->nit_o = ( met_u.nit_y ) ? c3y : c3n; - nam_u->aut_o = ( met_u.tau_y ) ? c3y : c3n; + nam_u->nit_o = __( met_u.nit_y == 1 ); + nam_u->aut_o = __( met_u.tau_y == 1 ); _sift_ship(sif_u, nam_u->her_u, 2 << met_u.ran_y); nam_u->rif_w = _sift_var_word(sif_u, met_u.rif_y + 1); nam_u->boq_y = _sift_byte(sif_u); - nam_u->fra_d = 0; if ( met_u.nit_y ) { assert( !met_u.tau_y ); assert( !met_u.gaf_y ); // XX init packet + nam_u->fra_d = 0; } else { nam_u->fra_d = _sift_var_word(sif_u, 1 << met_u.gaf_y); @@ -752,7 +773,10 @@ _mesa_sift_data(u3_sifter* sif_u, u3_mesa_data* dat_u) } } - c3_y nel_y = (met_u.men_y < 3) ? met_u.men_y : _sift_byte(sif_u); + c3_y nel_y = met_u.men_y; + if ( 3 == met_u.men_y ) { + nel_y = _sift_byte(sif_u); + } dat_u->len_w = _sift_var_word(sif_u, nel_y); dat_u->fra_y = _sift_bytes_alloc(sif_u, dat_u->len_w); } @@ -856,21 +880,24 @@ _mesa_sift_poke_pact(u3_sifter* sif_u, u3_mesa_poke_pact* pac_u) _mesa_sift_data(sif_u, &pac_u->dat_u); } -void -mesa_etch_pact(u3_etcher* ech_u, u3_mesa_pact* pac_u) +static void +_mesa_etch_pact(u3_etcher* ech_u, u3_mesa_pact* pac_u) { // use a separate etcher to make computing the mug easier u3_etcher pec_u; - etcher_init(&pec_u, ech_u->buf_y + ech_u->len_w + 8); + etcher_init(&pec_u, ech_u->buf_y + ech_u->len_w + 8, ech_u->cap_w - ech_u->len_w - 8); switch ( pac_u->hed_u.typ_y ) { case PACT_POKE: { + u3l_log("etch PACT_POKE"); _mesa_etch_poke_pact(&pec_u, &pac_u->pok_u); } break; case PACT_PAGE: { + u3l_log("etch PACT_PAGE"); _mesa_etch_page_pact(&pec_u, &pac_u->pag_u, &pac_u->hed_u); } break; case PACT_PEEK: { + u3l_log("etch PACT_PEEK"); _mesa_etch_peek_pact(&pec_u, &pac_u->pek_u); } break; default: { @@ -887,8 +914,8 @@ mesa_etch_pact(u3_etcher* ech_u, u3_mesa_pact* pac_u) ech_u->len_w += pec_u.len_w; } -void -mesa_sift_pact(u3_sifter* sif_u, u3_mesa_pact* pac_u) +static void +_mesa_sift_pact(u3_sifter* sif_u, u3_mesa_pact* pac_u) { mesa_sift_head(sif_u, &pac_u->hed_u); @@ -898,12 +925,15 @@ mesa_sift_pact(u3_sifter* sif_u, u3_mesa_pact* pac_u) switch ( pac_u->hed_u.typ_y ) { case PACT_PEEK: { + u3l_log("sift PACT_PEEK"); _mesa_sift_peek_pact(sif_u, &pac_u->pek_u); } break; case PACT_PAGE: { + u3l_log("sift PACT_PAGE"); _mesa_sift_page_pact(sif_u, &pac_u->pag_u, pac_u->hed_u.nex_y); } break; case PACT_POKE: { + u3l_log("sift PACT_POKE"); _mesa_sift_poke_pact(sif_u, &pac_u->pok_u); } break; default: { @@ -925,16 +955,20 @@ mesa_sift_pact(u3_sifter* sif_u, u3_mesa_pact* pac_u) /* packet etch/sift, with roundtrip tests */ c3_w -mesa_etch_pact_to_buf(c3_y* buf_y, u3_mesa_pact *pac_u) { +mesa_etch_pact_to_buf(c3_y* buf_y, c3_w cap_w, u3_mesa_pact *pac_u) { u3_etcher ech_u; - etcher_init(&ech_u, buf_y); - mesa_etch_pact(&ech_u, pac_u); + etcher_init(&ech_u, buf_y, cap_w); + _mesa_etch_pact(&ech_u, pac_u); + + u3l_log("etched packet:"); + _log_buf(ech_u.buf_y, ech_u.len_w); #ifdef MESA_ROUNDTRIP u3_mesa_pact poc_u; + memset(&poc_u, 0x11, sizeof(poc_u)); u3_sifter sif_u; sifter_init(&sif_u, ech_u.buf_y, ech_u.len_w); - mesa_sift_pact(&sif_u, &poc_u); + _mesa_sift_pact(&sif_u, &poc_u); if ( sif_u.rem_w && !sif_u.err_c ) { u3l_log("mesa: etch roundtrip failed: %u trailing bytes", sif_u.rem_w); _sift_fail(&sif_u, "trailing bytes"); @@ -943,6 +977,9 @@ mesa_etch_pact_to_buf(c3_y* buf_y, u3_mesa_pact *pac_u) { u3l_log("mesa: roundtrip failed: %s", sif_u.err_c); assert(!"roundtrip failed"); } + u3l_log("roundtrip:"); + log_pact(pac_u); + log_pact(&poc_u); _mesa_check_pacts_equal(&poc_u, pac_u); mesa_free_pact(&poc_u); #endif @@ -952,9 +989,10 @@ mesa_etch_pact_to_buf(c3_y* buf_y, u3_mesa_pact *pac_u) { c3_c* mesa_sift_pact_from_buf(u3_mesa_pact *pac_u, c3_y* buf_y, c3_w len_w) { + memset(pac_u, 0x11, sizeof(*pac_u)); u3_sifter sif_u; sifter_init(&sif_u, buf_y, len_w); - mesa_sift_pact(&sif_u, pac_u); + _mesa_sift_pact(&sif_u, pac_u); if ( sif_u.rem_w && !sif_u.err_c ) { _sift_fail(&sif_u, "trailing bytes"); } @@ -962,8 +1000,8 @@ mesa_sift_pact_from_buf(u3_mesa_pact *pac_u, c3_y* buf_y, c3_w len_w) { #ifdef MESA_ROUNDTRIP c3_y* bof_y = c3_calloc(len_w); u3_etcher ech_u; - etcher_init(&ech_u, bof_y); - mesa_etch_pact(&ech_u, pac_u); + etcher_init(&ech_u, bof_y, len_w); + _mesa_etch_pact(&ech_u, pac_u); u3_assert( 0 == memcmp(bof_y, buf_y, len_w) ); c3_free(bof_y); #endif @@ -1230,7 +1268,7 @@ _test_pact(u3_mesa_pact* pac_u) ret_i = 1; goto done; } - if ( len_w != (sif_w = mesa_sift_pact(&nex_u, buf_y, len_w)) ) { + if ( len_w != (sif_w = _mesa_sift_pact(&nex_u, buf_y, len_w)) ) { fprintf(stderr, "pact: sift failed len=%u sif=%u\r\n", len_w, sif_w); _log_buf(buf_y, len_w); ret_i = 1; goto done; From 6ececccc9145e67447081a8d2b3dd6aad7c61d07 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Fri, 16 Aug 2024 15:31:31 +0200 Subject: [PATCH 365/430] mesa: bugfixes --- pkg/vere/io/mesa.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 1290e9d654..1718c3cd19 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -986,7 +986,7 @@ static void _mesa_send_buf(u3_mesa* sam_u, u3_lane lan_u, c3_y* buf_y, c3_w len_ static void _mesa_send(u3_mesa_pict* pic_u, u3_lane* lan_u) { u3_mesa* sam_u = pic_u->sam_u; - c3_y buf_y[PACT_SIZE]; + c3_y *buf_y = c3_calloc(PACT_SIZE); c3_w len_w = mesa_etch_pact_to_buf(buf_y, PACT_SIZE, &pic_u->pac_u); _mesa_send_buf(sam_u, *lan_u, buf_y, len_w); } @@ -1885,7 +1885,7 @@ _mesa_send_pact(u3_mesa* sam_u, u3_peer* per_u, // null for response packets u3_mesa_pact* tac_u) { - c3_y buf_y[PACT_SIZE]; + c3_y* buf_y = c3_calloc(PACT_SIZE); c3_w len_w = mesa_etch_pact_to_buf(buf_y, PACT_SIZE, tac_u); _mesa_send_bufs(sam_u, per_u, buf_y, len_w, u3k(las)); u3z(las); @@ -2046,7 +2046,6 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun res) c3_w jumbo_w = u3r_met(3, pac); c3_y* jumbo_y = c3_calloc(jumbo_w); u3r_bytes(0, jumbo_w, jumbo_y, pac); - u3z(pac); u3l_log("sifting jumbo packet len=%u", jumbo_w); u3_mesa_pact jum_u; @@ -2478,7 +2477,6 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) // _mesa_del_pit(dat_u->per_u->sam_u, dat_u->nam_u); u3_auto_peer(ovo, dat_u, _mesa_page_news_cb, _mesa_page_bail_cb); - _mesa_free_pict(pic_u); u3z(pin); return; From fea4b85549e407735b605c5a89b5052ba941d194 Mon Sep 17 00:00:00 2001 From: lukechampine Date: Fri, 16 Aug 2024 11:50:39 -0400 Subject: [PATCH 366/430] mesa: cleanup --- pkg/vere/io/mesa.c | 1 - pkg/vere/io/mesa/pact.c | 33 ++++++++------------------------- 2 files changed, 8 insertions(+), 26 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 1718c3cd19..b90376ff16 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -2058,7 +2058,6 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun res) } u3_mesa_data* dat_u = &jum_u.pag_u.dat_u; - c3_d mev_d = mesa_num_leaves(dat_u->tob_d); // leaves in message c3_w tip_w = // bytes in Merkle spine (mev_d > 1 && jum_u.pag_u.nam_u.fra_d == 0)? diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index d6f8a4c460..a565aef241 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -140,7 +140,7 @@ static void _log_buf(c3_y* buf_y, c3_w len_w) { c3_c *buf_c = c3_malloc(2 * len_w + 1); - for( c3_w i_w = 0; i_w < len_w; i_w++ ) { + for ( c3_w i_w = 0; i_w < len_w; i_w++ ) { sprintf(buf_c + (i_w*2), "%02x", buf_y[i_w]); } u3l_log("%s", buf_c); @@ -382,7 +382,6 @@ _sift_fail(u3_sifter* sif_u, c3_c* msg_c) sif_u->err_c = msg_c; #ifdef MESA_ROUNDTRIP u3l_log(RED_TEXT "sift fail: %s" DEF_TEXT, msg_c); - u3m_bail(c3__czar); assert(!"sift fail"); #endif } @@ -496,7 +495,7 @@ _etch_var_word(u3_etcher* ech_u, c3_w val_w, c3_w len_w) { assert ( len_w <= 4 ); c3_y *buf_y = _etch_next(ech_u, len_w); - for( c3_w i = 0; i < len_w; i++ ) { + for ( c3_w i = 0; i < len_w; i++ ) { buf_y[i] |= (val_w >> (8*i)) & 0xFF; } } @@ -510,7 +509,7 @@ _sift_var_word(u3_sifter* sif_u, c3_w len_w) return 0; } c3_w val_w = 0; - for( c3_w i = 0; i < len_w; i++ ) { + for ( c3_w i = 0; i < len_w; i++ ) { val_w |= (res_y[i] << (8*i)); } return val_w; @@ -521,7 +520,7 @@ _etch_var_chub(u3_etcher* ech_u, c3_d val_d, c3_w len_w) { assert ( len_w <= 8 ); c3_y *buf_y = _etch_next(ech_u, len_w); - for( int i = 0; i < len_w; i++ ) { + for ( int i = 0; i < len_w; i++ ) { buf_y[i] |= (val_d >> (8*i)) & 0xFF; } } @@ -535,7 +534,7 @@ _sift_var_chub(u3_sifter* sif_u, c3_w len_w) return 0; } c3_d val_d = 0; - for( int i = 0; i < len_w; i++ ) { + for ( int i = 0; i < len_w; i++ ) { val_d |= (res_y[i] << (8*i)); } return val_d; @@ -652,8 +651,6 @@ _mesa_etch_name(u3_etcher *ech_u, u3_mesa_name* nam_u) _etch_bits(ech_u, 1, met_u.nit_y); _etch_bits(ech_u, 1, met_u.tau_y); _etch_bits(ech_u, 2, met_u.gaf_y); - - c3_y her_y = 2 << met_u.ran_y; // XX confirm _etch_ship(ech_u, nam_u->her_u, her_y); _etch_var_word(ech_u, nam_u->rif_w, met_u.rif_y + 1); @@ -812,7 +809,7 @@ _mesa_etch_page_pact(u3_etcher* ech_u, u3_mesa_page_pact* pag_u, u3_mesa_head* h } break; case HOP_MANY: { _etch_byte(ech_u, pag_u->man_u.len_w); - for( c3_w i = 0; i < pag_u->man_u.len_w; i++ ) { + for ( c3_w i = 0; i < pag_u->man_u.len_w; i++ ) { _mesa_etch_hop_long(ech_u, &pag_u->man_u.dat_y[i]); } } break; @@ -844,7 +841,7 @@ _mesa_sift_page_pact(u3_sifter* sif_u, u3_mesa_page_pact* pag_u, c3_y nex_y) case HOP_MANY: { pag_u->man_u.len_w = _sift_byte(sif_u); pag_u->man_u.dat_y = c3_calloc(sizeof(u3_mesa_hop_once) * pag_u->man_u.len_w); - for( c3_w i = 0; i < pag_u->man_u.len_w; i++ ) { + for ( c3_w i = 0; i < pag_u->man_u.len_w; i++ ) { _mesa_sift_hop_long(sif_u, &pag_u->man_u.dat_y[i]); } return; @@ -889,15 +886,12 @@ _mesa_etch_pact(u3_etcher* ech_u, u3_mesa_pact* pac_u) switch ( pac_u->hed_u.typ_y ) { case PACT_POKE: { - u3l_log("etch PACT_POKE"); _mesa_etch_poke_pact(&pec_u, &pac_u->pok_u); } break; case PACT_PAGE: { - u3l_log("etch PACT_PAGE"); _mesa_etch_page_pact(&pec_u, &pac_u->pag_u, &pac_u->hed_u); } break; case PACT_PEEK: { - u3l_log("etch PACT_PEEK"); _mesa_etch_peek_pact(&pec_u, &pac_u->pek_u); } break; default: { @@ -925,15 +919,12 @@ _mesa_sift_pact(u3_sifter* sif_u, u3_mesa_pact* pac_u) switch ( pac_u->hed_u.typ_y ) { case PACT_PEEK: { - u3l_log("sift PACT_PEEK"); _mesa_sift_peek_pact(sif_u, &pac_u->pek_u); } break; case PACT_PAGE: { - u3l_log("sift PACT_PAGE"); _mesa_sift_page_pact(sif_u, &pac_u->pag_u, pac_u->hed_u.nex_y); } break; case PACT_POKE: { - u3l_log("sift PACT_POKE"); _mesa_sift_poke_pact(sif_u, &pac_u->pok_u); } break; default: { @@ -960,12 +951,8 @@ mesa_etch_pact_to_buf(c3_y* buf_y, c3_w cap_w, u3_mesa_pact *pac_u) { etcher_init(&ech_u, buf_y, cap_w); _mesa_etch_pact(&ech_u, pac_u); - u3l_log("etched packet:"); - _log_buf(ech_u.buf_y, ech_u.len_w); - #ifdef MESA_ROUNDTRIP u3_mesa_pact poc_u; - memset(&poc_u, 0x11, sizeof(poc_u)); u3_sifter sif_u; sifter_init(&sif_u, ech_u.buf_y, ech_u.len_w); _mesa_sift_pact(&sif_u, &poc_u); @@ -977,9 +964,6 @@ mesa_etch_pact_to_buf(c3_y* buf_y, c3_w cap_w, u3_mesa_pact *pac_u) { u3l_log("mesa: roundtrip failed: %s", sif_u.err_c); assert(!"roundtrip failed"); } - u3l_log("roundtrip:"); - log_pact(pac_u); - log_pact(&poc_u); _mesa_check_pacts_equal(&poc_u, pac_u); mesa_free_pact(&poc_u); #endif @@ -989,7 +973,6 @@ mesa_etch_pact_to_buf(c3_y* buf_y, c3_w cap_w, u3_mesa_pact *pac_u) { c3_c* mesa_sift_pact_from_buf(u3_mesa_pact *pac_u, c3_y* buf_y, c3_w len_w) { - memset(pac_u, 0x11, sizeof(*pac_u)); u3_sifter sif_u; sifter_init(&sif_u, buf_y, len_w); _mesa_sift_pact(&sif_u, pac_u); @@ -1087,7 +1070,7 @@ _mesa_size_hops(u3_mesa_pact* pac_u) case HOP_LONG: return 1 + pac_u->pag_u.one_u.len_w; case HOP_MANY: { c3_w siz_w = 0; - for( c3_w i = 0; i < pac_u->pag_u.man_u.len_w; i++ ) { + for ( c3_w i = 0; i < pac_u->pag_u.man_u.len_w; i++ ) { siz_w += 1 + pac_u->pag_u.man_u.dat_y[i].len_w; } return siz_w; From 8fe8e87d70b448feaf101557ff3d3b2bb35e9548 Mon Sep 17 00:00:00 2001 From: lukechampine Date: Fri, 16 Aug 2024 15:07:45 -0400 Subject: [PATCH 367/430] mesa: always overwrite full bytes when etching --- pkg/vere/io/mesa/pact.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index a565aef241..48ac86d1f4 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -496,7 +496,7 @@ _etch_var_word(u3_etcher* ech_u, c3_w val_w, c3_w len_w) assert ( len_w <= 4 ); c3_y *buf_y = _etch_next(ech_u, len_w); for ( c3_w i = 0; i < len_w; i++ ) { - buf_y[i] |= (val_w >> (8*i)) & 0xFF; + buf_y[i] = (val_w >> (8*i)) & 0xFF; } } @@ -521,7 +521,7 @@ _etch_var_chub(u3_etcher* ech_u, c3_d val_d, c3_w len_w) assert ( len_w <= 8 ); c3_y *buf_y = _etch_next(ech_u, len_w); for ( int i = 0; i < len_w; i++ ) { - buf_y[i] |= (val_d >> (8*i)) & 0xFF; + buf_y[i] = (val_d >> (8*i)) & 0xFF; } } From 37d721ebb5b4ab301574131894784cde31cd0bb5 Mon Sep 17 00:00:00 2001 From: lukechampine Date: Fri, 16 Aug 2024 15:09:33 -0400 Subject: [PATCH 368/430] mesa: zero-initialize local structs when etching/sifting --- pkg/vere/io/mesa/pact.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index 48ac86d1f4..f88fe72a51 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -631,7 +631,7 @@ mesa_sift_head(u3_sifter* sif_u, u3_mesa_head* hed_u) static void _mesa_etch_name(u3_etcher *ech_u, u3_mesa_name* nam_u) { - u3_mesa_name_meta met_u; + u3_mesa_name_meta met_u = {0}; met_u.ran_y = _mesa_rank(nam_u->her_u); met_u.rif_y = safe_dec(_mesa_met3_w(nam_u->rif_w)); @@ -670,7 +670,7 @@ _mesa_etch_name(u3_etcher *ech_u, u3_mesa_name* nam_u) static void _mesa_sift_name(u3_sifter* sif_u, u3_mesa_name* nam_u) { - u3_mesa_name_meta met_u; + u3_mesa_name_meta met_u = {0}; met_u.ran_y = _sift_bits(sif_u, 2); met_u.rif_y = _sift_bits(sif_u, 2); met_u.nit_y = _sift_bits(sif_u, 1); @@ -704,7 +704,7 @@ _mesa_sift_name(u3_sifter* sif_u, u3_mesa_name* nam_u) static void _mesa_etch_data(u3_etcher* ech_u, u3_mesa_data* dat_u) { - u3_mesa_data_meta met_u; + u3_mesa_data_meta met_u = {0}; met_u.bot_y = _mesa_make_chub_tag(dat_u->tob_d); met_u.aut_o = __(dat_u->aut_u.typ_e == AUTH_SIGN || dat_u->aut_u.typ_e == AUTH_HMAC); met_u.auv_o = __(dat_u->aut_u.typ_e == AUTH_SIGN || dat_u->aut_u.typ_e == AUTH_NONE); @@ -741,7 +741,7 @@ _mesa_etch_data(u3_etcher* ech_u, u3_mesa_data* dat_u) static void _mesa_sift_data(u3_sifter* sif_u, u3_mesa_data* dat_u) { - u3_mesa_data_meta met_u; + u3_mesa_data_meta met_u = {0}; met_u.bot_y = _sift_bits(sif_u, 2); met_u.aut_o = _sift_bits(sif_u, 1); met_u.auv_o = _sift_bits(sif_u, 1); From 8478f5c2d3643be033953c1dabd4f53a75295f4b Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Tue, 20 Aug 2024 10:21:11 +0200 Subject: [PATCH 369/430] mesa: account for null lss pairs --- pkg/vere/io/mesa.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index b90376ff16..ed07713947 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -2080,8 +2080,14 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun res) lin_u->haz_y = lin_u->dat_y + dat_w; memcpy(lin_u->dat_y, dat_u->fra_y, dat_u->len_w); + c3_y* haz_y = lin_u->haz_y; + while ( pas != u3_nul ) { + u3r_bytes(0, 64, haz_y, u3h(pas)); + haz_y += 64; + pas = u3t(pas); + } + u3r_bytes(0, tip_w, lin_u->tip_y, pof); - u3r_bytes(0, haz_w, lin_u->haz_y, pas); mesa_free_pact(&jum_u); } From 5b57c96009ffbe789cb7ac20e4f9d9ac415eafd2 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Wed, 21 Aug 2024 16:06:58 +0200 Subject: [PATCH 370/430] mesa: correct 2-bit tag for 3-byte fragments --- pkg/vere/io/mesa/pact.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index f88fe72a51..075d91a119 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -294,7 +294,7 @@ _mesa_make_chub_tag(c3_d tot_d) { return (tot_d <= 0xff)? 0b00 : (tot_d <= 0xffff)? 0b01 : - (tot_d <= 0xffffffff)? 0b10 : + (tot_d <= 0xffffff)? 0b10 : 0b11; } From 97ec30f76e757a295c60b8f2b71bb61351fbdd4d Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Wed, 21 Aug 2024 16:08:00 +0200 Subject: [PATCH 371/430] mesa: don't delete from pit after _mesa_req_pact_done --- pkg/vere/io/mesa.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index ed07713947..dc1017c003 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -2516,7 +2516,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) c3_y boq_y = 31; // c3_o done_with_jumbo_frame = __(0 == req_u->hav_d % boq_y); c3_o done_with_jumbo_frame = __(req_u->hav_d == req_u->tof_d); // TODO: fix for non-message-sized jumbo frames - _mesa_del_pit(sam_u, nam_u); + // _mesa_del_pit(sam_u, nam_u); XX if ( c3y == done_with_jumbo_frame ) { u3_noun cad; From 86f2840638f7f9361006fa27a9e3b1e6c638f12c Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Wed, 21 Aug 2024 16:09:09 +0200 Subject: [PATCH 372/430] mesa: early return after hearing fragment outside window --- pkg/vere/io/mesa.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index dc1017c003..a30ade7ee9 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1234,8 +1234,12 @@ _mesa_req_pact_done(u3_pend_req* req_u, if ( req_u->los_u->counter != nam_u->fra_d ) { if ( nam_u->fra_d < req_u->los_u->counter ) { u3l_log("fragment number too low: %"PRIu64, nam_u->fra_d); + c3_free(par_u); + return; } else if ( nam_u->fra_d >= req_u->los_u->counter + (sizeof(req_u->mis_u)/sizeof(u3_misord_buf)) ) { u3l_log("fragment number too high: %"PRIu64, nam_u->fra_d); + c3_free(par_u); + return; } else { // insert into misordered queue u3_misord_buf* buf_u = &req_u->mis_u[nam_u->fra_d - req_u->los_u->counter - 1]; @@ -1243,6 +1247,7 @@ _mesa_req_pact_done(u3_pend_req* req_u, buf_u->len_w = dat_u->len_w; memcpy(buf_u->fra_y, dat_u->fra_y, dat_u->len_w); buf_u->par_u = par_u; + u3l_log("insert into misordered queue fra: %llu", nam_u->fra_d ); } } else if ( c3y != lss_verifier_ingest(req_u->los_u, dat_u->fra_y, dat_u->len_w, par_u) ) { From 764ed6d4e7a1c15e35c545d1e9c5d9b1103d9379 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Fri, 23 Aug 2024 16:28:47 +0200 Subject: [PATCH 373/430] mesa: only delete from bitset if fragmen was in flight --- pkg/vere/io/mesa.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index a30ade7ee9..6154825daf 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1268,7 +1268,6 @@ _mesa_req_pact_done(u3_pend_req* req_u, c3_free(par_u); } - bitset_del(&req_u->was_u, nam_u->fra_d); if ( nam_u->fra_d > req_u->ack_d ) { req_u->ack_d = nam_u->fra_d; } @@ -1282,9 +1281,10 @@ _mesa_req_pact_done(u3_pend_req* req_u, req_u->hav_d++; #ifdef MESA_DEBUG - u3l_log("fragment %"PRIu64" len %"PRIu64, nam_u->fra_d, req_u->hav_d); + u3l_log("fragment %llu hav_d %llu nex_d %llu ack_d %llu lef_d %llu old_d %llu", nam_u->fra_d, req_u->hav_d, req_u->nex_d, req_u->ack_d, req_u->lef_d, req_u->old_d); #endif if ( req_u->lef_d == nam_u->fra_d ) { + bitset_del(&req_u->was_u, nam_u->fra_d); req_u->lef_d++; } From 1528badbc7ee17b893086e65ff76c980e702563c Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Fri, 23 Aug 2024 16:30:14 +0200 Subject: [PATCH 374/430] mesa: on acked page, resend up to the next fragment --- pkg/vere/io/mesa.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 6154825daf..cfc3bcf83f 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1305,7 +1305,7 @@ _mesa_req_pact_done(u3_pend_req* req_u, c3_w siz_w = (1 << (nam_u->boq_y - 3)); memcpy(req_u->dat_y + (siz_w * nam_u->fra_d), dat_u->fra_y, dat_u->len_w); - _try_resend(req_u, nam_u->fra_d); + _try_resend(req_u, req_u->nex_d); // _try_resend(req_u, nam_u->fra_d); _update_resend_timer(req_u); } From 1802bb649fef38ebcc16adbda48b1de01bef42c8 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Tue, 27 Aug 2024 11:22:28 +0200 Subject: [PATCH 375/430] mesa: always bitset_del when adding to data buffer --- pkg/vere/io/mesa.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index cfc3bcf83f..4b0ed5ca23 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1279,12 +1279,12 @@ _mesa_req_pact_done(u3_pend_req* req_u, #endif req_u->hav_d++; + bitset_del(&req_u->was_u, nam_u->fra_d); #ifdef MESA_DEBUG u3l_log("fragment %llu hav_d %llu nex_d %llu ack_d %llu lef_d %llu old_d %llu", nam_u->fra_d, req_u->hav_d, req_u->nex_d, req_u->ack_d, req_u->lef_d, req_u->old_d); #endif if ( req_u->lef_d == nam_u->fra_d ) { - bitset_del(&req_u->was_u, nam_u->fra_d); req_u->lef_d++; } From 95ef0b13972bf0406ea8cd17adc0d850d8c02af8 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Tue, 27 Aug 2024 11:23:30 +0200 Subject: [PATCH 376/430] mesa: add out-of-window fragments to data buffer --- pkg/vere/io/mesa.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 4b0ed5ca23..6a2456b2f3 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1234,12 +1234,12 @@ _mesa_req_pact_done(u3_pend_req* req_u, if ( req_u->los_u->counter != nam_u->fra_d ) { if ( nam_u->fra_d < req_u->los_u->counter ) { u3l_log("fragment number too low: %"PRIu64, nam_u->fra_d); - c3_free(par_u); - return; + // c3_free(par_u); + // return; } else if ( nam_u->fra_d >= req_u->los_u->counter + (sizeof(req_u->mis_u)/sizeof(u3_misord_buf)) ) { u3l_log("fragment number too high: %"PRIu64, nam_u->fra_d); - c3_free(par_u); - return; + // c3_free(par_u); + // return; } else { // insert into misordered queue u3_misord_buf* buf_u = &req_u->mis_u[nam_u->fra_d - req_u->los_u->counter - 1]; From 1103e9bea7400082c5d13a82a822eee3ee7e2306 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Tue, 27 Aug 2024 16:36:12 +0200 Subject: [PATCH 377/430] mesa: fix misorder queue update we also move forward from lef_d to account for the misordered that we have received (note: check that this is right) --- pkg/vere/io/mesa.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 6a2456b2f3..ea19c6d5c5 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1188,9 +1188,15 @@ _mesa_burn_misorder_queue(u3_pend_req* req_u) } } // ratchet forward - num_w++; // account for the packet processed in _mesa_req_pact_done - memcpy(req_u->mis_u, req_u->mis_u + num_w, max_w - num_w); - memset(req_u->mis_u + max_w - num_w, 0, num_w * sizeof(u3_misord_buf)); + u3l_log("updating lef_d %u", req_u->lef_d); + req_u->lef_d += num_w; // XX could we be skipping some lef_d's that we have not received? + u3l_log("updated lef_d %u num_w: %u", req_u->lef_d, num_w); + + memset(req_u->mis_u, 0, num_w * sizeof(u3_misord_buf)); + memcpy(req_u->mis_u, + (c3_y*)req_u->mis_u + (num_w * sizeof(u3_misord_buf)), + (max_w - num_w) * sizeof(u3_misord_buf)); + return res_o; } From f9a18a8d45ba81e3b35a4356bca565ea2439093a Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Tue, 27 Aug 2024 16:36:48 +0200 Subject: [PATCH 378/430] mesa: restore early return for out of window fragments --- pkg/vere/io/mesa.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index ea19c6d5c5..ce2138e7cf 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1240,12 +1240,12 @@ _mesa_req_pact_done(u3_pend_req* req_u, if ( req_u->los_u->counter != nam_u->fra_d ) { if ( nam_u->fra_d < req_u->los_u->counter ) { u3l_log("fragment number too low: %"PRIu64, nam_u->fra_d); - // c3_free(par_u); - // return; + c3_free(par_u); + return; } else if ( nam_u->fra_d >= req_u->los_u->counter + (sizeof(req_u->mis_u)/sizeof(u3_misord_buf)) ) { u3l_log("fragment number too high: %"PRIu64, nam_u->fra_d); - // c3_free(par_u); - // return; + c3_free(par_u); + return; } else { // insert into misordered queue u3_misord_buf* buf_u = &req_u->mis_u[nam_u->fra_d - req_u->los_u->counter - 1]; From 2be8c02a9621b0c7f97368d4fe66efb3eba17855 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Tue, 27 Aug 2024 16:40:05 +0200 Subject: [PATCH 379/430] mesa: bitset_del for misordered and lef_d fragment --- pkg/vere/io/mesa.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index ce2138e7cf..8adf5da729 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1254,6 +1254,7 @@ _mesa_req_pact_done(u3_pend_req* req_u, memcpy(buf_u->fra_y, dat_u->fra_y, dat_u->len_w); buf_u->par_u = par_u; u3l_log("insert into misordered queue fra: %llu", nam_u->fra_d ); + bitset_del(&req_u->was_u, nam_u->fra_d); } } else if ( c3y != lss_verifier_ingest(req_u->los_u, dat_u->fra_y, dat_u->len_w, par_u) ) { @@ -1285,12 +1286,12 @@ _mesa_req_pact_done(u3_pend_req* req_u, #endif req_u->hav_d++; - bitset_del(&req_u->was_u, nam_u->fra_d); #ifdef MESA_DEBUG u3l_log("fragment %llu hav_d %llu nex_d %llu ack_d %llu lef_d %llu old_d %llu", nam_u->fra_d, req_u->hav_d, req_u->nex_d, req_u->ack_d, req_u->lef_d, req_u->old_d); #endif if ( req_u->lef_d == nam_u->fra_d ) { + bitset_del(&req_u->was_u, nam_u->fra_d); req_u->lef_d++; } From c4dca5feb9c4e2e258ecbcf696e0cc0be3b2d051 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Tue, 27 Aug 2024 16:41:00 +0200 Subject: [PATCH 380/430] mesa: fix memory leak --- pkg/vere/io/mesa.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 8adf5da729..5e8c20ca1d 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -2633,6 +2633,8 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) // otherwise, scry lin_u = c3_calloc(sizeof(u3_mesa_line)); lin_u->typ_y = CTAG_WAIT; + _mesa_copy_name(&lin_u->nam_u, &pac_u->pek_u.nam_u); // XX + _mesa_put_jumbo_cache(sam_u, &pac_u->pek_u.nam_u, lin_u); u3_noun sky = _name_to_jumbo_scry(&pac_u->pek_u.nam_u); u3_noun our = u3i_chubs(2, sam_u->car_u.pir_u->who_d); From c60fbb25deb2cf7e4adf95c04e67a47681e00058 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Tue, 27 Aug 2024 16:41:38 +0200 Subject: [PATCH 381/430] mesa WIP fix resend timer --- pkg/vere/io/mesa.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 5e8c20ca1d..701cc485a5 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1155,10 +1155,18 @@ _update_resend_timer(u3_pend_req *req_u) } req_u->old_d = idx_d; req_u->tim_u.data = req_u; - c3_d gap_d = req_u->wat_u[idx_d].sen_d - now_d; - /* u3l_log("timeout %llu", (gag_u->rto_w - gap_d) / 1000); */ - c3_w dur_w = (req_u->gag_u->rto_w - gap_d) / 1000; - uv_timer_start(&req_u->tim_u, _mesa_packet_timeout, dur_w, 0); + c3_d gap_d = req_u->wat_u[idx_d].sen_d == 0 ? + 0 : + now_d - req_u->wat_u[idx_d].sen_d; + // if ( gap_d > req_u->gag_u->rto_w ) { + // u3l_log("OMG %llu", gap_d); + // } + // u3l_log("timeout %llu", (req_u->gag_u->rto_w - gap_d) / 1000); + // c3_w dur_w = (req_u->gag_u->rto_w - gap_d) / 1000; + + c3_d next_expiry = (gap_d + req_u->gag_u->rto_w) / 1000; + // now_d == gap_d ? req_u->gag_u->rto_w : req_u->gag_u->rto_w - gap_d / 1000; + uv_timer_start(&req_u->tim_u, _mesa_packet_timeout, next_expiry, 0); } /* _mesa_packet_timeout(): callback for packet timeout From 05fe7e9e0a2099d8292cf915e2efd7c1d513ab2c Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Wed, 28 Aug 2024 15:09:12 +0200 Subject: [PATCH 382/430] mesa: fix remaining fragments --- pkg/vere/io/mesa.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 701cc485a5..0ae7b7d155 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -802,7 +802,7 @@ static void _mesa_handle_ack(u3_gage* gag_u, u3_pact_stat* pat_u) static inline c3_d _mesa_req_get_remaining(u3_pend_req* req_u) { - return req_u->tof_d - req_u->nex_d; + return req_u->tof_d - req_u->hav_d; } /* From b756796f5c2f67502eeef3373906ae9405f851a0 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Wed, 28 Aug 2024 15:09:47 +0200 Subject: [PATCH 383/430] mesa: init congestion to microseconds --- pkg/vere/io/mesa.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 0ae7b7d155..2289429ce9 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -505,11 +505,11 @@ _mesa_request_key(u3_mesa_name* nam_u) } static void -_init_gage(u3_gage* gag_u) +_init_gage(u3_gage* gag_u) // microseconds { - gag_u->rto_w = 1000000; - gag_u->rtt_w = 1000000; - gag_u->rtv_w = 1000000; + gag_u->rto_w = 1000 * 1000 * 1000; // ~s1 + gag_u->rtt_w = 1000 * 1000 * 1000; // ~s1 + gag_u->rtv_w = 1000 * 1000 * 1000; // ~s1 gag_u->con_w = 0; gag_u->wnd_w = 1; gag_u->sst_w = 10000; From 2895f5d23d9e8c76e8b8c00160345a1b3547ab0f Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Wed, 28 Aug 2024 15:10:16 +0200 Subject: [PATCH 384/430] mesa: update resent packet send time --- pkg/vere/io/mesa.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 2289429ce9..22d65905db 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1109,7 +1109,7 @@ _try_resend(u3_pend_req* req_u, c3_d ack_d) if ( (c3y == bitset_has(&req_u->was_u, i_d)) && (now_d - req_u->wat_u[i_d].sen_d > req_u->gag_u->rto_w) ) { los_o = c3y; - + _mesa_req_pact_resent(req_u, &pac_u->pek_u.nam_u); pac_u->pek_u.nam_u.fra_d = i_d; c3_w len_w = mesa_etch_pact_to_buf(buf_y, PACT_SIZE, pac_u); _mesa_send_modal(req_u->per_u, buf_y, len_w); From f6797424b3b7afc7bad5b44215ee0d07f3f3f08a Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Wed, 28 Aug 2024 15:11:34 +0200 Subject: [PATCH 385/430] mesa: use RTO as next timer resend --- pkg/vere/io/mesa.c | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 22d65905db..7680c6a9a9 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1155,18 +1155,12 @@ _update_resend_timer(u3_pend_req *req_u) } req_u->old_d = idx_d; req_u->tim_u.data = req_u; - c3_d gap_d = req_u->wat_u[idx_d].sen_d == 0 ? - 0 : - now_d - req_u->wat_u[idx_d].sen_d; - // if ( gap_d > req_u->gag_u->rto_w ) { - // u3l_log("OMG %llu", gap_d); - // } - // u3l_log("timeout %llu", (req_u->gag_u->rto_w - gap_d) / 1000); - // c3_w dur_w = (req_u->gag_u->rto_w - gap_d) / 1000; - - c3_d next_expiry = (gap_d + req_u->gag_u->rto_w) / 1000; - // now_d == gap_d ? req_u->gag_u->rto_w : req_u->gag_u->rto_w - gap_d / 1000; - uv_timer_start(&req_u->tim_u, _mesa_packet_timeout, next_expiry, 0); + // c3_d gap_d = req_u->wat_u[idx_d].sen_d == 0 ? + // 0 : + // now_d - req_u->wat_u[idx_d].sen_d; + c3_d next_expiry = req_u->gag_u->rto_w; + u3l_log("next_expiry %llu", next_expiry / 1000); + uv_timer_start(&req_u->tim_u, _mesa_packet_timeout, next_expiry / 1000, 0); } /* _mesa_packet_timeout(): callback for packet timeout From 9c4a01db75688fc33f1999a6d1b78a891a1f676e Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Wed, 28 Aug 2024 15:12:31 +0200 Subject: [PATCH 386/430] mesa: burn misorder queue on out-ot-window fragments --- pkg/vere/io/mesa.c | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 7680c6a9a9..f25efaadae 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1246,6 +1246,7 @@ _mesa_req_pact_done(u3_pend_req* req_u, return; } else if ( nam_u->fra_d >= req_u->los_u->counter + (sizeof(req_u->mis_u)/sizeof(u3_misord_buf)) ) { u3l_log("fragment number too high: %"PRIu64, nam_u->fra_d); + _mesa_burn_misorder_queue(req_u); c3_free(par_u); return; } else { From 5c932e9de47094140fc133bb1108e7b71948da01 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Wed, 28 Aug 2024 15:13:35 +0200 Subject: [PATCH 387/430] mesa: don't try to resend after ack --- pkg/vere/io/mesa.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index f25efaadae..b14735fe2e 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1315,7 +1315,8 @@ _mesa_req_pact_done(u3_pend_req* req_u, c3_w siz_w = (1 << (nam_u->boq_y - 3)); memcpy(req_u->dat_y + (siz_w * nam_u->fra_d), dat_u->fra_y, dat_u->len_w); - _try_resend(req_u, req_u->nex_d); // _try_resend(req_u, nam_u->fra_d); + // _try_resend(req_u, req_u->nex_d); + // _try_resend(req_u, nam_u->fra_d); _update_resend_timer(req_u); } From a7821f28fb7de997533132fc6e0c001bfbab38cb Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Wed, 28 Aug 2024 15:14:14 +0200 Subject: [PATCH 388/430] mesa: use correct length when inserting jumbo frame --- pkg/vere/io/mesa.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index b14735fe2e..f74554dea5 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -2548,7 +2548,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) pac_u->pag_u.nam_u.boq_y = boq_y; pac_u->pag_u.dat_u.tob_d = req_u->tob_d; pac_u->pag_u.nam_u.fra_d = (req_u->hav_d >> boq_y); - pac_u->pag_u.dat_u.len_w += (1024 * (lev_d - 1)); + pac_u->pag_u.dat_u.len_w = req_u->tob_d; pac_u->pag_u.dat_u.fra_y = req_u->dat_y; pac_u->pag_u.dat_u.aut_u = req_u->aut_u; From f19e314638eb12a94023bc184ca2fcdc1d038a63 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Wed, 28 Aug 2024 15:31:57 +0200 Subject: [PATCH 389/430] mesa: remove misordered queue handling --- pkg/vere/io/mesa.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index f74554dea5..182be7b8d8 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1249,16 +1249,17 @@ _mesa_req_pact_done(u3_pend_req* req_u, _mesa_burn_misorder_queue(req_u); c3_free(par_u); return; - } else { - // insert into misordered queue - u3_misord_buf* buf_u = &req_u->mis_u[nam_u->fra_d - req_u->los_u->counter - 1]; - buf_u->fra_y = c3_calloc(dat_u->len_w); - buf_u->len_w = dat_u->len_w; - memcpy(buf_u->fra_y, dat_u->fra_y, dat_u->len_w); - buf_u->par_u = par_u; - u3l_log("insert into misordered queue fra: %llu", nam_u->fra_d ); - bitset_del(&req_u->was_u, nam_u->fra_d); - } + } //else { // XXXX + // // insert into misordered queue + // // (4 - 3) - 1 + // u3_misord_buf* buf_u = &req_u->mis_u[nam_u->fra_d - req_u->los_u->counter - 1]; + // buf_u->fra_y = c3_calloc(dat_u->len_w); + // buf_u->len_w = dat_u->len_w; + // memcpy(buf_u->fra_y, dat_u->fra_y, dat_u->len_w); + // buf_u->par_u = par_u; + // u3l_log("insert into misordered queue fra: %llu", nam_u->fra_d ); + // bitset_del(&req_u->was_u, nam_u->fra_d); + // } } else if ( c3y != lss_verifier_ingest(req_u->los_u, dat_u->fra_y, dat_u->len_w, par_u) ) { u3l_log("auth fail frag %"PRIu64, nam_u->fra_d); From 6689197b593d3751704de2b2afeef2c42bd8fa28 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Thu, 29 Aug 2024 19:40:07 +0200 Subject: [PATCH 390/430] mesa: fix misordered queue handling --- pkg/vere/io/mesa.c | 80 ++++++++++++++++++++-------------------------- 1 file changed, 35 insertions(+), 45 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 182be7b8d8..56caa1f6a8 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1046,7 +1046,7 @@ _mesa_send_modal(u3_peer* per_u, c3_y* buf_y, c3_w len_w) if ( ( c3y == _mesa_is_direct_mode(per_u) ) || // if we are the sponsor of the ship, don't send to ourselves (our_o == c3y) ) { - u3l_log("mesa: direct"); + // u3l_log("mesa: direct"); _mesa_send_buf(sam_u, per_u->dan_u, sen_y, len_w); per_u->dir_u.sen_d = now_d; } @@ -1117,7 +1117,7 @@ _try_resend(u3_pend_req* req_u, c3_d ack_d) } if ( c3y == los_o ) { - req_u->gag_u->sst_w = (req_u->gag_u->wnd_w / 2) + 1; + req_u->gag_u->sst_w = c3_max(1, req_u->gag_u->wnd_w / 2); req_u->gag_u->wnd_w = req_u->gag_u->sst_w; req_u->gag_u->rto_w = _clamp_rto(req_u->gag_u->rto_w * 2); } @@ -1159,7 +1159,7 @@ _update_resend_timer(u3_pend_req *req_u) // 0 : // now_d - req_u->wat_u[idx_d].sen_d; c3_d next_expiry = req_u->gag_u->rto_w; - u3l_log("next_expiry %llu", next_expiry / 1000); + // u3l_log("next_expiry %llu", next_expiry / 1000); uv_timer_start(&req_u->tim_u, _mesa_packet_timeout, next_expiry / 1000, 0); } @@ -1189,11 +1189,11 @@ _mesa_burn_misorder_queue(u3_pend_req* req_u) break; } } - // ratchet forward - u3l_log("updating lef_d %u", req_u->lef_d); - req_u->lef_d += num_w; // XX could we be skipping some lef_d's that we have not received? - u3l_log("updated lef_d %u num_w: %u", req_u->lef_d, num_w); + // ratchet forward + num_w++; // account for the in-ordered packet processed in _mesa_req_pact_done + req_u->lef_d += num_w; + req_u->hav_d += num_w; memset(req_u->mis_u, 0, num_w * sizeof(u3_misord_buf)); memcpy(req_u->mis_u, (c3_y*)req_u->mis_u + (num_w * sizeof(u3_misord_buf)), @@ -1227,7 +1227,7 @@ _mesa_req_pact_done(u3_pend_req* req_u, // received duplicate if ( c3n == bitset_has(&req_u->was_u, nam_u->fra_d) ) { - MESA_LOG(sam_u, DUPE); + // MESA_LOG(sam_u, DUPE); return; } @@ -1241,25 +1241,22 @@ _mesa_req_pact_done(u3_pend_req* req_u, if ( req_u->los_u->counter != nam_u->fra_d ) { if ( nam_u->fra_d < req_u->los_u->counter ) { - u3l_log("fragment number too low: %"PRIu64, nam_u->fra_d); + // u3l_log("fragment number too low: %"PRIu64, nam_u->fra_d); c3_free(par_u); return; } else if ( nam_u->fra_d >= req_u->los_u->counter + (sizeof(req_u->mis_u)/sizeof(u3_misord_buf)) ) { - u3l_log("fragment number too high: %"PRIu64, nam_u->fra_d); - _mesa_burn_misorder_queue(req_u); + // u3l_log("fragment number too high: %"PRIu64, nam_u->fra_d); c3_free(par_u); return; - } //else { // XXXX - // // insert into misordered queue - // // (4 - 3) - 1 - // u3_misord_buf* buf_u = &req_u->mis_u[nam_u->fra_d - req_u->los_u->counter - 1]; - // buf_u->fra_y = c3_calloc(dat_u->len_w); - // buf_u->len_w = dat_u->len_w; - // memcpy(buf_u->fra_y, dat_u->fra_y, dat_u->len_w); - // buf_u->par_u = par_u; - // u3l_log("insert into misordered queue fra: %llu", nam_u->fra_d ); - // bitset_del(&req_u->was_u, nam_u->fra_d); - // } + } else { + // insert into misordered queue + u3_misord_buf* buf_u = &req_u->mis_u[nam_u->fra_d - req_u->los_u->counter - 1]; + buf_u->fra_y = c3_calloc(dat_u->len_w); + buf_u->len_w = dat_u->len_w; + memcpy(buf_u->fra_y, dat_u->fra_y, dat_u->len_w); + buf_u->par_u = par_u; + u3l_log("insert into misordered queue fra: [%llu] = %llu", nam_u->fra_d - req_u->los_u->counter - 1, nam_u->fra_d ); + } } else if ( c3y != lss_verifier_ingest(req_u->los_u, dat_u->fra_y, dat_u->len_w, par_u) ) { u3l_log("auth fail frag %"PRIu64, nam_u->fra_d); @@ -1275,7 +1272,7 @@ _mesa_req_pact_done(u3_pend_req* req_u, return; } else { - u3l_log("about to other free"); + // u3l_log("about to other free"); c3_free(par_u); } @@ -1283,19 +1280,13 @@ _mesa_req_pact_done(u3_pend_req* req_u, req_u->ack_d = nam_u->fra_d; } - #ifdef MESA_DEBUG - if ( nam_u->fra_d != 0 && req_u->wat_u[nam_u->fra_d].tie_y != 1 ) { - u3l_log("received retry %"PRIu64, nam_u->fra_d); - } - #endif - - req_u->hav_d++; + bitset_del(&req_u->was_u, nam_u->fra_d); #ifdef MESA_DEBUG - u3l_log("fragment %llu hav_d %llu nex_d %llu ack_d %llu lef_d %llu old_d %llu", nam_u->fra_d, req_u->hav_d, req_u->nex_d, req_u->ack_d, req_u->lef_d, req_u->old_d); + u3l_log("fragment %llu counter %llu hav_d %llu nex_d %llu ack_d %llu lef_d %llu old_d %llu", nam_u->fra_d, req_u->los_u->counter, req_u->hav_d, req_u->nex_d, req_u->ack_d, req_u->lef_d, req_u->old_d); #endif if ( req_u->lef_d == nam_u->fra_d ) { - bitset_del(&req_u->was_u, nam_u->fra_d); + req_u->hav_d++; req_u->lef_d++; } @@ -1878,7 +1869,7 @@ _mesa_get_jumbo_cache(u3_mesa* sam_u, u3_mesa_name* nam_u) u3_noun pax = _name_to_jumbo_scry(nam_u); u3_weak res = u3h_get(sam_u->pac_p, pax); #ifdef MESA_DEBUG - u3m_p((u3_none == res)? "mesa: cache miss" : "mesa: cache hit ", pax); + // u3m_p((u3_none == res)? "mesa: cache miss" : "mesa: cache hit ", pax); #endif u3z(pax); return ( u3_none == res ) ? NULL : u3a_into(res); @@ -1936,9 +1927,9 @@ _mesa_send_leaf(u3_mesa* sam_u, u3_weak pin = _mesa_get_pit(sam_u, nam_u); if ( u3_none != pin) { #ifdef MESA_DEBUG - u3l_log(" sending leaf packet, fra_d: %"PRIu64, nam_u->fra_d); + // u3l_log(" sending leaf packet, fra_d: %"PRIu64, nam_u->fra_d); #endif - log_pact(pac_u); + // log_pact(pac_u); _mesa_send_pact(sam_u, u3k(u3t(pin)), NULL, pac_u); _mesa_del_pit(sam_u, nam_u); u3z(pin); @@ -1949,7 +1940,7 @@ static void _mesa_send_jumbo_pieces(u3_mesa* sam_u, u3_mesa_line* lin_u, c3_d* fra_u) { #ifdef MESA_DEBUG - u3l_log("mesa: send_jumbo_pieces()"); + // u3l_log("mesa: send_jumbo_pieces()"); #endif u3_mesa_pact pac_u = {0}; @@ -2054,8 +2045,8 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun res) } #ifdef MESA_DEBUG - u3l_log("mesa: scry_jumbo_cb()"); - log_pact(pac_u); + // u3l_log("mesa: scry_jumbo_cb()"); + // log_pact(pac_u); #endif u3_mesa_line* lin_u; @@ -2064,7 +2055,6 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun res) c3_y* jumbo_y = c3_calloc(jumbo_w); u3r_bytes(0, jumbo_w, jumbo_y, pac); - u3l_log("sifting jumbo packet len=%u", jumbo_w); u3_mesa_pact jum_u; c3_c* err_c = mesa_sift_pact_from_buf(&jum_u, jumbo_y, jumbo_w); if ( err_c ) { @@ -2278,7 +2268,6 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) lss_root(root, pof_u, pof_w); req_u->los_u = c3_calloc(sizeof(lss_verifier)); lss_verifier_init(req_u->los_u, 0, req_u->tof_d, pof_u); - u3l_log("about to c3_free(pof_u)"); c3_free(pof_u); req_u = _mesa_put_request(sam_u, nam_u, req_u); @@ -2390,8 +2379,8 @@ static void _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) { #ifdef MESA_DEBUG - u3l_log("mesa: hear_page()"); - log_pact(&pic_u->pac_u); + // u3l_log("mesa: hear_page()"); + // log_pact(&pic_u->pac_u); u3_assert( PACT_PAGE == pic_u->pac_u.hed_u.typ_y ); #endif @@ -2435,7 +2424,8 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) if ( u3_none == pin ) { #ifdef MESA_DEBUG - u3l_log(" no PIT entry"); + // u3l_log(" no PIT entry"); + // log_name(nam_u); #endif return; } @@ -2537,7 +2527,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) if ( c3y == done_with_jumbo_frame ) { u3_noun cad; - u3l_log(" received last packet, tot_d: %llu", req_u->tof_d); + u3l_log(" received last packet, tof_d: %llu tob_d: %llu", req_u->tof_d, req_u->tob_d); c3_d now_d = _get_now_micros(); u3l_log("%llu kilobytes took %f ms", req_u->tof_d, (now_d - sam_u->tim_d)/1000.0); @@ -2606,7 +2596,7 @@ static void _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) { #ifdef MESA_DEBUG - u3l_log("mesa: hear_peek()"); + // u3l_log("mesa: hear_peek()"); u3_assert( PACT_PEEK == pic_u->pac_u.hed_u.typ_y ); #endif From dd5fae453aeb0b4443795e2569819bfd38f0b9cc Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Mon, 9 Sep 2024 14:32:42 +0200 Subject: [PATCH 391/430] mesa: add failing hamt test --- pkg/vere/BUILD.bazel | 12 ++++ pkg/vere/hamt_test.c | 135 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 147 insertions(+) create mode 100644 pkg/vere/hamt_test.c diff --git a/pkg/vere/BUILD.bazel b/pkg/vere/BUILD.bazel index e23dda43a2..5ac8f2fca3 100644 --- a/pkg/vere/BUILD.bazel +++ b/pkg/vere/BUILD.bazel @@ -305,6 +305,18 @@ cc_test( deps = [":vere"], ) +cc_test( + name = "hamt_test", + timeout = "short", + srcs = ["hamt_test.c"], + features = select({ + "@platforms//os:linux": ["fully_static_link"], + "//conditions:default": [], + }), + visibility = ["//visibility:private"], + deps = [":vere"], +) + cc_test( name = "benchmarks", timeout = "short", diff --git a/pkg/vere/hamt_test.c b/pkg/vere/hamt_test.c new file mode 100644 index 0000000000..09b3eaafa2 --- /dev/null +++ b/pkg/vere/hamt_test.c @@ -0,0 +1,135 @@ +/// @file + +#include "noun.h" +#include "vere.h" +#include "ivory.h" +#include "ur.h" +// #include "pact.h" + + +// defined in noun/hashtable.c +c3_w _ch_skip_slot(c3_w mug_w, c3_w lef_w); + +/* _setup(): prepare for tests. +*/ +static void +_setup(void) +{ + + c3_d len_d = u3_Ivory_pill_len; + c3_y* byt_y = u3_Ivory_pill; + u3_cue_xeno* sil_u; + u3_weak pil; + + u3C.wag_w |= u3o_hashless; + u3m_boot_lite(1 << 26); + sil_u = u3s_cue_xeno_init_with(ur_fib27, ur_fib28); + if ( u3_none == (pil = u3s_cue_xeno_with(sil_u, len_d, byt_y)) ) { + printf("*** fail _setup 1\n"); + exit(1); + } + u3s_cue_xeno_done(sil_u); + if ( c3n == u3v_boot_lite(pil) ) { + printf("*** fail _setup 2\n"); + exit(1); + } + + + // u3m_init(1 << 26); + // u3m_pave(c3y); + +} + + +/* main(): run all test cases. +*/ +int +main(int argc, char* argv[]) +{ + _setup(); + + // if ( !_test_safe() ) { + // fprintf(stderr, "test unix: failed\r\n"); + // exit(1); + // } + + u3p(u3h_root) pit_p = u3h_new_cache(1000000); + + u3_noun init = u3nc(u3i_string("mess"), + u3nc(48, + u3nc(c3__pact, + u3nc(13105, + u3nc(c3__etch, + u3nc(c3__init, + u3nc(u3i_string("chum"), + u3nc(49, + u3nc(u3i_string("~nec"), + u3nc(49, + u3nc(u3i_string("0voctt.mbs2p.q9qaf.b17em.tbsit.49vat.7ggc2.8avm6.oe870.fqhkt.dltit.u54hf.lse3l.au3pt"), 0) + ) + ) + )))))))); + u3h_put(pit_p, init, u3nc(c3y, u3_nul)); + + for ( c3_w fra_w = 0; fra_w < 100000; fra_w++ ) { + u3_noun data = u3nc(u3i_string("mess"), + u3nc(48, + u3nc(c3__pact, + u3nc(13105, + u3nc(c3__etch, + u3nc(c3__data, + u3nc(u3dc("scot", c3__ud, fra_w), + u3nc(u3i_string("chum"), + u3nc(49, + u3nc(u3i_string("~nec"), + u3nc(49, + u3nc(u3i_string("0voctt.mbs2p.q9qaf.b17em.tbsit.49vat.7ggc2.8avm6.oe870.fqhkt.dltit.u54hf.lse3l.au3pt"), 0) + ) + ) + ) + ) + ) + ) + ) + ) + ) + ) + ); + + u3h_put(pit_p, data, u3nc(c3y, u3_nul)); + } + + for ( c3_w fra_w = 0; fra_w < 100000; fra_w++ ) { + u3_noun data = u3nc(u3i_string("mess"), + u3nc(48, + u3nc(c3__pact, + u3nc(13105, + u3nc(c3__etch, + u3nc(c3__data, + u3nc(u3dc("scot", c3__ud, fra_w), + u3nc(u3i_string("chum"), + u3nc(49, + u3nc(u3i_string("~nec"), + u3nc(49, + u3nc(u3i_string("0voctt.mbs2p.q9qaf.b17em.tbsit.49vat.7ggc2.8avm6.oe870.fqhkt.dltit.u54hf.lse3l.au3pt"), 0) + ) + ) + ) + ) + ) + ) + ) + ) + ) + ) + ); + u3_weak res = u3h_get(pit_p, data); + if (res == u3_none) { + fprintf(stderr, "key gone from hamt %u: not ok\r\n", fra_w); + exit(1); + } + } + + fprintf(stderr, "test unix: ok\r\n"); + return 0; +} From 765d1703545eb22c5c586f7e3733610c30d94286 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Tue, 10 Sep 2024 13:41:59 +0200 Subject: [PATCH 392/430] mesa: use set for pit lanes --- pkg/vere/io/mesa.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 56caa1f6a8..1c108f3330 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1347,7 +1347,8 @@ _mesa_send_bufs(u3_mesa* sam_u, c3_w len_w, u3_noun las) { - u3_noun lan, t = las; + u3_noun lan, t = u3qdi_tap(las); + while ( t != u3_nul ) { u3x_cell(t, &lan, &t); u3_lane lan_u = _realise_lane(u3k(lan)); @@ -1363,7 +1364,7 @@ _mesa_send_bufs(u3_mesa* sam_u, } } } - u3z(las); + u3z(las); u3z(lan); } static void @@ -1488,13 +1489,13 @@ _mesa_queue_czar(u3_mesa* sam_u, u3_noun las, u3_noun pac) u3_czar_info* imp_u = &sam_u->imp_u[lan]; if ( 0 != imp_u->pip_w ) { - res = u3nc(u3nt(c3__if, u3i_word(imp_u->pip_w), _ames_czar_port(lan)), res); + res = u3qdi_put(res, u3nt(c3__if, u3i_word(imp_u->pip_w), _ames_czar_port(lan))); } if ( c3y == u3_Host.ops_u.net ) { _mesa_resolve_czar(sam_u, lan, u3k(pac)); } } else { - res = u3nc(u3k(lan), res); + res = u3qdi_put(res, u3k(lan)); } } u3z(las); @@ -1563,10 +1564,11 @@ _mesa_add_lane_to_pit(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_lane lan_u) u3_noun lan = u3_mesa_encode_lane(lan_u); u3_weak pin = _mesa_get_pit(sam_u, nam_u); if ( u3_none == pin ) { - pin = u3nt(c3n, u3k(lan), u3_nul); + pin = u3nc(c3n, u3nt(u3k(lan), u3_nul, u3_nul)); } else { - pin = u3nt(u3k(u3h(pin)), u3k(lan), u3k(u3t(pin))); + pin = //u3qdi_put(pin, u3nt(u3k(u3h(pin)), u3k(lan), u3k(u3t(pin)))); + u3nc(u3h(pin), u3qdi_put(u3t(pin), u3k(lan))); } _mesa_put_pit(sam_u, nam_u, u3k(pin)); u3z(lan); u3z(pin); From 0d8eb8b50bc5dfa6f1361afed9b14b0a38f0dd51 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Wed, 11 Sep 2024 11:38:11 +0200 Subject: [PATCH 393/430] mesa: don't send auth response for single-fragment message --- pkg/vere/io/mesa.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 1c108f3330..a72ba1014c 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1918,10 +1918,10 @@ _mesa_send_leaf(u3_mesa* sam_u, lss_pair* pair = ((lss_pair*)lin_u->haz_y) + i_d; - if ( 0 == memcmp(pair, &(lss_pair){0}, sizeof(lss_pair)) ) { - dat_u->aut_u.typ_e = AUTH_NONE; - } else if ( 0 == nam_u->fra_d ) { + if ( 0 == nam_u->fra_d ) { // XX _mesa_copy_auth_data(&dat_u->aut_u, &lin_u->aut_u); + } else if ( 0 == memcmp(pair, &(lss_pair){0}, sizeof(lss_pair)) ) { + dat_u->aut_u.typ_e = AUTH_NONE; }else { dat_u->aut_u.typ_e = AUTH_PAIR; memcpy(dat_u->aut_u.has_y, pair, sizeof(lss_pair)); @@ -1978,9 +1978,10 @@ _mesa_send_jumbo_pieces(u3_mesa* sam_u, u3_mesa_line* lin_u, c3_d* fra_u) nam_u->nit_o = c3y; u3_weak pin = _mesa_get_pit(sam_u, nam_u); if ( u3_none != pin ) { + // XX lss_proof_size always returns >= 1 // if the initial Merkle proof is nonzero, this is a multifragment // message; send auth response to the %init request - if ( pro_w > 0 ) { + if ( pro_w > 1 ) { dat_u->len_w = pro_w * sizeof(lss_hash); c3_y* pro_y = c3_malloc(dat_u->len_w); memcpy(pro_y, lin_u->tip_y, dat_u->len_w); From 96e28a919910d3594f6ba86706d35789fb94c1f0 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Wed, 11 Sep 2024 10:06:58 -0400 Subject: [PATCH 394/430] u3: corrects hashtable trimming in u3h_put_get() --- pkg/noun/hashtable.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/pkg/noun/hashtable.c b/pkg/noun/hashtable.c index 264d8218b1..be6dc92342 100644 --- a/pkg/noun/hashtable.c +++ b/pkg/noun/hashtable.c @@ -268,13 +268,19 @@ u3h_put_get(u3p(u3h_root) har_p, u3_noun key, u3_noun val) _ch_slot_put(sot_w, kev, 25, rem_w, &(har_u->use_w)); } - if ( har_u->max_w > 0 ) { - return _ch_trim_root(har_u); - } - else { - return u3_none; - } + { + u3_weak ret = u3_none; + if ( har_u->max_w && (har_u->use_w > har_u->max_w) ) { + do { + ret = _ch_trim_root(har_u); + } + while ( u3_none == ret ); + har_u->use_w -= 1; + } + + return ret; + } } /* u3h_put(): insert in hashtable. From 204a8c14cf0c964dfb60fb64709b7bb829f58eea Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Wed, 11 Sep 2024 10:10:16 -0400 Subject: [PATCH 395/430] test: fixes memory managment in new hamt_test --- pkg/vere/hamt_test.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/pkg/vere/hamt_test.c b/pkg/vere/hamt_test.c index 09b3eaafa2..89c8c6c4ac 100644 --- a/pkg/vere/hamt_test.c +++ b/pkg/vere/hamt_test.c @@ -15,7 +15,6 @@ c3_w _ch_skip_slot(c3_w mug_w, c3_w lef_w); static void _setup(void) { - c3_d len_d = u3_Ivory_pill_len; c3_y* byt_y = u3_Ivory_pill; u3_cue_xeno* sil_u; @@ -33,11 +32,6 @@ _setup(void) printf("*** fail _setup 2\n"); exit(1); } - - - // u3m_init(1 << 26); - // u3m_pave(c3y); - } @@ -70,6 +64,7 @@ main(int argc, char* argv[]) ) )))))))); u3h_put(pit_p, init, u3nc(c3y, u3_nul)); + u3z(init); for ( c3_w fra_w = 0; fra_w < 100000; fra_w++ ) { u3_noun data = u3nc(u3i_string("mess"), @@ -97,6 +92,7 @@ main(int argc, char* argv[]) ); u3h_put(pit_p, data, u3nc(c3y, u3_nul)); + u3z(data); } for ( c3_w fra_w = 0; fra_w < 100000; fra_w++ ) { @@ -123,13 +119,18 @@ main(int argc, char* argv[]) ) ) ); - u3_weak res = u3h_get(pit_p, data); + u3_weak res = u3h_git(pit_p, data); if (res == u3_none) { fprintf(stderr, "key gone from hamt %u: not ok\r\n", fra_w); exit(1); } + u3z(data); } + u3h_free(pit_p); + + u3m_grab(u3_none); + fprintf(stderr, "test unix: ok\r\n"); return 0; } From c74c45eb3ec5b456a73becb45845a91d2252b443 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Wed, 11 Sep 2024 10:10:39 -0400 Subject: [PATCH 396/430] test: cleans up existing hashtable_tests --- pkg/noun/hashtable_tests.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pkg/noun/hashtable_tests.c b/pkg/noun/hashtable_tests.c index dbd039816b..9a5a3f8ada 100644 --- a/pkg/noun/hashtable_tests.c +++ b/pkg/noun/hashtable_tests.c @@ -11,7 +11,7 @@ c3_w _ch_skip_slot(c3_w mug_w, c3_w lef_w); static void _setup(void) { - u3m_init(1 << 26); + u3m_init(1 << 27); u3m_pave(c3y); } @@ -30,7 +30,7 @@ _test_put_del() u3h_put(har_p, key, val); u3z(key); } - fprintf(stderr, "inserted"); + // fprintf(stderr, "inserted\r\n"); for ( i_w = 0; i_w < TEST_SIZE; i_w++ ) { u3_noun key = u3i_word(i_w); @@ -42,15 +42,15 @@ _test_put_del() u3z(key); u3z(val); } - fprintf(stderr, "presence"); - c3_w del_w[4] = {30, 82, 4921, 535}; + // fprintf(stderr, "presence\r\n"); + c3_w del_w[4] = {30, 82, 4921, 535}; for ( i_w = 0; i_w < 4; i_w++ ) { u3_noun key = u3i_word(del_w[i_w]); u3h_del(har_p, key); u3z(key); } - fprintf(stderr, "deleted"); + // fprintf(stderr, "deleted\r\n"); for ( i_w = 0; i_w < 4; i_w++ ) { u3_noun key = u3i_word(del_w[i_w]); @@ -61,9 +61,9 @@ _test_put_del() break; } } - fprintf(stderr, "presence two"); + // fprintf(stderr, "presence two\r\n"); u3h_free(har_p); - fprintf(stderr, "freed"); + // fprintf(stderr, "freed\r\n"); return ret_i; } From 234fb4b59ac1e43089bdd60d30c6f8252040d67c Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Tue, 17 Sep 2024 10:44:27 +0200 Subject: [PATCH 397/430] mesa: use sentinel value instead of deleting note: this fixes a known bug in the HAMT and should be removed --- pkg/noun/hashtable.c | 2 +- pkg/vere/io/mesa.c | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/pkg/noun/hashtable.c b/pkg/noun/hashtable.c index be6dc92342..0604969726 100644 --- a/pkg/noun/hashtable.c +++ b/pkg/noun/hashtable.c @@ -423,7 +423,7 @@ _ch_some_del(u3h_slot* sot_w, u3_noun key, c3_w lef_w, c3_w rem_w) /* u3h_del(); delete from hashtable. */ void -u3h_del(u3p(u3h_root) har_p, u3_noun key) +u3h_del(u3p(u3h_root) har_p, u3_noun key) { u3h_root* har_u = u3to(u3h_root, har_p); c3_w mug_w = u3r_mug(key); diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index a72ba1014c..4ad36dcd42 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1530,7 +1530,12 @@ _mesa_get_pit(u3_mesa* sam_u, u3_mesa_name* nam_u) u3_noun pax = _name_to_scry(nam_u); u3_weak res = u3h_get(sam_u->pit_p, pax); u3z(pax); - return res; + if (c3__sent == res) { + return u3_none; + } + else { + return res; + } } static void @@ -1549,7 +1554,8 @@ _mesa_del_pit(u3_mesa* sam_u, u3_mesa_name* nam_u) u3_noun pax = _name_to_scry(nam_u); // u3m_p("_mesa_del_pit pax", pax); if ( pin != u3_none ) { - u3h_del(sam_u->pit_p, pax); + // u3h_del(sam_u->pit_p, pax); // XX restore me + u3h_put(sam_u->pit_p, pax, c3__sent); } else { u3l_log("deleting non existent key in PIT"); } From daa2e13ac3559530e9571024491d7505f66e2787 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Tue, 17 Sep 2024 10:44:46 +0200 Subject: [PATCH 398/430] build: add malloc_error_break breakpoint --- lldbinit.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lldbinit.txt b/lldbinit.txt index da15d87afc..d0f8a83b8e 100644 --- a/lldbinit.txt +++ b/lldbinit.txt @@ -1,3 +1,4 @@ pro hand -p true -s false -n false SIGBUS SIGSEGV p (void)darwin_register_mach_exception_handler() -b u3m_bail \ No newline at end of file +b u3m_bail +b malloc_error_break From e1cf78599bc8c7e1b981c490f1edb71122310f0f Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Wed, 18 Sep 2024 11:25:34 +0200 Subject: [PATCH 399/430] mesa: fix double free --- pkg/vere/io/mesa.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 4ad36dcd42..0b5bd3cc31 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1070,7 +1070,9 @@ _mesa_send_modal(u3_peer* per_u, c3_y* buf_y, c3_w len_w) if ( (c3n == _mesa_is_lane_zero(&per_u->dan_u)) && (per_u->dir_u.sen_d + DIRECT_ROUTE_RETRY_MICROS > now_d)) { - _mesa_send_buf(sam_u, per_u->dan_u, sen_y, len_w); + c3_y* san_y = c3_calloc(len_w); + memcpy(san_y, buf_y, len_w); + _mesa_send_buf(sam_u, per_u->dan_u, san_y, len_w); per_u->dir_u.sen_d = now_d; } } From 95535f619a318e36388e61fb8ec2be688b44c6ab Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Wed, 18 Sep 2024 11:26:59 +0200 Subject: [PATCH 400/430] mesa: only ratchet forward if lss ingests the fragment --- pkg/vere/io/mesa.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 0b5bd3cc31..4edb436f16 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1188,19 +1188,20 @@ _mesa_burn_misorder_queue(u3_pend_req* req_u) } if ( c3y != lss_verifier_ingest(req_u->los_u, buf_u->fra_y, buf_u->len_w, buf_u->par_u) ) { res_o = c3n; + memset(req_u->mis_u, 0, (num_w + 1) * sizeof(u3_misord_buf)); break; } } - - // ratchet forward - num_w++; // account for the in-ordered packet processed in _mesa_req_pact_done - req_u->lef_d += num_w; - req_u->hav_d += num_w; - memset(req_u->mis_u, 0, num_w * sizeof(u3_misord_buf)); - memcpy(req_u->mis_u, - (c3_y*)req_u->mis_u + (num_w * sizeof(u3_misord_buf)), - (max_w - num_w) * sizeof(u3_misord_buf)); - + if (res_o == c3y) { + // ratchet forward + num_w++; // account for the in-ordered packet processed in _mesa_req_pact_done + req_u->lef_d += num_w; + req_u->hav_d += num_w; + memset(req_u->mis_u, 0, num_w * sizeof(u3_misord_buf)); + memcpy(req_u->mis_u, + (c3_y*)req_u->mis_u + (num_w * sizeof(u3_misord_buf)), + (max_w - num_w) * sizeof(u3_misord_buf)); + } return res_o; } From bf272b42c769a5fcfded3d4167033b96e4d06409 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Thu, 19 Sep 2024 08:51:00 +0200 Subject: [PATCH 401/430] mesa: copy misorderd fragments when burning the queue MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The previous behavior was saving everything (both in-order and out-of-order fragments) in req_u->dat_y and in the case of out-of-order fragments, we were skipping the lss verifification check. They were of course in the misordered queue as well, and burning them was just doing nothing—we were never transferring them to the main queue, they were already there— but, the misorderd handling was not increasing the pending fragment, so it's likely that we were hearing those fragments again, this time in order, so when trying to burn the queue we were failing to verify them since they were duplicates. --- pkg/vere/io/mesa.c | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 4edb436f16..d4e1e59dca 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1176,7 +1176,7 @@ _mesa_packet_timeout(uv_timer_t* tim_u) { } static c3_o -_mesa_burn_misorder_queue(u3_pend_req* req_u) +_mesa_burn_misorder_queue(u3_pend_req* req_u, c3_y boq_y, c3_w lef_d) { c3_w num_w; c3_w max_w = sizeof(req_u->mis_u) / sizeof(u3_misord_buf); @@ -1188,20 +1188,21 @@ _mesa_burn_misorder_queue(u3_pend_req* req_u) } if ( c3y != lss_verifier_ingest(req_u->los_u, buf_u->fra_y, buf_u->len_w, buf_u->par_u) ) { res_o = c3n; - memset(req_u->mis_u, 0, (num_w + 1) * sizeof(u3_misord_buf)); + u3l_log("fail to burn %u", num_w); break; } - } - if (res_o == c3y) { - // ratchet forward - num_w++; // account for the in-ordered packet processed in _mesa_req_pact_done - req_u->lef_d += num_w; - req_u->hav_d += num_w; - memset(req_u->mis_u, 0, num_w * sizeof(u3_misord_buf)); - memcpy(req_u->mis_u, - (c3_y*)req_u->mis_u + (num_w * sizeof(u3_misord_buf)), - (max_w - num_w) * sizeof(u3_misord_buf)); - } + c3_w siz_w = (1 << (boq_y - 3)); // XX + // u3l_log("size %u counter %u num %u fra %u inx %u lef_d %u", siz_w, req_u->los_u->counter , num_w, fra_d, (req_u->los_u->counter + num_w + 1), lef_d); + memcpy(req_u->dat_y + (siz_w * (lef_d + num_w + 1)), buf_u->fra_y, buf_u->len_w); + } + // ratchet forward + num_w++; // account for the in-ordered packet processed in _mesa_req_pact_done + req_u->lef_d += num_w; + req_u->hav_d += num_w; + memset(req_u->mis_u, 0, num_w * sizeof(u3_misord_buf)); + memcpy(req_u->mis_u, + (c3_y*)req_u->mis_u + (num_w * sizeof(u3_misord_buf)), + (max_w - num_w) * sizeof(u3_misord_buf)); return res_o; } @@ -1258,7 +1259,9 @@ _mesa_req_pact_done(u3_pend_req* req_u, buf_u->len_w = dat_u->len_w; memcpy(buf_u->fra_y, dat_u->fra_y, dat_u->len_w); buf_u->par_u = par_u; - u3l_log("insert into misordered queue fra: [%llu] = %llu", nam_u->fra_d - req_u->los_u->counter - 1, nam_u->fra_d ); + u3l_log("insert into misordered queue fra: [%llu] = %llu [counter %u]", nam_u->fra_d - req_u->los_u->counter - 1, nam_u->fra_d , req_u->los_u->counter); + _mesa_handle_ack(req_u->gag_u, &req_u->wat_u[nam_u->fra_d]); + return; } } else if ( c3y != lss_verifier_ingest(req_u->los_u, dat_u->fra_y, dat_u->len_w, par_u) ) { @@ -1268,7 +1271,7 @@ _mesa_req_pact_done(u3_pend_req* req_u, MESA_LOG(sam_u, AUTH); return; } - else if ( c3y != _mesa_burn_misorder_queue(req_u) ) { + else if ( c3y != _mesa_burn_misorder_queue(req_u, nam_u->boq_y, req_u->lef_d) ) { u3l_log("about to misorder free"); c3_free(par_u); MESA_LOG(sam_u, AUTH) @@ -1286,12 +1289,8 @@ _mesa_req_pact_done(u3_pend_req* req_u, bitset_del(&req_u->was_u, nam_u->fra_d); #ifdef MESA_DEBUG - u3l_log("fragment %llu counter %llu hav_d %llu nex_d %llu ack_d %llu lef_d %llu old_d %llu", nam_u->fra_d, req_u->los_u->counter, req_u->hav_d, req_u->nex_d, req_u->ack_d, req_u->lef_d, req_u->old_d); + // u3l_log("fragment %llu counter %llu hav_d %llu nex_d %llu ack_d %llu lef_d %llu old_d %llu", nam_u->fra_d, req_u->los_u->counter, req_u->hav_d, req_u->nex_d, req_u->ack_d, req_u->lef_d, req_u->old_d); #endif - if ( req_u->lef_d == nam_u->fra_d ) { - req_u->hav_d++; - req_u->lef_d++; - } u3_lane_state* sat_u; if ( 0 == hop_y && (c3n == _mesa_lanes_equal(&lan_u, &req_u->per_u->dan_u)) ) { From 08462fd9e65abe51d1b2606e666f3f3e63b773fe Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Thu, 19 Sep 2024 08:54:00 +0200 Subject: [PATCH 402/430] mesa: decrease refcount when getting sentinel value from the pit --- pkg/vere/io/mesa.c | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index d4e1e59dca..f046f8d607 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1533,6 +1533,7 @@ _mesa_get_pit(u3_mesa* sam_u, u3_mesa_name* nam_u) u3_weak res = u3h_get(sam_u->pit_p, pax); u3z(pax); if (c3__sent == res) { + u3z(res); return u3_none; } else { From ec18f6c50f548e7df7d6a383420b77fcc1f9aecb Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Fri, 20 Sep 2024 15:27:42 +0200 Subject: [PATCH 403/430] mesa: fix memset --- pkg/vere/io/mesa.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index f046f8d607..a3683966f8 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1195,14 +1195,17 @@ _mesa_burn_misorder_queue(u3_pend_req* req_u, c3_y boq_y, c3_w lef_d) // u3l_log("size %u counter %u num %u fra %u inx %u lef_d %u", siz_w, req_u->los_u->counter , num_w, fra_d, (req_u->los_u->counter + num_w + 1), lef_d); memcpy(req_u->dat_y + (siz_w * (lef_d + num_w + 1)), buf_u->fra_y, buf_u->len_w); } + // ratchet forward num_w++; // account for the in-ordered packet processed in _mesa_req_pact_done req_u->lef_d += num_w; req_u->hav_d += num_w; - memset(req_u->mis_u, 0, num_w * sizeof(u3_misord_buf)); + memcpy(req_u->mis_u, (c3_y*)req_u->mis_u + (num_w * sizeof(u3_misord_buf)), (max_w - num_w) * sizeof(u3_misord_buf)); + memset((c3_y*)req_u->mis_u + ((max_w - (num_w)) * sizeof(u3_misord_buf)), 0, (num_w) * sizeof(u3_misord_buf)); + return res_o; } @@ -1286,7 +1289,7 @@ _mesa_req_pact_done(u3_pend_req* req_u, req_u->ack_d = nam_u->fra_d; } - bitset_del(&req_u->was_u, nam_u->fra_d); + bitset_del(&req_u->was_u, nam_u->fra_d); // XX also for out of order frags? #ifdef MESA_DEBUG // u3l_log("fragment %llu counter %llu hav_d %llu nex_d %llu ack_d %llu lef_d %llu old_d %llu", nam_u->fra_d, req_u->los_u->counter, req_u->hav_d, req_u->nex_d, req_u->ack_d, req_u->lef_d, req_u->old_d); From 9acc0ca114f32d7dcfa0a7f84d92af72959486ec Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Sat, 21 Sep 2024 06:33:10 +0200 Subject: [PATCH 404/430] mesa: correctly free jumbo cache line (u3h_put_get returns the key-value pair, and the jumbo cache line was in the value) --- pkg/vere/io/mesa.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index a3683966f8..76d3cf91f7 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1895,7 +1895,7 @@ _mesa_put_jumbo_cache(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_mesa_line* lin_u) u3_noun pax = _name_to_jumbo_scry(nam_u); u3_weak del = u3h_put_get(sam_u->pac_p, pax, u3a_outa(lin_u)); if ( u3_none != del ) { - _mesa_free_line(u3a_into(del)); + _mesa_free_line(u3a_into(u3t(del))); u3z(del); } u3z(pax); From b83c7774915f4129bffb7d5694d0e852dccbae6d Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Sat, 21 Sep 2024 06:35:03 +0200 Subject: [PATCH 405/430] mesa: add comment --- pkg/vere/io/mesa.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 76d3cf91f7..15997f8bbf 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1312,8 +1312,8 @@ _mesa_req_pact_done(u3_pend_req* req_u, c3_w siz_w = (1 << (nam_u->boq_y - 3)); memcpy(req_u->dat_y + (siz_w * nam_u->fra_d), dat_u->fra_y, dat_u->len_w); - // _try_resend(req_u, req_u->nex_d); - // _try_resend(req_u, nam_u->fra_d); + // XX FIXME? + // _try_resend(req_u, req_u->nex_d); // XX _try_resend(req_u, nam_u->fra_d); _update_resend_timer(req_u); } From 7a33d310a70aae9adeced9b6a672717977d74dbd Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Mon, 23 Sep 2024 11:20:54 +0200 Subject: [PATCH 406/430] jets: add chacha jet declaration for %137 --- pkg/noun/jets/tree.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index 133bb5f8d4..b0fa4a6150 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -2586,15 +2586,16 @@ static u3j_core _137_hex_d[] = { "coed", 63, 0, _140_hex_coed_d, no_hashes }, { "aes", 31, 0, _140_hex_aes_d, no_hashes }, - { "hmac", 63, 0, _140_hex_hmac_d, no_hashes }, - { "argon", 31, 0, _140_hex_argon_d, no_hashes }, - { "blake", 31, 0, _138_hex_blake_d, no_hashes }, - { "kecc", 31, 0, _140_hex_kecc_d, no_hashes }, - { "ripemd", 31, 0, _140_hex_ripe_d, no_hashes }, - { "scr", 31, 0, _140_hex_scr_d, no_hashes }, - { "secp", 6, 0, _140_hex_secp_d, no_hashes }, - { "mimes", 31, 0, _140_hex_mimes_d, no_hashes }, - { "json", 31, 0, _139_hex_json_d, no_hashes }, + { "hmac", 63, 0, _140_hex_hmac_d, no_hashes }, + { "argon", 31, 0, _140_hex_argon_d, no_hashes }, + { "blake", 31, 0, _138_hex_blake_d, no_hashes }, + { "chacha", 31, 0, _138_hex_chacha_d, no_hashes }, + { "kecc", 31, 0, _140_hex_kecc_d, no_hashes }, + { "ripemd", 31, 0, _140_hex_ripe_d, no_hashes }, + { "scr", 31, 0, _140_hex_scr_d, no_hashes }, + { "secp", 6, 0, _140_hex_secp_d, no_hashes }, + { "mimes", 31, 0, _140_hex_mimes_d, no_hashes }, + { "json", 31, 0, _139_hex_json_d, no_hashes }, {} }; From 8467e739c27daf49e2bf9549410ce947941917ca Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Thu, 26 Sep 2024 14:32:19 +0200 Subject: [PATCH 407/430] mesa: WIP congestion control fix --- pkg/vere/io/mesa.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 15997f8bbf..5e3e08d2c4 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -329,7 +329,7 @@ _get_now_micros() { struct timeval tim_u; gettimeofday(&tim_u, NULL); - return (tim_u.tv_sec * 1000000) + tim_u.tv_usec; + return (tim_u.tv_sec * 1000 * 1000) + tim_u.tv_usec; } static c3_d @@ -340,8 +340,7 @@ _abs_dif(c3_d ayy_d, c3_d bee_d) static c3_d _clamp_rto(c3_d rto_d) { - /* u3l_log("clamp rto %llu", rto_d); */ - return c3_min(c3_max(rto_d, 200000), 25000000); + return c3_min(c3_max(rto_d, 200 * 1000), 25000 * 1000); // ~s25 max backoff } static inline c3_o @@ -1313,7 +1312,7 @@ _mesa_req_pact_done(u3_pend_req* req_u, memcpy(req_u->dat_y + (siz_w * nam_u->fra_d), dat_u->fra_y, dat_u->len_w); // XX FIXME? - // _try_resend(req_u, req_u->nex_d); // XX _try_resend(req_u, nam_u->fra_d); + _try_resend(req_u, nam_u->fra_d); // _try_resend(req_u, req_u->nex_d); _update_resend_timer(req_u); } @@ -2916,7 +2915,7 @@ u3_mesa_io_init(u3_pier* pir_u) sam_u->her_p = u3h_new_cache(100000); sam_u->lan_p = u3h_new_cache(100000); sam_u->pac_p = u3h_new_cache(10000); - sam_u->pit_p = u3h_new_cache(10000); + sam_u->pit_p = u3h_new_cache(1000000); u3_assert( !uv_udp_init(u3L, &sam_u->wax_u) ); sam_u->wax_u.data = sam_u; From b669acda22c7d5accfdd4ddb9ac5b015de6a517a Mon Sep 17 00:00:00 2001 From: pkova Date: Fri, 27 Sep 2024 14:33:09 +0300 Subject: [PATCH 408/430] mesa: remove some prints --- pkg/vere/io/mesa.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 5e3e08d2c4..e4f5c0679f 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -779,6 +779,7 @@ _mesa_put_lane(u3_mesa* sam_u, u3_ship her_u, u3_lane* lan_u, u3_gage* gag_u) // congestion control update static void _mesa_handle_ack(u3_gage* gag_u, u3_pact_stat* pat_u) { + /* _log_gage(gag_u); */ gag_u->con_w++; c3_d now_d = _get_now_micros(); @@ -853,7 +854,7 @@ _mesa_req_pact_sent(u3_pend_req* req_u, u3_mesa_name* nam_u) req_u->wat_u[nam_u->fra_d].tie_y = 1; #ifdef MESA_DEBUG - u3l_log("bitset_put %"PRIu64, nam_u->fra_d); + /* u3l_log("bitset_put %"PRIu64, nam_u->fra_d); */ #endif bitset_put(&req_u->was_u, nam_u->fra_d); } else { @@ -962,7 +963,7 @@ static void _mesa_send_buf(u3_mesa* sam_u, u3_lane lan_u, c3_y* buf_y, c3_w len_ #ifdef MESA_DEBUG c3_c* sip_c = inet_ntoa(add_u.sin_addr); - u3l_log("mesa: sending packet to %s:%u", sip_c, por_s); + /* u3l_log("mesa: sending packet to %s:%u", sip_c, por_s); */ #endif uv_buf_t buf_u = uv_buf_init((c3_c*)buf_y, len_w); @@ -1725,7 +1726,7 @@ static c3_o _mesa_kick(u3_mesa* sam_u, u3_noun tag, u3_noun dat) case c3__saxo: { #ifdef MESA_DEBUG c3_c* tag_c = u3r_string(tag); - u3l_log("mesa: send old %s", tag_c); + /* u3l_log("mesa: send old %s", tag_c); */ c3_free(tag_c); #endif ret_o = _ames_kick_newt(u3_Host.sam_u, u3k(tag), u3k(dat)); From 6425d3577f02dda06356afc8f6e4385762ee795f Mon Sep 17 00:00:00 2001 From: pkova Date: Mon, 30 Sep 2024 16:30:28 +0300 Subject: [PATCH 409/430] mesa: call uv_close on the resend timer --- pkg/vere/io/mesa.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index e4f5c0679f..eac0ba80d1 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1016,13 +1016,21 @@ _mesa_free_request_data(u3_mesa_request_data* dat_u) } static void -_mesa_free_resend_data(u3_mesa_resend_data* res_u) +_mesa_free_cb(uv_handle_t *han_u) { - uv_timer_stop(&res_u->tim_u); + u3_mesa_resend_data* res_u = han_u->data; _mesa_free_request_data(&res_u->dat_u); c3_free(res_u); } +static void +_mesa_free_resend_data(u3_mesa_resend_data* res_u) +{ + uv_timer_stop(&res_u->tim_u); + res_u->tim_u.data = res_u; + uv_close((uv_handle_t*)&res_u->tim_u, _mesa_free_cb); +} + static void _mesa_send_bufs(u3_mesa* sam_u, u3_peer* per_u, From 2d5c1a5393d0a07e366b231943abbda7dadde040 Mon Sep 17 00:00:00 2001 From: pkova Date: Mon, 30 Sep 2024 17:13:48 +0300 Subject: [PATCH 410/430] mesa: overlapping ranges need memmove --- pkg/vere/io/mesa.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index eac0ba80d1..c63e27cc89 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1209,9 +1209,9 @@ _mesa_burn_misorder_queue(u3_pend_req* req_u, c3_y boq_y, c3_w lef_d) req_u->lef_d += num_w; req_u->hav_d += num_w; - memcpy(req_u->mis_u, - (c3_y*)req_u->mis_u + (num_w * sizeof(u3_misord_buf)), - (max_w - num_w) * sizeof(u3_misord_buf)); + memmove(req_u->mis_u, + (c3_y*)req_u->mis_u + (num_w * sizeof(u3_misord_buf)), + (max_w - num_w) * sizeof(u3_misord_buf)); memset((c3_y*)req_u->mis_u + ((max_w - (num_w)) * sizeof(u3_misord_buf)), 0, (num_w) * sizeof(u3_misord_buf)); return res_o; From f7b55a5fee0f084eb01fcd06af1b211ae2a1b429 Mon Sep 17 00:00:00 2001 From: pkova Date: Tue, 1 Oct 2024 18:13:15 +0300 Subject: [PATCH 411/430] mesa: freeing but not closing a libuv handle causes loom corruption --- pkg/vere/io/mesa.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index c63e27cc89..ddfa9c6c35 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -601,6 +601,15 @@ _mesa_get_request(u3_mesa* sam_u, u3_mesa_name* nam_u) { u3z(key); return ret_u; } +static void +_mesa_del_request_cb(uv_handle_t* han_u) { + u3_pend_req* req_u = han_u->data; + _mesa_free_pict(req_u->pic_u); + c3_free(req_u->wat_u); + c3_free(req_u->dat_y); + lss_verifier_free(req_u->los_u); + u3a_free(req_u); +} static void _mesa_del_request(u3_mesa* sam_u, u3_mesa_name* nam_u) { @@ -619,13 +628,11 @@ _mesa_del_request(u3_mesa* sam_u, u3_mesa_name* nam_u) { // u3l_log("wat_u %p", req_u->wat_u); // u3l_log("was_u buf %p", req_u->was_u.buf_y); uv_timer_stop(&req_u->tim_u); - _mesa_free_pict(req_u->pic_u); - c3_free(req_u->wat_u); - c3_free(req_u->dat_y); - lss_verifier_free(req_u->los_u); + u3h_del(per_u->req_p, key); - u3a_free(req_u); u3z(key); + req_u->tim_u.data = req_u; + uv_close((uv_handle_t*)&req_u->tim_u, _mesa_del_request_cb); } /* _mesa_put_request(): save new pending request state for nam_u From 06562f786a0cc0c05205f401fa50b18547b78f10 Mon Sep 17 00:00:00 2001 From: pkova Date: Fri, 4 Oct 2024 14:22:54 +0300 Subject: [PATCH 412/430] mesa: do not leak pict --- pkg/vere/io/mesa.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index ddfa9c6c35..dd2e139301 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -2061,6 +2061,7 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun res) if ( u3_none == pag ) { // TODO: mark as dead u3z(res); + _mesa_free_pict(pic_u); u3l_log("mesa: jumbo frame missing"); log_pact(pac_u); return; @@ -2069,6 +2070,7 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun res) if ( c3n == u3r_trel(pag, &pac, &pas, &pof) || c3n == u3a_is_pug(pac) ) { u3l_log("mesa: jumbo frame misshapen"); + _mesa_free_pict(pic_u); log_pact(pac_u); u3z(res); return; @@ -2132,6 +2134,7 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun res) _mesa_put_jumbo_cache(sam_u, nam_u, lin_u); _mesa_send_jumbo_pieces(sam_u, lin_u, NULL); + _mesa_free_pict(pic_u); u3z(res); return; } @@ -2259,7 +2262,8 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) u3_pend_req* req_u = alloca(sizeof(u3_pend_req)); memset(req_u, 0, sizeof(u3_pend_req)); - req_u->pic_u = c3_calloc(sizeof(u3_mesa_pict)); + /* req_u->pic_u = c3_calloc(sizeof(u3_mesa_pict)); */ + req_u->pic_u = pic_u; req_u->pic_u->sam_u = sam_u; req_u->pic_u->pac_u.hed_u.typ_y = PACT_PEEK; req_u->pic_u->pac_u.hed_u.pro_y = MESA_VER; @@ -2587,7 +2591,11 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) u3_ovum_init(0, c3__ames, u3nc(c3__ames, u3_nul), cad)); } else if ( req_u->hav_d < lev_d ) { _mesa_request_next_fragments(sam_u, req_u, lan_u); + _mesa_free_pict(pic_u); + } else { + _mesa_free_pict(pic_u); } + } static void From 5290e9b66a0c2caf004771fb6b5f639e2ac392d4 Mon Sep 17 00:00:00 2001 From: pkova Date: Mon, 7 Oct 2024 19:16:01 +0300 Subject: [PATCH 413/430] mesa: fix very many memory leaks --- pkg/vere/io/mesa.c | 48 +++++++++++++++++++++++++++++++---------- pkg/vere/io/mesa/pact.c | 2 ++ 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index dd2e139301..96d6f99ecb 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -604,9 +604,9 @@ _mesa_get_request(u3_mesa* sam_u, u3_mesa_name* nam_u) { static void _mesa_del_request_cb(uv_handle_t* han_u) { u3_pend_req* req_u = han_u->data; + bitset_free(&req_u->was_u); _mesa_free_pict(req_u->pic_u); c3_free(req_u->wat_u); - c3_free(req_u->dat_y); lss_verifier_free(req_u->los_u); u3a_free(req_u); } @@ -1204,11 +1204,15 @@ _mesa_burn_misorder_queue(u3_pend_req* req_u, c3_y boq_y, c3_w lef_d) if ( c3y != lss_verifier_ingest(req_u->los_u, buf_u->fra_y, buf_u->len_w, buf_u->par_u) ) { res_o = c3n; u3l_log("fail to burn %u", num_w); + c3_free(buf_u->fra_y); + c3_free(buf_u->par_u); break; } c3_w siz_w = (1 << (boq_y - 3)); // XX // u3l_log("size %u counter %u num %u fra %u inx %u lef_d %u", siz_w, req_u->los_u->counter , num_w, fra_d, (req_u->los_u->counter + num_w + 1), lef_d); memcpy(req_u->dat_y + (siz_w * (lef_d + num_w + 1)), buf_u->fra_y, buf_u->len_w); + c3_free(buf_u->fra_y); + c3_free(buf_u->par_u); } // ratchet forward @@ -1662,6 +1666,8 @@ _mesa_ef_send(u3_mesa* sam_u, u3_noun las, u3_noun pac) c3_c* err_c = mesa_sift_pact_from_buf(&pac_u, buf_y, len_w); if ( err_c ) { u3l_log("mesa: ef_send: sift failed: %u %s", len_w, err_c); + u3z(pac); + u3z(las); return; } @@ -1672,6 +1678,9 @@ _mesa_ef_send(u3_mesa* sam_u, u3_noun las, u3_noun pac) #ifdef MESA_DEBUG u3l_log(" no PIT entry"); #endif + u3z(pac); + u3z(las); + mesa_free_pact(&pac_u); return; } @@ -1680,6 +1689,7 @@ _mesa_ef_send(u3_mesa* sam_u, u3_noun las, u3_noun pac) if ( u3_nul != las ) { _mesa_send_bufs(sam_u, NULL, buf_y, len_w, u3k(las)); _mesa_del_pit(sam_u, &pac_u.pek_u.nam_u); + c3_free(buf_y); u3z(pin); } } @@ -1707,7 +1717,7 @@ _mesa_ef_send(u3_mesa* sam_u, u3_noun las, u3_noun pac) sam_u->tim_d = _get_now_micros(); - // TODO: free pac_u or any fields in pac_u? + mesa_free_pact(&pac_u); u3z(pac); u3z(las); } @@ -2092,6 +2102,7 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun res) if ( err_c ) { u3l_log("mesa: jumbo frame parse failure: %s", err_c); log_pact(pac_u); + _mesa_free_pict(pic_u); u3z(res); return; } @@ -2241,6 +2252,8 @@ _mesa_veri_scry_cb(void* vod_p, u3_noun nun) else { u3l_log("mesa: %%veri returned strange value"); } + c3_free(ver_u->nam_u.pat_c); + c3_free(ver_u); } static void @@ -2262,12 +2275,13 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) u3_pend_req* req_u = alloca(sizeof(u3_pend_req)); memset(req_u, 0, sizeof(u3_pend_req)); - /* req_u->pic_u = c3_calloc(sizeof(u3_mesa_pict)); */ - req_u->pic_u = pic_u; + req_u->pic_u = c3_calloc(sizeof(u3_mesa_pict)); req_u->pic_u->sam_u = sam_u; req_u->pic_u->pac_u.hed_u.typ_y = PACT_PEEK; req_u->pic_u->pac_u.hed_u.pro_y = MESA_VER; memcpy(&req_u->pic_u->pac_u.pek_u.nam_u, nam_u, sizeof(u3_mesa_name)); // XX + req_u->pic_u->pac_u.pek_u.nam_u.pat_c = c3_malloc(nam_u->pat_s + 1); + memcpy(req_u->pic_u->pac_u.pek_u.nam_u.pat_c, nam_u->pat_c, nam_u->pat_s + 1); req_u->pic_u->pac_u.pek_u.nam_u.aut_o = c3n; req_u->pic_u->pac_u.pek_u.nam_u.nit_o = c3n; req_u->aut_u = dat_u->aut_u; @@ -2328,6 +2342,8 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, u3_lane* lan_u) u3_mesa_veri_cb_data* ver_u = c3_malloc(sizeof(u3_mesa_veri_cb_data)); ver_u->sam_u = sam_u; memcpy(&ver_u->nam_u, nam_u, sizeof(u3_mesa_name)); + ver_u->nam_u.pat_c = c3_malloc(nam_u->pat_s + 1); + memcpy(ver_u->nam_u.pat_c, nam_u->pat_c, nam_u->pat_s + 1); ver_u->lan_u = *lan_u; u3_pier_peek_last(sam_u->pir_u, u3_nul, c3__a, c3__veri, sky, ver_u, _mesa_veri_scry_cb); } @@ -2458,9 +2474,10 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) if ( u3_none == pin ) { #ifdef MESA_DEBUG - // u3l_log(" no PIT entry"); - // log_name(nam_u); + u3l_log(" no PIT entry"); + log_name(nam_u); #endif + _mesa_free_pict(pic_u); return; } u3_noun our, las; @@ -2485,6 +2502,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) } if ( c3n == our ) { // TODO: free pact and pict + _mesa_free_pict(pic_u); u3z(pin); return; } @@ -2533,9 +2551,9 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) if ( 0 == nam_u->fra_d ) { _mesa_req_pact_init(sam_u, pic_u, &lan_u); } - // _mesa_free_pict(pic_u); // XX leaks packet _mesa_del_pit(sam_u, nam_u); - // TODO free pin, other things too? + _mesa_free_pict(pic_u); // XX leaks packet + u3z(pin); return; } @@ -2574,6 +2592,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) pac_u->pag_u.dat_u.tob_d = req_u->tob_d; pac_u->pag_u.nam_u.fra_d = (req_u->hav_d >> boq_y); pac_u->pag_u.dat_u.len_w = req_u->tob_d; + c3_free(pac_u->pag_u.dat_u.fra_y); pac_u->pag_u.dat_u.fra_y = req_u->dat_y; pac_u->pag_u.dat_u.aut_u = req_u->aut_u; @@ -2586,14 +2605,17 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) } _mesa_del_request(sam_u, &pac_u->pag_u.nam_u); + _mesa_free_pict(pic_u); u3_auto_plan(&sam_u->car_u, u3_ovum_init(0, c3__ames, u3nc(c3__ames, u3_nul), cad)); } else if ( req_u->hav_d < lev_d ) { _mesa_request_next_fragments(sam_u, req_u, lan_u); _mesa_free_pict(pic_u); + u3z(pin); } else { _mesa_free_pict(pic_u); + u3z(pin); } } @@ -2684,15 +2706,16 @@ _mesa_poke_news_cb(u3_ovum* egg_u, u3_ovum_news new_e) } u3_mesa_lane_cb_data* dat_u = egg_u->ptr_v; u3l_log("free(dat_u) in poke_news_cb"); - c3_free(dat_u); + _mesa_free_lane_cb_data(dat_u); } static void _mesa_poke_bail_cb(u3_ovum* egg_u, u3_noun lud) { - u3_mesa_pict* pic_u = egg_u->ptr_v; + u3_mesa_lane_cb_data* dat_u = egg_u->ptr_v; // XX failure stuff here u3l_log("mesa: poke failure"); + _mesa_free_lane_cb_data(dat_u); } static void @@ -2765,11 +2788,14 @@ _mesa_hear_poke(u3_mesa_pict* pic_u, u3_lane* lan_u) { dat_u->nam_u = c3_malloc(sizeof(u3_mesa_name)); memcpy(dat_u->nam_u, &pac_u->pek_u.nam_u, sizeof(u3_mesa_name)); + dat_u->nam_u->pat_c = c3_malloc(pac_u->pek_u.nam_u.pat_s + 1); + memcpy(dat_u->nam_u->pat_c, pac_u->pek_u.nam_u.pat_c, pac_u->pek_u.nam_u.pat_s + 1); dat_u->lan_u.pip_w = lan_u->pip_w; dat_u->lan_u.por_s = lan_u->por_s; dat_u->per_u = per_u; } u3_auto_peer(ovo, dat_u, _mesa_poke_news_cb, _mesa_poke_bail_cb); + _mesa_free_pict(pic_u); } void @@ -2795,7 +2821,7 @@ _mesa_hear(u3_mesa* sam_u, c3_c* err_c = mesa_sift_pact_from_buf(&pic_u->pac_u, hun_y, len_w); if ( err_c ) { u3l_log("mesa: hear: sift failed: %s", err_c); - mesa_free_pact(&pic_u->pac_u); + _mesa_free_pict(pic_u); return; } c3_free(hun_y); diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index 075d91a119..392259a256 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -328,6 +328,8 @@ void mesa_free_pact(u3_mesa_pact* pac_u) break; }; case PACT_POKE: { + /* c3_free(pac_u->pok_u.nam_u.pat_c); */ + c3_free(pac_u->pok_u.pay_u.pat_c); c3_free(pac_u->pok_u.dat_u.fra_y); break; }; From 97c40bc412fb8a19641f0660040e6bfdfbe73556 Mon Sep 17 00:00:00 2001 From: Santeri Hannula Date: Mon, 7 Oct 2024 19:44:29 +0300 Subject: [PATCH 414/430] ext: upgrade urcrypt and include monocypher --- ext/urcrypt/build.zig | 36 ++++++++++++++++++++++++++++++++++++ ext/urcrypt/build.zig.zon | 4 ++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/ext/urcrypt/build.zig b/ext/urcrypt/build.zig index 69af969ded..79a54ac576 100644 --- a/ext/urcrypt/build.zig +++ b/ext/urcrypt/build.zig @@ -28,6 +28,7 @@ pub fn build(b: *std.Build) void { lib.linkLibrary(libed25519(b, target, optimize)); lib.linkLibrary(libge_additions(b, target, optimize)); lib.linkLibrary(libkeccak_tiny(b, target, optimize)); + lib.linkLibrary(libmonocypher(b, target, optimize)); lib.linkLibrary(libscrypt(b, target, optimize)); lib.linkLibrary(libaes_siv(b, target, optimize)); @@ -44,6 +45,7 @@ pub fn build(b: *std.Build) void { "aes_siv.c", "argon.c", "blake3.c", + "chacha.c", "ed25519.c", "ge_additions.c", "keccak.c", @@ -418,6 +420,40 @@ fn libkeccak_tiny( return lib; } +fn libmonocypher( + b: *std.Build, + target: std.Build.ResolvedTarget, + optimize: std.builtin.OptimizeMode, +) *std.Build.Step.Compile { + const dep_c = b.dependency("urcrypt", .{ + .target = target, + .optimize = optimize, + }); + + const lib = b.addStaticLibrary(.{ + .name = "monocypher", + .target = target, + .optimize = optimize, + }); + + lib.linkLibC(); + + lib.addIncludePath(dep_c.path("monocypher")); + + lib.addCSourceFiles(.{ + .root = dep_c.path("monocypher"), + .files = &.{"monocypher.c"}, + .flags = &.{ + "-O2", + "-fno-sanitize=all", + }, + }); + + lib.installHeader(dep_c.path("monocypher/monocypher.h"), "monocypher.h"); + + return lib; +} + fn libscrypt( b: *std.Build, target: std.Build.ResolvedTarget, diff --git a/ext/urcrypt/build.zig.zon b/ext/urcrypt/build.zig.zon index 7732c2a5e2..b8da2ecc08 100644 --- a/ext/urcrypt/build.zig.zon +++ b/ext/urcrypt/build.zig.zon @@ -17,8 +17,8 @@ .hash = "1220bb683a6df744e618f58a008eaae3eb62b70a78334cec676bd82b1b9e8e944eeb", }, .urcrypt = .{ - .url = "https://github.com/urbit/urcrypt/archive/9ae5d604528bc54ae48430f55ebbb17b1ad7956c.tar.gz", - .hash = "1220b2847be5292d9f625b4e238794fcd0d9f944b2165deaf48e68d17d606e2f4205", + .url = "https://github.com/urbit/urcrypt/archive/e77a0998809f7552c455cf7bf8c1b68c141cce18.tar.gz", + .hash = "12202efcf40dcfed9a9b59bba4743cfd0c31364a6d7504724bd4d643b474ff69245b", }, }, .paths = .{ From da7fc1cbbba85d33cb4149350fd30b280786b8e4 Mon Sep 17 00:00:00 2001 From: Santeri Hannula Date: Mon, 7 Oct 2024 19:46:33 +0300 Subject: [PATCH 415/430] mesa: fix pkg includes --- pkg/noun/ship.c | 4 ++-- pkg/noun/ship.h | 4 ++-- pkg/vere/io/lss.h | 2 +- pkg/vere/io/mesa.c | 5 ++--- pkg/vere/io/mesa/bitset.h | 4 ++-- pkg/vere/io/mesa/mesa.h | 2 +- pkg/vere/io/mesa/pact.c | 3 +-- 7 files changed, 11 insertions(+), 13 deletions(-) diff --git a/pkg/noun/ship.c b/pkg/noun/ship.c index 73fa53bd72..6aebff71aa 100644 --- a/pkg/noun/ship.c +++ b/pkg/noun/ship.c @@ -1,4 +1,4 @@ -#include "c3.h" +#include "c3/c3.h" #include "types.h" #include "imprison.h" #include "retrieve.h" @@ -106,4 +106,4 @@ c3_w u3_ship_duke(u3_ship who_u) { return who_u[0] & 0xffffffff; } c3_d -u3_ship_earl(u3_ship who_u) { return who_u[0]; } \ No newline at end of file +u3_ship_earl(u3_ship who_u) { return who_u[0]; } diff --git a/pkg/noun/ship.h b/pkg/noun/ship.h index d1e4696f4b..8104c42d5b 100644 --- a/pkg/noun/ship.h +++ b/pkg/noun/ship.h @@ -1,7 +1,7 @@ #ifndef U3_SHIP_H #define U3_SHIP_H -#include "c3.h" +#include "c3/c3.h" #include "types.h" typedef c3_d u3_ship[2]; @@ -54,4 +54,4 @@ u3_ship_duke(u3_ship who_u); c3_d u3_ship_earl(u3_ship who_u); -#endif /* ifndef U3_SHIP_H */ \ No newline at end of file +#endif /* ifndef U3_SHIP_H */ diff --git a/pkg/vere/io/lss.h b/pkg/vere/io/lss.h index a687a59794..e252a36d45 100644 --- a/pkg/vere/io/lss.h +++ b/pkg/vere/io/lss.h @@ -5,7 +5,7 @@ #include "ivory.h" #include "noun.h" -#include "ur.h" +#include "ur/ur.h" c3_w lss_proof_size(c3_w leaves); diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 96d6f99ecb..0a14ae4e8e 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -4,19 +4,18 @@ #include "ivory.h" #include "noun.h" -#include "ur.h" +#include "ur/ur.h" #include "ship.h" #include "io/ames/stun.h" #include "mesa/mesa.h" #include "mesa/bitset.h" #include -#include #include #include #include #include #include -#include +#include #include #include #include diff --git a/pkg/vere/io/mesa/bitset.h b/pkg/vere/io/mesa/bitset.h index eb76485f89..ab51dd14a8 100644 --- a/pkg/vere/io/mesa/bitset.h +++ b/pkg/vere/io/mesa/bitset.h @@ -1,7 +1,7 @@ #ifndef VERE_BITSET_H #define VERE_BITSET_H -#include "c3.h" +#include "c3/c3.h" typedef struct _u3_bitset { c3_w len_w; @@ -20,4 +20,4 @@ c3_o bitset_has(u3_bitset* bit_u, c3_w mem_w); void bitset_del(u3_bitset* bit_u, c3_w mem_w); -#endif \ No newline at end of file +#endif diff --git a/pkg/vere/io/mesa/mesa.h b/pkg/vere/io/mesa/mesa.h index d9fa43856a..02e08243ae 100644 --- a/pkg/vere/io/mesa/mesa.h +++ b/pkg/vere/io/mesa/mesa.h @@ -1,7 +1,7 @@ #ifndef VERE_MESA_H #define VERE_MESA_H -#include "c3.h" +#include "c3/c3.h" #include "ship.h" #define MESA_VER 1 diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index 392259a256..5d2eaf0b6e 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -1,12 +1,11 @@ #include "mesa.h" -#include #include #include #include // only need for tests, can remove #include "vere.h" #include "ivory.h" -#include "ur.h" +#include "ur/ur.h" #include "ship.h" #define RED_TEXT "\033[0;31m" #define DEF_TEXT "\033[0m" From 494f136ec3dbc3197ed77cb59f0abb921d20c961 Mon Sep 17 00:00:00 2001 From: Santeri Hannula Date: Mon, 7 Oct 2024 19:47:35 +0300 Subject: [PATCH 416/430] crc: fix incompitable pointer type --- pkg/noun/jets/e/crc32.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/noun/jets/e/crc32.c b/pkg/noun/jets/e/crc32.c index 1afc473a30..0daa032c7f 100644 --- a/pkg/noun/jets/e/crc32.c +++ b/pkg/noun/jets/e/crc32.c @@ -21,7 +21,7 @@ u3qe_crc32(u3_noun input_octs) c3_y* input; if (c3y == u3a_is_cat(tail)) { - input = &tail; + input = (c3_y*)&tail; } else { u3a_atom* vat_u = u3a_to_ptr(tail); From 98a4a0e1e1fafbe2999a68c351438ad6647b7cd4 Mon Sep 17 00:00:00 2001 From: Santeri Hannula Date: Mon, 7 Oct 2024 19:47:58 +0300 Subject: [PATCH 417/430] mesa: initialize var before usage --- pkg/vere/io/mesa.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 0a14ae4e8e..3f4efaf62b 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1866,7 +1866,7 @@ _mesa_add_galaxy_pend(u3_mesa* sam_u, u3_noun her, u3_noun pen) { u3_weak old = u3h_get(sam_u->her_p, her); u3_noun pes = u3_nul; - u3_noun wat; + u3_noun wat = {0}; if ( u3_none != old ) { if ( u3h(old) == MESA_CZAR ) { u3x_cell(u3t(old), &pes, &wat); From 53434c57914a4befe8a986f96a81b7a7040086bb Mon Sep 17 00:00:00 2001 From: Santeri Hannula Date: Mon, 7 Oct 2024 19:49:31 +0300 Subject: [PATCH 418/430] mesa: use correct format specifiers on linux --- pkg/vere/io/mesa.c | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 3f4efaf62b..fba83efa74 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1280,7 +1280,14 @@ _mesa_req_pact_done(u3_pend_req* req_u, buf_u->len_w = dat_u->len_w; memcpy(buf_u->fra_y, dat_u->fra_y, dat_u->len_w); buf_u->par_u = par_u; - u3l_log("insert into misordered queue fra: [%llu] = %llu [counter %u]", nam_u->fra_d - req_u->los_u->counter - 1, nam_u->fra_d , req_u->los_u->counter); +#ifdef U3_OS_osx + u3l_log("insert into misordered queue fra: [%llu] = %llu [counter %u]", +#else + u3l_log("insert into misordered queue fra: [%lu] = %lu [counter %u]", +#endif + nam_u->fra_d - req_u->los_u->counter - 1, + nam_u->fra_d, + req_u->los_u->counter); _mesa_handle_ack(req_u->gag_u, &req_u->wat_u[nam_u->fra_d]); return; } @@ -2578,9 +2585,21 @@ _mesa_hear_page(u3_mesa_pict* pic_u, u3_lane lan_u) if ( c3y == done_with_jumbo_frame ) { u3_noun cad; - u3l_log(" received last packet, tof_d: %llu tob_d: %llu", req_u->tof_d, req_u->tob_d); +#ifdef U3_OS_osx + u3l_log(" received last packet, tof_d: %llu tob_d: %llu", +#else + u3l_log(" received last packet, tof_d: %lu tob_d: %lu", +#endif + req_u->tof_d, + req_u->tob_d); c3_d now_d = _get_now_micros(); - u3l_log("%llu kilobytes took %f ms", req_u->tof_d, (now_d - sam_u->tim_d)/1000.0); +#ifdef U3_OS_osx + u3l_log("%llu kilobytes took %f ms", +#else + u3l_log("%lu kilobytes took %f ms", +#endif + req_u->tof_d, + (now_d - sam_u->tim_d)/1000.0); { // construct jumbo frame From 2a01589ed7acab172a5734a42f64e453460c754e Mon Sep 17 00:00:00 2001 From: Santeri Hannula Date: Mon, 7 Oct 2024 19:50:20 +0300 Subject: [PATCH 419/430] build: update build for mesa-pairs --- build.zig | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/build.zig b/build.zig index e6f1b25970..2220aab9c9 100644 --- a/build.zig +++ b/build.zig @@ -489,6 +489,9 @@ fn build_single( pkg_noun.linkLibrary(unwind.artifact("unwind")); pkg_noun.linkLibrary(urcrypt.artifact("urcrypt")); pkg_noun.linkLibrary(whereami.artifact("whereami")); + if (t.os.tag == .linux) + pkg_noun.linkLibrary(zlib.artifact("z")); + pkg_noun.linkLibC(); pkg_noun.addIncludePath(b.path("pkg/noun")); @@ -556,12 +559,16 @@ fn build_single( "jets/c/can.c", "jets/c/cap.c", "jets/c/cat.c", + "jets/c/clz.c", + "jets/c/ctz.c", "jets/c/cut.c", "jets/c/dis.c", "jets/c/dor.c", "jets/c/dvr.c", "jets/c/end.c", "jets/c/gor.c", + "jets/c/ham.c", + "jets/c/hew.c", "jets/c/lsh.c", "jets/c/mas.c", "jets/c/met.c", @@ -575,6 +582,7 @@ fn build_single( "jets/c/rap.c", "jets/c/rep.c", "jets/c/rev.c", + "jets/c/rig.c", "jets/c/rip.c", "jets/c/rsh.c", "jets/c/sqt.c", @@ -615,6 +623,8 @@ fn build_single( "jets/e/argon2.c", "jets/e/base.c", "jets/e/blake.c", + "jets/e/chacha.c", + "jets/e/crc32.c", "jets/e/cue.c", "jets/e/ed_add_double_scalarmult.c", "jets/e/ed_add_scalarmult_scalarmult_base.c", @@ -678,12 +688,14 @@ fn build_single( "jets/f/ut_mull.c", "jets/f/ut_nest.c", "jets/f/ut_rest.c", + "jets/g/plot.c", "jets/tree.c", "log.c", "manage.c", "nock.c", "options.c", "retrieve.c", + "ship.c", "serial.c", "trace.c", "urth.c", @@ -771,6 +783,7 @@ fn build_single( vere.linkLibrary(libuv.artifact("libuv")); vere.linkLibrary(lmdb.artifact("lmdb")); vere.linkLibrary(openssl.artifact("ssl")); + vere.linkLibrary(urcrypt.artifact("urcrypt")); vere.linkLibrary(zlib.artifact("z")); vere.linkLibrary(pkg_c3); vere.linkLibrary(pkg_ent); @@ -797,6 +810,10 @@ fn build_single( "io/hind.c", "io/http.c", "io/lick.c", + "io/lss.c", + "io/mesa.c", + "io/mesa/bitset.c", + "io/mesa/pact.c", "io/term.c", "io/unix.c", "ivory/ivory.c", @@ -873,6 +890,7 @@ fn build_single( urbit.linkLibrary(lmdb.artifact("lmdb")); urbit.linkLibrary(openssl.artifact("ssl")); urbit.linkLibrary(sigsegv.artifact("sigsegv")); + urbit.linkLibrary(urcrypt.artifact("urcrypt")); urbit.linkLibrary(whereami.artifact("whereami")); urbit.addCSourceFiles(.{ From ee7567b99668f1771cdbfaebd54aaddc13ad21c2 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Fri, 11 Oct 2024 13:17:12 +0200 Subject: [PATCH 420/430] mesa: remove dereference of null pointer --- pkg/vere/io/mesa.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index fba83efa74..2c716ac7e9 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -2036,7 +2036,7 @@ _mesa_send_jumbo_pieces(u3_mesa* sam_u, u3_mesa_line* lin_u, c3_d* fra_u) } // single-fragment message; just send the one data fragment else if ( ( NULL == fra_u) || (0 == *fra_u) ) { - _mesa_send_leaf(sam_u, lin_u, &pac_u, *fra_u); + _mesa_send_leaf(sam_u, lin_u, &pac_u, 0); } else { u3l_log("mesa: weird fragment number %"PRIu64, *fra_u); From 63ec415a9489898e0ac8dd35bab82eb1558a28bb Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Sun, 13 Oct 2024 13:25:11 +0200 Subject: [PATCH 421/430] mesa: scry again if previous +peek blocked --- pkg/vere/io/mesa.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 2c716ac7e9..aec5142d1a 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -2696,11 +2696,9 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) // if we have the page, send it u3_mesa_line* lin_u = _mesa_get_jumbo_cache(sam_u, &pac_u->pek_u.nam_u); - if ( NULL != lin_u ) { - if ( CTAG_ITEM == lin_u->typ_y ) { - _mesa_send_jumbo_pieces(sam_u, lin_u, &fra_d); - _mesa_free_pict(pic_u); - } + if ( ( NULL != lin_u ) && ( CTAG_ITEM == lin_u->typ_y )) { + _mesa_send_jumbo_pieces(sam_u, lin_u, &fra_d); + _mesa_free_pict(pic_u); return; } // otherwise, scry From 5e597202bd4ec4299ce0b4c6582adf1aa97ca3b0 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Mon, 14 Oct 2024 13:47:22 +0200 Subject: [PATCH 422/430] nock: revert byte padding nock.c --- pkg/noun/nock.c | 35 ++++++++++++----------------------- 1 file changed, 12 insertions(+), 23 deletions(-) diff --git a/pkg/noun/nock.c b/pkg/noun/nock.c index f9814a395f..eb459d17f7 100644 --- a/pkg/noun/nock.c +++ b/pkg/noun/nock.c @@ -706,11 +706,7 @@ _n_prog_new(c3_w byc_w, c3_w cal_w, reb_w = (sizeof(u3j_rite) * reg_w), lib_w = (sizeof(u3_noun) * lit_w), meb_w = (sizeof(u3n_memo) * mem_w), - pad_w = (8 - byc_w % 8) % 8, - pod_w = lit_w % 2, - ped_w = mem_w % 2, - dat_w = byc_w + cab_w + reb_w + lib_w + meb_w + pad_w + - (pod_w * sizeof(u3_noun)) + (ped_w * sizeof(u3n_memo)); + dat_w = byc_w + cab_w + reb_w + lib_w + meb_w; u3n_prog* pog_u = u3a_malloc(sizeof(u3n_prog) + dat_w); pog_u->byc_u.own_o = c3y; @@ -718,13 +714,13 @@ _n_prog_new(c3_w byc_w, c3_w cal_w, pog_u->byc_u.ops_y = (c3_y*) _n_prog_dat(pog_u); pog_u->lit_u.len_w = lit_w; - pog_u->lit_u.non = (u3_noun*) (pog_u->byc_u.ops_y + pog_u->byc_u.len_w + pad_w); + pog_u->lit_u.non = (u3_noun*) (pog_u->byc_u.ops_y + pog_u->byc_u.len_w); pog_u->mem_u.len_w = mem_w; - pog_u->mem_u.sot_u = (u3n_memo*) (pog_u->lit_u.non + pog_u->lit_u.len_w + pod_w); + pog_u->mem_u.sot_u = (u3n_memo*) (pog_u->lit_u.non + pog_u->lit_u.len_w); pog_u->cal_u.len_w = cal_w; - pog_u->cal_u.sit_u = (u3j_site*) (pog_u->mem_u.sot_u + pog_u->mem_u.len_w + ped_w); + pog_u->cal_u.sit_u = (u3j_site*) (pog_u->mem_u.sot_u + pog_u->mem_u.len_w); pog_u->reg_u.len_w = reg_w; pog_u->reg_u.rit_u = (u3j_rite*) (pog_u->cal_u.sit_u + pog_u->cal_u.len_w); @@ -742,10 +738,7 @@ _n_prog_old(u3n_prog* sep_u) reb_w = sizeof(u3j_rite) * sep_u->reg_u.len_w, lib_w = sizeof(u3_noun) * sep_u->lit_u.len_w, meb_w = sizeof(u3n_memo) * sep_u->mem_u.len_w, - pod_w = sep_u->lit_u.len_w % 2, - ped_w = sep_u->mem_u.len_w % 2, - dat_w = cab_w + reb_w + lib_w + meb_w + - (pod_w * sizeof(u3_noun)) + (ped_w * sizeof(u3n_memo)); + dat_w = cab_w + reb_w + lib_w + meb_w; u3n_prog* pog_u = u3a_malloc(sizeof(u3n_prog) + dat_w); pog_u->byc_u.own_o = c3n; @@ -756,10 +749,10 @@ _n_prog_old(u3n_prog* sep_u) pog_u->lit_u.non = (u3_noun*) _n_prog_dat(pog_u); pog_u->mem_u.len_w = sep_u->mem_u.len_w; - pog_u->mem_u.sot_u = (u3n_memo*) (pog_u->lit_u.non + pog_u->lit_u.len_w + pod_w); + pog_u->mem_u.sot_u = (u3n_memo*) (pog_u->lit_u.non + pog_u->lit_u.len_w); pog_u->cal_u.len_w = sep_u->cal_u.len_w; - pog_u->cal_u.sit_u = (u3j_site*) (pog_u->mem_u.sot_u + pog_u->mem_u.len_w + ped_w); + pog_u->cal_u.sit_u = (u3j_site*) (pog_u->mem_u.sot_u + pog_u->mem_u.len_w); pog_u->reg_u.len_w = sep_u->reg_u.len_w; pog_u->reg_u.rit_u = (u3j_rite*) (pog_u->cal_u.sit_u + pog_u->cal_u.len_w); @@ -2043,7 +2036,7 @@ _n_kale(u3_noun a) return a; } -typedef struct __attribute__((__packed__)) { +typedef struct { u3n_prog* pog_u; c3_w ip_w; } burnframe; @@ -2895,13 +2888,12 @@ _cn_take_prog_cb(u3p(u3n_prog) pog_p) u3n_prog* gop_u; if ( c3y == pog_u->byc_u.own_o ) { - c3_w pad_w = (8 - pog_u->byc_u.len_w % 8) % 8; gop_u = _n_prog_new(pog_u->byc_u.len_w, pog_u->cal_u.len_w, pog_u->reg_u.len_w, pog_u->lit_u.len_w, pog_u->mem_u.len_w); - memcpy(gop_u->byc_u.ops_y, pog_u->byc_u.ops_y, pog_u->byc_u.len_w + pad_w); + memcpy(gop_u->byc_u.ops_y, pog_u->byc_u.ops_y, pog_u->byc_u.len_w); } else { gop_u = _n_prog_old(pog_u); @@ -2996,14 +2988,11 @@ _n_ream(u3_noun kev) c3_w i_w; u3n_prog* pog_u = u3to(u3n_prog, u3t(kev)); - c3_w pad_w = (8 - pog_u->byc_u.len_w % 8) % 8; - c3_w pod_w = pog_u->lit_u.len_w % 2; - c3_w ped_w = pog_u->mem_u.len_w % 2; // fix up pointers for loom portability pog_u->byc_u.ops_y = (c3_y*) _n_prog_dat(pog_u); - pog_u->lit_u.non = (u3_noun*) (pog_u->byc_u.ops_y + pog_u->byc_u.len_w + pad_w); - pog_u->mem_u.sot_u = (u3n_memo*) (pog_u->lit_u.non + pog_u->lit_u.len_w + pod_w); - pog_u->cal_u.sit_u = (u3j_site*) (pog_u->mem_u.sot_u + pog_u->mem_u.len_w + ped_w); + pog_u->lit_u.non = (u3_noun*) (pog_u->byc_u.ops_y + pog_u->byc_u.len_w); + pog_u->mem_u.sot_u = (u3n_memo*) (pog_u->lit_u.non + pog_u->lit_u.len_w); + pog_u->cal_u.sit_u = (u3j_site*) (pog_u->mem_u.sot_u + pog_u->mem_u.len_w); pog_u->reg_u.rit_u = (u3j_rite*) (pog_u->cal_u.sit_u + pog_u->cal_u.len_w); for ( i_w = 0; i_w < pog_u->cal_u.len_w; ++i_w ) { From f1d8477b9e4fa59903a5d0dede525c955624e1bf Mon Sep 17 00:00:00 2001 From: pkova Date: Thu, 17 Oct 2024 19:02:32 +0300 Subject: [PATCH 423/430] build: merge develop to next/kelvin/410, convert lagoon to zig build --- build.zig | 9 +++++ build.zig.zon | 3 ++ ext/softblas/build.zig | 80 ++++++++++++++++++++++++++++++++++++++ ext/softblas/build.zig.zon | 16 ++++++++ pkg/noun/jets/e/crc32.c | 2 +- pkg/noun/jets/i/lagoon.c | 7 ++-- 6 files changed, 113 insertions(+), 4 deletions(-) create mode 100644 ext/softblas/build.zig create mode 100644 ext/softblas/build.zig.zon diff --git a/build.zig b/build.zig index e6f1b25970..815aedeb69 100644 --- a/build.zig +++ b/build.zig @@ -283,6 +283,11 @@ fn build_single( .optimize = optimize, }); + const softblas = b.dependency("softblas", .{ + .target = target, + .optimize = optimize, + }); + const softfloat = b.dependency("softfloat", .{ .target = target, .optimize = optimize, @@ -485,10 +490,12 @@ fn build_single( pkg_noun.linkLibrary(pdjson.artifact("pdjson")); pkg_noun.linkLibrary(sigsegv.artifact("sigsegv")); pkg_noun.linkLibrary(softfloat.artifact("softfloat")); + pkg_noun.linkLibrary(softblas.artifact("softblas")); if (t.os.tag == .linux) pkg_noun.linkLibrary(unwind.artifact("unwind")); pkg_noun.linkLibrary(urcrypt.artifact("urcrypt")); pkg_noun.linkLibrary(whereami.artifact("whereami")); + pkg_noun.linkLibrary(zlib.artifact("z")); pkg_noun.linkLibC(); pkg_noun.addIncludePath(b.path("pkg/noun")); @@ -615,6 +622,7 @@ fn build_single( "jets/e/argon2.c", "jets/e/base.c", "jets/e/blake.c", + "jets/e/crc32.c", "jets/e/cue.c", "jets/e/ed_add_double_scalarmult.c", "jets/e/ed_add_scalarmult_scalarmult_base.c", @@ -678,6 +686,7 @@ fn build_single( "jets/f/ut_mull.c", "jets/f/ut_nest.c", "jets/f/ut_rest.c", + "jets/i/lagoon.c", "jets/tree.c", "log.c", "manage.c", diff --git a/build.zig.zon b/build.zig.zon index f39c710e39..3f6a8c07b7 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -49,6 +49,9 @@ .softfloat = .{ .path = "./ext/softfloat", }, + .softblas = .{ + .path = "./ext/softblas", + }, .unwind = .{ .path = "./ext/unwind", }, diff --git a/ext/softblas/build.zig b/ext/softblas/build.zig new file mode 100644 index 0000000000..1fb3b066b5 --- /dev/null +++ b/ext/softblas/build.zig @@ -0,0 +1,80 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) void { + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + + const lib = b.addStaticLibrary(.{ + .name = "softblas", + .target = target, + .optimize = optimize, + }); + + const dep_c = b.dependency("softblas", .{ + .target = target, + .optimize = optimize, + }); + + const softfloat = b.dependency("softfloat", .{ + .target = target, + .optimize = optimize, + }); + + lib.addIncludePath(dep_c.path("include")); + + lib.addCSourceFiles(.{ + .root = dep_c.path(""), + .files = &.{ + "src/softblas_state.c", + "src/blas/level1/sasum.c", + "src/blas/level1/dasum.c", + "src/blas/level1/hasum.c", + "src/blas/level1/qasum.c", + "src/blas/level1/saxpy.c", + "src/blas/level1/daxpy.c", + "src/blas/level1/haxpy.c", + "src/blas/level1/qaxpy.c", + "src/blas/level1/scopy.c", + "src/blas/level1/dcopy.c", + "src/blas/level1/hcopy.c", + "src/blas/level1/qcopy.c", + "src/blas/level1/sdot.c", + "src/blas/level1/ddot.c", + "src/blas/level1/hdot.c", + "src/blas/level1/qdot.c", + "src/blas/level1/snrm2.c", + "src/blas/level1/dnrm2.c", + "src/blas/level1/hnrm2.c", + "src/blas/level1/qnrm2.c", + "src/blas/level1/sscal.c", + "src/blas/level1/dscal.c", + "src/blas/level1/hscal.c", + "src/blas/level1/qscal.c", + "src/blas/level1/sswap.c", + "src/blas/level1/dswap.c", + "src/blas/level1/hswap.c", + "src/blas/level1/qswap.c", + "src/blas/level1/isamax.c", + "src/blas/level1/idamax.c", + "src/blas/level1/ihamax.c", + "src/blas/level1/iqamax.c", + "src/blas/level2/sgemv.c", + "src/blas/level2/dgemv.c", + "src/blas/level2/hgemv.c", + "src/blas/level2/qgemv.c", + "src/blas/level3/sgemm.c", + "src/blas/level3/dgemm.c", + "src/blas/level3/hgemm.c", + "src/blas/level3/qgemm.c", + }, + .flags = &.{ + "-fno-sanitize=all", + }, + }); + + lib.installHeader(dep_c.path("include/softblas.h"), "softblas.h"); + + lib.linkLibC(); + lib.linkLibrary(softfloat.artifact("softfloat")); + b.installArtifact(lib); +} diff --git a/ext/softblas/build.zig.zon b/ext/softblas/build.zig.zon new file mode 100644 index 0000000000..431171b53b --- /dev/null +++ b/ext/softblas/build.zig.zon @@ -0,0 +1,16 @@ +.{ + .name = "softblas", + .version = "0.0.1", + .dependencies = .{ + .softfloat = .{ + .path = "../softfloat", + }, + .softblas = .{ + .url = "https://github.com/urbit/SoftBLAS/archive/cbffb33f19ea02f9ffbd184d445123c57929ec53.tar.gz", + .hash = "1220617c11d869ef2316571a430f51f93470e2d714141deb3bdfaa6b578cf151f258", + }, + }, + .paths = .{ + "", + }, +} diff --git a/pkg/noun/jets/e/crc32.c b/pkg/noun/jets/e/crc32.c index 1afc473a30..0daa032c7f 100644 --- a/pkg/noun/jets/e/crc32.c +++ b/pkg/noun/jets/e/crc32.c @@ -21,7 +21,7 @@ u3qe_crc32(u3_noun input_octs) c3_y* input; if (c3y == u3a_is_cat(tail)) { - input = &tail; + input = (c3_y*)&tail; } else { u3a_atom* vat_u = u3a_to_ptr(tail); diff --git a/pkg/noun/jets/i/lagoon.c b/pkg/noun/jets/i/lagoon.c index 13c0f2a138..db7088b96e 100644 --- a/pkg/noun/jets/i/lagoon.c +++ b/pkg/noun/jets/i/lagoon.c @@ -3,6 +3,8 @@ #include "jets/q.h" #include "jets/w.h" +#include "c3/motes.h" + #include "noun.h" #include "softfloat.h" #include "softblas.h" @@ -1628,7 +1630,7 @@ for (c3_d i = 0; i < len_x; i++) { float32_t x_val32 = ((float32_t*)x_bytes)[i]; // Perform division x/n - float32_t div_result32 = f32_mul((float32_t)in32, (float32_t)x_val32); + float32_t div_result32 = f32_mul(in32, x_val32); // Compute floor of the division result c3_ds floor_result32 = f32_to_i64(div_result32, softfloat_round_minMag, false); float32_t floor_float32 = i64_to_f32(floor_result32); @@ -3284,13 +3286,12 @@ { return u3m_bail(c3__exit); } else { - u3_noun x_shape, x_bloq, x_kind, x_tail, + u3_noun x_shape, x_bloq, x_kind, y_shape, rnd; x_shape = u3h(x_meta); // 2 x_bloq = u3h(u3t(x_meta)); // 6 x_kind = u3h(u3t(u3t(x_meta))); // 14 - x_tail = u3t(u3t(u3t(x_meta))); // 15 y_shape = u3h(y_meta); // 2 rnd = u3h(u3t(u3t(u3t(cor)))); // 30 if ( c3n == _check(u3nc(x_meta, x_data)) || From 75808282fca09112aedaa20f0dcbba98e02e0fee Mon Sep 17 00:00:00 2001 From: pkova Date: Fri, 18 Oct 2024 16:02:31 +0300 Subject: [PATCH 424/430] Revert "nock: revert byte padding nock.c" This reverts commit 5e597202bd4ec4299ce0b4c6582adf1aa97ca3b0. --- pkg/noun/nock.c | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/pkg/noun/nock.c b/pkg/noun/nock.c index eb459d17f7..f9814a395f 100644 --- a/pkg/noun/nock.c +++ b/pkg/noun/nock.c @@ -706,7 +706,11 @@ _n_prog_new(c3_w byc_w, c3_w cal_w, reb_w = (sizeof(u3j_rite) * reg_w), lib_w = (sizeof(u3_noun) * lit_w), meb_w = (sizeof(u3n_memo) * mem_w), - dat_w = byc_w + cab_w + reb_w + lib_w + meb_w; + pad_w = (8 - byc_w % 8) % 8, + pod_w = lit_w % 2, + ped_w = mem_w % 2, + dat_w = byc_w + cab_w + reb_w + lib_w + meb_w + pad_w + + (pod_w * sizeof(u3_noun)) + (ped_w * sizeof(u3n_memo)); u3n_prog* pog_u = u3a_malloc(sizeof(u3n_prog) + dat_w); pog_u->byc_u.own_o = c3y; @@ -714,13 +718,13 @@ _n_prog_new(c3_w byc_w, c3_w cal_w, pog_u->byc_u.ops_y = (c3_y*) _n_prog_dat(pog_u); pog_u->lit_u.len_w = lit_w; - pog_u->lit_u.non = (u3_noun*) (pog_u->byc_u.ops_y + pog_u->byc_u.len_w); + pog_u->lit_u.non = (u3_noun*) (pog_u->byc_u.ops_y + pog_u->byc_u.len_w + pad_w); pog_u->mem_u.len_w = mem_w; - pog_u->mem_u.sot_u = (u3n_memo*) (pog_u->lit_u.non + pog_u->lit_u.len_w); + pog_u->mem_u.sot_u = (u3n_memo*) (pog_u->lit_u.non + pog_u->lit_u.len_w + pod_w); pog_u->cal_u.len_w = cal_w; - pog_u->cal_u.sit_u = (u3j_site*) (pog_u->mem_u.sot_u + pog_u->mem_u.len_w); + pog_u->cal_u.sit_u = (u3j_site*) (pog_u->mem_u.sot_u + pog_u->mem_u.len_w + ped_w); pog_u->reg_u.len_w = reg_w; pog_u->reg_u.rit_u = (u3j_rite*) (pog_u->cal_u.sit_u + pog_u->cal_u.len_w); @@ -738,7 +742,10 @@ _n_prog_old(u3n_prog* sep_u) reb_w = sizeof(u3j_rite) * sep_u->reg_u.len_w, lib_w = sizeof(u3_noun) * sep_u->lit_u.len_w, meb_w = sizeof(u3n_memo) * sep_u->mem_u.len_w, - dat_w = cab_w + reb_w + lib_w + meb_w; + pod_w = sep_u->lit_u.len_w % 2, + ped_w = sep_u->mem_u.len_w % 2, + dat_w = cab_w + reb_w + lib_w + meb_w + + (pod_w * sizeof(u3_noun)) + (ped_w * sizeof(u3n_memo)); u3n_prog* pog_u = u3a_malloc(sizeof(u3n_prog) + dat_w); pog_u->byc_u.own_o = c3n; @@ -749,10 +756,10 @@ _n_prog_old(u3n_prog* sep_u) pog_u->lit_u.non = (u3_noun*) _n_prog_dat(pog_u); pog_u->mem_u.len_w = sep_u->mem_u.len_w; - pog_u->mem_u.sot_u = (u3n_memo*) (pog_u->lit_u.non + pog_u->lit_u.len_w); + pog_u->mem_u.sot_u = (u3n_memo*) (pog_u->lit_u.non + pog_u->lit_u.len_w + pod_w); pog_u->cal_u.len_w = sep_u->cal_u.len_w; - pog_u->cal_u.sit_u = (u3j_site*) (pog_u->mem_u.sot_u + pog_u->mem_u.len_w); + pog_u->cal_u.sit_u = (u3j_site*) (pog_u->mem_u.sot_u + pog_u->mem_u.len_w + ped_w); pog_u->reg_u.len_w = sep_u->reg_u.len_w; pog_u->reg_u.rit_u = (u3j_rite*) (pog_u->cal_u.sit_u + pog_u->cal_u.len_w); @@ -2036,7 +2043,7 @@ _n_kale(u3_noun a) return a; } -typedef struct { +typedef struct __attribute__((__packed__)) { u3n_prog* pog_u; c3_w ip_w; } burnframe; @@ -2888,12 +2895,13 @@ _cn_take_prog_cb(u3p(u3n_prog) pog_p) u3n_prog* gop_u; if ( c3y == pog_u->byc_u.own_o ) { + c3_w pad_w = (8 - pog_u->byc_u.len_w % 8) % 8; gop_u = _n_prog_new(pog_u->byc_u.len_w, pog_u->cal_u.len_w, pog_u->reg_u.len_w, pog_u->lit_u.len_w, pog_u->mem_u.len_w); - memcpy(gop_u->byc_u.ops_y, pog_u->byc_u.ops_y, pog_u->byc_u.len_w); + memcpy(gop_u->byc_u.ops_y, pog_u->byc_u.ops_y, pog_u->byc_u.len_w + pad_w); } else { gop_u = _n_prog_old(pog_u); @@ -2988,11 +2996,14 @@ _n_ream(u3_noun kev) c3_w i_w; u3n_prog* pog_u = u3to(u3n_prog, u3t(kev)); + c3_w pad_w = (8 - pog_u->byc_u.len_w % 8) % 8; + c3_w pod_w = pog_u->lit_u.len_w % 2; + c3_w ped_w = pog_u->mem_u.len_w % 2; // fix up pointers for loom portability pog_u->byc_u.ops_y = (c3_y*) _n_prog_dat(pog_u); - pog_u->lit_u.non = (u3_noun*) (pog_u->byc_u.ops_y + pog_u->byc_u.len_w); - pog_u->mem_u.sot_u = (u3n_memo*) (pog_u->lit_u.non + pog_u->lit_u.len_w); - pog_u->cal_u.sit_u = (u3j_site*) (pog_u->mem_u.sot_u + pog_u->mem_u.len_w); + pog_u->lit_u.non = (u3_noun*) (pog_u->byc_u.ops_y + pog_u->byc_u.len_w + pad_w); + pog_u->mem_u.sot_u = (u3n_memo*) (pog_u->lit_u.non + pog_u->lit_u.len_w + pod_w); + pog_u->cal_u.sit_u = (u3j_site*) (pog_u->mem_u.sot_u + pog_u->mem_u.len_w + ped_w); pog_u->reg_u.rit_u = (u3j_rite*) (pog_u->cal_u.sit_u + pog_u->cal_u.len_w); for ( i_w = 0; i_w < pog_u->cal_u.len_w; ++i_w ) { From c4ba180e1d49f8ecbffcba260cb2c213eb65ddc6 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Mon, 14 Oct 2024 13:54:01 +0200 Subject: [PATCH 425/430] Revert "bazel: delete bazel build system" This reverts commit 6d3f9b2e1ce648583e733a6cc9fa4eac9ec5c9fe. --- .bazelrc | 51 ++ BUILD.bazel | 214 ++++++ PACE | 1 + VERSION | 1 + WORKSPACE.bazel | 448 ++++++++++++ bazel/BUILD.bazel | 21 + bazel/common_settings.bzl | 50 ++ bazel/repo.bzl | 34 + bazel/third_party/aes_siv/BUILD.bazel | 0 bazel/third_party/aes_siv/aes_siv.BUILD | 21 + bazel/third_party/argon2/BUILD.bazel | 0 bazel/third_party/argon2/argon2.BUILD | 42 ++ bazel/third_party/avahi/BUILD.bazel | 0 bazel/third_party/avahi/avahi.BUILD | 30 + bazel/third_party/curl/BUILD.bazel | 0 bazel/third_party/curl/curl.BUILD | 67 ++ bazel/third_party/dbus/BUILD.bazel | 0 bazel/third_party/dbus/dbus.BUILD | 21 + bazel/third_party/ed25519/BUILD.bazel | 0 bazel/third_party/ed25519/ed25519.BUILD | 14 + bazel/third_party/expat/BUILD.bazel | 0 bazel/third_party/expat/expat.BUILD | 24 + bazel/third_party/gmp/BUILD.bazel | 0 bazel/third_party/gmp/gmp.BUILD | 30 + bazel/third_party/h2o/2.2.6.patch | 47 ++ bazel/third_party/h2o/BUILD.bazel | 0 bazel/third_party/h2o/h2o.BUILD | 375 +++++++++++ ...647514212b76ae7bca0dea9b7b197d1d8186.patch | 168 +++++ bazel/third_party/keccak_tiny/BUILD.bazel | 0 .../third_party/keccak_tiny/keccak_tiny.BUILD | 16 + bazel/third_party/libbacktrace/BUILD.bazel | 0 .../libbacktrace/libbacktrace.BUILD | 25 + bazel/third_party/libunwind/BUILD.bazel | 0 bazel/third_party/libunwind/libunwind.BUILD | 25 + bazel/third_party/lmdb/0.9.29.patch | 16 + bazel/third_party/lmdb/BUILD.bazel | 0 bazel/third_party/lmdb/lmdb.BUILD | 29 + bazel/third_party/murmur3/BUILD.bazel | 0 bazel/third_party/murmur3/murmur3.BUILD | 21 + bazel/third_party/natpmp/BUILD.bazel | 0 bazel/third_party/natpmp/natpmp.BUILD | 8 + bazel/third_party/openssl/BUILD.bazel | 0 bazel/third_party/openssl/openssl.BUILD | 43 ++ bazel/third_party/pdjson/BUILD.bazel | 0 bazel/third_party/pdjson/pdjson.BUILD | 16 + bazel/third_party/scrypt/BUILD.bazel | 0 bazel/third_party/scrypt/scrypt.BUILD | 30 + ...e8fb2d1fd8a3d641d7c33705691d41a2a860.patch | 14 + bazel/third_party/secp256k1/BUILD.bazel | 0 bazel/third_party/secp256k1/secp256k1.BUILD | 30 + bazel/third_party/sigsegv/BUILD.bazel | 0 bazel/third_party/sigsegv/sigsegv.BUILD | 31 + ...db33fc1e2130f67c045327b0ec949032df1d.patch | 27 + bazel/third_party/softfloat/BUILD.bazel | 0 bazel/third_party/softfloat/softfloat.BUILD | 637 ++++++++++++++++++ bazel/third_party/sse2neon/BUILD.bazel | 0 bazel/third_party/sse2neon/sse2neon.BUILD | 7 + bazel/third_party/urcrypt/BUILD.bazel | 0 bazel/third_party/urcrypt/urcrypt.BUILD | 28 + bazel/third_party/uv/BUILD.bazel | 0 bazel/third_party/uv/uv.BUILD | 28 + bazel/third_party/whereami/BUILD.bazel | 0 bazel/third_party/whereami/whereami.BUILD | 8 + bazel/third_party/zlib/BUILD.bazel | 0 bazel/third_party/zlib/zlib.BUILD | 19 + bazel/toolchain/BUILD.bazel | 410 +++++++++++ bazel/toolchain/cfg.bzl | 168 +++++ docker/BUILD.bazel | 116 ++++ pkg/c3/BUILD.bazel | 42 ++ pkg/ent/BUILD.bazel | 49 ++ pkg/noun/BUILD.bazel | 117 ++++ pkg/noun/platform/darwin/BUILD.bazel | 8 + pkg/noun/platform/linux/BUILD.bazel | 8 + pkg/ur/BUILD.bazel | 39 ++ pkg/vere/BUILD.bazel | 522 ++++++++++++++ 75 files changed, 4196 insertions(+) create mode 100644 .bazelrc create mode 100644 BUILD.bazel create mode 100644 PACE create mode 100644 VERSION create mode 100644 WORKSPACE.bazel create mode 100644 bazel/BUILD.bazel create mode 100644 bazel/common_settings.bzl create mode 100644 bazel/repo.bzl create mode 100644 bazel/third_party/aes_siv/BUILD.bazel create mode 100644 bazel/third_party/aes_siv/aes_siv.BUILD create mode 100644 bazel/third_party/argon2/BUILD.bazel create mode 100644 bazel/third_party/argon2/argon2.BUILD create mode 100644 bazel/third_party/avahi/BUILD.bazel create mode 100644 bazel/third_party/avahi/avahi.BUILD create mode 100644 bazel/third_party/curl/BUILD.bazel create mode 100644 bazel/third_party/curl/curl.BUILD create mode 100644 bazel/third_party/dbus/BUILD.bazel create mode 100644 bazel/third_party/dbus/dbus.BUILD create mode 100644 bazel/third_party/ed25519/BUILD.bazel create mode 100644 bazel/third_party/ed25519/ed25519.BUILD create mode 100644 bazel/third_party/expat/BUILD.bazel create mode 100644 bazel/third_party/expat/expat.BUILD create mode 100644 bazel/third_party/gmp/BUILD.bazel create mode 100644 bazel/third_party/gmp/gmp.BUILD create mode 100644 bazel/third_party/h2o/2.2.6.patch create mode 100644 bazel/third_party/h2o/BUILD.bazel create mode 100644 bazel/third_party/h2o/h2o.BUILD create mode 100644 bazel/third_party/keccak_tiny/64b6647514212b76ae7bca0dea9b7b197d1d8186.patch create mode 100644 bazel/third_party/keccak_tiny/BUILD.bazel create mode 100644 bazel/third_party/keccak_tiny/keccak_tiny.BUILD create mode 100644 bazel/third_party/libbacktrace/BUILD.bazel create mode 100644 bazel/third_party/libbacktrace/libbacktrace.BUILD create mode 100644 bazel/third_party/libunwind/BUILD.bazel create mode 100644 bazel/third_party/libunwind/libunwind.BUILD create mode 100644 bazel/third_party/lmdb/0.9.29.patch create mode 100644 bazel/third_party/lmdb/BUILD.bazel create mode 100644 bazel/third_party/lmdb/lmdb.BUILD create mode 100644 bazel/third_party/murmur3/BUILD.bazel create mode 100644 bazel/third_party/murmur3/murmur3.BUILD create mode 100644 bazel/third_party/natpmp/BUILD.bazel create mode 100644 bazel/third_party/natpmp/natpmp.BUILD create mode 100644 bazel/third_party/openssl/BUILD.bazel create mode 100644 bazel/third_party/openssl/openssl.BUILD create mode 100644 bazel/third_party/pdjson/BUILD.bazel create mode 100644 bazel/third_party/pdjson/pdjson.BUILD create mode 100644 bazel/third_party/scrypt/BUILD.bazel create mode 100644 bazel/third_party/scrypt/scrypt.BUILD create mode 100644 bazel/third_party/secp256k1/694ce8fb2d1fd8a3d641d7c33705691d41a2a860.patch create mode 100644 bazel/third_party/secp256k1/BUILD.bazel create mode 100644 bazel/third_party/secp256k1/secp256k1.BUILD create mode 100644 bazel/third_party/sigsegv/BUILD.bazel create mode 100644 bazel/third_party/sigsegv/sigsegv.BUILD create mode 100644 bazel/third_party/softfloat/5c06db33fc1e2130f67c045327b0ec949032df1d.patch create mode 100644 bazel/third_party/softfloat/BUILD.bazel create mode 100644 bazel/third_party/softfloat/softfloat.BUILD create mode 100644 bazel/third_party/sse2neon/BUILD.bazel create mode 100644 bazel/third_party/sse2neon/sse2neon.BUILD create mode 100644 bazel/third_party/urcrypt/BUILD.bazel create mode 100644 bazel/third_party/urcrypt/urcrypt.BUILD create mode 100644 bazel/third_party/uv/BUILD.bazel create mode 100644 bazel/third_party/uv/uv.BUILD create mode 100644 bazel/third_party/whereami/BUILD.bazel create mode 100644 bazel/third_party/whereami/whereami.BUILD create mode 100644 bazel/third_party/zlib/BUILD.bazel create mode 100644 bazel/third_party/zlib/zlib.BUILD create mode 100644 bazel/toolchain/BUILD.bazel create mode 100644 bazel/toolchain/cfg.bzl create mode 100644 docker/BUILD.bazel create mode 100644 pkg/c3/BUILD.bazel create mode 100644 pkg/ent/BUILD.bazel create mode 100644 pkg/noun/BUILD.bazel create mode 100644 pkg/noun/platform/darwin/BUILD.bazel create mode 100644 pkg/noun/platform/linux/BUILD.bazel create mode 100644 pkg/ur/BUILD.bazel create mode 100644 pkg/vere/BUILD.bazel diff --git a/.bazelrc b/.bazelrc new file mode 100644 index 0000000000..2a72ab5654 --- /dev/null +++ b/.bazelrc @@ -0,0 +1,51 @@ +# Enable configurations specific to the host platform. +common --enable_platform_specific_config + +# Disallow empty `glob()`s. +build --incompatible_disallow_empty_glob + +# Don't auto-detect the C/C++ toolchain. +build --action_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1 + +# Use platforms to select toolchains. +# See https://bazel.build/concepts/platforms#cxx and +# https://github.com/bazelbuild/bazel/issues/7260. +build --incompatible_enable_cc_toolchain_resolution + +# Disable transitions. +# See https://github.com/bazelbuild/rules_docker/issues/2052. +build --@io_bazel_rules_docker//transitions:enable=false + +# Add aliases for compiler version build settings. +build --flag_alias=clang_version=//:clang_version +build --flag_alias=gcc_version=//:gcc_version + +# Use optimized build by default. According to the bazel documentation, this +# corresponds to -O2 -DNDEBUG, but these are overriden in +# //bazel/common_settings.bzl:vere_library. +# https://bazel.build/docs/user-manual#build-semantics +build --compilation_mode=opt + +# Don't include source level debug info on macOS. See +# https://github.com/urbit/urbit/issues/5561 and +# https://github.com/urbit/vere/issues/131. +build:linux --host_copt='-g' +build --strip=never + +# Turn on optimization, CPU and memory debug for exec config, which we only use +# to run the fake ship tests. Also turn on extra snapshot validation. +build --host_copt='-O3' +build --host_copt='-DU3_CPU_DEBUG' +build --host_copt='-DU3_MEMORY_DEBUG' +build --host_copt='-DC3DBG' +build --host_copt='-DU3_SNAPSHOT_VALIDATION' + +# Set as per-file copts as a plain --copt gets passed to third party +# dependencies which forces recompilation (slow) if you are switching between +# including/excluding a symbol define. +build:mem_dbg --per_file_copt='pkg/.*@-DU3_MEMORY_DEBUG' +build:cpu_dbg --per_file_copt='pkg/.*@-DU3_CPU_DEBUG' +build:snp_dbg --per_file_copt='pkg/.*@-DU3_SNAPSHOT_VALIDATION' + +# Any personal configuration should go in .user.bazelrc. +try-import %workspace%/.user.bazelrc diff --git a/BUILD.bazel b/BUILD.bazel new file mode 100644 index 0000000000..730fe0ec81 --- /dev/null +++ b/BUILD.bazel @@ -0,0 +1,214 @@ +load("//bazel:common_settings.bzl", "string_flag") +exports_files(["PACE"]) + +# +# OS-CPU CONFIG SETTINGS +# + +config_setting( + name = "linux_aarch64", + constraint_values = [ + "@platforms//os:linux", + "@platforms//cpu:aarch64", + ], +) + +config_setting( + name = "linux_x86_64", + constraint_values = [ + "@platforms//os:linux", + "@platforms//cpu:x86_64", + ], +) + +config_setting( + name = "macos_aarch64", + constraint_values = [ + "@platforms//os:macos", + "@platforms//cpu:aarch64", + ], +) + +config_setting( + name = "macos_x86_64", + constraint_values = [ + "@platforms//os:macos", + "@platforms//cpu:x86_64", + ], +) + +# +# CONFIGS DETAILING WHEN TO ENABLE CERTAIN FEATURES BY DEFAULT. +# CHANGES BEHAVIOR OF //bazel/common_settings.bzl:vere_library. +# + +config_setting( + name = "thinlto", + constraint_values = [ + "@platforms//os:macos", + ], + values = { + "compilation_mode": "opt" + } +) + +config_setting( + name = "lto", + constraint_values = [ + "@platforms//os:linux", + ], + values = { + "compilation_mode": "opt" + } +) + +config_setting( + name = "debug", + values = { + "compilation_mode": "dbg" + } +) + +# +# COMPILERS +# + +# Version flag for clang. +string_flag( + name = "clang_version", + # macOS uses `clang-15.0.0` by default. + build_setting_default = "15.0.0", + visibility = ["//visibility:public"], +) + +# Version flag for gcc. +string_flag( + name = "gcc_version", + # musl-cross-make uses `gcc-11.4.0` by default. + build_setting_default = "11.4.0", + visibility = ["//visibility:public"], +) + +# +# PLATFORMS +# +# A platform takes one of two formats: `--` or `-` +# if the compiler is unspecified. + +# Linux aarch64 platform with unspecified compiler. +platform( + name = "linux-aarch64", + constraint_values = [ + "@platforms//os:linux", + "@platforms//cpu:aarch64", + ], +) + +# Linux x86_64 platform with unspecified compiler. +platform( + name = "linux-x86_64", + constraint_values = [ + "@platforms//os:linux", + "@platforms//cpu:x86_64", + ], +) + +# Linux x86_64 platform with gcc. +platform( + name = "gcc-linux-x86_64", + constraint_values = [ + ":gcc", + "@platforms//os:linux", + "@platforms//cpu:x86_64", + ], +) + +platform( + name = "macos-aarch64", + constraint_values = [ + "@platforms//os:macos", + "@platforms//cpu:aarch64", + ], +) + +platform( + name = "clang-macos-aarch64", + constraint_values = [ + ":clang", + "@platforms//os:macos", + "@platforms//cpu:aarch64", + ], +) + +platform( + name = "macos-x86_64", + constraint_values = [ + "@platforms//os:macos", + "@platforms//cpu:x86_64", + ], +) + +platform( + name = "clang-macos-x86_64", + constraint_values = [ + ":clang", + "@platforms//os:macos", + "@platforms//cpu:x86_64", + ], +) + +# Produce the `U3_VERE_PACE` macro. +genrule( + name = "pace_hdr", + srcs = ["PACE"], + outs = ["pace.h"], + cmd = """ + echo '#ifndef URBIT_PACE_H' > $@ + echo '#define URBIT_PACE_H' >> $@ + printf '#define U3_VERE_PACE "%s"\n' $$(cat $< | tr -d '\n') >> $@ + echo '#endif' >> $@ + """, + visibility = ["//:__subpackages__"], +) + +genrule( + name = "version_str", + srcs = [ + # Bazel warns dependency checking directories is unsound, but there's no + # apparent way around depending on .git if we want to run git commands. + ".git", + "PACE", + "VERSION", + ], + outs = ["version"], + cmd = """ + version_num=$$(cat $(execpath VERSION) | tr -d '\n') + commit_sha=$$(git rev-parse --short HEAD) + if [ $$(tr -d '\n' < $(execpath PACE)) == "live" ]; then + version="$$version_num" + else + version="$$version_num-$$commit_sha" + fi + echo -n $$version > $@ + """, + visibility = ["//:__subpackages__"], +) + +# Produce the `URBIT_VERSION` macro. +genrule( + name = "version_hdr", + srcs = [":version_str"], + outs = ["version.h"], + cmd = """ + echo '#ifndef URBIT_VERSION_H' > $@ + echo '#define URBIT_VERSION_H' >> $@ + printf '#define URBIT_VERSION "%s"\n' $$(cat $< | tr -d '\n') >> $@ + echo '#endif' >> $@ + """, + visibility = ["//:__subpackages__"], +) + +alias( + name = "urbit", + actual = "//pkg/vere:urbit", +) diff --git a/PACE b/PACE new file mode 100644 index 0000000000..b2d1a7762f --- /dev/null +++ b/PACE @@ -0,0 +1 @@ +once diff --git a/VERSION b/VERSION new file mode 100644 index 0000000000..8c50098d8a --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +3.1 diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel new file mode 100644 index 0000000000..e12aa50f5d --- /dev/null +++ b/WORKSPACE.bazel @@ -0,0 +1,448 @@ +# BUMPING A DEPENDENCY VERSION +# +# The general process for bumping the version of a dependency is as follows: +# +# (1) Update the `version` attribute in the dependency's repository rule (likely +# `versioned_http_archive()` or `versioned_http_file()`. +# (2) Download the source code of the new version of the dependency using +# the specified `url` attribute (substituting `{version}` with the actual +# version you're updating to in the URL). +# (3) Compute the SHA-256 hash of the new version's tarball (or zip archive) +# using `openssl`: +# ``` +# $ openssl dgst -sha256 +# ``` +# (4) Update the `sha256` attribute in the dependency's repository rule with the +# SHA-256 hash from (3). +# (5) Run `bazel clean` to ensure that Bazel removes the old version of the +# dependency. +# +# If a dependency has documention specific to that dependency that conflicts +# with the process described above, adhere to the dependency-specific +# documentation. +# +# UPDATING A DEPENDENCY'S BUILD FILE +# +# It's unlikely that you'll need to update a dependency's build file, but if you +# do, you presumably know what you're doing. Nonetheless, here are a few useful +# bits of non-obvious information: +# +# - Build logic for a dependency resides within its `.BUILD` file. +# `BUILD.bazel` is also present in the dependency's directory in +# `third_party/` to mark the directory as a package. Ideally, the logic in +# `.BUILD` would live in `BUILD.bazel`, but Bazel 5.3.1 cannot +# find the source files of the dependency when `BUILD.bazel` holds the build +# logic (at least on my machine - ThinkPad X1 Carbon Gen 9, Arch Linux). +# - Whenever possible, avoid delegating to foreign build systems. Delegating is +# inevitable when a third party dependency has a particularly complicated +# build system that would be too difficult to replicate in Bazel (i.e. +# OpenSSL). However, delegate only as a last resort. +# +# PATCHING A DEPENDENCY +# +# If a dependency has a bug that prevents it from being used in the project, +# you can patch the dependency to fix it. We recommend the process below: +# +# (1) Make a change to the source of the dependency. If the change is +# platform-specific, wrap it in the appropriate URBIT_RUNTIME_* macro. +# We "gate" source patches with these macros so that we only ever need +# at most one patch per dependency. +# (a) CPU architecture macros: `URBIT_RUNTIME_CPU_{X86_64,AARCH64}`. +# (b) OS macros: `URBIT_RUNTIME_OS_{LINUX,DARWIN,BSD,MINGW}`. +# (2) Generate a patch file named .patch and save it in +# `bazel/third_party/`. +# (3) Update the dependency's targets to locally define the platform-specific +# patch macro(s). `select()` statements should be used to define the +# correct macro for the current platform. +# (4) Update the dependency's repository rule in WORKSPACE.bazel to include +# `patch_args = ["-p1"]` (assuming the patch was generated with `git diff`) +# and `patches = ["//bazel/third_party/:.patch"]`. + +load("//bazel:repo.bzl", "versioned_http_archive", "versioned_http_file") + +# +# RULES REPOSITORIES +# + +versioned_http_archive( + name = "bazel_skylib", + sha256 = "74d544d96f4a5bb630d465ca8bbcfe231e3594e5aae57e1edbf17a6eb3ca2506", + url = "https://github.com/bazelbuild/bazel-skylib/releases/download/{version}/bazel-skylib-{version}.tar.gz", + version = "1.3.0", +) + +versioned_http_archive( + name = "rules_cc", + sha256 = "af6cc82d87db94585bceeda2561cb8a9d55ad435318ccb4ddfee18a43580fb5d", + strip_prefix = "rules_cc-{version}", + url = "https://github.com/bazelbuild/rules_cc/releases/download/{version}/rules_cc-{version}.tar.gz", + version = "0.0.4", +) + +versioned_http_archive( + name = "rules_foreign_cc", + sha256 = "2a4d07cd64b0719b39a7c12218a3e507672b82a97b98c6a89d38565894cf7c51", + strip_prefix = "rules_foreign_cc-{version}", + url = "https://github.com/bazelbuild/rules_foreign_cc/archive/refs/tags/{version}.tar.gz", + version = "0.9.0", +) + +# +# TOOLCHAINS SETUP +# + +# Use the toolchains we've configured. +register_toolchains( + "//bazel/toolchain:gcc-linux-aarch64-toolchain", + "//bazel/toolchain:clang-macos-aarch64-toolchain", + "//bazel/toolchain:clang-macos-x86_64-toolchain", + "//bazel/toolchain:gcc-linux-x86_64-toolchain", +) + +load("@rules_foreign_cc//foreign_cc:repositories.bzl", "rules_foreign_cc_dependencies") + +# See https://bazelbuild.github.io/rules_foreign_cc/0.9.0/flatten.html#rules_foreign_cc_dependencies. +rules_foreign_cc_dependencies( + register_built_tools = False, + register_default_tools = False, + register_preinstalled_tools = True, +) + +# +# THIRD PARTY DEPENDENCIES +# + +versioned_http_archive( + name = "aes_siv", + build_file = "//bazel/third_party/aes_siv:aes_siv.BUILD", + sha256 = "1916a428dff480e06b09dc0fb1c9d849c048f838dc9b8d141452233b508f6bb1", + strip_prefix = "libaes_siv-{version}", + url = "https://github.com/dfoxfranke/libaes_siv/archive/{version}.tar.gz", + version = "9681279cfaa6e6399bb7ca3afbbc27fc2e19df4b", +) + +versioned_http_archive( + name = "avahi", + build_file = "//bazel/third_party/avahi:avahi.BUILD", + sha256 = "060309d7a333d38d951bc27598c677af1796934dbd98e1024e7ad8de798fedda", + strip_prefix = "avahi-{version}", + url = "https://github.com/lathiat/avahi/releases/download/v{version}/avahi-{version}.tar.gz", + version = "0.8", +) + +versioned_http_archive( + name = "bazel_gazelle", + sha256 = "efbbba6ac1a4fd342d5122cbdfdb82aeb2cf2862e35022c752eaddffada7c3f3", + url = "https://github.com/bazelbuild/bazel-gazelle/releases/download/v{version}/bazel-gazelle-v{version}.tar.gz", + version = "0.27.0", +) + +# See https://curl.se/docs/caextract.html. +versioned_http_file( + name = "ca_bundle", + sha256 = "2cff03f9efdaf52626bd1b451d700605dc1ea000c5da56bd0fc59f8f43071040", + url = "https://curl.se/ca/cacert-{version}.pem", + version = "2022-10-11", +) + +versioned_http_archive( + name = "curl", + build_file = "//bazel/third_party/curl:curl.BUILD", + sha256 = "78a06f918bd5fde3c4573ef4f9806f56372b32ec1829c9ec474799eeee641c27", + strip_prefix = "curl-{version}", + url = "https://curl.se/download/curl-{version}.tar.gz", + version = "7.85.0", +) + +versioned_http_archive( + name = "dbus", + build_file = "//bazel/third_party/dbus:dbus.BUILD", + sha256 = "a6bd5bac5cf19f0c3c594bdae2565a095696980a683a0ef37cb6212e093bde35", + strip_prefix = "dbus-{version}", + url = "https://src.fedoraproject.org/repo/pkgs/dbus/dbus-{version}.tar.xz/sha512/f3dfc73da28cbe20449d15bbe4166c3574f0e551dfd15fca7cce2b8c71e778360ed2dd391ee5c414a7a47ff4b958727b26ef4cabfee70564f8d0a34bf5ad2386/dbus-1.14.8.tar.xz", + version = "1.14.8", +) + +versioned_http_archive( + name = "expat", + build_file = "//bazel/third_party/expat:expat.BUILD", + strip_prefix = "expat-{version}", + sha256 = "ef2420f0232c087801abf705e89ae65f6257df6b7931d37846a193ef2e8cdcbe", + # TODO: fix the R_2_5_0 nonsense + url = "https://github.com/libexpat/libexpat/releases/download/R_2_5_0/expat-{version}.tar.xz", + version = "2.5.0", +) + +versioned_http_archive( + name = "gmp", + build_file = "//bazel/third_party/gmp:gmp.BUILD", + sha256 = "fb776e19e0267053c3d6ffef0dcc001fb441da0e2b5224cfd9656aa2f1b8614d", + strip_prefix = "GMP-{version}", + url = "https://github.com/alisw/GMP/archive/refs/tags/v{version}.tar.gz", + version = "6.2.1", +) + +versioned_http_archive( + name = "h2o", + build_file = "//bazel/third_party/h2o:h2o.BUILD", + patch_args = ["-p1"], + patches = ["//bazel/third_party/h2o:{version}.patch"], + sha256 = "f8cbc1b530d85ff098f6efc2c3fdbc5e29baffb30614caac59d5c710f7bda201", + strip_prefix = "h2o-{version}", + url = "https://github.com/h2o/h2o/archive/refs/tags/v{version}.tar.gz", + # When bumping the version, compare `CMakeLists.txt` in the `h2o` repo to + # {build_file} and confirm that {build_file} remains an accurate description + # of the h2o build process. + version = "2.2.6", +) + +versioned_http_archive( + name = "io_bazel_rules_docker", + sha256 = "b1e80761a8a8243d03ebca8845e9cc1ba6c82ce7c5179ce2b295cd36f7e394bf", + url = "https://github.com/bazelbuild/rules_docker/releases/download/v{version}/rules_docker-v{version}.tar.gz", + version = "0.25.0", +) + +versioned_http_archive( + name = "io_bazel_rules_go", + sha256 = "d6ab6b57e48c09523e93050f13698f708428cfd5e619252e369d377af6597707", + url = "https://github.com/bazelbuild/rules_go/releases/download/v{version}/rules_go-v{version}.zip", + version = "0.43.0", +) + +versioned_http_file( + name = "ivory_pill", + sha256 = "26ff86808886beb831e4a135f478e42ce83ef4a09ad24808b3fe97248ce7a6b7", + url = "https://github.com/urbit/urbit/blob/{version}/bin/ivory.pill?raw=true", + version = "721fa05", +) + +versioned_http_archive( + name = "natpmp", + build_file = "//bazel/third_party/natpmp:natpmp.BUILD", + sha256 = "0684ed2c8406437e7519a1bd20ea83780db871b3a3a5d752311ba3e889dbfc70", + strip_prefix = "libnatpmp-{version}", + url = "http://download.openpkg.org/components/cache/libnatpmp/libnatpmp-{version}.tar.gz", + version = "20230423", +) + +versioned_http_file( + name = "solid_pill", + sha256 = "8b658fcee6978e2b19004a54233cab953e77ea0bb6c3a04d1bfda4ddc6be63c5", + url = "https://github.com/urbit/urbit/raw/{version}/bin/solid.pill", + version = "255fb1ca8206072f1d09425f0db61ecfe7ff5b17", +) + +versioned_http_archive( + name = "libbacktrace", + build_file = "//bazel/third_party/libbacktrace:libbacktrace.BUILD", + sha256 = "609c17352ec38eaf5ff6618fcbfb38cd8fa0e94a15a0d9aa259df514bbf47fcd", + url = "https://github.com/ianlancetaylor/libbacktrace/archive/{version}.tar.gz", + strip_prefix = "libbacktrace-{version}", + version = "4ead348bb45f753121ca0bd44170ff8352d4c514", +) + +versioned_http_archive( + name = "libunwind", + build_file = "//bazel/third_party/libunwind:libunwind.BUILD", + sha256 = "ddf0e32dd5fafe5283198d37e4bf9decf7ba1770b6e7e006c33e6df79e6a6157", + url = "https://github.com/libunwind/libunwind/releases/download/v1.8.1/libunwind-1.8.1.tar.gz", + strip_prefix = "libunwind-{version}", + version = "1.8.1", +) + +versioned_http_archive( + name = "lmdb", + build_file = "//bazel/third_party/lmdb:lmdb.BUILD", + patch_args = ["-p1"], + patches = ["//bazel/third_party/lmdb:{version}.patch"], + sha256 = "22054926b426c66d8f2bc22071365df6e35f3aacf19ad943bc6167d4cae3bebb", + strip_prefix = "lmdb-LMDB_{version}/libraries/liblmdb", + url = "https://github.com/LMDB/lmdb/archive/refs/tags/LMDB_{version}.tar.gz", + # When bumping the version, compare `libraries/liblmdb/Makefile` in the + # `lmdb` repo to {build_file} and confirm that {build_file} remains an + # accurate description of the lmdb build process. + version = "0.9.29", +) + +versioned_http_archive( + name = "murmur3", + build_file = "//bazel/third_party/murmur3:murmur3.BUILD", + sha256 = "d81836605204df2db9e0c095423b2856073d1b2ef900463151d0663b7ca3164f", + strip_prefix = "murmur3-{version}", + url = "https://github.com/PeterScott/murmur3/archive/{version}.tar.gz", + # When bumping the version, compare `makefile` in the `murmur3` repo to + # {build_file} and confirm that {build_file} remains an accurate description + # of the murmur3 build process. + version = "dae94be0c0f54a399d23ea6cbe54bca5a4e93ce4", +) + +versioned_http_archive( + name = "openssl", + build_file = "//bazel/third_party/openssl:openssl.BUILD", + sha256 = "cf3098950cb4d853ad95c0841f1f9c6d3dc102dccfcacd521d93925208b76ac8", + strip_prefix = "openssl-{version}", + url = "https://github.com/openssl/openssl/releases/download/OpenSSL_1_1_1w/openssl-{version}.tar.gz", + version = "1.1.1w", +) + +versioned_http_archive( + name = "pdjson", + build_file = "//bazel/third_party/pdjson:pdjson.BUILD", + sha256 = "928913d44d9021d69c0d23a8f59ed67028e5abf1ae7910f79c23a7af4e779b92", + strip_prefix = "pdjson-{version}", + url = "https://github.com/skeeto/pdjson/archive/{version}.tar.gz", + # When bumping the version, compare `Makefile` in the `pdjson` repo to + # {build_file} and confirm that {build_file} remains an accurate description + # of the pdjson build process. + version = "67108d883061043e55d0fb13961ac1b6fc8a485c", +) + +versioned_http_archive( + name = "secp256k1", + build_file = "//bazel/third_party/secp256k1:secp256k1.BUILD", + sha256 = "5f6e4a66bf8f3c318d91eacbf3262d1cd81a3fda6bb9af267b54cf38ffd44b1c", + strip_prefix = "secp256k1-{version}", + url = "https://github.com/bitcoin-core/secp256k1/archive/{version}.tar.gz", + version = "694ce8fb2d1fd8a3d641d7c33705691d41a2a860", + patch_args = ["-p1"], + patches = ["//bazel/third_party/secp256k1:{version}.patch"], +) + +versioned_http_archive( + name = "sigsegv", + build_file = "//bazel/third_party/sigsegv:sigsegv.BUILD", + sha256 = "cdac3941803364cf81a908499beb79c200ead60b6b5b40cad124fd1e06caa295", + strip_prefix = "libsigsegv-{version}", + url = "https://ftp.gnu.org/gnu/libsigsegv/libsigsegv-{version}.tar.gz", + version = "2.14", +) + +versioned_http_archive( + name = "softfloat", + build_file = "//bazel/third_party/softfloat:softfloat.BUILD", + patch_args = ["-p1"], + patches = ["//bazel/third_party/softfloat:{version}.patch"], + sha256 = "15ad5841e88fe09422a8e31a0ef3fe126ecf678f52c9a3882f3373d47752aebe", + strip_prefix = "berkeley-softfloat-3-{version}", + url = "https://github.com/ucb-bar/berkeley-softfloat-3/archive/{version}.tar.gz", + # When bumping the version, compare `build//Makefile` in the + # `softfloat` repo to {build_file} and compare that {build_file} remains an + # accurate description of the softfloat process for *all* supported + # ``s. + version = "5c06db33fc1e2130f67c045327b0ec949032df1d", +) + +versioned_http_archive( + name = "sse2neon", + build_file = "//bazel/third_party/sse2neon:sse2neon.BUILD", + sha256 = "4001e2dfb14fcf3831211581ed83bcc83cf6a3a69f638dcbaa899044a351bb2a", + strip_prefix = "sse2neon-{version}", + url = "https://github.com/DLTcollab/sse2neon/archive/refs/tags/v{version}.tar.gz", + version = "1.5.1", +) + +versioned_http_file( + name = "urbit", + sha256 = "a729d8b3c438fef33f5ae8c4da8d84ebdb5af5ad028d41696a92cddc8bf362a8", + url = "https://github.com/urbit/urbit/archive/{version}.tar.gz", + # We can't use a branch name for the `version` because each new commit + # will change the SHA256 hash. + version = "ea8fee3aa0434d4bdf1bf785e5ec346c7ecba7fd", +) + +versioned_http_archive( + name = "urcrypt", + build_file = "//bazel/third_party/urcrypt:urcrypt.BUILD", + sha256 = "d27ec04d3854da7c479dd815af92ffef986616bc7ff400022e2dfb7971853d86", + strip_prefix = "urcrypt-{version}", + url = "https://github.com/urbit/urcrypt/archive/{version}.tar.gz", + version = "9ae5d604528bc54ae48430f55ebbb17b1ad7956c", +) + +versioned_http_archive( + name = "uv", + build_file = "//bazel/third_party/uv:uv.BUILD", + sha256 = "ccfcdc968c55673c6526d8270a9c8655a806ea92468afcbcabc2b16040f03cb4", + strip_prefix = "libuv-v{version}", + url = "https://dist.libuv.org/dist/v{version}/libuv-v{version}.tar.gz", + version = "1.44.2", +) + +versioned_http_archive( + name = "whereami", + build_file = "//bazel/third_party/whereami:whereami.BUILD", + sha256 = "1d8744177f37e8386ec2f6c5992592399040cb93535ed4fd253e1976f889a744", + strip_prefix = "whereami-{version}", + url = "https://github.com/gpakosz/whereami/archive/{version}.tar.gz", + version = "ba364cd54fd431c76c045393b6522b4bff547f50", +) + +versioned_http_archive( + name = "zlib", + build_file = "//bazel/third_party/zlib:zlib.BUILD", + sha256 = "9a93b2b7dfdac77ceba5a558a580e74667dd6fede4585b91eefb60f03b72df23", + strip_prefix = "zlib-{version}", + url = "https://www.zlib.net/zlib-{version}.tar.gz", + version = "1.3.1", +) + +# +# HEDRON COMPILE COMMANDS SETUP +# + +# Hedron's Compile Commands Extractor for Bazel +# https://github.com/hedronvision/bazel-compile-commands-extractor +versioned_http_archive( + name = "hedron_compile_commands", + strip_prefix = "bazel-compile-commands-extractor-{version}", + sha256 = "d7ba7708816132f86f02864b9dba0c5abf249cc0fb035a34c430e4e538c87867", + url = "https://github.com/hedronvision/bazel-compile-commands-extractor/archive/{version}.tar.gz", + version = "d3afb5dfadd4beca48bb027112d029f2d34ff0a0", +) + +load("@hedron_compile_commands//:workspace_setup.bzl", "hedron_compile_commands_setup") + +hedron_compile_commands_setup() + +# +# DOCKER SETUP +# + +# These must be loaded before the Docker rules. +# See https://github.com/bazelbuild/rules_docker/issues/2075#issuecomment-1115954091. +load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies") +load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies") + +go_rules_dependencies() + +go_register_toolchains(version = "1.18") + +gazelle_dependencies(go_repository_default_config = "//:WORKSPACE.bazel") + +load( + "@io_bazel_rules_docker//repositories:repositories.bzl", + _container_repositories = "repositories", +) +load("@io_bazel_rules_docker//repositories:deps.bzl", _container_deps = "deps") +load("@io_bazel_rules_docker//cc:image.bzl", _cc_image_repos = "repositories") + +_container_repositories() + +_container_deps( + # See https://github.com/bazelbuild/rules_docker/issues/1902. + go_repository_default_config = "@//:WORKSPACE.bazel", +) + +_cc_image_repos() + +load("@io_bazel_rules_docker//container:container.bzl", "container_pull") + +container_pull( + name = "alpine_linux_x86_64", + timeout = 300, # [seconds] + architecture = "amd64", + digest = "sha256:93d5a28ff72d288d69b5997b8ba47396d2cbb62a72b5d87cd3351094b5d578a0", + registry = "docker.io", + repository = "alpine", +) diff --git a/bazel/BUILD.bazel b/bazel/BUILD.bazel new file mode 100644 index 0000000000..5ac74d0b96 --- /dev/null +++ b/bazel/BUILD.bazel @@ -0,0 +1,21 @@ +load("@hedron_compile_commands//:refresh_compile_commands.bzl", "refresh_compile_commands") + +# gazelle:exclude external + +refresh_compile_commands( + name = "refresh_compile_commands", + + # Specify the targets of interest. + # For example, specify a dict of targets and any flags required to build. + targets = [ + "//pkg/ent", + "//pkg/noun", + "//pkg/ur", + "//pkg/vere:urbit", + ], + # No need to add flags already in .bazelrc. They're automatically picked up. + # If you don't need flags, a list of targets is also okay, as is a single target string. + # Wildcard patterns, like //... for everything, *are* allowed here, just like a build. + # As are additional targets (+) and subtractions (-), like in bazel query https://docs.bazel.build/versions/main/query.html#expressions + # And if you're working on a header-only library, specify a test or binary target that compiles it. +) diff --git a/bazel/common_settings.bzl b/bazel/common_settings.bzl new file mode 100644 index 0000000000..9ec74beda5 --- /dev/null +++ b/bazel/common_settings.bzl @@ -0,0 +1,50 @@ +# For more, see https://bazel.build/extending/config and +# https://github.com/bazelbuild/bazel-skylib/blob/main/rules/common_settings.bzl. +load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo") + +def _impl(ctx): + return BuildSettingInfo(value = ctx.build_setting_value) + +string_flag = rule( + implementation = _impl, + build_setting = config.string(flag = True), + doc = "A string-typed build setting that can be set on the command line", +) + +def vere_library(copts = [], linkopts = [], **kwargs): + native.cc_library( + copts = copts + [ + "-fno-omit-frame-pointer", + ] + select({ + "//:debug": ["-O0", "-g3", "-DC3DBG", "-fdebug-compilation-dir=."], + "//conditions:default": ["-O3", "-g"] + }) + select({ + "//:lto": ['-flto'], + "//conditions:default": [] + }), + linkopts = linkopts + ['-g'] + select({ + "//:lto": ['-flto'], + "//:thinlto": ['-flto=thin'], + "//conditions:default": [] + }), + **kwargs, + ) + +def vere_binary(copts = [], linkopts = [], **kwargs): + native.cc_binary( + copts = copts + [ + "-fno-omit-frame-pointer", + ] + select({ + "//:debug": ["-O0", "-g3", "-DC3DBG", "-fdebug-compilation-dir=."], + "//conditions:default": ["-O3", "-g"] + }) + select({ + "//:lto": ['-flto'], + "//conditions:default": [] + }), + linkopts = linkopts + ['-g'] + select({ + "//:lto": ['-flto'], + "//:thinlto": ['-flto=thin'], + "//conditions:default": [] + }), + **kwargs, + ) diff --git a/bazel/repo.bzl b/bazel/repo.bzl new file mode 100644 index 0000000000..2c2eb1bb1f --- /dev/null +++ b/bazel/repo.bzl @@ -0,0 +1,34 @@ +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive", "http_file") +load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") + +# Adds a `version` attribute to `http_archive`. +def versioned_http_archive(name, version, **kwargs): + if kwargs.get("url") != None: + kwargs["url"] = kwargs["url"].format(version = version) + + if kwargs.get("urls") != None: + for index, url in enumerate(kwargs["urls"]): + kwargs["urls"][index] = url.format(version = version) + + if kwargs.get("patches") != None: + for index, patch in enumerate(kwargs["patches"]): + kwargs["patches"][index] = patch.format(version = version) + + if kwargs.get("strip_prefix") != None: + kwargs["strip_prefix"] = kwargs["strip_prefix"].format(version = version) + + maybe(http_archive, name, **kwargs) + +# Adds a `version` attribute to `http_file`. +def versioned_http_file(name, version, **kwargs): + if kwargs.get("url") != None: + kwargs["url"] = kwargs["url"].format(version = version) + + if kwargs.get("urls") != None: + for index, url in enumerate(kwargs["urls"]): + kwargs["urls"][index] = url.format(version = version) + + if kwargs.get("strip_prefix") != None: + kwargs["strip_prefix"] = kwargs["strip_prefix"].format(version = version) + + maybe(http_file, name, **kwargs) diff --git a/bazel/third_party/aes_siv/BUILD.bazel b/bazel/third_party/aes_siv/BUILD.bazel new file mode 100644 index 0000000000..e69de29bb2 diff --git a/bazel/third_party/aes_siv/aes_siv.BUILD b/bazel/third_party/aes_siv/aes_siv.BUILD new file mode 100644 index 0000000000..1cb6d7469f --- /dev/null +++ b/bazel/third_party/aes_siv/aes_siv.BUILD @@ -0,0 +1,21 @@ +genrule( + name = "config", + srcs = ["config.h.in"], + outs = ["config.h"], + # The options in config.h.in only affect libaes_siv's testing behavior, so + # it's sufficient to generate an empty config.h. + cmd = "touch $@", +) + +cc_library( + name = "aes_siv", + srcs = [ + "aes_siv.c", + "config.h", + ], + hdrs = ["aes_siv.h"], + copts = ["-O3"], + includes = ["."], + visibility = ["//visibility:public"], + deps = ["@openssl"], +) diff --git a/bazel/third_party/argon2/BUILD.bazel b/bazel/third_party/argon2/BUILD.bazel new file mode 100644 index 0000000000..e69de29bb2 diff --git a/bazel/third_party/argon2/argon2.BUILD b/bazel/third_party/argon2/argon2.BUILD new file mode 100644 index 0000000000..ebfc7d5f3c --- /dev/null +++ b/bazel/third_party/argon2/argon2.BUILD @@ -0,0 +1,42 @@ +cc_library( + name = "argon2", + srcs = [ + "src/argon2.c", + "src/blake2/blake2-impl.h", + "src/blake2/blake2b.c", + "src/core.c", + "src/core.h", + "src/encoding.c", + "src/encoding.h", + "src/thread.c", + "src/thread.h", + ] + select({ + # `opt.c` requires SSE instructions. See `Makefile` in the `argon2` repo + # for the check used to determine whether to use `opt.c` or `ref.c`. + "@platforms//cpu:x86_64": [ + "src/blake2/blamka-round-opt.h", + "src/opt.c", + ], + "//conditions:default": [ + "src/blake2/blamka-round-ref.h", + "src/ref.c", + ], + }), + hdrs = [ + "include/argon2.h", + "src/blake2/blake2.h", + ], + copts = [ + "-std=c89", + "-O3", + "-Wall", + "-g", + "-Isrc", + "-DARGON2_NO_THREADS", + ], + includes = [ + "include", + "src/blake2", + ], + visibility = ["//visibility:public"], +) diff --git a/bazel/third_party/avahi/BUILD.bazel b/bazel/third_party/avahi/BUILD.bazel new file mode 100644 index 0000000000..e69de29bb2 diff --git a/bazel/third_party/avahi/avahi.BUILD b/bazel/third_party/avahi/avahi.BUILD new file mode 100644 index 0000000000..2c0896f32d --- /dev/null +++ b/bazel/third_party/avahi/avahi.BUILD @@ -0,0 +1,30 @@ +load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make") + +filegroup( + name = "all", + srcs = glob(["**"]), +) + +cc_library( + name = "dns-sd", + hdrs = ["dns_sd.h"], + visibility = ["//visibility:public"], +) + +configure_make( + name = "avahi", + args = select({ + "@platforms//os:macos": ["--jobs=`sysctl -n hw.logicalcpu`"], + "//conditions:default": ["--jobs=`nproc`"], + }), + configure_options = ["--with-dbus-system-address='unix:path=/var/run/dbus/system_bus_socket' --with-xml=none --disable-libevent --disable-glib --disable-gobject --disable-gdbm --disable-qt3 --disable-qt4 --disable-qt5 --disable-gtk --disable-gtk3 --disable-mono --disable-monodoc --disable-python --disable-libdaemon --enable-compat-libdns_sd --disable-rpath --with-distro=none"], + lib_source = ":all", + # out_include_dir = "avahi-compat-libdns_sd", + deps = ["@dbus"], + configure_in_place = True, + autogen = True, + autoconf = True, + autogen_command = "bootstrap.sh", + out_static_libs = ["libdns_sd.a", "libavahi-client.a", "libavahi-common.a"], + visibility = ["//visibility:public"], +) diff --git a/bazel/third_party/curl/BUILD.bazel b/bazel/third_party/curl/BUILD.bazel new file mode 100644 index 0000000000..e69de29bb2 diff --git a/bazel/third_party/curl/curl.BUILD b/bazel/third_party/curl/curl.BUILD new file mode 100644 index 0000000000..c66424d52e --- /dev/null +++ b/bazel/third_party/curl/curl.BUILD @@ -0,0 +1,67 @@ +load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make") + +filegroup( + name = "all", + srcs = glob(["**"]), +) + +configure_make( + name = "curl", + args = select({ + "@platforms//os:macos": ["--jobs=`sysctl -n hw.logicalcpu`"], + "//conditions:default": ["--jobs=`nproc`"], + }), + # We disable unneeded features. + # TODO: double check that all disabled features below are in fact unneeded. + configure_options = [ + "--disable-alt-svc", + "--disable-ares", + "--disable-cookies", + "--disable-crypto-auth", + "--disable-dateparse", + "--disable-dnsshuffle", + "--disable-doh", + "--disable-get-easy-options", + "--disable-hsts", + "--disable-http-auth", + "--disable-ipv6", + "--disable-ldap", + "--disable-libcurl-option", + "--disable-manual", + "--disable-shared", + "--disable-netrc", + "--disable-ntlm-wb", + "--disable-progress-meter", + "--disable-proxy", + "--disable-pthreads", + "--disable-socketpair", + "--disable-threaded-resolver", + "--disable-tls-srp", + "--disable-unix-sockets", + "--disable-verbose", + "--disable-versioned-symbols", + "--enable-static", + # Use our openssl, not the system's openssl. + "--with-openssl=$URBIT_RUNTIME_OPENSSL", + "--without-brotli", + "--without-libidn2", + "--without-libpsl", + "--without-librtmp", + "--without-nghttp2", + "--without-ngtcp2", + "--without-zlib", + "--without-zstd", + ] + select({ + "@//:linux_aarch64": ["--host=aarch64-linux-musl"], + "@//:linux_x86_64": ["--host=x86_64-linux-musl"], + "//conditions:default": [], + }), + copts = ["-O3"], + env = { + "URBIT_RUNTIME_OPENSSL": "$$PWD/$(GENDIR)/external/openssl/openssl", + }, + lib_source = ":all", + out_static_libs = ["libcurl.a"], + visibility = ["//visibility:public"], + deps = ["@openssl"], +) diff --git a/bazel/third_party/dbus/BUILD.bazel b/bazel/third_party/dbus/BUILD.bazel new file mode 100644 index 0000000000..e69de29bb2 diff --git a/bazel/third_party/dbus/dbus.BUILD b/bazel/third_party/dbus/dbus.BUILD new file mode 100644 index 0000000000..25fd750092 --- /dev/null +++ b/bazel/third_party/dbus/dbus.BUILD @@ -0,0 +1,21 @@ +load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make") + +filegroup( + name = "all", + srcs = glob(["**"]), +) + +configure_make( + name = "dbus", + lib_name = "libdbus-1", + args = select({ + "@platforms//os:macos": ["--jobs=`sysctl -n hw.logicalcpu`"], + "//conditions:default": ["--jobs=`nproc`"], + }), + copts = ["-O3"], + configure_options = ["--disable-selinux --without-x --disable-tests --disable-systemd --disable-apparmor"], + lib_source = ":all", + configure_in_place = True, + deps = ["@expat"], + visibility = ["//visibility:public"], +) diff --git a/bazel/third_party/ed25519/BUILD.bazel b/bazel/third_party/ed25519/BUILD.bazel new file mode 100644 index 0000000000..e69de29bb2 diff --git a/bazel/third_party/ed25519/ed25519.BUILD b/bazel/third_party/ed25519/ed25519.BUILD new file mode 100644 index 0000000000..172752ca25 --- /dev/null +++ b/bazel/third_party/ed25519/ed25519.BUILD @@ -0,0 +1,14 @@ +cc_library( + name = "ed25519", + srcs = glob( + [ + "src/*.c", + "src/*.h", + ], + exclude = ["src/ed25519.h"], + ), + hdrs = ["src/ed25519.h"], + copts = ["-O3"], + includes = ["src"], + visibility = ["//visibility:public"], +) diff --git a/bazel/third_party/expat/BUILD.bazel b/bazel/third_party/expat/BUILD.bazel new file mode 100644 index 0000000000..e69de29bb2 diff --git a/bazel/third_party/expat/expat.BUILD b/bazel/third_party/expat/expat.BUILD new file mode 100644 index 0000000000..1c0a6bc36d --- /dev/null +++ b/bazel/third_party/expat/expat.BUILD @@ -0,0 +1,24 @@ +load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make") + +filegroup( + name = "all", + srcs = glob(["**"]), +) + +configure_make( + name = "expat", + args = select({ + "@platforms//os:macos": ["--jobs=`sysctl -n hw.logicalcpu`"], + "//conditions:default": ["--jobs=`nproc`"], + }), + configure_options = [ + ] + select({ + "@//:linux_aarch64": ["--host=aarch64-linux-musl"], + "@//:linux_x86_64": ["--host=x86_64-linux-musl"], + "//conditions:default": [], + }), + copts = ["-O3"], + lib_source = ":all", + out_static_libs = ["libexpat.a"], + visibility = ["//visibility:public"], +) diff --git a/bazel/third_party/gmp/BUILD.bazel b/bazel/third_party/gmp/BUILD.bazel new file mode 100644 index 0000000000..e69de29bb2 diff --git a/bazel/third_party/gmp/gmp.BUILD b/bazel/third_party/gmp/gmp.BUILD new file mode 100644 index 0000000000..25ef1db6a4 --- /dev/null +++ b/bazel/third_party/gmp/gmp.BUILD @@ -0,0 +1,30 @@ +load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make") + +filegroup( + name = "all", + srcs = glob(["**"]), +) + +configure_make( + name = "gmp", + args = select({ + "@platforms//os:macos": ["--jobs=`sysctl -n hw.logicalcpu`"], + "//conditions:default": ["--jobs=`nproc`"], + }), + configure_options = [ + "--disable-shared", + # NOTE: --with-pic is required to build PIE binaries on macos_x86_64, + # but we leave it in for all builds as a precaution. + "--with-pic", + ] + select({ + "@//:linux_aarch64": ["--host=aarch64-linux-musl"], + "@//:linux_x86_64": ["--host=x86_64-linux-musl"], + # See https://gmplib.org/list-archives/gmp-bugs/2023-January/005228.html. + "@//:macos_aarch64": ["--disable-assembly"], + "//conditions:default": [], + }), + copts = ["-O3"], + lib_source = ":all", + out_static_libs = ["libgmp.a"], + visibility = ["//visibility:public"], +) diff --git a/bazel/third_party/h2o/2.2.6.patch b/bazel/third_party/h2o/2.2.6.patch new file mode 100644 index 0000000000..c04571f7ad --- /dev/null +++ b/bazel/third_party/h2o/2.2.6.patch @@ -0,0 +1,47 @@ +diff --git a/deps/klib/ksw.c b/deps/klib/ksw.c +index 742fec90b..9a11a21bd 100644 +--- a/deps/klib/ksw.c ++++ b/deps/klib/ksw.c +@@ -25,7 +25,11 @@ + + #include + #include ++#if defined(URBIT_RUNTIME_CPU_AARCH64) ++#include "sse2neon.h" ++#else + #include ++#endif + #include "ksw.h" + + #ifdef __GNUC__ +diff --git a/deps/klib/test/kbit_test.c b/deps/klib/test/kbit_test.c +index 3ae3bd309..7db05a186 100644 +--- a/deps/klib/test/kbit_test.c ++++ b/deps/klib/test/kbit_test.c +@@ -1,7 +1,11 @@ + #include + #include + #include ++#if defined(URBIT_RUNTIME_CPU_AARCH64) ++#include "sse2neon.h" ++#else + #include ++#endif + #include "kbit.h" + + // from bowtie-0.9.8.1 +diff --git a/deps/picohttpparser/picohttpparser.c b/deps/picohttpparser/picohttpparser.c +index a707070d1..714850f64 100644 +--- a/deps/picohttpparser/picohttpparser.c ++++ b/deps/picohttpparser/picohttpparser.c +@@ -28,7 +28,9 @@ + #include + #include + #ifdef __SSE4_2__ +-#ifdef _MSC_VER ++#if defined(URBIT_RUNTIME_CPU_AARCH64) ++#include "sse2neon.h" ++#elif defined(_MSC_VER) + #include + #else + #include diff --git a/bazel/third_party/h2o/BUILD.bazel b/bazel/third_party/h2o/BUILD.bazel new file mode 100644 index 0000000000..e69de29bb2 diff --git a/bazel/third_party/h2o/h2o.BUILD b/bazel/third_party/h2o/h2o.BUILD new file mode 100644 index 0000000000..84b1f9b738 --- /dev/null +++ b/bazel/third_party/h2o/h2o.BUILD @@ -0,0 +1,375 @@ +# This build file is derived from `CMakeLists.txt` in the `h2o` repo at +# https://github.com/h2o/h2o. + +# See `CC_WARNING_FLAGS` in `CMakeLists.txt` in the `h2o` repo. +CC_WARNING_FLAGS = [ + "-Wall", + "-Wno-unused-value", + "-Wno-unused-function", + "-Wno-nullability-completeness", + "-Wno-expansion-to-defined", + "-Werror=implicit-function-declaration", + "-Werror=incompatible-pointer-types", +] + +# +# H2O DEPENDENCIES +# + +# See `deps/cloxec` in the `h2o` repo. +cc_library( + name = "cloexec", + srcs = ["deps/cloexec/cloexec.c"], + hdrs = ["deps/cloexec/cloexec.h"], + copts = ["-O3"], + includes = ["deps/cloexec"], + linkstatic = True, + visibility = ["//visibility:private"], +) + +# See `deps/golombset` in the `h2o` repo. +cc_library( + name = "golombset", + hdrs = ["deps/golombset/golombset.h"], + copts = ["-O3"], + includes = ["deps/golombset"], + linkstatic = True, + visibility = ["//visibility:private"], +) + +# See `deps/klib` in the `h2o` repo. +cc_library( + name = "klib", + srcs = glob(["deps/klib/*.c"]), + hdrs = glob(["deps/klib/*.h"]), + copts = ["-O3"], + includes = ["deps/klib"], + linkstatic = True, + local_defines = select({ + "@platforms//cpu:aarch64": ["URBIT_RUNTIME_CPU_AARCH64"], + "//conditions:default": [], + }), + visibility = ["//visibility:private"], + deps = [ + "@curl", + "@zlib", + ] + select({ + "@platforms//cpu:aarch64": ["@sse2neon"], + "//conditions:default": [], + }), +) + +# See `deps/libgkc` in the `h2o` repo. +cc_library( + name = "libgkc", + srcs = ["deps/libgkc/gkc.c"], + hdrs = ["deps/libgkc/gkc.h"], + copts = ["-O3"], + includes = ["deps/libgkc"], + linkstatic = True, + visibility = ["//visibility:private"], +) + +# See `deps/libyrmcds` in the `h2o` repo. +cc_library( + name = "libyrmcds", + srcs = glob( + ["deps/libyrmcds/*.c"], + exclude = [ + "deps/libyrmcds/yc.c", + "deps/libyrmcds/yc-cnt.c", + ], + ) + [ + "deps/libyrmcds/yrmcds_portability.h", + "deps/libyrmcds/yrmcds_text.h", + ], + hdrs = ["deps/libyrmcds/yrmcds.h"], + copts = [ + "-Wall", + "-Wconversion", + "-gdwarf-3", + "-O2", + ], + includes = ["deps/libyrmcds"], + linkstatic = True, + visibility = ["//visibility:private"], +) + +# See `deps/picohttpparser` in the `h2o` repo. +cc_library( + name = "picohttpparser", + srcs = ["deps/picohttpparser/picohttpparser.c"], + hdrs = ["deps/picohttpparser/picohttpparser.h"], + copts = ["-O3"], + includes = ["deps/picohttpparser"], + linkstatic = True, + local_defines = select({ + "@platforms//cpu:aarch64": ["URBIT_RUNTIME_CPU_AARCH64"], + "//conditions:default": [], + }), + visibility = ["//visibility:private"], + deps = select({ + "@platforms//cpu:aarch64": ["@sse2neon"], + "//conditions:default": [], + }), +) + +# See `deps/picotls` in the `h2o` repo. +cc_library( + name = "picotls", + srcs = glob( + ["deps/picotls/lib/*.c"], + ), + hdrs = ["deps/picotls/include/picotls.h"] + glob( + ["deps/picotls/include/picotls/*.h"], + ), + copts = [ + "-std=c99", + "-Wall", + "-O2", + "-g", + ], + includes = [ + "deps/picotls/include", + "deps/picotls/include/picotls", + ], + linkstatic = True, + local_defines = select({ + "@platforms//cpu:aarch64": ["URBIT_RUNTIME_CPU_AARCH64"], + "//conditions:default": [], + }), + visibility = ["//visibility:private"], + deps = [ + ":cifra", + ":micro_ecc", + "@openssl", + ] + select({ + "@platforms//cpu:aarch64": ["@sse2neon"], + "//conditions:default": [], + }), +) + +# See `deps/ssl-conservatory` in the `h2o` repo. +cc_library( + name = "ssl_conservatory", + hdrs = ["deps/ssl-conservatory/openssl/openssl_hostname_validation.h"], + copts = ["-O3"], + includes = ["deps/ssl-conservatory/openssl"], + linkstatic = True, + textual_hdrs = ["deps/ssl-conservatory/openssl/openssl_hostname_validation.c"], + visibility = ["//visibility:private"], +) + +# See `deps/yoml` in the `h2o` repo. +cc_library( + name = "yoml", + hdrs = glob(["deps/yoml/*.h"]), + copts = ["-O3"], + includes = ["deps/yoml"], + linkstatic = True, + visibility = ["//visibility:private"], +) + +# +# PICOTLS DEPENDENCIES +# + +# See `deps/picotls/deps/cifra` in the `h2o` repo. +cc_library( + name = "cifra", + srcs = [ + "deps/picotls/deps/cifra/src/aes.c", + "deps/picotls/deps/cifra/src/sha256.c", + "deps/picotls/deps/cifra/src/sha512.c", + "deps/picotls/deps/cifra/src/chash.c", + "deps/picotls/deps/cifra/src/hmac.c", + "deps/picotls/deps/cifra/src/pbkdf2.c", + "deps/picotls/deps/cifra/src/modes.c", + "deps/picotls/deps/cifra/src/eax.c", + "deps/picotls/deps/cifra/src/gf128.c", + "deps/picotls/deps/cifra/src/blockwise.c", + "deps/picotls/deps/cifra/src/cmac.c", + "deps/picotls/deps/cifra/src/salsa20.c", + "deps/picotls/deps/cifra/src/chacha20.c", + "deps/picotls/deps/cifra/src/curve25519.c", + "deps/picotls/deps/cifra/src/gcm.c", + "deps/picotls/deps/cifra/src/cbcmac.c", + "deps/picotls/deps/cifra/src/ccm.c", + "deps/picotls/deps/cifra/src/sha3.c", + "deps/picotls/deps/cifra/src/sha1.c", + "deps/picotls/deps/cifra/src/poly1305.c", + "deps/picotls/deps/cifra/src/norx.c", + "deps/picotls/deps/cifra/src/chacha20poly1305.c", + "deps/picotls/deps/cifra/src/drbg.c", + "deps/picotls/deps/cifra/src/ocb.c", + ] + glob( + [ + "deps/picotls/deps/cifra/src/*.h", + "deps/picotls/deps/cifra/src/ext/*.h", + ], + exclude = ["deps/picotls/deps/cifra/src/ext/handy.h"], + ), + hdrs = glob( + [ + "deps/picotls/deps/cifra/src/*.h", + "deps/picotls/deps/cifra/src/ext/*.h", + ], + ), + copts = ["-O3"], + includes = [ + "deps/picotls/deps/cifra/src", + "deps/picotls/deps/cifra/src/ext", + ], + linkstatic = True, + textual_hdrs = ["deps/picotls/deps/cifra/src/curve25519.tweetnacl.c"], + visibility = ["//visibility:private"], +) + +# See `deps/picotls/deps/micro-ecc` in the `h2o` repo. +cc_library( + name = "micro_ecc", + srcs = [ + "deps/picotls/deps/micro-ecc/types.h", + "deps/picotls/deps/micro-ecc/uECC.c", + "deps/picotls/deps/micro-ecc/uECC_vli.h", + ], + hdrs = ["deps/picotls/deps/micro-ecc/uECC.h"], + copts = ["-O3"], + includes = ["deps/picotls/deps/micro-ecc"], + textual_hdrs = [ + "deps/picotls/deps/micro-ecc/asm_arm.inc", + "deps/picotls/deps/micro-ecc/asm_arm_mult_square.inc", + "deps/picotls/deps/micro-ecc/asm_arm_mult_square_umaal.inc", + "deps/picotls/deps/micro-ecc/asm_avr.inc", + "deps/picotls/deps/micro-ecc/asm_avr_mult_square.inc", + "deps/picotls/deps/micro-ecc/curve-specific.inc", + "deps/picotls/deps/micro-ecc/platform-specific.inc", + ], + visibility = ["//visibility:private"], +) + +cc_library( + name = "h2o", + # The `*.c` files below correspond to the files in `lib/` in + # `LIB_SOURCE_FILES` in `CMakeLists.txt` in the `h2o` repo. + # + # Files in `deps/` in `LIB_SOURCE_FILES` are in their respective library + # targets above. + srcs = [ + "lib/common/cache.c", + "lib/common/file.c", + "lib/common/filecache.c", + "lib/common/hostinfo.c", + "lib/common/http1client.c", + "lib/common/memcached.c", + "lib/common/memory.c", + "lib/common/multithread.c", + "lib/common/serverutil.c", + "lib/common/socket.c", + "lib/common/socketpool.c", + "lib/common/string.c", + "lib/common/time.c", + "lib/common/timeout.c", + "lib/common/url.c", + "lib/core/config.c", + "lib/core/configurator.c", + "lib/core/context.c", + "lib/core/headers.c", + "lib/core/logconf.c", + "lib/core/proxy.c", + "lib/core/request.c", + "lib/core/token.c", + "lib/core/util.c", + "lib/handler/access_log.c", + "lib/handler/chunked.c", + "lib/handler/compress.c", + "lib/handler/compress/gzip.c", + "lib/handler/configurator/access_log.c", + "lib/handler/configurator/compress.c", + "lib/handler/configurator/errordoc.c", + "lib/handler/configurator/expires.c", + "lib/handler/configurator/fastcgi.c", + "lib/handler/configurator/file.c", + "lib/handler/configurator/headers.c", + "lib/handler/configurator/headers_util.c", + "lib/handler/configurator/http2_debug_state.c", + "lib/handler/configurator/proxy.c", + "lib/handler/configurator/redirect.c", + "lib/handler/configurator/reproxy.c", + "lib/handler/configurator/status.c", + "lib/handler/configurator/throttle_resp.c", + "lib/handler/errordoc.c", + "lib/handler/expires.c", + "lib/handler/fastcgi.c", + "lib/handler/file.c", + "lib/handler/headers.c", + "lib/handler/headers_util.c", + "lib/handler/http2_debug_state.c", + "lib/handler/mimemap.c", + "lib/handler/proxy.c", + "lib/handler/redirect.c", + "lib/handler/reproxy.c", + "lib/handler/status.c", + "lib/handler/status/durations.c", + "lib/handler/status/events.c", + "lib/handler/status/requests.c", + "lib/handler/throttle_resp.c", + "lib/http1.c", + "lib/http2/cache_digests.c", + "lib/http2/casper.c", + "lib/http2/connection.c", + "lib/http2/frame.c", + "lib/http2/hpack.c", + "lib/http2/http2_debug_state.c", + "lib/http2/scheduler.c", + "lib/http2/stream.c", + "lib/tunnel.c", + ] + glob( + [ + "lib/core/*.h", + "lib/handler/mimemap/*.h", + "lib/handler/file/*.h", + "lib/http2/*.h", + "lib/common/socket/*.h", + ], + ), + hdrs = ["include/h2o.h"] + glob( + [ + "include/h2o/*.h", + "include/h2o/socket/*.h", + ], + ), + copts = [ + "-std=c99", + "-g3", + "-O2", + "-pthread", + ] + CC_WARNING_FLAGS, + includes = [ + "include", + "include/h2o", + "include/h2o/socket", + ], + local_defines = [ + "H2O_USE_LIBUV", + "H2O_USE_PICOTLS", + ] + select({ + "@platforms//os:linux": ["_GNU_SOURCE"], + "//conditions:default": [], + }), + visibility = ["//visibility:public"], + deps = [ + ":cloexec", + ":golombset", + ":klib", + ":libgkc", + ":libyrmcds", + ":picohttpparser", + ":picotls", + ":ssl_conservatory", + ":yoml", + "@openssl", + "@uv", + "@zlib", + ], +) diff --git a/bazel/third_party/keccak_tiny/64b6647514212b76ae7bca0dea9b7b197d1d8186.patch b/bazel/third_party/keccak_tiny/64b6647514212b76ae7bca0dea9b7b197d1d8186.patch new file mode 100644 index 0000000000..04ba2d2e14 --- /dev/null +++ b/bazel/third_party/keccak_tiny/64b6647514212b76ae7bca0dea9b7b197d1d8186.patch @@ -0,0 +1,168 @@ +diff --git a/define-macros.h b/define-macros.h +new file mode 100644 +index 0000000..79b534c +--- /dev/null ++++ b/define-macros.h +@@ -0,0 +1,44 @@ ++#ifndef DEFINEMACROS_H ++#define DEFINEMACROS_H ++ ++/*** Helper macros to define SHA3 and SHAKE instances. ***/ ++#define defshake(bits) \ ++ int shake##bits(uint8_t* out, size_t outlen, \ ++ const uint8_t* in, size_t inlen) { \ ++ return hash(out, outlen, in, inlen, 200 - (bits / 4), 0x1f); \ ++ } ++#define defsha3(bits) \ ++ int sha3_##bits(uint8_t* out, size_t outlen, \ ++ const uint8_t* in, size_t inlen) { \ ++ if (outlen > (bits/8)) { \ ++ return -1; \ ++ } \ ++ return hash(out, outlen, in, inlen, 200 - (bits / 4), 0x06); \ ++ } ++ ++#define defkeccak(bits) \ ++ int keccak_##bits(uint8_t* out, size_t outlen, \ ++ const uint8_t* in, size_t inlen) { \ ++ if (outlen > (bits/8)) { \ ++ return -1; \ ++ } \ ++ return hash(out, outlen, in, inlen, 200 - (bits / 4), 0x01); \ ++ } ++ ++/*** FIPS202 SHAKE VOFs ***/ ++defshake(128) ++defshake(256) ++ ++/*** FIPS202 SHA3 FOFs ***/ ++defsha3(224) ++defsha3(256) ++defsha3(384) ++defsha3(512) ++ ++/*** Non FIP202 SHA3 (KECCAK) FOFs ***/ ++defkeccak(224) ++defkeccak(256) ++defkeccak(384) ++defkeccak(512) ++ ++#endif // DEFINEMACROS_H +diff --git a/keccak-tiny-unrolled.c b/keccak-tiny-unrolled.c +index c238af4..c1eb2a6 100644 +--- a/keccak-tiny-unrolled.c ++++ b/keccak-tiny-unrolled.c +@@ -133,31 +133,11 @@ static inline int hash(uint8_t* out, size_t outlen, + // Squeeze output. + foldP(out, outlen, setout); + setout(a, out, outlen); +- memset_s(a, 200, 0, 200); ++ //TODO: c11 problem: replaced ++ //memset_s(a, 200, 0, 200); ++ //Reference: http://en.cppreference.com/w/c/string/byte/memset ++ memset(a, 0, 200); + return 0; + } + +-/*** Helper macros to define SHA3 and SHAKE instances. ***/ +-#define defshake(bits) \ +- int shake##bits(uint8_t* out, size_t outlen, \ +- const uint8_t* in, size_t inlen) { \ +- return hash(out, outlen, in, inlen, 200 - (bits / 4), 0x1f); \ +- } +-#define defsha3(bits) \ +- int sha3_##bits(uint8_t* out, size_t outlen, \ +- const uint8_t* in, size_t inlen) { \ +- if (outlen > (bits/8)) { \ +- return -1; \ +- } \ +- return hash(out, outlen, in, inlen, 200 - (bits / 4), 0x06); \ +- } +- +-/*** FIPS202 SHAKE VOFs ***/ +-defshake(128) +-defshake(256) +- +-/*** FIPS202 SHA3 FOFs ***/ +-defsha3(224) +-defsha3(256) +-defsha3(384) +-defsha3(512) ++#include "define-macros.h" +diff --git a/keccak-tiny.c b/keccak-tiny.c +index 76d89fa..fcb3556 100644 +--- a/keccak-tiny.c ++++ b/keccak-tiny.c +@@ -133,31 +133,11 @@ static inline int hash(uint8_t* out, size_t outlen, + // Squeeze output. + foldP(out, outlen, setout); + setout(a, out, outlen); +- memset_s(a, 200, 0, 200); ++ //TODO: c11 problem: replaced ++ //memset_s(a, 200, 0, 200); ++ //Reference: http://en.cppreference.com/w/c/string/byte/memset ++ memset(a, 0, 200); + return 0; + } + +-/*** Helper macros to define SHA3 and SHAKE instances. ***/ +-#define defshake(bits) \ +- int shake##bits(uint8_t* out, size_t outlen, \ +- const uint8_t* in, size_t inlen) { \ +- return hash(out, outlen, in, inlen, 200 - (bits / 4), 0x1f); \ +- } +-#define defsha3(bits) \ +- int sha3_##bits(uint8_t* out, size_t outlen, \ +- const uint8_t* in, size_t inlen) { \ +- if (outlen > (bits/8)) { \ +- return -1; \ +- } \ +- return hash(out, outlen, in, inlen, 200 - (bits / 4), 0x06); \ +- } +- +-/*** FIPS202 SHAKE VOFs ***/ +-defshake(128) +-defshake(256) +- +-/*** FIPS202 SHA3 FOFs ***/ +-defsha3(224) +-defsha3(256) +-defsha3(384) +-defsha3(512) ++#include "define-macros.h" +diff --git a/keccak-tiny.h b/keccak-tiny.h +index c4c678e..5d643be 100644 +--- a/keccak-tiny.h ++++ b/keccak-tiny.h +@@ -4,16 +4,31 @@ + #include + #include + ++#define HASH224_SIZE 28 ++#define HASH256_SIZE 32 ++#define HASH384_SIZE 48 ++#define HASH512_SIZE 64 ++ + #define decshake(bits) \ + int shake##bits(uint8_t*, size_t, const uint8_t*, size_t); + + #define decsha3(bits) \ + int sha3_##bits(uint8_t*, size_t, const uint8_t*, size_t); + ++#define deckeccak(bits) \ ++ int keccak_##bits(uint8_t*, size_t, const uint8_t*, size_t); ++ + decshake(128) + decshake(256) ++ + decsha3(224) + decsha3(256) + decsha3(384) + decsha3(512) ++ ++deckeccak(224) ++deckeccak(256) ++deckeccak(384) ++deckeccak(512) ++ + #endif diff --git a/bazel/third_party/keccak_tiny/BUILD.bazel b/bazel/third_party/keccak_tiny/BUILD.bazel new file mode 100644 index 0000000000..e69de29bb2 diff --git a/bazel/third_party/keccak_tiny/keccak_tiny.BUILD b/bazel/third_party/keccak_tiny/keccak_tiny.BUILD new file mode 100644 index 0000000000..4c5e6b9d85 --- /dev/null +++ b/bazel/third_party/keccak_tiny/keccak_tiny.BUILD @@ -0,0 +1,16 @@ +cc_library( + name = "keccak_tiny", + srcs = [ + "define-macros.h", + "keccak-tiny.c", + ], + hdrs = ["keccak-tiny.h"], + copts = ["-O3"], + includes = ["."], + local_defines = select({ + # TODO: confirm which platforms have memset_s(). + "@platforms//os:linux": ["memset_s(W,WL,V,OL)=memset(W,V,OL)"], + "//conditions:default": [], + }), + visibility = ["//visibility:public"], +) diff --git a/bazel/third_party/libbacktrace/BUILD.bazel b/bazel/third_party/libbacktrace/BUILD.bazel new file mode 100644 index 0000000000..e69de29bb2 diff --git a/bazel/third_party/libbacktrace/libbacktrace.BUILD b/bazel/third_party/libbacktrace/libbacktrace.BUILD new file mode 100644 index 0000000000..ecb9ed9036 --- /dev/null +++ b/bazel/third_party/libbacktrace/libbacktrace.BUILD @@ -0,0 +1,25 @@ +load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make") + +filegroup( + name = "all", + srcs = glob(["**"]), +) + +configure_make( + name = "libbacktrace", + args = [ + ] + select({ + "@platforms//os:macos": ["--jobs=`sysctl -n hw.logicalcpu`"], + "//conditions:default": ["--jobs=`nproc`"], + }), + configure_options = [ + ] + select({ + "@//:linux_aarch64": ["--host=aarch64-linux-musl"], + "@//:linux_x86_64": ["--host=x86_64-linux-musl"], + "//conditions:default": [], + }), + copts = ["-O3"], + lib_source = ":all", + out_static_libs = ["libbacktrace.a"], + visibility = ["//visibility:public"], +) diff --git a/bazel/third_party/libunwind/BUILD.bazel b/bazel/third_party/libunwind/BUILD.bazel new file mode 100644 index 0000000000..e69de29bb2 diff --git a/bazel/third_party/libunwind/libunwind.BUILD b/bazel/third_party/libunwind/libunwind.BUILD new file mode 100644 index 0000000000..3934b8f8b3 --- /dev/null +++ b/bazel/third_party/libunwind/libunwind.BUILD @@ -0,0 +1,25 @@ +load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make") + +filegroup( + name = "all", + srcs = glob(["**"]), +) + +configure_make( + name = "libunwind", + args = select({ + "@platforms//os:macos": ["--jobs=`sysctl -n hw.logicalcpu`"], + "//conditions:default": ["--jobs=`nproc`"], + }), + configure_options = [ + "--enable-debug-frame", + ] + select({ + "@//:linux_aarch64": ["--host=aarch64-linux-musl"], + "@//:linux_x86_64": ["--host=x86_64-linux-musl"], + "//conditions:default": [], + }), + copts = ["-O3 -g"], + lib_source = ":all", + out_static_libs = ["libunwind.a"], + visibility = ["//visibility:public"], +) diff --git a/bazel/third_party/lmdb/0.9.29.patch b/bazel/third_party/lmdb/0.9.29.patch new file mode 100644 index 0000000000..cd8416d7cf --- /dev/null +++ b/bazel/third_party/lmdb/0.9.29.patch @@ -0,0 +1,16 @@ +diff --git a/mdb.c b/mdb.c +index 8cecdb2..b8d0648 100644 +--- a/mdb.c ++++ b/mdb.c +@@ -2529,7 +2529,11 @@ mdb_env_sync(MDB_env *env, int force) + rc = ErrCode(); + } else + #endif ++#if defined(URBIT_RUNTIME_OS_DARWIN) ++ if (fcntl(env->me_fd, F_FULLFSYNC, 0)) ++#else + if (MDB_FDATASYNC(env->me_fd)) ++#endif + rc = ErrCode(); + } + } diff --git a/bazel/third_party/lmdb/BUILD.bazel b/bazel/third_party/lmdb/BUILD.bazel new file mode 100644 index 0000000000..e69de29bb2 diff --git a/bazel/third_party/lmdb/lmdb.BUILD b/bazel/third_party/lmdb/lmdb.BUILD new file mode 100644 index 0000000000..334065c4bb --- /dev/null +++ b/bazel/third_party/lmdb/lmdb.BUILD @@ -0,0 +1,29 @@ +# This build file is derived from `libraries/liblmdb/Makefile` in the `lmdb` +# repo at https://github.com/LMDB/lmdb. + +cc_library( + name = "lmdb", + srcs = [ + "mdb.c", + "midl.c", + "midl.h", + ], + hdrs = ["lmdb.h"], + copts = [ + "-pthread", + "-O2", + "-g", + "-W", + "-Wall", + "-Wno-unused-parameter", + "-Wbad-function-cast", + "-Wuninitialized", + ], + include_prefix = "lmdb", + includes = ["."], + local_defines = select({ + "@platforms//os:macos": ["URBIT_RUNTIME_OS_DARWIN"], + "//conditions:default": [], + }), + visibility = ["//visibility:public"], +) diff --git a/bazel/third_party/murmur3/BUILD.bazel b/bazel/third_party/murmur3/BUILD.bazel new file mode 100644 index 0000000000..e69de29bb2 diff --git a/bazel/third_party/murmur3/murmur3.BUILD b/bazel/third_party/murmur3/murmur3.BUILD new file mode 100644 index 0000000000..106e7dc878 --- /dev/null +++ b/bazel/third_party/murmur3/murmur3.BUILD @@ -0,0 +1,21 @@ +# This build file is derived from `makefile` in the `murmur3` repo at +# https://github.com/PeterScott/murmur3. + +cc_library( + name = "murmur3", + srcs = ["murmur3.c"], + hdrs = ["murmur3.h"], + copts = [ + "-O3", + "-Wall", + ] + select({ + # TODO: use selects.with_or() from skylib once it's available. + "@platforms//os:macos": [ + "-fPIC", + "-c", + ], + "//conditions:default": [], + }), + includes = ["."], + visibility = ["//visibility:public"], +) diff --git a/bazel/third_party/natpmp/BUILD.bazel b/bazel/third_party/natpmp/BUILD.bazel new file mode 100644 index 0000000000..e69de29bb2 diff --git a/bazel/third_party/natpmp/natpmp.BUILD b/bazel/third_party/natpmp/natpmp.BUILD new file mode 100644 index 0000000000..b3a7edd7e0 --- /dev/null +++ b/bazel/third_party/natpmp/natpmp.BUILD @@ -0,0 +1,8 @@ +cc_library( + name = "natpmp", + srcs = ["natpmp.c", "getgateway.c"], + hdrs = ["natpmp.h", "getgateway.h", "natpmp_declspec.h"], + copts = ["-O3"], + linkstatic = True, + visibility = ["//visibility:public"], +) diff --git a/bazel/third_party/openssl/BUILD.bazel b/bazel/third_party/openssl/BUILD.bazel new file mode 100644 index 0000000000..e69de29bb2 diff --git a/bazel/third_party/openssl/openssl.BUILD b/bazel/third_party/openssl/openssl.BUILD new file mode 100644 index 0000000000..bb521c4a98 --- /dev/null +++ b/bazel/third_party/openssl/openssl.BUILD @@ -0,0 +1,43 @@ +load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make") + +filegroup( + name = "all", + srcs = glob(["**"]), +) + +configure_make( + name = "openssl", + args = select({ + "@platforms//os:macos": ["--jobs=`sysctl -n hw.logicalcpu`"], + "//conditions:default": ["--jobs=`nproc`"], + }), + configure_command = select({ + "@//:linux_aarch64": "Configure", + "//conditions:default": "config", + }), + configure_options = [ + "no-shared", + ] + select({ + "@//:linux_aarch64": [ + "linux-aarch64", + # Native compilation on linux-aarch64 isn't supported. The prefix is + # empty because the configure script detects an absolute path to the + # aarch64-linux-gnu-gcc instead of just the binary name. This is + # presumably because of the Bazel toolchain configuration but is not + # an issue. + "--cross-compile-prefix=", + ], + "//conditions:default": [], + }), + copts = ["-O3"], + lib_source = ":all", + out_static_libs = [ + "libssl.a", + "libcrypto.a", + ], + targets = [ + "build_libs", + "install_dev", + ], + visibility = ["//visibility:public"], +) diff --git a/bazel/third_party/pdjson/BUILD.bazel b/bazel/third_party/pdjson/BUILD.bazel new file mode 100644 index 0000000000..e69de29bb2 diff --git a/bazel/third_party/pdjson/pdjson.BUILD b/bazel/third_party/pdjson/pdjson.BUILD new file mode 100644 index 0000000000..3a6d82d96a --- /dev/null +++ b/bazel/third_party/pdjson/pdjson.BUILD @@ -0,0 +1,16 @@ +# This build file is derived from `Makefile` in the `pdjson` repo at +# https://github.com/skeeto/pdjson. + +cc_library( + name = "pdjson", + srcs = ["pdjson.c"], + hdrs = ["pdjson.h"], + copts = [ + "-std=c99", + "-pedantic", + "-Wall", + "-Wextra", + "-Wno-missing-field-initializers" + ], + visibility = ["//visibility:public"] +) diff --git a/bazel/third_party/scrypt/BUILD.bazel b/bazel/third_party/scrypt/BUILD.bazel new file mode 100644 index 0000000000..e69de29bb2 diff --git a/bazel/third_party/scrypt/scrypt.BUILD b/bazel/third_party/scrypt/scrypt.BUILD new file mode 100644 index 0000000000..607987d35f --- /dev/null +++ b/bazel/third_party/scrypt/scrypt.BUILD @@ -0,0 +1,30 @@ +# This build file is derived from `Makefile` in the `scrypt` repo at +# https://github.com/technion/libscrypt. + +cc_library( + name = "scrypt", + srcs = [ + "b64.c", + "b64.h", + "crypto-mcf.c", + "crypto-scrypt-saltgen.c", + "crypto_scrypt-check.c", + "crypto_scrypt-hash.c", + "crypto_scrypt-nosse.c", + "sha256.c", + "sha256.h", + "slowequals.c", + "slowequals.h", + "sysendian.h", + ], + hdrs = ["libscrypt.h"], + copts = [ + "-O2", + "-Wall", + "-g", + "-fstack-protector", + ], + includes = ["."], + local_defines = ["_FORTIFY_SOURCE=2"], + visibility = ["//visibility:public"], +) diff --git a/bazel/third_party/secp256k1/694ce8fb2d1fd8a3d641d7c33705691d41a2a860.patch b/bazel/third_party/secp256k1/694ce8fb2d1fd8a3d641d7c33705691d41a2a860.patch new file mode 100644 index 0000000000..f7f7b7c077 --- /dev/null +++ b/bazel/third_party/secp256k1/694ce8fb2d1fd8a3d641d7c33705691d41a2a860.patch @@ -0,0 +1,14 @@ +diff --git i/autogen.sh w/autogen.sh +index 65286b9..edcbeb9 100755 +--- i/autogen.sh ++++ w/autogen.sh +@@ -1,3 +1,9 @@ + #!/bin/sh + set -e ++if [ -r /etc/os-release ]; then ++ . /etc/os-release ++ if [ "$NAME" = NixOS ]; then ++ libtoolize --copy --force ++ fi ++fi + autoreconf -if --warnings=all diff --git a/bazel/third_party/secp256k1/BUILD.bazel b/bazel/third_party/secp256k1/BUILD.bazel new file mode 100644 index 0000000000..e69de29bb2 diff --git a/bazel/third_party/secp256k1/secp256k1.BUILD b/bazel/third_party/secp256k1/secp256k1.BUILD new file mode 100644 index 0000000000..fb7b939b23 --- /dev/null +++ b/bazel/third_party/secp256k1/secp256k1.BUILD @@ -0,0 +1,30 @@ +load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make") + +filegroup( + name = "all", + srcs = glob(["**"]), +) + +configure_make( + name = "secp256k1", + args = select({ + "@platforms//os:macos": ["--jobs=`sysctl -n hw.logicalcpu`"], + "//conditions:default": ["--jobs=`nproc`"], + }), + autogen = True, + configure_in_place = True, + configure_options = [ + "--disable-shared", + "--enable-module-recovery", + "--enable-module-schnorrsig", + "--enable-static", + ] + select({ + "@//:linux_aarch64": ["--host=aarch64-linux-musl"], + "@//:linux_x86_64": ["--host=x86_64-linux-musl"], + "//conditions:default": [], + }), + copts = ["-O3"], + lib_source = ":all", + out_static_libs = ["libsecp256k1.a"], + visibility = ["//visibility:public"], +) diff --git a/bazel/third_party/sigsegv/BUILD.bazel b/bazel/third_party/sigsegv/BUILD.bazel new file mode 100644 index 0000000000..e69de29bb2 diff --git a/bazel/third_party/sigsegv/sigsegv.BUILD b/bazel/third_party/sigsegv/sigsegv.BUILD new file mode 100644 index 0000000000..5b54736c66 --- /dev/null +++ b/bazel/third_party/sigsegv/sigsegv.BUILD @@ -0,0 +1,31 @@ +load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make") + +filegroup( + name = "all", + srcs = glob(["**"]), +) + +configure_make( + name = "sigsegv", + args = select({ + "@platforms//os:macos": ["--jobs=`sysctl -n hw.logicalcpu`"], + "//conditions:default": ["--jobs=`nproc`"], + }), + configure_options = [ + "--disable-shared", + "--enable-static", + ] + select({ + # Disable stack vma check, which reads from procfs, producing drastic + # slowdowns. + "@platforms//os:linux": ["--disable-stackvma"], + "//conditions:default": [], + }) + select({ + "@//:linux_aarch64": ["--host=aarch64-linux-musl"], + "@//:linux_x86_64": ["--host=x86_64-linux-musl"], + "//conditions:default": [], + }), + copts = ["-O3"], + lib_source = ":all", + out_static_libs = ["libsigsegv.a"], + visibility = ["//visibility:public"], +) diff --git a/bazel/third_party/softfloat/5c06db33fc1e2130f67c045327b0ec949032df1d.patch b/bazel/third_party/softfloat/5c06db33fc1e2130f67c045327b0ec949032df1d.patch new file mode 100644 index 0000000000..b05221ff82 --- /dev/null +++ b/bazel/third_party/softfloat/5c06db33fc1e2130f67c045327b0ec949032df1d.patch @@ -0,0 +1,27 @@ +diff --git a/build/template-FAST_INT64/platform.h b/build/template-FAST_INT64/platform.h +index 2094658..f8147a0 100644 +--- a/build/template-FAST_INT64/platform.h ++++ b/build/template-FAST_INT64/platform.h +@@ -38,13 +38,19 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + /*---------------------------------------------------------------------------- + *----------------------------------------------------------------------------*/ +-==> #define LITTLEENDIAN 1 ++#if defined(URBIT_RUNTIME_OS_DARWIN) || defined(URBIT_RUNTIME_OS_BSD) ++#define LITTLEENDIAN 1 ++#endif + + /*---------------------------------------------------------------------------- + *----------------------------------------------------------------------------*/ +-==> #define INLINE inline ++#if defined(URBIT_RUNTIME_OS_DARWIN) || defined(URBIT_RUNTIME_OS_BSD) ++#define INLINE inline ++#endif + + /*---------------------------------------------------------------------------- + *----------------------------------------------------------------------------*/ +-==> #define THREAD_LOCAL _Thread_local ++#if defined(URBIT_RUNTIME_OS_DARWIN) || defined(URBIT_RUNTIME_OS_BSD) ++#define THREAD_LOCAL _Thread_local ++#endif + diff --git a/bazel/third_party/softfloat/BUILD.bazel b/bazel/third_party/softfloat/BUILD.bazel new file mode 100644 index 0000000000..e69de29bb2 diff --git a/bazel/third_party/softfloat/softfloat.BUILD b/bazel/third_party/softfloat/softfloat.BUILD new file mode 100644 index 0000000000..0d57ba5d7f --- /dev/null +++ b/bazel/third_party/softfloat/softfloat.BUILD @@ -0,0 +1,637 @@ +# This build file is derived from the makefiles in `build/` in the `softfloat` +# repo at https://github.com/ucb-bar/berkeley-softfloat-3. +# +# The following link, which is referenced in `README.md` in the `softfloat` +# repo, was useful in deriving this build file: +# http://www.jhauser.us/arithmetic/SoftFloat-3/doc/SoftFloat-source.html + +cc_library( + name = "softfloat", + visibility = ["//visibility:public"], + deps = select({ + "@platforms//cpu:aarch64": [":softfloat_aarch64"], + "@platforms//cpu:x86_64": [":softfloat_x86_64"], + "//conditions:default": [], + }), +) + +cc_library( + name = "softfloat_aarch64", + srcs = [ + # See `OBJS_PRIMITIVES` in `build/Linux-ARM-VFPv2-GCC/Makefile` in the + # `softfloat` repo. + "source/s_compare96M.c", + "source/s_compare128M.c", + "source/s_shortShiftLeft64To96M.c", + "source/s_shortShiftLeftM.c", + "source/s_shiftLeftM.c", + "source/s_shortShiftRightM.c", + "source/s_shortShiftRightJam64.c", + "source/s_shortShiftRightJamM.c", + "source/s_shiftRightJam32.c", + "source/s_shiftRightJam64.c", + "source/s_shiftRightJamM.c", + "source/s_shiftRightM.c", + "source/s_countLeadingZeros8.c", + "source/s_countLeadingZeros16.c", + "source/s_countLeadingZeros32.c", + "source/s_countLeadingZeros64.c", + "source/s_addM.c", + "source/s_addCarryM.c", + "source/s_addComplCarryM.c", + "source/s_negXM.c", + "source/s_sub1XM.c", + "source/s_subM.c", + "source/s_mul64To128M.c", + "source/s_mul128MTo256M.c", + "source/s_approxRecip_1Ks.c", + "source/s_approxRecip32_1.c", + "source/s_approxRecipSqrt_1Ks.c", + "source/s_approxRecipSqrt32_1.c", + "source/s_remStepMBy32.c", + # See `OBJS_SPECIALIZE` in `build/Linux-ARM-VFPv2-GCC/Makefile` in the + # `softfloat` repo. + "source/ARM-VFPv2/softfloat_raiseFlags.c", + "source/ARM-VFPv2/s_f16UIToCommonNaN.c", + "source/ARM-VFPv2/s_commonNaNToF16UI.c", + "source/ARM-VFPv2/s_propagateNaNF16UI.c", + "source/ARM-VFPv2/s_f32UIToCommonNaN.c", + "source/ARM-VFPv2/s_commonNaNToF32UI.c", + "source/ARM-VFPv2/s_propagateNaNF32UI.c", + "source/ARM-VFPv2/s_f64UIToCommonNaN.c", + "source/ARM-VFPv2/s_commonNaNToF64UI.c", + "source/ARM-VFPv2/s_propagateNaNF64UI.c", + "source/ARM-VFPv2/extF80M_isSignalingNaN.c", + "source/ARM-VFPv2/s_extF80MToCommonNaN.c", + "source/ARM-VFPv2/s_commonNaNToExtF80M.c", + "source/ARM-VFPv2/s_propagateNaNExtF80M.c", + "source/ARM-VFPv2/f128M_isSignalingNaN.c", + "source/ARM-VFPv2/s_f128MToCommonNaN.c", + "source/ARM-VFPv2/s_commonNaNToF128M.c", + "source/ARM-VFPv2/s_propagateNaNF128M.c", + # See `OBJS_OTHERS` in `build/Linux-ARM-VFPv2-GCC/Makefile` in the + # `softfloat` repo. + "source/s_roundToUI32.c", + "source/s_roundMToUI64.c", + "source/s_roundToI32.c", + "source/s_roundMToI64.c", + "source/s_normSubnormalF16Sig.c", + "source/s_roundPackToF16.c", + "source/s_normRoundPackToF16.c", + "source/s_addMagsF16.c", + "source/s_subMagsF16.c", + "source/s_mulAddF16.c", + "source/s_normSubnormalF32Sig.c", + "source/s_roundPackToF32.c", + "source/s_normRoundPackToF32.c", + "source/s_addMagsF32.c", + "source/s_subMagsF32.c", + "source/s_mulAddF32.c", + "source/s_normSubnormalF64Sig.c", + "source/s_roundPackToF64.c", + "source/s_normRoundPackToF64.c", + "source/s_addMagsF64.c", + "source/s_subMagsF64.c", + "source/s_mulAddF64.c", + "source/s_tryPropagateNaNExtF80M.c", + "source/s_invalidExtF80M.c", + "source/s_normExtF80SigM.c", + "source/s_roundPackMToExtF80M.c", + "source/s_normRoundPackMToExtF80M.c", + "source/s_addExtF80M.c", + "source/s_compareNonnormExtF80M.c", + "source/s_isNaNF128M.c", + "source/s_tryPropagateNaNF128M.c", + "source/s_invalidF128M.c", + "source/s_shiftNormSigF128M.c", + "source/s_roundPackMToF128M.c", + "source/s_normRoundPackMToF128M.c", + "source/s_addF128M.c", + "source/s_mulAddF128M.c", + "source/softfloat_state.c", + "source/ui32_to_f16.c", + "source/ui32_to_f32.c", + "source/ui32_to_f64.c", + "source/ui32_to_extF80M.c", + "source/ui32_to_f128M.c", + "source/ui64_to_f16.c", + "source/ui64_to_f32.c", + "source/ui64_to_f64.c", + "source/ui64_to_extF80M.c", + "source/ui64_to_f128M.c", + "source/i32_to_f16.c", + "source/i32_to_f32.c", + "source/i32_to_f64.c", + "source/i32_to_extF80M.c", + "source/i32_to_f128M.c", + "source/i64_to_f16.c", + "source/i64_to_f32.c", + "source/i64_to_f64.c", + "source/i64_to_extF80M.c", + "source/i64_to_f128M.c", + "source/f16_to_ui32.c", + "source/f16_to_ui64.c", + "source/f16_to_i32.c", + "source/f16_to_i64.c", + "source/f16_to_ui32_r_minMag.c", + "source/f16_to_ui64_r_minMag.c", + "source/f16_to_i32_r_minMag.c", + "source/f16_to_i64_r_minMag.c", + "source/f16_to_f32.c", + "source/f16_to_f64.c", + "source/f16_to_extF80M.c", + "source/f16_to_f128M.c", + "source/f16_roundToInt.c", + "source/f16_add.c", + "source/f16_sub.c", + "source/f16_mul.c", + "source/f16_mulAdd.c", + "source/f16_div.c", + "source/f16_rem.c", + "source/f16_sqrt.c", + "source/f16_eq.c", + "source/f16_le.c", + "source/f16_lt.c", + "source/f16_eq_signaling.c", + "source/f16_le_quiet.c", + "source/f16_lt_quiet.c", + "source/f16_isSignalingNaN.c", + "source/f32_to_ui32.c", + "source/f32_to_ui64.c", + "source/f32_to_i32.c", + "source/f32_to_i64.c", + "source/f32_to_ui32_r_minMag.c", + "source/f32_to_ui64_r_minMag.c", + "source/f32_to_i32_r_minMag.c", + "source/f32_to_i64_r_minMag.c", + "source/f32_to_f16.c", + "source/f32_to_f64.c", + "source/f32_to_extF80M.c", + "source/f32_to_f128M.c", + "source/f32_roundToInt.c", + "source/f32_add.c", + "source/f32_sub.c", + "source/f32_mul.c", + "source/f32_mulAdd.c", + "source/f32_div.c", + "source/f32_rem.c", + "source/f32_sqrt.c", + "source/f32_eq.c", + "source/f32_le.c", + "source/f32_lt.c", + "source/f32_eq_signaling.c", + "source/f32_le_quiet.c", + "source/f32_lt_quiet.c", + "source/f32_isSignalingNaN.c", + "source/f64_to_ui32.c", + "source/f64_to_ui64.c", + "source/f64_to_i32.c", + "source/f64_to_i64.c", + "source/f64_to_ui32_r_minMag.c", + "source/f64_to_ui64_r_minMag.c", + "source/f64_to_i32_r_minMag.c", + "source/f64_to_i64_r_minMag.c", + "source/f64_to_f16.c", + "source/f64_to_f32.c", + "source/f64_to_extF80M.c", + "source/f64_to_f128M.c", + "source/f64_roundToInt.c", + "source/f64_add.c", + "source/f64_sub.c", + "source/f64_mul.c", + "source/f64_mulAdd.c", + "source/f64_div.c", + "source/f64_rem.c", + "source/f64_sqrt.c", + "source/f64_eq.c", + "source/f64_le.c", + "source/f64_lt.c", + "source/f64_eq_signaling.c", + "source/f64_le_quiet.c", + "source/f64_lt_quiet.c", + "source/f64_isSignalingNaN.c", + "source/extF80M_to_ui32.c", + "source/extF80M_to_ui64.c", + "source/extF80M_to_i32.c", + "source/extF80M_to_i64.c", + "source/extF80M_to_ui32_r_minMag.c", + "source/extF80M_to_ui64_r_minMag.c", + "source/extF80M_to_i32_r_minMag.c", + "source/extF80M_to_i64_r_minMag.c", + "source/extF80M_to_f16.c", + "source/extF80M_to_f32.c", + "source/extF80M_to_f64.c", + "source/extF80M_to_f128M.c", + "source/extF80M_roundToInt.c", + "source/extF80M_add.c", + "source/extF80M_sub.c", + "source/extF80M_mul.c", + "source/extF80M_div.c", + "source/extF80M_rem.c", + "source/extF80M_sqrt.c", + "source/extF80M_eq.c", + "source/extF80M_le.c", + "source/extF80M_lt.c", + "source/extF80M_eq_signaling.c", + "source/extF80M_le_quiet.c", + "source/extF80M_lt_quiet.c", + "source/f128M_to_ui32.c", + "source/f128M_to_ui64.c", + "source/f128M_to_i32.c", + "source/f128M_to_i64.c", + "source/f128M_to_ui32_r_minMag.c", + "source/f128M_to_ui64_r_minMag.c", + "source/f128M_to_i32_r_minMag.c", + "source/f128M_to_i64_r_minMag.c", + "source/f128M_to_f16.c", + "source/f128M_to_f32.c", + "source/f128M_to_f64.c", + "source/f128M_to_extF80M.c", + "source/f128M_roundToInt.c", + "source/f128M_add.c", + "source/f128M_sub.c", + "source/f128M_mul.c", + "source/f128M_mulAdd.c", + "source/f128M_div.c", + "source/f128M_rem.c", + "source/f128M_sqrt.c", + "source/f128M_eq.c", + "source/f128M_le.c", + "source/f128M_lt.c", + "source/f128M_eq_signaling.c", + "source/f128M_le_quiet.c", + "source/f128M_lt_quiet.c", + # See `$(OBJS_ALL)` target in `build/Linux-ARM-VFPv2-GCC/Makefile` in the + # `softfloat` repo. + "build/Linux-ARM-VFPv2-GCC/platform.h", + "source/include/primitiveTypes.h", + "source/include/primitives.h", + # See `$(OBJS_SPECIALIZE) $(OBJS_OTHERS)` target in + # `build/Linux-ARM-VFPv2-GCC/Makefile` in the `softfloat` repo. + "source/include/softfloat_types.h", + "source/include/internals.h", + "source/ARM-VFPv2/specialize.h", + # `platform.h` includes `opts-GCC.h`. + "source/include/opts-GCC.h", + ], + hdrs = ["source/include/softfloat.h"], + copts = [ + "-Iexternal/softfloat/build/Linux-ARM-VFPv2-GCC", + "-Iexternal/softfloat/source/ARM-VFPv2", + "-Werror-implicit-function-declaration", + "-O2", + ], + includes = ["source/include"], + local_defines = [ + "SOFTFLOAT_ROUND_ODD", + "INLINE_LEVEL=5", + ], + visibility = ["//visibility:private"], +) + +cc_library( + name = "softfloat_x86_64", + srcs = [ + # See `OBJS_PRIMITIVES` in `build/Linux-x86_64-GCC/Makefile` in the + # `softfloat` repo. + "source/s_eq128.c", + "source/s_le128.c", + "source/s_lt128.c", + "source/s_shortShiftLeft128.c", + "source/s_shortShiftRight128.c", + "source/s_shortShiftRightJam64.c", + "source/s_shortShiftRightJam64Extra.c", + "source/s_shortShiftRightJam128.c", + "source/s_shortShiftRightJam128Extra.c", + "source/s_shiftRightJam32.c", + "source/s_shiftRightJam64.c", + "source/s_shiftRightJam64Extra.c", + "source/s_shiftRightJam128.c", + "source/s_shiftRightJam128Extra.c", + "source/s_shiftRightJam256M.c", + "source/s_countLeadingZeros8.c", + "source/s_countLeadingZeros16.c", + "source/s_countLeadingZeros32.c", + "source/s_countLeadingZeros64.c", + "source/s_add128.c", + "source/s_add256M.c", + "source/s_sub128.c", + "source/s_sub256M.c", + "source/s_mul64ByShifted32To128.c", + "source/s_mul64To128.c", + "source/s_mul128By32.c", + "source/s_mul128To256M.c", + "source/s_approxRecip_1Ks.c", + "source/s_approxRecip32_1.c", + "source/s_approxRecipSqrt_1Ks.c", + "source/s_approxRecipSqrt32_1.c", + # See `OBJS_SPECIALIZE` in `build/Linux-x86_64-GCC/Makefile` in the + # `softfloat` repo. + "source/8086-SSE/softfloat_raiseFlags.c", + "source/8086-SSE/s_f16UIToCommonNaN.c", + "source/8086-SSE/s_commonNaNToF16UI.c", + "source/8086-SSE/s_propagateNaNF16UI.c", + "source/8086-SSE/s_f32UIToCommonNaN.c", + "source/8086-SSE/s_commonNaNToF32UI.c", + "source/8086-SSE/s_propagateNaNF32UI.c", + "source/8086-SSE/s_f64UIToCommonNaN.c", + "source/8086-SSE/s_commonNaNToF64UI.c", + "source/8086-SSE/s_propagateNaNF64UI.c", + "source/8086-SSE/extF80M_isSignalingNaN.c", + "source/8086-SSE/s_extF80UIToCommonNaN.c", + "source/8086-SSE/s_commonNaNToExtF80UI.c", + "source/8086-SSE/s_propagateNaNExtF80UI.c", + "source/8086-SSE/f128M_isSignalingNaN.c", + "source/8086-SSE/s_f128UIToCommonNaN.c", + "source/8086-SSE/s_commonNaNToF128UI.c", + "source/8086-SSE/s_propagateNaNF128UI.c", + # See `OBJS_OTHERS` in `build/Linux-x86_64-GCC/Makefile` in the + # `softfloat` repo. + "source/s_roundToUI32.c", + "source/s_roundToUI64.c", + "source/s_roundToI32.c", + "source/s_roundToI64.c", + "source/s_normSubnormalF16Sig.c", + "source/s_roundPackToF16.c", + "source/s_normRoundPackToF16.c", + "source/s_addMagsF16.c", + "source/s_subMagsF16.c", + "source/s_mulAddF16.c", + "source/s_normSubnormalF32Sig.c", + "source/s_roundPackToF32.c", + "source/s_normRoundPackToF32.c", + "source/s_addMagsF32.c", + "source/s_subMagsF32.c", + "source/s_mulAddF32.c", + "source/s_normSubnormalF64Sig.c", + "source/s_roundPackToF64.c", + "source/s_normRoundPackToF64.c", + "source/s_addMagsF64.c", + "source/s_subMagsF64.c", + "source/s_mulAddF64.c", + "source/s_normSubnormalExtF80Sig.c", + "source/s_roundPackToExtF80.c", + "source/s_normRoundPackToExtF80.c", + "source/s_addMagsExtF80.c", + "source/s_subMagsExtF80.c", + "source/s_normSubnormalF128Sig.c", + "source/s_roundPackToF128.c", + "source/s_normRoundPackToF128.c", + "source/s_addMagsF128.c", + "source/s_subMagsF128.c", + "source/s_mulAddF128.c", + "source/softfloat_state.c", + "source/ui32_to_f16.c", + "source/ui32_to_f32.c", + "source/ui32_to_f64.c", + "source/ui32_to_extF80.c", + "source/ui32_to_extF80M.c", + "source/ui32_to_f128.c", + "source/ui32_to_f128M.c", + "source/ui64_to_f16.c", + "source/ui64_to_f32.c", + "source/ui64_to_f64.c", + "source/ui64_to_extF80.c", + "source/ui64_to_extF80M.c", + "source/ui64_to_f128.c", + "source/ui64_to_f128M.c", + "source/i32_to_f16.c", + "source/i32_to_f32.c", + "source/i32_to_f64.c", + "source/i32_to_extF80.c", + "source/i32_to_extF80M.c", + "source/i32_to_f128.c", + "source/i32_to_f128M.c", + "source/i64_to_f16.c", + "source/i64_to_f32.c", + "source/i64_to_f64.c", + "source/i64_to_extF80.c", + "source/i64_to_extF80M.c", + "source/i64_to_f128.c", + "source/i64_to_f128M.c", + "source/f16_to_ui32.c", + "source/f16_to_ui64.c", + "source/f16_to_i32.c", + "source/f16_to_i64.c", + "source/f16_to_ui32_r_minMag.c", + "source/f16_to_ui64_r_minMag.c", + "source/f16_to_i32_r_minMag.c", + "source/f16_to_i64_r_minMag.c", + "source/f16_to_f32.c", + "source/f16_to_f64.c", + "source/f16_to_extF80.c", + "source/f16_to_extF80M.c", + "source/f16_to_f128.c", + "source/f16_to_f128M.c", + "source/f16_roundToInt.c", + "source/f16_add.c", + "source/f16_sub.c", + "source/f16_mul.c", + "source/f16_mulAdd.c", + "source/f16_div.c", + "source/f16_rem.c", + "source/f16_sqrt.c", + "source/f16_eq.c", + "source/f16_le.c", + "source/f16_lt.c", + "source/f16_eq_signaling.c", + "source/f16_le_quiet.c", + "source/f16_lt_quiet.c", + "source/f16_isSignalingNaN.c", + "source/f32_to_ui32.c", + "source/f32_to_ui64.c", + "source/f32_to_i32.c", + "source/f32_to_i64.c", + "source/f32_to_ui32_r_minMag.c", + "source/f32_to_ui64_r_minMag.c", + "source/f32_to_i32_r_minMag.c", + "source/f32_to_i64_r_minMag.c", + "source/f32_to_f16.c", + "source/f32_to_f64.c", + "source/f32_to_extF80.c", + "source/f32_to_extF80M.c", + "source/f32_to_f128.c", + "source/f32_to_f128M.c", + "source/f32_roundToInt.c", + "source/f32_add.c", + "source/f32_sub.c", + "source/f32_mul.c", + "source/f32_mulAdd.c", + "source/f32_div.c", + "source/f32_rem.c", + "source/f32_sqrt.c", + "source/f32_eq.c", + "source/f32_le.c", + "source/f32_lt.c", + "source/f32_eq_signaling.c", + "source/f32_le_quiet.c", + "source/f32_lt_quiet.c", + "source/f32_isSignalingNaN.c", + "source/f64_to_ui32.c", + "source/f64_to_ui64.c", + "source/f64_to_i32.c", + "source/f64_to_i64.c", + "source/f64_to_ui32_r_minMag.c", + "source/f64_to_ui64_r_minMag.c", + "source/f64_to_i32_r_minMag.c", + "source/f64_to_i64_r_minMag.c", + "source/f64_to_f16.c", + "source/f64_to_f32.c", + "source/f64_to_extF80.c", + "source/f64_to_extF80M.c", + "source/f64_to_f128.c", + "source/f64_to_f128M.c", + "source/f64_roundToInt.c", + "source/f64_add.c", + "source/f64_sub.c", + "source/f64_mul.c", + "source/f64_mulAdd.c", + "source/f64_div.c", + "source/f64_rem.c", + "source/f64_sqrt.c", + "source/f64_eq.c", + "source/f64_le.c", + "source/f64_lt.c", + "source/f64_eq_signaling.c", + "source/f64_le_quiet.c", + "source/f64_lt_quiet.c", + "source/f64_isSignalingNaN.c", + "source/extF80_to_ui32.c", + "source/extF80_to_ui64.c", + "source/extF80_to_i32.c", + "source/extF80_to_i64.c", + "source/extF80_to_ui32_r_minMag.c", + "source/extF80_to_ui64_r_minMag.c", + "source/extF80_to_i32_r_minMag.c", + "source/extF80_to_i64_r_minMag.c", + "source/extF80_to_f16.c", + "source/extF80_to_f32.c", + "source/extF80_to_f64.c", + "source/extF80_to_f128.c", + "source/extF80_roundToInt.c", + "source/extF80_add.c", + "source/extF80_sub.c", + "source/extF80_mul.c", + "source/extF80_div.c", + "source/extF80_rem.c", + "source/extF80_sqrt.c", + "source/extF80_eq.c", + "source/extF80_le.c", + "source/extF80_lt.c", + "source/extF80_eq_signaling.c", + "source/extF80_le_quiet.c", + "source/extF80_lt_quiet.c", + "source/extF80_isSignalingNaN.c", + "source/extF80M_to_ui32.c", + "source/extF80M_to_ui64.c", + "source/extF80M_to_i32.c", + "source/extF80M_to_i64.c", + "source/extF80M_to_ui32_r_minMag.c", + "source/extF80M_to_ui64_r_minMag.c", + "source/extF80M_to_i32_r_minMag.c", + "source/extF80M_to_i64_r_minMag.c", + "source/extF80M_to_f16.c", + "source/extF80M_to_f32.c", + "source/extF80M_to_f64.c", + "source/extF80M_to_f128M.c", + "source/extF80M_roundToInt.c", + "source/extF80M_add.c", + "source/extF80M_sub.c", + "source/extF80M_mul.c", + "source/extF80M_div.c", + "source/extF80M_rem.c", + "source/extF80M_sqrt.c", + "source/extF80M_eq.c", + "source/extF80M_le.c", + "source/extF80M_lt.c", + "source/extF80M_eq_signaling.c", + "source/extF80M_le_quiet.c", + "source/extF80M_lt_quiet.c", + "source/f128_to_ui32.c", + "source/f128_to_ui64.c", + "source/f128_to_i32.c", + "source/f128_to_i64.c", + "source/f128_to_ui32_r_minMag.c", + "source/f128_to_ui64_r_minMag.c", + "source/f128_to_i32_r_minMag.c", + "source/f128_to_i64_r_minMag.c", + "source/f128_to_f16.c", + "source/f128_to_f32.c", + "source/f128_to_extF80.c", + "source/f128_to_f64.c", + "source/f128_roundToInt.c", + "source/f128_add.c", + "source/f128_sub.c", + "source/f128_mul.c", + "source/f128_mulAdd.c", + "source/f128_div.c", + "source/f128_rem.c", + "source/f128_sqrt.c", + "source/f128_eq.c", + "source/f128_le.c", + "source/f128_lt.c", + "source/f128_eq_signaling.c", + "source/f128_le_quiet.c", + "source/f128_lt_quiet.c", + "source/f128_isSignalingNaN.c", + "source/f128M_to_ui32.c", + "source/f128M_to_ui64.c", + "source/f128M_to_i32.c", + "source/f128M_to_i64.c", + "source/f128M_to_ui32_r_minMag.c", + "source/f128M_to_ui64_r_minMag.c", + "source/f128M_to_i32_r_minMag.c", + "source/f128M_to_i64_r_minMag.c", + "source/f128M_to_f16.c", + "source/f128M_to_f32.c", + "source/f128M_to_extF80M.c", + "source/f128M_to_f64.c", + "source/f128M_roundToInt.c", + "source/f128M_add.c", + "source/f128M_sub.c", + "source/f128M_mul.c", + "source/f128M_mulAdd.c", + "source/f128M_div.c", + "source/f128M_rem.c", + "source/f128M_sqrt.c", + "source/f128M_eq.c", + "source/f128M_le.c", + "source/f128M_lt.c", + "source/f128M_eq_signaling.c", + "source/f128M_le_quiet.c", + "source/f128M_lt_quiet.c", + # See `$(OTHER_HEADERS)` in `build/Linux-x86_64-GCC/Makefile` in the + # `softfloat` repo. + "source/include/opts-GCC.h", + # See `$(OBJS_ALL)` target in `build/Linux-x86_64-GCC/Makefile` in the + # `softfloat` repo. + "build/Linux-x86_64-GCC/platform.h", + "source/include/primitiveTypes.h", + "source/include/primitives.h", + # See `$(OBJS_SPECIALIZE) $(OBJS_OTHERS)` target in + # `build/Linux-x86_64-GCC/Makefile` in the `softfloat` repo. + "source/include/softfloat_types.h", + "source/include/internals.h", + "source/8086-SSE/specialize.h", + ], + hdrs = [ + "source/include/softfloat.h", + ], + copts = [ + "-Iexternal/softfloat/build/Linux-x86_64-GCC", + "-Iexternal/softfloat/source/8086-SSE", + "-Werror-implicit-function-declaration", + "-O2", + ], + # `SOFTFLOAT_FAST_INT64` is used in `softfloat.h` and therefore needs to be + # passed to dependencies. + defines = ["SOFTFLOAT_FAST_INT64"], + includes = ["source/include"], + local_defines = [ + "SOFTFLOAT_ROUND_ODD", + "INLINE_LEVEL=5", + "SOFTFLOAT_FAST_DIV32TO16", + "SOFTFLOAT_FAST_DIV64TO32", + ], + visibility = ["//visibility:private"], +) diff --git a/bazel/third_party/sse2neon/BUILD.bazel b/bazel/third_party/sse2neon/BUILD.bazel new file mode 100644 index 0000000000..e69de29bb2 diff --git a/bazel/third_party/sse2neon/sse2neon.BUILD b/bazel/third_party/sse2neon/sse2neon.BUILD new file mode 100644 index 0000000000..95aee5072d --- /dev/null +++ b/bazel/third_party/sse2neon/sse2neon.BUILD @@ -0,0 +1,7 @@ +cc_library( + name = "sse2neon", + hdrs = ["sse2neon.h"], + copts = ["-O3"], + linkstatic = True, + visibility = ["//visibility:public"], +) diff --git a/bazel/third_party/urcrypt/BUILD.bazel b/bazel/third_party/urcrypt/BUILD.bazel new file mode 100644 index 0000000000..e69de29bb2 diff --git a/bazel/third_party/urcrypt/urcrypt.BUILD b/bazel/third_party/urcrypt/urcrypt.BUILD new file mode 100644 index 0000000000..c41d78f5b0 --- /dev/null +++ b/bazel/third_party/urcrypt/urcrypt.BUILD @@ -0,0 +1,28 @@ +load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make") + +filegroup( + name = "all", + srcs = glob(["**"]), +) + +configure_make( + name = "urcrypt", + autogen = True, + configure_in_place = True, + configure_options = [ + "--disable-shared", + ], + copts = [ + "-Wall", + "-g", + "-O2", + ], + deps = [ + "@aes_siv", + "@openssl", + "@secp256k1" + ], + lib_source = ":all", + out_static_libs = ["liburcrypt.a"], + visibility = ["//visibility:public"], +) diff --git a/bazel/third_party/uv/BUILD.bazel b/bazel/third_party/uv/BUILD.bazel new file mode 100644 index 0000000000..e69de29bb2 diff --git a/bazel/third_party/uv/uv.BUILD b/bazel/third_party/uv/uv.BUILD new file mode 100644 index 0000000000..1fd2352559 --- /dev/null +++ b/bazel/third_party/uv/uv.BUILD @@ -0,0 +1,28 @@ +load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make") + +filegroup( + name = "all", + srcs = glob(["**"]), +) + +configure_make( + name = "uv", + args = select({ + "@platforms//os:macos": ["--jobs=`sysctl -n hw.logicalcpu`"], + "//conditions:default": ["--jobs=`nproc`"], + }), + autogen = True, + configure_in_place = True, + configure_options = [ + "--disable-shared", + ] + select({ + "@//:linux_aarch64": ["--host=aarch64-linux-musl"], + "@//:linux_x86_64": ["--host=x86_64-linux-musl"], + "//conditions:default": [], + }), + copts = ["-O3"], + lib_source = ":all", + out_static_libs = ["libuv.a"], + targets = ["install"], + visibility = ["//visibility:public"], +) diff --git a/bazel/third_party/whereami/BUILD.bazel b/bazel/third_party/whereami/BUILD.bazel new file mode 100644 index 0000000000..e69de29bb2 diff --git a/bazel/third_party/whereami/whereami.BUILD b/bazel/third_party/whereami/whereami.BUILD new file mode 100644 index 0000000000..4d314afec4 --- /dev/null +++ b/bazel/third_party/whereami/whereami.BUILD @@ -0,0 +1,8 @@ +cc_library( + name = "whereami", + srcs = ["src/whereami.c"], + hdrs = ["src/whereami.h"], + copts = ["-O3"], + includes = ["src"], + visibility = ["//visibility:public"], +) diff --git a/bazel/third_party/zlib/BUILD.bazel b/bazel/third_party/zlib/BUILD.bazel new file mode 100644 index 0000000000..e69de29bb2 diff --git a/bazel/third_party/zlib/zlib.BUILD b/bazel/third_party/zlib/zlib.BUILD new file mode 100644 index 0000000000..2b351f2c30 --- /dev/null +++ b/bazel/third_party/zlib/zlib.BUILD @@ -0,0 +1,19 @@ +load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make") + +filegroup( + name = "all", + srcs = glob(["**"]), +) + +configure_make( + name = "zlib", + args = select({ + "@platforms//os:macos": ["--jobs=`sysctl -n hw.logicalcpu`"], + "//conditions:default": ["--jobs=`nproc`"], + }), + configure_options = ["--static"], + copts = ["-O3"], + lib_source = ":all", + out_static_libs = ["libz.a"], + visibility = ["//visibility:public"], +) diff --git a/bazel/toolchain/BUILD.bazel b/bazel/toolchain/BUILD.bazel new file mode 100644 index 0000000000..38fa379197 --- /dev/null +++ b/bazel/toolchain/BUILD.bazel @@ -0,0 +1,410 @@ +# ADDING A NEW TOOLCHAIN +# +# The general process for adding a new toolchain is as follows: +# +# (1) Define a `cc_toolchaing_config()` rule target with a `name` attribute of +# the form `---config` and a `toolchain_identifier` of +# the form `toolchain---`. The `cc_toolchain_config()` +# definition in `//bazel/toolchain:cfg.bzl` lists the mandatory attributes +# for the `cc_toolchain_config()` rule. If any of the toolchain's include +# paths require a compiler version number, simply replace the version number +# with `{compiler_version}`, which is replaced with the value of the label +# indicated by the `compiler_version` attribute when the rule is resolved. +# (2) Define a `cc_toolchain()` rule target with a `name` attribute of the form +# `--` and a `toolchain_config` attribute that references +# the `cc_toolchain_config()` target from (1). +# (3) Define a `toolchain()` rule target with a `name` attribute of the form +# `---toolchain` and specify the platform the toolchain +# runs on via the `exec_compatible_with` attribute and the platform the +# toolchain targets (i.e. compiles for) via the `target_compatible_with` +# attribute. Use the `cc_toolchain()` target from (2) as the value of the +# `toolchain` attribute and `:toolchain_type` for the `toolchain_type` +# attribute. +# (4) Register the new toolchain in `//:WORKSPACE.bazel` by adding the +# toolchain's label (i.e. +# `//bazel/toolchain:---toolchain`) to the +# `register_toolchains()` function. +# +# For more information on configuring toolchains, see +# https://bazel.build/tutorials/ccp-toolchain-config +# and https://bazel.build/extending/toolchains. + +load(":cfg.bzl", "cc_toolchain_config") + +# The prefix for installed external toolchains. +_install_prefix = "/usr/local" + +filegroup(name = "empty") + +alias( + name = "toolchain_type", + actual = "@bazel_tools//tools/cpp:toolchain_type", +) + +# +# gcc-linux-aarch64 +# + +# Toolchain identifier. +_aarch64_gcc = "toolchain-gcc-linux-aarch64" + +cc_toolchain_config( + name = "gcc-linux-aarch64-config", + ar = "{}/aarch64-linux-musl/bin/aarch64-linux-musl-gcc-ar".format(_install_prefix), + cc = "{}/aarch64-linux-musl/bin/aarch64-linux-musl-gcc".format(_install_prefix), + cc_flags = [ + "-static", + "--sysroot={}/aarch64-linux-musl".format(_install_prefix), + ], + compiler = "aarch64-linux-musl-gcc", + compiler_version = "//:gcc_version", + ld = "{}/aarch64-linux-musl/bin/aarch64-linux-musl-ld".format(_install_prefix), + ld_flags = ["--sysroot={}/aarch64-linux-musl".format(_install_prefix)], + sys_includes = [ + "{}/aarch64-linux-musl/aarch64-linux-musl/include".format(_install_prefix), + "{}/aarch64-linux-musl/lib/gcc/aarch64-linux-musl/{{compiler_version}}/include".format(_install_prefix), + ], + target_cpu = "aarch64", + toolchain_identifier = _aarch64_gcc, +) + +cc_toolchain( + name = "gcc-linux-aarch64", + all_files = ":empty", + compiler_files = ":empty", + dwp_files = ":empty", + linker_files = ":empty", + objcopy_files = ":empty", + strip_files = ":empty", + supports_param_files = 0, + toolchain_config = ":gcc-linux-aarch64-config", + toolchain_identifier = _aarch64_gcc, +) + +toolchain( + name = "gcc-linux-aarch64-toolchain", + exec_compatible_with = [ + "@platforms//os:linux", + "@platforms//cpu:aarch64", + ], + target_compatible_with = [ + "@platforms//os:linux", + "@platforms//cpu:aarch64", + ], + toolchain = ":gcc-linux-aarch64", + toolchain_type = ":toolchain_type", +) + +# +# gcc-linux-x86_64 +# + +# Toolchain identifier. +_x86_64_gcc = "toolchain-gcc-linux-x86_64" + +cc_toolchain_config( + name = "gcc-linux-x86_64-config", + ar = "{}/x86_64-linux-musl/bin/x86_64-linux-musl-gcc-ar".format(_install_prefix), + cc = "{}/x86_64-linux-musl/bin/x86_64-linux-musl-gcc".format(_install_prefix), + cc_flags = [ + "-static", + "--sysroot={}/x86_64-linux-musl".format(_install_prefix), + ], + compiler = "x86_64-linux-musl-gcc", + compiler_version = "//:gcc_version", + ld = "{}/x86_64-linux-musl/bin/x86_64-linux-musl-ld".format(_install_prefix), + ld_flags = ["--sysroot={}/x86_64-linux-musl".format(_install_prefix)], + sys_includes = [ + "{}/x86_64-linux-musl/x86_64-linux-musl/include".format(_install_prefix), + "{}/x86_64-linux-musl/lib/gcc/x86_64-linux-musl/{{compiler_version}}/include".format(_install_prefix), + ], + target_cpu = "x86_64", + toolchain_identifier = _x86_64_gcc, +) + +cc_toolchain( + name = "gcc-linux-x86_64", + all_files = ":empty", + compiler_files = ":empty", + dwp_files = ":empty", + linker_files = ":empty", + objcopy_files = ":empty", + strip_files = ":empty", + supports_param_files = 0, + toolchain_config = ":gcc-linux-x86_64-config", + toolchain_identifier = _x86_64_gcc, +) + +toolchain( + name = "gcc-linux-x86_64-toolchain", + exec_compatible_with = [ + "@platforms//os:linux", + "@platforms//cpu:x86_64", + ], + target_compatible_with = [ + "@platforms//os:linux", + "@platforms//cpu:x86_64", + ], + toolchain = ":gcc-linux-x86_64", + toolchain_type = ":toolchain_type", +) + +# +# clang-macos-aarch64 +# + +# Toolchain identifier. +_macos_aarch64_clang = "toolchain-clang-macos-aarch64" + +cc_toolchain_config( + name = "clang-macos-aarch64-config", + # NOTE: building with `libtool` does not work on macOS due to lack of + # support in the `configure_make` rule provided by `rules_foreign_cc`. + # Therefore, we require setting `ar` as the archiver tool on macOS. + ar = "/usr/bin/ar", + # By default, Bazel passes the `rcsD` flags to `ar`, but macOS's `ar` + # implementation doesn't support `D`. We remove it with this attribute + # and corresponding `ar_flags_feature` in `cfg.bzl`. + # See https://github.com/bazelbuild/bazel/issues/15875. + ar_flags = "rcs", + cc = "/usr/bin/clang", + compiler = "clang", + compiler_version = "//:clang_version", + ld = "/usr/bin/ld", + sys_includes = [ + "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include", + "/Library/Developer/CommandLineTools/usr/lib/clang/{compiler_version}/include", + "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include", + "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/{compiler_version}/include", + ], + target_cpu = "aarch64", + toolchain_identifier = _macos_aarch64_clang, +) + +cc_toolchain( + name = "clang-macos-aarch64", + all_files = ":empty", + compiler_files = ":empty", + dwp_files = ":empty", + linker_files = ":empty", + objcopy_files = ":empty", + strip_files = ":empty", + supports_param_files = 0, + toolchain_config = ":clang-macos-aarch64-config", + toolchain_identifier = _macos_aarch64_clang, +) + +toolchain( + name = "clang-macos-aarch64-toolchain", + exec_compatible_with = [ + "@platforms//os:macos", + "@platforms//cpu:aarch64", + ], + target_compatible_with = [ + "@platforms//os:macos", + "@platforms//cpu:aarch64", + ], + toolchain = ":clang-macos-aarch64", + toolchain_type = ":toolchain_type", +) + +# +# clang-macos-x86_64 +# + +# Toolchain identifier. +_macos_x86_64_clang = "toolchain-clang-macos-x86_64" + +cc_toolchain_config( + name = "clang-macos-x86_64-config", + # NOTE: building with `libtool` does not work on macOS due to lack of + # support in the `configure_make` rule provided by `rules_foreign_cc`. + # Therefore, we require setting `ar` as the archiver tool on macOS. + ar = "/usr/bin/ar", + # By default, Bazel passes the `rcsD` flags to `ar`, but macOS's `ar` + # implementation doesn't support `D`. We remove it with this attribute + # and corresponding `ar_flags_feature` in `cfg.bzl`. + # See https://github.com/bazelbuild/bazel/issues/15875. + ar_flags = "rcs", + cc = "/usr/bin/clang", + compiler = "clang", + compiler_version = "//:clang_version", + ld = "/usr/bin/ld", + sys_includes = [ + "/Library/Developer/CommandLineTools/usr/lib/clang/{compiler_version}/include", + "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/", + "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include", + "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/{compiler_version}/include", + ], + target_cpu = "x86_64", + toolchain_identifier = _macos_x86_64_clang, +) + +cc_toolchain( + name = "clang-macos-x86_64", + all_files = ":empty", + compiler_files = ":empty", + dwp_files = ":empty", + linker_files = ":empty", + objcopy_files = ":empty", + strip_files = ":empty", + supports_param_files = 0, + toolchain_config = ":clang-macos-x86_64-config", + toolchain_identifier = _macos_x86_64_clang, +) + +toolchain( + name = "clang-macos-x86_64-toolchain", + exec_compatible_with = [ + "@platforms//os:macos", + "@platforms//cpu:x86_64", + ], + target_compatible_with = [ + "@platforms//os:macos", + "@platforms//cpu:x86_64", + ], + toolchain = ":clang-macos-x86_64", + toolchain_type = ":toolchain_type", +) + +# +# brew-clang-macos-x86_64 +# + +# Toolchain identifier. +_macos_x86_64_brew_clang = "toolchain-brew-clang-macos-x86_64" + +cc_toolchain_config( + name = "brew-clang-macos-x86_64-config", + # NOTE: building with `libtool` does not work on macOS due to lack of + # support in the `configure_make` rule provided by `rules_foreign_cc`. + # Therefore, we require setting `ar` as the archiver tool on macOS. + ar = "/usr/local/opt/llvm@15/bin/llvm-ar", + # By default, Bazel passes the `rcsD` flags to `ar`, but macOS's `ar` + # implementation doesn't support `D`. We remove it with this attribute + # and corresponding `ar_flags_feature` in `cfg.bzl`. + # See https://github.com/bazelbuild/bazel/issues/15875. + ar_flags = "rcs", + cc = "/usr/local/opt/llvm@15/bin/clang", + compiler = "clang", + compiler_version = "//:clang_version", + ld = "/usr/local/opt/llvm@15/bin/llvm-lld", + nm = "/usr/local/opt/llvm@15/bin/llvm-nm", + sys_includes = [ + "/usr/local/Cellar/llvm@15/15.0.7/lib/clang/15.0.7/include", + "/Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk/usr/include", + ], + target_cpu = "x86_64", + toolchain_identifier = _macos_x86_64_brew_clang, +) + +cc_toolchain( + name = "brew-clang-macos-x86_64", + all_files = ":empty", + compiler_files = ":empty", + dwp_files = ":empty", + linker_files = ":empty", + objcopy_files = ":empty", + strip_files = ":empty", + supports_param_files = 0, + toolchain_config = ":brew-clang-macos-x86_64-config", + toolchain_identifier = _macos_x86_64_brew_clang, +) + +toolchain( + name = "brew-clang-macos-x86_64-toolchain", + exec_compatible_with = [ + "@platforms//os:macos", + "@platforms//cpu:x86_64", + ], + target_compatible_with = [ + "@platforms//os:macos", + "@platforms//cpu:x86_64", + ], + toolchain = ":brew-clang-macos-x86_64", + toolchain_type = ":toolchain_type", +) + +# +# BOOTSTRAPPING +# + +# We can't build some artifacts, like musl libc and its toolchain, in Bazel +# itself (i.e. by adding a remote repository to `WORKSPACE.bazel` and a +# corresponding `BUILD` file in `bazel/third_party/`) because doing +# so introduces a circular dependency during Bazel C/C++ toolchain resolution. + +# musl-cross-make builds musl-libc-compatible gcc toolchains from source. +_musl_cross_make_version = "99f2cbc7e230f72bde3394be3ebd50497cb53e89" + +_musl_cross_make_archive = "https://github.com/ripperi/musl-cross-make/archive/{}.tar.gz".format(_musl_cross_make_version) + +genrule( + name = "install-aarch64-linux-musl-gcc", + outs = ["install-aarch64-linux-musl-gcc.sh"], + cmd = """ + echo 'aarch64_linux_musl_install={}/aarch64-linux-musl' > $@ + echo 'if [ ! -d $$aarch64_linux_musl_install ]; then' >> $@ + echo ' wget -c {}' >> $@ + echo ' tar -xf {}.tar.gz' >> $@ + echo ' archive=musl-cross-make-{}' >> $@ + echo ' echo OUTPUT=$$aarch64_linux_musl_install > $$archive/config.mak' >> $@ + echo ' echo GCC_VER=11.4.0 >> $$archive/config.mak' >> $@ + echo ' echo MUSL_VER=1.2.5 >> $$archive/config.mak' >> $@ + echo ' TARGET=aarch64-linux-musl make -s -C$$archive -j`nproc`' >> $@ + echo ' sudo TARGET=aarch64-linux-musl make -s -C$$archive -j`nproc` install' >> $@ + echo ' sudo chown --recursive $$USER $$aarch64_linux_musl_install' >> $@ + echo 'fi' >> $@ + """.format( + _install_prefix, + _musl_cross_make_archive, + _musl_cross_make_version, + _musl_cross_make_version, + ), + exec_compatible_with = ["@platforms//os:linux"], + target_compatible_with = ["@platforms//os:linux"], + visibility = ["//visibility:private"], +) + +sh_binary( + name = "aarch64-linux-musl-gcc", + srcs = ["install-aarch64-linux-musl-gcc"], + exec_compatible_with = ["@platforms//os:linux"], + target_compatible_with = ["@platforms//os:linux"], + visibility = ["//visibility:public"], +) + +genrule( + name = "install-x86_64-linux-musl-gcc", + outs = ["install-x86_64-linux-musl-gcc.sh"], + cmd = """ + echo 'x86_64_linux_musl_install={}/x86_64-linux-musl' > $@ + echo 'if [ ! -d $$x86_64_linux_musl_install ]; then' >> $@ + echo ' wget -c {}' >> $@ + echo ' tar -xf {}.tar.gz' >> $@ + echo ' archive=musl-cross-make-{}' >> $@ + echo ' echo OUTPUT=$$x86_64_linux_musl_install > $$archive/config.mak' >> $@ + echo ' echo GCC_VER=11.4.0 >> $$archive/config.mak' >> $@ + echo ' echo MUSL_VER=1.2.5 >> $$archive/config.mak' >> $@ + echo ' TARGET=x86_64-linux-musl make -s -C$$archive -j`nproc`' >> $@ + echo ' sudo TARGET=x86_64-linux-musl make -s -C$$archive -j`nproc` install' >> $@ + echo ' sudo chown --recursive $$USER $$x86_64_linux_musl_install' >> $@ + echo 'fi' >> $@ + """.format( + _install_prefix, + _musl_cross_make_archive, + _musl_cross_make_version, + _musl_cross_make_version, + ), + exec_compatible_with = ["@platforms//os:linux"], + target_compatible_with = ["@platforms//os:linux"], + visibility = ["//visibility:private"], +) + +sh_binary( + name = "x86_64-linux-musl-gcc", + srcs = ["install-x86_64-linux-musl-gcc"], + exec_compatible_with = ["@platforms//os:linux"], + target_compatible_with = ["@platforms//os:linux"], + visibility = ["//visibility:public"], +) diff --git a/bazel/toolchain/cfg.bzl b/bazel/toolchain/cfg.bzl new file mode 100644 index 0000000000..aa41e1c2c4 --- /dev/null +++ b/bazel/toolchain/cfg.bzl @@ -0,0 +1,168 @@ +load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo") +load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES") +load( + "@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", + "feature", + "flag_group", + "flag_set", + "tool_path", + "variable_with_value", +) + +def _cc_toolchain_config_impl(ctx): + ar_flags = feature( + name = "archiver_flags", + flag_sets = [ + flag_set( + actions = [ACTION_NAMES.cpp_link_static_library], + flag_groups = [ + flag_group(flags = [ctx.attr.ar_flags]), + flag_group( + flags = ["%{output_execpath}"], + expand_if_available = "output_execpath", + ), + ], + ), + flag_set( + actions = [ACTION_NAMES.cpp_link_static_library], + flag_groups = [ + flag_group( + iterate_over = "libraries_to_link", + flag_groups = [ + flag_group( + flags = ["%{libraries_to_link.name}"], + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "object_file", + ), + ), + flag_group( + flags = ["%{libraries_to_link.object_files}"], + iterate_over = "libraries_to_link.object_files", + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "object_file_group", + ), + ), + ], + expand_if_available = "libraries_to_link", + ), + ], + ), + ], + ) + features = [ar_flags] + + if len(ctx.attr.cc_flags) > 0: + cc_flags = feature( + name = "cc_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = [ACTION_NAMES.c_compile], + flag_groups = [flag_group(flags = ctx.attr.cc_flags)], + ), + ], + ) + features.append(cc_flags) + + + if len(ctx.attr.ld_flags) > 0: + ld_flags = feature( + name = "ld_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = [ + ACTION_NAMES.cpp_link_dynamic_library, + ACTION_NAMES.cpp_link_executable, + ACTION_NAMES.cpp_link_nodeps_dynamic_library, + ], + flag_groups = [flag_group(flags = ctx.attr.ld_flags)], + ), + ], + ) + features.append(ld_flags) + + # See + # https://bazel.build/rules/lib/cc_common#create_cc_toolchain_config_info. + return cc_common.create_cc_toolchain_config_info( + ctx = ctx, + # Replace `{compiler_version}` in all include paths with the value of + # the `compiler_version` label. + cxx_builtin_include_directories = [ + path.format(compiler_version = ctx.attr.compiler_version[BuildSettingInfo].value) + for path in ctx.attr.sys_includes + ], + features = features, + toolchain_identifier = ctx.attr.toolchain_identifier, + target_system_name = ctx.attr.target_system_name, + target_cpu = ctx.attr.target_cpu, + target_libc = ctx.attr.target_libc, + compiler = ctx.attr.compiler, + abi_version = ctx.attr.abi_version, + abi_libc_version = ctx.attr.abi_libc_version, + tool_paths = [ + tool_path( + name = "gcc", + path = ctx.attr.cc, + ), + tool_path( + name = "ld", + path = ctx.attr.ld, + ), + tool_path( + name = "ar", + path = ctx.attr.ar, + ), + tool_path( + name = "cpp", + path = ctx.attr.cpp, + ), + tool_path( + name = "gcov", + path = ctx.attr.gcov, + ), + tool_path( + name = "nm", + path = ctx.attr.nm, + ), + tool_path( + name = "objdump", + path = ctx.attr.objdump, + ), + tool_path( + name = "strip", + path = ctx.attr.strip, + ), + ], + ) + +cc_toolchain_config = rule( + implementation = _cc_toolchain_config_impl, + attrs = { + # Required. + "ar": attr.string(mandatory = True), + "cc": attr.string(mandatory = True), + "compiler": attr.string(mandatory = True), + "compiler_version": attr.label(mandatory = True), + "ld": attr.string(mandatory = True), + "target_cpu": attr.string(mandatory = True), + "toolchain_identifier": attr.string(mandatory = True), + # Optional. + "abi_libc_version": attr.string(default = "unknown"), + "abi_version": attr.string(default = "unknown"), + "ar_flags": attr.string(default = "rcsD"), + "cc_flags": attr.string_list(default = []), + "cpp": attr.string(default = "/bin/false"), + "gcov": attr.string(default = "/bin/false"), + "ld_flags": attr.string_list(default = []), + "nm": attr.string(default = "/bin/false"), + "objdump": attr.string(default = "/bin/false"), + "strip": attr.string(default = "/bin/false"), + "sys_includes": attr.string_list(default = []), + "target_libc": attr.string(default = "unknown"), + "target_system_name": attr.string(default = "unknown"), + }, + provides = [CcToolchainConfigInfo], +) diff --git a/docker/BUILD.bazel b/docker/BUILD.bazel new file mode 100644 index 0000000000..6760b71221 --- /dev/null +++ b/docker/BUILD.bazel @@ -0,0 +1,116 @@ +load("@io_bazel_rules_docker//container:container.bzl", "container_image", "container_push") +load("@io_bazel_rules_docker//docker/util:run.bzl", "container_run_and_commit_layer") + +# +# IMAGE CREATION +# + +sh_binary( + name = "start-urbit", + srcs = ["start_urbit.sh"], + visibility = ["//visibility:private"], +) + +sh_binary( + name = "get-urbit-code", + srcs = ["get_urbit_code.sh"], + visibility = ["//visibility:private"], +) + +sh_binary( + name = "reset-urbit-code", + srcs = ["reset_urbit_code.sh"], + visibility = ["//visibility:private"], +) + +container_run_and_commit_layer( + name = "install_pkgs", + commands = [ + "apk update", + "apk add bash curl", + ], + docker_run_flags = ["--network=host"], + image = "@alpine_linux_x86_64//image", + visibility = ["//visibility:private"], +) + +container_image( + name = "image_x86_64", + base = "@alpine_linux_x86_64//image", + cmd = ["start-urbit"], + directory = "/bin", + # `/bin` is probably on the default path, but append it just to be safe. + env = { + "PATH": "$$PATH:/bin", + }, + files = [ + ":get-urbit-code", + ":reset-urbit-code", + ":start-urbit", + "//pkg/vere:urbit", + ], + layers = [":install_pkgs"], + ports = [ + "80/tcp", + "34343/udp", + ], + volumes = ["/urbit"], + workdir = "/urbit", + visibility = ["//visibility:public"], +) + +# +# IMAGE UPLOAD +# + +_docker_repository = "tloncorp/vere" + +genrule( + name = "version_tag", + srcs = ["//:version"], + outs = ["docker_version_tag"], + cmd = """ + echo -n v$$(cat $<) > $@ + """, + visibility = ["//visibility:private"], +) + +genrule( + name = "pace_tag", + srcs = ["//:PACE"], + outs = ["docker_pace_tag"], + cmd = """ + tr -d '\n' < $< > $@ + """, + visibility = ["//visibility:private"], +) + +container_push( + name = "push_latest_x86_64", + format = "Docker", + image = ":image_x86_64", + registry = "docker.io", + repository = _docker_repository, + tag = "latest", + visibility = ["//visibility:public"], +) + +container_push( + name = "push_version_x86_64", + format = "Docker", + image = ":image_x86_64", + registry = "docker.io", + repository = _docker_repository, + tag_file = ":version_tag", + visibility = ["//visibility:public"], +) + +container_push( + name = "push_pace_x86_64", + format = "Docker", + image = ":image_x86_64", + registry = "docker.io", + repository = _docker_repository, + tag_file = ":pace_tag", + visibility = ["//visibility:public"], +) diff --git a/pkg/c3/BUILD.bazel b/pkg/c3/BUILD.bazel new file mode 100644 index 0000000000..95b790b5ec --- /dev/null +++ b/pkg/c3/BUILD.bazel @@ -0,0 +1,42 @@ +# +# LIBRARIES +# + +load("//bazel:common_settings.bzl", "vere_library") + +vere_library( + name = "c3", + srcs = glob( + [ + "*.h", + "*.c", + ], + exclude = [ + "c3.h", + "*_tests.c", + ], + ), + hdrs = ["c3.h"], + # We define platform-specific macros here to avoid the need for a + # configuration header file. + defines = [ + # We don't build on any big endian CPUs. + "U3_OS_ENDIAN_little=1", + "U3_OS_PROF=1", + ] + select({ + "@platforms//cpu:aarch64": ["U3_CPU_aarch64=1"], + "//conditions:default": [], + }) + select({ + "@platforms//os:linux": ["U3_OS_linux=1"], + "@platforms//os:macos": ["U3_OS_osx=1"], + "//conditions:default": [], + }), + include_prefix = "c3", + includes = ["."], + linkstatic = True, + visibility = ["//pkg:__subpackages__"], +) + +# +# TESTS +# diff --git a/pkg/ent/BUILD.bazel b/pkg/ent/BUILD.bazel new file mode 100644 index 0000000000..ad9894b1e2 --- /dev/null +++ b/pkg/ent/BUILD.bazel @@ -0,0 +1,49 @@ +# +# LIBRARIES +# + +load("//bazel:common_settings.bzl", "vere_library") + +vere_library( + name = "ent", + srcs = ["ent.c"], + hdrs = ["ent.h"], + copts = [ + "-O3", + "-Wall", + "-Werror", + "-pedantic", + "-std=gnu99", + ], + include_prefix = "ent", + includes = ["."], + linkstatic = True, + local_defines = select({ + "@platforms//os:macos": ["ENT_GETENTROPY_SYSRANDOM"], + # TODO: support fallback to other options if `getrandom()` isn't + # available in `unistd.h`. Preferred order (from most preferred to least + # preferred) is: + # - ENT_GETENTROPY_UNISTD + # - ENT_GETENTROPY_SYSRANDOM + # - ENT_GETRANDOM_SYSCALL + # - ENT_DEV_URANDOM + "//conditions:default": ["ENT_GETENTROPY_UNISTD"], + }), + visibility = ["//:__subpackages__"], +) + +# +# TESTS +# + +cc_test( + name = "tests", + timeout = "short", + srcs = ["tests.c"], + features = select({ + "@platforms//os:linux": ["fully_static_link"], + "//conditions:default": [], + }), + visibility = ["//visibility:private"], + deps = [":ent"], +) diff --git a/pkg/noun/BUILD.bazel b/pkg/noun/BUILD.bazel new file mode 100644 index 0000000000..1be5825990 --- /dev/null +++ b/pkg/noun/BUILD.bazel @@ -0,0 +1,117 @@ +# +# LIBRARIES +# + +load("//bazel:common_settings.bzl", "vere_library") + +vere_library( + name = "noun", + srcs = glob( + [ + "**/*.c", + "**/*.h", + "jets/*.h", + "jets/**/*.c", + ], + exclude = [ + "noun.h", + "*_tests.c", + ], + ), + hdrs = ["noun.h"], + includes = ["."], + linkopts = select({ + # NOTE: macOS builds fail to link the `u3o_Config` symbol (and maybe + # others) without this. We should figure out why, and remove this. + "@platforms//os:macos": ["-force_load $(GENDIR)/pkg/noun/libnoun.a"], + "//conditions:default": [], + }), + linkstatic = True, + local_defines = ["U3_GUARD_PAGE"], + visibility = ["//pkg:__subpackages__"], + deps = [ + "//pkg/c3", + "//pkg/ent", + "//pkg/ur", + "@gmp", + "@libbacktrace", + "@murmur3", + "@openssl", + "@pdjson", + "@sigsegv", + "@softfloat", + "@urcrypt", + "@whereami", + ] + select({ + "@platforms//os:macos": ["//pkg/noun/platform/darwin"], + "@platforms//os:linux": [ + "//pkg/noun/platform/linux", + "@libunwind", + ], + "//conditions:default": [], + }), +) + +# +# TESTS +# + +cc_test( + name = "hashtable_tests", + timeout = "short", + srcs = ["hashtable_tests.c"], + features = select({ + "@platforms//os:linux": ["fully_static_link"], + "//conditions:default": [], + }), + visibility = ["//visibility:private"], + deps = [":noun"], +) + +cc_test( + name = "jets_tests", + timeout = "short", + srcs = ["jets_tests.c"], + features = select({ + "@platforms//os:linux": ["fully_static_link"], + "//conditions:default": [], + }), + visibility = ["//visibility:private"], + deps = [":noun"], +) + +cc_test( + name = "nock_tests", + timeout = "short", + srcs = ["nock_tests.c"], + features = select({ + "@platforms//os:linux": ["fully_static_link"], + "//conditions:default": [], + }), + visibility = ["//visibility:private"], + deps = [":noun"], +) + +cc_test( + name = "retrieve_tests", + timeout = "short", + srcs = ["retrieve_tests.c"], + features = select({ + "@platforms//os:linux": ["fully_static_link"], + "//conditions:default": [], + }), + visibility = ["//visibility:private"], + deps = [":noun"], +) + +cc_test( + name = "serial_tests", + timeout = "short", + srcs = ["serial_tests.c"], + features = select({ + "@platforms//os:linux": ["fully_static_link"], + "//conditions:default": [], + }), + visibility = ["//visibility:private"], + deps = [":noun"], +) diff --git a/pkg/noun/platform/darwin/BUILD.bazel b/pkg/noun/platform/darwin/BUILD.bazel new file mode 100644 index 0000000000..0724009886 --- /dev/null +++ b/pkg/noun/platform/darwin/BUILD.bazel @@ -0,0 +1,8 @@ +cc_library( + name = "darwin", + hdrs = ["rsignal.h"], + include_prefix = "platform", + includes = ["."], + target_compatible_with = ["@platforms//os:macos"], + visibility = ["//pkg:__subpackages__"], +) diff --git a/pkg/noun/platform/linux/BUILD.bazel b/pkg/noun/platform/linux/BUILD.bazel new file mode 100644 index 0000000000..506d64e175 --- /dev/null +++ b/pkg/noun/platform/linux/BUILD.bazel @@ -0,0 +1,8 @@ +cc_library( + name = "linux", + hdrs = ["rsignal.h"], + include_prefix = "platform", + includes = ["."], + target_compatible_with = ["@platforms//os:linux"], + visibility = ["//pkg:__subpackages__"], +) diff --git a/pkg/ur/BUILD.bazel b/pkg/ur/BUILD.bazel new file mode 100644 index 0000000000..4deff1e706 --- /dev/null +++ b/pkg/ur/BUILD.bazel @@ -0,0 +1,39 @@ +# +# LIBRARIES +# + +load("//bazel:common_settings.bzl", "vere_library") + +vere_library( + name = "ur", + srcs = [ + "bitstream.c", + "bitstream.h", + "defs.h", + "hashcons.c", + "hashcons.h", + "serial.c", + "serial.h", + ], + hdrs = ["ur.h"], + includes = ["."], + linkstatic = True, + visibility = ["//pkg:__subpackages__"], + deps = ["@murmur3"], +) + +# +# TESTS +# + +cc_test( + name = "tests", + timeout = "short", + srcs = ["tests.c"], + features = select({ + "@platforms//os:linux": ["fully_static_link"], + "//conditions:default": [], + }), + visibility = ["//visibility:private"], + deps = [":ur"], +) diff --git a/pkg/vere/BUILD.bazel b/pkg/vere/BUILD.bazel new file mode 100644 index 0000000000..33882e9244 --- /dev/null +++ b/pkg/vere/BUILD.bazel @@ -0,0 +1,522 @@ +# +# GENERATED FILES +# + +load("//bazel:common_settings.bzl", "vere_library", "vere_binary") + +# An approximation of `xxd -i` that runs on all platforms where Bash is +# present. Generates a `.h` file that declares the array and array length as +# `extern` global variables and a `.c` file containing the array and array +# length definitions that includes the `.h` file. +# +# Referencing a shell variable requires `$$`, not `$`. See +# https://bazel.build/reference/be/general#general-advice. +XXD_I = """ +function xxd_i() { + var=$$1 + src_file=$$2 + dst_h_file=$$3 + dst_c_file=$$4 + + # Generate `.h` file. + echo "#ifndef $${var}_H" > $$dst_h_file + echo "#define $${var}_H" >> $$dst_h_file + echo "extern unsigned char $$var[];" >> $$dst_h_file + echo "extern unsigned int $${var}_len;" >> $$dst_h_file + echo '#endif' >> $$dst_h_file + + # Generate `.c` file. + printf '#include "%s"\n' $$dst_h_file > $$dst_c_file + echo "unsigned char $$var[] = {" >> $$dst_c_file + cnt=0 + while IFS='' read line + do + for byte in $$line + do + echo -n " 0x$$byte," >> $$dst_c_file + cnt=$$((cnt+1)) + done + # <() is syntax for Bash process substitution. + done < <(od -An -v -tx1 $$src_file) + echo "};" >> $$dst_c_file + echo "unsigned int $${var}_len = $$cnt;" >> $$dst_c_file +} +""" + +# Produce a certificate authority bundle as a byte array. +genrule( + name = "ca_bundle", + srcs = ["@ca_bundle//file"], + outs = [ + "ca_bundle.h", + "ca_bundle.c", + ], + cmd_bash = """ + # Defines the function `xxd_i()`. + {} + + src_file=$(execpath @ca_bundle//file) + dst_h_file=$(execpath ca_bundle.h) + dst_c_file=$(execpath ca_bundle.c) + xxd_i include_ca_bundle_crt $$src_file $$dst_h_file $$dst_c_file + """.format(XXD_I), + visibility = ["//visibility:private"], +) + +# Produce an `ivory.pill` as a byte array. +genrule( + name = "ivory", + srcs = ["@ivory_pill//file"], + outs = [ + "ivory.h", + "ivory.c", + ], + cmd_bash = """ + # Defines the function `xxd_i()`. + {} + + # Create ivory.h. + src_file=$(execpath @ivory_pill//file) + dst_h_file=$(execpath ivory.h) + dst_c_file=$(execpath ivory.c) + xxd_i u3_Ivory_pill $$src_file $$dst_h_file $$dst_c_file + """.format(XXD_I), + visibility = ["//visibility:private"], +) + +# +# LIBRARIES +# + +vere_library( + name = "vere", + srcs = glob( + [ + "*.c", + "*.h", + "db/*.c", + "io/*.c", + "io/*/*.h", + "io/*/*.c", + ], + exclude = [ + "main.c", + "vere.h", + "*_tests.c", + ], + ) + [ + ":ivory", + "//:pace_hdr", + "//:version_hdr", + ] + select({ + "@platforms//os:macos": [ + "platform/darwin/daemon.c", + "platform/darwin/ptty.c", + "platform/darwin/mach.c", + ], + "@platforms//os:linux": [ + "platform/linux/daemon.c", + "platform/linux/ptty.c", + ], + }), + hdrs = [ + "db/lmdb.h", + "vere.h", + "mdns.h", + ] + select({ + "@platforms//os:macos": [], + "@platforms//os:linux": [ + "dns_sd.h", + ], + }), + includes = ["."], + linkstatic = True, + visibility = ["//pkg:__subpackages__"], + deps = [ + "//pkg/c3", + "//pkg/ent", + "//pkg/noun", + "//pkg/ur", + "@curl", + "@h2o", + "@lmdb", + "@openssl", + "@uv", + "@natpmp", + ] + select({ + "@platforms//os:macos": [], + "@platforms//os:linux": [ + "@avahi", + ], + }) +) + +# +# BINARIES +# + +vere_binary( + name = "urbit", + srcs = [ + "main.c", + ":ca_bundle", + ":ivory", + "//:pace_hdr", + "//:version_hdr", + ], + features = select({ + "@platforms//os:linux": ["fully_static_link"], + "//conditions:default": [], + }), + visibility = ["//visibility:public"], + deps = [ + "//pkg/c3", + "//pkg/noun", + "//pkg/ur", + ":vere", + # TODO: remove dependency (only used to report version). + "@h2o", + "@openssl", + # TODO: remove dependency (only used to report version). + "@sigsegv", + "@whereami", + ], +) + +# +# TESTS +# + + +cc_library( + name = "ames_src", + hdrs = ["io/ames.c"], +) + +cc_test( + name = "ames_tests", + timeout = "short", + srcs = ["ames_tests.c"], + features = select({ + "@platforms//os:linux": ["fully_static_link"], + "//conditions:default": [], + }), + visibility = ["//visibility:private"], + deps = [":vere", ":ames_src"] +) + +cc_test( + name = "boot_tests", + timeout = "short", + srcs = ["boot_tests.c"], + features = select({ + "@platforms//os:linux": ["fully_static_link"], + "//conditions:default": [], + }), + visibility = ["//visibility:private"], + deps = [":vere"], +) + +cc_test( + name = "newt_tests", + timeout = "short", + srcs = ["newt_tests.c"], + features = select({ + "@platforms//os:linux": ["fully_static_link"], + "//conditions:default": [], + }), + visibility = ["//visibility:private"], + deps = [":vere"], +) + +cc_test( + name = "noun_tests", + timeout = "short", + srcs = ["noun_tests.c"], + features = select({ + "@platforms//os:linux": ["fully_static_link"], + "//conditions:default": [], + }), + visibility = ["//visibility:private"], + deps = [":vere"], +) + +cc_test( + name = "unix_tests", + timeout = "short", + srcs = ["unix_tests.c"], + features = select({ + "@platforms//os:linux": ["fully_static_link"], + "//conditions:default": [], + }), + visibility = ["//visibility:private"], + deps = [":vere"], +) + +cc_test( + name = "benchmarks", + timeout = "short", + srcs = ["benchmarks.c"], + features = select({ + "@platforms//os:linux": ["fully_static_link"], + "//conditions:default": [], + }), + visibility = ["//visibility:private"], + deps = [ + ":vere", + "//pkg/noun", + "//pkg/ur", + ], +) + +# +# FAKE SHIP TESTS +# + +genrule( + name = "boot-fake-ship", + srcs = [ + "@solid_pill//file", + "@urbit//file", + ], + outs = ["fakebus.zip"], + cmd = """ + set -xeuo pipefail + + mkdir ./urbit + tar xfz $(execpath @urbit//file) -C ./urbit --strip-components=1 + cp -RL ./urbit/tests ./urbit/pkg/arvo/tests + + $(execpath :urbit) --lite-boot --daemon --fake bus \ + --bootstrap $(execpath @solid_pill//file) \ + --arvo ./urbit/pkg/arvo \ + --pier ./pier + + cleanup() { + if [ -f ./pier/.vere.lock ]; then + kill $$(< ./pier/.vere.lock) || true + fi + set +x + } + + trap cleanup EXIT + port=$$(grep loopback ./pier/.http.ports | awk -F ' ' '{print $$1}') + + lensd() { + curl -s \ + --data "{\\"source\\":{\\"dojo\\":\\"$$1\\"},\\"sink\\":{\\"stdout\\":null}}" \ + "http://localhost:$$port" | xargs printf %s | sed 's/\\\\n/\\n/g' + } + + lensa() { + curl -s \ + --data "{\\"source\\":{\\"dojo\\":\\"$$2\\"},\\"sink\\":{\\"app\\":\\"$$1\\"}}" \ + "http://localhost:$$port" | xargs printf %s | sed 's/\\\\n/\\n/g' + } + + check() { + [ 3 -eq $$(lensd 3) ] + } + + lensd '+vat %base' + + if check && sleep 10 && check; then + echo "boot success" + lensa hood '+hood/exit' + while [ -f ./pier/.vere.lock ]; do + echo "waiting for pier to shut down" + sleep 5 + done + else + echo "boot failure" + kill $$(< ./pier/.vere.lock) || true + set +x + exit 1 + fi + + set +x + ls + ls -a ./pier + zip -q -r $@ ./pier + """, + tools = [":urbit"], + visibility = ["//visibility:public"], +) + +genrule( + name = "test-fake-ship", + srcs = [ + ":boot-fake-ship", + "//pkg/vere:VERSION", + ], + outs = ["test-fake-ship-output.zip"], + cmd = """ + cp $(execpath :boot-fake-ship) pier.zip + unzip -qq pier.zip + chmod -R u+rw pier + + set -x + + $(execpath :urbit) --lite-boot --daemon --gc ./pier 2> urbit-output + + port=$$(grep loopback ./pier/.http.ports | awk -F ' ' '{print $$1}') + + lensd() { + curl -s \ + --data "{\\"source\\":{\\"dojo\\":\\"$$1\\"},\\"sink\\":{\\"stdout\\":null}}" \ + "http://localhost:$$port" | xargs printf %s | sed 's/\\\\n/\\n/g' + } + + lensa() { + curl -s \ + --data "{\\"source\\":{\\"dojo\\":\\"$$2\\"},\\"sink\\":{\\"app\\":\\"$$1\\"}}" \ + "http://localhost:$$port" | xargs printf %s | sed 's/\\\\n/\\n/g' + } + + tail -F urbit-output >&2 & + + tailproc=$$! + + cleanup () { + kill $$(cat ./pier/.vere.lock) || true + kill "$$tailproc" 2>/dev/null || true + + set +x + } + + trap cleanup EXIT + + # print the arvo version + # + lensd '+vat %base' + + # measure initial memory usage + # + lensd '~& ~ ~& %init-mass-start ~' + lensa hood '+hood/mass' + lensd '~& ~ ~& %init-mass-end ~' + + # run the unit tests + # + lensd '~& ~ ~& %test-unit-start ~' + lensd '-test %/tests ~' + lensd '~& ~ ~& %test-unit-end ~' + + # use the :test app to build all agents, generators, and marks + # + lensa hood '+hood/start %test' + + lensd '~& ~ ~& %test-agents-start ~' + lensa test '%agents' + lensd '~& ~ ~& %test-agents-end ~' + + lensd '~& ~ ~& %test-generators-start ~' + lensa test '%generators' + lensd '~& ~ ~& %test-generators-end ~' + + lensd '~& ~ ~& %test-marks-start ~' + lensa test '%marks' + lensd '~& ~ ~& %test-marks-end ~' + + # measure memory usage post tests + # + lensd '~& ~ ~& %test-mass-start ~' + lensa hood '+hood/mass' + lensd '~& ~ ~& %test-mass-end ~' + + # defragment the loom + # + lensd '~& ~ ~& %pack-start ~' + lensa hood '+hood/pack' + lensd '~& ~ ~& %pack-end ~' + + # reclaim space within arvo + # + lensd '~& ~ ~& %trim-start ~' + lensa hood '+hood/trim' + lensd '~& ~ ~& %trim-end ~' + + # measure memory usage pre |meld + # + lensd '~& ~ ~& %trim-mass-start ~' + lensa hood '+hood/mass' + lensd '~& ~ ~& %trim-mass-end ~' + + # globally deduplicate + # + lensd '~& ~ ~& %meld-start ~' + lensa hood '+hood/meld' + lensd '~& ~ ~& %meld-end ~' + + # measure memory usage post |meld + # + lensd '~& ~ ~& %meld-mass-start ~' + lensa hood '+hood/mass' + lensd '~& ~ ~& %meld-mass-end ~' + + lensa hood '+hood/exit' + + cleanup + + # Collect output + cp urbit-output test-output-unit + cp urbit-output test-output-agents + cp urbit-output test-output-generators + cp urbit-output test-output-marks + + # TODO: when re-enabling fake ship tests on macOS, use `sed -i ''` + # instead of `sed -i`. + sed -i '0,/test-unit-start/d' test-output-unit + sed -i '/test-unit-end/,$$d' test-output-unit + + sed -i '0,/test-agents-start/d' test-output-agents + sed -i '/test-agents-end/,$$d' test-output-agents + + sed -i '0,/test-generators-start/d' test-output-generators + sed -i '/test-generators-end/,$$d' test-output-generators + + sed -i '0,/test-marks-start/d' test-output-marks + sed -i '/test-marks-end/,$$d' test-output-marks + + OUTDIR="$$(pwd)/test-fake-ship-output" + mkdir -p $$OUTDIR + cp test-output-* $$OUTDIR + + set +x + + hdr () { + echo =====$$(sed 's/./=/g' <<< "$$1")===== + echo ==== $$1 ==== + echo =====$$(sed 's/./=/g' <<< "$$1")===== + } + + for f in $$(find "$$OUTDIR" -type f); do + hdr "$$(basename $$f)" + cat "$$f" + done + + fail=0 + + for f in $$(find "$$OUTDIR" -type f); do + if egrep "((FAILED|CRASHED)|warn:) " $$f >/dev/null; then + if [[ $$fail -eq 0 ]]; then + hdr "Test Failures" + fi + + echo "ERROR Test failure in $$(basename $$f)" + + ((fail++)) + fi + done + + if [[ $$fail -eq 0 ]]; then + hdr "Success" + fi + + zip -q -r $@ $$OUTDIR + + exit "$$fail" + """, + tools = [":urbit"], + visibility = ["//visibility:public"], +) From bbb93aefee81b54c1704067ceda4fb15841cd3c9 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Fri, 18 Oct 2024 15:19:01 +0200 Subject: [PATCH 426/430] mesa: no-op if waiting for scry response --- pkg/vere/io/mesa.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index aec5142d1a..1e2eb0e90b 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -189,6 +189,7 @@ typedef struct _u3_pend_req { typedef enum _u3_mesa_ctag { CTAG_WAIT = 1, CTAG_ITEM = 2, + CTAG_BLOCK = 3, } u3_mesa_ctag; // jumbo frame cache value @@ -2078,6 +2079,12 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun res) // TODO: mark as dead u3z(res); _mesa_free_pict(pic_u); + u3_mesa_line* lin_u = _mesa_get_jumbo_cache(sam_u, nam_u); + if ( NULL == lin_u ) { + return; + } + lin_u->typ_y = CTAG_BLOCK; + _mesa_put_jumbo_cache(sam_u, nam_u, lin_u); u3l_log("mesa: jumbo frame missing"); log_pact(pac_u); return; @@ -2701,7 +2708,16 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) _mesa_free_pict(pic_u); return; } - // otherwise, scry + + // if we are waiting, no-op + if ( ( NULL != lin_u ) && ( CTAG_WAIT == lin_u->typ_y )) { + _mesa_free_pict(pic_u); + return; + } + + if ( NULL == lin_u ) { u3l_log("lin_u NULL"); } + + // otherwise, if blocked or NULL scry lin_u = c3_calloc(sizeof(u3_mesa_line)); lin_u->typ_y = CTAG_WAIT; _mesa_copy_name(&lin_u->nam_u, &pac_u->pek_u.nam_u); // XX @@ -2710,7 +2726,7 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, u3_lane lan_u) u3_noun sky = _name_to_jumbo_scry(&pac_u->pek_u.nam_u); u3_noun our = u3i_chubs(2, sam_u->car_u.pir_u->who_d); u3_noun bem = u3nc(u3nt(our, u3_nul, u3nc(c3__ud, 1)), sky); - // NOTE: pic_u not freed + u3_pier_peek(sam_u->car_u.pir_u, u3_nul, u3k(u3nq(1, c3__beam, c3__ax, bem)), pic_u, _mesa_page_scry_jumbo_cb); } From 793046bf0c02ab8dcab9ea240bc7d10f954890b4 Mon Sep 17 00:00:00 2001 From: pkova Date: Mon, 21 Oct 2024 17:43:25 +0300 Subject: [PATCH 427/430] tree: restructure jet dashboard for easy upgrades --- build.zig | 1 + pkg/noun/jets/137/tree.c | 1098 ++++++++++++++++++++++++++++++++++++++ pkg/noun/jets/tree.c | 299 +---------- 3 files changed, 1103 insertions(+), 295 deletions(-) create mode 100644 pkg/noun/jets/137/tree.c diff --git a/build.zig b/build.zig index 287b1bcb38..6bc5e8d7a3 100644 --- a/build.zig +++ b/build.zig @@ -695,6 +695,7 @@ fn build_single( "jets/g/plot.c", "jets/i/lagoon.c", "jets/tree.c", + "jets/137/tree.c", "log.c", "manage.c", "nock.c", diff --git a/pkg/noun/jets/137/tree.c b/pkg/noun/jets/137/tree.c new file mode 100644 index 0000000000..fec03edf1f --- /dev/null +++ b/pkg/noun/jets/137/tree.c @@ -0,0 +1,1098 @@ +#include "c3/c3.h" +#include "jets.h" +#include "jets/w.h" + + +static c3_c* no_hashes[] = { 0 }; + +static u3j_harm _137_hex_mimes_base16_en_a[] = {{".2", u3we_en_base16}, {}}; +static u3j_harm _137_hex_mimes_base16_de_a[] = {{".2", u3we_de_base16}, {}}; +static u3j_core _137_hex_mimes_base16_d[] = + { { "en", 7, _137_hex_mimes_base16_en_a, 0, no_hashes }, + { "de", 7, _137_hex_mimes_base16_de_a, 0, no_hashes }, + {} + }; +static u3j_core _137_hex_mimes_d[] = + { { "base16", 3, 0, _137_hex_mimes_base16_d, no_hashes }, + {} + }; + +static u3j_harm _137_hex_aes_ecba_en_a[] = {{".2", u3wea_ecba_en}, {}}; +static u3j_harm _137_hex_aes_ecba_de_a[] = {{".2", u3wea_ecba_de}, {}}; +static u3j_core _137_hex_aes_ecba_d[] = + { { "en", 7, _137_hex_aes_ecba_en_a, 0, no_hashes }, + { "de", 7, _137_hex_aes_ecba_de_a, 0, no_hashes }, + {} + }; + +static u3j_harm _137_hex_aes_ecbb_en_a[] = {{".2", u3wea_ecbb_en}, {}}; +static u3j_harm _137_hex_aes_ecbb_de_a[] = {{".2", u3wea_ecbb_de}, {}}; +static u3j_core _137_hex_aes_ecbb_d[] = + { { "en", 7, _137_hex_aes_ecbb_en_a, 0, no_hashes }, + { "de", 7, _137_hex_aes_ecbb_de_a, 0, no_hashes }, + {} + }; + +static u3j_harm _137_hex_aes_ecbc_en_a[] = {{".2", u3wea_ecbc_en}, {}}; +static u3j_harm _137_hex_aes_ecbc_de_a[] = {{".2", u3wea_ecbc_de}, {}}; +static u3j_core _137_hex_aes_ecbc_d[] = + { { "en", 7, _137_hex_aes_ecbc_en_a, 0, no_hashes }, + { "de", 7, _137_hex_aes_ecbc_de_a, 0, no_hashes }, + {} + }; + +static u3j_harm _137_hex_aes_cbca_en_a[] = {{".2", u3wea_cbca_en}, {}}; +static u3j_harm _137_hex_aes_cbca_de_a[] = {{".2", u3wea_cbca_de}, {}}; +static u3j_core _137_hex_aes_cbca_d[] = + { { "en", 7, _137_hex_aes_cbca_en_a, 0, no_hashes }, + { "de", 7, _137_hex_aes_cbca_de_a, 0, no_hashes }, + {} + }; + +static u3j_harm _137_hex_aes_cbcb_en_a[] = {{".2", u3wea_cbcb_en}, {}}; +static u3j_harm _137_hex_aes_cbcb_de_a[] = {{".2", u3wea_cbcb_de}, {}}; +static u3j_core _137_hex_aes_cbcb_d[] = + { { "en", 7, _137_hex_aes_cbcb_en_a, 0, no_hashes }, + { "de", 7, _137_hex_aes_cbcb_de_a, 0, no_hashes }, + {} + }; + +static u3j_harm _137_hex_aes_cbcc_en_a[] = {{".2", u3wea_cbcc_en}, {}}; +static u3j_harm _137_hex_aes_cbcc_de_a[] = {{".2", u3wea_cbcc_de}, {}}; +static u3j_core _137_hex_aes_cbcc_d[] = + { { "en", 7, _137_hex_aes_cbcc_en_a, 0, no_hashes }, + { "de", 7, _137_hex_aes_cbcc_de_a, 0, no_hashes }, + {} + }; + +static u3j_harm _137_hex_aes_siva_en_a[] = {{".2", u3wea_siva_en}, {}}; +static u3j_harm _137_hex_aes_siva_de_a[] = {{".2", u3wea_siva_de}, {}}; +static u3j_core _137_hex_aes_siva_d[] = + { { "en", 7, _137_hex_aes_siva_en_a, 0, no_hashes }, + { "de", 7, _137_hex_aes_siva_de_a, 0, no_hashes }, + {} + }; + +static u3j_harm _137_hex_aes_sivb_en_a[] = {{".2", u3wea_sivb_en}, {}}; +static u3j_harm _137_hex_aes_sivb_de_a[] = {{".2", u3wea_sivb_de}, {}}; +static u3j_core _137_hex_aes_sivb_d[] = + { { "en", 7, _137_hex_aes_sivb_en_a, 0, no_hashes }, + { "de", 7, _137_hex_aes_sivb_de_a, 0, no_hashes }, + {} + }; + +static u3j_harm _137_hex_aes_sivc_en_a[] = {{".2", u3wea_sivc_en}, {}}; +static u3j_harm _137_hex_aes_sivc_de_a[] = {{".2", u3wea_sivc_de}, {}}; +static u3j_core _137_hex_aes_sivc_d[] = + { { "en", 7, _137_hex_aes_sivc_en_a, 0, no_hashes }, + { "de", 7, _137_hex_aes_sivc_de_a, 0, no_hashes }, + {} + }; +static u3j_core _137_hex_aes_d[] = + { { "ecba", 7, 0, _137_hex_aes_ecba_d, no_hashes }, + { "ecbb", 7, 0, _137_hex_aes_ecbb_d, no_hashes }, + { "ecbc", 7, 0, _137_hex_aes_ecbc_d, no_hashes }, + { "cbca", 7, 0, _137_hex_aes_cbca_d, no_hashes }, + { "cbcb", 7, 0, _137_hex_aes_cbcb_d, no_hashes }, + { "cbcc", 7, 0, _137_hex_aes_cbcc_d, no_hashes }, + { "siva", 7, 0, _137_hex_aes_siva_d, no_hashes }, + { "sivb", 7, 0, _137_hex_aes_sivb_d, no_hashes }, + { "sivc", 7, 0, _137_hex_aes_sivc_d, no_hashes }, + {} + }; + +static u3j_harm _137_hex_leer_a[] = {{".2", u3we_leer}, {}}; +static u3j_harm _137_hex_lore_a[] = {{".2", u3we_lore}, {}}; +static u3j_harm _137_hex_loss_a[] = {{".2", u3we_loss}, {}}; +static u3j_harm _137_hex_lune_a[] = {{".2", u3we_lune}, {}}; + + +static u3j_harm _137_hex__crc32_a[] = {{".2", u3we_crc32}, {}}; + +static u3j_core _137_hex__crc_d[] = {{"crc32", 7, _137_hex__crc32_a, 0, no_hashes }, {}}; + + +static u3j_harm _137_hex_coed__ed_puck_a[] = {{".2", u3wee_puck}, {}}; +static u3j_harm _137_hex_coed__ed_sign_octs_a[] = {{".2", u3wee_sign_octs}, {}}; +static u3j_harm _137_hex_coed__ed_veri_octs_a[] = {{".2", u3wee_veri_octs}, {}}; +static u3j_harm _137_hex_coed__ed_shar_a[] = {{".2", u3wee_shar}, {}}; + +static u3j_harm _137_hex_coed__ed_point_add_a[] = + {{".2", u3wee_point_add}, {}}; + +static u3j_harm _137_hex_coed__ed_scalarmult_a[] = + {{".2", u3wee_scalarmult}, {}}; + +static u3j_harm _137_hex_coed__ed_scalarmult_base_a[] = + {{".2", u3wee_scalarmult_base}, {}}; + +static u3j_harm _137_hex_coed__ed_add_scalarmult_scalarmult_base_a[] = + {{".2", u3wee_add_scalarmult_scalarmult_base}, {}}; + +static u3j_harm _137_hex_coed__ed_add_double_scalarmult_a[] = + {{".2", u3wee_add_double_scalarmult}, {}}; + +static u3j_core _137_hex_coed__ed_d[] = + { { "sign-octs", 7, _137_hex_coed__ed_sign_octs_a, 0, no_hashes }, + { "puck", 7, _137_hex_coed__ed_puck_a, 0, no_hashes }, + { "veri-octs", 7, _137_hex_coed__ed_veri_octs_a, 0, no_hashes }, + { "shar", 7, _137_hex_coed__ed_shar_a, 0, no_hashes }, + { "point-add", 7, _137_hex_coed__ed_point_add_a, 0, 0 }, + { "scalarmult", 7, _137_hex_coed__ed_scalarmult_a, 0, + no_hashes }, + { "scalarmult-base", 7, _137_hex_coed__ed_scalarmult_base_a, 0, + no_hashes }, + { "add-scalarmult-scalarmult-base", 7, + _137_hex_coed__ed_add_scalarmult_scalarmult_base_a, 0, + no_hashes }, + { "add-double-scalarmult", 7, + _137_hex_coed__ed_add_double_scalarmult_a, 0, + no_hashes }, + {} + }; + +static u3j_core _137_hex_coed_d[] = + { { "ed", 3, 0, _137_hex_coed__ed_d, no_hashes }, + {} + }; + +static u3j_harm _137_hex_hmac_hmac_a[] = {{".2", u3we_hmac}, {}}; +static u3j_core _137_hex_hmac_d[] = + { { "hmac", 7, _137_hex_hmac_hmac_a, 0, no_hashes }, + {} + }; + +static u3j_harm _137_hex_argon2_a[] = {{".2", u3we_argon2}, {}}; +static u3j_core _137_hex_argon_d[] = + { { "argon2", 511, _137_hex_argon2_a, 0, no_hashes }, + {} + }; + +static u3j_harm _137_hex_scr_pbk_a[] = {{".2", u3wes_pbk, c3y}, {}}; +static u3j_harm _137_hex_scr_pbl_a[] = {{".2", u3wes_pbl, c3y}, {}}; +static u3j_harm _137_hex_scr_hsh_a[] = {{".2", u3wes_hsh, c3y}, {}}; +static u3j_harm _137_hex_scr_hsl_a[] = {{".2", u3wes_hsl, c3y}, {}}; +static u3j_core _137_hex_scr_d[] = + { { "pbk", 7, _137_hex_scr_pbk_a, 0, no_hashes }, + { "pbl", 7, _137_hex_scr_pbl_a, 0, no_hashes }, + { "hsh", 7, _137_hex_scr_hsh_a, 0, no_hashes }, + { "hsl", 7, _137_hex_scr_hsl_a, 0, no_hashes }, + {} + }; + +static u3j_harm _137_hex_secp_secp256k1_make_a[] = {{".2", u3we_make, c3y}, {}}; +static u3j_harm _137_hex_secp_secp256k1_sign_a[] = {{".2", u3we_sign, c3y}, {}}; +static u3j_harm _137_hex_secp_secp256k1_reco_a[] = {{".2", u3we_reco, c3y}, {}}; + +static u3j_harm _137_hex_secp_secp256k1_schnorr_sosi_a[] = + {{".2", u3we_sosi}, {}}; +static u3j_harm _137_hex_secp_secp256k1_schnorr_sove_a[] = + {{".2", u3we_sove}, {}}; +static u3j_core _137_hex_secp_secp256k1_schnorr_d[] = + { { "sosi", 7, + _137_hex_secp_secp256k1_schnorr_sosi_a, 0, + no_hashes }, + { "sove", 7, + _137_hex_secp_secp256k1_schnorr_sove_a, 0, + no_hashes }, + {} + }; + +static u3j_core _137_hex_secp_secp256k1_d[] = + { { "make", 7, _137_hex_secp_secp256k1_make_a, 0, no_hashes }, + { "sign", 7, _137_hex_secp_secp256k1_sign_a, 0, no_hashes }, + { "reco", 7, _137_hex_secp_secp256k1_reco_a, 0, no_hashes }, + { "schnorr", 7, 0, + _137_hex_secp_secp256k1_schnorr_d, + no_hashes }, + {} + }; +static u3j_core _137_hex_secp_d[] = + { { "secp256k1", 3, 0, _137_hex_secp_secp256k1_d, no_hashes }, + {} + }; + + +static u3j_harm _137_hex_kecc_k224_a[] = + {{".2", u3we_kecc224, c3y, c3y, c3y}, {}}; +static u3j_harm _137_hex_kecc_k256_a[] = + {{".2", u3we_kecc256, c3y, c3y, c3y}, {}}; +static u3j_harm _137_hex_kecc_k384_a[] = + {{".2", u3we_kecc384, c3y, c3y, c3y}, {}}; +static u3j_harm _137_hex_kecc_k512_a[] = + {{".2", u3we_kecc512, c3y, c3y, c3y}, {}}; +static u3j_core _137_hex_kecc_d[] = + { { "k224", 7, _137_hex_kecc_k224_a, 0, no_hashes }, + { "k256", 7, _137_hex_kecc_k256_a, 0, no_hashes }, + { "k384", 7, _137_hex_kecc_k384_a, 0, no_hashes }, + { "k512", 7, _137_hex_kecc_k512_a, 0, no_hashes }, + {} + }; + +static u3j_harm _137_hex_ripemd_160_a[] = {{".2", u3we_ripe, c3y}, {}}; +static u3j_core _137_hex_ripe_d[] = + { { "ripemd160", 7, _137_hex_ripemd_160_a, 0, no_hashes }, + {} + }; + + + +/* layer five + */ +static u3j_harm _137_pen_cell_a[] = {{".2", u3wf_cell}, {}}; +static u3j_harm _137_pen_comb_a[] = {{".2", u3wf_comb}, {}}; +static u3j_harm _137_pen_cons_a[] = {{".2", u3wf_cons}, {}}; +static u3j_harm _137_pen_core_a[] = {{".2", u3wf_core}, {}}; +static u3j_harm _137_pen_face_a[] = {{".2", u3wf_face}, {}}; +static u3j_harm _137_pen_fitz_a[] = {{".2", u3wf_fitz}, {}}; +static u3j_harm _137_pen_fork_a[] = {{".2", u3wf_fork}, {}}; + +static u3j_harm _137_pen_look_a[] = {{".2", u3wf_look}, {}}; +static u3j_harm _137_pen_loot_a[] = {{".2", u3wf_loot}, {}}; + +static u3j_harm _137_pen__ut_crop_a[] = {{".2", u3wfu_crop}, {}}; +static u3j_harm _137_pen__ut_fish_a[] = {{".2", u3wfu_fish}, {}}; +static u3j_harm _137_pen__ut_fuse_a[] = {{".2", u3wfu_fuse}, {}}; +static u3j_harm _137_pen__ut_mint_a[] = {{".2", u3wfu_mint}, {}}; +static u3j_harm _137_pen__ut_mull_a[] = {{".2", u3wfu_mull}, {}}; + +static u3j_harm _137_pen__ut_nest_dext_a[] = {{".2", u3wfu_nest_dext}, {}}; +static u3j_core _137_pen__ut_nest_in_d[] = + { + { "nest-dext", 3, _137_pen__ut_nest_dext_a, 0, no_hashes }, + {} + }; +static u3j_core _137_pen__ut_nest_d[] = + { + { "nest-in", 7, 0, _137_pen__ut_nest_in_d, no_hashes }, + {} + }; + +static u3j_harm _137_pen__ut_rest_a[] = {{".2", u3wfu_rest}, {}}; + +static u3j_core _137_pen__ut_d[] = + { + { "crop", 7, _137_pen__ut_crop_a, 0, no_hashes }, + { "fish", 7, _137_pen__ut_fish_a, 0, no_hashes }, + { "fuse", 7, _137_pen__ut_fuse_a, 0, no_hashes }, + { "mint", 7, _137_pen__ut_mint_a, 0, no_hashes }, + { "mull", 7, _137_pen__ut_mull_a, 0, no_hashes }, + { "nest", 7, 0, _137_pen__ut_nest_d, no_hashes }, + { "rest", 7, _137_pen__ut_rest_a, 0, no_hashes }, + {} + }; + +static u3j_hood _137_pen__ut_ho[] = + { { "ar", 12282 }, + { "fan", 28, c3n }, + { "rib", 58, c3n }, + { "vet", 59, c3n }, + + { "blow", 6015 }, + { "burp", 342 }, + { "busk", 1373 }, + { "buss", 374 }, + { "crop", 1494 }, + { "duck", 1524 }, + { "dune", 2991 }, + { "dunk", 3066 }, + { "epla", 12206 }, + { "emin", 1534 }, + { "emul", 6134 }, + { "feel", 1502 }, + { "felt", 94 }, + { "fine", 49086 }, + { "fire", 4 }, + { "fish", 6006 }, + { "fond", 12283 }, + { "fund", 6014 }, + // XX +funk is not part of +ut, and this hook appears to be unused + // remove from here and the +ut hint + // + { "funk", 0xbefafa, c3y, 31 }, + { "fuse", 24021 }, + { "gain", 380 }, + { "lose", 0x2fefe }, + { "mile", 382 }, + { "mine", 372 }, + { "mint", 49083 }, + { "moot", 0x2feff }, + { "mull", 24020 }, + { "nest", 92 }, + { "peel", 1526 }, + { "play", 3006 }, + { "peek", 1532 }, + { "repo", 22 }, + { "rest", 6102 }, + { "tack", 6007 }, + { "toss", 24540 }, + { "wrap", 6137 }, + {}, + }; + + +static u3j_hood _137_pen_ho[] = { + { "ap", 22 }, + { "ut", 86 }, + {}, +}; + + +/* layer four + */ +static u3j_harm _137_qua_trip_a[] = {{".2", u3we_trip}, {}}; + +static u3j_harm _137_qua_slaw_a[] = {{".2", u3we_slaw}, {}}; +static u3j_harm _137_qua_scot_a[] = {{".2", u3we_scot}, {}}; +static u3j_harm _137_qua_scow_a[] = {{".2", u3we_scow}, {}}; + +static u3j_harm _137_qua__po_ind_a[] = {{".2", u3wcp_ind}, {}}; +static u3j_harm _137_qua__po_ins_a[] = {{".2", u3wcp_ins}, {}}; +static u3j_harm _137_qua__po_tod_a[] = {{".2", u3wcp_tod}, {}}; +static u3j_harm _137_qua__po_tos_a[] = {{".2", u3wcp_tos}, {}}; +static u3j_core _137_qua__po_d[] = + { { "ind", 7, _137_qua__po_ind_a, 0, no_hashes }, + { "ins", 7, _137_qua__po_ins_a, 0, no_hashes }, + { "tod", 7, _137_qua__po_tod_a, 0, no_hashes }, + { "tos", 7, _137_qua__po_tos_a, 0, no_hashes }, + {} + }; + +static u3j_harm _137_qua__bend_fun_a[] = {{".2", u3we_bend_fun}, {}}; +static u3j_core _137_qua__bend_d[] = + { { "fun", 7, _137_qua__bend_fun_a, 0, no_hashes }, + {} + }; + +static u3j_harm _137_qua__cold_fun_a[] = {{".2", u3we_cold_fun}, {}}; +static u3j_core _137_qua__cold_d[] = + { { "fun", 7, _137_qua__cold_fun_a, 0, no_hashes }, + {} + }; + +static u3j_harm _137_qua__cook_fun_a[] = {{".2", u3we_cook_fun}, {}}; +static u3j_core _137_qua__cook_d[] = + { { "fun", 7, _137_qua__cook_fun_a, 0, no_hashes }, + {} + }; + +static u3j_harm _137_qua__comp_fun_a[] = {{".2", u3we_comp_fun}, {}}; +static u3j_core _137_qua__comp_d[] = + { { "fun", 7, _137_qua__comp_fun_a, 0, no_hashes }, + {} + }; + +static u3j_harm _137_qua__easy_fun_a[] = {{".2", u3we_easy_fun}, {}}; +static u3j_core _137_qua__easy_d[] = + { { "fun", 7, _137_qua__easy_fun_a, 0, no_hashes }, + {} + }; + +static u3j_harm _137_qua__glue_fun_a[] = {{".2", u3we_glue_fun}, {}}; +static u3j_core _137_qua__glue_d[] = + { { "fun", 7, _137_qua__glue_fun_a, 0, no_hashes }, + {} + }; + +static u3j_harm _137_qua__here_fun_a[] = {{".2", u3we_here_fun}, {}}; +static u3j_core _137_qua__here_d[] = + { { "fun", 7, _137_qua__here_fun_a, 0, no_hashes }, + {} + }; + +static u3j_harm _137_qua__just_fun_a[] = {{".2", u3we_just_fun}, {}}; +static u3j_core _137_qua__just_d[] = + { { "fun", 7, _137_qua__just_fun_a, 0, no_hashes }, + {} + }; + +static u3j_harm _137_qua__mask_fun_a[] = {{".2", u3we_mask_fun}, {}}; +static u3j_core _137_qua__mask_d[] = + { { "fun", 7, _137_qua__mask_fun_a, 0, no_hashes }, + {} + }; + +static u3j_harm _137_qua__shim_fun_a[] = {{".2", u3we_shim_fun}, {}}; +static u3j_core _137_qua__shim_d[] = + { { "fun", 7, _137_qua__shim_fun_a, 0, no_hashes }, + {} + }; + +static u3j_harm _137_qua__stag_fun_a[] = {{".2", u3we_stag_fun}, {}}; +static u3j_core _137_qua__stag_d[] = + { { "fun", 7, _137_qua__stag_fun_a, 0, no_hashes }, + {} + }; + +static u3j_harm _137_qua__stew_fun_a[] = {{".2", u3we_stew_fun}, {}}; +static u3j_core _137_qua__stew_d[] = + { { "fun", 31, _137_qua__stew_fun_a, 0, no_hashes }, + {} + }; + +static u3j_harm _137_qua__stir_fun_a[] = {{".2", u3we_stir_fun}, {}}; +static u3j_core _137_qua__stir_d[] = + { { "fun", 7, _137_qua__stir_fun_a, 0, no_hashes }, + {} + }; + +static u3j_harm _137_qua_pfix_a[] = {{".2", u3we_pfix}, {}}; + +static u3j_harm _137_qua_plug_a[] = {{".2", u3we_plug}, {}}; +static u3j_harm _137_qua_pose_a[] = {{".2", u3we_pose}, {}}; + +static u3j_harm _137_qua_sfix_a[] = {{".2", u3we_sfix}, {}}; + +static u3j_harm _137_qua_mink_a[] = {{".2", u3we_mink}, {}}; +static u3j_harm _137_qua_mole_a[] = {{".2", u3we_mole}, {}}; +static u3j_harm _137_qua_mule_a[] = {{".2", u3we_mule}, {}}; + + +static u3j_hood _137_qua_ho[] = { + { "mute", 0x2fbabe }, + { "show", 24406 }, + { "mure", 1374 }, + {}, +}; + + +/* layer three + */ +static u3j_harm _137_tri__cofl__drg_a[] = {{".2", u3wef_drg}, {}}; +static u3j_harm _137_tri__cofl__lug_a[] = {{".2", u3wef_lug}, {}}; +static u3j_core _137_tri__cofl_d[] = + { { "drg", 7, _137_tri__cofl__drg_a, 0, no_hashes }, + { "lug", 7, _137_tri__cofl__lug_a, 0, no_hashes }, + {} + }; + +static u3j_harm _137_tri__rd_add_a[] = {{".2", u3wer_add}, {}}; +static u3j_harm _137_tri__rd_sub_a[] = {{".2", u3wer_sub}, {}}; +static u3j_harm _137_tri__rd_mul_a[] = {{".2", u3wer_mul}, {}}; +static u3j_harm _137_tri__rd_div_a[] = {{".2", u3wer_div}, {}}; +static u3j_harm _137_tri__rd_sqt_a[] = {{".2", u3wer_sqt}, {}}; +static u3j_harm _137_tri__rd_fma_a[] = {{".2", u3wer_fma}, {}}; +static u3j_harm _137_tri__rd_lth_a[] = {{".2", u3wer_lth}, {}}; +static u3j_harm _137_tri__rd_lte_a[] = {{".2", u3wer_lte}, {}}; +static u3j_harm _137_tri__rd_equ_a[] = {{".2", u3wer_equ}, {}}; +static u3j_harm _137_tri__rd_gte_a[] = {{".2", u3wer_gte}, {}}; +static u3j_harm _137_tri__rd_gth_a[] = {{".2", u3wer_gth}, {}}; +static u3j_core _137_tri__rd_d[] = + { { "add", 7, _137_tri__rd_add_a, 0, no_hashes }, + { "sub", 7, _137_tri__rd_sub_a, 0, no_hashes }, + { "mul", 7, _137_tri__rd_mul_a, 0, no_hashes }, + { "div", 7, _137_tri__rd_div_a, 0, no_hashes }, + { "sqt", 7, _137_tri__rd_sqt_a, 0, no_hashes }, + { "fma", 7, _137_tri__rd_fma_a, 0, no_hashes }, + { "lth", 7, _137_tri__rd_lth_a, 0, no_hashes }, + { "lte", 7, _137_tri__rd_lte_a, 0, no_hashes }, + { "equ", 7, _137_tri__rd_equ_a, 0, no_hashes }, + { "gte", 7, _137_tri__rd_gte_a, 0, no_hashes }, + { "gth", 7, _137_tri__rd_gth_a, 0, no_hashes }, + {} + }; + +static u3j_harm _137_tri__rs_add_a[] = {{".2", u3wet_add}, {}}; +static u3j_harm _137_tri__rs_sub_a[] = {{".2", u3wet_sub}, {}}; +static u3j_harm _137_tri__rs_mul_a[] = {{".2", u3wet_mul}, {}}; +static u3j_harm _137_tri__rs_div_a[] = {{".2", u3wet_div}, {}}; +static u3j_harm _137_tri__rs_sqt_a[] = {{".2", u3wet_sqt}, {}}; +static u3j_harm _137_tri__rs_fma_a[] = {{".2", u3wet_fma}, {}}; +static u3j_harm _137_tri__rs_lth_a[] = {{".2", u3wet_lth}, {}}; +static u3j_harm _137_tri__rs_lte_a[] = {{".2", u3wet_lte}, {}}; +static u3j_harm _137_tri__rs_equ_a[] = {{".2", u3wet_equ}, {}}; +static u3j_harm _137_tri__rs_gte_a[] = {{".2", u3wet_gte}, {}}; +static u3j_harm _137_tri__rs_gth_a[] = {{".2", u3wet_gth}, {}}; +static u3j_core _137_tri__rs_d[] = + { { "add", 7, _137_tri__rs_add_a, 0, no_hashes }, + { "sub", 7, _137_tri__rs_sub_a, 0, no_hashes }, + { "mul", 7, _137_tri__rs_mul_a, 0, no_hashes }, + { "div", 7, _137_tri__rs_div_a, 0, no_hashes }, + { "sqt", 7, _137_tri__rs_sqt_a, 0, no_hashes }, + { "fma", 7, _137_tri__rs_fma_a, 0, no_hashes }, + { "lth", 7, _137_tri__rs_lth_a, 0, no_hashes }, + { "lte", 7, _137_tri__rs_lte_a, 0, no_hashes }, + { "equ", 7, _137_tri__rs_equ_a, 0, no_hashes }, + { "gte", 7, _137_tri__rs_gte_a, 0, no_hashes }, + { "gth", 7, _137_tri__rs_gth_a, 0, no_hashes }, + {} + }; + +static u3j_harm _137_tri__rq_add_a[] = {{".2", u3weq_add}, {}}; +static u3j_harm _137_tri__rq_sub_a[] = {{".2", u3weq_sub}, {}}; +static u3j_harm _137_tri__rq_mul_a[] = {{".2", u3weq_mul}, {}}; +static u3j_harm _137_tri__rq_div_a[] = {{".2", u3weq_div}, {}}; +static u3j_harm _137_tri__rq_sqt_a[] = {{".2", u3weq_sqt}, {}}; +static u3j_harm _137_tri__rq_fma_a[] = {{".2", u3weq_fma}, {}}; +static u3j_harm _137_tri__rq_lth_a[] = {{".2", u3weq_lth}, {}}; +static u3j_harm _137_tri__rq_lte_a[] = {{".2", u3weq_lte}, {}}; +static u3j_harm _137_tri__rq_equ_a[] = {{".2", u3weq_equ}, {}}; +static u3j_harm _137_tri__rq_gte_a[] = {{".2", u3weq_gte}, {}}; +static u3j_harm _137_tri__rq_gth_a[] = {{".2", u3weq_gth}, {}}; +static u3j_core _137_tri__rq_d[] = + { { "add", 7, _137_tri__rq_add_a, 0, no_hashes }, + { "sub", 7, _137_tri__rq_sub_a, 0, no_hashes }, + { "mul", 7, _137_tri__rq_mul_a, 0, no_hashes }, + { "div", 7, _137_tri__rq_div_a, 0, no_hashes }, + { "sqt", 7, _137_tri__rq_sqt_a, 0, no_hashes }, + { "fma", 7, _137_tri__rq_fma_a, 0, no_hashes }, + { "lth", 7, _137_tri__rq_lth_a, 0, no_hashes }, + { "lte", 7, _137_tri__rq_lte_a, 0, no_hashes }, + { "equ", 7, _137_tri__rq_equ_a, 0, no_hashes }, + { "gte", 7, _137_tri__rq_gte_a, 0, no_hashes }, + { "gth", 7, _137_tri__rq_gth_a, 0, no_hashes }, + {} + }; + +static u3j_harm _137_tri__rh_add_a[] = {{".2", u3wes_add}, {}}; +static u3j_harm _137_tri__rh_sub_a[] = {{".2", u3wes_sub}, {}}; +static u3j_harm _137_tri__rh_mul_a[] = {{".2", u3wes_mul}, {}}; +static u3j_harm _137_tri__rh_div_a[] = {{".2", u3wes_div}, {}}; +static u3j_harm _137_tri__rh_sqt_a[] = {{".2", u3wes_sqt}, {}}; +static u3j_harm _137_tri__rh_fma_a[] = {{".2", u3wes_fma}, {}}; +static u3j_harm _137_tri__rh_lth_a[] = {{".2", u3wes_lth}, {}}; +static u3j_harm _137_tri__rh_lte_a[] = {{".2", u3wes_lte}, {}}; +static u3j_harm _137_tri__rh_equ_a[] = {{".2", u3wes_equ}, {}}; +static u3j_harm _137_tri__rh_gte_a[] = {{".2", u3wes_gte}, {}}; +static u3j_harm _137_tri__rh_gth_a[] = {{".2", u3wes_gth}, {}}; +static u3j_core _137_tri__rh_d[] = + { { "add", 7, _137_tri__rh_add_a, 0, no_hashes }, + { "sub", 7, _137_tri__rh_sub_a, 0, no_hashes }, + { "mul", 7, _137_tri__rh_mul_a, 0, no_hashes }, + { "div", 7, _137_tri__rh_div_a, 0, no_hashes }, + { "sqt", 7, _137_tri__rh_sqt_a, 0, no_hashes }, + { "fma", 7, _137_tri__rh_fma_a, 0, no_hashes }, + { "lth", 7, _137_tri__rh_lth_a, 0, no_hashes }, + { "lte", 7, _137_tri__rh_lte_a, 0, no_hashes }, + { "equ", 7, _137_tri__rh_equ_a, 0, no_hashes }, + { "gte", 7, _137_tri__rh_gte_a, 0, no_hashes }, + { "gth", 7, _137_tri__rh_gth_a, 0, no_hashes }, + {} + }; + +static u3j_harm _137_tri__og_raw_a[] = {{".2", u3weo_raw}, {}}; +static u3j_core _137_tri__og_d[] = + { { "raw", 7, _137_tri__og_raw_a, 0, no_hashes }, + {} + }; + +static u3j_harm _137_tri__sha_sha1_a[] = {{".2", u3we_sha1}, {}}; +static u3j_core _137_tri__sha_d[] = + { { "sha1", 7, _137_tri__sha_sha1_a, 0, no_hashes }, + {} + }; + +static u3j_harm _137_tri_shax_a[] = {{".2", u3we_shax}, {}}; +static u3j_harm _137_tri_shay_a[] = {{".2", u3we_shay}, {}}; +static u3j_harm _137_tri_shas_a[] = {{".2", u3we_shas}, {}}; +static u3j_harm _137_tri_shal_a[] = {{".2", u3we_shal}, {}}; + +static u3j_harm _137_ob_fynd_a[] = {{".2", u3we_fynd_ob}, {}}; +static u3j_harm _137_ob_fein_a[] = {{".2", u3we_fein_ob}, {}}; +static u3j_core _137_ob_d[] = { + { "fein", 7, _137_ob_fein_a, 0, no_hashes }, + { "fynd", 7, _137_ob_fynd_a, 0, no_hashes }, + {} +}; +static u3j_hood _137_ob_ho[] = { + { "fein", 42 }, + { "fynd", 20 }, + {}, +}; + + +static u3j_hood _137_tri_ho[] = { + { "ob", 20 }, + { "yore", 5462 }, + { "year", 44975 }, + {}, +}; + + +/* layer two + */ +static u3j_harm _137_two_find_a[] = {{".2", u3wb_find, c3y}, {}}; +static u3j_harm _137_two_flop_a[] = {{".2", u3wb_flop, c3y}, {}}; +static u3j_harm _137_two_lent_a[] = {{".2", u3wb_lent, c3y}, {}}; +static u3j_harm _137_two_levy_a[] = {{".2", u3wb_levy, c3y}, {}}; +static u3j_harm _137_two_lien_a[] = {{".2", u3wb_lien, c3y}, {}}; +static u3j_harm _137_two_murn_a[] = {{".2", u3wb_murn, c3y}, {}}; +static u3j_harm _137_two_need_a[] = {{".2", u3wb_need, c3y}, {}}; +static u3j_harm _137_two_reap_a[] = {{".2", u3wb_reap, c3y}, {}}; +static u3j_harm _137_two_reel_a[] = {{".2", u3wb_reel, c3y}, {}}; +static u3j_harm _137_two_roll_a[] = {{".2", u3wb_roll, c3y}, {}}; +static u3j_harm _137_two_skid_a[] = {{".2", u3wb_skid, c3y}, {}}; +static u3j_harm _137_two_skim_a[] = {{".2", u3wb_skim, c3y}, {}}; +static u3j_harm _137_two_skip_a[] = {{".2", u3wb_skip, c3y}, {}}; +static u3j_harm _137_two_scag_a[] = {{".2", u3wb_scag, c3y}, {}}; +static u3j_harm _137_two_slag_a[] = {{".2", u3wb_slag, c3y}, {}}; +static u3j_harm _137_two_snag_a[] = {{".2", u3wb_snag, c3y}, {}}; +static u3j_harm _137_two_sort_a[] = {{".2", u3wb_sort, c3y}, {}}; +static u3j_harm _137_two_turn_a[] = {{".2", u3wb_turn, c3y}, {}}; +static u3j_harm _137_two_weld_a[] = {{".2", u3wb_weld, c3y}, {}}; +static u3j_harm _137_two_welp_a[] = {{".2", u3wb_welp, c3y}, {}}; +static u3j_harm _137_two_zing_a[] = {{".2", u3wb_zing, c3y}, {}}; + +static u3j_harm _137_two_bex_a[] = {{".2", u3wc_bex, c3y}, {}}; +static u3j_harm _137_two_can_a[] = {{".2", u3wc_can, c3y}, {}}; +static u3j_harm _137_two_cat_a[] = {{".2", u3wc_cat, c3y}, {}}; +static u3j_harm _137_two_con_a[] = {{".2", u3wc_con, c3y}, {}}; +static u3j_harm _137_two_cut_a[] = {{".2", u3wc_cut, c3y}, {}}; +static u3j_harm _137_two_dis_a[] = {{".2", u3wc_dis, c3y}, {}}; +static u3j_harm _137_two_dor_a[] = {{".2", u3wc_dor, c3y}, {}}; +static u3j_harm _137_two_end_a[] = {{".2", u3wc_end, c3y}, {}}; +static u3j_harm _137_two_gor_a[] = {{".2", u3wc_gor, c3y}, {}}; +static u3j_harm _137_two_lsh_a[] = {{".2", u3wc_lsh, c3y}, {}}; +static u3j_harm _137_two_met_a[] = {{".2", u3wc_met, c3y}, {}}; +static u3j_harm _137_two_mix_a[] = {{".2", u3wc_mix, c3y}, {}}; +static u3j_harm _137_two_mor_a[] = {{".2", u3wc_mor, c3y}, {}}; +static u3j_harm _137_two_mug_a[] = {{".2", u3wc_mug, c3y}, {}}; +static u3j_harm _137_two_muk_a[] = {{".2", u3wc_muk, c3y}, {}}; +static u3j_harm _137_two_pow_a[] = {{".2", u3wc_pow, c3y}, {}}; +static u3j_harm _137_two_rap_a[] = {{".2", u3wc_rap, c3y}, {}}; +static u3j_harm _137_two_rep_a[] = {{".2", u3wc_rep, c3y}, {}}; +static u3j_harm _137_two_rev_a[] = {{".2", u3wc_rev, c3y}, {}}; +static u3j_harm _137_two_rip_a[] = {{".2", u3wc_rip, c3y}, {}}; +static u3j_harm _137_two_rsh_a[] = {{".2", u3wc_rsh, c3y}, {}}; +static u3j_harm _137_two_swp_a[] = {{".2", u3wc_swp, c3y}, {}}; +static u3j_harm _137_two_sqt_a[] = {{".2", u3wc_sqt, c3y}, {}}; +static u3j_harm _137_two_xeb_a[] = {{".2", u3wc_xeb, c3y}, {}}; + +static u3j_harm _137_two__in_bif_a[] = {{".2", u3wdi_bif}, {}}; +static u3j_harm _137_two__in_del_a[] = {{".2", u3wdi_del}, {}}; +static u3j_harm _137_two__in_dif_a[] = {{".2", u3wdi_dif}, {}}; +static u3j_harm _137_two__in_gas_a[] = {{".2", u3wdi_gas}, {}}; +static u3j_harm _137_two__in_has_a[] = {{".2", u3wdi_has}, {}}; +static u3j_harm _137_two__in_int_a[] = {{".2", u3wdi_int}, {}}; +static u3j_harm _137_two__in_put_a[] = {{".2", u3wdi_put}, {}}; +static u3j_harm _137_two__in_rep_a[] = {{".2", u3wdi_rep}, {}}; +static u3j_harm _137_two__in_run_a[] = {{".2", u3wdi_run}, {}}; +static u3j_harm _137_two__in_tap_a[] = {{".2", u3wdi_tap}, {}}; +static u3j_harm _137_two__in_wyt_a[] = {{".2", u3wdi_wyt}, {}}; +static u3j_harm _137_two__in_uni_a[] = {{".2", u3wdi_uni}, {}}; + +static u3j_harm _137_two__by_all_a[] = {{".2", u3wdb_all, c3y}, {}}; +static u3j_harm _137_two__by_any_a[] = {{".2", u3wdb_any, c3y}, {}}; +static u3j_harm _137_two__by_apt_a[] = {{".2", u3wdb_apt, c3y}, {}}; + +static u3j_harm _137_two__by_del_a[] = {{".2", u3wdb_del, c3y}, {}}; +static u3j_harm _137_two__by_dif_a[] = {{".2", u3wdb_dif, c3y}, {}}; +static u3j_harm _137_two__by_gas_a[] = {{".2", u3wdb_gas, c3y}, {}}; +static u3j_harm _137_two__by_get_a[] = {{".2", u3wdb_get, c3y}, {}}; +static u3j_harm _137_two__by_has_a[] = {{".2", u3wdb_has, c3y}, {}}; +static u3j_harm _137_two__by_int_a[] = {{".2", u3wdb_int, c3y}, {}}; +static u3j_harm _137_two__by_jab_a[] = {{".2", u3wdb_jab, c3y}, {}}; +static u3j_harm _137_two__by_key_a[] = {{".2", u3wdb_key, c3y}, {}}; +static u3j_harm _137_two__by_put_a[] = {{".2", u3wdb_put, c3y}, {}}; +static u3j_harm _137_two__by_rep_a[] = {{".2", u3wdb_rep, c3y}, {}}; +static u3j_harm _137_two__by_run_a[] = {{".2", u3wdb_run, c3y}, {}}; +static u3j_harm _137_two__by_tap_a[] = {{".2", u3wdb_tap, c3y}, {}}; +static u3j_harm _137_two__by_uni_a[] = {{".2", u3wdb_uni, c3y}, {}}; +static u3j_harm _137_two__by_urn_a[] = {{".2", u3wdb_urn, c3y}, {}}; +static u3j_harm _137_two__by_wyt_a[] = {{".2", u3wdb_wyt, c3y}, {}}; + +static u3j_harm _137_two_cue_a[] = {{".2", u3we_cue}, {}}; +static u3j_harm _137_two_jam_a[] = {{".2", u3we_jam}, {}}; +static u3j_harm _137_two_mat_a[] = {{".2", u3we_mat}, {}}; +static u3j_harm _137_two_rub_a[] = {{".2", u3we_rub}, {}}; + + + +/* layer one + */ +static u3j_harm _137_one_add_a[] = {{".2", u3wa_add, c3y}, {}}; +static u3j_harm _137_one_dec_a[] = {{".2", u3wa_dec, c3y}, {}}; +static u3j_harm _137_one_div_a[] = {{".2", u3wa_div, c3y}, {}}; +static u3j_harm _137_one_dvr_a[] = {{".2", u3wc_dvr, c3y}, {}}; +static u3j_harm _137_one_gte_a[] = {{".2", u3wa_gte, c3y}, {}}; +static u3j_harm _137_one_gth_a[] = {{".2", u3wa_gth, c3y}, {}}; +static u3j_harm _137_one_lte_a[] = {{".2", u3wa_lte, c3y}, {}}; +static u3j_harm _137_one_lth_a[] = {{".2", u3wa_lth, c3y}, {}}; +static u3j_harm _137_one_max_a[] = {{".2", u3wa_max, c3y}, {}}; +static u3j_harm _137_one_min_a[] = {{".2", u3wa_min, c3y}, {}}; +static u3j_harm _137_one_mod_a[] = {{".2", u3wa_mod, c3y}, {}}; +static u3j_harm _137_one_mul_a[] = {{".2", u3wa_mul, c3y}, {}}; +static u3j_harm _137_one_sub_a[] = {{".2", u3wa_sub, c3y}, {}}; + +static u3j_harm _137_one_cap_a[] = {{".2", u3wc_cap, c3y}, {}}; +static u3j_harm _137_one_peg_a[] = {{".2", u3wc_peg, c3y}, {}}; +static u3j_harm _137_one_mas_a[] = {{".2", u3wc_mas, c3y}, {}}; + +static u3j_harm _137_lull_plot_fax_a[] = {{".2", u3wg_plot_fax, c3y}, {}}; +static u3j_harm _137_lull_plot_met_a[] = {{".2", u3wg_plot_met, c3y}, {}}; + +static u3j_core _137_lull_plot_d[] = + { { "fax", 7, _137_lull_plot_fax_a, 0, no_hashes }, + { "met", 7, _137_lull_plot_met_a, 0, no_hashes }, + {} + }; + +static u3j_core _137_lull_d[] = + { { "plot", 31, 0, _137_lull_plot_d, no_hashes }, + {} + }; + +static u3j_harm _137_hex_blake3_hash_a[] = {{".2", u3we_blake3_hash, c3y}, {}}; +static u3j_harm _137_hex_blake3_compress_a[] = {{".2", u3we_blake3_compress, c3y}, {}}; +static u3j_harm _137_hex_blake3_chunk_output_a[] = {{".2", u3we_blake3_chunk_output, c3y}, {}}; + +static u3j_core _137_hex_blake3_d[] = + { { "hash", 7, _137_hex_blake3_hash_a, 0, no_hashes }, + { "chunk-output", 7, _137_hex_blake3_chunk_output_a, 0, no_hashes }, + {} + }; + + +static u3j_core _137_hex_blake3_impl_d[] = + { { "compress", 7, _137_hex_blake3_compress_a, 0, no_hashes }, + { "blake3", 7, 0, _137_hex_blake3_d, no_hashes }, + {} + }; + +static u3j_harm _137_hex_blake2b_a[] = {{".2", u3we_blake2b, c3y}, {}}; + +static u3j_core _137_hex_blake_d[] = + { { "blake2b", 7, _137_hex_blake2b_a, 0, no_hashes }, + { "blake3-impl", 7, 0, _137_hex_blake3_impl_d, no_hashes }, + {} + }; + + +static u3j_harm _137_hex_chacha_crypt_a[] = {{".2", u3we_chacha_crypt, c3y}, {}}; +static u3j_harm _137_hex_chacha_xchacha_a[] = {{".2", u3we_chacha_xchacha, c3y}, {}}; +static u3j_core _137_hex_chacha_d[] = + { { "crypt", 7, _137_hex_chacha_crypt_a, 0, no_hashes }, + { "xchacha", 7, _137_hex_chacha_xchacha_a, 0, no_hashes }, + {} + }; + +static u3j_harm _137_hex_json_de_a[] = {{".2", u3we_json_de}, {}}; +static u3j_harm _137_hex_json_en_a[] = {{".2", u3we_json_en}, {}}; +static u3j_core _137_hex_json_d[] = + { { "de", 15, _137_hex_json_de_a, 0, no_hashes }, + { "en", 15, _137_hex_json_en_a, 0, no_hashes }, + {} + }; + +/* /lib jets in non core +*/ +static u3j_harm _137_non__lagoon_add_a[] = {{".2", u3wi_la_add}, {}}; +static u3j_harm _137_non__lagoon_sub_a[] = {{".2", u3wi_la_sub}, {}}; +static u3j_harm _137_non__lagoon_mul_a[] = {{".2", u3wi_la_mul}, {}}; +static u3j_harm _137_non__lagoon_div_a[] = {{".2", u3wi_la_div}, {}}; +static u3j_harm _137_non__lagoon_mod_a[] = {{".2", u3wi_la_mod}, {}}; +static u3j_harm _137_non__lagoon_adds_a[] = {{".2", u3wi_la_adds}, {}}; +static u3j_harm _137_non__lagoon_subs_a[] = {{".2", u3wi_la_subs}, {}}; +static u3j_harm _137_non__lagoon_muls_a[] = {{".2", u3wi_la_muls}, {}}; +static u3j_harm _137_non__lagoon_divs_a[] = {{".2", u3wi_la_divs}, {}}; +static u3j_harm _137_non__lagoon_mods_a[] = {{".2", u3wi_la_mods}, {}}; +static u3j_harm _137_non__lagoon_dot_a[] = {{".2", u3wi_la_dot}, {}}; +static u3j_harm _137_non__lagoon_trans_a[] ={{".2", u3wi_la_transpose}, {}}; +static u3j_harm _137_non__lagoon_cumsum_a[]={{".2", u3wi_la_cumsum}, {}}; +static u3j_harm _137_non__lagoon_argmin_a[]={{".2", u3wi_la_argmin}, {}}; +static u3j_harm _137_non__lagoon_argmax_a[]={{".2", u3wi_la_argmax}, {}}; +static u3j_harm _137_non__lagoon_ravel_a[]={{".2", u3wi_la_ravel}, {}}; +static u3j_harm _137_non__lagoon_min_a[] = {{".2", u3wi_la_min}, {}}; +static u3j_harm _137_non__lagoon_max_a[] = {{".2", u3wi_la_max}, {}}; +static u3j_harm _137_non__lagoon_linspace_a[]={{".2", u3wi_la_linspace}, {}}; +static u3j_harm _137_non__lagoon_range_a[]= {{".2", u3wi_la_range}, {}}; +static u3j_harm _137_non__lagoon_abs_a[] = {{".2", u3wi_la_abs}, {}}; +static u3j_harm _137_non__lagoon_gth_a[] = {{".2", u3wi_la_gth}, {}}; +static u3j_harm _137_non__lagoon_gte_a[] = {{".2", u3wi_la_gte}, {}}; +static u3j_harm _137_non__lagoon_lth_a[] = {{".2", u3wi_la_lth}, {}}; +static u3j_harm _137_non__lagoon_lte_a[] = {{".2", u3wi_la_lte}, {}}; +static u3j_harm _137_non__lagoon_diag_a[] = {{".2", u3wi_la_diag}, {}}; +static u3j_harm _137_non__lagoon_trace_a[]= {{".2", u3wi_la_trace}, {}}; +static u3j_harm _137_non__lagoon_mmul_a[] = {{".2", u3wi_la_mmul}, {}}; + +static u3j_core _137_non__la_core_d[] = + { { "add-rays", 7, _137_non__lagoon_add_a, 0, no_hashes }, + { "sub-rays", 7, _137_non__lagoon_sub_a, 0, no_hashes }, + { "mul-rays", 7, _137_non__lagoon_mul_a, 0, no_hashes }, + { "div-rays", 7, _137_non__lagoon_div_a, 0, no_hashes }, + { "mod-rays", 7, _137_non__lagoon_mod_a, 0, no_hashes }, + { "add-scal", 7, _137_non__lagoon_adds_a, 0, no_hashes }, + { "sub-scal", 7, _137_non__lagoon_subs_a, 0, no_hashes }, + { "mul-scal", 7, _137_non__lagoon_muls_a, 0, no_hashes }, + { "div-scal", 7, _137_non__lagoon_divs_a, 0, no_hashes }, + { "mod-scal", 7, _137_non__lagoon_mods_a, 0, no_hashes }, + { "dot", 7, _137_non__lagoon_dot_a, 0, no_hashes }, + { "transpose",7, _137_non__lagoon_trans_a, 0, no_hashes }, + { "cumsum", 7, _137_non__lagoon_cumsum_a, 0, no_hashes }, + { "argmin", 7, _137_non__lagoon_argmin_a, 0, no_hashes }, + { "argmax", 7, _137_non__lagoon_argmax_a, 0, no_hashes }, + { "ravel", 7, _137_non__lagoon_ravel_a, 0, no_hashes }, + { "min", 7, _137_non__lagoon_min_a, 0, no_hashes }, + { "max", 7, _137_non__lagoon_max_a, 0, no_hashes }, + { "linspace", 7, _137_non__lagoon_linspace_a, 0, no_hashes }, + { "range", 7, _137_non__lagoon_range_a, 0, no_hashes }, + { "abs", 7, _137_non__lagoon_abs_a, 0, no_hashes }, + { "gth", 7, _137_non__lagoon_gth_a, 0, no_hashes }, + { "gte", 7, _137_non__lagoon_gte_a, 0, no_hashes }, + { "lth", 7, _137_non__lagoon_lth_a, 0, no_hashes }, + { "lte", 7, _137_non__lagoon_lte_a, 0, no_hashes }, + { "diag", 7, _137_non__lagoon_diag_a, 0, no_hashes }, + { "trace", 7, _137_non__lagoon_trace_a,0, no_hashes }, + { "mmul", 7, _137_non__lagoon_mmul_a, 0, no_hashes }, + {} + }; + +static u3j_core _137_non_d[] = + { { "lagoon", 7, 0, _137_non__la_core_d, no_hashes }, + {} + }; + + +static u3j_core _137_hex_d[] = + { { "non", 7, 0, _137_non_d, no_hashes }, + + { "lull", 3, 0, _137_lull_d, no_hashes }, + + { "lore", 63, _137_hex_lore_a, 0, no_hashes }, + + { "leer", 63, _137_hex_leer_a, 0, no_hashes }, + { "loss", 63, _137_hex_loss_a, 0, no_hashes }, + { "lune", 127, _137_hex_lune_a, 0, no_hashes }, + + { "crc", 31, 0, _137_hex__crc_d, no_hashes }, + + { "coed", 63, 0, _137_hex_coed_d, no_hashes }, + { "aes", 31, 0, _137_hex_aes_d, no_hashes }, + + { "hmac", 63, 0, _137_hex_hmac_d, no_hashes }, + { "argon", 31, 0, _137_hex_argon_d, no_hashes }, + { "blake", 31, 0, _137_hex_blake_d, no_hashes }, + { "chacha", 31, 0, _137_hex_chacha_d, no_hashes }, + { "kecc", 31, 0, _137_hex_kecc_d, no_hashes }, + { "ripemd", 31, 0, _137_hex_ripe_d, no_hashes }, + { "scr", 31, 0, _137_hex_scr_d, no_hashes }, + { "secp", 6, 0, _137_hex_secp_d, no_hashes }, + { "mimes", 31, 0, _137_hex_mimes_d, no_hashes }, + { "json", 31, 0, _137_hex_json_d, no_hashes }, + {} + }; + +static u3j_core _137_pen_d[] = + { { "hex", 7, 0, _137_hex_d, no_hashes }, + + { "cell", 7, _137_pen_cell_a, 0, no_hashes }, + { "comb", 7, _137_pen_comb_a, 0, no_hashes }, + { "cons", 7, _137_pen_cons_a, 0, no_hashes }, + { "core", 7, _137_pen_core_a, 0, no_hashes }, + { "face", 7, _137_pen_face_a, 0, no_hashes }, + { "fitz", 7, _137_pen_fitz_a, 0, no_hashes }, + { "fork", 7, _137_pen_fork_a, 0, no_hashes }, + { "look", 7, _137_pen_look_a, 0, no_hashes }, + { "loot", 7, _137_pen_loot_a, 0, no_hashes }, + { "ut", 15, 0, _137_pen__ut_d, no_hashes, _137_pen__ut_ho }, + {} + }; + +static u3j_core _137_qua_d[] = + { { "pen", 3, 0, _137_pen_d, no_hashes, _137_pen_ho }, + + { "po", 7, 0, _137_qua__po_d, no_hashes }, + + { "trip", 7, _137_qua_trip_a, 0, no_hashes }, + + { "bend", 7, 0, _137_qua__bend_d, no_hashes }, + { "cold", 7, 0, _137_qua__cold_d, no_hashes }, + { "comp", 7, 0, _137_qua__comp_d, no_hashes }, + { "cook", 7, 0, _137_qua__cook_d, no_hashes }, + { "easy", 7, 0, _137_qua__easy_d, no_hashes }, + { "glue", 7, 0, _137_qua__glue_d, no_hashes }, + { "here", 7, 0, _137_qua__here_d, no_hashes }, + { "just", 7, 0, _137_qua__just_d, no_hashes }, + { "mask", 7, 0, _137_qua__mask_d, no_hashes }, + { "shim", 7, 0, _137_qua__shim_d, no_hashes }, + { "stag", 7, 0, _137_qua__stag_d, no_hashes }, + { "stew", 7, 0, _137_qua__stew_d, no_hashes }, + { "stir", 7, 0, _137_qua__stir_d, no_hashes }, + + { "pfix", 7, _137_qua_pfix_a, 0, no_hashes }, + { "plug", 7, _137_qua_plug_a, 0, no_hashes }, + { "pose", 7, _137_qua_pose_a, 0, no_hashes }, + { "sfix", 7, _137_qua_sfix_a, 0, no_hashes }, + + { "mink", 7, _137_qua_mink_a, 0, no_hashes }, + { "mole", 7, _137_qua_mole_a, 0, no_hashes }, + { "mule", 7, _137_qua_mule_a, 0, no_hashes }, + + { "scot", 7, _137_qua_scot_a, 0, no_hashes }, + { "scow", 7, _137_qua_scow_a, 0, no_hashes }, + { "slaw", 7, _137_qua_slaw_a, 0, no_hashes }, + {} + }; + +static u3j_core _137_tri_d[] = + { { "qua", 3, 0, _137_qua_d, no_hashes, _137_qua_ho }, + + { "cofl", 7, 0, _137_tri__cofl_d, no_hashes }, + { "rd", 7, 0, _137_tri__rd_d, no_hashes }, + { "rs", 7, 0, _137_tri__rs_d, no_hashes }, + { "rq", 7, 0, _137_tri__rq_d, no_hashes }, + { "rh", 7, 0, _137_tri__rh_d, no_hashes }, + { "og", 7, 0, _137_tri__og_d, no_hashes }, + + { "sha", 7, 0, _137_tri__sha_d, no_hashes }, + { "shax", 7, _137_tri_shax_a, 0, no_hashes }, + { "shay", 7, _137_tri_shay_a, 0, no_hashes }, + { "shas", 7, _137_tri_shas_a, 0, no_hashes }, + { "shal", 7, _137_tri_shal_a, 0, no_hashes }, + + { "ob", 3, 0, _137_ob_d, no_hashes, _137_ob_ho }, + {} + }; + +static u3j_harm _137_two_clz_a[] = {{".2", u3wc_clz, c3n}, {}}; +static u3j_harm _137_two_ctz_a[] = {{".2", u3wc_ctz, c3n}, {}}; +static u3j_harm _137_two_ham_a[] = {{".2", u3wc_ham, c3n}, {}}; + +static u3j_harm _137_two__hew_fun_a[] = {{".2", u3wc_hew, c3n}, {}}; +static u3j_core _137_two__hew_d[] = + { { "fun", 15, _137_two__hew_fun_a, 0, no_hashes }, + {} + }; + +static u3j_harm _137_two__by_bif_a[] = {{".2", u3wdb_bif, c3y}, {}}; + +static u3j_core _137_two__by_d[] = + { { "all", 7, _137_two__by_all_a, 0, no_hashes }, + { "any", 7, _137_two__by_any_a, 0, no_hashes }, + { "apt", 7, _137_two__by_apt_a, 0, no_hashes }, + { "bif", 7, _137_two__by_bif_a, 0, no_hashes }, + { "del", 7, _137_two__by_del_a, 0, no_hashes }, + { "dif", 7, _137_two__by_dif_a, 0, no_hashes }, + { "gas", 7, _137_two__by_gas_a, 0, no_hashes }, + { "get", 7, _137_two__by_get_a, 0, no_hashes }, + { "has", 7, _137_two__by_has_a, 0, no_hashes }, + { "int", 7, _137_two__by_int_a, 0, no_hashes }, + { "jab", 7, _137_two__by_jab_a, 0, no_hashes }, + { "key", 7, _137_two__by_key_a, 0, no_hashes }, + { "put", 7, _137_two__by_put_a, 0, no_hashes }, + { "rep", 7, _137_two__by_rep_a, 0, no_hashes }, + { "run", 7, _137_two__by_run_a, 0, no_hashes }, + { "tap", 7, _137_two__by_tap_a, 0, no_hashes }, + { "uni", 7, _137_two__by_uni_a, 0, no_hashes }, + { "urn", 7, _137_two__by_urn_a, 0, no_hashes }, + { "wyt", 3, _137_two__by_wyt_a, 0, no_hashes }, + {} + }; + + +static u3j_harm _137_two__in_apt_a[] = {{".2", u3wdi_apt}, {}}; + +static u3j_core _137_two__in_d[] = + { { "apt", 7, _137_two__in_apt_a, 0, no_hashes }, + { "bif", 7, _137_two__in_bif_a, 0, no_hashes }, + { "del", 7, _137_two__in_del_a, 0, no_hashes }, + { "dif", 7, _137_two__in_dif_a, 0, no_hashes }, + { "gas", 7, _137_two__in_gas_a, 0, no_hashes }, + { "has", 7, _137_two__in_has_a, 0, no_hashes }, + { "int", 7, _137_two__in_int_a, 0, no_hashes }, + { "put", 7, _137_two__in_put_a, 0, no_hashes }, + { "rep", 7, _137_two__in_rep_a, 0, no_hashes }, + { "run", 7, _137_two__in_run_a, 0, no_hashes }, + { "tap", 7, _137_two__in_tap_a, 0, no_hashes }, + { "uni", 7, _137_two__in_uni_a, 0, no_hashes }, + { "wyt", 3, _137_two__in_wyt_a, 0, no_hashes }, + {} + }; + +static u3j_harm _137_two_rig_a[] = {{".2", u3wc_rig, c3n}, {}}; + +static u3j_harm _137_two_mate_a[] = {{".2", u3wb_mate, c3y}, {}}; + +static u3j_core _137_two_d[] = + { { "tri", 3, 0, _137_tri_d, no_hashes, _137_tri_ho }, + + { "find", 7, _137_two_find_a, 0, no_hashes }, + { "flop", 7, _137_two_flop_a, 0, no_hashes }, + { "lent", 7, _137_two_lent_a, 0, no_hashes }, + { "levy", 7, _137_two_levy_a, 0, no_hashes }, + { "lien", 7, _137_two_lien_a, 0, no_hashes }, + { "murn", 7, _137_two_murn_a, 0, no_hashes }, + { "need", 7, _137_two_need_a, 0, no_hashes }, + { "mate", 7, _137_two_mate_a, 0, no_hashes }, + { "reap", 7, _137_two_reap_a, 0, no_hashes }, + { "reel", 7, _137_two_reel_a, 0, no_hashes }, + { "roll", 7, _137_two_roll_a, 0, no_hashes }, + { "skid", 7, _137_two_skid_a, 0, no_hashes }, + { "skim", 7, _137_two_skim_a, 0, no_hashes }, + { "skip", 7, _137_two_skip_a, 0, no_hashes }, + { "scag", 7, _137_two_scag_a, 0, no_hashes }, + { "slag", 7, _137_two_slag_a, 0, no_hashes }, + { "snag", 7, _137_two_snag_a, 0, no_hashes }, + { "sort", 7, _137_two_sort_a, 0, no_hashes }, + { "turn", 7, _137_two_turn_a, 0, no_hashes }, + { "weld", 7, _137_two_weld_a, 0, no_hashes }, + { "welp", 7, _137_two_welp_a, 0, no_hashes }, + { "zing", 7, _137_two_zing_a, 0, no_hashes }, + + { "bex", 7, _137_two_bex_a, 0, no_hashes }, + { "cat", 7, _137_two_cat_a, 0, no_hashes }, + { "can", 7, _137_two_can_a, 0, no_hashes }, + { "clz", 7, _137_two_clz_a, 0, no_hashes }, + { "con", 7, _137_two_con_a, 0, no_hashes }, + { "ctz", 7, _137_two_ctz_a, 0, no_hashes }, + { "cue", 7, _137_two_cue_a, 0, no_hashes }, + { "cut", 7, _137_two_cut_a, 0, no_hashes }, + { "dis", 7, _137_two_dis_a, 0, no_hashes }, + { "dor", 7, _137_two_dor_a, 0, no_hashes }, + { "end", 7, _137_two_end_a, 0, no_hashes }, + { "gor", 7, _137_two_gor_a, 0, no_hashes }, + { "ham", 7, _137_two_ham_a, 0, no_hashes }, + { "hew", 7, 0, _137_two__hew_d, no_hashes }, + { "jam", 7, _137_two_jam_a, 0, no_hashes }, + { "lsh", 7, _137_two_lsh_a, 0, no_hashes }, + { "mat", 7, _137_two_mat_a, 0, no_hashes }, + { "met", 7, _137_two_met_a, 0, no_hashes }, + { "mix", 7, _137_two_mix_a, 0, no_hashes }, + { "mor", 7, _137_two_mor_a, 0, no_hashes }, + { "mug", 7, _137_two_mug_a, 0, no_hashes }, + { "muk", 59, _137_two_muk_a, 0, no_hashes }, + { "rap", 7, _137_two_rap_a, 0, no_hashes }, + { "rep", 7, _137_two_rep_a, 0, no_hashes }, + { "rev", 7, _137_two_rev_a, 0, no_hashes }, + { "rig", 7, _137_two_rig_a, 0, no_hashes }, + { "rip", 7, _137_two_rip_a, 0, no_hashes }, + { "rsh", 7, _137_two_rsh_a, 0, no_hashes }, + { "swp", 7, _137_two_swp_a, 0, no_hashes }, + { "rub", 7, _137_two_rub_a, 0, no_hashes }, + { "pow", 7, _137_two_pow_a, 0, no_hashes }, + { "sqt", 7, _137_two_sqt_a, 0, no_hashes }, + { "xeb", 7, _137_two_xeb_a, 0, no_hashes }, + + { "by", 7, 0, _137_two__by_d, no_hashes }, + { "in", 7, 0, _137_two__in_d, no_hashes }, + {} + }; + +static u3j_core _137_one_d[] = + { { "two", 3, 0, _137_two_d, no_hashes }, + + { "add", 7, _137_one_add_a, 0, no_hashes }, + { "dec", 7, _137_one_dec_a, 0, no_hashes }, + { "div", 7, _137_one_div_a, 0, no_hashes }, + { "dvr", 7, _137_one_dvr_a, 0, no_hashes }, + { "gte", 7, _137_one_gte_a, 0, no_hashes }, + { "gth", 7, _137_one_gth_a, 0, no_hashes }, + { "lte", 7, _137_one_lte_a, 0, no_hashes }, + { "lth", 7, _137_one_lth_a, 0, no_hashes }, + { "max", 7, _137_one_max_a, 0, no_hashes }, + { "min", 7, _137_one_min_a, 0, no_hashes }, + { "mod", 7, _137_one_mod_a, 0, no_hashes }, + { "mul", 7, _137_one_mul_a, 0, no_hashes }, + { "sub", 7, _137_one_sub_a, 0, no_hashes }, + + { "cap", 7, _137_one_cap_a, 0, no_hashes }, + { "mas", 7, _137_one_mas_a, 0, no_hashes }, + { "peg", 7, _137_one_peg_a, 0, no_hashes }, + {} + }; + +u3j_core _k137_d[] = + { { "one", 3, 0, _137_one_d, no_hashes }, + {} + }; + diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index 53a8cb952d..a2231bb3b9 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -21,7 +21,6 @@ #include "jets.h" #include "jets/w.h" - static c3_c* no_hashes[] = { 0 }; @@ -270,12 +269,6 @@ static c3_c* _140_hex_lune_ha[] = { 0 }; - -static u3j_harm _137_hex__crc32_a[] = {{".2", u3we_crc32}, {}}; - -static u3j_core _137_hex__crc_d[] = {{"crc32", 7, _137_hex__crc32_a, 0, no_hashes }, {}}; - - static u3j_harm _140_hex_coed__ed_puck_a[] = {{".2", u3wee_puck}, {}}; static c3_c* _140_hex_coed__ed_puck_ha[] = { "1bc694675842345c50b0e20a2193bb5bcbb42f163fc832431a3d1822a81e4c98", @@ -2126,76 +2119,10 @@ static u3j_core _139_hex_json_d[] = {} }; -/* /lib jets in non core -*/ -static u3j_harm _139_non__lagoon_add_a[] = {{".2", u3wi_la_add}, {}}; -static u3j_harm _139_non__lagoon_sub_a[] = {{".2", u3wi_la_sub}, {}}; -static u3j_harm _139_non__lagoon_mul_a[] = {{".2", u3wi_la_mul}, {}}; -static u3j_harm _139_non__lagoon_div_a[] = {{".2", u3wi_la_div}, {}}; -static u3j_harm _139_non__lagoon_mod_a[] = {{".2", u3wi_la_mod}, {}}; -static u3j_harm _139_non__lagoon_adds_a[] = {{".2", u3wi_la_adds}, {}}; -static u3j_harm _139_non__lagoon_subs_a[] = {{".2", u3wi_la_subs}, {}}; -static u3j_harm _139_non__lagoon_muls_a[] = {{".2", u3wi_la_muls}, {}}; -static u3j_harm _139_non__lagoon_divs_a[] = {{".2", u3wi_la_divs}, {}}; -static u3j_harm _139_non__lagoon_mods_a[] = {{".2", u3wi_la_mods}, {}}; -static u3j_harm _139_non__lagoon_dot_a[] = {{".2", u3wi_la_dot}, {}}; -static u3j_harm _139_non__lagoon_trans_a[] ={{".2", u3wi_la_transpose}, {}}; -static u3j_harm _139_non__lagoon_cumsum_a[]={{".2", u3wi_la_cumsum}, {}}; -static u3j_harm _139_non__lagoon_argmin_a[]={{".2", u3wi_la_argmin}, {}}; -static u3j_harm _139_non__lagoon_argmax_a[]={{".2", u3wi_la_argmax}, {}}; -static u3j_harm _139_non__lagoon_ravel_a[]={{".2", u3wi_la_ravel}, {}}; -static u3j_harm _139_non__lagoon_min_a[] = {{".2", u3wi_la_min}, {}}; -static u3j_harm _139_non__lagoon_max_a[] = {{".2", u3wi_la_max}, {}}; -static u3j_harm _139_non__lagoon_linspace_a[]={{".2", u3wi_la_linspace}, {}}; -static u3j_harm _139_non__lagoon_range_a[]= {{".2", u3wi_la_range}, {}}; -static u3j_harm _139_non__lagoon_abs_a[] = {{".2", u3wi_la_abs}, {}}; -static u3j_harm _139_non__lagoon_gth_a[] = {{".2", u3wi_la_gth}, {}}; -static u3j_harm _139_non__lagoon_gte_a[] = {{".2", u3wi_la_gte}, {}}; -static u3j_harm _139_non__lagoon_lth_a[] = {{".2", u3wi_la_lth}, {}}; -static u3j_harm _139_non__lagoon_lte_a[] = {{".2", u3wi_la_lte}, {}}; -static u3j_harm _139_non__lagoon_diag_a[] = {{".2", u3wi_la_diag}, {}}; -static u3j_harm _139_non__lagoon_trace_a[]= {{".2", u3wi_la_trace}, {}}; -static u3j_harm _139_non__lagoon_mmul_a[] = {{".2", u3wi_la_mmul}, {}}; -static u3j_core _139_non__la_core_d[] = - { { "add-rays", 7, _139_non__lagoon_add_a, 0, no_hashes }, - { "sub-rays", 7, _139_non__lagoon_sub_a, 0, no_hashes }, - { "mul-rays", 7, _139_non__lagoon_mul_a, 0, no_hashes }, - { "div-rays", 7, _139_non__lagoon_div_a, 0, no_hashes }, - { "mod-rays", 7, _139_non__lagoon_mod_a, 0, no_hashes }, - { "add-scal", 7, _139_non__lagoon_adds_a, 0, no_hashes }, - { "sub-scal", 7, _139_non__lagoon_subs_a, 0, no_hashes }, - { "mul-scal", 7, _139_non__lagoon_muls_a, 0, no_hashes }, - { "div-scal", 7, _139_non__lagoon_divs_a, 0, no_hashes }, - { "mod-scal", 7, _139_non__lagoon_mods_a, 0, no_hashes }, - { "dot", 7, _139_non__lagoon_dot_a, 0, no_hashes }, - { "transpose",7, _139_non__lagoon_trans_a, 0, no_hashes }, - { "cumsum", 7, _139_non__lagoon_cumsum_a, 0, no_hashes }, - { "argmin", 7, _139_non__lagoon_argmin_a, 0, no_hashes }, - { "argmax", 7, _139_non__lagoon_argmax_a, 0, no_hashes }, - { "ravel", 7, _139_non__lagoon_ravel_a, 0, no_hashes }, - { "min", 7, _139_non__lagoon_min_a, 0, no_hashes }, - { "max", 7, _139_non__lagoon_max_a, 0, no_hashes }, - { "linspace", 7, _139_non__lagoon_linspace_a, 0, no_hashes }, - { "range", 7, _139_non__lagoon_range_a, 0, no_hashes }, - { "abs", 7, _139_non__lagoon_abs_a, 0, no_hashes }, - { "gth", 7, _139_non__lagoon_gth_a, 0, no_hashes }, - { "gte", 7, _139_non__lagoon_gte_a, 0, no_hashes }, - { "lth", 7, _139_non__lagoon_lth_a, 0, no_hashes }, - { "lte", 7, _139_non__lagoon_lte_a, 0, no_hashes }, - { "diag", 7, _139_non__lagoon_diag_a, 0, no_hashes }, - { "trace", 7, _139_non__lagoon_trace_a,0, no_hashes }, - { "mmul", 7, _139_non__lagoon_mmul_a, 0, no_hashes }, - {} - }; -static u3j_core _139_non_d[] = - { { "lagoon", 7, 0, _139_non__la_core_d, no_hashes }, - {} - }; static u3j_core _139_hex_d[] = -{ { "non", 7, 0, _139_non_d, no_hashes }, - +{ { "lore", 63, _140_hex_lore_a, 0, no_hashes }, { "leer", 63, _140_hex_leer_a, 0, no_hashes }, { "loss", 63, _140_hex_loss_a, 0, no_hashes }, @@ -2429,8 +2356,6 @@ static u3j_core _138_hex_d[] = { "leer", 63, _140_hex_leer_a, 0, no_hashes }, { "loss", 63, _140_hex_loss_a, 0, no_hashes }, { "lune", 127, _140_hex_lune_a, 0, no_hashes }, -// XX move me to 137 when it exists - { "crc", 31, 0, _137_hex__crc_d, no_hashes }, { "coed", 63, 0, _140_hex_coed_d, no_hashes }, { "aes", 31, 0, _140_hex_aes_d, no_hashes }, @@ -2635,230 +2560,12 @@ static u3j_core _138_one_d[] = {} }; -u3j_core _k138_d[] = +static u3j_core _k138_d[] = { { "one", 3, 0, _138_one_d, no_hashes }, {} }; -static u3j_harm _137_lull_plot_fax_a[] = {{".2", u3wg_plot_fax, c3y}, {}}; -static u3j_harm _137_lull_plot_met_a[] = {{".2", u3wg_plot_met, c3y}, {}}; - -static u3j_core _137_lull_plot_d[] = - { { "fax", 7, _137_lull_plot_fax_a, 0, no_hashes }, - { "met", 7, _137_lull_plot_met_a, 0, no_hashes }, - {} - }; - -static u3j_core _137_lull_d[] = - { { "plot", 31, 0, _137_lull_plot_d, no_hashes }, - {} - }; - -static u3j_core _137_hex_d[] = -{ { "lull", 3, 0, _137_lull_d, no_hashes }, - - { "lore", 63, _140_hex_lore_a, 0, no_hashes }, - { "leer", 63, _140_hex_leer_a, 0, no_hashes }, - { "loss", 63, _140_hex_loss_a, 0, no_hashes }, - { "lune", 127, _140_hex_lune_a, 0, no_hashes }, - - { "coed", 63, 0, _140_hex_coed_d, no_hashes }, - { "aes", 31, 0, _140_hex_aes_d, no_hashes }, - - { "hmac", 63, 0, _140_hex_hmac_d, no_hashes }, - { "argon", 31, 0, _140_hex_argon_d, no_hashes }, - { "blake", 31, 0, _138_hex_blake_d, no_hashes }, - { "chacha", 31, 0, _138_hex_chacha_d, no_hashes }, - { "kecc", 31, 0, _140_hex_kecc_d, no_hashes }, - { "ripemd", 31, 0, _140_hex_ripe_d, no_hashes }, - { "scr", 31, 0, _140_hex_scr_d, no_hashes }, - { "secp", 6, 0, _140_hex_secp_d, no_hashes }, - { "mimes", 31, 0, _140_hex_mimes_d, no_hashes }, - { "json", 31, 0, _139_hex_json_d, no_hashes }, - {} -}; - -static u3j_core _137_pen_d[] = -{ { "hex", 7, 0, _137_hex_d, no_hashes }, - - { "cell", 7, _140_pen_cell_a, 0, no_hashes }, - { "comb", 7, _140_pen_comb_a, 0, no_hashes }, - { "cons", 7, _140_pen_cons_a, 0, no_hashes }, - { "core", 7, _140_pen_core_a, 0, no_hashes }, - { "face", 7, _140_pen_face_a, 0, no_hashes }, - { "fitz", 7, _140_pen_fitz_a, 0, no_hashes }, - { "fork", 7, _140_pen_fork_a, 0, no_hashes }, - { "look", 7, _140_pen_look_a, 0, no_hashes }, - { "loot", 7, _140_pen_loot_a, 0, no_hashes }, - { "ut", 15, 0, _140_pen__ut_d, no_hashes, _140_pen__ut_ho }, - {} -}; - -static u3j_core _137_qua_d[] = -{ { "pen", 3, 0, _137_pen_d, no_hashes, _140_pen_ho }, - - { "po", 7, 0, _140_qua__po_d, no_hashes }, - - { "trip", 7, _140_qua_trip_a, 0, no_hashes }, - - { "bend", 7, 0, _140_qua__bend_d, no_hashes }, - { "cold", 7, 0, _140_qua__cold_d, no_hashes }, - { "comp", 7, 0, _140_qua__comp_d, no_hashes }, - { "cook", 7, 0, _140_qua__cook_d, no_hashes }, - { "easy", 7, 0, _140_qua__easy_d, no_hashes }, - { "glue", 7, 0, _140_qua__glue_d, no_hashes }, - { "here", 7, 0, _140_qua__here_d, no_hashes }, - { "just", 7, 0, _140_qua__just_d, no_hashes }, - { "mask", 7, 0, _140_qua__mask_d, no_hashes }, - { "shim", 7, 0, _140_qua__shim_d, no_hashes }, - { "stag", 7, 0, _140_qua__stag_d, no_hashes }, - { "stew", 7, 0, _140_qua__stew_d, no_hashes }, - { "stir", 7, 0, _140_qua__stir_d, no_hashes }, - - { "pfix", 7, _140_qua_pfix_a, 0, no_hashes }, - { "plug", 7, _140_qua_plug_a, 0, no_hashes }, - { "pose", 7, _140_qua_pose_a, 0, no_hashes }, - { "sfix", 7, _140_qua_sfix_a, 0, no_hashes }, - - { "mink", 7, _140_qua_mink_a, 0, no_hashes }, - { "mole", 7, _140_qua_mole_a, 0, no_hashes }, - { "mule", 7, _140_qua_mule_a, 0, no_hashes }, - - { "scot", 7, _140_qua_scot_a, 0, no_hashes }, - { "scow", 7, _140_qua_scow_a, 0, no_hashes }, - { "slaw", 7, _140_qua_slaw_a, 0, no_hashes }, - {} -}; - -static u3j_core _137_tri_d[] = -{ { "qua", 3, 0, _137_qua_d, no_hashes, _140_qua_ho }, - - { "cofl", 7, 0, _140_tri__cofl_d, no_hashes }, - { "rd", 7, 0, _140_tri__rd_d, no_hashes }, - { "rs", 7, 0, _140_tri__rs_d, no_hashes }, - { "rq", 7, 0, _140_tri__rq_d, no_hashes }, - { "rh", 7, 0, _140_tri__rh_d, no_hashes }, - { "og", 7, 0, _140_tri__og_d, no_hashes }, - - { "sha", 7, 0, _140_tri__sha_d, no_hashes }, - { "shax", 7, _140_tri_shax_a, 0, no_hashes }, - { "shay", 7, _140_tri_shay_a, 0, no_hashes }, - { "shas", 7, _140_tri_shas_a, 0, no_hashes }, - { "shal", 7, _140_tri_shal_a, 0, no_hashes }, - - { "ob", 3, 0, _140_ob_d, no_hashes, _140_ob_ho }, - {} -}; - -static u3j_harm _137_two_clz_a[] = {{".2", u3wc_clz, c3n}, {}}; -static u3j_harm _137_two_ctz_a[] = {{".2", u3wc_ctz, c3n}, {}}; -static u3j_harm _137_two_ham_a[] = {{".2", u3wc_ham, c3n}, {}}; - -static u3j_harm _137_two__hew_fun_a[] = {{".2", u3wc_hew, c3n}, {}}; -static u3j_core _137_two__hew_d[] = - { { "fun", 15, _137_two__hew_fun_a, 0, no_hashes }, - {} - }; - -static u3j_harm _137_two_rig_a[] = {{".2", u3wc_rig, c3n}, {}}; - -static u3j_core _137_two_d[] = -{ { "tri", 3, 0, _137_tri_d, no_hashes, _140_tri_ho }, - - { "find", 7, _140_two_find_a, 0, no_hashes }, - { "flop", 7, _140_two_flop_a, 0, no_hashes }, - { "lent", 7, _140_two_lent_a, 0, no_hashes }, - { "levy", 7, _140_two_levy_a, 0, no_hashes }, - { "lien", 7, _140_two_lien_a, 0, no_hashes }, - { "murn", 7, _140_two_murn_a, 0, no_hashes }, - { "need", 7, _140_two_need_a, 0, no_hashes }, - { "mate", 7, _138_two_mate_a, 0, no_hashes }, - { "reap", 7, _140_two_reap_a, 0, no_hashes }, - { "reel", 7, _140_two_reel_a, 0, no_hashes }, - { "roll", 7, _140_two_roll_a, 0, no_hashes }, - { "skid", 7, _140_two_skid_a, 0, no_hashes }, - { "skim", 7, _140_two_skim_a, 0, no_hashes }, - { "skip", 7, _140_two_skip_a, 0, no_hashes }, - { "scag", 7, _140_two_scag_a, 0, no_hashes }, - { "slag", 7, _140_two_slag_a, 0, no_hashes }, - { "snag", 7, _140_two_snag_a, 0, no_hashes }, - { "sort", 7, _140_two_sort_a, 0, no_hashes }, - { "turn", 7, _140_two_turn_a, 0, no_hashes }, - { "weld", 7, _140_two_weld_a, 0, no_hashes }, - { "welp", 7, _140_two_welp_a, 0, no_hashes }, - { "zing", 7, _140_two_zing_a, 0, no_hashes }, - - { "bex", 7, _140_two_bex_a, 0, no_hashes }, - { "cat", 7, _140_two_cat_a, 0, no_hashes }, - { "can", 7, _140_two_can_a, 0, no_hashes }, - { "clz", 7, _137_two_clz_a, 0, no_hashes }, - { "con", 7, _140_two_con_a, 0, no_hashes }, - { "ctz", 7, _137_two_ctz_a, 0, no_hashes }, - { "cue", 7, _140_two_cue_a, 0, no_hashes }, - { "cut", 7, _140_two_cut_a, 0, no_hashes }, - { "dis", 7, _140_two_dis_a, 0, no_hashes }, - { "dor", 7, _140_two_dor_a, 0, no_hashes }, - { "end", 7, _140_two_end_a, 0, no_hashes }, - { "gor", 7, _140_two_gor_a, 0, no_hashes }, - { "ham", 7, _137_two_ham_a, 0, no_hashes }, - { "hew", 7, 0, _137_two__hew_d, no_hashes }, - { "jam", 7, _140_two_jam_a, 0, no_hashes }, - { "lsh", 7, _140_two_lsh_a, 0, no_hashes }, - { "mat", 7, _140_two_mat_a, 0, no_hashes }, - { "met", 7, _140_two_met_a, 0, no_hashes }, - { "mix", 7, _140_two_mix_a, 0, no_hashes }, - { "mor", 7, _140_two_mor_a, 0, no_hashes }, - { "mug", 7, _140_two_mug_a, 0, no_hashes }, - { "muk", 59, _140_two_muk_a, 0, no_hashes }, - { "rap", 7, _140_two_rap_a, 0, no_hashes }, - { "rep", 7, _140_two_rep_a, 0, no_hashes }, - { "rev", 7, _140_two_rev_a, 0, no_hashes }, - { "rig", 7, _137_two_rig_a, 0, no_hashes }, - { "rip", 7, _140_two_rip_a, 0, no_hashes }, - { "rsh", 7, _140_two_rsh_a, 0, no_hashes }, - { "swp", 7, _140_two_swp_a, 0, no_hashes }, - { "rub", 7, _140_two_rub_a, 0, no_hashes }, - { "pow", 7, _140_two_pow_a, 0, no_hashes }, - { "sqt", 7, _140_two_sqt_a, 0, no_hashes }, - { "xeb", 7, _140_two_xeb_a, 0, no_hashes }, - - { "by", 7, 0, _138_two__by_d, no_hashes }, - { "in", 7, 0, _139_two__in_d, no_hashes }, - {} -}; - -static u3j_core _137_one_d[] = -{ { "two", 3, 0, _137_two_d, no_hashes }, - - { "add", 7, _140_one_add_a, 0, no_hashes }, - { "dec", 7, _140_one_dec_a, 0, no_hashes }, - { "div", 7, _140_one_div_a, 0, no_hashes }, - { "dvr", 7, _140_one_dvr_a, 0, no_hashes }, - { "gte", 7, _140_one_gte_a, 0, no_hashes }, - { "gth", 7, _140_one_gth_a, 0, no_hashes }, - { "lte", 7, _140_one_lte_a, 0, no_hashes }, - { "lth", 7, _140_one_lth_a, 0, no_hashes }, - { "max", 7, _140_one_max_a, 0, no_hashes }, - { "min", 7, _140_one_min_a, 0, no_hashes }, - { "mod", 7, _140_one_mod_a, 0, no_hashes }, - { "mul", 7, _140_one_mul_a, 0, no_hashes }, - { "sub", 7, _140_one_sub_a, 0, no_hashes }, - - { "cap", 7, _140_one_cap_a, 0, no_hashes }, - { "mas", 7, _140_one_mas_a, 0, no_hashes }, - { "peg", 7, _140_one_peg_a, 0, no_hashes }, - {} -}; - -u3j_core _k137_d[] = -{ { "one", 3, 0, _137_one_d, no_hashes }, - {} -}; - - -// TODO: probably need different ha hashes - static u3j_core _a50_two__by_d[] = { { "apt", 7, _140_two__by_apt_a, 0, _140_two__by_apt_ha }, { "del", 7, _140_two__by_del_a, 0, _140_two__by_del_ha }, @@ -2922,6 +2629,8 @@ u3j_core _a50_d[] = {} }; +extern u3j_core _k137_d[]; + static u3j_core _d[] = { { "k140", 0, 0, _k140_d, _k140_ha, 0, (u3j_core*) 140, 0 }, { "k139", 0, 0, _k139_d, no_hashes, 0, (u3j_core*) 139, 0 }, From 83e2517c35dcf9d8679beedf33aa30d47c2ba0d4 Mon Sep 17 00:00:00 2001 From: pkova Date: Mon, 21 Oct 2024 17:46:03 +0300 Subject: [PATCH 428/430] build: upgrade VERSION to 3.2 --- VERSION | 2 +- build.zig | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/VERSION b/VERSION index 8c50098d8a..a3ec5a4bd3 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -3.1 +3.2 diff --git a/build.zig b/build.zig index 6bc5e8d7a3..ded7b4cca2 100644 --- a/build.zig +++ b/build.zig @@ -1,6 +1,6 @@ const std = @import("std"); -const VERSION = "3.1"; +const VERSION = "3.2"; const targets: []const std.Target.Query = &.{ .{ .cpu_arch = .aarch64, .os_tag = .macos, .abi = null }, From 7f3c040a3d43519fa88c093115d78bfc9c24fe4d Mon Sep 17 00:00:00 2001 From: pkova Date: Tue, 22 Oct 2024 15:31:09 +0300 Subject: [PATCH 429/430] build: restore ed sign and veri jets for 138 These were mistakenly removed in aba8da490dbbcfada119e0054f2017d1f6a47218 --- pkg/noun/jets/e/ed_sign.c | 34 ++++++++++++++++++++++++++++++++++ pkg/noun/jets/e/ed_veri.c | 35 +++++++++++++++++++++++++++++++++++ pkg/noun/jets/tree.c | 10 +++++++++- pkg/noun/jets/w.h | 2 ++ 4 files changed, 80 insertions(+), 1 deletion(-) diff --git a/pkg/noun/jets/e/ed_sign.c b/pkg/noun/jets/e/ed_sign.c index 4aad1c25fd..0cde84ec71 100644 --- a/pkg/noun/jets/e/ed_sign.c +++ b/pkg/noun/jets/e/ed_sign.c @@ -41,3 +41,37 @@ return _cqee_sign_octs(len, dat, key); } } + + static u3_atom + _cqee_sign(u3_noun a, + u3_noun b) + { + c3_y sed_y[32]; + + if ( 0 != u3r_bytes_fit(32, sed_y, b) ) { + // hoon calls suck, which calls puck, which crashes + return u3m_bail(c3__exit); + } + else { + c3_y sig_y[64]; + c3_w met_w; + c3_y* mes_y = u3r_bytes_all(&met_w, a); + + urcrypt_ed_sign(mes_y, met_w, sed_y, sig_y); + u3a_free(mes_y); + + return u3i_bytes(64, sig_y); + } + } + + u3_noun + u3wee_sign(u3_noun cor) + { + u3_noun a, b; + if ( c3n == u3r_mean(cor, + u3x_sam_2, &a, u3x_sam_3, &b, 0) ) { + return u3m_bail(c3__fail); + } else { + return _cqee_sign(a, b); + } + } diff --git a/pkg/noun/jets/e/ed_veri.c b/pkg/noun/jets/e/ed_veri.c index 42b8f30d69..82649fb939 100644 --- a/pkg/noun/jets/e/ed_veri.c +++ b/pkg/noun/jets/e/ed_veri.c @@ -43,3 +43,38 @@ return u3l_punt("veri-octs", _cqee_veri_octs(sig, len, dat, pub)); } } + + static u3_atom + _cqee_veri(u3_noun s, + u3_noun m, + u3_noun pk) + { + c3_y sig_y[64], pub_y[32]; + + if ( (0 != u3r_bytes_fit(64, sig_y, s)) || + (0 != u3r_bytes_fit(32, pub_y, pk)) ) { + // hoon checks sizes, but weirdly and without crashes + return u3_none; + } + else { + c3_w met_w; + c3_y* mes_y = u3r_bytes_all(&met_w, m); + c3_t val_t = urcrypt_ed_veri(mes_y, met_w, pub_y, sig_y); + u3a_free(mes_y); + + return val_t ? c3y : c3n; + } + } + + u3_noun + u3wee_veri(u3_noun cor) + { + u3_noun a, b, c; + if ( c3n == u3r_mean(cor, + u3x_sam_2, &a, u3x_sam_6, &b, + u3x_sam_7, &c, 0) ) { + return u3m_bail(c3__fail); + } else { + return u3l_punt("veri", _cqee_veri(a, b, c)); + } + } diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index a2231bb3b9..59a93204bb 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -274,11 +274,17 @@ static c3_c* _140_hex_coed__ed_puck_ha[] = { "1bc694675842345c50b0e20a2193bb5bcbb42f163fc832431a3d1822a81e4c98", 0 }; + +static u3j_harm _140_hex_coed__ed_sign_a[] = {{".2", u3wee_sign}, {}}; + static u3j_harm _140_hex_coed__ed_sign_octs_a[] = {{".2", u3wee_sign_octs}, {}}; static c3_c* _140_hex_coed__ed_sign_octs_ha[] = { "34ad749bf8443611cbf1f7de90a066318bd12be36f2f7f6f55281f6f7ed79754", 0 }; + +static u3j_harm _140_hex_coed__ed_veri_a[] = {{".2", u3wee_veri}, {}}; + static u3j_harm _140_hex_coed__ed_veri_octs_a[] = {{".2", u3wee_veri_octs}, {}}; static c3_c* _140_hex_coed__ed_veri_octs_ha[] = { "047a7eeccb2e68aeeee631b6db86e11a5a3aa9e179660553eca6304327612dcf", @@ -322,8 +328,10 @@ static c3_c* _140_hex_coed__ed_shar_ha[] = { }; static u3j_core _140_hex_coed__ed_d[] = - { { "sign-octs", 7, _140_hex_coed__ed_sign_octs_a, 0, _140_hex_coed__ed_sign_octs_ha }, + { { "sign", 7, _140_hex_coed__ed_sign_a, 0, no_hashes }, + { "sign-octs", 7, _140_hex_coed__ed_sign_octs_a, 0, _140_hex_coed__ed_sign_octs_ha }, { "puck", 7, _140_hex_coed__ed_puck_a, 0, _140_hex_coed__ed_puck_ha }, + { "veri", 7, _140_hex_coed__ed_veri_a, 0, no_hashes }, { "veri-octs", 7, _140_hex_coed__ed_veri_octs_a, 0, _140_hex_coed__ed_veri_octs_ha }, { "shar", 7, _140_hex_coed__ed_shar_a, 0, _140_hex_coed__ed_shar_ha }, { "point-add", 7, _140_hex_coed__ed_point_add_a, 0, 0 }, diff --git a/pkg/noun/jets/w.h b/pkg/noun/jets/w.h index f2353bcd3b..78494314fe 100644 --- a/pkg/noun/jets/w.h +++ b/pkg/noun/jets/w.h @@ -194,6 +194,8 @@ u3_noun u3weo_raw(u3_noun); u3_noun u3wee_puck(u3_noun); + u3_noun u3wee_sign(u3_noun); + u3_noun u3wee_veri(u3_noun); u3_noun u3wee_sign_octs(u3_noun); u3_noun u3wee_veri_octs(u3_noun); u3_noun u3wee_shar(u3_noun); From c337d9a945023a072615120badb09ece3d3dd166 Mon Sep 17 00:00:00 2001 From: pkova Date: Tue, 22 Oct 2024 15:52:55 +0300 Subject: [PATCH 430/430] Revert "Revert "bazel: delete bazel build system"" This reverts commit c4ba180e1d49f8ecbffcba260cb2c213eb65ddc6. We had some problems with the new zig build system so we brought bazel back for a little bit to triage. The problems were fixed by upgrading GMP to 6.3.0 in a537c85a5e6a30b96db88f86902c3872c3658943. --- .bazelrc | 51 -- BUILD.bazel | 214 ------ PACE | 1 - VERSION | 1 - WORKSPACE.bazel | 448 ------------ bazel/BUILD.bazel | 21 - bazel/common_settings.bzl | 50 -- bazel/repo.bzl | 34 - bazel/third_party/aes_siv/BUILD.bazel | 0 bazel/third_party/aes_siv/aes_siv.BUILD | 21 - bazel/third_party/argon2/BUILD.bazel | 0 bazel/third_party/argon2/argon2.BUILD | 42 -- bazel/third_party/avahi/BUILD.bazel | 0 bazel/third_party/avahi/avahi.BUILD | 30 - bazel/third_party/curl/BUILD.bazel | 0 bazel/third_party/curl/curl.BUILD | 67 -- bazel/third_party/dbus/BUILD.bazel | 0 bazel/third_party/dbus/dbus.BUILD | 21 - bazel/third_party/ed25519/BUILD.bazel | 0 bazel/third_party/ed25519/ed25519.BUILD | 14 - bazel/third_party/expat/BUILD.bazel | 0 bazel/third_party/expat/expat.BUILD | 24 - bazel/third_party/gmp/BUILD.bazel | 0 bazel/third_party/gmp/gmp.BUILD | 30 - bazel/third_party/h2o/2.2.6.patch | 47 -- bazel/third_party/h2o/BUILD.bazel | 0 bazel/third_party/h2o/h2o.BUILD | 375 ----------- ...647514212b76ae7bca0dea9b7b197d1d8186.patch | 168 ----- bazel/third_party/keccak_tiny/BUILD.bazel | 0 .../third_party/keccak_tiny/keccak_tiny.BUILD | 16 - bazel/third_party/libbacktrace/BUILD.bazel | 0 .../libbacktrace/libbacktrace.BUILD | 25 - bazel/third_party/libunwind/BUILD.bazel | 0 bazel/third_party/libunwind/libunwind.BUILD | 25 - bazel/third_party/lmdb/0.9.29.patch | 16 - bazel/third_party/lmdb/BUILD.bazel | 0 bazel/third_party/lmdb/lmdb.BUILD | 29 - bazel/third_party/murmur3/BUILD.bazel | 0 bazel/third_party/murmur3/murmur3.BUILD | 21 - bazel/third_party/natpmp/BUILD.bazel | 0 bazel/third_party/natpmp/natpmp.BUILD | 8 - bazel/third_party/openssl/BUILD.bazel | 0 bazel/third_party/openssl/openssl.BUILD | 43 -- bazel/third_party/pdjson/BUILD.bazel | 0 bazel/third_party/pdjson/pdjson.BUILD | 16 - bazel/third_party/scrypt/BUILD.bazel | 0 bazel/third_party/scrypt/scrypt.BUILD | 30 - ...e8fb2d1fd8a3d641d7c33705691d41a2a860.patch | 14 - bazel/third_party/secp256k1/BUILD.bazel | 0 bazel/third_party/secp256k1/secp256k1.BUILD | 30 - bazel/third_party/sigsegv/BUILD.bazel | 0 bazel/third_party/sigsegv/sigsegv.BUILD | 31 - ...db33fc1e2130f67c045327b0ec949032df1d.patch | 27 - bazel/third_party/softfloat/BUILD.bazel | 0 bazel/third_party/softfloat/softfloat.BUILD | 637 ------------------ bazel/third_party/sse2neon/BUILD.bazel | 0 bazel/third_party/sse2neon/sse2neon.BUILD | 7 - bazel/third_party/urcrypt/BUILD.bazel | 0 bazel/third_party/urcrypt/urcrypt.BUILD | 28 - bazel/third_party/uv/BUILD.bazel | 0 bazel/third_party/uv/uv.BUILD | 28 - bazel/third_party/whereami/BUILD.bazel | 0 bazel/third_party/whereami/whereami.BUILD | 8 - bazel/third_party/zlib/BUILD.bazel | 0 bazel/third_party/zlib/zlib.BUILD | 19 - bazel/toolchain/BUILD.bazel | 410 ----------- bazel/toolchain/cfg.bzl | 168 ----- docker/BUILD.bazel | 116 ---- pkg/c3/BUILD.bazel | 42 -- pkg/ent/BUILD.bazel | 49 -- pkg/noun/BUILD.bazel | 117 ---- pkg/noun/platform/darwin/BUILD.bazel | 8 - pkg/noun/platform/linux/BUILD.bazel | 8 - pkg/ur/BUILD.bazel | 39 -- pkg/vere/BUILD.bazel | 522 -------------- 75 files changed, 4196 deletions(-) delete mode 100644 .bazelrc delete mode 100644 BUILD.bazel delete mode 100644 PACE delete mode 100644 VERSION delete mode 100644 WORKSPACE.bazel delete mode 100644 bazel/BUILD.bazel delete mode 100644 bazel/common_settings.bzl delete mode 100644 bazel/repo.bzl delete mode 100644 bazel/third_party/aes_siv/BUILD.bazel delete mode 100644 bazel/third_party/aes_siv/aes_siv.BUILD delete mode 100644 bazel/third_party/argon2/BUILD.bazel delete mode 100644 bazel/third_party/argon2/argon2.BUILD delete mode 100644 bazel/third_party/avahi/BUILD.bazel delete mode 100644 bazel/third_party/avahi/avahi.BUILD delete mode 100644 bazel/third_party/curl/BUILD.bazel delete mode 100644 bazel/third_party/curl/curl.BUILD delete mode 100644 bazel/third_party/dbus/BUILD.bazel delete mode 100644 bazel/third_party/dbus/dbus.BUILD delete mode 100644 bazel/third_party/ed25519/BUILD.bazel delete mode 100644 bazel/third_party/ed25519/ed25519.BUILD delete mode 100644 bazel/third_party/expat/BUILD.bazel delete mode 100644 bazel/third_party/expat/expat.BUILD delete mode 100644 bazel/third_party/gmp/BUILD.bazel delete mode 100644 bazel/third_party/gmp/gmp.BUILD delete mode 100644 bazel/third_party/h2o/2.2.6.patch delete mode 100644 bazel/third_party/h2o/BUILD.bazel delete mode 100644 bazel/third_party/h2o/h2o.BUILD delete mode 100644 bazel/third_party/keccak_tiny/64b6647514212b76ae7bca0dea9b7b197d1d8186.patch delete mode 100644 bazel/third_party/keccak_tiny/BUILD.bazel delete mode 100644 bazel/third_party/keccak_tiny/keccak_tiny.BUILD delete mode 100644 bazel/third_party/libbacktrace/BUILD.bazel delete mode 100644 bazel/third_party/libbacktrace/libbacktrace.BUILD delete mode 100644 bazel/third_party/libunwind/BUILD.bazel delete mode 100644 bazel/third_party/libunwind/libunwind.BUILD delete mode 100644 bazel/third_party/lmdb/0.9.29.patch delete mode 100644 bazel/third_party/lmdb/BUILD.bazel delete mode 100644 bazel/third_party/lmdb/lmdb.BUILD delete mode 100644 bazel/third_party/murmur3/BUILD.bazel delete mode 100644 bazel/third_party/murmur3/murmur3.BUILD delete mode 100644 bazel/third_party/natpmp/BUILD.bazel delete mode 100644 bazel/third_party/natpmp/natpmp.BUILD delete mode 100644 bazel/third_party/openssl/BUILD.bazel delete mode 100644 bazel/third_party/openssl/openssl.BUILD delete mode 100644 bazel/third_party/pdjson/BUILD.bazel delete mode 100644 bazel/third_party/pdjson/pdjson.BUILD delete mode 100644 bazel/third_party/scrypt/BUILD.bazel delete mode 100644 bazel/third_party/scrypt/scrypt.BUILD delete mode 100644 bazel/third_party/secp256k1/694ce8fb2d1fd8a3d641d7c33705691d41a2a860.patch delete mode 100644 bazel/third_party/secp256k1/BUILD.bazel delete mode 100644 bazel/third_party/secp256k1/secp256k1.BUILD delete mode 100644 bazel/third_party/sigsegv/BUILD.bazel delete mode 100644 bazel/third_party/sigsegv/sigsegv.BUILD delete mode 100644 bazel/third_party/softfloat/5c06db33fc1e2130f67c045327b0ec949032df1d.patch delete mode 100644 bazel/third_party/softfloat/BUILD.bazel delete mode 100644 bazel/third_party/softfloat/softfloat.BUILD delete mode 100644 bazel/third_party/sse2neon/BUILD.bazel delete mode 100644 bazel/third_party/sse2neon/sse2neon.BUILD delete mode 100644 bazel/third_party/urcrypt/BUILD.bazel delete mode 100644 bazel/third_party/urcrypt/urcrypt.BUILD delete mode 100644 bazel/third_party/uv/BUILD.bazel delete mode 100644 bazel/third_party/uv/uv.BUILD delete mode 100644 bazel/third_party/whereami/BUILD.bazel delete mode 100644 bazel/third_party/whereami/whereami.BUILD delete mode 100644 bazel/third_party/zlib/BUILD.bazel delete mode 100644 bazel/third_party/zlib/zlib.BUILD delete mode 100644 bazel/toolchain/BUILD.bazel delete mode 100644 bazel/toolchain/cfg.bzl delete mode 100644 docker/BUILD.bazel delete mode 100644 pkg/c3/BUILD.bazel delete mode 100644 pkg/ent/BUILD.bazel delete mode 100644 pkg/noun/BUILD.bazel delete mode 100644 pkg/noun/platform/darwin/BUILD.bazel delete mode 100644 pkg/noun/platform/linux/BUILD.bazel delete mode 100644 pkg/ur/BUILD.bazel delete mode 100644 pkg/vere/BUILD.bazel diff --git a/.bazelrc b/.bazelrc deleted file mode 100644 index 2a72ab5654..0000000000 --- a/.bazelrc +++ /dev/null @@ -1,51 +0,0 @@ -# Enable configurations specific to the host platform. -common --enable_platform_specific_config - -# Disallow empty `glob()`s. -build --incompatible_disallow_empty_glob - -# Don't auto-detect the C/C++ toolchain. -build --action_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1 - -# Use platforms to select toolchains. -# See https://bazel.build/concepts/platforms#cxx and -# https://github.com/bazelbuild/bazel/issues/7260. -build --incompatible_enable_cc_toolchain_resolution - -# Disable transitions. -# See https://github.com/bazelbuild/rules_docker/issues/2052. -build --@io_bazel_rules_docker//transitions:enable=false - -# Add aliases for compiler version build settings. -build --flag_alias=clang_version=//:clang_version -build --flag_alias=gcc_version=//:gcc_version - -# Use optimized build by default. According to the bazel documentation, this -# corresponds to -O2 -DNDEBUG, but these are overriden in -# //bazel/common_settings.bzl:vere_library. -# https://bazel.build/docs/user-manual#build-semantics -build --compilation_mode=opt - -# Don't include source level debug info on macOS. See -# https://github.com/urbit/urbit/issues/5561 and -# https://github.com/urbit/vere/issues/131. -build:linux --host_copt='-g' -build --strip=never - -# Turn on optimization, CPU and memory debug for exec config, which we only use -# to run the fake ship tests. Also turn on extra snapshot validation. -build --host_copt='-O3' -build --host_copt='-DU3_CPU_DEBUG' -build --host_copt='-DU3_MEMORY_DEBUG' -build --host_copt='-DC3DBG' -build --host_copt='-DU3_SNAPSHOT_VALIDATION' - -# Set as per-file copts as a plain --copt gets passed to third party -# dependencies which forces recompilation (slow) if you are switching between -# including/excluding a symbol define. -build:mem_dbg --per_file_copt='pkg/.*@-DU3_MEMORY_DEBUG' -build:cpu_dbg --per_file_copt='pkg/.*@-DU3_CPU_DEBUG' -build:snp_dbg --per_file_copt='pkg/.*@-DU3_SNAPSHOT_VALIDATION' - -# Any personal configuration should go in .user.bazelrc. -try-import %workspace%/.user.bazelrc diff --git a/BUILD.bazel b/BUILD.bazel deleted file mode 100644 index 730fe0ec81..0000000000 --- a/BUILD.bazel +++ /dev/null @@ -1,214 +0,0 @@ -load("//bazel:common_settings.bzl", "string_flag") -exports_files(["PACE"]) - -# -# OS-CPU CONFIG SETTINGS -# - -config_setting( - name = "linux_aarch64", - constraint_values = [ - "@platforms//os:linux", - "@platforms//cpu:aarch64", - ], -) - -config_setting( - name = "linux_x86_64", - constraint_values = [ - "@platforms//os:linux", - "@platforms//cpu:x86_64", - ], -) - -config_setting( - name = "macos_aarch64", - constraint_values = [ - "@platforms//os:macos", - "@platforms//cpu:aarch64", - ], -) - -config_setting( - name = "macos_x86_64", - constraint_values = [ - "@platforms//os:macos", - "@platforms//cpu:x86_64", - ], -) - -# -# CONFIGS DETAILING WHEN TO ENABLE CERTAIN FEATURES BY DEFAULT. -# CHANGES BEHAVIOR OF //bazel/common_settings.bzl:vere_library. -# - -config_setting( - name = "thinlto", - constraint_values = [ - "@platforms//os:macos", - ], - values = { - "compilation_mode": "opt" - } -) - -config_setting( - name = "lto", - constraint_values = [ - "@platforms//os:linux", - ], - values = { - "compilation_mode": "opt" - } -) - -config_setting( - name = "debug", - values = { - "compilation_mode": "dbg" - } -) - -# -# COMPILERS -# - -# Version flag for clang. -string_flag( - name = "clang_version", - # macOS uses `clang-15.0.0` by default. - build_setting_default = "15.0.0", - visibility = ["//visibility:public"], -) - -# Version flag for gcc. -string_flag( - name = "gcc_version", - # musl-cross-make uses `gcc-11.4.0` by default. - build_setting_default = "11.4.0", - visibility = ["//visibility:public"], -) - -# -# PLATFORMS -# -# A platform takes one of two formats: `--` or `-` -# if the compiler is unspecified. - -# Linux aarch64 platform with unspecified compiler. -platform( - name = "linux-aarch64", - constraint_values = [ - "@platforms//os:linux", - "@platforms//cpu:aarch64", - ], -) - -# Linux x86_64 platform with unspecified compiler. -platform( - name = "linux-x86_64", - constraint_values = [ - "@platforms//os:linux", - "@platforms//cpu:x86_64", - ], -) - -# Linux x86_64 platform with gcc. -platform( - name = "gcc-linux-x86_64", - constraint_values = [ - ":gcc", - "@platforms//os:linux", - "@platforms//cpu:x86_64", - ], -) - -platform( - name = "macos-aarch64", - constraint_values = [ - "@platforms//os:macos", - "@platforms//cpu:aarch64", - ], -) - -platform( - name = "clang-macos-aarch64", - constraint_values = [ - ":clang", - "@platforms//os:macos", - "@platforms//cpu:aarch64", - ], -) - -platform( - name = "macos-x86_64", - constraint_values = [ - "@platforms//os:macos", - "@platforms//cpu:x86_64", - ], -) - -platform( - name = "clang-macos-x86_64", - constraint_values = [ - ":clang", - "@platforms//os:macos", - "@platforms//cpu:x86_64", - ], -) - -# Produce the `U3_VERE_PACE` macro. -genrule( - name = "pace_hdr", - srcs = ["PACE"], - outs = ["pace.h"], - cmd = """ - echo '#ifndef URBIT_PACE_H' > $@ - echo '#define URBIT_PACE_H' >> $@ - printf '#define U3_VERE_PACE "%s"\n' $$(cat $< | tr -d '\n') >> $@ - echo '#endif' >> $@ - """, - visibility = ["//:__subpackages__"], -) - -genrule( - name = "version_str", - srcs = [ - # Bazel warns dependency checking directories is unsound, but there's no - # apparent way around depending on .git if we want to run git commands. - ".git", - "PACE", - "VERSION", - ], - outs = ["version"], - cmd = """ - version_num=$$(cat $(execpath VERSION) | tr -d '\n') - commit_sha=$$(git rev-parse --short HEAD) - if [ $$(tr -d '\n' < $(execpath PACE)) == "live" ]; then - version="$$version_num" - else - version="$$version_num-$$commit_sha" - fi - echo -n $$version > $@ - """, - visibility = ["//:__subpackages__"], -) - -# Produce the `URBIT_VERSION` macro. -genrule( - name = "version_hdr", - srcs = [":version_str"], - outs = ["version.h"], - cmd = """ - echo '#ifndef URBIT_VERSION_H' > $@ - echo '#define URBIT_VERSION_H' >> $@ - printf '#define URBIT_VERSION "%s"\n' $$(cat $< | tr -d '\n') >> $@ - echo '#endif' >> $@ - """, - visibility = ["//:__subpackages__"], -) - -alias( - name = "urbit", - actual = "//pkg/vere:urbit", -) diff --git a/PACE b/PACE deleted file mode 100644 index b2d1a7762f..0000000000 --- a/PACE +++ /dev/null @@ -1 +0,0 @@ -once diff --git a/VERSION b/VERSION deleted file mode 100644 index a3ec5a4bd3..0000000000 --- a/VERSION +++ /dev/null @@ -1 +0,0 @@ -3.2 diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel deleted file mode 100644 index e12aa50f5d..0000000000 --- a/WORKSPACE.bazel +++ /dev/null @@ -1,448 +0,0 @@ -# BUMPING A DEPENDENCY VERSION -# -# The general process for bumping the version of a dependency is as follows: -# -# (1) Update the `version` attribute in the dependency's repository rule (likely -# `versioned_http_archive()` or `versioned_http_file()`. -# (2) Download the source code of the new version of the dependency using -# the specified `url` attribute (substituting `{version}` with the actual -# version you're updating to in the URL). -# (3) Compute the SHA-256 hash of the new version's tarball (or zip archive) -# using `openssl`: -# ``` -# $ openssl dgst -sha256 -# ``` -# (4) Update the `sha256` attribute in the dependency's repository rule with the -# SHA-256 hash from (3). -# (5) Run `bazel clean` to ensure that Bazel removes the old version of the -# dependency. -# -# If a dependency has documention specific to that dependency that conflicts -# with the process described above, adhere to the dependency-specific -# documentation. -# -# UPDATING A DEPENDENCY'S BUILD FILE -# -# It's unlikely that you'll need to update a dependency's build file, but if you -# do, you presumably know what you're doing. Nonetheless, here are a few useful -# bits of non-obvious information: -# -# - Build logic for a dependency resides within its `.BUILD` file. -# `BUILD.bazel` is also present in the dependency's directory in -# `third_party/` to mark the directory as a package. Ideally, the logic in -# `.BUILD` would live in `BUILD.bazel`, but Bazel 5.3.1 cannot -# find the source files of the dependency when `BUILD.bazel` holds the build -# logic (at least on my machine - ThinkPad X1 Carbon Gen 9, Arch Linux). -# - Whenever possible, avoid delegating to foreign build systems. Delegating is -# inevitable when a third party dependency has a particularly complicated -# build system that would be too difficult to replicate in Bazel (i.e. -# OpenSSL). However, delegate only as a last resort. -# -# PATCHING A DEPENDENCY -# -# If a dependency has a bug that prevents it from being used in the project, -# you can patch the dependency to fix it. We recommend the process below: -# -# (1) Make a change to the source of the dependency. If the change is -# platform-specific, wrap it in the appropriate URBIT_RUNTIME_* macro. -# We "gate" source patches with these macros so that we only ever need -# at most one patch per dependency. -# (a) CPU architecture macros: `URBIT_RUNTIME_CPU_{X86_64,AARCH64}`. -# (b) OS macros: `URBIT_RUNTIME_OS_{LINUX,DARWIN,BSD,MINGW}`. -# (2) Generate a patch file named .patch and save it in -# `bazel/third_party/`. -# (3) Update the dependency's targets to locally define the platform-specific -# patch macro(s). `select()` statements should be used to define the -# correct macro for the current platform. -# (4) Update the dependency's repository rule in WORKSPACE.bazel to include -# `patch_args = ["-p1"]` (assuming the patch was generated with `git diff`) -# and `patches = ["//bazel/third_party/:.patch"]`. - -load("//bazel:repo.bzl", "versioned_http_archive", "versioned_http_file") - -# -# RULES REPOSITORIES -# - -versioned_http_archive( - name = "bazel_skylib", - sha256 = "74d544d96f4a5bb630d465ca8bbcfe231e3594e5aae57e1edbf17a6eb3ca2506", - url = "https://github.com/bazelbuild/bazel-skylib/releases/download/{version}/bazel-skylib-{version}.tar.gz", - version = "1.3.0", -) - -versioned_http_archive( - name = "rules_cc", - sha256 = "af6cc82d87db94585bceeda2561cb8a9d55ad435318ccb4ddfee18a43580fb5d", - strip_prefix = "rules_cc-{version}", - url = "https://github.com/bazelbuild/rules_cc/releases/download/{version}/rules_cc-{version}.tar.gz", - version = "0.0.4", -) - -versioned_http_archive( - name = "rules_foreign_cc", - sha256 = "2a4d07cd64b0719b39a7c12218a3e507672b82a97b98c6a89d38565894cf7c51", - strip_prefix = "rules_foreign_cc-{version}", - url = "https://github.com/bazelbuild/rules_foreign_cc/archive/refs/tags/{version}.tar.gz", - version = "0.9.0", -) - -# -# TOOLCHAINS SETUP -# - -# Use the toolchains we've configured. -register_toolchains( - "//bazel/toolchain:gcc-linux-aarch64-toolchain", - "//bazel/toolchain:clang-macos-aarch64-toolchain", - "//bazel/toolchain:clang-macos-x86_64-toolchain", - "//bazel/toolchain:gcc-linux-x86_64-toolchain", -) - -load("@rules_foreign_cc//foreign_cc:repositories.bzl", "rules_foreign_cc_dependencies") - -# See https://bazelbuild.github.io/rules_foreign_cc/0.9.0/flatten.html#rules_foreign_cc_dependencies. -rules_foreign_cc_dependencies( - register_built_tools = False, - register_default_tools = False, - register_preinstalled_tools = True, -) - -# -# THIRD PARTY DEPENDENCIES -# - -versioned_http_archive( - name = "aes_siv", - build_file = "//bazel/third_party/aes_siv:aes_siv.BUILD", - sha256 = "1916a428dff480e06b09dc0fb1c9d849c048f838dc9b8d141452233b508f6bb1", - strip_prefix = "libaes_siv-{version}", - url = "https://github.com/dfoxfranke/libaes_siv/archive/{version}.tar.gz", - version = "9681279cfaa6e6399bb7ca3afbbc27fc2e19df4b", -) - -versioned_http_archive( - name = "avahi", - build_file = "//bazel/third_party/avahi:avahi.BUILD", - sha256 = "060309d7a333d38d951bc27598c677af1796934dbd98e1024e7ad8de798fedda", - strip_prefix = "avahi-{version}", - url = "https://github.com/lathiat/avahi/releases/download/v{version}/avahi-{version}.tar.gz", - version = "0.8", -) - -versioned_http_archive( - name = "bazel_gazelle", - sha256 = "efbbba6ac1a4fd342d5122cbdfdb82aeb2cf2862e35022c752eaddffada7c3f3", - url = "https://github.com/bazelbuild/bazel-gazelle/releases/download/v{version}/bazel-gazelle-v{version}.tar.gz", - version = "0.27.0", -) - -# See https://curl.se/docs/caextract.html. -versioned_http_file( - name = "ca_bundle", - sha256 = "2cff03f9efdaf52626bd1b451d700605dc1ea000c5da56bd0fc59f8f43071040", - url = "https://curl.se/ca/cacert-{version}.pem", - version = "2022-10-11", -) - -versioned_http_archive( - name = "curl", - build_file = "//bazel/third_party/curl:curl.BUILD", - sha256 = "78a06f918bd5fde3c4573ef4f9806f56372b32ec1829c9ec474799eeee641c27", - strip_prefix = "curl-{version}", - url = "https://curl.se/download/curl-{version}.tar.gz", - version = "7.85.0", -) - -versioned_http_archive( - name = "dbus", - build_file = "//bazel/third_party/dbus:dbus.BUILD", - sha256 = "a6bd5bac5cf19f0c3c594bdae2565a095696980a683a0ef37cb6212e093bde35", - strip_prefix = "dbus-{version}", - url = "https://src.fedoraproject.org/repo/pkgs/dbus/dbus-{version}.tar.xz/sha512/f3dfc73da28cbe20449d15bbe4166c3574f0e551dfd15fca7cce2b8c71e778360ed2dd391ee5c414a7a47ff4b958727b26ef4cabfee70564f8d0a34bf5ad2386/dbus-1.14.8.tar.xz", - version = "1.14.8", -) - -versioned_http_archive( - name = "expat", - build_file = "//bazel/third_party/expat:expat.BUILD", - strip_prefix = "expat-{version}", - sha256 = "ef2420f0232c087801abf705e89ae65f6257df6b7931d37846a193ef2e8cdcbe", - # TODO: fix the R_2_5_0 nonsense - url = "https://github.com/libexpat/libexpat/releases/download/R_2_5_0/expat-{version}.tar.xz", - version = "2.5.0", -) - -versioned_http_archive( - name = "gmp", - build_file = "//bazel/third_party/gmp:gmp.BUILD", - sha256 = "fb776e19e0267053c3d6ffef0dcc001fb441da0e2b5224cfd9656aa2f1b8614d", - strip_prefix = "GMP-{version}", - url = "https://github.com/alisw/GMP/archive/refs/tags/v{version}.tar.gz", - version = "6.2.1", -) - -versioned_http_archive( - name = "h2o", - build_file = "//bazel/third_party/h2o:h2o.BUILD", - patch_args = ["-p1"], - patches = ["//bazel/third_party/h2o:{version}.patch"], - sha256 = "f8cbc1b530d85ff098f6efc2c3fdbc5e29baffb30614caac59d5c710f7bda201", - strip_prefix = "h2o-{version}", - url = "https://github.com/h2o/h2o/archive/refs/tags/v{version}.tar.gz", - # When bumping the version, compare `CMakeLists.txt` in the `h2o` repo to - # {build_file} and confirm that {build_file} remains an accurate description - # of the h2o build process. - version = "2.2.6", -) - -versioned_http_archive( - name = "io_bazel_rules_docker", - sha256 = "b1e80761a8a8243d03ebca8845e9cc1ba6c82ce7c5179ce2b295cd36f7e394bf", - url = "https://github.com/bazelbuild/rules_docker/releases/download/v{version}/rules_docker-v{version}.tar.gz", - version = "0.25.0", -) - -versioned_http_archive( - name = "io_bazel_rules_go", - sha256 = "d6ab6b57e48c09523e93050f13698f708428cfd5e619252e369d377af6597707", - url = "https://github.com/bazelbuild/rules_go/releases/download/v{version}/rules_go-v{version}.zip", - version = "0.43.0", -) - -versioned_http_file( - name = "ivory_pill", - sha256 = "26ff86808886beb831e4a135f478e42ce83ef4a09ad24808b3fe97248ce7a6b7", - url = "https://github.com/urbit/urbit/blob/{version}/bin/ivory.pill?raw=true", - version = "721fa05", -) - -versioned_http_archive( - name = "natpmp", - build_file = "//bazel/third_party/natpmp:natpmp.BUILD", - sha256 = "0684ed2c8406437e7519a1bd20ea83780db871b3a3a5d752311ba3e889dbfc70", - strip_prefix = "libnatpmp-{version}", - url = "http://download.openpkg.org/components/cache/libnatpmp/libnatpmp-{version}.tar.gz", - version = "20230423", -) - -versioned_http_file( - name = "solid_pill", - sha256 = "8b658fcee6978e2b19004a54233cab953e77ea0bb6c3a04d1bfda4ddc6be63c5", - url = "https://github.com/urbit/urbit/raw/{version}/bin/solid.pill", - version = "255fb1ca8206072f1d09425f0db61ecfe7ff5b17", -) - -versioned_http_archive( - name = "libbacktrace", - build_file = "//bazel/third_party/libbacktrace:libbacktrace.BUILD", - sha256 = "609c17352ec38eaf5ff6618fcbfb38cd8fa0e94a15a0d9aa259df514bbf47fcd", - url = "https://github.com/ianlancetaylor/libbacktrace/archive/{version}.tar.gz", - strip_prefix = "libbacktrace-{version}", - version = "4ead348bb45f753121ca0bd44170ff8352d4c514", -) - -versioned_http_archive( - name = "libunwind", - build_file = "//bazel/third_party/libunwind:libunwind.BUILD", - sha256 = "ddf0e32dd5fafe5283198d37e4bf9decf7ba1770b6e7e006c33e6df79e6a6157", - url = "https://github.com/libunwind/libunwind/releases/download/v1.8.1/libunwind-1.8.1.tar.gz", - strip_prefix = "libunwind-{version}", - version = "1.8.1", -) - -versioned_http_archive( - name = "lmdb", - build_file = "//bazel/third_party/lmdb:lmdb.BUILD", - patch_args = ["-p1"], - patches = ["//bazel/third_party/lmdb:{version}.patch"], - sha256 = "22054926b426c66d8f2bc22071365df6e35f3aacf19ad943bc6167d4cae3bebb", - strip_prefix = "lmdb-LMDB_{version}/libraries/liblmdb", - url = "https://github.com/LMDB/lmdb/archive/refs/tags/LMDB_{version}.tar.gz", - # When bumping the version, compare `libraries/liblmdb/Makefile` in the - # `lmdb` repo to {build_file} and confirm that {build_file} remains an - # accurate description of the lmdb build process. - version = "0.9.29", -) - -versioned_http_archive( - name = "murmur3", - build_file = "//bazel/third_party/murmur3:murmur3.BUILD", - sha256 = "d81836605204df2db9e0c095423b2856073d1b2ef900463151d0663b7ca3164f", - strip_prefix = "murmur3-{version}", - url = "https://github.com/PeterScott/murmur3/archive/{version}.tar.gz", - # When bumping the version, compare `makefile` in the `murmur3` repo to - # {build_file} and confirm that {build_file} remains an accurate description - # of the murmur3 build process. - version = "dae94be0c0f54a399d23ea6cbe54bca5a4e93ce4", -) - -versioned_http_archive( - name = "openssl", - build_file = "//bazel/third_party/openssl:openssl.BUILD", - sha256 = "cf3098950cb4d853ad95c0841f1f9c6d3dc102dccfcacd521d93925208b76ac8", - strip_prefix = "openssl-{version}", - url = "https://github.com/openssl/openssl/releases/download/OpenSSL_1_1_1w/openssl-{version}.tar.gz", - version = "1.1.1w", -) - -versioned_http_archive( - name = "pdjson", - build_file = "//bazel/third_party/pdjson:pdjson.BUILD", - sha256 = "928913d44d9021d69c0d23a8f59ed67028e5abf1ae7910f79c23a7af4e779b92", - strip_prefix = "pdjson-{version}", - url = "https://github.com/skeeto/pdjson/archive/{version}.tar.gz", - # When bumping the version, compare `Makefile` in the `pdjson` repo to - # {build_file} and confirm that {build_file} remains an accurate description - # of the pdjson build process. - version = "67108d883061043e55d0fb13961ac1b6fc8a485c", -) - -versioned_http_archive( - name = "secp256k1", - build_file = "//bazel/third_party/secp256k1:secp256k1.BUILD", - sha256 = "5f6e4a66bf8f3c318d91eacbf3262d1cd81a3fda6bb9af267b54cf38ffd44b1c", - strip_prefix = "secp256k1-{version}", - url = "https://github.com/bitcoin-core/secp256k1/archive/{version}.tar.gz", - version = "694ce8fb2d1fd8a3d641d7c33705691d41a2a860", - patch_args = ["-p1"], - patches = ["//bazel/third_party/secp256k1:{version}.patch"], -) - -versioned_http_archive( - name = "sigsegv", - build_file = "//bazel/third_party/sigsegv:sigsegv.BUILD", - sha256 = "cdac3941803364cf81a908499beb79c200ead60b6b5b40cad124fd1e06caa295", - strip_prefix = "libsigsegv-{version}", - url = "https://ftp.gnu.org/gnu/libsigsegv/libsigsegv-{version}.tar.gz", - version = "2.14", -) - -versioned_http_archive( - name = "softfloat", - build_file = "//bazel/third_party/softfloat:softfloat.BUILD", - patch_args = ["-p1"], - patches = ["//bazel/third_party/softfloat:{version}.patch"], - sha256 = "15ad5841e88fe09422a8e31a0ef3fe126ecf678f52c9a3882f3373d47752aebe", - strip_prefix = "berkeley-softfloat-3-{version}", - url = "https://github.com/ucb-bar/berkeley-softfloat-3/archive/{version}.tar.gz", - # When bumping the version, compare `build//Makefile` in the - # `softfloat` repo to {build_file} and compare that {build_file} remains an - # accurate description of the softfloat process for *all* supported - # ``s. - version = "5c06db33fc1e2130f67c045327b0ec949032df1d", -) - -versioned_http_archive( - name = "sse2neon", - build_file = "//bazel/third_party/sse2neon:sse2neon.BUILD", - sha256 = "4001e2dfb14fcf3831211581ed83bcc83cf6a3a69f638dcbaa899044a351bb2a", - strip_prefix = "sse2neon-{version}", - url = "https://github.com/DLTcollab/sse2neon/archive/refs/tags/v{version}.tar.gz", - version = "1.5.1", -) - -versioned_http_file( - name = "urbit", - sha256 = "a729d8b3c438fef33f5ae8c4da8d84ebdb5af5ad028d41696a92cddc8bf362a8", - url = "https://github.com/urbit/urbit/archive/{version}.tar.gz", - # We can't use a branch name for the `version` because each new commit - # will change the SHA256 hash. - version = "ea8fee3aa0434d4bdf1bf785e5ec346c7ecba7fd", -) - -versioned_http_archive( - name = "urcrypt", - build_file = "//bazel/third_party/urcrypt:urcrypt.BUILD", - sha256 = "d27ec04d3854da7c479dd815af92ffef986616bc7ff400022e2dfb7971853d86", - strip_prefix = "urcrypt-{version}", - url = "https://github.com/urbit/urcrypt/archive/{version}.tar.gz", - version = "9ae5d604528bc54ae48430f55ebbb17b1ad7956c", -) - -versioned_http_archive( - name = "uv", - build_file = "//bazel/third_party/uv:uv.BUILD", - sha256 = "ccfcdc968c55673c6526d8270a9c8655a806ea92468afcbcabc2b16040f03cb4", - strip_prefix = "libuv-v{version}", - url = "https://dist.libuv.org/dist/v{version}/libuv-v{version}.tar.gz", - version = "1.44.2", -) - -versioned_http_archive( - name = "whereami", - build_file = "//bazel/third_party/whereami:whereami.BUILD", - sha256 = "1d8744177f37e8386ec2f6c5992592399040cb93535ed4fd253e1976f889a744", - strip_prefix = "whereami-{version}", - url = "https://github.com/gpakosz/whereami/archive/{version}.tar.gz", - version = "ba364cd54fd431c76c045393b6522b4bff547f50", -) - -versioned_http_archive( - name = "zlib", - build_file = "//bazel/third_party/zlib:zlib.BUILD", - sha256 = "9a93b2b7dfdac77ceba5a558a580e74667dd6fede4585b91eefb60f03b72df23", - strip_prefix = "zlib-{version}", - url = "https://www.zlib.net/zlib-{version}.tar.gz", - version = "1.3.1", -) - -# -# HEDRON COMPILE COMMANDS SETUP -# - -# Hedron's Compile Commands Extractor for Bazel -# https://github.com/hedronvision/bazel-compile-commands-extractor -versioned_http_archive( - name = "hedron_compile_commands", - strip_prefix = "bazel-compile-commands-extractor-{version}", - sha256 = "d7ba7708816132f86f02864b9dba0c5abf249cc0fb035a34c430e4e538c87867", - url = "https://github.com/hedronvision/bazel-compile-commands-extractor/archive/{version}.tar.gz", - version = "d3afb5dfadd4beca48bb027112d029f2d34ff0a0", -) - -load("@hedron_compile_commands//:workspace_setup.bzl", "hedron_compile_commands_setup") - -hedron_compile_commands_setup() - -# -# DOCKER SETUP -# - -# These must be loaded before the Docker rules. -# See https://github.com/bazelbuild/rules_docker/issues/2075#issuecomment-1115954091. -load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies") -load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies") - -go_rules_dependencies() - -go_register_toolchains(version = "1.18") - -gazelle_dependencies(go_repository_default_config = "//:WORKSPACE.bazel") - -load( - "@io_bazel_rules_docker//repositories:repositories.bzl", - _container_repositories = "repositories", -) -load("@io_bazel_rules_docker//repositories:deps.bzl", _container_deps = "deps") -load("@io_bazel_rules_docker//cc:image.bzl", _cc_image_repos = "repositories") - -_container_repositories() - -_container_deps( - # See https://github.com/bazelbuild/rules_docker/issues/1902. - go_repository_default_config = "@//:WORKSPACE.bazel", -) - -_cc_image_repos() - -load("@io_bazel_rules_docker//container:container.bzl", "container_pull") - -container_pull( - name = "alpine_linux_x86_64", - timeout = 300, # [seconds] - architecture = "amd64", - digest = "sha256:93d5a28ff72d288d69b5997b8ba47396d2cbb62a72b5d87cd3351094b5d578a0", - registry = "docker.io", - repository = "alpine", -) diff --git a/bazel/BUILD.bazel b/bazel/BUILD.bazel deleted file mode 100644 index 5ac74d0b96..0000000000 --- a/bazel/BUILD.bazel +++ /dev/null @@ -1,21 +0,0 @@ -load("@hedron_compile_commands//:refresh_compile_commands.bzl", "refresh_compile_commands") - -# gazelle:exclude external - -refresh_compile_commands( - name = "refresh_compile_commands", - - # Specify the targets of interest. - # For example, specify a dict of targets and any flags required to build. - targets = [ - "//pkg/ent", - "//pkg/noun", - "//pkg/ur", - "//pkg/vere:urbit", - ], - # No need to add flags already in .bazelrc. They're automatically picked up. - # If you don't need flags, a list of targets is also okay, as is a single target string. - # Wildcard patterns, like //... for everything, *are* allowed here, just like a build. - # As are additional targets (+) and subtractions (-), like in bazel query https://docs.bazel.build/versions/main/query.html#expressions - # And if you're working on a header-only library, specify a test or binary target that compiles it. -) diff --git a/bazel/common_settings.bzl b/bazel/common_settings.bzl deleted file mode 100644 index 9ec74beda5..0000000000 --- a/bazel/common_settings.bzl +++ /dev/null @@ -1,50 +0,0 @@ -# For more, see https://bazel.build/extending/config and -# https://github.com/bazelbuild/bazel-skylib/blob/main/rules/common_settings.bzl. -load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo") - -def _impl(ctx): - return BuildSettingInfo(value = ctx.build_setting_value) - -string_flag = rule( - implementation = _impl, - build_setting = config.string(flag = True), - doc = "A string-typed build setting that can be set on the command line", -) - -def vere_library(copts = [], linkopts = [], **kwargs): - native.cc_library( - copts = copts + [ - "-fno-omit-frame-pointer", - ] + select({ - "//:debug": ["-O0", "-g3", "-DC3DBG", "-fdebug-compilation-dir=."], - "//conditions:default": ["-O3", "-g"] - }) + select({ - "//:lto": ['-flto'], - "//conditions:default": [] - }), - linkopts = linkopts + ['-g'] + select({ - "//:lto": ['-flto'], - "//:thinlto": ['-flto=thin'], - "//conditions:default": [] - }), - **kwargs, - ) - -def vere_binary(copts = [], linkopts = [], **kwargs): - native.cc_binary( - copts = copts + [ - "-fno-omit-frame-pointer", - ] + select({ - "//:debug": ["-O0", "-g3", "-DC3DBG", "-fdebug-compilation-dir=."], - "//conditions:default": ["-O3", "-g"] - }) + select({ - "//:lto": ['-flto'], - "//conditions:default": [] - }), - linkopts = linkopts + ['-g'] + select({ - "//:lto": ['-flto'], - "//:thinlto": ['-flto=thin'], - "//conditions:default": [] - }), - **kwargs, - ) diff --git a/bazel/repo.bzl b/bazel/repo.bzl deleted file mode 100644 index 2c2eb1bb1f..0000000000 --- a/bazel/repo.bzl +++ /dev/null @@ -1,34 +0,0 @@ -load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive", "http_file") -load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") - -# Adds a `version` attribute to `http_archive`. -def versioned_http_archive(name, version, **kwargs): - if kwargs.get("url") != None: - kwargs["url"] = kwargs["url"].format(version = version) - - if kwargs.get("urls") != None: - for index, url in enumerate(kwargs["urls"]): - kwargs["urls"][index] = url.format(version = version) - - if kwargs.get("patches") != None: - for index, patch in enumerate(kwargs["patches"]): - kwargs["patches"][index] = patch.format(version = version) - - if kwargs.get("strip_prefix") != None: - kwargs["strip_prefix"] = kwargs["strip_prefix"].format(version = version) - - maybe(http_archive, name, **kwargs) - -# Adds a `version` attribute to `http_file`. -def versioned_http_file(name, version, **kwargs): - if kwargs.get("url") != None: - kwargs["url"] = kwargs["url"].format(version = version) - - if kwargs.get("urls") != None: - for index, url in enumerate(kwargs["urls"]): - kwargs["urls"][index] = url.format(version = version) - - if kwargs.get("strip_prefix") != None: - kwargs["strip_prefix"] = kwargs["strip_prefix"].format(version = version) - - maybe(http_file, name, **kwargs) diff --git a/bazel/third_party/aes_siv/BUILD.bazel b/bazel/third_party/aes_siv/BUILD.bazel deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/bazel/third_party/aes_siv/aes_siv.BUILD b/bazel/third_party/aes_siv/aes_siv.BUILD deleted file mode 100644 index 1cb6d7469f..0000000000 --- a/bazel/third_party/aes_siv/aes_siv.BUILD +++ /dev/null @@ -1,21 +0,0 @@ -genrule( - name = "config", - srcs = ["config.h.in"], - outs = ["config.h"], - # The options in config.h.in only affect libaes_siv's testing behavior, so - # it's sufficient to generate an empty config.h. - cmd = "touch $@", -) - -cc_library( - name = "aes_siv", - srcs = [ - "aes_siv.c", - "config.h", - ], - hdrs = ["aes_siv.h"], - copts = ["-O3"], - includes = ["."], - visibility = ["//visibility:public"], - deps = ["@openssl"], -) diff --git a/bazel/third_party/argon2/BUILD.bazel b/bazel/third_party/argon2/BUILD.bazel deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/bazel/third_party/argon2/argon2.BUILD b/bazel/third_party/argon2/argon2.BUILD deleted file mode 100644 index ebfc7d5f3c..0000000000 --- a/bazel/third_party/argon2/argon2.BUILD +++ /dev/null @@ -1,42 +0,0 @@ -cc_library( - name = "argon2", - srcs = [ - "src/argon2.c", - "src/blake2/blake2-impl.h", - "src/blake2/blake2b.c", - "src/core.c", - "src/core.h", - "src/encoding.c", - "src/encoding.h", - "src/thread.c", - "src/thread.h", - ] + select({ - # `opt.c` requires SSE instructions. See `Makefile` in the `argon2` repo - # for the check used to determine whether to use `opt.c` or `ref.c`. - "@platforms//cpu:x86_64": [ - "src/blake2/blamka-round-opt.h", - "src/opt.c", - ], - "//conditions:default": [ - "src/blake2/blamka-round-ref.h", - "src/ref.c", - ], - }), - hdrs = [ - "include/argon2.h", - "src/blake2/blake2.h", - ], - copts = [ - "-std=c89", - "-O3", - "-Wall", - "-g", - "-Isrc", - "-DARGON2_NO_THREADS", - ], - includes = [ - "include", - "src/blake2", - ], - visibility = ["//visibility:public"], -) diff --git a/bazel/third_party/avahi/BUILD.bazel b/bazel/third_party/avahi/BUILD.bazel deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/bazel/third_party/avahi/avahi.BUILD b/bazel/third_party/avahi/avahi.BUILD deleted file mode 100644 index 2c0896f32d..0000000000 --- a/bazel/third_party/avahi/avahi.BUILD +++ /dev/null @@ -1,30 +0,0 @@ -load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make") - -filegroup( - name = "all", - srcs = glob(["**"]), -) - -cc_library( - name = "dns-sd", - hdrs = ["dns_sd.h"], - visibility = ["//visibility:public"], -) - -configure_make( - name = "avahi", - args = select({ - "@platforms//os:macos": ["--jobs=`sysctl -n hw.logicalcpu`"], - "//conditions:default": ["--jobs=`nproc`"], - }), - configure_options = ["--with-dbus-system-address='unix:path=/var/run/dbus/system_bus_socket' --with-xml=none --disable-libevent --disable-glib --disable-gobject --disable-gdbm --disable-qt3 --disable-qt4 --disable-qt5 --disable-gtk --disable-gtk3 --disable-mono --disable-monodoc --disable-python --disable-libdaemon --enable-compat-libdns_sd --disable-rpath --with-distro=none"], - lib_source = ":all", - # out_include_dir = "avahi-compat-libdns_sd", - deps = ["@dbus"], - configure_in_place = True, - autogen = True, - autoconf = True, - autogen_command = "bootstrap.sh", - out_static_libs = ["libdns_sd.a", "libavahi-client.a", "libavahi-common.a"], - visibility = ["//visibility:public"], -) diff --git a/bazel/third_party/curl/BUILD.bazel b/bazel/third_party/curl/BUILD.bazel deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/bazel/third_party/curl/curl.BUILD b/bazel/third_party/curl/curl.BUILD deleted file mode 100644 index c66424d52e..0000000000 --- a/bazel/third_party/curl/curl.BUILD +++ /dev/null @@ -1,67 +0,0 @@ -load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make") - -filegroup( - name = "all", - srcs = glob(["**"]), -) - -configure_make( - name = "curl", - args = select({ - "@platforms//os:macos": ["--jobs=`sysctl -n hw.logicalcpu`"], - "//conditions:default": ["--jobs=`nproc`"], - }), - # We disable unneeded features. - # TODO: double check that all disabled features below are in fact unneeded. - configure_options = [ - "--disable-alt-svc", - "--disable-ares", - "--disable-cookies", - "--disable-crypto-auth", - "--disable-dateparse", - "--disable-dnsshuffle", - "--disable-doh", - "--disable-get-easy-options", - "--disable-hsts", - "--disable-http-auth", - "--disable-ipv6", - "--disable-ldap", - "--disable-libcurl-option", - "--disable-manual", - "--disable-shared", - "--disable-netrc", - "--disable-ntlm-wb", - "--disable-progress-meter", - "--disable-proxy", - "--disable-pthreads", - "--disable-socketpair", - "--disable-threaded-resolver", - "--disable-tls-srp", - "--disable-unix-sockets", - "--disable-verbose", - "--disable-versioned-symbols", - "--enable-static", - # Use our openssl, not the system's openssl. - "--with-openssl=$URBIT_RUNTIME_OPENSSL", - "--without-brotli", - "--without-libidn2", - "--without-libpsl", - "--without-librtmp", - "--without-nghttp2", - "--without-ngtcp2", - "--without-zlib", - "--without-zstd", - ] + select({ - "@//:linux_aarch64": ["--host=aarch64-linux-musl"], - "@//:linux_x86_64": ["--host=x86_64-linux-musl"], - "//conditions:default": [], - }), - copts = ["-O3"], - env = { - "URBIT_RUNTIME_OPENSSL": "$$PWD/$(GENDIR)/external/openssl/openssl", - }, - lib_source = ":all", - out_static_libs = ["libcurl.a"], - visibility = ["//visibility:public"], - deps = ["@openssl"], -) diff --git a/bazel/third_party/dbus/BUILD.bazel b/bazel/third_party/dbus/BUILD.bazel deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/bazel/third_party/dbus/dbus.BUILD b/bazel/third_party/dbus/dbus.BUILD deleted file mode 100644 index 25fd750092..0000000000 --- a/bazel/third_party/dbus/dbus.BUILD +++ /dev/null @@ -1,21 +0,0 @@ -load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make") - -filegroup( - name = "all", - srcs = glob(["**"]), -) - -configure_make( - name = "dbus", - lib_name = "libdbus-1", - args = select({ - "@platforms//os:macos": ["--jobs=`sysctl -n hw.logicalcpu`"], - "//conditions:default": ["--jobs=`nproc`"], - }), - copts = ["-O3"], - configure_options = ["--disable-selinux --without-x --disable-tests --disable-systemd --disable-apparmor"], - lib_source = ":all", - configure_in_place = True, - deps = ["@expat"], - visibility = ["//visibility:public"], -) diff --git a/bazel/third_party/ed25519/BUILD.bazel b/bazel/third_party/ed25519/BUILD.bazel deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/bazel/third_party/ed25519/ed25519.BUILD b/bazel/third_party/ed25519/ed25519.BUILD deleted file mode 100644 index 172752ca25..0000000000 --- a/bazel/third_party/ed25519/ed25519.BUILD +++ /dev/null @@ -1,14 +0,0 @@ -cc_library( - name = "ed25519", - srcs = glob( - [ - "src/*.c", - "src/*.h", - ], - exclude = ["src/ed25519.h"], - ), - hdrs = ["src/ed25519.h"], - copts = ["-O3"], - includes = ["src"], - visibility = ["//visibility:public"], -) diff --git a/bazel/third_party/expat/BUILD.bazel b/bazel/third_party/expat/BUILD.bazel deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/bazel/third_party/expat/expat.BUILD b/bazel/third_party/expat/expat.BUILD deleted file mode 100644 index 1c0a6bc36d..0000000000 --- a/bazel/third_party/expat/expat.BUILD +++ /dev/null @@ -1,24 +0,0 @@ -load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make") - -filegroup( - name = "all", - srcs = glob(["**"]), -) - -configure_make( - name = "expat", - args = select({ - "@platforms//os:macos": ["--jobs=`sysctl -n hw.logicalcpu`"], - "//conditions:default": ["--jobs=`nproc`"], - }), - configure_options = [ - ] + select({ - "@//:linux_aarch64": ["--host=aarch64-linux-musl"], - "@//:linux_x86_64": ["--host=x86_64-linux-musl"], - "//conditions:default": [], - }), - copts = ["-O3"], - lib_source = ":all", - out_static_libs = ["libexpat.a"], - visibility = ["//visibility:public"], -) diff --git a/bazel/third_party/gmp/BUILD.bazel b/bazel/third_party/gmp/BUILD.bazel deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/bazel/third_party/gmp/gmp.BUILD b/bazel/third_party/gmp/gmp.BUILD deleted file mode 100644 index 25ef1db6a4..0000000000 --- a/bazel/third_party/gmp/gmp.BUILD +++ /dev/null @@ -1,30 +0,0 @@ -load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make") - -filegroup( - name = "all", - srcs = glob(["**"]), -) - -configure_make( - name = "gmp", - args = select({ - "@platforms//os:macos": ["--jobs=`sysctl -n hw.logicalcpu`"], - "//conditions:default": ["--jobs=`nproc`"], - }), - configure_options = [ - "--disable-shared", - # NOTE: --with-pic is required to build PIE binaries on macos_x86_64, - # but we leave it in for all builds as a precaution. - "--with-pic", - ] + select({ - "@//:linux_aarch64": ["--host=aarch64-linux-musl"], - "@//:linux_x86_64": ["--host=x86_64-linux-musl"], - # See https://gmplib.org/list-archives/gmp-bugs/2023-January/005228.html. - "@//:macos_aarch64": ["--disable-assembly"], - "//conditions:default": [], - }), - copts = ["-O3"], - lib_source = ":all", - out_static_libs = ["libgmp.a"], - visibility = ["//visibility:public"], -) diff --git a/bazel/third_party/h2o/2.2.6.patch b/bazel/third_party/h2o/2.2.6.patch deleted file mode 100644 index c04571f7ad..0000000000 --- a/bazel/third_party/h2o/2.2.6.patch +++ /dev/null @@ -1,47 +0,0 @@ -diff --git a/deps/klib/ksw.c b/deps/klib/ksw.c -index 742fec90b..9a11a21bd 100644 ---- a/deps/klib/ksw.c -+++ b/deps/klib/ksw.c -@@ -25,7 +25,11 @@ - - #include - #include -+#if defined(URBIT_RUNTIME_CPU_AARCH64) -+#include "sse2neon.h" -+#else - #include -+#endif - #include "ksw.h" - - #ifdef __GNUC__ -diff --git a/deps/klib/test/kbit_test.c b/deps/klib/test/kbit_test.c -index 3ae3bd309..7db05a186 100644 ---- a/deps/klib/test/kbit_test.c -+++ b/deps/klib/test/kbit_test.c -@@ -1,7 +1,11 @@ - #include - #include - #include -+#if defined(URBIT_RUNTIME_CPU_AARCH64) -+#include "sse2neon.h" -+#else - #include -+#endif - #include "kbit.h" - - // from bowtie-0.9.8.1 -diff --git a/deps/picohttpparser/picohttpparser.c b/deps/picohttpparser/picohttpparser.c -index a707070d1..714850f64 100644 ---- a/deps/picohttpparser/picohttpparser.c -+++ b/deps/picohttpparser/picohttpparser.c -@@ -28,7 +28,9 @@ - #include - #include - #ifdef __SSE4_2__ --#ifdef _MSC_VER -+#if defined(URBIT_RUNTIME_CPU_AARCH64) -+#include "sse2neon.h" -+#elif defined(_MSC_VER) - #include - #else - #include diff --git a/bazel/third_party/h2o/BUILD.bazel b/bazel/third_party/h2o/BUILD.bazel deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/bazel/third_party/h2o/h2o.BUILD b/bazel/third_party/h2o/h2o.BUILD deleted file mode 100644 index 84b1f9b738..0000000000 --- a/bazel/third_party/h2o/h2o.BUILD +++ /dev/null @@ -1,375 +0,0 @@ -# This build file is derived from `CMakeLists.txt` in the `h2o` repo at -# https://github.com/h2o/h2o. - -# See `CC_WARNING_FLAGS` in `CMakeLists.txt` in the `h2o` repo. -CC_WARNING_FLAGS = [ - "-Wall", - "-Wno-unused-value", - "-Wno-unused-function", - "-Wno-nullability-completeness", - "-Wno-expansion-to-defined", - "-Werror=implicit-function-declaration", - "-Werror=incompatible-pointer-types", -] - -# -# H2O DEPENDENCIES -# - -# See `deps/cloxec` in the `h2o` repo. -cc_library( - name = "cloexec", - srcs = ["deps/cloexec/cloexec.c"], - hdrs = ["deps/cloexec/cloexec.h"], - copts = ["-O3"], - includes = ["deps/cloexec"], - linkstatic = True, - visibility = ["//visibility:private"], -) - -# See `deps/golombset` in the `h2o` repo. -cc_library( - name = "golombset", - hdrs = ["deps/golombset/golombset.h"], - copts = ["-O3"], - includes = ["deps/golombset"], - linkstatic = True, - visibility = ["//visibility:private"], -) - -# See `deps/klib` in the `h2o` repo. -cc_library( - name = "klib", - srcs = glob(["deps/klib/*.c"]), - hdrs = glob(["deps/klib/*.h"]), - copts = ["-O3"], - includes = ["deps/klib"], - linkstatic = True, - local_defines = select({ - "@platforms//cpu:aarch64": ["URBIT_RUNTIME_CPU_AARCH64"], - "//conditions:default": [], - }), - visibility = ["//visibility:private"], - deps = [ - "@curl", - "@zlib", - ] + select({ - "@platforms//cpu:aarch64": ["@sse2neon"], - "//conditions:default": [], - }), -) - -# See `deps/libgkc` in the `h2o` repo. -cc_library( - name = "libgkc", - srcs = ["deps/libgkc/gkc.c"], - hdrs = ["deps/libgkc/gkc.h"], - copts = ["-O3"], - includes = ["deps/libgkc"], - linkstatic = True, - visibility = ["//visibility:private"], -) - -# See `deps/libyrmcds` in the `h2o` repo. -cc_library( - name = "libyrmcds", - srcs = glob( - ["deps/libyrmcds/*.c"], - exclude = [ - "deps/libyrmcds/yc.c", - "deps/libyrmcds/yc-cnt.c", - ], - ) + [ - "deps/libyrmcds/yrmcds_portability.h", - "deps/libyrmcds/yrmcds_text.h", - ], - hdrs = ["deps/libyrmcds/yrmcds.h"], - copts = [ - "-Wall", - "-Wconversion", - "-gdwarf-3", - "-O2", - ], - includes = ["deps/libyrmcds"], - linkstatic = True, - visibility = ["//visibility:private"], -) - -# See `deps/picohttpparser` in the `h2o` repo. -cc_library( - name = "picohttpparser", - srcs = ["deps/picohttpparser/picohttpparser.c"], - hdrs = ["deps/picohttpparser/picohttpparser.h"], - copts = ["-O3"], - includes = ["deps/picohttpparser"], - linkstatic = True, - local_defines = select({ - "@platforms//cpu:aarch64": ["URBIT_RUNTIME_CPU_AARCH64"], - "//conditions:default": [], - }), - visibility = ["//visibility:private"], - deps = select({ - "@platforms//cpu:aarch64": ["@sse2neon"], - "//conditions:default": [], - }), -) - -# See `deps/picotls` in the `h2o` repo. -cc_library( - name = "picotls", - srcs = glob( - ["deps/picotls/lib/*.c"], - ), - hdrs = ["deps/picotls/include/picotls.h"] + glob( - ["deps/picotls/include/picotls/*.h"], - ), - copts = [ - "-std=c99", - "-Wall", - "-O2", - "-g", - ], - includes = [ - "deps/picotls/include", - "deps/picotls/include/picotls", - ], - linkstatic = True, - local_defines = select({ - "@platforms//cpu:aarch64": ["URBIT_RUNTIME_CPU_AARCH64"], - "//conditions:default": [], - }), - visibility = ["//visibility:private"], - deps = [ - ":cifra", - ":micro_ecc", - "@openssl", - ] + select({ - "@platforms//cpu:aarch64": ["@sse2neon"], - "//conditions:default": [], - }), -) - -# See `deps/ssl-conservatory` in the `h2o` repo. -cc_library( - name = "ssl_conservatory", - hdrs = ["deps/ssl-conservatory/openssl/openssl_hostname_validation.h"], - copts = ["-O3"], - includes = ["deps/ssl-conservatory/openssl"], - linkstatic = True, - textual_hdrs = ["deps/ssl-conservatory/openssl/openssl_hostname_validation.c"], - visibility = ["//visibility:private"], -) - -# See `deps/yoml` in the `h2o` repo. -cc_library( - name = "yoml", - hdrs = glob(["deps/yoml/*.h"]), - copts = ["-O3"], - includes = ["deps/yoml"], - linkstatic = True, - visibility = ["//visibility:private"], -) - -# -# PICOTLS DEPENDENCIES -# - -# See `deps/picotls/deps/cifra` in the `h2o` repo. -cc_library( - name = "cifra", - srcs = [ - "deps/picotls/deps/cifra/src/aes.c", - "deps/picotls/deps/cifra/src/sha256.c", - "deps/picotls/deps/cifra/src/sha512.c", - "deps/picotls/deps/cifra/src/chash.c", - "deps/picotls/deps/cifra/src/hmac.c", - "deps/picotls/deps/cifra/src/pbkdf2.c", - "deps/picotls/deps/cifra/src/modes.c", - "deps/picotls/deps/cifra/src/eax.c", - "deps/picotls/deps/cifra/src/gf128.c", - "deps/picotls/deps/cifra/src/blockwise.c", - "deps/picotls/deps/cifra/src/cmac.c", - "deps/picotls/deps/cifra/src/salsa20.c", - "deps/picotls/deps/cifra/src/chacha20.c", - "deps/picotls/deps/cifra/src/curve25519.c", - "deps/picotls/deps/cifra/src/gcm.c", - "deps/picotls/deps/cifra/src/cbcmac.c", - "deps/picotls/deps/cifra/src/ccm.c", - "deps/picotls/deps/cifra/src/sha3.c", - "deps/picotls/deps/cifra/src/sha1.c", - "deps/picotls/deps/cifra/src/poly1305.c", - "deps/picotls/deps/cifra/src/norx.c", - "deps/picotls/deps/cifra/src/chacha20poly1305.c", - "deps/picotls/deps/cifra/src/drbg.c", - "deps/picotls/deps/cifra/src/ocb.c", - ] + glob( - [ - "deps/picotls/deps/cifra/src/*.h", - "deps/picotls/deps/cifra/src/ext/*.h", - ], - exclude = ["deps/picotls/deps/cifra/src/ext/handy.h"], - ), - hdrs = glob( - [ - "deps/picotls/deps/cifra/src/*.h", - "deps/picotls/deps/cifra/src/ext/*.h", - ], - ), - copts = ["-O3"], - includes = [ - "deps/picotls/deps/cifra/src", - "deps/picotls/deps/cifra/src/ext", - ], - linkstatic = True, - textual_hdrs = ["deps/picotls/deps/cifra/src/curve25519.tweetnacl.c"], - visibility = ["//visibility:private"], -) - -# See `deps/picotls/deps/micro-ecc` in the `h2o` repo. -cc_library( - name = "micro_ecc", - srcs = [ - "deps/picotls/deps/micro-ecc/types.h", - "deps/picotls/deps/micro-ecc/uECC.c", - "deps/picotls/deps/micro-ecc/uECC_vli.h", - ], - hdrs = ["deps/picotls/deps/micro-ecc/uECC.h"], - copts = ["-O3"], - includes = ["deps/picotls/deps/micro-ecc"], - textual_hdrs = [ - "deps/picotls/deps/micro-ecc/asm_arm.inc", - "deps/picotls/deps/micro-ecc/asm_arm_mult_square.inc", - "deps/picotls/deps/micro-ecc/asm_arm_mult_square_umaal.inc", - "deps/picotls/deps/micro-ecc/asm_avr.inc", - "deps/picotls/deps/micro-ecc/asm_avr_mult_square.inc", - "deps/picotls/deps/micro-ecc/curve-specific.inc", - "deps/picotls/deps/micro-ecc/platform-specific.inc", - ], - visibility = ["//visibility:private"], -) - -cc_library( - name = "h2o", - # The `*.c` files below correspond to the files in `lib/` in - # `LIB_SOURCE_FILES` in `CMakeLists.txt` in the `h2o` repo. - # - # Files in `deps/` in `LIB_SOURCE_FILES` are in their respective library - # targets above. - srcs = [ - "lib/common/cache.c", - "lib/common/file.c", - "lib/common/filecache.c", - "lib/common/hostinfo.c", - "lib/common/http1client.c", - "lib/common/memcached.c", - "lib/common/memory.c", - "lib/common/multithread.c", - "lib/common/serverutil.c", - "lib/common/socket.c", - "lib/common/socketpool.c", - "lib/common/string.c", - "lib/common/time.c", - "lib/common/timeout.c", - "lib/common/url.c", - "lib/core/config.c", - "lib/core/configurator.c", - "lib/core/context.c", - "lib/core/headers.c", - "lib/core/logconf.c", - "lib/core/proxy.c", - "lib/core/request.c", - "lib/core/token.c", - "lib/core/util.c", - "lib/handler/access_log.c", - "lib/handler/chunked.c", - "lib/handler/compress.c", - "lib/handler/compress/gzip.c", - "lib/handler/configurator/access_log.c", - "lib/handler/configurator/compress.c", - "lib/handler/configurator/errordoc.c", - "lib/handler/configurator/expires.c", - "lib/handler/configurator/fastcgi.c", - "lib/handler/configurator/file.c", - "lib/handler/configurator/headers.c", - "lib/handler/configurator/headers_util.c", - "lib/handler/configurator/http2_debug_state.c", - "lib/handler/configurator/proxy.c", - "lib/handler/configurator/redirect.c", - "lib/handler/configurator/reproxy.c", - "lib/handler/configurator/status.c", - "lib/handler/configurator/throttle_resp.c", - "lib/handler/errordoc.c", - "lib/handler/expires.c", - "lib/handler/fastcgi.c", - "lib/handler/file.c", - "lib/handler/headers.c", - "lib/handler/headers_util.c", - "lib/handler/http2_debug_state.c", - "lib/handler/mimemap.c", - "lib/handler/proxy.c", - "lib/handler/redirect.c", - "lib/handler/reproxy.c", - "lib/handler/status.c", - "lib/handler/status/durations.c", - "lib/handler/status/events.c", - "lib/handler/status/requests.c", - "lib/handler/throttle_resp.c", - "lib/http1.c", - "lib/http2/cache_digests.c", - "lib/http2/casper.c", - "lib/http2/connection.c", - "lib/http2/frame.c", - "lib/http2/hpack.c", - "lib/http2/http2_debug_state.c", - "lib/http2/scheduler.c", - "lib/http2/stream.c", - "lib/tunnel.c", - ] + glob( - [ - "lib/core/*.h", - "lib/handler/mimemap/*.h", - "lib/handler/file/*.h", - "lib/http2/*.h", - "lib/common/socket/*.h", - ], - ), - hdrs = ["include/h2o.h"] + glob( - [ - "include/h2o/*.h", - "include/h2o/socket/*.h", - ], - ), - copts = [ - "-std=c99", - "-g3", - "-O2", - "-pthread", - ] + CC_WARNING_FLAGS, - includes = [ - "include", - "include/h2o", - "include/h2o/socket", - ], - local_defines = [ - "H2O_USE_LIBUV", - "H2O_USE_PICOTLS", - ] + select({ - "@platforms//os:linux": ["_GNU_SOURCE"], - "//conditions:default": [], - }), - visibility = ["//visibility:public"], - deps = [ - ":cloexec", - ":golombset", - ":klib", - ":libgkc", - ":libyrmcds", - ":picohttpparser", - ":picotls", - ":ssl_conservatory", - ":yoml", - "@openssl", - "@uv", - "@zlib", - ], -) diff --git a/bazel/third_party/keccak_tiny/64b6647514212b76ae7bca0dea9b7b197d1d8186.patch b/bazel/third_party/keccak_tiny/64b6647514212b76ae7bca0dea9b7b197d1d8186.patch deleted file mode 100644 index 04ba2d2e14..0000000000 --- a/bazel/third_party/keccak_tiny/64b6647514212b76ae7bca0dea9b7b197d1d8186.patch +++ /dev/null @@ -1,168 +0,0 @@ -diff --git a/define-macros.h b/define-macros.h -new file mode 100644 -index 0000000..79b534c ---- /dev/null -+++ b/define-macros.h -@@ -0,0 +1,44 @@ -+#ifndef DEFINEMACROS_H -+#define DEFINEMACROS_H -+ -+/*** Helper macros to define SHA3 and SHAKE instances. ***/ -+#define defshake(bits) \ -+ int shake##bits(uint8_t* out, size_t outlen, \ -+ const uint8_t* in, size_t inlen) { \ -+ return hash(out, outlen, in, inlen, 200 - (bits / 4), 0x1f); \ -+ } -+#define defsha3(bits) \ -+ int sha3_##bits(uint8_t* out, size_t outlen, \ -+ const uint8_t* in, size_t inlen) { \ -+ if (outlen > (bits/8)) { \ -+ return -1; \ -+ } \ -+ return hash(out, outlen, in, inlen, 200 - (bits / 4), 0x06); \ -+ } -+ -+#define defkeccak(bits) \ -+ int keccak_##bits(uint8_t* out, size_t outlen, \ -+ const uint8_t* in, size_t inlen) { \ -+ if (outlen > (bits/8)) { \ -+ return -1; \ -+ } \ -+ return hash(out, outlen, in, inlen, 200 - (bits / 4), 0x01); \ -+ } -+ -+/*** FIPS202 SHAKE VOFs ***/ -+defshake(128) -+defshake(256) -+ -+/*** FIPS202 SHA3 FOFs ***/ -+defsha3(224) -+defsha3(256) -+defsha3(384) -+defsha3(512) -+ -+/*** Non FIP202 SHA3 (KECCAK) FOFs ***/ -+defkeccak(224) -+defkeccak(256) -+defkeccak(384) -+defkeccak(512) -+ -+#endif // DEFINEMACROS_H -diff --git a/keccak-tiny-unrolled.c b/keccak-tiny-unrolled.c -index c238af4..c1eb2a6 100644 ---- a/keccak-tiny-unrolled.c -+++ b/keccak-tiny-unrolled.c -@@ -133,31 +133,11 @@ static inline int hash(uint8_t* out, size_t outlen, - // Squeeze output. - foldP(out, outlen, setout); - setout(a, out, outlen); -- memset_s(a, 200, 0, 200); -+ //TODO: c11 problem: replaced -+ //memset_s(a, 200, 0, 200); -+ //Reference: http://en.cppreference.com/w/c/string/byte/memset -+ memset(a, 0, 200); - return 0; - } - --/*** Helper macros to define SHA3 and SHAKE instances. ***/ --#define defshake(bits) \ -- int shake##bits(uint8_t* out, size_t outlen, \ -- const uint8_t* in, size_t inlen) { \ -- return hash(out, outlen, in, inlen, 200 - (bits / 4), 0x1f); \ -- } --#define defsha3(bits) \ -- int sha3_##bits(uint8_t* out, size_t outlen, \ -- const uint8_t* in, size_t inlen) { \ -- if (outlen > (bits/8)) { \ -- return -1; \ -- } \ -- return hash(out, outlen, in, inlen, 200 - (bits / 4), 0x06); \ -- } -- --/*** FIPS202 SHAKE VOFs ***/ --defshake(128) --defshake(256) -- --/*** FIPS202 SHA3 FOFs ***/ --defsha3(224) --defsha3(256) --defsha3(384) --defsha3(512) -+#include "define-macros.h" -diff --git a/keccak-tiny.c b/keccak-tiny.c -index 76d89fa..fcb3556 100644 ---- a/keccak-tiny.c -+++ b/keccak-tiny.c -@@ -133,31 +133,11 @@ static inline int hash(uint8_t* out, size_t outlen, - // Squeeze output. - foldP(out, outlen, setout); - setout(a, out, outlen); -- memset_s(a, 200, 0, 200); -+ //TODO: c11 problem: replaced -+ //memset_s(a, 200, 0, 200); -+ //Reference: http://en.cppreference.com/w/c/string/byte/memset -+ memset(a, 0, 200); - return 0; - } - --/*** Helper macros to define SHA3 and SHAKE instances. ***/ --#define defshake(bits) \ -- int shake##bits(uint8_t* out, size_t outlen, \ -- const uint8_t* in, size_t inlen) { \ -- return hash(out, outlen, in, inlen, 200 - (bits / 4), 0x1f); \ -- } --#define defsha3(bits) \ -- int sha3_##bits(uint8_t* out, size_t outlen, \ -- const uint8_t* in, size_t inlen) { \ -- if (outlen > (bits/8)) { \ -- return -1; \ -- } \ -- return hash(out, outlen, in, inlen, 200 - (bits / 4), 0x06); \ -- } -- --/*** FIPS202 SHAKE VOFs ***/ --defshake(128) --defshake(256) -- --/*** FIPS202 SHA3 FOFs ***/ --defsha3(224) --defsha3(256) --defsha3(384) --defsha3(512) -+#include "define-macros.h" -diff --git a/keccak-tiny.h b/keccak-tiny.h -index c4c678e..5d643be 100644 ---- a/keccak-tiny.h -+++ b/keccak-tiny.h -@@ -4,16 +4,31 @@ - #include - #include - -+#define HASH224_SIZE 28 -+#define HASH256_SIZE 32 -+#define HASH384_SIZE 48 -+#define HASH512_SIZE 64 -+ - #define decshake(bits) \ - int shake##bits(uint8_t*, size_t, const uint8_t*, size_t); - - #define decsha3(bits) \ - int sha3_##bits(uint8_t*, size_t, const uint8_t*, size_t); - -+#define deckeccak(bits) \ -+ int keccak_##bits(uint8_t*, size_t, const uint8_t*, size_t); -+ - decshake(128) - decshake(256) -+ - decsha3(224) - decsha3(256) - decsha3(384) - decsha3(512) -+ -+deckeccak(224) -+deckeccak(256) -+deckeccak(384) -+deckeccak(512) -+ - #endif diff --git a/bazel/third_party/keccak_tiny/BUILD.bazel b/bazel/third_party/keccak_tiny/BUILD.bazel deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/bazel/third_party/keccak_tiny/keccak_tiny.BUILD b/bazel/third_party/keccak_tiny/keccak_tiny.BUILD deleted file mode 100644 index 4c5e6b9d85..0000000000 --- a/bazel/third_party/keccak_tiny/keccak_tiny.BUILD +++ /dev/null @@ -1,16 +0,0 @@ -cc_library( - name = "keccak_tiny", - srcs = [ - "define-macros.h", - "keccak-tiny.c", - ], - hdrs = ["keccak-tiny.h"], - copts = ["-O3"], - includes = ["."], - local_defines = select({ - # TODO: confirm which platforms have memset_s(). - "@platforms//os:linux": ["memset_s(W,WL,V,OL)=memset(W,V,OL)"], - "//conditions:default": [], - }), - visibility = ["//visibility:public"], -) diff --git a/bazel/third_party/libbacktrace/BUILD.bazel b/bazel/third_party/libbacktrace/BUILD.bazel deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/bazel/third_party/libbacktrace/libbacktrace.BUILD b/bazel/third_party/libbacktrace/libbacktrace.BUILD deleted file mode 100644 index ecb9ed9036..0000000000 --- a/bazel/third_party/libbacktrace/libbacktrace.BUILD +++ /dev/null @@ -1,25 +0,0 @@ -load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make") - -filegroup( - name = "all", - srcs = glob(["**"]), -) - -configure_make( - name = "libbacktrace", - args = [ - ] + select({ - "@platforms//os:macos": ["--jobs=`sysctl -n hw.logicalcpu`"], - "//conditions:default": ["--jobs=`nproc`"], - }), - configure_options = [ - ] + select({ - "@//:linux_aarch64": ["--host=aarch64-linux-musl"], - "@//:linux_x86_64": ["--host=x86_64-linux-musl"], - "//conditions:default": [], - }), - copts = ["-O3"], - lib_source = ":all", - out_static_libs = ["libbacktrace.a"], - visibility = ["//visibility:public"], -) diff --git a/bazel/third_party/libunwind/BUILD.bazel b/bazel/third_party/libunwind/BUILD.bazel deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/bazel/third_party/libunwind/libunwind.BUILD b/bazel/third_party/libunwind/libunwind.BUILD deleted file mode 100644 index 3934b8f8b3..0000000000 --- a/bazel/third_party/libunwind/libunwind.BUILD +++ /dev/null @@ -1,25 +0,0 @@ -load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make") - -filegroup( - name = "all", - srcs = glob(["**"]), -) - -configure_make( - name = "libunwind", - args = select({ - "@platforms//os:macos": ["--jobs=`sysctl -n hw.logicalcpu`"], - "//conditions:default": ["--jobs=`nproc`"], - }), - configure_options = [ - "--enable-debug-frame", - ] + select({ - "@//:linux_aarch64": ["--host=aarch64-linux-musl"], - "@//:linux_x86_64": ["--host=x86_64-linux-musl"], - "//conditions:default": [], - }), - copts = ["-O3 -g"], - lib_source = ":all", - out_static_libs = ["libunwind.a"], - visibility = ["//visibility:public"], -) diff --git a/bazel/third_party/lmdb/0.9.29.patch b/bazel/third_party/lmdb/0.9.29.patch deleted file mode 100644 index cd8416d7cf..0000000000 --- a/bazel/third_party/lmdb/0.9.29.patch +++ /dev/null @@ -1,16 +0,0 @@ -diff --git a/mdb.c b/mdb.c -index 8cecdb2..b8d0648 100644 ---- a/mdb.c -+++ b/mdb.c -@@ -2529,7 +2529,11 @@ mdb_env_sync(MDB_env *env, int force) - rc = ErrCode(); - } else - #endif -+#if defined(URBIT_RUNTIME_OS_DARWIN) -+ if (fcntl(env->me_fd, F_FULLFSYNC, 0)) -+#else - if (MDB_FDATASYNC(env->me_fd)) -+#endif - rc = ErrCode(); - } - } diff --git a/bazel/third_party/lmdb/BUILD.bazel b/bazel/third_party/lmdb/BUILD.bazel deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/bazel/third_party/lmdb/lmdb.BUILD b/bazel/third_party/lmdb/lmdb.BUILD deleted file mode 100644 index 334065c4bb..0000000000 --- a/bazel/third_party/lmdb/lmdb.BUILD +++ /dev/null @@ -1,29 +0,0 @@ -# This build file is derived from `libraries/liblmdb/Makefile` in the `lmdb` -# repo at https://github.com/LMDB/lmdb. - -cc_library( - name = "lmdb", - srcs = [ - "mdb.c", - "midl.c", - "midl.h", - ], - hdrs = ["lmdb.h"], - copts = [ - "-pthread", - "-O2", - "-g", - "-W", - "-Wall", - "-Wno-unused-parameter", - "-Wbad-function-cast", - "-Wuninitialized", - ], - include_prefix = "lmdb", - includes = ["."], - local_defines = select({ - "@platforms//os:macos": ["URBIT_RUNTIME_OS_DARWIN"], - "//conditions:default": [], - }), - visibility = ["//visibility:public"], -) diff --git a/bazel/third_party/murmur3/BUILD.bazel b/bazel/third_party/murmur3/BUILD.bazel deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/bazel/third_party/murmur3/murmur3.BUILD b/bazel/third_party/murmur3/murmur3.BUILD deleted file mode 100644 index 106e7dc878..0000000000 --- a/bazel/third_party/murmur3/murmur3.BUILD +++ /dev/null @@ -1,21 +0,0 @@ -# This build file is derived from `makefile` in the `murmur3` repo at -# https://github.com/PeterScott/murmur3. - -cc_library( - name = "murmur3", - srcs = ["murmur3.c"], - hdrs = ["murmur3.h"], - copts = [ - "-O3", - "-Wall", - ] + select({ - # TODO: use selects.with_or() from skylib once it's available. - "@platforms//os:macos": [ - "-fPIC", - "-c", - ], - "//conditions:default": [], - }), - includes = ["."], - visibility = ["//visibility:public"], -) diff --git a/bazel/third_party/natpmp/BUILD.bazel b/bazel/third_party/natpmp/BUILD.bazel deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/bazel/third_party/natpmp/natpmp.BUILD b/bazel/third_party/natpmp/natpmp.BUILD deleted file mode 100644 index b3a7edd7e0..0000000000 --- a/bazel/third_party/natpmp/natpmp.BUILD +++ /dev/null @@ -1,8 +0,0 @@ -cc_library( - name = "natpmp", - srcs = ["natpmp.c", "getgateway.c"], - hdrs = ["natpmp.h", "getgateway.h", "natpmp_declspec.h"], - copts = ["-O3"], - linkstatic = True, - visibility = ["//visibility:public"], -) diff --git a/bazel/third_party/openssl/BUILD.bazel b/bazel/third_party/openssl/BUILD.bazel deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/bazel/third_party/openssl/openssl.BUILD b/bazel/third_party/openssl/openssl.BUILD deleted file mode 100644 index bb521c4a98..0000000000 --- a/bazel/third_party/openssl/openssl.BUILD +++ /dev/null @@ -1,43 +0,0 @@ -load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make") - -filegroup( - name = "all", - srcs = glob(["**"]), -) - -configure_make( - name = "openssl", - args = select({ - "@platforms//os:macos": ["--jobs=`sysctl -n hw.logicalcpu`"], - "//conditions:default": ["--jobs=`nproc`"], - }), - configure_command = select({ - "@//:linux_aarch64": "Configure", - "//conditions:default": "config", - }), - configure_options = [ - "no-shared", - ] + select({ - "@//:linux_aarch64": [ - "linux-aarch64", - # Native compilation on linux-aarch64 isn't supported. The prefix is - # empty because the configure script detects an absolute path to the - # aarch64-linux-gnu-gcc instead of just the binary name. This is - # presumably because of the Bazel toolchain configuration but is not - # an issue. - "--cross-compile-prefix=", - ], - "//conditions:default": [], - }), - copts = ["-O3"], - lib_source = ":all", - out_static_libs = [ - "libssl.a", - "libcrypto.a", - ], - targets = [ - "build_libs", - "install_dev", - ], - visibility = ["//visibility:public"], -) diff --git a/bazel/third_party/pdjson/BUILD.bazel b/bazel/third_party/pdjson/BUILD.bazel deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/bazel/third_party/pdjson/pdjson.BUILD b/bazel/third_party/pdjson/pdjson.BUILD deleted file mode 100644 index 3a6d82d96a..0000000000 --- a/bazel/third_party/pdjson/pdjson.BUILD +++ /dev/null @@ -1,16 +0,0 @@ -# This build file is derived from `Makefile` in the `pdjson` repo at -# https://github.com/skeeto/pdjson. - -cc_library( - name = "pdjson", - srcs = ["pdjson.c"], - hdrs = ["pdjson.h"], - copts = [ - "-std=c99", - "-pedantic", - "-Wall", - "-Wextra", - "-Wno-missing-field-initializers" - ], - visibility = ["//visibility:public"] -) diff --git a/bazel/third_party/scrypt/BUILD.bazel b/bazel/third_party/scrypt/BUILD.bazel deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/bazel/third_party/scrypt/scrypt.BUILD b/bazel/third_party/scrypt/scrypt.BUILD deleted file mode 100644 index 607987d35f..0000000000 --- a/bazel/third_party/scrypt/scrypt.BUILD +++ /dev/null @@ -1,30 +0,0 @@ -# This build file is derived from `Makefile` in the `scrypt` repo at -# https://github.com/technion/libscrypt. - -cc_library( - name = "scrypt", - srcs = [ - "b64.c", - "b64.h", - "crypto-mcf.c", - "crypto-scrypt-saltgen.c", - "crypto_scrypt-check.c", - "crypto_scrypt-hash.c", - "crypto_scrypt-nosse.c", - "sha256.c", - "sha256.h", - "slowequals.c", - "slowequals.h", - "sysendian.h", - ], - hdrs = ["libscrypt.h"], - copts = [ - "-O2", - "-Wall", - "-g", - "-fstack-protector", - ], - includes = ["."], - local_defines = ["_FORTIFY_SOURCE=2"], - visibility = ["//visibility:public"], -) diff --git a/bazel/third_party/secp256k1/694ce8fb2d1fd8a3d641d7c33705691d41a2a860.patch b/bazel/third_party/secp256k1/694ce8fb2d1fd8a3d641d7c33705691d41a2a860.patch deleted file mode 100644 index f7f7b7c077..0000000000 --- a/bazel/third_party/secp256k1/694ce8fb2d1fd8a3d641d7c33705691d41a2a860.patch +++ /dev/null @@ -1,14 +0,0 @@ -diff --git i/autogen.sh w/autogen.sh -index 65286b9..edcbeb9 100755 ---- i/autogen.sh -+++ w/autogen.sh -@@ -1,3 +1,9 @@ - #!/bin/sh - set -e -+if [ -r /etc/os-release ]; then -+ . /etc/os-release -+ if [ "$NAME" = NixOS ]; then -+ libtoolize --copy --force -+ fi -+fi - autoreconf -if --warnings=all diff --git a/bazel/third_party/secp256k1/BUILD.bazel b/bazel/third_party/secp256k1/BUILD.bazel deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/bazel/third_party/secp256k1/secp256k1.BUILD b/bazel/third_party/secp256k1/secp256k1.BUILD deleted file mode 100644 index fb7b939b23..0000000000 --- a/bazel/third_party/secp256k1/secp256k1.BUILD +++ /dev/null @@ -1,30 +0,0 @@ -load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make") - -filegroup( - name = "all", - srcs = glob(["**"]), -) - -configure_make( - name = "secp256k1", - args = select({ - "@platforms//os:macos": ["--jobs=`sysctl -n hw.logicalcpu`"], - "//conditions:default": ["--jobs=`nproc`"], - }), - autogen = True, - configure_in_place = True, - configure_options = [ - "--disable-shared", - "--enable-module-recovery", - "--enable-module-schnorrsig", - "--enable-static", - ] + select({ - "@//:linux_aarch64": ["--host=aarch64-linux-musl"], - "@//:linux_x86_64": ["--host=x86_64-linux-musl"], - "//conditions:default": [], - }), - copts = ["-O3"], - lib_source = ":all", - out_static_libs = ["libsecp256k1.a"], - visibility = ["//visibility:public"], -) diff --git a/bazel/third_party/sigsegv/BUILD.bazel b/bazel/third_party/sigsegv/BUILD.bazel deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/bazel/third_party/sigsegv/sigsegv.BUILD b/bazel/third_party/sigsegv/sigsegv.BUILD deleted file mode 100644 index 5b54736c66..0000000000 --- a/bazel/third_party/sigsegv/sigsegv.BUILD +++ /dev/null @@ -1,31 +0,0 @@ -load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make") - -filegroup( - name = "all", - srcs = glob(["**"]), -) - -configure_make( - name = "sigsegv", - args = select({ - "@platforms//os:macos": ["--jobs=`sysctl -n hw.logicalcpu`"], - "//conditions:default": ["--jobs=`nproc`"], - }), - configure_options = [ - "--disable-shared", - "--enable-static", - ] + select({ - # Disable stack vma check, which reads from procfs, producing drastic - # slowdowns. - "@platforms//os:linux": ["--disable-stackvma"], - "//conditions:default": [], - }) + select({ - "@//:linux_aarch64": ["--host=aarch64-linux-musl"], - "@//:linux_x86_64": ["--host=x86_64-linux-musl"], - "//conditions:default": [], - }), - copts = ["-O3"], - lib_source = ":all", - out_static_libs = ["libsigsegv.a"], - visibility = ["//visibility:public"], -) diff --git a/bazel/third_party/softfloat/5c06db33fc1e2130f67c045327b0ec949032df1d.patch b/bazel/third_party/softfloat/5c06db33fc1e2130f67c045327b0ec949032df1d.patch deleted file mode 100644 index b05221ff82..0000000000 --- a/bazel/third_party/softfloat/5c06db33fc1e2130f67c045327b0ec949032df1d.patch +++ /dev/null @@ -1,27 +0,0 @@ -diff --git a/build/template-FAST_INT64/platform.h b/build/template-FAST_INT64/platform.h -index 2094658..f8147a0 100644 ---- a/build/template-FAST_INT64/platform.h -+++ b/build/template-FAST_INT64/platform.h -@@ -38,13 +38,19 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - /*---------------------------------------------------------------------------- - *----------------------------------------------------------------------------*/ --==> #define LITTLEENDIAN 1 -+#if defined(URBIT_RUNTIME_OS_DARWIN) || defined(URBIT_RUNTIME_OS_BSD) -+#define LITTLEENDIAN 1 -+#endif - - /*---------------------------------------------------------------------------- - *----------------------------------------------------------------------------*/ --==> #define INLINE inline -+#if defined(URBIT_RUNTIME_OS_DARWIN) || defined(URBIT_RUNTIME_OS_BSD) -+#define INLINE inline -+#endif - - /*---------------------------------------------------------------------------- - *----------------------------------------------------------------------------*/ --==> #define THREAD_LOCAL _Thread_local -+#if defined(URBIT_RUNTIME_OS_DARWIN) || defined(URBIT_RUNTIME_OS_BSD) -+#define THREAD_LOCAL _Thread_local -+#endif - diff --git a/bazel/third_party/softfloat/BUILD.bazel b/bazel/third_party/softfloat/BUILD.bazel deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/bazel/third_party/softfloat/softfloat.BUILD b/bazel/third_party/softfloat/softfloat.BUILD deleted file mode 100644 index 0d57ba5d7f..0000000000 --- a/bazel/third_party/softfloat/softfloat.BUILD +++ /dev/null @@ -1,637 +0,0 @@ -# This build file is derived from the makefiles in `build/` in the `softfloat` -# repo at https://github.com/ucb-bar/berkeley-softfloat-3. -# -# The following link, which is referenced in `README.md` in the `softfloat` -# repo, was useful in deriving this build file: -# http://www.jhauser.us/arithmetic/SoftFloat-3/doc/SoftFloat-source.html - -cc_library( - name = "softfloat", - visibility = ["//visibility:public"], - deps = select({ - "@platforms//cpu:aarch64": [":softfloat_aarch64"], - "@platforms//cpu:x86_64": [":softfloat_x86_64"], - "//conditions:default": [], - }), -) - -cc_library( - name = "softfloat_aarch64", - srcs = [ - # See `OBJS_PRIMITIVES` in `build/Linux-ARM-VFPv2-GCC/Makefile` in the - # `softfloat` repo. - "source/s_compare96M.c", - "source/s_compare128M.c", - "source/s_shortShiftLeft64To96M.c", - "source/s_shortShiftLeftM.c", - "source/s_shiftLeftM.c", - "source/s_shortShiftRightM.c", - "source/s_shortShiftRightJam64.c", - "source/s_shortShiftRightJamM.c", - "source/s_shiftRightJam32.c", - "source/s_shiftRightJam64.c", - "source/s_shiftRightJamM.c", - "source/s_shiftRightM.c", - "source/s_countLeadingZeros8.c", - "source/s_countLeadingZeros16.c", - "source/s_countLeadingZeros32.c", - "source/s_countLeadingZeros64.c", - "source/s_addM.c", - "source/s_addCarryM.c", - "source/s_addComplCarryM.c", - "source/s_negXM.c", - "source/s_sub1XM.c", - "source/s_subM.c", - "source/s_mul64To128M.c", - "source/s_mul128MTo256M.c", - "source/s_approxRecip_1Ks.c", - "source/s_approxRecip32_1.c", - "source/s_approxRecipSqrt_1Ks.c", - "source/s_approxRecipSqrt32_1.c", - "source/s_remStepMBy32.c", - # See `OBJS_SPECIALIZE` in `build/Linux-ARM-VFPv2-GCC/Makefile` in the - # `softfloat` repo. - "source/ARM-VFPv2/softfloat_raiseFlags.c", - "source/ARM-VFPv2/s_f16UIToCommonNaN.c", - "source/ARM-VFPv2/s_commonNaNToF16UI.c", - "source/ARM-VFPv2/s_propagateNaNF16UI.c", - "source/ARM-VFPv2/s_f32UIToCommonNaN.c", - "source/ARM-VFPv2/s_commonNaNToF32UI.c", - "source/ARM-VFPv2/s_propagateNaNF32UI.c", - "source/ARM-VFPv2/s_f64UIToCommonNaN.c", - "source/ARM-VFPv2/s_commonNaNToF64UI.c", - "source/ARM-VFPv2/s_propagateNaNF64UI.c", - "source/ARM-VFPv2/extF80M_isSignalingNaN.c", - "source/ARM-VFPv2/s_extF80MToCommonNaN.c", - "source/ARM-VFPv2/s_commonNaNToExtF80M.c", - "source/ARM-VFPv2/s_propagateNaNExtF80M.c", - "source/ARM-VFPv2/f128M_isSignalingNaN.c", - "source/ARM-VFPv2/s_f128MToCommonNaN.c", - "source/ARM-VFPv2/s_commonNaNToF128M.c", - "source/ARM-VFPv2/s_propagateNaNF128M.c", - # See `OBJS_OTHERS` in `build/Linux-ARM-VFPv2-GCC/Makefile` in the - # `softfloat` repo. - "source/s_roundToUI32.c", - "source/s_roundMToUI64.c", - "source/s_roundToI32.c", - "source/s_roundMToI64.c", - "source/s_normSubnormalF16Sig.c", - "source/s_roundPackToF16.c", - "source/s_normRoundPackToF16.c", - "source/s_addMagsF16.c", - "source/s_subMagsF16.c", - "source/s_mulAddF16.c", - "source/s_normSubnormalF32Sig.c", - "source/s_roundPackToF32.c", - "source/s_normRoundPackToF32.c", - "source/s_addMagsF32.c", - "source/s_subMagsF32.c", - "source/s_mulAddF32.c", - "source/s_normSubnormalF64Sig.c", - "source/s_roundPackToF64.c", - "source/s_normRoundPackToF64.c", - "source/s_addMagsF64.c", - "source/s_subMagsF64.c", - "source/s_mulAddF64.c", - "source/s_tryPropagateNaNExtF80M.c", - "source/s_invalidExtF80M.c", - "source/s_normExtF80SigM.c", - "source/s_roundPackMToExtF80M.c", - "source/s_normRoundPackMToExtF80M.c", - "source/s_addExtF80M.c", - "source/s_compareNonnormExtF80M.c", - "source/s_isNaNF128M.c", - "source/s_tryPropagateNaNF128M.c", - "source/s_invalidF128M.c", - "source/s_shiftNormSigF128M.c", - "source/s_roundPackMToF128M.c", - "source/s_normRoundPackMToF128M.c", - "source/s_addF128M.c", - "source/s_mulAddF128M.c", - "source/softfloat_state.c", - "source/ui32_to_f16.c", - "source/ui32_to_f32.c", - "source/ui32_to_f64.c", - "source/ui32_to_extF80M.c", - "source/ui32_to_f128M.c", - "source/ui64_to_f16.c", - "source/ui64_to_f32.c", - "source/ui64_to_f64.c", - "source/ui64_to_extF80M.c", - "source/ui64_to_f128M.c", - "source/i32_to_f16.c", - "source/i32_to_f32.c", - "source/i32_to_f64.c", - "source/i32_to_extF80M.c", - "source/i32_to_f128M.c", - "source/i64_to_f16.c", - "source/i64_to_f32.c", - "source/i64_to_f64.c", - "source/i64_to_extF80M.c", - "source/i64_to_f128M.c", - "source/f16_to_ui32.c", - "source/f16_to_ui64.c", - "source/f16_to_i32.c", - "source/f16_to_i64.c", - "source/f16_to_ui32_r_minMag.c", - "source/f16_to_ui64_r_minMag.c", - "source/f16_to_i32_r_minMag.c", - "source/f16_to_i64_r_minMag.c", - "source/f16_to_f32.c", - "source/f16_to_f64.c", - "source/f16_to_extF80M.c", - "source/f16_to_f128M.c", - "source/f16_roundToInt.c", - "source/f16_add.c", - "source/f16_sub.c", - "source/f16_mul.c", - "source/f16_mulAdd.c", - "source/f16_div.c", - "source/f16_rem.c", - "source/f16_sqrt.c", - "source/f16_eq.c", - "source/f16_le.c", - "source/f16_lt.c", - "source/f16_eq_signaling.c", - "source/f16_le_quiet.c", - "source/f16_lt_quiet.c", - "source/f16_isSignalingNaN.c", - "source/f32_to_ui32.c", - "source/f32_to_ui64.c", - "source/f32_to_i32.c", - "source/f32_to_i64.c", - "source/f32_to_ui32_r_minMag.c", - "source/f32_to_ui64_r_minMag.c", - "source/f32_to_i32_r_minMag.c", - "source/f32_to_i64_r_minMag.c", - "source/f32_to_f16.c", - "source/f32_to_f64.c", - "source/f32_to_extF80M.c", - "source/f32_to_f128M.c", - "source/f32_roundToInt.c", - "source/f32_add.c", - "source/f32_sub.c", - "source/f32_mul.c", - "source/f32_mulAdd.c", - "source/f32_div.c", - "source/f32_rem.c", - "source/f32_sqrt.c", - "source/f32_eq.c", - "source/f32_le.c", - "source/f32_lt.c", - "source/f32_eq_signaling.c", - "source/f32_le_quiet.c", - "source/f32_lt_quiet.c", - "source/f32_isSignalingNaN.c", - "source/f64_to_ui32.c", - "source/f64_to_ui64.c", - "source/f64_to_i32.c", - "source/f64_to_i64.c", - "source/f64_to_ui32_r_minMag.c", - "source/f64_to_ui64_r_minMag.c", - "source/f64_to_i32_r_minMag.c", - "source/f64_to_i64_r_minMag.c", - "source/f64_to_f16.c", - "source/f64_to_f32.c", - "source/f64_to_extF80M.c", - "source/f64_to_f128M.c", - "source/f64_roundToInt.c", - "source/f64_add.c", - "source/f64_sub.c", - "source/f64_mul.c", - "source/f64_mulAdd.c", - "source/f64_div.c", - "source/f64_rem.c", - "source/f64_sqrt.c", - "source/f64_eq.c", - "source/f64_le.c", - "source/f64_lt.c", - "source/f64_eq_signaling.c", - "source/f64_le_quiet.c", - "source/f64_lt_quiet.c", - "source/f64_isSignalingNaN.c", - "source/extF80M_to_ui32.c", - "source/extF80M_to_ui64.c", - "source/extF80M_to_i32.c", - "source/extF80M_to_i64.c", - "source/extF80M_to_ui32_r_minMag.c", - "source/extF80M_to_ui64_r_minMag.c", - "source/extF80M_to_i32_r_minMag.c", - "source/extF80M_to_i64_r_minMag.c", - "source/extF80M_to_f16.c", - "source/extF80M_to_f32.c", - "source/extF80M_to_f64.c", - "source/extF80M_to_f128M.c", - "source/extF80M_roundToInt.c", - "source/extF80M_add.c", - "source/extF80M_sub.c", - "source/extF80M_mul.c", - "source/extF80M_div.c", - "source/extF80M_rem.c", - "source/extF80M_sqrt.c", - "source/extF80M_eq.c", - "source/extF80M_le.c", - "source/extF80M_lt.c", - "source/extF80M_eq_signaling.c", - "source/extF80M_le_quiet.c", - "source/extF80M_lt_quiet.c", - "source/f128M_to_ui32.c", - "source/f128M_to_ui64.c", - "source/f128M_to_i32.c", - "source/f128M_to_i64.c", - "source/f128M_to_ui32_r_minMag.c", - "source/f128M_to_ui64_r_minMag.c", - "source/f128M_to_i32_r_minMag.c", - "source/f128M_to_i64_r_minMag.c", - "source/f128M_to_f16.c", - "source/f128M_to_f32.c", - "source/f128M_to_f64.c", - "source/f128M_to_extF80M.c", - "source/f128M_roundToInt.c", - "source/f128M_add.c", - "source/f128M_sub.c", - "source/f128M_mul.c", - "source/f128M_mulAdd.c", - "source/f128M_div.c", - "source/f128M_rem.c", - "source/f128M_sqrt.c", - "source/f128M_eq.c", - "source/f128M_le.c", - "source/f128M_lt.c", - "source/f128M_eq_signaling.c", - "source/f128M_le_quiet.c", - "source/f128M_lt_quiet.c", - # See `$(OBJS_ALL)` target in `build/Linux-ARM-VFPv2-GCC/Makefile` in the - # `softfloat` repo. - "build/Linux-ARM-VFPv2-GCC/platform.h", - "source/include/primitiveTypes.h", - "source/include/primitives.h", - # See `$(OBJS_SPECIALIZE) $(OBJS_OTHERS)` target in - # `build/Linux-ARM-VFPv2-GCC/Makefile` in the `softfloat` repo. - "source/include/softfloat_types.h", - "source/include/internals.h", - "source/ARM-VFPv2/specialize.h", - # `platform.h` includes `opts-GCC.h`. - "source/include/opts-GCC.h", - ], - hdrs = ["source/include/softfloat.h"], - copts = [ - "-Iexternal/softfloat/build/Linux-ARM-VFPv2-GCC", - "-Iexternal/softfloat/source/ARM-VFPv2", - "-Werror-implicit-function-declaration", - "-O2", - ], - includes = ["source/include"], - local_defines = [ - "SOFTFLOAT_ROUND_ODD", - "INLINE_LEVEL=5", - ], - visibility = ["//visibility:private"], -) - -cc_library( - name = "softfloat_x86_64", - srcs = [ - # See `OBJS_PRIMITIVES` in `build/Linux-x86_64-GCC/Makefile` in the - # `softfloat` repo. - "source/s_eq128.c", - "source/s_le128.c", - "source/s_lt128.c", - "source/s_shortShiftLeft128.c", - "source/s_shortShiftRight128.c", - "source/s_shortShiftRightJam64.c", - "source/s_shortShiftRightJam64Extra.c", - "source/s_shortShiftRightJam128.c", - "source/s_shortShiftRightJam128Extra.c", - "source/s_shiftRightJam32.c", - "source/s_shiftRightJam64.c", - "source/s_shiftRightJam64Extra.c", - "source/s_shiftRightJam128.c", - "source/s_shiftRightJam128Extra.c", - "source/s_shiftRightJam256M.c", - "source/s_countLeadingZeros8.c", - "source/s_countLeadingZeros16.c", - "source/s_countLeadingZeros32.c", - "source/s_countLeadingZeros64.c", - "source/s_add128.c", - "source/s_add256M.c", - "source/s_sub128.c", - "source/s_sub256M.c", - "source/s_mul64ByShifted32To128.c", - "source/s_mul64To128.c", - "source/s_mul128By32.c", - "source/s_mul128To256M.c", - "source/s_approxRecip_1Ks.c", - "source/s_approxRecip32_1.c", - "source/s_approxRecipSqrt_1Ks.c", - "source/s_approxRecipSqrt32_1.c", - # See `OBJS_SPECIALIZE` in `build/Linux-x86_64-GCC/Makefile` in the - # `softfloat` repo. - "source/8086-SSE/softfloat_raiseFlags.c", - "source/8086-SSE/s_f16UIToCommonNaN.c", - "source/8086-SSE/s_commonNaNToF16UI.c", - "source/8086-SSE/s_propagateNaNF16UI.c", - "source/8086-SSE/s_f32UIToCommonNaN.c", - "source/8086-SSE/s_commonNaNToF32UI.c", - "source/8086-SSE/s_propagateNaNF32UI.c", - "source/8086-SSE/s_f64UIToCommonNaN.c", - "source/8086-SSE/s_commonNaNToF64UI.c", - "source/8086-SSE/s_propagateNaNF64UI.c", - "source/8086-SSE/extF80M_isSignalingNaN.c", - "source/8086-SSE/s_extF80UIToCommonNaN.c", - "source/8086-SSE/s_commonNaNToExtF80UI.c", - "source/8086-SSE/s_propagateNaNExtF80UI.c", - "source/8086-SSE/f128M_isSignalingNaN.c", - "source/8086-SSE/s_f128UIToCommonNaN.c", - "source/8086-SSE/s_commonNaNToF128UI.c", - "source/8086-SSE/s_propagateNaNF128UI.c", - # See `OBJS_OTHERS` in `build/Linux-x86_64-GCC/Makefile` in the - # `softfloat` repo. - "source/s_roundToUI32.c", - "source/s_roundToUI64.c", - "source/s_roundToI32.c", - "source/s_roundToI64.c", - "source/s_normSubnormalF16Sig.c", - "source/s_roundPackToF16.c", - "source/s_normRoundPackToF16.c", - "source/s_addMagsF16.c", - "source/s_subMagsF16.c", - "source/s_mulAddF16.c", - "source/s_normSubnormalF32Sig.c", - "source/s_roundPackToF32.c", - "source/s_normRoundPackToF32.c", - "source/s_addMagsF32.c", - "source/s_subMagsF32.c", - "source/s_mulAddF32.c", - "source/s_normSubnormalF64Sig.c", - "source/s_roundPackToF64.c", - "source/s_normRoundPackToF64.c", - "source/s_addMagsF64.c", - "source/s_subMagsF64.c", - "source/s_mulAddF64.c", - "source/s_normSubnormalExtF80Sig.c", - "source/s_roundPackToExtF80.c", - "source/s_normRoundPackToExtF80.c", - "source/s_addMagsExtF80.c", - "source/s_subMagsExtF80.c", - "source/s_normSubnormalF128Sig.c", - "source/s_roundPackToF128.c", - "source/s_normRoundPackToF128.c", - "source/s_addMagsF128.c", - "source/s_subMagsF128.c", - "source/s_mulAddF128.c", - "source/softfloat_state.c", - "source/ui32_to_f16.c", - "source/ui32_to_f32.c", - "source/ui32_to_f64.c", - "source/ui32_to_extF80.c", - "source/ui32_to_extF80M.c", - "source/ui32_to_f128.c", - "source/ui32_to_f128M.c", - "source/ui64_to_f16.c", - "source/ui64_to_f32.c", - "source/ui64_to_f64.c", - "source/ui64_to_extF80.c", - "source/ui64_to_extF80M.c", - "source/ui64_to_f128.c", - "source/ui64_to_f128M.c", - "source/i32_to_f16.c", - "source/i32_to_f32.c", - "source/i32_to_f64.c", - "source/i32_to_extF80.c", - "source/i32_to_extF80M.c", - "source/i32_to_f128.c", - "source/i32_to_f128M.c", - "source/i64_to_f16.c", - "source/i64_to_f32.c", - "source/i64_to_f64.c", - "source/i64_to_extF80.c", - "source/i64_to_extF80M.c", - "source/i64_to_f128.c", - "source/i64_to_f128M.c", - "source/f16_to_ui32.c", - "source/f16_to_ui64.c", - "source/f16_to_i32.c", - "source/f16_to_i64.c", - "source/f16_to_ui32_r_minMag.c", - "source/f16_to_ui64_r_minMag.c", - "source/f16_to_i32_r_minMag.c", - "source/f16_to_i64_r_minMag.c", - "source/f16_to_f32.c", - "source/f16_to_f64.c", - "source/f16_to_extF80.c", - "source/f16_to_extF80M.c", - "source/f16_to_f128.c", - "source/f16_to_f128M.c", - "source/f16_roundToInt.c", - "source/f16_add.c", - "source/f16_sub.c", - "source/f16_mul.c", - "source/f16_mulAdd.c", - "source/f16_div.c", - "source/f16_rem.c", - "source/f16_sqrt.c", - "source/f16_eq.c", - "source/f16_le.c", - "source/f16_lt.c", - "source/f16_eq_signaling.c", - "source/f16_le_quiet.c", - "source/f16_lt_quiet.c", - "source/f16_isSignalingNaN.c", - "source/f32_to_ui32.c", - "source/f32_to_ui64.c", - "source/f32_to_i32.c", - "source/f32_to_i64.c", - "source/f32_to_ui32_r_minMag.c", - "source/f32_to_ui64_r_minMag.c", - "source/f32_to_i32_r_minMag.c", - "source/f32_to_i64_r_minMag.c", - "source/f32_to_f16.c", - "source/f32_to_f64.c", - "source/f32_to_extF80.c", - "source/f32_to_extF80M.c", - "source/f32_to_f128.c", - "source/f32_to_f128M.c", - "source/f32_roundToInt.c", - "source/f32_add.c", - "source/f32_sub.c", - "source/f32_mul.c", - "source/f32_mulAdd.c", - "source/f32_div.c", - "source/f32_rem.c", - "source/f32_sqrt.c", - "source/f32_eq.c", - "source/f32_le.c", - "source/f32_lt.c", - "source/f32_eq_signaling.c", - "source/f32_le_quiet.c", - "source/f32_lt_quiet.c", - "source/f32_isSignalingNaN.c", - "source/f64_to_ui32.c", - "source/f64_to_ui64.c", - "source/f64_to_i32.c", - "source/f64_to_i64.c", - "source/f64_to_ui32_r_minMag.c", - "source/f64_to_ui64_r_minMag.c", - "source/f64_to_i32_r_minMag.c", - "source/f64_to_i64_r_minMag.c", - "source/f64_to_f16.c", - "source/f64_to_f32.c", - "source/f64_to_extF80.c", - "source/f64_to_extF80M.c", - "source/f64_to_f128.c", - "source/f64_to_f128M.c", - "source/f64_roundToInt.c", - "source/f64_add.c", - "source/f64_sub.c", - "source/f64_mul.c", - "source/f64_mulAdd.c", - "source/f64_div.c", - "source/f64_rem.c", - "source/f64_sqrt.c", - "source/f64_eq.c", - "source/f64_le.c", - "source/f64_lt.c", - "source/f64_eq_signaling.c", - "source/f64_le_quiet.c", - "source/f64_lt_quiet.c", - "source/f64_isSignalingNaN.c", - "source/extF80_to_ui32.c", - "source/extF80_to_ui64.c", - "source/extF80_to_i32.c", - "source/extF80_to_i64.c", - "source/extF80_to_ui32_r_minMag.c", - "source/extF80_to_ui64_r_minMag.c", - "source/extF80_to_i32_r_minMag.c", - "source/extF80_to_i64_r_minMag.c", - "source/extF80_to_f16.c", - "source/extF80_to_f32.c", - "source/extF80_to_f64.c", - "source/extF80_to_f128.c", - "source/extF80_roundToInt.c", - "source/extF80_add.c", - "source/extF80_sub.c", - "source/extF80_mul.c", - "source/extF80_div.c", - "source/extF80_rem.c", - "source/extF80_sqrt.c", - "source/extF80_eq.c", - "source/extF80_le.c", - "source/extF80_lt.c", - "source/extF80_eq_signaling.c", - "source/extF80_le_quiet.c", - "source/extF80_lt_quiet.c", - "source/extF80_isSignalingNaN.c", - "source/extF80M_to_ui32.c", - "source/extF80M_to_ui64.c", - "source/extF80M_to_i32.c", - "source/extF80M_to_i64.c", - "source/extF80M_to_ui32_r_minMag.c", - "source/extF80M_to_ui64_r_minMag.c", - "source/extF80M_to_i32_r_minMag.c", - "source/extF80M_to_i64_r_minMag.c", - "source/extF80M_to_f16.c", - "source/extF80M_to_f32.c", - "source/extF80M_to_f64.c", - "source/extF80M_to_f128M.c", - "source/extF80M_roundToInt.c", - "source/extF80M_add.c", - "source/extF80M_sub.c", - "source/extF80M_mul.c", - "source/extF80M_div.c", - "source/extF80M_rem.c", - "source/extF80M_sqrt.c", - "source/extF80M_eq.c", - "source/extF80M_le.c", - "source/extF80M_lt.c", - "source/extF80M_eq_signaling.c", - "source/extF80M_le_quiet.c", - "source/extF80M_lt_quiet.c", - "source/f128_to_ui32.c", - "source/f128_to_ui64.c", - "source/f128_to_i32.c", - "source/f128_to_i64.c", - "source/f128_to_ui32_r_minMag.c", - "source/f128_to_ui64_r_minMag.c", - "source/f128_to_i32_r_minMag.c", - "source/f128_to_i64_r_minMag.c", - "source/f128_to_f16.c", - "source/f128_to_f32.c", - "source/f128_to_extF80.c", - "source/f128_to_f64.c", - "source/f128_roundToInt.c", - "source/f128_add.c", - "source/f128_sub.c", - "source/f128_mul.c", - "source/f128_mulAdd.c", - "source/f128_div.c", - "source/f128_rem.c", - "source/f128_sqrt.c", - "source/f128_eq.c", - "source/f128_le.c", - "source/f128_lt.c", - "source/f128_eq_signaling.c", - "source/f128_le_quiet.c", - "source/f128_lt_quiet.c", - "source/f128_isSignalingNaN.c", - "source/f128M_to_ui32.c", - "source/f128M_to_ui64.c", - "source/f128M_to_i32.c", - "source/f128M_to_i64.c", - "source/f128M_to_ui32_r_minMag.c", - "source/f128M_to_ui64_r_minMag.c", - "source/f128M_to_i32_r_minMag.c", - "source/f128M_to_i64_r_minMag.c", - "source/f128M_to_f16.c", - "source/f128M_to_f32.c", - "source/f128M_to_extF80M.c", - "source/f128M_to_f64.c", - "source/f128M_roundToInt.c", - "source/f128M_add.c", - "source/f128M_sub.c", - "source/f128M_mul.c", - "source/f128M_mulAdd.c", - "source/f128M_div.c", - "source/f128M_rem.c", - "source/f128M_sqrt.c", - "source/f128M_eq.c", - "source/f128M_le.c", - "source/f128M_lt.c", - "source/f128M_eq_signaling.c", - "source/f128M_le_quiet.c", - "source/f128M_lt_quiet.c", - # See `$(OTHER_HEADERS)` in `build/Linux-x86_64-GCC/Makefile` in the - # `softfloat` repo. - "source/include/opts-GCC.h", - # See `$(OBJS_ALL)` target in `build/Linux-x86_64-GCC/Makefile` in the - # `softfloat` repo. - "build/Linux-x86_64-GCC/platform.h", - "source/include/primitiveTypes.h", - "source/include/primitives.h", - # See `$(OBJS_SPECIALIZE) $(OBJS_OTHERS)` target in - # `build/Linux-x86_64-GCC/Makefile` in the `softfloat` repo. - "source/include/softfloat_types.h", - "source/include/internals.h", - "source/8086-SSE/specialize.h", - ], - hdrs = [ - "source/include/softfloat.h", - ], - copts = [ - "-Iexternal/softfloat/build/Linux-x86_64-GCC", - "-Iexternal/softfloat/source/8086-SSE", - "-Werror-implicit-function-declaration", - "-O2", - ], - # `SOFTFLOAT_FAST_INT64` is used in `softfloat.h` and therefore needs to be - # passed to dependencies. - defines = ["SOFTFLOAT_FAST_INT64"], - includes = ["source/include"], - local_defines = [ - "SOFTFLOAT_ROUND_ODD", - "INLINE_LEVEL=5", - "SOFTFLOAT_FAST_DIV32TO16", - "SOFTFLOAT_FAST_DIV64TO32", - ], - visibility = ["//visibility:private"], -) diff --git a/bazel/third_party/sse2neon/BUILD.bazel b/bazel/third_party/sse2neon/BUILD.bazel deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/bazel/third_party/sse2neon/sse2neon.BUILD b/bazel/third_party/sse2neon/sse2neon.BUILD deleted file mode 100644 index 95aee5072d..0000000000 --- a/bazel/third_party/sse2neon/sse2neon.BUILD +++ /dev/null @@ -1,7 +0,0 @@ -cc_library( - name = "sse2neon", - hdrs = ["sse2neon.h"], - copts = ["-O3"], - linkstatic = True, - visibility = ["//visibility:public"], -) diff --git a/bazel/third_party/urcrypt/BUILD.bazel b/bazel/third_party/urcrypt/BUILD.bazel deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/bazel/third_party/urcrypt/urcrypt.BUILD b/bazel/third_party/urcrypt/urcrypt.BUILD deleted file mode 100644 index c41d78f5b0..0000000000 --- a/bazel/third_party/urcrypt/urcrypt.BUILD +++ /dev/null @@ -1,28 +0,0 @@ -load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make") - -filegroup( - name = "all", - srcs = glob(["**"]), -) - -configure_make( - name = "urcrypt", - autogen = True, - configure_in_place = True, - configure_options = [ - "--disable-shared", - ], - copts = [ - "-Wall", - "-g", - "-O2", - ], - deps = [ - "@aes_siv", - "@openssl", - "@secp256k1" - ], - lib_source = ":all", - out_static_libs = ["liburcrypt.a"], - visibility = ["//visibility:public"], -) diff --git a/bazel/third_party/uv/BUILD.bazel b/bazel/third_party/uv/BUILD.bazel deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/bazel/third_party/uv/uv.BUILD b/bazel/third_party/uv/uv.BUILD deleted file mode 100644 index 1fd2352559..0000000000 --- a/bazel/third_party/uv/uv.BUILD +++ /dev/null @@ -1,28 +0,0 @@ -load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make") - -filegroup( - name = "all", - srcs = glob(["**"]), -) - -configure_make( - name = "uv", - args = select({ - "@platforms//os:macos": ["--jobs=`sysctl -n hw.logicalcpu`"], - "//conditions:default": ["--jobs=`nproc`"], - }), - autogen = True, - configure_in_place = True, - configure_options = [ - "--disable-shared", - ] + select({ - "@//:linux_aarch64": ["--host=aarch64-linux-musl"], - "@//:linux_x86_64": ["--host=x86_64-linux-musl"], - "//conditions:default": [], - }), - copts = ["-O3"], - lib_source = ":all", - out_static_libs = ["libuv.a"], - targets = ["install"], - visibility = ["//visibility:public"], -) diff --git a/bazel/third_party/whereami/BUILD.bazel b/bazel/third_party/whereami/BUILD.bazel deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/bazel/third_party/whereami/whereami.BUILD b/bazel/third_party/whereami/whereami.BUILD deleted file mode 100644 index 4d314afec4..0000000000 --- a/bazel/third_party/whereami/whereami.BUILD +++ /dev/null @@ -1,8 +0,0 @@ -cc_library( - name = "whereami", - srcs = ["src/whereami.c"], - hdrs = ["src/whereami.h"], - copts = ["-O3"], - includes = ["src"], - visibility = ["//visibility:public"], -) diff --git a/bazel/third_party/zlib/BUILD.bazel b/bazel/third_party/zlib/BUILD.bazel deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/bazel/third_party/zlib/zlib.BUILD b/bazel/third_party/zlib/zlib.BUILD deleted file mode 100644 index 2b351f2c30..0000000000 --- a/bazel/third_party/zlib/zlib.BUILD +++ /dev/null @@ -1,19 +0,0 @@ -load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make") - -filegroup( - name = "all", - srcs = glob(["**"]), -) - -configure_make( - name = "zlib", - args = select({ - "@platforms//os:macos": ["--jobs=`sysctl -n hw.logicalcpu`"], - "//conditions:default": ["--jobs=`nproc`"], - }), - configure_options = ["--static"], - copts = ["-O3"], - lib_source = ":all", - out_static_libs = ["libz.a"], - visibility = ["//visibility:public"], -) diff --git a/bazel/toolchain/BUILD.bazel b/bazel/toolchain/BUILD.bazel deleted file mode 100644 index 38fa379197..0000000000 --- a/bazel/toolchain/BUILD.bazel +++ /dev/null @@ -1,410 +0,0 @@ -# ADDING A NEW TOOLCHAIN -# -# The general process for adding a new toolchain is as follows: -# -# (1) Define a `cc_toolchaing_config()` rule target with a `name` attribute of -# the form `---config` and a `toolchain_identifier` of -# the form `toolchain---`. The `cc_toolchain_config()` -# definition in `//bazel/toolchain:cfg.bzl` lists the mandatory attributes -# for the `cc_toolchain_config()` rule. If any of the toolchain's include -# paths require a compiler version number, simply replace the version number -# with `{compiler_version}`, which is replaced with the value of the label -# indicated by the `compiler_version` attribute when the rule is resolved. -# (2) Define a `cc_toolchain()` rule target with a `name` attribute of the form -# `--` and a `toolchain_config` attribute that references -# the `cc_toolchain_config()` target from (1). -# (3) Define a `toolchain()` rule target with a `name` attribute of the form -# `---toolchain` and specify the platform the toolchain -# runs on via the `exec_compatible_with` attribute and the platform the -# toolchain targets (i.e. compiles for) via the `target_compatible_with` -# attribute. Use the `cc_toolchain()` target from (2) as the value of the -# `toolchain` attribute and `:toolchain_type` for the `toolchain_type` -# attribute. -# (4) Register the new toolchain in `//:WORKSPACE.bazel` by adding the -# toolchain's label (i.e. -# `//bazel/toolchain:---toolchain`) to the -# `register_toolchains()` function. -# -# For more information on configuring toolchains, see -# https://bazel.build/tutorials/ccp-toolchain-config -# and https://bazel.build/extending/toolchains. - -load(":cfg.bzl", "cc_toolchain_config") - -# The prefix for installed external toolchains. -_install_prefix = "/usr/local" - -filegroup(name = "empty") - -alias( - name = "toolchain_type", - actual = "@bazel_tools//tools/cpp:toolchain_type", -) - -# -# gcc-linux-aarch64 -# - -# Toolchain identifier. -_aarch64_gcc = "toolchain-gcc-linux-aarch64" - -cc_toolchain_config( - name = "gcc-linux-aarch64-config", - ar = "{}/aarch64-linux-musl/bin/aarch64-linux-musl-gcc-ar".format(_install_prefix), - cc = "{}/aarch64-linux-musl/bin/aarch64-linux-musl-gcc".format(_install_prefix), - cc_flags = [ - "-static", - "--sysroot={}/aarch64-linux-musl".format(_install_prefix), - ], - compiler = "aarch64-linux-musl-gcc", - compiler_version = "//:gcc_version", - ld = "{}/aarch64-linux-musl/bin/aarch64-linux-musl-ld".format(_install_prefix), - ld_flags = ["--sysroot={}/aarch64-linux-musl".format(_install_prefix)], - sys_includes = [ - "{}/aarch64-linux-musl/aarch64-linux-musl/include".format(_install_prefix), - "{}/aarch64-linux-musl/lib/gcc/aarch64-linux-musl/{{compiler_version}}/include".format(_install_prefix), - ], - target_cpu = "aarch64", - toolchain_identifier = _aarch64_gcc, -) - -cc_toolchain( - name = "gcc-linux-aarch64", - all_files = ":empty", - compiler_files = ":empty", - dwp_files = ":empty", - linker_files = ":empty", - objcopy_files = ":empty", - strip_files = ":empty", - supports_param_files = 0, - toolchain_config = ":gcc-linux-aarch64-config", - toolchain_identifier = _aarch64_gcc, -) - -toolchain( - name = "gcc-linux-aarch64-toolchain", - exec_compatible_with = [ - "@platforms//os:linux", - "@platforms//cpu:aarch64", - ], - target_compatible_with = [ - "@platforms//os:linux", - "@platforms//cpu:aarch64", - ], - toolchain = ":gcc-linux-aarch64", - toolchain_type = ":toolchain_type", -) - -# -# gcc-linux-x86_64 -# - -# Toolchain identifier. -_x86_64_gcc = "toolchain-gcc-linux-x86_64" - -cc_toolchain_config( - name = "gcc-linux-x86_64-config", - ar = "{}/x86_64-linux-musl/bin/x86_64-linux-musl-gcc-ar".format(_install_prefix), - cc = "{}/x86_64-linux-musl/bin/x86_64-linux-musl-gcc".format(_install_prefix), - cc_flags = [ - "-static", - "--sysroot={}/x86_64-linux-musl".format(_install_prefix), - ], - compiler = "x86_64-linux-musl-gcc", - compiler_version = "//:gcc_version", - ld = "{}/x86_64-linux-musl/bin/x86_64-linux-musl-ld".format(_install_prefix), - ld_flags = ["--sysroot={}/x86_64-linux-musl".format(_install_prefix)], - sys_includes = [ - "{}/x86_64-linux-musl/x86_64-linux-musl/include".format(_install_prefix), - "{}/x86_64-linux-musl/lib/gcc/x86_64-linux-musl/{{compiler_version}}/include".format(_install_prefix), - ], - target_cpu = "x86_64", - toolchain_identifier = _x86_64_gcc, -) - -cc_toolchain( - name = "gcc-linux-x86_64", - all_files = ":empty", - compiler_files = ":empty", - dwp_files = ":empty", - linker_files = ":empty", - objcopy_files = ":empty", - strip_files = ":empty", - supports_param_files = 0, - toolchain_config = ":gcc-linux-x86_64-config", - toolchain_identifier = _x86_64_gcc, -) - -toolchain( - name = "gcc-linux-x86_64-toolchain", - exec_compatible_with = [ - "@platforms//os:linux", - "@platforms//cpu:x86_64", - ], - target_compatible_with = [ - "@platforms//os:linux", - "@platforms//cpu:x86_64", - ], - toolchain = ":gcc-linux-x86_64", - toolchain_type = ":toolchain_type", -) - -# -# clang-macos-aarch64 -# - -# Toolchain identifier. -_macos_aarch64_clang = "toolchain-clang-macos-aarch64" - -cc_toolchain_config( - name = "clang-macos-aarch64-config", - # NOTE: building with `libtool` does not work on macOS due to lack of - # support in the `configure_make` rule provided by `rules_foreign_cc`. - # Therefore, we require setting `ar` as the archiver tool on macOS. - ar = "/usr/bin/ar", - # By default, Bazel passes the `rcsD` flags to `ar`, but macOS's `ar` - # implementation doesn't support `D`. We remove it with this attribute - # and corresponding `ar_flags_feature` in `cfg.bzl`. - # See https://github.com/bazelbuild/bazel/issues/15875. - ar_flags = "rcs", - cc = "/usr/bin/clang", - compiler = "clang", - compiler_version = "//:clang_version", - ld = "/usr/bin/ld", - sys_includes = [ - "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include", - "/Library/Developer/CommandLineTools/usr/lib/clang/{compiler_version}/include", - "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include", - "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/{compiler_version}/include", - ], - target_cpu = "aarch64", - toolchain_identifier = _macos_aarch64_clang, -) - -cc_toolchain( - name = "clang-macos-aarch64", - all_files = ":empty", - compiler_files = ":empty", - dwp_files = ":empty", - linker_files = ":empty", - objcopy_files = ":empty", - strip_files = ":empty", - supports_param_files = 0, - toolchain_config = ":clang-macos-aarch64-config", - toolchain_identifier = _macos_aarch64_clang, -) - -toolchain( - name = "clang-macos-aarch64-toolchain", - exec_compatible_with = [ - "@platforms//os:macos", - "@platforms//cpu:aarch64", - ], - target_compatible_with = [ - "@platforms//os:macos", - "@platforms//cpu:aarch64", - ], - toolchain = ":clang-macos-aarch64", - toolchain_type = ":toolchain_type", -) - -# -# clang-macos-x86_64 -# - -# Toolchain identifier. -_macos_x86_64_clang = "toolchain-clang-macos-x86_64" - -cc_toolchain_config( - name = "clang-macos-x86_64-config", - # NOTE: building with `libtool` does not work on macOS due to lack of - # support in the `configure_make` rule provided by `rules_foreign_cc`. - # Therefore, we require setting `ar` as the archiver tool on macOS. - ar = "/usr/bin/ar", - # By default, Bazel passes the `rcsD` flags to `ar`, but macOS's `ar` - # implementation doesn't support `D`. We remove it with this attribute - # and corresponding `ar_flags_feature` in `cfg.bzl`. - # See https://github.com/bazelbuild/bazel/issues/15875. - ar_flags = "rcs", - cc = "/usr/bin/clang", - compiler = "clang", - compiler_version = "//:clang_version", - ld = "/usr/bin/ld", - sys_includes = [ - "/Library/Developer/CommandLineTools/usr/lib/clang/{compiler_version}/include", - "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/", - "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include", - "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/{compiler_version}/include", - ], - target_cpu = "x86_64", - toolchain_identifier = _macos_x86_64_clang, -) - -cc_toolchain( - name = "clang-macos-x86_64", - all_files = ":empty", - compiler_files = ":empty", - dwp_files = ":empty", - linker_files = ":empty", - objcopy_files = ":empty", - strip_files = ":empty", - supports_param_files = 0, - toolchain_config = ":clang-macos-x86_64-config", - toolchain_identifier = _macos_x86_64_clang, -) - -toolchain( - name = "clang-macos-x86_64-toolchain", - exec_compatible_with = [ - "@platforms//os:macos", - "@platforms//cpu:x86_64", - ], - target_compatible_with = [ - "@platforms//os:macos", - "@platforms//cpu:x86_64", - ], - toolchain = ":clang-macos-x86_64", - toolchain_type = ":toolchain_type", -) - -# -# brew-clang-macos-x86_64 -# - -# Toolchain identifier. -_macos_x86_64_brew_clang = "toolchain-brew-clang-macos-x86_64" - -cc_toolchain_config( - name = "brew-clang-macos-x86_64-config", - # NOTE: building with `libtool` does not work on macOS due to lack of - # support in the `configure_make` rule provided by `rules_foreign_cc`. - # Therefore, we require setting `ar` as the archiver tool on macOS. - ar = "/usr/local/opt/llvm@15/bin/llvm-ar", - # By default, Bazel passes the `rcsD` flags to `ar`, but macOS's `ar` - # implementation doesn't support `D`. We remove it with this attribute - # and corresponding `ar_flags_feature` in `cfg.bzl`. - # See https://github.com/bazelbuild/bazel/issues/15875. - ar_flags = "rcs", - cc = "/usr/local/opt/llvm@15/bin/clang", - compiler = "clang", - compiler_version = "//:clang_version", - ld = "/usr/local/opt/llvm@15/bin/llvm-lld", - nm = "/usr/local/opt/llvm@15/bin/llvm-nm", - sys_includes = [ - "/usr/local/Cellar/llvm@15/15.0.7/lib/clang/15.0.7/include", - "/Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk/usr/include", - ], - target_cpu = "x86_64", - toolchain_identifier = _macos_x86_64_brew_clang, -) - -cc_toolchain( - name = "brew-clang-macos-x86_64", - all_files = ":empty", - compiler_files = ":empty", - dwp_files = ":empty", - linker_files = ":empty", - objcopy_files = ":empty", - strip_files = ":empty", - supports_param_files = 0, - toolchain_config = ":brew-clang-macos-x86_64-config", - toolchain_identifier = _macos_x86_64_brew_clang, -) - -toolchain( - name = "brew-clang-macos-x86_64-toolchain", - exec_compatible_with = [ - "@platforms//os:macos", - "@platforms//cpu:x86_64", - ], - target_compatible_with = [ - "@platforms//os:macos", - "@platforms//cpu:x86_64", - ], - toolchain = ":brew-clang-macos-x86_64", - toolchain_type = ":toolchain_type", -) - -# -# BOOTSTRAPPING -# - -# We can't build some artifacts, like musl libc and its toolchain, in Bazel -# itself (i.e. by adding a remote repository to `WORKSPACE.bazel` and a -# corresponding `BUILD` file in `bazel/third_party/`) because doing -# so introduces a circular dependency during Bazel C/C++ toolchain resolution. - -# musl-cross-make builds musl-libc-compatible gcc toolchains from source. -_musl_cross_make_version = "99f2cbc7e230f72bde3394be3ebd50497cb53e89" - -_musl_cross_make_archive = "https://github.com/ripperi/musl-cross-make/archive/{}.tar.gz".format(_musl_cross_make_version) - -genrule( - name = "install-aarch64-linux-musl-gcc", - outs = ["install-aarch64-linux-musl-gcc.sh"], - cmd = """ - echo 'aarch64_linux_musl_install={}/aarch64-linux-musl' > $@ - echo 'if [ ! -d $$aarch64_linux_musl_install ]; then' >> $@ - echo ' wget -c {}' >> $@ - echo ' tar -xf {}.tar.gz' >> $@ - echo ' archive=musl-cross-make-{}' >> $@ - echo ' echo OUTPUT=$$aarch64_linux_musl_install > $$archive/config.mak' >> $@ - echo ' echo GCC_VER=11.4.0 >> $$archive/config.mak' >> $@ - echo ' echo MUSL_VER=1.2.5 >> $$archive/config.mak' >> $@ - echo ' TARGET=aarch64-linux-musl make -s -C$$archive -j`nproc`' >> $@ - echo ' sudo TARGET=aarch64-linux-musl make -s -C$$archive -j`nproc` install' >> $@ - echo ' sudo chown --recursive $$USER $$aarch64_linux_musl_install' >> $@ - echo 'fi' >> $@ - """.format( - _install_prefix, - _musl_cross_make_archive, - _musl_cross_make_version, - _musl_cross_make_version, - ), - exec_compatible_with = ["@platforms//os:linux"], - target_compatible_with = ["@platforms//os:linux"], - visibility = ["//visibility:private"], -) - -sh_binary( - name = "aarch64-linux-musl-gcc", - srcs = ["install-aarch64-linux-musl-gcc"], - exec_compatible_with = ["@platforms//os:linux"], - target_compatible_with = ["@platforms//os:linux"], - visibility = ["//visibility:public"], -) - -genrule( - name = "install-x86_64-linux-musl-gcc", - outs = ["install-x86_64-linux-musl-gcc.sh"], - cmd = """ - echo 'x86_64_linux_musl_install={}/x86_64-linux-musl' > $@ - echo 'if [ ! -d $$x86_64_linux_musl_install ]; then' >> $@ - echo ' wget -c {}' >> $@ - echo ' tar -xf {}.tar.gz' >> $@ - echo ' archive=musl-cross-make-{}' >> $@ - echo ' echo OUTPUT=$$x86_64_linux_musl_install > $$archive/config.mak' >> $@ - echo ' echo GCC_VER=11.4.0 >> $$archive/config.mak' >> $@ - echo ' echo MUSL_VER=1.2.5 >> $$archive/config.mak' >> $@ - echo ' TARGET=x86_64-linux-musl make -s -C$$archive -j`nproc`' >> $@ - echo ' sudo TARGET=x86_64-linux-musl make -s -C$$archive -j`nproc` install' >> $@ - echo ' sudo chown --recursive $$USER $$x86_64_linux_musl_install' >> $@ - echo 'fi' >> $@ - """.format( - _install_prefix, - _musl_cross_make_archive, - _musl_cross_make_version, - _musl_cross_make_version, - ), - exec_compatible_with = ["@platforms//os:linux"], - target_compatible_with = ["@platforms//os:linux"], - visibility = ["//visibility:private"], -) - -sh_binary( - name = "x86_64-linux-musl-gcc", - srcs = ["install-x86_64-linux-musl-gcc"], - exec_compatible_with = ["@platforms//os:linux"], - target_compatible_with = ["@platforms//os:linux"], - visibility = ["//visibility:public"], -) diff --git a/bazel/toolchain/cfg.bzl b/bazel/toolchain/cfg.bzl deleted file mode 100644 index aa41e1c2c4..0000000000 --- a/bazel/toolchain/cfg.bzl +++ /dev/null @@ -1,168 +0,0 @@ -load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo") -load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES") -load( - "@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", - "feature", - "flag_group", - "flag_set", - "tool_path", - "variable_with_value", -) - -def _cc_toolchain_config_impl(ctx): - ar_flags = feature( - name = "archiver_flags", - flag_sets = [ - flag_set( - actions = [ACTION_NAMES.cpp_link_static_library], - flag_groups = [ - flag_group(flags = [ctx.attr.ar_flags]), - flag_group( - flags = ["%{output_execpath}"], - expand_if_available = "output_execpath", - ), - ], - ), - flag_set( - actions = [ACTION_NAMES.cpp_link_static_library], - flag_groups = [ - flag_group( - iterate_over = "libraries_to_link", - flag_groups = [ - flag_group( - flags = ["%{libraries_to_link.name}"], - expand_if_equal = variable_with_value( - name = "libraries_to_link.type", - value = "object_file", - ), - ), - flag_group( - flags = ["%{libraries_to_link.object_files}"], - iterate_over = "libraries_to_link.object_files", - expand_if_equal = variable_with_value( - name = "libraries_to_link.type", - value = "object_file_group", - ), - ), - ], - expand_if_available = "libraries_to_link", - ), - ], - ), - ], - ) - features = [ar_flags] - - if len(ctx.attr.cc_flags) > 0: - cc_flags = feature( - name = "cc_flags", - enabled = True, - flag_sets = [ - flag_set( - actions = [ACTION_NAMES.c_compile], - flag_groups = [flag_group(flags = ctx.attr.cc_flags)], - ), - ], - ) - features.append(cc_flags) - - - if len(ctx.attr.ld_flags) > 0: - ld_flags = feature( - name = "ld_flags", - enabled = True, - flag_sets = [ - flag_set( - actions = [ - ACTION_NAMES.cpp_link_dynamic_library, - ACTION_NAMES.cpp_link_executable, - ACTION_NAMES.cpp_link_nodeps_dynamic_library, - ], - flag_groups = [flag_group(flags = ctx.attr.ld_flags)], - ), - ], - ) - features.append(ld_flags) - - # See - # https://bazel.build/rules/lib/cc_common#create_cc_toolchain_config_info. - return cc_common.create_cc_toolchain_config_info( - ctx = ctx, - # Replace `{compiler_version}` in all include paths with the value of - # the `compiler_version` label. - cxx_builtin_include_directories = [ - path.format(compiler_version = ctx.attr.compiler_version[BuildSettingInfo].value) - for path in ctx.attr.sys_includes - ], - features = features, - toolchain_identifier = ctx.attr.toolchain_identifier, - target_system_name = ctx.attr.target_system_name, - target_cpu = ctx.attr.target_cpu, - target_libc = ctx.attr.target_libc, - compiler = ctx.attr.compiler, - abi_version = ctx.attr.abi_version, - abi_libc_version = ctx.attr.abi_libc_version, - tool_paths = [ - tool_path( - name = "gcc", - path = ctx.attr.cc, - ), - tool_path( - name = "ld", - path = ctx.attr.ld, - ), - tool_path( - name = "ar", - path = ctx.attr.ar, - ), - tool_path( - name = "cpp", - path = ctx.attr.cpp, - ), - tool_path( - name = "gcov", - path = ctx.attr.gcov, - ), - tool_path( - name = "nm", - path = ctx.attr.nm, - ), - tool_path( - name = "objdump", - path = ctx.attr.objdump, - ), - tool_path( - name = "strip", - path = ctx.attr.strip, - ), - ], - ) - -cc_toolchain_config = rule( - implementation = _cc_toolchain_config_impl, - attrs = { - # Required. - "ar": attr.string(mandatory = True), - "cc": attr.string(mandatory = True), - "compiler": attr.string(mandatory = True), - "compiler_version": attr.label(mandatory = True), - "ld": attr.string(mandatory = True), - "target_cpu": attr.string(mandatory = True), - "toolchain_identifier": attr.string(mandatory = True), - # Optional. - "abi_libc_version": attr.string(default = "unknown"), - "abi_version": attr.string(default = "unknown"), - "ar_flags": attr.string(default = "rcsD"), - "cc_flags": attr.string_list(default = []), - "cpp": attr.string(default = "/bin/false"), - "gcov": attr.string(default = "/bin/false"), - "ld_flags": attr.string_list(default = []), - "nm": attr.string(default = "/bin/false"), - "objdump": attr.string(default = "/bin/false"), - "strip": attr.string(default = "/bin/false"), - "sys_includes": attr.string_list(default = []), - "target_libc": attr.string(default = "unknown"), - "target_system_name": attr.string(default = "unknown"), - }, - provides = [CcToolchainConfigInfo], -) diff --git a/docker/BUILD.bazel b/docker/BUILD.bazel deleted file mode 100644 index 6760b71221..0000000000 --- a/docker/BUILD.bazel +++ /dev/null @@ -1,116 +0,0 @@ -load("@io_bazel_rules_docker//container:container.bzl", "container_image", "container_push") -load("@io_bazel_rules_docker//docker/util:run.bzl", "container_run_and_commit_layer") - -# -# IMAGE CREATION -# - -sh_binary( - name = "start-urbit", - srcs = ["start_urbit.sh"], - visibility = ["//visibility:private"], -) - -sh_binary( - name = "get-urbit-code", - srcs = ["get_urbit_code.sh"], - visibility = ["//visibility:private"], -) - -sh_binary( - name = "reset-urbit-code", - srcs = ["reset_urbit_code.sh"], - visibility = ["//visibility:private"], -) - -container_run_and_commit_layer( - name = "install_pkgs", - commands = [ - "apk update", - "apk add bash curl", - ], - docker_run_flags = ["--network=host"], - image = "@alpine_linux_x86_64//image", - visibility = ["//visibility:private"], -) - -container_image( - name = "image_x86_64", - base = "@alpine_linux_x86_64//image", - cmd = ["start-urbit"], - directory = "/bin", - # `/bin` is probably on the default path, but append it just to be safe. - env = { - "PATH": "$$PATH:/bin", - }, - files = [ - ":get-urbit-code", - ":reset-urbit-code", - ":start-urbit", - "//pkg/vere:urbit", - ], - layers = [":install_pkgs"], - ports = [ - "80/tcp", - "34343/udp", - ], - volumes = ["/urbit"], - workdir = "/urbit", - visibility = ["//visibility:public"], -) - -# -# IMAGE UPLOAD -# - -_docker_repository = "tloncorp/vere" - -genrule( - name = "version_tag", - srcs = ["//:version"], - outs = ["docker_version_tag"], - cmd = """ - echo -n v$$(cat $<) > $@ - """, - visibility = ["//visibility:private"], -) - -genrule( - name = "pace_tag", - srcs = ["//:PACE"], - outs = ["docker_pace_tag"], - cmd = """ - tr -d '\n' < $< > $@ - """, - visibility = ["//visibility:private"], -) - -container_push( - name = "push_latest_x86_64", - format = "Docker", - image = ":image_x86_64", - registry = "docker.io", - repository = _docker_repository, - tag = "latest", - visibility = ["//visibility:public"], -) - -container_push( - name = "push_version_x86_64", - format = "Docker", - image = ":image_x86_64", - registry = "docker.io", - repository = _docker_repository, - tag_file = ":version_tag", - visibility = ["//visibility:public"], -) - -container_push( - name = "push_pace_x86_64", - format = "Docker", - image = ":image_x86_64", - registry = "docker.io", - repository = _docker_repository, - tag_file = ":pace_tag", - visibility = ["//visibility:public"], -) diff --git a/pkg/c3/BUILD.bazel b/pkg/c3/BUILD.bazel deleted file mode 100644 index 95b790b5ec..0000000000 --- a/pkg/c3/BUILD.bazel +++ /dev/null @@ -1,42 +0,0 @@ -# -# LIBRARIES -# - -load("//bazel:common_settings.bzl", "vere_library") - -vere_library( - name = "c3", - srcs = glob( - [ - "*.h", - "*.c", - ], - exclude = [ - "c3.h", - "*_tests.c", - ], - ), - hdrs = ["c3.h"], - # We define platform-specific macros here to avoid the need for a - # configuration header file. - defines = [ - # We don't build on any big endian CPUs. - "U3_OS_ENDIAN_little=1", - "U3_OS_PROF=1", - ] + select({ - "@platforms//cpu:aarch64": ["U3_CPU_aarch64=1"], - "//conditions:default": [], - }) + select({ - "@platforms//os:linux": ["U3_OS_linux=1"], - "@platforms//os:macos": ["U3_OS_osx=1"], - "//conditions:default": [], - }), - include_prefix = "c3", - includes = ["."], - linkstatic = True, - visibility = ["//pkg:__subpackages__"], -) - -# -# TESTS -# diff --git a/pkg/ent/BUILD.bazel b/pkg/ent/BUILD.bazel deleted file mode 100644 index ad9894b1e2..0000000000 --- a/pkg/ent/BUILD.bazel +++ /dev/null @@ -1,49 +0,0 @@ -# -# LIBRARIES -# - -load("//bazel:common_settings.bzl", "vere_library") - -vere_library( - name = "ent", - srcs = ["ent.c"], - hdrs = ["ent.h"], - copts = [ - "-O3", - "-Wall", - "-Werror", - "-pedantic", - "-std=gnu99", - ], - include_prefix = "ent", - includes = ["."], - linkstatic = True, - local_defines = select({ - "@platforms//os:macos": ["ENT_GETENTROPY_SYSRANDOM"], - # TODO: support fallback to other options if `getrandom()` isn't - # available in `unistd.h`. Preferred order (from most preferred to least - # preferred) is: - # - ENT_GETENTROPY_UNISTD - # - ENT_GETENTROPY_SYSRANDOM - # - ENT_GETRANDOM_SYSCALL - # - ENT_DEV_URANDOM - "//conditions:default": ["ENT_GETENTROPY_UNISTD"], - }), - visibility = ["//:__subpackages__"], -) - -# -# TESTS -# - -cc_test( - name = "tests", - timeout = "short", - srcs = ["tests.c"], - features = select({ - "@platforms//os:linux": ["fully_static_link"], - "//conditions:default": [], - }), - visibility = ["//visibility:private"], - deps = [":ent"], -) diff --git a/pkg/noun/BUILD.bazel b/pkg/noun/BUILD.bazel deleted file mode 100644 index 1be5825990..0000000000 --- a/pkg/noun/BUILD.bazel +++ /dev/null @@ -1,117 +0,0 @@ -# -# LIBRARIES -# - -load("//bazel:common_settings.bzl", "vere_library") - -vere_library( - name = "noun", - srcs = glob( - [ - "**/*.c", - "**/*.h", - "jets/*.h", - "jets/**/*.c", - ], - exclude = [ - "noun.h", - "*_tests.c", - ], - ), - hdrs = ["noun.h"], - includes = ["."], - linkopts = select({ - # NOTE: macOS builds fail to link the `u3o_Config` symbol (and maybe - # others) without this. We should figure out why, and remove this. - "@platforms//os:macos": ["-force_load $(GENDIR)/pkg/noun/libnoun.a"], - "//conditions:default": [], - }), - linkstatic = True, - local_defines = ["U3_GUARD_PAGE"], - visibility = ["//pkg:__subpackages__"], - deps = [ - "//pkg/c3", - "//pkg/ent", - "//pkg/ur", - "@gmp", - "@libbacktrace", - "@murmur3", - "@openssl", - "@pdjson", - "@sigsegv", - "@softfloat", - "@urcrypt", - "@whereami", - ] + select({ - "@platforms//os:macos": ["//pkg/noun/platform/darwin"], - "@platforms//os:linux": [ - "//pkg/noun/platform/linux", - "@libunwind", - ], - "//conditions:default": [], - }), -) - -# -# TESTS -# - -cc_test( - name = "hashtable_tests", - timeout = "short", - srcs = ["hashtable_tests.c"], - features = select({ - "@platforms//os:linux": ["fully_static_link"], - "//conditions:default": [], - }), - visibility = ["//visibility:private"], - deps = [":noun"], -) - -cc_test( - name = "jets_tests", - timeout = "short", - srcs = ["jets_tests.c"], - features = select({ - "@platforms//os:linux": ["fully_static_link"], - "//conditions:default": [], - }), - visibility = ["//visibility:private"], - deps = [":noun"], -) - -cc_test( - name = "nock_tests", - timeout = "short", - srcs = ["nock_tests.c"], - features = select({ - "@platforms//os:linux": ["fully_static_link"], - "//conditions:default": [], - }), - visibility = ["//visibility:private"], - deps = [":noun"], -) - -cc_test( - name = "retrieve_tests", - timeout = "short", - srcs = ["retrieve_tests.c"], - features = select({ - "@platforms//os:linux": ["fully_static_link"], - "//conditions:default": [], - }), - visibility = ["//visibility:private"], - deps = [":noun"], -) - -cc_test( - name = "serial_tests", - timeout = "short", - srcs = ["serial_tests.c"], - features = select({ - "@platforms//os:linux": ["fully_static_link"], - "//conditions:default": [], - }), - visibility = ["//visibility:private"], - deps = [":noun"], -) diff --git a/pkg/noun/platform/darwin/BUILD.bazel b/pkg/noun/platform/darwin/BUILD.bazel deleted file mode 100644 index 0724009886..0000000000 --- a/pkg/noun/platform/darwin/BUILD.bazel +++ /dev/null @@ -1,8 +0,0 @@ -cc_library( - name = "darwin", - hdrs = ["rsignal.h"], - include_prefix = "platform", - includes = ["."], - target_compatible_with = ["@platforms//os:macos"], - visibility = ["//pkg:__subpackages__"], -) diff --git a/pkg/noun/platform/linux/BUILD.bazel b/pkg/noun/platform/linux/BUILD.bazel deleted file mode 100644 index 506d64e175..0000000000 --- a/pkg/noun/platform/linux/BUILD.bazel +++ /dev/null @@ -1,8 +0,0 @@ -cc_library( - name = "linux", - hdrs = ["rsignal.h"], - include_prefix = "platform", - includes = ["."], - target_compatible_with = ["@platforms//os:linux"], - visibility = ["//pkg:__subpackages__"], -) diff --git a/pkg/ur/BUILD.bazel b/pkg/ur/BUILD.bazel deleted file mode 100644 index 4deff1e706..0000000000 --- a/pkg/ur/BUILD.bazel +++ /dev/null @@ -1,39 +0,0 @@ -# -# LIBRARIES -# - -load("//bazel:common_settings.bzl", "vere_library") - -vere_library( - name = "ur", - srcs = [ - "bitstream.c", - "bitstream.h", - "defs.h", - "hashcons.c", - "hashcons.h", - "serial.c", - "serial.h", - ], - hdrs = ["ur.h"], - includes = ["."], - linkstatic = True, - visibility = ["//pkg:__subpackages__"], - deps = ["@murmur3"], -) - -# -# TESTS -# - -cc_test( - name = "tests", - timeout = "short", - srcs = ["tests.c"], - features = select({ - "@platforms//os:linux": ["fully_static_link"], - "//conditions:default": [], - }), - visibility = ["//visibility:private"], - deps = [":ur"], -) diff --git a/pkg/vere/BUILD.bazel b/pkg/vere/BUILD.bazel deleted file mode 100644 index 33882e9244..0000000000 --- a/pkg/vere/BUILD.bazel +++ /dev/null @@ -1,522 +0,0 @@ -# -# GENERATED FILES -# - -load("//bazel:common_settings.bzl", "vere_library", "vere_binary") - -# An approximation of `xxd -i` that runs on all platforms where Bash is -# present. Generates a `.h` file that declares the array and array length as -# `extern` global variables and a `.c` file containing the array and array -# length definitions that includes the `.h` file. -# -# Referencing a shell variable requires `$$`, not `$`. See -# https://bazel.build/reference/be/general#general-advice. -XXD_I = """ -function xxd_i() { - var=$$1 - src_file=$$2 - dst_h_file=$$3 - dst_c_file=$$4 - - # Generate `.h` file. - echo "#ifndef $${var}_H" > $$dst_h_file - echo "#define $${var}_H" >> $$dst_h_file - echo "extern unsigned char $$var[];" >> $$dst_h_file - echo "extern unsigned int $${var}_len;" >> $$dst_h_file - echo '#endif' >> $$dst_h_file - - # Generate `.c` file. - printf '#include "%s"\n' $$dst_h_file > $$dst_c_file - echo "unsigned char $$var[] = {" >> $$dst_c_file - cnt=0 - while IFS='' read line - do - for byte in $$line - do - echo -n " 0x$$byte," >> $$dst_c_file - cnt=$$((cnt+1)) - done - # <() is syntax for Bash process substitution. - done < <(od -An -v -tx1 $$src_file) - echo "};" >> $$dst_c_file - echo "unsigned int $${var}_len = $$cnt;" >> $$dst_c_file -} -""" - -# Produce a certificate authority bundle as a byte array. -genrule( - name = "ca_bundle", - srcs = ["@ca_bundle//file"], - outs = [ - "ca_bundle.h", - "ca_bundle.c", - ], - cmd_bash = """ - # Defines the function `xxd_i()`. - {} - - src_file=$(execpath @ca_bundle//file) - dst_h_file=$(execpath ca_bundle.h) - dst_c_file=$(execpath ca_bundle.c) - xxd_i include_ca_bundle_crt $$src_file $$dst_h_file $$dst_c_file - """.format(XXD_I), - visibility = ["//visibility:private"], -) - -# Produce an `ivory.pill` as a byte array. -genrule( - name = "ivory", - srcs = ["@ivory_pill//file"], - outs = [ - "ivory.h", - "ivory.c", - ], - cmd_bash = """ - # Defines the function `xxd_i()`. - {} - - # Create ivory.h. - src_file=$(execpath @ivory_pill//file) - dst_h_file=$(execpath ivory.h) - dst_c_file=$(execpath ivory.c) - xxd_i u3_Ivory_pill $$src_file $$dst_h_file $$dst_c_file - """.format(XXD_I), - visibility = ["//visibility:private"], -) - -# -# LIBRARIES -# - -vere_library( - name = "vere", - srcs = glob( - [ - "*.c", - "*.h", - "db/*.c", - "io/*.c", - "io/*/*.h", - "io/*/*.c", - ], - exclude = [ - "main.c", - "vere.h", - "*_tests.c", - ], - ) + [ - ":ivory", - "//:pace_hdr", - "//:version_hdr", - ] + select({ - "@platforms//os:macos": [ - "platform/darwin/daemon.c", - "platform/darwin/ptty.c", - "platform/darwin/mach.c", - ], - "@platforms//os:linux": [ - "platform/linux/daemon.c", - "platform/linux/ptty.c", - ], - }), - hdrs = [ - "db/lmdb.h", - "vere.h", - "mdns.h", - ] + select({ - "@platforms//os:macos": [], - "@platforms//os:linux": [ - "dns_sd.h", - ], - }), - includes = ["."], - linkstatic = True, - visibility = ["//pkg:__subpackages__"], - deps = [ - "//pkg/c3", - "//pkg/ent", - "//pkg/noun", - "//pkg/ur", - "@curl", - "@h2o", - "@lmdb", - "@openssl", - "@uv", - "@natpmp", - ] + select({ - "@platforms//os:macos": [], - "@platforms//os:linux": [ - "@avahi", - ], - }) -) - -# -# BINARIES -# - -vere_binary( - name = "urbit", - srcs = [ - "main.c", - ":ca_bundle", - ":ivory", - "//:pace_hdr", - "//:version_hdr", - ], - features = select({ - "@platforms//os:linux": ["fully_static_link"], - "//conditions:default": [], - }), - visibility = ["//visibility:public"], - deps = [ - "//pkg/c3", - "//pkg/noun", - "//pkg/ur", - ":vere", - # TODO: remove dependency (only used to report version). - "@h2o", - "@openssl", - # TODO: remove dependency (only used to report version). - "@sigsegv", - "@whereami", - ], -) - -# -# TESTS -# - - -cc_library( - name = "ames_src", - hdrs = ["io/ames.c"], -) - -cc_test( - name = "ames_tests", - timeout = "short", - srcs = ["ames_tests.c"], - features = select({ - "@platforms//os:linux": ["fully_static_link"], - "//conditions:default": [], - }), - visibility = ["//visibility:private"], - deps = [":vere", ":ames_src"] -) - -cc_test( - name = "boot_tests", - timeout = "short", - srcs = ["boot_tests.c"], - features = select({ - "@platforms//os:linux": ["fully_static_link"], - "//conditions:default": [], - }), - visibility = ["//visibility:private"], - deps = [":vere"], -) - -cc_test( - name = "newt_tests", - timeout = "short", - srcs = ["newt_tests.c"], - features = select({ - "@platforms//os:linux": ["fully_static_link"], - "//conditions:default": [], - }), - visibility = ["//visibility:private"], - deps = [":vere"], -) - -cc_test( - name = "noun_tests", - timeout = "short", - srcs = ["noun_tests.c"], - features = select({ - "@platforms//os:linux": ["fully_static_link"], - "//conditions:default": [], - }), - visibility = ["//visibility:private"], - deps = [":vere"], -) - -cc_test( - name = "unix_tests", - timeout = "short", - srcs = ["unix_tests.c"], - features = select({ - "@platforms//os:linux": ["fully_static_link"], - "//conditions:default": [], - }), - visibility = ["//visibility:private"], - deps = [":vere"], -) - -cc_test( - name = "benchmarks", - timeout = "short", - srcs = ["benchmarks.c"], - features = select({ - "@platforms//os:linux": ["fully_static_link"], - "//conditions:default": [], - }), - visibility = ["//visibility:private"], - deps = [ - ":vere", - "//pkg/noun", - "//pkg/ur", - ], -) - -# -# FAKE SHIP TESTS -# - -genrule( - name = "boot-fake-ship", - srcs = [ - "@solid_pill//file", - "@urbit//file", - ], - outs = ["fakebus.zip"], - cmd = """ - set -xeuo pipefail - - mkdir ./urbit - tar xfz $(execpath @urbit//file) -C ./urbit --strip-components=1 - cp -RL ./urbit/tests ./urbit/pkg/arvo/tests - - $(execpath :urbit) --lite-boot --daemon --fake bus \ - --bootstrap $(execpath @solid_pill//file) \ - --arvo ./urbit/pkg/arvo \ - --pier ./pier - - cleanup() { - if [ -f ./pier/.vere.lock ]; then - kill $$(< ./pier/.vere.lock) || true - fi - set +x - } - - trap cleanup EXIT - port=$$(grep loopback ./pier/.http.ports | awk -F ' ' '{print $$1}') - - lensd() { - curl -s \ - --data "{\\"source\\":{\\"dojo\\":\\"$$1\\"},\\"sink\\":{\\"stdout\\":null}}" \ - "http://localhost:$$port" | xargs printf %s | sed 's/\\\\n/\\n/g' - } - - lensa() { - curl -s \ - --data "{\\"source\\":{\\"dojo\\":\\"$$2\\"},\\"sink\\":{\\"app\\":\\"$$1\\"}}" \ - "http://localhost:$$port" | xargs printf %s | sed 's/\\\\n/\\n/g' - } - - check() { - [ 3 -eq $$(lensd 3) ] - } - - lensd '+vat %base' - - if check && sleep 10 && check; then - echo "boot success" - lensa hood '+hood/exit' - while [ -f ./pier/.vere.lock ]; do - echo "waiting for pier to shut down" - sleep 5 - done - else - echo "boot failure" - kill $$(< ./pier/.vere.lock) || true - set +x - exit 1 - fi - - set +x - ls - ls -a ./pier - zip -q -r $@ ./pier - """, - tools = [":urbit"], - visibility = ["//visibility:public"], -) - -genrule( - name = "test-fake-ship", - srcs = [ - ":boot-fake-ship", - "//pkg/vere:VERSION", - ], - outs = ["test-fake-ship-output.zip"], - cmd = """ - cp $(execpath :boot-fake-ship) pier.zip - unzip -qq pier.zip - chmod -R u+rw pier - - set -x - - $(execpath :urbit) --lite-boot --daemon --gc ./pier 2> urbit-output - - port=$$(grep loopback ./pier/.http.ports | awk -F ' ' '{print $$1}') - - lensd() { - curl -s \ - --data "{\\"source\\":{\\"dojo\\":\\"$$1\\"},\\"sink\\":{\\"stdout\\":null}}" \ - "http://localhost:$$port" | xargs printf %s | sed 's/\\\\n/\\n/g' - } - - lensa() { - curl -s \ - --data "{\\"source\\":{\\"dojo\\":\\"$$2\\"},\\"sink\\":{\\"app\\":\\"$$1\\"}}" \ - "http://localhost:$$port" | xargs printf %s | sed 's/\\\\n/\\n/g' - } - - tail -F urbit-output >&2 & - - tailproc=$$! - - cleanup () { - kill $$(cat ./pier/.vere.lock) || true - kill "$$tailproc" 2>/dev/null || true - - set +x - } - - trap cleanup EXIT - - # print the arvo version - # - lensd '+vat %base' - - # measure initial memory usage - # - lensd '~& ~ ~& %init-mass-start ~' - lensa hood '+hood/mass' - lensd '~& ~ ~& %init-mass-end ~' - - # run the unit tests - # - lensd '~& ~ ~& %test-unit-start ~' - lensd '-test %/tests ~' - lensd '~& ~ ~& %test-unit-end ~' - - # use the :test app to build all agents, generators, and marks - # - lensa hood '+hood/start %test' - - lensd '~& ~ ~& %test-agents-start ~' - lensa test '%agents' - lensd '~& ~ ~& %test-agents-end ~' - - lensd '~& ~ ~& %test-generators-start ~' - lensa test '%generators' - lensd '~& ~ ~& %test-generators-end ~' - - lensd '~& ~ ~& %test-marks-start ~' - lensa test '%marks' - lensd '~& ~ ~& %test-marks-end ~' - - # measure memory usage post tests - # - lensd '~& ~ ~& %test-mass-start ~' - lensa hood '+hood/mass' - lensd '~& ~ ~& %test-mass-end ~' - - # defragment the loom - # - lensd '~& ~ ~& %pack-start ~' - lensa hood '+hood/pack' - lensd '~& ~ ~& %pack-end ~' - - # reclaim space within arvo - # - lensd '~& ~ ~& %trim-start ~' - lensa hood '+hood/trim' - lensd '~& ~ ~& %trim-end ~' - - # measure memory usage pre |meld - # - lensd '~& ~ ~& %trim-mass-start ~' - lensa hood '+hood/mass' - lensd '~& ~ ~& %trim-mass-end ~' - - # globally deduplicate - # - lensd '~& ~ ~& %meld-start ~' - lensa hood '+hood/meld' - lensd '~& ~ ~& %meld-end ~' - - # measure memory usage post |meld - # - lensd '~& ~ ~& %meld-mass-start ~' - lensa hood '+hood/mass' - lensd '~& ~ ~& %meld-mass-end ~' - - lensa hood '+hood/exit' - - cleanup - - # Collect output - cp urbit-output test-output-unit - cp urbit-output test-output-agents - cp urbit-output test-output-generators - cp urbit-output test-output-marks - - # TODO: when re-enabling fake ship tests on macOS, use `sed -i ''` - # instead of `sed -i`. - sed -i '0,/test-unit-start/d' test-output-unit - sed -i '/test-unit-end/,$$d' test-output-unit - - sed -i '0,/test-agents-start/d' test-output-agents - sed -i '/test-agents-end/,$$d' test-output-agents - - sed -i '0,/test-generators-start/d' test-output-generators - sed -i '/test-generators-end/,$$d' test-output-generators - - sed -i '0,/test-marks-start/d' test-output-marks - sed -i '/test-marks-end/,$$d' test-output-marks - - OUTDIR="$$(pwd)/test-fake-ship-output" - mkdir -p $$OUTDIR - cp test-output-* $$OUTDIR - - set +x - - hdr () { - echo =====$$(sed 's/./=/g' <<< "$$1")===== - echo ==== $$1 ==== - echo =====$$(sed 's/./=/g' <<< "$$1")===== - } - - for f in $$(find "$$OUTDIR" -type f); do - hdr "$$(basename $$f)" - cat "$$f" - done - - fail=0 - - for f in $$(find "$$OUTDIR" -type f); do - if egrep "((FAILED|CRASHED)|warn:) " $$f >/dev/null; then - if [[ $$fail -eq 0 ]]; then - hdr "Test Failures" - fi - - echo "ERROR Test failure in $$(basename $$f)" - - ((fail++)) - fi - done - - if [[ $$fail -eq 0 ]]; then - hdr "Success" - fi - - zip -q -r $@ $$OUTDIR - - exit "$$fail" - """, - tools = [":urbit"], - visibility = ["//visibility:public"], -)