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 LiteEthUDPDepacketizer(LiteEthDepacketizer):
|
|
|
|
def __init__(self):
|
|
|
|
LiteEthDepacketizer.__init__(self,
|
2015-02-04 14:50:49 -05:00
|
|
|
eth_ipv4_user_description(8),
|
2015-01-28 18:25:55 -05:00
|
|
|
eth_udp_description(8),
|
|
|
|
udp_header,
|
2015-01-29 18:03:16 -05:00
|
|
|
udp_header_len)
|
2015-01-28 18:25:55 -05:00
|
|
|
|
2015-01-29 18:03:16 -05:00
|
|
|
class LiteEthUDPPacketizer(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_udp_description(8),
|
2015-02-04 14:50:49 -05:00
|
|
|
eth_ipv4_user_description(8),
|
2015-01-28 18:25:55 -05:00
|
|
|
udp_header,
|
2015-01-29 18:03:16 -05:00
|
|
|
udp_header_len)
|
2015-02-04 14:50:49 -05:00
|
|
|
|
|
|
|
class LiteEthUDPTX(Module):
|
|
|
|
def __init__(self, ip_address):
|
|
|
|
self.sink = Sink(eth_udp_user_description(8))
|
|
|
|
self.source = Source(eth_ipv4_user_description(8))
|
|
|
|
###
|
2015-02-04 15:15:01 -05:00
|
|
|
packetizer = LiteEthUDPPacketizer()
|
2015-02-04 14:50:49 -05:00
|
|
|
self.submodules += packetizer
|
|
|
|
self.comb += [
|
|
|
|
packetizer.sink.stb.eq(self.sink.stb),
|
|
|
|
packetizer.sink.sop.eq(self.sink.sop),
|
|
|
|
packetizer.sink.eop.eq(self.sink.eop),
|
2015-02-04 16:15:59 -05:00
|
|
|
self.sink.ack.eq(packetizer.sink.ack),
|
2015-02-04 16:51:11 -05:00
|
|
|
packetizer.sink.src_port.eq(self.sink.src_port),
|
|
|
|
packetizer.sink.dst_port.eq(self.sink.dst_port),
|
2015-02-04 14:50:49 -05:00
|
|
|
packetizer.sink.length.eq(self.sink.length + udp_header_len),
|
|
|
|
packetizer.sink.checksum.eq(0),
|
2015-02-04 16:15:59 -05:00
|
|
|
packetizer.sink.data.eq(self.sink.data)
|
2015-02-04 14:50:49 -05:00
|
|
|
]
|
|
|
|
sink = packetizer.source
|
|
|
|
|
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",
|
|
|
|
sink.ack.eq(1),
|
|
|
|
If(sink.stb & sink.sop,
|
|
|
|
sink.ack.eq(0),
|
|
|
|
NextState("SEND")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("SEND",
|
|
|
|
Record.connect(packetizer.source, self.source),
|
2015-02-04 15:15:01 -05:00
|
|
|
self.source.length.eq(packetizer.sink.length + ipv4_header_len),
|
2015-02-04 14:50:49 -05:00
|
|
|
self.source.protocol.eq(udp_protocol),
|
|
|
|
self.source.ip_address.eq(self.sink.ip_address),
|
|
|
|
If(self.source.stb & self.source.eop & self.source.ack,
|
|
|
|
NextState("IDLE")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
class LiteEthUDPRX(Module):
|
|
|
|
def __init__(self, ip_address):
|
|
|
|
self.sink = Sink(eth_ipv4_user_description(8))
|
|
|
|
self.source = source = Source(eth_udp_user_description(8))
|
|
|
|
###
|
2015-02-04 15:15:01 -05:00
|
|
|
depacketizer = LiteEthUDPDepacketizer()
|
2015-02-04 14:50:49 -05:00
|
|
|
self.submodules += depacketizer
|
|
|
|
self.comb += Record.connect(self.sink, depacketizer.sink)
|
|
|
|
sink = depacketizer.source
|
|
|
|
|
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",
|
|
|
|
sink.ack.eq(1),
|
|
|
|
If(sink.stb & sink.sop,
|
|
|
|
sink.ack.eq(0),
|
|
|
|
NextState("CHECK")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
valid = Signal()
|
|
|
|
self.comb += valid.eq(
|
|
|
|
sink.stb &
|
2015-02-04 15:15:01 -05:00
|
|
|
(self.sink.protocol == udp_protocol) &
|
|
|
|
(self.sink.ip_address == ip_address)
|
2015-02-04 14:50:49 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
fsm.act("CHECK",
|
|
|
|
If(valid,
|
|
|
|
NextState("PRESENT")
|
|
|
|
).Else(
|
|
|
|
NextState("DROP")
|
|
|
|
)
|
2015-02-05 18:29:30 -05:00
|
|
|
)
|
|
|
|
self.comb += [
|
2015-02-04 14:50:49 -05:00
|
|
|
source.sop.eq(sink.sop),
|
|
|
|
source.eop.eq(sink.eop),
|
2015-02-04 16:51:11 -05:00
|
|
|
source.src_port.eq(sink.src_port),
|
|
|
|
source.dst_port.eq(sink.dst_port),
|
2015-02-04 14:50:49 -05:00
|
|
|
source.ip_address.eq(0),
|
|
|
|
source.length.eq(sink.length - udp_header_len),
|
|
|
|
source.data.eq(sink.data),
|
2015-02-05 18:29:30 -05:00
|
|
|
source.error.eq(sink.error)
|
|
|
|
]
|
|
|
|
fsm.act("PRESENT",
|
|
|
|
source.stb.eq(sink.stb),
|
|
|
|
sink.ack.eq(source.ack),
|
2015-02-04 14:50:49 -05:00
|
|
|
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 LiteEthUDP(Module):
|
2015-02-04 15:15:01 -05:00
|
|
|
def __init__(self, ip, ip_address):
|
2015-02-04 14:50:49 -05:00
|
|
|
self.submodules.tx = LiteEthUDPTX(ip_address)
|
|
|
|
self.submodules.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-05 18:54:05 -05:00
|
|
|
Record.connect(self.tx.source, ip_port.sink),
|
|
|
|
Record.connect(ip_port.source, self.rx.sink)
|
2015-02-04 15:15:01 -05:00
|
|
|
]
|
2015-02-04 14:50:49 -05:00
|
|
|
self.sink, self.source = self.tx.sink, self.rx.source
|