CSIT-128: Remove EXPECTED_FAILING tag from VXLAN over IPv6 test cases
[csit.git] / resources / traffic_scripts / icmpv6_echo.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 """Traffic script for ICMPv6 echo test."""
17
18 import sys
19 import logging
20 logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
21 from resources.libraries.python.PacketVerifier import RxQueue, TxQueue,\
22     checksum_equal
23 from resources.libraries.python.TrafficScriptArg import TrafficScriptArg
24 from scapy.layers.inet6 import IPv6, ICMPv6ND_NA, ICMPv6NDOptDstLLAddr
25 from scapy.layers.inet6 import ICMPv6EchoRequest, ICMPv6EchoReply
26 from scapy.all import Ether
27
28
29 def main():
30     args = TrafficScriptArg(['src_mac', 'dst_mac', 'src_ip', 'dst_ip'])
31
32     rxq = RxQueue(args.get_arg('rx_if'))
33     txq = TxQueue(args.get_arg('tx_if'))
34
35     src_mac = args.get_arg('src_mac')
36     dst_mac = args.get_arg('dst_mac')
37     src_ip = args.get_arg('src_ip')
38     dst_ip = args.get_arg('dst_ip')
39     echo_id = 0xa
40     echo_seq = 0x1
41
42     sent_packets = []
43
44     # send ICMPv6 neighbor advertisement message
45     pkt_send = (Ether(src=src_mac, dst='ff:ff:ff:ff:ff:ff') /
46                         IPv6(src=src_ip, dst='ff02::1:ff00:2') /
47                         ICMPv6ND_NA(tgt=src_ip, R=0) /
48                         ICMPv6NDOptDstLLAddr(lladdr=src_mac))
49     sent_packets.append(pkt_send)
50     txq.send(pkt_send)
51
52     # send ICMPv6 echo request
53     pkt_send = (Ether(src=src_mac, dst=dst_mac) /
54                         IPv6(src=src_ip, dst=dst_ip) /
55                         ICMPv6EchoRequest(id=echo_id, seq=echo_seq))
56     sent_packets.append(pkt_send)
57     txq.send(pkt_send)
58
59     # receive ICMPv6 echo reply
60     ether = rxq.recv(2, sent_packets)
61     if ether is None:
62         raise RuntimeError('ICMPv6 echo reply Rx timeout')
63
64     if not ether.haslayer(IPv6):
65         raise RuntimeError('Unexpected packet with no IPv6 received {0}'.format(
66             ether.__repr__()))
67
68     ipv6 = ether['IPv6']
69
70     if not ipv6.haslayer(ICMPv6EchoReply):
71         raise RuntimeError(
72             'Unexpected packet with no IPv6 ICMP received {0}'.format(
73                 ipv6.__repr__()))
74
75     icmpv6 = ipv6['ICMPv6 Echo Reply']
76
77     # check identifier and sequence number
78     if icmpv6.id != echo_id or icmpv6.seq != echo_seq:
79         raise RuntimeError(
80             'Invalid ICMPv6 echo reply received ID {0} seq {1} should be ' \
81             'ID {2} seq {3}'.format(icmpv6.id, icmpv6.seq, echo_id, echo_seq))
82
83     # verify checksum
84     cksum = icmpv6.cksum
85     del icmpv6.cksum
86     tmp = ICMPv6EchoReply(str(icmpv6))
87     if not checksum_equal(tmp.cksum, cksum):
88         raise RuntimeError(
89             'Invalid checksum {0} should be {1}'.format(cksum, tmp.cksum))
90
91     sys.exit(0)
92
93 if __name__ == "__main__":
94     main()