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