Remove intrusive useless logging
[csit.git] / resources / traffic_scripts / macswap_check.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2016 Cisco and/or its affiliates.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 """Macswap test."""
17
18 from scapy.all import Ether, IP
19
20 from resources.libraries.python.PacketVerifier import RxQueue, TxQueue
21 from resources.libraries.python.TrafficScriptArg import TrafficScriptArg
22
23 def check_macswap(pkt_send, pkt_recv):
24     """Compare MAC addresses of sent and received packet.
25
26     :param pkt_send: Sent packet.
27     :param pkt_recv: Received packet.
28     :type pkt_send: Ether
29     :type pkt_recv: Ether
30     """
31     print "Comparing following packets:"
32     pkt_send.show2()
33     pkt_recv.show2()
34
35     if pkt_send.dst != pkt_recv.src or pkt_send.src != pkt_recv.dst:
36         raise RuntimeError("MAC addresses were not swapped.")
37
38
39 def main():
40     """Main function."""
41     args = TrafficScriptArg(['src_mac', 'dst_mac', 'src_ip', 'dst_ip'])
42
43     rxq = RxQueue(args.get_arg('rx_if'))
44     txq = TxQueue(args.get_arg('tx_if'))
45
46     src_mac = args.get_arg('src_mac')
47     dst_mac = args.get_arg('dst_mac')
48     src_ip = args.get_arg('src_ip')
49     dst_ip = args.get_arg('dst_ip')
50
51     pkt_send = (Ether(src=src_mac, dst=dst_mac) /
52                     IP(src=src_ip, dst=dst_ip, proto=61))
53
54     sent_packets = []
55     sent_packets.append(pkt_send)
56     txq.send(pkt_send)
57
58     pkt_recv = rxq.recv(ignore=sent_packets)
59
60     if pkt_recv is None:
61         raise RuntimeError('Timeout waiting for packet')
62
63     check_macswap(pkt_send, pkt_recv)
64
65 if __name__ == "__main__":
66     main()