From: Marvin Liu <yong.liu@intel.com>
To: dts@dpdk.org
Cc: Yong Liu <yong.liu@intel.com>
Subject: [dts] [PATCH] framework packet: add function for strip chksum
Date: Tue, 21 Feb 2017 01:09:30 +0800 [thread overview]
Message-ID: <1487610570-115376-1-git-send-email-yong.liu@intel.com> (raw)
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
reply other threads:[~2017-02-20 9:18 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1487610570-115376-1-git-send-email-yong.liu@intel.com \
--to=yong.liu@intel.com \
--cc=dts@dpdk.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).