2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
15 #include <vppinfra/error.h>
16 #include <vppinfra/hash.h>
17 #include <vnet/vnet.h>
18 #include <vnet/ip/ip.h>
19 #include <vnet/ethernet/ethernet.h>
20 #include <vnet/nsh-vxlan-gpe/nsh_vxlan_gpe.h>
22 /* Statistics (not really errors) */
23 #define foreach_nsh_vxlan_gpe_encap_error \
24 _(ENCAPSULATED, "good packets encapsulated")
26 static char * nsh_vxlan_gpe_encap_error_strings[] = {
27 #define _(sym,string) string,
28 foreach_nsh_vxlan_gpe_encap_error
33 #define _(sym,str) NSH_VXLAN_GPE_ENCAP_ERROR_##sym,
34 foreach_nsh_vxlan_gpe_encap_error
36 NSH_VXLAN_GPE_ENCAP_N_ERROR,
37 } nsh_vxlan_gpe_encap_error_t;
40 NSH_VXLAN_GPE_ENCAP_NEXT_IP4_LOOKUP,
41 NSH_VXLAN_GPE_ENCAP_NEXT_DROP,
42 NSH_VXLAN_GPE_ENCAP_N_NEXT,
43 } nsh_vxlan_gpe_encap_next_t;
47 } nsh_vxlan_gpe_encap_trace_t;
49 u8 * format_nsh_vxlan_gpe_encap_trace (u8 * s, va_list * args)
51 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
52 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
53 nsh_vxlan_gpe_encap_trace_t * t
54 = va_arg (*args, nsh_vxlan_gpe_encap_trace_t *);
56 s = format (s, "NSH-VXLAN-ENCAP: tunnel %d", t->tunnel_index);
60 #define foreach_fixed_header_offset \
61 _(0) _(1) _(2) _(3) _(4) _(5) _(6)
64 nsh_vxlan_gpe_encap (vlib_main_t * vm,
65 vlib_node_runtime_t * node,
66 vlib_frame_t * from_frame)
68 u32 n_left_from, next_index, * from, * to_next;
69 nsh_vxlan_gpe_main_t * ngm = &nsh_vxlan_gpe_main;
70 vnet_main_t * vnm = ngm->vnet_main;
71 u32 pkts_encapsulated = 0;
72 u16 old_l0 = 0, old_l1 = 0;
74 from = vlib_frame_vector_args (from_frame);
75 n_left_from = from_frame->n_vectors;
77 next_index = node->cached_next_index;
79 while (n_left_from > 0)
83 vlib_get_next_frame (vm, node, next_index,
84 to_next, n_left_to_next);
86 while (n_left_from >= 4 && n_left_to_next >= 2)
89 vlib_buffer_t * b0, * b1;
90 u32 next0 = NSH_VXLAN_GPE_ENCAP_NEXT_IP4_LOOKUP;
91 u32 next1 = NSH_VXLAN_GPE_ENCAP_NEXT_IP4_LOOKUP;
92 vnet_hw_interface_t * hi0, * hi1;
93 ip4_header_t * ip0, * ip1;
94 udp_header_t * udp0, * udp1;
95 u64 * copy_src0, * copy_dst0;
96 u64 * copy_src1, * copy_dst1;
97 u32 * copy_src_last0, * copy_dst_last0;
98 u32 * copy_src_last1, * copy_dst_last1;
99 nsh_vxlan_gpe_tunnel_t * t0, * t1;
101 ip_csum_t sum0, sum1;
103 /* Prefetch next iteration. */
105 vlib_buffer_t * p2, * p3;
107 p2 = vlib_get_buffer (vm, from[2]);
108 p3 = vlib_get_buffer (vm, from[3]);
110 vlib_prefetch_buffer_header (p2, LOAD);
111 vlib_prefetch_buffer_header (p3, LOAD);
113 CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
114 CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
126 b0 = vlib_get_buffer (vm, bi0);
127 b1 = vlib_get_buffer (vm, bi1);
130 hi0 = vnet_get_sup_hw_interface
131 (vnm, vnet_buffer(b0)->sw_if_index[VLIB_TX]);
132 hi1 = vnet_get_sup_hw_interface
133 (vnm, vnet_buffer(b1)->sw_if_index[VLIB_TX]);
135 t0 = pool_elt_at_index (ngm->tunnels, hi0->dev_instance);
136 t1 = pool_elt_at_index (ngm->tunnels, hi1->dev_instance);
138 ASSERT(vec_len(t0->rewrite) >= 24);
139 ASSERT(vec_len(t1->rewrite) >= 24);
141 /* Apply the rewrite string. $$$$ vnet_rewrite? */
142 vlib_buffer_advance (b0, -(word)_vec_len(t0->rewrite));
143 vlib_buffer_advance (b1, -(word)_vec_len(t1->rewrite));
145 ip0 = vlib_buffer_get_current(b0);
146 ip1 = vlib_buffer_get_current(b1);
147 /* Copy the fixed header */
148 copy_dst0 = (u64 *) ip0;
149 copy_src0 = (u64 *) t0->rewrite;
150 copy_dst1 = (u64 *) ip1;
151 copy_src1 = (u64 *) t1->rewrite;
153 ASSERT (sizeof (ip4_vxlan_gpe_and_nsh_header_t) == 60);
155 /* Copy first 56 octets 8-bytes at a time */
156 #define _(offs) copy_dst0[offs] = copy_src0[offs];
157 foreach_fixed_header_offset;
159 #define _(offs) copy_dst1[offs] = copy_src1[offs];
160 foreach_fixed_header_offset;
163 /* Last 4 octets. Hopefully gcc will be our friend */
164 copy_dst_last0 = (u32 *)(©_dst0[7]);
165 copy_src_last0 = (u32 *)(©_src0[7]);
166 copy_dst_last1 = (u32 *)(©_dst1[7]);
167 copy_src_last1 = (u32 *)(©_src1[7]);
169 copy_dst_last0[0] = copy_src_last0[0];
170 copy_dst_last1[0] = copy_src_last1[0];
172 /* If there are TLVs to copy, do so */
173 if (PREDICT_FALSE (_vec_len(t0->rewrite) > 64))
174 memcpy (©_dst0[3], t0->rewrite + 64 ,
175 _vec_len (t0->rewrite)-64);
177 if (PREDICT_FALSE (_vec_len(t1->rewrite) > 64))
178 memcpy (©_dst0[3], t1->rewrite + 64 ,
179 _vec_len (t1->rewrite)-64);
181 /* fix the <bleep>ing outer-IP checksum */
182 sum0 = ip0->checksum;
183 /* old_l0 always 0, see the rewrite setup */
185 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
187 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
188 length /* changed member */);
189 ip0->checksum = ip_csum_fold (sum0);
190 ip0->length = new_l0;
192 sum1 = ip1->checksum;
193 /* old_l1 always 0, see the rewrite setup */
195 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1));
197 sum1 = ip_csum_update (sum1, old_l1, new_l1, ip4_header_t,
198 length /* changed member */);
199 ip1->checksum = ip_csum_fold (sum1);
200 ip1->length = new_l1;
203 udp0 = (udp_header_t *)(ip0+1);
204 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
206 udp1 = (udp_header_t *)(ip1+1);
207 new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1)
210 udp0->length = new_l0;
211 udp1->length = new_l1;
213 /* Reset to look up tunnel partner in the configured FIB */
214 vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->encap_fib_index;
215 vnet_buffer(b1)->sw_if_index[VLIB_TX] = t1->encap_fib_index;
217 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
219 nsh_vxlan_gpe_encap_trace_t *tr =
220 vlib_add_trace (vm, node, b0, sizeof (*tr));
221 tr->tunnel_index = t0 - ngm->tunnels;
224 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
226 nsh_vxlan_gpe_encap_trace_t *tr =
227 vlib_add_trace (vm, node, b1, sizeof (*tr));
228 tr->tunnel_index = t1 - ngm->tunnels;
231 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
232 to_next, n_left_to_next,
233 bi0, bi1, next0, next1);
236 while (n_left_from > 0 && n_left_to_next > 0)
240 u32 next0 = NSH_VXLAN_GPE_ENCAP_NEXT_IP4_LOOKUP;
241 vnet_hw_interface_t * hi0;
244 u64 * copy_src0, * copy_dst0;
245 u32 * copy_src_last0, * copy_dst_last0;
246 nsh_vxlan_gpe_tunnel_t * t0;
257 b0 = vlib_get_buffer (vm, bi0);
260 hi0 = vnet_get_sup_hw_interface
261 (vnm, vnet_buffer(b0)->sw_if_index[VLIB_TX]);
263 t0 = pool_elt_at_index (ngm->tunnels, hi0->dev_instance);
265 ASSERT(vec_len(t0->rewrite) >= 24);
267 /* Apply the rewrite string. $$$$ vnet_rewrite? */
268 vlib_buffer_advance (b0, -(word)_vec_len(t0->rewrite));
270 ip0 = vlib_buffer_get_current(b0);
271 /* Copy the fixed header */
272 copy_dst0 = (u64 *) ip0;
273 copy_src0 = (u64 *) t0->rewrite;
275 ASSERT (sizeof (ip4_vxlan_gpe_and_nsh_header_t) == 60);
277 /* Copy first 56 octets 8-bytes at a time */
278 #define _(offs) copy_dst0[offs] = copy_src0[offs];
279 foreach_fixed_header_offset;
281 /* Last 4 octets. Hopefully gcc will be our friend */
282 copy_dst_last0 = (u32 *)(©_dst0[7]);
283 copy_src_last0 = (u32 *)(©_src0[7]);
285 copy_dst_last0[0] = copy_src_last0[0];
287 /* If there are TLVs to copy, do so */
288 if (PREDICT_FALSE (_vec_len(t0->rewrite) > 64))
289 memcpy (©_dst0[3], t0->rewrite + 64 ,
290 _vec_len (t0->rewrite)-64);
292 /* fix the <bleep>ing outer-IP checksum */
293 sum0 = ip0->checksum;
294 /* old_l0 always 0, see the rewrite setup */
296 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
298 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
299 length /* changed member */);
300 ip0->checksum = ip_csum_fold (sum0);
301 ip0->length = new_l0;
304 udp0 = (udp_header_t *)(ip0+1);
305 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
308 udp0->length = new_l0;
310 /* Reset to look up tunnel partner in the configured FIB */
311 vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->encap_fib_index;
312 pkts_encapsulated ++;
314 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
316 nsh_vxlan_gpe_encap_trace_t *tr =
317 vlib_add_trace (vm, node, b0, sizeof (*tr));
318 tr->tunnel_index = t0 - ngm->tunnels;
320 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
321 to_next, n_left_to_next,
325 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
327 vlib_node_increment_counter (vm, node->node_index,
328 NSH_VXLAN_GPE_ENCAP_ERROR_ENCAPSULATED,
330 return from_frame->n_vectors;
333 VLIB_REGISTER_NODE (nsh_vxlan_gpe_encap_node) = {
334 .function = nsh_vxlan_gpe_encap,
335 .name = "nsh-vxlan-gpe-encap",
336 .vector_size = sizeof (u32),
337 .format_trace = format_nsh_vxlan_gpe_encap_trace,
338 .type = VLIB_NODE_TYPE_INTERNAL,
340 .n_errors = ARRAY_LEN(nsh_vxlan_gpe_encap_error_strings),
341 .error_strings = nsh_vxlan_gpe_encap_error_strings,
343 .n_next_nodes = NSH_VXLAN_GPE_ENCAP_N_NEXT,
346 [NSH_VXLAN_GPE_ENCAP_NEXT_IP4_LOOKUP] = "ip4-lookup",
347 [NSH_VXLAN_GPE_ENCAP_NEXT_DROP] = "error-drop",