58ab4c7aca3213717c5a0eafefc0ec1c2b8e053f
[qemu.git] / slirp / if.c
blob58ab4c7aca3213717c5a0eafefc0ec1c2b8e053f
1 /*
2 * Copyright (c) 1995 Danny Gasparovski.
4 * Please read the file COPYRIGHT for the
5 * terms and conditions of the copyright.
6 */
8 #include <slirp.h>
10 int if_queued = 0; /* Number of packets queued so far */
12 struct mbuf if_fastq; /* fast queue (for interactive data) */
13 struct mbuf if_batchq; /* queue for non-interactive data */
14 struct mbuf *next_m; /* Pointer to next mbuf to output */
16 #define ifs_init(ifm) ((ifm)->ifs_next = (ifm)->ifs_prev = (ifm))
18 static void
19 ifs_insque(struct mbuf *ifm, struct mbuf *ifmhead)
21 ifm->ifs_next = ifmhead->ifs_next;
22 ifmhead->ifs_next = ifm;
23 ifm->ifs_prev = ifmhead;
24 ifm->ifs_next->ifs_prev = ifm;
27 static void
28 ifs_remque(struct mbuf *ifm)
30 ifm->ifs_prev->ifs_next = ifm->ifs_next;
31 ifm->ifs_next->ifs_prev = ifm->ifs_prev;
34 void
35 if_init(void)
37 if_fastq.ifq_next = if_fastq.ifq_prev = &if_fastq;
38 if_batchq.ifq_next = if_batchq.ifq_prev = &if_batchq;
39 next_m = &if_batchq;
43 * if_output: Queue packet into an output queue.
44 * There are 2 output queue's, if_fastq and if_batchq.
45 * Each output queue is a doubly linked list of double linked lists
46 * of mbufs, each list belonging to one "session" (socket). This
47 * way, we can output packets fairly by sending one packet from each
48 * session, instead of all the packets from one session, then all packets
49 * from the next session, etc. Packets on the if_fastq get absolute
50 * priority, but if one session hogs the link, it gets "downgraded"
51 * to the batchq until it runs out of packets, then it'll return
52 * to the fastq (eg. if the user does an ls -alR in a telnet session,
53 * it'll temporarily get downgraded to the batchq)
55 void
56 if_output(struct socket *so, struct mbuf *ifm)
58 struct mbuf *ifq;
59 int on_fastq = 1;
61 DEBUG_CALL("if_output");
62 DEBUG_ARG("so = %lx", (long)so);
63 DEBUG_ARG("ifm = %lx", (long)ifm);
66 * First remove the mbuf from m_usedlist,
67 * since we're gonna use m_next and m_prev ourselves
68 * XXX Shouldn't need this, gotta change dtom() etc.
70 if (ifm->m_flags & M_USEDLIST) {
71 remque(ifm);
72 ifm->m_flags &= ~M_USEDLIST;
76 * See if there's already a batchq list for this session.
77 * This can include an interactive session, which should go on fastq,
78 * but gets too greedy... hence it'll be downgraded from fastq to batchq.
79 * We mustn't put this packet back on the fastq (or we'll send it out of order)
80 * XXX add cache here?
82 for (ifq = if_batchq.ifq_prev; ifq != &if_batchq; ifq = ifq->ifq_prev) {
83 if (so == ifq->ifq_so) {
84 /* A match! */
85 ifm->ifq_so = so;
86 ifs_insque(ifm, ifq->ifs_prev);
87 goto diddit;
91 /* No match, check which queue to put it on */
92 if (so && (so->so_iptos & IPTOS_LOWDELAY)) {
93 ifq = if_fastq.ifq_prev;
94 on_fastq = 1;
96 * Check if this packet is a part of the last
97 * packet's session
99 if (ifq->ifq_so == so) {
100 ifm->ifq_so = so;
101 ifs_insque(ifm, ifq->ifs_prev);
102 goto diddit;
104 } else
105 ifq = if_batchq.ifq_prev;
107 /* Create a new doubly linked list for this session */
108 ifm->ifq_so = so;
109 ifs_init(ifm);
110 insque(ifm, ifq);
112 diddit:
113 ++if_queued;
115 if (so) {
116 /* Update *_queued */
117 so->so_queued++;
118 so->so_nqueued++;
120 * Check if the interactive session should be downgraded to
121 * the batchq. A session is downgraded if it has queued 6
122 * packets without pausing, and at least 3 of those packets
123 * have been sent over the link
124 * (XXX These are arbitrary numbers, probably not optimal..)
126 if (on_fastq && ((so->so_nqueued >= 6) &&
127 (so->so_nqueued - so->so_queued) >= 3)) {
129 /* Remove from current queue... */
130 remque(ifm->ifs_next);
132 /* ...And insert in the new. That'll teach ya! */
133 insque(ifm->ifs_next, &if_batchq);
137 #ifndef FULL_BOLT
139 * This prevents us from malloc()ing too many mbufs
141 if_start();
142 #endif
146 * Send a packet
147 * We choose a packet based on it's position in the output queues;
148 * If there are packets on the fastq, they are sent FIFO, before
149 * everything else. Otherwise we choose the first packet from the
150 * batchq and send it. the next packet chosen will be from the session
151 * after this one, then the session after that one, and so on.. So,
152 * for example, if there are 3 ftp session's fighting for bandwidth,
153 * one packet will be sent from the first session, then one packet
154 * from the second session, then one packet from the third, then back
155 * to the first, etc. etc.
157 void
158 if_start(void)
160 struct mbuf *ifm, *ifqt;
162 DEBUG_CALL("if_start");
164 if (if_queued == 0)
165 return; /* Nothing to do */
167 again:
168 /* check if we can really output */
169 if (!slirp_can_output())
170 return;
173 * See which queue to get next packet from
174 * If there's something in the fastq, select it immediately
176 if (if_fastq.ifq_next != &if_fastq) {
177 ifm = if_fastq.ifq_next;
178 } else {
179 /* Nothing on fastq, see if next_m is valid */
180 if (next_m != &if_batchq)
181 ifm = next_m;
182 else
183 ifm = if_batchq.ifq_next;
185 /* Set which packet to send on next iteration */
186 next_m = ifm->ifq_next;
188 /* Remove it from the queue */
189 ifqt = ifm->ifq_prev;
190 remque(ifm);
191 --if_queued;
193 /* If there are more packets for this session, re-queue them */
194 if (ifm->ifs_next != /* ifm->ifs_prev != */ ifm) {
195 insque(ifm->ifs_next, ifqt);
196 ifs_remque(ifm);
199 /* Update so_queued */
200 if (ifm->ifq_so) {
201 if (--ifm->ifq_so->so_queued == 0)
202 /* If there's no more queued, reset nqueued */
203 ifm->ifq_so->so_nqueued = 0;
206 /* Encapsulate the packet for sending */
207 if_encap((uint8_t *)ifm->m_data, ifm->m_len);
209 m_free(ifm);
211 if (if_queued)
212 goto again;