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.
1268 * FIXME: since PKT_TX_UDP_TUNNEL_PKT has been removed,
1269 * the outer packet type is unknown. All we know is
1270 * that the L2 header is of unusual length (not
1271 * ETHER_HDR_LEN with or without 802.1Q header). */
1272 if ((buf->l2_len != ETHER_HDR_LEN) &&
1273 (buf->l2_len != (ETHER_HDR_LEN + 4)))
1274 send_flags |= IBV_EXP_QP_BURST_TUNNEL;
1276 if (likely(segs == 1)) {
1281 /* Retrieve buffer information. */
1282 addr = rte_pktmbuf_mtod(buf, uintptr_t);
1283 length = DATA_LEN(buf);
1284 /* Retrieve Memory Region key for this memory pool. */
1285 lkey = txq_mp2mr(txq, buf->pool);
1286 if (unlikely(lkey == (uint32_t)-1)) {
1287 /* MR does not exist. */
1288 DEBUG("%p: unable to get MP <-> MR"
1289 " association", (void *)txq);
1290 /* Clean up TX element. */
1294 /* Update element. */
1297 rte_prefetch0((volatile void *)
1299 RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
1300 /* Put packet into send queue. */
1301 #if MLX4_PMD_MAX_INLINE > 0
1302 if (length <= txq->max_inline)
1303 err = txq->if_qp->send_pending_inline
1310 err = txq->if_qp->send_pending
1318 #ifdef MLX4_PMD_SOFT_COUNTERS
1319 sent_size += length;
1322 #if MLX4_PMD_SGE_WR_N > 1
1323 struct ibv_sge sges[MLX4_PMD_SGE_WR_N];
1324 struct tx_burst_sg_ret ret;
1326 ret = tx_burst_sg(txq, segs, elt, buf, elts_head,
1328 if (ret.length == (unsigned int)-1)
1330 RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
1331 /* Put SG list into send queue. */
1332 err = txq->if_qp->send_pending_sg_list
1339 #ifdef MLX4_PMD_SOFT_COUNTERS
1340 sent_size += ret.length;
1342 #else /* MLX4_PMD_SGE_WR_N > 1 */
1343 DEBUG("%p: TX scattered buffers support not"
1344 " compiled in", (void *)txq);
1346 #endif /* MLX4_PMD_SGE_WR_N > 1 */
1348 elts_head = elts_head_next;
1349 #ifdef MLX4_PMD_SOFT_COUNTERS
1350 /* Increment sent bytes counter. */
1351 txq->stats.obytes += sent_size;
1355 /* Take a shortcut if nothing must be sent. */
1356 if (unlikely(i == 0))
1358 #ifdef MLX4_PMD_SOFT_COUNTERS
1359 /* Increment sent packets counter. */
1360 txq->stats.opackets += i;
1362 /* Ring QP doorbell. */
1363 err = txq->if_qp->send_flush(txq->qp);
1364 if (unlikely(err)) {
1365 /* A nonzero value is not supposed to be returned.
1366 * Nothing can be done about it. */
1367 DEBUG("%p: send_flush() failed with error %d",
1370 txq->elts_head = elts_head;
1371 txq->elts_comp += elts_comp;
1372 txq->elts_comp_cd = elts_comp_cd;
1377 * Configure a TX queue.
1380 * Pointer to Ethernet device structure.
1382 * Pointer to TX queue structure.
1384 * Number of descriptors to configure in queue.
1386 * NUMA socket on which memory must be allocated.
1388 * Thresholds parameters.
1391 * 0 on success, errno value on failure.
1394 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
1395 unsigned int socket, const struct rte_eth_txconf *conf)
1397 struct priv *priv = dev->data->dev_private;
1403 struct ibv_exp_query_intf_params params;
1404 struct ibv_exp_qp_init_attr init;
1405 struct ibv_exp_res_domain_init_attr rd;
1406 struct ibv_exp_cq_init_attr cq;
1407 struct ibv_exp_qp_attr mod;
1409 enum ibv_exp_query_intf_status status;
1412 (void)conf; /* Thresholds configuration (ignored). */
1413 if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
1414 ERROR("%p: invalid number of TX descriptors (must be a"
1415 " multiple of %d)", (void *)dev, MLX4_PMD_SGE_WR_N);
1418 desc /= MLX4_PMD_SGE_WR_N;
1419 /* MRs will be registered in mp2mr[] later. */
1420 attr.rd = (struct ibv_exp_res_domain_init_attr){
1421 .comp_mask = (IBV_EXP_RES_DOMAIN_THREAD_MODEL |
1422 IBV_EXP_RES_DOMAIN_MSG_MODEL),
1423 .thread_model = IBV_EXP_THREAD_SINGLE,
1424 .msg_model = IBV_EXP_MSG_HIGH_BW,
1426 tmpl.rd = ibv_exp_create_res_domain(priv->ctx, &attr.rd);
1427 if (tmpl.rd == NULL) {
1429 ERROR("%p: RD creation failure: %s",
1430 (void *)dev, strerror(ret));
1433 attr.cq = (struct ibv_exp_cq_init_attr){
1434 .comp_mask = IBV_EXP_CQ_INIT_ATTR_RES_DOMAIN,
1435 .res_domain = tmpl.rd,
1437 tmpl.cq = ibv_exp_create_cq(priv->ctx, desc, NULL, NULL, 0, &attr.cq);
1438 if (tmpl.cq == NULL) {
1440 ERROR("%p: CQ creation failure: %s",
1441 (void *)dev, strerror(ret));
1444 DEBUG("priv->device_attr.max_qp_wr is %d",
1445 priv->device_attr.max_qp_wr);
1446 DEBUG("priv->device_attr.max_sge is %d",
1447 priv->device_attr.max_sge);
1448 attr.init = (struct ibv_exp_qp_init_attr){
1449 /* CQ to be associated with the send queue. */
1451 /* CQ to be associated with the receive queue. */
1454 /* Max number of outstanding WRs. */
1455 .max_send_wr = ((priv->device_attr.max_qp_wr < desc) ?
1456 priv->device_attr.max_qp_wr :
1458 /* Max number of scatter/gather elements in a WR. */
1459 .max_send_sge = ((priv->device_attr.max_sge <
1460 MLX4_PMD_SGE_WR_N) ?
1461 priv->device_attr.max_sge :
1463 #if MLX4_PMD_MAX_INLINE > 0
1464 .max_inline_data = MLX4_PMD_MAX_INLINE,
1467 .qp_type = IBV_QPT_RAW_PACKET,
1468 /* Do *NOT* enable this, completions events are managed per
1472 .res_domain = tmpl.rd,
1473 .comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
1474 IBV_EXP_QP_INIT_ATTR_RES_DOMAIN),
1476 tmpl.qp = ibv_exp_create_qp(priv->ctx, &attr.init);
1477 if (tmpl.qp == NULL) {
1478 ret = (errno ? errno : EINVAL);
1479 ERROR("%p: QP creation failure: %s",
1480 (void *)dev, strerror(ret));
1483 #if MLX4_PMD_MAX_INLINE > 0
1484 /* ibv_create_qp() updates this value. */
1485 tmpl.max_inline = attr.init.cap.max_inline_data;
1487 attr.mod = (struct ibv_exp_qp_attr){
1488 /* Move the QP to this state. */
1489 .qp_state = IBV_QPS_INIT,
1490 /* Primary port number. */
1491 .port_num = priv->port
1493 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod,
1494 (IBV_EXP_QP_STATE | IBV_EXP_QP_PORT));
1496 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
1497 (void *)dev, strerror(ret));
1500 ret = txq_alloc_elts(&tmpl, desc);
1502 ERROR("%p: TXQ allocation failed: %s",
1503 (void *)dev, strerror(ret));
1506 attr.mod = (struct ibv_exp_qp_attr){
1507 .qp_state = IBV_QPS_RTR
1509 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1511 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
1512 (void *)dev, strerror(ret));
1515 attr.mod.qp_state = IBV_QPS_RTS;
1516 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1518 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
1519 (void *)dev, strerror(ret));
1522 attr.params = (struct ibv_exp_query_intf_params){
1523 .intf_scope = IBV_EXP_INTF_GLOBAL,
1524 .intf = IBV_EXP_INTF_CQ,
1527 tmpl.if_cq = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
1528 if (tmpl.if_cq == NULL) {
1529 ERROR("%p: CQ interface family query failed with status %d",
1530 (void *)dev, status);
1533 attr.params = (struct ibv_exp_query_intf_params){
1534 .intf_scope = IBV_EXP_INTF_GLOBAL,
1535 .intf = IBV_EXP_INTF_QP_BURST,
1537 #ifdef HAVE_EXP_QP_BURST_CREATE_DISABLE_ETH_LOOPBACK
1538 /* MC loopback must be disabled when not using a VF. */
1541 IBV_EXP_QP_BURST_CREATE_DISABLE_ETH_LOOPBACK :
1545 tmpl.if_qp = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
1546 if (tmpl.if_qp == NULL) {
1547 ERROR("%p: QP interface family query failed with status %d",
1548 (void *)dev, status);
1551 /* Clean up txq in case we're reinitializing it. */
1552 DEBUG("%p: cleaning-up old txq just in case", (void *)txq);
1555 DEBUG("%p: txq updated with %p", (void *)txq, (void *)&tmpl);
1565 * DPDK callback to configure a TX queue.
1568 * Pointer to Ethernet device structure.
1572 * Number of descriptors to configure in queue.
1574 * NUMA socket on which memory must be allocated.
1576 * Thresholds parameters.
1579 * 0 on success, negative errno value on failure.
1582 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1583 unsigned int socket, const struct rte_eth_txconf *conf)
1585 struct priv *priv = dev->data->dev_private;
1586 struct txq *txq = (*priv->txqs)[idx];
1590 DEBUG("%p: configuring queue %u for %u descriptors",
1591 (void *)dev, idx, desc);
1592 if (idx >= priv->txqs_n) {
1593 ERROR("%p: queue index out of range (%u >= %u)",
1594 (void *)dev, idx, priv->txqs_n);
1599 DEBUG("%p: reusing already allocated queue index %u (%p)",
1600 (void *)dev, idx, (void *)txq);
1601 if (priv->started) {
1605 (*priv->txqs)[idx] = NULL;
1608 txq = rte_calloc_socket("TXQ", 1, sizeof(*txq), 0, socket);
1610 ERROR("%p: unable to allocate queue index %u",
1616 ret = txq_setup(dev, txq, desc, socket, conf);
1620 txq->stats.idx = idx;
1621 DEBUG("%p: adding TX queue %p to list",
1622 (void *)dev, (void *)txq);
1623 (*priv->txqs)[idx] = txq;
1624 /* Update send callback. */
1625 dev->tx_pkt_burst = mlx4_tx_burst;
1632 * DPDK callback to release a TX queue.
1635 * Generic TX queue pointer.
1638 mlx4_tx_queue_release(void *dpdk_txq)
1640 struct txq *txq = (struct txq *)dpdk_txq;
1648 for (i = 0; (i != priv->txqs_n); ++i)
1649 if ((*priv->txqs)[i] == txq) {
1650 DEBUG("%p: removing TX queue %p from list",
1651 (void *)priv->dev, (void *)txq);
1652 (*priv->txqs)[i] = NULL;
1660 /* RX queues handling. */
1663 * Allocate RX queue elements with scattered packets support.
1666 * Pointer to RX queue structure.
1668 * Number of elements to allocate.
1670 * If not NULL, fetch buffers from this array instead of allocating them
1671 * with rte_pktmbuf_alloc().
1674 * 0 on success, errno value on failure.
1677 rxq_alloc_elts_sp(struct rxq *rxq, unsigned int elts_n,
1678 struct rte_mbuf **pool)
1681 struct rxq_elt_sp (*elts)[elts_n] =
1682 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1687 ERROR("%p: can't allocate packets array", (void *)rxq);
1691 /* For each WR (packet). */
1692 for (i = 0; (i != elts_n); ++i) {
1694 struct rxq_elt_sp *elt = &(*elts)[i];
1695 struct ibv_recv_wr *wr = &elt->wr;
1696 struct ibv_sge (*sges)[(elemof(elt->sges))] = &elt->sges;
1698 /* These two arrays must have the same size. */
1699 assert(elemof(elt->sges) == elemof(elt->bufs));
1702 wr->next = &(*elts)[(i + 1)].wr;
1703 wr->sg_list = &(*sges)[0];
1704 wr->num_sge = elemof(*sges);
1705 /* For each SGE (segment). */
1706 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1707 struct ibv_sge *sge = &(*sges)[j];
1708 struct rte_mbuf *buf;
1712 assert(buf != NULL);
1713 rte_pktmbuf_reset(buf);
1715 buf = rte_pktmbuf_alloc(rxq->mp);
1717 assert(pool == NULL);
1718 ERROR("%p: empty mbuf pool", (void *)rxq);
1723 /* Headroom is reserved by rte_pktmbuf_alloc(). */
1724 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1725 /* Buffer is supposed to be empty. */
1726 assert(rte_pktmbuf_data_len(buf) == 0);
1727 assert(rte_pktmbuf_pkt_len(buf) == 0);
1728 /* sge->addr must be able to store a pointer. */
1729 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1731 /* The first SGE keeps its headroom. */
1732 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
1733 sge->length = (buf->buf_len -
1734 RTE_PKTMBUF_HEADROOM);
1736 /* Subsequent SGEs lose theirs. */
1737 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1738 SET_DATA_OFF(buf, 0);
1739 sge->addr = (uintptr_t)buf->buf_addr;
1740 sge->length = buf->buf_len;
1742 sge->lkey = rxq->mr->lkey;
1743 /* Redundant check for tailroom. */
1744 assert(sge->length == rte_pktmbuf_tailroom(buf));
1747 /* The last WR pointer must be NULL. */
1748 (*elts)[(i - 1)].wr.next = NULL;
1749 DEBUG("%p: allocated and configured %u WRs (%zu segments)",
1750 (void *)rxq, elts_n, (elts_n * elemof((*elts)[0].sges)));
1751 rxq->elts_n = elts_n;
1753 rxq->elts.sp = elts;
1758 assert(pool == NULL);
1759 for (i = 0; (i != elemof(*elts)); ++i) {
1761 struct rxq_elt_sp *elt = &(*elts)[i];
1763 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1764 struct rte_mbuf *buf = elt->bufs[j];
1767 rte_pktmbuf_free_seg(buf);
1772 DEBUG("%p: failed, freed everything", (void *)rxq);
1778 * Free RX queue elements with scattered packets support.
1781 * Pointer to RX queue structure.
1784 rxq_free_elts_sp(struct rxq *rxq)
1787 unsigned int elts_n = rxq->elts_n;
1788 struct rxq_elt_sp (*elts)[elts_n] = rxq->elts.sp;
1790 DEBUG("%p: freeing WRs", (void *)rxq);
1792 rxq->elts.sp = NULL;
1795 for (i = 0; (i != elemof(*elts)); ++i) {
1797 struct rxq_elt_sp *elt = &(*elts)[i];
1799 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1800 struct rte_mbuf *buf = elt->bufs[j];
1803 rte_pktmbuf_free_seg(buf);
1810 * Allocate RX queue elements.
1813 * Pointer to RX queue structure.
1815 * Number of elements to allocate.
1817 * If not NULL, fetch buffers from this array instead of allocating them
1818 * with rte_pktmbuf_alloc().
1821 * 0 on success, errno value on failure.
1824 rxq_alloc_elts(struct rxq *rxq, unsigned int elts_n, struct rte_mbuf **pool)
1827 struct rxq_elt (*elts)[elts_n] =
1828 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1833 ERROR("%p: can't allocate packets array", (void *)rxq);
1837 /* For each WR (packet). */
1838 for (i = 0; (i != elts_n); ++i) {
1839 struct rxq_elt *elt = &(*elts)[i];
1840 struct ibv_recv_wr *wr = &elt->wr;
1841 struct ibv_sge *sge = &(*elts)[i].sge;
1842 struct rte_mbuf *buf;
1846 assert(buf != NULL);
1847 rte_pktmbuf_reset(buf);
1849 buf = rte_pktmbuf_alloc(rxq->mp);
1851 assert(pool == NULL);
1852 ERROR("%p: empty mbuf pool", (void *)rxq);
1856 /* Configure WR. Work request ID contains its own index in
1857 * the elts array and the offset between SGE buffer header and
1859 WR_ID(wr->wr_id).id = i;
1860 WR_ID(wr->wr_id).offset =
1861 (((uintptr_t)buf->buf_addr + RTE_PKTMBUF_HEADROOM) -
1863 wr->next = &(*elts)[(i + 1)].wr;
1866 /* Headroom is reserved by rte_pktmbuf_alloc(). */
1867 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1868 /* Buffer is supposed to be empty. */
1869 assert(rte_pktmbuf_data_len(buf) == 0);
1870 assert(rte_pktmbuf_pkt_len(buf) == 0);
1871 /* sge->addr must be able to store a pointer. */
1872 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1873 /* SGE keeps its headroom. */
1874 sge->addr = (uintptr_t)
1875 ((uint8_t *)buf->buf_addr + RTE_PKTMBUF_HEADROOM);
1876 sge->length = (buf->buf_len - RTE_PKTMBUF_HEADROOM);
1877 sge->lkey = rxq->mr->lkey;
1878 /* Redundant check for tailroom. */
1879 assert(sge->length == rte_pktmbuf_tailroom(buf));
1880 /* Make sure elts index and SGE mbuf pointer can be deduced
1882 if ((WR_ID(wr->wr_id).id != i) ||
1883 ((void *)((uintptr_t)sge->addr -
1884 WR_ID(wr->wr_id).offset) != buf)) {
1885 ERROR("%p: cannot store index and offset in WR ID",
1888 rte_pktmbuf_free(buf);
1893 /* The last WR pointer must be NULL. */
1894 (*elts)[(i - 1)].wr.next = NULL;
1895 DEBUG("%p: allocated and configured %u single-segment WRs",
1896 (void *)rxq, elts_n);
1897 rxq->elts_n = elts_n;
1899 rxq->elts.no_sp = elts;
1904 assert(pool == NULL);
1905 for (i = 0; (i != elemof(*elts)); ++i) {
1906 struct rxq_elt *elt = &(*elts)[i];
1907 struct rte_mbuf *buf;
1909 if (elt->sge.addr == 0)
1911 assert(WR_ID(elt->wr.wr_id).id == i);
1912 buf = (void *)((uintptr_t)elt->sge.addr -
1913 WR_ID(elt->wr.wr_id).offset);
1914 rte_pktmbuf_free_seg(buf);
1918 DEBUG("%p: failed, freed everything", (void *)rxq);
1924 * Free RX queue elements.
1927 * Pointer to RX queue structure.
1930 rxq_free_elts(struct rxq *rxq)
1933 unsigned int elts_n = rxq->elts_n;
1934 struct rxq_elt (*elts)[elts_n] = rxq->elts.no_sp;
1936 DEBUG("%p: freeing WRs", (void *)rxq);
1938 rxq->elts.no_sp = NULL;
1941 for (i = 0; (i != elemof(*elts)); ++i) {
1942 struct rxq_elt *elt = &(*elts)[i];
1943 struct rte_mbuf *buf;
1945 if (elt->sge.addr == 0)
1947 assert(WR_ID(elt->wr.wr_id).id == i);
1948 buf = (void *)((uintptr_t)elt->sge.addr -
1949 WR_ID(elt->wr.wr_id).offset);
1950 rte_pktmbuf_free_seg(buf);
1956 * Delete flow steering rule.
1959 * Pointer to RX queue structure.
1961 * MAC address index.
1966 rxq_del_flow(struct rxq *rxq, unsigned int mac_index, unsigned int vlan_index)
1969 struct priv *priv = rxq->priv;
1970 const uint8_t (*mac)[ETHER_ADDR_LEN] =
1971 (const uint8_t (*)[ETHER_ADDR_LEN])
1972 priv->mac[mac_index].addr_bytes;
1974 assert(rxq->mac_flow[mac_index][vlan_index] != NULL);
1975 DEBUG("%p: removing MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
1976 " (VLAN ID %" PRIu16 ")",
1978 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
1979 mac_index, priv->vlan_filter[vlan_index].id);
1980 claim_zero(ibv_destroy_flow(rxq->mac_flow[mac_index][vlan_index]));
1981 rxq->mac_flow[mac_index][vlan_index] = NULL;
1985 * Unregister a MAC address from a RX queue.
1988 * Pointer to RX queue structure.
1990 * MAC address index.
1993 rxq_mac_addr_del(struct rxq *rxq, unsigned int mac_index)
1995 struct priv *priv = rxq->priv;
1997 unsigned int vlans = 0;
1999 assert(mac_index < elemof(priv->mac));
2000 if (!BITFIELD_ISSET(rxq->mac_configured, mac_index))
2002 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
2003 if (!priv->vlan_filter[i].enabled)
2005 rxq_del_flow(rxq, mac_index, i);
2009 rxq_del_flow(rxq, mac_index, 0);
2011 BITFIELD_RESET(rxq->mac_configured, mac_index);
2015 * Unregister all MAC addresses from a RX queue.
2018 * Pointer to RX queue structure.
2021 rxq_mac_addrs_del(struct rxq *rxq)
2023 struct priv *priv = rxq->priv;
2026 for (i = 0; (i != elemof(priv->mac)); ++i)
2027 rxq_mac_addr_del(rxq, i);
2030 static int rxq_promiscuous_enable(struct rxq *);
2031 static void rxq_promiscuous_disable(struct rxq *);
2034 * Add single flow steering rule.
2037 * Pointer to RX queue structure.
2039 * MAC address index to register.
2041 * VLAN index. Use -1 for a flow without VLAN.
2044 * 0 on success, errno value on failure.
2047 rxq_add_flow(struct rxq *rxq, unsigned int mac_index, unsigned int vlan_index)
2049 struct ibv_flow *flow;
2050 struct priv *priv = rxq->priv;
2051 const uint8_t (*mac)[ETHER_ADDR_LEN] =
2052 (const uint8_t (*)[ETHER_ADDR_LEN])
2053 priv->mac[mac_index].addr_bytes;
2055 /* Allocate flow specification on the stack. */
2056 struct __attribute__((packed)) {
2057 struct ibv_flow_attr attr;
2058 struct ibv_flow_spec_eth spec;
2060 struct ibv_flow_attr *attr = &data.attr;
2061 struct ibv_flow_spec_eth *spec = &data.spec;
2063 assert(mac_index < elemof(priv->mac));
2064 assert((vlan_index < elemof(priv->vlan_filter)) || (vlan_index == -1u));
2066 * No padding must be inserted by the compiler between attr and spec.
2067 * This layout is expected by libibverbs.
2069 assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec);
2070 *attr = (struct ibv_flow_attr){
2071 .type = IBV_FLOW_ATTR_NORMAL,
2076 *spec = (struct ibv_flow_spec_eth){
2077 .type = IBV_FLOW_SPEC_ETH,
2078 .size = sizeof(*spec),
2081 (*mac)[0], (*mac)[1], (*mac)[2],
2082 (*mac)[3], (*mac)[4], (*mac)[5]
2084 .vlan_tag = ((vlan_index != -1u) ?
2085 htons(priv->vlan_filter[vlan_index].id) :
2089 .dst_mac = "\xff\xff\xff\xff\xff\xff",
2090 .vlan_tag = ((vlan_index != -1u) ? htons(0xfff) : 0),
2093 DEBUG("%p: adding MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
2094 " (VLAN %s %" PRIu16 ")",
2096 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
2098 ((vlan_index != -1u) ? "ID" : "index"),
2099 ((vlan_index != -1u) ? priv->vlan_filter[vlan_index].id : -1u));
2100 /* Create related flow. */
2102 flow = ibv_create_flow(rxq->qp, attr);
2104 /* It's not clear whether errno is always set in this case. */
2105 ERROR("%p: flow configuration failed, errno=%d: %s",
2107 (errno ? strerror(errno) : "Unknown error"));
2112 if (vlan_index == -1u)
2114 assert(rxq->mac_flow[mac_index][vlan_index] == NULL);
2115 rxq->mac_flow[mac_index][vlan_index] = flow;
2120 * Register a MAC address in a RX queue.
2123 * Pointer to RX queue structure.
2125 * MAC address index to register.
2128 * 0 on success, errno value on failure.
2131 rxq_mac_addr_add(struct rxq *rxq, unsigned int mac_index)
2133 struct priv *priv = rxq->priv;
2135 unsigned int vlans = 0;
2138 assert(mac_index < elemof(priv->mac));
2139 if (BITFIELD_ISSET(rxq->mac_configured, mac_index))
2140 rxq_mac_addr_del(rxq, mac_index);
2141 /* Fill VLAN specifications. */
2142 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
2143 if (!priv->vlan_filter[i].enabled)
2145 /* Create related flow. */
2146 ret = rxq_add_flow(rxq, mac_index, i);
2151 /* Failure, rollback. */
2153 if (priv->vlan_filter[--i].enabled)
2154 rxq_del_flow(rxq, mac_index, i);
2158 /* In case there is no VLAN filter. */
2160 ret = rxq_add_flow(rxq, mac_index, -1);
2164 BITFIELD_SET(rxq->mac_configured, mac_index);
2169 * Register all MAC addresses in a RX queue.
2172 * Pointer to RX queue structure.
2175 * 0 on success, errno value on failure.
2178 rxq_mac_addrs_add(struct rxq *rxq)
2180 struct priv *priv = rxq->priv;
2184 for (i = 0; (i != elemof(priv->mac)); ++i) {
2185 if (!BITFIELD_ISSET(priv->mac_configured, i))
2187 ret = rxq_mac_addr_add(rxq, i);
2190 /* Failure, rollback. */
2192 rxq_mac_addr_del(rxq, --i);
2200 * Unregister a MAC address.
2202 * In RSS mode, the MAC address is unregistered from the parent queue,
2203 * otherwise it is unregistered from each queue directly.
2206 * Pointer to private structure.
2208 * MAC address index.
2211 priv_mac_addr_del(struct priv *priv, unsigned int mac_index)
2215 assert(mac_index < elemof(priv->mac));
2216 if (!BITFIELD_ISSET(priv->mac_configured, mac_index))
2219 rxq_mac_addr_del(&priv->rxq_parent, mac_index);
2222 for (i = 0; (i != priv->dev->data->nb_rx_queues); ++i)
2223 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2225 BITFIELD_RESET(priv->mac_configured, mac_index);
2229 * Register a MAC address.
2231 * In RSS mode, the MAC address is registered in the parent queue,
2232 * otherwise it is registered in each queue directly.
2235 * Pointer to private structure.
2237 * MAC address index to use.
2239 * MAC address to register.
2242 * 0 on success, errno value on failure.
2245 priv_mac_addr_add(struct priv *priv, unsigned int mac_index,
2246 const uint8_t (*mac)[ETHER_ADDR_LEN])
2251 assert(mac_index < elemof(priv->mac));
2252 /* First, make sure this address isn't already configured. */
2253 for (i = 0; (i != elemof(priv->mac)); ++i) {
2254 /* Skip this index, it's going to be reconfigured. */
2257 if (!BITFIELD_ISSET(priv->mac_configured, i))
2259 if (memcmp(priv->mac[i].addr_bytes, *mac, sizeof(*mac)))
2261 /* Address already configured elsewhere, return with error. */
2264 if (BITFIELD_ISSET(priv->mac_configured, mac_index))
2265 priv_mac_addr_del(priv, mac_index);
2266 priv->mac[mac_index] = (struct ether_addr){
2268 (*mac)[0], (*mac)[1], (*mac)[2],
2269 (*mac)[3], (*mac)[4], (*mac)[5]
2272 /* If device isn't started, this is all we need to do. */
2273 if (!priv->started) {
2275 /* Verify that all queues have this index disabled. */
2276 for (i = 0; (i != priv->rxqs_n); ++i) {
2277 if ((*priv->rxqs)[i] == NULL)
2279 assert(!BITFIELD_ISSET
2280 ((*priv->rxqs)[i]->mac_configured, mac_index));
2286 ret = rxq_mac_addr_add(&priv->rxq_parent, mac_index);
2291 for (i = 0; (i != priv->rxqs_n); ++i) {
2292 if ((*priv->rxqs)[i] == NULL)
2294 ret = rxq_mac_addr_add((*priv->rxqs)[i], mac_index);
2297 /* Failure, rollback. */
2299 if ((*priv->rxqs)[(--i)] != NULL)
2300 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2304 BITFIELD_SET(priv->mac_configured, mac_index);
2309 * Enable allmulti mode in a RX queue.
2312 * Pointer to RX queue structure.
2315 * 0 on success, errno value on failure.
2318 rxq_allmulticast_enable(struct rxq *rxq)
2320 struct ibv_flow *flow;
2321 struct ibv_flow_attr attr = {
2322 .type = IBV_FLOW_ATTR_MC_DEFAULT,
2324 .port = rxq->priv->port,
2328 DEBUG("%p: enabling allmulticast mode", (void *)rxq);
2329 if (rxq->allmulti_flow != NULL)
2332 flow = ibv_create_flow(rxq->qp, &attr);
2334 /* It's not clear whether errno is always set in this case. */
2335 ERROR("%p: flow configuration failed, errno=%d: %s",
2337 (errno ? strerror(errno) : "Unknown error"));
2342 rxq->allmulti_flow = flow;
2343 DEBUG("%p: allmulticast mode enabled", (void *)rxq);
2348 * Disable allmulti mode in a RX queue.
2351 * Pointer to RX queue structure.
2354 rxq_allmulticast_disable(struct rxq *rxq)
2356 DEBUG("%p: disabling allmulticast mode", (void *)rxq);
2357 if (rxq->allmulti_flow == NULL)
2359 claim_zero(ibv_destroy_flow(rxq->allmulti_flow));
2360 rxq->allmulti_flow = NULL;
2361 DEBUG("%p: allmulticast mode disabled", (void *)rxq);
2365 * Enable promiscuous mode in a RX queue.
2368 * Pointer to RX queue structure.
2371 * 0 on success, errno value on failure.
2374 rxq_promiscuous_enable(struct rxq *rxq)
2376 struct ibv_flow *flow;
2377 struct ibv_flow_attr attr = {
2378 .type = IBV_FLOW_ATTR_ALL_DEFAULT,
2380 .port = rxq->priv->port,
2386 DEBUG("%p: enabling promiscuous mode", (void *)rxq);
2387 if (rxq->promisc_flow != NULL)
2390 flow = ibv_create_flow(rxq->qp, &attr);
2392 /* It's not clear whether errno is always set in this case. */
2393 ERROR("%p: flow configuration failed, errno=%d: %s",
2395 (errno ? strerror(errno) : "Unknown error"));
2400 rxq->promisc_flow = flow;
2401 DEBUG("%p: promiscuous mode enabled", (void *)rxq);
2406 * Disable promiscuous mode in a RX queue.
2409 * Pointer to RX queue structure.
2412 rxq_promiscuous_disable(struct rxq *rxq)
2416 DEBUG("%p: disabling promiscuous mode", (void *)rxq);
2417 if (rxq->promisc_flow == NULL)
2419 claim_zero(ibv_destroy_flow(rxq->promisc_flow));
2420 rxq->promisc_flow = NULL;
2421 DEBUG("%p: promiscuous mode disabled", (void *)rxq);
2425 * Clean up a RX queue.
2427 * Destroy objects, free allocated memory and reset the structure for reuse.
2430 * Pointer to RX queue structure.
2433 rxq_cleanup(struct rxq *rxq)
2435 struct ibv_exp_release_intf_params params;
2437 DEBUG("cleaning up %p", (void *)rxq);
2439 rxq_free_elts_sp(rxq);
2442 if (rxq->if_qp != NULL) {
2443 assert(rxq->priv != NULL);
2444 assert(rxq->priv->ctx != NULL);
2445 assert(rxq->qp != NULL);
2446 params = (struct ibv_exp_release_intf_params){
2449 claim_zero(ibv_exp_release_intf(rxq->priv->ctx,
2453 if (rxq->if_cq != NULL) {
2454 assert(rxq->priv != NULL);
2455 assert(rxq->priv->ctx != NULL);
2456 assert(rxq->cq != NULL);
2457 params = (struct ibv_exp_release_intf_params){
2460 claim_zero(ibv_exp_release_intf(rxq->priv->ctx,
2464 if (rxq->qp != NULL) {
2465 rxq_promiscuous_disable(rxq);
2466 rxq_allmulticast_disable(rxq);
2467 rxq_mac_addrs_del(rxq);
2468 claim_zero(ibv_destroy_qp(rxq->qp));
2470 if (rxq->cq != NULL)
2471 claim_zero(ibv_destroy_cq(rxq->cq));
2472 if (rxq->rd != NULL) {
2473 struct ibv_exp_destroy_res_domain_attr attr = {
2477 assert(rxq->priv != NULL);
2478 assert(rxq->priv->ctx != NULL);
2479 claim_zero(ibv_exp_destroy_res_domain(rxq->priv->ctx,
2483 if (rxq->mr != NULL)
2484 claim_zero(ibv_dereg_mr(rxq->mr));
2485 memset(rxq, 0, sizeof(*rxq));
2489 * Translate RX completion flags to offload flags.
2492 * Pointer to RX queue structure.
2494 * RX completion flags returned by poll_length_flags().
2497 * Offload flags (ol_flags) for struct rte_mbuf.
2499 static inline uint32_t
2500 rxq_cq_to_ol_flags(const struct rxq *rxq, uint32_t flags)
2505 TRANSPOSE(flags, IBV_EXP_CQ_RX_IPV4_PACKET, PKT_RX_IPV4_HDR) |
2506 TRANSPOSE(flags, IBV_EXP_CQ_RX_IPV6_PACKET, PKT_RX_IPV6_HDR);
2510 IBV_EXP_CQ_RX_IP_CSUM_OK,
2511 PKT_RX_IP_CKSUM_BAD) |
2513 IBV_EXP_CQ_RX_TCP_UDP_CSUM_OK,
2514 PKT_RX_L4_CKSUM_BAD);
2516 * PKT_RX_IP_CKSUM_BAD and PKT_RX_L4_CKSUM_BAD are used in place
2517 * of PKT_RX_EIP_CKSUM_BAD because the latter is not functional
2520 if ((flags & IBV_EXP_CQ_RX_TUNNEL_PACKET) && (rxq->csum_l2tun))
2523 IBV_EXP_CQ_RX_OUTER_IPV4_PACKET,
2524 PKT_RX_TUNNEL_IPV4_HDR) |
2526 IBV_EXP_CQ_RX_OUTER_IPV6_PACKET,
2527 PKT_RX_TUNNEL_IPV6_HDR) |
2529 IBV_EXP_CQ_RX_OUTER_IP_CSUM_OK,
2530 PKT_RX_IP_CKSUM_BAD) |
2532 IBV_EXP_CQ_RX_OUTER_TCP_UDP_CSUM_OK,
2533 PKT_RX_L4_CKSUM_BAD);
2538 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n);
2541 * DPDK callback for RX with scattered packets support.
2544 * Generic pointer to RX queue structure.
2546 * Array to store received packets.
2548 * Maximum number of packets in array.
2551 * Number of packets successfully received (<= pkts_n).
2554 mlx4_rx_burst_sp(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2556 struct rxq *rxq = (struct rxq *)dpdk_rxq;
2557 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
2558 const unsigned int elts_n = rxq->elts_n;
2559 unsigned int elts_head = rxq->elts_head;
2560 struct ibv_recv_wr head;
2561 struct ibv_recv_wr **next = &head.next;
2562 struct ibv_recv_wr *bad_wr;
2564 unsigned int pkts_ret = 0;
2567 if (unlikely(!rxq->sp))
2568 return mlx4_rx_burst(dpdk_rxq, pkts, pkts_n);
2569 if (unlikely(elts == NULL)) /* See RTE_DEV_CMD_SET_MTU. */
2571 for (i = 0; (i != pkts_n); ++i) {
2572 struct rxq_elt_sp *elt = &(*elts)[elts_head];
2573 struct ibv_recv_wr *wr = &elt->wr;
2574 uint64_t wr_id = wr->wr_id;
2576 unsigned int pkt_buf_len;
2577 struct rte_mbuf *pkt_buf = NULL; /* Buffer returned in pkts. */
2578 struct rte_mbuf **pkt_buf_next = &pkt_buf;
2579 unsigned int seg_headroom = RTE_PKTMBUF_HEADROOM;
2583 /* Sanity checks. */
2587 assert(wr_id < rxq->elts_n);
2588 assert(wr->sg_list == elt->sges);
2589 assert(wr->num_sge == elemof(elt->sges));
2590 assert(elts_head < rxq->elts_n);
2591 assert(rxq->elts_head < rxq->elts_n);
2592 ret = rxq->if_cq->poll_length_flags(rxq->cq, NULL, NULL,
2594 if (unlikely(ret < 0)) {
2598 DEBUG("rxq=%p, poll_length() failed (ret=%d)",
2600 /* ibv_poll_cq() must be used in case of failure. */
2601 wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
2602 if (unlikely(wcs_n == 0))
2604 if (unlikely(wcs_n < 0)) {
2605 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
2606 (void *)rxq, wcs_n);
2610 if (unlikely(wc.status != IBV_WC_SUCCESS)) {
2611 /* Whatever, just repost the offending WR. */
2612 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
2613 " completion status (%d): %s",
2614 (void *)rxq, wc.wr_id, wc.status,
2615 ibv_wc_status_str(wc.status));
2616 #ifdef MLX4_PMD_SOFT_COUNTERS
2617 /* Increment dropped packets counter. */
2618 ++rxq->stats.idropped;
2620 /* Link completed WRs together for repost. */
2631 /* Link completed WRs together for repost. */
2635 * Replace spent segments with new ones, concatenate and
2636 * return them as pkt_buf.
2639 struct ibv_sge *sge = &elt->sges[j];
2640 struct rte_mbuf *seg = elt->bufs[j];
2641 struct rte_mbuf *rep;
2642 unsigned int seg_tailroom;
2645 * Fetch initial bytes of packet descriptor into a
2646 * cacheline while allocating rep.
2649 rep = __rte_mbuf_raw_alloc(rxq->mp);
2650 if (unlikely(rep == NULL)) {
2652 * Unable to allocate a replacement mbuf,
2655 DEBUG("rxq=%p, wr_id=%" PRIu64 ":"
2656 " can't allocate a new mbuf",
2657 (void *)rxq, wr_id);
2658 if (pkt_buf != NULL) {
2659 *pkt_buf_next = NULL;
2660 rte_pktmbuf_free(pkt_buf);
2662 /* Increase out of memory counters. */
2663 ++rxq->stats.rx_nombuf;
2664 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
2668 /* Poison user-modifiable fields in rep. */
2669 NEXT(rep) = (void *)((uintptr_t)-1);
2670 SET_DATA_OFF(rep, 0xdead);
2671 DATA_LEN(rep) = 0xd00d;
2672 PKT_LEN(rep) = 0xdeadd00d;
2673 NB_SEGS(rep) = 0x2a;
2677 assert(rep->buf_len == seg->buf_len);
2678 assert(rep->buf_len == rxq->mb_len);
2679 /* Reconfigure sge to use rep instead of seg. */
2680 assert(sge->lkey == rxq->mr->lkey);
2681 sge->addr = ((uintptr_t)rep->buf_addr + seg_headroom);
2684 /* Update pkt_buf if it's the first segment, or link
2685 * seg to the previous one and update pkt_buf_next. */
2686 *pkt_buf_next = seg;
2687 pkt_buf_next = &NEXT(seg);
2688 /* Update seg information. */
2689 seg_tailroom = (seg->buf_len - seg_headroom);
2690 assert(sge->length == seg_tailroom);
2691 SET_DATA_OFF(seg, seg_headroom);
2692 if (likely(len <= seg_tailroom)) {
2694 DATA_LEN(seg) = len;
2697 assert(rte_pktmbuf_headroom(seg) ==
2699 assert(rte_pktmbuf_tailroom(seg) ==
2700 (seg_tailroom - len));
2703 DATA_LEN(seg) = seg_tailroom;
2704 PKT_LEN(seg) = seg_tailroom;
2706 assert(rte_pktmbuf_headroom(seg) == seg_headroom);
2707 assert(rte_pktmbuf_tailroom(seg) == 0);
2708 /* Fix len and clear headroom for next segments. */
2709 len -= seg_tailroom;
2712 /* Update head and tail segments. */
2713 *pkt_buf_next = NULL;
2714 assert(pkt_buf != NULL);
2716 NB_SEGS(pkt_buf) = j;
2717 PORT(pkt_buf) = rxq->port_id;
2718 PKT_LEN(pkt_buf) = pkt_buf_len;
2719 pkt_buf->ol_flags = rxq_cq_to_ol_flags(rxq, flags);
2721 /* Return packet. */
2722 *(pkts++) = pkt_buf;
2724 #ifdef MLX4_PMD_SOFT_COUNTERS
2725 /* Increase bytes counter. */
2726 rxq->stats.ibytes += pkt_buf_len;
2729 if (++elts_head >= elts_n)
2733 if (unlikely(i == 0))
2738 DEBUG("%p: reposting %d WRs", (void *)rxq, i);
2740 ret = ibv_post_recv(rxq->qp, head.next, &bad_wr);
2741 if (unlikely(ret)) {
2742 /* Inability to repost WRs is fatal. */
2743 DEBUG("%p: ibv_post_recv(): failed for WR %p: %s",
2749 rxq->elts_head = elts_head;
2750 #ifdef MLX4_PMD_SOFT_COUNTERS
2751 /* Increase packets counter. */
2752 rxq->stats.ipackets += pkts_ret;
2758 * DPDK callback for RX.
2760 * The following function is the same as mlx4_rx_burst_sp(), except it doesn't
2761 * manage scattered packets. Improves performance when MRU is lower than the
2762 * size of the first segment.
2765 * Generic pointer to RX queue structure.
2767 * Array to store received packets.
2769 * Maximum number of packets in array.
2772 * Number of packets successfully received (<= pkts_n).
2775 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2777 struct rxq *rxq = (struct rxq *)dpdk_rxq;
2778 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
2779 const unsigned int elts_n = rxq->elts_n;
2780 unsigned int elts_head = rxq->elts_head;
2781 struct ibv_sge sges[pkts_n];
2783 unsigned int pkts_ret = 0;
2786 if (unlikely(rxq->sp))
2787 return mlx4_rx_burst_sp(dpdk_rxq, pkts, pkts_n);
2788 for (i = 0; (i != pkts_n); ++i) {
2789 struct rxq_elt *elt = &(*elts)[elts_head];
2790 struct ibv_recv_wr *wr = &elt->wr;
2791 uint64_t wr_id = wr->wr_id;
2793 struct rte_mbuf *seg = (void *)((uintptr_t)elt->sge.addr -
2794 WR_ID(wr_id).offset);
2795 struct rte_mbuf *rep;
2798 /* Sanity checks. */
2799 assert(WR_ID(wr_id).id < rxq->elts_n);
2800 assert(wr->sg_list == &elt->sge);
2801 assert(wr->num_sge == 1);
2802 assert(elts_head < rxq->elts_n);
2803 assert(rxq->elts_head < rxq->elts_n);
2804 ret = rxq->if_cq->poll_length_flags(rxq->cq, NULL, NULL,
2806 if (unlikely(ret < 0)) {
2810 DEBUG("rxq=%p, poll_length() failed (ret=%d)",
2812 /* ibv_poll_cq() must be used in case of failure. */
2813 wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
2814 if (unlikely(wcs_n == 0))
2816 if (unlikely(wcs_n < 0)) {
2817 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
2818 (void *)rxq, wcs_n);
2822 if (unlikely(wc.status != IBV_WC_SUCCESS)) {
2823 /* Whatever, just repost the offending WR. */
2824 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
2825 " completion status (%d): %s",
2826 (void *)rxq, wc.wr_id, wc.status,
2827 ibv_wc_status_str(wc.status));
2828 #ifdef MLX4_PMD_SOFT_COUNTERS
2829 /* Increment dropped packets counter. */
2830 ++rxq->stats.idropped;
2832 /* Add SGE to array for repost. */
2842 * Fetch initial bytes of packet descriptor into a
2843 * cacheline while allocating rep.
2846 rep = __rte_mbuf_raw_alloc(rxq->mp);
2847 if (unlikely(rep == NULL)) {
2849 * Unable to allocate a replacement mbuf,
2852 DEBUG("rxq=%p, wr_id=%" PRIu32 ":"
2853 " can't allocate a new mbuf",
2854 (void *)rxq, WR_ID(wr_id).id);
2855 /* Increase out of memory counters. */
2856 ++rxq->stats.rx_nombuf;
2857 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
2861 /* Reconfigure sge to use rep instead of seg. */
2862 elt->sge.addr = (uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM;
2863 assert(elt->sge.lkey == rxq->mr->lkey);
2864 WR_ID(wr->wr_id).offset =
2865 (((uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM) -
2867 assert(WR_ID(wr->wr_id).id == WR_ID(wr_id).id);
2869 /* Add SGE to array for repost. */
2872 /* Update seg information. */
2873 SET_DATA_OFF(seg, RTE_PKTMBUF_HEADROOM);
2875 PORT(seg) = rxq->port_id;
2878 DATA_LEN(seg) = len;
2879 seg->ol_flags = rxq_cq_to_ol_flags(rxq, flags);
2881 /* Return packet. */
2884 #ifdef MLX4_PMD_SOFT_COUNTERS
2885 /* Increase bytes counter. */
2886 rxq->stats.ibytes += len;
2889 if (++elts_head >= elts_n)
2893 if (unlikely(i == 0))
2897 DEBUG("%p: reposting %u WRs", (void *)rxq, i);
2899 ret = rxq->if_qp->recv_burst(rxq->qp, sges, i);
2900 if (unlikely(ret)) {
2901 /* Inability to repost WRs is fatal. */
2902 DEBUG("%p: recv_burst(): failed (ret=%d)",
2907 rxq->elts_head = elts_head;
2908 #ifdef MLX4_PMD_SOFT_COUNTERS
2909 /* Increase packets counter. */
2910 rxq->stats.ipackets += pkts_ret;
2916 * Allocate a Queue Pair.
2917 * Optionally setup inline receive if supported.
2920 * Pointer to private structure.
2922 * Completion queue to associate with QP.
2924 * Number of descriptors in QP (hint only).
2927 * QP pointer or NULL in case of error.
2929 static struct ibv_qp *
2930 rxq_setup_qp(struct priv *priv, struct ibv_cq *cq, uint16_t desc,
2931 struct ibv_exp_res_domain *rd)
2933 struct ibv_exp_qp_init_attr attr = {
2934 /* CQ to be associated with the send queue. */
2936 /* CQ to be associated with the receive queue. */
2939 /* Max number of outstanding WRs. */
2940 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
2941 priv->device_attr.max_qp_wr :
2943 /* Max number of scatter/gather elements in a WR. */
2944 .max_recv_sge = ((priv->device_attr.max_sge <
2945 MLX4_PMD_SGE_WR_N) ?
2946 priv->device_attr.max_sge :
2949 .qp_type = IBV_QPT_RAW_PACKET,
2950 .comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
2951 IBV_EXP_QP_INIT_ATTR_RES_DOMAIN),
2957 attr.max_inl_recv = priv->inl_recv_size;
2958 attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
2960 return ibv_exp_create_qp(priv->ctx, &attr);
2966 * Allocate a RSS Queue Pair.
2967 * Optionally setup inline receive if supported.
2970 * Pointer to private structure.
2972 * Completion queue to associate with QP.
2974 * Number of descriptors in QP (hint only).
2976 * If nonzero, create a parent QP, otherwise a child.
2979 * QP pointer or NULL in case of error.
2981 static struct ibv_qp *
2982 rxq_setup_qp_rss(struct priv *priv, struct ibv_cq *cq, uint16_t desc,
2983 int parent, struct ibv_exp_res_domain *rd)
2985 struct ibv_exp_qp_init_attr attr = {
2986 /* CQ to be associated with the send queue. */
2988 /* CQ to be associated with the receive queue. */
2991 /* Max number of outstanding WRs. */
2992 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
2993 priv->device_attr.max_qp_wr :
2995 /* Max number of scatter/gather elements in a WR. */
2996 .max_recv_sge = ((priv->device_attr.max_sge <
2997 MLX4_PMD_SGE_WR_N) ?
2998 priv->device_attr.max_sge :
3001 .qp_type = IBV_QPT_RAW_PACKET,
3002 .comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
3003 IBV_EXP_QP_INIT_ATTR_RES_DOMAIN |
3004 IBV_EXP_QP_INIT_ATTR_QPG),
3010 attr.max_inl_recv = priv->inl_recv_size,
3011 attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
3014 attr.qpg.qpg_type = IBV_EXP_QPG_PARENT;
3015 /* TSS isn't necessary. */
3016 attr.qpg.parent_attrib.tss_child_count = 0;
3017 attr.qpg.parent_attrib.rss_child_count = priv->rxqs_n;
3018 DEBUG("initializing parent RSS queue");
3020 attr.qpg.qpg_type = IBV_EXP_QPG_CHILD_RX;
3021 attr.qpg.qpg_parent = priv->rxq_parent.qp;
3022 DEBUG("initializing child RSS queue");
3024 return ibv_exp_create_qp(priv->ctx, &attr);
3027 #endif /* RSS_SUPPORT */
3030 * Reconfigure a RX queue with new parameters.
3032 * rxq_rehash() does not allocate mbufs, which, if not done from the right
3033 * thread (such as a control thread), may corrupt the pool.
3034 * In case of failure, the queue is left untouched.
3037 * Pointer to Ethernet device structure.
3042 * 0 on success, errno value on failure.
3045 rxq_rehash(struct rte_eth_dev *dev, struct rxq *rxq)
3047 struct priv *priv = rxq->priv;
3048 struct rxq tmpl = *rxq;
3049 unsigned int mbuf_n;
3050 unsigned int desc_n;
3051 struct rte_mbuf **pool;
3053 struct ibv_exp_qp_attr mod;
3054 struct ibv_recv_wr *bad_wr;
3056 int parent = (rxq == &priv->rxq_parent);
3059 ERROR("%p: cannot rehash parent queue %p",
3060 (void *)dev, (void *)rxq);
3063 DEBUG("%p: rehashing queue %p", (void *)dev, (void *)rxq);
3064 /* Number of descriptors and mbufs currently allocated. */
3065 desc_n = (tmpl.elts_n * (tmpl.sp ? MLX4_PMD_SGE_WR_N : 1));
3067 /* Toggle RX checksum offload if hardware supports it. */
3068 if (priv->hw_csum) {
3069 tmpl.csum = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3070 rxq->csum = tmpl.csum;
3072 if (priv->hw_csum_l2tun) {
3073 tmpl.csum_l2tun = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3074 rxq->csum_l2tun = tmpl.csum_l2tun;
3076 /* Enable scattered packets support for this queue if necessary. */
3077 if ((dev->data->dev_conf.rxmode.jumbo_frame) &&
3078 (dev->data->dev_conf.rxmode.max_rx_pkt_len >
3079 (tmpl.mb_len - RTE_PKTMBUF_HEADROOM))) {
3081 desc_n /= MLX4_PMD_SGE_WR_N;
3084 DEBUG("%p: %s scattered packets support (%u WRs)",
3085 (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc_n);
3086 /* If scatter mode is the same as before, nothing to do. */
3087 if (tmpl.sp == rxq->sp) {
3088 DEBUG("%p: nothing to do", (void *)dev);
3091 /* Remove attached flows if RSS is disabled (no parent queue). */
3093 rxq_allmulticast_disable(&tmpl);
3094 rxq_promiscuous_disable(&tmpl);
3095 rxq_mac_addrs_del(&tmpl);
3096 /* Update original queue in case of failure. */
3097 rxq->allmulti_flow = tmpl.allmulti_flow;
3098 rxq->promisc_flow = tmpl.promisc_flow;
3099 memcpy(rxq->mac_configured, tmpl.mac_configured,
3100 sizeof(rxq->mac_configured));
3101 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
3103 /* From now on, any failure will render the queue unusable.
3104 * Reinitialize QP. */
3105 mod = (struct ibv_exp_qp_attr){ .qp_state = IBV_QPS_RESET };
3106 err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3108 ERROR("%p: cannot reset QP: %s", (void *)dev, strerror(err));
3112 err = ibv_resize_cq(tmpl.cq, desc_n);
3114 ERROR("%p: cannot resize CQ: %s", (void *)dev, strerror(err));
3118 mod = (struct ibv_exp_qp_attr){
3119 /* Move the QP to this state. */
3120 .qp_state = IBV_QPS_INIT,
3121 /* Primary port number. */
3122 .port_num = priv->port
3124 err = ibv_exp_modify_qp(tmpl.qp, &mod,
3127 (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
3128 #endif /* RSS_SUPPORT */
3131 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
3132 (void *)dev, strerror(err));
3136 /* Reconfigure flows. Do not care for errors. */
3138 rxq_mac_addrs_add(&tmpl);
3140 rxq_promiscuous_enable(&tmpl);
3142 rxq_allmulticast_enable(&tmpl);
3143 /* Update original queue in case of failure. */
3144 rxq->allmulti_flow = tmpl.allmulti_flow;
3145 rxq->promisc_flow = tmpl.promisc_flow;
3146 memcpy(rxq->mac_configured, tmpl.mac_configured,
3147 sizeof(rxq->mac_configured));
3148 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
3150 /* Allocate pool. */
3151 pool = rte_malloc(__func__, (mbuf_n * sizeof(*pool)), 0);
3153 ERROR("%p: cannot allocate memory", (void *)dev);
3156 /* Snatch mbufs from original queue. */
3159 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
3161 for (i = 0; (i != elemof(*elts)); ++i) {
3162 struct rxq_elt_sp *elt = &(*elts)[i];
3165 for (j = 0; (j != elemof(elt->bufs)); ++j) {
3166 assert(elt->bufs[j] != NULL);
3167 pool[k++] = elt->bufs[j];
3171 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
3173 for (i = 0; (i != elemof(*elts)); ++i) {
3174 struct rxq_elt *elt = &(*elts)[i];
3175 struct rte_mbuf *buf = (void *)
3176 ((uintptr_t)elt->sge.addr -
3177 WR_ID(elt->wr.wr_id).offset);
3179 assert(WR_ID(elt->wr.wr_id).id == i);
3183 assert(k == mbuf_n);
3185 tmpl.elts.sp = NULL;
3186 assert((void *)&tmpl.elts.sp == (void *)&tmpl.elts.no_sp);
3188 rxq_alloc_elts_sp(&tmpl, desc_n, pool) :
3189 rxq_alloc_elts(&tmpl, desc_n, pool));
3191 ERROR("%p: cannot reallocate WRs, aborting", (void *)dev);
3196 assert(tmpl.elts_n == desc_n);
3197 assert(tmpl.elts.sp != NULL);
3199 /* Clean up original data. */
3201 rte_free(rxq->elts.sp);
3202 rxq->elts.sp = NULL;
3204 err = ibv_post_recv(tmpl.qp,
3206 &(*tmpl.elts.sp)[0].wr :
3207 &(*tmpl.elts.no_sp)[0].wr),
3210 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
3216 mod = (struct ibv_exp_qp_attr){
3217 .qp_state = IBV_QPS_RTR
3219 err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3221 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
3222 (void *)dev, strerror(err));
3230 * Configure a RX queue.
3233 * Pointer to Ethernet device structure.
3235 * Pointer to RX queue structure.
3237 * Number of descriptors to configure in queue.
3239 * NUMA socket on which memory must be allocated.
3241 * Thresholds parameters.
3243 * Memory pool for buffer allocations.
3246 * 0 on success, errno value on failure.
3249 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
3250 unsigned int socket, const struct rte_eth_rxconf *conf,
3251 struct rte_mempool *mp)
3253 struct priv *priv = dev->data->dev_private;
3259 struct ibv_exp_qp_attr mod;
3261 struct ibv_exp_query_intf_params params;
3262 struct ibv_exp_cq_init_attr cq;
3263 struct ibv_exp_res_domain_init_attr rd;
3265 enum ibv_exp_query_intf_status status;
3266 struct ibv_recv_wr *bad_wr;
3267 struct rte_mbuf *buf;
3269 int parent = (rxq == &priv->rxq_parent);
3271 (void)conf; /* Thresholds configuration (ignored). */
3273 * If this is a parent queue, hardware must support RSS and
3274 * RSS must be enabled.
3276 assert((!parent) || ((priv->hw_rss) && (priv->rss)));
3278 /* Even if unused, ibv_create_cq() requires at least one
3283 if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
3284 ERROR("%p: invalid number of RX descriptors (must be a"
3285 " multiple of %d)", (void *)dev, MLX4_PMD_SGE_WR_N);
3288 /* Get mbuf length. */
3289 buf = rte_pktmbuf_alloc(mp);
3291 ERROR("%p: unable to allocate mbuf", (void *)dev);
3294 tmpl.mb_len = buf->buf_len;
3295 assert((rte_pktmbuf_headroom(buf) +
3296 rte_pktmbuf_tailroom(buf)) == tmpl.mb_len);
3297 assert(rte_pktmbuf_headroom(buf) == RTE_PKTMBUF_HEADROOM);
3298 rte_pktmbuf_free(buf);
3299 /* Toggle RX checksum offload if hardware supports it. */
3301 tmpl.csum = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3302 if (priv->hw_csum_l2tun)
3303 tmpl.csum_l2tun = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3304 /* Enable scattered packets support for this queue if necessary. */
3305 if ((dev->data->dev_conf.rxmode.jumbo_frame) &&
3306 (dev->data->dev_conf.rxmode.max_rx_pkt_len >
3307 (tmpl.mb_len - RTE_PKTMBUF_HEADROOM))) {
3309 desc /= MLX4_PMD_SGE_WR_N;
3311 DEBUG("%p: %s scattered packets support (%u WRs)",
3312 (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc);
3313 /* Use the entire RX mempool as the memory region. */
3314 tmpl.mr = ibv_reg_mr(priv->pd,
3315 (void *)mp->elt_va_start,
3316 (mp->elt_va_end - mp->elt_va_start),
3317 (IBV_ACCESS_LOCAL_WRITE |
3318 IBV_ACCESS_REMOTE_WRITE));
3319 if (tmpl.mr == NULL) {
3321 ERROR("%p: MR creation failure: %s",
3322 (void *)dev, strerror(ret));
3326 attr.rd = (struct ibv_exp_res_domain_init_attr){
3327 .comp_mask = (IBV_EXP_RES_DOMAIN_THREAD_MODEL |
3328 IBV_EXP_RES_DOMAIN_MSG_MODEL),
3329 .thread_model = IBV_EXP_THREAD_SINGLE,
3330 .msg_model = IBV_EXP_MSG_HIGH_BW,
3332 tmpl.rd = ibv_exp_create_res_domain(priv->ctx, &attr.rd);
3333 if (tmpl.rd == NULL) {
3335 ERROR("%p: RD creation failure: %s",
3336 (void *)dev, strerror(ret));
3339 attr.cq = (struct ibv_exp_cq_init_attr){
3340 .comp_mask = IBV_EXP_CQ_INIT_ATTR_RES_DOMAIN,
3341 .res_domain = tmpl.rd,
3343 tmpl.cq = ibv_exp_create_cq(priv->ctx, desc, NULL, NULL, 0, &attr.cq);
3344 if (tmpl.cq == NULL) {
3346 ERROR("%p: CQ creation failure: %s",
3347 (void *)dev, strerror(ret));
3350 DEBUG("priv->device_attr.max_qp_wr is %d",
3351 priv->device_attr.max_qp_wr);
3352 DEBUG("priv->device_attr.max_sge is %d",
3353 priv->device_attr.max_sge);
3356 tmpl.qp = rxq_setup_qp_rss(priv, tmpl.cq, desc, parent,
3359 #endif /* RSS_SUPPORT */
3360 tmpl.qp = rxq_setup_qp(priv, tmpl.cq, desc, tmpl.rd);
3361 if (tmpl.qp == NULL) {
3362 ret = (errno ? errno : EINVAL);
3363 ERROR("%p: QP creation failure: %s",
3364 (void *)dev, strerror(ret));
3367 mod = (struct ibv_exp_qp_attr){
3368 /* Move the QP to this state. */
3369 .qp_state = IBV_QPS_INIT,
3370 /* Primary port number. */
3371 .port_num = priv->port
3373 ret = ibv_exp_modify_qp(tmpl.qp, &mod,
3376 (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
3377 #endif /* RSS_SUPPORT */
3380 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
3381 (void *)dev, strerror(ret));
3384 if ((parent) || (!priv->rss)) {
3385 /* Configure MAC and broadcast addresses. */
3386 ret = rxq_mac_addrs_add(&tmpl);
3388 ERROR("%p: QP flow attachment failed: %s",
3389 (void *)dev, strerror(ret));
3393 /* Allocate descriptors for RX queues, except for the RSS parent. */
3397 ret = rxq_alloc_elts_sp(&tmpl, desc, NULL);
3399 ret = rxq_alloc_elts(&tmpl, desc, NULL);
3401 ERROR("%p: RXQ allocation failed: %s",
3402 (void *)dev, strerror(ret));
3405 ret = ibv_post_recv(tmpl.qp,
3407 &(*tmpl.elts.sp)[0].wr :
3408 &(*tmpl.elts.no_sp)[0].wr),
3411 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
3418 mod = (struct ibv_exp_qp_attr){
3419 .qp_state = IBV_QPS_RTR
3421 ret = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3423 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
3424 (void *)dev, strerror(ret));
3428 tmpl.port_id = dev->data->port_id;
3429 DEBUG("%p: RTE port ID: %u", (void *)rxq, tmpl.port_id);
3430 attr.params = (struct ibv_exp_query_intf_params){
3431 .intf_scope = IBV_EXP_INTF_GLOBAL,
3432 .intf = IBV_EXP_INTF_CQ,
3435 tmpl.if_cq = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
3436 if (tmpl.if_cq == NULL) {
3437 ERROR("%p: CQ interface family query failed with status %d",
3438 (void *)dev, status);
3441 attr.params = (struct ibv_exp_query_intf_params){
3442 .intf_scope = IBV_EXP_INTF_GLOBAL,
3443 .intf = IBV_EXP_INTF_QP_BURST,
3446 tmpl.if_qp = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
3447 if (tmpl.if_qp == NULL) {
3448 ERROR("%p: QP interface family query failed with status %d",
3449 (void *)dev, status);
3452 /* Clean up rxq in case we're reinitializing it. */
3453 DEBUG("%p: cleaning-up old rxq just in case", (void *)rxq);
3456 DEBUG("%p: rxq updated with %p", (void *)rxq, (void *)&tmpl);
3466 * DPDK callback to configure a RX queue.
3469 * Pointer to Ethernet device structure.
3473 * Number of descriptors to configure in queue.
3475 * NUMA socket on which memory must be allocated.
3477 * Thresholds parameters.
3479 * Memory pool for buffer allocations.
3482 * 0 on success, negative errno value on failure.
3485 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
3486 unsigned int socket, const struct rte_eth_rxconf *conf,
3487 struct rte_mempool *mp)
3489 struct priv *priv = dev->data->dev_private;
3490 struct rxq *rxq = (*priv->rxqs)[idx];
3494 DEBUG("%p: configuring queue %u for %u descriptors",
3495 (void *)dev, idx, desc);
3496 if (idx >= priv->rxqs_n) {
3497 ERROR("%p: queue index out of range (%u >= %u)",
3498 (void *)dev, idx, priv->rxqs_n);
3503 DEBUG("%p: reusing already allocated queue index %u (%p)",
3504 (void *)dev, idx, (void *)rxq);
3505 if (priv->started) {
3509 (*priv->rxqs)[idx] = NULL;
3512 rxq = rte_calloc_socket("RXQ", 1, sizeof(*rxq), 0, socket);
3514 ERROR("%p: unable to allocate queue index %u",
3520 ret = rxq_setup(dev, rxq, desc, socket, conf, mp);
3524 rxq->stats.idx = idx;
3525 DEBUG("%p: adding RX queue %p to list",
3526 (void *)dev, (void *)rxq);
3527 (*priv->rxqs)[idx] = rxq;
3528 /* Update receive callback. */
3530 dev->rx_pkt_burst = mlx4_rx_burst_sp;
3532 dev->rx_pkt_burst = mlx4_rx_burst;
3539 * DPDK callback to release a RX queue.
3542 * Generic RX queue pointer.
3545 mlx4_rx_queue_release(void *dpdk_rxq)
3547 struct rxq *rxq = (struct rxq *)dpdk_rxq;
3555 assert(rxq != &priv->rxq_parent);
3556 for (i = 0; (i != priv->rxqs_n); ++i)
3557 if ((*priv->rxqs)[i] == rxq) {
3558 DEBUG("%p: removing RX queue %p from list",
3559 (void *)priv->dev, (void *)rxq);
3560 (*priv->rxqs)[i] = NULL;
3569 * DPDK callback to start the device.
3571 * Simulate device start by attaching all configured flows.
3574 * Pointer to Ethernet device structure.
3577 * 0 on success, negative errno value on failure.
3580 mlx4_dev_start(struct rte_eth_dev *dev)
3582 struct priv *priv = dev->data->dev_private;
3588 if (priv->started) {
3592 DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
3595 rxq = &priv->rxq_parent;
3598 rxq = (*priv->rxqs)[0];
3601 /* Iterate only once when RSS is enabled. */
3605 /* Ignore nonexistent RX queues. */
3608 ret = rxq_mac_addrs_add(rxq);
3609 if (!ret && priv->promisc)
3610 ret = rxq_promiscuous_enable(rxq);
3611 if (!ret && priv->allmulti)
3612 ret = rxq_allmulticast_enable(rxq);
3615 WARN("%p: QP flow attachment failed: %s",
3616 (void *)dev, strerror(ret));
3619 rxq = (*priv->rxqs)[--i];
3621 rxq_allmulticast_disable(rxq);
3622 rxq_promiscuous_disable(rxq);
3623 rxq_mac_addrs_del(rxq);
3628 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
3634 * DPDK callback to stop the device.
3636 * Simulate device stop by detaching all configured flows.
3639 * Pointer to Ethernet device structure.
3642 mlx4_dev_stop(struct rte_eth_dev *dev)
3644 struct priv *priv = dev->data->dev_private;
3650 if (!priv->started) {
3654 DEBUG("%p: detaching flows from all RX queues", (void *)dev);
3657 rxq = &priv->rxq_parent;
3660 rxq = (*priv->rxqs)[0];
3663 /* Iterate only once when RSS is enabled. */
3665 /* Ignore nonexistent RX queues. */
3668 rxq_allmulticast_disable(rxq);
3669 rxq_promiscuous_disable(rxq);
3670 rxq_mac_addrs_del(rxq);
3671 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
3676 * Dummy DPDK callback for TX.
3678 * This function is used to temporarily replace the real callback during
3679 * unsafe control operations on the queue, or in case of error.
3682 * Generic pointer to TX queue structure.
3684 * Packets to transmit.
3686 * Number of packets in array.
3689 * Number of packets successfully transmitted (<= pkts_n).
3692 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
3701 * Dummy DPDK callback for RX.
3703 * This function is used to temporarily replace the real callback during
3704 * unsafe control operations on the queue, or in case of error.
3707 * Generic pointer to RX queue structure.
3709 * Array to store received packets.
3711 * Maximum number of packets in array.
3714 * Number of packets successfully received (<= pkts_n).
3717 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
3726 * DPDK callback to close the device.
3728 * Destroy all queues and objects, free memory.
3731 * Pointer to Ethernet device structure.
3734 mlx4_dev_close(struct rte_eth_dev *dev)
3736 struct priv *priv = dev->data->dev_private;
3741 DEBUG("%p: closing device \"%s\"",
3743 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
3744 /* Prevent crashes when queues are still in use. This is unfortunately
3745 * still required for DPDK 1.3 because some programs (such as testpmd)
3746 * never release them before closing the device. */
3747 dev->rx_pkt_burst = removed_rx_burst;
3748 dev->tx_pkt_burst = removed_tx_burst;
3749 if (priv->rxqs != NULL) {
3750 /* XXX race condition if mlx4_rx_burst() is still running. */
3752 for (i = 0; (i != priv->rxqs_n); ++i) {
3753 tmp = (*priv->rxqs)[i];
3756 (*priv->rxqs)[i] = NULL;
3763 if (priv->txqs != NULL) {
3764 /* XXX race condition if mlx4_tx_burst() is still running. */
3766 for (i = 0; (i != priv->txqs_n); ++i) {
3767 tmp = (*priv->txqs)[i];
3770 (*priv->txqs)[i] = NULL;
3778 rxq_cleanup(&priv->rxq_parent);
3779 if (priv->pd != NULL) {
3780 assert(priv->ctx != NULL);
3781 claim_zero(ibv_dealloc_pd(priv->pd));
3782 claim_zero(ibv_close_device(priv->ctx));
3784 assert(priv->ctx == NULL);
3786 memset(priv, 0, sizeof(*priv));
3790 * DPDK callback to get information about the device.
3793 * Pointer to Ethernet device structure.
3795 * Info structure output buffer.
3798 mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
3800 struct priv *priv = dev->data->dev_private;
3804 /* FIXME: we should ask the device for these values. */
3805 info->min_rx_bufsize = 32;
3806 info->max_rx_pktlen = 65536;
3808 * Since we need one CQ per QP, the limit is the minimum number
3809 * between the two values.
3811 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
3812 priv->device_attr.max_qp : priv->device_attr.max_cq);
3813 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
3816 info->max_rx_queues = max;
3817 info->max_tx_queues = max;
3818 info->max_mac_addrs = elemof(priv->mac);
3819 info->rx_offload_capa =
3821 (DEV_RX_OFFLOAD_IPV4_CKSUM |
3822 DEV_RX_OFFLOAD_UDP_CKSUM |
3823 DEV_RX_OFFLOAD_TCP_CKSUM) :
3825 info->tx_offload_capa =
3827 (DEV_TX_OFFLOAD_IPV4_CKSUM |
3828 DEV_TX_OFFLOAD_UDP_CKSUM |
3829 DEV_TX_OFFLOAD_TCP_CKSUM) :
3835 * DPDK callback to get device statistics.
3838 * Pointer to Ethernet device structure.
3840 * Stats structure output buffer.
3843 mlx4_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
3845 struct priv *priv = dev->data->dev_private;
3846 struct rte_eth_stats tmp = {0};
3851 /* Add software counters. */
3852 for (i = 0; (i != priv->rxqs_n); ++i) {
3853 struct rxq *rxq = (*priv->rxqs)[i];
3857 idx = rxq->stats.idx;
3858 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
3859 #ifdef MLX4_PMD_SOFT_COUNTERS
3860 tmp.q_ipackets[idx] += rxq->stats.ipackets;
3861 tmp.q_ibytes[idx] += rxq->stats.ibytes;
3863 tmp.q_errors[idx] += (rxq->stats.idropped +
3864 rxq->stats.rx_nombuf);
3866 #ifdef MLX4_PMD_SOFT_COUNTERS
3867 tmp.ipackets += rxq->stats.ipackets;
3868 tmp.ibytes += rxq->stats.ibytes;
3870 tmp.ierrors += rxq->stats.idropped;
3871 tmp.rx_nombuf += rxq->stats.rx_nombuf;
3873 for (i = 0; (i != priv->txqs_n); ++i) {
3874 struct txq *txq = (*priv->txqs)[i];
3878 idx = txq->stats.idx;
3879 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
3880 #ifdef MLX4_PMD_SOFT_COUNTERS
3881 tmp.q_opackets[idx] += txq->stats.opackets;
3882 tmp.q_obytes[idx] += txq->stats.obytes;
3884 tmp.q_errors[idx] += txq->stats.odropped;
3886 #ifdef MLX4_PMD_SOFT_COUNTERS
3887 tmp.opackets += txq->stats.opackets;
3888 tmp.obytes += txq->stats.obytes;
3890 tmp.oerrors += txq->stats.odropped;
3892 #ifndef MLX4_PMD_SOFT_COUNTERS
3893 /* FIXME: retrieve and add hardware counters. */
3900 * DPDK callback to clear device statistics.
3903 * Pointer to Ethernet device structure.
3906 mlx4_stats_reset(struct rte_eth_dev *dev)
3908 struct priv *priv = dev->data->dev_private;
3913 for (i = 0; (i != priv->rxqs_n); ++i) {
3914 if ((*priv->rxqs)[i] == NULL)
3916 idx = (*priv->rxqs)[i]->stats.idx;
3917 (*priv->rxqs)[i]->stats =
3918 (struct mlx4_rxq_stats){ .idx = idx };
3920 for (i = 0; (i != priv->txqs_n); ++i) {
3921 if ((*priv->txqs)[i] == NULL)
3923 idx = (*priv->rxqs)[i]->stats.idx;
3924 (*priv->txqs)[i]->stats =
3925 (struct mlx4_txq_stats){ .idx = idx };
3927 #ifndef MLX4_PMD_SOFT_COUNTERS
3928 /* FIXME: reset hardware counters. */
3934 * DPDK callback to remove a MAC address.
3937 * Pointer to Ethernet device structure.
3939 * MAC address index.
3942 mlx4_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
3944 struct priv *priv = dev->data->dev_private;
3947 DEBUG("%p: removing MAC address from index %" PRIu32,
3948 (void *)dev, index);
3949 if (index >= MLX4_MAX_MAC_ADDRESSES)
3951 /* Refuse to remove the broadcast address, this one is special. */
3952 if (!memcmp(priv->mac[index].addr_bytes, "\xff\xff\xff\xff\xff\xff",
3955 priv_mac_addr_del(priv, index);
3961 * DPDK callback to add a MAC address.
3964 * Pointer to Ethernet device structure.
3966 * MAC address to register.
3968 * MAC address index.
3970 * VMDq pool index to associate address with (ignored).
3973 mlx4_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
3974 uint32_t index, uint32_t vmdq)
3976 struct priv *priv = dev->data->dev_private;
3980 DEBUG("%p: adding MAC address at index %" PRIu32,
3981 (void *)dev, index);
3982 if (index >= MLX4_MAX_MAC_ADDRESSES)
3984 /* Refuse to add the broadcast address, this one is special. */
3985 if (!memcmp(mac_addr->addr_bytes, "\xff\xff\xff\xff\xff\xff",
3988 priv_mac_addr_add(priv, index,
3989 (const uint8_t (*)[ETHER_ADDR_LEN])
3990 mac_addr->addr_bytes);
3996 * DPDK callback to enable promiscuous mode.
3999 * Pointer to Ethernet device structure.
4002 mlx4_promiscuous_enable(struct rte_eth_dev *dev)
4004 struct priv *priv = dev->data->dev_private;
4009 if (priv->promisc) {
4013 /* If device isn't started, this is all we need to do. */
4017 ret = rxq_promiscuous_enable(&priv->rxq_parent);
4024 for (i = 0; (i != priv->rxqs_n); ++i) {
4025 if ((*priv->rxqs)[i] == NULL)
4027 ret = rxq_promiscuous_enable((*priv->rxqs)[i]);
4030 /* Failure, rollback. */
4032 if ((*priv->rxqs)[--i] != NULL)
4033 rxq_promiscuous_disable((*priv->rxqs)[i]);
4043 * DPDK callback to disable promiscuous mode.
4046 * Pointer to Ethernet device structure.
4049 mlx4_promiscuous_disable(struct rte_eth_dev *dev)
4051 struct priv *priv = dev->data->dev_private;
4055 if (!priv->promisc) {
4060 rxq_promiscuous_disable(&priv->rxq_parent);
4063 for (i = 0; (i != priv->rxqs_n); ++i)
4064 if ((*priv->rxqs)[i] != NULL)
4065 rxq_promiscuous_disable((*priv->rxqs)[i]);
4072 * DPDK callback to enable allmulti mode.
4075 * Pointer to Ethernet device structure.
4078 mlx4_allmulticast_enable(struct rte_eth_dev *dev)
4080 struct priv *priv = dev->data->dev_private;
4085 if (priv->allmulti) {
4089 /* If device isn't started, this is all we need to do. */
4093 ret = rxq_allmulticast_enable(&priv->rxq_parent);
4100 for (i = 0; (i != priv->rxqs_n); ++i) {
4101 if ((*priv->rxqs)[i] == NULL)
4103 ret = rxq_allmulticast_enable((*priv->rxqs)[i]);
4106 /* Failure, rollback. */
4108 if ((*priv->rxqs)[--i] != NULL)
4109 rxq_allmulticast_disable((*priv->rxqs)[i]);
4119 * DPDK callback to disable allmulti mode.
4122 * Pointer to Ethernet device structure.
4125 mlx4_allmulticast_disable(struct rte_eth_dev *dev)
4127 struct priv *priv = dev->data->dev_private;
4131 if (!priv->allmulti) {
4136 rxq_allmulticast_disable(&priv->rxq_parent);
4139 for (i = 0; (i != priv->rxqs_n); ++i)
4140 if ((*priv->rxqs)[i] != NULL)
4141 rxq_allmulticast_disable((*priv->rxqs)[i]);
4148 * DPDK callback to retrieve physical link information (unlocked version).
4151 * Pointer to Ethernet device structure.
4152 * @param wait_to_complete
4153 * Wait for request completion (ignored).
4156 mlx4_link_update_unlocked(struct rte_eth_dev *dev, int wait_to_complete)
4158 struct priv *priv = dev->data->dev_private;
4159 struct ethtool_cmd edata = {
4163 struct rte_eth_link dev_link;
4166 (void)wait_to_complete;
4167 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
4168 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
4171 memset(&dev_link, 0, sizeof(dev_link));
4172 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
4173 (ifr.ifr_flags & IFF_RUNNING));
4174 ifr.ifr_data = &edata;
4175 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4176 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
4180 link_speed = ethtool_cmd_speed(&edata);
4181 if (link_speed == -1)
4182 dev_link.link_speed = 0;
4184 dev_link.link_speed = link_speed;
4185 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
4186 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
4187 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
4188 /* Link status changed. */
4189 dev->data->dev_link = dev_link;
4192 /* Link status is still the same. */
4197 * DPDK callback to retrieve physical link information.
4200 * Pointer to Ethernet device structure.
4201 * @param wait_to_complete
4202 * Wait for request completion (ignored).
4205 mlx4_link_update(struct rte_eth_dev *dev, int wait_to_complete)
4207 struct priv *priv = dev->data->dev_private;
4211 ret = mlx4_link_update_unlocked(dev, wait_to_complete);
4217 * DPDK callback to change the MTU.
4219 * Setting the MTU affects hardware MRU (packets larger than the MTU cannot be
4220 * received). Use this as a hint to enable/disable scattered packets support
4221 * and improve performance when not needed.
4222 * Since failure is not an option, reconfiguring queues on the fly is not
4226 * Pointer to Ethernet device structure.
4231 * 0 on success, negative errno value on failure.
4234 mlx4_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
4236 struct priv *priv = dev->data->dev_private;
4239 uint16_t (*rx_func)(void *, struct rte_mbuf **, uint16_t) =
4243 /* Set kernel interface MTU first. */
4244 if (priv_set_mtu(priv, mtu)) {
4246 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
4250 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
4252 /* Temporarily replace RX handler with a fake one, assuming it has not
4253 * been copied elsewhere. */
4254 dev->rx_pkt_burst = removed_rx_burst;
4255 /* Make sure everyone has left mlx4_rx_burst() and uses
4256 * removed_rx_burst() instead. */
4259 /* Reconfigure each RX queue. */
4260 for (i = 0; (i != priv->rxqs_n); ++i) {
4261 struct rxq *rxq = (*priv->rxqs)[i];
4262 unsigned int max_frame_len;
4267 /* Calculate new maximum frame length according to MTU and
4268 * toggle scattered support (sp) if necessary. */
4269 max_frame_len = (priv->mtu + ETHER_HDR_LEN +
4270 (ETHER_MAX_VLAN_FRAME_LEN - ETHER_MAX_LEN));
4271 sp = (max_frame_len > (rxq->mb_len - RTE_PKTMBUF_HEADROOM));
4272 /* Provide new values to rxq_setup(). */
4273 dev->data->dev_conf.rxmode.jumbo_frame = sp;
4274 dev->data->dev_conf.rxmode.max_rx_pkt_len = max_frame_len;
4275 ret = rxq_rehash(dev, rxq);
4277 /* Force SP RX if that queue requires it and abort. */
4279 rx_func = mlx4_rx_burst_sp;
4282 /* Reenable non-RSS queue attributes. No need to check
4283 * for errors at this stage. */
4285 rxq_mac_addrs_add(rxq);
4287 rxq_promiscuous_enable(rxq);
4289 rxq_allmulticast_enable(rxq);
4291 /* Scattered burst function takes priority. */
4293 rx_func = mlx4_rx_burst_sp;
4295 /* Burst functions can now be called again. */
4297 dev->rx_pkt_burst = rx_func;
4305 * DPDK callback to get flow control status.
4308 * Pointer to Ethernet device structure.
4309 * @param[out] fc_conf
4310 * Flow control output buffer.
4313 * 0 on success, negative errno value on failure.
4316 mlx4_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4318 struct priv *priv = dev->data->dev_private;
4320 struct ethtool_pauseparam ethpause = {
4321 .cmd = ETHTOOL_GPAUSEPARAM
4325 ifr.ifr_data = ðpause;
4327 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4329 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
4335 fc_conf->autoneg = ethpause.autoneg;
4336 if (ethpause.rx_pause && ethpause.tx_pause)
4337 fc_conf->mode = RTE_FC_FULL;
4338 else if (ethpause.rx_pause)
4339 fc_conf->mode = RTE_FC_RX_PAUSE;
4340 else if (ethpause.tx_pause)
4341 fc_conf->mode = RTE_FC_TX_PAUSE;
4343 fc_conf->mode = RTE_FC_NONE;
4353 * DPDK callback to modify flow control parameters.
4356 * Pointer to Ethernet device structure.
4357 * @param[in] fc_conf
4358 * Flow control parameters.
4361 * 0 on success, negative errno value on failure.
4364 mlx4_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4366 struct priv *priv = dev->data->dev_private;
4368 struct ethtool_pauseparam ethpause = {
4369 .cmd = ETHTOOL_SPAUSEPARAM
4373 ifr.ifr_data = ðpause;
4374 ethpause.autoneg = fc_conf->autoneg;
4375 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4376 (fc_conf->mode & RTE_FC_RX_PAUSE))
4377 ethpause.rx_pause = 1;
4379 ethpause.rx_pause = 0;
4381 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4382 (fc_conf->mode & RTE_FC_TX_PAUSE))
4383 ethpause.tx_pause = 1;
4385 ethpause.tx_pause = 0;
4388 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4390 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
4404 * Configure a VLAN filter.
4407 * Pointer to Ethernet device structure.
4409 * VLAN ID to filter.
4414 * 0 on success, errno value on failure.
4417 vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
4419 struct priv *priv = dev->data->dev_private;
4421 unsigned int j = -1;
4423 DEBUG("%p: %s VLAN filter ID %" PRIu16,
4424 (void *)dev, (on ? "enable" : "disable"), vlan_id);
4425 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
4426 if (!priv->vlan_filter[i].enabled) {
4427 /* Unused index, remember it. */
4431 if (priv->vlan_filter[i].id != vlan_id)
4433 /* This VLAN ID is already known, use its index. */
4437 /* Check if there's room for another VLAN filter. */
4438 if (j == (unsigned int)-1)
4441 * VLAN filters apply to all configured MAC addresses, flow
4442 * specifications must be reconfigured accordingly.
4444 priv->vlan_filter[j].id = vlan_id;
4445 if ((on) && (!priv->vlan_filter[j].enabled)) {
4447 * Filter is disabled, enable it.
4448 * Rehashing flows in all RX queues is necessary.
4451 rxq_mac_addrs_del(&priv->rxq_parent);
4453 for (i = 0; (i != priv->rxqs_n); ++i)
4454 if ((*priv->rxqs)[i] != NULL)
4455 rxq_mac_addrs_del((*priv->rxqs)[i]);
4456 priv->vlan_filter[j].enabled = 1;
4457 if (priv->started) {
4459 rxq_mac_addrs_add(&priv->rxq_parent);
4461 for (i = 0; (i != priv->rxqs_n); ++i) {
4462 if ((*priv->rxqs)[i] == NULL)
4464 rxq_mac_addrs_add((*priv->rxqs)[i]);
4467 } else if ((!on) && (priv->vlan_filter[j].enabled)) {
4469 * Filter is enabled, disable it.
4470 * Rehashing flows in all RX queues is necessary.
4473 rxq_mac_addrs_del(&priv->rxq_parent);
4475 for (i = 0; (i != priv->rxqs_n); ++i)
4476 if ((*priv->rxqs)[i] != NULL)
4477 rxq_mac_addrs_del((*priv->rxqs)[i]);
4478 priv->vlan_filter[j].enabled = 0;
4479 if (priv->started) {
4481 rxq_mac_addrs_add(&priv->rxq_parent);
4483 for (i = 0; (i != priv->rxqs_n); ++i) {
4484 if ((*priv->rxqs)[i] == NULL)
4486 rxq_mac_addrs_add((*priv->rxqs)[i]);
4494 * DPDK callback to configure a VLAN filter.
4497 * Pointer to Ethernet device structure.
4499 * VLAN ID to filter.
4504 * 0 on success, negative errno value on failure.
4507 mlx4_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
4509 struct priv *priv = dev->data->dev_private;
4513 ret = vlan_filter_set(dev, vlan_id, on);
4519 static const struct eth_dev_ops mlx4_dev_ops = {
4520 .dev_configure = mlx4_dev_configure,
4521 .dev_start = mlx4_dev_start,
4522 .dev_stop = mlx4_dev_stop,
4523 .dev_close = mlx4_dev_close,
4524 .promiscuous_enable = mlx4_promiscuous_enable,
4525 .promiscuous_disable = mlx4_promiscuous_disable,
4526 .allmulticast_enable = mlx4_allmulticast_enable,
4527 .allmulticast_disable = mlx4_allmulticast_disable,
4528 .link_update = mlx4_link_update,
4529 .stats_get = mlx4_stats_get,
4530 .stats_reset = mlx4_stats_reset,
4531 .queue_stats_mapping_set = NULL,
4532 .dev_infos_get = mlx4_dev_infos_get,
4533 .vlan_filter_set = mlx4_vlan_filter_set,
4534 .vlan_tpid_set = NULL,
4535 .vlan_strip_queue_set = NULL,
4536 .vlan_offload_set = NULL,
4537 .rx_queue_setup = mlx4_rx_queue_setup,
4538 .tx_queue_setup = mlx4_tx_queue_setup,
4539 .rx_queue_release = mlx4_rx_queue_release,
4540 .tx_queue_release = mlx4_tx_queue_release,
4542 .dev_led_off = NULL,
4543 .flow_ctrl_get = mlx4_dev_get_flow_ctrl,
4544 .flow_ctrl_set = mlx4_dev_set_flow_ctrl,
4545 .priority_flow_ctrl_set = NULL,
4546 .mac_addr_remove = mlx4_mac_addr_remove,
4547 .mac_addr_add = mlx4_mac_addr_add,
4548 .mtu_set = mlx4_dev_set_mtu,
4549 .udp_tunnel_add = NULL,
4550 .udp_tunnel_del = NULL,
4551 .fdir_add_signature_filter = NULL,
4552 .fdir_update_signature_filter = NULL,
4553 .fdir_remove_signature_filter = NULL,
4554 .fdir_add_perfect_filter = NULL,
4555 .fdir_update_perfect_filter = NULL,
4556 .fdir_remove_perfect_filter = NULL,
4557 .fdir_set_masks = NULL
4561 * Get PCI information from struct ibv_device.
4564 * Pointer to Ethernet device structure.
4565 * @param[out] pci_addr
4566 * PCI bus address output buffer.
4569 * 0 on success, -1 on failure and errno is set.
4572 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
4573 struct rte_pci_addr *pci_addr)
4577 MKSTR(path, "%s/device/uevent", device->ibdev_path);
4579 file = fopen(path, "rb");
4582 while (fgets(line, sizeof(line), file) == line) {
4583 size_t len = strlen(line);
4586 /* Truncate long lines. */
4587 if (len == (sizeof(line) - 1))
4588 while (line[(len - 1)] != '\n') {
4592 line[(len - 1)] = ret;
4594 /* Extract information. */
4597 "%" SCNx16 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
4601 &pci_addr->function) == 4) {
4611 * Get MAC address by querying netdevice.
4614 * struct priv for the requested device.
4616 * MAC address output buffer.
4619 * 0 on success, -1 on failure and errno is set.
4622 priv_get_mac(struct priv *priv, uint8_t (*mac)[ETHER_ADDR_LEN])
4624 struct ifreq request;
4626 if (priv_ifreq(priv, SIOCGIFHWADDR, &request))
4628 memcpy(mac, request.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
4632 /* Support up to 32 adapters. */
4634 struct rte_pci_addr pci_addr; /* associated PCI address */
4635 uint32_t ports; /* physical ports bitfield. */
4639 * Get device index in mlx4_dev[] from PCI bus address.
4641 * @param[in] pci_addr
4642 * PCI bus address to look for.
4645 * mlx4_dev[] index on success, -1 on failure.
4648 mlx4_dev_idx(struct rte_pci_addr *pci_addr)
4653 assert(pci_addr != NULL);
4654 for (i = 0; (i != elemof(mlx4_dev)); ++i) {
4655 if ((mlx4_dev[i].pci_addr.domain == pci_addr->domain) &&
4656 (mlx4_dev[i].pci_addr.bus == pci_addr->bus) &&
4657 (mlx4_dev[i].pci_addr.devid == pci_addr->devid) &&
4658 (mlx4_dev[i].pci_addr.function == pci_addr->function))
4660 if ((mlx4_dev[i].ports == 0) && (ret == -1))
4667 * Retrieve integer value from environment variable.
4670 * Environment variable name.
4673 * Integer value, 0 if the variable is not set.
4676 mlx4_getenv_int(const char *name)
4678 const char *val = getenv(name);
4685 static struct eth_driver mlx4_driver;
4688 * DPDK callback to register a PCI device.
4690 * This function creates an Ethernet device for each port of a given
4693 * @param[in] pci_drv
4694 * PCI driver structure (mlx4_driver).
4695 * @param[in] pci_dev
4696 * PCI device information.
4699 * 0 on success, negative errno value on failure.
4702 mlx4_pci_devinit(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
4704 struct ibv_device **list;
4705 struct ibv_device *ibv_dev;
4707 struct ibv_context *attr_ctx = NULL;
4708 struct ibv_device_attr device_attr;
4714 assert(pci_drv == &mlx4_driver.pci_drv);
4715 /* Get mlx4_dev[] index. */
4716 idx = mlx4_dev_idx(&pci_dev->addr);
4718 ERROR("this driver cannot support any more adapters");
4721 DEBUG("using driver device index %d", idx);
4723 /* Save PCI address. */
4724 mlx4_dev[idx].pci_addr = pci_dev->addr;
4725 list = ibv_get_device_list(&i);
4728 if (errno == ENOSYS) {
4729 WARN("cannot list devices, is ib_uverbs loaded?");
4736 * For each listed device, check related sysfs entry against
4737 * the provided PCI ID.
4740 struct rte_pci_addr pci_addr;
4743 DEBUG("checking device \"%s\"", list[i]->name);
4744 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
4746 if ((pci_dev->addr.domain != pci_addr.domain) ||
4747 (pci_dev->addr.bus != pci_addr.bus) ||
4748 (pci_dev->addr.devid != pci_addr.devid) ||
4749 (pci_dev->addr.function != pci_addr.function))
4751 vf = (pci_dev->id.device_id ==
4752 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
4753 INFO("PCI information matches, using device \"%s\" (VF: %s)",
4754 list[i]->name, (vf ? "true" : "false"));
4755 attr_ctx = ibv_open_device(list[i]);
4759 if (attr_ctx == NULL) {
4760 ibv_free_device_list(list);
4763 WARN("cannot access device, is mlx4_ib loaded?");
4766 WARN("cannot use device, are drivers up to date?");
4774 DEBUG("device opened");
4775 if (ibv_query_device(attr_ctx, &device_attr))
4777 INFO("%u port(s) detected", device_attr.phys_port_cnt);
4779 for (i = 0; i < device_attr.phys_port_cnt; i++) {
4780 uint32_t port = i + 1; /* ports are indexed from one */
4781 uint32_t test = (1 << i);
4782 struct ibv_context *ctx = NULL;
4783 struct ibv_port_attr port_attr;
4784 struct ibv_pd *pd = NULL;
4785 struct priv *priv = NULL;
4786 struct rte_eth_dev *eth_dev;
4787 #ifdef HAVE_EXP_QUERY_DEVICE
4788 struct ibv_exp_device_attr exp_device_attr;
4789 #endif /* HAVE_EXP_QUERY_DEVICE */
4790 struct ether_addr mac;
4792 #ifdef HAVE_EXP_QUERY_DEVICE
4793 exp_device_attr.comp_mask = IBV_EXP_DEVICE_ATTR_EXP_CAP_FLAGS;
4795 exp_device_attr.comp_mask |= IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ;
4796 #endif /* RSS_SUPPORT */
4797 #endif /* HAVE_EXP_QUERY_DEVICE */
4799 DEBUG("using port %u (%08" PRIx32 ")", port, test);
4801 ctx = ibv_open_device(ibv_dev);
4805 /* Check port status. */
4806 err = ibv_query_port(ctx, port, &port_attr);
4808 ERROR("port query failed: %s", strerror(err));
4811 if (port_attr.state != IBV_PORT_ACTIVE)
4812 WARN("bad state for port %d: \"%s\" (%d)",
4813 port, ibv_port_state_str(port_attr.state),
4816 /* Allocate protection domain. */
4817 pd = ibv_alloc_pd(ctx);
4819 ERROR("PD allocation failure");
4824 mlx4_dev[idx].ports |= test;
4826 /* from rte_ethdev.c */
4827 priv = rte_zmalloc("ethdev private structure",
4829 RTE_CACHE_LINE_SIZE);
4831 ERROR("priv allocation failure");
4837 priv->device_attr = device_attr;
4840 priv->mtu = ETHER_MTU;
4841 #ifdef HAVE_EXP_QUERY_DEVICE
4842 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
4843 ERROR("ibv_exp_query_device() failed");
4847 if ((exp_device_attr.exp_device_cap_flags &
4848 IBV_EXP_DEVICE_QPG) &&
4849 (exp_device_attr.exp_device_cap_flags &
4850 IBV_EXP_DEVICE_UD_RSS) &&
4851 (exp_device_attr.comp_mask &
4852 IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ) &&
4853 (exp_device_attr.max_rss_tbl_sz > 0)) {
4856 priv->max_rss_tbl_sz = exp_device_attr.max_rss_tbl_sz;
4860 priv->max_rss_tbl_sz = 0;
4862 priv->hw_tss = !!(exp_device_attr.exp_device_cap_flags &
4863 IBV_EXP_DEVICE_UD_TSS);
4864 DEBUG("device flags: %s%s%s",
4865 (priv->hw_qpg ? "IBV_DEVICE_QPG " : ""),
4866 (priv->hw_tss ? "IBV_DEVICE_TSS " : ""),
4867 (priv->hw_rss ? "IBV_DEVICE_RSS " : ""));
4869 DEBUG("maximum RSS indirection table size: %u",
4870 exp_device_attr.max_rss_tbl_sz);
4871 #endif /* RSS_SUPPORT */
4874 ((exp_device_attr.exp_device_cap_flags &
4875 IBV_EXP_DEVICE_RX_CSUM_TCP_UDP_PKT) &&
4876 (exp_device_attr.exp_device_cap_flags &
4877 IBV_EXP_DEVICE_RX_CSUM_IP_PKT));
4878 DEBUG("checksum offloading is %ssupported",
4879 (priv->hw_csum ? "" : "not "));
4881 priv->hw_csum_l2tun = !!(exp_device_attr.exp_device_cap_flags &
4882 IBV_EXP_DEVICE_VXLAN_SUPPORT);
4883 DEBUG("L2 tunnel checksum offloads are %ssupported",
4884 (priv->hw_csum_l2tun ? "" : "not "));
4887 priv->inl_recv_size = mlx4_getenv_int("MLX4_INLINE_RECV_SIZE");
4889 if (priv->inl_recv_size) {
4890 exp_device_attr.comp_mask =
4891 IBV_EXP_DEVICE_ATTR_INLINE_RECV_SZ;
4892 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
4893 INFO("Couldn't query device for inline-receive"
4895 priv->inl_recv_size = 0;
4897 if ((unsigned)exp_device_attr.inline_recv_sz <
4898 priv->inl_recv_size) {
4899 INFO("Max inline-receive (%d) <"
4900 " requested inline-receive (%u)",
4901 exp_device_attr.inline_recv_sz,
4902 priv->inl_recv_size);
4903 priv->inl_recv_size =
4904 exp_device_attr.inline_recv_sz;
4907 INFO("Set inline receive size to %u",
4908 priv->inl_recv_size);
4910 #endif /* INLINE_RECV */
4911 #endif /* HAVE_EXP_QUERY_DEVICE */
4913 (void)mlx4_getenv_int;
4915 /* Configure the first MAC address by default. */
4916 if (priv_get_mac(priv, &mac.addr_bytes)) {
4917 ERROR("cannot get MAC address, is mlx4_en loaded?"
4918 " (errno: %s)", strerror(errno));
4921 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
4923 mac.addr_bytes[0], mac.addr_bytes[1],
4924 mac.addr_bytes[2], mac.addr_bytes[3],
4925 mac.addr_bytes[4], mac.addr_bytes[5]);
4926 /* Register MAC and broadcast addresses. */
4927 claim_zero(priv_mac_addr_add(priv, 0,
4928 (const uint8_t (*)[ETHER_ADDR_LEN])
4930 claim_zero(priv_mac_addr_add(priv, 1,
4931 &(const uint8_t [ETHER_ADDR_LEN])
4932 { "\xff\xff\xff\xff\xff\xff" }));
4935 char ifname[IF_NAMESIZE];
4937 if (priv_get_ifname(priv, &ifname) == 0)
4938 DEBUG("port %u ifname is \"%s\"",
4939 priv->port, ifname);
4941 DEBUG("port %u ifname is unknown", priv->port);
4944 /* Get actual MTU if possible. */
4945 priv_get_mtu(priv, &priv->mtu);
4946 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
4948 /* from rte_ethdev.c */
4950 char name[RTE_ETH_NAME_MAX_LEN];
4952 snprintf(name, sizeof(name), "%s port %u",
4953 ibv_get_device_name(ibv_dev), port);
4954 eth_dev = rte_eth_dev_allocate(name, RTE_ETH_DEV_PCI);
4956 if (eth_dev == NULL) {
4957 ERROR("can not allocate rte ethdev");
4962 eth_dev->data->dev_private = priv;
4963 eth_dev->pci_dev = pci_dev;
4964 eth_dev->driver = &mlx4_driver;
4965 eth_dev->data->rx_mbuf_alloc_failed = 0;
4966 eth_dev->data->mtu = ETHER_MTU;
4968 priv->dev = eth_dev;
4969 eth_dev->dev_ops = &mlx4_dev_ops;
4970 eth_dev->data->mac_addrs = priv->mac;
4972 /* Bring Ethernet device up. */
4973 DEBUG("forcing Ethernet interface up");
4974 priv_set_flags(priv, ~IFF_UP, IFF_UP);
4980 claim_zero(ibv_dealloc_pd(pd));
4982 claim_zero(ibv_close_device(ctx));
4987 * XXX if something went wrong in the loop above, there is a resource
4988 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
4989 * long as the dpdk does not provide a way to deallocate a ethdev and a
4990 * way to enumerate the registered ethdevs to free the previous ones.
4993 /* no port found, complain */
4994 if (!mlx4_dev[idx].ports) {
5001 claim_zero(ibv_close_device(attr_ctx));
5003 ibv_free_device_list(list);
5008 static const struct rte_pci_id mlx4_pci_id_map[] = {
5010 .vendor_id = PCI_VENDOR_ID_MELLANOX,
5011 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3,
5012 .subsystem_vendor_id = PCI_ANY_ID,
5013 .subsystem_device_id = PCI_ANY_ID
5016 .vendor_id = PCI_VENDOR_ID_MELLANOX,
5017 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO,
5018 .subsystem_vendor_id = PCI_ANY_ID,
5019 .subsystem_device_id = PCI_ANY_ID
5022 .vendor_id = PCI_VENDOR_ID_MELLANOX,
5023 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3VF,
5024 .subsystem_vendor_id = PCI_ANY_ID,
5025 .subsystem_device_id = PCI_ANY_ID
5032 static struct eth_driver mlx4_driver = {
5034 .name = MLX4_DRIVER_NAME,
5035 .id_table = mlx4_pci_id_map,
5036 .devinit = mlx4_pci_devinit,
5038 .dev_private_size = sizeof(struct priv)
5042 * Driver initialization routine.
5045 rte_mlx4_pmd_init(const char *name, const char *args)
5050 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
5051 * huge pages. Calling ibv_fork_init() during init allows
5052 * applications to use fork() safely for purposes other than
5053 * using this PMD, which is not supported in forked processes.
5055 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
5057 rte_eal_pci_register(&mlx4_driver.pci_drv);
5061 static struct rte_driver rte_mlx4_driver = {
5063 .name = MLX4_DRIVER_NAME,
5064 .init = rte_mlx4_pmd_init,
5067 PMD_REGISTER_DRIVER(rte_mlx4_driver)