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