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