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 * - Multiple RX VLAN filters can be configured, but only the first one
38 * - RSS hash key and options cannot be modified.
39 * - Hardware counters aren't implemented.
53 #include <arpa/inet.h>
56 #include <sys/ioctl.h>
57 #include <sys/socket.h>
58 #include <netinet/in.h>
60 #include <linux/ethtool.h>
61 #include <linux/sockios.h>
64 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
66 #pragma GCC diagnostic ignored "-pedantic"
68 #include <infiniband/verbs.h>
70 #pragma GCC diagnostic error "-pedantic"
73 /* DPDK headers don't like -pedantic. */
75 #pragma GCC diagnostic ignored "-pedantic"
77 #include <rte_config.h>
78 #include <rte_ether.h>
79 #include <rte_ethdev.h>
82 #include <rte_errno.h>
83 #include <rte_mempool.h>
84 #include <rte_prefetch.h>
85 #include <rte_malloc.h>
86 #include <rte_spinlock.h>
87 #include <rte_atomic.h>
88 #include <rte_version.h>
91 #pragma GCC diagnostic error "-pedantic"
94 /* Generated configuration header. */
95 #include "mlx4_autoconf.h"
100 /* Runtime logging through RTE_LOG() is enabled when not in debugging mode.
101 * Intermediate LOG_*() macros add the required end-of-line characters. */
103 #define INFO(...) DEBUG(__VA_ARGS__)
104 #define WARN(...) DEBUG(__VA_ARGS__)
105 #define ERROR(...) DEBUG(__VA_ARGS__)
107 #define LOG__(level, m, ...) \
108 RTE_LOG(level, PMD, MLX4_DRIVER_NAME ": " m "%c", __VA_ARGS__)
109 #define LOG_(level, ...) LOG__(level, __VA_ARGS__, '\n')
110 #define INFO(...) LOG_(INFO, __VA_ARGS__)
111 #define WARN(...) LOG_(WARNING, __VA_ARGS__)
112 #define ERROR(...) LOG_(ERR, __VA_ARGS__)
115 /* Convenience macros for accessing mbuf fields. */
116 #define NEXT(m) ((m)->next)
117 #define DATA_LEN(m) ((m)->data_len)
118 #define PKT_LEN(m) ((m)->pkt_len)
119 #define DATA_OFF(m) ((m)->data_off)
120 #define SET_DATA_OFF(m, o) ((m)->data_off = (o))
121 #define NB_SEGS(m) ((m)->nb_segs)
122 #define PORT(m) ((m)->port)
124 /* Work Request ID data type (64 bit). */
133 #define WR_ID(o) (((wr_id_t *)&(o))->data)
135 /* Compile-time check. */
136 static inline void wr_id_t_check(void)
138 wr_id_t check[1 + (2 * -!(sizeof(wr_id_t) == sizeof(uint64_t)))];
144 /* If raw send operations are available, use them since they are faster. */
145 #ifdef SEND_RAW_WR_SUPPORT
146 typedef struct ibv_send_wr_raw mlx4_send_wr_t;
147 #define mlx4_post_send ibv_post_send_raw
149 typedef struct ibv_send_wr mlx4_send_wr_t;
150 #define mlx4_post_send ibv_post_send
153 struct mlx4_rxq_stats {
154 unsigned int idx; /**< Mapping index. */
155 #ifdef MLX4_PMD_SOFT_COUNTERS
156 uint64_t ipackets; /**< Total of successfully received packets. */
157 uint64_t ibytes; /**< Total of successfully received bytes. */
159 uint64_t idropped; /**< Total of packets dropped when RX ring full. */
160 uint64_t rx_nombuf; /**< Total of RX mbuf allocation failures. */
163 struct mlx4_txq_stats {
164 unsigned int idx; /**< Mapping index. */
165 #ifdef MLX4_PMD_SOFT_COUNTERS
166 uint64_t opackets; /**< Total of successfully sent packets. */
167 uint64_t obytes; /**< Total of successfully sent bytes. */
169 uint64_t odropped; /**< Total of packets not sent when TX ring full. */
172 /* RX element (scattered packets). */
174 struct ibv_recv_wr wr; /* Work Request. */
175 struct ibv_sge sges[MLX4_PMD_SGE_WR_N]; /* Scatter/Gather Elements. */
176 struct rte_mbuf *bufs[MLX4_PMD_SGE_WR_N]; /* SGEs buffers. */
181 struct ibv_recv_wr wr; /* Work Request. */
182 struct ibv_sge sge; /* Scatter/Gather Element. */
183 /* mbuf pointer is derived from WR_ID(wr.wr_id).offset. */
186 /* RX queue descriptor. */
188 struct priv *priv; /* Back pointer to private data. */
189 struct rte_mempool *mp; /* Memory Pool for allocations. */
190 struct ibv_mr *mr; /* Memory Region (for mp). */
191 struct ibv_cq *cq; /* Completion Queue. */
192 struct ibv_qp *qp; /* Queue Pair. */
194 * There is exactly one flow configured per MAC address. Each flow
195 * may contain several specifications, one per configured VLAN ID.
197 BITFIELD_DECLARE(mac_configured, uint32_t, MLX4_MAX_MAC_ADDRESSES);
198 struct ibv_exp_flow *mac_flow[MLX4_MAX_MAC_ADDRESSES];
199 struct ibv_exp_flow *promisc_flow; /* Promiscuous flow. */
200 struct ibv_exp_flow *allmulti_flow; /* Multicast flow. */
201 unsigned int port_id; /* Port ID for incoming packets. */
202 unsigned int elts_n; /* (*elts)[] length. */
203 unsigned int elts_head; /* Current index in (*elts)[]. */
205 struct rxq_elt_sp (*sp)[]; /* Scattered RX elements. */
206 struct rxq_elt (*no_sp)[]; /* RX elements. */
208 unsigned int sp:1; /* Use scattered RX elements. */
209 uint32_t mb_len; /* Length of a mp-issued mbuf. */
210 struct mlx4_rxq_stats stats; /* RX queue counters. */
211 unsigned int socket; /* CPU socket ID for allocations. */
216 mlx4_send_wr_t wr; /* Work Request. */
217 struct ibv_sge sges[MLX4_PMD_SGE_WR_N]; /* Scatter/Gather Elements. */
218 /* mbuf pointer is derived from WR_ID(wr.wr_id).offset. */
221 /* Linear buffer type. It is used when transmitting buffers with too many
222 * segments that do not fit the hardware queue (see max_send_sge).
223 * Extra segments are copied (linearized) in such buffers, replacing the
224 * last SGE during TX.
225 * The size is arbitrary but large enough to hold a jumbo frame with
226 * 8 segments considering mbuf.buf_len is about 2048 bytes. */
227 typedef uint8_t linear_t[16384];
229 /* TX queue descriptor. */
231 struct priv *priv; /* Back pointer to private data. */
233 struct rte_mempool *mp; /* Cached Memory Pool. */
234 struct ibv_mr *mr; /* Memory Region (for mp). */
235 uint32_t lkey; /* mr->lkey */
236 } mp2mr[MLX4_PMD_TX_MP_CACHE]; /* MP to MR translation table. */
237 struct ibv_cq *cq; /* Completion Queue. */
238 struct ibv_qp *qp; /* Queue Pair. */
239 #if MLX4_PMD_MAX_INLINE > 0
240 uint32_t max_inline; /* Max inline send size <= MLX4_PMD_MAX_INLINE. */
242 unsigned int elts_n; /* (*elts)[] length. */
243 struct txq_elt (*elts)[]; /* TX elements. */
244 unsigned int elts_head; /* Current index in (*elts)[]. */
245 unsigned int elts_tail; /* First element awaiting completion. */
246 unsigned int elts_comp; /* Number of completion requests. */
247 struct mlx4_txq_stats stats; /* TX queue counters. */
248 linear_t (*elts_linear)[]; /* Linearized buffers. */
249 struct ibv_mr *mr_linear; /* Memory Region for linearized buffers. */
250 unsigned int socket; /* CPU socket ID for allocations. */
254 struct rte_eth_dev *dev; /* Ethernet device. */
255 struct ibv_context *ctx; /* Verbs context. */
256 struct ibv_device_attr device_attr; /* Device properties. */
257 struct ibv_port_attr port_attr; /* Physical port 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 promisc_ok:1; /* Promiscuous flow is supported. */
277 unsigned int allmulti:1; /* Device receives all multicast packets. */
278 unsigned int hw_qpg:1; /* QP groups are supported. */
279 unsigned int hw_tss:1; /* TSS is supported. */
280 unsigned int hw_rss:1; /* RSS is supported. */
281 unsigned int rss:1; /* RSS is enabled. */
282 unsigned int vf:1; /* This is a VF device. */
284 unsigned int inl_recv_size; /* Inline recv size */
286 unsigned int max_rss_tbl_sz; /* Maximum number of RSS queues. */
288 struct rxq rxq_parent; /* Parent queue when RSS is enabled. */
289 unsigned int rxqs_n; /* RX queues array size. */
290 unsigned int txqs_n; /* TX queues array size. */
291 struct rxq *(*rxqs)[]; /* RX queues. */
292 struct txq *(*txqs)[]; /* TX queues. */
293 rte_spinlock_t lock; /* Lock for control functions. */
297 * Lock private structure to protect it from concurrent access in the
301 * Pointer to private structure.
304 priv_lock(struct priv *priv)
306 rte_spinlock_lock(&priv->lock);
310 * Unlock private structure.
313 * Pointer to private structure.
316 priv_unlock(struct priv *priv)
318 rte_spinlock_unlock(&priv->lock);
321 /* Allocate a buffer on the stack and fill it with a printf format string. */
322 #define MKSTR(name, ...) \
323 char name[snprintf(NULL, 0, __VA_ARGS__) + 1]; \
325 snprintf(name, sizeof(name), __VA_ARGS__)
328 * Get interface name from private structure.
331 * Pointer to private structure.
333 * Interface name output buffer.
336 * 0 on success, -1 on failure and errno is set.
339 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
343 unsigned int dev_type = 0;
344 unsigned int dev_port_prev = ~0u;
345 char match[IF_NAMESIZE] = "";
348 MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
354 while ((dent = readdir(dir)) != NULL) {
355 char *name = dent->d_name;
357 unsigned int dev_port;
360 if ((name[0] == '.') &&
361 ((name[1] == '\0') ||
362 ((name[1] == '.') && (name[2] == '\0'))))
365 MKSTR(path, "%s/device/net/%s/%s",
366 priv->ctx->device->ibdev_path, name,
367 (dev_type ? "dev_id" : "dev_port"));
369 file = fopen(path, "rb");
374 * Switch to dev_id when dev_port does not exist as
375 * is the case with Linux kernel versions < 3.15.
386 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
391 * Switch to dev_id when dev_port returns the same value for
392 * all ports. May happen when using a MOFED release older than
393 * 3.0 with a Linux kernel >= 3.15.
395 if (dev_port == dev_port_prev)
397 dev_port_prev = dev_port;
398 if (dev_port == (priv->port - 1u))
399 snprintf(match, sizeof(match), "%s", name);
402 if (match[0] == '\0')
404 strncpy(*ifname, match, sizeof(*ifname));
409 * Read from sysfs entry.
412 * Pointer to private structure.
414 * Entry name relative to sysfs path.
416 * Data output buffer.
421 * 0 on success, -1 on failure and errno is set.
424 priv_sysfs_read(const struct priv *priv, const char *entry,
425 char *buf, size_t size)
427 char ifname[IF_NAMESIZE];
432 if (priv_get_ifname(priv, &ifname))
435 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
438 file = fopen(path, "rb");
441 ret = fread(buf, 1, size, file);
443 if (((size_t)ret < size) && (ferror(file)))
453 * Write to sysfs entry.
456 * Pointer to private structure.
458 * Entry name relative to sysfs path.
465 * 0 on success, -1 on failure and errno is set.
468 priv_sysfs_write(const struct priv *priv, const char *entry,
469 char *buf, size_t size)
471 char ifname[IF_NAMESIZE];
476 if (priv_get_ifname(priv, &ifname))
479 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
482 file = fopen(path, "wb");
485 ret = fwrite(buf, 1, size, file);
487 if (((size_t)ret < size) || (ferror(file)))
497 * Get unsigned long sysfs property.
500 * Pointer to private structure.
502 * Entry name relative to sysfs path.
504 * Value output buffer.
507 * 0 on success, -1 on failure and errno is set.
510 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
513 unsigned long value_ret;
516 ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
518 DEBUG("cannot read %s value from sysfs: %s",
519 name, strerror(errno));
522 value_str[ret] = '\0';
524 value_ret = strtoul(value_str, NULL, 0);
526 DEBUG("invalid %s value `%s': %s", name, value_str,
535 * Set unsigned long sysfs property.
538 * Pointer to private structure.
540 * Entry name relative to sysfs path.
545 * 0 on success, -1 on failure and errno is set.
548 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
551 MKSTR(value_str, "%lu", value);
553 ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
555 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
556 name, value_str, value, strerror(errno));
563 * Perform ifreq ioctl() on associated Ethernet device.
566 * Pointer to private structure.
568 * Request number to pass to ioctl().
570 * Interface request structure output buffer.
573 * 0 on success, -1 on failure and errno is set.
576 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
578 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
583 if (priv_get_ifname(priv, &ifr->ifr_name) == 0)
584 ret = ioctl(sock, req, ifr);
593 * Pointer to private structure.
595 * MTU value output buffer.
598 * 0 on success, -1 on failure and errno is set.
601 priv_get_mtu(struct priv *priv, uint16_t *mtu)
603 unsigned long ulong_mtu;
605 if (priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu) == -1)
615 * Pointer to private structure.
620 * 0 on success, -1 on failure and errno is set.
623 priv_set_mtu(struct priv *priv, uint16_t mtu)
625 return priv_set_sysfs_ulong(priv, "mtu", mtu);
632 * Pointer to private structure.
634 * Bitmask for flags that must remain untouched.
636 * Bitmask for flags to modify.
639 * 0 on success, -1 on failure and errno is set.
642 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
646 if (priv_get_sysfs_ulong(priv, "flags", &tmp) == -1)
650 return priv_set_sysfs_ulong(priv, "flags", tmp);
653 /* Device configuration. */
656 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
657 unsigned int socket, const struct rte_eth_rxconf *conf,
658 struct rte_mempool *mp);
661 rxq_cleanup(struct rxq *rxq);
664 * Ethernet device configuration.
666 * Prepare the driver for a given number of TX and RX queues.
667 * Allocate parent RSS queue when several RX queues are requested.
670 * Pointer to Ethernet device structure.
673 * 0 on success, errno value on failure.
676 dev_configure(struct rte_eth_dev *dev)
678 struct priv *priv = dev->data->dev_private;
679 unsigned int rxqs_n = dev->data->nb_rx_queues;
680 unsigned int txqs_n = dev->data->nb_tx_queues;
684 priv->rxqs = (void *)dev->data->rx_queues;
685 priv->txqs = (void *)dev->data->tx_queues;
686 if (txqs_n != priv->txqs_n) {
687 INFO("%p: TX queues number update: %u -> %u",
688 (void *)dev, priv->txqs_n, txqs_n);
689 priv->txqs_n = txqs_n;
691 if (rxqs_n == priv->rxqs_n)
693 INFO("%p: RX queues number update: %u -> %u",
694 (void *)dev, priv->rxqs_n, rxqs_n);
695 /* If RSS is enabled, disable it first. */
699 /* Only if there are no remaining child RX queues. */
700 for (i = 0; (i != priv->rxqs_n); ++i)
701 if ((*priv->rxqs)[i] != NULL)
703 rxq_cleanup(&priv->rxq_parent);
708 /* Nothing else to do. */
709 priv->rxqs_n = rxqs_n;
712 /* Allocate a new RSS parent queue if supported by hardware. */
714 ERROR("%p: only a single RX queue can be configured when"
715 " hardware doesn't support RSS",
719 /* Fail if hardware doesn't support that many RSS queues. */
720 if (rxqs_n >= priv->max_rss_tbl_sz) {
721 ERROR("%p: only %u RX queues can be configured for RSS",
722 (void *)dev, priv->max_rss_tbl_sz);
727 priv->rxqs_n = rxqs_n;
728 ret = rxq_setup(dev, &priv->rxq_parent, 0, 0, NULL, NULL);
731 /* Failure, rollback. */
739 * DPDK callback for Ethernet device configuration.
742 * Pointer to Ethernet device structure.
745 * 0 on success, negative errno value on failure.
748 mlx4_dev_configure(struct rte_eth_dev *dev)
750 struct priv *priv = dev->data->dev_private;
754 ret = dev_configure(dev);
760 /* TX queues handling. */
763 * Allocate TX queue elements.
766 * Pointer to TX queue structure.
768 * Number of elements to allocate.
771 * 0 on success, errno value on failure.
774 txq_alloc_elts(struct txq *txq, unsigned int elts_n)
777 struct txq_elt (*elts)[elts_n] =
778 rte_calloc_socket("TXQ", 1, sizeof(*elts), 0, txq->socket);
779 linear_t (*elts_linear)[elts_n] =
780 rte_calloc_socket("TXQ", 1, sizeof(*elts_linear), 0,
782 struct ibv_mr *mr_linear = NULL;
785 if ((elts == NULL) || (elts_linear == NULL)) {
786 ERROR("%p: can't allocate packets array", (void *)txq);
791 ibv_reg_mr(txq->priv->pd, elts_linear, sizeof(*elts_linear),
792 (IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE));
793 if (mr_linear == NULL) {
794 ERROR("%p: unable to configure MR, ibv_reg_mr() failed",
799 for (i = 0; (i != elts_n); ++i) {
800 struct txq_elt *elt = &(*elts)[i];
801 mlx4_send_wr_t *wr = &elt->wr;
804 WR_ID(wr->wr_id).id = i;
805 WR_ID(wr->wr_id).offset = 0;
806 wr->sg_list = &elt->sges[0];
807 wr->opcode = IBV_WR_SEND;
808 /* Other fields are updated during TX. */
810 DEBUG("%p: allocated and configured %u WRs", (void *)txq, elts_n);
811 txq->elts_n = elts_n;
816 txq->elts_linear = elts_linear;
817 txq->mr_linear = mr_linear;
821 if (mr_linear != NULL)
822 claim_zero(ibv_dereg_mr(mr_linear));
824 rte_free(elts_linear);
827 DEBUG("%p: failed, freed everything", (void *)txq);
833 * Free TX queue elements.
836 * Pointer to TX queue structure.
839 txq_free_elts(struct txq *txq)
842 unsigned int elts_n = txq->elts_n;
843 struct txq_elt (*elts)[elts_n] = txq->elts;
844 linear_t (*elts_linear)[elts_n] = txq->elts_linear;
845 struct ibv_mr *mr_linear = txq->mr_linear;
847 DEBUG("%p: freeing WRs", (void *)txq);
850 txq->elts_linear = NULL;
851 txq->mr_linear = NULL;
852 if (mr_linear != NULL)
853 claim_zero(ibv_dereg_mr(mr_linear));
855 rte_free(elts_linear);
858 for (i = 0; (i != elemof(*elts)); ++i) {
859 struct txq_elt *elt = &(*elts)[i];
861 if (WR_ID(elt->wr.wr_id).offset == 0)
863 rte_pktmbuf_free((void *)((uintptr_t)elt->sges[0].addr -
864 WR_ID(elt->wr.wr_id).offset));
871 * Clean up a TX queue.
873 * Destroy objects, free allocated memory and reset the structure for reuse.
876 * Pointer to TX queue structure.
879 txq_cleanup(struct txq *txq)
883 DEBUG("cleaning up %p", (void *)txq);
886 claim_zero(ibv_destroy_qp(txq->qp));
888 claim_zero(ibv_destroy_cq(txq->cq));
889 for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
890 if (txq->mp2mr[i].mp == NULL)
892 assert(txq->mp2mr[i].mr != NULL);
893 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
895 memset(txq, 0, sizeof(*txq));
899 * Manage TX completions.
901 * When sending a burst, mlx4_tx_burst() posts several WRs.
902 * To improve performance, a completion event is only required for the last of
903 * them. Doing so discards completion information for other WRs, but this
904 * information would not be used anyway.
907 * Pointer to TX queue structure.
910 * 0 on success, -1 on failure.
913 txq_complete(struct txq *txq)
915 unsigned int elts_comp = txq->elts_comp;
916 unsigned int elts_tail;
917 const unsigned int elts_n = txq->elts_n;
918 struct ibv_wc wcs[elts_comp];
921 if (unlikely(elts_comp == 0))
924 DEBUG("%p: processing %u work requests completions",
925 (void *)txq, elts_comp);
927 wcs_n = ibv_poll_cq(txq->cq, elts_comp, wcs);
928 if (unlikely(wcs_n == 0))
930 if (unlikely(wcs_n < 0)) {
931 DEBUG("%p: ibv_poll_cq() failed (wcs_n=%d)",
936 assert(elts_comp <= txq->elts_comp);
938 * Work Completion ID contains the associated element index in
939 * (*txq->elts)[]. Since WCs are returned in order, we only need to
940 * look at the last WC to clear older Work Requests.
942 * Assume WC status is successful as nothing can be done about it
945 elts_tail = WR_ID(wcs[wcs_n - 1].wr_id).id;
946 /* Consume the last WC. */
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;
1073 max = (elts_n - (elts_head - elts_tail));
1077 assert(max <= elts_n);
1078 /* Always leave one free entry in the ring. */
1084 for (i = 0; (i != max); ++i) {
1085 struct rte_mbuf *buf = pkts[i];
1086 struct txq_elt *elt = &(*txq->elts)[elts_head];
1087 mlx4_send_wr_t *wr = &elt->wr;
1088 unsigned int segs = NB_SEGS(buf);
1089 #if (MLX4_PMD_MAX_INLINE > 0) || defined(MLX4_PMD_SOFT_COUNTERS)
1090 unsigned int sent_size = 0;
1095 /* Clean up old buffer. */
1096 if (likely(WR_ID(wr->wr_id).offset != 0)) {
1097 struct rte_mbuf *tmp = (void *)
1098 ((uintptr_t)elt->sges[0].addr -
1099 WR_ID(wr->wr_id).offset);
1101 /* Faster than rte_pktmbuf_free(). */
1103 struct rte_mbuf *next = NEXT(tmp);
1105 rte_pktmbuf_free_seg(tmp);
1107 } while (tmp != NULL);
1111 WR_ID(wr->wr_id).offset = 0;
1112 for (j = 0; ((int)j < wr->num_sge); ++j) {
1113 elt->sges[j].addr = 0;
1114 elt->sges[j].length = 0;
1115 elt->sges[j].lkey = 0;
1120 /* Sanity checks, most of which are only relevant with
1121 * debugging enabled. */
1122 assert(WR_ID(wr->wr_id).id == elts_head);
1123 assert(WR_ID(wr->wr_id).offset == 0);
1124 assert(wr->next == NULL);
1125 assert(wr->sg_list == &elt->sges[0]);
1126 assert(wr->num_sge == 0);
1127 assert(wr->opcode == IBV_WR_SEND);
1128 /* When there are too many segments, extra segments are
1129 * linearized in the last SGE. */
1130 if (unlikely(segs > elemof(elt->sges))) {
1131 segs = (elemof(elt->sges) - 1);
1134 /* Set WR fields. */
1135 assert((rte_pktmbuf_mtod(buf, uintptr_t) -
1136 (uintptr_t)buf) <= 0xffff);
1137 WR_ID(wr->wr_id).offset =
1138 (rte_pktmbuf_mtod(buf, uintptr_t) -
1141 /* Register segments as SGEs. */
1142 for (j = 0; (j != segs); ++j) {
1143 struct ibv_sge *sge = &elt->sges[j];
1146 /* Retrieve Memory Region key for this memory pool. */
1147 lkey = txq_mp2mr(txq, buf->pool);
1148 if (unlikely(lkey == (uint32_t)-1)) {
1149 /* MR does not exist. */
1150 DEBUG("%p: unable to get MP <-> MR"
1151 " association", (void *)txq);
1152 /* Clean up TX element. */
1153 WR_ID(elt->wr.wr_id).offset = 0;
1167 /* Sanity checks, only relevant with debugging
1169 assert(sge->addr == 0);
1170 assert(sge->length == 0);
1171 assert(sge->lkey == 0);
1173 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
1175 rte_prefetch0((volatile void *)
1176 (uintptr_t)sge->addr);
1177 sge->length = DATA_LEN(buf);
1179 #if (MLX4_PMD_MAX_INLINE > 0) || defined(MLX4_PMD_SOFT_COUNTERS)
1180 sent_size += sge->length;
1184 /* If buf is not NULL here and is not going to be linearized,
1185 * nb_segs is not valid. */
1187 assert((buf == NULL) || (linearize));
1188 /* Linearize extra segments. */
1190 struct ibv_sge *sge = &elt->sges[segs];
1191 linear_t *linear = &(*txq->elts_linear)[elts_head];
1192 unsigned int size = linearize_mbuf(linear, buf);
1194 assert(segs == (elemof(elt->sges) - 1));
1196 /* Invalid packet. */
1197 DEBUG("%p: packet too large to be linearized.",
1199 /* Clean up TX element. */
1200 WR_ID(elt->wr.wr_id).offset = 0;
1214 /* If MLX4_PMD_SGE_WR_N is 1, free mbuf immediately
1215 * and clear offset from WR ID. */
1216 if (elemof(elt->sges) == 1) {
1218 struct rte_mbuf *next = NEXT(buf);
1220 rte_pktmbuf_free_seg(buf);
1222 } while (buf != NULL);
1223 WR_ID(wr->wr_id).offset = 0;
1225 /* Set WR fields and fill SGE with linear buffer. */
1227 /* Sanity checks, only relevant with debugging
1229 assert(sge->addr == 0);
1230 assert(sge->length == 0);
1231 assert(sge->lkey == 0);
1233 sge->addr = (uintptr_t)&(*linear)[0];
1235 sge->lkey = txq->mr_linear->lkey;
1236 #if (MLX4_PMD_MAX_INLINE > 0) || defined(MLX4_PMD_SOFT_COUNTERS)
1240 /* Link WRs together for ibv_post_send(). */
1242 wr_next = &wr->next;
1243 #if MLX4_PMD_MAX_INLINE > 0
1244 if (sent_size <= txq->max_inline)
1245 wr->send_flags = IBV_SEND_INLINE;
1249 if (++elts_head >= elts_n)
1251 #ifdef MLX4_PMD_SOFT_COUNTERS
1252 /* Increment sent bytes counter. */
1253 txq->stats.obytes += sent_size;
1257 /* Take a shortcut if nothing must be sent. */
1258 if (unlikely(i == 0))
1260 #ifdef MLX4_PMD_SOFT_COUNTERS
1261 /* Increment sent packets counter. */
1262 txq->stats.opackets += i;
1265 /* The last WR is the only one asking for a completion event. */
1266 containerof(wr_next, mlx4_send_wr_t, next)->
1267 send_flags |= IBV_SEND_SIGNALED;
1268 err = mlx4_post_send(txq->qp, head.next, &bad_wr);
1269 if (unlikely(err)) {
1270 unsigned int unsent = 0;
1272 /* An error occurred, completion event is lost. Fix counters. */
1273 while (bad_wr != NULL) {
1274 struct txq_elt *elt =
1275 containerof(bad_wr, struct txq_elt, wr);
1276 mlx4_send_wr_t *wr = &elt->wr;
1277 mlx4_send_wr_t *next = wr->next;
1278 #if defined(MLX4_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
1282 assert(wr == bad_wr);
1283 /* Clean up TX element without freeing it, caller
1284 * should take care of this. */
1285 WR_ID(elt->wr.wr_id).offset = 0;
1286 #ifdef MLX4_PMD_SOFT_COUNTERS
1287 for (j = 0; ((int)j < wr->num_sge); ++j)
1288 txq->stats.obytes -= wr->sg_list[j].length;
1293 for (j = 0; ((int)j < wr->num_sge); ++j) {
1294 elt->sges[j].addr = 0;
1295 elt->sges[j].length = 0;
1296 elt->sges[j].lkey = 0;
1303 #ifdef MLX4_PMD_SOFT_COUNTERS
1304 txq->stats.opackets -= unsent;
1306 assert(i >= unsent);
1308 /* "Unsend" remaining packets. */
1309 elts_head -= unsent;
1310 if (elts_head >= elts_n)
1311 elts_head += elts_n;
1312 assert(elts_head < elts_n);
1313 DEBUG("%p: mlx4_post_send() failed, %u unprocessed WRs: %s",
1314 (void *)txq, unsent,
1315 ((err <= -1) ? "Internal error" : strerror(err)));
1318 txq->elts_head = elts_head;
1323 * Configure a TX queue.
1326 * Pointer to Ethernet device structure.
1328 * Pointer to TX queue structure.
1330 * Number of descriptors to configure in queue.
1332 * NUMA socket on which memory must be allocated.
1334 * Thresholds parameters.
1337 * 0 on success, errno value on failure.
1340 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
1341 unsigned int socket, const struct rte_eth_txconf *conf)
1343 struct priv *priv = dev->data->dev_private;
1349 struct ibv_qp_init_attr init;
1350 struct ibv_exp_qp_attr mod;
1354 (void)conf; /* Thresholds configuration (ignored). */
1355 if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
1356 ERROR("%p: invalid number of TX descriptors (must be a"
1357 " multiple of %d)", (void *)dev, desc);
1360 desc /= MLX4_PMD_SGE_WR_N;
1361 /* MRs will be registered in mp2mr[] later. */
1362 tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
1363 if (tmpl.cq == NULL) {
1365 ERROR("%p: CQ creation failure: %s",
1366 (void *)dev, strerror(ret));
1369 DEBUG("priv->device_attr.max_qp_wr is %d",
1370 priv->device_attr.max_qp_wr);
1371 DEBUG("priv->device_attr.max_sge is %d",
1372 priv->device_attr.max_sge);
1373 attr.init = (struct ibv_qp_init_attr){
1374 /* CQ to be associated with the send queue. */
1376 /* CQ to be associated with the receive queue. */
1379 /* Max number of outstanding WRs. */
1380 .max_send_wr = ((priv->device_attr.max_qp_wr < desc) ?
1381 priv->device_attr.max_qp_wr :
1383 /* Max number of scatter/gather elements in a WR. */
1384 .max_send_sge = ((priv->device_attr.max_sge <
1385 MLX4_PMD_SGE_WR_N) ?
1386 priv->device_attr.max_sge :
1388 #if MLX4_PMD_MAX_INLINE > 0
1389 .max_inline_data = MLX4_PMD_MAX_INLINE,
1392 .qp_type = IBV_QPT_RAW_PACKET,
1393 /* Do *NOT* enable this, completions events are managed per
1397 tmpl.qp = ibv_create_qp(priv->pd, &attr.init);
1398 if (tmpl.qp == NULL) {
1399 ret = (errno ? errno : EINVAL);
1400 ERROR("%p: QP creation failure: %s",
1401 (void *)dev, strerror(ret));
1404 #if MLX4_PMD_MAX_INLINE > 0
1405 /* ibv_create_qp() updates this value. */
1406 tmpl.max_inline = attr.init.cap.max_inline_data;
1408 attr.mod = (struct ibv_exp_qp_attr){
1409 /* Move the QP to this state. */
1410 .qp_state = IBV_QPS_INIT,
1411 /* Primary port number. */
1412 .port_num = priv->port
1414 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod,
1415 (IBV_EXP_QP_STATE | IBV_EXP_QP_PORT));
1417 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
1418 (void *)dev, strerror(ret));
1421 ret = txq_alloc_elts(&tmpl, desc);
1423 ERROR("%p: TXQ allocation failed: %s",
1424 (void *)dev, strerror(ret));
1427 attr.mod = (struct ibv_exp_qp_attr){
1428 .qp_state = IBV_QPS_RTR
1430 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1432 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
1433 (void *)dev, strerror(ret));
1436 attr.mod.qp_state = IBV_QPS_RTS;
1437 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1439 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
1440 (void *)dev, strerror(ret));
1443 /* Clean up txq in case we're reinitializing it. */
1444 DEBUG("%p: cleaning-up old txq just in case", (void *)txq);
1447 DEBUG("%p: txq updated with %p", (void *)txq, (void *)&tmpl);
1457 * DPDK callback to configure a TX queue.
1460 * Pointer to Ethernet device structure.
1464 * Number of descriptors to configure in queue.
1466 * NUMA socket on which memory must be allocated.
1468 * Thresholds parameters.
1471 * 0 on success, negative errno value on failure.
1474 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1475 unsigned int socket, const struct rte_eth_txconf *conf)
1477 struct priv *priv = dev->data->dev_private;
1478 struct txq *txq = (*priv->txqs)[idx];
1482 DEBUG("%p: configuring queue %u for %u descriptors",
1483 (void *)dev, idx, desc);
1484 if (idx >= priv->txqs_n) {
1485 ERROR("%p: queue index out of range (%u >= %u)",
1486 (void *)dev, idx, priv->txqs_n);
1491 DEBUG("%p: reusing already allocated queue index %u (%p)",
1492 (void *)dev, idx, (void *)txq);
1493 if (priv->started) {
1497 (*priv->txqs)[idx] = NULL;
1500 txq = rte_calloc_socket("TXQ", 1, sizeof(*txq), 0, socket);
1502 ERROR("%p: unable to allocate queue index %u",
1508 ret = txq_setup(dev, txq, desc, socket, conf);
1512 txq->stats.idx = idx;
1513 DEBUG("%p: adding TX queue %p to list",
1514 (void *)dev, (void *)txq);
1515 (*priv->txqs)[idx] = txq;
1516 /* Update send callback. */
1517 dev->tx_pkt_burst = mlx4_tx_burst;
1524 * DPDK callback to release a TX queue.
1527 * Generic TX queue pointer.
1530 mlx4_tx_queue_release(void *dpdk_txq)
1532 struct txq *txq = (struct txq *)dpdk_txq;
1540 for (i = 0; (i != priv->txqs_n); ++i)
1541 if ((*priv->txqs)[i] == txq) {
1542 DEBUG("%p: removing TX queue %p from list",
1543 (void *)priv->dev, (void *)txq);
1544 (*priv->txqs)[i] = NULL;
1552 /* RX queues handling. */
1555 * Allocate RX queue elements with scattered packets support.
1558 * Pointer to RX queue structure.
1560 * Number of elements to allocate.
1562 * If not NULL, fetch buffers from this array instead of allocating them
1563 * with rte_pktmbuf_alloc().
1566 * 0 on success, errno value on failure.
1569 rxq_alloc_elts_sp(struct rxq *rxq, unsigned int elts_n,
1570 struct rte_mbuf **pool)
1573 struct rxq_elt_sp (*elts)[elts_n] =
1574 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1579 ERROR("%p: can't allocate packets array", (void *)rxq);
1583 /* For each WR (packet). */
1584 for (i = 0; (i != elts_n); ++i) {
1586 struct rxq_elt_sp *elt = &(*elts)[i];
1587 struct ibv_recv_wr *wr = &elt->wr;
1588 struct ibv_sge (*sges)[(elemof(elt->sges))] = &elt->sges;
1590 /* These two arrays must have the same size. */
1591 assert(elemof(elt->sges) == elemof(elt->bufs));
1594 wr->next = &(*elts)[(i + 1)].wr;
1595 wr->sg_list = &(*sges)[0];
1596 wr->num_sge = elemof(*sges);
1597 /* For each SGE (segment). */
1598 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1599 struct ibv_sge *sge = &(*sges)[j];
1600 struct rte_mbuf *buf;
1604 assert(buf != NULL);
1605 rte_pktmbuf_reset(buf);
1607 buf = rte_pktmbuf_alloc(rxq->mp);
1609 assert(pool == NULL);
1610 ERROR("%p: empty mbuf pool", (void *)rxq);
1615 /* Headroom is reserved by rte_pktmbuf_alloc(). */
1616 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1617 /* Buffer is supposed to be empty. */
1618 assert(rte_pktmbuf_data_len(buf) == 0);
1619 assert(rte_pktmbuf_pkt_len(buf) == 0);
1620 /* sge->addr must be able to store a pointer. */
1621 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1623 /* The first SGE keeps its headroom. */
1624 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
1625 sge->length = (buf->buf_len -
1626 RTE_PKTMBUF_HEADROOM);
1628 /* Subsequent SGEs lose theirs. */
1629 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1630 SET_DATA_OFF(buf, 0);
1631 sge->addr = (uintptr_t)buf->buf_addr;
1632 sge->length = buf->buf_len;
1634 sge->lkey = rxq->mr->lkey;
1635 /* Redundant check for tailroom. */
1636 assert(sge->length == rte_pktmbuf_tailroom(buf));
1639 /* The last WR pointer must be NULL. */
1640 (*elts)[(i - 1)].wr.next = NULL;
1641 DEBUG("%p: allocated and configured %u WRs (%zu segments)",
1642 (void *)rxq, elts_n, (elts_n * elemof((*elts)[0].sges)));
1643 rxq->elts_n = elts_n;
1645 rxq->elts.sp = elts;
1650 assert(pool == NULL);
1651 for (i = 0; (i != elemof(*elts)); ++i) {
1653 struct rxq_elt_sp *elt = &(*elts)[i];
1655 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1656 struct rte_mbuf *buf = elt->bufs[j];
1659 rte_pktmbuf_free_seg(buf);
1664 DEBUG("%p: failed, freed everything", (void *)rxq);
1670 * Free RX queue elements with scattered packets support.
1673 * Pointer to RX queue structure.
1676 rxq_free_elts_sp(struct rxq *rxq)
1679 unsigned int elts_n = rxq->elts_n;
1680 struct rxq_elt_sp (*elts)[elts_n] = rxq->elts.sp;
1682 DEBUG("%p: freeing WRs", (void *)rxq);
1684 rxq->elts.sp = NULL;
1687 for (i = 0; (i != elemof(*elts)); ++i) {
1689 struct rxq_elt_sp *elt = &(*elts)[i];
1691 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1692 struct rte_mbuf *buf = elt->bufs[j];
1695 rte_pktmbuf_free_seg(buf);
1702 * Allocate RX queue elements.
1705 * Pointer to RX queue structure.
1707 * Number of elements to allocate.
1709 * If not NULL, fetch buffers from this array instead of allocating them
1710 * with rte_pktmbuf_alloc().
1713 * 0 on success, errno value on failure.
1716 rxq_alloc_elts(struct rxq *rxq, unsigned int elts_n, struct rte_mbuf **pool)
1719 struct rxq_elt (*elts)[elts_n] =
1720 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1725 ERROR("%p: can't allocate packets array", (void *)rxq);
1729 /* For each WR (packet). */
1730 for (i = 0; (i != elts_n); ++i) {
1731 struct rxq_elt *elt = &(*elts)[i];
1732 struct ibv_recv_wr *wr = &elt->wr;
1733 struct ibv_sge *sge = &(*elts)[i].sge;
1734 struct rte_mbuf *buf;
1738 assert(buf != NULL);
1739 rte_pktmbuf_reset(buf);
1741 buf = rte_pktmbuf_alloc(rxq->mp);
1743 assert(pool == NULL);
1744 ERROR("%p: empty mbuf pool", (void *)rxq);
1748 /* Configure WR. Work request ID contains its own index in
1749 * the elts array and the offset between SGE buffer header and
1751 WR_ID(wr->wr_id).id = i;
1752 WR_ID(wr->wr_id).offset =
1753 (((uintptr_t)buf->buf_addr + RTE_PKTMBUF_HEADROOM) -
1755 wr->next = &(*elts)[(i + 1)].wr;
1758 /* Headroom is reserved by rte_pktmbuf_alloc(). */
1759 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1760 /* Buffer is supposed to be empty. */
1761 assert(rte_pktmbuf_data_len(buf) == 0);
1762 assert(rte_pktmbuf_pkt_len(buf) == 0);
1763 /* sge->addr must be able to store a pointer. */
1764 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1765 /* SGE keeps its headroom. */
1766 sge->addr = (uintptr_t)
1767 ((uint8_t *)buf->buf_addr + RTE_PKTMBUF_HEADROOM);
1768 sge->length = (buf->buf_len - RTE_PKTMBUF_HEADROOM);
1769 sge->lkey = rxq->mr->lkey;
1770 /* Redundant check for tailroom. */
1771 assert(sge->length == rte_pktmbuf_tailroom(buf));
1772 /* Make sure elts index and SGE mbuf pointer can be deduced
1774 if ((WR_ID(wr->wr_id).id != i) ||
1775 ((void *)((uintptr_t)sge->addr -
1776 WR_ID(wr->wr_id).offset) != buf)) {
1777 ERROR("%p: cannot store index and offset in WR ID",
1780 rte_pktmbuf_free(buf);
1785 /* The last WR pointer must be NULL. */
1786 (*elts)[(i - 1)].wr.next = NULL;
1787 DEBUG("%p: allocated and configured %u single-segment WRs",
1788 (void *)rxq, elts_n);
1789 rxq->elts_n = elts_n;
1791 rxq->elts.no_sp = elts;
1796 assert(pool == NULL);
1797 for (i = 0; (i != elemof(*elts)); ++i) {
1798 struct rxq_elt *elt = &(*elts)[i];
1799 struct rte_mbuf *buf;
1801 if (elt->sge.addr == 0)
1803 assert(WR_ID(elt->wr.wr_id).id == i);
1804 buf = (void *)((uintptr_t)elt->sge.addr -
1805 WR_ID(elt->wr.wr_id).offset);
1806 rte_pktmbuf_free_seg(buf);
1810 DEBUG("%p: failed, freed everything", (void *)rxq);
1816 * Free RX queue elements.
1819 * Pointer to RX queue structure.
1822 rxq_free_elts(struct rxq *rxq)
1825 unsigned int elts_n = rxq->elts_n;
1826 struct rxq_elt (*elts)[elts_n] = rxq->elts.no_sp;
1828 DEBUG("%p: freeing WRs", (void *)rxq);
1830 rxq->elts.no_sp = NULL;
1833 for (i = 0; (i != elemof(*elts)); ++i) {
1834 struct rxq_elt *elt = &(*elts)[i];
1835 struct rte_mbuf *buf;
1837 if (elt->sge.addr == 0)
1839 assert(WR_ID(elt->wr.wr_id).id == i);
1840 buf = (void *)((uintptr_t)elt->sge.addr -
1841 WR_ID(elt->wr.wr_id).offset);
1842 rte_pktmbuf_free_seg(buf);
1848 * Unregister a MAC address from a RX queue.
1851 * Pointer to RX queue structure.
1853 * MAC address index.
1856 rxq_mac_addr_del(struct rxq *rxq, unsigned int mac_index)
1859 struct priv *priv = rxq->priv;
1860 const uint8_t (*mac)[ETHER_ADDR_LEN] =
1861 (const uint8_t (*)[ETHER_ADDR_LEN])
1862 priv->mac[mac_index].addr_bytes;
1865 assert(mac_index < elemof(priv->mac));
1866 if (!BITFIELD_ISSET(rxq->mac_configured, mac_index)) {
1867 assert(rxq->mac_flow[mac_index] == NULL);
1870 DEBUG("%p: removing MAC address %02x:%02x:%02x:%02x:%02x:%02x"
1873 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
1875 assert(rxq->mac_flow[mac_index] != NULL);
1876 claim_zero(ibv_exp_destroy_flow(rxq->mac_flow[mac_index]));
1877 rxq->mac_flow[mac_index] = NULL;
1878 BITFIELD_RESET(rxq->mac_configured, mac_index);
1882 * Unregister all MAC addresses from a RX queue.
1885 * Pointer to RX queue structure.
1888 rxq_mac_addrs_del(struct rxq *rxq)
1890 struct priv *priv = rxq->priv;
1893 for (i = 0; (i != elemof(priv->mac)); ++i)
1894 rxq_mac_addr_del(rxq, i);
1897 static int rxq_promiscuous_enable(struct rxq *);
1898 static void rxq_promiscuous_disable(struct rxq *);
1901 * Register a MAC address in a RX queue.
1904 * Pointer to RX queue structure.
1906 * MAC address index to register.
1909 * 0 on success, errno value on failure.
1912 rxq_mac_addr_add(struct rxq *rxq, unsigned int mac_index)
1914 struct priv *priv = rxq->priv;
1915 const uint8_t (*mac)[ETHER_ADDR_LEN] =
1916 (const uint8_t (*)[ETHER_ADDR_LEN])
1917 priv->mac[mac_index].addr_bytes;
1918 unsigned int vlans = 0;
1919 unsigned int specs = 0;
1921 struct ibv_exp_flow *flow;
1923 assert(mac_index < elemof(priv->mac));
1924 if (BITFIELD_ISSET(rxq->mac_configured, mac_index))
1925 rxq_mac_addr_del(rxq, mac_index);
1926 /* Number of configured VLANs. */
1927 for (i = 0; (i != elemof(priv->vlan_filter)); ++i)
1928 if (priv->vlan_filter[i].enabled)
1930 specs = (vlans ? vlans : 1);
1932 /* Allocate flow specification on the stack. */
1933 struct ibv_exp_flow_attr data
1935 (sizeof(struct ibv_exp_flow_spec_eth[specs]) /
1936 sizeof(struct ibv_exp_flow_attr)) +
1937 !!(sizeof(struct ibv_exp_flow_spec_eth[specs]) %
1938 sizeof(struct ibv_exp_flow_attr))];
1939 struct ibv_exp_flow_attr *attr = (void *)&data[0];
1940 struct ibv_exp_flow_spec_eth *spec = (void *)&data[1];
1943 * No padding must be inserted by the compiler between attr and spec.
1944 * This layout is expected by libibverbs.
1946 assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec);
1947 *attr = (struct ibv_exp_flow_attr){
1948 .type = IBV_EXP_FLOW_ATTR_NORMAL,
1949 .num_of_specs = specs,
1953 *spec = (struct ibv_exp_flow_spec_eth){
1954 .type = IBV_EXP_FLOW_SPEC_ETH,
1955 .size = sizeof(*spec),
1958 (*mac)[0], (*mac)[1], (*mac)[2],
1959 (*mac)[3], (*mac)[4], (*mac)[5]
1963 .dst_mac = "\xff\xff\xff\xff\xff\xff",
1964 .vlan_tag = (vlans ? htons(0xfff) : 0)
1967 /* Fill VLAN specifications. */
1968 for (i = 0, j = 0; (i != elemof(priv->vlan_filter)); ++i) {
1969 if (!priv->vlan_filter[i].enabled)
1974 spec[j].val.vlan_tag = htons(priv->vlan_filter[i].id);
1977 DEBUG("%p: adding MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
1978 " (%u VLAN(s) configured)",
1980 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
1983 /* Create related flow. */
1985 flow = ibv_exp_create_flow(rxq->qp, attr);
1989 /* Flow creation failure is not fatal when in DMFS A0 mode.
1990 * Ignore error if promiscuity is already enabled or can be
1992 if (priv->promisc_ok)
1994 if ((rxq->promisc_flow != NULL) ||
1995 (rxq_promiscuous_enable(rxq) == 0)) {
1996 if (rxq->promisc_flow != NULL)
1997 rxq_promiscuous_disable(rxq);
1998 WARN("cannot configure normal flow but promiscuous"
1999 " mode is fine, assuming promiscuous optimization"
2001 " (options mlx4_core log_num_mgm_entry_size=-7)");
2002 priv->promisc_ok = 1;
2006 /* It's not clear whether errno is always set in this case. */
2007 ERROR("%p: flow configuration failed, errno=%d: %s",
2009 (errno ? strerror(errno) : "Unknown error"));
2014 assert(rxq->mac_flow[mac_index] == NULL);
2015 rxq->mac_flow[mac_index] = flow;
2016 BITFIELD_SET(rxq->mac_configured, mac_index);
2021 * Register all MAC addresses in a RX queue.
2024 * Pointer to RX queue structure.
2027 * 0 on success, errno value on failure.
2030 rxq_mac_addrs_add(struct rxq *rxq)
2032 struct priv *priv = rxq->priv;
2036 for (i = 0; (i != elemof(priv->mac)); ++i) {
2037 if (!BITFIELD_ISSET(priv->mac_configured, i))
2039 ret = rxq_mac_addr_add(rxq, i);
2042 /* Failure, rollback. */
2044 rxq_mac_addr_del(rxq, --i);
2052 * Unregister a MAC address.
2054 * In RSS mode, the MAC address is unregistered from the parent queue,
2055 * otherwise it is unregistered from each queue directly.
2058 * Pointer to private structure.
2060 * MAC address index.
2063 priv_mac_addr_del(struct priv *priv, unsigned int mac_index)
2067 assert(mac_index < elemof(priv->mac));
2068 if (!BITFIELD_ISSET(priv->mac_configured, mac_index))
2071 rxq_mac_addr_del(&priv->rxq_parent, mac_index);
2074 for (i = 0; (i != priv->dev->data->nb_rx_queues); ++i)
2075 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2077 BITFIELD_RESET(priv->mac_configured, mac_index);
2081 * Register a MAC address.
2083 * In RSS mode, the MAC address is registered in the parent queue,
2084 * otherwise it is registered in each queue directly.
2087 * Pointer to private structure.
2089 * MAC address index to use.
2091 * MAC address to register.
2094 * 0 on success, errno value on failure.
2097 priv_mac_addr_add(struct priv *priv, unsigned int mac_index,
2098 const uint8_t (*mac)[ETHER_ADDR_LEN])
2103 assert(mac_index < elemof(priv->mac));
2104 /* First, make sure this address isn't already configured. */
2105 for (i = 0; (i != elemof(priv->mac)); ++i) {
2106 /* Skip this index, it's going to be reconfigured. */
2109 if (!BITFIELD_ISSET(priv->mac_configured, i))
2111 if (memcmp(priv->mac[i].addr_bytes, *mac, sizeof(*mac)))
2113 /* Address already configured elsewhere, return with error. */
2116 if (BITFIELD_ISSET(priv->mac_configured, mac_index))
2117 priv_mac_addr_del(priv, mac_index);
2118 priv->mac[mac_index] = (struct ether_addr){
2120 (*mac)[0], (*mac)[1], (*mac)[2],
2121 (*mac)[3], (*mac)[4], (*mac)[5]
2124 /* If device isn't started, this is all we need to do. */
2125 if (!priv->started) {
2127 /* Verify that all queues have this index disabled. */
2128 for (i = 0; (i != priv->rxqs_n); ++i) {
2129 if ((*priv->rxqs)[i] == NULL)
2131 assert(!BITFIELD_ISSET
2132 ((*priv->rxqs)[i]->mac_configured, mac_index));
2138 ret = rxq_mac_addr_add(&priv->rxq_parent, mac_index);
2143 for (i = 0; (i != priv->rxqs_n); ++i) {
2144 if ((*priv->rxqs)[i] == NULL)
2146 ret = rxq_mac_addr_add((*priv->rxqs)[i], mac_index);
2149 /* Failure, rollback. */
2151 if ((*priv->rxqs)[(--i)] != NULL)
2152 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2156 BITFIELD_SET(priv->mac_configured, mac_index);
2161 * Enable allmulti mode in a RX queue.
2164 * Pointer to RX queue structure.
2167 * 0 on success, errno value on failure.
2170 rxq_allmulticast_enable(struct rxq *rxq)
2172 struct ibv_exp_flow *flow;
2173 struct ibv_exp_flow_attr attr = {
2174 .type = IBV_EXP_FLOW_ATTR_MC_DEFAULT,
2176 .port = rxq->priv->port,
2180 DEBUG("%p: enabling allmulticast mode", (void *)rxq);
2181 if (rxq->allmulti_flow != NULL)
2184 flow = ibv_exp_create_flow(rxq->qp, &attr);
2186 /* It's not clear whether errno is always set in this case. */
2187 ERROR("%p: flow configuration failed, errno=%d: %s",
2189 (errno ? strerror(errno) : "Unknown error"));
2194 rxq->allmulti_flow = flow;
2195 DEBUG("%p: allmulticast mode enabled", (void *)rxq);
2200 * Disable allmulti mode in a RX queue.
2203 * Pointer to RX queue structure.
2206 rxq_allmulticast_disable(struct rxq *rxq)
2208 DEBUG("%p: disabling allmulticast mode", (void *)rxq);
2209 if (rxq->allmulti_flow == NULL)
2211 claim_zero(ibv_exp_destroy_flow(rxq->allmulti_flow));
2212 rxq->allmulti_flow = NULL;
2213 DEBUG("%p: allmulticast mode disabled", (void *)rxq);
2217 * Enable promiscuous mode in a RX queue.
2220 * Pointer to RX queue structure.
2223 * 0 on success, errno value on failure.
2226 rxq_promiscuous_enable(struct rxq *rxq)
2228 struct ibv_exp_flow *flow;
2229 struct ibv_exp_flow_attr attr = {
2230 .type = IBV_EXP_FLOW_ATTR_ALL_DEFAULT,
2232 .port = rxq->priv->port,
2238 DEBUG("%p: enabling promiscuous mode", (void *)rxq);
2239 if (rxq->promisc_flow != NULL)
2242 flow = ibv_exp_create_flow(rxq->qp, &attr);
2244 /* It's not clear whether errno is always set in this case. */
2245 ERROR("%p: flow configuration failed, errno=%d: %s",
2247 (errno ? strerror(errno) : "Unknown error"));
2252 rxq->promisc_flow = flow;
2253 DEBUG("%p: promiscuous mode enabled", (void *)rxq);
2258 * Disable promiscuous mode in a RX queue.
2261 * Pointer to RX queue structure.
2264 rxq_promiscuous_disable(struct rxq *rxq)
2268 DEBUG("%p: disabling promiscuous mode", (void *)rxq);
2269 if (rxq->promisc_flow == NULL)
2271 claim_zero(ibv_exp_destroy_flow(rxq->promisc_flow));
2272 rxq->promisc_flow = NULL;
2273 DEBUG("%p: promiscuous mode disabled", (void *)rxq);
2277 * Clean up a RX queue.
2279 * Destroy objects, free allocated memory and reset the structure for reuse.
2282 * Pointer to RX queue structure.
2285 rxq_cleanup(struct rxq *rxq)
2287 DEBUG("cleaning up %p", (void *)rxq);
2289 rxq_free_elts_sp(rxq);
2292 if (rxq->qp != NULL) {
2293 rxq_promiscuous_disable(rxq);
2294 rxq_allmulticast_disable(rxq);
2295 rxq_mac_addrs_del(rxq);
2296 claim_zero(ibv_destroy_qp(rxq->qp));
2298 if (rxq->cq != NULL)
2299 claim_zero(ibv_destroy_cq(rxq->cq));
2300 if (rxq->mr != NULL)
2301 claim_zero(ibv_dereg_mr(rxq->mr));
2302 memset(rxq, 0, sizeof(*rxq));
2306 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n);
2309 * DPDK callback for RX with scattered packets support.
2312 * Generic pointer to RX queue structure.
2314 * Array to store received packets.
2316 * Maximum number of packets in array.
2319 * Number of packets successfully received (<= pkts_n).
2322 mlx4_rx_burst_sp(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2324 struct rxq *rxq = (struct rxq *)dpdk_rxq;
2325 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
2326 const unsigned int elts_n = rxq->elts_n;
2327 unsigned int elts_head = rxq->elts_head;
2328 struct ibv_wc wcs[pkts_n];
2329 struct ibv_recv_wr head;
2330 struct ibv_recv_wr **next = &head.next;
2331 struct ibv_recv_wr *bad_wr;
2336 if (unlikely(!rxq->sp))
2337 return mlx4_rx_burst(dpdk_rxq, pkts, pkts_n);
2338 if (unlikely(elts == NULL)) /* See RTE_DEV_CMD_SET_MTU. */
2340 wcs_n = ibv_poll_cq(rxq->cq, pkts_n, wcs);
2341 if (unlikely(wcs_n == 0))
2343 if (unlikely(wcs_n < 0)) {
2344 DEBUG("rxq=%p, ibv_poll_cq() failed (wc_n=%d)",
2345 (void *)rxq, wcs_n);
2348 assert(wcs_n <= (int)pkts_n);
2349 /* For each work completion. */
2350 for (i = 0; (i != wcs_n); ++i) {
2351 struct ibv_wc *wc = &wcs[i];
2352 uint64_t wr_id = wc->wr_id;
2353 uint32_t len = wc->byte_len;
2354 struct rxq_elt_sp *elt = &(*elts)[elts_head];
2355 struct ibv_recv_wr *wr = &elt->wr;
2356 struct rte_mbuf *pkt_buf = NULL; /* Buffer returned in pkts. */
2357 struct rte_mbuf **pkt_buf_next = &pkt_buf;
2358 unsigned int seg_headroom = RTE_PKTMBUF_HEADROOM;
2361 /* Sanity checks. */
2365 assert(wr_id < rxq->elts_n);
2366 assert(wr_id == wr->wr_id);
2367 assert(wr->sg_list == elt->sges);
2368 assert(wr->num_sge == elemof(elt->sges));
2369 assert(elts_head < rxq->elts_n);
2370 assert(rxq->elts_head < rxq->elts_n);
2371 /* Link completed WRs together for repost. */
2374 if (unlikely(wc->status != IBV_WC_SUCCESS)) {
2375 /* Whatever, just repost the offending WR. */
2376 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work completion"
2378 (void *)rxq, wc->wr_id, wc->status,
2379 ibv_wc_status_str(wc->status));
2380 #ifdef MLX4_PMD_SOFT_COUNTERS
2381 /* Increase dropped packets counter. */
2382 ++rxq->stats.idropped;
2387 * Replace spent segments with new ones, concatenate and
2388 * return them as pkt_buf.
2391 struct ibv_sge *sge = &elt->sges[j];
2392 struct rte_mbuf *seg = elt->bufs[j];
2393 struct rte_mbuf *rep;
2394 unsigned int seg_tailroom;
2397 * Fetch initial bytes of packet descriptor into a
2398 * cacheline while allocating rep.
2401 rep = __rte_mbuf_raw_alloc(rxq->mp);
2402 if (unlikely(rep == NULL)) {
2404 * Unable to allocate a replacement mbuf,
2407 DEBUG("rxq=%p, wr_id=%" PRIu64 ":"
2408 " can't allocate a new mbuf",
2409 (void *)rxq, wr_id);
2410 if (pkt_buf != NULL) {
2411 *pkt_buf_next = NULL;
2412 rte_pktmbuf_free(pkt_buf);
2414 /* Increase out of memory counters. */
2415 ++rxq->stats.rx_nombuf;
2416 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
2420 /* Poison user-modifiable fields in rep. */
2421 NEXT(rep) = (void *)((uintptr_t)-1);
2422 SET_DATA_OFF(rep, 0xdead);
2423 DATA_LEN(rep) = 0xd00d;
2424 PKT_LEN(rep) = 0xdeadd00d;
2425 NB_SEGS(rep) = 0x2a;
2429 assert(rep->buf_len == seg->buf_len);
2430 assert(rep->buf_len == rxq->mb_len);
2431 /* Reconfigure sge to use rep instead of seg. */
2432 assert(sge->lkey == rxq->mr->lkey);
2433 sge->addr = ((uintptr_t)rep->buf_addr + seg_headroom);
2436 /* Update pkt_buf if it's the first segment, or link
2437 * seg to the previous one and update pkt_buf_next. */
2438 *pkt_buf_next = seg;
2439 pkt_buf_next = &NEXT(seg);
2440 /* Update seg information. */
2441 seg_tailroom = (seg->buf_len - seg_headroom);
2442 assert(sge->length == seg_tailroom);
2443 SET_DATA_OFF(seg, seg_headroom);
2444 if (likely(len <= seg_tailroom)) {
2446 DATA_LEN(seg) = len;
2449 assert(rte_pktmbuf_headroom(seg) ==
2451 assert(rte_pktmbuf_tailroom(seg) ==
2452 (seg_tailroom - len));
2455 DATA_LEN(seg) = seg_tailroom;
2456 PKT_LEN(seg) = seg_tailroom;
2458 assert(rte_pktmbuf_headroom(seg) == seg_headroom);
2459 assert(rte_pktmbuf_tailroom(seg) == 0);
2460 /* Fix len and clear headroom for next segments. */
2461 len -= seg_tailroom;
2464 /* Update head and tail segments. */
2465 *pkt_buf_next = NULL;
2466 assert(pkt_buf != NULL);
2468 NB_SEGS(pkt_buf) = j;
2469 PORT(pkt_buf) = rxq->port_id;
2470 PKT_LEN(pkt_buf) = wc->byte_len;
2471 pkt_buf->ol_flags = 0;
2473 /* Return packet. */
2474 *(pkts++) = pkt_buf;
2476 #ifdef MLX4_PMD_SOFT_COUNTERS
2477 /* Increase bytes counter. */
2478 rxq->stats.ibytes += wc->byte_len;
2481 if (++elts_head >= elts_n)
2488 DEBUG("%p: reposting %d WRs starting from %" PRIu64 " (%p)",
2489 (void *)rxq, wcs_n, wcs[0].wr_id, (void *)head.next);
2491 i = ibv_post_recv(rxq->qp, head.next, &bad_wr);
2493 /* Inability to repost WRs is fatal. */
2494 DEBUG("%p: ibv_post_recv(): failed for WR %p: %s",
2500 rxq->elts_head = elts_head;
2501 #ifdef MLX4_PMD_SOFT_COUNTERS
2502 /* Increase packets counter. */
2503 rxq->stats.ipackets += ret;
2509 * DPDK callback for RX.
2511 * The following function is the same as mlx4_rx_burst_sp(), except it doesn't
2512 * manage scattered packets. Improves performance when MRU is lower than the
2513 * size of the first segment.
2516 * Generic pointer to RX queue structure.
2518 * Array to store received packets.
2520 * Maximum number of packets in array.
2523 * Number of packets successfully received (<= pkts_n).
2526 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2528 struct rxq *rxq = (struct rxq *)dpdk_rxq;
2529 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
2530 const unsigned int elts_n = rxq->elts_n;
2531 unsigned int elts_head = rxq->elts_head;
2532 struct ibv_wc wcs[pkts_n];
2533 struct ibv_recv_wr head;
2534 struct ibv_recv_wr **next = &head.next;
2535 struct ibv_recv_wr *bad_wr;
2540 if (unlikely(rxq->sp))
2541 return mlx4_rx_burst_sp(dpdk_rxq, pkts, pkts_n);
2542 wcs_n = ibv_poll_cq(rxq->cq, pkts_n, wcs);
2543 if (unlikely(wcs_n == 0))
2545 if (unlikely(wcs_n < 0)) {
2546 DEBUG("rxq=%p, ibv_poll_cq() failed (wc_n=%d)",
2547 (void *)rxq, wcs_n);
2550 assert(wcs_n <= (int)pkts_n);
2551 /* For each work completion. */
2552 for (i = 0; (i != wcs_n); ++i) {
2553 struct ibv_wc *wc = &wcs[i];
2554 uint64_t wr_id = wc->wr_id;
2555 uint32_t len = wc->byte_len;
2556 struct rxq_elt *elt = &(*elts)[elts_head];
2557 struct ibv_recv_wr *wr = &elt->wr;
2558 struct rte_mbuf *seg = (void *)((uintptr_t)elt->sge.addr -
2559 WR_ID(wr_id).offset);
2560 struct rte_mbuf *rep;
2562 /* Sanity checks. */
2563 assert(WR_ID(wr_id).id < rxq->elts_n);
2564 assert(wr_id == wr->wr_id);
2565 assert(wr->sg_list == &elt->sge);
2566 assert(wr->num_sge == 1);
2567 assert(elts_head < rxq->elts_n);
2568 assert(rxq->elts_head < rxq->elts_n);
2569 /* Link completed WRs together for repost. */
2572 if (unlikely(wc->status != IBV_WC_SUCCESS)) {
2573 /* Whatever, just repost the offending WR. */
2574 DEBUG("rxq=%p, wr_id=%" PRIu32 ": bad work completion"
2576 (void *)rxq, WR_ID(wr_id).id, wc->status,
2577 ibv_wc_status_str(wc->status));
2578 #ifdef MLX4_PMD_SOFT_COUNTERS
2579 /* Increase dropped packets counter. */
2580 ++rxq->stats.idropped;
2585 * Fetch initial bytes of packet descriptor into a
2586 * cacheline while allocating rep.
2589 rep = __rte_mbuf_raw_alloc(rxq->mp);
2590 if (unlikely(rep == NULL)) {
2592 * Unable to allocate a replacement mbuf,
2595 DEBUG("rxq=%p, wr_id=%" PRIu32 ":"
2596 " can't allocate a new mbuf",
2597 (void *)rxq, WR_ID(wr_id).id);
2598 /* Increase out of memory counters. */
2599 ++rxq->stats.rx_nombuf;
2600 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
2604 /* Reconfigure sge to use rep instead of seg. */
2605 elt->sge.addr = (uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM;
2606 assert(elt->sge.lkey == rxq->mr->lkey);
2607 WR_ID(wr->wr_id).offset =
2608 (((uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM) -
2610 assert(WR_ID(wr->wr_id).id == WR_ID(wr_id).id);
2612 /* Update seg information. */
2613 SET_DATA_OFF(seg, RTE_PKTMBUF_HEADROOM);
2615 PORT(seg) = rxq->port_id;
2618 DATA_LEN(seg) = len;
2621 /* Return packet. */
2624 #ifdef MLX4_PMD_SOFT_COUNTERS
2625 /* Increase bytes counter. */
2626 rxq->stats.ibytes += wc->byte_len;
2629 if (++elts_head >= elts_n)
2636 DEBUG("%p: reposting %d WRs starting from %" PRIu32 " (%p)",
2637 (void *)rxq, wcs_n, WR_ID(wcs[0].wr_id).id, (void *)head.next);
2639 i = ibv_post_recv(rxq->qp, head.next, &bad_wr);
2641 /* Inability to repost WRs is fatal. */
2642 DEBUG("%p: ibv_post_recv(): failed for WR %p: %s",
2648 rxq->elts_head = elts_head;
2649 #ifdef MLX4_PMD_SOFT_COUNTERS
2650 /* Increase packets counter. */
2651 rxq->stats.ipackets += ret;
2659 * Allocate a Queue Pair in case inline receive is supported.
2662 * Pointer to private structure.
2664 * Completion queue to associate with QP.
2666 * Number of descriptors in QP (hint only).
2669 * QP pointer or NULL in case of error.
2671 static struct ibv_qp *
2672 rxq_setup_qp(struct priv *priv, struct ibv_cq *cq, uint16_t desc)
2674 struct ibv_exp_qp_init_attr attr = {
2675 /* CQ to be associated with the send queue. */
2677 /* CQ to be associated with the receive queue. */
2679 .max_inl_recv = priv->inl_recv_size,
2681 /* Max number of outstanding WRs. */
2682 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
2683 priv->device_attr.max_qp_wr :
2685 /* Max number of scatter/gather elements in a WR. */
2686 .max_recv_sge = ((priv->device_attr.max_sge <
2687 MLX4_PMD_SGE_WR_N) ?
2688 priv->device_attr.max_sge :
2691 .qp_type = IBV_QPT_RAW_PACKET,
2695 attr.comp_mask = IBV_EXP_QP_INIT_ATTR_PD;
2696 attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
2698 return ibv_exp_create_qp(priv->ctx, &attr);
2701 #else /* INLINE_RECV */
2704 * Allocate a Queue Pair.
2707 * Pointer to private structure.
2709 * Completion queue to associate with QP.
2711 * Number of descriptors in QP (hint only).
2714 * QP pointer or NULL in case of error.
2716 static struct ibv_qp *
2717 rxq_setup_qp(struct priv *priv, struct ibv_cq *cq, uint16_t desc)
2719 struct ibv_qp_init_attr attr = {
2720 /* CQ to be associated with the send queue. */
2722 /* CQ to be associated with the receive queue. */
2725 /* Max number of outstanding WRs. */
2726 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
2727 priv->device_attr.max_qp_wr :
2729 /* Max number of scatter/gather elements in a WR. */
2730 .max_recv_sge = ((priv->device_attr.max_sge <
2731 MLX4_PMD_SGE_WR_N) ?
2732 priv->device_attr.max_sge :
2735 .qp_type = IBV_QPT_RAW_PACKET
2738 return ibv_create_qp(priv->pd, &attr);
2741 #endif /* INLINE_RECV */
2746 * Allocate a RSS Queue Pair.
2749 * Pointer to private structure.
2751 * Completion queue to associate with QP.
2753 * Number of descriptors in QP (hint only).
2755 * If nonzero, create a parent QP, otherwise a child.
2758 * QP pointer or NULL in case of error.
2760 static struct ibv_qp *
2761 rxq_setup_qp_rss(struct priv *priv, struct ibv_cq *cq, uint16_t desc,
2764 struct ibv_exp_qp_init_attr attr = {
2765 /* CQ to be associated with the send queue. */
2767 /* CQ to be associated with the receive queue. */
2770 .max_inl_recv = priv->inl_recv_size,
2773 /* Max number of outstanding WRs. */
2774 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
2775 priv->device_attr.max_qp_wr :
2777 /* Max number of scatter/gather elements in a WR. */
2778 .max_recv_sge = ((priv->device_attr.max_sge <
2779 MLX4_PMD_SGE_WR_N) ?
2780 priv->device_attr.max_sge :
2783 .qp_type = IBV_QPT_RAW_PACKET,
2784 .comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
2785 IBV_EXP_QP_INIT_ATTR_QPG),
2790 attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
2793 attr.qpg.qpg_type = IBV_EXP_QPG_PARENT;
2794 /* TSS isn't necessary. */
2795 attr.qpg.parent_attrib.tss_child_count = 0;
2796 attr.qpg.parent_attrib.rss_child_count = priv->rxqs_n;
2797 DEBUG("initializing parent RSS queue");
2799 attr.qpg.qpg_type = IBV_EXP_QPG_CHILD_RX;
2800 attr.qpg.qpg_parent = priv->rxq_parent.qp;
2801 DEBUG("initializing child RSS queue");
2803 return ibv_exp_create_qp(priv->ctx, &attr);
2806 #endif /* RSS_SUPPORT */
2809 * Reconfigure a RX queue with new parameters.
2811 * rxq_rehash() does not allocate mbufs, which, if not done from the right
2812 * thread (such as a control thread), may corrupt the pool.
2813 * In case of failure, the queue is left untouched.
2816 * Pointer to Ethernet device structure.
2821 * 0 on success, errno value on failure.
2824 rxq_rehash(struct rte_eth_dev *dev, struct rxq *rxq)
2826 struct priv *priv = rxq->priv;
2827 struct rxq tmpl = *rxq;
2828 unsigned int mbuf_n;
2829 unsigned int desc_n;
2830 struct rte_mbuf **pool;
2832 struct ibv_exp_qp_attr mod;
2833 struct ibv_recv_wr *bad_wr;
2835 int parent = (rxq == &priv->rxq_parent);
2838 ERROR("%p: cannot rehash parent queue %p",
2839 (void *)dev, (void *)rxq);
2842 DEBUG("%p: rehashing queue %p", (void *)dev, (void *)rxq);
2843 /* Number of descriptors and mbufs currently allocated. */
2844 desc_n = (tmpl.elts_n * (tmpl.sp ? MLX4_PMD_SGE_WR_N : 1));
2846 /* Enable scattered packets support for this queue if necessary. */
2847 if ((dev->data->dev_conf.rxmode.jumbo_frame) &&
2848 (dev->data->dev_conf.rxmode.max_rx_pkt_len >
2849 (tmpl.mb_len - RTE_PKTMBUF_HEADROOM))) {
2851 desc_n /= MLX4_PMD_SGE_WR_N;
2854 DEBUG("%p: %s scattered packets support (%u WRs)",
2855 (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc_n);
2856 /* If scatter mode is the same as before, nothing to do. */
2857 if (tmpl.sp == rxq->sp) {
2858 DEBUG("%p: nothing to do", (void *)dev);
2861 /* Remove attached flows if RSS is disabled (no parent queue). */
2863 rxq_allmulticast_disable(&tmpl);
2864 rxq_promiscuous_disable(&tmpl);
2865 rxq_mac_addrs_del(&tmpl);
2866 /* Update original queue in case of failure. */
2867 rxq->allmulti_flow = tmpl.allmulti_flow;
2868 rxq->promisc_flow = tmpl.promisc_flow;
2869 memcpy(rxq->mac_configured, tmpl.mac_configured,
2870 sizeof(rxq->mac_configured));
2871 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
2873 /* From now on, any failure will render the queue unusable.
2874 * Reinitialize QP. */
2875 mod = (struct ibv_exp_qp_attr){ .qp_state = IBV_QPS_RESET };
2876 err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
2878 ERROR("%p: cannot reset QP: %s", (void *)dev, strerror(err));
2882 err = ibv_resize_cq(tmpl.cq, desc_n);
2884 ERROR("%p: cannot resize CQ: %s", (void *)dev, strerror(err));
2888 mod = (struct ibv_exp_qp_attr){
2889 /* Move the QP to this state. */
2890 .qp_state = IBV_QPS_INIT,
2891 /* Primary port number. */
2892 .port_num = priv->port
2894 err = ibv_exp_modify_qp(tmpl.qp, &mod,
2897 (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
2898 #endif /* RSS_SUPPORT */
2901 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
2902 (void *)dev, strerror(err));
2906 /* Reconfigure flows. Do not care for errors. */
2908 rxq_mac_addrs_add(&tmpl);
2910 rxq_promiscuous_enable(&tmpl);
2912 rxq_allmulticast_enable(&tmpl);
2913 /* Update original queue in case of failure. */
2914 rxq->allmulti_flow = tmpl.allmulti_flow;
2915 rxq->promisc_flow = tmpl.promisc_flow;
2916 memcpy(rxq->mac_configured, tmpl.mac_configured,
2917 sizeof(rxq->mac_configured));
2918 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
2920 /* Allocate pool. */
2921 pool = rte_malloc(__func__, (mbuf_n * sizeof(*pool)), 0);
2923 ERROR("%p: cannot allocate memory", (void *)dev);
2926 /* Snatch mbufs from original queue. */
2929 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
2931 for (i = 0; (i != elemof(*elts)); ++i) {
2932 struct rxq_elt_sp *elt = &(*elts)[i];
2935 for (j = 0; (j != elemof(elt->bufs)); ++j) {
2936 assert(elt->bufs[j] != NULL);
2937 pool[k++] = elt->bufs[j];
2941 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
2943 for (i = 0; (i != elemof(*elts)); ++i) {
2944 struct rxq_elt *elt = &(*elts)[i];
2945 struct rte_mbuf *buf = (void *)
2946 ((uintptr_t)elt->sge.addr -
2947 WR_ID(elt->wr.wr_id).offset);
2949 assert(WR_ID(elt->wr.wr_id).id == i);
2953 assert(k == mbuf_n);
2955 tmpl.elts.sp = NULL;
2956 assert((void *)&tmpl.elts.sp == (void *)&tmpl.elts.no_sp);
2958 rxq_alloc_elts_sp(&tmpl, desc_n, pool) :
2959 rxq_alloc_elts(&tmpl, desc_n, pool));
2961 ERROR("%p: cannot reallocate WRs, aborting", (void *)dev);
2966 assert(tmpl.elts_n == desc_n);
2967 assert(tmpl.elts.sp != NULL);
2969 /* Clean up original data. */
2971 rte_free(rxq->elts.sp);
2972 rxq->elts.sp = NULL;
2974 err = ibv_post_recv(tmpl.qp,
2976 &(*tmpl.elts.sp)[0].wr :
2977 &(*tmpl.elts.no_sp)[0].wr),
2980 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
2986 mod = (struct ibv_exp_qp_attr){
2987 .qp_state = IBV_QPS_RTR
2989 err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
2991 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
2992 (void *)dev, strerror(err));
3000 * Configure a RX queue.
3003 * Pointer to Ethernet device structure.
3005 * Pointer to RX queue structure.
3007 * Number of descriptors to configure in queue.
3009 * NUMA socket on which memory must be allocated.
3011 * Thresholds parameters.
3013 * Memory pool for buffer allocations.
3016 * 0 on success, errno value on failure.
3019 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
3020 unsigned int socket, const struct rte_eth_rxconf *conf,
3021 struct rte_mempool *mp)
3023 struct priv *priv = dev->data->dev_private;
3029 struct ibv_exp_qp_attr mod;
3030 struct ibv_recv_wr *bad_wr;
3031 struct rte_mbuf *buf;
3033 int parent = (rxq == &priv->rxq_parent);
3035 (void)conf; /* Thresholds configuration (ignored). */
3037 * If this is a parent queue, hardware must support RSS and
3038 * RSS must be enabled.
3040 assert((!parent) || ((priv->hw_rss) && (priv->rss)));
3042 /* Even if unused, ibv_create_cq() requires at least one
3047 if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
3048 ERROR("%p: invalid number of RX descriptors (must be a"
3049 " multiple of %d)", (void *)dev, desc);
3052 /* Get mbuf length. */
3053 buf = rte_pktmbuf_alloc(mp);
3055 ERROR("%p: unable to allocate mbuf", (void *)dev);
3058 tmpl.mb_len = buf->buf_len;
3059 assert((rte_pktmbuf_headroom(buf) +
3060 rte_pktmbuf_tailroom(buf)) == tmpl.mb_len);
3061 assert(rte_pktmbuf_headroom(buf) == RTE_PKTMBUF_HEADROOM);
3062 rte_pktmbuf_free(buf);
3063 /* Enable scattered packets support for this queue if necessary. */
3064 if ((dev->data->dev_conf.rxmode.jumbo_frame) &&
3065 (dev->data->dev_conf.rxmode.max_rx_pkt_len >
3066 (tmpl.mb_len - RTE_PKTMBUF_HEADROOM))) {
3068 desc /= MLX4_PMD_SGE_WR_N;
3070 DEBUG("%p: %s scattered packets support (%u WRs)",
3071 (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc);
3072 /* Use the entire RX mempool as the memory region. */
3073 tmpl.mr = ibv_reg_mr(priv->pd,
3074 (void *)mp->elt_va_start,
3075 (mp->elt_va_end - mp->elt_va_start),
3076 (IBV_ACCESS_LOCAL_WRITE |
3077 IBV_ACCESS_REMOTE_WRITE));
3078 if (tmpl.mr == NULL) {
3080 ERROR("%p: MR creation failure: %s",
3081 (void *)dev, strerror(ret));
3085 tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
3086 if (tmpl.cq == NULL) {
3088 ERROR("%p: CQ creation failure: %s",
3089 (void *)dev, strerror(ret));
3092 DEBUG("priv->device_attr.max_qp_wr is %d",
3093 priv->device_attr.max_qp_wr);
3094 DEBUG("priv->device_attr.max_sge is %d",
3095 priv->device_attr.max_sge);
3098 tmpl.qp = rxq_setup_qp_rss(priv, tmpl.cq, desc, parent);
3100 #endif /* RSS_SUPPORT */
3101 tmpl.qp = rxq_setup_qp(priv, tmpl.cq, desc);
3102 if (tmpl.qp == NULL) {
3103 ret = (errno ? errno : EINVAL);
3104 ERROR("%p: QP creation failure: %s",
3105 (void *)dev, strerror(ret));
3108 mod = (struct ibv_exp_qp_attr){
3109 /* Move the QP to this state. */
3110 .qp_state = IBV_QPS_INIT,
3111 /* Primary port number. */
3112 .port_num = priv->port
3114 ret = ibv_exp_modify_qp(tmpl.qp, &mod,
3117 (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
3118 #endif /* RSS_SUPPORT */
3121 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
3122 (void *)dev, strerror(ret));
3125 if ((parent) || (!priv->rss)) {
3126 /* Configure MAC and broadcast addresses. */
3127 ret = rxq_mac_addrs_add(&tmpl);
3129 ERROR("%p: QP flow attachment failed: %s",
3130 (void *)dev, strerror(ret));
3134 /* Allocate descriptors for RX queues, except for the RSS parent. */
3138 ret = rxq_alloc_elts_sp(&tmpl, desc, NULL);
3140 ret = rxq_alloc_elts(&tmpl, desc, NULL);
3142 ERROR("%p: RXQ allocation failed: %s",
3143 (void *)dev, strerror(ret));
3146 ret = ibv_post_recv(tmpl.qp,
3148 &(*tmpl.elts.sp)[0].wr :
3149 &(*tmpl.elts.no_sp)[0].wr),
3152 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
3159 mod = (struct ibv_exp_qp_attr){
3160 .qp_state = IBV_QPS_RTR
3162 ret = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3164 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
3165 (void *)dev, strerror(ret));
3169 tmpl.port_id = dev->data->port_id;
3170 DEBUG("%p: RTE port ID: %u", (void *)rxq, tmpl.port_id);
3171 /* Clean up rxq in case we're reinitializing it. */
3172 DEBUG("%p: cleaning-up old rxq just in case", (void *)rxq);
3175 DEBUG("%p: rxq updated with %p", (void *)rxq, (void *)&tmpl);
3185 * DPDK callback to configure a RX queue.
3188 * Pointer to Ethernet device structure.
3192 * Number of descriptors to configure in queue.
3194 * NUMA socket on which memory must be allocated.
3196 * Thresholds parameters.
3198 * Memory pool for buffer allocations.
3201 * 0 on success, negative errno value on failure.
3204 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
3205 unsigned int socket, const struct rte_eth_rxconf *conf,
3206 struct rte_mempool *mp)
3208 struct priv *priv = dev->data->dev_private;
3209 struct rxq *rxq = (*priv->rxqs)[idx];
3213 DEBUG("%p: configuring queue %u for %u descriptors",
3214 (void *)dev, idx, desc);
3215 if (idx >= priv->rxqs_n) {
3216 ERROR("%p: queue index out of range (%u >= %u)",
3217 (void *)dev, idx, priv->rxqs_n);
3222 DEBUG("%p: reusing already allocated queue index %u (%p)",
3223 (void *)dev, idx, (void *)rxq);
3224 if (priv->started) {
3228 (*priv->rxqs)[idx] = NULL;
3231 rxq = rte_calloc_socket("RXQ", 1, sizeof(*rxq), 0, socket);
3233 ERROR("%p: unable to allocate queue index %u",
3239 ret = rxq_setup(dev, rxq, desc, socket, conf, mp);
3243 rxq->stats.idx = idx;
3244 DEBUG("%p: adding RX queue %p to list",
3245 (void *)dev, (void *)rxq);
3246 (*priv->rxqs)[idx] = rxq;
3247 /* Update receive callback. */
3249 dev->rx_pkt_burst = mlx4_rx_burst_sp;
3251 dev->rx_pkt_burst = mlx4_rx_burst;
3258 * DPDK callback to release a RX queue.
3261 * Generic RX queue pointer.
3264 mlx4_rx_queue_release(void *dpdk_rxq)
3266 struct rxq *rxq = (struct rxq *)dpdk_rxq;
3274 assert(rxq != &priv->rxq_parent);
3275 for (i = 0; (i != priv->rxqs_n); ++i)
3276 if ((*priv->rxqs)[i] == rxq) {
3277 DEBUG("%p: removing RX queue %p from list",
3278 (void *)priv->dev, (void *)rxq);
3279 (*priv->rxqs)[i] = NULL;
3288 * DPDK callback to start the device.
3290 * Simulate device start by attaching all configured flows.
3293 * Pointer to Ethernet device structure.
3296 * 0 on success, negative errno value on failure.
3299 mlx4_dev_start(struct rte_eth_dev *dev)
3301 struct priv *priv = dev->data->dev_private;
3307 if (priv->started) {
3311 DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
3314 rxq = &priv->rxq_parent;
3317 rxq = (*priv->rxqs)[0];
3320 /* Iterate only once when RSS is enabled. */
3324 /* Ignore nonexistent RX queues. */
3327 ret = rxq_mac_addrs_add(rxq);
3328 if (!ret && priv->promisc)
3329 ret = rxq_promiscuous_enable(rxq);
3330 if (!ret && priv->allmulti)
3331 ret = rxq_allmulticast_enable(rxq);
3334 WARN("%p: QP flow attachment failed: %s",
3335 (void *)dev, strerror(ret));
3338 rxq = (*priv->rxqs)[--i];
3340 rxq_allmulticast_disable(rxq);
3341 rxq_promiscuous_disable(rxq);
3342 rxq_mac_addrs_del(rxq);
3347 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
3353 * DPDK callback to stop the device.
3355 * Simulate device stop by detaching all configured flows.
3358 * Pointer to Ethernet device structure.
3361 mlx4_dev_stop(struct rte_eth_dev *dev)
3363 struct priv *priv = dev->data->dev_private;
3369 if (!priv->started) {
3373 DEBUG("%p: detaching flows from all RX queues", (void *)dev);
3376 rxq = &priv->rxq_parent;
3379 rxq = (*priv->rxqs)[0];
3382 /* Iterate only once when RSS is enabled. */
3384 /* Ignore nonexistent RX queues. */
3387 rxq_allmulticast_disable(rxq);
3388 rxq_promiscuous_disable(rxq);
3389 rxq_mac_addrs_del(rxq);
3390 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
3395 * Dummy DPDK callback for TX.
3397 * This function is used to temporarily replace the real callback during
3398 * unsafe control operations on the queue, or in case of error.
3401 * Generic pointer to TX queue structure.
3403 * Packets to transmit.
3405 * Number of packets in array.
3408 * Number of packets successfully transmitted (<= pkts_n).
3411 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
3420 * Dummy DPDK callback for RX.
3422 * This function is used to temporarily replace the real callback during
3423 * unsafe control operations on the queue, or in case of error.
3426 * Generic pointer to RX queue structure.
3428 * Array to store received packets.
3430 * Maximum number of packets in array.
3433 * Number of packets successfully received (<= pkts_n).
3436 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
3445 * DPDK callback to close the device.
3447 * Destroy all queues and objects, free memory.
3450 * Pointer to Ethernet device structure.
3453 mlx4_dev_close(struct rte_eth_dev *dev)
3455 struct priv *priv = dev->data->dev_private;
3460 DEBUG("%p: closing device \"%s\"",
3462 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
3463 /* Prevent crashes when queues are still in use. This is unfortunately
3464 * still required for DPDK 1.3 because some programs (such as testpmd)
3465 * never release them before closing the device. */
3466 dev->rx_pkt_burst = removed_rx_burst;
3467 dev->tx_pkt_burst = removed_tx_burst;
3468 if (priv->rxqs != NULL) {
3469 /* XXX race condition if mlx4_rx_burst() is still running. */
3471 for (i = 0; (i != priv->rxqs_n); ++i) {
3472 tmp = (*priv->rxqs)[i];
3475 (*priv->rxqs)[i] = NULL;
3482 if (priv->txqs != NULL) {
3483 /* XXX race condition if mlx4_tx_burst() is still running. */
3485 for (i = 0; (i != priv->txqs_n); ++i) {
3486 tmp = (*priv->txqs)[i];
3489 (*priv->txqs)[i] = NULL;
3497 rxq_cleanup(&priv->rxq_parent);
3498 if (priv->pd != NULL) {
3499 assert(priv->ctx != NULL);
3500 claim_zero(ibv_dealloc_pd(priv->pd));
3501 claim_zero(ibv_close_device(priv->ctx));
3503 assert(priv->ctx == NULL);
3505 memset(priv, 0, sizeof(*priv));
3509 * DPDK callback to get information about the device.
3512 * Pointer to Ethernet device structure.
3514 * Info structure output buffer.
3517 mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
3519 struct priv *priv = dev->data->dev_private;
3523 /* FIXME: we should ask the device for these values. */
3524 info->min_rx_bufsize = 32;
3525 info->max_rx_pktlen = 65536;
3527 * Since we need one CQ per QP, the limit is the minimum number
3528 * between the two values.
3530 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
3531 priv->device_attr.max_qp : priv->device_attr.max_cq);
3532 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
3535 info->max_rx_queues = max;
3536 info->max_tx_queues = max;
3537 info->max_mac_addrs = elemof(priv->mac);
3542 * DPDK callback to get device statistics.
3545 * Pointer to Ethernet device structure.
3547 * Stats structure output buffer.
3550 mlx4_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
3552 struct priv *priv = dev->data->dev_private;
3553 struct rte_eth_stats tmp = {0};
3558 /* Add software counters. */
3559 for (i = 0; (i != priv->rxqs_n); ++i) {
3560 struct rxq *rxq = (*priv->rxqs)[i];
3564 idx = rxq->stats.idx;
3565 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
3566 #ifdef MLX4_PMD_SOFT_COUNTERS
3567 tmp.q_ipackets[idx] += rxq->stats.ipackets;
3568 tmp.q_ibytes[idx] += rxq->stats.ibytes;
3570 tmp.q_errors[idx] += (rxq->stats.idropped +
3571 rxq->stats.rx_nombuf);
3573 #ifdef MLX4_PMD_SOFT_COUNTERS
3574 tmp.ipackets += rxq->stats.ipackets;
3575 tmp.ibytes += rxq->stats.ibytes;
3577 tmp.ierrors += rxq->stats.idropped;
3578 tmp.rx_nombuf += rxq->stats.rx_nombuf;
3580 for (i = 0; (i != priv->txqs_n); ++i) {
3581 struct txq *txq = (*priv->txqs)[i];
3585 idx = txq->stats.idx;
3586 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
3587 #ifdef MLX4_PMD_SOFT_COUNTERS
3588 tmp.q_opackets[idx] += txq->stats.opackets;
3589 tmp.q_obytes[idx] += txq->stats.obytes;
3591 tmp.q_errors[idx] += txq->stats.odropped;
3593 #ifdef MLX4_PMD_SOFT_COUNTERS
3594 tmp.opackets += txq->stats.opackets;
3595 tmp.obytes += txq->stats.obytes;
3597 tmp.oerrors += txq->stats.odropped;
3599 #ifndef MLX4_PMD_SOFT_COUNTERS
3600 /* FIXME: retrieve and add hardware counters. */
3607 * DPDK callback to clear device statistics.
3610 * Pointer to Ethernet device structure.
3613 mlx4_stats_reset(struct rte_eth_dev *dev)
3615 struct priv *priv = dev->data->dev_private;
3620 for (i = 0; (i != priv->rxqs_n); ++i) {
3621 if ((*priv->rxqs)[i] == NULL)
3623 idx = (*priv->rxqs)[i]->stats.idx;
3624 (*priv->rxqs)[i]->stats =
3625 (struct mlx4_rxq_stats){ .idx = idx };
3627 for (i = 0; (i != priv->txqs_n); ++i) {
3628 if ((*priv->txqs)[i] == NULL)
3630 idx = (*priv->rxqs)[i]->stats.idx;
3631 (*priv->txqs)[i]->stats =
3632 (struct mlx4_txq_stats){ .idx = idx };
3634 #ifndef MLX4_PMD_SOFT_COUNTERS
3635 /* FIXME: reset hardware counters. */
3641 * DPDK callback to remove a MAC address.
3644 * Pointer to Ethernet device structure.
3646 * MAC address index.
3649 mlx4_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
3651 struct priv *priv = dev->data->dev_private;
3654 DEBUG("%p: removing MAC address from index %" PRIu32,
3655 (void *)dev, index);
3656 if (index >= MLX4_MAX_MAC_ADDRESSES)
3658 /* Refuse to remove the broadcast address, this one is special. */
3659 if (!memcmp(priv->mac[index].addr_bytes, "\xff\xff\xff\xff\xff\xff",
3662 priv_mac_addr_del(priv, index);
3668 * DPDK callback to add a MAC address.
3671 * Pointer to Ethernet device structure.
3673 * MAC address to register.
3675 * MAC address index.
3677 * VMDq pool index to associate address with (ignored).
3680 mlx4_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
3681 uint32_t index, uint32_t vmdq)
3683 struct priv *priv = dev->data->dev_private;
3687 DEBUG("%p: adding MAC address at index %" PRIu32,
3688 (void *)dev, index);
3689 if (index >= MLX4_MAX_MAC_ADDRESSES)
3691 /* Refuse to add the broadcast address, this one is special. */
3692 if (!memcmp(mac_addr->addr_bytes, "\xff\xff\xff\xff\xff\xff",
3695 priv_mac_addr_add(priv, index,
3696 (const uint8_t (*)[ETHER_ADDR_LEN])
3697 mac_addr->addr_bytes);
3703 * DPDK callback to enable promiscuous mode.
3706 * Pointer to Ethernet device structure.
3709 mlx4_promiscuous_enable(struct rte_eth_dev *dev)
3711 struct priv *priv = dev->data->dev_private;
3716 if (priv->promisc) {
3720 /* If device isn't started, this is all we need to do. */
3724 ret = rxq_promiscuous_enable(&priv->rxq_parent);
3731 for (i = 0; (i != priv->rxqs_n); ++i) {
3732 if ((*priv->rxqs)[i] == NULL)
3734 ret = rxq_promiscuous_enable((*priv->rxqs)[i]);
3737 /* Failure, rollback. */
3739 if ((*priv->rxqs)[--i] != NULL)
3740 rxq_promiscuous_disable((*priv->rxqs)[i]);
3750 * DPDK callback to disable promiscuous mode.
3753 * Pointer to Ethernet device structure.
3756 mlx4_promiscuous_disable(struct rte_eth_dev *dev)
3758 struct priv *priv = dev->data->dev_private;
3762 if (!priv->promisc) {
3767 rxq_promiscuous_disable(&priv->rxq_parent);
3770 for (i = 0; (i != priv->rxqs_n); ++i)
3771 if ((*priv->rxqs)[i] != NULL)
3772 rxq_promiscuous_disable((*priv->rxqs)[i]);
3779 * DPDK callback to enable allmulti mode.
3782 * Pointer to Ethernet device structure.
3785 mlx4_allmulticast_enable(struct rte_eth_dev *dev)
3787 struct priv *priv = dev->data->dev_private;
3792 if (priv->allmulti) {
3796 /* If device isn't started, this is all we need to do. */
3800 ret = rxq_allmulticast_enable(&priv->rxq_parent);
3807 for (i = 0; (i != priv->rxqs_n); ++i) {
3808 if ((*priv->rxqs)[i] == NULL)
3810 ret = rxq_allmulticast_enable((*priv->rxqs)[i]);
3813 /* Failure, rollback. */
3815 if ((*priv->rxqs)[--i] != NULL)
3816 rxq_allmulticast_disable((*priv->rxqs)[i]);
3826 * DPDK callback to disable allmulti mode.
3829 * Pointer to Ethernet device structure.
3832 mlx4_allmulticast_disable(struct rte_eth_dev *dev)
3834 struct priv *priv = dev->data->dev_private;
3838 if (!priv->allmulti) {
3843 rxq_allmulticast_disable(&priv->rxq_parent);
3846 for (i = 0; (i != priv->rxqs_n); ++i)
3847 if ((*priv->rxqs)[i] != NULL)
3848 rxq_allmulticast_disable((*priv->rxqs)[i]);
3855 * DPDK callback to retrieve physical link information (unlocked version).
3858 * Pointer to Ethernet device structure.
3859 * @param wait_to_complete
3860 * Wait for request completion (ignored).
3863 mlx4_link_update_unlocked(struct rte_eth_dev *dev, int wait_to_complete)
3865 struct priv *priv = dev->data->dev_private;
3866 struct ibv_port_attr port_attr;
3867 static const uint8_t width_mult[] = {
3868 /* Multiplier values taken from devinfo.c in libibverbs. */
3869 0, 1, 4, 0, 8, 0, 0, 0, 12, 0
3872 (void)wait_to_complete;
3873 errno = ibv_query_port(priv->ctx, priv->port, &port_attr);
3875 WARN("port query failed: %s", strerror(errno));
3878 dev->data->dev_link = (struct rte_eth_link){
3879 .link_speed = (ibv_rate_to_mbps(mult_to_ibv_rate
3880 (port_attr.active_speed)) *
3881 width_mult[(port_attr.active_width %
3882 sizeof(width_mult))]),
3883 .link_duplex = ETH_LINK_FULL_DUPLEX,
3884 .link_status = (port_attr.state == IBV_PORT_ACTIVE)
3886 if (memcmp(&port_attr, &priv->port_attr, sizeof(port_attr))) {
3887 /* Link status changed. */
3888 priv->port_attr = port_attr;
3891 /* Link status is still the same. */
3896 * DPDK callback to retrieve physical link information.
3899 * Pointer to Ethernet device structure.
3900 * @param wait_to_complete
3901 * Wait for request completion (ignored).
3904 mlx4_link_update(struct rte_eth_dev *dev, int wait_to_complete)
3906 struct priv *priv = dev->data->dev_private;
3910 ret = mlx4_link_update_unlocked(dev, wait_to_complete);
3916 * DPDK callback to change the MTU.
3918 * Setting the MTU affects hardware MRU (packets larger than the MTU cannot be
3919 * received). Use this as a hint to enable/disable scattered packets support
3920 * and improve performance when not needed.
3921 * Since failure is not an option, reconfiguring queues on the fly is not
3925 * Pointer to Ethernet device structure.
3930 * 0 on success, negative errno value on failure.
3933 mlx4_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
3935 struct priv *priv = dev->data->dev_private;
3938 uint16_t (*rx_func)(void *, struct rte_mbuf **, uint16_t) =
3942 /* Set kernel interface MTU first. */
3943 if (priv_set_mtu(priv, mtu)) {
3945 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
3949 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
3951 /* Temporarily replace RX handler with a fake one, assuming it has not
3952 * been copied elsewhere. */
3953 dev->rx_pkt_burst = removed_rx_burst;
3954 /* Make sure everyone has left mlx4_rx_burst() and uses
3955 * removed_rx_burst() instead. */
3958 /* Reconfigure each RX queue. */
3959 for (i = 0; (i != priv->rxqs_n); ++i) {
3960 struct rxq *rxq = (*priv->rxqs)[i];
3961 unsigned int max_frame_len;
3966 /* Calculate new maximum frame length according to MTU and
3967 * toggle scattered support (sp) if necessary. */
3968 max_frame_len = (priv->mtu + ETHER_HDR_LEN +
3969 (ETHER_MAX_VLAN_FRAME_LEN - ETHER_MAX_LEN));
3970 sp = (max_frame_len > (rxq->mb_len - RTE_PKTMBUF_HEADROOM));
3971 /* Provide new values to rxq_setup(). */
3972 dev->data->dev_conf.rxmode.jumbo_frame = sp;
3973 dev->data->dev_conf.rxmode.max_rx_pkt_len = max_frame_len;
3974 ret = rxq_rehash(dev, rxq);
3976 /* Force SP RX if that queue requires it and abort. */
3978 rx_func = mlx4_rx_burst_sp;
3981 /* Reenable non-RSS queue attributes. No need to check
3982 * for errors at this stage. */
3984 rxq_mac_addrs_add(rxq);
3986 rxq_promiscuous_enable(rxq);
3988 rxq_allmulticast_enable(rxq);
3990 /* Scattered burst function takes priority. */
3992 rx_func = mlx4_rx_burst_sp;
3994 /* Burst functions can now be called again. */
3996 dev->rx_pkt_burst = rx_func;
4004 * DPDK callback to get flow control status.
4007 * Pointer to Ethernet device structure.
4008 * @param[out] fc_conf
4009 * Flow control output buffer.
4012 * 0 on success, negative errno value on failure.
4015 mlx4_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4017 struct priv *priv = dev->data->dev_private;
4019 struct ethtool_pauseparam ethpause = {
4020 .cmd = ETHTOOL_GPAUSEPARAM
4024 ifr.ifr_data = ðpause;
4026 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4028 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
4034 fc_conf->autoneg = ethpause.autoneg;
4035 if (ethpause.rx_pause && ethpause.tx_pause)
4036 fc_conf->mode = RTE_FC_FULL;
4037 else if (ethpause.rx_pause)
4038 fc_conf->mode = RTE_FC_RX_PAUSE;
4039 else if (ethpause.tx_pause)
4040 fc_conf->mode = RTE_FC_TX_PAUSE;
4042 fc_conf->mode = RTE_FC_NONE;
4052 * DPDK callback to modify flow control parameters.
4055 * Pointer to Ethernet device structure.
4056 * @param[in] fc_conf
4057 * Flow control parameters.
4060 * 0 on success, negative errno value on failure.
4063 mlx4_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4065 struct priv *priv = dev->data->dev_private;
4067 struct ethtool_pauseparam ethpause = {
4068 .cmd = ETHTOOL_SPAUSEPARAM
4072 ifr.ifr_data = ðpause;
4073 ethpause.autoneg = fc_conf->autoneg;
4074 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4075 (fc_conf->mode & RTE_FC_RX_PAUSE))
4076 ethpause.rx_pause = 1;
4078 ethpause.rx_pause = 0;
4080 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4081 (fc_conf->mode & RTE_FC_TX_PAUSE))
4082 ethpause.tx_pause = 1;
4084 ethpause.tx_pause = 0;
4087 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4089 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
4103 * Configure a VLAN filter.
4106 * Pointer to Ethernet device structure.
4108 * VLAN ID to filter.
4113 * 0 on success, errno value on failure.
4116 vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
4118 struct priv *priv = dev->data->dev_private;
4120 unsigned int j = -1;
4122 DEBUG("%p: %s VLAN filter ID %" PRIu16,
4123 (void *)dev, (on ? "enable" : "disable"), vlan_id);
4124 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
4125 if (!priv->vlan_filter[i].enabled) {
4126 /* Unused index, remember it. */
4130 if (priv->vlan_filter[i].id != vlan_id)
4132 /* This VLAN ID is already known, use its index. */
4136 /* Check if there's room for another VLAN filter. */
4137 if (j == (unsigned int)-1)
4140 * VLAN filters apply to all configured MAC addresses, flow
4141 * specifications must be reconfigured accordingly.
4143 priv->vlan_filter[j].id = vlan_id;
4144 if ((on) && (!priv->vlan_filter[j].enabled)) {
4146 * Filter is disabled, enable it.
4147 * Rehashing flows in all RX queues is necessary.
4150 rxq_mac_addrs_del(&priv->rxq_parent);
4152 for (i = 0; (i != priv->rxqs_n); ++i)
4153 if ((*priv->rxqs)[i] != NULL)
4154 rxq_mac_addrs_del((*priv->rxqs)[i]);
4155 priv->vlan_filter[j].enabled = 1;
4156 if (priv->started) {
4158 rxq_mac_addrs_add(&priv->rxq_parent);
4160 for (i = 0; (i != priv->rxqs_n); ++i) {
4161 if ((*priv->rxqs)[i] == NULL)
4163 rxq_mac_addrs_add((*priv->rxqs)[i]);
4166 } else if ((!on) && (priv->vlan_filter[j].enabled)) {
4168 * Filter is enabled, disable it.
4169 * Rehashing flows in all RX queues is necessary.
4172 rxq_mac_addrs_del(&priv->rxq_parent);
4174 for (i = 0; (i != priv->rxqs_n); ++i)
4175 if ((*priv->rxqs)[i] != NULL)
4176 rxq_mac_addrs_del((*priv->rxqs)[i]);
4177 priv->vlan_filter[j].enabled = 0;
4178 if (priv->started) {
4180 rxq_mac_addrs_add(&priv->rxq_parent);
4182 for (i = 0; (i != priv->rxqs_n); ++i) {
4183 if ((*priv->rxqs)[i] == NULL)
4185 rxq_mac_addrs_add((*priv->rxqs)[i]);
4193 * DPDK callback to configure a VLAN filter.
4196 * Pointer to Ethernet device structure.
4198 * VLAN ID to filter.
4203 * 0 on success, negative errno value on failure.
4206 mlx4_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
4208 struct priv *priv = dev->data->dev_private;
4212 ret = vlan_filter_set(dev, vlan_id, on);
4218 static const struct eth_dev_ops mlx4_dev_ops = {
4219 .dev_configure = mlx4_dev_configure,
4220 .dev_start = mlx4_dev_start,
4221 .dev_stop = mlx4_dev_stop,
4222 .dev_close = mlx4_dev_close,
4223 .promiscuous_enable = mlx4_promiscuous_enable,
4224 .promiscuous_disable = mlx4_promiscuous_disable,
4225 .allmulticast_enable = mlx4_allmulticast_enable,
4226 .allmulticast_disable = mlx4_allmulticast_disable,
4227 .link_update = mlx4_link_update,
4228 .stats_get = mlx4_stats_get,
4229 .stats_reset = mlx4_stats_reset,
4230 .queue_stats_mapping_set = NULL,
4231 .dev_infos_get = mlx4_dev_infos_get,
4232 .vlan_filter_set = mlx4_vlan_filter_set,
4233 .vlan_tpid_set = NULL,
4234 .vlan_strip_queue_set = NULL,
4235 .vlan_offload_set = NULL,
4236 .rx_queue_setup = mlx4_rx_queue_setup,
4237 .tx_queue_setup = mlx4_tx_queue_setup,
4238 .rx_queue_release = mlx4_rx_queue_release,
4239 .tx_queue_release = mlx4_tx_queue_release,
4241 .dev_led_off = NULL,
4242 .flow_ctrl_get = mlx4_dev_get_flow_ctrl,
4243 .flow_ctrl_set = mlx4_dev_set_flow_ctrl,
4244 .priority_flow_ctrl_set = NULL,
4245 .mac_addr_remove = mlx4_mac_addr_remove,
4246 .mac_addr_add = mlx4_mac_addr_add,
4247 .mtu_set = mlx4_dev_set_mtu,
4248 .fdir_add_signature_filter = NULL,
4249 .fdir_update_signature_filter = NULL,
4250 .fdir_remove_signature_filter = NULL,
4251 .fdir_add_perfect_filter = NULL,
4252 .fdir_update_perfect_filter = NULL,
4253 .fdir_remove_perfect_filter = NULL,
4254 .fdir_set_masks = NULL
4258 * Get PCI information from struct ibv_device.
4261 * Pointer to Ethernet device structure.
4262 * @param[out] pci_addr
4263 * PCI bus address output buffer.
4266 * 0 on success, -1 on failure and errno is set.
4269 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
4270 struct rte_pci_addr *pci_addr)
4274 MKSTR(path, "%s/device/uevent", device->ibdev_path);
4276 file = fopen(path, "rb");
4279 while (fgets(line, sizeof(line), file) == line) {
4280 size_t len = strlen(line);
4283 /* Truncate long lines. */
4284 if (len == (sizeof(line) - 1))
4285 while (line[(len - 1)] != '\n') {
4289 line[(len - 1)] = ret;
4291 /* Extract information. */
4294 "%" SCNx16 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
4298 &pci_addr->function) == 4) {
4308 * Derive MAC address from port GID.
4311 * MAC address output buffer.
4313 * Physical port number.
4318 mac_from_gid(uint8_t (*mac)[ETHER_ADDR_LEN], uint32_t port, uint8_t *gid)
4320 memcpy(&(*mac)[0], gid + 8, 3);
4321 memcpy(&(*mac)[3], gid + 13, 3);
4326 /* Support up to 32 adapters. */
4328 struct rte_pci_addr pci_addr; /* associated PCI address */
4329 uint32_t ports; /* physical ports bitfield. */
4333 * Get device index in mlx4_dev[] from PCI bus address.
4335 * @param[in] pci_addr
4336 * PCI bus address to look for.
4339 * mlx4_dev[] index on success, -1 on failure.
4342 mlx4_dev_idx(struct rte_pci_addr *pci_addr)
4347 assert(pci_addr != NULL);
4348 for (i = 0; (i != elemof(mlx4_dev)); ++i) {
4349 if ((mlx4_dev[i].pci_addr.domain == pci_addr->domain) &&
4350 (mlx4_dev[i].pci_addr.bus == pci_addr->bus) &&
4351 (mlx4_dev[i].pci_addr.devid == pci_addr->devid) &&
4352 (mlx4_dev[i].pci_addr.function == pci_addr->function))
4354 if ((mlx4_dev[i].ports == 0) && (ret == -1))
4361 * Retrieve integer value from environment variable.
4364 * Environment variable name.
4367 * Integer value, 0 if the variable is not set.
4370 mlx4_getenv_int(const char *name)
4372 const char *val = getenv(name);
4379 static struct eth_driver mlx4_driver;
4382 * DPDK callback to register a PCI device.
4384 * This function creates an Ethernet device for each port of a given
4387 * @param[in] pci_drv
4388 * PCI driver structure (mlx4_driver).
4389 * @param[in] pci_dev
4390 * PCI device information.
4393 * 0 on success, negative errno value on failure.
4396 mlx4_pci_devinit(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
4398 struct ibv_device **list;
4399 struct ibv_device *ibv_dev;
4401 struct ibv_context *attr_ctx = NULL;
4402 struct ibv_device_attr device_attr;
4408 assert(pci_drv == &mlx4_driver.pci_drv);
4409 /* Get mlx4_dev[] index. */
4410 idx = mlx4_dev_idx(&pci_dev->addr);
4412 ERROR("this driver cannot support any more adapters");
4415 DEBUG("using driver device index %d", idx);
4417 /* Save PCI address. */
4418 mlx4_dev[idx].pci_addr = pci_dev->addr;
4419 list = ibv_get_device_list(&i);
4422 if (errno == ENOSYS) {
4423 WARN("cannot list devices, is ib_uverbs loaded?");
4430 * For each listed device, check related sysfs entry against
4431 * the provided PCI ID.
4434 struct rte_pci_addr pci_addr;
4437 DEBUG("checking device \"%s\"", list[i]->name);
4438 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
4440 if ((pci_dev->addr.domain != pci_addr.domain) ||
4441 (pci_dev->addr.bus != pci_addr.bus) ||
4442 (pci_dev->addr.devid != pci_addr.devid) ||
4443 (pci_dev->addr.function != pci_addr.function))
4445 vf = (pci_dev->id.device_id ==
4446 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
4447 INFO("PCI information matches, using device \"%s\" (VF: %s)",
4448 list[i]->name, (vf ? "true" : "false"));
4449 attr_ctx = ibv_open_device(list[i]);
4453 if (attr_ctx == NULL) {
4454 ibv_free_device_list(list);
4457 WARN("cannot access device, is mlx4_ib loaded?");
4460 WARN("cannot use device, are drivers up to date?");
4468 DEBUG("device opened");
4469 if (ibv_query_device(attr_ctx, &device_attr))
4471 INFO("%u port(s) detected", device_attr.phys_port_cnt);
4473 for (i = 0; i < device_attr.phys_port_cnt; i++) {
4474 uint32_t port = i + 1; /* ports are indexed from one */
4475 uint32_t test = (1 << i);
4476 struct ibv_context *ctx = NULL;
4477 struct ibv_port_attr port_attr;
4478 struct ibv_pd *pd = NULL;
4479 struct priv *priv = NULL;
4480 struct rte_eth_dev *eth_dev;
4481 #ifdef HAVE_EXP_QUERY_DEVICE
4482 struct ibv_exp_device_attr exp_device_attr;
4483 #endif /* HAVE_EXP_QUERY_DEVICE */
4484 struct ether_addr mac;
4485 union ibv_gid temp_gid;
4487 #ifdef HAVE_EXP_QUERY_DEVICE
4488 exp_device_attr.comp_mask = IBV_EXP_DEVICE_ATTR_EXP_CAP_FLAGS;
4490 exp_device_attr.comp_mask |= IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ;
4491 #endif /* RSS_SUPPORT */
4492 #endif /* HAVE_EXP_QUERY_DEVICE */
4494 DEBUG("using port %u (%08" PRIx32 ")", port, test);
4496 ctx = ibv_open_device(ibv_dev);
4500 /* Check port status. */
4501 err = ibv_query_port(ctx, port, &port_attr);
4503 ERROR("port query failed: %s", strerror(err));
4506 if (port_attr.state != IBV_PORT_ACTIVE)
4507 WARN("bad state for port %d: \"%s\" (%d)",
4508 port, ibv_port_state_str(port_attr.state),
4511 /* Allocate protection domain. */
4512 pd = ibv_alloc_pd(ctx);
4514 ERROR("PD allocation failure");
4519 mlx4_dev[idx].ports |= test;
4521 /* from rte_ethdev.c */
4522 priv = rte_zmalloc("ethdev private structure",
4524 RTE_CACHE_LINE_SIZE);
4526 ERROR("priv allocation failure");
4532 priv->device_attr = device_attr;
4533 priv->port_attr = port_attr;
4536 priv->mtu = ETHER_MTU;
4537 #ifdef HAVE_EXP_QUERY_DEVICE
4538 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
4539 ERROR("ibv_exp_query_device() failed");
4543 if ((exp_device_attr.exp_device_cap_flags &
4544 IBV_EXP_DEVICE_QPG) &&
4545 (exp_device_attr.exp_device_cap_flags &
4546 IBV_EXP_DEVICE_UD_RSS) &&
4547 (exp_device_attr.comp_mask &
4548 IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ) &&
4549 (exp_device_attr.max_rss_tbl_sz > 0)) {
4552 priv->max_rss_tbl_sz = exp_device_attr.max_rss_tbl_sz;
4556 priv->max_rss_tbl_sz = 0;
4558 priv->hw_tss = !!(exp_device_attr.exp_device_cap_flags &
4559 IBV_EXP_DEVICE_UD_TSS);
4560 DEBUG("device flags: %s%s%s",
4561 (priv->hw_qpg ? "IBV_DEVICE_QPG " : ""),
4562 (priv->hw_tss ? "IBV_DEVICE_TSS " : ""),
4563 (priv->hw_rss ? "IBV_DEVICE_RSS " : ""));
4565 DEBUG("maximum RSS indirection table size: %u",
4566 exp_device_attr.max_rss_tbl_sz);
4567 #endif /* RSS_SUPPORT */
4570 priv->inl_recv_size = mlx4_getenv_int("MLX4_INLINE_RECV_SIZE");
4572 if (priv->inl_recv_size) {
4573 exp_device_attr.comp_mask =
4574 IBV_EXP_DEVICE_ATTR_INLINE_RECV_SZ;
4575 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
4576 INFO("Couldn't query device for inline-receive"
4578 priv->inl_recv_size = 0;
4580 if ((unsigned)exp_device_attr.inline_recv_sz <
4581 priv->inl_recv_size) {
4582 INFO("Max inline-receive (%d) <"
4583 " requested inline-receive (%u)",
4584 exp_device_attr.inline_recv_sz,
4585 priv->inl_recv_size);
4586 priv->inl_recv_size =
4587 exp_device_attr.inline_recv_sz;
4590 INFO("Set inline receive size to %u",
4591 priv->inl_recv_size);
4593 #endif /* INLINE_RECV */
4594 #endif /* HAVE_EXP_QUERY_DEVICE */
4596 (void)mlx4_getenv_int;
4598 if (ibv_query_gid(ctx, port, 0, &temp_gid)) {
4599 ERROR("ibv_query_gid() failure");
4602 /* Configure the first MAC address by default. */
4603 mac_from_gid(&mac.addr_bytes, port, temp_gid.raw);
4604 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
4606 mac.addr_bytes[0], mac.addr_bytes[1],
4607 mac.addr_bytes[2], mac.addr_bytes[3],
4608 mac.addr_bytes[4], mac.addr_bytes[5]);
4609 /* Register MAC and broadcast addresses. */
4610 claim_zero(priv_mac_addr_add(priv, 0,
4611 (const uint8_t (*)[ETHER_ADDR_LEN])
4613 claim_zero(priv_mac_addr_add(priv, 1,
4614 &(const uint8_t [ETHER_ADDR_LEN])
4615 { "\xff\xff\xff\xff\xff\xff" }));
4618 char ifname[IF_NAMESIZE];
4620 if (priv_get_ifname(priv, &ifname) == 0)
4621 DEBUG("port %u ifname is \"%s\"",
4622 priv->port, ifname);
4624 DEBUG("port %u ifname is unknown", priv->port);
4627 /* Get actual MTU if possible. */
4628 priv_get_mtu(priv, &priv->mtu);
4629 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
4631 /* from rte_ethdev.c */
4633 char name[RTE_ETH_NAME_MAX_LEN];
4635 snprintf(name, sizeof(name), "%s port %u",
4636 ibv_get_device_name(ibv_dev), port);
4637 eth_dev = rte_eth_dev_allocate(name, RTE_ETH_DEV_PCI);
4639 if (eth_dev == NULL) {
4640 ERROR("can not allocate rte ethdev");
4645 eth_dev->data->dev_private = priv;
4646 eth_dev->pci_dev = pci_dev;
4647 eth_dev->driver = &mlx4_driver;
4648 eth_dev->data->rx_mbuf_alloc_failed = 0;
4649 eth_dev->data->mtu = ETHER_MTU;
4651 priv->dev = eth_dev;
4652 eth_dev->dev_ops = &mlx4_dev_ops;
4653 eth_dev->data->mac_addrs = priv->mac;
4655 /* Bring Ethernet device up. */
4656 DEBUG("forcing Ethernet interface up");
4657 priv_set_flags(priv, ~IFF_UP, IFF_UP);
4663 claim_zero(ibv_dealloc_pd(pd));
4665 claim_zero(ibv_close_device(ctx));
4670 * XXX if something went wrong in the loop above, there is a resource
4671 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
4672 * long as the dpdk does not provide a way to deallocate a ethdev and a
4673 * way to enumerate the registered ethdevs to free the previous ones.
4676 /* no port found, complain */
4677 if (!mlx4_dev[idx].ports) {
4684 claim_zero(ibv_close_device(attr_ctx));
4686 ibv_free_device_list(list);
4691 static const struct rte_pci_id mlx4_pci_id_map[] = {
4693 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4694 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3,
4695 .subsystem_vendor_id = PCI_ANY_ID,
4696 .subsystem_device_id = PCI_ANY_ID
4699 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4700 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO,
4701 .subsystem_vendor_id = PCI_ANY_ID,
4702 .subsystem_device_id = PCI_ANY_ID
4705 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4706 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3VF,
4707 .subsystem_vendor_id = PCI_ANY_ID,
4708 .subsystem_device_id = PCI_ANY_ID
4715 static struct eth_driver mlx4_driver = {
4717 .name = MLX4_DRIVER_NAME,
4718 .id_table = mlx4_pci_id_map,
4719 .devinit = mlx4_pci_devinit,
4721 .dev_private_size = sizeof(struct priv)
4725 * Driver initialization routine.
4728 rte_mlx4_pmd_init(const char *name, const char *args)
4732 rte_eal_pci_register(&mlx4_driver.pci_drv);
4736 static struct rte_driver rte_mlx4_driver = {
4738 .name = MLX4_DRIVER_NAME,
4739 .init = rte_mlx4_pmd_init,
4742 PMD_REGISTER_DRIVER(rte_mlx4_driver)