Statistics
| Branch: | Revision:

root / slirp / if.c @ 9634d903

History | View | Annotate | Download (7.3 kB)

1
/*
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

    
10
int     if_queued = 0;                  /* Number of packets queued so far */
11
int     if_thresh = 10;                 /* Number of packets queued before we start sending
12
                                         * (to prevent allocing too many mbufs) */
13

    
14
struct  mbuf if_fastq;                  /* fast queue (for interactive data) */
15
struct  mbuf if_batchq;                 /* queue for non-interactive data */
16
struct        mbuf *next_m;                        /* Pointer to next mbuf to output */
17

    
18
#define ifs_init(ifm) ((ifm)->ifs_next = (ifm)->ifs_prev = (ifm))
19

    
20
void
21
ifs_insque(ifm, ifmhead)
22
        struct mbuf *ifm, *ifmhead;
23
{
24
        ifm->ifs_next = ifmhead->ifs_next;
25
        ifmhead->ifs_next = ifm;
26
        ifm->ifs_prev = ifmhead;
27
        ifm->ifs_next->ifs_prev = ifm;
28
}
29

    
30
void
31
ifs_remque(ifm)
32
        struct mbuf *ifm;
33
{
34
        ifm->ifs_prev->ifs_next = ifm->ifs_next;
35
        ifm->ifs_next->ifs_prev = ifm->ifs_prev;
36
}
37

    
38
void
39
if_init()
40
{
41
        if_fastq.ifq_next = if_fastq.ifq_prev = &if_fastq;
42
        if_batchq.ifq_next = if_batchq.ifq_prev = &if_batchq;
43
        //        sl_compress_init(&comp_s);
44
        next_m = &if_batchq;
45
}
46

    
47
#if 0
48
/*
49
 * This shouldn't be needed since the modem is blocking and
50
 * we don't expect any signals, but what the hell..
51
 */
52
inline int
53
writen(fd, bptr, n)
54
        int fd;
55
        char *bptr;
56
        int n;
57
{
58
        int ret;
59
        int total;
60

61
        /* This should succeed most of the time */
62
        ret = send(fd, bptr, n,0);
63
        if (ret == n || ret <= 0)
64
           return ret;
65

66
        /* Didn't write everything, go into the loop */
67
        total = ret;
68
        while (n > total) {
69
                ret = send(fd, bptr+total, n-total,0);
70
                if (ret <= 0)
71
                   return ret;
72
                total += ret;
73
        }
74
        return total;
75
}
76

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

91
        DEBUG_CALL("if_input");
92
        DEBUG_ARG("ttyp = %lx", (long)ttyp);
93

94
        if_n = recv(ttyp->fd, (char *)if_inbuff, INBUFF_SIZE,0);
95

96
        DEBUG_MISC((dfd, " read %d bytes\n", if_n));
97

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

122
        (*ttyp->if_input)(ttyp, if_inbuff, if_n);
123
}
124
#endif
125

    
126
/*
127
 * if_output: Queue packet into an output queue.
128
 * There are 2 output queue's, if_fastq and if_batchq.
129
 * Each output queue is a doubly linked list of double linked lists
130
 * of mbufs, each list belonging to one "session" (socket).  This
131
 * way, we can output packets fairly by sending one packet from each
132
 * session, instead of all the packets from one session, then all packets
133
 * from the next session, etc.  Packets on the if_fastq get absolute
134
 * priority, but if one session hogs the link, it gets "downgraded"
135
 * to the batchq until it runs out of packets, then it'll return
136
 * to the fastq (eg. if the user does an ls -alR in a telnet session,
137
 * it'll temporarily get downgraded to the batchq)
138
 */
139
void
140
if_output(so, ifm)
141
        struct socket *so;
142
        struct mbuf *ifm;
143
{
144
        struct mbuf *ifq;
145
        int on_fastq = 1;
146

    
147
        DEBUG_CALL("if_output");
148
        DEBUG_ARG("so = %lx", (long)so);
149
        DEBUG_ARG("ifm = %lx", (long)ifm);
150

    
151
        /*
152
         * First remove the mbuf from m_usedlist,
153
         * since we're gonna use m_next and m_prev ourselves
154
         * XXX Shouldn't need this, gotta change dtom() etc.
155
         */
156
        if (ifm->m_flags & M_USEDLIST) {
157
                remque(ifm);
158
                ifm->m_flags &= ~M_USEDLIST;
159
        }
160

    
161
        /*
162
         * See if there's already a batchq list for this session.
163
         * This can include an interactive session, which should go on fastq,
164
         * but gets too greedy... hence it'll be downgraded from fastq to batchq.
165
         * We mustn't put this packet back on the fastq (or we'll send it out of order)
166
         * XXX add cache here?
167
         */
168
        for (ifq = if_batchq.ifq_prev; ifq != &if_batchq; ifq = ifq->ifq_prev) {
169
                if (so == ifq->ifq_so) {
170
                        /* A match! */
171
                        ifm->ifq_so = so;
172
                        ifs_insque(ifm, ifq->ifs_prev);
173
                        goto diddit;
174
                }
175
        }
176

    
177
        /* No match, check which queue to put it on */
178
        if (so && (so->so_iptos & IPTOS_LOWDELAY)) {
179
                ifq = if_fastq.ifq_prev;
180
                on_fastq = 1;
181
                /*
182
                 * Check if this packet is a part of the last
183
                 * packet's session
184
                 */
185
                if (ifq->ifq_so == so) {
186
                        ifm->ifq_so = so;
187
                        ifs_insque(ifm, ifq->ifs_prev);
188
                        goto diddit;
189
                }
190
        } else
191
                ifq = if_batchq.ifq_prev;
192

    
193
        /* Create a new doubly linked list for this session */
194
        ifm->ifq_so = so;
195
        ifs_init(ifm);
196
        insque(ifm, ifq);
197

    
198
diddit:
199
        ++if_queued;
200

    
201
        if (so) {
202
                /* Update *_queued */
203
                so->so_queued++;
204
                so->so_nqueued++;
205
                /*
206
                 * Check if the interactive session should be downgraded to
207
                 * the batchq.  A session is downgraded if it has queued 6
208
                 * packets without pausing, and at least 3 of those packets
209
                 * have been sent over the link
210
                 * (XXX These are arbitrary numbers, probably not optimal..)
211
                 */
212
                if (on_fastq && ((so->so_nqueued >= 6) &&
213
                                 (so->so_nqueued - so->so_queued) >= 3)) {
214

    
215
                        /* Remove from current queue... */
216
                        remque(ifm->ifs_next);
217

    
218
                        /* ...And insert in the new.  That'll teach ya! */
219
                        insque(ifm->ifs_next, &if_batchq);
220
                }
221
        }
222

    
223
#ifndef FULL_BOLT
224
        /*
225
         * This prevents us from malloc()ing too many mbufs
226
         */
227
        if (link_up) {
228
                /* if_start will check towrite */
229
                if_start();
230
        }
231
#endif
232
}
233

    
234
/*
235
 * Send a packet
236
 * We choose a packet based on it's position in the output queues;
237
 * If there are packets on the fastq, they are sent FIFO, before
238
 * everything else.  Otherwise we choose the first packet from the
239
 * batchq and send it.  the next packet chosen will be from the session
240
 * after this one, then the session after that one, and so on..  So,
241
 * for example, if there are 3 ftp session's fighting for bandwidth,
242
 * one packet will be sent from the first session, then one packet
243
 * from the second session, then one packet from the third, then back
244
 * to the first, etc. etc.
245
 */
246
void
247
if_start(void)
248
{
249
        struct mbuf *ifm, *ifqt;
250

    
251
        DEBUG_CALL("if_start");
252

    
253
        if (if_queued == 0)
254
           return; /* Nothing to do */
255

    
256
 again:
257
        /* check if we can really output */
258
        if (!slirp_can_output())
259
            return;
260

    
261
        /*
262
         * See which queue to get next packet from
263
         * If there's something in the fastq, select it immediately
264
         */
265
        if (if_fastq.ifq_next != &if_fastq) {
266
                ifm = if_fastq.ifq_next;
267
        } else {
268
                /* Nothing on fastq, see if next_m is valid */
269
                if (next_m != &if_batchq)
270
                   ifm = next_m;
271
                else
272
                   ifm = if_batchq.ifq_next;
273

    
274
                /* Set which packet to send on next iteration */
275
                next_m = ifm->ifq_next;
276
        }
277
        /* Remove it from the queue */
278
        ifqt = ifm->ifq_prev;
279
        remque(ifm);
280
        --if_queued;
281

    
282
        /* If there are more packets for this session, re-queue them */
283
        if (ifm->ifs_next != /* ifm->ifs_prev != */ ifm) {
284
                insque(ifm->ifs_next, ifqt);
285
                ifs_remque(ifm);
286
        }
287

    
288
        /* Update so_queued */
289
        if (ifm->ifq_so) {
290
                if (--ifm->ifq_so->so_queued == 0)
291
                   /* If there's no more queued, reset nqueued */
292
                   ifm->ifq_so->so_nqueued = 0;
293
        }
294

    
295
        /* Encapsulate the packet for sending */
296
        if_encap(ifm->m_data, ifm->m_len);
297

    
298
        m_free(ifm);
299

    
300
        if (if_queued)
301
           goto again;
302
}