litex/liteeth/core/udp/__init__.py

127 lines
3.8 KiB
Python
Raw Normal View History

from liteeth.common import *
from liteeth.generic.depacketizer import LiteEthDepacketizer
from liteeth.generic.packetizer import LiteEthPacketizer
class LiteEthUDPDepacketizer(LiteEthDepacketizer):
def __init__(self):
LiteEthDepacketizer.__init__(self,
2015-02-04 14:50:49 -05:00
eth_ipv4_user_description(8),
eth_udp_description(8),
udp_header,
udp_header_len)
class LiteEthUDPPacketizer(LiteEthPacketizer):
def __init__(self):
LiteEthPacketizer.__init__(self,
eth_udp_description(8),
2015-02-04 14:50:49 -05:00
eth_ipv4_user_description(8),
udp_header,
udp_header_len)
2015-02-04 14:50:49 -05:00
class LiteEthUDPTX(Module):
def __init__(self, ip_address):
2015-02-09 03:26:56 -05:00
self.sink = sink = Sink(eth_udp_user_description(8))
self.source = source = Source(eth_ipv4_user_description(8))
2015-02-04 14:50:49 -05:00
###
2015-02-09 03:26:56 -05:00
self.submodules.packetizer = packetizer = LiteEthUDPPacketizer()
2015-02-04 14:50:49 -05:00
self.comb += [
2015-02-09 03:26:56 -05:00
packetizer.sink.stb.eq(sink.stb),
packetizer.sink.sop.eq(sink.sop),
packetizer.sink.eop.eq(sink.eop),
sink.ack.eq(packetizer.sink.ack),
packetizer.sink.src_port.eq(sink.src_port),
packetizer.sink.dst_port.eq(sink.dst_port),
packetizer.sink.length.eq(sink.length + udp_header_len),
packetizer.sink.checksum.eq(0), # Disabled (MAC CRC is enough)
packetizer.sink.data.eq(sink.data)
2015-02-04 14:50:49 -05:00
]
2015-02-05 05:58:40 -05:00
self.submodules.fsm = fsm = FSM(reset_state="IDLE")
2015-02-04 14:50:49 -05:00
fsm.act("IDLE",
2015-02-09 03:26:56 -05:00
packetizer.source.ack.eq(1),
If(packetizer.source.stb & packetizer.source.sop,
packetizer.source.ack.eq(0),
2015-02-04 14:50:49 -05:00
NextState("SEND")
)
)
fsm.act("SEND",
2015-02-09 03:26:56 -05:00
Record.connect(packetizer.source, source),
source.length.eq(packetizer.sink.length),
source.protocol.eq(udp_protocol),
source.ip_address.eq(sink.ip_address),
If(source.stb & source.eop & source.ack,
2015-02-04 14:50:49 -05:00
NextState("IDLE")
)
)
class LiteEthUDPRX(Module):
def __init__(self, ip_address):
2015-02-09 03:26:56 -05:00
self.sink = sink = Sink(eth_ipv4_user_description(8))
2015-02-04 14:50:49 -05:00
self.source = source = Source(eth_udp_user_description(8))
###
2015-02-09 03:26:56 -05:00
self.submodules.depacketizer = depacketizer = LiteEthUDPDepacketizer()
self.comb += Record.connect(sink, depacketizer.sink)
2015-02-04 14:50:49 -05:00
2015-02-05 05:58:40 -05:00
self.submodules.fsm = fsm = FSM(reset_state="IDLE")
2015-02-04 14:50:49 -05:00
fsm.act("IDLE",
2015-02-09 03:26:56 -05:00
depacketizer.source.ack.eq(1),
If(depacketizer.source.stb & depacketizer.source.sop,
depacketizer.source.ack.eq(0),
2015-02-04 14:50:49 -05:00
NextState("CHECK")
)
)
valid = Signal()
self.comb += valid.eq(
2015-02-09 03:26:56 -05:00
depacketizer.source.stb &
(sink.protocol == udp_protocol)
2015-02-04 14:50:49 -05:00
)
fsm.act("CHECK",
If(valid,
NextState("PRESENT")
).Else(
NextState("DROP")
)
)
self.comb += [
2015-02-09 03:26:56 -05:00
source.sop.eq(depacketizer.source.sop),
source.eop.eq(depacketizer.source.eop),
source.src_port.eq(depacketizer.source.src_port),
source.dst_port.eq(depacketizer.source.dst_port),
source.ip_address.eq(sink.ip_address),
source.length.eq(depacketizer.source.length - udp_header_len),
source.data.eq(depacketizer.source.data),
source.error.eq(depacketizer.source.error)
]
fsm.act("PRESENT",
2015-02-09 03:26:56 -05:00
source.stb.eq(depacketizer.source.stb),
depacketizer.source.ack.eq(source.ack),
2015-02-04 14:50:49 -05:00
If(source.stb & source.eop & source.ack,
NextState("IDLE")
)
)
fsm.act("DROP",
2015-02-09 03:26:56 -05:00
depacketizer.source.ack.eq(1),
If(depacketizer.source.stb & depacketizer.source.eop & depacketizer.source.ack,
2015-02-04 14:50:49 -05:00
NextState("IDLE")
)
)
class LiteEthUDP(Module):
def __init__(self, ip, ip_address, with_loopback):
2015-02-09 03:26:56 -05:00
self.submodules.tx = tx = LiteEthUDPTX(ip_address)
self.submodules.rx = rx = LiteEthUDPRX(ip_address)
2015-02-05 18:54:05 -05:00
ip_port = ip.crossbar.get_port(udp_protocol)
2015-02-04 15:15:01 -05:00
self.comb += [
2015-02-09 03:26:56 -05:00
Record.connect(tx.source, ip_port.sink),
Record.connect(ip_port.source, rx.sink)
2015-02-04 15:15:01 -05:00
]
if with_loopback:
2015-02-09 03:26:56 -05:00
self.submodules.fifo = fifo = SyncFIFO(eth_udp_user_description(8), 2048, buffered=True)
self.comb += [
2015-02-09 03:26:56 -05:00
Record.connect(rx.source, fifo.sink),
Record.connect(fifo.source, tx.sink)
]
else:
self.sink, self.source = self.tx.sink, self.rx.source