Module Name:    src
Committed By:   ozaki-r
Date:           Wed Jul 21 06:33:30 UTC 2021

Modified Files:
        src/sys/altq: altq_cbq.c altq_cbq.h altq_rmclass.c altq_rmclass.h

Log Message:
altq, cbq: convert ns_per_byte to ps_per_byte

Also the type of variables of it is changed to u_long from int.

This change provides fine-grain resolution of bandwidth.  For example
750 Mbps was treated as 800 Mbps internally because bandwidth was
represented as nanoseconds per byte.  Converting the representation
to picoseconds per byte enables to treat 750 Mbps as-is.

PR kern/56319


To generate a diff of this commit:
cvs rdiff -u -r1.34 -r1.35 src/sys/altq/altq_cbq.c
cvs rdiff -u -r1.8 -r1.9 src/sys/altq/altq_cbq.h
cvs rdiff -u -r1.24 -r1.25 src/sys/altq/altq_rmclass.c
cvs rdiff -u -r1.9 -r1.10 src/sys/altq/altq_rmclass.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/altq/altq_cbq.c
diff -u src/sys/altq/altq_cbq.c:1.34 src/sys/altq/altq_cbq.c:1.35
--- src/sys/altq/altq_cbq.c:1.34	Wed Jul 14 08:31:15 2021
+++ src/sys/altq/altq_cbq.c	Wed Jul 21 06:33:30 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: altq_cbq.c,v 1.34 2021/07/14 08:31:15 ozaki-r Exp $	*/
+/*	$NetBSD: altq_cbq.c,v 1.35 2021/07/21 06:33:30 ozaki-r Exp $	*/
 /*	$KAME: altq_cbq.c,v 1.21 2005/04/13 03:44:24 suz Exp $	*/
 
 /*
@@ -32,7 +32,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: altq_cbq.c,v 1.34 2021/07/14 08:31:15 ozaki-r Exp $");
+__KERNEL_RCSID(0, "$NetBSD: altq_cbq.c,v 1.35 2021/07/21 06:33:30 ozaki-r Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_altq.h"
@@ -226,7 +226,7 @@ get_class_stats(class_stats_t *statsp, s
 	statsp->minidle		= cl->minidle_;
 	statsp->offtime		= cl->offtime_;
 	statsp->qmax		= qlimit(cl->q_);
-	statsp->ns_per_byte	= cl->ns_per_byte_;
+	statsp->ps_per_byte	= cl->ps_per_byte_;
 	statsp->wrr_allot	= cl->w_allotment_;
 	statsp->qcnt		= qlen(cl->q_);
 	statsp->avgidle		= cl->avgidle_;
@@ -384,7 +384,7 @@ cbq_add_queue(struct pf_altq *a)
 	 */
 	if ((opts->flags & CBQCLF_ROOTCLASS) != 0) {
 		error = rmc_init(cbqp->ifnp.ifq_, &cbqp->ifnp,
-		    opts->ns_per_byte, cbqrestart, a->qlimit, RM_MAXQUEUED,
+		    opts->ps_per_byte, cbqrestart, a->qlimit, RM_MAXQUEUED,
 		    opts->maxidle, opts->minidle, opts->offtime,
 		    opts->flags);
 		if (error != 0)
@@ -392,7 +392,7 @@ cbq_add_queue(struct pf_altq *a)
 		cl = cbqp->ifnp.root_;
 	} else {
 		cl = rmc_newclass(a->priority,
-				  &cbqp->ifnp, opts->ns_per_byte,
+				  &cbqp->ifnp, opts->ps_per_byte,
 				  rmc_delay_action, a->qlimit, parent, borrow,
 				  opts->maxidle, opts->minidle, opts->offtime,
 				  opts->pktsize, opts->flags);
@@ -681,7 +681,7 @@ cbq_modify_class(struct cbq_modify_class
 	if ((cl = clh_to_clp(cbqp, acp->cbq_class_handle)) == NULL)
 		return (EINVAL);
 
-	if (rmc_modclass(cl, acp->cbq_class.nano_sec_per_byte,
+	if (rmc_modclass(cl, acp->cbq_class.pico_sec_per_byte,
 			 acp->cbq_class.maxq, acp->cbq_class.maxidle,
 			 acp->cbq_class.minidle, acp->cbq_class.offtime,
 			 acp->cbq_class.pktsize) < 0)
@@ -724,7 +724,7 @@ cbq_class_create(cbq_state_t *cbqp, stru
 	 */
 	if ((spec->flags & CBQCLF_ROOTCLASS) != 0) {
 		error = rmc_init(cbqp->ifnp.ifq_, &cbqp->ifnp,
-		    spec->nano_sec_per_byte, cbqrestart, spec->maxq,
+		    spec->pico_sec_per_byte, cbqrestart, spec->maxq,
 		    RM_MAXQUEUED, spec->maxidle, spec->minidle, spec->offtime,
 		    spec->flags);
 		if (error)
@@ -732,7 +732,7 @@ cbq_class_create(cbq_state_t *cbqp, stru
 		cl = cbqp->ifnp.root_;
 	} else {
 		cl = rmc_newclass(spec->priority,
-				  &cbqp->ifnp, spec->nano_sec_per_byte,
+				  &cbqp->ifnp, spec->pico_sec_per_byte,
 				  rmc_delay_action, spec->maxq, parent, borrow,
 				  spec->maxidle, spec->minidle, spec->offtime,
 				  spec->pktsize, spec->flags);

Index: src/sys/altq/altq_cbq.h
diff -u src/sys/altq/altq_cbq.h:1.8 src/sys/altq/altq_cbq.h:1.9
--- src/sys/altq/altq_cbq.h:1.8	Thu Oct 12 19:59:08 2006
+++ src/sys/altq/altq_cbq.h	Wed Jul 21 06:33:30 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: altq_cbq.h,v 1.8 2006/10/12 19:59:08 peter Exp $	*/
+/*	$NetBSD: altq_cbq.h,v 1.9 2021/07/21 06:33:30 ozaki-r Exp $	*/
 /*	$KAME: altq_cbq.h,v 1.12 2003/10/03 05:05:15 kjc Exp $	*/
 
 /*
@@ -85,7 +85,7 @@ typedef struct _cbq_class_stats_ {
 	int		minidle;
 	int		offtime;
 	int		qmax;
-	int		ns_per_byte;
+	u_long		ps_per_byte;
 	int		wrr_allot;
 
 	int		qcnt;		/* # packets in queue */
@@ -112,7 +112,7 @@ struct cbq_interface {
 
 typedef struct cbq_class_spec {
 	u_int		priority;
-	u_int		nano_sec_per_byte;
+	u_long		pico_sec_per_byte;
 	u_int		maxq;
 	u_int		maxidle;
 	int		minidle;

Index: src/sys/altq/altq_rmclass.c
diff -u src/sys/altq/altq_rmclass.c:1.24 src/sys/altq/altq_rmclass.c:1.25
--- src/sys/altq/altq_rmclass.c:1.24	Tue Jul 13 08:23:39 2021
+++ src/sys/altq/altq_rmclass.c	Wed Jul 21 06:33:30 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: altq_rmclass.c,v 1.24 2021/07/13 08:23:39 ozaki-r Exp $	*/
+/*	$NetBSD: altq_rmclass.c,v 1.25 2021/07/21 06:33:30 ozaki-r Exp $	*/
 /*	$KAME: altq_rmclass.c,v 1.19 2005/04/13 03:44:25 suz Exp $	*/
 
 /*
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: altq_rmclass.c,v 1.24 2021/07/13 08:23:39 ozaki-r Exp $");
+__KERNEL_RCSID(0, "$NetBSD: altq_rmclass.c,v 1.25 2021/07/21 06:33:30 ozaki-r Exp $");
 
 /* #ident "@(#)rm_class.c  1.48     97/12/05 SMI" */
 
@@ -80,6 +80,8 @@ __KERNEL_RCSID(0, "$NetBSD: altq_rmclass
 
 #define	reset_cutoff(ifd)	{ ifd->cutoff_ = RM_MAXDEPTH; }
 
+#define	PSEC_TO_NSEC(t)	((t) / 1000)
+
 /*
  * Local routines.
  */
@@ -192,7 +194,7 @@ static void	rmc_root_overlimit(struct rm
  * 	offtime = offtime * (8.0 / nsecPerByte);
  */
 struct rm_class *
-rmc_newclass(int pri, struct rm_ifdat *ifd, u_int nsecPerByte,
+rmc_newclass(int pri, struct rm_ifdat *ifd, u_long psecPerByte,
     void (*action)(rm_class_t *, rm_class_t *), int maxq,
     struct rm_class *parent, struct rm_class *borrow, u_int maxidle,
     int minidle, u_int offtime, int pktsize, int flags)
@@ -240,10 +242,10 @@ rmc_newclass(int pri, struct rm_ifdat *i
 	cl->leaf_ = 1;
 	cl->ifdat_ = ifd;
 	cl->pri_ = pri;
-	cl->allotment_ = RM_NS_PER_SEC / nsecPerByte; /* Bytes per sec */
+	cl->allotment_ = (u_int)(RM_PS_PER_SEC / psecPerByte); /* Bytes per sec */
 	cl->depth_ = 0;
 	cl->qthresh_ = 0;
-	cl->ns_per_byte_ = nsecPerByte;
+	cl->ps_per_byte_ = psecPerByte;
 
 	qlimit(cl->q_) = maxq;
 	qtype(cl->q_) = Q_DROPHEAD;
@@ -251,18 +253,18 @@ rmc_newclass(int pri, struct rm_ifdat *i
 	cl->flags_ = flags;
 
 #if 1 /* minidle is also scaled in ALTQ */
-	cl->minidle_ = (minidle * (int)nsecPerByte) / 8;
+	cl->minidle_ = (minidle * (int)PSEC_TO_NSEC(psecPerByte)) / 8;
 	if (cl->minidle_ > 0)
 		cl->minidle_ = 0;
 #else
 	cl->minidle_ = minidle;
 #endif
-	cl->maxidle_ = (maxidle * nsecPerByte) / 8;
+	cl->maxidle_ = (maxidle * PSEC_TO_NSEC(psecPerByte)) / 8;
 	if (cl->maxidle_ == 0)
 		cl->maxidle_ = 1;
 #if 1 /* offtime is also scaled in ALTQ */
 	cl->avgidle_ = cl->maxidle_;
-	cl->offtime_ = ((offtime * nsecPerByte) / 8) >> RM_FILTER_GAIN;
+	cl->offtime_ = ((offtime * PSEC_TO_NSEC(psecPerByte)) / 8) >> RM_FILTER_GAIN;
 	if (cl->offtime_ == 0)
 		cl->offtime_ = 1;
 #else
@@ -345,7 +347,7 @@ rmc_newclass(int pri, struct rm_ifdat *i
 }
 
 int
-rmc_modclass(struct rm_class *cl, u_int nsecPerByte, int maxq, u_int maxidle,
+rmc_modclass(struct rm_class *cl, u_long psecPerByte, int maxq, u_int maxidle,
     int minidle, u_int offtime, int pktsize)
 {
 	struct rm_ifdat	*ifd;
@@ -356,25 +358,25 @@ rmc_modclass(struct rm_class *cl, u_int 
 	old_allotment = cl->allotment_;
 
 	s = splnet();
-	cl->allotment_ = RM_NS_PER_SEC / nsecPerByte; /* Bytes per sec */
+	cl->allotment_ = (u_int)(RM_PS_PER_SEC / psecPerByte); /* Bytes per sec */
 	cl->qthresh_ = 0;
-	cl->ns_per_byte_ = nsecPerByte;
+	cl->ps_per_byte_ = psecPerByte;
 
 	qlimit(cl->q_) = maxq;
 
 #if 1 /* minidle is also scaled in ALTQ */
-	cl->minidle_ = (minidle * nsecPerByte) / 8;
+	cl->minidle_ = (minidle * PSEC_TO_NSEC(psecPerByte)) / 8;
 	if (cl->minidle_ > 0)
 		cl->minidle_ = 0;
 #else
 	cl->minidle_ = minidle;
 #endif
-	cl->maxidle_ = (maxidle * nsecPerByte) / 8;
+	cl->maxidle_ = (maxidle * PSEC_TO_NSEC(psecPerByte)) / 8;
 	if (cl->maxidle_ == 0)
 		cl->maxidle_ = 1;
 #if 1 /* offtime is also scaled in ALTQ */
 	cl->avgidle_ = cl->maxidle_;
-	cl->offtime_ = ((offtime * nsecPerByte) / 8) >> RM_FILTER_GAIN;
+	cl->offtime_ = ((offtime * PSEC_TO_NSEC(psecPerByte)) / 8) >> RM_FILTER_GAIN;
 	if (cl->offtime_ == 0)
 		cl->offtime_ = 1;
 #else
@@ -659,7 +661,7 @@ rmc_delete_class(struct rm_ifdat *ifd, s
  */
 
 int
-rmc_init(struct ifaltq *ifq, struct rm_ifdat *ifd, u_int nsecPerByte,
+rmc_init(struct ifaltq *ifq, struct rm_ifdat *ifd, u_long psecPerByte,
     void (*restart)(struct ifaltq *), int maxq, int maxqueued, u_int maxidle,
     int minidle, u_int offtime, int flags)
 {
@@ -681,13 +683,13 @@ rmc_init(struct ifaltq *ifq, struct rm_i
 	ifd->ifq_ = ifq;
 	ifd->restart = restart;
 	ifd->maxqueued_ = maxqueued;
-	ifd->ns_per_byte_ = nsecPerByte;
+	ifd->ps_per_byte_ = psecPerByte;
 	ifd->maxpkt_ = mtu;
 	ifd->wrr_ = (flags & RMCF_WRR) ? 1 : 0;
 	ifd->efficient_ = (flags & RMCF_EFFICIENT) ? 1 : 0;
 #if 1
-	ifd->maxiftime_ = mtu * nsecPerByte / 1000 * 16;
-	if (mtu * nsecPerByte > 10 * 1000000)
+	ifd->maxiftime_ = mtu * psecPerByte / 1000 / 1000 * 16;
+	if ((long)mtu * psecPerByte > (long)10 * 1000000000)
 		ifd->maxiftime_ /= 4;
 #endif
 
@@ -720,7 +722,7 @@ rmc_init(struct ifaltq *ifq, struct rm_i
 	 * Create the root class of the link-sharing structure.
 	 */
 	if ((ifd->root_ = rmc_newclass(0, ifd,
-				       nsecPerByte,
+				       psecPerByte,
 				       rmc_root_overlimit, maxq, 0, 0,
 				       maxidle, minidle, offtime,
 				       0, 0)) == NULL) {
@@ -1246,11 +1248,14 @@ rmc_dequeue_next(struct rm_ifdat *ifd, i
  * (on pentium, mul takes 9 cycles but div takes 46!)
  */
 #define	NSEC_TO_USEC(t)	(((t) >> 10) + ((t) >> 16) + ((t) >> 17))
+/* Don't worry.  Recent compilers don't use div. */
+#define	PSEC_TO_USEC(t)	((t) / 1000 / 1000)
 void
 rmc_update_class_util(struct rm_ifdat *ifd)
 {
 	int		 idle, avgidle, pktlen;
-	int		 pkt_time, tidle;
+	u_long		 pkt_time;
+	int		 tidle;
 	rm_class_t	*cl, *cl0, *borrowed;
 	rm_class_t	*borrows;
 	struct timeval	*nowp;
@@ -1281,8 +1286,8 @@ rmc_update_class_util(struct rm_ifdat *i
 	nowp = &ifd->now_[ifd->qo_];
 	/* get pkt_time (for link) in usec */
 #if 1  /* use approximation */
-	pkt_time = ifd->curlen_[ifd->qo_] * ifd->ns_per_byte_;
-	pkt_time = NSEC_TO_USEC(pkt_time);
+	pkt_time = (u_long)ifd->curlen_[ifd->qo_] * ifd->ps_per_byte_;
+	pkt_time = PSEC_TO_USEC(pkt_time);
 #else
 	pkt_time = ifd->curlen_[ifd->qo_] * ifd->ns_per_byte_ / 1000;
 #endif
@@ -1324,8 +1329,8 @@ rmc_update_class_util(struct rm_ifdat *i
 
 		/* get pkt_time (for class) in usec */
 #if 1  /* use approximation */
-		pkt_time = pktlen * cl->ns_per_byte_;
-		pkt_time = NSEC_TO_USEC(pkt_time);
+		pkt_time = (u_long)pktlen * cl->ps_per_byte_;
+		pkt_time = PSEC_TO_USEC(pkt_time);
 #else
 		pkt_time = pktlen * cl->ns_per_byte_ / 1000;
 #endif

Index: src/sys/altq/altq_rmclass.h
diff -u src/sys/altq/altq_rmclass.h:1.9 src/sys/altq/altq_rmclass.h:1.10
--- src/sys/altq/altq_rmclass.h:1.9	Tue Jul 13 08:04:31 2021
+++ src/sys/altq/altq_rmclass.h	Wed Jul 21 06:33:30 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: altq_rmclass.h,v 1.9 2021/07/13 08:04:31 ozaki-r Exp $	*/
+/*	$NetBSD: altq_rmclass.h,v 1.10 2021/07/21 06:33:30 ozaki-r Exp $	*/
 /*	$KAME: altq_rmclass.h,v 1.10 2003/08/20 23:30:23 itojun Exp $	*/
 
 /*
@@ -120,6 +120,7 @@ struct red;
 #define	RM_POWER	(1 << RM_FILTER_GAIN)
 #define	RM_MAXDEPTH	32
 #define	RM_NS_PER_SEC	(1000000000)
+#define	RM_PS_PER_SEC	(1000000000000)
 
 typedef struct _rm_class_stats_ {
 	u_int		handle;
@@ -141,7 +142,7 @@ struct rm_class {
 	rm_ifdat_t	*ifdat_;
 	int		pri_;		/* Class priority. */
 	int		depth_;		/* Class depth */
-	u_int		ns_per_byte_;	/* NanoSeconds per byte. */
+	u_long		ps_per_byte_;	/* PicoSeconds per byte. */
 	u_int		maxrate_;	/* Bytes per second for this class. */
 	u_int		allotment_;	/* Fraction of link bandwidth. */
 	u_int		w_allotment_;	/* Weighted allotment for WRR */
@@ -185,7 +186,7 @@ struct rm_ifdat {
 	int		queued_;	/* # pkts queued downstream */
 	int		efficient_;	/* Link Efficency bit */
 	int		wrr_;		/* Enable Weighted Round-Robin */
-	u_long		ns_per_byte_;	/* Link byte speed. */
+	u_long		ps_per_byte_;	/* Link byte speed. */
 	int		maxqueued_;	/* Max packets to queue */
 	int		maxpkt_;	/* Max packet size. */
 	int		qi_;		/* In/out pointers for downstream */
@@ -241,14 +242,14 @@ struct rm_ifdat {
 
 #define	is_a_parent_class(cl)	((cl)->children_ != NULL)
 
-extern rm_class_t *rmc_newclass(int, struct rm_ifdat *, u_int,
+extern rm_class_t *rmc_newclass(int, struct rm_ifdat *, u_long,
 				void (*)(struct rm_class *, struct rm_class *),
 				int, struct rm_class *, struct rm_class *,
 				u_int, int, u_int, int, int);
 extern void	rmc_delete_class(struct rm_ifdat *, struct rm_class *);
-extern int 	rmc_modclass(struct rm_class *, u_int, int,
+extern int 	rmc_modclass(struct rm_class *, u_long, int,
 			     u_int, int, u_int, int);
-extern int	rmc_init(struct ifaltq *, struct rm_ifdat *, u_int,
+extern int	rmc_init(struct ifaltq *, struct rm_ifdat *, u_long,
 			 void (*)(struct ifaltq *),
 			 int, int, u_int, int, u_int, int);
 extern int	rmc_queue_packet(struct rm_class *, mbuf_t *);

Reply via email to