xref: /freebsd/sys/compat/linuxkpi/common/src/linux_skbuff.c (revision 8c47d8f53854825d8e8591ccd06e32b2c798f81c)
1 /*-
2  * Copyright (c) 2020-2022 The FreeBSD Foundation
3  * Copyright (c) 2021-2022 Bjoern A. Zeeb
4  *
5  * This software was developed by Björn Zeeb under sponsorship from
6  * the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
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  * $FreeBSD$
30  */
31 
32 /*
33  * NOTE: this socket buffer compatibility code is highly EXPERIMENTAL.
34  *       Do not rely on the internals of this implementation.  They are highly
35  *       likely to change as we will improve the integration to FreeBSD mbufs.
36  */
37 
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40 
41 #include "opt_ddb.h"
42 
43 #include <sys/param.h>
44 #include <sys/types.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/sysctl.h>
48 
49 #ifdef DDB
50 #include <ddb/ddb.h>
51 #endif
52 
53 #include <linux/skbuff.h>
54 #include <linux/slab.h>
55 #include <linux/gfp.h>
56 
57 #ifdef SKB_DEBUG
58 SYSCTL_DECL(_compat_linuxkpi);
59 SYSCTL_NODE(_compat_linuxkpi, OID_AUTO, skb, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
60     "LinuxKPI skbuff");
61 
62 int linuxkpi_debug_skb;
63 SYSCTL_INT(_compat_linuxkpi_skb, OID_AUTO, debug, CTLFLAG_RWTUN,
64     &linuxkpi_debug_skb, 0, "SKB debug level");
65 #endif
66 
67 static MALLOC_DEFINE(M_LKPISKB, "lkpiskb", "Linux KPI skbuff compat");
68 
69 struct sk_buff *
70 linuxkpi_alloc_skb(size_t size, gfp_t gfp)
71 {
72 	struct sk_buff *skb;
73 	size_t len;
74 
75 	len = sizeof(*skb) + size + sizeof(struct skb_shared_info);
76 	/*
77 	 * Using our own type here not backing my kmalloc.
78 	 * We assume no one calls kfree directly on the skb.
79 	 */
80 	skb = malloc(len, M_LKPISKB, linux_check_m_flags(gfp) | M_ZERO);
81 	if (skb == NULL)
82 		return (skb);
83 	skb->_alloc_len = len;
84 	skb->truesize = size;
85 
86 	skb->head = skb->data = skb->tail = (uint8_t *)(skb+1);
87 	skb->end = skb->head + size;
88 
89 	skb->prev = skb->next = skb;
90 
91 	skb->shinfo = (struct skb_shared_info *)(skb->end);
92 
93 	SKB_TRACE_FMT(skb, "data %p size %zu", (skb) ? skb->data : NULL, size);
94 	return (skb);
95 }
96 
97 struct sk_buff *
98 linuxkpi_dev_alloc_skb(size_t size, gfp_t gfp)
99 {
100 	struct sk_buff *skb;
101 	size_t len;
102 
103 	len = size + NET_SKB_PAD;
104 	skb = linuxkpi_alloc_skb(len, gfp);
105 
106 	if (skb != NULL)
107 		skb_reserve(skb, NET_SKB_PAD);
108 
109 	SKB_TRACE_FMT(skb, "data %p size %zu len %zu",
110 	    (skb) ? skb->data : NULL, size, len);
111 	return (skb);
112 }
113 
114 struct sk_buff *
115 linuxkpi_skb_copy(struct sk_buff *skb, gfp_t gfp)
116 {
117 	struct sk_buff *new;
118 	struct skb_shared_info *shinfo;
119 	size_t len;
120 	unsigned int headroom;
121 
122 	/* Full buffer size + any fragments. */
123 	len = skb->end - skb->head + skb->data_len;
124 
125 	new = linuxkpi_alloc_skb(len, gfp);
126 	if (new == NULL)
127 		return (NULL);
128 
129 	headroom = skb_headroom(skb);
130 	/* Fixup head and end. */
131 	skb_reserve(new, headroom);	/* data and tail move headroom forward. */
132 	skb_put(new, skb->len);		/* tail and len get adjusted */
133 
134 	/* Copy data. */
135 	memcpy(new->head, skb->data - headroom, headroom + skb->len);
136 
137 	/* Deal with fragments. */
138 	shinfo = skb->shinfo;
139 	if (shinfo->nr_frags > 0) {
140 		printf("%s:%d: NOT YET SUPPORTED; missing %d frags\n",
141 		    __func__, __LINE__, shinfo->nr_frags);
142 		SKB_TODO();
143 	}
144 
145 	/* Deal with header fields. */
146 	memcpy(new->cb, skb->cb, sizeof(skb->cb));
147 	SKB_IMPROVE("more header fields to copy?");
148 
149 	return (new);
150 }
151 
152 void
153 linuxkpi_kfree_skb(struct sk_buff *skb)
154 {
155 	struct skb_shared_info *shinfo;
156 	uint16_t fragno, count;
157 
158 	SKB_TRACE(skb);
159 	if (skb == NULL)
160 		return;
161 
162 	/*
163 	 * XXX TODO this will go away once we have skb backed by mbuf.
164 	 * currently we allow the mbuf to stay around and use a private
165 	 * free function to allow secondary resources to be freed along.
166 	 */
167 	if (skb->m != NULL) {
168 		void *m;
169 
170 		m = skb->m;
171 		skb->m = NULL;
172 
173 		KASSERT(skb->m_free_func != NULL, ("%s: skb %p has m %p but no "
174 		    "m_free_func %p\n", __func__, skb, m, skb->m_free_func));
175 		skb->m_free_func(m);
176 	}
177 	KASSERT(skb->m == NULL,
178 	    ("%s: skb %p m %p != NULL\n", __func__, skb, skb->m));
179 
180 	shinfo = skb->shinfo;
181 	for (count = fragno = 0;
182 	    count < shinfo->nr_frags && fragno < nitems(shinfo->frags);
183 	    fragno++) {
184 
185 		if (shinfo->frags[fragno].page != NULL) {
186 			struct page *p;
187 
188 			p = shinfo->frags[fragno].page;
189 			shinfo->frags[fragno].size = 0;
190 			shinfo->frags[fragno].offset = 0;
191 			shinfo->frags[fragno].page = NULL;
192 			__free_page(p);
193 			count++;
194 		}
195 	}
196 
197 	free(skb, M_LKPISKB);
198 }
199 
200 #ifdef DDB
201 DB_SHOW_COMMAND(skb, db_show_skb)
202 {
203 	struct sk_buff *skb;
204 	int i;
205 
206 	if (!have_addr) {
207 		db_printf("usage: show skb <addr>\n");
208 			return;
209 	}
210 
211 	skb = (struct sk_buff *)addr;
212 
213 	db_printf("skb %p\n", skb);
214 	db_printf("\tnext %p prev %p\n", skb->next, skb->prev);
215 	db_printf("\tlist %d\n", skb->list);
216 	db_printf("\t_alloc_len %u len %u data_len %u truesize %u mac_len %u\n",
217 	    skb->_alloc_len, skb->len, skb->data_len, skb->truesize,
218 	    skb->mac_len);
219 	db_printf("\tcsum %#06x l3hdroff %u l4hdroff %u priority %u qmap %u\n",
220 	    skb->csum, skb->l3hdroff, skb->l4hdroff, skb->priority, skb->qmap);
221 	db_printf("\tpkt_type %d dev %p sk %p\n",
222 	    skb->pkt_type, skb->dev, skb->sk);
223 	db_printf("\tcsum_offset %d csum_start %d ip_summed %d protocol %d\n",
224 	    skb->csum_offset, skb->csum_start, skb->ip_summed, skb->protocol);
225 	db_printf("\thead %p data %p tail %p end %p\n",
226 	    skb->head, skb->data, skb->tail, skb->end);
227 	db_printf("\tshinfo %p m %p m_free_func %p\n",
228 	    skb->shinfo, skb->m, skb->m_free_func);
229 
230 	if (skb->shinfo != NULL) {
231 		struct skb_shared_info *shinfo;
232 
233 		shinfo = skb->shinfo;
234 		db_printf("\t\tgso_type %d gso_size %u nr_frags %u\n",
235 		    shinfo->gso_type, shinfo->gso_size, shinfo->nr_frags);
236 		for (i = 0; i < nitems(shinfo->frags); i++) {
237 			struct skb_frag *frag;
238 
239 			frag = &shinfo->frags[i];
240 			if (frag == NULL || frag->page == NULL)
241 				continue;
242 			db_printf("\t\t\tfrag %p fragno %d page %p %p "
243 			    "offset %ju size %zu\n",
244 			    frag, i, frag->page, linux_page_address(frag->page),
245 			    (uintmax_t)frag->offset, frag->size);
246 		}
247 	}
248 	db_printf("\tcb[] %p {", skb->cb);
249 	for (i = 0; i < nitems(skb->cb); i++) {
250 		db_printf("%#04x%s",
251 		    skb->cb[i], (i < (nitems(skb->cb)-1)) ? ", " : "");
252 	}
253 	db_printf("}\n");
254 
255 	db_printf("\t_spareu16_0 %#06x __scratch[0] %p\n",
256 	    skb->_spareu16_0, skb->__scratch);
257 };
258 #endif
259