1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2020 Inspur Corporation
10 #include <rte_vxlan.h>
12 #define INVALID_ARRAY_INDEX 0xffffffffUL
13 #define GRO_UDP4_TBL_MAX_ITEM_NUM (1024UL * 1024UL)
16 * The max length of a IPv4 packet, which includes the length of the L3
17 * header, the L4 header and the data payload.
19 #define MAX_IPV4_PKT_LENGTH UINT16_MAX
21 /* Header fields representing a UDP/IPv4 flow */
22 struct udp4_flow_key {
23 struct rte_ether_addr eth_saddr;
24 struct rte_ether_addr eth_daddr;
28 /* IP fragment for UDP does not contain UDP header
29 * except the first one. But IP ID must be same.
34 struct gro_udp4_flow {
35 struct udp4_flow_key key;
37 * The index of the first packet in the flow.
38 * INVALID_ARRAY_INDEX indicates an empty flow.
43 struct gro_udp4_item {
45 * The first MBUF segment of the packet. If the value
46 * is NULL, it means the item is empty.
48 struct rte_mbuf *firstseg;
49 /* The last MBUF segment of the packet */
50 struct rte_mbuf *lastseg;
52 * The time when the first packet is inserted into the table.
53 * This value won't be updated, even if the packet is merged
58 * next_pkt_idx is used to chain the packets that
59 * are in the same flow but can't be merged together
60 * (e.g. caused by packet reordering).
62 uint32_t next_pkt_idx;
63 /* offset of IP fragment packet */
65 /* is last IP fragment? */
67 /* the number of merged packets */
72 * UDP/IPv4 reassembly table structure.
76 struct gro_udp4_item *items;
78 struct gro_udp4_flow *flows;
79 /* current item number */
81 /* current flow num */
84 uint32_t max_item_num;
86 uint32_t max_flow_num;
90 * This function creates a UDP/IPv4 reassembly table.
93 * Socket index for allocating the UDP/IPv4 reassemble table
95 * The maximum number of flows in the UDP/IPv4 GRO table
96 * @param max_item_per_flow
97 * The maximum number of packets per flow
100 * - Return the table pointer on success.
101 * - Return NULL on failure.
103 void *gro_udp4_tbl_create(uint16_t socket_id,
104 uint16_t max_flow_num,
105 uint16_t max_item_per_flow);
108 * This function destroys a UDP/IPv4 reassembly table.
111 * Pointer pointing to the UDP/IPv4 reassembly table.
113 void gro_udp4_tbl_destroy(void *tbl);
116 * This function merges a UDP/IPv4 packet.
118 * This function does not check if the packet has correct checksums and
119 * does not re-calculate checksums for the merged packet. It returns the
120 * packet if it isn't UDP fragment or there is no available space in
124 * Packet to reassemble
126 * Pointer pointing to the UDP/IPv4 reassembly table
128 * The time when the packet is inserted into the table
131 * - Return a positive value if the packet is merged.
132 * - Return zero if the packet isn't merged but stored in the table.
133 * - Return a negative value for invalid parameters or no available
134 * space in the table.
136 int32_t gro_udp4_reassemble(struct rte_mbuf *pkt,
137 struct gro_udp4_tbl *tbl,
138 uint64_t start_time);
141 * This function flushes timeout packets in a UDP/IPv4 reassembly table,
142 * and without updating checksums.
145 * UDP/IPv4 reassembly table pointer
146 * @param flush_timestamp
147 * Flush packets which are inserted into the table before or at the
150 * Pointer array used to keep flushed packets
152 * The element number in 'out'. It also determines the maximum number of
153 * packets that can be flushed finally.
156 * The number of flushed packets
158 uint16_t gro_udp4_tbl_timeout_flush(struct gro_udp4_tbl *tbl,
159 uint64_t flush_timestamp,
160 struct rte_mbuf **out,
164 * This function returns the number of the packets in a UDP/IPv4
168 * UDP/IPv4 reassembly table pointer
171 * The number of packets in the table
173 uint32_t gro_udp4_tbl_pkt_count(void *tbl);
176 * Check if two UDP/IPv4 packets belong to the same flow.
179 is_same_udp4_flow(struct udp4_flow_key k1, struct udp4_flow_key k2)
181 return (rte_is_same_ether_addr(&k1.eth_saddr, &k2.eth_saddr) &&
182 rte_is_same_ether_addr(&k1.eth_daddr, &k2.eth_daddr) &&
183 (k1.ip_src_addr == k2.ip_src_addr) &&
184 (k1.ip_dst_addr == k2.ip_dst_addr) &&
185 (k1.ip_id == k2.ip_id));
189 * Merge two UDP/IPv4 packets without updating checksums.
190 * If cmp is larger than 0, append the new packet to the
191 * original packet. Otherwise, pre-pend the new packet to
192 * the original packet.
195 merge_two_udp4_packets(struct gro_udp4_item *item,
196 struct rte_mbuf *pkt,
198 uint16_t frag_offset,
199 uint8_t is_last_frag,
202 struct rte_mbuf *pkt_head, *pkt_tail, *lastseg;
203 uint16_t hdr_len, l2_len;
207 pkt_head = item->firstseg;
211 pkt_tail = item->firstseg;
214 /* check if the IPv4 packet length is greater than the max value */
215 hdr_len = l2_offset + pkt_head->l2_len + pkt_head->l3_len;
216 l2_len = l2_offset > 0 ? pkt_head->outer_l2_len : pkt_head->l2_len;
217 ip_len = pkt_head->pkt_len - l2_len
218 + pkt_tail->pkt_len - hdr_len;
219 if (unlikely(ip_len > MAX_IPV4_PKT_LENGTH))
222 /* remove the packet header for the tail packet */
223 rte_pktmbuf_adj(pkt_tail, hdr_len);
225 /* chain two packets together */
227 item->lastseg->next = pkt;
228 item->lastseg = rte_pktmbuf_lastseg(pkt);
230 lastseg = rte_pktmbuf_lastseg(pkt);
231 lastseg->next = item->firstseg;
232 item->firstseg = pkt;
233 item->frag_offset = frag_offset;
237 item->is_last_frag = is_last_frag;
239 /* update MBUF metadata for the merged packet */
240 pkt_head->nb_segs += pkt_tail->nb_segs;
241 pkt_head->pkt_len += pkt_tail->pkt_len;
247 * Check if two UDP/IPv4 packets are neighbors.
250 udp4_check_neighbor(struct gro_udp4_item *item,
251 uint16_t frag_offset,
255 struct rte_mbuf *pkt_orig = item->firstseg;
258 /* check if the two packets are neighbors */
259 len = pkt_orig->pkt_len - l2_offset - pkt_orig->l2_len -
261 if (frag_offset == item->frag_offset + len)
262 /* append the new packet */
264 else if (frag_offset + ip_dl == item->frag_offset)
265 /* pre-pend the new packet */
272 is_ipv4_fragment(const struct rte_ipv4_hdr *hdr)
274 uint16_t flag_offset, ip_flag, ip_ofs;
276 flag_offset = rte_be_to_cpu_16(hdr->fragment_offset);
277 ip_ofs = (uint16_t)(flag_offset & RTE_IPV4_HDR_OFFSET_MASK);
278 ip_flag = (uint16_t)(flag_offset & RTE_IPV4_HDR_MF_FLAG);
280 return ip_flag != 0 || ip_ofs != 0;