Index | Thread | Search

From:
Vitaliy Makkoveev <mvs@openbsd.org>
Subject:
Re: common divert netstat counters
To:
Alexander Bluhm <bluhm@openbsd.org>
Cc:
tech@openbsd.org
Date:
Fri, 20 Jun 2025 20:39:09 +0300

Download raw body.

Thread
On Thu, Jun 19, 2025 at 07:14:40PM +0200, Alexander Bluhm wrote:
> Hi,
> 
> TCP and UDP share counters for IPv4 an IPv6.  Divert has duplicated
> code for inet6.  Is anybody interested in seprarate counters?
> Otherwise count both address families in a common struct.
> 
> ok
> 

reads ok by me.

> bluhm
> 
> Index: sys/netinet6/in6_proto.c
> ===================================================================
> RCS file: /data/mirror/openbsd/cvs/src/sys/netinet6/in6_proto.c,v
> diff -u -p -r1.129 in6_proto.c
> --- sys/netinet6/in6_proto.c	16 Jun 2025 07:11:58 -0000	1.129
> +++ sys/netinet6/in6_proto.c	18 Jun 2025 20:34:10 -0000
> @@ -302,7 +302,6 @@ const struct protosw inet6sw[] = {
>    .pr_ctloutput	= rip6_ctloutput,
>    .pr_usrreqs	= &divert6_usrreqs,
>    .pr_init	= divert6_init,
> -  .pr_sysctl	= divert6_sysctl
>  },
>  #endif /* NPF > 0 */
>  #if NETHERIP > 0
> Index: sys/netinet6/ip6_divert.c
> ===================================================================
> RCS file: /data/mirror/openbsd/cvs/src/sys/netinet6/ip6_divert.c,v
> diff -u -p -r1.105 ip6_divert.c
> --- sys/netinet6/ip6_divert.c	18 Jun 2025 17:45:07 -0000	1.105
> +++ sys/netinet6/ip6_divert.c	18 Jun 2025 20:34:10 -0000
> @@ -91,7 +91,7 @@ divert6_output(struct inpcb *inp, struct
>  		goto fail;
>  	if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
>  		/* m_pullup() has freed the mbuf, so just return. */
> -		div6stat_inc(divs_errors);
> +		divstat_inc(divs_errors);
>  		return (ENOBUFS);
>  	}
>  	ip6 = mtod(m, struct ip6_hdr *);
> @@ -166,11 +166,11 @@ divert6_output(struct inpcb *inp, struct
>  		    IP_ALLOWBROADCAST | IP_RAWOUTPUT, NULL, NULL);
>  	}
>  
> -	div6stat_inc(divs_opackets);
> +	divstat_inc(divs_opackets);
>  	return (error);
>  
>  fail:
> -	div6stat_inc(divs_errors);
> +	divstat_inc(divs_errors);
>  	m_freem(m);
>  	return (error ? error : EINVAL);
>  }
> @@ -182,11 +182,11 @@ divert6_packet(struct mbuf *m, int dir, 
>  	struct socket *so;
>  	struct sockaddr_in6 sin6;
>  
> -	div6stat_inc(divs_ipackets);
> +	divstat_inc(divs_ipackets);
>  
>  	if (m->m_len < sizeof(struct ip6_hdr) &&
>  	    (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
> -		div6stat_inc(divs_errors);
> +		divstat_inc(divs_errors);
>  		goto bad;
>  	}
>  
> @@ -199,7 +199,7 @@ divert6_packet(struct mbuf *m, int dir, 
>  	}
>  	mtx_leave(&divb6table.inpt_mtx);
>  	if (inp == NULL) {
> -		div6stat_inc(divs_noport);
> +		divstat_inc(divs_noport);
>  		goto bad;
>  	}
>  
> @@ -213,7 +213,7 @@ divert6_packet(struct mbuf *m, int dir, 
>  
>  		ifp = if_get(m->m_pkthdr.ph_ifidx);
>  		if (ifp == NULL) {
> -			div6stat_inc(divs_errors);
> +			divstat_inc(divs_errors);
>  			goto bad;
>  		}
>  		TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
> @@ -235,7 +235,7 @@ divert6_packet(struct mbuf *m, int dir, 
>  	mtx_enter(&so->so_rcv.sb_mtx);
>  	if (sbappendaddr(&so->so_rcv, sin6tosa(&sin6), m, NULL) == 0) {
>  		mtx_leave(&so->so_rcv.sb_mtx);
> -		div6stat_inc(divs_fullsock);
> +		divstat_inc(divs_fullsock);
>  		goto bad;
>  	}
>  	mtx_leave(&so->so_rcv.sb_mtx);
> @@ -278,25 +278,4 @@ divert6_send(struct socket *so, struct m
>  
>  	soassertlocked(so);
>  	return (divert6_output(inp, m, addr, control));
> -}
> -
> -/*
> - * Sysctl for divert variables.
> - */
> -int
> -divert6_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
> -    void *newp, size_t newlen)
> -{
> -	/* All sysctl names at this level are terminal. */
> -	if (namelen != 1)
> -		return (ENOTDIR);
> -
> -	switch (name[0]) {
> -	case DIVERT6CTL_STATS:
> -		return (divert_sysctl_divstat(div6counters, oldp, oldlenp,
> -		    newp));
> -	default:
> -		return (EOPNOTSUPP);
> -	}
> -	/* NOTREACHED */
>  }
> Index: sys/netinet6/ip6_divert.h
> ===================================================================
> RCS file: /data/mirror/openbsd/cvs/src/sys/netinet6/ip6_divert.h,v
> diff -u -p -r1.25 ip6_divert.h
> --- sys/netinet6/ip6_divert.h	18 Jun 2025 17:45:07 -0000	1.25
> +++ sys/netinet6/ip6_divert.h	18 Jun 2025 20:37:48 -0000
> @@ -31,7 +31,7 @@
>  	{ 0, 0 }, \
>  	{ NULL,	0 }, \
>  	{ NULL,	0 }, \
> -	{ "stats",	CTLTYPE_STRUCT } \
> +	{ NULL,	0 } \
>  }
>  
>  #ifdef _KERNEL
> @@ -39,21 +39,12 @@
>  #include <sys/percpu.h>
>  #include <netinet/ip_divert.h>
>  
> -extern struct cpumem *div6counters;
> -
> -static inline void
> -div6stat_inc(enum divstat_counters c)
> -{
> -	counters_inc(div6counters, c);
> -}
> -
>  extern struct	inpcbtable	divb6table;
>  
>  extern const struct pr_usrreqs divert6_usrreqs;
>  
>  void	 divert6_init(void);
>  void	 divert6_packet(struct mbuf *, int, u_int16_t);
> -int	 divert6_sysctl(int *, u_int, void *, size_t *, void *, size_t);
>  int	 divert6_attach(struct socket *, int, int);
>  int	 divert6_send(struct socket *, struct mbuf *, struct mbuf *,
>  	     struct mbuf *);
> Index: sbin/sysctl/sysctl.c
> ===================================================================
> RCS file: /data/mirror/openbsd/cvs/src/sbin/sysctl/sysctl.c,v
> diff -u -p -r1.266 sysctl.c
> --- sbin/sysctl/sysctl.c	3 Jun 2025 09:57:57 -0000	1.266
> +++ sbin/sysctl/sysctl.c	18 Jun 2025 20:59:30 -0000
> @@ -75,7 +75,6 @@
>  
>  #include <netinet/ip6.h>
>  #include <netinet/icmp6.h>
> -#include <netinet6/ip6_divert.h>
>  
>  #include <netmpls/mpls.h>
>  
> @@ -699,8 +698,7 @@ parse(char *string, int flags)
>  				special |= HEX;
>  
>  			if ((mib[2] == IPPROTO_IPV6 && mib[3] == IPV6CTL_MRTMFC) ||
> -			    (mib[2] == IPPROTO_IPV6 && mib[3] == IPV6CTL_MRTMIF) ||
> -			    (mib[2] == IPPROTO_DIVERT && mib[3] == DIVERT6CTL_STATS)) {
> +			    (mib[2] == IPPROTO_IPV6 && mib[3] == IPV6CTL_MRTMIF)) {
>  				if (flags == 0)
>  					return;
>  				warnx("use netstat to view %s information",
> @@ -2169,7 +2167,6 @@ sysctl_inet(char *string, char **bufpp, 
>  struct ctlname inet6name[] = CTL_IPV6PROTO_NAMES;
>  struct ctlname ip6name[] = IPV6CTL_NAMES;
>  struct ctlname icmp6name[] = ICMPV6CTL_NAMES;
> -struct ctlname divert6name[] = DIVERT6CTL_NAMES;
>  struct list inet6list = { inet6name, IPV6PROTO_MAXID };
>  struct list inet6vars[] = {
>  /*0*/	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
> @@ -2250,7 +2247,7 @@ struct list inet6vars[] = {
>  	{ 0, 0 },
>  	{ 0, 0 },
>  	{ 0, 0 },
> -	{ divert6name, DIVERT6CTL_MAXID },
> +	{ 0, 0 },
>  };
>  
>  /*
> Index: usr.bin/netstat/inet6.c
> ===================================================================
> RCS file: /data/mirror/openbsd/cvs/src/usr.bin/netstat/inet6.c,v
> diff -u -p -r1.58 inet6.c
> --- usr.bin/netstat/inet6.c	4 Jun 2025 12:37:00 -0000	1.58
> +++ usr.bin/netstat/inet6.c	18 Jun 2025 21:16:44 -0000
> @@ -43,11 +43,9 @@
>  #include <netinet/icmp6.h>
>  #include <netinet/ip.h>
>  #include <netinet/ip_var.h>
> -#include <netinet/ip_divert.h>
>  #include <netinet6/ip6_var.h>
>  #include <netinet6/in6_var.h>
>  #include <netinet6/raw_ip6.h>
> -#include <netinet6/ip6_divert.h>
>  
>  #include <arpa/inet.h>
>  #include <netdb.h>
> @@ -863,37 +861,6 @@ rip6_stats(char *name)
>  		printf("\t%llu delivered\n", (unsigned long long)delivered);
>  	p(rip6s_opackets, "\t%llu datagram%s output\n");
>  #undef p
> -}
> -
> -/*
> - * Dump divert6 statistics structure.
> - */
> -void
> -div6_stats(char *name)
> -{
> -	struct divstat div6stat;
> -	int mib[] = { CTL_NET, PF_INET6, IPPROTO_DIVERT, DIVERT6CTL_STATS };
> -	size_t len = sizeof(div6stat);
> -
> -	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]),
> -	    &div6stat, &len, NULL, 0) == -1) {
> -		if (errno != ENOPROTOOPT)
> -			warn("%s", name);
> -		return;
> -	}
> -
> -	printf("%s:\n", name);
> -#define p(f, m) if (div6stat.f || sflag <= 1) \
> -    printf(m, div6stat.f, plural(div6stat.f))
> -#define p1(f, m) if (div6stat.f || sflag <= 1) \
> -    printf(m, div6stat.f)
> -	p(divs_ipackets, "\t%lu total packet%s received\n");
> -	p1(divs_noport, "\t%lu dropped due to no socket\n");
> -	p1(divs_fullsock, "\t%lu dropped due to full socket buffers\n");
> -	p(divs_opackets, "\t%lu packet%s output\n");
> -	p1(divs_errors, "\t%lu errors\n");
> -#undef p
> -#undef p1
>  }
>  
>  /*
> Index: usr.bin/netstat/main.c
> ===================================================================
> RCS file: /data/mirror/openbsd/cvs/src/usr.bin/netstat/main.c,v
> diff -u -p -r1.124 main.c
> --- usr.bin/netstat/main.c	23 Dec 2022 07:18:46 -0000	1.124
> +++ usr.bin/netstat/main.c	18 Jun 2025 20:49:23 -0000
> @@ -89,7 +89,6 @@ struct protox {
>  
>  struct protox ip6protox[] = {
>  	{ ip6_stats,	"ip6", IPPROTO_IPV6 },
> -	{ div6_stats,	"divert6", IPPROTO_DIVERT },
>  	{ icmp6_stats,	"icmp6", 0 },
>  	{ rip6_stats,	"rip6", 0 },
>  	{ NULL,		NULL, 0 }
> Index: usr.bin/netstat/netstat.h
> ===================================================================
> RCS file: /data/mirror/openbsd/cvs/src/usr.bin/netstat/netstat.h,v
> diff -u -p -r1.78 netstat.h
> --- usr.bin/netstat/netstat.h	18 May 2024 07:10:16 -0000	1.78
> +++ usr.bin/netstat/netstat.h	18 Jun 2025 20:49:39 -0000
> @@ -112,7 +112,6 @@ void	rdomainpr(void);
>  
>  void	ip6_stats(char *);
>  void	icmp6_stats(char *);
> -void	div6_stats(char *);
>  void	rip6_stats(char *);
>  void	mroute6pr(void);
>  void	mrt6_stats(void);
>