4 * Copyright 2012 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.
50 #include <arpa/inet.h>
53 #include <sys/ioctl.h>
54 #include <sys/socket.h>
55 #include <netinet/in.h>
56 #include <linux/ethtool.h>
57 #include <linux/sockios.h>
60 #include <rte_ether.h>
61 #include <rte_ethdev.h>
62 #include <rte_ethdev_pci.h>
65 #include <rte_errno.h>
66 #include <rte_mempool.h>
67 #include <rte_prefetch.h>
68 #include <rte_malloc.h>
69 #include <rte_spinlock.h>
70 #include <rte_atomic.h>
72 #include <rte_alarm.h>
73 #include <rte_memory.h>
75 #include <rte_kvargs.h>
76 #include <rte_interrupts.h>
77 #include <rte_branch_prediction.h>
79 /* Generated configuration header. */
80 #include "mlx4_autoconf.h"
84 #include "mlx4_flow.h"
86 /* Convenience macros for accessing mbuf fields. */
87 #define NEXT(m) ((m)->next)
88 #define DATA_LEN(m) ((m)->data_len)
89 #define PKT_LEN(m) ((m)->pkt_len)
90 #define DATA_OFF(m) ((m)->data_off)
91 #define SET_DATA_OFF(m, o) ((m)->data_off = (o))
92 #define NB_SEGS(m) ((m)->nb_segs)
93 #define PORT(m) ((m)->port)
95 /* Work Request ID data type (64 bit). */
104 #define WR_ID(o) (((wr_id_t *)&(o))->data)
106 /* Transpose flags. Useful to convert IBV to DPDK flags. */
107 #define TRANSPOSE(val, from, to) \
108 (((from) >= (to)) ? \
109 (((val) & (from)) / ((from) / (to))) : \
110 (((val) & (from)) * ((to) / (from))))
112 /** Configuration structure for device arguments. */
115 uint32_t present; /**< Bit-field for existing ports. */
116 uint32_t enabled; /**< Bit-field for user-enabled ports. */
120 /* Available parameters list. */
121 const char *pmd_mlx4_init_params[] = {
127 mlx4_rx_intr_enable(struct rte_eth_dev *dev, uint16_t idx);
130 mlx4_rx_intr_disable(struct rte_eth_dev *dev, uint16_t idx);
133 priv_rx_intr_vec_enable(struct priv *priv);
136 priv_rx_intr_vec_disable(struct priv *priv);
139 * Lock private structure to protect it from concurrent access in the
143 * Pointer to private structure.
145 void priv_lock(struct priv *priv)
147 rte_spinlock_lock(&priv->lock);
151 * Unlock private structure.
154 * Pointer to private structure.
156 void priv_unlock(struct priv *priv)
158 rte_spinlock_unlock(&priv->lock);
161 /* Allocate a buffer on the stack and fill it with a printf format string. */
162 #define MKSTR(name, ...) \
163 char name[snprintf(NULL, 0, __VA_ARGS__) + 1]; \
165 snprintf(name, sizeof(name), __VA_ARGS__)
168 * Get interface name from private structure.
171 * Pointer to private structure.
173 * Interface name output buffer.
176 * 0 on success, -1 on failure and errno is set.
179 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
183 unsigned int dev_type = 0;
184 unsigned int dev_port_prev = ~0u;
185 char match[IF_NAMESIZE] = "";
188 MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
194 while ((dent = readdir(dir)) != NULL) {
195 char *name = dent->d_name;
197 unsigned int dev_port;
200 if ((name[0] == '.') &&
201 ((name[1] == '\0') ||
202 ((name[1] == '.') && (name[2] == '\0'))))
205 MKSTR(path, "%s/device/net/%s/%s",
206 priv->ctx->device->ibdev_path, name,
207 (dev_type ? "dev_id" : "dev_port"));
209 file = fopen(path, "rb");
214 * Switch to dev_id when dev_port does not exist as
215 * is the case with Linux kernel versions < 3.15.
226 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
231 * Switch to dev_id when dev_port returns the same value for
232 * all ports. May happen when using a MOFED release older than
233 * 3.0 with a Linux kernel >= 3.15.
235 if (dev_port == dev_port_prev)
237 dev_port_prev = dev_port;
238 if (dev_port == (priv->port - 1u))
239 snprintf(match, sizeof(match), "%s", name);
242 if (match[0] == '\0')
244 strncpy(*ifname, match, sizeof(*ifname));
249 * Read from sysfs entry.
252 * Pointer to private structure.
254 * Entry name relative to sysfs path.
256 * Data output buffer.
261 * 0 on success, -1 on failure and errno is set.
264 priv_sysfs_read(const struct priv *priv, const char *entry,
265 char *buf, size_t size)
267 char ifname[IF_NAMESIZE];
272 if (priv_get_ifname(priv, &ifname))
275 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
278 file = fopen(path, "rb");
281 ret = fread(buf, 1, size, file);
283 if (((size_t)ret < size) && (ferror(file)))
293 * Write to sysfs entry.
296 * Pointer to private structure.
298 * Entry name relative to sysfs path.
305 * 0 on success, -1 on failure and errno is set.
308 priv_sysfs_write(const struct priv *priv, const char *entry,
309 char *buf, size_t size)
311 char ifname[IF_NAMESIZE];
316 if (priv_get_ifname(priv, &ifname))
319 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
322 file = fopen(path, "wb");
325 ret = fwrite(buf, 1, size, file);
327 if (((size_t)ret < size) || (ferror(file)))
337 * Get unsigned long sysfs property.
340 * Pointer to private structure.
342 * Entry name relative to sysfs path.
344 * Value output buffer.
347 * 0 on success, -1 on failure and errno is set.
350 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
353 unsigned long value_ret;
356 ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
358 DEBUG("cannot read %s value from sysfs: %s",
359 name, strerror(errno));
362 value_str[ret] = '\0';
364 value_ret = strtoul(value_str, NULL, 0);
366 DEBUG("invalid %s value `%s': %s", name, value_str,
375 * Set unsigned long sysfs property.
378 * Pointer to private structure.
380 * Entry name relative to sysfs path.
385 * 0 on success, -1 on failure and errno is set.
388 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
391 MKSTR(value_str, "%lu", value);
393 ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
395 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
396 name, value_str, value, strerror(errno));
403 * Perform ifreq ioctl() on associated Ethernet device.
406 * Pointer to private structure.
408 * Request number to pass to ioctl().
410 * Interface request structure output buffer.
413 * 0 on success, -1 on failure and errno is set.
416 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
418 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
423 if (priv_get_ifname(priv, &ifr->ifr_name) == 0)
424 ret = ioctl(sock, req, ifr);
433 * Pointer to private structure.
435 * MTU value output buffer.
438 * 0 on success, -1 on failure and errno is set.
441 priv_get_mtu(struct priv *priv, uint16_t *mtu)
443 unsigned long ulong_mtu;
445 if (priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu) == -1)
455 * Pointer to private structure.
460 * 0 on success, -1 on failure and errno is set.
463 priv_set_mtu(struct priv *priv, uint16_t mtu)
467 if (priv_set_sysfs_ulong(priv, "mtu", mtu) ||
468 priv_get_mtu(priv, &new_mtu))
480 * Pointer to private structure.
482 * Bitmask for flags that must remain untouched.
484 * Bitmask for flags to modify.
487 * 0 on success, -1 on failure and errno is set.
490 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
494 if (priv_get_sysfs_ulong(priv, "flags", &tmp) == -1)
497 tmp |= (flags & (~keep));
498 return priv_set_sysfs_ulong(priv, "flags", tmp);
501 /* Device configuration. */
504 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
505 unsigned int socket, const struct rte_eth_txconf *conf);
508 txq_cleanup(struct txq *txq);
511 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
512 unsigned int socket, int inactive,
513 const struct rte_eth_rxconf *conf,
514 struct rte_mempool *mp, int children_n,
515 struct rxq *rxq_parent);
518 rxq_cleanup(struct rxq *rxq);
521 * Create RSS parent queue.
523 * The new parent is inserted in front of the list in the private structure.
526 * Pointer to private structure.
528 * Queues indices array, if NULL use all Rx queues.
530 * The number of entries in queues[].
533 * Pointer to a parent rxq structure, NULL on failure.
536 priv_parent_create(struct priv *priv,
544 parent = rte_zmalloc("parent queue",
546 RTE_CACHE_LINE_SIZE);
548 ERROR("cannot allocate memory for RSS parent queue");
551 ret = rxq_setup(priv->dev, parent, 0, 0, 0,
552 NULL, NULL, children_n, NULL);
557 parent->rss.queues_n = children_n;
559 for (i = 0; i < children_n; ++i)
560 parent->rss.queues[i] = queues[i];
562 /* the default RSS ring case */
563 assert(priv->rxqs_n == children_n);
564 for (i = 0; i < priv->rxqs_n; ++i)
565 parent->rss.queues[i] = i;
567 LIST_INSERT_HEAD(&priv->parents, parent, next);
572 * Clean up RX queue parent structure.
575 * RX queue parent structure.
578 rxq_parent_cleanup(struct rxq *parent)
580 LIST_REMOVE(parent, next);
586 * Clean up parent structures from the parent list.
589 * Pointer to private structure.
592 priv_parent_list_cleanup(struct priv *priv)
594 while (!LIST_EMPTY(&priv->parents))
595 rxq_parent_cleanup(LIST_FIRST(&priv->parents));
599 * Ethernet device configuration.
601 * Prepare the driver for a given number of TX and RX queues.
602 * Allocate parent RSS queue when several RX queues are requested.
605 * Pointer to Ethernet device structure.
608 * 0 on success, errno value on failure.
611 dev_configure(struct rte_eth_dev *dev)
613 struct priv *priv = dev->data->dev_private;
614 unsigned int rxqs_n = dev->data->nb_rx_queues;
615 unsigned int txqs_n = dev->data->nb_tx_queues;
618 priv->rxqs = (void *)dev->data->rx_queues;
619 priv->txqs = (void *)dev->data->tx_queues;
620 if (txqs_n != priv->txqs_n) {
621 INFO("%p: TX queues number update: %u -> %u",
622 (void *)dev, priv->txqs_n, txqs_n);
623 priv->txqs_n = txqs_n;
625 if (rxqs_n == priv->rxqs_n)
627 if (!rte_is_power_of_2(rxqs_n) && !priv->isolated) {
630 n_active = rte_align32pow2(rxqs_n + 1) >> 1;
631 WARN("%p: number of RX queues must be a power"
632 " of 2: %u queues among %u will be active",
633 (void *)dev, n_active, rxqs_n);
636 INFO("%p: RX queues number update: %u -> %u",
637 (void *)dev, priv->rxqs_n, rxqs_n);
638 /* If RSS is enabled, disable it first. */
642 /* Only if there are no remaining child RX queues. */
643 for (i = 0; (i != priv->rxqs_n); ++i)
644 if ((*priv->rxqs)[i] != NULL)
646 priv_parent_list_cleanup(priv);
651 /* Nothing else to do. */
652 priv->rxqs_n = rxqs_n;
655 /* Allocate a new RSS parent queue if supported by hardware. */
657 ERROR("%p: only a single RX queue can be configured when"
658 " hardware doesn't support RSS",
662 /* Fail if hardware doesn't support that many RSS queues. */
663 if (rxqs_n >= priv->max_rss_tbl_sz) {
664 ERROR("%p: only %u RX queues can be configured for RSS",
665 (void *)dev, priv->max_rss_tbl_sz);
670 priv->rxqs_n = rxqs_n;
673 if (priv_parent_create(priv, NULL, priv->rxqs_n))
675 /* Failure, rollback. */
682 * DPDK callback for Ethernet device configuration.
685 * Pointer to Ethernet device structure.
688 * 0 on success, negative errno value on failure.
691 mlx4_dev_configure(struct rte_eth_dev *dev)
693 struct priv *priv = dev->data->dev_private;
697 ret = dev_configure(dev);
703 static uint16_t mlx4_tx_burst(void *, struct rte_mbuf **, uint16_t);
704 static uint16_t removed_rx_burst(void *, struct rte_mbuf **, uint16_t);
706 /* TX queues handling. */
709 * Allocate TX queue elements.
712 * Pointer to TX queue structure.
714 * Number of elements to allocate.
717 * 0 on success, errno value on failure.
720 txq_alloc_elts(struct txq *txq, unsigned int elts_n)
723 struct txq_elt (*elts)[elts_n] =
724 rte_calloc_socket("TXQ", 1, sizeof(*elts), 0, txq->socket);
725 linear_t (*elts_linear)[elts_n] =
726 rte_calloc_socket("TXQ", 1, sizeof(*elts_linear), 0,
728 struct ibv_mr *mr_linear = NULL;
731 if ((elts == NULL) || (elts_linear == NULL)) {
732 ERROR("%p: can't allocate packets array", (void *)txq);
737 ibv_reg_mr(txq->priv->pd, elts_linear, sizeof(*elts_linear),
738 IBV_ACCESS_LOCAL_WRITE);
739 if (mr_linear == NULL) {
740 ERROR("%p: unable to configure MR, ibv_reg_mr() failed",
745 for (i = 0; (i != elts_n); ++i) {
746 struct txq_elt *elt = &(*elts)[i];
750 DEBUG("%p: allocated and configured %u WRs", (void *)txq, elts_n);
751 txq->elts_n = elts_n;
756 /* Request send completion every MLX4_PMD_TX_PER_COMP_REQ packets or
757 * at least 4 times per ring. */
758 txq->elts_comp_cd_init =
759 ((MLX4_PMD_TX_PER_COMP_REQ < (elts_n / 4)) ?
760 MLX4_PMD_TX_PER_COMP_REQ : (elts_n / 4));
761 txq->elts_comp_cd = txq->elts_comp_cd_init;
762 txq->elts_linear = elts_linear;
763 txq->mr_linear = mr_linear;
767 if (mr_linear != NULL)
768 claim_zero(ibv_dereg_mr(mr_linear));
770 rte_free(elts_linear);
773 DEBUG("%p: failed, freed everything", (void *)txq);
779 * Free TX queue elements.
782 * Pointer to TX queue structure.
785 txq_free_elts(struct txq *txq)
787 unsigned int elts_n = txq->elts_n;
788 unsigned int elts_head = txq->elts_head;
789 unsigned int elts_tail = txq->elts_tail;
790 struct txq_elt (*elts)[elts_n] = txq->elts;
791 linear_t (*elts_linear)[elts_n] = txq->elts_linear;
792 struct ibv_mr *mr_linear = txq->mr_linear;
794 DEBUG("%p: freeing WRs", (void *)txq);
799 txq->elts_comp_cd = 0;
800 txq->elts_comp_cd_init = 0;
802 txq->elts_linear = NULL;
803 txq->mr_linear = NULL;
804 if (mr_linear != NULL)
805 claim_zero(ibv_dereg_mr(mr_linear));
807 rte_free(elts_linear);
810 while (elts_tail != elts_head) {
811 struct txq_elt *elt = &(*elts)[elts_tail];
813 assert(elt->buf != NULL);
814 rte_pktmbuf_free(elt->buf);
817 memset(elt, 0x77, sizeof(*elt));
819 if (++elts_tail == elts_n)
827 * Clean up a TX queue.
829 * Destroy objects, free allocated memory and reset the structure for reuse.
832 * Pointer to TX queue structure.
835 txq_cleanup(struct txq *txq)
837 struct ibv_exp_release_intf_params params;
840 DEBUG("cleaning up %p", (void *)txq);
842 if (txq->if_qp != NULL) {
843 assert(txq->priv != NULL);
844 assert(txq->priv->ctx != NULL);
845 assert(txq->qp != NULL);
846 params = (struct ibv_exp_release_intf_params){
849 claim_zero(ibv_exp_release_intf(txq->priv->ctx,
853 if (txq->if_cq != NULL) {
854 assert(txq->priv != NULL);
855 assert(txq->priv->ctx != NULL);
856 assert(txq->cq != NULL);
857 params = (struct ibv_exp_release_intf_params){
860 claim_zero(ibv_exp_release_intf(txq->priv->ctx,
865 claim_zero(ibv_destroy_qp(txq->qp));
867 claim_zero(ibv_destroy_cq(txq->cq));
868 if (txq->rd != NULL) {
869 struct ibv_exp_destroy_res_domain_attr attr = {
873 assert(txq->priv != NULL);
874 assert(txq->priv->ctx != NULL);
875 claim_zero(ibv_exp_destroy_res_domain(txq->priv->ctx,
879 for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
880 if (txq->mp2mr[i].mp == NULL)
882 assert(txq->mp2mr[i].mr != NULL);
883 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
885 memset(txq, 0, sizeof(*txq));
889 * Manage TX completions.
891 * When sending a burst, mlx4_tx_burst() posts several WRs.
892 * To improve performance, a completion event is only required once every
893 * MLX4_PMD_TX_PER_COMP_REQ sends. Doing so discards completion information
894 * for other WRs, but this information would not be used anyway.
897 * Pointer to TX queue structure.
900 * 0 on success, -1 on failure.
903 txq_complete(struct txq *txq)
905 unsigned int elts_comp = txq->elts_comp;
906 unsigned int elts_tail = txq->elts_tail;
907 const unsigned int elts_n = txq->elts_n;
910 if (unlikely(elts_comp == 0))
912 wcs_n = txq->if_cq->poll_cnt(txq->cq, elts_comp);
913 if (unlikely(wcs_n == 0))
915 if (unlikely(wcs_n < 0)) {
916 DEBUG("%p: ibv_poll_cq() failed (wcs_n=%d)",
921 assert(elts_comp <= txq->elts_comp);
923 * Assume WC status is successful as nothing can be done about it
926 elts_tail += wcs_n * txq->elts_comp_cd_init;
927 if (elts_tail >= elts_n)
929 txq->elts_tail = elts_tail;
930 txq->elts_comp = elts_comp;
934 struct mlx4_check_mempool_data {
940 /* Called by mlx4_check_mempool() when iterating the memory chunks. */
941 static void mlx4_check_mempool_cb(struct rte_mempool *mp,
942 void *opaque, struct rte_mempool_memhdr *memhdr,
945 struct mlx4_check_mempool_data *data = opaque;
950 /* It already failed, skip the next chunks. */
953 /* It is the first chunk. */
954 if (data->start == NULL && data->end == NULL) {
955 data->start = memhdr->addr;
956 data->end = data->start + memhdr->len;
959 if (data->end == memhdr->addr) {
960 data->end += memhdr->len;
963 if (data->start == (char *)memhdr->addr + memhdr->len) {
964 data->start -= memhdr->len;
967 /* Error, mempool is not virtually contigous. */
972 * Check if a mempool can be used: it must be virtually contiguous.
975 * Pointer to memory pool.
977 * Pointer to the start address of the mempool virtual memory area
979 * Pointer to the end address of the mempool virtual memory area
982 * 0 on success (mempool is virtually contiguous), -1 on error.
984 static int mlx4_check_mempool(struct rte_mempool *mp, uintptr_t *start,
987 struct mlx4_check_mempool_data data;
989 memset(&data, 0, sizeof(data));
990 rte_mempool_mem_iter(mp, mlx4_check_mempool_cb, &data);
991 *start = (uintptr_t)data.start;
992 *end = (uintptr_t)data.end;
997 /* For best performance, this function should not be inlined. */
998 static struct ibv_mr *mlx4_mp2mr(struct ibv_pd *, struct rte_mempool *)
1002 * Register mempool as a memory region.
1005 * Pointer to protection domain.
1007 * Pointer to memory pool.
1010 * Memory region pointer, NULL in case of error.
1012 static struct ibv_mr *
1013 mlx4_mp2mr(struct ibv_pd *pd, struct rte_mempool *mp)
1015 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
1020 if (mlx4_check_mempool(mp, &start, &end) != 0) {
1021 ERROR("mempool %p: not virtually contiguous",
1026 DEBUG("mempool %p area start=%p end=%p size=%zu",
1027 (void *)mp, (void *)start, (void *)end,
1028 (size_t)(end - start));
1029 /* Round start and end to page boundary if found in memory segments. */
1030 for (i = 0; (i < RTE_MAX_MEMSEG) && (ms[i].addr != NULL); ++i) {
1031 uintptr_t addr = (uintptr_t)ms[i].addr;
1032 size_t len = ms[i].len;
1033 unsigned int align = ms[i].hugepage_sz;
1035 if ((start > addr) && (start < addr + len))
1036 start = RTE_ALIGN_FLOOR(start, align);
1037 if ((end > addr) && (end < addr + len))
1038 end = RTE_ALIGN_CEIL(end, align);
1040 DEBUG("mempool %p using start=%p end=%p size=%zu for MR",
1041 (void *)mp, (void *)start, (void *)end,
1042 (size_t)(end - start));
1043 return ibv_reg_mr(pd,
1046 IBV_ACCESS_LOCAL_WRITE);
1050 * Get Memory Pool (MP) from mbuf. If mbuf is indirect, the pool from which
1051 * the cloned mbuf is allocated is returned instead.
1057 * Memory pool where data is located for given mbuf.
1059 static struct rte_mempool *
1060 txq_mb2mp(struct rte_mbuf *buf)
1062 if (unlikely(RTE_MBUF_INDIRECT(buf)))
1063 return rte_mbuf_from_indirect(buf)->pool;
1068 * Get Memory Region (MR) <-> Memory Pool (MP) association from txq->mp2mr[].
1069 * Add MP to txq->mp2mr[] if it's not registered yet. If mp2mr[] is full,
1070 * remove an entry first.
1073 * Pointer to TX queue structure.
1075 * Memory Pool for which a Memory Region lkey must be returned.
1078 * mr->lkey on success, (uint32_t)-1 on failure.
1081 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
1086 for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
1087 if (unlikely(txq->mp2mr[i].mp == NULL)) {
1088 /* Unknown MP, add a new MR for it. */
1091 if (txq->mp2mr[i].mp == mp) {
1092 assert(txq->mp2mr[i].lkey != (uint32_t)-1);
1093 assert(txq->mp2mr[i].mr->lkey == txq->mp2mr[i].lkey);
1094 return txq->mp2mr[i].lkey;
1097 /* Add a new entry, register MR first. */
1098 DEBUG("%p: discovered new memory pool \"%s\" (%p)",
1099 (void *)txq, mp->name, (void *)mp);
1100 mr = mlx4_mp2mr(txq->priv->pd, mp);
1101 if (unlikely(mr == NULL)) {
1102 DEBUG("%p: unable to configure MR, ibv_reg_mr() failed.",
1104 return (uint32_t)-1;
1106 if (unlikely(i == elemof(txq->mp2mr))) {
1107 /* Table is full, remove oldest entry. */
1108 DEBUG("%p: MR <-> MP table full, dropping oldest entry.",
1111 claim_zero(ibv_dereg_mr(txq->mp2mr[0].mr));
1112 memmove(&txq->mp2mr[0], &txq->mp2mr[1],
1113 (sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
1115 /* Store the new entry. */
1116 txq->mp2mr[i].mp = mp;
1117 txq->mp2mr[i].mr = mr;
1118 txq->mp2mr[i].lkey = mr->lkey;
1119 DEBUG("%p: new MR lkey for MP \"%s\" (%p): 0x%08" PRIu32,
1120 (void *)txq, mp->name, (void *)mp, txq->mp2mr[i].lkey);
1121 return txq->mp2mr[i].lkey;
1124 struct txq_mp2mr_mbuf_check_data {
1129 * Callback function for rte_mempool_obj_iter() to check whether a given
1130 * mempool object looks like a mbuf.
1133 * The mempool pointer
1135 * Context data (struct txq_mp2mr_mbuf_check_data). Contains the
1140 * Object index, unused.
1143 txq_mp2mr_mbuf_check(struct rte_mempool *mp, void *arg, void *obj,
1144 uint32_t index __rte_unused)
1146 struct txq_mp2mr_mbuf_check_data *data = arg;
1147 struct rte_mbuf *buf = obj;
1149 /* Check whether mbuf structure fits element size and whether mempool
1150 * pointer is valid. */
1151 if (sizeof(*buf) > mp->elt_size || buf->pool != mp)
1156 * Iterator function for rte_mempool_walk() to register existing mempools and
1157 * fill the MP to MR cache of a TX queue.
1160 * Memory Pool to register.
1162 * Pointer to TX queue structure.
1165 txq_mp2mr_iter(struct rte_mempool *mp, void *arg)
1167 struct txq *txq = arg;
1168 struct txq_mp2mr_mbuf_check_data data = {
1172 /* Register mempool only if the first element looks like a mbuf. */
1173 if (rte_mempool_obj_iter(mp, txq_mp2mr_mbuf_check, &data) == 0 ||
1180 * Copy scattered mbuf contents to a single linear buffer.
1182 * @param[out] linear
1183 * Linear output buffer.
1185 * Scattered input buffer.
1188 * Number of bytes copied to the output buffer or 0 if not large enough.
1191 linearize_mbuf(linear_t *linear, struct rte_mbuf *buf)
1193 unsigned int size = 0;
1194 unsigned int offset;
1197 unsigned int len = DATA_LEN(buf);
1201 if (unlikely(size > sizeof(*linear)))
1203 memcpy(&(*linear)[offset],
1204 rte_pktmbuf_mtod(buf, uint8_t *),
1207 } while (buf != NULL);
1212 * Handle scattered buffers for mlx4_tx_burst().
1215 * TX queue structure.
1217 * Number of segments in buf.
1219 * TX queue element to fill.
1221 * Buffer to process.
1223 * Index of the linear buffer to use if necessary (normally txq->elts_head).
1225 * Array filled with SGEs on success.
1228 * A structure containing the processed packet size in bytes and the
1229 * number of SGEs. Both fields are set to (unsigned int)-1 in case of
1232 static struct tx_burst_sg_ret {
1233 unsigned int length;
1236 tx_burst_sg(struct txq *txq, unsigned int segs, struct txq_elt *elt,
1237 struct rte_mbuf *buf, unsigned int elts_head,
1238 struct ibv_sge (*sges)[MLX4_PMD_SGE_WR_N])
1240 unsigned int sent_size = 0;
1244 /* When there are too many segments, extra segments are
1245 * linearized in the last SGE. */
1246 if (unlikely(segs > elemof(*sges))) {
1247 segs = (elemof(*sges) - 1);
1250 /* Update element. */
1252 /* Register segments as SGEs. */
1253 for (j = 0; (j != segs); ++j) {
1254 struct ibv_sge *sge = &(*sges)[j];
1257 /* Retrieve Memory Region key for this memory pool. */
1258 lkey = txq_mp2mr(txq, txq_mb2mp(buf));
1259 if (unlikely(lkey == (uint32_t)-1)) {
1260 /* MR does not exist. */
1261 DEBUG("%p: unable to get MP <-> MR association",
1263 /* Clean up TX element. */
1268 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
1270 rte_prefetch0((volatile void *)
1271 (uintptr_t)sge->addr);
1272 sge->length = DATA_LEN(buf);
1274 sent_size += sge->length;
1277 /* If buf is not NULL here and is not going to be linearized,
1278 * nb_segs is not valid. */
1280 assert((buf == NULL) || (linearize));
1281 /* Linearize extra segments. */
1283 struct ibv_sge *sge = &(*sges)[segs];
1284 linear_t *linear = &(*txq->elts_linear)[elts_head];
1285 unsigned int size = linearize_mbuf(linear, buf);
1287 assert(segs == (elemof(*sges) - 1));
1289 /* Invalid packet. */
1290 DEBUG("%p: packet too large to be linearized.",
1292 /* Clean up TX element. */
1296 /* If MLX4_PMD_SGE_WR_N is 1, free mbuf immediately. */
1297 if (elemof(*sges) == 1) {
1299 struct rte_mbuf *next = NEXT(buf);
1301 rte_pktmbuf_free_seg(buf);
1303 } while (buf != NULL);
1307 sge->addr = (uintptr_t)&(*linear)[0];
1309 sge->lkey = txq->mr_linear->lkey;
1311 /* Include last segment. */
1314 return (struct tx_burst_sg_ret){
1315 .length = sent_size,
1319 return (struct tx_burst_sg_ret){
1326 * DPDK callback for TX.
1329 * Generic pointer to TX queue structure.
1331 * Packets to transmit.
1333 * Number of packets in array.
1336 * Number of packets successfully transmitted (<= pkts_n).
1339 mlx4_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
1341 struct txq *txq = (struct txq *)dpdk_txq;
1342 unsigned int elts_head = txq->elts_head;
1343 const unsigned int elts_n = txq->elts_n;
1344 unsigned int elts_comp_cd = txq->elts_comp_cd;
1345 unsigned int elts_comp = 0;
1350 assert(elts_comp_cd != 0);
1352 max = (elts_n - (elts_head - txq->elts_tail));
1356 assert(max <= elts_n);
1357 /* Always leave one free entry in the ring. */
1363 for (i = 0; (i != max); ++i) {
1364 struct rte_mbuf *buf = pkts[i];
1365 unsigned int elts_head_next =
1366 (((elts_head + 1) == elts_n) ? 0 : elts_head + 1);
1367 struct txq_elt *elt_next = &(*txq->elts)[elts_head_next];
1368 struct txq_elt *elt = &(*txq->elts)[elts_head];
1369 unsigned int segs = NB_SEGS(buf);
1370 unsigned int sent_size = 0;
1371 uint32_t send_flags = 0;
1373 /* Clean up old buffer. */
1374 if (likely(elt->buf != NULL)) {
1375 struct rte_mbuf *tmp = elt->buf;
1379 memset(elt, 0x66, sizeof(*elt));
1381 /* Faster than rte_pktmbuf_free(). */
1383 struct rte_mbuf *next = NEXT(tmp);
1385 rte_pktmbuf_free_seg(tmp);
1387 } while (tmp != NULL);
1389 /* Request TX completion. */
1390 if (unlikely(--elts_comp_cd == 0)) {
1391 elts_comp_cd = txq->elts_comp_cd_init;
1393 send_flags |= IBV_EXP_QP_BURST_SIGNALED;
1395 /* Should we enable HW CKSUM offload */
1397 (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM)) {
1398 send_flags |= IBV_EXP_QP_BURST_IP_CSUM;
1399 /* HW does not support checksum offloads at arbitrary
1400 * offsets but automatically recognizes the packet
1401 * type. For inner L3/L4 checksums, only VXLAN (UDP)
1402 * tunnels are currently supported. */
1403 if (RTE_ETH_IS_TUNNEL_PKT(buf->packet_type))
1404 send_flags |= IBV_EXP_QP_BURST_TUNNEL;
1406 if (likely(segs == 1)) {
1411 /* Retrieve buffer information. */
1412 addr = rte_pktmbuf_mtod(buf, uintptr_t);
1413 length = DATA_LEN(buf);
1414 /* Retrieve Memory Region key for this memory pool. */
1415 lkey = txq_mp2mr(txq, txq_mb2mp(buf));
1416 if (unlikely(lkey == (uint32_t)-1)) {
1417 /* MR does not exist. */
1418 DEBUG("%p: unable to get MP <-> MR"
1419 " association", (void *)txq);
1420 /* Clean up TX element. */
1424 /* Update element. */
1427 rte_prefetch0((volatile void *)
1429 RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
1430 /* Put packet into send queue. */
1431 if (length <= txq->max_inline)
1432 err = txq->if_qp->send_pending_inline
1438 err = txq->if_qp->send_pending
1446 sent_size += length;
1448 struct ibv_sge sges[MLX4_PMD_SGE_WR_N];
1449 struct tx_burst_sg_ret ret;
1451 ret = tx_burst_sg(txq, segs, elt, buf, elts_head,
1453 if (ret.length == (unsigned int)-1)
1455 RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
1456 /* Put SG list into send queue. */
1457 err = txq->if_qp->send_pending_sg_list
1464 sent_size += ret.length;
1466 elts_head = elts_head_next;
1467 /* Increment sent bytes counter. */
1468 txq->stats.obytes += sent_size;
1471 /* Take a shortcut if nothing must be sent. */
1472 if (unlikely(i == 0))
1474 /* Increment sent packets counter. */
1475 txq->stats.opackets += i;
1476 /* Ring QP doorbell. */
1477 err = txq->if_qp->send_flush(txq->qp);
1478 if (unlikely(err)) {
1479 /* A nonzero value is not supposed to be returned.
1480 * Nothing can be done about it. */
1481 DEBUG("%p: send_flush() failed with error %d",
1484 txq->elts_head = elts_head;
1485 txq->elts_comp += elts_comp;
1486 txq->elts_comp_cd = elts_comp_cd;
1491 * Configure a TX queue.
1494 * Pointer to Ethernet device structure.
1496 * Pointer to TX queue structure.
1498 * Number of descriptors to configure in queue.
1500 * NUMA socket on which memory must be allocated.
1502 * Thresholds parameters.
1505 * 0 on success, errno value on failure.
1508 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
1509 unsigned int socket, const struct rte_eth_txconf *conf)
1511 struct priv *priv = dev->data->dev_private;
1517 struct ibv_exp_query_intf_params params;
1518 struct ibv_exp_qp_init_attr init;
1519 struct ibv_exp_res_domain_init_attr rd;
1520 struct ibv_exp_cq_init_attr cq;
1521 struct ibv_exp_qp_attr mod;
1523 enum ibv_exp_query_intf_status status;
1526 (void)conf; /* Thresholds configuration (ignored). */
1529 if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
1530 ERROR("%p: invalid number of TX descriptors (must be a"
1531 " multiple of %d)", (void *)dev, MLX4_PMD_SGE_WR_N);
1534 desc /= MLX4_PMD_SGE_WR_N;
1535 /* MRs will be registered in mp2mr[] later. */
1536 attr.rd = (struct ibv_exp_res_domain_init_attr){
1537 .comp_mask = (IBV_EXP_RES_DOMAIN_THREAD_MODEL |
1538 IBV_EXP_RES_DOMAIN_MSG_MODEL),
1539 .thread_model = IBV_EXP_THREAD_SINGLE,
1540 .msg_model = IBV_EXP_MSG_HIGH_BW,
1542 tmpl.rd = ibv_exp_create_res_domain(priv->ctx, &attr.rd);
1543 if (tmpl.rd == NULL) {
1545 ERROR("%p: RD creation failure: %s",
1546 (void *)dev, strerror(ret));
1549 attr.cq = (struct ibv_exp_cq_init_attr){
1550 .comp_mask = IBV_EXP_CQ_INIT_ATTR_RES_DOMAIN,
1551 .res_domain = tmpl.rd,
1553 tmpl.cq = ibv_exp_create_cq(priv->ctx, desc, NULL, NULL, 0, &attr.cq);
1554 if (tmpl.cq == NULL) {
1556 ERROR("%p: CQ creation failure: %s",
1557 (void *)dev, strerror(ret));
1560 DEBUG("priv->device_attr.max_qp_wr is %d",
1561 priv->device_attr.max_qp_wr);
1562 DEBUG("priv->device_attr.max_sge is %d",
1563 priv->device_attr.max_sge);
1564 attr.init = (struct ibv_exp_qp_init_attr){
1565 /* CQ to be associated with the send queue. */
1567 /* CQ to be associated with the receive queue. */
1570 /* Max number of outstanding WRs. */
1571 .max_send_wr = ((priv->device_attr.max_qp_wr < desc) ?
1572 priv->device_attr.max_qp_wr :
1574 /* Max number of scatter/gather elements in a WR. */
1575 .max_send_sge = ((priv->device_attr.max_sge <
1576 MLX4_PMD_SGE_WR_N) ?
1577 priv->device_attr.max_sge :
1579 .max_inline_data = MLX4_PMD_MAX_INLINE,
1581 .qp_type = IBV_QPT_RAW_PACKET,
1582 /* Do *NOT* enable this, completions events are managed per
1586 .res_domain = tmpl.rd,
1587 .comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
1588 IBV_EXP_QP_INIT_ATTR_RES_DOMAIN),
1590 tmpl.qp = ibv_exp_create_qp(priv->ctx, &attr.init);
1591 if (tmpl.qp == NULL) {
1592 ret = (errno ? errno : EINVAL);
1593 ERROR("%p: QP creation failure: %s",
1594 (void *)dev, strerror(ret));
1597 /* ibv_create_qp() updates this value. */
1598 tmpl.max_inline = attr.init.cap.max_inline_data;
1599 attr.mod = (struct ibv_exp_qp_attr){
1600 /* Move the QP to this state. */
1601 .qp_state = IBV_QPS_INIT,
1602 /* Primary port number. */
1603 .port_num = priv->port
1605 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod,
1606 (IBV_EXP_QP_STATE | IBV_EXP_QP_PORT));
1608 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
1609 (void *)dev, strerror(ret));
1612 ret = txq_alloc_elts(&tmpl, desc);
1614 ERROR("%p: TXQ allocation failed: %s",
1615 (void *)dev, strerror(ret));
1618 attr.mod = (struct ibv_exp_qp_attr){
1619 .qp_state = IBV_QPS_RTR
1621 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1623 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
1624 (void *)dev, strerror(ret));
1627 attr.mod.qp_state = IBV_QPS_RTS;
1628 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1630 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
1631 (void *)dev, strerror(ret));
1634 attr.params = (struct ibv_exp_query_intf_params){
1635 .intf_scope = IBV_EXP_INTF_GLOBAL,
1636 .intf = IBV_EXP_INTF_CQ,
1639 tmpl.if_cq = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
1640 if (tmpl.if_cq == NULL) {
1641 ERROR("%p: CQ interface family query failed with status %d",
1642 (void *)dev, status);
1645 attr.params = (struct ibv_exp_query_intf_params){
1646 .intf_scope = IBV_EXP_INTF_GLOBAL,
1647 .intf = IBV_EXP_INTF_QP_BURST,
1649 #ifdef HAVE_EXP_QP_BURST_CREATE_DISABLE_ETH_LOOPBACK
1650 /* MC loopback must be disabled when not using a VF. */
1653 IBV_EXP_QP_BURST_CREATE_DISABLE_ETH_LOOPBACK :
1657 tmpl.if_qp = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
1658 if (tmpl.if_qp == NULL) {
1659 ERROR("%p: QP interface family query failed with status %d",
1660 (void *)dev, status);
1663 /* Clean up txq in case we're reinitializing it. */
1664 DEBUG("%p: cleaning-up old txq just in case", (void *)txq);
1667 DEBUG("%p: txq updated with %p", (void *)txq, (void *)&tmpl);
1668 /* Pre-register known mempools. */
1669 rte_mempool_walk(txq_mp2mr_iter, txq);
1679 * DPDK callback to configure a TX queue.
1682 * Pointer to Ethernet device structure.
1686 * Number of descriptors to configure in queue.
1688 * NUMA socket on which memory must be allocated.
1690 * Thresholds parameters.
1693 * 0 on success, negative errno value on failure.
1696 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1697 unsigned int socket, const struct rte_eth_txconf *conf)
1699 struct priv *priv = dev->data->dev_private;
1700 struct txq *txq = (*priv->txqs)[idx];
1704 DEBUG("%p: configuring queue %u for %u descriptors",
1705 (void *)dev, idx, desc);
1706 if (idx >= priv->txqs_n) {
1707 ERROR("%p: queue index out of range (%u >= %u)",
1708 (void *)dev, idx, priv->txqs_n);
1713 DEBUG("%p: reusing already allocated queue index %u (%p)",
1714 (void *)dev, idx, (void *)txq);
1715 if (priv->started) {
1719 (*priv->txqs)[idx] = NULL;
1722 txq = rte_calloc_socket("TXQ", 1, sizeof(*txq), 0, socket);
1724 ERROR("%p: unable to allocate queue index %u",
1730 ret = txq_setup(dev, txq, desc, socket, conf);
1734 txq->stats.idx = idx;
1735 DEBUG("%p: adding TX queue %p to list",
1736 (void *)dev, (void *)txq);
1737 (*priv->txqs)[idx] = txq;
1738 /* Update send callback. */
1739 dev->tx_pkt_burst = mlx4_tx_burst;
1746 * DPDK callback to release a TX queue.
1749 * Generic TX queue pointer.
1752 mlx4_tx_queue_release(void *dpdk_txq)
1754 struct txq *txq = (struct txq *)dpdk_txq;
1762 for (i = 0; (i != priv->txqs_n); ++i)
1763 if ((*priv->txqs)[i] == txq) {
1764 DEBUG("%p: removing TX queue %p from list",
1765 (void *)priv->dev, (void *)txq);
1766 (*priv->txqs)[i] = NULL;
1774 /* RX queues handling. */
1777 * Allocate RX queue elements with scattered packets support.
1780 * Pointer to RX queue structure.
1782 * Number of elements to allocate.
1784 * If not NULL, fetch buffers from this array instead of allocating them
1785 * with rte_pktmbuf_alloc().
1788 * 0 on success, errno value on failure.
1791 rxq_alloc_elts_sp(struct rxq *rxq, unsigned int elts_n,
1792 struct rte_mbuf **pool)
1795 struct rxq_elt_sp (*elts)[elts_n] =
1796 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1801 ERROR("%p: can't allocate packets array", (void *)rxq);
1805 /* For each WR (packet). */
1806 for (i = 0; (i != elts_n); ++i) {
1808 struct rxq_elt_sp *elt = &(*elts)[i];
1809 struct ibv_recv_wr *wr = &elt->wr;
1810 struct ibv_sge (*sges)[(elemof(elt->sges))] = &elt->sges;
1812 /* These two arrays must have the same size. */
1813 assert(elemof(elt->sges) == elemof(elt->bufs));
1816 wr->next = &(*elts)[(i + 1)].wr;
1817 wr->sg_list = &(*sges)[0];
1818 wr->num_sge = elemof(*sges);
1819 /* For each SGE (segment). */
1820 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1821 struct ibv_sge *sge = &(*sges)[j];
1822 struct rte_mbuf *buf;
1826 assert(buf != NULL);
1827 rte_pktmbuf_reset(buf);
1829 buf = rte_pktmbuf_alloc(rxq->mp);
1831 assert(pool == NULL);
1832 ERROR("%p: empty mbuf pool", (void *)rxq);
1837 /* Headroom is reserved by rte_pktmbuf_alloc(). */
1838 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1839 /* Buffer is supposed to be empty. */
1840 assert(rte_pktmbuf_data_len(buf) == 0);
1841 assert(rte_pktmbuf_pkt_len(buf) == 0);
1842 /* sge->addr must be able to store a pointer. */
1843 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1845 /* The first SGE keeps its headroom. */
1846 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
1847 sge->length = (buf->buf_len -
1848 RTE_PKTMBUF_HEADROOM);
1850 /* Subsequent SGEs lose theirs. */
1851 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1852 SET_DATA_OFF(buf, 0);
1853 sge->addr = (uintptr_t)buf->buf_addr;
1854 sge->length = buf->buf_len;
1856 sge->lkey = rxq->mr->lkey;
1857 /* Redundant check for tailroom. */
1858 assert(sge->length == rte_pktmbuf_tailroom(buf));
1861 /* The last WR pointer must be NULL. */
1862 (*elts)[(i - 1)].wr.next = NULL;
1863 DEBUG("%p: allocated and configured %u WRs (%zu segments)",
1864 (void *)rxq, elts_n, (elts_n * elemof((*elts)[0].sges)));
1865 rxq->elts_n = elts_n;
1867 rxq->elts.sp = elts;
1872 assert(pool == NULL);
1873 for (i = 0; (i != elemof(*elts)); ++i) {
1875 struct rxq_elt_sp *elt = &(*elts)[i];
1877 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1878 struct rte_mbuf *buf = elt->bufs[j];
1881 rte_pktmbuf_free_seg(buf);
1886 DEBUG("%p: failed, freed everything", (void *)rxq);
1892 * Free RX queue elements with scattered packets support.
1895 * Pointer to RX queue structure.
1898 rxq_free_elts_sp(struct rxq *rxq)
1901 unsigned int elts_n = rxq->elts_n;
1902 struct rxq_elt_sp (*elts)[elts_n] = rxq->elts.sp;
1904 DEBUG("%p: freeing WRs", (void *)rxq);
1906 rxq->elts.sp = NULL;
1909 for (i = 0; (i != elemof(*elts)); ++i) {
1911 struct rxq_elt_sp *elt = &(*elts)[i];
1913 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1914 struct rte_mbuf *buf = elt->bufs[j];
1917 rte_pktmbuf_free_seg(buf);
1924 * Allocate RX queue elements.
1927 * Pointer to RX queue structure.
1929 * Number of elements to allocate.
1931 * If not NULL, fetch buffers from this array instead of allocating them
1932 * with rte_pktmbuf_alloc().
1935 * 0 on success, errno value on failure.
1938 rxq_alloc_elts(struct rxq *rxq, unsigned int elts_n, struct rte_mbuf **pool)
1941 struct rxq_elt (*elts)[elts_n] =
1942 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1947 ERROR("%p: can't allocate packets array", (void *)rxq);
1951 /* For each WR (packet). */
1952 for (i = 0; (i != elts_n); ++i) {
1953 struct rxq_elt *elt = &(*elts)[i];
1954 struct ibv_recv_wr *wr = &elt->wr;
1955 struct ibv_sge *sge = &(*elts)[i].sge;
1956 struct rte_mbuf *buf;
1960 assert(buf != NULL);
1961 rte_pktmbuf_reset(buf);
1963 buf = rte_pktmbuf_alloc(rxq->mp);
1965 assert(pool == NULL);
1966 ERROR("%p: empty mbuf pool", (void *)rxq);
1970 /* Configure WR. Work request ID contains its own index in
1971 * the elts array and the offset between SGE buffer header and
1973 WR_ID(wr->wr_id).id = i;
1974 WR_ID(wr->wr_id).offset =
1975 (((uintptr_t)buf->buf_addr + RTE_PKTMBUF_HEADROOM) -
1977 wr->next = &(*elts)[(i + 1)].wr;
1980 /* Headroom is reserved by rte_pktmbuf_alloc(). */
1981 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1982 /* Buffer is supposed to be empty. */
1983 assert(rte_pktmbuf_data_len(buf) == 0);
1984 assert(rte_pktmbuf_pkt_len(buf) == 0);
1985 /* sge->addr must be able to store a pointer. */
1986 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1987 /* SGE keeps its headroom. */
1988 sge->addr = (uintptr_t)
1989 ((uint8_t *)buf->buf_addr + RTE_PKTMBUF_HEADROOM);
1990 sge->length = (buf->buf_len - RTE_PKTMBUF_HEADROOM);
1991 sge->lkey = rxq->mr->lkey;
1992 /* Redundant check for tailroom. */
1993 assert(sge->length == rte_pktmbuf_tailroom(buf));
1994 /* Make sure elts index and SGE mbuf pointer can be deduced
1996 if ((WR_ID(wr->wr_id).id != i) ||
1997 ((void *)((uintptr_t)sge->addr -
1998 WR_ID(wr->wr_id).offset) != buf)) {
1999 ERROR("%p: cannot store index and offset in WR ID",
2002 rte_pktmbuf_free(buf);
2007 /* The last WR pointer must be NULL. */
2008 (*elts)[(i - 1)].wr.next = NULL;
2009 DEBUG("%p: allocated and configured %u single-segment WRs",
2010 (void *)rxq, elts_n);
2011 rxq->elts_n = elts_n;
2013 rxq->elts.no_sp = elts;
2018 assert(pool == NULL);
2019 for (i = 0; (i != elemof(*elts)); ++i) {
2020 struct rxq_elt *elt = &(*elts)[i];
2021 struct rte_mbuf *buf;
2023 if (elt->sge.addr == 0)
2025 assert(WR_ID(elt->wr.wr_id).id == i);
2026 buf = (void *)((uintptr_t)elt->sge.addr -
2027 WR_ID(elt->wr.wr_id).offset);
2028 rte_pktmbuf_free_seg(buf);
2032 DEBUG("%p: failed, freed everything", (void *)rxq);
2038 * Free RX queue elements.
2041 * Pointer to RX queue structure.
2044 rxq_free_elts(struct rxq *rxq)
2047 unsigned int elts_n = rxq->elts_n;
2048 struct rxq_elt (*elts)[elts_n] = rxq->elts.no_sp;
2050 DEBUG("%p: freeing WRs", (void *)rxq);
2052 rxq->elts.no_sp = NULL;
2055 for (i = 0; (i != elemof(*elts)); ++i) {
2056 struct rxq_elt *elt = &(*elts)[i];
2057 struct rte_mbuf *buf;
2059 if (elt->sge.addr == 0)
2061 assert(WR_ID(elt->wr.wr_id).id == i);
2062 buf = (void *)((uintptr_t)elt->sge.addr -
2063 WR_ID(elt->wr.wr_id).offset);
2064 rte_pktmbuf_free_seg(buf);
2070 * Delete flow steering rule.
2073 * Pointer to RX queue structure.
2075 * MAC address index.
2080 rxq_del_flow(struct rxq *rxq, unsigned int mac_index, unsigned int vlan_index)
2083 struct priv *priv = rxq->priv;
2084 const uint8_t (*mac)[ETHER_ADDR_LEN] =
2085 (const uint8_t (*)[ETHER_ADDR_LEN])
2086 priv->mac[mac_index].addr_bytes;
2088 assert(rxq->mac_flow[mac_index][vlan_index] != NULL);
2089 DEBUG("%p: removing MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
2090 " (VLAN ID %" PRIu16 ")",
2092 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
2093 mac_index, priv->vlan_filter[vlan_index].id);
2094 claim_zero(ibv_destroy_flow(rxq->mac_flow[mac_index][vlan_index]));
2095 rxq->mac_flow[mac_index][vlan_index] = NULL;
2099 * Unregister a MAC address from a RX queue.
2102 * Pointer to RX queue structure.
2104 * MAC address index.
2107 rxq_mac_addr_del(struct rxq *rxq, unsigned int mac_index)
2109 struct priv *priv = rxq->priv;
2111 unsigned int vlans = 0;
2113 assert(mac_index < elemof(priv->mac));
2114 if (!BITFIELD_ISSET(rxq->mac_configured, mac_index))
2116 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
2117 if (!priv->vlan_filter[i].enabled)
2119 rxq_del_flow(rxq, mac_index, i);
2123 rxq_del_flow(rxq, mac_index, 0);
2125 BITFIELD_RESET(rxq->mac_configured, mac_index);
2129 * Unregister all MAC addresses from a RX queue.
2132 * Pointer to RX queue structure.
2135 rxq_mac_addrs_del(struct rxq *rxq)
2137 struct priv *priv = rxq->priv;
2140 for (i = 0; (i != elemof(priv->mac)); ++i)
2141 rxq_mac_addr_del(rxq, i);
2144 static int rxq_promiscuous_enable(struct rxq *);
2145 static void rxq_promiscuous_disable(struct rxq *);
2148 * Add single flow steering rule.
2151 * Pointer to RX queue structure.
2153 * MAC address index to register.
2155 * VLAN index. Use -1 for a flow without VLAN.
2158 * 0 on success, errno value on failure.
2161 rxq_add_flow(struct rxq *rxq, unsigned int mac_index, unsigned int vlan_index)
2163 struct ibv_flow *flow;
2164 struct priv *priv = rxq->priv;
2165 const uint8_t (*mac)[ETHER_ADDR_LEN] =
2166 (const uint8_t (*)[ETHER_ADDR_LEN])
2167 priv->mac[mac_index].addr_bytes;
2169 /* Allocate flow specification on the stack. */
2170 struct __attribute__((packed)) {
2171 struct ibv_flow_attr attr;
2172 struct ibv_flow_spec_eth spec;
2174 struct ibv_flow_attr *attr = &data.attr;
2175 struct ibv_flow_spec_eth *spec = &data.spec;
2177 assert(mac_index < elemof(priv->mac));
2178 assert((vlan_index < elemof(priv->vlan_filter)) || (vlan_index == -1u));
2180 * No padding must be inserted by the compiler between attr and spec.
2181 * This layout is expected by libibverbs.
2183 assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec);
2184 *attr = (struct ibv_flow_attr){
2185 .type = IBV_FLOW_ATTR_NORMAL,
2191 *spec = (struct ibv_flow_spec_eth){
2192 .type = IBV_FLOW_SPEC_ETH,
2193 .size = sizeof(*spec),
2196 (*mac)[0], (*mac)[1], (*mac)[2],
2197 (*mac)[3], (*mac)[4], (*mac)[5]
2199 .vlan_tag = ((vlan_index != -1u) ?
2200 htons(priv->vlan_filter[vlan_index].id) :
2204 .dst_mac = "\xff\xff\xff\xff\xff\xff",
2205 .vlan_tag = ((vlan_index != -1u) ? htons(0xfff) : 0),
2208 DEBUG("%p: adding MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
2209 " (VLAN %s %" PRIu16 ")",
2211 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
2213 ((vlan_index != -1u) ? "ID" : "index"),
2214 ((vlan_index != -1u) ? priv->vlan_filter[vlan_index].id : -1u));
2215 /* Create related flow. */
2217 flow = ibv_create_flow(rxq->qp, attr);
2219 /* It's not clear whether errno is always set in this case. */
2220 ERROR("%p: flow configuration failed, errno=%d: %s",
2222 (errno ? strerror(errno) : "Unknown error"));
2227 if (vlan_index == -1u)
2229 assert(rxq->mac_flow[mac_index][vlan_index] == NULL);
2230 rxq->mac_flow[mac_index][vlan_index] = flow;
2235 * Register a MAC address in a RX queue.
2238 * Pointer to RX queue structure.
2240 * MAC address index to register.
2243 * 0 on success, errno value on failure.
2246 rxq_mac_addr_add(struct rxq *rxq, unsigned int mac_index)
2248 struct priv *priv = rxq->priv;
2250 unsigned int vlans = 0;
2253 assert(mac_index < elemof(priv->mac));
2254 if (BITFIELD_ISSET(rxq->mac_configured, mac_index))
2255 rxq_mac_addr_del(rxq, mac_index);
2256 /* Fill VLAN specifications. */
2257 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
2258 if (!priv->vlan_filter[i].enabled)
2260 /* Create related flow. */
2261 ret = rxq_add_flow(rxq, mac_index, i);
2266 /* Failure, rollback. */
2268 if (priv->vlan_filter[--i].enabled)
2269 rxq_del_flow(rxq, mac_index, i);
2273 /* In case there is no VLAN filter. */
2275 ret = rxq_add_flow(rxq, mac_index, -1);
2279 BITFIELD_SET(rxq->mac_configured, mac_index);
2284 * Register all MAC addresses in a RX queue.
2287 * Pointer to RX queue structure.
2290 * 0 on success, errno value on failure.
2293 rxq_mac_addrs_add(struct rxq *rxq)
2295 struct priv *priv = rxq->priv;
2299 for (i = 0; (i != elemof(priv->mac)); ++i) {
2300 if (!BITFIELD_ISSET(priv->mac_configured, i))
2302 ret = rxq_mac_addr_add(rxq, i);
2305 /* Failure, rollback. */
2307 rxq_mac_addr_del(rxq, --i);
2315 * Unregister a MAC address.
2317 * In RSS mode, the MAC address is unregistered from the parent queue,
2318 * otherwise it is unregistered from each queue directly.
2321 * Pointer to private structure.
2323 * MAC address index.
2326 priv_mac_addr_del(struct priv *priv, unsigned int mac_index)
2330 assert(!priv->isolated);
2331 assert(mac_index < elemof(priv->mac));
2332 if (!BITFIELD_ISSET(priv->mac_configured, mac_index))
2335 rxq_mac_addr_del(LIST_FIRST(&priv->parents), mac_index);
2338 for (i = 0; (i != priv->dev->data->nb_rx_queues); ++i)
2339 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2341 BITFIELD_RESET(priv->mac_configured, mac_index);
2345 * Register a MAC address.
2347 * In RSS mode, the MAC address is registered in the parent queue,
2348 * otherwise it is registered in each queue directly.
2351 * Pointer to private structure.
2353 * MAC address index to use.
2355 * MAC address to register.
2358 * 0 on success, errno value on failure.
2361 priv_mac_addr_add(struct priv *priv, unsigned int mac_index,
2362 const uint8_t (*mac)[ETHER_ADDR_LEN])
2367 assert(mac_index < elemof(priv->mac));
2368 /* First, make sure this address isn't already configured. */
2369 for (i = 0; (i != elemof(priv->mac)); ++i) {
2370 /* Skip this index, it's going to be reconfigured. */
2373 if (!BITFIELD_ISSET(priv->mac_configured, i))
2375 if (memcmp(priv->mac[i].addr_bytes, *mac, sizeof(*mac)))
2377 /* Address already configured elsewhere, return with error. */
2380 if (BITFIELD_ISSET(priv->mac_configured, mac_index))
2381 priv_mac_addr_del(priv, mac_index);
2382 priv->mac[mac_index] = (struct ether_addr){
2384 (*mac)[0], (*mac)[1], (*mac)[2],
2385 (*mac)[3], (*mac)[4], (*mac)[5]
2388 /* If device isn't started, this is all we need to do. */
2389 if (!priv->started) {
2391 /* Verify that all queues have this index disabled. */
2392 for (i = 0; (i != priv->rxqs_n); ++i) {
2393 if ((*priv->rxqs)[i] == NULL)
2395 assert(!BITFIELD_ISSET
2396 ((*priv->rxqs)[i]->mac_configured, mac_index));
2402 ret = rxq_mac_addr_add(LIST_FIRST(&priv->parents), mac_index);
2407 for (i = 0; (i != priv->rxqs_n); ++i) {
2408 if ((*priv->rxqs)[i] == NULL)
2410 ret = rxq_mac_addr_add((*priv->rxqs)[i], mac_index);
2413 /* Failure, rollback. */
2415 if ((*priv->rxqs)[(--i)] != NULL)
2416 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2420 BITFIELD_SET(priv->mac_configured, mac_index);
2425 * Enable allmulti mode in a RX queue.
2428 * Pointer to RX queue structure.
2431 * 0 on success, errno value on failure.
2434 rxq_allmulticast_enable(struct rxq *rxq)
2436 struct ibv_flow *flow;
2437 struct ibv_flow_attr attr = {
2438 .type = IBV_FLOW_ATTR_MC_DEFAULT,
2440 .port = rxq->priv->port,
2444 DEBUG("%p: enabling allmulticast mode", (void *)rxq);
2445 if (rxq->allmulti_flow != NULL)
2448 flow = ibv_create_flow(rxq->qp, &attr);
2450 /* It's not clear whether errno is always set in this case. */
2451 ERROR("%p: flow configuration failed, errno=%d: %s",
2453 (errno ? strerror(errno) : "Unknown error"));
2458 rxq->allmulti_flow = flow;
2459 DEBUG("%p: allmulticast mode enabled", (void *)rxq);
2464 * Disable allmulti mode in a RX queue.
2467 * Pointer to RX queue structure.
2470 rxq_allmulticast_disable(struct rxq *rxq)
2472 DEBUG("%p: disabling allmulticast mode", (void *)rxq);
2473 if (rxq->allmulti_flow == NULL)
2475 claim_zero(ibv_destroy_flow(rxq->allmulti_flow));
2476 rxq->allmulti_flow = NULL;
2477 DEBUG("%p: allmulticast mode disabled", (void *)rxq);
2481 * Enable promiscuous mode in a RX queue.
2484 * Pointer to RX queue structure.
2487 * 0 on success, errno value on failure.
2490 rxq_promiscuous_enable(struct rxq *rxq)
2492 struct ibv_flow *flow;
2493 struct ibv_flow_attr attr = {
2494 .type = IBV_FLOW_ATTR_ALL_DEFAULT,
2496 .port = rxq->priv->port,
2502 DEBUG("%p: enabling promiscuous mode", (void *)rxq);
2503 if (rxq->promisc_flow != NULL)
2506 flow = ibv_create_flow(rxq->qp, &attr);
2508 /* It's not clear whether errno is always set in this case. */
2509 ERROR("%p: flow configuration failed, errno=%d: %s",
2511 (errno ? strerror(errno) : "Unknown error"));
2516 rxq->promisc_flow = flow;
2517 DEBUG("%p: promiscuous mode enabled", (void *)rxq);
2522 * Disable promiscuous mode in a RX queue.
2525 * Pointer to RX queue structure.
2528 rxq_promiscuous_disable(struct rxq *rxq)
2532 DEBUG("%p: disabling promiscuous mode", (void *)rxq);
2533 if (rxq->promisc_flow == NULL)
2535 claim_zero(ibv_destroy_flow(rxq->promisc_flow));
2536 rxq->promisc_flow = NULL;
2537 DEBUG("%p: promiscuous mode disabled", (void *)rxq);
2541 * Clean up a RX queue.
2543 * Destroy objects, free allocated memory and reset the structure for reuse.
2546 * Pointer to RX queue structure.
2549 rxq_cleanup(struct rxq *rxq)
2551 struct ibv_exp_release_intf_params params;
2553 DEBUG("cleaning up %p", (void *)rxq);
2555 rxq_free_elts_sp(rxq);
2558 if (rxq->if_qp != NULL) {
2559 assert(rxq->priv != NULL);
2560 assert(rxq->priv->ctx != NULL);
2561 assert(rxq->qp != NULL);
2562 params = (struct ibv_exp_release_intf_params){
2565 claim_zero(ibv_exp_release_intf(rxq->priv->ctx,
2569 if (rxq->if_cq != NULL) {
2570 assert(rxq->priv != NULL);
2571 assert(rxq->priv->ctx != NULL);
2572 assert(rxq->cq != NULL);
2573 params = (struct ibv_exp_release_intf_params){
2576 claim_zero(ibv_exp_release_intf(rxq->priv->ctx,
2580 if (rxq->qp != NULL && !rxq->priv->isolated) {
2581 rxq_promiscuous_disable(rxq);
2582 rxq_allmulticast_disable(rxq);
2583 rxq_mac_addrs_del(rxq);
2585 if (rxq->qp != NULL)
2586 claim_zero(ibv_destroy_qp(rxq->qp));
2587 if (rxq->cq != NULL)
2588 claim_zero(ibv_destroy_cq(rxq->cq));
2589 if (rxq->channel != NULL)
2590 claim_zero(ibv_destroy_comp_channel(rxq->channel));
2591 if (rxq->rd != NULL) {
2592 struct ibv_exp_destroy_res_domain_attr attr = {
2596 assert(rxq->priv != NULL);
2597 assert(rxq->priv->ctx != NULL);
2598 claim_zero(ibv_exp_destroy_res_domain(rxq->priv->ctx,
2602 if (rxq->mr != NULL)
2603 claim_zero(ibv_dereg_mr(rxq->mr));
2604 memset(rxq, 0, sizeof(*rxq));
2608 * Translate RX completion flags to packet type.
2611 * RX completion flags returned by poll_length_flags().
2613 * @note: fix mlx4_dev_supported_ptypes_get() if any change here.
2616 * Packet type for struct rte_mbuf.
2618 static inline uint32_t
2619 rxq_cq_to_pkt_type(uint32_t flags)
2623 if (flags & IBV_EXP_CQ_RX_TUNNEL_PACKET)
2626 IBV_EXP_CQ_RX_OUTER_IPV4_PACKET,
2627 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN) |
2629 IBV_EXP_CQ_RX_OUTER_IPV6_PACKET,
2630 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN) |
2632 IBV_EXP_CQ_RX_IPV4_PACKET,
2633 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN) |
2635 IBV_EXP_CQ_RX_IPV6_PACKET,
2636 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN);
2640 IBV_EXP_CQ_RX_IPV4_PACKET,
2641 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN) |
2643 IBV_EXP_CQ_RX_IPV6_PACKET,
2644 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN);
2649 * Translate RX completion flags to offload flags.
2652 * Pointer to RX queue structure.
2654 * RX completion flags returned by poll_length_flags().
2657 * Offload flags (ol_flags) for struct rte_mbuf.
2659 static inline uint32_t
2660 rxq_cq_to_ol_flags(const struct rxq *rxq, uint32_t flags)
2662 uint32_t ol_flags = 0;
2667 IBV_EXP_CQ_RX_IP_CSUM_OK,
2668 PKT_RX_IP_CKSUM_GOOD) |
2670 IBV_EXP_CQ_RX_TCP_UDP_CSUM_OK,
2671 PKT_RX_L4_CKSUM_GOOD);
2672 if ((flags & IBV_EXP_CQ_RX_TUNNEL_PACKET) && (rxq->csum_l2tun))
2675 IBV_EXP_CQ_RX_OUTER_IP_CSUM_OK,
2676 PKT_RX_IP_CKSUM_GOOD) |
2678 IBV_EXP_CQ_RX_OUTER_TCP_UDP_CSUM_OK,
2679 PKT_RX_L4_CKSUM_GOOD);
2684 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n);
2687 * DPDK callback for RX with scattered packets support.
2690 * Generic pointer to RX queue structure.
2692 * Array to store received packets.
2694 * Maximum number of packets in array.
2697 * Number of packets successfully received (<= pkts_n).
2700 mlx4_rx_burst_sp(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2702 struct rxq *rxq = (struct rxq *)dpdk_rxq;
2703 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
2704 const unsigned int elts_n = rxq->elts_n;
2705 unsigned int elts_head = rxq->elts_head;
2706 struct ibv_recv_wr head;
2707 struct ibv_recv_wr **next = &head.next;
2708 struct ibv_recv_wr *bad_wr;
2710 unsigned int pkts_ret = 0;
2713 if (unlikely(!rxq->sp))
2714 return mlx4_rx_burst(dpdk_rxq, pkts, pkts_n);
2715 if (unlikely(elts == NULL)) /* See RTE_DEV_CMD_SET_MTU. */
2717 for (i = 0; (i != pkts_n); ++i) {
2718 struct rxq_elt_sp *elt = &(*elts)[elts_head];
2719 struct ibv_recv_wr *wr = &elt->wr;
2720 uint64_t wr_id = wr->wr_id;
2722 unsigned int pkt_buf_len;
2723 struct rte_mbuf *pkt_buf = NULL; /* Buffer returned in pkts. */
2724 struct rte_mbuf **pkt_buf_next = &pkt_buf;
2725 unsigned int seg_headroom = RTE_PKTMBUF_HEADROOM;
2729 /* Sanity checks. */
2733 assert(wr_id < rxq->elts_n);
2734 assert(wr->sg_list == elt->sges);
2735 assert(wr->num_sge == elemof(elt->sges));
2736 assert(elts_head < rxq->elts_n);
2737 assert(rxq->elts_head < rxq->elts_n);
2738 ret = rxq->if_cq->poll_length_flags(rxq->cq, NULL, NULL,
2740 if (unlikely(ret < 0)) {
2744 DEBUG("rxq=%p, poll_length() failed (ret=%d)",
2746 /* ibv_poll_cq() must be used in case of failure. */
2747 wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
2748 if (unlikely(wcs_n == 0))
2750 if (unlikely(wcs_n < 0)) {
2751 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
2752 (void *)rxq, wcs_n);
2756 if (unlikely(wc.status != IBV_WC_SUCCESS)) {
2757 /* Whatever, just repost the offending WR. */
2758 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
2759 " completion status (%d): %s",
2760 (void *)rxq, wc.wr_id, wc.status,
2761 ibv_wc_status_str(wc.status));
2762 /* Increment dropped packets counter. */
2763 ++rxq->stats.idropped;
2764 /* Link completed WRs together for repost. */
2775 /* Link completed WRs together for repost. */
2779 * Replace spent segments with new ones, concatenate and
2780 * return them as pkt_buf.
2783 struct ibv_sge *sge = &elt->sges[j];
2784 struct rte_mbuf *seg = elt->bufs[j];
2785 struct rte_mbuf *rep;
2786 unsigned int seg_tailroom;
2789 * Fetch initial bytes of packet descriptor into a
2790 * cacheline while allocating rep.
2793 rep = rte_mbuf_raw_alloc(rxq->mp);
2794 if (unlikely(rep == NULL)) {
2796 * Unable to allocate a replacement mbuf,
2799 DEBUG("rxq=%p, wr_id=%" PRIu64 ":"
2800 " can't allocate a new mbuf",
2801 (void *)rxq, wr_id);
2802 if (pkt_buf != NULL) {
2803 *pkt_buf_next = NULL;
2804 rte_pktmbuf_free(pkt_buf);
2806 /* Increase out of memory counters. */
2807 ++rxq->stats.rx_nombuf;
2808 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
2812 /* Poison user-modifiable fields in rep. */
2813 NEXT(rep) = (void *)((uintptr_t)-1);
2814 SET_DATA_OFF(rep, 0xdead);
2815 DATA_LEN(rep) = 0xd00d;
2816 PKT_LEN(rep) = 0xdeadd00d;
2817 NB_SEGS(rep) = 0x2a;
2821 * Clear special flags in mbuf to avoid
2822 * crashing while freeing.
2825 ~(uint64_t)(IND_ATTACHED_MBUF |
2828 assert(rep->buf_len == seg->buf_len);
2829 /* Reconfigure sge to use rep instead of seg. */
2830 assert(sge->lkey == rxq->mr->lkey);
2831 sge->addr = ((uintptr_t)rep->buf_addr + seg_headroom);
2834 /* Update pkt_buf if it's the first segment, or link
2835 * seg to the previous one and update pkt_buf_next. */
2836 *pkt_buf_next = seg;
2837 pkt_buf_next = &NEXT(seg);
2838 /* Update seg information. */
2839 seg_tailroom = (seg->buf_len - seg_headroom);
2840 assert(sge->length == seg_tailroom);
2841 SET_DATA_OFF(seg, seg_headroom);
2842 if (likely(len <= seg_tailroom)) {
2844 DATA_LEN(seg) = len;
2847 assert(rte_pktmbuf_headroom(seg) ==
2849 assert(rte_pktmbuf_tailroom(seg) ==
2850 (seg_tailroom - len));
2853 DATA_LEN(seg) = seg_tailroom;
2854 PKT_LEN(seg) = seg_tailroom;
2856 assert(rte_pktmbuf_headroom(seg) == seg_headroom);
2857 assert(rte_pktmbuf_tailroom(seg) == 0);
2858 /* Fix len and clear headroom for next segments. */
2859 len -= seg_tailroom;
2862 /* Update head and tail segments. */
2863 *pkt_buf_next = NULL;
2864 assert(pkt_buf != NULL);
2866 NB_SEGS(pkt_buf) = j;
2867 PORT(pkt_buf) = rxq->port_id;
2868 PKT_LEN(pkt_buf) = pkt_buf_len;
2869 pkt_buf->packet_type = rxq_cq_to_pkt_type(flags);
2870 pkt_buf->ol_flags = rxq_cq_to_ol_flags(rxq, flags);
2872 /* Return packet. */
2873 *(pkts++) = pkt_buf;
2875 /* Increase bytes counter. */
2876 rxq->stats.ibytes += pkt_buf_len;
2878 if (++elts_head >= elts_n)
2882 if (unlikely(i == 0))
2886 ret = ibv_post_recv(rxq->qp, head.next, &bad_wr);
2887 if (unlikely(ret)) {
2888 /* Inability to repost WRs is fatal. */
2889 DEBUG("%p: ibv_post_recv(): failed for WR %p: %s",
2895 rxq->elts_head = elts_head;
2896 /* Increase packets counter. */
2897 rxq->stats.ipackets += pkts_ret;
2902 * DPDK callback for RX.
2904 * The following function is the same as mlx4_rx_burst_sp(), except it doesn't
2905 * manage scattered packets. Improves performance when MRU is lower than the
2906 * size of the first segment.
2909 * Generic pointer to RX queue structure.
2911 * Array to store received packets.
2913 * Maximum number of packets in array.
2916 * Number of packets successfully received (<= pkts_n).
2919 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2921 struct rxq *rxq = (struct rxq *)dpdk_rxq;
2922 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
2923 const unsigned int elts_n = rxq->elts_n;
2924 unsigned int elts_head = rxq->elts_head;
2925 struct ibv_sge sges[pkts_n];
2927 unsigned int pkts_ret = 0;
2930 if (unlikely(rxq->sp))
2931 return mlx4_rx_burst_sp(dpdk_rxq, pkts, pkts_n);
2932 for (i = 0; (i != pkts_n); ++i) {
2933 struct rxq_elt *elt = &(*elts)[elts_head];
2934 struct ibv_recv_wr *wr = &elt->wr;
2935 uint64_t wr_id = wr->wr_id;
2937 struct rte_mbuf *seg = (void *)((uintptr_t)elt->sge.addr -
2938 WR_ID(wr_id).offset);
2939 struct rte_mbuf *rep;
2942 /* Sanity checks. */
2943 assert(WR_ID(wr_id).id < rxq->elts_n);
2944 assert(wr->sg_list == &elt->sge);
2945 assert(wr->num_sge == 1);
2946 assert(elts_head < rxq->elts_n);
2947 assert(rxq->elts_head < rxq->elts_n);
2949 * Fetch initial bytes of packet descriptor into a
2950 * cacheline while allocating rep.
2952 rte_mbuf_prefetch_part1(seg);
2953 rte_mbuf_prefetch_part2(seg);
2954 ret = rxq->if_cq->poll_length_flags(rxq->cq, NULL, NULL,
2956 if (unlikely(ret < 0)) {
2960 DEBUG("rxq=%p, poll_length() failed (ret=%d)",
2962 /* ibv_poll_cq() must be used in case of failure. */
2963 wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
2964 if (unlikely(wcs_n == 0))
2966 if (unlikely(wcs_n < 0)) {
2967 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
2968 (void *)rxq, wcs_n);
2972 if (unlikely(wc.status != IBV_WC_SUCCESS)) {
2973 /* Whatever, just repost the offending WR. */
2974 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
2975 " completion status (%d): %s",
2976 (void *)rxq, wc.wr_id, wc.status,
2977 ibv_wc_status_str(wc.status));
2978 /* Increment dropped packets counter. */
2979 ++rxq->stats.idropped;
2980 /* Add SGE to array for repost. */
2989 rep = rte_mbuf_raw_alloc(rxq->mp);
2990 if (unlikely(rep == NULL)) {
2992 * Unable to allocate a replacement mbuf,
2995 DEBUG("rxq=%p, wr_id=%" PRIu32 ":"
2996 " can't allocate a new mbuf",
2997 (void *)rxq, WR_ID(wr_id).id);
2998 /* Increase out of memory counters. */
2999 ++rxq->stats.rx_nombuf;
3000 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
3001 /* Add SGE to array for repost. */
3006 /* Reconfigure sge to use rep instead of seg. */
3007 elt->sge.addr = (uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM;
3008 assert(elt->sge.lkey == rxq->mr->lkey);
3009 WR_ID(wr->wr_id).offset =
3010 (((uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM) -
3012 assert(WR_ID(wr->wr_id).id == WR_ID(wr_id).id);
3014 /* Add SGE to array for repost. */
3017 /* Update seg information. */
3018 SET_DATA_OFF(seg, RTE_PKTMBUF_HEADROOM);
3020 PORT(seg) = rxq->port_id;
3023 DATA_LEN(seg) = len;
3024 seg->packet_type = rxq_cq_to_pkt_type(flags);
3025 seg->ol_flags = rxq_cq_to_ol_flags(rxq, flags);
3027 /* Return packet. */
3030 /* Increase bytes counter. */
3031 rxq->stats.ibytes += len;
3033 if (++elts_head >= elts_n)
3037 if (unlikely(i == 0))
3040 ret = rxq->if_qp->recv_burst(rxq->qp, sges, i);
3041 if (unlikely(ret)) {
3042 /* Inability to repost WRs is fatal. */
3043 DEBUG("%p: recv_burst(): failed (ret=%d)",
3048 rxq->elts_head = elts_head;
3049 /* Increase packets counter. */
3050 rxq->stats.ipackets += pkts_ret;
3055 * Allocate a Queue Pair.
3056 * Optionally setup inline receive if supported.
3059 * Pointer to private structure.
3061 * Completion queue to associate with QP.
3063 * Number of descriptors in QP (hint only).
3066 * QP pointer or NULL in case of error.
3068 static struct ibv_qp *
3069 rxq_setup_qp(struct priv *priv, struct ibv_cq *cq, uint16_t desc,
3070 struct ibv_exp_res_domain *rd)
3072 struct ibv_exp_qp_init_attr attr = {
3073 /* CQ to be associated with the send queue. */
3075 /* CQ to be associated with the receive queue. */
3078 /* Max number of outstanding WRs. */
3079 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
3080 priv->device_attr.max_qp_wr :
3082 /* Max number of scatter/gather elements in a WR. */
3083 .max_recv_sge = ((priv->device_attr.max_sge <
3084 MLX4_PMD_SGE_WR_N) ?
3085 priv->device_attr.max_sge :
3088 .qp_type = IBV_QPT_RAW_PACKET,
3089 .comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
3090 IBV_EXP_QP_INIT_ATTR_RES_DOMAIN),
3095 attr.max_inl_recv = priv->inl_recv_size;
3096 attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
3097 return ibv_exp_create_qp(priv->ctx, &attr);
3101 * Allocate a RSS Queue Pair.
3102 * Optionally setup inline receive if supported.
3105 * Pointer to private structure.
3107 * Completion queue to associate with QP.
3109 * Number of descriptors in QP (hint only).
3111 * If nonzero, a number of children for parent QP and zero for a child.
3113 * Pointer for a parent in a child case, NULL otherwise.
3116 * QP pointer or NULL in case of error.
3118 static struct ibv_qp *
3119 rxq_setup_qp_rss(struct priv *priv, struct ibv_cq *cq, uint16_t desc,
3120 int children_n, struct ibv_exp_res_domain *rd,
3121 struct rxq *rxq_parent)
3123 struct ibv_exp_qp_init_attr attr = {
3124 /* CQ to be associated with the send queue. */
3126 /* CQ to be associated with the receive queue. */
3129 /* Max number of outstanding WRs. */
3130 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
3131 priv->device_attr.max_qp_wr :
3133 /* Max number of scatter/gather elements in a WR. */
3134 .max_recv_sge = ((priv->device_attr.max_sge <
3135 MLX4_PMD_SGE_WR_N) ?
3136 priv->device_attr.max_sge :
3139 .qp_type = IBV_QPT_RAW_PACKET,
3140 .comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
3141 IBV_EXP_QP_INIT_ATTR_RES_DOMAIN |
3142 IBV_EXP_QP_INIT_ATTR_QPG),
3147 attr.max_inl_recv = priv->inl_recv_size,
3148 attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
3149 if (children_n > 0) {
3150 attr.qpg.qpg_type = IBV_EXP_QPG_PARENT;
3151 /* TSS isn't necessary. */
3152 attr.qpg.parent_attrib.tss_child_count = 0;
3153 attr.qpg.parent_attrib.rss_child_count =
3154 rte_align32pow2(children_n + 1) >> 1;
3155 DEBUG("initializing parent RSS queue");
3157 attr.qpg.qpg_type = IBV_EXP_QPG_CHILD_RX;
3158 attr.qpg.qpg_parent = rxq_parent->qp;
3159 DEBUG("initializing child RSS queue");
3161 return ibv_exp_create_qp(priv->ctx, &attr);
3165 * Reconfigure a RX queue with new parameters.
3167 * rxq_rehash() does not allocate mbufs, which, if not done from the right
3168 * thread (such as a control thread), may corrupt the pool.
3169 * In case of failure, the queue is left untouched.
3172 * Pointer to Ethernet device structure.
3177 * 0 on success, errno value on failure.
3180 rxq_rehash(struct rte_eth_dev *dev, struct rxq *rxq)
3182 struct priv *priv = rxq->priv;
3183 struct rxq tmpl = *rxq;
3184 unsigned int mbuf_n;
3185 unsigned int desc_n;
3186 struct rte_mbuf **pool;
3188 struct ibv_exp_qp_attr mod;
3189 struct ibv_recv_wr *bad_wr;
3190 unsigned int mb_len;
3193 mb_len = rte_pktmbuf_data_room_size(rxq->mp);
3194 DEBUG("%p: rehashing queue %p", (void *)dev, (void *)rxq);
3195 /* Number of descriptors and mbufs currently allocated. */
3196 desc_n = (tmpl.elts_n * (tmpl.sp ? MLX4_PMD_SGE_WR_N : 1));
3198 /* Toggle RX checksum offload if hardware supports it. */
3199 if (priv->hw_csum) {
3200 tmpl.csum = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3201 rxq->csum = tmpl.csum;
3203 if (priv->hw_csum_l2tun) {
3204 tmpl.csum_l2tun = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3205 rxq->csum_l2tun = tmpl.csum_l2tun;
3207 /* Enable scattered packets support for this queue if necessary. */
3208 assert(mb_len >= RTE_PKTMBUF_HEADROOM);
3209 if (dev->data->dev_conf.rxmode.enable_scatter &&
3210 (dev->data->dev_conf.rxmode.max_rx_pkt_len >
3211 (mb_len - RTE_PKTMBUF_HEADROOM))) {
3213 desc_n /= MLX4_PMD_SGE_WR_N;
3216 DEBUG("%p: %s scattered packets support (%u WRs)",
3217 (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc_n);
3218 /* If scatter mode is the same as before, nothing to do. */
3219 if (tmpl.sp == rxq->sp) {
3220 DEBUG("%p: nothing to do", (void *)dev);
3223 /* Remove attached flows if RSS is disabled (no parent queue). */
3224 if (!priv->rss && !priv->isolated) {
3225 rxq_allmulticast_disable(&tmpl);
3226 rxq_promiscuous_disable(&tmpl);
3227 rxq_mac_addrs_del(&tmpl);
3228 /* Update original queue in case of failure. */
3229 rxq->allmulti_flow = tmpl.allmulti_flow;
3230 rxq->promisc_flow = tmpl.promisc_flow;
3231 memcpy(rxq->mac_configured, tmpl.mac_configured,
3232 sizeof(rxq->mac_configured));
3233 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
3235 /* From now on, any failure will render the queue unusable.
3236 * Reinitialize QP. */
3239 mod = (struct ibv_exp_qp_attr){ .qp_state = IBV_QPS_RESET };
3240 err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3242 ERROR("%p: cannot reset QP: %s", (void *)dev, strerror(err));
3246 mod = (struct ibv_exp_qp_attr){
3247 /* Move the QP to this state. */
3248 .qp_state = IBV_QPS_INIT,
3249 /* Primary port number. */
3250 .port_num = priv->port
3252 err = ibv_exp_modify_qp(tmpl.qp, &mod,
3256 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
3257 (void *)dev, strerror(err));
3262 err = ibv_resize_cq(tmpl.cq, desc_n);
3264 ERROR("%p: cannot resize CQ: %s", (void *)dev, strerror(err));
3268 /* Reconfigure flows. Do not care for errors. */
3269 if (!priv->rss && !priv->isolated) {
3270 rxq_mac_addrs_add(&tmpl);
3272 rxq_promiscuous_enable(&tmpl);
3274 rxq_allmulticast_enable(&tmpl);
3275 /* Update original queue in case of failure. */
3276 rxq->allmulti_flow = tmpl.allmulti_flow;
3277 rxq->promisc_flow = tmpl.promisc_flow;
3278 memcpy(rxq->mac_configured, tmpl.mac_configured,
3279 sizeof(rxq->mac_configured));
3280 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
3282 /* Allocate pool. */
3283 pool = rte_malloc(__func__, (mbuf_n * sizeof(*pool)), 0);
3285 ERROR("%p: cannot allocate memory", (void *)dev);
3288 /* Snatch mbufs from original queue. */
3291 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
3293 for (i = 0; (i != elemof(*elts)); ++i) {
3294 struct rxq_elt_sp *elt = &(*elts)[i];
3297 for (j = 0; (j != elemof(elt->bufs)); ++j) {
3298 assert(elt->bufs[j] != NULL);
3299 pool[k++] = elt->bufs[j];
3303 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
3305 for (i = 0; (i != elemof(*elts)); ++i) {
3306 struct rxq_elt *elt = &(*elts)[i];
3307 struct rte_mbuf *buf = (void *)
3308 ((uintptr_t)elt->sge.addr -
3309 WR_ID(elt->wr.wr_id).offset);
3311 assert(WR_ID(elt->wr.wr_id).id == i);
3315 assert(k == mbuf_n);
3317 tmpl.elts.sp = NULL;
3318 assert((void *)&tmpl.elts.sp == (void *)&tmpl.elts.no_sp);
3320 rxq_alloc_elts_sp(&tmpl, desc_n, pool) :
3321 rxq_alloc_elts(&tmpl, desc_n, pool));
3323 ERROR("%p: cannot reallocate WRs, aborting", (void *)dev);
3328 assert(tmpl.elts_n == desc_n);
3329 assert(tmpl.elts.sp != NULL);
3331 /* Clean up original data. */
3333 rte_free(rxq->elts.sp);
3334 rxq->elts.sp = NULL;
3338 err = ibv_post_recv(tmpl.qp,
3340 &(*tmpl.elts.sp)[0].wr :
3341 &(*tmpl.elts.no_sp)[0].wr),
3344 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
3350 mod = (struct ibv_exp_qp_attr){
3351 .qp_state = IBV_QPS_RTR
3353 err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3355 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
3356 (void *)dev, strerror(err));
3364 * Create verbs QP resources associated with a rxq.
3367 * Pointer to RX queue structure.
3369 * Number of descriptors to configure in queue.
3371 * If true, the queue is disabled because its index is higher or
3372 * equal to the real number of queues, which must be a power of 2.
3374 * The number of children in a parent case, zero for a child.
3376 * The pointer to a parent RX structure for a child in RSS case,
3380 * 0 on success, errno value on failure.
3383 rxq_create_qp(struct rxq *rxq,
3387 struct rxq *rxq_parent)
3390 struct ibv_exp_qp_attr mod;
3391 struct ibv_exp_query_intf_params params;
3392 enum ibv_exp_query_intf_status status;
3393 struct ibv_recv_wr *bad_wr;
3394 int parent = (children_n > 0);
3395 struct priv *priv = rxq->priv;
3397 if (priv->rss && !inactive && (rxq_parent || parent))
3398 rxq->qp = rxq_setup_qp_rss(priv, rxq->cq, desc,
3399 children_n, rxq->rd,
3402 rxq->qp = rxq_setup_qp(priv, rxq->cq, desc, rxq->rd);
3403 if (rxq->qp == NULL) {
3404 ret = (errno ? errno : EINVAL);
3405 ERROR("QP creation failure: %s",
3409 mod = (struct ibv_exp_qp_attr){
3410 /* Move the QP to this state. */
3411 .qp_state = IBV_QPS_INIT,
3412 /* Primary port number. */
3413 .port_num = priv->port
3415 ret = ibv_exp_modify_qp(rxq->qp, &mod,
3417 (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
3420 ERROR("QP state to IBV_QPS_INIT failed: %s",
3424 if (!priv->isolated && (parent || !priv->rss)) {
3425 /* Configure MAC and broadcast addresses. */
3426 ret = rxq_mac_addrs_add(rxq);
3428 ERROR("QP flow attachment failed: %s",
3434 ret = ibv_post_recv(rxq->qp,
3436 &(*rxq->elts.sp)[0].wr :
3437 &(*rxq->elts.no_sp)[0].wr),
3440 ERROR("ibv_post_recv() failed for WR %p: %s",
3446 mod = (struct ibv_exp_qp_attr){
3447 .qp_state = IBV_QPS_RTR
3449 ret = ibv_exp_modify_qp(rxq->qp, &mod, IBV_EXP_QP_STATE);
3451 ERROR("QP state to IBV_QPS_RTR failed: %s",
3455 params = (struct ibv_exp_query_intf_params){
3456 .intf_scope = IBV_EXP_INTF_GLOBAL,
3457 .intf = IBV_EXP_INTF_QP_BURST,
3460 rxq->if_qp = ibv_exp_query_intf(priv->ctx, ¶ms, &status);
3461 if (rxq->if_qp == NULL) {
3462 ERROR("QP interface family query failed with status %d",
3470 * Configure a RX queue.
3473 * Pointer to Ethernet device structure.
3475 * Pointer to RX queue structure.
3477 * Number of descriptors to configure in queue.
3479 * NUMA socket on which memory must be allocated.
3481 * If true, the queue is disabled because its index is higher or
3482 * equal to the real number of queues, which must be a power of 2.
3484 * Thresholds parameters.
3486 * Memory pool for buffer allocations.
3488 * The number of children in a parent case, zero for a child.
3490 * The pointer to a parent RX structure (or NULL) in a child case,
3494 * 0 on success, errno value on failure.
3497 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
3498 unsigned int socket, int inactive,
3499 const struct rte_eth_rxconf *conf,
3500 struct rte_mempool *mp, int children_n,
3501 struct rxq *rxq_parent)
3503 struct priv *priv = dev->data->dev_private;
3510 struct ibv_exp_query_intf_params params;
3511 struct ibv_exp_cq_init_attr cq;
3512 struct ibv_exp_res_domain_init_attr rd;
3514 enum ibv_exp_query_intf_status status;
3515 unsigned int mb_len;
3517 int parent = (children_n > 0);
3519 (void)conf; /* Thresholds configuration (ignored). */
3521 * If this is a parent queue, hardware must support RSS and
3522 * RSS must be enabled.
3524 assert((!parent) || ((priv->hw_rss) && (priv->rss)));
3526 /* Even if unused, ibv_create_cq() requires at least one
3531 mb_len = rte_pktmbuf_data_room_size(mp);
3532 if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
3533 ERROR("%p: invalid number of RX descriptors (must be a"
3534 " multiple of %d)", (void *)dev, MLX4_PMD_SGE_WR_N);
3537 /* Toggle RX checksum offload if hardware supports it. */
3539 tmpl.csum = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3540 if (priv->hw_csum_l2tun)
3541 tmpl.csum_l2tun = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3542 /* Enable scattered packets support for this queue if necessary. */
3543 assert(mb_len >= RTE_PKTMBUF_HEADROOM);
3544 if (dev->data->dev_conf.rxmode.max_rx_pkt_len <=
3545 (mb_len - RTE_PKTMBUF_HEADROOM)) {
3547 } else if (dev->data->dev_conf.rxmode.enable_scatter) {
3549 desc /= MLX4_PMD_SGE_WR_N;
3551 WARN("%p: the requested maximum Rx packet size (%u) is"
3552 " larger than a single mbuf (%u) and scattered"
3553 " mode has not been requested",
3555 dev->data->dev_conf.rxmode.max_rx_pkt_len,
3556 mb_len - RTE_PKTMBUF_HEADROOM);
3558 DEBUG("%p: %s scattered packets support (%u WRs)",
3559 (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc);
3560 /* Use the entire RX mempool as the memory region. */
3561 tmpl.mr = mlx4_mp2mr(priv->pd, mp);
3562 if (tmpl.mr == NULL) {
3564 ERROR("%p: MR creation failure: %s",
3565 (void *)dev, strerror(ret));
3569 attr.rd = (struct ibv_exp_res_domain_init_attr){
3570 .comp_mask = (IBV_EXP_RES_DOMAIN_THREAD_MODEL |
3571 IBV_EXP_RES_DOMAIN_MSG_MODEL),
3572 .thread_model = IBV_EXP_THREAD_SINGLE,
3573 .msg_model = IBV_EXP_MSG_HIGH_BW,
3575 tmpl.rd = ibv_exp_create_res_domain(priv->ctx, &attr.rd);
3576 if (tmpl.rd == NULL) {
3578 ERROR("%p: RD creation failure: %s",
3579 (void *)dev, strerror(ret));
3582 if (dev->data->dev_conf.intr_conf.rxq) {
3583 tmpl.channel = ibv_create_comp_channel(priv->ctx);
3584 if (tmpl.channel == NULL) {
3586 ERROR("%p: Rx interrupt completion channel creation"
3588 (void *)dev, strerror(ret));
3592 attr.cq = (struct ibv_exp_cq_init_attr){
3593 .comp_mask = IBV_EXP_CQ_INIT_ATTR_RES_DOMAIN,
3594 .res_domain = tmpl.rd,
3596 tmpl.cq = ibv_exp_create_cq(priv->ctx, desc, NULL, tmpl.channel, 0,
3598 if (tmpl.cq == NULL) {
3600 ERROR("%p: CQ creation failure: %s",
3601 (void *)dev, strerror(ret));
3604 DEBUG("priv->device_attr.max_qp_wr is %d",
3605 priv->device_attr.max_qp_wr);
3606 DEBUG("priv->device_attr.max_sge is %d",
3607 priv->device_attr.max_sge);
3608 /* Allocate descriptors for RX queues, except for the RSS parent. */
3612 ret = rxq_alloc_elts_sp(&tmpl, desc, NULL);
3614 ret = rxq_alloc_elts(&tmpl, desc, NULL);
3616 ERROR("%p: RXQ allocation failed: %s",
3617 (void *)dev, strerror(ret));
3621 if (parent || rxq_parent || !priv->rss) {
3622 ret = rxq_create_qp(&tmpl, desc, inactive,
3623 children_n, rxq_parent);
3628 tmpl.port_id = dev->data->port_id;
3629 DEBUG("%p: RTE port ID: %u", (void *)rxq, tmpl.port_id);
3630 attr.params = (struct ibv_exp_query_intf_params){
3631 .intf_scope = IBV_EXP_INTF_GLOBAL,
3632 .intf = IBV_EXP_INTF_CQ,
3635 tmpl.if_cq = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
3636 if (tmpl.if_cq == NULL) {
3638 ERROR("%p: CQ interface family query failed with status %d",
3639 (void *)dev, status);
3642 /* Clean up rxq in case we're reinitializing it. */
3643 DEBUG("%p: cleaning-up old rxq just in case", (void *)rxq);
3646 DEBUG("%p: rxq updated with %p", (void *)rxq, (void *)&tmpl);
3656 * DPDK callback to configure a RX queue.
3659 * Pointer to Ethernet device structure.
3663 * Number of descriptors to configure in queue.
3665 * NUMA socket on which memory must be allocated.
3667 * Thresholds parameters.
3669 * Memory pool for buffer allocations.
3672 * 0 on success, negative errno value on failure.
3675 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
3676 unsigned int socket, const struct rte_eth_rxconf *conf,
3677 struct rte_mempool *mp)
3680 struct priv *priv = dev->data->dev_private;
3681 struct rxq *rxq = (*priv->rxqs)[idx];
3686 DEBUG("%p: configuring queue %u for %u descriptors",
3687 (void *)dev, idx, desc);
3688 if (idx >= priv->rxqs_n) {
3689 ERROR("%p: queue index out of range (%u >= %u)",
3690 (void *)dev, idx, priv->rxqs_n);
3695 DEBUG("%p: reusing already allocated queue index %u (%p)",
3696 (void *)dev, idx, (void *)rxq);
3697 if (priv->started) {
3701 (*priv->rxqs)[idx] = NULL;
3704 rxq = rte_calloc_socket("RXQ", 1, sizeof(*rxq), 0, socket);
3706 ERROR("%p: unable to allocate queue index %u",
3712 if (priv->rss && !priv->isolated) {
3713 /* The list consists of the single default one. */
3714 parent = LIST_FIRST(&priv->parents);
3715 if (idx >= rte_align32pow2(priv->rxqs_n + 1) >> 1)
3720 ret = rxq_setup(dev, rxq, desc, socket,
3721 inactive, conf, mp, 0, parent);
3725 rxq->stats.idx = idx;
3726 DEBUG("%p: adding RX queue %p to list",
3727 (void *)dev, (void *)rxq);
3728 (*priv->rxqs)[idx] = rxq;
3729 /* Update receive callback. */
3731 dev->rx_pkt_burst = mlx4_rx_burst_sp;
3733 dev->rx_pkt_burst = mlx4_rx_burst;
3740 * DPDK callback to release a RX queue.
3743 * Generic RX queue pointer.
3746 mlx4_rx_queue_release(void *dpdk_rxq)
3748 struct rxq *rxq = (struct rxq *)dpdk_rxq;
3756 for (i = 0; (i != priv->rxqs_n); ++i)
3757 if ((*priv->rxqs)[i] == rxq) {
3758 DEBUG("%p: removing RX queue %p from list",
3759 (void *)priv->dev, (void *)rxq);
3760 (*priv->rxqs)[i] = NULL;
3769 priv_dev_interrupt_handler_install(struct priv *, struct rte_eth_dev *);
3772 priv_dev_removal_interrupt_handler_install(struct priv *, struct rte_eth_dev *);
3775 priv_dev_link_interrupt_handler_install(struct priv *, struct rte_eth_dev *);
3778 * DPDK callback to start the device.
3780 * Simulate device start by attaching all configured flows.
3783 * Pointer to Ethernet device structure.
3786 * 0 on success, negative errno value on failure.
3789 mlx4_dev_start(struct rte_eth_dev *dev)
3791 struct priv *priv = dev->data->dev_private;
3798 if (priv->started) {
3802 DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
3804 if (priv->isolated) {
3807 } else if (priv->rss) {
3808 rxq = LIST_FIRST(&priv->parents);
3811 rxq = (*priv->rxqs)[0];
3814 /* Iterate only once when RSS is enabled. */
3816 /* Ignore nonexistent RX queues. */
3819 ret = rxq_mac_addrs_add(rxq);
3820 if (!ret && priv->promisc)
3821 ret = rxq_promiscuous_enable(rxq);
3822 if (!ret && priv->allmulti)
3823 ret = rxq_allmulticast_enable(rxq);
3826 WARN("%p: QP flow attachment failed: %s",
3827 (void *)dev, strerror(ret));
3829 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
3830 ret = priv_dev_link_interrupt_handler_install(priv, dev);
3832 ERROR("%p: LSC handler install failed",
3836 ret = priv_dev_removal_interrupt_handler_install(priv, dev);
3838 ERROR("%p: RMV handler install failed",
3842 ret = priv_rx_intr_vec_enable(priv);
3844 ERROR("%p: Rx interrupt vector creation failed",
3848 ret = mlx4_priv_flow_start(priv);
3850 ERROR("%p: flow start failed: %s",
3851 (void *)dev, strerror(ret));
3859 rxq = (*priv->rxqs)[i--];
3861 rxq_allmulticast_disable(rxq);
3862 rxq_promiscuous_disable(rxq);
3863 rxq_mac_addrs_del(rxq);
3872 * DPDK callback to stop the device.
3874 * Simulate device stop by detaching all configured flows.
3877 * Pointer to Ethernet device structure.
3880 mlx4_dev_stop(struct rte_eth_dev *dev)
3882 struct priv *priv = dev->data->dev_private;
3888 if (!priv->started) {
3892 DEBUG("%p: detaching flows from all RX queues", (void *)dev);
3894 if (priv->isolated) {
3897 } else if (priv->rss) {
3898 rxq = LIST_FIRST(&priv->parents);
3901 rxq = (*priv->rxqs)[0];
3904 mlx4_priv_flow_stop(priv);
3905 /* Iterate only once when RSS is enabled. */
3907 /* Ignore nonexistent RX queues. */
3910 rxq_allmulticast_disable(rxq);
3911 rxq_promiscuous_disable(rxq);
3912 rxq_mac_addrs_del(rxq);
3913 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
3918 * Dummy DPDK callback for TX.
3920 * This function is used to temporarily replace the real callback during
3921 * unsafe control operations on the queue, or in case of error.
3924 * Generic pointer to TX queue structure.
3926 * Packets to transmit.
3928 * Number of packets in array.
3931 * Number of packets successfully transmitted (<= pkts_n).
3934 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
3943 * Dummy DPDK callback for RX.
3945 * This function is used to temporarily replace the real callback during
3946 * unsafe control operations on the queue, or in case of error.
3949 * Generic pointer to RX queue structure.
3951 * Array to store received packets.
3953 * Maximum number of packets in array.
3956 * Number of packets successfully received (<= pkts_n).
3959 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
3968 priv_dev_interrupt_handler_uninstall(struct priv *, struct rte_eth_dev *);
3971 priv_dev_removal_interrupt_handler_uninstall(struct priv *,
3972 struct rte_eth_dev *);
3975 priv_dev_link_interrupt_handler_uninstall(struct priv *, struct rte_eth_dev *);
3978 * DPDK callback to close the device.
3980 * Destroy all queues and objects, free memory.
3983 * Pointer to Ethernet device structure.
3986 mlx4_dev_close(struct rte_eth_dev *dev)
3988 struct priv *priv = dev->data->dev_private;
3995 DEBUG("%p: closing device \"%s\"",
3997 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
3998 /* Prevent crashes when queues are still in use. This is unfortunately
3999 * still required for DPDK 1.3 because some programs (such as testpmd)
4000 * never release them before closing the device. */
4001 dev->rx_pkt_burst = removed_rx_burst;
4002 dev->tx_pkt_burst = removed_tx_burst;
4003 if (priv->rxqs != NULL) {
4004 /* XXX race condition if mlx4_rx_burst() is still running. */
4006 for (i = 0; (i != priv->rxqs_n); ++i) {
4007 tmp = (*priv->rxqs)[i];
4010 (*priv->rxqs)[i] = NULL;
4017 if (priv->txqs != NULL) {
4018 /* XXX race condition if mlx4_tx_burst() is still running. */
4020 for (i = 0; (i != priv->txqs_n); ++i) {
4021 tmp = (*priv->txqs)[i];
4024 (*priv->txqs)[i] = NULL;
4032 priv_parent_list_cleanup(priv);
4033 if (priv->pd != NULL) {
4034 assert(priv->ctx != NULL);
4035 claim_zero(ibv_dealloc_pd(priv->pd));
4036 claim_zero(ibv_close_device(priv->ctx));
4038 assert(priv->ctx == NULL);
4039 priv_dev_removal_interrupt_handler_uninstall(priv, dev);
4040 priv_dev_link_interrupt_handler_uninstall(priv, dev);
4041 priv_rx_intr_vec_disable(priv);
4043 memset(priv, 0, sizeof(*priv));
4047 * Change the link state (UP / DOWN).
4050 * Pointer to Ethernet device private data.
4052 * Nonzero for link up, otherwise link down.
4055 * 0 on success, errno value on failure.
4058 priv_set_link(struct priv *priv, int up)
4060 struct rte_eth_dev *dev = priv->dev;
4065 err = priv_set_flags(priv, ~IFF_UP, IFF_UP);
4068 for (i = 0; i < priv->rxqs_n; i++)
4069 if ((*priv->rxqs)[i]->sp)
4071 /* Check if an sp queue exists.
4072 * Note: Some old frames might be received.
4074 if (i == priv->rxqs_n)
4075 dev->rx_pkt_burst = mlx4_rx_burst;
4077 dev->rx_pkt_burst = mlx4_rx_burst_sp;
4078 dev->tx_pkt_burst = mlx4_tx_burst;
4080 err = priv_set_flags(priv, ~IFF_UP, ~IFF_UP);
4083 dev->rx_pkt_burst = removed_rx_burst;
4084 dev->tx_pkt_burst = removed_tx_burst;
4090 * DPDK callback to bring the link DOWN.
4093 * Pointer to Ethernet device structure.
4096 * 0 on success, errno value on failure.
4099 mlx4_set_link_down(struct rte_eth_dev *dev)
4101 struct priv *priv = dev->data->dev_private;
4105 err = priv_set_link(priv, 0);
4111 * DPDK callback to bring the link UP.
4114 * Pointer to Ethernet device structure.
4117 * 0 on success, errno value on failure.
4120 mlx4_set_link_up(struct rte_eth_dev *dev)
4122 struct priv *priv = dev->data->dev_private;
4126 err = priv_set_link(priv, 1);
4131 * DPDK callback to get information about the device.
4134 * Pointer to Ethernet device structure.
4136 * Info structure output buffer.
4139 mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
4141 struct priv *priv = dev->data->dev_private;
4143 char ifname[IF_NAMESIZE];
4145 info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
4150 /* FIXME: we should ask the device for these values. */
4151 info->min_rx_bufsize = 32;
4152 info->max_rx_pktlen = 65536;
4154 * Since we need one CQ per QP, the limit is the minimum number
4155 * between the two values.
4157 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
4158 priv->device_attr.max_qp : priv->device_attr.max_cq);
4159 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
4162 info->max_rx_queues = max;
4163 info->max_tx_queues = max;
4164 /* Last array entry is reserved for broadcast. */
4165 info->max_mac_addrs = (elemof(priv->mac) - 1);
4166 info->rx_offload_capa =
4168 (DEV_RX_OFFLOAD_IPV4_CKSUM |
4169 DEV_RX_OFFLOAD_UDP_CKSUM |
4170 DEV_RX_OFFLOAD_TCP_CKSUM) :
4172 info->tx_offload_capa =
4174 (DEV_TX_OFFLOAD_IPV4_CKSUM |
4175 DEV_TX_OFFLOAD_UDP_CKSUM |
4176 DEV_TX_OFFLOAD_TCP_CKSUM) :
4178 if (priv_get_ifname(priv, &ifname) == 0)
4179 info->if_index = if_nametoindex(ifname);
4182 ETH_LINK_SPEED_10G |
4183 ETH_LINK_SPEED_20G |
4184 ETH_LINK_SPEED_40G |
4189 static const uint32_t *
4190 mlx4_dev_supported_ptypes_get(struct rte_eth_dev *dev)
4192 static const uint32_t ptypes[] = {
4193 /* refers to rxq_cq_to_pkt_type() */
4196 RTE_PTYPE_INNER_L3_IPV4,
4197 RTE_PTYPE_INNER_L3_IPV6,
4201 if (dev->rx_pkt_burst == mlx4_rx_burst ||
4202 dev->rx_pkt_burst == mlx4_rx_burst_sp)
4208 * DPDK callback to get device statistics.
4211 * Pointer to Ethernet device structure.
4213 * Stats structure output buffer.
4216 mlx4_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
4218 struct priv *priv = dev->data->dev_private;
4219 struct rte_eth_stats tmp = {0};
4226 /* Add software counters. */
4227 for (i = 0; (i != priv->rxqs_n); ++i) {
4228 struct rxq *rxq = (*priv->rxqs)[i];
4232 idx = rxq->stats.idx;
4233 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
4234 tmp.q_ipackets[idx] += rxq->stats.ipackets;
4235 tmp.q_ibytes[idx] += rxq->stats.ibytes;
4236 tmp.q_errors[idx] += (rxq->stats.idropped +
4237 rxq->stats.rx_nombuf);
4239 tmp.ipackets += rxq->stats.ipackets;
4240 tmp.ibytes += rxq->stats.ibytes;
4241 tmp.ierrors += rxq->stats.idropped;
4242 tmp.rx_nombuf += rxq->stats.rx_nombuf;
4244 for (i = 0; (i != priv->txqs_n); ++i) {
4245 struct txq *txq = (*priv->txqs)[i];
4249 idx = txq->stats.idx;
4250 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
4251 tmp.q_opackets[idx] += txq->stats.opackets;
4252 tmp.q_obytes[idx] += txq->stats.obytes;
4253 tmp.q_errors[idx] += txq->stats.odropped;
4255 tmp.opackets += txq->stats.opackets;
4256 tmp.obytes += txq->stats.obytes;
4257 tmp.oerrors += txq->stats.odropped;
4264 * DPDK callback to clear device statistics.
4267 * Pointer to Ethernet device structure.
4270 mlx4_stats_reset(struct rte_eth_dev *dev)
4272 struct priv *priv = dev->data->dev_private;
4279 for (i = 0; (i != priv->rxqs_n); ++i) {
4280 if ((*priv->rxqs)[i] == NULL)
4282 idx = (*priv->rxqs)[i]->stats.idx;
4283 (*priv->rxqs)[i]->stats =
4284 (struct mlx4_rxq_stats){ .idx = idx };
4286 for (i = 0; (i != priv->txqs_n); ++i) {
4287 if ((*priv->txqs)[i] == NULL)
4289 idx = (*priv->txqs)[i]->stats.idx;
4290 (*priv->txqs)[i]->stats =
4291 (struct mlx4_txq_stats){ .idx = idx };
4297 * DPDK callback to remove a MAC address.
4300 * Pointer to Ethernet device structure.
4302 * MAC address index.
4305 mlx4_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
4307 struct priv *priv = dev->data->dev_private;
4312 DEBUG("%p: removing MAC address from index %" PRIu32,
4313 (void *)dev, index);
4314 /* Last array entry is reserved for broadcast. */
4315 if (index >= (elemof(priv->mac) - 1))
4317 priv_mac_addr_del(priv, index);
4323 * DPDK callback to add a MAC address.
4326 * Pointer to Ethernet device structure.
4328 * MAC address to register.
4330 * MAC address index.
4332 * VMDq pool index to associate address with (ignored).
4335 mlx4_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
4336 uint32_t index, uint32_t vmdq)
4338 struct priv *priv = dev->data->dev_private;
4343 if (priv->isolated) {
4344 DEBUG("%p: cannot add MAC address, "
4345 "device is in isolated mode", (void *)dev);
4349 DEBUG("%p: adding MAC address at index %" PRIu32,
4350 (void *)dev, index);
4351 /* Last array entry is reserved for broadcast. */
4352 if (index >= (elemof(priv->mac) - 1)) {
4356 re = priv_mac_addr_add(priv, index,
4357 (const uint8_t (*)[ETHER_ADDR_LEN])
4358 mac_addr->addr_bytes);
4365 * DPDK callback to set the primary MAC address.
4368 * Pointer to Ethernet device structure.
4370 * MAC address to register.
4373 mlx4_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
4375 DEBUG("%p: setting primary MAC address", (void *)dev);
4376 mlx4_mac_addr_remove(dev, 0);
4377 mlx4_mac_addr_add(dev, mac_addr, 0, 0);
4381 * DPDK callback to enable promiscuous mode.
4384 * Pointer to Ethernet device structure.
4387 mlx4_promiscuous_enable(struct rte_eth_dev *dev)
4389 struct priv *priv = dev->data->dev_private;
4394 if (priv->isolated) {
4395 DEBUG("%p: cannot enable promiscuous, "
4396 "device is in isolated mode", (void *)dev);
4400 if (priv->promisc) {
4404 /* If device isn't started, this is all we need to do. */
4408 ret = rxq_promiscuous_enable(LIST_FIRST(&priv->parents));
4415 for (i = 0; (i != priv->rxqs_n); ++i) {
4416 if ((*priv->rxqs)[i] == NULL)
4418 ret = rxq_promiscuous_enable((*priv->rxqs)[i]);
4421 /* Failure, rollback. */
4423 if ((*priv->rxqs)[--i] != NULL)
4424 rxq_promiscuous_disable((*priv->rxqs)[i]);
4434 * DPDK callback to disable promiscuous mode.
4437 * Pointer to Ethernet device structure.
4440 mlx4_promiscuous_disable(struct rte_eth_dev *dev)
4442 struct priv *priv = dev->data->dev_private;
4446 if (!priv->promisc || priv->isolated) {
4451 rxq_promiscuous_disable(LIST_FIRST(&priv->parents));
4454 for (i = 0; (i != priv->rxqs_n); ++i)
4455 if ((*priv->rxqs)[i] != NULL)
4456 rxq_promiscuous_disable((*priv->rxqs)[i]);
4463 * DPDK callback to enable allmulti mode.
4466 * Pointer to Ethernet device structure.
4469 mlx4_allmulticast_enable(struct rte_eth_dev *dev)
4471 struct priv *priv = dev->data->dev_private;
4476 if (priv->isolated) {
4477 DEBUG("%p: cannot enable allmulticast, "
4478 "device is in isolated mode", (void *)dev);
4482 if (priv->allmulti) {
4486 /* If device isn't started, this is all we need to do. */
4490 ret = rxq_allmulticast_enable(LIST_FIRST(&priv->parents));
4497 for (i = 0; (i != priv->rxqs_n); ++i) {
4498 if ((*priv->rxqs)[i] == NULL)
4500 ret = rxq_allmulticast_enable((*priv->rxqs)[i]);
4503 /* Failure, rollback. */
4505 if ((*priv->rxqs)[--i] != NULL)
4506 rxq_allmulticast_disable((*priv->rxqs)[i]);
4516 * DPDK callback to disable allmulti mode.
4519 * Pointer to Ethernet device structure.
4522 mlx4_allmulticast_disable(struct rte_eth_dev *dev)
4524 struct priv *priv = dev->data->dev_private;
4528 if (!priv->allmulti || priv->isolated) {
4533 rxq_allmulticast_disable(LIST_FIRST(&priv->parents));
4536 for (i = 0; (i != priv->rxqs_n); ++i)
4537 if ((*priv->rxqs)[i] != NULL)
4538 rxq_allmulticast_disable((*priv->rxqs)[i]);
4545 * DPDK callback to retrieve physical link information.
4548 * Pointer to Ethernet device structure.
4549 * @param wait_to_complete
4550 * Wait for request completion (ignored).
4553 mlx4_link_update(struct rte_eth_dev *dev, int wait_to_complete)
4555 const struct priv *priv = dev->data->dev_private;
4556 struct ethtool_cmd edata = {
4560 struct rte_eth_link dev_link;
4563 /* priv_lock() is not taken to allow concurrent calls. */
4567 (void)wait_to_complete;
4568 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
4569 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
4572 memset(&dev_link, 0, sizeof(dev_link));
4573 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
4574 (ifr.ifr_flags & IFF_RUNNING));
4575 ifr.ifr_data = (void *)&edata;
4576 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4577 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
4581 link_speed = ethtool_cmd_speed(&edata);
4582 if (link_speed == -1)
4583 dev_link.link_speed = 0;
4585 dev_link.link_speed = link_speed;
4586 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
4587 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
4588 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
4589 ETH_LINK_SPEED_FIXED);
4590 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
4591 /* Link status changed. */
4592 dev->data->dev_link = dev_link;
4595 /* Link status is still the same. */
4600 * DPDK callback to change the MTU.
4602 * Setting the MTU affects hardware MRU (packets larger than the MTU cannot be
4603 * received). Use this as a hint to enable/disable scattered packets support
4604 * and improve performance when not needed.
4605 * Since failure is not an option, reconfiguring queues on the fly is not
4609 * Pointer to Ethernet device structure.
4614 * 0 on success, negative errno value on failure.
4617 mlx4_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
4619 struct priv *priv = dev->data->dev_private;
4622 uint16_t (*rx_func)(void *, struct rte_mbuf **, uint16_t) =
4626 /* Set kernel interface MTU first. */
4627 if (priv_set_mtu(priv, mtu)) {
4629 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
4633 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
4635 /* Temporarily replace RX handler with a fake one, assuming it has not
4636 * been copied elsewhere. */
4637 dev->rx_pkt_burst = removed_rx_burst;
4638 /* Make sure everyone has left mlx4_rx_burst() and uses
4639 * removed_rx_burst() instead. */
4642 /* Reconfigure each RX queue. */
4643 for (i = 0; (i != priv->rxqs_n); ++i) {
4644 struct rxq *rxq = (*priv->rxqs)[i];
4645 unsigned int max_frame_len;
4649 /* Calculate new maximum frame length according to MTU. */
4650 max_frame_len = (priv->mtu + ETHER_HDR_LEN +
4651 (ETHER_MAX_VLAN_FRAME_LEN - ETHER_MAX_LEN));
4652 /* Provide new values to rxq_setup(). */
4653 dev->data->dev_conf.rxmode.jumbo_frame =
4654 (max_frame_len > ETHER_MAX_LEN);
4655 dev->data->dev_conf.rxmode.max_rx_pkt_len = max_frame_len;
4656 ret = rxq_rehash(dev, rxq);
4658 /* Force SP RX if that queue requires it and abort. */
4660 rx_func = mlx4_rx_burst_sp;
4663 /* Reenable non-RSS queue attributes. No need to check
4664 * for errors at this stage. */
4665 if (!priv->rss && !priv->isolated) {
4666 rxq_mac_addrs_add(rxq);
4668 rxq_promiscuous_enable(rxq);
4670 rxq_allmulticast_enable(rxq);
4672 /* Scattered burst function takes priority. */
4674 rx_func = mlx4_rx_burst_sp;
4676 /* Burst functions can now be called again. */
4678 dev->rx_pkt_burst = rx_func;
4686 * DPDK callback to get flow control status.
4689 * Pointer to Ethernet device structure.
4690 * @param[out] fc_conf
4691 * Flow control output buffer.
4694 * 0 on success, negative errno value on failure.
4697 mlx4_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4699 struct priv *priv = dev->data->dev_private;
4701 struct ethtool_pauseparam ethpause = {
4702 .cmd = ETHTOOL_GPAUSEPARAM
4706 ifr.ifr_data = (void *)ðpause;
4708 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4710 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
4716 fc_conf->autoneg = ethpause.autoneg;
4717 if (ethpause.rx_pause && ethpause.tx_pause)
4718 fc_conf->mode = RTE_FC_FULL;
4719 else if (ethpause.rx_pause)
4720 fc_conf->mode = RTE_FC_RX_PAUSE;
4721 else if (ethpause.tx_pause)
4722 fc_conf->mode = RTE_FC_TX_PAUSE;
4724 fc_conf->mode = RTE_FC_NONE;
4734 * DPDK callback to modify flow control parameters.
4737 * Pointer to Ethernet device structure.
4738 * @param[in] fc_conf
4739 * Flow control parameters.
4742 * 0 on success, negative errno value on failure.
4745 mlx4_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4747 struct priv *priv = dev->data->dev_private;
4749 struct ethtool_pauseparam ethpause = {
4750 .cmd = ETHTOOL_SPAUSEPARAM
4754 ifr.ifr_data = (void *)ðpause;
4755 ethpause.autoneg = fc_conf->autoneg;
4756 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4757 (fc_conf->mode & RTE_FC_RX_PAUSE))
4758 ethpause.rx_pause = 1;
4760 ethpause.rx_pause = 0;
4762 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4763 (fc_conf->mode & RTE_FC_TX_PAUSE))
4764 ethpause.tx_pause = 1;
4766 ethpause.tx_pause = 0;
4769 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4771 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
4785 * Configure a VLAN filter.
4788 * Pointer to Ethernet device structure.
4790 * VLAN ID to filter.
4795 * 0 on success, errno value on failure.
4798 vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
4800 struct priv *priv = dev->data->dev_private;
4802 unsigned int j = -1;
4804 DEBUG("%p: %s VLAN filter ID %" PRIu16,
4805 (void *)dev, (on ? "enable" : "disable"), vlan_id);
4806 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
4807 if (!priv->vlan_filter[i].enabled) {
4808 /* Unused index, remember it. */
4812 if (priv->vlan_filter[i].id != vlan_id)
4814 /* This VLAN ID is already known, use its index. */
4818 /* Check if there's room for another VLAN filter. */
4819 if (j == (unsigned int)-1)
4822 * VLAN filters apply to all configured MAC addresses, flow
4823 * specifications must be reconfigured accordingly.
4825 priv->vlan_filter[j].id = vlan_id;
4826 if ((on) && (!priv->vlan_filter[j].enabled)) {
4828 * Filter is disabled, enable it.
4829 * Rehashing flows in all RX queues is necessary.
4832 rxq_mac_addrs_del(LIST_FIRST(&priv->parents));
4834 for (i = 0; (i != priv->rxqs_n); ++i)
4835 if ((*priv->rxqs)[i] != NULL)
4836 rxq_mac_addrs_del((*priv->rxqs)[i]);
4837 priv->vlan_filter[j].enabled = 1;
4838 if (priv->started) {
4840 rxq_mac_addrs_add(LIST_FIRST(&priv->parents));
4842 for (i = 0; (i != priv->rxqs_n); ++i) {
4843 if ((*priv->rxqs)[i] == NULL)
4845 rxq_mac_addrs_add((*priv->rxqs)[i]);
4848 } else if ((!on) && (priv->vlan_filter[j].enabled)) {
4850 * Filter is enabled, disable it.
4851 * Rehashing flows in all RX queues is necessary.
4854 rxq_mac_addrs_del(LIST_FIRST(&priv->parents));
4856 for (i = 0; (i != priv->rxqs_n); ++i)
4857 if ((*priv->rxqs)[i] != NULL)
4858 rxq_mac_addrs_del((*priv->rxqs)[i]);
4859 priv->vlan_filter[j].enabled = 0;
4860 if (priv->started) {
4862 rxq_mac_addrs_add(LIST_FIRST(&priv->parents));
4864 for (i = 0; (i != priv->rxqs_n); ++i) {
4865 if ((*priv->rxqs)[i] == NULL)
4867 rxq_mac_addrs_add((*priv->rxqs)[i]);
4875 * DPDK callback to configure a VLAN filter.
4878 * Pointer to Ethernet device structure.
4880 * VLAN ID to filter.
4885 * 0 on success, negative errno value on failure.
4888 mlx4_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
4890 struct priv *priv = dev->data->dev_private;
4894 if (priv->isolated) {
4895 DEBUG("%p: cannot set vlan filter, "
4896 "device is in isolated mode", (void *)dev);
4900 ret = vlan_filter_set(dev, vlan_id, on);
4906 const struct rte_flow_ops mlx4_flow_ops = {
4907 .validate = mlx4_flow_validate,
4908 .create = mlx4_flow_create,
4909 .destroy = mlx4_flow_destroy,
4910 .flush = mlx4_flow_flush,
4912 .isolate = mlx4_flow_isolate,
4916 * Manage filter operations.
4919 * Pointer to Ethernet device structure.
4920 * @param filter_type
4923 * Operation to perform.
4925 * Pointer to operation-specific structure.
4928 * 0 on success, negative errno value on failure.
4931 mlx4_dev_filter_ctrl(struct rte_eth_dev *dev,
4932 enum rte_filter_type filter_type,
4933 enum rte_filter_op filter_op,
4938 switch (filter_type) {
4939 case RTE_ETH_FILTER_GENERIC:
4940 if (filter_op != RTE_ETH_FILTER_GET)
4942 *(const void **)arg = &mlx4_flow_ops;
4945 ERROR("%p: filter type (%d) not supported",
4946 (void *)dev, filter_type);
4952 static const struct eth_dev_ops mlx4_dev_ops = {
4953 .dev_configure = mlx4_dev_configure,
4954 .dev_start = mlx4_dev_start,
4955 .dev_stop = mlx4_dev_stop,
4956 .dev_set_link_down = mlx4_set_link_down,
4957 .dev_set_link_up = mlx4_set_link_up,
4958 .dev_close = mlx4_dev_close,
4959 .promiscuous_enable = mlx4_promiscuous_enable,
4960 .promiscuous_disable = mlx4_promiscuous_disable,
4961 .allmulticast_enable = mlx4_allmulticast_enable,
4962 .allmulticast_disable = mlx4_allmulticast_disable,
4963 .link_update = mlx4_link_update,
4964 .stats_get = mlx4_stats_get,
4965 .stats_reset = mlx4_stats_reset,
4966 .dev_infos_get = mlx4_dev_infos_get,
4967 .dev_supported_ptypes_get = mlx4_dev_supported_ptypes_get,
4968 .vlan_filter_set = mlx4_vlan_filter_set,
4969 .rx_queue_setup = mlx4_rx_queue_setup,
4970 .tx_queue_setup = mlx4_tx_queue_setup,
4971 .rx_queue_release = mlx4_rx_queue_release,
4972 .tx_queue_release = mlx4_tx_queue_release,
4973 .flow_ctrl_get = mlx4_dev_get_flow_ctrl,
4974 .flow_ctrl_set = mlx4_dev_set_flow_ctrl,
4975 .mac_addr_remove = mlx4_mac_addr_remove,
4976 .mac_addr_add = mlx4_mac_addr_add,
4977 .mac_addr_set = mlx4_mac_addr_set,
4978 .mtu_set = mlx4_dev_set_mtu,
4979 .filter_ctrl = mlx4_dev_filter_ctrl,
4980 .rx_queue_intr_enable = mlx4_rx_intr_enable,
4981 .rx_queue_intr_disable = mlx4_rx_intr_disable,
4985 * Get PCI information from struct ibv_device.
4988 * Pointer to Ethernet device structure.
4989 * @param[out] pci_addr
4990 * PCI bus address output buffer.
4993 * 0 on success, -1 on failure and errno is set.
4996 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
4997 struct rte_pci_addr *pci_addr)
5001 MKSTR(path, "%s/device/uevent", device->ibdev_path);
5003 file = fopen(path, "rb");
5006 while (fgets(line, sizeof(line), file) == line) {
5007 size_t len = strlen(line);
5010 /* Truncate long lines. */
5011 if (len == (sizeof(line) - 1))
5012 while (line[(len - 1)] != '\n') {
5016 line[(len - 1)] = ret;
5018 /* Extract information. */
5021 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
5025 &pci_addr->function) == 4) {
5035 * Get MAC address by querying netdevice.
5038 * struct priv for the requested device.
5040 * MAC address output buffer.
5043 * 0 on success, -1 on failure and errno is set.
5046 priv_get_mac(struct priv *priv, uint8_t (*mac)[ETHER_ADDR_LEN])
5048 struct ifreq request;
5050 if (priv_ifreq(priv, SIOCGIFHWADDR, &request))
5052 memcpy(mac, request.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
5057 * Retrieve integer value from environment variable.
5060 * Environment variable name.
5063 * Integer value, 0 if the variable is not set.
5066 mlx4_getenv_int(const char *name)
5068 const char *val = getenv(name);
5076 mlx4_dev_link_status_handler(void *);
5078 mlx4_dev_interrupt_handler(void *);
5081 * Link/device status handler.
5084 * Pointer to private structure.
5086 * Pointer to the rte_eth_dev structure.
5088 * Pointer to event flags holder.
5094 priv_dev_status_handler(struct priv *priv, struct rte_eth_dev *dev,
5097 struct ibv_async_event event;
5098 int port_change = 0;
5099 struct rte_eth_link *link = &dev->data->dev_link;
5103 /* Read all message and acknowledge them. */
5105 if (ibv_get_async_event(priv->ctx, &event))
5107 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
5108 event.event_type == IBV_EVENT_PORT_ERR) &&
5109 (priv->intr_conf.lsc == 1)) {
5112 } else if (event.event_type == IBV_EVENT_DEVICE_FATAL &&
5113 priv->intr_conf.rmv == 1) {
5114 *events |= (1 << RTE_ETH_EVENT_INTR_RMV);
5117 DEBUG("event type %d on port %d not handled",
5118 event.event_type, event.element.port_num);
5119 ibv_ack_async_event(&event);
5123 mlx4_link_update(dev, 0);
5124 if (((link->link_speed == 0) && link->link_status) ||
5125 ((link->link_speed != 0) && !link->link_status)) {
5126 if (!priv->pending_alarm) {
5127 /* Inconsistent status, check again later. */
5128 priv->pending_alarm = 1;
5129 rte_eal_alarm_set(MLX4_ALARM_TIMEOUT_US,
5130 mlx4_dev_link_status_handler,
5134 *events |= (1 << RTE_ETH_EVENT_INTR_LSC);
5140 * Handle delayed link status event.
5143 * Registered argument.
5146 mlx4_dev_link_status_handler(void *arg)
5148 struct rte_eth_dev *dev = arg;
5149 struct priv *priv = dev->data->dev_private;
5154 assert(priv->pending_alarm == 1);
5155 priv->pending_alarm = 0;
5156 ret = priv_dev_status_handler(priv, dev, &events);
5158 if (ret > 0 && events & (1 << RTE_ETH_EVENT_INTR_LSC))
5159 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL,
5164 * Handle interrupts from the NIC.
5166 * @param[in] intr_handle
5167 * Interrupt handler.
5169 * Callback argument.
5172 mlx4_dev_interrupt_handler(void *cb_arg)
5174 struct rte_eth_dev *dev = cb_arg;
5175 struct priv *priv = dev->data->dev_private;
5181 ret = priv_dev_status_handler(priv, dev, &ev);
5184 for (i = RTE_ETH_EVENT_UNKNOWN;
5185 i < RTE_ETH_EVENT_MAX;
5187 if (ev & (1 << i)) {
5189 _rte_eth_dev_callback_process(dev, i, NULL,
5195 WARN("%d event%s not processed", ret,
5196 (ret > 1 ? "s were" : " was"));
5201 * Uninstall interrupt handler.
5204 * Pointer to private structure.
5206 * Pointer to the rte_eth_dev structure.
5208 * 0 on success, negative errno value on failure.
5211 priv_dev_interrupt_handler_uninstall(struct priv *priv, struct rte_eth_dev *dev)
5215 if (priv->intr_conf.lsc ||
5216 priv->intr_conf.rmv)
5218 ret = rte_intr_callback_unregister(&priv->intr_handle,
5219 mlx4_dev_interrupt_handler,
5222 ERROR("rte_intr_callback_unregister failed with %d"
5224 (errno ? " (errno: " : ""),
5225 (errno ? strerror(errno) : ""),
5226 (errno ? ")" : ""));
5228 priv->intr_handle.fd = 0;
5229 priv->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
5234 * Install interrupt handler.
5237 * Pointer to private structure.
5239 * Pointer to the rte_eth_dev structure.
5241 * 0 on success, negative errno value on failure.
5244 priv_dev_interrupt_handler_install(struct priv *priv,
5245 struct rte_eth_dev *dev)
5250 /* Check whether the interrupt handler has already been installed
5251 * for either type of interrupt
5253 if (priv->intr_conf.lsc &&
5254 priv->intr_conf.rmv &&
5255 priv->intr_handle.fd)
5257 assert(priv->ctx->async_fd > 0);
5258 flags = fcntl(priv->ctx->async_fd, F_GETFL);
5259 rc = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
5261 INFO("failed to change file descriptor async event queue");
5262 dev->data->dev_conf.intr_conf.lsc = 0;
5263 dev->data->dev_conf.intr_conf.rmv = 0;
5266 priv->intr_handle.fd = priv->ctx->async_fd;
5267 priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
5268 rc = rte_intr_callback_register(&priv->intr_handle,
5269 mlx4_dev_interrupt_handler,
5272 ERROR("rte_intr_callback_register failed "
5273 " (errno: %s)", strerror(errno));
5281 * Uninstall interrupt handler.
5284 * Pointer to private structure.
5286 * Pointer to the rte_eth_dev structure.
5288 * 0 on success, negative value on error.
5291 priv_dev_removal_interrupt_handler_uninstall(struct priv *priv,
5292 struct rte_eth_dev *dev)
5294 if (dev->data->dev_conf.intr_conf.rmv) {
5295 priv->intr_conf.rmv = 0;
5296 return priv_dev_interrupt_handler_uninstall(priv, dev);
5302 * Uninstall interrupt handler.
5305 * Pointer to private structure.
5307 * Pointer to the rte_eth_dev structure.
5309 * 0 on success, negative value on error,
5312 priv_dev_link_interrupt_handler_uninstall(struct priv *priv,
5313 struct rte_eth_dev *dev)
5317 if (dev->data->dev_conf.intr_conf.lsc) {
5318 priv->intr_conf.lsc = 0;
5319 ret = priv_dev_interrupt_handler_uninstall(priv, dev);
5323 if (priv->pending_alarm)
5324 if (rte_eal_alarm_cancel(mlx4_dev_link_status_handler,
5326 ERROR("rte_eal_alarm_cancel failed "
5327 " (errno: %s)", strerror(rte_errno));
5330 priv->pending_alarm = 0;
5335 * Install link interrupt handler.
5338 * Pointer to private structure.
5340 * Pointer to the rte_eth_dev structure.
5342 * 0 on success, negative value on error.
5345 priv_dev_link_interrupt_handler_install(struct priv *priv,
5346 struct rte_eth_dev *dev)
5350 if (dev->data->dev_conf.intr_conf.lsc) {
5351 ret = priv_dev_interrupt_handler_install(priv, dev);
5354 priv->intr_conf.lsc = 1;
5360 * Install removal interrupt handler.
5363 * Pointer to private structure.
5365 * Pointer to the rte_eth_dev structure.
5367 * 0 on success, negative value on error.
5370 priv_dev_removal_interrupt_handler_install(struct priv *priv,
5371 struct rte_eth_dev *dev)
5375 if (dev->data->dev_conf.intr_conf.rmv) {
5376 ret = priv_dev_interrupt_handler_install(priv, dev);
5379 priv->intr_conf.rmv = 1;
5385 * Allocate queue vector and fill epoll fd list for Rx interrupts.
5388 * Pointer to private structure.
5391 * 0 on success, negative on failure.
5394 priv_rx_intr_vec_enable(struct priv *priv)
5397 unsigned int rxqs_n = priv->rxqs_n;
5398 unsigned int n = RTE_MIN(rxqs_n, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
5399 unsigned int count = 0;
5400 struct rte_intr_handle *intr_handle = priv->dev->intr_handle;
5402 if (!priv->dev->data->dev_conf.intr_conf.rxq)
5404 priv_rx_intr_vec_disable(priv);
5405 intr_handle->intr_vec = malloc(sizeof(intr_handle->intr_vec[rxqs_n]));
5406 if (intr_handle->intr_vec == NULL) {
5407 ERROR("failed to allocate memory for interrupt vector,"
5408 " Rx interrupts will not be supported");
5411 intr_handle->type = RTE_INTR_HANDLE_EXT;
5412 for (i = 0; i != n; ++i) {
5413 struct rxq *rxq = (*priv->rxqs)[i];
5418 /* Skip queues that cannot request interrupts. */
5419 if (!rxq || !rxq->channel) {
5420 /* Use invalid intr_vec[] index to disable entry. */
5421 intr_handle->intr_vec[i] =
5422 RTE_INTR_VEC_RXTX_OFFSET +
5423 RTE_MAX_RXTX_INTR_VEC_ID;
5426 if (count >= RTE_MAX_RXTX_INTR_VEC_ID) {
5427 ERROR("too many Rx queues for interrupt vector size"
5428 " (%d), Rx interrupts cannot be enabled",
5429 RTE_MAX_RXTX_INTR_VEC_ID);
5430 priv_rx_intr_vec_disable(priv);
5433 fd = rxq->channel->fd;
5434 flags = fcntl(fd, F_GETFL);
5435 rc = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
5437 ERROR("failed to make Rx interrupt file descriptor"
5438 " %d non-blocking for queue index %d", fd, i);
5439 priv_rx_intr_vec_disable(priv);
5442 intr_handle->intr_vec[i] = RTE_INTR_VEC_RXTX_OFFSET + count;
5443 intr_handle->efds[count] = fd;
5447 priv_rx_intr_vec_disable(priv);
5449 intr_handle->nb_efd = count;
5454 * Clean up Rx interrupts handler.
5457 * Pointer to private structure.
5460 priv_rx_intr_vec_disable(struct priv *priv)
5462 struct rte_intr_handle *intr_handle = priv->dev->intr_handle;
5464 rte_intr_free_epoll_fd(intr_handle);
5465 free(intr_handle->intr_vec);
5466 intr_handle->nb_efd = 0;
5467 intr_handle->intr_vec = NULL;
5471 * DPDK callback for Rx queue interrupt enable.
5474 * Pointer to Ethernet device structure.
5479 * 0 on success, negative on failure.
5482 mlx4_rx_intr_enable(struct rte_eth_dev *dev, uint16_t idx)
5484 struct priv *priv = dev->data->dev_private;
5485 struct rxq *rxq = (*priv->rxqs)[idx];
5488 if (!rxq || !rxq->channel)
5491 ret = ibv_req_notify_cq(rxq->cq, 0);
5493 WARN("unable to arm interrupt on rx queue %d", idx);
5498 * DPDK callback for Rx queue interrupt disable.
5501 * Pointer to Ethernet device structure.
5506 * 0 on success, negative on failure.
5509 mlx4_rx_intr_disable(struct rte_eth_dev *dev, uint16_t idx)
5511 struct priv *priv = dev->data->dev_private;
5512 struct rxq *rxq = (*priv->rxqs)[idx];
5513 struct ibv_cq *ev_cq;
5517 if (!rxq || !rxq->channel) {
5520 ret = ibv_get_cq_event(rxq->cq->channel, &ev_cq, &ev_ctx);
5521 if (ret || ev_cq != rxq->cq)
5525 WARN("unable to disable interrupt on rx queue %d",
5528 ibv_ack_cq_events(rxq->cq, 1);
5533 * Verify and store value for device argument.
5536 * Key argument to verify.
5538 * Value associated with key.
5539 * @param[in, out] conf
5540 * Shared configuration data.
5543 * 0 on success, negative errno value on failure.
5546 mlx4_arg_parse(const char *key, const char *val, struct mlx4_conf *conf)
5551 tmp = strtoul(val, NULL, 0);
5553 WARN("%s: \"%s\" is not a valid integer", key, val);
5556 if (strcmp(MLX4_PMD_PORT_KVARG, key) == 0) {
5557 uint32_t ports = rte_log2_u32(conf->ports.present);
5560 ERROR("port index %lu outside range [0,%" PRIu32 ")",
5564 if (!(conf->ports.present & (1 << tmp))) {
5565 ERROR("invalid port index %lu", tmp);
5568 conf->ports.enabled |= 1 << tmp;
5570 WARN("%s: unknown parameter", key);
5577 * Parse device parameters.
5580 * Device arguments structure.
5583 * 0 on success, negative errno value on failure.
5586 mlx4_args(struct rte_devargs *devargs, struct mlx4_conf *conf)
5588 struct rte_kvargs *kvlist;
5589 unsigned int arg_count;
5593 if (devargs == NULL)
5595 kvlist = rte_kvargs_parse(devargs->args, pmd_mlx4_init_params);
5596 if (kvlist == NULL) {
5597 ERROR("failed to parse kvargs");
5600 /* Process parameters. */
5601 for (i = 0; pmd_mlx4_init_params[i]; ++i) {
5602 arg_count = rte_kvargs_count(kvlist, MLX4_PMD_PORT_KVARG);
5603 while (arg_count-- > 0) {
5604 ret = rte_kvargs_process(kvlist,
5605 MLX4_PMD_PORT_KVARG,
5606 (int (*)(const char *,
5616 rte_kvargs_free(kvlist);
5620 static struct rte_pci_driver mlx4_driver;
5623 * DPDK callback to register a PCI device.
5625 * This function creates an Ethernet device for each port of a given
5628 * @param[in] pci_drv
5629 * PCI driver structure (mlx4_driver).
5630 * @param[in] pci_dev
5631 * PCI device information.
5634 * 0 on success, negative errno value on failure.
5637 mlx4_pci_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
5639 struct ibv_device **list;
5640 struct ibv_device *ibv_dev;
5642 struct ibv_context *attr_ctx = NULL;
5643 struct ibv_device_attr device_attr;
5644 struct mlx4_conf conf = {
5651 assert(pci_drv == &mlx4_driver);
5653 list = ibv_get_device_list(&i);
5656 if (errno == ENOSYS)
5657 ERROR("cannot list devices, is ib_uverbs loaded?");
5662 * For each listed device, check related sysfs entry against
5663 * the provided PCI ID.
5666 struct rte_pci_addr pci_addr;
5669 DEBUG("checking device \"%s\"", list[i]->name);
5670 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
5672 if ((pci_dev->addr.domain != pci_addr.domain) ||
5673 (pci_dev->addr.bus != pci_addr.bus) ||
5674 (pci_dev->addr.devid != pci_addr.devid) ||
5675 (pci_dev->addr.function != pci_addr.function))
5677 vf = (pci_dev->id.device_id ==
5678 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
5679 INFO("PCI information matches, using device \"%s\" (VF: %s)",
5680 list[i]->name, (vf ? "true" : "false"));
5681 attr_ctx = ibv_open_device(list[i]);
5685 if (attr_ctx == NULL) {
5686 ibv_free_device_list(list);
5689 ERROR("cannot access device, is mlx4_ib loaded?");
5692 ERROR("cannot use device, are drivers up to date?");
5700 DEBUG("device opened");
5701 if (ibv_query_device(attr_ctx, &device_attr)) {
5705 INFO("%u port(s) detected", device_attr.phys_port_cnt);
5707 conf.ports.present |= (UINT64_C(1) << device_attr.phys_port_cnt) - 1;
5708 if (mlx4_args(pci_dev->device.devargs, &conf)) {
5709 ERROR("failed to process device arguments");
5713 /* Use all ports when none are defined */
5714 if (!conf.ports.enabled)
5715 conf.ports.enabled = conf.ports.present;
5716 for (i = 0; i < device_attr.phys_port_cnt; i++) {
5717 uint32_t port = i + 1; /* ports are indexed from one */
5718 struct ibv_context *ctx = NULL;
5719 struct ibv_port_attr port_attr;
5720 struct ibv_pd *pd = NULL;
5721 struct priv *priv = NULL;
5722 struct rte_eth_dev *eth_dev = NULL;
5723 struct ibv_exp_device_attr exp_device_attr;
5724 struct ether_addr mac;
5726 /* If port is not enabled, skip. */
5727 if (!(conf.ports.enabled & (1 << i)))
5729 exp_device_attr.comp_mask = IBV_EXP_DEVICE_ATTR_EXP_CAP_FLAGS;
5730 exp_device_attr.comp_mask |= IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ;
5732 DEBUG("using port %u", port);
5734 ctx = ibv_open_device(ibv_dev);
5740 /* Check port status. */
5741 err = ibv_query_port(ctx, port, &port_attr);
5743 ERROR("port query failed: %s", strerror(err));
5748 if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
5749 ERROR("port %d is not configured in Ethernet mode",
5755 if (port_attr.state != IBV_PORT_ACTIVE)
5756 DEBUG("port %d is not active: \"%s\" (%d)",
5757 port, ibv_port_state_str(port_attr.state),
5760 /* Allocate protection domain. */
5761 pd = ibv_alloc_pd(ctx);
5763 ERROR("PD allocation failure");
5768 /* from rte_ethdev.c */
5769 priv = rte_zmalloc("ethdev private structure",
5771 RTE_CACHE_LINE_SIZE);
5773 ERROR("priv allocation failure");
5779 priv->device_attr = device_attr;
5782 priv->mtu = ETHER_MTU;
5783 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
5784 ERROR("ibv_exp_query_device() failed");
5788 if ((exp_device_attr.exp_device_cap_flags &
5789 IBV_EXP_DEVICE_QPG) &&
5790 (exp_device_attr.exp_device_cap_flags &
5791 IBV_EXP_DEVICE_UD_RSS) &&
5792 (exp_device_attr.comp_mask &
5793 IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ) &&
5794 (exp_device_attr.max_rss_tbl_sz > 0)) {
5797 priv->max_rss_tbl_sz = exp_device_attr.max_rss_tbl_sz;
5801 priv->max_rss_tbl_sz = 0;
5803 priv->hw_tss = !!(exp_device_attr.exp_device_cap_flags &
5804 IBV_EXP_DEVICE_UD_TSS);
5805 DEBUG("device flags: %s%s%s",
5806 (priv->hw_qpg ? "IBV_DEVICE_QPG " : ""),
5807 (priv->hw_tss ? "IBV_DEVICE_TSS " : ""),
5808 (priv->hw_rss ? "IBV_DEVICE_RSS " : ""));
5810 DEBUG("maximum RSS indirection table size: %u",
5811 exp_device_attr.max_rss_tbl_sz);
5814 ((exp_device_attr.exp_device_cap_flags &
5815 IBV_EXP_DEVICE_RX_CSUM_TCP_UDP_PKT) &&
5816 (exp_device_attr.exp_device_cap_flags &
5817 IBV_EXP_DEVICE_RX_CSUM_IP_PKT));
5818 DEBUG("checksum offloading is %ssupported",
5819 (priv->hw_csum ? "" : "not "));
5821 priv->hw_csum_l2tun = !!(exp_device_attr.exp_device_cap_flags &
5822 IBV_EXP_DEVICE_VXLAN_SUPPORT);
5823 DEBUG("L2 tunnel checksum offloads are %ssupported",
5824 (priv->hw_csum_l2tun ? "" : "not "));
5826 priv->inl_recv_size = mlx4_getenv_int("MLX4_INLINE_RECV_SIZE");
5828 if (priv->inl_recv_size) {
5829 exp_device_attr.comp_mask =
5830 IBV_EXP_DEVICE_ATTR_INLINE_RECV_SZ;
5831 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
5832 INFO("Couldn't query device for inline-receive"
5834 priv->inl_recv_size = 0;
5836 if ((unsigned)exp_device_attr.inline_recv_sz <
5837 priv->inl_recv_size) {
5838 INFO("Max inline-receive (%d) <"
5839 " requested inline-receive (%u)",
5840 exp_device_attr.inline_recv_sz,
5841 priv->inl_recv_size);
5842 priv->inl_recv_size =
5843 exp_device_attr.inline_recv_sz;
5846 INFO("Set inline receive size to %u",
5847 priv->inl_recv_size);
5851 /* Configure the first MAC address by default. */
5852 if (priv_get_mac(priv, &mac.addr_bytes)) {
5853 ERROR("cannot get MAC address, is mlx4_en loaded?"
5854 " (errno: %s)", strerror(errno));
5858 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
5860 mac.addr_bytes[0], mac.addr_bytes[1],
5861 mac.addr_bytes[2], mac.addr_bytes[3],
5862 mac.addr_bytes[4], mac.addr_bytes[5]);
5863 /* Register MAC and broadcast addresses. */
5864 claim_zero(priv_mac_addr_add(priv, 0,
5865 (const uint8_t (*)[ETHER_ADDR_LEN])
5867 claim_zero(priv_mac_addr_add(priv, (elemof(priv->mac) - 1),
5868 &(const uint8_t [ETHER_ADDR_LEN])
5869 { "\xff\xff\xff\xff\xff\xff" }));
5872 char ifname[IF_NAMESIZE];
5874 if (priv_get_ifname(priv, &ifname) == 0)
5875 DEBUG("port %u ifname is \"%s\"",
5876 priv->port, ifname);
5878 DEBUG("port %u ifname is unknown", priv->port);
5881 /* Get actual MTU if possible. */
5882 priv_get_mtu(priv, &priv->mtu);
5883 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
5885 /* from rte_ethdev.c */
5887 char name[RTE_ETH_NAME_MAX_LEN];
5889 snprintf(name, sizeof(name), "%s port %u",
5890 ibv_get_device_name(ibv_dev), port);
5891 eth_dev = rte_eth_dev_allocate(name);
5893 if (eth_dev == NULL) {
5894 ERROR("can not allocate rte ethdev");
5899 eth_dev->data->dev_private = priv;
5900 eth_dev->data->mac_addrs = priv->mac;
5901 eth_dev->device = &pci_dev->device;
5903 rte_eth_copy_pci_info(eth_dev, pci_dev);
5905 eth_dev->device->driver = &mlx4_driver.driver;
5908 * Copy and override interrupt handle to prevent it from
5909 * being shared between all ethdev instances of a given PCI
5910 * device. This is required to properly handle Rx interrupts
5913 priv->intr_handle_dev = *eth_dev->intr_handle;
5914 eth_dev->intr_handle = &priv->intr_handle_dev;
5916 priv->dev = eth_dev;
5917 eth_dev->dev_ops = &mlx4_dev_ops;
5918 eth_dev->data->dev_flags |= RTE_ETH_DEV_DETACHABLE;
5920 /* Bring Ethernet device up. */
5921 DEBUG("forcing Ethernet interface up");
5922 priv_set_flags(priv, ~IFF_UP, IFF_UP);
5923 /* Update link status once if waiting for LSC. */
5924 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
5925 mlx4_link_update(eth_dev, 0);
5931 claim_zero(ibv_dealloc_pd(pd));
5933 claim_zero(ibv_close_device(ctx));
5935 rte_eth_dev_release_port(eth_dev);
5938 if (i == device_attr.phys_port_cnt)
5942 * XXX if something went wrong in the loop above, there is a resource
5943 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
5944 * long as the dpdk does not provide a way to deallocate a ethdev and a
5945 * way to enumerate the registered ethdevs to free the previous ones.
5950 claim_zero(ibv_close_device(attr_ctx));
5952 ibv_free_device_list(list);
5957 static const struct rte_pci_id mlx4_pci_id_map[] = {
5959 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
5960 PCI_DEVICE_ID_MELLANOX_CONNECTX3)
5963 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
5964 PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO)
5967 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
5968 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF)
5975 static struct rte_pci_driver mlx4_driver = {
5977 .name = MLX4_DRIVER_NAME
5979 .id_table = mlx4_pci_id_map,
5980 .probe = mlx4_pci_probe,
5981 .drv_flags = RTE_PCI_DRV_INTR_LSC |
5982 RTE_PCI_DRV_INTR_RMV,
5986 * Driver initialization routine.
5988 RTE_INIT(rte_mlx4_pmd_init);
5990 rte_mlx4_pmd_init(void)
5992 RTE_BUILD_BUG_ON(sizeof(wr_id_t) != sizeof(uint64_t));
5994 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
5995 * huge pages. Calling ibv_fork_init() during init allows
5996 * applications to use fork() safely for purposes other than
5997 * using this PMD, which is not supported in forked processes.
5999 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
6001 rte_pci_register(&mlx4_driver);
6004 RTE_PMD_EXPORT_NAME(net_mlx4, __COUNTER__);
6005 RTE_PMD_REGISTER_PCI_TABLE(net_mlx4, mlx4_pci_id_map);
6006 RTE_PMD_REGISTER_KMOD_DEP(net_mlx4,
6007 "* ib_uverbs & mlx4_en & mlx4_core & mlx4_ib");