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-gre/nsh_gre.h>
22 /* Statistics (not really errors) */
23 #define foreach_nsh_gre_encap_error \
24 _(ENCAPSULATED, "good packets encapsulated")
26 static char * nsh_gre_encap_error_strings[] = {
27 #define _(sym,string) string,
28 foreach_nsh_gre_encap_error
33 #define _(sym,str) NSH_GRE_ENCAP_ERROR_##sym,
34 foreach_nsh_gre_encap_error
36 NSH_GRE_ENCAP_N_ERROR,
37 } nsh_gre_encap_error_t;
40 NSH_GRE_ENCAP_NEXT_IP4_LOOKUP,
41 NSH_GRE_ENCAP_NEXT_DROP,
43 } nsh_gre_encap_next_t;
47 } nsh_gre_encap_trace_t;
49 u8 * format_nsh_gre_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_gre_encap_trace_t * t = va_arg (*args, nsh_gre_encap_trace_t *);
55 s = format (s, "NSH-GRE-ENCAP: tunnel %d", t->tunnel_index);
60 nsh_gre_encap (vlib_main_t * vm,
61 vlib_node_runtime_t * node,
62 vlib_frame_t * from_frame)
64 u32 n_left_from, next_index, * from, * to_next;
65 nsh_gre_main_t * ngm = &nsh_gre_main;
66 vnet_main_t * vnm = ngm->vnet_main;
67 vnet_interface_main_t * im = &vnm->interface_main;
68 u32 pkts_encapsulated = 0;
69 u16 old_l0 = 0, old_l1 = 0;
70 u32 cpu_index = os_get_cpu_number();
71 u32 stats_sw_if_index, stats_n_packets, stats_n_bytes;
73 from = vlib_frame_vector_args (from_frame);
74 n_left_from = from_frame->n_vectors;
76 next_index = node->cached_next_index;
77 stats_sw_if_index = node->runtime_data[0];
78 stats_n_packets = stats_n_bytes = 0;
80 while (n_left_from > 0)
84 vlib_get_next_frame (vm, node, next_index,
85 to_next, n_left_to_next);
87 while (n_left_from >= 4 && n_left_to_next >= 2)
90 vlib_buffer_t * b0, * b1;
91 u32 next0 = NSH_GRE_ENCAP_NEXT_IP4_LOOKUP;
92 u32 next1 = NSH_GRE_ENCAP_NEXT_IP4_LOOKUP;
93 u32 sw_if_index0, sw_if_index1, len0, len1;
94 vnet_hw_interface_t * hi0, * hi1;
95 ip4_header_t * ip0, * ip1;
96 u64 * copy_src0, * copy_dst0;
97 u64 * copy_src1, * copy_dst1;
98 nsh_gre_tunnel_t * t0, * t1;
100 ip_csum_t sum0, sum1;
102 /* Prefetch next iteration. */
104 vlib_buffer_t * p2, * p3;
106 p2 = vlib_get_buffer (vm, from[2]);
107 p3 = vlib_get_buffer (vm, from[3]);
109 vlib_prefetch_buffer_header (p2, LOAD);
110 vlib_prefetch_buffer_header (p3, LOAD);
112 CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
113 CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
125 b0 = vlib_get_buffer (vm, bi0);
126 b1 = vlib_get_buffer (vm, bi1);
129 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_TX];
130 sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_TX];
131 hi0 = vnet_get_sup_hw_interface
132 (vnm, vnet_buffer(b0)->sw_if_index[VLIB_TX]);
133 hi1 = vnet_get_sup_hw_interface
134 (vnm, vnet_buffer(b1)->sw_if_index[VLIB_TX]);
136 t0 = pool_elt_at_index (ngm->tunnels, hi0->dev_instance);
137 t1 = pool_elt_at_index (ngm->tunnels, hi1->dev_instance);
139 ASSERT(vec_len(t0->rewrite) >= 24);
140 ASSERT(vec_len(t1->rewrite) >= 24);
142 /* Apply the rewrite string. $$$$ vnet_rewrite? */
143 vlib_buffer_advance (b0, -(word)_vec_len(t0->rewrite));
144 vlib_buffer_advance (b1, -(word)_vec_len(t1->rewrite));
146 ip0 = vlib_buffer_get_current(b0);
147 ip1 = vlib_buffer_get_current(b1);
148 /* Copy the fixed header */
149 copy_dst0 = (u64 *) ip0;
150 copy_src0 = (u64 *) t0->rewrite;
151 copy_dst1 = (u64 *) ip1;
152 copy_src1 = (u64 *) t1->rewrite;
154 copy_dst0[0] = copy_src0[0];
155 copy_dst0[1] = copy_src0[1];
156 copy_dst0[2] = copy_src0[2];
158 copy_dst1[0] = copy_src1[0];
159 copy_dst1[1] = copy_src1[1];
160 copy_dst1[2] = copy_src1[2];
162 /* If there are TLVs to copy, do so */
163 if (PREDICT_FALSE (_vec_len(t0->rewrite) > 24))
164 clib_memcpy (©_dst0[3], t0->rewrite + 24 ,
165 _vec_len (t0->rewrite)-24);
167 if (PREDICT_FALSE (_vec_len(t1->rewrite) > 24))
168 clib_memcpy (©_dst1[3], t1->rewrite + 24 ,
169 _vec_len (t1->rewrite)-24);
171 /* fix the <bleep>ing outer-IP checksums */
172 sum0 = ip0->checksum;
173 /* old_l0 always 0, see the rewrite setup */
175 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
177 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
178 length /* changed member */);
179 ip0->checksum = ip_csum_fold (sum0);
180 ip0->length = new_l0;
182 sum1 = ip1->checksum;
183 /* old_l1 always 1, see the rewrite setup */
185 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1));
187 sum1 = ip_csum_update (sum1, old_l1, new_l1, ip4_header_t,
188 length /* changed member */);
189 ip1->checksum = ip_csum_fold (sum1);
190 ip1->length = new_l1;
192 /* Reset to look up tunnel partner in the configured FIB */
193 vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->encap_fib_index;
194 vnet_buffer(b1)->sw_if_index[VLIB_TX] = t1->encap_fib_index;
195 vnet_buffer(b0)->sw_if_index[VLIB_RX] = sw_if_index0;
196 vnet_buffer(b1)->sw_if_index[VLIB_RX] = sw_if_index1;
197 pkts_encapsulated += 2;
199 len0 = vlib_buffer_length_in_chain(vm, b0);
200 len1 = vlib_buffer_length_in_chain(vm, b0);
201 stats_n_packets += 2;
202 stats_n_bytes += len0 + len1;
204 /* Batch stats increment on the same vxlan tunnel so counter is not
205 incremented per packet. Note stats are still incremented for deleted
206 and admin-down tunnel where packets are dropped. It is not worthwhile
207 to check for this rare case and affect normal path performance. */
209 (sw_if_index0 != stats_sw_if_index)
210 || (sw_if_index1 != stats_sw_if_index))) {
211 stats_n_packets -= 2;
212 stats_n_bytes -= len0 + len1;
213 if (sw_if_index0 == sw_if_index1) {
215 vlib_increment_combined_counter(
216 im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
217 cpu_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
218 stats_sw_if_index = sw_if_index0;
220 stats_n_bytes = len0 + len1;
222 vlib_increment_combined_counter(
223 im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
224 cpu_index, sw_if_index0, 1, len0);
225 vlib_increment_combined_counter(
226 im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
227 cpu_index, sw_if_index1, 1, len1);
231 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
233 nsh_gre_encap_trace_t *tr =
234 vlib_add_trace (vm, node, b0, sizeof (*tr));
235 tr->tunnel_index = t0 - ngm->tunnels;
238 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
240 nsh_gre_encap_trace_t *tr =
241 vlib_add_trace (vm, node, b1, sizeof (*tr));
242 tr->tunnel_index = t1 - ngm->tunnels;
245 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
246 to_next, n_left_to_next,
247 bi0, bi1, next0, next1);
250 while (n_left_from > 0 && n_left_to_next > 0)
254 u32 next0 = NSH_GRE_ENCAP_NEXT_IP4_LOOKUP;
255 u32 sw_if_index0, len0;
256 vnet_hw_interface_t * hi0;
258 u64 * copy_src0, * copy_dst0;
259 nsh_gre_tunnel_t * t0;
270 b0 = vlib_get_buffer (vm, bi0);
273 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_TX];
274 hi0 = vnet_get_sup_hw_interface
275 (vnm, vnet_buffer(b0)->sw_if_index[VLIB_TX]);
277 t0 = pool_elt_at_index (ngm->tunnels, hi0->dev_instance);
279 ASSERT(vec_len(t0->rewrite) >= 24);
281 /* Apply the rewrite string. $$$$ vnet_rewrite? */
282 vlib_buffer_advance (b0, -(word)_vec_len(t0->rewrite));
284 ip0 = vlib_buffer_get_current(b0);
285 /* Copy the fixed header */
286 copy_dst0 = (u64 *) ip0;
287 copy_src0 = (u64 *) t0->rewrite;
288 copy_dst0[0] = copy_src0[0];
289 copy_dst0[1] = copy_src0[1];
290 copy_dst0[2] = copy_src0[2];
292 /* If there are TLVs to copy, do so */
293 if (PREDICT_FALSE (_vec_len(t0->rewrite) > 24))
294 clib_memcpy (©_dst0[3], t0->rewrite + 24 ,
295 _vec_len (t0->rewrite)-24);
297 /* fix the <bleep>ing outer-IP checksum */
298 sum0 = ip0->checksum;
299 /* old_l0 always 0, see the rewrite setup */
301 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
303 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
304 length /* changed member */);
305 ip0->checksum = ip_csum_fold (sum0);
306 ip0->length = new_l0;
308 /* Reset to look up tunnel partner in the configured FIB */
309 vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->encap_fib_index;
310 vnet_buffer(b0)->sw_if_index[VLIB_RX] = sw_if_index0;
311 pkts_encapsulated ++;
313 len0 = vlib_buffer_length_in_chain(vm, b0);
314 stats_n_packets += 1;
315 stats_n_bytes += len0;
317 /* Batch stats increment on the same vxlan tunnel so counter is not
318 incremented per packet. Note stats are still incremented for deleted
319 and admin-down tunnel where packets are dropped. It is not worthwhile
320 to check for this rare case and affect normal path performance. */
321 if (PREDICT_FALSE(sw_if_index0 != stats_sw_if_index)) {
322 stats_n_packets -= 1;
323 stats_n_bytes -= len0;
325 vlib_increment_combined_counter(
326 im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
327 cpu_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
329 stats_n_bytes = len0;
330 stats_sw_if_index = sw_if_index0;
332 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
334 nsh_gre_encap_trace_t *tr =
335 vlib_add_trace (vm, node, b0, sizeof (*tr));
336 tr->tunnel_index = t0 - ngm->tunnels;
338 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
339 to_next, n_left_to_next,
343 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
345 vlib_node_increment_counter (vm, node->node_index,
346 NSH_GRE_ENCAP_ERROR_ENCAPSULATED,
348 /* Increment any remaining batch stats */
349 if (stats_n_packets) {
350 vlib_increment_combined_counter(
351 im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX, cpu_index,
352 stats_sw_if_index, stats_n_packets, stats_n_bytes);
353 node->runtime_data[0] = stats_sw_if_index;
356 return from_frame->n_vectors;
359 VLIB_REGISTER_NODE (nsh_gre_encap_node) = {
360 .function = nsh_gre_encap,
361 .name = "nsh-gre-encap",
362 .vector_size = sizeof (u32),
363 .format_trace = format_nsh_gre_encap_trace,
364 .type = VLIB_NODE_TYPE_INTERNAL,
366 .n_errors = ARRAY_LEN(nsh_gre_encap_error_strings),
367 .error_strings = nsh_gre_encap_error_strings,
369 .n_next_nodes = NSH_GRE_ENCAP_N_NEXT,
371 // add dispositions here
373 [NSH_GRE_ENCAP_NEXT_IP4_LOOKUP] = "ip4-lookup",
374 [NSH_GRE_ENCAP_NEXT_DROP] = "error-drop",