Index | Thread | Search

From:
Claudio Jeker <cjeker@diehard.n-r-g.com>
Subject:
Re: netstat multicast counter
To:
Alexander Bluhm <bluhm@openbsd.org>
Cc:
tech@openbsd.org
Date:
Thu, 16 Jul 2026 09:01:57 +0200

Download raw body.

Thread
On Thu, Jul 16, 2026 at 01:07:08AM +0200, Alexander Bluhm wrote:
> Hi,
> 
> I did not find a way to print the per interface multicast counter.
> netstat -i -g seems the natural way for these values.
> 
> $ netstat -I em0 -g
> Name    Mtu   Network     Address           Imcastpkts Omcastpkts
> em0     1500  <Link>      3c:97:0e:a7:09:b2       6136       4141
> em0     1500  fe80::%em0/ fe80::3e97:eff:fe       6136       4141
> em0     1500  10.188.17/2 10.188.17.17            6136       4141
> 
> $ netstat -I em0 -g -w1
>     em0 in             em0 out          total in           total out
> mcast pkts          mcast pkts      mcast pkts          mcast pkts
>       6140                4145          107002               58348
>          0                   1               5                   1
>          0                   1               2                   6
>          0                   0               0                   4
>          0                   0               1                   1
> 
> ok?

This is a lot of spaghetti code (not your fault it was already horrible
before).

I would put { } on all the if () else if () else constructions. Many of
them span multiple lines (even though they are single statements).

Manpage update is missing.

Apart from that OK claudio@
 
> bluhm
> 
> Index: usr.bin/netstat/if.c
> ===================================================================
> RCS file: /data/mirror/openbsd/cvs/src/usr.bin/netstat/if.c,v
> diff -u -p -r1.81 if.c
> --- usr.bin/netstat/if.c	8 Mar 2023 04:43:12 -0000	1.81
> +++ usr.bin/netstat/if.c	15 Jul 2026 23:01:47 -0000
> @@ -115,6 +115,8 @@ intpr(int interval, int repeatcount)
>  	    "Name", "Mtu", "Network", "Address");
>  	if (bflag)
>  		printf("%10.10s %10.10s", "Ibytes", "Obytes");
> +	else if (gflag)
> +		printf("%10.10s %10.10s", "Imcastpkts", "Omcastpkts");
>  	else {
>  		printf("%8.8s %5.5s %8.8s %5.5s %5.5s",
>  		    "Ipkts", if_errs->iname,
> @@ -155,14 +157,21 @@ intpr(int interval, int repeatcount)
>  			*cp = '\0';
>  
>  			if (qflag) {
> -				total = ifd->ifi_ibytes + ifd->ifi_obytes +
> -				    ifd->ifi_ipackets +
> -				    ifd->ifi_opackets +
> -				    ifd->ifi_collisions;
> -				total += if_errs->count(ifd->ifi_ierrors,
> -				    ifd->ifi_iqdrops);
> -				total += if_errs->count(ifd->ifi_oerrors,
> -				    ifd->ifi_oqdrops);
> +				if (bflag)
> +					total =
> +					    ifd->ifi_ibytes + ifd->ifi_obytes;
> +				else if (gflag)
> +					total =
> +					    ifd->ifi_imcasts + ifd->ifi_omcasts;
> +				else
> +					total =
> +					    ifd->ifi_ipackets +
> +					    if_errs->count(ifd->ifi_ierrors,
> +					    ifd->ifi_iqdrops) +
> +					    ifd->ifi_opackets +
> +					    if_errs->count(ifd->ifi_oerrors,
> +					    ifd->ifi_oqdrops) +
> +					    ifd->ifi_collisions;
>  				if (total == 0)
>  					continue;
>  			}
> @@ -289,7 +298,10 @@ hexprint:
>  		} else
>  			printf("%10llu %10llu",
>  			    ifd->ifi_ibytes, ifd->ifi_obytes);
> -	} else
> +	} else if (gflag)
> +		printf("%10llu %10llu",
> +		    ifd->ifi_imcasts, ifd->ifi_omcasts);
> +	else
>  		printf("%8llu %5llu %8llu %5llu %5llu",
>  		    ifd->ifi_ipackets,
>  		    if_errs->count(ifd->ifi_ierrors, ifd->ifi_iqdrops),
> @@ -303,10 +315,12 @@ struct	iftot {
>  	char	ift_name[IFNAMSIZ];	/* interface name */
>  	u_int64_t ift_ip;		/* input packets */
>  	u_int64_t ift_ib;		/* input bytes */
> +	u_int64_t ift_im;		/* input multicast */
>  	u_int64_t ift_ie;		/* input errors */
>  	u_int64_t ift_iq;		/* input qdrops */
>  	u_int64_t ift_op;		/* output packets */
>  	u_int64_t ift_ob;		/* output bytes */
> +	u_int64_t ift_om;		/* output multicast */
>  	u_int64_t ift_oe;		/* output errors */
>  	u_int64_t ift_oq;		/* output qdrops */
>  	u_int64_t ift_co;		/* collisions */
> @@ -339,7 +353,7 @@ sidewaysintpr(unsigned int interval, int
>  	signalled = 0;
>  	(void)alarm(interval);
>  banner:
> -	if (bflag)
> +	if (bflag || gflag)
>  		printf("%7.7s in %8.8s %6.6s out %5.5s",
>  		    ip_cur.ift_name, " ",
>  		    ip_cur.ift_name, " ");
> @@ -348,7 +362,7 @@ banner:
>  		    ip_cur.ift_name, " ",
>  		    ip_cur.ift_name, " ", " ");
>  
> -	if (bflag)
> +	if (bflag || gflag)
>  		printf("  %7.7s in %8.8s %6.6s out %5.5s",
>  		    "total", " ", "total", " ");
>  	else
> @@ -358,6 +372,9 @@ banner:
>  	if (bflag)
>  		printf("%10.10s %8.8s %10.10s %5.5s",
>  		    "bytes", " ", "bytes", " ");
> +	else if (gflag)
> +		printf("%10.10s %8.8s %10.10s %5.5s",
> +		    "mcast pkts", " ", "mcast pkts", " ");
>  	else
>  		printf("%8.8s %5.5s %8.8s %5.5s %5.5s",
>  		    "packets", if_errs->name,
> @@ -366,6 +383,9 @@ banner:
>  	if (bflag)
>  		printf("%10.10s %8.8s %10.10s %5.5s",
>  		    "bytes", " ", "bytes", " ");
> +	else if (gflag)
> +		printf("%10.10s %8.8s %10.10s %5.5s",
> +		    "mcast pkts", " ", "mcast pkts", " ");
>  	else
>  		printf("  %8.8s %5.5s %8.8s %5.5s %5.5s",
>  		    "packets", "errs", "packets", "errs", "colls");
> @@ -389,7 +409,11 @@ loop:
>  			printf("%10llu %8.8s %10llu %5.5s",
>  			    ip_cur.ift_ib - ip_old.ift_ib, " ",
>  			    ip_cur.ift_ob - ip_old.ift_ob, " ");
> -	} else
> +	} else if (gflag)
> +		printf("%10llu %8.8s %10llu %5.5s",
> +		    ip_cur.ift_im - ip_old.ift_im, " ",
> +		    ip_cur.ift_om - ip_old.ift_om, " ");
> +	else
>  		printf("%8llu %5llu %8llu %5llu %5llu",
>  		    ip_cur.ift_ip - ip_old.ift_ip,
>  		    if_errs->count(ip_cur.ift_ie - ip_old.ift_ie,
> @@ -411,7 +435,11 @@ loop:
>  			printf("%10llu %8.8s %10llu %5.5s",
>  			    sum_cur.ift_ib - sum_old.ift_ib, " ",
>  			    sum_cur.ift_ob - sum_old.ift_ob, " ");
> -	} else
> +	} else if (gflag)
> +		printf("%10llu %8.8s %10llu %5.5s",
> +		    sum_cur.ift_im - sum_old.ift_im, " ",
> +		    sum_cur.ift_om - sum_old.ift_om, " ");
> +	else
>  		printf("%8llu %5llu %8llu %5llu %5llu",
>  		    sum_cur.ift_ip - sum_old.ift_ip,
>  		    if_errs->count(sum_cur.ift_ie - sum_old.ift_ie,
> @@ -556,10 +584,12 @@ fetchifs(void)
>  				    sizeof(ip_cur.ift_name));
>  				ip_cur.ift_ip = ifd->ifi_ipackets;
>  				ip_cur.ift_ib = ifd->ifi_ibytes;
> +				ip_cur.ift_im = ifd->ifi_imcasts;
>  				ip_cur.ift_ie = ifd->ifi_ierrors;
>  				ip_cur.ift_iq = ifd->ifi_iqdrops;
>  				ip_cur.ift_op = ifd->ifi_opackets;
>  				ip_cur.ift_ob = ifd->ifi_obytes;
> +				ip_cur.ift_om = ifd->ifi_omcasts;
>  				ip_cur.ift_oe = ifd->ifi_oerrors;
>  				ip_cur.ift_oq = ifd->ifi_oqdrops;
>  				ip_cur.ift_co = ifd->ifi_collisions;
> @@ -567,10 +597,12 @@ fetchifs(void)
>  
>  			sum_cur.ift_ip += ifd->ifi_ipackets;
>  			sum_cur.ift_ib += ifd->ifi_ibytes;
> +			sum_cur.ift_im += ifd->ifi_imcasts;
>  			sum_cur.ift_ie += ifd->ifi_ierrors;
>  			sum_cur.ift_iq += ifd->ifi_iqdrops;
>  			sum_cur.ift_op += ifd->ifi_opackets;
>  			sum_cur.ift_ob += ifd->ifi_obytes;
> +			sum_cur.ift_om += ifd->ifi_omcasts;
>  			sum_cur.ift_oe += ifd->ifi_oerrors;
>  			sum_cur.ift_oq += ifd->ifi_oqdrops;
>  			sum_cur.ift_co += ifd->ifi_collisions;
> @@ -582,10 +614,12 @@ fetchifs(void)
>  		    sizeof(ip_cur.ift_name));
>  		ip_cur.ift_ip = ifd->ifi_ipackets;
>  		ip_cur.ift_ib = ifd->ifi_ibytes;
> +		ip_cur.ift_im = ifd->ifi_imcasts;
>  		ip_cur.ift_ie = ifd->ifi_ierrors;
>  		ip_cur.ift_iq = ifd->ifi_iqdrops;
>  		ip_cur.ift_op = ifd->ifi_opackets;
>  		ip_cur.ift_ob = ifd->ifi_obytes;
> +		ip_cur.ift_om = ifd->ifi_omcasts;
>  		ip_cur.ift_oe = ifd->ifi_oerrors;
>  		ip_cur.ift_oq = ifd->ifi_oqdrops;
>  		ip_cur.ift_co = ifd->ifi_collisions;
> 

-- 
:wq Claudio