1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2017 6WIND S.A.
3 * Copyright 2017 Mellanox
8 * Miscellaneous control operations for mlx4 driver.
14 #include <linux/ethtool.h>
15 #include <linux/sockios.h>
17 #include <netinet/ip.h>
23 #include <sys/ioctl.h>
24 #include <sys/socket.h>
27 /* Verbs headers do not support -pedantic. */
29 #pragma GCC diagnostic ignored "-Wpedantic"
31 #include <infiniband/verbs.h>
33 #pragma GCC diagnostic error "-Wpedantic"
36 #include <rte_bus_pci.h>
37 #include <rte_errno.h>
38 #include <rte_ethdev_driver.h>
39 #include <rte_ether.h>
44 #include "mlx4_flow.h"
45 #include "mlx4_glue.h"
46 #include "mlx4_rxtx.h"
47 #include "mlx4_utils.h"
50 * Get interface name from private structure.
53 * Pointer to private structure.
55 * Interface name output buffer.
58 * 0 on success, negative errno value otherwise and rte_errno is set.
61 mlx4_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
65 unsigned int dev_type = 0;
66 unsigned int dev_port_prev = ~0u;
67 char match[IF_NAMESIZE] = "";
70 MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
78 while ((dent = readdir(dir)) != NULL) {
79 char *name = dent->d_name;
81 unsigned int dev_port;
84 if ((name[0] == '.') &&
86 ((name[1] == '.') && (name[2] == '\0'))))
89 MKSTR(path, "%s/device/net/%s/%s",
90 priv->ctx->device->ibdev_path, name,
91 (dev_type ? "dev_id" : "dev_port"));
93 file = fopen(path, "rb");
98 * Switch to dev_id when dev_port does not exist as
99 * is the case with Linux kernel versions < 3.15.
110 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
115 * Switch to dev_id when dev_port returns the same value for
116 * all ports. May happen when using a MOFED release older than
117 * 3.0 with a Linux kernel >= 3.15.
119 if (dev_port == dev_port_prev)
121 dev_port_prev = dev_port;
122 if (dev_port == (priv->port - 1u))
123 snprintf(match, sizeof(match), "%s", name);
126 if (match[0] == '\0') {
130 strncpy(*ifname, match, sizeof(*ifname));
135 * Read from sysfs entry.
138 * Pointer to private structure.
140 * Entry name relative to sysfs path.
142 * Data output buffer.
147 * Number of bytes read on success, negative errno value otherwise and
151 mlx4_sysfs_read(const struct priv *priv, const char *entry,
152 char *buf, size_t size)
154 char ifname[IF_NAMESIZE];
158 ret = mlx4_get_ifname(priv, &ifname);
162 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
165 file = fopen(path, "rb");
170 ret = fread(buf, 1, size, file);
171 if ((size_t)ret < size && ferror(file)) {
182 * Write to sysfs entry.
185 * Pointer to private structure.
187 * Entry name relative to sysfs path.
194 * Number of bytes written on success, negative errno value otherwise and
198 mlx4_sysfs_write(const struct priv *priv, const char *entry,
199 char *buf, size_t size)
201 char ifname[IF_NAMESIZE];
205 ret = mlx4_get_ifname(priv, &ifname);
209 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
212 file = fopen(path, "wb");
217 ret = fwrite(buf, 1, size, file);
218 if ((size_t)ret < size || ferror(file)) {
229 * Get unsigned long sysfs property.
232 * Pointer to private structure.
234 * Entry name relative to sysfs path.
236 * Value output buffer.
239 * 0 on success, negative errno value otherwise and rte_errno is set.
242 mlx4_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
245 unsigned long value_ret;
248 ret = mlx4_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
250 DEBUG("cannot read %s value from sysfs: %s",
251 name, strerror(rte_errno));
254 value_str[ret] = '\0';
256 value_ret = strtoul(value_str, NULL, 0);
259 DEBUG("invalid %s value `%s': %s", name, value_str,
260 strerror(rte_errno));
268 * Set unsigned long sysfs property.
271 * Pointer to private structure.
273 * Entry name relative to sysfs path.
278 * 0 on success, negative errno value otherwise and rte_errno is set.
281 mlx4_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
284 MKSTR(value_str, "%lu", value);
286 ret = mlx4_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
288 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
289 name, value_str, value, strerror(rte_errno));
296 * Perform ifreq ioctl() on associated Ethernet device.
299 * Pointer to private structure.
301 * Request number to pass to ioctl().
303 * Interface request structure output buffer.
306 * 0 on success, negative errno value otherwise and rte_errno is set.
309 mlx4_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
311 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
318 ret = mlx4_get_ifname(priv, &ifr->ifr_name);
319 if (!ret && ioctl(sock, req, ifr) == -1) {
328 * Get MAC address by querying netdevice.
331 * Pointer to private structure.
333 * MAC address output buffer.
336 * 0 on success, negative errno value otherwise and rte_errno is set.
339 mlx4_get_mac(struct priv *priv, uint8_t (*mac)[ETHER_ADDR_LEN])
341 struct ifreq request;
342 int ret = mlx4_ifreq(priv, SIOCGIFHWADDR, &request);
346 memcpy(mac, request.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
354 * Pointer to private structure.
356 * MTU value output buffer.
359 * 0 on success, negative errno value otherwise and rte_errno is set.
362 mlx4_mtu_get(struct priv *priv, uint16_t *mtu)
364 unsigned long ulong_mtu = 0;
365 int ret = mlx4_get_sysfs_ulong(priv, "mtu", &ulong_mtu);
374 * DPDK callback to change the MTU.
377 * Pointer to Ethernet device structure.
382 * 0 on success, negative errno value otherwise and rte_errno is set.
385 mlx4_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
387 struct priv *priv = dev->data->dev_private;
389 int ret = mlx4_set_sysfs_ulong(priv, "mtu", mtu);
393 ret = mlx4_mtu_get(priv, &new_mtu);
396 if (new_mtu == mtu) {
408 * Pointer to private structure.
410 * Bitmask for flags that must remain untouched.
412 * Bitmask for flags to modify.
415 * 0 on success, negative errno value otherwise and rte_errno is set.
418 mlx4_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
420 unsigned long tmp = 0;
421 int ret = mlx4_get_sysfs_ulong(priv, "flags", &tmp);
426 tmp |= (flags & (~keep));
427 return mlx4_set_sysfs_ulong(priv, "flags", tmp);
431 * Change the link state (UP / DOWN).
434 * Pointer to Ethernet device private data.
436 * Nonzero for link up, otherwise link down.
439 * 0 on success, negative errno value otherwise and rte_errno is set.
442 mlx4_dev_set_link(struct priv *priv, int up)
447 err = mlx4_set_flags(priv, ~IFF_UP, IFF_UP);
451 err = mlx4_set_flags(priv, ~IFF_UP, ~IFF_UP);
459 * DPDK callback to bring the link DOWN.
462 * Pointer to Ethernet device structure.
465 * 0 on success, negative errno value otherwise and rte_errno is set.
468 mlx4_dev_set_link_down(struct rte_eth_dev *dev)
470 struct priv *priv = dev->data->dev_private;
472 return mlx4_dev_set_link(priv, 0);
476 * DPDK callback to bring the link UP.
479 * Pointer to Ethernet device structure.
482 * 0 on success, negative errno value otherwise and rte_errno is set.
485 mlx4_dev_set_link_up(struct rte_eth_dev *dev)
487 struct priv *priv = dev->data->dev_private;
489 return mlx4_dev_set_link(priv, 1);
493 * Supported Rx mode toggles.
495 * Even and odd values respectively stand for off and on.
498 RXMODE_TOGGLE_PROMISC_OFF,
499 RXMODE_TOGGLE_PROMISC_ON,
500 RXMODE_TOGGLE_ALLMULTI_OFF,
501 RXMODE_TOGGLE_ALLMULTI_ON,
505 * Helper function to toggle promiscuous and all multicast modes.
508 * Pointer to Ethernet device structure.
513 mlx4_rxmode_toggle(struct rte_eth_dev *dev, enum rxmode_toggle toggle)
515 struct priv *priv = dev->data->dev_private;
517 struct rte_flow_error error;
520 case RXMODE_TOGGLE_PROMISC_OFF:
521 case RXMODE_TOGGLE_PROMISC_ON:
522 mode = "promiscuous";
523 dev->data->promiscuous = toggle & 1;
525 case RXMODE_TOGGLE_ALLMULTI_OFF:
526 case RXMODE_TOGGLE_ALLMULTI_ON:
527 mode = "all multicast";
528 dev->data->all_multicast = toggle & 1;
531 if (!mlx4_flow_sync(priv, &error))
533 ERROR("cannot toggle %s mode (code %d, \"%s\"),"
534 " flow error type %d, cause %p, message: %s",
535 mode, rte_errno, strerror(rte_errno), error.type, error.cause,
536 error.message ? error.message : "(unspecified)");
540 * DPDK callback to enable promiscuous mode.
543 * Pointer to Ethernet device structure.
546 mlx4_promiscuous_enable(struct rte_eth_dev *dev)
548 mlx4_rxmode_toggle(dev, RXMODE_TOGGLE_PROMISC_ON);
552 * DPDK callback to disable promiscuous mode.
555 * Pointer to Ethernet device structure.
558 mlx4_promiscuous_disable(struct rte_eth_dev *dev)
560 mlx4_rxmode_toggle(dev, RXMODE_TOGGLE_PROMISC_OFF);
564 * DPDK callback to enable all multicast mode.
567 * Pointer to Ethernet device structure.
570 mlx4_allmulticast_enable(struct rte_eth_dev *dev)
572 mlx4_rxmode_toggle(dev, RXMODE_TOGGLE_ALLMULTI_ON);
576 * DPDK callback to disable all multicast mode.
579 * Pointer to Ethernet device structure.
582 mlx4_allmulticast_disable(struct rte_eth_dev *dev)
584 mlx4_rxmode_toggle(dev, RXMODE_TOGGLE_ALLMULTI_OFF);
588 * DPDK callback to remove a MAC address.
591 * Pointer to Ethernet device structure.
596 mlx4_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
598 struct priv *priv = dev->data->dev_private;
599 struct rte_flow_error error;
601 if (index >= RTE_DIM(priv->mac)) {
605 memset(&priv->mac[index], 0, sizeof(priv->mac[index]));
606 if (!mlx4_flow_sync(priv, &error))
608 ERROR("failed to synchronize flow rules after removing MAC address"
609 " at index %d (code %d, \"%s\"),"
610 " flow error type %d, cause %p, message: %s",
611 index, rte_errno, strerror(rte_errno), error.type, error.cause,
612 error.message ? error.message : "(unspecified)");
616 * DPDK callback to add a MAC address.
619 * Pointer to Ethernet device structure.
621 * MAC address to register.
625 * VMDq pool index to associate address with (ignored).
628 * 0 on success, negative errno value otherwise and rte_errno is set.
631 mlx4_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
632 uint32_t index, uint32_t vmdq)
634 struct priv *priv = dev->data->dev_private;
635 struct rte_flow_error error;
639 if (index >= RTE_DIM(priv->mac)) {
643 memcpy(&priv->mac[index], mac_addr, sizeof(priv->mac[index]));
644 ret = mlx4_flow_sync(priv, &error);
647 ERROR("failed to synchronize flow rules after adding MAC address"
648 " at index %d (code %d, \"%s\"),"
649 " flow error type %d, cause %p, message: %s",
650 index, rte_errno, strerror(rte_errno), error.type, error.cause,
651 error.message ? error.message : "(unspecified)");
656 * DPDK callback to configure a VLAN filter.
659 * Pointer to Ethernet device structure.
666 * 0 on success, negative errno value otherwise and rte_errno is set.
669 mlx4_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
671 struct priv *priv = dev->data->dev_private;
672 struct rte_flow_error error;
673 unsigned int vidx = vlan_id / 64;
674 unsigned int vbit = vlan_id % 64;
678 if (vidx >= RTE_DIM(dev->data->vlan_filter_conf.ids)) {
682 v = &dev->data->vlan_filter_conf.ids[vidx];
683 *v &= ~(UINT64_C(1) << vbit);
684 *v |= (uint64_t)!!on << vbit;
685 ret = mlx4_flow_sync(priv, &error);
688 ERROR("failed to synchronize flow rules after %s VLAN filter on ID %u"
689 " (code %d, \"%s\"), "
690 " flow error type %d, cause %p, message: %s",
691 on ? "enabling" : "disabling", vlan_id,
692 rte_errno, strerror(rte_errno), error.type, error.cause,
693 error.message ? error.message : "(unspecified)");
698 * DPDK callback to set the primary MAC address.
701 * Pointer to Ethernet device structure.
703 * MAC address to register.
706 mlx4_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
708 mlx4_mac_addr_add(dev, mac_addr, 0, 0);
712 * DPDK callback to get information about the device.
715 * Pointer to Ethernet device structure.
717 * Info structure output buffer.
720 mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
722 struct priv *priv = dev->data->dev_private;
724 char ifname[IF_NAMESIZE];
726 info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
727 /* FIXME: we should ask the device for these values. */
728 info->min_rx_bufsize = 32;
729 info->max_rx_pktlen = 65536;
731 * Since we need one CQ per QP, the limit is the minimum number
732 * between the two values.
734 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
735 priv->device_attr.max_qp : priv->device_attr.max_cq);
736 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
739 info->max_rx_queues = max;
740 info->max_tx_queues = max;
741 info->max_mac_addrs = RTE_DIM(priv->mac);
742 info->tx_offload_capa = mlx4_get_tx_port_offloads(priv);
743 info->rx_queue_offload_capa = mlx4_get_rx_queue_offloads(priv);
744 info->rx_offload_capa = (mlx4_get_rx_port_offloads(priv) |
745 info->rx_queue_offload_capa);
746 if (mlx4_get_ifname(priv, &ifname) == 0)
747 info->if_index = if_nametoindex(ifname);
748 info->hash_key_size = MLX4_RSS_HASH_KEY_SIZE;
758 * DPDK callback to get device statistics.
761 * Pointer to Ethernet device structure.
763 * Stats structure output buffer.
766 mlx4_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
768 struct rte_eth_stats tmp;
772 memset(&tmp, 0, sizeof(tmp));
773 /* Add software counters. */
774 for (i = 0; i != dev->data->nb_rx_queues; ++i) {
775 struct rxq *rxq = dev->data->rx_queues[i];
779 idx = rxq->stats.idx;
780 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
781 tmp.q_ipackets[idx] += rxq->stats.ipackets;
782 tmp.q_ibytes[idx] += rxq->stats.ibytes;
783 tmp.q_errors[idx] += (rxq->stats.idropped +
784 rxq->stats.rx_nombuf);
786 tmp.ipackets += rxq->stats.ipackets;
787 tmp.ibytes += rxq->stats.ibytes;
788 tmp.ierrors += rxq->stats.idropped;
789 tmp.rx_nombuf += rxq->stats.rx_nombuf;
791 for (i = 0; i != dev->data->nb_tx_queues; ++i) {
792 struct txq *txq = dev->data->tx_queues[i];
796 idx = txq->stats.idx;
797 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
798 tmp.q_opackets[idx] += txq->stats.opackets;
799 tmp.q_obytes[idx] += txq->stats.obytes;
800 tmp.q_errors[idx] += txq->stats.odropped;
802 tmp.opackets += txq->stats.opackets;
803 tmp.obytes += txq->stats.obytes;
804 tmp.oerrors += txq->stats.odropped;
811 * DPDK callback to clear device statistics.
814 * Pointer to Ethernet device structure.
817 mlx4_stats_reset(struct rte_eth_dev *dev)
821 for (i = 0; i != dev->data->nb_rx_queues; ++i) {
822 struct rxq *rxq = dev->data->rx_queues[i];
825 rxq->stats = (struct mlx4_rxq_stats){
826 .idx = rxq->stats.idx,
829 for (i = 0; i != dev->data->nb_tx_queues; ++i) {
830 struct txq *txq = dev->data->tx_queues[i];
833 txq->stats = (struct mlx4_txq_stats){
834 .idx = txq->stats.idx,
840 * DPDK callback to retrieve physical link information.
843 * Pointer to Ethernet device structure.
844 * @param wait_to_complete
845 * Wait for request completion (ignored).
848 * 0 on success, negative errno value otherwise and rte_errno is set.
851 mlx4_link_update(struct rte_eth_dev *dev, int wait_to_complete)
853 const struct priv *priv = dev->data->dev_private;
854 struct ethtool_cmd edata = {
858 struct rte_eth_link dev_link;
865 (void)wait_to_complete;
866 if (mlx4_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
867 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(rte_errno));
870 memset(&dev_link, 0, sizeof(dev_link));
871 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
872 (ifr.ifr_flags & IFF_RUNNING));
873 ifr.ifr_data = (void *)&edata;
874 if (mlx4_ifreq(priv, SIOCETHTOOL, &ifr)) {
875 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
876 strerror(rte_errno));
879 link_speed = ethtool_cmd_speed(&edata);
880 if (link_speed == -1)
881 dev_link.link_speed = 0;
883 dev_link.link_speed = link_speed;
884 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
885 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
886 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
887 ETH_LINK_SPEED_FIXED);
888 dev->data->dev_link = dev_link;
893 * DPDK callback to get flow control status.
896 * Pointer to Ethernet device structure.
897 * @param[out] fc_conf
898 * Flow control output buffer.
901 * 0 on success, negative errno value otherwise and rte_errno is set.
904 mlx4_flow_ctrl_get(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
906 struct priv *priv = dev->data->dev_private;
908 struct ethtool_pauseparam ethpause = {
909 .cmd = ETHTOOL_GPAUSEPARAM,
913 ifr.ifr_data = (void *)ðpause;
914 if (mlx4_ifreq(priv, SIOCETHTOOL, &ifr)) {
916 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
918 strerror(rte_errno));
921 fc_conf->autoneg = ethpause.autoneg;
922 if (ethpause.rx_pause && ethpause.tx_pause)
923 fc_conf->mode = RTE_FC_FULL;
924 else if (ethpause.rx_pause)
925 fc_conf->mode = RTE_FC_RX_PAUSE;
926 else if (ethpause.tx_pause)
927 fc_conf->mode = RTE_FC_TX_PAUSE;
929 fc_conf->mode = RTE_FC_NONE;
937 * DPDK callback to modify flow control parameters.
940 * Pointer to Ethernet device structure.
942 * Flow control parameters.
945 * 0 on success, negative errno value otherwise and rte_errno is set.
948 mlx4_flow_ctrl_set(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
950 struct priv *priv = dev->data->dev_private;
952 struct ethtool_pauseparam ethpause = {
953 .cmd = ETHTOOL_SPAUSEPARAM,
957 ifr.ifr_data = (void *)ðpause;
958 ethpause.autoneg = fc_conf->autoneg;
959 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
960 (fc_conf->mode & RTE_FC_RX_PAUSE))
961 ethpause.rx_pause = 1;
963 ethpause.rx_pause = 0;
964 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
965 (fc_conf->mode & RTE_FC_TX_PAUSE))
966 ethpause.tx_pause = 1;
968 ethpause.tx_pause = 0;
969 if (mlx4_ifreq(priv, SIOCETHTOOL, &ifr)) {
971 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
973 strerror(rte_errno));
983 * DPDK callback to retrieve the received packet types that are recognized
987 * Pointer to Ethernet device structure.
990 * Pointer to an array of recognized packet types if in Rx burst mode,
994 mlx4_dev_supported_ptypes_get(struct rte_eth_dev *dev)
996 static const uint32_t ptypes[] = {
997 /* refers to rxq_cq_to_pkt_type() */
999 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
1000 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
1006 static const uint32_t ptypes_l2tun[] = {
1007 /* refers to rxq_cq_to_pkt_type() */
1009 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
1010 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
1014 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
1015 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
1018 struct priv *priv = dev->data->dev_private;
1020 if (dev->rx_pkt_burst == mlx4_rx_burst) {
1021 if (priv->hw_csum_l2tun)
1022 return ptypes_l2tun;
1030 * Check if mlx4 device was removed.
1033 * Pointer to Ethernet device structure.
1036 * 1 when device is removed, otherwise 0.
1039 mlx4_is_removed(struct rte_eth_dev *dev)
1041 struct ibv_device_attr device_attr;
1042 struct priv *priv = dev->data->dev_private;
1044 if (mlx4_glue->query_device(priv->ctx, &device_attr) == EIO)