4 * Copyright 2012-2015 6WIND S.A.
5 * Copyright 2012 Mellanox.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of 6WIND S.A. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * - RSS hash key and options cannot be modified.
37 * - Hardware counters aren't implemented.
51 #include <arpa/inet.h>
54 #include <sys/ioctl.h>
55 #include <sys/socket.h>
56 #include <netinet/in.h>
58 #include <linux/ethtool.h>
59 #include <linux/sockios.h>
62 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
64 #pragma GCC diagnostic ignored "-pedantic"
66 #include <infiniband/verbs.h>
68 #pragma GCC diagnostic error "-pedantic"
71 /* DPDK headers don't like -pedantic. */
73 #pragma GCC diagnostic ignored "-pedantic"
75 #include <rte_config.h>
76 #include <rte_ether.h>
77 #include <rte_ethdev.h>
80 #include <rte_errno.h>
81 #include <rte_mempool.h>
82 #include <rte_prefetch.h>
83 #include <rte_malloc.h>
84 #include <rte_spinlock.h>
85 #include <rte_atomic.h>
86 #include <rte_version.h>
89 #pragma GCC diagnostic error "-pedantic"
92 /* Generated configuration header. */
93 #include "mlx4_autoconf.h"
98 /* Runtime logging through RTE_LOG() is enabled when not in debugging mode.
99 * Intermediate LOG_*() macros add the required end-of-line characters. */
101 #define INFO(...) DEBUG(__VA_ARGS__)
102 #define WARN(...) DEBUG(__VA_ARGS__)
103 #define ERROR(...) DEBUG(__VA_ARGS__)
105 #define LOG__(level, m, ...) \
106 RTE_LOG(level, PMD, MLX4_DRIVER_NAME ": " m "%c", __VA_ARGS__)
107 #define LOG_(level, ...) LOG__(level, __VA_ARGS__, '\n')
108 #define INFO(...) LOG_(INFO, __VA_ARGS__)
109 #define WARN(...) LOG_(WARNING, __VA_ARGS__)
110 #define ERROR(...) LOG_(ERR, __VA_ARGS__)
113 /* Convenience macros for accessing mbuf fields. */
114 #define NEXT(m) ((m)->next)
115 #define DATA_LEN(m) ((m)->data_len)
116 #define PKT_LEN(m) ((m)->pkt_len)
117 #define DATA_OFF(m) ((m)->data_off)
118 #define SET_DATA_OFF(m, o) ((m)->data_off = (o))
119 #define NB_SEGS(m) ((m)->nb_segs)
120 #define PORT(m) ((m)->port)
122 /* Work Request ID data type (64 bit). */
131 #define WR_ID(o) (((wr_id_t *)&(o))->data)
133 /* Compile-time check. */
134 static inline void wr_id_t_check(void)
136 wr_id_t check[1 + (2 * -!(sizeof(wr_id_t) == sizeof(uint64_t)))];
142 struct mlx4_rxq_stats {
143 unsigned int idx; /**< Mapping index. */
144 #ifdef MLX4_PMD_SOFT_COUNTERS
145 uint64_t ipackets; /**< Total of successfully received packets. */
146 uint64_t ibytes; /**< Total of successfully received bytes. */
148 uint64_t idropped; /**< Total of packets dropped when RX ring full. */
149 uint64_t rx_nombuf; /**< Total of RX mbuf allocation failures. */
152 struct mlx4_txq_stats {
153 unsigned int idx; /**< Mapping index. */
154 #ifdef MLX4_PMD_SOFT_COUNTERS
155 uint64_t opackets; /**< Total of successfully sent packets. */
156 uint64_t obytes; /**< Total of successfully sent bytes. */
158 uint64_t odropped; /**< Total of packets not sent when TX ring full. */
161 /* RX element (scattered packets). */
163 struct ibv_recv_wr wr; /* Work Request. */
164 struct ibv_sge sges[MLX4_PMD_SGE_WR_N]; /* Scatter/Gather Elements. */
165 struct rte_mbuf *bufs[MLX4_PMD_SGE_WR_N]; /* SGEs buffers. */
170 struct ibv_recv_wr wr; /* Work Request. */
171 struct ibv_sge sge; /* Scatter/Gather Element. */
172 /* mbuf pointer is derived from WR_ID(wr.wr_id).offset. */
175 /* RX queue descriptor. */
177 struct priv *priv; /* Back pointer to private data. */
178 struct rte_mempool *mp; /* Memory Pool for allocations. */
179 struct ibv_mr *mr; /* Memory Region (for mp). */
180 struct ibv_cq *cq; /* Completion Queue. */
181 struct ibv_qp *qp; /* Queue Pair. */
182 struct ibv_exp_qp_burst_family *if_qp; /* QP burst interface. */
183 struct ibv_exp_cq_family *if_cq; /* CQ interface. */
185 * Each VLAN ID requires a separate flow steering rule.
187 BITFIELD_DECLARE(mac_configured, uint32_t, MLX4_MAX_MAC_ADDRESSES);
188 struct ibv_flow *mac_flow[MLX4_MAX_MAC_ADDRESSES][MLX4_MAX_VLAN_IDS];
189 struct ibv_flow *promisc_flow; /* Promiscuous flow. */
190 struct ibv_flow *allmulti_flow; /* Multicast flow. */
191 unsigned int port_id; /* Port ID for incoming packets. */
192 unsigned int elts_n; /* (*elts)[] length. */
193 unsigned int elts_head; /* Current index in (*elts)[]. */
195 struct rxq_elt_sp (*sp)[]; /* Scattered RX elements. */
196 struct rxq_elt (*no_sp)[]; /* RX elements. */
198 unsigned int sp:1; /* Use scattered RX elements. */
199 uint32_t mb_len; /* Length of a mp-issued mbuf. */
200 struct mlx4_rxq_stats stats; /* RX queue counters. */
201 unsigned int socket; /* CPU socket ID for allocations. */
206 struct rte_mbuf *buf;
209 /* Linear buffer type. It is used when transmitting buffers with too many
210 * segments that do not fit the hardware queue (see max_send_sge).
211 * Extra segments are copied (linearized) in such buffers, replacing the
212 * last SGE during TX.
213 * The size is arbitrary but large enough to hold a jumbo frame with
214 * 8 segments considering mbuf.buf_len is about 2048 bytes. */
215 typedef uint8_t linear_t[16384];
217 /* TX queue descriptor. */
219 struct priv *priv; /* Back pointer to private data. */
221 struct rte_mempool *mp; /* Cached Memory Pool. */
222 struct ibv_mr *mr; /* Memory Region (for mp). */
223 uint32_t lkey; /* mr->lkey */
224 } mp2mr[MLX4_PMD_TX_MP_CACHE]; /* MP to MR translation table. */
225 struct ibv_cq *cq; /* Completion Queue. */
226 struct ibv_qp *qp; /* Queue Pair. */
227 struct ibv_exp_qp_burst_family *if_qp; /* QP burst interface. */
228 struct ibv_exp_cq_family *if_cq; /* CQ interface. */
229 #if MLX4_PMD_MAX_INLINE > 0
230 uint32_t max_inline; /* Max inline send size <= MLX4_PMD_MAX_INLINE. */
232 unsigned int elts_n; /* (*elts)[] length. */
233 struct txq_elt (*elts)[]; /* TX elements. */
234 unsigned int elts_head; /* Current index in (*elts)[]. */
235 unsigned int elts_tail; /* First element awaiting completion. */
236 unsigned int elts_comp; /* Number of completion requests. */
237 unsigned int elts_comp_cd; /* Countdown for next completion request. */
238 unsigned int elts_comp_cd_init; /* Initial value for countdown. */
239 struct mlx4_txq_stats stats; /* TX queue counters. */
240 linear_t (*elts_linear)[]; /* Linearized buffers. */
241 struct ibv_mr *mr_linear; /* Memory Region for linearized buffers. */
242 unsigned int socket; /* CPU socket ID for allocations. */
246 struct rte_eth_dev *dev; /* Ethernet device. */
247 struct ibv_context *ctx; /* Verbs context. */
248 struct ibv_device_attr device_attr; /* Device properties. */
249 struct ibv_pd *pd; /* Protection Domain. */
251 * MAC addresses array and configuration bit-field.
252 * An extra entry that cannot be modified by the DPDK is reserved
253 * for broadcast frames (destination MAC address ff:ff:ff:ff:ff:ff).
255 struct ether_addr mac[MLX4_MAX_MAC_ADDRESSES];
256 BITFIELD_DECLARE(mac_configured, uint32_t, MLX4_MAX_MAC_ADDRESSES);
259 unsigned int enabled:1; /* If enabled. */
260 unsigned int id:12; /* VLAN ID (0-4095). */
261 } vlan_filter[MLX4_MAX_VLAN_IDS]; /* VLAN filters table. */
262 /* Device properties. */
263 uint16_t mtu; /* Configured MTU. */
264 uint8_t port; /* Physical port number. */
265 unsigned int started:1; /* Device started, flows enabled. */
266 unsigned int promisc:1; /* Device in promiscuous mode. */
267 unsigned int allmulti:1; /* Device receives all multicast packets. */
268 unsigned int hw_qpg:1; /* QP groups are supported. */
269 unsigned int hw_tss:1; /* TSS is supported. */
270 unsigned int hw_rss:1; /* RSS is supported. */
271 unsigned int rss:1; /* RSS is enabled. */
272 unsigned int vf:1; /* This is a VF device. */
274 unsigned int inl_recv_size; /* Inline recv size */
276 unsigned int max_rss_tbl_sz; /* Maximum number of RSS queues. */
278 struct rxq rxq_parent; /* Parent queue when RSS is enabled. */
279 unsigned int rxqs_n; /* RX queues array size. */
280 unsigned int txqs_n; /* TX queues array size. */
281 struct rxq *(*rxqs)[]; /* RX queues. */
282 struct txq *(*txqs)[]; /* TX queues. */
283 rte_spinlock_t lock; /* Lock for control functions. */
287 * Lock private structure to protect it from concurrent access in the
291 * Pointer to private structure.
294 priv_lock(struct priv *priv)
296 rte_spinlock_lock(&priv->lock);
300 * Unlock private structure.
303 * Pointer to private structure.
306 priv_unlock(struct priv *priv)
308 rte_spinlock_unlock(&priv->lock);
311 /* Allocate a buffer on the stack and fill it with a printf format string. */
312 #define MKSTR(name, ...) \
313 char name[snprintf(NULL, 0, __VA_ARGS__) + 1]; \
315 snprintf(name, sizeof(name), __VA_ARGS__)
318 * Get interface name from private structure.
321 * Pointer to private structure.
323 * Interface name output buffer.
326 * 0 on success, -1 on failure and errno is set.
329 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
333 unsigned int dev_type = 0;
334 unsigned int dev_port_prev = ~0u;
335 char match[IF_NAMESIZE] = "";
338 MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
344 while ((dent = readdir(dir)) != NULL) {
345 char *name = dent->d_name;
347 unsigned int dev_port;
350 if ((name[0] == '.') &&
351 ((name[1] == '\0') ||
352 ((name[1] == '.') && (name[2] == '\0'))))
355 MKSTR(path, "%s/device/net/%s/%s",
356 priv->ctx->device->ibdev_path, name,
357 (dev_type ? "dev_id" : "dev_port"));
359 file = fopen(path, "rb");
364 * Switch to dev_id when dev_port does not exist as
365 * is the case with Linux kernel versions < 3.15.
376 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
381 * Switch to dev_id when dev_port returns the same value for
382 * all ports. May happen when using a MOFED release older than
383 * 3.0 with a Linux kernel >= 3.15.
385 if (dev_port == dev_port_prev)
387 dev_port_prev = dev_port;
388 if (dev_port == (priv->port - 1u))
389 snprintf(match, sizeof(match), "%s", name);
392 if (match[0] == '\0')
394 strncpy(*ifname, match, sizeof(*ifname));
399 * Read from sysfs entry.
402 * Pointer to private structure.
404 * Entry name relative to sysfs path.
406 * Data output buffer.
411 * 0 on success, -1 on failure and errno is set.
414 priv_sysfs_read(const struct priv *priv, const char *entry,
415 char *buf, size_t size)
417 char ifname[IF_NAMESIZE];
422 if (priv_get_ifname(priv, &ifname))
425 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
428 file = fopen(path, "rb");
431 ret = fread(buf, 1, size, file);
433 if (((size_t)ret < size) && (ferror(file)))
443 * Write to sysfs entry.
446 * Pointer to private structure.
448 * Entry name relative to sysfs path.
455 * 0 on success, -1 on failure and errno is set.
458 priv_sysfs_write(const struct priv *priv, const char *entry,
459 char *buf, size_t size)
461 char ifname[IF_NAMESIZE];
466 if (priv_get_ifname(priv, &ifname))
469 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
472 file = fopen(path, "wb");
475 ret = fwrite(buf, 1, size, file);
477 if (((size_t)ret < size) || (ferror(file)))
487 * Get unsigned long sysfs property.
490 * Pointer to private structure.
492 * Entry name relative to sysfs path.
494 * Value output buffer.
497 * 0 on success, -1 on failure and errno is set.
500 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
503 unsigned long value_ret;
506 ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
508 DEBUG("cannot read %s value from sysfs: %s",
509 name, strerror(errno));
512 value_str[ret] = '\0';
514 value_ret = strtoul(value_str, NULL, 0);
516 DEBUG("invalid %s value `%s': %s", name, value_str,
525 * Set unsigned long sysfs property.
528 * Pointer to private structure.
530 * Entry name relative to sysfs path.
535 * 0 on success, -1 on failure and errno is set.
538 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
541 MKSTR(value_str, "%lu", value);
543 ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
545 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
546 name, value_str, value, strerror(errno));
553 * Perform ifreq ioctl() on associated Ethernet device.
556 * Pointer to private structure.
558 * Request number to pass to ioctl().
560 * Interface request structure output buffer.
563 * 0 on success, -1 on failure and errno is set.
566 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
568 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
573 if (priv_get_ifname(priv, &ifr->ifr_name) == 0)
574 ret = ioctl(sock, req, ifr);
583 * Pointer to private structure.
585 * MTU value output buffer.
588 * 0 on success, -1 on failure and errno is set.
591 priv_get_mtu(struct priv *priv, uint16_t *mtu)
593 unsigned long ulong_mtu;
595 if (priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu) == -1)
605 * Pointer to private structure.
610 * 0 on success, -1 on failure and errno is set.
613 priv_set_mtu(struct priv *priv, uint16_t mtu)
615 return priv_set_sysfs_ulong(priv, "mtu", mtu);
622 * Pointer to private structure.
624 * Bitmask for flags that must remain untouched.
626 * Bitmask for flags to modify.
629 * 0 on success, -1 on failure and errno is set.
632 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
636 if (priv_get_sysfs_ulong(priv, "flags", &tmp) == -1)
640 return priv_set_sysfs_ulong(priv, "flags", tmp);
643 /* Device configuration. */
646 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
647 unsigned int socket, const struct rte_eth_rxconf *conf,
648 struct rte_mempool *mp);
651 rxq_cleanup(struct rxq *rxq);
654 * Ethernet device configuration.
656 * Prepare the driver for a given number of TX and RX queues.
657 * Allocate parent RSS queue when several RX queues are requested.
660 * Pointer to Ethernet device structure.
663 * 0 on success, errno value on failure.
666 dev_configure(struct rte_eth_dev *dev)
668 struct priv *priv = dev->data->dev_private;
669 unsigned int rxqs_n = dev->data->nb_rx_queues;
670 unsigned int txqs_n = dev->data->nb_tx_queues;
674 priv->rxqs = (void *)dev->data->rx_queues;
675 priv->txqs = (void *)dev->data->tx_queues;
676 if (txqs_n != priv->txqs_n) {
677 INFO("%p: TX queues number update: %u -> %u",
678 (void *)dev, priv->txqs_n, txqs_n);
679 priv->txqs_n = txqs_n;
681 if (rxqs_n == priv->rxqs_n)
683 INFO("%p: RX queues number update: %u -> %u",
684 (void *)dev, priv->rxqs_n, rxqs_n);
685 /* If RSS is enabled, disable it first. */
689 /* Only if there are no remaining child RX queues. */
690 for (i = 0; (i != priv->rxqs_n); ++i)
691 if ((*priv->rxqs)[i] != NULL)
693 rxq_cleanup(&priv->rxq_parent);
698 /* Nothing else to do. */
699 priv->rxqs_n = rxqs_n;
702 /* Allocate a new RSS parent queue if supported by hardware. */
704 ERROR("%p: only a single RX queue can be configured when"
705 " hardware doesn't support RSS",
709 /* Fail if hardware doesn't support that many RSS queues. */
710 if (rxqs_n >= priv->max_rss_tbl_sz) {
711 ERROR("%p: only %u RX queues can be configured for RSS",
712 (void *)dev, priv->max_rss_tbl_sz);
717 priv->rxqs_n = rxqs_n;
718 ret = rxq_setup(dev, &priv->rxq_parent, 0, 0, NULL, NULL);
721 /* Failure, rollback. */
729 * DPDK callback for Ethernet device configuration.
732 * Pointer to Ethernet device structure.
735 * 0 on success, negative errno value on failure.
738 mlx4_dev_configure(struct rte_eth_dev *dev)
740 struct priv *priv = dev->data->dev_private;
744 ret = dev_configure(dev);
750 /* TX queues handling. */
753 * Allocate TX queue elements.
756 * Pointer to TX queue structure.
758 * Number of elements to allocate.
761 * 0 on success, errno value on failure.
764 txq_alloc_elts(struct txq *txq, unsigned int elts_n)
767 struct txq_elt (*elts)[elts_n] =
768 rte_calloc_socket("TXQ", 1, sizeof(*elts), 0, txq->socket);
769 linear_t (*elts_linear)[elts_n] =
770 rte_calloc_socket("TXQ", 1, sizeof(*elts_linear), 0,
772 struct ibv_mr *mr_linear = NULL;
775 if ((elts == NULL) || (elts_linear == NULL)) {
776 ERROR("%p: can't allocate packets array", (void *)txq);
781 ibv_reg_mr(txq->priv->pd, elts_linear, sizeof(*elts_linear),
782 (IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE));
783 if (mr_linear == NULL) {
784 ERROR("%p: unable to configure MR, ibv_reg_mr() failed",
789 for (i = 0; (i != elts_n); ++i) {
790 struct txq_elt *elt = &(*elts)[i];
794 DEBUG("%p: allocated and configured %u WRs", (void *)txq, elts_n);
795 txq->elts_n = elts_n;
800 /* Request send completion every MLX4_PMD_TX_PER_COMP_REQ packets or
801 * at least 4 times per ring. */
802 txq->elts_comp_cd_init =
803 ((MLX4_PMD_TX_PER_COMP_REQ < (elts_n / 4)) ?
804 MLX4_PMD_TX_PER_COMP_REQ : (elts_n / 4));
805 txq->elts_comp_cd = txq->elts_comp_cd_init;
806 txq->elts_linear = elts_linear;
807 txq->mr_linear = mr_linear;
811 if (mr_linear != NULL)
812 claim_zero(ibv_dereg_mr(mr_linear));
814 rte_free(elts_linear);
817 DEBUG("%p: failed, freed everything", (void *)txq);
823 * Free TX queue elements.
826 * Pointer to TX queue structure.
829 txq_free_elts(struct txq *txq)
832 unsigned int elts_n = txq->elts_n;
833 struct txq_elt (*elts)[elts_n] = txq->elts;
834 linear_t (*elts_linear)[elts_n] = txq->elts_linear;
835 struct ibv_mr *mr_linear = txq->mr_linear;
837 DEBUG("%p: freeing WRs", (void *)txq);
840 txq->elts_linear = NULL;
841 txq->mr_linear = NULL;
842 if (mr_linear != NULL)
843 claim_zero(ibv_dereg_mr(mr_linear));
845 rte_free(elts_linear);
848 for (i = 0; (i != elemof(*elts)); ++i) {
849 struct txq_elt *elt = &(*elts)[i];
851 if (elt->buf == NULL)
853 rte_pktmbuf_free(elt->buf);
860 * Clean up a TX queue.
862 * Destroy objects, free allocated memory and reset the structure for reuse.
865 * Pointer to TX queue structure.
868 txq_cleanup(struct txq *txq)
870 struct ibv_exp_release_intf_params params;
873 DEBUG("cleaning up %p", (void *)txq);
875 if (txq->if_qp != NULL) {
876 assert(txq->priv != NULL);
877 assert(txq->priv->ctx != NULL);
878 assert(txq->qp != NULL);
879 params = (struct ibv_exp_release_intf_params){
882 claim_zero(ibv_exp_release_intf(txq->priv->ctx,
886 if (txq->if_cq != NULL) {
887 assert(txq->priv != NULL);
888 assert(txq->priv->ctx != NULL);
889 assert(txq->cq != NULL);
890 params = (struct ibv_exp_release_intf_params){
893 claim_zero(ibv_exp_release_intf(txq->priv->ctx,
898 claim_zero(ibv_destroy_qp(txq->qp));
900 claim_zero(ibv_destroy_cq(txq->cq));
901 for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
902 if (txq->mp2mr[i].mp == NULL)
904 assert(txq->mp2mr[i].mr != NULL);
905 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
907 memset(txq, 0, sizeof(*txq));
911 * Manage TX completions.
913 * When sending a burst, mlx4_tx_burst() posts several WRs.
914 * To improve performance, a completion event is only required once every
915 * MLX4_PMD_TX_PER_COMP_REQ sends. Doing so discards completion information
916 * for other WRs, but this information would not be used anyway.
919 * Pointer to TX queue structure.
922 * 0 on success, -1 on failure.
925 txq_complete(struct txq *txq)
927 unsigned int elts_comp = txq->elts_comp;
928 unsigned int elts_tail = txq->elts_tail;
929 const unsigned int elts_n = txq->elts_n;
932 if (unlikely(elts_comp == 0))
935 DEBUG("%p: processing %u work requests completions",
936 (void *)txq, elts_comp);
938 wcs_n = txq->if_cq->poll_cnt(txq->cq, elts_comp);
939 if (unlikely(wcs_n == 0))
941 if (unlikely(wcs_n < 0)) {
942 DEBUG("%p: ibv_poll_cq() failed (wcs_n=%d)",
947 assert(elts_comp <= txq->elts_comp);
949 * Assume WC status is successful as nothing can be done about it
952 elts_tail += wcs_n * txq->elts_comp_cd_init;
953 if (elts_tail >= elts_n)
955 txq->elts_tail = elts_tail;
956 txq->elts_comp = elts_comp;
961 * Get Memory Region (MR) <-> Memory Pool (MP) association from txq->mp2mr[].
962 * Add MP to txq->mp2mr[] if it's not registered yet. If mp2mr[] is full,
963 * remove an entry first.
966 * Pointer to TX queue structure.
968 * Memory Pool for which a Memory Region lkey must be returned.
971 * mr->lkey on success, (uint32_t)-1 on failure.
974 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
979 for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
980 if (unlikely(txq->mp2mr[i].mp == NULL)) {
981 /* Unknown MP, add a new MR for it. */
984 if (txq->mp2mr[i].mp == mp) {
985 assert(txq->mp2mr[i].lkey != (uint32_t)-1);
986 assert(txq->mp2mr[i].mr->lkey == txq->mp2mr[i].lkey);
987 return txq->mp2mr[i].lkey;
990 /* Add a new entry, register MR first. */
991 DEBUG("%p: discovered new memory pool %p", (void *)txq, (void *)mp);
992 mr = ibv_reg_mr(txq->priv->pd,
993 (void *)mp->elt_va_start,
994 (mp->elt_va_end - mp->elt_va_start),
995 (IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE));
996 if (unlikely(mr == NULL)) {
997 DEBUG("%p: unable to configure MR, ibv_reg_mr() failed.",
1001 if (unlikely(i == elemof(txq->mp2mr))) {
1002 /* Table is full, remove oldest entry. */
1003 DEBUG("%p: MR <-> MP table full, dropping oldest entry.",
1006 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
1007 memmove(&txq->mp2mr[0], &txq->mp2mr[1],
1008 (sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
1010 /* Store the new entry. */
1011 txq->mp2mr[i].mp = mp;
1012 txq->mp2mr[i].mr = mr;
1013 txq->mp2mr[i].lkey = mr->lkey;
1014 DEBUG("%p: new MR lkey for MP %p: 0x%08" PRIu32,
1015 (void *)txq, (void *)mp, txq->mp2mr[i].lkey);
1016 return txq->mp2mr[i].lkey;
1019 #if MLX4_PMD_SGE_WR_N > 1
1022 * Copy scattered mbuf contents to a single linear buffer.
1024 * @param[out] linear
1025 * Linear output buffer.
1027 * Scattered input buffer.
1030 * Number of bytes copied to the output buffer or 0 if not large enough.
1033 linearize_mbuf(linear_t *linear, struct rte_mbuf *buf)
1035 unsigned int size = 0;
1036 unsigned int offset;
1039 unsigned int len = DATA_LEN(buf);
1043 if (unlikely(size > sizeof(*linear)))
1045 memcpy(&(*linear)[offset],
1046 rte_pktmbuf_mtod(buf, uint8_t *),
1049 } while (buf != NULL);
1054 * Handle scattered buffers for mlx4_tx_burst().
1057 * TX queue structure.
1059 * Number of segments in buf.
1061 * TX queue element to fill.
1063 * Buffer to process.
1065 * Index of the linear buffer to use if necessary (normally txq->elts_head).
1067 * Array filled with SGEs on success.
1070 * A structure containing the processed packet size in bytes and the
1071 * number of SGEs. Both fields are set to (unsigned int)-1 in case of
1074 static struct tx_burst_sg_ret {
1075 unsigned int length;
1078 tx_burst_sg(struct txq *txq, unsigned int segs, struct txq_elt *elt,
1079 struct rte_mbuf *buf, unsigned int elts_head,
1080 struct ibv_sge (*sges)[MLX4_PMD_SGE_WR_N])
1082 unsigned int sent_size = 0;
1086 /* When there are too many segments, extra segments are
1087 * linearized in the last SGE. */
1088 if (unlikely(segs > elemof(*sges))) {
1089 segs = (elemof(*sges) - 1);
1092 /* Update element. */
1094 /* Register segments as SGEs. */
1095 for (j = 0; (j != segs); ++j) {
1096 struct ibv_sge *sge = &(*sges)[j];
1099 /* Retrieve Memory Region key for this memory pool. */
1100 lkey = txq_mp2mr(txq, buf->pool);
1101 if (unlikely(lkey == (uint32_t)-1)) {
1102 /* MR does not exist. */
1103 DEBUG("%p: unable to get MP <-> MR association",
1105 /* Clean up TX element. */
1110 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
1112 rte_prefetch0((volatile void *)
1113 (uintptr_t)sge->addr);
1114 sge->length = DATA_LEN(buf);
1116 sent_size += sge->length;
1119 /* If buf is not NULL here and is not going to be linearized,
1120 * nb_segs is not valid. */
1122 assert((buf == NULL) || (linearize));
1123 /* Linearize extra segments. */
1125 struct ibv_sge *sge = &(*sges)[segs];
1126 linear_t *linear = &(*txq->elts_linear)[elts_head];
1127 unsigned int size = linearize_mbuf(linear, buf);
1129 assert(segs == (elemof(*sges) - 1));
1131 /* Invalid packet. */
1132 DEBUG("%p: packet too large to be linearized.",
1134 /* Clean up TX element. */
1138 /* If MLX4_PMD_SGE_WR_N is 1, free mbuf immediately. */
1139 if (elemof(*sges) == 1) {
1141 struct rte_mbuf *next = NEXT(buf);
1143 rte_pktmbuf_free_seg(buf);
1145 } while (buf != NULL);
1149 sge->addr = (uintptr_t)&(*linear)[0];
1151 sge->lkey = txq->mr_linear->lkey;
1154 return (struct tx_burst_sg_ret){
1155 .length = sent_size,
1159 return (struct tx_burst_sg_ret){
1165 #endif /* MLX4_PMD_SGE_WR_N > 1 */
1168 * DPDK callback for TX.
1171 * Generic pointer to TX queue structure.
1173 * Packets to transmit.
1175 * Number of packets in array.
1178 * Number of packets successfully transmitted (<= pkts_n).
1181 mlx4_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
1183 struct txq *txq = (struct txq *)dpdk_txq;
1184 unsigned int elts_head = txq->elts_head;
1185 const unsigned int elts_tail = txq->elts_tail;
1186 const unsigned int elts_n = txq->elts_n;
1187 unsigned int elts_comp_cd = txq->elts_comp_cd;
1188 unsigned int elts_comp = 0;
1193 assert(elts_comp_cd != 0);
1195 max = (elts_n - (elts_head - elts_tail));
1199 assert(max <= elts_n);
1200 /* Always leave one free entry in the ring. */
1206 for (i = 0; (i != max); ++i) {
1207 struct rte_mbuf *buf = pkts[i];
1208 unsigned int elts_head_next =
1209 (((elts_head + 1) == elts_n) ? 0 : elts_head + 1);
1210 struct txq_elt *elt_next = &(*txq->elts)[elts_head_next];
1211 struct txq_elt *elt = &(*txq->elts)[elts_head];
1212 unsigned int segs = NB_SEGS(buf);
1213 #ifdef MLX4_PMD_SOFT_COUNTERS
1214 unsigned int sent_size = 0;
1216 uint32_t send_flags = 0;
1218 /* Clean up old buffer. */
1219 if (likely(elt->buf != NULL)) {
1220 struct rte_mbuf *tmp = elt->buf;
1222 /* Faster than rte_pktmbuf_free(). */
1224 struct rte_mbuf *next = NEXT(tmp);
1226 rte_pktmbuf_free_seg(tmp);
1228 } while (tmp != NULL);
1230 /* Request TX completion. */
1231 if (unlikely(--elts_comp_cd == 0)) {
1232 elts_comp_cd = txq->elts_comp_cd_init;
1234 send_flags |= IBV_EXP_QP_BURST_SIGNALED;
1236 if (likely(segs == 1)) {
1241 /* Retrieve buffer information. */
1242 addr = rte_pktmbuf_mtod(buf, uintptr_t);
1243 length = DATA_LEN(buf);
1244 /* Retrieve Memory Region key for this memory pool. */
1245 lkey = txq_mp2mr(txq, buf->pool);
1246 if (unlikely(lkey == (uint32_t)-1)) {
1247 /* MR does not exist. */
1248 DEBUG("%p: unable to get MP <-> MR"
1249 " association", (void *)txq);
1250 /* Clean up TX element. */
1254 /* Update element. */
1257 rte_prefetch0((volatile void *)
1259 RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
1260 /* Put packet into send queue. */
1261 #if MLX4_PMD_MAX_INLINE > 0
1262 if (length <= txq->max_inline)
1263 err = txq->if_qp->send_pending_inline
1270 err = txq->if_qp->send_pending
1278 #ifdef MLX4_PMD_SOFT_COUNTERS
1279 sent_size += length;
1282 #if MLX4_PMD_SGE_WR_N > 1
1283 struct ibv_sge sges[MLX4_PMD_SGE_WR_N];
1284 struct tx_burst_sg_ret ret;
1286 ret = tx_burst_sg(txq, segs, elt, buf, elts_head,
1288 if (ret.length == (unsigned int)-1)
1290 RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
1291 /* Put SG list into send queue. */
1292 err = txq->if_qp->send_pending_sg_list
1299 #ifdef MLX4_PMD_SOFT_COUNTERS
1300 sent_size += ret.length;
1302 #else /* MLX4_PMD_SGE_WR_N > 1 */
1303 DEBUG("%p: TX scattered buffers support not"
1304 " compiled in", (void *)txq);
1306 #endif /* MLX4_PMD_SGE_WR_N > 1 */
1308 elts_head = elts_head_next;
1309 #ifdef MLX4_PMD_SOFT_COUNTERS
1310 /* Increment sent bytes counter. */
1311 txq->stats.obytes += sent_size;
1315 /* Take a shortcut if nothing must be sent. */
1316 if (unlikely(i == 0))
1318 #ifdef MLX4_PMD_SOFT_COUNTERS
1319 /* Increment sent packets counter. */
1320 txq->stats.opackets += i;
1322 /* Ring QP doorbell. */
1323 err = txq->if_qp->send_flush(txq->qp);
1324 if (unlikely(err)) {
1325 /* A nonzero value is not supposed to be returned.
1326 * Nothing can be done about it. */
1327 DEBUG("%p: send_flush() failed with error %d",
1330 txq->elts_head = elts_head;
1331 txq->elts_comp += elts_comp;
1332 txq->elts_comp_cd = elts_comp_cd;
1337 * Configure a TX queue.
1340 * Pointer to Ethernet device structure.
1342 * Pointer to TX queue structure.
1344 * Number of descriptors to configure in queue.
1346 * NUMA socket on which memory must be allocated.
1348 * Thresholds parameters.
1351 * 0 on success, errno value on failure.
1354 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
1355 unsigned int socket, const struct rte_eth_txconf *conf)
1357 struct priv *priv = dev->data->dev_private;
1363 struct ibv_exp_query_intf_params params;
1364 struct ibv_qp_init_attr init;
1365 struct ibv_exp_qp_attr mod;
1367 enum ibv_exp_query_intf_status status;
1370 (void)conf; /* Thresholds configuration (ignored). */
1371 if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
1372 ERROR("%p: invalid number of TX descriptors (must be a"
1373 " multiple of %d)", (void *)dev, MLX4_PMD_SGE_WR_N);
1376 desc /= MLX4_PMD_SGE_WR_N;
1377 /* MRs will be registered in mp2mr[] later. */
1378 tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
1379 if (tmpl.cq == NULL) {
1381 ERROR("%p: CQ creation failure: %s",
1382 (void *)dev, strerror(ret));
1385 DEBUG("priv->device_attr.max_qp_wr is %d",
1386 priv->device_attr.max_qp_wr);
1387 DEBUG("priv->device_attr.max_sge is %d",
1388 priv->device_attr.max_sge);
1389 attr.init = (struct ibv_qp_init_attr){
1390 /* CQ to be associated with the send queue. */
1392 /* CQ to be associated with the receive queue. */
1395 /* Max number of outstanding WRs. */
1396 .max_send_wr = ((priv->device_attr.max_qp_wr < desc) ?
1397 priv->device_attr.max_qp_wr :
1399 /* Max number of scatter/gather elements in a WR. */
1400 .max_send_sge = ((priv->device_attr.max_sge <
1401 MLX4_PMD_SGE_WR_N) ?
1402 priv->device_attr.max_sge :
1404 #if MLX4_PMD_MAX_INLINE > 0
1405 .max_inline_data = MLX4_PMD_MAX_INLINE,
1408 .qp_type = IBV_QPT_RAW_PACKET,
1409 /* Do *NOT* enable this, completions events are managed per
1413 tmpl.qp = ibv_create_qp(priv->pd, &attr.init);
1414 if (tmpl.qp == NULL) {
1415 ret = (errno ? errno : EINVAL);
1416 ERROR("%p: QP creation failure: %s",
1417 (void *)dev, strerror(ret));
1420 #if MLX4_PMD_MAX_INLINE > 0
1421 /* ibv_create_qp() updates this value. */
1422 tmpl.max_inline = attr.init.cap.max_inline_data;
1424 attr.mod = (struct ibv_exp_qp_attr){
1425 /* Move the QP to this state. */
1426 .qp_state = IBV_QPS_INIT,
1427 /* Primary port number. */
1428 .port_num = priv->port
1430 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod,
1431 (IBV_EXP_QP_STATE | IBV_EXP_QP_PORT));
1433 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
1434 (void *)dev, strerror(ret));
1437 ret = txq_alloc_elts(&tmpl, desc);
1439 ERROR("%p: TXQ allocation failed: %s",
1440 (void *)dev, strerror(ret));
1443 attr.mod = (struct ibv_exp_qp_attr){
1444 .qp_state = IBV_QPS_RTR
1446 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1448 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
1449 (void *)dev, strerror(ret));
1452 attr.mod.qp_state = IBV_QPS_RTS;
1453 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1455 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
1456 (void *)dev, strerror(ret));
1459 attr.params = (struct ibv_exp_query_intf_params){
1460 .intf_scope = IBV_EXP_INTF_GLOBAL,
1461 .intf = IBV_EXP_INTF_CQ,
1464 tmpl.if_cq = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
1465 if (tmpl.if_cq == NULL) {
1466 ERROR("%p: CQ interface family query failed with status %d",
1467 (void *)dev, status);
1470 attr.params = (struct ibv_exp_query_intf_params){
1471 .intf_scope = IBV_EXP_INTF_GLOBAL,
1472 .intf = IBV_EXP_INTF_QP_BURST,
1475 tmpl.if_qp = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
1476 if (tmpl.if_qp == NULL) {
1477 ERROR("%p: QP interface family query failed with status %d",
1478 (void *)dev, status);
1481 /* Clean up txq in case we're reinitializing it. */
1482 DEBUG("%p: cleaning-up old txq just in case", (void *)txq);
1485 DEBUG("%p: txq updated with %p", (void *)txq, (void *)&tmpl);
1495 * DPDK callback to configure a TX queue.
1498 * Pointer to Ethernet device structure.
1502 * Number of descriptors to configure in queue.
1504 * NUMA socket on which memory must be allocated.
1506 * Thresholds parameters.
1509 * 0 on success, negative errno value on failure.
1512 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1513 unsigned int socket, const struct rte_eth_txconf *conf)
1515 struct priv *priv = dev->data->dev_private;
1516 struct txq *txq = (*priv->txqs)[idx];
1520 DEBUG("%p: configuring queue %u for %u descriptors",
1521 (void *)dev, idx, desc);
1522 if (idx >= priv->txqs_n) {
1523 ERROR("%p: queue index out of range (%u >= %u)",
1524 (void *)dev, idx, priv->txqs_n);
1529 DEBUG("%p: reusing already allocated queue index %u (%p)",
1530 (void *)dev, idx, (void *)txq);
1531 if (priv->started) {
1535 (*priv->txqs)[idx] = NULL;
1538 txq = rte_calloc_socket("TXQ", 1, sizeof(*txq), 0, socket);
1540 ERROR("%p: unable to allocate queue index %u",
1546 ret = txq_setup(dev, txq, desc, socket, conf);
1550 txq->stats.idx = idx;
1551 DEBUG("%p: adding TX queue %p to list",
1552 (void *)dev, (void *)txq);
1553 (*priv->txqs)[idx] = txq;
1554 /* Update send callback. */
1555 dev->tx_pkt_burst = mlx4_tx_burst;
1562 * DPDK callback to release a TX queue.
1565 * Generic TX queue pointer.
1568 mlx4_tx_queue_release(void *dpdk_txq)
1570 struct txq *txq = (struct txq *)dpdk_txq;
1578 for (i = 0; (i != priv->txqs_n); ++i)
1579 if ((*priv->txqs)[i] == txq) {
1580 DEBUG("%p: removing TX queue %p from list",
1581 (void *)priv->dev, (void *)txq);
1582 (*priv->txqs)[i] = NULL;
1590 /* RX queues handling. */
1593 * Allocate RX queue elements with scattered packets support.
1596 * Pointer to RX queue structure.
1598 * Number of elements to allocate.
1600 * If not NULL, fetch buffers from this array instead of allocating them
1601 * with rte_pktmbuf_alloc().
1604 * 0 on success, errno value on failure.
1607 rxq_alloc_elts_sp(struct rxq *rxq, unsigned int elts_n,
1608 struct rte_mbuf **pool)
1611 struct rxq_elt_sp (*elts)[elts_n] =
1612 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1617 ERROR("%p: can't allocate packets array", (void *)rxq);
1621 /* For each WR (packet). */
1622 for (i = 0; (i != elts_n); ++i) {
1624 struct rxq_elt_sp *elt = &(*elts)[i];
1625 struct ibv_recv_wr *wr = &elt->wr;
1626 struct ibv_sge (*sges)[(elemof(elt->sges))] = &elt->sges;
1628 /* These two arrays must have the same size. */
1629 assert(elemof(elt->sges) == elemof(elt->bufs));
1632 wr->next = &(*elts)[(i + 1)].wr;
1633 wr->sg_list = &(*sges)[0];
1634 wr->num_sge = elemof(*sges);
1635 /* For each SGE (segment). */
1636 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1637 struct ibv_sge *sge = &(*sges)[j];
1638 struct rte_mbuf *buf;
1642 assert(buf != NULL);
1643 rte_pktmbuf_reset(buf);
1645 buf = rte_pktmbuf_alloc(rxq->mp);
1647 assert(pool == NULL);
1648 ERROR("%p: empty mbuf pool", (void *)rxq);
1653 /* Headroom is reserved by rte_pktmbuf_alloc(). */
1654 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1655 /* Buffer is supposed to be empty. */
1656 assert(rte_pktmbuf_data_len(buf) == 0);
1657 assert(rte_pktmbuf_pkt_len(buf) == 0);
1658 /* sge->addr must be able to store a pointer. */
1659 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1661 /* The first SGE keeps its headroom. */
1662 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
1663 sge->length = (buf->buf_len -
1664 RTE_PKTMBUF_HEADROOM);
1666 /* Subsequent SGEs lose theirs. */
1667 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1668 SET_DATA_OFF(buf, 0);
1669 sge->addr = (uintptr_t)buf->buf_addr;
1670 sge->length = buf->buf_len;
1672 sge->lkey = rxq->mr->lkey;
1673 /* Redundant check for tailroom. */
1674 assert(sge->length == rte_pktmbuf_tailroom(buf));
1677 /* The last WR pointer must be NULL. */
1678 (*elts)[(i - 1)].wr.next = NULL;
1679 DEBUG("%p: allocated and configured %u WRs (%zu segments)",
1680 (void *)rxq, elts_n, (elts_n * elemof((*elts)[0].sges)));
1681 rxq->elts_n = elts_n;
1683 rxq->elts.sp = elts;
1688 assert(pool == NULL);
1689 for (i = 0; (i != elemof(*elts)); ++i) {
1691 struct rxq_elt_sp *elt = &(*elts)[i];
1693 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1694 struct rte_mbuf *buf = elt->bufs[j];
1697 rte_pktmbuf_free_seg(buf);
1702 DEBUG("%p: failed, freed everything", (void *)rxq);
1708 * Free RX queue elements with scattered packets support.
1711 * Pointer to RX queue structure.
1714 rxq_free_elts_sp(struct rxq *rxq)
1717 unsigned int elts_n = rxq->elts_n;
1718 struct rxq_elt_sp (*elts)[elts_n] = rxq->elts.sp;
1720 DEBUG("%p: freeing WRs", (void *)rxq);
1722 rxq->elts.sp = NULL;
1725 for (i = 0; (i != elemof(*elts)); ++i) {
1727 struct rxq_elt_sp *elt = &(*elts)[i];
1729 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1730 struct rte_mbuf *buf = elt->bufs[j];
1733 rte_pktmbuf_free_seg(buf);
1740 * Allocate RX queue elements.
1743 * Pointer to RX queue structure.
1745 * Number of elements to allocate.
1747 * If not NULL, fetch buffers from this array instead of allocating them
1748 * with rte_pktmbuf_alloc().
1751 * 0 on success, errno value on failure.
1754 rxq_alloc_elts(struct rxq *rxq, unsigned int elts_n, struct rte_mbuf **pool)
1757 struct rxq_elt (*elts)[elts_n] =
1758 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1763 ERROR("%p: can't allocate packets array", (void *)rxq);
1767 /* For each WR (packet). */
1768 for (i = 0; (i != elts_n); ++i) {
1769 struct rxq_elt *elt = &(*elts)[i];
1770 struct ibv_recv_wr *wr = &elt->wr;
1771 struct ibv_sge *sge = &(*elts)[i].sge;
1772 struct rte_mbuf *buf;
1776 assert(buf != NULL);
1777 rte_pktmbuf_reset(buf);
1779 buf = rte_pktmbuf_alloc(rxq->mp);
1781 assert(pool == NULL);
1782 ERROR("%p: empty mbuf pool", (void *)rxq);
1786 /* Configure WR. Work request ID contains its own index in
1787 * the elts array and the offset between SGE buffer header and
1789 WR_ID(wr->wr_id).id = i;
1790 WR_ID(wr->wr_id).offset =
1791 (((uintptr_t)buf->buf_addr + RTE_PKTMBUF_HEADROOM) -
1793 wr->next = &(*elts)[(i + 1)].wr;
1796 /* Headroom is reserved by rte_pktmbuf_alloc(). */
1797 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1798 /* Buffer is supposed to be empty. */
1799 assert(rte_pktmbuf_data_len(buf) == 0);
1800 assert(rte_pktmbuf_pkt_len(buf) == 0);
1801 /* sge->addr must be able to store a pointer. */
1802 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1803 /* SGE keeps its headroom. */
1804 sge->addr = (uintptr_t)
1805 ((uint8_t *)buf->buf_addr + RTE_PKTMBUF_HEADROOM);
1806 sge->length = (buf->buf_len - RTE_PKTMBUF_HEADROOM);
1807 sge->lkey = rxq->mr->lkey;
1808 /* Redundant check for tailroom. */
1809 assert(sge->length == rte_pktmbuf_tailroom(buf));
1810 /* Make sure elts index and SGE mbuf pointer can be deduced
1812 if ((WR_ID(wr->wr_id).id != i) ||
1813 ((void *)((uintptr_t)sge->addr -
1814 WR_ID(wr->wr_id).offset) != buf)) {
1815 ERROR("%p: cannot store index and offset in WR ID",
1818 rte_pktmbuf_free(buf);
1823 /* The last WR pointer must be NULL. */
1824 (*elts)[(i - 1)].wr.next = NULL;
1825 DEBUG("%p: allocated and configured %u single-segment WRs",
1826 (void *)rxq, elts_n);
1827 rxq->elts_n = elts_n;
1829 rxq->elts.no_sp = elts;
1834 assert(pool == NULL);
1835 for (i = 0; (i != elemof(*elts)); ++i) {
1836 struct rxq_elt *elt = &(*elts)[i];
1837 struct rte_mbuf *buf;
1839 if (elt->sge.addr == 0)
1841 assert(WR_ID(elt->wr.wr_id).id == i);
1842 buf = (void *)((uintptr_t)elt->sge.addr -
1843 WR_ID(elt->wr.wr_id).offset);
1844 rte_pktmbuf_free_seg(buf);
1848 DEBUG("%p: failed, freed everything", (void *)rxq);
1854 * Free RX queue elements.
1857 * Pointer to RX queue structure.
1860 rxq_free_elts(struct rxq *rxq)
1863 unsigned int elts_n = rxq->elts_n;
1864 struct rxq_elt (*elts)[elts_n] = rxq->elts.no_sp;
1866 DEBUG("%p: freeing WRs", (void *)rxq);
1868 rxq->elts.no_sp = NULL;
1871 for (i = 0; (i != elemof(*elts)); ++i) {
1872 struct rxq_elt *elt = &(*elts)[i];
1873 struct rte_mbuf *buf;
1875 if (elt->sge.addr == 0)
1877 assert(WR_ID(elt->wr.wr_id).id == i);
1878 buf = (void *)((uintptr_t)elt->sge.addr -
1879 WR_ID(elt->wr.wr_id).offset);
1880 rte_pktmbuf_free_seg(buf);
1886 * Delete flow steering rule.
1889 * Pointer to RX queue structure.
1891 * MAC address index.
1896 rxq_del_flow(struct rxq *rxq, unsigned int mac_index, unsigned int vlan_index)
1899 struct priv *priv = rxq->priv;
1900 const uint8_t (*mac)[ETHER_ADDR_LEN] =
1901 (const uint8_t (*)[ETHER_ADDR_LEN])
1902 priv->mac[mac_index].addr_bytes;
1904 assert(rxq->mac_flow[mac_index][vlan_index] != NULL);
1905 DEBUG("%p: removing MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
1906 " (VLAN ID %" PRIu16 ")",
1908 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
1909 mac_index, priv->vlan_filter[vlan_index].id);
1910 claim_zero(ibv_destroy_flow(rxq->mac_flow[mac_index][vlan_index]));
1911 rxq->mac_flow[mac_index][vlan_index] = NULL;
1915 * Unregister a MAC address from a RX queue.
1918 * Pointer to RX queue structure.
1920 * MAC address index.
1923 rxq_mac_addr_del(struct rxq *rxq, unsigned int mac_index)
1925 struct priv *priv = rxq->priv;
1927 unsigned int vlans = 0;
1929 assert(mac_index < elemof(priv->mac));
1930 if (!BITFIELD_ISSET(rxq->mac_configured, mac_index))
1932 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
1933 if (!priv->vlan_filter[i].enabled)
1935 rxq_del_flow(rxq, mac_index, i);
1939 rxq_del_flow(rxq, mac_index, 0);
1941 BITFIELD_RESET(rxq->mac_configured, mac_index);
1945 * Unregister all MAC addresses from a RX queue.
1948 * Pointer to RX queue structure.
1951 rxq_mac_addrs_del(struct rxq *rxq)
1953 struct priv *priv = rxq->priv;
1956 for (i = 0; (i != elemof(priv->mac)); ++i)
1957 rxq_mac_addr_del(rxq, i);
1960 static int rxq_promiscuous_enable(struct rxq *);
1961 static void rxq_promiscuous_disable(struct rxq *);
1964 * Add single flow steering rule.
1967 * Pointer to RX queue structure.
1969 * MAC address index to register.
1971 * VLAN index. Use -1 for a flow without VLAN.
1974 * 0 on success, errno value on failure.
1977 rxq_add_flow(struct rxq *rxq, unsigned int mac_index, unsigned int vlan_index)
1979 struct ibv_flow *flow;
1980 struct priv *priv = rxq->priv;
1981 const uint8_t (*mac)[ETHER_ADDR_LEN] =
1982 (const uint8_t (*)[ETHER_ADDR_LEN])
1983 priv->mac[mac_index].addr_bytes;
1985 /* Allocate flow specification on the stack. */
1986 struct __attribute__((packed)) {
1987 struct ibv_flow_attr attr;
1988 struct ibv_flow_spec_eth spec;
1990 struct ibv_flow_attr *attr = &data.attr;
1991 struct ibv_flow_spec_eth *spec = &data.spec;
1993 assert(mac_index < elemof(priv->mac));
1994 assert((vlan_index < elemof(priv->vlan_filter)) || (vlan_index == -1u));
1996 * No padding must be inserted by the compiler between attr and spec.
1997 * This layout is expected by libibverbs.
1999 assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec);
2000 *attr = (struct ibv_flow_attr){
2001 .type = IBV_FLOW_ATTR_NORMAL,
2006 *spec = (struct ibv_flow_spec_eth){
2007 .type = IBV_FLOW_SPEC_ETH,
2008 .size = sizeof(*spec),
2011 (*mac)[0], (*mac)[1], (*mac)[2],
2012 (*mac)[3], (*mac)[4], (*mac)[5]
2014 .vlan_tag = ((vlan_index != -1u) ?
2015 htons(priv->vlan_filter[vlan_index].id) :
2019 .dst_mac = "\xff\xff\xff\xff\xff\xff",
2020 .vlan_tag = ((vlan_index != -1u) ? htons(0xfff) : 0),
2023 DEBUG("%p: adding MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
2024 " (VLAN %s %" PRIu16 ")",
2026 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
2028 ((vlan_index != -1u) ? "ID" : "index"),
2029 ((vlan_index != -1u) ? priv->vlan_filter[vlan_index].id : -1u));
2030 /* Create related flow. */
2032 flow = ibv_create_flow(rxq->qp, attr);
2034 /* It's not clear whether errno is always set in this case. */
2035 ERROR("%p: flow configuration failed, errno=%d: %s",
2037 (errno ? strerror(errno) : "Unknown error"));
2042 if (vlan_index == -1u)
2044 assert(rxq->mac_flow[mac_index][vlan_index] == NULL);
2045 rxq->mac_flow[mac_index][vlan_index] = flow;
2050 * Register a MAC address in a RX queue.
2053 * Pointer to RX queue structure.
2055 * MAC address index to register.
2058 * 0 on success, errno value on failure.
2061 rxq_mac_addr_add(struct rxq *rxq, unsigned int mac_index)
2063 struct priv *priv = rxq->priv;
2065 unsigned int vlans = 0;
2068 assert(mac_index < elemof(priv->mac));
2069 if (BITFIELD_ISSET(rxq->mac_configured, mac_index))
2070 rxq_mac_addr_del(rxq, mac_index);
2071 /* Fill VLAN specifications. */
2072 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
2073 if (!priv->vlan_filter[i].enabled)
2075 /* Create related flow. */
2076 ret = rxq_add_flow(rxq, mac_index, i);
2081 /* Failure, rollback. */
2083 if (priv->vlan_filter[--i].enabled)
2084 rxq_del_flow(rxq, mac_index, i);
2088 /* In case there is no VLAN filter. */
2090 ret = rxq_add_flow(rxq, mac_index, -1);
2094 BITFIELD_SET(rxq->mac_configured, mac_index);
2099 * Register all MAC addresses in a RX queue.
2102 * Pointer to RX queue structure.
2105 * 0 on success, errno value on failure.
2108 rxq_mac_addrs_add(struct rxq *rxq)
2110 struct priv *priv = rxq->priv;
2114 for (i = 0; (i != elemof(priv->mac)); ++i) {
2115 if (!BITFIELD_ISSET(priv->mac_configured, i))
2117 ret = rxq_mac_addr_add(rxq, i);
2120 /* Failure, rollback. */
2122 rxq_mac_addr_del(rxq, --i);
2130 * Unregister a MAC address.
2132 * In RSS mode, the MAC address is unregistered from the parent queue,
2133 * otherwise it is unregistered from each queue directly.
2136 * Pointer to private structure.
2138 * MAC address index.
2141 priv_mac_addr_del(struct priv *priv, unsigned int mac_index)
2145 assert(mac_index < elemof(priv->mac));
2146 if (!BITFIELD_ISSET(priv->mac_configured, mac_index))
2149 rxq_mac_addr_del(&priv->rxq_parent, mac_index);
2152 for (i = 0; (i != priv->dev->data->nb_rx_queues); ++i)
2153 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2155 BITFIELD_RESET(priv->mac_configured, mac_index);
2159 * Register a MAC address.
2161 * In RSS mode, the MAC address is registered in the parent queue,
2162 * otherwise it is registered in each queue directly.
2165 * Pointer to private structure.
2167 * MAC address index to use.
2169 * MAC address to register.
2172 * 0 on success, errno value on failure.
2175 priv_mac_addr_add(struct priv *priv, unsigned int mac_index,
2176 const uint8_t (*mac)[ETHER_ADDR_LEN])
2181 assert(mac_index < elemof(priv->mac));
2182 /* First, make sure this address isn't already configured. */
2183 for (i = 0; (i != elemof(priv->mac)); ++i) {
2184 /* Skip this index, it's going to be reconfigured. */
2187 if (!BITFIELD_ISSET(priv->mac_configured, i))
2189 if (memcmp(priv->mac[i].addr_bytes, *mac, sizeof(*mac)))
2191 /* Address already configured elsewhere, return with error. */
2194 if (BITFIELD_ISSET(priv->mac_configured, mac_index))
2195 priv_mac_addr_del(priv, mac_index);
2196 priv->mac[mac_index] = (struct ether_addr){
2198 (*mac)[0], (*mac)[1], (*mac)[2],
2199 (*mac)[3], (*mac)[4], (*mac)[5]
2202 /* If device isn't started, this is all we need to do. */
2203 if (!priv->started) {
2205 /* Verify that all queues have this index disabled. */
2206 for (i = 0; (i != priv->rxqs_n); ++i) {
2207 if ((*priv->rxqs)[i] == NULL)
2209 assert(!BITFIELD_ISSET
2210 ((*priv->rxqs)[i]->mac_configured, mac_index));
2216 ret = rxq_mac_addr_add(&priv->rxq_parent, mac_index);
2221 for (i = 0; (i != priv->rxqs_n); ++i) {
2222 if ((*priv->rxqs)[i] == NULL)
2224 ret = rxq_mac_addr_add((*priv->rxqs)[i], mac_index);
2227 /* Failure, rollback. */
2229 if ((*priv->rxqs)[(--i)] != NULL)
2230 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2234 BITFIELD_SET(priv->mac_configured, mac_index);
2239 * Enable allmulti mode in a RX queue.
2242 * Pointer to RX queue structure.
2245 * 0 on success, errno value on failure.
2248 rxq_allmulticast_enable(struct rxq *rxq)
2250 struct ibv_flow *flow;
2251 struct ibv_flow_attr attr = {
2252 .type = IBV_FLOW_ATTR_MC_DEFAULT,
2254 .port = rxq->priv->port,
2258 DEBUG("%p: enabling allmulticast mode", (void *)rxq);
2259 if (rxq->allmulti_flow != NULL)
2262 flow = ibv_create_flow(rxq->qp, &attr);
2264 /* It's not clear whether errno is always set in this case. */
2265 ERROR("%p: flow configuration failed, errno=%d: %s",
2267 (errno ? strerror(errno) : "Unknown error"));
2272 rxq->allmulti_flow = flow;
2273 DEBUG("%p: allmulticast mode enabled", (void *)rxq);
2278 * Disable allmulti mode in a RX queue.
2281 * Pointer to RX queue structure.
2284 rxq_allmulticast_disable(struct rxq *rxq)
2286 DEBUG("%p: disabling allmulticast mode", (void *)rxq);
2287 if (rxq->allmulti_flow == NULL)
2289 claim_zero(ibv_destroy_flow(rxq->allmulti_flow));
2290 rxq->allmulti_flow = NULL;
2291 DEBUG("%p: allmulticast mode disabled", (void *)rxq);
2295 * Enable promiscuous mode in a RX queue.
2298 * Pointer to RX queue structure.
2301 * 0 on success, errno value on failure.
2304 rxq_promiscuous_enable(struct rxq *rxq)
2306 struct ibv_flow *flow;
2307 struct ibv_flow_attr attr = {
2308 .type = IBV_FLOW_ATTR_ALL_DEFAULT,
2310 .port = rxq->priv->port,
2316 DEBUG("%p: enabling promiscuous mode", (void *)rxq);
2317 if (rxq->promisc_flow != NULL)
2320 flow = ibv_create_flow(rxq->qp, &attr);
2322 /* It's not clear whether errno is always set in this case. */
2323 ERROR("%p: flow configuration failed, errno=%d: %s",
2325 (errno ? strerror(errno) : "Unknown error"));
2330 rxq->promisc_flow = flow;
2331 DEBUG("%p: promiscuous mode enabled", (void *)rxq);
2336 * Disable promiscuous mode in a RX queue.
2339 * Pointer to RX queue structure.
2342 rxq_promiscuous_disable(struct rxq *rxq)
2346 DEBUG("%p: disabling promiscuous mode", (void *)rxq);
2347 if (rxq->promisc_flow == NULL)
2349 claim_zero(ibv_destroy_flow(rxq->promisc_flow));
2350 rxq->promisc_flow = NULL;
2351 DEBUG("%p: promiscuous mode disabled", (void *)rxq);
2355 * Clean up a RX queue.
2357 * Destroy objects, free allocated memory and reset the structure for reuse.
2360 * Pointer to RX queue structure.
2363 rxq_cleanup(struct rxq *rxq)
2365 struct ibv_exp_release_intf_params params;
2367 DEBUG("cleaning up %p", (void *)rxq);
2369 rxq_free_elts_sp(rxq);
2372 if (rxq->if_qp != NULL) {
2373 assert(rxq->priv != NULL);
2374 assert(rxq->priv->ctx != NULL);
2375 assert(rxq->qp != NULL);
2376 params = (struct ibv_exp_release_intf_params){
2379 claim_zero(ibv_exp_release_intf(rxq->priv->ctx,
2383 if (rxq->if_cq != NULL) {
2384 assert(rxq->priv != NULL);
2385 assert(rxq->priv->ctx != NULL);
2386 assert(rxq->cq != NULL);
2387 params = (struct ibv_exp_release_intf_params){
2390 claim_zero(ibv_exp_release_intf(rxq->priv->ctx,
2394 if (rxq->qp != NULL) {
2395 rxq_promiscuous_disable(rxq);
2396 rxq_allmulticast_disable(rxq);
2397 rxq_mac_addrs_del(rxq);
2398 claim_zero(ibv_destroy_qp(rxq->qp));
2400 if (rxq->cq != NULL)
2401 claim_zero(ibv_destroy_cq(rxq->cq));
2402 if (rxq->mr != NULL)
2403 claim_zero(ibv_dereg_mr(rxq->mr));
2404 memset(rxq, 0, sizeof(*rxq));
2408 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n);
2411 * DPDK callback for RX with scattered packets support.
2414 * Generic pointer to RX queue structure.
2416 * Array to store received packets.
2418 * Maximum number of packets in array.
2421 * Number of packets successfully received (<= pkts_n).
2424 mlx4_rx_burst_sp(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2426 struct rxq *rxq = (struct rxq *)dpdk_rxq;
2427 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
2428 const unsigned int elts_n = rxq->elts_n;
2429 unsigned int elts_head = rxq->elts_head;
2430 struct ibv_recv_wr head;
2431 struct ibv_recv_wr **next = &head.next;
2432 struct ibv_recv_wr *bad_wr;
2434 unsigned int pkts_ret = 0;
2437 if (unlikely(!rxq->sp))
2438 return mlx4_rx_burst(dpdk_rxq, pkts, pkts_n);
2439 if (unlikely(elts == NULL)) /* See RTE_DEV_CMD_SET_MTU. */
2441 for (i = 0; (i != pkts_n); ++i) {
2442 struct rxq_elt_sp *elt = &(*elts)[elts_head];
2443 struct ibv_recv_wr *wr = &elt->wr;
2444 uint64_t wr_id = wr->wr_id;
2446 unsigned int pkt_buf_len;
2447 struct rte_mbuf *pkt_buf = NULL; /* Buffer returned in pkts. */
2448 struct rte_mbuf **pkt_buf_next = &pkt_buf;
2449 unsigned int seg_headroom = RTE_PKTMBUF_HEADROOM;
2452 /* Sanity checks. */
2456 assert(wr_id < rxq->elts_n);
2457 assert(wr->sg_list == elt->sges);
2458 assert(wr->num_sge == elemof(elt->sges));
2459 assert(elts_head < rxq->elts_n);
2460 assert(rxq->elts_head < rxq->elts_n);
2461 ret = rxq->if_cq->poll_length(rxq->cq, NULL, NULL);
2462 if (unlikely(ret < 0)) {
2466 DEBUG("rxq=%p, poll_length() failed (ret=%d)",
2468 /* ibv_poll_cq() must be used in case of failure. */
2469 wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
2470 if (unlikely(wcs_n == 0))
2472 if (unlikely(wcs_n < 0)) {
2473 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
2474 (void *)rxq, wcs_n);
2478 if (unlikely(wc.status != IBV_WC_SUCCESS)) {
2479 /* Whatever, just repost the offending WR. */
2480 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
2481 " completion status (%d): %s",
2482 (void *)rxq, wc.wr_id, wc.status,
2483 ibv_wc_status_str(wc.status));
2484 #ifdef MLX4_PMD_SOFT_COUNTERS
2485 /* Increment dropped packets counter. */
2486 ++rxq->stats.idropped;
2488 /* Link completed WRs together for repost. */
2499 /* Link completed WRs together for repost. */
2503 * Replace spent segments with new ones, concatenate and
2504 * return them as pkt_buf.
2507 struct ibv_sge *sge = &elt->sges[j];
2508 struct rte_mbuf *seg = elt->bufs[j];
2509 struct rte_mbuf *rep;
2510 unsigned int seg_tailroom;
2513 * Fetch initial bytes of packet descriptor into a
2514 * cacheline while allocating rep.
2517 rep = __rte_mbuf_raw_alloc(rxq->mp);
2518 if (unlikely(rep == NULL)) {
2520 * Unable to allocate a replacement mbuf,
2523 DEBUG("rxq=%p, wr_id=%" PRIu64 ":"
2524 " can't allocate a new mbuf",
2525 (void *)rxq, wr_id);
2526 if (pkt_buf != NULL) {
2527 *pkt_buf_next = NULL;
2528 rte_pktmbuf_free(pkt_buf);
2530 /* Increase out of memory counters. */
2531 ++rxq->stats.rx_nombuf;
2532 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
2536 /* Poison user-modifiable fields in rep. */
2537 NEXT(rep) = (void *)((uintptr_t)-1);
2538 SET_DATA_OFF(rep, 0xdead);
2539 DATA_LEN(rep) = 0xd00d;
2540 PKT_LEN(rep) = 0xdeadd00d;
2541 NB_SEGS(rep) = 0x2a;
2545 assert(rep->buf_len == seg->buf_len);
2546 assert(rep->buf_len == rxq->mb_len);
2547 /* Reconfigure sge to use rep instead of seg. */
2548 assert(sge->lkey == rxq->mr->lkey);
2549 sge->addr = ((uintptr_t)rep->buf_addr + seg_headroom);
2552 /* Update pkt_buf if it's the first segment, or link
2553 * seg to the previous one and update pkt_buf_next. */
2554 *pkt_buf_next = seg;
2555 pkt_buf_next = &NEXT(seg);
2556 /* Update seg information. */
2557 seg_tailroom = (seg->buf_len - seg_headroom);
2558 assert(sge->length == seg_tailroom);
2559 SET_DATA_OFF(seg, seg_headroom);
2560 if (likely(len <= seg_tailroom)) {
2562 DATA_LEN(seg) = len;
2565 assert(rte_pktmbuf_headroom(seg) ==
2567 assert(rte_pktmbuf_tailroom(seg) ==
2568 (seg_tailroom - len));
2571 DATA_LEN(seg) = seg_tailroom;
2572 PKT_LEN(seg) = seg_tailroom;
2574 assert(rte_pktmbuf_headroom(seg) == seg_headroom);
2575 assert(rte_pktmbuf_tailroom(seg) == 0);
2576 /* Fix len and clear headroom for next segments. */
2577 len -= seg_tailroom;
2580 /* Update head and tail segments. */
2581 *pkt_buf_next = NULL;
2582 assert(pkt_buf != NULL);
2584 NB_SEGS(pkt_buf) = j;
2585 PORT(pkt_buf) = rxq->port_id;
2586 PKT_LEN(pkt_buf) = pkt_buf_len;
2587 pkt_buf->ol_flags = 0;
2589 /* Return packet. */
2590 *(pkts++) = pkt_buf;
2592 #ifdef MLX4_PMD_SOFT_COUNTERS
2593 /* Increase bytes counter. */
2594 rxq->stats.ibytes += pkt_buf_len;
2597 if (++elts_head >= elts_n)
2601 if (unlikely(i == 0))
2606 DEBUG("%p: reposting %d WRs", (void *)rxq, i);
2608 ret = ibv_post_recv(rxq->qp, head.next, &bad_wr);
2609 if (unlikely(ret)) {
2610 /* Inability to repost WRs is fatal. */
2611 DEBUG("%p: ibv_post_recv(): failed for WR %p: %s",
2617 rxq->elts_head = elts_head;
2618 #ifdef MLX4_PMD_SOFT_COUNTERS
2619 /* Increase packets counter. */
2620 rxq->stats.ipackets += pkts_ret;
2626 * DPDK callback for RX.
2628 * The following function is the same as mlx4_rx_burst_sp(), except it doesn't
2629 * manage scattered packets. Improves performance when MRU is lower than the
2630 * size of the first segment.
2633 * Generic pointer to RX queue structure.
2635 * Array to store received packets.
2637 * Maximum number of packets in array.
2640 * Number of packets successfully received (<= pkts_n).
2643 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2645 struct rxq *rxq = (struct rxq *)dpdk_rxq;
2646 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
2647 const unsigned int elts_n = rxq->elts_n;
2648 unsigned int elts_head = rxq->elts_head;
2649 struct ibv_sge sges[pkts_n];
2651 unsigned int pkts_ret = 0;
2654 if (unlikely(rxq->sp))
2655 return mlx4_rx_burst_sp(dpdk_rxq, pkts, pkts_n);
2656 for (i = 0; (i != pkts_n); ++i) {
2657 struct rxq_elt *elt = &(*elts)[elts_head];
2658 struct ibv_recv_wr *wr = &elt->wr;
2659 uint64_t wr_id = wr->wr_id;
2661 struct rte_mbuf *seg = (void *)((uintptr_t)elt->sge.addr -
2662 WR_ID(wr_id).offset);
2663 struct rte_mbuf *rep;
2665 /* Sanity checks. */
2666 assert(WR_ID(wr_id).id < rxq->elts_n);
2667 assert(wr->sg_list == &elt->sge);
2668 assert(wr->num_sge == 1);
2669 assert(elts_head < rxq->elts_n);
2670 assert(rxq->elts_head < rxq->elts_n);
2671 ret = rxq->if_cq->poll_length(rxq->cq, NULL, NULL);
2672 if (unlikely(ret < 0)) {
2676 DEBUG("rxq=%p, poll_length() failed (ret=%d)",
2678 /* ibv_poll_cq() must be used in case of failure. */
2679 wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
2680 if (unlikely(wcs_n == 0))
2682 if (unlikely(wcs_n < 0)) {
2683 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
2684 (void *)rxq, wcs_n);
2688 if (unlikely(wc.status != IBV_WC_SUCCESS)) {
2689 /* Whatever, just repost the offending WR. */
2690 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
2691 " completion status (%d): %s",
2692 (void *)rxq, wc.wr_id, wc.status,
2693 ibv_wc_status_str(wc.status));
2694 #ifdef MLX4_PMD_SOFT_COUNTERS
2695 /* Increment dropped packets counter. */
2696 ++rxq->stats.idropped;
2698 /* Add SGE to array for repost. */
2708 * Fetch initial bytes of packet descriptor into a
2709 * cacheline while allocating rep.
2712 rep = __rte_mbuf_raw_alloc(rxq->mp);
2713 if (unlikely(rep == NULL)) {
2715 * Unable to allocate a replacement mbuf,
2718 DEBUG("rxq=%p, wr_id=%" PRIu32 ":"
2719 " can't allocate a new mbuf",
2720 (void *)rxq, WR_ID(wr_id).id);
2721 /* Increase out of memory counters. */
2722 ++rxq->stats.rx_nombuf;
2723 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
2727 /* Reconfigure sge to use rep instead of seg. */
2728 elt->sge.addr = (uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM;
2729 assert(elt->sge.lkey == rxq->mr->lkey);
2730 WR_ID(wr->wr_id).offset =
2731 (((uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM) -
2733 assert(WR_ID(wr->wr_id).id == WR_ID(wr_id).id);
2735 /* Add SGE to array for repost. */
2738 /* Update seg information. */
2739 SET_DATA_OFF(seg, RTE_PKTMBUF_HEADROOM);
2741 PORT(seg) = rxq->port_id;
2744 DATA_LEN(seg) = len;
2747 /* Return packet. */
2750 #ifdef MLX4_PMD_SOFT_COUNTERS
2751 /* Increase bytes counter. */
2752 rxq->stats.ibytes += len;
2755 if (++elts_head >= elts_n)
2759 if (unlikely(i == 0))
2763 DEBUG("%p: reposting %u WRs", (void *)rxq, i);
2765 ret = rxq->if_qp->recv_burst(rxq->qp, sges, i);
2766 if (unlikely(ret)) {
2767 /* Inability to repost WRs is fatal. */
2768 DEBUG("%p: recv_burst(): failed (ret=%d)",
2773 rxq->elts_head = elts_head;
2774 #ifdef MLX4_PMD_SOFT_COUNTERS
2775 /* Increase packets counter. */
2776 rxq->stats.ipackets += pkts_ret;
2782 * Allocate a Queue Pair.
2783 * Optionally setup inline receive if supported.
2786 * Pointer to private structure.
2788 * Completion queue to associate with QP.
2790 * Number of descriptors in QP (hint only).
2793 * QP pointer or NULL in case of error.
2795 static struct ibv_qp *
2796 rxq_setup_qp(struct priv *priv, struct ibv_cq *cq, uint16_t desc)
2798 struct ibv_exp_qp_init_attr attr = {
2799 /* CQ to be associated with the send queue. */
2801 /* CQ to be associated with the receive queue. */
2804 /* Max number of outstanding WRs. */
2805 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
2806 priv->device_attr.max_qp_wr :
2808 /* Max number of scatter/gather elements in a WR. */
2809 .max_recv_sge = ((priv->device_attr.max_sge <
2810 MLX4_PMD_SGE_WR_N) ?
2811 priv->device_attr.max_sge :
2814 .qp_type = IBV_QPT_RAW_PACKET,
2815 .comp_mask = IBV_EXP_QP_INIT_ATTR_PD,
2820 attr.max_inl_recv = priv->inl_recv_size;
2821 attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
2823 return ibv_exp_create_qp(priv->ctx, &attr);
2829 * Allocate a RSS Queue Pair.
2830 * Optionally setup inline receive if supported.
2833 * Pointer to private structure.
2835 * Completion queue to associate with QP.
2837 * Number of descriptors in QP (hint only).
2839 * If nonzero, create a parent QP, otherwise a child.
2842 * QP pointer or NULL in case of error.
2844 static struct ibv_qp *
2845 rxq_setup_qp_rss(struct priv *priv, struct ibv_cq *cq, uint16_t desc,
2848 struct ibv_exp_qp_init_attr attr = {
2849 /* CQ to be associated with the send queue. */
2851 /* CQ to be associated with the receive queue. */
2854 /* Max number of outstanding WRs. */
2855 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
2856 priv->device_attr.max_qp_wr :
2858 /* Max number of scatter/gather elements in a WR. */
2859 .max_recv_sge = ((priv->device_attr.max_sge <
2860 MLX4_PMD_SGE_WR_N) ?
2861 priv->device_attr.max_sge :
2864 .qp_type = IBV_QPT_RAW_PACKET,
2865 .comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
2866 IBV_EXP_QP_INIT_ATTR_QPG),
2871 attr.max_inl_recv = priv->inl_recv_size,
2872 attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
2875 attr.qpg.qpg_type = IBV_EXP_QPG_PARENT;
2876 /* TSS isn't necessary. */
2877 attr.qpg.parent_attrib.tss_child_count = 0;
2878 attr.qpg.parent_attrib.rss_child_count = priv->rxqs_n;
2879 DEBUG("initializing parent RSS queue");
2881 attr.qpg.qpg_type = IBV_EXP_QPG_CHILD_RX;
2882 attr.qpg.qpg_parent = priv->rxq_parent.qp;
2883 DEBUG("initializing child RSS queue");
2885 return ibv_exp_create_qp(priv->ctx, &attr);
2888 #endif /* RSS_SUPPORT */
2891 * Reconfigure a RX queue with new parameters.
2893 * rxq_rehash() does not allocate mbufs, which, if not done from the right
2894 * thread (such as a control thread), may corrupt the pool.
2895 * In case of failure, the queue is left untouched.
2898 * Pointer to Ethernet device structure.
2903 * 0 on success, errno value on failure.
2906 rxq_rehash(struct rte_eth_dev *dev, struct rxq *rxq)
2908 struct priv *priv = rxq->priv;
2909 struct rxq tmpl = *rxq;
2910 unsigned int mbuf_n;
2911 unsigned int desc_n;
2912 struct rte_mbuf **pool;
2914 struct ibv_exp_qp_attr mod;
2915 struct ibv_recv_wr *bad_wr;
2917 int parent = (rxq == &priv->rxq_parent);
2920 ERROR("%p: cannot rehash parent queue %p",
2921 (void *)dev, (void *)rxq);
2924 DEBUG("%p: rehashing queue %p", (void *)dev, (void *)rxq);
2925 /* Number of descriptors and mbufs currently allocated. */
2926 desc_n = (tmpl.elts_n * (tmpl.sp ? MLX4_PMD_SGE_WR_N : 1));
2928 /* Enable scattered packets support for this queue if necessary. */
2929 if ((dev->data->dev_conf.rxmode.jumbo_frame) &&
2930 (dev->data->dev_conf.rxmode.max_rx_pkt_len >
2931 (tmpl.mb_len - RTE_PKTMBUF_HEADROOM))) {
2933 desc_n /= MLX4_PMD_SGE_WR_N;
2936 DEBUG("%p: %s scattered packets support (%u WRs)",
2937 (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc_n);
2938 /* If scatter mode is the same as before, nothing to do. */
2939 if (tmpl.sp == rxq->sp) {
2940 DEBUG("%p: nothing to do", (void *)dev);
2943 /* Remove attached flows if RSS is disabled (no parent queue). */
2945 rxq_allmulticast_disable(&tmpl);
2946 rxq_promiscuous_disable(&tmpl);
2947 rxq_mac_addrs_del(&tmpl);
2948 /* Update original queue in case of failure. */
2949 rxq->allmulti_flow = tmpl.allmulti_flow;
2950 rxq->promisc_flow = tmpl.promisc_flow;
2951 memcpy(rxq->mac_configured, tmpl.mac_configured,
2952 sizeof(rxq->mac_configured));
2953 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
2955 /* From now on, any failure will render the queue unusable.
2956 * Reinitialize QP. */
2957 mod = (struct ibv_exp_qp_attr){ .qp_state = IBV_QPS_RESET };
2958 err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
2960 ERROR("%p: cannot reset QP: %s", (void *)dev, strerror(err));
2964 err = ibv_resize_cq(tmpl.cq, desc_n);
2966 ERROR("%p: cannot resize CQ: %s", (void *)dev, strerror(err));
2970 mod = (struct ibv_exp_qp_attr){
2971 /* Move the QP to this state. */
2972 .qp_state = IBV_QPS_INIT,
2973 /* Primary port number. */
2974 .port_num = priv->port
2976 err = ibv_exp_modify_qp(tmpl.qp, &mod,
2979 (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
2980 #endif /* RSS_SUPPORT */
2983 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
2984 (void *)dev, strerror(err));
2988 /* Reconfigure flows. Do not care for errors. */
2990 rxq_mac_addrs_add(&tmpl);
2992 rxq_promiscuous_enable(&tmpl);
2994 rxq_allmulticast_enable(&tmpl);
2995 /* Update original queue in case of failure. */
2996 rxq->allmulti_flow = tmpl.allmulti_flow;
2997 rxq->promisc_flow = tmpl.promisc_flow;
2998 memcpy(rxq->mac_configured, tmpl.mac_configured,
2999 sizeof(rxq->mac_configured));
3000 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
3002 /* Allocate pool. */
3003 pool = rte_malloc(__func__, (mbuf_n * sizeof(*pool)), 0);
3005 ERROR("%p: cannot allocate memory", (void *)dev);
3008 /* Snatch mbufs from original queue. */
3011 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
3013 for (i = 0; (i != elemof(*elts)); ++i) {
3014 struct rxq_elt_sp *elt = &(*elts)[i];
3017 for (j = 0; (j != elemof(elt->bufs)); ++j) {
3018 assert(elt->bufs[j] != NULL);
3019 pool[k++] = elt->bufs[j];
3023 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
3025 for (i = 0; (i != elemof(*elts)); ++i) {
3026 struct rxq_elt *elt = &(*elts)[i];
3027 struct rte_mbuf *buf = (void *)
3028 ((uintptr_t)elt->sge.addr -
3029 WR_ID(elt->wr.wr_id).offset);
3031 assert(WR_ID(elt->wr.wr_id).id == i);
3035 assert(k == mbuf_n);
3037 tmpl.elts.sp = NULL;
3038 assert((void *)&tmpl.elts.sp == (void *)&tmpl.elts.no_sp);
3040 rxq_alloc_elts_sp(&tmpl, desc_n, pool) :
3041 rxq_alloc_elts(&tmpl, desc_n, pool));
3043 ERROR("%p: cannot reallocate WRs, aborting", (void *)dev);
3048 assert(tmpl.elts_n == desc_n);
3049 assert(tmpl.elts.sp != NULL);
3051 /* Clean up original data. */
3053 rte_free(rxq->elts.sp);
3054 rxq->elts.sp = NULL;
3056 err = ibv_post_recv(tmpl.qp,
3058 &(*tmpl.elts.sp)[0].wr :
3059 &(*tmpl.elts.no_sp)[0].wr),
3062 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
3068 mod = (struct ibv_exp_qp_attr){
3069 .qp_state = IBV_QPS_RTR
3071 err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3073 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
3074 (void *)dev, strerror(err));
3082 * Configure a RX queue.
3085 * Pointer to Ethernet device structure.
3087 * Pointer to RX queue structure.
3089 * Number of descriptors to configure in queue.
3091 * NUMA socket on which memory must be allocated.
3093 * Thresholds parameters.
3095 * Memory pool for buffer allocations.
3098 * 0 on success, errno value on failure.
3101 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
3102 unsigned int socket, const struct rte_eth_rxconf *conf,
3103 struct rte_mempool *mp)
3105 struct priv *priv = dev->data->dev_private;
3111 struct ibv_exp_qp_attr mod;
3113 struct ibv_exp_query_intf_params params;
3115 enum ibv_exp_query_intf_status status;
3116 struct ibv_recv_wr *bad_wr;
3117 struct rte_mbuf *buf;
3119 int parent = (rxq == &priv->rxq_parent);
3121 (void)conf; /* Thresholds configuration (ignored). */
3123 * If this is a parent queue, hardware must support RSS and
3124 * RSS must be enabled.
3126 assert((!parent) || ((priv->hw_rss) && (priv->rss)));
3128 /* Even if unused, ibv_create_cq() requires at least one
3133 if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
3134 ERROR("%p: invalid number of RX descriptors (must be a"
3135 " multiple of %d)", (void *)dev, MLX4_PMD_SGE_WR_N);
3138 /* Get mbuf length. */
3139 buf = rte_pktmbuf_alloc(mp);
3141 ERROR("%p: unable to allocate mbuf", (void *)dev);
3144 tmpl.mb_len = buf->buf_len;
3145 assert((rte_pktmbuf_headroom(buf) +
3146 rte_pktmbuf_tailroom(buf)) == tmpl.mb_len);
3147 assert(rte_pktmbuf_headroom(buf) == RTE_PKTMBUF_HEADROOM);
3148 rte_pktmbuf_free(buf);
3149 /* Enable scattered packets support for this queue if necessary. */
3150 if ((dev->data->dev_conf.rxmode.jumbo_frame) &&
3151 (dev->data->dev_conf.rxmode.max_rx_pkt_len >
3152 (tmpl.mb_len - RTE_PKTMBUF_HEADROOM))) {
3154 desc /= MLX4_PMD_SGE_WR_N;
3156 DEBUG("%p: %s scattered packets support (%u WRs)",
3157 (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc);
3158 /* Use the entire RX mempool as the memory region. */
3159 tmpl.mr = ibv_reg_mr(priv->pd,
3160 (void *)mp->elt_va_start,
3161 (mp->elt_va_end - mp->elt_va_start),
3162 (IBV_ACCESS_LOCAL_WRITE |
3163 IBV_ACCESS_REMOTE_WRITE));
3164 if (tmpl.mr == NULL) {
3166 ERROR("%p: MR creation failure: %s",
3167 (void *)dev, strerror(ret));
3171 tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
3172 if (tmpl.cq == NULL) {
3174 ERROR("%p: CQ creation failure: %s",
3175 (void *)dev, strerror(ret));
3178 DEBUG("priv->device_attr.max_qp_wr is %d",
3179 priv->device_attr.max_qp_wr);
3180 DEBUG("priv->device_attr.max_sge is %d",
3181 priv->device_attr.max_sge);
3184 tmpl.qp = rxq_setup_qp_rss(priv, tmpl.cq, desc, parent);
3186 #endif /* RSS_SUPPORT */
3187 tmpl.qp = rxq_setup_qp(priv, tmpl.cq, desc);
3188 if (tmpl.qp == NULL) {
3189 ret = (errno ? errno : EINVAL);
3190 ERROR("%p: QP creation failure: %s",
3191 (void *)dev, strerror(ret));
3194 mod = (struct ibv_exp_qp_attr){
3195 /* Move the QP to this state. */
3196 .qp_state = IBV_QPS_INIT,
3197 /* Primary port number. */
3198 .port_num = priv->port
3200 ret = ibv_exp_modify_qp(tmpl.qp, &mod,
3203 (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
3204 #endif /* RSS_SUPPORT */
3207 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
3208 (void *)dev, strerror(ret));
3211 if ((parent) || (!priv->rss)) {
3212 /* Configure MAC and broadcast addresses. */
3213 ret = rxq_mac_addrs_add(&tmpl);
3215 ERROR("%p: QP flow attachment failed: %s",
3216 (void *)dev, strerror(ret));
3220 /* Allocate descriptors for RX queues, except for the RSS parent. */
3224 ret = rxq_alloc_elts_sp(&tmpl, desc, NULL);
3226 ret = rxq_alloc_elts(&tmpl, desc, NULL);
3228 ERROR("%p: RXQ allocation failed: %s",
3229 (void *)dev, strerror(ret));
3232 ret = ibv_post_recv(tmpl.qp,
3234 &(*tmpl.elts.sp)[0].wr :
3235 &(*tmpl.elts.no_sp)[0].wr),
3238 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
3245 mod = (struct ibv_exp_qp_attr){
3246 .qp_state = IBV_QPS_RTR
3248 ret = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3250 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
3251 (void *)dev, strerror(ret));
3255 tmpl.port_id = dev->data->port_id;
3256 DEBUG("%p: RTE port ID: %u", (void *)rxq, tmpl.port_id);
3257 attr.params = (struct ibv_exp_query_intf_params){
3258 .intf_scope = IBV_EXP_INTF_GLOBAL,
3259 .intf = IBV_EXP_INTF_CQ,
3262 tmpl.if_cq = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
3263 if (tmpl.if_cq == NULL) {
3264 ERROR("%p: CQ interface family query failed with status %d",
3265 (void *)dev, status);
3268 attr.params = (struct ibv_exp_query_intf_params){
3269 .intf_scope = IBV_EXP_INTF_GLOBAL,
3270 .intf = IBV_EXP_INTF_QP_BURST,
3273 tmpl.if_qp = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
3274 if (tmpl.if_qp == NULL) {
3275 ERROR("%p: QP interface family query failed with status %d",
3276 (void *)dev, status);
3279 /* Clean up rxq in case we're reinitializing it. */
3280 DEBUG("%p: cleaning-up old rxq just in case", (void *)rxq);
3283 DEBUG("%p: rxq updated with %p", (void *)rxq, (void *)&tmpl);
3293 * DPDK callback to configure a RX queue.
3296 * Pointer to Ethernet device structure.
3300 * Number of descriptors to configure in queue.
3302 * NUMA socket on which memory must be allocated.
3304 * Thresholds parameters.
3306 * Memory pool for buffer allocations.
3309 * 0 on success, negative errno value on failure.
3312 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
3313 unsigned int socket, const struct rte_eth_rxconf *conf,
3314 struct rte_mempool *mp)
3316 struct priv *priv = dev->data->dev_private;
3317 struct rxq *rxq = (*priv->rxqs)[idx];
3321 DEBUG("%p: configuring queue %u for %u descriptors",
3322 (void *)dev, idx, desc);
3323 if (idx >= priv->rxqs_n) {
3324 ERROR("%p: queue index out of range (%u >= %u)",
3325 (void *)dev, idx, priv->rxqs_n);
3330 DEBUG("%p: reusing already allocated queue index %u (%p)",
3331 (void *)dev, idx, (void *)rxq);
3332 if (priv->started) {
3336 (*priv->rxqs)[idx] = NULL;
3339 rxq = rte_calloc_socket("RXQ", 1, sizeof(*rxq), 0, socket);
3341 ERROR("%p: unable to allocate queue index %u",
3347 ret = rxq_setup(dev, rxq, desc, socket, conf, mp);
3351 rxq->stats.idx = idx;
3352 DEBUG("%p: adding RX queue %p to list",
3353 (void *)dev, (void *)rxq);
3354 (*priv->rxqs)[idx] = rxq;
3355 /* Update receive callback. */
3357 dev->rx_pkt_burst = mlx4_rx_burst_sp;
3359 dev->rx_pkt_burst = mlx4_rx_burst;
3366 * DPDK callback to release a RX queue.
3369 * Generic RX queue pointer.
3372 mlx4_rx_queue_release(void *dpdk_rxq)
3374 struct rxq *rxq = (struct rxq *)dpdk_rxq;
3382 assert(rxq != &priv->rxq_parent);
3383 for (i = 0; (i != priv->rxqs_n); ++i)
3384 if ((*priv->rxqs)[i] == rxq) {
3385 DEBUG("%p: removing RX queue %p from list",
3386 (void *)priv->dev, (void *)rxq);
3387 (*priv->rxqs)[i] = NULL;
3396 * DPDK callback to start the device.
3398 * Simulate device start by attaching all configured flows.
3401 * Pointer to Ethernet device structure.
3404 * 0 on success, negative errno value on failure.
3407 mlx4_dev_start(struct rte_eth_dev *dev)
3409 struct priv *priv = dev->data->dev_private;
3415 if (priv->started) {
3419 DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
3422 rxq = &priv->rxq_parent;
3425 rxq = (*priv->rxqs)[0];
3428 /* Iterate only once when RSS is enabled. */
3432 /* Ignore nonexistent RX queues. */
3435 ret = rxq_mac_addrs_add(rxq);
3436 if (!ret && priv->promisc)
3437 ret = rxq_promiscuous_enable(rxq);
3438 if (!ret && priv->allmulti)
3439 ret = rxq_allmulticast_enable(rxq);
3442 WARN("%p: QP flow attachment failed: %s",
3443 (void *)dev, strerror(ret));
3446 rxq = (*priv->rxqs)[--i];
3448 rxq_allmulticast_disable(rxq);
3449 rxq_promiscuous_disable(rxq);
3450 rxq_mac_addrs_del(rxq);
3455 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
3461 * DPDK callback to stop the device.
3463 * Simulate device stop by detaching all configured flows.
3466 * Pointer to Ethernet device structure.
3469 mlx4_dev_stop(struct rte_eth_dev *dev)
3471 struct priv *priv = dev->data->dev_private;
3477 if (!priv->started) {
3481 DEBUG("%p: detaching flows from all RX queues", (void *)dev);
3484 rxq = &priv->rxq_parent;
3487 rxq = (*priv->rxqs)[0];
3490 /* Iterate only once when RSS is enabled. */
3492 /* Ignore nonexistent RX queues. */
3495 rxq_allmulticast_disable(rxq);
3496 rxq_promiscuous_disable(rxq);
3497 rxq_mac_addrs_del(rxq);
3498 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
3503 * Dummy DPDK callback for TX.
3505 * This function is used to temporarily replace the real callback during
3506 * unsafe control operations on the queue, or in case of error.
3509 * Generic pointer to TX queue structure.
3511 * Packets to transmit.
3513 * Number of packets in array.
3516 * Number of packets successfully transmitted (<= pkts_n).
3519 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
3528 * Dummy DPDK callback for RX.
3530 * This function is used to temporarily replace the real callback during
3531 * unsafe control operations on the queue, or in case of error.
3534 * Generic pointer to RX queue structure.
3536 * Array to store received packets.
3538 * Maximum number of packets in array.
3541 * Number of packets successfully received (<= pkts_n).
3544 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
3553 * DPDK callback to close the device.
3555 * Destroy all queues and objects, free memory.
3558 * Pointer to Ethernet device structure.
3561 mlx4_dev_close(struct rte_eth_dev *dev)
3563 struct priv *priv = dev->data->dev_private;
3568 DEBUG("%p: closing device \"%s\"",
3570 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
3571 /* Prevent crashes when queues are still in use. This is unfortunately
3572 * still required for DPDK 1.3 because some programs (such as testpmd)
3573 * never release them before closing the device. */
3574 dev->rx_pkt_burst = removed_rx_burst;
3575 dev->tx_pkt_burst = removed_tx_burst;
3576 if (priv->rxqs != NULL) {
3577 /* XXX race condition if mlx4_rx_burst() is still running. */
3579 for (i = 0; (i != priv->rxqs_n); ++i) {
3580 tmp = (*priv->rxqs)[i];
3583 (*priv->rxqs)[i] = NULL;
3590 if (priv->txqs != NULL) {
3591 /* XXX race condition if mlx4_tx_burst() is still running. */
3593 for (i = 0; (i != priv->txqs_n); ++i) {
3594 tmp = (*priv->txqs)[i];
3597 (*priv->txqs)[i] = NULL;
3605 rxq_cleanup(&priv->rxq_parent);
3606 if (priv->pd != NULL) {
3607 assert(priv->ctx != NULL);
3608 claim_zero(ibv_dealloc_pd(priv->pd));
3609 claim_zero(ibv_close_device(priv->ctx));
3611 assert(priv->ctx == NULL);
3613 memset(priv, 0, sizeof(*priv));
3617 * DPDK callback to get information about the device.
3620 * Pointer to Ethernet device structure.
3622 * Info structure output buffer.
3625 mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
3627 struct priv *priv = dev->data->dev_private;
3631 /* FIXME: we should ask the device for these values. */
3632 info->min_rx_bufsize = 32;
3633 info->max_rx_pktlen = 65536;
3635 * Since we need one CQ per QP, the limit is the minimum number
3636 * between the two values.
3638 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
3639 priv->device_attr.max_qp : priv->device_attr.max_cq);
3640 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
3643 info->max_rx_queues = max;
3644 info->max_tx_queues = max;
3645 info->max_mac_addrs = elemof(priv->mac);
3650 * DPDK callback to get device statistics.
3653 * Pointer to Ethernet device structure.
3655 * Stats structure output buffer.
3658 mlx4_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
3660 struct priv *priv = dev->data->dev_private;
3661 struct rte_eth_stats tmp = {0};
3666 /* Add software counters. */
3667 for (i = 0; (i != priv->rxqs_n); ++i) {
3668 struct rxq *rxq = (*priv->rxqs)[i];
3672 idx = rxq->stats.idx;
3673 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
3674 #ifdef MLX4_PMD_SOFT_COUNTERS
3675 tmp.q_ipackets[idx] += rxq->stats.ipackets;
3676 tmp.q_ibytes[idx] += rxq->stats.ibytes;
3678 tmp.q_errors[idx] += (rxq->stats.idropped +
3679 rxq->stats.rx_nombuf);
3681 #ifdef MLX4_PMD_SOFT_COUNTERS
3682 tmp.ipackets += rxq->stats.ipackets;
3683 tmp.ibytes += rxq->stats.ibytes;
3685 tmp.ierrors += rxq->stats.idropped;
3686 tmp.rx_nombuf += rxq->stats.rx_nombuf;
3688 for (i = 0; (i != priv->txqs_n); ++i) {
3689 struct txq *txq = (*priv->txqs)[i];
3693 idx = txq->stats.idx;
3694 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
3695 #ifdef MLX4_PMD_SOFT_COUNTERS
3696 tmp.q_opackets[idx] += txq->stats.opackets;
3697 tmp.q_obytes[idx] += txq->stats.obytes;
3699 tmp.q_errors[idx] += txq->stats.odropped;
3701 #ifdef MLX4_PMD_SOFT_COUNTERS
3702 tmp.opackets += txq->stats.opackets;
3703 tmp.obytes += txq->stats.obytes;
3705 tmp.oerrors += txq->stats.odropped;
3707 #ifndef MLX4_PMD_SOFT_COUNTERS
3708 /* FIXME: retrieve and add hardware counters. */
3715 * DPDK callback to clear device statistics.
3718 * Pointer to Ethernet device structure.
3721 mlx4_stats_reset(struct rte_eth_dev *dev)
3723 struct priv *priv = dev->data->dev_private;
3728 for (i = 0; (i != priv->rxqs_n); ++i) {
3729 if ((*priv->rxqs)[i] == NULL)
3731 idx = (*priv->rxqs)[i]->stats.idx;
3732 (*priv->rxqs)[i]->stats =
3733 (struct mlx4_rxq_stats){ .idx = idx };
3735 for (i = 0; (i != priv->txqs_n); ++i) {
3736 if ((*priv->txqs)[i] == NULL)
3738 idx = (*priv->rxqs)[i]->stats.idx;
3739 (*priv->txqs)[i]->stats =
3740 (struct mlx4_txq_stats){ .idx = idx };
3742 #ifndef MLX4_PMD_SOFT_COUNTERS
3743 /* FIXME: reset hardware counters. */
3749 * DPDK callback to remove a MAC address.
3752 * Pointer to Ethernet device structure.
3754 * MAC address index.
3757 mlx4_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
3759 struct priv *priv = dev->data->dev_private;
3762 DEBUG("%p: removing MAC address from index %" PRIu32,
3763 (void *)dev, index);
3764 if (index >= MLX4_MAX_MAC_ADDRESSES)
3766 /* Refuse to remove the broadcast address, this one is special. */
3767 if (!memcmp(priv->mac[index].addr_bytes, "\xff\xff\xff\xff\xff\xff",
3770 priv_mac_addr_del(priv, index);
3776 * DPDK callback to add a MAC address.
3779 * Pointer to Ethernet device structure.
3781 * MAC address to register.
3783 * MAC address index.
3785 * VMDq pool index to associate address with (ignored).
3788 mlx4_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
3789 uint32_t index, uint32_t vmdq)
3791 struct priv *priv = dev->data->dev_private;
3795 DEBUG("%p: adding MAC address at index %" PRIu32,
3796 (void *)dev, index);
3797 if (index >= MLX4_MAX_MAC_ADDRESSES)
3799 /* Refuse to add the broadcast address, this one is special. */
3800 if (!memcmp(mac_addr->addr_bytes, "\xff\xff\xff\xff\xff\xff",
3803 priv_mac_addr_add(priv, index,
3804 (const uint8_t (*)[ETHER_ADDR_LEN])
3805 mac_addr->addr_bytes);
3811 * DPDK callback to enable promiscuous mode.
3814 * Pointer to Ethernet device structure.
3817 mlx4_promiscuous_enable(struct rte_eth_dev *dev)
3819 struct priv *priv = dev->data->dev_private;
3824 if (priv->promisc) {
3828 /* If device isn't started, this is all we need to do. */
3832 ret = rxq_promiscuous_enable(&priv->rxq_parent);
3839 for (i = 0; (i != priv->rxqs_n); ++i) {
3840 if ((*priv->rxqs)[i] == NULL)
3842 ret = rxq_promiscuous_enable((*priv->rxqs)[i]);
3845 /* Failure, rollback. */
3847 if ((*priv->rxqs)[--i] != NULL)
3848 rxq_promiscuous_disable((*priv->rxqs)[i]);
3858 * DPDK callback to disable promiscuous mode.
3861 * Pointer to Ethernet device structure.
3864 mlx4_promiscuous_disable(struct rte_eth_dev *dev)
3866 struct priv *priv = dev->data->dev_private;
3870 if (!priv->promisc) {
3875 rxq_promiscuous_disable(&priv->rxq_parent);
3878 for (i = 0; (i != priv->rxqs_n); ++i)
3879 if ((*priv->rxqs)[i] != NULL)
3880 rxq_promiscuous_disable((*priv->rxqs)[i]);
3887 * DPDK callback to enable allmulti mode.
3890 * Pointer to Ethernet device structure.
3893 mlx4_allmulticast_enable(struct rte_eth_dev *dev)
3895 struct priv *priv = dev->data->dev_private;
3900 if (priv->allmulti) {
3904 /* If device isn't started, this is all we need to do. */
3908 ret = rxq_allmulticast_enable(&priv->rxq_parent);
3915 for (i = 0; (i != priv->rxqs_n); ++i) {
3916 if ((*priv->rxqs)[i] == NULL)
3918 ret = rxq_allmulticast_enable((*priv->rxqs)[i]);
3921 /* Failure, rollback. */
3923 if ((*priv->rxqs)[--i] != NULL)
3924 rxq_allmulticast_disable((*priv->rxqs)[i]);
3934 * DPDK callback to disable allmulti mode.
3937 * Pointer to Ethernet device structure.
3940 mlx4_allmulticast_disable(struct rte_eth_dev *dev)
3942 struct priv *priv = dev->data->dev_private;
3946 if (!priv->allmulti) {
3951 rxq_allmulticast_disable(&priv->rxq_parent);
3954 for (i = 0; (i != priv->rxqs_n); ++i)
3955 if ((*priv->rxqs)[i] != NULL)
3956 rxq_allmulticast_disable((*priv->rxqs)[i]);
3963 * DPDK callback to retrieve physical link information (unlocked version).
3966 * Pointer to Ethernet device structure.
3967 * @param wait_to_complete
3968 * Wait for request completion (ignored).
3971 mlx4_link_update_unlocked(struct rte_eth_dev *dev, int wait_to_complete)
3973 struct priv *priv = dev->data->dev_private;
3974 struct ethtool_cmd edata = {
3978 struct rte_eth_link dev_link;
3981 (void)wait_to_complete;
3982 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
3983 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
3986 memset(&dev_link, 0, sizeof(dev_link));
3987 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
3988 (ifr.ifr_flags & IFF_RUNNING));
3989 ifr.ifr_data = &edata;
3990 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
3991 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
3995 link_speed = ethtool_cmd_speed(&edata);
3996 if (link_speed == -1)
3997 dev_link.link_speed = 0;
3999 dev_link.link_speed = link_speed;
4000 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
4001 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
4002 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
4003 /* Link status changed. */
4004 dev->data->dev_link = dev_link;
4007 /* Link status is still the same. */
4012 * DPDK callback to retrieve physical link information.
4015 * Pointer to Ethernet device structure.
4016 * @param wait_to_complete
4017 * Wait for request completion (ignored).
4020 mlx4_link_update(struct rte_eth_dev *dev, int wait_to_complete)
4022 struct priv *priv = dev->data->dev_private;
4026 ret = mlx4_link_update_unlocked(dev, wait_to_complete);
4032 * DPDK callback to change the MTU.
4034 * Setting the MTU affects hardware MRU (packets larger than the MTU cannot be
4035 * received). Use this as a hint to enable/disable scattered packets support
4036 * and improve performance when not needed.
4037 * Since failure is not an option, reconfiguring queues on the fly is not
4041 * Pointer to Ethernet device structure.
4046 * 0 on success, negative errno value on failure.
4049 mlx4_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
4051 struct priv *priv = dev->data->dev_private;
4054 uint16_t (*rx_func)(void *, struct rte_mbuf **, uint16_t) =
4058 /* Set kernel interface MTU first. */
4059 if (priv_set_mtu(priv, mtu)) {
4061 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
4065 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
4067 /* Temporarily replace RX handler with a fake one, assuming it has not
4068 * been copied elsewhere. */
4069 dev->rx_pkt_burst = removed_rx_burst;
4070 /* Make sure everyone has left mlx4_rx_burst() and uses
4071 * removed_rx_burst() instead. */
4074 /* Reconfigure each RX queue. */
4075 for (i = 0; (i != priv->rxqs_n); ++i) {
4076 struct rxq *rxq = (*priv->rxqs)[i];
4077 unsigned int max_frame_len;
4082 /* Calculate new maximum frame length according to MTU and
4083 * toggle scattered support (sp) if necessary. */
4084 max_frame_len = (priv->mtu + ETHER_HDR_LEN +
4085 (ETHER_MAX_VLAN_FRAME_LEN - ETHER_MAX_LEN));
4086 sp = (max_frame_len > (rxq->mb_len - RTE_PKTMBUF_HEADROOM));
4087 /* Provide new values to rxq_setup(). */
4088 dev->data->dev_conf.rxmode.jumbo_frame = sp;
4089 dev->data->dev_conf.rxmode.max_rx_pkt_len = max_frame_len;
4090 ret = rxq_rehash(dev, rxq);
4092 /* Force SP RX if that queue requires it and abort. */
4094 rx_func = mlx4_rx_burst_sp;
4097 /* Reenable non-RSS queue attributes. No need to check
4098 * for errors at this stage. */
4100 rxq_mac_addrs_add(rxq);
4102 rxq_promiscuous_enable(rxq);
4104 rxq_allmulticast_enable(rxq);
4106 /* Scattered burst function takes priority. */
4108 rx_func = mlx4_rx_burst_sp;
4110 /* Burst functions can now be called again. */
4112 dev->rx_pkt_burst = rx_func;
4120 * DPDK callback to get flow control status.
4123 * Pointer to Ethernet device structure.
4124 * @param[out] fc_conf
4125 * Flow control output buffer.
4128 * 0 on success, negative errno value on failure.
4131 mlx4_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4133 struct priv *priv = dev->data->dev_private;
4135 struct ethtool_pauseparam ethpause = {
4136 .cmd = ETHTOOL_GPAUSEPARAM
4140 ifr.ifr_data = ðpause;
4142 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4144 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
4150 fc_conf->autoneg = ethpause.autoneg;
4151 if (ethpause.rx_pause && ethpause.tx_pause)
4152 fc_conf->mode = RTE_FC_FULL;
4153 else if (ethpause.rx_pause)
4154 fc_conf->mode = RTE_FC_RX_PAUSE;
4155 else if (ethpause.tx_pause)
4156 fc_conf->mode = RTE_FC_TX_PAUSE;
4158 fc_conf->mode = RTE_FC_NONE;
4168 * DPDK callback to modify flow control parameters.
4171 * Pointer to Ethernet device structure.
4172 * @param[in] fc_conf
4173 * Flow control parameters.
4176 * 0 on success, negative errno value on failure.
4179 mlx4_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4181 struct priv *priv = dev->data->dev_private;
4183 struct ethtool_pauseparam ethpause = {
4184 .cmd = ETHTOOL_SPAUSEPARAM
4188 ifr.ifr_data = ðpause;
4189 ethpause.autoneg = fc_conf->autoneg;
4190 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4191 (fc_conf->mode & RTE_FC_RX_PAUSE))
4192 ethpause.rx_pause = 1;
4194 ethpause.rx_pause = 0;
4196 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4197 (fc_conf->mode & RTE_FC_TX_PAUSE))
4198 ethpause.tx_pause = 1;
4200 ethpause.tx_pause = 0;
4203 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4205 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
4219 * Configure a VLAN filter.
4222 * Pointer to Ethernet device structure.
4224 * VLAN ID to filter.
4229 * 0 on success, errno value on failure.
4232 vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
4234 struct priv *priv = dev->data->dev_private;
4236 unsigned int j = -1;
4238 DEBUG("%p: %s VLAN filter ID %" PRIu16,
4239 (void *)dev, (on ? "enable" : "disable"), vlan_id);
4240 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
4241 if (!priv->vlan_filter[i].enabled) {
4242 /* Unused index, remember it. */
4246 if (priv->vlan_filter[i].id != vlan_id)
4248 /* This VLAN ID is already known, use its index. */
4252 /* Check if there's room for another VLAN filter. */
4253 if (j == (unsigned int)-1)
4256 * VLAN filters apply to all configured MAC addresses, flow
4257 * specifications must be reconfigured accordingly.
4259 priv->vlan_filter[j].id = vlan_id;
4260 if ((on) && (!priv->vlan_filter[j].enabled)) {
4262 * Filter is disabled, enable it.
4263 * Rehashing flows in all RX queues is necessary.
4266 rxq_mac_addrs_del(&priv->rxq_parent);
4268 for (i = 0; (i != priv->rxqs_n); ++i)
4269 if ((*priv->rxqs)[i] != NULL)
4270 rxq_mac_addrs_del((*priv->rxqs)[i]);
4271 priv->vlan_filter[j].enabled = 1;
4272 if (priv->started) {
4274 rxq_mac_addrs_add(&priv->rxq_parent);
4276 for (i = 0; (i != priv->rxqs_n); ++i) {
4277 if ((*priv->rxqs)[i] == NULL)
4279 rxq_mac_addrs_add((*priv->rxqs)[i]);
4282 } else if ((!on) && (priv->vlan_filter[j].enabled)) {
4284 * Filter is enabled, disable it.
4285 * Rehashing flows in all RX queues is necessary.
4288 rxq_mac_addrs_del(&priv->rxq_parent);
4290 for (i = 0; (i != priv->rxqs_n); ++i)
4291 if ((*priv->rxqs)[i] != NULL)
4292 rxq_mac_addrs_del((*priv->rxqs)[i]);
4293 priv->vlan_filter[j].enabled = 0;
4294 if (priv->started) {
4296 rxq_mac_addrs_add(&priv->rxq_parent);
4298 for (i = 0; (i != priv->rxqs_n); ++i) {
4299 if ((*priv->rxqs)[i] == NULL)
4301 rxq_mac_addrs_add((*priv->rxqs)[i]);
4309 * DPDK callback to configure a VLAN filter.
4312 * Pointer to Ethernet device structure.
4314 * VLAN ID to filter.
4319 * 0 on success, negative errno value on failure.
4322 mlx4_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
4324 struct priv *priv = dev->data->dev_private;
4328 ret = vlan_filter_set(dev, vlan_id, on);
4334 static const struct eth_dev_ops mlx4_dev_ops = {
4335 .dev_configure = mlx4_dev_configure,
4336 .dev_start = mlx4_dev_start,
4337 .dev_stop = mlx4_dev_stop,
4338 .dev_close = mlx4_dev_close,
4339 .promiscuous_enable = mlx4_promiscuous_enable,
4340 .promiscuous_disable = mlx4_promiscuous_disable,
4341 .allmulticast_enable = mlx4_allmulticast_enable,
4342 .allmulticast_disable = mlx4_allmulticast_disable,
4343 .link_update = mlx4_link_update,
4344 .stats_get = mlx4_stats_get,
4345 .stats_reset = mlx4_stats_reset,
4346 .queue_stats_mapping_set = NULL,
4347 .dev_infos_get = mlx4_dev_infos_get,
4348 .vlan_filter_set = mlx4_vlan_filter_set,
4349 .vlan_tpid_set = NULL,
4350 .vlan_strip_queue_set = NULL,
4351 .vlan_offload_set = NULL,
4352 .rx_queue_setup = mlx4_rx_queue_setup,
4353 .tx_queue_setup = mlx4_tx_queue_setup,
4354 .rx_queue_release = mlx4_rx_queue_release,
4355 .tx_queue_release = mlx4_tx_queue_release,
4357 .dev_led_off = NULL,
4358 .flow_ctrl_get = mlx4_dev_get_flow_ctrl,
4359 .flow_ctrl_set = mlx4_dev_set_flow_ctrl,
4360 .priority_flow_ctrl_set = NULL,
4361 .mac_addr_remove = mlx4_mac_addr_remove,
4362 .mac_addr_add = mlx4_mac_addr_add,
4363 .mtu_set = mlx4_dev_set_mtu,
4364 .fdir_add_signature_filter = NULL,
4365 .fdir_update_signature_filter = NULL,
4366 .fdir_remove_signature_filter = NULL,
4367 .fdir_add_perfect_filter = NULL,
4368 .fdir_update_perfect_filter = NULL,
4369 .fdir_remove_perfect_filter = NULL,
4370 .fdir_set_masks = NULL
4374 * Get PCI information from struct ibv_device.
4377 * Pointer to Ethernet device structure.
4378 * @param[out] pci_addr
4379 * PCI bus address output buffer.
4382 * 0 on success, -1 on failure and errno is set.
4385 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
4386 struct rte_pci_addr *pci_addr)
4390 MKSTR(path, "%s/device/uevent", device->ibdev_path);
4392 file = fopen(path, "rb");
4395 while (fgets(line, sizeof(line), file) == line) {
4396 size_t len = strlen(line);
4399 /* Truncate long lines. */
4400 if (len == (sizeof(line) - 1))
4401 while (line[(len - 1)] != '\n') {
4405 line[(len - 1)] = ret;
4407 /* Extract information. */
4410 "%" SCNx16 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
4414 &pci_addr->function) == 4) {
4424 * Get MAC address by querying netdevice.
4427 * struct priv for the requested device.
4429 * MAC address output buffer.
4432 * 0 on success, -1 on failure and errno is set.
4435 priv_get_mac(struct priv *priv, uint8_t (*mac)[ETHER_ADDR_LEN])
4437 struct ifreq request;
4439 if (priv_ifreq(priv, SIOCGIFHWADDR, &request))
4441 memcpy(mac, request.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
4445 /* Support up to 32 adapters. */
4447 struct rte_pci_addr pci_addr; /* associated PCI address */
4448 uint32_t ports; /* physical ports bitfield. */
4452 * Get device index in mlx4_dev[] from PCI bus address.
4454 * @param[in] pci_addr
4455 * PCI bus address to look for.
4458 * mlx4_dev[] index on success, -1 on failure.
4461 mlx4_dev_idx(struct rte_pci_addr *pci_addr)
4466 assert(pci_addr != NULL);
4467 for (i = 0; (i != elemof(mlx4_dev)); ++i) {
4468 if ((mlx4_dev[i].pci_addr.domain == pci_addr->domain) &&
4469 (mlx4_dev[i].pci_addr.bus == pci_addr->bus) &&
4470 (mlx4_dev[i].pci_addr.devid == pci_addr->devid) &&
4471 (mlx4_dev[i].pci_addr.function == pci_addr->function))
4473 if ((mlx4_dev[i].ports == 0) && (ret == -1))
4480 * Retrieve integer value from environment variable.
4483 * Environment variable name.
4486 * Integer value, 0 if the variable is not set.
4489 mlx4_getenv_int(const char *name)
4491 const char *val = getenv(name);
4498 static struct eth_driver mlx4_driver;
4501 * DPDK callback to register a PCI device.
4503 * This function creates an Ethernet device for each port of a given
4506 * @param[in] pci_drv
4507 * PCI driver structure (mlx4_driver).
4508 * @param[in] pci_dev
4509 * PCI device information.
4512 * 0 on success, negative errno value on failure.
4515 mlx4_pci_devinit(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
4517 struct ibv_device **list;
4518 struct ibv_device *ibv_dev;
4520 struct ibv_context *attr_ctx = NULL;
4521 struct ibv_device_attr device_attr;
4527 assert(pci_drv == &mlx4_driver.pci_drv);
4528 /* Get mlx4_dev[] index. */
4529 idx = mlx4_dev_idx(&pci_dev->addr);
4531 ERROR("this driver cannot support any more adapters");
4534 DEBUG("using driver device index %d", idx);
4536 /* Save PCI address. */
4537 mlx4_dev[idx].pci_addr = pci_dev->addr;
4538 list = ibv_get_device_list(&i);
4541 if (errno == ENOSYS) {
4542 WARN("cannot list devices, is ib_uverbs loaded?");
4549 * For each listed device, check related sysfs entry against
4550 * the provided PCI ID.
4553 struct rte_pci_addr pci_addr;
4556 DEBUG("checking device \"%s\"", list[i]->name);
4557 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
4559 if ((pci_dev->addr.domain != pci_addr.domain) ||
4560 (pci_dev->addr.bus != pci_addr.bus) ||
4561 (pci_dev->addr.devid != pci_addr.devid) ||
4562 (pci_dev->addr.function != pci_addr.function))
4564 vf = (pci_dev->id.device_id ==
4565 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
4566 INFO("PCI information matches, using device \"%s\" (VF: %s)",
4567 list[i]->name, (vf ? "true" : "false"));
4568 attr_ctx = ibv_open_device(list[i]);
4572 if (attr_ctx == NULL) {
4573 ibv_free_device_list(list);
4576 WARN("cannot access device, is mlx4_ib loaded?");
4579 WARN("cannot use device, are drivers up to date?");
4587 DEBUG("device opened");
4588 if (ibv_query_device(attr_ctx, &device_attr))
4590 INFO("%u port(s) detected", device_attr.phys_port_cnt);
4592 for (i = 0; i < device_attr.phys_port_cnt; i++) {
4593 uint32_t port = i + 1; /* ports are indexed from one */
4594 uint32_t test = (1 << i);
4595 struct ibv_context *ctx = NULL;
4596 struct ibv_port_attr port_attr;
4597 struct ibv_pd *pd = NULL;
4598 struct priv *priv = NULL;
4599 struct rte_eth_dev *eth_dev;
4600 #ifdef HAVE_EXP_QUERY_DEVICE
4601 struct ibv_exp_device_attr exp_device_attr;
4602 #endif /* HAVE_EXP_QUERY_DEVICE */
4603 struct ether_addr mac;
4605 #ifdef HAVE_EXP_QUERY_DEVICE
4606 exp_device_attr.comp_mask = IBV_EXP_DEVICE_ATTR_EXP_CAP_FLAGS;
4608 exp_device_attr.comp_mask |= IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ;
4609 #endif /* RSS_SUPPORT */
4610 #endif /* HAVE_EXP_QUERY_DEVICE */
4612 DEBUG("using port %u (%08" PRIx32 ")", port, test);
4614 ctx = ibv_open_device(ibv_dev);
4618 /* Check port status. */
4619 err = ibv_query_port(ctx, port, &port_attr);
4621 ERROR("port query failed: %s", strerror(err));
4624 if (port_attr.state != IBV_PORT_ACTIVE)
4625 WARN("bad state for port %d: \"%s\" (%d)",
4626 port, ibv_port_state_str(port_attr.state),
4629 /* Allocate protection domain. */
4630 pd = ibv_alloc_pd(ctx);
4632 ERROR("PD allocation failure");
4637 mlx4_dev[idx].ports |= test;
4639 /* from rte_ethdev.c */
4640 priv = rte_zmalloc("ethdev private structure",
4642 RTE_CACHE_LINE_SIZE);
4644 ERROR("priv allocation failure");
4650 priv->device_attr = device_attr;
4653 priv->mtu = ETHER_MTU;
4654 #ifdef HAVE_EXP_QUERY_DEVICE
4655 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
4656 ERROR("ibv_exp_query_device() failed");
4660 if ((exp_device_attr.exp_device_cap_flags &
4661 IBV_EXP_DEVICE_QPG) &&
4662 (exp_device_attr.exp_device_cap_flags &
4663 IBV_EXP_DEVICE_UD_RSS) &&
4664 (exp_device_attr.comp_mask &
4665 IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ) &&
4666 (exp_device_attr.max_rss_tbl_sz > 0)) {
4669 priv->max_rss_tbl_sz = exp_device_attr.max_rss_tbl_sz;
4673 priv->max_rss_tbl_sz = 0;
4675 priv->hw_tss = !!(exp_device_attr.exp_device_cap_flags &
4676 IBV_EXP_DEVICE_UD_TSS);
4677 DEBUG("device flags: %s%s%s",
4678 (priv->hw_qpg ? "IBV_DEVICE_QPG " : ""),
4679 (priv->hw_tss ? "IBV_DEVICE_TSS " : ""),
4680 (priv->hw_rss ? "IBV_DEVICE_RSS " : ""));
4682 DEBUG("maximum RSS indirection table size: %u",
4683 exp_device_attr.max_rss_tbl_sz);
4684 #endif /* RSS_SUPPORT */
4687 priv->inl_recv_size = mlx4_getenv_int("MLX4_INLINE_RECV_SIZE");
4689 if (priv->inl_recv_size) {
4690 exp_device_attr.comp_mask =
4691 IBV_EXP_DEVICE_ATTR_INLINE_RECV_SZ;
4692 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
4693 INFO("Couldn't query device for inline-receive"
4695 priv->inl_recv_size = 0;
4697 if ((unsigned)exp_device_attr.inline_recv_sz <
4698 priv->inl_recv_size) {
4699 INFO("Max inline-receive (%d) <"
4700 " requested inline-receive (%u)",
4701 exp_device_attr.inline_recv_sz,
4702 priv->inl_recv_size);
4703 priv->inl_recv_size =
4704 exp_device_attr.inline_recv_sz;
4707 INFO("Set inline receive size to %u",
4708 priv->inl_recv_size);
4710 #endif /* INLINE_RECV */
4711 #endif /* HAVE_EXP_QUERY_DEVICE */
4713 (void)mlx4_getenv_int;
4715 /* Configure the first MAC address by default. */
4716 if (priv_get_mac(priv, &mac.addr_bytes)) {
4717 ERROR("cannot get MAC address, is mlx4_en loaded?"
4718 " (errno: %s)", strerror(errno));
4721 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
4723 mac.addr_bytes[0], mac.addr_bytes[1],
4724 mac.addr_bytes[2], mac.addr_bytes[3],
4725 mac.addr_bytes[4], mac.addr_bytes[5]);
4726 /* Register MAC and broadcast addresses. */
4727 claim_zero(priv_mac_addr_add(priv, 0,
4728 (const uint8_t (*)[ETHER_ADDR_LEN])
4730 claim_zero(priv_mac_addr_add(priv, 1,
4731 &(const uint8_t [ETHER_ADDR_LEN])
4732 { "\xff\xff\xff\xff\xff\xff" }));
4735 char ifname[IF_NAMESIZE];
4737 if (priv_get_ifname(priv, &ifname) == 0)
4738 DEBUG("port %u ifname is \"%s\"",
4739 priv->port, ifname);
4741 DEBUG("port %u ifname is unknown", priv->port);
4744 /* Get actual MTU if possible. */
4745 priv_get_mtu(priv, &priv->mtu);
4746 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
4748 /* from rte_ethdev.c */
4750 char name[RTE_ETH_NAME_MAX_LEN];
4752 snprintf(name, sizeof(name), "%s port %u",
4753 ibv_get_device_name(ibv_dev), port);
4754 eth_dev = rte_eth_dev_allocate(name, RTE_ETH_DEV_PCI);
4756 if (eth_dev == NULL) {
4757 ERROR("can not allocate rte ethdev");
4762 eth_dev->data->dev_private = priv;
4763 eth_dev->pci_dev = pci_dev;
4764 eth_dev->driver = &mlx4_driver;
4765 eth_dev->data->rx_mbuf_alloc_failed = 0;
4766 eth_dev->data->mtu = ETHER_MTU;
4768 priv->dev = eth_dev;
4769 eth_dev->dev_ops = &mlx4_dev_ops;
4770 eth_dev->data->mac_addrs = priv->mac;
4772 /* Bring Ethernet device up. */
4773 DEBUG("forcing Ethernet interface up");
4774 priv_set_flags(priv, ~IFF_UP, IFF_UP);
4780 claim_zero(ibv_dealloc_pd(pd));
4782 claim_zero(ibv_close_device(ctx));
4787 * XXX if something went wrong in the loop above, there is a resource
4788 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
4789 * long as the dpdk does not provide a way to deallocate a ethdev and a
4790 * way to enumerate the registered ethdevs to free the previous ones.
4793 /* no port found, complain */
4794 if (!mlx4_dev[idx].ports) {
4801 claim_zero(ibv_close_device(attr_ctx));
4803 ibv_free_device_list(list);
4808 static const struct rte_pci_id mlx4_pci_id_map[] = {
4810 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4811 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3,
4812 .subsystem_vendor_id = PCI_ANY_ID,
4813 .subsystem_device_id = PCI_ANY_ID
4816 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4817 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO,
4818 .subsystem_vendor_id = PCI_ANY_ID,
4819 .subsystem_device_id = PCI_ANY_ID
4822 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4823 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3VF,
4824 .subsystem_vendor_id = PCI_ANY_ID,
4825 .subsystem_device_id = PCI_ANY_ID
4832 static struct eth_driver mlx4_driver = {
4834 .name = MLX4_DRIVER_NAME,
4835 .id_table = mlx4_pci_id_map,
4836 .devinit = mlx4_pci_devinit,
4838 .dev_private_size = sizeof(struct priv)
4842 * Driver initialization routine.
4845 rte_mlx4_pmd_init(const char *name, const char *args)
4850 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
4851 * huge pages. Calling ibv_fork_init() during init allows
4852 * applications to use fork() safely for purposes other than
4853 * using this PMD, which is not supported in forked processes.
4855 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
4857 rte_eal_pci_register(&mlx4_driver.pci_drv);
4861 static struct rte_driver rte_mlx4_driver = {
4863 .name = MLX4_DRIVER_NAME,
4864 .init = rte_mlx4_pmd_init,
4867 PMD_REGISTER_DRIVER(rte_mlx4_driver)