+/* Returns a pointer to byte 'offset' in 'b', which must contain at least
+ * 'offset + size' bytes of data. */
+static inline void *dp_packet_at_assert(const struct dp_packet *b, size_t offset,
+ size_t size)
+{
+ ovs_assert(offset + size <= dp_packet_size(b));
+ return ((char *) dp_packet_data(b)) + offset;
+}
+
+/* Returns a pointer to byte following the last byte of data in use in 'b'. */
+static inline void *dp_packet_tail(const struct dp_packet *b)
+{
+ return (char *) dp_packet_data(b) + dp_packet_size(b);
+}
+
+/* Returns a pointer to byte following the last byte allocated for use (but
+ * not necessarily in use) in 'b'. */
+static inline void *dp_packet_end(const struct dp_packet *b)
+{
+ return (char *) dp_packet_base(b) + b->allocated;
+}
+
+/* Returns the number of bytes of headroom in 'b', that is, the number of bytes
+ * of unused space in dp_packet 'b' before the data that is in use. (Most
+ * commonly, the data in a dp_packet is at its beginning, and thus the dp_packet's
+ * headroom is 0.) */
+static inline size_t dp_packet_headroom(const struct dp_packet *b)
+{
+ return (char*)dp_packet_data(b) - (char*)dp_packet_base(b);
+}
+
+/* Returns the number of bytes that may be appended to the tail end of dp_packet
+ * 'b' before the dp_packet must be reallocated. */
+static inline size_t dp_packet_tailroom(const struct dp_packet *b)
+{
+ return (char*)dp_packet_end(b) - (char*)dp_packet_tail(b);
+}
+
+/* Clears any data from 'b'. */
+static inline void dp_packet_clear(struct dp_packet *b)
+{
+ dp_packet_set_data(b, dp_packet_base(b));
+ dp_packet_set_size(b, 0);
+}
+
+/* Removes 'size' bytes from the head end of 'b', which must contain at least
+ * 'size' bytes of data. Returns the first byte of data removed. */
+static inline void *dp_packet_pull(struct dp_packet *b, size_t size)
+{
+ void *data = dp_packet_data(b);
+ ovs_assert(dp_packet_size(b) - dp_packet_l2_pad_size(b) >= size);
+ dp_packet_set_data(b, (char*)dp_packet_data(b) + size);
+ dp_packet_set_size(b, dp_packet_size(b) - size);
+ return data;
+}
+
+/* If 'b' has at least 'size' bytes of data, removes that many bytes from the
+ * head end of 'b' and returns the first byte removed. Otherwise, returns a
+ * null pointer without modifying 'b'. */
+static inline void *dp_packet_try_pull(struct dp_packet *b, size_t size)
+{
+ return dp_packet_size(b) - dp_packet_l2_pad_size(b) >= size
+ ? dp_packet_pull(b, size) : NULL;
+}
+
+static inline struct dp_packet *dp_packet_from_list(const struct ovs_list *list)
+{
+ return CONTAINER_OF(list, struct dp_packet, list_node);
+}
+
+static inline bool dp_packet_equal(const struct dp_packet *a, const struct dp_packet *b)
+{
+ return dp_packet_size(a) == dp_packet_size(b) &&
+ memcmp(dp_packet_data(a), dp_packet_data(b), dp_packet_size(a)) == 0;
+}
+
+/* Get the start if the Ethernet frame. 'l3_ofs' marks the end of the l2
+ * headers, so return NULL if it is not set. */
+static inline void * dp_packet_l2(const struct dp_packet *b)
+{
+ return (b->l3_ofs != UINT16_MAX) ? b->frame : NULL;
+}
+
+/* Sets the packet frame start pointer and resets all layer offsets.
+ * l3 offset must be set before 'l2' can be retrieved. */
+static inline void dp_packet_set_frame(struct dp_packet *b, void *packet)
+{
+ b->frame = packet;
+ b->l2_pad_size = 0;
+ b->l2_5_ofs = UINT16_MAX;
+ b->l3_ofs = UINT16_MAX;
+ b->l4_ofs = UINT16_MAX;
+}
+
+static inline uint8_t dp_packet_l2_pad_size(const struct dp_packet *b)
+{
+ return b->l2_pad_size;
+}
+
+static inline void dp_packet_set_l2_pad_size(struct dp_packet *b, uint8_t pad_size)
+{
+ ovs_assert(pad_size <= dp_packet_size(b));
+ b->l2_pad_size = pad_size;
+}
+
+static inline void * dp_packet_l2_5(const struct dp_packet *b)
+{
+ return b->l2_5_ofs != UINT16_MAX ? (char *)b->frame + b->l2_5_ofs : NULL;
+}
+
+static inline void dp_packet_set_l2_5(struct dp_packet *b, void *l2_5)
+{
+ b->l2_5_ofs = l2_5 ? (char *)l2_5 - (char *)b->frame : UINT16_MAX;
+}
+
+static inline void * dp_packet_l3(const struct dp_packet *b)
+{
+ return b->l3_ofs != UINT16_MAX ? (char *)b->frame + b->l3_ofs : NULL;
+}
+
+static inline void dp_packet_set_l3(struct dp_packet *b, void *l3)
+{
+ b->l3_ofs = l3 ? (char *)l3 - (char *)b->frame : UINT16_MAX;
+}
+
+static inline void * dp_packet_l4(const struct dp_packet *b)
+{
+ return b->l4_ofs != UINT16_MAX ? (char *)b->frame + b->l4_ofs : NULL;
+}
+
+static inline void dp_packet_set_l4(struct dp_packet *b, void *l4)
+{
+ b->l4_ofs = l4 ? (char *)l4 - (char *)b->frame : UINT16_MAX;
+}
+
+static inline size_t dp_packet_l4_size(const struct dp_packet *b)
+{
+ return b->l4_ofs != UINT16_MAX
+ ? (const char *)dp_packet_tail(b) - (const char *)dp_packet_l4(b)
+ - dp_packet_l2_pad_size(b)
+ : 0;
+}
+
+static inline const void *dp_packet_get_tcp_payload(const struct dp_packet *b)
+{
+ size_t l4_size = dp_packet_l4_size(b);
+
+ if (OVS_LIKELY(l4_size >= TCP_HEADER_LEN)) {
+ struct tcp_header *tcp = dp_packet_l4(b);
+ int tcp_len = TCP_OFFSET(tcp->tcp_ctl) * 4;
+
+ if (OVS_LIKELY(tcp_len >= TCP_HEADER_LEN && tcp_len <= l4_size)) {
+ return (const char *)tcp + tcp_len;
+ }
+ }
+ return NULL;
+}
+
+static inline const void *dp_packet_get_udp_payload(const struct dp_packet *b)
+{
+ return OVS_LIKELY(dp_packet_l4_size(b) >= UDP_HEADER_LEN)
+ ? (const char *)dp_packet_l4(b) + UDP_HEADER_LEN : NULL;
+}
+
+static inline const void *dp_packet_get_sctp_payload(const struct dp_packet *b)
+{
+ return OVS_LIKELY(dp_packet_l4_size(b) >= SCTP_HEADER_LEN)
+ ? (const char *)dp_packet_l4(b) + SCTP_HEADER_LEN : NULL;
+}
+
+static inline const void *dp_packet_get_icmp_payload(const struct dp_packet *b)
+{
+ return OVS_LIKELY(dp_packet_l4_size(b) >= ICMP_HEADER_LEN)
+ ? (const char *)dp_packet_l4(b) + ICMP_HEADER_LEN : NULL;
+}
+
+static inline const void *dp_packet_get_nd_payload(const struct dp_packet *b)
+{
+ return OVS_LIKELY(dp_packet_l4_size(b) >= ND_MSG_LEN)
+ ? (const char *)dp_packet_l4(b) + ND_MSG_LEN : NULL;
+}
+
+#ifdef DPDK_NETDEV
+BUILD_ASSERT_DECL(offsetof(struct dp_packet, mbuf) == 0);
+
+static inline void * dp_packet_data(const struct dp_packet *b)
+{
+ return b->mbuf.pkt.data;
+}
+
+static inline void dp_packet_set_data(struct dp_packet *b, void *d)
+{
+ b->mbuf.pkt.data = d;
+}
+
+static inline void * dp_packet_base(const struct dp_packet *b)
+{
+ return b->mbuf.buf_addr;
+}
+
+static inline void dp_packet_set_base(struct dp_packet *b, void *d)
+{
+ b->mbuf.buf_addr = d;
+}
+
+static inline uint32_t dp_packet_size(const struct dp_packet *b)
+{
+ return b->mbuf.pkt.pkt_len;
+}
+
+static inline void dp_packet_set_size(struct dp_packet *b, uint32_t v)
+{
+ b->mbuf.pkt.data_len = v; /* Current seg length. */
+ b->mbuf.pkt.pkt_len = v; /* Total length of all segments linked to
+ * this segment. */
+}
+
+#else
+static inline void * dp_packet_data(const struct dp_packet *b)
+{
+ return b->data_;
+}
+
+static inline void dp_packet_set_data(struct dp_packet *b, void *d)
+{
+ b->data_ = d;
+}
+
+static inline void * dp_packet_base(const struct dp_packet *b)
+{
+ return b->base_;
+}
+
+static inline void dp_packet_set_base(struct dp_packet *b, void *d)
+{
+ b->base_ = d;
+}
+
+static inline uint32_t dp_packet_size(const struct dp_packet *b)
+{
+ return b->size_;
+}
+
+static inline void dp_packet_set_size(struct dp_packet *b, uint32_t v)
+{
+ b->size_ = v;
+}
+#endif
+
+static inline void dp_packet_reset_packet(struct dp_packet *b, int off)
+{
+ dp_packet_set_size(b, dp_packet_size(b) - off);
+ dp_packet_set_data(b, (void *) ((unsigned char *) b->frame + off));
+ b->frame = NULL;
+ b->l2_5_ofs = b->l3_ofs = b->l4_ofs = UINT16_MAX;