4 * Copyright 2012-2015 6WIND S.A.
5 * Copyright 2012 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 * - RSS hash key and options cannot be modified.
37 * - Hardware counters aren't implemented.
51 #include <arpa/inet.h>
54 #include <sys/ioctl.h>
55 #include <sys/socket.h>
56 #include <netinet/in.h>
58 #include <linux/ethtool.h>
59 #include <linux/sockios.h>
62 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
64 #pragma GCC diagnostic ignored "-pedantic"
66 #include <infiniband/verbs.h>
68 #pragma GCC diagnostic error "-pedantic"
71 /* DPDK headers don't like -pedantic. */
73 #pragma GCC diagnostic ignored "-pedantic"
75 #include <rte_config.h>
76 #include <rte_ether.h>
77 #include <rte_ethdev.h>
80 #include <rte_errno.h>
81 #include <rte_mempool.h>
82 #include <rte_prefetch.h>
83 #include <rte_malloc.h>
84 #include <rte_spinlock.h>
85 #include <rte_atomic.h>
86 #include <rte_version.h>
89 #pragma GCC diagnostic error "-pedantic"
92 /* Generated configuration header. */
93 #include "mlx4_autoconf.h"
98 /* Runtime logging through RTE_LOG() is enabled when not in debugging mode.
99 * Intermediate LOG_*() macros add the required end-of-line characters. */
101 #define INFO(...) DEBUG(__VA_ARGS__)
102 #define WARN(...) DEBUG(__VA_ARGS__)
103 #define ERROR(...) DEBUG(__VA_ARGS__)
105 #define LOG__(level, m, ...) \
106 RTE_LOG(level, PMD, MLX4_DRIVER_NAME ": " m "%c", __VA_ARGS__)
107 #define LOG_(level, ...) LOG__(level, __VA_ARGS__, '\n')
108 #define INFO(...) LOG_(INFO, __VA_ARGS__)
109 #define WARN(...) LOG_(WARNING, __VA_ARGS__)
110 #define ERROR(...) LOG_(ERR, __VA_ARGS__)
113 /* Convenience macros for accessing mbuf fields. */
114 #define NEXT(m) ((m)->next)
115 #define DATA_LEN(m) ((m)->data_len)
116 #define PKT_LEN(m) ((m)->pkt_len)
117 #define DATA_OFF(m) ((m)->data_off)
118 #define SET_DATA_OFF(m, o) ((m)->data_off = (o))
119 #define NB_SEGS(m) ((m)->nb_segs)
120 #define PORT(m) ((m)->port)
122 /* Work Request ID data type (64 bit). */
131 #define WR_ID(o) (((wr_id_t *)&(o))->data)
133 /* Compile-time check. */
134 static inline void wr_id_t_check(void)
136 wr_id_t check[1 + (2 * -!(sizeof(wr_id_t) == sizeof(uint64_t)))];
142 /* Transpose flags. Useful to convert IBV to DPDK flags. */
143 #define TRANSPOSE(val, from, to) \
144 (((from) >= (to)) ? \
145 (((val) & (from)) / ((from) / (to))) : \
146 (((val) & (from)) * ((to) / (from))))
148 struct mlx4_rxq_stats {
149 unsigned int idx; /**< Mapping index. */
150 #ifdef MLX4_PMD_SOFT_COUNTERS
151 uint64_t ipackets; /**< Total of successfully received packets. */
152 uint64_t ibytes; /**< Total of successfully received bytes. */
154 uint64_t idropped; /**< Total of packets dropped when RX ring full. */
155 uint64_t rx_nombuf; /**< Total of RX mbuf allocation failures. */
158 struct mlx4_txq_stats {
159 unsigned int idx; /**< Mapping index. */
160 #ifdef MLX4_PMD_SOFT_COUNTERS
161 uint64_t opackets; /**< Total of successfully sent packets. */
162 uint64_t obytes; /**< Total of successfully sent bytes. */
164 uint64_t odropped; /**< Total of packets not sent when TX ring full. */
167 /* RX element (scattered packets). */
169 struct ibv_recv_wr wr; /* Work Request. */
170 struct ibv_sge sges[MLX4_PMD_SGE_WR_N]; /* Scatter/Gather Elements. */
171 struct rte_mbuf *bufs[MLX4_PMD_SGE_WR_N]; /* SGEs buffers. */
176 struct ibv_recv_wr wr; /* Work Request. */
177 struct ibv_sge sge; /* Scatter/Gather Element. */
178 /* mbuf pointer is derived from WR_ID(wr.wr_id).offset. */
181 /* RX queue descriptor. */
183 struct priv *priv; /* Back pointer to private data. */
184 struct rte_mempool *mp; /* Memory Pool for allocations. */
185 struct ibv_mr *mr; /* Memory Region (for mp). */
186 struct ibv_cq *cq; /* Completion Queue. */
187 struct ibv_qp *qp; /* Queue Pair. */
188 struct ibv_exp_qp_burst_family *if_qp; /* QP burst interface. */
189 struct ibv_exp_cq_family *if_cq; /* CQ interface. */
191 * Each VLAN ID requires a separate flow steering rule.
193 BITFIELD_DECLARE(mac_configured, uint32_t, MLX4_MAX_MAC_ADDRESSES);
194 struct ibv_flow *mac_flow[MLX4_MAX_MAC_ADDRESSES][MLX4_MAX_VLAN_IDS];
195 struct ibv_flow *promisc_flow; /* Promiscuous flow. */
196 struct ibv_flow *allmulti_flow; /* Multicast flow. */
197 unsigned int port_id; /* Port ID for incoming packets. */
198 unsigned int elts_n; /* (*elts)[] length. */
199 unsigned int elts_head; /* Current index in (*elts)[]. */
201 struct rxq_elt_sp (*sp)[]; /* Scattered RX elements. */
202 struct rxq_elt (*no_sp)[]; /* RX elements. */
204 unsigned int sp:1; /* Use scattered RX elements. */
205 unsigned int csum:1; /* Enable checksum offloading. */
206 unsigned int csum_l2tun:1; /* Same for L2 tunnels. */
207 uint32_t mb_len; /* Length of a mp-issued mbuf. */
208 struct mlx4_rxq_stats stats; /* RX queue counters. */
209 unsigned int socket; /* CPU socket ID for allocations. */
214 struct rte_mbuf *buf;
217 /* Linear buffer type. It is used when transmitting buffers with too many
218 * segments that do not fit the hardware queue (see max_send_sge).
219 * Extra segments are copied (linearized) in such buffers, replacing the
220 * last SGE during TX.
221 * The size is arbitrary but large enough to hold a jumbo frame with
222 * 8 segments considering mbuf.buf_len is about 2048 bytes. */
223 typedef uint8_t linear_t[16384];
225 /* TX queue descriptor. */
227 struct priv *priv; /* Back pointer to private data. */
229 struct rte_mempool *mp; /* Cached Memory Pool. */
230 struct ibv_mr *mr; /* Memory Region (for mp). */
231 uint32_t lkey; /* mr->lkey */
232 } mp2mr[MLX4_PMD_TX_MP_CACHE]; /* MP to MR translation table. */
233 struct ibv_cq *cq; /* Completion Queue. */
234 struct ibv_qp *qp; /* Queue Pair. */
235 struct ibv_exp_qp_burst_family *if_qp; /* QP burst interface. */
236 struct ibv_exp_cq_family *if_cq; /* CQ interface. */
237 #if MLX4_PMD_MAX_INLINE > 0
238 uint32_t max_inline; /* Max inline send size <= MLX4_PMD_MAX_INLINE. */
240 unsigned int elts_n; /* (*elts)[] length. */
241 struct txq_elt (*elts)[]; /* TX elements. */
242 unsigned int elts_head; /* Current index in (*elts)[]. */
243 unsigned int elts_tail; /* First element awaiting completion. */
244 unsigned int elts_comp; /* Number of completion requests. */
245 unsigned int elts_comp_cd; /* Countdown for next completion request. */
246 unsigned int elts_comp_cd_init; /* Initial value for countdown. */
247 struct mlx4_txq_stats stats; /* TX queue counters. */
248 linear_t (*elts_linear)[]; /* Linearized buffers. */
249 struct ibv_mr *mr_linear; /* Memory Region for linearized buffers. */
250 unsigned int socket; /* CPU socket ID for allocations. */
254 struct rte_eth_dev *dev; /* Ethernet device. */
255 struct ibv_context *ctx; /* Verbs context. */
256 struct ibv_device_attr device_attr; /* Device properties. */
257 struct ibv_pd *pd; /* Protection Domain. */
259 * MAC addresses array and configuration bit-field.
260 * An extra entry that cannot be modified by the DPDK is reserved
261 * for broadcast frames (destination MAC address ff:ff:ff:ff:ff:ff).
263 struct ether_addr mac[MLX4_MAX_MAC_ADDRESSES];
264 BITFIELD_DECLARE(mac_configured, uint32_t, MLX4_MAX_MAC_ADDRESSES);
267 unsigned int enabled:1; /* If enabled. */
268 unsigned int id:12; /* VLAN ID (0-4095). */
269 } vlan_filter[MLX4_MAX_VLAN_IDS]; /* VLAN filters table. */
270 /* Device properties. */
271 uint16_t mtu; /* Configured MTU. */
272 uint8_t port; /* Physical port number. */
273 unsigned int started:1; /* Device started, flows enabled. */
274 unsigned int promisc:1; /* Device in promiscuous mode. */
275 unsigned int allmulti:1; /* Device receives all multicast packets. */
276 unsigned int hw_qpg:1; /* QP groups are supported. */
277 unsigned int hw_tss:1; /* TSS is supported. */
278 unsigned int hw_rss:1; /* RSS is supported. */
279 unsigned int hw_csum:1; /* Checksum offload is supported. */
280 unsigned int hw_csum_l2tun:1; /* Same for L2 tunnels. */
281 unsigned int rss:1; /* RSS is enabled. */
282 unsigned int vf:1; /* This is a VF device. */
284 unsigned int inl_recv_size; /* Inline recv size */
286 unsigned int max_rss_tbl_sz; /* Maximum number of RSS queues. */
288 struct rxq rxq_parent; /* Parent queue when RSS is enabled. */
289 unsigned int rxqs_n; /* RX queues array size. */
290 unsigned int txqs_n; /* TX queues array size. */
291 struct rxq *(*rxqs)[]; /* RX queues. */
292 struct txq *(*txqs)[]; /* TX queues. */
293 rte_spinlock_t lock; /* Lock for control functions. */
297 * Lock private structure to protect it from concurrent access in the
301 * Pointer to private structure.
304 priv_lock(struct priv *priv)
306 rte_spinlock_lock(&priv->lock);
310 * Unlock private structure.
313 * Pointer to private structure.
316 priv_unlock(struct priv *priv)
318 rte_spinlock_unlock(&priv->lock);
321 /* Allocate a buffer on the stack and fill it with a printf format string. */
322 #define MKSTR(name, ...) \
323 char name[snprintf(NULL, 0, __VA_ARGS__) + 1]; \
325 snprintf(name, sizeof(name), __VA_ARGS__)
328 * Get interface name from private structure.
331 * Pointer to private structure.
333 * Interface name output buffer.
336 * 0 on success, -1 on failure and errno is set.
339 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
343 unsigned int dev_type = 0;
344 unsigned int dev_port_prev = ~0u;
345 char match[IF_NAMESIZE] = "";
348 MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
354 while ((dent = readdir(dir)) != NULL) {
355 char *name = dent->d_name;
357 unsigned int dev_port;
360 if ((name[0] == '.') &&
361 ((name[1] == '\0') ||
362 ((name[1] == '.') && (name[2] == '\0'))))
365 MKSTR(path, "%s/device/net/%s/%s",
366 priv->ctx->device->ibdev_path, name,
367 (dev_type ? "dev_id" : "dev_port"));
369 file = fopen(path, "rb");
374 * Switch to dev_id when dev_port does not exist as
375 * is the case with Linux kernel versions < 3.15.
386 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
391 * Switch to dev_id when dev_port returns the same value for
392 * all ports. May happen when using a MOFED release older than
393 * 3.0 with a Linux kernel >= 3.15.
395 if (dev_port == dev_port_prev)
397 dev_port_prev = dev_port;
398 if (dev_port == (priv->port - 1u))
399 snprintf(match, sizeof(match), "%s", name);
402 if (match[0] == '\0')
404 strncpy(*ifname, match, sizeof(*ifname));
409 * Read from sysfs entry.
412 * Pointer to private structure.
414 * Entry name relative to sysfs path.
416 * Data output buffer.
421 * 0 on success, -1 on failure and errno is set.
424 priv_sysfs_read(const struct priv *priv, const char *entry,
425 char *buf, size_t size)
427 char ifname[IF_NAMESIZE];
432 if (priv_get_ifname(priv, &ifname))
435 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
438 file = fopen(path, "rb");
441 ret = fread(buf, 1, size, file);
443 if (((size_t)ret < size) && (ferror(file)))
453 * Write to sysfs entry.
456 * Pointer to private structure.
458 * Entry name relative to sysfs path.
465 * 0 on success, -1 on failure and errno is set.
468 priv_sysfs_write(const struct priv *priv, const char *entry,
469 char *buf, size_t size)
471 char ifname[IF_NAMESIZE];
476 if (priv_get_ifname(priv, &ifname))
479 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
482 file = fopen(path, "wb");
485 ret = fwrite(buf, 1, size, file);
487 if (((size_t)ret < size) || (ferror(file)))
497 * Get unsigned long sysfs property.
500 * Pointer to private structure.
502 * Entry name relative to sysfs path.
504 * Value output buffer.
507 * 0 on success, -1 on failure and errno is set.
510 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
513 unsigned long value_ret;
516 ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
518 DEBUG("cannot read %s value from sysfs: %s",
519 name, strerror(errno));
522 value_str[ret] = '\0';
524 value_ret = strtoul(value_str, NULL, 0);
526 DEBUG("invalid %s value `%s': %s", name, value_str,
535 * Set unsigned long sysfs property.
538 * Pointer to private structure.
540 * Entry name relative to sysfs path.
545 * 0 on success, -1 on failure and errno is set.
548 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
551 MKSTR(value_str, "%lu", value);
553 ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
555 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
556 name, value_str, value, strerror(errno));
563 * Perform ifreq ioctl() on associated Ethernet device.
566 * Pointer to private structure.
568 * Request number to pass to ioctl().
570 * Interface request structure output buffer.
573 * 0 on success, -1 on failure and errno is set.
576 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
578 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
583 if (priv_get_ifname(priv, &ifr->ifr_name) == 0)
584 ret = ioctl(sock, req, ifr);
593 * Pointer to private structure.
595 * MTU value output buffer.
598 * 0 on success, -1 on failure and errno is set.
601 priv_get_mtu(struct priv *priv, uint16_t *mtu)
603 unsigned long ulong_mtu;
605 if (priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu) == -1)
615 * Pointer to private structure.
620 * 0 on success, -1 on failure and errno is set.
623 priv_set_mtu(struct priv *priv, uint16_t mtu)
625 return priv_set_sysfs_ulong(priv, "mtu", mtu);
632 * Pointer to private structure.
634 * Bitmask for flags that must remain untouched.
636 * Bitmask for flags to modify.
639 * 0 on success, -1 on failure and errno is set.
642 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
646 if (priv_get_sysfs_ulong(priv, "flags", &tmp) == -1)
650 return priv_set_sysfs_ulong(priv, "flags", tmp);
653 /* Device configuration. */
656 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
657 unsigned int socket, const struct rte_eth_rxconf *conf,
658 struct rte_mempool *mp);
661 rxq_cleanup(struct rxq *rxq);
664 * Ethernet device configuration.
666 * Prepare the driver for a given number of TX and RX queues.
667 * Allocate parent RSS queue when several RX queues are requested.
670 * Pointer to Ethernet device structure.
673 * 0 on success, errno value on failure.
676 dev_configure(struct rte_eth_dev *dev)
678 struct priv *priv = dev->data->dev_private;
679 unsigned int rxqs_n = dev->data->nb_rx_queues;
680 unsigned int txqs_n = dev->data->nb_tx_queues;
684 priv->rxqs = (void *)dev->data->rx_queues;
685 priv->txqs = (void *)dev->data->tx_queues;
686 if (txqs_n != priv->txqs_n) {
687 INFO("%p: TX queues number update: %u -> %u",
688 (void *)dev, priv->txqs_n, txqs_n);
689 priv->txqs_n = txqs_n;
691 if (rxqs_n == priv->rxqs_n)
693 INFO("%p: RX queues number update: %u -> %u",
694 (void *)dev, priv->rxqs_n, rxqs_n);
695 /* If RSS is enabled, disable it first. */
699 /* Only if there are no remaining child RX queues. */
700 for (i = 0; (i != priv->rxqs_n); ++i)
701 if ((*priv->rxqs)[i] != NULL)
703 rxq_cleanup(&priv->rxq_parent);
708 /* Nothing else to do. */
709 priv->rxqs_n = rxqs_n;
712 /* Allocate a new RSS parent queue if supported by hardware. */
714 ERROR("%p: only a single RX queue can be configured when"
715 " hardware doesn't support RSS",
719 /* Fail if hardware doesn't support that many RSS queues. */
720 if (rxqs_n >= priv->max_rss_tbl_sz) {
721 ERROR("%p: only %u RX queues can be configured for RSS",
722 (void *)dev, priv->max_rss_tbl_sz);
727 priv->rxqs_n = rxqs_n;
728 ret = rxq_setup(dev, &priv->rxq_parent, 0, 0, NULL, NULL);
731 /* Failure, rollback. */
739 * DPDK callback for Ethernet device configuration.
742 * Pointer to Ethernet device structure.
745 * 0 on success, negative errno value on failure.
748 mlx4_dev_configure(struct rte_eth_dev *dev)
750 struct priv *priv = dev->data->dev_private;
754 ret = dev_configure(dev);
760 /* TX queues handling. */
763 * Allocate TX queue elements.
766 * Pointer to TX queue structure.
768 * Number of elements to allocate.
771 * 0 on success, errno value on failure.
774 txq_alloc_elts(struct txq *txq, unsigned int elts_n)
777 struct txq_elt (*elts)[elts_n] =
778 rte_calloc_socket("TXQ", 1, sizeof(*elts), 0, txq->socket);
779 linear_t (*elts_linear)[elts_n] =
780 rte_calloc_socket("TXQ", 1, sizeof(*elts_linear), 0,
782 struct ibv_mr *mr_linear = NULL;
785 if ((elts == NULL) || (elts_linear == NULL)) {
786 ERROR("%p: can't allocate packets array", (void *)txq);
791 ibv_reg_mr(txq->priv->pd, elts_linear, sizeof(*elts_linear),
792 (IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE));
793 if (mr_linear == NULL) {
794 ERROR("%p: unable to configure MR, ibv_reg_mr() failed",
799 for (i = 0; (i != elts_n); ++i) {
800 struct txq_elt *elt = &(*elts)[i];
804 DEBUG("%p: allocated and configured %u WRs", (void *)txq, elts_n);
805 txq->elts_n = elts_n;
810 /* Request send completion every MLX4_PMD_TX_PER_COMP_REQ packets or
811 * at least 4 times per ring. */
812 txq->elts_comp_cd_init =
813 ((MLX4_PMD_TX_PER_COMP_REQ < (elts_n / 4)) ?
814 MLX4_PMD_TX_PER_COMP_REQ : (elts_n / 4));
815 txq->elts_comp_cd = txq->elts_comp_cd_init;
816 txq->elts_linear = elts_linear;
817 txq->mr_linear = mr_linear;
821 if (mr_linear != NULL)
822 claim_zero(ibv_dereg_mr(mr_linear));
824 rte_free(elts_linear);
827 DEBUG("%p: failed, freed everything", (void *)txq);
833 * Free TX queue elements.
836 * Pointer to TX queue structure.
839 txq_free_elts(struct txq *txq)
842 unsigned int elts_n = txq->elts_n;
843 struct txq_elt (*elts)[elts_n] = txq->elts;
844 linear_t (*elts_linear)[elts_n] = txq->elts_linear;
845 struct ibv_mr *mr_linear = txq->mr_linear;
847 DEBUG("%p: freeing WRs", (void *)txq);
850 txq->elts_linear = NULL;
851 txq->mr_linear = NULL;
852 if (mr_linear != NULL)
853 claim_zero(ibv_dereg_mr(mr_linear));
855 rte_free(elts_linear);
858 for (i = 0; (i != elemof(*elts)); ++i) {
859 struct txq_elt *elt = &(*elts)[i];
861 if (elt->buf == NULL)
863 rte_pktmbuf_free(elt->buf);
870 * Clean up a TX queue.
872 * Destroy objects, free allocated memory and reset the structure for reuse.
875 * Pointer to TX queue structure.
878 txq_cleanup(struct txq *txq)
880 struct ibv_exp_release_intf_params params;
883 DEBUG("cleaning up %p", (void *)txq);
885 if (txq->if_qp != NULL) {
886 assert(txq->priv != NULL);
887 assert(txq->priv->ctx != NULL);
888 assert(txq->qp != NULL);
889 params = (struct ibv_exp_release_intf_params){
892 claim_zero(ibv_exp_release_intf(txq->priv->ctx,
896 if (txq->if_cq != NULL) {
897 assert(txq->priv != NULL);
898 assert(txq->priv->ctx != NULL);
899 assert(txq->cq != NULL);
900 params = (struct ibv_exp_release_intf_params){
903 claim_zero(ibv_exp_release_intf(txq->priv->ctx,
908 claim_zero(ibv_destroy_qp(txq->qp));
910 claim_zero(ibv_destroy_cq(txq->cq));
911 for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
912 if (txq->mp2mr[i].mp == NULL)
914 assert(txq->mp2mr[i].mr != NULL);
915 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
917 memset(txq, 0, sizeof(*txq));
921 * Manage TX completions.
923 * When sending a burst, mlx4_tx_burst() posts several WRs.
924 * To improve performance, a completion event is only required once every
925 * MLX4_PMD_TX_PER_COMP_REQ sends. Doing so discards completion information
926 * for other WRs, but this information would not be used anyway.
929 * Pointer to TX queue structure.
932 * 0 on success, -1 on failure.
935 txq_complete(struct txq *txq)
937 unsigned int elts_comp = txq->elts_comp;
938 unsigned int elts_tail = txq->elts_tail;
939 const unsigned int elts_n = txq->elts_n;
942 if (unlikely(elts_comp == 0))
945 DEBUG("%p: processing %u work requests completions",
946 (void *)txq, elts_comp);
948 wcs_n = txq->if_cq->poll_cnt(txq->cq, elts_comp);
949 if (unlikely(wcs_n == 0))
951 if (unlikely(wcs_n < 0)) {
952 DEBUG("%p: ibv_poll_cq() failed (wcs_n=%d)",
957 assert(elts_comp <= txq->elts_comp);
959 * Assume WC status is successful as nothing can be done about it
962 elts_tail += wcs_n * txq->elts_comp_cd_init;
963 if (elts_tail >= elts_n)
965 txq->elts_tail = elts_tail;
966 txq->elts_comp = elts_comp;
971 * Get Memory Region (MR) <-> Memory Pool (MP) association from txq->mp2mr[].
972 * Add MP to txq->mp2mr[] if it's not registered yet. If mp2mr[] is full,
973 * remove an entry first.
976 * Pointer to TX queue structure.
978 * Memory Pool for which a Memory Region lkey must be returned.
981 * mr->lkey on success, (uint32_t)-1 on failure.
984 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
989 for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
990 if (unlikely(txq->mp2mr[i].mp == NULL)) {
991 /* Unknown MP, add a new MR for it. */
994 if (txq->mp2mr[i].mp == mp) {
995 assert(txq->mp2mr[i].lkey != (uint32_t)-1);
996 assert(txq->mp2mr[i].mr->lkey == txq->mp2mr[i].lkey);
997 return txq->mp2mr[i].lkey;
1000 /* Add a new entry, register MR first. */
1001 DEBUG("%p: discovered new memory pool %p", (void *)txq, (void *)mp);
1002 mr = ibv_reg_mr(txq->priv->pd,
1003 (void *)mp->elt_va_start,
1004 (mp->elt_va_end - mp->elt_va_start),
1005 (IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE));
1006 if (unlikely(mr == NULL)) {
1007 DEBUG("%p: unable to configure MR, ibv_reg_mr() failed.",
1009 return (uint32_t)-1;
1011 if (unlikely(i == elemof(txq->mp2mr))) {
1012 /* Table is full, remove oldest entry. */
1013 DEBUG("%p: MR <-> MP table full, dropping oldest entry.",
1016 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
1017 memmove(&txq->mp2mr[0], &txq->mp2mr[1],
1018 (sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
1020 /* Store the new entry. */
1021 txq->mp2mr[i].mp = mp;
1022 txq->mp2mr[i].mr = mr;
1023 txq->mp2mr[i].lkey = mr->lkey;
1024 DEBUG("%p: new MR lkey for MP %p: 0x%08" PRIu32,
1025 (void *)txq, (void *)mp, txq->mp2mr[i].lkey);
1026 return txq->mp2mr[i].lkey;
1029 #if MLX4_PMD_SGE_WR_N > 1
1032 * Copy scattered mbuf contents to a single linear buffer.
1034 * @param[out] linear
1035 * Linear output buffer.
1037 * Scattered input buffer.
1040 * Number of bytes copied to the output buffer or 0 if not large enough.
1043 linearize_mbuf(linear_t *linear, struct rte_mbuf *buf)
1045 unsigned int size = 0;
1046 unsigned int offset;
1049 unsigned int len = DATA_LEN(buf);
1053 if (unlikely(size > sizeof(*linear)))
1055 memcpy(&(*linear)[offset],
1056 rte_pktmbuf_mtod(buf, uint8_t *),
1059 } while (buf != NULL);
1064 * Handle scattered buffers for mlx4_tx_burst().
1067 * TX queue structure.
1069 * Number of segments in buf.
1071 * TX queue element to fill.
1073 * Buffer to process.
1075 * Index of the linear buffer to use if necessary (normally txq->elts_head).
1077 * Array filled with SGEs on success.
1080 * A structure containing the processed packet size in bytes and the
1081 * number of SGEs. Both fields are set to (unsigned int)-1 in case of
1084 static struct tx_burst_sg_ret {
1085 unsigned int length;
1088 tx_burst_sg(struct txq *txq, unsigned int segs, struct txq_elt *elt,
1089 struct rte_mbuf *buf, unsigned int elts_head,
1090 struct ibv_sge (*sges)[MLX4_PMD_SGE_WR_N])
1092 unsigned int sent_size = 0;
1096 /* When there are too many segments, extra segments are
1097 * linearized in the last SGE. */
1098 if (unlikely(segs > elemof(*sges))) {
1099 segs = (elemof(*sges) - 1);
1102 /* Update element. */
1104 /* Register segments as SGEs. */
1105 for (j = 0; (j != segs); ++j) {
1106 struct ibv_sge *sge = &(*sges)[j];
1109 /* Retrieve Memory Region key for this memory pool. */
1110 lkey = txq_mp2mr(txq, buf->pool);
1111 if (unlikely(lkey == (uint32_t)-1)) {
1112 /* MR does not exist. */
1113 DEBUG("%p: unable to get MP <-> MR association",
1115 /* Clean up TX element. */
1120 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
1122 rte_prefetch0((volatile void *)
1123 (uintptr_t)sge->addr);
1124 sge->length = DATA_LEN(buf);
1126 sent_size += sge->length;
1129 /* If buf is not NULL here and is not going to be linearized,
1130 * nb_segs is not valid. */
1132 assert((buf == NULL) || (linearize));
1133 /* Linearize extra segments. */
1135 struct ibv_sge *sge = &(*sges)[segs];
1136 linear_t *linear = &(*txq->elts_linear)[elts_head];
1137 unsigned int size = linearize_mbuf(linear, buf);
1139 assert(segs == (elemof(*sges) - 1));
1141 /* Invalid packet. */
1142 DEBUG("%p: packet too large to be linearized.",
1144 /* Clean up TX element. */
1148 /* If MLX4_PMD_SGE_WR_N is 1, free mbuf immediately. */
1149 if (elemof(*sges) == 1) {
1151 struct rte_mbuf *next = NEXT(buf);
1153 rte_pktmbuf_free_seg(buf);
1155 } while (buf != NULL);
1159 sge->addr = (uintptr_t)&(*linear)[0];
1161 sge->lkey = txq->mr_linear->lkey;
1164 return (struct tx_burst_sg_ret){
1165 .length = sent_size,
1169 return (struct tx_burst_sg_ret){
1175 #endif /* MLX4_PMD_SGE_WR_N > 1 */
1178 * DPDK callback for TX.
1181 * Generic pointer to TX queue structure.
1183 * Packets to transmit.
1185 * Number of packets in array.
1188 * Number of packets successfully transmitted (<= pkts_n).
1191 mlx4_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
1193 struct txq *txq = (struct txq *)dpdk_txq;
1194 unsigned int elts_head = txq->elts_head;
1195 const unsigned int elts_tail = txq->elts_tail;
1196 const unsigned int elts_n = txq->elts_n;
1197 unsigned int elts_comp_cd = txq->elts_comp_cd;
1198 unsigned int elts_comp = 0;
1203 assert(elts_comp_cd != 0);
1205 max = (elts_n - (elts_head - elts_tail));
1209 assert(max <= elts_n);
1210 /* Always leave one free entry in the ring. */
1216 for (i = 0; (i != max); ++i) {
1217 struct rte_mbuf *buf = pkts[i];
1218 unsigned int elts_head_next =
1219 (((elts_head + 1) == elts_n) ? 0 : elts_head + 1);
1220 struct txq_elt *elt_next = &(*txq->elts)[elts_head_next];
1221 struct txq_elt *elt = &(*txq->elts)[elts_head];
1222 unsigned int segs = NB_SEGS(buf);
1223 #ifdef MLX4_PMD_SOFT_COUNTERS
1224 unsigned int sent_size = 0;
1226 uint32_t send_flags = 0;
1228 /* Clean up old buffer. */
1229 if (likely(elt->buf != NULL)) {
1230 struct rte_mbuf *tmp = elt->buf;
1232 /* Faster than rte_pktmbuf_free(). */
1234 struct rte_mbuf *next = NEXT(tmp);
1236 rte_pktmbuf_free_seg(tmp);
1238 } while (tmp != NULL);
1240 /* Request TX completion. */
1241 if (unlikely(--elts_comp_cd == 0)) {
1242 elts_comp_cd = txq->elts_comp_cd_init;
1244 send_flags |= IBV_EXP_QP_BURST_SIGNALED;
1246 /* Should we enable HW CKSUM offload */
1248 (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM)) {
1249 send_flags |= IBV_EXP_QP_BURST_IP_CSUM;
1250 /* HW does not support checksum offloads at arbitrary
1251 * offsets but automatically recognizes the packet
1252 * type. For inner L3/L4 checksums, only VXLAN (UDP)
1253 * tunnels are currently supported.
1255 * FIXME: since PKT_TX_UDP_TUNNEL_PKT has been removed,
1256 * the outer packet type is unknown. All we know is
1257 * that the L2 header is of unusual length (not
1258 * ETHER_HDR_LEN with or without 802.1Q header). */
1259 if ((buf->l2_len != ETHER_HDR_LEN) &&
1260 (buf->l2_len != (ETHER_HDR_LEN + 4)))
1261 send_flags |= IBV_EXP_QP_BURST_TUNNEL;
1263 if (likely(segs == 1)) {
1268 /* Retrieve buffer information. */
1269 addr = rte_pktmbuf_mtod(buf, uintptr_t);
1270 length = DATA_LEN(buf);
1271 /* Retrieve Memory Region key for this memory pool. */
1272 lkey = txq_mp2mr(txq, buf->pool);
1273 if (unlikely(lkey == (uint32_t)-1)) {
1274 /* MR does not exist. */
1275 DEBUG("%p: unable to get MP <-> MR"
1276 " association", (void *)txq);
1277 /* Clean up TX element. */
1281 /* Update element. */
1284 rte_prefetch0((volatile void *)
1286 RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
1287 /* Put packet into send queue. */
1288 #if MLX4_PMD_MAX_INLINE > 0
1289 if (length <= txq->max_inline)
1290 err = txq->if_qp->send_pending_inline
1297 err = txq->if_qp->send_pending
1305 #ifdef MLX4_PMD_SOFT_COUNTERS
1306 sent_size += length;
1309 #if MLX4_PMD_SGE_WR_N > 1
1310 struct ibv_sge sges[MLX4_PMD_SGE_WR_N];
1311 struct tx_burst_sg_ret ret;
1313 ret = tx_burst_sg(txq, segs, elt, buf, elts_head,
1315 if (ret.length == (unsigned int)-1)
1317 RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
1318 /* Put SG list into send queue. */
1319 err = txq->if_qp->send_pending_sg_list
1326 #ifdef MLX4_PMD_SOFT_COUNTERS
1327 sent_size += ret.length;
1329 #else /* MLX4_PMD_SGE_WR_N > 1 */
1330 DEBUG("%p: TX scattered buffers support not"
1331 " compiled in", (void *)txq);
1333 #endif /* MLX4_PMD_SGE_WR_N > 1 */
1335 elts_head = elts_head_next;
1336 #ifdef MLX4_PMD_SOFT_COUNTERS
1337 /* Increment sent bytes counter. */
1338 txq->stats.obytes += sent_size;
1342 /* Take a shortcut if nothing must be sent. */
1343 if (unlikely(i == 0))
1345 #ifdef MLX4_PMD_SOFT_COUNTERS
1346 /* Increment sent packets counter. */
1347 txq->stats.opackets += i;
1349 /* Ring QP doorbell. */
1350 err = txq->if_qp->send_flush(txq->qp);
1351 if (unlikely(err)) {
1352 /* A nonzero value is not supposed to be returned.
1353 * Nothing can be done about it. */
1354 DEBUG("%p: send_flush() failed with error %d",
1357 txq->elts_head = elts_head;
1358 txq->elts_comp += elts_comp;
1359 txq->elts_comp_cd = elts_comp_cd;
1364 * Configure a TX queue.
1367 * Pointer to Ethernet device structure.
1369 * Pointer to TX queue structure.
1371 * Number of descriptors to configure in queue.
1373 * NUMA socket on which memory must be allocated.
1375 * Thresholds parameters.
1378 * 0 on success, errno value on failure.
1381 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
1382 unsigned int socket, const struct rte_eth_txconf *conf)
1384 struct priv *priv = dev->data->dev_private;
1390 struct ibv_exp_query_intf_params params;
1391 struct ibv_qp_init_attr init;
1392 struct ibv_exp_qp_attr mod;
1394 enum ibv_exp_query_intf_status status;
1397 (void)conf; /* Thresholds configuration (ignored). */
1398 if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
1399 ERROR("%p: invalid number of TX descriptors (must be a"
1400 " multiple of %d)", (void *)dev, MLX4_PMD_SGE_WR_N);
1403 desc /= MLX4_PMD_SGE_WR_N;
1404 /* MRs will be registered in mp2mr[] later. */
1405 tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
1406 if (tmpl.cq == NULL) {
1408 ERROR("%p: CQ creation failure: %s",
1409 (void *)dev, strerror(ret));
1412 DEBUG("priv->device_attr.max_qp_wr is %d",
1413 priv->device_attr.max_qp_wr);
1414 DEBUG("priv->device_attr.max_sge is %d",
1415 priv->device_attr.max_sge);
1416 attr.init = (struct ibv_qp_init_attr){
1417 /* CQ to be associated with the send queue. */
1419 /* CQ to be associated with the receive queue. */
1422 /* Max number of outstanding WRs. */
1423 .max_send_wr = ((priv->device_attr.max_qp_wr < desc) ?
1424 priv->device_attr.max_qp_wr :
1426 /* Max number of scatter/gather elements in a WR. */
1427 .max_send_sge = ((priv->device_attr.max_sge <
1428 MLX4_PMD_SGE_WR_N) ?
1429 priv->device_attr.max_sge :
1431 #if MLX4_PMD_MAX_INLINE > 0
1432 .max_inline_data = MLX4_PMD_MAX_INLINE,
1435 .qp_type = IBV_QPT_RAW_PACKET,
1436 /* Do *NOT* enable this, completions events are managed per
1440 tmpl.qp = ibv_create_qp(priv->pd, &attr.init);
1441 if (tmpl.qp == NULL) {
1442 ret = (errno ? errno : EINVAL);
1443 ERROR("%p: QP creation failure: %s",
1444 (void *)dev, strerror(ret));
1447 #if MLX4_PMD_MAX_INLINE > 0
1448 /* ibv_create_qp() updates this value. */
1449 tmpl.max_inline = attr.init.cap.max_inline_data;
1451 attr.mod = (struct ibv_exp_qp_attr){
1452 /* Move the QP to this state. */
1453 .qp_state = IBV_QPS_INIT,
1454 /* Primary port number. */
1455 .port_num = priv->port
1457 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod,
1458 (IBV_EXP_QP_STATE | IBV_EXP_QP_PORT));
1460 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
1461 (void *)dev, strerror(ret));
1464 ret = txq_alloc_elts(&tmpl, desc);
1466 ERROR("%p: TXQ allocation failed: %s",
1467 (void *)dev, strerror(ret));
1470 attr.mod = (struct ibv_exp_qp_attr){
1471 .qp_state = IBV_QPS_RTR
1473 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1475 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
1476 (void *)dev, strerror(ret));
1479 attr.mod.qp_state = IBV_QPS_RTS;
1480 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1482 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
1483 (void *)dev, strerror(ret));
1486 attr.params = (struct ibv_exp_query_intf_params){
1487 .intf_scope = IBV_EXP_INTF_GLOBAL,
1488 .intf = IBV_EXP_INTF_CQ,
1491 tmpl.if_cq = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
1492 if (tmpl.if_cq == NULL) {
1493 ERROR("%p: CQ interface family query failed with status %d",
1494 (void *)dev, status);
1497 attr.params = (struct ibv_exp_query_intf_params){
1498 .intf_scope = IBV_EXP_INTF_GLOBAL,
1499 .intf = IBV_EXP_INTF_QP_BURST,
1502 tmpl.if_qp = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
1503 if (tmpl.if_qp == NULL) {
1504 ERROR("%p: QP interface family query failed with status %d",
1505 (void *)dev, status);
1508 /* Clean up txq in case we're reinitializing it. */
1509 DEBUG("%p: cleaning-up old txq just in case", (void *)txq);
1512 DEBUG("%p: txq updated with %p", (void *)txq, (void *)&tmpl);
1522 * DPDK callback to configure a TX queue.
1525 * Pointer to Ethernet device structure.
1529 * Number of descriptors to configure in queue.
1531 * NUMA socket on which memory must be allocated.
1533 * Thresholds parameters.
1536 * 0 on success, negative errno value on failure.
1539 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1540 unsigned int socket, const struct rte_eth_txconf *conf)
1542 struct priv *priv = dev->data->dev_private;
1543 struct txq *txq = (*priv->txqs)[idx];
1547 DEBUG("%p: configuring queue %u for %u descriptors",
1548 (void *)dev, idx, desc);
1549 if (idx >= priv->txqs_n) {
1550 ERROR("%p: queue index out of range (%u >= %u)",
1551 (void *)dev, idx, priv->txqs_n);
1556 DEBUG("%p: reusing already allocated queue index %u (%p)",
1557 (void *)dev, idx, (void *)txq);
1558 if (priv->started) {
1562 (*priv->txqs)[idx] = NULL;
1565 txq = rte_calloc_socket("TXQ", 1, sizeof(*txq), 0, socket);
1567 ERROR("%p: unable to allocate queue index %u",
1573 ret = txq_setup(dev, txq, desc, socket, conf);
1577 txq->stats.idx = idx;
1578 DEBUG("%p: adding TX queue %p to list",
1579 (void *)dev, (void *)txq);
1580 (*priv->txqs)[idx] = txq;
1581 /* Update send callback. */
1582 dev->tx_pkt_burst = mlx4_tx_burst;
1589 * DPDK callback to release a TX queue.
1592 * Generic TX queue pointer.
1595 mlx4_tx_queue_release(void *dpdk_txq)
1597 struct txq *txq = (struct txq *)dpdk_txq;
1605 for (i = 0; (i != priv->txqs_n); ++i)
1606 if ((*priv->txqs)[i] == txq) {
1607 DEBUG("%p: removing TX queue %p from list",
1608 (void *)priv->dev, (void *)txq);
1609 (*priv->txqs)[i] = NULL;
1617 /* RX queues handling. */
1620 * Allocate RX queue elements with scattered packets support.
1623 * Pointer to RX queue structure.
1625 * Number of elements to allocate.
1627 * If not NULL, fetch buffers from this array instead of allocating them
1628 * with rte_pktmbuf_alloc().
1631 * 0 on success, errno value on failure.
1634 rxq_alloc_elts_sp(struct rxq *rxq, unsigned int elts_n,
1635 struct rte_mbuf **pool)
1638 struct rxq_elt_sp (*elts)[elts_n] =
1639 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1644 ERROR("%p: can't allocate packets array", (void *)rxq);
1648 /* For each WR (packet). */
1649 for (i = 0; (i != elts_n); ++i) {
1651 struct rxq_elt_sp *elt = &(*elts)[i];
1652 struct ibv_recv_wr *wr = &elt->wr;
1653 struct ibv_sge (*sges)[(elemof(elt->sges))] = &elt->sges;
1655 /* These two arrays must have the same size. */
1656 assert(elemof(elt->sges) == elemof(elt->bufs));
1659 wr->next = &(*elts)[(i + 1)].wr;
1660 wr->sg_list = &(*sges)[0];
1661 wr->num_sge = elemof(*sges);
1662 /* For each SGE (segment). */
1663 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1664 struct ibv_sge *sge = &(*sges)[j];
1665 struct rte_mbuf *buf;
1669 assert(buf != NULL);
1670 rte_pktmbuf_reset(buf);
1672 buf = rte_pktmbuf_alloc(rxq->mp);
1674 assert(pool == NULL);
1675 ERROR("%p: empty mbuf pool", (void *)rxq);
1680 /* Headroom is reserved by rte_pktmbuf_alloc(). */
1681 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1682 /* Buffer is supposed to be empty. */
1683 assert(rte_pktmbuf_data_len(buf) == 0);
1684 assert(rte_pktmbuf_pkt_len(buf) == 0);
1685 /* sge->addr must be able to store a pointer. */
1686 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1688 /* The first SGE keeps its headroom. */
1689 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
1690 sge->length = (buf->buf_len -
1691 RTE_PKTMBUF_HEADROOM);
1693 /* Subsequent SGEs lose theirs. */
1694 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1695 SET_DATA_OFF(buf, 0);
1696 sge->addr = (uintptr_t)buf->buf_addr;
1697 sge->length = buf->buf_len;
1699 sge->lkey = rxq->mr->lkey;
1700 /* Redundant check for tailroom. */
1701 assert(sge->length == rte_pktmbuf_tailroom(buf));
1704 /* The last WR pointer must be NULL. */
1705 (*elts)[(i - 1)].wr.next = NULL;
1706 DEBUG("%p: allocated and configured %u WRs (%zu segments)",
1707 (void *)rxq, elts_n, (elts_n * elemof((*elts)[0].sges)));
1708 rxq->elts_n = elts_n;
1710 rxq->elts.sp = elts;
1715 assert(pool == NULL);
1716 for (i = 0; (i != elemof(*elts)); ++i) {
1718 struct rxq_elt_sp *elt = &(*elts)[i];
1720 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1721 struct rte_mbuf *buf = elt->bufs[j];
1724 rte_pktmbuf_free_seg(buf);
1729 DEBUG("%p: failed, freed everything", (void *)rxq);
1735 * Free RX queue elements with scattered packets support.
1738 * Pointer to RX queue structure.
1741 rxq_free_elts_sp(struct rxq *rxq)
1744 unsigned int elts_n = rxq->elts_n;
1745 struct rxq_elt_sp (*elts)[elts_n] = rxq->elts.sp;
1747 DEBUG("%p: freeing WRs", (void *)rxq);
1749 rxq->elts.sp = NULL;
1752 for (i = 0; (i != elemof(*elts)); ++i) {
1754 struct rxq_elt_sp *elt = &(*elts)[i];
1756 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1757 struct rte_mbuf *buf = elt->bufs[j];
1760 rte_pktmbuf_free_seg(buf);
1767 * Allocate RX queue elements.
1770 * Pointer to RX queue structure.
1772 * Number of elements to allocate.
1774 * If not NULL, fetch buffers from this array instead of allocating them
1775 * with rte_pktmbuf_alloc().
1778 * 0 on success, errno value on failure.
1781 rxq_alloc_elts(struct rxq *rxq, unsigned int elts_n, struct rte_mbuf **pool)
1784 struct rxq_elt (*elts)[elts_n] =
1785 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1790 ERROR("%p: can't allocate packets array", (void *)rxq);
1794 /* For each WR (packet). */
1795 for (i = 0; (i != elts_n); ++i) {
1796 struct rxq_elt *elt = &(*elts)[i];
1797 struct ibv_recv_wr *wr = &elt->wr;
1798 struct ibv_sge *sge = &(*elts)[i].sge;
1799 struct rte_mbuf *buf;
1803 assert(buf != NULL);
1804 rte_pktmbuf_reset(buf);
1806 buf = rte_pktmbuf_alloc(rxq->mp);
1808 assert(pool == NULL);
1809 ERROR("%p: empty mbuf pool", (void *)rxq);
1813 /* Configure WR. Work request ID contains its own index in
1814 * the elts array and the offset between SGE buffer header and
1816 WR_ID(wr->wr_id).id = i;
1817 WR_ID(wr->wr_id).offset =
1818 (((uintptr_t)buf->buf_addr + RTE_PKTMBUF_HEADROOM) -
1820 wr->next = &(*elts)[(i + 1)].wr;
1823 /* Headroom is reserved by rte_pktmbuf_alloc(). */
1824 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1825 /* Buffer is supposed to be empty. */
1826 assert(rte_pktmbuf_data_len(buf) == 0);
1827 assert(rte_pktmbuf_pkt_len(buf) == 0);
1828 /* sge->addr must be able to store a pointer. */
1829 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1830 /* SGE keeps its headroom. */
1831 sge->addr = (uintptr_t)
1832 ((uint8_t *)buf->buf_addr + RTE_PKTMBUF_HEADROOM);
1833 sge->length = (buf->buf_len - RTE_PKTMBUF_HEADROOM);
1834 sge->lkey = rxq->mr->lkey;
1835 /* Redundant check for tailroom. */
1836 assert(sge->length == rte_pktmbuf_tailroom(buf));
1837 /* Make sure elts index and SGE mbuf pointer can be deduced
1839 if ((WR_ID(wr->wr_id).id != i) ||
1840 ((void *)((uintptr_t)sge->addr -
1841 WR_ID(wr->wr_id).offset) != buf)) {
1842 ERROR("%p: cannot store index and offset in WR ID",
1845 rte_pktmbuf_free(buf);
1850 /* The last WR pointer must be NULL. */
1851 (*elts)[(i - 1)].wr.next = NULL;
1852 DEBUG("%p: allocated and configured %u single-segment WRs",
1853 (void *)rxq, elts_n);
1854 rxq->elts_n = elts_n;
1856 rxq->elts.no_sp = elts;
1861 assert(pool == NULL);
1862 for (i = 0; (i != elemof(*elts)); ++i) {
1863 struct rxq_elt *elt = &(*elts)[i];
1864 struct rte_mbuf *buf;
1866 if (elt->sge.addr == 0)
1868 assert(WR_ID(elt->wr.wr_id).id == i);
1869 buf = (void *)((uintptr_t)elt->sge.addr -
1870 WR_ID(elt->wr.wr_id).offset);
1871 rte_pktmbuf_free_seg(buf);
1875 DEBUG("%p: failed, freed everything", (void *)rxq);
1881 * Free RX queue elements.
1884 * Pointer to RX queue structure.
1887 rxq_free_elts(struct rxq *rxq)
1890 unsigned int elts_n = rxq->elts_n;
1891 struct rxq_elt (*elts)[elts_n] = rxq->elts.no_sp;
1893 DEBUG("%p: freeing WRs", (void *)rxq);
1895 rxq->elts.no_sp = NULL;
1898 for (i = 0; (i != elemof(*elts)); ++i) {
1899 struct rxq_elt *elt = &(*elts)[i];
1900 struct rte_mbuf *buf;
1902 if (elt->sge.addr == 0)
1904 assert(WR_ID(elt->wr.wr_id).id == i);
1905 buf = (void *)((uintptr_t)elt->sge.addr -
1906 WR_ID(elt->wr.wr_id).offset);
1907 rte_pktmbuf_free_seg(buf);
1913 * Delete flow steering rule.
1916 * Pointer to RX queue structure.
1918 * MAC address index.
1923 rxq_del_flow(struct rxq *rxq, unsigned int mac_index, unsigned int vlan_index)
1926 struct priv *priv = rxq->priv;
1927 const uint8_t (*mac)[ETHER_ADDR_LEN] =
1928 (const uint8_t (*)[ETHER_ADDR_LEN])
1929 priv->mac[mac_index].addr_bytes;
1931 assert(rxq->mac_flow[mac_index][vlan_index] != NULL);
1932 DEBUG("%p: removing MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
1933 " (VLAN ID %" PRIu16 ")",
1935 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
1936 mac_index, priv->vlan_filter[vlan_index].id);
1937 claim_zero(ibv_destroy_flow(rxq->mac_flow[mac_index][vlan_index]));
1938 rxq->mac_flow[mac_index][vlan_index] = NULL;
1942 * Unregister a MAC address from a RX queue.
1945 * Pointer to RX queue structure.
1947 * MAC address index.
1950 rxq_mac_addr_del(struct rxq *rxq, unsigned int mac_index)
1952 struct priv *priv = rxq->priv;
1954 unsigned int vlans = 0;
1956 assert(mac_index < elemof(priv->mac));
1957 if (!BITFIELD_ISSET(rxq->mac_configured, mac_index))
1959 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
1960 if (!priv->vlan_filter[i].enabled)
1962 rxq_del_flow(rxq, mac_index, i);
1966 rxq_del_flow(rxq, mac_index, 0);
1968 BITFIELD_RESET(rxq->mac_configured, mac_index);
1972 * Unregister all MAC addresses from a RX queue.
1975 * Pointer to RX queue structure.
1978 rxq_mac_addrs_del(struct rxq *rxq)
1980 struct priv *priv = rxq->priv;
1983 for (i = 0; (i != elemof(priv->mac)); ++i)
1984 rxq_mac_addr_del(rxq, i);
1987 static int rxq_promiscuous_enable(struct rxq *);
1988 static void rxq_promiscuous_disable(struct rxq *);
1991 * Add single flow steering rule.
1994 * Pointer to RX queue structure.
1996 * MAC address index to register.
1998 * VLAN index. Use -1 for a flow without VLAN.
2001 * 0 on success, errno value on failure.
2004 rxq_add_flow(struct rxq *rxq, unsigned int mac_index, unsigned int vlan_index)
2006 struct ibv_flow *flow;
2007 struct priv *priv = rxq->priv;
2008 const uint8_t (*mac)[ETHER_ADDR_LEN] =
2009 (const uint8_t (*)[ETHER_ADDR_LEN])
2010 priv->mac[mac_index].addr_bytes;
2012 /* Allocate flow specification on the stack. */
2013 struct __attribute__((packed)) {
2014 struct ibv_flow_attr attr;
2015 struct ibv_flow_spec_eth spec;
2017 struct ibv_flow_attr *attr = &data.attr;
2018 struct ibv_flow_spec_eth *spec = &data.spec;
2020 assert(mac_index < elemof(priv->mac));
2021 assert((vlan_index < elemof(priv->vlan_filter)) || (vlan_index == -1u));
2023 * No padding must be inserted by the compiler between attr and spec.
2024 * This layout is expected by libibverbs.
2026 assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec);
2027 *attr = (struct ibv_flow_attr){
2028 .type = IBV_FLOW_ATTR_NORMAL,
2033 *spec = (struct ibv_flow_spec_eth){
2034 .type = IBV_FLOW_SPEC_ETH,
2035 .size = sizeof(*spec),
2038 (*mac)[0], (*mac)[1], (*mac)[2],
2039 (*mac)[3], (*mac)[4], (*mac)[5]
2041 .vlan_tag = ((vlan_index != -1u) ?
2042 htons(priv->vlan_filter[vlan_index].id) :
2046 .dst_mac = "\xff\xff\xff\xff\xff\xff",
2047 .vlan_tag = ((vlan_index != -1u) ? htons(0xfff) : 0),
2050 DEBUG("%p: adding MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
2051 " (VLAN %s %" PRIu16 ")",
2053 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
2055 ((vlan_index != -1u) ? "ID" : "index"),
2056 ((vlan_index != -1u) ? priv->vlan_filter[vlan_index].id : -1u));
2057 /* Create related flow. */
2059 flow = ibv_create_flow(rxq->qp, attr);
2061 /* It's not clear whether errno is always set in this case. */
2062 ERROR("%p: flow configuration failed, errno=%d: %s",
2064 (errno ? strerror(errno) : "Unknown error"));
2069 if (vlan_index == -1u)
2071 assert(rxq->mac_flow[mac_index][vlan_index] == NULL);
2072 rxq->mac_flow[mac_index][vlan_index] = flow;
2077 * Register a MAC address in a RX queue.
2080 * Pointer to RX queue structure.
2082 * MAC address index to register.
2085 * 0 on success, errno value on failure.
2088 rxq_mac_addr_add(struct rxq *rxq, unsigned int mac_index)
2090 struct priv *priv = rxq->priv;
2092 unsigned int vlans = 0;
2095 assert(mac_index < elemof(priv->mac));
2096 if (BITFIELD_ISSET(rxq->mac_configured, mac_index))
2097 rxq_mac_addr_del(rxq, mac_index);
2098 /* Fill VLAN specifications. */
2099 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
2100 if (!priv->vlan_filter[i].enabled)
2102 /* Create related flow. */
2103 ret = rxq_add_flow(rxq, mac_index, i);
2108 /* Failure, rollback. */
2110 if (priv->vlan_filter[--i].enabled)
2111 rxq_del_flow(rxq, mac_index, i);
2115 /* In case there is no VLAN filter. */
2117 ret = rxq_add_flow(rxq, mac_index, -1);
2121 BITFIELD_SET(rxq->mac_configured, mac_index);
2126 * Register all MAC addresses in a RX queue.
2129 * Pointer to RX queue structure.
2132 * 0 on success, errno value on failure.
2135 rxq_mac_addrs_add(struct rxq *rxq)
2137 struct priv *priv = rxq->priv;
2141 for (i = 0; (i != elemof(priv->mac)); ++i) {
2142 if (!BITFIELD_ISSET(priv->mac_configured, i))
2144 ret = rxq_mac_addr_add(rxq, i);
2147 /* Failure, rollback. */
2149 rxq_mac_addr_del(rxq, --i);
2157 * Unregister a MAC address.
2159 * In RSS mode, the MAC address is unregistered from the parent queue,
2160 * otherwise it is unregistered from each queue directly.
2163 * Pointer to private structure.
2165 * MAC address index.
2168 priv_mac_addr_del(struct priv *priv, unsigned int mac_index)
2172 assert(mac_index < elemof(priv->mac));
2173 if (!BITFIELD_ISSET(priv->mac_configured, mac_index))
2176 rxq_mac_addr_del(&priv->rxq_parent, mac_index);
2179 for (i = 0; (i != priv->dev->data->nb_rx_queues); ++i)
2180 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2182 BITFIELD_RESET(priv->mac_configured, mac_index);
2186 * Register a MAC address.
2188 * In RSS mode, the MAC address is registered in the parent queue,
2189 * otherwise it is registered in each queue directly.
2192 * Pointer to private structure.
2194 * MAC address index to use.
2196 * MAC address to register.
2199 * 0 on success, errno value on failure.
2202 priv_mac_addr_add(struct priv *priv, unsigned int mac_index,
2203 const uint8_t (*mac)[ETHER_ADDR_LEN])
2208 assert(mac_index < elemof(priv->mac));
2209 /* First, make sure this address isn't already configured. */
2210 for (i = 0; (i != elemof(priv->mac)); ++i) {
2211 /* Skip this index, it's going to be reconfigured. */
2214 if (!BITFIELD_ISSET(priv->mac_configured, i))
2216 if (memcmp(priv->mac[i].addr_bytes, *mac, sizeof(*mac)))
2218 /* Address already configured elsewhere, return with error. */
2221 if (BITFIELD_ISSET(priv->mac_configured, mac_index))
2222 priv_mac_addr_del(priv, mac_index);
2223 priv->mac[mac_index] = (struct ether_addr){
2225 (*mac)[0], (*mac)[1], (*mac)[2],
2226 (*mac)[3], (*mac)[4], (*mac)[5]
2229 /* If device isn't started, this is all we need to do. */
2230 if (!priv->started) {
2232 /* Verify that all queues have this index disabled. */
2233 for (i = 0; (i != priv->rxqs_n); ++i) {
2234 if ((*priv->rxqs)[i] == NULL)
2236 assert(!BITFIELD_ISSET
2237 ((*priv->rxqs)[i]->mac_configured, mac_index));
2243 ret = rxq_mac_addr_add(&priv->rxq_parent, mac_index);
2248 for (i = 0; (i != priv->rxqs_n); ++i) {
2249 if ((*priv->rxqs)[i] == NULL)
2251 ret = rxq_mac_addr_add((*priv->rxqs)[i], mac_index);
2254 /* Failure, rollback. */
2256 if ((*priv->rxqs)[(--i)] != NULL)
2257 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2261 BITFIELD_SET(priv->mac_configured, mac_index);
2266 * Enable allmulti mode in a RX queue.
2269 * Pointer to RX queue structure.
2272 * 0 on success, errno value on failure.
2275 rxq_allmulticast_enable(struct rxq *rxq)
2277 struct ibv_flow *flow;
2278 struct ibv_flow_attr attr = {
2279 .type = IBV_FLOW_ATTR_MC_DEFAULT,
2281 .port = rxq->priv->port,
2285 DEBUG("%p: enabling allmulticast mode", (void *)rxq);
2286 if (rxq->allmulti_flow != NULL)
2289 flow = ibv_create_flow(rxq->qp, &attr);
2291 /* It's not clear whether errno is always set in this case. */
2292 ERROR("%p: flow configuration failed, errno=%d: %s",
2294 (errno ? strerror(errno) : "Unknown error"));
2299 rxq->allmulti_flow = flow;
2300 DEBUG("%p: allmulticast mode enabled", (void *)rxq);
2305 * Disable allmulti mode in a RX queue.
2308 * Pointer to RX queue structure.
2311 rxq_allmulticast_disable(struct rxq *rxq)
2313 DEBUG("%p: disabling allmulticast mode", (void *)rxq);
2314 if (rxq->allmulti_flow == NULL)
2316 claim_zero(ibv_destroy_flow(rxq->allmulti_flow));
2317 rxq->allmulti_flow = NULL;
2318 DEBUG("%p: allmulticast mode disabled", (void *)rxq);
2322 * Enable promiscuous mode in a RX queue.
2325 * Pointer to RX queue structure.
2328 * 0 on success, errno value on failure.
2331 rxq_promiscuous_enable(struct rxq *rxq)
2333 struct ibv_flow *flow;
2334 struct ibv_flow_attr attr = {
2335 .type = IBV_FLOW_ATTR_ALL_DEFAULT,
2337 .port = rxq->priv->port,
2343 DEBUG("%p: enabling promiscuous mode", (void *)rxq);
2344 if (rxq->promisc_flow != NULL)
2347 flow = ibv_create_flow(rxq->qp, &attr);
2349 /* It's not clear whether errno is always set in this case. */
2350 ERROR("%p: flow configuration failed, errno=%d: %s",
2352 (errno ? strerror(errno) : "Unknown error"));
2357 rxq->promisc_flow = flow;
2358 DEBUG("%p: promiscuous mode enabled", (void *)rxq);
2363 * Disable promiscuous mode in a RX queue.
2366 * Pointer to RX queue structure.
2369 rxq_promiscuous_disable(struct rxq *rxq)
2373 DEBUG("%p: disabling promiscuous mode", (void *)rxq);
2374 if (rxq->promisc_flow == NULL)
2376 claim_zero(ibv_destroy_flow(rxq->promisc_flow));
2377 rxq->promisc_flow = NULL;
2378 DEBUG("%p: promiscuous mode disabled", (void *)rxq);
2382 * Clean up a RX queue.
2384 * Destroy objects, free allocated memory and reset the structure for reuse.
2387 * Pointer to RX queue structure.
2390 rxq_cleanup(struct rxq *rxq)
2392 struct ibv_exp_release_intf_params params;
2394 DEBUG("cleaning up %p", (void *)rxq);
2396 rxq_free_elts_sp(rxq);
2399 if (rxq->if_qp != NULL) {
2400 assert(rxq->priv != NULL);
2401 assert(rxq->priv->ctx != NULL);
2402 assert(rxq->qp != NULL);
2403 params = (struct ibv_exp_release_intf_params){
2406 claim_zero(ibv_exp_release_intf(rxq->priv->ctx,
2410 if (rxq->if_cq != NULL) {
2411 assert(rxq->priv != NULL);
2412 assert(rxq->priv->ctx != NULL);
2413 assert(rxq->cq != NULL);
2414 params = (struct ibv_exp_release_intf_params){
2417 claim_zero(ibv_exp_release_intf(rxq->priv->ctx,
2421 if (rxq->qp != NULL) {
2422 rxq_promiscuous_disable(rxq);
2423 rxq_allmulticast_disable(rxq);
2424 rxq_mac_addrs_del(rxq);
2425 claim_zero(ibv_destroy_qp(rxq->qp));
2427 if (rxq->cq != NULL)
2428 claim_zero(ibv_destroy_cq(rxq->cq));
2429 if (rxq->mr != NULL)
2430 claim_zero(ibv_dereg_mr(rxq->mr));
2431 memset(rxq, 0, sizeof(*rxq));
2435 * Translate RX completion flags to offload flags.
2438 * Pointer to RX queue structure.
2440 * RX completion flags returned by poll_length_flags().
2443 * Offload flags (ol_flags) for struct rte_mbuf.
2445 static inline uint32_t
2446 rxq_cq_to_ol_flags(const struct rxq *rxq, uint32_t flags)
2451 TRANSPOSE(flags, IBV_EXP_CQ_RX_IPV4_PACKET, PKT_RX_IPV4_HDR) |
2452 TRANSPOSE(flags, IBV_EXP_CQ_RX_IPV6_PACKET, PKT_RX_IPV6_HDR);
2456 IBV_EXP_CQ_RX_IP_CSUM_OK,
2457 PKT_RX_IP_CKSUM_BAD) |
2459 IBV_EXP_CQ_RX_TCP_UDP_CSUM_OK,
2460 PKT_RX_L4_CKSUM_BAD);
2462 * PKT_RX_IP_CKSUM_BAD and PKT_RX_L4_CKSUM_BAD are used in place
2463 * of PKT_RX_EIP_CKSUM_BAD because the latter is not functional
2466 if ((flags & IBV_EXP_CQ_RX_TUNNEL_PACKET) && (rxq->csum_l2tun))
2469 IBV_EXP_CQ_RX_OUTER_IPV4_PACKET,
2470 PKT_RX_TUNNEL_IPV4_HDR) |
2472 IBV_EXP_CQ_RX_OUTER_IPV6_PACKET,
2473 PKT_RX_TUNNEL_IPV6_HDR) |
2475 IBV_EXP_CQ_RX_OUTER_IP_CSUM_OK,
2476 PKT_RX_IP_CKSUM_BAD) |
2478 IBV_EXP_CQ_RX_OUTER_TCP_UDP_CSUM_OK,
2479 PKT_RX_L4_CKSUM_BAD);
2484 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n);
2487 * DPDK callback for RX with scattered packets support.
2490 * Generic pointer to RX queue structure.
2492 * Array to store received packets.
2494 * Maximum number of packets in array.
2497 * Number of packets successfully received (<= pkts_n).
2500 mlx4_rx_burst_sp(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2502 struct rxq *rxq = (struct rxq *)dpdk_rxq;
2503 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
2504 const unsigned int elts_n = rxq->elts_n;
2505 unsigned int elts_head = rxq->elts_head;
2506 struct ibv_recv_wr head;
2507 struct ibv_recv_wr **next = &head.next;
2508 struct ibv_recv_wr *bad_wr;
2510 unsigned int pkts_ret = 0;
2513 if (unlikely(!rxq->sp))
2514 return mlx4_rx_burst(dpdk_rxq, pkts, pkts_n);
2515 if (unlikely(elts == NULL)) /* See RTE_DEV_CMD_SET_MTU. */
2517 for (i = 0; (i != pkts_n); ++i) {
2518 struct rxq_elt_sp *elt = &(*elts)[elts_head];
2519 struct ibv_recv_wr *wr = &elt->wr;
2520 uint64_t wr_id = wr->wr_id;
2522 unsigned int pkt_buf_len;
2523 struct rte_mbuf *pkt_buf = NULL; /* Buffer returned in pkts. */
2524 struct rte_mbuf **pkt_buf_next = &pkt_buf;
2525 unsigned int seg_headroom = RTE_PKTMBUF_HEADROOM;
2529 /* Sanity checks. */
2533 assert(wr_id < rxq->elts_n);
2534 assert(wr->sg_list == elt->sges);
2535 assert(wr->num_sge == elemof(elt->sges));
2536 assert(elts_head < rxq->elts_n);
2537 assert(rxq->elts_head < rxq->elts_n);
2538 ret = rxq->if_cq->poll_length_flags(rxq->cq, NULL, NULL,
2540 if (unlikely(ret < 0)) {
2544 DEBUG("rxq=%p, poll_length() failed (ret=%d)",
2546 /* ibv_poll_cq() must be used in case of failure. */
2547 wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
2548 if (unlikely(wcs_n == 0))
2550 if (unlikely(wcs_n < 0)) {
2551 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
2552 (void *)rxq, wcs_n);
2556 if (unlikely(wc.status != IBV_WC_SUCCESS)) {
2557 /* Whatever, just repost the offending WR. */
2558 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
2559 " completion status (%d): %s",
2560 (void *)rxq, wc.wr_id, wc.status,
2561 ibv_wc_status_str(wc.status));
2562 #ifdef MLX4_PMD_SOFT_COUNTERS
2563 /* Increment dropped packets counter. */
2564 ++rxq->stats.idropped;
2566 /* Link completed WRs together for repost. */
2577 /* Link completed WRs together for repost. */
2581 * Replace spent segments with new ones, concatenate and
2582 * return them as pkt_buf.
2585 struct ibv_sge *sge = &elt->sges[j];
2586 struct rte_mbuf *seg = elt->bufs[j];
2587 struct rte_mbuf *rep;
2588 unsigned int seg_tailroom;
2591 * Fetch initial bytes of packet descriptor into a
2592 * cacheline while allocating rep.
2595 rep = __rte_mbuf_raw_alloc(rxq->mp);
2596 if (unlikely(rep == NULL)) {
2598 * Unable to allocate a replacement mbuf,
2601 DEBUG("rxq=%p, wr_id=%" PRIu64 ":"
2602 " can't allocate a new mbuf",
2603 (void *)rxq, wr_id);
2604 if (pkt_buf != NULL) {
2605 *pkt_buf_next = NULL;
2606 rte_pktmbuf_free(pkt_buf);
2608 /* Increase out of memory counters. */
2609 ++rxq->stats.rx_nombuf;
2610 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
2614 /* Poison user-modifiable fields in rep. */
2615 NEXT(rep) = (void *)((uintptr_t)-1);
2616 SET_DATA_OFF(rep, 0xdead);
2617 DATA_LEN(rep) = 0xd00d;
2618 PKT_LEN(rep) = 0xdeadd00d;
2619 NB_SEGS(rep) = 0x2a;
2623 assert(rep->buf_len == seg->buf_len);
2624 assert(rep->buf_len == rxq->mb_len);
2625 /* Reconfigure sge to use rep instead of seg. */
2626 assert(sge->lkey == rxq->mr->lkey);
2627 sge->addr = ((uintptr_t)rep->buf_addr + seg_headroom);
2630 /* Update pkt_buf if it's the first segment, or link
2631 * seg to the previous one and update pkt_buf_next. */
2632 *pkt_buf_next = seg;
2633 pkt_buf_next = &NEXT(seg);
2634 /* Update seg information. */
2635 seg_tailroom = (seg->buf_len - seg_headroom);
2636 assert(sge->length == seg_tailroom);
2637 SET_DATA_OFF(seg, seg_headroom);
2638 if (likely(len <= seg_tailroom)) {
2640 DATA_LEN(seg) = len;
2643 assert(rte_pktmbuf_headroom(seg) ==
2645 assert(rte_pktmbuf_tailroom(seg) ==
2646 (seg_tailroom - len));
2649 DATA_LEN(seg) = seg_tailroom;
2650 PKT_LEN(seg) = seg_tailroom;
2652 assert(rte_pktmbuf_headroom(seg) == seg_headroom);
2653 assert(rte_pktmbuf_tailroom(seg) == 0);
2654 /* Fix len and clear headroom for next segments. */
2655 len -= seg_tailroom;
2658 /* Update head and tail segments. */
2659 *pkt_buf_next = NULL;
2660 assert(pkt_buf != NULL);
2662 NB_SEGS(pkt_buf) = j;
2663 PORT(pkt_buf) = rxq->port_id;
2664 PKT_LEN(pkt_buf) = pkt_buf_len;
2665 pkt_buf->ol_flags = rxq_cq_to_ol_flags(rxq, flags);
2667 /* Return packet. */
2668 *(pkts++) = pkt_buf;
2670 #ifdef MLX4_PMD_SOFT_COUNTERS
2671 /* Increase bytes counter. */
2672 rxq->stats.ibytes += pkt_buf_len;
2675 if (++elts_head >= elts_n)
2679 if (unlikely(i == 0))
2684 DEBUG("%p: reposting %d WRs", (void *)rxq, i);
2686 ret = ibv_post_recv(rxq->qp, head.next, &bad_wr);
2687 if (unlikely(ret)) {
2688 /* Inability to repost WRs is fatal. */
2689 DEBUG("%p: ibv_post_recv(): failed for WR %p: %s",
2695 rxq->elts_head = elts_head;
2696 #ifdef MLX4_PMD_SOFT_COUNTERS
2697 /* Increase packets counter. */
2698 rxq->stats.ipackets += pkts_ret;
2704 * DPDK callback for RX.
2706 * The following function is the same as mlx4_rx_burst_sp(), except it doesn't
2707 * manage scattered packets. Improves performance when MRU is lower than the
2708 * size of the first segment.
2711 * Generic pointer to RX queue structure.
2713 * Array to store received packets.
2715 * Maximum number of packets in array.
2718 * Number of packets successfully received (<= pkts_n).
2721 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2723 struct rxq *rxq = (struct rxq *)dpdk_rxq;
2724 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
2725 const unsigned int elts_n = rxq->elts_n;
2726 unsigned int elts_head = rxq->elts_head;
2727 struct ibv_sge sges[pkts_n];
2729 unsigned int pkts_ret = 0;
2732 if (unlikely(rxq->sp))
2733 return mlx4_rx_burst_sp(dpdk_rxq, pkts, pkts_n);
2734 for (i = 0; (i != pkts_n); ++i) {
2735 struct rxq_elt *elt = &(*elts)[elts_head];
2736 struct ibv_recv_wr *wr = &elt->wr;
2737 uint64_t wr_id = wr->wr_id;
2739 struct rte_mbuf *seg = (void *)((uintptr_t)elt->sge.addr -
2740 WR_ID(wr_id).offset);
2741 struct rte_mbuf *rep;
2744 /* Sanity checks. */
2745 assert(WR_ID(wr_id).id < rxq->elts_n);
2746 assert(wr->sg_list == &elt->sge);
2747 assert(wr->num_sge == 1);
2748 assert(elts_head < rxq->elts_n);
2749 assert(rxq->elts_head < rxq->elts_n);
2750 ret = rxq->if_cq->poll_length_flags(rxq->cq, NULL, NULL,
2752 if (unlikely(ret < 0)) {
2756 DEBUG("rxq=%p, poll_length() failed (ret=%d)",
2758 /* ibv_poll_cq() must be used in case of failure. */
2759 wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
2760 if (unlikely(wcs_n == 0))
2762 if (unlikely(wcs_n < 0)) {
2763 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
2764 (void *)rxq, wcs_n);
2768 if (unlikely(wc.status != IBV_WC_SUCCESS)) {
2769 /* Whatever, just repost the offending WR. */
2770 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
2771 " completion status (%d): %s",
2772 (void *)rxq, wc.wr_id, wc.status,
2773 ibv_wc_status_str(wc.status));
2774 #ifdef MLX4_PMD_SOFT_COUNTERS
2775 /* Increment dropped packets counter. */
2776 ++rxq->stats.idropped;
2778 /* Add SGE to array for repost. */
2788 * Fetch initial bytes of packet descriptor into a
2789 * cacheline while allocating rep.
2792 rep = __rte_mbuf_raw_alloc(rxq->mp);
2793 if (unlikely(rep == NULL)) {
2795 * Unable to allocate a replacement mbuf,
2798 DEBUG("rxq=%p, wr_id=%" PRIu32 ":"
2799 " can't allocate a new mbuf",
2800 (void *)rxq, WR_ID(wr_id).id);
2801 /* Increase out of memory counters. */
2802 ++rxq->stats.rx_nombuf;
2803 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
2807 /* Reconfigure sge to use rep instead of seg. */
2808 elt->sge.addr = (uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM;
2809 assert(elt->sge.lkey == rxq->mr->lkey);
2810 WR_ID(wr->wr_id).offset =
2811 (((uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM) -
2813 assert(WR_ID(wr->wr_id).id == WR_ID(wr_id).id);
2815 /* Add SGE to array for repost. */
2818 /* Update seg information. */
2819 SET_DATA_OFF(seg, RTE_PKTMBUF_HEADROOM);
2821 PORT(seg) = rxq->port_id;
2824 DATA_LEN(seg) = len;
2825 seg->ol_flags = rxq_cq_to_ol_flags(rxq, flags);
2827 /* Return packet. */
2830 #ifdef MLX4_PMD_SOFT_COUNTERS
2831 /* Increase bytes counter. */
2832 rxq->stats.ibytes += len;
2835 if (++elts_head >= elts_n)
2839 if (unlikely(i == 0))
2843 DEBUG("%p: reposting %u WRs", (void *)rxq, i);
2845 ret = rxq->if_qp->recv_burst(rxq->qp, sges, i);
2846 if (unlikely(ret)) {
2847 /* Inability to repost WRs is fatal. */
2848 DEBUG("%p: recv_burst(): failed (ret=%d)",
2853 rxq->elts_head = elts_head;
2854 #ifdef MLX4_PMD_SOFT_COUNTERS
2855 /* Increase packets counter. */
2856 rxq->stats.ipackets += pkts_ret;
2862 * Allocate a Queue Pair.
2863 * Optionally setup inline receive if supported.
2866 * Pointer to private structure.
2868 * Completion queue to associate with QP.
2870 * Number of descriptors in QP (hint only).
2873 * QP pointer or NULL in case of error.
2875 static struct ibv_qp *
2876 rxq_setup_qp(struct priv *priv, struct ibv_cq *cq, uint16_t desc)
2878 struct ibv_exp_qp_init_attr attr = {
2879 /* CQ to be associated with the send queue. */
2881 /* CQ to be associated with the receive queue. */
2884 /* Max number of outstanding WRs. */
2885 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
2886 priv->device_attr.max_qp_wr :
2888 /* Max number of scatter/gather elements in a WR. */
2889 .max_recv_sge = ((priv->device_attr.max_sge <
2890 MLX4_PMD_SGE_WR_N) ?
2891 priv->device_attr.max_sge :
2894 .qp_type = IBV_QPT_RAW_PACKET,
2895 .comp_mask = IBV_EXP_QP_INIT_ATTR_PD,
2900 attr.max_inl_recv = priv->inl_recv_size;
2901 attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
2903 return ibv_exp_create_qp(priv->ctx, &attr);
2909 * Allocate a RSS Queue Pair.
2910 * Optionally setup inline receive if supported.
2913 * Pointer to private structure.
2915 * Completion queue to associate with QP.
2917 * Number of descriptors in QP (hint only).
2919 * If nonzero, create a parent QP, otherwise a child.
2922 * QP pointer or NULL in case of error.
2924 static struct ibv_qp *
2925 rxq_setup_qp_rss(struct priv *priv, struct ibv_cq *cq, uint16_t desc,
2928 struct ibv_exp_qp_init_attr attr = {
2929 /* CQ to be associated with the send queue. */
2931 /* CQ to be associated with the receive queue. */
2934 /* Max number of outstanding WRs. */
2935 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
2936 priv->device_attr.max_qp_wr :
2938 /* Max number of scatter/gather elements in a WR. */
2939 .max_recv_sge = ((priv->device_attr.max_sge <
2940 MLX4_PMD_SGE_WR_N) ?
2941 priv->device_attr.max_sge :
2944 .qp_type = IBV_QPT_RAW_PACKET,
2945 .comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
2946 IBV_EXP_QP_INIT_ATTR_QPG),
2951 attr.max_inl_recv = priv->inl_recv_size,
2952 attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
2955 attr.qpg.qpg_type = IBV_EXP_QPG_PARENT;
2956 /* TSS isn't necessary. */
2957 attr.qpg.parent_attrib.tss_child_count = 0;
2958 attr.qpg.parent_attrib.rss_child_count = priv->rxqs_n;
2959 DEBUG("initializing parent RSS queue");
2961 attr.qpg.qpg_type = IBV_EXP_QPG_CHILD_RX;
2962 attr.qpg.qpg_parent = priv->rxq_parent.qp;
2963 DEBUG("initializing child RSS queue");
2965 return ibv_exp_create_qp(priv->ctx, &attr);
2968 #endif /* RSS_SUPPORT */
2971 * Reconfigure a RX queue with new parameters.
2973 * rxq_rehash() does not allocate mbufs, which, if not done from the right
2974 * thread (such as a control thread), may corrupt the pool.
2975 * In case of failure, the queue is left untouched.
2978 * Pointer to Ethernet device structure.
2983 * 0 on success, errno value on failure.
2986 rxq_rehash(struct rte_eth_dev *dev, struct rxq *rxq)
2988 struct priv *priv = rxq->priv;
2989 struct rxq tmpl = *rxq;
2990 unsigned int mbuf_n;
2991 unsigned int desc_n;
2992 struct rte_mbuf **pool;
2994 struct ibv_exp_qp_attr mod;
2995 struct ibv_recv_wr *bad_wr;
2997 int parent = (rxq == &priv->rxq_parent);
3000 ERROR("%p: cannot rehash parent queue %p",
3001 (void *)dev, (void *)rxq);
3004 DEBUG("%p: rehashing queue %p", (void *)dev, (void *)rxq);
3005 /* Number of descriptors and mbufs currently allocated. */
3006 desc_n = (tmpl.elts_n * (tmpl.sp ? MLX4_PMD_SGE_WR_N : 1));
3008 /* Toggle RX checksum offload if hardware supports it. */
3009 if (priv->hw_csum) {
3010 tmpl.csum = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3011 rxq->csum = tmpl.csum;
3013 if (priv->hw_csum_l2tun) {
3014 tmpl.csum_l2tun = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3015 rxq->csum_l2tun = tmpl.csum_l2tun;
3017 /* Enable scattered packets support for this queue if necessary. */
3018 if ((dev->data->dev_conf.rxmode.jumbo_frame) &&
3019 (dev->data->dev_conf.rxmode.max_rx_pkt_len >
3020 (tmpl.mb_len - RTE_PKTMBUF_HEADROOM))) {
3022 desc_n /= MLX4_PMD_SGE_WR_N;
3025 DEBUG("%p: %s scattered packets support (%u WRs)",
3026 (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc_n);
3027 /* If scatter mode is the same as before, nothing to do. */
3028 if (tmpl.sp == rxq->sp) {
3029 DEBUG("%p: nothing to do", (void *)dev);
3032 /* Remove attached flows if RSS is disabled (no parent queue). */
3034 rxq_allmulticast_disable(&tmpl);
3035 rxq_promiscuous_disable(&tmpl);
3036 rxq_mac_addrs_del(&tmpl);
3037 /* Update original queue in case of failure. */
3038 rxq->allmulti_flow = tmpl.allmulti_flow;
3039 rxq->promisc_flow = tmpl.promisc_flow;
3040 memcpy(rxq->mac_configured, tmpl.mac_configured,
3041 sizeof(rxq->mac_configured));
3042 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
3044 /* From now on, any failure will render the queue unusable.
3045 * Reinitialize QP. */
3046 mod = (struct ibv_exp_qp_attr){ .qp_state = IBV_QPS_RESET };
3047 err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3049 ERROR("%p: cannot reset QP: %s", (void *)dev, strerror(err));
3053 err = ibv_resize_cq(tmpl.cq, desc_n);
3055 ERROR("%p: cannot resize CQ: %s", (void *)dev, strerror(err));
3059 mod = (struct ibv_exp_qp_attr){
3060 /* Move the QP to this state. */
3061 .qp_state = IBV_QPS_INIT,
3062 /* Primary port number. */
3063 .port_num = priv->port
3065 err = ibv_exp_modify_qp(tmpl.qp, &mod,
3068 (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
3069 #endif /* RSS_SUPPORT */
3072 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
3073 (void *)dev, strerror(err));
3077 /* Reconfigure flows. Do not care for errors. */
3079 rxq_mac_addrs_add(&tmpl);
3081 rxq_promiscuous_enable(&tmpl);
3083 rxq_allmulticast_enable(&tmpl);
3084 /* Update original queue in case of failure. */
3085 rxq->allmulti_flow = tmpl.allmulti_flow;
3086 rxq->promisc_flow = tmpl.promisc_flow;
3087 memcpy(rxq->mac_configured, tmpl.mac_configured,
3088 sizeof(rxq->mac_configured));
3089 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
3091 /* Allocate pool. */
3092 pool = rte_malloc(__func__, (mbuf_n * sizeof(*pool)), 0);
3094 ERROR("%p: cannot allocate memory", (void *)dev);
3097 /* Snatch mbufs from original queue. */
3100 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
3102 for (i = 0; (i != elemof(*elts)); ++i) {
3103 struct rxq_elt_sp *elt = &(*elts)[i];
3106 for (j = 0; (j != elemof(elt->bufs)); ++j) {
3107 assert(elt->bufs[j] != NULL);
3108 pool[k++] = elt->bufs[j];
3112 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
3114 for (i = 0; (i != elemof(*elts)); ++i) {
3115 struct rxq_elt *elt = &(*elts)[i];
3116 struct rte_mbuf *buf = (void *)
3117 ((uintptr_t)elt->sge.addr -
3118 WR_ID(elt->wr.wr_id).offset);
3120 assert(WR_ID(elt->wr.wr_id).id == i);
3124 assert(k == mbuf_n);
3126 tmpl.elts.sp = NULL;
3127 assert((void *)&tmpl.elts.sp == (void *)&tmpl.elts.no_sp);
3129 rxq_alloc_elts_sp(&tmpl, desc_n, pool) :
3130 rxq_alloc_elts(&tmpl, desc_n, pool));
3132 ERROR("%p: cannot reallocate WRs, aborting", (void *)dev);
3137 assert(tmpl.elts_n == desc_n);
3138 assert(tmpl.elts.sp != NULL);
3140 /* Clean up original data. */
3142 rte_free(rxq->elts.sp);
3143 rxq->elts.sp = NULL;
3145 err = ibv_post_recv(tmpl.qp,
3147 &(*tmpl.elts.sp)[0].wr :
3148 &(*tmpl.elts.no_sp)[0].wr),
3151 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
3157 mod = (struct ibv_exp_qp_attr){
3158 .qp_state = IBV_QPS_RTR
3160 err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3162 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
3163 (void *)dev, strerror(err));
3171 * Configure a RX queue.
3174 * Pointer to Ethernet device structure.
3176 * Pointer to RX queue structure.
3178 * Number of descriptors to configure in queue.
3180 * NUMA socket on which memory must be allocated.
3182 * Thresholds parameters.
3184 * Memory pool for buffer allocations.
3187 * 0 on success, errno value on failure.
3190 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
3191 unsigned int socket, const struct rte_eth_rxconf *conf,
3192 struct rte_mempool *mp)
3194 struct priv *priv = dev->data->dev_private;
3200 struct ibv_exp_qp_attr mod;
3202 struct ibv_exp_query_intf_params params;
3204 enum ibv_exp_query_intf_status status;
3205 struct ibv_recv_wr *bad_wr;
3206 struct rte_mbuf *buf;
3208 int parent = (rxq == &priv->rxq_parent);
3210 (void)conf; /* Thresholds configuration (ignored). */
3212 * If this is a parent queue, hardware must support RSS and
3213 * RSS must be enabled.
3215 assert((!parent) || ((priv->hw_rss) && (priv->rss)));
3217 /* Even if unused, ibv_create_cq() requires at least one
3222 if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
3223 ERROR("%p: invalid number of RX descriptors (must be a"
3224 " multiple of %d)", (void *)dev, MLX4_PMD_SGE_WR_N);
3227 /* Get mbuf length. */
3228 buf = rte_pktmbuf_alloc(mp);
3230 ERROR("%p: unable to allocate mbuf", (void *)dev);
3233 tmpl.mb_len = buf->buf_len;
3234 assert((rte_pktmbuf_headroom(buf) +
3235 rte_pktmbuf_tailroom(buf)) == tmpl.mb_len);
3236 assert(rte_pktmbuf_headroom(buf) == RTE_PKTMBUF_HEADROOM);
3237 rte_pktmbuf_free(buf);
3238 /* Toggle RX checksum offload if hardware supports it. */
3240 tmpl.csum = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3241 if (priv->hw_csum_l2tun)
3242 tmpl.csum_l2tun = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3243 /* Enable scattered packets support for this queue if necessary. */
3244 if ((dev->data->dev_conf.rxmode.jumbo_frame) &&
3245 (dev->data->dev_conf.rxmode.max_rx_pkt_len >
3246 (tmpl.mb_len - RTE_PKTMBUF_HEADROOM))) {
3248 desc /= MLX4_PMD_SGE_WR_N;
3250 DEBUG("%p: %s scattered packets support (%u WRs)",
3251 (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc);
3252 /* Use the entire RX mempool as the memory region. */
3253 tmpl.mr = ibv_reg_mr(priv->pd,
3254 (void *)mp->elt_va_start,
3255 (mp->elt_va_end - mp->elt_va_start),
3256 (IBV_ACCESS_LOCAL_WRITE |
3257 IBV_ACCESS_REMOTE_WRITE));
3258 if (tmpl.mr == NULL) {
3260 ERROR("%p: MR creation failure: %s",
3261 (void *)dev, strerror(ret));
3265 tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
3266 if (tmpl.cq == NULL) {
3268 ERROR("%p: CQ creation failure: %s",
3269 (void *)dev, strerror(ret));
3272 DEBUG("priv->device_attr.max_qp_wr is %d",
3273 priv->device_attr.max_qp_wr);
3274 DEBUG("priv->device_attr.max_sge is %d",
3275 priv->device_attr.max_sge);
3278 tmpl.qp = rxq_setup_qp_rss(priv, tmpl.cq, desc, parent);
3280 #endif /* RSS_SUPPORT */
3281 tmpl.qp = rxq_setup_qp(priv, tmpl.cq, desc);
3282 if (tmpl.qp == NULL) {
3283 ret = (errno ? errno : EINVAL);
3284 ERROR("%p: QP creation failure: %s",
3285 (void *)dev, strerror(ret));
3288 mod = (struct ibv_exp_qp_attr){
3289 /* Move the QP to this state. */
3290 .qp_state = IBV_QPS_INIT,
3291 /* Primary port number. */
3292 .port_num = priv->port
3294 ret = ibv_exp_modify_qp(tmpl.qp, &mod,
3297 (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
3298 #endif /* RSS_SUPPORT */
3301 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
3302 (void *)dev, strerror(ret));
3305 if ((parent) || (!priv->rss)) {
3306 /* Configure MAC and broadcast addresses. */
3307 ret = rxq_mac_addrs_add(&tmpl);
3309 ERROR("%p: QP flow attachment failed: %s",
3310 (void *)dev, strerror(ret));
3314 /* Allocate descriptors for RX queues, except for the RSS parent. */
3318 ret = rxq_alloc_elts_sp(&tmpl, desc, NULL);
3320 ret = rxq_alloc_elts(&tmpl, desc, NULL);
3322 ERROR("%p: RXQ allocation failed: %s",
3323 (void *)dev, strerror(ret));
3326 ret = ibv_post_recv(tmpl.qp,
3328 &(*tmpl.elts.sp)[0].wr :
3329 &(*tmpl.elts.no_sp)[0].wr),
3332 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
3339 mod = (struct ibv_exp_qp_attr){
3340 .qp_state = IBV_QPS_RTR
3342 ret = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3344 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
3345 (void *)dev, strerror(ret));
3349 tmpl.port_id = dev->data->port_id;
3350 DEBUG("%p: RTE port ID: %u", (void *)rxq, tmpl.port_id);
3351 attr.params = (struct ibv_exp_query_intf_params){
3352 .intf_scope = IBV_EXP_INTF_GLOBAL,
3353 .intf = IBV_EXP_INTF_CQ,
3356 tmpl.if_cq = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
3357 if (tmpl.if_cq == NULL) {
3358 ERROR("%p: CQ interface family query failed with status %d",
3359 (void *)dev, status);
3362 attr.params = (struct ibv_exp_query_intf_params){
3363 .intf_scope = IBV_EXP_INTF_GLOBAL,
3364 .intf = IBV_EXP_INTF_QP_BURST,
3367 tmpl.if_qp = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
3368 if (tmpl.if_qp == NULL) {
3369 ERROR("%p: QP interface family query failed with status %d",
3370 (void *)dev, status);
3373 /* Clean up rxq in case we're reinitializing it. */
3374 DEBUG("%p: cleaning-up old rxq just in case", (void *)rxq);
3377 DEBUG("%p: rxq updated with %p", (void *)rxq, (void *)&tmpl);
3387 * DPDK callback to configure a RX queue.
3390 * Pointer to Ethernet device structure.
3394 * Number of descriptors to configure in queue.
3396 * NUMA socket on which memory must be allocated.
3398 * Thresholds parameters.
3400 * Memory pool for buffer allocations.
3403 * 0 on success, negative errno value on failure.
3406 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
3407 unsigned int socket, const struct rte_eth_rxconf *conf,
3408 struct rte_mempool *mp)
3410 struct priv *priv = dev->data->dev_private;
3411 struct rxq *rxq = (*priv->rxqs)[idx];
3415 DEBUG("%p: configuring queue %u for %u descriptors",
3416 (void *)dev, idx, desc);
3417 if (idx >= priv->rxqs_n) {
3418 ERROR("%p: queue index out of range (%u >= %u)",
3419 (void *)dev, idx, priv->rxqs_n);
3424 DEBUG("%p: reusing already allocated queue index %u (%p)",
3425 (void *)dev, idx, (void *)rxq);
3426 if (priv->started) {
3430 (*priv->rxqs)[idx] = NULL;
3433 rxq = rte_calloc_socket("RXQ", 1, sizeof(*rxq), 0, socket);
3435 ERROR("%p: unable to allocate queue index %u",
3441 ret = rxq_setup(dev, rxq, desc, socket, conf, mp);
3445 rxq->stats.idx = idx;
3446 DEBUG("%p: adding RX queue %p to list",
3447 (void *)dev, (void *)rxq);
3448 (*priv->rxqs)[idx] = rxq;
3449 /* Update receive callback. */
3451 dev->rx_pkt_burst = mlx4_rx_burst_sp;
3453 dev->rx_pkt_burst = mlx4_rx_burst;
3460 * DPDK callback to release a RX queue.
3463 * Generic RX queue pointer.
3466 mlx4_rx_queue_release(void *dpdk_rxq)
3468 struct rxq *rxq = (struct rxq *)dpdk_rxq;
3476 assert(rxq != &priv->rxq_parent);
3477 for (i = 0; (i != priv->rxqs_n); ++i)
3478 if ((*priv->rxqs)[i] == rxq) {
3479 DEBUG("%p: removing RX queue %p from list",
3480 (void *)priv->dev, (void *)rxq);
3481 (*priv->rxqs)[i] = NULL;
3490 * DPDK callback to start the device.
3492 * Simulate device start by attaching all configured flows.
3495 * Pointer to Ethernet device structure.
3498 * 0 on success, negative errno value on failure.
3501 mlx4_dev_start(struct rte_eth_dev *dev)
3503 struct priv *priv = dev->data->dev_private;
3509 if (priv->started) {
3513 DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
3516 rxq = &priv->rxq_parent;
3519 rxq = (*priv->rxqs)[0];
3522 /* Iterate only once when RSS is enabled. */
3526 /* Ignore nonexistent RX queues. */
3529 ret = rxq_mac_addrs_add(rxq);
3530 if (!ret && priv->promisc)
3531 ret = rxq_promiscuous_enable(rxq);
3532 if (!ret && priv->allmulti)
3533 ret = rxq_allmulticast_enable(rxq);
3536 WARN("%p: QP flow attachment failed: %s",
3537 (void *)dev, strerror(ret));
3540 rxq = (*priv->rxqs)[--i];
3542 rxq_allmulticast_disable(rxq);
3543 rxq_promiscuous_disable(rxq);
3544 rxq_mac_addrs_del(rxq);
3549 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
3555 * DPDK callback to stop the device.
3557 * Simulate device stop by detaching all configured flows.
3560 * Pointer to Ethernet device structure.
3563 mlx4_dev_stop(struct rte_eth_dev *dev)
3565 struct priv *priv = dev->data->dev_private;
3571 if (!priv->started) {
3575 DEBUG("%p: detaching flows from all RX queues", (void *)dev);
3578 rxq = &priv->rxq_parent;
3581 rxq = (*priv->rxqs)[0];
3584 /* Iterate only once when RSS is enabled. */
3586 /* Ignore nonexistent RX queues. */
3589 rxq_allmulticast_disable(rxq);
3590 rxq_promiscuous_disable(rxq);
3591 rxq_mac_addrs_del(rxq);
3592 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
3597 * Dummy DPDK callback for TX.
3599 * This function is used to temporarily replace the real callback during
3600 * unsafe control operations on the queue, or in case of error.
3603 * Generic pointer to TX queue structure.
3605 * Packets to transmit.
3607 * Number of packets in array.
3610 * Number of packets successfully transmitted (<= pkts_n).
3613 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
3622 * Dummy DPDK callback for RX.
3624 * This function is used to temporarily replace the real callback during
3625 * unsafe control operations on the queue, or in case of error.
3628 * Generic pointer to RX queue structure.
3630 * Array to store received packets.
3632 * Maximum number of packets in array.
3635 * Number of packets successfully received (<= pkts_n).
3638 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
3647 * DPDK callback to close the device.
3649 * Destroy all queues and objects, free memory.
3652 * Pointer to Ethernet device structure.
3655 mlx4_dev_close(struct rte_eth_dev *dev)
3657 struct priv *priv = dev->data->dev_private;
3662 DEBUG("%p: closing device \"%s\"",
3664 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
3665 /* Prevent crashes when queues are still in use. This is unfortunately
3666 * still required for DPDK 1.3 because some programs (such as testpmd)
3667 * never release them before closing the device. */
3668 dev->rx_pkt_burst = removed_rx_burst;
3669 dev->tx_pkt_burst = removed_tx_burst;
3670 if (priv->rxqs != NULL) {
3671 /* XXX race condition if mlx4_rx_burst() is still running. */
3673 for (i = 0; (i != priv->rxqs_n); ++i) {
3674 tmp = (*priv->rxqs)[i];
3677 (*priv->rxqs)[i] = NULL;
3684 if (priv->txqs != NULL) {
3685 /* XXX race condition if mlx4_tx_burst() is still running. */
3687 for (i = 0; (i != priv->txqs_n); ++i) {
3688 tmp = (*priv->txqs)[i];
3691 (*priv->txqs)[i] = NULL;
3699 rxq_cleanup(&priv->rxq_parent);
3700 if (priv->pd != NULL) {
3701 assert(priv->ctx != NULL);
3702 claim_zero(ibv_dealloc_pd(priv->pd));
3703 claim_zero(ibv_close_device(priv->ctx));
3705 assert(priv->ctx == NULL);
3707 memset(priv, 0, sizeof(*priv));
3711 * DPDK callback to get information about the device.
3714 * Pointer to Ethernet device structure.
3716 * Info structure output buffer.
3719 mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
3721 struct priv *priv = dev->data->dev_private;
3725 /* FIXME: we should ask the device for these values. */
3726 info->min_rx_bufsize = 32;
3727 info->max_rx_pktlen = 65536;
3729 * Since we need one CQ per QP, the limit is the minimum number
3730 * between the two values.
3732 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
3733 priv->device_attr.max_qp : priv->device_attr.max_cq);
3734 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
3737 info->max_rx_queues = max;
3738 info->max_tx_queues = max;
3739 info->max_mac_addrs = elemof(priv->mac);
3740 info->rx_offload_capa =
3742 (DEV_RX_OFFLOAD_IPV4_CKSUM |
3743 DEV_RX_OFFLOAD_UDP_CKSUM |
3744 DEV_RX_OFFLOAD_TCP_CKSUM) :
3746 info->tx_offload_capa =
3748 (DEV_TX_OFFLOAD_IPV4_CKSUM |
3749 DEV_TX_OFFLOAD_UDP_CKSUM |
3750 DEV_TX_OFFLOAD_TCP_CKSUM) :
3756 * DPDK callback to get device statistics.
3759 * Pointer to Ethernet device structure.
3761 * Stats structure output buffer.
3764 mlx4_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
3766 struct priv *priv = dev->data->dev_private;
3767 struct rte_eth_stats tmp = {0};
3772 /* Add software counters. */
3773 for (i = 0; (i != priv->rxqs_n); ++i) {
3774 struct rxq *rxq = (*priv->rxqs)[i];
3778 idx = rxq->stats.idx;
3779 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
3780 #ifdef MLX4_PMD_SOFT_COUNTERS
3781 tmp.q_ipackets[idx] += rxq->stats.ipackets;
3782 tmp.q_ibytes[idx] += rxq->stats.ibytes;
3784 tmp.q_errors[idx] += (rxq->stats.idropped +
3785 rxq->stats.rx_nombuf);
3787 #ifdef MLX4_PMD_SOFT_COUNTERS
3788 tmp.ipackets += rxq->stats.ipackets;
3789 tmp.ibytes += rxq->stats.ibytes;
3791 tmp.ierrors += rxq->stats.idropped;
3792 tmp.rx_nombuf += rxq->stats.rx_nombuf;
3794 for (i = 0; (i != priv->txqs_n); ++i) {
3795 struct txq *txq = (*priv->txqs)[i];
3799 idx = txq->stats.idx;
3800 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
3801 #ifdef MLX4_PMD_SOFT_COUNTERS
3802 tmp.q_opackets[idx] += txq->stats.opackets;
3803 tmp.q_obytes[idx] += txq->stats.obytes;
3805 tmp.q_errors[idx] += txq->stats.odropped;
3807 #ifdef MLX4_PMD_SOFT_COUNTERS
3808 tmp.opackets += txq->stats.opackets;
3809 tmp.obytes += txq->stats.obytes;
3811 tmp.oerrors += txq->stats.odropped;
3813 #ifndef MLX4_PMD_SOFT_COUNTERS
3814 /* FIXME: retrieve and add hardware counters. */
3821 * DPDK callback to clear device statistics.
3824 * Pointer to Ethernet device structure.
3827 mlx4_stats_reset(struct rte_eth_dev *dev)
3829 struct priv *priv = dev->data->dev_private;
3834 for (i = 0; (i != priv->rxqs_n); ++i) {
3835 if ((*priv->rxqs)[i] == NULL)
3837 idx = (*priv->rxqs)[i]->stats.idx;
3838 (*priv->rxqs)[i]->stats =
3839 (struct mlx4_rxq_stats){ .idx = idx };
3841 for (i = 0; (i != priv->txqs_n); ++i) {
3842 if ((*priv->txqs)[i] == NULL)
3844 idx = (*priv->rxqs)[i]->stats.idx;
3845 (*priv->txqs)[i]->stats =
3846 (struct mlx4_txq_stats){ .idx = idx };
3848 #ifndef MLX4_PMD_SOFT_COUNTERS
3849 /* FIXME: reset hardware counters. */
3855 * DPDK callback to remove a MAC address.
3858 * Pointer to Ethernet device structure.
3860 * MAC address index.
3863 mlx4_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
3865 struct priv *priv = dev->data->dev_private;
3868 DEBUG("%p: removing MAC address from index %" PRIu32,
3869 (void *)dev, index);
3870 if (index >= MLX4_MAX_MAC_ADDRESSES)
3872 /* Refuse to remove the broadcast address, this one is special. */
3873 if (!memcmp(priv->mac[index].addr_bytes, "\xff\xff\xff\xff\xff\xff",
3876 priv_mac_addr_del(priv, index);
3882 * DPDK callback to add a MAC address.
3885 * Pointer to Ethernet device structure.
3887 * MAC address to register.
3889 * MAC address index.
3891 * VMDq pool index to associate address with (ignored).
3894 mlx4_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
3895 uint32_t index, uint32_t vmdq)
3897 struct priv *priv = dev->data->dev_private;
3901 DEBUG("%p: adding MAC address at index %" PRIu32,
3902 (void *)dev, index);
3903 if (index >= MLX4_MAX_MAC_ADDRESSES)
3905 /* Refuse to add the broadcast address, this one is special. */
3906 if (!memcmp(mac_addr->addr_bytes, "\xff\xff\xff\xff\xff\xff",
3909 priv_mac_addr_add(priv, index,
3910 (const uint8_t (*)[ETHER_ADDR_LEN])
3911 mac_addr->addr_bytes);
3917 * DPDK callback to enable promiscuous mode.
3920 * Pointer to Ethernet device structure.
3923 mlx4_promiscuous_enable(struct rte_eth_dev *dev)
3925 struct priv *priv = dev->data->dev_private;
3930 if (priv->promisc) {
3934 /* If device isn't started, this is all we need to do. */
3938 ret = rxq_promiscuous_enable(&priv->rxq_parent);
3945 for (i = 0; (i != priv->rxqs_n); ++i) {
3946 if ((*priv->rxqs)[i] == NULL)
3948 ret = rxq_promiscuous_enable((*priv->rxqs)[i]);
3951 /* Failure, rollback. */
3953 if ((*priv->rxqs)[--i] != NULL)
3954 rxq_promiscuous_disable((*priv->rxqs)[i]);
3964 * DPDK callback to disable promiscuous mode.
3967 * Pointer to Ethernet device structure.
3970 mlx4_promiscuous_disable(struct rte_eth_dev *dev)
3972 struct priv *priv = dev->data->dev_private;
3976 if (!priv->promisc) {
3981 rxq_promiscuous_disable(&priv->rxq_parent);
3984 for (i = 0; (i != priv->rxqs_n); ++i)
3985 if ((*priv->rxqs)[i] != NULL)
3986 rxq_promiscuous_disable((*priv->rxqs)[i]);
3993 * DPDK callback to enable allmulti mode.
3996 * Pointer to Ethernet device structure.
3999 mlx4_allmulticast_enable(struct rte_eth_dev *dev)
4001 struct priv *priv = dev->data->dev_private;
4006 if (priv->allmulti) {
4010 /* If device isn't started, this is all we need to do. */
4014 ret = rxq_allmulticast_enable(&priv->rxq_parent);
4021 for (i = 0; (i != priv->rxqs_n); ++i) {
4022 if ((*priv->rxqs)[i] == NULL)
4024 ret = rxq_allmulticast_enable((*priv->rxqs)[i]);
4027 /* Failure, rollback. */
4029 if ((*priv->rxqs)[--i] != NULL)
4030 rxq_allmulticast_disable((*priv->rxqs)[i]);
4040 * DPDK callback to disable allmulti mode.
4043 * Pointer to Ethernet device structure.
4046 mlx4_allmulticast_disable(struct rte_eth_dev *dev)
4048 struct priv *priv = dev->data->dev_private;
4052 if (!priv->allmulti) {
4057 rxq_allmulticast_disable(&priv->rxq_parent);
4060 for (i = 0; (i != priv->rxqs_n); ++i)
4061 if ((*priv->rxqs)[i] != NULL)
4062 rxq_allmulticast_disable((*priv->rxqs)[i]);
4069 * DPDK callback to retrieve physical link information (unlocked version).
4072 * Pointer to Ethernet device structure.
4073 * @param wait_to_complete
4074 * Wait for request completion (ignored).
4077 mlx4_link_update_unlocked(struct rte_eth_dev *dev, int wait_to_complete)
4079 struct priv *priv = dev->data->dev_private;
4080 struct ethtool_cmd edata = {
4084 struct rte_eth_link dev_link;
4087 (void)wait_to_complete;
4088 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
4089 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
4092 memset(&dev_link, 0, sizeof(dev_link));
4093 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
4094 (ifr.ifr_flags & IFF_RUNNING));
4095 ifr.ifr_data = &edata;
4096 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4097 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
4101 link_speed = ethtool_cmd_speed(&edata);
4102 if (link_speed == -1)
4103 dev_link.link_speed = 0;
4105 dev_link.link_speed = link_speed;
4106 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
4107 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
4108 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
4109 /* Link status changed. */
4110 dev->data->dev_link = dev_link;
4113 /* Link status is still the same. */
4118 * DPDK callback to retrieve physical link information.
4121 * Pointer to Ethernet device structure.
4122 * @param wait_to_complete
4123 * Wait for request completion (ignored).
4126 mlx4_link_update(struct rte_eth_dev *dev, int wait_to_complete)
4128 struct priv *priv = dev->data->dev_private;
4132 ret = mlx4_link_update_unlocked(dev, wait_to_complete);
4138 * DPDK callback to change the MTU.
4140 * Setting the MTU affects hardware MRU (packets larger than the MTU cannot be
4141 * received). Use this as a hint to enable/disable scattered packets support
4142 * and improve performance when not needed.
4143 * Since failure is not an option, reconfiguring queues on the fly is not
4147 * Pointer to Ethernet device structure.
4152 * 0 on success, negative errno value on failure.
4155 mlx4_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
4157 struct priv *priv = dev->data->dev_private;
4160 uint16_t (*rx_func)(void *, struct rte_mbuf **, uint16_t) =
4164 /* Set kernel interface MTU first. */
4165 if (priv_set_mtu(priv, mtu)) {
4167 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
4171 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
4173 /* Temporarily replace RX handler with a fake one, assuming it has not
4174 * been copied elsewhere. */
4175 dev->rx_pkt_burst = removed_rx_burst;
4176 /* Make sure everyone has left mlx4_rx_burst() and uses
4177 * removed_rx_burst() instead. */
4180 /* Reconfigure each RX queue. */
4181 for (i = 0; (i != priv->rxqs_n); ++i) {
4182 struct rxq *rxq = (*priv->rxqs)[i];
4183 unsigned int max_frame_len;
4188 /* Calculate new maximum frame length according to MTU and
4189 * toggle scattered support (sp) if necessary. */
4190 max_frame_len = (priv->mtu + ETHER_HDR_LEN +
4191 (ETHER_MAX_VLAN_FRAME_LEN - ETHER_MAX_LEN));
4192 sp = (max_frame_len > (rxq->mb_len - RTE_PKTMBUF_HEADROOM));
4193 /* Provide new values to rxq_setup(). */
4194 dev->data->dev_conf.rxmode.jumbo_frame = sp;
4195 dev->data->dev_conf.rxmode.max_rx_pkt_len = max_frame_len;
4196 ret = rxq_rehash(dev, rxq);
4198 /* Force SP RX if that queue requires it and abort. */
4200 rx_func = mlx4_rx_burst_sp;
4203 /* Reenable non-RSS queue attributes. No need to check
4204 * for errors at this stage. */
4206 rxq_mac_addrs_add(rxq);
4208 rxq_promiscuous_enable(rxq);
4210 rxq_allmulticast_enable(rxq);
4212 /* Scattered burst function takes priority. */
4214 rx_func = mlx4_rx_burst_sp;
4216 /* Burst functions can now be called again. */
4218 dev->rx_pkt_burst = rx_func;
4226 * DPDK callback to get flow control status.
4229 * Pointer to Ethernet device structure.
4230 * @param[out] fc_conf
4231 * Flow control output buffer.
4234 * 0 on success, negative errno value on failure.
4237 mlx4_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4239 struct priv *priv = dev->data->dev_private;
4241 struct ethtool_pauseparam ethpause = {
4242 .cmd = ETHTOOL_GPAUSEPARAM
4246 ifr.ifr_data = ðpause;
4248 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4250 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
4256 fc_conf->autoneg = ethpause.autoneg;
4257 if (ethpause.rx_pause && ethpause.tx_pause)
4258 fc_conf->mode = RTE_FC_FULL;
4259 else if (ethpause.rx_pause)
4260 fc_conf->mode = RTE_FC_RX_PAUSE;
4261 else if (ethpause.tx_pause)
4262 fc_conf->mode = RTE_FC_TX_PAUSE;
4264 fc_conf->mode = RTE_FC_NONE;
4274 * DPDK callback to modify flow control parameters.
4277 * Pointer to Ethernet device structure.
4278 * @param[in] fc_conf
4279 * Flow control parameters.
4282 * 0 on success, negative errno value on failure.
4285 mlx4_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4287 struct priv *priv = dev->data->dev_private;
4289 struct ethtool_pauseparam ethpause = {
4290 .cmd = ETHTOOL_SPAUSEPARAM
4294 ifr.ifr_data = ðpause;
4295 ethpause.autoneg = fc_conf->autoneg;
4296 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4297 (fc_conf->mode & RTE_FC_RX_PAUSE))
4298 ethpause.rx_pause = 1;
4300 ethpause.rx_pause = 0;
4302 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4303 (fc_conf->mode & RTE_FC_TX_PAUSE))
4304 ethpause.tx_pause = 1;
4306 ethpause.tx_pause = 0;
4309 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4311 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
4325 * Configure a VLAN filter.
4328 * Pointer to Ethernet device structure.
4330 * VLAN ID to filter.
4335 * 0 on success, errno value on failure.
4338 vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
4340 struct priv *priv = dev->data->dev_private;
4342 unsigned int j = -1;
4344 DEBUG("%p: %s VLAN filter ID %" PRIu16,
4345 (void *)dev, (on ? "enable" : "disable"), vlan_id);
4346 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
4347 if (!priv->vlan_filter[i].enabled) {
4348 /* Unused index, remember it. */
4352 if (priv->vlan_filter[i].id != vlan_id)
4354 /* This VLAN ID is already known, use its index. */
4358 /* Check if there's room for another VLAN filter. */
4359 if (j == (unsigned int)-1)
4362 * VLAN filters apply to all configured MAC addresses, flow
4363 * specifications must be reconfigured accordingly.
4365 priv->vlan_filter[j].id = vlan_id;
4366 if ((on) && (!priv->vlan_filter[j].enabled)) {
4368 * Filter is disabled, enable it.
4369 * Rehashing flows in all RX queues is necessary.
4372 rxq_mac_addrs_del(&priv->rxq_parent);
4374 for (i = 0; (i != priv->rxqs_n); ++i)
4375 if ((*priv->rxqs)[i] != NULL)
4376 rxq_mac_addrs_del((*priv->rxqs)[i]);
4377 priv->vlan_filter[j].enabled = 1;
4378 if (priv->started) {
4380 rxq_mac_addrs_add(&priv->rxq_parent);
4382 for (i = 0; (i != priv->rxqs_n); ++i) {
4383 if ((*priv->rxqs)[i] == NULL)
4385 rxq_mac_addrs_add((*priv->rxqs)[i]);
4388 } else if ((!on) && (priv->vlan_filter[j].enabled)) {
4390 * Filter is enabled, disable it.
4391 * Rehashing flows in all RX queues is necessary.
4394 rxq_mac_addrs_del(&priv->rxq_parent);
4396 for (i = 0; (i != priv->rxqs_n); ++i)
4397 if ((*priv->rxqs)[i] != NULL)
4398 rxq_mac_addrs_del((*priv->rxqs)[i]);
4399 priv->vlan_filter[j].enabled = 0;
4400 if (priv->started) {
4402 rxq_mac_addrs_add(&priv->rxq_parent);
4404 for (i = 0; (i != priv->rxqs_n); ++i) {
4405 if ((*priv->rxqs)[i] == NULL)
4407 rxq_mac_addrs_add((*priv->rxqs)[i]);
4415 * DPDK callback to configure a VLAN filter.
4418 * Pointer to Ethernet device structure.
4420 * VLAN ID to filter.
4425 * 0 on success, negative errno value on failure.
4428 mlx4_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
4430 struct priv *priv = dev->data->dev_private;
4434 ret = vlan_filter_set(dev, vlan_id, on);
4440 static const struct eth_dev_ops mlx4_dev_ops = {
4441 .dev_configure = mlx4_dev_configure,
4442 .dev_start = mlx4_dev_start,
4443 .dev_stop = mlx4_dev_stop,
4444 .dev_close = mlx4_dev_close,
4445 .promiscuous_enable = mlx4_promiscuous_enable,
4446 .promiscuous_disable = mlx4_promiscuous_disable,
4447 .allmulticast_enable = mlx4_allmulticast_enable,
4448 .allmulticast_disable = mlx4_allmulticast_disable,
4449 .link_update = mlx4_link_update,
4450 .stats_get = mlx4_stats_get,
4451 .stats_reset = mlx4_stats_reset,
4452 .queue_stats_mapping_set = NULL,
4453 .dev_infos_get = mlx4_dev_infos_get,
4454 .vlan_filter_set = mlx4_vlan_filter_set,
4455 .vlan_tpid_set = NULL,
4456 .vlan_strip_queue_set = NULL,
4457 .vlan_offload_set = NULL,
4458 .rx_queue_setup = mlx4_rx_queue_setup,
4459 .tx_queue_setup = mlx4_tx_queue_setup,
4460 .rx_queue_release = mlx4_rx_queue_release,
4461 .tx_queue_release = mlx4_tx_queue_release,
4463 .dev_led_off = NULL,
4464 .flow_ctrl_get = mlx4_dev_get_flow_ctrl,
4465 .flow_ctrl_set = mlx4_dev_set_flow_ctrl,
4466 .priority_flow_ctrl_set = NULL,
4467 .mac_addr_remove = mlx4_mac_addr_remove,
4468 .mac_addr_add = mlx4_mac_addr_add,
4469 .mtu_set = mlx4_dev_set_mtu,
4470 .udp_tunnel_add = NULL,
4471 .udp_tunnel_del = NULL,
4472 .fdir_add_signature_filter = NULL,
4473 .fdir_update_signature_filter = NULL,
4474 .fdir_remove_signature_filter = NULL,
4475 .fdir_add_perfect_filter = NULL,
4476 .fdir_update_perfect_filter = NULL,
4477 .fdir_remove_perfect_filter = NULL,
4478 .fdir_set_masks = NULL
4482 * Get PCI information from struct ibv_device.
4485 * Pointer to Ethernet device structure.
4486 * @param[out] pci_addr
4487 * PCI bus address output buffer.
4490 * 0 on success, -1 on failure and errno is set.
4493 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
4494 struct rte_pci_addr *pci_addr)
4498 MKSTR(path, "%s/device/uevent", device->ibdev_path);
4500 file = fopen(path, "rb");
4503 while (fgets(line, sizeof(line), file) == line) {
4504 size_t len = strlen(line);
4507 /* Truncate long lines. */
4508 if (len == (sizeof(line) - 1))
4509 while (line[(len - 1)] != '\n') {
4513 line[(len - 1)] = ret;
4515 /* Extract information. */
4518 "%" SCNx16 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
4522 &pci_addr->function) == 4) {
4532 * Get MAC address by querying netdevice.
4535 * struct priv for the requested device.
4537 * MAC address output buffer.
4540 * 0 on success, -1 on failure and errno is set.
4543 priv_get_mac(struct priv *priv, uint8_t (*mac)[ETHER_ADDR_LEN])
4545 struct ifreq request;
4547 if (priv_ifreq(priv, SIOCGIFHWADDR, &request))
4549 memcpy(mac, request.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
4553 /* Support up to 32 adapters. */
4555 struct rte_pci_addr pci_addr; /* associated PCI address */
4556 uint32_t ports; /* physical ports bitfield. */
4560 * Get device index in mlx4_dev[] from PCI bus address.
4562 * @param[in] pci_addr
4563 * PCI bus address to look for.
4566 * mlx4_dev[] index on success, -1 on failure.
4569 mlx4_dev_idx(struct rte_pci_addr *pci_addr)
4574 assert(pci_addr != NULL);
4575 for (i = 0; (i != elemof(mlx4_dev)); ++i) {
4576 if ((mlx4_dev[i].pci_addr.domain == pci_addr->domain) &&
4577 (mlx4_dev[i].pci_addr.bus == pci_addr->bus) &&
4578 (mlx4_dev[i].pci_addr.devid == pci_addr->devid) &&
4579 (mlx4_dev[i].pci_addr.function == pci_addr->function))
4581 if ((mlx4_dev[i].ports == 0) && (ret == -1))
4588 * Retrieve integer value from environment variable.
4591 * Environment variable name.
4594 * Integer value, 0 if the variable is not set.
4597 mlx4_getenv_int(const char *name)
4599 const char *val = getenv(name);
4606 static struct eth_driver mlx4_driver;
4609 * DPDK callback to register a PCI device.
4611 * This function creates an Ethernet device for each port of a given
4614 * @param[in] pci_drv
4615 * PCI driver structure (mlx4_driver).
4616 * @param[in] pci_dev
4617 * PCI device information.
4620 * 0 on success, negative errno value on failure.
4623 mlx4_pci_devinit(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
4625 struct ibv_device **list;
4626 struct ibv_device *ibv_dev;
4628 struct ibv_context *attr_ctx = NULL;
4629 struct ibv_device_attr device_attr;
4635 assert(pci_drv == &mlx4_driver.pci_drv);
4636 /* Get mlx4_dev[] index. */
4637 idx = mlx4_dev_idx(&pci_dev->addr);
4639 ERROR("this driver cannot support any more adapters");
4642 DEBUG("using driver device index %d", idx);
4644 /* Save PCI address. */
4645 mlx4_dev[idx].pci_addr = pci_dev->addr;
4646 list = ibv_get_device_list(&i);
4649 if (errno == ENOSYS) {
4650 WARN("cannot list devices, is ib_uverbs loaded?");
4657 * For each listed device, check related sysfs entry against
4658 * the provided PCI ID.
4661 struct rte_pci_addr pci_addr;
4664 DEBUG("checking device \"%s\"", list[i]->name);
4665 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
4667 if ((pci_dev->addr.domain != pci_addr.domain) ||
4668 (pci_dev->addr.bus != pci_addr.bus) ||
4669 (pci_dev->addr.devid != pci_addr.devid) ||
4670 (pci_dev->addr.function != pci_addr.function))
4672 vf = (pci_dev->id.device_id ==
4673 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
4674 INFO("PCI information matches, using device \"%s\" (VF: %s)",
4675 list[i]->name, (vf ? "true" : "false"));
4676 attr_ctx = ibv_open_device(list[i]);
4680 if (attr_ctx == NULL) {
4681 ibv_free_device_list(list);
4684 WARN("cannot access device, is mlx4_ib loaded?");
4687 WARN("cannot use device, are drivers up to date?");
4695 DEBUG("device opened");
4696 if (ibv_query_device(attr_ctx, &device_attr))
4698 INFO("%u port(s) detected", device_attr.phys_port_cnt);
4700 for (i = 0; i < device_attr.phys_port_cnt; i++) {
4701 uint32_t port = i + 1; /* ports are indexed from one */
4702 uint32_t test = (1 << i);
4703 struct ibv_context *ctx = NULL;
4704 struct ibv_port_attr port_attr;
4705 struct ibv_pd *pd = NULL;
4706 struct priv *priv = NULL;
4707 struct rte_eth_dev *eth_dev;
4708 #ifdef HAVE_EXP_QUERY_DEVICE
4709 struct ibv_exp_device_attr exp_device_attr;
4710 #endif /* HAVE_EXP_QUERY_DEVICE */
4711 struct ether_addr mac;
4713 #ifdef HAVE_EXP_QUERY_DEVICE
4714 exp_device_attr.comp_mask = IBV_EXP_DEVICE_ATTR_EXP_CAP_FLAGS;
4716 exp_device_attr.comp_mask |= IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ;
4717 #endif /* RSS_SUPPORT */
4718 #endif /* HAVE_EXP_QUERY_DEVICE */
4720 DEBUG("using port %u (%08" PRIx32 ")", port, test);
4722 ctx = ibv_open_device(ibv_dev);
4726 /* Check port status. */
4727 err = ibv_query_port(ctx, port, &port_attr);
4729 ERROR("port query failed: %s", strerror(err));
4732 if (port_attr.state != IBV_PORT_ACTIVE)
4733 WARN("bad state for port %d: \"%s\" (%d)",
4734 port, ibv_port_state_str(port_attr.state),
4737 /* Allocate protection domain. */
4738 pd = ibv_alloc_pd(ctx);
4740 ERROR("PD allocation failure");
4745 mlx4_dev[idx].ports |= test;
4747 /* from rte_ethdev.c */
4748 priv = rte_zmalloc("ethdev private structure",
4750 RTE_CACHE_LINE_SIZE);
4752 ERROR("priv allocation failure");
4758 priv->device_attr = device_attr;
4761 priv->mtu = ETHER_MTU;
4762 #ifdef HAVE_EXP_QUERY_DEVICE
4763 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
4764 ERROR("ibv_exp_query_device() failed");
4768 if ((exp_device_attr.exp_device_cap_flags &
4769 IBV_EXP_DEVICE_QPG) &&
4770 (exp_device_attr.exp_device_cap_flags &
4771 IBV_EXP_DEVICE_UD_RSS) &&
4772 (exp_device_attr.comp_mask &
4773 IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ) &&
4774 (exp_device_attr.max_rss_tbl_sz > 0)) {
4777 priv->max_rss_tbl_sz = exp_device_attr.max_rss_tbl_sz;
4781 priv->max_rss_tbl_sz = 0;
4783 priv->hw_tss = !!(exp_device_attr.exp_device_cap_flags &
4784 IBV_EXP_DEVICE_UD_TSS);
4785 DEBUG("device flags: %s%s%s",
4786 (priv->hw_qpg ? "IBV_DEVICE_QPG " : ""),
4787 (priv->hw_tss ? "IBV_DEVICE_TSS " : ""),
4788 (priv->hw_rss ? "IBV_DEVICE_RSS " : ""));
4790 DEBUG("maximum RSS indirection table size: %u",
4791 exp_device_attr.max_rss_tbl_sz);
4792 #endif /* RSS_SUPPORT */
4795 ((exp_device_attr.exp_device_cap_flags &
4796 IBV_EXP_DEVICE_RX_CSUM_TCP_UDP_PKT) &&
4797 (exp_device_attr.exp_device_cap_flags &
4798 IBV_EXP_DEVICE_RX_CSUM_IP_PKT));
4799 DEBUG("checksum offloading is %ssupported",
4800 (priv->hw_csum ? "" : "not "));
4802 priv->hw_csum_l2tun = !!(exp_device_attr.exp_device_cap_flags &
4803 IBV_EXP_DEVICE_VXLAN_SUPPORT);
4804 DEBUG("L2 tunnel checksum offloads are %ssupported",
4805 (priv->hw_csum_l2tun ? "" : "not "));
4808 priv->inl_recv_size = mlx4_getenv_int("MLX4_INLINE_RECV_SIZE");
4810 if (priv->inl_recv_size) {
4811 exp_device_attr.comp_mask =
4812 IBV_EXP_DEVICE_ATTR_INLINE_RECV_SZ;
4813 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
4814 INFO("Couldn't query device for inline-receive"
4816 priv->inl_recv_size = 0;
4818 if ((unsigned)exp_device_attr.inline_recv_sz <
4819 priv->inl_recv_size) {
4820 INFO("Max inline-receive (%d) <"
4821 " requested inline-receive (%u)",
4822 exp_device_attr.inline_recv_sz,
4823 priv->inl_recv_size);
4824 priv->inl_recv_size =
4825 exp_device_attr.inline_recv_sz;
4828 INFO("Set inline receive size to %u",
4829 priv->inl_recv_size);
4831 #endif /* INLINE_RECV */
4832 #endif /* HAVE_EXP_QUERY_DEVICE */
4834 (void)mlx4_getenv_int;
4836 /* Configure the first MAC address by default. */
4837 if (priv_get_mac(priv, &mac.addr_bytes)) {
4838 ERROR("cannot get MAC address, is mlx4_en loaded?"
4839 " (errno: %s)", strerror(errno));
4842 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
4844 mac.addr_bytes[0], mac.addr_bytes[1],
4845 mac.addr_bytes[2], mac.addr_bytes[3],
4846 mac.addr_bytes[4], mac.addr_bytes[5]);
4847 /* Register MAC and broadcast addresses. */
4848 claim_zero(priv_mac_addr_add(priv, 0,
4849 (const uint8_t (*)[ETHER_ADDR_LEN])
4851 claim_zero(priv_mac_addr_add(priv, 1,
4852 &(const uint8_t [ETHER_ADDR_LEN])
4853 { "\xff\xff\xff\xff\xff\xff" }));
4856 char ifname[IF_NAMESIZE];
4858 if (priv_get_ifname(priv, &ifname) == 0)
4859 DEBUG("port %u ifname is \"%s\"",
4860 priv->port, ifname);
4862 DEBUG("port %u ifname is unknown", priv->port);
4865 /* Get actual MTU if possible. */
4866 priv_get_mtu(priv, &priv->mtu);
4867 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
4869 /* from rte_ethdev.c */
4871 char name[RTE_ETH_NAME_MAX_LEN];
4873 snprintf(name, sizeof(name), "%s port %u",
4874 ibv_get_device_name(ibv_dev), port);
4875 eth_dev = rte_eth_dev_allocate(name, RTE_ETH_DEV_PCI);
4877 if (eth_dev == NULL) {
4878 ERROR("can not allocate rte ethdev");
4883 eth_dev->data->dev_private = priv;
4884 eth_dev->pci_dev = pci_dev;
4885 eth_dev->driver = &mlx4_driver;
4886 eth_dev->data->rx_mbuf_alloc_failed = 0;
4887 eth_dev->data->mtu = ETHER_MTU;
4889 priv->dev = eth_dev;
4890 eth_dev->dev_ops = &mlx4_dev_ops;
4891 eth_dev->data->mac_addrs = priv->mac;
4893 /* Bring Ethernet device up. */
4894 DEBUG("forcing Ethernet interface up");
4895 priv_set_flags(priv, ~IFF_UP, IFF_UP);
4901 claim_zero(ibv_dealloc_pd(pd));
4903 claim_zero(ibv_close_device(ctx));
4908 * XXX if something went wrong in the loop above, there is a resource
4909 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
4910 * long as the dpdk does not provide a way to deallocate a ethdev and a
4911 * way to enumerate the registered ethdevs to free the previous ones.
4914 /* no port found, complain */
4915 if (!mlx4_dev[idx].ports) {
4922 claim_zero(ibv_close_device(attr_ctx));
4924 ibv_free_device_list(list);
4929 static const struct rte_pci_id mlx4_pci_id_map[] = {
4931 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4932 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3,
4933 .subsystem_vendor_id = PCI_ANY_ID,
4934 .subsystem_device_id = PCI_ANY_ID
4937 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4938 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO,
4939 .subsystem_vendor_id = PCI_ANY_ID,
4940 .subsystem_device_id = PCI_ANY_ID
4943 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4944 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3VF,
4945 .subsystem_vendor_id = PCI_ANY_ID,
4946 .subsystem_device_id = PCI_ANY_ID
4953 static struct eth_driver mlx4_driver = {
4955 .name = MLX4_DRIVER_NAME,
4956 .id_table = mlx4_pci_id_map,
4957 .devinit = mlx4_pci_devinit,
4959 .dev_private_size = sizeof(struct priv)
4963 * Driver initialization routine.
4966 rte_mlx4_pmd_init(const char *name, const char *args)
4971 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
4972 * huge pages. Calling ibv_fork_init() during init allows
4973 * applications to use fork() safely for purposes other than
4974 * using this PMD, which is not supported in forked processes.
4976 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
4978 rte_eal_pci_register(&mlx4_driver.pci_drv);
4982 static struct rte_driver rte_mlx4_driver = {
4984 .name = MLX4_DRIVER_NAME,
4985 .init = rte_mlx4_pmd_init,
4988 PMD_REGISTER_DRIVER(rte_mlx4_driver)