1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2017 6WIND S.A.
3 * Copyright 2017 Mellanox Technologies, Ltd
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>
42 #include <rte_string_fns.h>
45 #include "mlx4_flow.h"
46 #include "mlx4_glue.h"
47 #include "mlx4_rxtx.h"
48 #include "mlx4_utils.h"
51 * Get interface name from private structure.
54 * Pointer to private structure.
56 * Interface name output buffer.
59 * 0 on success, negative errno value otherwise and rte_errno is set.
62 mlx4_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
66 unsigned int dev_type = 0;
67 unsigned int dev_port_prev = ~0u;
68 char match[IF_NAMESIZE] = "";
71 MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
79 while ((dent = readdir(dir)) != NULL) {
80 char *name = dent->d_name;
82 unsigned int dev_port;
85 if ((name[0] == '.') &&
87 ((name[1] == '.') && (name[2] == '\0'))))
90 MKSTR(path, "%s/device/net/%s/%s",
91 priv->ctx->device->ibdev_path, name,
92 (dev_type ? "dev_id" : "dev_port"));
94 file = fopen(path, "rb");
99 * Switch to dev_id when dev_port does not exist as
100 * is the case with Linux kernel versions < 3.15.
111 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
116 * Switch to dev_id when dev_port returns the same value for
117 * all ports. May happen when using a MOFED release older than
118 * 3.0 with a Linux kernel >= 3.15.
120 if (dev_port == dev_port_prev)
122 dev_port_prev = dev_port;
123 if (dev_port == (priv->port - 1u))
124 strlcpy(match, name, sizeof(match));
127 if (match[0] == '\0') {
131 strncpy(*ifname, match, sizeof(*ifname));
136 * Perform ifreq ioctl() on associated Ethernet device.
139 * Pointer to private structure.
141 * Request number to pass to ioctl().
143 * Interface request structure output buffer.
146 * 0 on success, negative errno value otherwise and rte_errno is set.
149 mlx4_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
151 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
158 ret = mlx4_get_ifname(priv, &ifr->ifr_name);
159 if (!ret && ioctl(sock, req, ifr) == -1) {
168 * Get MAC address by querying netdevice.
171 * Pointer to private structure.
173 * MAC address output buffer.
176 * 0 on success, negative errno value otherwise and rte_errno is set.
179 mlx4_get_mac(struct priv *priv, uint8_t (*mac)[ETHER_ADDR_LEN])
181 struct ifreq request;
182 int ret = mlx4_ifreq(priv, SIOCGIFHWADDR, &request);
186 memcpy(mac, request.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
194 * Pointer to private structure.
196 * MTU value output buffer.
199 * 0 on success, negative errno value otherwise and rte_errno is set.
202 mlx4_mtu_get(struct priv *priv, uint16_t *mtu)
204 struct ifreq request;
205 int ret = mlx4_ifreq(priv, SIOCGIFMTU, &request);
209 *mtu = request.ifr_mtu;
214 * DPDK callback to change the MTU.
217 * Pointer to Ethernet device structure.
222 * 0 on success, negative errno value otherwise and rte_errno is set.
225 mlx4_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
227 struct priv *priv = dev->data->dev_private;
228 struct ifreq request = { .ifr_mtu = mtu, };
229 int ret = mlx4_ifreq(priv, SIOCSIFMTU, &request);
241 * Pointer to private structure.
243 * Bitmask for flags that must remain untouched.
245 * Bitmask for flags to modify.
248 * 0 on success, negative errno value otherwise and rte_errno is set.
251 mlx4_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
253 struct ifreq request;
254 int ret = mlx4_ifreq(priv, SIOCGIFFLAGS, &request);
258 request.ifr_flags &= keep;
259 request.ifr_flags |= flags & ~keep;
260 return mlx4_ifreq(priv, SIOCSIFFLAGS, &request);
264 * Change the link state (UP / DOWN).
267 * Pointer to Ethernet device private data.
269 * Nonzero for link up, otherwise link down.
272 * 0 on success, negative errno value otherwise and rte_errno is set.
275 mlx4_dev_set_link(struct priv *priv, int up)
280 err = mlx4_set_flags(priv, ~IFF_UP, IFF_UP);
284 err = mlx4_set_flags(priv, ~IFF_UP, ~IFF_UP);
292 * DPDK callback to bring the link DOWN.
295 * Pointer to Ethernet device structure.
298 * 0 on success, negative errno value otherwise and rte_errno is set.
301 mlx4_dev_set_link_down(struct rte_eth_dev *dev)
303 struct priv *priv = dev->data->dev_private;
305 return mlx4_dev_set_link(priv, 0);
309 * DPDK callback to bring the link UP.
312 * Pointer to Ethernet device structure.
315 * 0 on success, negative errno value otherwise and rte_errno is set.
318 mlx4_dev_set_link_up(struct rte_eth_dev *dev)
320 struct priv *priv = dev->data->dev_private;
322 return mlx4_dev_set_link(priv, 1);
326 * Supported Rx mode toggles.
328 * Even and odd values respectively stand for off and on.
331 RXMODE_TOGGLE_PROMISC_OFF,
332 RXMODE_TOGGLE_PROMISC_ON,
333 RXMODE_TOGGLE_ALLMULTI_OFF,
334 RXMODE_TOGGLE_ALLMULTI_ON,
338 * Helper function to toggle promiscuous and all multicast modes.
341 * Pointer to Ethernet device structure.
346 mlx4_rxmode_toggle(struct rte_eth_dev *dev, enum rxmode_toggle toggle)
348 struct priv *priv = dev->data->dev_private;
350 struct rte_flow_error error;
353 case RXMODE_TOGGLE_PROMISC_OFF:
354 case RXMODE_TOGGLE_PROMISC_ON:
355 mode = "promiscuous";
356 dev->data->promiscuous = toggle & 1;
358 case RXMODE_TOGGLE_ALLMULTI_OFF:
359 case RXMODE_TOGGLE_ALLMULTI_ON:
360 mode = "all multicast";
361 dev->data->all_multicast = toggle & 1;
366 if (!mlx4_flow_sync(priv, &error))
368 ERROR("cannot toggle %s mode (code %d, \"%s\"),"
369 " flow error type %d, cause %p, message: %s",
370 mode, rte_errno, strerror(rte_errno), error.type, error.cause,
371 error.message ? error.message : "(unspecified)");
375 * DPDK callback to enable promiscuous mode.
378 * Pointer to Ethernet device structure.
381 mlx4_promiscuous_enable(struct rte_eth_dev *dev)
383 mlx4_rxmode_toggle(dev, RXMODE_TOGGLE_PROMISC_ON);
387 * DPDK callback to disable promiscuous mode.
390 * Pointer to Ethernet device structure.
393 mlx4_promiscuous_disable(struct rte_eth_dev *dev)
395 mlx4_rxmode_toggle(dev, RXMODE_TOGGLE_PROMISC_OFF);
399 * DPDK callback to enable all multicast mode.
402 * Pointer to Ethernet device structure.
405 mlx4_allmulticast_enable(struct rte_eth_dev *dev)
407 mlx4_rxmode_toggle(dev, RXMODE_TOGGLE_ALLMULTI_ON);
411 * DPDK callback to disable all multicast mode.
414 * Pointer to Ethernet device structure.
417 mlx4_allmulticast_disable(struct rte_eth_dev *dev)
419 mlx4_rxmode_toggle(dev, RXMODE_TOGGLE_ALLMULTI_OFF);
423 * DPDK callback to remove a MAC address.
426 * Pointer to Ethernet device structure.
431 mlx4_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
433 struct priv *priv = dev->data->dev_private;
434 struct rte_flow_error error;
436 if (index >= RTE_DIM(priv->mac)) {
440 memset(&priv->mac[index], 0, sizeof(priv->mac[index]));
441 if (!mlx4_flow_sync(priv, &error))
443 ERROR("failed to synchronize flow rules after removing MAC address"
444 " at index %d (code %d, \"%s\"),"
445 " flow error type %d, cause %p, message: %s",
446 index, rte_errno, strerror(rte_errno), error.type, error.cause,
447 error.message ? error.message : "(unspecified)");
451 * DPDK callback to add a MAC address.
454 * Pointer to Ethernet device structure.
456 * MAC address to register.
460 * VMDq pool index to associate address with (ignored).
463 * 0 on success, negative errno value otherwise and rte_errno is set.
466 mlx4_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
467 uint32_t index, uint32_t vmdq)
469 struct priv *priv = dev->data->dev_private;
470 struct rte_flow_error error;
474 if (index >= RTE_DIM(priv->mac)) {
478 memcpy(&priv->mac[index], mac_addr, sizeof(priv->mac[index]));
479 ret = mlx4_flow_sync(priv, &error);
482 ERROR("failed to synchronize flow rules after adding MAC address"
483 " at index %d (code %d, \"%s\"),"
484 " flow error type %d, cause %p, message: %s",
485 index, rte_errno, strerror(rte_errno), error.type, error.cause,
486 error.message ? error.message : "(unspecified)");
491 * DPDK callback to configure a VLAN filter.
494 * Pointer to Ethernet device structure.
501 * 0 on success, negative errno value otherwise and rte_errno is set.
504 mlx4_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
506 struct priv *priv = dev->data->dev_private;
507 struct rte_flow_error error;
508 unsigned int vidx = vlan_id / 64;
509 unsigned int vbit = vlan_id % 64;
513 if (vidx >= RTE_DIM(dev->data->vlan_filter_conf.ids)) {
517 v = &dev->data->vlan_filter_conf.ids[vidx];
518 *v &= ~(UINT64_C(1) << vbit);
519 *v |= (uint64_t)!!on << vbit;
520 ret = mlx4_flow_sync(priv, &error);
523 ERROR("failed to synchronize flow rules after %s VLAN filter on ID %u"
524 " (code %d, \"%s\"), "
525 " flow error type %d, cause %p, message: %s",
526 on ? "enabling" : "disabling", vlan_id,
527 rte_errno, strerror(rte_errno), error.type, error.cause,
528 error.message ? error.message : "(unspecified)");
533 * DPDK callback to set the primary MAC address.
536 * Pointer to Ethernet device structure.
538 * MAC address to register.
541 * 0 on success, negative errno value otherwise and rte_errno is set.
544 mlx4_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
546 return mlx4_mac_addr_add(dev, mac_addr, 0, 0);
550 * DPDK callback to get information about the device.
553 * Pointer to Ethernet device structure.
555 * Info structure output buffer.
558 mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
560 struct priv *priv = dev->data->dev_private;
562 char ifname[IF_NAMESIZE];
564 /* FIXME: we should ask the device for these values. */
565 info->min_rx_bufsize = 32;
566 info->max_rx_pktlen = 65536;
568 * Since we need one CQ per QP, the limit is the minimum number
569 * between the two values.
571 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
572 priv->device_attr.max_qp : priv->device_attr.max_cq);
573 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
576 info->max_rx_queues = max;
577 info->max_tx_queues = max;
578 info->max_mac_addrs = RTE_DIM(priv->mac);
579 info->tx_offload_capa = mlx4_get_tx_port_offloads(priv);
580 info->rx_queue_offload_capa = mlx4_get_rx_queue_offloads(priv);
581 info->rx_offload_capa = (mlx4_get_rx_port_offloads(priv) |
582 info->rx_queue_offload_capa);
583 if (mlx4_get_ifname(priv, &ifname) == 0)
584 info->if_index = if_nametoindex(ifname);
585 info->hash_key_size = MLX4_RSS_HASH_KEY_SIZE;
592 info->flow_type_rss_offloads = mlx4_conv_rss_types(priv, 0, 1);
596 * DPDK callback to get device statistics.
599 * Pointer to Ethernet device structure.
601 * Stats structure output buffer.
604 mlx4_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
606 struct rte_eth_stats tmp;
610 memset(&tmp, 0, sizeof(tmp));
611 /* Add software counters. */
612 for (i = 0; i != dev->data->nb_rx_queues; ++i) {
613 struct rxq *rxq = dev->data->rx_queues[i];
617 idx = rxq->stats.idx;
618 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
619 tmp.q_ipackets[idx] += rxq->stats.ipackets;
620 tmp.q_ibytes[idx] += rxq->stats.ibytes;
621 tmp.q_errors[idx] += (rxq->stats.idropped +
622 rxq->stats.rx_nombuf);
624 tmp.ipackets += rxq->stats.ipackets;
625 tmp.ibytes += rxq->stats.ibytes;
626 tmp.ierrors += rxq->stats.idropped;
627 tmp.rx_nombuf += rxq->stats.rx_nombuf;
629 for (i = 0; i != dev->data->nb_tx_queues; ++i) {
630 struct txq *txq = dev->data->tx_queues[i];
634 idx = txq->stats.idx;
635 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
636 tmp.q_opackets[idx] += txq->stats.opackets;
637 tmp.q_obytes[idx] += txq->stats.obytes;
638 tmp.q_errors[idx] += txq->stats.odropped;
640 tmp.opackets += txq->stats.opackets;
641 tmp.obytes += txq->stats.obytes;
642 tmp.oerrors += txq->stats.odropped;
649 * DPDK callback to clear device statistics.
652 * Pointer to Ethernet device structure.
655 mlx4_stats_reset(struct rte_eth_dev *dev)
659 for (i = 0; i != dev->data->nb_rx_queues; ++i) {
660 struct rxq *rxq = dev->data->rx_queues[i];
663 rxq->stats = (struct mlx4_rxq_stats){
664 .idx = rxq->stats.idx,
667 for (i = 0; i != dev->data->nb_tx_queues; ++i) {
668 struct txq *txq = dev->data->tx_queues[i];
671 txq->stats = (struct mlx4_txq_stats){
672 .idx = txq->stats.idx,
678 * DPDK callback to retrieve physical link information.
681 * Pointer to Ethernet device structure.
682 * @param wait_to_complete
683 * Wait for request completion (ignored).
686 * 0 on success, negative errno value otherwise and rte_errno is set.
689 mlx4_link_update(struct rte_eth_dev *dev, int wait_to_complete)
691 const struct priv *priv = dev->data->dev_private;
692 struct ethtool_cmd edata = {
696 struct rte_eth_link dev_link;
703 (void)wait_to_complete;
704 if (mlx4_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
705 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(rte_errno));
708 memset(&dev_link, 0, sizeof(dev_link));
709 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
710 (ifr.ifr_flags & IFF_RUNNING));
711 ifr.ifr_data = (void *)&edata;
712 if (mlx4_ifreq(priv, SIOCETHTOOL, &ifr)) {
713 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
714 strerror(rte_errno));
717 link_speed = ethtool_cmd_speed(&edata);
718 if (link_speed == -1)
719 dev_link.link_speed = ETH_SPEED_NUM_NONE;
721 dev_link.link_speed = link_speed;
722 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
723 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
724 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
725 ETH_LINK_SPEED_FIXED);
726 dev->data->dev_link = dev_link;
731 * DPDK callback to get flow control status.
734 * Pointer to Ethernet device structure.
735 * @param[out] fc_conf
736 * Flow control output buffer.
739 * 0 on success, negative errno value otherwise and rte_errno is set.
742 mlx4_flow_ctrl_get(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
744 struct priv *priv = dev->data->dev_private;
746 struct ethtool_pauseparam ethpause = {
747 .cmd = ETHTOOL_GPAUSEPARAM,
751 ifr.ifr_data = (void *)ðpause;
752 if (mlx4_ifreq(priv, SIOCETHTOOL, &ifr)) {
754 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
756 strerror(rte_errno));
759 fc_conf->autoneg = ethpause.autoneg;
760 if (ethpause.rx_pause && ethpause.tx_pause)
761 fc_conf->mode = RTE_FC_FULL;
762 else if (ethpause.rx_pause)
763 fc_conf->mode = RTE_FC_RX_PAUSE;
764 else if (ethpause.tx_pause)
765 fc_conf->mode = RTE_FC_TX_PAUSE;
767 fc_conf->mode = RTE_FC_NONE;
775 * DPDK callback to modify flow control parameters.
778 * Pointer to Ethernet device structure.
780 * Flow control parameters.
783 * 0 on success, negative errno value otherwise and rte_errno is set.
786 mlx4_flow_ctrl_set(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
788 struct priv *priv = dev->data->dev_private;
790 struct ethtool_pauseparam ethpause = {
791 .cmd = ETHTOOL_SPAUSEPARAM,
795 ifr.ifr_data = (void *)ðpause;
796 ethpause.autoneg = fc_conf->autoneg;
797 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
798 (fc_conf->mode & RTE_FC_RX_PAUSE))
799 ethpause.rx_pause = 1;
801 ethpause.rx_pause = 0;
802 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
803 (fc_conf->mode & RTE_FC_TX_PAUSE))
804 ethpause.tx_pause = 1;
806 ethpause.tx_pause = 0;
807 if (mlx4_ifreq(priv, SIOCETHTOOL, &ifr)) {
809 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
811 strerror(rte_errno));
821 * DPDK callback to retrieve the received packet types that are recognized
825 * Pointer to Ethernet device structure.
828 * Pointer to an array of recognized packet types if in Rx burst mode,
832 mlx4_dev_supported_ptypes_get(struct rte_eth_dev *dev)
834 static const uint32_t ptypes[] = {
835 /* refers to rxq_cq_to_pkt_type() */
837 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
838 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
844 static const uint32_t ptypes_l2tun[] = {
845 /* refers to rxq_cq_to_pkt_type() */
847 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
848 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
852 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
853 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
856 struct priv *priv = dev->data->dev_private;
858 if (dev->rx_pkt_burst == mlx4_rx_burst) {
859 if (priv->hw_csum_l2tun)
868 * Check if mlx4 device was removed.
871 * Pointer to Ethernet device structure.
874 * 1 when device is removed, otherwise 0.
877 mlx4_is_removed(struct rte_eth_dev *dev)
879 struct ibv_device_attr device_attr;
880 struct priv *priv = dev->data->dev_private;
882 if (mlx4_glue->query_device(priv->ctx, &device_attr) == EIO)