]> git.proxmox.com Git - mirror_ovs.git/blame - lib/dp-packet.h
netdev-linux: Remove ingress qdisc before trying to add shared block
[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
3aaa6201 117void dp_packet_init_dpdk(struct dp_packet *);
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
69c45b36
DB
355/* Returns the size of the packet from the beginning of the L3 header to the
356 * end of the L3 payload. Hence L2 padding is not included. */
357static inline size_t
358dp_packet_l3_size(const struct dp_packet *b)
359{
360 return OVS_LIKELY(b->l3_ofs != UINT16_MAX)
361 ? (const char *)dp_packet_tail(b) - (const char *)dp_packet_l3(b)
362 - dp_packet_l2_pad_size(b)
363 : 0;
364}
365
366/* Returns the size of the packet from the beginning of the L4 header to the
367 * end of the L4 payload. Hence L2 padding is not included. */
5a07c6e1
DDP
368static inline size_t
369dp_packet_l4_size(const struct dp_packet *b)
cf62fa4c 370{
69c45b36 371 return OVS_LIKELY(b->l4_ofs != UINT16_MAX)
cf62fa4c
PS
372 ? (const char *)dp_packet_tail(b) - (const char *)dp_packet_l4(b)
373 - dp_packet_l2_pad_size(b)
374 : 0;
375}
376
5a07c6e1
DDP
377static inline const void *
378dp_packet_get_tcp_payload(const struct dp_packet *b)
cf62fa4c
PS
379{
380 size_t l4_size = dp_packet_l4_size(b);
381
382 if (OVS_LIKELY(l4_size >= TCP_HEADER_LEN)) {
383 struct tcp_header *tcp = dp_packet_l4(b);
384 int tcp_len = TCP_OFFSET(tcp->tcp_ctl) * 4;
385
386 if (OVS_LIKELY(tcp_len >= TCP_HEADER_LEN && tcp_len <= l4_size)) {
387 return (const char *)tcp + tcp_len;
388 }
389 }
390 return NULL;
391}
392
5a07c6e1
DDP
393static inline const void *
394dp_packet_get_udp_payload(const struct dp_packet *b)
cf62fa4c
PS
395{
396 return OVS_LIKELY(dp_packet_l4_size(b) >= UDP_HEADER_LEN)
397 ? (const char *)dp_packet_l4(b) + UDP_HEADER_LEN : NULL;
398}
399
5a07c6e1
DDP
400static inline const void *
401dp_packet_get_sctp_payload(const struct dp_packet *b)
cf62fa4c
PS
402{
403 return OVS_LIKELY(dp_packet_l4_size(b) >= SCTP_HEADER_LEN)
404 ? (const char *)dp_packet_l4(b) + SCTP_HEADER_LEN : NULL;
405}
406
5a07c6e1
DDP
407static inline const void *
408dp_packet_get_icmp_payload(const struct dp_packet *b)
cf62fa4c
PS
409{
410 return OVS_LIKELY(dp_packet_l4_size(b) >= ICMP_HEADER_LEN)
411 ? (const char *)dp_packet_l4(b) + ICMP_HEADER_LEN : NULL;
412}
413
5a07c6e1
DDP
414static inline const void *
415dp_packet_get_nd_payload(const struct dp_packet *b)
cf62fa4c
PS
416{
417 return OVS_LIKELY(dp_packet_l4_size(b) >= ND_MSG_LEN)
418 ? (const char *)dp_packet_l4(b) + ND_MSG_LEN : NULL;
419}
420
421#ifdef DPDK_NETDEV
422BUILD_ASSERT_DECL(offsetof(struct dp_packet, mbuf) == 0);
423
5a07c6e1
DDP
424static inline void *
425dp_packet_base(const struct dp_packet *b)
cf62fa4c
PS
426{
427 return b->mbuf.buf_addr;
428}
429
5a07c6e1
DDP
430static inline void
431dp_packet_set_base(struct dp_packet *b, void *d)
cf62fa4c
PS
432{
433 b->mbuf.buf_addr = d;
434}
435
5a07c6e1
DDP
436static inline uint32_t
437dp_packet_size(const struct dp_packet *b)
cf62fa4c 438{
b8e57534 439 return b->mbuf.pkt_len;
cf62fa4c
PS
440}
441
5a07c6e1
DDP
442static inline void
443dp_packet_set_size(struct dp_packet *b, uint32_t v)
cf62fa4c 444{
b8e57534
MK
445 /* netdev-dpdk does not currently support segmentation; consequently, for
446 * all intents and purposes, 'data_len' (16 bit) and 'pkt_len' (32 bit) may
447 * be used interchangably.
448 *
449 * On the datapath, it is expected that the size of packets
450 * (and thus 'v') will always be <= UINT16_MAX; this means that there is no
451 * loss of accuracy in assigning 'v' to 'data_len'.
b8e57534
MK
452 */
453 b->mbuf.data_len = (uint16_t)v; /* Current seg length. */
454 b->mbuf.pkt_len = v; /* Total length of all segments linked to
455 * this segment. */
cf62fa4c
PS
456}
457
5a07c6e1
DDP
458static inline uint16_t
459__packet_data(const struct dp_packet *b)
cf62fa4c 460{
b8e57534 461 return b->mbuf.data_off;
cf62fa4c
PS
462}
463
5a07c6e1
DDP
464static inline void
465__packet_set_data(struct dp_packet *b, uint16_t v)
cf62fa4c 466{
b8e57534 467 b->mbuf.data_off = v;
cf62fa4c
PS
468}
469
5a07c6e1
DDP
470static inline uint16_t
471dp_packet_get_allocated(const struct dp_packet *b)
11a6fbd5
DDP
472{
473 return b->mbuf.buf_len;
474}
475
5a07c6e1
DDP
476static inline void
477dp_packet_set_allocated(struct dp_packet *b, uint16_t s)
11a6fbd5
DDP
478{
479 b->mbuf.buf_len = s;
480}
e52ad721
FL
481
482/* Returns the RSS hash of the packet 'p'. Note that the returned value is
483 * correct only if 'dp_packet_rss_valid(p)' returns true */
484static inline uint32_t
92330af5 485dp_packet_get_rss_hash(const struct dp_packet *p)
cf62fa4c 486{
e52ad721 487 return p->mbuf.hash.rss;
cf62fa4c
PS
488}
489
5a07c6e1 490static inline void
e52ad721 491dp_packet_set_rss_hash(struct dp_packet *p, uint32_t hash)
cf62fa4c 492{
e52ad721
FL
493 p->mbuf.hash.rss = hash;
494 p->mbuf.ol_flags |= PKT_RX_RSS_HASH;
cf62fa4c
PS
495}
496
e52ad721 497static inline bool
92330af5 498dp_packet_rss_valid(const struct dp_packet *p)
cf62fa4c 499{
e52ad721 500 return p->mbuf.ol_flags & PKT_RX_RSS_HASH;
cf62fa4c
PS
501}
502
5a07c6e1 503static inline void
e52ad721 504dp_packet_rss_invalidate(struct dp_packet *p OVS_UNUSED)
cf62fa4c 505{
cf62fa4c 506}
b8e57534 507
e52ad721
FL
508static inline void
509dp_packet_mbuf_rss_flag_reset(struct dp_packet *p)
b8e57534 510{
e52ad721 511 p->mbuf.ol_flags &= ~PKT_RX_RSS_HASH;
b8e57534
MK
512}
513
4148de28
MK
514/* This initialization is needed for packets that do not come from DPDK
515 * interfaces, when vswitchd is built with --with-dpdk. */
5a07c6e1 516static inline void
e52ad721 517dp_packet_mbuf_init(struct dp_packet *p)
b8e57534 518{
4148de28
MK
519 p->mbuf.ol_flags = p->mbuf.tx_offload = p->mbuf.packet_type = 0;
520 p->mbuf.nb_segs = 1;
521 p->mbuf.next = NULL;
b8e57534
MK
522}
523
e52ad721 524static inline bool
e030622a 525dp_packet_ip_checksum_valid(const struct dp_packet *p)
11a6fbd5 526{
e52ad721
FL
527 return (p->mbuf.ol_flags & PKT_RX_IP_CKSUM_MASK) ==
528 PKT_RX_IP_CKSUM_GOOD;
11a6fbd5
DDP
529}
530
e52ad721 531static inline bool
e030622a 532dp_packet_ip_checksum_bad(const struct dp_packet *p)
11a6fbd5 533{
e52ad721
FL
534 return (p->mbuf.ol_flags & PKT_RX_IP_CKSUM_MASK) ==
535 PKT_RX_IP_CKSUM_BAD;
536}
537
538static inline bool
e030622a 539dp_packet_l4_checksum_valid(const struct dp_packet *p)
e52ad721
FL
540{
541 return (p->mbuf.ol_flags & PKT_RX_L4_CKSUM_MASK) ==
542 PKT_RX_L4_CKSUM_GOOD;
543}
544
545static inline bool
e030622a 546dp_packet_l4_checksum_bad(const struct dp_packet *p)
e52ad721
FL
547{
548 return (p->mbuf.ol_flags & PKT_RX_L4_CKSUM_MASK) ==
549 PKT_RX_L4_CKSUM_BAD;
11a6fbd5 550}
cf62fa4c 551
aaca4fe0 552static inline void
e52ad721 553reset_dp_packet_checksum_ol_flags(struct dp_packet *p)
aaca4fe0 554{
e52ad721
FL
555 p->mbuf.ol_flags &= ~(PKT_RX_L4_CKSUM_GOOD | PKT_RX_L4_CKSUM_BAD |
556 PKT_RX_IP_CKSUM_GOOD | PKT_RX_IP_CKSUM_BAD);
aaca4fe0
WT
557}
558
e52ad721 559static inline bool
92330af5 560dp_packet_has_flow_mark(const struct dp_packet *p, uint32_t *mark)
aaca4fe0 561{
e52ad721
FL
562 if (p->mbuf.ol_flags & PKT_RX_FDIR_ID) {
563 *mark = p->mbuf.hash.fdir.hi;
564 return true;
aaca4fe0
WT
565 }
566
e52ad721 567 return false;
aaca4fe0
WT
568}
569
e52ad721
FL
570#else /* DPDK_NETDEV */
571static inline void *
572dp_packet_base(const struct dp_packet *b)
aaca4fe0 573{
e52ad721
FL
574 return b->base_;
575}
576
577static inline void
578dp_packet_set_base(struct dp_packet *b, void *d)
579{
580 b->base_ = d;
aaca4fe0
WT
581}
582
a61a2891 583static inline uint32_t
e52ad721 584dp_packet_size(const struct dp_packet *b)
a61a2891 585{
e52ad721 586 return b->size_;
a61a2891
BP
587}
588
e52ad721
FL
589static inline void
590dp_packet_set_size(struct dp_packet *b, uint32_t v)
b8e57534 591{
e52ad721
FL
592 b->size_ = v;
593}
594
595static inline uint16_t
596__packet_data(const struct dp_packet *b)
597{
598 return b->data_ofs;
b8e57534
MK
599}
600
5a07c6e1 601static inline void
e52ad721 602__packet_set_data(struct dp_packet *b, uint16_t v)
b8e57534 603{
e52ad721
FL
604 b->data_ofs = v;
605}
606
607static inline uint16_t
608dp_packet_get_allocated(const struct dp_packet *b)
609{
610 return b->allocated_;
b8e57534
MK
611}
612
5a07c6e1 613static inline void
e52ad721 614dp_packet_set_allocated(struct dp_packet *b, uint16_t s)
cf62fa4c 615{
e52ad721 616 b->allocated_ = s;
91088554
DDP
617}
618
f2f44f5d
DDP
619/* Returns the RSS hash of the packet 'p'. Note that the returned value is
620 * correct only if 'dp_packet_rss_valid(p)' returns true */
5a07c6e1 621static inline uint32_t
92330af5 622dp_packet_get_rss_hash(const struct dp_packet *p)
61a2647e 623{
2bc1bbd2 624 return p->rss_hash;
61a2647e
DDP
625}
626
5a07c6e1
DDP
627static inline void
628dp_packet_set_rss_hash(struct dp_packet *p, uint32_t hash)
61a2647e 629{
2bc1bbd2 630 p->rss_hash = hash;
f2f44f5d 631 p->rss_hash_valid = true;
f2f44f5d
DDP
632}
633
634static inline bool
92330af5 635dp_packet_rss_valid(const struct dp_packet *p)
f2f44f5d 636{
f2f44f5d 637 return p->rss_hash_valid;
f2f44f5d
DDP
638}
639
640static inline void
e52ad721 641dp_packet_rss_invalidate(struct dp_packet *p)
f8121b39 642{
f8121b39 643 p->rss_hash_valid = false;
f8121b39
DB
644}
645
646static inline void
647dp_packet_mbuf_rss_flag_reset(struct dp_packet *p OVS_UNUSED)
f2f44f5d 648{
f8121b39
DB
649}
650
f8121b39
DB
651static inline void
652dp_packet_mbuf_init(struct dp_packet *p OVS_UNUSED)
653{
61a2647e
DDP
654}
655
1a2bb118 656static inline bool
e030622a 657dp_packet_ip_checksum_valid(const struct dp_packet *p OVS_UNUSED)
1a2bb118 658{
cfc19646 659 return false;
7451af61
SC
660}
661
662static inline bool
e030622a 663dp_packet_ip_checksum_bad(const struct dp_packet *p OVS_UNUSED)
7451af61 664{
cfc19646 665 return false;
1a2bb118
SC
666}
667
668static inline bool
e030622a 669dp_packet_l4_checksum_valid(const struct dp_packet *p OVS_UNUSED)
1a2bb118 670{
cfc19646 671 return false;
7451af61
SC
672}
673
674static inline bool
e030622a 675dp_packet_l4_checksum_bad(const struct dp_packet *p OVS_UNUSED)
7451af61 676{
cfc19646 677 return false;
1a2bb118
SC
678}
679
1a2bb118 680static inline void
e52ad721 681reset_dp_packet_checksum_ol_flags(struct dp_packet *p OVS_UNUSED)
1a2bb118 682{
1a2bb118 683}
1a2bb118 684
aab96ec4 685static inline bool
92330af5 686dp_packet_has_flow_mark(const struct dp_packet *p OVS_UNUSED,
aab96ec4
YL
687 uint32_t *mark OVS_UNUSED)
688{
aab96ec4
YL
689 return false;
690}
e52ad721
FL
691#endif /* DPDK_NETDEV */
692
693static inline void
694dp_packet_reset_cutlen(struct dp_packet *b)
695{
696 b->cutlen = 0;
697}
698
699static inline uint32_t
700dp_packet_set_cutlen(struct dp_packet *b, uint32_t max_len)
701{
702 if (max_len < ETH_HEADER_LEN) {
703 max_len = ETH_HEADER_LEN;
704 }
705
706 if (max_len >= dp_packet_size(b)) {
707 b->cutlen = 0;
708 } else {
709 b->cutlen = dp_packet_size(b) - max_len;
710 }
711 return b->cutlen;
712}
713
714static inline uint32_t
715dp_packet_get_cutlen(const struct dp_packet *b)
716{
717 /* Always in valid range if user uses dp_packet_set_cutlen. */
718 return b->cutlen;
719}
720
721static inline uint32_t
722dp_packet_get_send_len(const struct dp_packet *b)
723{
724 return dp_packet_size(b) - dp_packet_get_cutlen(b);
725}
726
727static inline void *
728dp_packet_data(const struct dp_packet *b)
729{
730 return __packet_data(b) != UINT16_MAX
731 ? (char *) dp_packet_base(b) + __packet_data(b) : NULL;
732}
733
734static inline void
735dp_packet_set_data(struct dp_packet *b, void *data)
736{
737 if (data) {
738 __packet_set_data(b, (char *) data - (char *) dp_packet_base(b));
739 } else {
740 __packet_set_data(b, UINT16_MAX);
741 }
742}
743
744static inline void
745dp_packet_reset_packet(struct dp_packet *b, int off)
746{
747 dp_packet_set_size(b, dp_packet_size(b) - off);
748 dp_packet_set_data(b, ((unsigned char *) dp_packet_data(b) + off));
749 dp_packet_reset_offsets(b);
750}
aab96ec4 751
1895cc8d
PS
752enum { NETDEV_MAX_BURST = 32 }; /* Maximum number packets in a batch. */
753
754struct dp_packet_batch {
72c84bc2 755 size_t count;
aaca4fe0 756 bool trunc; /* true if the batch needs truncate. */
9f17f104 757 bool do_not_steal; /* Indicate that the packets should not be stolen. */
1895cc8d
PS
758 struct dp_packet *packets[NETDEV_MAX_BURST];
759};
760
72c84bc2
AZ
761static inline void
762dp_packet_batch_init(struct dp_packet_batch *batch)
1895cc8d 763{
72c84bc2
AZ
764 batch->count = 0;
765 batch->trunc = false;
9f17f104 766 batch->do_not_steal = false;
1895cc8d
PS
767}
768
769static inline void
72c84bc2
AZ
770dp_packet_batch_add__(struct dp_packet_batch *batch,
771 struct dp_packet *packet, size_t limit)
1895cc8d 772{
72c84bc2
AZ
773 if (batch->count < limit) {
774 batch->packets[batch->count++] = packet;
775 } else {
776 dp_packet_delete(packet);
1895cc8d 777 }
1895cc8d
PS
778}
779
72c84bc2
AZ
780/* When the batch is full, 'packet' will be dropped and freed. */
781static inline void
782dp_packet_batch_add(struct dp_packet_batch *batch, struct dp_packet *packet)
783{
784 dp_packet_batch_add__(batch, packet, NETDEV_MAX_BURST);
785}
786
787static inline size_t
788dp_packet_batch_size(const struct dp_packet_batch *batch)
789{
790 return batch->count;
791}
792
feb86b41
IM
793/* Clear 'batch' for refill. Use dp_packet_batch_refill() to add
794 * packets back into the 'batch'. */
1895cc8d 795static inline void
72c84bc2 796dp_packet_batch_refill_init(struct dp_packet_batch *batch)
1895cc8d 797{
72c84bc2
AZ
798 batch->count = 0;
799};
800
801static inline void
802dp_packet_batch_refill(struct dp_packet_batch *batch,
803 struct dp_packet *packet, size_t idx)
804{
805 dp_packet_batch_add__(batch, packet, MIN(NETDEV_MAX_BURST, idx + 1));
806}
807
808static inline void
809dp_packet_batch_init_packet(struct dp_packet_batch *batch, struct dp_packet *p)
810{
811 dp_packet_batch_init(batch);
812 batch->count = 1;
813 batch->packets[0] = p;
814}
815
816static inline bool
817dp_packet_batch_is_empty(const struct dp_packet_batch *batch)
818{
819 return !dp_packet_batch_size(batch);
820}
821
1aa7bbca
DB
822static inline bool
823dp_packet_batch_is_full(const struct dp_packet_batch *batch)
824{
825 return dp_packet_batch_size(batch) == NETDEV_MAX_BURST;
826}
827
e883448e
JP
828#define DP_PACKET_BATCH_FOR_EACH(IDX, PACKET, BATCH) \
829 for (size_t IDX = 0; IDX < dp_packet_batch_size(BATCH); IDX++) \
830 if (PACKET = BATCH->packets[IDX], true)
72c84bc2
AZ
831
832/* Use this macro for cases where some packets in the 'BATCH' may be
833 * dropped after going through each packet in the 'BATCH'.
834 *
835 * For packets to stay in the 'BATCH', they need to be refilled back
836 * into the 'BATCH' by calling dp_packet_batch_refill(). Caller owns
837 * the packets that are not refilled.
838 *
839 * Caller needs to supply 'SIZE', that stores the current number of
840 * packets in 'BATCH'. It is best to declare this variable with
841 * the 'const' modifier since it should not be modified by
842 * the iterator. */
843#define DP_PACKET_BATCH_REFILL_FOR_EACH(IDX, SIZE, PACKET, BATCH) \
844 for (dp_packet_batch_refill_init(BATCH), IDX=0; IDX < SIZE; IDX++) \
845 if (PACKET = BATCH->packets[IDX], true)
846
847static inline void
848dp_packet_batch_clone(struct dp_packet_batch *dst,
849 struct dp_packet_batch *src)
850{
851 struct dp_packet *packet;
852
853 dp_packet_batch_init(dst);
e883448e 854 DP_PACKET_BATCH_FOR_EACH (i, packet, src) {
72c84bc2
AZ
855 dp_packet_batch_add(dst, dp_packet_clone(packet));
856 }
cf4cf916 857 dst->trunc = src->trunc;
1895cc8d
PS
858}
859
860static inline void
7d7ded7a 861dp_packet_delete_batch(struct dp_packet_batch *batch, bool should_steal)
1895cc8d 862{
7d7ded7a 863 if (should_steal) {
72c84bc2 864 struct dp_packet *packet;
1895cc8d 865
e883448e 866 DP_PACKET_BATCH_FOR_EACH (i, packet, batch) {
72c84bc2 867 dp_packet_delete(packet);
1895cc8d 868 }
72c84bc2 869 dp_packet_batch_init(batch);
1895cc8d
PS
870 }
871}
872
11d4c7a8 873static inline void
75fb9148 874dp_packet_batch_init_packet_fields(struct dp_packet_batch *batch)
11d4c7a8
DB
875{
876 struct dp_packet *packet;
877
e883448e 878 DP_PACKET_BATCH_FOR_EACH (i, packet, batch) {
11d4c7a8 879 dp_packet_reset_cutlen(packet);
75fb9148 880 packet->packet_type = htonl(PT_ETH);
11d4c7a8
DB
881 }
882}
883
aaca4fe0 884static inline void
72c84bc2 885dp_packet_batch_apply_cutlen(struct dp_packet_batch *batch)
aaca4fe0 886{
72c84bc2
AZ
887 if (batch->trunc) {
888 struct dp_packet *packet;
aaca4fe0 889
e883448e 890 DP_PACKET_BATCH_FOR_EACH (i, packet, batch) {
a61a2891 891 dp_packet_set_size(packet, dp_packet_get_send_len(packet));
72c84bc2
AZ
892 dp_packet_reset_cutlen(packet);
893 }
894 batch->trunc = false;
aaca4fe0 895 }
aaca4fe0
WT
896}
897
898static inline void
72c84bc2 899dp_packet_batch_reset_cutlen(struct dp_packet_batch *batch)
aaca4fe0 900{
72c84bc2
AZ
901 if (batch->trunc) {
902 struct dp_packet *packet;
aaca4fe0 903
e883448e 904 DP_PACKET_BATCH_FOR_EACH (i, packet, batch) {
72c84bc2
AZ
905 dp_packet_reset_cutlen(packet);
906 }
907 batch->trunc = false;
aaca4fe0
WT
908 }
909}
910
91088554
DDP
911#ifdef __cplusplus
912}
913#endif
914
aaaac302 915#endif /* dp-packet.h */