[dts] [PATCH] framework packet: support configure multiple layers

Marvin Liu yong.liu at intel.com
Wed Apr 20 03:07:16 CEST 2016


Add new function config_layers in packet module which support configure
multiple layers in one function.
Raw content will be in hexadecimal format, it will be easier for
configuration.

Signed-off-by: Marvin Liu <yong.liu at intel.com>

diff --git a/framework/packet.py b/framework/packet.py
index 2f3bf53..42ac4a5 100755
--- a/framework/packet.py
+++ b/framework/packet.py
@@ -60,16 +60,15 @@ from scapy.route import *
 from scapy.packet import bind_layers, Raw
 from scapy.sendrecv import sendp
 
-
 # load extension layers
+exec_file = os.path.realpath(__file__)
+DTS_PATH = exec_file.replace('/framework/packet.py', '')
+DEP_FOLDER = DTS_PATH + '/dep'
+sys.path.append(DEP_FOLDER)
+
 from vxlan import Vxlan
-bind_layers(UDP, Vxlan, dport=4789)
-bind_layers(Vxlan, Ether)
 from nvgre import NVGRE, IPPROTO_NVGRE
-bind_layers(IP, NVGRE, proto=IPPROTO_NVGRE)
-bind_layers(NVGRE, Ether)
 from lldp import LLDP, LLDPManagementAddress
-bind_layers(Ether, LLDP, type=0x88cc)
 
 # packet generator type should be configured later
 PACKETGEN = "scapy"
@@ -269,8 +268,8 @@ class scapy(object):
     def raw(self, payload=None):
         if payload is not None:
             self.pkt[Raw].load = ''
-            for load in payload:
-                self.pkt[Raw].load += '%c' % int(load, 16)
+            for hex1, hex2 in payload:
+                self.pkt[Raw].load += struct.pack("=B", int('%s%s' %(hex1, hex2), 16))
 
     def vxlan(self, vni=0):
         self.pkt[Vxlan].vni = vni
@@ -534,16 +533,29 @@ class Packet(object):
                 idx = self.pkt_layers.index(layer)
         except Exception as e:
             print "INVALID LAYER ID %s" % layer
-            return -1
+            return False
 
         if self.check_layer_config(layer, config) is False:
-            return -1
+            return False
 
         layer_conf = getattr(self, "_config_layer_%s" % layer)
         setattr(self, 'configured_layer_%s' % layer, True)
 
         return layer_conf(config)
 
+    def config_layers(self, layers=None):
+        """
+        Configure packet with multi configurations
+        """
+        for layer in layers:
+            name, config = layer
+            if name not in self.pkt_layers:
+                print "[%s] is missing in packet!!!" % name
+                raise
+            if self.config_layer(name, config) is False:
+                print "[%s] failed to configure!!!" % name
+                raise
+
     def _config_layer_ether(self, config):
         return self.pktgen.ether(**config)
 
@@ -767,3 +779,13 @@ if __name__ == "__main__":
     pkt.config_layer('udp', {'src': 4789, 'dst': 4789, 'chksum': 0x1111})
     pkt.config_layer('vxlan', {'vni': 2})
     pkt.config_layer('raw', {'payload': ['58'] * 18})
+    pkt.send_pkt(tx_port='lo')
+
+    pkt = Packet()
+    pkt.assign_layers(['ether', 'dot1q', 'ipv4', 'udp',
+                       '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})])
+
+    pkt.send_pkt(tx_port='lo')
-- 
1.9.3



More information about the dts mailing list