Initial support for DPDK 18.05
[samplevnf.git] / VNFs / DPPD-PROX / handle_nsh.c
1 /*
2 // Copyright (c) 2010-2017 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16
17 #include <rte_ethdev.h>
18 #include <rte_ether.h>
19 #include <rte_ip.h>
20 #include <rte_udp.h>
21
22 #include "vxlangpe_nsh.h"
23 #include "task_base.h"
24 #include "tx_pkt.h"
25 #include "task_init.h"
26 #include "thread_generic.h"
27 #include "prefetch.h"
28 #include "log.h"
29
30 #define VXLAN_GPE_HDR_SZ sizeof(struct ether_hdr) + sizeof(struct ipv4_hdr) + sizeof(struct udp_hdr) + sizeof(struct vxlan_gpe_hdr) + sizeof(struct nsh_hdr)
31 #define ETHER_NSH_TYPE 0x4F89 /* 0x894F in little endian */
32 #define VXLAN_GPE_NSH_TYPE 0xB612 /* 4790 in little endian */
33 #define VXLAN_GPE_NP 0x4
34
35 uint16_t decap_nsh_packets(struct rte_mbuf **mbufs, uint16_t n_pkts);
36 uint16_t encap_nsh_packets(struct rte_mbuf **mbufs, uint16_t n_pkts);
37
38 struct task_decap_nsh {
39         struct task_base base;
40 };
41
42 struct task_encap_nsh {
43         struct task_base base;
44 };
45
46 static void init_task_decap_nsh(__attribute__((unused)) struct task_base *tbase,
47                              __attribute__((unused)) struct task_args *targ)
48 {
49         return;
50 }
51
52 static inline uint8_t handle_decap_nsh(__attribute__((unused)) struct task_decap_nsh *task, struct rte_mbuf *mbuf)
53 {
54         struct ether_hdr *eth_hdr = NULL;
55         struct udp_hdr *udp_hdr = NULL;
56         struct vxlan_gpe_hdr *vxlan_gpe_hdr = NULL;
57         uint16_t hdr_len;
58
59         eth_hdr = rte_pktmbuf_mtod(mbuf, struct ether_hdr *);
60         if (eth_hdr->ether_type == ETHER_NSH_TYPE) {
61                 /* "decapsulate" Ethernet + NSH header by moving packet pointer */
62                 hdr_len = sizeof(struct ether_hdr) + sizeof(struct nsh_hdr);
63
64                 mbuf->data_len = (uint16_t)(mbuf->data_len - hdr_len);
65                 mbuf->data_off += hdr_len;
66                 mbuf->pkt_len = (uint32_t)(mbuf->pkt_len - hdr_len);
67                 /* save length of header in reserved 16bits of rte_mbuf */
68                 mbuf->udata64 = hdr_len;
69         }
70         else {
71                 if (mbuf->data_len < VXLAN_GPE_HDR_SZ) {
72                         mbuf->udata64 = 0;
73                         return 0;
74                 }
75
76                 /* check the UDP destination port */
77                 udp_hdr = (struct udp_hdr *)(((unsigned char *)eth_hdr) + sizeof(struct ether_hdr) + sizeof(struct ipv4_hdr));
78                 if (udp_hdr->dst_port != VXLAN_GPE_NSH_TYPE) {
79                         mbuf->udata64 = 0;
80                         return 0;
81                 }
82
83                 /* check the Next Protocol field in VxLAN-GPE header */
84                 vxlan_gpe_hdr = (struct vxlan_gpe_hdr *)(((unsigned char *)eth_hdr) + sizeof(struct ether_hdr) + sizeof(struct ipv4_hdr) + sizeof(struct udp_hdr));
85                 if (vxlan_gpe_hdr->proto != VXLAN_GPE_NP) {
86                         mbuf->udata64 = 0;
87                         return 0;
88                 }
89
90                 /* "decapsulate" VxLAN-GPE + NSH header by moving packet pointer */
91                 hdr_len = VXLAN_GPE_HDR_SZ;
92
93                 mbuf->data_len = (uint16_t)(mbuf->data_len - hdr_len);
94                 mbuf->data_off += hdr_len;
95                 mbuf->pkt_len  = (uint32_t)(mbuf->pkt_len - hdr_len);
96                 /* save length of header in reserved 16bits of rte_mbuf */
97                 mbuf->udata64 = hdr_len;
98         }
99
100         return 0;
101 }
102
103 static int handle_decap_nsh_bulk(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts)
104 {
105         struct task_decap_nsh *task = (struct task_decap_nsh *)tbase;
106         uint8_t out[MAX_PKT_BURST];
107         uint16_t j;
108
109         prefetch_first(mbufs, n_pkts);
110         for (j = 0; j + PREFETCH_OFFSET < n_pkts; ++j) {
111 #ifdef PROX_PREFETCH_OFFSET
112                 PREFETCH0(mbufs[j + PREFETCH_OFFSET]);
113                 PREFETCH0(rte_pktmbuf_mtod(mbufs[j + PREFETCH_OFFSET - 1], void *));
114 #endif
115                 out[j] = handle_decap_nsh(task, mbufs[j]);
116         }
117 #ifdef PROX_PREFETCH_OFFSET
118         PREFETCH0(rte_pktmbuf_mtod(mbufs[n_pkts - 1], void *));
119         for (; j < n_pkts; ++j) {
120                 out[j] = handle_decap_nsh(task, mbufs[j]);
121         }
122 #endif
123         return task->base.tx_pkt(&task->base, mbufs, n_pkts, out);
124 }
125
126 static void init_task_encap_nsh(__attribute__((unused)) struct task_base *tbase,
127                               __attribute__((unused)) struct task_args *targ)
128 {
129         return;
130 }
131
132 static inline uint8_t handle_encap_nsh(__attribute__((unused)) struct task_encap_nsh *task, struct rte_mbuf *mbuf)
133 {
134         struct ether_hdr *eth_hdr = NULL;
135         struct nsh_hdr *nsh_hdr = NULL;
136         struct udp_hdr *udp_hdr = NULL;
137         struct vxlan_gpe_hdr *vxlan_gpe_hdr = NULL;
138         uint16_t hdr_len;
139
140         if (mbuf == NULL)
141                 return 0;
142         if (mbuf->udata64 == 0)
143                 return 0;
144
145         /* use header length saved in reserved 16bits of rte_mbuf to
146            "encapsulate" transport + NSH header by moving packet pointer */
147         mbuf->data_len = (uint16_t)(mbuf->data_len + mbuf->udata64);
148         mbuf->data_off -= mbuf->udata64;
149         mbuf->pkt_len  = (uint32_t)(mbuf->pkt_len + mbuf->udata64);
150
151         eth_hdr = rte_pktmbuf_mtod(mbuf, struct ether_hdr *);
152         if (eth_hdr->ether_type == ETHER_NSH_TYPE) {
153                 nsh_hdr = (struct nsh_hdr *) (((unsigned char *)eth_hdr) + sizeof(struct ether_hdr));
154
155                 /* decrement Service Index in NSH header */
156                 if (nsh_hdr->sf_index > 0)
157                         nsh_hdr->sf_index -= 1;
158         }
159         else {
160                 /* "encapsulate" VxLAN-GPE + NSH header by moving packet pointer */
161                 if (mbuf->data_len < VXLAN_GPE_HDR_SZ)
162                         return 0;
163
164                 /* check the UDP destination port */
165                 udp_hdr = (struct udp_hdr *)(((unsigned char *)eth_hdr) + sizeof(struct ether_hdr) + sizeof(struct ipv4_hdr));
166                 if (udp_hdr->dst_port != VXLAN_GPE_NSH_TYPE)
167                         return 0;
168
169                 /* check the Next Protocol field in VxLAN-GPE header */
170                 vxlan_gpe_hdr = (struct vxlan_gpe_hdr *)(((unsigned char *)eth_hdr) + sizeof(struct ether_hdr) + sizeof(struct ipv4_hdr) + sizeof(struct udp_hdr));
171                 if (vxlan_gpe_hdr->proto != VXLAN_GPE_NP)
172                         return 0;
173
174                 /* decrement Service Index in NSH header */
175                 nsh_hdr = (struct nsh_hdr *)(((unsigned char *)vxlan_gpe_hdr) + sizeof(struct vxlan_gpe_hdr));
176                 if (nsh_hdr->sf_index > 0)
177                         nsh_hdr->sf_index -= 1;
178         }
179
180         return 0;
181 }
182
183 static int handle_encap_nsh_bulk(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts)
184 {
185         struct task_encap_nsh *task = (struct task_encap_nsh *)tbase;
186         uint8_t out[MAX_PKT_BURST];
187         uint16_t j;
188
189         prefetch_first(mbufs, n_pkts);
190         for (j = 0; j + PREFETCH_OFFSET < n_pkts; ++j) {
191 #ifdef PROX_PREFETCH_OFFSET
192                 PREFETCH0(mbufs[j + PREFETCH_OFFSET]);
193                 PREFETCH0(rte_pktmbuf_mtod(mbufs[j + PREFETCH_OFFSET - 1], void *));
194 #endif
195                 out[j] = handle_encap_nsh(task, mbufs[j]);
196         }
197 #ifdef PROX_PREFETCH_OFFSET
198         PREFETCH0(rte_pktmbuf_mtod(mbufs[n_pkts - 1], void *));
199         for (; j < n_pkts; ++j) {
200                 out[j] = handle_encap_nsh(task, mbufs[j]);
201         }
202 #endif
203         return task->base.tx_pkt(&task->base, mbufs, n_pkts, out);
204 }
205
206 static struct task_init task_init_decap_nsh = {
207         .mode_str = "decapnsh",
208         .init = init_task_decap_nsh,
209         .handle = handle_decap_nsh_bulk,
210         .thread_x = thread_generic,
211         .size = sizeof(struct task_decap_nsh)
212 };
213
214 static struct task_init task_init_encap_nsh = {
215         .mode_str = "encapnsh",
216         .init = init_task_encap_nsh,
217         .handle = handle_encap_nsh_bulk,
218         .size = sizeof(struct task_encap_nsh)
219 };
220
221 __attribute__((constructor)) static void reg_task_nshtag(void)
222 {
223         reg_task(&task_init_decap_nsh);
224         reg_task(&task_init_encap_nsh);
225 }