[2/4] dumpcap: allow multiple invocations

Message ID 20230921042349.104150-3-stephen@networkplumber.org (mailing list archive)
State Superseded, archived
Delegated to: David Marchand
Headers
Series pcapng fixes |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Stephen Hemminger Sept. 21, 2023, 4:23 a.m. UTC
  If dumpcap is run twice with each instance pointing a different
interface, it would fail because of overlap in ring a pool names.
Fix by putting process id in the name.

Fixes: cbb44143be74 ("app/dumpcap: add new packet capture application")
Reported-by: Isaac Boukris <iboukris@gmail.com>
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 app/dumpcap/main.c | 28 ++++++++++++++--------------
 1 file changed, 14 insertions(+), 14 deletions(-)
  

Comments

Morten Brørup Sept. 21, 2023, 6:22 a.m. UTC | #1
> From: Stephen Hemminger [mailto:stephen@networkplumber.org]
> Sent: Thursday, 21 September 2023 06.24
> 
> If dumpcap is run twice with each instance pointing a different
> interface, it would fail because of overlap in ring a pool names.
> Fix by putting process id in the name.
> 
> Fixes: cbb44143be74 ("app/dumpcap: add new packet capture application")
> Reported-by: Isaac Boukris <iboukris@gmail.com>
> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
> ---
>  app/dumpcap/main.c | 28 ++++++++++++++--------------
>  1 file changed, 14 insertions(+), 14 deletions(-)
> 
> diff --git a/app/dumpcap/main.c b/app/dumpcap/main.c
> index 64294bbfb3e6..37754fd06f4f 100644
> --- a/app/dumpcap/main.c
> +++ b/app/dumpcap/main.c
> @@ -44,7 +44,6 @@
>  #include <pcap/pcap.h>
>  #include <pcap/bpf.h>
> 
> -#define RING_NAME "capture-ring"
>  #define MONITOR_INTERVAL  (500 * 1000)
>  #define MBUF_POOL_CACHE_SIZE 32
>  #define BURST_SIZE 32
> @@ -647,6 +646,7 @@ static void dpdk_init(void)
>  static struct rte_ring *create_ring(void)
>  {
>  	struct rte_ring *ring;
> +	char ring_name[RTE_RING_NAMESIZE];
>  	size_t size, log2;
> 
>  	/* Find next power of 2 >= size. */
> @@ -660,28 +660,28 @@ static struct rte_ring *create_ring(void)
>  		ring_size = size;
>  	}
> 
> -	ring = rte_ring_lookup(RING_NAME);
> -	if (ring == NULL) {
> -		ring = rte_ring_create(RING_NAME, ring_size,
> -					rte_socket_id(), 0);
> -		if (ring == NULL)
> -			rte_exit(EXIT_FAILURE, "Could not create ring :%s\n",
> -				 rte_strerror(rte_errno));
> -	}
> +	/* Want one ring per invocation of program */
> +	snprintf(ring_name, sizeof(ring_name),
> +		 "dumpcap-%u", getpid());

I'm not sure getpid() is available on Windows. How about:

#ifdef _WIN32
#include <processthreadsapi.h> // With the headers, not here.
"dumpcap-%lu", GetCurrentProcessId());
#else
"dumpcap-%u", getpid());
#endif

> +
> +	ring = rte_ring_create(ring_name, ring_size,
> +			       rte_socket_id(), 0);
> +	if (ring == NULL)
> +		rte_exit(EXIT_FAILURE, "Could not create ring :%s\n",
> +			 rte_strerror(rte_errno));
> +
>  	return ring;
>  }
> 
>  static struct rte_mempool *create_mempool(void)
>  {
>  	const struct interface *intf;
> -	static const char pool_name[] = "capture_mbufs";
> +	char pool_name[RTE_MEMPOOL_NAMESIZE];
>  	size_t num_mbufs = 2 * ring_size;
>  	struct rte_mempool *mp;
>  	uint32_t data_size = 128;
> 
> -	mp = rte_mempool_lookup(pool_name);
> -	if (mp)
> -		return mp;
> +	snprintf(pool_name, sizeof(pool_name), "capture_%u", getpid());

Same regarding getpid().

> 
>  	/* Common pool so size mbuf for biggest snap length */
>  	TAILQ_FOREACH(intf, &interfaces, next) {
> @@ -826,7 +826,7 @@ static void enable_pdump(struct rte_ring *r, struct
> rte_mempool *mp)
>  			rte_exit(EXIT_FAILURE,
>  				"Packet dump enable on %u:%s failed %s\n",
>  				intf->port, intf->name,
> -				rte_strerror(-ret));
> +				rte_strerror(rte_errno));
>  		}
> 
>  		if (intf->opts.promisc_mode) {
> --
> 2.39.2
  
Isaac Boukris Sept. 21, 2023, 7:10 a.m. UTC | #2
On Thu, Sep 21, 2023 at 9:22 AM Morten Brørup <mb@smartsharesystems.com> wrote:
>
> > From: Stephen Hemminger [mailto:stephen@networkplumber.org]
> > Sent: Thursday, 21 September 2023 06.24
> >
> > If dumpcap is run twice with each instance pointing a different
> > interface, it would fail because of overlap in ring a pool names.
> > Fix by putting process id in the name.
> >
> > Fixes: cbb44143be74 ("app/dumpcap: add new packet capture application")
> > Reported-by: Isaac Boukris <iboukris@gmail.com>
> > Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
> > ---
> >  app/dumpcap/main.c | 28 ++++++++++++++--------------
> >  1 file changed, 14 insertions(+), 14 deletions(-)
> >
> > diff --git a/app/dumpcap/main.c b/app/dumpcap/main.c
> > index 64294bbfb3e6..37754fd06f4f 100644
> > --- a/app/dumpcap/main.c
> > +++ b/app/dumpcap/main.c
> > @@ -44,7 +44,6 @@
> >  #include <pcap/pcap.h>
> >  #include <pcap/bpf.h>
> >
> > -#define RING_NAME "capture-ring"
> >  #define MONITOR_INTERVAL  (500 * 1000)
> >  #define MBUF_POOL_CACHE_SIZE 32
> >  #define BURST_SIZE 32
> > @@ -647,6 +646,7 @@ static void dpdk_init(void)
> >  static struct rte_ring *create_ring(void)
> >  {
> >       struct rte_ring *ring;
> > +     char ring_name[RTE_RING_NAMESIZE];
> >       size_t size, log2;
> >
> >       /* Find next power of 2 >= size. */
> > @@ -660,28 +660,28 @@ static struct rte_ring *create_ring(void)
> >               ring_size = size;
> >       }
> >
> > -     ring = rte_ring_lookup(RING_NAME);
> > -     if (ring == NULL) {
> > -             ring = rte_ring_create(RING_NAME, ring_size,
> > -                                     rte_socket_id(), 0);
> > -             if (ring == NULL)
> > -                     rte_exit(EXIT_FAILURE, "Could not create ring :%s\n",
> > -                              rte_strerror(rte_errno));
> > -     }
> > +     /* Want one ring per invocation of program */
> > +     snprintf(ring_name, sizeof(ring_name),
> > +              "dumpcap-%u", getpid());
>
> I'm not sure getpid() is available on Windows. How about:

I think the 'app/dumpcap/meson.build' file indicates no support for Windows.

Regards

> #ifdef _WIN32
> #include <processthreadsapi.h> // With the headers, not here.
> "dumpcap-%lu", GetCurrentProcessId());
> #else
> "dumpcap-%u", getpid());
> #endif
>
> > +
> > +     ring = rte_ring_create(ring_name, ring_size,
> > +                            rte_socket_id(), 0);
> > +     if (ring == NULL)
> > +             rte_exit(EXIT_FAILURE, "Could not create ring :%s\n",
> > +                      rte_strerror(rte_errno));
> > +
> >       return ring;
> >  }
> >
> >  static struct rte_mempool *create_mempool(void)
> >  {
> >       const struct interface *intf;
> > -     static const char pool_name[] = "capture_mbufs";
> > +     char pool_name[RTE_MEMPOOL_NAMESIZE];
> >       size_t num_mbufs = 2 * ring_size;
> >       struct rte_mempool *mp;
> >       uint32_t data_size = 128;
> >
> > -     mp = rte_mempool_lookup(pool_name);
> > -     if (mp)
> > -             return mp;
> > +     snprintf(pool_name, sizeof(pool_name), "capture_%u", getpid());
>
> Same regarding getpid().
>
> >
> >       /* Common pool so size mbuf for biggest snap length */
> >       TAILQ_FOREACH(intf, &interfaces, next) {
> > @@ -826,7 +826,7 @@ static void enable_pdump(struct rte_ring *r, struct
> > rte_mempool *mp)
> >                       rte_exit(EXIT_FAILURE,
> >                               "Packet dump enable on %u:%s failed %s\n",
> >                               intf->port, intf->name,
> > -                             rte_strerror(-ret));
> > +                             rte_strerror(rte_errno));
> >               }
> >
> >               if (intf->opts.promisc_mode) {
> > --
> > 2.39.2
>
  
Stephen Hemminger Nov. 7, 2023, 2:34 a.m. UTC | #3
On Thu, 21 Sep 2023 08:22:12 +0200
Morten Brørup <mb@smartsharesystems.com> wrote:

> I'm not sure getpid() is available on Windows. How about:
> 
> #ifdef _WIN32
> #include <processthreadsapi.h> // With the headers, not here.
> "dumpcap-%lu", GetCurrentProcessId());
> #else
> "dumpcap-%u", getpid());
> #endif


Dumpcap doesn't support windows because there are lots of things about
pdump library that won't work on Windows.
  

Patch

diff --git a/app/dumpcap/main.c b/app/dumpcap/main.c
index 64294bbfb3e6..37754fd06f4f 100644
--- a/app/dumpcap/main.c
+++ b/app/dumpcap/main.c
@@ -44,7 +44,6 @@ 
 #include <pcap/pcap.h>
 #include <pcap/bpf.h>
 
-#define RING_NAME "capture-ring"
 #define MONITOR_INTERVAL  (500 * 1000)
 #define MBUF_POOL_CACHE_SIZE 32
 #define BURST_SIZE 32
@@ -647,6 +646,7 @@  static void dpdk_init(void)
 static struct rte_ring *create_ring(void)
 {
 	struct rte_ring *ring;
+	char ring_name[RTE_RING_NAMESIZE];
 	size_t size, log2;
 
 	/* Find next power of 2 >= size. */
@@ -660,28 +660,28 @@  static struct rte_ring *create_ring(void)
 		ring_size = size;
 	}
 
-	ring = rte_ring_lookup(RING_NAME);
-	if (ring == NULL) {
-		ring = rte_ring_create(RING_NAME, ring_size,
-					rte_socket_id(), 0);
-		if (ring == NULL)
-			rte_exit(EXIT_FAILURE, "Could not create ring :%s\n",
-				 rte_strerror(rte_errno));
-	}
+	/* Want one ring per invocation of program */
+	snprintf(ring_name, sizeof(ring_name),
+		 "dumpcap-%u", getpid());
+
+	ring = rte_ring_create(ring_name, ring_size,
+			       rte_socket_id(), 0);
+	if (ring == NULL)
+		rte_exit(EXIT_FAILURE, "Could not create ring :%s\n",
+			 rte_strerror(rte_errno));
+
 	return ring;
 }
 
 static struct rte_mempool *create_mempool(void)
 {
 	const struct interface *intf;
-	static const char pool_name[] = "capture_mbufs";
+	char pool_name[RTE_MEMPOOL_NAMESIZE];
 	size_t num_mbufs = 2 * ring_size;
 	struct rte_mempool *mp;
 	uint32_t data_size = 128;
 
-	mp = rte_mempool_lookup(pool_name);
-	if (mp)
-		return mp;
+	snprintf(pool_name, sizeof(pool_name), "capture_%u", getpid());
 
 	/* Common pool so size mbuf for biggest snap length */
 	TAILQ_FOREACH(intf, &interfaces, next) {
@@ -826,7 +826,7 @@  static void enable_pdump(struct rte_ring *r, struct rte_mempool *mp)
 			rte_exit(EXIT_FAILURE,
 				"Packet dump enable on %u:%s failed %s\n",
 				intf->port, intf->name,
-				rte_strerror(-ret));
+				rte_strerror(rte_errno));
 		}
 
 		if (intf->opts.promisc_mode) {