Add flow test suites
[csit.git] / GPL / traffic_scripts / send_flow_packet.py
1 #!/usr/bin/env python3
2
3 # Copyright (c) 2021 Intel and/or its affiliates.
4 #
5 # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 #
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:
11 #
12 #     http://www.apache.org/licenses/LICENSE-2.0
13 #     https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
14 #
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
18 # Apache 2.
19 #
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.
25
26 """Traffic script that send flow packet from one interface
27 to the other.
28 """
29
30 import sys
31 import binascii
32
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
40
41 from .PacketVerifier import TxQueue
42 from .TrafficScriptArg import TrafficScriptArg
43
44
45 def main():
46     """Send packet from one traffic generator interface to the other."""
47
48     args = TrafficScriptArg(
49         [
50             u"tg_if1_mac", u"dut_if1_mac", u"flow_type", u"proto",
51         ],
52         [
53             u"src_ip", u"dst_ip", u"src_port", u"dst_port", u"value"
54         ]
55     )
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")
61
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"))
67
68     txq = TxQueue(tx_if)
69
70     if flow_type == u"IP4":
71         pkt_raw = (Ether(src=tx_src_mac, dst=tx_dst_mac) /
72                    IP(src=src, dst=dst))
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)
78     else:
79         raise ValueError(f"Flow type error: {flow_type}")
80
81     if proto == u"TCP":
82         pkt_raw /= TCP(sport=sport, dport=dport)
83     elif proto == u"UDP":
84         pkt_raw /= UDP(sport=sport, dport=dport)
85     elif proto == u"AH":
86         pkt_raw /= AH(spi=value)
87     elif proto == u"ESP":
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)
94         pkt_raw.proto = 115
95         pkt_raw /= Raw(session_id)
96     elif proto == u"VXLAN":
97         pkt_raw /= (UDP()/VXLAN(vni=value))
98     elif proto == u"ARP":
99         pkt_raw.type = value
100         pkt_raw /= ARP()
101     else:
102         raise ValueError(f"Flow proto error: {proto}")
103
104     pkt_raw /= Raw()
105     txq.send(pkt_raw)
106     sys.exit(0)
107
108 if __name__ == u"__main__":
109     main()