1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2020 Inspur Corporation
11 #define INVALID_ARRAY_INDEX 0xffffffffUL
12 #define GRO_UDP4_TBL_MAX_ITEM_NUM (1024UL * 1024UL)
15 * The max length of a IPv4 packet, which includes the length of the L3
16 * header, the L4 header and the data payload.
18 #define MAX_IPV4_PKT_LENGTH UINT16_MAX
20 /* Header fields representing a UDP/IPv4 flow */
21 struct udp4_flow_key {
22 struct rte_ether_addr eth_saddr;
23 struct rte_ether_addr eth_daddr;
27 /* IP fragment for UDP does not contain UDP header
28 * except the first one. But IP ID must be same.
33 struct gro_udp4_flow {
34 struct udp4_flow_key key;
36 * The index of the first packet in the flow.
37 * INVALID_ARRAY_INDEX indicates an empty flow.
42 struct gro_udp4_item {
44 * The first MBUF segment of the packet. If the value
45 * is NULL, it means the item is empty.
47 struct rte_mbuf *firstseg;
48 /* The last MBUF segment of the packet */
49 struct rte_mbuf *lastseg;
51 * The time when the first packet is inserted into the table.
52 * This value won't be updated, even if the packet is merged
57 * next_pkt_idx is used to chain the packets that
58 * are in the same flow but can't be merged together
59 * (e.g. caused by packet reordering).
61 uint32_t next_pkt_idx;
62 /* offset of IP fragment packet */
64 /* is last IP fragment? */
66 /* the number of merged packets */
71 * UDP/IPv4 reassembly table structure.
75 struct gro_udp4_item *items;
77 struct gro_udp4_flow *flows;
78 /* current item number */
80 /* current flow num */
83 uint32_t max_item_num;
85 uint32_t max_flow_num;
89 * This function creates a UDP/IPv4 reassembly table.
92 * Socket index for allocating the UDP/IPv4 reassemble table
94 * The maximum number of flows in the UDP/IPv4 GRO table
95 * @param max_item_per_flow
96 * The maximum number of packets per flow
99 * - Return the table pointer on success.
100 * - Return NULL on failure.
102 void *gro_udp4_tbl_create(uint16_t socket_id,
103 uint16_t max_flow_num,
104 uint16_t max_item_per_flow);
107 * This function destroys a UDP/IPv4 reassembly table.
110 * Pointer pointing to the UDP/IPv4 reassembly table.
112 void gro_udp4_tbl_destroy(void *tbl);
115 * This function merges a UDP/IPv4 packet.
117 * This function does not check if the packet has correct checksums and
118 * does not re-calculate checksums for the merged packet. It returns the
119 * packet if it isn't UDP fragment or there is no available space in
123 * Packet to reassemble
125 * Pointer pointing to the UDP/IPv4 reassembly table
127 * The time when the packet is inserted into the table
130 * - Return a positive value if the packet is merged.
131 * - Return zero if the packet isn't merged but stored in the table.
132 * - Return a negative value for invalid parameters or no available
133 * space in the table.
135 int32_t gro_udp4_reassemble(struct rte_mbuf *pkt,
136 struct gro_udp4_tbl *tbl,
137 uint64_t start_time);
140 * This function flushes timeout packets in a UDP/IPv4 reassembly table,
141 * and without updating checksums.
144 * UDP/IPv4 reassembly table pointer
145 * @param flush_timestamp
146 * Flush packets which are inserted into the table before or at the
149 * Pointer array used to keep flushed packets
151 * The element number in 'out'. It also determines the maximum number of
152 * packets that can be flushed finally.
155 * The number of flushed packets
157 uint16_t gro_udp4_tbl_timeout_flush(struct gro_udp4_tbl *tbl,
158 uint64_t flush_timestamp,
159 struct rte_mbuf **out,
163 * This function returns the number of the packets in a UDP/IPv4
167 * UDP/IPv4 reassembly table pointer
170 * The number of packets in the table
172 uint32_t gro_udp4_tbl_pkt_count(void *tbl);
175 * Check if two UDP/IPv4 packets belong to the same flow.
178 is_same_udp4_flow(struct udp4_flow_key k1, struct udp4_flow_key k2)
180 return (rte_is_same_ether_addr(&k1.eth_saddr, &k2.eth_saddr) &&
181 rte_is_same_ether_addr(&k1.eth_daddr, &k2.eth_daddr) &&
182 (k1.ip_src_addr == k2.ip_src_addr) &&
183 (k1.ip_dst_addr == k2.ip_dst_addr) &&
184 (k1.ip_id == k2.ip_id));
188 * Merge two UDP/IPv4 packets without updating checksums.
189 * If cmp is larger than 0, append the new packet to the
190 * original packet. Otherwise, pre-pend the new packet to
191 * the original packet.
194 merge_two_udp4_packets(struct gro_udp4_item *item,
195 struct rte_mbuf *pkt,
197 uint16_t frag_offset,
198 uint8_t is_last_frag,
201 struct rte_mbuf *pkt_head, *pkt_tail, *lastseg;
202 uint16_t hdr_len, l2_len;
206 pkt_head = item->firstseg;
210 pkt_tail = item->firstseg;
213 /* check if the IPv4 packet length is greater than the max value */
214 hdr_len = l2_offset + pkt_head->l2_len + pkt_head->l3_len;
215 l2_len = l2_offset > 0 ? pkt_head->outer_l2_len : pkt_head->l2_len;
216 ip_len = pkt_head->pkt_len - l2_len
217 + pkt_tail->pkt_len - hdr_len;
218 if (unlikely(ip_len > MAX_IPV4_PKT_LENGTH))
221 /* remove the packet header for the tail packet */
222 rte_pktmbuf_adj(pkt_tail, hdr_len);
224 /* chain two packets together */
226 item->lastseg->next = pkt;
227 item->lastseg = rte_pktmbuf_lastseg(pkt);
229 lastseg = rte_pktmbuf_lastseg(pkt);
230 lastseg->next = item->firstseg;
231 item->firstseg = pkt;
232 item->frag_offset = frag_offset;
236 item->is_last_frag = is_last_frag;
238 /* update MBUF metadata for the merged packet */
239 pkt_head->nb_segs += pkt_tail->nb_segs;
240 pkt_head->pkt_len += pkt_tail->pkt_len;
246 * Check if two UDP/IPv4 packets are neighbors.
249 udp4_check_neighbor(struct gro_udp4_item *item,
250 uint16_t frag_offset,
254 struct rte_mbuf *pkt_orig = item->firstseg;
257 /* check if the two packets are neighbors */
258 len = pkt_orig->pkt_len - l2_offset - pkt_orig->l2_len -
260 if (frag_offset == item->frag_offset + len)
261 /* append the new packet */
263 else if (frag_offset + ip_dl == item->frag_offset)
264 /* pre-pend the new packet */
271 is_ipv4_fragment(const struct rte_ipv4_hdr *hdr)
273 uint16_t flag_offset, ip_flag, ip_ofs;
275 flag_offset = rte_be_to_cpu_16(hdr->fragment_offset);
276 ip_ofs = (uint16_t)(flag_offset & RTE_IPV4_HDR_OFFSET_MASK);
277 ip_flag = (uint16_t)(flag_offset & RTE_IPV4_HDR_MF_FLAG);
279 return ip_flag != 0 || ip_ofs != 0;