/
test_mlx5_dr.py
1402 lines (1298 loc) · 70.7 KB
/
test_mlx5_dr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# SPDX-License-Identifier: (GPL-2.0 OR Linux-OpenIB)
# Copyright (c) 2020 Nvidia All rights reserved. See COPYING file
"""
Test module for pyverbs' mlx5 dr module.
"""
from os import path, system
import unittest
import struct
import socket
import errno
import math
from pyverbs.providers.mlx5.dr_action import DrActionQp, DrActionModify, \
DrActionFlowCounter, DrActionDrop, DrActionTag, DrActionDestTable, \
DrActionPopVLan, DrActionPushVLan, DrActionDestAttr, DrActionDestArray, \
DrActionDefMiss, DrActionVPort, DrActionIBPort, DrActionDestTir, DrActionPacketReformat,\
DrFlowSamplerAttr, DrActionFlowSample, DrFlowMeterAttr, DrActionFlowMeter
from pyverbs.providers.mlx5.mlx5dv import Mlx5DevxObj, Mlx5Context, Mlx5DVContextAttr
from tests.utils import skip_unsupported, requires_root_on_eth, requires_eswitch_on, \
PacketConsts
from tests.mlx5_base import Mlx5RDMATestCase, PyverbsAPITestCase, MELLANOX_VENDOR_ID
from pyverbs.providers.mlx5.mlx5dv_flow import Mlx5FlowMatchParameters
from pyverbs.pyverbs_error import PyverbsRDMAError, PyverbsUserError
from pyverbs.providers.mlx5.dr_matcher import DrMatcher
from pyverbs.providers.mlx5.dr_domain import DrDomain
from pyverbs.providers.mlx5.dr_table import DrTable
from pyverbs.providers.mlx5.dr_rule import DrRule
import pyverbs.providers.mlx5.mlx5_enums as dve
from tests.test_mlx5_flow import requires_reformat_support
from pyverbs.cq import CqInitAttrEx, CQEX, CQ
from pyverbs.wq import WQInitAttr, WQ, WQAttr
from tests.base import RawResources
import pyverbs.enums as e
import tests.utils as u
SET_ACTION = 0x1
MAX_MATCH_PARAM_SIZE = 0x180
PF_VPORT = 0x0
GENEVE_PACKET_OUTER_LENGTH = 50
ROCE_PACKET_OUTER_LENGTH = 58
SAMPLER_ERROR_MARGIN = 0.2
SAMPLE_RATIO = 4
METADATA_C_FIELDS = ['metadata_reg_c_0', 'metadata_reg_c_1', 'metadata_reg_c_2',
'metadata_reg_c_3', 'metadata_reg_c_4', 'metadata_reg_c_5']
FLOW_METER_GREEN = 2
FLOW_METER_RED = 0
REG_C_DATA = 0x1234
class ModifyFields:
"""
Supported SW steering modify fields.
"""
OUT_SMAC_47_16 = 0x1
OUT_SMAC_15_0 = 0x2
META_DATA_REG_C_0 = 0x51
META_DATA_REG_C_1 = 0x52
class ModifyFieldsLen:
"""
Supported SW steering modify fields length.
"""
MAC_47_16 = 32
MAC_15_0 = 16
META_DATA_REG_C = 32
def skip_if_has_geneve_tx_bug(ctx):
"""
Some mlx5 devices such as CX5 and CX6 has a bug matching on Geneve fields
on TX side.
Raises unittest.SkipTest if that's the case.
:param ctx: Mlx5 Context
"""
dev_attrs = ctx.query_device()
mlx5_cx5_cx6 = [0x1017, 0x1018, 0x1019, 0x101a, 0x101b]
if dev_attrs.vendor_id == MELLANOX_VENDOR_ID and \
dev_attrs.vendor_part_id in mlx5_cx5_cx6:
raise unittest.SkipTest('This test is not supported on cx5/6')
def requires_geneve_fields_rx_support(func):
def func_wrapper(instance):
nic_tbl_caps = u.query_nic_flow_table_caps(instance)
field_support = nic_tbl_caps.flow_table_properties_nic_receive.ft_field_support
if not (field_support.outer_geneve_vni and field_support.outer_geneve_oam and
field_support.outer_geneve_protocol_type and field_support.outer_geneve_opt_len):
raise unittest.SkipTest('NIC flow table does not support geneve fields')
return func(instance)
return func_wrapper
class Mlx5DrResources(RawResources):
"""
Test various functionalities of the mlx5 direct rules class.
"""
def create_context(self):
mlx5dv_attr = Mlx5DVContextAttr()
try:
self.ctx = Mlx5Context(mlx5dv_attr, name=self.dev_name)
except PyverbsUserError as ex:
raise unittest.SkipTest(f'Could not open mlx5 context ({ex})')
except PyverbsRDMAError:
raise unittest.SkipTest('Opening mlx5 context is not supported')
def __init__(self, dev_name, ib_port, gid_index=0, wc_flags=0, msg_size=1024, qp_count=1):
self.wc_flags = wc_flags
super().__init__(dev_name=dev_name, ib_port=ib_port, gid_index=gid_index,
msg_size=msg_size, qp_count=qp_count)
@requires_root_on_eth()
def create_qps(self):
super().create_qps()
def create_cq(self):
"""
Create an Extended CQ.
"""
wc_flags = e.IBV_WC_STANDARD_FLAGS | self.wc_flags
cia = CqInitAttrEx(cqe=self.num_msgs, wc_flags=wc_flags)
try:
self.cq = CQEX(self.ctx, cia)
except PyverbsRDMAError as ex:
if ex.error_code == errno.EOPNOTSUPP:
raise unittest.SkipTest('Create Extended CQ is not supported')
raise ex
def get_first_flow_meter_reg_id(self):
"""
Queries hca caps for supported reg C indexes for flow meter.
:return: First reg C index that is supported
"""
from tests.mlx5_prm_structs import QueryHcaCapIn, QueryQosCapOut, DevxOps
query_cap_in = QueryHcaCapIn(op_mod=DevxOps.MLX5_CMD_OP_QUERY_QOS_CAP << 1)
cmd_res = self.ctx.devx_general_cmd(query_cap_in, len(QueryQosCapOut()))
query_cap_out = QueryQosCapOut(cmd_res)
if query_cap_out.status:
raise PyverbsRDMAError(f'QUERY_HCA_CAP has failed with status ({query_cap_out.status}) '
f'and syndrome ({query_cap_out.syndrome})')
bit_regs = query_cap_out.capability.flow_meter_reg_id
if bit_regs == 0:
raise PyverbsRDMAError(f'Reg C is not supported)')
return int(math.log2(bit_regs & -bit_regs))
class Mlx5DrTirResources(Mlx5DrResources):
def __init__(self, dev_name, ib_port, gid_index=0, wc_flags=0, msg_size=1024,
qp_count=1, server=False):
self.server = server
super().__init__(dev_name=dev_name, ib_port=ib_port, gid_index=gid_index,
wc_flags=wc_flags, msg_size=msg_size, qp_count=qp_count)
def create_cq(self):
self.cq = CQ(self.ctx, cqe=self.num_msgs)
@requires_root_on_eth()
def create_qps(self):
if not self.server:
super().create_qps()
else:
from tests.mlx5_prm_structs import Tirc, CreateTirIn, CreateTirOut
self.qps = [WQ(self.ctx, WQInitAttr(wq_pd=self.pd, wq_cq=self.cq))]
self.qps[0].modify(WQAttr(attr_mask=e.IBV_WQ_ATTR_STATE, wq_state=e.IBV_WQS_RDY))
tir_ctx = Tirc(inline_rqn=self.qps[0].wqn)
self.tir = Mlx5DevxObj(self.ctx, CreateTirIn(tir_context=tir_ctx), len(CreateTirOut()))
class Mlx5DrTest(Mlx5RDMATestCase):
def setUp(self):
super().setUp()
self.iters = 10
self.server = None
self.client = None
self.rules = []
def tearDown(self):
if self.server:
self.server.ctx.close()
if self.client:
self.client.ctx.close()
@skip_unsupported
def create_rx_recv_rules_based_on_match_params(self, mask_param, val_param, actions,
match_criteria=u.MatchCriteriaEnable.OUTER,
domain=None, log_matcher_size=None,
root_only=False):
"""
Creates a rule on RX domain that forwards packets that match on the provided parameters
to the SW steering flow table and another rule on that table
with provided actions.
:param mask_param: The FlowTableEntryMatchParam mask matcher value.
:param val_param: The FlowTableEntryMatchParam value matcher value.
:param actions: List of actions to attach to the recv rule.
:param match_criteria: the match criteria enable flag to match on
:param domain: RX DR domain to use if provided, otherwise create default RX domain.
:param log_matcher_size: Size of the matcher table
:param root_only : If True, rules are created only on root table
:return: Non-root table and dest table action to it if root=false else root_table
"""
self.domain_rx = domain if domain else DrDomain(self.server.ctx,
dve.MLX5DV_DR_DOMAIN_TYPE_NIC_RX)
root_table = DrTable(self.domain_rx, 0)
if not root_only:
non_root_table = DrTable(self.domain_rx, 1)
table = root_table if root_only else non_root_table
self.matcher = DrMatcher(table, 1, match_criteria, mask_param)
if log_matcher_size:
self.matcher.set_layout(log_matcher_size)
self.rules.append(DrRule(self.matcher, val_param, actions))
if not root_only:
self.root_matcher = DrMatcher(root_table, 0, match_criteria, mask_param)
self.dest_table_action = DrActionDestTable(table)
self.rules.append(DrRule(self.root_matcher, val_param, [self.dest_table_action]))
return table, self.dest_table_action
return table
@skip_unsupported
def create_rx_recv_rules(self, smac_value, actions, log_matcher_size=None, domain=None,
root_only=False):
"""
Creates a rule on RX domain that forwards packets that match the smac in the matcher
to the SW steering flow table and another rule on that table with provided actions.
:param smac_value: The smac matcher value.
:param actions: List of actions to attach to the recv rule.
:param log_matcher_size: Size of the matcher table
:param domain: RX DR domain to use if provided, otherwise create default RX domain.
:param root_only : If True, rules are created only on root table
:return: Non-root table and dest table action to it if root_only=false else root_table
"""
smac_mask = bytes([0xff] * 6) + bytes(2)
mask_param = Mlx5FlowMatchParameters(len(smac_mask), smac_mask)
# Size of the matcher value should be modulo 4
smac_value = smac_value if root_only else smac_value + bytes(2)
value_param = Mlx5FlowMatchParameters(len(smac_value), smac_value)
return self.create_rx_recv_rules_based_on_match_params(mask_param, value_param, actions,
u.MatchCriteriaEnable.OUTER,
domain, log_matcher_size,
root_only=root_only)
def send_client_raw_packets(self, iters, src_mac=None):
"""
Send raw packets.
:param iters: Number of packets to send.
:param src_mac: If set, src mac to set in the packets.
"""
c_send_wr, _, _ = u.get_send_elements_raw_qp(self.client, src_mac=src_mac)
poll_cq = u.poll_cq_ex if isinstance(self.client.cq, CQEX) else u.poll_cq
for _ in range(iters):
u.send(self.client, c_send_wr, e.IBV_WR_SEND)
poll_cq(self.client.cq)
def send_server_fdb_to_nic_packets(self, iters):
"""
Server sends and receives raw packets.
:param iters: Number of packets to send.
"""
s_recv_wr = u.get_recv_wr(self.server)
u.post_recv(self.server, s_recv_wr, qp_idx=0)
c_send_wr, _, msg = u.get_send_elements_raw_qp(self.server)
for _ in range(iters):
u.send(self.server, c_send_wr, e.IBV_WR_SEND)
u.poll_cq_ex(self.server.cq)
u.poll_cq_ex(self.server.cq)
u.post_recv(self.server, s_recv_wr, qp_idx=0)
msg_received = self.server.mr.read(self.server.msg_size, 0)
u.validate_raw(msg_received, msg, [])
def dest_port(self, is_vport=True):
"""
Creates FDB domain, root table with matcher on source mac on the server
side. Create a rule to forward all traffic to the non-root table.
On this table apply VPort/IBPort action goto PF.
On the server open another RX domain on PF with QP action and validate
packets by sending traffic from client, catch all traffic with
VPort/IBPort action goto PF, open another RX domain on PF with QP
action and validate packets.
:param is_vport: A flag to indicate if to use VPort or IBPort action.
"""
self.client = Mlx5DrResources(**self.dev_info)
self.server = Mlx5DrResources(**self.dev_info)
self.domain_fdb = DrDomain(self.server.ctx, dve.MLX5DV_DR_DOMAIN_TYPE_FDB)
port_action = DrActionVPort(self.domain_fdb, PF_VPORT) if is_vport \
else DrActionIBPort(self.domain_fdb, self.ib_port)
smac_value = struct.pack('!6s', bytes.fromhex(PacketConsts.SRC_MAC.replace(':', '')))
self.fdb_table, self.fdb_dest_act = self.create_rx_recv_rules(smac_value, [port_action],
domain=self.domain_fdb)
self.domain_rx = DrDomain(self.server.ctx, dve.MLX5DV_DR_DOMAIN_TYPE_NIC_RX)
rx_table = DrTable(self.domain_rx, 0)
qp_action = DrActionQp(self.server.qp)
smac_mask = bytes([0xff] * 6)
mask_param = Mlx5FlowMatchParameters(len(smac_mask), smac_mask)
rx_matcher = DrMatcher(rx_table, 0, u.MatchCriteriaEnable.OUTER, mask_param)
value_param = Mlx5FlowMatchParameters(len(smac_value), smac_value)
self.rules.append(DrRule(rx_matcher, value_param, [qp_action]))
# Validate traffic on RX
u.raw_traffic(self.client, self.server, self.iters)
@staticmethod
def create_dest_mac_params():
from tests.mlx5_prm_structs import FlowTableEntryMatchParam
eth_match_mask = FlowTableEntryMatchParam()
eth_match_mask.outer_headers.dmac = PacketConsts.MAC_MASK
eth_match_value = FlowTableEntryMatchParam()
eth_match_value.outer_headers.dmac = PacketConsts.DST_MAC
mask_param = Mlx5FlowMatchParameters(len(eth_match_mask), eth_match_mask)
value_param = Mlx5FlowMatchParameters(len(eth_match_value), eth_match_value)
return mask_param, value_param
@staticmethod
def create_counter(ctx):
"""
Create flow counter.
:param ctx: The player context to create the counter on.
:return: The counter object and the flow counter ID .
"""
from tests.mlx5_prm_structs import AllocFlowCounterIn, AllocFlowCounterOut
counter = Mlx5DevxObj(ctx, AllocFlowCounterIn(), len(AllocFlowCounterOut()))
flow_counter_id = AllocFlowCounterOut(counter.out_view).flow_counter_id
return counter, flow_counter_id
@staticmethod
def query_counter_packets(counter, flow_counter_id):
"""
Query flow counter packets count.
:param counter: The counter for the query.
:param flow_counter_id: The flow counter ID for the query.
:return: Number of packets on this counter.
"""
from tests.mlx5_prm_structs import QueryFlowCounterIn, QueryFlowCounterOut
query_in = QueryFlowCounterIn(flow_counter_id=flow_counter_id)
counter_out = QueryFlowCounterOut(counter.query(query_in, len(QueryFlowCounterOut())))
return counter_out.flow_statistics.packets
@staticmethod
def gen_gre_tunnel_encap_header(msg_size, is_l2_tunnel=True):
gre_ether_type = PacketConsts.ETHER_TYPE_ETH if is_l2_tunnel else \
PacketConsts.ETHER_TYPE_IPV4
gre_header = u.gen_gre_header(ether_type=gre_ether_type)
ip_header = u.gen_ipv4_header(packet_len=msg_size + len(gre_header),
next_proto=socket.IPPROTO_GRE)
mac_header = u.gen_ethernet_header()
return mac_header + ip_header + gre_header
@staticmethod
def gen_geneve_tunnel_encap_header(msg_size, is_l2_tunnel=True):
proto = PacketConsts.ETHER_TYPE_ETH if is_l2_tunnel else PacketConsts.ETHER_TYPE_IPV4
geneve_header = u.gen_geneve_header(proto=proto)
udp_header = u.gen_udp_header(packet_len=msg_size + len(geneve_header),
dst_port=PacketConsts.GENEVE_PORT)
ip_header = u.gen_ipv4_header(packet_len=msg_size + len(udp_header) + len(geneve_header))
mac_header = u.gen_ethernet_header()
return mac_header + ip_header + udp_header + geneve_header
@staticmethod
def create_geneve_params():
from tests.mlx5_prm_structs import FlowTableEntryMatchParam
geneve_mask = FlowTableEntryMatchParam()
geneve_mask.misc_parameters.geneve_vni = 0xffffff
geneve_mask.misc_parameters.geneve_oam = 1
geneve_value = FlowTableEntryMatchParam()
geneve_value.misc_parameters.geneve_vni = PacketConsts.GENEVE_VNI
geneve_value.misc_parameters.geneve_oam = PacketConsts.GENEVE_OAM
mask_param = Mlx5FlowMatchParameters(len(geneve_mask), geneve_mask)
value_param = Mlx5FlowMatchParameters(len(geneve_value), geneve_value)
return mask_param, value_param
@staticmethod
def gen_roce_bth_header(msg_size):
mac_header = u.gen_ethernet_header()
ip_header = u.gen_ipv4_header(packet_len=msg_size + PacketConsts.UDP_HEADER_SIZE +
PacketConsts.BTH_HEADER_SIZE)
udp_header = u.gen_udp_header(packet_len=msg_size + PacketConsts.BTH_HEADER_SIZE,
dst_port=PacketConsts.ROCE_PORT)
bth_header = u.gen_bth_header()
return mac_header + ip_header + udp_header + bth_header
@staticmethod
def create_roce_bth_params():
from tests.mlx5_prm_structs import FlowTableEntryMatchParam
roce_mask = FlowTableEntryMatchParam()
roce_mask.misc_parameters.bth_opcode = 0xff
roce_mask.misc_parameters.bth_dst_qp = 0xffffff
roce_mask.misc_parameters.bth_a = 0x1
roce_value = FlowTableEntryMatchParam()
roce_value.misc_parameters.bth_opcode = PacketConsts.BTH_OPCODE
roce_value.misc_parameters.bth_dst_qp = PacketConsts.BTH_DST_QP
roce_value.misc_parameters.bth_a = PacketConsts.BTH_A
mask_param = Mlx5FlowMatchParameters(len(roce_mask), roce_mask)
value_param = Mlx5FlowMatchParameters(len(roce_value), roce_value)
return mask_param, value_param
def create_empty_matcher_go_to_tbl(self, src_tbl, dst_tbl):
"""
Create rule that forward all packets (by empty matcher) from src_tbl to
dst_tbl.
"""
from tests.mlx5_prm_structs import FlowTableEntryMatchParam
empty_param = Mlx5FlowMatchParameters(len(FlowTableEntryMatchParam()),
FlowTableEntryMatchParam())
matcher = DrMatcher(src_tbl, 0, u.MatchCriteriaEnable.NONE, empty_param)
go_to_tbl_action = DrActionDestTable(dst_tbl)
self.rules.append(DrRule(matcher, empty_param, [go_to_tbl_action]))
return go_to_tbl_action
@requires_eswitch_on
@skip_unsupported
def test_dest_vport(self):
self.dest_port()
@requires_eswitch_on
@skip_unsupported
def test_dest_ib_port(self):
self.dest_port(False)
@skip_unsupported
def add_qp_rule_and_send_pkts(self, root_only=False):
"""
:param root_only : If True, rules are created only on root table
"""
self.create_players(Mlx5DrResources)
self.qp_action = DrActionQp(self.server.qp)
smac_value = struct.pack('!6s', bytes.fromhex(PacketConsts.SRC_MAC.replace(':', '')))
self.create_rx_recv_rules(smac_value, [self.qp_action], root_only=root_only)
u.raw_traffic(self.client, self.server, self.iters)
def test_tbl_qp_rule(self):
"""
Creates RX domain, SW table with matcher on source mac. Creates QP action
and a rule with this action on the matcher.
"""
self.add_qp_rule_and_send_pkts()
def test_root_tbl_qp_rule(self):
"""
Creates RX domain, SW table with matcher on source mac. Creates QP action
and a rule with this action on the matcher.
"""
self.add_qp_rule_and_send_pkts(root_only=True)
@skip_unsupported
def modify_tx_smac_and_send_pkts(self, root_only=False):
"""
Create a rule on TX domain that modifies smac of matched packet and
sends it to the wire.
:param root_only : If True, rules are created only on root table
"""
from tests.mlx5_prm_structs import SetActionIn
self.create_players(Mlx5DrResources)
self.domain_tx = DrDomain(self.client.ctx, dve.MLX5DV_DR_DOMAIN_TYPE_NIC_TX)
root_table_tx = DrTable(self.domain_tx, 0)
if not root_only:
non_root_table_tx = DrTable(self.domain_tx, 1)
self.move_action = self.create_empty_matcher_go_to_tbl(root_table_tx, non_root_table_tx)
table = root_table_tx if root_only else non_root_table_tx
smac_mask = bytes([0xff] * 6)
mask_param = Mlx5FlowMatchParameters(len(smac_mask), smac_mask)
matcher_tx = DrMatcher(table, 0, u.MatchCriteriaEnable.OUTER, mask_param)
smac_value = struct.pack('!6s', bytes.fromhex(PacketConsts.SRC_MAC.replace(':', '')))
smac_value += bytes(2)
value_param = Mlx5FlowMatchParameters(len(smac_value), smac_value)
action1 = SetActionIn(action_type=SET_ACTION, field=ModifyFields.OUT_SMAC_47_16,
data=0x88888888, length=ModifyFieldsLen.MAC_47_16)
action2 = SetActionIn(action_type=SET_ACTION, field=ModifyFields.OUT_SMAC_15_0,
data=0x8888, length=ModifyFieldsLen.MAC_15_0)
flags = dve.MLX5DV_DR_ACTION_FLAGS_ROOT_LEVEL if root_only else 0
self.modify_action_tx = DrActionModify(self.domain_tx, flags, [action1, action2])
self.rules.append(DrRule(matcher_tx, value_param, [self.modify_action_tx]))
src_mac = struct.pack('!6s', bytes.fromhex("88:88:88:88:88:88".replace(':', '')))
self.qp_action = DrActionQp(self.server.qp)
self.create_rx_recv_rules(src_mac, [self.qp_action], root_only=root_only)
exp_packet = u.gen_packet(self.client.msg_size, src_mac=src_mac)
u.raw_traffic(self.client, self.server, self.iters, expected_packet=exp_packet)
@skip_unsupported
def test_tbl_modify_header_rule(self):
"""
Creates TX domain, SW table with matcher on source mac and modify the smac.
Then creates RX domain and rule that forwards packets with the new smac
to server QP. Perform traffic that do this flow.
"""
self.modify_tx_smac_and_send_pkts()
@skip_unsupported
def test_root_tbl_modify_header_rule(self):
"""
Creates TX domain, root table with matcher on source mac and modify the smac.
Then creates RX domain and rule that forwards packets with the new smac
to server QP. Perform traffic that do this flow.
"""
self.modify_tx_smac_and_send_pkts(root_only=True)
@skip_unsupported
def test_metadata_modify_action_set_copy_match(self):
"""
Verify modify header with set and copy actions.
TX and RX:
- Root table:
Match empty (hit all):
Rule: prio 0 - val empty. Action: Go TO Table 1
- Table 1:
Match empty (hit all):
Rule: prio 0 - val empty. Action: Modify Header (set reg_c_0 to REG_C_DATA)
+ Go TO Table 2
- Table 2:
Match empty (hit all):
Rule: prio 0 - val empty. Action: Modify Header (copy reg_c_0 to reg_c_1)
+ Go To Table 3
TX:
- Table 3:
Match reg_c_0 and reg_c_1:
Rule: prio 0 - val REG_C_DATA. Action: Counter
RX:
- Table 3:
Match reg_c_0 and reg_c_1:
Rule: prio 0 - val REG_C_DATA. Action: Go To QP
"""
from tests.mlx5_prm_structs import FlowTableEntryMatchParam, FlowTableEntryMatchSetMisc2, \
SetActionIn, CopyActionIn
self.create_players(Mlx5DrResources)
match_param = FlowTableEntryMatchParam()
empty_param = Mlx5FlowMatchParameters(len(match_param), match_param)
mask_metadata = FlowTableEntryMatchParam(misc_parameters_2=
FlowTableEntryMatchSetMisc2(metadata_reg_c_0=0xffff, metadata_reg_c_1=0xffff))
mask_param = Mlx5FlowMatchParameters(len(match_param), mask_metadata)
value_metadata = FlowTableEntryMatchParam(misc_parameters_2=
FlowTableEntryMatchSetMisc2(metadata_reg_c_0=REG_C_DATA,
metadata_reg_c_1=REG_C_DATA))
value_param = Mlx5FlowMatchParameters(len(match_param), value_metadata)
self.client.domain = DrDomain(self.client.ctx, dve.MLX5DV_DR_DOMAIN_TYPE_NIC_TX)
self.server.domain = DrDomain(self.server.ctx, dve.MLX5DV_DR_DOMAIN_TYPE_NIC_RX)
for player in [self.client, self.server]:
player.tables = []
player.matchers = []
for i in range(4):
player.tables.append(DrTable(player.domain, i))
for i in range(2):
player.matchers.append(DrMatcher(player.tables[i + 1], 0,
u.MatchCriteriaEnable.NONE, empty_param))
player.matchers.append(DrMatcher(player.tables[3], 0,
u.MatchCriteriaEnable.MISC_2, mask_param))
player.go_to_tbl1_action = self.create_empty_matcher_go_to_tbl(player.tables[0],
player.tables[1])
set_reg = SetActionIn(field=ModifyFields.META_DATA_REG_C_0,
length=ModifyFieldsLen.META_DATA_REG_C, data=REG_C_DATA)
player.modify_action_set = DrActionModify(player.domain, 0, [set_reg])
player.go_to_tbl2_action = DrActionDestTable(player.tables[2])
self.rules.append(DrRule(player.matchers[0], empty_param, [player.modify_action_set,
player.go_to_tbl2_action]))
copy_reg = CopyActionIn(src_field=ModifyFields.META_DATA_REG_C_0,
length=ModifyFields.META_DATA_REG_C_0,
dst_field=ModifyFields.META_DATA_REG_C_1)
player.modify_action_copy = DrActionModify(player.domain, 0, [copy_reg])
player.go_to_tbl3_action = DrActionDestTable(player.tables[3])
self.rules.append(DrRule(player.matchers[1], empty_param, [player.modify_action_copy,
player.go_to_tbl3_action]))
counter, flow_counter_id = self.create_counter(self.client.ctx)
counter_action = DrActionFlowCounter(counter)
self.rules.append(DrRule(self.client.matchers[2], value_param, [counter_action]))
qp_action = DrActionQp(self.server.qp)
self.rules.append(DrRule(self.server.matchers[2], value_param, [qp_action]))
u.raw_traffic(self.client, self.server, self.iters)
sent_packets = self.query_counter_packets(counter, flow_counter_id)
self.assertEqual(sent_packets, self.iters, 'Counter of metadata missed some sent packets')
@skip_unsupported
def add_counter_action_and_send_pkts(self, root_only=False):
"""
:param root_only : If True, rules are created only on root table
"""
self.create_players(Mlx5DrResources)
counter, flow_counter_id = self.create_counter(self.server.ctx)
self.server_counter_action = DrActionFlowCounter(counter)
smac_value = struct.pack('!6s', bytes.fromhex(PacketConsts.SRC_MAC.replace(':', '')))
self.qp_action = DrActionQp(self.server.qp)
self.create_rx_recv_rules(smac_value, [self.qp_action, self.server_counter_action],
root_only=root_only)
u.raw_traffic(self.client, self.server, self.iters)
recv_packets = self.query_counter_packets(counter, flow_counter_id)
self.assertEqual(recv_packets, self.iters, 'Counter missed some recv packets')
@skip_unsupported
def test_root_tbl_counter_action(self):
"""
Create flow counter object, on root table attach it to a rule using counter action
and perform traffic that hit this rule. Verify that the packets counter
increased.
"""
self.add_counter_action_and_send_pkts(root_only=True)
@skip_unsupported
def test_tbl_counter_action(self):
"""
Create flow counter object, on non-root table attach it to a rule using counter action
and perform traffic that hit this rule. Verify that the packets counter
increased.
"""
self.add_counter_action_and_send_pkts()
@skip_unsupported
def test_prevent_duplicate_rule(self):
"""
Creates RX domain, sets duplicate rule to be not allowed on that domain,
try creating duplicate rule. Fail if creation succeeded.
"""
from tests.mlx5_prm_structs import FlowTableEntryMatchParam
self.server = Mlx5DrResources(**self.dev_info)
domain_rx = DrDomain(self.server.ctx, dve.MLX5DV_DR_DOMAIN_TYPE_NIC_RX)
domain_rx.allow_duplicate_rules(False)
table = DrTable(domain_rx, 1)
empty_param = Mlx5FlowMatchParameters(len(FlowTableEntryMatchParam()),
FlowTableEntryMatchParam())
matcher = DrMatcher(table, 0, u.MatchCriteriaEnable.NONE, empty_param)
self.qp_action = DrActionQp(self.server.qp)
self.drop_action = DrActionDrop()
self.rules.append(DrRule(matcher, empty_param, [self.qp_action]))
with self.assertRaises(PyverbsRDMAError) as ex:
self.rules.append(DrRule(matcher, empty_param, [self.drop_action]))
self.assertEqual(ex.exception.error_code, errno.EEXIST)
def _drop_action(self, root_only=False):
self.create_players(Mlx5DrResources)
# Initiate the sender side
domain_tx = DrDomain(self.client.ctx, dve.MLX5DV_DR_DOMAIN_TYPE_NIC_TX)
tx_root_table = DrTable(domain_tx, 0)
smac_value = struct.pack('!6s', bytes.fromhex(PacketConsts.SRC_MAC.replace(':', '')))
if not root_only:
tx_non_root_table = DrTable(domain_tx, 1)
tx_dest_table_action = self.fwd_packets_to_table(tx_root_table, tx_non_root_table)
smac_value += bytes(2)
tx_test_table = tx_root_table if root_only else tx_non_root_table
mask_param = Mlx5FlowMatchParameters(len(bytes([0xff] * 6)), bytes([0xff] * 6))
matcher = DrMatcher(tx_test_table, 0, u.MatchCriteriaEnable.OUTER, mask_param)
value_param = Mlx5FlowMatchParameters(len(smac_value), smac_value)
self.tx_drop_action = DrActionDrop()
self.rules.append(DrRule(matcher, value_param, [self.tx_drop_action]))
# Initiate the receiver side
domain_rx = DrDomain(self.server.ctx, dve.MLX5DV_DR_DOMAIN_TYPE_NIC_RX)
rx_root_table = DrTable(domain_rx, 0)
if not root_only:
rx_non_root_table = DrTable(domain_rx, 1)
rx_dest_table_action = self.fwd_packets_to_table(rx_root_table, rx_non_root_table)
rx_test_table = rx_root_table if root_only else rx_non_root_table
# Create server counter.
counter, flow_counter_id = self.create_counter(self.server.ctx)
self.server_counter_action = DrActionFlowCounter(counter)
mask_param, value_param = self.create_dest_mac_params()
matcher = DrMatcher(rx_test_table, 0, u.MatchCriteriaEnable.OUTER, mask_param)
self.rx_drop_action = DrActionDrop()
self.rules.append(DrRule(matcher, value_param, [self.server_counter_action,
self.rx_drop_action]))
# Send packets with two different smacs and expect half to be dropped.
src_mac_drop = struct.pack('!6s', bytes.fromhex(PacketConsts.SRC_MAC.replace(':', '')))
src_mac_non_drop = struct.pack('!6s', bytes.fromhex("88:88:88:88:88:88".replace(':', '')))
self.send_client_raw_packets(int(self.iters / 2), src_mac=src_mac_drop)
recv_packets = self.query_counter_packets(counter, flow_counter_id)
self.assertEqual(recv_packets, 0, 'Drop action did not drop the TX packets')
self.send_client_raw_packets(int(self.iters / 2), src_mac=src_mac_non_drop)
recv_packets = self.query_counter_packets(counter, flow_counter_id)
self.assertEqual(recv_packets, int(self.iters/2),
'Drop action dropped TX packets that not matched the rule')
@skip_unsupported
def test_root_tbl_drop_action(self):
"""
Create root drop actions on TX and RX. Verify using counter on the server RX that
only packets which miss the drop rule arrived to the server RX.
"""
self._drop_action(root_only=True)
@skip_unsupported
def test_tbl_drop_action(self):
"""
Create non-root drop actions on TX and RX. Verify using counter on the server RX that
only packets that which the drop rule arrived to the server RX.
"""
self._drop_action()
@skip_unsupported
def add_qp_tag_rule_and_send_pkts(self, root_only=False):
"""
Creates RX domain, table with matcher on source mac. Creates QP action
and tag action. Creates a rule with those actions on the matcher.
Verifies traffic and tag.
:param root_only : If True, rules are created only on root table
"""
self.wc_flags = e.IBV_WC_EX_WITH_FLOW_TAG
self.create_players(Mlx5DrResources, wc_flags=e.IBV_WC_EX_WITH_FLOW_TAG)
qp_action = DrActionQp(self.server.qp)
tag = 0x123
tag_action = DrActionTag(tag)
smac_value = struct.pack('!6s', bytes.fromhex(PacketConsts.SRC_MAC.replace(':', '')))
self.create_rx_recv_rules(smac_value, [tag_action, qp_action], root_only=root_only)
self.domain_rx.sync()
u.raw_traffic(self.client, self.server, self.iters)
# Verify tag
self.assertEqual(self.server.cq.read_flow_tag(), tag, 'Wrong tag value')
@skip_unsupported
def test_tbl_qp_tag_rule(self):
"""
Creates RX domain, non-root table with matcher on source mac. Creates QP action
and tag action. Creates a rule with those actions on the matcher.
Verifies traffic and tag.
"""
self.add_qp_tag_rule_and_send_pkts()
@skip_unsupported
def test_root_tbl_qp_tag_rule(self):
"""
Creates RX domain, root table with matcher on source mac. Creates QP action
and tag action. Creates a rule with those actions on the matcher.
Verifies traffic and tag.
"""
self.add_qp_tag_rule_and_send_pkts(root_only=True)
@skip_unsupported
def test_set_matcher_layout(self):
"""
Creates a non root matcher and sets its size. Creates a rule on that
matcher and increases the matcher size. Verifies the rule.
"""
log_matcher_size = 5
self.create_players(Mlx5DrResources)
self.qp_action = DrActionQp(self.server.qp)
smac_value = struct.pack('!6s', bytes.fromhex(PacketConsts.SRC_MAC.replace(':', '')))
self.create_rx_recv_rules(smac_value, [self.qp_action], log_matcher_size)
self.matcher.set_layout(log_matcher_size + 1)
u.raw_traffic(self.client, self.server, self.iters)
self.matcher.set_layout(flags=dve.MLX5DV_DR_MATCHER_LAYOUT_RESIZABLE)
u.raw_traffic(self.client, self.server, self.iters)
@skip_unsupported
def test_push_vlan(self):
"""
Creates RX domain, root table with matcher on source mac. Create a rule to forward
all traffic to the non-root table. Creates QP action and push VLAN action.
Creates a rule with those actions on the matcher.
Verifies traffic and packet with specified VLAN.
"""
self.client = Mlx5DrResources(**self.dev_info)
vlan_hdr = struct.pack('!HH', PacketConsts.VLAN_TPID, (PacketConsts.VLAN_PRIO << 13) +
(PacketConsts.VLAN_CFI << 12) + PacketConsts.VLAN_ID)
self.server = Mlx5DrResources(msg_size=self.client.msg_size + PacketConsts.VLAN_HEADER_SIZE,
**self.dev_info)
self.domain_tx = DrDomain(self.client.ctx, dve.MLX5DV_DR_DOMAIN_TYPE_NIC_TX)
smac_value = struct.pack('!6s', bytes.fromhex(PacketConsts.SRC_MAC.replace(':', '')))
push_action = DrActionPushVLan(self.domain_tx, struct.unpack('I', vlan_hdr)[0])
self.tx_table, self.tx_dest_act = self.create_rx_recv_rules(smac_value, [push_action],
domain=self.domain_tx)
self.domain_rx = DrDomain(self.server.ctx, dve.MLX5DV_DR_DOMAIN_TYPE_NIC_RX)
qp_action = DrActionQp(self.server.qp)
self.create_rx_recv_rules(smac_value, [qp_action], domain=self.domain_rx)
exp_packet = u.gen_packet(self.client.msg_size + PacketConsts.VLAN_HEADER_SIZE,
with_vlan=True)
u.raw_traffic(self.client, self.server, self.iters, expected_packet=exp_packet)
@skip_unsupported
def test_pop_vlan(self):
"""
Creates RX domain, root table with matcher on source mac. Create a rule to forward
all traffic to the non-root table. Creates QP action and pop VLAN action.
Creates a rule with those actions on the matcher.
Verifies packets received without VLAN header.
"""
self.server = Mlx5DrResources(**self.dev_info)
self.client = Mlx5DrResources(**self.dev_info)
exp_packet = u.gen_packet(self.server.msg_size - PacketConsts.VLAN_HEADER_SIZE)
qp_action = DrActionQp(self.server.qp)
pop_action = DrActionPopVLan()
smac_value = struct.pack('!6s', bytes.fromhex(PacketConsts.SRC_MAC.replace(':', '')))
self.create_rx_recv_rules(smac_value, [pop_action, qp_action])
u.raw_traffic(self.client, self.server, self.iters, with_vlan=True, expected_packet=exp_packet)
@skip_unsupported
def dest_array(self, root_only=False):
"""
Creates RX domain, root table with matcher on source mac. Create a rule
to forward all traffic to the non-root table. On this table add a rule
with multi dest array action which include destination QP actions and
next FT (also with QP action).
Validate on all QPs the received packets.
:param root_only : If True, rules are created only on root table
"""
max_actions = 8
self.client = Mlx5DrResources(qp_count=max_actions, **self.dev_info)
self.server = Mlx5DrResources(qp_count=max_actions, **self.dev_info)
self.domain_rx = DrDomain(self.server.ctx, dve.MLX5DV_DR_DOMAIN_TYPE_NIC_RX)
actions = []
dest_attrs = []
for qp in self.server.qps[:-1]:
qp_action = DrActionQp(qp)
actions.append(qp_action)
dest_attrs.append(DrActionDestAttr(dve.MLX5DV_DR_ACTION_DEST, qp_action))
ft_action = DrTable(self.domain_rx, 0xff)
last_table_action = DrActionDestTable(ft_action)
smac_mask = bytes([0xff] * 6) + bytes(2)
mask_param = Mlx5FlowMatchParameters(len(smac_mask), smac_mask)
last_matcher = DrMatcher(ft_action, 1, u.MatchCriteriaEnable.OUTER, mask_param)
dest_attrs.append(DrActionDestAttr(dve.MLX5DV_DR_ACTION_DEST, last_table_action))
last_qp_action = DrActionQp(self.server.qps[max_actions - 1])
smac_value = struct.pack('!6s2s', bytes.fromhex(PacketConsts.SRC_MAC.replace(':', '')),
bytes(2))
value_param = Mlx5FlowMatchParameters(len(smac_value), smac_value)
self.rules.append(DrRule(last_matcher, value_param, [last_qp_action]))
multi_dest_a = DrActionDestArray(self.domain_rx, len(dest_attrs), dest_attrs)
smac_value = struct.pack('!6s', bytes.fromhex(PacketConsts.SRC_MAC.replace(':', '')))
self.create_rx_recv_rules(smac_value, [multi_dest_a], domain=self.domain_rx,
root_only=root_only)
u.raw_traffic(self.client, self.server, self.iters)
@skip_unsupported
def test_root_dest_array(self):
"""
Creates RX domain, root table with matcher on source mac.on root table
add a rule with multi dest array action which include destination QP actions and
next FT (also with QP action).
Validate on all QPs the received packets.
"""
self.dest_array(root_only=True)
@skip_unsupported
def test_dest_array(self):
"""
Creates RX domain, non-root table with matcher on source mac. Create a rule
to forward all traffic to the non-root table. On this table add a rule
with multi dest array action which include destination QP actions and
next FT (also with QP action).
Validate on all QPs the received packets.
"""
self.dest_array()
@skip_unsupported
def test_tx_def_miss_action(self):
"""
Create TX root table and forward all traffic to next SW steering table,
create two matchers with different priorities, one with default miss
action (on TX it's go to wire action) and one with drop action, default
miss action should occur before the drop action hence packets
should reach server side which has RX rule with QP action.
"""
self.create_players(Mlx5DrResources)
self.domain_tx = DrDomain(self.client.ctx, dve.MLX5DV_DR_DOMAIN_TYPE_NIC_TX)
tx_def_miss = DrActionDefMiss()
tx_drop_action = DrActionDrop()
smac_value = struct.pack('!6s', bytes.fromhex(PacketConsts.SRC_MAC.replace(':', '')))
self.tx_table, self.tx_dest_act = self.create_rx_recv_rules(smac_value, [tx_def_miss],
domain=self.domain_tx)
qp_action = DrActionQp(self.server.qp)
self.create_rx_recv_rules(smac_value, [qp_action])
smac_mask = bytes([0xff] * 6) + bytes(2)
mask_param = Mlx5FlowMatchParameters(len(smac_mask), smac_mask)
matcher_tx2 = DrMatcher(self.tx_table, 2, u.MatchCriteriaEnable.OUTER, mask_param)
smac_value += bytes(2)
value_param = Mlx5FlowMatchParameters(len(smac_value), smac_value)
self.rules.append(DrRule(matcher_tx2, value_param, [tx_drop_action]))
u.raw_traffic(self.client, self.server, self.iters)
@skip_unsupported
def add_dest_tir_action_send_pkts(self, root_only=False):
"""
:param root_only: If True, rules are created only on root table
"""
self.client = Mlx5DrTirResources(**self.dev_info)
self.server = Mlx5DrTirResources(**self.dev_info, server=True)
tir_action = DrActionDestTir(self.server.tir)
smac_value = struct.pack('!6s', bytes.fromhex(PacketConsts.SRC_MAC.replace(':', '')))
self.create_rx_recv_rules(smac_value, [tir_action], root_only=root_only)
u.raw_traffic(self.client, self.server, self.iters)
@skip_unsupported
def test_dest_tir(self):
self.add_dest_tir_action_send_pkts()
@skip_unsupported
def test_root_dest_tir(self):
self.add_dest_tir_action_send_pkts(root_only=True)
def packet_reformat_actions(self, outer, root_only=False, l2_ref_type=True):
"""
Creates packet reformat actions on TX (encap) and on RX (decap).
:param outer: The outer header to encap.
:param root_only: If True create actions only on root tables
:param l2_ref_type: If False use L2 to L3 tunneling reformat
"""
smac_mask = bytes([0xff] * 6) + bytes(2)
mask_param = Mlx5FlowMatchParameters(len(smac_mask), smac_mask)
smac_value = struct.pack('!6s', bytes.fromhex(PacketConsts.SRC_MAC.replace(':', '')))
value_param = Mlx5FlowMatchParameters(len(smac_value), smac_value)
reformat_flag = dve.MLX5DV_DR_ACTION_FLAGS_ROOT_LEVEL if root_only else 0
# TX
domain_tx = DrDomain(self.client.ctx, dve.MLX5DV_DR_DOMAIN_TYPE_NIC_TX)
tx_root_table = DrTable(domain_tx, 0)
tx_root_matcher = DrMatcher(tx_root_table, 0, u.MatchCriteriaEnable.OUTER, mask_param)
if not root_only:
tx_table = DrTable(domain_tx, 1)
tx_matcher = DrMatcher(tx_table, 1, u.MatchCriteriaEnable.OUTER, mask_param)
dest_table_action_tx = DrActionDestTable(tx_table)
self.rules.append(DrRule(tx_root_matcher, value_param, [dest_table_action_tx]))
reformat_matcher = tx_root_matcher if root_only else tx_matcher
# Create encap action
tx_reformat_type = dve.MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TO_L2_TUNNEL_ if \
l2_ref_type else dve.MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TO_L3_TUNNEL_
reformat_action_tx = DrActionPacketReformat(domain=domain_tx, flags=reformat_flag,
reformat_type=tx_reformat_type, data=outer)
smac_value_tx = smac_value + bytes(2)
value_param = Mlx5FlowMatchParameters(len(smac_value_tx), smac_value_tx)
self.rules.append(DrRule(reformat_matcher, value_param, [reformat_action_tx]))
# RX
domain_rx = DrDomain(self.server.ctx, dve.MLX5DV_DR_DOMAIN_TYPE_NIC_RX)
# Create decap action
data = struct.pack('!6s6s',
bytes.fromhex(PacketConsts.DST_MAC.replace(':', '')),
bytes.fromhex(PacketConsts.SRC_MAC.replace(':', '')))
data += PacketConsts.ETHER_TYPE_IPV4.to_bytes(2, 'big')
rx_reformat_type = dve.MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TUNNEL_TO_L2_ if \
l2_ref_type else dve.MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L3_TUNNEL_TO_L2_
reformat_action_rx = DrActionPacketReformat(domain=domain_rx, flags=reformat_flag,
reformat_type=rx_reformat_type,
data=None if l2_ref_type else data)
qp_action = DrActionQp(self.server.qp)
if root_only:
rx_root_table = DrTable(domain_rx, 0)
rx_root_matcher = DrMatcher(rx_root_table, 0, u.MatchCriteriaEnable.OUTER, mask_param)
self.rules.append(DrRule(rx_root_matcher, value_param, [reformat_action_rx, qp_action]))
else:
self.create_rx_recv_rules(smac_value, [reformat_action_rx, qp_action], domain=domain_rx)
# Send traffic and validate packet
u.raw_traffic(self.client, self.server, self.iters)
@skip_unsupported
def test_flow_sampler(self):
"""
Flow sampler has a default table (all the packets are forwarded to it)
and a sampler actions (for sampled packets)
The default table has counter action.
For NIC RX table sampler actions are counter and TIR.
Verify that default counter counts all the packets.
Verify that sampled packets counter and receiving them on QP(from TIR)
"""
self.client = Mlx5DrTirResources(**self.dev_info)
self.server = Mlx5DrTirResources(**self.dev_info, server=True)
self.iters = 1000
# Create tir & counter actions for sampler attr
tir_action = DrActionDestTir(self.server.tir)
counter_1, flow_counter_id_1 = self.create_counter(self.server.ctx)
self.server_counter_action = DrActionFlowCounter(counter_1)
# Create resources
smac_value = struct.pack('!6s', bytes.fromhex(PacketConsts.SRC_MAC.replace(':', '')))
rx_domain = DrDomain(self.server.ctx, dve.MLX5DV_DR_DOMAIN_TYPE_NIC_RX)
default_tbl = DrTable(rx_domain, 2)
# Create sampler action on NIC RX table
sample_actions = [self.server_counter_action, tir_action]
sampler_attr = DrFlowSamplerAttr(sample_ratio=SAMPLE_RATIO, default_next_table=default_tbl,
sample_actions=sample_actions)
sampler_action = DrActionFlowSample(sampler_attr)
tbl, _ = self.create_rx_recv_rules(smac_value, [sampler_action], domain=rx_domain)
smac_mask = bytes([0xff] * 6) + bytes(2)
mask_param = Mlx5FlowMatchParameters(len(smac_mask), smac_mask)
self.default_matcher = DrMatcher(default_tbl, 1, u.MatchCriteriaEnable.OUTER, mask_param)
# Size of the matcher value should be modulo 4
smac_value += bytes(2)
value_param = Mlx5FlowMatchParameters(len(smac_value), smac_value)
# Create Counter action on default table
counter_2, flow_counter_id_2 = self.create_counter(self.server.ctx)
self.server_counter_action_2 = DrActionFlowCounter(counter_2)
self.rules.append(DrRule(self.default_matcher, value_param, [self.server_counter_action_2]))
# Send traffic and validate packet
u.sampler_traffic(self.client, self.server, self.iters)
recv_packets = self.query_counter_packets(counter=counter_1,
flow_counter_id=flow_counter_id_1)
exp_packets = math.ceil((self.iters / SAMPLE_RATIO))
max_exp_packets = int(exp_packets * (1 + SAMPLER_ERROR_MARGIN))
min_exp_packets = int(exp_packets * (1 - SAMPLER_ERROR_MARGIN))
is_sampled_packets_in_error_margin = min_exp_packets <= recv_packets <= max_exp_packets
self.assertTrue(is_sampled_packets_in_error_margin,
f'Expected sampled packets {exp_packets} is more than '
f'{SAMPLER_ERROR_MARGIN * 100}% \ndiffernt from actual {recv_packets}')
recv_packets_from_default_tbl = \
self.query_counter_packets(counter=counter_2, flow_counter_id=flow_counter_id_2)
self.assertEqual(recv_packets_from_default_tbl, self.iters,
'Counter on default table missed some recv packets')
@skip_unsupported
def geneve_match_rx(self, root_only=False):
"""
Creates matcher on RX to match on Geneve related fields with counter and qp action,
sends packets and verifies the matcher.
:param root_only: If True, rules are created only on root table
"""
self.create_players(Mlx5DrResources)
geneve_mask, geneve_val = self.create_geneve_params()
domain_rx = DrDomain(self.server.ctx, dve.MLX5DV_DR_DOMAIN_TYPE_NIC_RX)