2 * decap.c: lisp-gpe decap processing
4 * Copyright (c) 2014 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include <vlib/vlib.h>
19 #include <vnet/pg/pg.h>
20 #include <vnet/lisp-gpe/lisp_gpe.h>
27 } lisp_gpe_rx_trace_t;
29 static u8 * format_lisp_gpe_rx_trace (u8 * s, va_list * args)
31 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
32 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
33 lisp_gpe_rx_trace_t * t = va_arg (*args, lisp_gpe_rx_trace_t *);
35 if (t->tunnel_index != ~0)
37 s = format (s, "NSH-VXLAN: tunnel %d next %d error %d", t->tunnel_index,
38 t->next_index, t->error);
42 s = format (s, "NSH-VXLAN: no tunnel next %d error %d\n", t->next_index,
45 s = format (s, "\n %U", format_lisp_gpe_header_with_length, &t->h,
46 (u32) sizeof (t->h) /* max size */);
51 lisp_gpe_input (vlib_main_t * vm,
52 vlib_node_runtime_t * node,
53 vlib_frame_t * from_frame)
55 u32 n_left_from, next_index, * from, * to_next;
56 lisp_gpe_main_t * ngm = &lisp_gpe_main;
57 u32 last_tunnel_index = ~0;
58 lisp_gpe_tunnel_key_t last_key;
59 u32 pkts_decapsulated = 0;
61 memset (&last_key, 0xff, sizeof (last_key));
63 from = vlib_frame_vector_args (from_frame);
64 n_left_from = from_frame->n_vectors;
66 next_index = node->cached_next_index;
68 while (n_left_from > 0)
72 vlib_get_next_frame (vm, node, next_index,
73 to_next, n_left_to_next);
76 while (n_left_from >= 4 && n_left_to_next >= 2)
79 vlib_buffer_t * b0, * b1;
80 nsh_unicast_header_t * h0, * h1;
85 /* Prefetch next iteration. */
87 vlib_buffer_t * p2, * p3;
89 p2 = vlib_get_buffer (vm, from[2]);
90 p3 = vlib_get_buffer (vm, from[3]);
92 vlib_prefetch_buffer_header (p2, LOAD);
93 vlib_prefetch_buffer_header (p3, LOAD);
95 CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
96 CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
108 b0 = vlib_get_buffer (vm, bi0);
109 b1 = vlib_get_buffer (vm, bi1);
111 h0 = vlib_buffer_get_current (b0);
112 h1 = vlib_buffer_get_current (b1);
114 next0 = next1 = LISP_GPE_INPUT_NEXT_IP4_INPUT;
116 label0 = clib_net_to_host_u32 (h0->label_exp_s_ttl);
117 label1 = clib_net_to_host_u32 (h1->label_exp_s_ttl);
120 * Translate label contents into a fib index.
121 * This is a decent sanity check, and guarantees
122 * a sane FIB for the downstream lookup
124 label0 = vnet_nsh_uc_get_label (label0);
125 label1 = vnet_nsh_uc_get_label (label1);
127 /* If 2xlabels match, and match the 1-wide cache, use it */
128 if (label0 == label1 && rt->last_label == label0)
130 vnet_buffer(b0)->sw_if_index[VLIB_TX] = rt->last_fib_index;
131 vnet_buffer(b1)->sw_if_index[VLIB_TX] = rt->last_fib_index;
135 p0 = hash_get (rt->mm->fib_index_by_nsh_label, label0);
136 if (PREDICT_FALSE (p0 == 0))
138 next0 = LISP_GPE_INPUT_NEXT_DROP;
139 b0->error = node->errors[NSH_ERROR_BAD_LABEL];
142 vnet_buffer(b0)->sw_if_index[VLIB_TX] = p0[0];
144 p1 = hash_get (rt->mm->fib_index_by_nsh_label, label1);
145 if (PREDICT_FALSE (p1 == 0))
147 next1 = LISP_GPE_INPUT_NEXT_DROP;
148 b1->error = node->errors[NSH_ERROR_BAD_LABEL];
152 vnet_buffer(b1)->sw_if_index[VLIB_TX] = p1[0];
153 rt->last_fib_index = p1[0];
154 rt->last_label = label1;
158 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
160 nsh_rx_trace_t *tr = vlib_add_trace (vm, node,
162 tr->label_exp_s_ttl = label0;
164 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
166 nsh_rx_trace_t *tr = vlib_add_trace (vm, node,
168 tr->label_exp_s_ttl = label1;
171 vlib_buffer_advance (b0, sizeof (*h0));
172 vlib_buffer_advance (b1, sizeof (*h1));
174 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
175 to_next, n_left_to_next,
176 bi0, bi1, next0, next1);
180 while (n_left_from > 0 && n_left_to_next > 0)
185 ip4_udp_lisp_gpe_header_t * iul0;
188 lisp_gpe_tunnel_t * t0;
189 lisp_gpe_tunnel_key_t key0;
199 b0 = vlib_get_buffer (vm, bi0);
201 /* udp leaves current_data pointing at the lisp header */
203 (b0, -(word)(sizeof(udp_header_t)+sizeof(ip4_header_t)));
205 iul0 = vlib_buffer_get_current (b0);
207 /* pop (ip, udp, lisp-gpe) */
208 vlib_buffer_advance (b0, sizeof (*iul0));
212 next0 = LISP_GPE_INPUT_NEXT_DROP;
214 key0.src = iul0->ip4.src_address.as_u32;
215 key0.iid = iul0->lisp.iid;
217 if (PREDICT_FALSE ((key0.as_u64[0] != last_key.as_u64[0])))
219 p0 = hash_get_mem (ngm->lisp_gpe_tunnel_by_key, &key0);
223 error0 = LISP_GPE_ERROR_NO_SUCH_TUNNEL;
227 last_key.as_u64[0] = key0.as_u64[0];
228 tunnel_index0 = last_tunnel_index = p0[0];
231 tunnel_index0 = last_tunnel_index;
233 t0 = pool_elt_at_index (ngm->tunnels, tunnel_index0);
235 next0 = t0->decap_next_index;
237 /* Required to make the l2 tag push / pop code work on l2 subifs */
238 vnet_update_l2_len (b0);
241 * ip[46] lookup in the configured FIB
242 * lisp-gpe-encap, here's the encap tunnel sw_if_index
244 vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->decap_fib_index;
245 pkts_decapsulated ++;
248 b0->error = error0 ? node->errors[error0] : 0;
250 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
252 lisp_gpe_rx_trace_t *tr
253 = vlib_add_trace (vm, node, b0, sizeof (*tr));
254 tr->next_index = next0;
256 tr->tunnel_index = tunnel_index0;
259 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
260 to_next, n_left_to_next,
264 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
266 vlib_node_increment_counter (vm, lisp_gpe_input_node.index,
267 LISP_GPE_ERROR_DECAPSULATED,
269 return from_frame->n_vectors;
272 static char * lisp_gpe_error_strings[] = {
273 #define lisp_gpe_error(n,s) s,
274 #include <vnet/lisp-gpe/lisp_gpe_error.def>
275 #undef lisp_gpe_error
279 VLIB_REGISTER_NODE (lisp_gpe_input_node) = {
280 .function = lisp_gpe_input,
281 .name = "lisp-gpe-input",
282 /* Takes a vector of packets. */
283 .vector_size = sizeof (u32),
285 .n_errors = LISP_GPE_N_ERROR,
286 .error_strings = lisp_gpe_error_strings,
288 .n_next_nodes = LISP_GPE_INPUT_N_NEXT,
290 #define _(s,n) [LISP_GPE_INPUT_NEXT_##s] = n,
291 foreach_lisp_gpe_input_next
295 .format_buffer = format_lisp_gpe_header_with_length,
296 .format_trace = format_lisp_gpe_rx_trace,
297 // $$$$ .unformat_buffer = unformat_lisp_gpe_header,