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