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