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