litex/liteeth/core/icmp/__init__.py

138 lines
4.1 KiB
Python
Raw Normal View History

2015-02-05 19:38:30 -05:00
from liteeth.common import *
from liteeth.generic.depacketizer import LiteEthDepacketizer
from liteeth.generic.packetizer import LiteEthPacketizer
class LiteEthICMPDepacketizer(LiteEthDepacketizer):
def __init__(self):
LiteEthDepacketizer.__init__(self,
eth_ipv4_user_description(8),
eth_icmp_description(8),
icmp_header,
icmp_header_len)
class LiteEthICMPPacketizer(LiteEthPacketizer):
def __init__(self):
LiteEthPacketizer.__init__(self,
eth_icmp_description(8),
eth_ipv4_user_description(8),
icmp_header,
icmp_header_len)
class LiteEthICMPTX(Module):
def __init__(self, ip_address):
2015-02-09 03:26:56 -05:00
self.sink = sink = Sink(eth_icmp_user_description(8))
self.source = source = Source(eth_ipv4_user_description(8))
2015-02-05 19:38:30 -05:00
###
2015-02-09 03:26:56 -05:00
self.submodules.packetizer = packetizer = LiteEthICMPPacketizer()
2015-02-05 19:38:30 -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.msgtype.eq(sink.msgtype),
packetizer.sink.code.eq(sink.code),
packetizer.sink.checksum.eq(sink.checksum),
packetizer.sink.quench.eq(sink.quench),
packetizer.sink.data.eq(sink.data)
2015-02-05 19:38:30 -05:00
]
self.submodules.fsm = fsm = FSM(reset_state="IDLE")
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-05 19:38:30 -05:00
NextState("SEND")
)
)
fsm.act("SEND",
2015-02-09 03:26:56 -05:00
Record.connect(packetizer.source, source),
source.length.eq(sink.length + icmp_header_len),
source.protocol.eq(icmp_protocol),
source.ip_address.eq(sink.ip_address),
If(source.stb & source.eop & source.ack,
2015-02-05 19:38:30 -05:00
NextState("IDLE")
)
)
class LiteEthICMPRX(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-05 19:38:30 -05:00
self.source = source = Source(eth_icmp_user_description(8))
###
2015-02-09 03:26:56 -05:00
self.submodules.depacketizer = depacketizer = LiteEthICMPDepacketizer()
self.comb += Record.connect(sink, depacketizer.sink)
2015-02-05 19:38:30 -05:00
self.submodules.fsm = fsm = FSM(reset_state="IDLE")
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-05 19:38:30 -05:00
NextState("CHECK")
)
)
valid = Signal()
self.sync += valid.eq(
2015-02-09 03:26:56 -05:00
depacketizer.source.stb &
(sink.protocol == icmp_protocol)
2015-02-05 19:38:30 -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.msgtype.eq(depacketizer.source.msgtype),
source.code.eq(depacketizer.source.code),
source.checksum.eq(depacketizer.source.checksum),
source.quench.eq(depacketizer.source.quench),
source.ip_address.eq(sink.ip_address),
source.length.eq(sink.length - icmp_header_len),
source.data.eq(depacketizer.source.data),
source.error.eq(depacketizer.source.error)
2015-02-05 19:38:30 -05:00
]
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-05 19:38:30 -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-05 19:38:30 -05:00
NextState("IDLE")
)
)
class LiteEthICMPEcho(Module):
def __init__(self):
2015-02-09 03:26:56 -05:00
self.sink = sink = Sink(eth_icmp_user_description(8))
self.source = source = Source(eth_icmp_user_description(8))
2015-02-05 19:38:30 -05:00
###
self.submodules.buffer = PacketBuffer(eth_icmp_user_description(8), 128, 2)
2015-02-05 19:38:30 -05:00
self.comb += [
Record.connect(sink, self.buffer.sink),
Record.connect(self.buffer.source, source),
2015-02-05 19:38:30 -05:00
self.source.msgtype.eq(0x0),
self.source.checksum.eq(~((~self.buffer.source.checksum)-0x0800))
2015-02-05 19:38:30 -05:00
]
class LiteEthICMP(Module):
def __init__(self, ip, ip_address):
2015-02-09 03:26:56 -05:00
self.submodules.tx = tx = LiteEthICMPTX(ip_address)
self.submodules.rx = rx = LiteEthICMPRX(ip_address)
self.submodules.echo = echo = LiteEthICMPEcho()
2015-02-06 06:22:24 -05:00
self.comb += [
2015-02-09 03:26:56 -05:00
Record.connect(rx.source, echo.sink),
Record.connect(echo.source, tx.sink)
2015-02-06 06:22:24 -05:00
]
2015-02-05 19:38:30 -05:00
ip_port = ip.crossbar.get_port(icmp_protocol)
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-05 19:38:30 -05:00
]