interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / aienux / net / core / net.om

416 lines · 10020 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/net/core/net.om: Pure-Omega AIENOS M6-A network protocol layer and boot report framing.
3note Converted from aienux native/net/aienos_net.{h,c} and native/net/aienos_report.{h,c}.
4note Compose with: si/directives/om2.om aienux/net/core/net.om ...
5
6const NET_OK int 0
7const NET_ERR_TRUNCATED int 1
8const NET_ERR_INVALID int 2
9const NET_ERR_UNSUPPORTED int 3
10const NET_ERR_CAPACITY int 4
11
12const NET_ETHERTYPE_IPV4 ushort 0x0800
13const NET_ETHERTYPE_ARP ushort 0x0806
14const NET_ARP_REQUEST ushort 1
15const NET_ARP_REPLY ushort 2
16const NET_IPPROTO_ICMP uchar 1
17const NET_IPPROTO_UDP uchar 17
18
19const NET_REPORT_PORT ushort 47031
20const NET_REPORT_CHUNK ulong 1024
21const NET_REPORT_MAX ulong 65536
22
23struct NetEthHdr dst_hi ulong dst_lo ulong src_hi ulong src_lo ulong ethertype ushort pay_off ulong pay_len ulong end
24struct NetArp op ushort smac_hi ulong smac_lo ulong sip uint tmac_hi ulong tmac_lo ulong tip uint end
25struct NetIpv4 ihl uchar dscp uchar total_len ushort ident ushort flags_frag ushort ttl uchar proto uchar csum ushort src uint dst uint pay_off ulong pay_len ulong end
26struct NetUdp sport ushort dport ushort ulen ushort csum ushort pay_off ulong pay_len ulong end
27
28const NET_ARP_CAP ulong 32
29global net_arp_ip uint 32
30global net_arp_mac uchar 192
31global net_arp_exp ulong 32
32global net_arp_used uchar 32
33
34fn net_be16 buf view uchar * off ulong to ushort
35b0 = ushort get buf off
36b1 = ushort get buf add off 1
37ret or shl b0 8 b1
38end
39
40fn net_be32 buf view uchar * off ulong to uint
41b0 = shl uint get buf off 24
42b1 = shl uint get buf add off 1 16
43b2 = shl uint get buf add off 2 8
44b3 = uint get buf add off 3
45ret or or b0 b1 or b2 b3
46end
47
48fn net_put_be16 buf edit uchar * off ulong v ushort to void
49put buf off uchar and shr v 8 0xff
50put buf add off 1 uchar and v 0xff
51end
52
53fn net_put_be32 buf edit uchar * off ulong v uint to void
54put buf off uchar and shr v 24 0xff
55put buf add off 1 uchar and shr v 16 0xff
56put buf add off 2 uchar and shr v 8 0xff
57put buf add off 3 uchar and v 0xff
58end
59
60note RFC 1071 one's-complement checksum (64-bit accumulator, odd final byte as high octet)
61fn net_checksum buf view uchar * off ulong n ulong to ushort
62acc = ulong 0
63i = ulong 0
64while lt add i 1 n bound 65536
65w = ulong net_be16 buf add off i
66acc = add acc w
67i = add i 2
68end
69if lt i n
70acc = add acc shl ulong get buf add off i 8
71end
72while gt shr acc 16 0 bound 8
73acc = add and acc 0xffff shr acc 16
74end
75ret ushort xor and acc 0xffff 0xffff
76end
77
78note ---- Ethernet II ----
79
80fn net_eth_parse buf view uchar * n ulong hdr edit NetEthHdr to int
81if lt n 14
82ret NET_ERR_TRUNCATED
83end
84et = net_be16 buf 12
85if lt et 1536
86ret NET_ERR_UNSUPPORTED
87end
88dhi = ulong net_be32 buf 0
89dlo = ulong net_be16 buf 4
90shi = ulong net_be32 buf 6
91slo = ulong net_be16 buf 10
92put hdr dst_hi dhi
93put hdr dst_lo dlo
94put hdr src_hi shi
95put hdr src_lo slo
96put hdr ethertype et
97put hdr pay_off 14
98put hdr pay_len sub n 14
99ret NET_OK
100end
101
102fn net_eth_build macs view uchar 12 et ushort pay view uchar * buf edit uchar * to ulong
103need = add 14 len pay
104if gt need len buf
105ret 0
106end
107for i ulong 12
108put buf i get macs i
109end
110net_put_be16 buf 12 et
111for i ulong len pay bound 2048
112put buf add 14 i get pay i
113end
114ret need
115end
116
117note ---- ARP (Ethernet / IPv4) ----
118
119fn net_arp_parse buf view uchar * off ulong n ulong pkt edit NetArp to int
120if lt n 28
121ret NET_ERR_TRUNCATED
122end
123if ne net_be16 buf off 1
124ret NET_ERR_UNSUPPORTED
125end
126if ne net_be16 buf add off 2 NET_ETHERTYPE_IPV4
127ret NET_ERR_UNSUPPORTED
128end
129if or ne get buf add off 4 6 ne get buf add off 5 4
130ret NET_ERR_UNSUPPORTED
131end
132op = net_be16 buf add off 6
133if and ne op NET_ARP_REQUEST ne op NET_ARP_REPLY
134ret NET_ERR_UNSUPPORTED
135end
136put pkt op op
137put pkt smac_hi ulong net_be32 buf add off 8
138put pkt smac_lo ulong net_be16 buf add off 12
139put pkt sip net_be32 buf add off 14
140put pkt tmac_hi ulong net_be32 buf add off 18
141put pkt tmac_lo ulong net_be16 buf add off 22
142put pkt tip net_be32 buf add off 24
143ret NET_OK
144end
145
146fn net_arp_cache_reset to void
147for i ulong NET_ARP_CAP
148put net_arp_used i 0
149put net_arp_ip i 0
150put net_arp_exp i 0
151end
152end
153
154fn net_arp_cache_learn ip uint mac view uchar 6 now ulong ttl ulong to int
155exp = add now ttl
156slot = ulong NET_ARP_CAP
157for i ulong NET_ARP_CAP
158if and ne get net_arp_used i 0 eq get net_arp_ip i ip
159slot = i
160break
161end
162end
163if eq slot NET_ARP_CAP
164for i ulong NET_ARP_CAP
165if or eq get net_arp_used i 0 le get net_arp_exp i now
166slot = i
167break
168end
169end
170end
171if eq slot NET_ARP_CAP
172ret NET_ERR_CAPACITY
173end
174put net_arp_used slot 1
175put net_arp_ip slot ip
176put net_arp_exp slot exp
177base = mul slot 6
178for j ulong 6
179put net_arp_mac add base j get mac j
180end
181ret NET_OK
182end
183
184fn net_arp_cache_lookup ip uint now ulong mac_out edit uchar 6 to bool
185for i ulong NET_ARP_CAP
186if ne get net_arp_used i 0
187if and eq get net_arp_ip i ip gt get net_arp_exp i now
188base = mul i 6
189for j ulong 6
190put mac_out j get net_arp_mac add base j
191end
192ret true
193end
194end
195end
196ret false
197end
198
199note ---- IPv4 ----
200
201fn net_ipv4_parse buf view uchar * off ulong n ulong hdr edit NetIpv4 to int
202if lt n 20
203ret NET_ERR_TRUNCATED
204end
205vihl = get buf off
206ver = shr vihl 4
207ihl_w = and vihl 0x0f
208if ne ver 4
209ret NET_ERR_UNSUPPORTED
210end
211if lt ihl_w 5
212ret NET_ERR_INVALID
213end
214hlen = mul ulong ihl_w 4
215if gt hlen n
216ret NET_ERR_TRUNCATED
217end
218tot = ulong net_be16 buf add off 2
219if lt tot hlen
220ret NET_ERR_INVALID
221end
222if gt tot n
223ret NET_ERR_TRUNCATED
224end
225if ne net_checksum buf off hlen 0
226ret NET_ERR_INVALID
227end
228put hdr ihl uchar hlen
229put hdr dscp get buf add off 1
230put hdr total_len ushort tot
231put hdr ident net_be16 buf add off 4
232put hdr flags_frag net_be16 buf add off 6
233put hdr ttl get buf add off 8
234put hdr proto get buf add off 9
235put hdr csum net_be16 buf add off 10
236put hdr src net_be32 buf add off 12
237put hdr dst net_be32 buf add off 16
238put hdr pay_off add off hlen
239put hdr pay_len sub tot hlen
240ret NET_OK
241end
242
243note ip_meta: 0=src 1=dst 2=proto 3=ttl
244fn net_ipv4_build ip_meta view uint 4 pay view uchar * buf edit uchar * to ulong
245tot = add 20 len pay
246if gt tot len buf
247ret 0
248end
249put buf 0 0x45
250put buf 1 0
251net_put_be16 buf 2 ushort tot
252net_put_be16 buf 4 0
253net_put_be16 buf 6 0
254put buf 8 uchar get ip_meta 3
255put buf 9 uchar get ip_meta 2
256net_put_be16 buf 10 0
257net_put_be32 buf 12 get ip_meta 0
258net_put_be32 buf 16 get ip_meta 1
259cs = net_checksum buf 0 20
260net_put_be16 buf 10 cs
261for i ulong len pay bound 2048
262put buf add 20 i get pay i
263end
264ret tot
265end
266
267note ---- UDP ----
268
269fn net_udp_parse buf view uchar * off ulong n ulong hdr edit NetUdp to int
270if lt n 8
271ret NET_ERR_TRUNCATED
272end
273ul = ulong net_be16 buf add off 4
274if lt ul 8
275ret NET_ERR_INVALID
276end
277if gt ul n
278ret NET_ERR_TRUNCATED
279end
280put hdr sport net_be16 buf off
281put hdr dport net_be16 buf add off 2
282put hdr ulen ushort ul
283put hdr csum net_be16 buf add off 6
284put hdr pay_off add off 8
285put hdr pay_len sub ul 8
286ret NET_OK
287end
288
289fn net_udp_build sport ushort dport ushort pay view uchar * buf edit uchar * to ulong
290ul = add 8 len pay
291if gt ul len buf
292ret 0
293end
294net_put_be16 buf 0 sport
295net_put_be16 buf 2 dport
296net_put_be16 buf 4 ushort ul
297net_put_be16 buf 6 0
298for i ulong len pay bound 2048
299put buf add 8 i get pay i
300end
301ret ul
302end
303
304note ---- Boot Report Framing (aienos_report) ----
305
306fn net_report_parts n ulong to ulong
307if or eq n 0 gt n NET_REPORT_MAX
308ret 0
309end
310ret div sub add n NET_REPORT_CHUNK 1 NET_REPORT_CHUNK
311end
312
313fn net_report_wrap_eth mac view uchar 6 ubuf view uchar * dst edit uchar * to ulong
314ip_meta = new uint 4 0
315put ip_meta 0 0
316put ip_meta 1 0xffffffff
317put ip_meta 2 uint NET_IPPROTO_UDP
318put ip_meta 3 1
319ipbuf = new uchar 1152 0
320iplen = net_ipv4_build ip_meta ubuf ipbuf
321if eq iplen 0
322ret 0
323end
324macs = new uchar 12 0xff
325for i ulong 6
326put macs add 6 i get mac i
327end
328ret net_eth_build macs NET_ETHERTYPE_IPV4 part ipbuf 0 iplen dst
329end
330
331note rmeta: 0=run_id 1=part_idx 2=parts
332fn net_report_build_frame mac view uchar 6 rmeta view ulong 3 chunk view uchar * dst edit uchar * to ulong
333run_id = get rmeta 0
334part_idx = get rmeta 1
335parts = get rmeta 2
336if or eq parts 0 ge part_idx parts
337ret 0
338end
339if gt len chunk NET_REPORT_CHUNK
340ret 0
341end
342udp_pay = new uchar 1088 0
343note Binary/compact header: "ARPT" (4) + run_id (8) + part_idx (4) + parts (4) + chunk_len (4) = 24 bytes
344put udp_pay 0 65
345put udp_pay 1 82
346put udp_pay 2 80
347put udp_pay 3 84
348net_put_be32 udp_pay 4 uint shr run_id 32
349net_put_be32 udp_pay 8 uint and run_id 0xffffffff
350net_put_be32 udp_pay 12 uint part_idx
351net_put_be32 udp_pay 16 uint parts
352net_put_be32 udp_pay 20 uint len chunk
353for i ulong len chunk bound 1024
354put udp_pay add 24 i get chunk i
355end
356plen = add 24 len chunk
357ubuf = new uchar 1120 0
358ulen = net_udp_build NET_REPORT_PORT NET_REPORT_PORT part udp_pay 0 plen ubuf
359if eq ulen 0
360ret 0
361end
362ret net_report_wrap_eth mac part ubuf 0 ulen dst
363end
364
365fn net_report_parse_payload frame view uchar * poff ulong plen ulong meta_out edit ulong 4 to int
366if lt plen 24
367ret -7
368end
369if or ne get frame poff 65 ne get frame add poff 1 82
370ret -8
371end
372if or ne get frame add poff 2 80 ne get frame add poff 3 84
373ret -8
374end
375rhi = ulong net_be32 frame add poff 4
376rlo = ulong net_be32 frame add poff 8
377pidx = ulong net_be32 frame add poff 12
378ptot = ulong net_be32 frame add poff 16
379clen = ulong net_be32 frame add poff 20
380if or eq ptot 0 ge pidx ptot
381ret -9
382end
383if ne add 24 clen plen
384ret -9
385end
386put meta_out 0 or shl rhi 32 rlo
387put meta_out 1 pidx
388put meta_out 2 ptot
389put meta_out 3 clen
390ret 0
391end
392
393fn net_report_parse_frame frame view uchar * n ulong meta_out edit ulong 4 to int
394eh = new NetEthHdr 0 0 0 0 0 0 0
395if ne net_eth_parse frame n eh NET_OK
396ret -1
397end
398if ne get eh ethertype NET_ETHERTYPE_IPV4
399ret -2
400end
401ih = new NetIpv4 0 0 0 0 0 0 0 0 0 0 0 0
402if ne net_ipv4_parse frame get eh pay_off get eh pay_len ih NET_OK
403ret -3
404end
405if ne get ih proto NET_IPPROTO_UDP
406ret -4
407end
408uh = new NetUdp 0 0 0 0 0 0
409if ne net_udp_parse frame get ih pay_off get ih pay_len uh NET_OK
410ret -5
411end
412if or ne get uh sport NET_REPORT_PORT ne get uh dport NET_REPORT_PORT
413ret -6
414end
415ret net_report_parse_payload frame get uh pay_off get uh pay_len meta_out
416end