|
|
1.1 root 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:
1.1.1.2 ! root 10: size_t if_mtu, if_mru;
1.1 root 11: int if_comp;
12: int if_maxlinkhdr;
1.1.1.2 ! root 13: int if_queued = 0; /* Number of packets queued so far */
! 14: int if_thresh = 10; /* Number of packets queued before we start sending
1.1 root 15: * (to prevent allocing too many mbufs) */
16:
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 */
20:
21: #define ifs_init(ifm) ((ifm)->ifs_next = (ifm)->ifs_prev = (ifm))
22:
23: void
24: ifs_insque(ifm, ifmhead)
25: struct mbuf *ifm, *ifmhead;
26: {
27: ifm->ifs_next = ifmhead->ifs_next;
28: ifmhead->ifs_next = ifm;
29: ifm->ifs_prev = ifmhead;
30: ifm->ifs_next->ifs_prev = ifm;
31: }
32:
33: void
34: ifs_remque(ifm)
35: struct mbuf *ifm;
36: {
37: ifm->ifs_prev->ifs_next = ifm->ifs_next;
38: ifm->ifs_next->ifs_prev = ifm->ifs_prev;
39: }
40:
41: void
42: if_init()
43: {
44: #if 0
45: /*
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
48: */
49: #ifdef USE_PPP
50: if_maxlinkhdr = 48;
51: #else
52: if_maxlinkhdr = 40;
53: #endif
54: #else
55: /* 2 for alignment, 14 for ethernet, 40 for TCP/IP */
56: if_maxlinkhdr = 2 + 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;
65: }
66:
67: #if 0
68: /*
69: * This shouldn't be needed since the modem is blocking and
70: * we don't expect any signals, but what the hell..
71: */
72: inline int
73: writen(fd, bptr, n)
74: int fd;
75: char *bptr;
76: int n;
77: {
78: int ret;
79: int total;
80:
81: /* This should succeed most of the time */
82: ret = send(fd, bptr, n,0);
83: if (ret == n || ret <= 0)
84: return ret;
85:
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;
93: }
94: return total;
95: }
96:
97: /*
98: * if_input - read() the tty, do "top level" processing (ie: check for any escapes),
99: * and pass onto (*ttyp->if_input)
100: *
101: * XXXXX Any zeros arriving by themselves are NOT placed into the arriving packet.
102: */
103: #define INBUFF_SIZE 2048 /* XXX */
104: void
105: if_input(ttyp)
106: struct ttys *ttyp;
107: {
108: u_char if_inbuff[INBUFF_SIZE];
109: int if_n;
110:
111: DEBUG_CALL("if_input");
112: DEBUG_ARG("ttyp = %lx", (long)ttyp);
113:
114: if_n = recv(ttyp->fd, (char *)if_inbuff, INBUFF_SIZE,0);
115:
116: DEBUG_MISC((dfd, " read %d bytes\n", if_n));
117:
118: if (if_n <= 0) {
1.1.1.2 ! root 119: int error = WSAGetLastError();
! 120: if (if_n == 0 || (error != WSAEINTR && error != EAGAIN)) {
1.1 root 121: if (ttyp->up)
122: link_up--;
123: tty_detached(ttyp, 0);
124: }
125: return;
126: }
127: if (if_n == 1) {
128: if (*if_inbuff == '0') {
129: ttyp->ones = 0;
130: if (++ttyp->zeros >= 5)
131: slirp_exit(0);
132: return;
133: }
134: if (*if_inbuff == '1') {
135: ttyp->zeros = 0;
136: if (++ttyp->ones >= 5)
137: tty_detached(ttyp, 0);
138: return;
139: }
140: }
141: ttyp->ones = ttyp->zeros = 0;
142:
143: (*ttyp->if_input)(ttyp, if_inbuff, if_n);
144: }
145: #endif
146:
147: /*
148: * if_output: Queue packet into an output queue.
149: * There are 2 output queue's, if_fastq and if_batchq.
150: * Each output queue is a doubly linked list of double linked lists
151: * of mbufs, each list belonging to one "session" (socket). This
152: * way, we can output packets fairly by sending one packet from each
153: * session, instead of all the packets from one session, then all packets
154: * from the next session, etc. Packets on the if_fastq get absolute
155: * priority, but if one session hogs the link, it gets "downgraded"
156: * to the batchq until it runs out of packets, then it'll return
157: * to the fastq (eg. if the user does an ls -alR in a telnet session,
158: * it'll temporarily get downgraded to the batchq)
159: */
160: void
161: if_output(so, ifm)
162: struct socket *so;
163: struct mbuf *ifm;
164: {
165: struct mbuf *ifq;
166: int on_fastq = 1;
167:
168: DEBUG_CALL("if_output");
169: DEBUG_ARG("so = %lx", (long)so);
170: DEBUG_ARG("ifm = %lx", (long)ifm);
171:
172: /*
173: * First remove the mbuf from m_usedlist,
174: * since we're gonna use m_next and m_prev ourselves
175: * XXX Shouldn't need this, gotta change dtom() etc.
176: */
177: if (ifm->m_flags & M_USEDLIST) {
178: remque(ifm);
179: ifm->m_flags &= ~M_USEDLIST;
180: }
181:
182: /*
183: * See if there's already a batchq list for this session.
184: * This can include an interactive session, which should go on fastq,
185: * but gets too greedy... hence it'll be downgraded from fastq to batchq.
186: * We mustn't put this packet back on the fastq (or we'll send it out of order)
187: * XXX add cache here?
188: */
189: for (ifq = if_batchq.ifq_prev; ifq != &if_batchq; ifq = ifq->ifq_prev) {
190: if (so == ifq->ifq_so) {
191: /* A match! */
192: ifm->ifq_so = so;
193: ifs_insque(ifm, ifq->ifs_prev);
194: goto diddit;
195: }
196: }
197:
198: /* No match, check which queue to put it on */
199: if (so && (so->so_iptos & IPTOS_LOWDELAY)) {
200: ifq = if_fastq.ifq_prev;
201: on_fastq = 1;
202: /*
203: * Check if this packet is a part of the last
204: * packet's session
205: */
206: if (ifq->ifq_so == so) {
207: ifm->ifq_so = so;
208: ifs_insque(ifm, ifq->ifs_prev);
209: goto diddit;
210: }
211: } else
212: ifq = if_batchq.ifq_prev;
213:
214: /* Create a new doubly linked list for this session */
215: ifm->ifq_so = so;
216: ifs_init(ifm);
217: insque(ifm, ifq);
218:
219: diddit:
220: ++if_queued;
221:
222: if (so) {
223: /* Update *_queued */
224: so->so_queued++;
225: so->so_nqueued++;
226: /*
227: * Check if the interactive session should be downgraded to
228: * the batchq. A session is downgraded if it has queued 6
229: * packets without pausing, and at least 3 of those packets
230: * have been sent over the link
231: * (XXX These are arbitrary numbers, probably not optimal..)
232: */
233: if (on_fastq && ((so->so_nqueued >= 6) &&
234: (so->so_nqueued - so->so_queued) >= 3)) {
235:
236: /* Remove from current queue... */
237: remque(ifm->ifs_next);
238:
239: /* ...And insert in the new. That'll teach ya! */
240: insque(ifm->ifs_next, &if_batchq);
241: }
242: }
243:
244: #ifndef FULL_BOLT
245: /*
246: * This prevents us from malloc()ing too many mbufs
247: */
248: if (link_up) {
249: /* if_start will check towrite */
250: if_start();
251: }
252: #endif
253: }
254:
255: /*
256: * Send a packet
257: * We choose a packet based on it's position in the output queues;
258: * If there are packets on the fastq, they are sent FIFO, before
259: * everything else. Otherwise we choose the first packet from the
260: * batchq and send it. the next packet chosen will be from the session
261: * after this one, then the session after that one, and so on.. So,
262: * for example, if there are 3 ftp session's fighting for bandwidth,
263: * one packet will be sent from the first session, then one packet
264: * from the second session, then one packet from the third, then back
265: * to the first, etc. etc.
266: */
267: void
268: if_start(void)
269: {
270: struct mbuf *ifm, *ifqt;
271:
272: DEBUG_CALL("if_start");
273:
274: if (if_queued == 0)
275: return; /* Nothing to do */
276:
277: again:
278: /* check if we can really output */
279: if (!slirp_can_output())
280: return;
281:
282: /*
283: * See which queue to get next packet from
284: * If there's something in the fastq, select it immediately
285: */
286: if (if_fastq.ifq_next != &if_fastq) {
287: ifm = if_fastq.ifq_next;
288: } else {
289: /* Nothing on fastq, see if next_m is valid */
290: if (next_m != &if_batchq)
291: ifm = next_m;
292: else
293: ifm = if_batchq.ifq_next;
294:
295: /* Set which packet to send on next iteration */
296: next_m = ifm->ifq_next;
297: }
298: /* Remove it from the queue */
299: ifqt = ifm->ifq_prev;
300: remque(ifm);
301: --if_queued;
302:
303: /* If there are more packets for this session, re-queue them */
304: if (ifm->ifs_next != /* ifm->ifs_prev != */ ifm) {
305: insque(ifm->ifs_next, ifqt);
306: ifs_remque(ifm);
307: }
308:
309: /* Update so_queued */
310: if (ifm->ifq_so) {
311: if (--ifm->ifq_so->so_queued == 0)
312: /* If there's no more queued, reset nqueued */
313: ifm->ifq_so->so_nqueued = 0;
314: }
315:
316: /* Encapsulate the packet for sending */
317: if_encap((uint8_t*)ifm->m_data, ifm->m_len);
318:
319: m_free(ifm);
320:
321: if (if_queued)
322: goto again;
323: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.