blob: a9a81213c0f844b75a1f42ab26edc0c564049538 [file] [log] [blame]
Dave Barach8d0f2f02018-03-12 09:31:36 -04001;;; Copyright (c) 2016 Cisco and/or its affiliates.
2;;; Licensed under the Apache License, Version 2.0 (the "License");
3;;; you may not use this file except in compliance with the License.
4;;; You may obtain a copy of the License at:
5;;;
6;;; http://www.apache.org/licenses/LICENSE-2.0
7;;;
8;;; Unless required by applicable law or agreed to in writing, software
9;;; distributed under the License is distributed on an "AS IS" BASIS,
10;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11;;; See the License for the specific language governing permissions and
12;;; limitations under the License.
13
Ed Warnickecb9cada2015-12-08 15:45:58 -070014;;; tunnel-decap-skel.el - tunnel decapsulation skeleton
15
16(require 'skeleton)
17
Keith Burns (alagalah)ca46d8c2016-03-18 07:22:15 -070018(define-skeleton skel-tunnel-decap
Ed Warnickecb9cada2015-12-08 15:45:58 -070019"Insert a tunnel decap implementation"
20nil
21'(setq encap_stack (skeleton-read "encap_stack (e.g ip4_udp_lisp): "))
22'(setq ENCAP_STACK (upcase encap_stack))
23'(setq encap-stack (replace-regexp-in-string "_" "-" encap_stack))
24'(setq ENCAP-STACK (upcase encap-stack))
25"
26#include <vlib/vlib.h>
27#include <vnet/pg/pg.h>
28#include <vnet/" encap-stack "/" encap_stack ".h>
29
30typedef struct {
31 u32 next_index;
32 u32 tunnel_index;
33 u32 error;
34 " encap_stack "_header_t h;
35} " encap_stack "_rx_trace_t;
36
37static u8 * format_" encap_stack "_rx_trace (u8 * s, va_list * args)
38{
39 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
40 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
41 " encap_stack "_rx_trace_t * t = va_arg (*args, " encap_stack "_rx_trace_t *);
42
43 if (t->tunnel_index != ~0)
44 {
45 s = format (s, \"" ENCAP-STACK ": tunnel %d next %d error %d\",
46 t->tunnel_index, t->next_index, t->error);
47 }
48 else
49 {
50 s = format (s, \"" ENCAP-STACK ": no tunnel next %d error %d\\n\",
51 t->next_index, t->error);
52 }
53 s = format (s, \"\\n %U\", format_" encap_stack "_header_with_length, &t->h,
54 (u32) sizeof (t->h) /* max size */);
55 return s;
56}
57
58static uword
59" encap_stack "_input (vlib_main_t * vm,
60 vlib_node_runtime_t * node,
61 vlib_frame_t * from_frame)
62{
63 u32 n_left_from, next_index, * from, * to_next;
64 " encap_stack "_main_t * ngm = &" encap_stack "_main;
65 u32 last_tunnel_index = ~0;
66 " encap_stack "_tunnel_key_t last_key;
67 u32 pkts_decapsulated = 0;
68
69 memset (&last_key, 0xff, sizeof (last_key));
70
71 from = vlib_frame_vector_args (from_frame);
72 n_left_from = from_frame->n_vectors;
73
74 next_index = node->cached_next_index;
75
76 while (n_left_from > 0)
77 {
78 u32 n_left_to_next;
79
80 vlib_get_next_frame (vm, node, next_index,
81 to_next, n_left_to_next);
82
83#if 0 /* $$$ dual loop when the single loop works */
84 while (n_left_from >= 4 && n_left_to_next >= 2)
85 {
86 u32 bi0, bi1;
87 vlib_buffer_t * b0, * b1;
88 nsh_unicast_header_t * h0, * h1;
89 u32 label0, label1;
90 u32 next0, next1;
91 uword * p0, * p1;
92
93 /* Prefetch next iteration. */
94 {
95 vlib_buffer_t * p2, * p3;
96
97 p2 = vlib_get_buffer (vm, from[2]);
98 p3 = vlib_get_buffer (vm, from[3]);
99
100 vlib_prefetch_buffer_header (p2, LOAD);
101 vlib_prefetch_buffer_header (p3, LOAD);
102
103 CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
104 CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
105 }
106
107 bi0 = from[0];
108 bi1 = from[1];
109 to_next[0] = bi0;
110 to_next[1] = bi1;
111 from += 2;
112 to_next += 2;
113 n_left_to_next -= 2;
114 n_left_from -= 2;
115
116 b0 = vlib_get_buffer (vm, bi0);
117 b1 = vlib_get_buffer (vm, bi1);
118
119 h0 = vlib_buffer_get_current (b0);
120 h1 = vlib_buffer_get_current (b1);
121
122 next0 = next1 = " ENCAP_STACK "_INPUT_NEXT_IP4_INPUT;
123
124 label0 = clib_net_to_host_u32 (h0->label_exp_s_ttl);
125 label1 = clib_net_to_host_u32 (h1->label_exp_s_ttl);
126
127 /*
128 * Translate label contents into a fib index.
129 * This is a decent sanity check, and guarantees
130 * a sane FIB for the downstream lookup
131 */
132 label0 = vnet_nsh_uc_get_label (label0);
133 label1 = vnet_nsh_uc_get_label (label1);
134
135 /* If 2xlabels match, and match the 1-wide cache, use it */
136 if (label0 == label1 && rt->last_label == label0)
137 {
138 vnet_buffer(b0)->sw_if_index[VLIB_TX] = rt->last_fib_index;
139 vnet_buffer(b1)->sw_if_index[VLIB_TX] = rt->last_fib_index;
140 }
141 else
142 {
143 p0 = hash_get (rt->mm->fib_index_by_nsh_label, label0);
144 if (PREDICT_FALSE (p0 == 0))
145 {
146 next0 = " ENCAP_STACK "_INPUT_NEXT_DROP;
147 b0->error = node->errors[NSH_ERROR_BAD_LABEL];
148 }
149 else
150 vnet_buffer(b0)->sw_if_index[VLIB_TX] = p0[0];
151
152 p1 = hash_get (rt->mm->fib_index_by_nsh_label, label1);
153 if (PREDICT_FALSE (p1 == 0))
154 {
155 next1 = " ENCAP_STACK "_INPUT_NEXT_DROP;
156 b1->error = node->errors[NSH_ERROR_BAD_LABEL];
157 }
158 else
159 {
160 vnet_buffer(b1)->sw_if_index[VLIB_TX] = p1[0];
161 rt->last_fib_index = p1[0];
162 rt->last_label = label1;
163 }
164 }
165
166 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
167 {
168 nsh_rx_trace_t *tr = vlib_add_trace (vm, node,
169 b0, sizeof (*tr));
170 tr->label_exp_s_ttl = label0;
171 }
172 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
173 {
174 nsh_rx_trace_t *tr = vlib_add_trace (vm, node,
175 b1, sizeof (*tr));
176 tr->label_exp_s_ttl = label1;
177 }
178
179 vlib_buffer_advance (b0, sizeof (*h0));
180 vlib_buffer_advance (b1, sizeof (*h1));
181
182 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
183 to_next, n_left_to_next,
184 bi0, bi1, next0, next1);
185 }
186#endif
187
188 while (n_left_from > 0 && n_left_to_next > 0)
189 {
190 u32 bi0;
191 vlib_buffer_t * b0;
192 u32 next0;
193 " encap_stack "_header_t * iuX0;
194 uword * p0;
195 u32 tunnel_index0;
196 " encap_stack "_tunnel_t * t0;
197 " encap_stack "_tunnel_key_t key0;
198 u32 error0;
199
200 bi0 = from[0];
201 to_next[0] = bi0;
202 from += 1;
203 to_next += 1;
204 n_left_from -= 1;
205 n_left_to_next -= 1;
206
207 b0 = vlib_get_buffer (vm, bi0);
208
209 /*
210 * udp leaves current_data pointing at the tunnel header
211 * $$$$ FIXME
212 */
213 vlib_buffer_advance
214 (b0, -(word)(sizeof(udp_header_t)+sizeof(ip4_header_t)));
215
216 iuX0 = vlib_buffer_get_current (b0);
217
218 /* pop (ip, udp, lisp-gpe) */
219 vlib_buffer_advance (b0, sizeof (*iuX0));
220
221 tunnel_index0 = ~0;
222 error0 = 0;
223 next0 = " ENCAP_STACK "_INPUT_NEXT_DROP;
224
225 key0.src = iuX0->ip4.src_address.as_u32;
226 key0.iid = iuX0->lisp.iid;
227
228 /* $$$ validate key comparison */
229 if (PREDICT_FALSE ((key0.as_u64[0] != last_key.as_u64[0])))
230 {
231 p0 = hash_get_mem (ngm->" encap_stack "_tunnel_by_key, &key0);
232
233 if (p0 == 0)
234 {
235 error0 = " ENCAP_STACK "_ERROR_NO_SUCH_TUNNEL;
236 goto trace0;
237 }
238
239 last_key.as_u64[0] = key0.as_u64[0];
240 tunnel_index0 = last_tunnel_index = p0[0];
241 }
242 else
243 tunnel_index0 = last_tunnel_index;
244
245 t0 = pool_elt_at_index (ngm->tunnels, tunnel_index0);
246
247 next0 = t0->decap_next_index;
248
249 /* Required to make the l2 tag push / pop code work on l2 subifs */
250 vnet_update_l2_len (b0);
251
252 /*
253 * ip[46] lookup in the configured FIB
254 * " encap-stack ", here's the encap tunnel sw_if_index
255 */
256 vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->decap_fib_index;
257 pkts_decapsulated ++;
258
259 trace0:
260 b0->error = error0 ? node->errors[error0] : 0;
261
262 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
263 {
264 " encap_stack "_rx_trace_t *tr
265 = vlib_add_trace (vm, node, b0, sizeof (*tr));
266 tr->next_index = next0;
267 tr->error = error0;
268 tr->tunnel_index = tunnel_index0;
269 tr->h = iuX0->lisp;
270 }
271 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
272 to_next, n_left_to_next,
273 bi0, next0);
274 }
275
276 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
277 }
278 vlib_node_increment_counter (vm, " encap_stack "_input_node.index,
279 " ENCAP_STACK "_ERROR_DECAPSULATED,
280 pkts_decapsulated);
281 return from_frame->n_vectors;
282}
283
284static char * " encap_stack "_error_strings[] = {
285#define " encap_stack "_error(n,s) s,
286#include <vnet/" encap-stack "/" encap_stack "_error.def>
287#undef " encap_stack "_error
288#undef _
289};
290
291VLIB_REGISTER_NODE (" encap_stack "_input_node) = {
292 .function = \"" encap_stack "_input\",
293 .name = \"" encap-stack "-input\",
294 /* Takes a vector of packets. */
295 .vector_size = sizeof (u32),
296
297 .n_errors = " ENCAP_STACK "_N_ERROR,
298 .error_strings = " encap_stack "_error_strings,
299
300 .n_next_nodes = " ENCAP_STACK "_INPUT_N_NEXT,
301 .next_nodes = {
302#define _(s,n) [" ENCAP_STACK "_INPUT_NEXT_##s] = n,
303 foreach_" encap_stack "_input_next
304#undef _
305 },
306
307 .format_buffer = format_" encap_stack "_header_with_length,
308 .format_trace = format_" encap_stack "_rx_trace,
309 // $$$$ .unformat_buffer = unformat_" encap_stack "_header,
310};
311
312")