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