Tolerate failures when setting MTU
[csit.git] / resources / traffic_scripts / send_vxlan_check_vxlan.py
1 #!/usr/bin/env python
2 # Copyright (c) 2016 Cisco and/or its affiliates.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 """Traffic script that sends an IP ICMPv4/ICMPv6 packet from one interface to
16 the other one. Dot1q or Dot1ad tagging of the ethernet frame can be set.
17 """
18
19 import sys
20
21 import vxlan
22
23 from scapy.layers.inet import IP, UDP, Raw
24 from scapy.layers.l2 import Ether
25 from resources.libraries.python.PacketVerifier import RxQueue, TxQueue
26 from resources.libraries.python.TrafficScriptArg import TrafficScriptArg
27
28
29 def main():
30     """Send IP ICMPv4/ICMPv6 packet from one traffic generator interface to
31     the other one. Dot1q or Dot1ad tagging of the ethernet frame can be set.
32     """
33     args = TrafficScriptArg(['tx_src_mac', 'tx_dst_mac', 'tx_src_ip',
34                              'tx_dst_ip', 'tx_vni', 'rx_src_ip', 'rx_dst_ip',
35                              'rx_vni'])
36
37     tx_if = args.get_arg('tx_if')
38     rx_if = args.get_arg('rx_if')
39     tx_src_mac = args.get_arg('tx_src_mac')
40     tx_dst_mac = args.get_arg('tx_dst_mac')
41     tx_src_ip = args.get_arg('tx_src_ip')
42     tx_dst_ip = args.get_arg('tx_dst_ip')
43     tx_vni = args.get_arg('tx_vni')
44     rx_src_ip = args.get_arg('rx_src_ip')
45     rx_dst_ip = args.get_arg('rx_dst_ip')
46     rx_vni = args.get_arg('rx_vni')
47
48     rxq = RxQueue(rx_if)
49     txq = TxQueue(tx_if)
50
51     sent_packets = []
52
53     tx_pkt_p = (Ether(src='02:00:00:00:00:01', dst='02:00:00:00:00:02') /
54                 IP(src='192.168.1.1', dst='192.168.1.2') /
55                 UDP(sport=12345, dport=1234) /
56                 Raw('rew data'))
57
58     pkt_raw = (Ether(src=tx_src_mac, dst=tx_dst_mac) /
59                IP(src=tx_src_ip, dst=tx_dst_ip) /
60                UDP(sport=23456) /
61                vxlan.VXLAN(vni=int(tx_vni)) /
62                tx_pkt_p)
63
64     # Send created packet on one interface and receive on the other
65     sent_packets.append(pkt_raw)
66     txq.send(pkt_raw)
67
68     ether = rxq.recv(2, ignore=sent_packets)
69
70     # Check whether received packet contains layers Ether, IP and VXLAN
71     if ether is None:
72         raise RuntimeError('Packet Rx timeout')
73     ip = ether.payload
74
75     if ip.src != rx_src_ip:
76         raise RuntimeError('IP src mismatch {} != {}'.format(ip.src, rx_src_ip))
77     if ip.dst != rx_dst_ip:
78         raise RuntimeError('IP dst mismatch {} != {}'.format(ip.dst, rx_dst_ip))
79     if ip.payload.dport != 4789:
80         raise RuntimeError('VXLAN UDP port mismatch {} != {}'.
81                            format(ip.payload.dport, 4789))
82     vxlan_pkt = ip.payload.payload
83
84     if int(vxlan_pkt.vni) != int(rx_vni):
85         raise RuntimeError('vxlan mismatch')
86     rx_pkt_p = vxlan_pkt.payload
87
88     if rx_pkt_p.src != tx_pkt_p.src:
89         raise RuntimeError('RX encapsulated MAC src mismatch {} != {}'.
90                            format(rx_pkt_p.src, tx_pkt_p.src))
91     if rx_pkt_p.dst != tx_pkt_p.dst:
92         raise RuntimeError('RX encapsulated MAC dst mismatch {} != {}'.
93                            format(rx_pkt_p.dst, tx_pkt_p.dst))
94     if rx_pkt_p['IP'].src != tx_pkt_p['IP'].src:
95         raise RuntimeError('RX encapsulated IP src mismatch {} != {}'.
96                            format(rx_pkt_p['IP'].src, tx_pkt_p['IP'].src))
97     if rx_pkt_p['IP'].dst != tx_pkt_p['IP'].dst:
98         raise RuntimeError('RX encapsulated IP dst mismatch {} != {}'.
99                            format(rx_pkt_p['IP'].dst, tx_pkt_p['IP'].dst))
100
101     # TODO: verify inner Ether()
102
103     sys.exit(0)
104
105 if __name__ == "__main__":
106     main()