Check for hairpin queue before enabling queues to detect
errors before registering callbacks. An explicit queue number is
also now range checked against the number of queues on the port.

A port with a hairpin queue set up for a flow rule still has normal
queues worth capturing, and asking for all queues is the common case.
Skip the hairpin queues there and log which ones were left out, rather
than failing the whole request. Naming a hairpin queue explicitly is
still an error, since that capture would never produce a packet.

If the skipping leaves nothing at all, the enable fails. A capture
with no callbacks installed delivers no packets, so the client would
sit waiting for traffic that cannot arrive. The count is taken across
both directions, so capturing rx and tx still works when only one of
them has a non hairpin queue.

The other failure possible is running out of memory while adding a
callback. Remove the callbacks already installed for the request
when that happens, rather than leaving them referencing a ring the
client is about to free. Only callbacks using this request's ring
are removed, so an unrelated capture on the port is left alone.

Bugzilla ID: 2075
Fixes: cf5516696d77 ("ethdev: add hairpin queue")
Fixes: 278f945402c5 ("pdump: add new library for packet capture")
Cc: [email protected]

Signed-off-by: Stephen Hemminger <[email protected]>
---
 lib/pdump/rte_pdump.c | 209 ++++++++++++++++++++++++++++++++----------
 1 file changed, 162 insertions(+), 47 deletions(-)

diff --git a/lib/pdump/rte_pdump.c b/lib/pdump/rte_pdump.c
index 56a12f96ca..eb4bd11acc 100644
--- a/lib/pdump/rte_pdump.c
+++ b/lib/pdump/rte_pdump.c
@@ -8,6 +8,7 @@
 #include <rte_alarm.h>
 #include <rte_mbuf.h>
 #include <rte_ethdev.h>
+#include <ethdev_driver.h>
 #include <rte_lcore.h>
 #include <rte_log.h>
 #include <rte_memzone.h>
@@ -250,6 +251,65 @@ pdump_tx(uint16_t port, uint16_t queue,
 }
 
 
+/*
+ * Check all queues before changing any. Capturing all queues skips the
+ * hairpin ones, since a port set up for hairpin still has normal queues
+ * worth capturing. Asking for a hairpin queue by number is an error.
+ *
+ * Returns the number of queues to act on, so the caller can tell a
+ * request that does nothing from one that works on a single direction.
+ */
+static int
+pdump_check_queues(uint16_t port, uint16_t queue, uint16_t end_q,
+                  bool rx, uint16_t operation)
+{
+       struct rte_eth_dev *dev = &rte_eth_devices[port];
+       bool all = queue == RTE_PDUMP_ALL_QUEUES;
+       const char *dir = rx ? "rx" : "tx";
+       uint16_t qid, count = 0;
+
+       qid = all ? 0 : queue;
+       for (; qid < end_q; qid++) {
+               const struct pdump_rxtx_cbs *cbs =
+                       rx ? &rx_cbs[port][qid] : &tx_cbs[port][qid];
+
+               /* hairpin queues do not pass through rx/tx burst */
+               if (rx ? rte_eth_dev_is_rx_hairpin_queue(dev, qid) :
+                        rte_eth_dev_is_tx_hairpin_queue(dev, qid)) {
+                       if (all) {
+                               if (operation == ENABLE)
+                                       PDUMP_LOG_LINE(NOTICE,
+                                               "skipping hairpin %s queue 
port=%u queue=%u",
+                                               dir, port, qid);
+                               continue;
+                       }
+                       PDUMP_LOG_LINE(ERR,
+                               "cannot capture on hairpin %s queue port=%u 
queue=%u",
+                               dir, port, qid);
+                       return -EINVAL;
+               }
+
+               /* a port can only be captured by one client at a time */
+               if (operation == ENABLE && cbs->cb != NULL) {
+                       PDUMP_LOG_LINE(ERR,
+                               "%s callback for port=%u queue=%u, already 
exists",
+                               dir, port, qid);
+                       return -EEXIST;
+               }
+               if (operation == DISABLE && cbs->cb == NULL) {
+                       if (all)
+                               continue;
+                       PDUMP_LOG_LINE(ERR,
+                               "no existing %s callback for port=%u queue=%u",
+                               dir, port, qid);
+                       return -EINVAL;
+               }
+               ++count;
+       }
+
+       return count;
+}
+
 static int
 pdump_register_rx_callbacks(enum pdump_version ver,
                            uint16_t end_q, uint16_t port, uint16_t queue,
@@ -257,19 +317,20 @@ pdump_register_rx_callbacks(enum pdump_version ver,
                            struct rte_bpf *filter,
                            uint16_t operation, uint32_t snaplen)
 {
+       struct rte_eth_dev *dev = &rte_eth_devices[port];
        uint16_t qid;
 
        qid = (queue == RTE_PDUMP_ALL_QUEUES) ? 0 : queue;
        for (; qid < end_q; qid++) {
                struct pdump_rxtx_cbs *cbs = &rx_cbs[port][qid];
 
+               if (rte_eth_dev_is_rx_hairpin_queue(dev, qid))
+                       continue;
+
+               if (operation == DISABLE && cbs->cb == NULL)
+                       continue;
+
                if (operation == ENABLE) {
-                       if (cbs->cb) {
-                               PDUMP_LOG_LINE(ERR,
-                                       "rx callback for port=%d queue=%d, 
already exists",
-                                       port, qid);
-                               return -EEXIST;
-                       }
                        cbs->use_count = 0;
                        cbs->ver = ver;
                        cbs->ring = ring;
@@ -281,8 +342,8 @@ pdump_register_rx_callbacks(enum pdump_version ver,
                                                                pdump_rx, cbs);
                        if (cbs->cb == NULL) {
                                PDUMP_LOG_LINE(ERR,
-                                       "failed to add rx callback, errno=%d",
-                                       rte_errno);
+                                       "failed to add rx callback port=%u 
queue=%u: %s",
+                                       port, qid, rte_strerror(rte_errno));
                                return -rte_errno;
                        }
 
@@ -290,12 +351,14 @@ pdump_register_rx_callbacks(enum pdump_version ver,
                } else if (operation == DISABLE) {
                        int ret;
 
-                       if (cbs->cb == NULL) {
-                               PDUMP_LOG_LINE(ERR,
-                                       "no existing rx callback for port=%d 
queue=%d",
-                                       port, qid);
-                               return -EINVAL;
-                       }
+                       /*
+                        * An undo passes the ring to remove only what the
+                        * failed request installed. A client disable has no
+                        * ring and removes whatever is there.
+                        */
+                       if (ring != NULL && cbs->ring != ring)
+                               continue;
+
                        ret = rte_eth_remove_rx_callback(port, qid, cbs->cb);
                        if (ret < 0) {
                                PDUMP_LOG_LINE(ERR,
@@ -318,20 +381,20 @@ pdump_register_tx_callbacks(enum pdump_version ver,
                            struct rte_bpf *filter,
                            uint16_t operation, uint32_t snaplen)
 {
-
+       struct rte_eth_dev *dev = &rte_eth_devices[port];
        uint16_t qid;
 
        qid = (queue == RTE_PDUMP_ALL_QUEUES) ? 0 : queue;
        for (; qid < end_q; qid++) {
                struct pdump_rxtx_cbs *cbs = &tx_cbs[port][qid];
 
+               if (rte_eth_dev_is_tx_hairpin_queue(dev, qid))
+                       continue;
+
+               if (operation == DISABLE && cbs->cb == NULL)
+                       continue;
+
                if (operation == ENABLE) {
-                       if (cbs->cb) {
-                               PDUMP_LOG_LINE(ERR,
-                                       "tx callback for port=%d queue=%d, 
already exists",
-                                       port, qid);
-                               return -EEXIST;
-                       }
                        cbs->use_count = 0;
                        cbs->ver = ver;
                        cbs->ring = ring;
@@ -343,20 +406,22 @@ pdump_register_tx_callbacks(enum pdump_version ver,
                                                                cbs);
                        if (cbs->cb == NULL) {
                                PDUMP_LOG_LINE(ERR,
-                                       "failed to add tx callback, errno=%d",
-                                       rte_errno);
+                                       "failed to add tx callback port=%u 
queue=%u: %s",
+                                       port, qid, rte_strerror(rte_errno));
                                return -rte_errno;
                        }
                        memset(&pdump_stats->tx[port][qid], 0, sizeof(struct 
rte_pdump_stats));
                } else if (operation == DISABLE) {
                        int ret;
 
-                       if (cbs->cb == NULL) {
-                               PDUMP_LOG_LINE(ERR,
-                                       "no existing tx callback for port=%d 
queue=%d",
-                                       port, qid);
-                               return -EINVAL;
-                       }
+                       /*
+                        * An undo passes the ring to remove only what the
+                        * failed request installed. A client disable has no
+                        * ring and removes whatever is there.
+                        */
+                       if (ring != NULL && cbs->ring != ring)
+                               continue;
+
                        ret = rte_eth_remove_tx_callback(port, qid, cbs->cb);
                        if (ret < 0) {
                                PDUMP_LOG_LINE(ERR,
@@ -376,8 +441,10 @@ pdump_register_tx_callbacks(enum pdump_version ver,
 static int
 set_pdump_rxtx_cbs(const struct pdump_request *p)
 {
-       uint16_t nb_rx_q = 0, nb_tx_q = 0, end_q, queue;
+       struct rte_eth_dev_info dev_info;
+       uint16_t nb_rx_q, nb_tx_q, queue;
        uint16_t port;
+       int nb_queues;
        int ret = 0;
        struct rte_bpf *filter = NULL;
        uint32_t flags;
@@ -422,18 +489,15 @@ set_pdump_rxtx_cbs(const struct pdump_request *p)
                return -EINVAL;
        }
 
-       /* validation if packet capture is for all queues */
-       if (queue == RTE_PDUMP_ALL_QUEUES) {
-               struct rte_eth_dev_info dev_info;
-
-               ret = rte_eth_dev_info_get(port, &dev_info);
-               if (ret != 0) {
-                       PDUMP_LOG_LINE(ERR,
-                               "Error during getting device (port %u) info: 
%s",
-                               port, strerror(-ret));
-                       return ret;
-               }
+       ret = rte_eth_dev_info_get(port, &dev_info);
+       if (ret != 0) {
+               PDUMP_LOG_LINE(ERR,
+                       "Error during getting device (port %u) info: %s",
+                       port, strerror(-ret));
+               return ret;
+       }
 
+       if (queue == RTE_PDUMP_ALL_QUEUES) {
                nb_rx_q = dev_info.nb_rx_queues;
                nb_tx_q = dev_info.nb_tx_queues;
                if (nb_rx_q == 0 && flags & RTE_PDUMP_FLAG_RX) {
@@ -452,26 +516,77 @@ set_pdump_rxtx_cbs(const struct pdump_request *p)
                                "both tx&rx queues must be non zero");
                        return -EINVAL;
                }
+       } else {
+               if (((flags & RTE_PDUMP_FLAG_RX) && queue >= 
dev_info.nb_rx_queues) ||
+                   ((flags & RTE_PDUMP_FLAG_TX) && queue >= 
dev_info.nb_tx_queues)) {
+                       PDUMP_LOG_LINE(ERR,
+                               "invalid queue %u for port %u", queue, port);
+                       return -EINVAL;
+               }
+               nb_rx_q = nb_tx_q = queue + 1;
+       }
+
+       /* check all queues before changing any */
+       nb_queues = 0;
+       if (flags & RTE_PDUMP_FLAG_RX) {
+               ret = pdump_check_queues(port, queue, nb_rx_q, true, operation);
+               if (ret < 0)
+                       return ret;
+               nb_queues += ret;
+       }
+       if (flags & RTE_PDUMP_FLAG_TX) {
+               ret = pdump_check_queues(port, queue, nb_tx_q, false, 
operation);
+               if (ret < 0)
+                       return ret;
+               nb_queues += ret;
+       }
+
+       /*
+        * A capture with no callbacks would never deliver a packet and the
+        * client would wait for traffic that cannot arrive. One direction
+        * being all hairpin is fine as long as the other has a queue.
+        */
+       if (nb_queues == 0 && operation == ENABLE) {
+               PDUMP_LOG_LINE(ERR, "no queue available to capture on port=%u",
+                       port);
+               return -EINVAL;
        }
 
        /* register RX callback */
        if (flags & RTE_PDUMP_FLAG_RX) {
-               end_q = (queue == RTE_PDUMP_ALL_QUEUES) ? nb_rx_q : queue + 1;
-               ret = pdump_register_rx_callbacks(p->ver, end_q, port, queue,
+               ret = pdump_register_rx_callbacks(p->ver, nb_rx_q, port, queue,
                                                  ring, mp, filter,
                                                  operation, p->snaplen);
                if (ret < 0)
-                       return ret;
+                       goto undo;
        }
 
        /* register TX callback */
        if (flags & RTE_PDUMP_FLAG_TX) {
-               end_q = (queue == RTE_PDUMP_ALL_QUEUES) ? nb_tx_q : queue + 1;
-               ret = pdump_register_tx_callbacks(p->ver, end_q, port, queue,
+               ret = pdump_register_tx_callbacks(p->ver, nb_tx_q, port, queue,
                                                  ring, mp, filter,
                                                  operation, p->snaplen);
                if (ret < 0)
-                       return ret;
+                       goto undo;
+       }
+
+       return 0;
+
+undo:
+       /*
+        * Remove the callbacks already installed, they reference a ring the
+        * client is about to free. Registration is called directly here
+        * because the checks above reject this half enabled state.
+        */
+       if (operation == ENABLE) {
+               if (flags & RTE_PDUMP_FLAG_RX)
+                       pdump_register_rx_callbacks(p->ver, nb_rx_q, port, 
queue,
+                                                   ring, mp, filter,
+                                                   DISABLE, p->snaplen);
+               if (flags & RTE_PDUMP_FLAG_TX)
+                       pdump_register_tx_callbacks(p->ver, nb_tx_q, port, 
queue,
+                                                   ring, mp, filter,
+                                                   DISABLE, p->snaplen);
        }
 
        return ret;
-- 
2.53.0

Reply via email to