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