From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by dpdk.org (Postfix) with ESMTP id EBFDE2C66 for ; Mon, 20 Feb 2017 10:18:06 +0100 (CET) Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga103.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 20 Feb 2017 01:18:05 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.35,185,1484035200"; d="scan'208";a="1132290368" Received: from marvin-s2600gz.sh.intel.com ([10.239.129.187]) by fmsmga002.fm.intel.com with ESMTP; 20 Feb 2017 01:18:04 -0800 From: Marvin Liu To: dts@dpdk.org Cc: Yong Liu Date: Tue, 21 Feb 2017 01:09:30 +0800 Message-Id: <1487610570-115376-1-git-send-email-yong.liu@intel.com> X-Mailer: git-send-email 1.9.1 Subject: [dts] [PATCH] framework packet: add function for strip chksum X-BeenThere: dts@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: test suite reviews and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 20 Feb 2017 09:18:07 -0000 Signed-off-by: Yong Liu diff --git a/framework/packet.py b/framework/packet.py index fb99d35..cd2d7e7 100755 --- a/framework/packet.py +++ b/framework/packet.py @@ -43,6 +43,7 @@ import random import subprocess from uuid import uuid4 from settings import FOLDERS +import binascii from scapy.config import conf conf.use_pcap = True @@ -165,7 +166,7 @@ class scapy(object): def vlan(self, pkt_layer, vlan, prio=0, type=None): if pkt_layer.name != "802.1Q": - return + return pkt_layer.vlan = int(vlan) pkt_layer.prio = prio if type is not None: @@ -183,7 +184,7 @@ class scapy(object): def etag(self, pkt_layer, ECIDbase=0, prio=0, type=None): if pkt_layer.name != "802.1BR": - return + return pkt_layer.ECIDbase = int(ECIDbase) pkt_layer.prio = prio if type is not None: @@ -303,7 +304,7 @@ class scapy(object): if payload is not None: pkt_layer.load = '' for hex1, hex2 in payload: - pkt_layer.load += struct.pack("=B", int('%s%s' %(hex1, hex2), 16)) + pkt_layer.load += struct.pack("=B", int('%s%s' % (hex1, hex2), 16)) def gre(self, pkt_layer, proto=None): if proto is not None: @@ -500,11 +501,11 @@ class Packet(object): 'VLAN': 'vlan', 'ETAG': 'etag', 'IP': 'ipv4', - 'IPv4-TUNNEL':'inner_ipv4', + 'IPv4-TUNNEL': 'inner_ipv4', 'IPihl': 'ipv4ihl', 'IPFRAG': 'ipv4_ext', 'IPv6': 'ipv6', - 'IPv6-TUNNEL':'inner_ipv6', + 'IPv6-TUNNEL': 'inner_ipv6', 'IPv6FRAG': 'ipv6_frag', 'IPv6EXT': 'ipv6_ext', 'IPv6EXT2': 'ipv6_ext2', @@ -714,7 +715,7 @@ def sniff_packets(intf, count=0, timeout=5): param = "" direct_param = r"(\s+)\[ -(\w) in\|out\|inout \]" tcpdump_help = subprocess.check_output("tcpdump -h; echo 0", - stderr=subprocess.STDOUT, shell=True) + stderr=subprocess.STDOUT, shell=True) for line in tcpdump_help.split('\n'): m = re.match(direct_param, line) if m: @@ -808,6 +809,7 @@ def compare_pktload(pkt1=None, pkt2=None, layer="L2"): else: return False + def strip_pktload(pkt=None, layer="L2"): if layer == "L2": l_idx = 0 @@ -825,6 +827,47 @@ def strip_pktload(pkt=None, layer="L2"): return load + +def get_checksum(pkt=None): + """ + Strip checksum value of one packet, assume one ipv6 layer + """ + chksum = {} + ascii_str = pkt.pktgen.pkt.do_build() + pkt.pktgen.pkt.show2() + hex_str = binascii.b2a_hex(ascii_str) + ip_chk_offset = 14 * 2 + 20 + if pkt.pkt_type == 'UDP': + chksum['ipv4'] = hex_str[ip_chk_offset:ip_chk_offset + 4] + ip_hdrlen = int(hex_str[29], 16) * 4 + udp_chk_offset = 14 * 2 + ip_hdrlen * 2 + 12 + chksum['udp'] = hex_str[udp_chk_offset:udp_chk_offset + 4] + elif pkt.pkt_type == 'TCP': + chksum['ipv4'] = hex_str[ip_chk_offset:ip_chk_offset + 4] + ip_hdrlen = int(hex_str[29], 16) * 4 + tcp_chk_offset = 14 * 2 + ip_hdrlen * 2 + 32 + chksum['tcp'] = hex_str[tcp_chk_offset:tcp_chk_offset + 4] + elif pkt.pkt_type == 'SCTP': + chksum['ipv4'] = hex_str[ip_chk_offset:ip_chk_offset + 4] + ip_hdrlen = int(hex_str[29], 16) * 4 + sctp_chk_offset = 14 * 2 + ip_hdrlen * 2 + 16 + chksum['sctp'] = hex_str[sctp_chk_offset:sctp_chk_offset + 8] + elif pkt.pkt_type == 'IPv6_UDP': + ip_hdrlen = 40 + tcp_chk_offset = 14 * 2 + ip_hdrlen * 2 + 12 + chksum['udp'] = hex_str[tcp_chk_offset:tcp_chk_offset + 4] + elif pkt.pkt_type == 'IPv6_TCP': + ip_hdrlen = 40 + tcp_chk_offset = 14 * 2 + ip_hdrlen * 2 + 32 + chksum['tcp'] = hex_str[tcp_chk_offset:tcp_chk_offset + 4] + elif pkt.pkt_type == 'IPv6_SCTP': + ip_hdrlen = 40 + sctp_chk_offset = 14 * 2 + ip_hdrlen * 2 + 16 + chksum['sctp'] = hex_str[sctp_chk_offset:sctp_chk_offset + 8] + + return chksum + + ############################################################################### ############################################################################### if __name__ == "__main__": @@ -859,6 +902,20 @@ if __name__ == "__main__": 'vxlan', 'inner_mac', 'inner_ipv4', 'inner_udp', 'raw']) # config packet pkt.config_layers([('ether', {'dst': '00:11:22:33:44:55'}), ('ipv4', {'dst': '1.1.1.1'}), - ('vxlan', {'vni': 2}), ('raw', {'payload': ['01'] * 18})]) + ('vxlan', {'vni': 2}), ('raw', {'payload': ['01'] * 18})]) pkt.send_pkt(tx_port='lo') + + # check chksum value + pkt = Packet(pkt_type='UDP') + print get_checksum(pkt) + pkt = Packet(pkt_type='TCP') + print get_checksum(pkt) + pkt = Packet(pkt_type='SCTP') + print get_checksum(pkt) + pkt = Packet(pkt_type='IPv6_UDP') + print get_checksum(pkt) + pkt = Packet(pkt_type='IPv6_TCP') + print get_checksum(pkt) + pkt = Packet(pkt_type='IPv6_SCTP') + print get_checksum(pkt) -- 1.9.3