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 /* If raw send operations are available, use them since they are faster. */
143 #ifdef SEND_RAW_WR_SUPPORT
144 typedef struct ibv_send_wr_raw mlx4_send_wr_t;
145 #define mlx4_post_send ibv_post_send_raw
147 typedef struct ibv_send_wr mlx4_send_wr_t;
148 #define mlx4_post_send ibv_post_send
151 struct mlx4_rxq_stats {
152 unsigned int idx; /**< Mapping index. */
153 #ifdef MLX4_PMD_SOFT_COUNTERS
154 uint64_t ipackets; /**< Total of successfully received packets. */
155 uint64_t ibytes; /**< Total of successfully received bytes. */
157 uint64_t idropped; /**< Total of packets dropped when RX ring full. */
158 uint64_t rx_nombuf; /**< Total of RX mbuf allocation failures. */
161 struct mlx4_txq_stats {
162 unsigned int idx; /**< Mapping index. */
163 #ifdef MLX4_PMD_SOFT_COUNTERS
164 uint64_t opackets; /**< Total of successfully sent packets. */
165 uint64_t obytes; /**< Total of successfully sent bytes. */
167 uint64_t odropped; /**< Total of packets not sent when TX ring full. */
170 /* RX element (scattered packets). */
172 struct ibv_recv_wr wr; /* Work Request. */
173 struct ibv_sge sges[MLX4_PMD_SGE_WR_N]; /* Scatter/Gather Elements. */
174 struct rte_mbuf *bufs[MLX4_PMD_SGE_WR_N]; /* SGEs buffers. */
179 struct ibv_recv_wr wr; /* Work Request. */
180 struct ibv_sge sge; /* Scatter/Gather Element. */
181 /* mbuf pointer is derived from WR_ID(wr.wr_id).offset. */
184 /* RX queue descriptor. */
186 struct priv *priv; /* Back pointer to private data. */
187 struct rte_mempool *mp; /* Memory Pool for allocations. */
188 struct ibv_mr *mr; /* Memory Region (for mp). */
189 struct ibv_cq *cq; /* Completion Queue. */
190 struct ibv_qp *qp; /* Queue Pair. */
191 struct ibv_exp_qp_burst_family *if_qp; /* QP burst interface. */
192 struct ibv_exp_cq_family *if_cq; /* CQ interface. */
194 * Each VLAN ID requires a separate flow steering rule.
196 BITFIELD_DECLARE(mac_configured, uint32_t, MLX4_MAX_MAC_ADDRESSES);
197 struct ibv_flow *mac_flow[MLX4_MAX_MAC_ADDRESSES][MLX4_MAX_VLAN_IDS];
198 struct ibv_flow *promisc_flow; /* Promiscuous flow. */
199 struct ibv_flow *allmulti_flow; /* Multicast flow. */
200 unsigned int port_id; /* Port ID for incoming packets. */
201 unsigned int elts_n; /* (*elts)[] length. */
202 unsigned int elts_head; /* Current index in (*elts)[]. */
204 struct rxq_elt_sp (*sp)[]; /* Scattered RX elements. */
205 struct rxq_elt (*no_sp)[]; /* RX elements. */
207 unsigned int sp:1; /* Use scattered RX elements. */
208 uint32_t mb_len; /* Length of a mp-issued mbuf. */
209 struct mlx4_rxq_stats stats; /* RX queue counters. */
210 unsigned int socket; /* CPU socket ID for allocations. */
215 mlx4_send_wr_t wr; /* Work Request. */
216 struct ibv_sge sges[MLX4_PMD_SGE_WR_N]; /* Scatter/Gather Elements. */
217 /* mbuf pointer is derived from WR_ID(wr.wr_id).offset. */
220 /* Linear buffer type. It is used when transmitting buffers with too many
221 * segments that do not fit the hardware queue (see max_send_sge).
222 * Extra segments are copied (linearized) in such buffers, replacing the
223 * last SGE during TX.
224 * The size is arbitrary but large enough to hold a jumbo frame with
225 * 8 segments considering mbuf.buf_len is about 2048 bytes. */
226 typedef uint8_t linear_t[16384];
228 /* TX queue descriptor. */
230 struct priv *priv; /* Back pointer to private data. */
232 struct rte_mempool *mp; /* Cached Memory Pool. */
233 struct ibv_mr *mr; /* Memory Region (for mp). */
234 uint32_t lkey; /* mr->lkey */
235 } mp2mr[MLX4_PMD_TX_MP_CACHE]; /* MP to MR translation table. */
236 struct ibv_cq *cq; /* Completion Queue. */
237 struct ibv_qp *qp; /* Queue Pair. */
238 #if MLX4_PMD_MAX_INLINE > 0
239 uint32_t max_inline; /* Max inline send size <= MLX4_PMD_MAX_INLINE. */
241 unsigned int elts_n; /* (*elts)[] length. */
242 struct txq_elt (*elts)[]; /* TX elements. */
243 unsigned int elts_head; /* Current index in (*elts)[]. */
244 unsigned int elts_tail; /* First element awaiting completion. */
245 unsigned int elts_comp; /* Number of completion requests. */
246 unsigned int elts_comp_cd; /* Countdown for next completion request. */
247 unsigned int elts_comp_cd_init; /* Initial value for countdown. */
248 struct mlx4_txq_stats stats; /* TX queue counters. */
249 linear_t (*elts_linear)[]; /* Linearized buffers. */
250 struct ibv_mr *mr_linear; /* Memory Region for linearized buffers. */
251 unsigned int socket; /* CPU socket ID for allocations. */
255 struct rte_eth_dev *dev; /* Ethernet device. */
256 struct ibv_context *ctx; /* Verbs context. */
257 struct ibv_device_attr device_attr; /* Device properties. */
258 struct ibv_pd *pd; /* Protection Domain. */
260 * MAC addresses array and configuration bit-field.
261 * An extra entry that cannot be modified by the DPDK is reserved
262 * for broadcast frames (destination MAC address ff:ff:ff:ff:ff:ff).
264 struct ether_addr mac[MLX4_MAX_MAC_ADDRESSES];
265 BITFIELD_DECLARE(mac_configured, uint32_t, MLX4_MAX_MAC_ADDRESSES);
268 unsigned int enabled:1; /* If enabled. */
269 unsigned int id:12; /* VLAN ID (0-4095). */
270 } vlan_filter[MLX4_MAX_VLAN_IDS]; /* VLAN filters table. */
271 /* Device properties. */
272 uint16_t mtu; /* Configured MTU. */
273 uint8_t port; /* Physical port number. */
274 unsigned int started:1; /* Device started, flows enabled. */
275 unsigned int promisc:1; /* Device in promiscuous mode. */
276 unsigned int allmulti:1; /* Device receives all multicast packets. */
277 unsigned int hw_qpg:1; /* QP groups are supported. */
278 unsigned int hw_tss:1; /* TSS is supported. */
279 unsigned int hw_rss:1; /* RSS is supported. */
280 unsigned int rss:1; /* RSS is enabled. */
281 unsigned int vf:1; /* This is a VF device. */
283 unsigned int inl_recv_size; /* Inline recv size */
285 unsigned int max_rss_tbl_sz; /* Maximum number of RSS queues. */
287 struct rxq rxq_parent; /* Parent queue when RSS is enabled. */
288 unsigned int rxqs_n; /* RX queues array size. */
289 unsigned int txqs_n; /* TX queues array size. */
290 struct rxq *(*rxqs)[]; /* RX queues. */
291 struct txq *(*txqs)[]; /* TX queues. */
292 rte_spinlock_t lock; /* Lock for control functions. */
296 * Lock private structure to protect it from concurrent access in the
300 * Pointer to private structure.
303 priv_lock(struct priv *priv)
305 rte_spinlock_lock(&priv->lock);
309 * Unlock private structure.
312 * Pointer to private structure.
315 priv_unlock(struct priv *priv)
317 rte_spinlock_unlock(&priv->lock);
320 /* Allocate a buffer on the stack and fill it with a printf format string. */
321 #define MKSTR(name, ...) \
322 char name[snprintf(NULL, 0, __VA_ARGS__) + 1]; \
324 snprintf(name, sizeof(name), __VA_ARGS__)
327 * Get interface name from private structure.
330 * Pointer to private structure.
332 * Interface name output buffer.
335 * 0 on success, -1 on failure and errno is set.
338 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
342 unsigned int dev_type = 0;
343 unsigned int dev_port_prev = ~0u;
344 char match[IF_NAMESIZE] = "";
347 MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
353 while ((dent = readdir(dir)) != NULL) {
354 char *name = dent->d_name;
356 unsigned int dev_port;
359 if ((name[0] == '.') &&
360 ((name[1] == '\0') ||
361 ((name[1] == '.') && (name[2] == '\0'))))
364 MKSTR(path, "%s/device/net/%s/%s",
365 priv->ctx->device->ibdev_path, name,
366 (dev_type ? "dev_id" : "dev_port"));
368 file = fopen(path, "rb");
373 * Switch to dev_id when dev_port does not exist as
374 * is the case with Linux kernel versions < 3.15.
385 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
390 * Switch to dev_id when dev_port returns the same value for
391 * all ports. May happen when using a MOFED release older than
392 * 3.0 with a Linux kernel >= 3.15.
394 if (dev_port == dev_port_prev)
396 dev_port_prev = dev_port;
397 if (dev_port == (priv->port - 1u))
398 snprintf(match, sizeof(match), "%s", name);
401 if (match[0] == '\0')
403 strncpy(*ifname, match, sizeof(*ifname));
408 * Read from sysfs entry.
411 * Pointer to private structure.
413 * Entry name relative to sysfs path.
415 * Data output buffer.
420 * 0 on success, -1 on failure and errno is set.
423 priv_sysfs_read(const struct priv *priv, const char *entry,
424 char *buf, size_t size)
426 char ifname[IF_NAMESIZE];
431 if (priv_get_ifname(priv, &ifname))
434 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
437 file = fopen(path, "rb");
440 ret = fread(buf, 1, size, file);
442 if (((size_t)ret < size) && (ferror(file)))
452 * Write to sysfs entry.
455 * Pointer to private structure.
457 * Entry name relative to sysfs path.
464 * 0 on success, -1 on failure and errno is set.
467 priv_sysfs_write(const struct priv *priv, const char *entry,
468 char *buf, size_t size)
470 char ifname[IF_NAMESIZE];
475 if (priv_get_ifname(priv, &ifname))
478 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
481 file = fopen(path, "wb");
484 ret = fwrite(buf, 1, size, file);
486 if (((size_t)ret < size) || (ferror(file)))
496 * Get unsigned long sysfs property.
499 * Pointer to private structure.
501 * Entry name relative to sysfs path.
503 * Value output buffer.
506 * 0 on success, -1 on failure and errno is set.
509 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
512 unsigned long value_ret;
515 ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
517 DEBUG("cannot read %s value from sysfs: %s",
518 name, strerror(errno));
521 value_str[ret] = '\0';
523 value_ret = strtoul(value_str, NULL, 0);
525 DEBUG("invalid %s value `%s': %s", name, value_str,
534 * Set unsigned long sysfs property.
537 * Pointer to private structure.
539 * Entry name relative to sysfs path.
544 * 0 on success, -1 on failure and errno is set.
547 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
550 MKSTR(value_str, "%lu", value);
552 ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
554 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
555 name, value_str, value, strerror(errno));
562 * Perform ifreq ioctl() on associated Ethernet device.
565 * Pointer to private structure.
567 * Request number to pass to ioctl().
569 * Interface request structure output buffer.
572 * 0 on success, -1 on failure and errno is set.
575 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
577 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
582 if (priv_get_ifname(priv, &ifr->ifr_name) == 0)
583 ret = ioctl(sock, req, ifr);
592 * Pointer to private structure.
594 * MTU value output buffer.
597 * 0 on success, -1 on failure and errno is set.
600 priv_get_mtu(struct priv *priv, uint16_t *mtu)
602 unsigned long ulong_mtu;
604 if (priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu) == -1)
614 * Pointer to private structure.
619 * 0 on success, -1 on failure and errno is set.
622 priv_set_mtu(struct priv *priv, uint16_t mtu)
624 return priv_set_sysfs_ulong(priv, "mtu", mtu);
631 * Pointer to private structure.
633 * Bitmask for flags that must remain untouched.
635 * Bitmask for flags to modify.
638 * 0 on success, -1 on failure and errno is set.
641 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
645 if (priv_get_sysfs_ulong(priv, "flags", &tmp) == -1)
649 return priv_set_sysfs_ulong(priv, "flags", tmp);
652 /* Device configuration. */
655 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
656 unsigned int socket, const struct rte_eth_rxconf *conf,
657 struct rte_mempool *mp);
660 rxq_cleanup(struct rxq *rxq);
663 * Ethernet device configuration.
665 * Prepare the driver for a given number of TX and RX queues.
666 * Allocate parent RSS queue when several RX queues are requested.
669 * Pointer to Ethernet device structure.
672 * 0 on success, errno value on failure.
675 dev_configure(struct rte_eth_dev *dev)
677 struct priv *priv = dev->data->dev_private;
678 unsigned int rxqs_n = dev->data->nb_rx_queues;
679 unsigned int txqs_n = dev->data->nb_tx_queues;
683 priv->rxqs = (void *)dev->data->rx_queues;
684 priv->txqs = (void *)dev->data->tx_queues;
685 if (txqs_n != priv->txqs_n) {
686 INFO("%p: TX queues number update: %u -> %u",
687 (void *)dev, priv->txqs_n, txqs_n);
688 priv->txqs_n = txqs_n;
690 if (rxqs_n == priv->rxqs_n)
692 INFO("%p: RX queues number update: %u -> %u",
693 (void *)dev, priv->rxqs_n, rxqs_n);
694 /* If RSS is enabled, disable it first. */
698 /* Only if there are no remaining child RX queues. */
699 for (i = 0; (i != priv->rxqs_n); ++i)
700 if ((*priv->rxqs)[i] != NULL)
702 rxq_cleanup(&priv->rxq_parent);
707 /* Nothing else to do. */
708 priv->rxqs_n = rxqs_n;
711 /* Allocate a new RSS parent queue if supported by hardware. */
713 ERROR("%p: only a single RX queue can be configured when"
714 " hardware doesn't support RSS",
718 /* Fail if hardware doesn't support that many RSS queues. */
719 if (rxqs_n >= priv->max_rss_tbl_sz) {
720 ERROR("%p: only %u RX queues can be configured for RSS",
721 (void *)dev, priv->max_rss_tbl_sz);
726 priv->rxqs_n = rxqs_n;
727 ret = rxq_setup(dev, &priv->rxq_parent, 0, 0, NULL, NULL);
730 /* Failure, rollback. */
738 * DPDK callback for Ethernet device configuration.
741 * Pointer to Ethernet device structure.
744 * 0 on success, negative errno value on failure.
747 mlx4_dev_configure(struct rte_eth_dev *dev)
749 struct priv *priv = dev->data->dev_private;
753 ret = dev_configure(dev);
759 /* TX queues handling. */
762 * Allocate TX queue elements.
765 * Pointer to TX queue structure.
767 * Number of elements to allocate.
770 * 0 on success, errno value on failure.
773 txq_alloc_elts(struct txq *txq, unsigned int elts_n)
776 struct txq_elt (*elts)[elts_n] =
777 rte_calloc_socket("TXQ", 1, sizeof(*elts), 0, txq->socket);
778 linear_t (*elts_linear)[elts_n] =
779 rte_calloc_socket("TXQ", 1, sizeof(*elts_linear), 0,
781 struct ibv_mr *mr_linear = NULL;
784 if ((elts == NULL) || (elts_linear == NULL)) {
785 ERROR("%p: can't allocate packets array", (void *)txq);
790 ibv_reg_mr(txq->priv->pd, elts_linear, sizeof(*elts_linear),
791 (IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE));
792 if (mr_linear == NULL) {
793 ERROR("%p: unable to configure MR, ibv_reg_mr() failed",
798 for (i = 0; (i != elts_n); ++i) {
799 struct txq_elt *elt = &(*elts)[i];
800 mlx4_send_wr_t *wr = &elt->wr;
803 WR_ID(wr->wr_id).id = i;
804 WR_ID(wr->wr_id).offset = 0;
805 wr->sg_list = &elt->sges[0];
806 wr->opcode = IBV_WR_SEND;
807 /* Other fields are updated during TX. */
809 DEBUG("%p: allocated and configured %u WRs", (void *)txq, elts_n);
810 txq->elts_n = elts_n;
815 /* Request send completion every MLX4_PMD_TX_PER_COMP_REQ packets or
816 * at least 4 times per ring. */
817 txq->elts_comp_cd_init =
818 ((MLX4_PMD_TX_PER_COMP_REQ < (elts_n / 4)) ?
819 MLX4_PMD_TX_PER_COMP_REQ : (elts_n / 4));
820 txq->elts_comp_cd = txq->elts_comp_cd_init;
821 txq->elts_linear = elts_linear;
822 txq->mr_linear = mr_linear;
826 if (mr_linear != NULL)
827 claim_zero(ibv_dereg_mr(mr_linear));
829 rte_free(elts_linear);
832 DEBUG("%p: failed, freed everything", (void *)txq);
838 * Free TX queue elements.
841 * Pointer to TX queue structure.
844 txq_free_elts(struct txq *txq)
847 unsigned int elts_n = txq->elts_n;
848 struct txq_elt (*elts)[elts_n] = txq->elts;
849 linear_t (*elts_linear)[elts_n] = txq->elts_linear;
850 struct ibv_mr *mr_linear = txq->mr_linear;
852 DEBUG("%p: freeing WRs", (void *)txq);
855 txq->elts_linear = NULL;
856 txq->mr_linear = NULL;
857 if (mr_linear != NULL)
858 claim_zero(ibv_dereg_mr(mr_linear));
860 rte_free(elts_linear);
863 for (i = 0; (i != elemof(*elts)); ++i) {
864 struct txq_elt *elt = &(*elts)[i];
866 if (WR_ID(elt->wr.wr_id).offset == 0)
868 rte_pktmbuf_free((void *)((uintptr_t)elt->sges[0].addr -
869 WR_ID(elt->wr.wr_id).offset));
876 * Clean up a TX queue.
878 * Destroy objects, free allocated memory and reset the structure for reuse.
881 * Pointer to TX queue structure.
884 txq_cleanup(struct txq *txq)
888 DEBUG("cleaning up %p", (void *)txq);
891 claim_zero(ibv_destroy_qp(txq->qp));
893 claim_zero(ibv_destroy_cq(txq->cq));
894 for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
895 if (txq->mp2mr[i].mp == NULL)
897 assert(txq->mp2mr[i].mr != NULL);
898 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
900 memset(txq, 0, sizeof(*txq));
904 * Manage TX completions.
906 * When sending a burst, mlx4_tx_burst() posts several WRs.
907 * To improve performance, a completion event is only required once every
908 * MLX4_PMD_TX_PER_COMP_REQ sends. Doing so discards completion information
909 * for other WRs, but this information would not be used anyway.
912 * Pointer to TX queue structure.
915 * 0 on success, -1 on failure.
918 txq_complete(struct txq *txq)
920 unsigned int elts_comp = txq->elts_comp;
921 unsigned int elts_tail = txq->elts_tail;
922 const unsigned int elts_n = txq->elts_n;
923 struct ibv_wc wcs[elts_comp];
926 if (unlikely(elts_comp == 0))
929 DEBUG("%p: processing %u work requests completions",
930 (void *)txq, elts_comp);
932 wcs_n = ibv_poll_cq(txq->cq, elts_comp, wcs);
933 if (unlikely(wcs_n == 0))
935 if (unlikely(wcs_n < 0)) {
936 DEBUG("%p: ibv_poll_cq() failed (wcs_n=%d)",
941 assert(elts_comp <= txq->elts_comp);
943 * Assume WC status is successful as nothing can be done about it
946 elts_tail += wcs_n * txq->elts_comp_cd_init;
947 if (elts_tail >= elts_n)
949 txq->elts_tail = elts_tail;
950 txq->elts_comp = elts_comp;
955 * Get Memory Region (MR) <-> Memory Pool (MP) association from txq->mp2mr[].
956 * Add MP to txq->mp2mr[] if it's not registered yet. If mp2mr[] is full,
957 * remove an entry first.
960 * Pointer to TX queue structure.
962 * Memory Pool for which a Memory Region lkey must be returned.
965 * mr->lkey on success, (uint32_t)-1 on failure.
968 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
973 for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
974 if (unlikely(txq->mp2mr[i].mp == NULL)) {
975 /* Unknown MP, add a new MR for it. */
978 if (txq->mp2mr[i].mp == mp) {
979 assert(txq->mp2mr[i].lkey != (uint32_t)-1);
980 assert(txq->mp2mr[i].mr->lkey == txq->mp2mr[i].lkey);
981 return txq->mp2mr[i].lkey;
984 /* Add a new entry, register MR first. */
985 DEBUG("%p: discovered new memory pool %p", (void *)txq, (void *)mp);
986 mr = ibv_reg_mr(txq->priv->pd,
987 (void *)mp->elt_va_start,
988 (mp->elt_va_end - mp->elt_va_start),
989 (IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE));
990 if (unlikely(mr == NULL)) {
991 DEBUG("%p: unable to configure MR, ibv_reg_mr() failed.",
995 if (unlikely(i == elemof(txq->mp2mr))) {
996 /* Table is full, remove oldest entry. */
997 DEBUG("%p: MR <-> MP table full, dropping oldest entry.",
1000 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
1001 memmove(&txq->mp2mr[0], &txq->mp2mr[1],
1002 (sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
1004 /* Store the new entry. */
1005 txq->mp2mr[i].mp = mp;
1006 txq->mp2mr[i].mr = mr;
1007 txq->mp2mr[i].lkey = mr->lkey;
1008 DEBUG("%p: new MR lkey for MP %p: 0x%08" PRIu32,
1009 (void *)txq, (void *)mp, txq->mp2mr[i].lkey);
1010 return txq->mp2mr[i].lkey;
1014 * Copy scattered mbuf contents to a single linear buffer.
1016 * @param[out] linear
1017 * Linear output buffer.
1019 * Scattered input buffer.
1022 * Number of bytes copied to the output buffer or 0 if not large enough.
1025 linearize_mbuf(linear_t *linear, struct rte_mbuf *buf)
1027 unsigned int size = 0;
1028 unsigned int offset;
1031 unsigned int len = DATA_LEN(buf);
1035 if (unlikely(size > sizeof(*linear)))
1037 memcpy(&(*linear)[offset],
1038 rte_pktmbuf_mtod(buf, uint8_t *),
1041 } while (buf != NULL);
1046 * DPDK callback for TX.
1049 * Generic pointer to TX queue structure.
1051 * Packets to transmit.
1053 * Number of packets in array.
1056 * Number of packets successfully transmitted (<= pkts_n).
1059 mlx4_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
1061 struct txq *txq = (struct txq *)dpdk_txq;
1062 mlx4_send_wr_t head;
1063 mlx4_send_wr_t **wr_next = &head.next;
1064 mlx4_send_wr_t *bad_wr;
1065 unsigned int elts_head = txq->elts_head;
1066 const unsigned int elts_tail = txq->elts_tail;
1067 const unsigned int elts_n = txq->elts_n;
1068 unsigned int elts_comp_cd = txq->elts_comp_cd;
1069 unsigned int elts_comp = 0;
1074 assert(elts_comp_cd != 0);
1076 max = (elts_n - (elts_head - elts_tail));
1080 assert(max <= elts_n);
1081 /* Always leave one free entry in the ring. */
1087 for (i = 0; (i != max); ++i) {
1088 struct rte_mbuf *buf = pkts[i];
1089 struct txq_elt *elt = &(*txq->elts)[elts_head];
1090 mlx4_send_wr_t *wr = &elt->wr;
1091 unsigned int segs = NB_SEGS(buf);
1092 #if (MLX4_PMD_MAX_INLINE > 0) || defined(MLX4_PMD_SOFT_COUNTERS)
1093 unsigned int sent_size = 0;
1098 /* Clean up old buffer. */
1099 if (likely(WR_ID(wr->wr_id).offset != 0)) {
1100 struct rte_mbuf *tmp = (void *)
1101 ((uintptr_t)elt->sges[0].addr -
1102 WR_ID(wr->wr_id).offset);
1104 /* Faster than rte_pktmbuf_free(). */
1106 struct rte_mbuf *next = NEXT(tmp);
1108 rte_pktmbuf_free_seg(tmp);
1110 } while (tmp != NULL);
1114 WR_ID(wr->wr_id).offset = 0;
1115 for (j = 0; ((int)j < wr->num_sge); ++j) {
1116 elt->sges[j].addr = 0;
1117 elt->sges[j].length = 0;
1118 elt->sges[j].lkey = 0;
1123 /* Sanity checks, most of which are only relevant with
1124 * debugging enabled. */
1125 assert(WR_ID(wr->wr_id).id == elts_head);
1126 assert(WR_ID(wr->wr_id).offset == 0);
1127 assert(wr->next == NULL);
1128 assert(wr->sg_list == &elt->sges[0]);
1129 assert(wr->num_sge == 0);
1130 assert(wr->opcode == IBV_WR_SEND);
1131 /* When there are too many segments, extra segments are
1132 * linearized in the last SGE. */
1133 if (unlikely(segs > elemof(elt->sges))) {
1134 segs = (elemof(elt->sges) - 1);
1137 /* Set WR fields. */
1138 assert((rte_pktmbuf_mtod(buf, uintptr_t) -
1139 (uintptr_t)buf) <= 0xffff);
1140 WR_ID(wr->wr_id).offset =
1141 (rte_pktmbuf_mtod(buf, uintptr_t) -
1144 /* Register segments as SGEs. */
1145 for (j = 0; (j != segs); ++j) {
1146 struct ibv_sge *sge = &elt->sges[j];
1149 /* Retrieve Memory Region key for this memory pool. */
1150 lkey = txq_mp2mr(txq, buf->pool);
1151 if (unlikely(lkey == (uint32_t)-1)) {
1152 /* MR does not exist. */
1153 DEBUG("%p: unable to get MP <-> MR"
1154 " association", (void *)txq);
1155 /* Clean up TX element. */
1156 WR_ID(elt->wr.wr_id).offset = 0;
1170 /* Sanity checks, only relevant with debugging
1172 assert(sge->addr == 0);
1173 assert(sge->length == 0);
1174 assert(sge->lkey == 0);
1176 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
1178 rte_prefetch0((volatile void *)
1179 (uintptr_t)sge->addr);
1180 sge->length = DATA_LEN(buf);
1182 #if (MLX4_PMD_MAX_INLINE > 0) || defined(MLX4_PMD_SOFT_COUNTERS)
1183 sent_size += sge->length;
1187 /* If buf is not NULL here and is not going to be linearized,
1188 * nb_segs is not valid. */
1190 assert((buf == NULL) || (linearize));
1191 /* Linearize extra segments. */
1193 struct ibv_sge *sge = &elt->sges[segs];
1194 linear_t *linear = &(*txq->elts_linear)[elts_head];
1195 unsigned int size = linearize_mbuf(linear, buf);
1197 assert(segs == (elemof(elt->sges) - 1));
1199 /* Invalid packet. */
1200 DEBUG("%p: packet too large to be linearized.",
1202 /* Clean up TX element. */
1203 WR_ID(elt->wr.wr_id).offset = 0;
1217 /* If MLX4_PMD_SGE_WR_N is 1, free mbuf immediately
1218 * and clear offset from WR ID. */
1219 if (elemof(elt->sges) == 1) {
1221 struct rte_mbuf *next = NEXT(buf);
1223 rte_pktmbuf_free_seg(buf);
1225 } while (buf != NULL);
1226 WR_ID(wr->wr_id).offset = 0;
1228 /* Set WR fields and fill SGE with linear buffer. */
1230 /* Sanity checks, only relevant with debugging
1232 assert(sge->addr == 0);
1233 assert(sge->length == 0);
1234 assert(sge->lkey == 0);
1236 sge->addr = (uintptr_t)&(*linear)[0];
1238 sge->lkey = txq->mr_linear->lkey;
1239 #if (MLX4_PMD_MAX_INLINE > 0) || defined(MLX4_PMD_SOFT_COUNTERS)
1243 /* Link WRs together for ibv_post_send(). */
1245 wr_next = &wr->next;
1246 #if MLX4_PMD_MAX_INLINE > 0
1247 if (sent_size <= txq->max_inline)
1248 wr->send_flags = IBV_SEND_INLINE;
1252 /* Request TX completion. */
1253 if (unlikely(--elts_comp_cd == 0)) {
1254 elts_comp_cd = txq->elts_comp_cd_init;
1256 wr->send_flags |= IBV_SEND_SIGNALED;
1258 if (++elts_head >= elts_n)
1260 #ifdef MLX4_PMD_SOFT_COUNTERS
1261 /* Increment sent bytes counter. */
1262 txq->stats.obytes += sent_size;
1266 /* Take a shortcut if nothing must be sent. */
1267 if (unlikely(i == 0))
1269 #ifdef MLX4_PMD_SOFT_COUNTERS
1270 /* Increment sent packets counter. */
1271 txq->stats.opackets += i;
1274 err = mlx4_post_send(txq->qp, head.next, &bad_wr);
1275 if (unlikely(err)) {
1276 unsigned int unsent = 0;
1278 /* An error occurred, fix counters. */
1279 while (bad_wr != NULL) {
1280 struct txq_elt *elt =
1281 containerof(bad_wr, struct txq_elt, wr);
1282 mlx4_send_wr_t *wr = &elt->wr;
1283 mlx4_send_wr_t *next = wr->next;
1284 #if defined(MLX4_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
1288 assert(wr == bad_wr);
1289 /* Clean up TX element without freeing it, caller
1290 * should take care of this. */
1291 WR_ID(elt->wr.wr_id).offset = 0;
1292 #ifdef MLX4_PMD_SOFT_COUNTERS
1293 for (j = 0; ((int)j < wr->num_sge); ++j)
1294 txq->stats.obytes -= wr->sg_list[j].length;
1297 if (wr->send_flags & IBV_SEND_SIGNALED) {
1298 assert(elts_comp != 0);
1301 if (elts_comp_cd == txq->elts_comp_cd_init)
1307 for (j = 0; ((int)j < wr->num_sge); ++j) {
1308 elt->sges[j].addr = 0;
1309 elt->sges[j].length = 0;
1310 elt->sges[j].lkey = 0;
1317 #ifdef MLX4_PMD_SOFT_COUNTERS
1318 txq->stats.opackets -= unsent;
1320 assert(i >= unsent);
1322 /* "Unsend" remaining packets. */
1323 elts_head -= unsent;
1324 if (elts_head >= elts_n)
1325 elts_head += elts_n;
1326 assert(elts_head < elts_n);
1327 DEBUG("%p: mlx4_post_send() failed, %u unprocessed WRs: %s",
1328 (void *)txq, unsent,
1329 ((err <= -1) ? "Internal error" : strerror(err)));
1331 txq->elts_head = elts_head;
1332 txq->elts_comp += elts_comp;
1333 txq->elts_comp_cd = elts_comp_cd;
1338 * Configure a TX queue.
1341 * Pointer to Ethernet device structure.
1343 * Pointer to TX queue structure.
1345 * Number of descriptors to configure in queue.
1347 * NUMA socket on which memory must be allocated.
1349 * Thresholds parameters.
1352 * 0 on success, errno value on failure.
1355 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
1356 unsigned int socket, const struct rte_eth_txconf *conf)
1358 struct priv *priv = dev->data->dev_private;
1364 struct ibv_qp_init_attr init;
1365 struct ibv_exp_qp_attr mod;
1369 (void)conf; /* Thresholds configuration (ignored). */
1370 if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
1371 ERROR("%p: invalid number of TX descriptors (must be a"
1372 " multiple of %d)", (void *)dev, MLX4_PMD_SGE_WR_N);
1375 desc /= MLX4_PMD_SGE_WR_N;
1376 /* MRs will be registered in mp2mr[] later. */
1377 tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
1378 if (tmpl.cq == NULL) {
1380 ERROR("%p: CQ creation failure: %s",
1381 (void *)dev, strerror(ret));
1384 DEBUG("priv->device_attr.max_qp_wr is %d",
1385 priv->device_attr.max_qp_wr);
1386 DEBUG("priv->device_attr.max_sge is %d",
1387 priv->device_attr.max_sge);
1388 attr.init = (struct ibv_qp_init_attr){
1389 /* CQ to be associated with the send queue. */
1391 /* CQ to be associated with the receive queue. */
1394 /* Max number of outstanding WRs. */
1395 .max_send_wr = ((priv->device_attr.max_qp_wr < desc) ?
1396 priv->device_attr.max_qp_wr :
1398 /* Max number of scatter/gather elements in a WR. */
1399 .max_send_sge = ((priv->device_attr.max_sge <
1400 MLX4_PMD_SGE_WR_N) ?
1401 priv->device_attr.max_sge :
1403 #if MLX4_PMD_MAX_INLINE > 0
1404 .max_inline_data = MLX4_PMD_MAX_INLINE,
1407 .qp_type = IBV_QPT_RAW_PACKET,
1408 /* Do *NOT* enable this, completions events are managed per
1412 tmpl.qp = ibv_create_qp(priv->pd, &attr.init);
1413 if (tmpl.qp == NULL) {
1414 ret = (errno ? errno : EINVAL);
1415 ERROR("%p: QP creation failure: %s",
1416 (void *)dev, strerror(ret));
1419 #if MLX4_PMD_MAX_INLINE > 0
1420 /* ibv_create_qp() updates this value. */
1421 tmpl.max_inline = attr.init.cap.max_inline_data;
1423 attr.mod = (struct ibv_exp_qp_attr){
1424 /* Move the QP to this state. */
1425 .qp_state = IBV_QPS_INIT,
1426 /* Primary port number. */
1427 .port_num = priv->port
1429 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod,
1430 (IBV_EXP_QP_STATE | IBV_EXP_QP_PORT));
1432 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
1433 (void *)dev, strerror(ret));
1436 ret = txq_alloc_elts(&tmpl, desc);
1438 ERROR("%p: TXQ allocation failed: %s",
1439 (void *)dev, strerror(ret));
1442 attr.mod = (struct ibv_exp_qp_attr){
1443 .qp_state = IBV_QPS_RTR
1445 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1447 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
1448 (void *)dev, strerror(ret));
1451 attr.mod.qp_state = IBV_QPS_RTS;
1452 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1454 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
1455 (void *)dev, strerror(ret));
1458 /* Clean up txq in case we're reinitializing it. */
1459 DEBUG("%p: cleaning-up old txq just in case", (void *)txq);
1462 DEBUG("%p: txq updated with %p", (void *)txq, (void *)&tmpl);
1472 * DPDK callback to configure a TX queue.
1475 * Pointer to Ethernet device structure.
1479 * Number of descriptors to configure in queue.
1481 * NUMA socket on which memory must be allocated.
1483 * Thresholds parameters.
1486 * 0 on success, negative errno value on failure.
1489 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1490 unsigned int socket, const struct rte_eth_txconf *conf)
1492 struct priv *priv = dev->data->dev_private;
1493 struct txq *txq = (*priv->txqs)[idx];
1497 DEBUG("%p: configuring queue %u for %u descriptors",
1498 (void *)dev, idx, desc);
1499 if (idx >= priv->txqs_n) {
1500 ERROR("%p: queue index out of range (%u >= %u)",
1501 (void *)dev, idx, priv->txqs_n);
1506 DEBUG("%p: reusing already allocated queue index %u (%p)",
1507 (void *)dev, idx, (void *)txq);
1508 if (priv->started) {
1512 (*priv->txqs)[idx] = NULL;
1515 txq = rte_calloc_socket("TXQ", 1, sizeof(*txq), 0, socket);
1517 ERROR("%p: unable to allocate queue index %u",
1523 ret = txq_setup(dev, txq, desc, socket, conf);
1527 txq->stats.idx = idx;
1528 DEBUG("%p: adding TX queue %p to list",
1529 (void *)dev, (void *)txq);
1530 (*priv->txqs)[idx] = txq;
1531 /* Update send callback. */
1532 dev->tx_pkt_burst = mlx4_tx_burst;
1539 * DPDK callback to release a TX queue.
1542 * Generic TX queue pointer.
1545 mlx4_tx_queue_release(void *dpdk_txq)
1547 struct txq *txq = (struct txq *)dpdk_txq;
1555 for (i = 0; (i != priv->txqs_n); ++i)
1556 if ((*priv->txqs)[i] == txq) {
1557 DEBUG("%p: removing TX queue %p from list",
1558 (void *)priv->dev, (void *)txq);
1559 (*priv->txqs)[i] = NULL;
1567 /* RX queues handling. */
1570 * Allocate RX queue elements with scattered packets support.
1573 * Pointer to RX queue structure.
1575 * Number of elements to allocate.
1577 * If not NULL, fetch buffers from this array instead of allocating them
1578 * with rte_pktmbuf_alloc().
1581 * 0 on success, errno value on failure.
1584 rxq_alloc_elts_sp(struct rxq *rxq, unsigned int elts_n,
1585 struct rte_mbuf **pool)
1588 struct rxq_elt_sp (*elts)[elts_n] =
1589 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1594 ERROR("%p: can't allocate packets array", (void *)rxq);
1598 /* For each WR (packet). */
1599 for (i = 0; (i != elts_n); ++i) {
1601 struct rxq_elt_sp *elt = &(*elts)[i];
1602 struct ibv_recv_wr *wr = &elt->wr;
1603 struct ibv_sge (*sges)[(elemof(elt->sges))] = &elt->sges;
1605 /* These two arrays must have the same size. */
1606 assert(elemof(elt->sges) == elemof(elt->bufs));
1609 wr->next = &(*elts)[(i + 1)].wr;
1610 wr->sg_list = &(*sges)[0];
1611 wr->num_sge = elemof(*sges);
1612 /* For each SGE (segment). */
1613 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1614 struct ibv_sge *sge = &(*sges)[j];
1615 struct rte_mbuf *buf;
1619 assert(buf != NULL);
1620 rte_pktmbuf_reset(buf);
1622 buf = rte_pktmbuf_alloc(rxq->mp);
1624 assert(pool == NULL);
1625 ERROR("%p: empty mbuf pool", (void *)rxq);
1630 /* Headroom is reserved by rte_pktmbuf_alloc(). */
1631 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1632 /* Buffer is supposed to be empty. */
1633 assert(rte_pktmbuf_data_len(buf) == 0);
1634 assert(rte_pktmbuf_pkt_len(buf) == 0);
1635 /* sge->addr must be able to store a pointer. */
1636 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1638 /* The first SGE keeps its headroom. */
1639 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
1640 sge->length = (buf->buf_len -
1641 RTE_PKTMBUF_HEADROOM);
1643 /* Subsequent SGEs lose theirs. */
1644 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1645 SET_DATA_OFF(buf, 0);
1646 sge->addr = (uintptr_t)buf->buf_addr;
1647 sge->length = buf->buf_len;
1649 sge->lkey = rxq->mr->lkey;
1650 /* Redundant check for tailroom. */
1651 assert(sge->length == rte_pktmbuf_tailroom(buf));
1654 /* The last WR pointer must be NULL. */
1655 (*elts)[(i - 1)].wr.next = NULL;
1656 DEBUG("%p: allocated and configured %u WRs (%zu segments)",
1657 (void *)rxq, elts_n, (elts_n * elemof((*elts)[0].sges)));
1658 rxq->elts_n = elts_n;
1660 rxq->elts.sp = elts;
1665 assert(pool == NULL);
1666 for (i = 0; (i != elemof(*elts)); ++i) {
1668 struct rxq_elt_sp *elt = &(*elts)[i];
1670 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1671 struct rte_mbuf *buf = elt->bufs[j];
1674 rte_pktmbuf_free_seg(buf);
1679 DEBUG("%p: failed, freed everything", (void *)rxq);
1685 * Free RX queue elements with scattered packets support.
1688 * Pointer to RX queue structure.
1691 rxq_free_elts_sp(struct rxq *rxq)
1694 unsigned int elts_n = rxq->elts_n;
1695 struct rxq_elt_sp (*elts)[elts_n] = rxq->elts.sp;
1697 DEBUG("%p: freeing WRs", (void *)rxq);
1699 rxq->elts.sp = NULL;
1702 for (i = 0; (i != elemof(*elts)); ++i) {
1704 struct rxq_elt_sp *elt = &(*elts)[i];
1706 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1707 struct rte_mbuf *buf = elt->bufs[j];
1710 rte_pktmbuf_free_seg(buf);
1717 * Allocate RX queue elements.
1720 * Pointer to RX queue structure.
1722 * Number of elements to allocate.
1724 * If not NULL, fetch buffers from this array instead of allocating them
1725 * with rte_pktmbuf_alloc().
1728 * 0 on success, errno value on failure.
1731 rxq_alloc_elts(struct rxq *rxq, unsigned int elts_n, struct rte_mbuf **pool)
1734 struct rxq_elt (*elts)[elts_n] =
1735 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1740 ERROR("%p: can't allocate packets array", (void *)rxq);
1744 /* For each WR (packet). */
1745 for (i = 0; (i != elts_n); ++i) {
1746 struct rxq_elt *elt = &(*elts)[i];
1747 struct ibv_recv_wr *wr = &elt->wr;
1748 struct ibv_sge *sge = &(*elts)[i].sge;
1749 struct rte_mbuf *buf;
1753 assert(buf != NULL);
1754 rte_pktmbuf_reset(buf);
1756 buf = rte_pktmbuf_alloc(rxq->mp);
1758 assert(pool == NULL);
1759 ERROR("%p: empty mbuf pool", (void *)rxq);
1763 /* Configure WR. Work request ID contains its own index in
1764 * the elts array and the offset between SGE buffer header and
1766 WR_ID(wr->wr_id).id = i;
1767 WR_ID(wr->wr_id).offset =
1768 (((uintptr_t)buf->buf_addr + RTE_PKTMBUF_HEADROOM) -
1770 wr->next = &(*elts)[(i + 1)].wr;
1773 /* Headroom is reserved by rte_pktmbuf_alloc(). */
1774 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1775 /* Buffer is supposed to be empty. */
1776 assert(rte_pktmbuf_data_len(buf) == 0);
1777 assert(rte_pktmbuf_pkt_len(buf) == 0);
1778 /* sge->addr must be able to store a pointer. */
1779 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1780 /* SGE keeps its headroom. */
1781 sge->addr = (uintptr_t)
1782 ((uint8_t *)buf->buf_addr + RTE_PKTMBUF_HEADROOM);
1783 sge->length = (buf->buf_len - RTE_PKTMBUF_HEADROOM);
1784 sge->lkey = rxq->mr->lkey;
1785 /* Redundant check for tailroom. */
1786 assert(sge->length == rte_pktmbuf_tailroom(buf));
1787 /* Make sure elts index and SGE mbuf pointer can be deduced
1789 if ((WR_ID(wr->wr_id).id != i) ||
1790 ((void *)((uintptr_t)sge->addr -
1791 WR_ID(wr->wr_id).offset) != buf)) {
1792 ERROR("%p: cannot store index and offset in WR ID",
1795 rte_pktmbuf_free(buf);
1800 /* The last WR pointer must be NULL. */
1801 (*elts)[(i - 1)].wr.next = NULL;
1802 DEBUG("%p: allocated and configured %u single-segment WRs",
1803 (void *)rxq, elts_n);
1804 rxq->elts_n = elts_n;
1806 rxq->elts.no_sp = elts;
1811 assert(pool == NULL);
1812 for (i = 0; (i != elemof(*elts)); ++i) {
1813 struct rxq_elt *elt = &(*elts)[i];
1814 struct rte_mbuf *buf;
1816 if (elt->sge.addr == 0)
1818 assert(WR_ID(elt->wr.wr_id).id == i);
1819 buf = (void *)((uintptr_t)elt->sge.addr -
1820 WR_ID(elt->wr.wr_id).offset);
1821 rte_pktmbuf_free_seg(buf);
1825 DEBUG("%p: failed, freed everything", (void *)rxq);
1831 * Free RX queue elements.
1834 * Pointer to RX queue structure.
1837 rxq_free_elts(struct rxq *rxq)
1840 unsigned int elts_n = rxq->elts_n;
1841 struct rxq_elt (*elts)[elts_n] = rxq->elts.no_sp;
1843 DEBUG("%p: freeing WRs", (void *)rxq);
1845 rxq->elts.no_sp = NULL;
1848 for (i = 0; (i != elemof(*elts)); ++i) {
1849 struct rxq_elt *elt = &(*elts)[i];
1850 struct rte_mbuf *buf;
1852 if (elt->sge.addr == 0)
1854 assert(WR_ID(elt->wr.wr_id).id == i);
1855 buf = (void *)((uintptr_t)elt->sge.addr -
1856 WR_ID(elt->wr.wr_id).offset);
1857 rte_pktmbuf_free_seg(buf);
1863 * Delete flow steering rule.
1866 * Pointer to RX queue structure.
1868 * MAC address index.
1873 rxq_del_flow(struct rxq *rxq, unsigned int mac_index, unsigned int vlan_index)
1876 struct priv *priv = rxq->priv;
1877 const uint8_t (*mac)[ETHER_ADDR_LEN] =
1878 (const uint8_t (*)[ETHER_ADDR_LEN])
1879 priv->mac[mac_index].addr_bytes;
1881 assert(rxq->mac_flow[mac_index][vlan_index] != NULL);
1882 DEBUG("%p: removing MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
1883 " (VLAN ID %" PRIu16 ")",
1885 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
1886 mac_index, priv->vlan_filter[vlan_index].id);
1887 claim_zero(ibv_destroy_flow(rxq->mac_flow[mac_index][vlan_index]));
1888 rxq->mac_flow[mac_index][vlan_index] = NULL;
1892 * Unregister a MAC address from a RX queue.
1895 * Pointer to RX queue structure.
1897 * MAC address index.
1900 rxq_mac_addr_del(struct rxq *rxq, unsigned int mac_index)
1902 struct priv *priv = rxq->priv;
1904 unsigned int vlans = 0;
1906 assert(mac_index < elemof(priv->mac));
1907 if (!BITFIELD_ISSET(rxq->mac_configured, mac_index))
1909 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
1910 if (!priv->vlan_filter[i].enabled)
1912 rxq_del_flow(rxq, mac_index, i);
1916 rxq_del_flow(rxq, mac_index, 0);
1918 BITFIELD_RESET(rxq->mac_configured, mac_index);
1922 * Unregister all MAC addresses from a RX queue.
1925 * Pointer to RX queue structure.
1928 rxq_mac_addrs_del(struct rxq *rxq)
1930 struct priv *priv = rxq->priv;
1933 for (i = 0; (i != elemof(priv->mac)); ++i)
1934 rxq_mac_addr_del(rxq, i);
1937 static int rxq_promiscuous_enable(struct rxq *);
1938 static void rxq_promiscuous_disable(struct rxq *);
1941 * Add single flow steering rule.
1944 * Pointer to RX queue structure.
1946 * MAC address index to register.
1948 * VLAN index. Use -1 for a flow without VLAN.
1951 * 0 on success, errno value on failure.
1954 rxq_add_flow(struct rxq *rxq, unsigned int mac_index, unsigned int vlan_index)
1956 struct ibv_flow *flow;
1957 struct priv *priv = rxq->priv;
1958 const uint8_t (*mac)[ETHER_ADDR_LEN] =
1959 (const uint8_t (*)[ETHER_ADDR_LEN])
1960 priv->mac[mac_index].addr_bytes;
1962 /* Allocate flow specification on the stack. */
1963 struct __attribute__((packed)) {
1964 struct ibv_flow_attr attr;
1965 struct ibv_flow_spec_eth spec;
1967 struct ibv_flow_attr *attr = &data.attr;
1968 struct ibv_flow_spec_eth *spec = &data.spec;
1970 assert(mac_index < elemof(priv->mac));
1971 assert((vlan_index < elemof(priv->vlan_filter)) || (vlan_index == -1u));
1973 * No padding must be inserted by the compiler between attr and spec.
1974 * This layout is expected by libibverbs.
1976 assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec);
1977 *attr = (struct ibv_flow_attr){
1978 .type = IBV_FLOW_ATTR_NORMAL,
1983 *spec = (struct ibv_flow_spec_eth){
1984 .type = IBV_FLOW_SPEC_ETH,
1985 .size = sizeof(*spec),
1988 (*mac)[0], (*mac)[1], (*mac)[2],
1989 (*mac)[3], (*mac)[4], (*mac)[5]
1991 .vlan_tag = ((vlan_index != -1u) ?
1992 htons(priv->vlan_filter[vlan_index].id) :
1996 .dst_mac = "\xff\xff\xff\xff\xff\xff",
1997 .vlan_tag = ((vlan_index != -1u) ? htons(0xfff) : 0),
2000 DEBUG("%p: adding MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
2001 " (VLAN %s %" PRIu16 ")",
2003 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
2005 ((vlan_index != -1u) ? "ID" : "index"),
2006 ((vlan_index != -1u) ? priv->vlan_filter[vlan_index].id : -1u));
2007 /* Create related flow. */
2009 flow = ibv_create_flow(rxq->qp, attr);
2011 /* It's not clear whether errno is always set in this case. */
2012 ERROR("%p: flow configuration failed, errno=%d: %s",
2014 (errno ? strerror(errno) : "Unknown error"));
2019 if (vlan_index == -1u)
2021 assert(rxq->mac_flow[mac_index][vlan_index] == NULL);
2022 rxq->mac_flow[mac_index][vlan_index] = flow;
2027 * Register a MAC address in a RX queue.
2030 * Pointer to RX queue structure.
2032 * MAC address index to register.
2035 * 0 on success, errno value on failure.
2038 rxq_mac_addr_add(struct rxq *rxq, unsigned int mac_index)
2040 struct priv *priv = rxq->priv;
2042 unsigned int vlans = 0;
2045 assert(mac_index < elemof(priv->mac));
2046 if (BITFIELD_ISSET(rxq->mac_configured, mac_index))
2047 rxq_mac_addr_del(rxq, mac_index);
2048 /* Fill VLAN specifications. */
2049 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
2050 if (!priv->vlan_filter[i].enabled)
2052 /* Create related flow. */
2053 ret = rxq_add_flow(rxq, mac_index, i);
2058 /* Failure, rollback. */
2060 if (priv->vlan_filter[--i].enabled)
2061 rxq_del_flow(rxq, mac_index, i);
2065 /* In case there is no VLAN filter. */
2067 ret = rxq_add_flow(rxq, mac_index, -1);
2071 BITFIELD_SET(rxq->mac_configured, mac_index);
2076 * Register all MAC addresses in a RX queue.
2079 * Pointer to RX queue structure.
2082 * 0 on success, errno value on failure.
2085 rxq_mac_addrs_add(struct rxq *rxq)
2087 struct priv *priv = rxq->priv;
2091 for (i = 0; (i != elemof(priv->mac)); ++i) {
2092 if (!BITFIELD_ISSET(priv->mac_configured, i))
2094 ret = rxq_mac_addr_add(rxq, i);
2097 /* Failure, rollback. */
2099 rxq_mac_addr_del(rxq, --i);
2107 * Unregister a MAC address.
2109 * In RSS mode, the MAC address is unregistered from the parent queue,
2110 * otherwise it is unregistered from each queue directly.
2113 * Pointer to private structure.
2115 * MAC address index.
2118 priv_mac_addr_del(struct priv *priv, unsigned int mac_index)
2122 assert(mac_index < elemof(priv->mac));
2123 if (!BITFIELD_ISSET(priv->mac_configured, mac_index))
2126 rxq_mac_addr_del(&priv->rxq_parent, mac_index);
2129 for (i = 0; (i != priv->dev->data->nb_rx_queues); ++i)
2130 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2132 BITFIELD_RESET(priv->mac_configured, mac_index);
2136 * Register a MAC address.
2138 * In RSS mode, the MAC address is registered in the parent queue,
2139 * otherwise it is registered in each queue directly.
2142 * Pointer to private structure.
2144 * MAC address index to use.
2146 * MAC address to register.
2149 * 0 on success, errno value on failure.
2152 priv_mac_addr_add(struct priv *priv, unsigned int mac_index,
2153 const uint8_t (*mac)[ETHER_ADDR_LEN])
2158 assert(mac_index < elemof(priv->mac));
2159 /* First, make sure this address isn't already configured. */
2160 for (i = 0; (i != elemof(priv->mac)); ++i) {
2161 /* Skip this index, it's going to be reconfigured. */
2164 if (!BITFIELD_ISSET(priv->mac_configured, i))
2166 if (memcmp(priv->mac[i].addr_bytes, *mac, sizeof(*mac)))
2168 /* Address already configured elsewhere, return with error. */
2171 if (BITFIELD_ISSET(priv->mac_configured, mac_index))
2172 priv_mac_addr_del(priv, mac_index);
2173 priv->mac[mac_index] = (struct ether_addr){
2175 (*mac)[0], (*mac)[1], (*mac)[2],
2176 (*mac)[3], (*mac)[4], (*mac)[5]
2179 /* If device isn't started, this is all we need to do. */
2180 if (!priv->started) {
2182 /* Verify that all queues have this index disabled. */
2183 for (i = 0; (i != priv->rxqs_n); ++i) {
2184 if ((*priv->rxqs)[i] == NULL)
2186 assert(!BITFIELD_ISSET
2187 ((*priv->rxqs)[i]->mac_configured, mac_index));
2193 ret = rxq_mac_addr_add(&priv->rxq_parent, mac_index);
2198 for (i = 0; (i != priv->rxqs_n); ++i) {
2199 if ((*priv->rxqs)[i] == NULL)
2201 ret = rxq_mac_addr_add((*priv->rxqs)[i], mac_index);
2204 /* Failure, rollback. */
2206 if ((*priv->rxqs)[(--i)] != NULL)
2207 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2211 BITFIELD_SET(priv->mac_configured, mac_index);
2216 * Enable allmulti mode in a RX queue.
2219 * Pointer to RX queue structure.
2222 * 0 on success, errno value on failure.
2225 rxq_allmulticast_enable(struct rxq *rxq)
2227 struct ibv_flow *flow;
2228 struct ibv_flow_attr attr = {
2229 .type = IBV_FLOW_ATTR_MC_DEFAULT,
2231 .port = rxq->priv->port,
2235 DEBUG("%p: enabling allmulticast mode", (void *)rxq);
2236 if (rxq->allmulti_flow != NULL)
2239 flow = ibv_create_flow(rxq->qp, &attr);
2241 /* It's not clear whether errno is always set in this case. */
2242 ERROR("%p: flow configuration failed, errno=%d: %s",
2244 (errno ? strerror(errno) : "Unknown error"));
2249 rxq->allmulti_flow = flow;
2250 DEBUG("%p: allmulticast mode enabled", (void *)rxq);
2255 * Disable allmulti mode in a RX queue.
2258 * Pointer to RX queue structure.
2261 rxq_allmulticast_disable(struct rxq *rxq)
2263 DEBUG("%p: disabling allmulticast mode", (void *)rxq);
2264 if (rxq->allmulti_flow == NULL)
2266 claim_zero(ibv_destroy_flow(rxq->allmulti_flow));
2267 rxq->allmulti_flow = NULL;
2268 DEBUG("%p: allmulticast mode disabled", (void *)rxq);
2272 * Enable promiscuous mode in a RX queue.
2275 * Pointer to RX queue structure.
2278 * 0 on success, errno value on failure.
2281 rxq_promiscuous_enable(struct rxq *rxq)
2283 struct ibv_flow *flow;
2284 struct ibv_flow_attr attr = {
2285 .type = IBV_FLOW_ATTR_ALL_DEFAULT,
2287 .port = rxq->priv->port,
2293 DEBUG("%p: enabling promiscuous mode", (void *)rxq);
2294 if (rxq->promisc_flow != NULL)
2297 flow = ibv_create_flow(rxq->qp, &attr);
2299 /* It's not clear whether errno is always set in this case. */
2300 ERROR("%p: flow configuration failed, errno=%d: %s",
2302 (errno ? strerror(errno) : "Unknown error"));
2307 rxq->promisc_flow = flow;
2308 DEBUG("%p: promiscuous mode enabled", (void *)rxq);
2313 * Disable promiscuous mode in a RX queue.
2316 * Pointer to RX queue structure.
2319 rxq_promiscuous_disable(struct rxq *rxq)
2323 DEBUG("%p: disabling promiscuous mode", (void *)rxq);
2324 if (rxq->promisc_flow == NULL)
2326 claim_zero(ibv_destroy_flow(rxq->promisc_flow));
2327 rxq->promisc_flow = NULL;
2328 DEBUG("%p: promiscuous mode disabled", (void *)rxq);
2332 * Clean up a RX queue.
2334 * Destroy objects, free allocated memory and reset the structure for reuse.
2337 * Pointer to RX queue structure.
2340 rxq_cleanup(struct rxq *rxq)
2342 struct ibv_exp_release_intf_params params;
2344 DEBUG("cleaning up %p", (void *)rxq);
2346 rxq_free_elts_sp(rxq);
2349 if (rxq->if_qp != NULL) {
2350 assert(rxq->priv != NULL);
2351 assert(rxq->priv->ctx != NULL);
2352 assert(rxq->qp != NULL);
2353 params = (struct ibv_exp_release_intf_params){
2356 claim_zero(ibv_exp_release_intf(rxq->priv->ctx,
2360 if (rxq->if_cq != NULL) {
2361 assert(rxq->priv != NULL);
2362 assert(rxq->priv->ctx != NULL);
2363 assert(rxq->cq != NULL);
2364 params = (struct ibv_exp_release_intf_params){
2367 claim_zero(ibv_exp_release_intf(rxq->priv->ctx,
2371 if (rxq->qp != NULL) {
2372 rxq_promiscuous_disable(rxq);
2373 rxq_allmulticast_disable(rxq);
2374 rxq_mac_addrs_del(rxq);
2375 claim_zero(ibv_destroy_qp(rxq->qp));
2377 if (rxq->cq != NULL)
2378 claim_zero(ibv_destroy_cq(rxq->cq));
2379 if (rxq->mr != NULL)
2380 claim_zero(ibv_dereg_mr(rxq->mr));
2381 memset(rxq, 0, sizeof(*rxq));
2385 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n);
2388 * DPDK callback for RX with scattered packets support.
2391 * Generic pointer to RX queue structure.
2393 * Array to store received packets.
2395 * Maximum number of packets in array.
2398 * Number of packets successfully received (<= pkts_n).
2401 mlx4_rx_burst_sp(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2403 struct rxq *rxq = (struct rxq *)dpdk_rxq;
2404 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
2405 const unsigned int elts_n = rxq->elts_n;
2406 unsigned int elts_head = rxq->elts_head;
2407 struct ibv_recv_wr head;
2408 struct ibv_recv_wr **next = &head.next;
2409 struct ibv_recv_wr *bad_wr;
2411 unsigned int pkts_ret = 0;
2414 if (unlikely(!rxq->sp))
2415 return mlx4_rx_burst(dpdk_rxq, pkts, pkts_n);
2416 if (unlikely(elts == NULL)) /* See RTE_DEV_CMD_SET_MTU. */
2418 for (i = 0; (i != pkts_n); ++i) {
2419 struct rxq_elt_sp *elt = &(*elts)[elts_head];
2420 struct ibv_recv_wr *wr = &elt->wr;
2421 uint64_t wr_id = wr->wr_id;
2423 unsigned int pkt_buf_len;
2424 struct rte_mbuf *pkt_buf = NULL; /* Buffer returned in pkts. */
2425 struct rte_mbuf **pkt_buf_next = &pkt_buf;
2426 unsigned int seg_headroom = RTE_PKTMBUF_HEADROOM;
2429 /* Sanity checks. */
2433 assert(wr_id < rxq->elts_n);
2434 assert(wr->sg_list == elt->sges);
2435 assert(wr->num_sge == elemof(elt->sges));
2436 assert(elts_head < rxq->elts_n);
2437 assert(rxq->elts_head < rxq->elts_n);
2438 ret = rxq->if_cq->poll_length(rxq->cq, NULL, NULL);
2439 if (unlikely(ret < 0)) {
2443 DEBUG("rxq=%p, poll_length() failed (ret=%d)",
2445 /* ibv_poll_cq() must be used in case of failure. */
2446 wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
2447 if (unlikely(wcs_n == 0))
2449 if (unlikely(wcs_n < 0)) {
2450 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
2451 (void *)rxq, wcs_n);
2455 if (unlikely(wc.status != IBV_WC_SUCCESS)) {
2456 /* Whatever, just repost the offending WR. */
2457 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
2458 " completion status (%d): %s",
2459 (void *)rxq, wc.wr_id, wc.status,
2460 ibv_wc_status_str(wc.status));
2461 #ifdef MLX4_PMD_SOFT_COUNTERS
2462 /* Increment dropped packets counter. */
2463 ++rxq->stats.idropped;
2465 /* Link completed WRs together for repost. */
2476 /* Link completed WRs together for repost. */
2480 * Replace spent segments with new ones, concatenate and
2481 * return them as pkt_buf.
2484 struct ibv_sge *sge = &elt->sges[j];
2485 struct rte_mbuf *seg = elt->bufs[j];
2486 struct rte_mbuf *rep;
2487 unsigned int seg_tailroom;
2490 * Fetch initial bytes of packet descriptor into a
2491 * cacheline while allocating rep.
2494 rep = __rte_mbuf_raw_alloc(rxq->mp);
2495 if (unlikely(rep == NULL)) {
2497 * Unable to allocate a replacement mbuf,
2500 DEBUG("rxq=%p, wr_id=%" PRIu64 ":"
2501 " can't allocate a new mbuf",
2502 (void *)rxq, wr_id);
2503 if (pkt_buf != NULL) {
2504 *pkt_buf_next = NULL;
2505 rte_pktmbuf_free(pkt_buf);
2507 /* Increase out of memory counters. */
2508 ++rxq->stats.rx_nombuf;
2509 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
2513 /* Poison user-modifiable fields in rep. */
2514 NEXT(rep) = (void *)((uintptr_t)-1);
2515 SET_DATA_OFF(rep, 0xdead);
2516 DATA_LEN(rep) = 0xd00d;
2517 PKT_LEN(rep) = 0xdeadd00d;
2518 NB_SEGS(rep) = 0x2a;
2522 assert(rep->buf_len == seg->buf_len);
2523 assert(rep->buf_len == rxq->mb_len);
2524 /* Reconfigure sge to use rep instead of seg. */
2525 assert(sge->lkey == rxq->mr->lkey);
2526 sge->addr = ((uintptr_t)rep->buf_addr + seg_headroom);
2529 /* Update pkt_buf if it's the first segment, or link
2530 * seg to the previous one and update pkt_buf_next. */
2531 *pkt_buf_next = seg;
2532 pkt_buf_next = &NEXT(seg);
2533 /* Update seg information. */
2534 seg_tailroom = (seg->buf_len - seg_headroom);
2535 assert(sge->length == seg_tailroom);
2536 SET_DATA_OFF(seg, seg_headroom);
2537 if (likely(len <= seg_tailroom)) {
2539 DATA_LEN(seg) = len;
2542 assert(rte_pktmbuf_headroom(seg) ==
2544 assert(rte_pktmbuf_tailroom(seg) ==
2545 (seg_tailroom - len));
2548 DATA_LEN(seg) = seg_tailroom;
2549 PKT_LEN(seg) = seg_tailroom;
2551 assert(rte_pktmbuf_headroom(seg) == seg_headroom);
2552 assert(rte_pktmbuf_tailroom(seg) == 0);
2553 /* Fix len and clear headroom for next segments. */
2554 len -= seg_tailroom;
2557 /* Update head and tail segments. */
2558 *pkt_buf_next = NULL;
2559 assert(pkt_buf != NULL);
2561 NB_SEGS(pkt_buf) = j;
2562 PORT(pkt_buf) = rxq->port_id;
2563 PKT_LEN(pkt_buf) = pkt_buf_len;
2564 pkt_buf->ol_flags = 0;
2566 /* Return packet. */
2567 *(pkts++) = pkt_buf;
2569 #ifdef MLX4_PMD_SOFT_COUNTERS
2570 /* Increase bytes counter. */
2571 rxq->stats.ibytes += pkt_buf_len;
2574 if (++elts_head >= elts_n)
2578 if (unlikely(i == 0))
2583 DEBUG("%p: reposting %d WRs", (void *)rxq, i);
2585 ret = ibv_post_recv(rxq->qp, head.next, &bad_wr);
2586 if (unlikely(ret)) {
2587 /* Inability to repost WRs is fatal. */
2588 DEBUG("%p: ibv_post_recv(): failed for WR %p: %s",
2594 rxq->elts_head = elts_head;
2595 #ifdef MLX4_PMD_SOFT_COUNTERS
2596 /* Increase packets counter. */
2597 rxq->stats.ipackets += pkts_ret;
2603 * DPDK callback for RX.
2605 * The following function is the same as mlx4_rx_burst_sp(), except it doesn't
2606 * manage scattered packets. Improves performance when MRU is lower than the
2607 * size of the first segment.
2610 * Generic pointer to RX queue structure.
2612 * Array to store received packets.
2614 * Maximum number of packets in array.
2617 * Number of packets successfully received (<= pkts_n).
2620 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2622 struct rxq *rxq = (struct rxq *)dpdk_rxq;
2623 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
2624 const unsigned int elts_n = rxq->elts_n;
2625 unsigned int elts_head = rxq->elts_head;
2626 struct ibv_sge sges[pkts_n];
2628 unsigned int pkts_ret = 0;
2631 if (unlikely(rxq->sp))
2632 return mlx4_rx_burst_sp(dpdk_rxq, pkts, pkts_n);
2633 for (i = 0; (i != pkts_n); ++i) {
2634 struct rxq_elt *elt = &(*elts)[elts_head];
2635 struct ibv_recv_wr *wr = &elt->wr;
2636 uint64_t wr_id = wr->wr_id;
2638 struct rte_mbuf *seg = (void *)((uintptr_t)elt->sge.addr -
2639 WR_ID(wr_id).offset);
2640 struct rte_mbuf *rep;
2642 /* Sanity checks. */
2643 assert(WR_ID(wr_id).id < rxq->elts_n);
2644 assert(wr->sg_list == &elt->sge);
2645 assert(wr->num_sge == 1);
2646 assert(elts_head < rxq->elts_n);
2647 assert(rxq->elts_head < rxq->elts_n);
2648 ret = rxq->if_cq->poll_length(rxq->cq, NULL, NULL);
2649 if (unlikely(ret < 0)) {
2653 DEBUG("rxq=%p, poll_length() failed (ret=%d)",
2655 /* ibv_poll_cq() must be used in case of failure. */
2656 wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
2657 if (unlikely(wcs_n == 0))
2659 if (unlikely(wcs_n < 0)) {
2660 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
2661 (void *)rxq, wcs_n);
2665 if (unlikely(wc.status != IBV_WC_SUCCESS)) {
2666 /* Whatever, just repost the offending WR. */
2667 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
2668 " completion status (%d): %s",
2669 (void *)rxq, wc.wr_id, wc.status,
2670 ibv_wc_status_str(wc.status));
2671 #ifdef MLX4_PMD_SOFT_COUNTERS
2672 /* Increment dropped packets counter. */
2673 ++rxq->stats.idropped;
2675 /* Add SGE to array for repost. */
2685 * Fetch initial bytes of packet descriptor into a
2686 * cacheline while allocating rep.
2689 rep = __rte_mbuf_raw_alloc(rxq->mp);
2690 if (unlikely(rep == NULL)) {
2692 * Unable to allocate a replacement mbuf,
2695 DEBUG("rxq=%p, wr_id=%" PRIu32 ":"
2696 " can't allocate a new mbuf",
2697 (void *)rxq, WR_ID(wr_id).id);
2698 /* Increase out of memory counters. */
2699 ++rxq->stats.rx_nombuf;
2700 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
2704 /* Reconfigure sge to use rep instead of seg. */
2705 elt->sge.addr = (uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM;
2706 assert(elt->sge.lkey == rxq->mr->lkey);
2707 WR_ID(wr->wr_id).offset =
2708 (((uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM) -
2710 assert(WR_ID(wr->wr_id).id == WR_ID(wr_id).id);
2712 /* Add SGE to array for repost. */
2715 /* Update seg information. */
2716 SET_DATA_OFF(seg, RTE_PKTMBUF_HEADROOM);
2718 PORT(seg) = rxq->port_id;
2721 DATA_LEN(seg) = len;
2724 /* Return packet. */
2727 #ifdef MLX4_PMD_SOFT_COUNTERS
2728 /* Increase bytes counter. */
2729 rxq->stats.ibytes += len;
2732 if (++elts_head >= elts_n)
2736 if (unlikely(i == 0))
2740 DEBUG("%p: reposting %u WRs", (void *)rxq, i);
2742 ret = rxq->if_qp->recv_burst(rxq->qp, sges, i);
2743 if (unlikely(ret)) {
2744 /* Inability to repost WRs is fatal. */
2745 DEBUG("%p: recv_burst(): failed (ret=%d)",
2750 rxq->elts_head = elts_head;
2751 #ifdef MLX4_PMD_SOFT_COUNTERS
2752 /* Increase packets counter. */
2753 rxq->stats.ipackets += pkts_ret;
2759 * Allocate a Queue Pair.
2760 * Optionally setup inline receive if supported.
2763 * Pointer to private structure.
2765 * Completion queue to associate with QP.
2767 * Number of descriptors in QP (hint only).
2770 * QP pointer or NULL in case of error.
2772 static struct ibv_qp *
2773 rxq_setup_qp(struct priv *priv, struct ibv_cq *cq, uint16_t desc)
2775 struct ibv_exp_qp_init_attr attr = {
2776 /* CQ to be associated with the send queue. */
2778 /* CQ to be associated with the receive queue. */
2781 /* Max number of outstanding WRs. */
2782 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
2783 priv->device_attr.max_qp_wr :
2785 /* Max number of scatter/gather elements in a WR. */
2786 .max_recv_sge = ((priv->device_attr.max_sge <
2787 MLX4_PMD_SGE_WR_N) ?
2788 priv->device_attr.max_sge :
2791 .qp_type = IBV_QPT_RAW_PACKET,
2792 .comp_mask = IBV_EXP_QP_INIT_ATTR_PD,
2797 attr.max_inl_recv = priv->inl_recv_size;
2798 attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
2800 return ibv_exp_create_qp(priv->ctx, &attr);
2806 * Allocate a RSS Queue Pair.
2807 * Optionally setup inline receive if supported.
2810 * Pointer to private structure.
2812 * Completion queue to associate with QP.
2814 * Number of descriptors in QP (hint only).
2816 * If nonzero, create a parent QP, otherwise a child.
2819 * QP pointer or NULL in case of error.
2821 static struct ibv_qp *
2822 rxq_setup_qp_rss(struct priv *priv, struct ibv_cq *cq, uint16_t desc,
2825 struct ibv_exp_qp_init_attr attr = {
2826 /* CQ to be associated with the send queue. */
2828 /* CQ to be associated with the receive queue. */
2831 /* Max number of outstanding WRs. */
2832 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
2833 priv->device_attr.max_qp_wr :
2835 /* Max number of scatter/gather elements in a WR. */
2836 .max_recv_sge = ((priv->device_attr.max_sge <
2837 MLX4_PMD_SGE_WR_N) ?
2838 priv->device_attr.max_sge :
2841 .qp_type = IBV_QPT_RAW_PACKET,
2842 .comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
2843 IBV_EXP_QP_INIT_ATTR_QPG),
2848 attr.max_inl_recv = priv->inl_recv_size,
2849 attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
2852 attr.qpg.qpg_type = IBV_EXP_QPG_PARENT;
2853 /* TSS isn't necessary. */
2854 attr.qpg.parent_attrib.tss_child_count = 0;
2855 attr.qpg.parent_attrib.rss_child_count = priv->rxqs_n;
2856 DEBUG("initializing parent RSS queue");
2858 attr.qpg.qpg_type = IBV_EXP_QPG_CHILD_RX;
2859 attr.qpg.qpg_parent = priv->rxq_parent.qp;
2860 DEBUG("initializing child RSS queue");
2862 return ibv_exp_create_qp(priv->ctx, &attr);
2865 #endif /* RSS_SUPPORT */
2868 * Reconfigure a RX queue with new parameters.
2870 * rxq_rehash() does not allocate mbufs, which, if not done from the right
2871 * thread (such as a control thread), may corrupt the pool.
2872 * In case of failure, the queue is left untouched.
2875 * Pointer to Ethernet device structure.
2880 * 0 on success, errno value on failure.
2883 rxq_rehash(struct rte_eth_dev *dev, struct rxq *rxq)
2885 struct priv *priv = rxq->priv;
2886 struct rxq tmpl = *rxq;
2887 unsigned int mbuf_n;
2888 unsigned int desc_n;
2889 struct rte_mbuf **pool;
2891 struct ibv_exp_qp_attr mod;
2892 struct ibv_recv_wr *bad_wr;
2894 int parent = (rxq == &priv->rxq_parent);
2897 ERROR("%p: cannot rehash parent queue %p",
2898 (void *)dev, (void *)rxq);
2901 DEBUG("%p: rehashing queue %p", (void *)dev, (void *)rxq);
2902 /* Number of descriptors and mbufs currently allocated. */
2903 desc_n = (tmpl.elts_n * (tmpl.sp ? MLX4_PMD_SGE_WR_N : 1));
2905 /* Enable scattered packets support for this queue if necessary. */
2906 if ((dev->data->dev_conf.rxmode.jumbo_frame) &&
2907 (dev->data->dev_conf.rxmode.max_rx_pkt_len >
2908 (tmpl.mb_len - RTE_PKTMBUF_HEADROOM))) {
2910 desc_n /= MLX4_PMD_SGE_WR_N;
2913 DEBUG("%p: %s scattered packets support (%u WRs)",
2914 (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc_n);
2915 /* If scatter mode is the same as before, nothing to do. */
2916 if (tmpl.sp == rxq->sp) {
2917 DEBUG("%p: nothing to do", (void *)dev);
2920 /* Remove attached flows if RSS is disabled (no parent queue). */
2922 rxq_allmulticast_disable(&tmpl);
2923 rxq_promiscuous_disable(&tmpl);
2924 rxq_mac_addrs_del(&tmpl);
2925 /* Update original queue in case of failure. */
2926 rxq->allmulti_flow = tmpl.allmulti_flow;
2927 rxq->promisc_flow = tmpl.promisc_flow;
2928 memcpy(rxq->mac_configured, tmpl.mac_configured,
2929 sizeof(rxq->mac_configured));
2930 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
2932 /* From now on, any failure will render the queue unusable.
2933 * Reinitialize QP. */
2934 mod = (struct ibv_exp_qp_attr){ .qp_state = IBV_QPS_RESET };
2935 err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
2937 ERROR("%p: cannot reset QP: %s", (void *)dev, strerror(err));
2941 err = ibv_resize_cq(tmpl.cq, desc_n);
2943 ERROR("%p: cannot resize CQ: %s", (void *)dev, strerror(err));
2947 mod = (struct ibv_exp_qp_attr){
2948 /* Move the QP to this state. */
2949 .qp_state = IBV_QPS_INIT,
2950 /* Primary port number. */
2951 .port_num = priv->port
2953 err = ibv_exp_modify_qp(tmpl.qp, &mod,
2956 (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
2957 #endif /* RSS_SUPPORT */
2960 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
2961 (void *)dev, strerror(err));
2965 /* Reconfigure flows. Do not care for errors. */
2967 rxq_mac_addrs_add(&tmpl);
2969 rxq_promiscuous_enable(&tmpl);
2971 rxq_allmulticast_enable(&tmpl);
2972 /* Update original queue in case of failure. */
2973 rxq->allmulti_flow = tmpl.allmulti_flow;
2974 rxq->promisc_flow = tmpl.promisc_flow;
2975 memcpy(rxq->mac_configured, tmpl.mac_configured,
2976 sizeof(rxq->mac_configured));
2977 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
2979 /* Allocate pool. */
2980 pool = rte_malloc(__func__, (mbuf_n * sizeof(*pool)), 0);
2982 ERROR("%p: cannot allocate memory", (void *)dev);
2985 /* Snatch mbufs from original queue. */
2988 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
2990 for (i = 0; (i != elemof(*elts)); ++i) {
2991 struct rxq_elt_sp *elt = &(*elts)[i];
2994 for (j = 0; (j != elemof(elt->bufs)); ++j) {
2995 assert(elt->bufs[j] != NULL);
2996 pool[k++] = elt->bufs[j];
3000 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
3002 for (i = 0; (i != elemof(*elts)); ++i) {
3003 struct rxq_elt *elt = &(*elts)[i];
3004 struct rte_mbuf *buf = (void *)
3005 ((uintptr_t)elt->sge.addr -
3006 WR_ID(elt->wr.wr_id).offset);
3008 assert(WR_ID(elt->wr.wr_id).id == i);
3012 assert(k == mbuf_n);
3014 tmpl.elts.sp = NULL;
3015 assert((void *)&tmpl.elts.sp == (void *)&tmpl.elts.no_sp);
3017 rxq_alloc_elts_sp(&tmpl, desc_n, pool) :
3018 rxq_alloc_elts(&tmpl, desc_n, pool));
3020 ERROR("%p: cannot reallocate WRs, aborting", (void *)dev);
3025 assert(tmpl.elts_n == desc_n);
3026 assert(tmpl.elts.sp != NULL);
3028 /* Clean up original data. */
3030 rte_free(rxq->elts.sp);
3031 rxq->elts.sp = NULL;
3033 err = ibv_post_recv(tmpl.qp,
3035 &(*tmpl.elts.sp)[0].wr :
3036 &(*tmpl.elts.no_sp)[0].wr),
3039 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
3045 mod = (struct ibv_exp_qp_attr){
3046 .qp_state = IBV_QPS_RTR
3048 err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3050 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
3051 (void *)dev, strerror(err));
3059 * Configure a RX queue.
3062 * Pointer to Ethernet device structure.
3064 * Pointer to RX queue structure.
3066 * Number of descriptors to configure in queue.
3068 * NUMA socket on which memory must be allocated.
3070 * Thresholds parameters.
3072 * Memory pool for buffer allocations.
3075 * 0 on success, errno value on failure.
3078 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
3079 unsigned int socket, const struct rte_eth_rxconf *conf,
3080 struct rte_mempool *mp)
3082 struct priv *priv = dev->data->dev_private;
3088 struct ibv_exp_qp_attr mod;
3090 struct ibv_exp_query_intf_params params;
3092 enum ibv_exp_query_intf_status status;
3093 struct ibv_recv_wr *bad_wr;
3094 struct rte_mbuf *buf;
3096 int parent = (rxq == &priv->rxq_parent);
3098 (void)conf; /* Thresholds configuration (ignored). */
3100 * If this is a parent queue, hardware must support RSS and
3101 * RSS must be enabled.
3103 assert((!parent) || ((priv->hw_rss) && (priv->rss)));
3105 /* Even if unused, ibv_create_cq() requires at least one
3110 if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
3111 ERROR("%p: invalid number of RX descriptors (must be a"
3112 " multiple of %d)", (void *)dev, MLX4_PMD_SGE_WR_N);
3115 /* Get mbuf length. */
3116 buf = rte_pktmbuf_alloc(mp);
3118 ERROR("%p: unable to allocate mbuf", (void *)dev);
3121 tmpl.mb_len = buf->buf_len;
3122 assert((rte_pktmbuf_headroom(buf) +
3123 rte_pktmbuf_tailroom(buf)) == tmpl.mb_len);
3124 assert(rte_pktmbuf_headroom(buf) == RTE_PKTMBUF_HEADROOM);
3125 rte_pktmbuf_free(buf);
3126 /* Enable scattered packets support for this queue if necessary. */
3127 if ((dev->data->dev_conf.rxmode.jumbo_frame) &&
3128 (dev->data->dev_conf.rxmode.max_rx_pkt_len >
3129 (tmpl.mb_len - RTE_PKTMBUF_HEADROOM))) {
3131 desc /= MLX4_PMD_SGE_WR_N;
3133 DEBUG("%p: %s scattered packets support (%u WRs)",
3134 (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc);
3135 /* Use the entire RX mempool as the memory region. */
3136 tmpl.mr = ibv_reg_mr(priv->pd,
3137 (void *)mp->elt_va_start,
3138 (mp->elt_va_end - mp->elt_va_start),
3139 (IBV_ACCESS_LOCAL_WRITE |
3140 IBV_ACCESS_REMOTE_WRITE));
3141 if (tmpl.mr == NULL) {
3143 ERROR("%p: MR creation failure: %s",
3144 (void *)dev, strerror(ret));
3148 tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
3149 if (tmpl.cq == NULL) {
3151 ERROR("%p: CQ creation failure: %s",
3152 (void *)dev, strerror(ret));
3155 DEBUG("priv->device_attr.max_qp_wr is %d",
3156 priv->device_attr.max_qp_wr);
3157 DEBUG("priv->device_attr.max_sge is %d",
3158 priv->device_attr.max_sge);
3161 tmpl.qp = rxq_setup_qp_rss(priv, tmpl.cq, desc, parent);
3163 #endif /* RSS_SUPPORT */
3164 tmpl.qp = rxq_setup_qp(priv, tmpl.cq, desc);
3165 if (tmpl.qp == NULL) {
3166 ret = (errno ? errno : EINVAL);
3167 ERROR("%p: QP creation failure: %s",
3168 (void *)dev, strerror(ret));
3171 mod = (struct ibv_exp_qp_attr){
3172 /* Move the QP to this state. */
3173 .qp_state = IBV_QPS_INIT,
3174 /* Primary port number. */
3175 .port_num = priv->port
3177 ret = ibv_exp_modify_qp(tmpl.qp, &mod,
3180 (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
3181 #endif /* RSS_SUPPORT */
3184 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
3185 (void *)dev, strerror(ret));
3188 if ((parent) || (!priv->rss)) {
3189 /* Configure MAC and broadcast addresses. */
3190 ret = rxq_mac_addrs_add(&tmpl);
3192 ERROR("%p: QP flow attachment failed: %s",
3193 (void *)dev, strerror(ret));
3197 /* Allocate descriptors for RX queues, except for the RSS parent. */
3201 ret = rxq_alloc_elts_sp(&tmpl, desc, NULL);
3203 ret = rxq_alloc_elts(&tmpl, desc, NULL);
3205 ERROR("%p: RXQ allocation failed: %s",
3206 (void *)dev, strerror(ret));
3209 ret = ibv_post_recv(tmpl.qp,
3211 &(*tmpl.elts.sp)[0].wr :
3212 &(*tmpl.elts.no_sp)[0].wr),
3215 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
3222 mod = (struct ibv_exp_qp_attr){
3223 .qp_state = IBV_QPS_RTR
3225 ret = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3227 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
3228 (void *)dev, strerror(ret));
3232 tmpl.port_id = dev->data->port_id;
3233 DEBUG("%p: RTE port ID: %u", (void *)rxq, tmpl.port_id);
3234 attr.params = (struct ibv_exp_query_intf_params){
3235 .intf_scope = IBV_EXP_INTF_GLOBAL,
3236 .intf = IBV_EXP_INTF_CQ,
3239 tmpl.if_cq = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
3240 if (tmpl.if_cq == NULL) {
3241 ERROR("%p: CQ interface family query failed with status %d",
3242 (void *)dev, status);
3245 attr.params = (struct ibv_exp_query_intf_params){
3246 .intf_scope = IBV_EXP_INTF_GLOBAL,
3247 .intf = IBV_EXP_INTF_QP_BURST,
3250 tmpl.if_qp = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
3251 if (tmpl.if_qp == NULL) {
3252 ERROR("%p: QP interface family query failed with status %d",
3253 (void *)dev, status);
3256 /* Clean up rxq in case we're reinitializing it. */
3257 DEBUG("%p: cleaning-up old rxq just in case", (void *)rxq);
3260 DEBUG("%p: rxq updated with %p", (void *)rxq, (void *)&tmpl);
3270 * DPDK callback to configure a RX queue.
3273 * Pointer to Ethernet device structure.
3277 * Number of descriptors to configure in queue.
3279 * NUMA socket on which memory must be allocated.
3281 * Thresholds parameters.
3283 * Memory pool for buffer allocations.
3286 * 0 on success, negative errno value on failure.
3289 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
3290 unsigned int socket, const struct rte_eth_rxconf *conf,
3291 struct rte_mempool *mp)
3293 struct priv *priv = dev->data->dev_private;
3294 struct rxq *rxq = (*priv->rxqs)[idx];
3298 DEBUG("%p: configuring queue %u for %u descriptors",
3299 (void *)dev, idx, desc);
3300 if (idx >= priv->rxqs_n) {
3301 ERROR("%p: queue index out of range (%u >= %u)",
3302 (void *)dev, idx, priv->rxqs_n);
3307 DEBUG("%p: reusing already allocated queue index %u (%p)",
3308 (void *)dev, idx, (void *)rxq);
3309 if (priv->started) {
3313 (*priv->rxqs)[idx] = NULL;
3316 rxq = rte_calloc_socket("RXQ", 1, sizeof(*rxq), 0, socket);
3318 ERROR("%p: unable to allocate queue index %u",
3324 ret = rxq_setup(dev, rxq, desc, socket, conf, mp);
3328 rxq->stats.idx = idx;
3329 DEBUG("%p: adding RX queue %p to list",
3330 (void *)dev, (void *)rxq);
3331 (*priv->rxqs)[idx] = rxq;
3332 /* Update receive callback. */
3334 dev->rx_pkt_burst = mlx4_rx_burst_sp;
3336 dev->rx_pkt_burst = mlx4_rx_burst;
3343 * DPDK callback to release a RX queue.
3346 * Generic RX queue pointer.
3349 mlx4_rx_queue_release(void *dpdk_rxq)
3351 struct rxq *rxq = (struct rxq *)dpdk_rxq;
3359 assert(rxq != &priv->rxq_parent);
3360 for (i = 0; (i != priv->rxqs_n); ++i)
3361 if ((*priv->rxqs)[i] == rxq) {
3362 DEBUG("%p: removing RX queue %p from list",
3363 (void *)priv->dev, (void *)rxq);
3364 (*priv->rxqs)[i] = NULL;
3373 * DPDK callback to start the device.
3375 * Simulate device start by attaching all configured flows.
3378 * Pointer to Ethernet device structure.
3381 * 0 on success, negative errno value on failure.
3384 mlx4_dev_start(struct rte_eth_dev *dev)
3386 struct priv *priv = dev->data->dev_private;
3392 if (priv->started) {
3396 DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
3399 rxq = &priv->rxq_parent;
3402 rxq = (*priv->rxqs)[0];
3405 /* Iterate only once when RSS is enabled. */
3409 /* Ignore nonexistent RX queues. */
3412 ret = rxq_mac_addrs_add(rxq);
3413 if (!ret && priv->promisc)
3414 ret = rxq_promiscuous_enable(rxq);
3415 if (!ret && priv->allmulti)
3416 ret = rxq_allmulticast_enable(rxq);
3419 WARN("%p: QP flow attachment failed: %s",
3420 (void *)dev, strerror(ret));
3423 rxq = (*priv->rxqs)[--i];
3425 rxq_allmulticast_disable(rxq);
3426 rxq_promiscuous_disable(rxq);
3427 rxq_mac_addrs_del(rxq);
3432 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
3438 * DPDK callback to stop the device.
3440 * Simulate device stop by detaching all configured flows.
3443 * Pointer to Ethernet device structure.
3446 mlx4_dev_stop(struct rte_eth_dev *dev)
3448 struct priv *priv = dev->data->dev_private;
3454 if (!priv->started) {
3458 DEBUG("%p: detaching flows from all RX queues", (void *)dev);
3461 rxq = &priv->rxq_parent;
3464 rxq = (*priv->rxqs)[0];
3467 /* Iterate only once when RSS is enabled. */
3469 /* Ignore nonexistent RX queues. */
3472 rxq_allmulticast_disable(rxq);
3473 rxq_promiscuous_disable(rxq);
3474 rxq_mac_addrs_del(rxq);
3475 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
3480 * Dummy DPDK callback for TX.
3482 * This function is used to temporarily replace the real callback during
3483 * unsafe control operations on the queue, or in case of error.
3486 * Generic pointer to TX queue structure.
3488 * Packets to transmit.
3490 * Number of packets in array.
3493 * Number of packets successfully transmitted (<= pkts_n).
3496 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
3505 * Dummy DPDK callback for RX.
3507 * This function is used to temporarily replace the real callback during
3508 * unsafe control operations on the queue, or in case of error.
3511 * Generic pointer to RX queue structure.
3513 * Array to store received packets.
3515 * Maximum number of packets in array.
3518 * Number of packets successfully received (<= pkts_n).
3521 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
3530 * DPDK callback to close the device.
3532 * Destroy all queues and objects, free memory.
3535 * Pointer to Ethernet device structure.
3538 mlx4_dev_close(struct rte_eth_dev *dev)
3540 struct priv *priv = dev->data->dev_private;
3545 DEBUG("%p: closing device \"%s\"",
3547 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
3548 /* Prevent crashes when queues are still in use. This is unfortunately
3549 * still required for DPDK 1.3 because some programs (such as testpmd)
3550 * never release them before closing the device. */
3551 dev->rx_pkt_burst = removed_rx_burst;
3552 dev->tx_pkt_burst = removed_tx_burst;
3553 if (priv->rxqs != NULL) {
3554 /* XXX race condition if mlx4_rx_burst() is still running. */
3556 for (i = 0; (i != priv->rxqs_n); ++i) {
3557 tmp = (*priv->rxqs)[i];
3560 (*priv->rxqs)[i] = NULL;
3567 if (priv->txqs != NULL) {
3568 /* XXX race condition if mlx4_tx_burst() is still running. */
3570 for (i = 0; (i != priv->txqs_n); ++i) {
3571 tmp = (*priv->txqs)[i];
3574 (*priv->txqs)[i] = NULL;
3582 rxq_cleanup(&priv->rxq_parent);
3583 if (priv->pd != NULL) {
3584 assert(priv->ctx != NULL);
3585 claim_zero(ibv_dealloc_pd(priv->pd));
3586 claim_zero(ibv_close_device(priv->ctx));
3588 assert(priv->ctx == NULL);
3590 memset(priv, 0, sizeof(*priv));
3594 * DPDK callback to get information about the device.
3597 * Pointer to Ethernet device structure.
3599 * Info structure output buffer.
3602 mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
3604 struct priv *priv = dev->data->dev_private;
3608 /* FIXME: we should ask the device for these values. */
3609 info->min_rx_bufsize = 32;
3610 info->max_rx_pktlen = 65536;
3612 * Since we need one CQ per QP, the limit is the minimum number
3613 * between the two values.
3615 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
3616 priv->device_attr.max_qp : priv->device_attr.max_cq);
3617 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
3620 info->max_rx_queues = max;
3621 info->max_tx_queues = max;
3622 info->max_mac_addrs = elemof(priv->mac);
3627 * DPDK callback to get device statistics.
3630 * Pointer to Ethernet device structure.
3632 * Stats structure output buffer.
3635 mlx4_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
3637 struct priv *priv = dev->data->dev_private;
3638 struct rte_eth_stats tmp = {0};
3643 /* Add software counters. */
3644 for (i = 0; (i != priv->rxqs_n); ++i) {
3645 struct rxq *rxq = (*priv->rxqs)[i];
3649 idx = rxq->stats.idx;
3650 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
3651 #ifdef MLX4_PMD_SOFT_COUNTERS
3652 tmp.q_ipackets[idx] += rxq->stats.ipackets;
3653 tmp.q_ibytes[idx] += rxq->stats.ibytes;
3655 tmp.q_errors[idx] += (rxq->stats.idropped +
3656 rxq->stats.rx_nombuf);
3658 #ifdef MLX4_PMD_SOFT_COUNTERS
3659 tmp.ipackets += rxq->stats.ipackets;
3660 tmp.ibytes += rxq->stats.ibytes;
3662 tmp.ierrors += rxq->stats.idropped;
3663 tmp.rx_nombuf += rxq->stats.rx_nombuf;
3665 for (i = 0; (i != priv->txqs_n); ++i) {
3666 struct txq *txq = (*priv->txqs)[i];
3670 idx = txq->stats.idx;
3671 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
3672 #ifdef MLX4_PMD_SOFT_COUNTERS
3673 tmp.q_opackets[idx] += txq->stats.opackets;
3674 tmp.q_obytes[idx] += txq->stats.obytes;
3676 tmp.q_errors[idx] += txq->stats.odropped;
3678 #ifdef MLX4_PMD_SOFT_COUNTERS
3679 tmp.opackets += txq->stats.opackets;
3680 tmp.obytes += txq->stats.obytes;
3682 tmp.oerrors += txq->stats.odropped;
3684 #ifndef MLX4_PMD_SOFT_COUNTERS
3685 /* FIXME: retrieve and add hardware counters. */
3692 * DPDK callback to clear device statistics.
3695 * Pointer to Ethernet device structure.
3698 mlx4_stats_reset(struct rte_eth_dev *dev)
3700 struct priv *priv = dev->data->dev_private;
3705 for (i = 0; (i != priv->rxqs_n); ++i) {
3706 if ((*priv->rxqs)[i] == NULL)
3708 idx = (*priv->rxqs)[i]->stats.idx;
3709 (*priv->rxqs)[i]->stats =
3710 (struct mlx4_rxq_stats){ .idx = idx };
3712 for (i = 0; (i != priv->txqs_n); ++i) {
3713 if ((*priv->txqs)[i] == NULL)
3715 idx = (*priv->rxqs)[i]->stats.idx;
3716 (*priv->txqs)[i]->stats =
3717 (struct mlx4_txq_stats){ .idx = idx };
3719 #ifndef MLX4_PMD_SOFT_COUNTERS
3720 /* FIXME: reset hardware counters. */
3726 * DPDK callback to remove a MAC address.
3729 * Pointer to Ethernet device structure.
3731 * MAC address index.
3734 mlx4_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
3736 struct priv *priv = dev->data->dev_private;
3739 DEBUG("%p: removing MAC address from index %" PRIu32,
3740 (void *)dev, index);
3741 if (index >= MLX4_MAX_MAC_ADDRESSES)
3743 /* Refuse to remove the broadcast address, this one is special. */
3744 if (!memcmp(priv->mac[index].addr_bytes, "\xff\xff\xff\xff\xff\xff",
3747 priv_mac_addr_del(priv, index);
3753 * DPDK callback to add a MAC address.
3756 * Pointer to Ethernet device structure.
3758 * MAC address to register.
3760 * MAC address index.
3762 * VMDq pool index to associate address with (ignored).
3765 mlx4_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
3766 uint32_t index, uint32_t vmdq)
3768 struct priv *priv = dev->data->dev_private;
3772 DEBUG("%p: adding MAC address at index %" PRIu32,
3773 (void *)dev, index);
3774 if (index >= MLX4_MAX_MAC_ADDRESSES)
3776 /* Refuse to add the broadcast address, this one is special. */
3777 if (!memcmp(mac_addr->addr_bytes, "\xff\xff\xff\xff\xff\xff",
3780 priv_mac_addr_add(priv, index,
3781 (const uint8_t (*)[ETHER_ADDR_LEN])
3782 mac_addr->addr_bytes);
3788 * DPDK callback to enable promiscuous mode.
3791 * Pointer to Ethernet device structure.
3794 mlx4_promiscuous_enable(struct rte_eth_dev *dev)
3796 struct priv *priv = dev->data->dev_private;
3801 if (priv->promisc) {
3805 /* If device isn't started, this is all we need to do. */
3809 ret = rxq_promiscuous_enable(&priv->rxq_parent);
3816 for (i = 0; (i != priv->rxqs_n); ++i) {
3817 if ((*priv->rxqs)[i] == NULL)
3819 ret = rxq_promiscuous_enable((*priv->rxqs)[i]);
3822 /* Failure, rollback. */
3824 if ((*priv->rxqs)[--i] != NULL)
3825 rxq_promiscuous_disable((*priv->rxqs)[i]);
3835 * DPDK callback to disable promiscuous mode.
3838 * Pointer to Ethernet device structure.
3841 mlx4_promiscuous_disable(struct rte_eth_dev *dev)
3843 struct priv *priv = dev->data->dev_private;
3847 if (!priv->promisc) {
3852 rxq_promiscuous_disable(&priv->rxq_parent);
3855 for (i = 0; (i != priv->rxqs_n); ++i)
3856 if ((*priv->rxqs)[i] != NULL)
3857 rxq_promiscuous_disable((*priv->rxqs)[i]);
3864 * DPDK callback to enable allmulti mode.
3867 * Pointer to Ethernet device structure.
3870 mlx4_allmulticast_enable(struct rte_eth_dev *dev)
3872 struct priv *priv = dev->data->dev_private;
3877 if (priv->allmulti) {
3881 /* If device isn't started, this is all we need to do. */
3885 ret = rxq_allmulticast_enable(&priv->rxq_parent);
3892 for (i = 0; (i != priv->rxqs_n); ++i) {
3893 if ((*priv->rxqs)[i] == NULL)
3895 ret = rxq_allmulticast_enable((*priv->rxqs)[i]);
3898 /* Failure, rollback. */
3900 if ((*priv->rxqs)[--i] != NULL)
3901 rxq_allmulticast_disable((*priv->rxqs)[i]);
3911 * DPDK callback to disable allmulti mode.
3914 * Pointer to Ethernet device structure.
3917 mlx4_allmulticast_disable(struct rte_eth_dev *dev)
3919 struct priv *priv = dev->data->dev_private;
3923 if (!priv->allmulti) {
3928 rxq_allmulticast_disable(&priv->rxq_parent);
3931 for (i = 0; (i != priv->rxqs_n); ++i)
3932 if ((*priv->rxqs)[i] != NULL)
3933 rxq_allmulticast_disable((*priv->rxqs)[i]);
3940 * DPDK callback to retrieve physical link information (unlocked version).
3943 * Pointer to Ethernet device structure.
3944 * @param wait_to_complete
3945 * Wait for request completion (ignored).
3948 mlx4_link_update_unlocked(struct rte_eth_dev *dev, int wait_to_complete)
3950 struct priv *priv = dev->data->dev_private;
3951 struct ethtool_cmd edata = {
3955 struct rte_eth_link dev_link;
3958 (void)wait_to_complete;
3959 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
3960 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
3963 memset(&dev_link, 0, sizeof(dev_link));
3964 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
3965 (ifr.ifr_flags & IFF_RUNNING));
3966 ifr.ifr_data = &edata;
3967 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
3968 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
3972 link_speed = ethtool_cmd_speed(&edata);
3973 if (link_speed == -1)
3974 dev_link.link_speed = 0;
3976 dev_link.link_speed = link_speed;
3977 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
3978 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
3979 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
3980 /* Link status changed. */
3981 dev->data->dev_link = dev_link;
3984 /* Link status is still the same. */
3989 * DPDK callback to retrieve physical link information.
3992 * Pointer to Ethernet device structure.
3993 * @param wait_to_complete
3994 * Wait for request completion (ignored).
3997 mlx4_link_update(struct rte_eth_dev *dev, int wait_to_complete)
3999 struct priv *priv = dev->data->dev_private;
4003 ret = mlx4_link_update_unlocked(dev, wait_to_complete);
4009 * DPDK callback to change the MTU.
4011 * Setting the MTU affects hardware MRU (packets larger than the MTU cannot be
4012 * received). Use this as a hint to enable/disable scattered packets support
4013 * and improve performance when not needed.
4014 * Since failure is not an option, reconfiguring queues on the fly is not
4018 * Pointer to Ethernet device structure.
4023 * 0 on success, negative errno value on failure.
4026 mlx4_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
4028 struct priv *priv = dev->data->dev_private;
4031 uint16_t (*rx_func)(void *, struct rte_mbuf **, uint16_t) =
4035 /* Set kernel interface MTU first. */
4036 if (priv_set_mtu(priv, mtu)) {
4038 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
4042 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
4044 /* Temporarily replace RX handler with a fake one, assuming it has not
4045 * been copied elsewhere. */
4046 dev->rx_pkt_burst = removed_rx_burst;
4047 /* Make sure everyone has left mlx4_rx_burst() and uses
4048 * removed_rx_burst() instead. */
4051 /* Reconfigure each RX queue. */
4052 for (i = 0; (i != priv->rxqs_n); ++i) {
4053 struct rxq *rxq = (*priv->rxqs)[i];
4054 unsigned int max_frame_len;
4059 /* Calculate new maximum frame length according to MTU and
4060 * toggle scattered support (sp) if necessary. */
4061 max_frame_len = (priv->mtu + ETHER_HDR_LEN +
4062 (ETHER_MAX_VLAN_FRAME_LEN - ETHER_MAX_LEN));
4063 sp = (max_frame_len > (rxq->mb_len - RTE_PKTMBUF_HEADROOM));
4064 /* Provide new values to rxq_setup(). */
4065 dev->data->dev_conf.rxmode.jumbo_frame = sp;
4066 dev->data->dev_conf.rxmode.max_rx_pkt_len = max_frame_len;
4067 ret = rxq_rehash(dev, rxq);
4069 /* Force SP RX if that queue requires it and abort. */
4071 rx_func = mlx4_rx_burst_sp;
4074 /* Reenable non-RSS queue attributes. No need to check
4075 * for errors at this stage. */
4077 rxq_mac_addrs_add(rxq);
4079 rxq_promiscuous_enable(rxq);
4081 rxq_allmulticast_enable(rxq);
4083 /* Scattered burst function takes priority. */
4085 rx_func = mlx4_rx_burst_sp;
4087 /* Burst functions can now be called again. */
4089 dev->rx_pkt_burst = rx_func;
4097 * DPDK callback to get flow control status.
4100 * Pointer to Ethernet device structure.
4101 * @param[out] fc_conf
4102 * Flow control output buffer.
4105 * 0 on success, negative errno value on failure.
4108 mlx4_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4110 struct priv *priv = dev->data->dev_private;
4112 struct ethtool_pauseparam ethpause = {
4113 .cmd = ETHTOOL_GPAUSEPARAM
4117 ifr.ifr_data = ðpause;
4119 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4121 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
4127 fc_conf->autoneg = ethpause.autoneg;
4128 if (ethpause.rx_pause && ethpause.tx_pause)
4129 fc_conf->mode = RTE_FC_FULL;
4130 else if (ethpause.rx_pause)
4131 fc_conf->mode = RTE_FC_RX_PAUSE;
4132 else if (ethpause.tx_pause)
4133 fc_conf->mode = RTE_FC_TX_PAUSE;
4135 fc_conf->mode = RTE_FC_NONE;
4145 * DPDK callback to modify flow control parameters.
4148 * Pointer to Ethernet device structure.
4149 * @param[in] fc_conf
4150 * Flow control parameters.
4153 * 0 on success, negative errno value on failure.
4156 mlx4_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4158 struct priv *priv = dev->data->dev_private;
4160 struct ethtool_pauseparam ethpause = {
4161 .cmd = ETHTOOL_SPAUSEPARAM
4165 ifr.ifr_data = ðpause;
4166 ethpause.autoneg = fc_conf->autoneg;
4167 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4168 (fc_conf->mode & RTE_FC_RX_PAUSE))
4169 ethpause.rx_pause = 1;
4171 ethpause.rx_pause = 0;
4173 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4174 (fc_conf->mode & RTE_FC_TX_PAUSE))
4175 ethpause.tx_pause = 1;
4177 ethpause.tx_pause = 0;
4180 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4182 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
4196 * Configure a VLAN filter.
4199 * Pointer to Ethernet device structure.
4201 * VLAN ID to filter.
4206 * 0 on success, errno value on failure.
4209 vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
4211 struct priv *priv = dev->data->dev_private;
4213 unsigned int j = -1;
4215 DEBUG("%p: %s VLAN filter ID %" PRIu16,
4216 (void *)dev, (on ? "enable" : "disable"), vlan_id);
4217 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
4218 if (!priv->vlan_filter[i].enabled) {
4219 /* Unused index, remember it. */
4223 if (priv->vlan_filter[i].id != vlan_id)
4225 /* This VLAN ID is already known, use its index. */
4229 /* Check if there's room for another VLAN filter. */
4230 if (j == (unsigned int)-1)
4233 * VLAN filters apply to all configured MAC addresses, flow
4234 * specifications must be reconfigured accordingly.
4236 priv->vlan_filter[j].id = vlan_id;
4237 if ((on) && (!priv->vlan_filter[j].enabled)) {
4239 * Filter is disabled, enable it.
4240 * Rehashing flows in all RX queues is necessary.
4243 rxq_mac_addrs_del(&priv->rxq_parent);
4245 for (i = 0; (i != priv->rxqs_n); ++i)
4246 if ((*priv->rxqs)[i] != NULL)
4247 rxq_mac_addrs_del((*priv->rxqs)[i]);
4248 priv->vlan_filter[j].enabled = 1;
4249 if (priv->started) {
4251 rxq_mac_addrs_add(&priv->rxq_parent);
4253 for (i = 0; (i != priv->rxqs_n); ++i) {
4254 if ((*priv->rxqs)[i] == NULL)
4256 rxq_mac_addrs_add((*priv->rxqs)[i]);
4259 } else if ((!on) && (priv->vlan_filter[j].enabled)) {
4261 * Filter is enabled, disable it.
4262 * Rehashing flows in all RX queues is necessary.
4265 rxq_mac_addrs_del(&priv->rxq_parent);
4267 for (i = 0; (i != priv->rxqs_n); ++i)
4268 if ((*priv->rxqs)[i] != NULL)
4269 rxq_mac_addrs_del((*priv->rxqs)[i]);
4270 priv->vlan_filter[j].enabled = 0;
4271 if (priv->started) {
4273 rxq_mac_addrs_add(&priv->rxq_parent);
4275 for (i = 0; (i != priv->rxqs_n); ++i) {
4276 if ((*priv->rxqs)[i] == NULL)
4278 rxq_mac_addrs_add((*priv->rxqs)[i]);
4286 * DPDK callback to configure a VLAN filter.
4289 * Pointer to Ethernet device structure.
4291 * VLAN ID to filter.
4296 * 0 on success, negative errno value on failure.
4299 mlx4_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
4301 struct priv *priv = dev->data->dev_private;
4305 ret = vlan_filter_set(dev, vlan_id, on);
4311 static const struct eth_dev_ops mlx4_dev_ops = {
4312 .dev_configure = mlx4_dev_configure,
4313 .dev_start = mlx4_dev_start,
4314 .dev_stop = mlx4_dev_stop,
4315 .dev_close = mlx4_dev_close,
4316 .promiscuous_enable = mlx4_promiscuous_enable,
4317 .promiscuous_disable = mlx4_promiscuous_disable,
4318 .allmulticast_enable = mlx4_allmulticast_enable,
4319 .allmulticast_disable = mlx4_allmulticast_disable,
4320 .link_update = mlx4_link_update,
4321 .stats_get = mlx4_stats_get,
4322 .stats_reset = mlx4_stats_reset,
4323 .queue_stats_mapping_set = NULL,
4324 .dev_infos_get = mlx4_dev_infos_get,
4325 .vlan_filter_set = mlx4_vlan_filter_set,
4326 .vlan_tpid_set = NULL,
4327 .vlan_strip_queue_set = NULL,
4328 .vlan_offload_set = NULL,
4329 .rx_queue_setup = mlx4_rx_queue_setup,
4330 .tx_queue_setup = mlx4_tx_queue_setup,
4331 .rx_queue_release = mlx4_rx_queue_release,
4332 .tx_queue_release = mlx4_tx_queue_release,
4334 .dev_led_off = NULL,
4335 .flow_ctrl_get = mlx4_dev_get_flow_ctrl,
4336 .flow_ctrl_set = mlx4_dev_set_flow_ctrl,
4337 .priority_flow_ctrl_set = NULL,
4338 .mac_addr_remove = mlx4_mac_addr_remove,
4339 .mac_addr_add = mlx4_mac_addr_add,
4340 .mtu_set = mlx4_dev_set_mtu,
4341 .fdir_add_signature_filter = NULL,
4342 .fdir_update_signature_filter = NULL,
4343 .fdir_remove_signature_filter = NULL,
4344 .fdir_add_perfect_filter = NULL,
4345 .fdir_update_perfect_filter = NULL,
4346 .fdir_remove_perfect_filter = NULL,
4347 .fdir_set_masks = NULL
4351 * Get PCI information from struct ibv_device.
4354 * Pointer to Ethernet device structure.
4355 * @param[out] pci_addr
4356 * PCI bus address output buffer.
4359 * 0 on success, -1 on failure and errno is set.
4362 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
4363 struct rte_pci_addr *pci_addr)
4367 MKSTR(path, "%s/device/uevent", device->ibdev_path);
4369 file = fopen(path, "rb");
4372 while (fgets(line, sizeof(line), file) == line) {
4373 size_t len = strlen(line);
4376 /* Truncate long lines. */
4377 if (len == (sizeof(line) - 1))
4378 while (line[(len - 1)] != '\n') {
4382 line[(len - 1)] = ret;
4384 /* Extract information. */
4387 "%" SCNx16 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
4391 &pci_addr->function) == 4) {
4401 * Get MAC address by querying netdevice.
4404 * struct priv for the requested device.
4406 * MAC address output buffer.
4409 * 0 on success, -1 on failure and errno is set.
4412 priv_get_mac(struct priv *priv, uint8_t (*mac)[ETHER_ADDR_LEN])
4414 struct ifreq request;
4416 if (priv_ifreq(priv, SIOCGIFHWADDR, &request))
4418 memcpy(mac, request.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
4422 /* Support up to 32 adapters. */
4424 struct rte_pci_addr pci_addr; /* associated PCI address */
4425 uint32_t ports; /* physical ports bitfield. */
4429 * Get device index in mlx4_dev[] from PCI bus address.
4431 * @param[in] pci_addr
4432 * PCI bus address to look for.
4435 * mlx4_dev[] index on success, -1 on failure.
4438 mlx4_dev_idx(struct rte_pci_addr *pci_addr)
4443 assert(pci_addr != NULL);
4444 for (i = 0; (i != elemof(mlx4_dev)); ++i) {
4445 if ((mlx4_dev[i].pci_addr.domain == pci_addr->domain) &&
4446 (mlx4_dev[i].pci_addr.bus == pci_addr->bus) &&
4447 (mlx4_dev[i].pci_addr.devid == pci_addr->devid) &&
4448 (mlx4_dev[i].pci_addr.function == pci_addr->function))
4450 if ((mlx4_dev[i].ports == 0) && (ret == -1))
4457 * Retrieve integer value from environment variable.
4460 * Environment variable name.
4463 * Integer value, 0 if the variable is not set.
4466 mlx4_getenv_int(const char *name)
4468 const char *val = getenv(name);
4475 static struct eth_driver mlx4_driver;
4478 * DPDK callback to register a PCI device.
4480 * This function creates an Ethernet device for each port of a given
4483 * @param[in] pci_drv
4484 * PCI driver structure (mlx4_driver).
4485 * @param[in] pci_dev
4486 * PCI device information.
4489 * 0 on success, negative errno value on failure.
4492 mlx4_pci_devinit(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
4494 struct ibv_device **list;
4495 struct ibv_device *ibv_dev;
4497 struct ibv_context *attr_ctx = NULL;
4498 struct ibv_device_attr device_attr;
4504 assert(pci_drv == &mlx4_driver.pci_drv);
4505 /* Get mlx4_dev[] index. */
4506 idx = mlx4_dev_idx(&pci_dev->addr);
4508 ERROR("this driver cannot support any more adapters");
4511 DEBUG("using driver device index %d", idx);
4513 /* Save PCI address. */
4514 mlx4_dev[idx].pci_addr = pci_dev->addr;
4515 list = ibv_get_device_list(&i);
4518 if (errno == ENOSYS) {
4519 WARN("cannot list devices, is ib_uverbs loaded?");
4526 * For each listed device, check related sysfs entry against
4527 * the provided PCI ID.
4530 struct rte_pci_addr pci_addr;
4533 DEBUG("checking device \"%s\"", list[i]->name);
4534 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
4536 if ((pci_dev->addr.domain != pci_addr.domain) ||
4537 (pci_dev->addr.bus != pci_addr.bus) ||
4538 (pci_dev->addr.devid != pci_addr.devid) ||
4539 (pci_dev->addr.function != pci_addr.function))
4541 vf = (pci_dev->id.device_id ==
4542 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
4543 INFO("PCI information matches, using device \"%s\" (VF: %s)",
4544 list[i]->name, (vf ? "true" : "false"));
4545 attr_ctx = ibv_open_device(list[i]);
4549 if (attr_ctx == NULL) {
4550 ibv_free_device_list(list);
4553 WARN("cannot access device, is mlx4_ib loaded?");
4556 WARN("cannot use device, are drivers up to date?");
4564 DEBUG("device opened");
4565 if (ibv_query_device(attr_ctx, &device_attr))
4567 INFO("%u port(s) detected", device_attr.phys_port_cnt);
4569 for (i = 0; i < device_attr.phys_port_cnt; i++) {
4570 uint32_t port = i + 1; /* ports are indexed from one */
4571 uint32_t test = (1 << i);
4572 struct ibv_context *ctx = NULL;
4573 struct ibv_port_attr port_attr;
4574 struct ibv_pd *pd = NULL;
4575 struct priv *priv = NULL;
4576 struct rte_eth_dev *eth_dev;
4577 #ifdef HAVE_EXP_QUERY_DEVICE
4578 struct ibv_exp_device_attr exp_device_attr;
4579 #endif /* HAVE_EXP_QUERY_DEVICE */
4580 struct ether_addr mac;
4582 #ifdef HAVE_EXP_QUERY_DEVICE
4583 exp_device_attr.comp_mask = IBV_EXP_DEVICE_ATTR_EXP_CAP_FLAGS;
4585 exp_device_attr.comp_mask |= IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ;
4586 #endif /* RSS_SUPPORT */
4587 #endif /* HAVE_EXP_QUERY_DEVICE */
4589 DEBUG("using port %u (%08" PRIx32 ")", port, test);
4591 ctx = ibv_open_device(ibv_dev);
4595 /* Check port status. */
4596 err = ibv_query_port(ctx, port, &port_attr);
4598 ERROR("port query failed: %s", strerror(err));
4601 if (port_attr.state != IBV_PORT_ACTIVE)
4602 WARN("bad state for port %d: \"%s\" (%d)",
4603 port, ibv_port_state_str(port_attr.state),
4606 /* Allocate protection domain. */
4607 pd = ibv_alloc_pd(ctx);
4609 ERROR("PD allocation failure");
4614 mlx4_dev[idx].ports |= test;
4616 /* from rte_ethdev.c */
4617 priv = rte_zmalloc("ethdev private structure",
4619 RTE_CACHE_LINE_SIZE);
4621 ERROR("priv allocation failure");
4627 priv->device_attr = device_attr;
4630 priv->mtu = ETHER_MTU;
4631 #ifdef HAVE_EXP_QUERY_DEVICE
4632 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
4633 ERROR("ibv_exp_query_device() failed");
4637 if ((exp_device_attr.exp_device_cap_flags &
4638 IBV_EXP_DEVICE_QPG) &&
4639 (exp_device_attr.exp_device_cap_flags &
4640 IBV_EXP_DEVICE_UD_RSS) &&
4641 (exp_device_attr.comp_mask &
4642 IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ) &&
4643 (exp_device_attr.max_rss_tbl_sz > 0)) {
4646 priv->max_rss_tbl_sz = exp_device_attr.max_rss_tbl_sz;
4650 priv->max_rss_tbl_sz = 0;
4652 priv->hw_tss = !!(exp_device_attr.exp_device_cap_flags &
4653 IBV_EXP_DEVICE_UD_TSS);
4654 DEBUG("device flags: %s%s%s",
4655 (priv->hw_qpg ? "IBV_DEVICE_QPG " : ""),
4656 (priv->hw_tss ? "IBV_DEVICE_TSS " : ""),
4657 (priv->hw_rss ? "IBV_DEVICE_RSS " : ""));
4659 DEBUG("maximum RSS indirection table size: %u",
4660 exp_device_attr.max_rss_tbl_sz);
4661 #endif /* RSS_SUPPORT */
4664 priv->inl_recv_size = mlx4_getenv_int("MLX4_INLINE_RECV_SIZE");
4666 if (priv->inl_recv_size) {
4667 exp_device_attr.comp_mask =
4668 IBV_EXP_DEVICE_ATTR_INLINE_RECV_SZ;
4669 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
4670 INFO("Couldn't query device for inline-receive"
4672 priv->inl_recv_size = 0;
4674 if ((unsigned)exp_device_attr.inline_recv_sz <
4675 priv->inl_recv_size) {
4676 INFO("Max inline-receive (%d) <"
4677 " requested inline-receive (%u)",
4678 exp_device_attr.inline_recv_sz,
4679 priv->inl_recv_size);
4680 priv->inl_recv_size =
4681 exp_device_attr.inline_recv_sz;
4684 INFO("Set inline receive size to %u",
4685 priv->inl_recv_size);
4687 #endif /* INLINE_RECV */
4688 #endif /* HAVE_EXP_QUERY_DEVICE */
4690 (void)mlx4_getenv_int;
4692 /* Configure the first MAC address by default. */
4693 if (priv_get_mac(priv, &mac.addr_bytes)) {
4694 ERROR("cannot get MAC address, is mlx4_en loaded?"
4695 " (errno: %s)", strerror(errno));
4698 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
4700 mac.addr_bytes[0], mac.addr_bytes[1],
4701 mac.addr_bytes[2], mac.addr_bytes[3],
4702 mac.addr_bytes[4], mac.addr_bytes[5]);
4703 /* Register MAC and broadcast addresses. */
4704 claim_zero(priv_mac_addr_add(priv, 0,
4705 (const uint8_t (*)[ETHER_ADDR_LEN])
4707 claim_zero(priv_mac_addr_add(priv, 1,
4708 &(const uint8_t [ETHER_ADDR_LEN])
4709 { "\xff\xff\xff\xff\xff\xff" }));
4712 char ifname[IF_NAMESIZE];
4714 if (priv_get_ifname(priv, &ifname) == 0)
4715 DEBUG("port %u ifname is \"%s\"",
4716 priv->port, ifname);
4718 DEBUG("port %u ifname is unknown", priv->port);
4721 /* Get actual MTU if possible. */
4722 priv_get_mtu(priv, &priv->mtu);
4723 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
4725 /* from rte_ethdev.c */
4727 char name[RTE_ETH_NAME_MAX_LEN];
4729 snprintf(name, sizeof(name), "%s port %u",
4730 ibv_get_device_name(ibv_dev), port);
4731 eth_dev = rte_eth_dev_allocate(name, RTE_ETH_DEV_PCI);
4733 if (eth_dev == NULL) {
4734 ERROR("can not allocate rte ethdev");
4739 eth_dev->data->dev_private = priv;
4740 eth_dev->pci_dev = pci_dev;
4741 eth_dev->driver = &mlx4_driver;
4742 eth_dev->data->rx_mbuf_alloc_failed = 0;
4743 eth_dev->data->mtu = ETHER_MTU;
4745 priv->dev = eth_dev;
4746 eth_dev->dev_ops = &mlx4_dev_ops;
4747 eth_dev->data->mac_addrs = priv->mac;
4749 /* Bring Ethernet device up. */
4750 DEBUG("forcing Ethernet interface up");
4751 priv_set_flags(priv, ~IFF_UP, IFF_UP);
4757 claim_zero(ibv_dealloc_pd(pd));
4759 claim_zero(ibv_close_device(ctx));
4764 * XXX if something went wrong in the loop above, there is a resource
4765 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
4766 * long as the dpdk does not provide a way to deallocate a ethdev and a
4767 * way to enumerate the registered ethdevs to free the previous ones.
4770 /* no port found, complain */
4771 if (!mlx4_dev[idx].ports) {
4778 claim_zero(ibv_close_device(attr_ctx));
4780 ibv_free_device_list(list);
4785 static const struct rte_pci_id mlx4_pci_id_map[] = {
4787 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4788 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3,
4789 .subsystem_vendor_id = PCI_ANY_ID,
4790 .subsystem_device_id = PCI_ANY_ID
4793 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4794 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO,
4795 .subsystem_vendor_id = PCI_ANY_ID,
4796 .subsystem_device_id = PCI_ANY_ID
4799 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4800 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3VF,
4801 .subsystem_vendor_id = PCI_ANY_ID,
4802 .subsystem_device_id = PCI_ANY_ID
4809 static struct eth_driver mlx4_driver = {
4811 .name = MLX4_DRIVER_NAME,
4812 .id_table = mlx4_pci_id_map,
4813 .devinit = mlx4_pci_devinit,
4815 .dev_private_size = sizeof(struct priv)
4819 * Driver initialization routine.
4822 rte_mlx4_pmd_init(const char *name, const char *args)
4827 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
4828 * huge pages. Calling ibv_fork_init() during init allows
4829 * applications to use fork() safely for purposes other than
4830 * using this PMD, which is not supported in forked processes.
4832 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
4834 rte_eal_pci_register(&mlx4_driver.pci_drv);
4838 static struct rte_driver rte_mlx4_driver = {
4840 .name = MLX4_DRIVER_NAME,
4841 .init = rte_mlx4_pmd_init,
4844 PMD_REGISTER_DRIVER(rte_mlx4_driver)