xref: /freebsd/sys/netpfil/ipfw/test/test_dn_sched.c (revision 1cdc5f0b8794e82f0cbf9dca07967ca2559ba331)
13b3a8eb9SGleb Smirnoff /*
23b3a8eb9SGleb Smirnoff  * $FreeBSD$
33b3a8eb9SGleb Smirnoff  *
43b3a8eb9SGleb Smirnoff  * library functions for userland testing of dummynet schedulers
53b3a8eb9SGleb Smirnoff  */
63b3a8eb9SGleb Smirnoff 
73b3a8eb9SGleb Smirnoff #include "dn_test.h"
83b3a8eb9SGleb Smirnoff 
93b3a8eb9SGleb Smirnoff void
103b3a8eb9SGleb Smirnoff m_freem(struct mbuf *m)
113b3a8eb9SGleb Smirnoff {
123b3a8eb9SGleb Smirnoff 	printf("free %p\n", m);
133b3a8eb9SGleb Smirnoff }
143b3a8eb9SGleb Smirnoff 
153b3a8eb9SGleb Smirnoff int
163b3a8eb9SGleb Smirnoff dn_sched_modevent(module_t mod, int cmd, void *arg)
173b3a8eb9SGleb Smirnoff {
1810d72ffcSLuigi Rizzo 	(void)mod;
1910d72ffcSLuigi Rizzo 	(void)cmd;
2010d72ffcSLuigi Rizzo 	(void)arg;
213b3a8eb9SGleb Smirnoff 	return 0;
223b3a8eb9SGleb Smirnoff }
233b3a8eb9SGleb Smirnoff 
243b3a8eb9SGleb Smirnoff void
253b3a8eb9SGleb Smirnoff dn_free_pkts(struct mbuf *m)
263b3a8eb9SGleb Smirnoff {
273b3a8eb9SGleb Smirnoff 	struct mbuf *x;
283b3a8eb9SGleb Smirnoff 	while ( (x = m) ) {
293b3a8eb9SGleb Smirnoff 		m = m->m_nextpkt;
303b3a8eb9SGleb Smirnoff 		m_freem(x);
313b3a8eb9SGleb Smirnoff 	}
323b3a8eb9SGleb Smirnoff }
333b3a8eb9SGleb Smirnoff 
343b3a8eb9SGleb Smirnoff int
353b3a8eb9SGleb Smirnoff dn_delete_queue(void *_q, void *do_free)
363b3a8eb9SGleb Smirnoff {
373b3a8eb9SGleb Smirnoff 	struct dn_queue *q = _q;
3810d72ffcSLuigi Rizzo 
3910d72ffcSLuigi Rizzo 	(void)do_free;
403b3a8eb9SGleb Smirnoff         if (q->mq.head)
413b3a8eb9SGleb Smirnoff                 dn_free_pkts(q->mq.head);
423b3a8eb9SGleb Smirnoff         free(q);
433b3a8eb9SGleb Smirnoff         return 0;
443b3a8eb9SGleb Smirnoff }
453b3a8eb9SGleb Smirnoff 
463b3a8eb9SGleb Smirnoff /*
473b3a8eb9SGleb Smirnoff  * This is a simplified function for testing purposes, which does
483b3a8eb9SGleb Smirnoff  * not implement statistics or random loss.
493b3a8eb9SGleb Smirnoff  * Enqueue a packet in q, subject to space and queue management policy
503b3a8eb9SGleb Smirnoff  * (whose parameters are in q->fs).
513b3a8eb9SGleb Smirnoff  * Update stats for the queue and the scheduler.
523b3a8eb9SGleb Smirnoff  * Return 0 on success, 1 on drop. The packet is consumed anyways.
533b3a8eb9SGleb Smirnoff  */
543b3a8eb9SGleb Smirnoff int
553b3a8eb9SGleb Smirnoff dn_enqueue(struct dn_queue *q, struct mbuf* m, int drop)
563b3a8eb9SGleb Smirnoff {
573b3a8eb9SGleb Smirnoff         if (drop)
583b3a8eb9SGleb Smirnoff                 goto drop;
593b3a8eb9SGleb Smirnoff         if (q->ni.length >= 200)
603b3a8eb9SGleb Smirnoff                 goto drop;
613b3a8eb9SGleb Smirnoff         mq_append(&q->mq, m);
623b3a8eb9SGleb Smirnoff         q->ni.length++;
633b3a8eb9SGleb Smirnoff         q->ni.tot_bytes += m->m_pkthdr.len;
64*1cdc5f0bSLuigi Rizzo         q->ni.tot_pkts++;
653b3a8eb9SGleb Smirnoff         return 0;
663b3a8eb9SGleb Smirnoff 
673b3a8eb9SGleb Smirnoff drop:
683b3a8eb9SGleb Smirnoff         q->ni.drops++;
693b3a8eb9SGleb Smirnoff         return 1;
703b3a8eb9SGleb Smirnoff }
713b3a8eb9SGleb Smirnoff 
723b3a8eb9SGleb Smirnoff int
733b3a8eb9SGleb Smirnoff ipdn_bound_var(int *v, int dflt, int lo, int hi, const char *msg)
743b3a8eb9SGleb Smirnoff {
7510d72ffcSLuigi Rizzo 	(void)msg;
763b3a8eb9SGleb Smirnoff         if (*v < lo) {
773b3a8eb9SGleb Smirnoff                 *v = dflt;
783b3a8eb9SGleb Smirnoff         } else if (*v > hi) {
793b3a8eb9SGleb Smirnoff                 *v = hi;
803b3a8eb9SGleb Smirnoff         }
813b3a8eb9SGleb Smirnoff         return *v;
823b3a8eb9SGleb Smirnoff }
833b3a8eb9SGleb Smirnoff 
843b3a8eb9SGleb Smirnoff #ifndef __FreeBSD__
853b3a8eb9SGleb Smirnoff int
863b3a8eb9SGleb Smirnoff fls(int mask)
873b3a8eb9SGleb Smirnoff {
883b3a8eb9SGleb Smirnoff 	int bit;
893b3a8eb9SGleb Smirnoff 
903b3a8eb9SGleb Smirnoff 	if (mask == 0)
913b3a8eb9SGleb Smirnoff 		return (0);
923b3a8eb9SGleb Smirnoff 	for (bit = 1; mask != 1; bit++)
933b3a8eb9SGleb Smirnoff 		mask = (unsigned int)mask >> 1;
943b3a8eb9SGleb Smirnoff 	return (bit);
953b3a8eb9SGleb Smirnoff }
963b3a8eb9SGleb Smirnoff #endif
97