2015-01-28 18:25:55 -05:00
|
|
|
from liteeth.common import *
|
|
|
|
from liteeth.generic.depacketizer import LiteEthDepacketizer
|
|
|
|
from liteeth.generic.packetizer import LiteEthPacketizer
|
2015-02-05 18:54:05 -05:00
|
|
|
from liteeth.core.ip.crossbar import LiteEthIPV4Crossbar
|
2015-01-28 18:25:55 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
2015-02-04 13:35:38 -05:00
|
|
|
class LiteEthIPV4Checksum(Module):
|
|
|
|
def __init__(self, skip_header=False):
|
|
|
|
self.header = Signal(ipv4_header_len*8)
|
|
|
|
self.value = Signal(16)
|
|
|
|
|
|
|
|
s = Signal(17)
|
|
|
|
r = Signal(17)
|
|
|
|
for i in range(ipv4_header_len//2):
|
|
|
|
if skip_header and i == 5:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
s_next = Signal(17)
|
|
|
|
r_next = Signal(17)
|
|
|
|
self.comb += [
|
|
|
|
s_next.eq(r + self.header[i*16:(i+1)*16]),
|
|
|
|
r_next.eq(Cat(s_next[:16]+s_next[16], Signal()))
|
|
|
|
]
|
|
|
|
s, r = s_next, r_next
|
|
|
|
self.comb += self.value.eq(~Cat(r[8:16], r[:8]))
|
|
|
|
|
2015-01-30 07:23:06 -05:00
|
|
|
class LiteEthIPTX(Module):
|
2015-01-30 12:32:55 -05:00
|
|
|
def __init__(self, mac_address, ip_address, arp_table):
|
2015-01-30 13:14:05 -05:00
|
|
|
self.sink = Sink(eth_ipv4_user_description(8))
|
2015-01-30 07:23:06 -05:00
|
|
|
self.source = Source(eth_mac_description(8))
|
2015-02-05 18:05:25 -05:00
|
|
|
self.target_unreachable = Signal()
|
2015-01-30 07:23:06 -05:00
|
|
|
###
|
|
|
|
packetizer = LiteEthIPV4Packetizer()
|
|
|
|
self.submodules += packetizer
|
2015-01-30 13:14:05 -05:00
|
|
|
self.comb += [
|
2015-02-04 10:31:37 -05:00
|
|
|
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-05 14:10:09 -05:00
|
|
|
packetizer.sink.target_ip.eq(self.sink.ip_address),
|
2015-02-04 10:31:37 -05:00
|
|
|
packetizer.sink.protocol.eq(self.sink.protocol),
|
|
|
|
packetizer.sink.total_length.eq(self.sink.length + (0x5*4)),
|
|
|
|
packetizer.sink.version.eq(0x4), # ipv4
|
|
|
|
packetizer.sink.ihl.eq(0x5), # 20 bytes
|
2015-01-30 13:14:05 -05:00
|
|
|
packetizer.sink.identification.eq(0),
|
2015-02-04 16:51:11 -05:00
|
|
|
packetizer.sink.ttl.eq(0x80),
|
|
|
|
packetizer.sink.sender_ip.eq(ip_address),
|
2015-02-04 16:15:59 -05:00
|
|
|
packetizer.sink.data.eq(self.sink.data)
|
2015-01-30 13:14:05 -05:00
|
|
|
]
|
2015-01-30 11:44:44 -05:00
|
|
|
sink = packetizer.source
|
2015-01-30 07:23:06 -05:00
|
|
|
|
2015-02-04 13:35:38 -05:00
|
|
|
checksum = LiteEthIPV4Checksum(skip_header=True)
|
|
|
|
self.submodules += checksum
|
|
|
|
self.comb += [
|
|
|
|
checksum.header.eq(packetizer.header),
|
2015-02-04 16:51:11 -05:00
|
|
|
packetizer.sink.checksum.eq(checksum.value)
|
2015-02-04 13:35:38 -05:00
|
|
|
]
|
|
|
|
|
2015-02-04 16:51:11 -05:00
|
|
|
target_mac = Signal(48)
|
2015-01-30 12:32:55 -05:00
|
|
|
|
2015-02-05 05:58:40 -05:00
|
|
|
self.submodules.fsm = fsm = FSM(reset_state="IDLE")
|
2015-01-30 07:23:06 -05:00
|
|
|
fsm.act("IDLE",
|
|
|
|
sink.ack.eq(1),
|
|
|
|
If(sink.stb & sink.sop,
|
|
|
|
sink.ack.eq(0),
|
|
|
|
NextState("SEND_MAC_ADDRESS_REQUEST")
|
|
|
|
)
|
|
|
|
)
|
2015-02-05 18:29:30 -05:00
|
|
|
self.comb += arp_table.request.ip_address.eq(self.sink.ip_address)
|
2015-01-30 07:23:06 -05:00
|
|
|
fsm.act("SEND_MAC_ADDRESS_REQUEST",
|
|
|
|
arp_table.request.stb.eq(1),
|
|
|
|
If(arp_table.request.stb & arp_table.request.ack,
|
|
|
|
NextState("WAIT_MAC_ADDRESS_RESPONSE")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("WAIT_MAC_ADDRESS_RESPONSE",
|
|
|
|
If(arp_table.response.stb,
|
2015-01-30 11:44:44 -05:00
|
|
|
arp_table.response.ack.eq(1),
|
2015-02-05 18:05:25 -05:00
|
|
|
If(arp_table.response.failed,
|
|
|
|
self.target_unreachable.eq(1),
|
|
|
|
NextState("DROP"),
|
|
|
|
).Else(
|
|
|
|
NextState("SEND")
|
|
|
|
)
|
2015-01-30 07:23:06 -05:00
|
|
|
)
|
|
|
|
)
|
2015-02-04 16:51:11 -05:00
|
|
|
self.sync += If(arp_table.response.stb, target_mac.eq(arp_table.response.mac_address))
|
2015-01-30 07:23:06 -05:00
|
|
|
fsm.act("SEND",
|
|
|
|
Record.connect(packetizer.source, self.source),
|
2015-01-30 12:32:55 -05:00
|
|
|
self.source.ethernet_type.eq(ethernet_type_ip),
|
2015-02-04 16:51:11 -05:00
|
|
|
self.source.target_mac.eq(target_mac),
|
|
|
|
self.source.sender_mac.eq(mac_address),
|
2015-01-30 12:32:55 -05:00
|
|
|
If(self.source.stb & self.source.eop & self.source.ack,
|
2015-02-05 18:05:25 -05:00
|
|
|
NextState("IDLE")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("DROP",
|
|
|
|
packetizer.source.ack.eq(1),
|
|
|
|
If(packetizer.source.stb & packetizer.source.eop & packetizer.source.ack,
|
2015-01-30 12:32:55 -05:00
|
|
|
NextState("IDLE")
|
2015-01-30 07:23:06 -05:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
class LiteEthIPRX(Module):
|
2015-01-30 12:32:55 -05:00
|
|
|
def __init__(self, mac_address, ip_address):
|
2015-01-30 07:23:06 -05:00
|
|
|
self.sink = Sink(eth_mac_description(8))
|
2015-01-30 13:14:05 -05:00
|
|
|
self.source = source = Source(eth_ipv4_user_description(8))
|
2015-01-30 07:23:06 -05:00
|
|
|
###
|
|
|
|
depacketizer = LiteEthIPV4Depacketizer()
|
|
|
|
self.submodules += depacketizer
|
|
|
|
self.comb += Record.connect(self.sink, depacketizer.sink)
|
|
|
|
sink = depacketizer.source
|
|
|
|
|
2015-02-04 13:42:50 -05:00
|
|
|
checksum = LiteEthIPV4Checksum(skip_header=False)
|
|
|
|
self.submodules += checksum
|
|
|
|
self.comb += checksum.header.eq(depacketizer.header)
|
|
|
|
|
2015-02-05 05:58:40 -05:00
|
|
|
self.submodules.fsm = fsm = FSM(reset_state="IDLE")
|
2015-01-30 07:23:06 -05:00
|
|
|
fsm.act("IDLE",
|
|
|
|
sink.ack.eq(1),
|
|
|
|
If(sink.stb & sink.sop,
|
|
|
|
sink.ack.eq(0),
|
|
|
|
NextState("CHECK")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
valid = Signal()
|
2015-02-04 13:42:50 -05:00
|
|
|
self.comb += valid.eq(
|
|
|
|
sink.stb &
|
2015-02-04 16:51:11 -05:00
|
|
|
(sink.target_ip == ip_address) &
|
2015-02-04 13:42:50 -05:00
|
|
|
(sink.version == 0x4) &
|
|
|
|
(sink.ihl == 0x5) &
|
|
|
|
(checksum.value == 0)
|
|
|
|
)
|
|
|
|
|
2015-01-30 07:23:06 -05:00
|
|
|
fsm.act("CHECK",
|
|
|
|
If(valid,
|
|
|
|
NextState("PRESENT")
|
|
|
|
).Else(
|
|
|
|
NextState("DROP")
|
|
|
|
)
|
2015-02-05 18:29:30 -05:00
|
|
|
)
|
|
|
|
self.comb += [
|
2015-01-30 13:14:05 -05:00
|
|
|
source.sop.eq(sink.sop),
|
|
|
|
source.eop.eq(sink.eop),
|
2015-02-04 10:31:37 -05:00
|
|
|
source.length.eq(sink.total_length - (sink.ihl*4)),
|
2015-01-30 13:14:05 -05:00
|
|
|
source.protocol.eq(sink.protocol),
|
2015-02-06 10:58:05 -05:00
|
|
|
source.ip_address.eq(sink.sender_ip),
|
2015-01-30 13:14:05 -05:00
|
|
|
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-01-30 07:23:06 -05:00
|
|
|
If(source.stb & source.eop & source.ack,
|
|
|
|
NextState("IDLE")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("DROP",
|
|
|
|
sink.ack.eq(1),
|
2015-02-05 18:29:30 -05:00
|
|
|
If(sink.stb & sink.eop & sink.ack,
|
2015-01-30 07:23:06 -05:00
|
|
|
NextState("IDLE")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
class LiteEthIP(Module):
|
2015-01-30 12:32:55 -05:00
|
|
|
def __init__(self, mac, mac_address, ip_address, arp_table):
|
|
|
|
self.submodules.tx = LiteEthIPTX(mac_address, ip_address, arp_table)
|
|
|
|
self.submodules.rx = LiteEthIPRX(mac_address, 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)
|
|
|
|
]
|
2015-02-05 18:54:05 -05:00
|
|
|
self.submodules.crossbar = LiteEthIPV4Crossbar()
|
|
|
|
self.comb += [
|
|
|
|
Record.connect(self.crossbar.master.source, self.tx.sink),
|
|
|
|
Record.connect(self.rx.source, self.crossbar.master.sink)
|
|
|
|
]
|