+
+ pipe_nr &= 0xffff ;
+
+ lck_mtx_lock(dn_mutex);
+
+ /* make all time measurements in milliseconds (ms) -
+ * here we convert secs and usecs to msecs (just divide the
+ * usecs and take the closest whole number).
+ */
+ microuptime(&tv);
+ curr_time = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
+
+ /*
+ * This is a dummynet rule, so we expect an O_PIPE or O_QUEUE rule.
+ */
+ if (is_pipe) {
+ pipe = locate_pipe(pipe_nr);
+ if (pipe != NULL)
+ fs = &(pipe->fs);
+ } else
+ fs = locate_flowset(pipe_nr);
+
+
+ if (fs == NULL){
+ goto dropit ; /* this queue/pipe does not exist! */
+ }
+ pipe = fs->pipe ;
+ if (pipe == NULL) { /* must be a queue, try find a matching pipe */
+ pipe = locate_pipe(fs->parent_nr);
+
+ if (pipe != NULL)
+ fs->pipe = pipe ;
+ else {
+ printf("dummynet: no pipe %d for queue %d, drop pkt\n",
+ fs->parent_nr, fs->fs_nr);
+ goto dropit ;
+ }
+ }
+ q = find_queue(fs, &(fwa->f_id));
+ if ( q == NULL )
+ goto dropit ; /* cannot allocate queue */
+ /*
+ * update statistics, then check reasons to drop pkt
+ */
+ q->tot_bytes += len ;
+ q->tot_pkts++ ;
+ if ( fs->plr && (MY_RANDOM < fs->plr) )
+ goto dropit ; /* random pkt drop */
+ if ( fs->flags_fs & DN_QSIZE_IS_BYTES) {
+ if (q->len_bytes > fs->qsize)
+ goto dropit ; /* queue size overflow */
+ } else {
+ if (q->len >= fs->qsize)
+ goto dropit ; /* queue count overflow */
+ }
+ if ( fs->flags_fs & DN_IS_RED && red_drops(fs, q, len) )
+ goto dropit ;
+
+ /* XXX expensive to zero, see if we can remove it*/
+ mtag = m_tag_create(KERNEL_MODULE_TAG_ID, KERNEL_TAG_TYPE_DUMMYNET,
+ sizeof(struct dn_pkt_tag), M_NOWAIT, m);
+ if ( mtag == NULL )
+ goto dropit ; /* cannot allocate packet header */
+ m_tag_prepend(m, mtag); /* attach to mbuf chain */
+
+ pkt = (struct dn_pkt_tag *)(mtag+1);
+ bzero(pkt, sizeof(struct dn_pkt_tag));
+ /* ok, i can handle the pkt now... */
+ /* build and enqueue packet + parameters */
+ pkt->rule = fwa->rule ;
+ pkt->dn_dir = dir ;
+
+ pkt->ifp = fwa->oif;
+ if (dir == DN_TO_IP_OUT) {
+ /*
+ * We need to copy *ro because for ICMP pkts (and maybe others)
+ * the caller passed a pointer into the stack; dst might also be
+ * a pointer into *ro so it needs to be updated.
+ */
+ pkt->ro = *(fwa->ro);
+ if (fwa->ro->ro_rt)
+ RT_ADDREF(fwa->ro->ro_rt);
+
+ if (fwa->dst == (struct sockaddr_in *)&fwa->ro->ro_dst) /* dst points into ro */
+ fwa->dst = (struct sockaddr_in *)&(pkt->ro.ro_dst) ;
+
+ bcopy (fwa->dst, &pkt->dn_dst, sizeof(pkt->dn_dst));
+ pkt->flags = fwa->flags;
+ if (fwa->ipoa != NULL)
+ pkt->ipoa = *(fwa->ipoa);
+ }
+ if (q->head == NULL)
+ q->head = m;
+ else
+ q->tail->m_nextpkt = m;
+ q->tail = m;
+ q->len++;
+ q->len_bytes += len ;
+
+ if ( q->head != m ) /* flow was not idle, we are done */
+ goto done;
+ /*
+ * If we reach this point the flow was previously idle, so we need
+ * to schedule it. This involves different actions for fixed-rate or
+ * WF2Q queues.
+ */
+ if (is_pipe) {
+ /*
+ * Fixed-rate queue: just insert into the ready_heap.
+ */
+ dn_key t = 0 ;
+ if (pipe->bandwidth)
+ t = SET_TICKS(m, q, pipe);
+ q->sched_time = curr_time ;
+ if (t == 0) /* must process it now */
+ ready_event( q , &head, &tail );
+ else
+ heap_insert(&ready_heap, curr_time + t , q );
+ } else {
+ /*
+ * WF2Q. First, compute start time S: if the flow was idle (S=F+1)
+ * set S to the virtual time V for the controlling pipe, and update
+ * the sum of weights for the pipe; otherwise, remove flow from
+ * idle_heap and set S to max(F,V).
+ * Second, compute finish time F = S + len/weight.
+ * Third, if pipe was idle, update V=max(S, V).
+ * Fourth, count one more backlogged flow.
+ */
+ if (DN_KEY_GT(q->S, q->F)) { /* means timestamps are invalid */
+ q->S = pipe->V ;
+ pipe->sum += fs->weight ; /* add weight of new queue */
+ } else {
+ heap_extract(&(pipe->idle_heap), q);
+ q->S = MAX64(q->F, pipe->V ) ;
+ }
+ q->F = q->S + ( len<<MY_M )/(u_int64_t) fs->weight;
+
+ if (pipe->not_eligible_heap.elements == 0 &&
+ pipe->scheduler_heap.elements == 0)
+ pipe->V = MAX64 ( q->S, pipe->V );
+ fs->backlogged++ ;
+ /*
+ * Look at eligibility. A flow is not eligibile if S>V (when
+ * this happens, it means that there is some other flow already
+ * scheduled for the same pipe, so the scheduler_heap cannot be
+ * empty). If the flow is not eligible we just store it in the
+ * not_eligible_heap. Otherwise, we store in the scheduler_heap
+ * and possibly invoke ready_event_wfq() right now if there is
+ * leftover credit.
+ * Note that for all flows in scheduler_heap (SCH), S_i <= V,
+ * and for all flows in not_eligible_heap (NEH), S_i > V .
+ * So when we need to compute max( V, min(S_i) ) forall i in SCH+NEH,
+ * we only need to look into NEH.
+ */
+ if (DN_KEY_GT(q->S, pipe->V) ) { /* not eligible */
+ if (pipe->scheduler_heap.elements == 0)
+ printf("dummynet: ++ ouch! not eligible but empty scheduler!\n");
+ heap_insert(&(pipe->not_eligible_heap), q->S, q);
+ } else {
+ heap_insert(&(pipe->scheduler_heap), q->F, q);
+ if (pipe->numbytes >= 0) { /* pipe is idle */
+ if (pipe->scheduler_heap.elements != 1)
+ printf("dummynet: OUCH! pipe should have been idle!\n");
+ DPRINTF(("dummynet: waking up pipe %d at %d\n",
+ pipe->pipe_nr, (int)(q->F >> MY_M)));
+ pipe->sched_time = curr_time ;
+ ready_event_wfq(pipe, &head, &tail);
+ }
+ }
+ }
+done:
+ /* start the timer and set global if not already set */
+ if (!timer_enabled) {
+ ts.tv_sec = 0;
+ ts.tv_nsec = 1 * 1000000; // 1ms
+ timer_enabled = 1;
+ bsd_timeout(dummynet, NULL, &ts);
+ }
+
+ lck_mtx_unlock(dn_mutex);
+
+ if (head != NULL)
+ dummynet_send(head);
+
+ return 0;
+
+dropit:
+ if (q)
+ q->drops++ ;
+ lck_mtx_unlock(dn_mutex);
+ m_freem(m);
+ return ( (fs && (fs->flags_fs & DN_NOERROR)) ? 0 : ENOBUFS);
+}
+
+/*
+ * Below, the rtfree is only needed when (pkt->dn_dir == DN_TO_IP_OUT)
+ * Doing this would probably save us the initial bzero of dn_pkt
+ */
+#define DN_FREE_PKT(_m) do { \
+ struct m_tag *tag = m_tag_locate(m, KERNEL_MODULE_TAG_ID, KERNEL_TAG_TYPE_DUMMYNET, NULL); \
+ if (tag) { \
+ struct dn_pkt_tag *n = (struct dn_pkt_tag *)(tag+1); \
+ if (n->ro.ro_rt != NULL) { \
+ rtfree(n->ro.ro_rt); \
+ n->ro.ro_rt = NULL; \
+ } \
+ } \
+ m_tag_delete(_m, tag); \
+ m_freem(_m); \
+} while (0)
+
+/*
+ * Dispose all packets and flow_queues on a flow_set.
+ * If all=1, also remove red lookup table and other storage,
+ * including the descriptor itself.
+ * For the one in dn_pipe MUST also cleanup ready_heap...
+ */
+static void
+purge_flow_set(struct dn_flow_set *fs, int all)
+{
+ struct dn_flow_queue *q, *qn ;
+ int i ;
+
+ lck_mtx_assert(dn_mutex, LCK_MTX_ASSERT_OWNED);
+
+ for (i = 0 ; i <= fs->rq_size ; i++ ) {
+ for (q = fs->rq[i] ; q ; q = qn ) {
+ struct mbuf *m, *mnext;
+
+ mnext = q->head;
+ while ((m = mnext) != NULL) {
+ mnext = m->m_nextpkt;
+ DN_FREE_PKT(m);
+ }
+ qn = q->next ;
+ FREE(q, M_DUMMYNET);
+ }
+ fs->rq[i] = NULL ;
+ }
+ fs->rq_elements = 0 ;
+ if (all) {
+ /* RED - free lookup table */
+ if (fs->w_q_lookup)
+ FREE(fs->w_q_lookup, M_DUMMYNET);
+ if (fs->rq)
+ FREE(fs->rq, M_DUMMYNET);
+ /* if this fs is not part of a pipe, free it */
+ if (fs->pipe && fs != &(fs->pipe->fs) )
+ FREE(fs, M_DUMMYNET);
+ }
+}
+
+/*
+ * Dispose all packets queued on a pipe (not a flow_set).
+ * Also free all resources associated to a pipe, which is about
+ * to be deleted.
+ */
+static void
+purge_pipe(struct dn_pipe *pipe)
+{
+ struct mbuf *m, *mnext;
+
+ purge_flow_set( &(pipe->fs), 1 );
+
+ mnext = pipe->head;
+ while ((m = mnext) != NULL) {
+ mnext = m->m_nextpkt;
+ DN_FREE_PKT(m);
+ }
+
+ heap_free( &(pipe->scheduler_heap) );
+ heap_free( &(pipe->not_eligible_heap) );
+ heap_free( &(pipe->idle_heap) );
+}
+
+/*
+ * Delete all pipes and heaps returning memory. Must also
+ * remove references from all ipfw rules to all pipes.
+ */
+static void
+dummynet_flush(void)
+{
+ struct dn_pipe *pipe, *pipe1;
+ struct dn_flow_set *fs, *fs1;
+ int i;
+
+ lck_mtx_lock(dn_mutex);
+
+ /* remove all references to pipes ...*/
+ flush_pipe_ptrs(NULL);
+
+ /* Free heaps so we don't have unwanted events. */
+ heap_free(&ready_heap);
+ heap_free(&wfq_ready_heap);
+ heap_free(&extract_heap);
+
+ /*
+ * Now purge all queued pkts and delete all pipes.
+ *
+ * XXXGL: can we merge the for(;;) cycles into one or not?
+ */
+ for (i = 0; i < HASHSIZE; i++)
+ SLIST_FOREACH_SAFE(fs, &flowsethash[i], next, fs1) {
+ SLIST_REMOVE(&flowsethash[i], fs, dn_flow_set, next);
+ purge_flow_set(fs, 1);
+ }
+ for (i = 0; i < HASHSIZE; i++)
+ SLIST_FOREACH_SAFE(pipe, &pipehash[i], next, pipe1) {
+ SLIST_REMOVE(&pipehash[i], pipe, dn_pipe, next);
+ purge_pipe(pipe);
+ FREE(pipe, M_DUMMYNET);
+ }
+ lck_mtx_unlock(dn_mutex);
+}
+
+
+extern struct ip_fw *ip_fw_default_rule ;
+static void
+dn_rule_delete_fs(struct dn_flow_set *fs, void *r)
+{
+ int i ;
+ struct dn_flow_queue *q ;
+ struct mbuf *m ;
+
+ for (i = 0 ; i <= fs->rq_size ; i++) /* last one is ovflow */
+ for (q = fs->rq[i] ; q ; q = q->next )
+ for (m = q->head ; m ; m = m->m_nextpkt ) {
+ struct dn_pkt_tag *pkt = dn_tag_get(m) ;
+ if (pkt->rule == r)
+ pkt->rule = ip_fw_default_rule ;
+ }
+}
+/*
+ * when a firewall rule is deleted, scan all queues and remove the flow-id
+ * from packets matching this rule.
+ */
+void
+dn_rule_delete(void *r)
+{
+ struct dn_pipe *p ;
+ struct dn_flow_set *fs ;
+ struct dn_pkt_tag *pkt ;
+ struct mbuf *m ;
+ int i;
+
+ lck_mtx_lock(dn_mutex);
+
+ /*
+ * If the rule references a queue (dn_flow_set), then scan
+ * the flow set, otherwise scan pipes. Should do either, but doing
+ * both does not harm.
+ */
+ for (i = 0; i < HASHSIZE; i++)
+ SLIST_FOREACH(fs, &flowsethash[i], next)
+ dn_rule_delete_fs(fs, r);
+
+ for (i = 0; i < HASHSIZE; i++)
+ SLIST_FOREACH(p, &pipehash[i], next) {
+ fs = &(p->fs);
+ dn_rule_delete_fs(fs, r);
+ for (m = p->head ; m ; m = m->m_nextpkt ) {
+ pkt = dn_tag_get(m);
+ if (pkt->rule == r)
+ pkt->rule = ip_fw_default_rule;
+ }
+ }
+ lck_mtx_unlock(dn_mutex);
+}
+
+/*
+ * setup RED parameters
+ */
+static int
+config_red(struct dn_flow_set *p, struct dn_flow_set * x)
+{
+ int i;
+
+ x->w_q = p->w_q;
+ x->min_th = SCALE(p->min_th);
+ x->max_th = SCALE(p->max_th);
+ x->max_p = p->max_p;
+
+ x->c_1 = p->max_p / (p->max_th - p->min_th);
+ x->c_2 = SCALE_MUL(x->c_1, SCALE(p->min_th));
+ if (x->flags_fs & DN_IS_GENTLE_RED) {
+ x->c_3 = (SCALE(1) - p->max_p) / p->max_th;
+ x->c_4 = (SCALE(1) - 2 * p->max_p);
+ }
+
+ /* if the lookup table already exist, free and create it again */
+ if (x->w_q_lookup) {
+ FREE(x->w_q_lookup, M_DUMMYNET);
+ x->w_q_lookup = NULL ;
+ }
+ if (red_lookup_depth == 0) {
+ printf("\ndummynet: net.inet.ip.dummynet.red_lookup_depth must be > 0\n");
+ FREE(x, M_DUMMYNET);
+ return EINVAL;
+ }
+ x->lookup_depth = red_lookup_depth;
+ x->w_q_lookup = (u_int *) _MALLOC(x->lookup_depth * sizeof(int),
+ M_DUMMYNET, M_DONTWAIT);
+ if (x->w_q_lookup == NULL) {
+ printf("dummynet: sorry, cannot allocate red lookup table\n");
+ FREE(x, M_DUMMYNET);
+ return ENOSPC;
+ }
+
+ /* fill the lookup table with (1 - w_q)^x */
+ x->lookup_step = p->lookup_step ;
+ x->lookup_weight = p->lookup_weight ;
+ x->w_q_lookup[0] = SCALE(1) - x->w_q;
+ for (i = 1; i < x->lookup_depth; i++)
+ x->w_q_lookup[i] = SCALE_MUL(x->w_q_lookup[i - 1], x->lookup_weight);
+ if (red_avg_pkt_size < 1)
+ red_avg_pkt_size = 512 ;
+ x->avg_pkt_size = red_avg_pkt_size ;
+ if (red_max_pkt_size < 1)
+ red_max_pkt_size = 1500 ;
+ x->max_pkt_size = red_max_pkt_size ;
+ return 0 ;
+}
+
+static int
+alloc_hash(struct dn_flow_set *x, struct dn_flow_set *pfs)
+{
+ if (x->flags_fs & DN_HAVE_FLOW_MASK) { /* allocate some slots */
+ int l = pfs->rq_size;
+
+ if (l == 0)
+ l = dn_hash_size;
+ if (l < 4)
+ l = 4;
+ else if (l > DN_MAX_HASH_SIZE)
+ l = DN_MAX_HASH_SIZE;
+ x->rq_size = l;
+ } else /* one is enough for null mask */
+ x->rq_size = 1;
+ x->rq = _MALLOC((1 + x->rq_size) * sizeof(struct dn_flow_queue *),
+ M_DUMMYNET, M_DONTWAIT | M_ZERO);
+ if (x->rq == NULL) {
+ printf("dummynet: sorry, cannot allocate queue\n");
+ return ENOSPC;
+ }
+ x->rq_elements = 0;
+ return 0 ;
+}
+
+static void
+set_fs_parms(struct dn_flow_set *x, struct dn_flow_set *src)
+{
+ x->flags_fs = src->flags_fs;
+ x->qsize = src->qsize;
+ x->plr = src->plr;
+ x->flow_mask = src->flow_mask;
+ if (x->flags_fs & DN_QSIZE_IS_BYTES) {
+ if (x->qsize > 1024*1024)
+ x->qsize = 1024*1024 ;
+ } else {
+ if (x->qsize == 0)
+ x->qsize = 50;
+ if (x->qsize > 100)
+ x->qsize = 50;
+ }
+ /* configuring RED */
+ if ( x->flags_fs & DN_IS_RED )
+ config_red(src, x) ; /* XXX should check errors */
+}
+
+/*
+ * setup pipe or queue parameters.
+ */
+
+static int
+config_pipe(struct dn_pipe *p)
+{
+ int i, r;
+ struct dn_flow_set *pfs = &(p->fs);
+ struct dn_flow_queue *q;
+
+ /*
+ * The config program passes parameters as follows:
+ * bw = bits/second (0 means no limits),
+ * delay = ms, must be translated into ticks.
+ * qsize = slots/bytes
+ */
+ p->delay = ( p->delay * (hz*10) ) / 1000 ;
+ /* We need either a pipe number or a flow_set number */
+ if (p->pipe_nr == 0 && pfs->fs_nr == 0)
+ return EINVAL ;
+ if (p->pipe_nr != 0 && pfs->fs_nr != 0)
+ return EINVAL ;
+ if (p->pipe_nr != 0) { /* this is a pipe */
+ struct dn_pipe *x, *b;
+
+ lck_mtx_lock(dn_mutex);
+
+ /* locate pipe */
+ b = locate_pipe(p->pipe_nr);
+
+ if (b == NULL || b->pipe_nr != p->pipe_nr) { /* new pipe */
+ x = _MALLOC(sizeof(struct dn_pipe), M_DUMMYNET, M_DONTWAIT | M_ZERO) ;
+ if (x == NULL) {
+ lck_mtx_unlock(dn_mutex);
+ printf("dummynet: no memory for new pipe\n");
+ return ENOSPC;
+ }
+ x->pipe_nr = p->pipe_nr;
+ x->fs.pipe = x ;
+ /* idle_heap is the only one from which we extract from the middle.
+ */
+ x->idle_heap.size = x->idle_heap.elements = 0 ;
+ x->idle_heap.offset=offsetof(struct dn_flow_queue, heap_pos);
+ } else {
+ x = b;
+ /* Flush accumulated credit for all queues */
+ for (i = 0; i <= x->fs.rq_size; i++)
+ for (q = x->fs.rq[i]; q; q = q->next)
+ q->numbytes = 0;
+ }
+
+ x->bandwidth = p->bandwidth ;
+ x->numbytes = 0; /* just in case... */
+ bcopy(p->if_name, x->if_name, sizeof(p->if_name) );
+ x->ifp = NULL ; /* reset interface ptr */
+ x->delay = p->delay ;
+ set_fs_parms(&(x->fs), pfs);
+
+
+ if ( x->fs.rq == NULL ) { /* a new pipe */
+ r = alloc_hash(&(x->fs), pfs) ;
+ if (r) {
+ lck_mtx_unlock(dn_mutex);
+ FREE(x, M_DUMMYNET);
+ return r ;
+ }
+ SLIST_INSERT_HEAD(&pipehash[HASH(x->pipe_nr)],
+ x, next);
+ }
+ lck_mtx_unlock(dn_mutex);
+ } else { /* config queue */
+ struct dn_flow_set *x, *b ;
+
+ lck_mtx_lock(dn_mutex);
+ /* locate flow_set */
+ b = locate_flowset(pfs->fs_nr);
+
+ if (b == NULL || b->fs_nr != pfs->fs_nr) { /* new */
+ if (pfs->parent_nr == 0) { /* need link to a pipe */
+ lck_mtx_unlock(dn_mutex);
+ return EINVAL ;
+ }
+ x = _MALLOC(sizeof(struct dn_flow_set), M_DUMMYNET, M_DONTWAIT | M_ZERO);
+ if (x == NULL) {
+ lck_mtx_unlock(dn_mutex);
+ printf("dummynet: no memory for new flow_set\n");
+ return ENOSPC;
+ }
+ x->fs_nr = pfs->fs_nr;
+ x->parent_nr = pfs->parent_nr;
+ x->weight = pfs->weight ;
+ if (x->weight == 0)
+ x->weight = 1 ;
+ else if (x->weight > 100)
+ x->weight = 100 ;
+ } else {
+ /* Change parent pipe not allowed; must delete and recreate */
+ if (pfs->parent_nr != 0 && b->parent_nr != pfs->parent_nr) {
+ lck_mtx_unlock(dn_mutex);
+ return EINVAL ;
+ }
+ x = b;
+ }
+ set_fs_parms(x, pfs);
+
+ if ( x->rq == NULL ) { /* a new flow_set */
+ r = alloc_hash(x, pfs) ;
+ if (r) {
+ lck_mtx_unlock(dn_mutex);
+ FREE(x, M_DUMMYNET);
+ return r ;
+ }
+ SLIST_INSERT_HEAD(&flowsethash[HASH(x->fs_nr)],
+ x, next);
+ }
+ lck_mtx_unlock(dn_mutex);
+ }
+ return 0 ;
+}
+
+/*
+ * Helper function to remove from a heap queues which are linked to
+ * a flow_set about to be deleted.
+ */
+static void
+fs_remove_from_heap(struct dn_heap *h, struct dn_flow_set *fs)
+{
+ int i = 0, found = 0 ;
+ for (; i < h->elements ;)
+ if ( ((struct dn_flow_queue *)h->p[i].object)->fs == fs) {
+ h->elements-- ;
+ h->p[i] = h->p[h->elements] ;
+ found++ ;
+ } else
+ i++ ;
+ if (found)
+ heapify(h);
+}
+
+/*
+ * helper function to remove a pipe from a heap (can be there at most once)
+ */
+static void
+pipe_remove_from_heap(struct dn_heap *h, struct dn_pipe *p)
+{
+ if (h->elements > 0) {
+ int i = 0 ;
+ for (i=0; i < h->elements ; i++ ) {
+ if (h->p[i].object == p) { /* found it */
+ h->elements-- ;
+ h->p[i] = h->p[h->elements] ;
+ heapify(h);
+ break ;
+ }
+ }
+ }
+}
+
+/*
+ * drain all queues. Called in case of severe mbuf shortage.
+ */
+void
+dummynet_drain(void)
+{
+ struct dn_flow_set *fs;
+ struct dn_pipe *p;
+ struct mbuf *m, *mnext;
+ int i;
+
+ lck_mtx_assert(dn_mutex, LCK_MTX_ASSERT_OWNED);
+
+ heap_free(&ready_heap);
+ heap_free(&wfq_ready_heap);
+ heap_free(&extract_heap);
+ /* remove all references to this pipe from flow_sets */
+ for (i = 0; i < HASHSIZE; i++)
+ SLIST_FOREACH(fs, &flowsethash[i], next)
+ purge_flow_set(fs, 0);
+
+ for (i = 0; i < HASHSIZE; i++)
+ SLIST_FOREACH(p, &pipehash[i], next) {
+ purge_flow_set(&(p->fs), 0);
+
+ mnext = p->head;
+ while ((m = mnext) != NULL) {
+ mnext = m->m_nextpkt;
+ DN_FREE_PKT(m);
+ }
+ p->head = p->tail = NULL ;
+ }
+}
+
+/*
+ * Fully delete a pipe or a queue, cleaning up associated info.
+ */
+static int
+delete_pipe(struct dn_pipe *p)
+{
+ if (p->pipe_nr == 0 && p->fs.fs_nr == 0)
+ return EINVAL ;
+ if (p->pipe_nr != 0 && p->fs.fs_nr != 0)
+ return EINVAL ;
+ if (p->pipe_nr != 0) { /* this is an old-style pipe */
+ struct dn_pipe *b;
+ struct dn_flow_set *fs;
+ int i;
+
+ lck_mtx_lock(dn_mutex);
+ /* locate pipe */
+ b = locate_pipe(p->pipe_nr);
+ if(b == NULL){
+ lck_mtx_unlock(dn_mutex);
+ return EINVAL ; /* not found */
+ }
+
+ /* Unlink from list of pipes. */
+ SLIST_REMOVE(&pipehash[HASH(b->pipe_nr)], b, dn_pipe, next);
+
+ /* remove references to this pipe from the ip_fw rules. */
+ flush_pipe_ptrs(&(b->fs));
+
+ /* Remove all references to this pipe from flow_sets. */
+ for (i = 0; i < HASHSIZE; i++)
+ SLIST_FOREACH(fs, &flowsethash[i], next)
+ if (fs->pipe == b) {
+ printf("dummynet: ++ ref to pipe %d from fs %d\n",
+ p->pipe_nr, fs->fs_nr);
+ fs->pipe = NULL ;
+ purge_flow_set(fs, 0);
+ }
+ fs_remove_from_heap(&ready_heap, &(b->fs));
+
+ purge_pipe(b); /* remove all data associated to this pipe */
+ /* remove reference to here from extract_heap and wfq_ready_heap */
+ pipe_remove_from_heap(&extract_heap, b);
+ pipe_remove_from_heap(&wfq_ready_heap, b);
+ lck_mtx_unlock(dn_mutex);
+
+ FREE(b, M_DUMMYNET);
+ } else { /* this is a WF2Q queue (dn_flow_set) */
+ struct dn_flow_set *b;
+
+ lck_mtx_lock(dn_mutex);
+ /* locate set */
+ b = locate_flowset(p->fs.fs_nr);
+ if (b == NULL) {
+ lck_mtx_unlock(dn_mutex);
+ return EINVAL ; /* not found */
+ }
+
+ /* remove references to this flow_set from the ip_fw rules. */
+ flush_pipe_ptrs(b);
+
+ /* Unlink from list of flowsets. */
+ SLIST_REMOVE( &flowsethash[HASH(b->fs_nr)], b, dn_flow_set, next);
+
+ if (b->pipe != NULL) {
+ /* Update total weight on parent pipe and cleanup parent heaps */
+ b->pipe->sum -= b->weight * b->backlogged ;
+ fs_remove_from_heap(&(b->pipe->not_eligible_heap), b);
+ fs_remove_from_heap(&(b->pipe->scheduler_heap), b);
+#if 1 /* XXX should i remove from idle_heap as well ? */
+ fs_remove_from_heap(&(b->pipe->idle_heap), b);
+#endif
+ }
+ purge_flow_set(b, 1);
+ lck_mtx_unlock(dn_mutex);
+ }
+ return 0 ;
+}
+
+/*
+ * helper function used to copy data from kernel in DUMMYNET_GET
+ */
+static
+char* dn_copy_set_32(struct dn_flow_set *set, char *bp)
+{
+ int i, copied = 0 ;
+ struct dn_flow_queue *q;
+ struct dn_flow_queue_32 *qp = (struct dn_flow_queue_32 *)bp;
+
+ lck_mtx_assert(dn_mutex, LCK_MTX_ASSERT_OWNED);
+
+ for (i = 0 ; i <= set->rq_size ; i++)
+ for (q = set->rq[i] ; q ; q = q->next, qp++ ) {
+ if (q->hash_slot != i)
+ printf("dummynet: ++ at %d: wrong slot (have %d, "
+ "should be %d)\n", copied, q->hash_slot, i);
+ if (q->fs != set)
+ printf("dummynet: ++ at %d: wrong fs ptr (have %p, should be %p)\n",
+ i, q->fs, set);
+ copied++ ;
+ cp_queue_to_32_user( q, qp );
+ /* cleanup pointers */
+ qp->next = (user32_addr_t)0 ;
+ qp->head = qp->tail = (user32_addr_t)0 ;
+ qp->fs = (user32_addr_t)0 ;
+ }
+ if (copied != set->rq_elements)
+ printf("dummynet: ++ wrong count, have %d should be %d\n",
+ copied, set->rq_elements);
+ return (char *)qp ;
+}
+
+static
+char* dn_copy_set_64(struct dn_flow_set *set, char *bp)
+{
+ int i, copied = 0 ;
+ struct dn_flow_queue *q;
+ struct dn_flow_queue_64 *qp = (struct dn_flow_queue_64 *)bp;
+
+ lck_mtx_assert(dn_mutex, LCK_MTX_ASSERT_OWNED);
+
+ for (i = 0 ; i <= set->rq_size ; i++)
+ for (q = set->rq[i] ; q ; q = q->next, qp++ ) {
+ if (q->hash_slot != i)
+ printf("dummynet: ++ at %d: wrong slot (have %d, "
+ "should be %d)\n", copied, q->hash_slot, i);
+ if (q->fs != set)
+ printf("dummynet: ++ at %d: wrong fs ptr (have %p, should be %p)\n",
+ i, q->fs, set);
+ copied++ ;
+ //bcopy(q, qp, sizeof(*q));
+ cp_queue_to_64_user( q, qp );
+ /* cleanup pointers */
+ qp->next = USER_ADDR_NULL ;
+ qp->head = qp->tail = USER_ADDR_NULL ;
+ qp->fs = USER_ADDR_NULL ;
+ }
+ if (copied != set->rq_elements)
+ printf("dummynet: ++ wrong count, have %d should be %d\n",
+ copied, set->rq_elements);
+ return (char *)qp ;
+}
+
+static size_t
+dn_calc_size(int is64user)
+{
+ struct dn_flow_set *set ;
+ struct dn_pipe *p ;
+ size_t size = 0 ;
+ size_t pipesize;
+ size_t queuesize;
+ size_t setsize;
+ int i;
+
+ lck_mtx_assert(dn_mutex, LCK_MTX_ASSERT_OWNED);
+ if ( is64user ){
+ pipesize = sizeof(struct dn_pipe_64);
+ queuesize = sizeof(struct dn_flow_queue_64);
+ setsize = sizeof(struct dn_flow_set_64);
+ }
+ else {
+ pipesize = sizeof(struct dn_pipe_32);
+ queuesize = sizeof( struct dn_flow_queue_32 );
+ setsize = sizeof(struct dn_flow_set_32);
+ }
+ /*
+ * compute size of data structures: list of pipes and flow_sets.
+ */
+ for (i = 0; i < HASHSIZE; i++) {
+ SLIST_FOREACH(p, &pipehash[i], next)
+ size += sizeof(*p) +
+ p->fs.rq_elements * sizeof(struct dn_flow_queue);
+ SLIST_FOREACH(set, &flowsethash[i], next)
+ size += sizeof (*set) +
+ set->rq_elements * sizeof(struct dn_flow_queue);
+ }
+ return size;
+}
+
+static int
+dummynet_get(struct sockopt *sopt)
+{
+ char *buf, *bp=NULL; /* bp is the "copy-pointer" */
+ size_t size ;
+ struct dn_flow_set *set ;
+ struct dn_pipe *p ;
+ int error=0, i ;
+ int is64user = 0;
+
+ /* XXX lock held too long */
+ lck_mtx_lock(dn_mutex);
+ /*
+ * XXX: Ugly, but we need to allocate memory with M_WAITOK flag and we
+ * cannot use this flag while holding a mutex.
+ */
+ if (proc_is64bit(sopt->sopt_p))
+ is64user = 1;
+ for (i = 0; i < 10; i++) {
+ size = dn_calc_size(is64user);
+ lck_mtx_unlock(dn_mutex);
+ buf = _MALLOC(size, M_TEMP, M_WAITOK);
+ if (buf == NULL)
+ return ENOBUFS;
+ lck_mtx_lock(dn_mutex);
+ if (size == dn_calc_size(is64user))
+ break;
+ FREE(buf, M_TEMP);
+ buf = NULL;
+ }
+ if (buf == NULL) {
+ lck_mtx_unlock(dn_mutex);
+ return ENOBUFS ;
+ }
+
+
+ bp = buf;
+ for (i = 0; i < HASHSIZE; i++)
+ SLIST_FOREACH(p, &pipehash[i], next) {
+ /*
+ * copy pipe descriptor into *bp, convert delay back to ms,
+ * then copy the flow_set descriptor(s) one at a time.
+ * After each flow_set, copy the queue descriptor it owns.
+ */
+ if ( is64user ){
+ bp = cp_pipe_to_64_user(p, (struct dn_pipe_64 *)bp);
+ }
+ else{
+ bp = cp_pipe_to_32_user(p, (struct dn_pipe_32 *)bp);
+ }
+ }
+ for (i = 0; i < HASHSIZE; i++)
+ SLIST_FOREACH(set, &flowsethash[i], next) {
+ struct dn_flow_set_64 *fs_bp = (struct dn_flow_set_64 *)bp ;
+ cp_flow_set_to_64_user(set, fs_bp);
+ /* XXX same hack as above */
+ fs_bp->next = CAST_DOWN(user64_addr_t, DN_IS_QUEUE);
+ fs_bp->pipe = USER_ADDR_NULL;
+ fs_bp->rq = USER_ADDR_NULL ;
+ bp += sizeof(struct dn_flow_set_64);
+ bp = dn_copy_set_64( set, bp );
+ }
+ lck_mtx_unlock(dn_mutex);
+
+ error = sooptcopyout(sopt, buf, size);
+ FREE(buf, M_TEMP);
+ return error ;
+}
+
+/*
+ * Handler for the various dummynet socket options (get, flush, config, del)
+ */
+static int
+ip_dn_ctl(struct sockopt *sopt)
+{
+ int error = 0 ;
+ struct dn_pipe *p, tmp_pipe;
+
+ /* Disallow sets in really-really secure mode. */
+ if (sopt->sopt_dir == SOPT_SET && securelevel >= 3)
+ return (EPERM);
+
+ switch (sopt->sopt_name) {
+ default :
+ printf("dummynet: -- unknown option %d", sopt->sopt_name);
+ return EINVAL ;
+
+ case IP_DUMMYNET_GET :
+ error = dummynet_get(sopt);
+ break ;
+
+ case IP_DUMMYNET_FLUSH :
+ dummynet_flush() ;
+ break ;
+
+ case IP_DUMMYNET_CONFIGURE :
+ p = &tmp_pipe ;
+ if (proc_is64bit(sopt->sopt_p))
+ error = cp_pipe_from_user_64( sopt, p );
+ else
+ error = cp_pipe_from_user_32( sopt, p );
+
+ if (error)
+ break ;
+ error = config_pipe(p);
+ break ;
+
+ case IP_DUMMYNET_DEL : /* remove a pipe or queue */
+ p = &tmp_pipe ;
+ if (proc_is64bit(sopt->sopt_p))
+ error = cp_pipe_from_user_64( sopt, p );
+ else
+ error = cp_pipe_from_user_32( sopt, p );
+ if (error)
+ break ;
+
+ error = delete_pipe(p);
+ break ;
+ }
+ return error ;
+}
+
+void
+ip_dn_init(void)
+{
+ /* setup locks */
+ dn_mutex_grp_attr = lck_grp_attr_alloc_init();
+ dn_mutex_grp = lck_grp_alloc_init("dn", dn_mutex_grp_attr);
+ dn_mutex_attr = lck_attr_alloc_init();
+
+ if ((dn_mutex = lck_mtx_alloc_init(dn_mutex_grp, dn_mutex_attr)) == NULL) {
+ printf("ip_dn_init: can't alloc dn_mutex\n");
+ return;
+ }
+
+ ready_heap.size = ready_heap.elements = 0 ;
+ ready_heap.offset = 0 ;
+
+ wfq_ready_heap.size = wfq_ready_heap.elements = 0 ;
+ wfq_ready_heap.offset = 0 ;
+
+ extract_heap.size = extract_heap.elements = 0 ;
+ extract_heap.offset = 0 ;
+ ip_dn_ctl_ptr = ip_dn_ctl;
+ ip_dn_io_ptr = dummynet_io;
+ ip_dn_ruledel_ptr = dn_rule_delete;
+}