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;
390 if (!mlx4_flow_sync(priv, &error))
392 ERROR("cannot toggle %s mode (code %d, \"%s\"),"
393 " flow error type %d, cause %p, message: %s",
394 mode, rte_errno, strerror(rte_errno), error.type, error.cause,
395 error.message ? error.message : "(unspecified)");
399 * DPDK callback to enable promiscuous mode.
402 * Pointer to Ethernet device structure.
405 mlx4_promiscuous_enable(struct rte_eth_dev *dev)
407 mlx4_rxmode_toggle(dev, RXMODE_TOGGLE_PROMISC_ON);
411 * DPDK callback to disable promiscuous mode.
414 * Pointer to Ethernet device structure.
417 mlx4_promiscuous_disable(struct rte_eth_dev *dev)
419 mlx4_rxmode_toggle(dev, RXMODE_TOGGLE_PROMISC_OFF);
423 * DPDK callback to enable all multicast mode.
426 * Pointer to Ethernet device structure.
429 mlx4_allmulticast_enable(struct rte_eth_dev *dev)
431 mlx4_rxmode_toggle(dev, RXMODE_TOGGLE_ALLMULTI_ON);
435 * DPDK callback to disable all multicast mode.
438 * Pointer to Ethernet device structure.
441 mlx4_allmulticast_disable(struct rte_eth_dev *dev)
443 mlx4_rxmode_toggle(dev, RXMODE_TOGGLE_ALLMULTI_OFF);
447 * DPDK callback to remove a MAC address.
450 * Pointer to Ethernet device structure.
455 mlx4_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
457 struct priv *priv = dev->data->dev_private;
458 struct rte_flow_error error;
460 if (index >= RTE_DIM(priv->mac)) {
464 memset(&priv->mac[index], 0, sizeof(priv->mac[index]));
465 if (!mlx4_flow_sync(priv, &error))
467 ERROR("failed to synchronize flow rules after removing MAC address"
468 " at index %d (code %d, \"%s\"),"
469 " flow error type %d, cause %p, message: %s",
470 index, rte_errno, strerror(rte_errno), error.type, error.cause,
471 error.message ? error.message : "(unspecified)");
475 * DPDK callback to add a MAC address.
478 * Pointer to Ethernet device structure.
480 * MAC address to register.
484 * VMDq pool index to associate address with (ignored).
487 * 0 on success, negative errno value otherwise and rte_errno is set.
490 mlx4_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
491 uint32_t index, uint32_t vmdq)
493 struct priv *priv = dev->data->dev_private;
494 struct rte_flow_error error;
498 if (index >= RTE_DIM(priv->mac)) {
502 memcpy(&priv->mac[index], mac_addr, sizeof(priv->mac[index]));
503 ret = mlx4_flow_sync(priv, &error);
506 ERROR("failed to synchronize flow rules after adding MAC address"
507 " at index %d (code %d, \"%s\"),"
508 " flow error type %d, cause %p, message: %s",
509 index, rte_errno, strerror(rte_errno), error.type, error.cause,
510 error.message ? error.message : "(unspecified)");
515 * DPDK callback to configure a VLAN filter.
518 * Pointer to Ethernet device structure.
525 * 0 on success, negative errno value otherwise and rte_errno is set.
528 mlx4_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
530 struct priv *priv = dev->data->dev_private;
531 struct rte_flow_error error;
532 unsigned int vidx = vlan_id / 64;
533 unsigned int vbit = vlan_id % 64;
537 if (vidx >= RTE_DIM(dev->data->vlan_filter_conf.ids)) {
541 v = &dev->data->vlan_filter_conf.ids[vidx];
542 *v &= ~(UINT64_C(1) << vbit);
543 *v |= (uint64_t)!!on << vbit;
544 ret = mlx4_flow_sync(priv, &error);
547 ERROR("failed to synchronize flow rules after %s VLAN filter on ID %u"
548 " (code %d, \"%s\"), "
549 " flow error type %d, cause %p, message: %s",
550 on ? "enabling" : "disabling", vlan_id,
551 rte_errno, strerror(rte_errno), error.type, error.cause,
552 error.message ? error.message : "(unspecified)");
557 * DPDK callback to set the primary MAC address.
560 * Pointer to Ethernet device structure.
562 * MAC address to register.
565 mlx4_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
567 mlx4_mac_addr_add(dev, mac_addr, 0, 0);
571 * DPDK callback to get information about the device.
574 * Pointer to Ethernet device structure.
576 * Info structure output buffer.
579 mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
581 struct priv *priv = dev->data->dev_private;
583 char ifname[IF_NAMESIZE];
585 info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
586 /* FIXME: we should ask the device for these values. */
587 info->min_rx_bufsize = 32;
588 info->max_rx_pktlen = 65536;
590 * Since we need one CQ per QP, the limit is the minimum number
591 * between the two values.
593 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
594 priv->device_attr.max_qp : priv->device_attr.max_cq);
595 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
598 info->max_rx_queues = max;
599 info->max_tx_queues = max;
600 info->max_mac_addrs = RTE_DIM(priv->mac);
601 info->rx_offload_capa = 0;
602 info->tx_offload_capa = 0;
604 info->tx_offload_capa |= (DEV_TX_OFFLOAD_IPV4_CKSUM |
605 DEV_TX_OFFLOAD_UDP_CKSUM |
606 DEV_TX_OFFLOAD_TCP_CKSUM);
607 info->rx_offload_capa |= (DEV_RX_OFFLOAD_IPV4_CKSUM |
608 DEV_RX_OFFLOAD_UDP_CKSUM |
609 DEV_RX_OFFLOAD_TCP_CKSUM);
611 if (priv->hw_csum_l2tun)
612 info->tx_offload_capa |= DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM;
613 if (mlx4_get_ifname(priv, &ifname) == 0)
614 info->if_index = if_nametoindex(ifname);
615 info->hash_key_size = MLX4_RSS_HASH_KEY_SIZE;
625 * DPDK callback to get device statistics.
628 * Pointer to Ethernet device structure.
630 * Stats structure output buffer.
633 mlx4_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
635 struct rte_eth_stats tmp;
639 memset(&tmp, 0, sizeof(tmp));
640 /* Add software counters. */
641 for (i = 0; i != dev->data->nb_rx_queues; ++i) {
642 struct rxq *rxq = dev->data->rx_queues[i];
646 idx = rxq->stats.idx;
647 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
648 tmp.q_ipackets[idx] += rxq->stats.ipackets;
649 tmp.q_ibytes[idx] += rxq->stats.ibytes;
650 tmp.q_errors[idx] += (rxq->stats.idropped +
651 rxq->stats.rx_nombuf);
653 tmp.ipackets += rxq->stats.ipackets;
654 tmp.ibytes += rxq->stats.ibytes;
655 tmp.ierrors += rxq->stats.idropped;
656 tmp.rx_nombuf += rxq->stats.rx_nombuf;
658 for (i = 0; i != dev->data->nb_tx_queues; ++i) {
659 struct txq *txq = dev->data->tx_queues[i];
663 idx = txq->stats.idx;
664 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
665 tmp.q_opackets[idx] += txq->stats.opackets;
666 tmp.q_obytes[idx] += txq->stats.obytes;
667 tmp.q_errors[idx] += txq->stats.odropped;
669 tmp.opackets += txq->stats.opackets;
670 tmp.obytes += txq->stats.obytes;
671 tmp.oerrors += txq->stats.odropped;
678 * DPDK callback to clear device statistics.
681 * Pointer to Ethernet device structure.
684 mlx4_stats_reset(struct rte_eth_dev *dev)
688 for (i = 0; i != dev->data->nb_rx_queues; ++i) {
689 struct rxq *rxq = dev->data->rx_queues[i];
692 rxq->stats = (struct mlx4_rxq_stats){
693 .idx = rxq->stats.idx,
696 for (i = 0; i != dev->data->nb_tx_queues; ++i) {
697 struct txq *txq = dev->data->tx_queues[i];
700 txq->stats = (struct mlx4_txq_stats){
701 .idx = txq->stats.idx,
707 * DPDK callback to retrieve physical link information.
710 * Pointer to Ethernet device structure.
711 * @param wait_to_complete
712 * Wait for request completion (ignored).
715 * 0 on success, negative errno value otherwise and rte_errno is set.
718 mlx4_link_update(struct rte_eth_dev *dev, int wait_to_complete)
720 const struct priv *priv = dev->data->dev_private;
721 struct ethtool_cmd edata = {
725 struct rte_eth_link dev_link;
732 (void)wait_to_complete;
733 if (mlx4_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
734 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(rte_errno));
737 memset(&dev_link, 0, sizeof(dev_link));
738 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
739 (ifr.ifr_flags & IFF_RUNNING));
740 ifr.ifr_data = (void *)&edata;
741 if (mlx4_ifreq(priv, SIOCETHTOOL, &ifr)) {
742 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
743 strerror(rte_errno));
746 link_speed = ethtool_cmd_speed(&edata);
747 if (link_speed == -1)
748 dev_link.link_speed = 0;
750 dev_link.link_speed = link_speed;
751 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
752 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
753 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
754 ETH_LINK_SPEED_FIXED);
755 dev->data->dev_link = dev_link;
760 * DPDK callback to get flow control status.
763 * Pointer to Ethernet device structure.
764 * @param[out] fc_conf
765 * Flow control output buffer.
768 * 0 on success, negative errno value otherwise and rte_errno is set.
771 mlx4_flow_ctrl_get(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
773 struct priv *priv = dev->data->dev_private;
775 struct ethtool_pauseparam ethpause = {
776 .cmd = ETHTOOL_GPAUSEPARAM,
780 ifr.ifr_data = (void *)ðpause;
781 if (mlx4_ifreq(priv, SIOCETHTOOL, &ifr)) {
783 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
785 strerror(rte_errno));
788 fc_conf->autoneg = ethpause.autoneg;
789 if (ethpause.rx_pause && ethpause.tx_pause)
790 fc_conf->mode = RTE_FC_FULL;
791 else if (ethpause.rx_pause)
792 fc_conf->mode = RTE_FC_RX_PAUSE;
793 else if (ethpause.tx_pause)
794 fc_conf->mode = RTE_FC_TX_PAUSE;
796 fc_conf->mode = RTE_FC_NONE;
804 * DPDK callback to modify flow control parameters.
807 * Pointer to Ethernet device structure.
809 * Flow control parameters.
812 * 0 on success, negative errno value otherwise and rte_errno is set.
815 mlx4_flow_ctrl_set(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
817 struct priv *priv = dev->data->dev_private;
819 struct ethtool_pauseparam ethpause = {
820 .cmd = ETHTOOL_SPAUSEPARAM,
824 ifr.ifr_data = (void *)ðpause;
825 ethpause.autoneg = fc_conf->autoneg;
826 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
827 (fc_conf->mode & RTE_FC_RX_PAUSE))
828 ethpause.rx_pause = 1;
830 ethpause.rx_pause = 0;
831 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
832 (fc_conf->mode & RTE_FC_TX_PAUSE))
833 ethpause.tx_pause = 1;
835 ethpause.tx_pause = 0;
836 if (mlx4_ifreq(priv, SIOCETHTOOL, &ifr)) {
838 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
840 strerror(rte_errno));
850 * DPDK callback to retrieve the received packet types that are recognized
854 * Pointer to Ethernet device structure.
857 * Pointer to an array of recognized packet types if in Rx burst mode,
861 mlx4_dev_supported_ptypes_get(struct rte_eth_dev *dev)
863 static const uint32_t ptypes[] = {
864 /* refers to rxq_cq_to_pkt_type() */
866 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
867 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
873 static const uint32_t ptypes_l2tun[] = {
874 /* refers to rxq_cq_to_pkt_type() */
876 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
877 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
881 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
882 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
885 struct priv *priv = dev->data->dev_private;
887 if (dev->rx_pkt_burst == mlx4_rx_burst) {
888 if (priv->hw_csum_l2tun)