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)
192 int sndbuf_size = MLX5_SEND_BUF_SIZE;
193 int rcvbuf_size = MLX5_RECV_BUF_SIZE;
194 struct sockaddr_nl local = {
195 .nl_family = AF_NETLINK,
199 fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, protocol);
204 ret = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sndbuf_size, sizeof(int));
209 ret = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf_size, sizeof(int));
214 ret = bind(fd, (struct sockaddr *)&local, sizeof(local));
226 * Send a request message to the kernel on the Netlink socket.
229 * Netlink socket file descriptor.
231 * The Netlink message send to the kernel.
235 * Pointer to the request structure.
237 * Length of the request in bytes.
240 * The number of sent bytes on success, a negative errno value otherwise and
244 mlx5_nl_request(int nlsk_fd, struct nlmsghdr *nh, uint32_t sn, void *req,
247 struct sockaddr_nl sa = {
248 .nl_family = AF_NETLINK,
250 struct iovec iov[2] = {
251 { .iov_base = nh, .iov_len = sizeof(*nh), },
252 { .iov_base = req, .iov_len = len, },
254 struct msghdr msg = {
256 .msg_namelen = sizeof(sa),
262 nh->nlmsg_pid = 0; /* communication with the kernel uses pid 0 */
264 send_bytes = sendmsg(nlsk_fd, &msg, 0);
265 if (send_bytes < 0) {
273 * Send a message to the kernel on the Netlink socket.
276 * The Netlink socket file descriptor used for communication.
278 * The Netlink message send to the kernel.
283 * The number of sent bytes on success, a negative errno value otherwise and
287 mlx5_nl_send(int nlsk_fd, struct nlmsghdr *nh, uint32_t sn)
289 struct sockaddr_nl sa = {
290 .nl_family = AF_NETLINK,
294 .iov_len = nh->nlmsg_len,
296 struct msghdr msg = {
298 .msg_namelen = sizeof(sa),
304 nh->nlmsg_pid = 0; /* communication with the kernel uses pid 0 */
306 send_bytes = sendmsg(nlsk_fd, &msg, 0);
307 if (send_bytes < 0) {
315 * Receive a message from the kernel on the Netlink socket, following
319 * The Netlink socket file descriptor used for communication.
323 * The callback function to call for each Netlink message received.
324 * @param[in, out] arg
325 * Custom arguments for the callback.
328 * 0 on success, a negative errno value otherwise and rte_errno is set.
331 mlx5_nl_recv(int nlsk_fd, uint32_t sn, int (*cb)(struct nlmsghdr *, void *arg),
334 struct sockaddr_nl sa;
335 void *buf = mlx5_malloc(0, MLX5_RECV_BUF_SIZE, 0, SOCKET_ID_ANY);
338 .iov_len = MLX5_RECV_BUF_SIZE,
340 struct msghdr msg = {
342 .msg_namelen = sizeof(sa),
344 /* One message at a time */
359 recv_bytes = recvmsg(nlsk_fd, &msg, 0);
360 if (recv_bytes == -1) {
365 nh = (struct nlmsghdr *)buf;
366 } while (nh->nlmsg_seq != sn);
368 NLMSG_OK(nh, (unsigned int)recv_bytes);
369 nh = NLMSG_NEXT(nh, recv_bytes)) {
370 if (nh->nlmsg_type == NLMSG_ERROR) {
371 struct nlmsgerr *err_data = NLMSG_DATA(nh);
373 if (err_data->error < 0) {
374 rte_errno = -err_data->error;
382 /* Multi-part msgs and their trailing DONE message. */
383 if (nh->nlmsg_flags & NLM_F_MULTI) {
384 if (nh->nlmsg_type == NLMSG_DONE) {
403 * Parse Netlink message to retrieve the bridge MAC address.
406 * Pointer to Netlink Message Header.
408 * PMD data register with this callback.
411 * 0 on success, a negative errno value otherwise and rte_errno is set.
414 mlx5_nl_mac_addr_cb(struct nlmsghdr *nh, void *arg)
416 struct mlx5_nl_mac_addr *data = arg;
417 struct ndmsg *r = NLMSG_DATA(nh);
418 struct rtattr *attribute;
421 len = nh->nlmsg_len - NLMSG_LENGTH(sizeof(*r));
422 for (attribute = MLX5_NDA_RTA(r);
423 RTA_OK(attribute, len);
424 attribute = RTA_NEXT(attribute, len)) {
425 if (attribute->rta_type == NDA_LLADDR) {
426 if (data->mac_n == MLX5_MAX_MAC_ADDRESSES) {
428 "not enough room to finalize the"
433 #ifdef RTE_LIBRTE_MLX5_DEBUG
434 char m[RTE_ETHER_ADDR_FMT_SIZE];
436 rte_ether_format_addr(m, RTE_ETHER_ADDR_FMT_SIZE,
437 RTA_DATA(attribute));
438 DRV_LOG(DEBUG, "bridge MAC address %s", m);
440 memcpy(&(*data->mac)[data->mac_n++],
441 RTA_DATA(attribute), RTE_ETHER_ADDR_LEN);
448 * Get bridge MAC addresses.
451 * Netlink socket file descriptor.
452 * @param[in] iface_idx
453 * Net device interface index.
455 * Pointer to the array table of MAC addresses to fill.
456 * Its size should be of MLX5_MAX_MAC_ADDRESSES.
458 * Number of entries filled in MAC array.
461 * 0 on success, a negative errno value otherwise and rte_errno is set.
464 mlx5_nl_mac_addr_list(int nlsk_fd, unsigned int iface_idx,
465 struct rte_ether_addr (*mac)[], int *mac_n)
469 struct ifinfomsg ifm;
472 .nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
473 .nlmsg_type = RTM_GETNEIGH,
474 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
477 .ifi_family = PF_BRIDGE,
478 .ifi_index = iface_idx,
481 struct mlx5_nl_mac_addr data = {
485 uint32_t sn = MLX5_NL_SN_GENERATE;
490 ret = mlx5_nl_request(nlsk_fd, &req.hdr, sn, &req.ifm,
491 sizeof(struct ifinfomsg));
494 ret = mlx5_nl_recv(nlsk_fd, sn, mlx5_nl_mac_addr_cb, &data);
500 DRV_LOG(DEBUG, "Interface %u cannot retrieve MAC address list %s",
501 iface_idx, strerror(rte_errno));
506 * Modify the MAC address neighbour table with Netlink.
509 * Netlink socket file descriptor.
510 * @param[in] iface_idx
511 * Net device interface index.
513 * MAC address to consider.
515 * 1 to add the MAC address, 0 to remove the MAC address.
518 * 0 on success, a negative errno value otherwise and rte_errno is set.
521 mlx5_nl_mac_addr_modify(int nlsk_fd, unsigned int iface_idx,
522 struct rte_ether_addr *mac, int add)
528 uint8_t buffer[RTE_ETHER_ADDR_LEN];
531 .nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg)),
532 .nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE |
533 NLM_F_EXCL | NLM_F_ACK,
534 .nlmsg_type = add ? RTM_NEWNEIGH : RTM_DELNEIGH,
537 .ndm_family = PF_BRIDGE,
538 .ndm_state = NUD_NOARP | NUD_PERMANENT,
539 .ndm_ifindex = iface_idx,
540 .ndm_flags = NTF_SELF,
543 .rta_type = NDA_LLADDR,
544 .rta_len = RTA_LENGTH(RTE_ETHER_ADDR_LEN),
547 uint32_t sn = MLX5_NL_SN_GENERATE;
552 memcpy(RTA_DATA(&req.rta), mac, RTE_ETHER_ADDR_LEN);
553 req.hdr.nlmsg_len = NLMSG_ALIGN(req.hdr.nlmsg_len) +
554 RTA_ALIGN(req.rta.rta_len);
555 ret = mlx5_nl_send(nlsk_fd, &req.hdr, sn);
558 ret = mlx5_nl_recv(nlsk_fd, sn, NULL, NULL);
563 #ifdef RTE_LIBRTE_MLX5_DEBUG
565 char m[RTE_ETHER_ADDR_FMT_SIZE];
567 rte_ether_format_addr(m, RTE_ETHER_ADDR_FMT_SIZE, mac);
569 "Interface %u cannot %s MAC address %s %s",
571 add ? "add" : "remove", m, strerror(rte_errno));
578 * Modify the VF MAC address neighbour table with Netlink.
581 * Netlink socket file descriptor.
582 * @param[in] iface_idx
583 * Net device interface index.
585 * MAC address to consider.
590 * 0 on success, a negative errno value otherwise and rte_errno is set.
593 mlx5_nl_vf_mac_addr_modify(int nlsk_fd, unsigned int iface_idx,
594 struct rte_ether_addr *mac, int vf_index)
599 struct ifinfomsg ifm;
600 struct rtattr vf_list_rta;
601 struct rtattr vf_info_rta;
602 struct rtattr vf_mac_rta;
603 struct ifla_vf_mac ivm;
606 .nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
607 .nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
608 .nlmsg_type = RTM_BASE,
611 .ifi_index = iface_idx,
614 .rta_type = IFLA_VFINFO_LIST,
615 .rta_len = RTA_ALIGN(RTA_LENGTH(0)),
618 .rta_type = IFLA_VF_INFO,
619 .rta_len = RTA_ALIGN(RTA_LENGTH(0)),
622 .rta_type = IFLA_VF_MAC,
625 struct ifla_vf_mac ivm = {
628 uint32_t sn = MLX5_NL_SN_GENERATE;
630 memcpy(&ivm.mac, mac, RTE_ETHER_ADDR_LEN);
631 memcpy(RTA_DATA(&req.vf_mac_rta), &ivm, sizeof(ivm));
633 req.vf_mac_rta.rta_len = RTA_LENGTH(sizeof(ivm));
634 req.hdr.nlmsg_len = NLMSG_ALIGN(req.hdr.nlmsg_len) +
635 RTA_ALIGN(req.vf_list_rta.rta_len) +
636 RTA_ALIGN(req.vf_info_rta.rta_len) +
637 RTA_ALIGN(req.vf_mac_rta.rta_len);
638 req.vf_list_rta.rta_len = RTE_PTR_DIFF(NLMSG_TAIL(&req.hdr),
640 req.vf_info_rta.rta_len = RTE_PTR_DIFF(NLMSG_TAIL(&req.hdr),
645 ret = mlx5_nl_send(nlsk_fd, &req.hdr, sn);
648 ret = mlx5_nl_recv(nlsk_fd, sn, NULL, NULL);
654 "representor %u cannot set VF MAC address "
655 "%02X:%02X:%02X:%02X:%02X:%02X : %s",
657 mac->addr_bytes[0], mac->addr_bytes[1],
658 mac->addr_bytes[2], mac->addr_bytes[3],
659 mac->addr_bytes[4], mac->addr_bytes[5],
660 strerror(rte_errno));
668 * Netlink socket file descriptor.
669 * @param[in] iface_idx
670 * Net device interface index.
672 * BITFIELD_DECLARE array to store the mac.
674 * MAC address to register.
679 * 0 on success, a negative errno value otherwise and rte_errno is set.
682 mlx5_nl_mac_addr_add(int nlsk_fd, unsigned int iface_idx,
683 uint64_t *mac_own, struct rte_ether_addr *mac,
688 ret = mlx5_nl_mac_addr_modify(nlsk_fd, iface_idx, mac, 1);
690 MLX5_ASSERT(index < MLX5_MAX_MAC_ADDRESSES);
691 if (index >= MLX5_MAX_MAC_ADDRESSES)
694 BITFIELD_SET(mac_own, index);
702 * Remove a MAC address.
705 * Netlink socket file descriptor.
706 * @param[in] iface_idx
707 * Net device interface index.
709 * BITFIELD_DECLARE array to store the mac.
711 * MAC address to remove.
716 * 0 on success, a negative errno value otherwise and rte_errno is set.
719 mlx5_nl_mac_addr_remove(int nlsk_fd, unsigned int iface_idx, uint64_t *mac_own,
720 struct rte_ether_addr *mac, uint32_t index)
722 MLX5_ASSERT(index < MLX5_MAX_MAC_ADDRESSES);
723 if (index >= MLX5_MAX_MAC_ADDRESSES)
726 BITFIELD_RESET(mac_own, index);
727 return mlx5_nl_mac_addr_modify(nlsk_fd, iface_idx, mac, 0);
731 * Synchronize Netlink bridge table to the internal table.
734 * Netlink socket file descriptor.
735 * @param[in] iface_idx
736 * Net device interface index.
738 * Mac addresses array to sync.
740 * @p mac_addrs array size.
743 mlx5_nl_mac_addr_sync(int nlsk_fd, unsigned int iface_idx,
744 struct rte_ether_addr *mac_addrs, int n)
746 struct rte_ether_addr macs[n];
751 memset(macs, 0, n * sizeof(macs[0]));
752 ret = mlx5_nl_mac_addr_list(nlsk_fd, iface_idx, &macs, &macs_n);
755 for (i = 0; i != macs_n; ++i) {
758 /* Verify the address is not in the array yet. */
759 for (j = 0; j != n; ++j)
760 if (rte_is_same_ether_addr(&macs[i], &mac_addrs[j]))
764 if (rte_is_multicast_ether_addr(&macs[i])) {
765 /* Find the first entry available. */
766 for (j = MLX5_MAX_UC_MAC_ADDRESSES; j != n; ++j) {
767 if (rte_is_zero_ether_addr(&mac_addrs[j])) {
768 mac_addrs[j] = macs[i];
773 /* Find the first entry available. */
774 for (j = 0; j != MLX5_MAX_UC_MAC_ADDRESSES; ++j) {
775 if (rte_is_zero_ether_addr(&mac_addrs[j])) {
776 mac_addrs[j] = macs[i];
785 * Flush all added MAC addresses.
788 * Netlink socket file descriptor.
789 * @param[in] iface_idx
790 * Net device interface index.
791 * @param[in] mac_addrs
792 * Mac addresses array to flush.
794 * @p mac_addrs array size.
796 * BITFIELD_DECLARE array to store the mac.
799 mlx5_nl_mac_addr_flush(int nlsk_fd, unsigned int iface_idx,
800 struct rte_ether_addr *mac_addrs, int n,
805 if (n <= 0 || n > MLX5_MAX_MAC_ADDRESSES)
808 for (i = n - 1; i >= 0; --i) {
809 struct rte_ether_addr *m = &mac_addrs[i];
811 if (BITFIELD_ISSET(mac_own, i))
812 mlx5_nl_mac_addr_remove(nlsk_fd, iface_idx, mac_own, m,
818 * Enable promiscuous / all multicast mode through Netlink.
821 * Netlink socket file descriptor.
822 * @param[in] iface_idx
823 * Net device interface index.
825 * IFF_PROMISC for promiscuous, IFF_ALLMULTI for allmulti.
827 * Nonzero to enable, disable otherwise.
830 * 0 on success, a negative errno value otherwise and rte_errno is set.
833 mlx5_nl_device_flags(int nlsk_fd, unsigned int iface_idx, uint32_t flags,
838 struct ifinfomsg ifi;
841 .nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
842 .nlmsg_type = RTM_NEWLINK,
843 .nlmsg_flags = NLM_F_REQUEST,
846 .ifi_flags = enable ? flags : 0,
848 .ifi_index = iface_idx,
851 uint32_t sn = MLX5_NL_SN_GENERATE;
854 MLX5_ASSERT(!(flags & ~(IFF_PROMISC | IFF_ALLMULTI)));
857 ret = mlx5_nl_send(nlsk_fd, &req.hdr, sn);
864 * Enable promiscuous mode through Netlink.
867 * Netlink socket file descriptor.
868 * @param[in] iface_idx
869 * Net device interface index.
871 * Nonzero to enable, disable otherwise.
874 * 0 on success, a negative errno value otherwise and rte_errno is set.
877 mlx5_nl_promisc(int nlsk_fd, unsigned int iface_idx, int enable)
879 int ret = mlx5_nl_device_flags(nlsk_fd, iface_idx, IFF_PROMISC, enable);
883 "Interface %u cannot %s promisc mode: Netlink error %s",
884 iface_idx, enable ? "enable" : "disable",
885 strerror(rte_errno));
890 * Enable all multicast mode through Netlink.
893 * Netlink socket file descriptor.
894 * @param[in] iface_idx
895 * Net device interface index.
897 * Nonzero to enable, disable otherwise.
900 * 0 on success, a negative errno value otherwise and rte_errno is set.
903 mlx5_nl_allmulti(int nlsk_fd, unsigned int iface_idx, int enable)
905 int ret = mlx5_nl_device_flags(nlsk_fd, iface_idx, IFF_ALLMULTI,
910 "Interface %u cannot %s allmulti : Netlink error %s",
911 iface_idx, enable ? "enable" : "disable",
912 strerror(rte_errno));
917 * Process network interface information from Netlink message.
920 * Pointer to Netlink message header.
922 * Opaque data pointer for this callback.
925 * 0 on success, a negative errno value otherwise and rte_errno is set.
928 mlx5_nl_cmdget_cb(struct nlmsghdr *nh, void *arg)
930 struct mlx5_nl_ifindex_data *data = arg;
931 struct mlx5_nl_ifindex_data local = {
934 size_t off = NLMSG_HDRLEN;
936 if (nh->nlmsg_type !=
937 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET) &&
939 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_PORT_GET))
941 while (off < nh->nlmsg_len) {
942 struct nlattr *na = (void *)((uintptr_t)nh + off);
943 void *payload = (void *)((uintptr_t)na + NLA_HDRLEN);
945 if (na->nla_len > nh->nlmsg_len - off)
947 switch (na->nla_type) {
948 case RDMA_NLDEV_ATTR_DEV_INDEX:
949 local.ibindex = *(uint32_t *)payload;
950 local.flags |= MLX5_NL_CMD_GET_IB_INDEX;
952 case RDMA_NLDEV_ATTR_DEV_NAME:
953 if (!strcmp(payload, data->name))
954 local.flags |= MLX5_NL_CMD_GET_IB_NAME;
956 case RDMA_NLDEV_ATTR_NDEV_INDEX:
957 local.ifindex = *(uint32_t *)payload;
958 local.flags |= MLX5_NL_CMD_GET_NET_INDEX;
960 case RDMA_NLDEV_ATTR_PORT_INDEX:
961 local.portnum = *(uint32_t *)payload;
962 local.flags |= MLX5_NL_CMD_GET_PORT_INDEX;
967 off += NLA_ALIGN(na->nla_len);
970 * It is possible to have multiple messages for all
971 * Infiniband devices in the system with appropriate name.
972 * So we should gather parameters locally and copy to
973 * query context only in case of coinciding device name.
975 if (local.flags & MLX5_NL_CMD_GET_IB_NAME) {
976 data->flags = local.flags;
977 data->ibindex = local.ibindex;
978 data->ifindex = local.ifindex;
979 data->portnum = local.portnum;
988 * Get index of network interface associated with some IB device.
990 * This is the only somewhat safe method to avoid resorting to heuristics
991 * when faced with port representors. Unfortunately it requires at least
995 * Netlink socket of the RDMA kind (NETLINK_RDMA).
999 * IB device port index, starting from 1
1001 * A valid (nonzero) interface index on success, 0 otherwise and rte_errno
1005 mlx5_nl_ifindex(int nl, const char *name, uint32_t pindex)
1007 struct mlx5_nl_ifindex_data data = {
1010 .ibindex = 0, /* Determined during first pass. */
1011 .ifindex = 0, /* Determined during second pass. */
1015 uint8_t buf[NLMSG_HDRLEN +
1016 NLA_HDRLEN + NLA_ALIGN(sizeof(data.ibindex)) +
1017 NLA_HDRLEN + NLA_ALIGN(sizeof(pindex))];
1020 .nlmsg_len = NLMSG_LENGTH(0),
1021 .nlmsg_type = RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1022 RDMA_NLDEV_CMD_GET),
1023 .nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP,
1027 uint32_t sn = MLX5_NL_SN_GENERATE;
1030 ret = mlx5_nl_send(nl, &req.nh, sn);
1033 ret = mlx5_nl_recv(nl, sn, mlx5_nl_cmdget_cb, &data);
1036 if (!(data.flags & MLX5_NL_CMD_GET_IB_NAME) ||
1037 !(data.flags & MLX5_NL_CMD_GET_IB_INDEX))
1040 sn = MLX5_NL_SN_GENERATE;
1041 req.nh.nlmsg_type = RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1042 RDMA_NLDEV_CMD_PORT_GET);
1043 req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1044 req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(req.buf) - NLMSG_HDRLEN);
1045 na = (void *)((uintptr_t)req.buf + NLMSG_HDRLEN);
1046 na->nla_len = NLA_HDRLEN + sizeof(data.ibindex);
1047 na->nla_type = RDMA_NLDEV_ATTR_DEV_INDEX;
1048 memcpy((void *)((uintptr_t)na + NLA_HDRLEN),
1049 &data.ibindex, sizeof(data.ibindex));
1050 na = (void *)((uintptr_t)na + NLA_ALIGN(na->nla_len));
1051 na->nla_len = NLA_HDRLEN + sizeof(pindex);
1052 na->nla_type = RDMA_NLDEV_ATTR_PORT_INDEX;
1053 memcpy((void *)((uintptr_t)na + NLA_HDRLEN),
1054 &pindex, sizeof(pindex));
1055 ret = mlx5_nl_send(nl, &req.nh, sn);
1058 ret = mlx5_nl_recv(nl, sn, mlx5_nl_cmdget_cb, &data);
1061 if (!(data.flags & MLX5_NL_CMD_GET_IB_NAME) ||
1062 !(data.flags & MLX5_NL_CMD_GET_IB_INDEX) ||
1063 !(data.flags & MLX5_NL_CMD_GET_NET_INDEX) ||
1066 return data.ifindex;
1073 * Get the number of physical ports of given IB device.
1076 * Netlink socket of the RDMA kind (NETLINK_RDMA).
1081 * A valid (nonzero) number of ports on success, 0 otherwise
1082 * and rte_errno is set.
1085 mlx5_nl_portnum(int nl, const char *name)
1087 struct mlx5_nl_ifindex_data data = {
1093 struct nlmsghdr req = {
1094 .nlmsg_len = NLMSG_LENGTH(0),
1095 .nlmsg_type = RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1096 RDMA_NLDEV_CMD_GET),
1097 .nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP,
1099 uint32_t sn = MLX5_NL_SN_GENERATE;
1102 ret = mlx5_nl_send(nl, &req, sn);
1105 ret = mlx5_nl_recv(nl, sn, mlx5_nl_cmdget_cb, &data);
1108 if (!(data.flags & MLX5_NL_CMD_GET_IB_NAME) ||
1109 !(data.flags & MLX5_NL_CMD_GET_IB_INDEX) ||
1110 !(data.flags & MLX5_NL_CMD_GET_PORT_INDEX)) {
1116 return data.portnum;
1120 * Analyze gathered port parameters via Netlink to recognize master
1121 * and representor devices for E-Switch configuration.
1123 * @param[in] num_vf_set
1124 * flag of presence of number of VFs port attribute.
1125 * @param[inout] switch_info
1126 * Port information, including port name as a number and port name
1127 * type if recognized
1130 * master and representor flags are set in switch_info according to
1131 * recognized parameters (if any).
1134 mlx5_nl_check_switch_info(bool num_vf_set,
1135 struct mlx5_switch_info *switch_info)
1137 switch (switch_info->name_type) {
1138 case MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN:
1140 * Name is not recognized, assume the master,
1141 * check the number of VFs key presence.
1143 switch_info->master = num_vf_set;
1145 case MLX5_PHYS_PORT_NAME_TYPE_NOTSET:
1147 * Name is not set, this assumes the legacy naming
1148 * schema for master, just check if there is a
1149 * number of VFs key.
1151 switch_info->master = num_vf_set;
1153 case MLX5_PHYS_PORT_NAME_TYPE_UPLINK:
1154 /* New uplink naming schema recognized. */
1155 switch_info->master = 1;
1157 case MLX5_PHYS_PORT_NAME_TYPE_LEGACY:
1158 /* Legacy representors naming schema. */
1159 switch_info->representor = !num_vf_set;
1161 case MLX5_PHYS_PORT_NAME_TYPE_PFHPF:
1163 case MLX5_PHYS_PORT_NAME_TYPE_PFVF:
1165 case MLX5_PHYS_PORT_NAME_TYPE_PFSF:
1166 /* New representors naming schema. */
1167 switch_info->representor = 1;
1173 * Process switch information from Netlink message.
1176 * Pointer to Netlink message header.
1178 * Opaque data pointer for this callback.
1181 * 0 on success, a negative errno value otherwise and rte_errno is set.
1184 mlx5_nl_switch_info_cb(struct nlmsghdr *nh, void *arg)
1186 struct mlx5_switch_info info = {
1189 .name_type = MLX5_PHYS_PORT_NAME_TYPE_NOTSET,
1193 size_t off = NLMSG_LENGTH(sizeof(struct ifinfomsg));
1194 bool switch_id_set = false;
1195 bool num_vf_set = false;
1198 if (nh->nlmsg_type != RTM_NEWLINK)
1200 while (off < nh->nlmsg_len) {
1201 struct rtattr *ra = (void *)((uintptr_t)nh + off);
1202 void *payload = RTA_DATA(ra);
1205 if (ra->rta_len > nh->nlmsg_len - off)
1207 switch (ra->rta_type) {
1211 case IFLA_PHYS_PORT_NAME:
1212 len = RTA_PAYLOAD(ra);
1213 /* Some kernels do not pad attributes with zero. */
1214 if (len > 0 && len < MLX5_PHYS_PORT_NAME_MAX) {
1215 char name[MLX5_PHYS_PORT_NAME_MAX];
1218 * We can't just patch the message with padding
1219 * zero - it might corrupt the following items
1220 * in the message, we have to copy the string
1221 * by attribute length and pad the copied one.
1223 memcpy(name, payload, len);
1225 mlx5_translate_port_name(name, &info);
1228 MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN;
1231 case IFLA_PHYS_SWITCH_ID:
1233 for (i = 0; i < RTA_PAYLOAD(ra); ++i) {
1234 info.switch_id <<= 8;
1235 info.switch_id |= ((uint8_t *)payload)[i];
1237 switch_id_set = true;
1240 off += RTA_ALIGN(ra->rta_len);
1242 if (switch_id_set) {
1243 /* We have some E-Switch configuration. */
1244 mlx5_nl_check_switch_info(num_vf_set, &info);
1246 MLX5_ASSERT(!(info.master && info.representor));
1247 memcpy(arg, &info, sizeof(info));
1255 * Get switch information associated with network interface.
1258 * Netlink socket of the ROUTE kind (NETLINK_ROUTE).
1260 * Network interface index.
1262 * Switch information object, populated in case of success.
1265 * 0 on success, a negative errno value otherwise and rte_errno is set.
1268 mlx5_nl_switch_info(int nl, unsigned int ifindex,
1269 struct mlx5_switch_info *info)
1273 struct ifinfomsg info;
1278 .nlmsg_len = NLMSG_LENGTH
1280 RTA_LENGTH(sizeof(uint32_t))),
1281 .nlmsg_type = RTM_GETLINK,
1282 .nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
1285 .ifi_family = AF_UNSPEC,
1286 .ifi_index = ifindex,
1289 .rta_type = IFLA_EXT_MASK,
1290 .rta_len = RTA_LENGTH(sizeof(int32_t)),
1292 .extmask = RTE_LE32(1),
1294 uint32_t sn = MLX5_NL_SN_GENERATE;
1297 ret = mlx5_nl_send(nl, &req.nh, sn);
1299 ret = mlx5_nl_recv(nl, sn, mlx5_nl_switch_info_cb, info);
1300 if (info->master && info->representor) {
1301 DRV_LOG(ERR, "ifindex %u device is recognized as master"
1302 " and as representor", ifindex);
1310 * Delete VLAN network device by ifindex.
1313 * Context object initialized by mlx5_nl_vlan_vmwa_init().
1314 * @param[in] ifindex
1315 * Interface index of network device to delete.
1318 mlx5_nl_vlan_vmwa_delete(struct mlx5_nl_vlan_vmwa_context *vmwa,
1321 uint32_t sn = MLX5_NL_SN_GENERATE;
1325 struct ifinfomsg info;
1328 .nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
1329 .nlmsg_type = RTM_DELLINK,
1330 .nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
1333 .ifi_family = AF_UNSPEC,
1334 .ifi_index = ifindex,
1339 ret = mlx5_nl_send(vmwa->nl_socket, &req.nh, sn);
1341 ret = mlx5_nl_recv(vmwa->nl_socket, sn, NULL, NULL);
1343 DRV_LOG(WARNING, "netlink: error deleting VLAN WA"
1344 " ifindex %u, %d", ifindex, ret);
1348 /* Set of subroutines to build Netlink message. */
1349 static struct nlattr *
1350 nl_msg_tail(struct nlmsghdr *nlh)
1352 return (struct nlattr *)
1353 (((uint8_t *)nlh) + NLMSG_ALIGN(nlh->nlmsg_len));
1357 nl_attr_put(struct nlmsghdr *nlh, int type, const void *data, int alen)
1359 struct nlattr *nla = nl_msg_tail(nlh);
1361 nla->nla_type = type;
1362 nla->nla_len = NLMSG_ALIGN(sizeof(struct nlattr)) + alen;
1363 nlh->nlmsg_len += NLMSG_ALIGN(nla->nla_len);
1366 memcpy((uint8_t *)nla + sizeof(struct nlattr), data, alen);
1369 static struct nlattr *
1370 nl_attr_nest_start(struct nlmsghdr *nlh, int type)
1372 struct nlattr *nest = (struct nlattr *)nl_msg_tail(nlh);
1374 nl_attr_put(nlh, type, NULL, 0);
1379 nl_attr_nest_end(struct nlmsghdr *nlh, struct nlattr *nest)
1381 nest->nla_len = (uint8_t *)nl_msg_tail(nlh) - (uint8_t *)nest;
1385 * Create network VLAN device with specified VLAN tag.
1388 * Context object initialized by mlx5_nl_vlan_vmwa_init().
1389 * @param[in] ifindex
1390 * Base network interface index.
1392 * VLAN tag for VLAN network device to create.
1395 mlx5_nl_vlan_vmwa_create(struct mlx5_nl_vlan_vmwa_context *vmwa,
1396 uint32_t ifindex, uint16_t tag)
1398 struct nlmsghdr *nlh;
1399 struct ifinfomsg *ifm;
1400 char name[sizeof(MLX5_VMWA_VLAN_DEVICE_PFX) + 32];
1403 uint8_t buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
1404 NLMSG_ALIGN(sizeof(struct ifinfomsg)) +
1405 NLMSG_ALIGN(sizeof(struct nlattr)) * 8 +
1406 NLMSG_ALIGN(sizeof(uint32_t)) +
1407 NLMSG_ALIGN(sizeof(name)) +
1408 NLMSG_ALIGN(sizeof("vlan")) +
1409 NLMSG_ALIGN(sizeof(uint32_t)) +
1410 NLMSG_ALIGN(sizeof(uint16_t)) + 16];
1411 struct nlattr *na_info;
1412 struct nlattr *na_vlan;
1413 uint32_t sn = MLX5_NL_SN_GENERATE;
1416 memset(buf, 0, sizeof(buf));
1417 nlh = (struct nlmsghdr *)buf;
1418 nlh->nlmsg_len = sizeof(struct nlmsghdr);
1419 nlh->nlmsg_type = RTM_NEWLINK;
1420 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE |
1421 NLM_F_EXCL | NLM_F_ACK;
1422 ifm = (struct ifinfomsg *)nl_msg_tail(nlh);
1423 nlh->nlmsg_len += sizeof(struct ifinfomsg);
1424 ifm->ifi_family = AF_UNSPEC;
1427 ifm->ifi_flags = IFF_UP;
1428 ifm->ifi_change = 0xffffffff;
1429 nl_attr_put(nlh, IFLA_LINK, &ifindex, sizeof(ifindex));
1430 ret = snprintf(name, sizeof(name), "%s.%u.%u",
1431 MLX5_VMWA_VLAN_DEVICE_PFX, ifindex, tag);
1432 nl_attr_put(nlh, IFLA_IFNAME, name, ret + 1);
1433 na_info = nl_attr_nest_start(nlh, IFLA_LINKINFO);
1434 nl_attr_put(nlh, IFLA_INFO_KIND, "vlan", sizeof("vlan"));
1435 na_vlan = nl_attr_nest_start(nlh, IFLA_INFO_DATA);
1436 nl_attr_put(nlh, IFLA_VLAN_ID, &tag, sizeof(tag));
1437 nl_attr_nest_end(nlh, na_vlan);
1438 nl_attr_nest_end(nlh, na_info);
1439 MLX5_ASSERT(sizeof(buf) >= nlh->nlmsg_len);
1440 ret = mlx5_nl_send(vmwa->nl_socket, nlh, sn);
1442 ret = mlx5_nl_recv(vmwa->nl_socket, sn, NULL, NULL);
1444 DRV_LOG(WARNING, "netlink: VLAN %s create failure (%d)", name,
1447 /* Try to get ifindex of created or pre-existing device. */
1448 ret = if_nametoindex(name);
1450 DRV_LOG(WARNING, "VLAN %s failed to get index (%d)", name,
1458 * Parse Netlink message to retrieve the general family ID.
1461 * Pointer to Netlink Message Header.
1463 * PMD data register with this callback.
1466 * 0 on success, a negative errno value otherwise and rte_errno is set.
1469 mlx5_nl_family_id_cb(struct nlmsghdr *nh, void *arg)
1472 struct nlattr *tail = RTE_PTR_ADD(nh, nh->nlmsg_len);
1473 struct nlattr *nla = RTE_PTR_ADD(nh, NLMSG_ALIGN(sizeof(*nh)) +
1474 NLMSG_ALIGN(sizeof(struct genlmsghdr)));
1476 for (; nla->nla_len && nla < tail;
1477 nla = RTE_PTR_ADD(nla, NLMSG_ALIGN(nla->nla_len))) {
1478 if (nla->nla_type == CTRL_ATTR_FAMILY_ID) {
1479 *(uint16_t *)arg = *(uint16_t *)(nla + 1);
1486 #define MLX5_NL_MAX_ATTR_SIZE 100
1488 * Get generic netlink family ID.
1490 * @param[in] nlsk_fd
1491 * Netlink socket file descriptor.
1496 * ID >= 0 on success and @p enable is updated, a negative errno value
1497 * otherwise and rte_errno is set.
1500 mlx5_nl_generic_family_id_get(int nlsk_fd, const char *name)
1502 struct nlmsghdr *nlh;
1503 struct genlmsghdr *genl;
1504 uint32_t sn = MLX5_NL_SN_GENERATE;
1505 int name_size = strlen(name) + 1;
1508 uint8_t buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
1509 NLMSG_ALIGN(sizeof(struct genlmsghdr)) +
1510 NLMSG_ALIGN(sizeof(struct nlattr)) +
1511 NLMSG_ALIGN(MLX5_NL_MAX_ATTR_SIZE)];
1513 memset(buf, 0, sizeof(buf));
1514 nlh = (struct nlmsghdr *)buf;
1515 nlh->nlmsg_len = sizeof(struct nlmsghdr);
1516 nlh->nlmsg_type = GENL_ID_CTRL;
1517 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1518 genl = (struct genlmsghdr *)nl_msg_tail(nlh);
1519 nlh->nlmsg_len += sizeof(struct genlmsghdr);
1520 genl->cmd = CTRL_CMD_GETFAMILY;
1522 nl_attr_put(nlh, CTRL_ATTR_FAMILY_NAME, name, name_size);
1523 ret = mlx5_nl_send(nlsk_fd, nlh, sn);
1525 ret = mlx5_nl_recv(nlsk_fd, sn, mlx5_nl_family_id_cb, &id);
1527 DRV_LOG(DEBUG, "Failed to get Netlink %s family ID: %d.", name,
1531 DRV_LOG(DEBUG, "Netlink \"%s\" family ID is %u.", name, id);
1536 * Get Devlink family ID.
1538 * @param[in] nlsk_fd
1539 * Netlink socket file descriptor.
1542 * ID >= 0 on success and @p enable is updated, a negative errno value
1543 * otherwise and rte_errno is set.
1547 mlx5_nl_devlink_family_id_get(int nlsk_fd)
1549 return mlx5_nl_generic_family_id_get(nlsk_fd, DEVLINK_GENL_NAME);
1553 * Parse Netlink message to retrieve the ROCE enable status.
1556 * Pointer to Netlink Message Header.
1558 * PMD data register with this callback.
1561 * 0 on success, a negative errno value otherwise and rte_errno is set.
1564 mlx5_nl_roce_cb(struct nlmsghdr *nh, void *arg)
1569 struct nlattr *tail = RTE_PTR_ADD(nh, nh->nlmsg_len);
1570 struct nlattr *nla = RTE_PTR_ADD(nh, NLMSG_ALIGN(sizeof(*nh)) +
1571 NLMSG_ALIGN(sizeof(struct genlmsghdr)));
1573 while (nla->nla_len && nla < tail) {
1574 switch (nla->nla_type) {
1575 /* Expected nested attributes case. */
1576 case DEVLINK_ATTR_PARAM:
1577 case DEVLINK_ATTR_PARAM_VALUES_LIST:
1578 case DEVLINK_ATTR_PARAM_VALUE:
1582 case DEVLINK_ATTR_PARAM_VALUE_DATA:
1586 nla = RTE_PTR_ADD(nla, NLMSG_ALIGN(nla->nla_len));
1594 * Get ROCE enable status through Netlink.
1596 * @param[in] nlsk_fd
1597 * Netlink socket file descriptor.
1598 * @param[in] family_id
1599 * the Devlink family ID.
1601 * The device PCI address.
1602 * @param[out] enable
1603 * Where to store the enable status.
1606 * 0 on success and @p enable is updated, a negative errno value otherwise
1607 * and rte_errno is set.
1610 mlx5_nl_enable_roce_get(int nlsk_fd, int family_id, const char *pci_addr,
1613 struct nlmsghdr *nlh;
1614 struct genlmsghdr *genl;
1615 uint32_t sn = MLX5_NL_SN_GENERATE;
1618 uint8_t buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
1619 NLMSG_ALIGN(sizeof(struct genlmsghdr)) +
1620 NLMSG_ALIGN(sizeof(struct nlattr)) * 4 +
1621 NLMSG_ALIGN(MLX5_NL_MAX_ATTR_SIZE) * 4];
1623 memset(buf, 0, sizeof(buf));
1624 nlh = (struct nlmsghdr *)buf;
1625 nlh->nlmsg_len = sizeof(struct nlmsghdr);
1626 nlh->nlmsg_type = family_id;
1627 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1628 genl = (struct genlmsghdr *)nl_msg_tail(nlh);
1629 nlh->nlmsg_len += sizeof(struct genlmsghdr);
1630 genl->cmd = DEVLINK_CMD_PARAM_GET;
1631 genl->version = DEVLINK_GENL_VERSION;
1632 nl_attr_put(nlh, DEVLINK_ATTR_BUS_NAME, "pci", 4);
1633 nl_attr_put(nlh, DEVLINK_ATTR_DEV_NAME, pci_addr, strlen(pci_addr) + 1);
1634 nl_attr_put(nlh, DEVLINK_ATTR_PARAM_NAME, "enable_roce", 12);
1635 ret = mlx5_nl_send(nlsk_fd, nlh, sn);
1637 ret = mlx5_nl_recv(nlsk_fd, sn, mlx5_nl_roce_cb, &cur_en);
1639 DRV_LOG(DEBUG, "Failed to get ROCE enable on device %s: %d.",
1644 DRV_LOG(DEBUG, "ROCE is %sabled for device \"%s\".",
1645 cur_en ? "en" : "dis", pci_addr);
1650 * Reload mlx5 device kernel driver through Netlink.
1652 * @param[in] nlsk_fd
1653 * Netlink socket file descriptor.
1654 * @param[in] family_id
1655 * the Devlink family ID.
1657 * The device PCI address.
1658 * @param[out] enable
1659 * The enable status to set.
1662 * 0 on success, a negative errno value otherwise and rte_errno is set.
1665 mlx5_nl_driver_reload(int nlsk_fd, int family_id, const char *pci_addr)
1667 struct nlmsghdr *nlh;
1668 struct genlmsghdr *genl;
1669 uint32_t sn = MLX5_NL_SN_GENERATE;
1671 uint8_t buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
1672 NLMSG_ALIGN(sizeof(struct genlmsghdr)) +
1673 NLMSG_ALIGN(sizeof(struct nlattr)) * 2 +
1674 NLMSG_ALIGN(MLX5_NL_MAX_ATTR_SIZE) * 2];
1676 memset(buf, 0, sizeof(buf));
1677 nlh = (struct nlmsghdr *)buf;
1678 nlh->nlmsg_len = sizeof(struct nlmsghdr);
1679 nlh->nlmsg_type = family_id;
1680 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1681 genl = (struct genlmsghdr *)nl_msg_tail(nlh);
1682 nlh->nlmsg_len += sizeof(struct genlmsghdr);
1683 genl->cmd = DEVLINK_CMD_RELOAD;
1684 genl->version = DEVLINK_GENL_VERSION;
1685 nl_attr_put(nlh, DEVLINK_ATTR_BUS_NAME, "pci", 4);
1686 nl_attr_put(nlh, DEVLINK_ATTR_DEV_NAME, pci_addr, strlen(pci_addr) + 1);
1687 ret = mlx5_nl_send(nlsk_fd, nlh, sn);
1689 ret = mlx5_nl_recv(nlsk_fd, sn, NULL, NULL);
1691 DRV_LOG(DEBUG, "Failed to reload %s device by Netlink - %d",
1695 DRV_LOG(DEBUG, "Device \"%s\" was reloaded by Netlink successfully.",
1701 * Set ROCE enable status through Netlink.
1703 * @param[in] nlsk_fd
1704 * Netlink socket file descriptor.
1705 * @param[in] family_id
1706 * the Devlink family ID.
1708 * The device PCI address.
1709 * @param[out] enable
1710 * The enable status to set.
1713 * 0 on success, a negative errno value otherwise and rte_errno is set.
1716 mlx5_nl_enable_roce_set(int nlsk_fd, int family_id, const char *pci_addr,
1719 struct nlmsghdr *nlh;
1720 struct genlmsghdr *genl;
1721 uint32_t sn = MLX5_NL_SN_GENERATE;
1723 uint8_t buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
1724 NLMSG_ALIGN(sizeof(struct genlmsghdr)) +
1725 NLMSG_ALIGN(sizeof(struct nlattr)) * 6 +
1726 NLMSG_ALIGN(MLX5_NL_MAX_ATTR_SIZE) * 6];
1727 uint8_t cmode = DEVLINK_PARAM_CMODE_DRIVERINIT;
1728 uint8_t ptype = NLA_FLAG;
1731 memset(buf, 0, sizeof(buf));
1732 nlh = (struct nlmsghdr *)buf;
1733 nlh->nlmsg_len = sizeof(struct nlmsghdr);
1734 nlh->nlmsg_type = family_id;
1735 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1736 genl = (struct genlmsghdr *)nl_msg_tail(nlh);
1737 nlh->nlmsg_len += sizeof(struct genlmsghdr);
1738 genl->cmd = DEVLINK_CMD_PARAM_SET;
1739 genl->version = DEVLINK_GENL_VERSION;
1740 nl_attr_put(nlh, DEVLINK_ATTR_BUS_NAME, "pci", 4);
1741 nl_attr_put(nlh, DEVLINK_ATTR_DEV_NAME, pci_addr, strlen(pci_addr) + 1);
1742 nl_attr_put(nlh, DEVLINK_ATTR_PARAM_NAME, "enable_roce", 12);
1743 nl_attr_put(nlh, DEVLINK_ATTR_PARAM_VALUE_CMODE, &cmode, sizeof(cmode));
1744 nl_attr_put(nlh, DEVLINK_ATTR_PARAM_TYPE, &ptype, sizeof(ptype));
1746 nl_attr_put(nlh, DEVLINK_ATTR_PARAM_VALUE_DATA, NULL, 0);
1747 ret = mlx5_nl_send(nlsk_fd, nlh, sn);
1749 ret = mlx5_nl_recv(nlsk_fd, sn, NULL, NULL);
1751 DRV_LOG(DEBUG, "Failed to %sable ROCE for device %s by Netlink:"
1752 " %d.", enable ? "en" : "dis", pci_addr, ret);
1755 DRV_LOG(DEBUG, "Device %s ROCE was %sabled by Netlink successfully.",
1756 pci_addr, enable ? "en" : "dis");
1757 /* Now, need to reload the driver. */
1758 return mlx5_nl_driver_reload(nlsk_fd, family_id, pci_addr);