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