1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2020 Inspur Corporation
10 #define INVALID_ARRAY_INDEX 0xffffffffUL
11 #define GRO_UDP4_TBL_MAX_ITEM_NUM (1024UL * 1024UL)
14 * The max length of a IPv4 packet, which includes the length of the L3
15 * header, the L4 header and the data payload.
17 #define MAX_IPV4_PKT_LENGTH UINT16_MAX
19 /* Header fields representing a UDP/IPv4 flow */
20 struct udp4_flow_key {
21 struct rte_ether_addr eth_saddr;
22 struct rte_ether_addr eth_daddr;
26 /* IP fragment for UDP does not contain UDP header
27 * except the first one. But IP ID must be same.
32 struct gro_udp4_flow {
33 struct udp4_flow_key key;
35 * The index of the first packet in the flow.
36 * INVALID_ARRAY_INDEX indicates an empty flow.
41 struct gro_udp4_item {
43 * The first MBUF segment of the packet. If the value
44 * is NULL, it means the item is empty.
46 struct rte_mbuf *firstseg;
47 /* The last MBUF segment of the packet */
48 struct rte_mbuf *lastseg;
50 * The time when the first packet is inserted into the table.
51 * This value won't be updated, even if the packet is merged
56 * next_pkt_idx is used to chain the packets that
57 * are in the same flow but can't be merged together
58 * (e.g. caused by packet reordering).
60 uint32_t next_pkt_idx;
61 /* offset of IP fragment packet */
63 /* is last IP fragment? */
65 /* the number of merged packets */
70 * UDP/IPv4 reassembly table structure.
74 struct gro_udp4_item *items;
76 struct gro_udp4_flow *flows;
77 /* current item number */
79 /* current flow num */
82 uint32_t max_item_num;
84 uint32_t max_flow_num;
88 * This function creates a UDP/IPv4 reassembly table.
91 * Socket index for allocating the UDP/IPv4 reassemble table
93 * The maximum number of flows in the UDP/IPv4 GRO table
94 * @param max_item_per_flow
95 * The maximum number of packets per flow
98 * - Return the table pointer on success.
99 * - Return NULL on failure.
101 void *gro_udp4_tbl_create(uint16_t socket_id,
102 uint16_t max_flow_num,
103 uint16_t max_item_per_flow);
106 * This function destroys a UDP/IPv4 reassembly table.
109 * Pointer pointing to the UDP/IPv4 reassembly table.
111 void gro_udp4_tbl_destroy(void *tbl);
114 * This function merges a UDP/IPv4 packet.
116 * This function does not check if the packet has correct checksums and
117 * does not re-calculate checksums for the merged packet. It returns the
118 * packet if it isn't UDP fragment or there is no available space in
122 * Packet to reassemble
124 * Pointer pointing to the UDP/IPv4 reassembly table
126 * The time when the packet is inserted into the table
129 * - Return a positive value if the packet is merged.
130 * - Return zero if the packet isn't merged but stored in the table.
131 * - Return a negative value for invalid parameters or no available
132 * space in the table.
134 int32_t gro_udp4_reassemble(struct rte_mbuf *pkt,
135 struct gro_udp4_tbl *tbl,
136 uint64_t start_time);
139 * This function flushes timeout packets in a UDP/IPv4 reassembly table,
140 * and without updating checksums.
143 * UDP/IPv4 reassembly table pointer
144 * @param flush_timestamp
145 * Flush packets which are inserted into the table before or at the
148 * Pointer array used to keep flushed packets
150 * The element number in 'out'. It also determines the maximum number of
151 * packets that can be flushed finally.
154 * The number of flushed packets
156 uint16_t gro_udp4_tbl_timeout_flush(struct gro_udp4_tbl *tbl,
157 uint64_t flush_timestamp,
158 struct rte_mbuf **out,
162 * This function returns the number of the packets in a UDP/IPv4
166 * UDP/IPv4 reassembly table pointer
169 * The number of packets in the table
171 uint32_t gro_udp4_tbl_pkt_count(void *tbl);
174 * Check if two UDP/IPv4 packets belong to the same flow.
177 is_same_udp4_flow(struct udp4_flow_key k1, struct udp4_flow_key k2)
179 return (rte_is_same_ether_addr(&k1.eth_saddr, &k2.eth_saddr) &&
180 rte_is_same_ether_addr(&k1.eth_daddr, &k2.eth_daddr) &&
181 (k1.ip_src_addr == k2.ip_src_addr) &&
182 (k1.ip_dst_addr == k2.ip_dst_addr) &&
183 (k1.ip_id == k2.ip_id));
187 * Merge two UDP/IPv4 packets without updating checksums.
188 * If cmp is larger than 0, append the new packet to the
189 * original packet. Otherwise, pre-pend the new packet to
190 * the original packet.
193 merge_two_udp4_packets(struct gro_udp4_item *item,
194 struct rte_mbuf *pkt,
196 uint16_t frag_offset,
197 uint8_t is_last_frag,
200 struct rte_mbuf *pkt_head, *pkt_tail, *lastseg;
201 uint16_t hdr_len, l2_len;
205 pkt_head = item->firstseg;
209 pkt_tail = item->firstseg;
212 /* check if the IPv4 packet length is greater than the max value */
213 hdr_len = l2_offset + pkt_head->l2_len + pkt_head->l3_len;
214 l2_len = l2_offset > 0 ? pkt_head->outer_l2_len : pkt_head->l2_len;
215 ip_len = pkt_head->pkt_len - l2_len
216 + pkt_tail->pkt_len - hdr_len;
217 if (unlikely(ip_len > MAX_IPV4_PKT_LENGTH))
220 /* remove the packet header for the tail packet */
221 rte_pktmbuf_adj(pkt_tail, hdr_len);
223 /* chain two packets together */
225 item->lastseg->next = pkt;
226 item->lastseg = rte_pktmbuf_lastseg(pkt);
228 lastseg = rte_pktmbuf_lastseg(pkt);
229 lastseg->next = item->firstseg;
230 item->firstseg = pkt;
231 item->frag_offset = frag_offset;
235 item->is_last_frag = is_last_frag;
237 /* update MBUF metadata for the merged packet */
238 pkt_head->nb_segs += pkt_tail->nb_segs;
239 pkt_head->pkt_len += pkt_tail->pkt_len;
245 * Check if two UDP/IPv4 packets are neighbors.
248 udp4_check_neighbor(struct gro_udp4_item *item,
249 uint16_t frag_offset,
253 struct rte_mbuf *pkt_orig = item->firstseg;
256 /* check if the two packets are neighbors */
257 len = pkt_orig->pkt_len - l2_offset - pkt_orig->l2_len -
259 if (frag_offset == item->frag_offset + len)
260 /* append the new packet */
262 else if (frag_offset + ip_dl == item->frag_offset)
263 /* pre-pend the new packet */
270 is_ipv4_fragment(const struct rte_ipv4_hdr *hdr)
272 uint16_t flag_offset, ip_flag, ip_ofs;
274 flag_offset = rte_be_to_cpu_16(hdr->fragment_offset);
275 ip_ofs = (uint16_t)(flag_offset & RTE_IPV4_HDR_OFFSET_MASK);
276 ip_flag = (uint16_t)(flag_offset & RTE_IPV4_HDR_MF_FLAG);
278 return ip_flag != 0 || ip_ofs != 0;