1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
/*-
* Copyright (c) 2009-2020 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This material is based upon work partially supported by The
* NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* NPF network buffer management interface.
*
* Network buffer in NetBSD is mbuf. Internal mbuf structures are
* abstracted within this source.
*/
#ifdef _KERNEL
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: npf_mbuf.c,v 1.25 2023/02/12 13:38:37 kardel Exp $");
#include <sys/param.h>
#include <sys/mbuf.h>
#include <netinet/in_offload.h>
#endif
#include "npf_impl.h"
#ifdef _KERNEL
#ifdef INET6
#include <netinet6/in6.h>
#include <netinet6/in6_offload.h>
#endif
#endif
#if defined(_NPF_STANDALONE)
#define m_length(m) (nbuf)->nb_mops->getchainlen(m)
#define m_buflen(m) (nbuf)->nb_mops->getlen(m)
#define m_next_ptr(m) (nbuf)->nb_mops->getnext(m)
#define m_ensure_contig(m,t) (nbuf)->nb_mops->ensure_contig((m), (t))
#define m_makewritable(m,o,l,f) (nbuf)->nb_mops->ensure_writable((m), (o+l))
#define mtod(m,t) ((t)((nbuf)->nb_mops->getdata(m)))
#define m_flags_p(m,f) true
#define M_UNWRITABLE(m, l) false
#else
#define m_next_ptr(m) (m)->m_next
#define m_buflen(m) ((size_t)(m)->m_len)
#define m_flags_p(m,f) (((m)->m_flags & (f)) != 0)
#endif
#define NBUF_ENSURE_ALIGN (MAX(COHERENCY_UNIT, 64))
#define NBUF_ENSURE_MASK (NBUF_ENSURE_ALIGN - 1)
#define NBUF_ENSURE_ROUNDUP(x) (((x) + NBUF_ENSURE_ALIGN) & ~NBUF_ENSURE_MASK)
void
nbuf_init(npf_t *npf, nbuf_t *nbuf, struct mbuf *m, const ifnet_t *ifp)
{
unsigned ifid = npf_ifmap_getid(npf, ifp);
KASSERT(m_flags_p(m, M_PKTHDR));
nbuf->nb_mops = npf->mbufops;
nbuf->nb_mbuf0 = m;
nbuf->nb_ifp = ifp;
nbuf->nb_ifid = ifid;
nbuf_reset(nbuf);
}
void
nbuf_reset(nbuf_t *nbuf)
{
struct mbuf *m = nbuf->nb_mbuf0;
nbuf->nb_mbuf = m;
nbuf->nb_nptr = mtod(m, void *);
}
void *
nbuf_dataptr(nbuf_t *nbuf)
{
KASSERT(nbuf->nb_nptr);
return nbuf->nb_nptr;
}
size_t
nbuf_offset(const nbuf_t *nbuf)
{
const struct mbuf *m = nbuf->nb_mbuf;
const unsigned off = (uintptr_t)nbuf->nb_nptr - mtod(m, uintptr_t);
const int poff = m_length(nbuf->nb_mbuf0) - m_length(m) + off;
return poff;
}
struct mbuf *
nbuf_head_mbuf(nbuf_t *nbuf)
{
return nbuf->nb_mbuf0;
}
bool
nbuf_flag_p(const nbuf_t *nbuf, int flag)
{
return (nbuf->nb_flags & flag) != 0;
}
void
nbuf_unset_flag(nbuf_t *nbuf, int flag)
{
nbuf->nb_flags &= ~flag;
}
/*
* nbuf_advance: advance in nbuf or chain by specified amount of bytes and,
* if requested, ensure that the area *after* advance is contiguous.
*
* => Returns new pointer to data in nbuf or NULL if offset is invalid.
* => Current nbuf and the offset is stored in the nbuf metadata.
*/
void *
nbuf_advance(nbuf_t *nbuf, size_t len, size_t ensure)
{
struct mbuf *m = nbuf->nb_mbuf;
unsigned off, wmark;
uint8_t *d;
/* Offset with amount to advance. */
off = (uintptr_t)nbuf->nb_nptr - mtod(m, uintptr_t) + len;
wmark = m_buflen(m);
/* Find the mbuf according to offset. */
while (__predict_false(wmark <= off)) {
m = m_next_ptr(m);
if (__predict_false(m == NULL)) {
/*
* If end of the chain, then the offset is
* higher than packet length.
*/
return NULL;
}
wmark += m_buflen(m);
}
KASSERT(off < m_length(nbuf->nb_mbuf0));
/* Offset in mbuf data. */
d = mtod(m, uint8_t *);
KASSERT(off >= (wmark - m_buflen(m)));
d += (off - (wmark - m_buflen(m)));
nbuf->nb_mbuf = m;
nbuf->nb_nptr = d;
if (ensure) {
/* Ensure contiguousness (may change nbuf chain). */
d = nbuf_ensure_contig(nbuf, ensure);
}
return d;
}
/*
* nbuf_ensure_contig: check whether the specified length from the current
* point in the nbuf is contiguous. If not, rearrange the chain to be so.
*
* => Returns pointer to the data at the current offset in the buffer.
* => Returns NULL on failure and nbuf becomes invalid.
*/
void *
nbuf_ensure_contig(nbuf_t *nbuf, size_t len)
{
const struct mbuf * const n = nbuf->nb_mbuf;
const size_t off = (uintptr_t)nbuf->nb_nptr - mtod(n, uintptr_t);
KASSERT(off <= m_buflen(n));
if (__predict_false(m_buflen(n) < (off + len))) {
struct mbuf *m = nbuf->nb_mbuf0;
const size_t foff = nbuf_offset(nbuf);
const size_t plen = m_length(m);
const size_t mlen = m_buflen(m);
size_t target;
bool success;
//npf_stats_inc(npf, NPF_STAT_NBUF_NONCONTIG);
/* Attempt to round-up to NBUF_ENSURE_ALIGN bytes. */
if ((target = NBUF_ENSURE_ROUNDUP(foff + len)) > plen) {
target = foff + len;
}
/* Rearrange the chain to be contiguous. */
KASSERT(m_flags_p(m, M_PKTHDR));
success = m_ensure_contig(&m, target);
KASSERT(m != NULL);
/* If no change in the chain: return what we have. */
if (m == nbuf->nb_mbuf0 && m_buflen(m) == mlen) {
return success ? nbuf->nb_nptr : NULL;
}
/*
* The mbuf chain was re-arranged. Update the pointers
* accordingly and indicate that the references to the data
* might need a reset.
*/
KASSERT(m_flags_p(m, M_PKTHDR));
nbuf->nb_mbuf0 = m;
nbuf->nb_mbuf = m;
KASSERT(foff < m_buflen(m) && foff < m_length(m));
nbuf->nb_nptr = mtod(m, uint8_t *) + foff;
nbuf->nb_flags |= NBUF_DATAREF_RESET;
if (!success) {
//npf_stats_inc(npf, NPF_STAT_NBUF_CONTIG_FAIL);
return NULL;
}
}
return nbuf->nb_nptr;
}
void *
nbuf_ensure_writable(nbuf_t *nbuf, size_t len)
{
struct mbuf *m = nbuf->nb_mbuf;
const unsigned off = (uintptr_t)nbuf->nb_nptr - mtod(m, uintptr_t);
const int tlen = off + len;
bool head_buf;
KASSERT(off < m_length(nbuf->nb_mbuf0));
if (!M_UNWRITABLE(m, tlen)) {
return nbuf->nb_nptr;
}
head_buf = (nbuf->nb_mbuf0 == m);
if (m_makewritable(&m, 0, tlen, M_NOWAIT)) {
memset(nbuf, 0, sizeof(nbuf_t));
return NULL;
}
if (head_buf) {
KASSERT(m_flags_p(m, M_PKTHDR));
KASSERT(off < m_length(m));
nbuf->nb_mbuf0 = m;
}
nbuf->nb_mbuf = m;
nbuf->nb_nptr = mtod(m, uint8_t *) + off;
return nbuf->nb_nptr;
}
bool
nbuf_cksum_barrier(nbuf_t *nbuf, int di)
{
#ifdef _KERNEL
struct mbuf *m;
if (di != PFIL_OUT) {
return false;
}
m = nbuf->nb_mbuf0;
KASSERT(m_flags_p(m, M_PKTHDR));
if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4 | M_CSUM_UDPv4)) {
in_undefer_cksum_tcpudp(m);
m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4 | M_CSUM_UDPv4);
return true;
}
#ifdef INET6
if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv6 | M_CSUM_UDPv6)) {
in6_undefer_cksum_tcpudp(m);
m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv6 | M_CSUM_UDPv6);
return true;
}
#endif
#else
(void)nbuf; (void)di;
#endif
return false;
}
/*
* npf_mbuf_add_tag: associate a tag with the network buffer.
*
* => Returns 0 on success or error number on failure.
*/
int
npf_mbuf_add_tag(nbuf_t *nbuf, struct mbuf *m, uint32_t val)
{
#ifdef _KERNEL
struct m_tag *mt;
uint32_t *dat;
KASSERT(m_flags_p(m, M_PKTHDR));
mt = m_tag_get(PACKET_TAG_NPF, sizeof(uint32_t), M_NOWAIT);
if (mt == NULL) {
return ENOMEM;
}
dat = (uint32_t *)(mt + 1);
*dat = val;
m_tag_prepend(m, mt);
return 0;
#else
if (!nbuf->nb_mops->set_tag) {
return ENOTSUP;
}
return nbuf->nb_mops->set_tag(m, val);
#endif
}
/*
* nbuf_add_tag: associate a tag with the network buffer.
*
* => Returns 0 on success or error number on failure.
*/
int
nbuf_add_tag(nbuf_t *nbuf, uint32_t val)
{
struct mbuf *m = nbuf->nb_mbuf0;
return npf_mbuf_add_tag(nbuf, m, val);
}
/*
* nbuf_find_tag: find a tag associated with a network buffer.
*
* => Returns 0 on success or error number on failure.
*/
int
nbuf_find_tag(nbuf_t *nbuf, uint32_t *val)
{
struct mbuf *m = nbuf->nb_mbuf0;
#ifdef _KERNEL
struct m_tag *mt;
KASSERT(m_flags_p(m, M_PKTHDR));
mt = m_tag_find(m, PACKET_TAG_NPF);
if (mt == NULL) {
return EINVAL;
}
*val = *(uint32_t *)(mt + 1);
return 0;
#else
if (!nbuf->nb_mops->get_tag) {
return ENOTSUP;
}
return nbuf->nb_mops->get_tag(m, val);
#endif
}
|