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