This repository has been archived by the owner on Sep 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Write IP lookback hardware test
- Loading branch information
Showing
15 changed files
with
308 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import unittest | ||
import util | ||
import os | ||
import socket | ||
import struct | ||
import random | ||
|
||
IFNAME = os.environ['IFNAME'] | ||
DEV = os.environ['DEV'] | ||
|
||
def open_socket(): | ||
s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(3)) | ||
s.bind((IFNAME, 0)) | ||
return s | ||
|
||
src_ip = '192.168.1.0' | ||
dst_ip = '192.168.1.123' | ||
src_mac = b"\x8c\x8c\xaa\xc8\x2b\xee" # hardcoded in echo stack | ||
dst_mac = b"\x00\x00\x00\xff\xff\xff" | ||
|
||
class TestEthUartEcho(unittest.TestCase): | ||
def _test(self, dst_ip): | ||
""" | ||
Sends five IP packets and tests if they are sent back | ||
""" | ||
with open_socket() as s: | ||
s.settimeout(5) | ||
|
||
for _ in range(5): | ||
data = os.urandom(random.randint(0,25)) | ||
payload = struct.pack("!HHHH", 1337, 1337, len(data) + 8, 0) + data | ||
ip_header = create_ip_header(src_ip, dst_ip, len(payload)) | ||
packet = dst_mac + src_mac + b"\x08\x00" + ip_header + payload | ||
|
||
s.send(packet) | ||
|
||
response = s.recv(2**16 - 1) | ||
total_length = struct.unpack_from("!H", response, 16)[0] | ||
response = response[:total_length + 14] | ||
|
||
expected_ip_header = create_ip_header(dst_ip, src_ip, len(payload)) | ||
expected = src_mac + dst_mac + b"\x08\x00" + expected_ip_header + payload | ||
|
||
self.assertEqual(expected, response) | ||
|
||
def testWithIp(self): | ||
self._test(dst_ip) | ||
|
||
def testWithBroadcast(self): | ||
self._test("192.168.1.255") | ||
|
||
def create_ip_header(src_ip, dst_ip, payload_length): | ||
header = bytearray(struct.pack('!BBHHHBBH4s4s', | ||
(4 << 4) + 5, # version, ihl | ||
0, | ||
20 + payload_length, # total length | ||
0, | ||
0, | ||
64, # TTL. Set to 64 so Wireshark thinks this is a totally normal UDP packet | ||
0, # Protocol. 0, because we can't set this in ipLitePacketizerC | ||
0, # checksum | ||
socket.inet_aton(src_ip), | ||
socket.inet_aton(dst_ip))) | ||
checksum = util.internet_checksum(header) | ||
header[10:12] = struct.pack("!H", checksum) | ||
return bytes(header) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
{-# language FlexibleContexts #-} | ||
|
||
{-| | ||
Module : Clash.Cores.Ethernet.Examples.RxStacks | ||
Description : Provides the entire receive stack as a circuit. | ||
-} | ||
module Clash.Cores.Ethernet.Examples.RxStacks | ||
( macRxStack | ||
, ipRxStack | ||
) where | ||
|
||
import Clash.Cores.Crc | ||
import Clash.Cores.Crc.Catalog | ||
import Clash.Prelude | ||
|
||
import Protocols | ||
import Protocols.Extra.PacketStream | ||
import Protocols.Extra.PacketStream.AsyncFIFO ( asyncFifoC ) | ||
import Protocols.Extra.PacketStream.Converters ( upConverterC ) | ||
import Protocols.Extra.PacketStream.Routing ( packetDispatcherC ) | ||
|
||
import Clash.Cores.Ethernet.IP.IPPacketizers | ||
import Clash.Cores.Ethernet.IP.IPv4Types | ||
import Clash.Cores.Ethernet.Mac.EthernetTypes | ||
import Clash.Cores.Ethernet.Mac.FrameCheckSequence ( fcsValidatorC ) | ||
import Clash.Cores.Ethernet.Mac.MacPacketizers ( macDepacketizerC ) | ||
import Clash.Cores.Ethernet.Mac.Preamble ( preambleStripperC ) | ||
|
||
-- | Processes received ethernet frames | ||
macRxStack | ||
:: forall (dataWidth :: Nat) (dom :: Domain) (domEth :: Domain) | ||
. ( HiddenClockResetEnable dom | ||
, KnownDomain domEth | ||
, HardwareCrc Crc32_ethernet 8 dataWidth | ||
, KnownNat dataWidth | ||
, 1 <= dataWidth | ||
) | ||
=> Clock domEth | ||
-> Reset domEth | ||
-> Enable domEth | ||
-> Signal dom MacAddress | ||
-> Circuit (PacketStream domEth 1 ()) (PacketStream dom dataWidth EthernetHeader) | ||
macRxStack ethClk ethRst ethEn macAddressS = | ||
upConverterC' | ||
|> asyncFifoC' | ||
|> preambleStripperC | ||
|> fcsValidatorC | ||
|> macDepacketizerC | ||
|> filterMetaS (isForMyMac <$> macAddressS) | ||
where | ||
upConverterC' = exposeClockResetEnable upConverterC ethClk ethRst ethEn | ||
asyncFifoC' = asyncFifoC d4 ethClk ethRst ethEn hasClock hasReset hasEnable | ||
isForMyMac myMac (_macDst -> to) = to == myMac || to == broadcastMac | ||
|
||
-- | Processes received IP packets | ||
ipRxStack | ||
:: forall (dataWidth :: Nat) (dom :: Domain) (domEth :: Domain) | ||
. ( HiddenClockResetEnable dom | ||
, KnownDomain domEth | ||
, HardwareCrc Crc32_ethernet 8 dataWidth | ||
, KnownNat dataWidth | ||
, 1 <= dataWidth | ||
) | ||
=> Clock domEth | ||
-> Reset domEth | ||
-> Enable domEth | ||
-> Signal dom MacAddress | ||
-> Signal dom (IPv4Address, IPv4Address) | ||
-> Circuit (PacketStream domEth 1 ()) (PacketStream dom dataWidth IPv4HeaderLite) | ||
ipRxStack ethClk ethRst ethEn macAddressS ipS = circuit $ \raw -> do | ||
ethernetFrames <- macRxStack ethClk ethRst ethEn macAddressS -< raw | ||
[ip] <- packetDispatcherC (isIpv4 :> Nil) -< ethernetFrames | ||
ipDepacketizerLiteC |> filterMetaS (isForMyIp <$> ipS) -< ip | ||
where | ||
isIpv4 = (== 0x0800) . _etherType | ||
isForMyIp (ip, subnet) (_ipv4lDestination -> to) = to == ip || to == ipv4Broadcast ip subnet |
Oops, something went wrong.