4 * Copyright 2017 6WIND S.A.
5 * Copyright 2017 Mellanox
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of 6WIND S.A. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * Miscellaneous control operations for mlx4 driver.
42 #include <linux/ethtool.h>
43 #include <linux/sockios.h>
45 #include <netinet/ip.h>
51 #include <sys/ioctl.h>
52 #include <sys/socket.h>
55 /* Verbs headers do not support -pedantic. */
57 #pragma GCC diagnostic ignored "-Wpedantic"
59 #include <infiniband/verbs.h>
61 #pragma GCC diagnostic error "-Wpedantic"
64 #include <rte_bus_pci.h>
65 #include <rte_errno.h>
66 #include <rte_ethdev.h>
67 #include <rte_ether.h>
72 #include "mlx4_flow.h"
73 #include "mlx4_rxtx.h"
74 #include "mlx4_utils.h"
77 * Get interface name from private structure.
80 * Pointer to private structure.
82 * Interface name output buffer.
85 * 0 on success, negative errno value otherwise and rte_errno is set.
88 mlx4_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
92 unsigned int dev_type = 0;
93 unsigned int dev_port_prev = ~0u;
94 char match[IF_NAMESIZE] = "";
97 MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
105 while ((dent = readdir(dir)) != NULL) {
106 char *name = dent->d_name;
108 unsigned int dev_port;
111 if ((name[0] == '.') &&
112 ((name[1] == '\0') ||
113 ((name[1] == '.') && (name[2] == '\0'))))
116 MKSTR(path, "%s/device/net/%s/%s",
117 priv->ctx->device->ibdev_path, name,
118 (dev_type ? "dev_id" : "dev_port"));
120 file = fopen(path, "rb");
125 * Switch to dev_id when dev_port does not exist as
126 * is the case with Linux kernel versions < 3.15.
137 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
142 * Switch to dev_id when dev_port returns the same value for
143 * all ports. May happen when using a MOFED release older than
144 * 3.0 with a Linux kernel >= 3.15.
146 if (dev_port == dev_port_prev)
148 dev_port_prev = dev_port;
149 if (dev_port == (priv->port - 1u))
150 snprintf(match, sizeof(match), "%s", name);
153 if (match[0] == '\0') {
157 strncpy(*ifname, match, sizeof(*ifname));
162 * Perform ifreq ioctl() on associated Ethernet device.
165 * Pointer to private structure.
167 * Request number to pass to ioctl().
169 * Interface request structure output buffer.
172 * 0 on success, negative errno value otherwise and rte_errno is set.
175 mlx4_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
177 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
184 ret = mlx4_get_ifname(priv, &ifr->ifr_name);
185 if (!ret && ioctl(sock, req, ifr) == -1) {
194 * Get MAC address by querying netdevice.
197 * Pointer to private structure.
199 * MAC address output buffer.
202 * 0 on success, negative errno value otherwise and rte_errno is set.
205 mlx4_get_mac(struct priv *priv, uint8_t (*mac)[ETHER_ADDR_LEN])
207 struct ifreq request;
208 int ret = mlx4_ifreq(priv, SIOCGIFHWADDR, &request);
212 memcpy(mac, request.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
220 * Pointer to private structure.
222 * MTU value output buffer.
225 * 0 on success, negative errno value otherwise and rte_errno is set.
228 mlx4_mtu_get(struct priv *priv, uint16_t *mtu)
230 struct ifreq request;
231 int ret = mlx4_ifreq(priv, SIOCGIFMTU, &request);
235 *mtu = request.ifr_mtu;
240 * DPDK callback to change the MTU.
243 * Pointer to Ethernet device structure.
248 * 0 on success, negative errno value otherwise and rte_errno is set.
251 mlx4_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
253 struct priv *priv = dev->data->dev_private;
254 struct ifreq request = { .ifr_mtu = mtu, };
255 int ret = mlx4_ifreq(priv, SIOCSIFMTU, &request);
267 * Pointer to private structure.
269 * Bitmask for flags that must remain untouched.
271 * Bitmask for flags to modify.
274 * 0 on success, negative errno value otherwise and rte_errno is set.
277 mlx4_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
279 struct ifreq request;
280 int ret = mlx4_ifreq(priv, SIOCGIFFLAGS, &request);
284 request.ifr_flags &= keep;
285 request.ifr_flags |= flags & ~keep;
286 return mlx4_ifreq(priv, SIOCSIFFLAGS, &request);
290 * Change the link state (UP / DOWN).
293 * Pointer to Ethernet device private data.
295 * Nonzero for link up, otherwise link down.
298 * 0 on success, negative errno value otherwise and rte_errno is set.
301 mlx4_dev_set_link(struct priv *priv, int up)
306 err = mlx4_set_flags(priv, ~IFF_UP, IFF_UP);
310 err = mlx4_set_flags(priv, ~IFF_UP, ~IFF_UP);
318 * DPDK callback to bring the link DOWN.
321 * Pointer to Ethernet device structure.
324 * 0 on success, negative errno value otherwise and rte_errno is set.
327 mlx4_dev_set_link_down(struct rte_eth_dev *dev)
329 struct priv *priv = dev->data->dev_private;
331 return mlx4_dev_set_link(priv, 0);
335 * DPDK callback to bring the link UP.
338 * Pointer to Ethernet device structure.
341 * 0 on success, negative errno value otherwise and rte_errno is set.
344 mlx4_dev_set_link_up(struct rte_eth_dev *dev)
346 struct priv *priv = dev->data->dev_private;
348 return mlx4_dev_set_link(priv, 1);
352 * Supported Rx mode toggles.
354 * Even and odd values respectively stand for off and on.
357 RXMODE_TOGGLE_PROMISC_OFF,
358 RXMODE_TOGGLE_PROMISC_ON,
359 RXMODE_TOGGLE_ALLMULTI_OFF,
360 RXMODE_TOGGLE_ALLMULTI_ON,
364 * Helper function to toggle promiscuous and all multicast modes.
367 * Pointer to Ethernet device structure.
372 mlx4_rxmode_toggle(struct rte_eth_dev *dev, enum rxmode_toggle toggle)
374 struct priv *priv = dev->data->dev_private;
376 struct rte_flow_error error;
379 case RXMODE_TOGGLE_PROMISC_OFF:
380 case RXMODE_TOGGLE_PROMISC_ON:
381 mode = "promiscuous";
382 dev->data->promiscuous = toggle & 1;
384 case RXMODE_TOGGLE_ALLMULTI_OFF:
385 case RXMODE_TOGGLE_ALLMULTI_ON:
386 mode = "all multicast";
387 dev->data->all_multicast = toggle & 1;
392 if (!mlx4_flow_sync(priv, &error))
394 ERROR("cannot toggle %s mode (code %d, \"%s\"),"
395 " flow error type %d, cause %p, message: %s",
396 mode, rte_errno, strerror(rte_errno), error.type, error.cause,
397 error.message ? error.message : "(unspecified)");
401 * DPDK callback to enable promiscuous mode.
404 * Pointer to Ethernet device structure.
407 mlx4_promiscuous_enable(struct rte_eth_dev *dev)
409 mlx4_rxmode_toggle(dev, RXMODE_TOGGLE_PROMISC_ON);
413 * DPDK callback to disable promiscuous mode.
416 * Pointer to Ethernet device structure.
419 mlx4_promiscuous_disable(struct rte_eth_dev *dev)
421 mlx4_rxmode_toggle(dev, RXMODE_TOGGLE_PROMISC_OFF);
425 * DPDK callback to enable all multicast mode.
428 * Pointer to Ethernet device structure.
431 mlx4_allmulticast_enable(struct rte_eth_dev *dev)
433 mlx4_rxmode_toggle(dev, RXMODE_TOGGLE_ALLMULTI_ON);
437 * DPDK callback to disable all multicast mode.
440 * Pointer to Ethernet device structure.
443 mlx4_allmulticast_disable(struct rte_eth_dev *dev)
445 mlx4_rxmode_toggle(dev, RXMODE_TOGGLE_ALLMULTI_OFF);
449 * DPDK callback to remove a MAC address.
452 * Pointer to Ethernet device structure.
457 mlx4_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
459 struct priv *priv = dev->data->dev_private;
460 struct rte_flow_error error;
462 if (index >= RTE_DIM(priv->mac)) {
466 memset(&priv->mac[index], 0, sizeof(priv->mac[index]));
467 if (!mlx4_flow_sync(priv, &error))
469 ERROR("failed to synchronize flow rules after removing MAC address"
470 " at index %d (code %d, \"%s\"),"
471 " flow error type %d, cause %p, message: %s",
472 index, rte_errno, strerror(rte_errno), error.type, error.cause,
473 error.message ? error.message : "(unspecified)");
477 * DPDK callback to add a MAC address.
480 * Pointer to Ethernet device structure.
482 * MAC address to register.
486 * VMDq pool index to associate address with (ignored).
489 * 0 on success, negative errno value otherwise and rte_errno is set.
492 mlx4_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
493 uint32_t index, uint32_t vmdq)
495 struct priv *priv = dev->data->dev_private;
496 struct rte_flow_error error;
500 if (index >= RTE_DIM(priv->mac)) {
504 memcpy(&priv->mac[index], mac_addr, sizeof(priv->mac[index]));
505 ret = mlx4_flow_sync(priv, &error);
508 ERROR("failed to synchronize flow rules after adding MAC address"
509 " at index %d (code %d, \"%s\"),"
510 " flow error type %d, cause %p, message: %s",
511 index, rte_errno, strerror(rte_errno), error.type, error.cause,
512 error.message ? error.message : "(unspecified)");
517 * DPDK callback to configure a VLAN filter.
520 * Pointer to Ethernet device structure.
527 * 0 on success, negative errno value otherwise and rte_errno is set.
530 mlx4_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
532 struct priv *priv = dev->data->dev_private;
533 struct rte_flow_error error;
534 unsigned int vidx = vlan_id / 64;
535 unsigned int vbit = vlan_id % 64;
539 if (vidx >= RTE_DIM(dev->data->vlan_filter_conf.ids)) {
543 v = &dev->data->vlan_filter_conf.ids[vidx];
544 *v &= ~(UINT64_C(1) << vbit);
545 *v |= (uint64_t)!!on << vbit;
546 ret = mlx4_flow_sync(priv, &error);
549 ERROR("failed to synchronize flow rules after %s VLAN filter on ID %u"
550 " (code %d, \"%s\"), "
551 " flow error type %d, cause %p, message: %s",
552 on ? "enabling" : "disabling", vlan_id,
553 rte_errno, strerror(rte_errno), error.type, error.cause,
554 error.message ? error.message : "(unspecified)");
559 * DPDK callback to set the primary MAC address.
562 * Pointer to Ethernet device structure.
564 * MAC address to register.
567 mlx4_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
569 mlx4_mac_addr_add(dev, mac_addr, 0, 0);
573 * DPDK callback to get information about the device.
576 * Pointer to Ethernet device structure.
578 * Info structure output buffer.
581 mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
583 struct priv *priv = dev->data->dev_private;
585 char ifname[IF_NAMESIZE];
587 info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
588 /* FIXME: we should ask the device for these values. */
589 info->min_rx_bufsize = 32;
590 info->max_rx_pktlen = 65536;
592 * Since we need one CQ per QP, the limit is the minimum number
593 * between the two values.
595 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
596 priv->device_attr.max_qp : priv->device_attr.max_cq);
597 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
600 info->max_rx_queues = max;
601 info->max_tx_queues = max;
602 info->max_mac_addrs = RTE_DIM(priv->mac);
603 info->rx_offload_capa = 0;
604 info->tx_offload_capa = 0;
606 info->tx_offload_capa |= (DEV_TX_OFFLOAD_IPV4_CKSUM |
607 DEV_TX_OFFLOAD_UDP_CKSUM |
608 DEV_TX_OFFLOAD_TCP_CKSUM);
609 info->rx_offload_capa |= (DEV_RX_OFFLOAD_IPV4_CKSUM |
610 DEV_RX_OFFLOAD_UDP_CKSUM |
611 DEV_RX_OFFLOAD_TCP_CKSUM);
613 if (priv->hw_csum_l2tun)
614 info->tx_offload_capa |= DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM;
615 if (mlx4_get_ifname(priv, &ifname) == 0)
616 info->if_index = if_nametoindex(ifname);
617 info->hash_key_size = MLX4_RSS_HASH_KEY_SIZE;
627 * DPDK callback to get device statistics.
630 * Pointer to Ethernet device structure.
632 * Stats structure output buffer.
635 mlx4_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
637 struct rte_eth_stats tmp;
641 memset(&tmp, 0, sizeof(tmp));
642 /* Add software counters. */
643 for (i = 0; i != dev->data->nb_rx_queues; ++i) {
644 struct rxq *rxq = dev->data->rx_queues[i];
648 idx = rxq->stats.idx;
649 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
650 tmp.q_ipackets[idx] += rxq->stats.ipackets;
651 tmp.q_ibytes[idx] += rxq->stats.ibytes;
652 tmp.q_errors[idx] += (rxq->stats.idropped +
653 rxq->stats.rx_nombuf);
655 tmp.ipackets += rxq->stats.ipackets;
656 tmp.ibytes += rxq->stats.ibytes;
657 tmp.ierrors += rxq->stats.idropped;
658 tmp.rx_nombuf += rxq->stats.rx_nombuf;
660 for (i = 0; i != dev->data->nb_tx_queues; ++i) {
661 struct txq *txq = dev->data->tx_queues[i];
665 idx = txq->stats.idx;
666 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
667 tmp.q_opackets[idx] += txq->stats.opackets;
668 tmp.q_obytes[idx] += txq->stats.obytes;
669 tmp.q_errors[idx] += txq->stats.odropped;
671 tmp.opackets += txq->stats.opackets;
672 tmp.obytes += txq->stats.obytes;
673 tmp.oerrors += txq->stats.odropped;
680 * DPDK callback to clear device statistics.
683 * Pointer to Ethernet device structure.
686 mlx4_stats_reset(struct rte_eth_dev *dev)
690 for (i = 0; i != dev->data->nb_rx_queues; ++i) {
691 struct rxq *rxq = dev->data->rx_queues[i];
694 rxq->stats = (struct mlx4_rxq_stats){
695 .idx = rxq->stats.idx,
698 for (i = 0; i != dev->data->nb_tx_queues; ++i) {
699 struct txq *txq = dev->data->tx_queues[i];
702 txq->stats = (struct mlx4_txq_stats){
703 .idx = txq->stats.idx,
709 * DPDK callback to retrieve physical link information.
712 * Pointer to Ethernet device structure.
713 * @param wait_to_complete
714 * Wait for request completion (ignored).
717 * 0 on success, negative errno value otherwise and rte_errno is set.
720 mlx4_link_update(struct rte_eth_dev *dev, int wait_to_complete)
722 const struct priv *priv = dev->data->dev_private;
723 struct ethtool_cmd edata = {
727 struct rte_eth_link dev_link;
734 (void)wait_to_complete;
735 if (mlx4_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
736 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(rte_errno));
739 memset(&dev_link, 0, sizeof(dev_link));
740 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
741 (ifr.ifr_flags & IFF_RUNNING));
742 ifr.ifr_data = (void *)&edata;
743 if (mlx4_ifreq(priv, SIOCETHTOOL, &ifr)) {
744 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
745 strerror(rte_errno));
748 link_speed = ethtool_cmd_speed(&edata);
749 if (link_speed == -1)
750 dev_link.link_speed = 0;
752 dev_link.link_speed = link_speed;
753 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
754 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
755 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
756 ETH_LINK_SPEED_FIXED);
757 dev->data->dev_link = dev_link;
762 * DPDK callback to get flow control status.
765 * Pointer to Ethernet device structure.
766 * @param[out] fc_conf
767 * Flow control output buffer.
770 * 0 on success, negative errno value otherwise and rte_errno is set.
773 mlx4_flow_ctrl_get(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
775 struct priv *priv = dev->data->dev_private;
777 struct ethtool_pauseparam ethpause = {
778 .cmd = ETHTOOL_GPAUSEPARAM,
782 ifr.ifr_data = (void *)ðpause;
783 if (mlx4_ifreq(priv, SIOCETHTOOL, &ifr)) {
785 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
787 strerror(rte_errno));
790 fc_conf->autoneg = ethpause.autoneg;
791 if (ethpause.rx_pause && ethpause.tx_pause)
792 fc_conf->mode = RTE_FC_FULL;
793 else if (ethpause.rx_pause)
794 fc_conf->mode = RTE_FC_RX_PAUSE;
795 else if (ethpause.tx_pause)
796 fc_conf->mode = RTE_FC_TX_PAUSE;
798 fc_conf->mode = RTE_FC_NONE;
806 * DPDK callback to modify flow control parameters.
809 * Pointer to Ethernet device structure.
811 * Flow control parameters.
814 * 0 on success, negative errno value otherwise and rte_errno is set.
817 mlx4_flow_ctrl_set(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
819 struct priv *priv = dev->data->dev_private;
821 struct ethtool_pauseparam ethpause = {
822 .cmd = ETHTOOL_SPAUSEPARAM,
826 ifr.ifr_data = (void *)ðpause;
827 ethpause.autoneg = fc_conf->autoneg;
828 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
829 (fc_conf->mode & RTE_FC_RX_PAUSE))
830 ethpause.rx_pause = 1;
832 ethpause.rx_pause = 0;
833 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
834 (fc_conf->mode & RTE_FC_TX_PAUSE))
835 ethpause.tx_pause = 1;
837 ethpause.tx_pause = 0;
838 if (mlx4_ifreq(priv, SIOCETHTOOL, &ifr)) {
840 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
842 strerror(rte_errno));
852 * DPDK callback to retrieve the received packet types that are recognized
856 * Pointer to Ethernet device structure.
859 * Pointer to an array of recognized packet types if in Rx burst mode,
863 mlx4_dev_supported_ptypes_get(struct rte_eth_dev *dev)
865 static const uint32_t ptypes[] = {
866 /* refers to rxq_cq_to_pkt_type() */
868 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
869 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
875 static const uint32_t ptypes_l2tun[] = {
876 /* refers to rxq_cq_to_pkt_type() */
878 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
879 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
883 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
884 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
887 struct priv *priv = dev->data->dev_private;
889 if (dev->rx_pkt_burst == mlx4_rx_burst) {
890 if (priv->hw_csum_l2tun)