21fea0927368aa8b15529a6d74fa5c42ae76281e
[dpdk.git] / lib / librte_gso / gso_udp4.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Intel Corporation
3  */
4
5 #include "gso_common.h"
6 #include "gso_udp4.h"
7
8 #define IPV4_HDR_MF_BIT (1U << 13)
9
10 static inline void
11 update_ipv4_udp_headers(struct rte_mbuf *pkt, struct rte_mbuf **segs,
12                 uint16_t nb_segs)
13 {
14         struct rte_ipv4_hdr *ipv4_hdr;
15         uint16_t frag_offset = 0, is_mf;
16         uint16_t l2_hdrlen = pkt->l2_len, l3_hdrlen = pkt->l3_len;
17         uint16_t tail_idx = nb_segs - 1, length, i;
18
19         /*
20          * Update IP header fields for output segments. Specifically,
21          * keep the same IP id, update fragment offset and total
22          * length.
23          */
24         for (i = 0; i < nb_segs; i++) {
25                 ipv4_hdr = rte_pktmbuf_mtod_offset(segs[i],
26                         struct rte_ipv4_hdr *, l2_hdrlen);
27                 length = segs[i]->pkt_len - l2_hdrlen;
28                 ipv4_hdr->total_length = rte_cpu_to_be_16(length);
29
30                 is_mf = i < tail_idx ? IPV4_HDR_MF_BIT : 0;
31                 ipv4_hdr->fragment_offset =
32                         rte_cpu_to_be_16(frag_offset | is_mf);
33                 frag_offset += ((length - l3_hdrlen) >> 3);
34         }
35 }
36
37 int
38 gso_udp4_segment(struct rte_mbuf *pkt,
39                 uint16_t gso_size,
40                 struct rte_mempool *direct_pool,
41                 struct rte_mempool *indirect_pool,
42                 struct rte_mbuf **pkts_out,
43                 uint16_t nb_pkts_out)
44 {
45         struct rte_ipv4_hdr *ipv4_hdr;
46         uint16_t pyld_unit_size, hdr_offset;
47         uint16_t frag_off;
48         int ret;
49
50         /* Don't process the fragmented packet */
51         ipv4_hdr = rte_pktmbuf_mtod_offset(pkt, struct rte_ipv4_hdr *,
52                         pkt->l2_len);
53         frag_off = rte_be_to_cpu_16(ipv4_hdr->fragment_offset);
54         if (unlikely(IS_FRAGMENTED(frag_off))) {
55                 pkts_out[0] = pkt;
56                 return 1;
57         }
58
59         /*
60          * UDP fragmentation is the same as IP fragmentation.
61          * Except the first one, other output packets just have l2
62          * and l3 headers.
63          */
64         hdr_offset = pkt->l2_len + pkt->l3_len;
65
66         /* Don't process the packet without data. */
67         if (unlikely(hdr_offset + pkt->l4_len >= pkt->pkt_len)) {
68                 pkts_out[0] = pkt;
69                 return 1;
70         }
71
72         pyld_unit_size = gso_size - hdr_offset;
73
74         /* Segment the payload */
75         ret = gso_do_segment(pkt, hdr_offset, pyld_unit_size, direct_pool,
76                         indirect_pool, pkts_out, nb_pkts_out);
77         if (ret > 1)
78                 update_ipv4_udp_headers(pkt, pkts_out, ret);
79
80         return ret;
81 }