Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright 2018 6WIND S.A.
3 : : * Copyright 2018 Mellanox Technologies, Ltd
4 : : */
5 : :
6 : : #include <errno.h>
7 : : #include <linux/if_link.h>
8 : : #include <linux/rtnetlink.h>
9 : : #include <linux/genetlink.h>
10 : : #include <net/if.h>
11 : : #include <rdma/rdma_netlink.h>
12 : : #include <stdbool.h>
13 : : #include <stdint.h>
14 : : #include <stdlib.h>
15 : : #include <stdalign.h>
16 : : #include <string.h>
17 : : #include <sys/socket.h>
18 : : #include <unistd.h>
19 : :
20 : : #include <rte_errno.h>
21 : :
22 : : #include "mlx5_nl.h"
23 : : #include "../mlx5_common_log.h"
24 : : #include "mlx5_malloc.h"
25 : : #ifdef HAVE_DEVLINK
26 : : #include <linux/devlink.h>
27 : : #endif
28 : :
29 : :
30 : : /* Size of the buffer to receive kernel messages */
31 : : #define MLX5_NL_BUF_SIZE (32 * 1024)
32 : : /* Send buffer size for the Netlink socket */
33 : : #define MLX5_SEND_BUF_SIZE 32768
34 : : /* Receive buffer size for the Netlink socket */
35 : : #define MLX5_RECV_BUF_SIZE 32768
36 : : /* Maximal physical port name length. */
37 : : #define MLX5_PHYS_PORT_NAME_MAX 128
38 : :
39 : : /** Parameters of VLAN devices created by driver. */
40 : : #define MLX5_VMWA_VLAN_DEVICE_PFX "evmlx"
41 : : /*
42 : : * Define NDA_RTA as defined in iproute2 sources.
43 : : *
44 : : * see in iproute2 sources file include/libnetlink.h
45 : : */
46 : : #ifndef MLX5_NDA_RTA
47 : : #define MLX5_NDA_RTA(r) \
48 : : ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
49 : : #endif
50 : : /*
51 : : * Define NLMSG_TAIL as defined in iproute2 sources.
52 : : *
53 : : * see in iproute2 sources file include/libnetlink.h
54 : : */
55 : : #ifndef NLMSG_TAIL
56 : : #define NLMSG_TAIL(nmsg) \
57 : : ((struct rtattr *)(((char *)(nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
58 : : #endif
59 : : /*
60 : : * The following definitions are normally found in rdma/rdma_netlink.h,
61 : : * however they are so recent that most systems do not expose them yet.
62 : : */
63 : : #ifndef HAVE_RDMA_NL_NLDEV
64 : : #define RDMA_NL_NLDEV 5
65 : : #endif
66 : : #ifndef HAVE_RDMA_NLDEV_CMD_GET
67 : : #define RDMA_NLDEV_CMD_GET 1
68 : : #endif
69 : : #ifndef HAVE_RDMA_NLDEV_CMD_PORT_GET
70 : : #define RDMA_NLDEV_CMD_PORT_GET 5
71 : : #endif
72 : : #ifndef HAVE_RDMA_NLDEV_ATTR_DEV_INDEX
73 : : #define RDMA_NLDEV_ATTR_DEV_INDEX 1
74 : : #endif
75 : : #ifndef HAVE_RDMA_NLDEV_ATTR_DEV_NAME
76 : : #define RDMA_NLDEV_ATTR_DEV_NAME 2
77 : : #endif
78 : : #ifndef HAVE_RDMA_NLDEV_ATTR_PORT_INDEX
79 : : #define RDMA_NLDEV_ATTR_PORT_INDEX 3
80 : : #endif
81 : : #ifndef HAVE_RDMA_NLDEV_ATTR_PORT_STATE
82 : : #define RDMA_NLDEV_ATTR_PORT_STATE 12
83 : : #endif
84 : : #ifndef HAVE_RDMA_NLDEV_ATTR_NDEV_INDEX
85 : : #define RDMA_NLDEV_ATTR_NDEV_INDEX 50
86 : : #endif
87 : : #ifndef HAVE_RDMA_NLDEV_ATTR_EVENT_TYPE
88 : : #define RDMA_NLDEV_ATTR_EVENT_TYPE 102
89 : : #define RDMA_NETDEV_ATTACH_EVENT 2
90 : : #define RDMA_NETDEV_DETACH_EVENT 3
91 : : #endif
92 : : #ifndef HAVE_RDMA_NLDEV_SYS_ATTR_MONITOR_MODE
93 : : #define RDMA_NLDEV_SYS_ATTR_MONITOR_MODE 103
94 : : #endif
95 : : #ifndef HAVE_RDMA_NLDEV_CMD_MONITOR
96 : : #define RDMA_NLDEV_CMD_MONITOR 28
97 : : #endif
98 : : #ifndef HAVE_RDMA_NLDEV_CMD_SYS_GET
99 : : #define RDMA_NLDEV_CMD_SYS_GET 6
100 : : #endif
101 : : #ifndef HAVE_RDMA_NL_GROUP_NOTIFY
102 : : #define RDMA_NL_GROUP_NOTIFY 4
103 : : #endif
104 : : #define RDMA_NL_GROUP_NOTIFICATION (1 << (RDMA_NL_GROUP_NOTIFY - 1))
105 : :
106 : : /* These are normally found in linux/if_link.h. */
107 : : #ifndef HAVE_IFLA_NUM_VF
108 : : #define IFLA_NUM_VF 21
109 : : #endif
110 : : #ifndef HAVE_IFLA_EXT_MASK
111 : : #define IFLA_EXT_MASK 29
112 : : #endif
113 : : #ifndef HAVE_IFLA_PHYS_SWITCH_ID
114 : : #define IFLA_PHYS_SWITCH_ID 36
115 : : #endif
116 : : #ifndef HAVE_IFLA_PHYS_PORT_NAME
117 : : #define IFLA_PHYS_PORT_NAME 38
118 : : #endif
119 : :
120 : : /*
121 : : * Some Devlink defines may be missed in old kernel versions,
122 : : * adjust used defines.
123 : : */
124 : : #ifndef DEVLINK_GENL_NAME
125 : : #define DEVLINK_GENL_NAME "devlink"
126 : : #endif
127 : : #ifndef DEVLINK_GENL_VERSION
128 : : #define DEVLINK_GENL_VERSION 1
129 : : #endif
130 : : #ifndef DEVLINK_ATTR_BUS_NAME
131 : : #define DEVLINK_ATTR_BUS_NAME 1
132 : : #endif
133 : : #ifndef DEVLINK_ATTR_DEV_NAME
134 : : #define DEVLINK_ATTR_DEV_NAME 2
135 : : #endif
136 : : #ifndef DEVLINK_ATTR_PARAM
137 : : #define DEVLINK_ATTR_PARAM 80
138 : : #endif
139 : : #ifndef DEVLINK_ATTR_PARAM_NAME
140 : : #define DEVLINK_ATTR_PARAM_NAME 81
141 : : #endif
142 : : #ifndef DEVLINK_ATTR_PARAM_TYPE
143 : : #define DEVLINK_ATTR_PARAM_TYPE 83
144 : : #endif
145 : : #ifndef DEVLINK_ATTR_PARAM_VALUES_LIST
146 : : #define DEVLINK_ATTR_PARAM_VALUES_LIST 84
147 : : #endif
148 : : #ifndef DEVLINK_ATTR_PARAM_VALUE
149 : : #define DEVLINK_ATTR_PARAM_VALUE 85
150 : : #endif
151 : : #ifndef DEVLINK_ATTR_PARAM_VALUE_DATA
152 : : #define DEVLINK_ATTR_PARAM_VALUE_DATA 86
153 : : #endif
154 : : #ifndef DEVLINK_ATTR_PARAM_VALUE_CMODE
155 : : #define DEVLINK_ATTR_PARAM_VALUE_CMODE 87
156 : : #endif
157 : : #ifndef DEVLINK_PARAM_CMODE_DRIVERINIT
158 : : #define DEVLINK_PARAM_CMODE_DRIVERINIT 1
159 : : #endif
160 : : #ifndef DEVLINK_CMD_RELOAD
161 : : #define DEVLINK_CMD_RELOAD 37
162 : : #endif
163 : : #ifndef DEVLINK_CMD_PARAM_GET
164 : : #define DEVLINK_CMD_PARAM_GET 38
165 : : #endif
166 : : #ifndef DEVLINK_CMD_PARAM_SET
167 : : #define DEVLINK_CMD_PARAM_SET 39
168 : : #endif
169 : : #ifndef NLA_FLAG
170 : : #define NLA_FLAG 6
171 : : #endif
172 : :
173 : : /* Add/remove MAC address through Netlink */
174 : : struct mlx5_nl_mac_addr {
175 : : struct rte_ether_addr (*mac)[];
176 : : /**< MAC address handled by the device. */
177 : : int mac_n; /**< Number of addresses in the array. */
178 : : };
179 : :
180 : : RTE_ATOMIC(uint32_t) atomic_sn;
181 : :
182 : : /* Generate Netlink sequence number. */
183 : : #define MLX5_NL_SN_GENERATE (rte_atomic_fetch_add_explicit(&atomic_sn, 1, \
184 : : rte_memory_order_relaxed) + 1)
185 : :
186 : : /**
187 : : * Opens a Netlink socket.
188 : : *
189 : : * @param protocol
190 : : * Netlink protocol (e.g. NETLINK_ROUTE, NETLINK_RDMA).
191 : : * @param groups
192 : : * Groups to listen (e.g. RTMGRP_LINK), can be 0.
193 : : *
194 : : * @return
195 : : * A file descriptor on success, a negative errno value otherwise and
196 : : * rte_errno is set.
197 : : */
198 : : int
199 : 0 : mlx5_nl_init(int protocol, int groups)
200 : : {
201 : : int fd;
202 : : int buf_size;
203 : : socklen_t opt_size;
204 : 0 : struct sockaddr_nl local = {
205 : : .nl_family = AF_NETLINK,
206 : : .nl_groups = groups,
207 : : };
208 : : int ret;
209 : :
210 : 0 : fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, protocol);
211 [ # # ]: 0 : if (fd == -1) {
212 : 0 : rte_errno = errno;
213 : 0 : return -rte_errno;
214 : : }
215 : 0 : opt_size = sizeof(buf_size);
216 : 0 : ret = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &buf_size, &opt_size);
217 [ # # ]: 0 : if (ret == -1) {
218 : 0 : rte_errno = errno;
219 : 0 : goto error;
220 : : }
221 : 0 : DRV_LOG(DEBUG, "Netlink socket send buffer: %d", buf_size);
222 [ # # ]: 0 : if (buf_size < MLX5_SEND_BUF_SIZE) {
223 : 0 : ret = setsockopt(fd, SOL_SOCKET, SO_SNDBUF,
224 : : &buf_size, sizeof(buf_size));
225 [ # # ]: 0 : if (ret == -1) {
226 : 0 : rte_errno = errno;
227 : 0 : goto error;
228 : : }
229 : : }
230 : 0 : opt_size = sizeof(buf_size);
231 : 0 : ret = getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &buf_size, &opt_size);
232 [ # # ]: 0 : if (ret == -1) {
233 : 0 : rte_errno = errno;
234 : 0 : goto error;
235 : : }
236 : 0 : DRV_LOG(DEBUG, "Netlink socket recv buffer: %d", buf_size);
237 [ # # ]: 0 : if (buf_size < MLX5_RECV_BUF_SIZE) {
238 : 0 : ret = setsockopt(fd, SOL_SOCKET, SO_RCVBUF,
239 : : &buf_size, sizeof(buf_size));
240 [ # # ]: 0 : if (ret == -1) {
241 : 0 : rte_errno = errno;
242 : 0 : goto error;
243 : : }
244 : : }
245 : 0 : ret = bind(fd, (struct sockaddr *)&local, sizeof(local));
246 [ # # ]: 0 : if (ret == -1) {
247 : 0 : rte_errno = errno;
248 : 0 : goto error;
249 : : }
250 : : return fd;
251 : 0 : error:
252 : 0 : close(fd);
253 : 0 : return -rte_errno;
254 : : }
255 : :
256 : : /**
257 : : * Send a request message to the kernel on the Netlink socket.
258 : : *
259 : : * @param[in] nlsk_fd
260 : : * Netlink socket file descriptor.
261 : : * @param[in] nh
262 : : * The Netlink message send to the kernel.
263 : : * @param[in] ssn
264 : : * Sequence number.
265 : : * @param[in] req
266 : : * Pointer to the request structure.
267 : : * @param[in] len
268 : : * Length of the request in bytes.
269 : : *
270 : : * @return
271 : : * The number of sent bytes on success, a negative errno value otherwise and
272 : : * rte_errno is set.
273 : : */
274 : : static int
275 : 0 : mlx5_nl_request(int nlsk_fd, struct nlmsghdr *nh, uint32_t sn, void *req,
276 : : int len)
277 : : {
278 : 0 : struct sockaddr_nl sa = {
279 : : .nl_family = AF_NETLINK,
280 : : };
281 : 0 : struct iovec iov[2] = {
282 : : { .iov_base = nh, .iov_len = sizeof(*nh), },
283 : : { .iov_base = req, .iov_len = len, },
284 : : };
285 : 0 : struct msghdr msg = {
286 : : .msg_name = &sa,
287 : : .msg_namelen = sizeof(sa),
288 : : .msg_iov = iov,
289 : : .msg_iovlen = 2,
290 : : };
291 : : int send_bytes;
292 : :
293 : 0 : nh->nlmsg_pid = 0; /* communication with the kernel uses pid 0 */
294 : 0 : nh->nlmsg_seq = sn;
295 : 0 : send_bytes = sendmsg(nlsk_fd, &msg, 0);
296 [ # # ]: 0 : if (send_bytes < 0) {
297 : 0 : rte_errno = errno;
298 : 0 : return -rte_errno;
299 : : }
300 : : return send_bytes;
301 : : }
302 : :
303 : : /**
304 : : * Send a message to the kernel on the Netlink socket.
305 : : *
306 : : * @param[in] nlsk_fd
307 : : * The Netlink socket file descriptor used for communication.
308 : : * @param[in] nh
309 : : * The Netlink message send to the kernel.
310 : : * @param[in] sn
311 : : * Sequence number.
312 : : *
313 : : * @return
314 : : * The number of sent bytes on success, a negative errno value otherwise and
315 : : * rte_errno is set.
316 : : */
317 : : static int
318 : 0 : mlx5_nl_send(int nlsk_fd, struct nlmsghdr *nh, uint32_t sn)
319 : : {
320 : 0 : struct sockaddr_nl sa = {
321 : : .nl_family = AF_NETLINK,
322 : : };
323 : 0 : struct iovec iov = {
324 : : .iov_base = nh,
325 : 0 : .iov_len = nh->nlmsg_len,
326 : : };
327 : 0 : struct msghdr msg = {
328 : : .msg_name = &sa,
329 : : .msg_namelen = sizeof(sa),
330 : : .msg_iov = &iov,
331 : : .msg_iovlen = 1,
332 : : };
333 : : int send_bytes;
334 : :
335 : 0 : nh->nlmsg_pid = 0; /* communication with the kernel uses pid 0 */
336 : 0 : nh->nlmsg_seq = sn;
337 : 0 : send_bytes = sendmsg(nlsk_fd, &msg, 0);
338 [ # # ]: 0 : if (send_bytes < 0) {
339 : 0 : rte_errno = errno;
340 : 0 : return -rte_errno;
341 : : }
342 : : return send_bytes;
343 : : }
344 : :
345 : : /**
346 : : * Receive a message from the kernel on the Netlink socket, following
347 : : * mlx5_nl_send().
348 : : *
349 : : * @param[in] nlsk_fd
350 : : * The Netlink socket file descriptor used for communication.
351 : : * @param[in] sn
352 : : * Sequence number.
353 : : * @param[in] cb
354 : : * The callback function to call for each Netlink message received.
355 : : * @param[in, out] arg
356 : : * Custom arguments for the callback.
357 : : *
358 : : * @return
359 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
360 : : */
361 : : static int
362 : 0 : mlx5_nl_recv(int nlsk_fd, uint32_t sn, int (*cb)(struct nlmsghdr *, void *arg),
363 : : void *arg)
364 : : {
365 : : struct sockaddr_nl sa;
366 : : struct iovec iov;
367 : 0 : struct msghdr msg = {
368 : : .msg_name = &sa,
369 : : .msg_namelen = sizeof(sa),
370 : : .msg_iov = &iov,
371 : : /* One message at a time */
372 : : .msg_iovlen = 1,
373 : : };
374 : : void *buf = NULL;
375 : : int multipart = 0;
376 : : int ret = 0;
377 : :
378 : : do {
379 : : struct nlmsghdr *nh;
380 : : int recv_bytes;
381 : :
382 : : do {
383 : : /* Query length of incoming message. */
384 : 0 : iov.iov_base = NULL;
385 : 0 : iov.iov_len = 0;
386 : 0 : recv_bytes = recvmsg(nlsk_fd, &msg,
387 : : MSG_PEEK | MSG_TRUNC);
388 [ # # ]: 0 : if (recv_bytes < 0) {
389 : 0 : rte_errno = errno;
390 : 0 : ret = -rte_errno;
391 : 0 : goto exit;
392 : : }
393 [ # # ]: 0 : if (recv_bytes == 0) {
394 : 0 : rte_errno = ENODATA;
395 : : ret = -rte_errno;
396 : 0 : goto exit;
397 : : }
398 : : /* Allocate buffer to fetch the message. */
399 : : if (recv_bytes < MLX5_RECV_BUF_SIZE)
400 : : recv_bytes = MLX5_RECV_BUF_SIZE;
401 : 0 : mlx5_free(buf);
402 : 0 : buf = mlx5_malloc(0, recv_bytes, 0, SOCKET_ID_ANY);
403 [ # # ]: 0 : if (!buf) {
404 : 0 : rte_errno = ENOMEM;
405 : : ret = -rte_errno;
406 : 0 : goto exit;
407 : : }
408 : : /* Fetch the message. */
409 : 0 : iov.iov_base = buf;
410 : 0 : iov.iov_len = recv_bytes;
411 : 0 : recv_bytes = recvmsg(nlsk_fd, &msg, 0);
412 [ # # ]: 0 : if (recv_bytes == -1) {
413 : 0 : rte_errno = errno;
414 : 0 : ret = -rte_errno;
415 : 0 : goto exit;
416 : : }
417 : : nh = (struct nlmsghdr *)buf;
418 [ # # ]: 0 : } while (nh->nlmsg_seq != sn);
419 : : for (;
420 [ # # # # : 0 : NLMSG_OK(nh, (unsigned int)recv_bytes);
# # ]
421 : 0 : nh = NLMSG_NEXT(nh, recv_bytes)) {
422 [ # # ]: 0 : if (nh->nlmsg_type == NLMSG_ERROR) {
423 : : struct nlmsgerr *err_data = NLMSG_DATA(nh);
424 : :
425 [ # # ]: 0 : if (err_data->error < 0) {
426 : 0 : rte_errno = -err_data->error;
427 : : ret = -rte_errno;
428 : 0 : goto exit;
429 : : }
430 : : /* Ack message. */
431 : : ret = 0;
432 : 0 : goto exit;
433 : : }
434 : : /* Multi-part msgs and their trailing DONE message. */
435 [ # # ]: 0 : if (nh->nlmsg_flags & NLM_F_MULTI) {
436 [ # # ]: 0 : if (nh->nlmsg_type == NLMSG_DONE) {
437 : : ret = 0;
438 : 0 : goto exit;
439 : : }
440 : : multipart = 1;
441 : : }
442 [ # # ]: 0 : if (cb) {
443 : 0 : ret = cb(nh, arg);
444 [ # # ]: 0 : if (ret < 0)
445 : 0 : goto exit;
446 : : }
447 : : }
448 [ # # ]: 0 : } while (multipart);
449 : 0 : exit:
450 : 0 : mlx5_free(buf);
451 : 0 : return ret;
452 : : }
453 : :
454 : : /**
455 : : * Parse Netlink message to retrieve the bridge MAC address.
456 : : *
457 : : * @param nh
458 : : * Pointer to Netlink Message Header.
459 : : * @param arg
460 : : * PMD data register with this callback.
461 : : *
462 : : * @return
463 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
464 : : */
465 : : static int
466 : 0 : mlx5_nl_mac_addr_cb(struct nlmsghdr *nh, void *arg)
467 : : {
468 : : struct mlx5_nl_mac_addr *data = arg;
469 : : struct ndmsg *r = NLMSG_DATA(nh);
470 : : struct rtattr *attribute;
471 : : int len;
472 : :
473 : 0 : len = nh->nlmsg_len - NLMSG_LENGTH(sizeof(*r));
474 : 0 : for (attribute = MLX5_NDA_RTA(r);
475 [ # # # # : 0 : RTA_OK(attribute, len);
# # ]
476 : 0 : attribute = RTA_NEXT(attribute, len)) {
477 [ # # ]: 0 : if (attribute->rta_type == NDA_LLADDR) {
478 [ # # ]: 0 : if (data->mac_n == MLX5_MAX_MAC_ADDRESSES) {
479 : 0 : DRV_LOG(WARNING,
480 : : "not enough room to finalize the"
481 : : " request");
482 : 0 : rte_errno = ENOMEM;
483 : 0 : return -rte_errno;
484 : : }
485 : : #ifdef RTE_LIBRTE_MLX5_DEBUG
486 : : char m[RTE_ETHER_ADDR_FMT_SIZE];
487 : :
488 : : rte_ether_format_addr(m, RTE_ETHER_ADDR_FMT_SIZE,
489 : : RTA_DATA(attribute));
490 : : DRV_LOG(DEBUG, "bridge MAC address %s", m);
491 : : #endif
492 : 0 : memcpy(&(*data->mac)[data->mac_n++],
493 : : RTA_DATA(attribute), RTE_ETHER_ADDR_LEN);
494 : : }
495 : : }
496 : : return 0;
497 : : }
498 : :
499 : : /**
500 : : * Get bridge MAC addresses.
501 : : *
502 : : * @param[in] nlsk_fd
503 : : * Netlink socket file descriptor.
504 : : * @param[in] iface_idx
505 : : * Net device interface index.
506 : : * @param mac[out]
507 : : * Pointer to the array table of MAC addresses to fill.
508 : : * Its size should be of MLX5_MAX_MAC_ADDRESSES.
509 : : * @param mac_n[out]
510 : : * Number of entries filled in MAC array.
511 : : *
512 : : * @return
513 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
514 : : */
515 : : static int
516 : 0 : mlx5_nl_mac_addr_list(int nlsk_fd, unsigned int iface_idx,
517 : : struct rte_ether_addr (*mac)[], int *mac_n)
518 : : {
519 : : struct {
520 : : struct nlmsghdr hdr;
521 : : struct ifinfomsg ifm;
522 : 0 : } req = {
523 : : .hdr = {
524 : : .nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
525 : : .nlmsg_type = RTM_GETNEIGH,
526 : : .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
527 : : },
528 : : .ifm = {
529 : : .ifi_family = PF_BRIDGE,
530 : : .ifi_index = iface_idx,
531 : : },
532 : : };
533 : 0 : struct mlx5_nl_mac_addr data = {
534 : : .mac = mac,
535 : : .mac_n = 0,
536 : : };
537 : 0 : uint32_t sn = MLX5_NL_SN_GENERATE;
538 : : int ret;
539 : :
540 [ # # ]: 0 : if (nlsk_fd == -1)
541 : : return 0;
542 : 0 : ret = mlx5_nl_request(nlsk_fd, &req.hdr, sn, &req.ifm,
543 : : sizeof(struct ifinfomsg));
544 [ # # ]: 0 : if (ret < 0)
545 : 0 : goto error;
546 : 0 : ret = mlx5_nl_recv(nlsk_fd, sn, mlx5_nl_mac_addr_cb, &data);
547 [ # # ]: 0 : if (ret < 0)
548 : 0 : goto error;
549 : 0 : *mac_n = data.mac_n;
550 : 0 : return 0;
551 : 0 : error:
552 : 0 : DRV_LOG(DEBUG, "Interface %u cannot retrieve MAC address list %s",
553 : : iface_idx, strerror(rte_errno));
554 : 0 : return -rte_errno;
555 : : }
556 : :
557 : : /**
558 : : * Modify the MAC address neighbour table with Netlink.
559 : : *
560 : : * @param[in] nlsk_fd
561 : : * Netlink socket file descriptor.
562 : : * @param[in] iface_idx
563 : : * Net device interface index.
564 : : * @param mac
565 : : * MAC address to consider.
566 : : * @param add
567 : : * 1 to add the MAC address, 0 to remove the MAC address.
568 : : *
569 : : * @return
570 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
571 : : */
572 : : static int
573 : 0 : mlx5_nl_mac_addr_modify(int nlsk_fd, unsigned int iface_idx,
574 : : struct rte_ether_addr *mac, int add)
575 : : {
576 : : struct {
577 : : struct nlmsghdr hdr;
578 : : struct ndmsg ndm;
579 : : struct rtattr rta;
580 : : uint8_t buffer[RTE_ETHER_ADDR_LEN];
581 [ # # ]: 0 : } req = {
582 : : .hdr = {
583 : : .nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg)),
584 : : .nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE |
585 : : NLM_F_EXCL | NLM_F_ACK,
586 : : .nlmsg_type = add ? RTM_NEWNEIGH : RTM_DELNEIGH,
587 : : },
588 : : .ndm = {
589 : : .ndm_family = PF_BRIDGE,
590 : : .ndm_state = NUD_NOARP | NUD_PERMANENT,
591 : : .ndm_ifindex = iface_idx,
592 : : .ndm_flags = NTF_SELF,
593 : : },
594 : : .rta = {
595 : : .rta_type = NDA_LLADDR,
596 : : .rta_len = RTA_LENGTH(RTE_ETHER_ADDR_LEN),
597 : : },
598 : : };
599 : 0 : uint32_t sn = MLX5_NL_SN_GENERATE;
600 : : int ret;
601 : :
602 [ # # ]: 0 : if (nlsk_fd == -1)
603 : : return 0;
604 : : memcpy(RTA_DATA(&req.rta), mac, RTE_ETHER_ADDR_LEN);
605 : 0 : req.hdr.nlmsg_len = NLMSG_ALIGN(req.hdr.nlmsg_len) +
606 : 0 : RTA_ALIGN(req.rta.rta_len);
607 : 0 : ret = mlx5_nl_send(nlsk_fd, &req.hdr, sn);
608 [ # # ]: 0 : if (ret < 0)
609 : 0 : goto error;
610 : 0 : ret = mlx5_nl_recv(nlsk_fd, sn, NULL, NULL);
611 [ # # ]: 0 : if (ret < 0)
612 : 0 : goto error;
613 : : return 0;
614 : 0 : error:
615 : : #ifdef RTE_LIBRTE_MLX5_DEBUG
616 : : {
617 : : char m[RTE_ETHER_ADDR_FMT_SIZE];
618 : :
619 : : rte_ether_format_addr(m, RTE_ETHER_ADDR_FMT_SIZE, mac);
620 : : DRV_LOG(DEBUG,
621 : : "Interface %u cannot %s MAC address %s %s",
622 : : iface_idx,
623 : : add ? "add" : "remove", m, strerror(rte_errno));
624 : : }
625 : : #endif
626 : 0 : return -rte_errno;
627 : : }
628 : :
629 : : /**
630 : : * Modify the VF MAC address neighbour table with Netlink.
631 : : *
632 : : * @param[in] nlsk_fd
633 : : * Netlink socket file descriptor.
634 : : * @param[in] iface_idx
635 : : * Net device interface index.
636 : : * @param mac
637 : : * MAC address to consider.
638 : : * @param vf_index
639 : : * VF index.
640 : : *
641 : : * @return
642 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
643 : : */
644 : : int
645 : 0 : mlx5_nl_vf_mac_addr_modify(int nlsk_fd, unsigned int iface_idx,
646 : : struct rte_ether_addr *mac, int vf_index)
647 : : {
648 : : int ret;
649 : : struct {
650 : : struct nlmsghdr hdr;
651 : : struct ifinfomsg ifm;
652 : : struct rtattr vf_list_rta;
653 : : struct rtattr vf_info_rta;
654 : : struct rtattr vf_mac_rta;
655 : : struct ifla_vf_mac ivm;
656 : 0 : } req = {
657 : : .hdr = {
658 : : .nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
659 : : .nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
660 : : .nlmsg_type = RTM_BASE,
661 : : },
662 : : .ifm = {
663 : : .ifi_index = iface_idx,
664 : : },
665 : : .vf_list_rta = {
666 : : .rta_type = IFLA_VFINFO_LIST,
667 : : .rta_len = RTA_ALIGN(RTA_LENGTH(0)),
668 : : },
669 : : .vf_info_rta = {
670 : : .rta_type = IFLA_VF_INFO,
671 : : .rta_len = RTA_ALIGN(RTA_LENGTH(0)),
672 : : },
673 : : .vf_mac_rta = {
674 : : .rta_type = IFLA_VF_MAC,
675 : : },
676 : : };
677 : 0 : struct ifla_vf_mac ivm = {
678 : : .vf = vf_index,
679 : : };
680 [ # # ]: 0 : uint32_t sn = MLX5_NL_SN_GENERATE;
681 : :
682 : : memcpy(&ivm.mac, mac, RTE_ETHER_ADDR_LEN);
683 : : memcpy(RTA_DATA(&req.vf_mac_rta), &ivm, sizeof(ivm));
684 : :
685 : 0 : req.vf_mac_rta.rta_len = RTA_LENGTH(sizeof(ivm));
686 : 0 : req.hdr.nlmsg_len = NLMSG_ALIGN(req.hdr.nlmsg_len) +
687 : 0 : RTA_ALIGN(req.vf_list_rta.rta_len) +
688 : 0 : RTA_ALIGN(req.vf_info_rta.rta_len) +
689 : : RTA_ALIGN(req.vf_mac_rta.rta_len);
690 : 0 : req.vf_list_rta.rta_len = RTE_PTR_DIFF(NLMSG_TAIL(&req.hdr),
691 : : &req.vf_list_rta);
692 : 0 : req.vf_info_rta.rta_len = RTE_PTR_DIFF(NLMSG_TAIL(&req.hdr),
693 : : &req.vf_info_rta);
694 : :
695 [ # # ]: 0 : if (nlsk_fd < 0)
696 : : return -1;
697 : 0 : ret = mlx5_nl_send(nlsk_fd, &req.hdr, sn);
698 [ # # ]: 0 : if (ret < 0)
699 : 0 : goto error;
700 : 0 : ret = mlx5_nl_recv(nlsk_fd, sn, NULL, NULL);
701 [ # # ]: 0 : if (ret < 0)
702 : 0 : goto error;
703 : : return 0;
704 : 0 : error:
705 : 0 : DRV_LOG(ERR,
706 : : "representor %u cannot set VF MAC address "
707 : : RTE_ETHER_ADDR_PRT_FMT " : %s",
708 : : vf_index,
709 : : RTE_ETHER_ADDR_BYTES(mac),
710 : : strerror(rte_errno));
711 : 0 : return -rte_errno;
712 : : }
713 : :
714 : : /**
715 : : * Add a MAC address.
716 : : *
717 : : * @param[in] nlsk_fd
718 : : * Netlink socket file descriptor.
719 : : * @param[in] iface_idx
720 : : * Net device interface index.
721 : : * @param mac_own
722 : : * BITFIELD_DECLARE array to store the mac.
723 : : * @param mac
724 : : * MAC address to register.
725 : : * @param index
726 : : * MAC address index.
727 : : *
728 : : * @return
729 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
730 : : */
731 : : int
732 : 0 : mlx5_nl_mac_addr_add(int nlsk_fd, unsigned int iface_idx,
733 : : uint64_t *mac_own, struct rte_ether_addr *mac,
734 : : uint32_t index)
735 : : {
736 : : int ret;
737 : :
738 : 0 : ret = mlx5_nl_mac_addr_modify(nlsk_fd, iface_idx, mac, 1);
739 [ # # ]: 0 : if (!ret) {
740 : : MLX5_ASSERT(index < MLX5_MAX_MAC_ADDRESSES);
741 [ # # ]: 0 : if (index >= MLX5_MAX_MAC_ADDRESSES)
742 : : return -EINVAL;
743 : :
744 : 0 : BITFIELD_SET(mac_own, index);
745 : : }
746 [ # # ]: 0 : if (ret == -EEXIST)
747 : 0 : return 0;
748 : : return ret;
749 : : }
750 : :
751 : : /**
752 : : * Remove a MAC address.
753 : : *
754 : : * @param[in] nlsk_fd
755 : : * Netlink socket file descriptor.
756 : : * @param[in] iface_idx
757 : : * Net device interface index.
758 : : * @param mac_own
759 : : * BITFIELD_DECLARE array to store the mac.
760 : : * @param mac
761 : : * MAC address to remove.
762 : : * @param index
763 : : * MAC address index.
764 : : *
765 : : * @return
766 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
767 : : */
768 : : int
769 : 0 : mlx5_nl_mac_addr_remove(int nlsk_fd, unsigned int iface_idx, uint64_t *mac_own,
770 : : struct rte_ether_addr *mac, uint32_t index)
771 : : {
772 : : MLX5_ASSERT(index < MLX5_MAX_MAC_ADDRESSES);
773 [ # # ]: 0 : if (index >= MLX5_MAX_MAC_ADDRESSES)
774 : : return -EINVAL;
775 : :
776 : 0 : BITFIELD_RESET(mac_own, index);
777 : 0 : return mlx5_nl_mac_addr_modify(nlsk_fd, iface_idx, mac, 0);
778 : : }
779 : :
780 : : /**
781 : : * Synchronize Netlink bridge table to the internal table.
782 : : *
783 : : * @param[in] nlsk_fd
784 : : * Netlink socket file descriptor.
785 : : * @param[in] iface_idx
786 : : * Net device interface index.
787 : : * @param mac_addrs
788 : : * Mac addresses array to sync.
789 : : * @param n
790 : : * @p mac_addrs array size.
791 : : */
792 : : void
793 : 0 : mlx5_nl_mac_addr_sync(int nlsk_fd, unsigned int iface_idx,
794 : : struct rte_ether_addr *mac_addrs, int n)
795 : 0 : {
796 : 0 : struct rte_ether_addr macs[n];
797 : 0 : int macs_n = 0;
798 : : int i;
799 : : int ret;
800 : :
801 : : memset(macs, 0, n * sizeof(macs[0]));
802 : 0 : ret = mlx5_nl_mac_addr_list(nlsk_fd, iface_idx, &macs, &macs_n);
803 [ # # ]: 0 : if (ret)
804 : 0 : return;
805 [ # # ]: 0 : for (i = 0; i != macs_n; ++i) {
806 : : int j;
807 : :
808 : : /* Verify the address is not in the array yet. */
809 [ # # ]: 0 : for (j = 0; j != n; ++j)
810 [ # # ]: 0 : if (rte_is_same_ether_addr(&macs[i], &mac_addrs[j]))
811 : : break;
812 [ # # ]: 0 : if (j != n)
813 : 0 : continue;
814 [ # # ]: 0 : if (rte_is_multicast_ether_addr(&macs[i])) {
815 : : /* Find the first entry available. */
816 [ # # ]: 0 : for (j = MLX5_MAX_UC_MAC_ADDRESSES; j != n; ++j) {
817 [ # # ]: 0 : if (rte_is_zero_ether_addr(&mac_addrs[j])) {
818 : 0 : mac_addrs[j] = macs[i];
819 : 0 : break;
820 : : }
821 : : }
822 : : } else {
823 : : /* Find the first entry available. */
824 [ # # ]: 0 : for (j = 0; j != MLX5_MAX_UC_MAC_ADDRESSES; ++j) {
825 [ # # ]: 0 : if (rte_is_zero_ether_addr(&mac_addrs[j])) {
826 : 0 : mac_addrs[j] = macs[i];
827 : 0 : break;
828 : : }
829 : : }
830 : : }
831 : : }
832 : : }
833 : :
834 : : /**
835 : : * Flush all added MAC addresses.
836 : : *
837 : : * @param[in] nlsk_fd
838 : : * Netlink socket file descriptor.
839 : : * @param[in] iface_idx
840 : : * Net device interface index.
841 : : * @param[in] mac_addrs
842 : : * Mac addresses array to flush.
843 : : * @param n
844 : : * @p mac_addrs array size.
845 : : * @param mac_own
846 : : * BITFIELD_DECLARE array to store the mac.
847 : : */
848 : : void
849 : 0 : mlx5_nl_mac_addr_flush(int nlsk_fd, unsigned int iface_idx,
850 : : struct rte_ether_addr *mac_addrs, int n,
851 : : uint64_t *mac_own)
852 : : {
853 : : int i;
854 : :
855 [ # # ]: 0 : if (n <= 0 || n > MLX5_MAX_MAC_ADDRESSES)
856 : : return;
857 : :
858 [ # # ]: 0 : for (i = n - 1; i >= 0; --i) {
859 : 0 : struct rte_ether_addr *m = &mac_addrs[i];
860 : :
861 [ # # ]: 0 : if (BITFIELD_ISSET(mac_own, i))
862 : 0 : mlx5_nl_mac_addr_remove(nlsk_fd, iface_idx, mac_own, m,
863 : : i);
864 : : }
865 : : }
866 : :
867 : : /**
868 : : * Enable promiscuous / all multicast mode through Netlink.
869 : : *
870 : : * @param[in] nlsk_fd
871 : : * Netlink socket file descriptor.
872 : : * @param[in] iface_idx
873 : : * Net device interface index.
874 : : * @param flags
875 : : * IFF_PROMISC for promiscuous, IFF_ALLMULTI for allmulti.
876 : : * @param enable
877 : : * Nonzero to enable, disable otherwise.
878 : : *
879 : : * @return
880 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
881 : : */
882 : : static int
883 : 0 : mlx5_nl_device_flags(int nlsk_fd, unsigned int iface_idx, uint32_t flags,
884 : : int enable)
885 : : {
886 : : struct {
887 : : struct nlmsghdr hdr;
888 : : struct ifinfomsg ifi;
889 : 0 : } req = {
890 : : .hdr = {
891 : : .nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
892 : : .nlmsg_type = RTM_NEWLINK,
893 : : .nlmsg_flags = NLM_F_REQUEST,
894 : : },
895 : : .ifi = {
896 [ # # ]: 0 : .ifi_flags = enable ? flags : 0,
897 : : .ifi_change = flags,
898 : : .ifi_index = iface_idx,
899 : : },
900 : : };
901 : 0 : uint32_t sn = MLX5_NL_SN_GENERATE;
902 : : int ret;
903 : :
904 : : MLX5_ASSERT(!(flags & ~(IFF_PROMISC | IFF_ALLMULTI)));
905 [ # # ]: 0 : if (nlsk_fd < 0)
906 : : return 0;
907 : 0 : ret = mlx5_nl_send(nlsk_fd, &req.hdr, sn);
908 : : if (ret < 0)
909 : : return ret;
910 : : return 0;
911 : : }
912 : :
913 : : /**
914 : : * Enable promiscuous mode through Netlink.
915 : : *
916 : : * @param[in] nlsk_fd
917 : : * Netlink socket file descriptor.
918 : : * @param[in] iface_idx
919 : : * Net device interface index.
920 : : * @param enable
921 : : * Nonzero to enable, disable otherwise.
922 : : *
923 : : * @return
924 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
925 : : */
926 : : int
927 : 0 : mlx5_nl_promisc(int nlsk_fd, unsigned int iface_idx, int enable)
928 : : {
929 : 0 : int ret = mlx5_nl_device_flags(nlsk_fd, iface_idx, IFF_PROMISC, enable);
930 : :
931 [ # # ]: 0 : if (ret)
932 [ # # ]: 0 : DRV_LOG(DEBUG,
933 : : "Interface %u cannot %s promisc mode: Netlink error %s",
934 : : iface_idx, enable ? "enable" : "disable",
935 : : strerror(rte_errno));
936 : 0 : return ret;
937 : : }
938 : :
939 : : /**
940 : : * Enable all multicast mode through Netlink.
941 : : *
942 : : * @param[in] nlsk_fd
943 : : * Netlink socket file descriptor.
944 : : * @param[in] iface_idx
945 : : * Net device interface index.
946 : : * @param enable
947 : : * Nonzero to enable, disable otherwise.
948 : : *
949 : : * @return
950 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
951 : : */
952 : : int
953 : 0 : mlx5_nl_allmulti(int nlsk_fd, unsigned int iface_idx, int enable)
954 : : {
955 : 0 : int ret = mlx5_nl_device_flags(nlsk_fd, iface_idx, IFF_ALLMULTI,
956 : : enable);
957 : :
958 [ # # ]: 0 : if (ret)
959 [ # # ]: 0 : DRV_LOG(DEBUG,
960 : : "Interface %u cannot %s allmulti : Netlink error %s",
961 : : iface_idx, enable ? "enable" : "disable",
962 : : strerror(rte_errno));
963 : 0 : return ret;
964 : : }
965 : :
966 : : /**
967 : : * Process network interface information from Netlink message.
968 : : *
969 : : * @param nh
970 : : * Pointer to Netlink message header.
971 : : * @param arg
972 : : * Opaque data pointer for this callback.
973 : : *
974 : : * @return
975 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
976 : : */
977 : : static int
978 : 0 : mlx5_nl_cmdget_cb(struct nlmsghdr *nh, void *arg)
979 : : {
980 : : struct mlx5_nl_port_info *data = arg;
981 : : struct mlx5_nl_port_info local = {
982 : : .flags = 0,
983 : : };
984 : : size_t off = NLMSG_HDRLEN;
985 : :
986 : 0 : if (nh->nlmsg_type !=
987 [ # # ]: 0 : RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET) &&
988 : : nh->nlmsg_type !=
989 : : RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_PORT_GET))
990 : 0 : goto error;
991 [ # # ]: 0 : while (off < nh->nlmsg_len) {
992 : 0 : struct nlattr *na = (void *)((uintptr_t)nh + off);
993 : 0 : void *payload = (void *)((uintptr_t)na + NLA_HDRLEN);
994 : :
995 [ # # ]: 0 : if (na->nla_len > nh->nlmsg_len - off)
996 : 0 : goto error;
997 [ # # # # : 0 : switch (na->nla_type) {
# # ]
998 : 0 : case RDMA_NLDEV_ATTR_DEV_INDEX:
999 : 0 : local.ibindex = *(uint32_t *)payload;
1000 : 0 : local.flags |= MLX5_NL_CMD_GET_IB_INDEX;
1001 : 0 : break;
1002 : 0 : case RDMA_NLDEV_ATTR_DEV_NAME:
1003 [ # # ]: 0 : if (!strcmp(payload, data->name))
1004 : 0 : local.flags |= MLX5_NL_CMD_GET_IB_NAME;
1005 : : break;
1006 : 0 : case RDMA_NLDEV_ATTR_NDEV_INDEX:
1007 : 0 : local.ifindex = *(uint32_t *)payload;
1008 : 0 : local.flags |= MLX5_NL_CMD_GET_NET_INDEX;
1009 : 0 : break;
1010 : 0 : case RDMA_NLDEV_ATTR_PORT_INDEX:
1011 : 0 : local.portnum = *(uint32_t *)payload;
1012 : 0 : local.flags |= MLX5_NL_CMD_GET_PORT_INDEX;
1013 : 0 : break;
1014 : 0 : case RDMA_NLDEV_ATTR_PORT_STATE:
1015 : 0 : local.state = *(uint8_t *)payload;
1016 : 0 : local.flags |= MLX5_NL_CMD_GET_PORT_STATE;
1017 : 0 : break;
1018 : : default:
1019 : : break;
1020 : : }
1021 : 0 : off += NLA_ALIGN(na->nla_len);
1022 : : }
1023 : : /*
1024 : : * It is possible to have multiple messages for all
1025 : : * Infiniband devices in the system with appropriate name.
1026 : : * So we should gather parameters locally and copy to
1027 : : * query context only in case of coinciding device name.
1028 : : */
1029 [ # # ]: 0 : if (local.flags & MLX5_NL_CMD_GET_IB_NAME) {
1030 : 0 : data->flags = local.flags;
1031 : 0 : data->ibindex = local.ibindex;
1032 : 0 : data->ifindex = local.ifindex;
1033 : 0 : data->portnum = local.portnum;
1034 : 0 : data->state = local.state;
1035 : : }
1036 : : return 0;
1037 : 0 : error:
1038 : 0 : rte_errno = EINVAL;
1039 : 0 : return -rte_errno;
1040 : : }
1041 : :
1042 : : /**
1043 : : * Get port info of network interface associated with some IB device.
1044 : : *
1045 : : * This is the only somewhat safe method to avoid resorting to heuristics
1046 : : * when faced with port representors. Unfortunately it requires at least
1047 : : * Linux 4.17.
1048 : : *
1049 : : * @param nl
1050 : : * Netlink socket of the RDMA kind (NETLINK_RDMA).
1051 : : * @param[in] pindex
1052 : : * IB device port index, starting from 1
1053 : : * @param[out] data
1054 : : * Pointer to port info.
1055 : : * @return
1056 : : * 0 on success, negative on error and rte_errno is set.
1057 : : */
1058 : : static int
1059 : 0 : mlx5_nl_port_info(int nl, uint32_t pindex, struct mlx5_nl_port_info *data)
1060 : : {
1061 : : union {
1062 : : struct nlmsghdr nh;
1063 : : uint8_t buf[NLMSG_HDRLEN +
1064 : : NLA_HDRLEN + NLA_ALIGN(sizeof(data->ibindex)) +
1065 : : NLA_HDRLEN + NLA_ALIGN(sizeof(pindex))];
1066 : 0 : } req = {
1067 : : .nh = {
1068 : : .nlmsg_len = NLMSG_LENGTH(0),
1069 : : .nlmsg_type = RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1070 : : RDMA_NLDEV_CMD_GET),
1071 : : .nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP,
1072 : : },
1073 : : };
1074 : : struct nlattr *na;
1075 : 0 : uint32_t sn = MLX5_NL_SN_GENERATE;
1076 : : int ret;
1077 : :
1078 [ # # ]: 0 : if (data->ibindex == UINT32_MAX) {
1079 : 0 : ret = mlx5_nl_send(nl, &req.nh, sn);
1080 [ # # ]: 0 : if (ret < 0)
1081 : : return ret;
1082 : 0 : ret = mlx5_nl_recv(nl, sn, mlx5_nl_cmdget_cb, data);
1083 [ # # ]: 0 : if (ret < 0)
1084 : : return ret;
1085 [ # # ]: 0 : if (!(data->flags & MLX5_NL_CMD_GET_IB_NAME) ||
1086 : : !(data->flags & MLX5_NL_CMD_GET_IB_INDEX))
1087 : 0 : goto error;
1088 : 0 : data->flags = 0;
1089 : : }
1090 : 0 : sn = MLX5_NL_SN_GENERATE;
1091 : 0 : req.nh.nlmsg_type = RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1092 : : RDMA_NLDEV_CMD_PORT_GET);
1093 : 0 : req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1094 : 0 : req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(req.buf) - NLMSG_HDRLEN);
1095 : 0 : na = (void *)((uintptr_t)req.buf + NLMSG_HDRLEN);
1096 : 0 : na->nla_len = NLA_HDRLEN + sizeof(data->ibindex);
1097 : 0 : na->nla_type = RDMA_NLDEV_ATTR_DEV_INDEX;
1098 : 0 : memcpy((void *)((uintptr_t)na + NLA_HDRLEN),
1099 : 0 : &data->ibindex, sizeof(data->ibindex));
1100 : 0 : na = (void *)((uintptr_t)na + NLA_ALIGN(na->nla_len));
1101 : 0 : na->nla_len = NLA_HDRLEN + sizeof(pindex);
1102 : 0 : na->nla_type = RDMA_NLDEV_ATTR_PORT_INDEX;
1103 : 0 : memcpy((void *)((uintptr_t)na + NLA_HDRLEN),
1104 : : &pindex, sizeof(pindex));
1105 : 0 : ret = mlx5_nl_send(nl, &req.nh, sn);
1106 [ # # ]: 0 : if (ret < 0)
1107 : : return ret;
1108 : 0 : ret = mlx5_nl_recv(nl, sn, mlx5_nl_cmdget_cb, data);
1109 [ # # ]: 0 : if (ret < 0)
1110 : : return ret;
1111 : 0 : if (!(data->flags & MLX5_NL_CMD_GET_IB_NAME) ||
1112 [ # # ]: 0 : !(data->flags & MLX5_NL_CMD_GET_IB_INDEX) ||
1113 : 0 : !(data->flags & MLX5_NL_CMD_GET_NET_INDEX) ||
1114 [ # # ]: 0 : !data->ifindex)
1115 : 0 : goto error;
1116 : : return 0;
1117 : 0 : error:
1118 : 0 : rte_errno = ENODEV;
1119 : 0 : return -rte_errno;
1120 : : }
1121 : :
1122 : : /**
1123 : : * Get index of network interface associated with some IB device.
1124 : : *
1125 : : * This is the only somewhat safe method to avoid resorting to heuristics
1126 : : * when faced with port representors. Unfortunately it requires at least
1127 : : * Linux 4.17.
1128 : : *
1129 : : * @param nl
1130 : : * Netlink socket of the RDMA kind (NETLINK_RDMA).
1131 : : * @param[in] name
1132 : : * IB device name.
1133 : : * @param[in] pindex
1134 : : * IB device port index, starting from 1
1135 : : * @param[in] dev_info
1136 : : * Cached mlx5 device information.
1137 : : * @return
1138 : : * A valid (nonzero) interface index on success, 0 otherwise and rte_errno
1139 : : * is set.
1140 : : */
1141 : : unsigned int
1142 : 0 : mlx5_nl_ifindex(int nl, const char *name, uint32_t pindex, struct mlx5_dev_info *dev_info)
1143 : : {
1144 : : int ret;
1145 : :
1146 : 0 : struct mlx5_nl_port_info data = {
1147 : : .ifindex = 0,
1148 : : .name = name,
1149 : : .ibindex = UINT32_MAX,
1150 : : .flags = 0,
1151 : : };
1152 : :
1153 [ # # # # ]: 0 : if (dev_info->probe_opt && !strcmp(name, dev_info->ibname)) {
1154 [ # # # # ]: 0 : if (dev_info->port_info && pindex <= dev_info->port_num &&
1155 [ # # ]: 0 : dev_info->port_info[pindex].valid) {
1156 [ # # ]: 0 : if (!dev_info->port_info[pindex].ifindex)
1157 : 0 : rte_errno = ENODEV;
1158 : 0 : return dev_info->port_info[pindex].ifindex;
1159 : : }
1160 [ # # ]: 0 : if (dev_info->port_num)
1161 : 0 : data.ibindex = dev_info->ibindex;
1162 : : }
1163 : :
1164 : 0 : ret = mlx5_nl_port_info(nl, pindex, &data);
1165 : :
1166 [ # # # # ]: 0 : if (dev_info->probe_opt && !strcmp(dev_info->ibname, name)) {
1167 [ # # # # ]: 0 : if ((!ret || ret == -ENODEV) && dev_info->port_info &&
1168 [ # # ]: 0 : pindex <= dev_info->port_num) {
1169 [ # # ]: 0 : if (!ret)
1170 : 0 : dev_info->port_info[pindex].ifindex = data.ifindex;
1171 : : /* -ENODEV means the pindex is unused but still valid case */
1172 : 0 : dev_info->port_info[pindex].valid = 1;
1173 : : }
1174 : : }
1175 : :
1176 [ # # ]: 0 : return ret ? 0 : data.ifindex;
1177 : : }
1178 : :
1179 : : /**
1180 : : * Get IB device port state.
1181 : : *
1182 : : * This is the only somewhat safe method to get info for port number >= 255.
1183 : : * Unfortunately it requires at least Linux 4.17.
1184 : : *
1185 : : * @param nl
1186 : : * Netlink socket of the RDMA kind (NETLINK_RDMA).
1187 : : * @param[in] name
1188 : : * IB device name.
1189 : : * @param[in] pindex
1190 : : * IB device port index, starting from 1
1191 : : * @param[in] dev_info
1192 : : * Cached mlx5 device information.
1193 : : * @return
1194 : : * Port state (ibv_port_state) on success, negative on error
1195 : : * and rte_errno is set.
1196 : : */
1197 : : int
1198 : 0 : mlx5_nl_port_state(int nl, const char *name, uint32_t pindex, struct mlx5_dev_info *dev_info)
1199 : : {
1200 : 0 : struct mlx5_nl_port_info data = {
1201 : : .state = 0,
1202 : : .name = name,
1203 : : .ibindex = UINT32_MAX,
1204 : : };
1205 : :
1206 [ # # # # ]: 0 : if (dev_info && dev_info->probe_opt &&
1207 [ # # # # ]: 0 : !strcmp(name, dev_info->ibname) && dev_info->port_num)
1208 : 0 : data.ibindex = dev_info->ibindex;
1209 [ # # ]: 0 : if (mlx5_nl_port_info(nl, pindex, &data) < 0)
1210 : 0 : return -rte_errno;
1211 [ # # ]: 0 : if ((data.flags & MLX5_NL_CMD_GET_PORT_STATE) == 0) {
1212 : 0 : rte_errno = ENOTSUP;
1213 : 0 : return -rte_errno;
1214 : : }
1215 : 0 : return (int)data.state;
1216 : : }
1217 : :
1218 : : /**
1219 : : * Get the number of physical ports of given IB device.
1220 : : *
1221 : : * @param nl
1222 : : * Netlink socket of the RDMA kind (NETLINK_RDMA).
1223 : : * @param[in] name
1224 : : * IB device name.
1225 : : * @param[in] dev_info
1226 : : * Cached mlx5 device info.
1227 : : *
1228 : : * @return
1229 : : * A valid (nonzero) number of ports on success, 0 otherwise
1230 : : * and rte_errno is set.
1231 : : */
1232 : : unsigned int
1233 : 0 : mlx5_nl_portnum(int nl, const char *name, struct mlx5_dev_info *dev_info)
1234 : : {
1235 : 0 : struct mlx5_nl_port_info data = {
1236 : : .flags = 0,
1237 : : .name = name,
1238 : : .ifindex = 0,
1239 : : .portnum = 0,
1240 : : };
1241 : 0 : struct nlmsghdr req = {
1242 : : .nlmsg_len = NLMSG_LENGTH(0),
1243 : : .nlmsg_type = RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1244 : : RDMA_NLDEV_CMD_GET),
1245 : : .nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP,
1246 : : };
1247 : 0 : uint32_t sn = MLX5_NL_SN_GENERATE;
1248 : : int ret, size;
1249 : :
1250 [ # # # # ]: 0 : if (dev_info->probe_opt && dev_info->port_num &&
1251 [ # # ]: 0 : !strcmp(name, dev_info->ibname))
1252 : : return dev_info->port_num;
1253 : :
1254 : 0 : ret = mlx5_nl_send(nl, &req, sn);
1255 [ # # ]: 0 : if (ret < 0)
1256 : : return 0;
1257 : 0 : ret = mlx5_nl_recv(nl, sn, mlx5_nl_cmdget_cb, &data);
1258 [ # # ]: 0 : if (ret < 0)
1259 : : return 0;
1260 : 0 : if (!(data.flags & MLX5_NL_CMD_GET_IB_NAME) ||
1261 [ # # ]: 0 : !(data.flags & MLX5_NL_CMD_GET_IB_INDEX) ||
1262 : : !(data.flags & MLX5_NL_CMD_GET_PORT_INDEX)) {
1263 : 0 : rte_errno = ENODEV;
1264 : 0 : return 0;
1265 : : }
1266 [ # # ]: 0 : if (!data.portnum) {
1267 : 0 : rte_errno = EINVAL;
1268 : 0 : return 0;
1269 : : }
1270 [ # # ]: 0 : if (!dev_info->probe_opt)
1271 : : return data.portnum;
1272 : : MLX5_ASSERT(!strlen(dev_info->ibname));
1273 : 0 : dev_info->port_num = data.portnum;
1274 : 0 : dev_info->ibindex = data.ibindex;
1275 [ # # ]: 0 : snprintf(dev_info->ibname, MLX5_FS_NAME_MAX, "%s", name);
1276 [ # # ]: 0 : if (data.portnum > 1) {
1277 : 0 : size = (data.portnum + 1) * sizeof(struct mlx5_port_nl_info);
1278 : 0 : dev_info->port_info = mlx5_malloc(MLX5_MEM_ZERO | MLX5_MEM_RTE, size,
1279 : : RTE_CACHE_LINE_SIZE,
1280 : : SOCKET_ID_ANY);
1281 [ # # ]: 0 : if (dev_info->port_info == NULL) {
1282 : : memset(dev_info, 0, sizeof(*dev_info));
1283 : 0 : rte_errno = ENOMEM;
1284 : 0 : return 0;
1285 : : }
1286 : : }
1287 : 0 : return data.portnum;
1288 : : }
1289 : :
1290 : : /**
1291 : : * Analyze gathered port parameters via Netlink to recognize master
1292 : : * and representor devices for E-Switch configuration.
1293 : : *
1294 : : * @param[in] num_vf_set
1295 : : * flag of presence of number of VFs port attribute.
1296 : : * @param[inout] switch_info
1297 : : * Port information, including port name as a number and port name
1298 : : * type if recognized
1299 : : *
1300 : : * @return
1301 : : * master and representor flags are set in switch_info according to
1302 : : * recognized parameters (if any).
1303 : : */
1304 : : static void
1305 : : mlx5_nl_check_switch_info(bool num_vf_set,
1306 : : struct mlx5_switch_info *switch_info)
1307 : : {
1308 [ # # # # : 0 : switch (switch_info->name_type) {
# # ]
1309 : 0 : case MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN:
1310 : : /*
1311 : : * Name is not recognized, assume the master,
1312 : : * check the number of VFs key presence.
1313 : : */
1314 : 0 : switch_info->master = num_vf_set;
1315 : 0 : break;
1316 : 0 : case MLX5_PHYS_PORT_NAME_TYPE_NOTSET:
1317 : : /*
1318 : : * Name is not set, this assumes the legacy naming
1319 : : * schema for master, just check if there is a
1320 : : * number of VFs key.
1321 : : */
1322 : 0 : switch_info->master = num_vf_set;
1323 : 0 : break;
1324 : 0 : case MLX5_PHYS_PORT_NAME_TYPE_UPLINK:
1325 : : /* New uplink naming schema recognized. */
1326 : 0 : switch_info->master = 1;
1327 : 0 : break;
1328 : 0 : case MLX5_PHYS_PORT_NAME_TYPE_LEGACY:
1329 : : /* Legacy representors naming schema. */
1330 : 0 : switch_info->representor = !num_vf_set;
1331 : 0 : break;
1332 : 0 : case MLX5_PHYS_PORT_NAME_TYPE_PFHPF:
1333 : : /* Fallthrough */
1334 : : case MLX5_PHYS_PORT_NAME_TYPE_PFVF:
1335 : : /* Fallthrough */
1336 : : case MLX5_PHYS_PORT_NAME_TYPE_PFSF:
1337 : : /* New representors naming schema. */
1338 : 0 : switch_info->representor = 1;
1339 : 0 : break;
1340 : : }
1341 : : }
1342 : :
1343 : : /**
1344 : : * Process switch information from Netlink message.
1345 : : *
1346 : : * @param nh
1347 : : * Pointer to Netlink message header.
1348 : : * @param arg
1349 : : * Opaque data pointer for this callback.
1350 : : *
1351 : : * @return
1352 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
1353 : : */
1354 : : static int
1355 : 0 : mlx5_nl_switch_info_cb(struct nlmsghdr *nh, void *arg)
1356 : : {
1357 : 0 : struct mlx5_switch_info info = {
1358 : : .master = 0,
1359 : : .representor = 0,
1360 : : .name_type = MLX5_PHYS_PORT_NAME_TYPE_NOTSET,
1361 : : .port_name = 0,
1362 : : .switch_id = 0,
1363 : : };
1364 : : size_t off = NLMSG_LENGTH(sizeof(struct ifinfomsg));
1365 : : bool switch_id_set = false;
1366 : : bool num_vf_set = false;
1367 : : int len;
1368 : :
1369 [ # # ]: 0 : if (nh->nlmsg_type != RTM_NEWLINK)
1370 : 0 : goto error;
1371 [ # # ]: 0 : while (off < nh->nlmsg_len) {
1372 : 0 : struct rtattr *ra = (void *)((uintptr_t)nh + off);
1373 : 0 : void *payload = RTA_DATA(ra);
1374 : : unsigned int i;
1375 : :
1376 [ # # ]: 0 : if (ra->rta_len > nh->nlmsg_len - off)
1377 : 0 : goto error;
1378 [ # # # # ]: 0 : switch (ra->rta_type) {
1379 : 0 : case IFLA_NUM_VF:
1380 : : num_vf_set = true;
1381 : 0 : break;
1382 : 0 : case IFLA_PHYS_PORT_NAME:
1383 : 0 : len = RTA_PAYLOAD(ra);
1384 : : /* Some kernels do not pad attributes with zero. */
1385 [ # # ]: 0 : if (len > 0 && len < MLX5_PHYS_PORT_NAME_MAX) {
1386 : : char name[MLX5_PHYS_PORT_NAME_MAX];
1387 : :
1388 : : /*
1389 : : * We can't just patch the message with padding
1390 : : * zero - it might corrupt the following items
1391 : : * in the message, we have to copy the string
1392 : : * by attribute length and pad the copied one.
1393 : : */
1394 : 0 : memcpy(name, payload, len);
1395 : 0 : name[len] = 0;
1396 : 0 : mlx5_translate_port_name(name, &info);
1397 : : } else {
1398 : 0 : info.name_type =
1399 : : MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN;
1400 : : }
1401 : : break;
1402 : 0 : case IFLA_PHYS_SWITCH_ID:
1403 : 0 : info.switch_id = 0;
1404 [ # # ]: 0 : for (i = 0; i < RTA_PAYLOAD(ra); ++i) {
1405 : 0 : info.switch_id <<= 8;
1406 : 0 : info.switch_id |= ((uint8_t *)payload)[i];
1407 : : }
1408 : : switch_id_set = true;
1409 : : break;
1410 : : }
1411 : 0 : off += RTA_ALIGN(ra->rta_len);
1412 : : }
1413 [ # # ]: 0 : if (switch_id_set) {
1414 : : /* We have some E-Switch configuration. */
1415 : : mlx5_nl_check_switch_info(num_vf_set, &info);
1416 : : }
1417 : : MLX5_ASSERT(!(info.master && info.representor));
1418 : : memcpy(arg, &info, sizeof(info));
1419 : 0 : return 0;
1420 : 0 : error:
1421 : 0 : rte_errno = EINVAL;
1422 : 0 : return -rte_errno;
1423 : : }
1424 : :
1425 : : /**
1426 : : * Get switch information associated with network interface.
1427 : : *
1428 : : * @param nl
1429 : : * Netlink socket of the ROUTE kind (NETLINK_ROUTE).
1430 : : * @param ifindex
1431 : : * Network interface index.
1432 : : * @param[out] info
1433 : : * Switch information object, populated in case of success.
1434 : : *
1435 : : * @return
1436 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
1437 : : */
1438 : : int
1439 : 0 : mlx5_nl_switch_info(int nl, unsigned int ifindex,
1440 : : struct mlx5_switch_info *info)
1441 : : {
1442 : : struct {
1443 : : struct nlmsghdr nh;
1444 : : struct ifinfomsg info;
1445 : : struct rtattr rta;
1446 : : uint32_t extmask;
1447 : 0 : } req = {
1448 : : .nh = {
1449 : : .nlmsg_len = NLMSG_LENGTH
1450 : : (sizeof(req.info) +
1451 : : RTA_LENGTH(sizeof(uint32_t))),
1452 : : .nlmsg_type = RTM_GETLINK,
1453 : : .nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
1454 : : },
1455 : : .info = {
1456 : : .ifi_family = AF_UNSPEC,
1457 : : .ifi_index = ifindex,
1458 : : },
1459 : : .rta = {
1460 : : .rta_type = IFLA_EXT_MASK,
1461 : : .rta_len = RTA_LENGTH(sizeof(int32_t)),
1462 : : },
1463 : : .extmask = RTE_LE32(1),
1464 : : };
1465 : 0 : uint32_t sn = MLX5_NL_SN_GENERATE;
1466 : : int ret;
1467 : :
1468 : 0 : ret = mlx5_nl_send(nl, &req.nh, sn);
1469 [ # # ]: 0 : if (ret >= 0)
1470 : 0 : ret = mlx5_nl_recv(nl, sn, mlx5_nl_switch_info_cb, info);
1471 [ # # ]: 0 : if (info->master && info->representor) {
1472 : 0 : DRV_LOG(ERR, "ifindex %u device is recognized as master"
1473 : : " and as representor", ifindex);
1474 : 0 : rte_errno = ENODEV;
1475 : : ret = -rte_errno;
1476 : : }
1477 : 0 : return ret;
1478 : : }
1479 : :
1480 : : /*
1481 : : * Delete VLAN network device by ifindex.
1482 : : *
1483 : : * @param[in] tcf
1484 : : * Context object initialized by mlx5_nl_vlan_vmwa_init().
1485 : : * @param[in] ifindex
1486 : : * Interface index of network device to delete.
1487 : : */
1488 : : void
1489 : 0 : mlx5_nl_vlan_vmwa_delete(struct mlx5_nl_vlan_vmwa_context *vmwa,
1490 : : uint32_t ifindex)
1491 : : {
1492 : 0 : uint32_t sn = MLX5_NL_SN_GENERATE;
1493 : : int ret;
1494 : : struct {
1495 : : struct nlmsghdr nh;
1496 : : struct ifinfomsg info;
1497 : 0 : } req = {
1498 : : .nh = {
1499 : : .nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
1500 : : .nlmsg_type = RTM_DELLINK,
1501 : : .nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
1502 : : },
1503 : : .info = {
1504 : : .ifi_family = AF_UNSPEC,
1505 : : .ifi_index = ifindex,
1506 : : },
1507 : : };
1508 : :
1509 [ # # ]: 0 : if (ifindex) {
1510 : 0 : ret = mlx5_nl_send(vmwa->nl_socket, &req.nh, sn);
1511 [ # # ]: 0 : if (ret >= 0)
1512 : 0 : ret = mlx5_nl_recv(vmwa->nl_socket, sn, NULL, NULL);
1513 [ # # ]: 0 : if (ret < 0)
1514 : 0 : DRV_LOG(WARNING, "netlink: error deleting VLAN WA"
1515 : : " ifindex %u, %d", ifindex, ret);
1516 : : }
1517 : 0 : }
1518 : :
1519 : : /* Set of subroutines to build Netlink message. */
1520 : : static struct nlattr *
1521 : : nl_msg_tail(struct nlmsghdr *nlh)
1522 : : {
1523 : 0 : return (struct nlattr *)
1524 : 0 : (((uint8_t *)nlh) + NLMSG_ALIGN(nlh->nlmsg_len));
1525 : : }
1526 : :
1527 : : static void
1528 : 0 : nl_attr_put(struct nlmsghdr *nlh, int type, const void *data, int alen)
1529 : : {
1530 : : struct nlattr *nla = nl_msg_tail(nlh);
1531 : :
1532 : 0 : nla->nla_type = type;
1533 : 0 : nla->nla_len = NLMSG_ALIGN(sizeof(struct nlattr)) + alen;
1534 : 0 : nlh->nlmsg_len += NLMSG_ALIGN(nla->nla_len);
1535 : :
1536 [ # # ]: 0 : if (alen)
1537 : 0 : memcpy((uint8_t *)nla + sizeof(struct nlattr), data, alen);
1538 : 0 : }
1539 : :
1540 : : static struct nlattr *
1541 : : nl_attr_nest_start(struct nlmsghdr *nlh, int type)
1542 : : {
1543 : : struct nlattr *nest = (struct nlattr *)nl_msg_tail(nlh);
1544 : :
1545 : : nl_attr_put(nlh, type, NULL, 0);
1546 : : return nest;
1547 : : }
1548 : :
1549 : : static void
1550 : : nl_attr_nest_end(struct nlmsghdr *nlh, struct nlattr *nest)
1551 : : {
1552 : 0 : nest->nla_len = (uint8_t *)nl_msg_tail(nlh) - (uint8_t *)nest;
1553 : : }
1554 : :
1555 : : /*
1556 : : * Create network VLAN device with specified VLAN tag.
1557 : : *
1558 : : * @param[in] tcf
1559 : : * Context object initialized by mlx5_nl_vlan_vmwa_init().
1560 : : * @param[in] ifindex
1561 : : * Base network interface index.
1562 : : * @param[in] tag
1563 : : * VLAN tag for VLAN network device to create.
1564 : : */
1565 : : uint32_t
1566 : 0 : mlx5_nl_vlan_vmwa_create(struct mlx5_nl_vlan_vmwa_context *vmwa,
1567 : : uint32_t ifindex, uint16_t tag)
1568 : : {
1569 : : struct nlmsghdr *nlh;
1570 : : struct ifinfomsg *ifm;
1571 : : char name[sizeof(MLX5_VMWA_VLAN_DEVICE_PFX) + 32];
1572 : :
1573 : : alignas(RTE_CACHE_LINE_SIZE)
1574 : : uint8_t buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
1575 : : NLMSG_ALIGN(sizeof(struct ifinfomsg)) +
1576 : : NLMSG_ALIGN(sizeof(struct nlattr)) * 8 +
1577 : : NLMSG_ALIGN(sizeof(uint32_t)) +
1578 : : NLMSG_ALIGN(sizeof(name)) +
1579 : : NLMSG_ALIGN(sizeof("vlan")) +
1580 : : NLMSG_ALIGN(sizeof(uint32_t)) +
1581 : : NLMSG_ALIGN(sizeof(uint16_t)) + 16];
1582 : : struct nlattr *na_info;
1583 : : struct nlattr *na_vlan;
1584 : 0 : uint32_t sn = MLX5_NL_SN_GENERATE;
1585 : : int ret;
1586 : :
1587 : : memset(buf, 0, sizeof(buf));
1588 : : nlh = (struct nlmsghdr *)buf;
1589 : : nlh->nlmsg_len = sizeof(struct nlmsghdr);
1590 : 0 : nlh->nlmsg_type = RTM_NEWLINK;
1591 : 0 : nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE |
1592 : : NLM_F_EXCL | NLM_F_ACK;
1593 : : ifm = (struct ifinfomsg *)nl_msg_tail(nlh);
1594 : 0 : nlh->nlmsg_len += sizeof(struct ifinfomsg);
1595 : : ifm->ifi_family = AF_UNSPEC;
1596 : : ifm->ifi_type = 0;
1597 : : ifm->ifi_index = 0;
1598 : 0 : ifm->ifi_flags = IFF_UP;
1599 : 0 : ifm->ifi_change = 0xffffffff;
1600 : 0 : nl_attr_put(nlh, IFLA_LINK, &ifindex, sizeof(ifindex));
1601 : 0 : ret = snprintf(name, sizeof(name), "%s.%u.%u",
1602 : : MLX5_VMWA_VLAN_DEVICE_PFX, ifindex, tag);
1603 : 0 : nl_attr_put(nlh, IFLA_IFNAME, name, ret + 1);
1604 : : na_info = nl_attr_nest_start(nlh, IFLA_LINKINFO);
1605 : 0 : nl_attr_put(nlh, IFLA_INFO_KIND, "vlan", sizeof("vlan"));
1606 : : na_vlan = nl_attr_nest_start(nlh, IFLA_INFO_DATA);
1607 : 0 : nl_attr_put(nlh, IFLA_VLAN_ID, &tag, sizeof(tag));
1608 : : nl_attr_nest_end(nlh, na_vlan);
1609 : : nl_attr_nest_end(nlh, na_info);
1610 : : MLX5_ASSERT(sizeof(buf) >= nlh->nlmsg_len);
1611 : 0 : ret = mlx5_nl_send(vmwa->nl_socket, nlh, sn);
1612 [ # # ]: 0 : if (ret >= 0)
1613 : 0 : ret = mlx5_nl_recv(vmwa->nl_socket, sn, NULL, NULL);
1614 [ # # ]: 0 : if (ret < 0) {
1615 : 0 : DRV_LOG(WARNING, "netlink: VLAN %s create failure (%d)", name,
1616 : : ret);
1617 : : }
1618 : : /* Try to get ifindex of created or pre-existing device. */
1619 : 0 : ret = if_nametoindex(name);
1620 [ # # ]: 0 : if (!ret) {
1621 : 0 : DRV_LOG(WARNING, "VLAN %s failed to get index (%d)", name,
1622 : : errno);
1623 : 0 : return 0;
1624 : : }
1625 : : return ret;
1626 : : }
1627 : :
1628 : : /**
1629 : : * Parse Netlink message to retrieve the general family ID.
1630 : : *
1631 : : * @param nh
1632 : : * Pointer to Netlink Message Header.
1633 : : * @param arg
1634 : : * PMD data register with this callback.
1635 : : *
1636 : : * @return
1637 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
1638 : : */
1639 : : static int
1640 : 0 : mlx5_nl_family_id_cb(struct nlmsghdr *nh, void *arg)
1641 : : {
1642 : :
1643 : 0 : struct nlattr *tail = RTE_PTR_ADD(nh, nh->nlmsg_len);
1644 : 0 : struct nlattr *nla = RTE_PTR_ADD(nh, NLMSG_ALIGN(sizeof(*nh)) +
1645 : : NLMSG_ALIGN(sizeof(struct genlmsghdr)));
1646 : :
1647 [ # # # # ]: 0 : for (; nla->nla_len && nla < tail;
1648 : 0 : nla = RTE_PTR_ADD(nla, NLMSG_ALIGN(nla->nla_len))) {
1649 [ # # ]: 0 : if (nla->nla_type == CTRL_ATTR_FAMILY_ID) {
1650 : 0 : *(uint16_t *)arg = *(uint16_t *)(nla + 1);
1651 : 0 : return 0;
1652 : : }
1653 : : }
1654 : : return -EINVAL;
1655 : : }
1656 : :
1657 : : #define MLX5_NL_MAX_ATTR_SIZE 100
1658 : : /**
1659 : : * Get generic netlink family ID.
1660 : : *
1661 : : * @param[in] nlsk_fd
1662 : : * Netlink socket file descriptor.
1663 : : * @param[in] name
1664 : : * The family name.
1665 : : *
1666 : : * @return
1667 : : * ID >= 0 on success and @p enable is updated, a negative errno value
1668 : : * otherwise and rte_errno is set.
1669 : : */
1670 : : static int
1671 : 0 : mlx5_nl_generic_family_id_get(int nlsk_fd, const char *name)
1672 : : {
1673 : : struct nlmsghdr *nlh;
1674 : : struct genlmsghdr *genl;
1675 : 0 : uint32_t sn = MLX5_NL_SN_GENERATE;
1676 : 0 : int name_size = strlen(name) + 1;
1677 : : int ret;
1678 : 0 : uint16_t id = -1;
1679 : : uint8_t buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
1680 : : NLMSG_ALIGN(sizeof(struct genlmsghdr)) +
1681 : : NLMSG_ALIGN(sizeof(struct nlattr)) +
1682 : : NLMSG_ALIGN(MLX5_NL_MAX_ATTR_SIZE)];
1683 : :
1684 : : memset(buf, 0, sizeof(buf));
1685 : : nlh = (struct nlmsghdr *)buf;
1686 : : nlh->nlmsg_len = sizeof(struct nlmsghdr);
1687 : 0 : nlh->nlmsg_type = GENL_ID_CTRL;
1688 : 0 : nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1689 : : genl = (struct genlmsghdr *)nl_msg_tail(nlh);
1690 : 0 : nlh->nlmsg_len += sizeof(struct genlmsghdr);
1691 : 0 : genl->cmd = CTRL_CMD_GETFAMILY;
1692 : 0 : genl->version = 1;
1693 : 0 : nl_attr_put(nlh, CTRL_ATTR_FAMILY_NAME, name, name_size);
1694 : 0 : ret = mlx5_nl_send(nlsk_fd, nlh, sn);
1695 [ # # ]: 0 : if (ret >= 0)
1696 : 0 : ret = mlx5_nl_recv(nlsk_fd, sn, mlx5_nl_family_id_cb, &id);
1697 [ # # ]: 0 : if (ret < 0) {
1698 : 0 : DRV_LOG(DEBUG, "Failed to get Netlink %s family ID: %d.", name,
1699 : : ret);
1700 : 0 : return ret;
1701 : : }
1702 : 0 : DRV_LOG(DEBUG, "Netlink \"%s\" family ID is %u.", name, id);
1703 : 0 : return (int)id;
1704 : : }
1705 : :
1706 : : /**
1707 : : * Get Devlink family ID.
1708 : : *
1709 : : * @param[in] nlsk_fd
1710 : : * Netlink socket file descriptor.
1711 : : *
1712 : : * @return
1713 : : * ID >= 0 on success and @p enable is updated, a negative errno value
1714 : : * otherwise and rte_errno is set.
1715 : : */
1716 : :
1717 : : int
1718 : 0 : mlx5_nl_devlink_family_id_get(int nlsk_fd)
1719 : : {
1720 : 0 : return mlx5_nl_generic_family_id_get(nlsk_fd, DEVLINK_GENL_NAME);
1721 : : }
1722 : :
1723 : : /**
1724 : : * Parse Netlink message to retrieve the ROCE enable status.
1725 : : *
1726 : : * @param nh
1727 : : * Pointer to Netlink Message Header.
1728 : : * @param arg
1729 : : * PMD data register with this callback.
1730 : : *
1731 : : * @return
1732 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
1733 : : */
1734 : : static int
1735 : 0 : mlx5_nl_roce_cb(struct nlmsghdr *nh, void *arg)
1736 : : {
1737 : :
1738 : : int ret = -EINVAL;
1739 : : int *enable = arg;
1740 : 0 : struct nlattr *tail = RTE_PTR_ADD(nh, nh->nlmsg_len);
1741 : 0 : struct nlattr *nla = RTE_PTR_ADD(nh, NLMSG_ALIGN(sizeof(*nh)) +
1742 : : NLMSG_ALIGN(sizeof(struct genlmsghdr)));
1743 : :
1744 [ # # # # ]: 0 : while (nla->nla_len && nla < tail) {
1745 [ # # # ]: 0 : switch (nla->nla_type) {
1746 : : /* Expected nested attributes case. */
1747 : 0 : case DEVLINK_ATTR_PARAM:
1748 : : case DEVLINK_ATTR_PARAM_VALUES_LIST:
1749 : : case DEVLINK_ATTR_PARAM_VALUE:
1750 : : ret = 0;
1751 : 0 : nla += 1;
1752 : 0 : break;
1753 : 0 : case DEVLINK_ATTR_PARAM_VALUE_DATA:
1754 : 0 : *enable = 1;
1755 : 0 : return 0;
1756 : 0 : default:
1757 : 0 : nla = RTE_PTR_ADD(nla, NLMSG_ALIGN(nla->nla_len));
1758 : : }
1759 : : }
1760 : 0 : *enable = 0;
1761 : 0 : return ret;
1762 : : }
1763 : :
1764 : : /**
1765 : : * Get ROCE enable status through Netlink.
1766 : : *
1767 : : * @param[in] nlsk_fd
1768 : : * Netlink socket file descriptor.
1769 : : * @param[in] family_id
1770 : : * the Devlink family ID.
1771 : : * @param pci_addr
1772 : : * The device PCI address.
1773 : : * @param[out] enable
1774 : : * Where to store the enable status.
1775 : : *
1776 : : * @return
1777 : : * 0 on success and @p enable is updated, a negative errno value otherwise
1778 : : * and rte_errno is set.
1779 : : */
1780 : : int
1781 : 0 : mlx5_nl_enable_roce_get(int nlsk_fd, int family_id, const char *pci_addr,
1782 : : int *enable)
1783 : : {
1784 : : struct nlmsghdr *nlh;
1785 : : struct genlmsghdr *genl;
1786 : 0 : uint32_t sn = MLX5_NL_SN_GENERATE;
1787 : : int ret;
1788 : 0 : int cur_en = 0;
1789 : : uint8_t buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
1790 : : NLMSG_ALIGN(sizeof(struct genlmsghdr)) +
1791 : : NLMSG_ALIGN(sizeof(struct nlattr)) * 4 +
1792 : : NLMSG_ALIGN(MLX5_NL_MAX_ATTR_SIZE) * 4];
1793 : :
1794 : : memset(buf, 0, sizeof(buf));
1795 : : nlh = (struct nlmsghdr *)buf;
1796 : : nlh->nlmsg_len = sizeof(struct nlmsghdr);
1797 : 0 : nlh->nlmsg_type = family_id;
1798 : 0 : nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1799 : : genl = (struct genlmsghdr *)nl_msg_tail(nlh);
1800 : 0 : nlh->nlmsg_len += sizeof(struct genlmsghdr);
1801 : 0 : genl->cmd = DEVLINK_CMD_PARAM_GET;
1802 : 0 : genl->version = DEVLINK_GENL_VERSION;
1803 : 0 : nl_attr_put(nlh, DEVLINK_ATTR_BUS_NAME, "pci", 4);
1804 : 0 : nl_attr_put(nlh, DEVLINK_ATTR_DEV_NAME, pci_addr, strlen(pci_addr) + 1);
1805 : 0 : nl_attr_put(nlh, DEVLINK_ATTR_PARAM_NAME, "enable_roce", 12);
1806 : 0 : ret = mlx5_nl_send(nlsk_fd, nlh, sn);
1807 [ # # ]: 0 : if (ret >= 0)
1808 : 0 : ret = mlx5_nl_recv(nlsk_fd, sn, mlx5_nl_roce_cb, &cur_en);
1809 [ # # ]: 0 : if (ret < 0) {
1810 : 0 : DRV_LOG(DEBUG, "Failed to get ROCE enable on device %s: %d.",
1811 : : pci_addr, ret);
1812 : 0 : return ret;
1813 : : }
1814 : 0 : *enable = cur_en;
1815 [ # # ]: 0 : DRV_LOG(DEBUG, "ROCE is %sabled for device \"%s\".",
1816 : : cur_en ? "en" : "dis", pci_addr);
1817 : 0 : return ret;
1818 : : }
1819 : :
1820 : : /**
1821 : : * Reload mlx5 device kernel driver through Netlink.
1822 : : *
1823 : : * @param[in] nlsk_fd
1824 : : * Netlink socket file descriptor.
1825 : : * @param[in] family_id
1826 : : * the Devlink family ID.
1827 : : * @param pci_addr
1828 : : * The device PCI address.
1829 : : * @param[out] enable
1830 : : * The enable status to set.
1831 : : *
1832 : : * @return
1833 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
1834 : : */
1835 : : static int
1836 : 0 : mlx5_nl_driver_reload(int nlsk_fd, int family_id, const char *pci_addr)
1837 : : {
1838 : : struct nlmsghdr *nlh;
1839 : : struct genlmsghdr *genl;
1840 : 0 : uint32_t sn = MLX5_NL_SN_GENERATE;
1841 : : int ret;
1842 : : uint8_t buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
1843 : : NLMSG_ALIGN(sizeof(struct genlmsghdr)) +
1844 : : NLMSG_ALIGN(sizeof(struct nlattr)) * 2 +
1845 : : NLMSG_ALIGN(MLX5_NL_MAX_ATTR_SIZE) * 2];
1846 : :
1847 : : memset(buf, 0, sizeof(buf));
1848 : : nlh = (struct nlmsghdr *)buf;
1849 : : nlh->nlmsg_len = sizeof(struct nlmsghdr);
1850 : 0 : nlh->nlmsg_type = family_id;
1851 : 0 : nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1852 : : genl = (struct genlmsghdr *)nl_msg_tail(nlh);
1853 : 0 : nlh->nlmsg_len += sizeof(struct genlmsghdr);
1854 : 0 : genl->cmd = DEVLINK_CMD_RELOAD;
1855 : 0 : genl->version = DEVLINK_GENL_VERSION;
1856 : 0 : nl_attr_put(nlh, DEVLINK_ATTR_BUS_NAME, "pci", 4);
1857 : 0 : nl_attr_put(nlh, DEVLINK_ATTR_DEV_NAME, pci_addr, strlen(pci_addr) + 1);
1858 : 0 : ret = mlx5_nl_send(nlsk_fd, nlh, sn);
1859 [ # # ]: 0 : if (ret >= 0)
1860 : 0 : ret = mlx5_nl_recv(nlsk_fd, sn, NULL, NULL);
1861 [ # # ]: 0 : if (ret < 0) {
1862 : 0 : DRV_LOG(DEBUG, "Failed to reload %s device by Netlink - %d",
1863 : : pci_addr, ret);
1864 : 0 : return ret;
1865 : : }
1866 : 0 : DRV_LOG(DEBUG, "Device \"%s\" was reloaded by Netlink successfully.",
1867 : : pci_addr);
1868 : 0 : return 0;
1869 : : }
1870 : :
1871 : : /**
1872 : : * Set ROCE enable status through Netlink.
1873 : : *
1874 : : * @param[in] nlsk_fd
1875 : : * Netlink socket file descriptor.
1876 : : * @param[in] family_id
1877 : : * the Devlink family ID.
1878 : : * @param pci_addr
1879 : : * The device PCI address.
1880 : : * @param[out] enable
1881 : : * The enable status to set.
1882 : : *
1883 : : * @return
1884 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
1885 : : */
1886 : : int
1887 : 0 : mlx5_nl_enable_roce_set(int nlsk_fd, int family_id, const char *pci_addr,
1888 : : int enable)
1889 : : {
1890 : : struct nlmsghdr *nlh;
1891 : : struct genlmsghdr *genl;
1892 : 0 : uint32_t sn = MLX5_NL_SN_GENERATE;
1893 : : int ret;
1894 : : uint8_t buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
1895 : : NLMSG_ALIGN(sizeof(struct genlmsghdr)) +
1896 : : NLMSG_ALIGN(sizeof(struct nlattr)) * 6 +
1897 : : NLMSG_ALIGN(MLX5_NL_MAX_ATTR_SIZE) * 6];
1898 : 0 : uint8_t cmode = DEVLINK_PARAM_CMODE_DRIVERINIT;
1899 : 0 : uint8_t ptype = NLA_FLAG;
1900 : : ;
1901 : :
1902 : : memset(buf, 0, sizeof(buf));
1903 : : nlh = (struct nlmsghdr *)buf;
1904 : : nlh->nlmsg_len = sizeof(struct nlmsghdr);
1905 : 0 : nlh->nlmsg_type = family_id;
1906 : 0 : nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1907 : : genl = (struct genlmsghdr *)nl_msg_tail(nlh);
1908 : 0 : nlh->nlmsg_len += sizeof(struct genlmsghdr);
1909 : 0 : genl->cmd = DEVLINK_CMD_PARAM_SET;
1910 : 0 : genl->version = DEVLINK_GENL_VERSION;
1911 : 0 : nl_attr_put(nlh, DEVLINK_ATTR_BUS_NAME, "pci", 4);
1912 : 0 : nl_attr_put(nlh, DEVLINK_ATTR_DEV_NAME, pci_addr, strlen(pci_addr) + 1);
1913 : 0 : nl_attr_put(nlh, DEVLINK_ATTR_PARAM_NAME, "enable_roce", 12);
1914 : 0 : nl_attr_put(nlh, DEVLINK_ATTR_PARAM_VALUE_CMODE, &cmode, sizeof(cmode));
1915 : 0 : nl_attr_put(nlh, DEVLINK_ATTR_PARAM_TYPE, &ptype, sizeof(ptype));
1916 [ # # ]: 0 : if (enable)
1917 : : nl_attr_put(nlh, DEVLINK_ATTR_PARAM_VALUE_DATA, NULL, 0);
1918 : 0 : ret = mlx5_nl_send(nlsk_fd, nlh, sn);
1919 [ # # ]: 0 : if (ret >= 0)
1920 : 0 : ret = mlx5_nl_recv(nlsk_fd, sn, NULL, NULL);
1921 [ # # ]: 0 : if (ret < 0) {
1922 [ # # ]: 0 : DRV_LOG(DEBUG, "Failed to %sable ROCE for device %s by Netlink:"
1923 : : " %d.", enable ? "en" : "dis", pci_addr, ret);
1924 : 0 : return ret;
1925 : : }
1926 [ # # ]: 0 : DRV_LOG(DEBUG, "Device %s ROCE was %sabled by Netlink successfully.",
1927 : : pci_addr, enable ? "en" : "dis");
1928 : : /* Now, need to reload the driver. */
1929 : 0 : return mlx5_nl_driver_reload(nlsk_fd, family_id, pci_addr);
1930 : : }
1931 : :
1932 : : /**
1933 : : * Try to parse a Netlink message as a link status update.
1934 : : *
1935 : : * @param hdr
1936 : : * Netlink message header.
1937 : : * @param[out] ifindex
1938 : : * Index of the updated interface.
1939 : : *
1940 : : * @return
1941 : : * 0 on success, negative on failure.
1942 : : */
1943 : : int
1944 : 0 : mlx5_nl_parse_link_status_update(struct nlmsghdr *hdr, uint32_t *ifindex)
1945 : : {
1946 : : struct ifinfomsg *info;
1947 : :
1948 [ # # ]: 0 : switch (hdr->nlmsg_type) {
1949 : 0 : case RTM_NEWLINK:
1950 : : case RTM_DELLINK:
1951 : : case RTM_GETLINK:
1952 : : case RTM_SETLINK:
1953 : : info = NLMSG_DATA(hdr);
1954 : 0 : *ifindex = info->ifi_index;
1955 : 0 : return 0;
1956 : : }
1957 : : return -1;
1958 : : }
1959 : :
1960 : : /**
1961 : : * Read pending events from a Netlink socket.
1962 : : *
1963 : : * @param nlsk_fd
1964 : : * Netlink socket.
1965 : : * @param cb
1966 : : * Callback invoked for each of the events.
1967 : : * @param cb_arg
1968 : : * User data for the callback.
1969 : : *
1970 : : * @return
1971 : : * 0 on success, including the case when there are no events.
1972 : : * Negative on failure and rte_errno is set.
1973 : : */
1974 : : int
1975 : 0 : mlx5_nl_read_events(int nlsk_fd, mlx5_nl_event_cb *cb, void *cb_arg)
1976 : : {
1977 : : char buf[8192];
1978 : : struct sockaddr_nl addr;
1979 : 0 : struct iovec iov = {
1980 : : .iov_base = buf,
1981 : : .iov_len = sizeof(buf),
1982 : : };
1983 : 0 : struct msghdr msg = {
1984 : : .msg_name = &addr,
1985 : : .msg_namelen = sizeof(addr),
1986 : : .msg_iov = &iov,
1987 : : .msg_iovlen = 1,
1988 : : };
1989 : : struct nlmsghdr *hdr;
1990 : : ssize_t size;
1991 : :
1992 : : while (1) {
1993 : 0 : size = recvmsg(nlsk_fd, &msg, MSG_DONTWAIT);
1994 [ # # ]: 0 : if (size < 0) {
1995 [ # # ]: 0 : if (errno == EAGAIN)
1996 : : return 0;
1997 [ # # ]: 0 : if (errno == EINTR)
1998 : 0 : continue;
1999 : 0 : DRV_LOG(DEBUG, "Failed to receive netlink message: %s",
2000 : : strerror(errno));
2001 : 0 : rte_errno = errno;
2002 : 0 : return -rte_errno;
2003 : : }
2004 : : hdr = (struct nlmsghdr *)buf;
2005 [ # # ]: 0 : while (size >= (ssize_t)sizeof(*hdr)) {
2006 : 0 : ssize_t msg_len = hdr->nlmsg_len;
2007 : 0 : ssize_t data_len = msg_len - sizeof(*hdr);
2008 : : ssize_t aligned_len;
2009 : :
2010 [ # # ]: 0 : if (data_len < 0) {
2011 : 0 : DRV_LOG(DEBUG, "Netlink message too short");
2012 : 0 : rte_errno = EINVAL;
2013 : 0 : return -rte_errno;
2014 : : }
2015 : 0 : aligned_len = NLMSG_ALIGN(msg_len);
2016 [ # # ]: 0 : if (aligned_len > size) {
2017 : 0 : DRV_LOG(DEBUG, "Netlink message too long");
2018 : 0 : rte_errno = EINVAL;
2019 : 0 : return -rte_errno;
2020 : : }
2021 : 0 : cb(hdr, cb_arg);
2022 : 0 : hdr = RTE_PTR_ADD(hdr, aligned_len);
2023 : 0 : size -= aligned_len;
2024 : : }
2025 : : }
2026 : : return 0;
2027 : : }
2028 : :
2029 : : static int
2030 : 0 : mlx5_nl_esw_multiport_cb(struct nlmsghdr *nh, void *arg)
2031 : : {
2032 : :
2033 : : int ret = -EINVAL;
2034 : : int *enable = arg;
2035 : 0 : struct nlattr *tail = RTE_PTR_ADD(nh, nh->nlmsg_len);
2036 : 0 : struct nlattr *nla = RTE_PTR_ADD(nh, NLMSG_ALIGN(sizeof(*nh)) +
2037 : : NLMSG_ALIGN(sizeof(struct genlmsghdr)));
2038 : :
2039 [ # # # # ]: 0 : while (nla->nla_len && nla < tail) {
2040 [ # # # ]: 0 : switch (nla->nla_type) {
2041 : : /* Expected nested attributes case. */
2042 : 0 : case DEVLINK_ATTR_PARAM:
2043 : : case DEVLINK_ATTR_PARAM_VALUES_LIST:
2044 : : case DEVLINK_ATTR_PARAM_VALUE:
2045 : : ret = 0;
2046 : 0 : nla += 1;
2047 : 0 : break;
2048 : 0 : case DEVLINK_ATTR_PARAM_VALUE_DATA:
2049 : 0 : *enable = 1;
2050 : 0 : return 0;
2051 : 0 : default:
2052 : 0 : nla = RTE_PTR_ADD(nla, NLMSG_ALIGN(nla->nla_len));
2053 : : }
2054 : : }
2055 : 0 : *enable = 0;
2056 : 0 : return ret;
2057 : : }
2058 : :
2059 : : #define NL_ESW_MULTIPORT_PARAM "esw_multiport"
2060 : :
2061 : : int
2062 : 0 : mlx5_nl_devlink_esw_multiport_get(int nlsk_fd, int family_id, const char *pci_addr, int *enable)
2063 : : {
2064 : : struct nlmsghdr *nlh;
2065 : : struct genlmsghdr *genl;
2066 : 0 : uint32_t sn = MLX5_NL_SN_GENERATE;
2067 : : int ret;
2068 : : uint8_t buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
2069 : : NLMSG_ALIGN(sizeof(struct genlmsghdr)) +
2070 : : NLMSG_ALIGN(sizeof(struct nlattr)) * 4 +
2071 : : NLMSG_ALIGN(MLX5_NL_MAX_ATTR_SIZE) * 4];
2072 : :
2073 : : memset(buf, 0, sizeof(buf));
2074 : : nlh = (struct nlmsghdr *)buf;
2075 : : nlh->nlmsg_len = sizeof(struct nlmsghdr);
2076 : 0 : nlh->nlmsg_type = family_id;
2077 : 0 : nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
2078 : : genl = (struct genlmsghdr *)nl_msg_tail(nlh);
2079 : 0 : nlh->nlmsg_len += sizeof(struct genlmsghdr);
2080 : 0 : genl->cmd = DEVLINK_CMD_PARAM_GET;
2081 : 0 : genl->version = DEVLINK_GENL_VERSION;
2082 : 0 : nl_attr_put(nlh, DEVLINK_ATTR_BUS_NAME, "pci", 4);
2083 : 0 : nl_attr_put(nlh, DEVLINK_ATTR_DEV_NAME, pci_addr, strlen(pci_addr) + 1);
2084 : 0 : nl_attr_put(nlh, DEVLINK_ATTR_PARAM_NAME,
2085 : : NL_ESW_MULTIPORT_PARAM, sizeof(NL_ESW_MULTIPORT_PARAM));
2086 : 0 : ret = mlx5_nl_send(nlsk_fd, nlh, sn);
2087 [ # # ]: 0 : if (ret >= 0)
2088 : 0 : ret = mlx5_nl_recv(nlsk_fd, sn, mlx5_nl_esw_multiport_cb, enable);
2089 [ # # ]: 0 : if (ret < 0) {
2090 : 0 : DRV_LOG(DEBUG, "Failed to get Multiport E-Switch enable on device %s: %d.",
2091 : : pci_addr, ret);
2092 : 0 : return ret;
2093 : : }
2094 [ # # ]: 0 : DRV_LOG(DEBUG, "Multiport E-Switch is %sabled for device \"%s\".",
2095 : : *enable ? "en" : "dis", pci_addr);
2096 : 0 : return ret;
2097 : : }
2098 : :
2099 : : int
2100 : 0 : mlx5_nl_rdma_monitor_init(void)
2101 : : {
2102 : 0 : return mlx5_nl_init(NETLINK_RDMA, RDMA_NL_GROUP_NOTIFICATION);
2103 : : }
2104 : :
2105 : : void
2106 : 0 : mlx5_nl_rdma_monitor_info_get(struct nlmsghdr *hdr, struct mlx5_nl_port_info *data)
2107 : : {
2108 : : size_t off = NLMSG_HDRLEN;
2109 : : uint8_t event_type = 0;
2110 : :
2111 [ # # ]: 0 : if (hdr->nlmsg_type != RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_MONITOR))
2112 : 0 : goto error;
2113 : :
2114 [ # # ]: 0 : while (off < hdr->nlmsg_len) {
2115 : 0 : struct nlattr *na = (void *)((uintptr_t)hdr + off);
2116 : 0 : void *payload = (void *)((uintptr_t)na + NLA_HDRLEN);
2117 : :
2118 [ # # ]: 0 : if (na->nla_len > hdr->nlmsg_len - off)
2119 : 0 : goto error;
2120 [ # # # # : 0 : switch (na->nla_type) {
# ]
2121 : 0 : case RDMA_NLDEV_ATTR_EVENT_TYPE:
2122 : 0 : event_type = *(uint8_t *)payload;
2123 [ # # ]: 0 : if (event_type == RDMA_NETDEV_ATTACH_EVENT) {
2124 : 0 : data->flags |= MLX5_NL_CMD_GET_EVENT_TYPE;
2125 : 0 : data->event_type = MLX5_NL_RDMA_NETDEV_ATTACH_EVENT;
2126 [ # # ]: 0 : } else if (event_type == RDMA_NETDEV_DETACH_EVENT) {
2127 : 0 : data->flags |= MLX5_NL_CMD_GET_EVENT_TYPE;
2128 : 0 : data->event_type = MLX5_NL_RDMA_NETDEV_DETACH_EVENT;
2129 : : }
2130 : : break;
2131 : 0 : case RDMA_NLDEV_ATTR_DEV_INDEX:
2132 : 0 : data->ibindex = *(uint32_t *)payload;
2133 : 0 : data->flags |= MLX5_NL_CMD_GET_IB_INDEX;
2134 : 0 : break;
2135 : 0 : case RDMA_NLDEV_ATTR_PORT_INDEX:
2136 : 0 : data->portnum = *(uint32_t *)payload;
2137 : 0 : data->flags |= MLX5_NL_CMD_GET_PORT_INDEX;
2138 : 0 : break;
2139 : 0 : case RDMA_NLDEV_ATTR_NDEV_INDEX:
2140 : 0 : data->ifindex = *(uint32_t *)payload;
2141 : 0 : data->flags |= MLX5_NL_CMD_GET_NET_INDEX;
2142 : 0 : break;
2143 : 0 : default:
2144 : 0 : DRV_LOG(DEBUG, "Unknown attribute[%d] found", na->nla_type);
2145 : 0 : break;
2146 : : }
2147 : 0 : off += NLA_ALIGN(na->nla_len);
2148 : : }
2149 : :
2150 : : return;
2151 : :
2152 : 0 : error:
2153 : 0 : rte_errno = EINVAL;
2154 : : }
2155 : :
2156 : : static int
2157 : 0 : mlx5_nl_rdma_monitor_cap_get_cb(struct nlmsghdr *hdr, void *arg)
2158 : : {
2159 : : size_t off = NLMSG_HDRLEN;
2160 : : uint8_t *cap = arg;
2161 : :
2162 [ # # ]: 0 : if (hdr->nlmsg_type != RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_SYS_GET))
2163 : 0 : goto error;
2164 : :
2165 : 0 : *cap = 0;
2166 [ # # ]: 0 : while (off < hdr->nlmsg_len) {
2167 : 0 : struct nlattr *na = (void *)((uintptr_t)hdr + off);
2168 : 0 : void *payload = (void *)((uintptr_t)na + NLA_HDRLEN);
2169 : :
2170 [ # # ]: 0 : if (na->nla_len > hdr->nlmsg_len - off)
2171 : 0 : goto error;
2172 [ # # ]: 0 : switch (na->nla_type) {
2173 : 0 : case RDMA_NLDEV_SYS_ATTR_MONITOR_MODE:
2174 : 0 : *cap = *(uint8_t *)payload;
2175 : 0 : return 0;
2176 : : default:
2177 : : break;
2178 : : }
2179 : 0 : off += NLA_ALIGN(na->nla_len);
2180 : : }
2181 : :
2182 : : return 0;
2183 : :
2184 : : error:
2185 : : return -EINVAL;
2186 : : }
2187 : :
2188 : : /**
2189 : : * Get RDMA monitor support in driver.
2190 : : *
2191 : : *
2192 : : * @param nl
2193 : : * Netlink socket of the RDMA kind (NETLINK_RDMA).
2194 : : * @param[out] cap
2195 : : * Pointer to port info.
2196 : : * @return
2197 : : * 0 on success, negative on error and rte_errno is set.
2198 : : */
2199 : : int
2200 : 0 : mlx5_nl_rdma_monitor_cap_get(int nl, uint8_t *cap)
2201 : : {
2202 : : union {
2203 : : struct nlmsghdr nh;
2204 : : uint8_t buf[NLMSG_HDRLEN];
2205 : 0 : } req = {
2206 : : .nh = {
2207 : : .nlmsg_len = NLMSG_LENGTH(0),
2208 : : .nlmsg_type = RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
2209 : : RDMA_NLDEV_CMD_SYS_GET),
2210 : : .nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
2211 : : },
2212 : : };
2213 : 0 : uint32_t sn = MLX5_NL_SN_GENERATE;
2214 : : int ret;
2215 : :
2216 : 0 : ret = mlx5_nl_send(nl, &req.nh, sn);
2217 [ # # ]: 0 : if (ret < 0) {
2218 : 0 : rte_errno = -ret;
2219 : 0 : return ret;
2220 : : }
2221 : 0 : ret = mlx5_nl_recv(nl, sn, mlx5_nl_rdma_monitor_cap_get_cb, cap);
2222 [ # # ]: 0 : if (ret < 0) {
2223 : 0 : rte_errno = -ret;
2224 : 0 : return ret;
2225 : : }
2226 : : return 0;
2227 : : }
|