4 * Copyright 2012-2015 6WIND S.A.
5 * Copyright 2012 Mellanox.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of 6WIND S.A. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * - RSS hash key and options cannot be modified.
37 * - Hardware counters aren't implemented.
51 #include <arpa/inet.h>
54 #include <sys/ioctl.h>
55 #include <sys/socket.h>
56 #include <netinet/in.h>
58 #include <linux/ethtool.h>
59 #include <linux/sockios.h>
62 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
64 #pragma GCC diagnostic ignored "-pedantic"
66 #include <infiniband/verbs.h>
68 #pragma GCC diagnostic error "-pedantic"
71 /* DPDK headers don't like -pedantic. */
73 #pragma GCC diagnostic ignored "-pedantic"
75 #include <rte_config.h>
76 #include <rte_ether.h>
77 #include <rte_ethdev.h>
80 #include <rte_errno.h>
81 #include <rte_mempool.h>
82 #include <rte_prefetch.h>
83 #include <rte_malloc.h>
84 #include <rte_spinlock.h>
85 #include <rte_atomic.h>
86 #include <rte_version.h>
89 #pragma GCC diagnostic error "-pedantic"
92 /* Generated configuration header. */
93 #include "mlx4_autoconf.h"
98 /* Runtime logging through RTE_LOG() is enabled when not in debugging mode.
99 * Intermediate LOG_*() macros add the required end-of-line characters. */
101 #define INFO(...) DEBUG(__VA_ARGS__)
102 #define WARN(...) DEBUG(__VA_ARGS__)
103 #define ERROR(...) DEBUG(__VA_ARGS__)
105 #define LOG__(level, m, ...) \
106 RTE_LOG(level, PMD, MLX4_DRIVER_NAME ": " m "%c", __VA_ARGS__)
107 #define LOG_(level, ...) LOG__(level, __VA_ARGS__, '\n')
108 #define INFO(...) LOG_(INFO, __VA_ARGS__)
109 #define WARN(...) LOG_(WARNING, __VA_ARGS__)
110 #define ERROR(...) LOG_(ERR, __VA_ARGS__)
113 /* Convenience macros for accessing mbuf fields. */
114 #define NEXT(m) ((m)->next)
115 #define DATA_LEN(m) ((m)->data_len)
116 #define PKT_LEN(m) ((m)->pkt_len)
117 #define DATA_OFF(m) ((m)->data_off)
118 #define SET_DATA_OFF(m, o) ((m)->data_off = (o))
119 #define NB_SEGS(m) ((m)->nb_segs)
120 #define PORT(m) ((m)->port)
122 /* Work Request ID data type (64 bit). */
131 #define WR_ID(o) (((wr_id_t *)&(o))->data)
133 /* Compile-time check. */
134 static inline void wr_id_t_check(void)
136 wr_id_t check[1 + (2 * -!(sizeof(wr_id_t) == sizeof(uint64_t)))];
142 /* Transpose flags. Useful to convert IBV to DPDK flags. */
143 #define TRANSPOSE(val, from, to) \
144 (((from) >= (to)) ? \
145 (((val) & (from)) / ((from) / (to))) : \
146 (((val) & (from)) * ((to) / (from))))
148 struct mlx4_rxq_stats {
149 unsigned int idx; /**< Mapping index. */
150 #ifdef MLX4_PMD_SOFT_COUNTERS
151 uint64_t ipackets; /**< Total of successfully received packets. */
152 uint64_t ibytes; /**< Total of successfully received bytes. */
154 uint64_t idropped; /**< Total of packets dropped when RX ring full. */
155 uint64_t rx_nombuf; /**< Total of RX mbuf allocation failures. */
158 struct mlx4_txq_stats {
159 unsigned int idx; /**< Mapping index. */
160 #ifdef MLX4_PMD_SOFT_COUNTERS
161 uint64_t opackets; /**< Total of successfully sent packets. */
162 uint64_t obytes; /**< Total of successfully sent bytes. */
164 uint64_t odropped; /**< Total of packets not sent when TX ring full. */
167 /* RX element (scattered packets). */
169 struct ibv_recv_wr wr; /* Work Request. */
170 struct ibv_sge sges[MLX4_PMD_SGE_WR_N]; /* Scatter/Gather Elements. */
171 struct rte_mbuf *bufs[MLX4_PMD_SGE_WR_N]; /* SGEs buffers. */
176 struct ibv_recv_wr wr; /* Work Request. */
177 struct ibv_sge sge; /* Scatter/Gather Element. */
178 /* mbuf pointer is derived from WR_ID(wr.wr_id).offset. */
181 /* RX queue descriptor. */
183 struct priv *priv; /* Back pointer to private data. */
184 struct rte_mempool *mp; /* Memory Pool for allocations. */
185 struct ibv_mr *mr; /* Memory Region (for mp). */
186 struct ibv_cq *cq; /* Completion Queue. */
187 struct ibv_qp *qp; /* Queue Pair. */
188 struct ibv_exp_qp_burst_family *if_qp; /* QP burst interface. */
189 struct ibv_exp_cq_family *if_cq; /* CQ interface. */
191 * Each VLAN ID requires a separate flow steering rule.
193 BITFIELD_DECLARE(mac_configured, uint32_t, MLX4_MAX_MAC_ADDRESSES);
194 struct ibv_flow *mac_flow[MLX4_MAX_MAC_ADDRESSES][MLX4_MAX_VLAN_IDS];
195 struct ibv_flow *promisc_flow; /* Promiscuous flow. */
196 struct ibv_flow *allmulti_flow; /* Multicast flow. */
197 unsigned int port_id; /* Port ID for incoming packets. */
198 unsigned int elts_n; /* (*elts)[] length. */
199 unsigned int elts_head; /* Current index in (*elts)[]. */
201 struct rxq_elt_sp (*sp)[]; /* Scattered RX elements. */
202 struct rxq_elt (*no_sp)[]; /* RX elements. */
204 unsigned int sp:1; /* Use scattered RX elements. */
205 unsigned int csum:1; /* Enable checksum offloading. */
206 unsigned int csum_l2tun:1; /* Same for L2 tunnels. */
207 uint32_t mb_len; /* Length of a mp-issued mbuf. */
208 struct mlx4_rxq_stats stats; /* RX queue counters. */
209 unsigned int socket; /* CPU socket ID for allocations. */
210 struct ibv_exp_res_domain *rd; /* Resource Domain. */
215 struct rte_mbuf *buf;
218 /* Linear buffer type. It is used when transmitting buffers with too many
219 * segments that do not fit the hardware queue (see max_send_sge).
220 * Extra segments are copied (linearized) in such buffers, replacing the
221 * last SGE during TX.
222 * The size is arbitrary but large enough to hold a jumbo frame with
223 * 8 segments considering mbuf.buf_len is about 2048 bytes. */
224 typedef uint8_t linear_t[16384];
226 /* TX queue descriptor. */
228 struct priv *priv; /* Back pointer to private data. */
230 struct rte_mempool *mp; /* Cached Memory Pool. */
231 struct ibv_mr *mr; /* Memory Region (for mp). */
232 uint32_t lkey; /* mr->lkey */
233 } mp2mr[MLX4_PMD_TX_MP_CACHE]; /* MP to MR translation table. */
234 struct ibv_cq *cq; /* Completion Queue. */
235 struct ibv_qp *qp; /* Queue Pair. */
236 struct ibv_exp_qp_burst_family *if_qp; /* QP burst interface. */
237 struct ibv_exp_cq_family *if_cq; /* CQ interface. */
238 #if MLX4_PMD_MAX_INLINE > 0
239 uint32_t max_inline; /* Max inline send size <= MLX4_PMD_MAX_INLINE. */
241 unsigned int elts_n; /* (*elts)[] length. */
242 struct txq_elt (*elts)[]; /* TX elements. */
243 unsigned int elts_head; /* Current index in (*elts)[]. */
244 unsigned int elts_tail; /* First element awaiting completion. */
245 unsigned int elts_comp; /* Number of completion requests. */
246 unsigned int elts_comp_cd; /* Countdown for next completion request. */
247 unsigned int elts_comp_cd_init; /* Initial value for countdown. */
248 struct mlx4_txq_stats stats; /* TX queue counters. */
249 linear_t (*elts_linear)[]; /* Linearized buffers. */
250 struct ibv_mr *mr_linear; /* Memory Region for linearized buffers. */
251 unsigned int socket; /* CPU socket ID for allocations. */
252 struct ibv_exp_res_domain *rd; /* Resource Domain. */
256 struct rte_eth_dev *dev; /* Ethernet device. */
257 struct ibv_context *ctx; /* Verbs context. */
258 struct ibv_device_attr device_attr; /* Device properties. */
259 struct ibv_pd *pd; /* Protection Domain. */
261 * MAC addresses array and configuration bit-field.
262 * An extra entry that cannot be modified by the DPDK is reserved
263 * for broadcast frames (destination MAC address ff:ff:ff:ff:ff:ff).
265 struct ether_addr mac[MLX4_MAX_MAC_ADDRESSES];
266 BITFIELD_DECLARE(mac_configured, uint32_t, MLX4_MAX_MAC_ADDRESSES);
269 unsigned int enabled:1; /* If enabled. */
270 unsigned int id:12; /* VLAN ID (0-4095). */
271 } vlan_filter[MLX4_MAX_VLAN_IDS]; /* VLAN filters table. */
272 /* Device properties. */
273 uint16_t mtu; /* Configured MTU. */
274 uint8_t port; /* Physical port number. */
275 unsigned int started:1; /* Device started, flows enabled. */
276 unsigned int promisc:1; /* Device in promiscuous mode. */
277 unsigned int allmulti:1; /* Device receives all multicast packets. */
278 unsigned int hw_qpg:1; /* QP groups are supported. */
279 unsigned int hw_tss:1; /* TSS is supported. */
280 unsigned int hw_rss:1; /* RSS is supported. */
281 unsigned int hw_csum:1; /* Checksum offload is supported. */
282 unsigned int hw_csum_l2tun:1; /* Same for L2 tunnels. */
283 unsigned int rss:1; /* RSS is enabled. */
284 unsigned int vf:1; /* This is a VF device. */
286 unsigned int inl_recv_size; /* Inline recv size */
288 unsigned int max_rss_tbl_sz; /* Maximum number of RSS queues. */
290 struct rxq rxq_parent; /* Parent queue when RSS is enabled. */
291 unsigned int rxqs_n; /* RX queues array size. */
292 unsigned int txqs_n; /* TX queues array size. */
293 struct rxq *(*rxqs)[]; /* RX queues. */
294 struct txq *(*txqs)[]; /* TX queues. */
295 rte_spinlock_t lock; /* Lock for control functions. */
299 * Lock private structure to protect it from concurrent access in the
303 * Pointer to private structure.
306 priv_lock(struct priv *priv)
308 rte_spinlock_lock(&priv->lock);
312 * Unlock private structure.
315 * Pointer to private structure.
318 priv_unlock(struct priv *priv)
320 rte_spinlock_unlock(&priv->lock);
323 /* Allocate a buffer on the stack and fill it with a printf format string. */
324 #define MKSTR(name, ...) \
325 char name[snprintf(NULL, 0, __VA_ARGS__) + 1]; \
327 snprintf(name, sizeof(name), __VA_ARGS__)
330 * Get interface name from private structure.
333 * Pointer to private structure.
335 * Interface name output buffer.
338 * 0 on success, -1 on failure and errno is set.
341 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
345 unsigned int dev_type = 0;
346 unsigned int dev_port_prev = ~0u;
347 char match[IF_NAMESIZE] = "";
350 MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
356 while ((dent = readdir(dir)) != NULL) {
357 char *name = dent->d_name;
359 unsigned int dev_port;
362 if ((name[0] == '.') &&
363 ((name[1] == '\0') ||
364 ((name[1] == '.') && (name[2] == '\0'))))
367 MKSTR(path, "%s/device/net/%s/%s",
368 priv->ctx->device->ibdev_path, name,
369 (dev_type ? "dev_id" : "dev_port"));
371 file = fopen(path, "rb");
376 * Switch to dev_id when dev_port does not exist as
377 * is the case with Linux kernel versions < 3.15.
388 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
393 * Switch to dev_id when dev_port returns the same value for
394 * all ports. May happen when using a MOFED release older than
395 * 3.0 with a Linux kernel >= 3.15.
397 if (dev_port == dev_port_prev)
399 dev_port_prev = dev_port;
400 if (dev_port == (priv->port - 1u))
401 snprintf(match, sizeof(match), "%s", name);
404 if (match[0] == '\0')
406 strncpy(*ifname, match, sizeof(*ifname));
411 * Read from sysfs entry.
414 * Pointer to private structure.
416 * Entry name relative to sysfs path.
418 * Data output buffer.
423 * 0 on success, -1 on failure and errno is set.
426 priv_sysfs_read(const struct priv *priv, const char *entry,
427 char *buf, size_t size)
429 char ifname[IF_NAMESIZE];
434 if (priv_get_ifname(priv, &ifname))
437 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
440 file = fopen(path, "rb");
443 ret = fread(buf, 1, size, file);
445 if (((size_t)ret < size) && (ferror(file)))
455 * Write to sysfs entry.
458 * Pointer to private structure.
460 * Entry name relative to sysfs path.
467 * 0 on success, -1 on failure and errno is set.
470 priv_sysfs_write(const struct priv *priv, const char *entry,
471 char *buf, size_t size)
473 char ifname[IF_NAMESIZE];
478 if (priv_get_ifname(priv, &ifname))
481 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
484 file = fopen(path, "wb");
487 ret = fwrite(buf, 1, size, file);
489 if (((size_t)ret < size) || (ferror(file)))
499 * Get unsigned long sysfs property.
502 * Pointer to private structure.
504 * Entry name relative to sysfs path.
506 * Value output buffer.
509 * 0 on success, -1 on failure and errno is set.
512 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
515 unsigned long value_ret;
518 ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
520 DEBUG("cannot read %s value from sysfs: %s",
521 name, strerror(errno));
524 value_str[ret] = '\0';
526 value_ret = strtoul(value_str, NULL, 0);
528 DEBUG("invalid %s value `%s': %s", name, value_str,
537 * Set unsigned long sysfs property.
540 * Pointer to private structure.
542 * Entry name relative to sysfs path.
547 * 0 on success, -1 on failure and errno is set.
550 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
553 MKSTR(value_str, "%lu", value);
555 ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
557 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
558 name, value_str, value, strerror(errno));
565 * Perform ifreq ioctl() on associated Ethernet device.
568 * Pointer to private structure.
570 * Request number to pass to ioctl().
572 * Interface request structure output buffer.
575 * 0 on success, -1 on failure and errno is set.
578 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
580 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
585 if (priv_get_ifname(priv, &ifr->ifr_name) == 0)
586 ret = ioctl(sock, req, ifr);
595 * Pointer to private structure.
597 * MTU value output buffer.
600 * 0 on success, -1 on failure and errno is set.
603 priv_get_mtu(struct priv *priv, uint16_t *mtu)
605 unsigned long ulong_mtu;
607 if (priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu) == -1)
617 * Pointer to private structure.
622 * 0 on success, -1 on failure and errno is set.
625 priv_set_mtu(struct priv *priv, uint16_t mtu)
627 return priv_set_sysfs_ulong(priv, "mtu", mtu);
634 * Pointer to private structure.
636 * Bitmask for flags that must remain untouched.
638 * Bitmask for flags to modify.
641 * 0 on success, -1 on failure and errno is set.
644 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
648 if (priv_get_sysfs_ulong(priv, "flags", &tmp) == -1)
652 return priv_set_sysfs_ulong(priv, "flags", tmp);
655 /* Device configuration. */
658 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
659 unsigned int socket, const struct rte_eth_rxconf *conf,
660 struct rte_mempool *mp);
663 rxq_cleanup(struct rxq *rxq);
666 * Ethernet device configuration.
668 * Prepare the driver for a given number of TX and RX queues.
669 * Allocate parent RSS queue when several RX queues are requested.
672 * Pointer to Ethernet device structure.
675 * 0 on success, errno value on failure.
678 dev_configure(struct rte_eth_dev *dev)
680 struct priv *priv = dev->data->dev_private;
681 unsigned int rxqs_n = dev->data->nb_rx_queues;
682 unsigned int txqs_n = dev->data->nb_tx_queues;
686 priv->rxqs = (void *)dev->data->rx_queues;
687 priv->txqs = (void *)dev->data->tx_queues;
688 if (txqs_n != priv->txqs_n) {
689 INFO("%p: TX queues number update: %u -> %u",
690 (void *)dev, priv->txqs_n, txqs_n);
691 priv->txqs_n = txqs_n;
693 if (rxqs_n == priv->rxqs_n)
695 INFO("%p: RX queues number update: %u -> %u",
696 (void *)dev, priv->rxqs_n, rxqs_n);
697 /* If RSS is enabled, disable it first. */
701 /* Only if there are no remaining child RX queues. */
702 for (i = 0; (i != priv->rxqs_n); ++i)
703 if ((*priv->rxqs)[i] != NULL)
705 rxq_cleanup(&priv->rxq_parent);
710 /* Nothing else to do. */
711 priv->rxqs_n = rxqs_n;
714 /* Allocate a new RSS parent queue if supported by hardware. */
716 ERROR("%p: only a single RX queue can be configured when"
717 " hardware doesn't support RSS",
721 /* Fail if hardware doesn't support that many RSS queues. */
722 if (rxqs_n >= priv->max_rss_tbl_sz) {
723 ERROR("%p: only %u RX queues can be configured for RSS",
724 (void *)dev, priv->max_rss_tbl_sz);
729 priv->rxqs_n = rxqs_n;
730 ret = rxq_setup(dev, &priv->rxq_parent, 0, 0, NULL, NULL);
733 /* Failure, rollback. */
741 * DPDK callback for Ethernet device configuration.
744 * Pointer to Ethernet device structure.
747 * 0 on success, negative errno value on failure.
750 mlx4_dev_configure(struct rte_eth_dev *dev)
752 struct priv *priv = dev->data->dev_private;
756 ret = dev_configure(dev);
762 /* TX queues handling. */
765 * Allocate TX queue elements.
768 * Pointer to TX queue structure.
770 * Number of elements to allocate.
773 * 0 on success, errno value on failure.
776 txq_alloc_elts(struct txq *txq, unsigned int elts_n)
779 struct txq_elt (*elts)[elts_n] =
780 rte_calloc_socket("TXQ", 1, sizeof(*elts), 0, txq->socket);
781 linear_t (*elts_linear)[elts_n] =
782 rte_calloc_socket("TXQ", 1, sizeof(*elts_linear), 0,
784 struct ibv_mr *mr_linear = NULL;
787 if ((elts == NULL) || (elts_linear == NULL)) {
788 ERROR("%p: can't allocate packets array", (void *)txq);
793 ibv_reg_mr(txq->priv->pd, elts_linear, sizeof(*elts_linear),
794 (IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE));
795 if (mr_linear == NULL) {
796 ERROR("%p: unable to configure MR, ibv_reg_mr() failed",
801 for (i = 0; (i != elts_n); ++i) {
802 struct txq_elt *elt = &(*elts)[i];
806 DEBUG("%p: allocated and configured %u WRs", (void *)txq, elts_n);
807 txq->elts_n = elts_n;
812 /* Request send completion every MLX4_PMD_TX_PER_COMP_REQ packets or
813 * at least 4 times per ring. */
814 txq->elts_comp_cd_init =
815 ((MLX4_PMD_TX_PER_COMP_REQ < (elts_n / 4)) ?
816 MLX4_PMD_TX_PER_COMP_REQ : (elts_n / 4));
817 txq->elts_comp_cd = txq->elts_comp_cd_init;
818 txq->elts_linear = elts_linear;
819 txq->mr_linear = mr_linear;
823 if (mr_linear != NULL)
824 claim_zero(ibv_dereg_mr(mr_linear));
826 rte_free(elts_linear);
829 DEBUG("%p: failed, freed everything", (void *)txq);
835 * Free TX queue elements.
838 * Pointer to TX queue structure.
841 txq_free_elts(struct txq *txq)
844 unsigned int elts_n = txq->elts_n;
845 struct txq_elt (*elts)[elts_n] = txq->elts;
846 linear_t (*elts_linear)[elts_n] = txq->elts_linear;
847 struct ibv_mr *mr_linear = txq->mr_linear;
849 DEBUG("%p: freeing WRs", (void *)txq);
852 txq->elts_linear = NULL;
853 txq->mr_linear = NULL;
854 if (mr_linear != NULL)
855 claim_zero(ibv_dereg_mr(mr_linear));
857 rte_free(elts_linear);
860 for (i = 0; (i != elemof(*elts)); ++i) {
861 struct txq_elt *elt = &(*elts)[i];
863 if (elt->buf == NULL)
865 rte_pktmbuf_free(elt->buf);
872 * Clean up a TX queue.
874 * Destroy objects, free allocated memory and reset the structure for reuse.
877 * Pointer to TX queue structure.
880 txq_cleanup(struct txq *txq)
882 struct ibv_exp_release_intf_params params;
885 DEBUG("cleaning up %p", (void *)txq);
887 if (txq->if_qp != NULL) {
888 assert(txq->priv != NULL);
889 assert(txq->priv->ctx != NULL);
890 assert(txq->qp != NULL);
891 params = (struct ibv_exp_release_intf_params){
894 claim_zero(ibv_exp_release_intf(txq->priv->ctx,
898 if (txq->if_cq != NULL) {
899 assert(txq->priv != NULL);
900 assert(txq->priv->ctx != NULL);
901 assert(txq->cq != NULL);
902 params = (struct ibv_exp_release_intf_params){
905 claim_zero(ibv_exp_release_intf(txq->priv->ctx,
910 claim_zero(ibv_destroy_qp(txq->qp));
912 claim_zero(ibv_destroy_cq(txq->cq));
913 if (txq->rd != NULL) {
914 struct ibv_exp_destroy_res_domain_attr attr = {
918 assert(txq->priv != NULL);
919 assert(txq->priv->ctx != NULL);
920 claim_zero(ibv_exp_destroy_res_domain(txq->priv->ctx,
924 for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
925 if (txq->mp2mr[i].mp == NULL)
927 assert(txq->mp2mr[i].mr != NULL);
928 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
930 memset(txq, 0, sizeof(*txq));
934 * Manage TX completions.
936 * When sending a burst, mlx4_tx_burst() posts several WRs.
937 * To improve performance, a completion event is only required once every
938 * MLX4_PMD_TX_PER_COMP_REQ sends. Doing so discards completion information
939 * for other WRs, but this information would not be used anyway.
942 * Pointer to TX queue structure.
945 * 0 on success, -1 on failure.
948 txq_complete(struct txq *txq)
950 unsigned int elts_comp = txq->elts_comp;
951 unsigned int elts_tail = txq->elts_tail;
952 const unsigned int elts_n = txq->elts_n;
955 if (unlikely(elts_comp == 0))
958 DEBUG("%p: processing %u work requests completions",
959 (void *)txq, elts_comp);
961 wcs_n = txq->if_cq->poll_cnt(txq->cq, elts_comp);
962 if (unlikely(wcs_n == 0))
964 if (unlikely(wcs_n < 0)) {
965 DEBUG("%p: ibv_poll_cq() failed (wcs_n=%d)",
970 assert(elts_comp <= txq->elts_comp);
972 * Assume WC status is successful as nothing can be done about it
975 elts_tail += wcs_n * txq->elts_comp_cd_init;
976 if (elts_tail >= elts_n)
978 txq->elts_tail = elts_tail;
979 txq->elts_comp = elts_comp;
984 * Get Memory Region (MR) <-> Memory Pool (MP) association from txq->mp2mr[].
985 * Add MP to txq->mp2mr[] if it's not registered yet. If mp2mr[] is full,
986 * remove an entry first.
989 * Pointer to TX queue structure.
991 * Memory Pool for which a Memory Region lkey must be returned.
994 * mr->lkey on success, (uint32_t)-1 on failure.
997 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
1002 for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
1003 if (unlikely(txq->mp2mr[i].mp == NULL)) {
1004 /* Unknown MP, add a new MR for it. */
1007 if (txq->mp2mr[i].mp == mp) {
1008 assert(txq->mp2mr[i].lkey != (uint32_t)-1);
1009 assert(txq->mp2mr[i].mr->lkey == txq->mp2mr[i].lkey);
1010 return txq->mp2mr[i].lkey;
1013 /* Add a new entry, register MR first. */
1014 DEBUG("%p: discovered new memory pool %p", (void *)txq, (void *)mp);
1015 mr = ibv_reg_mr(txq->priv->pd,
1016 (void *)mp->elt_va_start,
1017 (mp->elt_va_end - mp->elt_va_start),
1018 (IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE));
1019 if (unlikely(mr == NULL)) {
1020 DEBUG("%p: unable to configure MR, ibv_reg_mr() failed.",
1022 return (uint32_t)-1;
1024 if (unlikely(i == elemof(txq->mp2mr))) {
1025 /* Table is full, remove oldest entry. */
1026 DEBUG("%p: MR <-> MP table full, dropping oldest entry.",
1029 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
1030 memmove(&txq->mp2mr[0], &txq->mp2mr[1],
1031 (sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
1033 /* Store the new entry. */
1034 txq->mp2mr[i].mp = mp;
1035 txq->mp2mr[i].mr = mr;
1036 txq->mp2mr[i].lkey = mr->lkey;
1037 DEBUG("%p: new MR lkey for MP %p: 0x%08" PRIu32,
1038 (void *)txq, (void *)mp, txq->mp2mr[i].lkey);
1039 return txq->mp2mr[i].lkey;
1042 #if MLX4_PMD_SGE_WR_N > 1
1045 * Copy scattered mbuf contents to a single linear buffer.
1047 * @param[out] linear
1048 * Linear output buffer.
1050 * Scattered input buffer.
1053 * Number of bytes copied to the output buffer or 0 if not large enough.
1056 linearize_mbuf(linear_t *linear, struct rte_mbuf *buf)
1058 unsigned int size = 0;
1059 unsigned int offset;
1062 unsigned int len = DATA_LEN(buf);
1066 if (unlikely(size > sizeof(*linear)))
1068 memcpy(&(*linear)[offset],
1069 rte_pktmbuf_mtod(buf, uint8_t *),
1072 } while (buf != NULL);
1077 * Handle scattered buffers for mlx4_tx_burst().
1080 * TX queue structure.
1082 * Number of segments in buf.
1084 * TX queue element to fill.
1086 * Buffer to process.
1088 * Index of the linear buffer to use if necessary (normally txq->elts_head).
1090 * Array filled with SGEs on success.
1093 * A structure containing the processed packet size in bytes and the
1094 * number of SGEs. Both fields are set to (unsigned int)-1 in case of
1097 static struct tx_burst_sg_ret {
1098 unsigned int length;
1101 tx_burst_sg(struct txq *txq, unsigned int segs, struct txq_elt *elt,
1102 struct rte_mbuf *buf, unsigned int elts_head,
1103 struct ibv_sge (*sges)[MLX4_PMD_SGE_WR_N])
1105 unsigned int sent_size = 0;
1109 /* When there are too many segments, extra segments are
1110 * linearized in the last SGE. */
1111 if (unlikely(segs > elemof(*sges))) {
1112 segs = (elemof(*sges) - 1);
1115 /* Update element. */
1117 /* Register segments as SGEs. */
1118 for (j = 0; (j != segs); ++j) {
1119 struct ibv_sge *sge = &(*sges)[j];
1122 /* Retrieve Memory Region key for this memory pool. */
1123 lkey = txq_mp2mr(txq, buf->pool);
1124 if (unlikely(lkey == (uint32_t)-1)) {
1125 /* MR does not exist. */
1126 DEBUG("%p: unable to get MP <-> MR association",
1128 /* Clean up TX element. */
1133 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
1135 rte_prefetch0((volatile void *)
1136 (uintptr_t)sge->addr);
1137 sge->length = DATA_LEN(buf);
1139 sent_size += sge->length;
1142 /* If buf is not NULL here and is not going to be linearized,
1143 * nb_segs is not valid. */
1145 assert((buf == NULL) || (linearize));
1146 /* Linearize extra segments. */
1148 struct ibv_sge *sge = &(*sges)[segs];
1149 linear_t *linear = &(*txq->elts_linear)[elts_head];
1150 unsigned int size = linearize_mbuf(linear, buf);
1152 assert(segs == (elemof(*sges) - 1));
1154 /* Invalid packet. */
1155 DEBUG("%p: packet too large to be linearized.",
1157 /* Clean up TX element. */
1161 /* If MLX4_PMD_SGE_WR_N is 1, free mbuf immediately. */
1162 if (elemof(*sges) == 1) {
1164 struct rte_mbuf *next = NEXT(buf);
1166 rte_pktmbuf_free_seg(buf);
1168 } while (buf != NULL);
1172 sge->addr = (uintptr_t)&(*linear)[0];
1174 sge->lkey = txq->mr_linear->lkey;
1177 return (struct tx_burst_sg_ret){
1178 .length = sent_size,
1182 return (struct tx_burst_sg_ret){
1188 #endif /* MLX4_PMD_SGE_WR_N > 1 */
1191 * DPDK callback for TX.
1194 * Generic pointer to TX queue structure.
1196 * Packets to transmit.
1198 * Number of packets in array.
1201 * Number of packets successfully transmitted (<= pkts_n).
1204 mlx4_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
1206 struct txq *txq = (struct txq *)dpdk_txq;
1207 unsigned int elts_head = txq->elts_head;
1208 const unsigned int elts_tail = txq->elts_tail;
1209 const unsigned int elts_n = txq->elts_n;
1210 unsigned int elts_comp_cd = txq->elts_comp_cd;
1211 unsigned int elts_comp = 0;
1216 assert(elts_comp_cd != 0);
1218 max = (elts_n - (elts_head - elts_tail));
1222 assert(max <= elts_n);
1223 /* Always leave one free entry in the ring. */
1229 for (i = 0; (i != max); ++i) {
1230 struct rte_mbuf *buf = pkts[i];
1231 unsigned int elts_head_next =
1232 (((elts_head + 1) == elts_n) ? 0 : elts_head + 1);
1233 struct txq_elt *elt_next = &(*txq->elts)[elts_head_next];
1234 struct txq_elt *elt = &(*txq->elts)[elts_head];
1235 unsigned int segs = NB_SEGS(buf);
1236 #ifdef MLX4_PMD_SOFT_COUNTERS
1237 unsigned int sent_size = 0;
1239 uint32_t send_flags = 0;
1241 /* Clean up old buffer. */
1242 if (likely(elt->buf != NULL)) {
1243 struct rte_mbuf *tmp = elt->buf;
1245 /* Faster than rte_pktmbuf_free(). */
1247 struct rte_mbuf *next = NEXT(tmp);
1249 rte_pktmbuf_free_seg(tmp);
1251 } while (tmp != NULL);
1253 /* Request TX completion. */
1254 if (unlikely(--elts_comp_cd == 0)) {
1255 elts_comp_cd = txq->elts_comp_cd_init;
1257 send_flags |= IBV_EXP_QP_BURST_SIGNALED;
1259 /* Should we enable HW CKSUM offload */
1261 (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM)) {
1262 send_flags |= IBV_EXP_QP_BURST_IP_CSUM;
1263 /* HW does not support checksum offloads at arbitrary
1264 * offsets but automatically recognizes the packet
1265 * type. For inner L3/L4 checksums, only VXLAN (UDP)
1266 * tunnels are currently supported. */
1267 if (RTE_ETH_IS_TUNNEL_PKT(buf->packet_type))
1268 send_flags |= IBV_EXP_QP_BURST_TUNNEL;
1270 if (likely(segs == 1)) {
1275 /* Retrieve buffer information. */
1276 addr = rte_pktmbuf_mtod(buf, uintptr_t);
1277 length = DATA_LEN(buf);
1278 /* Retrieve Memory Region key for this memory pool. */
1279 lkey = txq_mp2mr(txq, buf->pool);
1280 if (unlikely(lkey == (uint32_t)-1)) {
1281 /* MR does not exist. */
1282 DEBUG("%p: unable to get MP <-> MR"
1283 " association", (void *)txq);
1284 /* Clean up TX element. */
1288 /* Update element. */
1291 rte_prefetch0((volatile void *)
1293 RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
1294 /* Put packet into send queue. */
1295 #if MLX4_PMD_MAX_INLINE > 0
1296 if (length <= txq->max_inline)
1297 err = txq->if_qp->send_pending_inline
1304 err = txq->if_qp->send_pending
1312 #ifdef MLX4_PMD_SOFT_COUNTERS
1313 sent_size += length;
1316 #if MLX4_PMD_SGE_WR_N > 1
1317 struct ibv_sge sges[MLX4_PMD_SGE_WR_N];
1318 struct tx_burst_sg_ret ret;
1320 ret = tx_burst_sg(txq, segs, elt, buf, elts_head,
1322 if (ret.length == (unsigned int)-1)
1324 RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
1325 /* Put SG list into send queue. */
1326 err = txq->if_qp->send_pending_sg_list
1333 #ifdef MLX4_PMD_SOFT_COUNTERS
1334 sent_size += ret.length;
1336 #else /* MLX4_PMD_SGE_WR_N > 1 */
1337 DEBUG("%p: TX scattered buffers support not"
1338 " compiled in", (void *)txq);
1340 #endif /* MLX4_PMD_SGE_WR_N > 1 */
1342 elts_head = elts_head_next;
1343 #ifdef MLX4_PMD_SOFT_COUNTERS
1344 /* Increment sent bytes counter. */
1345 txq->stats.obytes += sent_size;
1349 /* Take a shortcut if nothing must be sent. */
1350 if (unlikely(i == 0))
1352 #ifdef MLX4_PMD_SOFT_COUNTERS
1353 /* Increment sent packets counter. */
1354 txq->stats.opackets += i;
1356 /* Ring QP doorbell. */
1357 err = txq->if_qp->send_flush(txq->qp);
1358 if (unlikely(err)) {
1359 /* A nonzero value is not supposed to be returned.
1360 * Nothing can be done about it. */
1361 DEBUG("%p: send_flush() failed with error %d",
1364 txq->elts_head = elts_head;
1365 txq->elts_comp += elts_comp;
1366 txq->elts_comp_cd = elts_comp_cd;
1371 * Configure a TX queue.
1374 * Pointer to Ethernet device structure.
1376 * Pointer to TX queue structure.
1378 * Number of descriptors to configure in queue.
1380 * NUMA socket on which memory must be allocated.
1382 * Thresholds parameters.
1385 * 0 on success, errno value on failure.
1388 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
1389 unsigned int socket, const struct rte_eth_txconf *conf)
1391 struct priv *priv = dev->data->dev_private;
1397 struct ibv_exp_query_intf_params params;
1398 struct ibv_exp_qp_init_attr init;
1399 struct ibv_exp_res_domain_init_attr rd;
1400 struct ibv_exp_cq_init_attr cq;
1401 struct ibv_exp_qp_attr mod;
1403 enum ibv_exp_query_intf_status status;
1406 (void)conf; /* Thresholds configuration (ignored). */
1407 if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
1408 ERROR("%p: invalid number of TX descriptors (must be a"
1409 " multiple of %d)", (void *)dev, MLX4_PMD_SGE_WR_N);
1412 desc /= MLX4_PMD_SGE_WR_N;
1413 /* MRs will be registered in mp2mr[] later. */
1414 attr.rd = (struct ibv_exp_res_domain_init_attr){
1415 .comp_mask = (IBV_EXP_RES_DOMAIN_THREAD_MODEL |
1416 IBV_EXP_RES_DOMAIN_MSG_MODEL),
1417 .thread_model = IBV_EXP_THREAD_SINGLE,
1418 .msg_model = IBV_EXP_MSG_HIGH_BW,
1420 tmpl.rd = ibv_exp_create_res_domain(priv->ctx, &attr.rd);
1421 if (tmpl.rd == NULL) {
1423 ERROR("%p: RD creation failure: %s",
1424 (void *)dev, strerror(ret));
1427 attr.cq = (struct ibv_exp_cq_init_attr){
1428 .comp_mask = IBV_EXP_CQ_INIT_ATTR_RES_DOMAIN,
1429 .res_domain = tmpl.rd,
1431 tmpl.cq = ibv_exp_create_cq(priv->ctx, desc, NULL, NULL, 0, &attr.cq);
1432 if (tmpl.cq == NULL) {
1434 ERROR("%p: CQ creation failure: %s",
1435 (void *)dev, strerror(ret));
1438 DEBUG("priv->device_attr.max_qp_wr is %d",
1439 priv->device_attr.max_qp_wr);
1440 DEBUG("priv->device_attr.max_sge is %d",
1441 priv->device_attr.max_sge);
1442 attr.init = (struct ibv_exp_qp_init_attr){
1443 /* CQ to be associated with the send queue. */
1445 /* CQ to be associated with the receive queue. */
1448 /* Max number of outstanding WRs. */
1449 .max_send_wr = ((priv->device_attr.max_qp_wr < desc) ?
1450 priv->device_attr.max_qp_wr :
1452 /* Max number of scatter/gather elements in a WR. */
1453 .max_send_sge = ((priv->device_attr.max_sge <
1454 MLX4_PMD_SGE_WR_N) ?
1455 priv->device_attr.max_sge :
1457 #if MLX4_PMD_MAX_INLINE > 0
1458 .max_inline_data = MLX4_PMD_MAX_INLINE,
1461 .qp_type = IBV_QPT_RAW_PACKET,
1462 /* Do *NOT* enable this, completions events are managed per
1466 .res_domain = tmpl.rd,
1467 .comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
1468 IBV_EXP_QP_INIT_ATTR_RES_DOMAIN),
1470 tmpl.qp = ibv_exp_create_qp(priv->ctx, &attr.init);
1471 if (tmpl.qp == NULL) {
1472 ret = (errno ? errno : EINVAL);
1473 ERROR("%p: QP creation failure: %s",
1474 (void *)dev, strerror(ret));
1477 #if MLX4_PMD_MAX_INLINE > 0
1478 /* ibv_create_qp() updates this value. */
1479 tmpl.max_inline = attr.init.cap.max_inline_data;
1481 attr.mod = (struct ibv_exp_qp_attr){
1482 /* Move the QP to this state. */
1483 .qp_state = IBV_QPS_INIT,
1484 /* Primary port number. */
1485 .port_num = priv->port
1487 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod,
1488 (IBV_EXP_QP_STATE | IBV_EXP_QP_PORT));
1490 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
1491 (void *)dev, strerror(ret));
1494 ret = txq_alloc_elts(&tmpl, desc);
1496 ERROR("%p: TXQ allocation failed: %s",
1497 (void *)dev, strerror(ret));
1500 attr.mod = (struct ibv_exp_qp_attr){
1501 .qp_state = IBV_QPS_RTR
1503 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1505 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
1506 (void *)dev, strerror(ret));
1509 attr.mod.qp_state = IBV_QPS_RTS;
1510 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1512 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
1513 (void *)dev, strerror(ret));
1516 attr.params = (struct ibv_exp_query_intf_params){
1517 .intf_scope = IBV_EXP_INTF_GLOBAL,
1518 .intf = IBV_EXP_INTF_CQ,
1521 tmpl.if_cq = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
1522 if (tmpl.if_cq == NULL) {
1523 ERROR("%p: CQ interface family query failed with status %d",
1524 (void *)dev, status);
1527 attr.params = (struct ibv_exp_query_intf_params){
1528 .intf_scope = IBV_EXP_INTF_GLOBAL,
1529 .intf = IBV_EXP_INTF_QP_BURST,
1531 #ifdef HAVE_EXP_QP_BURST_CREATE_DISABLE_ETH_LOOPBACK
1532 /* MC loopback must be disabled when not using a VF. */
1535 IBV_EXP_QP_BURST_CREATE_DISABLE_ETH_LOOPBACK :
1539 tmpl.if_qp = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
1540 if (tmpl.if_qp == NULL) {
1541 ERROR("%p: QP interface family query failed with status %d",
1542 (void *)dev, status);
1545 /* Clean up txq in case we're reinitializing it. */
1546 DEBUG("%p: cleaning-up old txq just in case", (void *)txq);
1549 DEBUG("%p: txq updated with %p", (void *)txq, (void *)&tmpl);
1559 * DPDK callback to configure a TX queue.
1562 * Pointer to Ethernet device structure.
1566 * Number of descriptors to configure in queue.
1568 * NUMA socket on which memory must be allocated.
1570 * Thresholds parameters.
1573 * 0 on success, negative errno value on failure.
1576 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1577 unsigned int socket, const struct rte_eth_txconf *conf)
1579 struct priv *priv = dev->data->dev_private;
1580 struct txq *txq = (*priv->txqs)[idx];
1584 DEBUG("%p: configuring queue %u for %u descriptors",
1585 (void *)dev, idx, desc);
1586 if (idx >= priv->txqs_n) {
1587 ERROR("%p: queue index out of range (%u >= %u)",
1588 (void *)dev, idx, priv->txqs_n);
1593 DEBUG("%p: reusing already allocated queue index %u (%p)",
1594 (void *)dev, idx, (void *)txq);
1595 if (priv->started) {
1599 (*priv->txqs)[idx] = NULL;
1602 txq = rte_calloc_socket("TXQ", 1, sizeof(*txq), 0, socket);
1604 ERROR("%p: unable to allocate queue index %u",
1610 ret = txq_setup(dev, txq, desc, socket, conf);
1614 txq->stats.idx = idx;
1615 DEBUG("%p: adding TX queue %p to list",
1616 (void *)dev, (void *)txq);
1617 (*priv->txqs)[idx] = txq;
1618 /* Update send callback. */
1619 dev->tx_pkt_burst = mlx4_tx_burst;
1626 * DPDK callback to release a TX queue.
1629 * Generic TX queue pointer.
1632 mlx4_tx_queue_release(void *dpdk_txq)
1634 struct txq *txq = (struct txq *)dpdk_txq;
1642 for (i = 0; (i != priv->txqs_n); ++i)
1643 if ((*priv->txqs)[i] == txq) {
1644 DEBUG("%p: removing TX queue %p from list",
1645 (void *)priv->dev, (void *)txq);
1646 (*priv->txqs)[i] = NULL;
1654 /* RX queues handling. */
1657 * Allocate RX queue elements with scattered packets support.
1660 * Pointer to RX queue structure.
1662 * Number of elements to allocate.
1664 * If not NULL, fetch buffers from this array instead of allocating them
1665 * with rte_pktmbuf_alloc().
1668 * 0 on success, errno value on failure.
1671 rxq_alloc_elts_sp(struct rxq *rxq, unsigned int elts_n,
1672 struct rte_mbuf **pool)
1675 struct rxq_elt_sp (*elts)[elts_n] =
1676 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1681 ERROR("%p: can't allocate packets array", (void *)rxq);
1685 /* For each WR (packet). */
1686 for (i = 0; (i != elts_n); ++i) {
1688 struct rxq_elt_sp *elt = &(*elts)[i];
1689 struct ibv_recv_wr *wr = &elt->wr;
1690 struct ibv_sge (*sges)[(elemof(elt->sges))] = &elt->sges;
1692 /* These two arrays must have the same size. */
1693 assert(elemof(elt->sges) == elemof(elt->bufs));
1696 wr->next = &(*elts)[(i + 1)].wr;
1697 wr->sg_list = &(*sges)[0];
1698 wr->num_sge = elemof(*sges);
1699 /* For each SGE (segment). */
1700 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1701 struct ibv_sge *sge = &(*sges)[j];
1702 struct rte_mbuf *buf;
1706 assert(buf != NULL);
1707 rte_pktmbuf_reset(buf);
1709 buf = rte_pktmbuf_alloc(rxq->mp);
1711 assert(pool == NULL);
1712 ERROR("%p: empty mbuf pool", (void *)rxq);
1717 /* Headroom is reserved by rte_pktmbuf_alloc(). */
1718 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1719 /* Buffer is supposed to be empty. */
1720 assert(rte_pktmbuf_data_len(buf) == 0);
1721 assert(rte_pktmbuf_pkt_len(buf) == 0);
1722 /* sge->addr must be able to store a pointer. */
1723 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1725 /* The first SGE keeps its headroom. */
1726 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
1727 sge->length = (buf->buf_len -
1728 RTE_PKTMBUF_HEADROOM);
1730 /* Subsequent SGEs lose theirs. */
1731 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1732 SET_DATA_OFF(buf, 0);
1733 sge->addr = (uintptr_t)buf->buf_addr;
1734 sge->length = buf->buf_len;
1736 sge->lkey = rxq->mr->lkey;
1737 /* Redundant check for tailroom. */
1738 assert(sge->length == rte_pktmbuf_tailroom(buf));
1741 /* The last WR pointer must be NULL. */
1742 (*elts)[(i - 1)].wr.next = NULL;
1743 DEBUG("%p: allocated and configured %u WRs (%zu segments)",
1744 (void *)rxq, elts_n, (elts_n * elemof((*elts)[0].sges)));
1745 rxq->elts_n = elts_n;
1747 rxq->elts.sp = elts;
1752 assert(pool == NULL);
1753 for (i = 0; (i != elemof(*elts)); ++i) {
1755 struct rxq_elt_sp *elt = &(*elts)[i];
1757 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1758 struct rte_mbuf *buf = elt->bufs[j];
1761 rte_pktmbuf_free_seg(buf);
1766 DEBUG("%p: failed, freed everything", (void *)rxq);
1772 * Free RX queue elements with scattered packets support.
1775 * Pointer to RX queue structure.
1778 rxq_free_elts_sp(struct rxq *rxq)
1781 unsigned int elts_n = rxq->elts_n;
1782 struct rxq_elt_sp (*elts)[elts_n] = rxq->elts.sp;
1784 DEBUG("%p: freeing WRs", (void *)rxq);
1786 rxq->elts.sp = NULL;
1789 for (i = 0; (i != elemof(*elts)); ++i) {
1791 struct rxq_elt_sp *elt = &(*elts)[i];
1793 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1794 struct rte_mbuf *buf = elt->bufs[j];
1797 rte_pktmbuf_free_seg(buf);
1804 * Allocate RX queue elements.
1807 * Pointer to RX queue structure.
1809 * Number of elements to allocate.
1811 * If not NULL, fetch buffers from this array instead of allocating them
1812 * with rte_pktmbuf_alloc().
1815 * 0 on success, errno value on failure.
1818 rxq_alloc_elts(struct rxq *rxq, unsigned int elts_n, struct rte_mbuf **pool)
1821 struct rxq_elt (*elts)[elts_n] =
1822 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1827 ERROR("%p: can't allocate packets array", (void *)rxq);
1831 /* For each WR (packet). */
1832 for (i = 0; (i != elts_n); ++i) {
1833 struct rxq_elt *elt = &(*elts)[i];
1834 struct ibv_recv_wr *wr = &elt->wr;
1835 struct ibv_sge *sge = &(*elts)[i].sge;
1836 struct rte_mbuf *buf;
1840 assert(buf != NULL);
1841 rte_pktmbuf_reset(buf);
1843 buf = rte_pktmbuf_alloc(rxq->mp);
1845 assert(pool == NULL);
1846 ERROR("%p: empty mbuf pool", (void *)rxq);
1850 /* Configure WR. Work request ID contains its own index in
1851 * the elts array and the offset between SGE buffer header and
1853 WR_ID(wr->wr_id).id = i;
1854 WR_ID(wr->wr_id).offset =
1855 (((uintptr_t)buf->buf_addr + RTE_PKTMBUF_HEADROOM) -
1857 wr->next = &(*elts)[(i + 1)].wr;
1860 /* Headroom is reserved by rte_pktmbuf_alloc(). */
1861 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1862 /* Buffer is supposed to be empty. */
1863 assert(rte_pktmbuf_data_len(buf) == 0);
1864 assert(rte_pktmbuf_pkt_len(buf) == 0);
1865 /* sge->addr must be able to store a pointer. */
1866 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1867 /* SGE keeps its headroom. */
1868 sge->addr = (uintptr_t)
1869 ((uint8_t *)buf->buf_addr + RTE_PKTMBUF_HEADROOM);
1870 sge->length = (buf->buf_len - RTE_PKTMBUF_HEADROOM);
1871 sge->lkey = rxq->mr->lkey;
1872 /* Redundant check for tailroom. */
1873 assert(sge->length == rte_pktmbuf_tailroom(buf));
1874 /* Make sure elts index and SGE mbuf pointer can be deduced
1876 if ((WR_ID(wr->wr_id).id != i) ||
1877 ((void *)((uintptr_t)sge->addr -
1878 WR_ID(wr->wr_id).offset) != buf)) {
1879 ERROR("%p: cannot store index and offset in WR ID",
1882 rte_pktmbuf_free(buf);
1887 /* The last WR pointer must be NULL. */
1888 (*elts)[(i - 1)].wr.next = NULL;
1889 DEBUG("%p: allocated and configured %u single-segment WRs",
1890 (void *)rxq, elts_n);
1891 rxq->elts_n = elts_n;
1893 rxq->elts.no_sp = elts;
1898 assert(pool == NULL);
1899 for (i = 0; (i != elemof(*elts)); ++i) {
1900 struct rxq_elt *elt = &(*elts)[i];
1901 struct rte_mbuf *buf;
1903 if (elt->sge.addr == 0)
1905 assert(WR_ID(elt->wr.wr_id).id == i);
1906 buf = (void *)((uintptr_t)elt->sge.addr -
1907 WR_ID(elt->wr.wr_id).offset);
1908 rte_pktmbuf_free_seg(buf);
1912 DEBUG("%p: failed, freed everything", (void *)rxq);
1918 * Free RX queue elements.
1921 * Pointer to RX queue structure.
1924 rxq_free_elts(struct rxq *rxq)
1927 unsigned int elts_n = rxq->elts_n;
1928 struct rxq_elt (*elts)[elts_n] = rxq->elts.no_sp;
1930 DEBUG("%p: freeing WRs", (void *)rxq);
1932 rxq->elts.no_sp = NULL;
1935 for (i = 0; (i != elemof(*elts)); ++i) {
1936 struct rxq_elt *elt = &(*elts)[i];
1937 struct rte_mbuf *buf;
1939 if (elt->sge.addr == 0)
1941 assert(WR_ID(elt->wr.wr_id).id == i);
1942 buf = (void *)((uintptr_t)elt->sge.addr -
1943 WR_ID(elt->wr.wr_id).offset);
1944 rte_pktmbuf_free_seg(buf);
1950 * Delete flow steering rule.
1953 * Pointer to RX queue structure.
1955 * MAC address index.
1960 rxq_del_flow(struct rxq *rxq, unsigned int mac_index, unsigned int vlan_index)
1963 struct priv *priv = rxq->priv;
1964 const uint8_t (*mac)[ETHER_ADDR_LEN] =
1965 (const uint8_t (*)[ETHER_ADDR_LEN])
1966 priv->mac[mac_index].addr_bytes;
1968 assert(rxq->mac_flow[mac_index][vlan_index] != NULL);
1969 DEBUG("%p: removing MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
1970 " (VLAN ID %" PRIu16 ")",
1972 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
1973 mac_index, priv->vlan_filter[vlan_index].id);
1974 claim_zero(ibv_destroy_flow(rxq->mac_flow[mac_index][vlan_index]));
1975 rxq->mac_flow[mac_index][vlan_index] = NULL;
1979 * Unregister a MAC address from a RX queue.
1982 * Pointer to RX queue structure.
1984 * MAC address index.
1987 rxq_mac_addr_del(struct rxq *rxq, unsigned int mac_index)
1989 struct priv *priv = rxq->priv;
1991 unsigned int vlans = 0;
1993 assert(mac_index < elemof(priv->mac));
1994 if (!BITFIELD_ISSET(rxq->mac_configured, mac_index))
1996 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
1997 if (!priv->vlan_filter[i].enabled)
1999 rxq_del_flow(rxq, mac_index, i);
2003 rxq_del_flow(rxq, mac_index, 0);
2005 BITFIELD_RESET(rxq->mac_configured, mac_index);
2009 * Unregister all MAC addresses from a RX queue.
2012 * Pointer to RX queue structure.
2015 rxq_mac_addrs_del(struct rxq *rxq)
2017 struct priv *priv = rxq->priv;
2020 for (i = 0; (i != elemof(priv->mac)); ++i)
2021 rxq_mac_addr_del(rxq, i);
2024 static int rxq_promiscuous_enable(struct rxq *);
2025 static void rxq_promiscuous_disable(struct rxq *);
2028 * Add single flow steering rule.
2031 * Pointer to RX queue structure.
2033 * MAC address index to register.
2035 * VLAN index. Use -1 for a flow without VLAN.
2038 * 0 on success, errno value on failure.
2041 rxq_add_flow(struct rxq *rxq, unsigned int mac_index, unsigned int vlan_index)
2043 struct ibv_flow *flow;
2044 struct priv *priv = rxq->priv;
2045 const uint8_t (*mac)[ETHER_ADDR_LEN] =
2046 (const uint8_t (*)[ETHER_ADDR_LEN])
2047 priv->mac[mac_index].addr_bytes;
2049 /* Allocate flow specification on the stack. */
2050 struct __attribute__((packed)) {
2051 struct ibv_flow_attr attr;
2052 struct ibv_flow_spec_eth spec;
2054 struct ibv_flow_attr *attr = &data.attr;
2055 struct ibv_flow_spec_eth *spec = &data.spec;
2057 assert(mac_index < elemof(priv->mac));
2058 assert((vlan_index < elemof(priv->vlan_filter)) || (vlan_index == -1u));
2060 * No padding must be inserted by the compiler between attr and spec.
2061 * This layout is expected by libibverbs.
2063 assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec);
2064 *attr = (struct ibv_flow_attr){
2065 .type = IBV_FLOW_ATTR_NORMAL,
2070 *spec = (struct ibv_flow_spec_eth){
2071 .type = IBV_FLOW_SPEC_ETH,
2072 .size = sizeof(*spec),
2075 (*mac)[0], (*mac)[1], (*mac)[2],
2076 (*mac)[3], (*mac)[4], (*mac)[5]
2078 .vlan_tag = ((vlan_index != -1u) ?
2079 htons(priv->vlan_filter[vlan_index].id) :
2083 .dst_mac = "\xff\xff\xff\xff\xff\xff",
2084 .vlan_tag = ((vlan_index != -1u) ? htons(0xfff) : 0),
2087 DEBUG("%p: adding MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
2088 " (VLAN %s %" PRIu16 ")",
2090 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
2092 ((vlan_index != -1u) ? "ID" : "index"),
2093 ((vlan_index != -1u) ? priv->vlan_filter[vlan_index].id : -1u));
2094 /* Create related flow. */
2096 flow = ibv_create_flow(rxq->qp, attr);
2098 /* It's not clear whether errno is always set in this case. */
2099 ERROR("%p: flow configuration failed, errno=%d: %s",
2101 (errno ? strerror(errno) : "Unknown error"));
2106 if (vlan_index == -1u)
2108 assert(rxq->mac_flow[mac_index][vlan_index] == NULL);
2109 rxq->mac_flow[mac_index][vlan_index] = flow;
2114 * Register a MAC address in a RX queue.
2117 * Pointer to RX queue structure.
2119 * MAC address index to register.
2122 * 0 on success, errno value on failure.
2125 rxq_mac_addr_add(struct rxq *rxq, unsigned int mac_index)
2127 struct priv *priv = rxq->priv;
2129 unsigned int vlans = 0;
2132 assert(mac_index < elemof(priv->mac));
2133 if (BITFIELD_ISSET(rxq->mac_configured, mac_index))
2134 rxq_mac_addr_del(rxq, mac_index);
2135 /* Fill VLAN specifications. */
2136 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
2137 if (!priv->vlan_filter[i].enabled)
2139 /* Create related flow. */
2140 ret = rxq_add_flow(rxq, mac_index, i);
2145 /* Failure, rollback. */
2147 if (priv->vlan_filter[--i].enabled)
2148 rxq_del_flow(rxq, mac_index, i);
2152 /* In case there is no VLAN filter. */
2154 ret = rxq_add_flow(rxq, mac_index, -1);
2158 BITFIELD_SET(rxq->mac_configured, mac_index);
2163 * Register all MAC addresses in a RX queue.
2166 * Pointer to RX queue structure.
2169 * 0 on success, errno value on failure.
2172 rxq_mac_addrs_add(struct rxq *rxq)
2174 struct priv *priv = rxq->priv;
2178 for (i = 0; (i != elemof(priv->mac)); ++i) {
2179 if (!BITFIELD_ISSET(priv->mac_configured, i))
2181 ret = rxq_mac_addr_add(rxq, i);
2184 /* Failure, rollback. */
2186 rxq_mac_addr_del(rxq, --i);
2194 * Unregister a MAC address.
2196 * In RSS mode, the MAC address is unregistered from the parent queue,
2197 * otherwise it is unregistered from each queue directly.
2200 * Pointer to private structure.
2202 * MAC address index.
2205 priv_mac_addr_del(struct priv *priv, unsigned int mac_index)
2209 assert(mac_index < elemof(priv->mac));
2210 if (!BITFIELD_ISSET(priv->mac_configured, mac_index))
2213 rxq_mac_addr_del(&priv->rxq_parent, mac_index);
2216 for (i = 0; (i != priv->dev->data->nb_rx_queues); ++i)
2217 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2219 BITFIELD_RESET(priv->mac_configured, mac_index);
2223 * Register a MAC address.
2225 * In RSS mode, the MAC address is registered in the parent queue,
2226 * otherwise it is registered in each queue directly.
2229 * Pointer to private structure.
2231 * MAC address index to use.
2233 * MAC address to register.
2236 * 0 on success, errno value on failure.
2239 priv_mac_addr_add(struct priv *priv, unsigned int mac_index,
2240 const uint8_t (*mac)[ETHER_ADDR_LEN])
2245 assert(mac_index < elemof(priv->mac));
2246 /* First, make sure this address isn't already configured. */
2247 for (i = 0; (i != elemof(priv->mac)); ++i) {
2248 /* Skip this index, it's going to be reconfigured. */
2251 if (!BITFIELD_ISSET(priv->mac_configured, i))
2253 if (memcmp(priv->mac[i].addr_bytes, *mac, sizeof(*mac)))
2255 /* Address already configured elsewhere, return with error. */
2258 if (BITFIELD_ISSET(priv->mac_configured, mac_index))
2259 priv_mac_addr_del(priv, mac_index);
2260 priv->mac[mac_index] = (struct ether_addr){
2262 (*mac)[0], (*mac)[1], (*mac)[2],
2263 (*mac)[3], (*mac)[4], (*mac)[5]
2266 /* If device isn't started, this is all we need to do. */
2267 if (!priv->started) {
2269 /* Verify that all queues have this index disabled. */
2270 for (i = 0; (i != priv->rxqs_n); ++i) {
2271 if ((*priv->rxqs)[i] == NULL)
2273 assert(!BITFIELD_ISSET
2274 ((*priv->rxqs)[i]->mac_configured, mac_index));
2280 ret = rxq_mac_addr_add(&priv->rxq_parent, mac_index);
2285 for (i = 0; (i != priv->rxqs_n); ++i) {
2286 if ((*priv->rxqs)[i] == NULL)
2288 ret = rxq_mac_addr_add((*priv->rxqs)[i], mac_index);
2291 /* Failure, rollback. */
2293 if ((*priv->rxqs)[(--i)] != NULL)
2294 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2298 BITFIELD_SET(priv->mac_configured, mac_index);
2303 * Enable allmulti mode in a RX queue.
2306 * Pointer to RX queue structure.
2309 * 0 on success, errno value on failure.
2312 rxq_allmulticast_enable(struct rxq *rxq)
2314 struct ibv_flow *flow;
2315 struct ibv_flow_attr attr = {
2316 .type = IBV_FLOW_ATTR_MC_DEFAULT,
2318 .port = rxq->priv->port,
2322 DEBUG("%p: enabling allmulticast mode", (void *)rxq);
2323 if (rxq->allmulti_flow != NULL)
2326 flow = ibv_create_flow(rxq->qp, &attr);
2328 /* It's not clear whether errno is always set in this case. */
2329 ERROR("%p: flow configuration failed, errno=%d: %s",
2331 (errno ? strerror(errno) : "Unknown error"));
2336 rxq->allmulti_flow = flow;
2337 DEBUG("%p: allmulticast mode enabled", (void *)rxq);
2342 * Disable allmulti mode in a RX queue.
2345 * Pointer to RX queue structure.
2348 rxq_allmulticast_disable(struct rxq *rxq)
2350 DEBUG("%p: disabling allmulticast mode", (void *)rxq);
2351 if (rxq->allmulti_flow == NULL)
2353 claim_zero(ibv_destroy_flow(rxq->allmulti_flow));
2354 rxq->allmulti_flow = NULL;
2355 DEBUG("%p: allmulticast mode disabled", (void *)rxq);
2359 * Enable promiscuous mode in a RX queue.
2362 * Pointer to RX queue structure.
2365 * 0 on success, errno value on failure.
2368 rxq_promiscuous_enable(struct rxq *rxq)
2370 struct ibv_flow *flow;
2371 struct ibv_flow_attr attr = {
2372 .type = IBV_FLOW_ATTR_ALL_DEFAULT,
2374 .port = rxq->priv->port,
2380 DEBUG("%p: enabling promiscuous mode", (void *)rxq);
2381 if (rxq->promisc_flow != NULL)
2384 flow = ibv_create_flow(rxq->qp, &attr);
2386 /* It's not clear whether errno is always set in this case. */
2387 ERROR("%p: flow configuration failed, errno=%d: %s",
2389 (errno ? strerror(errno) : "Unknown error"));
2394 rxq->promisc_flow = flow;
2395 DEBUG("%p: promiscuous mode enabled", (void *)rxq);
2400 * Disable promiscuous mode in a RX queue.
2403 * Pointer to RX queue structure.
2406 rxq_promiscuous_disable(struct rxq *rxq)
2410 DEBUG("%p: disabling promiscuous mode", (void *)rxq);
2411 if (rxq->promisc_flow == NULL)
2413 claim_zero(ibv_destroy_flow(rxq->promisc_flow));
2414 rxq->promisc_flow = NULL;
2415 DEBUG("%p: promiscuous mode disabled", (void *)rxq);
2419 * Clean up a RX queue.
2421 * Destroy objects, free allocated memory and reset the structure for reuse.
2424 * Pointer to RX queue structure.
2427 rxq_cleanup(struct rxq *rxq)
2429 struct ibv_exp_release_intf_params params;
2431 DEBUG("cleaning up %p", (void *)rxq);
2433 rxq_free_elts_sp(rxq);
2436 if (rxq->if_qp != NULL) {
2437 assert(rxq->priv != NULL);
2438 assert(rxq->priv->ctx != NULL);
2439 assert(rxq->qp != NULL);
2440 params = (struct ibv_exp_release_intf_params){
2443 claim_zero(ibv_exp_release_intf(rxq->priv->ctx,
2447 if (rxq->if_cq != NULL) {
2448 assert(rxq->priv != NULL);
2449 assert(rxq->priv->ctx != NULL);
2450 assert(rxq->cq != NULL);
2451 params = (struct ibv_exp_release_intf_params){
2454 claim_zero(ibv_exp_release_intf(rxq->priv->ctx,
2458 if (rxq->qp != NULL) {
2459 rxq_promiscuous_disable(rxq);
2460 rxq_allmulticast_disable(rxq);
2461 rxq_mac_addrs_del(rxq);
2462 claim_zero(ibv_destroy_qp(rxq->qp));
2464 if (rxq->cq != NULL)
2465 claim_zero(ibv_destroy_cq(rxq->cq));
2466 if (rxq->rd != NULL) {
2467 struct ibv_exp_destroy_res_domain_attr attr = {
2471 assert(rxq->priv != NULL);
2472 assert(rxq->priv->ctx != NULL);
2473 claim_zero(ibv_exp_destroy_res_domain(rxq->priv->ctx,
2477 if (rxq->mr != NULL)
2478 claim_zero(ibv_dereg_mr(rxq->mr));
2479 memset(rxq, 0, sizeof(*rxq));
2483 * Translate RX completion flags to packet type.
2486 * RX completion flags returned by poll_length_flags().
2489 * Packet type for struct rte_mbuf.
2491 static inline uint32_t
2492 rxq_cq_to_pkt_type(uint32_t flags)
2496 if (flags & IBV_EXP_CQ_RX_TUNNEL_PACKET)
2499 IBV_EXP_CQ_RX_OUTER_IPV4_PACKET, RTE_PTYPE_L3_IPV4) |
2501 IBV_EXP_CQ_RX_OUTER_IPV6_PACKET, RTE_PTYPE_L3_IPV6) |
2503 IBV_EXP_CQ_RX_IPV4_PACKET, RTE_PTYPE_INNER_L3_IPV4) |
2505 IBV_EXP_CQ_RX_IPV6_PACKET, RTE_PTYPE_INNER_L3_IPV6);
2509 IBV_EXP_CQ_RX_IPV4_PACKET, RTE_PTYPE_L3_IPV4) |
2511 IBV_EXP_CQ_RX_IPV6_PACKET, RTE_PTYPE_L3_IPV6);
2516 * Translate RX completion flags to offload flags.
2519 * Pointer to RX queue structure.
2521 * RX completion flags returned by poll_length_flags().
2524 * Offload flags (ol_flags) for struct rte_mbuf.
2526 static inline uint32_t
2527 rxq_cq_to_ol_flags(const struct rxq *rxq, uint32_t flags)
2529 uint32_t ol_flags = 0;
2534 IBV_EXP_CQ_RX_IP_CSUM_OK,
2535 PKT_RX_IP_CKSUM_BAD) |
2537 IBV_EXP_CQ_RX_TCP_UDP_CSUM_OK,
2538 PKT_RX_L4_CKSUM_BAD);
2540 * PKT_RX_IP_CKSUM_BAD and PKT_RX_L4_CKSUM_BAD are used in place
2541 * of PKT_RX_EIP_CKSUM_BAD because the latter is not functional
2544 if ((flags & IBV_EXP_CQ_RX_TUNNEL_PACKET) && (rxq->csum_l2tun))
2547 IBV_EXP_CQ_RX_OUTER_IP_CSUM_OK,
2548 PKT_RX_IP_CKSUM_BAD) |
2550 IBV_EXP_CQ_RX_OUTER_TCP_UDP_CSUM_OK,
2551 PKT_RX_L4_CKSUM_BAD);
2556 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n);
2559 * DPDK callback for RX with scattered packets support.
2562 * Generic pointer to RX queue structure.
2564 * Array to store received packets.
2566 * Maximum number of packets in array.
2569 * Number of packets successfully received (<= pkts_n).
2572 mlx4_rx_burst_sp(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2574 struct rxq *rxq = (struct rxq *)dpdk_rxq;
2575 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
2576 const unsigned int elts_n = rxq->elts_n;
2577 unsigned int elts_head = rxq->elts_head;
2578 struct ibv_recv_wr head;
2579 struct ibv_recv_wr **next = &head.next;
2580 struct ibv_recv_wr *bad_wr;
2582 unsigned int pkts_ret = 0;
2585 if (unlikely(!rxq->sp))
2586 return mlx4_rx_burst(dpdk_rxq, pkts, pkts_n);
2587 if (unlikely(elts == NULL)) /* See RTE_DEV_CMD_SET_MTU. */
2589 for (i = 0; (i != pkts_n); ++i) {
2590 struct rxq_elt_sp *elt = &(*elts)[elts_head];
2591 struct ibv_recv_wr *wr = &elt->wr;
2592 uint64_t wr_id = wr->wr_id;
2594 unsigned int pkt_buf_len;
2595 struct rte_mbuf *pkt_buf = NULL; /* Buffer returned in pkts. */
2596 struct rte_mbuf **pkt_buf_next = &pkt_buf;
2597 unsigned int seg_headroom = RTE_PKTMBUF_HEADROOM;
2601 /* Sanity checks. */
2605 assert(wr_id < rxq->elts_n);
2606 assert(wr->sg_list == elt->sges);
2607 assert(wr->num_sge == elemof(elt->sges));
2608 assert(elts_head < rxq->elts_n);
2609 assert(rxq->elts_head < rxq->elts_n);
2610 ret = rxq->if_cq->poll_length_flags(rxq->cq, NULL, NULL,
2612 if (unlikely(ret < 0)) {
2616 DEBUG("rxq=%p, poll_length() failed (ret=%d)",
2618 /* ibv_poll_cq() must be used in case of failure. */
2619 wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
2620 if (unlikely(wcs_n == 0))
2622 if (unlikely(wcs_n < 0)) {
2623 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
2624 (void *)rxq, wcs_n);
2628 if (unlikely(wc.status != IBV_WC_SUCCESS)) {
2629 /* Whatever, just repost the offending WR. */
2630 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
2631 " completion status (%d): %s",
2632 (void *)rxq, wc.wr_id, wc.status,
2633 ibv_wc_status_str(wc.status));
2634 #ifdef MLX4_PMD_SOFT_COUNTERS
2635 /* Increment dropped packets counter. */
2636 ++rxq->stats.idropped;
2638 /* Link completed WRs together for repost. */
2649 /* Link completed WRs together for repost. */
2653 * Replace spent segments with new ones, concatenate and
2654 * return them as pkt_buf.
2657 struct ibv_sge *sge = &elt->sges[j];
2658 struct rte_mbuf *seg = elt->bufs[j];
2659 struct rte_mbuf *rep;
2660 unsigned int seg_tailroom;
2663 * Fetch initial bytes of packet descriptor into a
2664 * cacheline while allocating rep.
2667 rep = __rte_mbuf_raw_alloc(rxq->mp);
2668 if (unlikely(rep == NULL)) {
2670 * Unable to allocate a replacement mbuf,
2673 DEBUG("rxq=%p, wr_id=%" PRIu64 ":"
2674 " can't allocate a new mbuf",
2675 (void *)rxq, wr_id);
2676 if (pkt_buf != NULL) {
2677 *pkt_buf_next = NULL;
2678 rte_pktmbuf_free(pkt_buf);
2680 /* Increase out of memory counters. */
2681 ++rxq->stats.rx_nombuf;
2682 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
2686 /* Poison user-modifiable fields in rep. */
2687 NEXT(rep) = (void *)((uintptr_t)-1);
2688 SET_DATA_OFF(rep, 0xdead);
2689 DATA_LEN(rep) = 0xd00d;
2690 PKT_LEN(rep) = 0xdeadd00d;
2691 NB_SEGS(rep) = 0x2a;
2695 assert(rep->buf_len == seg->buf_len);
2696 assert(rep->buf_len == rxq->mb_len);
2697 /* Reconfigure sge to use rep instead of seg. */
2698 assert(sge->lkey == rxq->mr->lkey);
2699 sge->addr = ((uintptr_t)rep->buf_addr + seg_headroom);
2702 /* Update pkt_buf if it's the first segment, or link
2703 * seg to the previous one and update pkt_buf_next. */
2704 *pkt_buf_next = seg;
2705 pkt_buf_next = &NEXT(seg);
2706 /* Update seg information. */
2707 seg_tailroom = (seg->buf_len - seg_headroom);
2708 assert(sge->length == seg_tailroom);
2709 SET_DATA_OFF(seg, seg_headroom);
2710 if (likely(len <= seg_tailroom)) {
2712 DATA_LEN(seg) = len;
2715 assert(rte_pktmbuf_headroom(seg) ==
2717 assert(rte_pktmbuf_tailroom(seg) ==
2718 (seg_tailroom - len));
2721 DATA_LEN(seg) = seg_tailroom;
2722 PKT_LEN(seg) = seg_tailroom;
2724 assert(rte_pktmbuf_headroom(seg) == seg_headroom);
2725 assert(rte_pktmbuf_tailroom(seg) == 0);
2726 /* Fix len and clear headroom for next segments. */
2727 len -= seg_tailroom;
2730 /* Update head and tail segments. */
2731 *pkt_buf_next = NULL;
2732 assert(pkt_buf != NULL);
2734 NB_SEGS(pkt_buf) = j;
2735 PORT(pkt_buf) = rxq->port_id;
2736 PKT_LEN(pkt_buf) = pkt_buf_len;
2737 pkt_buf->packet_type = rxq_cq_to_pkt_type(flags);
2739 /* Return packet. */
2740 *(pkts++) = pkt_buf;
2742 #ifdef MLX4_PMD_SOFT_COUNTERS
2743 /* Increase bytes counter. */
2744 rxq->stats.ibytes += pkt_buf_len;
2747 if (++elts_head >= elts_n)
2751 if (unlikely(i == 0))
2756 DEBUG("%p: reposting %d WRs", (void *)rxq, i);
2758 ret = ibv_post_recv(rxq->qp, head.next, &bad_wr);
2759 if (unlikely(ret)) {
2760 /* Inability to repost WRs is fatal. */
2761 DEBUG("%p: ibv_post_recv(): failed for WR %p: %s",
2767 rxq->elts_head = elts_head;
2768 #ifdef MLX4_PMD_SOFT_COUNTERS
2769 /* Increase packets counter. */
2770 rxq->stats.ipackets += pkts_ret;
2776 * DPDK callback for RX.
2778 * The following function is the same as mlx4_rx_burst_sp(), except it doesn't
2779 * manage scattered packets. Improves performance when MRU is lower than the
2780 * size of the first segment.
2783 * Generic pointer to RX queue structure.
2785 * Array to store received packets.
2787 * Maximum number of packets in array.
2790 * Number of packets successfully received (<= pkts_n).
2793 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2795 struct rxq *rxq = (struct rxq *)dpdk_rxq;
2796 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
2797 const unsigned int elts_n = rxq->elts_n;
2798 unsigned int elts_head = rxq->elts_head;
2799 struct ibv_sge sges[pkts_n];
2801 unsigned int pkts_ret = 0;
2804 if (unlikely(rxq->sp))
2805 return mlx4_rx_burst_sp(dpdk_rxq, pkts, pkts_n);
2806 for (i = 0; (i != pkts_n); ++i) {
2807 struct rxq_elt *elt = &(*elts)[elts_head];
2808 struct ibv_recv_wr *wr = &elt->wr;
2809 uint64_t wr_id = wr->wr_id;
2811 struct rte_mbuf *seg = (void *)((uintptr_t)elt->sge.addr -
2812 WR_ID(wr_id).offset);
2813 struct rte_mbuf *rep;
2816 /* Sanity checks. */
2817 assert(WR_ID(wr_id).id < rxq->elts_n);
2818 assert(wr->sg_list == &elt->sge);
2819 assert(wr->num_sge == 1);
2820 assert(elts_head < rxq->elts_n);
2821 assert(rxq->elts_head < rxq->elts_n);
2822 ret = rxq->if_cq->poll_length_flags(rxq->cq, NULL, NULL,
2824 if (unlikely(ret < 0)) {
2828 DEBUG("rxq=%p, poll_length() failed (ret=%d)",
2830 /* ibv_poll_cq() must be used in case of failure. */
2831 wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
2832 if (unlikely(wcs_n == 0))
2834 if (unlikely(wcs_n < 0)) {
2835 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
2836 (void *)rxq, wcs_n);
2840 if (unlikely(wc.status != IBV_WC_SUCCESS)) {
2841 /* Whatever, just repost the offending WR. */
2842 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
2843 " completion status (%d): %s",
2844 (void *)rxq, wc.wr_id, wc.status,
2845 ibv_wc_status_str(wc.status));
2846 #ifdef MLX4_PMD_SOFT_COUNTERS
2847 /* Increment dropped packets counter. */
2848 ++rxq->stats.idropped;
2850 /* Add SGE to array for repost. */
2860 * Fetch initial bytes of packet descriptor into a
2861 * cacheline while allocating rep.
2864 rep = __rte_mbuf_raw_alloc(rxq->mp);
2865 if (unlikely(rep == NULL)) {
2867 * Unable to allocate a replacement mbuf,
2870 DEBUG("rxq=%p, wr_id=%" PRIu32 ":"
2871 " can't allocate a new mbuf",
2872 (void *)rxq, WR_ID(wr_id).id);
2873 /* Increase out of memory counters. */
2874 ++rxq->stats.rx_nombuf;
2875 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
2879 /* Reconfigure sge to use rep instead of seg. */
2880 elt->sge.addr = (uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM;
2881 assert(elt->sge.lkey == rxq->mr->lkey);
2882 WR_ID(wr->wr_id).offset =
2883 (((uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM) -
2885 assert(WR_ID(wr->wr_id).id == WR_ID(wr_id).id);
2887 /* Add SGE to array for repost. */
2890 /* Update seg information. */
2891 SET_DATA_OFF(seg, RTE_PKTMBUF_HEADROOM);
2893 PORT(seg) = rxq->port_id;
2896 DATA_LEN(seg) = len;
2897 seg->packet_type = rxq_cq_to_pkt_type(flags);
2898 seg->ol_flags = rxq_cq_to_ol_flags(rxq, flags);
2900 /* Return packet. */
2903 #ifdef MLX4_PMD_SOFT_COUNTERS
2904 /* Increase bytes counter. */
2905 rxq->stats.ibytes += len;
2908 if (++elts_head >= elts_n)
2912 if (unlikely(i == 0))
2916 DEBUG("%p: reposting %u WRs", (void *)rxq, i);
2918 ret = rxq->if_qp->recv_burst(rxq->qp, sges, i);
2919 if (unlikely(ret)) {
2920 /* Inability to repost WRs is fatal. */
2921 DEBUG("%p: recv_burst(): failed (ret=%d)",
2926 rxq->elts_head = elts_head;
2927 #ifdef MLX4_PMD_SOFT_COUNTERS
2928 /* Increase packets counter. */
2929 rxq->stats.ipackets += pkts_ret;
2935 * Allocate a Queue Pair.
2936 * Optionally setup inline receive if supported.
2939 * Pointer to private structure.
2941 * Completion queue to associate with QP.
2943 * Number of descriptors in QP (hint only).
2946 * QP pointer or NULL in case of error.
2948 static struct ibv_qp *
2949 rxq_setup_qp(struct priv *priv, struct ibv_cq *cq, uint16_t desc,
2950 struct ibv_exp_res_domain *rd)
2952 struct ibv_exp_qp_init_attr attr = {
2953 /* CQ to be associated with the send queue. */
2955 /* CQ to be associated with the receive queue. */
2958 /* Max number of outstanding WRs. */
2959 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
2960 priv->device_attr.max_qp_wr :
2962 /* Max number of scatter/gather elements in a WR. */
2963 .max_recv_sge = ((priv->device_attr.max_sge <
2964 MLX4_PMD_SGE_WR_N) ?
2965 priv->device_attr.max_sge :
2968 .qp_type = IBV_QPT_RAW_PACKET,
2969 .comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
2970 IBV_EXP_QP_INIT_ATTR_RES_DOMAIN),
2976 attr.max_inl_recv = priv->inl_recv_size;
2977 attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
2979 return ibv_exp_create_qp(priv->ctx, &attr);
2985 * Allocate a RSS Queue Pair.
2986 * Optionally setup inline receive if supported.
2989 * Pointer to private structure.
2991 * Completion queue to associate with QP.
2993 * Number of descriptors in QP (hint only).
2995 * If nonzero, create a parent QP, otherwise a child.
2998 * QP pointer or NULL in case of error.
3000 static struct ibv_qp *
3001 rxq_setup_qp_rss(struct priv *priv, struct ibv_cq *cq, uint16_t desc,
3002 int parent, struct ibv_exp_res_domain *rd)
3004 struct ibv_exp_qp_init_attr attr = {
3005 /* CQ to be associated with the send queue. */
3007 /* CQ to be associated with the receive queue. */
3010 /* Max number of outstanding WRs. */
3011 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
3012 priv->device_attr.max_qp_wr :
3014 /* Max number of scatter/gather elements in a WR. */
3015 .max_recv_sge = ((priv->device_attr.max_sge <
3016 MLX4_PMD_SGE_WR_N) ?
3017 priv->device_attr.max_sge :
3020 .qp_type = IBV_QPT_RAW_PACKET,
3021 .comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
3022 IBV_EXP_QP_INIT_ATTR_RES_DOMAIN |
3023 IBV_EXP_QP_INIT_ATTR_QPG),
3029 attr.max_inl_recv = priv->inl_recv_size,
3030 attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
3033 attr.qpg.qpg_type = IBV_EXP_QPG_PARENT;
3034 /* TSS isn't necessary. */
3035 attr.qpg.parent_attrib.tss_child_count = 0;
3036 attr.qpg.parent_attrib.rss_child_count = priv->rxqs_n;
3037 DEBUG("initializing parent RSS queue");
3039 attr.qpg.qpg_type = IBV_EXP_QPG_CHILD_RX;
3040 attr.qpg.qpg_parent = priv->rxq_parent.qp;
3041 DEBUG("initializing child RSS queue");
3043 return ibv_exp_create_qp(priv->ctx, &attr);
3046 #endif /* RSS_SUPPORT */
3049 * Reconfigure a RX queue with new parameters.
3051 * rxq_rehash() does not allocate mbufs, which, if not done from the right
3052 * thread (such as a control thread), may corrupt the pool.
3053 * In case of failure, the queue is left untouched.
3056 * Pointer to Ethernet device structure.
3061 * 0 on success, errno value on failure.
3064 rxq_rehash(struct rte_eth_dev *dev, struct rxq *rxq)
3066 struct priv *priv = rxq->priv;
3067 struct rxq tmpl = *rxq;
3068 unsigned int mbuf_n;
3069 unsigned int desc_n;
3070 struct rte_mbuf **pool;
3072 struct ibv_exp_qp_attr mod;
3073 struct ibv_recv_wr *bad_wr;
3075 int parent = (rxq == &priv->rxq_parent);
3078 ERROR("%p: cannot rehash parent queue %p",
3079 (void *)dev, (void *)rxq);
3082 DEBUG("%p: rehashing queue %p", (void *)dev, (void *)rxq);
3083 /* Number of descriptors and mbufs currently allocated. */
3084 desc_n = (tmpl.elts_n * (tmpl.sp ? MLX4_PMD_SGE_WR_N : 1));
3086 /* Toggle RX checksum offload if hardware supports it. */
3087 if (priv->hw_csum) {
3088 tmpl.csum = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3089 rxq->csum = tmpl.csum;
3091 if (priv->hw_csum_l2tun) {
3092 tmpl.csum_l2tun = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3093 rxq->csum_l2tun = tmpl.csum_l2tun;
3095 /* Enable scattered packets support for this queue if necessary. */
3096 if ((dev->data->dev_conf.rxmode.jumbo_frame) &&
3097 (dev->data->dev_conf.rxmode.max_rx_pkt_len >
3098 (tmpl.mb_len - RTE_PKTMBUF_HEADROOM))) {
3100 desc_n /= MLX4_PMD_SGE_WR_N;
3103 DEBUG("%p: %s scattered packets support (%u WRs)",
3104 (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc_n);
3105 /* If scatter mode is the same as before, nothing to do. */
3106 if (tmpl.sp == rxq->sp) {
3107 DEBUG("%p: nothing to do", (void *)dev);
3110 /* Remove attached flows if RSS is disabled (no parent queue). */
3112 rxq_allmulticast_disable(&tmpl);
3113 rxq_promiscuous_disable(&tmpl);
3114 rxq_mac_addrs_del(&tmpl);
3115 /* Update original queue in case of failure. */
3116 rxq->allmulti_flow = tmpl.allmulti_flow;
3117 rxq->promisc_flow = tmpl.promisc_flow;
3118 memcpy(rxq->mac_configured, tmpl.mac_configured,
3119 sizeof(rxq->mac_configured));
3120 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
3122 /* From now on, any failure will render the queue unusable.
3123 * Reinitialize QP. */
3124 mod = (struct ibv_exp_qp_attr){ .qp_state = IBV_QPS_RESET };
3125 err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3127 ERROR("%p: cannot reset QP: %s", (void *)dev, strerror(err));
3131 err = ibv_resize_cq(tmpl.cq, desc_n);
3133 ERROR("%p: cannot resize CQ: %s", (void *)dev, strerror(err));
3137 mod = (struct ibv_exp_qp_attr){
3138 /* Move the QP to this state. */
3139 .qp_state = IBV_QPS_INIT,
3140 /* Primary port number. */
3141 .port_num = priv->port
3143 err = ibv_exp_modify_qp(tmpl.qp, &mod,
3146 (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
3147 #endif /* RSS_SUPPORT */
3150 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
3151 (void *)dev, strerror(err));
3155 /* Reconfigure flows. Do not care for errors. */
3157 rxq_mac_addrs_add(&tmpl);
3159 rxq_promiscuous_enable(&tmpl);
3161 rxq_allmulticast_enable(&tmpl);
3162 /* Update original queue in case of failure. */
3163 rxq->allmulti_flow = tmpl.allmulti_flow;
3164 rxq->promisc_flow = tmpl.promisc_flow;
3165 memcpy(rxq->mac_configured, tmpl.mac_configured,
3166 sizeof(rxq->mac_configured));
3167 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
3169 /* Allocate pool. */
3170 pool = rte_malloc(__func__, (mbuf_n * sizeof(*pool)), 0);
3172 ERROR("%p: cannot allocate memory", (void *)dev);
3175 /* Snatch mbufs from original queue. */
3178 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
3180 for (i = 0; (i != elemof(*elts)); ++i) {
3181 struct rxq_elt_sp *elt = &(*elts)[i];
3184 for (j = 0; (j != elemof(elt->bufs)); ++j) {
3185 assert(elt->bufs[j] != NULL);
3186 pool[k++] = elt->bufs[j];
3190 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
3192 for (i = 0; (i != elemof(*elts)); ++i) {
3193 struct rxq_elt *elt = &(*elts)[i];
3194 struct rte_mbuf *buf = (void *)
3195 ((uintptr_t)elt->sge.addr -
3196 WR_ID(elt->wr.wr_id).offset);
3198 assert(WR_ID(elt->wr.wr_id).id == i);
3202 assert(k == mbuf_n);
3204 tmpl.elts.sp = NULL;
3205 assert((void *)&tmpl.elts.sp == (void *)&tmpl.elts.no_sp);
3207 rxq_alloc_elts_sp(&tmpl, desc_n, pool) :
3208 rxq_alloc_elts(&tmpl, desc_n, pool));
3210 ERROR("%p: cannot reallocate WRs, aborting", (void *)dev);
3215 assert(tmpl.elts_n == desc_n);
3216 assert(tmpl.elts.sp != NULL);
3218 /* Clean up original data. */
3220 rte_free(rxq->elts.sp);
3221 rxq->elts.sp = NULL;
3223 err = ibv_post_recv(tmpl.qp,
3225 &(*tmpl.elts.sp)[0].wr :
3226 &(*tmpl.elts.no_sp)[0].wr),
3229 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
3235 mod = (struct ibv_exp_qp_attr){
3236 .qp_state = IBV_QPS_RTR
3238 err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3240 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
3241 (void *)dev, strerror(err));
3249 * Configure a RX queue.
3252 * Pointer to Ethernet device structure.
3254 * Pointer to RX queue structure.
3256 * Number of descriptors to configure in queue.
3258 * NUMA socket on which memory must be allocated.
3260 * Thresholds parameters.
3262 * Memory pool for buffer allocations.
3265 * 0 on success, errno value on failure.
3268 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
3269 unsigned int socket, const struct rte_eth_rxconf *conf,
3270 struct rte_mempool *mp)
3272 struct priv *priv = dev->data->dev_private;
3278 struct ibv_exp_qp_attr mod;
3280 struct ibv_exp_query_intf_params params;
3281 struct ibv_exp_cq_init_attr cq;
3282 struct ibv_exp_res_domain_init_attr rd;
3284 enum ibv_exp_query_intf_status status;
3285 struct ibv_recv_wr *bad_wr;
3286 struct rte_mbuf *buf;
3288 int parent = (rxq == &priv->rxq_parent);
3290 (void)conf; /* Thresholds configuration (ignored). */
3292 * If this is a parent queue, hardware must support RSS and
3293 * RSS must be enabled.
3295 assert((!parent) || ((priv->hw_rss) && (priv->rss)));
3297 /* Even if unused, ibv_create_cq() requires at least one
3302 if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
3303 ERROR("%p: invalid number of RX descriptors (must be a"
3304 " multiple of %d)", (void *)dev, MLX4_PMD_SGE_WR_N);
3307 /* Get mbuf length. */
3308 buf = rte_pktmbuf_alloc(mp);
3310 ERROR("%p: unable to allocate mbuf", (void *)dev);
3313 tmpl.mb_len = buf->buf_len;
3314 assert((rte_pktmbuf_headroom(buf) +
3315 rte_pktmbuf_tailroom(buf)) == tmpl.mb_len);
3316 assert(rte_pktmbuf_headroom(buf) == RTE_PKTMBUF_HEADROOM);
3317 rte_pktmbuf_free(buf);
3318 /* Toggle RX checksum offload if hardware supports it. */
3320 tmpl.csum = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3321 if (priv->hw_csum_l2tun)
3322 tmpl.csum_l2tun = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3323 /* Enable scattered packets support for this queue if necessary. */
3324 if ((dev->data->dev_conf.rxmode.jumbo_frame) &&
3325 (dev->data->dev_conf.rxmode.max_rx_pkt_len >
3326 (tmpl.mb_len - RTE_PKTMBUF_HEADROOM))) {
3328 desc /= MLX4_PMD_SGE_WR_N;
3330 DEBUG("%p: %s scattered packets support (%u WRs)",
3331 (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc);
3332 /* Use the entire RX mempool as the memory region. */
3333 tmpl.mr = ibv_reg_mr(priv->pd,
3334 (void *)mp->elt_va_start,
3335 (mp->elt_va_end - mp->elt_va_start),
3336 (IBV_ACCESS_LOCAL_WRITE |
3337 IBV_ACCESS_REMOTE_WRITE));
3338 if (tmpl.mr == NULL) {
3340 ERROR("%p: MR creation failure: %s",
3341 (void *)dev, strerror(ret));
3345 attr.rd = (struct ibv_exp_res_domain_init_attr){
3346 .comp_mask = (IBV_EXP_RES_DOMAIN_THREAD_MODEL |
3347 IBV_EXP_RES_DOMAIN_MSG_MODEL),
3348 .thread_model = IBV_EXP_THREAD_SINGLE,
3349 .msg_model = IBV_EXP_MSG_HIGH_BW,
3351 tmpl.rd = ibv_exp_create_res_domain(priv->ctx, &attr.rd);
3352 if (tmpl.rd == NULL) {
3354 ERROR("%p: RD creation failure: %s",
3355 (void *)dev, strerror(ret));
3358 attr.cq = (struct ibv_exp_cq_init_attr){
3359 .comp_mask = IBV_EXP_CQ_INIT_ATTR_RES_DOMAIN,
3360 .res_domain = tmpl.rd,
3362 tmpl.cq = ibv_exp_create_cq(priv->ctx, desc, NULL, NULL, 0, &attr.cq);
3363 if (tmpl.cq == NULL) {
3365 ERROR("%p: CQ creation failure: %s",
3366 (void *)dev, strerror(ret));
3369 DEBUG("priv->device_attr.max_qp_wr is %d",
3370 priv->device_attr.max_qp_wr);
3371 DEBUG("priv->device_attr.max_sge is %d",
3372 priv->device_attr.max_sge);
3375 tmpl.qp = rxq_setup_qp_rss(priv, tmpl.cq, desc, parent,
3378 #endif /* RSS_SUPPORT */
3379 tmpl.qp = rxq_setup_qp(priv, tmpl.cq, desc, tmpl.rd);
3380 if (tmpl.qp == NULL) {
3381 ret = (errno ? errno : EINVAL);
3382 ERROR("%p: QP creation failure: %s",
3383 (void *)dev, strerror(ret));
3386 mod = (struct ibv_exp_qp_attr){
3387 /* Move the QP to this state. */
3388 .qp_state = IBV_QPS_INIT,
3389 /* Primary port number. */
3390 .port_num = priv->port
3392 ret = ibv_exp_modify_qp(tmpl.qp, &mod,
3395 (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
3396 #endif /* RSS_SUPPORT */
3399 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
3400 (void *)dev, strerror(ret));
3403 if ((parent) || (!priv->rss)) {
3404 /* Configure MAC and broadcast addresses. */
3405 ret = rxq_mac_addrs_add(&tmpl);
3407 ERROR("%p: QP flow attachment failed: %s",
3408 (void *)dev, strerror(ret));
3412 /* Allocate descriptors for RX queues, except for the RSS parent. */
3416 ret = rxq_alloc_elts_sp(&tmpl, desc, NULL);
3418 ret = rxq_alloc_elts(&tmpl, desc, NULL);
3420 ERROR("%p: RXQ allocation failed: %s",
3421 (void *)dev, strerror(ret));
3424 ret = ibv_post_recv(tmpl.qp,
3426 &(*tmpl.elts.sp)[0].wr :
3427 &(*tmpl.elts.no_sp)[0].wr),
3430 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
3437 mod = (struct ibv_exp_qp_attr){
3438 .qp_state = IBV_QPS_RTR
3440 ret = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3442 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
3443 (void *)dev, strerror(ret));
3447 tmpl.port_id = dev->data->port_id;
3448 DEBUG("%p: RTE port ID: %u", (void *)rxq, tmpl.port_id);
3449 attr.params = (struct ibv_exp_query_intf_params){
3450 .intf_scope = IBV_EXP_INTF_GLOBAL,
3451 .intf = IBV_EXP_INTF_CQ,
3454 tmpl.if_cq = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
3455 if (tmpl.if_cq == NULL) {
3456 ERROR("%p: CQ interface family query failed with status %d",
3457 (void *)dev, status);
3460 attr.params = (struct ibv_exp_query_intf_params){
3461 .intf_scope = IBV_EXP_INTF_GLOBAL,
3462 .intf = IBV_EXP_INTF_QP_BURST,
3465 tmpl.if_qp = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
3466 if (tmpl.if_qp == NULL) {
3467 ERROR("%p: QP interface family query failed with status %d",
3468 (void *)dev, status);
3471 /* Clean up rxq in case we're reinitializing it. */
3472 DEBUG("%p: cleaning-up old rxq just in case", (void *)rxq);
3475 DEBUG("%p: rxq updated with %p", (void *)rxq, (void *)&tmpl);
3485 * DPDK callback to configure a RX queue.
3488 * Pointer to Ethernet device structure.
3492 * Number of descriptors to configure in queue.
3494 * NUMA socket on which memory must be allocated.
3496 * Thresholds parameters.
3498 * Memory pool for buffer allocations.
3501 * 0 on success, negative errno value on failure.
3504 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
3505 unsigned int socket, const struct rte_eth_rxconf *conf,
3506 struct rte_mempool *mp)
3508 struct priv *priv = dev->data->dev_private;
3509 struct rxq *rxq = (*priv->rxqs)[idx];
3513 DEBUG("%p: configuring queue %u for %u descriptors",
3514 (void *)dev, idx, desc);
3515 if (idx >= priv->rxqs_n) {
3516 ERROR("%p: queue index out of range (%u >= %u)",
3517 (void *)dev, idx, priv->rxqs_n);
3522 DEBUG("%p: reusing already allocated queue index %u (%p)",
3523 (void *)dev, idx, (void *)rxq);
3524 if (priv->started) {
3528 (*priv->rxqs)[idx] = NULL;
3531 rxq = rte_calloc_socket("RXQ", 1, sizeof(*rxq), 0, socket);
3533 ERROR("%p: unable to allocate queue index %u",
3539 ret = rxq_setup(dev, rxq, desc, socket, conf, mp);
3543 rxq->stats.idx = idx;
3544 DEBUG("%p: adding RX queue %p to list",
3545 (void *)dev, (void *)rxq);
3546 (*priv->rxqs)[idx] = rxq;
3547 /* Update receive callback. */
3549 dev->rx_pkt_burst = mlx4_rx_burst_sp;
3551 dev->rx_pkt_burst = mlx4_rx_burst;
3558 * DPDK callback to release a RX queue.
3561 * Generic RX queue pointer.
3564 mlx4_rx_queue_release(void *dpdk_rxq)
3566 struct rxq *rxq = (struct rxq *)dpdk_rxq;
3574 assert(rxq != &priv->rxq_parent);
3575 for (i = 0; (i != priv->rxqs_n); ++i)
3576 if ((*priv->rxqs)[i] == rxq) {
3577 DEBUG("%p: removing RX queue %p from list",
3578 (void *)priv->dev, (void *)rxq);
3579 (*priv->rxqs)[i] = NULL;
3588 * DPDK callback to start the device.
3590 * Simulate device start by attaching all configured flows.
3593 * Pointer to Ethernet device structure.
3596 * 0 on success, negative errno value on failure.
3599 mlx4_dev_start(struct rte_eth_dev *dev)
3601 struct priv *priv = dev->data->dev_private;
3607 if (priv->started) {
3611 DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
3614 rxq = &priv->rxq_parent;
3617 rxq = (*priv->rxqs)[0];
3620 /* Iterate only once when RSS is enabled. */
3624 /* Ignore nonexistent RX queues. */
3627 ret = rxq_mac_addrs_add(rxq);
3628 if (!ret && priv->promisc)
3629 ret = rxq_promiscuous_enable(rxq);
3630 if (!ret && priv->allmulti)
3631 ret = rxq_allmulticast_enable(rxq);
3634 WARN("%p: QP flow attachment failed: %s",
3635 (void *)dev, strerror(ret));
3638 rxq = (*priv->rxqs)[--i];
3640 rxq_allmulticast_disable(rxq);
3641 rxq_promiscuous_disable(rxq);
3642 rxq_mac_addrs_del(rxq);
3647 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
3653 * DPDK callback to stop the device.
3655 * Simulate device stop by detaching all configured flows.
3658 * Pointer to Ethernet device structure.
3661 mlx4_dev_stop(struct rte_eth_dev *dev)
3663 struct priv *priv = dev->data->dev_private;
3669 if (!priv->started) {
3673 DEBUG("%p: detaching flows from all RX queues", (void *)dev);
3676 rxq = &priv->rxq_parent;
3679 rxq = (*priv->rxqs)[0];
3682 /* Iterate only once when RSS is enabled. */
3684 /* Ignore nonexistent RX queues. */
3687 rxq_allmulticast_disable(rxq);
3688 rxq_promiscuous_disable(rxq);
3689 rxq_mac_addrs_del(rxq);
3690 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
3695 * Dummy DPDK callback for TX.
3697 * This function is used to temporarily replace the real callback during
3698 * unsafe control operations on the queue, or in case of error.
3701 * Generic pointer to TX queue structure.
3703 * Packets to transmit.
3705 * Number of packets in array.
3708 * Number of packets successfully transmitted (<= pkts_n).
3711 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
3720 * Dummy DPDK callback for RX.
3722 * This function is used to temporarily replace the real callback during
3723 * unsafe control operations on the queue, or in case of error.
3726 * Generic pointer to RX queue structure.
3728 * Array to store received packets.
3730 * Maximum number of packets in array.
3733 * Number of packets successfully received (<= pkts_n).
3736 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
3745 * DPDK callback to close the device.
3747 * Destroy all queues and objects, free memory.
3750 * Pointer to Ethernet device structure.
3753 mlx4_dev_close(struct rte_eth_dev *dev)
3755 struct priv *priv = dev->data->dev_private;
3760 DEBUG("%p: closing device \"%s\"",
3762 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
3763 /* Prevent crashes when queues are still in use. This is unfortunately
3764 * still required for DPDK 1.3 because some programs (such as testpmd)
3765 * never release them before closing the device. */
3766 dev->rx_pkt_burst = removed_rx_burst;
3767 dev->tx_pkt_burst = removed_tx_burst;
3768 if (priv->rxqs != NULL) {
3769 /* XXX race condition if mlx4_rx_burst() is still running. */
3771 for (i = 0; (i != priv->rxqs_n); ++i) {
3772 tmp = (*priv->rxqs)[i];
3775 (*priv->rxqs)[i] = NULL;
3782 if (priv->txqs != NULL) {
3783 /* XXX race condition if mlx4_tx_burst() is still running. */
3785 for (i = 0; (i != priv->txqs_n); ++i) {
3786 tmp = (*priv->txqs)[i];
3789 (*priv->txqs)[i] = NULL;
3797 rxq_cleanup(&priv->rxq_parent);
3798 if (priv->pd != NULL) {
3799 assert(priv->ctx != NULL);
3800 claim_zero(ibv_dealloc_pd(priv->pd));
3801 claim_zero(ibv_close_device(priv->ctx));
3803 assert(priv->ctx == NULL);
3805 memset(priv, 0, sizeof(*priv));
3809 * DPDK callback to get information about the device.
3812 * Pointer to Ethernet device structure.
3814 * Info structure output buffer.
3817 mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
3819 struct priv *priv = dev->data->dev_private;
3823 /* FIXME: we should ask the device for these values. */
3824 info->min_rx_bufsize = 32;
3825 info->max_rx_pktlen = 65536;
3827 * Since we need one CQ per QP, the limit is the minimum number
3828 * between the two values.
3830 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
3831 priv->device_attr.max_qp : priv->device_attr.max_cq);
3832 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
3835 info->max_rx_queues = max;
3836 info->max_tx_queues = max;
3837 info->max_mac_addrs = elemof(priv->mac);
3838 info->rx_offload_capa =
3840 (DEV_RX_OFFLOAD_IPV4_CKSUM |
3841 DEV_RX_OFFLOAD_UDP_CKSUM |
3842 DEV_RX_OFFLOAD_TCP_CKSUM) :
3844 info->tx_offload_capa =
3846 (DEV_TX_OFFLOAD_IPV4_CKSUM |
3847 DEV_TX_OFFLOAD_UDP_CKSUM |
3848 DEV_TX_OFFLOAD_TCP_CKSUM) :
3854 * DPDK callback to get device statistics.
3857 * Pointer to Ethernet device structure.
3859 * Stats structure output buffer.
3862 mlx4_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
3864 struct priv *priv = dev->data->dev_private;
3865 struct rte_eth_stats tmp = {0};
3870 /* Add software counters. */
3871 for (i = 0; (i != priv->rxqs_n); ++i) {
3872 struct rxq *rxq = (*priv->rxqs)[i];
3876 idx = rxq->stats.idx;
3877 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
3878 #ifdef MLX4_PMD_SOFT_COUNTERS
3879 tmp.q_ipackets[idx] += rxq->stats.ipackets;
3880 tmp.q_ibytes[idx] += rxq->stats.ibytes;
3882 tmp.q_errors[idx] += (rxq->stats.idropped +
3883 rxq->stats.rx_nombuf);
3885 #ifdef MLX4_PMD_SOFT_COUNTERS
3886 tmp.ipackets += rxq->stats.ipackets;
3887 tmp.ibytes += rxq->stats.ibytes;
3889 tmp.ierrors += rxq->stats.idropped;
3890 tmp.rx_nombuf += rxq->stats.rx_nombuf;
3892 for (i = 0; (i != priv->txqs_n); ++i) {
3893 struct txq *txq = (*priv->txqs)[i];
3897 idx = txq->stats.idx;
3898 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
3899 #ifdef MLX4_PMD_SOFT_COUNTERS
3900 tmp.q_opackets[idx] += txq->stats.opackets;
3901 tmp.q_obytes[idx] += txq->stats.obytes;
3903 tmp.q_errors[idx] += txq->stats.odropped;
3905 #ifdef MLX4_PMD_SOFT_COUNTERS
3906 tmp.opackets += txq->stats.opackets;
3907 tmp.obytes += txq->stats.obytes;
3909 tmp.oerrors += txq->stats.odropped;
3911 #ifndef MLX4_PMD_SOFT_COUNTERS
3912 /* FIXME: retrieve and add hardware counters. */
3919 * DPDK callback to clear device statistics.
3922 * Pointer to Ethernet device structure.
3925 mlx4_stats_reset(struct rte_eth_dev *dev)
3927 struct priv *priv = dev->data->dev_private;
3932 for (i = 0; (i != priv->rxqs_n); ++i) {
3933 if ((*priv->rxqs)[i] == NULL)
3935 idx = (*priv->rxqs)[i]->stats.idx;
3936 (*priv->rxqs)[i]->stats =
3937 (struct mlx4_rxq_stats){ .idx = idx };
3939 for (i = 0; (i != priv->txqs_n); ++i) {
3940 if ((*priv->txqs)[i] == NULL)
3942 idx = (*priv->rxqs)[i]->stats.idx;
3943 (*priv->txqs)[i]->stats =
3944 (struct mlx4_txq_stats){ .idx = idx };
3946 #ifndef MLX4_PMD_SOFT_COUNTERS
3947 /* FIXME: reset hardware counters. */
3953 * DPDK callback to remove a MAC address.
3956 * Pointer to Ethernet device structure.
3958 * MAC address index.
3961 mlx4_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
3963 struct priv *priv = dev->data->dev_private;
3966 DEBUG("%p: removing MAC address from index %" PRIu32,
3967 (void *)dev, index);
3968 if (index >= MLX4_MAX_MAC_ADDRESSES)
3970 /* Refuse to remove the broadcast address, this one is special. */
3971 if (!memcmp(priv->mac[index].addr_bytes, "\xff\xff\xff\xff\xff\xff",
3974 priv_mac_addr_del(priv, index);
3980 * DPDK callback to add a MAC address.
3983 * Pointer to Ethernet device structure.
3985 * MAC address to register.
3987 * MAC address index.
3989 * VMDq pool index to associate address with (ignored).
3992 mlx4_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
3993 uint32_t index, uint32_t vmdq)
3995 struct priv *priv = dev->data->dev_private;
3999 DEBUG("%p: adding MAC address at index %" PRIu32,
4000 (void *)dev, index);
4001 if (index >= MLX4_MAX_MAC_ADDRESSES)
4003 /* Refuse to add the broadcast address, this one is special. */
4004 if (!memcmp(mac_addr->addr_bytes, "\xff\xff\xff\xff\xff\xff",
4007 priv_mac_addr_add(priv, index,
4008 (const uint8_t (*)[ETHER_ADDR_LEN])
4009 mac_addr->addr_bytes);
4015 * DPDK callback to enable promiscuous mode.
4018 * Pointer to Ethernet device structure.
4021 mlx4_promiscuous_enable(struct rte_eth_dev *dev)
4023 struct priv *priv = dev->data->dev_private;
4028 if (priv->promisc) {
4032 /* If device isn't started, this is all we need to do. */
4036 ret = rxq_promiscuous_enable(&priv->rxq_parent);
4043 for (i = 0; (i != priv->rxqs_n); ++i) {
4044 if ((*priv->rxqs)[i] == NULL)
4046 ret = rxq_promiscuous_enable((*priv->rxqs)[i]);
4049 /* Failure, rollback. */
4051 if ((*priv->rxqs)[--i] != NULL)
4052 rxq_promiscuous_disable((*priv->rxqs)[i]);
4062 * DPDK callback to disable promiscuous mode.
4065 * Pointer to Ethernet device structure.
4068 mlx4_promiscuous_disable(struct rte_eth_dev *dev)
4070 struct priv *priv = dev->data->dev_private;
4074 if (!priv->promisc) {
4079 rxq_promiscuous_disable(&priv->rxq_parent);
4082 for (i = 0; (i != priv->rxqs_n); ++i)
4083 if ((*priv->rxqs)[i] != NULL)
4084 rxq_promiscuous_disable((*priv->rxqs)[i]);
4091 * DPDK callback to enable allmulti mode.
4094 * Pointer to Ethernet device structure.
4097 mlx4_allmulticast_enable(struct rte_eth_dev *dev)
4099 struct priv *priv = dev->data->dev_private;
4104 if (priv->allmulti) {
4108 /* If device isn't started, this is all we need to do. */
4112 ret = rxq_allmulticast_enable(&priv->rxq_parent);
4119 for (i = 0; (i != priv->rxqs_n); ++i) {
4120 if ((*priv->rxqs)[i] == NULL)
4122 ret = rxq_allmulticast_enable((*priv->rxqs)[i]);
4125 /* Failure, rollback. */
4127 if ((*priv->rxqs)[--i] != NULL)
4128 rxq_allmulticast_disable((*priv->rxqs)[i]);
4138 * DPDK callback to disable allmulti mode.
4141 * Pointer to Ethernet device structure.
4144 mlx4_allmulticast_disable(struct rte_eth_dev *dev)
4146 struct priv *priv = dev->data->dev_private;
4150 if (!priv->allmulti) {
4155 rxq_allmulticast_disable(&priv->rxq_parent);
4158 for (i = 0; (i != priv->rxqs_n); ++i)
4159 if ((*priv->rxqs)[i] != NULL)
4160 rxq_allmulticast_disable((*priv->rxqs)[i]);
4167 * DPDK callback to retrieve physical link information (unlocked version).
4170 * Pointer to Ethernet device structure.
4171 * @param wait_to_complete
4172 * Wait for request completion (ignored).
4175 mlx4_link_update_unlocked(struct rte_eth_dev *dev, int wait_to_complete)
4177 struct priv *priv = dev->data->dev_private;
4178 struct ethtool_cmd edata = {
4182 struct rte_eth_link dev_link;
4185 (void)wait_to_complete;
4186 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
4187 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
4190 memset(&dev_link, 0, sizeof(dev_link));
4191 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
4192 (ifr.ifr_flags & IFF_RUNNING));
4193 ifr.ifr_data = &edata;
4194 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4195 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
4199 link_speed = ethtool_cmd_speed(&edata);
4200 if (link_speed == -1)
4201 dev_link.link_speed = 0;
4203 dev_link.link_speed = link_speed;
4204 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
4205 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
4206 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
4207 /* Link status changed. */
4208 dev->data->dev_link = dev_link;
4211 /* Link status is still the same. */
4216 * DPDK callback to retrieve physical link information.
4219 * Pointer to Ethernet device structure.
4220 * @param wait_to_complete
4221 * Wait for request completion (ignored).
4224 mlx4_link_update(struct rte_eth_dev *dev, int wait_to_complete)
4226 struct priv *priv = dev->data->dev_private;
4230 ret = mlx4_link_update_unlocked(dev, wait_to_complete);
4236 * DPDK callback to change the MTU.
4238 * Setting the MTU affects hardware MRU (packets larger than the MTU cannot be
4239 * received). Use this as a hint to enable/disable scattered packets support
4240 * and improve performance when not needed.
4241 * Since failure is not an option, reconfiguring queues on the fly is not
4245 * Pointer to Ethernet device structure.
4250 * 0 on success, negative errno value on failure.
4253 mlx4_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
4255 struct priv *priv = dev->data->dev_private;
4258 uint16_t (*rx_func)(void *, struct rte_mbuf **, uint16_t) =
4262 /* Set kernel interface MTU first. */
4263 if (priv_set_mtu(priv, mtu)) {
4265 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
4269 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
4271 /* Temporarily replace RX handler with a fake one, assuming it has not
4272 * been copied elsewhere. */
4273 dev->rx_pkt_burst = removed_rx_burst;
4274 /* Make sure everyone has left mlx4_rx_burst() and uses
4275 * removed_rx_burst() instead. */
4278 /* Reconfigure each RX queue. */
4279 for (i = 0; (i != priv->rxqs_n); ++i) {
4280 struct rxq *rxq = (*priv->rxqs)[i];
4281 unsigned int max_frame_len;
4286 /* Calculate new maximum frame length according to MTU and
4287 * toggle scattered support (sp) if necessary. */
4288 max_frame_len = (priv->mtu + ETHER_HDR_LEN +
4289 (ETHER_MAX_VLAN_FRAME_LEN - ETHER_MAX_LEN));
4290 sp = (max_frame_len > (rxq->mb_len - RTE_PKTMBUF_HEADROOM));
4291 /* Provide new values to rxq_setup(). */
4292 dev->data->dev_conf.rxmode.jumbo_frame = sp;
4293 dev->data->dev_conf.rxmode.max_rx_pkt_len = max_frame_len;
4294 ret = rxq_rehash(dev, rxq);
4296 /* Force SP RX if that queue requires it and abort. */
4298 rx_func = mlx4_rx_burst_sp;
4301 /* Reenable non-RSS queue attributes. No need to check
4302 * for errors at this stage. */
4304 rxq_mac_addrs_add(rxq);
4306 rxq_promiscuous_enable(rxq);
4308 rxq_allmulticast_enable(rxq);
4310 /* Scattered burst function takes priority. */
4312 rx_func = mlx4_rx_burst_sp;
4314 /* Burst functions can now be called again. */
4316 dev->rx_pkt_burst = rx_func;
4324 * DPDK callback to get flow control status.
4327 * Pointer to Ethernet device structure.
4328 * @param[out] fc_conf
4329 * Flow control output buffer.
4332 * 0 on success, negative errno value on failure.
4335 mlx4_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4337 struct priv *priv = dev->data->dev_private;
4339 struct ethtool_pauseparam ethpause = {
4340 .cmd = ETHTOOL_GPAUSEPARAM
4344 ifr.ifr_data = ðpause;
4346 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4348 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
4354 fc_conf->autoneg = ethpause.autoneg;
4355 if (ethpause.rx_pause && ethpause.tx_pause)
4356 fc_conf->mode = RTE_FC_FULL;
4357 else if (ethpause.rx_pause)
4358 fc_conf->mode = RTE_FC_RX_PAUSE;
4359 else if (ethpause.tx_pause)
4360 fc_conf->mode = RTE_FC_TX_PAUSE;
4362 fc_conf->mode = RTE_FC_NONE;
4372 * DPDK callback to modify flow control parameters.
4375 * Pointer to Ethernet device structure.
4376 * @param[in] fc_conf
4377 * Flow control parameters.
4380 * 0 on success, negative errno value on failure.
4383 mlx4_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4385 struct priv *priv = dev->data->dev_private;
4387 struct ethtool_pauseparam ethpause = {
4388 .cmd = ETHTOOL_SPAUSEPARAM
4392 ifr.ifr_data = ðpause;
4393 ethpause.autoneg = fc_conf->autoneg;
4394 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4395 (fc_conf->mode & RTE_FC_RX_PAUSE))
4396 ethpause.rx_pause = 1;
4398 ethpause.rx_pause = 0;
4400 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4401 (fc_conf->mode & RTE_FC_TX_PAUSE))
4402 ethpause.tx_pause = 1;
4404 ethpause.tx_pause = 0;
4407 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4409 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
4423 * Configure a VLAN filter.
4426 * Pointer to Ethernet device structure.
4428 * VLAN ID to filter.
4433 * 0 on success, errno value on failure.
4436 vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
4438 struct priv *priv = dev->data->dev_private;
4440 unsigned int j = -1;
4442 DEBUG("%p: %s VLAN filter ID %" PRIu16,
4443 (void *)dev, (on ? "enable" : "disable"), vlan_id);
4444 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
4445 if (!priv->vlan_filter[i].enabled) {
4446 /* Unused index, remember it. */
4450 if (priv->vlan_filter[i].id != vlan_id)
4452 /* This VLAN ID is already known, use its index. */
4456 /* Check if there's room for another VLAN filter. */
4457 if (j == (unsigned int)-1)
4460 * VLAN filters apply to all configured MAC addresses, flow
4461 * specifications must be reconfigured accordingly.
4463 priv->vlan_filter[j].id = vlan_id;
4464 if ((on) && (!priv->vlan_filter[j].enabled)) {
4466 * Filter is disabled, enable it.
4467 * Rehashing flows in all RX queues is necessary.
4470 rxq_mac_addrs_del(&priv->rxq_parent);
4472 for (i = 0; (i != priv->rxqs_n); ++i)
4473 if ((*priv->rxqs)[i] != NULL)
4474 rxq_mac_addrs_del((*priv->rxqs)[i]);
4475 priv->vlan_filter[j].enabled = 1;
4476 if (priv->started) {
4478 rxq_mac_addrs_add(&priv->rxq_parent);
4480 for (i = 0; (i != priv->rxqs_n); ++i) {
4481 if ((*priv->rxqs)[i] == NULL)
4483 rxq_mac_addrs_add((*priv->rxqs)[i]);
4486 } else if ((!on) && (priv->vlan_filter[j].enabled)) {
4488 * Filter is enabled, disable it.
4489 * Rehashing flows in all RX queues is necessary.
4492 rxq_mac_addrs_del(&priv->rxq_parent);
4494 for (i = 0; (i != priv->rxqs_n); ++i)
4495 if ((*priv->rxqs)[i] != NULL)
4496 rxq_mac_addrs_del((*priv->rxqs)[i]);
4497 priv->vlan_filter[j].enabled = 0;
4498 if (priv->started) {
4500 rxq_mac_addrs_add(&priv->rxq_parent);
4502 for (i = 0; (i != priv->rxqs_n); ++i) {
4503 if ((*priv->rxqs)[i] == NULL)
4505 rxq_mac_addrs_add((*priv->rxqs)[i]);
4513 * DPDK callback to configure a VLAN filter.
4516 * Pointer to Ethernet device structure.
4518 * VLAN ID to filter.
4523 * 0 on success, negative errno value on failure.
4526 mlx4_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
4528 struct priv *priv = dev->data->dev_private;
4532 ret = vlan_filter_set(dev, vlan_id, on);
4538 static const struct eth_dev_ops mlx4_dev_ops = {
4539 .dev_configure = mlx4_dev_configure,
4540 .dev_start = mlx4_dev_start,
4541 .dev_stop = mlx4_dev_stop,
4542 .dev_close = mlx4_dev_close,
4543 .promiscuous_enable = mlx4_promiscuous_enable,
4544 .promiscuous_disable = mlx4_promiscuous_disable,
4545 .allmulticast_enable = mlx4_allmulticast_enable,
4546 .allmulticast_disable = mlx4_allmulticast_disable,
4547 .link_update = mlx4_link_update,
4548 .stats_get = mlx4_stats_get,
4549 .stats_reset = mlx4_stats_reset,
4550 .queue_stats_mapping_set = NULL,
4551 .dev_infos_get = mlx4_dev_infos_get,
4552 .vlan_filter_set = mlx4_vlan_filter_set,
4553 .vlan_tpid_set = NULL,
4554 .vlan_strip_queue_set = NULL,
4555 .vlan_offload_set = NULL,
4556 .rx_queue_setup = mlx4_rx_queue_setup,
4557 .tx_queue_setup = mlx4_tx_queue_setup,
4558 .rx_queue_release = mlx4_rx_queue_release,
4559 .tx_queue_release = mlx4_tx_queue_release,
4561 .dev_led_off = NULL,
4562 .flow_ctrl_get = mlx4_dev_get_flow_ctrl,
4563 .flow_ctrl_set = mlx4_dev_set_flow_ctrl,
4564 .priority_flow_ctrl_set = NULL,
4565 .mac_addr_remove = mlx4_mac_addr_remove,
4566 .mac_addr_add = mlx4_mac_addr_add,
4567 .mtu_set = mlx4_dev_set_mtu,
4568 .udp_tunnel_add = NULL,
4569 .udp_tunnel_del = NULL,
4573 * Get PCI information from struct ibv_device.
4576 * Pointer to Ethernet device structure.
4577 * @param[out] pci_addr
4578 * PCI bus address output buffer.
4581 * 0 on success, -1 on failure and errno is set.
4584 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
4585 struct rte_pci_addr *pci_addr)
4589 MKSTR(path, "%s/device/uevent", device->ibdev_path);
4591 file = fopen(path, "rb");
4594 while (fgets(line, sizeof(line), file) == line) {
4595 size_t len = strlen(line);
4598 /* Truncate long lines. */
4599 if (len == (sizeof(line) - 1))
4600 while (line[(len - 1)] != '\n') {
4604 line[(len - 1)] = ret;
4606 /* Extract information. */
4609 "%" SCNx16 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
4613 &pci_addr->function) == 4) {
4623 * Get MAC address by querying netdevice.
4626 * struct priv for the requested device.
4628 * MAC address output buffer.
4631 * 0 on success, -1 on failure and errno is set.
4634 priv_get_mac(struct priv *priv, uint8_t (*mac)[ETHER_ADDR_LEN])
4636 struct ifreq request;
4638 if (priv_ifreq(priv, SIOCGIFHWADDR, &request))
4640 memcpy(mac, request.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
4644 /* Support up to 32 adapters. */
4646 struct rte_pci_addr pci_addr; /* associated PCI address */
4647 uint32_t ports; /* physical ports bitfield. */
4651 * Get device index in mlx4_dev[] from PCI bus address.
4653 * @param[in] pci_addr
4654 * PCI bus address to look for.
4657 * mlx4_dev[] index on success, -1 on failure.
4660 mlx4_dev_idx(struct rte_pci_addr *pci_addr)
4665 assert(pci_addr != NULL);
4666 for (i = 0; (i != elemof(mlx4_dev)); ++i) {
4667 if ((mlx4_dev[i].pci_addr.domain == pci_addr->domain) &&
4668 (mlx4_dev[i].pci_addr.bus == pci_addr->bus) &&
4669 (mlx4_dev[i].pci_addr.devid == pci_addr->devid) &&
4670 (mlx4_dev[i].pci_addr.function == pci_addr->function))
4672 if ((mlx4_dev[i].ports == 0) && (ret == -1))
4679 * Retrieve integer value from environment variable.
4682 * Environment variable name.
4685 * Integer value, 0 if the variable is not set.
4688 mlx4_getenv_int(const char *name)
4690 const char *val = getenv(name);
4697 static struct eth_driver mlx4_driver;
4700 * DPDK callback to register a PCI device.
4702 * This function creates an Ethernet device for each port of a given
4705 * @param[in] pci_drv
4706 * PCI driver structure (mlx4_driver).
4707 * @param[in] pci_dev
4708 * PCI device information.
4711 * 0 on success, negative errno value on failure.
4714 mlx4_pci_devinit(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
4716 struct ibv_device **list;
4717 struct ibv_device *ibv_dev;
4719 struct ibv_context *attr_ctx = NULL;
4720 struct ibv_device_attr device_attr;
4726 assert(pci_drv == &mlx4_driver.pci_drv);
4727 /* Get mlx4_dev[] index. */
4728 idx = mlx4_dev_idx(&pci_dev->addr);
4730 ERROR("this driver cannot support any more adapters");
4733 DEBUG("using driver device index %d", idx);
4735 /* Save PCI address. */
4736 mlx4_dev[idx].pci_addr = pci_dev->addr;
4737 list = ibv_get_device_list(&i);
4740 if (errno == ENOSYS) {
4741 WARN("cannot list devices, is ib_uverbs loaded?");
4748 * For each listed device, check related sysfs entry against
4749 * the provided PCI ID.
4752 struct rte_pci_addr pci_addr;
4755 DEBUG("checking device \"%s\"", list[i]->name);
4756 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
4758 if ((pci_dev->addr.domain != pci_addr.domain) ||
4759 (pci_dev->addr.bus != pci_addr.bus) ||
4760 (pci_dev->addr.devid != pci_addr.devid) ||
4761 (pci_dev->addr.function != pci_addr.function))
4763 vf = (pci_dev->id.device_id ==
4764 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
4765 INFO("PCI information matches, using device \"%s\" (VF: %s)",
4766 list[i]->name, (vf ? "true" : "false"));
4767 attr_ctx = ibv_open_device(list[i]);
4771 if (attr_ctx == NULL) {
4772 ibv_free_device_list(list);
4775 WARN("cannot access device, is mlx4_ib loaded?");
4778 WARN("cannot use device, are drivers up to date?");
4786 DEBUG("device opened");
4787 if (ibv_query_device(attr_ctx, &device_attr))
4789 INFO("%u port(s) detected", device_attr.phys_port_cnt);
4791 for (i = 0; i < device_attr.phys_port_cnt; i++) {
4792 uint32_t port = i + 1; /* ports are indexed from one */
4793 uint32_t test = (1 << i);
4794 struct ibv_context *ctx = NULL;
4795 struct ibv_port_attr port_attr;
4796 struct ibv_pd *pd = NULL;
4797 struct priv *priv = NULL;
4798 struct rte_eth_dev *eth_dev;
4799 #ifdef HAVE_EXP_QUERY_DEVICE
4800 struct ibv_exp_device_attr exp_device_attr;
4801 #endif /* HAVE_EXP_QUERY_DEVICE */
4802 struct ether_addr mac;
4804 #ifdef HAVE_EXP_QUERY_DEVICE
4805 exp_device_attr.comp_mask = IBV_EXP_DEVICE_ATTR_EXP_CAP_FLAGS;
4807 exp_device_attr.comp_mask |= IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ;
4808 #endif /* RSS_SUPPORT */
4809 #endif /* HAVE_EXP_QUERY_DEVICE */
4811 DEBUG("using port %u (%08" PRIx32 ")", port, test);
4813 ctx = ibv_open_device(ibv_dev);
4817 /* Check port status. */
4818 err = ibv_query_port(ctx, port, &port_attr);
4820 ERROR("port query failed: %s", strerror(err));
4823 if (port_attr.state != IBV_PORT_ACTIVE)
4824 WARN("bad state for port %d: \"%s\" (%d)",
4825 port, ibv_port_state_str(port_attr.state),
4828 /* Allocate protection domain. */
4829 pd = ibv_alloc_pd(ctx);
4831 ERROR("PD allocation failure");
4836 mlx4_dev[idx].ports |= test;
4838 /* from rte_ethdev.c */
4839 priv = rte_zmalloc("ethdev private structure",
4841 RTE_CACHE_LINE_SIZE);
4843 ERROR("priv allocation failure");
4849 priv->device_attr = device_attr;
4852 priv->mtu = ETHER_MTU;
4853 #ifdef HAVE_EXP_QUERY_DEVICE
4854 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
4855 ERROR("ibv_exp_query_device() failed");
4859 if ((exp_device_attr.exp_device_cap_flags &
4860 IBV_EXP_DEVICE_QPG) &&
4861 (exp_device_attr.exp_device_cap_flags &
4862 IBV_EXP_DEVICE_UD_RSS) &&
4863 (exp_device_attr.comp_mask &
4864 IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ) &&
4865 (exp_device_attr.max_rss_tbl_sz > 0)) {
4868 priv->max_rss_tbl_sz = exp_device_attr.max_rss_tbl_sz;
4872 priv->max_rss_tbl_sz = 0;
4874 priv->hw_tss = !!(exp_device_attr.exp_device_cap_flags &
4875 IBV_EXP_DEVICE_UD_TSS);
4876 DEBUG("device flags: %s%s%s",
4877 (priv->hw_qpg ? "IBV_DEVICE_QPG " : ""),
4878 (priv->hw_tss ? "IBV_DEVICE_TSS " : ""),
4879 (priv->hw_rss ? "IBV_DEVICE_RSS " : ""));
4881 DEBUG("maximum RSS indirection table size: %u",
4882 exp_device_attr.max_rss_tbl_sz);
4883 #endif /* RSS_SUPPORT */
4886 ((exp_device_attr.exp_device_cap_flags &
4887 IBV_EXP_DEVICE_RX_CSUM_TCP_UDP_PKT) &&
4888 (exp_device_attr.exp_device_cap_flags &
4889 IBV_EXP_DEVICE_RX_CSUM_IP_PKT));
4890 DEBUG("checksum offloading is %ssupported",
4891 (priv->hw_csum ? "" : "not "));
4893 priv->hw_csum_l2tun = !!(exp_device_attr.exp_device_cap_flags &
4894 IBV_EXP_DEVICE_VXLAN_SUPPORT);
4895 DEBUG("L2 tunnel checksum offloads are %ssupported",
4896 (priv->hw_csum_l2tun ? "" : "not "));
4899 priv->inl_recv_size = mlx4_getenv_int("MLX4_INLINE_RECV_SIZE");
4901 if (priv->inl_recv_size) {
4902 exp_device_attr.comp_mask =
4903 IBV_EXP_DEVICE_ATTR_INLINE_RECV_SZ;
4904 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
4905 INFO("Couldn't query device for inline-receive"
4907 priv->inl_recv_size = 0;
4909 if ((unsigned)exp_device_attr.inline_recv_sz <
4910 priv->inl_recv_size) {
4911 INFO("Max inline-receive (%d) <"
4912 " requested inline-receive (%u)",
4913 exp_device_attr.inline_recv_sz,
4914 priv->inl_recv_size);
4915 priv->inl_recv_size =
4916 exp_device_attr.inline_recv_sz;
4919 INFO("Set inline receive size to %u",
4920 priv->inl_recv_size);
4922 #endif /* INLINE_RECV */
4923 #endif /* HAVE_EXP_QUERY_DEVICE */
4925 (void)mlx4_getenv_int;
4927 /* Configure the first MAC address by default. */
4928 if (priv_get_mac(priv, &mac.addr_bytes)) {
4929 ERROR("cannot get MAC address, is mlx4_en loaded?"
4930 " (errno: %s)", strerror(errno));
4933 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
4935 mac.addr_bytes[0], mac.addr_bytes[1],
4936 mac.addr_bytes[2], mac.addr_bytes[3],
4937 mac.addr_bytes[4], mac.addr_bytes[5]);
4938 /* Register MAC and broadcast addresses. */
4939 claim_zero(priv_mac_addr_add(priv, 0,
4940 (const uint8_t (*)[ETHER_ADDR_LEN])
4942 claim_zero(priv_mac_addr_add(priv, 1,
4943 &(const uint8_t [ETHER_ADDR_LEN])
4944 { "\xff\xff\xff\xff\xff\xff" }));
4947 char ifname[IF_NAMESIZE];
4949 if (priv_get_ifname(priv, &ifname) == 0)
4950 DEBUG("port %u ifname is \"%s\"",
4951 priv->port, ifname);
4953 DEBUG("port %u ifname is unknown", priv->port);
4956 /* Get actual MTU if possible. */
4957 priv_get_mtu(priv, &priv->mtu);
4958 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
4960 /* from rte_ethdev.c */
4962 char name[RTE_ETH_NAME_MAX_LEN];
4964 snprintf(name, sizeof(name), "%s port %u",
4965 ibv_get_device_name(ibv_dev), port);
4966 eth_dev = rte_eth_dev_allocate(name, RTE_ETH_DEV_PCI);
4968 if (eth_dev == NULL) {
4969 ERROR("can not allocate rte ethdev");
4974 eth_dev->data->dev_private = priv;
4975 eth_dev->pci_dev = pci_dev;
4976 eth_dev->driver = &mlx4_driver;
4977 eth_dev->data->rx_mbuf_alloc_failed = 0;
4978 eth_dev->data->mtu = ETHER_MTU;
4980 priv->dev = eth_dev;
4981 eth_dev->dev_ops = &mlx4_dev_ops;
4982 eth_dev->data->mac_addrs = priv->mac;
4984 /* Bring Ethernet device up. */
4985 DEBUG("forcing Ethernet interface up");
4986 priv_set_flags(priv, ~IFF_UP, IFF_UP);
4992 claim_zero(ibv_dealloc_pd(pd));
4994 claim_zero(ibv_close_device(ctx));
4999 * XXX if something went wrong in the loop above, there is a resource
5000 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
5001 * long as the dpdk does not provide a way to deallocate a ethdev and a
5002 * way to enumerate the registered ethdevs to free the previous ones.
5005 /* no port found, complain */
5006 if (!mlx4_dev[idx].ports) {
5013 claim_zero(ibv_close_device(attr_ctx));
5015 ibv_free_device_list(list);
5020 static const struct rte_pci_id mlx4_pci_id_map[] = {
5022 .vendor_id = PCI_VENDOR_ID_MELLANOX,
5023 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3,
5024 .subsystem_vendor_id = PCI_ANY_ID,
5025 .subsystem_device_id = PCI_ANY_ID
5028 .vendor_id = PCI_VENDOR_ID_MELLANOX,
5029 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO,
5030 .subsystem_vendor_id = PCI_ANY_ID,
5031 .subsystem_device_id = PCI_ANY_ID
5034 .vendor_id = PCI_VENDOR_ID_MELLANOX,
5035 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3VF,
5036 .subsystem_vendor_id = PCI_ANY_ID,
5037 .subsystem_device_id = PCI_ANY_ID
5044 static struct eth_driver mlx4_driver = {
5046 .name = MLX4_DRIVER_NAME,
5047 .id_table = mlx4_pci_id_map,
5048 .devinit = mlx4_pci_devinit,
5050 .dev_private_size = sizeof(struct priv)
5054 * Driver initialization routine.
5057 rte_mlx4_pmd_init(const char *name, const char *args)
5062 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
5063 * huge pages. Calling ibv_fork_init() during init allows
5064 * applications to use fork() safely for purposes other than
5065 * using this PMD, which is not supported in forked processes.
5067 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
5069 rte_eal_pci_register(&mlx4_driver.pci_drv);
5073 static struct rte_driver rte_mlx4_driver = {
5075 .name = MLX4_DRIVER_NAME,
5076 .init = rte_mlx4_pmd_init,
5079 PMD_REGISTER_DRIVER(rte_mlx4_driver)