def build_ip(self): self.build() data = b'' data += struct.pack('>H', self.udp_source_port) data += struct.pack('>H', self.udp_dest_port) data += struct.pack('>H', self.udp_length) data += struct.pack('>H', self.udp_checksum) data += self.payload.data return ip_ep.IPFrame( data, self.eth_dest_mac, self.eth_src_mac, self.eth_type, self.ip_version, self.ip_ihl, self.ip_dscp, self.ip_ecn, self.ip_length, self.ip_identification, self.ip_flags, self.ip_fragment_offset, self.ip_ttl, self.ip_protocol, self.ip_header_checksum, self.ip_source_ip, self.ip_dest_ip )
def check(): yield delay(100) yield clk.posedge rst.next = 1 yield clk.posedge rst.next = 0 yield clk.posedge yield delay(100) yield clk.posedge yield clk.posedge enable.next = True yield clk.posedge print("test 1: select port 0") current_test.next = 1 select.next = 0 test_frame = ip_ep.IPFrame() test_frame.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame.eth_src_mac = 0x5A5152535455 test_frame.eth_type = 0x8000 test_frame.ip_version = 4 test_frame.ip_ihl = 5 test_frame.ip_dscp = 0 test_frame.ip_ecn = 0 test_frame.ip_length = None test_frame.ip_identification = 0 test_frame.ip_flags = 2 test_frame.ip_fragment_offset = 0 test_frame.ip_ttl = 64 test_frame.ip_protocol = 0x11 test_frame.ip_header_checksum = None test_frame.ip_source_ip = 0xc0a80165 test_frame.ip_dest_ip = 0xc0a80164 test_frame.payload = bytearray(range(32)) test_frame.build() source.send(test_frame) yield sink_0.wait() rx_frame = sink_0.recv() assert rx_frame == test_frame yield delay(100) yield clk.posedge print("test 2: select port 1") current_test.next = 2 select.next = 1 test_frame = ip_ep.IPFrame() test_frame.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame.eth_src_mac = 0x5A5152535455 test_frame.eth_type = 0x8000 test_frame.ip_version = 4 test_frame.ip_ihl = 5 test_frame.ip_dscp = 0 test_frame.ip_ecn = 0 test_frame.ip_length = None test_frame.ip_identification = 0 test_frame.ip_flags = 2 test_frame.ip_fragment_offset = 0 test_frame.ip_ttl = 64 test_frame.ip_protocol = 0x11 test_frame.ip_header_checksum = None test_frame.ip_source_ip = 0xc0a80165 test_frame.ip_dest_ip = 0xc0a80164 test_frame.payload = bytearray(range(32)) test_frame.build() source.send(test_frame) yield sink_1.wait() rx_frame = sink_1.recv() assert rx_frame == test_frame yield delay(100) yield clk.posedge print("test 3: back-to-back packets, same port") current_test.next = 3 select.next = 0 test_frame1 = ip_ep.IPFrame() test_frame1.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame1.eth_src_mac = 0x5A5152535455 test_frame1.eth_type = 0x8000 test_frame1.ip_version = 4 test_frame1.ip_ihl = 5 test_frame1.ip_dscp = 0 test_frame1.ip_ecn = 0 test_frame1.ip_length = None test_frame1.ip_identification = 0 test_frame1.ip_flags = 2 test_frame1.ip_fragment_offset = 0 test_frame1.ip_ttl = 64 test_frame1.ip_protocol = 0x11 test_frame1.ip_header_checksum = None test_frame1.ip_source_ip = 0xc0a80165 test_frame1.ip_dest_ip = 0xc0a80164 test_frame1.payload = bytearray(range(32)) test_frame1.build() test_frame2 = ip_ep.IPFrame() test_frame2.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame2.eth_src_mac = 0x5A5152535455 test_frame2.eth_type = 0x8000 test_frame2.ip_version = 4 test_frame2.ip_ihl = 5 test_frame2.ip_dscp = 0 test_frame2.ip_ecn = 0 test_frame2.ip_length = None test_frame2.ip_identification = 0 test_frame2.ip_flags = 2 test_frame2.ip_fragment_offset = 0 test_frame2.ip_ttl = 64 test_frame2.ip_protocol = 0x11 test_frame2.ip_header_checksum = None test_frame2.ip_source_ip = 0xc0a80165 test_frame2.ip_dest_ip = 0xc0a80164 test_frame2.payload = bytearray(range(32)) test_frame2.build() source.send(test_frame1) source.send(test_frame2) yield sink_0.wait() rx_frame = sink_0.recv() assert rx_frame == test_frame1 yield sink_0.wait() rx_frame = sink_0.recv() assert rx_frame == test_frame2 yield delay(100) yield clk.posedge print("test 4: back-to-back packets, different ports") current_test.next = 4 select.next = 1 test_frame1 = ip_ep.IPFrame() test_frame1.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame1.eth_src_mac = 0x5A5152535455 test_frame1.eth_type = 0x8000 test_frame1.ip_version = 4 test_frame1.ip_ihl = 5 test_frame1.ip_dscp = 0 test_frame1.ip_ecn = 0 test_frame1.ip_length = None test_frame1.ip_identification = 0 test_frame1.ip_flags = 2 test_frame1.ip_fragment_offset = 0 test_frame1.ip_ttl = 64 test_frame1.ip_protocol = 0x11 test_frame1.ip_header_checksum = None test_frame1.ip_source_ip = 0xc0a80165 test_frame1.ip_dest_ip = 0xc0a80164 test_frame1.payload = bytearray(range(32)) test_frame1.build() test_frame2 = ip_ep.IPFrame() test_frame2.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame2.eth_src_mac = 0x5A5152535455 test_frame2.eth_type = 0x8000 test_frame2.ip_version = 4 test_frame2.ip_ihl = 5 test_frame2.ip_dscp = 0 test_frame2.ip_ecn = 0 test_frame2.ip_length = None test_frame2.ip_identification = 0 test_frame2.ip_flags = 2 test_frame2.ip_fragment_offset = 0 test_frame2.ip_ttl = 64 test_frame2.ip_protocol = 0x11 test_frame2.ip_header_checksum = None test_frame2.ip_source_ip = 0xc0a80165 test_frame2.ip_dest_ip = 0xc0a80164 test_frame2.payload = bytearray(range(32)) test_frame2.build() source.send(test_frame1) source.send(test_frame2) yield clk.posedge while input_ip_payload_tvalid or input_ip_hdr_valid: yield clk.posedge select.next = 2 yield sink_1.wait() rx_frame = sink_1.recv() assert rx_frame == test_frame1 yield sink_2.wait() rx_frame = sink_2.recv() assert rx_frame == test_frame2 yield delay(100) yield clk.posedge print("test 5: alterate pause source") current_test.next = 5 select.next = 1 test_frame1 = ip_ep.IPFrame() test_frame1.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame1.eth_src_mac = 0x5A5152535455 test_frame1.eth_type = 0x8000 test_frame1.ip_version = 4 test_frame1.ip_ihl = 5 test_frame1.ip_dscp = 0 test_frame1.ip_ecn = 0 test_frame1.ip_length = None test_frame1.ip_identification = 0 test_frame1.ip_flags = 2 test_frame1.ip_fragment_offset = 0 test_frame1.ip_ttl = 64 test_frame1.ip_protocol = 0x11 test_frame1.ip_header_checksum = None test_frame1.ip_source_ip = 0xc0a80165 test_frame1.ip_dest_ip = 0xc0a80164 test_frame1.payload = bytearray(range(32)) test_frame1.build() test_frame2 = ip_ep.IPFrame() test_frame2.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame2.eth_src_mac = 0x5A5152535455 test_frame2.eth_type = 0x8000 test_frame2.ip_version = 4 test_frame2.ip_ihl = 5 test_frame2.ip_dscp = 0 test_frame2.ip_ecn = 0 test_frame2.ip_length = None test_frame2.ip_identification = 0 test_frame2.ip_flags = 2 test_frame2.ip_fragment_offset = 0 test_frame2.ip_ttl = 64 test_frame2.ip_protocol = 0x11 test_frame2.ip_header_checksum = None test_frame2.ip_source_ip = 0xc0a80165 test_frame2.ip_dest_ip = 0xc0a80164 test_frame2.payload = bytearray(range(32)) test_frame2.build() source.send(test_frame1) source.send(test_frame2) yield clk.posedge while input_ip_payload_tvalid or input_ip_hdr_valid: source_pause.next = True yield clk.posedge yield clk.posedge yield clk.posedge source_pause.next = False yield clk.posedge select.next = 2 yield sink_1.wait() rx_frame = sink_1.recv() assert rx_frame == test_frame1 yield sink_2.wait() rx_frame = sink_2.recv() assert rx_frame == test_frame2 yield delay(100) yield clk.posedge print("test 6: alterate pause sink") current_test.next = 6 select.next = 1 test_frame1 = ip_ep.IPFrame() test_frame1.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame1.eth_src_mac = 0x5A5152535455 test_frame1.eth_type = 0x8000 test_frame1.ip_version = 4 test_frame1.ip_ihl = 5 test_frame1.ip_dscp = 0 test_frame1.ip_ecn = 0 test_frame1.ip_length = None test_frame1.ip_identification = 0 test_frame1.ip_flags = 2 test_frame1.ip_fragment_offset = 0 test_frame1.ip_ttl = 64 test_frame1.ip_protocol = 0x11 test_frame1.ip_header_checksum = None test_frame1.ip_source_ip = 0xc0a80165 test_frame1.ip_dest_ip = 0xc0a80164 test_frame1.payload = bytearray(range(32)) test_frame1.build() test_frame2 = ip_ep.IPFrame() test_frame2.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame2.eth_src_mac = 0x5A5152535455 test_frame2.eth_type = 0x8000 test_frame2.ip_version = 4 test_frame2.ip_ihl = 5 test_frame2.ip_dscp = 0 test_frame2.ip_ecn = 0 test_frame2.ip_length = None test_frame2.ip_identification = 0 test_frame2.ip_flags = 2 test_frame2.ip_fragment_offset = 0 test_frame2.ip_ttl = 64 test_frame2.ip_protocol = 0x11 test_frame2.ip_header_checksum = None test_frame2.ip_source_ip = 0xc0a80165 test_frame2.ip_dest_ip = 0xc0a80164 test_frame2.payload = bytearray(range(32)) test_frame2.build() source.send(test_frame1) source.send(test_frame2) yield clk.posedge while input_ip_payload_tvalid or input_ip_hdr_valid: sink_0_pause.next = True sink_1_pause.next = True sink_2_pause.next = True sink_3_pause.next = True yield clk.posedge yield clk.posedge yield clk.posedge sink_0_pause.next = False sink_1_pause.next = False sink_2_pause.next = False sink_3_pause.next = False yield clk.posedge select.next = 2 yield sink_1.wait() rx_frame = sink_1.recv() assert rx_frame == test_frame1 yield sink_2.wait() rx_frame = sink_2.recv() assert rx_frame == test_frame2 yield delay(100) raise StopSimulation
def check(): yield delay(100) yield clk.posedge rst.next = 1 yield clk.posedge rst.next = 0 yield clk.posedge yield delay(100) yield clk.posedge for payload_len in range(1, 18): yield clk.posedge print("test 1: test packet, length %d" % payload_len) current_test.next = 1 test_frame = ip_ep.IPFrame() test_frame.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame.eth_src_mac = 0x5A5152535455 test_frame.eth_type = 0x0800 test_frame.ip_version = 4 test_frame.ip_ihl = 5 test_frame.ip_length = None test_frame.ip_identification = 0 test_frame.ip_flags = 2 test_frame.ip_fragment_offset = 0 test_frame.ip_ttl = 64 test_frame.ip_protocol = 0x11 test_frame.ip_header_checksum = None test_frame.ip_source_ip = 0xc0a80164 test_frame.ip_dest_ip = 0xc0a80165 test_frame.payload = bytearray(range(payload_len)) test_frame.build() eth_frame = test_frame.build_eth() for wait in wait_normal, wait_pause_source, wait_pause_sink: source.send(eth_frame) yield clk.posedge yield clk.posedge yield wait() yield clk.posedge yield clk.posedge yield clk.posedge rx_frame = sink.recv() assert rx_frame == test_frame assert sink.empty() yield delay(100) yield clk.posedge print("test 2: back-to-back packets, length %d" % payload_len) current_test.next = 2 test_frame1 = ip_ep.IPFrame() test_frame1.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame1.eth_src_mac = 0x5A5152535455 test_frame1.eth_type = 0x0800 test_frame1.ip_version = 4 test_frame1.ip_ihl = 5 test_frame1.ip_length = None test_frame1.ip_identification = 0 test_frame1.ip_flags = 2 test_frame1.ip_fragment_offset = 0 test_frame1.ip_ttl = 64 test_frame1.ip_protocol = 0x11 test_frame1.ip_header_checksum = None test_frame1.ip_source_ip = 0xc0a80164 test_frame1.ip_dest_ip = 0xc0a80165 test_frame1.payload = bytearray(range(payload_len)) test_frame1.build() test_frame2 = ip_ep.IPFrame() test_frame2.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame2.eth_src_mac = 0x5A5152535455 test_frame2.eth_type = 0x0800 test_frame2.ip_version = 4 test_frame2.ip_ihl = 5 test_frame2.ip_length = None test_frame2.ip_identification = 0 test_frame2.ip_flags = 2 test_frame2.ip_fragment_offset = 0 test_frame2.ip_ttl = 64 test_frame2.ip_protocol = 0x11 test_frame2.ip_header_checksum = None test_frame2.ip_source_ip = 0xc0a80164 test_frame2.ip_dest_ip = 0xc0a80166 test_frame2.payload = bytearray(range(payload_len)) test_frame2.build() eth_frame1 = test_frame1.build_eth() eth_frame2 = test_frame2.build_eth() for wait in wait_normal, wait_pause_source, wait_pause_sink: source.send(eth_frame1) source.send(eth_frame2) yield clk.posedge yield clk.posedge yield wait() yield clk.posedge yield clk.posedge yield clk.posedge rx_frame = sink.recv() assert rx_frame == test_frame1 rx_frame = sink.recv() assert rx_frame == test_frame2 assert sink.empty() yield delay(100) yield clk.posedge print("test 3: tuser assert, length %d" % payload_len) current_test.next = 3 test_frame1 = ip_ep.IPFrame() test_frame1.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame1.eth_src_mac = 0x5A5152535455 test_frame1.eth_type = 0x0800 test_frame1.ip_version = 4 test_frame1.ip_ihl = 5 test_frame1.ip_length = None test_frame1.ip_identification = 0 test_frame1.ip_flags = 2 test_frame1.ip_fragment_offset = 0 test_frame1.ip_ttl = 64 test_frame1.ip_protocol = 0x11 test_frame1.ip_header_checksum = None test_frame1.ip_source_ip = 0xc0a80164 test_frame1.ip_dest_ip = 0xc0a80165 test_frame1.payload = bytearray(range(payload_len)) test_frame1.build() test_frame2 = ip_ep.IPFrame() test_frame2.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame2.eth_src_mac = 0x5A5152535455 test_frame2.eth_type = 0x0800 test_frame2.ip_version = 4 test_frame2.ip_ihl = 5 test_frame2.ip_length = None test_frame2.ip_identification = 0 test_frame2.ip_flags = 2 test_frame2.ip_fragment_offset = 0 test_frame2.ip_ttl = 64 test_frame2.ip_protocol = 0x11 test_frame2.ip_header_checksum = None test_frame2.ip_source_ip = 0xc0a80164 test_frame2.ip_dest_ip = 0xc0a80166 test_frame2.payload = bytearray(range(payload_len)) test_frame2.build() eth_frame1 = test_frame1.build_eth() eth_frame2 = test_frame2.build_eth() eth_frame1.payload.user = 1 for wait in wait_normal, wait_pause_source, wait_pause_sink: source.send(eth_frame1) source.send(eth_frame2) yield clk.posedge yield clk.posedge yield wait() yield clk.posedge yield clk.posedge yield clk.posedge rx_frame = sink.recv() assert rx_frame == test_frame1 assert rx_frame.payload.user[-1] rx_frame = sink.recv() assert rx_frame == test_frame2 assert sink.empty() yield delay(100) yield clk.posedge print("test 4: trailing bytes (1), length %d" % payload_len) current_test.next = 4 test_frame1 = ip_ep.IPFrame() test_frame1.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame1.eth_src_mac = 0x5A5152535455 test_frame1.eth_type = 0x0800 test_frame1.ip_version = 4 test_frame1.ip_ihl = 5 test_frame1.ip_length = None test_frame1.ip_identification = 0 test_frame1.ip_flags = 2 test_frame1.ip_fragment_offset = 0 test_frame1.ip_ttl = 64 test_frame1.ip_protocol = 0x11 test_frame1.ip_header_checksum = None test_frame1.ip_source_ip = 0xc0a80164 test_frame1.ip_dest_ip = 0xc0a80165 test_frame1.payload = bytearray(range(payload_len)) test_frame1.build() test_frame2 = ip_ep.IPFrame() test_frame2.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame2.eth_src_mac = 0x5A5152535455 test_frame2.eth_type = 0x0800 test_frame2.ip_version = 4 test_frame2.ip_ihl = 5 test_frame2.ip_length = None test_frame2.ip_identification = 0 test_frame2.ip_flags = 2 test_frame2.ip_fragment_offset = 0 test_frame2.ip_ttl = 64 test_frame2.ip_protocol = 0x11 test_frame2.ip_header_checksum = None test_frame2.ip_source_ip = 0xc0a80164 test_frame2.ip_dest_ip = 0xc0a80166 test_frame2.payload = bytearray(range(payload_len)) test_frame2.build() eth_frame1 = test_frame1.build_eth() eth_frame2 = test_frame2.build_eth() eth_frame1.payload.data += bytearray(b'\x00') for wait in wait_normal, wait_pause_source, wait_pause_sink: source.send(eth_frame1) source.send(eth_frame2) yield clk.posedge yield clk.posedge yield wait() yield clk.posedge yield clk.posedge yield clk.posedge rx_frame = sink.recv() assert rx_frame == test_frame1 rx_frame = sink.recv() assert rx_frame == test_frame2 assert sink.empty() yield delay(100) yield clk.posedge print("test 5: trailing bytes (10), length %d" % payload_len) current_test.next = 5 test_frame1 = ip_ep.IPFrame() test_frame1.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame1.eth_src_mac = 0x5A5152535455 test_frame1.eth_type = 0x0800 test_frame1.ip_version = 4 test_frame1.ip_ihl = 5 test_frame1.ip_length = None test_frame1.ip_identification = 0 test_frame1.ip_flags = 2 test_frame1.ip_fragment_offset = 0 test_frame1.ip_ttl = 64 test_frame1.ip_protocol = 0x11 test_frame1.ip_header_checksum = None test_frame1.ip_source_ip = 0xc0a80164 test_frame1.ip_dest_ip = 0xc0a80165 test_frame1.payload = bytearray(range(payload_len)) test_frame1.build() test_frame2 = ip_ep.IPFrame() test_frame2.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame2.eth_src_mac = 0x5A5152535455 test_frame2.eth_type = 0x0800 test_frame2.ip_version = 4 test_frame2.ip_ihl = 5 test_frame2.ip_length = None test_frame2.ip_identification = 0 test_frame2.ip_flags = 2 test_frame2.ip_fragment_offset = 0 test_frame2.ip_ttl = 64 test_frame2.ip_protocol = 0x11 test_frame2.ip_header_checksum = None test_frame2.ip_source_ip = 0xc0a80164 test_frame2.ip_dest_ip = 0xc0a80166 test_frame2.payload = bytearray(range(payload_len)) test_frame2.build() eth_frame1 = test_frame1.build_eth() eth_frame2 = test_frame2.build_eth() eth_frame1.payload.data += bytearray(b'\x00' * 10) for wait in wait_normal, wait_pause_source, wait_pause_sink: source.send(eth_frame1) source.send(eth_frame2) yield clk.posedge yield clk.posedge yield wait() yield clk.posedge yield clk.posedge yield clk.posedge rx_frame = sink.recv() assert rx_frame == test_frame1 rx_frame = sink.recv() assert rx_frame == test_frame2 assert sink.empty() yield delay(100) yield clk.posedge print("test 6: trailing bytes with tuser assert (1), length %d" % payload_len) current_test.next = 6 test_frame1 = ip_ep.IPFrame() test_frame1.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame1.eth_src_mac = 0x5A5152535455 test_frame1.eth_type = 0x0800 test_frame1.ip_version = 4 test_frame1.ip_ihl = 5 test_frame1.ip_length = None test_frame1.ip_identification = 0 test_frame1.ip_flags = 2 test_frame1.ip_fragment_offset = 0 test_frame1.ip_ttl = 64 test_frame1.ip_protocol = 0x11 test_frame1.ip_header_checksum = None test_frame1.ip_source_ip = 0xc0a80164 test_frame1.ip_dest_ip = 0xc0a80165 test_frame1.payload = bytearray(range(payload_len)) test_frame1.build() test_frame2 = ip_ep.IPFrame() test_frame2.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame2.eth_src_mac = 0x5A5152535455 test_frame2.eth_type = 0x0800 test_frame2.ip_version = 4 test_frame2.ip_ihl = 5 test_frame2.ip_length = None test_frame2.ip_identification = 0 test_frame2.ip_flags = 2 test_frame2.ip_fragment_offset = 0 test_frame2.ip_ttl = 64 test_frame2.ip_protocol = 0x11 test_frame2.ip_header_checksum = None test_frame2.ip_source_ip = 0xc0a80164 test_frame2.ip_dest_ip = 0xc0a80166 test_frame2.payload = bytearray(range(payload_len)) test_frame2.build() eth_frame1 = test_frame1.build_eth() eth_frame2 = test_frame2.build_eth() eth_frame1.payload.data += bytearray(b'\x00') eth_frame1.payload.user = 1 for wait in wait_normal, wait_pause_source, wait_pause_sink: source.send(eth_frame1) source.send(eth_frame2) yield clk.posedge yield clk.posedge yield wait() yield clk.posedge yield clk.posedge yield clk.posedge rx_frame = sink.recv() assert rx_frame == test_frame1 assert rx_frame.payload.user[-1] rx_frame = sink.recv() assert rx_frame == test_frame2 assert sink.empty() yield delay(100) yield clk.posedge print("test 7: trailing bytes with tuser assert (10), length %d" % payload_len) current_test.next = 7 test_frame1 = ip_ep.IPFrame() test_frame1.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame1.eth_src_mac = 0x5A5152535455 test_frame1.eth_type = 0x0800 test_frame1.ip_version = 4 test_frame1.ip_ihl = 5 test_frame1.ip_length = None test_frame1.ip_identification = 0 test_frame1.ip_flags = 2 test_frame1.ip_fragment_offset = 0 test_frame1.ip_ttl = 64 test_frame1.ip_protocol = 0x11 test_frame1.ip_header_checksum = None test_frame1.ip_source_ip = 0xc0a80164 test_frame1.ip_dest_ip = 0xc0a80165 test_frame1.payload = bytearray(range(payload_len)) test_frame1.build() test_frame2 = ip_ep.IPFrame() test_frame2.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame2.eth_src_mac = 0x5A5152535455 test_frame2.eth_type = 0x0800 test_frame2.ip_version = 4 test_frame2.ip_ihl = 5 test_frame2.ip_length = None test_frame2.ip_identification = 0 test_frame2.ip_flags = 2 test_frame2.ip_fragment_offset = 0 test_frame2.ip_ttl = 64 test_frame2.ip_protocol = 0x11 test_frame2.ip_header_checksum = None test_frame2.ip_source_ip = 0xc0a80164 test_frame2.ip_dest_ip = 0xc0a80166 test_frame2.payload = bytearray(range(payload_len)) test_frame2.build() eth_frame1 = test_frame1.build_eth() eth_frame2 = test_frame2.build_eth() eth_frame1.payload.data += bytearray(b'\x00' * 10) eth_frame1.payload.user = 1 for wait in wait_normal, wait_pause_source, wait_pause_sink: source.send(eth_frame1) source.send(eth_frame2) yield clk.posedge yield clk.posedge yield wait() yield clk.posedge yield clk.posedge yield clk.posedge rx_frame = sink.recv() assert rx_frame == test_frame1 assert rx_frame.payload.user[-1] rx_frame = sink.recv() assert rx_frame == test_frame2 assert sink.empty() yield delay(100) yield clk.posedge print("test 8: truncated payload (1), length %d" % payload_len) current_test.next = 8 test_frame1 = ip_ep.IPFrame() test_frame1.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame1.eth_src_mac = 0x5A5152535455 test_frame1.eth_type = 0x0800 test_frame1.ip_version = 4 test_frame1.ip_ihl = 5 test_frame1.ip_length = None test_frame1.ip_identification = 0 test_frame1.ip_flags = 2 test_frame1.ip_fragment_offset = 0 test_frame1.ip_ttl = 64 test_frame1.ip_protocol = 0x11 test_frame1.ip_header_checksum = None test_frame1.ip_source_ip = 0xc0a80164 test_frame1.ip_dest_ip = 0xc0a80165 test_frame1.payload = bytearray(range(payload_len + 1)) test_frame1.build() test_frame2 = ip_ep.IPFrame() test_frame2.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame2.eth_src_mac = 0x5A5152535455 test_frame2.eth_type = 0x0800 test_frame2.ip_version = 4 test_frame2.ip_ihl = 5 test_frame2.ip_length = None test_frame2.ip_identification = 0 test_frame2.ip_flags = 2 test_frame2.ip_fragment_offset = 0 test_frame2.ip_ttl = 64 test_frame2.ip_protocol = 0x11 test_frame2.ip_header_checksum = None test_frame2.ip_source_ip = 0xc0a80164 test_frame2.ip_dest_ip = 0xc0a80166 test_frame2.payload = bytearray(range(payload_len)) test_frame2.build() eth_frame1 = test_frame1.build_eth() eth_frame2 = test_frame2.build_eth() eth_frame1.payload.data = eth_frame1.payload.data[:-1] for wait in wait_normal, wait_pause_source, wait_pause_sink: error_payload_early_termination_asserted.next = 0 source.send(eth_frame1) source.send(eth_frame2) yield clk.posedge yield clk.posedge yield wait() yield clk.posedge yield clk.posedge yield clk.posedge rx_frame = sink.recv() assert rx_frame.payload.user[-1] assert error_payload_early_termination_asserted rx_frame = sink.recv() assert rx_frame == test_frame2 assert sink.empty() yield delay(100) yield clk.posedge print("test 9: truncated payload (10), length %d" % payload_len) current_test.next = 9 test_frame1 = ip_ep.IPFrame() test_frame1.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame1.eth_src_mac = 0x5A5152535455 test_frame1.eth_type = 0x0800 test_frame1.ip_version = 4 test_frame1.ip_ihl = 5 test_frame1.ip_length = None test_frame1.ip_identification = 0 test_frame1.ip_flags = 2 test_frame1.ip_fragment_offset = 0 test_frame1.ip_ttl = 64 test_frame1.ip_protocol = 0x11 test_frame1.ip_header_checksum = None test_frame1.ip_source_ip = 0xc0a80164 test_frame1.ip_dest_ip = 0xc0a80165 test_frame1.payload = bytearray(range(payload_len + 10)) test_frame1.build() test_frame2 = ip_ep.IPFrame() test_frame2.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame2.eth_src_mac = 0x5A5152535455 test_frame2.eth_type = 0x0800 test_frame2.ip_version = 4 test_frame2.ip_ihl = 5 test_frame2.ip_length = None test_frame2.ip_identification = 0 test_frame2.ip_flags = 2 test_frame2.ip_fragment_offset = 0 test_frame2.ip_ttl = 64 test_frame2.ip_protocol = 0x11 test_frame2.ip_header_checksum = None test_frame2.ip_source_ip = 0xc0a80164 test_frame2.ip_dest_ip = 0xc0a80166 test_frame2.payload = bytearray(range(payload_len)) test_frame2.build() eth_frame1 = test_frame1.build_eth() eth_frame2 = test_frame2.build_eth() eth_frame1.payload.data = eth_frame1.payload.data[:-10] for wait in wait_normal, wait_pause_source, wait_pause_sink: error_payload_early_termination_asserted.next = 0 source.send(eth_frame1) source.send(eth_frame2) yield clk.posedge yield clk.posedge yield wait() yield clk.posedge yield clk.posedge yield clk.posedge rx_frame = sink.recv() assert rx_frame.payload.user[-1] assert error_payload_early_termination_asserted rx_frame = sink.recv() assert rx_frame == test_frame2 assert sink.empty() yield delay(100) yield clk.posedge print("test 10: bad IHL, length %d" % payload_len) current_test.next = 10 test_frame1 = ip_ep.IPFrame() test_frame1.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame1.eth_src_mac = 0x5A5152535455 test_frame1.eth_type = 0x0800 test_frame1.ip_version = 4 test_frame1.ip_ihl = 6 test_frame1.ip_length = None test_frame1.ip_identification = 0 test_frame1.ip_flags = 2 test_frame1.ip_fragment_offset = 0 test_frame1.ip_ttl = 64 test_frame1.ip_protocol = 0x11 test_frame1.ip_header_checksum = None test_frame1.ip_source_ip = 0xc0a80164 test_frame1.ip_dest_ip = 0xc0a80165 test_frame1.payload = bytearray(range(payload_len)) test_frame1.build() test_frame2 = ip_ep.IPFrame() test_frame2.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame2.eth_src_mac = 0x5A5152535455 test_frame2.eth_type = 0x0800 test_frame2.ip_version = 4 test_frame2.ip_ihl = 5 test_frame2.ip_length = None test_frame2.ip_identification = 0 test_frame2.ip_flags = 2 test_frame2.ip_fragment_offset = 0 test_frame2.ip_ttl = 64 test_frame2.ip_protocol = 0x11 test_frame2.ip_header_checksum = None test_frame2.ip_source_ip = 0xc0a80164 test_frame2.ip_dest_ip = 0xc0a80166 test_frame2.payload = bytearray(range(payload_len)) test_frame2.build() eth_frame1 = test_frame1.build_eth() eth_frame2 = test_frame2.build_eth() for wait in wait_normal, wait_pause_source, wait_pause_sink: error_invalid_header_asserted.next = 0 source.send(eth_frame1) source.send(eth_frame2) yield clk.posedge yield clk.posedge yield wait() yield clk.posedge yield clk.posedge yield clk.posedge assert error_invalid_header_asserted rx_frame = sink.recv() assert rx_frame == test_frame2 assert sink.empty() yield delay(100) yield clk.posedge print("test 11: bad checksum, length %d" % payload_len) current_test.next = 11 test_frame1 = ip_ep.IPFrame() test_frame1.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame1.eth_src_mac = 0x5A5152535455 test_frame1.eth_type = 0x0800 test_frame1.ip_version = 4 test_frame1.ip_ihl = 5 test_frame1.ip_length = None test_frame1.ip_identification = 0 test_frame1.ip_flags = 2 test_frame1.ip_fragment_offset = 0 test_frame1.ip_ttl = 64 test_frame1.ip_protocol = 0x11 test_frame1.ip_header_checksum = 0x1234 test_frame1.ip_source_ip = 0xc0a80164 test_frame1.ip_dest_ip = 0xc0a80165 test_frame1.payload = bytearray(range(payload_len)) test_frame1.build() test_frame2 = ip_ep.IPFrame() test_frame2.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame2.eth_src_mac = 0x5A5152535455 test_frame2.eth_type = 0x0800 test_frame2.ip_version = 4 test_frame2.ip_ihl = 5 test_frame2.ip_length = None test_frame2.ip_identification = 0 test_frame2.ip_flags = 2 test_frame2.ip_fragment_offset = 0 test_frame2.ip_ttl = 64 test_frame2.ip_protocol = 0x11 test_frame2.ip_header_checksum = None test_frame2.ip_source_ip = 0xc0a80164 test_frame2.ip_dest_ip = 0xc0a80166 test_frame2.payload = bytearray(range(payload_len)) test_frame2.build() eth_frame1 = test_frame1.build_eth() eth_frame2 = test_frame2.build_eth() for wait in wait_normal, wait_pause_source, wait_pause_sink: error_invalid_checksum_asserted.next = 0 source.send(eth_frame1) source.send(eth_frame2) yield clk.posedge yield clk.posedge yield wait() yield clk.posedge yield clk.posedge yield clk.posedge assert error_invalid_checksum_asserted rx_frame = sink.recv() assert rx_frame == test_frame2 assert sink.empty() yield delay(100) for length in range(1, 21): yield clk.posedge print("test 12: truncated header, length %d" % length) current_test.next = 12 test_frame1 = ip_ep.IPFrame() test_frame1.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame1.eth_src_mac = 0x5A5152535455 test_frame1.eth_type = 0x0800 test_frame1.ip_version = 4 test_frame1.ip_ihl = 5 test_frame1.ip_length = None test_frame1.ip_identification = 0 test_frame1.ip_flags = 2 test_frame1.ip_fragment_offset = 0 test_frame1.ip_ttl = 64 test_frame1.ip_protocol = 0x11 test_frame1.ip_header_checksum = None test_frame1.ip_source_ip = 0xc0a80164 test_frame1.ip_dest_ip = 0xc0a80165 test_frame1.payload = bytearray(range(16)) test_frame1.build() test_frame2 = ip_ep.IPFrame() test_frame2.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame2.eth_src_mac = 0x5A5152535455 test_frame2.eth_type = 0x0800 test_frame2.ip_version = 4 test_frame2.ip_ihl = 5 test_frame2.ip_length = None test_frame2.ip_identification = 0 test_frame2.ip_flags = 2 test_frame2.ip_fragment_offset = 0 test_frame2.ip_ttl = 64 test_frame2.ip_protocol = 0x11 test_frame2.ip_header_checksum = None test_frame2.ip_source_ip = 0xc0a80164 test_frame2.ip_dest_ip = 0xc0a80166 test_frame2.payload = bytearray(range(16)) test_frame2.build() eth_frame1 = test_frame1.build_eth() eth_frame2 = test_frame2.build_eth() eth_frame1.payload.data = eth_frame1.payload.data[:length] for wait in wait_normal, wait_pause_source, wait_pause_sink: error_header_early_termination_asserted.next = 0 source.send(eth_frame1) source.send(eth_frame2) yield clk.posedge yield clk.posedge yield wait() yield clk.posedge yield clk.posedge yield clk.posedge assert error_header_early_termination_asserted rx_frame = sink.recv() assert rx_frame == test_frame2 assert sink.empty() yield delay(100) raise StopSimulation
def check(): yield delay(100) yield clk.posedge rst.next = 1 yield clk.posedge rst.next = 0 yield clk.posedge yield delay(100) yield clk.posedge yield clk.posedge yield clk.posedge print("test 1: port 0") current_test.next = 1 test_frame = ip_ep.IPFrame() test_frame.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame.eth_src_mac = 0x5A5152535455 test_frame.eth_type = 0x8000 test_frame.ip_version = 4 test_frame.ip_ihl = 5 test_frame.ip_dscp = 0 test_frame.ip_ecn = 0 test_frame.ip_length = None test_frame.ip_identification = 0 test_frame.ip_flags = 2 test_frame.ip_fragment_offset = 0 test_frame.ip_ttl = 64 test_frame.ip_protocol = 0x11 test_frame.ip_header_checksum = None test_frame.ip_source_ip = 0xc0a80165 test_frame.ip_dest_ip = 0xc0a80164 test_frame.payload = bytearray(range(32)) test_frame.build() source_list[0].send(test_frame) yield sink.wait() rx_frame = sink.recv() assert rx_frame == test_frame yield delay(100) yield clk.posedge print("test 2: port 1") current_test.next = 2 test_frame = ip_ep.IPFrame() test_frame.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame.eth_src_mac = 0x5A5152535455 test_frame.eth_type = 0x8000 test_frame.ip_version = 4 test_frame.ip_ihl = 5 test_frame.ip_dscp = 0 test_frame.ip_ecn = 0 test_frame.ip_length = None test_frame.ip_identification = 0 test_frame.ip_flags = 2 test_frame.ip_fragment_offset = 0 test_frame.ip_ttl = 64 test_frame.ip_protocol = 0x11 test_frame.ip_header_checksum = None test_frame.ip_source_ip = 0xc0a80165 test_frame.ip_dest_ip = 0xc0a80164 test_frame.payload = bytearray(range(32)) test_frame.build() source_list[1].send(test_frame) yield sink.wait() rx_frame = sink.recv() assert rx_frame == test_frame yield delay(100) yield clk.posedge print("test 3: back-to-back packets, same port") current_test.next = 3 test_frame1 = ip_ep.IPFrame() test_frame1.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame1.eth_src_mac = 0x5A5152535455 test_frame1.eth_type = 0x8000 test_frame1.ip_version = 4 test_frame1.ip_ihl = 5 test_frame1.ip_dscp = 0 test_frame1.ip_ecn = 0 test_frame1.ip_length = None test_frame1.ip_identification = 0 test_frame1.ip_flags = 2 test_frame1.ip_fragment_offset = 0 test_frame1.ip_ttl = 64 test_frame1.ip_protocol = 0x11 test_frame1.ip_header_checksum = None test_frame1.ip_source_ip = 0xc0a80165 test_frame1.ip_dest_ip = 0xc0a80164 test_frame1.payload = bytearray(range(32)) test_frame1.build() test_frame2 = ip_ep.IPFrame() test_frame2.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame2.eth_src_mac = 0x5A5152535455 test_frame2.eth_type = 0x8000 test_frame2.ip_version = 4 test_frame2.ip_ihl = 5 test_frame2.ip_dscp = 0 test_frame2.ip_ecn = 0 test_frame2.ip_length = None test_frame2.ip_identification = 0 test_frame2.ip_flags = 2 test_frame2.ip_fragment_offset = 0 test_frame2.ip_ttl = 64 test_frame2.ip_protocol = 0x11 test_frame2.ip_header_checksum = None test_frame2.ip_source_ip = 0xc0a80165 test_frame2.ip_dest_ip = 0xc0a80164 test_frame2.payload = bytearray(range(32)) test_frame2.build() source_list[0].send(test_frame1) source_list[0].send(test_frame2) yield sink.wait() rx_frame = sink.recv() assert rx_frame == test_frame1 yield sink.wait() rx_frame = sink.recv() assert rx_frame == test_frame2 yield delay(100) yield clk.posedge print("test 4: back-to-back packets, different ports") current_test.next = 4 test_frame1 = ip_ep.IPFrame() test_frame1.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame1.eth_src_mac = 0x5A0152535455 test_frame1.eth_type = 0x8000 test_frame1.ip_version = 4 test_frame1.ip_ihl = 5 test_frame1.ip_dscp = 0 test_frame1.ip_ecn = 0 test_frame1.ip_length = None test_frame1.ip_identification = 0 test_frame1.ip_flags = 2 test_frame1.ip_fragment_offset = 0 test_frame1.ip_ttl = 64 test_frame1.ip_protocol = 0x11 test_frame1.ip_header_checksum = None test_frame1.ip_source_ip = 0xc0a80165 test_frame1.ip_dest_ip = 0xc0a80164 test_frame1.payload = bytearray(range(32)) test_frame1.build() test_frame2 = ip_ep.IPFrame() test_frame2.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame2.eth_src_mac = 0x5A0252535455 test_frame2.eth_type = 0x8000 test_frame2.ip_version = 4 test_frame2.ip_ihl = 5 test_frame2.ip_dscp = 0 test_frame2.ip_ecn = 0 test_frame2.ip_length = None test_frame2.ip_identification = 0 test_frame2.ip_flags = 2 test_frame2.ip_fragment_offset = 0 test_frame2.ip_ttl = 64 test_frame2.ip_protocol = 0x11 test_frame2.ip_header_checksum = None test_frame2.ip_source_ip = 0xc0a80165 test_frame2.ip_dest_ip = 0xc0a80164 test_frame2.payload = bytearray(range(32)) test_frame2.build() source_list[1].send(test_frame1) source_list[2].send(test_frame2) yield sink.wait() rx_frame = sink.recv() assert rx_frame == test_frame1 yield sink.wait() rx_frame = sink.recv() assert rx_frame == test_frame2 yield delay(100) yield clk.posedge print("test 5: alterate pause source") current_test.next = 5 test_frame1 = ip_ep.IPFrame() test_frame1.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame1.eth_src_mac = 0x5A0152535455 test_frame1.eth_type = 0x8000 test_frame1.ip_version = 4 test_frame1.ip_ihl = 5 test_frame1.ip_dscp = 0 test_frame1.ip_ecn = 0 test_frame1.ip_length = None test_frame1.ip_identification = 0 test_frame1.ip_flags = 2 test_frame1.ip_fragment_offset = 0 test_frame1.ip_ttl = 64 test_frame1.ip_protocol = 0x11 test_frame1.ip_header_checksum = None test_frame1.ip_source_ip = 0xc0a80165 test_frame1.ip_dest_ip = 0xc0a80164 test_frame1.payload = bytearray(range(32)) test_frame1.build() test_frame2 = ip_ep.IPFrame() test_frame2.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame2.eth_src_mac = 0x5A0252535455 test_frame2.eth_type = 0x8000 test_frame2.ip_version = 4 test_frame2.ip_ihl = 5 test_frame2.ip_dscp = 0 test_frame2.ip_ecn = 0 test_frame2.ip_length = None test_frame2.ip_identification = 0 test_frame2.ip_flags = 2 test_frame2.ip_fragment_offset = 0 test_frame2.ip_ttl = 64 test_frame2.ip_protocol = 0x11 test_frame2.ip_header_checksum = None test_frame2.ip_source_ip = 0xc0a80165 test_frame2.ip_dest_ip = 0xc0a80164 test_frame2.payload = bytearray(range(32)) test_frame2.build() source_list[1].send(test_frame1) source_list[2].send(test_frame2) yield clk.posedge yield clk.posedge while s_ip_payload_axis_tvalid: source_pause_list[0].next = True source_pause_list[1].next = True source_pause_list[2].next = True source_pause_list[3].next = True yield clk.posedge yield clk.posedge yield clk.posedge source_pause_list[0].next = False source_pause_list[1].next = False source_pause_list[2].next = False source_pause_list[3].next = False yield clk.posedge yield sink.wait() rx_frame = sink.recv() assert rx_frame == test_frame1 yield sink.wait() rx_frame = sink.recv() assert rx_frame == test_frame2 yield delay(100) yield clk.posedge print("test 6: alterate pause sink") current_test.next = 6 test_frame1 = ip_ep.IPFrame() test_frame1.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame1.eth_src_mac = 0x5A0152535455 test_frame1.eth_type = 0x8000 test_frame1.ip_version = 4 test_frame1.ip_ihl = 5 test_frame1.ip_dscp = 0 test_frame1.ip_ecn = 0 test_frame1.ip_length = None test_frame1.ip_identification = 0 test_frame1.ip_flags = 2 test_frame1.ip_fragment_offset = 0 test_frame1.ip_ttl = 64 test_frame1.ip_protocol = 0x11 test_frame1.ip_header_checksum = None test_frame1.ip_source_ip = 0xc0a80165 test_frame1.ip_dest_ip = 0xc0a80164 test_frame1.payload = bytearray(range(32)) test_frame1.build() test_frame2 = ip_ep.IPFrame() test_frame2.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame2.eth_src_mac = 0x5A0252535455 test_frame2.eth_type = 0x8000 test_frame2.ip_version = 4 test_frame2.ip_ihl = 5 test_frame2.ip_dscp = 0 test_frame2.ip_ecn = 0 test_frame2.ip_length = None test_frame2.ip_identification = 0 test_frame2.ip_flags = 2 test_frame2.ip_fragment_offset = 0 test_frame2.ip_ttl = 64 test_frame2.ip_protocol = 0x11 test_frame2.ip_header_checksum = None test_frame2.ip_source_ip = 0xc0a80165 test_frame2.ip_dest_ip = 0xc0a80164 test_frame2.payload = bytearray(range(32)) test_frame2.build() source_list[1].send(test_frame1) source_list[2].send(test_frame2) yield clk.posedge yield clk.posedge while s_ip_payload_axis_tvalid: sink_pause.next = True yield clk.posedge yield clk.posedge yield clk.posedge sink_pause.next = False yield clk.posedge yield sink.wait() rx_frame = sink.recv() assert rx_frame == test_frame1 yield sink.wait() rx_frame = sink.recv() assert rx_frame == test_frame2 yield delay(100) yield clk.posedge print( "test 7: back-to-back packets, different ports, arbitration test") current_test.next = 7 test_frame1 = ip_ep.IPFrame() test_frame1.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame1.eth_src_mac = 0x5A0152535455 test_frame1.eth_type = 0x8000 test_frame1.ip_version = 4 test_frame1.ip_ihl = 5 test_frame1.ip_dscp = 0 test_frame1.ip_ecn = 0 test_frame1.ip_length = None test_frame1.ip_identification = 0 test_frame1.ip_flags = 2 test_frame1.ip_fragment_offset = 0 test_frame1.ip_ttl = 64 test_frame1.ip_protocol = 0x11 test_frame1.ip_header_checksum = None test_frame1.ip_source_ip = 0xc0a80165 test_frame1.ip_dest_ip = 0xc0a80164 test_frame1.payload = bytearray(range(32)) test_frame1.build() test_frame2 = ip_ep.IPFrame() test_frame2.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame2.eth_src_mac = 0x5A0252535455 test_frame2.eth_type = 0x8000 test_frame2.ip_version = 4 test_frame2.ip_ihl = 5 test_frame2.ip_dscp = 0 test_frame2.ip_ecn = 0 test_frame2.ip_length = None test_frame2.ip_identification = 0 test_frame2.ip_flags = 2 test_frame2.ip_fragment_offset = 0 test_frame2.ip_ttl = 64 test_frame2.ip_protocol = 0x11 test_frame2.ip_header_checksum = None test_frame2.ip_source_ip = 0xc0a80165 test_frame2.ip_dest_ip = 0xc0a80164 test_frame2.payload = bytearray(range(32)) test_frame2.build() source_list[1].send(test_frame1) source_list[2].send(test_frame2) source_list[2].send(test_frame2) source_list[2].send(test_frame2) source_list[2].send(test_frame2) source_list[2].send(test_frame2) yield clk.posedge yield delay(120) yield clk.posedge source_list[1].send(test_frame1) yield sink.wait() rx_frame = sink.recv() assert rx_frame == test_frame1 yield sink.wait() rx_frame = sink.recv() assert rx_frame == test_frame2 yield sink.wait() rx_frame = sink.recv() assert rx_frame == test_frame2 yield sink.wait() rx_frame = sink.recv() assert rx_frame == test_frame2 yield sink.wait() rx_frame = sink.recv() assert rx_frame == test_frame1 yield sink.wait() rx_frame = sink.recv() assert rx_frame == test_frame2 yield delay(100) raise StopSimulation
def check(): yield delay(100) yield clk.posedge rst.next = 1 yield clk.posedge rst.next = 0 yield clk.posedge yield delay(100) yield clk.posedge # set MAC and IP address local_mac.next = 0x5A5152535455 local_ip.next = 0xc0a80164 gateway_ip.next = 0xc0a80101 subnet_mask.next = 0xffffff00 yield clk.posedge print("test 1: test IP RX packet") current_test.next = 1 test_frame = ip_ep.IPFrame() test_frame.eth_dest_mac = 0x5A5152535455 test_frame.eth_src_mac = 0xDAD1D2D3D4D5 test_frame.eth_type = 0x0800 test_frame.ip_version = 4 test_frame.ip_ihl = 5 test_frame.ip_dscp = 0 test_frame.ip_ecn = 0 test_frame.ip_length = None test_frame.ip_identification = 0 test_frame.ip_flags = 2 test_frame.ip_fragment_offset = 0 test_frame.ip_ttl = 64 test_frame.ip_protocol = 0x11 test_frame.ip_header_checksum = None test_frame.ip_source_ip = 0xc0a80165 test_frame.ip_dest_ip = 0xc0a80164 test_frame.payload = bytearray(range(32)) test_frame.build() eth_frame = test_frame.build_eth() eth_source.send(eth_frame) yield clk.posedge yield clk.posedge yield wait_normal() yield clk.posedge yield clk.posedge rx_frame = ip_sink.recv() assert rx_frame == test_frame assert eth_source.empty() assert eth_sink.empty() assert ip_source.empty() assert ip_sink.empty() yield delay(100) yield clk.posedge print("test 2: test IP TX packet") current_test.next = 2 # send IP packet test_frame = ip_ep.IPFrame() test_frame.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame.eth_src_mac = 0x5A5152535455 test_frame.eth_type = 0x0800 test_frame.ip_version = 4 test_frame.ip_ihl = 5 test_frame.ip_dscp = 0 test_frame.ip_ecn = 0 test_frame.ip_length = None test_frame.ip_identification = 0 test_frame.ip_flags = 2 test_frame.ip_fragment_offset = 0 test_frame.ip_ttl = 64 test_frame.ip_protocol = 0x11 test_frame.ip_header_checksum = None test_frame.ip_source_ip = 0xc0a80164 test_frame.ip_dest_ip = 0xc0a80166 test_frame.payload = bytearray(range(32)) test_frame.build() ip_source.send(test_frame) # wait for ARP request packet while eth_sink.empty(): yield clk.posedge rx_frame = eth_sink.recv() check_frame = arp_ep.ARPFrame() check_frame.parse_eth(rx_frame) assert check_frame.eth_dest_mac == 0xFFFFFFFFFFFF assert check_frame.eth_src_mac == 0x5A5152535455 assert check_frame.eth_type == 0x0806 assert check_frame.arp_htype == 0x0001 assert check_frame.arp_ptype == 0x0800 assert check_frame.arp_hlen == 6 assert check_frame.arp_plen == 4 assert check_frame.arp_oper == 1 assert check_frame.arp_sha == 0x5A5152535455 assert check_frame.arp_spa == 0xc0a80164 assert check_frame.arp_tha == 0x000000000000 assert check_frame.arp_tpa == 0xc0a80166 # generate response arp_frame = arp_ep.ARPFrame() arp_frame.eth_dest_mac = 0x5A5152535455 arp_frame.eth_src_mac = 0xDAD1D2D3D4D5 arp_frame.eth_type = 0x0806 arp_frame.arp_htype = 0x0001 arp_frame.arp_ptype = 0x0800 arp_frame.arp_hlen = 6 arp_frame.arp_plen = 4 arp_frame.arp_oper = 2 arp_frame.arp_sha = 0xDAD1D2D3D4D5 arp_frame.arp_spa = 0xc0a80166 arp_frame.arp_tha = 0x5A5152535455 arp_frame.arp_tpa = 0xc0a80164 eth_source.send(arp_frame.build_eth()) yield clk.posedge yield clk.posedge yield wait_normal() yield clk.posedge yield clk.posedge rx_frame = eth_sink.recv() check_frame = ip_ep.IPFrame() check_frame.parse_eth(rx_frame) print(test_frame) print(check_frame) assert check_frame == test_frame assert eth_source.empty() assert eth_sink.empty() assert ip_source.empty() assert ip_sink.empty() yield delay(100) yield clk.posedge print("test 3: test IP TX arp fail packet") current_test.next = 2 tx_error_arp_failed_asserted.next = 0 test_frame = ip_ep.IPFrame() test_frame.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame.eth_src_mac = 0x5A5152535455 test_frame.eth_type = 0x0800 test_frame.ip_version = 4 test_frame.ip_ihl = 5 test_frame.ip_dscp = 0 test_frame.ip_ecn = 0 test_frame.ip_length = None test_frame.ip_identification = 0 test_frame.ip_flags = 2 test_frame.ip_fragment_offset = 0 test_frame.ip_ttl = 64 test_frame.ip_protocol = 0x11 test_frame.ip_header_checksum = None test_frame.ip_source_ip = 0xc0a80164 test_frame.ip_dest_ip = 0xc0a80167 test_frame.payload = bytearray(range(32)) test_frame.build() ip_source.send(test_frame) yield clk.posedge yield clk.posedge yield wait_normal() yield clk.posedge yield clk.posedge assert tx_error_arp_failed_asserted # check for 4 ARP requests assert eth_sink.count() == 4 while not eth_sink.empty(): rx_frame = eth_sink.recv() check_frame = arp_ep.ARPFrame() check_frame.parse_eth(rx_frame) assert check_frame.eth_dest_mac == 0xFFFFFFFFFFFF assert check_frame.eth_src_mac == 0x5A5152535455 assert check_frame.eth_type == 0x0806 assert check_frame.arp_htype == 0x0001 assert check_frame.arp_ptype == 0x0800 assert check_frame.arp_hlen == 6 assert check_frame.arp_plen == 4 assert check_frame.arp_oper == 1 assert check_frame.arp_sha == 0x5A5152535455 assert check_frame.arp_spa == 0xc0a80164 assert check_frame.arp_tha == 0x000000000000 assert check_frame.arp_tpa == 0xc0a80167 assert eth_source.empty() assert eth_sink.empty() assert ip_source.empty() assert ip_sink.empty() yield delay(100) raise StopSimulation
def parse_eth(self, data): frame = ip_ep.IPFrame() frame.parse_eth(data) self.parse_ip(frame)
def check(): yield delay(100) yield clk.posedge rst.next = 1 yield clk.posedge rst.next = 0 yield clk.posedge yield delay(100) yield clk.posedge # set MAC and IP address local_mac.next = 0x5A5152535455 local_ip.next = 0xc0a80164 # put an entry in the ARP table arp_table[0xc0a80165] = 0xDAD1D2D3D4D5 yield clk.posedge print("test 1: test IP RX packet") current_test.next = 1 test_frame = ip_ep.IPFrame() test_frame.eth_dest_mac = 0x5A5152535455 test_frame.eth_src_mac = 0xDAD1D2D3D4D5 test_frame.eth_type = 0x0800 test_frame.ip_version = 4 test_frame.ip_ihl = 5 test_frame.ip_dscp = 0 test_frame.ip_ecn = 0 test_frame.ip_length = None test_frame.ip_identification = 0 test_frame.ip_flags = 2 test_frame.ip_fragment_offset = 0 test_frame.ip_ttl = 64 test_frame.ip_protocol = 0x11 test_frame.ip_header_checksum = None test_frame.ip_source_ip = 0xc0a80165 test_frame.ip_dest_ip = 0xc0a80164 test_frame.payload = bytearray(range(32)) test_frame.build() eth_frame = test_frame.build_eth() eth_source.send(eth_frame) yield clk.posedge yield clk.posedge yield wait_normal() yield clk.posedge yield clk.posedge rx_frame = ip_sink.recv() assert rx_frame == test_frame assert eth_source.empty() assert eth_sink.empty() assert ip_source.empty() assert ip_sink.empty() yield delay(100) yield clk.posedge print("test 2: test IP TX packet") current_test.next = 2 test_frame = ip_ep.IPFrame() test_frame.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame.eth_src_mac = 0x5A5152535455 test_frame.eth_type = 0x0800 test_frame.ip_version = 4 test_frame.ip_ihl = 5 test_frame.ip_dscp = 0 test_frame.ip_ecn = 0 test_frame.ip_length = None test_frame.ip_identification = 0 test_frame.ip_flags = 2 test_frame.ip_fragment_offset = 0 test_frame.ip_ttl = 64 test_frame.ip_protocol = 0x11 test_frame.ip_header_checksum = None test_frame.ip_source_ip = 0xc0a80164 test_frame.ip_dest_ip = 0xc0a80165 test_frame.payload = bytearray(range(32)) test_frame.build() ip_source.send(test_frame) yield clk.posedge yield clk.posedge yield wait_normal() yield clk.posedge yield clk.posedge rx_frame = eth_sink.recv() check_frame = ip_ep.IPFrame() check_frame.parse_eth(rx_frame) assert check_frame == test_frame assert eth_source.empty() assert eth_sink.empty() assert ip_source.empty() assert ip_sink.empty() yield delay(100) yield clk.posedge print("test 3: test IP TX arp fail packet") current_test.next = 2 tx_error_arp_failed_asserted.next = 0 test_frame = ip_ep.IPFrame() test_frame.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame.eth_src_mac = 0x5A5152535455 test_frame.eth_type = 0x0800 test_frame.ip_version = 4 test_frame.ip_ihl = 5 test_frame.ip_dscp = 0 test_frame.ip_ecn = 0 test_frame.ip_length = None test_frame.ip_identification = 0 test_frame.ip_flags = 2 test_frame.ip_fragment_offset = 0 test_frame.ip_ttl = 64 test_frame.ip_protocol = 0x11 test_frame.ip_header_checksum = None test_frame.ip_source_ip = 0xc0a80164 test_frame.ip_dest_ip = 0xc0a80166 test_frame.payload = bytearray(range(32)) test_frame.build() ip_source.send(test_frame) yield clk.posedge yield clk.posedge yield wait_normal() yield clk.posedge yield clk.posedge assert tx_error_arp_failed_asserted assert eth_source.empty() assert eth_sink.empty() assert ip_source.empty() assert ip_sink.empty() yield delay(100) raise StopSimulation
def check(): yield delay(100) yield clk.posedge rst.next = 1 yield clk.posedge rst.next = 0 yield clk.posedge yield delay(100) yield clk.posedge # testbench stimulus key = [ 0x6d, 0x5a, 0x56, 0xda, 0x25, 0x5b, 0x0e, 0xc2, 0x41, 0x67, 0x25, 0x3d, 0x43, 0xa3, 0x8f, 0xb0, 0xd0, 0xca, 0x2b, 0xcb, 0xae, 0x7b, 0x30, 0xb4, 0x77, 0xcb, 0x2d, 0xa3, 0x80, 0x30, 0xf2, 0x0c, 0x6a, 0x42, 0xb7, 0x3b, 0xbe, 0xac, 0x01, 0xfa ] hash_key.next = int.from_bytes(key, 'big') for payload_len in list(range(1, 128)) + list([1024, 1500, 9000, 9214 ]): yield clk.posedge print("test 1: test raw ethernet frame, length %d" % payload_len) current_test.next = 1 test_frame = eth_ep.EthFrame() test_frame.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame.eth_src_mac = 0x5A5152535455 test_frame.eth_type = 0x8000 test_frame.payload = bytearray( (x % 256 for x in range(payload_len))) axis_frame = test_frame.build_axis() for wait in wait_normal, wait_pause_source: source.send(axis_frame) yield clk.posedge yield clk.posedge yield wait() yield sink.wait() rx_hash = sink.recv().data[0] print(rx_hash) assert rx_hash[1] == 0b0000 assert sink.empty() yield delay(100) yield clk.posedge print("test 2: test raw IP frame, length %d" % payload_len) current_test.next = 2 test_frame = ip_ep.IPFrame() test_frame.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame.eth_src_mac = 0x5A5152535455 test_frame.eth_type = 0x0800 test_frame.ip_version = 4 test_frame.ip_ihl = 5 test_frame.ip_length = None test_frame.ip_identification = 0 test_frame.ip_flags = 2 test_frame.ip_fragment_offset = 0 test_frame.ip_ttl = 64 test_frame.ip_protocol = 0x1 test_frame.ip_header_checksum = None test_frame.ip_source_ip = 0xc0a80164 test_frame.ip_dest_ip = 0xc0a80165 test_frame.payload = bytearray( (x % 256 for x in range(payload_len))) axis_frame = test_frame.build_axis() for wait in wait_normal, wait_pause_source: source.send(axis_frame) yield clk.posedge yield clk.posedge yield wait() yield sink.wait() rx_hash = sink.recv().data[0] print(rx_hash) h = hash_toep( tuple_pack(test_frame.ip_source_ip, test_frame.ip_dest_ip), key) print(hex(h)) assert rx_hash[0] == h assert rx_hash[1] == 0b0001 assert sink.empty() yield delay(100) yield clk.posedge print("test 3: test UDP frame, length %d" % payload_len) current_test.next = 3 test_frame = udp_ep.UDPFrame() test_frame.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame.eth_src_mac = 0x5A5152535455 test_frame.eth_type = 0x0800 test_frame.ip_version = 4 test_frame.ip_ihl = 5 test_frame.ip_length = None test_frame.ip_identification = 0 test_frame.ip_flags = 2 test_frame.ip_fragment_offset = 0 test_frame.ip_ttl = 64 test_frame.ip_protocol = 0x11 test_frame.ip_header_checksum = None test_frame.ip_source_ip = 0xc0a80164 test_frame.ip_dest_ip = 0xc0a80165 test_frame.udp_source_port = 1 test_frame.udp_dest_port = 2 test_frame.udp_length = None test_frame.udp_checksum = None test_frame.payload = bytearray( (x % 256 for x in range(payload_len))) axis_frame = test_frame.build_axis() for wait in wait_normal, wait_pause_source: source.send(axis_frame) yield clk.posedge yield clk.posedge yield wait() yield sink.wait() rx_hash = sink.recv().data[0] print(rx_hash) h = hash_toep( tuple_pack(test_frame.ip_source_ip, test_frame.ip_dest_ip, test_frame.udp_source_port, test_frame.udp_dest_port), key) print(hex(h)) assert rx_hash[0] == h assert rx_hash[1] == 0b1001 assert sink.empty() yield delay(100) yield clk.posedge print("test 4: test TCP frame, length %d" % payload_len) current_test.next = 4 test_frame = ip_ep.IPFrame() test_frame.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame.eth_src_mac = 0x5A5152535455 test_frame.eth_type = 0x0800 test_frame.ip_version = 4 test_frame.ip_ihl = 5 test_frame.ip_length = None test_frame.ip_identification = 0 test_frame.ip_flags = 2 test_frame.ip_fragment_offset = 0 test_frame.ip_ttl = 64 test_frame.ip_protocol = 0x6 test_frame.ip_header_checksum = None test_frame.ip_source_ip = 0xc0a80164 test_frame.ip_dest_ip = 0xc0a80165 test_frame.payload = b'\x12\x34\x43\x21' + bytearray( (x % 256 for x in range(payload_len))) axis_frame = test_frame.build_axis() for wait in wait_normal, wait_pause_source: source.send(axis_frame) yield clk.posedge yield clk.posedge yield wait() yield sink.wait() rx_hash = sink.recv().data[0] print(rx_hash) h = hash_toep( tuple_pack(test_frame.ip_source_ip, test_frame.ip_dest_ip, 0x1234, 0x4321), key) print(hex(h)) assert rx_hash[0] == h assert rx_hash[1] == 0b0101 assert sink.empty() yield delay(100) yield clk.posedge print("test 5: back-to-back frames, length %d" % payload_len) current_test.next = 5 test_frame1 = eth_ep.EthFrame() test_frame1.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame1.eth_src_mac = 0x5A5152535455 test_frame1.eth_type = 0x8000 test_frame1.payload = bytearray( (x % 256 for x in range(payload_len))) test_frame2 = ip_ep.IPFrame() test_frame2.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame2.eth_src_mac = 0x5A5152535455 test_frame2.eth_type = 0x0800 test_frame2.ip_version = 4 test_frame2.ip_ihl = 5 test_frame2.ip_length = None test_frame2.ip_identification = 0 test_frame2.ip_flags = 2 test_frame2.ip_fragment_offset = 0 test_frame2.ip_ttl = 64 test_frame2.ip_protocol = 0x1 test_frame2.ip_header_checksum = None test_frame2.ip_source_ip = 0xc0a80164 test_frame2.ip_dest_ip = 0xc0a80165 test_frame2.payload = bytearray( (x % 256 for x in range(payload_len))) test_frame3 = udp_ep.UDPFrame() test_frame3.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame3.eth_src_mac = 0x5A5152535455 test_frame3.eth_type = 0x0800 test_frame3.ip_version = 4 test_frame3.ip_ihl = 5 test_frame3.ip_length = None test_frame3.ip_identification = 0 test_frame3.ip_flags = 2 test_frame3.ip_fragment_offset = 0 test_frame3.ip_ttl = 64 test_frame3.ip_protocol = 0x11 test_frame3.ip_header_checksum = None test_frame3.ip_source_ip = 0xc0a80164 test_frame3.ip_dest_ip = 0xc0a80165 test_frame3.udp_source_port = 1 test_frame3.udp_dest_port = 2 test_frame3.udp_length = None test_frame3.udp_checksum = None test_frame3.payload = bytearray( (x % 256 for x in range(payload_len))) test_frame4 = ip_ep.IPFrame() test_frame4.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame4.eth_src_mac = 0x5A5152535455 test_frame4.eth_type = 0x0800 test_frame4.ip_version = 4 test_frame4.ip_ihl = 5 test_frame4.ip_length = None test_frame4.ip_identification = 0 test_frame4.ip_flags = 2 test_frame4.ip_fragment_offset = 0 test_frame4.ip_ttl = 64 test_frame4.ip_protocol = 0x6 test_frame4.ip_header_checksum = None test_frame4.ip_source_ip = 0xc0a80164 test_frame4.ip_dest_ip = 0xc0a80165 test_frame4.payload = b'\x12\x34\x43\x21' + bytearray( (x % 256 for x in range(payload_len))) axis_frame1 = test_frame1.build_axis() axis_frame2 = test_frame2.build_axis() axis_frame3 = test_frame3.build_axis() axis_frame4 = test_frame4.build_axis() for wait in wait_normal, wait_pause_source: source.send(axis_frame1) source.send(axis_frame2) source.send(axis_frame3) source.send(axis_frame4) yield clk.posedge yield clk.posedge yield wait() yield sink.wait() rx_hash = sink.recv().data[0] print(rx_hash) assert rx_hash[1] == 0b0000 yield sink.wait() rx_hash = sink.recv().data[0] print(rx_hash) h = hash_toep( tuple_pack(test_frame2.ip_source_ip, test_frame2.ip_dest_ip), key) print(hex(h)) assert rx_hash[0] == h assert rx_hash[1] == 0b0001 yield sink.wait() rx_hash = sink.recv().data[0] print(rx_hash) h = hash_toep( tuple_pack(test_frame3.ip_source_ip, test_frame3.ip_dest_ip, test_frame3.udp_source_port, test_frame3.udp_dest_port), key) print(hex(h)) assert rx_hash[0] == h assert rx_hash[1] == 0b1001 yield sink.wait() rx_hash = sink.recv().data[0] print(rx_hash) h = hash_toep( tuple_pack(test_frame4.ip_source_ip, test_frame4.ip_dest_ip, 0x1234, 0x4321), key) print(hex(h)) assert rx_hash[0] == h assert rx_hash[1] == 0b0101 assert sink.empty() yield delay(100) yield clk.posedge print("test 6: hash test") current_test.next = 6 test_frame = udp_ep.UDPFrame() test_frame.eth_dest_mac = 0xDAD1D2D3D4D5 test_frame.eth_src_mac = 0x5A5152535455 test_frame.eth_type = 0x0800 test_frame.ip_version = 4 test_frame.ip_ihl = 5 test_frame.ip_length = None test_frame.ip_identification = 0 test_frame.ip_flags = 2 test_frame.ip_fragment_offset = 0 test_frame.ip_ttl = 64 test_frame.ip_protocol = 0x11 test_frame.ip_header_checksum = None test_frame.ip_source_ip = 0x420995bb test_frame.ip_dest_ip = 0xa18e6450 test_frame.udp_source_port = 2794 test_frame.udp_dest_port = 1766 test_frame.udp_length = None test_frame.udp_checksum = None test_frame.payload = bytearray((x % 256 for x in range(128))) axis_frame = test_frame.build_axis() for wait in wait_normal, wait_pause_source: source.send(axis_frame) yield clk.posedge yield clk.posedge yield wait() yield sink.wait() rx_hash = sink.recv().data[0] print(rx_hash) h = hash_toep( tuple_pack(test_frame.ip_source_ip, test_frame.ip_dest_ip, test_frame.udp_source_port, test_frame.udp_dest_port), key) print(hex(h)) assert rx_hash[0] == h assert rx_hash[1] == 0b1001 assert h == 0x51ccc178 assert sink.empty() yield delay(100) raise StopSimulation