From 91f0f4ce80c953185e696ff90a55ab99fa7b454c Mon Sep 17 00:00:00 2001 From: Florent Kermarrec Date: Sat, 23 Nov 2019 15:16:29 +0100 Subject: [PATCH] test/model: improve presentation/readability --- test/model/arp.py | 55 ++++++++++++++++++++++------------------- test/model/dumps.py | 10 +++++++- test/model/etherbone.py | 14 ++++++----- test/model/icmp.py | 35 ++++++++++++++------------ test/model/ip.py | 29 ++++++++++++---------- test/model/mac.py | 17 +++++++------ test/model/phy.py | 14 +++++++---- test/model/udp.py | 37 ++++++++++++++------------- 8 files changed, 119 insertions(+), 92 deletions(-) diff --git a/test/model/arp.py b/test/model/arp.py index 8290ebe..7ac44ab 100644 --- a/test/model/arp.py +++ b/test/model/arp.py @@ -1,4 +1,4 @@ -# This file is Copyright (c) 2015-2017 Florent Kermarrec +# This file is Copyright (c) 2015-2019 Florent Kermarrec # License: BSD import math @@ -9,6 +9,7 @@ from liteeth.common import * from test.model import mac +# Helpers ------------------------------------------------------------------------------------------ def print_arp(s): print_with_prefix(s, "[ARP]") @@ -16,7 +17,8 @@ def print_arp(s): preamble = split_bytes(eth_preamble, 8) -# ARP model +# ARP Packet --------------------------------------------------------------------------------------- + class ARPPacket(Packet): def __init__(self, init=[]): Packet.__init__(self, init) @@ -47,17 +49,18 @@ class ARPPacket(Packet): r += "{:02x}".format(d) return r +# ARP ---------------------------------------------------------------------------------------------- class ARP(Module): def __init__(self, mac, mac_address, ip_address, debug=False): - self.mac = mac - self.mac_address = mac_address - self.ip_address = ip_address - self.debug = debug - self.tx_packets = [] - self.tx_packet = ARPPacket() - self.rx_packet = ARPPacket() - self.table = {} + self.mac = mac + self.mac_address = mac_address + self.ip_address = ip_address + self.debug = debug + self.tx_packets = [] + self.tx_packet = ARPPacket() + self.rx_packet = ARPPacket() + self.table = {} self.request_pending = False self.mac.set_arp_callback(self.callback) @@ -68,8 +71,8 @@ class ARP(Module): print_arp(">>>>>>>>") print_arp(packet) mac_packet = mac.MACPacket(packet) - mac_packet.target_mac = packet.target_mac - mac_packet.sender_mac = packet.sender_mac + mac_packet.target_mac = packet.target_mac + mac_packet.sender_mac = packet.sender_mac mac_packet.ethernet_type = ethernet_type_arp self.mac.send(mac_packet) @@ -100,15 +103,15 @@ class ARP(Module): def process_request(self, request): if request.target_ip == self.ip_address: reply = ARPPacket([0]*(eth_min_len-arp_header.length)) - reply.hwtype = arp_hwtype_ethernet - reply.proto = arp_proto_ip - reply.opcode = arp_opcode_reply - reply.hwsize = 6 - reply.protosize = 4 + reply.hwtype = arp_hwtype_ethernet + reply.proto = arp_proto_ip + reply.opcode = arp_opcode_reply + reply.hwsize = 6 + reply.protosize = 4 reply.sender_mac = self.mac_address - reply.sender_ip = self.ip_address + reply.sender_ip = self.ip_address reply.target_mac = request.sender_mac - reply.target_ip = request.sender_ip + reply.target_ip = request.sender_ip self.send(reply) def process_reply(self, reply): @@ -116,12 +119,12 @@ class ARP(Module): def request(self, ip_address): request = ARPPacket([0]*(eth_min_len-arp_header.length)) - request.hwtype = arp_hwtype_ethernet - request.proto = arp_proto_ip - request.opcode = arp_opcode_request - request.hwsize = 6 - request.protosize = 4 + request.hwtype = arp_hwtype_ethernet + request.proto = arp_proto_ip + request.opcode = arp_opcode_request + request.hwsize = 6 + request.protosize = 4 request.sender_mac = self.mac_address - request.sender_ip = self.ip_address + request.sender_ip = self.ip_address request.target_mac = 0xffffffffffff - request.target_ip = ip_address + request.target_ip = ip_address diff --git a/test/model/dumps.py b/test/model/dumps.py index ed23d31..af2a0cf 100644 --- a/test/model/dumps.py +++ b/test/model/dumps.py @@ -1,8 +1,9 @@ -# This file is Copyright (c) 2015 Florent Kermarrec +# This file is Copyright (c) 2015-2019 Florent Kermarrec # License: BSD import re +# Helpers ------------------------------------------------------------------------------------------ def format_dump(dump): return [int(s, 16) for s in re.split(r'[;,\s\n]\s*', dump) if s is not ""] @@ -17,6 +18,8 @@ def verify_packet(packet, infos): errors += 1 return errors +# ARP Dumps ---------------------------------------------------------------------------------------- + arp_request = format_dump(""" 00 22 19 22 54 9e 00 12 3f 97 92 01 08 06 00 01 08 00 06 04 00 01 00 12 3f 97 92 01 a9 fe ff 42 @@ -53,6 +56,8 @@ arp_reply_infos = { "target_ip": 0xa9feff42 } +# UDP Dumps ---------------------------------------------------------------------------------------- + udp = format_dump(""" d0 7a b5 96 cd 0a 00 14 0b 33 33 27 08 00 45 00 00 5f 31 16 00 00 80 11 87 77 c0 a8 01 65 b2 7b @@ -72,6 +77,9 @@ udp_infos = { "dst_port": 0x690f } + +# Ping Dumps --------------------------------------------------------------------------------------- + ping_request = format_dump(""" 00 50 56 e0 14 49 00 0c 29 34 0b de 08 00 45 00 00 3c d7 43 00 00 80 01 2b 73 c0 a8 9e 8b ae 89 diff --git a/test/model/etherbone.py b/test/model/etherbone.py index a2ace9e..af1f2c4 100644 --- a/test/model/etherbone.py +++ b/test/model/etherbone.py @@ -1,4 +1,4 @@ -# This file is Copyright (c) 2015-2017 Florent Kermarrec +# This file is Copyright (c) 2015-2019 Florent Kermarrec # License: BSD import math @@ -10,19 +10,21 @@ from liteeth.common import * from test.model import udp +# Helpers ------------------------------------------------------------------------------------------ def print_etherbone(s): print_with_prefix(s, "[ETHERBONE]") -# Etherbone model +# Etherbone ---------------------------------------------------------------------------------------- + class Etherbone(Module): def __init__(self, udp, debug=False): - self.udp = udp + self.udp = udp self.debug = debug self.tx_packets = [] - self.tx_packet = EtherbonePacket() - self.rx_packet = EtherbonePacket() + self.tx_packet = EtherbonePacket() + self.rx_packet = EtherbonePacket() udp.set_etherbone_callback(self.callback) @@ -34,7 +36,7 @@ class Etherbone(Module): udp_packet = udp.UDPPacket(packet) udp_packet.src_port = 0x1234 # XXX udp_packet.dst_port = 20000 # XXX - udp_packet.length = len(packet) + udp_packet.length = len(packet) udp_packet.checksum = 0 self.udp.send(udp_packet) diff --git a/test/model/icmp.py b/test/model/icmp.py index 7bb621c..3fa8542 100644 --- a/test/model/icmp.py +++ b/test/model/icmp.py @@ -1,4 +1,4 @@ -# This file is Copyright (c) 2015-2017 Florent Kermarrec +# This file is Copyright (c) 2015-2019 Florent Kermarrec # License: BSD import math @@ -9,12 +9,14 @@ from liteeth.common import * from test.model import ip +# Helpers ------------------------------------------------------------------------------------------ def print_icmp(s): print_with_prefix(s, "[ICMP]") -# ICMP model +# ICMP Packet -------------------------------------------------------------------------------------- + class ICMPPacket(Packet): def __init__(self, init=[]): Packet.__init__(self, init) @@ -45,15 +47,16 @@ class ICMPPacket(Packet): r += "{:02x}".format(d) return r +# ICMP --------------------------------------------------------------------------------------------- class ICMP(Module): def __init__(self, ip, ip_address, debug=False): - self.ip = ip + self.ip = ip self.ip_address = ip_address - self.debug = debug + self.debug = debug self.tx_packets = [] - self.tx_packet = ICMPPacket() - self.rx_packet = ICMPPacket() + self.tx_packet = ICMPPacket() + self.rx_packet = ICMPPacket() self.ip.set_icmp_callback(self.callback) @@ -63,17 +66,17 @@ class ICMP(Module): print_icmp(">>>>>>>>") print_icmp(packet) ip_packet = ip.IPPacket(packet) - ip_packet.version = 0x4 - ip_packet.ihl = 0x5 - ip_packet.total_length = len(packet) + ip_packet.ihl - ip_packet.identification = 0 - ip_packet.flags = 0 + ip_packet.version = 0x4 + ip_packet.ihl = 0x5 + ip_packet.total_length = len(packet) + ip_packet.ihl + ip_packet.identification = 0 + ip_packet.flags = 0 ip_packet.fragment_offset = 0 - ip_packet.ttl = 0x80 - ip_packet.sender_ip = self.ip_address - ip_packet.target_ip = 0x12345678 # XXX - ip_packet.checksum = 0 - ip_packet.protocol = icmp_protocol + ip_packet.ttl = 0x80 + ip_packet.sender_ip = self.ip_address + ip_packet.target_ip = 0x12345678 # XXX + ip_packet.checksum = 0 + ip_packet.protocol = icmp_protocol self.ip.send(ip_packet) def callback(self, packet): diff --git a/test/model/ip.py b/test/model/ip.py index 198a8fa..b32adf6 100644 --- a/test/model/ip.py +++ b/test/model/ip.py @@ -1,4 +1,4 @@ -# This file is Copyright (c) 2015-2017 Florent Kermarrec +# This file is Copyright (c) 2015-2019 Florent Kermarrec # License: BSD import math @@ -9,6 +9,7 @@ from liteeth.common import * from test.model import mac +# Helpers ------------------------------------------------------------------------------------------ def print_ip(s): print_with_prefix(s, "[IP]") @@ -27,7 +28,8 @@ def checksum(msg): return ~s & 0xffff -# IP model +# IP Packet ---------------------------------------------------------------------------------------- + class IPPacket(Packet): def __init__(self, init=[]): Packet.__init__(self, init) @@ -71,20 +73,21 @@ class IPPacket(Packet): r += "{:02x}".format(d) return r +# IP ----------------------------------------------------------------------------------------------- class IP(Module): def __init__(self, mac, mac_address, ip_address, debug=False, loopback=False): - self.mac = mac - self.mac_address = mac_address - self.ip_address = ip_address - self.debug = debug - self.loopback = loopback - self.rx_packet = IPPacket() - self.table = {} + self.mac = mac + self.mac_address = mac_address + self.ip_address = ip_address + self.debug = debug + self.loopback = loopback + self.rx_packet = IPPacket() + self.table = {} self.request_pending = False - self.udp_callback = None - self.icmp_callback = None + self.udp_callback = None + self.icmp_callback = None self.mac.set_ip_callback(self.callback) @@ -101,8 +104,8 @@ class IP(Module): print_ip(">>>>>>>>") print_ip(packet) mac_packet = mac.MACPacket(packet) - mac_packet.target_mac = 0x12345678abcd # XXX - mac_packet.sender_mac = self.mac_address + mac_packet.target_mac = 0x12345678abcd # XXX + mac_packet.sender_mac = self.mac_address mac_packet.ethernet_type = ethernet_type_ip self.mac.send(mac_packet) diff --git a/test/model/mac.py b/test/model/mac.py index 99de822..e49cd80 100644 --- a/test/model/mac.py +++ b/test/model/mac.py @@ -8,13 +8,13 @@ from litex.soc.interconnect.stream_sim import * from liteeth.common import * +# Helpers ------------------------------------------------------------------------------------------ def print_mac(s): print_with_prefix(s, "[MAC]") preamble = split_bytes(eth_preamble, 8, "little") - def crc32(l): crc = [] crc_bytes = split_bytes(binascii.crc32(bytes(l)), 4, "little") @@ -22,13 +22,13 @@ def crc32(l): crc.append(int(byte)) return crc +# MAC Packet --------------------------------------------------------------------------------------- -# MAC model class MACPacket(Packet): def __init__(self, init=[]): Packet.__init__(self, init) self.preamble_error = False - self.crc_error = False + self.crc_error = False def check_remove_preamble(self): if comp(self[0:8], preamble): @@ -93,15 +93,16 @@ class MACPacket(Packet): r += "{:02x}".format(d) return r +# MAC ---------------------------------------------------------------------------------------------- class MAC(Module): def __init__(self, phy, debug=False, loopback=False): - self.phy = phy - self.debug = debug - self.loopback = loopback - self.rx_packet = MACPacket() + self.phy = phy + self.debug = debug + self.loopback = loopback + self.rx_packet = MACPacket() - self.ip_callback = None + self.ip_callback = None self.arp_callback = None phy.set_mac_callback(self.callback) diff --git a/test/model/phy.py b/test/model/phy.py index 70116c9..5f2b0f2 100644 --- a/test/model/phy.py +++ b/test/model/phy.py @@ -1,36 +1,40 @@ -# This file is Copyright (c) 2015-2016 Florent Kermarrec +# This file is Copyright (c) 2015-2019 Florent Kermarrec # License: BSD from litex.soc.interconnect.stream_sim import * from liteeth.common import * +# Helpers ------------------------------------------------------------------------------------------ def print_phy(s): print_with_prefix(s, "[PHY]") -# PHY model +# PHY Source --------------------------------------------------------------------------------------- + class PHYSource(PacketStreamer): def __init__(self, dw): PacketStreamer.__init__(self, eth_phy_description(dw)) +# PHY Sink ----------------------------------------------------------------------------------------- class PHYSink(PacketLogger): def __init__(self, dw): PacketLogger.__init__(self, eth_phy_description(dw)) +# PHY ---------------------------------------------------------------------------------------------- class PHY(Module): def __init__(self, dw, debug=False): - self.dw = dw + self.dw = dw self.debug = debug self.submodules.phy_source = PHYSource(dw) - self.submodules.phy_sink = PHYSink(dw) + self.submodules.phy_sink = PHYSink(dw) self.source = self.phy_source.source - self.sink = self.phy_sink.sink + self.sink = self.phy_sink.sink self.mac_callback = None diff --git a/test/model/udp.py b/test/model/udp.py index 23f8c4e..8ca6fc5 100644 --- a/test/model/udp.py +++ b/test/model/udp.py @@ -1,4 +1,4 @@ -# This file is Copyright (c) 2015-2017 Florent Kermarrec +# This file is Copyright (c) 2015-2019 Florent Kermarrec # License: BSD import math @@ -9,12 +9,14 @@ from liteeth.common import * from test.model import ip +# Helpers ------------------------------------------------------------------------------------------ def print_udp(s): print_with_prefix(s, "[UDP]") -# UDP model +# UDP Packet --------------------------------------------------------------------------------------- + class UDPPacket(Packet): def __init__(self, init=[]): Packet.__init__(self, init) @@ -45,16 +47,17 @@ class UDPPacket(Packet): r += "{:02x}".format(d) return r +# UDP ---------------------------------------------------------------------------------------------- class UDP(Module): def __init__(self, ip, ip_address, debug=False, loopback=False): - self.ip = ip + self.ip = ip self.ip_address = ip_address - self.debug = debug - self.loopback = loopback + self.debug = debug + self.loopback = loopback self.tx_packets = [] - self.tx_packet = UDPPacket() - self.rx_packet = UDPPacket() + self.tx_packet = UDPPacket() + self.rx_packet = UDPPacket() self.etherbone_callback = None @@ -69,17 +72,17 @@ class UDP(Module): print_udp(">>>>>>>>") print_udp(packet) ip_packet = ip.IPPacket(packet) - ip_packet.version = 0x4 - ip_packet.ihl = 0x5 - ip_packet.total_length = len(packet) + ip_packet.ihl - ip_packet.identification = 0 - ip_packet.flags = 0 + ip_packet.version = 0x4 + ip_packet.ihl = 0x5 + ip_packet.total_length = len(packet) + ip_packet.ihl + ip_packet.identification = 0 + ip_packet.flags = 0 ip_packet.fragment_offset = 0 - ip_packet.ttl = 0x80 - ip_packet.sender_ip = self.ip_address - ip_packet.target_ip = 0x12345678 # XXX - ip_packet.checksum = 0 - ip_packet.protocol = udp_protocol + ip_packet.ttl = 0x80 + ip_packet.sender_ip = self.ip_address + ip_packet.target_ip = 0x12345678 # XXX + ip_packet.checksum = 0 + ip_packet.protocol = udp_protocol self.ip.send(ip_packet) def callback(self, packet):