VirtualBox

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

Last change on this file since 21610 was 20712, checked in by vboxsync, 16 years ago

DrvNat,slirp: simplify statistics and deregister them.

  • Property svn:eol-style set to native
File size: 5.9 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/* Done as a macro in socket.h */
11/* int
12 * sbspace(struct sockbuff *sb)
13 * {
14 * return SB_DATALEN - sb->sb_cc;
15 * }
16 */
17
18void
19sbfree(struct sbuf *sb)
20{
21 RTMemFree(sb->sb_data);
22}
23
24void
25sbdrop(struct sbuf *sb, int num)
26{
27 /*
28 * We can only drop how much we have
29 * This should never succeed
30 */
31 if (num > sb->sb_cc)
32 num = sb->sb_cc;
33 sb->sb_cc -= num;
34 sb->sb_rptr += num;
35 if (sb->sb_rptr >= sb->sb_data + sb->sb_datalen)
36 sb->sb_rptr -= sb->sb_datalen;
37
38}
39
40void
41sbreserve(PNATState pData, struct sbuf *sb, int size)
42{
43 if (sb->sb_data)
44 {
45 /* Already alloced, realloc if necessary */
46 if (sb->sb_datalen != size)
47 {
48 sb->sb_wptr =
49 sb->sb_rptr =
50 sb->sb_data = (char *)RTMemRealloc(sb->sb_data, size);
51 sb->sb_cc = 0;
52 if (sb->sb_wptr)
53 sb->sb_datalen = size;
54 else
55 sb->sb_datalen = 0;
56 }
57 }
58 else
59 {
60 sb->sb_wptr = sb->sb_rptr = sb->sb_data = (char *)RTMemAlloc(size);
61 sb->sb_cc = 0;
62 if (sb->sb_wptr)
63 sb->sb_datalen = size;
64 else
65 sb->sb_datalen = 0;
66 }
67}
68
69/*
70 * Try and write() to the socket, whatever doesn't get written
71 * append to the buffer... for a host with a fast net connection,
72 * this prevents an unnecessary copy of the data
73 * (the socket is non-blocking, so we won't hang)
74 */
75void
76sbappend(PNATState pData, struct socket *so, struct mbuf *m)
77{
78 int ret = 0;
79
80 STAM_PROFILE_START(&pData->StatIOSBAppend_pf, a);
81 DEBUG_CALL("sbappend");
82 DEBUG_ARG("so = %lx", (long)so);
83 DEBUG_ARG("m = %lx", (long)m);
84 DEBUG_ARG("m->m_len = %d", m->m_len);
85
86 STAM_COUNTER_RESET(&pData->StatIOSBAppend);
87 STAM_COUNTER_RESET(&pData->StatIOSBAppend_zm);
88 STAM_COUNTER_RESET(&pData->StatIOSBAppend_wa);
89 STAM_COUNTER_RESET(&pData->StatIOSBAppend_wf);
90 STAM_COUNTER_RESET(&pData->StatIOSBAppend_wp);
91
92 STAM_COUNTER_INC(&pData->StatIOSBAppend);
93 /* Shouldn't happen, but... e.g. foreign host closes connection */
94 if (m->m_len <= 0)
95 {
96 STAM_COUNTER_INC(&pData->StatIOSBAppend_zm);
97 goto done;
98 }
99
100 /*
101 * If there is urgent data, call sosendoob
102 * if not all was sent, sowrite will take care of the rest
103 * (The rest of this function is just an optimisation)
104 */
105 if (so->so_urgc)
106 {
107 sbappendsb(pData, &so->so_rcv, m);
108 m_free(pData, m);
109 sosendoob(so);
110 return;
111 }
112
113 /*
114 * We only write if there's nothing in the buffer,
115 * ottherwise it'll arrive out of order, and hence corrupt
116 */
117 if (!so->so_rcv.sb_cc)
118 ret = send(so->s, m->m_data, m->m_len, 0);
119
120 if (ret <= 0)
121 {
122 STAM_COUNTER_INC(&pData->StatIOSBAppend_wf);
123 /*
124 * Nothing was written
125 * It's possible that the socket has closed, but
126 * we don't need to check because if it has closed,
127 * it will be detected in the normal way by soread()
128 */
129 sbappendsb(pData, &so->so_rcv, m);
130 STAM_PROFILE_STOP(&pData->StatIOSBAppend_pf_wf, a);
131 goto done;
132 }
133 else if (ret != m->m_len)
134 {
135 STAM_COUNTER_INC(&pData->StatIOSBAppend_wp);
136 /*
137 * Something was written, but not everything..
138 * sbappendsb the rest
139 */
140 m->m_len -= ret;
141 m->m_data += ret;
142 sbappendsb(pData, &so->so_rcv, m);
143 STAM_PROFILE_STOP(&pData->StatIOSBAppend_pf_wp, a);
144 goto done;
145 } /* else */
146 /* Whatever happened, we free the mbuf */
147 STAM_COUNTER_INC(&pData->StatIOSBAppend_wa);
148 STAM_PROFILE_STOP(&pData->StatIOSBAppend_pf_wa, a);
149done:
150 m_free(pData, m);
151}
152
153/*
154 * Copy the data from m into sb
155 * The caller is responsible to make sure there's enough room
156 */
157void
158sbappendsb(PNATState pData, struct sbuf *sb, struct mbuf *m)
159{
160 int len, n, nn;
161
162 len = m->m_len;
163
164 STAM_COUNTER_RESET(&pData->StatIOSBAppendSB);
165 STAM_COUNTER_RESET(&pData->StatIOSBAppendSB_w_l_r);
166 STAM_COUNTER_RESET(&pData->StatIOSBAppendSB_w_ge_r);
167 STAM_COUNTER_RESET(&pData->StatIOSBAppendSB_w_alter);
168
169 STAM_COUNTER_INC(&pData->StatIOSBAppendSB);
170 if (sb->sb_wptr < sb->sb_rptr)
171 {
172 STAM_COUNTER_INC(&pData->StatIOSBAppendSB_w_l_r);
173 n = sb->sb_rptr - sb->sb_wptr;
174 if (n > len)
175 n = len;
176 memcpy(sb->sb_wptr, m->m_data, n);
177 }
178 else
179 {
180 STAM_COUNTER_INC(&pData->StatIOSBAppendSB_w_ge_r);
181 /* Do the right edge first */
182 n = sb->sb_data + sb->sb_datalen - sb->sb_wptr;
183 if (n > len)
184 n = len;
185 memcpy(sb->sb_wptr, m->m_data, n);
186 len -= n;
187 if (len)
188 {
189 /* Now the left edge */
190 nn = sb->sb_rptr - sb->sb_data;
191 if (nn > len)
192 nn = len;
193 memcpy(sb->sb_data, m->m_data+n, nn);
194 n += nn;
195 }
196 }
197
198 sb->sb_cc += n;
199 sb->sb_wptr += n;
200 if (sb->sb_wptr >= sb->sb_data + sb->sb_datalen)
201 {
202 STAM_COUNTER_INC(&pData->StatIOSBAppendSB_w_alter);
203 sb->sb_wptr -= sb->sb_datalen;
204 }
205}
206
207/*
208 * Copy data from sbuf to a normal, straight buffer
209 * Don't update the sbuf rptr, this will be
210 * done in sbdrop when the data is acked
211 */
212void
213sbcopy(struct sbuf *sb, int off, int len, char *to)
214{
215 char *from;
216
217 from = sb->sb_rptr + off;
218 if (from >= sb->sb_data + sb->sb_datalen)
219 from -= sb->sb_datalen;
220
221 if (from < sb->sb_wptr)
222 {
223 if (len > sb->sb_cc)
224 len = sb->sb_cc;
225 memcpy(to, from, len);
226 }
227 else
228 {
229 /* re-use off */
230 off = (sb->sb_data + sb->sb_datalen) - from;
231 if (off > len)
232 off = len;
233 memcpy(to, from, off);
234 len -= off;
235 if (len)
236 memcpy(to+off, sb->sb_data, len);
237 }
238}
239
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