A Protocol Independent Hierarchical FIB (VPP-352)
[vpp.git] / plugins / ila-plugin / ila / ila.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include <ila/ila.h>
17 #include <vnet/plugin/plugin.h>
18 #include <vnet/ip/lookup.h>
19 #include <vnet/dpo/dpo.h>
20 #include <vnet/fib/fib_table.h>
21
22 static ila_main_t ila_main;
23
24 #define ILA_TABLE_DEFAULT_HASH_NUM_BUCKETS (64 * 1024)
25 #define ILA_TABLE_DEFAULT_HASH_MEMORY_SIZE (32<<20)
26
27 #define foreach_ila_error \
28  _(NONE, "valid ILA packets")
29
30 typedef enum {
31 #define _(sym,str) ILA_ERROR_##sym,
32   foreach_ila_error
33 #undef _
34     ILA_N_ERROR,
35 } ila_error_t;
36
37 static char *ila_error_strings[] = {
38 #define _(sym,string) string,
39   foreach_ila_error
40 #undef _
41 };
42
43 typedef enum {
44   ILA_ILA2SIR_NEXT_DROP,
45   ILA_ILA2SIR_N_NEXT,
46 } ila_ila2sir_next_t;
47
48 typedef struct {
49   u32 ila_index;
50   ip6_address_t initial_dst;
51   u32 adj_index;
52 } ila_ila2sir_trace_t;
53
54 static ila_entry_t ila_sir2ila_default_entry = {
55   .csum_mode = ILA_CSUM_MODE_NO_ACTION,
56   .type = ILA_TYPE_IID,
57   .dir = ILA_DIR_ILA2SIR, //Will pass the packet with no
58 };
59
60 /**
61  * @brief Dynamically registered DPO Type for ILA
62  */
63 static dpo_type_t ila_dpo_type;
64
65 /**
66  * @brief Dynamically registered FIB node type for ILA
67  */
68 static fib_node_type_t ila_fib_node_type;
69
70 u8 *
71 format_half_ip6_address (u8 * s, va_list * va)
72 {
73   u64 v = clib_net_to_host_u64 (va_arg (*va, u64));
74
75   return format (s, "%04x:%04x:%04x:%04x",
76                  v >> 48, (v >> 32) & 0xffff, (v >> 16) & 0xffff, v & 0xffff);
77
78 }
79
80 u8 *
81 format_ila_direction (u8 * s, va_list * args)
82 {
83   ila_direction_t t = va_arg (*args, ila_direction_t);
84 #define _(i,n,st) \
85   if (t == ILA_DIR_##i) \
86     return format(s, st);
87   ila_foreach_direction
88 #undef _
89     return format (s, "invalid_ila_direction");
90 }
91
92 static u8 *
93 format_csum_mode (u8 * s, va_list * va)
94 {
95   ila_csum_mode_t csum_mode = va_arg (*va, ila_csum_mode_t);
96   char *txt;
97
98   switch (csum_mode)
99     {
100 #define _(i,n,st) \
101   case ILA_CSUM_MODE_##i: \
102     txt = st; \
103     break;
104       ila_csum_foreach_type
105 #undef _
106     default:
107       txt = "invalid_ila_csum_mode";
108       break;
109     }
110   return format (s, txt);
111 }
112
113 u8 *
114 format_ila_type (u8 * s, va_list * args)
115 {
116   ila_type_t t = va_arg (*args, ila_type_t);
117 #define _(i,n,st) \
118   if (t == ILA_TYPE_##i) \
119     return format(s, st);
120   ila_foreach_type
121 #undef _
122     return format (s, "invalid_ila_type");
123 }
124
125 static u8 *
126 format_ila_entry (u8 * s, va_list * va)
127 {
128   vnet_main_t *vnm = va_arg (*va, vnet_main_t *);
129   ila_entry_t *e = va_arg (*va, ila_entry_t *);
130
131   if (!e)
132     {
133       return format (s, "%-15s%=40s%=40s%+16s%+18s%+11s", "Type", "SIR Address",
134                      "ILA Address", "Checksum Mode", "Direction", "Next DPO");
135     }
136   else if (vnm)
137     {
138       if (ip6_address_is_zero(&e->next_hop))
139         {
140           return format (s, "%-15U%=40U%=40U%18U%11U%s",
141                          format_ila_type, e->type,
142                          format_ip6_address, &e->sir_address,
143                          format_ip6_address, &e->ila_address,
144                          format_csum_mode, e->csum_mode,
145                          format_ila_direction, e->dir,
146                          "n/a");
147         }
148       else
149         {
150           return format (s, "%-15U%=40U%=40U%18U%11U%U",
151                          format_ila_type, e->type,
152                          format_ip6_address, &e->sir_address,
153                          format_ip6_address, &e->ila_address,
154                          format_csum_mode, e->csum_mode,
155                          format_ila_direction, e->dir,
156                          format_dpo_id, &e->ila_dpo, 0);
157         }
158     }
159
160   return NULL;
161 }
162
163 u8 *
164 format_ila_ila2sir_trace (u8 * s, va_list * args)
165 {
166   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
167   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
168   ila_ila2sir_trace_t *t = va_arg (*args, ila_ila2sir_trace_t *);
169   return format (s,
170                  "ILA -> SIR adj index: %d entry index: %d initial_dst: %U",
171                  t->adj_index, t->ila_index, format_ip6_address,
172                  &t->initial_dst);
173 }
174
175 static uword
176 unformat_ila_direction (unformat_input_t * input, va_list * args)
177 {
178   ila_direction_t *result = va_arg (*args, ila_direction_t *);
179 #define _(i,n,s) \
180   if (unformat(input, s)) \
181       { \
182         *result = ILA_DIR_##i; \
183         return 1;\
184       }
185
186   ila_foreach_direction
187 #undef _
188     return 0;
189 }
190
191 static uword
192 unformat_ila_type (unformat_input_t * input, va_list * args)
193 {
194   ila_type_t *result = va_arg (*args, ila_type_t *);
195 #define _(i,n,s) \
196   if (unformat(input, s)) \
197       { \
198         *result = ILA_TYPE_##i; \
199         return 1;\
200       }
201
202   ila_foreach_type
203 #undef _
204     return 0;
205 }
206
207 static uword
208 unformat_ila_csum_mode (unformat_input_t * input, va_list * args)
209 {
210   ila_csum_mode_t *result = va_arg (*args, ila_csum_mode_t *);
211   if (unformat (input, "none") || unformat (input, "no-action"))
212     {
213       *result = ILA_CSUM_MODE_NO_ACTION;
214       return 1;
215     }
216   if (unformat (input, "neutral-map"))
217     {
218       *result = ILA_CSUM_MODE_NEUTRAL_MAP;
219       return 1;
220     }
221   if (unformat (input, "adjust-transport"))
222     {
223       *result = ILA_CSUM_MODE_ADJUST_TRANSPORT;
224       return 1;
225     }
226   return 0;
227 }
228
229 static uword
230 unformat_half_ip6_address (unformat_input_t * input, va_list * args)
231 {
232   u64 *result = va_arg (*args, u64 *);
233   u32 a[4];
234
235   if (!unformat (input, "%x:%x:%x:%x", &a[0], &a[1], &a[2], &a[3]))
236     return 0;
237
238   if (a[0] > 0xFFFF || a[1] > 0xFFFF || a[2] > 0xFFFF || a[3] > 0xFFFF)
239     return 0;
240
241   *result = clib_host_to_net_u64 ((((u64) a[0]) << 48) |
242                                   (((u64) a[1]) << 32) |
243                                   (((u64) a[2]) << 16) | (((u64) a[3])));
244
245   return 1;
246 }
247
248 static vlib_node_registration_t ila_ila2sir_node;
249
250 static uword
251 ila_ila2sir (vlib_main_t * vm,
252              vlib_node_runtime_t * node, vlib_frame_t * frame)
253 {
254   u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
255   ila_main_t *ilm = &ila_main;
256
257   from = vlib_frame_vector_args (frame);
258   n_left_from = frame->n_vectors;
259   next_index = node->cached_next_index;
260
261   while (n_left_from > 0)
262     {
263       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
264
265       while (n_left_from >= 4 && n_left_to_next >= 2)
266         {
267           u32 pi0, pi1;
268           vlib_buffer_t *p0, *p1;
269           ila_entry_t *ie0, *ie1;
270           ip6_header_t *ip60, *ip61;
271           ip6_address_t *sir_address0, *sir_address1;
272
273           {
274             vlib_buffer_t *p2, *p3;
275
276             p2 = vlib_get_buffer (vm, from[2]);
277             p3 = vlib_get_buffer (vm, from[3]);
278
279             vlib_prefetch_buffer_header (p2, LOAD);
280             vlib_prefetch_buffer_header (p3, LOAD);
281             CLIB_PREFETCH (p2->data, sizeof (ip6_header_t), LOAD);
282             CLIB_PREFETCH (p3->data, sizeof (ip6_header_t), LOAD);
283           }
284
285           pi0 = to_next[0] = from[0];
286           pi1 = to_next[1] = from[1];
287           from += 2;
288           n_left_from -= 2;
289           to_next += 2;
290           n_left_to_next -= 2;
291
292           p0 = vlib_get_buffer (vm, pi0);
293           p1 = vlib_get_buffer (vm, pi1);
294           ip60 = vlib_buffer_get_current (p0);
295           ip61 = vlib_buffer_get_current (p1);
296           sir_address0 = &ip60->dst_address;
297           sir_address1 = &ip61->dst_address;
298           ie0 = pool_elt_at_index (ilm->entries,
299                                    vnet_buffer (p0)->ip.adj_index[VLIB_TX]);
300           ie1 = pool_elt_at_index (ilm->entries,
301                                    vnet_buffer (p1)->ip.adj_index[VLIB_TX]);
302
303           if (PREDICT_FALSE (p0->flags & VLIB_BUFFER_IS_TRACED))
304             {
305               ila_ila2sir_trace_t *tr =
306                 vlib_add_trace (vm, node, p0, sizeof (*tr));
307               tr->ila_index = ie0 - ilm->entries;
308               tr->initial_dst = ip60->dst_address;
309               tr->adj_index = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
310             }
311
312           if (PREDICT_FALSE (p1->flags & VLIB_BUFFER_IS_TRACED))
313             {
314               ila_ila2sir_trace_t *tr =
315                 vlib_add_trace (vm, node, p1, sizeof (*tr));
316               tr->ila_index = ie1 - ilm->entries;
317               tr->initial_dst = ip61->dst_address;
318               tr->adj_index = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
319             }
320
321           sir_address0 = (ie0->dir != ILA_DIR_SIR2ILA) ? &ie0->sir_address : sir_address0;
322           sir_address1 = (ie1->dir != ILA_DIR_SIR2ILA) ? &ie1->sir_address : sir_address1;
323           ip60->dst_address.as_u64[0] = sir_address0->as_u64[0];
324           ip60->dst_address.as_u64[1] = sir_address0->as_u64[1];
325           ip61->dst_address.as_u64[0] = sir_address1->as_u64[0];
326           ip61->dst_address.as_u64[1] = sir_address1->as_u64[1];
327
328           vnet_buffer (p0)->ip.adj_index[VLIB_TX] = ie0->ila_dpo.dpoi_index;
329           vnet_buffer (p1)->ip.adj_index[VLIB_TX] = ie1->ila_dpo.dpoi_index;
330
331           vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
332                                            n_left_to_next, pi0, pi1,
333                                            ie0->ila_dpo.dpoi_next_node,
334                                            ie1->ila_dpo.dpoi_next_node);
335         }
336
337       /* Single loop */
338       while (n_left_from > 0 && n_left_to_next > 0)
339         {
340           u32 pi0;
341           vlib_buffer_t *p0;
342           ila_entry_t *ie0;
343           ip6_header_t *ip60;
344           ip6_address_t *sir_address0;
345
346           pi0 = to_next[0] = from[0];
347           from += 1;
348           n_left_from -= 1;
349           to_next += 1;
350           n_left_to_next -= 1;
351
352           p0 = vlib_get_buffer (vm, pi0);
353           ip60 = vlib_buffer_get_current (p0);
354           sir_address0 = &ip60->dst_address;
355           ie0 = pool_elt_at_index (ilm->entries,
356                                    vnet_buffer (p0)->ip.adj_index[VLIB_TX]);
357
358           if (PREDICT_FALSE (p0->flags & VLIB_BUFFER_IS_TRACED))
359             {
360               ila_ila2sir_trace_t *tr =
361                 vlib_add_trace (vm, node, p0, sizeof (*tr));
362               tr->ila_index = ie0 ? (ie0 - ilm->entries) : ~0;
363               tr->initial_dst = ip60->dst_address;
364               tr->adj_index = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
365             }
366
367           sir_address0 = (ie0->dir != ILA_DIR_SIR2ILA) ? &ie0->sir_address : sir_address0;
368           ip60->dst_address.as_u64[0] = sir_address0->as_u64[0];
369           ip60->dst_address.as_u64[1] = sir_address0->as_u64[1];
370           vnet_buffer (p0)->ip.adj_index[VLIB_TX] = ie0->ila_dpo.dpoi_index;
371
372           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
373                                            n_left_to_next, pi0,
374                                            ie0->ila_dpo.dpoi_next_node);
375         }
376       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
377     }
378
379   return frame->n_vectors;
380 }
381
382 /** *INDENT-OFF* */
383 VLIB_REGISTER_NODE (ila_ila2sir_node, static) =
384 {
385   .function = ila_ila2sir,
386   .name = "ila-to-sir",
387   .vector_size = sizeof (u32),
388   .format_trace = format_ila_ila2sir_trace,
389   .n_errors = ILA_N_ERROR,
390   .error_strings = ila_error_strings,
391   .n_next_nodes = ILA_ILA2SIR_N_NEXT,
392   .next_nodes =
393   {
394       [ILA_ILA2SIR_NEXT_DROP] = "error-drop"
395   },
396 };
397 /** *INDENT-ON* */
398
399 typedef enum
400 {
401   ILA_SIR2ILA_NEXT_DROP,
402   ILA_SIR2ILA_N_NEXT,
403 } ila_sir2ila_next_t;
404
405 typedef struct
406 {
407   u32 ila_index;
408   ip6_address_t initial_dst;
409 } ila_sir2ila_trace_t;
410
411 u8 *
412 format_ila_sir2ila_trace (u8 * s, va_list * args)
413 {
414   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
415   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
416   ila_sir2ila_trace_t *t = va_arg (*args, ila_sir2ila_trace_t *);
417
418   return format (s, "SIR -> ILA entry index: %d initial_dst: %U",
419                  t->ila_index, format_ip6_address, &t->initial_dst);
420 }
421
422 static vlib_node_registration_t ila_sir2ila_node;
423
424 static uword
425 ila_sir2ila (vlib_main_t * vm,
426              vlib_node_runtime_t * node, vlib_frame_t * frame)
427 {
428   ip6_main_t *im = &ip6_main;
429   ip_lookup_main_t *lm = &im->lookup_main;
430   ip_config_main_t *cm = &lm->feature_config_mains[VNET_IP_RX_UNICAST_FEAT];
431   u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
432   ila_main_t *ilm = &ila_main;
433
434   from = vlib_frame_vector_args (frame);
435   n_left_from = frame->n_vectors;
436   next_index = node->cached_next_index;
437
438   while (n_left_from > 0)
439     {
440       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
441
442       while (n_left_from >= 4 && n_left_to_next >= 2)
443         {
444           u32 pi0, pi1;
445           vlib_buffer_t *p0, *p1;
446           ip6_header_t *ip60, *ip61;
447           u32 next0 = ILA_SIR2ILA_NEXT_DROP;
448           u32 next1 = ILA_SIR2ILA_NEXT_DROP;
449           BVT (clib_bihash_kv) kv0, value0;
450           BVT (clib_bihash_kv) kv1, value1;
451           ila_entry_t *ie0 = &ila_sir2ila_default_entry;
452           ila_entry_t *ie1 = &ila_sir2ila_default_entry;
453           ip6_address_t *ila_address0, *ila_address1;
454
455           {
456             vlib_buffer_t *p2, *p3;
457
458             p2 = vlib_get_buffer (vm, from[2]);
459             p3 = vlib_get_buffer (vm, from[3]);
460
461             vlib_prefetch_buffer_header (p2, LOAD);
462             vlib_prefetch_buffer_header (p3, LOAD);
463             CLIB_PREFETCH (p2->data, sizeof (ip6_header_t), LOAD);
464             CLIB_PREFETCH (p3->data, sizeof (ip6_header_t), LOAD);
465           }
466
467           pi0 = to_next[0] = from[0];
468           pi1 = to_next[1] = from[1];
469           from += 2;
470           n_left_from -= 2;
471           to_next += 2;
472           n_left_to_next -= 2;
473
474           p0 = vlib_get_buffer (vm, pi0);
475           p1 = vlib_get_buffer (vm, pi1);
476           ip60 = vlib_buffer_get_current (p0);
477           ip61 = vlib_buffer_get_current (p1);
478           ila_address0 = &ip60->dst_address;
479           ila_address1 = &ip61->dst_address;
480           kv0.key[0] = ip60->dst_address.as_u64[0];
481           kv0.key[1] = ip60->dst_address.as_u64[1];
482           kv0.key[2] = 0;
483           kv1.key[0] = ip61->dst_address.as_u64[0];
484           kv1.key[1] = ip61->dst_address.as_u64[1];
485           kv1.key[2] = 0;
486
487           if (PREDICT_TRUE((BV (clib_bihash_search)
488               (&ilm->id_to_entry_table, &kv0, &value0)) == 0)) {
489               ie0 = &ilm->entries[value0.value];
490               ila_address0 = (ie0->dir != ILA_DIR_ILA2SIR) ? &ie0->ila_address : ila_address0;
491           }
492
493           if ((BV (clib_bihash_search)
494                (&ilm->id_to_entry_table, &kv1, &value1)) == 0) {
495             ie1 = &ilm->entries[value1.value];
496             ila_address1 = (ie1->dir != ILA_DIR_ILA2SIR) ? &ie1->ila_address : ila_address1;
497           }
498
499           if (PREDICT_FALSE (p0->flags & VLIB_BUFFER_IS_TRACED))
500             {
501               ila_sir2ila_trace_t *tr =
502                 vlib_add_trace (vm, node, p0, sizeof (*tr));
503               tr->ila_index =
504                 (ie0 != &ila_sir2ila_default_entry) ? (ie0 - ilm->entries) : ~0;
505               tr->initial_dst = ip60->dst_address;
506             }
507
508           if (PREDICT_FALSE (p1->flags & VLIB_BUFFER_IS_TRACED))
509             {
510               ila_sir2ila_trace_t *tr =
511                 vlib_add_trace (vm, node, p1, sizeof (*tr));
512               tr->ila_index =
513                 (ie1 != &ila_sir2ila_default_entry) ? (ie1 - ilm->entries) : ~0;
514               tr->initial_dst = ip61->dst_address;
515             }
516
517           ip60->dst_address.as_u64[0] = ila_address0->as_u64[0];
518           ip60->dst_address.as_u64[1] = ila_address0->as_u64[1];
519           ip61->dst_address.as_u64[0] = ila_address1->as_u64[0];
520           ip61->dst_address.as_u64[1] = ila_address1->as_u64[1];
521
522           vnet_get_config_data (&cm->config_main,
523                                 &p0->current_config_index, &next0, 0);
524
525           vnet_get_config_data (&cm->config_main,
526                                 &p1->current_config_index, &next1, 0);
527
528           vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
529                                            n_left_to_next, pi0, pi1, next0,
530                                            next1);
531         }
532
533       /* Single loop */
534       while (n_left_from > 0 && n_left_to_next > 0)
535         {
536           u32 pi0;
537           vlib_buffer_t *p0;
538           ip6_header_t *ip60;
539           u32 next0 = ILA_SIR2ILA_NEXT_DROP;
540           BVT (clib_bihash_kv) kv0, value0;
541           ila_entry_t *ie0 = &ila_sir2ila_default_entry;
542           ip6_address_t *ila_address0;
543
544           pi0 = to_next[0] = from[0];
545           from += 1;
546           n_left_from -= 1;
547           to_next += 1;
548           n_left_to_next -= 1;
549
550           p0 = vlib_get_buffer (vm, pi0);
551           ip60 = vlib_buffer_get_current (p0);
552           ila_address0 = &ip60->dst_address;
553
554           kv0.key[0] = ip60->dst_address.as_u64[0];
555           kv0.key[1] = ip60->dst_address.as_u64[1];
556           kv0.key[2] = 0;
557
558           if (PREDICT_TRUE((BV (clib_bihash_search)
559                (&ilm->id_to_entry_table, &kv0, &value0)) == 0)) {
560             ie0 = &ilm->entries[value0.value];
561             ila_address0 = (ie0->dir != ILA_DIR_ILA2SIR) ? &ie0->ila_address : ila_address0;
562           }
563
564           if (PREDICT_FALSE (p0->flags & VLIB_BUFFER_IS_TRACED))
565             {
566               ila_sir2ila_trace_t *tr =
567                 vlib_add_trace (vm, node, p0, sizeof (*tr));
568               tr->ila_index =
569                 (ie0 != &ila_sir2ila_default_entry) ? (ie0 - ilm->entries) : ~0;
570               tr->initial_dst = ip60->dst_address;
571             }
572
573           //This operation should do everything for any type (except vnid4 obviously)
574           ip60->dst_address.as_u64[0] = ila_address0->as_u64[0];
575           ip60->dst_address.as_u64[1] = ila_address0->as_u64[1];
576
577           vnet_get_config_data (&cm->config_main,
578                                 &p0->current_config_index, &next0, 0);
579
580           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
581                                            n_left_to_next, pi0, next0);
582         }
583       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
584     }
585
586   return frame->n_vectors;
587 }
588
589 /** *INDENT-OFF* */
590 VLIB_REGISTER_NODE (ila_sir2ila_node, static) =
591 {
592   .function = ila_sir2ila,.name = "sir-to-ila",
593   .vector_size = sizeof (u32),
594   .format_trace = format_ila_sir2ila_trace,
595   .n_errors = ILA_N_ERROR,
596   .error_strings = ila_error_strings,
597   .n_next_nodes = ILA_SIR2ILA_N_NEXT,
598   .next_nodes =
599   {
600       [ILA_SIR2ILA_NEXT_DROP] = "error-drop"
601   },
602 };
603 /** *INDENT-ON* */
604
605 /** *INDENT-OFF* */
606 VNET_IP6_UNICAST_FEATURE_INIT (ila_sir2ila, static) =
607 {
608   .node_name = "sir-to-ila",
609   .runs_before = ORDER_CONSTRAINTS{"ip6-lookup", 0},
610   .feature_index = &ila_main.ila_sir2ila_feature_index,
611 };
612 /** *INDENT-ON* */
613
614 static void
615 ila_entry_stack (ila_entry_t *ie)
616 {
617     /*
618      * restack on the next-hop's FIB entry
619      */
620     dpo_stack(ila_dpo_type,
621               DPO_PROTO_IP6,
622               &ie->ila_dpo,
623               fib_entry_contribute_ip_forwarding(
624                   ie->next_hop_fib_entry_index));
625 }
626
627 int
628 ila_add_del_entry (ila_add_del_entry_args_t * args)
629 {
630   ila_main_t *ilm = &ila_main;
631   BVT (clib_bihash_kv) kv, value;
632
633   //Sanity check
634   if (args->type == ILA_TYPE_IID || args->type == ILA_TYPE_LUID)
635     {
636       if ((args->sir_address.as_u8[8] >> 5) != args->type)
637         {
638           clib_warning ("Incorrect SIR address (ILA type mismatch %d %d)",
639                         args->sir_address.as_u8[8] >> 1, args->type);
640           return -1;
641         }
642       if (args->sir_address.as_u8[8] & 0x10)
643         {
644           clib_warning ("Checksum bit should not be set in SIR address");
645           return -1;
646         }
647     }
648   else if (args->type == ILA_TYPE_VNIDM)
649     {
650       if (args->sir_address.as_u8[0] != 0xff ||
651           (args->sir_address.as_u8[1] & 0xf0) != 0xf0)
652         {
653           clib_warning ("SIR multicast address must start with fff");
654           return -1;
655         }
656       if (args->sir_address.as_u16[1] || args->sir_address.as_u16[2] ||
657           args->sir_address.as_u16[3] || args->sir_address.as_u16[4] ||
658           args->sir_address.as_u16[5] || (args->sir_address.as_u8[12] & 0xf0))
659         {
660           clib_warning ("SIR multicast address must start with fff");
661           return -1;
662         }
663     }
664
665   if (!args->is_del)
666     {
667       ila_entry_t *e;
668       pool_get (ilm->entries, e);
669       e->type = args->type;
670       e->sir_address = args->sir_address;
671       e->next_hop = args->next_hop_address;
672       e->csum_mode = args->csum_mode;
673       e->dir = args->dir;
674
675       //Construct ILA address
676       switch (e->type)
677         {
678         case ILA_TYPE_IID:
679           e->ila_address = e->sir_address;
680           break;
681         case ILA_TYPE_LUID:
682           e->ila_address.as_u64[0] = args->locator;
683           e->ila_address.as_u64[1] = args->sir_address.as_u64[1];
684           break;
685         case ILA_TYPE_VNID6:
686           e->ila_address.as_u64[0] = args->locator;
687           e->ila_address.as_u8[8] = (ILA_TYPE_VNID6 << 1);
688           e->ila_address.as_u32[2] |= args->vnid;
689           e->ila_address.as_u32[3] = args->sir_address.as_u32[3];
690           break;
691         case ILA_TYPE_VNIDM:
692           e->ila_address.as_u64[0] = args->locator;
693           e->ila_address.as_u8[8] = (ILA_TYPE_VNIDM << 1);
694           e->ila_address.as_u32[2] |= args->vnid;
695           e->ila_address.as_u32[3] = args->sir_address.as_u32[3];
696           e->ila_address.as_u8[12] |= args->sir_address.as_u8[2] << 4;
697           break;
698         case ILA_TYPE_VNID4:
699           clib_warning ("ILA type '%U' is not supported", format_ila_type,
700                         e->type);
701           return -1;
702         }
703
704       //Modify ILA checksum if necessary
705       if (e->csum_mode == ILA_CSUM_MODE_NEUTRAL_MAP)
706         {
707           ip_csum_t csum = e->ila_address.as_u16[7];
708           int i;
709           for (i = 0; i < 4; i++)
710             {
711               csum = ip_csum_sub_even (csum, e->sir_address.as_u32[i]);
712               csum = ip_csum_add_even (csum, e->ila_address.as_u32[i]);
713             }
714           csum = ip_csum_add_even (csum, clib_host_to_net_u16 (0x1000));
715           e->ila_address.as_u16[7] = ip_csum_fold (csum);
716           e->ila_address.as_u8[8] |= 0x10;
717         }
718
719       //Create entry with the sir address
720       kv.key[0] = e->sir_address.as_u64[0];
721       kv.key[1] = e->sir_address.as_u64[1];
722       kv.key[2] = 0;
723       kv.value = e - ilm->entries;
724       BV (clib_bihash_add_del) (&ilm->id_to_entry_table, &kv,
725                                 1 /* is_add */ );
726
727       if (!ip6_address_is_zero(&e->next_hop))
728         {
729           /*
730            * become a child of the FIB netry for the next-hop
731            * so we are informed when its forwarding changes
732            */
733           fib_prefix_t next_hop = {
734               .fp_addr = {
735                   .ip6 = e->next_hop,
736               },
737               .fp_len = 128,
738               .fp_proto = FIB_PROTOCOL_IP6,
739           };
740
741           e->next_hop_fib_entry_index = 
742               fib_table_entry_special_add(0,
743                                           &next_hop,
744                                           FIB_SOURCE_RR,
745                                           FIB_ENTRY_FLAG_NONE,
746                                           ADJ_INDEX_INVALID);
747           e->next_hop_child_index =
748               fib_entry_child_add(e->next_hop_fib_entry_index,
749                                   ila_fib_node_type,
750                                   e - ilm->entries);
751
752           /*
753            * Create a route that results in the ILA entry
754            */
755           dpo_id_t dpo = DPO_NULL;
756           fib_prefix_t pfx = {
757               .fp_addr = {
758                   .ip6 = e->ila_address,
759               },
760               .fp_len = 128,
761               .fp_proto = FIB_PROTOCOL_IP6,
762           };
763
764           dpo_set(&dpo, ila_dpo_type, DPO_PROTO_IP6, e - ilm->entries);
765
766           fib_table_entry_special_dpo_add(0,
767                                           &pfx,
768                                           FIB_SOURCE_PLUGIN_HI,
769                                           FIB_ENTRY_FLAG_EXCLUSIVE,
770                                           &dpo);
771           dpo_reset(&dpo);
772
773           /*
774            * finally stack the ILA entry so it will forward to the next-hop
775            */
776           ila_entry_stack (e);
777         }
778     }
779   else
780     {
781       ila_entry_t *e;
782       kv.key[0] = args->sir_address.as_u64[0];
783       kv.key[1] = args->sir_address.as_u64[1];
784       kv.key[2] = 0;
785
786       if ((BV (clib_bihash_search) (&ilm->id_to_entry_table, &kv, &value) <
787            0))
788         {
789           return -1;
790         }
791
792       e = &ilm->entries[value.value];
793
794       if (!ip6_address_is_zero(&e->next_hop))
795         {
796           fib_prefix_t pfx = {
797               .fp_addr = {
798                   .ip6 = e->ila_address,
799               },
800               .fp_len = 128,
801               .fp_proto = FIB_PROTOCOL_IP6,
802           };
803
804           fib_table_entry_special_remove(0, &pfx, FIB_SOURCE_PLUGIN_HI);
805           /*
806            * remove this ILA entry as child of the FIB netry for the next-hop
807            */
808           fib_entry_child_remove(e->next_hop_fib_entry_index,
809                                  e->next_hop_child_index);
810           fib_table_entry_delete_index(e->next_hop_fib_entry_index,
811                                        FIB_SOURCE_RR);
812           e->next_hop_fib_entry_index = FIB_NODE_INDEX_INVALID;
813         }
814       dpo_reset (&e->ila_dpo);
815
816       BV (clib_bihash_add_del) (&ilm->id_to_entry_table, &kv,
817                                 0 /* is_add */ );
818       pool_put (ilm->entries, e);
819     }
820   return 0;
821 }
822
823 int
824 ila_interface (u32 sw_if_index, u8 disable)
825 {
826   vlib_main_t *vm = vlib_get_main ();
827   ila_main_t *ilm = &ila_main;
828   ip6_main_t *im = &ip6_main;
829   ip_lookup_main_t *lm = &im->lookup_main;
830   ip_config_main_t *cm = &lm->feature_config_mains[VNET_IP_RX_UNICAST_FEAT];
831   vnet_config_main_t *vcm = &cm->config_main;
832   u32 ci, feature_index;
833
834   vec_validate_init_empty (cm->config_index_by_sw_if_index, sw_if_index, ~0);
835   ci = cm->config_index_by_sw_if_index[sw_if_index];
836   feature_index = ilm->ila_sir2ila_feature_index;
837
838   ci = ((disable) ? vnet_config_del_feature : vnet_config_add_feature)
839     (vm, vcm, ci, feature_index,
840      /* config data */ 0,
841      /* # bytes of config data */ 0);
842
843   cm->config_index_by_sw_if_index[sw_if_index] = ci;
844   return 0;
845 }
846
847 clib_error_t *
848 vlib_plugin_register (vlib_main_t * vm, vnet_plugin_handoff_t * h,
849                       int from_early_init)
850 {
851   clib_error_t *error = 0;
852
853   return error;
854 }
855
856 u8 *format_ila_dpo (u8 * s, va_list * va)
857 {
858   index_t index = va_arg (*va, index_t);
859   CLIB_UNUSED(u32 indent) = va_arg (*va, u32);
860   ila_main_t *ilm = &ila_main;
861   ila_entry_t *ie = pool_elt_at_index (ilm->entries, index);
862   return format(s, "ILA: idx:%d sir:%U",
863                 index,
864                 format_ip6_address, &ie->sir_address);
865 }
866
867 /**
868  * @brief no-op lock function.
869  * The lifetime of the ILA entry is managed by the control plane
870  */
871 static void
872 ila_dpo_lock (dpo_id_t *dpo)
873 {
874 }
875
876 /**
877  * @brief no-op unlock function.
878  * The lifetime of the ILA entry is managed by the control plane
879  */
880 static void
881 ila_dpo_unlock (dpo_id_t *dpo)
882 {
883 }
884
885 const static dpo_vft_t ila_vft = {
886     .dv_lock = ila_dpo_lock,
887     .dv_unlock = ila_dpo_unlock,
888     .dv_format = format_ila_dpo,
889 };
890 const static char* const ila_ip6_nodes[] =
891 {
892     "ila-to-sir",
893     NULL,
894 };
895 const static char* const * const ila_nodes[DPO_PROTO_NUM] =
896 {
897     [DPO_PROTO_IP6]  = ila_ip6_nodes,
898 };
899
900 static fib_node_t *
901 ila_fib_node_get_node (fib_node_index_t index)
902 {
903   ila_main_t *ilm = &ila_main;
904   ila_entry_t *ie = pool_elt_at_index (ilm->entries, index);
905
906   return (&ie->ila_fib_node);
907 }
908
909 /**
910  * @brief no-op unlock function.
911  * The lifetime of the ILA entry is managed by the control plane
912  */
913 static void
914 ila_fib_node_last_lock_gone (fib_node_t *node)
915 {
916 }
917
918 static ila_entry_t *
919 ila_entry_from_fib_node (fib_node_t *node)
920 {
921     return ((ila_entry_t*)(((char*)node) -
922                            STRUCT_OFFSET_OF(ila_entry_t, ila_fib_node)));
923 }
924
925 /**
926  * @brief
927  * Callback function invoked when the forwarding changes for the ILA next-hop
928  */
929 static fib_node_back_walk_rc_t
930 ila_fib_node_back_walk_notify (fib_node_t *node,
931                                fib_node_back_walk_ctx_t *ctx)
932 {
933     ila_entry_stack(ila_entry_from_fib_node(node));
934
935     return (FIB_NODE_BACK_WALK_CONTINUE);
936 }
937
938 /*
939  * ILA's FIB graph node virtual function table
940  */
941 static const fib_node_vft_t ila_fib_node_vft = {
942     .fnv_get = ila_fib_node_get_node,
943     .fnv_last_lock = ila_fib_node_last_lock_gone,
944     .fnv_back_walk = ila_fib_node_back_walk_notify,
945 };
946
947 clib_error_t *
948 ila_init (vlib_main_t * vm)
949 {
950   ila_main_t *ilm = &ila_main;
951   ilm->entries = NULL;
952
953   ilm->lookup_table_nbuckets = ILA_TABLE_DEFAULT_HASH_NUM_BUCKETS;
954   ilm->lookup_table_nbuckets = 1 << max_log2 (ilm->lookup_table_nbuckets);
955   ilm->lookup_table_size = ILA_TABLE_DEFAULT_HASH_MEMORY_SIZE;
956
957   BV (clib_bihash_init) (&ilm->id_to_entry_table,
958                          "ila id to entry index table",
959                          ilm->lookup_table_nbuckets, ilm->lookup_table_size);
960
961   ila_dpo_type = dpo_register_new_type(&ila_vft, ila_nodes);
962   ila_fib_node_type = fib_node_register_new_type(&ila_fib_node_vft);
963
964   return NULL;
965 }
966
967 VLIB_INIT_FUNCTION (ila_init);
968
969 static clib_error_t *
970 ila_entry_command_fn (vlib_main_t * vm,
971                       unformat_input_t * input, vlib_cli_command_t * cmd)
972 {
973   unformat_input_t _line_input, *line_input = &_line_input;
974   ila_add_del_entry_args_t args = { 0 };
975   u8 next_hop_set = 0;
976   int ret;
977
978   args.type = ILA_TYPE_IID;
979   args.csum_mode = ILA_CSUM_MODE_NO_ACTION;
980   args.local_adj_index = ~0;
981   args.dir = ILA_DIR_BIDIR;
982
983   if (!unformat_user (input, unformat_line_input, line_input))
984     return 0;
985
986   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
987     {
988       if (unformat (line_input, "type %U", unformat_ila_type, &args.type))
989         ;
990       else if (unformat
991                (line_input, "sir-address %U", unformat_ip6_address,
992                 &args.sir_address))
993         ;
994       else if (unformat
995                (line_input, "locator %U", unformat_half_ip6_address,
996                 &args.locator))
997         ;
998       else if (unformat
999                (line_input, "csum-mode %U", unformat_ila_csum_mode,
1000                 &args.csum_mode))
1001         ;
1002       else if (unformat (line_input, "vnid %x", &args.vnid))
1003         ;
1004       else if (unformat
1005                (line_input, "next-hop %U", unformat_ip6_address,
1006                 &args.next_hop_address))
1007         ;
1008       else if (unformat
1009               (line_input, "direction %U", unformat_ila_direction, &args.dir))
1010         next_hop_set = 1;
1011       else if (unformat (line_input, "del"))
1012         args.is_del = 1;
1013       else
1014         return clib_error_return (0, "parse error: '%U'",
1015                                   format_unformat_error, line_input);
1016     }
1017
1018   unformat_free (line_input);
1019
1020   if (!next_hop_set)
1021       return clib_error_return (0, "Specified a next hop");
1022
1023   if ((ret = ila_add_del_entry (&args)))
1024     return clib_error_return (0, "ila_add_del_entry returned error %d", ret);
1025
1026   return NULL;
1027 }
1028
1029 VLIB_CLI_COMMAND (ila_entry_command, static) =
1030 {
1031   .path = "ila entry",
1032   .short_help = "ila entry [type <type>] [sir-address <address>] [locator <locator>] [vnid <hex-vnid>]"
1033     " [adj-index <adj-index>] [next-hop <next-hop>] [direction (bidir|sir2ila|ila2sir)]"
1034     " [csum-mode (no-action|neutral-map|transport-adjust)] [del]",
1035   .function = ila_entry_command_fn,
1036 };
1037
1038 static clib_error_t *
1039 ila_interface_command_fn (vlib_main_t * vm,
1040                           unformat_input_t * input, vlib_cli_command_t * cmd)
1041 {
1042   vnet_main_t *vnm = vnet_get_main ();
1043   u32 sw_if_index = ~0;
1044   u8 disable = 0;
1045
1046   if (!unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
1047     {
1048       return clib_error_return (0, "Invalid interface name");
1049     }
1050
1051   if (unformat (input, "disable"))
1052     {
1053       disable = 1;
1054     }
1055
1056   int ret;
1057   if ((ret = ila_interface (sw_if_index, disable)))
1058     return clib_error_return (0, "ila_interface returned error %d", ret);
1059
1060   return NULL;
1061 }
1062
1063 VLIB_CLI_COMMAND (ila_interface_command, static) =
1064 {
1065   .path = "ila interface",
1066   .short_help = "ila interface <interface-name> [disable]",
1067   .function = ila_interface_command_fn,
1068 };
1069
1070 static clib_error_t *
1071 ila_show_entries_command_fn (vlib_main_t * vm,
1072                              unformat_input_t * input,
1073                              vlib_cli_command_t * cmd)
1074 {
1075   vnet_main_t *vnm = vnet_get_main ();
1076   ila_main_t *ilm = &ila_main;
1077   ila_entry_t *e;
1078
1079   vlib_cli_output (vm, "  %U\n", format_ila_entry, vnm, NULL);
1080   pool_foreach (e, ilm->entries,
1081     ({
1082       vlib_cli_output (vm, "  %U\n", format_ila_entry, vnm, e);
1083     }));
1084
1085   return NULL;
1086 }
1087
1088 VLIB_CLI_COMMAND (ila_show_entries_command, static) =
1089 {
1090   .path = "show ila entries",
1091   .short_help = "show ila entries",
1092   .function = ila_show_entries_command_fn,
1093 };