-- -------------------------------------------------------------------------- -- -- 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 -- 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) := (others => x"00"); -- Init to zero to squelch metavalue warnings 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 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; 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'; 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; ipv4_out.rx_length <= unsigned(std_logic_vector'(sr_total_length(2) & sr_total_length(3))) - to_integer(unsigned(sr_ihl)) * 4; end block rx; tx : block type state_t is (IDLE, ARP, CALC_CKS, HEADER, PAYLOAD); signal state : state_t; signal full_length : ipv4_length; signal sr : byte_vector(0 to 19); signal byte_cnt : integer range 0 to sr'length - 1; signal alt_byte : std_logic; signal checksum : unsigned(20 downto 0); -- 20 Header fields -> 19 chances for carry -> 5 additional bits signal checksum_ones : unsigned(15 downto 0); constant TTL : byte := x"40"; impure function get_header(checksum : std_logic_vector(15 downto 0)) return byte_vector is variable ret : byte_vector(sr'range); begin ret := -- byte_vector'( x"45", x"00", byte(full_length(15 downto 8)), byte(full_length(7 downto 0)), -- Ver/IHL, DSCP/ECN, Len x"00", x"00", x"00", x"00" -- Identification, Flags/FragOffset ) & TTL & ipv4_in.tx_protocol & checksum(15 downto 8) & checksum(7 downto 0) & -- TTL, Protocol, Header Checksum ipv4_config.ip_address & -- Source IP ipv4_in.tx_ip_address -- Destination IP ; return ret; end function get_header; begin full_length <= sr'length + ipv4_in.tx_length; checksum_ones <= checksum(15 downto 0) + checksum(checksum'high downto 16); tx_fsm : process(clk, rst) is begin if rst then state <= IDLE; ipv4_out.tx_data_ack <= '0'; arp_in.arp_query_stb <= '0'; ipv4_out.tx_err_stb <= '0'; elsif rising_edge(clk) then arp_in.arp_query_stb <= '0'; ipv4_out.tx_data_ack <= '0'; ipv4_out.tx_err_stb <= '0'; if (ethernet_ii_out.tx_data_ack = '1') or (state = CALC_CKS) then if byte_cnt > 0 then byte_cnt <= byte_cnt - 1; end if; sr <= sr(sr'low + 1 to sr'high) & x"00"; end if; case state is when IDLE => if ipv4_in.tx_en then arp_in.arp_query_stb <= '1'; state <= ARP; end if; when ARP => if arp_out.arp_ok_stb then ethernet_ii_in.tx_mac_address <= arp_out.arp_mac; byte_cnt <= sr'length - 1; sr <= get_header(x"0000"); alt_byte <= '1'; checksum <= (others => '0'); state <= CALC_CKS; end if; if arp_out.arp_fail_stb then ipv4_out.tx_err_stb <= '1'; -- Then we lock up in this state until tx_en is disabled end if; when CALC_CKS => alt_byte <= not alt_byte; if alt_byte then checksum <= checksum + unsigned(std_logic_vector'(sr(sr'low) & sr(sr'low + 1))); else if byte_cnt = 0 then state <= HEADER; byte_cnt <= sr'length - 1; sr <= get_header(not std_logic_vector(checksum_ones)); end if; end if; when HEADER => if byte_cnt = 0 then state <= PAYLOAD; end if; when PAYLOAD => if ethernet_ii_out.tx_data_ack then sr(sr'low) <= ipv4_in.tx_data; ipv4_out.tx_data_ack <= '1'; if not ipv4_in.tx_en then state <= IDLE; end if; end if; end case; end if; end process tx_fsm; ethernet_ii_in.tx_data <= sr(sr'low); ethernet_ii_in.tx_en <= '1' when (state = HEADER) or (state = PAYLOAD) else '0'; arp_in.arp_ip <= ipv4_in.tx_ip_address; end block tx; end architecture rtl;