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 / tests / courier_udp_test.c
| 1 | /* courier_udp_test.c -- host test of Allen's Ethernet courier (x86/courier_udp_x86.c) |
| 2 | * against the real relay (crates/allen-courier --udp). |
| 3 | * |
| 4 | * The courier is the kernel's own code, built with -DCK_COURIER_HOST. Its frame |
| 5 | * device is a UDP socket: a frame the courier sends is unwrapped (Ethernet, IP, |
| 6 | * UDP, by the same net library the kernel uses) and its datagram sent to the |
| 7 | * relay's port on this machine; a datagram the relay sends back is wrapped into |
| 8 | * an Ethernet frame the courier reads. Optionally every Nth frame is lost in each |
| 9 | * direction (a seeded pseudo-random 1 in DROP_EVERY), to prove the resend and the |
| 10 | * "GOT" rules. |
| 11 | * |
| 12 | * usage: courier_udp_test RELAY_PORT DROP_EVERY JOB_FILE [MAX_SECONDS] |
| 13 | * Prints plain lines, then "COURIER_UDP: PASS ..." or "COURIER_UDP: FAIL <why>". */ |
| 14 | #include <arpa/inet.h> |
| 15 | #include <errno.h> |
| 16 | #include <fcntl.h> |
| 17 | #include <netinet/in.h> |
| 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
| 21 | #include <sys/socket.h> |
| 22 | #include <time.h> |
| 23 | #include <unistd.h> |
| 24 | |
| 25 | #include "../../../net/aienos_net.h" |
| 26 | #include "../courier_udp.h" |
| 27 | |
| 28 | typedef struct { |
| 29 | int fd; |
| 30 | struct sockaddr_in relay; |
| 31 | unsigned drop_every; |
| 32 | unsigned sent, got, lost_out, lost_in; |
| 33 | uint32_t rng; /* xorshift: the same losses every run */ |
| 34 | uint8_t mac[6]; |
| 35 | } dev; |
| 36 | |
| 37 | static uint64_t ms_now(void) |
| 38 | { |
| 39 | struct timespec t; |
| 40 | clock_gettime(CLOCK_MONOTONIC, &t); |
| 41 | return (uint64_t)t.tv_sec * 1000u + (uint64_t)t.tv_nsec / 1000000u; |
| 42 | } |
| 43 | |
| 44 | /* 1 about once in `every` calls (0: never). A strict every-Nth rule can line up with the |
| 45 | * resend cycle and starve one datagram forever, which no real wire does. */ |
| 46 | static int lose(dev *d) |
| 47 | { |
| 48 | if (!d->drop_every) |
| 49 | return 0; |
| 50 | d->rng ^= d->rng << 13, d->rng ^= d->rng >> 17, d->rng ^= d->rng << 5; |
| 51 | return d->rng % d->drop_every == 0; |
| 52 | } |
| 53 | |
| 54 | static int d_send(void *ctx, const uint8_t *frame, size_t n) |
| 55 | { |
| 56 | dev *d = ctx; |
| 57 | net_eth_frame e; |
| 58 | net_ipv4_header ih; |
| 59 | net_udp_header uh; |
| 60 | const uint8_t *ip, *data; |
| 61 | size_t ipn, dn; |
| 62 | if (net_eth_parse(frame, n, &e) != NET_OK || e.ethertype != NET_ETHERTYPE_IPV4) |
| 63 | return -1; |
| 64 | if (net_ipv4_parse(e.payload, e.payload_len, &ih, &ip, &ipn) != NET_OK || ih.protocol != NET_IPPROTO_UDP) |
| 65 | return -1; |
| 66 | if (net_udp_parse(ip, ipn, ih.source, ih.destination, &uh, &data, &dn) != NET_OK) |
| 67 | return -1; |
| 68 | if (uh.destination_port != CK_COURIER_PORT || uh.source_port != CK_COURIER_PORT) |
| 69 | return -1; |
| 70 | if (e.destination.b[0] != 0xff || ih.destination.b[0] != 255 || ih.source.b[0] != 0) |
| 71 | return -1; /* the lab shape: broadcast out, source address 0.0.0.0 */ |
| 72 | d->sent++; |
| 73 | if (lose(d)) { |
| 74 | d->lost_out++; |
| 75 | return 0; |
| 76 | } |
| 77 | return sendto(d->fd, data, dn, 0, (struct sockaddr *)&d->relay, sizeof d->relay) == (ssize_t)dn ? 0 : -1; |
| 78 | } |
| 79 | |
| 80 | static size_t d_recv(void *ctx, uint8_t *frame, size_t cap) |
| 81 | { |
| 82 | dev *d = ctx; |
| 83 | static uint8_t dg[2048], udp[2100], ipb[2200]; |
| 84 | ssize_t n = recvfrom(d->fd, dg, sizeof dg, 0, 0, 0); |
| 85 | if (n <= 0) |
| 86 | return 0; |
| 87 | d->got++; |
| 88 | if (lose(d)) { |
| 89 | d->lost_in++; |
| 90 | return 0; |
| 91 | } |
| 92 | const net_ipv4 src = {{192, 168, 1, 108}}, dst = {{255, 255, 255, 255}}; |
| 93 | net_udp_header uh = {CK_COURIER_PORT, CK_COURIER_PORT}; |
| 94 | size_t uw = 0, iw = 0, ew = 0; |
| 95 | if (net_udp_build(&uh, dg, (size_t)n, src, dst, udp, sizeof udp, &uw) != NET_OK) |
| 96 | return 0; |
| 97 | net_ipv4_header ih = {0}; |
| 98 | ih.ttl = 64; |
| 99 | ih.protocol = NET_IPPROTO_UDP; |
| 100 | ih.source = src; |
| 101 | ih.destination = dst; |
| 102 | if (net_ipv4_build(&ih, udp, uw, ipb, sizeof ipb, &iw) != NET_OK) |
| 103 | return 0; |
| 104 | net_eth_frame e; |
| 105 | memset(e.destination.b, 0xff, 6); |
| 106 | memset(e.source.b, 0x11, 6); |
| 107 | e.ethertype = NET_ETHERTYPE_IPV4; |
| 108 | e.payload = ipb; |
| 109 | e.payload_len = iw; |
| 110 | if (net_eth_build(&e, frame, cap, &ew) != NET_OK) |
| 111 | return 0; |
| 112 | return ew; |
| 113 | } |
| 114 | |
| 115 | static int d_link(void *ctx) |
| 116 | { |
| 117 | (void)ctx; |
| 118 | return 1; |
| 119 | } |
| 120 | static uint64_t d_now(void *ctx) |
| 121 | { |
| 122 | (void)ctx; |
| 123 | return ms_now(); |
| 124 | } |
| 125 | |
| 126 | static int fail(const char *why) |
| 127 | { |
| 128 | printf("COURIER_UDP: FAIL %s\n", why); |
| 129 | return 1; |
| 130 | } |
| 131 | |
| 132 | int main(int argc, char **argv) |
| 133 | { |
| 134 | if (argc < 4) |
| 135 | return fail("usage: courier_udp_test RELAY_PORT DROP_EVERY JOB_FILE [MAX_SECONDS]"); |
| 136 | dev d; |
| 137 | memset(&d, 0, sizeof d); |
| 138 | d.fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 139 | d.relay.sin_family = AF_INET; |
| 140 | d.relay.sin_port = htons((uint16_t)atoi(argv[1])); |
| 141 | inet_pton(AF_INET, "127.0.0.1", &d.relay.sin_addr); |
| 142 | d.drop_every = (unsigned)atoi(argv[2]); |
| 143 | d.rng = 0x2545f491u; |
| 144 | unsigned max_s = argc > 4 ? (unsigned)atoi(argv[4]) : 60; |
| 145 | fcntl(d.fd, F_SETFL, O_NONBLOCK); |
| 146 | static char job[CK_COURIER_JOB_MAX]; |
| 147 | FILE *f = fopen(argv[3], "r"); |
| 148 | if (!f) |
| 149 | return fail("no job file"); |
| 150 | size_t jn = fread(job, 1, sizeof job - 1, f); |
| 151 | fclose(f); |
| 152 | while (jn && (job[jn - 1] == '\n' || job[jn - 1] == '\r')) |
| 153 | jn--; |
| 154 | job[jn] = 0; |
| 155 | |
| 156 | const ck_netio io = {&d, d_send, d_recv, d_link, d_now}; |
| 157 | const uint8_t mac[6] = {0x00, 0x1f, 0xf3, 0x12, 0x34, 0x56}, none[4] = {0, 0, 0, 0}, peer[4] = {255, 255, 255, 255}; |
| 158 | const ck_courier *c = ck_courier_udp_init(&io, mac, none, peer, CK_COURIER_PORT, 1000); |
| 159 | if (!c) |
| 160 | return fail("no courier"); |
| 161 | |
| 162 | uint64_t t0 = ms_now(); |
| 163 | char line[CK_COURIER_LINE_MAX]; |
| 164 | while (!c->ready()) { |
| 165 | c->take(line, sizeof line); /* pumps */ |
| 166 | if (ms_now() - t0 > 10000) |
| 167 | return fail("no HI from the relay within 10 s"); |
| 168 | usleep(2000); |
| 169 | } |
| 170 | uint64_t t_ready = ms_now() - t0; |
| 171 | printf("courier test: the relay said hello after %llu ms\n", (unsigned long long)t_ready); |
| 172 | |
| 173 | if (c->send(job, jn)) |
| 174 | return fail("the courier would not take the job"); |
| 175 | uint64_t t1 = ms_now(), t_first = 0, t_whole = 0; |
| 176 | unsigned parts = 0, expect = 1, waits = 0; |
| 177 | char said[1024]; |
| 178 | said[0] = 0; |
| 179 | for (;;) { |
| 180 | if (ms_now() - t1 > max_s * 1000u) |
| 181 | return fail("the answer did not come whole in time"); |
| 182 | if (!c->take(line, sizeof line)) { |
| 183 | usleep(2000); |
| 184 | continue; |
| 185 | } |
| 186 | unsigned seq = 0, i = 0, n = 0; |
| 187 | if (sscanf(line, "WAIT %u", &seq) == 1) { |
| 188 | waits++; |
| 189 | continue; |
| 190 | } |
| 191 | int off = 0; |
| 192 | if (sscanf(line, "SAY %u %u/%u %n", &seq, &i, &n, &off) < 3 || off == 0) |
| 193 | return fail("a line that is neither SAY nor WAIT"); |
| 194 | if (i != expect) |
| 195 | return fail("answer parts out of order or repeated"); |
| 196 | expect++; |
| 197 | parts++; |
| 198 | if (!t_first) |
| 199 | t_first = ms_now() - t1; |
| 200 | if (strlen(said) + strlen(line + off) + 2 < sizeof said) { |
| 201 | if (said[0]) |
| 202 | strcat(said, " "); |
| 203 | strcat(said, line + off); |
| 204 | } |
| 205 | printf("courier test: part %u/%u: %s\n", i, n, line + off); |
| 206 | if (n && i == n) { |
| 207 | t_whole = ms_now() - t1; |
| 208 | break; |
| 209 | } |
| 210 | } |
| 211 | unsigned in, out, lines, resends, dropped; |
| 212 | ck_courier_udp_stats(&in, &out, &lines, &resends, &dropped); |
| 213 | printf("courier test: first part after %llu ms, whole after %llu ms, %u parts, %u waits; datagrams in %u out %u, " |
| 214 | "lines taken %u, job resends %u, dropped %u; lost out %u in %u\n", |
| 215 | (unsigned long long)t_first, (unsigned long long)t_whole, parts, waits, in, out, lines, resends, dropped, |
| 216 | d.lost_out, d.lost_in); |
| 217 | if (!said[0] || strstr(said, "lost my thread")) |
| 218 | return fail("the answer had no words (the model was not reached)"); |
| 219 | if (d.drop_every && !d.lost_out && !d.lost_in) |
| 220 | return fail("loss was asked for and none happened"); |
| 221 | printf("COURIER_UDP: PASS (%u parts, first %llu ms, whole %llu ms, drop every %u)\n", parts, |
| 222 | (unsigned long long)t_first, (unsigned long long)t_whole, d.drop_every); |
| 223 | return 0; |
| 224 | } |