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>
21 #include <rte_atomic.h>
24 #include "mlx5_common_utils.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
37 /** Parameters of VLAN devices created by driver. */
38 #define MLX5_VMWA_VLAN_DEVICE_PFX "evmlx"
40 * Define NDA_RTA as defined in iproute2 sources.
42 * see in iproute2 sources file include/libnetlink.h
45 #define MLX5_NDA_RTA(r) \
46 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
49 * Define NLMSG_TAIL as defined in iproute2 sources.
51 * see in iproute2 sources file include/libnetlink.h
54 #define NLMSG_TAIL(nmsg) \
55 ((struct rtattr *)(((char *)(nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
58 * The following definitions are normally found in rdma/rdma_netlink.h,
59 * however they are so recent that most systems do not expose them yet.
61 #ifndef HAVE_RDMA_NL_NLDEV
62 #define RDMA_NL_NLDEV 5
64 #ifndef HAVE_RDMA_NLDEV_CMD_GET
65 #define RDMA_NLDEV_CMD_GET 1
67 #ifndef HAVE_RDMA_NLDEV_CMD_PORT_GET
68 #define RDMA_NLDEV_CMD_PORT_GET 5
70 #ifndef HAVE_RDMA_NLDEV_ATTR_DEV_INDEX
71 #define RDMA_NLDEV_ATTR_DEV_INDEX 1
73 #ifndef HAVE_RDMA_NLDEV_ATTR_DEV_NAME
74 #define RDMA_NLDEV_ATTR_DEV_NAME 2
76 #ifndef HAVE_RDMA_NLDEV_ATTR_PORT_INDEX
77 #define RDMA_NLDEV_ATTR_PORT_INDEX 3
79 #ifndef HAVE_RDMA_NLDEV_ATTR_NDEV_INDEX
80 #define RDMA_NLDEV_ATTR_NDEV_INDEX 50
83 /* These are normally found in linux/if_link.h. */
84 #ifndef HAVE_IFLA_NUM_VF
85 #define IFLA_NUM_VF 21
87 #ifndef HAVE_IFLA_EXT_MASK
88 #define IFLA_EXT_MASK 29
90 #ifndef HAVE_IFLA_PHYS_SWITCH_ID
91 #define IFLA_PHYS_SWITCH_ID 36
93 #ifndef HAVE_IFLA_PHYS_PORT_NAME
94 #define IFLA_PHYS_PORT_NAME 38
98 * Some Devlink defines may be missed in old kernel versions,
99 * adjust used defines.
101 #ifndef DEVLINK_GENL_NAME
102 #define DEVLINK_GENL_NAME "devlink"
104 #ifndef DEVLINK_GENL_VERSION
105 #define DEVLINK_GENL_VERSION 1
107 #ifndef DEVLINK_ATTR_BUS_NAME
108 #define DEVLINK_ATTR_BUS_NAME 1
110 #ifndef DEVLINK_ATTR_DEV_NAME
111 #define DEVLINK_ATTR_DEV_NAME 2
113 #ifndef DEVLINK_ATTR_PARAM
114 #define DEVLINK_ATTR_PARAM 80
116 #ifndef DEVLINK_ATTR_PARAM_NAME
117 #define DEVLINK_ATTR_PARAM_NAME 81
119 #ifndef DEVLINK_ATTR_PARAM_TYPE
120 #define DEVLINK_ATTR_PARAM_TYPE 83
122 #ifndef DEVLINK_ATTR_PARAM_VALUES_LIST
123 #define DEVLINK_ATTR_PARAM_VALUES_LIST 84
125 #ifndef DEVLINK_ATTR_PARAM_VALUE
126 #define DEVLINK_ATTR_PARAM_VALUE 85
128 #ifndef DEVLINK_ATTR_PARAM_VALUE_DATA
129 #define DEVLINK_ATTR_PARAM_VALUE_DATA 86
131 #ifndef DEVLINK_ATTR_PARAM_VALUE_CMODE
132 #define DEVLINK_ATTR_PARAM_VALUE_CMODE 87
134 #ifndef DEVLINK_PARAM_CMODE_DRIVERINIT
135 #define DEVLINK_PARAM_CMODE_DRIVERINIT 1
137 #ifndef DEVLINK_CMD_RELOAD
138 #define DEVLINK_CMD_RELOAD 37
140 #ifndef DEVLINK_CMD_PARAM_GET
141 #define DEVLINK_CMD_PARAM_GET 38
143 #ifndef DEVLINK_CMD_PARAM_SET
144 #define DEVLINK_CMD_PARAM_SET 39
150 /* Add/remove MAC address through Netlink */
151 struct mlx5_nl_mac_addr {
152 struct rte_ether_addr (*mac)[];
153 /**< MAC address handled by the device. */
154 int mac_n; /**< Number of addresses in the array. */
157 #define MLX5_NL_CMD_GET_IB_NAME (1 << 0)
158 #define MLX5_NL_CMD_GET_IB_INDEX (1 << 1)
159 #define MLX5_NL_CMD_GET_NET_INDEX (1 << 2)
160 #define MLX5_NL_CMD_GET_PORT_INDEX (1 << 3)
162 /** Data structure used by mlx5_nl_cmdget_cb(). */
163 struct mlx5_nl_ifindex_data {
164 const char *name; /**< IB device name (in). */
165 uint32_t flags; /**< found attribute flags (out). */
166 uint32_t ibindex; /**< IB device index (out). */
167 uint32_t ifindex; /**< Network interface index (out). */
168 uint32_t portnum; /**< IB device max port number (out). */
171 rte_atomic32_t atomic_sn = RTE_ATOMIC32_INIT(0);
173 /* Generate Netlink sequence number. */
174 #define MLX5_NL_SN_GENERATE ((uint32_t)rte_atomic32_add_return(&atomic_sn, 1))
177 * Opens a Netlink socket.
180 * Netlink protocol (e.g. NETLINK_ROUTE, NETLINK_RDMA).
183 * A file descriptor on success, a negative errno value otherwise and
187 mlx5_nl_init(int protocol)
190 int sndbuf_size = MLX5_SEND_BUF_SIZE;
191 int rcvbuf_size = MLX5_RECV_BUF_SIZE;
192 struct sockaddr_nl local = {
193 .nl_family = AF_NETLINK,
197 fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, protocol);
202 ret = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sndbuf_size, sizeof(int));
207 ret = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf_size, sizeof(int));
212 ret = bind(fd, (struct sockaddr *)&local, sizeof(local));
224 * Send a request message to the kernel on the Netlink socket.
227 * Netlink socket file descriptor.
229 * The Netlink message send to the kernel.
233 * Pointer to the request structure.
235 * Length of the request in bytes.
238 * The number of sent bytes on success, a negative errno value otherwise and
242 mlx5_nl_request(int nlsk_fd, struct nlmsghdr *nh, uint32_t sn, void *req,
245 struct sockaddr_nl sa = {
246 .nl_family = AF_NETLINK,
248 struct iovec iov[2] = {
249 { .iov_base = nh, .iov_len = sizeof(*nh), },
250 { .iov_base = req, .iov_len = len, },
252 struct msghdr msg = {
254 .msg_namelen = sizeof(sa),
260 nh->nlmsg_pid = 0; /* communication with the kernel uses pid 0 */
262 send_bytes = sendmsg(nlsk_fd, &msg, 0);
263 if (send_bytes < 0) {
271 * Send a message to the kernel on the Netlink socket.
274 * The Netlink socket file descriptor used for communication.
276 * The Netlink message send to the kernel.
281 * The number of sent bytes on success, a negative errno value otherwise and
285 mlx5_nl_send(int nlsk_fd, struct nlmsghdr *nh, uint32_t sn)
287 struct sockaddr_nl sa = {
288 .nl_family = AF_NETLINK,
292 .iov_len = nh->nlmsg_len,
294 struct msghdr msg = {
296 .msg_namelen = sizeof(sa),
302 nh->nlmsg_pid = 0; /* communication with the kernel uses pid 0 */
304 send_bytes = sendmsg(nlsk_fd, &msg, 0);
305 if (send_bytes < 0) {
313 * Receive a message from the kernel on the Netlink socket, following
317 * The Netlink socket file descriptor used for communication.
321 * The callback function to call for each Netlink message received.
322 * @param[in, out] arg
323 * Custom arguments for the callback.
326 * 0 on success, a negative errno value otherwise and rte_errno is set.
329 mlx5_nl_recv(int nlsk_fd, uint32_t sn, int (*cb)(struct nlmsghdr *, void *arg),
332 struct sockaddr_nl sa;
333 char buf[MLX5_RECV_BUF_SIZE];
336 .iov_len = sizeof(buf),
338 struct msghdr msg = {
340 .msg_namelen = sizeof(sa),
342 /* One message at a time */
353 recv_bytes = recvmsg(nlsk_fd, &msg, 0);
354 if (recv_bytes == -1) {
358 nh = (struct nlmsghdr *)buf;
359 } while (nh->nlmsg_seq != sn);
361 NLMSG_OK(nh, (unsigned int)recv_bytes);
362 nh = NLMSG_NEXT(nh, recv_bytes)) {
363 if (nh->nlmsg_type == NLMSG_ERROR) {
364 struct nlmsgerr *err_data = NLMSG_DATA(nh);
366 if (err_data->error < 0) {
367 rte_errno = -err_data->error;
373 /* Multi-part msgs and their trailing DONE message. */
374 if (nh->nlmsg_flags & NLM_F_MULTI) {
375 if (nh->nlmsg_type == NLMSG_DONE)
390 * Parse Netlink message to retrieve the bridge MAC address.
393 * Pointer to Netlink Message Header.
395 * PMD data register with this callback.
398 * 0 on success, a negative errno value otherwise and rte_errno is set.
401 mlx5_nl_mac_addr_cb(struct nlmsghdr *nh, void *arg)
403 struct mlx5_nl_mac_addr *data = arg;
404 struct ndmsg *r = NLMSG_DATA(nh);
405 struct rtattr *attribute;
408 len = nh->nlmsg_len - NLMSG_LENGTH(sizeof(*r));
409 for (attribute = MLX5_NDA_RTA(r);
410 RTA_OK(attribute, len);
411 attribute = RTA_NEXT(attribute, len)) {
412 if (attribute->rta_type == NDA_LLADDR) {
413 if (data->mac_n == MLX5_MAX_MAC_ADDRESSES) {
415 "not enough room to finalize the"
423 rte_ether_format_addr(m, 18, RTA_DATA(attribute));
424 DRV_LOG(DEBUG, "bridge MAC address %s", m);
426 memcpy(&(*data->mac)[data->mac_n++],
427 RTA_DATA(attribute), RTE_ETHER_ADDR_LEN);
434 * Get bridge MAC addresses.
437 * Netlink socket file descriptor.
438 * @param[in] iface_idx
439 * Net device interface index.
441 * Pointer to the array table of MAC addresses to fill.
442 * Its size should be of MLX5_MAX_MAC_ADDRESSES.
444 * Number of entries filled in MAC array.
447 * 0 on success, a negative errno value otherwise and rte_errno is set.
450 mlx5_nl_mac_addr_list(int nlsk_fd, unsigned int iface_idx,
451 struct rte_ether_addr (*mac)[], int *mac_n)
455 struct ifinfomsg ifm;
458 .nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
459 .nlmsg_type = RTM_GETNEIGH,
460 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
463 .ifi_family = PF_BRIDGE,
464 .ifi_index = iface_idx,
467 struct mlx5_nl_mac_addr data = {
471 uint32_t sn = MLX5_NL_SN_GENERATE;
476 ret = mlx5_nl_request(nlsk_fd, &req.hdr, sn, &req.ifm,
477 sizeof(struct ifinfomsg));
480 ret = mlx5_nl_recv(nlsk_fd, sn, mlx5_nl_mac_addr_cb, &data);
486 DRV_LOG(DEBUG, "Interface %u cannot retrieve MAC address list %s",
487 iface_idx, strerror(rte_errno));
492 * Modify the MAC address neighbour table with Netlink.
495 * Netlink socket file descriptor.
496 * @param[in] iface_idx
497 * Net device interface index.
499 * MAC address to consider.
501 * 1 to add the MAC address, 0 to remove the MAC address.
504 * 0 on success, a negative errno value otherwise and rte_errno is set.
507 mlx5_nl_mac_addr_modify(int nlsk_fd, unsigned int iface_idx,
508 struct rte_ether_addr *mac, int add)
514 uint8_t buffer[RTE_ETHER_ADDR_LEN];
517 .nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg)),
518 .nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE |
519 NLM_F_EXCL | NLM_F_ACK,
520 .nlmsg_type = add ? RTM_NEWNEIGH : RTM_DELNEIGH,
523 .ndm_family = PF_BRIDGE,
524 .ndm_state = NUD_NOARP | NUD_PERMANENT,
525 .ndm_ifindex = iface_idx,
526 .ndm_flags = NTF_SELF,
529 .rta_type = NDA_LLADDR,
530 .rta_len = RTA_LENGTH(RTE_ETHER_ADDR_LEN),
533 uint32_t sn = MLX5_NL_SN_GENERATE;
538 memcpy(RTA_DATA(&req.rta), mac, RTE_ETHER_ADDR_LEN);
539 req.hdr.nlmsg_len = NLMSG_ALIGN(req.hdr.nlmsg_len) +
540 RTA_ALIGN(req.rta.rta_len);
541 ret = mlx5_nl_send(nlsk_fd, &req.hdr, sn);
544 ret = mlx5_nl_recv(nlsk_fd, sn, NULL, NULL);
550 "Interface %u cannot %s MAC address"
551 " %02X:%02X:%02X:%02X:%02X:%02X %s",
553 add ? "add" : "remove",
554 mac->addr_bytes[0], mac->addr_bytes[1],
555 mac->addr_bytes[2], mac->addr_bytes[3],
556 mac->addr_bytes[4], mac->addr_bytes[5],
557 strerror(rte_errno));
562 * Modify the VF MAC address neighbour table with Netlink.
565 * Netlink socket file descriptor.
566 * @param[in] iface_idx
567 * Net device interface index.
569 * MAC address to consider.
574 * 0 on success, a negative errno value otherwise and rte_errno is set.
577 mlx5_nl_vf_mac_addr_modify(int nlsk_fd, unsigned int iface_idx,
578 struct rte_ether_addr *mac, int vf_index)
583 struct ifinfomsg ifm;
584 struct rtattr vf_list_rta;
585 struct rtattr vf_info_rta;
586 struct rtattr vf_mac_rta;
587 struct ifla_vf_mac ivm;
590 .nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
591 .nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
592 .nlmsg_type = RTM_BASE,
595 .ifi_index = iface_idx,
598 .rta_type = IFLA_VFINFO_LIST,
599 .rta_len = RTA_ALIGN(RTA_LENGTH(0)),
602 .rta_type = IFLA_VF_INFO,
603 .rta_len = RTA_ALIGN(RTA_LENGTH(0)),
606 .rta_type = IFLA_VF_MAC,
609 struct ifla_vf_mac ivm = {
612 uint32_t sn = MLX5_NL_SN_GENERATE;
614 memcpy(&ivm.mac, mac, RTE_ETHER_ADDR_LEN);
615 memcpy(RTA_DATA(&req.vf_mac_rta), &ivm, sizeof(ivm));
617 req.vf_mac_rta.rta_len = RTA_LENGTH(sizeof(ivm));
618 req.hdr.nlmsg_len = NLMSG_ALIGN(req.hdr.nlmsg_len) +
619 RTA_ALIGN(req.vf_list_rta.rta_len) +
620 RTA_ALIGN(req.vf_info_rta.rta_len) +
621 RTA_ALIGN(req.vf_mac_rta.rta_len);
622 req.vf_list_rta.rta_len = RTE_PTR_DIFF(NLMSG_TAIL(&req.hdr),
624 req.vf_info_rta.rta_len = RTE_PTR_DIFF(NLMSG_TAIL(&req.hdr),
629 ret = mlx5_nl_send(nlsk_fd, &req.hdr, sn);
632 ret = mlx5_nl_recv(nlsk_fd, sn, NULL, NULL);
638 "representor %u cannot set VF MAC address "
639 "%02X:%02X:%02X:%02X:%02X:%02X : %s",
641 mac->addr_bytes[0], mac->addr_bytes[1],
642 mac->addr_bytes[2], mac->addr_bytes[3],
643 mac->addr_bytes[4], mac->addr_bytes[5],
644 strerror(rte_errno));
652 * Netlink socket file descriptor.
653 * @param[in] iface_idx
654 * Net device interface index.
656 * BITFIELD_DECLARE array to store the mac.
658 * MAC address to register.
663 * 0 on success, a negative errno value otherwise and rte_errno is set.
666 mlx5_nl_mac_addr_add(int nlsk_fd, unsigned int iface_idx,
667 uint64_t *mac_own, struct rte_ether_addr *mac,
672 ret = mlx5_nl_mac_addr_modify(nlsk_fd, iface_idx, mac, 1);
674 BITFIELD_SET(mac_own, index);
681 * Remove a MAC address.
684 * Netlink socket file descriptor.
685 * @param[in] iface_idx
686 * Net device interface index.
688 * BITFIELD_DECLARE array to store the mac.
690 * MAC address to remove.
695 * 0 on success, a negative errno value otherwise and rte_errno is set.
698 mlx5_nl_mac_addr_remove(int nlsk_fd, unsigned int iface_idx, uint64_t *mac_own,
699 struct rte_ether_addr *mac, uint32_t index)
701 BITFIELD_RESET(mac_own, index);
702 return mlx5_nl_mac_addr_modify(nlsk_fd, iface_idx, mac, 0);
706 * Synchronize Netlink bridge table to the internal table.
709 * Netlink socket file descriptor.
710 * @param[in] iface_idx
711 * Net device interface index.
713 * Mac addresses array to sync.
715 * @p mac_addrs array size.
718 mlx5_nl_mac_addr_sync(int nlsk_fd, unsigned int iface_idx,
719 struct rte_ether_addr *mac_addrs, int n)
721 struct rte_ether_addr macs[n];
726 ret = mlx5_nl_mac_addr_list(nlsk_fd, iface_idx, &macs, &macs_n);
729 for (i = 0; i != macs_n; ++i) {
732 /* Verify the address is not in the array yet. */
733 for (j = 0; j != n; ++j)
734 if (rte_is_same_ether_addr(&macs[i], &mac_addrs[j]))
738 /* Find the first entry available. */
739 for (j = 0; j != n; ++j) {
740 if (rte_is_zero_ether_addr(&mac_addrs[j])) {
741 mac_addrs[j] = macs[i];
749 * Flush all added MAC addresses.
752 * Netlink socket file descriptor.
753 * @param[in] iface_idx
754 * Net device interface index.
755 * @param[in] mac_addrs
756 * Mac addresses array to flush.
758 * @p mac_addrs array size.
760 * BITFIELD_DECLARE array to store the mac.
763 mlx5_nl_mac_addr_flush(int nlsk_fd, unsigned int iface_idx,
764 struct rte_ether_addr *mac_addrs, int n,
769 for (i = n - 1; i >= 0; --i) {
770 struct rte_ether_addr *m = &mac_addrs[i];
772 if (BITFIELD_ISSET(mac_own, i))
773 mlx5_nl_mac_addr_remove(nlsk_fd, iface_idx, mac_own, m,
779 * Enable promiscuous / all multicast mode through Netlink.
782 * Netlink socket file descriptor.
783 * @param[in] iface_idx
784 * Net device interface index.
786 * IFF_PROMISC for promiscuous, IFF_ALLMULTI for allmulti.
788 * Nonzero to enable, disable otherwise.
791 * 0 on success, a negative errno value otherwise and rte_errno is set.
794 mlx5_nl_device_flags(int nlsk_fd, unsigned int iface_idx, uint32_t flags,
799 struct ifinfomsg ifi;
802 .nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
803 .nlmsg_type = RTM_NEWLINK,
804 .nlmsg_flags = NLM_F_REQUEST,
807 .ifi_flags = enable ? flags : 0,
809 .ifi_index = iface_idx,
812 uint32_t sn = MLX5_NL_SN_GENERATE;
815 assert(!(flags & ~(IFF_PROMISC | IFF_ALLMULTI)));
818 ret = mlx5_nl_send(nlsk_fd, &req.hdr, sn);
825 * Enable promiscuous mode through Netlink.
828 * Netlink socket file descriptor.
829 * @param[in] iface_idx
830 * Net device interface index.
832 * Nonzero to enable, disable otherwise.
835 * 0 on success, a negative errno value otherwise and rte_errno is set.
838 mlx5_nl_promisc(int nlsk_fd, unsigned int iface_idx, int enable)
840 int ret = mlx5_nl_device_flags(nlsk_fd, iface_idx, IFF_PROMISC, enable);
844 "Interface %u cannot %s promisc mode: Netlink error %s",
845 iface_idx, enable ? "enable" : "disable",
846 strerror(rte_errno));
851 * Enable all multicast mode through Netlink.
854 * Netlink socket file descriptor.
855 * @param[in] iface_idx
856 * Net device interface index.
858 * Nonzero to enable, disable otherwise.
861 * 0 on success, a negative errno value otherwise and rte_errno is set.
864 mlx5_nl_allmulti(int nlsk_fd, unsigned int iface_idx, int enable)
866 int ret = mlx5_nl_device_flags(nlsk_fd, iface_idx, IFF_ALLMULTI,
871 "Interface %u cannot %s allmulti : Netlink error %s",
872 iface_idx, enable ? "enable" : "disable",
873 strerror(rte_errno));
878 * Process network interface information from Netlink message.
881 * Pointer to Netlink message header.
883 * Opaque data pointer for this callback.
886 * 0 on success, a negative errno value otherwise and rte_errno is set.
889 mlx5_nl_cmdget_cb(struct nlmsghdr *nh, void *arg)
891 struct mlx5_nl_ifindex_data *data = arg;
892 struct mlx5_nl_ifindex_data local = {
895 size_t off = NLMSG_HDRLEN;
897 if (nh->nlmsg_type !=
898 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET) &&
900 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_PORT_GET))
902 while (off < nh->nlmsg_len) {
903 struct nlattr *na = (void *)((uintptr_t)nh + off);
904 void *payload = (void *)((uintptr_t)na + NLA_HDRLEN);
906 if (na->nla_len > nh->nlmsg_len - off)
908 switch (na->nla_type) {
909 case RDMA_NLDEV_ATTR_DEV_INDEX:
910 local.ibindex = *(uint32_t *)payload;
911 local.flags |= MLX5_NL_CMD_GET_IB_INDEX;
913 case RDMA_NLDEV_ATTR_DEV_NAME:
914 if (!strcmp(payload, data->name))
915 local.flags |= MLX5_NL_CMD_GET_IB_NAME;
917 case RDMA_NLDEV_ATTR_NDEV_INDEX:
918 local.ifindex = *(uint32_t *)payload;
919 local.flags |= MLX5_NL_CMD_GET_NET_INDEX;
921 case RDMA_NLDEV_ATTR_PORT_INDEX:
922 local.portnum = *(uint32_t *)payload;
923 local.flags |= MLX5_NL_CMD_GET_PORT_INDEX;
928 off += NLA_ALIGN(na->nla_len);
931 * It is possible to have multiple messages for all
932 * Infiniband devices in the system with appropriate name.
933 * So we should gather parameters locally and copy to
934 * query context only in case of coinciding device name.
936 if (local.flags & MLX5_NL_CMD_GET_IB_NAME) {
937 data->flags = local.flags;
938 data->ibindex = local.ibindex;
939 data->ifindex = local.ifindex;
940 data->portnum = local.portnum;
949 * Get index of network interface associated with some IB device.
951 * This is the only somewhat safe method to avoid resorting to heuristics
952 * when faced with port representors. Unfortunately it requires at least
956 * Netlink socket of the RDMA kind (NETLINK_RDMA).
960 * IB device port index, starting from 1
962 * A valid (nonzero) interface index on success, 0 otherwise and rte_errno
966 mlx5_nl_ifindex(int nl, const char *name, uint32_t pindex)
968 struct mlx5_nl_ifindex_data data = {
971 .ibindex = 0, /* Determined during first pass. */
972 .ifindex = 0, /* Determined during second pass. */
976 uint8_t buf[NLMSG_HDRLEN +
977 NLA_HDRLEN + NLA_ALIGN(sizeof(data.ibindex)) +
978 NLA_HDRLEN + NLA_ALIGN(sizeof(pindex))];
981 .nlmsg_len = NLMSG_LENGTH(0),
982 .nlmsg_type = RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
984 .nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP,
988 uint32_t sn = MLX5_NL_SN_GENERATE;
991 ret = mlx5_nl_send(nl, &req.nh, sn);
994 ret = mlx5_nl_recv(nl, sn, mlx5_nl_cmdget_cb, &data);
997 if (!(data.flags & MLX5_NL_CMD_GET_IB_NAME) ||
998 !(data.flags & MLX5_NL_CMD_GET_IB_INDEX))
1001 sn = MLX5_NL_SN_GENERATE;
1002 req.nh.nlmsg_type = RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1003 RDMA_NLDEV_CMD_PORT_GET);
1004 req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1005 req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(req.buf) - NLMSG_HDRLEN);
1006 na = (void *)((uintptr_t)req.buf + NLMSG_HDRLEN);
1007 na->nla_len = NLA_HDRLEN + sizeof(data.ibindex);
1008 na->nla_type = RDMA_NLDEV_ATTR_DEV_INDEX;
1009 memcpy((void *)((uintptr_t)na + NLA_HDRLEN),
1010 &data.ibindex, sizeof(data.ibindex));
1011 na = (void *)((uintptr_t)na + NLA_ALIGN(na->nla_len));
1012 na->nla_len = NLA_HDRLEN + sizeof(pindex);
1013 na->nla_type = RDMA_NLDEV_ATTR_PORT_INDEX;
1014 memcpy((void *)((uintptr_t)na + NLA_HDRLEN),
1015 &pindex, sizeof(pindex));
1016 ret = mlx5_nl_send(nl, &req.nh, sn);
1019 ret = mlx5_nl_recv(nl, sn, mlx5_nl_cmdget_cb, &data);
1022 if (!(data.flags & MLX5_NL_CMD_GET_IB_NAME) ||
1023 !(data.flags & MLX5_NL_CMD_GET_IB_INDEX) ||
1024 !(data.flags & MLX5_NL_CMD_GET_NET_INDEX) ||
1027 return data.ifindex;
1034 * Get the number of physical ports of given IB device.
1037 * Netlink socket of the RDMA kind (NETLINK_RDMA).
1042 * A valid (nonzero) number of ports on success, 0 otherwise
1043 * and rte_errno is set.
1046 mlx5_nl_portnum(int nl, const char *name)
1048 struct mlx5_nl_ifindex_data data = {
1054 struct nlmsghdr req = {
1055 .nlmsg_len = NLMSG_LENGTH(0),
1056 .nlmsg_type = RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1057 RDMA_NLDEV_CMD_GET),
1058 .nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP,
1060 uint32_t sn = MLX5_NL_SN_GENERATE;
1063 ret = mlx5_nl_send(nl, &req, sn);
1066 ret = mlx5_nl_recv(nl, sn, mlx5_nl_cmdget_cb, &data);
1069 if (!(data.flags & MLX5_NL_CMD_GET_IB_NAME) ||
1070 !(data.flags & MLX5_NL_CMD_GET_IB_INDEX) ||
1071 !(data.flags & MLX5_NL_CMD_GET_PORT_INDEX)) {
1077 return data.portnum;
1081 * Analyze gathered port parameters via Netlink to recognize master
1082 * and representor devices for E-Switch configuration.
1084 * @param[in] num_vf_set
1085 * flag of presence of number of VFs port attribute.
1086 * @param[inout] switch_info
1087 * Port information, including port name as a number and port name
1088 * type if recognized
1091 * master and representor flags are set in switch_info according to
1092 * recognized parameters (if any).
1095 mlx5_nl_check_switch_info(bool num_vf_set,
1096 struct mlx5_switch_info *switch_info)
1098 switch (switch_info->name_type) {
1099 case MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN:
1101 * Name is not recognized, assume the master,
1102 * check the number of VFs key presence.
1104 switch_info->master = num_vf_set;
1106 case MLX5_PHYS_PORT_NAME_TYPE_NOTSET:
1108 * Name is not set, this assumes the legacy naming
1109 * schema for master, just check if there is a
1110 * number of VFs key.
1112 switch_info->master = num_vf_set;
1114 case MLX5_PHYS_PORT_NAME_TYPE_UPLINK:
1115 /* New uplink naming schema recognized. */
1116 switch_info->master = 1;
1118 case MLX5_PHYS_PORT_NAME_TYPE_LEGACY:
1119 /* Legacy representors naming schema. */
1120 switch_info->representor = !num_vf_set;
1122 case MLX5_PHYS_PORT_NAME_TYPE_PFVF:
1123 /* New representors naming schema. */
1124 switch_info->representor = 1;
1130 * Process switch information from Netlink message.
1133 * Pointer to Netlink message header.
1135 * Opaque data pointer for this callback.
1138 * 0 on success, a negative errno value otherwise and rte_errno is set.
1141 mlx5_nl_switch_info_cb(struct nlmsghdr *nh, void *arg)
1143 struct mlx5_switch_info info = {
1146 .name_type = MLX5_PHYS_PORT_NAME_TYPE_NOTSET,
1150 size_t off = NLMSG_LENGTH(sizeof(struct ifinfomsg));
1151 bool switch_id_set = false;
1152 bool num_vf_set = false;
1154 if (nh->nlmsg_type != RTM_NEWLINK)
1156 while (off < nh->nlmsg_len) {
1157 struct rtattr *ra = (void *)((uintptr_t)nh + off);
1158 void *payload = RTA_DATA(ra);
1161 if (ra->rta_len > nh->nlmsg_len - off)
1163 switch (ra->rta_type) {
1167 case IFLA_PHYS_PORT_NAME:
1168 mlx5_translate_port_name((char *)payload, &info);
1170 case IFLA_PHYS_SWITCH_ID:
1172 for (i = 0; i < RTA_PAYLOAD(ra); ++i) {
1173 info.switch_id <<= 8;
1174 info.switch_id |= ((uint8_t *)payload)[i];
1176 switch_id_set = true;
1179 off += RTA_ALIGN(ra->rta_len);
1181 if (switch_id_set) {
1182 /* We have some E-Switch configuration. */
1183 mlx5_nl_check_switch_info(num_vf_set, &info);
1185 assert(!(info.master && info.representor));
1186 memcpy(arg, &info, sizeof(info));
1194 * Get switch information associated with network interface.
1197 * Netlink socket of the ROUTE kind (NETLINK_ROUTE).
1199 * Network interface index.
1201 * Switch information object, populated in case of success.
1204 * 0 on success, a negative errno value otherwise and rte_errno is set.
1207 mlx5_nl_switch_info(int nl, unsigned int ifindex,
1208 struct mlx5_switch_info *info)
1212 struct ifinfomsg info;
1217 .nlmsg_len = NLMSG_LENGTH
1219 RTA_LENGTH(sizeof(uint32_t))),
1220 .nlmsg_type = RTM_GETLINK,
1221 .nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
1224 .ifi_family = AF_UNSPEC,
1225 .ifi_index = ifindex,
1228 .rta_type = IFLA_EXT_MASK,
1229 .rta_len = RTA_LENGTH(sizeof(int32_t)),
1231 .extmask = RTE_LE32(1),
1233 uint32_t sn = MLX5_NL_SN_GENERATE;
1236 ret = mlx5_nl_send(nl, &req.nh, sn);
1238 ret = mlx5_nl_recv(nl, sn, mlx5_nl_switch_info_cb, info);
1239 if (info->master && info->representor) {
1240 DRV_LOG(ERR, "ifindex %u device is recognized as master"
1241 " and as representor", ifindex);
1249 * Delete VLAN network device by ifindex.
1252 * Context object initialized by mlx5_nl_vlan_vmwa_init().
1253 * @param[in] ifindex
1254 * Interface index of network device to delete.
1257 mlx5_nl_vlan_vmwa_delete(struct mlx5_nl_vlan_vmwa_context *vmwa,
1260 uint32_t sn = MLX5_NL_SN_GENERATE;
1264 struct ifinfomsg info;
1267 .nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
1268 .nlmsg_type = RTM_DELLINK,
1269 .nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
1272 .ifi_family = AF_UNSPEC,
1273 .ifi_index = ifindex,
1278 ret = mlx5_nl_send(vmwa->nl_socket, &req.nh, sn);
1280 ret = mlx5_nl_recv(vmwa->nl_socket, sn, NULL, NULL);
1282 DRV_LOG(WARNING, "netlink: error deleting VLAN WA"
1283 " ifindex %u, %d", ifindex, ret);
1287 /* Set of subroutines to build Netlink message. */
1288 static struct nlattr *
1289 nl_msg_tail(struct nlmsghdr *nlh)
1291 return (struct nlattr *)
1292 (((uint8_t *)nlh) + NLMSG_ALIGN(nlh->nlmsg_len));
1296 nl_attr_put(struct nlmsghdr *nlh, int type, const void *data, int alen)
1298 struct nlattr *nla = nl_msg_tail(nlh);
1300 nla->nla_type = type;
1301 nla->nla_len = NLMSG_ALIGN(sizeof(struct nlattr)) + alen;
1302 nlh->nlmsg_len += NLMSG_ALIGN(nla->nla_len);
1305 memcpy((uint8_t *)nla + sizeof(struct nlattr), data, alen);
1308 static struct nlattr *
1309 nl_attr_nest_start(struct nlmsghdr *nlh, int type)
1311 struct nlattr *nest = (struct nlattr *)nl_msg_tail(nlh);
1313 nl_attr_put(nlh, type, NULL, 0);
1318 nl_attr_nest_end(struct nlmsghdr *nlh, struct nlattr *nest)
1320 nest->nla_len = (uint8_t *)nl_msg_tail(nlh) - (uint8_t *)nest;
1324 * Create network VLAN device with specified VLAN tag.
1327 * Context object initialized by mlx5_nl_vlan_vmwa_init().
1328 * @param[in] ifindex
1329 * Base network interface index.
1331 * VLAN tag for VLAN network device to create.
1334 mlx5_nl_vlan_vmwa_create(struct mlx5_nl_vlan_vmwa_context *vmwa,
1335 uint32_t ifindex, uint16_t tag)
1337 struct nlmsghdr *nlh;
1338 struct ifinfomsg *ifm;
1339 char name[sizeof(MLX5_VMWA_VLAN_DEVICE_PFX) + 32];
1341 alignas(RTE_CACHE_LINE_SIZE)
1342 uint8_t buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
1343 NLMSG_ALIGN(sizeof(struct ifinfomsg)) +
1344 NLMSG_ALIGN(sizeof(struct nlattr)) * 8 +
1345 NLMSG_ALIGN(sizeof(uint32_t)) +
1346 NLMSG_ALIGN(sizeof(name)) +
1347 NLMSG_ALIGN(sizeof("vlan")) +
1348 NLMSG_ALIGN(sizeof(uint32_t)) +
1349 NLMSG_ALIGN(sizeof(uint16_t)) + 16];
1350 struct nlattr *na_info;
1351 struct nlattr *na_vlan;
1352 uint32_t sn = MLX5_NL_SN_GENERATE;
1355 memset(buf, 0, sizeof(buf));
1356 nlh = (struct nlmsghdr *)buf;
1357 nlh->nlmsg_len = sizeof(struct nlmsghdr);
1358 nlh->nlmsg_type = RTM_NEWLINK;
1359 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE |
1360 NLM_F_EXCL | NLM_F_ACK;
1361 ifm = (struct ifinfomsg *)nl_msg_tail(nlh);
1362 nlh->nlmsg_len += sizeof(struct ifinfomsg);
1363 ifm->ifi_family = AF_UNSPEC;
1366 ifm->ifi_flags = IFF_UP;
1367 ifm->ifi_change = 0xffffffff;
1368 nl_attr_put(nlh, IFLA_LINK, &ifindex, sizeof(ifindex));
1369 ret = snprintf(name, sizeof(name), "%s.%u.%u",
1370 MLX5_VMWA_VLAN_DEVICE_PFX, ifindex, tag);
1371 nl_attr_put(nlh, IFLA_IFNAME, name, ret + 1);
1372 na_info = nl_attr_nest_start(nlh, IFLA_LINKINFO);
1373 nl_attr_put(nlh, IFLA_INFO_KIND, "vlan", sizeof("vlan"));
1374 na_vlan = nl_attr_nest_start(nlh, IFLA_INFO_DATA);
1375 nl_attr_put(nlh, IFLA_VLAN_ID, &tag, sizeof(tag));
1376 nl_attr_nest_end(nlh, na_vlan);
1377 nl_attr_nest_end(nlh, na_info);
1378 assert(sizeof(buf) >= nlh->nlmsg_len);
1379 ret = mlx5_nl_send(vmwa->nl_socket, nlh, sn);
1381 ret = mlx5_nl_recv(vmwa->nl_socket, sn, NULL, NULL);
1383 DRV_LOG(WARNING, "netlink: VLAN %s create failure (%d)", name,
1386 // Try to get ifindex of created or pre-existing device.
1387 ret = if_nametoindex(name);
1389 DRV_LOG(WARNING, "VLAN %s failed to get index (%d)", name,
1397 * Parse Netlink message to retrieve the general family ID.
1400 * Pointer to Netlink Message Header.
1402 * PMD data register with this callback.
1405 * 0 on success, a negative errno value otherwise and rte_errno is set.
1408 mlx5_nl_family_id_cb(struct nlmsghdr *nh, void *arg)
1411 struct nlattr *tail = RTE_PTR_ADD(nh, nh->nlmsg_len);
1412 struct nlattr *nla = RTE_PTR_ADD(nh, NLMSG_ALIGN(sizeof(*nh)) +
1413 NLMSG_ALIGN(sizeof(struct genlmsghdr)));
1415 for (; nla->nla_len && nla < tail;
1416 nla = RTE_PTR_ADD(nla, NLMSG_ALIGN(nla->nla_len))) {
1417 if (nla->nla_type == CTRL_ATTR_FAMILY_ID) {
1418 *(uint16_t *)arg = *(uint16_t *)(nla + 1);
1425 #define MLX5_NL_MAX_ATTR_SIZE 100
1427 * Get generic netlink family ID.
1429 * @param[in] nlsk_fd
1430 * Netlink socket file descriptor.
1435 * ID >= 0 on success and @p enable is updated, a negative errno value
1436 * otherwise and rte_errno is set.
1439 mlx5_nl_generic_family_id_get(int nlsk_fd, const char *name)
1441 struct nlmsghdr *nlh;
1442 struct genlmsghdr *genl;
1443 uint32_t sn = MLX5_NL_SN_GENERATE;
1444 int name_size = strlen(name) + 1;
1447 uint8_t buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
1448 NLMSG_ALIGN(sizeof(struct genlmsghdr)) +
1449 NLMSG_ALIGN(sizeof(struct nlattr)) +
1450 NLMSG_ALIGN(MLX5_NL_MAX_ATTR_SIZE)];
1452 memset(buf, 0, sizeof(buf));
1453 nlh = (struct nlmsghdr *)buf;
1454 nlh->nlmsg_len = sizeof(struct nlmsghdr);
1455 nlh->nlmsg_type = GENL_ID_CTRL;
1456 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1457 genl = (struct genlmsghdr *)nl_msg_tail(nlh);
1458 nlh->nlmsg_len += sizeof(struct genlmsghdr);
1459 genl->cmd = CTRL_CMD_GETFAMILY;
1461 nl_attr_put(nlh, CTRL_ATTR_FAMILY_NAME, name, name_size);
1462 ret = mlx5_nl_send(nlsk_fd, nlh, sn);
1464 ret = mlx5_nl_recv(nlsk_fd, sn, mlx5_nl_family_id_cb, &id);
1466 DRV_LOG(DEBUG, "Failed to get Netlink %s family ID: %d.", name,
1470 DRV_LOG(DEBUG, "Netlink \"%s\" family ID is %u.", name, id);
1475 * Get Devlink family ID.
1477 * @param[in] nlsk_fd
1478 * Netlink socket file descriptor.
1481 * ID >= 0 on success and @p enable is updated, a negative errno value
1482 * otherwise and rte_errno is set.
1486 mlx5_nl_devlink_family_id_get(int nlsk_fd)
1488 return mlx5_nl_generic_family_id_get(nlsk_fd, DEVLINK_GENL_NAME);
1492 * Parse Netlink message to retrieve the ROCE enable status.
1495 * Pointer to Netlink Message Header.
1497 * PMD data register with this callback.
1500 * 0 on success, a negative errno value otherwise and rte_errno is set.
1503 mlx5_nl_roce_cb(struct nlmsghdr *nh, void *arg)
1508 struct nlattr *tail = RTE_PTR_ADD(nh, nh->nlmsg_len);
1509 struct nlattr *nla = RTE_PTR_ADD(nh, NLMSG_ALIGN(sizeof(*nh)) +
1510 NLMSG_ALIGN(sizeof(struct genlmsghdr)));
1512 while (nla->nla_len && nla < tail) {
1513 switch (nla->nla_type) {
1514 /* Expected nested attributes case. */
1515 case DEVLINK_ATTR_PARAM:
1516 case DEVLINK_ATTR_PARAM_VALUES_LIST:
1517 case DEVLINK_ATTR_PARAM_VALUE:
1521 case DEVLINK_ATTR_PARAM_VALUE_DATA:
1525 nla = RTE_PTR_ADD(nla, NLMSG_ALIGN(nla->nla_len));
1533 * Get ROCE enable status through Netlink.
1535 * @param[in] nlsk_fd
1536 * Netlink socket file descriptor.
1537 * @param[in] family_id
1538 * the Devlink family ID.
1540 * The device PCI address.
1541 * @param[out] enable
1542 * Where to store the enable status.
1545 * 0 on success and @p enable is updated, a negative errno value otherwise
1546 * and rte_errno is set.
1549 mlx5_nl_enable_roce_get(int nlsk_fd, int family_id, const char *pci_addr,
1552 struct nlmsghdr *nlh;
1553 struct genlmsghdr *genl;
1554 uint32_t sn = MLX5_NL_SN_GENERATE;
1557 uint8_t buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
1558 NLMSG_ALIGN(sizeof(struct genlmsghdr)) +
1559 NLMSG_ALIGN(sizeof(struct nlattr)) * 4 +
1560 NLMSG_ALIGN(MLX5_NL_MAX_ATTR_SIZE) * 4];
1562 memset(buf, 0, sizeof(buf));
1563 nlh = (struct nlmsghdr *)buf;
1564 nlh->nlmsg_len = sizeof(struct nlmsghdr);
1565 nlh->nlmsg_type = family_id;
1566 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1567 genl = (struct genlmsghdr *)nl_msg_tail(nlh);
1568 nlh->nlmsg_len += sizeof(struct genlmsghdr);
1569 genl->cmd = DEVLINK_CMD_PARAM_GET;
1570 genl->version = DEVLINK_GENL_VERSION;
1571 nl_attr_put(nlh, DEVLINK_ATTR_BUS_NAME, "pci", 4);
1572 nl_attr_put(nlh, DEVLINK_ATTR_DEV_NAME, pci_addr, strlen(pci_addr) + 1);
1573 nl_attr_put(nlh, DEVLINK_ATTR_PARAM_NAME, "enable_roce", 12);
1574 ret = mlx5_nl_send(nlsk_fd, nlh, sn);
1576 ret = mlx5_nl_recv(nlsk_fd, sn, mlx5_nl_roce_cb, &cur_en);
1578 DRV_LOG(DEBUG, "Failed to get ROCE enable on device %s: %d.",
1583 DRV_LOG(DEBUG, "ROCE is %sabled for device \"%s\".",
1584 cur_en ? "en" : "dis", pci_addr);
1589 * Reload mlx5 device kernel driver through Netlink.
1591 * @param[in] nlsk_fd
1592 * Netlink socket file descriptor.
1593 * @param[in] family_id
1594 * the Devlink family ID.
1596 * The device PCI address.
1597 * @param[out] enable
1598 * The enable status to set.
1601 * 0 on success, a negative errno value otherwise and rte_errno is set.
1604 mlx5_nl_driver_reload(int nlsk_fd, int family_id, const char *pci_addr)
1606 struct nlmsghdr *nlh;
1607 struct genlmsghdr *genl;
1608 uint32_t sn = MLX5_NL_SN_GENERATE;
1610 uint8_t buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
1611 NLMSG_ALIGN(sizeof(struct genlmsghdr)) +
1612 NLMSG_ALIGN(sizeof(struct nlattr)) * 2 +
1613 NLMSG_ALIGN(MLX5_NL_MAX_ATTR_SIZE) * 2];
1615 memset(buf, 0, sizeof(buf));
1616 nlh = (struct nlmsghdr *)buf;
1617 nlh->nlmsg_len = sizeof(struct nlmsghdr);
1618 nlh->nlmsg_type = family_id;
1619 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1620 genl = (struct genlmsghdr *)nl_msg_tail(nlh);
1621 nlh->nlmsg_len += sizeof(struct genlmsghdr);
1622 genl->cmd = DEVLINK_CMD_RELOAD;
1623 genl->version = DEVLINK_GENL_VERSION;
1624 nl_attr_put(nlh, DEVLINK_ATTR_BUS_NAME, "pci", 4);
1625 nl_attr_put(nlh, DEVLINK_ATTR_DEV_NAME, pci_addr, strlen(pci_addr) + 1);
1626 ret = mlx5_nl_send(nlsk_fd, nlh, sn);
1628 ret = mlx5_nl_recv(nlsk_fd, sn, NULL, NULL);
1630 DRV_LOG(DEBUG, "Failed to reload %s device by Netlink - %d",
1634 DRV_LOG(DEBUG, "Device \"%s\" was reloaded by Netlink successfully.",
1640 * Set ROCE enable status through Netlink.
1642 * @param[in] nlsk_fd
1643 * Netlink socket file descriptor.
1644 * @param[in] family_id
1645 * the Devlink family ID.
1647 * The device PCI address.
1648 * @param[out] enable
1649 * The enable status to set.
1652 * 0 on success, a negative errno value otherwise and rte_errno is set.
1655 mlx5_nl_enable_roce_set(int nlsk_fd, int family_id, const char *pci_addr,
1658 struct nlmsghdr *nlh;
1659 struct genlmsghdr *genl;
1660 uint32_t sn = MLX5_NL_SN_GENERATE;
1662 uint8_t buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
1663 NLMSG_ALIGN(sizeof(struct genlmsghdr)) +
1664 NLMSG_ALIGN(sizeof(struct nlattr)) * 6 +
1665 NLMSG_ALIGN(MLX5_NL_MAX_ATTR_SIZE) * 6];
1666 uint8_t cmode = DEVLINK_PARAM_CMODE_DRIVERINIT;
1667 uint8_t ptype = NLA_FLAG;
1670 memset(buf, 0, sizeof(buf));
1671 nlh = (struct nlmsghdr *)buf;
1672 nlh->nlmsg_len = sizeof(struct nlmsghdr);
1673 nlh->nlmsg_type = family_id;
1674 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1675 genl = (struct genlmsghdr *)nl_msg_tail(nlh);
1676 nlh->nlmsg_len += sizeof(struct genlmsghdr);
1677 genl->cmd = DEVLINK_CMD_PARAM_SET;
1678 genl->version = DEVLINK_GENL_VERSION;
1679 nl_attr_put(nlh, DEVLINK_ATTR_BUS_NAME, "pci", 4);
1680 nl_attr_put(nlh, DEVLINK_ATTR_DEV_NAME, pci_addr, strlen(pci_addr) + 1);
1681 nl_attr_put(nlh, DEVLINK_ATTR_PARAM_NAME, "enable_roce", 12);
1682 nl_attr_put(nlh, DEVLINK_ATTR_PARAM_VALUE_CMODE, &cmode, sizeof(cmode));
1683 nl_attr_put(nlh, DEVLINK_ATTR_PARAM_TYPE, &ptype, sizeof(ptype));
1685 nl_attr_put(nlh, DEVLINK_ATTR_PARAM_VALUE_DATA, NULL, 0);
1686 ret = mlx5_nl_send(nlsk_fd, nlh, sn);
1688 ret = mlx5_nl_recv(nlsk_fd, sn, NULL, NULL);
1690 DRV_LOG(DEBUG, "Failed to %sable ROCE for device %s by Netlink:"
1691 " %d.", enable ? "en" : "dis", pci_addr, ret);
1694 DRV_LOG(DEBUG, "Device %s ROCE was %sabled by Netlink successfully.",
1695 pci_addr, enable ? "en" : "dis");
1696 /* Now, need to reload the driver. */
1697 return mlx5_nl_driver_reload(nlsk_fd, family_id, pci_addr);