blob: 17f8a733781e795e40223278c0ad38711349fbfd [file] [log] [blame]
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001/*
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
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080010int if_queued = 0; /* Number of packets queued so far */
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080011
12struct mbuf if_fastq; /* fast queue (for interactive data) */
13struct mbuf if_batchq; /* queue for non-interactive data */
14struct mbuf *next_m; /* Pointer to next mbuf to output */
15
16#define ifs_init(ifm) ((ifm)->ifs_next = (ifm)->ifs_prev = (ifm))
17
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070018static void
19ifs_insque(struct mbuf *ifm, struct mbuf *ifmhead)
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080020{
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
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070027static void
28ifs_remque(struct mbuf *ifm)
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080029{
30 ifm->ifs_prev->ifs_next = ifm->ifs_next;
31 ifm->ifs_next->ifs_prev = ifm->ifs_prev;
32}
33
34void
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070035if_init(void)
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080036{
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080037 if_fastq.ifq_next = if_fastq.ifq_prev = &if_fastq;
38 if_batchq.ifq_next = if_batchq.ifq_prev = &if_batchq;
39 // sl_compress_init(&comp_s);
40 next_m = &if_batchq;
41}
42
43#if 0
44/*
45 * This shouldn't be needed since the modem is blocking and
46 * we don't expect any signals, but what the hell..
47 */
48inline int
49writen(fd, bptr, n)
50 int fd;
51 char *bptr;
52 int n;
53{
54 int ret;
55 int total;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070056
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080057 /* This should succeed most of the time */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070058 ret = send(fd, bptr, n,0);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080059 if (ret == n || ret <= 0)
60 return ret;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070061
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080062 /* Didn't write everything, go into the loop */
63 total = ret;
64 while (n > total) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070065 ret = send(fd, bptr+total, n-total,0);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080066 if (ret <= 0)
67 return ret;
68 total += ret;
69 }
70 return total;
71}
72
73/*
74 * if_input - read() the tty, do "top level" processing (ie: check for any escapes),
75 * and pass onto (*ttyp->if_input)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070076 *
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080077 * XXXXX Any zeros arriving by themselves are NOT placed into the arriving packet.
78 */
79#define INBUFF_SIZE 2048 /* XXX */
80void
81if_input(ttyp)
82 struct ttys *ttyp;
83{
84 u_char if_inbuff[INBUFF_SIZE];
85 int if_n;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070086
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080087 DEBUG_CALL("if_input");
88 DEBUG_ARG("ttyp = %lx", (long)ttyp);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070089
90 if_n = recv(ttyp->fd, (char *)if_inbuff, INBUFF_SIZE,0);
91
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080092 DEBUG_MISC((dfd, " read %d bytes\n", if_n));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070093
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080094 if (if_n <= 0) {
95 if (if_n == 0 || (errno != EINTR && errno != EAGAIN)) {
96 if (ttyp->up)
97 link_up--;
98 tty_detached(ttyp, 0);
99 }
100 return;
101 }
102 if (if_n == 1) {
103 if (*if_inbuff == '0') {
104 ttyp->ones = 0;
105 if (++ttyp->zeros >= 5)
106 slirp_exit(0);
107 return;
108 }
109 if (*if_inbuff == '1') {
110 ttyp->zeros = 0;
111 if (++ttyp->ones >= 5)
112 tty_detached(ttyp, 0);
113 return;
114 }
115 }
116 ttyp->ones = ttyp->zeros = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700117
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800118 (*ttyp->if_input)(ttyp, if_inbuff, if_n);
119}
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700120#endif
121
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800122/*
123 * if_output: Queue packet into an output queue.
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700124 * There are 2 output queue's, if_fastq and if_batchq.
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800125 * Each output queue is a doubly linked list of double linked lists
126 * of mbufs, each list belonging to one "session" (socket). This
127 * way, we can output packets fairly by sending one packet from each
128 * session, instead of all the packets from one session, then all packets
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700129 * from the next session, etc. Packets on the if_fastq get absolute
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800130 * priority, but if one session hogs the link, it gets "downgraded"
131 * to the batchq until it runs out of packets, then it'll return
132 * to the fastq (eg. if the user does an ls -alR in a telnet session,
133 * it'll temporarily get downgraded to the batchq)
134 */
135void
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700136if_output(struct socket *so, struct mbuf *ifm)
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800137{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700138 struct mbuf *ifq;
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800139 int on_fastq = 1;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700140
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800141 DEBUG_CALL("if_output");
142 DEBUG_ARG("so = %lx", (long)so);
143 DEBUG_ARG("ifm = %lx", (long)ifm);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700144
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800145 /*
146 * First remove the mbuf from m_usedlist,
147 * since we're gonna use m_next and m_prev ourselves
148 * XXX Shouldn't need this, gotta change dtom() etc.
149 */
150 if (ifm->m_flags & M_USEDLIST) {
151 remque(ifm);
152 ifm->m_flags &= ~M_USEDLIST;
153 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700154
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800155 /*
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700156 * See if there's already a batchq list for this session.
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800157 * This can include an interactive session, which should go on fastq,
158 * but gets too greedy... hence it'll be downgraded from fastq to batchq.
159 * We mustn't put this packet back on the fastq (or we'll send it out of order)
160 * XXX add cache here?
161 */
162 for (ifq = if_batchq.ifq_prev; ifq != &if_batchq; ifq = ifq->ifq_prev) {
163 if (so == ifq->ifq_so) {
164 /* A match! */
165 ifm->ifq_so = so;
166 ifs_insque(ifm, ifq->ifs_prev);
167 goto diddit;
168 }
169 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700170
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800171 /* No match, check which queue to put it on */
172 if (so && (so->so_iptos & IPTOS_LOWDELAY)) {
173 ifq = if_fastq.ifq_prev;
174 on_fastq = 1;
175 /*
176 * Check if this packet is a part of the last
177 * packet's session
178 */
179 if (ifq->ifq_so == so) {
180 ifm->ifq_so = so;
181 ifs_insque(ifm, ifq->ifs_prev);
182 goto diddit;
183 }
184 } else
185 ifq = if_batchq.ifq_prev;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700186
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800187 /* Create a new doubly linked list for this session */
188 ifm->ifq_so = so;
189 ifs_init(ifm);
190 insque(ifm, ifq);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700191
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800192diddit:
193 ++if_queued;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700194
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800195 if (so) {
196 /* Update *_queued */
197 so->so_queued++;
198 so->so_nqueued++;
199 /*
200 * Check if the interactive session should be downgraded to
201 * the batchq. A session is downgraded if it has queued 6
202 * packets without pausing, and at least 3 of those packets
203 * have been sent over the link
204 * (XXX These are arbitrary numbers, probably not optimal..)
205 */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700206 if (on_fastq && ((so->so_nqueued >= 6) &&
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800207 (so->so_nqueued - so->so_queued) >= 3)) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700208
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800209 /* Remove from current queue... */
210 remque(ifm->ifs_next);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700211
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800212 /* ...And insert in the new. That'll teach ya! */
213 insque(ifm->ifs_next, &if_batchq);
214 }
215 }
216
217#ifndef FULL_BOLT
218 /*
219 * This prevents us from malloc()ing too many mbufs
220 */
221 if (link_up) {
222 /* if_start will check towrite */
223 if_start();
224 }
225#endif
226}
227
228/*
229 * Send a packet
230 * We choose a packet based on it's position in the output queues;
231 * If there are packets on the fastq, they are sent FIFO, before
232 * everything else. Otherwise we choose the first packet from the
233 * batchq and send it. the next packet chosen will be from the session
234 * after this one, then the session after that one, and so on.. So,
235 * for example, if there are 3 ftp session's fighting for bandwidth,
236 * one packet will be sent from the first session, then one packet
237 * from the second session, then one packet from the third, then back
238 * to the first, etc. etc.
239 */
240void
241if_start(void)
242{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700243 struct mbuf *ifm, *ifqt;
244
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800245 DEBUG_CALL("if_start");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700246
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800247 if (if_queued == 0)
248 return; /* Nothing to do */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700249
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800250 again:
251 /* check if we can really output */
252 if (!slirp_can_output())
253 return;
254
255 /*
256 * See which queue to get next packet from
257 * If there's something in the fastq, select it immediately
258 */
259 if (if_fastq.ifq_next != &if_fastq) {
260 ifm = if_fastq.ifq_next;
261 } else {
262 /* Nothing on fastq, see if next_m is valid */
263 if (next_m != &if_batchq)
264 ifm = next_m;
265 else
266 ifm = if_batchq.ifq_next;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700267
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800268 /* Set which packet to send on next iteration */
269 next_m = ifm->ifq_next;
270 }
271 /* Remove it from the queue */
272 ifqt = ifm->ifq_prev;
273 remque(ifm);
274 --if_queued;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700275
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800276 /* If there are more packets for this session, re-queue them */
277 if (ifm->ifs_next != /* ifm->ifs_prev != */ ifm) {
278 insque(ifm->ifs_next, ifqt);
279 ifs_remque(ifm);
280 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700281
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800282 /* Update so_queued */
283 if (ifm->ifq_so) {
284 if (--ifm->ifq_so->so_queued == 0)
285 /* If there's no more queued, reset nqueued */
286 ifm->ifq_so->so_nqueued = 0;
287 }
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800288
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700289 /* Encapsulate the packet for sending */
290 if_encap((uint8_t *)ifm->m_data, ifm->m_len);
291
292 m_free(ifm);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800293
294 if (if_queued)
295 goto again;
296}