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