interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / net / tcp / tcpd.om

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