Fix Arm msr spsr bug.
[qemu/mini2440.git] / slirp / if.c
blob2ce9a6424fb9024c71dfc84e9e833c143cc15d68
1 /*
2 * Copyright (c) 1995 Danny Gasparovski.
4 * Please read the file COPYRIGHT for the
5 * terms and conditions of the copyright.
6 */
8 #include <slirp.h>
10 int if_mtu, if_mru;
11 int if_comp;
12 int if_maxlinkhdr;
13 int if_queued = 0; /* Number of packets queued so far */
14 int if_thresh = 10; /* Number of packets queued before we start sending
15 * (to prevent allocing too many mbufs) */
17 struct mbuf if_fastq; /* fast queue (for interactive data) */
18 struct mbuf if_batchq; /* queue for non-interactive data */
19 struct mbuf *next_m; /* Pointer to next mbuf to output */
21 #define ifs_init(ifm) ((ifm)->ifs_next = (ifm)->ifs_prev = (ifm))
23 void
24 ifs_insque(ifm, ifmhead)
25 struct mbuf *ifm, *ifmhead;
27 ifm->ifs_next = ifmhead->ifs_next;
28 ifmhead->ifs_next = ifm;
29 ifm->ifs_prev = ifmhead;
30 ifm->ifs_next->ifs_prev = ifm;
33 void
34 ifs_remque(ifm)
35 struct mbuf *ifm;
37 ifm->ifs_prev->ifs_next = ifm->ifs_next;
38 ifm->ifs_next->ifs_prev = ifm->ifs_prev;
41 void
42 if_init()
44 #if 0
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
49 #ifdef USE_PPP
50 if_maxlinkhdr = 48;
51 #else
52 if_maxlinkhdr = 40;
53 #endif
54 #else
55 /* 14 for ethernet + 40 */
56 if_maxlinkhdr = 14 + 40;
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;
67 #if 0
69 * This shouldn't be needed since the modem is blocking and
70 * we don't expect any signals, but what the hell..
72 inline int
73 writen(fd, bptr, n)
74 int fd;
75 char *bptr;
76 int n;
78 int ret;
79 int total;
81 /* This should succeed most of the time */
82 ret = send(fd, bptr, n,0);
83 if (ret == n || ret <= 0)
84 return ret;
86 /* Didn't write everything, go into the loop */
87 total = ret;
88 while (n > total) {
89 ret = send(fd, bptr+total, n-total,0);
90 if (ret <= 0)
91 return ret;
92 total += ret;
94 return total;
98 * if_input - read() the tty, do "top level" processing (ie: check for any escapes),
99 * and pass onto (*ttyp->if_input)
101 * XXXXX Any zeros arriving by themselves are NOT placed into the arriving packet.
103 #define INBUFF_SIZE 2048 /* XXX */
104 void
105 if_input(ttyp)
106 struct ttys *ttyp;
108 u_char if_inbuff[INBUFF_SIZE];
109 int if_n;
111 DEBUG_CALL("if_input");
112 DEBUG_ARG("ttyp = %lx", (long)ttyp);
114 if_n = recv(ttyp->fd, (char *)if_inbuff, INBUFF_SIZE,0);
116 DEBUG_MISC((dfd, " read %d bytes\n", if_n));
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);
124 return;
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;
133 if (*if_inbuff == '1') {
134 ttyp->zeros = 0;
135 if (++ttyp->ones >= 5)
136 tty_detached(ttyp, 0);
137 return;
140 ttyp->ones = ttyp->zeros = 0;
142 (*ttyp->if_input)(ttyp, if_inbuff, if_n);
144 #endif
147 * if_output: Queue packet into an output queue.
148 * There are 2 output queue's, if_fastq and if_batchq.
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
153 * from the next session, etc. Packets on the if_fastq get absolute
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)
159 void
160 if_output(so, ifm)
161 struct socket *so;
162 struct mbuf *ifm;
164 struct mbuf *ifq;
165 int on_fastq = 1;
167 DEBUG_CALL("if_output");
168 DEBUG_ARG("so = %lx", (long)so);
169 DEBUG_ARG("ifm = %lx", (long)ifm);
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.
176 if (ifm->m_flags & M_USEDLIST) {
177 remque(ifm);
178 ifm->m_flags &= ~M_USEDLIST;
182 * See if there's already a batchq list for this session.
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?
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;
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;
202 * Check if this packet is a part of the last
203 * packet's session
205 if (ifq->ifq_so == so) {
206 ifm->ifq_so = so;
207 ifs_insque(ifm, ifq->ifs_prev);
208 goto diddit;
210 } else
211 ifq = if_batchq.ifq_prev;
213 /* Create a new doubly linked list for this session */
214 ifm->ifq_so = so;
215 ifs_init(ifm);
216 insque(ifm, ifq);
218 diddit:
219 ++if_queued;
221 if (so) {
222 /* Update *_queued */
223 so->so_queued++;
224 so->so_nqueued++;
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..)
232 if (on_fastq && ((so->so_nqueued >= 6) &&
233 (so->so_nqueued - so->so_queued) >= 3)) {
235 /* Remove from current queue... */
236 remque(ifm->ifs_next);
238 /* ...And insert in the new. That'll teach ya! */
239 insque(ifm->ifs_next, &if_batchq);
243 #ifndef FULL_BOLT
245 * This prevents us from malloc()ing too many mbufs
247 if (link_up) {
248 /* if_start will check towrite */
249 if_start();
251 #endif
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.
266 void
267 if_start(void)
269 struct mbuf *ifm, *ifqt;
271 DEBUG_CALL("if_start");
273 if (if_queued == 0)
274 return; /* Nothing to do */
276 again:
277 /* check if we can really output */
278 if (!slirp_can_output())
279 return;
282 * See which queue to get next packet from
283 * If there's something in the fastq, select it immediately
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;
294 /* Set which packet to send on next iteration */
295 next_m = ifm->ifq_next;
297 /* Remove it from the queue */
298 ifqt = ifm->ifq_prev;
299 remque(ifm);
300 --if_queued;
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);
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;
315 /* Encapsulate the packet for sending */
316 if_encap(ifm->m_data, ifm->m_len);
318 m_free(ifm);
320 if (if_queued)
321 goto again;