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