2 * Copyright (c) 2016 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.
16 #include <vppinfra/error.h>
17 #include <vppinfra/hash.h>
18 #include <vnet/vnet.h>
19 #include <vnet/ip/ip.h>
20 #include <vnet/ip/udp.h>
21 #include <vnet/ethernet/ethernet.h>
22 #include <vnet/lisp-gpe/lisp_gpe.h>
24 /* Statistics (not really errors) */
25 #define foreach_lisp_gpe_encap_error \
26 _(ENCAPSULATED, "good packets encapsulated")
28 static char * lisp_gpe_encap_error_strings[] = {
29 #define _(sym,string) string,
30 foreach_lisp_gpe_encap_error
35 #define _(sym,str) LISP_GPE_ENCAP_ERROR_##sym,
36 foreach_lisp_gpe_encap_error
38 LISP_GPE_ENCAP_N_ERROR,
39 } lisp_gpe_encap_error_t;
43 LISP_GPE_ENCAP_NEXT_DROP,
44 LISP_GPE_ENCAP_NEXT_IP4_LOOKUP,
45 LISP_GPE_ENCAP_N_NEXT,
46 } lisp_gpe_encap_next_t;
51 } lisp_gpe_encap_trace_t;
54 format_lisp_gpe_encap_trace (u8 * s, va_list * args)
56 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
57 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
58 lisp_gpe_encap_trace_t * t = va_arg (*args, lisp_gpe_encap_trace_t *);
60 s = format (s, "LISP-GPE-ENCAP: tunnel %d", t->tunnel_index);
65 lisp_gpe_encap (vlib_main_t * vm, vlib_node_runtime_t * node,
66 vlib_frame_t * from_frame)
68 u32 n_left_from, next_index, * from, * to_next;
69 lisp_gpe_main_t * lgm = &lisp_gpe_main;
70 u32 pkts_encapsulated = 0;
72 from = vlib_frame_vector_args (from_frame);
73 n_left_from = from_frame->n_vectors;
75 next_index = node->cached_next_index;
77 while (n_left_from > 0)
81 vlib_get_next_frame (vm, node, next_index,
82 to_next, n_left_to_next);
84 while (n_left_from >= 4 && n_left_to_next >= 2)
87 vlib_buffer_t * b0, * b1;
89 u32 adj_index0, adj_index1, tunnel_index0, tunnel_index1;
90 ip_adjacency_t * adj0, * adj1;
91 lisp_gpe_tunnel_t * t0, * t1;
93 next0 = next1 = LISP_GPE_ENCAP_NEXT_IP4_LOOKUP;
95 /* Prefetch next iteration. */
97 vlib_buffer_t * p2, *p3;
99 p2 = vlib_get_buffer (vm, from[2]);
100 p3 = vlib_get_buffer (vm, from[3]);
102 vlib_prefetch_buffer_header(p2, LOAD);
103 vlib_prefetch_buffer_header(p3, LOAD);
105 CLIB_PREFETCH(p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
106 CLIB_PREFETCH(p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
118 b0 = vlib_get_buffer (vm, bi0);
119 b1 = vlib_get_buffer (vm, bi1);
121 /* Get adjacency and from it the tunnel_index */
122 adj_index0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
123 adj_index1 = vnet_buffer(b1)->ip.adj_index[VLIB_TX];
125 adj0 = ip_get_adjacency (lgm->lookup_main, adj_index0);
126 adj1 = ip_get_adjacency (lgm->lookup_main, adj_index1);
128 tunnel_index0 = adj0->rewrite_header.sw_if_index;
129 tunnel_index1 = adj1->rewrite_header.sw_if_index;
131 t0 = pool_elt_at_index (lgm->tunnels, tunnel_index0);
132 t1 = pool_elt_at_index (lgm->tunnels, tunnel_index1);
137 ASSERT (sizeof(ip4_udp_lisp_gpe_header_t) == 36);
138 ip4_udp_encap_two (vm, b0, b1, t0->rewrite, t1->rewrite, 36);
140 /* Reset to look up tunnel partner in the configured FIB */
141 vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->encap_fib_index;
142 vnet_buffer(b1)->sw_if_index[VLIB_TX] = t1->encap_fib_index;
144 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
146 lisp_gpe_encap_trace_t *tr = vlib_add_trace (vm, node, b0,
148 tr->tunnel_index = t0 - lgm->tunnels;
150 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
152 lisp_gpe_encap_trace_t *tr = vlib_add_trace (vm, node, b1,
154 tr->tunnel_index = t1 - lgm->tunnels;
157 pkts_encapsulated += 2;
159 vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next,
160 n_left_to_next, bi0, bi1, next0,
164 while (n_left_from > 0 && n_left_to_next > 0)
167 u32 bi0, adj_index0, tunnel_index0;
168 u32 next0 = LISP_GPE_ENCAP_NEXT_IP4_LOOKUP;
169 lisp_gpe_tunnel_t * t0 = 0;
170 ip_adjacency_t * adj0;
179 b0 = vlib_get_buffer (vm, bi0);
181 /* Get adjacency and from it the tunnel_index */
182 adj_index0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
183 adj0 = ip_get_adjacency (lgm->lookup_main, adj_index0);
185 tunnel_index0 = adj0->rewrite_header.sw_if_index;
186 t0 = pool_elt_at_index (lgm->tunnels, tunnel_index0);
190 ASSERT (sizeof(ip4_udp_lisp_gpe_header_t) == 36);
191 ip4_udp_encap_one (vm, b0, t0->rewrite, 36);
193 /* Reset to look up tunnel partner in the configured FIB */
194 vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->encap_fib_index;
198 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
200 lisp_gpe_encap_trace_t *tr = vlib_add_trace (vm, node, b0,
202 tr->tunnel_index = t0 - lgm->tunnels;
204 vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
205 n_left_to_next, bi0, next0);
208 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
210 vlib_node_increment_counter (vm, node->node_index,
211 LISP_GPE_ENCAP_ERROR_ENCAPSULATED,
213 return from_frame->n_vectors;
216 VLIB_REGISTER_NODE (lisp_gpe_encap_node) = {
217 .function = lisp_gpe_encap,
218 .name = "lisp-gpe-encap",
219 .vector_size = sizeof (u32),
220 .format_trace = format_lisp_gpe_encap_trace,
221 .type = VLIB_NODE_TYPE_INTERNAL,
223 .n_errors = ARRAY_LEN(lisp_gpe_encap_error_strings),
224 .error_strings = lisp_gpe_encap_error_strings,
226 .n_next_nodes = LISP_GPE_ENCAP_N_NEXT,
229 [LISP_GPE_ENCAP_NEXT_DROP] = "error-drop",
230 [LISP_GPE_ENCAP_NEXT_IP4_LOOKUP] = "ip4-lookup",