xref: /freebsd/sys/xdr/xdr_mbuf.c (revision 3c6e15bceeab4470243c60c9a4b5b9cafca9abaa)
1 /*-
2  * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
3  * Authors: Doug Rabson <dfr@rabson.org>
4  * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/malloc.h>
34 #include <sys/mbuf.h>
35 
36 #include <rpc/types.h>
37 #include <rpc/xdr.h>
38 
39 static void xdrmbuf_destroy(XDR *);
40 static bool_t xdrmbuf_getlong(XDR *, long *);
41 static bool_t xdrmbuf_putlong(XDR *, const long *);
42 static bool_t xdrmbuf_getbytes(XDR *, char *, u_int);
43 static bool_t xdrmbuf_putbytes(XDR *, const char *, u_int);
44 /* XXX: w/64-bit pointers, u_int not enough! */
45 static u_int xdrmbuf_getpos(XDR *);
46 static bool_t xdrmbuf_setpos(XDR *, u_int);
47 static int32_t *xdrmbuf_inline(XDR *, u_int);
48 
49 static const struct	xdr_ops xdrmbuf_ops = {
50 	xdrmbuf_getlong,
51 	xdrmbuf_putlong,
52 	xdrmbuf_getbytes,
53 	xdrmbuf_putbytes,
54 	xdrmbuf_getpos,
55 	xdrmbuf_setpos,
56 	xdrmbuf_inline,
57 	xdrmbuf_destroy
58 };
59 
60 /*
61  * The procedure xdrmbuf_create initializes a stream descriptor for a
62  * mbuf.
63  */
64 void
65 xdrmbuf_create(XDR *xdrs, struct mbuf *m, enum xdr_op op)
66 {
67 
68 	xdrs->x_op = op;
69 	xdrs->x_ops = &xdrmbuf_ops;
70 	xdrs->x_base = (char *) m;
71 	if (op == XDR_ENCODE) {
72 		m = m_last(m);
73 		xdrs->x_private = m;
74 		xdrs->x_handy = m->m_len;
75 	} else {
76 		xdrs->x_private = m;
77 		xdrs->x_handy = 0;
78 	}
79 }
80 
81 void
82 xdrmbuf_append(XDR *xdrs, struct mbuf *madd)
83 {
84 	struct mbuf *m;
85 
86 	KASSERT(xdrs->x_ops == &xdrmbuf_ops && xdrs->x_op == XDR_ENCODE,
87 	    ("xdrmbuf_append: invalid XDR stream"));
88 
89 	if (m_length(madd, NULL) == 0) {
90 		m_freem(madd);
91 		return;
92 	}
93 
94 	m = (struct mbuf *) xdrs->x_private;
95 	m->m_next = madd;
96 
97 	m = m_last(madd);
98 	xdrs->x_private = m;
99 	xdrs->x_handy = m->m_len;
100 }
101 
102 struct mbuf *
103 xdrmbuf_getall(XDR *xdrs)
104 {
105 	struct mbuf *m0, *m;
106 
107 	KASSERT(xdrs->x_ops == &xdrmbuf_ops && xdrs->x_op == XDR_DECODE,
108 	    ("xdrmbuf_append: invalid XDR stream"));
109 
110 	m0 = (struct mbuf *) xdrs->x_base;
111 	m = (struct mbuf *) xdrs->x_private;
112 	if (m0 != m) {
113 		while (m0->m_next != m)
114 			m0 = m0->m_next;
115 		m0->m_next = NULL;
116 		xdrs->x_private = NULL;
117 	} else {
118 		xdrs->x_base = NULL;
119 		xdrs->x_private = NULL;
120 	}
121 
122 	if (m)
123 		m_adj(m, xdrs->x_handy);
124 	else
125 		MGET(m, M_WAITOK, MT_DATA);
126 	return (m);
127 }
128 
129 static void
130 xdrmbuf_destroy(XDR *xdrs)
131 {
132 
133 	if (xdrs->x_op == XDR_DECODE && xdrs->x_base) {
134 		m_freem((struct mbuf *) xdrs->x_base);
135 		xdrs->x_base = NULL;
136 		xdrs->x_private = NULL;
137 	}
138 }
139 
140 static bool_t
141 xdrmbuf_getlong(XDR *xdrs, long *lp)
142 {
143 	int32_t *p;
144 	int32_t t;
145 
146 	p = xdrmbuf_inline(xdrs, sizeof(int32_t));
147 	if (p) {
148 		t = *p;
149 	} else {
150 		xdrmbuf_getbytes(xdrs, (char *) &t, sizeof(int32_t));
151 	}
152 
153 	*lp = ntohl(t);
154 	return (TRUE);
155 }
156 
157 static bool_t
158 xdrmbuf_putlong(xdrs, lp)
159 	XDR *xdrs;
160 	const long *lp;
161 {
162 	int32_t *p;
163 	int32_t t = htonl(*lp);
164 
165 	p = xdrmbuf_inline(xdrs, sizeof(int32_t));
166 	if (p) {
167 		*p = t;
168 		return (TRUE);
169 	} else {
170 		return (xdrmbuf_putbytes(xdrs, (char *) &t, sizeof(int32_t)));
171 	}
172 }
173 
174 static bool_t
175 xdrmbuf_getbytes(XDR *xdrs, char *addr, u_int len)
176 {
177 	struct mbuf *m = (struct mbuf *) xdrs->x_private;
178 	size_t sz;
179 
180 	while (len > 0) {
181 		/*
182 		 * Make sure we haven't hit the end.
183 		 */
184 		if (!m) {
185 			return (FALSE);
186 		}
187 
188 		/*
189 		 * See how much we can get from this mbuf.
190 		 */
191 		sz = m->m_len - xdrs->x_handy;
192 		if (sz > len)
193 			sz = len;
194 		bcopy(mtod(m, const char *) + xdrs->x_handy, addr, sz);
195 
196 		addr += sz;
197 		xdrs->x_handy += sz;
198 		len -= sz;
199 
200 		if (xdrs->x_handy == m->m_len) {
201 			m = m->m_next;
202 			xdrs->x_private = (void *) m;
203 			xdrs->x_handy = 0;
204 		}
205 	}
206 
207 	return (TRUE);
208 }
209 
210 static bool_t
211 xdrmbuf_putbytes(XDR *xdrs, const char *addr, u_int len)
212 {
213 	struct mbuf *m = (struct mbuf *) xdrs->x_private;
214 	struct mbuf *n;
215 	size_t sz;
216 
217 	while (len > 0) {
218 		sz = M_TRAILINGSPACE(m) + (m->m_len - xdrs->x_handy);
219 		if (sz > len)
220 			sz = len;
221 		bcopy(addr, mtod(m, char *) + xdrs->x_handy, sz);
222 		addr += sz;
223 		xdrs->x_handy += sz;
224 		if (xdrs->x_handy > m->m_len)
225 			m->m_len = xdrs->x_handy;
226 		len -= sz;
227 
228 		if (xdrs->x_handy == m->m_len && M_TRAILINGSPACE(m) == 0) {
229 			if (!m->m_next) {
230 				MGET(n, M_TRYWAIT, m->m_type);
231 				if (m->m_flags & M_EXT)
232 					MCLGET(n, M_TRYWAIT);
233 				m->m_next = n;
234 			}
235 			m = m->m_next;
236 			xdrs->x_private = (void *) m;
237 			xdrs->x_handy = 0;
238 		}
239 	}
240 
241 	return (TRUE);
242 }
243 
244 static u_int
245 xdrmbuf_getpos(XDR *xdrs)
246 {
247 	struct mbuf *m0 = (struct mbuf *) xdrs->x_base;
248 	struct mbuf *m = (struct mbuf *) xdrs->x_private;
249 	u_int pos = 0;
250 
251 	while (m0 && m0 != m) {
252 		pos += m0->m_len;
253 		m0 = m0->m_next;
254 	}
255 	KASSERT(m0, ("Corrupted mbuf chain"));
256 
257 	return (pos + xdrs->x_handy);
258 }
259 
260 static bool_t
261 xdrmbuf_setpos(XDR *xdrs, u_int pos)
262 {
263 	struct mbuf *m = (struct mbuf *) xdrs->x_base;
264 
265 	while (m && pos > m->m_len) {
266 		pos -= m->m_len;
267 		m = m->m_next;
268 	}
269 	KASSERT(m, ("Corrupted mbuf chain"));
270 
271 	xdrs->x_private = (void *) m;
272 	xdrs->x_handy = pos;
273 
274 	return (TRUE);
275 }
276 
277 static int32_t *
278 xdrmbuf_inline(XDR *xdrs, u_int len)
279 {
280 	struct mbuf *m = (struct mbuf *) xdrs->x_private;
281 	size_t available;
282 	char *p;
283 
284 	if (xdrs->x_op == XDR_ENCODE) {
285 		available = M_TRAILINGSPACE(m) + (m->m_len - xdrs->x_handy);
286 	} else {
287 		available = m->m_len - xdrs->x_handy;
288 	}
289 
290 	if (available >= len) {
291 		p = mtod(m, char *) + xdrs->x_handy;
292 		if (((uintptr_t) p) & (sizeof(int32_t) - 1))
293 			return (0);
294 		xdrs->x_handy += len;
295 		if (xdrs->x_handy > m->m_len)
296 			m->m_len = xdrs->x_handy;
297 		return ((int32_t *) p);
298 	}
299 
300 	return (0);
301 }
302