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 |
|
---|
11 | #define ifs_init(ifm) ((ifm)->ifs_next = (ifm)->ifs_prev = (ifm))
|
---|
12 |
|
---|
13 | static void ifs_insque(struct mbuf *ifm, struct mbuf *ifmhead)
|
---|
14 | {
|
---|
15 | ifm->ifs_next = ifmhead->ifs_next;
|
---|
16 | ifmhead->ifs_next = ifm;
|
---|
17 | ifm->ifs_prev = ifmhead;
|
---|
18 | ifm->ifs_next->ifs_prev = ifm;
|
---|
19 | }
|
---|
20 |
|
---|
21 | static void ifs_remque(struct mbuf *ifm)
|
---|
22 | {
|
---|
23 | ifm->ifs_prev->ifs_next = ifm->ifs_next;
|
---|
24 | ifm->ifs_next->ifs_prev = ifm->ifs_prev;
|
---|
25 | }
|
---|
26 |
|
---|
27 | void
|
---|
28 | if_init(PNATState pData)
|
---|
29 | {
|
---|
30 | /* 14 for ethernet */
|
---|
31 | if_maxlinkhdr = 14;
|
---|
32 | if_queued = 0;
|
---|
33 | if_thresh = 10;
|
---|
34 | if_mtu = 1500;
|
---|
35 | if_mru = 1500;
|
---|
36 | if_comp = IF_AUTOCOMP;
|
---|
37 | if_fastq.ifq_next = if_fastq.ifq_prev = &if_fastq;
|
---|
38 | if_batchq.ifq_next = if_batchq.ifq_prev = &if_batchq;
|
---|
39 | /* sl_compress_init(&comp_s); */
|
---|
40 | next_m = &if_batchq;
|
---|
41 | }
|
---|
42 |
|
---|
43 | /*
|
---|
44 | * if_output: Queue packet into an output queue.
|
---|
45 | * There are 2 output queue's, if_fastq and if_batchq.
|
---|
46 | * Each output queue is a doubly linked list of double linked lists
|
---|
47 | * of mbufs, each list belonging to one "session" (socket). This
|
---|
48 | * way, we can output packets fairly by sending one packet from each
|
---|
49 | * session, instead of all the packets from one session, then all packets
|
---|
50 | * from the next session, etc. Packets on the if_fastq get absolute
|
---|
51 | * priority, but if one session hogs the link, it gets "downgraded"
|
---|
52 | * to the batchq until it runs out of packets, then it'll return
|
---|
53 | * to the fastq (eg. if the user does an ls -alR in a telnet session,
|
---|
54 | * it'll temporarily get downgraded to the batchq)
|
---|
55 | */
|
---|
56 | void
|
---|
57 | if_output(PNATState pData, struct socket *so, struct mbuf *ifm)
|
---|
58 | {
|
---|
59 | struct mbuf *ifq;
|
---|
60 | int on_fastq = 1;
|
---|
61 |
|
---|
62 | DEBUG_CALL("if_output");
|
---|
63 | DEBUG_ARG("so = %lx", (long)so);
|
---|
64 | DEBUG_ARG("ifm = %lx", (long)ifm);
|
---|
65 |
|
---|
66 | /*
|
---|
67 | * First remove the mbuf from m_usedlist,
|
---|
68 | * since we're gonna use m_next and m_prev ourselves
|
---|
69 | * XXX Shouldn't need this, gotta change dtom() etc.
|
---|
70 | */
|
---|
71 | if (ifm->m_flags & M_USEDLIST)
|
---|
72 | {
|
---|
73 | remque(pData, ifm);
|
---|
74 | ifm->m_flags &= ~M_USEDLIST;
|
---|
75 | }
|
---|
76 |
|
---|
77 | /*
|
---|
78 | * See if there's already a batchq list for this session.
|
---|
79 | * This can include an interactive session, which should go on fastq,
|
---|
80 | * but gets too greedy... hence it'll be downgraded from fastq to batchq.
|
---|
81 | * We mustn't put this packet back on the fastq (or we'll send it out of order)
|
---|
82 | * XXX add cache here?
|
---|
83 | */
|
---|
84 | for (ifq = if_batchq.ifq_prev; ifq != &if_batchq; ifq = ifq->ifq_prev)
|
---|
85 | {
|
---|
86 | if (so == ifq->ifq_so)
|
---|
87 | {
|
---|
88 | /* A match! */
|
---|
89 | ifm->ifq_so = so;
|
---|
90 | ifs_insque(ifm, ifq->ifs_prev);
|
---|
91 | goto diddit;
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | /* No match, check which queue to put it on */
|
---|
96 | if (so && (so->so_iptos & IPTOS_LOWDELAY))
|
---|
97 | {
|
---|
98 | ifq = if_fastq.ifq_prev;
|
---|
99 | on_fastq = 1;
|
---|
100 | /*
|
---|
101 | * Check if this packet is a part of the last
|
---|
102 | * packet's session
|
---|
103 | */
|
---|
104 | if (ifq->ifq_so == so)
|
---|
105 | {
|
---|
106 | ifm->ifq_so = so;
|
---|
107 | ifs_insque(ifm, ifq->ifs_prev);
|
---|
108 | goto diddit;
|
---|
109 | }
|
---|
110 | }
|
---|
111 | else
|
---|
112 | ifq = if_batchq.ifq_prev;
|
---|
113 |
|
---|
114 | /* Create a new doubly linked list for this session */
|
---|
115 | ifm->ifq_so = so;
|
---|
116 | ifs_init(ifm);
|
---|
117 | insque(pData, ifm, ifq);
|
---|
118 |
|
---|
119 | diddit:
|
---|
120 | ++if_queued;
|
---|
121 |
|
---|
122 | if (so)
|
---|
123 | {
|
---|
124 | /* Update *_queued */
|
---|
125 | so->so_queued++;
|
---|
126 | so->so_nqueued++;
|
---|
127 | /*
|
---|
128 | * Check if the interactive session should be downgraded to
|
---|
129 | * the batchq. A session is downgraded if it has queued 6
|
---|
130 | * packets without pausing, and at least 3 of those packets
|
---|
131 | * have been sent over the link
|
---|
132 | * (XXX These are arbitrary numbers, probably not optimal..)
|
---|
133 | */
|
---|
134 | if (on_fastq
|
---|
135 | && so->so_nqueued >= 6
|
---|
136 | && (so->so_nqueued - so->so_queued) >= 3)
|
---|
137 | {
|
---|
138 | /* Remove from current queue... */
|
---|
139 | remque(pData, ifm->ifs_next);
|
---|
140 |
|
---|
141 | /* ...And insert in the new. That'll teach ya! */
|
---|
142 | insque(pData, ifm->ifs_next, &if_batchq);
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | #ifndef FULL_BOLT
|
---|
147 | /*
|
---|
148 | * This prevents us from malloc()ing too many mbufs
|
---|
149 | */
|
---|
150 | if (link_up)
|
---|
151 | {
|
---|
152 | /* if_start will check towrite */
|
---|
153 | if_start(pData);
|
---|
154 | }
|
---|
155 | #endif
|
---|
156 | }
|
---|
157 |
|
---|
158 | /*
|
---|
159 | * Send a packet
|
---|
160 | * We choose a packet based on it's position in the output queues;
|
---|
161 | * If there are packets on the fastq, they are sent FIFO, before
|
---|
162 | * everything else. Otherwise we choose the first packet from the
|
---|
163 | * batchq and send it. the next packet chosen will be from the session
|
---|
164 | * after this one, then the session after that one, and so on.. So,
|
---|
165 | * for example, if there are 3 ftp session's fighting for bandwidth,
|
---|
166 | * one packet will be sent from the first session, then one packet
|
---|
167 | * from the second session, then one packet from the third, then back
|
---|
168 | * to the first, etc. etc.
|
---|
169 | */
|
---|
170 | void
|
---|
171 | if_start(PNATState pData)
|
---|
172 | {
|
---|
173 | struct mbuf *ifm, *ifqt;
|
---|
174 |
|
---|
175 | DEBUG_CALL("if_start");
|
---|
176 |
|
---|
177 | if (!if_queued)
|
---|
178 | return; /* Nothing to do */
|
---|
179 |
|
---|
180 | for (;;)
|
---|
181 | {
|
---|
182 | /* check if we can really output */
|
---|
183 | if (!slirp_can_output(pData->pvUser))
|
---|
184 | {
|
---|
185 | Log(("if_start: can't send\n"));
|
---|
186 | return;
|
---|
187 | }
|
---|
188 |
|
---|
189 | /*
|
---|
190 | * See which queue to get next packet from
|
---|
191 | * If there's something in the fastq, select it immediately
|
---|
192 | */
|
---|
193 | if (if_fastq.ifq_next != &if_fastq)
|
---|
194 | ifm = if_fastq.ifq_next;
|
---|
195 | else
|
---|
196 | {
|
---|
197 | /* Nothing on fastq, see if next_m is valid */
|
---|
198 | if (next_m != &if_batchq)
|
---|
199 | ifm = next_m;
|
---|
200 | else
|
---|
201 | ifm = if_batchq.ifq_next;
|
---|
202 |
|
---|
203 | /* Set which packet to send on next iteration */
|
---|
204 | next_m = ifm->ifq_next;
|
---|
205 | }
|
---|
206 | /* Remove it from the queue */
|
---|
207 | ifqt = ifm->ifq_prev;
|
---|
208 | remque(pData, ifm);
|
---|
209 | --if_queued;
|
---|
210 |
|
---|
211 | /* If there are more packets for this session, re-queue them */
|
---|
212 | if (ifm->ifs_next != /* ifm->ifs_prev != */ ifm)
|
---|
213 | {
|
---|
214 | insque(pData, ifm->ifs_next, ifqt);
|
---|
215 | ifs_remque(ifm);
|
---|
216 | }
|
---|
217 |
|
---|
218 | /* Update so_queued */
|
---|
219 | if (ifm->ifq_so)
|
---|
220 | {
|
---|
221 | if (--ifm->ifq_so->so_queued == 0)
|
---|
222 | /* If there's no more queued, reset nqueued */
|
---|
223 | ifm->ifq_so->so_nqueued = 0;
|
---|
224 | }
|
---|
225 |
|
---|
226 | if_encap(pData, ETH_P_IP, ifm);
|
---|
227 |
|
---|
228 | if (!if_queued)
|
---|
229 | {
|
---|
230 | slirp_flush_dev(pData->pvUser);
|
---|
231 | return;
|
---|
232 | }
|
---|
233 | }
|
---|
234 | }
|
---|