1 /*
2 * $FreeBSD$
3 *
4 * library functions for userland testing of dummynet schedulers
5 */
6
7 #include "dn_test.h"
8
9 void
m_freem(struct mbuf * m)10 m_freem(struct mbuf *m)
11 {
12 printf("free %p\n", m);
13 }
14
15 int
dn_sched_modevent(module_t mod,int cmd,void * arg)16 dn_sched_modevent(module_t mod, int cmd, void *arg)
17 {
18 return 0;
19 }
20
21 void
dn_free_pkts(struct mbuf * m)22 dn_free_pkts(struct mbuf *m)
23 {
24 struct mbuf *x;
25 while ( (x = m) ) {
26 m = m->m_nextpkt;
27 m_freem(x);
28 }
29 }
30
31 int
dn_delete_queue(void * _q,void * do_free)32 dn_delete_queue(void *_q, void *do_free)
33 {
34 struct dn_queue *q = _q;
35 if (q->mq.head)
36 dn_free_pkts(q->mq.head);
37 free(q);
38 return 0;
39 }
40
41 /*
42 * This is a simplified function for testing purposes, which does
43 * not implement statistics or random loss.
44 * Enqueue a packet in q, subject to space and queue management policy
45 * (whose parameters are in q->fs).
46 * Update stats for the queue and the scheduler.
47 * Return 0 on success, 1 on drop. The packet is consumed anyways.
48 */
49 int
dn_enqueue(struct dn_queue * q,struct mbuf * m,int drop)50 dn_enqueue(struct dn_queue *q, struct mbuf* m, int drop)
51 {
52 if (drop)
53 goto drop;
54 if (q->ni.length >= 200)
55 goto drop;
56 mq_append(&q->mq, m);
57 q->ni.length++;
58 q->ni.tot_bytes += m->m_pkthdr.len;
59 return 0;
60
61 drop:
62 q->ni.drops++;
63 return 1;
64 }
65
66 int
ipdn_bound_var(int * v,int dflt,int lo,int hi,const char * msg)67 ipdn_bound_var(int *v, int dflt, int lo, int hi, const char *msg)
68 {
69 if (*v < lo) {
70 *v = dflt;
71 } else if (*v > hi) {
72 *v = hi;
73 }
74 return *v;
75 }
76
77 #ifndef __FreeBSD__
78 int
fls(int mask)79 fls(int mask)
80 {
81 int bit;
82
83 if (mask == 0)
84 return (0);
85 for (bit = 1; mask != 1; bit++)
86 mask = (unsigned int)mask >> 1;
87 return (bit);
88 }
89 #endif
90