1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2018 6WIND S.A.
3 * Copyright 2018 Mellanox Technologies, Ltd
7 #include <linux/if_link.h>
8 #include <linux/rtnetlink.h>
9 #include <linux/genetlink.h>
11 #include <rdma/rdma_netlink.h>
17 #include <sys/socket.h>
20 #include <rte_errno.h>
23 #include "../mlx5_common_log.h"
24 #include "mlx5_malloc.h"
26 #include <linux/devlink.h>
30 /* Size of the buffer to receive kernel messages */
31 #define MLX5_NL_BUF_SIZE (32 * 1024)
32 /* Send buffer size for the Netlink socket */
33 #define MLX5_SEND_BUF_SIZE 32768
34 /* Receive buffer size for the Netlink socket */
35 #define MLX5_RECV_BUF_SIZE 32768
36 /* Maximal physical port name length. */
37 #define MLX5_PHYS_PORT_NAME_MAX 128
39 /** Parameters of VLAN devices created by driver. */
40 #define MLX5_VMWA_VLAN_DEVICE_PFX "evmlx"
42 * Define NDA_RTA as defined in iproute2 sources.
44 * see in iproute2 sources file include/libnetlink.h
47 #define MLX5_NDA_RTA(r) \
48 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
51 * Define NLMSG_TAIL as defined in iproute2 sources.
53 * see in iproute2 sources file include/libnetlink.h
56 #define NLMSG_TAIL(nmsg) \
57 ((struct rtattr *)(((char *)(nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
60 * The following definitions are normally found in rdma/rdma_netlink.h,
61 * however they are so recent that most systems do not expose them yet.
63 #ifndef HAVE_RDMA_NL_NLDEV
64 #define RDMA_NL_NLDEV 5
66 #ifndef HAVE_RDMA_NLDEV_CMD_GET
67 #define RDMA_NLDEV_CMD_GET 1
69 #ifndef HAVE_RDMA_NLDEV_CMD_PORT_GET
70 #define RDMA_NLDEV_CMD_PORT_GET 5
72 #ifndef HAVE_RDMA_NLDEV_ATTR_DEV_INDEX
73 #define RDMA_NLDEV_ATTR_DEV_INDEX 1
75 #ifndef HAVE_RDMA_NLDEV_ATTR_DEV_NAME
76 #define RDMA_NLDEV_ATTR_DEV_NAME 2
78 #ifndef HAVE_RDMA_NLDEV_ATTR_PORT_INDEX
79 #define RDMA_NLDEV_ATTR_PORT_INDEX 3
81 #ifndef HAVE_RDMA_NLDEV_ATTR_NDEV_INDEX
82 #define RDMA_NLDEV_ATTR_NDEV_INDEX 50
85 /* These are normally found in linux/if_link.h. */
86 #ifndef HAVE_IFLA_NUM_VF
87 #define IFLA_NUM_VF 21
89 #ifndef HAVE_IFLA_EXT_MASK
90 #define IFLA_EXT_MASK 29
92 #ifndef HAVE_IFLA_PHYS_SWITCH_ID
93 #define IFLA_PHYS_SWITCH_ID 36
95 #ifndef HAVE_IFLA_PHYS_PORT_NAME
96 #define IFLA_PHYS_PORT_NAME 38
100 * Some Devlink defines may be missed in old kernel versions,
101 * adjust used defines.
103 #ifndef DEVLINK_GENL_NAME
104 #define DEVLINK_GENL_NAME "devlink"
106 #ifndef DEVLINK_GENL_VERSION
107 #define DEVLINK_GENL_VERSION 1
109 #ifndef DEVLINK_ATTR_BUS_NAME
110 #define DEVLINK_ATTR_BUS_NAME 1
112 #ifndef DEVLINK_ATTR_DEV_NAME
113 #define DEVLINK_ATTR_DEV_NAME 2
115 #ifndef DEVLINK_ATTR_PARAM
116 #define DEVLINK_ATTR_PARAM 80
118 #ifndef DEVLINK_ATTR_PARAM_NAME
119 #define DEVLINK_ATTR_PARAM_NAME 81
121 #ifndef DEVLINK_ATTR_PARAM_TYPE
122 #define DEVLINK_ATTR_PARAM_TYPE 83
124 #ifndef DEVLINK_ATTR_PARAM_VALUES_LIST
125 #define DEVLINK_ATTR_PARAM_VALUES_LIST 84
127 #ifndef DEVLINK_ATTR_PARAM_VALUE
128 #define DEVLINK_ATTR_PARAM_VALUE 85
130 #ifndef DEVLINK_ATTR_PARAM_VALUE_DATA
131 #define DEVLINK_ATTR_PARAM_VALUE_DATA 86
133 #ifndef DEVLINK_ATTR_PARAM_VALUE_CMODE
134 #define DEVLINK_ATTR_PARAM_VALUE_CMODE 87
136 #ifndef DEVLINK_PARAM_CMODE_DRIVERINIT
137 #define DEVLINK_PARAM_CMODE_DRIVERINIT 1
139 #ifndef DEVLINK_CMD_RELOAD
140 #define DEVLINK_CMD_RELOAD 37
142 #ifndef DEVLINK_CMD_PARAM_GET
143 #define DEVLINK_CMD_PARAM_GET 38
145 #ifndef DEVLINK_CMD_PARAM_SET
146 #define DEVLINK_CMD_PARAM_SET 39
152 /* Add/remove MAC address through Netlink */
153 struct mlx5_nl_mac_addr {
154 struct rte_ether_addr (*mac)[];
155 /**< MAC address handled by the device. */
156 int mac_n; /**< Number of addresses in the array. */
159 #define MLX5_NL_CMD_GET_IB_NAME (1 << 0)
160 #define MLX5_NL_CMD_GET_IB_INDEX (1 << 1)
161 #define MLX5_NL_CMD_GET_NET_INDEX (1 << 2)
162 #define MLX5_NL_CMD_GET_PORT_INDEX (1 << 3)
164 /** Data structure used by mlx5_nl_cmdget_cb(). */
165 struct mlx5_nl_ifindex_data {
166 const char *name; /**< IB device name (in). */
167 uint32_t flags; /**< found attribute flags (out). */
168 uint32_t ibindex; /**< IB device index (out). */
169 uint32_t ifindex; /**< Network interface index (out). */
170 uint32_t portnum; /**< IB device max port number (out). */
175 /* Generate Netlink sequence number. */
176 #define MLX5_NL_SN_GENERATE __atomic_add_fetch(&atomic_sn, 1, __ATOMIC_RELAXED)
179 * Opens a Netlink socket.
182 * Netlink protocol (e.g. NETLINK_ROUTE, NETLINK_RDMA).
185 * A file descriptor on success, a negative errno value otherwise and
189 mlx5_nl_init(int protocol)
194 struct sockaddr_nl local = {
195 .nl_family = AF_NETLINK,
199 fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, protocol);
204 opt_size = sizeof(buf_size);
205 ret = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &buf_size, &opt_size);
210 DRV_LOG(DEBUG, "Netlink socket send buffer: %d", buf_size);
211 if (buf_size < MLX5_SEND_BUF_SIZE) {
212 ret = setsockopt(fd, SOL_SOCKET, SO_SNDBUF,
213 &buf_size, sizeof(buf_size));
219 opt_size = sizeof(buf_size);
220 ret = getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &buf_size, &opt_size);
225 DRV_LOG(DEBUG, "Netlink socket recv buffer: %d", buf_size);
226 if (buf_size < MLX5_RECV_BUF_SIZE) {
227 ret = setsockopt(fd, SOL_SOCKET, SO_RCVBUF,
228 &buf_size, sizeof(buf_size));
234 ret = bind(fd, (struct sockaddr *)&local, sizeof(local));
246 * Send a request message to the kernel on the Netlink socket.
249 * Netlink socket file descriptor.
251 * The Netlink message send to the kernel.
255 * Pointer to the request structure.
257 * Length of the request in bytes.
260 * The number of sent bytes on success, a negative errno value otherwise and
264 mlx5_nl_request(int nlsk_fd, struct nlmsghdr *nh, uint32_t sn, void *req,
267 struct sockaddr_nl sa = {
268 .nl_family = AF_NETLINK,
270 struct iovec iov[2] = {
271 { .iov_base = nh, .iov_len = sizeof(*nh), },
272 { .iov_base = req, .iov_len = len, },
274 struct msghdr msg = {
276 .msg_namelen = sizeof(sa),
282 nh->nlmsg_pid = 0; /* communication with the kernel uses pid 0 */
284 send_bytes = sendmsg(nlsk_fd, &msg, 0);
285 if (send_bytes < 0) {
293 * Send a message to the kernel on the Netlink socket.
296 * The Netlink socket file descriptor used for communication.
298 * The Netlink message send to the kernel.
303 * The number of sent bytes on success, a negative errno value otherwise and
307 mlx5_nl_send(int nlsk_fd, struct nlmsghdr *nh, uint32_t sn)
309 struct sockaddr_nl sa = {
310 .nl_family = AF_NETLINK,
314 .iov_len = nh->nlmsg_len,
316 struct msghdr msg = {
318 .msg_namelen = sizeof(sa),
324 nh->nlmsg_pid = 0; /* communication with the kernel uses pid 0 */
326 send_bytes = sendmsg(nlsk_fd, &msg, 0);
327 if (send_bytes < 0) {
335 * Receive a message from the kernel on the Netlink socket, following
339 * The Netlink socket file descriptor used for communication.
343 * The callback function to call for each Netlink message received.
344 * @param[in, out] arg
345 * Custom arguments for the callback.
348 * 0 on success, a negative errno value otherwise and rte_errno is set.
351 mlx5_nl_recv(int nlsk_fd, uint32_t sn, int (*cb)(struct nlmsghdr *, void *arg),
354 struct sockaddr_nl sa;
356 struct msghdr msg = {
358 .msg_namelen = sizeof(sa),
360 /* One message at a time */
372 /* Query length of incoming message. */
375 recv_bytes = recvmsg(nlsk_fd, &msg,
376 MSG_PEEK | MSG_TRUNC);
377 if (recv_bytes < 0) {
382 if (recv_bytes == 0) {
387 /* Allocate buffer to fetch the message. */
388 if (recv_bytes < MLX5_RECV_BUF_SIZE)
389 recv_bytes = MLX5_RECV_BUF_SIZE;
391 buf = mlx5_malloc(0, recv_bytes, 0, SOCKET_ID_ANY);
397 /* Fetch the message. */
399 iov.iov_len = recv_bytes;
400 recv_bytes = recvmsg(nlsk_fd, &msg, 0);
401 if (recv_bytes == -1) {
406 nh = (struct nlmsghdr *)buf;
407 } while (nh->nlmsg_seq != sn);
409 NLMSG_OK(nh, (unsigned int)recv_bytes);
410 nh = NLMSG_NEXT(nh, recv_bytes)) {
411 if (nh->nlmsg_type == NLMSG_ERROR) {
412 struct nlmsgerr *err_data = NLMSG_DATA(nh);
414 if (err_data->error < 0) {
415 rte_errno = -err_data->error;
423 /* Multi-part msgs and their trailing DONE message. */
424 if (nh->nlmsg_flags & NLM_F_MULTI) {
425 if (nh->nlmsg_type == NLMSG_DONE) {
444 * Parse Netlink message to retrieve the bridge MAC address.
447 * Pointer to Netlink Message Header.
449 * PMD data register with this callback.
452 * 0 on success, a negative errno value otherwise and rte_errno is set.
455 mlx5_nl_mac_addr_cb(struct nlmsghdr *nh, void *arg)
457 struct mlx5_nl_mac_addr *data = arg;
458 struct ndmsg *r = NLMSG_DATA(nh);
459 struct rtattr *attribute;
462 len = nh->nlmsg_len - NLMSG_LENGTH(sizeof(*r));
463 for (attribute = MLX5_NDA_RTA(r);
464 RTA_OK(attribute, len);
465 attribute = RTA_NEXT(attribute, len)) {
466 if (attribute->rta_type == NDA_LLADDR) {
467 if (data->mac_n == MLX5_MAX_MAC_ADDRESSES) {
469 "not enough room to finalize the"
474 #ifdef RTE_LIBRTE_MLX5_DEBUG
475 char m[RTE_ETHER_ADDR_FMT_SIZE];
477 rte_ether_format_addr(m, RTE_ETHER_ADDR_FMT_SIZE,
478 RTA_DATA(attribute));
479 DRV_LOG(DEBUG, "bridge MAC address %s", m);
481 memcpy(&(*data->mac)[data->mac_n++],
482 RTA_DATA(attribute), RTE_ETHER_ADDR_LEN);
489 * Get bridge MAC addresses.
492 * Netlink socket file descriptor.
493 * @param[in] iface_idx
494 * Net device interface index.
496 * Pointer to the array table of MAC addresses to fill.
497 * Its size should be of MLX5_MAX_MAC_ADDRESSES.
499 * Number of entries filled in MAC array.
502 * 0 on success, a negative errno value otherwise and rte_errno is set.
505 mlx5_nl_mac_addr_list(int nlsk_fd, unsigned int iface_idx,
506 struct rte_ether_addr (*mac)[], int *mac_n)
510 struct ifinfomsg ifm;
513 .nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
514 .nlmsg_type = RTM_GETNEIGH,
515 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
518 .ifi_family = PF_BRIDGE,
519 .ifi_index = iface_idx,
522 struct mlx5_nl_mac_addr data = {
526 uint32_t sn = MLX5_NL_SN_GENERATE;
531 ret = mlx5_nl_request(nlsk_fd, &req.hdr, sn, &req.ifm,
532 sizeof(struct ifinfomsg));
535 ret = mlx5_nl_recv(nlsk_fd, sn, mlx5_nl_mac_addr_cb, &data);
541 DRV_LOG(DEBUG, "Interface %u cannot retrieve MAC address list %s",
542 iface_idx, strerror(rte_errno));
547 * Modify the MAC address neighbour table with Netlink.
550 * Netlink socket file descriptor.
551 * @param[in] iface_idx
552 * Net device interface index.
554 * MAC address to consider.
556 * 1 to add the MAC address, 0 to remove the MAC address.
559 * 0 on success, a negative errno value otherwise and rte_errno is set.
562 mlx5_nl_mac_addr_modify(int nlsk_fd, unsigned int iface_idx,
563 struct rte_ether_addr *mac, int add)
569 uint8_t buffer[RTE_ETHER_ADDR_LEN];
572 .nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg)),
573 .nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE |
574 NLM_F_EXCL | NLM_F_ACK,
575 .nlmsg_type = add ? RTM_NEWNEIGH : RTM_DELNEIGH,
578 .ndm_family = PF_BRIDGE,
579 .ndm_state = NUD_NOARP | NUD_PERMANENT,
580 .ndm_ifindex = iface_idx,
581 .ndm_flags = NTF_SELF,
584 .rta_type = NDA_LLADDR,
585 .rta_len = RTA_LENGTH(RTE_ETHER_ADDR_LEN),
588 uint32_t sn = MLX5_NL_SN_GENERATE;
593 memcpy(RTA_DATA(&req.rta), mac, RTE_ETHER_ADDR_LEN);
594 req.hdr.nlmsg_len = NLMSG_ALIGN(req.hdr.nlmsg_len) +
595 RTA_ALIGN(req.rta.rta_len);
596 ret = mlx5_nl_send(nlsk_fd, &req.hdr, sn);
599 ret = mlx5_nl_recv(nlsk_fd, sn, NULL, NULL);
604 #ifdef RTE_LIBRTE_MLX5_DEBUG
606 char m[RTE_ETHER_ADDR_FMT_SIZE];
608 rte_ether_format_addr(m, RTE_ETHER_ADDR_FMT_SIZE, mac);
610 "Interface %u cannot %s MAC address %s %s",
612 add ? "add" : "remove", m, strerror(rte_errno));
619 * Modify the VF MAC address neighbour table with Netlink.
622 * Netlink socket file descriptor.
623 * @param[in] iface_idx
624 * Net device interface index.
626 * MAC address to consider.
631 * 0 on success, a negative errno value otherwise and rte_errno is set.
634 mlx5_nl_vf_mac_addr_modify(int nlsk_fd, unsigned int iface_idx,
635 struct rte_ether_addr *mac, int vf_index)
640 struct ifinfomsg ifm;
641 struct rtattr vf_list_rta;
642 struct rtattr vf_info_rta;
643 struct rtattr vf_mac_rta;
644 struct ifla_vf_mac ivm;
647 .nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
648 .nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
649 .nlmsg_type = RTM_BASE,
652 .ifi_index = iface_idx,
655 .rta_type = IFLA_VFINFO_LIST,
656 .rta_len = RTA_ALIGN(RTA_LENGTH(0)),
659 .rta_type = IFLA_VF_INFO,
660 .rta_len = RTA_ALIGN(RTA_LENGTH(0)),
663 .rta_type = IFLA_VF_MAC,
666 struct ifla_vf_mac ivm = {
669 uint32_t sn = MLX5_NL_SN_GENERATE;
671 memcpy(&ivm.mac, mac, RTE_ETHER_ADDR_LEN);
672 memcpy(RTA_DATA(&req.vf_mac_rta), &ivm, sizeof(ivm));
674 req.vf_mac_rta.rta_len = RTA_LENGTH(sizeof(ivm));
675 req.hdr.nlmsg_len = NLMSG_ALIGN(req.hdr.nlmsg_len) +
676 RTA_ALIGN(req.vf_list_rta.rta_len) +
677 RTA_ALIGN(req.vf_info_rta.rta_len) +
678 RTA_ALIGN(req.vf_mac_rta.rta_len);
679 req.vf_list_rta.rta_len = RTE_PTR_DIFF(NLMSG_TAIL(&req.hdr),
681 req.vf_info_rta.rta_len = RTE_PTR_DIFF(NLMSG_TAIL(&req.hdr),
686 ret = mlx5_nl_send(nlsk_fd, &req.hdr, sn);
689 ret = mlx5_nl_recv(nlsk_fd, sn, NULL, NULL);
695 "representor %u cannot set VF MAC address "
696 RTE_ETHER_ADDR_PRT_FMT " : %s",
698 RTE_ETHER_ADDR_BYTES(mac),
699 strerror(rte_errno));
707 * Netlink socket file descriptor.
708 * @param[in] iface_idx
709 * Net device interface index.
711 * BITFIELD_DECLARE array to store the mac.
713 * MAC address to register.
718 * 0 on success, a negative errno value otherwise and rte_errno is set.
721 mlx5_nl_mac_addr_add(int nlsk_fd, unsigned int iface_idx,
722 uint64_t *mac_own, struct rte_ether_addr *mac,
727 ret = mlx5_nl_mac_addr_modify(nlsk_fd, iface_idx, mac, 1);
729 MLX5_ASSERT(index < MLX5_MAX_MAC_ADDRESSES);
730 if (index >= MLX5_MAX_MAC_ADDRESSES)
733 BITFIELD_SET(mac_own, index);
741 * Remove a MAC address.
744 * Netlink socket file descriptor.
745 * @param[in] iface_idx
746 * Net device interface index.
748 * BITFIELD_DECLARE array to store the mac.
750 * MAC address to remove.
755 * 0 on success, a negative errno value otherwise and rte_errno is set.
758 mlx5_nl_mac_addr_remove(int nlsk_fd, unsigned int iface_idx, uint64_t *mac_own,
759 struct rte_ether_addr *mac, uint32_t index)
761 MLX5_ASSERT(index < MLX5_MAX_MAC_ADDRESSES);
762 if (index >= MLX5_MAX_MAC_ADDRESSES)
765 BITFIELD_RESET(mac_own, index);
766 return mlx5_nl_mac_addr_modify(nlsk_fd, iface_idx, mac, 0);
770 * Synchronize Netlink bridge table to the internal table.
773 * Netlink socket file descriptor.
774 * @param[in] iface_idx
775 * Net device interface index.
777 * Mac addresses array to sync.
779 * @p mac_addrs array size.
782 mlx5_nl_mac_addr_sync(int nlsk_fd, unsigned int iface_idx,
783 struct rte_ether_addr *mac_addrs, int n)
785 struct rte_ether_addr macs[n];
790 memset(macs, 0, n * sizeof(macs[0]));
791 ret = mlx5_nl_mac_addr_list(nlsk_fd, iface_idx, &macs, &macs_n);
794 for (i = 0; i != macs_n; ++i) {
797 /* Verify the address is not in the array yet. */
798 for (j = 0; j != n; ++j)
799 if (rte_is_same_ether_addr(&macs[i], &mac_addrs[j]))
803 if (rte_is_multicast_ether_addr(&macs[i])) {
804 /* Find the first entry available. */
805 for (j = MLX5_MAX_UC_MAC_ADDRESSES; j != n; ++j) {
806 if (rte_is_zero_ether_addr(&mac_addrs[j])) {
807 mac_addrs[j] = macs[i];
812 /* Find the first entry available. */
813 for (j = 0; j != MLX5_MAX_UC_MAC_ADDRESSES; ++j) {
814 if (rte_is_zero_ether_addr(&mac_addrs[j])) {
815 mac_addrs[j] = macs[i];
824 * Flush all added MAC addresses.
827 * Netlink socket file descriptor.
828 * @param[in] iface_idx
829 * Net device interface index.
830 * @param[in] mac_addrs
831 * Mac addresses array to flush.
833 * @p mac_addrs array size.
835 * BITFIELD_DECLARE array to store the mac.
838 mlx5_nl_mac_addr_flush(int nlsk_fd, unsigned int iface_idx,
839 struct rte_ether_addr *mac_addrs, int n,
844 if (n <= 0 || n > MLX5_MAX_MAC_ADDRESSES)
847 for (i = n - 1; i >= 0; --i) {
848 struct rte_ether_addr *m = &mac_addrs[i];
850 if (BITFIELD_ISSET(mac_own, i))
851 mlx5_nl_mac_addr_remove(nlsk_fd, iface_idx, mac_own, m,
857 * Enable promiscuous / all multicast mode through Netlink.
860 * Netlink socket file descriptor.
861 * @param[in] iface_idx
862 * Net device interface index.
864 * IFF_PROMISC for promiscuous, IFF_ALLMULTI for allmulti.
866 * Nonzero to enable, disable otherwise.
869 * 0 on success, a negative errno value otherwise and rte_errno is set.
872 mlx5_nl_device_flags(int nlsk_fd, unsigned int iface_idx, uint32_t flags,
877 struct ifinfomsg ifi;
880 .nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
881 .nlmsg_type = RTM_NEWLINK,
882 .nlmsg_flags = NLM_F_REQUEST,
885 .ifi_flags = enable ? flags : 0,
887 .ifi_index = iface_idx,
890 uint32_t sn = MLX5_NL_SN_GENERATE;
893 MLX5_ASSERT(!(flags & ~(IFF_PROMISC | IFF_ALLMULTI)));
896 ret = mlx5_nl_send(nlsk_fd, &req.hdr, sn);
903 * Enable promiscuous mode through Netlink.
906 * Netlink socket file descriptor.
907 * @param[in] iface_idx
908 * Net device interface index.
910 * Nonzero to enable, disable otherwise.
913 * 0 on success, a negative errno value otherwise and rte_errno is set.
916 mlx5_nl_promisc(int nlsk_fd, unsigned int iface_idx, int enable)
918 int ret = mlx5_nl_device_flags(nlsk_fd, iface_idx, IFF_PROMISC, enable);
922 "Interface %u cannot %s promisc mode: Netlink error %s",
923 iface_idx, enable ? "enable" : "disable",
924 strerror(rte_errno));
929 * Enable all multicast mode through Netlink.
932 * Netlink socket file descriptor.
933 * @param[in] iface_idx
934 * Net device interface index.
936 * Nonzero to enable, disable otherwise.
939 * 0 on success, a negative errno value otherwise and rte_errno is set.
942 mlx5_nl_allmulti(int nlsk_fd, unsigned int iface_idx, int enable)
944 int ret = mlx5_nl_device_flags(nlsk_fd, iface_idx, IFF_ALLMULTI,
949 "Interface %u cannot %s allmulti : Netlink error %s",
950 iface_idx, enable ? "enable" : "disable",
951 strerror(rte_errno));
956 * Process network interface information from Netlink message.
959 * Pointer to Netlink message header.
961 * Opaque data pointer for this callback.
964 * 0 on success, a negative errno value otherwise and rte_errno is set.
967 mlx5_nl_cmdget_cb(struct nlmsghdr *nh, void *arg)
969 struct mlx5_nl_ifindex_data *data = arg;
970 struct mlx5_nl_ifindex_data local = {
973 size_t off = NLMSG_HDRLEN;
975 if (nh->nlmsg_type !=
976 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET) &&
978 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_PORT_GET))
980 while (off < nh->nlmsg_len) {
981 struct nlattr *na = (void *)((uintptr_t)nh + off);
982 void *payload = (void *)((uintptr_t)na + NLA_HDRLEN);
984 if (na->nla_len > nh->nlmsg_len - off)
986 switch (na->nla_type) {
987 case RDMA_NLDEV_ATTR_DEV_INDEX:
988 local.ibindex = *(uint32_t *)payload;
989 local.flags |= MLX5_NL_CMD_GET_IB_INDEX;
991 case RDMA_NLDEV_ATTR_DEV_NAME:
992 if (!strcmp(payload, data->name))
993 local.flags |= MLX5_NL_CMD_GET_IB_NAME;
995 case RDMA_NLDEV_ATTR_NDEV_INDEX:
996 local.ifindex = *(uint32_t *)payload;
997 local.flags |= MLX5_NL_CMD_GET_NET_INDEX;
999 case RDMA_NLDEV_ATTR_PORT_INDEX:
1000 local.portnum = *(uint32_t *)payload;
1001 local.flags |= MLX5_NL_CMD_GET_PORT_INDEX;
1006 off += NLA_ALIGN(na->nla_len);
1009 * It is possible to have multiple messages for all
1010 * Infiniband devices in the system with appropriate name.
1011 * So we should gather parameters locally and copy to
1012 * query context only in case of coinciding device name.
1014 if (local.flags & MLX5_NL_CMD_GET_IB_NAME) {
1015 data->flags = local.flags;
1016 data->ibindex = local.ibindex;
1017 data->ifindex = local.ifindex;
1018 data->portnum = local.portnum;
1027 * Get index of network interface associated with some IB device.
1029 * This is the only somewhat safe method to avoid resorting to heuristics
1030 * when faced with port representors. Unfortunately it requires at least
1034 * Netlink socket of the RDMA kind (NETLINK_RDMA).
1038 * IB device port index, starting from 1
1040 * A valid (nonzero) interface index on success, 0 otherwise and rte_errno
1044 mlx5_nl_ifindex(int nl, const char *name, uint32_t pindex)
1046 struct mlx5_nl_ifindex_data data = {
1049 .ibindex = 0, /* Determined during first pass. */
1050 .ifindex = 0, /* Determined during second pass. */
1054 uint8_t buf[NLMSG_HDRLEN +
1055 NLA_HDRLEN + NLA_ALIGN(sizeof(data.ibindex)) +
1056 NLA_HDRLEN + NLA_ALIGN(sizeof(pindex))];
1059 .nlmsg_len = NLMSG_LENGTH(0),
1060 .nlmsg_type = RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1061 RDMA_NLDEV_CMD_GET),
1062 .nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP,
1066 uint32_t sn = MLX5_NL_SN_GENERATE;
1069 ret = mlx5_nl_send(nl, &req.nh, sn);
1072 ret = mlx5_nl_recv(nl, sn, mlx5_nl_cmdget_cb, &data);
1075 if (!(data.flags & MLX5_NL_CMD_GET_IB_NAME) ||
1076 !(data.flags & MLX5_NL_CMD_GET_IB_INDEX))
1079 sn = MLX5_NL_SN_GENERATE;
1080 req.nh.nlmsg_type = RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1081 RDMA_NLDEV_CMD_PORT_GET);
1082 req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1083 req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(req.buf) - NLMSG_HDRLEN);
1084 na = (void *)((uintptr_t)req.buf + NLMSG_HDRLEN);
1085 na->nla_len = NLA_HDRLEN + sizeof(data.ibindex);
1086 na->nla_type = RDMA_NLDEV_ATTR_DEV_INDEX;
1087 memcpy((void *)((uintptr_t)na + NLA_HDRLEN),
1088 &data.ibindex, sizeof(data.ibindex));
1089 na = (void *)((uintptr_t)na + NLA_ALIGN(na->nla_len));
1090 na->nla_len = NLA_HDRLEN + sizeof(pindex);
1091 na->nla_type = RDMA_NLDEV_ATTR_PORT_INDEX;
1092 memcpy((void *)((uintptr_t)na + NLA_HDRLEN),
1093 &pindex, sizeof(pindex));
1094 ret = mlx5_nl_send(nl, &req.nh, sn);
1097 ret = mlx5_nl_recv(nl, sn, mlx5_nl_cmdget_cb, &data);
1100 if (!(data.flags & MLX5_NL_CMD_GET_IB_NAME) ||
1101 !(data.flags & MLX5_NL_CMD_GET_IB_INDEX) ||
1102 !(data.flags & MLX5_NL_CMD_GET_NET_INDEX) ||
1105 return data.ifindex;
1112 * Get the number of physical ports of given IB device.
1115 * Netlink socket of the RDMA kind (NETLINK_RDMA).
1120 * A valid (nonzero) number of ports on success, 0 otherwise
1121 * and rte_errno is set.
1124 mlx5_nl_portnum(int nl, const char *name)
1126 struct mlx5_nl_ifindex_data data = {
1132 struct nlmsghdr req = {
1133 .nlmsg_len = NLMSG_LENGTH(0),
1134 .nlmsg_type = RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1135 RDMA_NLDEV_CMD_GET),
1136 .nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP,
1138 uint32_t sn = MLX5_NL_SN_GENERATE;
1141 ret = mlx5_nl_send(nl, &req, sn);
1144 ret = mlx5_nl_recv(nl, sn, mlx5_nl_cmdget_cb, &data);
1147 if (!(data.flags & MLX5_NL_CMD_GET_IB_NAME) ||
1148 !(data.flags & MLX5_NL_CMD_GET_IB_INDEX) ||
1149 !(data.flags & MLX5_NL_CMD_GET_PORT_INDEX)) {
1155 return data.portnum;
1159 * Analyze gathered port parameters via Netlink to recognize master
1160 * and representor devices for E-Switch configuration.
1162 * @param[in] num_vf_set
1163 * flag of presence of number of VFs port attribute.
1164 * @param[inout] switch_info
1165 * Port information, including port name as a number and port name
1166 * type if recognized
1169 * master and representor flags are set in switch_info according to
1170 * recognized parameters (if any).
1173 mlx5_nl_check_switch_info(bool num_vf_set,
1174 struct mlx5_switch_info *switch_info)
1176 switch (switch_info->name_type) {
1177 case MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN:
1179 * Name is not recognized, assume the master,
1180 * check the number of VFs key presence.
1182 switch_info->master = num_vf_set;
1184 case MLX5_PHYS_PORT_NAME_TYPE_NOTSET:
1186 * Name is not set, this assumes the legacy naming
1187 * schema for master, just check if there is a
1188 * number of VFs key.
1190 switch_info->master = num_vf_set;
1192 case MLX5_PHYS_PORT_NAME_TYPE_UPLINK:
1193 /* New uplink naming schema recognized. */
1194 switch_info->master = 1;
1196 case MLX5_PHYS_PORT_NAME_TYPE_LEGACY:
1197 /* Legacy representors naming schema. */
1198 switch_info->representor = !num_vf_set;
1200 case MLX5_PHYS_PORT_NAME_TYPE_PFHPF:
1202 case MLX5_PHYS_PORT_NAME_TYPE_PFVF:
1204 case MLX5_PHYS_PORT_NAME_TYPE_PFSF:
1205 /* New representors naming schema. */
1206 switch_info->representor = 1;
1212 * Process switch information from Netlink message.
1215 * Pointer to Netlink message header.
1217 * Opaque data pointer for this callback.
1220 * 0 on success, a negative errno value otherwise and rte_errno is set.
1223 mlx5_nl_switch_info_cb(struct nlmsghdr *nh, void *arg)
1225 struct mlx5_switch_info info = {
1228 .name_type = MLX5_PHYS_PORT_NAME_TYPE_NOTSET,
1232 size_t off = NLMSG_LENGTH(sizeof(struct ifinfomsg));
1233 bool switch_id_set = false;
1234 bool num_vf_set = false;
1237 if (nh->nlmsg_type != RTM_NEWLINK)
1239 while (off < nh->nlmsg_len) {
1240 struct rtattr *ra = (void *)((uintptr_t)nh + off);
1241 void *payload = RTA_DATA(ra);
1244 if (ra->rta_len > nh->nlmsg_len - off)
1246 switch (ra->rta_type) {
1250 case IFLA_PHYS_PORT_NAME:
1251 len = RTA_PAYLOAD(ra);
1252 /* Some kernels do not pad attributes with zero. */
1253 if (len > 0 && len < MLX5_PHYS_PORT_NAME_MAX) {
1254 char name[MLX5_PHYS_PORT_NAME_MAX];
1257 * We can't just patch the message with padding
1258 * zero - it might corrupt the following items
1259 * in the message, we have to copy the string
1260 * by attribute length and pad the copied one.
1262 memcpy(name, payload, len);
1264 mlx5_translate_port_name(name, &info);
1267 MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN;
1270 case IFLA_PHYS_SWITCH_ID:
1272 for (i = 0; i < RTA_PAYLOAD(ra); ++i) {
1273 info.switch_id <<= 8;
1274 info.switch_id |= ((uint8_t *)payload)[i];
1276 switch_id_set = true;
1279 off += RTA_ALIGN(ra->rta_len);
1281 if (switch_id_set) {
1282 /* We have some E-Switch configuration. */
1283 mlx5_nl_check_switch_info(num_vf_set, &info);
1285 MLX5_ASSERT(!(info.master && info.representor));
1286 memcpy(arg, &info, sizeof(info));
1294 * Get switch information associated with network interface.
1297 * Netlink socket of the ROUTE kind (NETLINK_ROUTE).
1299 * Network interface index.
1301 * Switch information object, populated in case of success.
1304 * 0 on success, a negative errno value otherwise and rte_errno is set.
1307 mlx5_nl_switch_info(int nl, unsigned int ifindex,
1308 struct mlx5_switch_info *info)
1312 struct ifinfomsg info;
1317 .nlmsg_len = NLMSG_LENGTH
1319 RTA_LENGTH(sizeof(uint32_t))),
1320 .nlmsg_type = RTM_GETLINK,
1321 .nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
1324 .ifi_family = AF_UNSPEC,
1325 .ifi_index = ifindex,
1328 .rta_type = IFLA_EXT_MASK,
1329 .rta_len = RTA_LENGTH(sizeof(int32_t)),
1331 .extmask = RTE_LE32(1),
1333 uint32_t sn = MLX5_NL_SN_GENERATE;
1336 ret = mlx5_nl_send(nl, &req.nh, sn);
1338 ret = mlx5_nl_recv(nl, sn, mlx5_nl_switch_info_cb, info);
1339 if (info->master && info->representor) {
1340 DRV_LOG(ERR, "ifindex %u device is recognized as master"
1341 " and as representor", ifindex);
1349 * Delete VLAN network device by ifindex.
1352 * Context object initialized by mlx5_nl_vlan_vmwa_init().
1353 * @param[in] ifindex
1354 * Interface index of network device to delete.
1357 mlx5_nl_vlan_vmwa_delete(struct mlx5_nl_vlan_vmwa_context *vmwa,
1360 uint32_t sn = MLX5_NL_SN_GENERATE;
1364 struct ifinfomsg info;
1367 .nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
1368 .nlmsg_type = RTM_DELLINK,
1369 .nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
1372 .ifi_family = AF_UNSPEC,
1373 .ifi_index = ifindex,
1378 ret = mlx5_nl_send(vmwa->nl_socket, &req.nh, sn);
1380 ret = mlx5_nl_recv(vmwa->nl_socket, sn, NULL, NULL);
1382 DRV_LOG(WARNING, "netlink: error deleting VLAN WA"
1383 " ifindex %u, %d", ifindex, ret);
1387 /* Set of subroutines to build Netlink message. */
1388 static struct nlattr *
1389 nl_msg_tail(struct nlmsghdr *nlh)
1391 return (struct nlattr *)
1392 (((uint8_t *)nlh) + NLMSG_ALIGN(nlh->nlmsg_len));
1396 nl_attr_put(struct nlmsghdr *nlh, int type, const void *data, int alen)
1398 struct nlattr *nla = nl_msg_tail(nlh);
1400 nla->nla_type = type;
1401 nla->nla_len = NLMSG_ALIGN(sizeof(struct nlattr)) + alen;
1402 nlh->nlmsg_len += NLMSG_ALIGN(nla->nla_len);
1405 memcpy((uint8_t *)nla + sizeof(struct nlattr), data, alen);
1408 static struct nlattr *
1409 nl_attr_nest_start(struct nlmsghdr *nlh, int type)
1411 struct nlattr *nest = (struct nlattr *)nl_msg_tail(nlh);
1413 nl_attr_put(nlh, type, NULL, 0);
1418 nl_attr_nest_end(struct nlmsghdr *nlh, struct nlattr *nest)
1420 nest->nla_len = (uint8_t *)nl_msg_tail(nlh) - (uint8_t *)nest;
1424 * Create network VLAN device with specified VLAN tag.
1427 * Context object initialized by mlx5_nl_vlan_vmwa_init().
1428 * @param[in] ifindex
1429 * Base network interface index.
1431 * VLAN tag for VLAN network device to create.
1434 mlx5_nl_vlan_vmwa_create(struct mlx5_nl_vlan_vmwa_context *vmwa,
1435 uint32_t ifindex, uint16_t tag)
1437 struct nlmsghdr *nlh;
1438 struct ifinfomsg *ifm;
1439 char name[sizeof(MLX5_VMWA_VLAN_DEVICE_PFX) + 32];
1442 uint8_t buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
1443 NLMSG_ALIGN(sizeof(struct ifinfomsg)) +
1444 NLMSG_ALIGN(sizeof(struct nlattr)) * 8 +
1445 NLMSG_ALIGN(sizeof(uint32_t)) +
1446 NLMSG_ALIGN(sizeof(name)) +
1447 NLMSG_ALIGN(sizeof("vlan")) +
1448 NLMSG_ALIGN(sizeof(uint32_t)) +
1449 NLMSG_ALIGN(sizeof(uint16_t)) + 16];
1450 struct nlattr *na_info;
1451 struct nlattr *na_vlan;
1452 uint32_t sn = MLX5_NL_SN_GENERATE;
1455 memset(buf, 0, sizeof(buf));
1456 nlh = (struct nlmsghdr *)buf;
1457 nlh->nlmsg_len = sizeof(struct nlmsghdr);
1458 nlh->nlmsg_type = RTM_NEWLINK;
1459 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE |
1460 NLM_F_EXCL | NLM_F_ACK;
1461 ifm = (struct ifinfomsg *)nl_msg_tail(nlh);
1462 nlh->nlmsg_len += sizeof(struct ifinfomsg);
1463 ifm->ifi_family = AF_UNSPEC;
1466 ifm->ifi_flags = IFF_UP;
1467 ifm->ifi_change = 0xffffffff;
1468 nl_attr_put(nlh, IFLA_LINK, &ifindex, sizeof(ifindex));
1469 ret = snprintf(name, sizeof(name), "%s.%u.%u",
1470 MLX5_VMWA_VLAN_DEVICE_PFX, ifindex, tag);
1471 nl_attr_put(nlh, IFLA_IFNAME, name, ret + 1);
1472 na_info = nl_attr_nest_start(nlh, IFLA_LINKINFO);
1473 nl_attr_put(nlh, IFLA_INFO_KIND, "vlan", sizeof("vlan"));
1474 na_vlan = nl_attr_nest_start(nlh, IFLA_INFO_DATA);
1475 nl_attr_put(nlh, IFLA_VLAN_ID, &tag, sizeof(tag));
1476 nl_attr_nest_end(nlh, na_vlan);
1477 nl_attr_nest_end(nlh, na_info);
1478 MLX5_ASSERT(sizeof(buf) >= nlh->nlmsg_len);
1479 ret = mlx5_nl_send(vmwa->nl_socket, nlh, sn);
1481 ret = mlx5_nl_recv(vmwa->nl_socket, sn, NULL, NULL);
1483 DRV_LOG(WARNING, "netlink: VLAN %s create failure (%d)", name,
1486 /* Try to get ifindex of created or pre-existing device. */
1487 ret = if_nametoindex(name);
1489 DRV_LOG(WARNING, "VLAN %s failed to get index (%d)", name,
1497 * Parse Netlink message to retrieve the general family ID.
1500 * Pointer to Netlink Message Header.
1502 * PMD data register with this callback.
1505 * 0 on success, a negative errno value otherwise and rte_errno is set.
1508 mlx5_nl_family_id_cb(struct nlmsghdr *nh, void *arg)
1511 struct nlattr *tail = RTE_PTR_ADD(nh, nh->nlmsg_len);
1512 struct nlattr *nla = RTE_PTR_ADD(nh, NLMSG_ALIGN(sizeof(*nh)) +
1513 NLMSG_ALIGN(sizeof(struct genlmsghdr)));
1515 for (; nla->nla_len && nla < tail;
1516 nla = RTE_PTR_ADD(nla, NLMSG_ALIGN(nla->nla_len))) {
1517 if (nla->nla_type == CTRL_ATTR_FAMILY_ID) {
1518 *(uint16_t *)arg = *(uint16_t *)(nla + 1);
1525 #define MLX5_NL_MAX_ATTR_SIZE 100
1527 * Get generic netlink family ID.
1529 * @param[in] nlsk_fd
1530 * Netlink socket file descriptor.
1535 * ID >= 0 on success and @p enable is updated, a negative errno value
1536 * otherwise and rte_errno is set.
1539 mlx5_nl_generic_family_id_get(int nlsk_fd, const char *name)
1541 struct nlmsghdr *nlh;
1542 struct genlmsghdr *genl;
1543 uint32_t sn = MLX5_NL_SN_GENERATE;
1544 int name_size = strlen(name) + 1;
1547 uint8_t buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
1548 NLMSG_ALIGN(sizeof(struct genlmsghdr)) +
1549 NLMSG_ALIGN(sizeof(struct nlattr)) +
1550 NLMSG_ALIGN(MLX5_NL_MAX_ATTR_SIZE)];
1552 memset(buf, 0, sizeof(buf));
1553 nlh = (struct nlmsghdr *)buf;
1554 nlh->nlmsg_len = sizeof(struct nlmsghdr);
1555 nlh->nlmsg_type = GENL_ID_CTRL;
1556 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1557 genl = (struct genlmsghdr *)nl_msg_tail(nlh);
1558 nlh->nlmsg_len += sizeof(struct genlmsghdr);
1559 genl->cmd = CTRL_CMD_GETFAMILY;
1561 nl_attr_put(nlh, CTRL_ATTR_FAMILY_NAME, name, name_size);
1562 ret = mlx5_nl_send(nlsk_fd, nlh, sn);
1564 ret = mlx5_nl_recv(nlsk_fd, sn, mlx5_nl_family_id_cb, &id);
1566 DRV_LOG(DEBUG, "Failed to get Netlink %s family ID: %d.", name,
1570 DRV_LOG(DEBUG, "Netlink \"%s\" family ID is %u.", name, id);
1575 * Get Devlink family ID.
1577 * @param[in] nlsk_fd
1578 * Netlink socket file descriptor.
1581 * ID >= 0 on success and @p enable is updated, a negative errno value
1582 * otherwise and rte_errno is set.
1586 mlx5_nl_devlink_family_id_get(int nlsk_fd)
1588 return mlx5_nl_generic_family_id_get(nlsk_fd, DEVLINK_GENL_NAME);
1592 * Parse Netlink message to retrieve the ROCE enable status.
1595 * Pointer to Netlink Message Header.
1597 * PMD data register with this callback.
1600 * 0 on success, a negative errno value otherwise and rte_errno is set.
1603 mlx5_nl_roce_cb(struct nlmsghdr *nh, void *arg)
1608 struct nlattr *tail = RTE_PTR_ADD(nh, nh->nlmsg_len);
1609 struct nlattr *nla = RTE_PTR_ADD(nh, NLMSG_ALIGN(sizeof(*nh)) +
1610 NLMSG_ALIGN(sizeof(struct genlmsghdr)));
1612 while (nla->nla_len && nla < tail) {
1613 switch (nla->nla_type) {
1614 /* Expected nested attributes case. */
1615 case DEVLINK_ATTR_PARAM:
1616 case DEVLINK_ATTR_PARAM_VALUES_LIST:
1617 case DEVLINK_ATTR_PARAM_VALUE:
1621 case DEVLINK_ATTR_PARAM_VALUE_DATA:
1625 nla = RTE_PTR_ADD(nla, NLMSG_ALIGN(nla->nla_len));
1633 * Get ROCE enable status through Netlink.
1635 * @param[in] nlsk_fd
1636 * Netlink socket file descriptor.
1637 * @param[in] family_id
1638 * the Devlink family ID.
1640 * The device PCI address.
1641 * @param[out] enable
1642 * Where to store the enable status.
1645 * 0 on success and @p enable is updated, a negative errno value otherwise
1646 * and rte_errno is set.
1649 mlx5_nl_enable_roce_get(int nlsk_fd, int family_id, const char *pci_addr,
1652 struct nlmsghdr *nlh;
1653 struct genlmsghdr *genl;
1654 uint32_t sn = MLX5_NL_SN_GENERATE;
1657 uint8_t buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
1658 NLMSG_ALIGN(sizeof(struct genlmsghdr)) +
1659 NLMSG_ALIGN(sizeof(struct nlattr)) * 4 +
1660 NLMSG_ALIGN(MLX5_NL_MAX_ATTR_SIZE) * 4];
1662 memset(buf, 0, sizeof(buf));
1663 nlh = (struct nlmsghdr *)buf;
1664 nlh->nlmsg_len = sizeof(struct nlmsghdr);
1665 nlh->nlmsg_type = family_id;
1666 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1667 genl = (struct genlmsghdr *)nl_msg_tail(nlh);
1668 nlh->nlmsg_len += sizeof(struct genlmsghdr);
1669 genl->cmd = DEVLINK_CMD_PARAM_GET;
1670 genl->version = DEVLINK_GENL_VERSION;
1671 nl_attr_put(nlh, DEVLINK_ATTR_BUS_NAME, "pci", 4);
1672 nl_attr_put(nlh, DEVLINK_ATTR_DEV_NAME, pci_addr, strlen(pci_addr) + 1);
1673 nl_attr_put(nlh, DEVLINK_ATTR_PARAM_NAME, "enable_roce", 12);
1674 ret = mlx5_nl_send(nlsk_fd, nlh, sn);
1676 ret = mlx5_nl_recv(nlsk_fd, sn, mlx5_nl_roce_cb, &cur_en);
1678 DRV_LOG(DEBUG, "Failed to get ROCE enable on device %s: %d.",
1683 DRV_LOG(DEBUG, "ROCE is %sabled for device \"%s\".",
1684 cur_en ? "en" : "dis", pci_addr);
1689 * Reload mlx5 device kernel driver through Netlink.
1691 * @param[in] nlsk_fd
1692 * Netlink socket file descriptor.
1693 * @param[in] family_id
1694 * the Devlink family ID.
1696 * The device PCI address.
1697 * @param[out] enable
1698 * The enable status to set.
1701 * 0 on success, a negative errno value otherwise and rte_errno is set.
1704 mlx5_nl_driver_reload(int nlsk_fd, int family_id, const char *pci_addr)
1706 struct nlmsghdr *nlh;
1707 struct genlmsghdr *genl;
1708 uint32_t sn = MLX5_NL_SN_GENERATE;
1710 uint8_t buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
1711 NLMSG_ALIGN(sizeof(struct genlmsghdr)) +
1712 NLMSG_ALIGN(sizeof(struct nlattr)) * 2 +
1713 NLMSG_ALIGN(MLX5_NL_MAX_ATTR_SIZE) * 2];
1715 memset(buf, 0, sizeof(buf));
1716 nlh = (struct nlmsghdr *)buf;
1717 nlh->nlmsg_len = sizeof(struct nlmsghdr);
1718 nlh->nlmsg_type = family_id;
1719 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1720 genl = (struct genlmsghdr *)nl_msg_tail(nlh);
1721 nlh->nlmsg_len += sizeof(struct genlmsghdr);
1722 genl->cmd = DEVLINK_CMD_RELOAD;
1723 genl->version = DEVLINK_GENL_VERSION;
1724 nl_attr_put(nlh, DEVLINK_ATTR_BUS_NAME, "pci", 4);
1725 nl_attr_put(nlh, DEVLINK_ATTR_DEV_NAME, pci_addr, strlen(pci_addr) + 1);
1726 ret = mlx5_nl_send(nlsk_fd, nlh, sn);
1728 ret = mlx5_nl_recv(nlsk_fd, sn, NULL, NULL);
1730 DRV_LOG(DEBUG, "Failed to reload %s device by Netlink - %d",
1734 DRV_LOG(DEBUG, "Device \"%s\" was reloaded by Netlink successfully.",
1740 * Set ROCE enable status through Netlink.
1742 * @param[in] nlsk_fd
1743 * Netlink socket file descriptor.
1744 * @param[in] family_id
1745 * the Devlink family ID.
1747 * The device PCI address.
1748 * @param[out] enable
1749 * The enable status to set.
1752 * 0 on success, a negative errno value otherwise and rte_errno is set.
1755 mlx5_nl_enable_roce_set(int nlsk_fd, int family_id, const char *pci_addr,
1758 struct nlmsghdr *nlh;
1759 struct genlmsghdr *genl;
1760 uint32_t sn = MLX5_NL_SN_GENERATE;
1762 uint8_t buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
1763 NLMSG_ALIGN(sizeof(struct genlmsghdr)) +
1764 NLMSG_ALIGN(sizeof(struct nlattr)) * 6 +
1765 NLMSG_ALIGN(MLX5_NL_MAX_ATTR_SIZE) * 6];
1766 uint8_t cmode = DEVLINK_PARAM_CMODE_DRIVERINIT;
1767 uint8_t ptype = NLA_FLAG;
1770 memset(buf, 0, sizeof(buf));
1771 nlh = (struct nlmsghdr *)buf;
1772 nlh->nlmsg_len = sizeof(struct nlmsghdr);
1773 nlh->nlmsg_type = family_id;
1774 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1775 genl = (struct genlmsghdr *)nl_msg_tail(nlh);
1776 nlh->nlmsg_len += sizeof(struct genlmsghdr);
1777 genl->cmd = DEVLINK_CMD_PARAM_SET;
1778 genl->version = DEVLINK_GENL_VERSION;
1779 nl_attr_put(nlh, DEVLINK_ATTR_BUS_NAME, "pci", 4);
1780 nl_attr_put(nlh, DEVLINK_ATTR_DEV_NAME, pci_addr, strlen(pci_addr) + 1);
1781 nl_attr_put(nlh, DEVLINK_ATTR_PARAM_NAME, "enable_roce", 12);
1782 nl_attr_put(nlh, DEVLINK_ATTR_PARAM_VALUE_CMODE, &cmode, sizeof(cmode));
1783 nl_attr_put(nlh, DEVLINK_ATTR_PARAM_TYPE, &ptype, sizeof(ptype));
1785 nl_attr_put(nlh, DEVLINK_ATTR_PARAM_VALUE_DATA, NULL, 0);
1786 ret = mlx5_nl_send(nlsk_fd, nlh, sn);
1788 ret = mlx5_nl_recv(nlsk_fd, sn, NULL, NULL);
1790 DRV_LOG(DEBUG, "Failed to %sable ROCE for device %s by Netlink:"
1791 " %d.", enable ? "en" : "dis", pci_addr, ret);
1794 DRV_LOG(DEBUG, "Device %s ROCE was %sabled by Netlink successfully.",
1795 pci_addr, enable ? "en" : "dis");
1796 /* Now, need to reload the driver. */
1797 return mlx5_nl_driver_reload(nlsk_fd, family_id, pci_addr);