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