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