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