xref: /freebsd/sys/compat/linuxkpi/common/src/linux_skbuff.c (revision c7046f76c2c027b00c0e6ba57cfd28f1a78f5e23)
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 #ifdef __LP64__
57 #include <linux/log2.h>
58 #endif
59 
60 SYSCTL_DECL(_compat_linuxkpi);
61 SYSCTL_NODE(_compat_linuxkpi, OID_AUTO, skb, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
62     "LinuxKPI skbuff");
63 
64 #ifdef SKB_DEBUG
65 int linuxkpi_debug_skb;
66 SYSCTL_INT(_compat_linuxkpi_skb, OID_AUTO, debug, CTLFLAG_RWTUN,
67     &linuxkpi_debug_skb, 0, "SKB debug level");
68 #endif
69 
70 #ifdef __LP64__
71 /*
72  * Realtek wireless drivers (e.g., rtw88) require 32bit DMA in a single segment.
73  * busdma(9) has a hard time providing this currently for 3-ish pages at large
74  * quantities (see lkpi_pci_nseg1_fail in linux_pci.c).
75  * Work around this for now by allowing a tunable to enforce physical addresses
76  * allocation limits on 64bit platforms using "old-school" contigmalloc(9) to
77  * avoid bouncing.
78  */
79 static int linuxkpi_skb_memlimit;
80 SYSCTL_INT(_compat_linuxkpi_skb, OID_AUTO, mem_limit, CTLFLAG_RDTUN,
81     &linuxkpi_skb_memlimit, 0, "SKB memory limit: 0=no limit, "
82     "1=32bit, 2=36bit, other=undef (currently 32bit)");
83 #endif
84 
85 static MALLOC_DEFINE(M_LKPISKB, "lkpiskb", "Linux KPI skbuff compat");
86 
87 struct sk_buff *
88 linuxkpi_alloc_skb(size_t size, gfp_t gfp)
89 {
90 	struct sk_buff *skb;
91 	size_t len;
92 
93 	len = sizeof(*skb) + size + sizeof(struct skb_shared_info);
94 	/*
95 	 * Using our own type here not backing my kmalloc.
96 	 * We assume no one calls kfree directly on the skb.
97 	 */
98 #ifdef __LP64__
99 	if (__predict_true(linuxkpi_skb_memlimit == 0)) {
100 		skb = malloc(len, M_LKPISKB, linux_check_m_flags(gfp) | M_ZERO);
101 	} else {
102 		vm_paddr_t high;
103 
104 		switch (linuxkpi_skb_memlimit) {
105 		case 2:
106 			high = (0xfffffffff);	/* 1<<36 really. */
107 			break;
108 		case 1:
109 		default:
110 			high = (0xffffffff);	/* 1<<32 really. */
111 			break;
112 		}
113 		len = roundup_pow_of_two(len);
114 		skb = contigmalloc(len, M_LKPISKB,
115 		    linux_check_m_flags(gfp) | M_ZERO, 0, high, PAGE_SIZE, 0);
116 	}
117 #else
118 	skb = malloc(len, M_LKPISKB, linux_check_m_flags(gfp) | M_ZERO);
119 #endif
120 	if (skb == NULL)
121 		return (skb);
122 	skb->_alloc_len = len;
123 	skb->truesize = size;
124 
125 	skb->head = skb->data = skb->tail = (uint8_t *)(skb+1);
126 	skb->end = skb->head + size;
127 
128 	skb->prev = skb->next = skb;
129 
130 	skb->shinfo = (struct skb_shared_info *)(skb->end);
131 
132 	SKB_TRACE_FMT(skb, "data %p size %zu", (skb) ? skb->data : NULL, size);
133 	return (skb);
134 }
135 
136 struct sk_buff *
137 linuxkpi_dev_alloc_skb(size_t size, gfp_t gfp)
138 {
139 	struct sk_buff *skb;
140 	size_t len;
141 
142 	len = size + NET_SKB_PAD;
143 	skb = linuxkpi_alloc_skb(len, gfp);
144 
145 	if (skb != NULL)
146 		skb_reserve(skb, NET_SKB_PAD);
147 
148 	SKB_TRACE_FMT(skb, "data %p size %zu len %zu",
149 	    (skb) ? skb->data : NULL, size, len);
150 	return (skb);
151 }
152 
153 struct sk_buff *
154 linuxkpi_skb_copy(struct sk_buff *skb, gfp_t gfp)
155 {
156 	struct sk_buff *new;
157 	struct skb_shared_info *shinfo;
158 	size_t len;
159 	unsigned int headroom;
160 
161 	/* Full buffer size + any fragments. */
162 	len = skb->end - skb->head + skb->data_len;
163 
164 	new = linuxkpi_alloc_skb(len, gfp);
165 	if (new == NULL)
166 		return (NULL);
167 
168 	headroom = skb_headroom(skb);
169 	/* Fixup head and end. */
170 	skb_reserve(new, headroom);	/* data and tail move headroom forward. */
171 	skb_put(new, skb->len);		/* tail and len get adjusted */
172 
173 	/* Copy data. */
174 	memcpy(new->head, skb->data - headroom, headroom + skb->len);
175 
176 	/* Deal with fragments. */
177 	shinfo = skb->shinfo;
178 	if (shinfo->nr_frags > 0) {
179 		printf("%s:%d: NOT YET SUPPORTED; missing %d frags\n",
180 		    __func__, __LINE__, shinfo->nr_frags);
181 		SKB_TODO();
182 	}
183 
184 	/* Deal with header fields. */
185 	memcpy(new->cb, skb->cb, sizeof(skb->cb));
186 	SKB_IMPROVE("more header fields to copy?");
187 
188 	return (new);
189 }
190 
191 void
192 linuxkpi_kfree_skb(struct sk_buff *skb)
193 {
194 	struct skb_shared_info *shinfo;
195 	uint16_t fragno, count;
196 
197 	SKB_TRACE(skb);
198 	if (skb == NULL)
199 		return;
200 
201 	/*
202 	 * XXX TODO this will go away once we have skb backed by mbuf.
203 	 * currently we allow the mbuf to stay around and use a private
204 	 * free function to allow secondary resources to be freed along.
205 	 */
206 	if (skb->m != NULL) {
207 		void *m;
208 
209 		m = skb->m;
210 		skb->m = NULL;
211 
212 		KASSERT(skb->m_free_func != NULL, ("%s: skb %p has m %p but no "
213 		    "m_free_func %p\n", __func__, skb, m, skb->m_free_func));
214 		skb->m_free_func(m);
215 	}
216 	KASSERT(skb->m == NULL,
217 	    ("%s: skb %p m %p != NULL\n", __func__, skb, skb->m));
218 
219 	shinfo = skb->shinfo;
220 	for (count = fragno = 0;
221 	    count < shinfo->nr_frags && fragno < nitems(shinfo->frags);
222 	    fragno++) {
223 
224 		if (shinfo->frags[fragno].page != NULL) {
225 			struct page *p;
226 
227 			p = shinfo->frags[fragno].page;
228 			shinfo->frags[fragno].size = 0;
229 			shinfo->frags[fragno].offset = 0;
230 			shinfo->frags[fragno].page = NULL;
231 			__free_page(p);
232 			count++;
233 		}
234 	}
235 
236 #ifdef __LP64__
237 	if (__predict_true(linuxkpi_skb_memlimit == 0))
238 		free(skb, M_LKPISKB);
239 	else
240 		contigfree(skb, skb->_alloc_len, M_LKPISKB);
241 #else
242 	free(skb, M_LKPISKB);
243 #endif
244 }
245 
246 #ifdef DDB
247 DB_SHOW_COMMAND(skb, db_show_skb)
248 {
249 	struct sk_buff *skb;
250 	int i;
251 
252 	if (!have_addr) {
253 		db_printf("usage: show skb <addr>\n");
254 			return;
255 	}
256 
257 	skb = (struct sk_buff *)addr;
258 
259 	db_printf("skb %p\n", skb);
260 	db_printf("\tnext %p prev %p\n", skb->next, skb->prev);
261 	db_printf("\tlist %p\n", &skb->list);
262 	db_printf("\t_alloc_len %u len %u data_len %u truesize %u mac_len %u\n",
263 	    skb->_alloc_len, skb->len, skb->data_len, skb->truesize,
264 	    skb->mac_len);
265 	db_printf("\tcsum %#06x l3hdroff %u l4hdroff %u priority %u qmap %u\n",
266 	    skb->csum, skb->l3hdroff, skb->l4hdroff, skb->priority, skb->qmap);
267 	db_printf("\tpkt_type %d dev %p sk %p\n",
268 	    skb->pkt_type, skb->dev, skb->sk);
269 	db_printf("\tcsum_offset %d csum_start %d ip_summed %d protocol %d\n",
270 	    skb->csum_offset, skb->csum_start, skb->ip_summed, skb->protocol);
271 	db_printf("\thead %p data %p tail %p end %p\n",
272 	    skb->head, skb->data, skb->tail, skb->end);
273 	db_printf("\tshinfo %p m %p m_free_func %p\n",
274 	    skb->shinfo, skb->m, skb->m_free_func);
275 
276 	if (skb->shinfo != NULL) {
277 		struct skb_shared_info *shinfo;
278 
279 		shinfo = skb->shinfo;
280 		db_printf("\t\tgso_type %d gso_size %u nr_frags %u\n",
281 		    shinfo->gso_type, shinfo->gso_size, shinfo->nr_frags);
282 		for (i = 0; i < nitems(shinfo->frags); i++) {
283 			struct skb_frag *frag;
284 
285 			frag = &shinfo->frags[i];
286 			if (frag == NULL || frag->page == NULL)
287 				continue;
288 			db_printf("\t\t\tfrag %p fragno %d page %p %p "
289 			    "offset %ju size %zu\n",
290 			    frag, i, frag->page, linux_page_address(frag->page),
291 			    (uintmax_t)frag->offset, frag->size);
292 		}
293 	}
294 	db_printf("\tcb[] %p {", skb->cb);
295 	for (i = 0; i < nitems(skb->cb); i++) {
296 		db_printf("%#04x%s",
297 		    skb->cb[i], (i < (nitems(skb->cb)-1)) ? ", " : "");
298 	}
299 	db_printf("}\n");
300 
301 	db_printf("\t_spareu16_0 %#06x __scratch[0] %p\n",
302 	    skb->_spareu16_0, skb->__scratch);
303 };
304 #endif
305