aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / x86 / courier_udp_x86.c
| 1 | /* courier_udp_x86.c -- Allen's courier over Ethernet; see courier_udp.h. |
| 2 | * Built with -DCK_COURIER_HOST for the host test, where the frame device is |
| 3 | * a UDP socket wrapped in Ethernet headers by the same net library. */ |
| 4 | #ifdef CK_COURIER_HOST |
| 5 | #include <stdio.h> |
| 6 | #include <string.h> |
| 7 | #define say_log(...) printf(__VA_ARGS__) |
| 8 | #else |
| 9 | #include "../core/ck_internal.h" |
| 10 | #define say_log(...) ck_printf(__VA_ARGS__) |
| 11 | #endif |
| 12 | #include "aienos_net.h" |
| 13 | #include "courier_udp.h" |
| 14 | |
| 15 | #define HEAD "ALLEN-COURIER v=1 run=" |
| 16 | #define LINES 8u |
| 17 | |
| 18 | static const ck_netio *io; |
| 19 | static uint8_t mac[6], ip[4], peer[4]; |
| 20 | static uint16_t port; |
| 21 | static uint64_t hz, run; |
| 22 | static uint16_t ip_id; |
| 23 | |
| 24 | static uint64_t hi_at, hello_at; /* clock of the last HI heard, the last HELLO sent */ |
| 25 | static int hi, hello_sent; /* a HI was ever heard; a HELLO was ever sent */ |
| 26 | |
| 27 | static char job[CK_COURIER_JOB_MAX]; /* the job out, whole */ |
| 28 | static unsigned job_n, job_parts; |
| 29 | static uint32_t job_seq; |
| 30 | static uint64_t job_sent; /* clock of the last send of the whole set */ |
| 31 | static int job_out; /* a job is out (sent, not yet heard by the far side) */ |
| 32 | static int job_acked; |
| 33 | |
| 34 | static char lines[LINES][CK_COURIER_LINE_MAX]; /* LINE bodies waiting for take(), in order */ |
| 35 | static unsigned line_head, line_count; |
| 36 | static uint32_t line_seq; /* the job whose answer lines are being taken */ |
| 37 | static unsigned line_part; /* the last part taken for it (0: none yet) */ |
| 38 | |
| 39 | static unsigned st_in, st_out, st_lines, st_resends, st_dropped; |
| 40 | static uint8_t frame[1600]; |
| 41 | |
| 42 | static void hex16(char *o, uint64_t v) |
| 43 | { |
| 44 | for (int i = 15; i >= 0; i--, v >>= 4) |
| 45 | o[i] = "0123456789abcdef"[v & 15]; |
| 46 | } |
| 47 | static unsigned dec(char *o, uint32_t v) |
| 48 | { |
| 49 | char t[10]; |
| 50 | unsigned n = 0, k = 0; |
| 51 | do |
| 52 | t[n++] = (char)('0' + v % 10); |
| 53 | while ((v /= 10) && n < sizeof t); |
| 54 | while (n) |
| 55 | o[k++] = t[--n]; |
| 56 | return k; |
| 57 | } |
| 58 | static unsigned cat(char *o, const char *s) |
| 59 | { |
| 60 | unsigned n = 0; |
| 61 | while (*s) |
| 62 | o[n++] = *s++; |
| 63 | return n; |
| 64 | } |
| 65 | |
| 66 | /* One datagram out: head + body in a broadcast frame. 0, or -1. */ |
| 67 | static int emit(const char *kind, uint32_t seq, unsigned part, unsigned total, unsigned bytes, const char *body, |
| 68 | unsigned body_n) |
| 69 | { |
| 70 | static uint8_t data[128 + CK_COURIER_PART], udp[8 + sizeof data], ipb[20 + sizeof udp]; |
| 71 | char *h = (char *)data; |
| 72 | unsigned n = cat(h, HEAD); |
| 73 | hex16(h + n, run), n += 16; |
| 74 | n += cat(h + n, " kind="); |
| 75 | n += cat(h + n, kind); |
| 76 | n += cat(h + n, " seq="); |
| 77 | n += dec(h + n, seq); |
| 78 | n += cat(h + n, " part="); |
| 79 | n += dec(h + n, part); |
| 80 | h[n++] = '/'; |
| 81 | n += dec(h + n, total); |
| 82 | n += cat(h + n, " bytes="); |
| 83 | n += dec(h + n, bytes); |
| 84 | h[n++] = '\n'; |
| 85 | if (body_n > CK_COURIER_PART) |
| 86 | return -1; |
| 87 | memcpy(data + n, body, body_n); |
| 88 | n += body_n; |
| 89 | const net_ipv4 src = {{ip[0], ip[1], ip[2], ip[3]}}, dst = {{peer[0], peer[1], peer[2], peer[3]}}; |
| 90 | net_udp_header uh = {port, port}; |
| 91 | size_t uw = 0, iw = 0, ew = 0; |
| 92 | if (net_udp_build(&uh, data, n, src, dst, udp, sizeof udp, &uw) != NET_OK) |
| 93 | return -1; |
| 94 | net_ipv4_header ih; |
| 95 | ih.dscp_ecn = 0; |
| 96 | ih.identification = ip_id++; |
| 97 | ih.ttl = 64; |
| 98 | ih.protocol = NET_IPPROTO_UDP; |
| 99 | ih.source = src; |
| 100 | ih.destination = dst; |
| 101 | if (net_ipv4_build(&ih, udp, uw, ipb, sizeof ipb, &iw) != NET_OK) |
| 102 | return -1; |
| 103 | net_eth_frame e; |
| 104 | memset(e.destination.b, 0xff, 6); |
| 105 | memcpy(e.source.b, mac, 6); |
| 106 | e.ethertype = NET_ETHERTYPE_IPV4; |
| 107 | e.payload = ipb; |
| 108 | e.payload_len = iw; |
| 109 | if (net_eth_build(&e, frame, sizeof frame, &ew) != NET_OK) |
| 110 | return -1; |
| 111 | if (io->send(io->ctx, frame, ew)) |
| 112 | return -1; |
| 113 | st_out++; |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | static void send_job(void) |
| 118 | { |
| 119 | for (unsigned p = 0; p < job_parts; p++) { |
| 120 | unsigned off = p * CK_COURIER_PART, n = job_n - off < CK_COURIER_PART ? job_n - off : CK_COURIER_PART; |
| 121 | emit("JOB", job_seq, p + 1, job_parts, job_n, job + off, n); |
| 122 | } |
| 123 | job_sent = io->now(io->ctx); |
| 124 | job_out = 1; |
| 125 | } |
| 126 | |
| 127 | static int ready(void) |
| 128 | { |
| 129 | if (!hi) |
| 130 | return 0; |
| 131 | return io->now(io->ctx) - hi_at < 30 * hz; |
| 132 | } |
| 133 | |
| 134 | /* This courier is unencrypted. Secret-bearing key tests must never be queued. */ |
| 135 | static void clear_job(void) |
| 136 | { |
| 137 | volatile char *p = job; |
| 138 | for (unsigned i = 0; i < sizeof job; i++) p[i] = 0; |
| 139 | job_n = job_parts = 0; |
| 140 | job_out = 0; |
| 141 | } |
| 142 | |
| 143 | static int send(const char *line, unsigned n) |
| 144 | { |
| 145 | if (!line || !n || n > sizeof job || !io->link(io->ctx)) |
| 146 | return -1; |
| 147 | if (n >= 7 && !memcmp(line, "KEYTEST", 7)) |
| 148 | return -1; |
| 149 | clear_job(); |
| 150 | memcpy(job, line, n); |
| 151 | for (unsigned i = 0; i < n; i++) |
| 152 | if (job[i] == '\n') |
| 153 | job[i] = ' '; |
| 154 | job_n = n; |
| 155 | job_parts = (n + CK_COURIER_PART - 1) / CK_COURIER_PART; |
| 156 | job_seq++; |
| 157 | job_acked = 0; |
| 158 | send_job(); |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | /* The decimal after key= in the head; 0 when absent. */ |
| 163 | static uint32_t field(const char *h, unsigned n, const char *key) |
| 164 | { |
| 165 | unsigned k = 0; |
| 166 | while (key[k]) |
| 167 | k++; |
| 168 | for (unsigned i = 0; i + k < n; i++) { |
| 169 | if (memcmp(h + i, key, k)) |
| 170 | continue; |
| 171 | uint32_t v = 0; |
| 172 | for (i += k; i < n && h[i] >= '0' && h[i] <= '9'; i++) |
| 173 | v = v * 10 + (uint32_t)(h[i] - '0'); |
| 174 | return v; |
| 175 | } |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | /* 1 when the head's kind= field is exactly `kind` (the head is HEAD, 16 hex digits, then " kind=K ..."). */ |
| 180 | static int kind_is(const char *h, unsigned hn, const char *kind) |
| 181 | { |
| 182 | unsigned at = sizeof HEAD - 1 + 16 + 1 + 5, k = 0; /* past the space and "kind=" */ |
| 183 | if (hn < at) |
| 184 | return 0; |
| 185 | while (kind[k]) { |
| 186 | if (at + k >= hn || h[at + k] != kind[k]) |
| 187 | return 0; |
| 188 | k++; |
| 189 | } |
| 190 | return at + k == hn || h[at + k] == ' '; |
| 191 | } |
| 192 | |
| 193 | /* One datagram in, already ours by run id: the head's kind decides. */ |
| 194 | static void on_datagram(const char *h, unsigned hn, const char *body, unsigned bn) |
| 195 | { |
| 196 | st_in++; |
| 197 | uint32_t seq = field(h, hn, "seq="); |
| 198 | unsigned part = field(h, hn, "part="); |
| 199 | if (kind_is(h, hn, "HI")) { |
| 200 | hi = 1; |
| 201 | hi_at = io->now(io->ctx); |
| 202 | return; |
| 203 | } |
| 204 | if (kind_is(h, hn, "ACK")) { |
| 205 | if (seq == job_seq) { |
| 206 | job_acked = 1; |
| 207 | clear_job(); |
| 208 | } |
| 209 | return; |
| 210 | } |
| 211 | if (!kind_is(h, hn, "LINE")) { |
| 212 | st_dropped++; |
| 213 | return; |
| 214 | } |
| 215 | if (seq == job_seq) { |
| 216 | job_acked = 1; /* an answer line says the job arrived */ |
| 217 | clear_job(); |
| 218 | } |
| 219 | if (seq != line_seq) { |
| 220 | line_seq = seq; |
| 221 | line_part = 0; |
| 222 | } |
| 223 | if (part <= line_part) { /* taken already: the far side missed our GOT */ |
| 224 | emit("GOT", seq, part, 0, 0, "", 0); |
| 225 | return; |
| 226 | } |
| 227 | if (part != line_part + 1 || line_count == LINES) { /* out of turn, or no room: it comes again */ |
| 228 | st_dropped++; |
| 229 | return; |
| 230 | } |
| 231 | unsigned slot = (line_head + line_count) % LINES; |
| 232 | unsigned n = bn < CK_COURIER_LINE_MAX - 1 ? bn : CK_COURIER_LINE_MAX - 1; |
| 233 | memcpy(lines[slot], body, n); |
| 234 | lines[slot][n] = 0; |
| 235 | line_count++; |
| 236 | line_part = part; |
| 237 | st_lines++; |
| 238 | emit("GOT", seq, part, 0, 0, "", 0); |
| 239 | } |
| 240 | |
| 241 | /* Reads the frames waiting and keeps the datagrams that are ours. */ |
| 242 | static void pump(void) |
| 243 | { |
| 244 | uint64_t now = io->now(io->ctx); |
| 245 | for (unsigned k = 0; k < 64; k++) { |
| 246 | size_t n = io->recv(io->ctx, frame, sizeof frame); |
| 247 | if (!n) |
| 248 | break; |
| 249 | net_eth_frame e; |
| 250 | if (net_eth_parse(frame, n, &e) != NET_OK || e.ethertype != NET_ETHERTYPE_IPV4) |
| 251 | continue; |
| 252 | int bcast = 1, ours = 1; |
| 253 | for (int i = 0; i < 6; i++) { |
| 254 | bcast &= e.destination.b[i] == 0xff; |
| 255 | ours &= e.destination.b[i] == mac[i]; |
| 256 | } |
| 257 | if (!bcast && !ours) |
| 258 | continue; |
| 259 | net_ipv4_header ih; |
| 260 | const uint8_t *ipp; |
| 261 | size_t ipn; |
| 262 | if (net_ipv4_parse(e.payload, e.payload_len, &ih, &ipp, &ipn) != NET_OK || ih.protocol != NET_IPPROTO_UDP) |
| 263 | continue; |
| 264 | net_udp_header uh; |
| 265 | const uint8_t *d; |
| 266 | size_t dn; |
| 267 | if (net_udp_parse(ipp, ipn, ih.source, ih.destination, &uh, &d, &dn) != NET_OK || uh.destination_port != port) |
| 268 | continue; |
| 269 | /* the head: ours when the run id matches; one line, then the body */ |
| 270 | char want[sizeof HEAD - 1 + 16]; |
| 271 | memcpy(want, HEAD, sizeof HEAD - 1); |
| 272 | hex16(want + sizeof HEAD - 1, run); |
| 273 | if (dn < sizeof want || memcmp(d, want, sizeof want)) |
| 274 | continue; |
| 275 | unsigned hn = 0; |
| 276 | while (hn < dn && d[hn] != '\n') |
| 277 | hn++; |
| 278 | if (hn == dn || hn > 160) |
| 279 | continue; |
| 280 | on_datagram((const char *)d, hn, (const char *)d + hn + 1, (unsigned)(dn - hn - 1)); |
| 281 | } |
| 282 | /* the timers: HELLO until HI then every 10 s; the job again every 2 s until it is heard */ |
| 283 | if (io->link(io->ctx)) { |
| 284 | uint64_t hello_every = ready() ? 10 * hz : 2 * hz; |
| 285 | if (!hello_sent || now - hello_at >= hello_every) { |
| 286 | emit("HELLO", 0, 0, 0, 0, "", 0); |
| 287 | hello_at = now; |
| 288 | hello_sent = 1; |
| 289 | } |
| 290 | if (job_out && !job_acked && now - job_sent >= 2 * hz) { |
| 291 | st_resends++; |
| 292 | send_job(); |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | static int take(char *out, unsigned cap) |
| 298 | { |
| 299 | pump(); |
| 300 | if (!line_count || !out || !cap) |
| 301 | return 0; |
| 302 | unsigned n = 0; |
| 303 | const char *s = lines[line_head]; |
| 304 | while (s[n] && n + 1 < cap) |
| 305 | out[n] = s[n], n++; |
| 306 | out[n] = 0; |
| 307 | line_head = (line_head + 1) % LINES; |
| 308 | line_count--; |
| 309 | return 1; |
| 310 | } |
| 311 | |
| 312 | static const ck_courier courier = {ready, send, take}; |
| 313 | |
| 314 | const ck_courier *ck_courier_udp_init(const ck_netio *dev, const uint8_t m[6], const uint8_t a[4], |
| 315 | const uint8_t p[4], uint16_t prt, uint64_t clock_hz) |
| 316 | { |
| 317 | io = dev; |
| 318 | memcpy(mac, m, 6); |
| 319 | memcpy(ip, a, 4); |
| 320 | memcpy(peer, p, 4); |
| 321 | port = prt; |
| 322 | hz = clock_hz ? clock_hz : 1; |
| 323 | run = io->now(io->ctx); |
| 324 | for (int i = 0; i < 6; i++) |
| 325 | run ^= (uint64_t)mac[i] << (8 * (i + 2)); |
| 326 | if (!run) |
| 327 | run = 1; |
| 328 | hi = 0; |
| 329 | hi_at = hello_at = job_sent = 0; |
| 330 | hello_sent = job_out = 0; |
| 331 | clear_job(); |
| 332 | job_acked = 0; |
| 333 | job_seq = 0; |
| 334 | line_head = line_count = 0; |
| 335 | line_seq = 0; |
| 336 | line_part = 0; |
| 337 | say_log("courier: Ethernet to the far side on UDP port %u (broadcast, source 0.0.0.0, run %08x%08x), calling\n", |
| 338 | (unsigned)port, (unsigned)(run >> 32), (unsigned)run); |
| 339 | pump(); |
| 340 | return &courier; |
| 341 | } |
| 342 | |
| 343 | void ck_courier_udp_stats(unsigned *in, unsigned *out, unsigned *ln, unsigned *rs, unsigned *dr) |
| 344 | { |
| 345 | if (in) |
| 346 | *in = st_in; |
| 347 | if (out) |
| 348 | *out = st_out; |
| 349 | if (ln) |
| 350 | *ln = st_lines; |
| 351 | if (rs) |
| 352 | *rs = st_resends; |
| 353 | if (dr) |
| 354 | *dr = st_dropped; |
| 355 | } |
| 356 | |
| 357 | uint64_t ck_courier_udp_run(void) { return run; } |