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