Renato Botelho do Couto | ead1e53 | 2019-10-31 13:31:07 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 2 | |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 3 | from __future__ import print_function |
Paul Vinciguerra | 9beabd8 | 2019-12-01 22:24:28 -0500 | [diff] [blame] | 4 | import logging |
Paul Vinciguerra | 72f0004 | 2018-11-25 11:05:13 -0800 | [diff] [blame] | 5 | import sys |
Ole Trøan | 162989e | 2018-11-26 10:27:50 +0000 | [diff] [blame] | 6 | import os |
| 7 | import select |
Paul Vinciguerra | 496b0de | 2019-06-20 12:24:12 -0400 | [diff] [blame] | 8 | import signal |
Paul Vinciguerra | d6f2217 | 2020-12-05 22:39:14 +0000 | [diff] [blame] | 9 | import subprocess |
Ole Trøan | 162989e | 2018-11-26 10:27:50 +0000 | [diff] [blame] | 10 | import unittest |
Klement Sekera | b23ffd7 | 2021-05-31 16:08:53 +0200 | [diff] [blame] | 11 | import re |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 12 | import time |
Paul Vinciguerra | 72f0004 | 2018-11-25 11:05:13 -0800 | [diff] [blame] | 13 | import faulthandler |
Ole Trøan | 162989e | 2018-11-26 10:27:50 +0000 | [diff] [blame] | 14 | import random |
| 15 | import copy |
juraj.linkes | 68ebc83 | 2018-11-29 09:37:08 +0100 | [diff] [blame] | 16 | import platform |
Klement Sekera | b23ffd7 | 2021-05-31 16:08:53 +0200 | [diff] [blame] | 17 | import shutil |
Ole Trøan | 162989e | 2018-11-26 10:27:50 +0000 | [diff] [blame] | 18 | from collections import deque |
| 19 | from threading import Thread, Event |
| 20 | from inspect import getdoc, isclass |
| 21 | from traceback import format_exception |
| 22 | from logging import FileHandler, DEBUG, Formatter |
Andrew Yourtchenko | 06f3281 | 2021-01-14 10:19:08 +0000 | [diff] [blame] | 23 | from enum import Enum |
Klement Sekera | 558ceab | 2021-04-08 19:37:41 +0200 | [diff] [blame] | 24 | from abc import ABC, abstractmethod |
Ray Kinsella | b8165b9 | 2021-09-22 11:24:06 +0100 | [diff] [blame] | 25 | from struct import pack, unpack |
Paul Vinciguerra | a7427ec | 2019-03-10 10:04:23 -0700 | [diff] [blame] | 26 | |
| 27 | import scapy.compat |
Klement Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 28 | from scapy.packet import Raw, Packet |
Ivan Ivanets | b2b87e4 | 2024-09-27 17:11:18 +0300 | [diff] [blame] | 29 | from vpp_pg_interface import VppPGInterface, is_ipv6_misc |
Ole Troan | a45dc07 | 2018-12-21 16:04:22 +0100 | [diff] [blame] | 30 | from vpp_sub_interface import VppSubInterface |
Ole Trøan | 162989e | 2018-11-26 10:27:50 +0000 | [diff] [blame] | 31 | from vpp_lo_interface import VppLoInterface |
Neale Ranns | 192b13f | 2019-03-15 02:16:20 -0700 | [diff] [blame] | 32 | from vpp_bvi_interface import VppBviInterface |
Ole Trøan | 162989e | 2018-11-26 10:27:50 +0000 | [diff] [blame] | 33 | from vpp_papi_provider import VppPapiProvider |
Neale Ranns | 6197cb7 | 2021-06-03 14:43:21 +0000 | [diff] [blame] | 34 | from vpp_papi import VppEnum |
Paul Vinciguerra | 1043fd3 | 2019-12-02 21:42:28 -0500 | [diff] [blame] | 35 | import vpp_papi |
Ole Trøan | 162989e | 2018-11-26 10:27:50 +0000 | [diff] [blame] | 36 | from vpp_object import VppObjectRegistry |
| 37 | from util import ppp, is_core_present |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 38 | from scapy.layers.inet import IPerror, TCPerror, UDPerror, ICMPerror |
| 39 | from scapy.layers.inet6 import ICMPv6DestUnreach, ICMPv6EchoRequest |
| 40 | from scapy.layers.inet6 import ICMPv6EchoReply |
Naveen Joy | c872cec | 2022-08-30 13:59:03 -0700 | [diff] [blame] | 41 | from vpp_running import use_running |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 42 | from asfframework import VppAsfTestCase |
Paul Vinciguerra | 6919b0d | 2018-12-09 15:37:04 -0800 | [diff] [blame] | 43 | |
Klement Sekera | 558ceab | 2021-04-08 19:37:41 +0200 | [diff] [blame] | 44 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 45 | """ |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 46 | Packet Generator / Scapy Test framework module. |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 47 | |
| 48 | The module provides a set of tools for constructing and running tests and |
| 49 | representing the results. |
| 50 | """ |
| 51 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 52 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 53 | class _PacketInfo(object): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 54 | """Private class to create packet info object. |
| 55 | |
| 56 | Help process information about the next packet. |
| 57 | Set variables to default values. |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 58 | """ |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 59 | |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 60 | #: Store the index of the packet. |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 61 | index = -1 |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 62 | #: Store the index of the source packet generator interface of the packet. |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 63 | src = -1 |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 64 | #: Store the index of the destination packet generator interface |
| 65 | #: of the packet. |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 66 | dst = -1 |
Pavel Kotucek | 59dda06 | 2017-03-02 15:22:47 +0100 | [diff] [blame] | 67 | #: Store expected ip version |
| 68 | ip = -1 |
| 69 | #: Store expected upper protocol |
| 70 | proto = -1 |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 71 | #: Store the copy of the former packet. |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 72 | data = None |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 73 | |
Dave Wallace | cf9356d | 2024-07-23 01:28:19 -0400 | [diff] [blame] | 74 | def __repr__(self): |
| 75 | return f"_PacketInfo index:{self.index} src:{self.src} dst:{self.dst} ip:{self.ip} proto:{self.proto} data:{self.data}" |
| 76 | |
Matej Klotton | 16a14cd | 2016-12-07 15:09:13 +0100 | [diff] [blame] | 77 | def __eq__(self, other): |
| 78 | index = self.index == other.index |
| 79 | src = self.src == other.src |
| 80 | dst = self.dst == other.dst |
| 81 | data = self.data == other.data |
| 82 | return index and src and dst and data |
| 83 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 84 | |
Naveen Joy | c872cec | 2022-08-30 13:59:03 -0700 | [diff] [blame] | 85 | @use_running |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 86 | class VppTestCase(VppAsfTestCase): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 87 | """This subclass is a base class for VPP test cases that are implemented as |
| 88 | classes. It provides methods to create and run test case. |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 89 | """ |
| 90 | |
| 91 | @property |
| 92 | def packet_infos(self): |
| 93 | """List of packet infos""" |
| 94 | return self._packet_infos |
| 95 | |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 96 | @classmethod |
| 97 | def get_packet_count_for_if_idx(cls, dst_if_index): |
| 98 | """Get the number of packet info for specified destination if index""" |
| 99 | if dst_if_index in cls._packet_count_for_dst_if_idx: |
| 100 | return cls._packet_count_for_dst_if_idx[dst_if_index] |
| 101 | else: |
| 102 | return 0 |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 103 | |
| 104 | @classmethod |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 105 | def setUpClass(cls): |
Paul Vinciguerra | 8d991d9 | 2019-01-25 14:05:48 -0800 | [diff] [blame] | 106 | super(VppTestCase, cls).setUpClass() |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 107 | cls.reset_packet_infos() |
Klement Sekera | 3ff6ffc | 2021-04-01 18:19:29 +0200 | [diff] [blame] | 108 | cls._pcaps = [] |
| 109 | cls._old_pcaps = [] |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 110 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 111 | @classmethod |
| 112 | def tearDownClass(cls): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 113 | cls.logger.debug("--- tearDownClass() for %s called ---" % cls.__name__) |
Klement Sekera | ebbaf55 | 2018-02-17 13:41:33 +0100 | [diff] [blame] | 114 | cls.reset_packet_infos() |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 115 | super(VppTestCase, cls).tearDownClass() |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 116 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 117 | @classmethod |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 118 | def pg_enable_capture(cls, interfaces=None): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 119 | """ |
| 120 | Enable capture on packet-generator interfaces |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 121 | |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 122 | :param interfaces: iterable interface indexes (if None, |
| 123 | use self.pg_interfaces) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 124 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 125 | """ |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 126 | if interfaces is None: |
| 127 | interfaces = cls.pg_interfaces |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 128 | for i in interfaces: |
| 129 | i.enable_capture() |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 130 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 131 | @classmethod |
Klement Sekera | 3ff6ffc | 2021-04-01 18:19:29 +0200 | [diff] [blame] | 132 | def register_pcap(cls, intf, worker): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 133 | """Register a pcap in the testclass""" |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 134 | # add to the list of captures with current timestamp |
Klement Sekera | 3ff6ffc | 2021-04-01 18:19:29 +0200 | [diff] [blame] | 135 | cls._pcaps.append((intf, worker)) |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 136 | |
| 137 | @classmethod |
adrianvillin | c6fe617 | 2023-12-06 19:21:49 +0100 | [diff] [blame] | 138 | def pg_start(cls, trace=True, traceFilter=False): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 139 | """Enable the PG, wait till it is done, then clean up""" |
Dave Wallace | 7b8b465 | 2023-08-15 19:05:26 -0400 | [diff] [blame] | 140 | for intf, worker in cls._old_pcaps: |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 141 | intf.remove_old_pcap_file(intf.get_in_path(worker)) |
Klement Sekera | 3ff6ffc | 2021-04-01 18:19:29 +0200 | [diff] [blame] | 142 | cls._old_pcaps = [] |
Benoît Ganne | 8c45e51 | 2021-02-19 16:39:13 +0100 | [diff] [blame] | 143 | if trace: |
| 144 | cls.vapi.cli("clear trace") |
adrianvillin | c6fe617 | 2023-12-06 19:21:49 +0100 | [diff] [blame] | 145 | cls.vapi.cli("trace add pg-input 1000" + (" filter" if traceFilter else "")) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 146 | cls.vapi.cli("packet-generator enable") |
Andrew Yourtchenko | 8d829f6 | 2019-10-13 10:09:50 +0000 | [diff] [blame] | 147 | # PG, when starts, runs to completion - |
| 148 | # so let's avoid a race condition, |
| 149 | # and wait a little till it's done. |
| 150 | # Then clean it up - and then be gone. |
| 151 | deadline = time.time() + 300 |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 152 | while cls.vapi.cli("show packet-generator").find("Yes") != -1: |
Andrew Yourtchenko | 8d829f6 | 2019-10-13 10:09:50 +0000 | [diff] [blame] | 153 | cls.sleep(0.01) # yield |
| 154 | if time.time() > deadline: |
| 155 | cls.logger.error("Timeout waiting for pg to stop") |
| 156 | break |
Klement Sekera | 3ff6ffc | 2021-04-01 18:19:29 +0200 | [diff] [blame] | 157 | for intf, worker in cls._pcaps: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 158 | cls.vapi.cli("packet-generator delete %s" % intf.get_cap_name(worker)) |
Klement Sekera | 3ff6ffc | 2021-04-01 18:19:29 +0200 | [diff] [blame] | 159 | cls._old_pcaps = cls._pcaps |
| 160 | cls._pcaps = [] |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 161 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 162 | @classmethod |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 163 | def create_pg_interfaces_internal(cls, interfaces, gso=0, gso_size=0, mode=None): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 164 | """ |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 165 | Create packet-generator interfaces. |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 166 | |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 167 | :param interfaces: iterable indexes of the interfaces. |
| 168 | :returns: List of created interfaces. |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 169 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 170 | """ |
| 171 | result = [] |
| 172 | for i in interfaces: |
Neale Ranns | 6197cb7 | 2021-06-03 14:43:21 +0000 | [diff] [blame] | 173 | intf = VppPGInterface(cls, i, gso, gso_size, mode) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 174 | setattr(cls, intf.name, intf) |
| 175 | result.append(intf) |
| 176 | cls.pg_interfaces = result |
| 177 | return result |
| 178 | |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 179 | @classmethod |
Neale Ranns | 6197cb7 | 2021-06-03 14:43:21 +0000 | [diff] [blame] | 180 | def create_pg_ip4_interfaces(cls, interfaces, gso=0, gso_size=0): |
Dave Wallace | 670724c | 2022-09-20 21:52:18 -0400 | [diff] [blame] | 181 | if not hasattr(cls, "vpp"): |
| 182 | cls.pg_interfaces = [] |
| 183 | return cls.pg_interfaces |
Neale Ranns | 6197cb7 | 2021-06-03 14:43:21 +0000 | [diff] [blame] | 184 | pgmode = VppEnum.vl_api_pg_interface_mode_t |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 185 | return cls.create_pg_interfaces_internal( |
| 186 | interfaces, gso, gso_size, pgmode.PG_API_MODE_IP4 |
| 187 | ) |
Neale Ranns | 6197cb7 | 2021-06-03 14:43:21 +0000 | [diff] [blame] | 188 | |
| 189 | @classmethod |
| 190 | def create_pg_ip6_interfaces(cls, interfaces, gso=0, gso_size=0): |
Dave Wallace | 670724c | 2022-09-20 21:52:18 -0400 | [diff] [blame] | 191 | if not hasattr(cls, "vpp"): |
| 192 | cls.pg_interfaces = [] |
| 193 | return cls.pg_interfaces |
Neale Ranns | 6197cb7 | 2021-06-03 14:43:21 +0000 | [diff] [blame] | 194 | pgmode = VppEnum.vl_api_pg_interface_mode_t |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 195 | return cls.create_pg_interfaces_internal( |
| 196 | interfaces, gso, gso_size, pgmode.PG_API_MODE_IP6 |
| 197 | ) |
Neale Ranns | 6197cb7 | 2021-06-03 14:43:21 +0000 | [diff] [blame] | 198 | |
| 199 | @classmethod |
| 200 | def create_pg_interfaces(cls, interfaces, gso=0, gso_size=0): |
Dave Wallace | 670724c | 2022-09-20 21:52:18 -0400 | [diff] [blame] | 201 | if not hasattr(cls, "vpp"): |
| 202 | cls.pg_interfaces = [] |
| 203 | return cls.pg_interfaces |
Neale Ranns | 6197cb7 | 2021-06-03 14:43:21 +0000 | [diff] [blame] | 204 | pgmode = VppEnum.vl_api_pg_interface_mode_t |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 205 | return cls.create_pg_interfaces_internal( |
| 206 | interfaces, gso, gso_size, pgmode.PG_API_MODE_ETHERNET |
| 207 | ) |
Neale Ranns | 6197cb7 | 2021-06-03 14:43:21 +0000 | [diff] [blame] | 208 | |
| 209 | @classmethod |
| 210 | def create_pg_ethernet_interfaces(cls, interfaces, gso=0, gso_size=0): |
Dave Wallace | 670724c | 2022-09-20 21:52:18 -0400 | [diff] [blame] | 211 | if not hasattr(cls, "vpp"): |
| 212 | cls.pg_interfaces = [] |
| 213 | return cls.pg_interfaces |
Neale Ranns | 6197cb7 | 2021-06-03 14:43:21 +0000 | [diff] [blame] | 214 | pgmode = VppEnum.vl_api_pg_interface_mode_t |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 215 | return cls.create_pg_interfaces_internal( |
| 216 | interfaces, gso, gso_size, pgmode.PG_API_MODE_ETHERNET |
| 217 | ) |
Neale Ranns | 6197cb7 | 2021-06-03 14:43:21 +0000 | [diff] [blame] | 218 | |
| 219 | @classmethod |
Klement Sekera | b9ef273 | 2018-06-24 22:49:33 +0200 | [diff] [blame] | 220 | def create_loopback_interfaces(cls, count): |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 221 | """ |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 222 | Create loopback interfaces. |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 223 | |
Klement Sekera | b9ef273 | 2018-06-24 22:49:33 +0200 | [diff] [blame] | 224 | :param count: number of interfaces created. |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 225 | :returns: List of created interfaces. |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 226 | """ |
Dave Wallace | 670724c | 2022-09-20 21:52:18 -0400 | [diff] [blame] | 227 | if not hasattr(cls, "vpp"): |
| 228 | cls.lo_interfaces = [] |
| 229 | return cls.lo_interfaces |
Klement Sekera | b9ef273 | 2018-06-24 22:49:33 +0200 | [diff] [blame] | 230 | result = [VppLoInterface(cls) for i in range(count)] |
| 231 | for intf in result: |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 232 | setattr(cls, intf.name, intf) |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 233 | cls.lo_interfaces = result |
| 234 | return result |
| 235 | |
Neale Ranns | 192b13f | 2019-03-15 02:16:20 -0700 | [diff] [blame] | 236 | @classmethod |
| 237 | def create_bvi_interfaces(cls, count): |
| 238 | """ |
| 239 | Create BVI interfaces. |
| 240 | |
| 241 | :param count: number of interfaces created. |
| 242 | :returns: List of created interfaces. |
| 243 | """ |
Dave Wallace | 670724c | 2022-09-20 21:52:18 -0400 | [diff] [blame] | 244 | if not hasattr(cls, "vpp"): |
| 245 | cls.bvi_interfaces = [] |
| 246 | return cls.bvi_interfaces |
Neale Ranns | 192b13f | 2019-03-15 02:16:20 -0700 | [diff] [blame] | 247 | result = [VppBviInterface(cls) for i in range(count)] |
| 248 | for intf in result: |
| 249 | setattr(cls, intf.name, intf) |
| 250 | cls.bvi_interfaces = result |
| 251 | return result |
| 252 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 253 | @staticmethod |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 254 | def extend_packet(packet, size, padding=" "): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 255 | """ |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 256 | Extend packet to given size by padding with spaces or custom padding |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 257 | NOTE: Currently works only when Raw layer is present. |
| 258 | |
| 259 | :param packet: packet |
| 260 | :param size: target size |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 261 | :param padding: padding used to extend the payload |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 262 | |
| 263 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 264 | packet_len = len(packet) + 4 |
| 265 | extend = size - packet_len |
| 266 | if extend > 0: |
Alexandre Poirrier | cc99149 | 2019-05-07 10:39:57 +0200 | [diff] [blame] | 267 | num = (extend // len(padding)) + 1 |
| 268 | packet[Raw].load += (padding * num)[:extend].encode("ascii") |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 269 | |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 270 | @classmethod |
| 271 | def reset_packet_infos(cls): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 272 | """Reset the list of packet info objects and packet counts to zero""" |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 273 | cls._packet_infos = {} |
| 274 | cls._packet_count_for_dst_if_idx = {} |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 275 | |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 276 | @classmethod |
| 277 | def create_packet_info(cls, src_if, dst_if): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 278 | """ |
| 279 | Create packet info object containing the source and destination indexes |
| 280 | and add it to the testcase's packet info list |
| 281 | |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 282 | :param VppInterface src_if: source interface |
| 283 | :param VppInterface dst_if: destination interface |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 284 | |
| 285 | :returns: _PacketInfo object |
| 286 | |
| 287 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 288 | info = _PacketInfo() |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 289 | info.index = len(cls._packet_infos) |
| 290 | info.src = src_if.sw_if_index |
| 291 | info.dst = dst_if.sw_if_index |
| 292 | if isinstance(dst_if, VppSubInterface): |
| 293 | dst_idx = dst_if.parent.sw_if_index |
| 294 | else: |
| 295 | dst_idx = dst_if.sw_if_index |
| 296 | if dst_idx in cls._packet_count_for_dst_if_idx: |
| 297 | cls._packet_count_for_dst_if_idx[dst_idx] += 1 |
| 298 | else: |
| 299 | cls._packet_count_for_dst_if_idx[dst_idx] = 1 |
| 300 | cls._packet_infos[info.index] = info |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 301 | return info |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 302 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 303 | @staticmethod |
| 304 | def info_to_payload(info): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 305 | """ |
| 306 | Convert _PacketInfo object to packet payload |
| 307 | |
| 308 | :param info: _PacketInfo object |
| 309 | |
| 310 | :returns: string containing serialized data from packet info |
| 311 | """ |
Ray Kinsella | b8165b9 | 2021-09-22 11:24:06 +0100 | [diff] [blame] | 312 | |
| 313 | # retrieve payload, currently 18 bytes (4 x ints + 1 short) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 314 | return pack("iiiih", info.index, info.src, info.dst, info.ip, info.proto) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 315 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 316 | @staticmethod |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 317 | def payload_to_info(payload, payload_field="load"): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 318 | """ |
| 319 | Convert packet payload to _PacketInfo object |
| 320 | |
| 321 | :param payload: packet payload |
Paul Vinciguerra | 22364e4 | 2019-03-12 20:04:56 -0700 | [diff] [blame] | 322 | :type payload: <class 'scapy.packet.Raw'> |
| 323 | :param payload_field: packet fieldname of payload "load" for |
Paul Vinciguerra | eaea421 | 2019-03-06 11:58:06 -0800 | [diff] [blame] | 324 | <class 'scapy.packet.Raw'> |
Paul Vinciguerra | 22364e4 | 2019-03-12 20:04:56 -0700 | [diff] [blame] | 325 | :type payload_field: str |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 326 | :returns: _PacketInfo object containing de-serialized data from payload |
| 327 | |
| 328 | """ |
Ray Kinsella | b8165b9 | 2021-09-22 11:24:06 +0100 | [diff] [blame] | 329 | |
| 330 | # retrieve payload, currently 18 bytes (4 x ints + 1 short) |
| 331 | payload_b = getattr(payload, payload_field)[:18] |
| 332 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 333 | info = _PacketInfo() |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 334 | info.index, info.src, info.dst, info.ip, info.proto = unpack("iiiih", payload_b) |
Ray Kinsella | b8165b9 | 2021-09-22 11:24:06 +0100 | [diff] [blame] | 335 | |
| 336 | # some SRv6 TCs depend on get an exception if bad values are detected |
| 337 | if info.index > 0x4000: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 338 | raise ValueError("Index value is invalid") |
Ray Kinsella | b8165b9 | 2021-09-22 11:24:06 +0100 | [diff] [blame] | 339 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 340 | return info |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 341 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 342 | def get_next_packet_info(self, info): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 343 | """ |
| 344 | Iterate over the packet info list stored in the testcase |
| 345 | Start iteration with first element if info is None |
| 346 | Continue based on index in info if info is specified |
| 347 | |
| 348 | :param info: info or None |
| 349 | :returns: next info in list or None if no more infos |
| 350 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 351 | if info is None: |
| 352 | next_index = 0 |
| 353 | else: |
| 354 | next_index = info.index + 1 |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 355 | if next_index == len(self._packet_infos): |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 356 | return None |
| 357 | else: |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 358 | return self._packet_infos[next_index] |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 359 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 360 | def get_next_packet_info_for_interface(self, src_index, info): |
| 361 | """ |
| 362 | Search the packet info list for the next packet info with same source |
| 363 | interface index |
| 364 | |
| 365 | :param src_index: source interface index to search for |
| 366 | :param info: packet info - where to start the search |
| 367 | :returns: packet info or None |
| 368 | |
| 369 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 370 | while True: |
| 371 | info = self.get_next_packet_info(info) |
| 372 | if info is None: |
| 373 | return None |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 374 | if info.src == src_index: |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 375 | return info |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 376 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 377 | def get_next_packet_info_for_interface2(self, src_index, dst_index, info): |
| 378 | """ |
| 379 | Search the packet info list for the next packet info with same source |
| 380 | and destination interface indexes |
| 381 | |
| 382 | :param src_index: source interface index to search for |
| 383 | :param dst_index: destination interface index to search for |
| 384 | :param info: packet info - where to start the search |
| 385 | :returns: packet info or None |
| 386 | |
| 387 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 388 | while True: |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 389 | info = self.get_next_packet_info_for_interface(src_index, info) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 390 | if info is None: |
| 391 | return None |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 392 | if info.dst == dst_index: |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 393 | return info |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 394 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 395 | def assert_packet_checksums_valid(self, packet, ignore_zero_udp_checksums=True): |
Paul Vinciguerra | a7427ec | 2019-03-10 10:04:23 -0700 | [diff] [blame] | 396 | received = packet.__class__(scapy.compat.raw(packet)) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 397 | udp_layers = ["UDP", "UDPerror"] |
| 398 | checksum_fields = ["cksum", "chksum"] |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 399 | checksums = [] |
| 400 | counter = 0 |
Paul Vinciguerra | a7427ec | 2019-03-10 10:04:23 -0700 | [diff] [blame] | 401 | temp = received.__class__(scapy.compat.raw(received)) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 402 | while True: |
| 403 | layer = temp.getlayer(counter) |
| 404 | if layer: |
Klement Sekera | 66cea09 | 2019-12-05 13:13:21 +0000 | [diff] [blame] | 405 | layer = layer.copy() |
| 406 | layer.remove_payload() |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 407 | for cf in checksum_fields: |
| 408 | if hasattr(layer, cf): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 409 | if ( |
| 410 | ignore_zero_udp_checksums |
| 411 | and 0 == getattr(layer, cf) |
| 412 | and layer.name in udp_layers |
| 413 | ): |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 414 | continue |
Klement Sekera | 66cea09 | 2019-12-05 13:13:21 +0000 | [diff] [blame] | 415 | delattr(temp.getlayer(counter), cf) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 416 | checksums.append((counter, cf)) |
| 417 | else: |
| 418 | break |
| 419 | counter = counter + 1 |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 420 | if 0 == len(checksums): |
| 421 | return |
Paul Vinciguerra | a7427ec | 2019-03-10 10:04:23 -0700 | [diff] [blame] | 422 | temp = temp.__class__(scapy.compat.raw(temp)) |
Klement Sekera | 738cf73 | 2022-11-14 11:26:18 +0100 | [diff] [blame] | 423 | for layer, cf in reversed(checksums): |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 424 | calc_sum = getattr(temp[layer], cf) |
| 425 | self.assert_equal( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 426 | getattr(received[layer], cf), |
| 427 | calc_sum, |
| 428 | "packet checksum on layer #%d: %s" % (layer, temp[layer].name), |
| 429 | ) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 430 | self.logger.debug( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 431 | "Checksum field `%s` on `%s` layer has correct value `%s`" |
| 432 | % (cf, temp[layer].name, calc_sum) |
| 433 | ) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 434 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 435 | def assert_checksum_valid( |
Klement Sekera | 738cf73 | 2022-11-14 11:26:18 +0100 | [diff] [blame] | 436 | self, |
| 437 | received_packet, |
| 438 | layer, |
| 439 | checksum_field_names=["chksum", "cksum"], |
| 440 | ignore_zero_checksum=False, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 441 | ): |
| 442 | """Check checksum of received packet on given layer""" |
Klement Sekera | 738cf73 | 2022-11-14 11:26:18 +0100 | [diff] [blame] | 443 | layer_copy = received_packet[layer].copy() |
| 444 | layer_copy.remove_payload() |
| 445 | field_name = None |
| 446 | for f in checksum_field_names: |
| 447 | if hasattr(layer_copy, f): |
| 448 | field_name = f |
| 449 | break |
| 450 | if field_name is None: |
| 451 | raise Exception( |
| 452 | f"Layer `{layer}` has none of checksum fields: `{checksum_field_names}`." |
| 453 | ) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 454 | received_packet_checksum = getattr(received_packet[layer], field_name) |
| 455 | if ignore_zero_checksum and 0 == received_packet_checksum: |
| 456 | return |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 457 | recalculated = received_packet.__class__(scapy.compat.raw(received_packet)) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 458 | delattr(recalculated[layer], field_name) |
Paul Vinciguerra | a7427ec | 2019-03-10 10:04:23 -0700 | [diff] [blame] | 459 | recalculated = recalculated.__class__(scapy.compat.raw(recalculated)) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 460 | self.assert_equal( |
| 461 | received_packet_checksum, |
| 462 | getattr(recalculated[layer], field_name), |
Klement Sekera | 738cf73 | 2022-11-14 11:26:18 +0100 | [diff] [blame] | 463 | f"packet checksum (field: {field_name}) on layer: %s" % layer, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 464 | ) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 465 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 466 | def assert_ip_checksum_valid(self, received_packet, ignore_zero_checksum=False): |
| 467 | self.assert_checksum_valid( |
| 468 | received_packet, "IP", ignore_zero_checksum=ignore_zero_checksum |
| 469 | ) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 470 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 471 | def assert_tcp_checksum_valid(self, received_packet, ignore_zero_checksum=False): |
| 472 | self.assert_checksum_valid( |
| 473 | received_packet, "TCP", ignore_zero_checksum=ignore_zero_checksum |
| 474 | ) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 475 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 476 | def assert_udp_checksum_valid(self, received_packet, ignore_zero_checksum=True): |
| 477 | self.assert_checksum_valid( |
| 478 | received_packet, "UDP", ignore_zero_checksum=ignore_zero_checksum |
| 479 | ) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 480 | |
| 481 | def assert_embedded_icmp_checksum_valid(self, received_packet): |
| 482 | if received_packet.haslayer(IPerror): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 483 | self.assert_checksum_valid(received_packet, "IPerror") |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 484 | if received_packet.haslayer(TCPerror): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 485 | self.assert_checksum_valid(received_packet, "TCPerror") |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 486 | if received_packet.haslayer(UDPerror): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 487 | self.assert_checksum_valid( |
| 488 | received_packet, "UDPerror", ignore_zero_checksum=True |
| 489 | ) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 490 | if received_packet.haslayer(ICMPerror): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 491 | self.assert_checksum_valid(received_packet, "ICMPerror") |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 492 | |
| 493 | def assert_icmp_checksum_valid(self, received_packet): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 494 | self.assert_checksum_valid(received_packet, "ICMP") |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 495 | self.assert_embedded_icmp_checksum_valid(received_packet) |
| 496 | |
| 497 | def assert_icmpv6_checksum_valid(self, pkt): |
| 498 | if pkt.haslayer(ICMPv6DestUnreach): |
Klement Sekera | 738cf73 | 2022-11-14 11:26:18 +0100 | [diff] [blame] | 499 | self.assert_checksum_valid(pkt, "ICMPv6DestUnreach") |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 500 | self.assert_embedded_icmp_checksum_valid(pkt) |
| 501 | if pkt.haslayer(ICMPv6EchoRequest): |
Klement Sekera | 738cf73 | 2022-11-14 11:26:18 +0100 | [diff] [blame] | 502 | self.assert_checksum_valid(pkt, "ICMPv6EchoRequest") |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 503 | if pkt.haslayer(ICMPv6EchoReply): |
Klement Sekera | 738cf73 | 2022-11-14 11:26:18 +0100 | [diff] [blame] | 504 | self.assert_checksum_valid(pkt, "ICMPv6EchoReply") |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 505 | |
Klement Sekera | f37c3ba | 2018-11-08 11:24:34 +0100 | [diff] [blame] | 506 | def assert_packet_counter_equal(self, counter, expected_value): |
Klement Sekera | 107ad73 | 2022-02-18 10:32:08 +0000 | [diff] [blame] | 507 | counter_value = self.get_counter(counter) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 508 | self.assert_equal( |
| 509 | counter_value, expected_value, "packet counter `%s'" % counter |
| 510 | ) |
Klement Sekera | f37c3ba | 2018-11-08 11:24:34 +0100 | [diff] [blame] | 511 | |
Benoît Ganne | 8c45e51 | 2021-02-19 16:39:13 +0100 | [diff] [blame] | 512 | def pg_send(self, intf, pkts, worker=None, trace=True): |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 513 | intf.add_stream(pkts, worker=worker) |
Neale Ranns | 52fae86 | 2018-01-08 04:41:42 -0800 | [diff] [blame] | 514 | self.pg_enable_capture(self.pg_interfaces) |
Benoît Ganne | 8c45e51 | 2021-02-19 16:39:13 +0100 | [diff] [blame] | 515 | self.pg_start(trace=trace) |
Paul Vinciguerra | eb41443 | 2019-02-20 09:01:14 -0800 | [diff] [blame] | 516 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 517 | def send_and_assert_no_replies( |
| 518 | self, intf, pkts, remark="", timeout=None, stats_diff=None, trace=True, msg=None |
| 519 | ): |
Klement Sekera | ad3187f | 2022-02-18 10:34:35 +0000 | [diff] [blame] | 520 | if stats_diff: |
| 521 | stats_snapshot = self.snapshot_stats(stats_diff) |
| 522 | |
Paul Vinciguerra | eb41443 | 2019-02-20 09:01:14 -0800 | [diff] [blame] | 523 | self.pg_send(intf, pkts) |
Klement Sekera | ad3187f | 2022-02-18 10:34:35 +0000 | [diff] [blame] | 524 | |
| 525 | try: |
| 526 | if not timeout: |
| 527 | timeout = 1 |
| 528 | for i in self.pg_interfaces: |
Klement Sekera | 26cd024 | 2022-02-18 10:35:08 +0000 | [diff] [blame] | 529 | i.assert_nothing_captured(timeout=timeout, remark=remark) |
Klement Sekera | ad3187f | 2022-02-18 10:34:35 +0000 | [diff] [blame] | 530 | timeout = 0.1 |
| 531 | finally: |
| 532 | if trace: |
| 533 | if msg: |
| 534 | self.logger.debug(f"send_and_assert_no_replies: {msg}") |
| 535 | self.logger.debug(self.vapi.cli("show trace")) |
| 536 | |
| 537 | if stats_diff: |
| 538 | self.compare_stats_with_snapshot(stats_diff, stats_snapshot) |
Neale Ranns | 52fae86 | 2018-01-08 04:41:42 -0800 | [diff] [blame] | 539 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 540 | def send_and_expect( |
| 541 | self, |
| 542 | intf, |
| 543 | pkts, |
| 544 | output, |
| 545 | n_rx=None, |
| 546 | worker=None, |
| 547 | trace=True, |
| 548 | msg=None, |
| 549 | stats_diff=None, |
Ivan Ivanets | b2b87e4 | 2024-09-27 17:11:18 +0300 | [diff] [blame] | 550 | filter_out_fn=is_ipv6_misc, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 551 | ): |
Klement Sekera | ad3187f | 2022-02-18 10:34:35 +0000 | [diff] [blame] | 552 | if stats_diff: |
| 553 | stats_snapshot = self.snapshot_stats(stats_diff) |
| 554 | |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 555 | if not n_rx: |
Klement Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 556 | n_rx = 1 if isinstance(pkts, Packet) else len(pkts) |
Benoît Ganne | 8c45e51 | 2021-02-19 16:39:13 +0100 | [diff] [blame] | 557 | self.pg_send(intf, pkts, worker=worker, trace=trace) |
Ivan Ivanets | b2b87e4 | 2024-09-27 17:11:18 +0300 | [diff] [blame] | 558 | rx = output.get_capture(n_rx, filter_out_fn=filter_out_fn) |
Klement Sekera | ff334db | 2021-05-26 13:02:35 +0200 | [diff] [blame] | 559 | if trace: |
Klement Sekera | ad3187f | 2022-02-18 10:34:35 +0000 | [diff] [blame] | 560 | if msg: |
| 561 | self.logger.debug(f"send_and_expect: {msg}") |
Klement Sekera | ff334db | 2021-05-26 13:02:35 +0200 | [diff] [blame] | 562 | self.logger.debug(self.vapi.cli("show trace")) |
Klement Sekera | ad3187f | 2022-02-18 10:34:35 +0000 | [diff] [blame] | 563 | |
| 564 | if stats_diff: |
| 565 | self.compare_stats_with_snapshot(stats_diff, stats_snapshot) |
| 566 | |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 567 | return rx |
| 568 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 569 | def send_and_expect_load_balancing( |
| 570 | self, input, pkts, outputs, worker=None, trace=True |
| 571 | ): |
Neale Ranns | 699bea2 | 2022-02-17 09:22:16 +0000 | [diff] [blame] | 572 | self.pg_send(input, pkts, worker=worker, trace=trace) |
| 573 | rxs = [] |
| 574 | for oo in outputs: |
| 575 | rx = oo._get_capture(1) |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 576 | self.assertNotEqual(0, len(rx), f"0 != len(rx) ({len(rx)})") |
Neale Ranns | 699bea2 | 2022-02-17 09:22:16 +0000 | [diff] [blame] | 577 | rxs.append(rx) |
| 578 | if trace: |
| 579 | self.logger.debug(self.vapi.cli("show trace")) |
| 580 | return rxs |
| 581 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 582 | def send_and_expect_some(self, intf, pkts, output, worker=None, trace=True): |
Neale Ranns | 5c6dd17 | 2022-02-17 09:08:47 +0000 | [diff] [blame] | 583 | self.pg_send(intf, pkts, worker=worker, trace=trace) |
| 584 | rx = output._get_capture(1) |
| 585 | if trace: |
| 586 | self.logger.debug(self.vapi.cli("show trace")) |
| 587 | self.assertTrue(len(rx) > 0) |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 588 | self.assertTrue( |
| 589 | len(rx) <= len(pkts), f"len(rx) ({len(rx)}) > len(pkts) ({len(pkts)})" |
| 590 | ) |
Neale Ranns | 5c6dd17 | 2022-02-17 09:08:47 +0000 | [diff] [blame] | 591 | return rx |
| 592 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 593 | def send_and_expect_only(self, intf, pkts, output, timeout=None, stats_diff=None): |
Klement Sekera | ad3187f | 2022-02-18 10:34:35 +0000 | [diff] [blame] | 594 | if stats_diff: |
| 595 | stats_snapshot = self.snapshot_stats(stats_diff) |
| 596 | |
Paul Vinciguerra | eb41443 | 2019-02-20 09:01:14 -0800 | [diff] [blame] | 597 | self.pg_send(intf, pkts) |
Paul Vinciguerra | 8aeb220 | 2019-01-07 16:29:26 -0800 | [diff] [blame] | 598 | rx = output.get_capture(len(pkts)) |
| 599 | outputs = [output] |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 600 | if not timeout: |
| 601 | timeout = 1 |
| 602 | for i in self.pg_interfaces: |
| 603 | if i not in outputs: |
Klement Sekera | 26cd024 | 2022-02-18 10:35:08 +0000 | [diff] [blame] | 604 | i.assert_nothing_captured(timeout=timeout) |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 605 | timeout = 0.1 |
| 606 | |
Klement Sekera | ad3187f | 2022-02-18 10:34:35 +0000 | [diff] [blame] | 607 | if stats_diff: |
| 608 | self.compare_stats_with_snapshot(stats_diff, stats_snapshot) |
| 609 | |
Neale Ranns | 52fae86 | 2018-01-08 04:41:42 -0800 | [diff] [blame] | 610 | return rx |
| 611 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 612 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 613 | if __name__ == "__main__": |
Paul Vinciguerra | dd3c5d2 | 2019-01-13 16:09:10 -0800 | [diff] [blame] | 614 | pass |