Skip to content

Commit

Permalink
core/ip.py: Add gateway support
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanG077 committed Jul 16, 2023
1 parent 64cceb2 commit b09150f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
12 changes: 12 additions & 0 deletions liteeth/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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).
Expand All @@ -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,
Expand All @@ -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,
Expand Down
48 changes: 44 additions & 4 deletions liteeth/core/ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -148,7 +161,20 @@ 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:
masked_ip = Signal(32, reset_less=True)
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,
Expand Down Expand Up @@ -258,7 +284,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)
Expand Down

0 comments on commit b09150f

Please sign in to comment.