143 lines
5.0 KiB
VHDL
143 lines
5.0 KiB
VHDL
-- -------------------------------------------------------------------------- --
|
|
-- TRASHERNET - A Trashy Ethernet Stack for FPGAs --
|
|
-- -------------------------------------------------------------------------- --
|
|
-- trashernet_ipv4.vhd : Ethernet OSI Layer 3, Network (IPv4)
|
|
-- Implements packet handling and IP-Layer en-/decoding.
|
|
-- -------------------------------------------------------------------------- --
|
|
-- Author : Markus Koch <markus@notsyncing.net>
|
|
-- Contributors : None
|
|
-- License : Mozilla Public License (MPL) Version 2
|
|
-- -------------------------------------------------------------------------- --
|
|
|
|
library ieee;
|
|
use ieee.std_logic_1164.all;
|
|
use ieee.numeric_std.all;
|
|
|
|
use work.trashernet_pkg.all;
|
|
|
|
entity trashernet_ipv4 is
|
|
port(
|
|
-- Global
|
|
clk : in std_logic; -- Global clock
|
|
rst : in std_logic; -- Asynchronous reset
|
|
|
|
-- Configuration
|
|
ipv4_config : in ipv4_configuration_t; -- Trashernet IP configuration
|
|
|
|
-- ARP application interface
|
|
arp_out : in arp_out_t; -- ARP IF (out from ARP)
|
|
arp_in : out arp_in_t; -- ARP IF (into ARP)
|
|
|
|
-- Ethernet II application interface
|
|
ethernet_ii_out : in ethernet_ii_out_t; -- Ethernet II IF (out from MAC)
|
|
ethernet_ii_in : out ethernet_ii_in_t; -- Ethernet II IF (into MAC)
|
|
|
|
-- IPv4 application interface
|
|
ipv4_out : out ipv4_out_t; -- IPv4 IF (out from IP)
|
|
ipv4_in : in ipv4_in_t -- IPv4 IF (into IP)
|
|
);
|
|
end entity trashernet_ipv4;
|
|
|
|
architecture rtl of trashernet_ipv4 is
|
|
begin
|
|
rx : block
|
|
constant BYTECOUNT_HEADER : integer := 20; -- Mandatory header fields
|
|
|
|
signal sr : byte_vector(0 to BYTECOUNT_HEADER - 1);
|
|
alias sr_version is sr(0)(7 downto 4);
|
|
alias sr_ihl is sr(0)(3 downto 0);
|
|
alias sr_dscp is sr(1)(7 downto 2);
|
|
alias sr_ecn is sr(1)(1 downto 0);
|
|
alias sr_total_length is sr(2 to 3);
|
|
alias sr_identification is sr(4 to 5);
|
|
alias sr_flags is sr(6)(7 downto 5);
|
|
signal sr_fragment_offset : std_logic_vector(12 downto 0);
|
|
alias sr_ttl is sr(8);
|
|
alias sr_protocol is sr(9);
|
|
alias sr_header_cks is sr(10 to 11);
|
|
alias sr_source_ip is sr(12 to 15);
|
|
alias sr_destination_ip is sr(16 to 19);
|
|
|
|
signal header_ok : std_logic;
|
|
|
|
signal bytecount : integer range 0 to 65535;
|
|
signal shifted : std_logic;
|
|
signal block_done : std_logic;
|
|
|
|
type state_t is (HEADER, OPT, PAYLOAD, DONE);
|
|
signal state : state_t;
|
|
begin
|
|
sr_fragment_offset <= sr(6)(4 downto 0) & sr(7);
|
|
|
|
block_done <= '1' when bytecount = 0 else '0';
|
|
header_ok <= '1' when --
|
|
(std_logic_vector'(sr_flags(7) & sr_flags(5)) = "00") and -- Reserved = 0, MF unset
|
|
(to_integer(unsigned(sr_fragment_offset)) = 0) and -- No fragment offset -> not last fragment
|
|
(sr_destination_ip = ipv4_config.ip_address) else -- It's addressed to us. TODO: We do not support broadcasts
|
|
'0';
|
|
rx_fsm : process(clk, rst) is
|
|
begin
|
|
if rst then
|
|
state <= HEADER;
|
|
shifted <= '0';
|
|
ipv4_out.rx_ok_stb <= '0';
|
|
ipv4_out.rx_error_stb <= '0';
|
|
ipv4_out.rx_header_rcv <= '0';
|
|
bytecount <= BYTECOUNT_HEADER;
|
|
|
|
elsif rising_edge(clk) then
|
|
ipv4_out.rx_ok_stb <= '0';
|
|
ipv4_out.rx_error_stb <= '0';
|
|
ipv4_out.rx_header_rcv <= '0';
|
|
|
|
shifted <= ethernet_ii_out.rx_data_valid;
|
|
|
|
if (ethernet_ii_out.rx_crc_ok or ethernet_ii_out.rx_crc_error) then
|
|
state <= HEADER;
|
|
bytecount <= BYTECOUNT_HEADER;
|
|
|
|
ipv4_out.rx_error_stb <= ethernet_ii_out.rx_crc_error or to_std_logic(state = PAYLOAD);
|
|
ipv4_out.rx_ok_stb <= ethernet_ii_out.rx_crc_ok and to_std_logic(state = DONE);
|
|
end if;
|
|
if (ethernet_ii_out.rx_data_valid = '1') and (bytecount > 0) then
|
|
if state = HEADER then
|
|
sr <= sr(sr'low + 1 to sr'high) & ethernet_ii_out.rx_data;
|
|
end if;
|
|
bytecount <= bytecount - 1;
|
|
end if;
|
|
|
|
case state is
|
|
when HEADER =>
|
|
if block_done then
|
|
bytecount <= to_integer(unsigned(sr_ihl) - 5) * 4; -- five 32-bit words is the header itself
|
|
if header_ok then
|
|
state <= OPT;
|
|
ipv4_out.rx_header_rcv <= '1';
|
|
end if;
|
|
end if;
|
|
|
|
when OPT =>
|
|
if block_done then
|
|
bytecount <= to_integer(unsigned(std_logic_vector'(sr_total_length(2) & sr_total_length(3)))) - to_integer(unsigned(sr_ihl)) * 4;
|
|
state <= PAYLOAD;
|
|
end if;
|
|
|
|
when PAYLOAD =>
|
|
if block_done then
|
|
state <= DONE;
|
|
end if;
|
|
|
|
when DONE => -- @suppress "Dead state 'DONE'": Outgoing state transition is outside of case statement
|
|
null; -- We just wait here until the MAC gives us a CRC OK/error
|
|
end case;
|
|
end if;
|
|
end process rx_fsm;
|
|
|
|
ipv4_out.rx_data <= ethernet_ii_out.rx_data;
|
|
ipv4_out.rx_data_valid <= ethernet_ii_out.rx_data_valid when state = PAYLOAD else '0';
|
|
ipv4_out.rx_ip_address <= sr_source_ip;
|
|
ipv4_out.rx_protocol <= sr_protocol;
|
|
|
|
end block rx;
|
|
end architecture rtl;
|