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