VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/slirp/if.c@ 15580

Last change on this file since 15580 was 15472, checked in by vboxsync, 16 years ago

NAT: readability

  • Property svn:eol-style set to native
File size: 6.4 KB
Line 
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
13static 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
21static 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
27void
28if_init(PNATState pData)
29{
30 /* 2 for alignment, 14 for ethernet, 40 for TCP/IP */
31 if_maxlinkhdr = 2 + 14 + 40;
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 */
56void
57if_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
119diddit:
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 */
170void
171if_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 return;
185
186 /*
187 * See which queue to get next packet from
188 * If there's something in the fastq, select it immediately
189 */
190 if (if_fastq.ifq_next != &if_fastq)
191 ifm = if_fastq.ifq_next;
192 else
193 {
194 /* Nothing on fastq, see if next_m is valid */
195 if (next_m != &if_batchq)
196 ifm = next_m;
197 else
198 ifm = if_batchq.ifq_next;
199
200 /* Set which packet to send on next iteration */
201 next_m = ifm->ifq_next;
202 }
203 /* Remove it from the queue */
204 ifqt = ifm->ifq_prev;
205 remque(pData, ifm);
206 --if_queued;
207
208 /* If there are more packets for this session, re-queue them */
209 if (ifm->ifs_next != /* ifm->ifs_prev != */ ifm)
210 {
211 insque(pData, ifm->ifs_next, ifqt);
212 ifs_remque(ifm);
213 }
214
215 /* Update so_queued */
216 if (ifm->ifq_so)
217 {
218 if (--ifm->ifq_so->so_queued == 0)
219 /* If there's no more queued, reset nqueued */
220 ifm->ifq_so->so_nqueued = 0;
221 }
222
223 /* Encapsulate the packet for sending */
224 if_encap(pData, (const uint8_t *)ifm->m_data, ifm->m_len);
225
226 m_free(pData, ifm);
227
228 if (!if_queued)
229 return;
230 }
231}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette