2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include "openvswitch/list.h"
25 #include "netdev-dpdk.h"
31 enum OVS_PACKED_ENUM dp_packet_source {
32 DPBUF_MALLOC, /* Obtained via malloc(). */
33 DPBUF_STACK, /* Un-movable stack space or static buffer. */
34 DPBUF_STUB, /* Starts on stack, may expand into heap. */
35 DPBUF_DPDK, /* buffer data is from DPDK allocated memory.
36 * ref to build_dp_packet() in netdev-dpdk. */
39 #define DP_PACKET_CONTEXT_SIZE 64
41 /* Buffer for holding packet data. A dp_packet is automatically reallocated
42 * as necessary if it grows too large for the available memory.
46 struct rte_mbuf mbuf; /* DPDK mbuf */
48 void *base_; /* First byte of allocated space. */
49 uint16_t allocated_; /* Number of bytes allocated. */
50 uint16_t data_ofs; /* First byte actually in use. */
51 uint32_t size_; /* Number of bytes in use. */
52 uint32_t rss_hash; /* Packet hash. */
53 bool rss_hash_valid; /* Is the 'rss_hash' valid? */
55 enum dp_packet_source source; /* Source of memory allocated as 'base'. */
56 uint8_t l2_pad_size; /* Detected l2 padding size.
57 * Padding is non-pullable. */
58 uint16_t l2_5_ofs; /* MPLS label stack offset, or UINT16_MAX */
59 uint16_t l3_ofs; /* Network-level header offset,
61 uint16_t l4_ofs; /* Transport-level header offset,
64 struct pkt_metadata md;
65 uint64_t data[DP_PACKET_CONTEXT_SIZE / 8];
69 static inline void *dp_packet_data(const struct dp_packet *);
70 static inline void dp_packet_set_data(struct dp_packet *, void *);
71 static inline void *dp_packet_base(const struct dp_packet *);
72 static inline void dp_packet_set_base(struct dp_packet *, void *);
74 static inline uint32_t dp_packet_size(const struct dp_packet *);
75 static inline void dp_packet_set_size(struct dp_packet *, uint32_t);
77 static inline uint16_t dp_packet_get_allocated(const struct dp_packet *);
78 static inline void dp_packet_set_allocated(struct dp_packet *, uint16_t);
80 void *dp_packet_resize_l2(struct dp_packet *, int increment);
81 void *dp_packet_resize_l2_5(struct dp_packet *, int increment);
82 static inline void *dp_packet_l2(const struct dp_packet *);
83 static inline void dp_packet_reset_offsets(struct dp_packet *);
84 static inline uint8_t dp_packet_l2_pad_size(const struct dp_packet *);
85 static inline void dp_packet_set_l2_pad_size(struct dp_packet *, uint8_t);
86 static inline void *dp_packet_l2_5(const struct dp_packet *);
87 static inline void dp_packet_set_l2_5(struct dp_packet *, void *);
88 static inline void *dp_packet_l3(const struct dp_packet *);
89 static inline void dp_packet_set_l3(struct dp_packet *, void *);
90 static inline void *dp_packet_l4(const struct dp_packet *);
91 static inline void dp_packet_set_l4(struct dp_packet *, void *);
92 static inline size_t dp_packet_l4_size(const struct dp_packet *);
93 static inline const void *dp_packet_get_tcp_payload(const struct dp_packet *);
94 static inline const void *dp_packet_get_udp_payload(const struct dp_packet *);
95 static inline const void *dp_packet_get_sctp_payload(const struct dp_packet *);
96 static inline const void *dp_packet_get_icmp_payload(const struct dp_packet *);
97 static inline const void *dp_packet_get_nd_payload(const struct dp_packet *);
99 void dp_packet_use(struct dp_packet *, void *, size_t);
100 void dp_packet_use_stub(struct dp_packet *, void *, size_t);
101 void dp_packet_use_const(struct dp_packet *, const void *, size_t);
103 void dp_packet_init_dpdk(struct dp_packet *, size_t allocated);
105 void dp_packet_init(struct dp_packet *, size_t);
106 void dp_packet_uninit(struct dp_packet *);
108 struct dp_packet *dp_packet_new(size_t);
109 struct dp_packet *dp_packet_new_with_headroom(size_t, size_t headroom);
110 struct dp_packet *dp_packet_clone(const struct dp_packet *);
111 struct dp_packet *dp_packet_clone_with_headroom(const struct dp_packet *,
113 struct dp_packet *dp_packet_clone_data(const void *, size_t);
114 struct dp_packet *dp_packet_clone_data_with_headroom(const void *, size_t,
116 static inline void dp_packet_delete(struct dp_packet *);
118 static inline void *dp_packet_at(const struct dp_packet *, size_t offset,
120 static inline void *dp_packet_at_assert(const struct dp_packet *,
121 size_t offset, size_t size);
122 static inline void *dp_packet_tail(const struct dp_packet *);
123 static inline void *dp_packet_end(const struct dp_packet *);
125 void *dp_packet_put_uninit(struct dp_packet *, size_t);
126 void *dp_packet_put_zeros(struct dp_packet *, size_t);
127 void *dp_packet_put(struct dp_packet *, const void *, size_t);
128 char *dp_packet_put_hex(struct dp_packet *, const char *s, size_t *n);
129 void dp_packet_reserve(struct dp_packet *, size_t);
130 void dp_packet_reserve_with_tailroom(struct dp_packet *, size_t headroom,
132 void *dp_packet_push_uninit(struct dp_packet *, size_t);
133 void *dp_packet_push_zeros(struct dp_packet *, size_t);
134 void *dp_packet_push(struct dp_packet *, const void *, size_t);
136 static inline size_t dp_packet_headroom(const struct dp_packet *);
137 static inline size_t dp_packet_tailroom(const struct dp_packet *);
138 void dp_packet_prealloc_headroom(struct dp_packet *, size_t);
139 void dp_packet_prealloc_tailroom(struct dp_packet *, size_t);
140 void dp_packet_shift(struct dp_packet *, int);
142 static inline void dp_packet_clear(struct dp_packet *);
143 static inline void *dp_packet_pull(struct dp_packet *, size_t);
144 static inline void *dp_packet_try_pull(struct dp_packet *, size_t);
146 void *dp_packet_steal_data(struct dp_packet *);
148 static inline bool dp_packet_equal(const struct dp_packet *,
149 const struct dp_packet *);
152 /* Frees memory that 'b' points to, as well as 'b' itself. */
154 dp_packet_delete(struct dp_packet *b)
157 if (b->source == DPBUF_DPDK) {
158 /* If this dp_packet was allocated by DPDK it must have been
159 * created as a dp_packet */
160 free_dpdk_buf((struct dp_packet*) b);
169 /* If 'b' contains at least 'offset + size' bytes of data, returns a pointer to
170 * byte 'offset'. Otherwise, returns a null pointer. */
172 dp_packet_at(const struct dp_packet *b, size_t offset, size_t size)
174 return offset + size <= dp_packet_size(b)
175 ? (char *) dp_packet_data(b) + offset
179 /* Returns a pointer to byte 'offset' in 'b', which must contain at least
180 * 'offset + size' bytes of data. */
182 dp_packet_at_assert(const struct dp_packet *b, size_t offset, size_t size)
184 ovs_assert(offset + size <= dp_packet_size(b));
185 return ((char *) dp_packet_data(b)) + offset;
188 /* Returns a pointer to byte following the last byte of data in use in 'b'. */
190 dp_packet_tail(const struct dp_packet *b)
192 return (char *) dp_packet_data(b) + dp_packet_size(b);
195 /* Returns a pointer to byte following the last byte allocated for use (but
196 * not necessarily in use) in 'b'. */
198 dp_packet_end(const struct dp_packet *b)
200 return (char *) dp_packet_base(b) + dp_packet_get_allocated(b);
203 /* Returns the number of bytes of headroom in 'b', that is, the number of bytes
204 * of unused space in dp_packet 'b' before the data that is in use. (Most
205 * commonly, the data in a dp_packet is at its beginning, and thus the
206 * dp_packet's headroom is 0.) */
208 dp_packet_headroom(const struct dp_packet *b)
210 return (char *) dp_packet_data(b) - (char *) dp_packet_base(b);
213 /* Returns the number of bytes that may be appended to the tail end of
214 * dp_packet 'b' before the dp_packet must be reallocated. */
216 dp_packet_tailroom(const struct dp_packet *b)
218 return (char *) dp_packet_end(b) - (char *) dp_packet_tail(b);
221 /* Clears any data from 'b'. */
223 dp_packet_clear(struct dp_packet *b)
225 dp_packet_set_data(b, dp_packet_base(b));
226 dp_packet_set_size(b, 0);
229 /* Removes 'size' bytes from the head end of 'b', which must contain at least
230 * 'size' bytes of data. Returns the first byte of data removed. */
232 dp_packet_pull(struct dp_packet *b, size_t size)
234 void *data = dp_packet_data(b);
235 ovs_assert(dp_packet_size(b) - dp_packet_l2_pad_size(b) >= size);
236 dp_packet_set_data(b, (char *) dp_packet_data(b) + size);
237 dp_packet_set_size(b, dp_packet_size(b) - size);
241 /* If 'b' has at least 'size' bytes of data, removes that many bytes from the
242 * head end of 'b' and returns the first byte removed. Otherwise, returns a
243 * null pointer without modifying 'b'. */
245 dp_packet_try_pull(struct dp_packet *b, size_t size)
247 return dp_packet_size(b) - dp_packet_l2_pad_size(b) >= size
248 ? dp_packet_pull(b, size) : NULL;
252 dp_packet_equal(const struct dp_packet *a, const struct dp_packet *b)
254 return dp_packet_size(a) == dp_packet_size(b) &&
255 !memcmp(dp_packet_data(a), dp_packet_data(b), dp_packet_size(a));
258 /* Get the start of the Ethernet frame. 'l3_ofs' marks the end of the l2
259 * headers, so return NULL if it is not set. */
261 dp_packet_l2(const struct dp_packet *b)
263 return (b->l3_ofs != UINT16_MAX) ? dp_packet_data(b) : NULL;
266 /* Resets all layer offsets. 'l3' offset must be set before 'l2' can be
269 dp_packet_reset_offsets(struct dp_packet *b)
272 b->l2_5_ofs = UINT16_MAX;
273 b->l3_ofs = UINT16_MAX;
274 b->l4_ofs = UINT16_MAX;
277 static inline uint8_t
278 dp_packet_l2_pad_size(const struct dp_packet *b)
280 return b->l2_pad_size;
284 dp_packet_set_l2_pad_size(struct dp_packet *b, uint8_t pad_size)
286 ovs_assert(pad_size <= dp_packet_size(b));
287 b->l2_pad_size = pad_size;
291 dp_packet_l2_5(const struct dp_packet *b)
293 return b->l2_5_ofs != UINT16_MAX
294 ? (char *) dp_packet_data(b) + b->l2_5_ofs
299 dp_packet_set_l2_5(struct dp_packet *b, void *l2_5)
302 ? (char *) l2_5 - (char *) dp_packet_data(b)
307 dp_packet_l3(const struct dp_packet *b)
309 return b->l3_ofs != UINT16_MAX
310 ? (char *) dp_packet_data(b) + b->l3_ofs
315 dp_packet_set_l3(struct dp_packet *b, void *l3)
317 b->l3_ofs = l3 ? (char *) l3 - (char *) dp_packet_data(b) : UINT16_MAX;
321 dp_packet_l4(const struct dp_packet *b)
323 return b->l4_ofs != UINT16_MAX
324 ? (char *) dp_packet_data(b) + b->l4_ofs
329 dp_packet_set_l4(struct dp_packet *b, void *l4)
331 b->l4_ofs = l4 ? (char *) l4 - (char *) dp_packet_data(b) : UINT16_MAX;
335 dp_packet_l4_size(const struct dp_packet *b)
337 return b->l4_ofs != UINT16_MAX
338 ? (const char *)dp_packet_tail(b) - (const char *)dp_packet_l4(b)
339 - dp_packet_l2_pad_size(b)
343 static inline const void *
344 dp_packet_get_tcp_payload(const struct dp_packet *b)
346 size_t l4_size = dp_packet_l4_size(b);
348 if (OVS_LIKELY(l4_size >= TCP_HEADER_LEN)) {
349 struct tcp_header *tcp = dp_packet_l4(b);
350 int tcp_len = TCP_OFFSET(tcp->tcp_ctl) * 4;
352 if (OVS_LIKELY(tcp_len >= TCP_HEADER_LEN && tcp_len <= l4_size)) {
353 return (const char *)tcp + tcp_len;
359 static inline const void *
360 dp_packet_get_udp_payload(const struct dp_packet *b)
362 return OVS_LIKELY(dp_packet_l4_size(b) >= UDP_HEADER_LEN)
363 ? (const char *)dp_packet_l4(b) + UDP_HEADER_LEN : NULL;
366 static inline const void *
367 dp_packet_get_sctp_payload(const struct dp_packet *b)
369 return OVS_LIKELY(dp_packet_l4_size(b) >= SCTP_HEADER_LEN)
370 ? (const char *)dp_packet_l4(b) + SCTP_HEADER_LEN : NULL;
373 static inline const void *
374 dp_packet_get_icmp_payload(const struct dp_packet *b)
376 return OVS_LIKELY(dp_packet_l4_size(b) >= ICMP_HEADER_LEN)
377 ? (const char *)dp_packet_l4(b) + ICMP_HEADER_LEN : NULL;
380 static inline const void *
381 dp_packet_get_nd_payload(const struct dp_packet *b)
383 return OVS_LIKELY(dp_packet_l4_size(b) >= ND_MSG_LEN)
384 ? (const char *)dp_packet_l4(b) + ND_MSG_LEN : NULL;
388 BUILD_ASSERT_DECL(offsetof(struct dp_packet, mbuf) == 0);
391 dp_packet_base(const struct dp_packet *b)
393 return b->mbuf.buf_addr;
397 dp_packet_set_base(struct dp_packet *b, void *d)
399 b->mbuf.buf_addr = d;
402 static inline uint32_t
403 dp_packet_size(const struct dp_packet *b)
405 return b->mbuf.pkt_len;
409 dp_packet_set_size(struct dp_packet *b, uint32_t v)
411 /* netdev-dpdk does not currently support segmentation; consequently, for
412 * all intents and purposes, 'data_len' (16 bit) and 'pkt_len' (32 bit) may
413 * be used interchangably.
415 * On the datapath, it is expected that the size of packets
416 * (and thus 'v') will always be <= UINT16_MAX; this means that there is no
417 * loss of accuracy in assigning 'v' to 'data_len'.
419 b->mbuf.data_len = (uint16_t)v; /* Current seg length. */
420 b->mbuf.pkt_len = v; /* Total length of all segments linked to
424 static inline uint16_t
425 __packet_data(const struct dp_packet *b)
427 return b->mbuf.data_off;
431 __packet_set_data(struct dp_packet *b, uint16_t v)
433 b->mbuf.data_off = v;
436 static inline uint16_t
437 dp_packet_get_allocated(const struct dp_packet *b)
439 return b->mbuf.buf_len;
443 dp_packet_set_allocated(struct dp_packet *b, uint16_t s)
449 dp_packet_base(const struct dp_packet *b)
455 dp_packet_set_base(struct dp_packet *b, void *d)
460 static inline uint32_t
461 dp_packet_size(const struct dp_packet *b)
467 dp_packet_set_size(struct dp_packet *b, uint32_t v)
472 static inline uint16_t
473 __packet_data(const struct dp_packet *b)
479 __packet_set_data(struct dp_packet *b, uint16_t v)
484 static inline uint16_t
485 dp_packet_get_allocated(const struct dp_packet *b)
487 return b->allocated_;
491 dp_packet_set_allocated(struct dp_packet *b, uint16_t s)
498 dp_packet_data(const struct dp_packet *b)
500 return __packet_data(b) != UINT16_MAX
501 ? (char *) dp_packet_base(b) + __packet_data(b) : NULL;
505 dp_packet_set_data(struct dp_packet *b, void *data)
508 __packet_set_data(b, (char *) data - (char *) dp_packet_base(b));
510 __packet_set_data(b, UINT16_MAX);
515 dp_packet_reset_packet(struct dp_packet *b, int off)
517 dp_packet_set_size(b, dp_packet_size(b) - off);
518 dp_packet_set_data(b, ((unsigned char *) dp_packet_data(b) + off));
519 dp_packet_reset_offsets(b);
522 /* Returns the RSS hash of the packet 'p'. Note that the returned value is
523 * correct only if 'dp_packet_rss_valid(p)' returns true */
524 static inline uint32_t
525 dp_packet_get_rss_hash(struct dp_packet *p)
528 return p->mbuf.hash.rss;
535 dp_packet_set_rss_hash(struct dp_packet *p, uint32_t hash)
538 p->mbuf.hash.rss = hash;
539 p->mbuf.ol_flags |= PKT_RX_RSS_HASH;
542 p->rss_hash_valid = true;
547 dp_packet_rss_valid(struct dp_packet *p)
550 return p->mbuf.ol_flags & PKT_RX_RSS_HASH;
552 return p->rss_hash_valid;
557 dp_packet_rss_invalidate(struct dp_packet *p)
560 p->mbuf.ol_flags &= ~PKT_RX_RSS_HASH;
562 p->rss_hash_valid = false;
566 enum { NETDEV_MAX_BURST = 32 }; /* Maximum number packets in a batch. */
568 struct dp_packet_batch {
570 struct dp_packet *packets[NETDEV_MAX_BURST];
573 static inline void dp_packet_batch_init(struct dp_packet_batch *b)
579 dp_packet_batch_clone(struct dp_packet_batch *dst,
580 struct dp_packet_batch *src)
584 for (i = 0; i < src->count; i++) {
585 dst->packets[i] = dp_packet_clone(src->packets[i]);
587 dst->count = src->count;
591 packet_batch_init_packet(struct dp_packet_batch *b, struct dp_packet *p)
598 dp_packet_delete_batch(struct dp_packet_batch *batch, bool may_steal)
603 for (i = 0; i < batch->count; i++) {
604 dp_packet_delete(batch->packets[i]);
613 #endif /* dp-packet.h */