CSIT-651 Add keywords and template for memif
[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 from scapy.all import Ether, IP, IPv6, TCP
25 from ipaddress import ip_address
26
27 from resources.libraries.python.TrafficScriptArg import TrafficScriptArg
28 from resources.libraries.python.PacketVerifier import RxQueue, TxQueue
29
30
31 def check_ipv4(pkt_recv, dscp):
32     """Check received IPv4 IPsec packet.
33
34     :param pkt_recv: Received packet to verify.
35     :param dscp: DSCP value to check.
36     :type pkt_recv: scapy.Ether
37     :type dscp: int
38     :raises RuntimeError: If received packet is invalid.
39     """
40     if not pkt_recv.haslayer(IP):
41         raise RuntimeError(
42             'Not an IPv4 packet received: {0}'.format(pkt_recv.__repr__()))
43
44     rx_dscp = pkt_recv['IP'].tos >> 2
45     if rx_dscp != dscp:
46         raise RuntimeError(
47             'Invalid DSCP {0} should be {1}'.format(rx_dscp, dscp))
48
49     if not pkt_recv.haslayer(TCP):
50         raise RuntimeError(
51             'Not a TCP packet received: {0}'.format(pkt_recv.__repr__()))
52
53
54 def check_ipv6(pkt_recv, dscp):
55     """Check received IPv6 IPsec packet.
56
57     :param pkt_recv: Received packet to verify.
58     :param dscp: DSCP value to check.
59     :type pkt_recv: scapy.Ether
60     :type dscp: int
61     :raises RuntimeError: If received packet is invalid.
62     """
63     if not pkt_recv.haslayer(IPv6):
64         raise RuntimeError(
65             'Not an IPv6 packet received: {0}'.format(pkt_recv.__repr__()))
66
67     rx_dscp = pkt_recv['IPv6'].tc >> 2
68     if rx_dscp != dscp:
69         raise RuntimeError(
70             'Invalid DSCP {0} should be {1}'.format(rx_dscp, dscp))
71
72     if not pkt_recv.haslayer(TCP):
73         raise RuntimeError(
74             'Not a TCP packet received: {0}'.format(pkt_recv.__repr__()))
75
76
77 # pylint: disable=too-many-locals
78 # pylint: disable=too-many-statements
79 def main():
80     """Send and receive TCP packet."""
81     args = TrafficScriptArg(['src_mac', 'dst_mac', 'src_ip', 'dst_ip', 'dscp'])
82
83     rxq = RxQueue(args.get_arg('rx_if'))
84     txq = TxQueue(args.get_arg('tx_if'))
85
86     src_mac = args.get_arg('src_mac')
87     dst_mac = args.get_arg('dst_mac')
88     src_ip = args.get_arg('src_ip')
89     dst_ip = args.get_arg('dst_ip')
90     dscp = int(args.get_arg('dscp'))
91
92     if 6 == ip_address(unicode(src_ip)).version:
93         is_ipv4 = False
94     else:
95         is_ipv4 = True
96
97     sent_packets = []
98
99     if is_ipv4:
100         ip_pkt = IP(src=src_ip, dst=dst_ip) / \
101                  TCP()
102     else:
103         ip_pkt = IPv6(src=src_ip, dst=dst_ip) / \
104                  TCP()
105
106     pkt_send = Ether(src=src_mac, dst=dst_mac) / \
107                ip_pkt
108
109     sent_packets.append(pkt_send)
110     txq.send(pkt_send)
111
112     pkt_recv = rxq.recv(2, sent_packets)
113
114     if pkt_recv is None:
115         raise RuntimeError('Rx timeout')
116
117     if is_ipv4:
118         check_ipv4(pkt_recv, dscp)
119     else:
120         check_ipv6(pkt_recv, dscp)
121
122     sys.exit(0)
123
124 if __name__ == "__main__":
125     main()