From 7732e0d23cff4504333fba213bf79035cdcc9dc6 Mon Sep 17 00:00:00 2001 From: rowanG077 Date: Sun, 16 Jul 2023 23:56:40 +0200 Subject: [PATCH] core/ip.py: Add gateway support --- liteeth/core/__init__.py | 12 ++++++++++ liteeth/core/ip.py | 47 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/liteeth/core/__init__.py b/liteeth/core/__init__.py index 4eca8008..819d0953 100644 --- a/liteeth/core/__init__.py +++ b/liteeth/core/__init__.py @@ -19,6 +19,9 @@ def __init__(self, phy, mac_address, ip_address, clk_freq, dw=8, with_icmp = True, with_ip_broadcast = True, with_sys_datapath = False, + gateway = None, + netaddress = None, + netmask = None, tx_cdc_depth = 32, tx_cdc_buffered = True, rx_cdc_depth = 32, @@ -60,6 +63,9 @@ def __init__(self, phy, mac_address, ip_address, clk_freq, dw=8, ip_address = ip_address, arp_table = self.arp.table, with_broadcast = with_ip_broadcast, + gateway = gateway, + netaddress = netaddress, + netmask = netmask, dw = dw, ) # ICMP (Optional). @@ -78,6 +84,9 @@ def __init__(self, phy, mac_address, ip_address, clk_freq, dw=8, with_icmp = True, with_ip_broadcast = True, with_sys_datapath = False, + gateway = None, + netaddress = None, + netmask = None, tx_cdc_depth = 32, tx_cdc_buffered = True, rx_cdc_depth = 32, @@ -98,6 +107,9 @@ def __init__(self, phy, mac_address, ip_address, clk_freq, dw=8, dw = dw, with_ip_broadcast = with_ip_broadcast, with_sys_datapath = with_sys_datapath, + gateway = gateway, + netaddress = netaddress, + netmask = netmask, tx_cdc_depth = tx_cdc_depth, tx_cdc_buffered = tx_cdc_buffered, rx_cdc_depth = rx_cdc_depth, diff --git a/liteeth/core/ip.py b/liteeth/core/ip.py index 84e8e280..6ecee475 100644 --- a/liteeth/core/ip.py +++ b/liteeth/core/ip.py @@ -93,8 +93,21 @@ def __init__(self, dw=8): ) -class LiteEthIPTX(LiteXModule): - def __init__(self, mac_address, ip_address, arp_table, dw=8): +class LiteEthIPTX(Module): + def __init__(self, mac_address, ip_address, arp_table, + gateway = None, + netaddress = None, + netmask = None, + dw=8 + ): + # Must all be either set or not + assert (gateway is None) == (netaddress is None) == (netmask is None) + + if gateway is not None: + gateway = convert_ip(gateway) + netaddress = convert_ip(netaddress) + netmask = convert_ip(netmask) + self.sink = sink = stream.Endpoint(eth_ipv4_user_description(dw)) self.source = source = stream.Endpoint(eth_mac_description(dw)) self.target_unreachable = Signal() @@ -148,7 +161,19 @@ def __init__(self, mac_address, ip_address, arp_table, dw=8): ) ) ) - self.comb += arp_table.request.ip_address.eq(sink.ip_address) + + if gateway is not None: + in_subnet = Signal(1, reset_less=True) + + self.sync += in_subnet.eq((sink.ip_address & netmask) == netaddress) + self.comb += If(in_subnet, + arp_table.request.ip_address.eq(sink.ip_address) + ).Else( + arp_table.request.ip_address.eq(gateway) + ) + else: + self.comb += arp_table.request.ip_address.eq(sink.ip_address) + fsm.act("SEND_MAC_ADDRESS_REQUEST", arp_table.request.valid.eq(1), If(arp_table.request.valid & arp_table.request.ready, @@ -258,7 +283,21 @@ def __init__(self, mac_address, ip_address, with_broadcast=True, dw=8): # IP ----------------------------------------------------------------------------------------------- class LiteEthIP(LiteXModule): - def __init__(self, mac, mac_address, ip_address, arp_table, with_broadcast=True, dw=8): + def __init__(self, mac, mac_address, ip_address, arp_table, dw=8, + gateway = None, + netaddress = None, + netmask = None, + with_broadcast=True + ): + self.submodules.tx = tx = LiteEthIPTX( + mac_address, + ip_address, + arp_table, + gateway=gateway, + netaddress=netaddress, + netmask=netmask, + dw=dw + ) self.tx = tx = LiteEthIPTX(mac_address, ip_address, arp_table, dw=dw) self.rx = rx = LiteEthIPRX(mac_address, ip_address, with_broadcast, dw=dw) mac_port = mac.crossbar.get_port(ethernet_type_ip, dw)