4 * Copyright 2012-2015 6WIND S.A.
5 * Copyright 2012 Mellanox.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of 6WIND S.A. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * - RSS hash key and options cannot be modified.
37 * - Hardware counters aren't implemented.
51 #include <arpa/inet.h>
54 #include <sys/ioctl.h>
55 #include <sys/socket.h>
56 #include <netinet/in.h>
58 #include <linux/ethtool.h>
59 #include <linux/sockios.h>
62 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
64 #pragma GCC diagnostic ignored "-pedantic"
66 #include <infiniband/verbs.h>
68 #pragma GCC diagnostic error "-pedantic"
71 /* DPDK headers don't like -pedantic. */
73 #pragma GCC diagnostic ignored "-pedantic"
75 #include <rte_config.h>
76 #include <rte_ether.h>
77 #include <rte_ethdev.h>
80 #include <rte_errno.h>
81 #include <rte_mempool.h>
82 #include <rte_prefetch.h>
83 #include <rte_malloc.h>
84 #include <rte_spinlock.h>
85 #include <rte_atomic.h>
86 #include <rte_version.h>
89 #pragma GCC diagnostic error "-pedantic"
92 /* Generated configuration header. */
93 #include "mlx4_autoconf.h"
98 /* Runtime logging through RTE_LOG() is enabled when not in debugging mode.
99 * Intermediate LOG_*() macros add the required end-of-line characters. */
101 #define INFO(...) DEBUG(__VA_ARGS__)
102 #define WARN(...) DEBUG(__VA_ARGS__)
103 #define ERROR(...) DEBUG(__VA_ARGS__)
105 #define LOG__(level, m, ...) \
106 RTE_LOG(level, PMD, MLX4_DRIVER_NAME ": " m "%c", __VA_ARGS__)
107 #define LOG_(level, ...) LOG__(level, __VA_ARGS__, '\n')
108 #define INFO(...) LOG_(INFO, __VA_ARGS__)
109 #define WARN(...) LOG_(WARNING, __VA_ARGS__)
110 #define ERROR(...) LOG_(ERR, __VA_ARGS__)
113 /* Convenience macros for accessing mbuf fields. */
114 #define NEXT(m) ((m)->next)
115 #define DATA_LEN(m) ((m)->data_len)
116 #define PKT_LEN(m) ((m)->pkt_len)
117 #define DATA_OFF(m) ((m)->data_off)
118 #define SET_DATA_OFF(m, o) ((m)->data_off = (o))
119 #define NB_SEGS(m) ((m)->nb_segs)
120 #define PORT(m) ((m)->port)
122 /* Work Request ID data type (64 bit). */
131 #define WR_ID(o) (((wr_id_t *)&(o))->data)
133 /* Compile-time check. */
134 static inline void wr_id_t_check(void)
136 wr_id_t check[1 + (2 * -!(sizeof(wr_id_t) == sizeof(uint64_t)))];
142 struct mlx4_rxq_stats {
143 unsigned int idx; /**< Mapping index. */
144 #ifdef MLX4_PMD_SOFT_COUNTERS
145 uint64_t ipackets; /**< Total of successfully received packets. */
146 uint64_t ibytes; /**< Total of successfully received bytes. */
148 uint64_t idropped; /**< Total of packets dropped when RX ring full. */
149 uint64_t rx_nombuf; /**< Total of RX mbuf allocation failures. */
152 struct mlx4_txq_stats {
153 unsigned int idx; /**< Mapping index. */
154 #ifdef MLX4_PMD_SOFT_COUNTERS
155 uint64_t opackets; /**< Total of successfully sent packets. */
156 uint64_t obytes; /**< Total of successfully sent bytes. */
158 uint64_t odropped; /**< Total of packets not sent when TX ring full. */
161 /* RX element (scattered packets). */
163 struct ibv_recv_wr wr; /* Work Request. */
164 struct ibv_sge sges[MLX4_PMD_SGE_WR_N]; /* Scatter/Gather Elements. */
165 struct rte_mbuf *bufs[MLX4_PMD_SGE_WR_N]; /* SGEs buffers. */
170 struct ibv_recv_wr wr; /* Work Request. */
171 struct ibv_sge sge; /* Scatter/Gather Element. */
172 /* mbuf pointer is derived from WR_ID(wr.wr_id).offset. */
175 /* RX queue descriptor. */
177 struct priv *priv; /* Back pointer to private data. */
178 struct rte_mempool *mp; /* Memory Pool for allocations. */
179 struct ibv_mr *mr; /* Memory Region (for mp). */
180 struct ibv_cq *cq; /* Completion Queue. */
181 struct ibv_qp *qp; /* Queue Pair. */
182 struct ibv_exp_qp_burst_family *if_qp; /* QP burst interface. */
183 struct ibv_exp_cq_family *if_cq; /* CQ interface. */
185 * Each VLAN ID requires a separate flow steering rule.
187 BITFIELD_DECLARE(mac_configured, uint32_t, MLX4_MAX_MAC_ADDRESSES);
188 struct ibv_flow *mac_flow[MLX4_MAX_MAC_ADDRESSES][MLX4_MAX_VLAN_IDS];
189 struct ibv_flow *promisc_flow; /* Promiscuous flow. */
190 struct ibv_flow *allmulti_flow; /* Multicast flow. */
191 unsigned int port_id; /* Port ID for incoming packets. */
192 unsigned int elts_n; /* (*elts)[] length. */
193 unsigned int elts_head; /* Current index in (*elts)[]. */
195 struct rxq_elt_sp (*sp)[]; /* Scattered RX elements. */
196 struct rxq_elt (*no_sp)[]; /* RX elements. */
198 unsigned int sp:1; /* Use scattered RX elements. */
199 uint32_t mb_len; /* Length of a mp-issued mbuf. */
200 struct mlx4_rxq_stats stats; /* RX queue counters. */
201 unsigned int socket; /* CPU socket ID for allocations. */
206 struct rte_mbuf *buf;
209 /* Linear buffer type. It is used when transmitting buffers with too many
210 * segments that do not fit the hardware queue (see max_send_sge).
211 * Extra segments are copied (linearized) in such buffers, replacing the
212 * last SGE during TX.
213 * The size is arbitrary but large enough to hold a jumbo frame with
214 * 8 segments considering mbuf.buf_len is about 2048 bytes. */
215 typedef uint8_t linear_t[16384];
217 /* TX queue descriptor. */
219 struct priv *priv; /* Back pointer to private data. */
221 struct rte_mempool *mp; /* Cached Memory Pool. */
222 struct ibv_mr *mr; /* Memory Region (for mp). */
223 uint32_t lkey; /* mr->lkey */
224 } mp2mr[MLX4_PMD_TX_MP_CACHE]; /* MP to MR translation table. */
225 struct ibv_cq *cq; /* Completion Queue. */
226 struct ibv_qp *qp; /* Queue Pair. */
227 struct ibv_exp_qp_burst_family *if_qp; /* QP burst interface. */
228 struct ibv_exp_cq_family *if_cq; /* CQ interface. */
229 #if MLX4_PMD_MAX_INLINE > 0
230 uint32_t max_inline; /* Max inline send size <= MLX4_PMD_MAX_INLINE. */
232 unsigned int elts_n; /* (*elts)[] length. */
233 struct txq_elt (*elts)[]; /* TX elements. */
234 unsigned int elts_head; /* Current index in (*elts)[]. */
235 unsigned int elts_tail; /* First element awaiting completion. */
236 unsigned int elts_comp; /* Number of completion requests. */
237 unsigned int elts_comp_cd; /* Countdown for next completion request. */
238 unsigned int elts_comp_cd_init; /* Initial value for countdown. */
239 struct mlx4_txq_stats stats; /* TX queue counters. */
240 linear_t (*elts_linear)[]; /* Linearized buffers. */
241 struct ibv_mr *mr_linear; /* Memory Region for linearized buffers. */
242 unsigned int socket; /* CPU socket ID for allocations. */
246 struct rte_eth_dev *dev; /* Ethernet device. */
247 struct ibv_context *ctx; /* Verbs context. */
248 struct ibv_device_attr device_attr; /* Device properties. */
249 struct ibv_pd *pd; /* Protection Domain. */
251 * MAC addresses array and configuration bit-field.
252 * An extra entry that cannot be modified by the DPDK is reserved
253 * for broadcast frames (destination MAC address ff:ff:ff:ff:ff:ff).
255 struct ether_addr mac[MLX4_MAX_MAC_ADDRESSES];
256 BITFIELD_DECLARE(mac_configured, uint32_t, MLX4_MAX_MAC_ADDRESSES);
259 unsigned int enabled:1; /* If enabled. */
260 unsigned int id:12; /* VLAN ID (0-4095). */
261 } vlan_filter[MLX4_MAX_VLAN_IDS]; /* VLAN filters table. */
262 /* Device properties. */
263 uint16_t mtu; /* Configured MTU. */
264 uint8_t port; /* Physical port number. */
265 unsigned int started:1; /* Device started, flows enabled. */
266 unsigned int promisc:1; /* Device in promiscuous mode. */
267 unsigned int allmulti:1; /* Device receives all multicast packets. */
268 unsigned int hw_qpg:1; /* QP groups are supported. */
269 unsigned int hw_tss:1; /* TSS is supported. */
270 unsigned int hw_rss:1; /* RSS is supported. */
271 unsigned int rss:1; /* RSS is enabled. */
272 unsigned int vf:1; /* This is a VF device. */
274 unsigned int inl_recv_size; /* Inline recv size */
276 unsigned int max_rss_tbl_sz; /* Maximum number of RSS queues. */
278 struct rxq rxq_parent; /* Parent queue when RSS is enabled. */
279 unsigned int rxqs_n; /* RX queues array size. */
280 unsigned int txqs_n; /* TX queues array size. */
281 struct rxq *(*rxqs)[]; /* RX queues. */
282 struct txq *(*txqs)[]; /* TX queues. */
283 rte_spinlock_t lock; /* Lock for control functions. */
287 * Lock private structure to protect it from concurrent access in the
291 * Pointer to private structure.
294 priv_lock(struct priv *priv)
296 rte_spinlock_lock(&priv->lock);
300 * Unlock private structure.
303 * Pointer to private structure.
306 priv_unlock(struct priv *priv)
308 rte_spinlock_unlock(&priv->lock);
311 /* Allocate a buffer on the stack and fill it with a printf format string. */
312 #define MKSTR(name, ...) \
313 char name[snprintf(NULL, 0, __VA_ARGS__) + 1]; \
315 snprintf(name, sizeof(name), __VA_ARGS__)
318 * Get interface name from private structure.
321 * Pointer to private structure.
323 * Interface name output buffer.
326 * 0 on success, -1 on failure and errno is set.
329 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
333 unsigned int dev_type = 0;
334 unsigned int dev_port_prev = ~0u;
335 char match[IF_NAMESIZE] = "";
338 MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
344 while ((dent = readdir(dir)) != NULL) {
345 char *name = dent->d_name;
347 unsigned int dev_port;
350 if ((name[0] == '.') &&
351 ((name[1] == '\0') ||
352 ((name[1] == '.') && (name[2] == '\0'))))
355 MKSTR(path, "%s/device/net/%s/%s",
356 priv->ctx->device->ibdev_path, name,
357 (dev_type ? "dev_id" : "dev_port"));
359 file = fopen(path, "rb");
364 * Switch to dev_id when dev_port does not exist as
365 * is the case with Linux kernel versions < 3.15.
376 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
381 * Switch to dev_id when dev_port returns the same value for
382 * all ports. May happen when using a MOFED release older than
383 * 3.0 with a Linux kernel >= 3.15.
385 if (dev_port == dev_port_prev)
387 dev_port_prev = dev_port;
388 if (dev_port == (priv->port - 1u))
389 snprintf(match, sizeof(match), "%s", name);
392 if (match[0] == '\0')
394 strncpy(*ifname, match, sizeof(*ifname));
399 * Read from sysfs entry.
402 * Pointer to private structure.
404 * Entry name relative to sysfs path.
406 * Data output buffer.
411 * 0 on success, -1 on failure and errno is set.
414 priv_sysfs_read(const struct priv *priv, const char *entry,
415 char *buf, size_t size)
417 char ifname[IF_NAMESIZE];
422 if (priv_get_ifname(priv, &ifname))
425 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
428 file = fopen(path, "rb");
431 ret = fread(buf, 1, size, file);
433 if (((size_t)ret < size) && (ferror(file)))
443 * Write to sysfs entry.
446 * Pointer to private structure.
448 * Entry name relative to sysfs path.
455 * 0 on success, -1 on failure and errno is set.
458 priv_sysfs_write(const struct priv *priv, const char *entry,
459 char *buf, size_t size)
461 char ifname[IF_NAMESIZE];
466 if (priv_get_ifname(priv, &ifname))
469 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
472 file = fopen(path, "wb");
475 ret = fwrite(buf, 1, size, file);
477 if (((size_t)ret < size) || (ferror(file)))
487 * Get unsigned long sysfs property.
490 * Pointer to private structure.
492 * Entry name relative to sysfs path.
494 * Value output buffer.
497 * 0 on success, -1 on failure and errno is set.
500 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
503 unsigned long value_ret;
506 ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
508 DEBUG("cannot read %s value from sysfs: %s",
509 name, strerror(errno));
512 value_str[ret] = '\0';
514 value_ret = strtoul(value_str, NULL, 0);
516 DEBUG("invalid %s value `%s': %s", name, value_str,
525 * Set unsigned long sysfs property.
528 * Pointer to private structure.
530 * Entry name relative to sysfs path.
535 * 0 on success, -1 on failure and errno is set.
538 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
541 MKSTR(value_str, "%lu", value);
543 ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
545 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
546 name, value_str, value, strerror(errno));
553 * Perform ifreq ioctl() on associated Ethernet device.
556 * Pointer to private structure.
558 * Request number to pass to ioctl().
560 * Interface request structure output buffer.
563 * 0 on success, -1 on failure and errno is set.
566 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
568 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
573 if (priv_get_ifname(priv, &ifr->ifr_name) == 0)
574 ret = ioctl(sock, req, ifr);
583 * Pointer to private structure.
585 * MTU value output buffer.
588 * 0 on success, -1 on failure and errno is set.
591 priv_get_mtu(struct priv *priv, uint16_t *mtu)
593 unsigned long ulong_mtu;
595 if (priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu) == -1)
605 * Pointer to private structure.
610 * 0 on success, -1 on failure and errno is set.
613 priv_set_mtu(struct priv *priv, uint16_t mtu)
615 return priv_set_sysfs_ulong(priv, "mtu", mtu);
622 * Pointer to private structure.
624 * Bitmask for flags that must remain untouched.
626 * Bitmask for flags to modify.
629 * 0 on success, -1 on failure and errno is set.
632 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
636 if (priv_get_sysfs_ulong(priv, "flags", &tmp) == -1)
640 return priv_set_sysfs_ulong(priv, "flags", tmp);
643 /* Device configuration. */
646 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
647 unsigned int socket, const struct rte_eth_rxconf *conf,
648 struct rte_mempool *mp);
651 rxq_cleanup(struct rxq *rxq);
654 * Ethernet device configuration.
656 * Prepare the driver for a given number of TX and RX queues.
657 * Allocate parent RSS queue when several RX queues are requested.
660 * Pointer to Ethernet device structure.
663 * 0 on success, errno value on failure.
666 dev_configure(struct rte_eth_dev *dev)
668 struct priv *priv = dev->data->dev_private;
669 unsigned int rxqs_n = dev->data->nb_rx_queues;
670 unsigned int txqs_n = dev->data->nb_tx_queues;
674 priv->rxqs = (void *)dev->data->rx_queues;
675 priv->txqs = (void *)dev->data->tx_queues;
676 if (txqs_n != priv->txqs_n) {
677 INFO("%p: TX queues number update: %u -> %u",
678 (void *)dev, priv->txqs_n, txqs_n);
679 priv->txqs_n = txqs_n;
681 if (rxqs_n == priv->rxqs_n)
683 INFO("%p: RX queues number update: %u -> %u",
684 (void *)dev, priv->rxqs_n, rxqs_n);
685 /* If RSS is enabled, disable it first. */
689 /* Only if there are no remaining child RX queues. */
690 for (i = 0; (i != priv->rxqs_n); ++i)
691 if ((*priv->rxqs)[i] != NULL)
693 rxq_cleanup(&priv->rxq_parent);
698 /* Nothing else to do. */
699 priv->rxqs_n = rxqs_n;
702 /* Allocate a new RSS parent queue if supported by hardware. */
704 ERROR("%p: only a single RX queue can be configured when"
705 " hardware doesn't support RSS",
709 /* Fail if hardware doesn't support that many RSS queues. */
710 if (rxqs_n >= priv->max_rss_tbl_sz) {
711 ERROR("%p: only %u RX queues can be configured for RSS",
712 (void *)dev, priv->max_rss_tbl_sz);
717 priv->rxqs_n = rxqs_n;
718 ret = rxq_setup(dev, &priv->rxq_parent, 0, 0, NULL, NULL);
721 /* Failure, rollback. */
729 * DPDK callback for Ethernet device configuration.
732 * Pointer to Ethernet device structure.
735 * 0 on success, negative errno value on failure.
738 mlx4_dev_configure(struct rte_eth_dev *dev)
740 struct priv *priv = dev->data->dev_private;
744 ret = dev_configure(dev);
750 /* TX queues handling. */
753 * Allocate TX queue elements.
756 * Pointer to TX queue structure.
758 * Number of elements to allocate.
761 * 0 on success, errno value on failure.
764 txq_alloc_elts(struct txq *txq, unsigned int elts_n)
767 struct txq_elt (*elts)[elts_n] =
768 rte_calloc_socket("TXQ", 1, sizeof(*elts), 0, txq->socket);
769 linear_t (*elts_linear)[elts_n] =
770 rte_calloc_socket("TXQ", 1, sizeof(*elts_linear), 0,
772 struct ibv_mr *mr_linear = NULL;
775 if ((elts == NULL) || (elts_linear == NULL)) {
776 ERROR("%p: can't allocate packets array", (void *)txq);
781 ibv_reg_mr(txq->priv->pd, elts_linear, sizeof(*elts_linear),
782 (IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE));
783 if (mr_linear == NULL) {
784 ERROR("%p: unable to configure MR, ibv_reg_mr() failed",
789 for (i = 0; (i != elts_n); ++i) {
790 struct txq_elt *elt = &(*elts)[i];
794 DEBUG("%p: allocated and configured %u WRs", (void *)txq, elts_n);
795 txq->elts_n = elts_n;
800 /* Request send completion every MLX4_PMD_TX_PER_COMP_REQ packets or
801 * at least 4 times per ring. */
802 txq->elts_comp_cd_init =
803 ((MLX4_PMD_TX_PER_COMP_REQ < (elts_n / 4)) ?
804 MLX4_PMD_TX_PER_COMP_REQ : (elts_n / 4));
805 txq->elts_comp_cd = txq->elts_comp_cd_init;
806 txq->elts_linear = elts_linear;
807 txq->mr_linear = mr_linear;
811 if (mr_linear != NULL)
812 claim_zero(ibv_dereg_mr(mr_linear));
814 rte_free(elts_linear);
817 DEBUG("%p: failed, freed everything", (void *)txq);
823 * Free TX queue elements.
826 * Pointer to TX queue structure.
829 txq_free_elts(struct txq *txq)
832 unsigned int elts_n = txq->elts_n;
833 struct txq_elt (*elts)[elts_n] = txq->elts;
834 linear_t (*elts_linear)[elts_n] = txq->elts_linear;
835 struct ibv_mr *mr_linear = txq->mr_linear;
837 DEBUG("%p: freeing WRs", (void *)txq);
840 txq->elts_linear = NULL;
841 txq->mr_linear = NULL;
842 if (mr_linear != NULL)
843 claim_zero(ibv_dereg_mr(mr_linear));
845 rte_free(elts_linear);
848 for (i = 0; (i != elemof(*elts)); ++i) {
849 struct txq_elt *elt = &(*elts)[i];
851 if (elt->buf == NULL)
853 rte_pktmbuf_free(elt->buf);
860 * Clean up a TX queue.
862 * Destroy objects, free allocated memory and reset the structure for reuse.
865 * Pointer to TX queue structure.
868 txq_cleanup(struct txq *txq)
870 struct ibv_exp_release_intf_params params;
873 DEBUG("cleaning up %p", (void *)txq);
875 if (txq->if_qp != NULL) {
876 assert(txq->priv != NULL);
877 assert(txq->priv->ctx != NULL);
878 assert(txq->qp != NULL);
879 params = (struct ibv_exp_release_intf_params){
882 claim_zero(ibv_exp_release_intf(txq->priv->ctx,
886 if (txq->if_cq != NULL) {
887 assert(txq->priv != NULL);
888 assert(txq->priv->ctx != NULL);
889 assert(txq->cq != NULL);
890 params = (struct ibv_exp_release_intf_params){
893 claim_zero(ibv_exp_release_intf(txq->priv->ctx,
898 claim_zero(ibv_destroy_qp(txq->qp));
900 claim_zero(ibv_destroy_cq(txq->cq));
901 for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
902 if (txq->mp2mr[i].mp == NULL)
904 assert(txq->mp2mr[i].mr != NULL);
905 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
907 memset(txq, 0, sizeof(*txq));
911 * Manage TX completions.
913 * When sending a burst, mlx4_tx_burst() posts several WRs.
914 * To improve performance, a completion event is only required once every
915 * MLX4_PMD_TX_PER_COMP_REQ sends. Doing so discards completion information
916 * for other WRs, but this information would not be used anyway.
919 * Pointer to TX queue structure.
922 * 0 on success, -1 on failure.
925 txq_complete(struct txq *txq)
927 unsigned int elts_comp = txq->elts_comp;
928 unsigned int elts_tail = txq->elts_tail;
929 const unsigned int elts_n = txq->elts_n;
932 if (unlikely(elts_comp == 0))
935 DEBUG("%p: processing %u work requests completions",
936 (void *)txq, elts_comp);
938 wcs_n = txq->if_cq->poll_cnt(txq->cq, elts_comp);
939 if (unlikely(wcs_n == 0))
941 if (unlikely(wcs_n < 0)) {
942 DEBUG("%p: ibv_poll_cq() failed (wcs_n=%d)",
947 assert(elts_comp <= txq->elts_comp);
949 * Assume WC status is successful as nothing can be done about it
952 elts_tail += wcs_n * txq->elts_comp_cd_init;
953 if (elts_tail >= elts_n)
955 txq->elts_tail = elts_tail;
956 txq->elts_comp = elts_comp;
961 * Get Memory Region (MR) <-> Memory Pool (MP) association from txq->mp2mr[].
962 * Add MP to txq->mp2mr[] if it's not registered yet. If mp2mr[] is full,
963 * remove an entry first.
966 * Pointer to TX queue structure.
968 * Memory Pool for which a Memory Region lkey must be returned.
971 * mr->lkey on success, (uint32_t)-1 on failure.
974 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
979 for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
980 if (unlikely(txq->mp2mr[i].mp == NULL)) {
981 /* Unknown MP, add a new MR for it. */
984 if (txq->mp2mr[i].mp == mp) {
985 assert(txq->mp2mr[i].lkey != (uint32_t)-1);
986 assert(txq->mp2mr[i].mr->lkey == txq->mp2mr[i].lkey);
987 return txq->mp2mr[i].lkey;
990 /* Add a new entry, register MR first. */
991 DEBUG("%p: discovered new memory pool %p", (void *)txq, (void *)mp);
992 mr = ibv_reg_mr(txq->priv->pd,
993 (void *)mp->elt_va_start,
994 (mp->elt_va_end - mp->elt_va_start),
995 (IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE));
996 if (unlikely(mr == NULL)) {
997 DEBUG("%p: unable to configure MR, ibv_reg_mr() failed.",
1001 if (unlikely(i == elemof(txq->mp2mr))) {
1002 /* Table is full, remove oldest entry. */
1003 DEBUG("%p: MR <-> MP table full, dropping oldest entry.",
1006 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
1007 memmove(&txq->mp2mr[0], &txq->mp2mr[1],
1008 (sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
1010 /* Store the new entry. */
1011 txq->mp2mr[i].mp = mp;
1012 txq->mp2mr[i].mr = mr;
1013 txq->mp2mr[i].lkey = mr->lkey;
1014 DEBUG("%p: new MR lkey for MP %p: 0x%08" PRIu32,
1015 (void *)txq, (void *)mp, txq->mp2mr[i].lkey);
1016 return txq->mp2mr[i].lkey;
1019 #if MLX4_PMD_SGE_WR_N > 1
1022 * Copy scattered mbuf contents to a single linear buffer.
1024 * @param[out] linear
1025 * Linear output buffer.
1027 * Scattered input buffer.
1030 * Number of bytes copied to the output buffer or 0 if not large enough.
1033 linearize_mbuf(linear_t *linear, struct rte_mbuf *buf)
1035 unsigned int size = 0;
1036 unsigned int offset;
1039 unsigned int len = DATA_LEN(buf);
1043 if (unlikely(size > sizeof(*linear)))
1045 memcpy(&(*linear)[offset],
1046 rte_pktmbuf_mtod(buf, uint8_t *),
1049 } while (buf != NULL);
1054 * Handle scattered buffers for mlx4_tx_burst().
1057 * TX queue structure.
1059 * Number of segments in buf.
1061 * TX queue element to fill.
1063 * Buffer to process.
1065 * Index of the linear buffer to use if necessary (normally txq->elts_head).
1067 * Array filled with SGEs on success.
1070 * A structure containing the processed packet size in bytes and the
1071 * number of SGEs. Both fields are set to (unsigned int)-1 in case of
1074 static struct tx_burst_sg_ret {
1075 unsigned int length;
1078 tx_burst_sg(struct txq *txq, unsigned int segs, struct txq_elt *elt,
1079 struct rte_mbuf *buf, unsigned int elts_head,
1080 struct ibv_sge (*sges)[MLX4_PMD_SGE_WR_N])
1082 unsigned int sent_size = 0;
1086 /* When there are too many segments, extra segments are
1087 * linearized in the last SGE. */
1088 if (unlikely(segs > elemof(*sges))) {
1089 segs = (elemof(*sges) - 1);
1092 /* Update element. */
1094 /* Register segments as SGEs. */
1095 for (j = 0; (j != segs); ++j) {
1096 struct ibv_sge *sge = &(*sges)[j];
1099 /* Retrieve Memory Region key for this memory pool. */
1100 lkey = txq_mp2mr(txq, buf->pool);
1101 if (unlikely(lkey == (uint32_t)-1)) {
1102 /* MR does not exist. */
1103 DEBUG("%p: unable to get MP <-> MR association",
1105 /* Clean up TX element. */
1110 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
1112 rte_prefetch0((volatile void *)
1113 (uintptr_t)sge->addr);
1114 sge->length = DATA_LEN(buf);
1116 sent_size += sge->length;
1119 /* If buf is not NULL here and is not going to be linearized,
1120 * nb_segs is not valid. */
1122 assert((buf == NULL) || (linearize));
1123 /* Linearize extra segments. */
1125 struct ibv_sge *sge = &(*sges)[segs];
1126 linear_t *linear = &(*txq->elts_linear)[elts_head];
1127 unsigned int size = linearize_mbuf(linear, buf);
1129 assert(segs == (elemof(*sges) - 1));
1131 /* Invalid packet. */
1132 DEBUG("%p: packet too large to be linearized.",
1134 /* Clean up TX element. */
1138 /* If MLX4_PMD_SGE_WR_N is 1, free mbuf immediately. */
1139 if (elemof(*sges) == 1) {
1141 struct rte_mbuf *next = NEXT(buf);
1143 rte_pktmbuf_free_seg(buf);
1145 } while (buf != NULL);
1149 sge->addr = (uintptr_t)&(*linear)[0];
1151 sge->lkey = txq->mr_linear->lkey;
1154 return (struct tx_burst_sg_ret){
1155 .length = sent_size,
1159 return (struct tx_burst_sg_ret){
1165 #endif /* MLX4_PMD_SGE_WR_N > 1 */
1168 * DPDK callback for TX.
1171 * Generic pointer to TX queue structure.
1173 * Packets to transmit.
1175 * Number of packets in array.
1178 * Number of packets successfully transmitted (<= pkts_n).
1181 mlx4_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
1183 struct txq *txq = (struct txq *)dpdk_txq;
1184 unsigned int elts_head = txq->elts_head;
1185 const unsigned int elts_tail = txq->elts_tail;
1186 const unsigned int elts_n = txq->elts_n;
1187 unsigned int elts_comp_cd = txq->elts_comp_cd;
1188 unsigned int elts_comp = 0;
1193 assert(elts_comp_cd != 0);
1195 max = (elts_n - (elts_head - elts_tail));
1199 assert(max <= elts_n);
1200 /* Always leave one free entry in the ring. */
1206 for (i = 0; (i != max); ++i) {
1207 struct rte_mbuf *buf = pkts[i];
1208 struct txq_elt *elt = &(*txq->elts)[elts_head];
1209 unsigned int segs = NB_SEGS(buf);
1210 #ifdef MLX4_PMD_SOFT_COUNTERS
1211 unsigned int sent_size = 0;
1213 uint32_t send_flags = 0;
1215 /* Clean up old buffer. */
1216 if (likely(elt->buf != NULL)) {
1217 struct rte_mbuf *tmp = elt->buf;
1219 /* Faster than rte_pktmbuf_free(). */
1221 struct rte_mbuf *next = NEXT(tmp);
1223 rte_pktmbuf_free_seg(tmp);
1225 } while (tmp != NULL);
1227 /* Request TX completion. */
1228 if (unlikely(--elts_comp_cd == 0)) {
1229 elts_comp_cd = txq->elts_comp_cd_init;
1231 send_flags |= IBV_EXP_QP_BURST_SIGNALED;
1233 if (likely(segs == 1)) {
1238 /* Retrieve buffer information. */
1239 addr = rte_pktmbuf_mtod(buf, uintptr_t);
1240 length = DATA_LEN(buf);
1241 /* Retrieve Memory Region key for this memory pool. */
1242 lkey = txq_mp2mr(txq, buf->pool);
1243 if (unlikely(lkey == (uint32_t)-1)) {
1244 /* MR does not exist. */
1245 DEBUG("%p: unable to get MP <-> MR"
1246 " association", (void *)txq);
1247 /* Clean up TX element. */
1251 /* Update element. */
1254 rte_prefetch0((volatile void *)
1256 /* Put packet into send queue. */
1257 #if MLX4_PMD_MAX_INLINE > 0
1258 if (length <= txq->max_inline)
1259 err = txq->if_qp->send_pending_inline
1266 err = txq->if_qp->send_pending
1274 #ifdef MLX4_PMD_SOFT_COUNTERS
1275 sent_size += length;
1278 #if MLX4_PMD_SGE_WR_N > 1
1279 struct ibv_sge sges[MLX4_PMD_SGE_WR_N];
1280 struct tx_burst_sg_ret ret;
1282 ret = tx_burst_sg(txq, segs, elt, buf, elts_head,
1284 if (ret.length == (unsigned int)-1)
1286 /* Put SG list into send queue. */
1287 err = txq->if_qp->send_pending_sg_list
1294 #ifdef MLX4_PMD_SOFT_COUNTERS
1295 sent_size += ret.length;
1297 #else /* MLX4_PMD_SGE_WR_N > 1 */
1298 DEBUG("%p: TX scattered buffers support not"
1299 " compiled in", (void *)txq);
1301 #endif /* MLX4_PMD_SGE_WR_N > 1 */
1303 if (++elts_head >= elts_n)
1305 #ifdef MLX4_PMD_SOFT_COUNTERS
1306 /* Increment sent bytes counter. */
1307 txq->stats.obytes += sent_size;
1311 /* Take a shortcut if nothing must be sent. */
1312 if (unlikely(i == 0))
1314 #ifdef MLX4_PMD_SOFT_COUNTERS
1315 /* Increment sent packets counter. */
1316 txq->stats.opackets += i;
1318 /* Ring QP doorbell. */
1319 err = txq->if_qp->send_flush(txq->qp);
1320 if (unlikely(err)) {
1321 /* A nonzero value is not supposed to be returned.
1322 * Nothing can be done about it. */
1323 DEBUG("%p: send_flush() failed with error %d",
1326 txq->elts_head = elts_head;
1327 txq->elts_comp += elts_comp;
1328 txq->elts_comp_cd = elts_comp_cd;
1333 * Configure a TX queue.
1336 * Pointer to Ethernet device structure.
1338 * Pointer to TX queue structure.
1340 * Number of descriptors to configure in queue.
1342 * NUMA socket on which memory must be allocated.
1344 * Thresholds parameters.
1347 * 0 on success, errno value on failure.
1350 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
1351 unsigned int socket, const struct rte_eth_txconf *conf)
1353 struct priv *priv = dev->data->dev_private;
1359 struct ibv_exp_query_intf_params params;
1360 struct ibv_qp_init_attr init;
1361 struct ibv_exp_qp_attr mod;
1363 enum ibv_exp_query_intf_status status;
1366 (void)conf; /* Thresholds configuration (ignored). */
1367 if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
1368 ERROR("%p: invalid number of TX descriptors (must be a"
1369 " multiple of %d)", (void *)dev, MLX4_PMD_SGE_WR_N);
1372 desc /= MLX4_PMD_SGE_WR_N;
1373 /* MRs will be registered in mp2mr[] later. */
1374 tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
1375 if (tmpl.cq == NULL) {
1377 ERROR("%p: CQ creation failure: %s",
1378 (void *)dev, strerror(ret));
1381 DEBUG("priv->device_attr.max_qp_wr is %d",
1382 priv->device_attr.max_qp_wr);
1383 DEBUG("priv->device_attr.max_sge is %d",
1384 priv->device_attr.max_sge);
1385 attr.init = (struct ibv_qp_init_attr){
1386 /* CQ to be associated with the send queue. */
1388 /* CQ to be associated with the receive queue. */
1391 /* Max number of outstanding WRs. */
1392 .max_send_wr = ((priv->device_attr.max_qp_wr < desc) ?
1393 priv->device_attr.max_qp_wr :
1395 /* Max number of scatter/gather elements in a WR. */
1396 .max_send_sge = ((priv->device_attr.max_sge <
1397 MLX4_PMD_SGE_WR_N) ?
1398 priv->device_attr.max_sge :
1400 #if MLX4_PMD_MAX_INLINE > 0
1401 .max_inline_data = MLX4_PMD_MAX_INLINE,
1404 .qp_type = IBV_QPT_RAW_PACKET,
1405 /* Do *NOT* enable this, completions events are managed per
1409 tmpl.qp = ibv_create_qp(priv->pd, &attr.init);
1410 if (tmpl.qp == NULL) {
1411 ret = (errno ? errno : EINVAL);
1412 ERROR("%p: QP creation failure: %s",
1413 (void *)dev, strerror(ret));
1416 #if MLX4_PMD_MAX_INLINE > 0
1417 /* ibv_create_qp() updates this value. */
1418 tmpl.max_inline = attr.init.cap.max_inline_data;
1420 attr.mod = (struct ibv_exp_qp_attr){
1421 /* Move the QP to this state. */
1422 .qp_state = IBV_QPS_INIT,
1423 /* Primary port number. */
1424 .port_num = priv->port
1426 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod,
1427 (IBV_EXP_QP_STATE | IBV_EXP_QP_PORT));
1429 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
1430 (void *)dev, strerror(ret));
1433 ret = txq_alloc_elts(&tmpl, desc);
1435 ERROR("%p: TXQ allocation failed: %s",
1436 (void *)dev, strerror(ret));
1439 attr.mod = (struct ibv_exp_qp_attr){
1440 .qp_state = IBV_QPS_RTR
1442 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1444 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
1445 (void *)dev, strerror(ret));
1448 attr.mod.qp_state = IBV_QPS_RTS;
1449 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1451 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
1452 (void *)dev, strerror(ret));
1455 attr.params = (struct ibv_exp_query_intf_params){
1456 .intf_scope = IBV_EXP_INTF_GLOBAL,
1457 .intf = IBV_EXP_INTF_CQ,
1460 tmpl.if_cq = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
1461 if (tmpl.if_cq == NULL) {
1462 ERROR("%p: CQ interface family query failed with status %d",
1463 (void *)dev, status);
1466 attr.params = (struct ibv_exp_query_intf_params){
1467 .intf_scope = IBV_EXP_INTF_GLOBAL,
1468 .intf = IBV_EXP_INTF_QP_BURST,
1471 tmpl.if_qp = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
1472 if (tmpl.if_qp == NULL) {
1473 ERROR("%p: QP interface family query failed with status %d",
1474 (void *)dev, status);
1477 /* Clean up txq in case we're reinitializing it. */
1478 DEBUG("%p: cleaning-up old txq just in case", (void *)txq);
1481 DEBUG("%p: txq updated with %p", (void *)txq, (void *)&tmpl);
1491 * DPDK callback to configure a TX queue.
1494 * Pointer to Ethernet device structure.
1498 * Number of descriptors to configure in queue.
1500 * NUMA socket on which memory must be allocated.
1502 * Thresholds parameters.
1505 * 0 on success, negative errno value on failure.
1508 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1509 unsigned int socket, const struct rte_eth_txconf *conf)
1511 struct priv *priv = dev->data->dev_private;
1512 struct txq *txq = (*priv->txqs)[idx];
1516 DEBUG("%p: configuring queue %u for %u descriptors",
1517 (void *)dev, idx, desc);
1518 if (idx >= priv->txqs_n) {
1519 ERROR("%p: queue index out of range (%u >= %u)",
1520 (void *)dev, idx, priv->txqs_n);
1525 DEBUG("%p: reusing already allocated queue index %u (%p)",
1526 (void *)dev, idx, (void *)txq);
1527 if (priv->started) {
1531 (*priv->txqs)[idx] = NULL;
1534 txq = rte_calloc_socket("TXQ", 1, sizeof(*txq), 0, socket);
1536 ERROR("%p: unable to allocate queue index %u",
1542 ret = txq_setup(dev, txq, desc, socket, conf);
1546 txq->stats.idx = idx;
1547 DEBUG("%p: adding TX queue %p to list",
1548 (void *)dev, (void *)txq);
1549 (*priv->txqs)[idx] = txq;
1550 /* Update send callback. */
1551 dev->tx_pkt_burst = mlx4_tx_burst;
1558 * DPDK callback to release a TX queue.
1561 * Generic TX queue pointer.
1564 mlx4_tx_queue_release(void *dpdk_txq)
1566 struct txq *txq = (struct txq *)dpdk_txq;
1574 for (i = 0; (i != priv->txqs_n); ++i)
1575 if ((*priv->txqs)[i] == txq) {
1576 DEBUG("%p: removing TX queue %p from list",
1577 (void *)priv->dev, (void *)txq);
1578 (*priv->txqs)[i] = NULL;
1586 /* RX queues handling. */
1589 * Allocate RX queue elements with scattered packets support.
1592 * Pointer to RX queue structure.
1594 * Number of elements to allocate.
1596 * If not NULL, fetch buffers from this array instead of allocating them
1597 * with rte_pktmbuf_alloc().
1600 * 0 on success, errno value on failure.
1603 rxq_alloc_elts_sp(struct rxq *rxq, unsigned int elts_n,
1604 struct rte_mbuf **pool)
1607 struct rxq_elt_sp (*elts)[elts_n] =
1608 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1613 ERROR("%p: can't allocate packets array", (void *)rxq);
1617 /* For each WR (packet). */
1618 for (i = 0; (i != elts_n); ++i) {
1620 struct rxq_elt_sp *elt = &(*elts)[i];
1621 struct ibv_recv_wr *wr = &elt->wr;
1622 struct ibv_sge (*sges)[(elemof(elt->sges))] = &elt->sges;
1624 /* These two arrays must have the same size. */
1625 assert(elemof(elt->sges) == elemof(elt->bufs));
1628 wr->next = &(*elts)[(i + 1)].wr;
1629 wr->sg_list = &(*sges)[0];
1630 wr->num_sge = elemof(*sges);
1631 /* For each SGE (segment). */
1632 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1633 struct ibv_sge *sge = &(*sges)[j];
1634 struct rte_mbuf *buf;
1638 assert(buf != NULL);
1639 rte_pktmbuf_reset(buf);
1641 buf = rte_pktmbuf_alloc(rxq->mp);
1643 assert(pool == NULL);
1644 ERROR("%p: empty mbuf pool", (void *)rxq);
1649 /* Headroom is reserved by rte_pktmbuf_alloc(). */
1650 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1651 /* Buffer is supposed to be empty. */
1652 assert(rte_pktmbuf_data_len(buf) == 0);
1653 assert(rte_pktmbuf_pkt_len(buf) == 0);
1654 /* sge->addr must be able to store a pointer. */
1655 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1657 /* The first SGE keeps its headroom. */
1658 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
1659 sge->length = (buf->buf_len -
1660 RTE_PKTMBUF_HEADROOM);
1662 /* Subsequent SGEs lose theirs. */
1663 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1664 SET_DATA_OFF(buf, 0);
1665 sge->addr = (uintptr_t)buf->buf_addr;
1666 sge->length = buf->buf_len;
1668 sge->lkey = rxq->mr->lkey;
1669 /* Redundant check for tailroom. */
1670 assert(sge->length == rte_pktmbuf_tailroom(buf));
1673 /* The last WR pointer must be NULL. */
1674 (*elts)[(i - 1)].wr.next = NULL;
1675 DEBUG("%p: allocated and configured %u WRs (%zu segments)",
1676 (void *)rxq, elts_n, (elts_n * elemof((*elts)[0].sges)));
1677 rxq->elts_n = elts_n;
1679 rxq->elts.sp = elts;
1684 assert(pool == NULL);
1685 for (i = 0; (i != elemof(*elts)); ++i) {
1687 struct rxq_elt_sp *elt = &(*elts)[i];
1689 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1690 struct rte_mbuf *buf = elt->bufs[j];
1693 rte_pktmbuf_free_seg(buf);
1698 DEBUG("%p: failed, freed everything", (void *)rxq);
1704 * Free RX queue elements with scattered packets support.
1707 * Pointer to RX queue structure.
1710 rxq_free_elts_sp(struct rxq *rxq)
1713 unsigned int elts_n = rxq->elts_n;
1714 struct rxq_elt_sp (*elts)[elts_n] = rxq->elts.sp;
1716 DEBUG("%p: freeing WRs", (void *)rxq);
1718 rxq->elts.sp = NULL;
1721 for (i = 0; (i != elemof(*elts)); ++i) {
1723 struct rxq_elt_sp *elt = &(*elts)[i];
1725 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1726 struct rte_mbuf *buf = elt->bufs[j];
1729 rte_pktmbuf_free_seg(buf);
1736 * Allocate RX queue elements.
1739 * Pointer to RX queue structure.
1741 * Number of elements to allocate.
1743 * If not NULL, fetch buffers from this array instead of allocating them
1744 * with rte_pktmbuf_alloc().
1747 * 0 on success, errno value on failure.
1750 rxq_alloc_elts(struct rxq *rxq, unsigned int elts_n, struct rte_mbuf **pool)
1753 struct rxq_elt (*elts)[elts_n] =
1754 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1759 ERROR("%p: can't allocate packets array", (void *)rxq);
1763 /* For each WR (packet). */
1764 for (i = 0; (i != elts_n); ++i) {
1765 struct rxq_elt *elt = &(*elts)[i];
1766 struct ibv_recv_wr *wr = &elt->wr;
1767 struct ibv_sge *sge = &(*elts)[i].sge;
1768 struct rte_mbuf *buf;
1772 assert(buf != NULL);
1773 rte_pktmbuf_reset(buf);
1775 buf = rte_pktmbuf_alloc(rxq->mp);
1777 assert(pool == NULL);
1778 ERROR("%p: empty mbuf pool", (void *)rxq);
1782 /* Configure WR. Work request ID contains its own index in
1783 * the elts array and the offset between SGE buffer header and
1785 WR_ID(wr->wr_id).id = i;
1786 WR_ID(wr->wr_id).offset =
1787 (((uintptr_t)buf->buf_addr + RTE_PKTMBUF_HEADROOM) -
1789 wr->next = &(*elts)[(i + 1)].wr;
1792 /* Headroom is reserved by rte_pktmbuf_alloc(). */
1793 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1794 /* Buffer is supposed to be empty. */
1795 assert(rte_pktmbuf_data_len(buf) == 0);
1796 assert(rte_pktmbuf_pkt_len(buf) == 0);
1797 /* sge->addr must be able to store a pointer. */
1798 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1799 /* SGE keeps its headroom. */
1800 sge->addr = (uintptr_t)
1801 ((uint8_t *)buf->buf_addr + RTE_PKTMBUF_HEADROOM);
1802 sge->length = (buf->buf_len - RTE_PKTMBUF_HEADROOM);
1803 sge->lkey = rxq->mr->lkey;
1804 /* Redundant check for tailroom. */
1805 assert(sge->length == rte_pktmbuf_tailroom(buf));
1806 /* Make sure elts index and SGE mbuf pointer can be deduced
1808 if ((WR_ID(wr->wr_id).id != i) ||
1809 ((void *)((uintptr_t)sge->addr -
1810 WR_ID(wr->wr_id).offset) != buf)) {
1811 ERROR("%p: cannot store index and offset in WR ID",
1814 rte_pktmbuf_free(buf);
1819 /* The last WR pointer must be NULL. */
1820 (*elts)[(i - 1)].wr.next = NULL;
1821 DEBUG("%p: allocated and configured %u single-segment WRs",
1822 (void *)rxq, elts_n);
1823 rxq->elts_n = elts_n;
1825 rxq->elts.no_sp = elts;
1830 assert(pool == NULL);
1831 for (i = 0; (i != elemof(*elts)); ++i) {
1832 struct rxq_elt *elt = &(*elts)[i];
1833 struct rte_mbuf *buf;
1835 if (elt->sge.addr == 0)
1837 assert(WR_ID(elt->wr.wr_id).id == i);
1838 buf = (void *)((uintptr_t)elt->sge.addr -
1839 WR_ID(elt->wr.wr_id).offset);
1840 rte_pktmbuf_free_seg(buf);
1844 DEBUG("%p: failed, freed everything", (void *)rxq);
1850 * Free RX queue elements.
1853 * Pointer to RX queue structure.
1856 rxq_free_elts(struct rxq *rxq)
1859 unsigned int elts_n = rxq->elts_n;
1860 struct rxq_elt (*elts)[elts_n] = rxq->elts.no_sp;
1862 DEBUG("%p: freeing WRs", (void *)rxq);
1864 rxq->elts.no_sp = NULL;
1867 for (i = 0; (i != elemof(*elts)); ++i) {
1868 struct rxq_elt *elt = &(*elts)[i];
1869 struct rte_mbuf *buf;
1871 if (elt->sge.addr == 0)
1873 assert(WR_ID(elt->wr.wr_id).id == i);
1874 buf = (void *)((uintptr_t)elt->sge.addr -
1875 WR_ID(elt->wr.wr_id).offset);
1876 rte_pktmbuf_free_seg(buf);
1882 * Delete flow steering rule.
1885 * Pointer to RX queue structure.
1887 * MAC address index.
1892 rxq_del_flow(struct rxq *rxq, unsigned int mac_index, unsigned int vlan_index)
1895 struct priv *priv = rxq->priv;
1896 const uint8_t (*mac)[ETHER_ADDR_LEN] =
1897 (const uint8_t (*)[ETHER_ADDR_LEN])
1898 priv->mac[mac_index].addr_bytes;
1900 assert(rxq->mac_flow[mac_index][vlan_index] != NULL);
1901 DEBUG("%p: removing MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
1902 " (VLAN ID %" PRIu16 ")",
1904 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
1905 mac_index, priv->vlan_filter[vlan_index].id);
1906 claim_zero(ibv_destroy_flow(rxq->mac_flow[mac_index][vlan_index]));
1907 rxq->mac_flow[mac_index][vlan_index] = NULL;
1911 * Unregister a MAC address from a RX queue.
1914 * Pointer to RX queue structure.
1916 * MAC address index.
1919 rxq_mac_addr_del(struct rxq *rxq, unsigned int mac_index)
1921 struct priv *priv = rxq->priv;
1923 unsigned int vlans = 0;
1925 assert(mac_index < elemof(priv->mac));
1926 if (!BITFIELD_ISSET(rxq->mac_configured, mac_index))
1928 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
1929 if (!priv->vlan_filter[i].enabled)
1931 rxq_del_flow(rxq, mac_index, i);
1935 rxq_del_flow(rxq, mac_index, 0);
1937 BITFIELD_RESET(rxq->mac_configured, mac_index);
1941 * Unregister all MAC addresses from a RX queue.
1944 * Pointer to RX queue structure.
1947 rxq_mac_addrs_del(struct rxq *rxq)
1949 struct priv *priv = rxq->priv;
1952 for (i = 0; (i != elemof(priv->mac)); ++i)
1953 rxq_mac_addr_del(rxq, i);
1956 static int rxq_promiscuous_enable(struct rxq *);
1957 static void rxq_promiscuous_disable(struct rxq *);
1960 * Add single flow steering rule.
1963 * Pointer to RX queue structure.
1965 * MAC address index to register.
1967 * VLAN index. Use -1 for a flow without VLAN.
1970 * 0 on success, errno value on failure.
1973 rxq_add_flow(struct rxq *rxq, unsigned int mac_index, unsigned int vlan_index)
1975 struct ibv_flow *flow;
1976 struct priv *priv = rxq->priv;
1977 const uint8_t (*mac)[ETHER_ADDR_LEN] =
1978 (const uint8_t (*)[ETHER_ADDR_LEN])
1979 priv->mac[mac_index].addr_bytes;
1981 /* Allocate flow specification on the stack. */
1982 struct __attribute__((packed)) {
1983 struct ibv_flow_attr attr;
1984 struct ibv_flow_spec_eth spec;
1986 struct ibv_flow_attr *attr = &data.attr;
1987 struct ibv_flow_spec_eth *spec = &data.spec;
1989 assert(mac_index < elemof(priv->mac));
1990 assert((vlan_index < elemof(priv->vlan_filter)) || (vlan_index == -1u));
1992 * No padding must be inserted by the compiler between attr and spec.
1993 * This layout is expected by libibverbs.
1995 assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec);
1996 *attr = (struct ibv_flow_attr){
1997 .type = IBV_FLOW_ATTR_NORMAL,
2002 *spec = (struct ibv_flow_spec_eth){
2003 .type = IBV_FLOW_SPEC_ETH,
2004 .size = sizeof(*spec),
2007 (*mac)[0], (*mac)[1], (*mac)[2],
2008 (*mac)[3], (*mac)[4], (*mac)[5]
2010 .vlan_tag = ((vlan_index != -1u) ?
2011 htons(priv->vlan_filter[vlan_index].id) :
2015 .dst_mac = "\xff\xff\xff\xff\xff\xff",
2016 .vlan_tag = ((vlan_index != -1u) ? htons(0xfff) : 0),
2019 DEBUG("%p: adding MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
2020 " (VLAN %s %" PRIu16 ")",
2022 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
2024 ((vlan_index != -1u) ? "ID" : "index"),
2025 ((vlan_index != -1u) ? priv->vlan_filter[vlan_index].id : -1u));
2026 /* Create related flow. */
2028 flow = ibv_create_flow(rxq->qp, attr);
2030 /* It's not clear whether errno is always set in this case. */
2031 ERROR("%p: flow configuration failed, errno=%d: %s",
2033 (errno ? strerror(errno) : "Unknown error"));
2038 if (vlan_index == -1u)
2040 assert(rxq->mac_flow[mac_index][vlan_index] == NULL);
2041 rxq->mac_flow[mac_index][vlan_index] = flow;
2046 * Register a MAC address in a RX queue.
2049 * Pointer to RX queue structure.
2051 * MAC address index to register.
2054 * 0 on success, errno value on failure.
2057 rxq_mac_addr_add(struct rxq *rxq, unsigned int mac_index)
2059 struct priv *priv = rxq->priv;
2061 unsigned int vlans = 0;
2064 assert(mac_index < elemof(priv->mac));
2065 if (BITFIELD_ISSET(rxq->mac_configured, mac_index))
2066 rxq_mac_addr_del(rxq, mac_index);
2067 /* Fill VLAN specifications. */
2068 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
2069 if (!priv->vlan_filter[i].enabled)
2071 /* Create related flow. */
2072 ret = rxq_add_flow(rxq, mac_index, i);
2077 /* Failure, rollback. */
2079 if (priv->vlan_filter[--i].enabled)
2080 rxq_del_flow(rxq, mac_index, i);
2084 /* In case there is no VLAN filter. */
2086 ret = rxq_add_flow(rxq, mac_index, -1);
2090 BITFIELD_SET(rxq->mac_configured, mac_index);
2095 * Register all MAC addresses in a RX queue.
2098 * Pointer to RX queue structure.
2101 * 0 on success, errno value on failure.
2104 rxq_mac_addrs_add(struct rxq *rxq)
2106 struct priv *priv = rxq->priv;
2110 for (i = 0; (i != elemof(priv->mac)); ++i) {
2111 if (!BITFIELD_ISSET(priv->mac_configured, i))
2113 ret = rxq_mac_addr_add(rxq, i);
2116 /* Failure, rollback. */
2118 rxq_mac_addr_del(rxq, --i);
2126 * Unregister a MAC address.
2128 * In RSS mode, the MAC address is unregistered from the parent queue,
2129 * otherwise it is unregistered from each queue directly.
2132 * Pointer to private structure.
2134 * MAC address index.
2137 priv_mac_addr_del(struct priv *priv, unsigned int mac_index)
2141 assert(mac_index < elemof(priv->mac));
2142 if (!BITFIELD_ISSET(priv->mac_configured, mac_index))
2145 rxq_mac_addr_del(&priv->rxq_parent, mac_index);
2148 for (i = 0; (i != priv->dev->data->nb_rx_queues); ++i)
2149 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2151 BITFIELD_RESET(priv->mac_configured, mac_index);
2155 * Register a MAC address.
2157 * In RSS mode, the MAC address is registered in the parent queue,
2158 * otherwise it is registered in each queue directly.
2161 * Pointer to private structure.
2163 * MAC address index to use.
2165 * MAC address to register.
2168 * 0 on success, errno value on failure.
2171 priv_mac_addr_add(struct priv *priv, unsigned int mac_index,
2172 const uint8_t (*mac)[ETHER_ADDR_LEN])
2177 assert(mac_index < elemof(priv->mac));
2178 /* First, make sure this address isn't already configured. */
2179 for (i = 0; (i != elemof(priv->mac)); ++i) {
2180 /* Skip this index, it's going to be reconfigured. */
2183 if (!BITFIELD_ISSET(priv->mac_configured, i))
2185 if (memcmp(priv->mac[i].addr_bytes, *mac, sizeof(*mac)))
2187 /* Address already configured elsewhere, return with error. */
2190 if (BITFIELD_ISSET(priv->mac_configured, mac_index))
2191 priv_mac_addr_del(priv, mac_index);
2192 priv->mac[mac_index] = (struct ether_addr){
2194 (*mac)[0], (*mac)[1], (*mac)[2],
2195 (*mac)[3], (*mac)[4], (*mac)[5]
2198 /* If device isn't started, this is all we need to do. */
2199 if (!priv->started) {
2201 /* Verify that all queues have this index disabled. */
2202 for (i = 0; (i != priv->rxqs_n); ++i) {
2203 if ((*priv->rxqs)[i] == NULL)
2205 assert(!BITFIELD_ISSET
2206 ((*priv->rxqs)[i]->mac_configured, mac_index));
2212 ret = rxq_mac_addr_add(&priv->rxq_parent, mac_index);
2217 for (i = 0; (i != priv->rxqs_n); ++i) {
2218 if ((*priv->rxqs)[i] == NULL)
2220 ret = rxq_mac_addr_add((*priv->rxqs)[i], mac_index);
2223 /* Failure, rollback. */
2225 if ((*priv->rxqs)[(--i)] != NULL)
2226 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2230 BITFIELD_SET(priv->mac_configured, mac_index);
2235 * Enable allmulti mode in a RX queue.
2238 * Pointer to RX queue structure.
2241 * 0 on success, errno value on failure.
2244 rxq_allmulticast_enable(struct rxq *rxq)
2246 struct ibv_flow *flow;
2247 struct ibv_flow_attr attr = {
2248 .type = IBV_FLOW_ATTR_MC_DEFAULT,
2250 .port = rxq->priv->port,
2254 DEBUG("%p: enabling allmulticast mode", (void *)rxq);
2255 if (rxq->allmulti_flow != NULL)
2258 flow = ibv_create_flow(rxq->qp, &attr);
2260 /* It's not clear whether errno is always set in this case. */
2261 ERROR("%p: flow configuration failed, errno=%d: %s",
2263 (errno ? strerror(errno) : "Unknown error"));
2268 rxq->allmulti_flow = flow;
2269 DEBUG("%p: allmulticast mode enabled", (void *)rxq);
2274 * Disable allmulti mode in a RX queue.
2277 * Pointer to RX queue structure.
2280 rxq_allmulticast_disable(struct rxq *rxq)
2282 DEBUG("%p: disabling allmulticast mode", (void *)rxq);
2283 if (rxq->allmulti_flow == NULL)
2285 claim_zero(ibv_destroy_flow(rxq->allmulti_flow));
2286 rxq->allmulti_flow = NULL;
2287 DEBUG("%p: allmulticast mode disabled", (void *)rxq);
2291 * Enable promiscuous mode in a RX queue.
2294 * Pointer to RX queue structure.
2297 * 0 on success, errno value on failure.
2300 rxq_promiscuous_enable(struct rxq *rxq)
2302 struct ibv_flow *flow;
2303 struct ibv_flow_attr attr = {
2304 .type = IBV_FLOW_ATTR_ALL_DEFAULT,
2306 .port = rxq->priv->port,
2312 DEBUG("%p: enabling promiscuous mode", (void *)rxq);
2313 if (rxq->promisc_flow != NULL)
2316 flow = ibv_create_flow(rxq->qp, &attr);
2318 /* It's not clear whether errno is always set in this case. */
2319 ERROR("%p: flow configuration failed, errno=%d: %s",
2321 (errno ? strerror(errno) : "Unknown error"));
2326 rxq->promisc_flow = flow;
2327 DEBUG("%p: promiscuous mode enabled", (void *)rxq);
2332 * Disable promiscuous mode in a RX queue.
2335 * Pointer to RX queue structure.
2338 rxq_promiscuous_disable(struct rxq *rxq)
2342 DEBUG("%p: disabling promiscuous mode", (void *)rxq);
2343 if (rxq->promisc_flow == NULL)
2345 claim_zero(ibv_destroy_flow(rxq->promisc_flow));
2346 rxq->promisc_flow = NULL;
2347 DEBUG("%p: promiscuous mode disabled", (void *)rxq);
2351 * Clean up a RX queue.
2353 * Destroy objects, free allocated memory and reset the structure for reuse.
2356 * Pointer to RX queue structure.
2359 rxq_cleanup(struct rxq *rxq)
2361 struct ibv_exp_release_intf_params params;
2363 DEBUG("cleaning up %p", (void *)rxq);
2365 rxq_free_elts_sp(rxq);
2368 if (rxq->if_qp != NULL) {
2369 assert(rxq->priv != NULL);
2370 assert(rxq->priv->ctx != NULL);
2371 assert(rxq->qp != NULL);
2372 params = (struct ibv_exp_release_intf_params){
2375 claim_zero(ibv_exp_release_intf(rxq->priv->ctx,
2379 if (rxq->if_cq != NULL) {
2380 assert(rxq->priv != NULL);
2381 assert(rxq->priv->ctx != NULL);
2382 assert(rxq->cq != NULL);
2383 params = (struct ibv_exp_release_intf_params){
2386 claim_zero(ibv_exp_release_intf(rxq->priv->ctx,
2390 if (rxq->qp != NULL) {
2391 rxq_promiscuous_disable(rxq);
2392 rxq_allmulticast_disable(rxq);
2393 rxq_mac_addrs_del(rxq);
2394 claim_zero(ibv_destroy_qp(rxq->qp));
2396 if (rxq->cq != NULL)
2397 claim_zero(ibv_destroy_cq(rxq->cq));
2398 if (rxq->mr != NULL)
2399 claim_zero(ibv_dereg_mr(rxq->mr));
2400 memset(rxq, 0, sizeof(*rxq));
2404 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n);
2407 * DPDK callback for RX with scattered packets support.
2410 * Generic pointer to RX queue structure.
2412 * Array to store received packets.
2414 * Maximum number of packets in array.
2417 * Number of packets successfully received (<= pkts_n).
2420 mlx4_rx_burst_sp(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2422 struct rxq *rxq = (struct rxq *)dpdk_rxq;
2423 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
2424 const unsigned int elts_n = rxq->elts_n;
2425 unsigned int elts_head = rxq->elts_head;
2426 struct ibv_recv_wr head;
2427 struct ibv_recv_wr **next = &head.next;
2428 struct ibv_recv_wr *bad_wr;
2430 unsigned int pkts_ret = 0;
2433 if (unlikely(!rxq->sp))
2434 return mlx4_rx_burst(dpdk_rxq, pkts, pkts_n);
2435 if (unlikely(elts == NULL)) /* See RTE_DEV_CMD_SET_MTU. */
2437 for (i = 0; (i != pkts_n); ++i) {
2438 struct rxq_elt_sp *elt = &(*elts)[elts_head];
2439 struct ibv_recv_wr *wr = &elt->wr;
2440 uint64_t wr_id = wr->wr_id;
2442 unsigned int pkt_buf_len;
2443 struct rte_mbuf *pkt_buf = NULL; /* Buffer returned in pkts. */
2444 struct rte_mbuf **pkt_buf_next = &pkt_buf;
2445 unsigned int seg_headroom = RTE_PKTMBUF_HEADROOM;
2448 /* Sanity checks. */
2452 assert(wr_id < rxq->elts_n);
2453 assert(wr->sg_list == elt->sges);
2454 assert(wr->num_sge == elemof(elt->sges));
2455 assert(elts_head < rxq->elts_n);
2456 assert(rxq->elts_head < rxq->elts_n);
2457 ret = rxq->if_cq->poll_length(rxq->cq, NULL, NULL);
2458 if (unlikely(ret < 0)) {
2462 DEBUG("rxq=%p, poll_length() failed (ret=%d)",
2464 /* ibv_poll_cq() must be used in case of failure. */
2465 wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
2466 if (unlikely(wcs_n == 0))
2468 if (unlikely(wcs_n < 0)) {
2469 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
2470 (void *)rxq, wcs_n);
2474 if (unlikely(wc.status != IBV_WC_SUCCESS)) {
2475 /* Whatever, just repost the offending WR. */
2476 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
2477 " completion status (%d): %s",
2478 (void *)rxq, wc.wr_id, wc.status,
2479 ibv_wc_status_str(wc.status));
2480 #ifdef MLX4_PMD_SOFT_COUNTERS
2481 /* Increment dropped packets counter. */
2482 ++rxq->stats.idropped;
2484 /* Link completed WRs together for repost. */
2495 /* Link completed WRs together for repost. */
2499 * Replace spent segments with new ones, concatenate and
2500 * return them as pkt_buf.
2503 struct ibv_sge *sge = &elt->sges[j];
2504 struct rte_mbuf *seg = elt->bufs[j];
2505 struct rte_mbuf *rep;
2506 unsigned int seg_tailroom;
2509 * Fetch initial bytes of packet descriptor into a
2510 * cacheline while allocating rep.
2513 rep = __rte_mbuf_raw_alloc(rxq->mp);
2514 if (unlikely(rep == NULL)) {
2516 * Unable to allocate a replacement mbuf,
2519 DEBUG("rxq=%p, wr_id=%" PRIu64 ":"
2520 " can't allocate a new mbuf",
2521 (void *)rxq, wr_id);
2522 if (pkt_buf != NULL) {
2523 *pkt_buf_next = NULL;
2524 rte_pktmbuf_free(pkt_buf);
2526 /* Increase out of memory counters. */
2527 ++rxq->stats.rx_nombuf;
2528 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
2532 /* Poison user-modifiable fields in rep. */
2533 NEXT(rep) = (void *)((uintptr_t)-1);
2534 SET_DATA_OFF(rep, 0xdead);
2535 DATA_LEN(rep) = 0xd00d;
2536 PKT_LEN(rep) = 0xdeadd00d;
2537 NB_SEGS(rep) = 0x2a;
2541 assert(rep->buf_len == seg->buf_len);
2542 assert(rep->buf_len == rxq->mb_len);
2543 /* Reconfigure sge to use rep instead of seg. */
2544 assert(sge->lkey == rxq->mr->lkey);
2545 sge->addr = ((uintptr_t)rep->buf_addr + seg_headroom);
2548 /* Update pkt_buf if it's the first segment, or link
2549 * seg to the previous one and update pkt_buf_next. */
2550 *pkt_buf_next = seg;
2551 pkt_buf_next = &NEXT(seg);
2552 /* Update seg information. */
2553 seg_tailroom = (seg->buf_len - seg_headroom);
2554 assert(sge->length == seg_tailroom);
2555 SET_DATA_OFF(seg, seg_headroom);
2556 if (likely(len <= seg_tailroom)) {
2558 DATA_LEN(seg) = len;
2561 assert(rte_pktmbuf_headroom(seg) ==
2563 assert(rte_pktmbuf_tailroom(seg) ==
2564 (seg_tailroom - len));
2567 DATA_LEN(seg) = seg_tailroom;
2568 PKT_LEN(seg) = seg_tailroom;
2570 assert(rte_pktmbuf_headroom(seg) == seg_headroom);
2571 assert(rte_pktmbuf_tailroom(seg) == 0);
2572 /* Fix len and clear headroom for next segments. */
2573 len -= seg_tailroom;
2576 /* Update head and tail segments. */
2577 *pkt_buf_next = NULL;
2578 assert(pkt_buf != NULL);
2580 NB_SEGS(pkt_buf) = j;
2581 PORT(pkt_buf) = rxq->port_id;
2582 PKT_LEN(pkt_buf) = pkt_buf_len;
2583 pkt_buf->ol_flags = 0;
2585 /* Return packet. */
2586 *(pkts++) = pkt_buf;
2588 #ifdef MLX4_PMD_SOFT_COUNTERS
2589 /* Increase bytes counter. */
2590 rxq->stats.ibytes += pkt_buf_len;
2593 if (++elts_head >= elts_n)
2597 if (unlikely(i == 0))
2602 DEBUG("%p: reposting %d WRs", (void *)rxq, i);
2604 ret = ibv_post_recv(rxq->qp, head.next, &bad_wr);
2605 if (unlikely(ret)) {
2606 /* Inability to repost WRs is fatal. */
2607 DEBUG("%p: ibv_post_recv(): failed for WR %p: %s",
2613 rxq->elts_head = elts_head;
2614 #ifdef MLX4_PMD_SOFT_COUNTERS
2615 /* Increase packets counter. */
2616 rxq->stats.ipackets += pkts_ret;
2622 * DPDK callback for RX.
2624 * The following function is the same as mlx4_rx_burst_sp(), except it doesn't
2625 * manage scattered packets. Improves performance when MRU is lower than the
2626 * size of the first segment.
2629 * Generic pointer to RX queue structure.
2631 * Array to store received packets.
2633 * Maximum number of packets in array.
2636 * Number of packets successfully received (<= pkts_n).
2639 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2641 struct rxq *rxq = (struct rxq *)dpdk_rxq;
2642 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
2643 const unsigned int elts_n = rxq->elts_n;
2644 unsigned int elts_head = rxq->elts_head;
2645 struct ibv_sge sges[pkts_n];
2647 unsigned int pkts_ret = 0;
2650 if (unlikely(rxq->sp))
2651 return mlx4_rx_burst_sp(dpdk_rxq, pkts, pkts_n);
2652 for (i = 0; (i != pkts_n); ++i) {
2653 struct rxq_elt *elt = &(*elts)[elts_head];
2654 struct ibv_recv_wr *wr = &elt->wr;
2655 uint64_t wr_id = wr->wr_id;
2657 struct rte_mbuf *seg = (void *)((uintptr_t)elt->sge.addr -
2658 WR_ID(wr_id).offset);
2659 struct rte_mbuf *rep;
2661 /* Sanity checks. */
2662 assert(WR_ID(wr_id).id < rxq->elts_n);
2663 assert(wr->sg_list == &elt->sge);
2664 assert(wr->num_sge == 1);
2665 assert(elts_head < rxq->elts_n);
2666 assert(rxq->elts_head < rxq->elts_n);
2667 ret = rxq->if_cq->poll_length(rxq->cq, NULL, NULL);
2668 if (unlikely(ret < 0)) {
2672 DEBUG("rxq=%p, poll_length() failed (ret=%d)",
2674 /* ibv_poll_cq() must be used in case of failure. */
2675 wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
2676 if (unlikely(wcs_n == 0))
2678 if (unlikely(wcs_n < 0)) {
2679 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
2680 (void *)rxq, wcs_n);
2684 if (unlikely(wc.status != IBV_WC_SUCCESS)) {
2685 /* Whatever, just repost the offending WR. */
2686 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
2687 " completion status (%d): %s",
2688 (void *)rxq, wc.wr_id, wc.status,
2689 ibv_wc_status_str(wc.status));
2690 #ifdef MLX4_PMD_SOFT_COUNTERS
2691 /* Increment dropped packets counter. */
2692 ++rxq->stats.idropped;
2694 /* Add SGE to array for repost. */
2704 * Fetch initial bytes of packet descriptor into a
2705 * cacheline while allocating rep.
2708 rep = __rte_mbuf_raw_alloc(rxq->mp);
2709 if (unlikely(rep == NULL)) {
2711 * Unable to allocate a replacement mbuf,
2714 DEBUG("rxq=%p, wr_id=%" PRIu32 ":"
2715 " can't allocate a new mbuf",
2716 (void *)rxq, WR_ID(wr_id).id);
2717 /* Increase out of memory counters. */
2718 ++rxq->stats.rx_nombuf;
2719 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
2723 /* Reconfigure sge to use rep instead of seg. */
2724 elt->sge.addr = (uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM;
2725 assert(elt->sge.lkey == rxq->mr->lkey);
2726 WR_ID(wr->wr_id).offset =
2727 (((uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM) -
2729 assert(WR_ID(wr->wr_id).id == WR_ID(wr_id).id);
2731 /* Add SGE to array for repost. */
2734 /* Update seg information. */
2735 SET_DATA_OFF(seg, RTE_PKTMBUF_HEADROOM);
2737 PORT(seg) = rxq->port_id;
2740 DATA_LEN(seg) = len;
2743 /* Return packet. */
2746 #ifdef MLX4_PMD_SOFT_COUNTERS
2747 /* Increase bytes counter. */
2748 rxq->stats.ibytes += len;
2751 if (++elts_head >= elts_n)
2755 if (unlikely(i == 0))
2759 DEBUG("%p: reposting %u WRs", (void *)rxq, i);
2761 ret = rxq->if_qp->recv_burst(rxq->qp, sges, i);
2762 if (unlikely(ret)) {
2763 /* Inability to repost WRs is fatal. */
2764 DEBUG("%p: recv_burst(): failed (ret=%d)",
2769 rxq->elts_head = elts_head;
2770 #ifdef MLX4_PMD_SOFT_COUNTERS
2771 /* Increase packets counter. */
2772 rxq->stats.ipackets += pkts_ret;
2778 * Allocate a Queue Pair.
2779 * Optionally setup inline receive if supported.
2782 * Pointer to private structure.
2784 * Completion queue to associate with QP.
2786 * Number of descriptors in QP (hint only).
2789 * QP pointer or NULL in case of error.
2791 static struct ibv_qp *
2792 rxq_setup_qp(struct priv *priv, struct ibv_cq *cq, uint16_t desc)
2794 struct ibv_exp_qp_init_attr attr = {
2795 /* CQ to be associated with the send queue. */
2797 /* CQ to be associated with the receive queue. */
2800 /* Max number of outstanding WRs. */
2801 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
2802 priv->device_attr.max_qp_wr :
2804 /* Max number of scatter/gather elements in a WR. */
2805 .max_recv_sge = ((priv->device_attr.max_sge <
2806 MLX4_PMD_SGE_WR_N) ?
2807 priv->device_attr.max_sge :
2810 .qp_type = IBV_QPT_RAW_PACKET,
2811 .comp_mask = IBV_EXP_QP_INIT_ATTR_PD,
2816 attr.max_inl_recv = priv->inl_recv_size;
2817 attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
2819 return ibv_exp_create_qp(priv->ctx, &attr);
2825 * Allocate a RSS Queue Pair.
2826 * Optionally setup inline receive if supported.
2829 * Pointer to private structure.
2831 * Completion queue to associate with QP.
2833 * Number of descriptors in QP (hint only).
2835 * If nonzero, create a parent QP, otherwise a child.
2838 * QP pointer or NULL in case of error.
2840 static struct ibv_qp *
2841 rxq_setup_qp_rss(struct priv *priv, struct ibv_cq *cq, uint16_t desc,
2844 struct ibv_exp_qp_init_attr attr = {
2845 /* CQ to be associated with the send queue. */
2847 /* CQ to be associated with the receive queue. */
2850 /* Max number of outstanding WRs. */
2851 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
2852 priv->device_attr.max_qp_wr :
2854 /* Max number of scatter/gather elements in a WR. */
2855 .max_recv_sge = ((priv->device_attr.max_sge <
2856 MLX4_PMD_SGE_WR_N) ?
2857 priv->device_attr.max_sge :
2860 .qp_type = IBV_QPT_RAW_PACKET,
2861 .comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
2862 IBV_EXP_QP_INIT_ATTR_QPG),
2867 attr.max_inl_recv = priv->inl_recv_size,
2868 attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
2871 attr.qpg.qpg_type = IBV_EXP_QPG_PARENT;
2872 /* TSS isn't necessary. */
2873 attr.qpg.parent_attrib.tss_child_count = 0;
2874 attr.qpg.parent_attrib.rss_child_count = priv->rxqs_n;
2875 DEBUG("initializing parent RSS queue");
2877 attr.qpg.qpg_type = IBV_EXP_QPG_CHILD_RX;
2878 attr.qpg.qpg_parent = priv->rxq_parent.qp;
2879 DEBUG("initializing child RSS queue");
2881 return ibv_exp_create_qp(priv->ctx, &attr);
2884 #endif /* RSS_SUPPORT */
2887 * Reconfigure a RX queue with new parameters.
2889 * rxq_rehash() does not allocate mbufs, which, if not done from the right
2890 * thread (such as a control thread), may corrupt the pool.
2891 * In case of failure, the queue is left untouched.
2894 * Pointer to Ethernet device structure.
2899 * 0 on success, errno value on failure.
2902 rxq_rehash(struct rte_eth_dev *dev, struct rxq *rxq)
2904 struct priv *priv = rxq->priv;
2905 struct rxq tmpl = *rxq;
2906 unsigned int mbuf_n;
2907 unsigned int desc_n;
2908 struct rte_mbuf **pool;
2910 struct ibv_exp_qp_attr mod;
2911 struct ibv_recv_wr *bad_wr;
2913 int parent = (rxq == &priv->rxq_parent);
2916 ERROR("%p: cannot rehash parent queue %p",
2917 (void *)dev, (void *)rxq);
2920 DEBUG("%p: rehashing queue %p", (void *)dev, (void *)rxq);
2921 /* Number of descriptors and mbufs currently allocated. */
2922 desc_n = (tmpl.elts_n * (tmpl.sp ? MLX4_PMD_SGE_WR_N : 1));
2924 /* Enable scattered packets support for this queue if necessary. */
2925 if ((dev->data->dev_conf.rxmode.jumbo_frame) &&
2926 (dev->data->dev_conf.rxmode.max_rx_pkt_len >
2927 (tmpl.mb_len - RTE_PKTMBUF_HEADROOM))) {
2929 desc_n /= MLX4_PMD_SGE_WR_N;
2932 DEBUG("%p: %s scattered packets support (%u WRs)",
2933 (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc_n);
2934 /* If scatter mode is the same as before, nothing to do. */
2935 if (tmpl.sp == rxq->sp) {
2936 DEBUG("%p: nothing to do", (void *)dev);
2939 /* Remove attached flows if RSS is disabled (no parent queue). */
2941 rxq_allmulticast_disable(&tmpl);
2942 rxq_promiscuous_disable(&tmpl);
2943 rxq_mac_addrs_del(&tmpl);
2944 /* Update original queue in case of failure. */
2945 rxq->allmulti_flow = tmpl.allmulti_flow;
2946 rxq->promisc_flow = tmpl.promisc_flow;
2947 memcpy(rxq->mac_configured, tmpl.mac_configured,
2948 sizeof(rxq->mac_configured));
2949 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
2951 /* From now on, any failure will render the queue unusable.
2952 * Reinitialize QP. */
2953 mod = (struct ibv_exp_qp_attr){ .qp_state = IBV_QPS_RESET };
2954 err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
2956 ERROR("%p: cannot reset QP: %s", (void *)dev, strerror(err));
2960 err = ibv_resize_cq(tmpl.cq, desc_n);
2962 ERROR("%p: cannot resize CQ: %s", (void *)dev, strerror(err));
2966 mod = (struct ibv_exp_qp_attr){
2967 /* Move the QP to this state. */
2968 .qp_state = IBV_QPS_INIT,
2969 /* Primary port number. */
2970 .port_num = priv->port
2972 err = ibv_exp_modify_qp(tmpl.qp, &mod,
2975 (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
2976 #endif /* RSS_SUPPORT */
2979 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
2980 (void *)dev, strerror(err));
2984 /* Reconfigure flows. Do not care for errors. */
2986 rxq_mac_addrs_add(&tmpl);
2988 rxq_promiscuous_enable(&tmpl);
2990 rxq_allmulticast_enable(&tmpl);
2991 /* Update original queue in case of failure. */
2992 rxq->allmulti_flow = tmpl.allmulti_flow;
2993 rxq->promisc_flow = tmpl.promisc_flow;
2994 memcpy(rxq->mac_configured, tmpl.mac_configured,
2995 sizeof(rxq->mac_configured));
2996 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
2998 /* Allocate pool. */
2999 pool = rte_malloc(__func__, (mbuf_n * sizeof(*pool)), 0);
3001 ERROR("%p: cannot allocate memory", (void *)dev);
3004 /* Snatch mbufs from original queue. */
3007 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
3009 for (i = 0; (i != elemof(*elts)); ++i) {
3010 struct rxq_elt_sp *elt = &(*elts)[i];
3013 for (j = 0; (j != elemof(elt->bufs)); ++j) {
3014 assert(elt->bufs[j] != NULL);
3015 pool[k++] = elt->bufs[j];
3019 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
3021 for (i = 0; (i != elemof(*elts)); ++i) {
3022 struct rxq_elt *elt = &(*elts)[i];
3023 struct rte_mbuf *buf = (void *)
3024 ((uintptr_t)elt->sge.addr -
3025 WR_ID(elt->wr.wr_id).offset);
3027 assert(WR_ID(elt->wr.wr_id).id == i);
3031 assert(k == mbuf_n);
3033 tmpl.elts.sp = NULL;
3034 assert((void *)&tmpl.elts.sp == (void *)&tmpl.elts.no_sp);
3036 rxq_alloc_elts_sp(&tmpl, desc_n, pool) :
3037 rxq_alloc_elts(&tmpl, desc_n, pool));
3039 ERROR("%p: cannot reallocate WRs, aborting", (void *)dev);
3044 assert(tmpl.elts_n == desc_n);
3045 assert(tmpl.elts.sp != NULL);
3047 /* Clean up original data. */
3049 rte_free(rxq->elts.sp);
3050 rxq->elts.sp = NULL;
3052 err = ibv_post_recv(tmpl.qp,
3054 &(*tmpl.elts.sp)[0].wr :
3055 &(*tmpl.elts.no_sp)[0].wr),
3058 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
3064 mod = (struct ibv_exp_qp_attr){
3065 .qp_state = IBV_QPS_RTR
3067 err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3069 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
3070 (void *)dev, strerror(err));
3078 * Configure a RX queue.
3081 * Pointer to Ethernet device structure.
3083 * Pointer to RX queue structure.
3085 * Number of descriptors to configure in queue.
3087 * NUMA socket on which memory must be allocated.
3089 * Thresholds parameters.
3091 * Memory pool for buffer allocations.
3094 * 0 on success, errno value on failure.
3097 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
3098 unsigned int socket, const struct rte_eth_rxconf *conf,
3099 struct rte_mempool *mp)
3101 struct priv *priv = dev->data->dev_private;
3107 struct ibv_exp_qp_attr mod;
3109 struct ibv_exp_query_intf_params params;
3111 enum ibv_exp_query_intf_status status;
3112 struct ibv_recv_wr *bad_wr;
3113 struct rte_mbuf *buf;
3115 int parent = (rxq == &priv->rxq_parent);
3117 (void)conf; /* Thresholds configuration (ignored). */
3119 * If this is a parent queue, hardware must support RSS and
3120 * RSS must be enabled.
3122 assert((!parent) || ((priv->hw_rss) && (priv->rss)));
3124 /* Even if unused, ibv_create_cq() requires at least one
3129 if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
3130 ERROR("%p: invalid number of RX descriptors (must be a"
3131 " multiple of %d)", (void *)dev, MLX4_PMD_SGE_WR_N);
3134 /* Get mbuf length. */
3135 buf = rte_pktmbuf_alloc(mp);
3137 ERROR("%p: unable to allocate mbuf", (void *)dev);
3140 tmpl.mb_len = buf->buf_len;
3141 assert((rte_pktmbuf_headroom(buf) +
3142 rte_pktmbuf_tailroom(buf)) == tmpl.mb_len);
3143 assert(rte_pktmbuf_headroom(buf) == RTE_PKTMBUF_HEADROOM);
3144 rte_pktmbuf_free(buf);
3145 /* Enable scattered packets support for this queue if necessary. */
3146 if ((dev->data->dev_conf.rxmode.jumbo_frame) &&
3147 (dev->data->dev_conf.rxmode.max_rx_pkt_len >
3148 (tmpl.mb_len - RTE_PKTMBUF_HEADROOM))) {
3150 desc /= MLX4_PMD_SGE_WR_N;
3152 DEBUG("%p: %s scattered packets support (%u WRs)",
3153 (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc);
3154 /* Use the entire RX mempool as the memory region. */
3155 tmpl.mr = ibv_reg_mr(priv->pd,
3156 (void *)mp->elt_va_start,
3157 (mp->elt_va_end - mp->elt_va_start),
3158 (IBV_ACCESS_LOCAL_WRITE |
3159 IBV_ACCESS_REMOTE_WRITE));
3160 if (tmpl.mr == NULL) {
3162 ERROR("%p: MR creation failure: %s",
3163 (void *)dev, strerror(ret));
3167 tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
3168 if (tmpl.cq == NULL) {
3170 ERROR("%p: CQ creation failure: %s",
3171 (void *)dev, strerror(ret));
3174 DEBUG("priv->device_attr.max_qp_wr is %d",
3175 priv->device_attr.max_qp_wr);
3176 DEBUG("priv->device_attr.max_sge is %d",
3177 priv->device_attr.max_sge);
3180 tmpl.qp = rxq_setup_qp_rss(priv, tmpl.cq, desc, parent);
3182 #endif /* RSS_SUPPORT */
3183 tmpl.qp = rxq_setup_qp(priv, tmpl.cq, desc);
3184 if (tmpl.qp == NULL) {
3185 ret = (errno ? errno : EINVAL);
3186 ERROR("%p: QP creation failure: %s",
3187 (void *)dev, strerror(ret));
3190 mod = (struct ibv_exp_qp_attr){
3191 /* Move the QP to this state. */
3192 .qp_state = IBV_QPS_INIT,
3193 /* Primary port number. */
3194 .port_num = priv->port
3196 ret = ibv_exp_modify_qp(tmpl.qp, &mod,
3199 (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
3200 #endif /* RSS_SUPPORT */
3203 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
3204 (void *)dev, strerror(ret));
3207 if ((parent) || (!priv->rss)) {
3208 /* Configure MAC and broadcast addresses. */
3209 ret = rxq_mac_addrs_add(&tmpl);
3211 ERROR("%p: QP flow attachment failed: %s",
3212 (void *)dev, strerror(ret));
3216 /* Allocate descriptors for RX queues, except for the RSS parent. */
3220 ret = rxq_alloc_elts_sp(&tmpl, desc, NULL);
3222 ret = rxq_alloc_elts(&tmpl, desc, NULL);
3224 ERROR("%p: RXQ allocation failed: %s",
3225 (void *)dev, strerror(ret));
3228 ret = ibv_post_recv(tmpl.qp,
3230 &(*tmpl.elts.sp)[0].wr :
3231 &(*tmpl.elts.no_sp)[0].wr),
3234 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
3241 mod = (struct ibv_exp_qp_attr){
3242 .qp_state = IBV_QPS_RTR
3244 ret = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3246 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
3247 (void *)dev, strerror(ret));
3251 tmpl.port_id = dev->data->port_id;
3252 DEBUG("%p: RTE port ID: %u", (void *)rxq, tmpl.port_id);
3253 attr.params = (struct ibv_exp_query_intf_params){
3254 .intf_scope = IBV_EXP_INTF_GLOBAL,
3255 .intf = IBV_EXP_INTF_CQ,
3258 tmpl.if_cq = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
3259 if (tmpl.if_cq == NULL) {
3260 ERROR("%p: CQ interface family query failed with status %d",
3261 (void *)dev, status);
3264 attr.params = (struct ibv_exp_query_intf_params){
3265 .intf_scope = IBV_EXP_INTF_GLOBAL,
3266 .intf = IBV_EXP_INTF_QP_BURST,
3269 tmpl.if_qp = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
3270 if (tmpl.if_qp == NULL) {
3271 ERROR("%p: QP interface family query failed with status %d",
3272 (void *)dev, status);
3275 /* Clean up rxq in case we're reinitializing it. */
3276 DEBUG("%p: cleaning-up old rxq just in case", (void *)rxq);
3279 DEBUG("%p: rxq updated with %p", (void *)rxq, (void *)&tmpl);
3289 * DPDK callback to configure a RX queue.
3292 * Pointer to Ethernet device structure.
3296 * Number of descriptors to configure in queue.
3298 * NUMA socket on which memory must be allocated.
3300 * Thresholds parameters.
3302 * Memory pool for buffer allocations.
3305 * 0 on success, negative errno value on failure.
3308 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
3309 unsigned int socket, const struct rte_eth_rxconf *conf,
3310 struct rte_mempool *mp)
3312 struct priv *priv = dev->data->dev_private;
3313 struct rxq *rxq = (*priv->rxqs)[idx];
3317 DEBUG("%p: configuring queue %u for %u descriptors",
3318 (void *)dev, idx, desc);
3319 if (idx >= priv->rxqs_n) {
3320 ERROR("%p: queue index out of range (%u >= %u)",
3321 (void *)dev, idx, priv->rxqs_n);
3326 DEBUG("%p: reusing already allocated queue index %u (%p)",
3327 (void *)dev, idx, (void *)rxq);
3328 if (priv->started) {
3332 (*priv->rxqs)[idx] = NULL;
3335 rxq = rte_calloc_socket("RXQ", 1, sizeof(*rxq), 0, socket);
3337 ERROR("%p: unable to allocate queue index %u",
3343 ret = rxq_setup(dev, rxq, desc, socket, conf, mp);
3347 rxq->stats.idx = idx;
3348 DEBUG("%p: adding RX queue %p to list",
3349 (void *)dev, (void *)rxq);
3350 (*priv->rxqs)[idx] = rxq;
3351 /* Update receive callback. */
3353 dev->rx_pkt_burst = mlx4_rx_burst_sp;
3355 dev->rx_pkt_burst = mlx4_rx_burst;
3362 * DPDK callback to release a RX queue.
3365 * Generic RX queue pointer.
3368 mlx4_rx_queue_release(void *dpdk_rxq)
3370 struct rxq *rxq = (struct rxq *)dpdk_rxq;
3378 assert(rxq != &priv->rxq_parent);
3379 for (i = 0; (i != priv->rxqs_n); ++i)
3380 if ((*priv->rxqs)[i] == rxq) {
3381 DEBUG("%p: removing RX queue %p from list",
3382 (void *)priv->dev, (void *)rxq);
3383 (*priv->rxqs)[i] = NULL;
3392 * DPDK callback to start the device.
3394 * Simulate device start by attaching all configured flows.
3397 * Pointer to Ethernet device structure.
3400 * 0 on success, negative errno value on failure.
3403 mlx4_dev_start(struct rte_eth_dev *dev)
3405 struct priv *priv = dev->data->dev_private;
3411 if (priv->started) {
3415 DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
3418 rxq = &priv->rxq_parent;
3421 rxq = (*priv->rxqs)[0];
3424 /* Iterate only once when RSS is enabled. */
3428 /* Ignore nonexistent RX queues. */
3431 ret = rxq_mac_addrs_add(rxq);
3432 if (!ret && priv->promisc)
3433 ret = rxq_promiscuous_enable(rxq);
3434 if (!ret && priv->allmulti)
3435 ret = rxq_allmulticast_enable(rxq);
3438 WARN("%p: QP flow attachment failed: %s",
3439 (void *)dev, strerror(ret));
3442 rxq = (*priv->rxqs)[--i];
3444 rxq_allmulticast_disable(rxq);
3445 rxq_promiscuous_disable(rxq);
3446 rxq_mac_addrs_del(rxq);
3451 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
3457 * DPDK callback to stop the device.
3459 * Simulate device stop by detaching all configured flows.
3462 * Pointer to Ethernet device structure.
3465 mlx4_dev_stop(struct rte_eth_dev *dev)
3467 struct priv *priv = dev->data->dev_private;
3473 if (!priv->started) {
3477 DEBUG("%p: detaching flows from all RX queues", (void *)dev);
3480 rxq = &priv->rxq_parent;
3483 rxq = (*priv->rxqs)[0];
3486 /* Iterate only once when RSS is enabled. */
3488 /* Ignore nonexistent RX queues. */
3491 rxq_allmulticast_disable(rxq);
3492 rxq_promiscuous_disable(rxq);
3493 rxq_mac_addrs_del(rxq);
3494 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
3499 * Dummy DPDK callback for TX.
3501 * This function is used to temporarily replace the real callback during
3502 * unsafe control operations on the queue, or in case of error.
3505 * Generic pointer to TX queue structure.
3507 * Packets to transmit.
3509 * Number of packets in array.
3512 * Number of packets successfully transmitted (<= pkts_n).
3515 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
3524 * Dummy DPDK callback for RX.
3526 * This function is used to temporarily replace the real callback during
3527 * unsafe control operations on the queue, or in case of error.
3530 * Generic pointer to RX queue structure.
3532 * Array to store received packets.
3534 * Maximum number of packets in array.
3537 * Number of packets successfully received (<= pkts_n).
3540 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
3549 * DPDK callback to close the device.
3551 * Destroy all queues and objects, free memory.
3554 * Pointer to Ethernet device structure.
3557 mlx4_dev_close(struct rte_eth_dev *dev)
3559 struct priv *priv = dev->data->dev_private;
3564 DEBUG("%p: closing device \"%s\"",
3566 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
3567 /* Prevent crashes when queues are still in use. This is unfortunately
3568 * still required for DPDK 1.3 because some programs (such as testpmd)
3569 * never release them before closing the device. */
3570 dev->rx_pkt_burst = removed_rx_burst;
3571 dev->tx_pkt_burst = removed_tx_burst;
3572 if (priv->rxqs != NULL) {
3573 /* XXX race condition if mlx4_rx_burst() is still running. */
3575 for (i = 0; (i != priv->rxqs_n); ++i) {
3576 tmp = (*priv->rxqs)[i];
3579 (*priv->rxqs)[i] = NULL;
3586 if (priv->txqs != NULL) {
3587 /* XXX race condition if mlx4_tx_burst() is still running. */
3589 for (i = 0; (i != priv->txqs_n); ++i) {
3590 tmp = (*priv->txqs)[i];
3593 (*priv->txqs)[i] = NULL;
3601 rxq_cleanup(&priv->rxq_parent);
3602 if (priv->pd != NULL) {
3603 assert(priv->ctx != NULL);
3604 claim_zero(ibv_dealloc_pd(priv->pd));
3605 claim_zero(ibv_close_device(priv->ctx));
3607 assert(priv->ctx == NULL);
3609 memset(priv, 0, sizeof(*priv));
3613 * DPDK callback to get information about the device.
3616 * Pointer to Ethernet device structure.
3618 * Info structure output buffer.
3621 mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
3623 struct priv *priv = dev->data->dev_private;
3627 /* FIXME: we should ask the device for these values. */
3628 info->min_rx_bufsize = 32;
3629 info->max_rx_pktlen = 65536;
3631 * Since we need one CQ per QP, the limit is the minimum number
3632 * between the two values.
3634 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
3635 priv->device_attr.max_qp : priv->device_attr.max_cq);
3636 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
3639 info->max_rx_queues = max;
3640 info->max_tx_queues = max;
3641 info->max_mac_addrs = elemof(priv->mac);
3646 * DPDK callback to get device statistics.
3649 * Pointer to Ethernet device structure.
3651 * Stats structure output buffer.
3654 mlx4_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
3656 struct priv *priv = dev->data->dev_private;
3657 struct rte_eth_stats tmp = {0};
3662 /* Add software counters. */
3663 for (i = 0; (i != priv->rxqs_n); ++i) {
3664 struct rxq *rxq = (*priv->rxqs)[i];
3668 idx = rxq->stats.idx;
3669 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
3670 #ifdef MLX4_PMD_SOFT_COUNTERS
3671 tmp.q_ipackets[idx] += rxq->stats.ipackets;
3672 tmp.q_ibytes[idx] += rxq->stats.ibytes;
3674 tmp.q_errors[idx] += (rxq->stats.idropped +
3675 rxq->stats.rx_nombuf);
3677 #ifdef MLX4_PMD_SOFT_COUNTERS
3678 tmp.ipackets += rxq->stats.ipackets;
3679 tmp.ibytes += rxq->stats.ibytes;
3681 tmp.ierrors += rxq->stats.idropped;
3682 tmp.rx_nombuf += rxq->stats.rx_nombuf;
3684 for (i = 0; (i != priv->txqs_n); ++i) {
3685 struct txq *txq = (*priv->txqs)[i];
3689 idx = txq->stats.idx;
3690 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
3691 #ifdef MLX4_PMD_SOFT_COUNTERS
3692 tmp.q_opackets[idx] += txq->stats.opackets;
3693 tmp.q_obytes[idx] += txq->stats.obytes;
3695 tmp.q_errors[idx] += txq->stats.odropped;
3697 #ifdef MLX4_PMD_SOFT_COUNTERS
3698 tmp.opackets += txq->stats.opackets;
3699 tmp.obytes += txq->stats.obytes;
3701 tmp.oerrors += txq->stats.odropped;
3703 #ifndef MLX4_PMD_SOFT_COUNTERS
3704 /* FIXME: retrieve and add hardware counters. */
3711 * DPDK callback to clear device statistics.
3714 * Pointer to Ethernet device structure.
3717 mlx4_stats_reset(struct rte_eth_dev *dev)
3719 struct priv *priv = dev->data->dev_private;
3724 for (i = 0; (i != priv->rxqs_n); ++i) {
3725 if ((*priv->rxqs)[i] == NULL)
3727 idx = (*priv->rxqs)[i]->stats.idx;
3728 (*priv->rxqs)[i]->stats =
3729 (struct mlx4_rxq_stats){ .idx = idx };
3731 for (i = 0; (i != priv->txqs_n); ++i) {
3732 if ((*priv->txqs)[i] == NULL)
3734 idx = (*priv->rxqs)[i]->stats.idx;
3735 (*priv->txqs)[i]->stats =
3736 (struct mlx4_txq_stats){ .idx = idx };
3738 #ifndef MLX4_PMD_SOFT_COUNTERS
3739 /* FIXME: reset hardware counters. */
3745 * DPDK callback to remove a MAC address.
3748 * Pointer to Ethernet device structure.
3750 * MAC address index.
3753 mlx4_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
3755 struct priv *priv = dev->data->dev_private;
3758 DEBUG("%p: removing MAC address from index %" PRIu32,
3759 (void *)dev, index);
3760 if (index >= MLX4_MAX_MAC_ADDRESSES)
3762 /* Refuse to remove the broadcast address, this one is special. */
3763 if (!memcmp(priv->mac[index].addr_bytes, "\xff\xff\xff\xff\xff\xff",
3766 priv_mac_addr_del(priv, index);
3772 * DPDK callback to add a MAC address.
3775 * Pointer to Ethernet device structure.
3777 * MAC address to register.
3779 * MAC address index.
3781 * VMDq pool index to associate address with (ignored).
3784 mlx4_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
3785 uint32_t index, uint32_t vmdq)
3787 struct priv *priv = dev->data->dev_private;
3791 DEBUG("%p: adding MAC address at index %" PRIu32,
3792 (void *)dev, index);
3793 if (index >= MLX4_MAX_MAC_ADDRESSES)
3795 /* Refuse to add the broadcast address, this one is special. */
3796 if (!memcmp(mac_addr->addr_bytes, "\xff\xff\xff\xff\xff\xff",
3799 priv_mac_addr_add(priv, index,
3800 (const uint8_t (*)[ETHER_ADDR_LEN])
3801 mac_addr->addr_bytes);
3807 * DPDK callback to enable promiscuous mode.
3810 * Pointer to Ethernet device structure.
3813 mlx4_promiscuous_enable(struct rte_eth_dev *dev)
3815 struct priv *priv = dev->data->dev_private;
3820 if (priv->promisc) {
3824 /* If device isn't started, this is all we need to do. */
3828 ret = rxq_promiscuous_enable(&priv->rxq_parent);
3835 for (i = 0; (i != priv->rxqs_n); ++i) {
3836 if ((*priv->rxqs)[i] == NULL)
3838 ret = rxq_promiscuous_enable((*priv->rxqs)[i]);
3841 /* Failure, rollback. */
3843 if ((*priv->rxqs)[--i] != NULL)
3844 rxq_promiscuous_disable((*priv->rxqs)[i]);
3854 * DPDK callback to disable promiscuous mode.
3857 * Pointer to Ethernet device structure.
3860 mlx4_promiscuous_disable(struct rte_eth_dev *dev)
3862 struct priv *priv = dev->data->dev_private;
3866 if (!priv->promisc) {
3871 rxq_promiscuous_disable(&priv->rxq_parent);
3874 for (i = 0; (i != priv->rxqs_n); ++i)
3875 if ((*priv->rxqs)[i] != NULL)
3876 rxq_promiscuous_disable((*priv->rxqs)[i]);
3883 * DPDK callback to enable allmulti mode.
3886 * Pointer to Ethernet device structure.
3889 mlx4_allmulticast_enable(struct rte_eth_dev *dev)
3891 struct priv *priv = dev->data->dev_private;
3896 if (priv->allmulti) {
3900 /* If device isn't started, this is all we need to do. */
3904 ret = rxq_allmulticast_enable(&priv->rxq_parent);
3911 for (i = 0; (i != priv->rxqs_n); ++i) {
3912 if ((*priv->rxqs)[i] == NULL)
3914 ret = rxq_allmulticast_enable((*priv->rxqs)[i]);
3917 /* Failure, rollback. */
3919 if ((*priv->rxqs)[--i] != NULL)
3920 rxq_allmulticast_disable((*priv->rxqs)[i]);
3930 * DPDK callback to disable allmulti mode.
3933 * Pointer to Ethernet device structure.
3936 mlx4_allmulticast_disable(struct rte_eth_dev *dev)
3938 struct priv *priv = dev->data->dev_private;
3942 if (!priv->allmulti) {
3947 rxq_allmulticast_disable(&priv->rxq_parent);
3950 for (i = 0; (i != priv->rxqs_n); ++i)
3951 if ((*priv->rxqs)[i] != NULL)
3952 rxq_allmulticast_disable((*priv->rxqs)[i]);
3959 * DPDK callback to retrieve physical link information (unlocked version).
3962 * Pointer to Ethernet device structure.
3963 * @param wait_to_complete
3964 * Wait for request completion (ignored).
3967 mlx4_link_update_unlocked(struct rte_eth_dev *dev, int wait_to_complete)
3969 struct priv *priv = dev->data->dev_private;
3970 struct ethtool_cmd edata = {
3974 struct rte_eth_link dev_link;
3977 (void)wait_to_complete;
3978 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
3979 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
3982 memset(&dev_link, 0, sizeof(dev_link));
3983 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
3984 (ifr.ifr_flags & IFF_RUNNING));
3985 ifr.ifr_data = &edata;
3986 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
3987 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
3991 link_speed = ethtool_cmd_speed(&edata);
3992 if (link_speed == -1)
3993 dev_link.link_speed = 0;
3995 dev_link.link_speed = link_speed;
3996 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
3997 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
3998 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
3999 /* Link status changed. */
4000 dev->data->dev_link = dev_link;
4003 /* Link status is still the same. */
4008 * DPDK callback to retrieve physical link information.
4011 * Pointer to Ethernet device structure.
4012 * @param wait_to_complete
4013 * Wait for request completion (ignored).
4016 mlx4_link_update(struct rte_eth_dev *dev, int wait_to_complete)
4018 struct priv *priv = dev->data->dev_private;
4022 ret = mlx4_link_update_unlocked(dev, wait_to_complete);
4028 * DPDK callback to change the MTU.
4030 * Setting the MTU affects hardware MRU (packets larger than the MTU cannot be
4031 * received). Use this as a hint to enable/disable scattered packets support
4032 * and improve performance when not needed.
4033 * Since failure is not an option, reconfiguring queues on the fly is not
4037 * Pointer to Ethernet device structure.
4042 * 0 on success, negative errno value on failure.
4045 mlx4_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
4047 struct priv *priv = dev->data->dev_private;
4050 uint16_t (*rx_func)(void *, struct rte_mbuf **, uint16_t) =
4054 /* Set kernel interface MTU first. */
4055 if (priv_set_mtu(priv, mtu)) {
4057 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
4061 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
4063 /* Temporarily replace RX handler with a fake one, assuming it has not
4064 * been copied elsewhere. */
4065 dev->rx_pkt_burst = removed_rx_burst;
4066 /* Make sure everyone has left mlx4_rx_burst() and uses
4067 * removed_rx_burst() instead. */
4070 /* Reconfigure each RX queue. */
4071 for (i = 0; (i != priv->rxqs_n); ++i) {
4072 struct rxq *rxq = (*priv->rxqs)[i];
4073 unsigned int max_frame_len;
4078 /* Calculate new maximum frame length according to MTU and
4079 * toggle scattered support (sp) if necessary. */
4080 max_frame_len = (priv->mtu + ETHER_HDR_LEN +
4081 (ETHER_MAX_VLAN_FRAME_LEN - ETHER_MAX_LEN));
4082 sp = (max_frame_len > (rxq->mb_len - RTE_PKTMBUF_HEADROOM));
4083 /* Provide new values to rxq_setup(). */
4084 dev->data->dev_conf.rxmode.jumbo_frame = sp;
4085 dev->data->dev_conf.rxmode.max_rx_pkt_len = max_frame_len;
4086 ret = rxq_rehash(dev, rxq);
4088 /* Force SP RX if that queue requires it and abort. */
4090 rx_func = mlx4_rx_burst_sp;
4093 /* Reenable non-RSS queue attributes. No need to check
4094 * for errors at this stage. */
4096 rxq_mac_addrs_add(rxq);
4098 rxq_promiscuous_enable(rxq);
4100 rxq_allmulticast_enable(rxq);
4102 /* Scattered burst function takes priority. */
4104 rx_func = mlx4_rx_burst_sp;
4106 /* Burst functions can now be called again. */
4108 dev->rx_pkt_burst = rx_func;
4116 * DPDK callback to get flow control status.
4119 * Pointer to Ethernet device structure.
4120 * @param[out] fc_conf
4121 * Flow control output buffer.
4124 * 0 on success, negative errno value on failure.
4127 mlx4_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4129 struct priv *priv = dev->data->dev_private;
4131 struct ethtool_pauseparam ethpause = {
4132 .cmd = ETHTOOL_GPAUSEPARAM
4136 ifr.ifr_data = ðpause;
4138 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4140 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
4146 fc_conf->autoneg = ethpause.autoneg;
4147 if (ethpause.rx_pause && ethpause.tx_pause)
4148 fc_conf->mode = RTE_FC_FULL;
4149 else if (ethpause.rx_pause)
4150 fc_conf->mode = RTE_FC_RX_PAUSE;
4151 else if (ethpause.tx_pause)
4152 fc_conf->mode = RTE_FC_TX_PAUSE;
4154 fc_conf->mode = RTE_FC_NONE;
4164 * DPDK callback to modify flow control parameters.
4167 * Pointer to Ethernet device structure.
4168 * @param[in] fc_conf
4169 * Flow control parameters.
4172 * 0 on success, negative errno value on failure.
4175 mlx4_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4177 struct priv *priv = dev->data->dev_private;
4179 struct ethtool_pauseparam ethpause = {
4180 .cmd = ETHTOOL_SPAUSEPARAM
4184 ifr.ifr_data = ðpause;
4185 ethpause.autoneg = fc_conf->autoneg;
4186 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4187 (fc_conf->mode & RTE_FC_RX_PAUSE))
4188 ethpause.rx_pause = 1;
4190 ethpause.rx_pause = 0;
4192 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4193 (fc_conf->mode & RTE_FC_TX_PAUSE))
4194 ethpause.tx_pause = 1;
4196 ethpause.tx_pause = 0;
4199 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4201 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
4215 * Configure a VLAN filter.
4218 * Pointer to Ethernet device structure.
4220 * VLAN ID to filter.
4225 * 0 on success, errno value on failure.
4228 vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
4230 struct priv *priv = dev->data->dev_private;
4232 unsigned int j = -1;
4234 DEBUG("%p: %s VLAN filter ID %" PRIu16,
4235 (void *)dev, (on ? "enable" : "disable"), vlan_id);
4236 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
4237 if (!priv->vlan_filter[i].enabled) {
4238 /* Unused index, remember it. */
4242 if (priv->vlan_filter[i].id != vlan_id)
4244 /* This VLAN ID is already known, use its index. */
4248 /* Check if there's room for another VLAN filter. */
4249 if (j == (unsigned int)-1)
4252 * VLAN filters apply to all configured MAC addresses, flow
4253 * specifications must be reconfigured accordingly.
4255 priv->vlan_filter[j].id = vlan_id;
4256 if ((on) && (!priv->vlan_filter[j].enabled)) {
4258 * Filter is disabled, enable it.
4259 * Rehashing flows in all RX queues is necessary.
4262 rxq_mac_addrs_del(&priv->rxq_parent);
4264 for (i = 0; (i != priv->rxqs_n); ++i)
4265 if ((*priv->rxqs)[i] != NULL)
4266 rxq_mac_addrs_del((*priv->rxqs)[i]);
4267 priv->vlan_filter[j].enabled = 1;
4268 if (priv->started) {
4270 rxq_mac_addrs_add(&priv->rxq_parent);
4272 for (i = 0; (i != priv->rxqs_n); ++i) {
4273 if ((*priv->rxqs)[i] == NULL)
4275 rxq_mac_addrs_add((*priv->rxqs)[i]);
4278 } else if ((!on) && (priv->vlan_filter[j].enabled)) {
4280 * Filter is enabled, disable it.
4281 * Rehashing flows in all RX queues is necessary.
4284 rxq_mac_addrs_del(&priv->rxq_parent);
4286 for (i = 0; (i != priv->rxqs_n); ++i)
4287 if ((*priv->rxqs)[i] != NULL)
4288 rxq_mac_addrs_del((*priv->rxqs)[i]);
4289 priv->vlan_filter[j].enabled = 0;
4290 if (priv->started) {
4292 rxq_mac_addrs_add(&priv->rxq_parent);
4294 for (i = 0; (i != priv->rxqs_n); ++i) {
4295 if ((*priv->rxqs)[i] == NULL)
4297 rxq_mac_addrs_add((*priv->rxqs)[i]);
4305 * DPDK callback to configure a VLAN filter.
4308 * Pointer to Ethernet device structure.
4310 * VLAN ID to filter.
4315 * 0 on success, negative errno value on failure.
4318 mlx4_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
4320 struct priv *priv = dev->data->dev_private;
4324 ret = vlan_filter_set(dev, vlan_id, on);
4330 static const struct eth_dev_ops mlx4_dev_ops = {
4331 .dev_configure = mlx4_dev_configure,
4332 .dev_start = mlx4_dev_start,
4333 .dev_stop = mlx4_dev_stop,
4334 .dev_close = mlx4_dev_close,
4335 .promiscuous_enable = mlx4_promiscuous_enable,
4336 .promiscuous_disable = mlx4_promiscuous_disable,
4337 .allmulticast_enable = mlx4_allmulticast_enable,
4338 .allmulticast_disable = mlx4_allmulticast_disable,
4339 .link_update = mlx4_link_update,
4340 .stats_get = mlx4_stats_get,
4341 .stats_reset = mlx4_stats_reset,
4342 .queue_stats_mapping_set = NULL,
4343 .dev_infos_get = mlx4_dev_infos_get,
4344 .vlan_filter_set = mlx4_vlan_filter_set,
4345 .vlan_tpid_set = NULL,
4346 .vlan_strip_queue_set = NULL,
4347 .vlan_offload_set = NULL,
4348 .rx_queue_setup = mlx4_rx_queue_setup,
4349 .tx_queue_setup = mlx4_tx_queue_setup,
4350 .rx_queue_release = mlx4_rx_queue_release,
4351 .tx_queue_release = mlx4_tx_queue_release,
4353 .dev_led_off = NULL,
4354 .flow_ctrl_get = mlx4_dev_get_flow_ctrl,
4355 .flow_ctrl_set = mlx4_dev_set_flow_ctrl,
4356 .priority_flow_ctrl_set = NULL,
4357 .mac_addr_remove = mlx4_mac_addr_remove,
4358 .mac_addr_add = mlx4_mac_addr_add,
4359 .mtu_set = mlx4_dev_set_mtu,
4360 .fdir_add_signature_filter = NULL,
4361 .fdir_update_signature_filter = NULL,
4362 .fdir_remove_signature_filter = NULL,
4363 .fdir_add_perfect_filter = NULL,
4364 .fdir_update_perfect_filter = NULL,
4365 .fdir_remove_perfect_filter = NULL,
4366 .fdir_set_masks = NULL
4370 * Get PCI information from struct ibv_device.
4373 * Pointer to Ethernet device structure.
4374 * @param[out] pci_addr
4375 * PCI bus address output buffer.
4378 * 0 on success, -1 on failure and errno is set.
4381 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
4382 struct rte_pci_addr *pci_addr)
4386 MKSTR(path, "%s/device/uevent", device->ibdev_path);
4388 file = fopen(path, "rb");
4391 while (fgets(line, sizeof(line), file) == line) {
4392 size_t len = strlen(line);
4395 /* Truncate long lines. */
4396 if (len == (sizeof(line) - 1))
4397 while (line[(len - 1)] != '\n') {
4401 line[(len - 1)] = ret;
4403 /* Extract information. */
4406 "%" SCNx16 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
4410 &pci_addr->function) == 4) {
4420 * Get MAC address by querying netdevice.
4423 * struct priv for the requested device.
4425 * MAC address output buffer.
4428 * 0 on success, -1 on failure and errno is set.
4431 priv_get_mac(struct priv *priv, uint8_t (*mac)[ETHER_ADDR_LEN])
4433 struct ifreq request;
4435 if (priv_ifreq(priv, SIOCGIFHWADDR, &request))
4437 memcpy(mac, request.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
4441 /* Support up to 32 adapters. */
4443 struct rte_pci_addr pci_addr; /* associated PCI address */
4444 uint32_t ports; /* physical ports bitfield. */
4448 * Get device index in mlx4_dev[] from PCI bus address.
4450 * @param[in] pci_addr
4451 * PCI bus address to look for.
4454 * mlx4_dev[] index on success, -1 on failure.
4457 mlx4_dev_idx(struct rte_pci_addr *pci_addr)
4462 assert(pci_addr != NULL);
4463 for (i = 0; (i != elemof(mlx4_dev)); ++i) {
4464 if ((mlx4_dev[i].pci_addr.domain == pci_addr->domain) &&
4465 (mlx4_dev[i].pci_addr.bus == pci_addr->bus) &&
4466 (mlx4_dev[i].pci_addr.devid == pci_addr->devid) &&
4467 (mlx4_dev[i].pci_addr.function == pci_addr->function))
4469 if ((mlx4_dev[i].ports == 0) && (ret == -1))
4476 * Retrieve integer value from environment variable.
4479 * Environment variable name.
4482 * Integer value, 0 if the variable is not set.
4485 mlx4_getenv_int(const char *name)
4487 const char *val = getenv(name);
4494 static struct eth_driver mlx4_driver;
4497 * DPDK callback to register a PCI device.
4499 * This function creates an Ethernet device for each port of a given
4502 * @param[in] pci_drv
4503 * PCI driver structure (mlx4_driver).
4504 * @param[in] pci_dev
4505 * PCI device information.
4508 * 0 on success, negative errno value on failure.
4511 mlx4_pci_devinit(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
4513 struct ibv_device **list;
4514 struct ibv_device *ibv_dev;
4516 struct ibv_context *attr_ctx = NULL;
4517 struct ibv_device_attr device_attr;
4523 assert(pci_drv == &mlx4_driver.pci_drv);
4524 /* Get mlx4_dev[] index. */
4525 idx = mlx4_dev_idx(&pci_dev->addr);
4527 ERROR("this driver cannot support any more adapters");
4530 DEBUG("using driver device index %d", idx);
4532 /* Save PCI address. */
4533 mlx4_dev[idx].pci_addr = pci_dev->addr;
4534 list = ibv_get_device_list(&i);
4537 if (errno == ENOSYS) {
4538 WARN("cannot list devices, is ib_uverbs loaded?");
4545 * For each listed device, check related sysfs entry against
4546 * the provided PCI ID.
4549 struct rte_pci_addr pci_addr;
4552 DEBUG("checking device \"%s\"", list[i]->name);
4553 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
4555 if ((pci_dev->addr.domain != pci_addr.domain) ||
4556 (pci_dev->addr.bus != pci_addr.bus) ||
4557 (pci_dev->addr.devid != pci_addr.devid) ||
4558 (pci_dev->addr.function != pci_addr.function))
4560 vf = (pci_dev->id.device_id ==
4561 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
4562 INFO("PCI information matches, using device \"%s\" (VF: %s)",
4563 list[i]->name, (vf ? "true" : "false"));
4564 attr_ctx = ibv_open_device(list[i]);
4568 if (attr_ctx == NULL) {
4569 ibv_free_device_list(list);
4572 WARN("cannot access device, is mlx4_ib loaded?");
4575 WARN("cannot use device, are drivers up to date?");
4583 DEBUG("device opened");
4584 if (ibv_query_device(attr_ctx, &device_attr))
4586 INFO("%u port(s) detected", device_attr.phys_port_cnt);
4588 for (i = 0; i < device_attr.phys_port_cnt; i++) {
4589 uint32_t port = i + 1; /* ports are indexed from one */
4590 uint32_t test = (1 << i);
4591 struct ibv_context *ctx = NULL;
4592 struct ibv_port_attr port_attr;
4593 struct ibv_pd *pd = NULL;
4594 struct priv *priv = NULL;
4595 struct rte_eth_dev *eth_dev;
4596 #ifdef HAVE_EXP_QUERY_DEVICE
4597 struct ibv_exp_device_attr exp_device_attr;
4598 #endif /* HAVE_EXP_QUERY_DEVICE */
4599 struct ether_addr mac;
4601 #ifdef HAVE_EXP_QUERY_DEVICE
4602 exp_device_attr.comp_mask = IBV_EXP_DEVICE_ATTR_EXP_CAP_FLAGS;
4604 exp_device_attr.comp_mask |= IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ;
4605 #endif /* RSS_SUPPORT */
4606 #endif /* HAVE_EXP_QUERY_DEVICE */
4608 DEBUG("using port %u (%08" PRIx32 ")", port, test);
4610 ctx = ibv_open_device(ibv_dev);
4614 /* Check port status. */
4615 err = ibv_query_port(ctx, port, &port_attr);
4617 ERROR("port query failed: %s", strerror(err));
4620 if (port_attr.state != IBV_PORT_ACTIVE)
4621 WARN("bad state for port %d: \"%s\" (%d)",
4622 port, ibv_port_state_str(port_attr.state),
4625 /* Allocate protection domain. */
4626 pd = ibv_alloc_pd(ctx);
4628 ERROR("PD allocation failure");
4633 mlx4_dev[idx].ports |= test;
4635 /* from rte_ethdev.c */
4636 priv = rte_zmalloc("ethdev private structure",
4638 RTE_CACHE_LINE_SIZE);
4640 ERROR("priv allocation failure");
4646 priv->device_attr = device_attr;
4649 priv->mtu = ETHER_MTU;
4650 #ifdef HAVE_EXP_QUERY_DEVICE
4651 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
4652 ERROR("ibv_exp_query_device() failed");
4656 if ((exp_device_attr.exp_device_cap_flags &
4657 IBV_EXP_DEVICE_QPG) &&
4658 (exp_device_attr.exp_device_cap_flags &
4659 IBV_EXP_DEVICE_UD_RSS) &&
4660 (exp_device_attr.comp_mask &
4661 IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ) &&
4662 (exp_device_attr.max_rss_tbl_sz > 0)) {
4665 priv->max_rss_tbl_sz = exp_device_attr.max_rss_tbl_sz;
4669 priv->max_rss_tbl_sz = 0;
4671 priv->hw_tss = !!(exp_device_attr.exp_device_cap_flags &
4672 IBV_EXP_DEVICE_UD_TSS);
4673 DEBUG("device flags: %s%s%s",
4674 (priv->hw_qpg ? "IBV_DEVICE_QPG " : ""),
4675 (priv->hw_tss ? "IBV_DEVICE_TSS " : ""),
4676 (priv->hw_rss ? "IBV_DEVICE_RSS " : ""));
4678 DEBUG("maximum RSS indirection table size: %u",
4679 exp_device_attr.max_rss_tbl_sz);
4680 #endif /* RSS_SUPPORT */
4683 priv->inl_recv_size = mlx4_getenv_int("MLX4_INLINE_RECV_SIZE");
4685 if (priv->inl_recv_size) {
4686 exp_device_attr.comp_mask =
4687 IBV_EXP_DEVICE_ATTR_INLINE_RECV_SZ;
4688 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
4689 INFO("Couldn't query device for inline-receive"
4691 priv->inl_recv_size = 0;
4693 if ((unsigned)exp_device_attr.inline_recv_sz <
4694 priv->inl_recv_size) {
4695 INFO("Max inline-receive (%d) <"
4696 " requested inline-receive (%u)",
4697 exp_device_attr.inline_recv_sz,
4698 priv->inl_recv_size);
4699 priv->inl_recv_size =
4700 exp_device_attr.inline_recv_sz;
4703 INFO("Set inline receive size to %u",
4704 priv->inl_recv_size);
4706 #endif /* INLINE_RECV */
4707 #endif /* HAVE_EXP_QUERY_DEVICE */
4709 (void)mlx4_getenv_int;
4711 /* Configure the first MAC address by default. */
4712 if (priv_get_mac(priv, &mac.addr_bytes)) {
4713 ERROR("cannot get MAC address, is mlx4_en loaded?"
4714 " (errno: %s)", strerror(errno));
4717 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
4719 mac.addr_bytes[0], mac.addr_bytes[1],
4720 mac.addr_bytes[2], mac.addr_bytes[3],
4721 mac.addr_bytes[4], mac.addr_bytes[5]);
4722 /* Register MAC and broadcast addresses. */
4723 claim_zero(priv_mac_addr_add(priv, 0,
4724 (const uint8_t (*)[ETHER_ADDR_LEN])
4726 claim_zero(priv_mac_addr_add(priv, 1,
4727 &(const uint8_t [ETHER_ADDR_LEN])
4728 { "\xff\xff\xff\xff\xff\xff" }));
4731 char ifname[IF_NAMESIZE];
4733 if (priv_get_ifname(priv, &ifname) == 0)
4734 DEBUG("port %u ifname is \"%s\"",
4735 priv->port, ifname);
4737 DEBUG("port %u ifname is unknown", priv->port);
4740 /* Get actual MTU if possible. */
4741 priv_get_mtu(priv, &priv->mtu);
4742 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
4744 /* from rte_ethdev.c */
4746 char name[RTE_ETH_NAME_MAX_LEN];
4748 snprintf(name, sizeof(name), "%s port %u",
4749 ibv_get_device_name(ibv_dev), port);
4750 eth_dev = rte_eth_dev_allocate(name, RTE_ETH_DEV_PCI);
4752 if (eth_dev == NULL) {
4753 ERROR("can not allocate rte ethdev");
4758 eth_dev->data->dev_private = priv;
4759 eth_dev->pci_dev = pci_dev;
4760 eth_dev->driver = &mlx4_driver;
4761 eth_dev->data->rx_mbuf_alloc_failed = 0;
4762 eth_dev->data->mtu = ETHER_MTU;
4764 priv->dev = eth_dev;
4765 eth_dev->dev_ops = &mlx4_dev_ops;
4766 eth_dev->data->mac_addrs = priv->mac;
4768 /* Bring Ethernet device up. */
4769 DEBUG("forcing Ethernet interface up");
4770 priv_set_flags(priv, ~IFF_UP, IFF_UP);
4776 claim_zero(ibv_dealloc_pd(pd));
4778 claim_zero(ibv_close_device(ctx));
4783 * XXX if something went wrong in the loop above, there is a resource
4784 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
4785 * long as the dpdk does not provide a way to deallocate a ethdev and a
4786 * way to enumerate the registered ethdevs to free the previous ones.
4789 /* no port found, complain */
4790 if (!mlx4_dev[idx].ports) {
4797 claim_zero(ibv_close_device(attr_ctx));
4799 ibv_free_device_list(list);
4804 static const struct rte_pci_id mlx4_pci_id_map[] = {
4806 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4807 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3,
4808 .subsystem_vendor_id = PCI_ANY_ID,
4809 .subsystem_device_id = PCI_ANY_ID
4812 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4813 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO,
4814 .subsystem_vendor_id = PCI_ANY_ID,
4815 .subsystem_device_id = PCI_ANY_ID
4818 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4819 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3VF,
4820 .subsystem_vendor_id = PCI_ANY_ID,
4821 .subsystem_device_id = PCI_ANY_ID
4828 static struct eth_driver mlx4_driver = {
4830 .name = MLX4_DRIVER_NAME,
4831 .id_table = mlx4_pci_id_map,
4832 .devinit = mlx4_pci_devinit,
4834 .dev_private_size = sizeof(struct priv)
4838 * Driver initialization routine.
4841 rte_mlx4_pmd_init(const char *name, const char *args)
4846 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
4847 * huge pages. Calling ibv_fork_init() during init allows
4848 * applications to use fork() safely for purposes other than
4849 * using this PMD, which is not supported in forked processes.
4851 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
4853 rte_eal_pci_register(&mlx4_driver.pci_drv);
4857 static struct rte_driver rte_mlx4_driver = {
4859 .name = MLX4_DRIVER_NAME,
4860 .init = rte_mlx4_pmd_init,
4863 PMD_REGISTER_DRIVER(rte_mlx4_driver)