4 * Copyright 2012-2015 6WIND S.A.
5 * Copyright 2012 Mellanox.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of 6WIND S.A. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * - RSS hash key and options cannot be modified.
37 * - Hardware counters aren't implemented.
51 #include <arpa/inet.h>
54 #include <sys/ioctl.h>
55 #include <sys/socket.h>
56 #include <netinet/in.h>
58 #include <linux/ethtool.h>
59 #include <linux/sockios.h>
62 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
64 #pragma GCC diagnostic ignored "-pedantic"
66 #include <infiniband/verbs.h>
68 #pragma GCC diagnostic error "-pedantic"
71 /* DPDK headers don't like -pedantic. */
73 #pragma GCC diagnostic ignored "-pedantic"
75 #include <rte_config.h>
76 #include <rte_ether.h>
77 #include <rte_ethdev.h>
80 #include <rte_errno.h>
81 #include <rte_mempool.h>
82 #include <rte_prefetch.h>
83 #include <rte_malloc.h>
84 #include <rte_spinlock.h>
85 #include <rte_atomic.h>
86 #include <rte_version.h>
89 #pragma GCC diagnostic error "-pedantic"
92 /* Generated configuration header. */
93 #include "mlx4_autoconf.h"
98 /* Runtime logging through RTE_LOG() is enabled when not in debugging mode.
99 * Intermediate LOG_*() macros add the required end-of-line characters. */
101 #define INFO(...) DEBUG(__VA_ARGS__)
102 #define WARN(...) DEBUG(__VA_ARGS__)
103 #define ERROR(...) DEBUG(__VA_ARGS__)
105 #define LOG__(level, m, ...) \
106 RTE_LOG(level, PMD, MLX4_DRIVER_NAME ": " m "%c", __VA_ARGS__)
107 #define LOG_(level, ...) LOG__(level, __VA_ARGS__, '\n')
108 #define INFO(...) LOG_(INFO, __VA_ARGS__)
109 #define WARN(...) LOG_(WARNING, __VA_ARGS__)
110 #define ERROR(...) LOG_(ERR, __VA_ARGS__)
113 /* Convenience macros for accessing mbuf fields. */
114 #define NEXT(m) ((m)->next)
115 #define DATA_LEN(m) ((m)->data_len)
116 #define PKT_LEN(m) ((m)->pkt_len)
117 #define DATA_OFF(m) ((m)->data_off)
118 #define SET_DATA_OFF(m, o) ((m)->data_off = (o))
119 #define NB_SEGS(m) ((m)->nb_segs)
120 #define PORT(m) ((m)->port)
122 /* Work Request ID data type (64 bit). */
131 #define WR_ID(o) (((wr_id_t *)&(o))->data)
133 /* Compile-time check. */
134 static inline void wr_id_t_check(void)
136 wr_id_t check[1 + (2 * -!(sizeof(wr_id_t) == sizeof(uint64_t)))];
142 /* Transpose flags. Useful to convert IBV to DPDK flags. */
143 #define TRANSPOSE(val, from, to) \
144 (((from) >= (to)) ? \
145 (((val) & (from)) / ((from) / (to))) : \
146 (((val) & (from)) * ((to) / (from))))
148 struct mlx4_rxq_stats {
149 unsigned int idx; /**< Mapping index. */
150 #ifdef MLX4_PMD_SOFT_COUNTERS
151 uint64_t ipackets; /**< Total of successfully received packets. */
152 uint64_t ibytes; /**< Total of successfully received bytes. */
154 uint64_t idropped; /**< Total of packets dropped when RX ring full. */
155 uint64_t rx_nombuf; /**< Total of RX mbuf allocation failures. */
158 struct mlx4_txq_stats {
159 unsigned int idx; /**< Mapping index. */
160 #ifdef MLX4_PMD_SOFT_COUNTERS
161 uint64_t opackets; /**< Total of successfully sent packets. */
162 uint64_t obytes; /**< Total of successfully sent bytes. */
164 uint64_t odropped; /**< Total of packets not sent when TX ring full. */
167 /* RX element (scattered packets). */
169 struct ibv_recv_wr wr; /* Work Request. */
170 struct ibv_sge sges[MLX4_PMD_SGE_WR_N]; /* Scatter/Gather Elements. */
171 struct rte_mbuf *bufs[MLX4_PMD_SGE_WR_N]; /* SGEs buffers. */
176 struct ibv_recv_wr wr; /* Work Request. */
177 struct ibv_sge sge; /* Scatter/Gather Element. */
178 /* mbuf pointer is derived from WR_ID(wr.wr_id).offset. */
181 /* RX queue descriptor. */
183 struct priv *priv; /* Back pointer to private data. */
184 struct rte_mempool *mp; /* Memory Pool for allocations. */
185 struct ibv_mr *mr; /* Memory Region (for mp). */
186 struct ibv_cq *cq; /* Completion Queue. */
187 struct ibv_qp *qp; /* Queue Pair. */
188 struct ibv_exp_qp_burst_family *if_qp; /* QP burst interface. */
189 struct ibv_exp_cq_family *if_cq; /* CQ interface. */
191 * Each VLAN ID requires a separate flow steering rule.
193 BITFIELD_DECLARE(mac_configured, uint32_t, MLX4_MAX_MAC_ADDRESSES);
194 struct ibv_flow *mac_flow[MLX4_MAX_MAC_ADDRESSES][MLX4_MAX_VLAN_IDS];
195 struct ibv_flow *promisc_flow; /* Promiscuous flow. */
196 struct ibv_flow *allmulti_flow; /* Multicast flow. */
197 unsigned int port_id; /* Port ID for incoming packets. */
198 unsigned int elts_n; /* (*elts)[] length. */
199 unsigned int elts_head; /* Current index in (*elts)[]. */
201 struct rxq_elt_sp (*sp)[]; /* Scattered RX elements. */
202 struct rxq_elt (*no_sp)[]; /* RX elements. */
204 unsigned int sp:1; /* Use scattered RX elements. */
205 unsigned int csum:1; /* Enable checksum offloading. */
206 uint32_t mb_len; /* Length of a mp-issued mbuf. */
207 struct mlx4_rxq_stats stats; /* RX queue counters. */
208 unsigned int socket; /* CPU socket ID for allocations. */
213 struct rte_mbuf *buf;
216 /* Linear buffer type. It is used when transmitting buffers with too many
217 * segments that do not fit the hardware queue (see max_send_sge).
218 * Extra segments are copied (linearized) in such buffers, replacing the
219 * last SGE during TX.
220 * The size is arbitrary but large enough to hold a jumbo frame with
221 * 8 segments considering mbuf.buf_len is about 2048 bytes. */
222 typedef uint8_t linear_t[16384];
224 /* TX queue descriptor. */
226 struct priv *priv; /* Back pointer to private data. */
228 struct rte_mempool *mp; /* Cached Memory Pool. */
229 struct ibv_mr *mr; /* Memory Region (for mp). */
230 uint32_t lkey; /* mr->lkey */
231 } mp2mr[MLX4_PMD_TX_MP_CACHE]; /* MP to MR translation table. */
232 struct ibv_cq *cq; /* Completion Queue. */
233 struct ibv_qp *qp; /* Queue Pair. */
234 struct ibv_exp_qp_burst_family *if_qp; /* QP burst interface. */
235 struct ibv_exp_cq_family *if_cq; /* CQ interface. */
236 #if MLX4_PMD_MAX_INLINE > 0
237 uint32_t max_inline; /* Max inline send size <= MLX4_PMD_MAX_INLINE. */
239 unsigned int elts_n; /* (*elts)[] length. */
240 struct txq_elt (*elts)[]; /* TX elements. */
241 unsigned int elts_head; /* Current index in (*elts)[]. */
242 unsigned int elts_tail; /* First element awaiting completion. */
243 unsigned int elts_comp; /* Number of completion requests. */
244 unsigned int elts_comp_cd; /* Countdown for next completion request. */
245 unsigned int elts_comp_cd_init; /* Initial value for countdown. */
246 struct mlx4_txq_stats stats; /* TX queue counters. */
247 linear_t (*elts_linear)[]; /* Linearized buffers. */
248 struct ibv_mr *mr_linear; /* Memory Region for linearized buffers. */
249 unsigned int socket; /* CPU socket ID for allocations. */
253 struct rte_eth_dev *dev; /* Ethernet device. */
254 struct ibv_context *ctx; /* Verbs context. */
255 struct ibv_device_attr device_attr; /* Device properties. */
256 struct ibv_pd *pd; /* Protection Domain. */
258 * MAC addresses array and configuration bit-field.
259 * An extra entry that cannot be modified by the DPDK is reserved
260 * for broadcast frames (destination MAC address ff:ff:ff:ff:ff:ff).
262 struct ether_addr mac[MLX4_MAX_MAC_ADDRESSES];
263 BITFIELD_DECLARE(mac_configured, uint32_t, MLX4_MAX_MAC_ADDRESSES);
266 unsigned int enabled:1; /* If enabled. */
267 unsigned int id:12; /* VLAN ID (0-4095). */
268 } vlan_filter[MLX4_MAX_VLAN_IDS]; /* VLAN filters table. */
269 /* Device properties. */
270 uint16_t mtu; /* Configured MTU. */
271 uint8_t port; /* Physical port number. */
272 unsigned int started:1; /* Device started, flows enabled. */
273 unsigned int promisc:1; /* Device in promiscuous mode. */
274 unsigned int allmulti:1; /* Device receives all multicast packets. */
275 unsigned int hw_qpg:1; /* QP groups are supported. */
276 unsigned int hw_tss:1; /* TSS is supported. */
277 unsigned int hw_rss:1; /* RSS is supported. */
278 unsigned int hw_csum:1; /* Checksum offload is supported. */
279 unsigned int rss:1; /* RSS is enabled. */
280 unsigned int vf:1; /* This is a VF device. */
282 unsigned int inl_recv_size; /* Inline recv size */
284 unsigned int max_rss_tbl_sz; /* Maximum number of RSS queues. */
286 struct rxq rxq_parent; /* Parent queue when RSS is enabled. */
287 unsigned int rxqs_n; /* RX queues array size. */
288 unsigned int txqs_n; /* TX queues array size. */
289 struct rxq *(*rxqs)[]; /* RX queues. */
290 struct txq *(*txqs)[]; /* TX queues. */
291 rte_spinlock_t lock; /* Lock for control functions. */
295 * Lock private structure to protect it from concurrent access in the
299 * Pointer to private structure.
302 priv_lock(struct priv *priv)
304 rte_spinlock_lock(&priv->lock);
308 * Unlock private structure.
311 * Pointer to private structure.
314 priv_unlock(struct priv *priv)
316 rte_spinlock_unlock(&priv->lock);
319 /* Allocate a buffer on the stack and fill it with a printf format string. */
320 #define MKSTR(name, ...) \
321 char name[snprintf(NULL, 0, __VA_ARGS__) + 1]; \
323 snprintf(name, sizeof(name), __VA_ARGS__)
326 * Get interface name from private structure.
329 * Pointer to private structure.
331 * Interface name output buffer.
334 * 0 on success, -1 on failure and errno is set.
337 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
341 unsigned int dev_type = 0;
342 unsigned int dev_port_prev = ~0u;
343 char match[IF_NAMESIZE] = "";
346 MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
352 while ((dent = readdir(dir)) != NULL) {
353 char *name = dent->d_name;
355 unsigned int dev_port;
358 if ((name[0] == '.') &&
359 ((name[1] == '\0') ||
360 ((name[1] == '.') && (name[2] == '\0'))))
363 MKSTR(path, "%s/device/net/%s/%s",
364 priv->ctx->device->ibdev_path, name,
365 (dev_type ? "dev_id" : "dev_port"));
367 file = fopen(path, "rb");
372 * Switch to dev_id when dev_port does not exist as
373 * is the case with Linux kernel versions < 3.15.
384 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
389 * Switch to dev_id when dev_port returns the same value for
390 * all ports. May happen when using a MOFED release older than
391 * 3.0 with a Linux kernel >= 3.15.
393 if (dev_port == dev_port_prev)
395 dev_port_prev = dev_port;
396 if (dev_port == (priv->port - 1u))
397 snprintf(match, sizeof(match), "%s", name);
400 if (match[0] == '\0')
402 strncpy(*ifname, match, sizeof(*ifname));
407 * Read from sysfs entry.
410 * Pointer to private structure.
412 * Entry name relative to sysfs path.
414 * Data output buffer.
419 * 0 on success, -1 on failure and errno is set.
422 priv_sysfs_read(const struct priv *priv, const char *entry,
423 char *buf, size_t size)
425 char ifname[IF_NAMESIZE];
430 if (priv_get_ifname(priv, &ifname))
433 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
436 file = fopen(path, "rb");
439 ret = fread(buf, 1, size, file);
441 if (((size_t)ret < size) && (ferror(file)))
451 * Write to sysfs entry.
454 * Pointer to private structure.
456 * Entry name relative to sysfs path.
463 * 0 on success, -1 on failure and errno is set.
466 priv_sysfs_write(const struct priv *priv, const char *entry,
467 char *buf, size_t size)
469 char ifname[IF_NAMESIZE];
474 if (priv_get_ifname(priv, &ifname))
477 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
480 file = fopen(path, "wb");
483 ret = fwrite(buf, 1, size, file);
485 if (((size_t)ret < size) || (ferror(file)))
495 * Get unsigned long sysfs property.
498 * Pointer to private structure.
500 * Entry name relative to sysfs path.
502 * Value output buffer.
505 * 0 on success, -1 on failure and errno is set.
508 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
511 unsigned long value_ret;
514 ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
516 DEBUG("cannot read %s value from sysfs: %s",
517 name, strerror(errno));
520 value_str[ret] = '\0';
522 value_ret = strtoul(value_str, NULL, 0);
524 DEBUG("invalid %s value `%s': %s", name, value_str,
533 * Set unsigned long sysfs property.
536 * Pointer to private structure.
538 * Entry name relative to sysfs path.
543 * 0 on success, -1 on failure and errno is set.
546 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
549 MKSTR(value_str, "%lu", value);
551 ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
553 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
554 name, value_str, value, strerror(errno));
561 * Perform ifreq ioctl() on associated Ethernet device.
564 * Pointer to private structure.
566 * Request number to pass to ioctl().
568 * Interface request structure output buffer.
571 * 0 on success, -1 on failure and errno is set.
574 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
576 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
581 if (priv_get_ifname(priv, &ifr->ifr_name) == 0)
582 ret = ioctl(sock, req, ifr);
591 * Pointer to private structure.
593 * MTU value output buffer.
596 * 0 on success, -1 on failure and errno is set.
599 priv_get_mtu(struct priv *priv, uint16_t *mtu)
601 unsigned long ulong_mtu;
603 if (priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu) == -1)
613 * Pointer to private structure.
618 * 0 on success, -1 on failure and errno is set.
621 priv_set_mtu(struct priv *priv, uint16_t mtu)
623 return priv_set_sysfs_ulong(priv, "mtu", mtu);
630 * Pointer to private structure.
632 * Bitmask for flags that must remain untouched.
634 * Bitmask for flags to modify.
637 * 0 on success, -1 on failure and errno is set.
640 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
644 if (priv_get_sysfs_ulong(priv, "flags", &tmp) == -1)
648 return priv_set_sysfs_ulong(priv, "flags", tmp);
651 /* Device configuration. */
654 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
655 unsigned int socket, const struct rte_eth_rxconf *conf,
656 struct rte_mempool *mp);
659 rxq_cleanup(struct rxq *rxq);
662 * Ethernet device configuration.
664 * Prepare the driver for a given number of TX and RX queues.
665 * Allocate parent RSS queue when several RX queues are requested.
668 * Pointer to Ethernet device structure.
671 * 0 on success, errno value on failure.
674 dev_configure(struct rte_eth_dev *dev)
676 struct priv *priv = dev->data->dev_private;
677 unsigned int rxqs_n = dev->data->nb_rx_queues;
678 unsigned int txqs_n = dev->data->nb_tx_queues;
682 priv->rxqs = (void *)dev->data->rx_queues;
683 priv->txqs = (void *)dev->data->tx_queues;
684 if (txqs_n != priv->txqs_n) {
685 INFO("%p: TX queues number update: %u -> %u",
686 (void *)dev, priv->txqs_n, txqs_n);
687 priv->txqs_n = txqs_n;
689 if (rxqs_n == priv->rxqs_n)
691 INFO("%p: RX queues number update: %u -> %u",
692 (void *)dev, priv->rxqs_n, rxqs_n);
693 /* If RSS is enabled, disable it first. */
697 /* Only if there are no remaining child RX queues. */
698 for (i = 0; (i != priv->rxqs_n); ++i)
699 if ((*priv->rxqs)[i] != NULL)
701 rxq_cleanup(&priv->rxq_parent);
706 /* Nothing else to do. */
707 priv->rxqs_n = rxqs_n;
710 /* Allocate a new RSS parent queue if supported by hardware. */
712 ERROR("%p: only a single RX queue can be configured when"
713 " hardware doesn't support RSS",
717 /* Fail if hardware doesn't support that many RSS queues. */
718 if (rxqs_n >= priv->max_rss_tbl_sz) {
719 ERROR("%p: only %u RX queues can be configured for RSS",
720 (void *)dev, priv->max_rss_tbl_sz);
725 priv->rxqs_n = rxqs_n;
726 ret = rxq_setup(dev, &priv->rxq_parent, 0, 0, NULL, NULL);
729 /* Failure, rollback. */
737 * DPDK callback for Ethernet device configuration.
740 * Pointer to Ethernet device structure.
743 * 0 on success, negative errno value on failure.
746 mlx4_dev_configure(struct rte_eth_dev *dev)
748 struct priv *priv = dev->data->dev_private;
752 ret = dev_configure(dev);
758 /* TX queues handling. */
761 * Allocate TX queue elements.
764 * Pointer to TX queue structure.
766 * Number of elements to allocate.
769 * 0 on success, errno value on failure.
772 txq_alloc_elts(struct txq *txq, unsigned int elts_n)
775 struct txq_elt (*elts)[elts_n] =
776 rte_calloc_socket("TXQ", 1, sizeof(*elts), 0, txq->socket);
777 linear_t (*elts_linear)[elts_n] =
778 rte_calloc_socket("TXQ", 1, sizeof(*elts_linear), 0,
780 struct ibv_mr *mr_linear = NULL;
783 if ((elts == NULL) || (elts_linear == NULL)) {
784 ERROR("%p: can't allocate packets array", (void *)txq);
789 ibv_reg_mr(txq->priv->pd, elts_linear, sizeof(*elts_linear),
790 (IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE));
791 if (mr_linear == NULL) {
792 ERROR("%p: unable to configure MR, ibv_reg_mr() failed",
797 for (i = 0; (i != elts_n); ++i) {
798 struct txq_elt *elt = &(*elts)[i];
802 DEBUG("%p: allocated and configured %u WRs", (void *)txq, elts_n);
803 txq->elts_n = elts_n;
808 /* Request send completion every MLX4_PMD_TX_PER_COMP_REQ packets or
809 * at least 4 times per ring. */
810 txq->elts_comp_cd_init =
811 ((MLX4_PMD_TX_PER_COMP_REQ < (elts_n / 4)) ?
812 MLX4_PMD_TX_PER_COMP_REQ : (elts_n / 4));
813 txq->elts_comp_cd = txq->elts_comp_cd_init;
814 txq->elts_linear = elts_linear;
815 txq->mr_linear = mr_linear;
819 if (mr_linear != NULL)
820 claim_zero(ibv_dereg_mr(mr_linear));
822 rte_free(elts_linear);
825 DEBUG("%p: failed, freed everything", (void *)txq);
831 * Free TX queue elements.
834 * Pointer to TX queue structure.
837 txq_free_elts(struct txq *txq)
840 unsigned int elts_n = txq->elts_n;
841 struct txq_elt (*elts)[elts_n] = txq->elts;
842 linear_t (*elts_linear)[elts_n] = txq->elts_linear;
843 struct ibv_mr *mr_linear = txq->mr_linear;
845 DEBUG("%p: freeing WRs", (void *)txq);
848 txq->elts_linear = NULL;
849 txq->mr_linear = NULL;
850 if (mr_linear != NULL)
851 claim_zero(ibv_dereg_mr(mr_linear));
853 rte_free(elts_linear);
856 for (i = 0; (i != elemof(*elts)); ++i) {
857 struct txq_elt *elt = &(*elts)[i];
859 if (elt->buf == NULL)
861 rte_pktmbuf_free(elt->buf);
868 * Clean up a TX queue.
870 * Destroy objects, free allocated memory and reset the structure for reuse.
873 * Pointer to TX queue structure.
876 txq_cleanup(struct txq *txq)
878 struct ibv_exp_release_intf_params params;
881 DEBUG("cleaning up %p", (void *)txq);
883 if (txq->if_qp != NULL) {
884 assert(txq->priv != NULL);
885 assert(txq->priv->ctx != NULL);
886 assert(txq->qp != NULL);
887 params = (struct ibv_exp_release_intf_params){
890 claim_zero(ibv_exp_release_intf(txq->priv->ctx,
894 if (txq->if_cq != NULL) {
895 assert(txq->priv != NULL);
896 assert(txq->priv->ctx != NULL);
897 assert(txq->cq != NULL);
898 params = (struct ibv_exp_release_intf_params){
901 claim_zero(ibv_exp_release_intf(txq->priv->ctx,
906 claim_zero(ibv_destroy_qp(txq->qp));
908 claim_zero(ibv_destroy_cq(txq->cq));
909 for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
910 if (txq->mp2mr[i].mp == NULL)
912 assert(txq->mp2mr[i].mr != NULL);
913 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
915 memset(txq, 0, sizeof(*txq));
919 * Manage TX completions.
921 * When sending a burst, mlx4_tx_burst() posts several WRs.
922 * To improve performance, a completion event is only required once every
923 * MLX4_PMD_TX_PER_COMP_REQ sends. Doing so discards completion information
924 * for other WRs, but this information would not be used anyway.
927 * Pointer to TX queue structure.
930 * 0 on success, -1 on failure.
933 txq_complete(struct txq *txq)
935 unsigned int elts_comp = txq->elts_comp;
936 unsigned int elts_tail = txq->elts_tail;
937 const unsigned int elts_n = txq->elts_n;
940 if (unlikely(elts_comp == 0))
943 DEBUG("%p: processing %u work requests completions",
944 (void *)txq, elts_comp);
946 wcs_n = txq->if_cq->poll_cnt(txq->cq, elts_comp);
947 if (unlikely(wcs_n == 0))
949 if (unlikely(wcs_n < 0)) {
950 DEBUG("%p: ibv_poll_cq() failed (wcs_n=%d)",
955 assert(elts_comp <= txq->elts_comp);
957 * Assume WC status is successful as nothing can be done about it
960 elts_tail += wcs_n * txq->elts_comp_cd_init;
961 if (elts_tail >= elts_n)
963 txq->elts_tail = elts_tail;
964 txq->elts_comp = elts_comp;
969 * Get Memory Region (MR) <-> Memory Pool (MP) association from txq->mp2mr[].
970 * Add MP to txq->mp2mr[] if it's not registered yet. If mp2mr[] is full,
971 * remove an entry first.
974 * Pointer to TX queue structure.
976 * Memory Pool for which a Memory Region lkey must be returned.
979 * mr->lkey on success, (uint32_t)-1 on failure.
982 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
987 for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
988 if (unlikely(txq->mp2mr[i].mp == NULL)) {
989 /* Unknown MP, add a new MR for it. */
992 if (txq->mp2mr[i].mp == mp) {
993 assert(txq->mp2mr[i].lkey != (uint32_t)-1);
994 assert(txq->mp2mr[i].mr->lkey == txq->mp2mr[i].lkey);
995 return txq->mp2mr[i].lkey;
998 /* Add a new entry, register MR first. */
999 DEBUG("%p: discovered new memory pool %p", (void *)txq, (void *)mp);
1000 mr = ibv_reg_mr(txq->priv->pd,
1001 (void *)mp->elt_va_start,
1002 (mp->elt_va_end - mp->elt_va_start),
1003 (IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE));
1004 if (unlikely(mr == NULL)) {
1005 DEBUG("%p: unable to configure MR, ibv_reg_mr() failed.",
1007 return (uint32_t)-1;
1009 if (unlikely(i == elemof(txq->mp2mr))) {
1010 /* Table is full, remove oldest entry. */
1011 DEBUG("%p: MR <-> MP table full, dropping oldest entry.",
1014 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
1015 memmove(&txq->mp2mr[0], &txq->mp2mr[1],
1016 (sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
1018 /* Store the new entry. */
1019 txq->mp2mr[i].mp = mp;
1020 txq->mp2mr[i].mr = mr;
1021 txq->mp2mr[i].lkey = mr->lkey;
1022 DEBUG("%p: new MR lkey for MP %p: 0x%08" PRIu32,
1023 (void *)txq, (void *)mp, txq->mp2mr[i].lkey);
1024 return txq->mp2mr[i].lkey;
1027 #if MLX4_PMD_SGE_WR_N > 1
1030 * Copy scattered mbuf contents to a single linear buffer.
1032 * @param[out] linear
1033 * Linear output buffer.
1035 * Scattered input buffer.
1038 * Number of bytes copied to the output buffer or 0 if not large enough.
1041 linearize_mbuf(linear_t *linear, struct rte_mbuf *buf)
1043 unsigned int size = 0;
1044 unsigned int offset;
1047 unsigned int len = DATA_LEN(buf);
1051 if (unlikely(size > sizeof(*linear)))
1053 memcpy(&(*linear)[offset],
1054 rte_pktmbuf_mtod(buf, uint8_t *),
1057 } while (buf != NULL);
1062 * Handle scattered buffers for mlx4_tx_burst().
1065 * TX queue structure.
1067 * Number of segments in buf.
1069 * TX queue element to fill.
1071 * Buffer to process.
1073 * Index of the linear buffer to use if necessary (normally txq->elts_head).
1075 * Array filled with SGEs on success.
1078 * A structure containing the processed packet size in bytes and the
1079 * number of SGEs. Both fields are set to (unsigned int)-1 in case of
1082 static struct tx_burst_sg_ret {
1083 unsigned int length;
1086 tx_burst_sg(struct txq *txq, unsigned int segs, struct txq_elt *elt,
1087 struct rte_mbuf *buf, unsigned int elts_head,
1088 struct ibv_sge (*sges)[MLX4_PMD_SGE_WR_N])
1090 unsigned int sent_size = 0;
1094 /* When there are too many segments, extra segments are
1095 * linearized in the last SGE. */
1096 if (unlikely(segs > elemof(*sges))) {
1097 segs = (elemof(*sges) - 1);
1100 /* Update element. */
1102 /* Register segments as SGEs. */
1103 for (j = 0; (j != segs); ++j) {
1104 struct ibv_sge *sge = &(*sges)[j];
1107 /* Retrieve Memory Region key for this memory pool. */
1108 lkey = txq_mp2mr(txq, buf->pool);
1109 if (unlikely(lkey == (uint32_t)-1)) {
1110 /* MR does not exist. */
1111 DEBUG("%p: unable to get MP <-> MR association",
1113 /* Clean up TX element. */
1118 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
1120 rte_prefetch0((volatile void *)
1121 (uintptr_t)sge->addr);
1122 sge->length = DATA_LEN(buf);
1124 sent_size += sge->length;
1127 /* If buf is not NULL here and is not going to be linearized,
1128 * nb_segs is not valid. */
1130 assert((buf == NULL) || (linearize));
1131 /* Linearize extra segments. */
1133 struct ibv_sge *sge = &(*sges)[segs];
1134 linear_t *linear = &(*txq->elts_linear)[elts_head];
1135 unsigned int size = linearize_mbuf(linear, buf);
1137 assert(segs == (elemof(*sges) - 1));
1139 /* Invalid packet. */
1140 DEBUG("%p: packet too large to be linearized.",
1142 /* Clean up TX element. */
1146 /* If MLX4_PMD_SGE_WR_N is 1, free mbuf immediately. */
1147 if (elemof(*sges) == 1) {
1149 struct rte_mbuf *next = NEXT(buf);
1151 rte_pktmbuf_free_seg(buf);
1153 } while (buf != NULL);
1157 sge->addr = (uintptr_t)&(*linear)[0];
1159 sge->lkey = txq->mr_linear->lkey;
1162 return (struct tx_burst_sg_ret){
1163 .length = sent_size,
1167 return (struct tx_burst_sg_ret){
1173 #endif /* MLX4_PMD_SGE_WR_N > 1 */
1176 * DPDK callback for TX.
1179 * Generic pointer to TX queue structure.
1181 * Packets to transmit.
1183 * Number of packets in array.
1186 * Number of packets successfully transmitted (<= pkts_n).
1189 mlx4_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
1191 struct txq *txq = (struct txq *)dpdk_txq;
1192 unsigned int elts_head = txq->elts_head;
1193 const unsigned int elts_tail = txq->elts_tail;
1194 const unsigned int elts_n = txq->elts_n;
1195 unsigned int elts_comp_cd = txq->elts_comp_cd;
1196 unsigned int elts_comp = 0;
1201 assert(elts_comp_cd != 0);
1203 max = (elts_n - (elts_head - elts_tail));
1207 assert(max <= elts_n);
1208 /* Always leave one free entry in the ring. */
1214 for (i = 0; (i != max); ++i) {
1215 struct rte_mbuf *buf = pkts[i];
1216 unsigned int elts_head_next =
1217 (((elts_head + 1) == elts_n) ? 0 : elts_head + 1);
1218 struct txq_elt *elt_next = &(*txq->elts)[elts_head_next];
1219 struct txq_elt *elt = &(*txq->elts)[elts_head];
1220 unsigned int segs = NB_SEGS(buf);
1221 #ifdef MLX4_PMD_SOFT_COUNTERS
1222 unsigned int sent_size = 0;
1224 uint32_t send_flags = 0;
1226 /* Clean up old buffer. */
1227 if (likely(elt->buf != NULL)) {
1228 struct rte_mbuf *tmp = elt->buf;
1230 /* Faster than rte_pktmbuf_free(). */
1232 struct rte_mbuf *next = NEXT(tmp);
1234 rte_pktmbuf_free_seg(tmp);
1236 } while (tmp != NULL);
1238 /* Request TX completion. */
1239 if (unlikely(--elts_comp_cd == 0)) {
1240 elts_comp_cd = txq->elts_comp_cd_init;
1242 send_flags |= IBV_EXP_QP_BURST_SIGNALED;
1244 /* Should we enable HW CKSUM offload */
1246 (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM))
1247 send_flags |= IBV_EXP_QP_BURST_IP_CSUM;
1248 if (likely(segs == 1)) {
1253 /* Retrieve buffer information. */
1254 addr = rte_pktmbuf_mtod(buf, uintptr_t);
1255 length = DATA_LEN(buf);
1256 /* Retrieve Memory Region key for this memory pool. */
1257 lkey = txq_mp2mr(txq, buf->pool);
1258 if (unlikely(lkey == (uint32_t)-1)) {
1259 /* MR does not exist. */
1260 DEBUG("%p: unable to get MP <-> MR"
1261 " association", (void *)txq);
1262 /* Clean up TX element. */
1266 /* Update element. */
1269 rte_prefetch0((volatile void *)
1271 RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
1272 /* Put packet into send queue. */
1273 #if MLX4_PMD_MAX_INLINE > 0
1274 if (length <= txq->max_inline)
1275 err = txq->if_qp->send_pending_inline
1282 err = txq->if_qp->send_pending
1290 #ifdef MLX4_PMD_SOFT_COUNTERS
1291 sent_size += length;
1294 #if MLX4_PMD_SGE_WR_N > 1
1295 struct ibv_sge sges[MLX4_PMD_SGE_WR_N];
1296 struct tx_burst_sg_ret ret;
1298 ret = tx_burst_sg(txq, segs, elt, buf, elts_head,
1300 if (ret.length == (unsigned int)-1)
1302 RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
1303 /* Put SG list into send queue. */
1304 err = txq->if_qp->send_pending_sg_list
1311 #ifdef MLX4_PMD_SOFT_COUNTERS
1312 sent_size += ret.length;
1314 #else /* MLX4_PMD_SGE_WR_N > 1 */
1315 DEBUG("%p: TX scattered buffers support not"
1316 " compiled in", (void *)txq);
1318 #endif /* MLX4_PMD_SGE_WR_N > 1 */
1320 elts_head = elts_head_next;
1321 #ifdef MLX4_PMD_SOFT_COUNTERS
1322 /* Increment sent bytes counter. */
1323 txq->stats.obytes += sent_size;
1327 /* Take a shortcut if nothing must be sent. */
1328 if (unlikely(i == 0))
1330 #ifdef MLX4_PMD_SOFT_COUNTERS
1331 /* Increment sent packets counter. */
1332 txq->stats.opackets += i;
1334 /* Ring QP doorbell. */
1335 err = txq->if_qp->send_flush(txq->qp);
1336 if (unlikely(err)) {
1337 /* A nonzero value is not supposed to be returned.
1338 * Nothing can be done about it. */
1339 DEBUG("%p: send_flush() failed with error %d",
1342 txq->elts_head = elts_head;
1343 txq->elts_comp += elts_comp;
1344 txq->elts_comp_cd = elts_comp_cd;
1349 * Configure a TX queue.
1352 * Pointer to Ethernet device structure.
1354 * Pointer to TX queue structure.
1356 * Number of descriptors to configure in queue.
1358 * NUMA socket on which memory must be allocated.
1360 * Thresholds parameters.
1363 * 0 on success, errno value on failure.
1366 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
1367 unsigned int socket, const struct rte_eth_txconf *conf)
1369 struct priv *priv = dev->data->dev_private;
1375 struct ibv_exp_query_intf_params params;
1376 struct ibv_qp_init_attr init;
1377 struct ibv_exp_qp_attr mod;
1379 enum ibv_exp_query_intf_status status;
1382 (void)conf; /* Thresholds configuration (ignored). */
1383 if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
1384 ERROR("%p: invalid number of TX descriptors (must be a"
1385 " multiple of %d)", (void *)dev, MLX4_PMD_SGE_WR_N);
1388 desc /= MLX4_PMD_SGE_WR_N;
1389 /* MRs will be registered in mp2mr[] later. */
1390 tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
1391 if (tmpl.cq == NULL) {
1393 ERROR("%p: CQ creation failure: %s",
1394 (void *)dev, strerror(ret));
1397 DEBUG("priv->device_attr.max_qp_wr is %d",
1398 priv->device_attr.max_qp_wr);
1399 DEBUG("priv->device_attr.max_sge is %d",
1400 priv->device_attr.max_sge);
1401 attr.init = (struct ibv_qp_init_attr){
1402 /* CQ to be associated with the send queue. */
1404 /* CQ to be associated with the receive queue. */
1407 /* Max number of outstanding WRs. */
1408 .max_send_wr = ((priv->device_attr.max_qp_wr < desc) ?
1409 priv->device_attr.max_qp_wr :
1411 /* Max number of scatter/gather elements in a WR. */
1412 .max_send_sge = ((priv->device_attr.max_sge <
1413 MLX4_PMD_SGE_WR_N) ?
1414 priv->device_attr.max_sge :
1416 #if MLX4_PMD_MAX_INLINE > 0
1417 .max_inline_data = MLX4_PMD_MAX_INLINE,
1420 .qp_type = IBV_QPT_RAW_PACKET,
1421 /* Do *NOT* enable this, completions events are managed per
1425 tmpl.qp = ibv_create_qp(priv->pd, &attr.init);
1426 if (tmpl.qp == NULL) {
1427 ret = (errno ? errno : EINVAL);
1428 ERROR("%p: QP creation failure: %s",
1429 (void *)dev, strerror(ret));
1432 #if MLX4_PMD_MAX_INLINE > 0
1433 /* ibv_create_qp() updates this value. */
1434 tmpl.max_inline = attr.init.cap.max_inline_data;
1436 attr.mod = (struct ibv_exp_qp_attr){
1437 /* Move the QP to this state. */
1438 .qp_state = IBV_QPS_INIT,
1439 /* Primary port number. */
1440 .port_num = priv->port
1442 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod,
1443 (IBV_EXP_QP_STATE | IBV_EXP_QP_PORT));
1445 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
1446 (void *)dev, strerror(ret));
1449 ret = txq_alloc_elts(&tmpl, desc);
1451 ERROR("%p: TXQ allocation failed: %s",
1452 (void *)dev, strerror(ret));
1455 attr.mod = (struct ibv_exp_qp_attr){
1456 .qp_state = IBV_QPS_RTR
1458 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1460 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
1461 (void *)dev, strerror(ret));
1464 attr.mod.qp_state = IBV_QPS_RTS;
1465 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1467 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
1468 (void *)dev, strerror(ret));
1471 attr.params = (struct ibv_exp_query_intf_params){
1472 .intf_scope = IBV_EXP_INTF_GLOBAL,
1473 .intf = IBV_EXP_INTF_CQ,
1476 tmpl.if_cq = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
1477 if (tmpl.if_cq == NULL) {
1478 ERROR("%p: CQ interface family query failed with status %d",
1479 (void *)dev, status);
1482 attr.params = (struct ibv_exp_query_intf_params){
1483 .intf_scope = IBV_EXP_INTF_GLOBAL,
1484 .intf = IBV_EXP_INTF_QP_BURST,
1487 tmpl.if_qp = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
1488 if (tmpl.if_qp == NULL) {
1489 ERROR("%p: QP interface family query failed with status %d",
1490 (void *)dev, status);
1493 /* Clean up txq in case we're reinitializing it. */
1494 DEBUG("%p: cleaning-up old txq just in case", (void *)txq);
1497 DEBUG("%p: txq updated with %p", (void *)txq, (void *)&tmpl);
1507 * DPDK callback to configure a TX queue.
1510 * Pointer to Ethernet device structure.
1514 * Number of descriptors to configure in queue.
1516 * NUMA socket on which memory must be allocated.
1518 * Thresholds parameters.
1521 * 0 on success, negative errno value on failure.
1524 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1525 unsigned int socket, const struct rte_eth_txconf *conf)
1527 struct priv *priv = dev->data->dev_private;
1528 struct txq *txq = (*priv->txqs)[idx];
1532 DEBUG("%p: configuring queue %u for %u descriptors",
1533 (void *)dev, idx, desc);
1534 if (idx >= priv->txqs_n) {
1535 ERROR("%p: queue index out of range (%u >= %u)",
1536 (void *)dev, idx, priv->txqs_n);
1541 DEBUG("%p: reusing already allocated queue index %u (%p)",
1542 (void *)dev, idx, (void *)txq);
1543 if (priv->started) {
1547 (*priv->txqs)[idx] = NULL;
1550 txq = rte_calloc_socket("TXQ", 1, sizeof(*txq), 0, socket);
1552 ERROR("%p: unable to allocate queue index %u",
1558 ret = txq_setup(dev, txq, desc, socket, conf);
1562 txq->stats.idx = idx;
1563 DEBUG("%p: adding TX queue %p to list",
1564 (void *)dev, (void *)txq);
1565 (*priv->txqs)[idx] = txq;
1566 /* Update send callback. */
1567 dev->tx_pkt_burst = mlx4_tx_burst;
1574 * DPDK callback to release a TX queue.
1577 * Generic TX queue pointer.
1580 mlx4_tx_queue_release(void *dpdk_txq)
1582 struct txq *txq = (struct txq *)dpdk_txq;
1590 for (i = 0; (i != priv->txqs_n); ++i)
1591 if ((*priv->txqs)[i] == txq) {
1592 DEBUG("%p: removing TX queue %p from list",
1593 (void *)priv->dev, (void *)txq);
1594 (*priv->txqs)[i] = NULL;
1602 /* RX queues handling. */
1605 * Allocate RX queue elements with scattered packets support.
1608 * Pointer to RX queue structure.
1610 * Number of elements to allocate.
1612 * If not NULL, fetch buffers from this array instead of allocating them
1613 * with rte_pktmbuf_alloc().
1616 * 0 on success, errno value on failure.
1619 rxq_alloc_elts_sp(struct rxq *rxq, unsigned int elts_n,
1620 struct rte_mbuf **pool)
1623 struct rxq_elt_sp (*elts)[elts_n] =
1624 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1629 ERROR("%p: can't allocate packets array", (void *)rxq);
1633 /* For each WR (packet). */
1634 for (i = 0; (i != elts_n); ++i) {
1636 struct rxq_elt_sp *elt = &(*elts)[i];
1637 struct ibv_recv_wr *wr = &elt->wr;
1638 struct ibv_sge (*sges)[(elemof(elt->sges))] = &elt->sges;
1640 /* These two arrays must have the same size. */
1641 assert(elemof(elt->sges) == elemof(elt->bufs));
1644 wr->next = &(*elts)[(i + 1)].wr;
1645 wr->sg_list = &(*sges)[0];
1646 wr->num_sge = elemof(*sges);
1647 /* For each SGE (segment). */
1648 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1649 struct ibv_sge *sge = &(*sges)[j];
1650 struct rte_mbuf *buf;
1654 assert(buf != NULL);
1655 rte_pktmbuf_reset(buf);
1657 buf = rte_pktmbuf_alloc(rxq->mp);
1659 assert(pool == NULL);
1660 ERROR("%p: empty mbuf pool", (void *)rxq);
1665 /* Headroom is reserved by rte_pktmbuf_alloc(). */
1666 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1667 /* Buffer is supposed to be empty. */
1668 assert(rte_pktmbuf_data_len(buf) == 0);
1669 assert(rte_pktmbuf_pkt_len(buf) == 0);
1670 /* sge->addr must be able to store a pointer. */
1671 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1673 /* The first SGE keeps its headroom. */
1674 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
1675 sge->length = (buf->buf_len -
1676 RTE_PKTMBUF_HEADROOM);
1678 /* Subsequent SGEs lose theirs. */
1679 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1680 SET_DATA_OFF(buf, 0);
1681 sge->addr = (uintptr_t)buf->buf_addr;
1682 sge->length = buf->buf_len;
1684 sge->lkey = rxq->mr->lkey;
1685 /* Redundant check for tailroom. */
1686 assert(sge->length == rte_pktmbuf_tailroom(buf));
1689 /* The last WR pointer must be NULL. */
1690 (*elts)[(i - 1)].wr.next = NULL;
1691 DEBUG("%p: allocated and configured %u WRs (%zu segments)",
1692 (void *)rxq, elts_n, (elts_n * elemof((*elts)[0].sges)));
1693 rxq->elts_n = elts_n;
1695 rxq->elts.sp = elts;
1700 assert(pool == NULL);
1701 for (i = 0; (i != elemof(*elts)); ++i) {
1703 struct rxq_elt_sp *elt = &(*elts)[i];
1705 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1706 struct rte_mbuf *buf = elt->bufs[j];
1709 rte_pktmbuf_free_seg(buf);
1714 DEBUG("%p: failed, freed everything", (void *)rxq);
1720 * Free RX queue elements with scattered packets support.
1723 * Pointer to RX queue structure.
1726 rxq_free_elts_sp(struct rxq *rxq)
1729 unsigned int elts_n = rxq->elts_n;
1730 struct rxq_elt_sp (*elts)[elts_n] = rxq->elts.sp;
1732 DEBUG("%p: freeing WRs", (void *)rxq);
1734 rxq->elts.sp = NULL;
1737 for (i = 0; (i != elemof(*elts)); ++i) {
1739 struct rxq_elt_sp *elt = &(*elts)[i];
1741 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1742 struct rte_mbuf *buf = elt->bufs[j];
1745 rte_pktmbuf_free_seg(buf);
1752 * Allocate RX queue elements.
1755 * Pointer to RX queue structure.
1757 * Number of elements to allocate.
1759 * If not NULL, fetch buffers from this array instead of allocating them
1760 * with rte_pktmbuf_alloc().
1763 * 0 on success, errno value on failure.
1766 rxq_alloc_elts(struct rxq *rxq, unsigned int elts_n, struct rte_mbuf **pool)
1769 struct rxq_elt (*elts)[elts_n] =
1770 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1775 ERROR("%p: can't allocate packets array", (void *)rxq);
1779 /* For each WR (packet). */
1780 for (i = 0; (i != elts_n); ++i) {
1781 struct rxq_elt *elt = &(*elts)[i];
1782 struct ibv_recv_wr *wr = &elt->wr;
1783 struct ibv_sge *sge = &(*elts)[i].sge;
1784 struct rte_mbuf *buf;
1788 assert(buf != NULL);
1789 rte_pktmbuf_reset(buf);
1791 buf = rte_pktmbuf_alloc(rxq->mp);
1793 assert(pool == NULL);
1794 ERROR("%p: empty mbuf pool", (void *)rxq);
1798 /* Configure WR. Work request ID contains its own index in
1799 * the elts array and the offset between SGE buffer header and
1801 WR_ID(wr->wr_id).id = i;
1802 WR_ID(wr->wr_id).offset =
1803 (((uintptr_t)buf->buf_addr + RTE_PKTMBUF_HEADROOM) -
1805 wr->next = &(*elts)[(i + 1)].wr;
1808 /* Headroom is reserved by rte_pktmbuf_alloc(). */
1809 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1810 /* Buffer is supposed to be empty. */
1811 assert(rte_pktmbuf_data_len(buf) == 0);
1812 assert(rte_pktmbuf_pkt_len(buf) == 0);
1813 /* sge->addr must be able to store a pointer. */
1814 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1815 /* SGE keeps its headroom. */
1816 sge->addr = (uintptr_t)
1817 ((uint8_t *)buf->buf_addr + RTE_PKTMBUF_HEADROOM);
1818 sge->length = (buf->buf_len - RTE_PKTMBUF_HEADROOM);
1819 sge->lkey = rxq->mr->lkey;
1820 /* Redundant check for tailroom. */
1821 assert(sge->length == rte_pktmbuf_tailroom(buf));
1822 /* Make sure elts index and SGE mbuf pointer can be deduced
1824 if ((WR_ID(wr->wr_id).id != i) ||
1825 ((void *)((uintptr_t)sge->addr -
1826 WR_ID(wr->wr_id).offset) != buf)) {
1827 ERROR("%p: cannot store index and offset in WR ID",
1830 rte_pktmbuf_free(buf);
1835 /* The last WR pointer must be NULL. */
1836 (*elts)[(i - 1)].wr.next = NULL;
1837 DEBUG("%p: allocated and configured %u single-segment WRs",
1838 (void *)rxq, elts_n);
1839 rxq->elts_n = elts_n;
1841 rxq->elts.no_sp = elts;
1846 assert(pool == NULL);
1847 for (i = 0; (i != elemof(*elts)); ++i) {
1848 struct rxq_elt *elt = &(*elts)[i];
1849 struct rte_mbuf *buf;
1851 if (elt->sge.addr == 0)
1853 assert(WR_ID(elt->wr.wr_id).id == i);
1854 buf = (void *)((uintptr_t)elt->sge.addr -
1855 WR_ID(elt->wr.wr_id).offset);
1856 rte_pktmbuf_free_seg(buf);
1860 DEBUG("%p: failed, freed everything", (void *)rxq);
1866 * Free RX queue elements.
1869 * Pointer to RX queue structure.
1872 rxq_free_elts(struct rxq *rxq)
1875 unsigned int elts_n = rxq->elts_n;
1876 struct rxq_elt (*elts)[elts_n] = rxq->elts.no_sp;
1878 DEBUG("%p: freeing WRs", (void *)rxq);
1880 rxq->elts.no_sp = NULL;
1883 for (i = 0; (i != elemof(*elts)); ++i) {
1884 struct rxq_elt *elt = &(*elts)[i];
1885 struct rte_mbuf *buf;
1887 if (elt->sge.addr == 0)
1889 assert(WR_ID(elt->wr.wr_id).id == i);
1890 buf = (void *)((uintptr_t)elt->sge.addr -
1891 WR_ID(elt->wr.wr_id).offset);
1892 rte_pktmbuf_free_seg(buf);
1898 * Delete flow steering rule.
1901 * Pointer to RX queue structure.
1903 * MAC address index.
1908 rxq_del_flow(struct rxq *rxq, unsigned int mac_index, unsigned int vlan_index)
1911 struct priv *priv = rxq->priv;
1912 const uint8_t (*mac)[ETHER_ADDR_LEN] =
1913 (const uint8_t (*)[ETHER_ADDR_LEN])
1914 priv->mac[mac_index].addr_bytes;
1916 assert(rxq->mac_flow[mac_index][vlan_index] != NULL);
1917 DEBUG("%p: removing MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
1918 " (VLAN ID %" PRIu16 ")",
1920 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
1921 mac_index, priv->vlan_filter[vlan_index].id);
1922 claim_zero(ibv_destroy_flow(rxq->mac_flow[mac_index][vlan_index]));
1923 rxq->mac_flow[mac_index][vlan_index] = NULL;
1927 * Unregister a MAC address from a RX queue.
1930 * Pointer to RX queue structure.
1932 * MAC address index.
1935 rxq_mac_addr_del(struct rxq *rxq, unsigned int mac_index)
1937 struct priv *priv = rxq->priv;
1939 unsigned int vlans = 0;
1941 assert(mac_index < elemof(priv->mac));
1942 if (!BITFIELD_ISSET(rxq->mac_configured, mac_index))
1944 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
1945 if (!priv->vlan_filter[i].enabled)
1947 rxq_del_flow(rxq, mac_index, i);
1951 rxq_del_flow(rxq, mac_index, 0);
1953 BITFIELD_RESET(rxq->mac_configured, mac_index);
1957 * Unregister all MAC addresses from a RX queue.
1960 * Pointer to RX queue structure.
1963 rxq_mac_addrs_del(struct rxq *rxq)
1965 struct priv *priv = rxq->priv;
1968 for (i = 0; (i != elemof(priv->mac)); ++i)
1969 rxq_mac_addr_del(rxq, i);
1972 static int rxq_promiscuous_enable(struct rxq *);
1973 static void rxq_promiscuous_disable(struct rxq *);
1976 * Add single flow steering rule.
1979 * Pointer to RX queue structure.
1981 * MAC address index to register.
1983 * VLAN index. Use -1 for a flow without VLAN.
1986 * 0 on success, errno value on failure.
1989 rxq_add_flow(struct rxq *rxq, unsigned int mac_index, unsigned int vlan_index)
1991 struct ibv_flow *flow;
1992 struct priv *priv = rxq->priv;
1993 const uint8_t (*mac)[ETHER_ADDR_LEN] =
1994 (const uint8_t (*)[ETHER_ADDR_LEN])
1995 priv->mac[mac_index].addr_bytes;
1997 /* Allocate flow specification on the stack. */
1998 struct __attribute__((packed)) {
1999 struct ibv_flow_attr attr;
2000 struct ibv_flow_spec_eth spec;
2002 struct ibv_flow_attr *attr = &data.attr;
2003 struct ibv_flow_spec_eth *spec = &data.spec;
2005 assert(mac_index < elemof(priv->mac));
2006 assert((vlan_index < elemof(priv->vlan_filter)) || (vlan_index == -1u));
2008 * No padding must be inserted by the compiler between attr and spec.
2009 * This layout is expected by libibverbs.
2011 assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec);
2012 *attr = (struct ibv_flow_attr){
2013 .type = IBV_FLOW_ATTR_NORMAL,
2018 *spec = (struct ibv_flow_spec_eth){
2019 .type = IBV_FLOW_SPEC_ETH,
2020 .size = sizeof(*spec),
2023 (*mac)[0], (*mac)[1], (*mac)[2],
2024 (*mac)[3], (*mac)[4], (*mac)[5]
2026 .vlan_tag = ((vlan_index != -1u) ?
2027 htons(priv->vlan_filter[vlan_index].id) :
2031 .dst_mac = "\xff\xff\xff\xff\xff\xff",
2032 .vlan_tag = ((vlan_index != -1u) ? htons(0xfff) : 0),
2035 DEBUG("%p: adding MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
2036 " (VLAN %s %" PRIu16 ")",
2038 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
2040 ((vlan_index != -1u) ? "ID" : "index"),
2041 ((vlan_index != -1u) ? priv->vlan_filter[vlan_index].id : -1u));
2042 /* Create related flow. */
2044 flow = ibv_create_flow(rxq->qp, attr);
2046 /* It's not clear whether errno is always set in this case. */
2047 ERROR("%p: flow configuration failed, errno=%d: %s",
2049 (errno ? strerror(errno) : "Unknown error"));
2054 if (vlan_index == -1u)
2056 assert(rxq->mac_flow[mac_index][vlan_index] == NULL);
2057 rxq->mac_flow[mac_index][vlan_index] = flow;
2062 * Register a MAC address in a RX queue.
2065 * Pointer to RX queue structure.
2067 * MAC address index to register.
2070 * 0 on success, errno value on failure.
2073 rxq_mac_addr_add(struct rxq *rxq, unsigned int mac_index)
2075 struct priv *priv = rxq->priv;
2077 unsigned int vlans = 0;
2080 assert(mac_index < elemof(priv->mac));
2081 if (BITFIELD_ISSET(rxq->mac_configured, mac_index))
2082 rxq_mac_addr_del(rxq, mac_index);
2083 /* Fill VLAN specifications. */
2084 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
2085 if (!priv->vlan_filter[i].enabled)
2087 /* Create related flow. */
2088 ret = rxq_add_flow(rxq, mac_index, i);
2093 /* Failure, rollback. */
2095 if (priv->vlan_filter[--i].enabled)
2096 rxq_del_flow(rxq, mac_index, i);
2100 /* In case there is no VLAN filter. */
2102 ret = rxq_add_flow(rxq, mac_index, -1);
2106 BITFIELD_SET(rxq->mac_configured, mac_index);
2111 * Register all MAC addresses in a RX queue.
2114 * Pointer to RX queue structure.
2117 * 0 on success, errno value on failure.
2120 rxq_mac_addrs_add(struct rxq *rxq)
2122 struct priv *priv = rxq->priv;
2126 for (i = 0; (i != elemof(priv->mac)); ++i) {
2127 if (!BITFIELD_ISSET(priv->mac_configured, i))
2129 ret = rxq_mac_addr_add(rxq, i);
2132 /* Failure, rollback. */
2134 rxq_mac_addr_del(rxq, --i);
2142 * Unregister a MAC address.
2144 * In RSS mode, the MAC address is unregistered from the parent queue,
2145 * otherwise it is unregistered from each queue directly.
2148 * Pointer to private structure.
2150 * MAC address index.
2153 priv_mac_addr_del(struct priv *priv, unsigned int mac_index)
2157 assert(mac_index < elemof(priv->mac));
2158 if (!BITFIELD_ISSET(priv->mac_configured, mac_index))
2161 rxq_mac_addr_del(&priv->rxq_parent, mac_index);
2164 for (i = 0; (i != priv->dev->data->nb_rx_queues); ++i)
2165 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2167 BITFIELD_RESET(priv->mac_configured, mac_index);
2171 * Register a MAC address.
2173 * In RSS mode, the MAC address is registered in the parent queue,
2174 * otherwise it is registered in each queue directly.
2177 * Pointer to private structure.
2179 * MAC address index to use.
2181 * MAC address to register.
2184 * 0 on success, errno value on failure.
2187 priv_mac_addr_add(struct priv *priv, unsigned int mac_index,
2188 const uint8_t (*mac)[ETHER_ADDR_LEN])
2193 assert(mac_index < elemof(priv->mac));
2194 /* First, make sure this address isn't already configured. */
2195 for (i = 0; (i != elemof(priv->mac)); ++i) {
2196 /* Skip this index, it's going to be reconfigured. */
2199 if (!BITFIELD_ISSET(priv->mac_configured, i))
2201 if (memcmp(priv->mac[i].addr_bytes, *mac, sizeof(*mac)))
2203 /* Address already configured elsewhere, return with error. */
2206 if (BITFIELD_ISSET(priv->mac_configured, mac_index))
2207 priv_mac_addr_del(priv, mac_index);
2208 priv->mac[mac_index] = (struct ether_addr){
2210 (*mac)[0], (*mac)[1], (*mac)[2],
2211 (*mac)[3], (*mac)[4], (*mac)[5]
2214 /* If device isn't started, this is all we need to do. */
2215 if (!priv->started) {
2217 /* Verify that all queues have this index disabled. */
2218 for (i = 0; (i != priv->rxqs_n); ++i) {
2219 if ((*priv->rxqs)[i] == NULL)
2221 assert(!BITFIELD_ISSET
2222 ((*priv->rxqs)[i]->mac_configured, mac_index));
2228 ret = rxq_mac_addr_add(&priv->rxq_parent, mac_index);
2233 for (i = 0; (i != priv->rxqs_n); ++i) {
2234 if ((*priv->rxqs)[i] == NULL)
2236 ret = rxq_mac_addr_add((*priv->rxqs)[i], mac_index);
2239 /* Failure, rollback. */
2241 if ((*priv->rxqs)[(--i)] != NULL)
2242 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2246 BITFIELD_SET(priv->mac_configured, mac_index);
2251 * Enable allmulti mode in a RX queue.
2254 * Pointer to RX queue structure.
2257 * 0 on success, errno value on failure.
2260 rxq_allmulticast_enable(struct rxq *rxq)
2262 struct ibv_flow *flow;
2263 struct ibv_flow_attr attr = {
2264 .type = IBV_FLOW_ATTR_MC_DEFAULT,
2266 .port = rxq->priv->port,
2270 DEBUG("%p: enabling allmulticast mode", (void *)rxq);
2271 if (rxq->allmulti_flow != NULL)
2274 flow = ibv_create_flow(rxq->qp, &attr);
2276 /* It's not clear whether errno is always set in this case. */
2277 ERROR("%p: flow configuration failed, errno=%d: %s",
2279 (errno ? strerror(errno) : "Unknown error"));
2284 rxq->allmulti_flow = flow;
2285 DEBUG("%p: allmulticast mode enabled", (void *)rxq);
2290 * Disable allmulti mode in a RX queue.
2293 * Pointer to RX queue structure.
2296 rxq_allmulticast_disable(struct rxq *rxq)
2298 DEBUG("%p: disabling allmulticast mode", (void *)rxq);
2299 if (rxq->allmulti_flow == NULL)
2301 claim_zero(ibv_destroy_flow(rxq->allmulti_flow));
2302 rxq->allmulti_flow = NULL;
2303 DEBUG("%p: allmulticast mode disabled", (void *)rxq);
2307 * Enable promiscuous mode in a RX queue.
2310 * Pointer to RX queue structure.
2313 * 0 on success, errno value on failure.
2316 rxq_promiscuous_enable(struct rxq *rxq)
2318 struct ibv_flow *flow;
2319 struct ibv_flow_attr attr = {
2320 .type = IBV_FLOW_ATTR_ALL_DEFAULT,
2322 .port = rxq->priv->port,
2328 DEBUG("%p: enabling promiscuous mode", (void *)rxq);
2329 if (rxq->promisc_flow != NULL)
2332 flow = ibv_create_flow(rxq->qp, &attr);
2334 /* It's not clear whether errno is always set in this case. */
2335 ERROR("%p: flow configuration failed, errno=%d: %s",
2337 (errno ? strerror(errno) : "Unknown error"));
2342 rxq->promisc_flow = flow;
2343 DEBUG("%p: promiscuous mode enabled", (void *)rxq);
2348 * Disable promiscuous mode in a RX queue.
2351 * Pointer to RX queue structure.
2354 rxq_promiscuous_disable(struct rxq *rxq)
2358 DEBUG("%p: disabling promiscuous mode", (void *)rxq);
2359 if (rxq->promisc_flow == NULL)
2361 claim_zero(ibv_destroy_flow(rxq->promisc_flow));
2362 rxq->promisc_flow = NULL;
2363 DEBUG("%p: promiscuous mode disabled", (void *)rxq);
2367 * Clean up a RX queue.
2369 * Destroy objects, free allocated memory and reset the structure for reuse.
2372 * Pointer to RX queue structure.
2375 rxq_cleanup(struct rxq *rxq)
2377 struct ibv_exp_release_intf_params params;
2379 DEBUG("cleaning up %p", (void *)rxq);
2381 rxq_free_elts_sp(rxq);
2384 if (rxq->if_qp != NULL) {
2385 assert(rxq->priv != NULL);
2386 assert(rxq->priv->ctx != NULL);
2387 assert(rxq->qp != NULL);
2388 params = (struct ibv_exp_release_intf_params){
2391 claim_zero(ibv_exp_release_intf(rxq->priv->ctx,
2395 if (rxq->if_cq != NULL) {
2396 assert(rxq->priv != NULL);
2397 assert(rxq->priv->ctx != NULL);
2398 assert(rxq->cq != NULL);
2399 params = (struct ibv_exp_release_intf_params){
2402 claim_zero(ibv_exp_release_intf(rxq->priv->ctx,
2406 if (rxq->qp != NULL) {
2407 rxq_promiscuous_disable(rxq);
2408 rxq_allmulticast_disable(rxq);
2409 rxq_mac_addrs_del(rxq);
2410 claim_zero(ibv_destroy_qp(rxq->qp));
2412 if (rxq->cq != NULL)
2413 claim_zero(ibv_destroy_cq(rxq->cq));
2414 if (rxq->mr != NULL)
2415 claim_zero(ibv_dereg_mr(rxq->mr));
2416 memset(rxq, 0, sizeof(*rxq));
2420 * Translate RX completion flags to offload flags.
2423 * Pointer to RX queue structure.
2425 * RX completion flags returned by poll_length_flags().
2428 * Offload flags (ol_flags) for struct rte_mbuf.
2430 static inline uint32_t
2431 rxq_cq_to_ol_flags(const struct rxq *rxq, uint32_t flags)
2436 TRANSPOSE(flags, IBV_EXP_CQ_RX_IPV4_PACKET, PKT_RX_IPV4_HDR) |
2437 TRANSPOSE(flags, IBV_EXP_CQ_RX_IPV6_PACKET, PKT_RX_IPV6_HDR);
2441 IBV_EXP_CQ_RX_IP_CSUM_OK,
2442 PKT_RX_IP_CKSUM_BAD) |
2444 IBV_EXP_CQ_RX_TCP_UDP_CSUM_OK,
2445 PKT_RX_L4_CKSUM_BAD);
2450 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n);
2453 * DPDK callback for RX with scattered packets support.
2456 * Generic pointer to RX queue structure.
2458 * Array to store received packets.
2460 * Maximum number of packets in array.
2463 * Number of packets successfully received (<= pkts_n).
2466 mlx4_rx_burst_sp(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2468 struct rxq *rxq = (struct rxq *)dpdk_rxq;
2469 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
2470 const unsigned int elts_n = rxq->elts_n;
2471 unsigned int elts_head = rxq->elts_head;
2472 struct ibv_recv_wr head;
2473 struct ibv_recv_wr **next = &head.next;
2474 struct ibv_recv_wr *bad_wr;
2476 unsigned int pkts_ret = 0;
2479 if (unlikely(!rxq->sp))
2480 return mlx4_rx_burst(dpdk_rxq, pkts, pkts_n);
2481 if (unlikely(elts == NULL)) /* See RTE_DEV_CMD_SET_MTU. */
2483 for (i = 0; (i != pkts_n); ++i) {
2484 struct rxq_elt_sp *elt = &(*elts)[elts_head];
2485 struct ibv_recv_wr *wr = &elt->wr;
2486 uint64_t wr_id = wr->wr_id;
2488 unsigned int pkt_buf_len;
2489 struct rte_mbuf *pkt_buf = NULL; /* Buffer returned in pkts. */
2490 struct rte_mbuf **pkt_buf_next = &pkt_buf;
2491 unsigned int seg_headroom = RTE_PKTMBUF_HEADROOM;
2495 /* Sanity checks. */
2499 assert(wr_id < rxq->elts_n);
2500 assert(wr->sg_list == elt->sges);
2501 assert(wr->num_sge == elemof(elt->sges));
2502 assert(elts_head < rxq->elts_n);
2503 assert(rxq->elts_head < rxq->elts_n);
2504 ret = rxq->if_cq->poll_length_flags(rxq->cq, NULL, NULL,
2506 if (unlikely(ret < 0)) {
2510 DEBUG("rxq=%p, poll_length() failed (ret=%d)",
2512 /* ibv_poll_cq() must be used in case of failure. */
2513 wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
2514 if (unlikely(wcs_n == 0))
2516 if (unlikely(wcs_n < 0)) {
2517 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
2518 (void *)rxq, wcs_n);
2522 if (unlikely(wc.status != IBV_WC_SUCCESS)) {
2523 /* Whatever, just repost the offending WR. */
2524 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
2525 " completion status (%d): %s",
2526 (void *)rxq, wc.wr_id, wc.status,
2527 ibv_wc_status_str(wc.status));
2528 #ifdef MLX4_PMD_SOFT_COUNTERS
2529 /* Increment dropped packets counter. */
2530 ++rxq->stats.idropped;
2532 /* Link completed WRs together for repost. */
2543 /* Link completed WRs together for repost. */
2547 * Replace spent segments with new ones, concatenate and
2548 * return them as pkt_buf.
2551 struct ibv_sge *sge = &elt->sges[j];
2552 struct rte_mbuf *seg = elt->bufs[j];
2553 struct rte_mbuf *rep;
2554 unsigned int seg_tailroom;
2557 * Fetch initial bytes of packet descriptor into a
2558 * cacheline while allocating rep.
2561 rep = __rte_mbuf_raw_alloc(rxq->mp);
2562 if (unlikely(rep == NULL)) {
2564 * Unable to allocate a replacement mbuf,
2567 DEBUG("rxq=%p, wr_id=%" PRIu64 ":"
2568 " can't allocate a new mbuf",
2569 (void *)rxq, wr_id);
2570 if (pkt_buf != NULL) {
2571 *pkt_buf_next = NULL;
2572 rte_pktmbuf_free(pkt_buf);
2574 /* Increase out of memory counters. */
2575 ++rxq->stats.rx_nombuf;
2576 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
2580 /* Poison user-modifiable fields in rep. */
2581 NEXT(rep) = (void *)((uintptr_t)-1);
2582 SET_DATA_OFF(rep, 0xdead);
2583 DATA_LEN(rep) = 0xd00d;
2584 PKT_LEN(rep) = 0xdeadd00d;
2585 NB_SEGS(rep) = 0x2a;
2589 assert(rep->buf_len == seg->buf_len);
2590 assert(rep->buf_len == rxq->mb_len);
2591 /* Reconfigure sge to use rep instead of seg. */
2592 assert(sge->lkey == rxq->mr->lkey);
2593 sge->addr = ((uintptr_t)rep->buf_addr + seg_headroom);
2596 /* Update pkt_buf if it's the first segment, or link
2597 * seg to the previous one and update pkt_buf_next. */
2598 *pkt_buf_next = seg;
2599 pkt_buf_next = &NEXT(seg);
2600 /* Update seg information. */
2601 seg_tailroom = (seg->buf_len - seg_headroom);
2602 assert(sge->length == seg_tailroom);
2603 SET_DATA_OFF(seg, seg_headroom);
2604 if (likely(len <= seg_tailroom)) {
2606 DATA_LEN(seg) = len;
2609 assert(rte_pktmbuf_headroom(seg) ==
2611 assert(rte_pktmbuf_tailroom(seg) ==
2612 (seg_tailroom - len));
2615 DATA_LEN(seg) = seg_tailroom;
2616 PKT_LEN(seg) = seg_tailroom;
2618 assert(rte_pktmbuf_headroom(seg) == seg_headroom);
2619 assert(rte_pktmbuf_tailroom(seg) == 0);
2620 /* Fix len and clear headroom for next segments. */
2621 len -= seg_tailroom;
2624 /* Update head and tail segments. */
2625 *pkt_buf_next = NULL;
2626 assert(pkt_buf != NULL);
2628 NB_SEGS(pkt_buf) = j;
2629 PORT(pkt_buf) = rxq->port_id;
2630 PKT_LEN(pkt_buf) = pkt_buf_len;
2631 pkt_buf->ol_flags = rxq_cq_to_ol_flags(rxq, flags);
2633 /* Return packet. */
2634 *(pkts++) = pkt_buf;
2636 #ifdef MLX4_PMD_SOFT_COUNTERS
2637 /* Increase bytes counter. */
2638 rxq->stats.ibytes += pkt_buf_len;
2641 if (++elts_head >= elts_n)
2645 if (unlikely(i == 0))
2650 DEBUG("%p: reposting %d WRs", (void *)rxq, i);
2652 ret = ibv_post_recv(rxq->qp, head.next, &bad_wr);
2653 if (unlikely(ret)) {
2654 /* Inability to repost WRs is fatal. */
2655 DEBUG("%p: ibv_post_recv(): failed for WR %p: %s",
2661 rxq->elts_head = elts_head;
2662 #ifdef MLX4_PMD_SOFT_COUNTERS
2663 /* Increase packets counter. */
2664 rxq->stats.ipackets += pkts_ret;
2670 * DPDK callback for RX.
2672 * The following function is the same as mlx4_rx_burst_sp(), except it doesn't
2673 * manage scattered packets. Improves performance when MRU is lower than the
2674 * size of the first segment.
2677 * Generic pointer to RX queue structure.
2679 * Array to store received packets.
2681 * Maximum number of packets in array.
2684 * Number of packets successfully received (<= pkts_n).
2687 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2689 struct rxq *rxq = (struct rxq *)dpdk_rxq;
2690 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
2691 const unsigned int elts_n = rxq->elts_n;
2692 unsigned int elts_head = rxq->elts_head;
2693 struct ibv_sge sges[pkts_n];
2695 unsigned int pkts_ret = 0;
2698 if (unlikely(rxq->sp))
2699 return mlx4_rx_burst_sp(dpdk_rxq, pkts, pkts_n);
2700 for (i = 0; (i != pkts_n); ++i) {
2701 struct rxq_elt *elt = &(*elts)[elts_head];
2702 struct ibv_recv_wr *wr = &elt->wr;
2703 uint64_t wr_id = wr->wr_id;
2705 struct rte_mbuf *seg = (void *)((uintptr_t)elt->sge.addr -
2706 WR_ID(wr_id).offset);
2707 struct rte_mbuf *rep;
2710 /* Sanity checks. */
2711 assert(WR_ID(wr_id).id < rxq->elts_n);
2712 assert(wr->sg_list == &elt->sge);
2713 assert(wr->num_sge == 1);
2714 assert(elts_head < rxq->elts_n);
2715 assert(rxq->elts_head < rxq->elts_n);
2716 ret = rxq->if_cq->poll_length_flags(rxq->cq, NULL, NULL,
2718 if (unlikely(ret < 0)) {
2722 DEBUG("rxq=%p, poll_length() failed (ret=%d)",
2724 /* ibv_poll_cq() must be used in case of failure. */
2725 wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
2726 if (unlikely(wcs_n == 0))
2728 if (unlikely(wcs_n < 0)) {
2729 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
2730 (void *)rxq, wcs_n);
2734 if (unlikely(wc.status != IBV_WC_SUCCESS)) {
2735 /* Whatever, just repost the offending WR. */
2736 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
2737 " completion status (%d): %s",
2738 (void *)rxq, wc.wr_id, wc.status,
2739 ibv_wc_status_str(wc.status));
2740 #ifdef MLX4_PMD_SOFT_COUNTERS
2741 /* Increment dropped packets counter. */
2742 ++rxq->stats.idropped;
2744 /* Add SGE to array for repost. */
2754 * Fetch initial bytes of packet descriptor into a
2755 * cacheline while allocating rep.
2758 rep = __rte_mbuf_raw_alloc(rxq->mp);
2759 if (unlikely(rep == NULL)) {
2761 * Unable to allocate a replacement mbuf,
2764 DEBUG("rxq=%p, wr_id=%" PRIu32 ":"
2765 " can't allocate a new mbuf",
2766 (void *)rxq, WR_ID(wr_id).id);
2767 /* Increase out of memory counters. */
2768 ++rxq->stats.rx_nombuf;
2769 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
2773 /* Reconfigure sge to use rep instead of seg. */
2774 elt->sge.addr = (uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM;
2775 assert(elt->sge.lkey == rxq->mr->lkey);
2776 WR_ID(wr->wr_id).offset =
2777 (((uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM) -
2779 assert(WR_ID(wr->wr_id).id == WR_ID(wr_id).id);
2781 /* Add SGE to array for repost. */
2784 /* Update seg information. */
2785 SET_DATA_OFF(seg, RTE_PKTMBUF_HEADROOM);
2787 PORT(seg) = rxq->port_id;
2790 DATA_LEN(seg) = len;
2791 seg->ol_flags = rxq_cq_to_ol_flags(rxq, flags);
2793 /* Return packet. */
2796 #ifdef MLX4_PMD_SOFT_COUNTERS
2797 /* Increase bytes counter. */
2798 rxq->stats.ibytes += len;
2801 if (++elts_head >= elts_n)
2805 if (unlikely(i == 0))
2809 DEBUG("%p: reposting %u WRs", (void *)rxq, i);
2811 ret = rxq->if_qp->recv_burst(rxq->qp, sges, i);
2812 if (unlikely(ret)) {
2813 /* Inability to repost WRs is fatal. */
2814 DEBUG("%p: recv_burst(): failed (ret=%d)",
2819 rxq->elts_head = elts_head;
2820 #ifdef MLX4_PMD_SOFT_COUNTERS
2821 /* Increase packets counter. */
2822 rxq->stats.ipackets += pkts_ret;
2828 * Allocate a Queue Pair.
2829 * Optionally setup inline receive if supported.
2832 * Pointer to private structure.
2834 * Completion queue to associate with QP.
2836 * Number of descriptors in QP (hint only).
2839 * QP pointer or NULL in case of error.
2841 static struct ibv_qp *
2842 rxq_setup_qp(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,
2866 attr.max_inl_recv = priv->inl_recv_size;
2867 attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
2869 return ibv_exp_create_qp(priv->ctx, &attr);
2875 * Allocate a RSS Queue Pair.
2876 * Optionally setup inline receive if supported.
2879 * Pointer to private structure.
2881 * Completion queue to associate with QP.
2883 * Number of descriptors in QP (hint only).
2885 * If nonzero, create a parent QP, otherwise a child.
2888 * QP pointer or NULL in case of error.
2890 static struct ibv_qp *
2891 rxq_setup_qp_rss(struct priv *priv, struct ibv_cq *cq, uint16_t desc,
2894 struct ibv_exp_qp_init_attr attr = {
2895 /* CQ to be associated with the send queue. */
2897 /* CQ to be associated with the receive queue. */
2900 /* Max number of outstanding WRs. */
2901 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
2902 priv->device_attr.max_qp_wr :
2904 /* Max number of scatter/gather elements in a WR. */
2905 .max_recv_sge = ((priv->device_attr.max_sge <
2906 MLX4_PMD_SGE_WR_N) ?
2907 priv->device_attr.max_sge :
2910 .qp_type = IBV_QPT_RAW_PACKET,
2911 .comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
2912 IBV_EXP_QP_INIT_ATTR_QPG),
2917 attr.max_inl_recv = priv->inl_recv_size,
2918 attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
2921 attr.qpg.qpg_type = IBV_EXP_QPG_PARENT;
2922 /* TSS isn't necessary. */
2923 attr.qpg.parent_attrib.tss_child_count = 0;
2924 attr.qpg.parent_attrib.rss_child_count = priv->rxqs_n;
2925 DEBUG("initializing parent RSS queue");
2927 attr.qpg.qpg_type = IBV_EXP_QPG_CHILD_RX;
2928 attr.qpg.qpg_parent = priv->rxq_parent.qp;
2929 DEBUG("initializing child RSS queue");
2931 return ibv_exp_create_qp(priv->ctx, &attr);
2934 #endif /* RSS_SUPPORT */
2937 * Reconfigure a RX queue with new parameters.
2939 * rxq_rehash() does not allocate mbufs, which, if not done from the right
2940 * thread (such as a control thread), may corrupt the pool.
2941 * In case of failure, the queue is left untouched.
2944 * Pointer to Ethernet device structure.
2949 * 0 on success, errno value on failure.
2952 rxq_rehash(struct rte_eth_dev *dev, struct rxq *rxq)
2954 struct priv *priv = rxq->priv;
2955 struct rxq tmpl = *rxq;
2956 unsigned int mbuf_n;
2957 unsigned int desc_n;
2958 struct rte_mbuf **pool;
2960 struct ibv_exp_qp_attr mod;
2961 struct ibv_recv_wr *bad_wr;
2963 int parent = (rxq == &priv->rxq_parent);
2966 ERROR("%p: cannot rehash parent queue %p",
2967 (void *)dev, (void *)rxq);
2970 DEBUG("%p: rehashing queue %p", (void *)dev, (void *)rxq);
2971 /* Number of descriptors and mbufs currently allocated. */
2972 desc_n = (tmpl.elts_n * (tmpl.sp ? MLX4_PMD_SGE_WR_N : 1));
2974 /* Toggle RX checksum offload if hardware supports it. */
2975 if (priv->hw_csum) {
2976 tmpl.csum = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
2977 rxq->csum = tmpl.csum;
2979 /* Enable scattered packets support for this queue if necessary. */
2980 if ((dev->data->dev_conf.rxmode.jumbo_frame) &&
2981 (dev->data->dev_conf.rxmode.max_rx_pkt_len >
2982 (tmpl.mb_len - RTE_PKTMBUF_HEADROOM))) {
2984 desc_n /= MLX4_PMD_SGE_WR_N;
2987 DEBUG("%p: %s scattered packets support (%u WRs)",
2988 (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc_n);
2989 /* If scatter mode is the same as before, nothing to do. */
2990 if (tmpl.sp == rxq->sp) {
2991 DEBUG("%p: nothing to do", (void *)dev);
2994 /* Remove attached flows if RSS is disabled (no parent queue). */
2996 rxq_allmulticast_disable(&tmpl);
2997 rxq_promiscuous_disable(&tmpl);
2998 rxq_mac_addrs_del(&tmpl);
2999 /* Update original queue in case of failure. */
3000 rxq->allmulti_flow = tmpl.allmulti_flow;
3001 rxq->promisc_flow = tmpl.promisc_flow;
3002 memcpy(rxq->mac_configured, tmpl.mac_configured,
3003 sizeof(rxq->mac_configured));
3004 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
3006 /* From now on, any failure will render the queue unusable.
3007 * Reinitialize QP. */
3008 mod = (struct ibv_exp_qp_attr){ .qp_state = IBV_QPS_RESET };
3009 err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3011 ERROR("%p: cannot reset QP: %s", (void *)dev, strerror(err));
3015 err = ibv_resize_cq(tmpl.cq, desc_n);
3017 ERROR("%p: cannot resize CQ: %s", (void *)dev, strerror(err));
3021 mod = (struct ibv_exp_qp_attr){
3022 /* Move the QP to this state. */
3023 .qp_state = IBV_QPS_INIT,
3024 /* Primary port number. */
3025 .port_num = priv->port
3027 err = ibv_exp_modify_qp(tmpl.qp, &mod,
3030 (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
3031 #endif /* RSS_SUPPORT */
3034 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
3035 (void *)dev, strerror(err));
3039 /* Reconfigure flows. Do not care for errors. */
3041 rxq_mac_addrs_add(&tmpl);
3043 rxq_promiscuous_enable(&tmpl);
3045 rxq_allmulticast_enable(&tmpl);
3046 /* Update original queue in case of failure. */
3047 rxq->allmulti_flow = tmpl.allmulti_flow;
3048 rxq->promisc_flow = tmpl.promisc_flow;
3049 memcpy(rxq->mac_configured, tmpl.mac_configured,
3050 sizeof(rxq->mac_configured));
3051 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
3053 /* Allocate pool. */
3054 pool = rte_malloc(__func__, (mbuf_n * sizeof(*pool)), 0);
3056 ERROR("%p: cannot allocate memory", (void *)dev);
3059 /* Snatch mbufs from original queue. */
3062 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
3064 for (i = 0; (i != elemof(*elts)); ++i) {
3065 struct rxq_elt_sp *elt = &(*elts)[i];
3068 for (j = 0; (j != elemof(elt->bufs)); ++j) {
3069 assert(elt->bufs[j] != NULL);
3070 pool[k++] = elt->bufs[j];
3074 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
3076 for (i = 0; (i != elemof(*elts)); ++i) {
3077 struct rxq_elt *elt = &(*elts)[i];
3078 struct rte_mbuf *buf = (void *)
3079 ((uintptr_t)elt->sge.addr -
3080 WR_ID(elt->wr.wr_id).offset);
3082 assert(WR_ID(elt->wr.wr_id).id == i);
3086 assert(k == mbuf_n);
3088 tmpl.elts.sp = NULL;
3089 assert((void *)&tmpl.elts.sp == (void *)&tmpl.elts.no_sp);
3091 rxq_alloc_elts_sp(&tmpl, desc_n, pool) :
3092 rxq_alloc_elts(&tmpl, desc_n, pool));
3094 ERROR("%p: cannot reallocate WRs, aborting", (void *)dev);
3099 assert(tmpl.elts_n == desc_n);
3100 assert(tmpl.elts.sp != NULL);
3102 /* Clean up original data. */
3104 rte_free(rxq->elts.sp);
3105 rxq->elts.sp = NULL;
3107 err = ibv_post_recv(tmpl.qp,
3109 &(*tmpl.elts.sp)[0].wr :
3110 &(*tmpl.elts.no_sp)[0].wr),
3113 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
3119 mod = (struct ibv_exp_qp_attr){
3120 .qp_state = IBV_QPS_RTR
3122 err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3124 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
3125 (void *)dev, strerror(err));
3133 * Configure a RX queue.
3136 * Pointer to Ethernet device structure.
3138 * Pointer to RX queue structure.
3140 * Number of descriptors to configure in queue.
3142 * NUMA socket on which memory must be allocated.
3144 * Thresholds parameters.
3146 * Memory pool for buffer allocations.
3149 * 0 on success, errno value on failure.
3152 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
3153 unsigned int socket, const struct rte_eth_rxconf *conf,
3154 struct rte_mempool *mp)
3156 struct priv *priv = dev->data->dev_private;
3162 struct ibv_exp_qp_attr mod;
3164 struct ibv_exp_query_intf_params params;
3166 enum ibv_exp_query_intf_status status;
3167 struct ibv_recv_wr *bad_wr;
3168 struct rte_mbuf *buf;
3170 int parent = (rxq == &priv->rxq_parent);
3172 (void)conf; /* Thresholds configuration (ignored). */
3174 * If this is a parent queue, hardware must support RSS and
3175 * RSS must be enabled.
3177 assert((!parent) || ((priv->hw_rss) && (priv->rss)));
3179 /* Even if unused, ibv_create_cq() requires at least one
3184 if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
3185 ERROR("%p: invalid number of RX descriptors (must be a"
3186 " multiple of %d)", (void *)dev, MLX4_PMD_SGE_WR_N);
3189 /* Get mbuf length. */
3190 buf = rte_pktmbuf_alloc(mp);
3192 ERROR("%p: unable to allocate mbuf", (void *)dev);
3195 tmpl.mb_len = buf->buf_len;
3196 assert((rte_pktmbuf_headroom(buf) +
3197 rte_pktmbuf_tailroom(buf)) == tmpl.mb_len);
3198 assert(rte_pktmbuf_headroom(buf) == RTE_PKTMBUF_HEADROOM);
3199 rte_pktmbuf_free(buf);
3200 /* Toggle RX checksum offload if hardware supports it. */
3202 tmpl.csum = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3203 /* Enable scattered packets support for this queue if necessary. */
3204 if ((dev->data->dev_conf.rxmode.jumbo_frame) &&
3205 (dev->data->dev_conf.rxmode.max_rx_pkt_len >
3206 (tmpl.mb_len - RTE_PKTMBUF_HEADROOM))) {
3208 desc /= MLX4_PMD_SGE_WR_N;
3210 DEBUG("%p: %s scattered packets support (%u WRs)",
3211 (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc);
3212 /* Use the entire RX mempool as the memory region. */
3213 tmpl.mr = ibv_reg_mr(priv->pd,
3214 (void *)mp->elt_va_start,
3215 (mp->elt_va_end - mp->elt_va_start),
3216 (IBV_ACCESS_LOCAL_WRITE |
3217 IBV_ACCESS_REMOTE_WRITE));
3218 if (tmpl.mr == NULL) {
3220 ERROR("%p: MR creation failure: %s",
3221 (void *)dev, strerror(ret));
3225 tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
3226 if (tmpl.cq == NULL) {
3228 ERROR("%p: CQ creation failure: %s",
3229 (void *)dev, strerror(ret));
3232 DEBUG("priv->device_attr.max_qp_wr is %d",
3233 priv->device_attr.max_qp_wr);
3234 DEBUG("priv->device_attr.max_sge is %d",
3235 priv->device_attr.max_sge);
3238 tmpl.qp = rxq_setup_qp_rss(priv, tmpl.cq, desc, parent);
3240 #endif /* RSS_SUPPORT */
3241 tmpl.qp = rxq_setup_qp(priv, tmpl.cq, desc);
3242 if (tmpl.qp == NULL) {
3243 ret = (errno ? errno : EINVAL);
3244 ERROR("%p: QP creation failure: %s",
3245 (void *)dev, strerror(ret));
3248 mod = (struct ibv_exp_qp_attr){
3249 /* Move the QP to this state. */
3250 .qp_state = IBV_QPS_INIT,
3251 /* Primary port number. */
3252 .port_num = priv->port
3254 ret = ibv_exp_modify_qp(tmpl.qp, &mod,
3257 (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
3258 #endif /* RSS_SUPPORT */
3261 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
3262 (void *)dev, strerror(ret));
3265 if ((parent) || (!priv->rss)) {
3266 /* Configure MAC and broadcast addresses. */
3267 ret = rxq_mac_addrs_add(&tmpl);
3269 ERROR("%p: QP flow attachment failed: %s",
3270 (void *)dev, strerror(ret));
3274 /* Allocate descriptors for RX queues, except for the RSS parent. */
3278 ret = rxq_alloc_elts_sp(&tmpl, desc, NULL);
3280 ret = rxq_alloc_elts(&tmpl, desc, NULL);
3282 ERROR("%p: RXQ allocation failed: %s",
3283 (void *)dev, strerror(ret));
3286 ret = ibv_post_recv(tmpl.qp,
3288 &(*tmpl.elts.sp)[0].wr :
3289 &(*tmpl.elts.no_sp)[0].wr),
3292 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
3299 mod = (struct ibv_exp_qp_attr){
3300 .qp_state = IBV_QPS_RTR
3302 ret = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3304 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
3305 (void *)dev, strerror(ret));
3309 tmpl.port_id = dev->data->port_id;
3310 DEBUG("%p: RTE port ID: %u", (void *)rxq, tmpl.port_id);
3311 attr.params = (struct ibv_exp_query_intf_params){
3312 .intf_scope = IBV_EXP_INTF_GLOBAL,
3313 .intf = IBV_EXP_INTF_CQ,
3316 tmpl.if_cq = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
3317 if (tmpl.if_cq == NULL) {
3318 ERROR("%p: CQ interface family query failed with status %d",
3319 (void *)dev, status);
3322 attr.params = (struct ibv_exp_query_intf_params){
3323 .intf_scope = IBV_EXP_INTF_GLOBAL,
3324 .intf = IBV_EXP_INTF_QP_BURST,
3327 tmpl.if_qp = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
3328 if (tmpl.if_qp == NULL) {
3329 ERROR("%p: QP interface family query failed with status %d",
3330 (void *)dev, status);
3333 /* Clean up rxq in case we're reinitializing it. */
3334 DEBUG("%p: cleaning-up old rxq just in case", (void *)rxq);
3337 DEBUG("%p: rxq updated with %p", (void *)rxq, (void *)&tmpl);
3347 * DPDK callback to configure a RX queue.
3350 * Pointer to Ethernet device structure.
3354 * Number of descriptors to configure in queue.
3356 * NUMA socket on which memory must be allocated.
3358 * Thresholds parameters.
3360 * Memory pool for buffer allocations.
3363 * 0 on success, negative errno value on failure.
3366 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
3367 unsigned int socket, const struct rte_eth_rxconf *conf,
3368 struct rte_mempool *mp)
3370 struct priv *priv = dev->data->dev_private;
3371 struct rxq *rxq = (*priv->rxqs)[idx];
3375 DEBUG("%p: configuring queue %u for %u descriptors",
3376 (void *)dev, idx, desc);
3377 if (idx >= priv->rxqs_n) {
3378 ERROR("%p: queue index out of range (%u >= %u)",
3379 (void *)dev, idx, priv->rxqs_n);
3384 DEBUG("%p: reusing already allocated queue index %u (%p)",
3385 (void *)dev, idx, (void *)rxq);
3386 if (priv->started) {
3390 (*priv->rxqs)[idx] = NULL;
3393 rxq = rte_calloc_socket("RXQ", 1, sizeof(*rxq), 0, socket);
3395 ERROR("%p: unable to allocate queue index %u",
3401 ret = rxq_setup(dev, rxq, desc, socket, conf, mp);
3405 rxq->stats.idx = idx;
3406 DEBUG("%p: adding RX queue %p to list",
3407 (void *)dev, (void *)rxq);
3408 (*priv->rxqs)[idx] = rxq;
3409 /* Update receive callback. */
3411 dev->rx_pkt_burst = mlx4_rx_burst_sp;
3413 dev->rx_pkt_burst = mlx4_rx_burst;
3420 * DPDK callback to release a RX queue.
3423 * Generic RX queue pointer.
3426 mlx4_rx_queue_release(void *dpdk_rxq)
3428 struct rxq *rxq = (struct rxq *)dpdk_rxq;
3436 assert(rxq != &priv->rxq_parent);
3437 for (i = 0; (i != priv->rxqs_n); ++i)
3438 if ((*priv->rxqs)[i] == rxq) {
3439 DEBUG("%p: removing RX queue %p from list",
3440 (void *)priv->dev, (void *)rxq);
3441 (*priv->rxqs)[i] = NULL;
3450 * DPDK callback to start the device.
3452 * Simulate device start by attaching all configured flows.
3455 * Pointer to Ethernet device structure.
3458 * 0 on success, negative errno value on failure.
3461 mlx4_dev_start(struct rte_eth_dev *dev)
3463 struct priv *priv = dev->data->dev_private;
3469 if (priv->started) {
3473 DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
3476 rxq = &priv->rxq_parent;
3479 rxq = (*priv->rxqs)[0];
3482 /* Iterate only once when RSS is enabled. */
3486 /* Ignore nonexistent RX queues. */
3489 ret = rxq_mac_addrs_add(rxq);
3490 if (!ret && priv->promisc)
3491 ret = rxq_promiscuous_enable(rxq);
3492 if (!ret && priv->allmulti)
3493 ret = rxq_allmulticast_enable(rxq);
3496 WARN("%p: QP flow attachment failed: %s",
3497 (void *)dev, strerror(ret));
3500 rxq = (*priv->rxqs)[--i];
3502 rxq_allmulticast_disable(rxq);
3503 rxq_promiscuous_disable(rxq);
3504 rxq_mac_addrs_del(rxq);
3509 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
3515 * DPDK callback to stop the device.
3517 * Simulate device stop by detaching all configured flows.
3520 * Pointer to Ethernet device structure.
3523 mlx4_dev_stop(struct rte_eth_dev *dev)
3525 struct priv *priv = dev->data->dev_private;
3531 if (!priv->started) {
3535 DEBUG("%p: detaching flows from all RX queues", (void *)dev);
3538 rxq = &priv->rxq_parent;
3541 rxq = (*priv->rxqs)[0];
3544 /* Iterate only once when RSS is enabled. */
3546 /* Ignore nonexistent RX queues. */
3549 rxq_allmulticast_disable(rxq);
3550 rxq_promiscuous_disable(rxq);
3551 rxq_mac_addrs_del(rxq);
3552 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
3557 * Dummy DPDK callback for TX.
3559 * This function is used to temporarily replace the real callback during
3560 * unsafe control operations on the queue, or in case of error.
3563 * Generic pointer to TX queue structure.
3565 * Packets to transmit.
3567 * Number of packets in array.
3570 * Number of packets successfully transmitted (<= pkts_n).
3573 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
3582 * Dummy DPDK callback for RX.
3584 * This function is used to temporarily replace the real callback during
3585 * unsafe control operations on the queue, or in case of error.
3588 * Generic pointer to RX queue structure.
3590 * Array to store received packets.
3592 * Maximum number of packets in array.
3595 * Number of packets successfully received (<= pkts_n).
3598 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
3607 * DPDK callback to close the device.
3609 * Destroy all queues and objects, free memory.
3612 * Pointer to Ethernet device structure.
3615 mlx4_dev_close(struct rte_eth_dev *dev)
3617 struct priv *priv = dev->data->dev_private;
3622 DEBUG("%p: closing device \"%s\"",
3624 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
3625 /* Prevent crashes when queues are still in use. This is unfortunately
3626 * still required for DPDK 1.3 because some programs (such as testpmd)
3627 * never release them before closing the device. */
3628 dev->rx_pkt_burst = removed_rx_burst;
3629 dev->tx_pkt_burst = removed_tx_burst;
3630 if (priv->rxqs != NULL) {
3631 /* XXX race condition if mlx4_rx_burst() is still running. */
3633 for (i = 0; (i != priv->rxqs_n); ++i) {
3634 tmp = (*priv->rxqs)[i];
3637 (*priv->rxqs)[i] = NULL;
3644 if (priv->txqs != NULL) {
3645 /* XXX race condition if mlx4_tx_burst() is still running. */
3647 for (i = 0; (i != priv->txqs_n); ++i) {
3648 tmp = (*priv->txqs)[i];
3651 (*priv->txqs)[i] = NULL;
3659 rxq_cleanup(&priv->rxq_parent);
3660 if (priv->pd != NULL) {
3661 assert(priv->ctx != NULL);
3662 claim_zero(ibv_dealloc_pd(priv->pd));
3663 claim_zero(ibv_close_device(priv->ctx));
3665 assert(priv->ctx == NULL);
3667 memset(priv, 0, sizeof(*priv));
3671 * DPDK callback to get information about the device.
3674 * Pointer to Ethernet device structure.
3676 * Info structure output buffer.
3679 mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
3681 struct priv *priv = dev->data->dev_private;
3685 /* FIXME: we should ask the device for these values. */
3686 info->min_rx_bufsize = 32;
3687 info->max_rx_pktlen = 65536;
3689 * Since we need one CQ per QP, the limit is the minimum number
3690 * between the two values.
3692 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
3693 priv->device_attr.max_qp : priv->device_attr.max_cq);
3694 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
3697 info->max_rx_queues = max;
3698 info->max_tx_queues = max;
3699 info->max_mac_addrs = elemof(priv->mac);
3700 info->rx_offload_capa =
3702 (DEV_RX_OFFLOAD_IPV4_CKSUM |
3703 DEV_RX_OFFLOAD_UDP_CKSUM |
3704 DEV_RX_OFFLOAD_TCP_CKSUM) :
3706 info->tx_offload_capa =
3708 (DEV_TX_OFFLOAD_IPV4_CKSUM |
3709 DEV_TX_OFFLOAD_UDP_CKSUM |
3710 DEV_TX_OFFLOAD_TCP_CKSUM) :
3716 * DPDK callback to get device statistics.
3719 * Pointer to Ethernet device structure.
3721 * Stats structure output buffer.
3724 mlx4_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
3726 struct priv *priv = dev->data->dev_private;
3727 struct rte_eth_stats tmp = {0};
3732 /* Add software counters. */
3733 for (i = 0; (i != priv->rxqs_n); ++i) {
3734 struct rxq *rxq = (*priv->rxqs)[i];
3738 idx = rxq->stats.idx;
3739 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
3740 #ifdef MLX4_PMD_SOFT_COUNTERS
3741 tmp.q_ipackets[idx] += rxq->stats.ipackets;
3742 tmp.q_ibytes[idx] += rxq->stats.ibytes;
3744 tmp.q_errors[idx] += (rxq->stats.idropped +
3745 rxq->stats.rx_nombuf);
3747 #ifdef MLX4_PMD_SOFT_COUNTERS
3748 tmp.ipackets += rxq->stats.ipackets;
3749 tmp.ibytes += rxq->stats.ibytes;
3751 tmp.ierrors += rxq->stats.idropped;
3752 tmp.rx_nombuf += rxq->stats.rx_nombuf;
3754 for (i = 0; (i != priv->txqs_n); ++i) {
3755 struct txq *txq = (*priv->txqs)[i];
3759 idx = txq->stats.idx;
3760 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
3761 #ifdef MLX4_PMD_SOFT_COUNTERS
3762 tmp.q_opackets[idx] += txq->stats.opackets;
3763 tmp.q_obytes[idx] += txq->stats.obytes;
3765 tmp.q_errors[idx] += txq->stats.odropped;
3767 #ifdef MLX4_PMD_SOFT_COUNTERS
3768 tmp.opackets += txq->stats.opackets;
3769 tmp.obytes += txq->stats.obytes;
3771 tmp.oerrors += txq->stats.odropped;
3773 #ifndef MLX4_PMD_SOFT_COUNTERS
3774 /* FIXME: retrieve and add hardware counters. */
3781 * DPDK callback to clear device statistics.
3784 * Pointer to Ethernet device structure.
3787 mlx4_stats_reset(struct rte_eth_dev *dev)
3789 struct priv *priv = dev->data->dev_private;
3794 for (i = 0; (i != priv->rxqs_n); ++i) {
3795 if ((*priv->rxqs)[i] == NULL)
3797 idx = (*priv->rxqs)[i]->stats.idx;
3798 (*priv->rxqs)[i]->stats =
3799 (struct mlx4_rxq_stats){ .idx = idx };
3801 for (i = 0; (i != priv->txqs_n); ++i) {
3802 if ((*priv->txqs)[i] == NULL)
3804 idx = (*priv->rxqs)[i]->stats.idx;
3805 (*priv->txqs)[i]->stats =
3806 (struct mlx4_txq_stats){ .idx = idx };
3808 #ifndef MLX4_PMD_SOFT_COUNTERS
3809 /* FIXME: reset hardware counters. */
3815 * DPDK callback to remove a MAC address.
3818 * Pointer to Ethernet device structure.
3820 * MAC address index.
3823 mlx4_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
3825 struct priv *priv = dev->data->dev_private;
3828 DEBUG("%p: removing MAC address from index %" PRIu32,
3829 (void *)dev, index);
3830 if (index >= MLX4_MAX_MAC_ADDRESSES)
3832 /* Refuse to remove the broadcast address, this one is special. */
3833 if (!memcmp(priv->mac[index].addr_bytes, "\xff\xff\xff\xff\xff\xff",
3836 priv_mac_addr_del(priv, index);
3842 * DPDK callback to add a MAC address.
3845 * Pointer to Ethernet device structure.
3847 * MAC address to register.
3849 * MAC address index.
3851 * VMDq pool index to associate address with (ignored).
3854 mlx4_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
3855 uint32_t index, uint32_t vmdq)
3857 struct priv *priv = dev->data->dev_private;
3861 DEBUG("%p: adding MAC address at index %" PRIu32,
3862 (void *)dev, index);
3863 if (index >= MLX4_MAX_MAC_ADDRESSES)
3865 /* Refuse to add the broadcast address, this one is special. */
3866 if (!memcmp(mac_addr->addr_bytes, "\xff\xff\xff\xff\xff\xff",
3869 priv_mac_addr_add(priv, index,
3870 (const uint8_t (*)[ETHER_ADDR_LEN])
3871 mac_addr->addr_bytes);
3877 * DPDK callback to enable promiscuous mode.
3880 * Pointer to Ethernet device structure.
3883 mlx4_promiscuous_enable(struct rte_eth_dev *dev)
3885 struct priv *priv = dev->data->dev_private;
3890 if (priv->promisc) {
3894 /* If device isn't started, this is all we need to do. */
3898 ret = rxq_promiscuous_enable(&priv->rxq_parent);
3905 for (i = 0; (i != priv->rxqs_n); ++i) {
3906 if ((*priv->rxqs)[i] == NULL)
3908 ret = rxq_promiscuous_enable((*priv->rxqs)[i]);
3911 /* Failure, rollback. */
3913 if ((*priv->rxqs)[--i] != NULL)
3914 rxq_promiscuous_disable((*priv->rxqs)[i]);
3924 * DPDK callback to disable promiscuous mode.
3927 * Pointer to Ethernet device structure.
3930 mlx4_promiscuous_disable(struct rte_eth_dev *dev)
3932 struct priv *priv = dev->data->dev_private;
3936 if (!priv->promisc) {
3941 rxq_promiscuous_disable(&priv->rxq_parent);
3944 for (i = 0; (i != priv->rxqs_n); ++i)
3945 if ((*priv->rxqs)[i] != NULL)
3946 rxq_promiscuous_disable((*priv->rxqs)[i]);
3953 * DPDK callback to enable allmulti mode.
3956 * Pointer to Ethernet device structure.
3959 mlx4_allmulticast_enable(struct rte_eth_dev *dev)
3961 struct priv *priv = dev->data->dev_private;
3966 if (priv->allmulti) {
3970 /* If device isn't started, this is all we need to do. */
3974 ret = rxq_allmulticast_enable(&priv->rxq_parent);
3981 for (i = 0; (i != priv->rxqs_n); ++i) {
3982 if ((*priv->rxqs)[i] == NULL)
3984 ret = rxq_allmulticast_enable((*priv->rxqs)[i]);
3987 /* Failure, rollback. */
3989 if ((*priv->rxqs)[--i] != NULL)
3990 rxq_allmulticast_disable((*priv->rxqs)[i]);
4000 * DPDK callback to disable allmulti mode.
4003 * Pointer to Ethernet device structure.
4006 mlx4_allmulticast_disable(struct rte_eth_dev *dev)
4008 struct priv *priv = dev->data->dev_private;
4012 if (!priv->allmulti) {
4017 rxq_allmulticast_disable(&priv->rxq_parent);
4020 for (i = 0; (i != priv->rxqs_n); ++i)
4021 if ((*priv->rxqs)[i] != NULL)
4022 rxq_allmulticast_disable((*priv->rxqs)[i]);
4029 * DPDK callback to retrieve physical link information (unlocked version).
4032 * Pointer to Ethernet device structure.
4033 * @param wait_to_complete
4034 * Wait for request completion (ignored).
4037 mlx4_link_update_unlocked(struct rte_eth_dev *dev, int wait_to_complete)
4039 struct priv *priv = dev->data->dev_private;
4040 struct ethtool_cmd edata = {
4044 struct rte_eth_link dev_link;
4047 (void)wait_to_complete;
4048 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
4049 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
4052 memset(&dev_link, 0, sizeof(dev_link));
4053 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
4054 (ifr.ifr_flags & IFF_RUNNING));
4055 ifr.ifr_data = &edata;
4056 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4057 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
4061 link_speed = ethtool_cmd_speed(&edata);
4062 if (link_speed == -1)
4063 dev_link.link_speed = 0;
4065 dev_link.link_speed = link_speed;
4066 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
4067 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
4068 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
4069 /* Link status changed. */
4070 dev->data->dev_link = dev_link;
4073 /* Link status is still the same. */
4078 * DPDK callback to retrieve physical link information.
4081 * Pointer to Ethernet device structure.
4082 * @param wait_to_complete
4083 * Wait for request completion (ignored).
4086 mlx4_link_update(struct rte_eth_dev *dev, int wait_to_complete)
4088 struct priv *priv = dev->data->dev_private;
4092 ret = mlx4_link_update_unlocked(dev, wait_to_complete);
4098 * DPDK callback to change the MTU.
4100 * Setting the MTU affects hardware MRU (packets larger than the MTU cannot be
4101 * received). Use this as a hint to enable/disable scattered packets support
4102 * and improve performance when not needed.
4103 * Since failure is not an option, reconfiguring queues on the fly is not
4107 * Pointer to Ethernet device structure.
4112 * 0 on success, negative errno value on failure.
4115 mlx4_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
4117 struct priv *priv = dev->data->dev_private;
4120 uint16_t (*rx_func)(void *, struct rte_mbuf **, uint16_t) =
4124 /* Set kernel interface MTU first. */
4125 if (priv_set_mtu(priv, mtu)) {
4127 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
4131 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
4133 /* Temporarily replace RX handler with a fake one, assuming it has not
4134 * been copied elsewhere. */
4135 dev->rx_pkt_burst = removed_rx_burst;
4136 /* Make sure everyone has left mlx4_rx_burst() and uses
4137 * removed_rx_burst() instead. */
4140 /* Reconfigure each RX queue. */
4141 for (i = 0; (i != priv->rxqs_n); ++i) {
4142 struct rxq *rxq = (*priv->rxqs)[i];
4143 unsigned int max_frame_len;
4148 /* Calculate new maximum frame length according to MTU and
4149 * toggle scattered support (sp) if necessary. */
4150 max_frame_len = (priv->mtu + ETHER_HDR_LEN +
4151 (ETHER_MAX_VLAN_FRAME_LEN - ETHER_MAX_LEN));
4152 sp = (max_frame_len > (rxq->mb_len - RTE_PKTMBUF_HEADROOM));
4153 /* Provide new values to rxq_setup(). */
4154 dev->data->dev_conf.rxmode.jumbo_frame = sp;
4155 dev->data->dev_conf.rxmode.max_rx_pkt_len = max_frame_len;
4156 ret = rxq_rehash(dev, rxq);
4158 /* Force SP RX if that queue requires it and abort. */
4160 rx_func = mlx4_rx_burst_sp;
4163 /* Reenable non-RSS queue attributes. No need to check
4164 * for errors at this stage. */
4166 rxq_mac_addrs_add(rxq);
4168 rxq_promiscuous_enable(rxq);
4170 rxq_allmulticast_enable(rxq);
4172 /* Scattered burst function takes priority. */
4174 rx_func = mlx4_rx_burst_sp;
4176 /* Burst functions can now be called again. */
4178 dev->rx_pkt_burst = rx_func;
4186 * DPDK callback to get flow control status.
4189 * Pointer to Ethernet device structure.
4190 * @param[out] fc_conf
4191 * Flow control output buffer.
4194 * 0 on success, negative errno value on failure.
4197 mlx4_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4199 struct priv *priv = dev->data->dev_private;
4201 struct ethtool_pauseparam ethpause = {
4202 .cmd = ETHTOOL_GPAUSEPARAM
4206 ifr.ifr_data = ðpause;
4208 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4210 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
4216 fc_conf->autoneg = ethpause.autoneg;
4217 if (ethpause.rx_pause && ethpause.tx_pause)
4218 fc_conf->mode = RTE_FC_FULL;
4219 else if (ethpause.rx_pause)
4220 fc_conf->mode = RTE_FC_RX_PAUSE;
4221 else if (ethpause.tx_pause)
4222 fc_conf->mode = RTE_FC_TX_PAUSE;
4224 fc_conf->mode = RTE_FC_NONE;
4234 * DPDK callback to modify flow control parameters.
4237 * Pointer to Ethernet device structure.
4238 * @param[in] fc_conf
4239 * Flow control parameters.
4242 * 0 on success, negative errno value on failure.
4245 mlx4_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4247 struct priv *priv = dev->data->dev_private;
4249 struct ethtool_pauseparam ethpause = {
4250 .cmd = ETHTOOL_SPAUSEPARAM
4254 ifr.ifr_data = ðpause;
4255 ethpause.autoneg = fc_conf->autoneg;
4256 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4257 (fc_conf->mode & RTE_FC_RX_PAUSE))
4258 ethpause.rx_pause = 1;
4260 ethpause.rx_pause = 0;
4262 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4263 (fc_conf->mode & RTE_FC_TX_PAUSE))
4264 ethpause.tx_pause = 1;
4266 ethpause.tx_pause = 0;
4269 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4271 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
4285 * Configure a VLAN filter.
4288 * Pointer to Ethernet device structure.
4290 * VLAN ID to filter.
4295 * 0 on success, errno value on failure.
4298 vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
4300 struct priv *priv = dev->data->dev_private;
4302 unsigned int j = -1;
4304 DEBUG("%p: %s VLAN filter ID %" PRIu16,
4305 (void *)dev, (on ? "enable" : "disable"), vlan_id);
4306 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
4307 if (!priv->vlan_filter[i].enabled) {
4308 /* Unused index, remember it. */
4312 if (priv->vlan_filter[i].id != vlan_id)
4314 /* This VLAN ID is already known, use its index. */
4318 /* Check if there's room for another VLAN filter. */
4319 if (j == (unsigned int)-1)
4322 * VLAN filters apply to all configured MAC addresses, flow
4323 * specifications must be reconfigured accordingly.
4325 priv->vlan_filter[j].id = vlan_id;
4326 if ((on) && (!priv->vlan_filter[j].enabled)) {
4328 * Filter is disabled, enable it.
4329 * Rehashing flows in all RX queues is necessary.
4332 rxq_mac_addrs_del(&priv->rxq_parent);
4334 for (i = 0; (i != priv->rxqs_n); ++i)
4335 if ((*priv->rxqs)[i] != NULL)
4336 rxq_mac_addrs_del((*priv->rxqs)[i]);
4337 priv->vlan_filter[j].enabled = 1;
4338 if (priv->started) {
4340 rxq_mac_addrs_add(&priv->rxq_parent);
4342 for (i = 0; (i != priv->rxqs_n); ++i) {
4343 if ((*priv->rxqs)[i] == NULL)
4345 rxq_mac_addrs_add((*priv->rxqs)[i]);
4348 } else if ((!on) && (priv->vlan_filter[j].enabled)) {
4350 * Filter is enabled, disable it.
4351 * Rehashing flows in all RX queues is necessary.
4354 rxq_mac_addrs_del(&priv->rxq_parent);
4356 for (i = 0; (i != priv->rxqs_n); ++i)
4357 if ((*priv->rxqs)[i] != NULL)
4358 rxq_mac_addrs_del((*priv->rxqs)[i]);
4359 priv->vlan_filter[j].enabled = 0;
4360 if (priv->started) {
4362 rxq_mac_addrs_add(&priv->rxq_parent);
4364 for (i = 0; (i != priv->rxqs_n); ++i) {
4365 if ((*priv->rxqs)[i] == NULL)
4367 rxq_mac_addrs_add((*priv->rxqs)[i]);
4375 * DPDK callback to configure a VLAN filter.
4378 * Pointer to Ethernet device structure.
4380 * VLAN ID to filter.
4385 * 0 on success, negative errno value on failure.
4388 mlx4_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
4390 struct priv *priv = dev->data->dev_private;
4394 ret = vlan_filter_set(dev, vlan_id, on);
4400 static const struct eth_dev_ops mlx4_dev_ops = {
4401 .dev_configure = mlx4_dev_configure,
4402 .dev_start = mlx4_dev_start,
4403 .dev_stop = mlx4_dev_stop,
4404 .dev_close = mlx4_dev_close,
4405 .promiscuous_enable = mlx4_promiscuous_enable,
4406 .promiscuous_disable = mlx4_promiscuous_disable,
4407 .allmulticast_enable = mlx4_allmulticast_enable,
4408 .allmulticast_disable = mlx4_allmulticast_disable,
4409 .link_update = mlx4_link_update,
4410 .stats_get = mlx4_stats_get,
4411 .stats_reset = mlx4_stats_reset,
4412 .queue_stats_mapping_set = NULL,
4413 .dev_infos_get = mlx4_dev_infos_get,
4414 .vlan_filter_set = mlx4_vlan_filter_set,
4415 .vlan_tpid_set = NULL,
4416 .vlan_strip_queue_set = NULL,
4417 .vlan_offload_set = NULL,
4418 .rx_queue_setup = mlx4_rx_queue_setup,
4419 .tx_queue_setup = mlx4_tx_queue_setup,
4420 .rx_queue_release = mlx4_rx_queue_release,
4421 .tx_queue_release = mlx4_tx_queue_release,
4423 .dev_led_off = NULL,
4424 .flow_ctrl_get = mlx4_dev_get_flow_ctrl,
4425 .flow_ctrl_set = mlx4_dev_set_flow_ctrl,
4426 .priority_flow_ctrl_set = NULL,
4427 .mac_addr_remove = mlx4_mac_addr_remove,
4428 .mac_addr_add = mlx4_mac_addr_add,
4429 .mtu_set = mlx4_dev_set_mtu,
4430 .fdir_add_signature_filter = NULL,
4431 .fdir_update_signature_filter = NULL,
4432 .fdir_remove_signature_filter = NULL,
4433 .fdir_add_perfect_filter = NULL,
4434 .fdir_update_perfect_filter = NULL,
4435 .fdir_remove_perfect_filter = NULL,
4436 .fdir_set_masks = NULL
4440 * Get PCI information from struct ibv_device.
4443 * Pointer to Ethernet device structure.
4444 * @param[out] pci_addr
4445 * PCI bus address output buffer.
4448 * 0 on success, -1 on failure and errno is set.
4451 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
4452 struct rte_pci_addr *pci_addr)
4456 MKSTR(path, "%s/device/uevent", device->ibdev_path);
4458 file = fopen(path, "rb");
4461 while (fgets(line, sizeof(line), file) == line) {
4462 size_t len = strlen(line);
4465 /* Truncate long lines. */
4466 if (len == (sizeof(line) - 1))
4467 while (line[(len - 1)] != '\n') {
4471 line[(len - 1)] = ret;
4473 /* Extract information. */
4476 "%" SCNx16 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
4480 &pci_addr->function) == 4) {
4490 * Get MAC address by querying netdevice.
4493 * struct priv for the requested device.
4495 * MAC address output buffer.
4498 * 0 on success, -1 on failure and errno is set.
4501 priv_get_mac(struct priv *priv, uint8_t (*mac)[ETHER_ADDR_LEN])
4503 struct ifreq request;
4505 if (priv_ifreq(priv, SIOCGIFHWADDR, &request))
4507 memcpy(mac, request.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
4511 /* Support up to 32 adapters. */
4513 struct rte_pci_addr pci_addr; /* associated PCI address */
4514 uint32_t ports; /* physical ports bitfield. */
4518 * Get device index in mlx4_dev[] from PCI bus address.
4520 * @param[in] pci_addr
4521 * PCI bus address to look for.
4524 * mlx4_dev[] index on success, -1 on failure.
4527 mlx4_dev_idx(struct rte_pci_addr *pci_addr)
4532 assert(pci_addr != NULL);
4533 for (i = 0; (i != elemof(mlx4_dev)); ++i) {
4534 if ((mlx4_dev[i].pci_addr.domain == pci_addr->domain) &&
4535 (mlx4_dev[i].pci_addr.bus == pci_addr->bus) &&
4536 (mlx4_dev[i].pci_addr.devid == pci_addr->devid) &&
4537 (mlx4_dev[i].pci_addr.function == pci_addr->function))
4539 if ((mlx4_dev[i].ports == 0) && (ret == -1))
4546 * Retrieve integer value from environment variable.
4549 * Environment variable name.
4552 * Integer value, 0 if the variable is not set.
4555 mlx4_getenv_int(const char *name)
4557 const char *val = getenv(name);
4564 static struct eth_driver mlx4_driver;
4567 * DPDK callback to register a PCI device.
4569 * This function creates an Ethernet device for each port of a given
4572 * @param[in] pci_drv
4573 * PCI driver structure (mlx4_driver).
4574 * @param[in] pci_dev
4575 * PCI device information.
4578 * 0 on success, negative errno value on failure.
4581 mlx4_pci_devinit(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
4583 struct ibv_device **list;
4584 struct ibv_device *ibv_dev;
4586 struct ibv_context *attr_ctx = NULL;
4587 struct ibv_device_attr device_attr;
4593 assert(pci_drv == &mlx4_driver.pci_drv);
4594 /* Get mlx4_dev[] index. */
4595 idx = mlx4_dev_idx(&pci_dev->addr);
4597 ERROR("this driver cannot support any more adapters");
4600 DEBUG("using driver device index %d", idx);
4602 /* Save PCI address. */
4603 mlx4_dev[idx].pci_addr = pci_dev->addr;
4604 list = ibv_get_device_list(&i);
4607 if (errno == ENOSYS) {
4608 WARN("cannot list devices, is ib_uverbs loaded?");
4615 * For each listed device, check related sysfs entry against
4616 * the provided PCI ID.
4619 struct rte_pci_addr pci_addr;
4622 DEBUG("checking device \"%s\"", list[i]->name);
4623 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
4625 if ((pci_dev->addr.domain != pci_addr.domain) ||
4626 (pci_dev->addr.bus != pci_addr.bus) ||
4627 (pci_dev->addr.devid != pci_addr.devid) ||
4628 (pci_dev->addr.function != pci_addr.function))
4630 vf = (pci_dev->id.device_id ==
4631 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
4632 INFO("PCI information matches, using device \"%s\" (VF: %s)",
4633 list[i]->name, (vf ? "true" : "false"));
4634 attr_ctx = ibv_open_device(list[i]);
4638 if (attr_ctx == NULL) {
4639 ibv_free_device_list(list);
4642 WARN("cannot access device, is mlx4_ib loaded?");
4645 WARN("cannot use device, are drivers up to date?");
4653 DEBUG("device opened");
4654 if (ibv_query_device(attr_ctx, &device_attr))
4656 INFO("%u port(s) detected", device_attr.phys_port_cnt);
4658 for (i = 0; i < device_attr.phys_port_cnt; i++) {
4659 uint32_t port = i + 1; /* ports are indexed from one */
4660 uint32_t test = (1 << i);
4661 struct ibv_context *ctx = NULL;
4662 struct ibv_port_attr port_attr;
4663 struct ibv_pd *pd = NULL;
4664 struct priv *priv = NULL;
4665 struct rte_eth_dev *eth_dev;
4666 #ifdef HAVE_EXP_QUERY_DEVICE
4667 struct ibv_exp_device_attr exp_device_attr;
4668 #endif /* HAVE_EXP_QUERY_DEVICE */
4669 struct ether_addr mac;
4671 #ifdef HAVE_EXP_QUERY_DEVICE
4672 exp_device_attr.comp_mask = IBV_EXP_DEVICE_ATTR_EXP_CAP_FLAGS;
4674 exp_device_attr.comp_mask |= IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ;
4675 #endif /* RSS_SUPPORT */
4676 #endif /* HAVE_EXP_QUERY_DEVICE */
4678 DEBUG("using port %u (%08" PRIx32 ")", port, test);
4680 ctx = ibv_open_device(ibv_dev);
4684 /* Check port status. */
4685 err = ibv_query_port(ctx, port, &port_attr);
4687 ERROR("port query failed: %s", strerror(err));
4690 if (port_attr.state != IBV_PORT_ACTIVE)
4691 WARN("bad state for port %d: \"%s\" (%d)",
4692 port, ibv_port_state_str(port_attr.state),
4695 /* Allocate protection domain. */
4696 pd = ibv_alloc_pd(ctx);
4698 ERROR("PD allocation failure");
4703 mlx4_dev[idx].ports |= test;
4705 /* from rte_ethdev.c */
4706 priv = rte_zmalloc("ethdev private structure",
4708 RTE_CACHE_LINE_SIZE);
4710 ERROR("priv allocation failure");
4716 priv->device_attr = device_attr;
4719 priv->mtu = ETHER_MTU;
4720 #ifdef HAVE_EXP_QUERY_DEVICE
4721 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
4722 ERROR("ibv_exp_query_device() failed");
4726 if ((exp_device_attr.exp_device_cap_flags &
4727 IBV_EXP_DEVICE_QPG) &&
4728 (exp_device_attr.exp_device_cap_flags &
4729 IBV_EXP_DEVICE_UD_RSS) &&
4730 (exp_device_attr.comp_mask &
4731 IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ) &&
4732 (exp_device_attr.max_rss_tbl_sz > 0)) {
4735 priv->max_rss_tbl_sz = exp_device_attr.max_rss_tbl_sz;
4739 priv->max_rss_tbl_sz = 0;
4741 priv->hw_tss = !!(exp_device_attr.exp_device_cap_flags &
4742 IBV_EXP_DEVICE_UD_TSS);
4743 DEBUG("device flags: %s%s%s",
4744 (priv->hw_qpg ? "IBV_DEVICE_QPG " : ""),
4745 (priv->hw_tss ? "IBV_DEVICE_TSS " : ""),
4746 (priv->hw_rss ? "IBV_DEVICE_RSS " : ""));
4748 DEBUG("maximum RSS indirection table size: %u",
4749 exp_device_attr.max_rss_tbl_sz);
4750 #endif /* RSS_SUPPORT */
4753 ((exp_device_attr.exp_device_cap_flags &
4754 IBV_EXP_DEVICE_RX_CSUM_TCP_UDP_PKT) &&
4755 (exp_device_attr.exp_device_cap_flags &
4756 IBV_EXP_DEVICE_RX_CSUM_IP_PKT));
4757 DEBUG("checksum offloading is %ssupported",
4758 (priv->hw_csum ? "" : "not "));
4761 priv->inl_recv_size = mlx4_getenv_int("MLX4_INLINE_RECV_SIZE");
4763 if (priv->inl_recv_size) {
4764 exp_device_attr.comp_mask =
4765 IBV_EXP_DEVICE_ATTR_INLINE_RECV_SZ;
4766 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
4767 INFO("Couldn't query device for inline-receive"
4769 priv->inl_recv_size = 0;
4771 if ((unsigned)exp_device_attr.inline_recv_sz <
4772 priv->inl_recv_size) {
4773 INFO("Max inline-receive (%d) <"
4774 " requested inline-receive (%u)",
4775 exp_device_attr.inline_recv_sz,
4776 priv->inl_recv_size);
4777 priv->inl_recv_size =
4778 exp_device_attr.inline_recv_sz;
4781 INFO("Set inline receive size to %u",
4782 priv->inl_recv_size);
4784 #endif /* INLINE_RECV */
4785 #endif /* HAVE_EXP_QUERY_DEVICE */
4787 (void)mlx4_getenv_int;
4789 /* Configure the first MAC address by default. */
4790 if (priv_get_mac(priv, &mac.addr_bytes)) {
4791 ERROR("cannot get MAC address, is mlx4_en loaded?"
4792 " (errno: %s)", strerror(errno));
4795 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
4797 mac.addr_bytes[0], mac.addr_bytes[1],
4798 mac.addr_bytes[2], mac.addr_bytes[3],
4799 mac.addr_bytes[4], mac.addr_bytes[5]);
4800 /* Register MAC and broadcast addresses. */
4801 claim_zero(priv_mac_addr_add(priv, 0,
4802 (const uint8_t (*)[ETHER_ADDR_LEN])
4804 claim_zero(priv_mac_addr_add(priv, 1,
4805 &(const uint8_t [ETHER_ADDR_LEN])
4806 { "\xff\xff\xff\xff\xff\xff" }));
4809 char ifname[IF_NAMESIZE];
4811 if (priv_get_ifname(priv, &ifname) == 0)
4812 DEBUG("port %u ifname is \"%s\"",
4813 priv->port, ifname);
4815 DEBUG("port %u ifname is unknown", priv->port);
4818 /* Get actual MTU if possible. */
4819 priv_get_mtu(priv, &priv->mtu);
4820 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
4822 /* from rte_ethdev.c */
4824 char name[RTE_ETH_NAME_MAX_LEN];
4826 snprintf(name, sizeof(name), "%s port %u",
4827 ibv_get_device_name(ibv_dev), port);
4828 eth_dev = rte_eth_dev_allocate(name, RTE_ETH_DEV_PCI);
4830 if (eth_dev == NULL) {
4831 ERROR("can not allocate rte ethdev");
4836 eth_dev->data->dev_private = priv;
4837 eth_dev->pci_dev = pci_dev;
4838 eth_dev->driver = &mlx4_driver;
4839 eth_dev->data->rx_mbuf_alloc_failed = 0;
4840 eth_dev->data->mtu = ETHER_MTU;
4842 priv->dev = eth_dev;
4843 eth_dev->dev_ops = &mlx4_dev_ops;
4844 eth_dev->data->mac_addrs = priv->mac;
4846 /* Bring Ethernet device up. */
4847 DEBUG("forcing Ethernet interface up");
4848 priv_set_flags(priv, ~IFF_UP, IFF_UP);
4854 claim_zero(ibv_dealloc_pd(pd));
4856 claim_zero(ibv_close_device(ctx));
4861 * XXX if something went wrong in the loop above, there is a resource
4862 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
4863 * long as the dpdk does not provide a way to deallocate a ethdev and a
4864 * way to enumerate the registered ethdevs to free the previous ones.
4867 /* no port found, complain */
4868 if (!mlx4_dev[idx].ports) {
4875 claim_zero(ibv_close_device(attr_ctx));
4877 ibv_free_device_list(list);
4882 static const struct rte_pci_id mlx4_pci_id_map[] = {
4884 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4885 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3,
4886 .subsystem_vendor_id = PCI_ANY_ID,
4887 .subsystem_device_id = PCI_ANY_ID
4890 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4891 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO,
4892 .subsystem_vendor_id = PCI_ANY_ID,
4893 .subsystem_device_id = PCI_ANY_ID
4896 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4897 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3VF,
4898 .subsystem_vendor_id = PCI_ANY_ID,
4899 .subsystem_device_id = PCI_ANY_ID
4906 static struct eth_driver mlx4_driver = {
4908 .name = MLX4_DRIVER_NAME,
4909 .id_table = mlx4_pci_id_map,
4910 .devinit = mlx4_pci_devinit,
4912 .dev_private_size = sizeof(struct priv)
4916 * Driver initialization routine.
4919 rte_mlx4_pmd_init(const char *name, const char *args)
4924 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
4925 * huge pages. Calling ibv_fork_init() during init allows
4926 * applications to use fork() safely for purposes other than
4927 * using this PMD, which is not supported in forked processes.
4929 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
4931 rte_eal_pci_register(&mlx4_driver.pci_drv);
4935 static struct rte_driver rte_mlx4_driver = {
4937 .name = MLX4_DRIVER_NAME,
4938 .init = rte_mlx4_pmd_init,
4941 PMD_REGISTER_DRIVER(rte_mlx4_driver)