xref: /freebsd/sys/kern/subr_mchain.c (revision 884a2a699669ec61e2366e3e358342dbc94be24a)
1 /*-
2  * Copyright (c) 2000, 2001 Boris Popov
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the author nor the names of any co-contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/sysctl.h>
36 #include <sys/endian.h>
37 #include <sys/errno.h>
38 #include <sys/mbuf.h>
39 #include <sys/module.h>
40 #include <sys/uio.h>
41 
42 #include <sys/mchain.h>
43 
44 FEATURE(libmchain, "mchain library");
45 
46 MODULE_VERSION(libmchain, 1);
47 
48 #define MBERROR(format, ...) printf("%s(%d): "format, __func__ , \
49 				    __LINE__ , ## __VA_ARGS__)
50 
51 #define MBPANIC(format, ...) printf("%s(%d): "format, __func__ , \
52 				    __LINE__ , ## __VA_ARGS__)
53 
54 /*
55  * Various helper functions
56  */
57 int
58 mb_init(struct mbchain *mbp)
59 {
60 	struct mbuf *m;
61 
62 	m = m_gethdr(M_WAIT, MT_DATA);
63 	m->m_len = 0;
64 	mb_initm(mbp, m);
65 	return 0;
66 }
67 
68 void
69 mb_initm(struct mbchain *mbp, struct mbuf *m)
70 {
71 	bzero(mbp, sizeof(*mbp));
72 	mbp->mb_top = mbp->mb_cur = m;
73 	mbp->mb_mleft = M_TRAILINGSPACE(m);
74 }
75 
76 void
77 mb_done(struct mbchain *mbp)
78 {
79 	if (mbp->mb_top) {
80 		m_freem(mbp->mb_top);
81 		mbp->mb_top = NULL;
82 	}
83 }
84 
85 struct mbuf *
86 mb_detach(struct mbchain *mbp)
87 {
88 	struct mbuf *m;
89 
90 	m = mbp->mb_top;
91 	mbp->mb_top = NULL;
92 	return m;
93 }
94 
95 int
96 mb_fixhdr(struct mbchain *mbp)
97 {
98 	return mbp->mb_top->m_pkthdr.len = m_fixhdr(mbp->mb_top);
99 }
100 
101 /*
102  * Check if object of size 'size' fit to the current position and
103  * allocate new mbuf if not. Advance pointers and increase length of mbuf(s).
104  * Return pointer to the object placeholder or NULL if any error occured.
105  * Note: size should be <= MLEN
106  */
107 caddr_t
108 mb_reserve(struct mbchain *mbp, int size)
109 {
110 	struct mbuf *m, *mn;
111 	caddr_t bpos;
112 
113 	if (size > MLEN)
114 		panic("mb_reserve: size = %d\n", size);
115 	m = mbp->mb_cur;
116 	if (mbp->mb_mleft < size) {
117 		mn = m_get(M_WAIT, MT_DATA);
118 		mbp->mb_cur = m->m_next = mn;
119 		m = mn;
120 		m->m_len = 0;
121 		mbp->mb_mleft = M_TRAILINGSPACE(m);
122 	}
123 	mbp->mb_mleft -= size;
124 	mbp->mb_count += size;
125 	bpos = mtod(m, caddr_t) + m->m_len;
126 	m->m_len += size;
127 	return bpos;
128 }
129 
130 int
131 mb_put_uint8(struct mbchain *mbp, uint8_t x)
132 {
133 	return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM);
134 }
135 
136 int
137 mb_put_uint16be(struct mbchain *mbp, uint16_t x)
138 {
139 	x = htobe16(x);
140 	return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM);
141 }
142 
143 int
144 mb_put_uint16le(struct mbchain *mbp, uint16_t x)
145 {
146 	x = htole16(x);
147 	return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM);
148 }
149 
150 int
151 mb_put_uint32be(struct mbchain *mbp, uint32_t x)
152 {
153 	x = htobe32(x);
154 	return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM);
155 }
156 
157 int
158 mb_put_uint32le(struct mbchain *mbp, uint32_t x)
159 {
160 	x = htole32(x);
161 	return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM);
162 }
163 
164 int
165 mb_put_int64be(struct mbchain *mbp, int64_t x)
166 {
167 	x = htobe64(x);
168 	return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM);
169 }
170 
171 int
172 mb_put_int64le(struct mbchain *mbp, int64_t x)
173 {
174 	x = htole64(x);
175 	return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM);
176 }
177 
178 int
179 mb_put_mem(struct mbchain *mbp, c_caddr_t source, int size, int type)
180 {
181 	struct mbuf *m;
182 	caddr_t dst;
183 	c_caddr_t src;
184 	int cplen, error, mleft, count;
185 	size_t srclen, dstlen;
186 
187 	m = mbp->mb_cur;
188 	mleft = mbp->mb_mleft;
189 
190 	while (size > 0) {
191 		if (mleft == 0) {
192 			if (m->m_next == NULL)
193 				m = m_getm(m, size, M_WAIT, MT_DATA);
194 			else
195 				m = m->m_next;
196 			mleft = M_TRAILINGSPACE(m);
197 			continue;
198 		}
199 		cplen = mleft > size ? size : mleft;
200 		srclen = dstlen = cplen;
201 		dst = mtod(m, caddr_t) + m->m_len;
202 		switch (type) {
203 		    case MB_MCUSTOM:
204 			srclen = size;
205 			dstlen = mleft;
206 			error = mbp->mb_copy(mbp, source, dst, &srclen, &dstlen);
207 			if (error)
208 				return error;
209 			break;
210 		    case MB_MINLINE:
211 			for (src = source, count = cplen; count; count--)
212 				*dst++ = *src++;
213 			break;
214 		    case MB_MSYSTEM:
215 			bcopy(source, dst, cplen);
216 			break;
217 		    case MB_MUSER:
218 			error = copyin(source, dst, cplen);
219 			if (error)
220 				return error;
221 			break;
222 		    case MB_MZERO:
223 			bzero(dst, cplen);
224 			break;
225 		}
226 		size -= srclen;
227 		source += srclen;
228 		m->m_len += dstlen;
229 		mleft -= dstlen;
230 		mbp->mb_count += dstlen;
231 	}
232 	mbp->mb_cur = m;
233 	mbp->mb_mleft = mleft;
234 	return 0;
235 }
236 
237 int
238 mb_put_mbuf(struct mbchain *mbp, struct mbuf *m)
239 {
240 	mbp->mb_cur->m_next = m;
241 	while (m) {
242 		mbp->mb_count += m->m_len;
243 		if (m->m_next == NULL)
244 			break;
245 		m = m->m_next;
246 	}
247 	mbp->mb_mleft = M_TRAILINGSPACE(m);
248 	mbp->mb_cur = m;
249 	return 0;
250 }
251 
252 /*
253  * copies a uio scatter/gather list to an mbuf chain.
254  */
255 int
256 mb_put_uio(struct mbchain *mbp, struct uio *uiop, int size)
257 {
258 	long left;
259 	int mtype, error;
260 
261 	mtype = (uiop->uio_segflg == UIO_SYSSPACE) ? MB_MSYSTEM : MB_MUSER;
262 
263 	while (size > 0 && uiop->uio_resid) {
264 		if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
265 			return EFBIG;
266 		left = uiop->uio_iov->iov_len;
267 		if (left == 0) {
268 			uiop->uio_iov++;
269 			uiop->uio_iovcnt--;
270 			continue;
271 		}
272 		if (left > size)
273 			left = size;
274 		error = mb_put_mem(mbp, uiop->uio_iov->iov_base, left, mtype);
275 		if (error)
276 			return error;
277 		uiop->uio_offset += left;
278 		uiop->uio_resid -= left;
279 		uiop->uio_iov->iov_base =
280 		    (char *)uiop->uio_iov->iov_base + left;
281 		uiop->uio_iov->iov_len -= left;
282 		size -= left;
283 	}
284 	return 0;
285 }
286 
287 /*
288  * Routines for fetching data from an mbuf chain
289  */
290 int
291 md_init(struct mdchain *mdp)
292 {
293 	struct mbuf *m;
294 
295 	m = m_gethdr(M_WAIT, MT_DATA);
296 	m->m_len = 0;
297 	md_initm(mdp, m);
298 	return 0;
299 }
300 
301 void
302 md_initm(struct mdchain *mdp, struct mbuf *m)
303 {
304 	bzero(mdp, sizeof(*mdp));
305 	mdp->md_top = mdp->md_cur = m;
306 	mdp->md_pos = mtod(m, u_char*);
307 }
308 
309 void
310 md_done(struct mdchain *mdp)
311 {
312 	if (mdp->md_top) {
313 		m_freem(mdp->md_top);
314 		mdp->md_top = NULL;
315 	}
316 }
317 
318 /*
319  * Append a separate mbuf chain. It is caller responsibility to prevent
320  * multiple calls to fetch/record routines.
321  */
322 void
323 md_append_record(struct mdchain *mdp, struct mbuf *top)
324 {
325 	struct mbuf *m;
326 
327 	if (mdp->md_top == NULL) {
328 		md_initm(mdp, top);
329 		return;
330 	}
331 	m = mdp->md_top;
332 	while (m->m_nextpkt)
333 		m = m->m_nextpkt;
334 	m->m_nextpkt = top;
335 	top->m_nextpkt = NULL;
336 	return;
337 }
338 
339 /*
340  * Put next record in place of existing
341  */
342 int
343 md_next_record(struct mdchain *mdp)
344 {
345 	struct mbuf *m;
346 
347 	if (mdp->md_top == NULL)
348 		return ENOENT;
349 	m = mdp->md_top->m_nextpkt;
350 	md_done(mdp);
351 	if (m == NULL)
352 		return ENOENT;
353 	md_initm(mdp, m);
354 	return 0;
355 }
356 
357 int
358 md_get_uint8(struct mdchain *mdp, uint8_t *x)
359 {
360 	return md_get_mem(mdp, x, 1, MB_MINLINE);
361 }
362 
363 int
364 md_get_uint16(struct mdchain *mdp, uint16_t *x)
365 {
366 	return md_get_mem(mdp, (caddr_t)x, 2, MB_MINLINE);
367 }
368 
369 int
370 md_get_uint16le(struct mdchain *mdp, uint16_t *x)
371 {
372 	uint16_t v;
373 	int error = md_get_uint16(mdp, &v);
374 
375 	if (x != NULL)
376 		*x = le16toh(v);
377 	return error;
378 }
379 
380 int
381 md_get_uint16be(struct mdchain *mdp, uint16_t *x)
382 {
383 	uint16_t v;
384 	int error = md_get_uint16(mdp, &v);
385 
386 	if (x != NULL)
387 		*x = be16toh(v);
388 	return error;
389 }
390 
391 int
392 md_get_uint32(struct mdchain *mdp, uint32_t *x)
393 {
394 	return md_get_mem(mdp, (caddr_t)x, 4, MB_MINLINE);
395 }
396 
397 int
398 md_get_uint32be(struct mdchain *mdp, uint32_t *x)
399 {
400 	uint32_t v;
401 	int error;
402 
403 	error = md_get_uint32(mdp, &v);
404 	if (x != NULL)
405 		*x = be32toh(v);
406 	return error;
407 }
408 
409 int
410 md_get_uint32le(struct mdchain *mdp, uint32_t *x)
411 {
412 	uint32_t v;
413 	int error;
414 
415 	error = md_get_uint32(mdp, &v);
416 	if (x != NULL)
417 		*x = le32toh(v);
418 	return error;
419 }
420 
421 int
422 md_get_int64(struct mdchain *mdp, int64_t *x)
423 {
424 	return md_get_mem(mdp, (caddr_t)x, 8, MB_MINLINE);
425 }
426 
427 int
428 md_get_int64be(struct mdchain *mdp, int64_t *x)
429 {
430 	int64_t v;
431 	int error;
432 
433 	error = md_get_int64(mdp, &v);
434 	if (x != NULL)
435 		*x = be64toh(v);
436 	return error;
437 }
438 
439 int
440 md_get_int64le(struct mdchain *mdp, int64_t *x)
441 {
442 	int64_t v;
443 	int error;
444 
445 	error = md_get_int64(mdp, &v);
446 	if (x != NULL)
447 		*x = le64toh(v);
448 	return error;
449 }
450 
451 int
452 md_get_mem(struct mdchain *mdp, caddr_t target, int size, int type)
453 {
454 	struct mbuf *m = mdp->md_cur;
455 	int error;
456 	u_int count;
457 	u_char *s;
458 
459 	while (size > 0) {
460 		if (m == NULL) {
461 			MBERROR("incomplete copy\n");
462 			return EBADRPC;
463 		}
464 		s = mdp->md_pos;
465 		count = mtod(m, u_char*) + m->m_len - s;
466 		if (count == 0) {
467 			mdp->md_cur = m = m->m_next;
468 			if (m)
469 				s = mdp->md_pos = mtod(m, caddr_t);
470 			continue;
471 		}
472 		if (count > size)
473 			count = size;
474 		size -= count;
475 		mdp->md_pos += count;
476 		if (target == NULL)
477 			continue;
478 		switch (type) {
479 		    case MB_MUSER:
480 			error = copyout(s, target, count);
481 			if (error)
482 				return error;
483 			break;
484 		    case MB_MSYSTEM:
485 			bcopy(s, target, count);
486 			break;
487 		    case MB_MINLINE:
488 			while (count--)
489 				*target++ = *s++;
490 			continue;
491 		}
492 		target += count;
493 	}
494 	return 0;
495 }
496 
497 int
498 md_get_mbuf(struct mdchain *mdp, int size, struct mbuf **ret)
499 {
500 	struct mbuf *m = mdp->md_cur, *rm;
501 
502 	rm = m_copym(m, mdp->md_pos - mtod(m, u_char*), size, M_WAIT);
503 	md_get_mem(mdp, NULL, size, MB_MZERO);
504 	*ret = rm;
505 	return 0;
506 }
507 
508 int
509 md_get_uio(struct mdchain *mdp, struct uio *uiop, int size)
510 {
511 	char *uiocp;
512 	long left;
513 	int mtype, error;
514 
515 	mtype = (uiop->uio_segflg == UIO_SYSSPACE) ? MB_MSYSTEM : MB_MUSER;
516 	while (size > 0 && uiop->uio_resid) {
517 		if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
518 			return EFBIG;
519 		left = uiop->uio_iov->iov_len;
520 		if (left == 0) {
521 			uiop->uio_iov++;
522 			uiop->uio_iovcnt--;
523 			continue;
524 		}
525 		uiocp = uiop->uio_iov->iov_base;
526 		if (left > size)
527 			left = size;
528 		error = md_get_mem(mdp, uiocp, left, mtype);
529 		if (error)
530 			return error;
531 		uiop->uio_offset += left;
532 		uiop->uio_resid -= left;
533 		uiop->uio_iov->iov_base =
534 		    (char *)uiop->uio_iov->iov_base + left;
535 		uiop->uio_iov->iov_len -= left;
536 		size -= left;
537 	}
538 	return 0;
539 }
540