Coverage Report

Created: 2026-04-12 06:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openvswitch/lib/dp-packet.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2016 Nicira, Inc.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at:
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
17
#include <config.h>
18
#include <stdlib.h>
19
#include <string.h>
20
21
#include "dp-packet.h"
22
#include "netdev-afxdp.h"
23
#include "netdev-dpdk.h"
24
#include "netdev-provider.h"
25
#include "openvswitch/dynamic-string.h"
26
#include "util.h"
27
28
static void
29
dp_packet_init__(struct dp_packet *b, size_t allocated, enum dp_packet_source source)
30
48.2k
{
31
48.2k
    dp_packet_set_allocated(b, allocated);
32
48.2k
    b->source = source;
33
48.2k
    dp_packet_reset_offsets(b);
34
48.2k
    pkt_metadata_init(&b->md, 0);
35
48.2k
    dp_packet_reset_cutlen(b);
36
48.2k
    dp_packet_reset_offload(b);
37
48.2k
    dp_packet_set_tso_segsz(b, 0);
38
    /* Initialize implementation-specific fields of dp_packet. */
39
48.2k
    dp_packet_init_specific(b);
40
    /* By default assume the packet type to be Ethernet. */
41
48.2k
    b->packet_type = htonl(PT_ETH);
42
48.2k
}
43
44
static void
45
dp_packet_use__(struct dp_packet *b, void *base, size_t allocated,
46
             enum dp_packet_source source)
47
48.2k
{
48
48.2k
    dp_packet_set_base(b, base);
49
48.2k
    dp_packet_set_data(b, base);
50
48.2k
    dp_packet_set_size(b, 0);
51
52
48.2k
    dp_packet_init__(b, allocated, source);
53
48.2k
}
54
55
/* Initializes 'b' as an empty dp_packet that contains the 'allocated' bytes of
56
 * memory starting at 'base'.  'base' should be the first byte of a region
57
 * obtained from malloc().  It will be freed (with free()) if 'b' is resized or
58
 * freed. */
59
void
60
dp_packet_use(struct dp_packet *b, void *base, size_t allocated)
61
0
{
62
0
    dp_packet_use__(b, base, allocated, DPBUF_MALLOC);
63
0
}
64
65
#if HAVE_AF_XDP
66
/* Initialize 'b' as an empty dp_packet that contains
67
 * memory starting at AF_XDP umem base.
68
 */
69
void
70
dp_packet_use_afxdp(struct dp_packet *b, void *data, size_t allocated,
71
                    size_t headroom)
72
{
73
    dp_packet_set_base(b, (char *)data - headroom);
74
    dp_packet_set_data(b, data);
75
    dp_packet_set_size(b, 0);
76
77
    dp_packet_init__(b, allocated, DPBUF_AFXDP);
78
}
79
#endif
80
81
/* Initializes 'b' as an empty dp_packet that contains the 'allocated' bytes of
82
 * memory starting at 'base'.  'base' should point to a buffer on the stack.
83
 * (Nothing actually relies on 'base' being allocated on the stack.  It could
84
 * be static or malloc()'d memory.  But stack space is the most common use
85
 * case.)
86
 *
87
 * 'base' should be appropriately aligned.  Using an array of uint32_t or
88
 * uint64_t for the buffer is a reasonable way to ensure appropriate alignment
89
 * for 32- or 64-bit data.
90
 *
91
 * An dp_packet operation that requires reallocating data will copy the provided
92
 * buffer into a malloc()'d buffer.  Thus, it is wise to call dp_packet_uninit()
93
 * on an dp_packet initialized by this function, so that if it expanded into the
94
 * heap, that memory is freed. */
95
void
96
dp_packet_use_stub(struct dp_packet *b, void *base, size_t allocated)
97
0
{
98
0
    dp_packet_use__(b, base, allocated, DPBUF_STUB);
99
0
}
100
101
/* Initializes 'b' as an dp_packet whose data starts at 'data' and continues for
102
 * 'size' bytes.  This is appropriate for an dp_packet that will be used to
103
 * inspect existing data, without moving it around or reallocating it, and
104
 * generally without modifying it at all.
105
 *
106
 * An dp_packet operation that requires reallocating data will assert-fail if this
107
 * function was used to initialize it. */
108
void
109
dp_packet_use_const(struct dp_packet *b, const void *data, size_t size)
110
48.2k
{
111
48.2k
    dp_packet_use__(b, CONST_CAST(void *, data), size, DPBUF_STACK);
112
48.2k
    dp_packet_set_size(b, size);
113
48.2k
}
114
115
/* Initializes 'b' as a DPDK dp-packet, which must have been allocated from a
116
 * DPDK memory pool. */
117
void
118
dp_packet_init_dpdk(struct dp_packet *b)
119
0
{
120
0
    b->source = DPBUF_DPDK;
121
0
}
122
123
/* Initializes 'b' as an empty dp_packet with an initial capacity of 'size'
124
 * bytes. */
125
void
126
dp_packet_init(struct dp_packet *b, size_t size)
127
0
{
128
0
    dp_packet_use(b, size ? xmalloc(size) : NULL, size);
129
0
}
130
131
/* Frees memory that 'b' points to. */
132
void
133
dp_packet_uninit(struct dp_packet *b)
134
0
{
135
0
    if (b) {
136
0
        if (b->source == DPBUF_MALLOC) {
137
0
            free(dp_packet_base(b));
138
0
        } else if (b->source == DPBUF_DPDK) {
139
0
            free_dpdk_buf(b);
140
0
        } else if (b->source == DPBUF_AFXDP) {
141
0
            free_afxdp_buf(b);
142
0
        }
143
0
    }
144
0
}
145
146
/* Creates and returns a new dp_packet with an initial capacity of 'size'
147
 * bytes. */
148
struct dp_packet *
149
dp_packet_new(size_t size)
150
0
{
151
#ifdef DPDK_NETDEV
152
    struct dp_packet *b = xmalloc_cacheline(sizeof *b);
153
#else
154
0
    struct dp_packet *b = xmalloc(sizeof *b);
155
0
#endif
156
0
    dp_packet_init(b, size);
157
0
    return b;
158
0
}
159
160
/* Creates and returns a new dp_packet with an initial capacity of 'size +
161
 * headroom' bytes, reserving the first 'headroom' bytes as headroom. */
162
struct dp_packet *
163
dp_packet_new_with_headroom(size_t size, size_t headroom)
164
0
{
165
0
    struct dp_packet *b = dp_packet_new(size + headroom);
166
0
    dp_packet_reserve(b, headroom);
167
0
    return b;
168
0
}
169
170
/* Creates and returns a new dp_packet that initially contains a copy of the
171
 * 'dp_packet_size(buffer)' bytes of data starting at 'buffer->data' with no headroom or
172
 * tailroom. */
173
struct dp_packet *
174
dp_packet_clone(const struct dp_packet *buffer)
175
0
{
176
0
    ovs_assert(buffer);
177
0
    return dp_packet_clone_with_headroom(buffer, 0);
178
0
}
179
180
/* Creates and returns a new dp_packet whose data are copied from 'buffer'.
181
 * The returned dp_packet will additionally have 'headroom' bytes of
182
 * headroom. */
183
struct dp_packet *
184
dp_packet_clone_with_headroom(const struct dp_packet *buffer, size_t headroom)
185
0
{
186
0
    const void *data_dp = dp_packet_data(buffer);
187
0
    struct dp_packet *new_buffer;
188
0
    uint32_t mark;
189
190
0
    ovs_assert(data_dp);
191
192
0
    new_buffer = dp_packet_clone_data_with_headroom(data_dp,
193
0
                                                    dp_packet_size(buffer),
194
0
                                                    headroom);
195
    /* Copy the following fields into the returned buffer: l2_pad_size,
196
     * l2_5_ofs, l3_ofs, l4_ofs, cutlen, packet_type, offloads and md. */
197
0
    memcpy(&new_buffer->l2_pad_size, &buffer->l2_pad_size,
198
0
            sizeof(struct dp_packet) -
199
0
            offsetof(struct dp_packet, l2_pad_size));
200
201
0
    dp_packet_set_tso_segsz(new_buffer, dp_packet_get_tso_segsz(buffer));
202
203
0
    if (dp_packet_rss_valid(buffer)) {
204
0
        dp_packet_set_rss_hash(new_buffer, dp_packet_get_rss_hash(buffer));
205
0
    }
206
0
    if (dp_packet_has_flow_mark(buffer, &mark)) {
207
0
        dp_packet_set_flow_mark(new_buffer, mark);
208
0
    }
209
210
0
    return new_buffer;
211
0
}
212
213
/* Creates and returns a new dp_packet that initially contains a copy of the
214
 * 'size' bytes of data starting at 'data' with no headroom or tailroom. */
215
struct dp_packet *
216
dp_packet_clone_data(const void *data, size_t size)
217
0
{
218
0
    return dp_packet_clone_data_with_headroom(data, size, 0);
219
0
}
220
221
/* Creates and returns a new dp_packet that initially contains 'headroom' bytes of
222
 * headroom followed by a copy of the 'size' bytes of data starting at
223
 * 'data'. */
224
struct dp_packet *
225
dp_packet_clone_data_with_headroom(const void *data, size_t size, size_t headroom)
226
0
{
227
0
    struct dp_packet *b = dp_packet_new_with_headroom(size, headroom);
228
0
    dp_packet_put(b, data, size);
229
0
    return b;
230
0
}
231
232
static void
233
dp_packet_copy__(struct dp_packet *b, uint8_t *new_base,
234
              size_t new_headroom, size_t new_tailroom)
235
0
{
236
0
    const uint8_t *old_base = dp_packet_base(b);
237
0
    size_t old_headroom = dp_packet_headroom(b);
238
0
    size_t old_tailroom = dp_packet_tailroom(b);
239
0
    size_t copy_headroom = MIN(old_headroom, new_headroom);
240
0
    size_t copy_tailroom = MIN(old_tailroom, new_tailroom);
241
242
0
    memcpy(&new_base[new_headroom - copy_headroom],
243
0
           &old_base[old_headroom - copy_headroom],
244
0
           copy_headroom + dp_packet_size(b) + copy_tailroom);
245
0
}
246
247
/* Reallocates 'b' so that it has exactly 'new_headroom' and 'new_tailroom'
248
 * bytes of headroom and tailroom, respectively. */
249
void
250
dp_packet_resize(struct dp_packet *b, size_t new_headroom, size_t new_tailroom)
251
0
{
252
0
    void *new_base, *new_data;
253
0
    size_t new_allocated;
254
255
0
    new_allocated = new_headroom + dp_packet_size(b) + new_tailroom;
256
257
0
    switch (b->source) {
258
0
    case DPBUF_DPDK: {
259
#ifdef DPDK_NETDEV
260
        uint32_t extbuf_len;
261
262
        extbuf_len = netdev_dpdk_extbuf_size(new_allocated);
263
        ovs_assert(extbuf_len <= UINT16_MAX);
264
        new_base = netdev_dpdk_extbuf_allocate(extbuf_len);
265
        if (!new_base) {
266
            out_of_memory();
267
        }
268
        dp_packet_copy__(b, new_base, new_headroom, new_tailroom);
269
        netdev_dpdk_extbuf_replace(b, new_base, extbuf_len);
270
        /* Because of alignment, we may have gained a bit more tailroom than
271
         * expected.  Update from the currently allocated length which got
272
         * adjusted by rte_pktmbuf_attach_extbuf(). */
273
        new_allocated = dp_packet_get_allocated(b);
274
        break;
275
#else
276
0
        OVS_NOT_REACHED();
277
0
#endif
278
0
    }
279
280
0
    case DPBUF_MALLOC:
281
0
        if (new_headroom == dp_packet_headroom(b)) {
282
0
            new_base = xrealloc(dp_packet_base(b), new_allocated);
283
0
        } else {
284
0
            new_base = xmalloc(new_allocated);
285
0
            dp_packet_copy__(b, new_base, new_headroom, new_tailroom);
286
0
            free(dp_packet_base(b));
287
0
        }
288
0
        break;
289
290
0
    case DPBUF_STACK:
291
0
        OVS_NOT_REACHED();
292
293
0
    case DPBUF_AFXDP:
294
0
        OVS_NOT_REACHED();
295
296
0
    case DPBUF_STUB:
297
0
        b->source = DPBUF_MALLOC;
298
0
        new_base = xmalloc(new_allocated);
299
0
        dp_packet_copy__(b, new_base, new_headroom, new_tailroom);
300
0
        break;
301
302
0
    default:
303
0
        OVS_NOT_REACHED();
304
0
    }
305
306
0
    dp_packet_set_allocated(b, new_allocated);
307
0
    dp_packet_set_base(b, new_base);
308
309
0
    new_data = (char *) new_base + new_headroom;
310
0
    if (dp_packet_data(b) != new_data) {
311
0
        dp_packet_set_data(b, new_data);
312
0
    }
313
0
}
314
315
/* Ensures that 'b' has room for at least 'size' bytes at its tail end,
316
 * reallocating and copying its data if necessary.  Its headroom, if any, is
317
 * preserved. */
318
void
319
dp_packet_prealloc_tailroom(struct dp_packet *b, size_t size)
320
0
{
321
0
    if ((size && !dp_packet_base(b)) || (size > dp_packet_tailroom(b))) {
322
0
        dp_packet_resize(b, dp_packet_headroom(b), MAX(size, 64));
323
0
    }
324
0
}
325
326
/* Ensures that 'b' has room for at least 'size' bytes at its head,
327
 * reallocating and copying its data if necessary.  Its tailroom, if any, is
328
 * preserved. */
329
void
330
dp_packet_prealloc_headroom(struct dp_packet *b, size_t size)
331
0
{
332
0
    if (size > dp_packet_headroom(b)) {
333
0
        dp_packet_resize(b, MAX(size, 64), dp_packet_tailroom(b));
334
0
    }
335
0
}
336
337
/* Shifts all of the data within the allocated space in 'b' by 'delta' bytes.
338
 * For example, a 'delta' of 1 would cause each byte of data to move one byte
339
 * forward (from address 'p' to 'p+1'), and a 'delta' of -1 would cause each
340
 * byte to move one byte backward (from 'p' to 'p-1'). */
341
void
342
dp_packet_shift(struct dp_packet *b, int delta)
343
0
{
344
0
    ovs_assert(delta > 0 ? delta <= dp_packet_tailroom(b)
345
0
               : delta < 0 ? -delta <= dp_packet_headroom(b)
346
0
               : true);
347
348
0
    if (delta != 0) {
349
0
        const void *data_dp = dp_packet_data(b);
350
0
        char *dst = (char *) data_dp + delta;
351
352
0
        ovs_assert(data_dp);
353
354
0
        memmove(dst, data_dp, dp_packet_size(b));
355
0
        dp_packet_set_data(b, dst);
356
0
    }
357
0
}
358
359
/* Appends 'size' bytes of data to the tail end of 'b', reallocating and
360
 * copying its data if necessary.  Returns a pointer to the first byte of the
361
 * new data, which is left uninitialized. */
362
void *
363
dp_packet_put_uninit(struct dp_packet *b, size_t size)
364
0
{
365
0
    void *p;
366
0
    dp_packet_prealloc_tailroom(b, size);
367
0
    p = dp_packet_tail(b);
368
0
    dp_packet_set_size(b, dp_packet_size(b) + size);
369
0
    return p;
370
0
}
371
372
/* Appends 'size' zeroed bytes to the tail end of 'b'.  Data in 'b' is
373
 * reallocated and copied if necessary.  Returns a pointer to the first byte of
374
 * the data's location in the dp_packet. */
375
void *
376
dp_packet_put_zeros(struct dp_packet *b, size_t size)
377
0
{
378
0
    void *dst = dp_packet_put_uninit(b, size);
379
0
    nullable_memset(dst, 0, size);
380
0
    return dst;
381
0
}
382
383
/* Appends the 'size' bytes of data in 'p' to the tail end of 'b'.  Data in 'b'
384
 * is reallocated and copied if necessary.  Returns a pointer to the first
385
 * byte of the data's location in the dp_packet. */
386
void *
387
dp_packet_put(struct dp_packet *b, const void *p, size_t size)
388
0
{
389
0
    void *dst = dp_packet_put_uninit(b, size);
390
0
    nullable_memcpy(dst, p, size);
391
0
    return dst;
392
0
}
393
394
/* Parses as many pairs of hex digits as possible (possibly separated by
395
 * spaces) from the beginning of 's', appending bytes for their values to 'b'.
396
 * Returns the first character of 's' that is not the first of a pair of hex
397
 * digits.  If 'n' is nonnull, stores the number of bytes added to 'b' in
398
 * '*n'. */
399
char *
400
dp_packet_put_hex(struct dp_packet *b, const char *s, size_t *n)
401
0
{
402
0
    size_t initial_size = dp_packet_size(b);
403
0
    for (;;) {
404
0
        uint8_t byte;
405
0
        bool ok;
406
407
0
        s += strspn(s, " \t\r\n");
408
0
        byte = hexits_value(s, 2, &ok);
409
0
        if (!ok) {
410
0
            if (n) {
411
0
                *n = dp_packet_size(b) - initial_size;
412
0
            }
413
0
            return CONST_CAST(char *, s);
414
0
        }
415
416
0
        dp_packet_put(b, &byte, 1);
417
0
        s += 2;
418
0
    }
419
0
}
420
421
/* Reserves 'size' bytes of headroom so that they can be later allocated with
422
 * dp_packet_push_uninit() without reallocating the dp_packet. */
423
void
424
dp_packet_reserve(struct dp_packet *b, size_t size)
425
0
{
426
0
    ovs_assert(!dp_packet_size(b));
427
0
    dp_packet_prealloc_tailroom(b, size);
428
0
    dp_packet_set_data(b, (char*)dp_packet_data(b) + size);
429
0
}
430
431
/* Reserves 'headroom' bytes at the head and 'tailroom' at the end so that
432
 * they can be later allocated with dp_packet_push_uninit() or
433
 * dp_packet_put_uninit() without reallocating the dp_packet. */
434
void
435
dp_packet_reserve_with_tailroom(struct dp_packet *b, size_t headroom,
436
                             size_t tailroom)
437
0
{
438
0
    ovs_assert(!dp_packet_size(b));
439
0
    dp_packet_prealloc_tailroom(b, headroom + tailroom);
440
0
    dp_packet_set_data(b, (char*)dp_packet_data(b) + headroom);
441
0
}
442
443
/* Prefixes 'size' bytes to the head end of 'b', reallocating and copying its
444
 * data if necessary.  Returns a pointer to the first byte of the data's
445
 * location in the dp_packet.  The new data is left uninitialized. */
446
void *
447
dp_packet_push_uninit(struct dp_packet *b, size_t size)
448
0
{
449
0
    dp_packet_prealloc_headroom(b, size);
450
0
    dp_packet_set_data(b, (char*)dp_packet_data(b) - size);
451
0
    dp_packet_set_size(b, dp_packet_size(b) + size);
452
0
    return dp_packet_data(b);
453
0
}
454
455
/* Prefixes 'size' zeroed bytes to the head end of 'b', reallocating and
456
 * copying its data if necessary.  Returns a pointer to the first byte of the
457
 * data's location in the dp_packet. */
458
void *
459
dp_packet_push_zeros(struct dp_packet *b, size_t size)
460
0
{
461
0
    void *dst = dp_packet_push_uninit(b, size);
462
0
    nullable_memset(dst, 0, size);
463
0
    return dst;
464
0
}
465
466
/* Copies the 'size' bytes starting at 'p' to the head end of 'b', reallocating
467
 * and copying its data if necessary.  Returns a pointer to the first byte of
468
 * the data's location in the dp_packet. */
469
void *
470
dp_packet_push(struct dp_packet *b, const void *p, size_t size)
471
0
{
472
0
    void *dst = dp_packet_push_uninit(b, size);
473
0
    nullable_memcpy(dst, p, size);
474
0
    return dst;
475
0
}
476
477
/* Returns the data in 'b' as a block of malloc()'d memory and frees the buffer
478
 * within 'b'.  (If 'b' itself was dynamically allocated, e.g. with
479
 * dp_packet_new(), then it should still be freed with, e.g., dp_packet_delete().) */
480
void *
481
dp_packet_steal_data(struct dp_packet *b)
482
0
{
483
0
    void *p;
484
0
    ovs_assert(b->source != DPBUF_DPDK);
485
0
    ovs_assert(b->source != DPBUF_AFXDP);
486
487
0
    if (b->source == DPBUF_MALLOC && dp_packet_data(b) == dp_packet_base(b)) {
488
0
        p = dp_packet_data(b);
489
0
    } else {
490
0
        p = xmemdup(dp_packet_data(b), dp_packet_size(b));
491
0
        if (b->source == DPBUF_MALLOC) {
492
0
            free(dp_packet_base(b));
493
0
        }
494
0
    }
495
0
    dp_packet_set_base(b, NULL);
496
0
    dp_packet_set_data(b, NULL);
497
0
    return p;
498
0
}
499
500
static inline void
501
dp_packet_adjust_layer_offset(uint16_t *offset, int increment)
502
0
{
503
0
    if (*offset != UINT16_MAX) {
504
0
        *offset += increment;
505
0
    }
506
0
}
507
508
/* Adjust the size of the l2_5 portion of the dp_packet, updating the l2
509
 * pointer and the layer offsets.  The caller is responsible for
510
 * modifying the contents. */
511
void *
512
dp_packet_resize_l2_5(struct dp_packet *b, int increment)
513
0
{
514
0
    if (increment >= 0) {
515
0
        dp_packet_push_uninit(b, increment);
516
0
    } else {
517
0
        dp_packet_pull(b, -increment);
518
0
    }
519
520
    /* Adjust layer offsets after l2_5. */
521
0
    dp_packet_adjust_layer_offset(&b->l3_ofs, increment);
522
0
    dp_packet_adjust_layer_offset(&b->l4_ofs, increment);
523
0
    dp_packet_adjust_layer_offset(&b->inner_l3_ofs, increment);
524
0
    dp_packet_adjust_layer_offset(&b->inner_l4_ofs, increment);
525
526
0
    return dp_packet_data(b);
527
0
}
528
529
/* Adjust the size of the l2 portion of the dp_packet, updating the l2
530
 * pointer and the layer offsets.  The caller is responsible for
531
 * modifying the contents. */
532
void *
533
dp_packet_resize_l2(struct dp_packet *b, int increment)
534
0
{
535
0
    dp_packet_resize_l2_5(b, increment);
536
0
    dp_packet_adjust_layer_offset(&b->l2_5_ofs, increment);
537
0
    return dp_packet_data(b);
538
0
}
539
540
bool
541
dp_packet_compare_offsets(struct dp_packet *b1, struct dp_packet *b2,
542
                          struct ds *err_str)
543
0
{
544
0
    if ((b1->l2_pad_size != b2->l2_pad_size) ||
545
0
        (b1->l2_5_ofs != b2->l2_5_ofs) ||
546
0
        (b1->l3_ofs != b2->l3_ofs) ||
547
0
        (b1->l4_ofs != b2->l4_ofs) ||
548
0
        (b1->inner_l3_ofs != b2->inner_l3_ofs) ||
549
0
        (b1->inner_l4_ofs != b2->inner_l4_ofs)) {
550
0
        if (err_str) {
551
0
            ds_put_format(err_str, "Packet offset comparison failed\n");
552
0
            ds_put_format(err_str, "Buffer 1 offsets: l2_pad_size %u,"
553
0
                          " l2_5_ofs : %u l3_ofs %u, l4_ofs %u,"
554
0
                          " inner_l3_ofs %u, inner_l4_ofs %u\n",
555
0
                          b1->l2_pad_size, b1->l2_5_ofs,
556
0
                          b1->l3_ofs, b1->l4_ofs,
557
0
                          b1->inner_l3_ofs, b1->inner_l4_ofs);
558
0
            ds_put_format(err_str, "Buffer 2 offsets: l2_pad_size %u,"
559
0
                          " l2_5_ofs : %u l3_ofs %u, l4_ofs %u,"
560
0
                          " inner_l3_ofs %u, inner_l4_ofs %u\n",
561
0
                          b2->l2_pad_size, b2->l2_5_ofs,
562
0
                          b2->l3_ofs, b2->l4_ofs,
563
0
                          b2->inner_l3_ofs, b2->inner_l4_ofs);
564
0
        }
565
0
        return false;
566
0
    }
567
0
    return true;
568
0
}
569
570
/* Checks if the packet 'p' is compatible with netdev_ol_flags 'flags'
571
 * and if not, updates the packet with the software fall back. */
572
void
573
dp_packet_ol_send_prepare(struct dp_packet *p, uint64_t flags)
574
0
{
575
0
    if (!dp_packet_inner_ip_checksum_partial(p)
576
0
        && !dp_packet_inner_l4_checksum_partial(p)) {
577
578
0
        if (!dp_packet_ip_checksum_partial(p)
579
0
            && !dp_packet_l4_checksum_partial(p)) {
580
            /* No checksumming needed. */
581
0
            return;
582
0
        }
583
584
0
        if (OVS_UNLIKELY(dp_packet_tunnel(p) && !dp_packet_get_tso_segsz(p))) {
585
0
            p->offloads &= ~DP_PACKET_OL_TUNNEL_MASK;
586
0
        }
587
0
    }
588
589
0
    if (!dp_packet_tunnel(p)) {
590
0
        if (dp_packet_ip_checksum_partial(p)
591
0
            && !(flags & NETDEV_TX_OFFLOAD_IPV4_CKSUM)) {
592
0
            dp_packet_ip_set_header_csum(p, false);
593
0
        }
594
595
0
        if (dp_packet_l4_checksum_partial(p)) {
596
0
            if (dp_packet_l4_proto_tcp(p)) {
597
0
                if (!(flags & NETDEV_TX_OFFLOAD_TCP_CKSUM)) {
598
0
                    packet_tcp_complete_csum(p, false);
599
0
                }
600
0
            } else if (dp_packet_l4_proto_udp(p)) {
601
0
                if (!(flags & NETDEV_TX_OFFLOAD_UDP_CKSUM)) {
602
0
                    packet_udp_complete_csum(p, false);
603
0
                }
604
0
            } else {
605
0
                ovs_assert(dp_packet_l4_proto_sctp(p));
606
0
                if (!(flags & NETDEV_TX_OFFLOAD_SCTP_CKSUM)) {
607
0
                    packet_sctp_complete_csum(p, false);
608
0
                }
609
0
            }
610
0
        }
611
612
0
        return;
613
0
    }
614
615
0
    if (dp_packet_tunnel_geneve(p) || dp_packet_tunnel_vxlan(p)) {
616
        /* If the TX interface doesn't support UDP tunnel offload but does
617
         * support inner SCTP checksum offload and an outer UDP checksum is
618
         * required, then we can't offload inner checksum either as that would
619
         * invalidate the outer checksum. */
620
0
        if (!(flags & NETDEV_TX_OFFLOAD_OUTER_UDP_CKSUM)
621
0
            && dp_packet_l4_checksum_partial(p)) {
622
0
            flags &= ~NETDEV_TX_OFFLOAD_SCTP_CKSUM;
623
0
            if (!packet_udp_tunnel_csum(p)) {
624
                /* Similarly to the previous comment, since the outer UDP
625
                 * checksum optimisation did not happen, invalidate inner
626
                 * checksum offloads support. */
627
0
                flags &= ~(NETDEV_TX_OFFLOAD_TCP_CKSUM |
628
0
                           NETDEV_TX_OFFLOAD_UDP_CKSUM |
629
0
                           NETDEV_TX_OFFLOAD_IPV4_CKSUM);
630
0
            }
631
0
        }
632
0
    }
633
634
0
    if (dp_packet_inner_ip_checksum_partial(p)
635
0
        && !(flags & NETDEV_TX_OFFLOAD_IPV4_CKSUM)) {
636
0
        dp_packet_ip_set_header_csum(p, true);
637
0
    }
638
639
0
    if (dp_packet_inner_l4_checksum_partial(p)) {
640
0
        if (dp_packet_inner_l4_proto_tcp(p)) {
641
0
            if (!(flags & NETDEV_TX_OFFLOAD_TCP_CKSUM)) {
642
0
                packet_tcp_complete_csum(p, true);
643
0
            }
644
0
        } else if (dp_packet_inner_l4_proto_udp(p)) {
645
0
            if (!(flags & NETDEV_TX_OFFLOAD_UDP_CKSUM)) {
646
0
                packet_udp_complete_csum(p, true);
647
0
            }
648
0
        } else {
649
0
            ovs_assert(dp_packet_inner_l4_proto_sctp(p));
650
0
            if (!(flags & NETDEV_TX_OFFLOAD_SCTP_CKSUM)) {
651
0
                packet_sctp_complete_csum(p, true);
652
0
            }
653
0
        }
654
0
    }
655
656
0
    if (dp_packet_ip_checksum_partial(p)
657
0
        && !(flags & NETDEV_TX_OFFLOAD_OUTER_IP_CKSUM)) {
658
0
        dp_packet_ip_set_header_csum(p, false);
659
0
    }
660
661
0
    if (dp_packet_l4_checksum_partial(p)) {
662
0
        ovs_assert(dp_packet_l4_proto_udp(p));
663
0
        if (!(flags & NETDEV_TX_OFFLOAD_OUTER_UDP_CKSUM)) {
664
            packet_udp_complete_csum(p, false);
665
0
        }
666
0
    }
667
0
}