2015-01-28 18:25:55 -05:00
|
|
|
from liteeth.common import *
|
|
|
|
from liteeth.generic.depacketizer import LiteEthDepacketizer
|
|
|
|
from liteeth.generic.packetizer import LiteEthPacketizer
|
|
|
|
|
|
|
|
class LiteEthIPV4Depacketizer(LiteEthDepacketizer):
|
|
|
|
def __init__(self):
|
|
|
|
LiteEthDepacketizer.__init__(self,
|
|
|
|
eth_mac_description(8),
|
|
|
|
eth_ipv4_description(8),
|
|
|
|
ipv4_header,
|
2015-01-29 18:03:16 -05:00
|
|
|
ipv4_header_len)
|
2015-01-28 18:25:55 -05:00
|
|
|
|
2015-01-29 18:03:16 -05:00
|
|
|
class LiteEthIPV4Packetizer(LiteEthPacketizer):
|
2015-01-28 18:25:55 -05:00
|
|
|
def __init__(self):
|
2015-01-29 18:03:16 -05:00
|
|
|
LiteEthPacketizer.__init__(self,
|
2015-01-28 18:25:55 -05:00
|
|
|
eth_ipv4_description(8),
|
|
|
|
eth_mac_description(8),
|
|
|
|
ipv4_header,
|
2015-01-29 18:03:16 -05:00
|
|
|
ipv4_header_len)
|
2015-01-30 07:23:06 -05:00
|
|
|
|
|
|
|
class LiteEthIPTX(Module):
|
|
|
|
def __init__(self, ip_address, arp_table):
|
2015-01-30 11:44:44 -05:00
|
|
|
self.sink = Sink(eth_ipv4_description(8))
|
2015-01-30 07:23:06 -05:00
|
|
|
self.source = Source(eth_mac_description(8))
|
|
|
|
###
|
|
|
|
packetizer = LiteEthIPV4Packetizer()
|
|
|
|
self.submodules += packetizer
|
2015-01-30 11:44:44 -05:00
|
|
|
self.comb += Record.connect(self.sink, packetizer.sink)
|
|
|
|
sink = packetizer.source
|
2015-01-30 07:23:06 -05:00
|
|
|
|
|
|
|
fsm = FSM(reset_state="IDLE")
|
|
|
|
self.submodules += fsm
|
|
|
|
fsm.act("IDLE",
|
|
|
|
sink.ack.eq(1),
|
|
|
|
If(sink.stb & sink.sop,
|
|
|
|
sink.ack.eq(0),
|
|
|
|
NextState("SEND_MAC_ADDRESS_REQUEST")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("SEND_MAC_ADDRESS_REQUEST",
|
|
|
|
arp_table.request.stb.eq(1),
|
2015-01-30 11:44:44 -05:00
|
|
|
arp_table.request.ip_address.eq(self.sink.destination_ip_address),
|
2015-01-30 07:23:06 -05:00
|
|
|
If(arp_table.request.stb & arp_table.request.ack,
|
|
|
|
NextState("WAIT_MAC_ADDRESS_RESPONSE")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("WAIT_MAC_ADDRESS_RESPONSE",
|
|
|
|
# XXX add timeout
|
|
|
|
If(arp_table.response.stb,
|
2015-01-30 11:44:44 -05:00
|
|
|
arp_table.response.ack.eq(1),
|
2015-01-30 07:23:06 -05:00
|
|
|
# XXX manage failed
|
|
|
|
NextState("SEND")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("SEND",
|
|
|
|
Record.connect(packetizer.source, self.source),
|
|
|
|
# XXX compute check sum
|
|
|
|
|
|
|
|
# XXX add timeout
|
|
|
|
If(arp_table.response.stb,
|
|
|
|
# XXX manage failed
|
|
|
|
NextState("SEND")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
class LiteEthIPRX(Module):
|
|
|
|
def __init__(self, ip_address):
|
|
|
|
self.sink = Sink(eth_mac_description(8))
|
|
|
|
self.source = source = Source(eth_ipv4_description(8))
|
|
|
|
###
|
|
|
|
depacketizer = LiteEthIPV4Depacketizer()
|
|
|
|
self.submodules += depacketizer
|
|
|
|
self.comb += Record.connect(self.sink, depacketizer.sink)
|
|
|
|
sink = depacketizer.source
|
|
|
|
|
|
|
|
fsm = FSM(reset_state="IDLE")
|
|
|
|
self.submodules += fsm
|
|
|
|
fsm.act("IDLE",
|
|
|
|
sink.ack.eq(1),
|
|
|
|
If(sink.stb & sink.sop,
|
|
|
|
sink.ack.eq(0),
|
|
|
|
NextState("CHECK")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
valid = Signal()
|
|
|
|
self.comb += valid.eq(1) # XXX FIXME
|
|
|
|
fsm.act("CHECK",
|
|
|
|
If(valid,
|
|
|
|
NextState("PRESENT")
|
|
|
|
).Else(
|
|
|
|
NextState("DROP")
|
|
|
|
)
|
|
|
|
),
|
|
|
|
fsm.act("PRESENT",
|
|
|
|
Record.connect(sink, source),
|
|
|
|
If(source.stb & source.eop & source.ack,
|
|
|
|
NextState("IDLE")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("DROP",
|
|
|
|
sink.ack.eq(1),
|
|
|
|
If(source.stb & source.eop & source.ack,
|
|
|
|
NextState("IDLE")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
class LiteEthIP(Module):
|
2015-01-30 10:27:56 -05:00
|
|
|
def __init__(self, mac, ip_address, arp_table):
|
2015-01-30 07:23:06 -05:00
|
|
|
self.submodules.tx = LiteEthIPTX(ip_address, arp_table)
|
|
|
|
self.submodules.rx = LiteEthIPRX(ip_address)
|
2015-01-30 10:27:56 -05:00
|
|
|
mac_port = mac.crossbar.get_port(ethernet_type_ip)
|
|
|
|
self.comb += [
|
|
|
|
Record.connect(self.tx.source, mac_port.sink),
|
|
|
|
Record.connect(mac_port.source, self.rx.sink)
|
|
|
|
]
|
|
|
|
self.sink, self.source = self.tx.sink, self.rx.source
|