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