test suite reviews and discussions
 help / color / mirror / Atom feed
* [dts] [PATCH] framework packet: add function for strip chksum
@ 2017-02-20 17:09 Marvin Liu
  0 siblings, 0 replies; only message in thread
From: Marvin Liu @ 2017-02-20 17:09 UTC (permalink / raw)
  To: dts; +Cc: Yong Liu

Signed-off-by: Yong Liu <yong.liu@intel.com>

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

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2017-02-20  9:18 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-02-20 17:09 [dts] [PATCH] framework packet: add function for strip chksum Marvin Liu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).