VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/slirp/sbuf.c@ 30426

Last change on this file since 30426 was 30426, checked in by vboxsync, 15 years ago

NAT: small performance enhancement. (allocate mbuf if it's required).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.9 KB
Line 
1/* $Id: sbuf.c 30426 2010-06-24 12:25:24Z vboxsync $ */
2/** @file
3 * NAT - sbuf implemenation.
4 */
5
6/*
7 * Copyright (C) 2006-2010 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18/*
19 * This code is based on:
20 *
21 * Copyright (c) 1995 Danny Gasparovski.
22 *
23 * Please read the file COPYRIGHT for the
24 * terms and conditions of the copyright.
25 */
26
27#include <slirp.h>
28#ifndef VBOX_WITH_SLIRP_BSD_SBUF
29
30/* Done as a macro in socket.h */
31/* int
32 * sbspace(struct sockbuff *sb)
33 * {
34 * return SB_DATALEN - sb->sb_cc;
35 * }
36 */
37
38void
39sbfree(struct sbuf *sb)
40{
41 /*
42 * Catch double frees. Actually tcp_close() already filters out listening sockets
43 * passing NULL.
44 */
45 Assert((sb->sb_data));
46
47 /*
48 * Don't call RTMemFree() for an already freed buffer, the EFence could complain
49 */
50 if (sb->sb_data)
51 {
52 RTMemFree(sb->sb_data);
53 sb->sb_data = NULL;
54 }
55}
56
57void
58sbdrop(struct sbuf *sb, int num)
59{
60 /*
61 * We can only drop how much we have
62 * This should never succeed
63 */
64 if (num > sb->sb_cc)
65 num = sb->sb_cc;
66 sb->sb_cc -= num;
67 sb->sb_rptr += num;
68 if (sb->sb_rptr >= sb->sb_data + sb->sb_datalen)
69 sb->sb_rptr -= sb->sb_datalen;
70
71}
72
73void
74sbreserve(PNATState pData, struct sbuf *sb, int size)
75{
76 if (sb->sb_data)
77 {
78 /* Already alloced, realloc if necessary */
79 if (sb->sb_datalen != size)
80 {
81 sb->sb_wptr =
82 sb->sb_rptr =
83 sb->sb_data = (char *)RTMemRealloc(sb->sb_data, size);
84 sb->sb_cc = 0;
85 if (sb->sb_wptr)
86 sb->sb_datalen = size;
87 else
88 sb->sb_datalen = 0;
89 }
90 }
91 else
92 {
93 sb->sb_wptr = sb->sb_rptr = sb->sb_data = (char *)RTMemAlloc(size);
94 sb->sb_cc = 0;
95 if (sb->sb_wptr)
96 sb->sb_datalen = size;
97 else
98 sb->sb_datalen = 0;
99 }
100}
101
102/*
103 * Try and write() to the socket, whatever doesn't get written
104 * append to the buffer... for a host with a fast net connection,
105 * this prevents an unnecessary copy of the data
106 * (the socket is non-blocking, so we won't hang)
107 */
108void
109sbappend(PNATState pData, struct socket *so, struct mbuf *m)
110{
111 int ret = 0;
112 int mlen = 0;
113 caddr_t buf = NULL;
114
115 STAM_PROFILE_START(&pData->StatIOSBAppend_pf, a);
116 DEBUG_CALL("sbappend");
117 DEBUG_ARG("so = %lx", (long)so);
118 DEBUG_ARG("m = %lx", (long)m);
119 DEBUG_ARG("m->m_len = %d", m ? m->m_len : 0);
120
121 STAM_COUNTER_INC(&pData->StatIOSBAppend);
122 /* Shouldn't happen, but... e.g. foreign host closes connection */
123 mlen = m_length(m, NULL);
124 if (mlen <= 0)
125 {
126 STAM_COUNTER_INC(&pData->StatIOSBAppend_zm);
127 goto done;
128 }
129
130 /*
131 * If there is urgent data, call sosendoob
132 * if not all was sent, sowrite will take care of the rest
133 * (The rest of this function is just an optimisation)
134 */
135 if (so->so_urgc)
136 {
137 sbappendsb(pData, &so->so_rcv, m);
138 m_freem(pData, m);
139 sosendoob(so);
140 return;
141 }
142
143 /*
144 * We only write if there's nothing in the buffer,
145 * ottherwise it'll arrive out of order, and hence corrupt
146 */
147 if (m->m_next)
148 {
149 buf = RTMemAlloc(mlen);
150 if (buf == NULL)
151 {
152 ret = 0;
153 goto no_sent;
154 }
155 m_copydata(m, 0, mlen, buf);
156 }
157 else
158 buf = mtod(m, char *);
159
160 if(!so->so_rcv.sb_cc)
161 ret = send(so->s, buf, mlen, 0);
162
163 if (m->m_next)
164 RTMemFree(buf);
165no_sent:
166
167 if (ret <= 0)
168 {
169 STAM_COUNTER_INC(&pData->StatIOSBAppend_wf);
170 /*
171 * Nothing was written
172 * It's possible that the socket has closed, but
173 * we don't need to check because if it has closed,
174 * it will be detected in the normal way by soread()
175 */
176 sbappendsb(pData, &so->so_rcv, m);
177 STAM_PROFILE_STOP(&pData->StatIOSBAppend_pf_wf, a);
178 goto done;
179 }
180 else if (ret != mlen)
181 {
182 STAM_COUNTER_INC(&pData->StatIOSBAppend_wp);
183 /*
184 * Something was written, but not everything..
185 * sbappendsb the rest
186 */
187 m_adj(m, ret);
188 sbappendsb(pData, &so->so_rcv, m);
189 STAM_PROFILE_STOP(&pData->StatIOSBAppend_pf_wp, a);
190 goto done;
191 } /* else */
192 /* Whatever happened, we free the mbuf */
193 STAM_COUNTER_INC(&pData->StatIOSBAppend_wa);
194 STAM_PROFILE_STOP(&pData->StatIOSBAppend_pf_wa, a);
195done:
196 m_freem(pData, m);
197}
198
199/*
200 * Copy the data from m into sb
201 * The caller is responsible to make sure there's enough room
202 */
203void
204sbappendsb(PNATState pData, struct sbuf *sb, struct mbuf *m)
205{
206 int len, n, nn;
207
208 len = m_length(m, NULL);
209
210 STAM_COUNTER_INC(&pData->StatIOSBAppendSB);
211 if (sb->sb_wptr < sb->sb_rptr)
212 {
213 STAM_COUNTER_INC(&pData->StatIOSBAppendSB_w_l_r);
214 n = sb->sb_rptr - sb->sb_wptr;
215 if (n > len)
216 n = len;
217 m_copydata(m, 0, n, sb->sb_wptr);
218 }
219 else
220 {
221 STAM_COUNTER_INC(&pData->StatIOSBAppendSB_w_ge_r);
222 /* Do the right edge first */
223 n = sb->sb_data + sb->sb_datalen - sb->sb_wptr;
224 if (n > len)
225 n = len;
226 m_copydata(m, 0, n, sb->sb_wptr);
227 len -= n;
228 if (len)
229 {
230 /* Now the left edge */
231 nn = sb->sb_rptr - sb->sb_data;
232 if (nn > len)
233 nn = len;
234 m_copydata(m, n, nn, sb->sb_data);
235 n += nn;
236 }
237 }
238
239 sb->sb_cc += n;
240 sb->sb_wptr += n;
241 if (sb->sb_wptr >= sb->sb_data + sb->sb_datalen)
242 {
243 STAM_COUNTER_INC(&pData->StatIOSBAppendSB_w_alter);
244 sb->sb_wptr -= sb->sb_datalen;
245 }
246}
247
248/*
249 * Copy data from sbuf to a normal, straight buffer
250 * Don't update the sbuf rptr, this will be
251 * done in sbdrop when the data is acked
252 */
253void
254sbcopy(struct sbuf *sb, int off, int len, char *to)
255{
256 char *from;
257
258 from = sb->sb_rptr + off;
259 if (from >= sb->sb_data + sb->sb_datalen)
260 from -= sb->sb_datalen;
261
262 if (from < sb->sb_wptr)
263 {
264 if (len > sb->sb_cc)
265 len = sb->sb_cc;
266 memcpy(to, from, len);
267 }
268 else
269 {
270 /* re-use off */
271 off = (sb->sb_data + sb->sb_datalen) - from;
272 if (off > len)
273 off = len;
274 memcpy(to, from, off);
275 len -= off;
276 if (len)
277 memcpy(to+off, sb->sb_data, len);
278 }
279}
280#else /* VBOX_WITH_SLIRP_BSD_SBUF */
281void
282sbappend (PNATState pData, struct socket *so, struct mbuf *m)
283{
284 int ret = 0;
285 int mlen = 0;
286 caddr_t buf = NULL;
287
288 STAM_PROFILE_START(&pData->StatIOSBAppend_pf, a);
289 DEBUG_CALL("sbappend");
290 DEBUG_ARG("so = %lx", (long)so);
291 DEBUG_ARG("m = %lx", (long)m);
292 DEBUG_ARG("m->m_len = %d", m ? m->m_len : 0);
293
294 STAM_COUNTER_INC(&pData->StatIOSBAppend);
295 mlen = m_length(m, NULL);
296 if (mlen <= 0)
297 {
298 STAM_COUNTER_INC(&pData->StatIOSBAppend_zm);
299 goto done;
300 }
301
302 /*
303 * We only write if there's nothing in the buffer,
304 * ottherwise it'll arrive out of order, and hence corrupt
305 */
306 buf = RTMemAlloc(mlen);
307 if (buf == NULL)
308 {
309 ret = 0;
310 goto no_sent;
311 }
312 m_copydata(m, 0, mlen, buf);
313
314 /*
315 * If there is urgent data, call sosendoob
316 * if not all was sent, sowrite will take care of the rest
317 * (The rest of this function is just an optimisation)
318 */
319 if (so->so_urgc)
320 {
321 sbuf_bcpy(&so->so_rcv, buf, mlen);
322 RTMemFree(buf);
323 m_free(pData, m);
324 sosendoob(so);
325 return;
326 }
327
328 if(!sbuf_len(&so->so_rcv))
329 ret = send(so->s, buf, mlen, 0);
330no_sent:
331
332 if (ret <= 0)
333 {
334 STAM_COUNTER_INC(&pData->StatIOSBAppend_wf);
335 /*
336 * Nothing was written
337 * It's possible that the socket has closed, but
338 * we don't need to check because if it has closed,
339 * it will be detected in the normal way by soread()
340 */
341 sbuf_bcpy(&so->so_rcv, buf, mlen);
342 STAM_PROFILE_STOP(&pData->StatIOSBAppend_pf_wf, a);
343 goto done;
344 }
345 else if (ret != mlen)
346 {
347 STAM_COUNTER_INC(&pData->StatIOSBAppend_wp);
348 /*
349 * Something was written, but not everything..
350 * sbappendsb the rest
351 */
352 sbuf_bcpy(&so->so_rcv, &buf[ret + 1], mlen - ret);
353 STAM_PROFILE_STOP(&pData->StatIOSBAppend_pf_wp, a);
354 goto done;
355 } /* else */
356 /* Whatever happened, we free the mbuf */
357 STAM_COUNTER_INC(&pData->StatIOSBAppend_wa);
358 STAM_PROFILE_STOP(&pData->StatIOSBAppend_pf_wa, a);
359done:
360 RTMemFree(buf);
361 m_freem(pData, m);
362}
363#endif
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