VirtualBox

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

Last change on this file since 15229 was 14951, checked in by vboxsync, 16 years ago

NAT: hopefully fixed Windows host

  • Property svn:eol-style set to native
File size: 7.0 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 remque(pData, ifm);
73 ifm->m_flags &= ~M_USEDLIST;
74 }
75
76 /*
77 * See if there's already a batchq list for this session.
78 * This can include an interactive session, which should go on fastq,
79 * but gets too greedy... hence it'll be downgraded from fastq to batchq.
80 * We mustn't put this packet back on the fastq (or we'll send it out of order)
81 * XXX add cache here?
82 */
83 for (ifq = if_batchq.ifq_prev; ifq != &if_batchq; ifq = ifq->ifq_prev) {
84 if (so == ifq->ifq_so) {
85 /* A match! */
86 ifm->ifq_so = so;
87 ifs_insque(ifm, ifq->ifs_prev);
88 goto diddit;
89 }
90 }
91
92 /* No match, check which queue to put it on */
93 if (so && (so->so_iptos & IPTOS_LOWDELAY)) {
94 ifq = if_fastq.ifq_prev;
95 on_fastq = 1;
96 /*
97 * Check if this packet is a part of the last
98 * packet's session
99 */
100 if (ifq->ifq_so == so) {
101 ifm->ifq_so = so;
102 ifs_insque(ifm, ifq->ifs_prev);
103 goto diddit;
104 }
105 } else
106 ifq = if_batchq.ifq_prev;
107
108 /* Create a new doubly linked list for this session */
109 ifm->ifq_so = so;
110 ifs_init(ifm);
111 insque(pData, ifm, ifq);
112
113diddit:
114 ++if_queued;
115
116 if (so) {
117 /* Update *_queued */
118 so->so_queued++;
119 so->so_nqueued++;
120 /*
121 * Check if the interactive session should be downgraded to
122 * the batchq. A session is downgraded if it has queued 6
123 * packets without pausing, and at least 3 of those packets
124 * have been sent over the link
125 * (XXX These are arbitrary numbers, probably not optimal..)
126 */
127 if (on_fastq && ((so->so_nqueued >= 6) &&
128 (so->so_nqueued - so->so_queued) >= 3)) {
129
130 /* Remove from current queue... */
131 remque(pData, ifm->ifs_next);
132
133 /* ...And insert in the new. That'll teach ya! */
134 insque(pData, ifm->ifs_next, &if_batchq);
135 }
136 }
137
138#ifndef FULL_BOLT
139 /*
140 * This prevents us from malloc()ing too many mbufs
141 */
142 if (link_up) {
143 /* if_start will check towrite */
144 if_start(pData);
145 }
146#endif
147}
148
149/*
150 * Send a packet
151 * We choose a packet based on it's position in the output queues;
152 * If there are packets on the fastq, they are sent FIFO, before
153 * everything else. Otherwise we choose the first packet from the
154 * batchq and send it. the next packet chosen will be from the session
155 * after this one, then the session after that one, and so on.. So,
156 * for example, if there are 3 ftp session's fighting for bandwidth,
157 * one packet will be sent from the first session, then one packet
158 * from the second session, then one packet from the third, then back
159 * to the first, etc. etc.
160 */
161void
162if_start(PNATState pData)
163{
164 struct mbuf *ifm, *ifqt;
165
166 DEBUG_CALL("if_start");
167
168 if (if_queued == 0)
169 return; /* Nothing to do */
170
171 again:
172 /* check if we can really output */
173 if (!slirp_can_output(pData->pvUser))
174 return;
175
176 /*
177 * See which queue to get next packet from
178 * If there's something in the fastq, select it immediately
179 */
180 if (if_fastq.ifq_next != &if_fastq) {
181 ifm = if_fastq.ifq_next;
182 } else {
183 /* Nothing on fastq, see if next_m is valid */
184 if (next_m != &if_batchq)
185 ifm = next_m;
186 else
187 ifm = if_batchq.ifq_next;
188
189 /* Set which packet to send on next iteration */
190 next_m = ifm->ifq_next;
191 }
192 /* Remove it from the queue */
193 ifqt = ifm->ifq_prev;
194 remque(pData, ifm);
195 --if_queued;
196
197 /* If there are more packets for this session, re-queue them */
198 if (ifm->ifs_next != /* ifm->ifs_prev != */ ifm) {
199 insque(pData, ifm->ifs_next, ifqt);
200 ifs_remque(ifm);
201 }
202
203 /* Update so_queued */
204 if (ifm->ifq_so) {
205 if (--ifm->ifq_so->so_queued == 0)
206 /* If there's no more queued, reset nqueued */
207 ifm->ifq_so->so_nqueued = 0;
208 }
209
210 /* Encapsulate the packet for sending */
211 if_encap(pData, (const uint8_t *)ifm->m_data, ifm->m_len);
212
213 m_free(pData, ifm);
214
215 if (if_queued)
216 goto again;
217}
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