VAT-to-PAPI: VPPCounters
[csit.git] / resources / traffic_scripts / policer.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 IPsec verification."""
17
18 import sys
19 import logging
20
21 # pylint: disable=no-name-in-module
22 # pylint: disable=import-error
23 logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
24
25 from scapy.all import Ether
26 from scapy.layers.inet import IP, TCP
27 from scapy.layers.inet6 import IPv6, ICMPv6ND_NS
28 from ipaddress import ip_address
29
30 from resources.libraries.python.TrafficScriptArg import TrafficScriptArg
31 from resources.libraries.python.PacketVerifier import RxQueue, TxQueue
32
33
34 def check_ipv4(pkt_recv, dscp):
35     """Check received IPv4 IPsec packet.
36
37     :param pkt_recv: Received packet to verify.
38     :param dscp: DSCP value to check.
39     :type pkt_recv: scapy.Ether
40     :type dscp: int
41     :raises RuntimeError: If received packet is invalid.
42     """
43     if not pkt_recv.haslayer(IP):
44         raise RuntimeError('Not an IPv4 packet received: {0}'.
45                            format(pkt_recv.__repr__()))
46
47     rx_dscp = pkt_recv[IP].tos >> 2
48     if rx_dscp != dscp:
49         raise RuntimeError('Invalid DSCP {0} should be {1}'.
50                            format(rx_dscp, dscp))
51
52     if not pkt_recv.haslayer(TCP):
53         raise RuntimeError('Not a TCP packet received: {0}'.
54                            format(pkt_recv.__repr__()))
55
56
57 def check_ipv6(pkt_recv, dscp):
58     """Check received IPv6 IPsec packet.
59
60     :param pkt_recv: Received packet to verify.
61     :param dscp: DSCP value to check.
62     :type pkt_recv: scapy.Ether
63     :type dscp: int
64     :raises RuntimeError: If received packet is invalid.
65     """
66     if not pkt_recv.haslayer(IPv6):
67         raise RuntimeError('Not an IPv6 packet received: {0}'.
68                            format(pkt_recv.__repr__()))
69
70     rx_dscp = pkt_recv[IPv6].tc >> 2
71     if rx_dscp != dscp:
72         raise RuntimeError('Invalid DSCP {0} should be {1}'.
73                            format(rx_dscp, dscp))
74
75     if not pkt_recv.haslayer(TCP):
76         raise RuntimeError('Not a TCP packet received: {0}'.
77                            format(pkt_recv.__repr__()))
78
79
80 # pylint: disable=too-many-locals
81 # pylint: disable=too-many-statements
82 def main():
83     """Send and receive TCP packet."""
84     args = TrafficScriptArg(['src_mac', 'dst_mac', 'src_ip', 'dst_ip', 'dscp'])
85
86     rxq = RxQueue(args.get_arg('rx_if'))
87     txq = TxQueue(args.get_arg('tx_if'))
88
89     src_mac = args.get_arg('src_mac')
90     dst_mac = args.get_arg('dst_mac')
91     src_ip = args.get_arg('src_ip')
92     dst_ip = args.get_arg('dst_ip')
93     dscp = int(args.get_arg('dscp'))
94
95     if 6 == ip_address(unicode(src_ip)).version:
96         is_ipv4 = False
97     else:
98         is_ipv4 = True
99
100     sent_packets = []
101
102     if is_ipv4:
103         ip_pkt = (IP(src=src_ip, dst=dst_ip) /
104                   TCP())
105     else:
106         ip_pkt = (IPv6(src=src_ip, dst=dst_ip) /
107                   TCP())
108
109     pkt_send = (Ether(src=src_mac, dst=dst_mac) /
110                 ip_pkt)
111
112     sent_packets.append(pkt_send)
113     txq.send(pkt_send)
114
115     while True:
116         pkt_recv = rxq.recv(2, sent_packets)
117         if pkt_recv is None:
118             raise RuntimeError('ICMPv6 echo reply Rx timeout')
119
120         if pkt_recv.haslayer(ICMPv6ND_NS):
121             # read another packet in the queue if the current one is ICMPv6ND_NS
122             continue
123         else:
124             # otherwise process the current packet
125             break
126
127     if pkt_recv is None:
128         raise RuntimeError('Rx timeout')
129
130     if is_ipv4:
131         check_ipv4(pkt_recv, dscp)
132     else:
133         check_ipv6(pkt_recv, dscp)
134
135     sys.exit(0)
136
137
138 if __name__ == "__main__":
139     main()