VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/slirp/mbuf.c@ 9113

最後變更 在這個檔案從9113是 1076,由 vboxsync 提交於 18 年 前

Removed tons of ifdef VBOX conditionals to make slirp readable again

  • 屬性 svn:eol-style 設為 native
檔案大小: 4.7 KB
 
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/*
9 * mbuf's in SLiRP are much simpler than the real mbufs in
10 * FreeBSD. They are fixed size, determined by the MTU,
11 * so that one whole packet can fit. Mbuf's cannot be
12 * chained together. If there's more data than the mbuf
13 * could hold, an external malloced buffer is pointed to
14 * by m_ext (and the data pointers) and M_EXT is set in
15 * the flags
16 */
17
18#include <slirp.h>
19
20
21void
22m_init(PNATState pData)
23{
24 m_freelist.m_next = m_freelist.m_prev = &m_freelist;
25 m_usedlist.m_next = m_usedlist.m_prev = &m_usedlist;
26 mbuf_alloced = 0;
27 msize_init(pData);
28}
29
30void
31msize_init(PNATState pData)
32{
33 /*
34 * Find a nice value for msize
35 * XXX if_maxlinkhdr already in mtu
36 */
37 msize = (if_mtu>if_mru?if_mtu:if_mru) +
38 if_maxlinkhdr + sizeof(struct m_hdr ) + 6;
39}
40
41/*
42 * Get an mbuf from the free list, if there are none
43 * malloc one
44 *
45 * Because fragmentation can occur if we alloc new mbufs and
46 * free old mbufs, we mark all mbufs above mbuf_thresh as M_DOFREE,
47 * which tells m_free to actually free() it
48 */
49struct mbuf *
50m_get(PNATState pData)
51{
52 register struct mbuf *m;
53 int flags = 0;
54
55 DEBUG_CALL("m_get");
56
57 if (m_freelist.m_next == &m_freelist) {
58 m = (struct mbuf *)malloc(msize);
59 if (m == NULL) goto end_error;
60 mbuf_alloced++;
61 if (mbuf_alloced > mbuf_thresh)
62 flags = M_DOFREE;
63 if (mbuf_alloced > mbuf_max)
64 mbuf_max = mbuf_alloced;
65 } else {
66 m = m_freelist.m_next;
67 remque(pData, m);
68 }
69
70 /* Insert it in the used list */
71 insque(pData, m,&m_usedlist);
72 m->m_flags = (flags | M_USEDLIST);
73
74 /* Initialise it */
75 m->m_size = msize - sizeof(struct m_hdr);
76 m->m_data = m->m_dat;
77 m->m_len = 0;
78 m->m_nextpkt = 0;
79 m->m_prevpkt = 0;
80end_error:
81 DEBUG_ARG("m = %lx", (long )m);
82 return m;
83}
84
85void
86m_free(PNATState pData, struct mbuf *m)
87{
88
89 DEBUG_CALL("m_free");
90 DEBUG_ARG("m = %lx", (long )m);
91
92 if(m) {
93 /* Remove from m_usedlist */
94 if (m->m_flags & M_USEDLIST)
95 remque(pData, m);
96
97 /* If it's M_EXT, free() it */
98 if (m->m_flags & M_EXT)
99 free(m->m_ext);
100
101 /*
102 * Either free() it or put it on the free list
103 */
104 if (m->m_flags & M_DOFREE) {
105 u32ptr_done(pData, ptr_to_u32(pData, m), m);
106 free(m);
107 mbuf_alloced--;
108 } else if ((m->m_flags & M_FREELIST) == 0) {
109 insque(pData, m,&m_freelist);
110 m->m_flags = M_FREELIST; /* Clobber other flags */
111 }
112 } /* if(m) */
113}
114
115/*
116 * Copy data from one mbuf to the end of
117 * the other.. if result is too big for one mbuf, malloc()
118 * an M_EXT data segment
119 */
120void
121m_cat(PNATState pData, register struct mbuf *m, register struct mbuf *n)
122{
123 /*
124 * If there's no room, realloc
125 */
126 if (M_FREEROOM(m) < n->m_len)
127 m_inc(m,m->m_size+MINCSIZE);
128
129 memcpy(m->m_data+m->m_len, n->m_data, n->m_len);
130 m->m_len += n->m_len;
131
132 m_free(pData, n);
133}
134
135
136/* make m size bytes large */
137void
138m_inc(m, size)
139 struct mbuf *m;
140 int size;
141{
142 int datasize;
143
144 /* some compiles throw up on gotos. This one we can fake. */
145 if(m->m_size>size) return;
146
147 if (m->m_flags & M_EXT) {
148 datasize = m->m_data - m->m_ext;
149 m->m_ext = (char *)realloc(m->m_ext,size);
150/* if (m->m_ext == NULL)
151 * return (struct mbuf *)NULL;
152 */
153 m->m_data = m->m_ext + datasize;
154 } else {
155 char *dat;
156 datasize = m->m_data - m->m_dat;
157 dat = (char *)malloc(size);
158/* if (dat == NULL)
159 * return (struct mbuf *)NULL;
160 */
161 memcpy(dat, m->m_dat, m->m_size);
162
163 m->m_ext = dat;
164 m->m_data = m->m_ext + datasize;
165 m->m_flags |= M_EXT;
166 }
167
168 m->m_size = size;
169
170}
171
172
173
174void
175m_adj(m, len)
176 struct mbuf *m;
177 int len;
178{
179 if (m == NULL)
180 return;
181 if (len >= 0) {
182 /* Trim from head */
183 m->m_data += len;
184 m->m_len -= len;
185 } else {
186 /* Trim from tail */
187 len = -len;
188 m->m_len -= len;
189 }
190}
191
192
193/*
194 * Copy len bytes from m, starting off bytes into n
195 */
196int
197m_copy(n, m, off, len)
198 struct mbuf *n, *m;
199 int off, len;
200{
201 if (len > M_FREEROOM(n))
202 return -1;
203
204 memcpy((n->m_data + n->m_len), (m->m_data + off), len);
205 n->m_len += len;
206 return 0;
207}
208
209
210/*
211 * Given a pointer into an mbuf, return the mbuf
212 * XXX This is a kludge, I should eliminate the need for it
213 * Fortunately, it's not used often
214 */
215struct mbuf *
216dtom(PNATState pData, void *dat)
217{
218 struct mbuf *m;
219
220 DEBUG_CALL("dtom");
221 DEBUG_ARG("dat = %lx", (long )dat);
222
223 /* bug corrected for M_EXT buffers */
224 for (m = m_usedlist.m_next; m != &m_usedlist; m = m->m_next) {
225 if (m->m_flags & M_EXT) {
226 if( (char *)dat>=m->m_ext && (char *)dat<(m->m_ext + m->m_size) )
227 return m;
228 } else {
229 if( (char *)dat >= m->m_dat && (char *)dat<(m->m_dat + m->m_size) )
230 return m;
231 }
232 }
233
234 DEBUG_ERROR((dfd, "dtom failed"));
235
236 return (struct mbuf *)0;
237}
238
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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