4 * Copyright 2012-2015 6WIND S.A.
5 * Copyright 2012 Mellanox.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of 6WIND S.A. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * - RSS hash key and options cannot be modified.
37 * - Hardware counters aren't implemented.
51 #include <arpa/inet.h>
54 #include <sys/ioctl.h>
55 #include <sys/socket.h>
56 #include <netinet/in.h>
58 #include <linux/ethtool.h>
59 #include <linux/sockios.h>
62 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
64 #pragma GCC diagnostic ignored "-pedantic"
66 #include <infiniband/verbs.h>
68 #pragma GCC diagnostic error "-pedantic"
71 /* DPDK headers don't like -pedantic. */
73 #pragma GCC diagnostic ignored "-pedantic"
75 #include <rte_config.h>
76 #include <rte_ether.h>
77 #include <rte_ethdev.h>
80 #include <rte_errno.h>
81 #include <rte_mempool.h>
82 #include <rte_prefetch.h>
83 #include <rte_malloc.h>
84 #include <rte_spinlock.h>
85 #include <rte_atomic.h>
86 #include <rte_version.h>
89 #pragma GCC diagnostic error "-pedantic"
92 /* Generated configuration header. */
93 #include "mlx4_autoconf.h"
98 /* Runtime logging through RTE_LOG() is enabled when not in debugging mode.
99 * Intermediate LOG_*() macros add the required end-of-line characters. */
101 #define INFO(...) DEBUG(__VA_ARGS__)
102 #define WARN(...) DEBUG(__VA_ARGS__)
103 #define ERROR(...) DEBUG(__VA_ARGS__)
105 #define LOG__(level, m, ...) \
106 RTE_LOG(level, PMD, MLX4_DRIVER_NAME ": " m "%c", __VA_ARGS__)
107 #define LOG_(level, ...) LOG__(level, __VA_ARGS__, '\n')
108 #define INFO(...) LOG_(INFO, __VA_ARGS__)
109 #define WARN(...) LOG_(WARNING, __VA_ARGS__)
110 #define ERROR(...) LOG_(ERR, __VA_ARGS__)
113 /* Convenience macros for accessing mbuf fields. */
114 #define NEXT(m) ((m)->next)
115 #define DATA_LEN(m) ((m)->data_len)
116 #define PKT_LEN(m) ((m)->pkt_len)
117 #define DATA_OFF(m) ((m)->data_off)
118 #define SET_DATA_OFF(m, o) ((m)->data_off = (o))
119 #define NB_SEGS(m) ((m)->nb_segs)
120 #define PORT(m) ((m)->port)
122 /* Work Request ID data type (64 bit). */
131 #define WR_ID(o) (((wr_id_t *)&(o))->data)
133 /* Compile-time check. */
134 static inline void wr_id_t_check(void)
136 wr_id_t check[1 + (2 * -!(sizeof(wr_id_t) == sizeof(uint64_t)))];
142 struct mlx4_rxq_stats {
143 unsigned int idx; /**< Mapping index. */
144 #ifdef MLX4_PMD_SOFT_COUNTERS
145 uint64_t ipackets; /**< Total of successfully received packets. */
146 uint64_t ibytes; /**< Total of successfully received bytes. */
148 uint64_t idropped; /**< Total of packets dropped when RX ring full. */
149 uint64_t rx_nombuf; /**< Total of RX mbuf allocation failures. */
152 struct mlx4_txq_stats {
153 unsigned int idx; /**< Mapping index. */
154 #ifdef MLX4_PMD_SOFT_COUNTERS
155 uint64_t opackets; /**< Total of successfully sent packets. */
156 uint64_t obytes; /**< Total of successfully sent bytes. */
158 uint64_t odropped; /**< Total of packets not sent when TX ring full. */
161 /* RX element (scattered packets). */
163 struct ibv_recv_wr wr; /* Work Request. */
164 struct ibv_sge sges[MLX4_PMD_SGE_WR_N]; /* Scatter/Gather Elements. */
165 struct rte_mbuf *bufs[MLX4_PMD_SGE_WR_N]; /* SGEs buffers. */
170 struct ibv_recv_wr wr; /* Work Request. */
171 struct ibv_sge sge; /* Scatter/Gather Element. */
172 /* mbuf pointer is derived from WR_ID(wr.wr_id).offset. */
175 /* RX queue descriptor. */
177 struct priv *priv; /* Back pointer to private data. */
178 struct rte_mempool *mp; /* Memory Pool for allocations. */
179 struct ibv_mr *mr; /* Memory Region (for mp). */
180 struct ibv_cq *cq; /* Completion Queue. */
181 struct ibv_qp *qp; /* Queue Pair. */
182 struct ibv_exp_qp_burst_family *if_qp; /* QP burst interface. */
183 struct ibv_exp_cq_family *if_cq; /* CQ interface. */
185 * Each VLAN ID requires a separate flow steering rule.
187 BITFIELD_DECLARE(mac_configured, uint32_t, MLX4_MAX_MAC_ADDRESSES);
188 struct ibv_flow *mac_flow[MLX4_MAX_MAC_ADDRESSES][MLX4_MAX_VLAN_IDS];
189 struct ibv_flow *promisc_flow; /* Promiscuous flow. */
190 struct ibv_flow *allmulti_flow; /* Multicast flow. */
191 unsigned int port_id; /* Port ID for incoming packets. */
192 unsigned int elts_n; /* (*elts)[] length. */
193 unsigned int elts_head; /* Current index in (*elts)[]. */
195 struct rxq_elt_sp (*sp)[]; /* Scattered RX elements. */
196 struct rxq_elt (*no_sp)[]; /* RX elements. */
198 unsigned int sp:1; /* Use scattered RX elements. */
199 uint32_t mb_len; /* Length of a mp-issued mbuf. */
200 struct mlx4_rxq_stats stats; /* RX queue counters. */
201 unsigned int socket; /* CPU socket ID for allocations. */
206 struct ibv_send_wr wr; /* Work Request. */
207 struct ibv_sge sges[MLX4_PMD_SGE_WR_N]; /* Scatter/Gather Elements. */
208 /* mbuf pointer is derived from WR_ID(wr.wr_id).offset. */
211 /* Linear buffer type. It is used when transmitting buffers with too many
212 * segments that do not fit the hardware queue (see max_send_sge).
213 * Extra segments are copied (linearized) in such buffers, replacing the
214 * last SGE during TX.
215 * The size is arbitrary but large enough to hold a jumbo frame with
216 * 8 segments considering mbuf.buf_len is about 2048 bytes. */
217 typedef uint8_t linear_t[16384];
219 /* TX queue descriptor. */
221 struct priv *priv; /* Back pointer to private data. */
223 struct rte_mempool *mp; /* Cached Memory Pool. */
224 struct ibv_mr *mr; /* Memory Region (for mp). */
225 uint32_t lkey; /* mr->lkey */
226 } mp2mr[MLX4_PMD_TX_MP_CACHE]; /* MP to MR translation table. */
227 struct ibv_cq *cq; /* Completion Queue. */
228 struct ibv_qp *qp; /* Queue Pair. */
229 struct ibv_exp_qp_burst_family *if_qp; /* QP burst interface. */
230 struct ibv_exp_cq_family *if_cq; /* CQ interface. */
231 #if MLX4_PMD_MAX_INLINE > 0
232 uint32_t max_inline; /* Max inline send size <= MLX4_PMD_MAX_INLINE. */
234 unsigned int elts_n; /* (*elts)[] length. */
235 struct txq_elt (*elts)[]; /* TX elements. */
236 unsigned int elts_head; /* Current index in (*elts)[]. */
237 unsigned int elts_tail; /* First element awaiting completion. */
238 unsigned int elts_comp; /* Number of completion requests. */
239 unsigned int elts_comp_cd; /* Countdown for next completion request. */
240 unsigned int elts_comp_cd_init; /* Initial value for countdown. */
241 struct mlx4_txq_stats stats; /* TX queue counters. */
242 linear_t (*elts_linear)[]; /* Linearized buffers. */
243 struct ibv_mr *mr_linear; /* Memory Region for linearized buffers. */
244 unsigned int socket; /* CPU socket ID for allocations. */
248 struct rte_eth_dev *dev; /* Ethernet device. */
249 struct ibv_context *ctx; /* Verbs context. */
250 struct ibv_device_attr device_attr; /* Device properties. */
251 struct ibv_pd *pd; /* Protection Domain. */
253 * MAC addresses array and configuration bit-field.
254 * An extra entry that cannot be modified by the DPDK is reserved
255 * for broadcast frames (destination MAC address ff:ff:ff:ff:ff:ff).
257 struct ether_addr mac[MLX4_MAX_MAC_ADDRESSES];
258 BITFIELD_DECLARE(mac_configured, uint32_t, MLX4_MAX_MAC_ADDRESSES);
261 unsigned int enabled:1; /* If enabled. */
262 unsigned int id:12; /* VLAN ID (0-4095). */
263 } vlan_filter[MLX4_MAX_VLAN_IDS]; /* VLAN filters table. */
264 /* Device properties. */
265 uint16_t mtu; /* Configured MTU. */
266 uint8_t port; /* Physical port number. */
267 unsigned int started:1; /* Device started, flows enabled. */
268 unsigned int promisc:1; /* Device in promiscuous mode. */
269 unsigned int allmulti:1; /* Device receives all multicast packets. */
270 unsigned int hw_qpg:1; /* QP groups are supported. */
271 unsigned int hw_tss:1; /* TSS is supported. */
272 unsigned int hw_rss:1; /* RSS is supported. */
273 unsigned int rss:1; /* RSS is enabled. */
274 unsigned int vf:1; /* This is a VF device. */
276 unsigned int inl_recv_size; /* Inline recv size */
278 unsigned int max_rss_tbl_sz; /* Maximum number of RSS queues. */
280 struct rxq rxq_parent; /* Parent queue when RSS is enabled. */
281 unsigned int rxqs_n; /* RX queues array size. */
282 unsigned int txqs_n; /* TX queues array size. */
283 struct rxq *(*rxqs)[]; /* RX queues. */
284 struct txq *(*txqs)[]; /* TX queues. */
285 rte_spinlock_t lock; /* Lock for control functions. */
289 * Lock private structure to protect it from concurrent access in the
293 * Pointer to private structure.
296 priv_lock(struct priv *priv)
298 rte_spinlock_lock(&priv->lock);
302 * Unlock private structure.
305 * Pointer to private structure.
308 priv_unlock(struct priv *priv)
310 rte_spinlock_unlock(&priv->lock);
313 /* Allocate a buffer on the stack and fill it with a printf format string. */
314 #define MKSTR(name, ...) \
315 char name[snprintf(NULL, 0, __VA_ARGS__) + 1]; \
317 snprintf(name, sizeof(name), __VA_ARGS__)
320 * Get interface name from private structure.
323 * Pointer to private structure.
325 * Interface name output buffer.
328 * 0 on success, -1 on failure and errno is set.
331 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
335 unsigned int dev_type = 0;
336 unsigned int dev_port_prev = ~0u;
337 char match[IF_NAMESIZE] = "";
340 MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
346 while ((dent = readdir(dir)) != NULL) {
347 char *name = dent->d_name;
349 unsigned int dev_port;
352 if ((name[0] == '.') &&
353 ((name[1] == '\0') ||
354 ((name[1] == '.') && (name[2] == '\0'))))
357 MKSTR(path, "%s/device/net/%s/%s",
358 priv->ctx->device->ibdev_path, name,
359 (dev_type ? "dev_id" : "dev_port"));
361 file = fopen(path, "rb");
366 * Switch to dev_id when dev_port does not exist as
367 * is the case with Linux kernel versions < 3.15.
378 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
383 * Switch to dev_id when dev_port returns the same value for
384 * all ports. May happen when using a MOFED release older than
385 * 3.0 with a Linux kernel >= 3.15.
387 if (dev_port == dev_port_prev)
389 dev_port_prev = dev_port;
390 if (dev_port == (priv->port - 1u))
391 snprintf(match, sizeof(match), "%s", name);
394 if (match[0] == '\0')
396 strncpy(*ifname, match, sizeof(*ifname));
401 * Read from sysfs entry.
404 * Pointer to private structure.
406 * Entry name relative to sysfs path.
408 * Data output buffer.
413 * 0 on success, -1 on failure and errno is set.
416 priv_sysfs_read(const struct priv *priv, const char *entry,
417 char *buf, size_t size)
419 char ifname[IF_NAMESIZE];
424 if (priv_get_ifname(priv, &ifname))
427 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
430 file = fopen(path, "rb");
433 ret = fread(buf, 1, size, file);
435 if (((size_t)ret < size) && (ferror(file)))
445 * Write to sysfs entry.
448 * Pointer to private structure.
450 * Entry name relative to sysfs path.
457 * 0 on success, -1 on failure and errno is set.
460 priv_sysfs_write(const struct priv *priv, const char *entry,
461 char *buf, size_t size)
463 char ifname[IF_NAMESIZE];
468 if (priv_get_ifname(priv, &ifname))
471 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
474 file = fopen(path, "wb");
477 ret = fwrite(buf, 1, size, file);
479 if (((size_t)ret < size) || (ferror(file)))
489 * Get unsigned long sysfs property.
492 * Pointer to private structure.
494 * Entry name relative to sysfs path.
496 * Value output buffer.
499 * 0 on success, -1 on failure and errno is set.
502 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
505 unsigned long value_ret;
508 ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
510 DEBUG("cannot read %s value from sysfs: %s",
511 name, strerror(errno));
514 value_str[ret] = '\0';
516 value_ret = strtoul(value_str, NULL, 0);
518 DEBUG("invalid %s value `%s': %s", name, value_str,
527 * Set unsigned long sysfs property.
530 * Pointer to private structure.
532 * Entry name relative to sysfs path.
537 * 0 on success, -1 on failure and errno is set.
540 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
543 MKSTR(value_str, "%lu", value);
545 ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
547 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
548 name, value_str, value, strerror(errno));
555 * Perform ifreq ioctl() on associated Ethernet device.
558 * Pointer to private structure.
560 * Request number to pass to ioctl().
562 * Interface request structure output buffer.
565 * 0 on success, -1 on failure and errno is set.
568 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
570 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
575 if (priv_get_ifname(priv, &ifr->ifr_name) == 0)
576 ret = ioctl(sock, req, ifr);
585 * Pointer to private structure.
587 * MTU value output buffer.
590 * 0 on success, -1 on failure and errno is set.
593 priv_get_mtu(struct priv *priv, uint16_t *mtu)
595 unsigned long ulong_mtu;
597 if (priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu) == -1)
607 * Pointer to private structure.
612 * 0 on success, -1 on failure and errno is set.
615 priv_set_mtu(struct priv *priv, uint16_t mtu)
617 return priv_set_sysfs_ulong(priv, "mtu", mtu);
624 * Pointer to private structure.
626 * Bitmask for flags that must remain untouched.
628 * Bitmask for flags to modify.
631 * 0 on success, -1 on failure and errno is set.
634 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
638 if (priv_get_sysfs_ulong(priv, "flags", &tmp) == -1)
642 return priv_set_sysfs_ulong(priv, "flags", tmp);
645 /* Device configuration. */
648 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
649 unsigned int socket, const struct rte_eth_rxconf *conf,
650 struct rte_mempool *mp);
653 rxq_cleanup(struct rxq *rxq);
656 * Ethernet device configuration.
658 * Prepare the driver for a given number of TX and RX queues.
659 * Allocate parent RSS queue when several RX queues are requested.
662 * Pointer to Ethernet device structure.
665 * 0 on success, errno value on failure.
668 dev_configure(struct rte_eth_dev *dev)
670 struct priv *priv = dev->data->dev_private;
671 unsigned int rxqs_n = dev->data->nb_rx_queues;
672 unsigned int txqs_n = dev->data->nb_tx_queues;
676 priv->rxqs = (void *)dev->data->rx_queues;
677 priv->txqs = (void *)dev->data->tx_queues;
678 if (txqs_n != priv->txqs_n) {
679 INFO("%p: TX queues number update: %u -> %u",
680 (void *)dev, priv->txqs_n, txqs_n);
681 priv->txqs_n = txqs_n;
683 if (rxqs_n == priv->rxqs_n)
685 INFO("%p: RX queues number update: %u -> %u",
686 (void *)dev, priv->rxqs_n, rxqs_n);
687 /* If RSS is enabled, disable it first. */
691 /* Only if there are no remaining child RX queues. */
692 for (i = 0; (i != priv->rxqs_n); ++i)
693 if ((*priv->rxqs)[i] != NULL)
695 rxq_cleanup(&priv->rxq_parent);
700 /* Nothing else to do. */
701 priv->rxqs_n = rxqs_n;
704 /* Allocate a new RSS parent queue if supported by hardware. */
706 ERROR("%p: only a single RX queue can be configured when"
707 " hardware doesn't support RSS",
711 /* Fail if hardware doesn't support that many RSS queues. */
712 if (rxqs_n >= priv->max_rss_tbl_sz) {
713 ERROR("%p: only %u RX queues can be configured for RSS",
714 (void *)dev, priv->max_rss_tbl_sz);
719 priv->rxqs_n = rxqs_n;
720 ret = rxq_setup(dev, &priv->rxq_parent, 0, 0, NULL, NULL);
723 /* Failure, rollback. */
731 * DPDK callback for Ethernet device configuration.
734 * Pointer to Ethernet device structure.
737 * 0 on success, negative errno value on failure.
740 mlx4_dev_configure(struct rte_eth_dev *dev)
742 struct priv *priv = dev->data->dev_private;
746 ret = dev_configure(dev);
752 /* TX queues handling. */
755 * Allocate TX queue elements.
758 * Pointer to TX queue structure.
760 * Number of elements to allocate.
763 * 0 on success, errno value on failure.
766 txq_alloc_elts(struct txq *txq, unsigned int elts_n)
769 struct txq_elt (*elts)[elts_n] =
770 rte_calloc_socket("TXQ", 1, sizeof(*elts), 0, txq->socket);
771 linear_t (*elts_linear)[elts_n] =
772 rte_calloc_socket("TXQ", 1, sizeof(*elts_linear), 0,
774 struct ibv_mr *mr_linear = NULL;
777 if ((elts == NULL) || (elts_linear == NULL)) {
778 ERROR("%p: can't allocate packets array", (void *)txq);
783 ibv_reg_mr(txq->priv->pd, elts_linear, sizeof(*elts_linear),
784 (IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE));
785 if (mr_linear == NULL) {
786 ERROR("%p: unable to configure MR, ibv_reg_mr() failed",
791 for (i = 0; (i != elts_n); ++i) {
792 struct txq_elt *elt = &(*elts)[i];
793 struct ibv_send_wr *wr = &elt->wr;
796 WR_ID(wr->wr_id).id = i;
797 WR_ID(wr->wr_id).offset = 0;
798 wr->sg_list = &elt->sges[0];
799 wr->opcode = IBV_WR_SEND;
800 /* Other fields are updated during TX. */
802 DEBUG("%p: allocated and configured %u WRs", (void *)txq, elts_n);
803 txq->elts_n = elts_n;
808 /* Request send completion every MLX4_PMD_TX_PER_COMP_REQ packets or
809 * at least 4 times per ring. */
810 txq->elts_comp_cd_init =
811 ((MLX4_PMD_TX_PER_COMP_REQ < (elts_n / 4)) ?
812 MLX4_PMD_TX_PER_COMP_REQ : (elts_n / 4));
813 txq->elts_comp_cd = txq->elts_comp_cd_init;
814 txq->elts_linear = elts_linear;
815 txq->mr_linear = mr_linear;
819 if (mr_linear != NULL)
820 claim_zero(ibv_dereg_mr(mr_linear));
822 rte_free(elts_linear);
825 DEBUG("%p: failed, freed everything", (void *)txq);
831 * Free TX queue elements.
834 * Pointer to TX queue structure.
837 txq_free_elts(struct txq *txq)
840 unsigned int elts_n = txq->elts_n;
841 struct txq_elt (*elts)[elts_n] = txq->elts;
842 linear_t (*elts_linear)[elts_n] = txq->elts_linear;
843 struct ibv_mr *mr_linear = txq->mr_linear;
845 DEBUG("%p: freeing WRs", (void *)txq);
848 txq->elts_linear = NULL;
849 txq->mr_linear = NULL;
850 if (mr_linear != NULL)
851 claim_zero(ibv_dereg_mr(mr_linear));
853 rte_free(elts_linear);
856 for (i = 0; (i != elemof(*elts)); ++i) {
857 struct txq_elt *elt = &(*elts)[i];
859 if (WR_ID(elt->wr.wr_id).offset == 0)
861 rte_pktmbuf_free((void *)((uintptr_t)elt->sges[0].addr -
862 WR_ID(elt->wr.wr_id).offset));
869 * Clean up a TX queue.
871 * Destroy objects, free allocated memory and reset the structure for reuse.
874 * Pointer to TX queue structure.
877 txq_cleanup(struct txq *txq)
879 struct ibv_exp_release_intf_params params;
882 DEBUG("cleaning up %p", (void *)txq);
884 if (txq->if_qp != NULL) {
885 assert(txq->priv != NULL);
886 assert(txq->priv->ctx != NULL);
887 assert(txq->qp != NULL);
888 params = (struct ibv_exp_release_intf_params){
891 claim_zero(ibv_exp_release_intf(txq->priv->ctx,
895 if (txq->if_cq != NULL) {
896 assert(txq->priv != NULL);
897 assert(txq->priv->ctx != NULL);
898 assert(txq->cq != NULL);
899 params = (struct ibv_exp_release_intf_params){
902 claim_zero(ibv_exp_release_intf(txq->priv->ctx,
907 claim_zero(ibv_destroy_qp(txq->qp));
909 claim_zero(ibv_destroy_cq(txq->cq));
910 for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
911 if (txq->mp2mr[i].mp == NULL)
913 assert(txq->mp2mr[i].mr != NULL);
914 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
916 memset(txq, 0, sizeof(*txq));
920 * Manage TX completions.
922 * When sending a burst, mlx4_tx_burst() posts several WRs.
923 * To improve performance, a completion event is only required once every
924 * MLX4_PMD_TX_PER_COMP_REQ sends. Doing so discards completion information
925 * for other WRs, but this information would not be used anyway.
928 * Pointer to TX queue structure.
931 * 0 on success, -1 on failure.
934 txq_complete(struct txq *txq)
936 unsigned int elts_comp = txq->elts_comp;
937 unsigned int elts_tail = txq->elts_tail;
938 const unsigned int elts_n = txq->elts_n;
941 if (unlikely(elts_comp == 0))
944 DEBUG("%p: processing %u work requests completions",
945 (void *)txq, elts_comp);
947 wcs_n = txq->if_cq->poll_cnt(txq->cq, elts_comp);
948 if (unlikely(wcs_n == 0))
950 if (unlikely(wcs_n < 0)) {
951 DEBUG("%p: ibv_poll_cq() failed (wcs_n=%d)",
956 assert(elts_comp <= txq->elts_comp);
958 * Assume WC status is successful as nothing can be done about it
961 elts_tail += wcs_n * txq->elts_comp_cd_init;
962 if (elts_tail >= elts_n)
964 txq->elts_tail = elts_tail;
965 txq->elts_comp = elts_comp;
970 * Get Memory Region (MR) <-> Memory Pool (MP) association from txq->mp2mr[].
971 * Add MP to txq->mp2mr[] if it's not registered yet. If mp2mr[] is full,
972 * remove an entry first.
975 * Pointer to TX queue structure.
977 * Memory Pool for which a Memory Region lkey must be returned.
980 * mr->lkey on success, (uint32_t)-1 on failure.
983 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
988 for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
989 if (unlikely(txq->mp2mr[i].mp == NULL)) {
990 /* Unknown MP, add a new MR for it. */
993 if (txq->mp2mr[i].mp == mp) {
994 assert(txq->mp2mr[i].lkey != (uint32_t)-1);
995 assert(txq->mp2mr[i].mr->lkey == txq->mp2mr[i].lkey);
996 return txq->mp2mr[i].lkey;
999 /* Add a new entry, register MR first. */
1000 DEBUG("%p: discovered new memory pool %p", (void *)txq, (void *)mp);
1001 mr = ibv_reg_mr(txq->priv->pd,
1002 (void *)mp->elt_va_start,
1003 (mp->elt_va_end - mp->elt_va_start),
1004 (IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE));
1005 if (unlikely(mr == NULL)) {
1006 DEBUG("%p: unable to configure MR, ibv_reg_mr() failed.",
1008 return (uint32_t)-1;
1010 if (unlikely(i == elemof(txq->mp2mr))) {
1011 /* Table is full, remove oldest entry. */
1012 DEBUG("%p: MR <-> MP table full, dropping oldest entry.",
1015 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
1016 memmove(&txq->mp2mr[0], &txq->mp2mr[1],
1017 (sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
1019 /* Store the new entry. */
1020 txq->mp2mr[i].mp = mp;
1021 txq->mp2mr[i].mr = mr;
1022 txq->mp2mr[i].lkey = mr->lkey;
1023 DEBUG("%p: new MR lkey for MP %p: 0x%08" PRIu32,
1024 (void *)txq, (void *)mp, txq->mp2mr[i].lkey);
1025 return txq->mp2mr[i].lkey;
1028 #if MLX4_PMD_SGE_WR_N > 1
1031 * Copy scattered mbuf contents to a single linear buffer.
1033 * @param[out] linear
1034 * Linear output buffer.
1036 * Scattered input buffer.
1039 * Number of bytes copied to the output buffer or 0 if not large enough.
1042 linearize_mbuf(linear_t *linear, struct rte_mbuf *buf)
1044 unsigned int size = 0;
1045 unsigned int offset;
1048 unsigned int len = DATA_LEN(buf);
1052 if (unlikely(size > sizeof(*linear)))
1054 memcpy(&(*linear)[offset],
1055 rte_pktmbuf_mtod(buf, uint8_t *),
1058 } while (buf != NULL);
1063 * Handle scattered buffers for mlx4_tx_burst().
1066 * TX queue structure.
1068 * Number of segments in buf.
1070 * TX queue element to fill.
1072 * Buffer to process.
1074 * Index of the linear buffer to use if necessary (normally txq->elts_head).
1077 * Processed packet size in bytes or (unsigned int)-1 in case of failure.
1080 tx_burst_sg(struct txq *txq, unsigned int segs, struct txq_elt *elt,
1081 struct rte_mbuf *buf, unsigned int elts_head)
1083 struct ibv_send_wr *wr = &elt->wr;
1084 unsigned int sent_size = 0;
1088 /* When there are too many segments, extra segments are
1089 * linearized in the last SGE. */
1090 if (unlikely(segs > elemof(elt->sges))) {
1091 segs = (elemof(elt->sges) - 1);
1094 /* Set WR fields. */
1095 assert((rte_pktmbuf_mtod(buf, uintptr_t) -
1096 (uintptr_t)buf) <= 0xffff);
1097 WR_ID(wr->wr_id).offset =
1098 (rte_pktmbuf_mtod(buf, uintptr_t) -
1101 /* Register segments as SGEs. */
1102 for (j = 0; (j != segs); ++j) {
1103 struct ibv_sge *sge = &elt->sges[j];
1106 /* Retrieve Memory Region key for this memory pool. */
1107 lkey = txq_mp2mr(txq, buf->pool);
1108 if (unlikely(lkey == (uint32_t)-1)) {
1109 /* MR does not exist. */
1110 DEBUG("%p: unable to get MP <-> MR association",
1112 /* Clean up TX element. */
1113 WR_ID(elt->wr.wr_id).offset = 0;
1127 /* Sanity checks, only relevant with debugging enabled. */
1128 assert(sge->addr == 0);
1129 assert(sge->length == 0);
1130 assert(sge->lkey == 0);
1132 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
1134 rte_prefetch0((volatile void *)
1135 (uintptr_t)sge->addr);
1136 sge->length = DATA_LEN(buf);
1138 sent_size += sge->length;
1141 /* If buf is not NULL here and is not going to be linearized,
1142 * nb_segs is not valid. */
1144 assert((buf == NULL) || (linearize));
1145 /* Linearize extra segments. */
1147 struct ibv_sge *sge = &elt->sges[segs];
1148 linear_t *linear = &(*txq->elts_linear)[elts_head];
1149 unsigned int size = linearize_mbuf(linear, buf);
1151 assert(segs == (elemof(elt->sges) - 1));
1153 /* Invalid packet. */
1154 DEBUG("%p: packet too large to be linearized.",
1156 /* Clean up TX element. */
1157 WR_ID(elt->wr.wr_id).offset = 0;
1171 /* If MLX4_PMD_SGE_WR_N is 1, free mbuf immediately
1172 * and clear offset from WR ID. */
1173 if (elemof(elt->sges) == 1) {
1175 struct rte_mbuf *next = NEXT(buf);
1177 rte_pktmbuf_free_seg(buf);
1179 } while (buf != NULL);
1180 WR_ID(wr->wr_id).offset = 0;
1182 /* Set WR fields and fill SGE with linear buffer. */
1184 /* Sanity checks, only relevant with debugging
1186 assert(sge->addr == 0);
1187 assert(sge->length == 0);
1188 assert(sge->lkey == 0);
1190 sge->addr = (uintptr_t)&(*linear)[0];
1192 sge->lkey = txq->mr_linear->lkey;
1200 #endif /* MLX4_PMD_SGE_WR_N > 1 */
1203 * DPDK callback for TX.
1206 * Generic pointer to TX queue structure.
1208 * Packets to transmit.
1210 * Number of packets in array.
1213 * Number of packets successfully transmitted (<= pkts_n).
1216 mlx4_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
1218 struct txq *txq = (struct txq *)dpdk_txq;
1219 struct ibv_send_wr head;
1220 struct ibv_send_wr **wr_next = &head.next;
1221 unsigned int elts_head = txq->elts_head;
1222 const unsigned int elts_tail = txq->elts_tail;
1223 const unsigned int elts_n = txq->elts_n;
1224 unsigned int elts_comp_cd = txq->elts_comp_cd;
1225 unsigned int elts_comp = 0;
1230 assert(elts_comp_cd != 0);
1232 max = (elts_n - (elts_head - elts_tail));
1236 assert(max <= elts_n);
1237 /* Always leave one free entry in the ring. */
1243 for (i = 0; (i != max); ++i) {
1244 struct rte_mbuf *buf = pkts[i];
1245 struct txq_elt *elt = &(*txq->elts)[elts_head];
1246 struct ibv_send_wr *wr = &elt->wr;
1247 unsigned int segs = NB_SEGS(buf);
1248 #ifdef MLX4_PMD_SOFT_COUNTERS
1249 unsigned int sent_size = 0;
1254 uint32_t send_flags = 0;
1256 /* Clean up old buffer. */
1257 if (likely(WR_ID(wr->wr_id).offset != 0)) {
1258 struct rte_mbuf *tmp = (void *)
1259 ((uintptr_t)elt->sges[0].addr -
1260 WR_ID(wr->wr_id).offset);
1262 /* Faster than rte_pktmbuf_free(). */
1264 struct rte_mbuf *next = NEXT(tmp);
1266 rte_pktmbuf_free_seg(tmp);
1268 } while (tmp != NULL);
1272 WR_ID(wr->wr_id).offset = 0;
1273 for (j = 0; ((int)j < wr->num_sge); ++j) {
1274 elt->sges[j].addr = 0;
1275 elt->sges[j].length = 0;
1276 elt->sges[j].lkey = 0;
1281 /* Sanity checks, most of which are only relevant with
1282 * debugging enabled. */
1283 assert(WR_ID(wr->wr_id).id == elts_head);
1284 assert(WR_ID(wr->wr_id).offset == 0);
1285 assert(wr->next == NULL);
1286 assert(wr->sg_list == &elt->sges[0]);
1287 assert(wr->num_sge == 0);
1288 assert(wr->opcode == IBV_WR_SEND);
1289 if (likely(segs == 1)) {
1290 struct ibv_sge *sge = &elt->sges[0];
1293 /* Set WR fields. */
1294 assert((rte_pktmbuf_mtod(buf, uintptr_t) -
1295 (uintptr_t)buf) <= 0xffff);
1296 WR_ID(wr->wr_id).offset =
1297 (rte_pktmbuf_mtod(buf, uintptr_t) -
1300 /* Register segment as SGE. */
1301 sge = &elt->sges[0];
1302 /* Retrieve Memory Region key for this memory pool. */
1303 lkey = txq_mp2mr(txq, buf->pool);
1304 if (unlikely(lkey == (uint32_t)-1)) {
1305 /* MR does not exist. */
1306 DEBUG("%p: unable to get MP <-> MR"
1307 " association", (void *)txq);
1308 /* Clean up TX element. */
1309 WR_ID(elt->wr.wr_id).offset = 0;
1319 /* Sanity checks, only relevant with debugging
1321 assert(sge->addr == 0);
1322 assert(sge->length == 0);
1323 assert(sge->lkey == 0);
1325 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
1327 rte_prefetch0((volatile void *)
1328 (uintptr_t)sge->addr);
1329 sge->length = DATA_LEN(buf);
1331 #ifdef MLX4_PMD_SOFT_COUNTERS
1332 sent_size += sge->length;
1335 #if MLX4_PMD_SGE_WR_N > 1
1338 ret = tx_burst_sg(txq, segs, elt, buf, elts_head);
1339 if (ret == (unsigned int)-1)
1341 #ifdef MLX4_PMD_SOFT_COUNTERS
1344 #else /* MLX4_PMD_SGE_WR_N > 1 */
1345 DEBUG("%p: TX scattered buffers support not"
1346 " compiled in", (void *)txq);
1348 #endif /* MLX4_PMD_SGE_WR_N > 1 */
1350 /* Link WRs together for ibv_post_send(). */
1352 wr_next = &wr->next;
1353 assert(wr->send_flags == 0);
1354 /* Request TX completion. */
1355 if (unlikely(--elts_comp_cd == 0)) {
1356 elts_comp_cd = txq->elts_comp_cd_init;
1358 send_flags |= IBV_EXP_QP_BURST_SIGNALED;
1360 if (++elts_head >= elts_n)
1362 #ifdef MLX4_PMD_SOFT_COUNTERS
1363 /* Increment sent bytes counter. */
1364 txq->stats.obytes += sent_size;
1366 /* Put SG list into send queue and ask for completion event. */
1367 #if MLX4_PMD_MAX_INLINE > 0
1369 (elt->sges[0].length <= txq->max_inline))
1370 err = txq->if_qp->send_pending_inline
1372 (void *)(uintptr_t)elt->sges[0].addr,
1373 elt->sges[0].length,
1377 err = txq->if_qp->send_pending_sg_list
1386 /* Take a shortcut if nothing must be sent. */
1387 if (unlikely(i == 0))
1389 #ifdef MLX4_PMD_SOFT_COUNTERS
1390 /* Increment sent packets counter. */
1391 txq->stats.opackets += i;
1394 /* Ring QP doorbell. */
1395 err = txq->if_qp->send_flush(txq->qp);
1396 if (unlikely(err)) {
1397 /* A nonzero value is not supposed to be returned.
1398 * Nothing can be done about it. */
1399 DEBUG("%p: send_flush() failed with error %d",
1402 txq->elts_head = elts_head;
1403 txq->elts_comp += elts_comp;
1404 txq->elts_comp_cd = elts_comp_cd;
1409 * Configure a TX queue.
1412 * Pointer to Ethernet device structure.
1414 * Pointer to TX queue structure.
1416 * Number of descriptors to configure in queue.
1418 * NUMA socket on which memory must be allocated.
1420 * Thresholds parameters.
1423 * 0 on success, errno value on failure.
1426 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
1427 unsigned int socket, const struct rte_eth_txconf *conf)
1429 struct priv *priv = dev->data->dev_private;
1435 struct ibv_exp_query_intf_params params;
1436 struct ibv_qp_init_attr init;
1437 struct ibv_exp_qp_attr mod;
1439 enum ibv_exp_query_intf_status status;
1442 (void)conf; /* Thresholds configuration (ignored). */
1443 if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
1444 ERROR("%p: invalid number of TX descriptors (must be a"
1445 " multiple of %d)", (void *)dev, MLX4_PMD_SGE_WR_N);
1448 desc /= MLX4_PMD_SGE_WR_N;
1449 /* MRs will be registered in mp2mr[] later. */
1450 tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
1451 if (tmpl.cq == NULL) {
1453 ERROR("%p: CQ creation failure: %s",
1454 (void *)dev, strerror(ret));
1457 DEBUG("priv->device_attr.max_qp_wr is %d",
1458 priv->device_attr.max_qp_wr);
1459 DEBUG("priv->device_attr.max_sge is %d",
1460 priv->device_attr.max_sge);
1461 attr.init = (struct ibv_qp_init_attr){
1462 /* CQ to be associated with the send queue. */
1464 /* CQ to be associated with the receive queue. */
1467 /* Max number of outstanding WRs. */
1468 .max_send_wr = ((priv->device_attr.max_qp_wr < desc) ?
1469 priv->device_attr.max_qp_wr :
1471 /* Max number of scatter/gather elements in a WR. */
1472 .max_send_sge = ((priv->device_attr.max_sge <
1473 MLX4_PMD_SGE_WR_N) ?
1474 priv->device_attr.max_sge :
1476 #if MLX4_PMD_MAX_INLINE > 0
1477 .max_inline_data = MLX4_PMD_MAX_INLINE,
1480 .qp_type = IBV_QPT_RAW_PACKET,
1481 /* Do *NOT* enable this, completions events are managed per
1485 tmpl.qp = ibv_create_qp(priv->pd, &attr.init);
1486 if (tmpl.qp == NULL) {
1487 ret = (errno ? errno : EINVAL);
1488 ERROR("%p: QP creation failure: %s",
1489 (void *)dev, strerror(ret));
1492 #if MLX4_PMD_MAX_INLINE > 0
1493 /* ibv_create_qp() updates this value. */
1494 tmpl.max_inline = attr.init.cap.max_inline_data;
1496 attr.mod = (struct ibv_exp_qp_attr){
1497 /* Move the QP to this state. */
1498 .qp_state = IBV_QPS_INIT,
1499 /* Primary port number. */
1500 .port_num = priv->port
1502 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod,
1503 (IBV_EXP_QP_STATE | IBV_EXP_QP_PORT));
1505 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
1506 (void *)dev, strerror(ret));
1509 ret = txq_alloc_elts(&tmpl, desc);
1511 ERROR("%p: TXQ allocation failed: %s",
1512 (void *)dev, strerror(ret));
1515 attr.mod = (struct ibv_exp_qp_attr){
1516 .qp_state = IBV_QPS_RTR
1518 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1520 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
1521 (void *)dev, strerror(ret));
1524 attr.mod.qp_state = IBV_QPS_RTS;
1525 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1527 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
1528 (void *)dev, strerror(ret));
1531 attr.params = (struct ibv_exp_query_intf_params){
1532 .intf_scope = IBV_EXP_INTF_GLOBAL,
1533 .intf = IBV_EXP_INTF_CQ,
1536 tmpl.if_cq = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
1537 if (tmpl.if_cq == NULL) {
1538 ERROR("%p: CQ interface family query failed with status %d",
1539 (void *)dev, status);
1542 attr.params = (struct ibv_exp_query_intf_params){
1543 .intf_scope = IBV_EXP_INTF_GLOBAL,
1544 .intf = IBV_EXP_INTF_QP_BURST,
1547 tmpl.if_qp = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
1548 if (tmpl.if_qp == NULL) {
1549 ERROR("%p: QP interface family query failed with status %d",
1550 (void *)dev, status);
1553 /* Clean up txq in case we're reinitializing it. */
1554 DEBUG("%p: cleaning-up old txq just in case", (void *)txq);
1557 DEBUG("%p: txq updated with %p", (void *)txq, (void *)&tmpl);
1567 * DPDK callback to configure a TX queue.
1570 * Pointer to Ethernet device structure.
1574 * Number of descriptors to configure in queue.
1576 * NUMA socket on which memory must be allocated.
1578 * Thresholds parameters.
1581 * 0 on success, negative errno value on failure.
1584 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1585 unsigned int socket, const struct rte_eth_txconf *conf)
1587 struct priv *priv = dev->data->dev_private;
1588 struct txq *txq = (*priv->txqs)[idx];
1592 DEBUG("%p: configuring queue %u for %u descriptors",
1593 (void *)dev, idx, desc);
1594 if (idx >= priv->txqs_n) {
1595 ERROR("%p: queue index out of range (%u >= %u)",
1596 (void *)dev, idx, priv->txqs_n);
1601 DEBUG("%p: reusing already allocated queue index %u (%p)",
1602 (void *)dev, idx, (void *)txq);
1603 if (priv->started) {
1607 (*priv->txqs)[idx] = NULL;
1610 txq = rte_calloc_socket("TXQ", 1, sizeof(*txq), 0, socket);
1612 ERROR("%p: unable to allocate queue index %u",
1618 ret = txq_setup(dev, txq, desc, socket, conf);
1622 txq->stats.idx = idx;
1623 DEBUG("%p: adding TX queue %p to list",
1624 (void *)dev, (void *)txq);
1625 (*priv->txqs)[idx] = txq;
1626 /* Update send callback. */
1627 dev->tx_pkt_burst = mlx4_tx_burst;
1634 * DPDK callback to release a TX queue.
1637 * Generic TX queue pointer.
1640 mlx4_tx_queue_release(void *dpdk_txq)
1642 struct txq *txq = (struct txq *)dpdk_txq;
1650 for (i = 0; (i != priv->txqs_n); ++i)
1651 if ((*priv->txqs)[i] == txq) {
1652 DEBUG("%p: removing TX queue %p from list",
1653 (void *)priv->dev, (void *)txq);
1654 (*priv->txqs)[i] = NULL;
1662 /* RX queues handling. */
1665 * Allocate RX queue elements with scattered packets support.
1668 * Pointer to RX queue structure.
1670 * Number of elements to allocate.
1672 * If not NULL, fetch buffers from this array instead of allocating them
1673 * with rte_pktmbuf_alloc().
1676 * 0 on success, errno value on failure.
1679 rxq_alloc_elts_sp(struct rxq *rxq, unsigned int elts_n,
1680 struct rte_mbuf **pool)
1683 struct rxq_elt_sp (*elts)[elts_n] =
1684 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1689 ERROR("%p: can't allocate packets array", (void *)rxq);
1693 /* For each WR (packet). */
1694 for (i = 0; (i != elts_n); ++i) {
1696 struct rxq_elt_sp *elt = &(*elts)[i];
1697 struct ibv_recv_wr *wr = &elt->wr;
1698 struct ibv_sge (*sges)[(elemof(elt->sges))] = &elt->sges;
1700 /* These two arrays must have the same size. */
1701 assert(elemof(elt->sges) == elemof(elt->bufs));
1704 wr->next = &(*elts)[(i + 1)].wr;
1705 wr->sg_list = &(*sges)[0];
1706 wr->num_sge = elemof(*sges);
1707 /* For each SGE (segment). */
1708 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1709 struct ibv_sge *sge = &(*sges)[j];
1710 struct rte_mbuf *buf;
1714 assert(buf != NULL);
1715 rte_pktmbuf_reset(buf);
1717 buf = rte_pktmbuf_alloc(rxq->mp);
1719 assert(pool == NULL);
1720 ERROR("%p: empty mbuf pool", (void *)rxq);
1725 /* Headroom is reserved by rte_pktmbuf_alloc(). */
1726 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1727 /* Buffer is supposed to be empty. */
1728 assert(rte_pktmbuf_data_len(buf) == 0);
1729 assert(rte_pktmbuf_pkt_len(buf) == 0);
1730 /* sge->addr must be able to store a pointer. */
1731 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1733 /* The first SGE keeps its headroom. */
1734 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
1735 sge->length = (buf->buf_len -
1736 RTE_PKTMBUF_HEADROOM);
1738 /* Subsequent SGEs lose theirs. */
1739 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1740 SET_DATA_OFF(buf, 0);
1741 sge->addr = (uintptr_t)buf->buf_addr;
1742 sge->length = buf->buf_len;
1744 sge->lkey = rxq->mr->lkey;
1745 /* Redundant check for tailroom. */
1746 assert(sge->length == rte_pktmbuf_tailroom(buf));
1749 /* The last WR pointer must be NULL. */
1750 (*elts)[(i - 1)].wr.next = NULL;
1751 DEBUG("%p: allocated and configured %u WRs (%zu segments)",
1752 (void *)rxq, elts_n, (elts_n * elemof((*elts)[0].sges)));
1753 rxq->elts_n = elts_n;
1755 rxq->elts.sp = elts;
1760 assert(pool == NULL);
1761 for (i = 0; (i != elemof(*elts)); ++i) {
1763 struct rxq_elt_sp *elt = &(*elts)[i];
1765 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1766 struct rte_mbuf *buf = elt->bufs[j];
1769 rte_pktmbuf_free_seg(buf);
1774 DEBUG("%p: failed, freed everything", (void *)rxq);
1780 * Free RX queue elements with scattered packets support.
1783 * Pointer to RX queue structure.
1786 rxq_free_elts_sp(struct rxq *rxq)
1789 unsigned int elts_n = rxq->elts_n;
1790 struct rxq_elt_sp (*elts)[elts_n] = rxq->elts.sp;
1792 DEBUG("%p: freeing WRs", (void *)rxq);
1794 rxq->elts.sp = NULL;
1797 for (i = 0; (i != elemof(*elts)); ++i) {
1799 struct rxq_elt_sp *elt = &(*elts)[i];
1801 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1802 struct rte_mbuf *buf = elt->bufs[j];
1805 rte_pktmbuf_free_seg(buf);
1812 * Allocate RX queue elements.
1815 * Pointer to RX queue structure.
1817 * Number of elements to allocate.
1819 * If not NULL, fetch buffers from this array instead of allocating them
1820 * with rte_pktmbuf_alloc().
1823 * 0 on success, errno value on failure.
1826 rxq_alloc_elts(struct rxq *rxq, unsigned int elts_n, struct rte_mbuf **pool)
1829 struct rxq_elt (*elts)[elts_n] =
1830 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1835 ERROR("%p: can't allocate packets array", (void *)rxq);
1839 /* For each WR (packet). */
1840 for (i = 0; (i != elts_n); ++i) {
1841 struct rxq_elt *elt = &(*elts)[i];
1842 struct ibv_recv_wr *wr = &elt->wr;
1843 struct ibv_sge *sge = &(*elts)[i].sge;
1844 struct rte_mbuf *buf;
1848 assert(buf != NULL);
1849 rte_pktmbuf_reset(buf);
1851 buf = rte_pktmbuf_alloc(rxq->mp);
1853 assert(pool == NULL);
1854 ERROR("%p: empty mbuf pool", (void *)rxq);
1858 /* Configure WR. Work request ID contains its own index in
1859 * the elts array and the offset between SGE buffer header and
1861 WR_ID(wr->wr_id).id = i;
1862 WR_ID(wr->wr_id).offset =
1863 (((uintptr_t)buf->buf_addr + RTE_PKTMBUF_HEADROOM) -
1865 wr->next = &(*elts)[(i + 1)].wr;
1868 /* Headroom is reserved by rte_pktmbuf_alloc(). */
1869 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1870 /* Buffer is supposed to be empty. */
1871 assert(rte_pktmbuf_data_len(buf) == 0);
1872 assert(rte_pktmbuf_pkt_len(buf) == 0);
1873 /* sge->addr must be able to store a pointer. */
1874 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1875 /* SGE keeps its headroom. */
1876 sge->addr = (uintptr_t)
1877 ((uint8_t *)buf->buf_addr + RTE_PKTMBUF_HEADROOM);
1878 sge->length = (buf->buf_len - RTE_PKTMBUF_HEADROOM);
1879 sge->lkey = rxq->mr->lkey;
1880 /* Redundant check for tailroom. */
1881 assert(sge->length == rte_pktmbuf_tailroom(buf));
1882 /* Make sure elts index and SGE mbuf pointer can be deduced
1884 if ((WR_ID(wr->wr_id).id != i) ||
1885 ((void *)((uintptr_t)sge->addr -
1886 WR_ID(wr->wr_id).offset) != buf)) {
1887 ERROR("%p: cannot store index and offset in WR ID",
1890 rte_pktmbuf_free(buf);
1895 /* The last WR pointer must be NULL. */
1896 (*elts)[(i - 1)].wr.next = NULL;
1897 DEBUG("%p: allocated and configured %u single-segment WRs",
1898 (void *)rxq, elts_n);
1899 rxq->elts_n = elts_n;
1901 rxq->elts.no_sp = elts;
1906 assert(pool == NULL);
1907 for (i = 0; (i != elemof(*elts)); ++i) {
1908 struct rxq_elt *elt = &(*elts)[i];
1909 struct rte_mbuf *buf;
1911 if (elt->sge.addr == 0)
1913 assert(WR_ID(elt->wr.wr_id).id == i);
1914 buf = (void *)((uintptr_t)elt->sge.addr -
1915 WR_ID(elt->wr.wr_id).offset);
1916 rte_pktmbuf_free_seg(buf);
1920 DEBUG("%p: failed, freed everything", (void *)rxq);
1926 * Free RX queue elements.
1929 * Pointer to RX queue structure.
1932 rxq_free_elts(struct rxq *rxq)
1935 unsigned int elts_n = rxq->elts_n;
1936 struct rxq_elt (*elts)[elts_n] = rxq->elts.no_sp;
1938 DEBUG("%p: freeing WRs", (void *)rxq);
1940 rxq->elts.no_sp = NULL;
1943 for (i = 0; (i != elemof(*elts)); ++i) {
1944 struct rxq_elt *elt = &(*elts)[i];
1945 struct rte_mbuf *buf;
1947 if (elt->sge.addr == 0)
1949 assert(WR_ID(elt->wr.wr_id).id == i);
1950 buf = (void *)((uintptr_t)elt->sge.addr -
1951 WR_ID(elt->wr.wr_id).offset);
1952 rte_pktmbuf_free_seg(buf);
1958 * Delete flow steering rule.
1961 * Pointer to RX queue structure.
1963 * MAC address index.
1968 rxq_del_flow(struct rxq *rxq, unsigned int mac_index, unsigned int vlan_index)
1971 struct priv *priv = rxq->priv;
1972 const uint8_t (*mac)[ETHER_ADDR_LEN] =
1973 (const uint8_t (*)[ETHER_ADDR_LEN])
1974 priv->mac[mac_index].addr_bytes;
1976 assert(rxq->mac_flow[mac_index][vlan_index] != NULL);
1977 DEBUG("%p: removing MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
1978 " (VLAN ID %" PRIu16 ")",
1980 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
1981 mac_index, priv->vlan_filter[vlan_index].id);
1982 claim_zero(ibv_destroy_flow(rxq->mac_flow[mac_index][vlan_index]));
1983 rxq->mac_flow[mac_index][vlan_index] = NULL;
1987 * Unregister a MAC address from a RX queue.
1990 * Pointer to RX queue structure.
1992 * MAC address index.
1995 rxq_mac_addr_del(struct rxq *rxq, unsigned int mac_index)
1997 struct priv *priv = rxq->priv;
1999 unsigned int vlans = 0;
2001 assert(mac_index < elemof(priv->mac));
2002 if (!BITFIELD_ISSET(rxq->mac_configured, mac_index))
2004 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
2005 if (!priv->vlan_filter[i].enabled)
2007 rxq_del_flow(rxq, mac_index, i);
2011 rxq_del_flow(rxq, mac_index, 0);
2013 BITFIELD_RESET(rxq->mac_configured, mac_index);
2017 * Unregister all MAC addresses from a RX queue.
2020 * Pointer to RX queue structure.
2023 rxq_mac_addrs_del(struct rxq *rxq)
2025 struct priv *priv = rxq->priv;
2028 for (i = 0; (i != elemof(priv->mac)); ++i)
2029 rxq_mac_addr_del(rxq, i);
2032 static int rxq_promiscuous_enable(struct rxq *);
2033 static void rxq_promiscuous_disable(struct rxq *);
2036 * Add single flow steering rule.
2039 * Pointer to RX queue structure.
2041 * MAC address index to register.
2043 * VLAN index. Use -1 for a flow without VLAN.
2046 * 0 on success, errno value on failure.
2049 rxq_add_flow(struct rxq *rxq, unsigned int mac_index, unsigned int vlan_index)
2051 struct ibv_flow *flow;
2052 struct priv *priv = rxq->priv;
2053 const uint8_t (*mac)[ETHER_ADDR_LEN] =
2054 (const uint8_t (*)[ETHER_ADDR_LEN])
2055 priv->mac[mac_index].addr_bytes;
2057 /* Allocate flow specification on the stack. */
2058 struct __attribute__((packed)) {
2059 struct ibv_flow_attr attr;
2060 struct ibv_flow_spec_eth spec;
2062 struct ibv_flow_attr *attr = &data.attr;
2063 struct ibv_flow_spec_eth *spec = &data.spec;
2065 assert(mac_index < elemof(priv->mac));
2066 assert((vlan_index < elemof(priv->vlan_filter)) || (vlan_index == -1u));
2068 * No padding must be inserted by the compiler between attr and spec.
2069 * This layout is expected by libibverbs.
2071 assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec);
2072 *attr = (struct ibv_flow_attr){
2073 .type = IBV_FLOW_ATTR_NORMAL,
2078 *spec = (struct ibv_flow_spec_eth){
2079 .type = IBV_FLOW_SPEC_ETH,
2080 .size = sizeof(*spec),
2083 (*mac)[0], (*mac)[1], (*mac)[2],
2084 (*mac)[3], (*mac)[4], (*mac)[5]
2086 .vlan_tag = ((vlan_index != -1u) ?
2087 htons(priv->vlan_filter[vlan_index].id) :
2091 .dst_mac = "\xff\xff\xff\xff\xff\xff",
2092 .vlan_tag = ((vlan_index != -1u) ? htons(0xfff) : 0),
2095 DEBUG("%p: adding MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
2096 " (VLAN %s %" PRIu16 ")",
2098 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
2100 ((vlan_index != -1u) ? "ID" : "index"),
2101 ((vlan_index != -1u) ? priv->vlan_filter[vlan_index].id : -1u));
2102 /* Create related flow. */
2104 flow = ibv_create_flow(rxq->qp, attr);
2106 /* It's not clear whether errno is always set in this case. */
2107 ERROR("%p: flow configuration failed, errno=%d: %s",
2109 (errno ? strerror(errno) : "Unknown error"));
2114 if (vlan_index == -1u)
2116 assert(rxq->mac_flow[mac_index][vlan_index] == NULL);
2117 rxq->mac_flow[mac_index][vlan_index] = flow;
2122 * Register a MAC address in a RX queue.
2125 * Pointer to RX queue structure.
2127 * MAC address index to register.
2130 * 0 on success, errno value on failure.
2133 rxq_mac_addr_add(struct rxq *rxq, unsigned int mac_index)
2135 struct priv *priv = rxq->priv;
2137 unsigned int vlans = 0;
2140 assert(mac_index < elemof(priv->mac));
2141 if (BITFIELD_ISSET(rxq->mac_configured, mac_index))
2142 rxq_mac_addr_del(rxq, mac_index);
2143 /* Fill VLAN specifications. */
2144 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
2145 if (!priv->vlan_filter[i].enabled)
2147 /* Create related flow. */
2148 ret = rxq_add_flow(rxq, mac_index, i);
2153 /* Failure, rollback. */
2155 if (priv->vlan_filter[--i].enabled)
2156 rxq_del_flow(rxq, mac_index, i);
2160 /* In case there is no VLAN filter. */
2162 ret = rxq_add_flow(rxq, mac_index, -1);
2166 BITFIELD_SET(rxq->mac_configured, mac_index);
2171 * Register all MAC addresses in a RX queue.
2174 * Pointer to RX queue structure.
2177 * 0 on success, errno value on failure.
2180 rxq_mac_addrs_add(struct rxq *rxq)
2182 struct priv *priv = rxq->priv;
2186 for (i = 0; (i != elemof(priv->mac)); ++i) {
2187 if (!BITFIELD_ISSET(priv->mac_configured, i))
2189 ret = rxq_mac_addr_add(rxq, i);
2192 /* Failure, rollback. */
2194 rxq_mac_addr_del(rxq, --i);
2202 * Unregister a MAC address.
2204 * In RSS mode, the MAC address is unregistered from the parent queue,
2205 * otherwise it is unregistered from each queue directly.
2208 * Pointer to private structure.
2210 * MAC address index.
2213 priv_mac_addr_del(struct priv *priv, unsigned int mac_index)
2217 assert(mac_index < elemof(priv->mac));
2218 if (!BITFIELD_ISSET(priv->mac_configured, mac_index))
2221 rxq_mac_addr_del(&priv->rxq_parent, mac_index);
2224 for (i = 0; (i != priv->dev->data->nb_rx_queues); ++i)
2225 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2227 BITFIELD_RESET(priv->mac_configured, mac_index);
2231 * Register a MAC address.
2233 * In RSS mode, the MAC address is registered in the parent queue,
2234 * otherwise it is registered in each queue directly.
2237 * Pointer to private structure.
2239 * MAC address index to use.
2241 * MAC address to register.
2244 * 0 on success, errno value on failure.
2247 priv_mac_addr_add(struct priv *priv, unsigned int mac_index,
2248 const uint8_t (*mac)[ETHER_ADDR_LEN])
2253 assert(mac_index < elemof(priv->mac));
2254 /* First, make sure this address isn't already configured. */
2255 for (i = 0; (i != elemof(priv->mac)); ++i) {
2256 /* Skip this index, it's going to be reconfigured. */
2259 if (!BITFIELD_ISSET(priv->mac_configured, i))
2261 if (memcmp(priv->mac[i].addr_bytes, *mac, sizeof(*mac)))
2263 /* Address already configured elsewhere, return with error. */
2266 if (BITFIELD_ISSET(priv->mac_configured, mac_index))
2267 priv_mac_addr_del(priv, mac_index);
2268 priv->mac[mac_index] = (struct ether_addr){
2270 (*mac)[0], (*mac)[1], (*mac)[2],
2271 (*mac)[3], (*mac)[4], (*mac)[5]
2274 /* If device isn't started, this is all we need to do. */
2275 if (!priv->started) {
2277 /* Verify that all queues have this index disabled. */
2278 for (i = 0; (i != priv->rxqs_n); ++i) {
2279 if ((*priv->rxqs)[i] == NULL)
2281 assert(!BITFIELD_ISSET
2282 ((*priv->rxqs)[i]->mac_configured, mac_index));
2288 ret = rxq_mac_addr_add(&priv->rxq_parent, mac_index);
2293 for (i = 0; (i != priv->rxqs_n); ++i) {
2294 if ((*priv->rxqs)[i] == NULL)
2296 ret = rxq_mac_addr_add((*priv->rxqs)[i], mac_index);
2299 /* Failure, rollback. */
2301 if ((*priv->rxqs)[(--i)] != NULL)
2302 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2306 BITFIELD_SET(priv->mac_configured, mac_index);
2311 * Enable allmulti mode in a RX queue.
2314 * Pointer to RX queue structure.
2317 * 0 on success, errno value on failure.
2320 rxq_allmulticast_enable(struct rxq *rxq)
2322 struct ibv_flow *flow;
2323 struct ibv_flow_attr attr = {
2324 .type = IBV_FLOW_ATTR_MC_DEFAULT,
2326 .port = rxq->priv->port,
2330 DEBUG("%p: enabling allmulticast mode", (void *)rxq);
2331 if (rxq->allmulti_flow != NULL)
2334 flow = ibv_create_flow(rxq->qp, &attr);
2336 /* It's not clear whether errno is always set in this case. */
2337 ERROR("%p: flow configuration failed, errno=%d: %s",
2339 (errno ? strerror(errno) : "Unknown error"));
2344 rxq->allmulti_flow = flow;
2345 DEBUG("%p: allmulticast mode enabled", (void *)rxq);
2350 * Disable allmulti mode in a RX queue.
2353 * Pointer to RX queue structure.
2356 rxq_allmulticast_disable(struct rxq *rxq)
2358 DEBUG("%p: disabling allmulticast mode", (void *)rxq);
2359 if (rxq->allmulti_flow == NULL)
2361 claim_zero(ibv_destroy_flow(rxq->allmulti_flow));
2362 rxq->allmulti_flow = NULL;
2363 DEBUG("%p: allmulticast mode disabled", (void *)rxq);
2367 * Enable promiscuous mode in a RX queue.
2370 * Pointer to RX queue structure.
2373 * 0 on success, errno value on failure.
2376 rxq_promiscuous_enable(struct rxq *rxq)
2378 struct ibv_flow *flow;
2379 struct ibv_flow_attr attr = {
2380 .type = IBV_FLOW_ATTR_ALL_DEFAULT,
2382 .port = rxq->priv->port,
2388 DEBUG("%p: enabling promiscuous mode", (void *)rxq);
2389 if (rxq->promisc_flow != NULL)
2392 flow = ibv_create_flow(rxq->qp, &attr);
2394 /* It's not clear whether errno is always set in this case. */
2395 ERROR("%p: flow configuration failed, errno=%d: %s",
2397 (errno ? strerror(errno) : "Unknown error"));
2402 rxq->promisc_flow = flow;
2403 DEBUG("%p: promiscuous mode enabled", (void *)rxq);
2408 * Disable promiscuous mode in a RX queue.
2411 * Pointer to RX queue structure.
2414 rxq_promiscuous_disable(struct rxq *rxq)
2418 DEBUG("%p: disabling promiscuous mode", (void *)rxq);
2419 if (rxq->promisc_flow == NULL)
2421 claim_zero(ibv_destroy_flow(rxq->promisc_flow));
2422 rxq->promisc_flow = NULL;
2423 DEBUG("%p: promiscuous mode disabled", (void *)rxq);
2427 * Clean up a RX queue.
2429 * Destroy objects, free allocated memory and reset the structure for reuse.
2432 * Pointer to RX queue structure.
2435 rxq_cleanup(struct rxq *rxq)
2437 struct ibv_exp_release_intf_params params;
2439 DEBUG("cleaning up %p", (void *)rxq);
2441 rxq_free_elts_sp(rxq);
2444 if (rxq->if_qp != NULL) {
2445 assert(rxq->priv != NULL);
2446 assert(rxq->priv->ctx != NULL);
2447 assert(rxq->qp != NULL);
2448 params = (struct ibv_exp_release_intf_params){
2451 claim_zero(ibv_exp_release_intf(rxq->priv->ctx,
2455 if (rxq->if_cq != NULL) {
2456 assert(rxq->priv != NULL);
2457 assert(rxq->priv->ctx != NULL);
2458 assert(rxq->cq != NULL);
2459 params = (struct ibv_exp_release_intf_params){
2462 claim_zero(ibv_exp_release_intf(rxq->priv->ctx,
2466 if (rxq->qp != NULL) {
2467 rxq_promiscuous_disable(rxq);
2468 rxq_allmulticast_disable(rxq);
2469 rxq_mac_addrs_del(rxq);
2470 claim_zero(ibv_destroy_qp(rxq->qp));
2472 if (rxq->cq != NULL)
2473 claim_zero(ibv_destroy_cq(rxq->cq));
2474 if (rxq->mr != NULL)
2475 claim_zero(ibv_dereg_mr(rxq->mr));
2476 memset(rxq, 0, sizeof(*rxq));
2480 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n);
2483 * DPDK callback for RX with scattered packets support.
2486 * Generic pointer to RX queue structure.
2488 * Array to store received packets.
2490 * Maximum number of packets in array.
2493 * Number of packets successfully received (<= pkts_n).
2496 mlx4_rx_burst_sp(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2498 struct rxq *rxq = (struct rxq *)dpdk_rxq;
2499 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
2500 const unsigned int elts_n = rxq->elts_n;
2501 unsigned int elts_head = rxq->elts_head;
2502 struct ibv_recv_wr head;
2503 struct ibv_recv_wr **next = &head.next;
2504 struct ibv_recv_wr *bad_wr;
2506 unsigned int pkts_ret = 0;
2509 if (unlikely(!rxq->sp))
2510 return mlx4_rx_burst(dpdk_rxq, pkts, pkts_n);
2511 if (unlikely(elts == NULL)) /* See RTE_DEV_CMD_SET_MTU. */
2513 for (i = 0; (i != pkts_n); ++i) {
2514 struct rxq_elt_sp *elt = &(*elts)[elts_head];
2515 struct ibv_recv_wr *wr = &elt->wr;
2516 uint64_t wr_id = wr->wr_id;
2518 unsigned int pkt_buf_len;
2519 struct rte_mbuf *pkt_buf = NULL; /* Buffer returned in pkts. */
2520 struct rte_mbuf **pkt_buf_next = &pkt_buf;
2521 unsigned int seg_headroom = RTE_PKTMBUF_HEADROOM;
2524 /* Sanity checks. */
2528 assert(wr_id < rxq->elts_n);
2529 assert(wr->sg_list == elt->sges);
2530 assert(wr->num_sge == elemof(elt->sges));
2531 assert(elts_head < rxq->elts_n);
2532 assert(rxq->elts_head < rxq->elts_n);
2533 ret = rxq->if_cq->poll_length(rxq->cq, NULL, NULL);
2534 if (unlikely(ret < 0)) {
2538 DEBUG("rxq=%p, poll_length() failed (ret=%d)",
2540 /* ibv_poll_cq() must be used in case of failure. */
2541 wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
2542 if (unlikely(wcs_n == 0))
2544 if (unlikely(wcs_n < 0)) {
2545 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
2546 (void *)rxq, wcs_n);
2550 if (unlikely(wc.status != IBV_WC_SUCCESS)) {
2551 /* Whatever, just repost the offending WR. */
2552 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
2553 " completion status (%d): %s",
2554 (void *)rxq, wc.wr_id, wc.status,
2555 ibv_wc_status_str(wc.status));
2556 #ifdef MLX4_PMD_SOFT_COUNTERS
2557 /* Increment dropped packets counter. */
2558 ++rxq->stats.idropped;
2560 /* Link completed WRs together for repost. */
2571 /* Link completed WRs together for repost. */
2575 * Replace spent segments with new ones, concatenate and
2576 * return them as pkt_buf.
2579 struct ibv_sge *sge = &elt->sges[j];
2580 struct rte_mbuf *seg = elt->bufs[j];
2581 struct rte_mbuf *rep;
2582 unsigned int seg_tailroom;
2585 * Fetch initial bytes of packet descriptor into a
2586 * cacheline while allocating rep.
2589 rep = __rte_mbuf_raw_alloc(rxq->mp);
2590 if (unlikely(rep == NULL)) {
2592 * Unable to allocate a replacement mbuf,
2595 DEBUG("rxq=%p, wr_id=%" PRIu64 ":"
2596 " can't allocate a new mbuf",
2597 (void *)rxq, wr_id);
2598 if (pkt_buf != NULL) {
2599 *pkt_buf_next = NULL;
2600 rte_pktmbuf_free(pkt_buf);
2602 /* Increase out of memory counters. */
2603 ++rxq->stats.rx_nombuf;
2604 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
2608 /* Poison user-modifiable fields in rep. */
2609 NEXT(rep) = (void *)((uintptr_t)-1);
2610 SET_DATA_OFF(rep, 0xdead);
2611 DATA_LEN(rep) = 0xd00d;
2612 PKT_LEN(rep) = 0xdeadd00d;
2613 NB_SEGS(rep) = 0x2a;
2617 assert(rep->buf_len == seg->buf_len);
2618 assert(rep->buf_len == rxq->mb_len);
2619 /* Reconfigure sge to use rep instead of seg. */
2620 assert(sge->lkey == rxq->mr->lkey);
2621 sge->addr = ((uintptr_t)rep->buf_addr + seg_headroom);
2624 /* Update pkt_buf if it's the first segment, or link
2625 * seg to the previous one and update pkt_buf_next. */
2626 *pkt_buf_next = seg;
2627 pkt_buf_next = &NEXT(seg);
2628 /* Update seg information. */
2629 seg_tailroom = (seg->buf_len - seg_headroom);
2630 assert(sge->length == seg_tailroom);
2631 SET_DATA_OFF(seg, seg_headroom);
2632 if (likely(len <= seg_tailroom)) {
2634 DATA_LEN(seg) = len;
2637 assert(rte_pktmbuf_headroom(seg) ==
2639 assert(rte_pktmbuf_tailroom(seg) ==
2640 (seg_tailroom - len));
2643 DATA_LEN(seg) = seg_tailroom;
2644 PKT_LEN(seg) = seg_tailroom;
2646 assert(rte_pktmbuf_headroom(seg) == seg_headroom);
2647 assert(rte_pktmbuf_tailroom(seg) == 0);
2648 /* Fix len and clear headroom for next segments. */
2649 len -= seg_tailroom;
2652 /* Update head and tail segments. */
2653 *pkt_buf_next = NULL;
2654 assert(pkt_buf != NULL);
2656 NB_SEGS(pkt_buf) = j;
2657 PORT(pkt_buf) = rxq->port_id;
2658 PKT_LEN(pkt_buf) = pkt_buf_len;
2659 pkt_buf->ol_flags = 0;
2661 /* Return packet. */
2662 *(pkts++) = pkt_buf;
2664 #ifdef MLX4_PMD_SOFT_COUNTERS
2665 /* Increase bytes counter. */
2666 rxq->stats.ibytes += pkt_buf_len;
2669 if (++elts_head >= elts_n)
2673 if (unlikely(i == 0))
2678 DEBUG("%p: reposting %d WRs", (void *)rxq, i);
2680 ret = ibv_post_recv(rxq->qp, head.next, &bad_wr);
2681 if (unlikely(ret)) {
2682 /* Inability to repost WRs is fatal. */
2683 DEBUG("%p: ibv_post_recv(): failed for WR %p: %s",
2689 rxq->elts_head = elts_head;
2690 #ifdef MLX4_PMD_SOFT_COUNTERS
2691 /* Increase packets counter. */
2692 rxq->stats.ipackets += pkts_ret;
2698 * DPDK callback for RX.
2700 * The following function is the same as mlx4_rx_burst_sp(), except it doesn't
2701 * manage scattered packets. Improves performance when MRU is lower than the
2702 * size of the first segment.
2705 * Generic pointer to RX queue structure.
2707 * Array to store received packets.
2709 * Maximum number of packets in array.
2712 * Number of packets successfully received (<= pkts_n).
2715 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2717 struct rxq *rxq = (struct rxq *)dpdk_rxq;
2718 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
2719 const unsigned int elts_n = rxq->elts_n;
2720 unsigned int elts_head = rxq->elts_head;
2721 struct ibv_sge sges[pkts_n];
2723 unsigned int pkts_ret = 0;
2726 if (unlikely(rxq->sp))
2727 return mlx4_rx_burst_sp(dpdk_rxq, pkts, pkts_n);
2728 for (i = 0; (i != pkts_n); ++i) {
2729 struct rxq_elt *elt = &(*elts)[elts_head];
2730 struct ibv_recv_wr *wr = &elt->wr;
2731 uint64_t wr_id = wr->wr_id;
2733 struct rte_mbuf *seg = (void *)((uintptr_t)elt->sge.addr -
2734 WR_ID(wr_id).offset);
2735 struct rte_mbuf *rep;
2737 /* Sanity checks. */
2738 assert(WR_ID(wr_id).id < rxq->elts_n);
2739 assert(wr->sg_list == &elt->sge);
2740 assert(wr->num_sge == 1);
2741 assert(elts_head < rxq->elts_n);
2742 assert(rxq->elts_head < rxq->elts_n);
2743 ret = rxq->if_cq->poll_length(rxq->cq, NULL, NULL);
2744 if (unlikely(ret < 0)) {
2748 DEBUG("rxq=%p, poll_length() failed (ret=%d)",
2750 /* ibv_poll_cq() must be used in case of failure. */
2751 wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
2752 if (unlikely(wcs_n == 0))
2754 if (unlikely(wcs_n < 0)) {
2755 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
2756 (void *)rxq, wcs_n);
2760 if (unlikely(wc.status != IBV_WC_SUCCESS)) {
2761 /* Whatever, just repost the offending WR. */
2762 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
2763 " completion status (%d): %s",
2764 (void *)rxq, wc.wr_id, wc.status,
2765 ibv_wc_status_str(wc.status));
2766 #ifdef MLX4_PMD_SOFT_COUNTERS
2767 /* Increment dropped packets counter. */
2768 ++rxq->stats.idropped;
2770 /* Add SGE to array for repost. */
2780 * Fetch initial bytes of packet descriptor into a
2781 * cacheline while allocating rep.
2784 rep = __rte_mbuf_raw_alloc(rxq->mp);
2785 if (unlikely(rep == NULL)) {
2787 * Unable to allocate a replacement mbuf,
2790 DEBUG("rxq=%p, wr_id=%" PRIu32 ":"
2791 " can't allocate a new mbuf",
2792 (void *)rxq, WR_ID(wr_id).id);
2793 /* Increase out of memory counters. */
2794 ++rxq->stats.rx_nombuf;
2795 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
2799 /* Reconfigure sge to use rep instead of seg. */
2800 elt->sge.addr = (uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM;
2801 assert(elt->sge.lkey == rxq->mr->lkey);
2802 WR_ID(wr->wr_id).offset =
2803 (((uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM) -
2805 assert(WR_ID(wr->wr_id).id == WR_ID(wr_id).id);
2807 /* Add SGE to array for repost. */
2810 /* Update seg information. */
2811 SET_DATA_OFF(seg, RTE_PKTMBUF_HEADROOM);
2813 PORT(seg) = rxq->port_id;
2816 DATA_LEN(seg) = len;
2819 /* Return packet. */
2822 #ifdef MLX4_PMD_SOFT_COUNTERS
2823 /* Increase bytes counter. */
2824 rxq->stats.ibytes += len;
2827 if (++elts_head >= elts_n)
2831 if (unlikely(i == 0))
2835 DEBUG("%p: reposting %u WRs", (void *)rxq, i);
2837 ret = rxq->if_qp->recv_burst(rxq->qp, sges, i);
2838 if (unlikely(ret)) {
2839 /* Inability to repost WRs is fatal. */
2840 DEBUG("%p: recv_burst(): failed (ret=%d)",
2845 rxq->elts_head = elts_head;
2846 #ifdef MLX4_PMD_SOFT_COUNTERS
2847 /* Increase packets counter. */
2848 rxq->stats.ipackets += pkts_ret;
2854 * Allocate a Queue Pair.
2855 * Optionally setup inline receive if supported.
2858 * Pointer to private structure.
2860 * Completion queue to associate with QP.
2862 * Number of descriptors in QP (hint only).
2865 * QP pointer or NULL in case of error.
2867 static struct ibv_qp *
2868 rxq_setup_qp(struct priv *priv, struct ibv_cq *cq, uint16_t desc)
2870 struct ibv_exp_qp_init_attr attr = {
2871 /* CQ to be associated with the send queue. */
2873 /* CQ to be associated with the receive queue. */
2876 /* Max number of outstanding WRs. */
2877 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
2878 priv->device_attr.max_qp_wr :
2880 /* Max number of scatter/gather elements in a WR. */
2881 .max_recv_sge = ((priv->device_attr.max_sge <
2882 MLX4_PMD_SGE_WR_N) ?
2883 priv->device_attr.max_sge :
2886 .qp_type = IBV_QPT_RAW_PACKET,
2887 .comp_mask = IBV_EXP_QP_INIT_ATTR_PD,
2892 attr.max_inl_recv = priv->inl_recv_size;
2893 attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
2895 return ibv_exp_create_qp(priv->ctx, &attr);
2901 * Allocate a RSS Queue Pair.
2902 * Optionally setup inline receive if supported.
2905 * Pointer to private structure.
2907 * Completion queue to associate with QP.
2909 * Number of descriptors in QP (hint only).
2911 * If nonzero, create a parent QP, otherwise a child.
2914 * QP pointer or NULL in case of error.
2916 static struct ibv_qp *
2917 rxq_setup_qp_rss(struct priv *priv, struct ibv_cq *cq, uint16_t desc,
2920 struct ibv_exp_qp_init_attr attr = {
2921 /* CQ to be associated with the send queue. */
2923 /* CQ to be associated with the receive queue. */
2926 /* Max number of outstanding WRs. */
2927 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
2928 priv->device_attr.max_qp_wr :
2930 /* Max number of scatter/gather elements in a WR. */
2931 .max_recv_sge = ((priv->device_attr.max_sge <
2932 MLX4_PMD_SGE_WR_N) ?
2933 priv->device_attr.max_sge :
2936 .qp_type = IBV_QPT_RAW_PACKET,
2937 .comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
2938 IBV_EXP_QP_INIT_ATTR_QPG),
2943 attr.max_inl_recv = priv->inl_recv_size,
2944 attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
2947 attr.qpg.qpg_type = IBV_EXP_QPG_PARENT;
2948 /* TSS isn't necessary. */
2949 attr.qpg.parent_attrib.tss_child_count = 0;
2950 attr.qpg.parent_attrib.rss_child_count = priv->rxqs_n;
2951 DEBUG("initializing parent RSS queue");
2953 attr.qpg.qpg_type = IBV_EXP_QPG_CHILD_RX;
2954 attr.qpg.qpg_parent = priv->rxq_parent.qp;
2955 DEBUG("initializing child RSS queue");
2957 return ibv_exp_create_qp(priv->ctx, &attr);
2960 #endif /* RSS_SUPPORT */
2963 * Reconfigure a RX queue with new parameters.
2965 * rxq_rehash() does not allocate mbufs, which, if not done from the right
2966 * thread (such as a control thread), may corrupt the pool.
2967 * In case of failure, the queue is left untouched.
2970 * Pointer to Ethernet device structure.
2975 * 0 on success, errno value on failure.
2978 rxq_rehash(struct rte_eth_dev *dev, struct rxq *rxq)
2980 struct priv *priv = rxq->priv;
2981 struct rxq tmpl = *rxq;
2982 unsigned int mbuf_n;
2983 unsigned int desc_n;
2984 struct rte_mbuf **pool;
2986 struct ibv_exp_qp_attr mod;
2987 struct ibv_recv_wr *bad_wr;
2989 int parent = (rxq == &priv->rxq_parent);
2992 ERROR("%p: cannot rehash parent queue %p",
2993 (void *)dev, (void *)rxq);
2996 DEBUG("%p: rehashing queue %p", (void *)dev, (void *)rxq);
2997 /* Number of descriptors and mbufs currently allocated. */
2998 desc_n = (tmpl.elts_n * (tmpl.sp ? MLX4_PMD_SGE_WR_N : 1));
3000 /* Enable scattered packets support for this queue if necessary. */
3001 if ((dev->data->dev_conf.rxmode.jumbo_frame) &&
3002 (dev->data->dev_conf.rxmode.max_rx_pkt_len >
3003 (tmpl.mb_len - RTE_PKTMBUF_HEADROOM))) {
3005 desc_n /= MLX4_PMD_SGE_WR_N;
3008 DEBUG("%p: %s scattered packets support (%u WRs)",
3009 (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc_n);
3010 /* If scatter mode is the same as before, nothing to do. */
3011 if (tmpl.sp == rxq->sp) {
3012 DEBUG("%p: nothing to do", (void *)dev);
3015 /* Remove attached flows if RSS is disabled (no parent queue). */
3017 rxq_allmulticast_disable(&tmpl);
3018 rxq_promiscuous_disable(&tmpl);
3019 rxq_mac_addrs_del(&tmpl);
3020 /* Update original queue in case of failure. */
3021 rxq->allmulti_flow = tmpl.allmulti_flow;
3022 rxq->promisc_flow = tmpl.promisc_flow;
3023 memcpy(rxq->mac_configured, tmpl.mac_configured,
3024 sizeof(rxq->mac_configured));
3025 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
3027 /* From now on, any failure will render the queue unusable.
3028 * Reinitialize QP. */
3029 mod = (struct ibv_exp_qp_attr){ .qp_state = IBV_QPS_RESET };
3030 err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3032 ERROR("%p: cannot reset QP: %s", (void *)dev, strerror(err));
3036 err = ibv_resize_cq(tmpl.cq, desc_n);
3038 ERROR("%p: cannot resize CQ: %s", (void *)dev, strerror(err));
3042 mod = (struct ibv_exp_qp_attr){
3043 /* Move the QP to this state. */
3044 .qp_state = IBV_QPS_INIT,
3045 /* Primary port number. */
3046 .port_num = priv->port
3048 err = ibv_exp_modify_qp(tmpl.qp, &mod,
3051 (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
3052 #endif /* RSS_SUPPORT */
3055 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
3056 (void *)dev, strerror(err));
3060 /* Reconfigure flows. Do not care for errors. */
3062 rxq_mac_addrs_add(&tmpl);
3064 rxq_promiscuous_enable(&tmpl);
3066 rxq_allmulticast_enable(&tmpl);
3067 /* Update original queue in case of failure. */
3068 rxq->allmulti_flow = tmpl.allmulti_flow;
3069 rxq->promisc_flow = tmpl.promisc_flow;
3070 memcpy(rxq->mac_configured, tmpl.mac_configured,
3071 sizeof(rxq->mac_configured));
3072 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
3074 /* Allocate pool. */
3075 pool = rte_malloc(__func__, (mbuf_n * sizeof(*pool)), 0);
3077 ERROR("%p: cannot allocate memory", (void *)dev);
3080 /* Snatch mbufs from original queue. */
3083 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
3085 for (i = 0; (i != elemof(*elts)); ++i) {
3086 struct rxq_elt_sp *elt = &(*elts)[i];
3089 for (j = 0; (j != elemof(elt->bufs)); ++j) {
3090 assert(elt->bufs[j] != NULL);
3091 pool[k++] = elt->bufs[j];
3095 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
3097 for (i = 0; (i != elemof(*elts)); ++i) {
3098 struct rxq_elt *elt = &(*elts)[i];
3099 struct rte_mbuf *buf = (void *)
3100 ((uintptr_t)elt->sge.addr -
3101 WR_ID(elt->wr.wr_id).offset);
3103 assert(WR_ID(elt->wr.wr_id).id == i);
3107 assert(k == mbuf_n);
3109 tmpl.elts.sp = NULL;
3110 assert((void *)&tmpl.elts.sp == (void *)&tmpl.elts.no_sp);
3112 rxq_alloc_elts_sp(&tmpl, desc_n, pool) :
3113 rxq_alloc_elts(&tmpl, desc_n, pool));
3115 ERROR("%p: cannot reallocate WRs, aborting", (void *)dev);
3120 assert(tmpl.elts_n == desc_n);
3121 assert(tmpl.elts.sp != NULL);
3123 /* Clean up original data. */
3125 rte_free(rxq->elts.sp);
3126 rxq->elts.sp = NULL;
3128 err = ibv_post_recv(tmpl.qp,
3130 &(*tmpl.elts.sp)[0].wr :
3131 &(*tmpl.elts.no_sp)[0].wr),
3134 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
3140 mod = (struct ibv_exp_qp_attr){
3141 .qp_state = IBV_QPS_RTR
3143 err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3145 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
3146 (void *)dev, strerror(err));
3154 * Configure a RX queue.
3157 * Pointer to Ethernet device structure.
3159 * Pointer to RX queue structure.
3161 * Number of descriptors to configure in queue.
3163 * NUMA socket on which memory must be allocated.
3165 * Thresholds parameters.
3167 * Memory pool for buffer allocations.
3170 * 0 on success, errno value on failure.
3173 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
3174 unsigned int socket, const struct rte_eth_rxconf *conf,
3175 struct rte_mempool *mp)
3177 struct priv *priv = dev->data->dev_private;
3183 struct ibv_exp_qp_attr mod;
3185 struct ibv_exp_query_intf_params params;
3187 enum ibv_exp_query_intf_status status;
3188 struct ibv_recv_wr *bad_wr;
3189 struct rte_mbuf *buf;
3191 int parent = (rxq == &priv->rxq_parent);
3193 (void)conf; /* Thresholds configuration (ignored). */
3195 * If this is a parent queue, hardware must support RSS and
3196 * RSS must be enabled.
3198 assert((!parent) || ((priv->hw_rss) && (priv->rss)));
3200 /* Even if unused, ibv_create_cq() requires at least one
3205 if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
3206 ERROR("%p: invalid number of RX descriptors (must be a"
3207 " multiple of %d)", (void *)dev, MLX4_PMD_SGE_WR_N);
3210 /* Get mbuf length. */
3211 buf = rte_pktmbuf_alloc(mp);
3213 ERROR("%p: unable to allocate mbuf", (void *)dev);
3216 tmpl.mb_len = buf->buf_len;
3217 assert((rte_pktmbuf_headroom(buf) +
3218 rte_pktmbuf_tailroom(buf)) == tmpl.mb_len);
3219 assert(rte_pktmbuf_headroom(buf) == RTE_PKTMBUF_HEADROOM);
3220 rte_pktmbuf_free(buf);
3221 /* Enable scattered packets support for this queue if necessary. */
3222 if ((dev->data->dev_conf.rxmode.jumbo_frame) &&
3223 (dev->data->dev_conf.rxmode.max_rx_pkt_len >
3224 (tmpl.mb_len - RTE_PKTMBUF_HEADROOM))) {
3226 desc /= MLX4_PMD_SGE_WR_N;
3228 DEBUG("%p: %s scattered packets support (%u WRs)",
3229 (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc);
3230 /* Use the entire RX mempool as the memory region. */
3231 tmpl.mr = ibv_reg_mr(priv->pd,
3232 (void *)mp->elt_va_start,
3233 (mp->elt_va_end - mp->elt_va_start),
3234 (IBV_ACCESS_LOCAL_WRITE |
3235 IBV_ACCESS_REMOTE_WRITE));
3236 if (tmpl.mr == NULL) {
3238 ERROR("%p: MR creation failure: %s",
3239 (void *)dev, strerror(ret));
3243 tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
3244 if (tmpl.cq == NULL) {
3246 ERROR("%p: CQ creation failure: %s",
3247 (void *)dev, strerror(ret));
3250 DEBUG("priv->device_attr.max_qp_wr is %d",
3251 priv->device_attr.max_qp_wr);
3252 DEBUG("priv->device_attr.max_sge is %d",
3253 priv->device_attr.max_sge);
3256 tmpl.qp = rxq_setup_qp_rss(priv, tmpl.cq, desc, parent);
3258 #endif /* RSS_SUPPORT */
3259 tmpl.qp = rxq_setup_qp(priv, tmpl.cq, desc);
3260 if (tmpl.qp == NULL) {
3261 ret = (errno ? errno : EINVAL);
3262 ERROR("%p: QP creation failure: %s",
3263 (void *)dev, strerror(ret));
3266 mod = (struct ibv_exp_qp_attr){
3267 /* Move the QP to this state. */
3268 .qp_state = IBV_QPS_INIT,
3269 /* Primary port number. */
3270 .port_num = priv->port
3272 ret = ibv_exp_modify_qp(tmpl.qp, &mod,
3275 (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
3276 #endif /* RSS_SUPPORT */
3279 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
3280 (void *)dev, strerror(ret));
3283 if ((parent) || (!priv->rss)) {
3284 /* Configure MAC and broadcast addresses. */
3285 ret = rxq_mac_addrs_add(&tmpl);
3287 ERROR("%p: QP flow attachment failed: %s",
3288 (void *)dev, strerror(ret));
3292 /* Allocate descriptors for RX queues, except for the RSS parent. */
3296 ret = rxq_alloc_elts_sp(&tmpl, desc, NULL);
3298 ret = rxq_alloc_elts(&tmpl, desc, NULL);
3300 ERROR("%p: RXQ allocation failed: %s",
3301 (void *)dev, strerror(ret));
3304 ret = ibv_post_recv(tmpl.qp,
3306 &(*tmpl.elts.sp)[0].wr :
3307 &(*tmpl.elts.no_sp)[0].wr),
3310 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
3317 mod = (struct ibv_exp_qp_attr){
3318 .qp_state = IBV_QPS_RTR
3320 ret = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3322 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
3323 (void *)dev, strerror(ret));
3327 tmpl.port_id = dev->data->port_id;
3328 DEBUG("%p: RTE port ID: %u", (void *)rxq, tmpl.port_id);
3329 attr.params = (struct ibv_exp_query_intf_params){
3330 .intf_scope = IBV_EXP_INTF_GLOBAL,
3331 .intf = IBV_EXP_INTF_CQ,
3334 tmpl.if_cq = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
3335 if (tmpl.if_cq == NULL) {
3336 ERROR("%p: CQ interface family query failed with status %d",
3337 (void *)dev, status);
3340 attr.params = (struct ibv_exp_query_intf_params){
3341 .intf_scope = IBV_EXP_INTF_GLOBAL,
3342 .intf = IBV_EXP_INTF_QP_BURST,
3345 tmpl.if_qp = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
3346 if (tmpl.if_qp == NULL) {
3347 ERROR("%p: QP interface family query failed with status %d",
3348 (void *)dev, status);
3351 /* Clean up rxq in case we're reinitializing it. */
3352 DEBUG("%p: cleaning-up old rxq just in case", (void *)rxq);
3355 DEBUG("%p: rxq updated with %p", (void *)rxq, (void *)&tmpl);
3365 * DPDK callback to configure a RX queue.
3368 * Pointer to Ethernet device structure.
3372 * Number of descriptors to configure in queue.
3374 * NUMA socket on which memory must be allocated.
3376 * Thresholds parameters.
3378 * Memory pool for buffer allocations.
3381 * 0 on success, negative errno value on failure.
3384 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
3385 unsigned int socket, const struct rte_eth_rxconf *conf,
3386 struct rte_mempool *mp)
3388 struct priv *priv = dev->data->dev_private;
3389 struct rxq *rxq = (*priv->rxqs)[idx];
3393 DEBUG("%p: configuring queue %u for %u descriptors",
3394 (void *)dev, idx, desc);
3395 if (idx >= priv->rxqs_n) {
3396 ERROR("%p: queue index out of range (%u >= %u)",
3397 (void *)dev, idx, priv->rxqs_n);
3402 DEBUG("%p: reusing already allocated queue index %u (%p)",
3403 (void *)dev, idx, (void *)rxq);
3404 if (priv->started) {
3408 (*priv->rxqs)[idx] = NULL;
3411 rxq = rte_calloc_socket("RXQ", 1, sizeof(*rxq), 0, socket);
3413 ERROR("%p: unable to allocate queue index %u",
3419 ret = rxq_setup(dev, rxq, desc, socket, conf, mp);
3423 rxq->stats.idx = idx;
3424 DEBUG("%p: adding RX queue %p to list",
3425 (void *)dev, (void *)rxq);
3426 (*priv->rxqs)[idx] = rxq;
3427 /* Update receive callback. */
3429 dev->rx_pkt_burst = mlx4_rx_burst_sp;
3431 dev->rx_pkt_burst = mlx4_rx_burst;
3438 * DPDK callback to release a RX queue.
3441 * Generic RX queue pointer.
3444 mlx4_rx_queue_release(void *dpdk_rxq)
3446 struct rxq *rxq = (struct rxq *)dpdk_rxq;
3454 assert(rxq != &priv->rxq_parent);
3455 for (i = 0; (i != priv->rxqs_n); ++i)
3456 if ((*priv->rxqs)[i] == rxq) {
3457 DEBUG("%p: removing RX queue %p from list",
3458 (void *)priv->dev, (void *)rxq);
3459 (*priv->rxqs)[i] = NULL;
3468 * DPDK callback to start the device.
3470 * Simulate device start by attaching all configured flows.
3473 * Pointer to Ethernet device structure.
3476 * 0 on success, negative errno value on failure.
3479 mlx4_dev_start(struct rte_eth_dev *dev)
3481 struct priv *priv = dev->data->dev_private;
3487 if (priv->started) {
3491 DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
3494 rxq = &priv->rxq_parent;
3497 rxq = (*priv->rxqs)[0];
3500 /* Iterate only once when RSS is enabled. */
3504 /* Ignore nonexistent RX queues. */
3507 ret = rxq_mac_addrs_add(rxq);
3508 if (!ret && priv->promisc)
3509 ret = rxq_promiscuous_enable(rxq);
3510 if (!ret && priv->allmulti)
3511 ret = rxq_allmulticast_enable(rxq);
3514 WARN("%p: QP flow attachment failed: %s",
3515 (void *)dev, strerror(ret));
3518 rxq = (*priv->rxqs)[--i];
3520 rxq_allmulticast_disable(rxq);
3521 rxq_promiscuous_disable(rxq);
3522 rxq_mac_addrs_del(rxq);
3527 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
3533 * DPDK callback to stop the device.
3535 * Simulate device stop by detaching all configured flows.
3538 * Pointer to Ethernet device structure.
3541 mlx4_dev_stop(struct rte_eth_dev *dev)
3543 struct priv *priv = dev->data->dev_private;
3549 if (!priv->started) {
3553 DEBUG("%p: detaching flows from all RX queues", (void *)dev);
3556 rxq = &priv->rxq_parent;
3559 rxq = (*priv->rxqs)[0];
3562 /* Iterate only once when RSS is enabled. */
3564 /* Ignore nonexistent RX queues. */
3567 rxq_allmulticast_disable(rxq);
3568 rxq_promiscuous_disable(rxq);
3569 rxq_mac_addrs_del(rxq);
3570 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
3575 * Dummy DPDK callback for TX.
3577 * This function is used to temporarily replace the real callback during
3578 * unsafe control operations on the queue, or in case of error.
3581 * Generic pointer to TX queue structure.
3583 * Packets to transmit.
3585 * Number of packets in array.
3588 * Number of packets successfully transmitted (<= pkts_n).
3591 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
3600 * Dummy DPDK callback for RX.
3602 * This function is used to temporarily replace the real callback during
3603 * unsafe control operations on the queue, or in case of error.
3606 * Generic pointer to RX queue structure.
3608 * Array to store received packets.
3610 * Maximum number of packets in array.
3613 * Number of packets successfully received (<= pkts_n).
3616 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
3625 * DPDK callback to close the device.
3627 * Destroy all queues and objects, free memory.
3630 * Pointer to Ethernet device structure.
3633 mlx4_dev_close(struct rte_eth_dev *dev)
3635 struct priv *priv = dev->data->dev_private;
3640 DEBUG("%p: closing device \"%s\"",
3642 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
3643 /* Prevent crashes when queues are still in use. This is unfortunately
3644 * still required for DPDK 1.3 because some programs (such as testpmd)
3645 * never release them before closing the device. */
3646 dev->rx_pkt_burst = removed_rx_burst;
3647 dev->tx_pkt_burst = removed_tx_burst;
3648 if (priv->rxqs != NULL) {
3649 /* XXX race condition if mlx4_rx_burst() is still running. */
3651 for (i = 0; (i != priv->rxqs_n); ++i) {
3652 tmp = (*priv->rxqs)[i];
3655 (*priv->rxqs)[i] = NULL;
3662 if (priv->txqs != NULL) {
3663 /* XXX race condition if mlx4_tx_burst() is still running. */
3665 for (i = 0; (i != priv->txqs_n); ++i) {
3666 tmp = (*priv->txqs)[i];
3669 (*priv->txqs)[i] = NULL;
3677 rxq_cleanup(&priv->rxq_parent);
3678 if (priv->pd != NULL) {
3679 assert(priv->ctx != NULL);
3680 claim_zero(ibv_dealloc_pd(priv->pd));
3681 claim_zero(ibv_close_device(priv->ctx));
3683 assert(priv->ctx == NULL);
3685 memset(priv, 0, sizeof(*priv));
3689 * DPDK callback to get information about the device.
3692 * Pointer to Ethernet device structure.
3694 * Info structure output buffer.
3697 mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
3699 struct priv *priv = dev->data->dev_private;
3703 /* FIXME: we should ask the device for these values. */
3704 info->min_rx_bufsize = 32;
3705 info->max_rx_pktlen = 65536;
3707 * Since we need one CQ per QP, the limit is the minimum number
3708 * between the two values.
3710 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
3711 priv->device_attr.max_qp : priv->device_attr.max_cq);
3712 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
3715 info->max_rx_queues = max;
3716 info->max_tx_queues = max;
3717 info->max_mac_addrs = elemof(priv->mac);
3722 * DPDK callback to get device statistics.
3725 * Pointer to Ethernet device structure.
3727 * Stats structure output buffer.
3730 mlx4_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
3732 struct priv *priv = dev->data->dev_private;
3733 struct rte_eth_stats tmp = {0};
3738 /* Add software counters. */
3739 for (i = 0; (i != priv->rxqs_n); ++i) {
3740 struct rxq *rxq = (*priv->rxqs)[i];
3744 idx = rxq->stats.idx;
3745 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
3746 #ifdef MLX4_PMD_SOFT_COUNTERS
3747 tmp.q_ipackets[idx] += rxq->stats.ipackets;
3748 tmp.q_ibytes[idx] += rxq->stats.ibytes;
3750 tmp.q_errors[idx] += (rxq->stats.idropped +
3751 rxq->stats.rx_nombuf);
3753 #ifdef MLX4_PMD_SOFT_COUNTERS
3754 tmp.ipackets += rxq->stats.ipackets;
3755 tmp.ibytes += rxq->stats.ibytes;
3757 tmp.ierrors += rxq->stats.idropped;
3758 tmp.rx_nombuf += rxq->stats.rx_nombuf;
3760 for (i = 0; (i != priv->txqs_n); ++i) {
3761 struct txq *txq = (*priv->txqs)[i];
3765 idx = txq->stats.idx;
3766 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
3767 #ifdef MLX4_PMD_SOFT_COUNTERS
3768 tmp.q_opackets[idx] += txq->stats.opackets;
3769 tmp.q_obytes[idx] += txq->stats.obytes;
3771 tmp.q_errors[idx] += txq->stats.odropped;
3773 #ifdef MLX4_PMD_SOFT_COUNTERS
3774 tmp.opackets += txq->stats.opackets;
3775 tmp.obytes += txq->stats.obytes;
3777 tmp.oerrors += txq->stats.odropped;
3779 #ifndef MLX4_PMD_SOFT_COUNTERS
3780 /* FIXME: retrieve and add hardware counters. */
3787 * DPDK callback to clear device statistics.
3790 * Pointer to Ethernet device structure.
3793 mlx4_stats_reset(struct rte_eth_dev *dev)
3795 struct priv *priv = dev->data->dev_private;
3800 for (i = 0; (i != priv->rxqs_n); ++i) {
3801 if ((*priv->rxqs)[i] == NULL)
3803 idx = (*priv->rxqs)[i]->stats.idx;
3804 (*priv->rxqs)[i]->stats =
3805 (struct mlx4_rxq_stats){ .idx = idx };
3807 for (i = 0; (i != priv->txqs_n); ++i) {
3808 if ((*priv->txqs)[i] == NULL)
3810 idx = (*priv->rxqs)[i]->stats.idx;
3811 (*priv->txqs)[i]->stats =
3812 (struct mlx4_txq_stats){ .idx = idx };
3814 #ifndef MLX4_PMD_SOFT_COUNTERS
3815 /* FIXME: reset hardware counters. */
3821 * DPDK callback to remove a MAC address.
3824 * Pointer to Ethernet device structure.
3826 * MAC address index.
3829 mlx4_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
3831 struct priv *priv = dev->data->dev_private;
3834 DEBUG("%p: removing MAC address from index %" PRIu32,
3835 (void *)dev, index);
3836 if (index >= MLX4_MAX_MAC_ADDRESSES)
3838 /* Refuse to remove the broadcast address, this one is special. */
3839 if (!memcmp(priv->mac[index].addr_bytes, "\xff\xff\xff\xff\xff\xff",
3842 priv_mac_addr_del(priv, index);
3848 * DPDK callback to add a MAC address.
3851 * Pointer to Ethernet device structure.
3853 * MAC address to register.
3855 * MAC address index.
3857 * VMDq pool index to associate address with (ignored).
3860 mlx4_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
3861 uint32_t index, uint32_t vmdq)
3863 struct priv *priv = dev->data->dev_private;
3867 DEBUG("%p: adding MAC address at index %" PRIu32,
3868 (void *)dev, index);
3869 if (index >= MLX4_MAX_MAC_ADDRESSES)
3871 /* Refuse to add the broadcast address, this one is special. */
3872 if (!memcmp(mac_addr->addr_bytes, "\xff\xff\xff\xff\xff\xff",
3875 priv_mac_addr_add(priv, index,
3876 (const uint8_t (*)[ETHER_ADDR_LEN])
3877 mac_addr->addr_bytes);
3883 * DPDK callback to enable promiscuous mode.
3886 * Pointer to Ethernet device structure.
3889 mlx4_promiscuous_enable(struct rte_eth_dev *dev)
3891 struct priv *priv = dev->data->dev_private;
3896 if (priv->promisc) {
3900 /* If device isn't started, this is all we need to do. */
3904 ret = rxq_promiscuous_enable(&priv->rxq_parent);
3911 for (i = 0; (i != priv->rxqs_n); ++i) {
3912 if ((*priv->rxqs)[i] == NULL)
3914 ret = rxq_promiscuous_enable((*priv->rxqs)[i]);
3917 /* Failure, rollback. */
3919 if ((*priv->rxqs)[--i] != NULL)
3920 rxq_promiscuous_disable((*priv->rxqs)[i]);
3930 * DPDK callback to disable promiscuous mode.
3933 * Pointer to Ethernet device structure.
3936 mlx4_promiscuous_disable(struct rte_eth_dev *dev)
3938 struct priv *priv = dev->data->dev_private;
3942 if (!priv->promisc) {
3947 rxq_promiscuous_disable(&priv->rxq_parent);
3950 for (i = 0; (i != priv->rxqs_n); ++i)
3951 if ((*priv->rxqs)[i] != NULL)
3952 rxq_promiscuous_disable((*priv->rxqs)[i]);
3959 * DPDK callback to enable allmulti mode.
3962 * Pointer to Ethernet device structure.
3965 mlx4_allmulticast_enable(struct rte_eth_dev *dev)
3967 struct priv *priv = dev->data->dev_private;
3972 if (priv->allmulti) {
3976 /* If device isn't started, this is all we need to do. */
3980 ret = rxq_allmulticast_enable(&priv->rxq_parent);
3987 for (i = 0; (i != priv->rxqs_n); ++i) {
3988 if ((*priv->rxqs)[i] == NULL)
3990 ret = rxq_allmulticast_enable((*priv->rxqs)[i]);
3993 /* Failure, rollback. */
3995 if ((*priv->rxqs)[--i] != NULL)
3996 rxq_allmulticast_disable((*priv->rxqs)[i]);
4006 * DPDK callback to disable allmulti mode.
4009 * Pointer to Ethernet device structure.
4012 mlx4_allmulticast_disable(struct rte_eth_dev *dev)
4014 struct priv *priv = dev->data->dev_private;
4018 if (!priv->allmulti) {
4023 rxq_allmulticast_disable(&priv->rxq_parent);
4026 for (i = 0; (i != priv->rxqs_n); ++i)
4027 if ((*priv->rxqs)[i] != NULL)
4028 rxq_allmulticast_disable((*priv->rxqs)[i]);
4035 * DPDK callback to retrieve physical link information (unlocked version).
4038 * Pointer to Ethernet device structure.
4039 * @param wait_to_complete
4040 * Wait for request completion (ignored).
4043 mlx4_link_update_unlocked(struct rte_eth_dev *dev, int wait_to_complete)
4045 struct priv *priv = dev->data->dev_private;
4046 struct ethtool_cmd edata = {
4050 struct rte_eth_link dev_link;
4053 (void)wait_to_complete;
4054 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
4055 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
4058 memset(&dev_link, 0, sizeof(dev_link));
4059 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
4060 (ifr.ifr_flags & IFF_RUNNING));
4061 ifr.ifr_data = &edata;
4062 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4063 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
4067 link_speed = ethtool_cmd_speed(&edata);
4068 if (link_speed == -1)
4069 dev_link.link_speed = 0;
4071 dev_link.link_speed = link_speed;
4072 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
4073 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
4074 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
4075 /* Link status changed. */
4076 dev->data->dev_link = dev_link;
4079 /* Link status is still the same. */
4084 * DPDK callback to retrieve physical link information.
4087 * Pointer to Ethernet device structure.
4088 * @param wait_to_complete
4089 * Wait for request completion (ignored).
4092 mlx4_link_update(struct rte_eth_dev *dev, int wait_to_complete)
4094 struct priv *priv = dev->data->dev_private;
4098 ret = mlx4_link_update_unlocked(dev, wait_to_complete);
4104 * DPDK callback to change the MTU.
4106 * Setting the MTU affects hardware MRU (packets larger than the MTU cannot be
4107 * received). Use this as a hint to enable/disable scattered packets support
4108 * and improve performance when not needed.
4109 * Since failure is not an option, reconfiguring queues on the fly is not
4113 * Pointer to Ethernet device structure.
4118 * 0 on success, negative errno value on failure.
4121 mlx4_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
4123 struct priv *priv = dev->data->dev_private;
4126 uint16_t (*rx_func)(void *, struct rte_mbuf **, uint16_t) =
4130 /* Set kernel interface MTU first. */
4131 if (priv_set_mtu(priv, mtu)) {
4133 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
4137 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
4139 /* Temporarily replace RX handler with a fake one, assuming it has not
4140 * been copied elsewhere. */
4141 dev->rx_pkt_burst = removed_rx_burst;
4142 /* Make sure everyone has left mlx4_rx_burst() and uses
4143 * removed_rx_burst() instead. */
4146 /* Reconfigure each RX queue. */
4147 for (i = 0; (i != priv->rxqs_n); ++i) {
4148 struct rxq *rxq = (*priv->rxqs)[i];
4149 unsigned int max_frame_len;
4154 /* Calculate new maximum frame length according to MTU and
4155 * toggle scattered support (sp) if necessary. */
4156 max_frame_len = (priv->mtu + ETHER_HDR_LEN +
4157 (ETHER_MAX_VLAN_FRAME_LEN - ETHER_MAX_LEN));
4158 sp = (max_frame_len > (rxq->mb_len - RTE_PKTMBUF_HEADROOM));
4159 /* Provide new values to rxq_setup(). */
4160 dev->data->dev_conf.rxmode.jumbo_frame = sp;
4161 dev->data->dev_conf.rxmode.max_rx_pkt_len = max_frame_len;
4162 ret = rxq_rehash(dev, rxq);
4164 /* Force SP RX if that queue requires it and abort. */
4166 rx_func = mlx4_rx_burst_sp;
4169 /* Reenable non-RSS queue attributes. No need to check
4170 * for errors at this stage. */
4172 rxq_mac_addrs_add(rxq);
4174 rxq_promiscuous_enable(rxq);
4176 rxq_allmulticast_enable(rxq);
4178 /* Scattered burst function takes priority. */
4180 rx_func = mlx4_rx_burst_sp;
4182 /* Burst functions can now be called again. */
4184 dev->rx_pkt_burst = rx_func;
4192 * DPDK callback to get flow control status.
4195 * Pointer to Ethernet device structure.
4196 * @param[out] fc_conf
4197 * Flow control output buffer.
4200 * 0 on success, negative errno value on failure.
4203 mlx4_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4205 struct priv *priv = dev->data->dev_private;
4207 struct ethtool_pauseparam ethpause = {
4208 .cmd = ETHTOOL_GPAUSEPARAM
4212 ifr.ifr_data = ðpause;
4214 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4216 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
4222 fc_conf->autoneg = ethpause.autoneg;
4223 if (ethpause.rx_pause && ethpause.tx_pause)
4224 fc_conf->mode = RTE_FC_FULL;
4225 else if (ethpause.rx_pause)
4226 fc_conf->mode = RTE_FC_RX_PAUSE;
4227 else if (ethpause.tx_pause)
4228 fc_conf->mode = RTE_FC_TX_PAUSE;
4230 fc_conf->mode = RTE_FC_NONE;
4240 * DPDK callback to modify flow control parameters.
4243 * Pointer to Ethernet device structure.
4244 * @param[in] fc_conf
4245 * Flow control parameters.
4248 * 0 on success, negative errno value on failure.
4251 mlx4_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4253 struct priv *priv = dev->data->dev_private;
4255 struct ethtool_pauseparam ethpause = {
4256 .cmd = ETHTOOL_SPAUSEPARAM
4260 ifr.ifr_data = ðpause;
4261 ethpause.autoneg = fc_conf->autoneg;
4262 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4263 (fc_conf->mode & RTE_FC_RX_PAUSE))
4264 ethpause.rx_pause = 1;
4266 ethpause.rx_pause = 0;
4268 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4269 (fc_conf->mode & RTE_FC_TX_PAUSE))
4270 ethpause.tx_pause = 1;
4272 ethpause.tx_pause = 0;
4275 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4277 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
4291 * Configure a VLAN filter.
4294 * Pointer to Ethernet device structure.
4296 * VLAN ID to filter.
4301 * 0 on success, errno value on failure.
4304 vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
4306 struct priv *priv = dev->data->dev_private;
4308 unsigned int j = -1;
4310 DEBUG("%p: %s VLAN filter ID %" PRIu16,
4311 (void *)dev, (on ? "enable" : "disable"), vlan_id);
4312 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
4313 if (!priv->vlan_filter[i].enabled) {
4314 /* Unused index, remember it. */
4318 if (priv->vlan_filter[i].id != vlan_id)
4320 /* This VLAN ID is already known, use its index. */
4324 /* Check if there's room for another VLAN filter. */
4325 if (j == (unsigned int)-1)
4328 * VLAN filters apply to all configured MAC addresses, flow
4329 * specifications must be reconfigured accordingly.
4331 priv->vlan_filter[j].id = vlan_id;
4332 if ((on) && (!priv->vlan_filter[j].enabled)) {
4334 * Filter is disabled, enable it.
4335 * Rehashing flows in all RX queues is necessary.
4338 rxq_mac_addrs_del(&priv->rxq_parent);
4340 for (i = 0; (i != priv->rxqs_n); ++i)
4341 if ((*priv->rxqs)[i] != NULL)
4342 rxq_mac_addrs_del((*priv->rxqs)[i]);
4343 priv->vlan_filter[j].enabled = 1;
4344 if (priv->started) {
4346 rxq_mac_addrs_add(&priv->rxq_parent);
4348 for (i = 0; (i != priv->rxqs_n); ++i) {
4349 if ((*priv->rxqs)[i] == NULL)
4351 rxq_mac_addrs_add((*priv->rxqs)[i]);
4354 } else if ((!on) && (priv->vlan_filter[j].enabled)) {
4356 * Filter is enabled, disable it.
4357 * Rehashing flows in all RX queues is necessary.
4360 rxq_mac_addrs_del(&priv->rxq_parent);
4362 for (i = 0; (i != priv->rxqs_n); ++i)
4363 if ((*priv->rxqs)[i] != NULL)
4364 rxq_mac_addrs_del((*priv->rxqs)[i]);
4365 priv->vlan_filter[j].enabled = 0;
4366 if (priv->started) {
4368 rxq_mac_addrs_add(&priv->rxq_parent);
4370 for (i = 0; (i != priv->rxqs_n); ++i) {
4371 if ((*priv->rxqs)[i] == NULL)
4373 rxq_mac_addrs_add((*priv->rxqs)[i]);
4381 * DPDK callback to configure a VLAN filter.
4384 * Pointer to Ethernet device structure.
4386 * VLAN ID to filter.
4391 * 0 on success, negative errno value on failure.
4394 mlx4_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
4396 struct priv *priv = dev->data->dev_private;
4400 ret = vlan_filter_set(dev, vlan_id, on);
4406 static const struct eth_dev_ops mlx4_dev_ops = {
4407 .dev_configure = mlx4_dev_configure,
4408 .dev_start = mlx4_dev_start,
4409 .dev_stop = mlx4_dev_stop,
4410 .dev_close = mlx4_dev_close,
4411 .promiscuous_enable = mlx4_promiscuous_enable,
4412 .promiscuous_disable = mlx4_promiscuous_disable,
4413 .allmulticast_enable = mlx4_allmulticast_enable,
4414 .allmulticast_disable = mlx4_allmulticast_disable,
4415 .link_update = mlx4_link_update,
4416 .stats_get = mlx4_stats_get,
4417 .stats_reset = mlx4_stats_reset,
4418 .queue_stats_mapping_set = NULL,
4419 .dev_infos_get = mlx4_dev_infos_get,
4420 .vlan_filter_set = mlx4_vlan_filter_set,
4421 .vlan_tpid_set = NULL,
4422 .vlan_strip_queue_set = NULL,
4423 .vlan_offload_set = NULL,
4424 .rx_queue_setup = mlx4_rx_queue_setup,
4425 .tx_queue_setup = mlx4_tx_queue_setup,
4426 .rx_queue_release = mlx4_rx_queue_release,
4427 .tx_queue_release = mlx4_tx_queue_release,
4429 .dev_led_off = NULL,
4430 .flow_ctrl_get = mlx4_dev_get_flow_ctrl,
4431 .flow_ctrl_set = mlx4_dev_set_flow_ctrl,
4432 .priority_flow_ctrl_set = NULL,
4433 .mac_addr_remove = mlx4_mac_addr_remove,
4434 .mac_addr_add = mlx4_mac_addr_add,
4435 .mtu_set = mlx4_dev_set_mtu,
4436 .fdir_add_signature_filter = NULL,
4437 .fdir_update_signature_filter = NULL,
4438 .fdir_remove_signature_filter = NULL,
4439 .fdir_add_perfect_filter = NULL,
4440 .fdir_update_perfect_filter = NULL,
4441 .fdir_remove_perfect_filter = NULL,
4442 .fdir_set_masks = NULL
4446 * Get PCI information from struct ibv_device.
4449 * Pointer to Ethernet device structure.
4450 * @param[out] pci_addr
4451 * PCI bus address output buffer.
4454 * 0 on success, -1 on failure and errno is set.
4457 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
4458 struct rte_pci_addr *pci_addr)
4462 MKSTR(path, "%s/device/uevent", device->ibdev_path);
4464 file = fopen(path, "rb");
4467 while (fgets(line, sizeof(line), file) == line) {
4468 size_t len = strlen(line);
4471 /* Truncate long lines. */
4472 if (len == (sizeof(line) - 1))
4473 while (line[(len - 1)] != '\n') {
4477 line[(len - 1)] = ret;
4479 /* Extract information. */
4482 "%" SCNx16 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
4486 &pci_addr->function) == 4) {
4496 * Get MAC address by querying netdevice.
4499 * struct priv for the requested device.
4501 * MAC address output buffer.
4504 * 0 on success, -1 on failure and errno is set.
4507 priv_get_mac(struct priv *priv, uint8_t (*mac)[ETHER_ADDR_LEN])
4509 struct ifreq request;
4511 if (priv_ifreq(priv, SIOCGIFHWADDR, &request))
4513 memcpy(mac, request.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
4517 /* Support up to 32 adapters. */
4519 struct rte_pci_addr pci_addr; /* associated PCI address */
4520 uint32_t ports; /* physical ports bitfield. */
4524 * Get device index in mlx4_dev[] from PCI bus address.
4526 * @param[in] pci_addr
4527 * PCI bus address to look for.
4530 * mlx4_dev[] index on success, -1 on failure.
4533 mlx4_dev_idx(struct rte_pci_addr *pci_addr)
4538 assert(pci_addr != NULL);
4539 for (i = 0; (i != elemof(mlx4_dev)); ++i) {
4540 if ((mlx4_dev[i].pci_addr.domain == pci_addr->domain) &&
4541 (mlx4_dev[i].pci_addr.bus == pci_addr->bus) &&
4542 (mlx4_dev[i].pci_addr.devid == pci_addr->devid) &&
4543 (mlx4_dev[i].pci_addr.function == pci_addr->function))
4545 if ((mlx4_dev[i].ports == 0) && (ret == -1))
4552 * Retrieve integer value from environment variable.
4555 * Environment variable name.
4558 * Integer value, 0 if the variable is not set.
4561 mlx4_getenv_int(const char *name)
4563 const char *val = getenv(name);
4570 static struct eth_driver mlx4_driver;
4573 * DPDK callback to register a PCI device.
4575 * This function creates an Ethernet device for each port of a given
4578 * @param[in] pci_drv
4579 * PCI driver structure (mlx4_driver).
4580 * @param[in] pci_dev
4581 * PCI device information.
4584 * 0 on success, negative errno value on failure.
4587 mlx4_pci_devinit(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
4589 struct ibv_device **list;
4590 struct ibv_device *ibv_dev;
4592 struct ibv_context *attr_ctx = NULL;
4593 struct ibv_device_attr device_attr;
4599 assert(pci_drv == &mlx4_driver.pci_drv);
4600 /* Get mlx4_dev[] index. */
4601 idx = mlx4_dev_idx(&pci_dev->addr);
4603 ERROR("this driver cannot support any more adapters");
4606 DEBUG("using driver device index %d", idx);
4608 /* Save PCI address. */
4609 mlx4_dev[idx].pci_addr = pci_dev->addr;
4610 list = ibv_get_device_list(&i);
4613 if (errno == ENOSYS) {
4614 WARN("cannot list devices, is ib_uverbs loaded?");
4621 * For each listed device, check related sysfs entry against
4622 * the provided PCI ID.
4625 struct rte_pci_addr pci_addr;
4628 DEBUG("checking device \"%s\"", list[i]->name);
4629 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
4631 if ((pci_dev->addr.domain != pci_addr.domain) ||
4632 (pci_dev->addr.bus != pci_addr.bus) ||
4633 (pci_dev->addr.devid != pci_addr.devid) ||
4634 (pci_dev->addr.function != pci_addr.function))
4636 vf = (pci_dev->id.device_id ==
4637 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
4638 INFO("PCI information matches, using device \"%s\" (VF: %s)",
4639 list[i]->name, (vf ? "true" : "false"));
4640 attr_ctx = ibv_open_device(list[i]);
4644 if (attr_ctx == NULL) {
4645 ibv_free_device_list(list);
4648 WARN("cannot access device, is mlx4_ib loaded?");
4651 WARN("cannot use device, are drivers up to date?");
4659 DEBUG("device opened");
4660 if (ibv_query_device(attr_ctx, &device_attr))
4662 INFO("%u port(s) detected", device_attr.phys_port_cnt);
4664 for (i = 0; i < device_attr.phys_port_cnt; i++) {
4665 uint32_t port = i + 1; /* ports are indexed from one */
4666 uint32_t test = (1 << i);
4667 struct ibv_context *ctx = NULL;
4668 struct ibv_port_attr port_attr;
4669 struct ibv_pd *pd = NULL;
4670 struct priv *priv = NULL;
4671 struct rte_eth_dev *eth_dev;
4672 #ifdef HAVE_EXP_QUERY_DEVICE
4673 struct ibv_exp_device_attr exp_device_attr;
4674 #endif /* HAVE_EXP_QUERY_DEVICE */
4675 struct ether_addr mac;
4677 #ifdef HAVE_EXP_QUERY_DEVICE
4678 exp_device_attr.comp_mask = IBV_EXP_DEVICE_ATTR_EXP_CAP_FLAGS;
4680 exp_device_attr.comp_mask |= IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ;
4681 #endif /* RSS_SUPPORT */
4682 #endif /* HAVE_EXP_QUERY_DEVICE */
4684 DEBUG("using port %u (%08" PRIx32 ")", port, test);
4686 ctx = ibv_open_device(ibv_dev);
4690 /* Check port status. */
4691 err = ibv_query_port(ctx, port, &port_attr);
4693 ERROR("port query failed: %s", strerror(err));
4696 if (port_attr.state != IBV_PORT_ACTIVE)
4697 WARN("bad state for port %d: \"%s\" (%d)",
4698 port, ibv_port_state_str(port_attr.state),
4701 /* Allocate protection domain. */
4702 pd = ibv_alloc_pd(ctx);
4704 ERROR("PD allocation failure");
4709 mlx4_dev[idx].ports |= test;
4711 /* from rte_ethdev.c */
4712 priv = rte_zmalloc("ethdev private structure",
4714 RTE_CACHE_LINE_SIZE);
4716 ERROR("priv allocation failure");
4722 priv->device_attr = device_attr;
4725 priv->mtu = ETHER_MTU;
4726 #ifdef HAVE_EXP_QUERY_DEVICE
4727 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
4728 ERROR("ibv_exp_query_device() failed");
4732 if ((exp_device_attr.exp_device_cap_flags &
4733 IBV_EXP_DEVICE_QPG) &&
4734 (exp_device_attr.exp_device_cap_flags &
4735 IBV_EXP_DEVICE_UD_RSS) &&
4736 (exp_device_attr.comp_mask &
4737 IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ) &&
4738 (exp_device_attr.max_rss_tbl_sz > 0)) {
4741 priv->max_rss_tbl_sz = exp_device_attr.max_rss_tbl_sz;
4745 priv->max_rss_tbl_sz = 0;
4747 priv->hw_tss = !!(exp_device_attr.exp_device_cap_flags &
4748 IBV_EXP_DEVICE_UD_TSS);
4749 DEBUG("device flags: %s%s%s",
4750 (priv->hw_qpg ? "IBV_DEVICE_QPG " : ""),
4751 (priv->hw_tss ? "IBV_DEVICE_TSS " : ""),
4752 (priv->hw_rss ? "IBV_DEVICE_RSS " : ""));
4754 DEBUG("maximum RSS indirection table size: %u",
4755 exp_device_attr.max_rss_tbl_sz);
4756 #endif /* RSS_SUPPORT */
4759 priv->inl_recv_size = mlx4_getenv_int("MLX4_INLINE_RECV_SIZE");
4761 if (priv->inl_recv_size) {
4762 exp_device_attr.comp_mask =
4763 IBV_EXP_DEVICE_ATTR_INLINE_RECV_SZ;
4764 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
4765 INFO("Couldn't query device for inline-receive"
4767 priv->inl_recv_size = 0;
4769 if ((unsigned)exp_device_attr.inline_recv_sz <
4770 priv->inl_recv_size) {
4771 INFO("Max inline-receive (%d) <"
4772 " requested inline-receive (%u)",
4773 exp_device_attr.inline_recv_sz,
4774 priv->inl_recv_size);
4775 priv->inl_recv_size =
4776 exp_device_attr.inline_recv_sz;
4779 INFO("Set inline receive size to %u",
4780 priv->inl_recv_size);
4782 #endif /* INLINE_RECV */
4783 #endif /* HAVE_EXP_QUERY_DEVICE */
4785 (void)mlx4_getenv_int;
4787 /* Configure the first MAC address by default. */
4788 if (priv_get_mac(priv, &mac.addr_bytes)) {
4789 ERROR("cannot get MAC address, is mlx4_en loaded?"
4790 " (errno: %s)", strerror(errno));
4793 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
4795 mac.addr_bytes[0], mac.addr_bytes[1],
4796 mac.addr_bytes[2], mac.addr_bytes[3],
4797 mac.addr_bytes[4], mac.addr_bytes[5]);
4798 /* Register MAC and broadcast addresses. */
4799 claim_zero(priv_mac_addr_add(priv, 0,
4800 (const uint8_t (*)[ETHER_ADDR_LEN])
4802 claim_zero(priv_mac_addr_add(priv, 1,
4803 &(const uint8_t [ETHER_ADDR_LEN])
4804 { "\xff\xff\xff\xff\xff\xff" }));
4807 char ifname[IF_NAMESIZE];
4809 if (priv_get_ifname(priv, &ifname) == 0)
4810 DEBUG("port %u ifname is \"%s\"",
4811 priv->port, ifname);
4813 DEBUG("port %u ifname is unknown", priv->port);
4816 /* Get actual MTU if possible. */
4817 priv_get_mtu(priv, &priv->mtu);
4818 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
4820 /* from rte_ethdev.c */
4822 char name[RTE_ETH_NAME_MAX_LEN];
4824 snprintf(name, sizeof(name), "%s port %u",
4825 ibv_get_device_name(ibv_dev), port);
4826 eth_dev = rte_eth_dev_allocate(name, RTE_ETH_DEV_PCI);
4828 if (eth_dev == NULL) {
4829 ERROR("can not allocate rte ethdev");
4834 eth_dev->data->dev_private = priv;
4835 eth_dev->pci_dev = pci_dev;
4836 eth_dev->driver = &mlx4_driver;
4837 eth_dev->data->rx_mbuf_alloc_failed = 0;
4838 eth_dev->data->mtu = ETHER_MTU;
4840 priv->dev = eth_dev;
4841 eth_dev->dev_ops = &mlx4_dev_ops;
4842 eth_dev->data->mac_addrs = priv->mac;
4844 /* Bring Ethernet device up. */
4845 DEBUG("forcing Ethernet interface up");
4846 priv_set_flags(priv, ~IFF_UP, IFF_UP);
4852 claim_zero(ibv_dealloc_pd(pd));
4854 claim_zero(ibv_close_device(ctx));
4859 * XXX if something went wrong in the loop above, there is a resource
4860 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
4861 * long as the dpdk does not provide a way to deallocate a ethdev and a
4862 * way to enumerate the registered ethdevs to free the previous ones.
4865 /* no port found, complain */
4866 if (!mlx4_dev[idx].ports) {
4873 claim_zero(ibv_close_device(attr_ctx));
4875 ibv_free_device_list(list);
4880 static const struct rte_pci_id mlx4_pci_id_map[] = {
4882 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4883 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3,
4884 .subsystem_vendor_id = PCI_ANY_ID,
4885 .subsystem_device_id = PCI_ANY_ID
4888 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4889 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO,
4890 .subsystem_vendor_id = PCI_ANY_ID,
4891 .subsystem_device_id = PCI_ANY_ID
4894 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4895 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3VF,
4896 .subsystem_vendor_id = PCI_ANY_ID,
4897 .subsystem_device_id = PCI_ANY_ID
4904 static struct eth_driver mlx4_driver = {
4906 .name = MLX4_DRIVER_NAME,
4907 .id_table = mlx4_pci_id_map,
4908 .devinit = mlx4_pci_devinit,
4910 .dev_private_size = sizeof(struct priv)
4914 * Driver initialization routine.
4917 rte_mlx4_pmd_init(const char *name, const char *args)
4922 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
4923 * huge pages. Calling ibv_fork_init() during init allows
4924 * applications to use fork() safely for purposes other than
4925 * using this PMD, which is not supported in forked processes.
4927 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
4929 rte_eal_pci_register(&mlx4_driver.pci_drv);
4933 static struct rte_driver rte_mlx4_driver = {
4935 .name = MLX4_DRIVER_NAME,
4936 .init = rte_mlx4_pmd_init,
4939 PMD_REGISTER_DRIVER(rte_mlx4_driver)