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