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;
1029 * Copy scattered mbuf contents to a single linear buffer.
1031 * @param[out] linear
1032 * Linear output buffer.
1034 * Scattered input buffer.
1037 * Number of bytes copied to the output buffer or 0 if not large enough.
1040 linearize_mbuf(linear_t *linear, struct rte_mbuf *buf)
1042 unsigned int size = 0;
1043 unsigned int offset;
1046 unsigned int len = DATA_LEN(buf);
1050 if (unlikely(size > sizeof(*linear)))
1052 memcpy(&(*linear)[offset],
1053 rte_pktmbuf_mtod(buf, uint8_t *),
1056 } while (buf != NULL);
1061 * DPDK callback for TX.
1064 * Generic pointer to TX queue structure.
1066 * Packets to transmit.
1068 * Number of packets in array.
1071 * Number of packets successfully transmitted (<= pkts_n).
1074 mlx4_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
1076 struct txq *txq = (struct txq *)dpdk_txq;
1077 struct ibv_send_wr head;
1078 struct ibv_send_wr **wr_next = &head.next;
1079 unsigned int elts_head = txq->elts_head;
1080 const unsigned int elts_tail = txq->elts_tail;
1081 const unsigned int elts_n = txq->elts_n;
1082 unsigned int elts_comp_cd = txq->elts_comp_cd;
1083 unsigned int elts_comp = 0;
1088 assert(elts_comp_cd != 0);
1090 max = (elts_n - (elts_head - elts_tail));
1094 assert(max <= elts_n);
1095 /* Always leave one free entry in the ring. */
1101 for (i = 0; (i != max); ++i) {
1102 struct rte_mbuf *buf = pkts[i];
1103 struct txq_elt *elt = &(*txq->elts)[elts_head];
1104 struct ibv_send_wr *wr = &elt->wr;
1105 unsigned int segs = NB_SEGS(buf);
1106 #ifdef MLX4_PMD_SOFT_COUNTERS
1107 unsigned int sent_size = 0;
1111 uint32_t send_flags = 0;
1113 /* Clean up old buffer. */
1114 if (likely(WR_ID(wr->wr_id).offset != 0)) {
1115 struct rte_mbuf *tmp = (void *)
1116 ((uintptr_t)elt->sges[0].addr -
1117 WR_ID(wr->wr_id).offset);
1119 /* Faster than rte_pktmbuf_free(). */
1121 struct rte_mbuf *next = NEXT(tmp);
1123 rte_pktmbuf_free_seg(tmp);
1125 } while (tmp != NULL);
1129 WR_ID(wr->wr_id).offset = 0;
1130 for (j = 0; ((int)j < wr->num_sge); ++j) {
1131 elt->sges[j].addr = 0;
1132 elt->sges[j].length = 0;
1133 elt->sges[j].lkey = 0;
1138 /* Sanity checks, most of which are only relevant with
1139 * debugging enabled. */
1140 assert(WR_ID(wr->wr_id).id == elts_head);
1141 assert(WR_ID(wr->wr_id).offset == 0);
1142 assert(wr->next == NULL);
1143 assert(wr->sg_list == &elt->sges[0]);
1144 assert(wr->num_sge == 0);
1145 assert(wr->opcode == IBV_WR_SEND);
1146 /* When there are too many segments, extra segments are
1147 * linearized in the last SGE. */
1148 if (unlikely(segs > elemof(elt->sges))) {
1149 segs = (elemof(elt->sges) - 1);
1152 /* Set WR fields. */
1153 assert((rte_pktmbuf_mtod(buf, uintptr_t) -
1154 (uintptr_t)buf) <= 0xffff);
1155 WR_ID(wr->wr_id).offset =
1156 (rte_pktmbuf_mtod(buf, uintptr_t) -
1159 /* Register segments as SGEs. */
1160 for (j = 0; (j != segs); ++j) {
1161 struct ibv_sge *sge = &elt->sges[j];
1164 /* Retrieve Memory Region key for this memory pool. */
1165 lkey = txq_mp2mr(txq, buf->pool);
1166 if (unlikely(lkey == (uint32_t)-1)) {
1167 /* MR does not exist. */
1168 DEBUG("%p: unable to get MP <-> MR"
1169 " association", (void *)txq);
1170 /* Clean up TX element. */
1171 WR_ID(elt->wr.wr_id).offset = 0;
1185 /* Sanity checks, only relevant with debugging
1187 assert(sge->addr == 0);
1188 assert(sge->length == 0);
1189 assert(sge->lkey == 0);
1191 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
1193 rte_prefetch0((volatile void *)
1194 (uintptr_t)sge->addr);
1195 sge->length = DATA_LEN(buf);
1197 #ifdef MLX4_PMD_SOFT_COUNTERS
1198 sent_size += sge->length;
1202 /* If buf is not NULL here and is not going to be linearized,
1203 * nb_segs is not valid. */
1205 assert((buf == NULL) || (linearize));
1206 /* Linearize extra segments. */
1208 struct ibv_sge *sge = &elt->sges[segs];
1209 linear_t *linear = &(*txq->elts_linear)[elts_head];
1210 unsigned int size = linearize_mbuf(linear, buf);
1212 assert(segs == (elemof(elt->sges) - 1));
1214 /* Invalid packet. */
1215 DEBUG("%p: packet too large to be linearized.",
1217 /* Clean up TX element. */
1218 WR_ID(elt->wr.wr_id).offset = 0;
1232 /* If MLX4_PMD_SGE_WR_N is 1, free mbuf immediately
1233 * and clear offset from WR ID. */
1234 if (elemof(elt->sges) == 1) {
1236 struct rte_mbuf *next = NEXT(buf);
1238 rte_pktmbuf_free_seg(buf);
1240 } while (buf != NULL);
1241 WR_ID(wr->wr_id).offset = 0;
1243 /* Set WR fields and fill SGE with linear buffer. */
1245 /* Sanity checks, only relevant with debugging
1247 assert(sge->addr == 0);
1248 assert(sge->length == 0);
1249 assert(sge->lkey == 0);
1251 sge->addr = (uintptr_t)&(*linear)[0];
1253 sge->lkey = txq->mr_linear->lkey;
1254 #ifdef MLX4_PMD_SOFT_COUNTERS
1258 /* Link WRs together for ibv_post_send(). */
1260 wr_next = &wr->next;
1261 assert(wr->send_flags == 0);
1262 /* Request TX completion. */
1263 if (unlikely(--elts_comp_cd == 0)) {
1264 elts_comp_cd = txq->elts_comp_cd_init;
1266 send_flags |= IBV_EXP_QP_BURST_SIGNALED;
1268 if (++elts_head >= elts_n)
1270 #ifdef MLX4_PMD_SOFT_COUNTERS
1271 /* Increment sent bytes counter. */
1272 txq->stats.obytes += sent_size;
1274 /* Put SG list into send queue and ask for completion event. */
1275 #if MLX4_PMD_MAX_INLINE > 0
1277 (elt->sges[0].length <= txq->max_inline))
1278 err = txq->if_qp->send_pending_inline
1280 (void *)(uintptr_t)elt->sges[0].addr,
1281 elt->sges[0].length,
1285 err = txq->if_qp->send_pending_sg_list
1294 /* Take a shortcut if nothing must be sent. */
1295 if (unlikely(i == 0))
1297 #ifdef MLX4_PMD_SOFT_COUNTERS
1298 /* Increment sent packets counter. */
1299 txq->stats.opackets += i;
1302 /* Ring QP doorbell. */
1303 err = txq->if_qp->send_flush(txq->qp);
1304 if (unlikely(err)) {
1305 /* A nonzero value is not supposed to be returned.
1306 * Nothing can be done about it. */
1307 DEBUG("%p: send_flush() failed with error %d",
1310 txq->elts_head = elts_head;
1311 txq->elts_comp += elts_comp;
1312 txq->elts_comp_cd = elts_comp_cd;
1317 * Configure a TX queue.
1320 * Pointer to Ethernet device structure.
1322 * Pointer to TX queue structure.
1324 * Number of descriptors to configure in queue.
1326 * NUMA socket on which memory must be allocated.
1328 * Thresholds parameters.
1331 * 0 on success, errno value on failure.
1334 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
1335 unsigned int socket, const struct rte_eth_txconf *conf)
1337 struct priv *priv = dev->data->dev_private;
1343 struct ibv_exp_query_intf_params params;
1344 struct ibv_qp_init_attr init;
1345 struct ibv_exp_qp_attr mod;
1347 enum ibv_exp_query_intf_status status;
1350 (void)conf; /* Thresholds configuration (ignored). */
1351 if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
1352 ERROR("%p: invalid number of TX descriptors (must be a"
1353 " multiple of %d)", (void *)dev, MLX4_PMD_SGE_WR_N);
1356 desc /= MLX4_PMD_SGE_WR_N;
1357 /* MRs will be registered in mp2mr[] later. */
1358 tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
1359 if (tmpl.cq == NULL) {
1361 ERROR("%p: CQ creation failure: %s",
1362 (void *)dev, strerror(ret));
1365 DEBUG("priv->device_attr.max_qp_wr is %d",
1366 priv->device_attr.max_qp_wr);
1367 DEBUG("priv->device_attr.max_sge is %d",
1368 priv->device_attr.max_sge);
1369 attr.init = (struct ibv_qp_init_attr){
1370 /* CQ to be associated with the send queue. */
1372 /* CQ to be associated with the receive queue. */
1375 /* Max number of outstanding WRs. */
1376 .max_send_wr = ((priv->device_attr.max_qp_wr < desc) ?
1377 priv->device_attr.max_qp_wr :
1379 /* Max number of scatter/gather elements in a WR. */
1380 .max_send_sge = ((priv->device_attr.max_sge <
1381 MLX4_PMD_SGE_WR_N) ?
1382 priv->device_attr.max_sge :
1384 #if MLX4_PMD_MAX_INLINE > 0
1385 .max_inline_data = MLX4_PMD_MAX_INLINE,
1388 .qp_type = IBV_QPT_RAW_PACKET,
1389 /* Do *NOT* enable this, completions events are managed per
1393 tmpl.qp = ibv_create_qp(priv->pd, &attr.init);
1394 if (tmpl.qp == NULL) {
1395 ret = (errno ? errno : EINVAL);
1396 ERROR("%p: QP creation failure: %s",
1397 (void *)dev, strerror(ret));
1400 #if MLX4_PMD_MAX_INLINE > 0
1401 /* ibv_create_qp() updates this value. */
1402 tmpl.max_inline = attr.init.cap.max_inline_data;
1404 attr.mod = (struct ibv_exp_qp_attr){
1405 /* Move the QP to this state. */
1406 .qp_state = IBV_QPS_INIT,
1407 /* Primary port number. */
1408 .port_num = priv->port
1410 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod,
1411 (IBV_EXP_QP_STATE | IBV_EXP_QP_PORT));
1413 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
1414 (void *)dev, strerror(ret));
1417 ret = txq_alloc_elts(&tmpl, desc);
1419 ERROR("%p: TXQ allocation failed: %s",
1420 (void *)dev, strerror(ret));
1423 attr.mod = (struct ibv_exp_qp_attr){
1424 .qp_state = IBV_QPS_RTR
1426 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1428 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
1429 (void *)dev, strerror(ret));
1432 attr.mod.qp_state = IBV_QPS_RTS;
1433 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1435 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
1436 (void *)dev, strerror(ret));
1439 attr.params = (struct ibv_exp_query_intf_params){
1440 .intf_scope = IBV_EXP_INTF_GLOBAL,
1441 .intf = IBV_EXP_INTF_CQ,
1444 tmpl.if_cq = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
1445 if (tmpl.if_cq == NULL) {
1446 ERROR("%p: CQ interface family query failed with status %d",
1447 (void *)dev, status);
1450 attr.params = (struct ibv_exp_query_intf_params){
1451 .intf_scope = IBV_EXP_INTF_GLOBAL,
1452 .intf = IBV_EXP_INTF_QP_BURST,
1455 tmpl.if_qp = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
1456 if (tmpl.if_qp == NULL) {
1457 ERROR("%p: QP interface family query failed with status %d",
1458 (void *)dev, status);
1461 /* Clean up txq in case we're reinitializing it. */
1462 DEBUG("%p: cleaning-up old txq just in case", (void *)txq);
1465 DEBUG("%p: txq updated with %p", (void *)txq, (void *)&tmpl);
1475 * DPDK callback to configure a TX queue.
1478 * Pointer to Ethernet device structure.
1482 * Number of descriptors to configure in queue.
1484 * NUMA socket on which memory must be allocated.
1486 * Thresholds parameters.
1489 * 0 on success, negative errno value on failure.
1492 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1493 unsigned int socket, const struct rte_eth_txconf *conf)
1495 struct priv *priv = dev->data->dev_private;
1496 struct txq *txq = (*priv->txqs)[idx];
1500 DEBUG("%p: configuring queue %u for %u descriptors",
1501 (void *)dev, idx, desc);
1502 if (idx >= priv->txqs_n) {
1503 ERROR("%p: queue index out of range (%u >= %u)",
1504 (void *)dev, idx, priv->txqs_n);
1509 DEBUG("%p: reusing already allocated queue index %u (%p)",
1510 (void *)dev, idx, (void *)txq);
1511 if (priv->started) {
1515 (*priv->txqs)[idx] = NULL;
1518 txq = rte_calloc_socket("TXQ", 1, sizeof(*txq), 0, socket);
1520 ERROR("%p: unable to allocate queue index %u",
1526 ret = txq_setup(dev, txq, desc, socket, conf);
1530 txq->stats.idx = idx;
1531 DEBUG("%p: adding TX queue %p to list",
1532 (void *)dev, (void *)txq);
1533 (*priv->txqs)[idx] = txq;
1534 /* Update send callback. */
1535 dev->tx_pkt_burst = mlx4_tx_burst;
1542 * DPDK callback to release a TX queue.
1545 * Generic TX queue pointer.
1548 mlx4_tx_queue_release(void *dpdk_txq)
1550 struct txq *txq = (struct txq *)dpdk_txq;
1558 for (i = 0; (i != priv->txqs_n); ++i)
1559 if ((*priv->txqs)[i] == txq) {
1560 DEBUG("%p: removing TX queue %p from list",
1561 (void *)priv->dev, (void *)txq);
1562 (*priv->txqs)[i] = NULL;
1570 /* RX queues handling. */
1573 * Allocate RX queue elements with scattered packets support.
1576 * Pointer to RX queue structure.
1578 * Number of elements to allocate.
1580 * If not NULL, fetch buffers from this array instead of allocating them
1581 * with rte_pktmbuf_alloc().
1584 * 0 on success, errno value on failure.
1587 rxq_alloc_elts_sp(struct rxq *rxq, unsigned int elts_n,
1588 struct rte_mbuf **pool)
1591 struct rxq_elt_sp (*elts)[elts_n] =
1592 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1597 ERROR("%p: can't allocate packets array", (void *)rxq);
1601 /* For each WR (packet). */
1602 for (i = 0; (i != elts_n); ++i) {
1604 struct rxq_elt_sp *elt = &(*elts)[i];
1605 struct ibv_recv_wr *wr = &elt->wr;
1606 struct ibv_sge (*sges)[(elemof(elt->sges))] = &elt->sges;
1608 /* These two arrays must have the same size. */
1609 assert(elemof(elt->sges) == elemof(elt->bufs));
1612 wr->next = &(*elts)[(i + 1)].wr;
1613 wr->sg_list = &(*sges)[0];
1614 wr->num_sge = elemof(*sges);
1615 /* For each SGE (segment). */
1616 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1617 struct ibv_sge *sge = &(*sges)[j];
1618 struct rte_mbuf *buf;
1622 assert(buf != NULL);
1623 rte_pktmbuf_reset(buf);
1625 buf = rte_pktmbuf_alloc(rxq->mp);
1627 assert(pool == NULL);
1628 ERROR("%p: empty mbuf pool", (void *)rxq);
1633 /* Headroom is reserved by rte_pktmbuf_alloc(). */
1634 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1635 /* Buffer is supposed to be empty. */
1636 assert(rte_pktmbuf_data_len(buf) == 0);
1637 assert(rte_pktmbuf_pkt_len(buf) == 0);
1638 /* sge->addr must be able to store a pointer. */
1639 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1641 /* The first SGE keeps its headroom. */
1642 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
1643 sge->length = (buf->buf_len -
1644 RTE_PKTMBUF_HEADROOM);
1646 /* Subsequent SGEs lose theirs. */
1647 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1648 SET_DATA_OFF(buf, 0);
1649 sge->addr = (uintptr_t)buf->buf_addr;
1650 sge->length = buf->buf_len;
1652 sge->lkey = rxq->mr->lkey;
1653 /* Redundant check for tailroom. */
1654 assert(sge->length == rte_pktmbuf_tailroom(buf));
1657 /* The last WR pointer must be NULL. */
1658 (*elts)[(i - 1)].wr.next = NULL;
1659 DEBUG("%p: allocated and configured %u WRs (%zu segments)",
1660 (void *)rxq, elts_n, (elts_n * elemof((*elts)[0].sges)));
1661 rxq->elts_n = elts_n;
1663 rxq->elts.sp = elts;
1668 assert(pool == NULL);
1669 for (i = 0; (i != elemof(*elts)); ++i) {
1671 struct rxq_elt_sp *elt = &(*elts)[i];
1673 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1674 struct rte_mbuf *buf = elt->bufs[j];
1677 rte_pktmbuf_free_seg(buf);
1682 DEBUG("%p: failed, freed everything", (void *)rxq);
1688 * Free RX queue elements with scattered packets support.
1691 * Pointer to RX queue structure.
1694 rxq_free_elts_sp(struct rxq *rxq)
1697 unsigned int elts_n = rxq->elts_n;
1698 struct rxq_elt_sp (*elts)[elts_n] = rxq->elts.sp;
1700 DEBUG("%p: freeing WRs", (void *)rxq);
1702 rxq->elts.sp = NULL;
1705 for (i = 0; (i != elemof(*elts)); ++i) {
1707 struct rxq_elt_sp *elt = &(*elts)[i];
1709 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1710 struct rte_mbuf *buf = elt->bufs[j];
1713 rte_pktmbuf_free_seg(buf);
1720 * Allocate RX queue elements.
1723 * Pointer to RX queue structure.
1725 * Number of elements to allocate.
1727 * If not NULL, fetch buffers from this array instead of allocating them
1728 * with rte_pktmbuf_alloc().
1731 * 0 on success, errno value on failure.
1734 rxq_alloc_elts(struct rxq *rxq, unsigned int elts_n, struct rte_mbuf **pool)
1737 struct rxq_elt (*elts)[elts_n] =
1738 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1743 ERROR("%p: can't allocate packets array", (void *)rxq);
1747 /* For each WR (packet). */
1748 for (i = 0; (i != elts_n); ++i) {
1749 struct rxq_elt *elt = &(*elts)[i];
1750 struct ibv_recv_wr *wr = &elt->wr;
1751 struct ibv_sge *sge = &(*elts)[i].sge;
1752 struct rte_mbuf *buf;
1756 assert(buf != NULL);
1757 rte_pktmbuf_reset(buf);
1759 buf = rte_pktmbuf_alloc(rxq->mp);
1761 assert(pool == NULL);
1762 ERROR("%p: empty mbuf pool", (void *)rxq);
1766 /* Configure WR. Work request ID contains its own index in
1767 * the elts array and the offset between SGE buffer header and
1769 WR_ID(wr->wr_id).id = i;
1770 WR_ID(wr->wr_id).offset =
1771 (((uintptr_t)buf->buf_addr + RTE_PKTMBUF_HEADROOM) -
1773 wr->next = &(*elts)[(i + 1)].wr;
1776 /* Headroom is reserved by rte_pktmbuf_alloc(). */
1777 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1778 /* Buffer is supposed to be empty. */
1779 assert(rte_pktmbuf_data_len(buf) == 0);
1780 assert(rte_pktmbuf_pkt_len(buf) == 0);
1781 /* sge->addr must be able to store a pointer. */
1782 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1783 /* SGE keeps its headroom. */
1784 sge->addr = (uintptr_t)
1785 ((uint8_t *)buf->buf_addr + RTE_PKTMBUF_HEADROOM);
1786 sge->length = (buf->buf_len - RTE_PKTMBUF_HEADROOM);
1787 sge->lkey = rxq->mr->lkey;
1788 /* Redundant check for tailroom. */
1789 assert(sge->length == rte_pktmbuf_tailroom(buf));
1790 /* Make sure elts index and SGE mbuf pointer can be deduced
1792 if ((WR_ID(wr->wr_id).id != i) ||
1793 ((void *)((uintptr_t)sge->addr -
1794 WR_ID(wr->wr_id).offset) != buf)) {
1795 ERROR("%p: cannot store index and offset in WR ID",
1798 rte_pktmbuf_free(buf);
1803 /* The last WR pointer must be NULL. */
1804 (*elts)[(i - 1)].wr.next = NULL;
1805 DEBUG("%p: allocated and configured %u single-segment WRs",
1806 (void *)rxq, elts_n);
1807 rxq->elts_n = elts_n;
1809 rxq->elts.no_sp = elts;
1814 assert(pool == NULL);
1815 for (i = 0; (i != elemof(*elts)); ++i) {
1816 struct rxq_elt *elt = &(*elts)[i];
1817 struct rte_mbuf *buf;
1819 if (elt->sge.addr == 0)
1821 assert(WR_ID(elt->wr.wr_id).id == i);
1822 buf = (void *)((uintptr_t)elt->sge.addr -
1823 WR_ID(elt->wr.wr_id).offset);
1824 rte_pktmbuf_free_seg(buf);
1828 DEBUG("%p: failed, freed everything", (void *)rxq);
1834 * Free RX queue elements.
1837 * Pointer to RX queue structure.
1840 rxq_free_elts(struct rxq *rxq)
1843 unsigned int elts_n = rxq->elts_n;
1844 struct rxq_elt (*elts)[elts_n] = rxq->elts.no_sp;
1846 DEBUG("%p: freeing WRs", (void *)rxq);
1848 rxq->elts.no_sp = NULL;
1851 for (i = 0; (i != elemof(*elts)); ++i) {
1852 struct rxq_elt *elt = &(*elts)[i];
1853 struct rte_mbuf *buf;
1855 if (elt->sge.addr == 0)
1857 assert(WR_ID(elt->wr.wr_id).id == i);
1858 buf = (void *)((uintptr_t)elt->sge.addr -
1859 WR_ID(elt->wr.wr_id).offset);
1860 rte_pktmbuf_free_seg(buf);
1866 * Delete flow steering rule.
1869 * Pointer to RX queue structure.
1871 * MAC address index.
1876 rxq_del_flow(struct rxq *rxq, unsigned int mac_index, unsigned int vlan_index)
1879 struct priv *priv = rxq->priv;
1880 const uint8_t (*mac)[ETHER_ADDR_LEN] =
1881 (const uint8_t (*)[ETHER_ADDR_LEN])
1882 priv->mac[mac_index].addr_bytes;
1884 assert(rxq->mac_flow[mac_index][vlan_index] != NULL);
1885 DEBUG("%p: removing MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
1886 " (VLAN ID %" PRIu16 ")",
1888 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
1889 mac_index, priv->vlan_filter[vlan_index].id);
1890 claim_zero(ibv_destroy_flow(rxq->mac_flow[mac_index][vlan_index]));
1891 rxq->mac_flow[mac_index][vlan_index] = NULL;
1895 * Unregister a MAC address from a RX queue.
1898 * Pointer to RX queue structure.
1900 * MAC address index.
1903 rxq_mac_addr_del(struct rxq *rxq, unsigned int mac_index)
1905 struct priv *priv = rxq->priv;
1907 unsigned int vlans = 0;
1909 assert(mac_index < elemof(priv->mac));
1910 if (!BITFIELD_ISSET(rxq->mac_configured, mac_index))
1912 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
1913 if (!priv->vlan_filter[i].enabled)
1915 rxq_del_flow(rxq, mac_index, i);
1919 rxq_del_flow(rxq, mac_index, 0);
1921 BITFIELD_RESET(rxq->mac_configured, mac_index);
1925 * Unregister all MAC addresses from a RX queue.
1928 * Pointer to RX queue structure.
1931 rxq_mac_addrs_del(struct rxq *rxq)
1933 struct priv *priv = rxq->priv;
1936 for (i = 0; (i != elemof(priv->mac)); ++i)
1937 rxq_mac_addr_del(rxq, i);
1940 static int rxq_promiscuous_enable(struct rxq *);
1941 static void rxq_promiscuous_disable(struct rxq *);
1944 * Add single flow steering rule.
1947 * Pointer to RX queue structure.
1949 * MAC address index to register.
1951 * VLAN index. Use -1 for a flow without VLAN.
1954 * 0 on success, errno value on failure.
1957 rxq_add_flow(struct rxq *rxq, unsigned int mac_index, unsigned int vlan_index)
1959 struct ibv_flow *flow;
1960 struct priv *priv = rxq->priv;
1961 const uint8_t (*mac)[ETHER_ADDR_LEN] =
1962 (const uint8_t (*)[ETHER_ADDR_LEN])
1963 priv->mac[mac_index].addr_bytes;
1965 /* Allocate flow specification on the stack. */
1966 struct __attribute__((packed)) {
1967 struct ibv_flow_attr attr;
1968 struct ibv_flow_spec_eth spec;
1970 struct ibv_flow_attr *attr = &data.attr;
1971 struct ibv_flow_spec_eth *spec = &data.spec;
1973 assert(mac_index < elemof(priv->mac));
1974 assert((vlan_index < elemof(priv->vlan_filter)) || (vlan_index == -1u));
1976 * No padding must be inserted by the compiler between attr and spec.
1977 * This layout is expected by libibverbs.
1979 assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec);
1980 *attr = (struct ibv_flow_attr){
1981 .type = IBV_FLOW_ATTR_NORMAL,
1986 *spec = (struct ibv_flow_spec_eth){
1987 .type = IBV_FLOW_SPEC_ETH,
1988 .size = sizeof(*spec),
1991 (*mac)[0], (*mac)[1], (*mac)[2],
1992 (*mac)[3], (*mac)[4], (*mac)[5]
1994 .vlan_tag = ((vlan_index != -1u) ?
1995 htons(priv->vlan_filter[vlan_index].id) :
1999 .dst_mac = "\xff\xff\xff\xff\xff\xff",
2000 .vlan_tag = ((vlan_index != -1u) ? htons(0xfff) : 0),
2003 DEBUG("%p: adding MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
2004 " (VLAN %s %" PRIu16 ")",
2006 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
2008 ((vlan_index != -1u) ? "ID" : "index"),
2009 ((vlan_index != -1u) ? priv->vlan_filter[vlan_index].id : -1u));
2010 /* Create related flow. */
2012 flow = ibv_create_flow(rxq->qp, attr);
2014 /* It's not clear whether errno is always set in this case. */
2015 ERROR("%p: flow configuration failed, errno=%d: %s",
2017 (errno ? strerror(errno) : "Unknown error"));
2022 if (vlan_index == -1u)
2024 assert(rxq->mac_flow[mac_index][vlan_index] == NULL);
2025 rxq->mac_flow[mac_index][vlan_index] = flow;
2030 * Register a MAC address in a RX queue.
2033 * Pointer to RX queue structure.
2035 * MAC address index to register.
2038 * 0 on success, errno value on failure.
2041 rxq_mac_addr_add(struct rxq *rxq, unsigned int mac_index)
2043 struct priv *priv = rxq->priv;
2045 unsigned int vlans = 0;
2048 assert(mac_index < elemof(priv->mac));
2049 if (BITFIELD_ISSET(rxq->mac_configured, mac_index))
2050 rxq_mac_addr_del(rxq, mac_index);
2051 /* Fill VLAN specifications. */
2052 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
2053 if (!priv->vlan_filter[i].enabled)
2055 /* Create related flow. */
2056 ret = rxq_add_flow(rxq, mac_index, i);
2061 /* Failure, rollback. */
2063 if (priv->vlan_filter[--i].enabled)
2064 rxq_del_flow(rxq, mac_index, i);
2068 /* In case there is no VLAN filter. */
2070 ret = rxq_add_flow(rxq, mac_index, -1);
2074 BITFIELD_SET(rxq->mac_configured, mac_index);
2079 * Register all MAC addresses in a RX queue.
2082 * Pointer to RX queue structure.
2085 * 0 on success, errno value on failure.
2088 rxq_mac_addrs_add(struct rxq *rxq)
2090 struct priv *priv = rxq->priv;
2094 for (i = 0; (i != elemof(priv->mac)); ++i) {
2095 if (!BITFIELD_ISSET(priv->mac_configured, i))
2097 ret = rxq_mac_addr_add(rxq, i);
2100 /* Failure, rollback. */
2102 rxq_mac_addr_del(rxq, --i);
2110 * Unregister a MAC address.
2112 * In RSS mode, the MAC address is unregistered from the parent queue,
2113 * otherwise it is unregistered from each queue directly.
2116 * Pointer to private structure.
2118 * MAC address index.
2121 priv_mac_addr_del(struct priv *priv, unsigned int mac_index)
2125 assert(mac_index < elemof(priv->mac));
2126 if (!BITFIELD_ISSET(priv->mac_configured, mac_index))
2129 rxq_mac_addr_del(&priv->rxq_parent, mac_index);
2132 for (i = 0; (i != priv->dev->data->nb_rx_queues); ++i)
2133 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2135 BITFIELD_RESET(priv->mac_configured, mac_index);
2139 * Register a MAC address.
2141 * In RSS mode, the MAC address is registered in the parent queue,
2142 * otherwise it is registered in each queue directly.
2145 * Pointer to private structure.
2147 * MAC address index to use.
2149 * MAC address to register.
2152 * 0 on success, errno value on failure.
2155 priv_mac_addr_add(struct priv *priv, unsigned int mac_index,
2156 const uint8_t (*mac)[ETHER_ADDR_LEN])
2161 assert(mac_index < elemof(priv->mac));
2162 /* First, make sure this address isn't already configured. */
2163 for (i = 0; (i != elemof(priv->mac)); ++i) {
2164 /* Skip this index, it's going to be reconfigured. */
2167 if (!BITFIELD_ISSET(priv->mac_configured, i))
2169 if (memcmp(priv->mac[i].addr_bytes, *mac, sizeof(*mac)))
2171 /* Address already configured elsewhere, return with error. */
2174 if (BITFIELD_ISSET(priv->mac_configured, mac_index))
2175 priv_mac_addr_del(priv, mac_index);
2176 priv->mac[mac_index] = (struct ether_addr){
2178 (*mac)[0], (*mac)[1], (*mac)[2],
2179 (*mac)[3], (*mac)[4], (*mac)[5]
2182 /* If device isn't started, this is all we need to do. */
2183 if (!priv->started) {
2185 /* Verify that all queues have this index disabled. */
2186 for (i = 0; (i != priv->rxqs_n); ++i) {
2187 if ((*priv->rxqs)[i] == NULL)
2189 assert(!BITFIELD_ISSET
2190 ((*priv->rxqs)[i]->mac_configured, mac_index));
2196 ret = rxq_mac_addr_add(&priv->rxq_parent, mac_index);
2201 for (i = 0; (i != priv->rxqs_n); ++i) {
2202 if ((*priv->rxqs)[i] == NULL)
2204 ret = rxq_mac_addr_add((*priv->rxqs)[i], mac_index);
2207 /* Failure, rollback. */
2209 if ((*priv->rxqs)[(--i)] != NULL)
2210 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2214 BITFIELD_SET(priv->mac_configured, mac_index);
2219 * Enable allmulti mode in a RX queue.
2222 * Pointer to RX queue structure.
2225 * 0 on success, errno value on failure.
2228 rxq_allmulticast_enable(struct rxq *rxq)
2230 struct ibv_flow *flow;
2231 struct ibv_flow_attr attr = {
2232 .type = IBV_FLOW_ATTR_MC_DEFAULT,
2234 .port = rxq->priv->port,
2238 DEBUG("%p: enabling allmulticast mode", (void *)rxq);
2239 if (rxq->allmulti_flow != NULL)
2242 flow = ibv_create_flow(rxq->qp, &attr);
2244 /* It's not clear whether errno is always set in this case. */
2245 ERROR("%p: flow configuration failed, errno=%d: %s",
2247 (errno ? strerror(errno) : "Unknown error"));
2252 rxq->allmulti_flow = flow;
2253 DEBUG("%p: allmulticast mode enabled", (void *)rxq);
2258 * Disable allmulti mode in a RX queue.
2261 * Pointer to RX queue structure.
2264 rxq_allmulticast_disable(struct rxq *rxq)
2266 DEBUG("%p: disabling allmulticast mode", (void *)rxq);
2267 if (rxq->allmulti_flow == NULL)
2269 claim_zero(ibv_destroy_flow(rxq->allmulti_flow));
2270 rxq->allmulti_flow = NULL;
2271 DEBUG("%p: allmulticast mode disabled", (void *)rxq);
2275 * Enable promiscuous mode in a RX queue.
2278 * Pointer to RX queue structure.
2281 * 0 on success, errno value on failure.
2284 rxq_promiscuous_enable(struct rxq *rxq)
2286 struct ibv_flow *flow;
2287 struct ibv_flow_attr attr = {
2288 .type = IBV_FLOW_ATTR_ALL_DEFAULT,
2290 .port = rxq->priv->port,
2296 DEBUG("%p: enabling promiscuous mode", (void *)rxq);
2297 if (rxq->promisc_flow != NULL)
2300 flow = ibv_create_flow(rxq->qp, &attr);
2302 /* It's not clear whether errno is always set in this case. */
2303 ERROR("%p: flow configuration failed, errno=%d: %s",
2305 (errno ? strerror(errno) : "Unknown error"));
2310 rxq->promisc_flow = flow;
2311 DEBUG("%p: promiscuous mode enabled", (void *)rxq);
2316 * Disable promiscuous mode in a RX queue.
2319 * Pointer to RX queue structure.
2322 rxq_promiscuous_disable(struct rxq *rxq)
2326 DEBUG("%p: disabling promiscuous mode", (void *)rxq);
2327 if (rxq->promisc_flow == NULL)
2329 claim_zero(ibv_destroy_flow(rxq->promisc_flow));
2330 rxq->promisc_flow = NULL;
2331 DEBUG("%p: promiscuous mode disabled", (void *)rxq);
2335 * Clean up a RX queue.
2337 * Destroy objects, free allocated memory and reset the structure for reuse.
2340 * Pointer to RX queue structure.
2343 rxq_cleanup(struct rxq *rxq)
2345 struct ibv_exp_release_intf_params params;
2347 DEBUG("cleaning up %p", (void *)rxq);
2349 rxq_free_elts_sp(rxq);
2352 if (rxq->if_qp != NULL) {
2353 assert(rxq->priv != NULL);
2354 assert(rxq->priv->ctx != NULL);
2355 assert(rxq->qp != NULL);
2356 params = (struct ibv_exp_release_intf_params){
2359 claim_zero(ibv_exp_release_intf(rxq->priv->ctx,
2363 if (rxq->if_cq != NULL) {
2364 assert(rxq->priv != NULL);
2365 assert(rxq->priv->ctx != NULL);
2366 assert(rxq->cq != NULL);
2367 params = (struct ibv_exp_release_intf_params){
2370 claim_zero(ibv_exp_release_intf(rxq->priv->ctx,
2374 if (rxq->qp != NULL) {
2375 rxq_promiscuous_disable(rxq);
2376 rxq_allmulticast_disable(rxq);
2377 rxq_mac_addrs_del(rxq);
2378 claim_zero(ibv_destroy_qp(rxq->qp));
2380 if (rxq->cq != NULL)
2381 claim_zero(ibv_destroy_cq(rxq->cq));
2382 if (rxq->mr != NULL)
2383 claim_zero(ibv_dereg_mr(rxq->mr));
2384 memset(rxq, 0, sizeof(*rxq));
2388 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n);
2391 * DPDK callback for RX with scattered packets support.
2394 * Generic pointer to RX queue structure.
2396 * Array to store received packets.
2398 * Maximum number of packets in array.
2401 * Number of packets successfully received (<= pkts_n).
2404 mlx4_rx_burst_sp(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2406 struct rxq *rxq = (struct rxq *)dpdk_rxq;
2407 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
2408 const unsigned int elts_n = rxq->elts_n;
2409 unsigned int elts_head = rxq->elts_head;
2410 struct ibv_recv_wr head;
2411 struct ibv_recv_wr **next = &head.next;
2412 struct ibv_recv_wr *bad_wr;
2414 unsigned int pkts_ret = 0;
2417 if (unlikely(!rxq->sp))
2418 return mlx4_rx_burst(dpdk_rxq, pkts, pkts_n);
2419 if (unlikely(elts == NULL)) /* See RTE_DEV_CMD_SET_MTU. */
2421 for (i = 0; (i != pkts_n); ++i) {
2422 struct rxq_elt_sp *elt = &(*elts)[elts_head];
2423 struct ibv_recv_wr *wr = &elt->wr;
2424 uint64_t wr_id = wr->wr_id;
2426 unsigned int pkt_buf_len;
2427 struct rte_mbuf *pkt_buf = NULL; /* Buffer returned in pkts. */
2428 struct rte_mbuf **pkt_buf_next = &pkt_buf;
2429 unsigned int seg_headroom = RTE_PKTMBUF_HEADROOM;
2432 /* Sanity checks. */
2436 assert(wr_id < rxq->elts_n);
2437 assert(wr->sg_list == elt->sges);
2438 assert(wr->num_sge == elemof(elt->sges));
2439 assert(elts_head < rxq->elts_n);
2440 assert(rxq->elts_head < rxq->elts_n);
2441 ret = rxq->if_cq->poll_length(rxq->cq, NULL, NULL);
2442 if (unlikely(ret < 0)) {
2446 DEBUG("rxq=%p, poll_length() failed (ret=%d)",
2448 /* ibv_poll_cq() must be used in case of failure. */
2449 wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
2450 if (unlikely(wcs_n == 0))
2452 if (unlikely(wcs_n < 0)) {
2453 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
2454 (void *)rxq, wcs_n);
2458 if (unlikely(wc.status != IBV_WC_SUCCESS)) {
2459 /* Whatever, just repost the offending WR. */
2460 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
2461 " completion status (%d): %s",
2462 (void *)rxq, wc.wr_id, wc.status,
2463 ibv_wc_status_str(wc.status));
2464 #ifdef MLX4_PMD_SOFT_COUNTERS
2465 /* Increment dropped packets counter. */
2466 ++rxq->stats.idropped;
2468 /* Link completed WRs together for repost. */
2479 /* Link completed WRs together for repost. */
2483 * Replace spent segments with new ones, concatenate and
2484 * return them as pkt_buf.
2487 struct ibv_sge *sge = &elt->sges[j];
2488 struct rte_mbuf *seg = elt->bufs[j];
2489 struct rte_mbuf *rep;
2490 unsigned int seg_tailroom;
2493 * Fetch initial bytes of packet descriptor into a
2494 * cacheline while allocating rep.
2497 rep = __rte_mbuf_raw_alloc(rxq->mp);
2498 if (unlikely(rep == NULL)) {
2500 * Unable to allocate a replacement mbuf,
2503 DEBUG("rxq=%p, wr_id=%" PRIu64 ":"
2504 " can't allocate a new mbuf",
2505 (void *)rxq, wr_id);
2506 if (pkt_buf != NULL) {
2507 *pkt_buf_next = NULL;
2508 rte_pktmbuf_free(pkt_buf);
2510 /* Increase out of memory counters. */
2511 ++rxq->stats.rx_nombuf;
2512 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
2516 /* Poison user-modifiable fields in rep. */
2517 NEXT(rep) = (void *)((uintptr_t)-1);
2518 SET_DATA_OFF(rep, 0xdead);
2519 DATA_LEN(rep) = 0xd00d;
2520 PKT_LEN(rep) = 0xdeadd00d;
2521 NB_SEGS(rep) = 0x2a;
2525 assert(rep->buf_len == seg->buf_len);
2526 assert(rep->buf_len == rxq->mb_len);
2527 /* Reconfigure sge to use rep instead of seg. */
2528 assert(sge->lkey == rxq->mr->lkey);
2529 sge->addr = ((uintptr_t)rep->buf_addr + seg_headroom);
2532 /* Update pkt_buf if it's the first segment, or link
2533 * seg to the previous one and update pkt_buf_next. */
2534 *pkt_buf_next = seg;
2535 pkt_buf_next = &NEXT(seg);
2536 /* Update seg information. */
2537 seg_tailroom = (seg->buf_len - seg_headroom);
2538 assert(sge->length == seg_tailroom);
2539 SET_DATA_OFF(seg, seg_headroom);
2540 if (likely(len <= seg_tailroom)) {
2542 DATA_LEN(seg) = len;
2545 assert(rte_pktmbuf_headroom(seg) ==
2547 assert(rte_pktmbuf_tailroom(seg) ==
2548 (seg_tailroom - len));
2551 DATA_LEN(seg) = seg_tailroom;
2552 PKT_LEN(seg) = seg_tailroom;
2554 assert(rte_pktmbuf_headroom(seg) == seg_headroom);
2555 assert(rte_pktmbuf_tailroom(seg) == 0);
2556 /* Fix len and clear headroom for next segments. */
2557 len -= seg_tailroom;
2560 /* Update head and tail segments. */
2561 *pkt_buf_next = NULL;
2562 assert(pkt_buf != NULL);
2564 NB_SEGS(pkt_buf) = j;
2565 PORT(pkt_buf) = rxq->port_id;
2566 PKT_LEN(pkt_buf) = pkt_buf_len;
2567 pkt_buf->ol_flags = 0;
2569 /* Return packet. */
2570 *(pkts++) = pkt_buf;
2572 #ifdef MLX4_PMD_SOFT_COUNTERS
2573 /* Increase bytes counter. */
2574 rxq->stats.ibytes += pkt_buf_len;
2577 if (++elts_head >= elts_n)
2581 if (unlikely(i == 0))
2586 DEBUG("%p: reposting %d WRs", (void *)rxq, i);
2588 ret = ibv_post_recv(rxq->qp, head.next, &bad_wr);
2589 if (unlikely(ret)) {
2590 /* Inability to repost WRs is fatal. */
2591 DEBUG("%p: ibv_post_recv(): failed for WR %p: %s",
2597 rxq->elts_head = elts_head;
2598 #ifdef MLX4_PMD_SOFT_COUNTERS
2599 /* Increase packets counter. */
2600 rxq->stats.ipackets += pkts_ret;
2606 * DPDK callback for RX.
2608 * The following function is the same as mlx4_rx_burst_sp(), except it doesn't
2609 * manage scattered packets. Improves performance when MRU is lower than the
2610 * size of the first segment.
2613 * Generic pointer to RX queue structure.
2615 * Array to store received packets.
2617 * Maximum number of packets in array.
2620 * Number of packets successfully received (<= pkts_n).
2623 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2625 struct rxq *rxq = (struct rxq *)dpdk_rxq;
2626 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
2627 const unsigned int elts_n = rxq->elts_n;
2628 unsigned int elts_head = rxq->elts_head;
2629 struct ibv_sge sges[pkts_n];
2631 unsigned int pkts_ret = 0;
2634 if (unlikely(rxq->sp))
2635 return mlx4_rx_burst_sp(dpdk_rxq, pkts, pkts_n);
2636 for (i = 0; (i != pkts_n); ++i) {
2637 struct rxq_elt *elt = &(*elts)[elts_head];
2638 struct ibv_recv_wr *wr = &elt->wr;
2639 uint64_t wr_id = wr->wr_id;
2641 struct rte_mbuf *seg = (void *)((uintptr_t)elt->sge.addr -
2642 WR_ID(wr_id).offset);
2643 struct rte_mbuf *rep;
2645 /* Sanity checks. */
2646 assert(WR_ID(wr_id).id < rxq->elts_n);
2647 assert(wr->sg_list == &elt->sge);
2648 assert(wr->num_sge == 1);
2649 assert(elts_head < rxq->elts_n);
2650 assert(rxq->elts_head < rxq->elts_n);
2651 ret = rxq->if_cq->poll_length(rxq->cq, NULL, NULL);
2652 if (unlikely(ret < 0)) {
2656 DEBUG("rxq=%p, poll_length() failed (ret=%d)",
2658 /* ibv_poll_cq() must be used in case of failure. */
2659 wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
2660 if (unlikely(wcs_n == 0))
2662 if (unlikely(wcs_n < 0)) {
2663 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
2664 (void *)rxq, wcs_n);
2668 if (unlikely(wc.status != IBV_WC_SUCCESS)) {
2669 /* Whatever, just repost the offending WR. */
2670 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
2671 " completion status (%d): %s",
2672 (void *)rxq, wc.wr_id, wc.status,
2673 ibv_wc_status_str(wc.status));
2674 #ifdef MLX4_PMD_SOFT_COUNTERS
2675 /* Increment dropped packets counter. */
2676 ++rxq->stats.idropped;
2678 /* Add SGE to array for repost. */
2688 * Fetch initial bytes of packet descriptor into a
2689 * cacheline while allocating rep.
2692 rep = __rte_mbuf_raw_alloc(rxq->mp);
2693 if (unlikely(rep == NULL)) {
2695 * Unable to allocate a replacement mbuf,
2698 DEBUG("rxq=%p, wr_id=%" PRIu32 ":"
2699 " can't allocate a new mbuf",
2700 (void *)rxq, WR_ID(wr_id).id);
2701 /* Increase out of memory counters. */
2702 ++rxq->stats.rx_nombuf;
2703 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
2707 /* Reconfigure sge to use rep instead of seg. */
2708 elt->sge.addr = (uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM;
2709 assert(elt->sge.lkey == rxq->mr->lkey);
2710 WR_ID(wr->wr_id).offset =
2711 (((uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM) -
2713 assert(WR_ID(wr->wr_id).id == WR_ID(wr_id).id);
2715 /* Add SGE to array for repost. */
2718 /* Update seg information. */
2719 SET_DATA_OFF(seg, RTE_PKTMBUF_HEADROOM);
2721 PORT(seg) = rxq->port_id;
2724 DATA_LEN(seg) = len;
2727 /* Return packet. */
2730 #ifdef MLX4_PMD_SOFT_COUNTERS
2731 /* Increase bytes counter. */
2732 rxq->stats.ibytes += len;
2735 if (++elts_head >= elts_n)
2739 if (unlikely(i == 0))
2743 DEBUG("%p: reposting %u WRs", (void *)rxq, i);
2745 ret = rxq->if_qp->recv_burst(rxq->qp, sges, i);
2746 if (unlikely(ret)) {
2747 /* Inability to repost WRs is fatal. */
2748 DEBUG("%p: recv_burst(): failed (ret=%d)",
2753 rxq->elts_head = elts_head;
2754 #ifdef MLX4_PMD_SOFT_COUNTERS
2755 /* Increase packets counter. */
2756 rxq->stats.ipackets += pkts_ret;
2762 * Allocate a Queue Pair.
2763 * Optionally setup inline receive if supported.
2766 * Pointer to private structure.
2768 * Completion queue to associate with QP.
2770 * Number of descriptors in QP (hint only).
2773 * QP pointer or NULL in case of error.
2775 static struct ibv_qp *
2776 rxq_setup_qp(struct priv *priv, struct ibv_cq *cq, uint16_t desc)
2778 struct ibv_exp_qp_init_attr attr = {
2779 /* CQ to be associated with the send queue. */
2781 /* CQ to be associated with the receive queue. */
2784 /* Max number of outstanding WRs. */
2785 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
2786 priv->device_attr.max_qp_wr :
2788 /* Max number of scatter/gather elements in a WR. */
2789 .max_recv_sge = ((priv->device_attr.max_sge <
2790 MLX4_PMD_SGE_WR_N) ?
2791 priv->device_attr.max_sge :
2794 .qp_type = IBV_QPT_RAW_PACKET,
2795 .comp_mask = IBV_EXP_QP_INIT_ATTR_PD,
2800 attr.max_inl_recv = priv->inl_recv_size;
2801 attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
2803 return ibv_exp_create_qp(priv->ctx, &attr);
2809 * Allocate a RSS Queue Pair.
2810 * Optionally setup inline receive if supported.
2813 * Pointer to private structure.
2815 * Completion queue to associate with QP.
2817 * Number of descriptors in QP (hint only).
2819 * If nonzero, create a parent QP, otherwise a child.
2822 * QP pointer or NULL in case of error.
2824 static struct ibv_qp *
2825 rxq_setup_qp_rss(struct priv *priv, struct ibv_cq *cq, uint16_t desc,
2828 struct ibv_exp_qp_init_attr attr = {
2829 /* CQ to be associated with the send queue. */
2831 /* CQ to be associated with the receive queue. */
2834 /* Max number of outstanding WRs. */
2835 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
2836 priv->device_attr.max_qp_wr :
2838 /* Max number of scatter/gather elements in a WR. */
2839 .max_recv_sge = ((priv->device_attr.max_sge <
2840 MLX4_PMD_SGE_WR_N) ?
2841 priv->device_attr.max_sge :
2844 .qp_type = IBV_QPT_RAW_PACKET,
2845 .comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
2846 IBV_EXP_QP_INIT_ATTR_QPG),
2851 attr.max_inl_recv = priv->inl_recv_size,
2852 attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
2855 attr.qpg.qpg_type = IBV_EXP_QPG_PARENT;
2856 /* TSS isn't necessary. */
2857 attr.qpg.parent_attrib.tss_child_count = 0;
2858 attr.qpg.parent_attrib.rss_child_count = priv->rxqs_n;
2859 DEBUG("initializing parent RSS queue");
2861 attr.qpg.qpg_type = IBV_EXP_QPG_CHILD_RX;
2862 attr.qpg.qpg_parent = priv->rxq_parent.qp;
2863 DEBUG("initializing child RSS queue");
2865 return ibv_exp_create_qp(priv->ctx, &attr);
2868 #endif /* RSS_SUPPORT */
2871 * Reconfigure a RX queue with new parameters.
2873 * rxq_rehash() does not allocate mbufs, which, if not done from the right
2874 * thread (such as a control thread), may corrupt the pool.
2875 * In case of failure, the queue is left untouched.
2878 * Pointer to Ethernet device structure.
2883 * 0 on success, errno value on failure.
2886 rxq_rehash(struct rte_eth_dev *dev, struct rxq *rxq)
2888 struct priv *priv = rxq->priv;
2889 struct rxq tmpl = *rxq;
2890 unsigned int mbuf_n;
2891 unsigned int desc_n;
2892 struct rte_mbuf **pool;
2894 struct ibv_exp_qp_attr mod;
2895 struct ibv_recv_wr *bad_wr;
2897 int parent = (rxq == &priv->rxq_parent);
2900 ERROR("%p: cannot rehash parent queue %p",
2901 (void *)dev, (void *)rxq);
2904 DEBUG("%p: rehashing queue %p", (void *)dev, (void *)rxq);
2905 /* Number of descriptors and mbufs currently allocated. */
2906 desc_n = (tmpl.elts_n * (tmpl.sp ? MLX4_PMD_SGE_WR_N : 1));
2908 /* Enable scattered packets support for this queue if necessary. */
2909 if ((dev->data->dev_conf.rxmode.jumbo_frame) &&
2910 (dev->data->dev_conf.rxmode.max_rx_pkt_len >
2911 (tmpl.mb_len - RTE_PKTMBUF_HEADROOM))) {
2913 desc_n /= MLX4_PMD_SGE_WR_N;
2916 DEBUG("%p: %s scattered packets support (%u WRs)",
2917 (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc_n);
2918 /* If scatter mode is the same as before, nothing to do. */
2919 if (tmpl.sp == rxq->sp) {
2920 DEBUG("%p: nothing to do", (void *)dev);
2923 /* Remove attached flows if RSS is disabled (no parent queue). */
2925 rxq_allmulticast_disable(&tmpl);
2926 rxq_promiscuous_disable(&tmpl);
2927 rxq_mac_addrs_del(&tmpl);
2928 /* Update original queue in case of failure. */
2929 rxq->allmulti_flow = tmpl.allmulti_flow;
2930 rxq->promisc_flow = tmpl.promisc_flow;
2931 memcpy(rxq->mac_configured, tmpl.mac_configured,
2932 sizeof(rxq->mac_configured));
2933 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
2935 /* From now on, any failure will render the queue unusable.
2936 * Reinitialize QP. */
2937 mod = (struct ibv_exp_qp_attr){ .qp_state = IBV_QPS_RESET };
2938 err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
2940 ERROR("%p: cannot reset QP: %s", (void *)dev, strerror(err));
2944 err = ibv_resize_cq(tmpl.cq, desc_n);
2946 ERROR("%p: cannot resize CQ: %s", (void *)dev, strerror(err));
2950 mod = (struct ibv_exp_qp_attr){
2951 /* Move the QP to this state. */
2952 .qp_state = IBV_QPS_INIT,
2953 /* Primary port number. */
2954 .port_num = priv->port
2956 err = ibv_exp_modify_qp(tmpl.qp, &mod,
2959 (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
2960 #endif /* RSS_SUPPORT */
2963 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
2964 (void *)dev, strerror(err));
2968 /* Reconfigure flows. Do not care for errors. */
2970 rxq_mac_addrs_add(&tmpl);
2972 rxq_promiscuous_enable(&tmpl);
2974 rxq_allmulticast_enable(&tmpl);
2975 /* Update original queue in case of failure. */
2976 rxq->allmulti_flow = tmpl.allmulti_flow;
2977 rxq->promisc_flow = tmpl.promisc_flow;
2978 memcpy(rxq->mac_configured, tmpl.mac_configured,
2979 sizeof(rxq->mac_configured));
2980 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
2982 /* Allocate pool. */
2983 pool = rte_malloc(__func__, (mbuf_n * sizeof(*pool)), 0);
2985 ERROR("%p: cannot allocate memory", (void *)dev);
2988 /* Snatch mbufs from original queue. */
2991 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
2993 for (i = 0; (i != elemof(*elts)); ++i) {
2994 struct rxq_elt_sp *elt = &(*elts)[i];
2997 for (j = 0; (j != elemof(elt->bufs)); ++j) {
2998 assert(elt->bufs[j] != NULL);
2999 pool[k++] = elt->bufs[j];
3003 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
3005 for (i = 0; (i != elemof(*elts)); ++i) {
3006 struct rxq_elt *elt = &(*elts)[i];
3007 struct rte_mbuf *buf = (void *)
3008 ((uintptr_t)elt->sge.addr -
3009 WR_ID(elt->wr.wr_id).offset);
3011 assert(WR_ID(elt->wr.wr_id).id == i);
3015 assert(k == mbuf_n);
3017 tmpl.elts.sp = NULL;
3018 assert((void *)&tmpl.elts.sp == (void *)&tmpl.elts.no_sp);
3020 rxq_alloc_elts_sp(&tmpl, desc_n, pool) :
3021 rxq_alloc_elts(&tmpl, desc_n, pool));
3023 ERROR("%p: cannot reallocate WRs, aborting", (void *)dev);
3028 assert(tmpl.elts_n == desc_n);
3029 assert(tmpl.elts.sp != NULL);
3031 /* Clean up original data. */
3033 rte_free(rxq->elts.sp);
3034 rxq->elts.sp = NULL;
3036 err = ibv_post_recv(tmpl.qp,
3038 &(*tmpl.elts.sp)[0].wr :
3039 &(*tmpl.elts.no_sp)[0].wr),
3042 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
3048 mod = (struct ibv_exp_qp_attr){
3049 .qp_state = IBV_QPS_RTR
3051 err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3053 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
3054 (void *)dev, strerror(err));
3062 * Configure a RX queue.
3065 * Pointer to Ethernet device structure.
3067 * Pointer to RX queue structure.
3069 * Number of descriptors to configure in queue.
3071 * NUMA socket on which memory must be allocated.
3073 * Thresholds parameters.
3075 * Memory pool for buffer allocations.
3078 * 0 on success, errno value on failure.
3081 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
3082 unsigned int socket, const struct rte_eth_rxconf *conf,
3083 struct rte_mempool *mp)
3085 struct priv *priv = dev->data->dev_private;
3091 struct ibv_exp_qp_attr mod;
3093 struct ibv_exp_query_intf_params params;
3095 enum ibv_exp_query_intf_status status;
3096 struct ibv_recv_wr *bad_wr;
3097 struct rte_mbuf *buf;
3099 int parent = (rxq == &priv->rxq_parent);
3101 (void)conf; /* Thresholds configuration (ignored). */
3103 * If this is a parent queue, hardware must support RSS and
3104 * RSS must be enabled.
3106 assert((!parent) || ((priv->hw_rss) && (priv->rss)));
3108 /* Even if unused, ibv_create_cq() requires at least one
3113 if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
3114 ERROR("%p: invalid number of RX descriptors (must be a"
3115 " multiple of %d)", (void *)dev, MLX4_PMD_SGE_WR_N);
3118 /* Get mbuf length. */
3119 buf = rte_pktmbuf_alloc(mp);
3121 ERROR("%p: unable to allocate mbuf", (void *)dev);
3124 tmpl.mb_len = buf->buf_len;
3125 assert((rte_pktmbuf_headroom(buf) +
3126 rte_pktmbuf_tailroom(buf)) == tmpl.mb_len);
3127 assert(rte_pktmbuf_headroom(buf) == RTE_PKTMBUF_HEADROOM);
3128 rte_pktmbuf_free(buf);
3129 /* Enable scattered packets support for this queue if necessary. */
3130 if ((dev->data->dev_conf.rxmode.jumbo_frame) &&
3131 (dev->data->dev_conf.rxmode.max_rx_pkt_len >
3132 (tmpl.mb_len - RTE_PKTMBUF_HEADROOM))) {
3134 desc /= MLX4_PMD_SGE_WR_N;
3136 DEBUG("%p: %s scattered packets support (%u WRs)",
3137 (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc);
3138 /* Use the entire RX mempool as the memory region. */
3139 tmpl.mr = ibv_reg_mr(priv->pd,
3140 (void *)mp->elt_va_start,
3141 (mp->elt_va_end - mp->elt_va_start),
3142 (IBV_ACCESS_LOCAL_WRITE |
3143 IBV_ACCESS_REMOTE_WRITE));
3144 if (tmpl.mr == NULL) {
3146 ERROR("%p: MR creation failure: %s",
3147 (void *)dev, strerror(ret));
3151 tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
3152 if (tmpl.cq == NULL) {
3154 ERROR("%p: CQ creation failure: %s",
3155 (void *)dev, strerror(ret));
3158 DEBUG("priv->device_attr.max_qp_wr is %d",
3159 priv->device_attr.max_qp_wr);
3160 DEBUG("priv->device_attr.max_sge is %d",
3161 priv->device_attr.max_sge);
3164 tmpl.qp = rxq_setup_qp_rss(priv, tmpl.cq, desc, parent);
3166 #endif /* RSS_SUPPORT */
3167 tmpl.qp = rxq_setup_qp(priv, tmpl.cq, desc);
3168 if (tmpl.qp == NULL) {
3169 ret = (errno ? errno : EINVAL);
3170 ERROR("%p: QP creation failure: %s",
3171 (void *)dev, strerror(ret));
3174 mod = (struct ibv_exp_qp_attr){
3175 /* Move the QP to this state. */
3176 .qp_state = IBV_QPS_INIT,
3177 /* Primary port number. */
3178 .port_num = priv->port
3180 ret = ibv_exp_modify_qp(tmpl.qp, &mod,
3183 (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
3184 #endif /* RSS_SUPPORT */
3187 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
3188 (void *)dev, strerror(ret));
3191 if ((parent) || (!priv->rss)) {
3192 /* Configure MAC and broadcast addresses. */
3193 ret = rxq_mac_addrs_add(&tmpl);
3195 ERROR("%p: QP flow attachment failed: %s",
3196 (void *)dev, strerror(ret));
3200 /* Allocate descriptors for RX queues, except for the RSS parent. */
3204 ret = rxq_alloc_elts_sp(&tmpl, desc, NULL);
3206 ret = rxq_alloc_elts(&tmpl, desc, NULL);
3208 ERROR("%p: RXQ allocation failed: %s",
3209 (void *)dev, strerror(ret));
3212 ret = ibv_post_recv(tmpl.qp,
3214 &(*tmpl.elts.sp)[0].wr :
3215 &(*tmpl.elts.no_sp)[0].wr),
3218 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
3225 mod = (struct ibv_exp_qp_attr){
3226 .qp_state = IBV_QPS_RTR
3228 ret = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3230 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
3231 (void *)dev, strerror(ret));
3235 tmpl.port_id = dev->data->port_id;
3236 DEBUG("%p: RTE port ID: %u", (void *)rxq, tmpl.port_id);
3237 attr.params = (struct ibv_exp_query_intf_params){
3238 .intf_scope = IBV_EXP_INTF_GLOBAL,
3239 .intf = IBV_EXP_INTF_CQ,
3242 tmpl.if_cq = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
3243 if (tmpl.if_cq == NULL) {
3244 ERROR("%p: CQ interface family query failed with status %d",
3245 (void *)dev, status);
3248 attr.params = (struct ibv_exp_query_intf_params){
3249 .intf_scope = IBV_EXP_INTF_GLOBAL,
3250 .intf = IBV_EXP_INTF_QP_BURST,
3253 tmpl.if_qp = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
3254 if (tmpl.if_qp == NULL) {
3255 ERROR("%p: QP interface family query failed with status %d",
3256 (void *)dev, status);
3259 /* Clean up rxq in case we're reinitializing it. */
3260 DEBUG("%p: cleaning-up old rxq just in case", (void *)rxq);
3263 DEBUG("%p: rxq updated with %p", (void *)rxq, (void *)&tmpl);
3273 * DPDK callback to configure a RX queue.
3276 * Pointer to Ethernet device structure.
3280 * Number of descriptors to configure in queue.
3282 * NUMA socket on which memory must be allocated.
3284 * Thresholds parameters.
3286 * Memory pool for buffer allocations.
3289 * 0 on success, negative errno value on failure.
3292 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
3293 unsigned int socket, const struct rte_eth_rxconf *conf,
3294 struct rte_mempool *mp)
3296 struct priv *priv = dev->data->dev_private;
3297 struct rxq *rxq = (*priv->rxqs)[idx];
3301 DEBUG("%p: configuring queue %u for %u descriptors",
3302 (void *)dev, idx, desc);
3303 if (idx >= priv->rxqs_n) {
3304 ERROR("%p: queue index out of range (%u >= %u)",
3305 (void *)dev, idx, priv->rxqs_n);
3310 DEBUG("%p: reusing already allocated queue index %u (%p)",
3311 (void *)dev, idx, (void *)rxq);
3312 if (priv->started) {
3316 (*priv->rxqs)[idx] = NULL;
3319 rxq = rte_calloc_socket("RXQ", 1, sizeof(*rxq), 0, socket);
3321 ERROR("%p: unable to allocate queue index %u",
3327 ret = rxq_setup(dev, rxq, desc, socket, conf, mp);
3331 rxq->stats.idx = idx;
3332 DEBUG("%p: adding RX queue %p to list",
3333 (void *)dev, (void *)rxq);
3334 (*priv->rxqs)[idx] = rxq;
3335 /* Update receive callback. */
3337 dev->rx_pkt_burst = mlx4_rx_burst_sp;
3339 dev->rx_pkt_burst = mlx4_rx_burst;
3346 * DPDK callback to release a RX queue.
3349 * Generic RX queue pointer.
3352 mlx4_rx_queue_release(void *dpdk_rxq)
3354 struct rxq *rxq = (struct rxq *)dpdk_rxq;
3362 assert(rxq != &priv->rxq_parent);
3363 for (i = 0; (i != priv->rxqs_n); ++i)
3364 if ((*priv->rxqs)[i] == rxq) {
3365 DEBUG("%p: removing RX queue %p from list",
3366 (void *)priv->dev, (void *)rxq);
3367 (*priv->rxqs)[i] = NULL;
3376 * DPDK callback to start the device.
3378 * Simulate device start by attaching all configured flows.
3381 * Pointer to Ethernet device structure.
3384 * 0 on success, negative errno value on failure.
3387 mlx4_dev_start(struct rte_eth_dev *dev)
3389 struct priv *priv = dev->data->dev_private;
3395 if (priv->started) {
3399 DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
3402 rxq = &priv->rxq_parent;
3405 rxq = (*priv->rxqs)[0];
3408 /* Iterate only once when RSS is enabled. */
3412 /* Ignore nonexistent RX queues. */
3415 ret = rxq_mac_addrs_add(rxq);
3416 if (!ret && priv->promisc)
3417 ret = rxq_promiscuous_enable(rxq);
3418 if (!ret && priv->allmulti)
3419 ret = rxq_allmulticast_enable(rxq);
3422 WARN("%p: QP flow attachment failed: %s",
3423 (void *)dev, strerror(ret));
3426 rxq = (*priv->rxqs)[--i];
3428 rxq_allmulticast_disable(rxq);
3429 rxq_promiscuous_disable(rxq);
3430 rxq_mac_addrs_del(rxq);
3435 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
3441 * DPDK callback to stop the device.
3443 * Simulate device stop by detaching all configured flows.
3446 * Pointer to Ethernet device structure.
3449 mlx4_dev_stop(struct rte_eth_dev *dev)
3451 struct priv *priv = dev->data->dev_private;
3457 if (!priv->started) {
3461 DEBUG("%p: detaching flows from all RX queues", (void *)dev);
3464 rxq = &priv->rxq_parent;
3467 rxq = (*priv->rxqs)[0];
3470 /* Iterate only once when RSS is enabled. */
3472 /* Ignore nonexistent RX queues. */
3475 rxq_allmulticast_disable(rxq);
3476 rxq_promiscuous_disable(rxq);
3477 rxq_mac_addrs_del(rxq);
3478 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
3483 * Dummy DPDK callback for TX.
3485 * This function is used to temporarily replace the real callback during
3486 * unsafe control operations on the queue, or in case of error.
3489 * Generic pointer to TX queue structure.
3491 * Packets to transmit.
3493 * Number of packets in array.
3496 * Number of packets successfully transmitted (<= pkts_n).
3499 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
3508 * Dummy DPDK callback for RX.
3510 * This function is used to temporarily replace the real callback during
3511 * unsafe control operations on the queue, or in case of error.
3514 * Generic pointer to RX queue structure.
3516 * Array to store received packets.
3518 * Maximum number of packets in array.
3521 * Number of packets successfully received (<= pkts_n).
3524 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
3533 * DPDK callback to close the device.
3535 * Destroy all queues and objects, free memory.
3538 * Pointer to Ethernet device structure.
3541 mlx4_dev_close(struct rte_eth_dev *dev)
3543 struct priv *priv = dev->data->dev_private;
3548 DEBUG("%p: closing device \"%s\"",
3550 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
3551 /* Prevent crashes when queues are still in use. This is unfortunately
3552 * still required for DPDK 1.3 because some programs (such as testpmd)
3553 * never release them before closing the device. */
3554 dev->rx_pkt_burst = removed_rx_burst;
3555 dev->tx_pkt_burst = removed_tx_burst;
3556 if (priv->rxqs != NULL) {
3557 /* XXX race condition if mlx4_rx_burst() is still running. */
3559 for (i = 0; (i != priv->rxqs_n); ++i) {
3560 tmp = (*priv->rxqs)[i];
3563 (*priv->rxqs)[i] = NULL;
3570 if (priv->txqs != NULL) {
3571 /* XXX race condition if mlx4_tx_burst() is still running. */
3573 for (i = 0; (i != priv->txqs_n); ++i) {
3574 tmp = (*priv->txqs)[i];
3577 (*priv->txqs)[i] = NULL;
3585 rxq_cleanup(&priv->rxq_parent);
3586 if (priv->pd != NULL) {
3587 assert(priv->ctx != NULL);
3588 claim_zero(ibv_dealloc_pd(priv->pd));
3589 claim_zero(ibv_close_device(priv->ctx));
3591 assert(priv->ctx == NULL);
3593 memset(priv, 0, sizeof(*priv));
3597 * DPDK callback to get information about the device.
3600 * Pointer to Ethernet device structure.
3602 * Info structure output buffer.
3605 mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
3607 struct priv *priv = dev->data->dev_private;
3611 /* FIXME: we should ask the device for these values. */
3612 info->min_rx_bufsize = 32;
3613 info->max_rx_pktlen = 65536;
3615 * Since we need one CQ per QP, the limit is the minimum number
3616 * between the two values.
3618 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
3619 priv->device_attr.max_qp : priv->device_attr.max_cq);
3620 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
3623 info->max_rx_queues = max;
3624 info->max_tx_queues = max;
3625 info->max_mac_addrs = elemof(priv->mac);
3630 * DPDK callback to get device statistics.
3633 * Pointer to Ethernet device structure.
3635 * Stats structure output buffer.
3638 mlx4_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
3640 struct priv *priv = dev->data->dev_private;
3641 struct rte_eth_stats tmp = {0};
3646 /* Add software counters. */
3647 for (i = 0; (i != priv->rxqs_n); ++i) {
3648 struct rxq *rxq = (*priv->rxqs)[i];
3652 idx = rxq->stats.idx;
3653 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
3654 #ifdef MLX4_PMD_SOFT_COUNTERS
3655 tmp.q_ipackets[idx] += rxq->stats.ipackets;
3656 tmp.q_ibytes[idx] += rxq->stats.ibytes;
3658 tmp.q_errors[idx] += (rxq->stats.idropped +
3659 rxq->stats.rx_nombuf);
3661 #ifdef MLX4_PMD_SOFT_COUNTERS
3662 tmp.ipackets += rxq->stats.ipackets;
3663 tmp.ibytes += rxq->stats.ibytes;
3665 tmp.ierrors += rxq->stats.idropped;
3666 tmp.rx_nombuf += rxq->stats.rx_nombuf;
3668 for (i = 0; (i != priv->txqs_n); ++i) {
3669 struct txq *txq = (*priv->txqs)[i];
3673 idx = txq->stats.idx;
3674 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
3675 #ifdef MLX4_PMD_SOFT_COUNTERS
3676 tmp.q_opackets[idx] += txq->stats.opackets;
3677 tmp.q_obytes[idx] += txq->stats.obytes;
3679 tmp.q_errors[idx] += txq->stats.odropped;
3681 #ifdef MLX4_PMD_SOFT_COUNTERS
3682 tmp.opackets += txq->stats.opackets;
3683 tmp.obytes += txq->stats.obytes;
3685 tmp.oerrors += txq->stats.odropped;
3687 #ifndef MLX4_PMD_SOFT_COUNTERS
3688 /* FIXME: retrieve and add hardware counters. */
3695 * DPDK callback to clear device statistics.
3698 * Pointer to Ethernet device structure.
3701 mlx4_stats_reset(struct rte_eth_dev *dev)
3703 struct priv *priv = dev->data->dev_private;
3708 for (i = 0; (i != priv->rxqs_n); ++i) {
3709 if ((*priv->rxqs)[i] == NULL)
3711 idx = (*priv->rxqs)[i]->stats.idx;
3712 (*priv->rxqs)[i]->stats =
3713 (struct mlx4_rxq_stats){ .idx = idx };
3715 for (i = 0; (i != priv->txqs_n); ++i) {
3716 if ((*priv->txqs)[i] == NULL)
3718 idx = (*priv->rxqs)[i]->stats.idx;
3719 (*priv->txqs)[i]->stats =
3720 (struct mlx4_txq_stats){ .idx = idx };
3722 #ifndef MLX4_PMD_SOFT_COUNTERS
3723 /* FIXME: reset hardware counters. */
3729 * DPDK callback to remove a MAC address.
3732 * Pointer to Ethernet device structure.
3734 * MAC address index.
3737 mlx4_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
3739 struct priv *priv = dev->data->dev_private;
3742 DEBUG("%p: removing MAC address from index %" PRIu32,
3743 (void *)dev, index);
3744 if (index >= MLX4_MAX_MAC_ADDRESSES)
3746 /* Refuse to remove the broadcast address, this one is special. */
3747 if (!memcmp(priv->mac[index].addr_bytes, "\xff\xff\xff\xff\xff\xff",
3750 priv_mac_addr_del(priv, index);
3756 * DPDK callback to add a MAC address.
3759 * Pointer to Ethernet device structure.
3761 * MAC address to register.
3763 * MAC address index.
3765 * VMDq pool index to associate address with (ignored).
3768 mlx4_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
3769 uint32_t index, uint32_t vmdq)
3771 struct priv *priv = dev->data->dev_private;
3775 DEBUG("%p: adding MAC address at index %" PRIu32,
3776 (void *)dev, index);
3777 if (index >= MLX4_MAX_MAC_ADDRESSES)
3779 /* Refuse to add the broadcast address, this one is special. */
3780 if (!memcmp(mac_addr->addr_bytes, "\xff\xff\xff\xff\xff\xff",
3783 priv_mac_addr_add(priv, index,
3784 (const uint8_t (*)[ETHER_ADDR_LEN])
3785 mac_addr->addr_bytes);
3791 * DPDK callback to enable promiscuous mode.
3794 * Pointer to Ethernet device structure.
3797 mlx4_promiscuous_enable(struct rte_eth_dev *dev)
3799 struct priv *priv = dev->data->dev_private;
3804 if (priv->promisc) {
3808 /* If device isn't started, this is all we need to do. */
3812 ret = rxq_promiscuous_enable(&priv->rxq_parent);
3819 for (i = 0; (i != priv->rxqs_n); ++i) {
3820 if ((*priv->rxqs)[i] == NULL)
3822 ret = rxq_promiscuous_enable((*priv->rxqs)[i]);
3825 /* Failure, rollback. */
3827 if ((*priv->rxqs)[--i] != NULL)
3828 rxq_promiscuous_disable((*priv->rxqs)[i]);
3838 * DPDK callback to disable promiscuous mode.
3841 * Pointer to Ethernet device structure.
3844 mlx4_promiscuous_disable(struct rte_eth_dev *dev)
3846 struct priv *priv = dev->data->dev_private;
3850 if (!priv->promisc) {
3855 rxq_promiscuous_disable(&priv->rxq_parent);
3858 for (i = 0; (i != priv->rxqs_n); ++i)
3859 if ((*priv->rxqs)[i] != NULL)
3860 rxq_promiscuous_disable((*priv->rxqs)[i]);
3867 * DPDK callback to enable allmulti mode.
3870 * Pointer to Ethernet device structure.
3873 mlx4_allmulticast_enable(struct rte_eth_dev *dev)
3875 struct priv *priv = dev->data->dev_private;
3880 if (priv->allmulti) {
3884 /* If device isn't started, this is all we need to do. */
3888 ret = rxq_allmulticast_enable(&priv->rxq_parent);
3895 for (i = 0; (i != priv->rxqs_n); ++i) {
3896 if ((*priv->rxqs)[i] == NULL)
3898 ret = rxq_allmulticast_enable((*priv->rxqs)[i]);
3901 /* Failure, rollback. */
3903 if ((*priv->rxqs)[--i] != NULL)
3904 rxq_allmulticast_disable((*priv->rxqs)[i]);
3914 * DPDK callback to disable allmulti mode.
3917 * Pointer to Ethernet device structure.
3920 mlx4_allmulticast_disable(struct rte_eth_dev *dev)
3922 struct priv *priv = dev->data->dev_private;
3926 if (!priv->allmulti) {
3931 rxq_allmulticast_disable(&priv->rxq_parent);
3934 for (i = 0; (i != priv->rxqs_n); ++i)
3935 if ((*priv->rxqs)[i] != NULL)
3936 rxq_allmulticast_disable((*priv->rxqs)[i]);
3943 * DPDK callback to retrieve physical link information (unlocked version).
3946 * Pointer to Ethernet device structure.
3947 * @param wait_to_complete
3948 * Wait for request completion (ignored).
3951 mlx4_link_update_unlocked(struct rte_eth_dev *dev, int wait_to_complete)
3953 struct priv *priv = dev->data->dev_private;
3954 struct ethtool_cmd edata = {
3958 struct rte_eth_link dev_link;
3961 (void)wait_to_complete;
3962 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
3963 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
3966 memset(&dev_link, 0, sizeof(dev_link));
3967 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
3968 (ifr.ifr_flags & IFF_RUNNING));
3969 ifr.ifr_data = &edata;
3970 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
3971 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
3975 link_speed = ethtool_cmd_speed(&edata);
3976 if (link_speed == -1)
3977 dev_link.link_speed = 0;
3979 dev_link.link_speed = link_speed;
3980 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
3981 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
3982 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
3983 /* Link status changed. */
3984 dev->data->dev_link = dev_link;
3987 /* Link status is still the same. */
3992 * DPDK callback to retrieve physical link information.
3995 * Pointer to Ethernet device structure.
3996 * @param wait_to_complete
3997 * Wait for request completion (ignored).
4000 mlx4_link_update(struct rte_eth_dev *dev, int wait_to_complete)
4002 struct priv *priv = dev->data->dev_private;
4006 ret = mlx4_link_update_unlocked(dev, wait_to_complete);
4012 * DPDK callback to change the MTU.
4014 * Setting the MTU affects hardware MRU (packets larger than the MTU cannot be
4015 * received). Use this as a hint to enable/disable scattered packets support
4016 * and improve performance when not needed.
4017 * Since failure is not an option, reconfiguring queues on the fly is not
4021 * Pointer to Ethernet device structure.
4026 * 0 on success, negative errno value on failure.
4029 mlx4_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
4031 struct priv *priv = dev->data->dev_private;
4034 uint16_t (*rx_func)(void *, struct rte_mbuf **, uint16_t) =
4038 /* Set kernel interface MTU first. */
4039 if (priv_set_mtu(priv, mtu)) {
4041 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
4045 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
4047 /* Temporarily replace RX handler with a fake one, assuming it has not
4048 * been copied elsewhere. */
4049 dev->rx_pkt_burst = removed_rx_burst;
4050 /* Make sure everyone has left mlx4_rx_burst() and uses
4051 * removed_rx_burst() instead. */
4054 /* Reconfigure each RX queue. */
4055 for (i = 0; (i != priv->rxqs_n); ++i) {
4056 struct rxq *rxq = (*priv->rxqs)[i];
4057 unsigned int max_frame_len;
4062 /* Calculate new maximum frame length according to MTU and
4063 * toggle scattered support (sp) if necessary. */
4064 max_frame_len = (priv->mtu + ETHER_HDR_LEN +
4065 (ETHER_MAX_VLAN_FRAME_LEN - ETHER_MAX_LEN));
4066 sp = (max_frame_len > (rxq->mb_len - RTE_PKTMBUF_HEADROOM));
4067 /* Provide new values to rxq_setup(). */
4068 dev->data->dev_conf.rxmode.jumbo_frame = sp;
4069 dev->data->dev_conf.rxmode.max_rx_pkt_len = max_frame_len;
4070 ret = rxq_rehash(dev, rxq);
4072 /* Force SP RX if that queue requires it and abort. */
4074 rx_func = mlx4_rx_burst_sp;
4077 /* Reenable non-RSS queue attributes. No need to check
4078 * for errors at this stage. */
4080 rxq_mac_addrs_add(rxq);
4082 rxq_promiscuous_enable(rxq);
4084 rxq_allmulticast_enable(rxq);
4086 /* Scattered burst function takes priority. */
4088 rx_func = mlx4_rx_burst_sp;
4090 /* Burst functions can now be called again. */
4092 dev->rx_pkt_burst = rx_func;
4100 * DPDK callback to get flow control status.
4103 * Pointer to Ethernet device structure.
4104 * @param[out] fc_conf
4105 * Flow control output buffer.
4108 * 0 on success, negative errno value on failure.
4111 mlx4_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4113 struct priv *priv = dev->data->dev_private;
4115 struct ethtool_pauseparam ethpause = {
4116 .cmd = ETHTOOL_GPAUSEPARAM
4120 ifr.ifr_data = ðpause;
4122 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4124 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
4130 fc_conf->autoneg = ethpause.autoneg;
4131 if (ethpause.rx_pause && ethpause.tx_pause)
4132 fc_conf->mode = RTE_FC_FULL;
4133 else if (ethpause.rx_pause)
4134 fc_conf->mode = RTE_FC_RX_PAUSE;
4135 else if (ethpause.tx_pause)
4136 fc_conf->mode = RTE_FC_TX_PAUSE;
4138 fc_conf->mode = RTE_FC_NONE;
4148 * DPDK callback to modify flow control parameters.
4151 * Pointer to Ethernet device structure.
4152 * @param[in] fc_conf
4153 * Flow control parameters.
4156 * 0 on success, negative errno value on failure.
4159 mlx4_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4161 struct priv *priv = dev->data->dev_private;
4163 struct ethtool_pauseparam ethpause = {
4164 .cmd = ETHTOOL_SPAUSEPARAM
4168 ifr.ifr_data = ðpause;
4169 ethpause.autoneg = fc_conf->autoneg;
4170 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4171 (fc_conf->mode & RTE_FC_RX_PAUSE))
4172 ethpause.rx_pause = 1;
4174 ethpause.rx_pause = 0;
4176 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4177 (fc_conf->mode & RTE_FC_TX_PAUSE))
4178 ethpause.tx_pause = 1;
4180 ethpause.tx_pause = 0;
4183 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4185 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
4199 * Configure a VLAN filter.
4202 * Pointer to Ethernet device structure.
4204 * VLAN ID to filter.
4209 * 0 on success, errno value on failure.
4212 vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
4214 struct priv *priv = dev->data->dev_private;
4216 unsigned int j = -1;
4218 DEBUG("%p: %s VLAN filter ID %" PRIu16,
4219 (void *)dev, (on ? "enable" : "disable"), vlan_id);
4220 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
4221 if (!priv->vlan_filter[i].enabled) {
4222 /* Unused index, remember it. */
4226 if (priv->vlan_filter[i].id != vlan_id)
4228 /* This VLAN ID is already known, use its index. */
4232 /* Check if there's room for another VLAN filter. */
4233 if (j == (unsigned int)-1)
4236 * VLAN filters apply to all configured MAC addresses, flow
4237 * specifications must be reconfigured accordingly.
4239 priv->vlan_filter[j].id = vlan_id;
4240 if ((on) && (!priv->vlan_filter[j].enabled)) {
4242 * Filter is disabled, enable it.
4243 * Rehashing flows in all RX queues is necessary.
4246 rxq_mac_addrs_del(&priv->rxq_parent);
4248 for (i = 0; (i != priv->rxqs_n); ++i)
4249 if ((*priv->rxqs)[i] != NULL)
4250 rxq_mac_addrs_del((*priv->rxqs)[i]);
4251 priv->vlan_filter[j].enabled = 1;
4252 if (priv->started) {
4254 rxq_mac_addrs_add(&priv->rxq_parent);
4256 for (i = 0; (i != priv->rxqs_n); ++i) {
4257 if ((*priv->rxqs)[i] == NULL)
4259 rxq_mac_addrs_add((*priv->rxqs)[i]);
4262 } else if ((!on) && (priv->vlan_filter[j].enabled)) {
4264 * Filter is enabled, disable it.
4265 * Rehashing flows in all RX queues is necessary.
4268 rxq_mac_addrs_del(&priv->rxq_parent);
4270 for (i = 0; (i != priv->rxqs_n); ++i)
4271 if ((*priv->rxqs)[i] != NULL)
4272 rxq_mac_addrs_del((*priv->rxqs)[i]);
4273 priv->vlan_filter[j].enabled = 0;
4274 if (priv->started) {
4276 rxq_mac_addrs_add(&priv->rxq_parent);
4278 for (i = 0; (i != priv->rxqs_n); ++i) {
4279 if ((*priv->rxqs)[i] == NULL)
4281 rxq_mac_addrs_add((*priv->rxqs)[i]);
4289 * DPDK callback to configure a VLAN filter.
4292 * Pointer to Ethernet device structure.
4294 * VLAN ID to filter.
4299 * 0 on success, negative errno value on failure.
4302 mlx4_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
4304 struct priv *priv = dev->data->dev_private;
4308 ret = vlan_filter_set(dev, vlan_id, on);
4314 static const struct eth_dev_ops mlx4_dev_ops = {
4315 .dev_configure = mlx4_dev_configure,
4316 .dev_start = mlx4_dev_start,
4317 .dev_stop = mlx4_dev_stop,
4318 .dev_close = mlx4_dev_close,
4319 .promiscuous_enable = mlx4_promiscuous_enable,
4320 .promiscuous_disable = mlx4_promiscuous_disable,
4321 .allmulticast_enable = mlx4_allmulticast_enable,
4322 .allmulticast_disable = mlx4_allmulticast_disable,
4323 .link_update = mlx4_link_update,
4324 .stats_get = mlx4_stats_get,
4325 .stats_reset = mlx4_stats_reset,
4326 .queue_stats_mapping_set = NULL,
4327 .dev_infos_get = mlx4_dev_infos_get,
4328 .vlan_filter_set = mlx4_vlan_filter_set,
4329 .vlan_tpid_set = NULL,
4330 .vlan_strip_queue_set = NULL,
4331 .vlan_offload_set = NULL,
4332 .rx_queue_setup = mlx4_rx_queue_setup,
4333 .tx_queue_setup = mlx4_tx_queue_setup,
4334 .rx_queue_release = mlx4_rx_queue_release,
4335 .tx_queue_release = mlx4_tx_queue_release,
4337 .dev_led_off = NULL,
4338 .flow_ctrl_get = mlx4_dev_get_flow_ctrl,
4339 .flow_ctrl_set = mlx4_dev_set_flow_ctrl,
4340 .priority_flow_ctrl_set = NULL,
4341 .mac_addr_remove = mlx4_mac_addr_remove,
4342 .mac_addr_add = mlx4_mac_addr_add,
4343 .mtu_set = mlx4_dev_set_mtu,
4344 .fdir_add_signature_filter = NULL,
4345 .fdir_update_signature_filter = NULL,
4346 .fdir_remove_signature_filter = NULL,
4347 .fdir_add_perfect_filter = NULL,
4348 .fdir_update_perfect_filter = NULL,
4349 .fdir_remove_perfect_filter = NULL,
4350 .fdir_set_masks = NULL
4354 * Get PCI information from struct ibv_device.
4357 * Pointer to Ethernet device structure.
4358 * @param[out] pci_addr
4359 * PCI bus address output buffer.
4362 * 0 on success, -1 on failure and errno is set.
4365 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
4366 struct rte_pci_addr *pci_addr)
4370 MKSTR(path, "%s/device/uevent", device->ibdev_path);
4372 file = fopen(path, "rb");
4375 while (fgets(line, sizeof(line), file) == line) {
4376 size_t len = strlen(line);
4379 /* Truncate long lines. */
4380 if (len == (sizeof(line) - 1))
4381 while (line[(len - 1)] != '\n') {
4385 line[(len - 1)] = ret;
4387 /* Extract information. */
4390 "%" SCNx16 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
4394 &pci_addr->function) == 4) {
4404 * Get MAC address by querying netdevice.
4407 * struct priv for the requested device.
4409 * MAC address output buffer.
4412 * 0 on success, -1 on failure and errno is set.
4415 priv_get_mac(struct priv *priv, uint8_t (*mac)[ETHER_ADDR_LEN])
4417 struct ifreq request;
4419 if (priv_ifreq(priv, SIOCGIFHWADDR, &request))
4421 memcpy(mac, request.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
4425 /* Support up to 32 adapters. */
4427 struct rte_pci_addr pci_addr; /* associated PCI address */
4428 uint32_t ports; /* physical ports bitfield. */
4432 * Get device index in mlx4_dev[] from PCI bus address.
4434 * @param[in] pci_addr
4435 * PCI bus address to look for.
4438 * mlx4_dev[] index on success, -1 on failure.
4441 mlx4_dev_idx(struct rte_pci_addr *pci_addr)
4446 assert(pci_addr != NULL);
4447 for (i = 0; (i != elemof(mlx4_dev)); ++i) {
4448 if ((mlx4_dev[i].pci_addr.domain == pci_addr->domain) &&
4449 (mlx4_dev[i].pci_addr.bus == pci_addr->bus) &&
4450 (mlx4_dev[i].pci_addr.devid == pci_addr->devid) &&
4451 (mlx4_dev[i].pci_addr.function == pci_addr->function))
4453 if ((mlx4_dev[i].ports == 0) && (ret == -1))
4460 * Retrieve integer value from environment variable.
4463 * Environment variable name.
4466 * Integer value, 0 if the variable is not set.
4469 mlx4_getenv_int(const char *name)
4471 const char *val = getenv(name);
4478 static struct eth_driver mlx4_driver;
4481 * DPDK callback to register a PCI device.
4483 * This function creates an Ethernet device for each port of a given
4486 * @param[in] pci_drv
4487 * PCI driver structure (mlx4_driver).
4488 * @param[in] pci_dev
4489 * PCI device information.
4492 * 0 on success, negative errno value on failure.
4495 mlx4_pci_devinit(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
4497 struct ibv_device **list;
4498 struct ibv_device *ibv_dev;
4500 struct ibv_context *attr_ctx = NULL;
4501 struct ibv_device_attr device_attr;
4507 assert(pci_drv == &mlx4_driver.pci_drv);
4508 /* Get mlx4_dev[] index. */
4509 idx = mlx4_dev_idx(&pci_dev->addr);
4511 ERROR("this driver cannot support any more adapters");
4514 DEBUG("using driver device index %d", idx);
4516 /* Save PCI address. */
4517 mlx4_dev[idx].pci_addr = pci_dev->addr;
4518 list = ibv_get_device_list(&i);
4521 if (errno == ENOSYS) {
4522 WARN("cannot list devices, is ib_uverbs loaded?");
4529 * For each listed device, check related sysfs entry against
4530 * the provided PCI ID.
4533 struct rte_pci_addr pci_addr;
4536 DEBUG("checking device \"%s\"", list[i]->name);
4537 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
4539 if ((pci_dev->addr.domain != pci_addr.domain) ||
4540 (pci_dev->addr.bus != pci_addr.bus) ||
4541 (pci_dev->addr.devid != pci_addr.devid) ||
4542 (pci_dev->addr.function != pci_addr.function))
4544 vf = (pci_dev->id.device_id ==
4545 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
4546 INFO("PCI information matches, using device \"%s\" (VF: %s)",
4547 list[i]->name, (vf ? "true" : "false"));
4548 attr_ctx = ibv_open_device(list[i]);
4552 if (attr_ctx == NULL) {
4553 ibv_free_device_list(list);
4556 WARN("cannot access device, is mlx4_ib loaded?");
4559 WARN("cannot use device, are drivers up to date?");
4567 DEBUG("device opened");
4568 if (ibv_query_device(attr_ctx, &device_attr))
4570 INFO("%u port(s) detected", device_attr.phys_port_cnt);
4572 for (i = 0; i < device_attr.phys_port_cnt; i++) {
4573 uint32_t port = i + 1; /* ports are indexed from one */
4574 uint32_t test = (1 << i);
4575 struct ibv_context *ctx = NULL;
4576 struct ibv_port_attr port_attr;
4577 struct ibv_pd *pd = NULL;
4578 struct priv *priv = NULL;
4579 struct rte_eth_dev *eth_dev;
4580 #ifdef HAVE_EXP_QUERY_DEVICE
4581 struct ibv_exp_device_attr exp_device_attr;
4582 #endif /* HAVE_EXP_QUERY_DEVICE */
4583 struct ether_addr mac;
4585 #ifdef HAVE_EXP_QUERY_DEVICE
4586 exp_device_attr.comp_mask = IBV_EXP_DEVICE_ATTR_EXP_CAP_FLAGS;
4588 exp_device_attr.comp_mask |= IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ;
4589 #endif /* RSS_SUPPORT */
4590 #endif /* HAVE_EXP_QUERY_DEVICE */
4592 DEBUG("using port %u (%08" PRIx32 ")", port, test);
4594 ctx = ibv_open_device(ibv_dev);
4598 /* Check port status. */
4599 err = ibv_query_port(ctx, port, &port_attr);
4601 ERROR("port query failed: %s", strerror(err));
4604 if (port_attr.state != IBV_PORT_ACTIVE)
4605 WARN("bad state for port %d: \"%s\" (%d)",
4606 port, ibv_port_state_str(port_attr.state),
4609 /* Allocate protection domain. */
4610 pd = ibv_alloc_pd(ctx);
4612 ERROR("PD allocation failure");
4617 mlx4_dev[idx].ports |= test;
4619 /* from rte_ethdev.c */
4620 priv = rte_zmalloc("ethdev private structure",
4622 RTE_CACHE_LINE_SIZE);
4624 ERROR("priv allocation failure");
4630 priv->device_attr = device_attr;
4633 priv->mtu = ETHER_MTU;
4634 #ifdef HAVE_EXP_QUERY_DEVICE
4635 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
4636 ERROR("ibv_exp_query_device() failed");
4640 if ((exp_device_attr.exp_device_cap_flags &
4641 IBV_EXP_DEVICE_QPG) &&
4642 (exp_device_attr.exp_device_cap_flags &
4643 IBV_EXP_DEVICE_UD_RSS) &&
4644 (exp_device_attr.comp_mask &
4645 IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ) &&
4646 (exp_device_attr.max_rss_tbl_sz > 0)) {
4649 priv->max_rss_tbl_sz = exp_device_attr.max_rss_tbl_sz;
4653 priv->max_rss_tbl_sz = 0;
4655 priv->hw_tss = !!(exp_device_attr.exp_device_cap_flags &
4656 IBV_EXP_DEVICE_UD_TSS);
4657 DEBUG("device flags: %s%s%s",
4658 (priv->hw_qpg ? "IBV_DEVICE_QPG " : ""),
4659 (priv->hw_tss ? "IBV_DEVICE_TSS " : ""),
4660 (priv->hw_rss ? "IBV_DEVICE_RSS " : ""));
4662 DEBUG("maximum RSS indirection table size: %u",
4663 exp_device_attr.max_rss_tbl_sz);
4664 #endif /* RSS_SUPPORT */
4667 priv->inl_recv_size = mlx4_getenv_int("MLX4_INLINE_RECV_SIZE");
4669 if (priv->inl_recv_size) {
4670 exp_device_attr.comp_mask =
4671 IBV_EXP_DEVICE_ATTR_INLINE_RECV_SZ;
4672 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
4673 INFO("Couldn't query device for inline-receive"
4675 priv->inl_recv_size = 0;
4677 if ((unsigned)exp_device_attr.inline_recv_sz <
4678 priv->inl_recv_size) {
4679 INFO("Max inline-receive (%d) <"
4680 " requested inline-receive (%u)",
4681 exp_device_attr.inline_recv_sz,
4682 priv->inl_recv_size);
4683 priv->inl_recv_size =
4684 exp_device_attr.inline_recv_sz;
4687 INFO("Set inline receive size to %u",
4688 priv->inl_recv_size);
4690 #endif /* INLINE_RECV */
4691 #endif /* HAVE_EXP_QUERY_DEVICE */
4693 (void)mlx4_getenv_int;
4695 /* Configure the first MAC address by default. */
4696 if (priv_get_mac(priv, &mac.addr_bytes)) {
4697 ERROR("cannot get MAC address, is mlx4_en loaded?"
4698 " (errno: %s)", strerror(errno));
4701 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
4703 mac.addr_bytes[0], mac.addr_bytes[1],
4704 mac.addr_bytes[2], mac.addr_bytes[3],
4705 mac.addr_bytes[4], mac.addr_bytes[5]);
4706 /* Register MAC and broadcast addresses. */
4707 claim_zero(priv_mac_addr_add(priv, 0,
4708 (const uint8_t (*)[ETHER_ADDR_LEN])
4710 claim_zero(priv_mac_addr_add(priv, 1,
4711 &(const uint8_t [ETHER_ADDR_LEN])
4712 { "\xff\xff\xff\xff\xff\xff" }));
4715 char ifname[IF_NAMESIZE];
4717 if (priv_get_ifname(priv, &ifname) == 0)
4718 DEBUG("port %u ifname is \"%s\"",
4719 priv->port, ifname);
4721 DEBUG("port %u ifname is unknown", priv->port);
4724 /* Get actual MTU if possible. */
4725 priv_get_mtu(priv, &priv->mtu);
4726 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
4728 /* from rte_ethdev.c */
4730 char name[RTE_ETH_NAME_MAX_LEN];
4732 snprintf(name, sizeof(name), "%s port %u",
4733 ibv_get_device_name(ibv_dev), port);
4734 eth_dev = rte_eth_dev_allocate(name, RTE_ETH_DEV_PCI);
4736 if (eth_dev == NULL) {
4737 ERROR("can not allocate rte ethdev");
4742 eth_dev->data->dev_private = priv;
4743 eth_dev->pci_dev = pci_dev;
4744 eth_dev->driver = &mlx4_driver;
4745 eth_dev->data->rx_mbuf_alloc_failed = 0;
4746 eth_dev->data->mtu = ETHER_MTU;
4748 priv->dev = eth_dev;
4749 eth_dev->dev_ops = &mlx4_dev_ops;
4750 eth_dev->data->mac_addrs = priv->mac;
4752 /* Bring Ethernet device up. */
4753 DEBUG("forcing Ethernet interface up");
4754 priv_set_flags(priv, ~IFF_UP, IFF_UP);
4760 claim_zero(ibv_dealloc_pd(pd));
4762 claim_zero(ibv_close_device(ctx));
4767 * XXX if something went wrong in the loop above, there is a resource
4768 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
4769 * long as the dpdk does not provide a way to deallocate a ethdev and a
4770 * way to enumerate the registered ethdevs to free the previous ones.
4773 /* no port found, complain */
4774 if (!mlx4_dev[idx].ports) {
4781 claim_zero(ibv_close_device(attr_ctx));
4783 ibv_free_device_list(list);
4788 static const struct rte_pci_id mlx4_pci_id_map[] = {
4790 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4791 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3,
4792 .subsystem_vendor_id = PCI_ANY_ID,
4793 .subsystem_device_id = PCI_ANY_ID
4796 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4797 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO,
4798 .subsystem_vendor_id = PCI_ANY_ID,
4799 .subsystem_device_id = PCI_ANY_ID
4802 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4803 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3VF,
4804 .subsystem_vendor_id = PCI_ANY_ID,
4805 .subsystem_device_id = PCI_ANY_ID
4812 static struct eth_driver mlx4_driver = {
4814 .name = MLX4_DRIVER_NAME,
4815 .id_table = mlx4_pci_id_map,
4816 .devinit = mlx4_pci_devinit,
4818 .dev_private_size = sizeof(struct priv)
4822 * Driver initialization routine.
4825 rte_mlx4_pmd_init(const char *name, const char *args)
4830 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
4831 * huge pages. Calling ibv_fork_init() during init allows
4832 * applications to use fork() safely for purposes other than
4833 * using this PMD, which is not supported in forked processes.
4835 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
4837 rte_eal_pci_register(&mlx4_driver.pci_drv);
4841 static struct rte_driver rte_mlx4_driver = {
4843 .name = MLX4_DRIVER_NAME,
4844 .init = rte_mlx4_pmd_init,
4847 PMD_REGISTER_DRIVER(rte_mlx4_driver)