net: add rte prefix to SCTP structure
[dpdk.git] / examples / tep_termination / vxlan.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2015 Intel Corporation
3  */
4
5 #include <stdint.h>
6 #include <rte_mbuf.h>
7 #include <rte_hash_crc.h>
8 #include <rte_byteorder.h>
9 #include <rte_udp.h>
10 #include <rte_tcp.h>
11 #include <rte_sctp.h>
12
13 #include "main.h"
14 #include "vxlan.h"
15
16 static uint16_t
17 get_psd_sum(void *l3_hdr, uint16_t ethertype, uint64_t ol_flags)
18 {
19         if (ethertype == RTE_ETHER_TYPE_IPv4)
20                 return rte_ipv4_phdr_cksum(l3_hdr, ol_flags);
21         else /* assume ethertype == RTE_ETHER_TYPE_IPv6 */
22                 return rte_ipv6_phdr_cksum(l3_hdr, ol_flags);
23 }
24
25 /**
26  * Parse an ethernet header to fill the ethertype, outer_l2_len, outer_l3_len and
27  * ipproto. This function is able to recognize IPv4/IPv6 with one optional vlan
28  * header.
29  */
30 static void
31 parse_ethernet(struct rte_ether_hdr *eth_hdr, union tunnel_offload_info *info,
32                 uint8_t *l4_proto)
33 {
34         struct rte_ipv4_hdr *ipv4_hdr;
35         struct rte_ipv6_hdr *ipv6_hdr;
36         uint16_t ethertype;
37
38         info->outer_l2_len = sizeof(struct rte_ether_hdr);
39         ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
40
41         if (ethertype == RTE_ETHER_TYPE_VLAN) {
42                 struct rte_vlan_hdr *vlan_hdr =
43                         (struct rte_vlan_hdr *)(eth_hdr + 1);
44                 info->outer_l2_len  += sizeof(struct rte_vlan_hdr);
45                 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
46         }
47
48         switch (ethertype) {
49         case RTE_ETHER_TYPE_IPv4:
50                 ipv4_hdr = (struct rte_ipv4_hdr *)
51                         ((char *)eth_hdr + info->outer_l2_len);
52                 info->outer_l3_len = sizeof(struct rte_ipv4_hdr);
53                 *l4_proto = ipv4_hdr->next_proto_id;
54                 break;
55         case RTE_ETHER_TYPE_IPv6:
56                 ipv6_hdr = (struct rte_ipv6_hdr *)
57                         ((char *)eth_hdr + info->outer_l2_len);
58                 info->outer_l3_len = sizeof(struct rte_ipv6_hdr);
59                 *l4_proto = ipv6_hdr->proto;
60                 break;
61         default:
62                 info->outer_l3_len = 0;
63                 *l4_proto = 0;
64                 break;
65         }
66 }
67
68 /**
69  * Calculate the checksum of a packet in hardware
70  */
71 static uint64_t
72 process_inner_cksums(struct rte_ether_hdr *eth_hdr,
73                 union tunnel_offload_info *info)
74 {
75         void *l3_hdr = NULL;
76         uint8_t l4_proto;
77         uint16_t ethertype;
78         struct rte_ipv4_hdr *ipv4_hdr;
79         struct rte_ipv6_hdr *ipv6_hdr;
80         struct udp_hdr *udp_hdr;
81         struct tcp_hdr *tcp_hdr;
82         struct rte_sctp_hdr *sctp_hdr;
83         uint64_t ol_flags = 0;
84
85         info->l2_len = sizeof(struct rte_ether_hdr);
86         ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
87
88         if (ethertype == RTE_ETHER_TYPE_VLAN) {
89                 struct rte_vlan_hdr *vlan_hdr =
90                         (struct rte_vlan_hdr *)(eth_hdr + 1);
91                 info->l2_len  += sizeof(struct rte_vlan_hdr);
92                 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
93         }
94
95         l3_hdr = (char *)eth_hdr + info->l2_len;
96
97         if (ethertype == RTE_ETHER_TYPE_IPv4) {
98                 ipv4_hdr = (struct rte_ipv4_hdr *)l3_hdr;
99                 ipv4_hdr->hdr_checksum = 0;
100                 ol_flags |= PKT_TX_IPV4;
101                 ol_flags |= PKT_TX_IP_CKSUM;
102                 info->l3_len = sizeof(struct rte_ipv4_hdr);
103                 l4_proto = ipv4_hdr->next_proto_id;
104         } else if (ethertype == RTE_ETHER_TYPE_IPv6) {
105                 ipv6_hdr = (struct rte_ipv6_hdr *)l3_hdr;
106                 info->l3_len = sizeof(struct rte_ipv6_hdr);
107                 l4_proto = ipv6_hdr->proto;
108                 ol_flags |= PKT_TX_IPV6;
109         } else
110                 return 0; /* packet type not supported, nothing to do */
111
112         if (l4_proto == IPPROTO_UDP) {
113                 udp_hdr = (struct udp_hdr *)((char *)l3_hdr + info->l3_len);
114                 ol_flags |= PKT_TX_UDP_CKSUM;
115                 udp_hdr->dgram_cksum = get_psd_sum(l3_hdr,
116                                 ethertype, ol_flags);
117         } else if (l4_proto == IPPROTO_TCP) {
118                 tcp_hdr = (struct tcp_hdr *)((char *)l3_hdr + info->l3_len);
119                 /* Put PKT_TX_TCP_SEG bit setting before get_psd_sum(), because
120                  * it depends on PKT_TX_TCP_SEG to calculate pseudo-header
121                  * checksum.
122                  */
123                 if (tso_segsz != 0) {
124                         ol_flags |= PKT_TX_TCP_SEG;
125                         info->tso_segsz = tso_segsz;
126                         info->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
127                 }
128                 ol_flags |= PKT_TX_TCP_CKSUM;
129                 tcp_hdr->cksum = get_psd_sum(l3_hdr, ethertype, ol_flags);
130
131         } else if (l4_proto == IPPROTO_SCTP) {
132                 sctp_hdr = (struct rte_sctp_hdr *)
133                         ((char *)l3_hdr + info->l3_len);
134                 sctp_hdr->cksum = 0;
135                 ol_flags |= PKT_TX_SCTP_CKSUM;
136         }
137
138         return ol_flags;
139 }
140
141 int
142 decapsulation(struct rte_mbuf *pkt)
143 {
144         uint8_t l4_proto = 0;
145         uint16_t outer_header_len;
146         struct udp_hdr *udp_hdr;
147         union tunnel_offload_info info = { .data = 0 };
148         struct rte_ether_hdr *phdr =
149                 rte_pktmbuf_mtod(pkt, struct rte_ether_hdr *);
150
151         parse_ethernet(phdr, &info, &l4_proto);
152
153         if (l4_proto != IPPROTO_UDP)
154                 return -1;
155
156         udp_hdr = (struct udp_hdr *)((char *)phdr +
157                 info.outer_l2_len + info.outer_l3_len);
158
159         /** check udp destination port, 4789 is the default vxlan port
160          * (rfc7348) or that the rx offload flag is set (i40e only
161          * currently)*/
162         if (udp_hdr->dst_port != rte_cpu_to_be_16(DEFAULT_VXLAN_PORT) &&
163                 (pkt->packet_type & RTE_PTYPE_TUNNEL_MASK) == 0)
164                 return -1;
165         outer_header_len = info.outer_l2_len + info.outer_l3_len
166                 + sizeof(struct udp_hdr) + sizeof(struct rte_vxlan_hdr);
167
168         rte_pktmbuf_adj(pkt, outer_header_len);
169
170         return 0;
171 }
172
173 void
174 encapsulation(struct rte_mbuf *m, uint8_t queue_id)
175 {
176         uint vport_id;
177         uint64_t ol_flags = 0;
178         uint32_t old_len = m->pkt_len, hash;
179         union tunnel_offload_info tx_offload = { .data = 0 };
180         struct rte_ether_hdr *phdr =
181                 rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
182
183         /*Allocate space for new ethernet, IPv4, UDP and VXLAN headers*/
184         struct rte_ether_hdr *pneth =
185                 (struct rte_ether_hdr *) rte_pktmbuf_prepend(m,
186                 sizeof(struct rte_ether_hdr) + sizeof(struct rte_ipv4_hdr)
187                 + sizeof(struct udp_hdr) + sizeof(struct rte_vxlan_hdr));
188
189         struct rte_ipv4_hdr *ip = (struct rte_ipv4_hdr *) &pneth[1];
190         struct udp_hdr *udp = (struct udp_hdr *) &ip[1];
191         struct rte_vxlan_hdr *vxlan = (struct rte_vxlan_hdr *) &udp[1];
192
193         /* convert TX queue ID to vport ID */
194         vport_id = queue_id - 1;
195
196         /* replace original Ethernet header with ours */
197         pneth = rte_memcpy(pneth, &app_l2_hdr[vport_id],
198                 sizeof(struct rte_ether_hdr));
199
200         /* copy in IP header */
201         ip = rte_memcpy(ip, &app_ip_hdr[vport_id],
202                 sizeof(struct rte_ipv4_hdr));
203         ip->total_length = rte_cpu_to_be_16(m->pkt_len
204                                 - sizeof(struct rte_ether_hdr));
205
206         /* outer IP checksum */
207         ol_flags |= PKT_TX_OUTER_IP_CKSUM;
208         ip->hdr_checksum = 0;
209
210         /* inner IP checksum offload */
211         if (tx_checksum) {
212                 ol_flags |= process_inner_cksums(phdr, &tx_offload);
213                 m->l2_len = tx_offload.l2_len;
214                 m->l3_len = tx_offload.l3_len;
215                 m->l4_len = tx_offload.l4_len;
216                 m->l2_len += RTE_ETHER_VXLAN_HLEN;
217         }
218
219         m->outer_l2_len = sizeof(struct rte_ether_hdr);
220         m->outer_l3_len = sizeof(struct rte_ipv4_hdr);
221
222         ol_flags |= PKT_TX_TUNNEL_VXLAN;
223
224         m->ol_flags |= ol_flags;
225         m->tso_segsz = tx_offload.tso_segsz;
226
227         /*VXLAN HEADER*/
228         vxlan->vx_flags = rte_cpu_to_be_32(VXLAN_HF_VNI);
229         vxlan->vx_vni = rte_cpu_to_be_32(vxdev.out_key << 8);
230
231         /*UDP HEADER*/
232         udp->dgram_cksum = 0;
233         udp->dgram_len = rte_cpu_to_be_16(old_len
234                                 + sizeof(struct udp_hdr)
235                                 + sizeof(struct rte_vxlan_hdr));
236
237         udp->dst_port = rte_cpu_to_be_16(vxdev.dst_port);
238         hash = rte_hash_crc(phdr, 2 * RTE_ETHER_ADDR_LEN, phdr->ether_type);
239         udp->src_port = rte_cpu_to_be_16((((uint64_t) hash * PORT_RANGE) >> 32)
240                                         + PORT_MIN);
241
242         return;
243 }