1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Intel Corporation
3 */
4
5 #ifndef _GSO_COMMON_H_
6 #define _GSO_COMMON_H_
7
8 #include <stdint.h>
9
10 #include <rte_ip.h>
11 #include <rte_tcp.h>
12 #include <rte_udp.h>
13
14 #define IS_FRAGMENTED(frag_off) (((frag_off) & RTE_IPV4_HDR_OFFSET_MASK) != 0 \
15 || ((frag_off) & RTE_IPV4_HDR_MF_FLAG) == RTE_IPV4_HDR_MF_FLAG)
16
17 #define TCP_HDR_PSH_MASK ((uint8_t)0x08)
18 #define TCP_HDR_FIN_MASK ((uint8_t)0x01)
19
20 #define IS_IPV4_TCP(flag) (((flag) & (RTE_MBUF_F_TX_TCP_SEG | RTE_MBUF_F_TX_IPV4)) == \
21 (RTE_MBUF_F_TX_TCP_SEG | RTE_MBUF_F_TX_IPV4))
22
23 #define IS_IPV4_VXLAN_TCP4(flag) (((flag) & (RTE_MBUF_F_TX_TCP_SEG | RTE_MBUF_F_TX_IPV4 | \
24 RTE_MBUF_F_TX_OUTER_IPV4 | RTE_MBUF_F_TX_TUNNEL_MASK)) == \
25 (RTE_MBUF_F_TX_TCP_SEG | RTE_MBUF_F_TX_IPV4 | RTE_MBUF_F_TX_OUTER_IPV4 | \
26 RTE_MBUF_F_TX_TUNNEL_VXLAN))
27
28 #define IS_IPV4_VXLAN_UDP4(flag) (((flag) & (RTE_MBUF_F_TX_UDP_SEG | RTE_MBUF_F_TX_IPV4 | \
29 RTE_MBUF_F_TX_OUTER_IPV4 | RTE_MBUF_F_TX_TUNNEL_MASK)) == \
30 (RTE_MBUF_F_TX_UDP_SEG | RTE_MBUF_F_TX_IPV4 | RTE_MBUF_F_TX_OUTER_IPV4 | \
31 RTE_MBUF_F_TX_TUNNEL_VXLAN))
32
33 #define IS_IPV4_GRE_TCP4(flag) (((flag) & (RTE_MBUF_F_TX_TCP_SEG | RTE_MBUF_F_TX_IPV4 | \
34 RTE_MBUF_F_TX_OUTER_IPV4 | RTE_MBUF_F_TX_TUNNEL_MASK)) == \
35 (RTE_MBUF_F_TX_TCP_SEG | RTE_MBUF_F_TX_IPV4 | RTE_MBUF_F_TX_OUTER_IPV4 | \
36 RTE_MBUF_F_TX_TUNNEL_GRE))
37
38 #define IS_IPV4_UDP(flag) (((flag) & (RTE_MBUF_F_TX_UDP_SEG | RTE_MBUF_F_TX_IPV4)) == \
39 (RTE_MBUF_F_TX_UDP_SEG | RTE_MBUF_F_TX_IPV4))
40
41 /**
42 * Internal function which updates the UDP header of a packet, following
43 * segmentation. This is required to update the header's datagram length field.
44 *
45 * @param pkt
46 * The packet containing the UDP header.
47 * @param udp_offset
48 * The offset of the UDP header from the start of the packet.
49 */
50 static inline void
update_udp_header(struct rte_mbuf * pkt,uint16_t udp_offset)51 update_udp_header(struct rte_mbuf *pkt, uint16_t udp_offset)
52 {
53 struct rte_udp_hdr *udp_hdr;
54
55 udp_hdr = rte_pktmbuf_mtod_offset(pkt, struct rte_udp_hdr *,
56 udp_offset);
57 udp_hdr->dgram_len = rte_cpu_to_be_16(pkt->pkt_len - udp_offset);
58 }
59
60 /**
61 * Internal function which updates the TCP header of a packet, following
62 * segmentation. This is required to update the header's 'sent' sequence
63 * number, and also to clear 'PSH' and 'FIN' flags for non-tail segments.
64 *
65 * @param pkt
66 * The packet containing the TCP header.
67 * @param l4_offset
68 * The offset of the TCP header from the start of the packet.
69 * @param sent_seq
70 * The sent sequence number.
71 * @param non-tail
72 * Indicates whether or not this is a tail segment.
73 */
74 static inline void
update_tcp_header(struct rte_mbuf * pkt,uint16_t l4_offset,uint32_t sent_seq,uint8_t non_tail)75 update_tcp_header(struct rte_mbuf *pkt, uint16_t l4_offset, uint32_t sent_seq,
76 uint8_t non_tail)
77 {
78 struct rte_tcp_hdr *tcp_hdr;
79
80 tcp_hdr = rte_pktmbuf_mtod_offset(pkt, struct rte_tcp_hdr *,
81 l4_offset);
82 tcp_hdr->sent_seq = rte_cpu_to_be_32(sent_seq);
83 if (likely(non_tail))
84 tcp_hdr->tcp_flags &= (~(TCP_HDR_PSH_MASK |
85 TCP_HDR_FIN_MASK));
86 }
87
88 /**
89 * Internal function which updates the IPv4 header of a packet, following
90 * segmentation. This is required to update the header's 'total_length' field,
91 * to reflect the reduced length of the now-segmented packet. Furthermore, the
92 * header's 'packet_id' field must be updated to reflect the new ID of the
93 * now-segmented packet.
94 *
95 * @param pkt
96 * The packet containing the IPv4 header.
97 * @param l3_offset
98 * The offset of the IPv4 header from the start of the packet.
99 * @param id
100 * The new ID of the packet.
101 */
102 static inline void
update_ipv4_header(struct rte_mbuf * pkt,uint16_t l3_offset,uint16_t id)103 update_ipv4_header(struct rte_mbuf *pkt, uint16_t l3_offset, uint16_t id)
104 {
105 struct rte_ipv4_hdr *ipv4_hdr;
106
107 ipv4_hdr = rte_pktmbuf_mtod_offset(pkt, struct rte_ipv4_hdr *,
108 l3_offset);
109 ipv4_hdr->total_length = rte_cpu_to_be_16(pkt->pkt_len - l3_offset);
110 ipv4_hdr->packet_id = rte_cpu_to_be_16(id);
111 }
112
113 /**
114 * Internal function which divides the input packet into small segments.
115 * Each of the newly-created segments is organized as a two-segment MBUF,
116 * where the first segment is a standard mbuf, which stores a copy of
117 * packet header, and the second is an indirect mbuf which points to a
118 * section of data in the input packet.
119 *
120 * @param pkt
121 * Packet to segment.
122 * @param pkt_hdr_offset
123 * Packet header offset, measured in bytes.
124 * @param pyld_unit_size
125 * The max payload length of a GSO segment.
126 * @param direct_pool
127 * MBUF pool used for allocating direct buffers for output segments.
128 * @param indirect_pool
129 * MBUF pool used for allocating indirect buffers for output segments.
130 * @param pkts_out
131 * Pointer array used to keep the mbuf addresses of output segments. If
132 * the memory space in pkts_out is insufficient, gso_do_segment() fails
133 * and returns -EINVAL.
134 * @param nb_pkts_out
135 * The max number of items that pkts_out can keep.
136 *
137 * @return
138 * - The number of segments created in the event of success.
139 * - Return -ENOMEM if run out of memory in MBUF pools.
140 * - Return -EINVAL for invalid parameters.
141 */
142 int gso_do_segment(struct rte_mbuf *pkt,
143 uint16_t pkt_hdr_offset,
144 uint16_t pyld_unit_size,
145 struct rte_mempool *direct_pool,
146 struct rte_mempool *indirect_pool,
147 struct rte_mbuf **pkts_out,
148 uint16_t nb_pkts_out);
149 #endif
150