99d1db8a0474c46f6a6bd71871d8b948d4465890
[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 - ilm->entries;
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 - ilm->entries;
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->feature_config_mains[VNET_IP_RX_UNICAST_FEAT];
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",
596   .runs_before = ORDER_CONSTRAINTS{"ip6-lookup", 0},
597   .feature_index = &ila_main.ila_sir2ila_feature_index,
598 };
599
600 int
601 ila_add_del_entry (ila_add_del_entry_args_t * args)
602 {
603   ila_main_t *ilm = &ila_main;
604   ip6_main_t *im6 = &ip6_main;
605   BVT (clib_bihash_kv) kv, value;
606
607   //Sanity check
608   if (args->type == ILA_TYPE_IID || args->type == ILA_TYPE_LUID)
609     {
610       if ((args->sir_address.as_u8[8] >> 5) != args->type)
611         {
612           clib_warning ("Incorrect SIR address (ILA type mismatch %d %d)",
613                         args->sir_address.as_u8[8] >> 1, args->type);
614           return -1;
615         }
616       if (args->sir_address.as_u8[8] & 0x10)
617         {
618           clib_warning ("Checksum bit should not be set in SIR address");
619           return -1;
620         }
621     }
622   else if (args->type == ILA_TYPE_VNIDM)
623     {
624       if (args->sir_address.as_u8[0] != 0xff ||
625           (args->sir_address.as_u8[1] & 0xf0) != 0xf0)
626         {
627           clib_warning ("SIR multicast address must start with fff");
628           return -1;
629         }
630       if (args->sir_address.as_u16[1] || args->sir_address.as_u16[2] ||
631           args->sir_address.as_u16[3] || args->sir_address.as_u16[4] ||
632           args->sir_address.as_u16[5] || (args->sir_address.as_u8[12] & 0xf0))
633         {
634           clib_warning ("SIR multicast address must start with fff");
635           return -1;
636         }
637     }
638
639   if (!args->is_del)
640     {
641       ila_entry_t *e;
642       pool_get (ilm->entries, e);
643       e->type = args->type;
644       e->sir_address = args->sir_address;
645       e->ila_adj_index = args->local_adj_index;
646       e->csum_mode = args->csum_mode;
647       e->dir = args->dir;
648
649       //Construct ILA address
650       switch (e->type)
651         {
652         case ILA_TYPE_IID:
653           e->ila_address = e->sir_address;
654           break;
655         case ILA_TYPE_LUID:
656           e->ila_address.as_u64[0] = args->locator;
657           e->ila_address.as_u64[1] = args->sir_address.as_u64[1];
658           break;
659         case ILA_TYPE_VNID6:
660           e->ila_address.as_u64[0] = args->locator;
661           e->ila_address.as_u8[8] = (ILA_TYPE_VNID6 << 1);
662           e->ila_address.as_u32[2] |= args->vnid;
663           e->ila_address.as_u32[3] = args->sir_address.as_u32[3];
664           break;
665         case ILA_TYPE_VNIDM:
666           e->ila_address.as_u64[0] = args->locator;
667           e->ila_address.as_u8[8] = (ILA_TYPE_VNIDM << 1);
668           e->ila_address.as_u32[2] |= args->vnid;
669           e->ila_address.as_u32[3] = args->sir_address.as_u32[3];
670           e->ila_address.as_u8[12] |= args->sir_address.as_u8[2] << 4;
671           break;
672         case ILA_TYPE_VNID4:
673           clib_warning ("ILA type '%U' is not supported", format_ila_type,
674                         e->type);
675           return -1;
676         }
677
678       //Modify ILA checksum if necessary
679       if (e->csum_mode == ILA_CSUM_MODE_NEUTRAL_MAP)
680         {
681           ip_csum_t csum = e->ila_address.as_u16[7];
682           int i;
683           for (i = 0; i < 4; i++)
684             {
685               csum = ip_csum_sub_even (csum, e->sir_address.as_u32[i]);
686               csum = ip_csum_add_even (csum, e->ila_address.as_u32[i]);
687             }
688           csum = ip_csum_add_even (csum, clib_host_to_net_u16 (0x1000));
689           e->ila_address.as_u16[7] = ip_csum_fold (csum);
690           e->ila_address.as_u8[8] |= 0x10;
691         }
692
693       //Create entry with the sir address
694       kv.key[0] = e->sir_address.as_u64[0];
695       kv.key[1] = e->sir_address.as_u64[1];
696       kv.key[2] = 0;
697       kv.value = e - ilm->entries;
698       BV (clib_bihash_add_del) (&ilm->id_to_entry_table, &kv,
699                                 1 /* is_add */ );
700
701       if (e->ila_adj_index != ~0)
702         {
703           //This is a local entry - let's create a local adjacency
704           ip_adjacency_t adj;
705           ip6_add_del_route_args_t route_args;
706           ila_adj_data_t *ad;
707
708           //Adjacency
709           memset (&adj, 0, sizeof (adj));
710           adj.explicit_fib_index = ~0;
711           adj.lookup_next_index = ilm->ip6_lookup_next_index;
712           ad = (ila_adj_data_t *) & adj.opaque;
713           ad->entry_index = e - ilm->entries;
714
715           //Route
716           memset (&route_args, 0, sizeof (route_args));
717           route_args.table_index_or_table_id = 0;
718           route_args.flags = IP6_ROUTE_FLAG_ADD;
719           route_args.dst_address = e->ila_address;
720           route_args.dst_address_length = 128;
721           route_args.adj_index = ~0;
722           route_args.add_adj = &adj;
723           route_args.n_add_adj = 1;
724
725           ip6_add_del_route (im6, &route_args);
726         }
727     }
728   else
729     {
730       ila_entry_t *e;
731       kv.key[0] = args->sir_address.as_u64[0];
732       kv.key[1] = args->sir_address.as_u64[1];
733       kv.key[2] = 0;
734
735       if ((BV (clib_bihash_search) (&ilm->id_to_entry_table, &kv, &value) <
736            0))
737         {
738           return -1;
739         }
740
741       e = &ilm->entries[value.value];
742
743       if (e->ila_adj_index != ~0)
744         {
745           //Delete that route - Associated adjacency will be deleted too
746           ip6_add_del_route_args_t route_args;
747           memset (&route_args, 0, sizeof (route_args));
748           route_args.table_index_or_table_id = 0;
749           route_args.flags = IP6_ROUTE_FLAG_DEL;
750           route_args.dst_address = e->ila_address;
751           route_args.dst_address_length = 128;
752           route_args.adj_index = ~0;
753           route_args.add_adj = NULL;
754           route_args.n_add_adj = 0;
755
756           ip6_add_del_route (im6, &route_args);
757         }
758
759       BV (clib_bihash_add_del) (&ilm->id_to_entry_table, &kv,
760                                 0 /* is_add */ );
761       pool_put (ilm->entries, e);
762     }
763   return 0;
764 }
765
766 int
767 ila_interface (u32 sw_if_index, u8 disable)
768 {
769   vlib_main_t *vm = vlib_get_main ();
770   ila_main_t *ilm = &ila_main;
771   ip6_main_t *im = &ip6_main;
772   ip_lookup_main_t *lm = &im->lookup_main;
773   ip_config_main_t *cm = &lm->feature_config_mains[VNET_IP_RX_UNICAST_FEAT];
774   vnet_config_main_t *vcm = &cm->config_main;
775   u32 ci, feature_index;
776
777   vec_validate_init_empty (cm->config_index_by_sw_if_index, sw_if_index, ~0);
778   ci = cm->config_index_by_sw_if_index[sw_if_index];
779   feature_index = ilm->ila_sir2ila_feature_index;
780
781   ci = ((disable) ? vnet_config_del_feature : vnet_config_add_feature)
782     (vm, vcm, ci, feature_index,
783      /* config data */ 0,
784      /* # bytes of config data */ 0);
785
786   cm->config_index_by_sw_if_index[sw_if_index] = ci;
787   return 0;
788 }
789
790 clib_error_t *
791 vlib_plugin_register (vlib_main_t * vm, vnet_plugin_handoff_t * h,
792                       int from_early_init)
793 {
794   clib_error_t *error = 0;
795
796   return error;
797 }
798
799 u8 *ila_format_adjacency(u8 * s, va_list * va)
800 {
801   ila_main_t *ilm = &ila_main;
802   __attribute((unused)) ip_lookup_main_t *lm = va_arg (*va, ip_lookup_main_t *);
803   ip_adjacency_t *adj = va_arg (*va, ip_adjacency_t *);
804   ila_adj_data_t * ad = (ila_adj_data_t *) & adj->opaque;
805   ila_entry_t *ie = pool_elt_at_index (ilm->entries, ad->entry_index);
806   return format(s, "idx:%d sir:%U", ad->entry_index, format_ip6_address, &ie->sir_address);
807 }
808
809 clib_error_t *
810 ila_init (vlib_main_t * vm)
811 {
812   ila_main_t *ilm = &ila_main;
813   ilm->entries = NULL;
814
815   ASSERT (sizeof (ila_adj_data_t) < IP_ADJACENCY_OPAQUE_SZ);
816
817   ilm->lookup_table_nbuckets = ILA_TABLE_DEFAULT_HASH_NUM_BUCKETS;
818   ilm->lookup_table_nbuckets = 1 << max_log2 (ilm->lookup_table_nbuckets);
819   ilm->lookup_table_size = ILA_TABLE_DEFAULT_HASH_MEMORY_SIZE;
820
821   BV (clib_bihash_init) (&ilm->id_to_entry_table,
822                          "ila id to entry index table",
823                          ilm->lookup_table_nbuckets, ilm->lookup_table_size);
824
825   return NULL;
826 }
827
828 VNET_IP6_REGISTER_ADJACENCY(ila2sir) = {
829   .node_name = "ila-to-sir",
830   .fn = ila_format_adjacency,
831   .next_index = &ila_main.ip6_lookup_next_index
832 };
833
834 VLIB_INIT_FUNCTION (ila_init);
835
836 static clib_error_t *
837 ila_entry_command_fn (vlib_main_t * vm,
838                       unformat_input_t * input, vlib_cli_command_t * cmd)
839 {
840   unformat_input_t _line_input, *line_input = &_line_input;
841   ila_add_del_entry_args_t args = { 0 };
842   ip6_address_t next_hop;
843   u8 next_hop_set = 0;
844   ip6_main_t *im6 = &ip6_main;
845   int ret;
846
847   args.type = ILA_TYPE_IID;
848   args.csum_mode = ILA_CSUM_MODE_NO_ACTION;
849   args.local_adj_index = ~0;
850   args.dir = ILA_DIR_BIDIR;
851
852   if (!unformat_user (input, unformat_line_input, line_input))
853     return 0;
854
855   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
856     {
857       if (unformat (line_input, "type %U", unformat_ila_type, &args.type))
858         ;
859       else
860         if (unformat
861             (line_input, "sir-address %U", unformat_ip6_address,
862              &args.sir_address))
863         ;
864       else
865         if (unformat
866             (line_input, "locator %U", unformat_half_ip6_address,
867              &args.locator))
868         ;
869       else if (unformat (line_input, "adj-index %u", &args.local_adj_index))
870         ;
871       else
872         if (unformat
873             (line_input, "csum-mode %U", unformat_ila_csum_mode,
874              &args.csum_mode))
875         ;
876       else if (unformat (line_input, "vnid %x", &args.vnid))
877         ;
878       else
879         if (unformat
880             (line_input, "next-hop %U", unformat_ip6_address, &next_hop))
881         next_hop_set = 1;
882       else if (unformat
883               (line_input, "direction %U", unformat_ila_direction, &args.dir))
884             ;
885       else if (unformat (line_input, "del"))
886         args.is_del = 1;
887       else
888         return clib_error_return (0, "parse error: '%U'",
889                                   format_unformat_error, line_input);
890     }
891
892   unformat_free (line_input);
893
894   if (next_hop_set)
895     {
896       if (args.local_adj_index != ~0)
897         return clib_error_return (0,
898                                   "Specified both next hop and adjacency index");
899
900       u32 ai = ip6_get_route (im6, 0, 0, &next_hop, 128);
901       if (ai == 0)
902         return clib_error_return (0, "No route to next-hop %U",
903                                   format_ip6_address, &next_hop);
904
905       ip_lookup_main_t *lm6 = &ip6_main.lookup_main;
906       ip_adjacency_t *adj6 = ip_get_adjacency (lm6, ai);
907       if (adj6->lookup_next_index != IP_LOOKUP_NEXT_REWRITE)
908         {
909           return clib_error_return (0,
910                                     "Next-Hop route has to be a rewrite route");
911         }
912       args.local_adj_index = ai;
913     }
914
915   if ((ret = ila_add_del_entry (&args)))
916     return clib_error_return (0, "ila_add_del_entry returned error %d", ret);
917
918   return NULL;
919 }
920
921 VLIB_CLI_COMMAND (ila_entry_command, static) =
922 {
923   .path = "ila entry",
924   .short_help = "ila entry [type <type>] [sir-address <address>] [locator <locator>] [vnid <hex-vnid>]"
925     " [adj-index <adj-index>] [next-hop <next-hop>] [direction (bidir|sir2ila|ila2sir)]"
926     " [csum-mode (no-action|neutral-map|transport-adjust)] [del]",
927   .function = ila_entry_command_fn,
928 };
929
930 static clib_error_t *
931 ila_interface_command_fn (vlib_main_t * vm,
932                           unformat_input_t * input, vlib_cli_command_t * cmd)
933 {
934   vnet_main_t *vnm = vnet_get_main ();
935   u32 sw_if_index = ~0;
936   u8 disable = 0;
937
938   if (!unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
939     {
940       return clib_error_return (0, "Invalid interface name");
941     }
942
943   if (unformat (input, "disable"))
944     {
945       disable = 1;
946     }
947
948   int ret;
949   if ((ret = ila_interface (sw_if_index, disable)))
950     return clib_error_return (0, "ila_interface returned error %d", ret);
951
952   return NULL;
953 }
954
955 VLIB_CLI_COMMAND (ila_interface_command, static) =
956 {
957   .path = "ila interface",
958   .short_help = "ila interface <interface-name> [disable]",
959   .function = ila_interface_command_fn,
960 };
961
962 static clib_error_t *
963 ila_show_entries_command_fn (vlib_main_t * vm,
964                              unformat_input_t * input,
965                              vlib_cli_command_t * cmd)
966 {
967   vnet_main_t *vnm = vnet_get_main ();
968   ila_main_t *ilm = &ila_main;
969   ila_entry_t *e;
970
971   vlib_cli_output (vm, "  %U\n", format_ila_entry, vnm, NULL);
972   pool_foreach (e, ilm->entries,
973     ({
974       vlib_cli_output (vm, "  %U\n", format_ila_entry, vnm, e);
975     }));
976
977   return NULL;
978 }
979
980 VLIB_CLI_COMMAND (ila_show_entries_command, static) =
981 {
982   .path = "show ila entries",
983   .short_help = "show ila entries",
984   .function = ila_show_entries_command_fn,
985 };