2 * Copyright (c) 1995 Danny Gasparovski.
4 * Please read the file COPYRIGHT for the
5 * terms and conditions of the copyright.
8 #include "qemu/osdep.h"
10 #include "qemu/timer.h"
13 ifs_insque(struct mbuf *ifm, struct mbuf *ifmhead)
15 ifm->ifs_next = ifmhead->ifs_next;
16 ifmhead->ifs_next = ifm;
17 ifm->ifs_prev = ifmhead;
18 ifm->ifs_next->ifs_prev = ifm;
22 ifs_remque(struct mbuf *ifm)
24 ifm->ifs_prev->ifs_next = ifm->ifs_next;
25 ifm->ifs_next->ifs_prev = ifm->ifs_prev;
31 slirp->if_fastq.ifq_next = slirp->if_fastq.ifq_prev = &slirp->if_fastq;
32 slirp->if_batchq.ifq_next = slirp->if_batchq.ifq_prev = &slirp->if_batchq;
33 slirp->next_m = &slirp->if_batchq;
37 * if_output: Queue packet into an output queue.
38 * There are 2 output queue's, if_fastq and if_batchq.
39 * Each output queue is a doubly linked list of double linked lists
40 * of mbufs, each list belonging to one "session" (socket). This
41 * way, we can output packets fairly by sending one packet from each
42 * session, instead of all the packets from one session, then all packets
43 * from the next session, etc. Packets on the if_fastq get absolute
44 * priority, but if one session hogs the link, it gets "downgraded"
45 * to the batchq until it runs out of packets, then it'll return
46 * to the fastq (eg. if the user does an ls -alR in a telnet session,
47 * it'll temporarily get downgraded to the batchq)
50 if_output(struct socket *so, struct mbuf *ifm)
52 Slirp *slirp = ifm->slirp;
56 DEBUG_CALL("if_output");
57 DEBUG_ARG("so = %p", so);
58 DEBUG_ARG("ifm = %p", ifm);
61 * First remove the mbuf from m_usedlist,
62 * since we're gonna use m_next and m_prev ourselves
63 * XXX Shouldn't need this, gotta change dtom() etc.
65 if (ifm->m_flags & M_USEDLIST) {
67 ifm->m_flags &= ~M_USEDLIST;
71 * See if there's already a batchq list for this session.
72 * This can include an interactive session, which should go on fastq,
73 * but gets too greedy... hence it'll be downgraded from fastq to batchq.
74 * We mustn't put this packet back on the fastq (or we'll send it out of order)
77 for (ifq = slirp->if_batchq.ifq_prev; ifq != &slirp->if_batchq;
78 ifq = ifq->ifq_prev) {
79 if (so == ifq->ifq_so) {
82 ifs_insque(ifm, ifq->ifs_prev);
87 /* No match, check which queue to put it on */
88 if (so && (so->so_iptos & IPTOS_LOWDELAY)) {
89 ifq = slirp->if_fastq.ifq_prev;
92 * Check if this packet is a part of the last
95 if (ifq->ifq_so == so) {
97 ifs_insque(ifm, ifq->ifs_prev);
101 ifq = slirp->if_batchq.ifq_prev;
102 /* Set next_m if the queue was empty so far */
103 if (slirp->next_m == &slirp->if_batchq) {
108 /* Create a new doubly linked list for this session */
115 /* Update *_queued */
119 * Check if the interactive session should be downgraded to
120 * the batchq. A session is downgraded if it has queued 6
121 * packets without pausing, and at least 3 of those packets
122 * have been sent over the link
123 * (XXX These are arbitrary numbers, probably not optimal..)
125 if (on_fastq && ((so->so_nqueued >= 6) &&
126 (so->so_nqueued - so->so_queued) >= 3)) {
128 /* Remove from current queue... */
129 remque(ifm->ifs_next);
131 /* ...And insert in the new. That'll teach ya! */
132 insque(ifm->ifs_next, &slirp->if_batchq);
138 * This prevents us from malloc()ing too many mbufs
140 if_start(ifm->slirp);
146 * We choose a packet based on its position in the output queues;
147 * If there are packets on the fastq, they are sent FIFO, before
148 * everything else. Otherwise we choose the first packet from the
149 * batchq and send it. the next packet chosen will be from the session
150 * after this one, then the session after that one, and so on.. So,
151 * for example, if there are 3 ftp session's fighting for bandwidth,
152 * one packet will be sent from the first session, then one packet
153 * from the second session, then one packet from the third, then back
154 * to the first, etc. etc.
156 void if_start(Slirp *slirp)
158 uint64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
159 bool from_batchq, next_from_batchq;
160 struct mbuf *ifm, *ifm_next, *ifqt;
162 DEBUG_CALL("if_start");
164 if (slirp->if_start_busy) {
167 slirp->if_start_busy = true;
169 if (slirp->if_fastq.ifq_next != &slirp->if_fastq) {
170 ifm_next = slirp->if_fastq.ifq_next;
171 next_from_batchq = false;
172 } else if (slirp->next_m != &slirp->if_batchq) {
173 /* Nothing on fastq, pick up from batchq via next_m */
174 ifm_next = slirp->next_m;
175 next_from_batchq = true;
182 from_batchq = next_from_batchq;
184 ifm_next = ifm->ifq_next;
185 if (ifm_next == &slirp->if_fastq) {
186 /* No more packets in fastq, switch to batchq */
187 ifm_next = slirp->next_m;
188 next_from_batchq = true;
190 if (ifm_next == &slirp->if_batchq) {
195 /* Try to send packet unless it already expired */
196 if (ifm->expiration_date >= now && !if_encap(slirp, ifm)) {
197 /* Packet is delayed due to pending ARP or NDP resolution */
201 if (ifm == slirp->next_m) {
202 /* Set which packet to send on next iteration */
203 slirp->next_m = ifm->ifq_next;
206 /* Remove it from the queue */
207 ifqt = ifm->ifq_prev;
210 /* If there are more packets for this session, re-queue them */
211 if (ifm->ifs_next != ifm) {
212 struct mbuf *next = ifm->ifs_next;
218 /* Next packet in fastq is from the same session */
220 next_from_batchq = false;
221 } else if (slirp->next_m == &slirp->if_batchq) {
222 /* Set next_m and ifm_next if the session packet is now the
223 * only one on batchq */
224 slirp->next_m = ifm_next = next;
228 /* Update so_queued */
229 if (ifm->ifq_so && --ifm->ifq_so->so_queued == 0) {
230 /* If there's no more queued, reset nqueued */
231 ifm->ifq_so->so_nqueued = 0;
237 slirp->if_start_busy = false;