Network Wizard for VHDL Test Benches
UDP

UDP for IPv4

The UDP library provides functions for creating and manipulation UDP packets.

Functionality

  • Create UDP packets of any length
  • Create and extract UDP headers
  • Verify checksum of UDP packets



More details in nw_udpv4_pkg

Example use

Include the libraries:

library nw_util;
context nw_util.nw_util_context;
library nw_ipv4;
context nw_ipv4.nw_ipv4_context;

Assume the variable v_payload contains the UDP payload. The variables are defined:

variable v_header : t_udp_header; -- UDP header record
variable v_udp_pkt : t_slv_arr(0 to 1500)(7 downto 0); -- byte array
variable v_len : natural;

First setup the header, then calculate the total UDP packet length before creating the packet. Checksum is optional for UDP over IPv4. Here it will be set to x"0000".

v_header := C_DEFAULT_UDP_HEADER; -- copy default header
v_header.src_port := x"0101"; -- change source port
v_len := f_udpv4_create_pkt_len(v_header, v_payload); -- calculate total packet length
v_udp_pkt(0 to v_len - 1) := f_udpv4_create_pkt(v_header, v_payload); -- create the packet (no checksum)

If checksum is desired, the IPv4 header must be supplied for the pseudo header:

v_ipv4_header := C_DEFAULT_IPV4_HEADER; -- copy default header
v_udp_pkt(0 to v_len - 1) := f_udpv4_create_pkt(v_ipv4_header, v_header, v_payload); -- create the packet

The variable v_udp_pkt is an 8-bit array. This can of course be rearranged to any word width with f_repack .

v_ipv4_pkt_32 := f_repack(v_udp_pkt, 32, C_MSB_FIRST); -- repack to 32bit words (padded with zeros if required)

See further examples in the test bench nw_ipv4_tb.vhd.