Debian Codegraph @

Documentation/pyverbs.md

raw
1 # Pyverbs
2
3 Pyverbs provides a Python API over rdma-core, the Linux userspace C API for
4 the RDMA stack.
5
6 ## Goals
7
8 1. Provide easier access to RDMA: RDMA has a steep learning curve as is and
9 the C interface requires the user to initialize multiple structs before
10 having usable objects. Pyverbs attempts to remove much of this overhead and
11 provide a smoother user experience.
12 2. Improve our code by providing a test suite for rdma-core. This means that
13 new features will be tested before merge, and it also means that users and
14 distros will have tests for new and existing features, as well as the means
15 to create them quickly.
16 3. Stay up-to-date with rdma-core - cover new features during development and
17 provide a test / unit-test alongside the feature.
18
19 ## Limitations
20
21 Python handles memory for users. As a result, memory is allocated by Pyverbs
22 when needed (e.g. user buffer for memory region). The memory will be accessible
23 to the users, but not allocated or freed by them.
24
25 ## Usage Examples
26 Note that all examples use a hard-coded device name ('mlx5_0').
27 ##### Open an IB device
28
29 Import the device module and open a device by name:
30
31 ```python
32 import pyverbs.device as d
33 ctx = d.Context(name='mlx5_0')
34 ```
35
36 'ctx' is Pyverbs' equivalent to rdma-core's ibv_context. At this point, the IB
37 device is already open and ready to use.
38
39 ##### Query a device
40 ```python
41 import pyverbs.device as d
42 ctx = d.Context(name='mlx5_0')
43 attr = ctx.query_device()
44 print(attr)
45 FW version : 16.24.0185
46 Node guid : 9803:9b03:0000:e4c6
47 Sys image GUID : 9803:9b03:0000:e4c6
48 Max MR size : 0xffffffffffffffff
49 Page size cap : 0xfffffffffffff000
50 Vendor ID : 0x2c9
51 Vendor part ID : 4119
52 HW version : 0
53 Max QP : 262144
54 Max QP WR : 32768
55 Device cap flags : 3983678518
56 Max SGE : 30
57 Max SGE RD : 30
58 MAX CQ : 16777216
59 Max CQE : 4194303
60 Max MR : 16777216
61 Max PD : 16777216
62 Max QP RD atom : 16
63 Max EE RD atom : 0
64 Max res RD atom : 4194304
65 Max QP init RD atom : 16
66 Max EE init RD atom : 0
67 Atomic caps : 1
68 Max EE : 0
69 Max RDD : 0
70 Max MW : 16777216
71 Max raw IPv6 QPs : 0
72 Max raw ethy QP : 0
73 Max mcast group : 2097152
74 Max mcast QP attach : 240
75 Max AH : 2147483647
76 Max FMR : 0
77 Max map per FMR : 2147483647
78 Max SRQ : 8388608
79 Max SRQ WR : 32767
80 Max SRQ SGE : 31
81 Max PKeys : 128
82 local CA ack delay : 16
83 Phys port count : 1
84 ```
85
86 'attr' is Pyverbs' equivalent to ibv_device_attr. Pyverbs will provide it to
87 the user upon completion of the call to ibv_query_device.
88
89 ##### Query GID
90
91 ```python
92 import pyverbs.device as d
93 ctx = d.Context(name='mlx5_0')
94 gid = ctx.query_gid(port_num=1, index=3)
95 print(gid)
96 0000:0000:0000:0000:0000:ffff:0b87:3c08
97 ```
98
99 'gid' is Pyverbs' equivalent to ibv_gid, provided to the user by Pyverbs.
100
101 ##### Query port
102 The following code snippet provides an example of pyverbs' equivalent of
103 querying a port. Context's query_port() command wraps ibv_query_port().
104 The example below queries the first port of the device.
105 ```python
106 import pyverbs.device as d
107 ctx=d.Context(name='mlx5_0')
108 port_attr = ctx.query_port(1)
109 print(port_attr)
110 Port state : Active (4)
111 Max MTU : 4096 (5)
112 Active MTU : 1024 (3)
113 SM lid : 0
114 Port lid : 0
115 lmc : 0x0
116 Link layer : Ethernet
117 Max message size : 0x40000000
118 Port cap flags : IBV_PORT_CM_SUP IBV_PORT_IP_BASED_GIDS
119 Port cap flags 2 :
120 max VL num : 0
121 Bad Pkey counter : 0
122 Qkey violations counter : 0
123 Gid table len : 256
124 Pkey table len : 1
125 SM sl : 0
126 Subnet timeout : 0
127 Init type reply : 0
128 Active width : 4X (2)
129 Ative speed : 25.0 Gbps (32)
130 Phys state : Link up (5)
131 Flags : 1
132 ```
133
134 ##### Extended query device
135 The example below shows how to open a device using pyverbs and query the
136 extended device's attributes.
137 Context's query_device_ex() command wraps ibv_query_device_ex().
138 ```python
139 import pyverbs.device as d
140
141 ctx = d.Context(name='mlx5_0')
142 attr = ctx.query_device_ex()
143 attr.max_dm_size
144 131072
145 attr.rss_caps.max_rwq_indirection_table_size
146 2048
147 ```
148
149 #### Create RDMA objects
150 ##### PD
151 The following example shows how to open a device and use its context to create
152 a PD.
153 ```python
154 import pyverbs.device as d
155 from pyverbs.pd import PD
156
157 with d.Context(name='mlx5_0') as ctx:
158 pd = PD(ctx)
159 ```
160 ##### MR
161 The example below shows how to create a MR using pyverbs. Similar to C, a
162 device must be opened prior to creation and a PD has to be allocated.
163 ```python
164 import pyverbs.device as d
165 from pyverbs.pd import PD
166 from pyverbs.mr import MR
167 from pyverbs.libibverbs_enums import ibv_access_flags
168
169 with d.Context(name='mlx5_0') as ctx:
170 with PD(ctx) as pd:
171 mr_len = 1000
172 flags = ibv_access_flags.IBV_ACCESS_LOCAL_WRITE
173 mr = MR(pd, mr_len, flags)
174 ```
175 ##### Memory window
176 The following example shows the equivalent of creating a type 1 memory window.
177 It includes opening a device and allocating the necessary PD.
178 The user should unbind or close the memory window before being able to
179 deregister an MR that the MW is bound to.
180 ```python
181 import pyverbs.device as d
182 from pyverbs.pd import PD
183 from pyverbs.mr import MW
184 from pyverbs.libibverbs_enums import ibv_mw_type
185
186 with d.Context(name='mlx5_0') as ctx:
187 with PD(ctx) as pd:
188 mw = MW(pd, ibv_mw_type.IBV_MW_TYPE_1)
189 ```
190 ##### Device memory
191 The following snippet shows how to allocate a DM - a direct memory object,
192 using the device's memory.
193 ```python
194 import random
195
196 from pyverbs.device import DM, AllocDmAttr
197 import pyverbs.device as d
198
199 with d.Context(name='mlx5_0') as ctx:
200 attr = ctx.query_device_ex()
201 if attr.max_dm_size != 0:
202 dm_len = random.randint(4, attr.max_dm_size)
203 dm_attrs = AllocDmAttr(dm_len)
204 dm = DM(ctx, dm_attrs)
205 ```
206
207 ##### DM MR
208 The example below shows how to open a DMMR - device memory MR, using the
209 device's own memory rather than a user-allocated buffer.
210 ```python
211 import random
212
213 from pyverbs.device import DM, AllocDmAttr
214 from pyverbs.mr import DMMR
215 import pyverbs.device as d
216 from pyverbs.pd import PD
217 from pyverbs.libibverbs_enums import ibv_access_flags
218
219 with d.Context(name='mlx5_0') as ctx:
220 attr = ctx.query_device_ex()
221 if attr.max_dm_size != 0:
222 dm_len = random.randint(4, attr.max_dm_size)
223 dm_attrs = AllocDmAttr(dm_len)
224 dm_mr_len = random.randint(4, dm_len)
225 with DM(ctx, dm_attrs) as dm:
226 with PD(ctx) as pd:
227 dm_mr = DMMR(pd, dm_mr_len, ibv_access_flags.IBV_ACCESS_ZERO_BASED, dm=dm,
228 offset=0)
229 ```
230
231 ##### CQ
232 The following snippets show how to create CQs using pyverbs. Pyverbs supports
233 both CQ and extended CQ (CQEX).
234 As in C, a completion queue can be created with or without a completion
235 channel, the snippets show that.
236 CQ's 3rd parameter is cq_context, a user-defined context. We're using None in
237 our snippets.
238 ```python
239 import random
240
241 from pyverbs.cq import CompChannel, CQ
242 import pyverbs.device as d
243
244 with d.Context(name='mlx5_0') as ctx:
245 num_cqes = random.randint(0, 200) # Just arbitrary values. Max value can be
246 # found in device attributes
247 comp_vector = 0 # An arbitrary value. comp_vector is limited by the
248 # context's num_comp_vectors
249 if random.choice([True, False]):
250 with CompChannel(ctx) as cc:
251 cq = CQ(ctx, num_cqes, None, cc, comp_vector)
252 else:
253 cq = CQ(ctx, num_cqes, None, None, comp_vector)
254 print(cq)
255 CQ
256 Handle : 0
257 CQEs : 63
258 ```
259
260 ```python
261 import random
262
263 from pyverbs.cq import CqInitAttrEx, CQEX
264 import pyverbs.device as d
265 from pyverbs.libibverbs_enums import ibv_create_cq_wc_flags
266
267 with d.Context(name='mlx5_0') as ctx:
268 num_cqe = random.randint(0, 200)
269 wc_flags = ibv_create_cq_wc_flags.IBV_WC_EX_WITH_CVLAN
270 comp_mask = 0 # Not using flags in this example
271 # completion channel is not used in this example
272 attrs = CqInitAttrEx(cqe=num_cqe, wc_flags=wc_flags, comp_mask=comp_mask,
273 flags=0)
274 print(attrs)
275 cq_ex = CQEX(ctx, attrs)
276 print(cq_ex)
277 Number of CQEs : 10
278 WC flags : IBV_WC_EX_WITH_CVLAN
279 comp mask : 0
280 flags : 0
281
282 Extended CQ:
283 Handle : 0
284 CQEs : 15
285 ```
286
287 ##### Addressing related objects
288 The following code demonstrates creation of GlobalRoute, AHAttr and AH objects.
289 The example creates a global AH so it can also run on RoCE without
290 modifications.
291 ```python
292
293 from pyverbs.addr import GlobalRoute, AHAttr, AH
294 import pyverbs.device as d
295 from pyverbs.pd import PD
296
297 with d.Context(name='mlx5_0') as ctx:
298 port_number = 1
299 gid_index = 0 # GID index 0 always exists and valid
300 gid = ctx.query_gid(port_number, gid_index)
301 gr = GlobalRoute(dgid=gid, sgid_index=gid_index)
302 ah_attr = AHAttr(gr=gr, is_global=1, port_num=port_number)
303 print(ah_attr)
304 with PD(ctx) as pd:
305 ah = AH(pd, attr=ah_attr)
306 DGID : fe80:0000:0000:0000:9a03:9bff:fe00:e4bf
307 flow label : 0
308 sgid index : 0
309 hop limit : 1
310 traffic class : 0
311 ```
312
313 ##### QP
314 The following snippets will demonstrate creation of a QP and a simple post_send
315 operation. For more complex examples, please see pyverbs/examples section.
316 ```python
317 from pyverbs.qp import QPCap, QPInitAttr, QPAttr, QP
318 from pyverbs.addr import GlobalRoute
319 from pyverbs.addr import AH, AHAttr
320 import pyverbs.device as d
321 from pyverbs.libibverbs_enums import ibv_qp_type
322 from pyverbs.pd import PD
323 from pyverbs.cq import CQ
324 import pyverbs.wr as pwr
325
326
327 ctx = d.Context(name='mlx5_0')
328 pd = PD(ctx)
329 cq = CQ(ctx, 100, None, None, 0)
330 cap = QPCap(100, 10, 1, 1, 0)
331 qia = QPInitAttr(cap=cap, qp_type = ibv_qp_type.IBV_QPT_UD, scq=cq, rcq=cq)
332 # A UD QP will be in RTS if a QPAttr object is provided
333 udqp = QP(pd, qia, QPAttr())
334 port_num = 1
335 gid_index = 3 # Hard-coded for RoCE v2 interface
336 gid = ctx.query_gid(port_num, gid_index)
337 gr = GlobalRoute(dgid=gid, sgid_index=gid_index)
338 ah_attr = AHAttr(gr=gr, is_global=1, port_num=port_num)
339 ah=AH(pd, ah_attr)
340 wr = pwr.SendWR()
341 wr.set_wr_ud(ah, 0x1101, 0) # in real life, use real values
342 udqp.post_send(wr)
343 ```
344 ###### Extended QP
345 An extended QP exposes a new set of QP send operations to the user -
346 extensibility for new send opcodes, vendor specific send opcodes and even vendor
347 specific QP types.
348 Pyverbs now exposes the needed interface to create such a QP.
349 Note that the IBV_QP_INIT_ATTR_SEND_OPS_FLAGS in the `comp_mask` is mandatory
350 when using the extended QP's new post send mechanism.
351 ```python
352 from pyverbs.qp import QPCap, QPInitAttrEx, QPAttr, QPEx
353 import pyverbs.device as d
354 from pyverbs.libibverbs_enums import ibv_qp_type, ibv_qp_init_attr_mask
355 from pyverbs.pd import PD
356 from pyverbs.cq import CQ
357
358
359 ctx = d.Context(name='mlx5_0')
360 pd = PD(ctx)
361 cq = CQ(ctx, 100)
362 cap = QPCap(100, 10, 1, 1, 0)
363 qia = QPInitAttrEx(qp_type=ibv_qp_type.IBV_QPT_UD, scq=cq, rcq=cq, cap=cap, pd=pd,
364 comp_mask=ibv_qp_init_attr_mask.IBV_QP_INIT_ATTR_SEND_OPS_FLAGS| \
365 ibv_qp_init_attr_mask.IBV_QP_INIT_ATTR_PD)
366 qp = QPEx(ctx, qia)
367 ```
368
369 ##### XRCD
370 The following code demonstrates creation of an XRCD object.
371 ```python
372 from pyverbs.xrcd import XRCD, XRCDInitAttr
373 import pyverbs.device as d
374 from pyverbs.libibverbs_enums import ibv_xrcd_init_attr_mask
375 import stat
376 import os
377
378
379 ctx = d.Context(name='ibp0s8f0')
380 xrcd_fd = os.open('/tmp/xrcd', os.O_RDONLY | os.O_CREAT,
381 stat.S_IRUSR | stat.S_IRGRP)
382 init = XRCDInitAttr(ibv_xrcd_init_attr_mask.IBV_XRCD_INIT_ATTR_FD | ibv_xrcd_init_attr_mask.IBV_XRCD_INIT_ATTR_OFLAGS,
383 os.O_CREAT, xrcd_fd)
384 xrcd = XRCD(ctx, init)
385 ```
386
387 ##### SRQ
388 The following code snippet will demonstrate creation of an XRC SRQ object.
389 For more complex examples, please see pyverbs/tests/test_odp.
390 ```python
391 from pyverbs.xrcd import XRCD, XRCDInitAttr
392 from pyverbs.srq import SRQ, SrqInitAttrEx
393 import pyverbs.device as d
394 from pyverbs.libibverbs_enums import ibv_xrcd_init_attr_mask, ibv_srq_type, ibv_srq_init_attr_mask
395 from pyverbs.cq import CQ
396 from pyverbs.pd import PD
397 import stat
398 import os
399
400
401 ctx = d.Context(name='ibp0s8f0')
402 pd = PD(ctx)
403 cq = CQ(ctx, 100, None, None, 0)
404 xrcd_fd = os.open('/tmp/xrcd', os.O_RDONLY | os.O_CREAT,
405 stat.S_IRUSR | stat.S_IRGRP)
406 init = XRCDInitAttr(ibv_xrcd_init_attr_mask.IBV_XRCD_INIT_ATTR_FD | ibv_xrcd_init_attr_mask.IBV_XRCD_INIT_ATTR_OFLAGS,
407 os.O_CREAT, xrcd_fd)
408 xrcd = XRCD(ctx, init)
409
410 srq_attr = SrqInitAttrEx(max_wr=10)
411 srq_attr.srq_type = ibv_srq_type.IBV_SRQT_XRC
412 srq_attr.pd = pd
413 srq_attr.xrcd = xrcd
414 srq_attr.cq = cq
415 srq_attr.comp_mask = ibv_srq_init_attr_mask.IBV_SRQ_INIT_ATTR_TYPE | ibv_srq_init_attr_mask.IBV_SRQ_INIT_ATTR_PD | \
416 ibv_srq_init_attr_mask.IBV_SRQ_INIT_ATTR_CQ | ibv_srq_init_attr_mask.IBV_SRQ_INIT_ATTR_XRCD
417 srq = SRQ(ctx, srq_attr)
418 ```
419
420 ##### Open an mlx5 provider
421 A provider is essentially a Context with driver-specific extra features. As
422 such, it inherits from Context. In legacy flow Context iterates over the IB
423 devices and opens the one matches the name given by the user (name= argument).
424 When provider attributes are also given (attr=), the Context will assign the
425 relevant ib_device to its device member, so that the provider will be able to
426 open the device in its specific way as demonstated below:
427
428 ```python
429 import pyverbs.providers.mlx5.mlx5dv as m
430 from pyverbs.pd import PD
431 attr = m.Mlx5DVContextAttr() # Default values are fine
432 ctx = m.Mlx5Context(attr=attr, name='rocep0s8f0')
433 # The provider context can be used as a regular Context, e.g.:
434 pd = PD(ctx) # Success
435 ```
436
437 ##### Query an mlx5 provider
438 After opening an mlx5 provider, users can use the device-specific query for
439 non-legacy attributes. The following snippet demonstrates how to do that.
440 ```python
441 import pyverbs.providers.mlx5.mlx5dv as m
442 ctx = m.Mlx5Context(attr=m.Mlx5DVContextAttr(), name='ibp0s8f0')
443 mlx5_attrs = ctx.query_mlx5_device()
444 print(mlx5_attrs)
445 Version : 0
446 Flags : CQE v1, Support CQE 128B compression, Support CQE 128B padding, Support packet based credit mode (in RC QP)
447 comp mask : CQE compression, SW parsing, Striding RQ, Tunnel offloads, Dynamic BF regs, Clock info update, Flow action flags
448 CQE compression caps:
449 max num : 64
450 supported formats : with hash, with RX checksum CSUM, with stride index
451 SW parsing caps:
452 SW parsing offloads :
453 supported QP types :
454 Striding RQ caps:
455 min single stride log num of bytes: 6
456 max single stride log num of bytes: 13
457 min single wqe log num of strides: 9
458 max single wqe log num of strides: 16
459 supported QP types : Raw Packet
460 Tunnel offloads caps:
461 Max dynamic BF registers: 1024
462 Max clock info update [nsec]: 1099511
463 Flow action flags : 0
464 ```
465
466 ##### Create an mlx5 QP
467 Using an Mlx5Context object, one can create either a legacy QP (creation
468 process is the same) or an mlx5 QP. An mlx5 QP is a QP by inheritance but its
469 constructor receives a keyword argument named `dv_init_attr`. If the user
470 provides it, the QP will be created using `mlx5dv_create_qp` rather than
471 `ibv_create_qp_ex`. The following snippet demonstrates how to create both a DC
472 (dynamically connected) QP and a Raw Packet QP which uses mlx5-specific
473 capabilities, unavailable using the legacy interface. Currently, pyverbs
474 supports only creation of a DCI. DCT support will be added in one of the
475 following PRs.
476 ```python
477 from pyverbs.providers.mlx5.mlx5dv import Mlx5Context, Mlx5DVContextAttr
478 from pyverbs.providers.mlx5.mlx5dv import Mlx5DVQPInitAttr, Mlx5QP
479 from pyverbs.providers.mlx5.mlx5_enums import mlx5dv_qp_init_attr_mask, mlx5dv_dc_type, mlx5dv_qp_create_flags
480 from pyverbs.qp import QPInitAttrEx, QPCap
481 from pyverbs.libibverbs_enums import ibv_qp_type, ibv_qp_init_attr_mask, ibv_qp_type
482 from pyverbs.cq import CQ
483 from pyverbs.pd import PD
484
485 with Mlx5Context(name='rocep0s8f0', attr=Mlx5DVContextAttr()) as ctx:
486 with PD(ctx) as pd:
487 with CQ(ctx, 100) as cq:
488 cap = QPCap(100, 0, 1, 0)
489 # Create a DC QP of type DCI
490 qia = QPInitAttrEx(cap=cap, pd=pd, scq=cq, qp_type=ibv_qp_type.IBV_QPT_DRIVER,
491 comp_mask=ibv_qp_init_attr_mask.IBV_QP_INIT_ATTR_PD, rcq=cq)
492 attr = Mlx5DVQPInitAttr(comp_mask=mlx5dv_qp_init_attr_mask.MLX5DV_QP_INIT_ATTR_MASK_DC)
493 attr.dc_type = mlx5dv_dc_type.MLX5DV_DCTYPE_DCI
494
495 dci = Mlx5QP(ctx, qia, dv_init_attr=attr)
496
497 # Create a Raw Packet QP using mlx5-specific capabilities
498 qia.qp_type = ibv_qp_type.IBV_QPT_RAW_PACKET
499 attr.comp_mask = mlx5dv_qp_init_attr_mask.MLX5DV_QP_INIT_ATTR_MASK_QP_CREATE_FLAGS
500 attr.create_flags = mlx5dv_qp_create_flags.MLX5DV_QP_CREATE_ALLOW_SCATTER_TO_CQE | \
501 mlx5dv_qp_create_flags.MLX5DV_QP_CREATE_TIR_ALLOW_SELF_LOOPBACK_UC | \
502 mlx5dv_qp_create_flags.MLX5DV_QP_CREATE_TUNNEL_OFFLOADS
503 qp = Mlx5QP(ctx, qia, dv_init_attr=attr)
504 ```
505
506 ##### Create an mlx5 CQ
507 Mlx5Context also allows users to create an mlx5 specific CQ. The Mlx5CQ inherits
508 from CQEX, but its constructor receives 3 parameters instead of 2. The 3rd
509 parameter is a keyword argument named `dv_init_attr`. If provided by the user,
510 the CQ will be created using `mlx5dv_create_cq`.
511 The following snippet shows this simple creation process.
512 ```python
513 from pyverbs.providers.mlx5.mlx5dv import Mlx5Context, Mlx5DVContextAttr
514 from pyverbs.providers.mlx5.mlx5dv import Mlx5DVCQInitAttr, Mlx5CQ
515 from pyverbs.providers.mlx5.mlx5_enums import mlx5dv_cq_init_attr_mask, mlx5dv_cqe_comp_res_format
516 from pyverbs.cq import CqInitAttrEx
517
518 with Mlx5Context(name='rocep0s8f0', attr=Mlx5DVContextAttr()) as ctx:
519 cqia = CqInitAttrEx()
520 mlx5_cqia = Mlx5DVCQInitAttr(comp_mask=mlx5dv_cq_init_attr_mask.MLX5DV_CQ_INIT_ATTR_MASK_COMPRESSED_CQE,
521 cqe_comp_res_format=mlx5dv_cqe_comp_res_format.MLX5DV_CQE_RES_FORMAT_CSUM)
522 cq = Mlx5CQ(ctx, cqia, dv_init_attr=mlx5_cqia)
523 ```
524
525 ##### CMID
526 The following code snippet will demonstrate creation of a CMID object, which
527 represents rdma_cm_id C struct, and establish connection between two peers.
528 Currently only synchronous control path is supported (rdma_create_ep).
529 For more complex examples, please see tests/test_rdmacm.
530 ```python
531 from pyverbs.qp import QPInitAttr, QPCap
532 from pyverbs.cmid import CMID, AddrInfo
533 from pyverbs.librdmacm_enums import rdma_port_space, RAI_PASSIVE
534
535 cap = QPCap(max_recv_wr=1)
536 qp_init_attr = QPInitAttr(cap=cap)
537 addr = '11.137.14.124'
538 port = '7471'
539
540 # Passive side
541
542 sai = AddrInfo(src=addr, src_service=port, port_space=rdma_port_space.RDMA_PS_TCP, flags=RAI_PASSIVE)
543 sid = CMID(creator=sai, qp_init_attr=qp_init_attr)
544 sid.listen() # listen for incoming connection requests
545 new_id = sid.get_request() # check if there are any connection requests
546 new_id.accept() # new_id is connected to remote peer and ready to communicate
547
548 # Active side
549
550 cai = AddrInfo(src=addr, dst=addr, dst_service=port, port_space=rdma_port_space.RDMA_PS_TCP)
551 cid = CMID(creator=cai, qp_init_attr=qp_init_attr)
552 cid.connect() # send connection request to passive addr
553 ```
554
555 ##### ParentDomain
556 The following code demonstrates the creation of Parent Domain object.
557 In this example, a simple Python allocator is defined. It uses MemAlloc class to
558 allocate aligned memory using a C style aligned_alloc.
559 ```python
560 from pyverbs.pd import PD, ParentDomainInitAttr, ParentDomain, \
561 ParentDomainContext
562 from pyverbs.device import Context
563 import pyverbs.mem_alloc as mem
564
565
566 def alloc_p_func(pd, context, size, alignment, resource_type):
567 p = mem.posix_memalign(size, alignment)
568 return p
569
570
571 def free_p_func(pd, context, ptr, resource_type):
572 mem.free(ptr)
573
574
575 ctx = Context(name='rocep0s8f0')
576 pd = PD(ctx)
577 pd_ctx = ParentDomainContext(pd, alloc_p_func, free_p_func)
578 pd_attr = ParentDomainInitAttr(pd=pd, pd_context=pd_ctx)
579 parent_domain = ParentDomain(ctx, attr=pd_attr)
580 ```
581
582 ##### MLX5 VAR
583 The following code snippet demonstrates how to allocate an mlx5dv_var then using
584 it for memory address mapping, then freeing the VAR.
585 ```python
586 from pyverbs.providers.mlx5.mlx5dv import Mlx5VAR
587 from pyverbs.device import Context
588 import mmap
589
590 ctx = Context(name='rocep0s8f0')
591 var = Mlx5VAR(ctx)
592 var_map = mmap.mmap(fileno=ctx.cmd_fd, length=var.length, offset=var.mmap_off)
593 # There is no munmap method in mmap Python module, but by closing the mmap
594 # instance the memory is unmapped.
595 var_map.close()
596 var.close()
597 ```
598
599 ##### MLX5 PP
600 Packet Pacing (PP) entry can be used for some device commands over the DEVX
601 interface. It allows a rate-limited flow configuration on SQs.
602 The following code snippet demonstrates how to allocate an mlx5dv_pp with rate
603 limit value of 5, then frees the entry.
604 ```python
605 from pyverbs.providers.mlx5.mlx5dv import Mlx5Context, Mlx5DVContextAttr, Mlx5PP
606 from pyverbs.providers.mlx5.mlx5_enums import mlx5dv_context_attr_flags
607
608 # The device must be opened as DEVX context
609 mlx5dv_attr = Mlx5DVContextAttr(mlx5dv_context_attr_flags.MLX5DV_CONTEXT_FLAGS_DEVX)
610 ctx = Mlx5Context(attr=mlx5dv_attr, name='rocep0s8f0')
611 rate_limit_inbox = (5).to_bytes(length=4, byteorder='big', signed=True)
612 pp = Mlx5PP(ctx, rate_limit_inbox)
613 pp.close()
614 ```
615
616 ##### MLX5 UAR
617 User Access Region (UAR) is part of PCI address space that is mapped for direct
618 access to the HCA from the CPU.
619 The UAR is needed for some device commands over the DevX interface.
620 The following code snippet demonstrates how to allocate and free an
621 mlx5dv_devx_uar.
622 ```python
623 from pyverbs.providers.mlx5.mlx5dv import Mlx5UAR
624 from pyverbs.device import Context
625
626 ctx = Context(name='rocep0s8f0')
627 uar = Mlx5UAR(ctx)
628 uar.close()
629 ```
630
631 ##### Import device, PD and MR
632 Importing a device, PD and MR enables processes to share their context and then
633 share PDs and MRs that is associated with.
634 A process creates a device and then uses some of the Linux systems calls to dup
635 its 'cmd_fd' member which lets other process to obtain ownership.
636 Once other process obtains the 'cmd_fd' it can import the device, then PD(s) and
637 MR(s) to share these objects.
638 Like in C, Pyverbs users are responsible for unimporting the imported objects
639 (which will also close the Pyverbs instance in our case) after they finish using
640 them, and they have to sync between the different processes in order to
641 coordinate the closure of the objects.
642 Unlike in C, closing the underlying objects is currently supported only via the
643 "original" object (meaning only by the process that creates them) and not via
644 the imported object. This limitation is made because currently there's no
645 reference or relation between different Pyverbs objects in different processes.
646 But it's doable and might be added in the future.
647 Here is a demonstration of importing a device, PD and MR in one process.
648 ```python
649 from pyverbs.device import Context
650 from pyverbs.pd import PD
651 from pyverbs.mr import MR
652 from pyverbs.libibverbs_enums import ibv_access_flags
653 import os
654
655 ctx = Context(name='ibp0s8f0')
656 pd = PD(ctx)
657 mr = MR(pd, 100, ibv_access_flags.IBV_ACCESS_LOCAL_WRITE)
658 cmd_fd_dup = os.dup(ctx.cmd_fd)
659 imported_ctx = Context(cmd_fd=cmd_fd_dup)
660 imported_pd = PD(imported_ctx, handle=pd.handle)
661 imported_mr = MR(imported_pd, handle=mr.handle)
662 # MRs can be created as usual on the imported PD
663 secondary_mr = MR(imported_pd, 100, ibv_access_flags.IBV_ACCESS_REMOTE_READ)
664 # Must manually unimport the imported objects (which close the object and frees
665 # other resources that use them) before closing the "original" objects.
666 # This prevents unexpected behaviours caused by the GC.
667 imported_mr.unimport()
668 imported_pd.unimport()
669 ```
670
671
672 ##### Flow Steering
673 Flow steering rules define packet matching done by the hardware.
674 A spec describes packet matching on a specific layer (L2, L3 etc.).
675 A flow is a collection of specs.
676 A user QP can attach to flows in order to receive specific packets.
677
678 ###### Flow and FlowAttr
679
680 ```python
681 from pyverbs.qp import QPCap, QPInitAttr, QPAttr, QP
682 from pyverbs.flow import FlowAttr, Flow
683 from pyverbs.spec import EthSpec
684 import pyverbs.device as d
685 from pyverbs.libibverbs_enums import ibv_qp_type
686 from pyverbs.pd import PD
687 from pyverbs.cq import CQ
688
689
690 ctx = d.Context(name='rocep0s8f0')
691 pd = PD(ctx)
692 cq = CQ(ctx, 100, None, None, 0)
693 cap = QPCap(100, 10, 1, 1, 0)
694 qia = QPInitAttr(cap=cap, qp_type = ibv_qp_type.IBV_QPT_UD, scq=cq, rcq=cq)
695 qp = QP(pd, qia, QPAttr())
696
697 # Create Eth spec
698 eth_spec = EthSpec(ether_type=0x800, dst_mac="01:50:56:19:20:a7")
699 eth_spec.src_mac = "24:8a:07:a5:28:c8"
700 eth_spec.src_mac_mask = "ff:ff:ff:ff:ff:ff"
701
702 # Create Flow
703 flow_attr = FlowAttr(num_of_specs=1)
704 flow_attr.specs.append(eth_spec)
705 flow = Flow(qp, flow_attr)
706 ```
707
708 ###### Specs
709 Each spec holds a specific network layer parameters for matching. To enforce
710 the match, the user sets a mask for each parameter. If the bit is set in the
711 mask, the corresponding bit in the value should be matched.
712 Packets coming from the wire are matched against the flow specification. If a
713 match is found, the associated flow actions are executed on the packet. In
714 ingress flows, the QP parameter is treated as another action of scattering the
715 packet to the respected QP.
716
717
718 ###### Notes
719 * When creating specs mask will be set to FF's to all the given values (unless
720 provided by the user). When editing a spec mask should be specified explicitly.
721 * If a field is not provided its value and mask will be set to zeros.
722 * Hardware only supports full / empty masks.
723 * Ethernet, IPv4, TCP/UDP, IPv6 and ESP specs can be inner (IBV_FLOW_SPEC_INNER),
724 but set to outer by default.
725
726
727 ###### Ethernet spec
728 Example of creating and editing Ethernet spec
729 ```python
730 from pyverbs.spec import EthSpec
731 eth_spec = EthSpec(src_mac="ab:cd:ef:ab:cd:ef", vlan_tag=0x123, is_inner=1)
732 eth_spec.dst_mac = "de:de:de:00:de:de"
733 eth_spec.dst_mac_mask = "ff:ff:ff:ff:ff:ff"
734 eth_spec.ether_type = 0x321
735 eth_spec.ether_type_mask = 0xffff
736 # Resulting spec
737 print(f'{eth_spec}')
738 ```
739 Below is the output when printing the spec.
740
741 Spec type : IBV_FLOW_SPEC_INNER IBV_FLOW_SPEC_ETH
742 Size : 40
743 Src mac : ab:cd:ef:ab:cd:ef mask: ff:ff:ff:ff:ff:ff
744 Dst mac : de:de:de:00:de:de mask: ff:ff:ff:ff:ff:ff
745 Ether type : 8451 mask: 65535
746 Vlan tag : 8961 mask: 65535
747
748
749 ##### MLX5 DevX Objects
750 A DevX object represents some underlay firmware object, the input command to
751 create it is some raw data given by the user application which should match the
752 device specification.
753 Upon successful creation, the output buffer includes the raw data from the device
754 according to its specification and is stored in the Mlx5DevxObj instance. This
755 data can be used as part of related firmware commands to this object.
756 In addition to creation, the user can query/modify and destroy the object.
757
758 Although weakrefs and DevX objects closure are added and handled by
759 Pyverbs, the users must manually close these objects when finished, and
760 should not let them be handled by the GC, or by closing the Mlx5Context directly,
761 since there's no guarantee that the DevX objects are closed in the correct order,
762 because Mlx5DevxObj is a general class that can be any of the device's available
763 objects.
764 But Pyverbs does guarantee to close DevX UARs and UMEMs in order, and after
765 closing the other DevX objects.
766
767 The following code snippet shows how to allocate and destroy a PD object over DevX.
768 ```python
769 from pyverbs.providers.mlx5.mlx5dv import Mlx5Context, Mlx5DVContextAttr, Mlx5DevxObj
770 import pyverbs.providers.mlx5.mlx5_enums as dve
771 import struct
772
773 attr = Mlx5DVContextAttr(dve.MLX5DV_CONTEXT_FLAGS_DEVX)
774 ctx = Mlx5Context(attr, 'rocep8s0f0')
775 MLX5_CMD_OP_ALLOC_PD = 0x800
776 MLX5_CMD_OP_ALLOC_PD_OUTLEN = 0x10
777 cmd_in = struct.pack('!H14s', MLX5_CMD_OP_ALLOC_PD, bytes(0))
778 pd = Mlx5DevxObj(ctx, cmd_in, MLX5_CMD_OP_ALLOC_PD_OUTLEN)
779 pd.close()
780 ```