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