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 / net / tcp / tcpd.om
| 1 | note tcpd.om: guest driver for tcp.om (ledger L232). Compose: cat tcp.om tcpd.om | omc. |
| 2 | note Runs inside the QEMU guest. It brings eth0 up with GRO disabled through |
| 3 | note rtnetlink (so every frame it sees is one wire frame), opens an AF_PACKET |
| 4 | note socket on eth0 and runs one TCP task. The guest kernel has no IPv4 address, |
| 5 | note so only this stack answers for its address. |
| 6 | note Arguments: MODE PORT IP FILE LIMIT (IP and FILE may be -). |
| 7 | note echo PORT - - LIMIT passive: echo every byte until the peer closes |
| 8 | note sink PORT - FILE LIMIT passive: write the received stream to FILE |
| 9 | note send PORT IP FILE LIMIT active: connect to IP:PORT, send FILE, close |
| 10 | note Exit 0 only when the connection was established, closed without error and |
| 11 | note the task finished; it prints TCPD_PASS or TCPD_FAIL with counters. |
| 12 | global Dfd ulong 4 |
| 13 | global Dcnt ulong 4 |
| 14 | global Dcfg ulong 4 |
| 15 | global Darg uchar 256 |
| 16 | global Dnl uchar 256 |
| 17 | global Dsa uchar 32 |
| 18 | global Dbuf uchar 64 |
| 19 | global Dnum uchar 32 |
| 20 | text Pifx "/sys/class/net/eth0/ifindex" |
| 21 | text Paddr "/sys/class/net/eth0/address" |
| 22 | text Mready "TCPD_READY\n" |
| 23 | text Mpass "TCPD_PASS" |
| 24 | text Mfail "TCPD_FAIL" |
| 25 | text Mmode " mode=" |
| 26 | text Mbytes " bytes=" |
| 27 | text Merr " err=" |
| 28 | text Mest " est=" |
| 29 | text Mst " state=" |
| 30 | text Mwhy " why=" |
| 31 | text Mtst "TCPD_TST" |
| 32 | text Msp " " |
| 33 | text Mnl "\n" |
| 34 | fn tlinkout n ulong to void |
| 35 | k = send int get Dfd 0 Ttx 0 n 0 |
| 36 | if ne k long n |
| 37 | tstat 30 |
| 38 | end |
| 39 | end |
| 40 | fn dputs which ulong to void |
| 41 | if eq which 0 |
| 42 | k = write 1 Mready 0 len Mready |
| 43 | end |
| 44 | if eq which 1 |
| 45 | k = write 1 Mpass 0 len Mpass |
| 46 | end |
| 47 | if eq which 2 |
| 48 | k = write 1 Mfail 0 len Mfail |
| 49 | end |
| 50 | if eq which 3 |
| 51 | k = write 1 Mmode 0 len Mmode |
| 52 | end |
| 53 | if eq which 4 |
| 54 | k = write 1 Mbytes 0 len Mbytes |
| 55 | end |
| 56 | if eq which 5 |
| 57 | k = write 1 Merr 0 len Merr |
| 58 | end |
| 59 | if eq which 6 |
| 60 | k = write 1 Mest 0 len Mest |
| 61 | end |
| 62 | if eq which 7 |
| 63 | k = write 1 Mst 0 len Mst |
| 64 | end |
| 65 | if eq which 8 |
| 66 | k = write 1 Mwhy 0 len Mwhy |
| 67 | end |
| 68 | if eq which 9 |
| 69 | k = write 1 Mtst 0 len Mtst |
| 70 | end |
| 71 | if eq which 10 |
| 72 | k = write 1 Msp 0 len Msp |
| 73 | end |
| 74 | if eq which 11 |
| 75 | k = write 1 Mnl 0 len Mnl |
| 76 | end |
| 77 | end |
| 78 | fn dnumout v ulong to void |
| 79 | i = ulong 32 |
| 80 | while true bound 21 |
| 81 | i = sub i 1 |
| 82 | put Dnum i uchar add 48 rem v 10 |
| 83 | v = div v 10 |
| 84 | if eq v 0 |
| 85 | break |
| 86 | end |
| 87 | end |
| 88 | k = write 1 Dnum i sub 32 i |
| 89 | end |
| 90 | note Copy argument i into Darg; returns its length (0 when absent). |
| 91 | fn darg i ulong to ulong |
| 92 | if ge long i argcnt |
| 93 | ret 0 |
| 94 | end |
| 95 | k = argread i Darg 0 255 |
| 96 | if lt k 0 |
| 97 | exit 2 |
| 98 | end |
| 99 | ret ulong k |
| 100 | end |
| 101 | fn ddec n ulong to ulong |
| 102 | v = ulong 0 |
| 103 | if eq n 0 |
| 104 | exit 2 |
| 105 | end |
| 106 | for i ulong n bound 20 |
| 107 | c = ulong get Darg i |
| 108 | if or lt c 48 gt c 57 |
| 109 | exit 2 |
| 110 | end |
| 111 | v = add mul v 10 sub c 48 |
| 112 | end |
| 113 | ret v |
| 114 | end |
| 115 | fn dip n ulong to ulong |
| 116 | v = ulong 0 |
| 117 | o = ulong 0 |
| 118 | d = ulong 0 |
| 119 | for i ulong n bound 16 |
| 120 | c = ulong get Darg i |
| 121 | if eq c 46 |
| 122 | if or eq d 0 gt o 255 |
| 123 | exit 2 |
| 124 | end |
| 125 | v = add shl v 8 o |
| 126 | o = ulong 0 |
| 127 | d = ulong 0 |
| 128 | continue |
| 129 | end |
| 130 | if or lt c 48 gt c 57 |
| 131 | exit 2 |
| 132 | end |
| 133 | o = add mul o 10 sub c 48 |
| 134 | d = add d 1 |
| 135 | end |
| 136 | if or eq d 0 gt o 255 |
| 137 | exit 2 |
| 138 | end |
| 139 | ret add shl v 8 o |
| 140 | end |
| 141 | fn dhex c ulong to ulong |
| 142 | if and ge c 48 le c 57 |
| 143 | ret sub c 48 |
| 144 | end |
| 145 | if and ge c 97 le c 102 |
| 146 | ret sub c 87 |
| 147 | end |
| 148 | exit 3 |
| 149 | end |
| 150 | note Read the interface index and MAC address from sysfs. |
| 151 | fn dsysfs to ulong |
| 152 | f = fopen Pifx 0 len Pifx 0 0 |
| 153 | if lt f 0 |
| 154 | exit 3 |
| 155 | end |
| 156 | k = read int f Dbuf 0 63 |
| 157 | c = fclose int f |
| 158 | if le k 0 |
| 159 | exit 3 |
| 160 | end |
| 161 | x = ulong 0 |
| 162 | for i ulong ulong k bound 64 |
| 163 | y = ulong get Dbuf i |
| 164 | if and ge y 48 le y 57 |
| 165 | x = add mul x 10 sub y 48 |
| 166 | end |
| 167 | end |
| 168 | f = fopen Paddr 0 len Paddr 0 0 |
| 169 | if lt f 0 |
| 170 | exit 3 |
| 171 | end |
| 172 | k = read int f Dbuf 0 63 |
| 173 | c = fclose int f |
| 174 | if lt k 17 |
| 175 | exit 3 |
| 176 | end |
| 177 | for j ulong 6 |
| 178 | h = dhex ulong get Dbuf mul j 3 |
| 179 | l = dhex ulong get Dbuf add mul j 3 1 |
| 180 | put Tmac j uchar add shl h 4 l |
| 181 | end |
| 182 | ret x |
| 183 | end |
| 184 | fn dle p ulong v ulong n ulong to void |
| 185 | for i ulong n bound 8 |
| 186 | put Dnl add p i uchar and shr v mul i 8 255 |
| 187 | end |
| 188 | end |
| 189 | note RTM_NEWLINK: IFF_UP, IFLA_GRO_MAX_SIZE 1, IFLA_GRO_IPV4_MAX_SIZE 1 (no merging). |
| 190 | fn dlinkup ifx ulong to void |
| 191 | s = socknew 16 3 0 |
| 192 | if lt s 0 |
| 193 | exit 4 |
| 194 | end |
| 195 | dle 0 48 4 |
| 196 | dle 4 16 2 |
| 197 | dle 6 5 2 |
| 198 | dle 8 1 4 |
| 199 | dle 12 0 4 |
| 200 | dle 16 0 4 |
| 201 | dle 20 ifx 4 |
| 202 | dle 24 1 4 |
| 203 | dle 28 1 4 |
| 204 | dle 32 8 2 |
| 205 | dle 34 58 2 |
| 206 | dle 36 1 4 |
| 207 | dle 40 8 2 |
| 208 | dle 42 64 2 |
| 209 | dle 44 1 4 |
| 210 | k = send int s Dnl 0 48 0 |
| 211 | if ne k 48 |
| 212 | exit 4 |
| 213 | end |
| 214 | k = recv int s Dnl 0 256 0 |
| 215 | if lt k 20 |
| 216 | exit 4 |
| 217 | end |
| 218 | if ne get Dnl 4 2 |
| 219 | exit 4 |
| 220 | end |
| 221 | for i ulong 4 |
| 222 | if ne get Dnl add 16 i 0 |
| 223 | exit 5 |
| 224 | end |
| 225 | end |
| 226 | c = fclose int s |
| 227 | end |
| 228 | fn dpacket ifx ulong to void |
| 229 | s = socknew 17 3 768 |
| 230 | if lt s 0 |
| 231 | exit 6 |
| 232 | end |
| 233 | put Dsa 0 17 |
| 234 | put Dsa 3 3 |
| 235 | for i ulong 4 |
| 236 | put Dsa add 4 i uchar and shr ifx mul i 8 255 |
| 237 | end |
| 238 | if ne bind int s Dsa 0 20 0 |
| 239 | exit 6 |
| 240 | end |
| 241 | put Dfd 0 ulong s |
| 242 | end |
| 243 | fn dnow to ulong |
| 244 | t = monons |
| 245 | if lt t 0 |
| 246 | exit 7 |
| 247 | end |
| 248 | ret div ulong t 1000000 |
| 249 | end |
| 250 | fn dpump to void |
| 251 | for i ulong 256 |
| 252 | k = recv int get Dfd 0 Trx 0 2048 64 |
| 253 | if le k 0 |
| 254 | ret |
| 255 | end |
| 256 | if ne trmac 6 tmymac |
| 257 | tinput ulong k |
| 258 | end |
| 259 | end |
| 260 | end |
| 261 | fn dwrall n ulong to void |
| 262 | o = ulong 0 |
| 263 | while lt o n bound 65536 |
| 264 | k = write int get Dfd 1 Tapp o sub n o |
| 265 | if le k 0 |
| 266 | exit 8 |
| 267 | end |
| 268 | o = add o ulong k |
| 269 | end |
| 270 | end |
| 271 | note Mode steps. Dcnt: 0 bytes moved, 1 file at end, 2 close requested. |
| 272 | fn decho to void |
| 273 | c = umin get Tcb kRCN sub 65535 get Tcb kSCN |
| 274 | if gt c 0 |
| 275 | n = tread c |
| 276 | m = twrite n |
| 277 | if ne m n |
| 278 | exit 10 |
| 279 | end |
| 280 | put Dcnt 0 add get Dcnt 0 n |
| 281 | end |
| 282 | if eq get Tcb kST 7 |
| 283 | if eq get Tcb kRCN 0 |
| 284 | if eq get Dcnt 2 0 |
| 285 | put Dcnt 2 1 |
| 286 | tclose |
| 287 | end |
| 288 | end |
| 289 | end |
| 290 | end |
| 291 | fn dsink to void |
| 292 | n = tread 65536 |
| 293 | if gt n 0 |
| 294 | dwrall n |
| 295 | put Dcnt 0 add get Dcnt 0 n |
| 296 | end |
| 297 | if eq get Tcb kST 7 |
| 298 | if eq get Tcb kRCN 0 |
| 299 | if eq get Dcnt 2 0 |
| 300 | put Dcnt 2 1 |
| 301 | tclose |
| 302 | end |
| 303 | end |
| 304 | end |
| 305 | end |
| 306 | fn dsend to void |
| 307 | n = tread 65536 |
| 308 | s = get Tcb kST |
| 309 | if eq get Dcnt 1 0 |
| 310 | if or eq s 2 or eq s 3 eq s 4 |
| 311 | f = sub 65535 get Tcb kSCN |
| 312 | if gt f 0 |
| 313 | k = read int get Dfd 1 Tapp 0 f |
| 314 | if lt k 0 |
| 315 | exit 8 |
| 316 | end |
| 317 | if eq k 0 |
| 318 | put Dcnt 1 1 |
| 319 | else |
| 320 | m = twrite ulong k |
| 321 | if ne m ulong k |
| 322 | exit 10 |
| 323 | end |
| 324 | put Dcnt 0 add get Dcnt 0 m |
| 325 | end |
| 326 | end |
| 327 | end |
| 328 | end |
| 329 | if ne get Dcnt 1 0 |
| 330 | if eq get Dcnt 2 0 |
| 331 | if eq s 4 |
| 332 | put Dcnt 2 1 |
| 333 | tclose |
| 334 | end |
| 335 | end |
| 336 | end |
| 337 | end |
| 338 | fn dreport ok bool mode ulong why ulong to void |
| 339 | if ok |
| 340 | dputs 1 |
| 341 | else |
| 342 | dputs 2 |
| 343 | end |
| 344 | dputs 3 |
| 345 | dnumout mode |
| 346 | dputs 4 |
| 347 | dnumout get Dcnt 0 |
| 348 | dputs 5 |
| 349 | dnumout get Tcb kERR |
| 350 | dputs 6 |
| 351 | dnumout get Tcb kEST |
| 352 | dputs 7 |
| 353 | dnumout get Tcb kST |
| 354 | dputs 8 |
| 355 | dnumout why |
| 356 | dputs 11 |
| 357 | dputs 9 |
| 358 | for i ulong 32 |
| 359 | dputs 10 |
| 360 | dnumout get Tst i |
| 361 | end |
| 362 | dputs 11 |
| 363 | end |
| 364 | note Dcfg: 0 mode letter, 1 port, 2 peer IPv4, 3 limit seconds. |
| 365 | fn dargs to void |
| 366 | n = darg 1 |
| 367 | if eq n 0 |
| 368 | exit 2 |
| 369 | end |
| 370 | put Dcfg 0 ulong get Darg 0 |
| 371 | put Dcfg 1 ddec darg 2 |
| 372 | n = darg 3 |
| 373 | if ne get Darg 0 45 |
| 374 | put Dcfg 2 dip n |
| 375 | end |
| 376 | n = darg 4 |
| 377 | if ne get Darg 0 45 |
| 378 | fl = int 0 |
| 379 | if eq get Dcfg 0 114 |
| 380 | fl = int 577 |
| 381 | end |
| 382 | f = fopen Darg 0 n fl 420 |
| 383 | if lt f 0 |
| 384 | exit 3 |
| 385 | end |
| 386 | put Dfd 1 ulong f |
| 387 | end |
| 388 | put Dcfg 3 ddec darg 5 |
| 389 | end |
| 390 | fn dseed to void |
| 391 | k = random Dbuf 0 8 0 |
| 392 | if ne k 8 |
| 393 | exit 7 |
| 394 | end |
| 395 | for i ulong 8 |
| 396 | put Tcf 5 add shl get Tcf 5 8 ulong get Dbuf i |
| 397 | end |
| 398 | end |
| 399 | fn dstep mode ulong to void |
| 400 | if eq mode 101 |
| 401 | decho |
| 402 | end |
| 403 | if eq mode 114 |
| 404 | dsink |
| 405 | end |
| 406 | if eq mode 115 |
| 407 | dsend |
| 408 | end |
| 409 | end |
| 410 | note 0 running, 1 pass, 2 fail. |
| 411 | fn ddone mode ulong now ulong dl ulong to ulong |
| 412 | if eq get Tcb kST 0 |
| 413 | if ne get Tcb kEST 0 |
| 414 | ok = and eq get Tcb kERR 0 ne get Dcnt 2 0 |
| 415 | dreport ok mode 0 |
| 416 | if ok |
| 417 | ret 1 |
| 418 | end |
| 419 | ret 2 |
| 420 | end |
| 421 | if ne get Tcb kERR 0 |
| 422 | dreport false mode 1 |
| 423 | ret 2 |
| 424 | end |
| 425 | end |
| 426 | if gt now dl |
| 427 | dreport false mode 2 |
| 428 | ret 2 |
| 429 | end |
| 430 | ret 0 |
| 431 | end |
| 432 | fn dloop to int |
| 433 | mode = get Dcfg 0 |
| 434 | t0 = dnow |
| 435 | put Tcf 0 t0 |
| 436 | if eq mode 115 |
| 437 | tconnect get Dcfg 2 get Dcfg 1 add 40000 and trnd 16383 |
| 438 | else |
| 439 | tlisten get Dcfg 1 |
| 440 | end |
| 441 | dputs 0 |
| 442 | dl = add t0 mul get Dcfg 3 1000 |
| 443 | while true bound 100000000 |
| 444 | r = waitio int get Dfd 0 1 1000000 |
| 445 | now = dnow |
| 446 | put Tcf 0 now |
| 447 | dpump |
| 448 | tstep now |
| 449 | dstep mode |
| 450 | tstep now |
| 451 | v = ddone mode now dl |
| 452 | if eq v 1 |
| 453 | ret 0 |
| 454 | end |
| 455 | if eq v 2 |
| 456 | ret 1 |
| 457 | end |
| 458 | end |
| 459 | ret 1 |
| 460 | end |
| 461 | fn main to int |
| 462 | dargs |
| 463 | ifx = dsysfs |
| 464 | put Tcf 1 167772687 |
| 465 | put Tcf 2 167772674 |
| 466 | put Tcf 3 4294967040 |
| 467 | dseed |
| 468 | tinit |
| 469 | dlinkup ifx |
| 470 | dpacket ifx |
| 471 | ret dloop |
| 472 | end |