liteeth/test/model/icmp.py

95 lines
2.9 KiB
Python
Raw Normal View History

#
# This file is part of LiteEth.
#
# Copyright (c) 2015-2019 Florent Kermarrec <florent@enjoy-digital.fr>
# SPDX-License-Identifier: BSD-2-Clause
2015-09-07 07:28:02 -04:00
import math
from litex.soc.interconnect.stream_sim import *
2015-09-08 03:50:45 -04:00
from liteeth.common import *
2015-09-07 07:28:02 -04:00
from test.model import ip
2015-09-07 07:28:02 -04:00
# Helpers ------------------------------------------------------------------------------------------
2015-09-07 07:28:02 -04:00
def print_icmp(s):
print_with_prefix(s, "[ICMP]")
# ICMP Packet --------------------------------------------------------------------------------------
2015-09-07 07:28:02 -04:00
class ICMPPacket(Packet):
def __init__(self, init=[]):
Packet.__init__(self, init)
def decode(self):
header = []
for byte in self[:icmp_header.length]:
header.append(self.pop(0))
for k, v in sorted(icmp_header.fields.items()):
setattr(self, k, get_field_data(v, header))
def encode(self):
header = 0
for k, v in sorted(icmp_header.fields.items()):
value = merge_bytes(split_bytes(getattr(self, k),
math.ceil(v.width/8)),
"little")
header += (value << v.offset+(v.byte*8))
for d in split_bytes(header, icmp_header.length):
self.insert(0, d)
def __repr__(self):
r = "--------\n"
for k in sorted(icmp_header.fields.keys()):
r += k + " : 0x{:0x}\n".format(getattr(self, k))
r += "payload: "
for d in self:
r += "{:02x}".format(d)
return r
# ICMP ---------------------------------------------------------------------------------------------
2015-09-07 07:28:02 -04:00
class ICMP(Module):
def __init__(self, ip, ip_address, debug=False):
self.ip = ip
2015-09-07 07:28:02 -04:00
self.ip_address = ip_address
self.debug = debug
2015-09-07 07:28:02 -04:00
self.tx_packets = []
self.tx_packet = ICMPPacket()
self.rx_packet = ICMPPacket()
2015-09-07 07:28:02 -04:00
self.ip.set_icmp_callback(self.callback)
def send(self, packet):
packet.encode()
if self.debug:
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
2015-09-07 07:28:02 -04:00
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
2015-09-07 07:28:02 -04:00
self.ip.send(ip_packet)
def callback(self, packet):
packet = ICMPPacket(packet)
packet.decode()
if self.debug:
print_icmp("<<<<<<<<")
print_icmp(packet)
self.process(packet)
def process(self, packet):
pass