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 void 115 linuxkpi_kfree_skb(struct sk_buff *skb) 116 { 117 struct skb_shared_info *shinfo; 118 uint16_t fragno, count; 119 120 SKB_TRACE(skb); 121 if (skb == NULL) 122 return; 123 124 /* 125 * XXX TODO this will go away once we have skb backed by mbuf. 126 * currently we allow the mbuf to stay around and use a private 127 * free function to allow secondary resources to be freed along. 128 */ 129 if (skb->m != NULL) { 130 void *m; 131 132 m = skb->m; 133 skb->m = NULL; 134 135 KASSERT(skb->m_free_func != NULL, ("%s: skb %p has m %p but no " 136 "m_free_func %p\n", __func__, skb, m, skb->m_free_func)); 137 skb->m_free_func(m); 138 } 139 KASSERT(skb->m == NULL, 140 ("%s: skb %p m %p != NULL\n", __func__, skb, skb->m)); 141 142 shinfo = skb->shinfo; 143 for (count = fragno = 0; 144 count < shinfo->nr_frags && fragno < nitems(shinfo->frags); 145 fragno++) { 146 147 if (shinfo->frags[fragno].page != NULL) { 148 struct page *p; 149 150 p = shinfo->frags[fragno].page; 151 shinfo->frags[fragno].size = 0; 152 shinfo->frags[fragno].offset = 0; 153 shinfo->frags[fragno].page = NULL; 154 __free_page(p); 155 count++; 156 } 157 } 158 159 free(skb, M_LKPISKB); 160 } 161 162 #ifdef DDB 163 DB_SHOW_COMMAND(skb, db_show_skb) 164 { 165 struct sk_buff *skb; 166 int i; 167 168 if (!have_addr) { 169 db_printf("usage: show skb <addr>\n"); 170 return; 171 } 172 173 skb = (struct sk_buff *)addr; 174 175 db_printf("skb %p\n", skb); 176 db_printf("\tnext %p prev %p\n", skb->next, skb->prev); 177 db_printf("\tlist %d\n", skb->list); 178 db_printf("\t_alloc_len %u len %u data_len %u truesize %u mac_len %u\n", 179 skb->_alloc_len, skb->len, skb->data_len, skb->truesize, 180 skb->mac_len); 181 db_printf("\tcsum %#06x l3hdroff %u l4hdroff %u priority %u qmap %u\n", 182 skb->csum, skb->l3hdroff, skb->l4hdroff, skb->priority, skb->qmap); 183 db_printf("\tpkt_type %d dev %p sk %p\n", 184 skb->pkt_type, skb->dev, skb->sk); 185 db_printf("\tcsum_offset %d csum_start %d ip_summed %d protocol %d\n", 186 skb->csum_offset, skb->csum_start, skb->ip_summed, skb->protocol); 187 db_printf("\thead %p data %p tail %p end %p\n", 188 skb->head, skb->data, skb->tail, skb->end); 189 db_printf("\tshinfo %p m %p m_free_func %p\n", 190 skb->shinfo, skb->m, skb->m_free_func); 191 192 if (skb->shinfo != NULL) { 193 struct skb_shared_info *shinfo; 194 195 shinfo = skb->shinfo; 196 db_printf("\t\tgso_type %d gso_size %u nr_frags %u\n", 197 shinfo->gso_type, shinfo->gso_size, shinfo->nr_frags); 198 for (i = 0; i < nitems(shinfo->frags); i++) { 199 struct skb_frag *frag; 200 201 frag = &shinfo->frags[i]; 202 if (frag == NULL || frag->page == NULL) 203 continue; 204 db_printf("\t\t\tfrag %p fragno %d page %p %p " 205 "offset %ju size %zu\n", 206 frag, i, frag->page, linux_page_address(frag->page), 207 (uintmax_t)frag->offset, frag->size); 208 } 209 } 210 db_printf("\tcb[] %p {", skb->cb); 211 for (i = 0; i < nitems(skb->cb); i++) { 212 db_printf("%#04x%s", 213 skb->cb[i], (i < (nitems(skb->cb)-1)) ? ", " : ""); 214 } 215 db_printf("}\n"); 216 217 db_printf("\t_spareu16_0 %#06x __scratch[0] %p\n", 218 skb->_spareu16_0, skb->__scratch); 219 }; 220 #endif 221