]> git.proxmox.com Git - mirror_ovs.git/blame - lib/dp-packet.h
docs/dpdk: Consolidate pmd-cpu-mask references.
[mirror_ovs.git] / lib / dp-packet.h
CommitLineData
91088554 1/*
cf62fa4c 2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
91088554
DDP
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
cf62fa4c
PS
17#ifndef DPBUF_H
18#define DPBUF_H 1
91088554 19
cf62fa4c
PS
20#include <stddef.h>
21#include <stdint.h>
01961bbd
DDP
22
23#ifdef DPDK_NETDEV
24#include <rte_config.h>
25#include <rte_mbuf.h>
26#endif
27
28#include "netdev-dpdk.h"
b19bab5b 29#include "openvswitch/list.h"
cf62fa4c
PS
30#include "packets.h"
31#include "util.h"
2482b0b0 32#include "flow.h"
91088554
DDP
33
34#ifdef __cplusplus
35extern "C" {
36#endif
37
cf62fa4c
PS
38enum OVS_PACKED_ENUM dp_packet_source {
39 DPBUF_MALLOC, /* Obtained via malloc(). */
40 DPBUF_STACK, /* Un-movable stack space or static buffer. */
41 DPBUF_STUB, /* Starts on stack, may expand into heap. */
42 DPBUF_DPDK, /* buffer data is from DPDK allocated memory.
88cbc2dd 43 * ref to dp_packet_init_dpdk() in dp-packet.c. */
cf62fa4c 44};
91088554 45
eb18b2a6
PS
46#define DP_PACKET_CONTEXT_SIZE 64
47
82eb5b0a 48/* Buffer for holding packet data. A dp_packet is automatically reallocated
cf62fa4c 49 * as necessary if it grows too large for the available memory.
2482b0b0 50 * By default the packet type is set to Ethernet (PT_ETH).
cf62fa4c 51 */
e14deea0 52struct dp_packet {
cf62fa4c
PS
53#ifdef DPDK_NETDEV
54 struct rte_mbuf mbuf; /* DPDK mbuf */
55#else
b8e57534 56 void *base_; /* First byte of allocated space. */
11a6fbd5 57 uint16_t allocated_; /* Number of bytes allocated. */
b8e57534
MK
58 uint16_t data_ofs; /* First byte actually in use. */
59 uint32_t size_; /* Number of bytes in use. */
2bc1bbd2 60 uint32_t rss_hash; /* Packet hash. */
f2f44f5d 61 bool rss_hash_valid; /* Is the 'rss_hash' valid? */
61a2647e 62#endif
cf62fa4c 63 enum dp_packet_source source; /* Source of memory allocated as 'base'. */
82eb5b0a
DDP
64 uint8_t l2_pad_size; /* Detected l2 padding size.
65 * Padding is non-pullable. */
66 uint16_t l2_5_ofs; /* MPLS label stack offset, or UINT16_MAX */
67 uint16_t l3_ofs; /* Network-level header offset,
68 * or UINT16_MAX. */
69 uint16_t l4_ofs; /* Transport-level header offset,
70 or UINT16_MAX. */
aaca4fe0 71 uint32_t cutlen; /* length in bytes to cut from the end. */
2482b0b0 72 ovs_be32 packet_type; /* Packet type as defined in OpenFlow */
eb18b2a6
PS
73 union {
74 struct pkt_metadata md;
75 uint64_t data[DP_PACKET_CONTEXT_SIZE / 8];
76 };
91088554
DDP
77};
78
5a07c6e1 79static inline void *dp_packet_data(const struct dp_packet *);
cf62fa4c 80static inline void dp_packet_set_data(struct dp_packet *, void *);
5a07c6e1 81static inline void *dp_packet_base(const struct dp_packet *);
cf62fa4c
PS
82static inline void dp_packet_set_base(struct dp_packet *, void *);
83
84static inline uint32_t dp_packet_size(const struct dp_packet *);
85static inline void dp_packet_set_size(struct dp_packet *, uint32_t);
86
11a6fbd5
DDP
87static inline uint16_t dp_packet_get_allocated(const struct dp_packet *);
88static inline void dp_packet_set_allocated(struct dp_packet *, uint16_t);
89
5a07c6e1
DDP
90void *dp_packet_resize_l2(struct dp_packet *, int increment);
91void *dp_packet_resize_l2_5(struct dp_packet *, int increment);
2482b0b0 92static inline void *dp_packet_eth(const struct dp_packet *);
82eb5b0a 93static inline void dp_packet_reset_offsets(struct dp_packet *);
cf62fa4c
PS
94static inline uint8_t dp_packet_l2_pad_size(const struct dp_packet *);
95static inline void dp_packet_set_l2_pad_size(struct dp_packet *, uint8_t);
5a07c6e1 96static inline void *dp_packet_l2_5(const struct dp_packet *);
cf62fa4c 97static inline void dp_packet_set_l2_5(struct dp_packet *, void *);
5a07c6e1 98static inline void *dp_packet_l3(const struct dp_packet *);
cf62fa4c 99static inline void dp_packet_set_l3(struct dp_packet *, void *);
5a07c6e1 100static inline void *dp_packet_l4(const struct dp_packet *);
cf62fa4c
PS
101static inline void dp_packet_set_l4(struct dp_packet *, void *);
102static inline size_t dp_packet_l4_size(const struct dp_packet *);
103static inline const void *dp_packet_get_tcp_payload(const struct dp_packet *);
104static inline const void *dp_packet_get_udp_payload(const struct dp_packet *);
105static inline const void *dp_packet_get_sctp_payload(const struct dp_packet *);
106static inline const void *dp_packet_get_icmp_payload(const struct dp_packet *);
107static inline const void *dp_packet_get_nd_payload(const struct dp_packet *);
108
109void dp_packet_use(struct dp_packet *, void *, size_t);
110void dp_packet_use_stub(struct dp_packet *, void *, size_t);
111void dp_packet_use_const(struct dp_packet *, const void *, size_t);
112
5a07c6e1 113void dp_packet_init_dpdk(struct dp_packet *, size_t allocated);
cf62fa4c
PS
114
115void dp_packet_init(struct dp_packet *, size_t);
116void dp_packet_uninit(struct dp_packet *);
cf62fa4c
PS
117
118struct dp_packet *dp_packet_new(size_t);
119struct dp_packet *dp_packet_new_with_headroom(size_t, size_t headroom);
120struct dp_packet *dp_packet_clone(const struct dp_packet *);
121struct dp_packet *dp_packet_clone_with_headroom(const struct dp_packet *,
5a07c6e1 122 size_t headroom);
cf62fa4c
PS
123struct dp_packet *dp_packet_clone_data(const void *, size_t);
124struct dp_packet *dp_packet_clone_data_with_headroom(const void *, size_t,
5a07c6e1 125 size_t headroom);
cf62fa4c
PS
126static inline void dp_packet_delete(struct dp_packet *);
127
128static inline void *dp_packet_at(const struct dp_packet *, size_t offset,
5a07c6e1
DDP
129 size_t size);
130static inline void *dp_packet_at_assert(const struct dp_packet *,
131 size_t offset, size_t size);
cf62fa4c
PS
132static inline void *dp_packet_tail(const struct dp_packet *);
133static inline void *dp_packet_end(const struct dp_packet *);
134
135void *dp_packet_put_uninit(struct dp_packet *, size_t);
136void *dp_packet_put_zeros(struct dp_packet *, size_t);
137void *dp_packet_put(struct dp_packet *, const void *, size_t);
138char *dp_packet_put_hex(struct dp_packet *, const char *s, size_t *n);
139void dp_packet_reserve(struct dp_packet *, size_t);
5a07c6e1
DDP
140void dp_packet_reserve_with_tailroom(struct dp_packet *, size_t headroom,
141 size_t tailroom);
142void *dp_packet_push_uninit(struct dp_packet *, size_t);
cf62fa4c 143void *dp_packet_push_zeros(struct dp_packet *, size_t);
5a07c6e1 144void *dp_packet_push(struct dp_packet *, const void *, size_t);
cf62fa4c
PS
145
146static inline size_t dp_packet_headroom(const struct dp_packet *);
147static inline size_t dp_packet_tailroom(const struct dp_packet *);
148void dp_packet_prealloc_headroom(struct dp_packet *, size_t);
149void dp_packet_prealloc_tailroom(struct dp_packet *, size_t);
150void dp_packet_shift(struct dp_packet *, int);
91088554 151
cf62fa4c
PS
152static inline void dp_packet_clear(struct dp_packet *);
153static inline void *dp_packet_pull(struct dp_packet *, size_t);
154static inline void *dp_packet_try_pull(struct dp_packet *, size_t);
91088554 155
cf62fa4c 156void *dp_packet_steal_data(struct dp_packet *);
91088554 157
5a07c6e1
DDP
158static inline bool dp_packet_equal(const struct dp_packet *,
159 const struct dp_packet *);
cf62fa4c
PS
160
161\f
cf62fa4c 162/* Frees memory that 'b' points to, as well as 'b' itself. */
5a07c6e1
DDP
163static inline void
164dp_packet_delete(struct dp_packet *b)
cf62fa4c
PS
165{
166 if (b) {
167 if (b->source == DPBUF_DPDK) {
168 /* If this dp_packet was allocated by DPDK it must have been
169 * created as a dp_packet */
170 free_dpdk_buf((struct dp_packet*) b);
171 return;
172 }
173
174 dp_packet_uninit(b);
175 free(b);
176 }
177}
178
179/* If 'b' contains at least 'offset + size' bytes of data, returns a pointer to
180 * byte 'offset'. Otherwise, returns a null pointer. */
5a07c6e1
DDP
181static inline void *
182dp_packet_at(const struct dp_packet *b, size_t offset, size_t size)
91088554 183{
5a07c6e1
DDP
184 return offset + size <= dp_packet_size(b)
185 ? (char *) dp_packet_data(b) + offset
186 : NULL;
cf62fa4c 187}
91088554 188
cf62fa4c
PS
189/* Returns a pointer to byte 'offset' in 'b', which must contain at least
190 * 'offset + size' bytes of data. */
5a07c6e1
DDP
191static inline void *
192dp_packet_at_assert(const struct dp_packet *b, size_t offset, size_t size)
cf62fa4c
PS
193{
194 ovs_assert(offset + size <= dp_packet_size(b));
195 return ((char *) dp_packet_data(b)) + offset;
196}
197
198/* Returns a pointer to byte following the last byte of data in use in 'b'. */
5a07c6e1
DDP
199static inline void *
200dp_packet_tail(const struct dp_packet *b)
cf62fa4c
PS
201{
202 return (char *) dp_packet_data(b) + dp_packet_size(b);
203}
204
205/* Returns a pointer to byte following the last byte allocated for use (but
206 * not necessarily in use) in 'b'. */
5a07c6e1
DDP
207static inline void *
208dp_packet_end(const struct dp_packet *b)
cf62fa4c 209{
11a6fbd5 210 return (char *) dp_packet_base(b) + dp_packet_get_allocated(b);
cf62fa4c
PS
211}
212
213/* Returns the number of bytes of headroom in 'b', that is, the number of bytes
214 * of unused space in dp_packet 'b' before the data that is in use. (Most
5a07c6e1
DDP
215 * commonly, the data in a dp_packet is at its beginning, and thus the
216 * dp_packet's headroom is 0.) */
217static inline size_t
218dp_packet_headroom(const struct dp_packet *b)
cf62fa4c 219{
5a07c6e1 220 return (char *) dp_packet_data(b) - (char *) dp_packet_base(b);
cf62fa4c
PS
221}
222
5a07c6e1
DDP
223/* Returns the number of bytes that may be appended to the tail end of
224 * dp_packet 'b' before the dp_packet must be reallocated. */
225static inline size_t
226dp_packet_tailroom(const struct dp_packet *b)
cf62fa4c 227{
5a07c6e1 228 return (char *) dp_packet_end(b) - (char *) dp_packet_tail(b);
cf62fa4c
PS
229}
230
231/* Clears any data from 'b'. */
5a07c6e1
DDP
232static inline void
233dp_packet_clear(struct dp_packet *b)
cf62fa4c
PS
234{
235 dp_packet_set_data(b, dp_packet_base(b));
236 dp_packet_set_size(b, 0);
237}
238
239/* Removes 'size' bytes from the head end of 'b', which must contain at least
240 * 'size' bytes of data. Returns the first byte of data removed. */
5a07c6e1
DDP
241static inline void *
242dp_packet_pull(struct dp_packet *b, size_t size)
cf62fa4c
PS
243{
244 void *data = dp_packet_data(b);
245 ovs_assert(dp_packet_size(b) - dp_packet_l2_pad_size(b) >= size);
5a07c6e1 246 dp_packet_set_data(b, (char *) dp_packet_data(b) + size);
cf62fa4c
PS
247 dp_packet_set_size(b, dp_packet_size(b) - size);
248 return data;
249}
250
251/* If 'b' has at least 'size' bytes of data, removes that many bytes from the
252 * head end of 'b' and returns the first byte removed. Otherwise, returns a
253 * null pointer without modifying 'b'. */
5a07c6e1
DDP
254static inline void *
255dp_packet_try_pull(struct dp_packet *b, size_t size)
cf62fa4c
PS
256{
257 return dp_packet_size(b) - dp_packet_l2_pad_size(b) >= size
258 ? dp_packet_pull(b, size) : NULL;
259}
260
5a07c6e1
DDP
261static inline bool
262dp_packet_equal(const struct dp_packet *a, const struct dp_packet *b)
cf62fa4c
PS
263{
264 return dp_packet_size(a) == dp_packet_size(b) &&
5a07c6e1 265 !memcmp(dp_packet_data(a), dp_packet_data(b), dp_packet_size(a));
cf62fa4c
PS
266}
267
2482b0b0
JS
268static inline bool
269dp_packet_is_eth(const struct dp_packet *b)
270{
271 return b->packet_type == htonl(PT_ETH);
272}
273
274/* Get the start of the Ethernet frame. 'l3_ofs' marks the end of the l2
cf62fa4c 275 * headers, so return NULL if it is not set. */
5a07c6e1 276static inline void *
2482b0b0 277dp_packet_eth(const struct dp_packet *b)
cf62fa4c 278{
2482b0b0
JS
279 return (dp_packet_is_eth(b) && b->l3_ofs != UINT16_MAX)
280 ? dp_packet_data(b) : NULL;
cf62fa4c
PS
281}
282
82eb5b0a
DDP
283/* Resets all layer offsets. 'l3' offset must be set before 'l2' can be
284 * retrieved. */
5a07c6e1
DDP
285static inline void
286dp_packet_reset_offsets(struct dp_packet *b)
cf62fa4c 287{
cf62fa4c
PS
288 b->l2_pad_size = 0;
289 b->l2_5_ofs = UINT16_MAX;
290 b->l3_ofs = UINT16_MAX;
291 b->l4_ofs = UINT16_MAX;
292}
293
5a07c6e1
DDP
294static inline uint8_t
295dp_packet_l2_pad_size(const struct dp_packet *b)
cf62fa4c
PS
296{
297 return b->l2_pad_size;
298}
299
5a07c6e1
DDP
300static inline void
301dp_packet_set_l2_pad_size(struct dp_packet *b, uint8_t pad_size)
cf62fa4c
PS
302{
303 ovs_assert(pad_size <= dp_packet_size(b));
304 b->l2_pad_size = pad_size;
305}
306
5a07c6e1
DDP
307static inline void *
308dp_packet_l2_5(const struct dp_packet *b)
cf62fa4c 309{
82eb5b0a
DDP
310 return b->l2_5_ofs != UINT16_MAX
311 ? (char *) dp_packet_data(b) + b->l2_5_ofs
312 : NULL;
cf62fa4c
PS
313}
314
5a07c6e1
DDP
315static inline void
316dp_packet_set_l2_5(struct dp_packet *b, void *l2_5)
cf62fa4c 317{
82eb5b0a
DDP
318 b->l2_5_ofs = l2_5
319 ? (char *) l2_5 - (char *) dp_packet_data(b)
320 : UINT16_MAX;
cf62fa4c
PS
321}
322
5a07c6e1
DDP
323static inline void *
324dp_packet_l3(const struct dp_packet *b)
cf62fa4c 325{
82eb5b0a
DDP
326 return b->l3_ofs != UINT16_MAX
327 ? (char *) dp_packet_data(b) + b->l3_ofs
328 : NULL;
cf62fa4c
PS
329}
330
5a07c6e1
DDP
331static inline void
332dp_packet_set_l3(struct dp_packet *b, void *l3)
cf62fa4c 333{
82eb5b0a 334 b->l3_ofs = l3 ? (char *) l3 - (char *) dp_packet_data(b) : UINT16_MAX;
cf62fa4c
PS
335}
336
5a07c6e1
DDP
337static inline void *
338dp_packet_l4(const struct dp_packet *b)
cf62fa4c 339{
82eb5b0a
DDP
340 return b->l4_ofs != UINT16_MAX
341 ? (char *) dp_packet_data(b) + b->l4_ofs
342 : NULL;
cf62fa4c
PS
343}
344
5a07c6e1
DDP
345static inline void
346dp_packet_set_l4(struct dp_packet *b, void *l4)
cf62fa4c 347{
82eb5b0a 348 b->l4_ofs = l4 ? (char *) l4 - (char *) dp_packet_data(b) : UINT16_MAX;
cf62fa4c
PS
349}
350
5a07c6e1
DDP
351static inline size_t
352dp_packet_l4_size(const struct dp_packet *b)
cf62fa4c
PS
353{
354 return b->l4_ofs != UINT16_MAX
355 ? (const char *)dp_packet_tail(b) - (const char *)dp_packet_l4(b)
356 - dp_packet_l2_pad_size(b)
357 : 0;
358}
359
5a07c6e1
DDP
360static inline const void *
361dp_packet_get_tcp_payload(const struct dp_packet *b)
cf62fa4c
PS
362{
363 size_t l4_size = dp_packet_l4_size(b);
364
365 if (OVS_LIKELY(l4_size >= TCP_HEADER_LEN)) {
366 struct tcp_header *tcp = dp_packet_l4(b);
367 int tcp_len = TCP_OFFSET(tcp->tcp_ctl) * 4;
368
369 if (OVS_LIKELY(tcp_len >= TCP_HEADER_LEN && tcp_len <= l4_size)) {
370 return (const char *)tcp + tcp_len;
371 }
372 }
373 return NULL;
374}
375
5a07c6e1
DDP
376static inline const void *
377dp_packet_get_udp_payload(const struct dp_packet *b)
cf62fa4c
PS
378{
379 return OVS_LIKELY(dp_packet_l4_size(b) >= UDP_HEADER_LEN)
380 ? (const char *)dp_packet_l4(b) + UDP_HEADER_LEN : NULL;
381}
382
5a07c6e1
DDP
383static inline const void *
384dp_packet_get_sctp_payload(const struct dp_packet *b)
cf62fa4c
PS
385{
386 return OVS_LIKELY(dp_packet_l4_size(b) >= SCTP_HEADER_LEN)
387 ? (const char *)dp_packet_l4(b) + SCTP_HEADER_LEN : NULL;
388}
389
5a07c6e1
DDP
390static inline const void *
391dp_packet_get_icmp_payload(const struct dp_packet *b)
cf62fa4c
PS
392{
393 return OVS_LIKELY(dp_packet_l4_size(b) >= ICMP_HEADER_LEN)
394 ? (const char *)dp_packet_l4(b) + ICMP_HEADER_LEN : NULL;
395}
396
5a07c6e1
DDP
397static inline const void *
398dp_packet_get_nd_payload(const struct dp_packet *b)
cf62fa4c
PS
399{
400 return OVS_LIKELY(dp_packet_l4_size(b) >= ND_MSG_LEN)
401 ? (const char *)dp_packet_l4(b) + ND_MSG_LEN : NULL;
402}
403
404#ifdef DPDK_NETDEV
405BUILD_ASSERT_DECL(offsetof(struct dp_packet, mbuf) == 0);
406
5a07c6e1
DDP
407static inline void *
408dp_packet_base(const struct dp_packet *b)
cf62fa4c
PS
409{
410 return b->mbuf.buf_addr;
411}
412
5a07c6e1
DDP
413static inline void
414dp_packet_set_base(struct dp_packet *b, void *d)
cf62fa4c
PS
415{
416 b->mbuf.buf_addr = d;
417}
418
5a07c6e1
DDP
419static inline uint32_t
420dp_packet_size(const struct dp_packet *b)
cf62fa4c 421{
b8e57534 422 return b->mbuf.pkt_len;
cf62fa4c
PS
423}
424
5a07c6e1
DDP
425static inline void
426dp_packet_set_size(struct dp_packet *b, uint32_t v)
cf62fa4c 427{
b8e57534
MK
428 /* netdev-dpdk does not currently support segmentation; consequently, for
429 * all intents and purposes, 'data_len' (16 bit) and 'pkt_len' (32 bit) may
430 * be used interchangably.
431 *
432 * On the datapath, it is expected that the size of packets
433 * (and thus 'v') will always be <= UINT16_MAX; this means that there is no
434 * loss of accuracy in assigning 'v' to 'data_len'.
b8e57534
MK
435 */
436 b->mbuf.data_len = (uint16_t)v; /* Current seg length. */
437 b->mbuf.pkt_len = v; /* Total length of all segments linked to
438 * this segment. */
cf62fa4c
PS
439}
440
5a07c6e1
DDP
441static inline uint16_t
442__packet_data(const struct dp_packet *b)
cf62fa4c 443{
b8e57534 444 return b->mbuf.data_off;
cf62fa4c
PS
445}
446
5a07c6e1
DDP
447static inline void
448__packet_set_data(struct dp_packet *b, uint16_t v)
cf62fa4c 449{
b8e57534 450 b->mbuf.data_off = v;
cf62fa4c
PS
451}
452
5a07c6e1
DDP
453static inline uint16_t
454dp_packet_get_allocated(const struct dp_packet *b)
11a6fbd5
DDP
455{
456 return b->mbuf.buf_len;
457}
458
5a07c6e1
DDP
459static inline void
460dp_packet_set_allocated(struct dp_packet *b, uint16_t s)
11a6fbd5
DDP
461{
462 b->mbuf.buf_len = s;
463}
b8e57534 464#else
5a07c6e1
DDP
465static inline void *
466dp_packet_base(const struct dp_packet *b)
cf62fa4c
PS
467{
468 return b->base_;
469}
470
5a07c6e1
DDP
471static inline void
472dp_packet_set_base(struct dp_packet *b, void *d)
cf62fa4c
PS
473{
474 b->base_ = d;
475}
476
5a07c6e1
DDP
477static inline uint32_t
478dp_packet_size(const struct dp_packet *b)
cf62fa4c
PS
479{
480 return b->size_;
481}
482
5a07c6e1
DDP
483static inline void
484dp_packet_set_size(struct dp_packet *b, uint32_t v)
cf62fa4c
PS
485{
486 b->size_ = v;
487}
b8e57534 488
5a07c6e1
DDP
489static inline uint16_t
490__packet_data(const struct dp_packet *b)
b8e57534
MK
491{
492 return b->data_ofs;
493}
494
5a07c6e1
DDP
495static inline void
496__packet_set_data(struct dp_packet *b, uint16_t v)
b8e57534
MK
497{
498 b->data_ofs = v;
499}
500
5a07c6e1
DDP
501static inline uint16_t
502dp_packet_get_allocated(const struct dp_packet *b)
11a6fbd5
DDP
503{
504 return b->allocated_;
505}
506
5a07c6e1
DDP
507static inline void
508dp_packet_set_allocated(struct dp_packet *b, uint16_t s)
11a6fbd5
DDP
509{
510 b->allocated_ = s;
511}
cf62fa4c
PS
512#endif
513
aaca4fe0
WT
514static inline void
515dp_packet_reset_cutlen(struct dp_packet *b)
516{
517 b->cutlen = 0;
518}
519
520static inline uint32_t
521dp_packet_set_cutlen(struct dp_packet *b, uint32_t max_len)
522{
523 if (max_len < ETH_HEADER_LEN) {
524 max_len = ETH_HEADER_LEN;
525 }
526
527 if (max_len >= dp_packet_size(b)) {
528 b->cutlen = 0;
529 } else {
530 b->cutlen = dp_packet_size(b) - max_len;
531 }
532 return b->cutlen;
533}
534
535static inline uint32_t
a61a2891 536dp_packet_get_cutlen(const struct dp_packet *b)
aaca4fe0
WT
537{
538 /* Always in valid range if user uses dp_packet_set_cutlen. */
539 return b->cutlen;
540}
541
a61a2891
BP
542static inline uint32_t
543dp_packet_get_send_len(const struct dp_packet *b)
544{
545 return dp_packet_size(b) - dp_packet_get_cutlen(b);
546}
547
5a07c6e1
DDP
548static inline void *
549dp_packet_data(const struct dp_packet *b)
b8e57534 550{
5a07c6e1
DDP
551 return __packet_data(b) != UINT16_MAX
552 ? (char *) dp_packet_base(b) + __packet_data(b) : NULL;
b8e57534
MK
553}
554
5a07c6e1
DDP
555static inline void
556dp_packet_set_data(struct dp_packet *b, void *data)
b8e57534
MK
557{
558 if (data) {
5a07c6e1 559 __packet_set_data(b, (char *) data - (char *) dp_packet_base(b));
b8e57534
MK
560 } else {
561 __packet_set_data(b, UINT16_MAX);
562 }
563}
564
5a07c6e1
DDP
565static inline void
566dp_packet_reset_packet(struct dp_packet *b, int off)
cf62fa4c
PS
567{
568 dp_packet_set_size(b, dp_packet_size(b) - off);
82eb5b0a 569 dp_packet_set_data(b, ((unsigned char *) dp_packet_data(b) + off));
b513ea34 570 dp_packet_reset_offsets(b);
91088554
DDP
571}
572
f2f44f5d
DDP
573/* Returns the RSS hash of the packet 'p'. Note that the returned value is
574 * correct only if 'dp_packet_rss_valid(p)' returns true */
5a07c6e1
DDP
575static inline uint32_t
576dp_packet_get_rss_hash(struct dp_packet *p)
61a2647e
DDP
577{
578#ifdef DPDK_NETDEV
b8e57534 579 return p->mbuf.hash.rss;
61a2647e 580#else
2bc1bbd2 581 return p->rss_hash;
61a2647e
DDP
582#endif
583}
584
5a07c6e1
DDP
585static inline void
586dp_packet_set_rss_hash(struct dp_packet *p, uint32_t hash)
61a2647e
DDP
587{
588#ifdef DPDK_NETDEV
b8e57534 589 p->mbuf.hash.rss = hash;
f2f44f5d 590 p->mbuf.ol_flags |= PKT_RX_RSS_HASH;
61a2647e 591#else
2bc1bbd2 592 p->rss_hash = hash;
f2f44f5d
DDP
593 p->rss_hash_valid = true;
594#endif
595}
596
597static inline bool
598dp_packet_rss_valid(struct dp_packet *p)
599{
600#ifdef DPDK_NETDEV
601 return p->mbuf.ol_flags & PKT_RX_RSS_HASH;
602#else
603 return p->rss_hash_valid;
604#endif
605}
606
607static inline void
f8121b39
DB
608dp_packet_rss_invalidate(struct dp_packet *p OVS_UNUSED)
609{
610#ifndef DPDK_NETDEV
611 p->rss_hash_valid = false;
612#endif
613}
614
615static inline void
616dp_packet_mbuf_rss_flag_reset(struct dp_packet *p OVS_UNUSED)
f2f44f5d
DDP
617{
618#ifdef DPDK_NETDEV
619 p->mbuf.ol_flags &= ~PKT_RX_RSS_HASH;
f8121b39
DB
620#endif
621}
622
623/* This initialization is needed for packets that do not come
624 * from DPDK interfaces, when vswitchd is built with --with-dpdk.
625 * The DPDK rte library will still otherwise manage the mbuf.
626 * We only need to initialize the mbuf ol_flags. */
627static inline void
628dp_packet_mbuf_init(struct dp_packet *p OVS_UNUSED)
629{
630#ifdef DPDK_NETDEV
631 p->mbuf.ol_flags = 0;
61a2647e
DDP
632#endif
633}
634
1a2bb118 635static inline bool
cfc19646 636dp_packet_ip_checksum_valid(struct dp_packet *p OVS_UNUSED)
1a2bb118
SC
637{
638#ifdef DPDK_NETDEV
7451af61
SC
639 return (p->mbuf.ol_flags & PKT_RX_IP_CKSUM_MASK) ==
640 PKT_RX_IP_CKSUM_GOOD;
641#else
cfc19646 642 return false;
7451af61
SC
643#endif
644}
645
646static inline bool
cfc19646 647dp_packet_ip_checksum_bad(struct dp_packet *p OVS_UNUSED)
7451af61
SC
648{
649#ifdef DPDK_NETDEV
650 return (p->mbuf.ol_flags & PKT_RX_IP_CKSUM_MASK) ==
651 PKT_RX_IP_CKSUM_BAD;
1a2bb118 652#else
cfc19646 653 return false;
1a2bb118
SC
654#endif
655}
656
657static inline bool
cfc19646 658dp_packet_l4_checksum_valid(struct dp_packet *p OVS_UNUSED)
1a2bb118
SC
659{
660#ifdef DPDK_NETDEV
7451af61
SC
661 return (p->mbuf.ol_flags & PKT_RX_L4_CKSUM_MASK) ==
662 PKT_RX_L4_CKSUM_GOOD;
663#else
cfc19646 664 return false;
7451af61
SC
665#endif
666}
667
668static inline bool
cfc19646 669dp_packet_l4_checksum_bad(struct dp_packet *p OVS_UNUSED)
7451af61
SC
670{
671#ifdef DPDK_NETDEV
672 return (p->mbuf.ol_flags & PKT_RX_L4_CKSUM_MASK) ==
673 PKT_RX_L4_CKSUM_BAD;
1a2bb118 674#else
cfc19646 675 return false;
1a2bb118
SC
676#endif
677}
678
679#ifdef DPDK_NETDEV
680static inline void
681reset_dp_packet_checksum_ol_flags(struct dp_packet *p)
682{
683 p->mbuf.ol_flags &= ~(PKT_RX_L4_CKSUM_GOOD | PKT_RX_L4_CKSUM_BAD |
684 PKT_RX_IP_CKSUM_GOOD | PKT_RX_IP_CKSUM_BAD);
685}
686#else
687#define reset_dp_packet_checksum_ol_flags(arg)
688#endif
689
1895cc8d
PS
690enum { NETDEV_MAX_BURST = 32 }; /* Maximum number packets in a batch. */
691
692struct dp_packet_batch {
72c84bc2 693 size_t count;
aaca4fe0 694 bool trunc; /* true if the batch needs truncate. */
1895cc8d
PS
695 struct dp_packet *packets[NETDEV_MAX_BURST];
696};
697
72c84bc2
AZ
698static inline void
699dp_packet_batch_init(struct dp_packet_batch *batch)
1895cc8d 700{
72c84bc2
AZ
701 batch->count = 0;
702 batch->trunc = false;
1895cc8d
PS
703}
704
705static inline void
72c84bc2
AZ
706dp_packet_batch_add__(struct dp_packet_batch *batch,
707 struct dp_packet *packet, size_t limit)
1895cc8d 708{
72c84bc2
AZ
709 if (batch->count < limit) {
710 batch->packets[batch->count++] = packet;
711 } else {
712 dp_packet_delete(packet);
1895cc8d 713 }
1895cc8d
PS
714}
715
72c84bc2
AZ
716/* When the batch is full, 'packet' will be dropped and freed. */
717static inline void
718dp_packet_batch_add(struct dp_packet_batch *batch, struct dp_packet *packet)
719{
720 dp_packet_batch_add__(batch, packet, NETDEV_MAX_BURST);
721}
722
723static inline size_t
724dp_packet_batch_size(const struct dp_packet_batch *batch)
725{
726 return batch->count;
727}
728
feb86b41
IM
729/* Clear 'batch' for refill. Use dp_packet_batch_refill() to add
730 * packets back into the 'batch'. */
1895cc8d 731static inline void
72c84bc2 732dp_packet_batch_refill_init(struct dp_packet_batch *batch)
1895cc8d 733{
72c84bc2
AZ
734 batch->count = 0;
735};
736
737static inline void
738dp_packet_batch_refill(struct dp_packet_batch *batch,
739 struct dp_packet *packet, size_t idx)
740{
741 dp_packet_batch_add__(batch, packet, MIN(NETDEV_MAX_BURST, idx + 1));
742}
743
744static inline void
745dp_packet_batch_init_packet(struct dp_packet_batch *batch, struct dp_packet *p)
746{
747 dp_packet_batch_init(batch);
748 batch->count = 1;
749 batch->packets[0] = p;
750}
751
752static inline bool
753dp_packet_batch_is_empty(const struct dp_packet_batch *batch)
754{
755 return !dp_packet_batch_size(batch);
756}
757
758#define DP_PACKET_BATCH_FOR_EACH(PACKET, BATCH) \
759 for (size_t i = 0; i < dp_packet_batch_size(BATCH); i++) \
760 if (PACKET = BATCH->packets[i], true)
761
762/* Use this macro for cases where some packets in the 'BATCH' may be
763 * dropped after going through each packet in the 'BATCH'.
764 *
765 * For packets to stay in the 'BATCH', they need to be refilled back
766 * into the 'BATCH' by calling dp_packet_batch_refill(). Caller owns
767 * the packets that are not refilled.
768 *
769 * Caller needs to supply 'SIZE', that stores the current number of
770 * packets in 'BATCH'. It is best to declare this variable with
771 * the 'const' modifier since it should not be modified by
772 * the iterator. */
773#define DP_PACKET_BATCH_REFILL_FOR_EACH(IDX, SIZE, PACKET, BATCH) \
774 for (dp_packet_batch_refill_init(BATCH), IDX=0; IDX < SIZE; IDX++) \
775 if (PACKET = BATCH->packets[IDX], true)
776
777static inline void
778dp_packet_batch_clone(struct dp_packet_batch *dst,
779 struct dp_packet_batch *src)
780{
781 struct dp_packet *packet;
782
783 dp_packet_batch_init(dst);
784 DP_PACKET_BATCH_FOR_EACH (packet, src) {
785 dp_packet_batch_add(dst, dp_packet_clone(packet));
786 }
cf4cf916 787 dst->trunc = src->trunc;
1895cc8d
PS
788}
789
790static inline void
791dp_packet_delete_batch(struct dp_packet_batch *batch, bool may_steal)
792{
793 if (may_steal) {
72c84bc2 794 struct dp_packet *packet;
1895cc8d 795
72c84bc2
AZ
796 DP_PACKET_BATCH_FOR_EACH (packet, batch) {
797 dp_packet_delete(packet);
1895cc8d 798 }
72c84bc2 799 dp_packet_batch_init(batch);
1895cc8d
PS
800 }
801}
802
aaca4fe0 803static inline void
72c84bc2 804dp_packet_batch_apply_cutlen(struct dp_packet_batch *batch)
aaca4fe0 805{
72c84bc2
AZ
806 if (batch->trunc) {
807 struct dp_packet *packet;
aaca4fe0 808
72c84bc2 809 DP_PACKET_BATCH_FOR_EACH (packet, batch) {
a61a2891 810 dp_packet_set_size(packet, dp_packet_get_send_len(packet));
72c84bc2
AZ
811 dp_packet_reset_cutlen(packet);
812 }
813 batch->trunc = false;
aaca4fe0 814 }
aaca4fe0
WT
815}
816
817static inline void
72c84bc2 818dp_packet_batch_reset_cutlen(struct dp_packet_batch *batch)
aaca4fe0 819{
72c84bc2
AZ
820 if (batch->trunc) {
821 struct dp_packet *packet;
aaca4fe0 822
72c84bc2
AZ
823 DP_PACKET_BATCH_FOR_EACH (packet, batch) {
824 dp_packet_reset_cutlen(packet);
825 }
826 batch->trunc = false;
aaca4fe0
WT
827 }
828}
829
91088554
DDP
830#ifdef __cplusplus
831}
832#endif
833
aaaac302 834#endif /* dp-packet.h */