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