]> git.proxmox.com Git - qemu.git/blob - slirp/if.c
slirp: Drop unused icmp_var.h
[qemu.git] / slirp / if.c
1 /*
2 * Copyright (c) 1995 Danny Gasparovski.
3 *
4 * Please read the file COPYRIGHT for the
5 * terms and conditions of the copyright.
6 */
7
8 #include <slirp.h>
9
10 int if_queued = 0; /* Number of packets queued so far */
11
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 */
15
16 #define ifs_init(ifm) ((ifm)->ifs_next = (ifm)->ifs_prev = (ifm))
17
18 static void
19 ifs_insque(struct mbuf *ifm, struct mbuf *ifmhead)
20 {
21 ifm->ifs_next = ifmhead->ifs_next;
22 ifmhead->ifs_next = ifm;
23 ifm->ifs_prev = ifmhead;
24 ifm->ifs_next->ifs_prev = ifm;
25 }
26
27 static void
28 ifs_remque(struct mbuf *ifm)
29 {
30 ifm->ifs_prev->ifs_next = ifm->ifs_next;
31 ifm->ifs_next->ifs_prev = ifm->ifs_prev;
32 }
33
34 void
35 if_init(void)
36 {
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;
40 }
41
42 /*
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)
54 */
55 void
56 if_output(struct socket *so, struct mbuf *ifm)
57 {
58 struct mbuf *ifq;
59 int on_fastq = 1;
60
61 DEBUG_CALL("if_output");
62 DEBUG_ARG("so = %lx", (long)so);
63 DEBUG_ARG("ifm = %lx", (long)ifm);
64
65 /*
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.
69 */
70 if (ifm->m_flags & M_USEDLIST) {
71 remque(ifm);
72 ifm->m_flags &= ~M_USEDLIST;
73 }
74
75 /*
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?
81 */
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;
88 }
89 }
90
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;
95 /*
96 * Check if this packet is a part of the last
97 * packet's session
98 */
99 if (ifq->ifq_so == so) {
100 ifm->ifq_so = so;
101 ifs_insque(ifm, ifq->ifs_prev);
102 goto diddit;
103 }
104 } else
105 ifq = if_batchq.ifq_prev;
106
107 /* Create a new doubly linked list for this session */
108 ifm->ifq_so = so;
109 ifs_init(ifm);
110 insque(ifm, ifq);
111
112 diddit:
113 ++if_queued;
114
115 if (so) {
116 /* Update *_queued */
117 so->so_queued++;
118 so->so_nqueued++;
119 /*
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..)
125 */
126 if (on_fastq && ((so->so_nqueued >= 6) &&
127 (so->so_nqueued - so->so_queued) >= 3)) {
128
129 /* Remove from current queue... */
130 remque(ifm->ifs_next);
131
132 /* ...And insert in the new. That'll teach ya! */
133 insque(ifm->ifs_next, &if_batchq);
134 }
135 }
136
137 #ifndef FULL_BOLT
138 /*
139 * This prevents us from malloc()ing too many mbufs
140 */
141 if (link_up) {
142 /* if_start will check towrite */
143 if_start();
144 }
145 #endif
146 }
147
148 /*
149 * Send a packet
150 * We choose a packet based on it's position in the output queues;
151 * If there are packets on the fastq, they are sent FIFO, before
152 * everything else. Otherwise we choose the first packet from the
153 * batchq and send it. the next packet chosen will be from the session
154 * after this one, then the session after that one, and so on.. So,
155 * for example, if there are 3 ftp session's fighting for bandwidth,
156 * one packet will be sent from the first session, then one packet
157 * from the second session, then one packet from the third, then back
158 * to the first, etc. etc.
159 */
160 void
161 if_start(void)
162 {
163 struct mbuf *ifm, *ifqt;
164
165 DEBUG_CALL("if_start");
166
167 if (if_queued == 0)
168 return; /* Nothing to do */
169
170 again:
171 /* check if we can really output */
172 if (!slirp_can_output())
173 return;
174
175 /*
176 * See which queue to get next packet from
177 * If there's something in the fastq, select it immediately
178 */
179 if (if_fastq.ifq_next != &if_fastq) {
180 ifm = if_fastq.ifq_next;
181 } else {
182 /* Nothing on fastq, see if next_m is valid */
183 if (next_m != &if_batchq)
184 ifm = next_m;
185 else
186 ifm = if_batchq.ifq_next;
187
188 /* Set which packet to send on next iteration */
189 next_m = ifm->ifq_next;
190 }
191 /* Remove it from the queue */
192 ifqt = ifm->ifq_prev;
193 remque(ifm);
194 --if_queued;
195
196 /* If there are more packets for this session, re-queue them */
197 if (ifm->ifs_next != /* ifm->ifs_prev != */ ifm) {
198 insque(ifm->ifs_next, ifqt);
199 ifs_remque(ifm);
200 }
201
202 /* Update so_queued */
203 if (ifm->ifq_so) {
204 if (--ifm->ifq_so->so_queued == 0)
205 /* If there's no more queued, reset nqueued */
206 ifm->ifq_so->so_nqueued = 0;
207 }
208
209 /* Encapsulate the packet for sending */
210 if_encap((uint8_t *)ifm->m_data, ifm->m_len);
211
212 m_free(ifm);
213
214 if (if_queued)
215 goto again;
216 }