add ip model skeleton

This commit is contained in:
Florent Kermarrec 2015-01-30 18:07:26 +01:00
parent d066497ab1
commit 328295c124
3 changed files with 107 additions and 18 deletions

View File

@ -6,6 +6,7 @@ CMD = PYTHONPATH=$(LEDIR) $(PYTHON)
model_tb: model_tb:
$(CMD) ./model/mac.py $(CMD) ./model/mac.py
$(CMD) ./model/arp.py $(CMD) ./model/arp.py
$(CMD) ./model/ip.py
mac_core_tb: mac_core_tb:
$(CMD) mac_core_tb.py $(CMD) mac_core_tb.py

View File

@ -48,24 +48,6 @@ arp_reply_infos = {
"destination_ip_address" : 0xa9feff42 "destination_ip_address" : 0xa9feff42
} }
ping_request = format_dump("""
ff 03 00 21 45 00 00 64 00 00 00 00 ff 01 a7 96
0a 00 00 01 0a 00 00 02 08 00 d6 7f 00 00 00 00
00 00 00 00 00 02 a7 c8 ab cd ab cd ab cd ab cd
ab cd ab cd ab cd ab cd ab cd ab cd ab cd ab cd
ab cd ab cd ab cd ab cd ab cd ab cd ab cd ab cd
ab cd ab cd ab cd ab cd ab cd ab cd ab cd ab cd
ab cd ab cd ab cd ab cd""")
icmp_echo = format_dump("""
ff 03 00 21 45 00 00 64 00 00 00 00 ff 01 a7 96
0a 00 00 02 0a 00 00 01 00 00 de 7f 00 00 00 00
00 00 00 00 00 02 a7 c8 ab cd ab cd ab cd ab cd
ab cd ab cd ab cd ab cd ab cd ab cd ab cd ab cd
ab cd ab cd ab cd ab cd ab cd ab cd ab cd ab cd
ab cd ab cd ab cd ab cd ab cd ab cd ab cd ab cd
ab cd ab cd ab cd ab cd""")
udp = format_dump(""" udp = format_dump("""
d0 7a b5 96 cd 0a 00 14 0b 33 33 27 08 00 45 00 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 00 5f 31 16 00 00 80 11 87 77 c0 a8 01 65 b2 7b
@ -74,3 +56,11 @@ d0 7a b5 96 cd 0a 00 14 0b 33 33 27 08 00 45 00
aa 9b 4e 4d f9 2e 51 52 fe ff 65 31 3a 71 34 3a aa 9b 4e 4d f9 2e 51 52 fe ff 65 31 3a 71 34 3a
70 69 6e 67 31 3a 74 34 3a 85 72 00 00 31 3a 76 70 69 6e 67 31 3a 74 34 3a 85 72 00 00 31 3a 76
34 3a 55 54 7e 62 31 3a 79 31 3a 71 65""") 34 3a 55 54 7e 62 31 3a 79 31 3a 71 65""")
udp_infos = {
"source_mac_address" : 0x00140b333327,
"destination_mac_address" : 0xd07ab596cd0a,
"protocol" : 0x11,
"source_ip_address" : 0xc0a80165,
"destination_ip_address" : 0xb27b0d78
}

98
liteeth/test/model/ip.py Normal file
View File

@ -0,0 +1,98 @@
import math, binascii
from liteeth.common import *
from liteeth.mac.common import *
from liteeth.test.common import *
from liteeth.test.model import mac
def print_ip(s):
print_with_prefix(s, "[IP]")
preamble = split_bytes(eth_preamble, 8)
# IP model
class IPPacket(Packet):
def __init__(self, init=[]):
Packet.__init__(self, init)
def decode(self):
header = []
for byte in self[:ipv4_header_len]:
header.append(self.pop(0))
for k, v in sorted(ipv4_header.items()):
setattr(self, k, get_field_data(v, header))
def encode(self):
header = 0
for k, v in sorted(ipv4_header.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, ipv4_header_len):
self.insert(0, d)
def __repr__(self):
r = "--------\n"
for k in sorted(ipv4_header.keys()):
r += k + " : 0x%x" %getattr(self,k) + "\n"
r += "payload: "
for d in self:
r += "%02x" %d
return r
class IP(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 = IPPacket()
self.rx_packet = IPPacket()
self.table = {}
self.request_pending = False
self.mac.set_ip_callback(self.callback)
def send(self, packet):
packet.encode()
if self.debug:
print_ip(">>>>>>>>")
print_ip(packet)
mac_packet = mac.MACPacket(packet)
mac_packet.destination_mac_address = packet.destination_mac_address
mac_packet.source_mac_address = packet.source_mac_address
mac_packet.ethernet_type = ethernet_type_ip
self.mac.send(mac_packet)
def callback(self, packet):
packet = IPPacket(packet)
packet.decode()
if self.debug:
print_ip("<<<<<<<<")
print_ip(packet)
self.process(packet)
def process(self, packet):
pass
if __name__ == "__main__":
from liteeth.test.model.dumps import *
from liteeth.test.model.mac import *
errors = 0
# ARP request
packet = MACPacket(udp)
packet.decode_remove_header()
#print(packet)
packet = IPPacket(packet)
# check decoding
packet.decode()
#print(packet)
errors += verify_packet(packet, {})
# check encoding
packet.encode()
packet.decode()
#print(packet)
errors += verify_packet(packet, {})
print("ip errors " + str(errors))