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
omega-mv / aienux / net / core / net_test.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note aienux/net/core/net_test.om: Behavioral self-test for Pure-Omega AIENOS Net Core, Boot Report, and Virtio-Net. |
| 3 | note Compose with: si/directives/om2.om aienux/net/core/net.om aienux/net/core/virtio.om aienux/net/core/net_test.om |
| 4 | |
| 5 | text nt_pass "PASS " |
| 6 | text nt_fail "FAIL " |
| 7 | text nt_nl "\n" |
| 8 | text nt_t1 "checksum_and_eth_arp_ipv4_udp" |
| 9 | text nt_t2 "arp_cache_learn_lookup_and_expiry" |
| 10 | text nt_t3 "boot_report_broadcast_roundtrip" |
| 11 | text nt_t4 "virtio_pci_caps_discovery_and_loop" |
| 12 | text nt_t5 "virtio_net_iommu_and_untrusted_ring_failstop" |
| 13 | |
| 14 | fn nt_say t view uchar * to void |
| 15 | k = write 1 t 0 len t |
| 16 | end |
| 17 | |
| 18 | fn nt_run name view uchar * ok bool to uint |
| 19 | if ok |
| 20 | nt_say nt_pass |
| 21 | nt_say name |
| 22 | nt_say nt_nl |
| 23 | ret 0 |
| 24 | end |
| 25 | nt_say nt_fail |
| 26 | nt_say name |
| 27 | nt_say nt_nl |
| 28 | ret 1 |
| 29 | end |
| 30 | |
| 31 | fn test_01_parse frame edit uchar * fw ulong to bool |
| 32 | eh = new NetEthHdr 0 0 0 0 0 0 0 |
| 33 | if ne net_eth_parse frame fw eh NET_OK |
| 34 | ret false |
| 35 | end |
| 36 | if ne get eh ethertype NET_ETHERTYPE_IPV4 |
| 37 | ret false |
| 38 | end |
| 39 | ih = new NetIpv4 0 0 0 0 0 0 0 0 0 0 0 0 |
| 40 | if ne net_ipv4_parse frame get eh pay_off get eh pay_len ih NET_OK |
| 41 | ret false |
| 42 | end |
| 43 | if or ne get ih src 0x0a000001 ne get ih dst 0x0a000002 |
| 44 | ret false |
| 45 | end |
| 46 | uh = new NetUdp 0 0 0 0 0 0 |
| 47 | if ne net_udp_parse frame get ih pay_off get ih pay_len uh NET_OK |
| 48 | ret false |
| 49 | end |
| 50 | if or ne get uh sport 1234 ne get uh dport 5678 |
| 51 | ret false |
| 52 | end |
| 53 | if ne get uh pay_len 5 |
| 54 | ret false |
| 55 | end |
| 56 | note Corrupt IPv4 TTL byte (frame[22] = ipv4[8]) -> checksum invalid |
| 57 | put frame 22 xor get frame 22 1 |
| 58 | if ne net_ipv4_parse frame get eh pay_off get eh pay_len ih NET_ERR_INVALID |
| 59 | ret false |
| 60 | end |
| 61 | ret true |
| 62 | end |
| 63 | |
| 64 | fn test_01_proto to bool |
| 65 | note RFC 1071 odd-length checksum + IPv4 + UDP + Eth roundtrip |
| 66 | pay = new uchar 5 0 |
| 67 | put pay 0 0x48 |
| 68 | put pay 1 0x45 |
| 69 | put pay 2 0x4c |
| 70 | put pay 3 0x4c |
| 71 | put pay 4 0x4f |
| 72 | ubuf = new uchar 64 0 |
| 73 | ul = net_udp_build 1234 5678 pay ubuf |
| 74 | if ne ul 13 |
| 75 | ret false |
| 76 | end |
| 77 | ip_meta = new uint 4 0 |
| 78 | put ip_meta 0 0x0a000001 |
| 79 | put ip_meta 1 0x0a000002 |
| 80 | put ip_meta 2 uint NET_IPPROTO_UDP |
| 81 | put ip_meta 3 64 |
| 82 | ipbuf = new uchar 128 0 |
| 83 | ipl = net_ipv4_build ip_meta part ubuf 0 ul ipbuf |
| 84 | if ne ipl 33 |
| 85 | ret false |
| 86 | end |
| 87 | macs = new uchar 12 0xff |
| 88 | for i ulong 6 |
| 89 | put macs add 6 i 0x02 |
| 90 | end |
| 91 | frame = new uchar 128 0 |
| 92 | fw = net_eth_build macs NET_ETHERTYPE_IPV4 part ipbuf 0 ipl frame |
| 93 | if eq fw 0 |
| 94 | ret false |
| 95 | end |
| 96 | ret test_01_parse frame fw |
| 97 | end |
| 98 | |
| 99 | fn test_02_arp_cache to bool |
| 100 | net_arp_cache_reset |
| 101 | m1 = new uchar 6 0xaa |
| 102 | m2 = new uchar 6 0xbb |
| 103 | outm = new uchar 6 0 |
| 104 | if ne net_arp_cache_learn 0x0a000001 m1 100 50 NET_OK |
| 105 | ret false |
| 106 | end |
| 107 | if not net_arp_cache_lookup 0x0a000001 120 outm |
| 108 | ret false |
| 109 | end |
| 110 | if ne get outm 0 0xaa |
| 111 | ret false |
| 112 | end |
| 113 | note Expired at t=150 |
| 114 | if net_arp_cache_lookup 0x0a000001 150 outm |
| 115 | ret false |
| 116 | end |
| 117 | note Reuses expired slot |
| 118 | if ne net_arp_cache_learn 0x0a000002 m2 150 50 NET_OK |
| 119 | ret false |
| 120 | end |
| 121 | if not net_arp_cache_lookup 0x0a000002 180 outm |
| 122 | ret false |
| 123 | end |
| 124 | ret eq get outm 0 0xbb |
| 125 | end |
| 126 | |
| 127 | fn test_03_boot_report to bool |
| 128 | if ne net_report_parts 0 0 |
| 129 | ret false |
| 130 | end |
| 131 | if ne net_report_parts 1025 2 |
| 132 | ret false |
| 133 | end |
| 134 | mac = new uchar 6 0x12 |
| 135 | rmeta = new ulong 3 0 |
| 136 | put rmeta 0 0x0123456789abcdef |
| 137 | put rmeta 1 1 |
| 138 | put rmeta 2 2 |
| 139 | chunk = new uchar 16 0x5a |
| 140 | frame = new uchar 1280 0 |
| 141 | flen = net_report_build_frame mac rmeta chunk frame |
| 142 | if eq flen 0 |
| 143 | ret false |
| 144 | end |
| 145 | meta = new ulong 4 0 |
| 146 | if ne net_report_parse_frame frame flen meta 0 |
| 147 | ret false |
| 148 | end |
| 149 | if ne get meta 0 0x0123456789abcdef |
| 150 | ret false |
| 151 | end |
| 152 | if or ne get meta 1 1 ne get meta 2 2 |
| 153 | ret false |
| 154 | end |
| 155 | ret eq get meta 3 16 |
| 156 | end |
| 157 | |
| 158 | fn test_04_vpci_caps to bool |
| 159 | cfg = new uchar 256 0 |
| 160 | put cfg 6 0x10 |
| 161 | put cfg 52 0x40 |
| 162 | note Cap 1 at 0x40: COMMON (len=16, bar=4, off=0x1000, next=0x50) |
| 163 | put cfg 0x40 VPCI_CAP_VENDOR |
| 164 | put cfg 0x41 0x50 |
| 165 | put cfg 0x42 16 |
| 166 | put cfg 0x43 VPCI_CFG_COMMON |
| 167 | put cfg 0x44 4 |
| 168 | put cfg 0x49 0x10 |
| 169 | note Cap 2 at 0x50: NOTIFY (len=20, bar=4, off=0x2000, mult=4, next=0x64) |
| 170 | put cfg 0x50 VPCI_CAP_VENDOR |
| 171 | put cfg 0x51 0x64 |
| 172 | put cfg 0x52 20 |
| 173 | put cfg 0x53 VPCI_CFG_NOTIFY |
| 174 | put cfg 0x54 4 |
| 175 | put cfg 0x59 0x20 |
| 176 | put cfg 0x60 4 |
| 177 | note Cap 3 at 0x64: ISR (len=16, bar=4, off=0x3000, next=0x74) |
| 178 | put cfg 0x64 VPCI_CAP_VENDOR |
| 179 | put cfg 0x65 0x74 |
| 180 | put cfg 0x66 16 |
| 181 | put cfg 0x67 VPCI_CFG_ISR |
| 182 | put cfg 0x68 4 |
| 183 | put cfg 0x6d 0x30 |
| 184 | note Cap 4 at 0x74: DEVICE (len=16, bar=4, off=0x4000, next=0) |
| 185 | put cfg 0x74 VPCI_CAP_VENDOR |
| 186 | put cfg 0x75 0 |
| 187 | put cfg 0x76 16 |
| 188 | put cfg 0x77 VPCI_CFG_DEVICE |
| 189 | put cfg 0x78 4 |
| 190 | put cfg 0x7d 0x40 |
| 191 | caps = new uint 10 0 |
| 192 | if ne vpci_parse_caps cfg caps VPCI_OK |
| 193 | ret false |
| 194 | end |
| 195 | if or ne get caps 1 0x1000 ne get caps 4 4 |
| 196 | ret false |
| 197 | end |
| 198 | note Introduce capability cycle -> VPCI_ERR_LOOP |
| 199 | put cfg 0x75 0x40 |
| 200 | ret eq vpci_parse_caps cfg caps VPCI_ERR_LOOP |
| 201 | end |
| 202 | |
| 203 | fn test_05_virtio_net to bool |
| 204 | note Fail closed if ACCESS_PLATFORM required but not offered |
| 205 | if ne vnet_init VNET_F_VERSION_1 8 VNET_INIT_REQUIRE_ACCESS_PLATFORM VNET_ERR_ACCESS_PLATFORM |
| 206 | ret false |
| 207 | end |
| 208 | note Succeed when both VERSION_1 and ACCESS_PLATFORM offered |
| 209 | feats = or VNET_F_VERSION_1 or VNET_F_ACCESS_PLATFORM VNET_F_MAC |
| 210 | if ne vnet_init feats 8 VNET_INIT_REQUIRE_ACCESS_PLATFORM VNET_OK |
| 211 | ret false |
| 212 | end |
| 213 | pkt = new uchar 64 0x42 |
| 214 | if ne vnet_tx_submit pkt VNET_OK |
| 215 | ret false |
| 216 | end |
| 217 | if ne vnet_tx_complete 0 VNET_OK |
| 218 | ret false |
| 219 | end |
| 220 | if ne vnet_rx_post VNET_OK |
| 221 | ret false |
| 222 | end |
| 223 | if ne vnet_rx_complete 0 add VNET_HDR_LEN 64 pkt VNET_OK |
| 224 | ret false |
| 225 | end |
| 226 | note Untrusted device reports duplicate/unposted completion -> sticky BROKEN |
| 227 | if ne vnet_rx_complete 0 add VNET_HDR_LEN 64 pkt VNET_ERR_BROKEN |
| 228 | ret false |
| 229 | end |
| 230 | if not vnet_is_broken |
| 231 | ret false |
| 232 | end |
| 233 | ret eq vnet_tx_submit pkt VNET_ERR_BROKEN |
| 234 | end |
| 235 | |
| 236 | fn main to int |
| 237 | failed = uint 0 |
| 238 | failed = add failed nt_run nt_t1 test_01_proto |
| 239 | failed = add failed nt_run nt_t2 test_02_arp_cache |
| 240 | failed = add failed nt_run nt_t3 test_03_boot_report |
| 241 | failed = add failed nt_run nt_t4 test_04_vpci_caps |
| 242 | failed = add failed nt_run nt_t5 test_05_virtio_net |
| 243 | if ne failed 0 |
| 244 | ret 1 |
| 245 | end |
| 246 | ret 0 |
| 247 | end |