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