VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/slirp/if.c@ 14329

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette