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