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