]> git.proxmox.com Git - mirror_qemu.git/blame - slirp/if.c
find -type f | xargs sed -i 's/[\t ]$//g' # on most files
[mirror_qemu.git] / slirp / if.c
CommitLineData
f0cbd3ec
FB
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
10int if_mtu, if_mru;
11int if_comp;
12int if_maxlinkhdr;
13int if_queued = 0; /* Number of packets queued so far */
14int if_thresh = 10; /* Number of packets queued before we start sending
15 * (to prevent allocing too many mbufs) */
16
17struct mbuf if_fastq; /* fast queue (for interactive data) */
18struct mbuf if_batchq; /* queue for non-interactive data */
19struct mbuf *next_m; /* Pointer to next mbuf to output */
20
21#define ifs_init(ifm) ((ifm)->ifs_next = (ifm)->ifs_prev = (ifm))
22
23void
24ifs_insque(ifm, ifmhead)
25 struct mbuf *ifm, *ifmhead;
26{
27 ifm->ifs_next = ifmhead->ifs_next;
28 ifmhead->ifs_next = ifm;
29 ifm->ifs_prev = ifmhead;
30 ifm->ifs_next->ifs_prev = ifm;
31}
32
33void
34ifs_remque(ifm)
35 struct mbuf *ifm;
36{
37 ifm->ifs_prev->ifs_next = ifm->ifs_next;
38 ifm->ifs_next->ifs_prev = ifm->ifs_prev;
39}
40
41void
42if_init()
43{
44#if 0
45 /*
46 * Set if_maxlinkhdr to 48 because it's 40 bytes for TCP/IP,
47 * and 8 bytes for PPP, but need to have it on an 8byte boundary
48 */
49#ifdef USE_PPP
50 if_maxlinkhdr = 48;
51#else
52 if_maxlinkhdr = 40;
53#endif
54#else
38f3e7c2
FB
55 /* 2 for alignment, 14 for ethernet, 40 for TCP/IP */
56 if_maxlinkhdr = 2 + 14 + 40;
f0cbd3ec
FB
57#endif
58 if_mtu = 1500;
59 if_mru = 1500;
60 if_comp = IF_AUTOCOMP;
61 if_fastq.ifq_next = if_fastq.ifq_prev = &if_fastq;
62 if_batchq.ifq_next = if_batchq.ifq_prev = &if_batchq;
63 // sl_compress_init(&comp_s);
64 next_m = &if_batchq;
65}
66
67#if 0
68/*
69 * This shouldn't be needed since the modem is blocking and
70 * we don't expect any signals, but what the hell..
71 */
72inline int
73writen(fd, bptr, n)
74 int fd;
75 char *bptr;
76 int n;
77{
78 int ret;
79 int total;
5fafdf24 80
f0cbd3ec 81 /* This should succeed most of the time */
02d2c54c 82 ret = send(fd, bptr, n,0);
f0cbd3ec
FB
83 if (ret == n || ret <= 0)
84 return ret;
5fafdf24 85
f0cbd3ec
FB
86 /* Didn't write everything, go into the loop */
87 total = ret;
88 while (n > total) {
02d2c54c 89 ret = send(fd, bptr+total, n-total,0);
f0cbd3ec
FB
90 if (ret <= 0)
91 return ret;
92 total += ret;
93 }
94 return total;
95}
96
97/*
98 * if_input - read() the tty, do "top level" processing (ie: check for any escapes),
99 * and pass onto (*ttyp->if_input)
5fafdf24 100 *
f0cbd3ec
FB
101 * XXXXX Any zeros arriving by themselves are NOT placed into the arriving packet.
102 */
103#define INBUFF_SIZE 2048 /* XXX */
104void
105if_input(ttyp)
106 struct ttys *ttyp;
107{
108 u_char if_inbuff[INBUFF_SIZE];
109 int if_n;
5fafdf24 110
f0cbd3ec
FB
111 DEBUG_CALL("if_input");
112 DEBUG_ARG("ttyp = %lx", (long)ttyp);
5fafdf24 113
02d2c54c 114 if_n = recv(ttyp->fd, (char *)if_inbuff, INBUFF_SIZE,0);
5fafdf24 115
f0cbd3ec 116 DEBUG_MISC((dfd, " read %d bytes\n", if_n));
5fafdf24 117
f0cbd3ec
FB
118 if (if_n <= 0) {
119 if (if_n == 0 || (errno != EINTR && errno != EAGAIN)) {
120 if (ttyp->up)
121 link_up--;
122 tty_detached(ttyp, 0);
123 }
124 return;
125 }
126 if (if_n == 1) {
127 if (*if_inbuff == '0') {
128 ttyp->ones = 0;
129 if (++ttyp->zeros >= 5)
130 slirp_exit(0);
131 return;
132 }
133 if (*if_inbuff == '1') {
134 ttyp->zeros = 0;
135 if (++ttyp->ones >= 5)
136 tty_detached(ttyp, 0);
137 return;
138 }
139 }
140 ttyp->ones = ttyp->zeros = 0;
5fafdf24 141
f0cbd3ec
FB
142 (*ttyp->if_input)(ttyp, if_inbuff, if_n);
143}
5fafdf24
TS
144#endif
145
f0cbd3ec
FB
146/*
147 * if_output: Queue packet into an output queue.
5fafdf24 148 * There are 2 output queue's, if_fastq and if_batchq.
f0cbd3ec
FB
149 * Each output queue is a doubly linked list of double linked lists
150 * of mbufs, each list belonging to one "session" (socket). This
151 * way, we can output packets fairly by sending one packet from each
152 * session, instead of all the packets from one session, then all packets
5fafdf24 153 * from the next session, etc. Packets on the if_fastq get absolute
f0cbd3ec
FB
154 * priority, but if one session hogs the link, it gets "downgraded"
155 * to the batchq until it runs out of packets, then it'll return
156 * to the fastq (eg. if the user does an ls -alR in a telnet session,
157 * it'll temporarily get downgraded to the batchq)
158 */
159void
160if_output(so, ifm)
161 struct socket *so;
162 struct mbuf *ifm;
163{
164 struct mbuf *ifq;
165 int on_fastq = 1;
5fafdf24 166
f0cbd3ec
FB
167 DEBUG_CALL("if_output");
168 DEBUG_ARG("so = %lx", (long)so);
169 DEBUG_ARG("ifm = %lx", (long)ifm);
5fafdf24 170
f0cbd3ec
FB
171 /*
172 * First remove the mbuf from m_usedlist,
173 * since we're gonna use m_next and m_prev ourselves
174 * XXX Shouldn't need this, gotta change dtom() etc.
175 */
176 if (ifm->m_flags & M_USEDLIST) {
177 remque(ifm);
178 ifm->m_flags &= ~M_USEDLIST;
179 }
5fafdf24 180
f0cbd3ec 181 /*
5fafdf24 182 * See if there's already a batchq list for this session.
f0cbd3ec
FB
183 * This can include an interactive session, which should go on fastq,
184 * but gets too greedy... hence it'll be downgraded from fastq to batchq.
185 * We mustn't put this packet back on the fastq (or we'll send it out of order)
186 * XXX add cache here?
187 */
188 for (ifq = if_batchq.ifq_prev; ifq != &if_batchq; ifq = ifq->ifq_prev) {
189 if (so == ifq->ifq_so) {
190 /* A match! */
191 ifm->ifq_so = so;
192 ifs_insque(ifm, ifq->ifs_prev);
193 goto diddit;
194 }
195 }
5fafdf24 196
f0cbd3ec
FB
197 /* No match, check which queue to put it on */
198 if (so && (so->so_iptos & IPTOS_LOWDELAY)) {
199 ifq = if_fastq.ifq_prev;
200 on_fastq = 1;
201 /*
202 * Check if this packet is a part of the last
203 * packet's session
204 */
205 if (ifq->ifq_so == so) {
206 ifm->ifq_so = so;
207 ifs_insque(ifm, ifq->ifs_prev);
208 goto diddit;
209 }
210 } else
211 ifq = if_batchq.ifq_prev;
5fafdf24 212
f0cbd3ec
FB
213 /* Create a new doubly linked list for this session */
214 ifm->ifq_so = so;
215 ifs_init(ifm);
216 insque(ifm, ifq);
5fafdf24 217
f0cbd3ec
FB
218diddit:
219 ++if_queued;
5fafdf24 220
f0cbd3ec
FB
221 if (so) {
222 /* Update *_queued */
223 so->so_queued++;
224 so->so_nqueued++;
225 /*
226 * Check if the interactive session should be downgraded to
227 * the batchq. A session is downgraded if it has queued 6
228 * packets without pausing, and at least 3 of those packets
229 * have been sent over the link
230 * (XXX These are arbitrary numbers, probably not optimal..)
231 */
5fafdf24 232 if (on_fastq && ((so->so_nqueued >= 6) &&
f0cbd3ec 233 (so->so_nqueued - so->so_queued) >= 3)) {
5fafdf24 234
f0cbd3ec
FB
235 /* Remove from current queue... */
236 remque(ifm->ifs_next);
5fafdf24 237
f0cbd3ec
FB
238 /* ...And insert in the new. That'll teach ya! */
239 insque(ifm->ifs_next, &if_batchq);
240 }
241 }
242
243#ifndef FULL_BOLT
244 /*
245 * This prevents us from malloc()ing too many mbufs
246 */
247 if (link_up) {
248 /* if_start will check towrite */
249 if_start();
250 }
251#endif
252}
253
254/*
255 * Send a packet
256 * We choose a packet based on it's position in the output queues;
257 * If there are packets on the fastq, they are sent FIFO, before
258 * everything else. Otherwise we choose the first packet from the
259 * batchq and send it. the next packet chosen will be from the session
260 * after this one, then the session after that one, and so on.. So,
261 * for example, if there are 3 ftp session's fighting for bandwidth,
262 * one packet will be sent from the first session, then one packet
263 * from the second session, then one packet from the third, then back
264 * to the first, etc. etc.
265 */
266void
267if_start(void)
268{
269 struct mbuf *ifm, *ifqt;
5fafdf24 270
f0cbd3ec 271 DEBUG_CALL("if_start");
5fafdf24 272
f0cbd3ec
FB
273 if (if_queued == 0)
274 return; /* Nothing to do */
5fafdf24 275
f0cbd3ec
FB
276 again:
277 /* check if we can really output */
278 if (!slirp_can_output())
279 return;
280
281 /*
282 * See which queue to get next packet from
283 * If there's something in the fastq, select it immediately
284 */
285 if (if_fastq.ifq_next != &if_fastq) {
286 ifm = if_fastq.ifq_next;
287 } else {
288 /* Nothing on fastq, see if next_m is valid */
289 if (next_m != &if_batchq)
290 ifm = next_m;
291 else
292 ifm = if_batchq.ifq_next;
5fafdf24 293
f0cbd3ec
FB
294 /* Set which packet to send on next iteration */
295 next_m = ifm->ifq_next;
296 }
297 /* Remove it from the queue */
298 ifqt = ifm->ifq_prev;
299 remque(ifm);
300 --if_queued;
5fafdf24 301
f0cbd3ec
FB
302 /* If there are more packets for this session, re-queue them */
303 if (ifm->ifs_next != /* ifm->ifs_prev != */ ifm) {
304 insque(ifm->ifs_next, ifqt);
305 ifs_remque(ifm);
306 }
5fafdf24 307
f0cbd3ec
FB
308 /* Update so_queued */
309 if (ifm->ifq_so) {
310 if (--ifm->ifq_so->so_queued == 0)
311 /* If there's no more queued, reset nqueued */
312 ifm->ifq_so->so_nqueued = 0;
313 }
5fafdf24 314
f0cbd3ec
FB
315 /* Encapsulate the packet for sending */
316 if_encap(ifm->m_data, ifm->m_len);
317
c9a62117
FB
318 m_free(ifm);
319
f0cbd3ec
FB
320 if (if_queued)
321 goto again;
322}