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