Statistics
| Branch: | Revision:

root / slirp / if.c @ ed23fbd9

History | View | Annotate | Download (7.1 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
12 f0cbd3ec bellard
struct  mbuf if_fastq;                  /* fast queue (for interactive data) */
13 f0cbd3ec bellard
struct  mbuf if_batchq;                 /* queue for non-interactive data */
14 f0cbd3ec bellard
struct        mbuf *next_m;                        /* Pointer to next mbuf to output */
15 f0cbd3ec bellard
16 f0cbd3ec bellard
#define ifs_init(ifm) ((ifm)->ifs_next = (ifm)->ifs_prev = (ifm))
17 f0cbd3ec bellard
18 f0cbd3ec bellard
void
19 a5f1b965 blueswir1
ifs_insque(struct mbuf *ifm, struct mbuf *ifmhead)
20 f0cbd3ec bellard
{
21 f0cbd3ec bellard
        ifm->ifs_next = ifmhead->ifs_next;
22 f0cbd3ec bellard
        ifmhead->ifs_next = ifm;
23 f0cbd3ec bellard
        ifm->ifs_prev = ifmhead;
24 f0cbd3ec bellard
        ifm->ifs_next->ifs_prev = ifm;
25 f0cbd3ec bellard
}
26 f0cbd3ec bellard
27 f0cbd3ec bellard
void
28 a5f1b965 blueswir1
ifs_remque(struct mbuf *ifm)
29 f0cbd3ec bellard
{
30 f0cbd3ec bellard
        ifm->ifs_prev->ifs_next = ifm->ifs_next;
31 f0cbd3ec bellard
        ifm->ifs_next->ifs_prev = ifm->ifs_prev;
32 f0cbd3ec bellard
}
33 f0cbd3ec bellard
34 f0cbd3ec bellard
void
35 f0cbd3ec bellard
if_init()
36 f0cbd3ec bellard
{
37 f0cbd3ec bellard
        if_fastq.ifq_next = if_fastq.ifq_prev = &if_fastq;
38 f0cbd3ec bellard
        if_batchq.ifq_next = if_batchq.ifq_prev = &if_batchq;
39 f0cbd3ec bellard
        //        sl_compress_init(&comp_s);
40 f0cbd3ec bellard
        next_m = &if_batchq;
41 f0cbd3ec bellard
}
42 f0cbd3ec bellard
43 f0cbd3ec bellard
#if 0
44 f0cbd3ec bellard
/*
45 f0cbd3ec bellard
 * This shouldn't be needed since the modem is blocking and
46 f0cbd3ec bellard
 * we don't expect any signals, but what the hell..
47 f0cbd3ec bellard
 */
48 f0cbd3ec bellard
inline int
49 f0cbd3ec bellard
writen(fd, bptr, n)
50 f0cbd3ec bellard
        int fd;
51 f0cbd3ec bellard
        char *bptr;
52 f0cbd3ec bellard
        int n;
53 f0cbd3ec bellard
{
54 f0cbd3ec bellard
        int ret;
55 f0cbd3ec bellard
        int total;
56 5fafdf24 ths

57 f0cbd3ec bellard
        /* This should succeed most of the time */
58 02d2c54c bellard
        ret = send(fd, bptr, n,0);
59 f0cbd3ec bellard
        if (ret == n || ret <= 0)
60 f0cbd3ec bellard
           return ret;
61 5fafdf24 ths

62 f0cbd3ec bellard
        /* Didn't write everything, go into the loop */
63 f0cbd3ec bellard
        total = ret;
64 f0cbd3ec bellard
        while (n > total) {
65 02d2c54c bellard
                ret = send(fd, bptr+total, n-total,0);
66 f0cbd3ec bellard
                if (ret <= 0)
67 f0cbd3ec bellard
                   return ret;
68 f0cbd3ec bellard
                total += ret;
69 f0cbd3ec bellard
        }
70 f0cbd3ec bellard
        return total;
71 f0cbd3ec bellard
}
72 f0cbd3ec bellard

73 f0cbd3ec bellard
/*
74 f0cbd3ec bellard
 * if_input - read() the tty, do "top level" processing (ie: check for any escapes),
75 f0cbd3ec bellard
 * and pass onto (*ttyp->if_input)
76 5fafdf24 ths
 *
77 f0cbd3ec bellard
 * XXXXX Any zeros arriving by themselves are NOT placed into the arriving packet.
78 f0cbd3ec bellard
 */
79 f0cbd3ec bellard
#define INBUFF_SIZE 2048 /* XXX */
80 f0cbd3ec bellard
void
81 f0cbd3ec bellard
if_input(ttyp)
82 f0cbd3ec bellard
        struct ttys *ttyp;
83 f0cbd3ec bellard
{
84 f0cbd3ec bellard
        u_char if_inbuff[INBUFF_SIZE];
85 f0cbd3ec bellard
        int if_n;
86 5fafdf24 ths

87 f0cbd3ec bellard
        DEBUG_CALL("if_input");
88 f0cbd3ec bellard
        DEBUG_ARG("ttyp = %lx", (long)ttyp);
89 5fafdf24 ths

90 02d2c54c bellard
        if_n = recv(ttyp->fd, (char *)if_inbuff, INBUFF_SIZE,0);
91 5fafdf24 ths

92 f0cbd3ec bellard
        DEBUG_MISC((dfd, " read %d bytes\n", if_n));
93 5fafdf24 ths

94 f0cbd3ec bellard
        if (if_n <= 0) {
95 f0cbd3ec bellard
                if (if_n == 0 || (errno != EINTR && errno != EAGAIN)) {
96 f0cbd3ec bellard
                        if (ttyp->up)
97 f0cbd3ec bellard
                           link_up--;
98 f0cbd3ec bellard
                        tty_detached(ttyp, 0);
99 f0cbd3ec bellard
                }
100 f0cbd3ec bellard
                return;
101 f0cbd3ec bellard
        }
102 f0cbd3ec bellard
        if (if_n == 1) {
103 f0cbd3ec bellard
                if (*if_inbuff == '0') {
104 f0cbd3ec bellard
                        ttyp->ones = 0;
105 f0cbd3ec bellard
                        if (++ttyp->zeros >= 5)
106 f0cbd3ec bellard
                           slirp_exit(0);
107 f0cbd3ec bellard
                        return;
108 f0cbd3ec bellard
                }
109 f0cbd3ec bellard
                if (*if_inbuff == '1') {
110 f0cbd3ec bellard
                        ttyp->zeros = 0;
111 f0cbd3ec bellard
                        if (++ttyp->ones >= 5)
112 f0cbd3ec bellard
                           tty_detached(ttyp, 0);
113 f0cbd3ec bellard
                        return;
114 f0cbd3ec bellard
                }
115 f0cbd3ec bellard
        }
116 f0cbd3ec bellard
        ttyp->ones = ttyp->zeros = 0;
117 5fafdf24 ths

118 f0cbd3ec bellard
        (*ttyp->if_input)(ttyp, if_inbuff, if_n);
119 f0cbd3ec bellard
}
120 5fafdf24 ths
#endif
121 5fafdf24 ths
122 f0cbd3ec bellard
/*
123 f0cbd3ec bellard
 * if_output: Queue packet into an output queue.
124 5fafdf24 ths
 * There are 2 output queue's, if_fastq and if_batchq.
125 f0cbd3ec bellard
 * Each output queue is a doubly linked list of double linked lists
126 f0cbd3ec bellard
 * of mbufs, each list belonging to one "session" (socket).  This
127 f0cbd3ec bellard
 * way, we can output packets fairly by sending one packet from each
128 f0cbd3ec bellard
 * session, instead of all the packets from one session, then all packets
129 5fafdf24 ths
 * from the next session, etc.  Packets on the if_fastq get absolute
130 f0cbd3ec bellard
 * priority, but if one session hogs the link, it gets "downgraded"
131 f0cbd3ec bellard
 * to the batchq until it runs out of packets, then it'll return
132 f0cbd3ec bellard
 * to the fastq (eg. if the user does an ls -alR in a telnet session,
133 f0cbd3ec bellard
 * it'll temporarily get downgraded to the batchq)
134 f0cbd3ec bellard
 */
135 f0cbd3ec bellard
void
136 f0cbd3ec bellard
if_output(so, ifm)
137 f0cbd3ec bellard
        struct socket *so;
138 f0cbd3ec bellard
        struct mbuf *ifm;
139 f0cbd3ec bellard
{
140 f0cbd3ec bellard
        struct mbuf *ifq;
141 f0cbd3ec bellard
        int on_fastq = 1;
142 5fafdf24 ths
143 f0cbd3ec bellard
        DEBUG_CALL("if_output");
144 f0cbd3ec bellard
        DEBUG_ARG("so = %lx", (long)so);
145 f0cbd3ec bellard
        DEBUG_ARG("ifm = %lx", (long)ifm);
146 5fafdf24 ths
147 f0cbd3ec bellard
        /*
148 f0cbd3ec bellard
         * First remove the mbuf from m_usedlist,
149 f0cbd3ec bellard
         * since we're gonna use m_next and m_prev ourselves
150 f0cbd3ec bellard
         * XXX Shouldn't need this, gotta change dtom() etc.
151 f0cbd3ec bellard
         */
152 f0cbd3ec bellard
        if (ifm->m_flags & M_USEDLIST) {
153 f0cbd3ec bellard
                remque(ifm);
154 f0cbd3ec bellard
                ifm->m_flags &= ~M_USEDLIST;
155 f0cbd3ec bellard
        }
156 5fafdf24 ths
157 f0cbd3ec bellard
        /*
158 3b46e624 ths
         * See if there's already a batchq list for this session.
159 f0cbd3ec bellard
         * This can include an interactive session, which should go on fastq,
160 f0cbd3ec bellard
         * but gets too greedy... hence it'll be downgraded from fastq to batchq.
161 f0cbd3ec bellard
         * We mustn't put this packet back on the fastq (or we'll send it out of order)
162 f0cbd3ec bellard
         * XXX add cache here?
163 f0cbd3ec bellard
         */
164 f0cbd3ec bellard
        for (ifq = if_batchq.ifq_prev; ifq != &if_batchq; ifq = ifq->ifq_prev) {
165 f0cbd3ec bellard
                if (so == ifq->ifq_so) {
166 f0cbd3ec bellard
                        /* A match! */
167 f0cbd3ec bellard
                        ifm->ifq_so = so;
168 f0cbd3ec bellard
                        ifs_insque(ifm, ifq->ifs_prev);
169 f0cbd3ec bellard
                        goto diddit;
170 f0cbd3ec bellard
                }
171 f0cbd3ec bellard
        }
172 5fafdf24 ths
173 f0cbd3ec bellard
        /* No match, check which queue to put it on */
174 f0cbd3ec bellard
        if (so && (so->so_iptos & IPTOS_LOWDELAY)) {
175 f0cbd3ec bellard
                ifq = if_fastq.ifq_prev;
176 f0cbd3ec bellard
                on_fastq = 1;
177 f0cbd3ec bellard
                /*
178 f0cbd3ec bellard
                 * Check if this packet is a part of the last
179 f0cbd3ec bellard
                 * packet's session
180 f0cbd3ec bellard
                 */
181 f0cbd3ec bellard
                if (ifq->ifq_so == so) {
182 f0cbd3ec bellard
                        ifm->ifq_so = so;
183 f0cbd3ec bellard
                        ifs_insque(ifm, ifq->ifs_prev);
184 f0cbd3ec bellard
                        goto diddit;
185 f0cbd3ec bellard
                }
186 f0cbd3ec bellard
        } else
187 f0cbd3ec bellard
                ifq = if_batchq.ifq_prev;
188 5fafdf24 ths
189 f0cbd3ec bellard
        /* Create a new doubly linked list for this session */
190 f0cbd3ec bellard
        ifm->ifq_so = so;
191 f0cbd3ec bellard
        ifs_init(ifm);
192 f0cbd3ec bellard
        insque(ifm, ifq);
193 5fafdf24 ths
194 f0cbd3ec bellard
diddit:
195 f0cbd3ec bellard
        ++if_queued;
196 5fafdf24 ths
197 f0cbd3ec bellard
        if (so) {
198 f0cbd3ec bellard
                /* Update *_queued */
199 f0cbd3ec bellard
                so->so_queued++;
200 f0cbd3ec bellard
                so->so_nqueued++;
201 f0cbd3ec bellard
                /*
202 f0cbd3ec bellard
                 * Check if the interactive session should be downgraded to
203 f0cbd3ec bellard
                 * the batchq.  A session is downgraded if it has queued 6
204 f0cbd3ec bellard
                 * packets without pausing, and at least 3 of those packets
205 f0cbd3ec bellard
                 * have been sent over the link
206 f0cbd3ec bellard
                 * (XXX These are arbitrary numbers, probably not optimal..)
207 f0cbd3ec bellard
                 */
208 5fafdf24 ths
                if (on_fastq && ((so->so_nqueued >= 6) &&
209 f0cbd3ec bellard
                                 (so->so_nqueued - so->so_queued) >= 3)) {
210 3b46e624 ths
211 f0cbd3ec bellard
                        /* Remove from current queue... */
212 f0cbd3ec bellard
                        remque(ifm->ifs_next);
213 3b46e624 ths
214 f0cbd3ec bellard
                        /* ...And insert in the new.  That'll teach ya! */
215 f0cbd3ec bellard
                        insque(ifm->ifs_next, &if_batchq);
216 f0cbd3ec bellard
                }
217 f0cbd3ec bellard
        }
218 f0cbd3ec bellard
219 f0cbd3ec bellard
#ifndef FULL_BOLT
220 f0cbd3ec bellard
        /*
221 f0cbd3ec bellard
         * This prevents us from malloc()ing too many mbufs
222 f0cbd3ec bellard
         */
223 f0cbd3ec bellard
        if (link_up) {
224 f0cbd3ec bellard
                /* if_start will check towrite */
225 f0cbd3ec bellard
                if_start();
226 f0cbd3ec bellard
        }
227 f0cbd3ec bellard
#endif
228 f0cbd3ec bellard
}
229 f0cbd3ec bellard
230 f0cbd3ec bellard
/*
231 f0cbd3ec bellard
 * Send a packet
232 f0cbd3ec bellard
 * We choose a packet based on it's position in the output queues;
233 f0cbd3ec bellard
 * If there are packets on the fastq, they are sent FIFO, before
234 f0cbd3ec bellard
 * everything else.  Otherwise we choose the first packet from the
235 f0cbd3ec bellard
 * batchq and send it.  the next packet chosen will be from the session
236 f0cbd3ec bellard
 * after this one, then the session after that one, and so on..  So,
237 f0cbd3ec bellard
 * for example, if there are 3 ftp session's fighting for bandwidth,
238 f0cbd3ec bellard
 * one packet will be sent from the first session, then one packet
239 f0cbd3ec bellard
 * from the second session, then one packet from the third, then back
240 f0cbd3ec bellard
 * to the first, etc. etc.
241 f0cbd3ec bellard
 */
242 f0cbd3ec bellard
void
243 f0cbd3ec bellard
if_start(void)
244 f0cbd3ec bellard
{
245 f0cbd3ec bellard
        struct mbuf *ifm, *ifqt;
246 5fafdf24 ths
247 f0cbd3ec bellard
        DEBUG_CALL("if_start");
248 5fafdf24 ths
249 f0cbd3ec bellard
        if (if_queued == 0)
250 f0cbd3ec bellard
           return; /* Nothing to do */
251 5fafdf24 ths
252 f0cbd3ec bellard
 again:
253 f0cbd3ec bellard
        /* check if we can really output */
254 f0cbd3ec bellard
        if (!slirp_can_output())
255 f0cbd3ec bellard
            return;
256 f0cbd3ec bellard
257 f0cbd3ec bellard
        /*
258 f0cbd3ec bellard
         * See which queue to get next packet from
259 f0cbd3ec bellard
         * If there's something in the fastq, select it immediately
260 f0cbd3ec bellard
         */
261 f0cbd3ec bellard
        if (if_fastq.ifq_next != &if_fastq) {
262 f0cbd3ec bellard
                ifm = if_fastq.ifq_next;
263 f0cbd3ec bellard
        } else {
264 f0cbd3ec bellard
                /* Nothing on fastq, see if next_m is valid */
265 f0cbd3ec bellard
                if (next_m != &if_batchq)
266 f0cbd3ec bellard
                   ifm = next_m;
267 f0cbd3ec bellard
                else
268 f0cbd3ec bellard
                   ifm = if_batchq.ifq_next;
269 3b46e624 ths
270 f0cbd3ec bellard
                /* Set which packet to send on next iteration */
271 f0cbd3ec bellard
                next_m = ifm->ifq_next;
272 f0cbd3ec bellard
        }
273 f0cbd3ec bellard
        /* Remove it from the queue */
274 f0cbd3ec bellard
        ifqt = ifm->ifq_prev;
275 f0cbd3ec bellard
        remque(ifm);
276 f0cbd3ec bellard
        --if_queued;
277 5fafdf24 ths
278 f0cbd3ec bellard
        /* If there are more packets for this session, re-queue them */
279 f0cbd3ec bellard
        if (ifm->ifs_next != /* ifm->ifs_prev != */ ifm) {
280 f0cbd3ec bellard
                insque(ifm->ifs_next, ifqt);
281 f0cbd3ec bellard
                ifs_remque(ifm);
282 f0cbd3ec bellard
        }
283 5fafdf24 ths
284 f0cbd3ec bellard
        /* Update so_queued */
285 f0cbd3ec bellard
        if (ifm->ifq_so) {
286 f0cbd3ec bellard
                if (--ifm->ifq_so->so_queued == 0)
287 f0cbd3ec bellard
                   /* If there's no more queued, reset nqueued */
288 f0cbd3ec bellard
                   ifm->ifq_so->so_nqueued = 0;
289 f0cbd3ec bellard
        }
290 5fafdf24 ths
291 f0cbd3ec bellard
        /* Encapsulate the packet for sending */
292 242acf3a balrog
        if_encap((uint8_t *)ifm->m_data, ifm->m_len);
293 f0cbd3ec bellard
294 c9a62117 bellard
        m_free(ifm);
295 c9a62117 bellard
296 f0cbd3ec bellard
        if (if_queued)
297 f0cbd3ec bellard
           goto again;
298 f0cbd3ec bellard
}