2
* Copyright (c) 1995 Danny Gasparovski.
4
* Please read the file COPYRIGHT for the
5
* terms and conditions of the copyright.
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))
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;
37
ifm->ifs_prev->ifs_next = ifm->ifs_next;
38
ifm->ifs_next->ifs_prev = ifm->ifs_prev;
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
55
/* 2 for alignment, 14 for ethernet, 40 for TCP/IP */
56
if_maxlinkhdr = 2 + 14 + 40;
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);
69
* This shouldn't be needed since the modem is blocking and
70
* we don't expect any signals, but what the hell..
81
/* This should succeed most of the time */
82
ret = send(fd, bptr, n,0);
83
if (ret == n || ret <= 0)
86
/* Didn't write everything, go into the loop */
89
ret = send(fd, bptr+total, n-total,0);
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 */
108
u_char if_inbuff[INBUFF_SIZE];
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));
119
if (if_n == 0 || (errno != EINTR && errno != EAGAIN)) {
122
tty_detached(ttyp, 0);
127
if (*if_inbuff == '0') {
129
if (++ttyp->zeros >= 5)
133
if (*if_inbuff == '1') {
135
if (++ttyp->ones >= 5)
136
tty_detached(ttyp, 0);
140
ttyp->ones = ttyp->zeros = 0;
142
(*ttyp->if_input)(ttyp, if_inbuff, if_n);
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)
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) {
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) {
192
ifs_insque(ifm, ifq->ifs_prev);
197
/* No match, check which queue to put it on */
198
if (so && (so->so_iptos & IPTOS_LOWDELAY)) {
199
ifq = if_fastq.ifq_prev;
202
* Check if this packet is a part of the last
205
if (ifq->ifq_so == so) {
207
ifs_insque(ifm, ifq->ifs_prev);
211
ifq = if_batchq.ifq_prev;
213
/* Create a new doubly linked list for this session */
222
/* Update *_queued */
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);
245
* This prevents us from malloc()ing too many mbufs
248
/* if_start will check towrite */
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.
269
struct mbuf *ifm, *ifqt;
271
DEBUG_CALL("if_start");
274
return; /* Nothing to do */
277
/* check if we can really output */
278
if (!slirp_can_output())
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;
288
/* Nothing on fastq, see if next_m is valid */
289
if (next_m != &if_batchq)
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;
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);
308
/* Update so_queued */
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);