| 1 |
# SPDX-License-Identifier: (GPL-2.0 OR Linux-OpenIB) |
| 2 |
# Copyright (c) 2020 Nvidia All rights reserved. See COPYING file |
| 3 |
""" |
| 4 |
Test module for pyverbs' mlx5 flow module. |
| 5 |
""" |
| 6 |
|
| 7 |
import unittest |
| 8 |
import errno |
| 9 |
|
| 10 |
from pyverbs.providers.mlx5.mlx5dv_flow import Mlx5FlowMatcher, \ |
| 11 |
Mlx5FlowMatcherAttr, Mlx5FlowMatchParameters, Mlx5FlowActionAttr, Mlx5Flow,\ |
| 12 |
Mlx5PacketReformatFlowAction |
| 13 |
from pyverbs.providers.mlx5.mlx5dv import Mlx5Context, Mlx5DVContextAttr, Mlx5DevxObj |
| 14 |
from pyverbs.pyverbs_error import PyverbsRDMAError, PyverbsUserError |
| 15 |
from pyverbs.providers.mlx5.mlx5_enums import mlx5dv_flow_matcher_attr_mask, mlx5dv_flow_matcher_attr_mask, mlx5dv_flow_action_type, \ |
| 16 |
MLX5DV_FLOW_TABLE_TYPE_NIC_RX_, MLX5DV_FLOW_TABLE_TYPE_RDMA_TRANSPORT_RX_, MLX5DV_FLOW_TABLE_TYPE_RDMA_TRANSPORT_TX_, \ |
| 17 |
MLX5DV_FLOW_TABLE_TYPE_RDMA_RX_, MLX5DV_FLOW_TABLE_TYPE_RDMA_TX_, MLX5DV_FLOW_TABLE_TYPE_NIC_TX_, \ |
| 18 |
MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TO_L2_TUNNEL_ |
| 19 |
from pyverbs.libibverbs_enums import ibv_flow_flags |
| 20 |
from tests.mlx5_base import Mlx5RDMATestCase, create_privileged_context, Mlx5RcResources |
| 21 |
from tests.utils import PacketConsts, is_eth, requires_no_sriov, requires_eth,\ |
| 22 |
requires_cap_net_raw, requires_root |
| 23 |
from tests.base import RawResources |
| 24 |
import tests.utils as u |
| 25 |
import struct |
| 26 |
|
| 27 |
|
| 28 |
MAX_MATCH_PARAM_SIZE = 0x180 |
| 29 |
NIC_RX_RDMA_TABLE_TYPE = 0X7 |
| 30 |
NIC_TX_RDMA_TABLE_TYPE = 0X8 |
| 31 |
RDMA_TRANSPORT_RX = 0xd |
| 32 |
RDMA_TRANSPORT_TX = 0xe |
| 33 |
DROP_ACTION = 0X2 |
| 34 |
ALLOW_ACTION = 0X1 |
| 35 |
TABLE_LEVEL = 200 |
| 36 |
|
| 37 |
|
| 38 |
@u.skip_unsupported |
| 39 |
def requires_reformat_support(func): |
| 40 |
def func_wrapper(instance): |
| 41 |
nic_tbl_caps = u.query_nic_flow_table_caps(instance) |
| 42 |
# Verify that both NIC RX and TX support reformat actions by checking |
| 43 |
# the following PRM fields: encap_general_header, |
| 44 |
# log_max_packet_reformat, and reformat (for both RX and TX). |
| 45 |
if not(nic_tbl_caps.encap_general_header and |
| 46 |
nic_tbl_caps.log_max_packet_reformat_context and |
| 47 |
nic_tbl_caps.flow_table_properties_nic_receive.reformat and |
| 48 |
nic_tbl_caps.flow_table_properties_nic_transmit.reformat): |
| 49 |
raise unittest.SkipTest('NIC flow table does not support reformat') |
| 50 |
return func(instance) |
| 51 |
return func_wrapper |
| 52 |
|
| 53 |
|
| 54 |
def gen_vxlan_l2_tunnel_encap_header(msg_size): |
| 55 |
vxlan_header = u.gen_vxlan_header() |
| 56 |
udp_header = u.gen_udp_header(packet_len=msg_size + len(vxlan_header), |
| 57 |
dst_port=PacketConsts.VXLAN_PORT) |
| 58 |
ip_header = u.gen_ipv4_header(packet_len=msg_size + len(vxlan_header) + len(udp_header)) |
| 59 |
mac_header = u.gen_ethernet_header() |
| 60 |
return mac_header + ip_header + udp_header + vxlan_header |
| 61 |
|
| 62 |
|
| 63 |
def check_rdma_transport_domain_caps(agr_obj): |
| 64 |
""" |
| 65 |
Check if the device and the given resources support rdma transport domain |
| 66 |
creation. If not, it raises unittest.SkipTest. |
| 67 |
This function should be called (directly or indirectly) from test functions |
| 68 |
only. |
| 69 |
:param agr_obj: Aggregation object which contains all resources necessary. |
| 70 |
""" |
| 71 |
from tests.mlx5_prm_structs import QueryAdvRdmaCapOut, \ |
| 72 |
QueryHcaCapIn, QueryCmdHcaCapOut, QueryHcaCapOp, QueryHcaCapMod |
| 73 |
query_cap_in = QueryHcaCapIn(op_mod=0x1) |
| 74 |
query_cap_out = QueryCmdHcaCapOut(agr_obj.ctx.devx_general_cmd( |
| 75 |
query_cap_in, len(QueryCmdHcaCapOut()))) |
| 76 |
|
| 77 |
if query_cap_out.status: |
| 78 |
raise unittest.SkipTest('Failed to query general HCA CAPs with syndrome ' |
| 79 |
f'({query_cap_out.syndrome}') |
| 80 |
|
| 81 |
if not query_cap_out.capability.adv_rdma_cap: |
| 82 |
raise unittest.SkipTest("The device doesn't support adv_rdma_cap") |
| 83 |
|
| 84 |
query_adv_rdma_cap_in = QueryHcaCapIn(op_mod=(QueryHcaCapOp.ADV_RDMA_CAP << 0x1) | \ |
| 85 |
QueryHcaCapMod.CURRENT) |
| 86 |
query_adv_rdma_cap_out = QueryAdvRdmaCapOut(agr_obj.ctx.devx_general_cmd( |
| 87 |
query_adv_rdma_cap_in, len(QueryAdvRdmaCapOut()))) |
| 88 |
|
| 89 |
if not query_adv_rdma_cap_out.capability.rdma_transport_rx_flow_table_properties.ft_support or \ |
| 90 |
not query_adv_rdma_cap_out.capability.rdma_transport_tx_flow_table_properties.ft_support: |
| 91 |
raise unittest.SkipTest("The device doesn't support the RDMA transport domain") |
| 92 |
|
| 93 |
|
| 94 |
class Mlx5FlowResources(RawResources): |
| 95 |
def create_matcher(self, mask, match_criteria_enable, flags=0, |
| 96 |
ft_type=MLX5DV_FLOW_TABLE_TYPE_NIC_RX_): |
| 97 |
""" |
| 98 |
Creates a matcher from a provided mask. |
| 99 |
:param mask: The mask to match on (in bytes) |
| 100 |
:param match_criteria_enable: Bitmask representing which of the |
| 101 |
headers and parameters in match_criteria |
| 102 |
are used |
| 103 |
:param flags: Flow matcher flags |
| 104 |
:param ft_type: Flow table type |
| 105 |
:return: Resulting matcher |
| 106 |
""" |
| 107 |
try: |
| 108 |
flow_match_param = Mlx5FlowMatchParameters(len(mask), mask) |
| 109 |
attr = Mlx5FlowMatcherAttr(match_mask=flow_match_param, |
| 110 |
match_criteria_enable=match_criteria_enable, |
| 111 |
flags=flags, ft_type=ft_type) |
| 112 |
matcher = Mlx5FlowMatcher(self.ctx, attr) |
| 113 |
except PyverbsRDMAError as ex: |
| 114 |
if ex.error_code in [errno.EOPNOTSUPP, errno.EPROTONOSUPPORT]: |
| 115 |
raise unittest.SkipTest('Matcher creation is not supported') |
| 116 |
raise ex |
| 117 |
return matcher |
| 118 |
|
| 119 |
@requires_cap_net_raw() |
| 120 |
@requires_eth() |
| 121 |
def create_qps(self): |
| 122 |
super().create_qps() |
| 123 |
|
| 124 |
|
| 125 |
class Mlx5CounterFlowResources(Mlx5FlowResources): |
| 126 |
|
| 127 |
def create_context(self): |
| 128 |
mlx5dv_attr = Mlx5DVContextAttr() |
| 129 |
try: |
| 130 |
self.ctx = Mlx5Context(mlx5dv_attr, name=self.dev_name) |
| 131 |
except PyverbsUserError as ex: |
| 132 |
raise unittest.SkipTest(f'Could not open mlx5 context ({ex})') |
| 133 |
except PyverbsRDMAError: |
| 134 |
raise unittest.SkipTest('Opening mlx5 context is not supported') |
| 135 |
|
| 136 |
def query_bulk_counter_cap(self): |
| 137 |
""" |
| 138 |
Query the device for the maximum allowed bulk allocation size for flow counters. |
| 139 |
This method queries the HCA capabilities to determine the maximum log2 bulk size |
| 140 |
that can be allocated for flow counters. It first checks for general HCA capabilities, |
| 141 |
then queries HCA_CAP_2 for the specific flow_counter_bulk_log_max_alloc field. |
| 142 |
:return: The maximum log2 bulk allocation size for flow counters. |
| 143 |
""" |
| 144 |
from tests.mlx5_prm_structs import QueryCmdHcaCap2Out, \ |
| 145 |
QueryHcaCapIn, QueryCmdHcaCapOut, QueryHcaCapOp, QueryHcaCapMod |
| 146 |
query_cap_in = QueryHcaCapIn(op_mod=0x1) |
| 147 |
query_cap_out = QueryCmdHcaCapOut(self.ctx.devx_general_cmd( |
| 148 |
query_cap_in, len(QueryCmdHcaCapOut()))) |
| 149 |
if query_cap_out.status: |
| 150 |
raise PyverbsRDMAError('Failed to query general HCA CAPs with syndrome ' |
| 151 |
f'({query_cap_out.syndrome}') |
| 152 |
|
| 153 |
if not query_cap_out.capability.hca_cap_2: |
| 154 |
raise unittest.SkipTest('The device doesn\'t support general HCA CAPs 2') |
| 155 |
query_cap2_in = QueryHcaCapIn(op_mod=(QueryHcaCapOp.HCA_CAP_2 << 0x1) | \ |
| 156 |
QueryHcaCapMod.CURRENT) |
| 157 |
query_cap2_out = QueryCmdHcaCap2Out(self.ctx.devx_general_cmd( |
| 158 |
query_cap2_in, len(QueryCmdHcaCap2Out()))) |
| 159 |
return query_cap2_out.capability.flow_counter_bulk_log_max_alloc |
| 160 |
|
| 161 |
@staticmethod |
| 162 |
def create_counter(ctx): |
| 163 |
""" |
| 164 |
Create flow counter. |
| 165 |
:param ctx: The player context to create the counter on. |
| 166 |
:return: The counter object and the flow counter ID . |
| 167 |
""" |
| 168 |
from tests.mlx5_prm_structs import AllocFlowCounterIn, AllocFlowCounterOut |
| 169 |
counter = Mlx5DevxObj(ctx, AllocFlowCounterIn(), len(AllocFlowCounterOut())) |
| 170 |
flow_counter_id = AllocFlowCounterOut(counter.out_view).flow_counter_id |
| 171 |
return counter, flow_counter_id |
| 172 |
|
| 173 |
def create_bulk_counter(self, ctx, bulk_size=0): |
| 174 |
""" |
| 175 |
Create flow bulk counter. |
| 176 |
:param ctx: The player context to create the counter on. |
| 177 |
:param bulk_size: The bulk size to use for the counter. |
| 178 |
:return: The counter object and the flow counter ID . |
| 179 |
""" |
| 180 |
from tests.mlx5_prm_structs import AllocFlowCounterIn, AllocFlowCounterOut |
| 181 |
bulk_log_max_alloc = self.query_bulk_counter_cap() |
| 182 |
if bulk_log_max_alloc > 0: |
| 183 |
# Bulk size is log2 of the number of counters eg. if bulk_size is 9, then there |
| 184 |
# are 512 counters |
| 185 |
cmd_in = AllocFlowCounterIn(flow_counter_bulk_log_size=bulk_size) |
| 186 |
else: |
| 187 |
# If bulk_log_max_alloc=0 then use old bitmask field, 0b100 means 512 counters |
| 188 |
cmd_in = AllocFlowCounterIn(flow_counter_bulk=0b100) |
| 189 |
counter = Mlx5DevxObj(ctx, cmd_in, len(AllocFlowCounterOut())) |
| 190 |
flow_counter_id = AllocFlowCounterOut(counter.out_view).flow_counter_id |
| 191 |
return counter, flow_counter_id |
| 192 |
|
| 193 |
@staticmethod |
| 194 |
def query_counter_packets(counter, flow_counter_id): |
| 195 |
""" |
| 196 |
Query flow counter packets count. |
| 197 |
:param counter: The counter for the query. |
| 198 |
:param flow_counter_id: The flow counter ID for the query. |
| 199 |
:return: Number of packets on this counter. |
| 200 |
""" |
| 201 |
from tests.mlx5_prm_structs import QueryFlowCounterIn, QueryFlowCounterOut |
| 202 |
query_in = QueryFlowCounterIn(flow_counter_id=flow_counter_id) |
| 203 |
counter_out = QueryFlowCounterOut(counter.query(query_in, len(QueryFlowCounterOut()))) |
| 204 |
return counter_out.flow_statistics.packets |
| 205 |
|
| 206 |
@staticmethod |
| 207 |
def query_bulk_counter(counter, flow_counter_id, num_of_counters=1): |
| 208 |
""" |
| 209 |
Query flow counter packets count for bulk counters. |
| 210 |
:param counter: The counter for the query. |
| 211 |
:param flow_counter_id: The flow counter ID for the query. |
| 212 |
:param num_of_counters: Number of counters to query. |
| 213 |
:return: Counter statistics. |
| 214 |
""" |
| 215 |
from tests.mlx5_prm_structs import QueryFlowCounterIn, QueryBulkFlowCounterOut |
| 216 |
query_in = QueryFlowCounterIn(flow_counter_id=flow_counter_id, |
| 217 |
num_of_counters=num_of_counters) |
| 218 |
counter_out = QueryBulkFlowCounterOut(counter.query(query_in, |
| 219 |
len(QueryBulkFlowCounterOut( |
| 220 |
num_statistics=num_of_counters)))) |
| 221 |
return counter_out.flow_statistics |
| 222 |
|
| 223 |
|
| 224 |
class Mlx5RCFlowResources(Mlx5RcResources): |
| 225 |
def __init__(self, dev_name, ib_port, gid_index, is_privileged_ctx=False, **kwargs): |
| 226 |
""" |
| 227 |
Initializes a Mlx5RCFlowResources object with the given values and creates |
| 228 |
basic RDMA resources. |
| 229 |
:param dev_name: Device name to be used |
| 230 |
:param ib_port: IB port of the device to use |
| 231 |
:param gid_index: Which GID index to use |
| 232 |
:param is_privileged_ctx: If True, creates a privileged context (default: False) |
| 233 |
""" |
| 234 |
self.obj_to_cleanup = [] |
| 235 |
self.is_privileged_ctx = is_privileged_ctx |
| 236 |
super().__init__(dev_name, ib_port, gid_index, **kwargs) |
| 237 |
|
| 238 |
def create_context(self): |
| 239 |
if self.is_privileged_ctx: |
| 240 |
create_privileged_context(self) |
| 241 |
check_rdma_transport_domain_caps(self) |
| 242 |
return |
| 243 |
super().create_context() |
| 244 |
|
| 245 |
def close_resources(self): |
| 246 |
for obj in self.obj_to_cleanup: |
| 247 |
if obj: |
| 248 |
obj.close() |
| 249 |
|
| 250 |
def create_matcher(self, mask, match_criteria_enable, flags=0, |
| 251 |
ft_type=MLX5DV_FLOW_TABLE_TYPE_NIC_RX_, ib_port=1, priority=0): |
| 252 |
""" |
| 253 |
Creates a matcher from a provided mask. |
| 254 |
:param mask: The mask to match on (in bytes) |
| 255 |
:param match_criteria_enable: Bitmask representing which of the |
| 256 |
headers and parameters in match_criteria |
| 257 |
are used |
| 258 |
:param flags: Flow matcher flags. |
| 259 |
:param ft_type: Flow table type. |
| 260 |
:param ib_port: Specify its corresponding port. |
| 261 |
:param priority: Priority value for the matcher. |
| 262 |
:return: Resulting matcher |
| 263 |
""" |
| 264 |
try: |
| 265 |
flow_match_param = Mlx5FlowMatchParameters(len(mask), mask) |
| 266 |
comp_mask = mlx5dv_flow_matcher_attr_mask.MLX5DV_FLOW_MATCHER_MASK_FT_TYPE |
| 267 |
|
| 268 |
if ft_type in [MLX5DV_FLOW_TABLE_TYPE_RDMA_TRANSPORT_RX_, |
| 269 |
MLX5DV_FLOW_TABLE_TYPE_RDMA_TRANSPORT_TX_]: |
| 270 |
if not is_eth(self.ctx, ib_port): |
| 271 |
raise unittest.SkipTest('Must be run on Ethernet link layer') |
| 272 |
|
| 273 |
comp_mask |= mlx5dv_flow_matcher_attr_mask.MLX5DV_FLOW_MATCHER_MASK_IB_PORT |
| 274 |
|
| 275 |
attr = Mlx5FlowMatcherAttr(match_mask=flow_match_param, |
| 276 |
match_criteria_enable=match_criteria_enable, |
| 277 |
flags=flags, ft_type=ft_type, comp_mask=comp_mask, |
| 278 |
ib_port=ib_port, priority=priority) |
| 279 |
matcher = Mlx5FlowMatcher(self.ctx, attr) |
| 280 |
except PyverbsRDMAError as ex: |
| 281 |
if ex.error_code in [errno.EOPNOTSUPP, errno.EPROTONOSUPPORT]: |
| 282 |
raise unittest.SkipTest('Matcher creation is not supported') |
| 283 |
raise ex |
| 284 |
return matcher |
| 285 |
|
| 286 |
def store_for_cleanup_stage(self, flow_table_obj): |
| 287 |
""" |
| 288 |
Stores the given object for cleanup in the correct order. |
| 289 |
:param flow_table_obj: The object to be stored. |
| 290 |
""" |
| 291 |
self.obj_to_cleanup.insert(0, flow_table_obj) |
| 292 |
|
| 293 |
def create_devx_flow_table(self, table_type, level): |
| 294 |
""" |
| 295 |
Creates a DEVX flow table with the given type and level. |
| 296 |
:param table_type: The flow table type. |
| 297 |
:param level: The flow table level. |
| 298 |
:return: A tuple containing the created flow table object and the table ID. |
| 299 |
""" |
| 300 |
from tests.mlx5_prm_structs import CreateFlowTableIn,\ |
| 301 |
CreateFlowTableOut, FlowTableContext,CreateFlowTableOut |
| 302 |
cmd_in = CreateFlowTableIn(table_type=table_type, |
| 303 |
flow_table_context=FlowTableContext(level=level)) |
| 304 |
flow_table_obj = Mlx5DevxObj(self.ctx, cmd_in, len(CreateFlowTableOut())) |
| 305 |
self.store_for_cleanup_stage(flow_table_obj) |
| 306 |
out = CreateFlowTableOut(flow_table_obj.out_view) |
| 307 |
return flow_table_obj, out.table_id |
| 308 |
|
| 309 |
def create_devx_flow_group(self, table_id, table_type): |
| 310 |
""" |
| 311 |
Creates a DEVX flow group for the specified table. |
| 312 |
:param table_id: The ID of the flow table. |
| 313 |
:param table_type: The type of the flow table. |
| 314 |
:return: The ID of the created flow group. |
| 315 |
""" |
| 316 |
from tests.mlx5_prm_structs import CreateFlowGroupIn, CreateFlowGroupOut |
| 317 |
cmd_in = CreateFlowGroupIn(table_type=table_type, |
| 318 |
table_id=table_id) |
| 319 |
flow_group_ojb = Mlx5DevxObj(self.ctx, cmd_in, len(CreateFlowGroupOut())) |
| 320 |
self.store_for_cleanup_stage(flow_group_ojb) |
| 321 |
out = CreateFlowGroupOut(flow_group_ojb.out_view) |
| 322 |
return out.group_id |
| 323 |
|
| 324 |
def create_devx_flow_entry(self, table_id, group_id, action, table_type): |
| 325 |
""" |
| 326 |
Creates a DEVX flow table entry with the specified parameters. |
| 327 |
:param table_id: The ID of the flow table. |
| 328 |
:param group_id: The ID of the flow group. |
| 329 |
:param action: The action for the flow entry. |
| 330 |
:param table_type: The type of the flow table. |
| 331 |
:return: The created flow table entry object. |
| 332 |
""" |
| 333 |
from tests.mlx5_prm_structs import SetFlowTableEntryIn, FlowContext, SetFlowTableEntryOut |
| 334 |
cmd_in = SetFlowTableEntryIn(table_type=table_type, table_id=table_id, |
| 335 |
flow_context=FlowContext(group_id=group_id, action=action)) |
| 336 |
flow_table_entry_obj = Mlx5DevxObj(self.ctx, cmd_in, len(SetFlowTableEntryOut())) |
| 337 |
self.store_for_cleanup_stage(flow_table_entry_obj) |
| 338 |
|
| 339 |
|
| 340 |
class Mlx5MatcherTest(Mlx5RDMATestCase): |
| 341 |
def setUp(self): |
| 342 |
super().setUp() |
| 343 |
self.iters = 10 |
| 344 |
self.server = None |
| 345 |
self.client = None |
| 346 |
|
| 347 |
def tearDown(self): |
| 348 |
""" |
| 349 |
Cleans up resources if manual cleanup is needed. |
| 350 |
""" |
| 351 |
if self.server and hasattr(self.server, 'obj_to_cleanup'): |
| 352 |
self.server.close_resources() |
| 353 |
if self.client and hasattr(self.client, 'obj_to_cleanup'): |
| 354 |
self.client.close_resources() |
| 355 |
super().tearDown() |
| 356 |
|
| 357 |
@u.skip_unsupported |
| 358 |
def test_create_empty_matcher(self): |
| 359 |
""" |
| 360 |
Creates an empty matcher |
| 361 |
""" |
| 362 |
self.res = Mlx5FlowResources(**self.dev_info) |
| 363 |
empty_mask = bytes(MAX_MATCH_PARAM_SIZE) |
| 364 |
self.res.create_matcher(empty_mask, u.MatchCriteriaEnable.NONE) |
| 365 |
|
| 366 |
@u.skip_unsupported |
| 367 |
def test_create_smac_matcher(self): |
| 368 |
""" |
| 369 |
Creates a matcher to match on outer source mac |
| 370 |
""" |
| 371 |
self.res = Mlx5FlowResources(**self.dev_info) |
| 372 |
smac_mask = bytes([0xff, 0xff, 0xff, 0xff, 0xff, 0xff]) |
| 373 |
self.res.create_matcher(smac_mask, u.MatchCriteriaEnable.OUTER) |
| 374 |
|
| 375 |
def generic_test_mlx5_flow_table(self, flow_table_type_mapping, action, ib_port=1, |
| 376 |
traffic=False, priority=0): |
| 377 |
""" |
| 378 |
This function performs the following steps on the server side: |
| 379 |
1. Creates a DEVX flow table with the specified table type. |
| 380 |
2. Creates a flow group in the DEVX flow table. |
| 381 |
3. Inserts a flow table entry (FTE) into the flow table group. |
| 382 |
4. Creates an mlx5 flow with an empty matcher (to steer all packets) on the specified |
| 383 |
table type, directing them to the DEVX flow table created in step #1. |
| 384 |
5. If the `traffic` flag is set to `True`, RDMA traffic is run to test the flow steering. |
| 385 |
|
| 386 |
:param flow_table_type_mapping: Dictionary mapping flow table types. |
| 387 |
:param action: Specifies the action to be performed on the matched packets. |
| 388 |
:param ib_port: The port for flow matching. |
| 389 |
:param traffic: Boolean flag indicating whether RDMA traffic should run. |
| 390 |
:param priority: Priority value for the matcher. |
| 391 |
""" |
| 392 |
empty_mask = bytes(MAX_MATCH_PARAM_SIZE) |
| 393 |
|
| 394 |
for ft_type, table_type in flow_table_type_mapping.items(): |
| 395 |
devx_table_obj, table_id = self.server.create_devx_flow_table(table_type, |
| 396 |
level=TABLE_LEVEL) |
| 397 |
matcher = self.server.create_matcher(empty_mask, u.MatchCriteriaEnable.NONE, |
| 398 |
ft_type=ft_type, ib_port=ib_port, |
| 399 |
priority=priority) |
| 400 |
self.server.store_for_cleanup_stage(matcher) |
| 401 |
group_id = self.server.create_devx_flow_group(table_id, table_type) |
| 402 |
self.server.create_devx_flow_entry(table_id, group_id, action, table_type) |
| 403 |
empty_value_param = Mlx5FlowMatchParameters(len(empty_mask), empty_mask) |
| 404 |
action_dest = Mlx5FlowActionAttr(action_type=mlx5dv_flow_action_type.MLX5DV_FLOW_ACTION_DEST_DEVX, |
| 405 |
obj=devx_table_obj) |
| 406 |
self.server.flow = Mlx5Flow(matcher, empty_value_param, [action_dest], 1) |
| 407 |
|
| 408 |
if traffic: |
| 409 |
u.traffic(client=self.client, server=self.server,iters=self.iters, |
| 410 |
gid_idx=self.gid_index, port=self.ib_port, is_cq_ex=True) |
| 411 |
|
| 412 |
@u.skip_unsupported |
| 413 |
@requires_no_sriov() |
| 414 |
@requires_cap_net_raw() |
| 415 |
def test_flow_table_drop(self): |
| 416 |
""" |
| 417 |
Creates rules with DevX objects for RDMA RX and TX tables. |
| 418 |
- Creates two flow tables, one for RDMA RX and one for RDMA TX. |
| 419 |
- Configures matchers with different flow tables. |
| 420 |
- Configures flow actions to drop. |
| 421 |
""" |
| 422 |
self.create_players(Mlx5RCFlowResources) |
| 423 |
flow_table_type_mapping = { |
| 424 |
MLX5DV_FLOW_TABLE_TYPE_RDMA_RX_: NIC_RX_RDMA_TABLE_TYPE, |
| 425 |
MLX5DV_FLOW_TABLE_TYPE_RDMA_TX_: NIC_TX_RDMA_TABLE_TYPE} |
| 426 |
self.generic_test_mlx5_flow_table(flow_table_type_mapping, action=DROP_ACTION) |
| 427 |
|
| 428 |
@u.skip_unsupported |
| 429 |
@requires_root() |
| 430 |
def test_flow_rdma_transport_domain_traffic(self): |
| 431 |
""" |
| 432 |
Creates a devx table object with the RDMA transport domain, |
| 433 |
Verifies that the traffic passes successfully. |
| 434 |
""" |
| 435 |
self.rdma_transport_domain_test(priority=0) |
| 436 |
|
| 437 |
@u.skip_unsupported |
| 438 |
@requires_root() |
| 439 |
def test_priority_rdma_transport_domain_traffic(self): |
| 440 |
""" |
| 441 |
Creates a devx table object with the RDMA transport domain with priority, |
| 442 |
Verifies that the traffic passes successfully. |
| 443 |
""" |
| 444 |
self.rdma_transport_domain_test(priority=1) |
| 445 |
|
| 446 |
def rdma_transport_domain_test(self, priority): |
| 447 |
""" |
| 448 |
:param priority: Priority value for the flow table matcher. |
| 449 |
""" |
| 450 |
self.client = Mlx5RcResources(**self.dev_info) |
| 451 |
self.server = Mlx5RCFlowResources(is_privileged_ctx=True, **self.dev_info) |
| 452 |
self.pre_run() |
| 453 |
self.sync_remote_attr() |
| 454 |
flow_table_type_mapping = { |
| 455 |
MLX5DV_FLOW_TABLE_TYPE_RDMA_TRANSPORT_RX_: RDMA_TRANSPORT_RX, |
| 456 |
MLX5DV_FLOW_TABLE_TYPE_RDMA_TRANSPORT_TX_: RDMA_TRANSPORT_TX} |
| 457 |
self.generic_test_mlx5_flow_table(flow_table_type_mapping, action=ALLOW_ACTION, ib_port=1, |
| 458 |
traffic=True, priority=priority) |
| 459 |
|
| 460 |
@u.skip_unsupported |
| 461 |
def test_smac_matcher_to_qp_flow(self): |
| 462 |
""" |
| 463 |
Creates a matcher to match on outer source mac and a flow that forwards |
| 464 |
packets to QP when matching on source mac. |
| 465 |
""" |
| 466 |
self.create_players(Mlx5FlowResources) |
| 467 |
smac_mask = bytes([0xff] * 6) |
| 468 |
matcher = self.server.create_matcher(smac_mask, |
| 469 |
u.MatchCriteriaEnable.OUTER) |
| 470 |
smac_value = struct.pack('!6s', |
| 471 |
bytes.fromhex(PacketConsts.SRC_MAC.replace(':', ''))) |
| 472 |
value_param = Mlx5FlowMatchParameters(len(smac_value), smac_value) |
| 473 |
action_qp = Mlx5FlowActionAttr(action_type=mlx5dv_flow_action_type.MLX5DV_FLOW_ACTION_DEST_IBV_QP, |
| 474 |
qp=self.server.qp) |
| 475 |
self.server.flow = Mlx5Flow(matcher, value_param, [action_qp], 1) |
| 476 |
u.raw_traffic(self.client, self.server, self.iters) |
| 477 |
|
| 478 |
@u.skip_unsupported |
| 479 |
def test_counter_qp_flow(self): |
| 480 |
""" |
| 481 |
Creates a matcher to match on outer source mac and a flow that forwards |
| 482 |
packets to QP when matching on source mac. |
| 483 |
""" |
| 484 |
self.create_players(Mlx5CounterFlowResources) |
| 485 |
empty_bytes_arr = bytes(MAX_MATCH_PARAM_SIZE) |
| 486 |
empty_value_param = Mlx5FlowMatchParameters(len(empty_bytes_arr), empty_bytes_arr) |
| 487 |
matcher = self.server.create_matcher(empty_bytes_arr, u.MatchCriteriaEnable.NONE) |
| 488 |
qp_attr = Mlx5FlowActionAttr(action_type=mlx5dv_flow_action_type.MLX5DV_FLOW_ACTION_DEST_IBV_QP, |
| 489 |
qp=self.server.qp) |
| 490 |
counter, flow_counter_id = self.server.create_counter(self.server.ctx) |
| 491 |
ctr_attr = Mlx5FlowActionAttr(action_type=mlx5dv_flow_action_type.MLX5DV_FLOW_ACTION_COUNTERS_DEVX, |
| 492 |
obj=counter) |
| 493 |
self.server.flow = Mlx5Flow(matcher, empty_value_param, [ctr_attr, qp_attr], 2) |
| 494 |
u.raw_traffic(self.client, self.server, self.iters) |
| 495 |
sent_packets = self.server.query_counter_packets(counter, flow_counter_id) |
| 496 |
self.assertEqual(sent_packets, self.iters, 'Counter of metadata missed some sent packets') |
| 497 |
|
| 498 |
@u.skip_unsupported |
| 499 |
def test_counters_bulk_flow(self): |
| 500 |
""" |
| 501 |
Flow action counters with bulk size. |
| 502 |
Creates 4 flows with different dst ip addresses. |
| 503 |
Sends 10 packets to each flow. |
| 504 |
Verifies each flow get a hit and goes to QP final destination and counter is incremented |
| 505 |
accordingly. |
| 506 |
""" |
| 507 |
from tests.mlx5_prm_structs import FlowTableEntryMatchSetLyr24, FlowTableEntryMatchParam |
| 508 |
|
| 509 |
self.create_players(Mlx5CounterFlowResources) |
| 510 |
outer_header = FlowTableEntryMatchSetLyr24(ethertype=0xffff, ip_version=0xf, |
| 511 |
src_ip_mask=0xffffffff) |
| 512 |
mask = FlowTableEntryMatchParam(outer_headers=outer_header) |
| 513 |
matcher = self.server.create_matcher(mask, u.MatchCriteriaEnable.OUTER) |
| 514 |
action_qp_attr = Mlx5FlowActionAttr( |
| 515 |
action_type=mlx5dv_flow_action_type.MLX5DV_FLOW_ACTION_DEST_IBV_QP, |
| 516 |
qp=self.server.qp) |
| 517 |
# Create a counter with bulk_size=2 means there are 4 counters in the bulk. |
| 518 |
counter, flow_counter_id = self.server.create_bulk_counter(self.server.ctx, bulk_size=2) |
| 519 |
action_type = mlx5dv_flow_action_type.MLX5DV_FLOW_ACTION_COUNTERS_DEVX_WITH_OFFSET |
| 520 |
flows = [] |
| 521 |
flows_num = 4 |
| 522 |
|
| 523 |
for flow_idx in range(flows_num): |
| 524 |
src_ip = f'{flow_idx + 2}.{flow_idx + 2}.{flow_idx + 2}.{flow_idx + 2}' |
| 525 |
packet = u.gen_packet(self.client.msg_size, ip_version=4, src_ipv4=src_ip) |
| 526 |
# Each offset is different counter |
| 527 |
action_counter_attr = Mlx5FlowActionAttr(action_type=action_type, obj=counter, |
| 528 |
offset=flow_idx) |
| 529 |
outer_header = FlowTableEntryMatchSetLyr24(ethertype=PacketConsts.ETHER_TYPE_IPV4, |
| 530 |
ip_version=4, src_ip4=src_ip) |
| 531 |
value_param = Mlx5FlowMatchParameters(len(outer_header), outer_header) |
| 532 |
flows.append(Mlx5Flow(matcher, value_param, [action_counter_attr, action_qp_attr], 2)) |
| 533 |
u.raw_traffic(self.client, self.server, self.iters, packet_to_send=packet) |
| 534 |
ctr_stats = self.server.query_bulk_counter(counter, flow_counter_id, |
| 535 |
num_of_counters=flows_num) |
| 536 |
# Verify counters |
| 537 |
for j in range(flows_num): |
| 538 |
exp_num_pkts = self.iters if j <= flow_idx else 0 |
| 539 |
self.assertEqual(ctr_stats[j].packets, exp_num_pkts, |
| 540 |
f'Counter {j} in metadata missed some sent packets') |
| 541 |
|
| 542 |
@requires_reformat_support |
| 543 |
@u.requires_encap_disabled_if_eswitch_on |
| 544 |
def test_tx_packet_reformat(self): |
| 545 |
""" |
| 546 |
Creates packet reformat (encap) action on TX and with QP action on RX |
| 547 |
verifies that the packet was encapsulated as expected. |
| 548 |
""" |
| 549 |
self.client = Mlx5FlowResources(**self.dev_info) |
| 550 |
outer = gen_vxlan_l2_tunnel_encap_header(self.client.msg_size) |
| 551 |
# Due to encapsulation action Ipv4 and UDP checksum of the outer header |
| 552 |
# will be recalculated, need to skip them during packet validation. |
| 553 |
ipv4_id_idx = [18, 19] |
| 554 |
ipv4_chksum_idx = [24, 25] |
| 555 |
udp_chksum_idx = [34, 35] |
| 556 |
# Server will receive encaped packet so message size must include the |
| 557 |
# length of the outer part. |
| 558 |
self.server = Mlx5FlowResources(msg_size=self.client.msg_size + len(outer), |
| 559 |
**self.dev_info) |
| 560 |
empty_bytes_arr = bytes(MAX_MATCH_PARAM_SIZE) |
| 561 |
empty_value_param = Mlx5FlowMatchParameters(len(empty_bytes_arr), |
| 562 |
empty_bytes_arr) |
| 563 |
|
| 564 |
# TX steering |
| 565 |
tx_matcher = self.client.create_matcher(empty_bytes_arr, |
| 566 |
u.MatchCriteriaEnable.NONE, |
| 567 |
ibv_flow_flags.IBV_FLOW_ATTR_FLAGS_EGRESS, |
| 568 |
MLX5DV_FLOW_TABLE_TYPE_NIC_TX_) |
| 569 |
# Create encap action |
| 570 |
reformat_action = Mlx5PacketReformatFlowAction( |
| 571 |
self.client.ctx, data=outer, |
| 572 |
reformat_type=MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TO_L2_TUNNEL_, |
| 573 |
ft_type=MLX5DV_FLOW_TABLE_TYPE_NIC_TX_) |
| 574 |
action_reformat_attr = Mlx5FlowActionAttr(flow_action=reformat_action, |
| 575 |
action_type=mlx5dv_flow_action_type.MLX5DV_FLOW_ACTION_IBV_FLOW_ACTION) |
| 576 |
self.client.flow = Mlx5Flow(tx_matcher, empty_value_param, |
| 577 |
[action_reformat_attr], 1) |
| 578 |
|
| 579 |
# RX steering |
| 580 |
rx_matcher = self.server.create_matcher(empty_bytes_arr, u.MatchCriteriaEnable.NONE) |
| 581 |
action_qp_attr = Mlx5FlowActionAttr(action_type=mlx5dv_flow_action_type.MLX5DV_FLOW_ACTION_DEST_IBV_QP, |
| 582 |
qp=self.server.qp) |
| 583 |
self.server.flow = Mlx5Flow(rx_matcher, empty_value_param, [action_qp_attr], 1) |
| 584 |
|
| 585 |
# Send traffic and validate packet |
| 586 |
packet = u.gen_packet(self.client.msg_size) |
| 587 |
u.raw_traffic(self.client, self.server, self.iters, |
| 588 |
expected_packet=outer + packet, |
| 589 |
skip_idxs=ipv4_id_idx + ipv4_chksum_idx + udp_chksum_idx) |