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/netlink.h>
9 #include <linux/rtnetlink.h>
11 #include <rdma/rdma_netlink.h>
16 #include <sys/socket.h>
19 #include <rte_errno.h>
22 #include "mlx5_utils.h"
24 /* Size of the buffer to receive kernel messages */
25 #define MLX5_NL_BUF_SIZE (32 * 1024)
26 /* Send buffer size for the Netlink socket */
27 #define MLX5_SEND_BUF_SIZE 32768
28 /* Receive buffer size for the Netlink socket */
29 #define MLX5_RECV_BUF_SIZE 32768
32 * Define NDA_RTA as defined in iproute2 sources.
34 * see in iproute2 sources file include/libnetlink.h
37 #define MLX5_NDA_RTA(r) \
38 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
42 * The following definitions are normally found in rdma/rdma_netlink.h,
43 * however they are so recent that most systems do not expose them yet.
45 #ifndef HAVE_RDMA_NL_NLDEV
46 #define RDMA_NL_NLDEV 5
48 #ifndef HAVE_RDMA_NLDEV_CMD_GET
49 #define RDMA_NLDEV_CMD_GET 1
51 #ifndef HAVE_RDMA_NLDEV_CMD_PORT_GET
52 #define RDMA_NLDEV_CMD_PORT_GET 5
54 #ifndef HAVE_RDMA_NLDEV_ATTR_DEV_INDEX
55 #define RDMA_NLDEV_ATTR_DEV_INDEX 1
57 #ifndef HAVE_RDMA_NLDEV_ATTR_DEV_NAME
58 #define RDMA_NLDEV_ATTR_DEV_NAME 2
60 #ifndef HAVE_RDMA_NLDEV_ATTR_PORT_INDEX
61 #define RDMA_NLDEV_ATTR_PORT_INDEX 3
63 #ifndef HAVE_RDMA_NLDEV_ATTR_NDEV_INDEX
64 #define RDMA_NLDEV_ATTR_NDEV_INDEX 50
67 /* These are normally found in linux/if_link.h. */
68 #ifndef HAVE_IFLA_PHYS_SWITCH_ID
69 #define IFLA_PHYS_SWITCH_ID 36
71 #ifndef HAVE_IFLA_PHYS_PORT_NAME
72 #define IFLA_PHYS_PORT_NAME 38
75 /* Add/remove MAC address through Netlink */
76 struct mlx5_nl_mac_addr {
77 struct ether_addr (*mac)[];
78 /**< MAC address handled by the device. */
79 int mac_n; /**< Number of addresses in the array. */
82 /** Data structure used by mlx5_nl_ifindex_cb(). */
83 struct mlx5_nl_ifindex_data {
84 const char *name; /**< IB device name (in). */
85 uint32_t ibindex; /**< IB device index (out). */
86 uint32_t ifindex; /**< Network interface index (out). */
90 * Opens a Netlink socket.
93 * Netlink protocol (e.g. NETLINK_ROUTE, NETLINK_RDMA).
96 * A file descriptor on success, a negative errno value otherwise and
100 mlx5_nl_init(int protocol)
103 int sndbuf_size = MLX5_SEND_BUF_SIZE;
104 int rcvbuf_size = MLX5_RECV_BUF_SIZE;
105 struct sockaddr_nl local = {
106 .nl_family = AF_NETLINK,
110 fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, protocol);
115 ret = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sndbuf_size, sizeof(int));
120 ret = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf_size, sizeof(int));
125 ret = bind(fd, (struct sockaddr *)&local, sizeof(local));
137 * Send a request message to the kernel on the Netlink socket.
140 * Netlink socket file descriptor.
142 * The Netlink message send to the kernel.
146 * Pointer to the request structure.
148 * Length of the request in bytes.
151 * The number of sent bytes on success, a negative errno value otherwise and
155 mlx5_nl_request(int nlsk_fd, struct nlmsghdr *nh, uint32_t sn, void *req,
158 struct sockaddr_nl sa = {
159 .nl_family = AF_NETLINK,
161 struct iovec iov[2] = {
162 { .iov_base = nh, .iov_len = sizeof(*nh), },
163 { .iov_base = req, .iov_len = len, },
165 struct msghdr msg = {
167 .msg_namelen = sizeof(sa),
173 nh->nlmsg_pid = 0; /* communication with the kernel uses pid 0 */
175 send_bytes = sendmsg(nlsk_fd, &msg, 0);
176 if (send_bytes < 0) {
184 * Send a message to the kernel on the Netlink socket.
187 * The Netlink socket file descriptor used for communication.
189 * The Netlink message send to the kernel.
194 * The number of sent bytes on success, a negative errno value otherwise and
198 mlx5_nl_send(int nlsk_fd, struct nlmsghdr *nh, uint32_t sn)
200 struct sockaddr_nl sa = {
201 .nl_family = AF_NETLINK,
205 .iov_len = nh->nlmsg_len,
207 struct msghdr msg = {
209 .msg_namelen = sizeof(sa),
215 nh->nlmsg_pid = 0; /* communication with the kernel uses pid 0 */
217 send_bytes = sendmsg(nlsk_fd, &msg, 0);
218 if (send_bytes < 0) {
226 * Receive a message from the kernel on the Netlink socket, following
230 * The Netlink socket file descriptor used for communication.
234 * The callback function to call for each Netlink message received.
235 * @param[in, out] arg
236 * Custom arguments for the callback.
239 * 0 on success, a negative errno value otherwise and rte_errno is set.
242 mlx5_nl_recv(int nlsk_fd, uint32_t sn, int (*cb)(struct nlmsghdr *, void *arg),
245 struct sockaddr_nl sa;
246 char buf[MLX5_RECV_BUF_SIZE];
249 .iov_len = sizeof(buf),
251 struct msghdr msg = {
253 .msg_namelen = sizeof(sa),
255 /* One message at a time */
266 recv_bytes = recvmsg(nlsk_fd, &msg, 0);
267 if (recv_bytes == -1) {
271 nh = (struct nlmsghdr *)buf;
272 } while (nh->nlmsg_seq != sn);
274 NLMSG_OK(nh, (unsigned int)recv_bytes);
275 nh = NLMSG_NEXT(nh, recv_bytes)) {
276 if (nh->nlmsg_type == NLMSG_ERROR) {
277 struct nlmsgerr *err_data = NLMSG_DATA(nh);
279 if (err_data->error < 0) {
280 rte_errno = -err_data->error;
286 /* Multi-part msgs and their trailing DONE message. */
287 if (nh->nlmsg_flags & NLM_F_MULTI) {
288 if (nh->nlmsg_type == NLMSG_DONE)
303 * Parse Netlink message to retrieve the bridge MAC address.
306 * Pointer to Netlink Message Header.
308 * PMD data register with this callback.
311 * 0 on success, a negative errno value otherwise and rte_errno is set.
314 mlx5_nl_mac_addr_cb(struct nlmsghdr *nh, void *arg)
316 struct mlx5_nl_mac_addr *data = arg;
317 struct ndmsg *r = NLMSG_DATA(nh);
318 struct rtattr *attribute;
321 len = nh->nlmsg_len - NLMSG_LENGTH(sizeof(*r));
322 for (attribute = MLX5_NDA_RTA(r);
323 RTA_OK(attribute, len);
324 attribute = RTA_NEXT(attribute, len)) {
325 if (attribute->rta_type == NDA_LLADDR) {
326 if (data->mac_n == MLX5_MAX_MAC_ADDRESSES) {
328 "not enough room to finalize the"
336 ether_format_addr(m, 18, RTA_DATA(attribute));
337 DRV_LOG(DEBUG, "bridge MAC address %s", m);
339 memcpy(&(*data->mac)[data->mac_n++],
340 RTA_DATA(attribute), ETHER_ADDR_LEN);
347 * Get bridge MAC addresses.
350 * Pointer to Ethernet device.
352 * Pointer to the array table of MAC addresses to fill.
353 * Its size should be of MLX5_MAX_MAC_ADDRESSES.
355 * Number of entries filled in MAC array.
358 * 0 on success, a negative errno value otherwise and rte_errno is set.
361 mlx5_nl_mac_addr_list(struct rte_eth_dev *dev, struct ether_addr (*mac)[],
364 struct priv *priv = dev->data->dev_private;
365 unsigned int iface_idx = mlx5_ifindex(dev);
368 struct ifinfomsg ifm;
371 .nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
372 .nlmsg_type = RTM_GETNEIGH,
373 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
376 .ifi_family = PF_BRIDGE,
377 .ifi_index = iface_idx,
380 struct mlx5_nl_mac_addr data = {
386 uint32_t sn = priv->nl_sn++;
388 if (priv->nl_socket_route == -1)
390 fd = priv->nl_socket_route;
391 ret = mlx5_nl_request(fd, &req.hdr, sn, &req.ifm,
392 sizeof(struct ifinfomsg));
395 ret = mlx5_nl_recv(fd, sn, mlx5_nl_mac_addr_cb, &data);
401 DRV_LOG(DEBUG, "port %u cannot retrieve MAC address list %s",
402 dev->data->port_id, strerror(rte_errno));
407 * Modify the MAC address neighbour table with Netlink.
410 * Pointer to Ethernet device.
412 * MAC address to consider.
414 * 1 to add the MAC address, 0 to remove the MAC address.
417 * 0 on success, a negative errno value otherwise and rte_errno is set.
420 mlx5_nl_mac_addr_modify(struct rte_eth_dev *dev, struct ether_addr *mac,
423 struct priv *priv = dev->data->dev_private;
424 unsigned int iface_idx = mlx5_ifindex(dev);
429 uint8_t buffer[ETHER_ADDR_LEN];
432 .nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg)),
433 .nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE |
434 NLM_F_EXCL | NLM_F_ACK,
435 .nlmsg_type = add ? RTM_NEWNEIGH : RTM_DELNEIGH,
438 .ndm_family = PF_BRIDGE,
439 .ndm_state = NUD_NOARP | NUD_PERMANENT,
440 .ndm_ifindex = iface_idx,
441 .ndm_flags = NTF_SELF,
444 .rta_type = NDA_LLADDR,
445 .rta_len = RTA_LENGTH(ETHER_ADDR_LEN),
450 uint32_t sn = priv->nl_sn++;
452 if (priv->nl_socket_route == -1)
454 fd = priv->nl_socket_route;
455 memcpy(RTA_DATA(&req.rta), mac, ETHER_ADDR_LEN);
456 req.hdr.nlmsg_len = NLMSG_ALIGN(req.hdr.nlmsg_len) +
457 RTA_ALIGN(req.rta.rta_len);
458 ret = mlx5_nl_send(fd, &req.hdr, sn);
461 ret = mlx5_nl_recv(fd, sn, NULL, NULL);
467 "port %u cannot %s MAC address %02X:%02X:%02X:%02X:%02X:%02X"
470 add ? "add" : "remove",
471 mac->addr_bytes[0], mac->addr_bytes[1],
472 mac->addr_bytes[2], mac->addr_bytes[3],
473 mac->addr_bytes[4], mac->addr_bytes[5],
474 strerror(rte_errno));
482 * Pointer to Ethernet device.
484 * MAC address to register.
489 * 0 on success, a negative errno value otherwise and rte_errno is set.
492 mlx5_nl_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac,
495 struct priv *priv = dev->data->dev_private;
498 ret = mlx5_nl_mac_addr_modify(dev, mac, 1);
500 BITFIELD_SET(priv->mac_own, index);
507 * Remove a MAC address.
510 * Pointer to Ethernet device.
512 * MAC address to remove.
517 * 0 on success, a negative errno value otherwise and rte_errno is set.
520 mlx5_nl_mac_addr_remove(struct rte_eth_dev *dev, struct ether_addr *mac,
523 struct priv *priv = dev->data->dev_private;
525 BITFIELD_RESET(priv->mac_own, index);
526 return mlx5_nl_mac_addr_modify(dev, mac, 0);
530 * Synchronize Netlink bridge table to the internal table.
533 * Pointer to Ethernet device.
536 mlx5_nl_mac_addr_sync(struct rte_eth_dev *dev)
538 struct ether_addr macs[MLX5_MAX_MAC_ADDRESSES];
543 ret = mlx5_nl_mac_addr_list(dev, &macs, &macs_n);
546 for (i = 0; i != macs_n; ++i) {
549 /* Verify the address is not in the array yet. */
550 for (j = 0; j != MLX5_MAX_MAC_ADDRESSES; ++j)
551 if (is_same_ether_addr(&macs[i],
552 &dev->data->mac_addrs[j]))
554 if (j != MLX5_MAX_MAC_ADDRESSES)
556 /* Find the first entry available. */
557 for (j = 0; j != MLX5_MAX_MAC_ADDRESSES; ++j) {
558 if (is_zero_ether_addr(&dev->data->mac_addrs[j])) {
559 dev->data->mac_addrs[j] = macs[i];
567 * Flush all added MAC addresses.
570 * Pointer to Ethernet device.
573 mlx5_nl_mac_addr_flush(struct rte_eth_dev *dev)
575 struct priv *priv = dev->data->dev_private;
578 for (i = MLX5_MAX_MAC_ADDRESSES - 1; i >= 0; --i) {
579 struct ether_addr *m = &dev->data->mac_addrs[i];
581 if (BITFIELD_ISSET(priv->mac_own, i))
582 mlx5_nl_mac_addr_remove(dev, m, i);
587 * Enable promiscuous / all multicast mode through Netlink.
590 * Pointer to Ethernet device structure.
592 * IFF_PROMISC for promiscuous, IFF_ALLMULTI for allmulti.
594 * Nonzero to enable, disable otherwise.
597 * 0 on success, a negative errno value otherwise and rte_errno is set.
600 mlx5_nl_device_flags(struct rte_eth_dev *dev, uint32_t flags, int enable)
602 struct priv *priv = dev->data->dev_private;
603 unsigned int iface_idx = mlx5_ifindex(dev);
606 struct ifinfomsg ifi;
609 .nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
610 .nlmsg_type = RTM_NEWLINK,
611 .nlmsg_flags = NLM_F_REQUEST,
614 .ifi_flags = enable ? flags : 0,
616 .ifi_index = iface_idx,
622 assert(!(flags & ~(IFF_PROMISC | IFF_ALLMULTI)));
623 if (priv->nl_socket_route < 0)
625 fd = priv->nl_socket_route;
626 ret = mlx5_nl_send(fd, &req.hdr, priv->nl_sn++);
633 * Enable promiscuous mode through Netlink.
636 * Pointer to Ethernet device structure.
638 * Nonzero to enable, disable otherwise.
641 * 0 on success, a negative errno value otherwise and rte_errno is set.
644 mlx5_nl_promisc(struct rte_eth_dev *dev, int enable)
646 int ret = mlx5_nl_device_flags(dev, IFF_PROMISC, enable);
650 "port %u cannot %s promisc mode: Netlink error %s",
651 dev->data->port_id, enable ? "enable" : "disable",
652 strerror(rte_errno));
657 * Enable all multicast mode through Netlink.
660 * Pointer to Ethernet device structure.
662 * Nonzero to enable, disable otherwise.
665 * 0 on success, a negative errno value otherwise and rte_errno is set.
668 mlx5_nl_allmulti(struct rte_eth_dev *dev, int enable)
670 int ret = mlx5_nl_device_flags(dev, IFF_ALLMULTI, enable);
674 "port %u cannot %s allmulti mode: Netlink error %s",
675 dev->data->port_id, enable ? "enable" : "disable",
676 strerror(rte_errno));
681 * Process network interface information from Netlink message.
684 * Pointer to Netlink message header.
686 * Opaque data pointer for this callback.
689 * 0 on success, a negative errno value otherwise and rte_errno is set.
692 mlx5_nl_ifindex_cb(struct nlmsghdr *nh, void *arg)
694 struct mlx5_nl_ifindex_data *data = arg;
695 size_t off = NLMSG_HDRLEN;
696 uint32_t ibindex = 0;
697 uint32_t ifindex = 0;
700 if (nh->nlmsg_type !=
701 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET) &&
703 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_PORT_GET))
705 while (off < nh->nlmsg_len) {
706 struct nlattr *na = (void *)((uintptr_t)nh + off);
707 void *payload = (void *)((uintptr_t)na + NLA_HDRLEN);
709 if (na->nla_len > nh->nlmsg_len - off)
711 switch (na->nla_type) {
712 case RDMA_NLDEV_ATTR_DEV_INDEX:
713 ibindex = *(uint32_t *)payload;
715 case RDMA_NLDEV_ATTR_DEV_NAME:
716 if (!strcmp(payload, data->name))
719 case RDMA_NLDEV_ATTR_NDEV_INDEX:
720 ifindex = *(uint32_t *)payload;
725 off += NLA_ALIGN(na->nla_len);
728 data->ibindex = ibindex;
729 data->ifindex = ifindex;
738 * Get index of network interface associated with some IB device.
740 * This is the only somewhat safe method to avoid resorting to heuristics
741 * when faced with port representors. Unfortunately it requires at least
745 * Netlink socket of the RDMA kind (NETLINK_RDMA).
750 * A valid (nonzero) interface index on success, 0 otherwise and rte_errno
754 mlx5_nl_ifindex(int nl, const char *name)
756 static const uint32_t pindex = 1;
757 uint32_t seq = random();
758 struct mlx5_nl_ifindex_data data = {
760 .ibindex = 0, /* Determined during first pass. */
761 .ifindex = 0, /* Determined during second pass. */
765 uint8_t buf[NLMSG_HDRLEN +
766 NLA_HDRLEN + NLA_ALIGN(sizeof(data.ibindex)) +
767 NLA_HDRLEN + NLA_ALIGN(sizeof(pindex))];
770 .nlmsg_len = NLMSG_LENGTH(0),
771 .nlmsg_type = RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
773 .nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP,
779 ret = mlx5_nl_send(nl, &req.nh, seq);
782 ret = mlx5_nl_recv(nl, seq, mlx5_nl_ifindex_cb, &data);
788 req.nh.nlmsg_type = RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
789 RDMA_NLDEV_CMD_PORT_GET);
790 req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
791 req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(req.buf) - NLMSG_HDRLEN);
792 na = (void *)((uintptr_t)req.buf + NLMSG_HDRLEN);
793 na->nla_len = NLA_HDRLEN + sizeof(data.ibindex);
794 na->nla_type = RDMA_NLDEV_ATTR_DEV_INDEX;
795 memcpy((void *)((uintptr_t)na + NLA_HDRLEN),
796 &data.ibindex, sizeof(data.ibindex));
797 na = (void *)((uintptr_t)na + NLA_ALIGN(na->nla_len));
798 na->nla_len = NLA_HDRLEN + sizeof(pindex);
799 na->nla_type = RDMA_NLDEV_ATTR_PORT_INDEX;
800 memcpy((void *)((uintptr_t)na + NLA_HDRLEN),
801 &pindex, sizeof(pindex));
802 ret = mlx5_nl_send(nl, &req.nh, seq);
805 ret = mlx5_nl_recv(nl, seq, mlx5_nl_ifindex_cb, &data);
817 * Process switch information from Netlink message.
820 * Pointer to Netlink message header.
822 * Opaque data pointer for this callback.
825 * 0 on success, a negative errno value otherwise and rte_errno is set.
828 mlx5_nl_switch_info_cb(struct nlmsghdr *nh, void *arg)
830 struct mlx5_switch_info info = {
836 size_t off = NLMSG_LENGTH(sizeof(struct ifinfomsg));
837 bool port_name_set = false;
838 bool switch_id_set = false;
840 if (nh->nlmsg_type != RTM_NEWLINK)
842 while (off < nh->nlmsg_len) {
843 struct rtattr *ra = (void *)((uintptr_t)nh + off);
844 void *payload = RTA_DATA(ra);
848 if (ra->rta_len > nh->nlmsg_len - off)
850 switch (ra->rta_type) {
851 case IFLA_PHYS_PORT_NAME:
853 info.port_name = strtol(payload, &end, 0);
855 (size_t)(end - (char *)payload) != strlen(payload))
857 port_name_set = true;
859 case IFLA_PHYS_SWITCH_ID:
861 for (i = 0; i < RTA_PAYLOAD(ra); ++i) {
862 info.switch_id <<= 8;
863 info.switch_id |= ((uint8_t *)payload)[i];
865 switch_id_set = true;
868 off += RTA_ALIGN(ra->rta_len);
870 info.master = switch_id_set && !port_name_set;
871 info.representor = switch_id_set && port_name_set;
872 memcpy(arg, &info, sizeof(info));
880 * Get switch information associated with network interface.
883 * Netlink socket of the ROUTE kind (NETLINK_ROUTE).
885 * Network interface index.
887 * Switch information object, populated in case of success.
890 * 0 on success, a negative errno value otherwise and rte_errno is set.
893 mlx5_nl_switch_info(int nl, unsigned int ifindex, struct mlx5_switch_info *info)
895 uint32_t seq = random();
898 struct ifinfomsg info;
901 .nlmsg_len = NLMSG_LENGTH(sizeof(req.info)),
902 .nlmsg_type = RTM_GETLINK,
903 .nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
906 .ifi_family = AF_UNSPEC,
907 .ifi_index = ifindex,
912 ret = mlx5_nl_send(nl, &req.nh, seq);
914 ret = mlx5_nl_recv(nl, seq, mlx5_nl_switch_info_cb, info);