aboutsummaryrefslogtreecommitdiffstats
path: root/GPL/traffic_scripts/policer.py
diff options
context:
space:
mode:
authorVratko Polak <vrpolak@cisco.com>2020-05-04 13:05:26 +0200
committerVratko Polak <vrpolak@cisco.com>2020-05-06 14:03:21 +0000
commit79f5ba9bf7656972dd988508eff9465562dde42c (patch)
treea84714e81d65bfc4f0454fa8e327d4129f58ac79 /GPL/traffic_scripts/policer.py
parent16ef90a2415aa3b65341fdb6517cf58721bfff7a (diff)
Separate files needing GPL license
+ Keep apache license for now, until this is completed: https://wiki.fd.io/view/TSC/Relicensing_Procedure + Add utilities for switching license comment blocks. - They do not preserve attributes, so executable flag is lost. + Move the affected files to GPL/. + Update paths so files are executed from the new location. + Change the way scripts are started to do not require executable flag. + Employ OptionString when constructing longer command lines. + Move also PacketVerifier.py and TrafficScriptArg.py as they are linked with traffic scripts. + That means the two files are outside "resources" package tree now. + Added __init__.py files so relative imports work in new package tree. + Start traffic scripts as python modules to allow relative imports. + Once again needed because they are outside the default PYTHONPATH. Change-Id: Ieb135629e890adbaf5b79497570f3be25b746f9f Signed-off-by: Vratko Polak <vrpolak@cisco.com>
Diffstat (limited to 'GPL/traffic_scripts/policer.py')
-rw-r--r--GPL/traffic_scripts/policer.py122
1 files changed, 122 insertions, 0 deletions
diff --git a/GPL/traffic_scripts/policer.py b/GPL/traffic_scripts/policer.py
new file mode 100644
index 0000000000..db90bb21f3
--- /dev/null
+++ b/GPL/traffic_scripts/policer.py
@@ -0,0 +1,122 @@
+#!/usr/bin/env python3
+
+# Copyright (c) 2020 Cisco and/or its affiliates.
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""Traffic script for IPsec verification."""
+
+import sys
+import logging
+
+from ipaddress import ip_address
+from scapy.layers.l2 import Ether
+from scapy.layers.inet import IP, TCP
+from scapy.layers.inet6 import IPv6, ICMPv6ND_NS
+from scapy.packet import Raw
+
+from .TrafficScriptArg import TrafficScriptArg
+from .PacketVerifier import RxQueue, TxQueue
+
+
+def check_ipv4(pkt_recv, dscp):
+ """Check received IPv4 IPsec packet.
+
+ :param pkt_recv: Received packet to verify.
+ :param dscp: DSCP value to check.
+ :type pkt_recv: scapy.Ether
+ :type dscp: int
+ :raises RuntimeError: If received packet is invalid.
+ """
+ if not pkt_recv.haslayer(IP):
+ raise RuntimeError(f"Not an IPv4 packet received: {pkt_recv!r}")
+
+ rx_dscp = pkt_recv[IP].tos >> 2
+ if rx_dscp != dscp:
+ raise RuntimeError(f"Invalid DSCP {rx_dscp} should be {dscp}")
+
+ if not pkt_recv.haslayer(TCP):
+ raise RuntimeError(f"Not a TCP packet received: {pkt_recv!r}")
+
+
+def check_ipv6(pkt_recv, dscp):
+ """Check received IPv6 IPsec packet.
+
+ :param pkt_recv: Received packet to verify.
+ :param dscp: DSCP value to check.
+ :type pkt_recv: scapy.Ether
+ :type dscp: int
+ :raises RuntimeError: If received packet is invalid.
+ """
+ if not pkt_recv.haslayer(IPv6):
+ raise RuntimeError(f"Not an IPv6 packet received: {pkt_recv!r}")
+
+ rx_dscp = pkt_recv[IPv6].tc >> 2
+ if rx_dscp != dscp:
+ raise RuntimeError(f"Invalid DSCP {rx_dscp} should be {dscp}")
+
+ if not pkt_recv.haslayer(TCP):
+ raise RuntimeError(f"Not a TCP packet received: {pkt_recv!r}")
+
+
+# TODO: Pylint says too-many-locals and too-many-statements. Refactor!
+def main():
+ """Send and receive TCP packet."""
+ args = TrafficScriptArg(
+ [u"src_mac", u"dst_mac", u"src_ip", u"dst_ip", u"dscp"]
+ )
+
+ rxq = RxQueue(args.get_arg(u"rx_if"))
+ txq = TxQueue(args.get_arg(u"tx_if"))
+
+ src_mac = args.get_arg(u"src_mac")
+ dst_mac = args.get_arg(u"dst_mac")
+ src_ip = args.get_arg(u"src_ip")
+ dst_ip = args.get_arg(u"dst_ip")
+ dscp = int(args.get_arg(u"dscp"))
+
+ ip_layer = IPv6 if ip_address(src_ip).version == 6 else IP
+
+ sent_packets = list()
+ pkt_send = (Ether(src=src_mac, dst=dst_mac) /
+ ip_layer(src=src_ip, dst=dst_ip) /
+ TCP())
+
+ pkt_send /= Raw()
+ sent_packets.append(pkt_send)
+ txq.send(pkt_send)
+
+ while True:
+ pkt_recv = rxq.recv(2, sent_packets)
+ if pkt_recv is None:
+ raise RuntimeError(u"ICMPv6 echo reply Rx timeout")
+
+ if pkt_recv.haslayer(ICMPv6ND_NS):
+ # read another packet in the queue if the current one is ICMPv6ND_NS
+ continue
+ else:
+ # otherwise process the current packet
+ break
+
+ if pkt_recv is None:
+ raise RuntimeError(u"Rx timeout")
+
+ if ip_layer == IP:
+ check_ipv4(pkt_recv, dscp)
+ else:
+ check_ipv6(pkt_recv, dscp)
+
+ sys.exit(0)
+
+
+if __name__ == u"__main__":
+ main()