2 // Copyright (c) 2010-2017 Intel Corporation
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <rte_ethdev.h>
18 #include <rte_ether.h>
22 #include "vxlangpe_nsh.h"
23 #include "task_base.h"
25 #include "task_init.h"
26 #include "thread_generic.h"
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
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);
38 struct task_decap_nsh {
39 struct task_base base;
42 struct task_encap_nsh {
43 struct task_base base;
46 static void init_task_decap_nsh(__attribute__((unused)) struct task_base *tbase,
47 __attribute__((unused)) struct task_args *targ)
52 static inline uint8_t handle_decap_nsh(__attribute__((unused)) struct task_decap_nsh *task, struct rte_mbuf *mbuf)
54 struct ether_hdr *eth_hdr = NULL;
55 struct udp_hdr *udp_hdr = NULL;
56 struct vxlan_gpe_hdr *vxlan_gpe_hdr = NULL;
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);
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;
71 if (mbuf->data_len < VXLAN_GPE_HDR_SZ) {
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) {
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) {
90 /* "decapsulate" VxLAN-GPE + NSH header by moving packet pointer */
91 hdr_len = VXLAN_GPE_HDR_SZ;
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;
103 static int handle_decap_nsh_bulk(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts)
105 struct task_decap_nsh *task = (struct task_decap_nsh *)tbase;
106 uint8_t out[MAX_PKT_BURST];
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 *));
115 out[j] = handle_decap_nsh(task, mbufs[j]);
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]);
123 return task->base.tx_pkt(&task->base, mbufs, n_pkts, out);
126 static void init_task_encap_nsh(__attribute__((unused)) struct task_base *tbase,
127 __attribute__((unused)) struct task_args *targ)
132 static inline uint8_t handle_encap_nsh(__attribute__((unused)) struct task_encap_nsh *task, struct rte_mbuf *mbuf)
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;
142 if (mbuf->udata64 == 0)
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);
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));
155 /* decrement Service Index in NSH header */
156 if (nsh_hdr->sf_index > 0)
157 nsh_hdr->sf_index -= 1;
160 /* "encapsulate" VxLAN-GPE + NSH header by moving packet pointer */
161 if (mbuf->data_len < VXLAN_GPE_HDR_SZ)
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)
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)
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;
183 static int handle_encap_nsh_bulk(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts)
185 struct task_encap_nsh *task = (struct task_encap_nsh *)tbase;
186 uint8_t out[MAX_PKT_BURST];
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 *));
195 out[j] = handle_encap_nsh(task, mbufs[j]);
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]);
203 return task->base.tx_pkt(&task->base, mbufs, n_pkts, out);
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)
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)
221 __attribute__((constructor)) static void reg_task_nshtag(void)
223 reg_task(&task_init_decap_nsh);
224 reg_task(&task_init_encap_nsh);