3 # Copyright (c) 2021 Intel and/or its affiliates.
5 # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
7 # Licensed under the Apache License 2.0 or
8 # GNU General Public License v2.0 or later; you may not use this file
9 # except in compliance with one of these Licenses. You
10 # may obtain a copy of the Licenses at:
12 # http://www.apache.org/licenses/LICENSE-2.0
13 # https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
15 # Note: If this file is linked with Scapy, which is GPLv2+, your use of it
16 # must be under GPLv2+. If at any point in the future it is no longer linked
17 # with Scapy (or other GPLv2+ licensed software), you are free to choose
20 # Unless required by applicable law or agreed to in writing, software
21 # distributed under the License is distributed on an "AS IS" BASIS,
22 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 # See the License for the specific language governing permissions and
24 # limitations under the License.
26 """Traffic script that send flow packet from one interface
33 from scapy.layers.inet import IP, UDP, TCP
34 from scapy.layers.inet6 import IPv6
35 from scapy.layers.l2 import Ether, ARP
36 from scapy.packet import Raw
37 from scapy.contrib.gtp import GTP_U_Header
38 from scapy.layers.ipsec import ESP, AH
39 from scapy.layers.vxlan import VXLAN
41 from .PacketVerifier import TxQueue
42 from .TrafficScriptArg import TrafficScriptArg
46 """Send packet from one traffic generator interface to the other."""
48 args = TrafficScriptArg(
50 u"tg_if1_mac", u"dut_if1_mac", u"flow_type", u"proto",
53 u"src_ip", u"dst_ip", u"src_port", u"dst_port", u"value"
56 tx_if = args.get_arg(u"tx_if")
57 tx_src_mac = args.get_arg(u"tg_if1_mac")
58 tx_dst_mac = args.get_arg(u"dut_if1_mac")
59 flow_type = args.get_arg(u"flow_type")
60 proto = args.get_arg(u"proto")
62 src = args.get_arg(u"src_ip")
63 dst = args.get_arg(u"dst_ip")
64 sport = eval(args.get_arg(u"src_port"))
65 dport = eval(args.get_arg(u"dst_port"))
66 value = eval(args.get_arg(u"value"))
70 if flow_type == u"IP4":
71 pkt_raw = (Ether(src=tx_src_mac, dst=tx_dst_mac) /
73 elif flow_type == u"IP6":
74 pkt_raw = (Ether(src=tx_src_mac, dst=tx_dst_mac) /
75 IPv6(src=src, dst=dst))
76 elif flow_type == u"ETHER":
77 pkt_raw = Ether(src=tx_src_mac, dst=tx_dst_mac)
79 raise ValueError(f"Flow type error: {flow_type}")
82 pkt_raw /= TCP(sport=sport, dport=dport)
84 pkt_raw /= UDP(sport=sport, dport=dport)
86 pkt_raw /= AH(spi=value)
88 pkt_raw /= ESP(spi=value)
89 elif proto == u"GTPU":
90 pkt_raw /= (UDP()/GTP_U_Header(teid=value)/IP(src=u"192.168.10.20"))
91 elif proto == u"L2TPV3":
92 value_hex = hex(value).replace('0x', (8-len(hex(value))+2)*'0')
93 session_id = binascii.a2b_hex(value_hex)
95 pkt_raw /= Raw(session_id)
96 elif proto == u"VXLAN":
97 pkt_raw /= (UDP()/VXLAN(vni=value))
102 raise ValueError(f"Flow proto error: {proto}")
108 if __name__ == u"__main__":