socket_test.sh: Don't hard code debug image with gdb.
[vpp.git] / src / plugins / sixrd / sixrd.c
1 /*
2  * Copyright (c) 2018 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  * This code supports the following sixrd modes:
17  *
18  * 32 EA bits (Complete IPv4 address is embedded):
19  *   ea_bits_len = 32
20  * IPv4 suffix is embedded:
21  *   ea_bits_len = < 32
22  * No embedded address bits (1:1 mode):
23  *   ea_bits_len = 0
24  */
25
26 #include "sixrd.h"
27 #include <vnet/plugin/plugin.h>
28 #include <vlibapi/api.h>
29 #include <vlibmemory/api.h>
30 #include <vnet/adj/adj.h>
31 #include <vnet/fib/fib_table.h>
32 #include <vnet/fib/ip6_fib.h>
33 #include <vnet/adj/adj_midchain.h>
34 #include <vnet/adj/adj_delegate.h>
35 #include <vnet/dpo/lookup_dpo.h>
36 #include <vpp/app/version.h>    // Really needed?
37
38 /* define message IDs */
39 #include "sixrd_msg_enum.h"
40
41 /* define message structures */
42 #define vl_typedefs
43 #include "sixrd_all_api_h.h"
44 #undef vl_typedefs
45
46 /* define generated endian-swappers */
47 #define vl_endianfun
48 #include "sixrd_all_api_h.h"
49 #undef vl_endianfun
50
51 /* instantiate all the print functions we know about */
52 #define vl_print(handle, ...) vlib_cli_output(handle, __VA_ARGS__)
53 #define vl_printfun
54 #include "sixrd_all_api_h.h"
55 #undef vl_printfun
56
57 /* Get the API version number */
58 #define vl_api_version(n, v) static u32 api_version = (v);
59 #include "sixrd_all_api_h.h"
60 #undef vl_api_version
61
62 #define REPLY_MSG_ID_BASE sm->msg_id_base
63 #include <vlibapi/api_helper_macros.h>
64
65 extern vlib_node_registration_t ip4_sixrd_node;
66
67 /**
68  * Adj delegate data
69  */
70 typedef struct sixrd_adj_delegate_t_
71 {
72     /**
73      * linkage to the adj
74      */
75   u32 adj_index;
76
77     /**
78      * Linnkage to the FIB node graph
79      */
80   fib_node_t sixrd_node;
81
82     /**
83      * tracking of the IPv4 next-hop
84      */
85   fib_node_index_t sixrd_fib_entry_index;
86
87     /**
88      * sibling on the fib-entry
89      */
90   u32 sixrd_sibling;
91 } sixrd_adj_delegate_t;
92
93 /**
94  * Pool of delegate structs
95  */
96 static sixrd_adj_delegate_t *sixrd_adj_delegate_pool;
97
98 /**
99  * Adj delegate registered type
100  */
101 static adj_delegate_type_t sixrd_adj_delegate_type;
102
103 /**
104  * FIB node registered type
105  */
106 static fib_node_type_t sixrd_fib_node_type;
107
108 static inline sixrd_adj_delegate_t *
109 sixrd_adj_from_base (adj_delegate_t * ad)
110 {
111   if (NULL == ad)
112     {
113       return (NULL);
114     }
115   return (pool_elt_at_index (sixrd_adj_delegate_pool, ad->ad_index));
116 }
117
118 static inline const sixrd_adj_delegate_t *
119 sixrd_adj_from_const_base (const adj_delegate_t * ad)
120 {
121   if (NULL == ad)
122     {
123       return (NULL);
124     }
125   return (pool_elt_at_index (sixrd_adj_delegate_pool, ad->ad_index));
126 }
127
128 sixrd_main_t sixrd_main;
129
130 static void
131 sixrd_fixup (vlib_main_t * vm,
132              ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data)
133 {
134   ip4_header_t *ip4 = vlib_buffer_get_current (b0);
135   ip6_header_t *ip6 = vlib_buffer_get_current (b0) + sizeof (ip4_header_t);
136   const sixrd_tunnel_t *t = data;
137
138   ip4->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
139   ip4->dst_address.as_u32 =
140     sixrd_get_addr_net (t, ip6->dst_address.as_u64[0]);
141   ip4->checksum = ip4_header_checksum (ip4);
142 }
143
144 static void
145 ip6ip_fixup (vlib_main_t * vm,
146              ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data)
147 {
148   const sixrd_tunnel_t *t = data;
149   ip4_header_t *ip4 = vlib_buffer_get_current (b0);
150   ip4->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
151   ip4->dst_address.as_u32 =
152     sixrd_get_addr_net (t, adj->sub_type.nbr.next_hop.as_u64[0]);
153   ip4->checksum = ip4_header_checksum (ip4);
154 }
155
156 static sixrd_tunnel_t *
157 find_tunnel_by_sw_if_index (u32 sw_if_index)
158 {
159   sixrd_main_t *sm = &sixrd_main;
160   u32 ti = sm->tunnel_index_by_sw_if_index[sw_if_index];
161   if (ti == ~0)
162     {
163       clib_warning ("Not our tunnel\n");
164       return (0);
165     }
166   return pool_elt_at_index (sm->tunnels, ti);
167 }
168
169 static sixrd_tunnel_t *
170 find_tunnel (ip6_address_t * ip6_prefix)
171 {
172   sixrd_main_t *sm = &sixrd_main;
173   sixrd_tunnel_t *d;
174
175   /* *INDENT-OFF* */
176   pool_foreach (d, sm->tunnels,
177   ({
178     if (!memcmp (&d->ip6_prefix, ip6_prefix, 16))
179       return d;
180   }));
181   /* *INDENT-ON* */
182   return 0;
183 }
184
185
186 static u8 *
187 sixrd_build_rewrite (vnet_main_t * vnm,
188                      u32 sw_if_index,
189                      vnet_link_t link_type, const void *dst_address)
190 {
191   u8 *rewrite = NULL;
192   sixrd_tunnel_t *t;
193
194   t = find_tunnel_by_sw_if_index (sw_if_index);
195   if (!t)
196     {
197       return 0;
198     }
199
200   vec_validate (rewrite, sizeof (ip4_header_t) - 1);
201   ip4_header_t *ip4 = (ip4_header_t *) rewrite;
202   ip4->ip_version_and_header_length = 0x45;
203   ip4->ttl = 64;
204   ip4->protocol = IP_PROTOCOL_IPV6;
205   /* fixup ip4 header length and checksum after-the-fact */
206   ip4->src_address.as_u32 = t->ip4_src.as_u32;
207   ip4->dst_address.as_u32 = 0;
208   ip4->checksum = ip4_header_checksum (ip4);
209
210   return rewrite;
211 }
212
213 static void
214 ip6ip_tunnel_stack (adj_index_t ai, u32 fib_entry_index)
215 {
216   sixrd_main_t *sm = &sixrd_main;
217   ip_adjacency_t *adj = adj_get (ai);
218   sixrd_tunnel_t *t;
219   u32 sw_if_index = adj->rewrite_header.sw_if_index;
220
221   if ((vec_len (sm->tunnel_index_by_sw_if_index) < sw_if_index) ||
222       (~0 == sm->tunnel_index_by_sw_if_index[sw_if_index]))
223     return;
224
225   t =
226     pool_elt_at_index (sm->tunnels,
227                        sm->tunnel_index_by_sw_if_index[sw_if_index]);
228
229   /*
230    * find the adjacency that is contributed by the FIB entry
231    * that this tunnel resolves via, and use it as the next adj
232    * in the midchain
233    */
234   if (vnet_hw_interface_get_flags (vnet_get_main (), t->hw_if_index) &
235       VNET_HW_INTERFACE_FLAG_LINK_UP)
236     {
237       adj_nbr_midchain_stack (ai,
238                               fib_entry_contribute_ip_forwarding
239                               (fib_entry_index));
240     }
241   else
242     {
243       adj_nbr_midchain_unstack (ai);
244     }
245 }
246
247 static void
248 sixrd_tunnel_stack (adj_index_t ai, u32 fib_index)
249 {
250   dpo_id_t dpo = DPO_INVALID;
251   sixrd_main_t *sm = &sixrd_main;
252   ip_adjacency_t *adj = adj_get (ai);
253   u32 sw_if_index = adj->rewrite_header.sw_if_index;
254
255   if ((vec_len (sm->tunnel_index_by_sw_if_index) < sw_if_index) ||
256       (~0 == sm->tunnel_index_by_sw_if_index[sw_if_index]))
257     return;
258
259   if ((vec_len (sm->tunnel_index_by_sw_if_index) < sw_if_index) ||
260       (sm->tunnel_index_by_sw_if_index[sw_if_index] == ~0))
261     return;
262
263   lookup_dpo_add_or_lock_w_fib_index (fib_index, DPO_PROTO_IP4,
264                                       LOOKUP_UNICAST, LOOKUP_INPUT_DST_ADDR,
265                                       LOOKUP_TABLE_FROM_CONFIG, &dpo);
266   adj_nbr_midchain_stack (ai, &dpo);
267 }
268
269 const static ip46_address_t sixrd_special_nh = {
270   .ip6 = {
271           .as_u64 = {
272                      [0] = 0xffffffffffffffff,
273                      [1] = 0xffffffffffffffff,
274                      },
275           },
276 };
277
278 static void
279 sixrd_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
280 {
281   ip_adjacency_t *adj = adj_get (ai);
282   sixrd_tunnel_t *t = find_tunnel_by_sw_if_index (sw_if_index);
283
284   if (!memcmp (&sixrd_special_nh,
285                &adj->sub_type.nbr.next_hop, sizeof (sixrd_special_nh)))
286     {
287       adj_nbr_midchain_update_rewrite (ai, sixrd_fixup, t, ADJ_FLAG_NONE,
288                                        sixrd_build_rewrite (vnm, sw_if_index,
289                                                             adj_get_link_type
290                                                             (ai), NULL));
291       sixrd_tunnel_stack (ai, t->fib_index);
292     }
293   else
294     {
295       sixrd_adj_delegate_t *sixrd_ad;
296       ip4_address_t da4;
297
298       da4.as_u32 =
299         sixrd_get_addr_net (t, adj->sub_type.nbr.next_hop.as_u64[0]);
300
301       fib_prefix_t pfx = {
302         .fp_proto = FIB_PROTOCOL_IP4,
303         .fp_len = 32,
304         .fp_addr = {.ip4 = da4,}
305         ,
306       };
307
308       adj_nbr_midchain_update_rewrite
309         (ai, ip6ip_fixup, t,
310          ADJ_FLAG_NONE,
311          sixrd_build_rewrite (vnm, sw_if_index,
312                               adj_get_link_type (ai), NULL));
313
314       sixrd_ad =
315         sixrd_adj_from_base (adj_delegate_get (adj, sixrd_adj_delegate_type));
316       if (NULL == sixrd_ad)
317         {
318           pool_get (sixrd_adj_delegate_pool, sixrd_ad);
319           fib_node_init (&sixrd_ad->sixrd_node, sixrd_fib_node_type);
320           sixrd_ad->adj_index = ai;
321           sixrd_ad->sixrd_fib_entry_index =
322             fib_table_entry_special_add (t->fib_index, &pfx,
323                                          FIB_SOURCE_RR, FIB_ENTRY_FLAG_NONE);
324           sixrd_ad->sixrd_sibling =
325             fib_entry_child_add (sixrd_ad->sixrd_fib_entry_index,
326                                  sixrd_fib_node_type,
327                                  sixrd_ad - sixrd_adj_delegate_pool);
328
329           adj_delegate_add (adj,
330                             sixrd_adj_delegate_type,
331                             sixrd_ad - sixrd_adj_delegate_pool);
332
333           ip6ip_tunnel_stack (ai, sixrd_ad->sixrd_fib_entry_index);
334         }
335     }
336 }
337
338
339 clib_error_t *
340 sixrd_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
341 {
342   /* Always up */
343   vnet_hw_interface_set_flags (vnm, hw_if_index,
344                                VNET_HW_INTERFACE_FLAG_LINK_UP);
345   return /* no error */ 0;
346 }
347
348 /* *INDENT-OFF* */
349 VNET_HW_INTERFACE_CLASS (sixrd_hw_interface_class) =
350 {
351   .name = "ip6ip-6rd",
352   .build_rewrite = sixrd_build_rewrite,
353   .update_adjacency = sixrd_update_adj,
354 };
355
356 VNET_DEVICE_CLASS (sixrd_device_class) =
357 {
358   .name = "ip6ip-6rd",
359   .admin_up_down_function = sixrd_interface_admin_up_down,
360 #ifdef SOON
361   .clear counter = 0;
362 #endif
363 };
364 /* *INDENT-ON* */
365
366 static int
367 sixrd_create_tunnel (ip6_address_t * ip6_prefix, u8 ip6_prefix_len,
368                      ip4_address_t * ip4_prefix, u8 ip4_prefix_len,
369                      ip4_address_t * ip4_src, u16 mtu, bool security_check,
370                      u32 fib_index, u32 * sixrd_tunnel_index)
371 {
372   sixrd_main_t *sm = &sixrd_main;
373   sixrd_tunnel_t *t;
374
375   if (fib_index == ~0)
376     return VNET_API_ERROR_NO_SUCH_FIB;
377
378   if ((ip6_prefix_len + 32 - ip4_prefix_len) > 64)
379     return VNET_API_ERROR_INVALID_VALUE;
380
381   /* Tunnel already configured */
382   if (find_tunnel (ip6_prefix))
383     return VNET_API_ERROR_INVALID_VALUE;
384   /* Tunnel already configured for this source */
385   if (find_tunnel_by_ip4_address (ip4_src))
386     return VNET_API_ERROR_INVALID_VALUE;
387
388   /* Get tunnel index */
389   pool_get_aligned (sm->tunnels, t, CLIB_CACHE_LINE_BYTES);
390   memset (t, 0, sizeof (*t));
391   t->tunnel_index = t - sm->tunnels;
392
393   /* Init tunnel struct */
394   t->ip4_prefix.as_u32 = ip4_prefix->as_u32;
395   t->ip4_prefix_len = ip4_prefix_len;
396   t->ip6_prefix = *ip6_prefix;
397   t->ip6_prefix_len = ip6_prefix_len;
398   t->ip4_src = *ip4_src;
399   t->mtu = mtu ? mtu : SIXRD_DEFAULT_MTU;
400   t->security_check = security_check;
401
402   t->shift = (ip4_prefix_len < 32) ?
403     64 - ip6_prefix_len - (32 - ip4_prefix_len) : 0;
404
405   /* Create interface */
406   u32 hw_if_index = vnet_register_interface (vnet_get_main (),
407                                              sixrd_device_class.index,
408                                              t->tunnel_index,
409                                              sixrd_hw_interface_class.index,
410                                              t->tunnel_index);
411
412   /* Default the interface to up and enable IPv6 (payload) */
413   vnet_hw_interface_t *hi =
414     vnet_get_hw_interface (vnet_get_main (), hw_if_index);
415   t->hw_if_index = hw_if_index;
416   t->fib_index = fib_index;
417   t->sw_if_index = hi->sw_if_index;
418
419   hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] =
420     t->mtu;
421
422   vec_validate_init_empty (sm->tunnel_index_by_sw_if_index, hi->sw_if_index,
423                            ~0);
424   sm->tunnel_index_by_sw_if_index[hi->sw_if_index] = t->tunnel_index;
425
426   hash_set (sm->tunnel_by_ip, ip4_src->as_u32, t->tunnel_index);
427
428   vnet_hw_interface_set_flags (vnet_get_main (), hw_if_index,
429                                VNET_HW_INTERFACE_FLAG_LINK_UP);
430   vnet_sw_interface_set_flags (vnet_get_main (), hi->sw_if_index,
431                                VNET_SW_INTERFACE_FLAG_ADMIN_UP);
432
433   /* Create IPv6 route/adjacency */
434   fib_prefix_t pfx6 = {
435     .fp_proto = FIB_PROTOCOL_IP6,
436     .fp_len = t->ip6_prefix_len,
437     .fp_addr = {.ip6 = t->ip6_prefix,}
438     ,
439   };
440
441   fib_table_entry_update_one_path (fib_index, &pfx6,
442                                    FIB_SOURCE_CLI, FIB_ENTRY_FLAG_ATTACHED,
443                                    DPO_PROTO_IP6, &sixrd_special_nh,
444                                    hi->sw_if_index,
445                                    ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
446
447   *sixrd_tunnel_index = hi->sw_if_index;
448
449   ip4_register_protocol (IP_PROTOCOL_IPV6, ip4_sixrd_node.index);
450
451   return 0;
452 }
453
454 /*
455  * sixrd_delete_tunnel
456  */
457 int
458 sixrd_delete_tunnel (u32 sw_if_index)
459 {
460   sixrd_main_t *sm = &sixrd_main;
461   sixrd_tunnel_t *t = find_tunnel_by_sw_if_index (sw_if_index);
462
463   if (!t)
464     {
465       clib_warning ("SIXRD tunnel delete: tunnel does not exist: %d",
466                     sw_if_index);
467       return -1;
468     }
469
470   fib_prefix_t pfx6 = {
471     .fp_proto = FIB_PROTOCOL_IP6,
472     .fp_len = t->ip6_prefix_len,
473     .fp_addr = {.ip6 = t->ip6_prefix,}
474     ,
475   };
476   fib_table_entry_special_remove (0, &pfx6, FIB_SOURCE_CLI);
477   vnet_sw_interface_set_flags (vnet_get_main (), t->sw_if_index,
478                                0 /* down */ );
479
480   sm->tunnel_index_by_sw_if_index[t->sw_if_index] = ~0;
481
482   hash_unset (sm->tunnel_by_ip, t->ip4_src.as_u32);
483
484   vnet_delete_hw_interface (vnet_get_main (), t->hw_if_index);
485
486   pool_put (sm->tunnels, t);
487
488   return 0;
489 }
490
491 static clib_error_t *
492 sixrd_add_del_tunnel_command_fn (vlib_main_t * vm,
493                                  unformat_input_t * input,
494                                  vlib_cli_command_t * cmd)
495 {
496   unformat_input_t _line_input, *line_input = &_line_input;
497   ip4_address_t ip4_prefix;
498   ip6_address_t ip6_prefix;
499   ip4_address_t ip4_src;
500   u32 ip6_prefix_len = 0, ip4_prefix_len = 0, sixrd_tunnel_index;
501   u32 num_m_args = 0;
502   /* Optional arguments */
503   u32 mtu = 0;
504   u32 fib_index = 0;
505   clib_error_t *error = 0;
506   bool is_add = true, security_check = false;
507
508   /* Get a line of input. */
509   if (!unformat_user (input, unformat_line_input, line_input))
510     return 0;
511   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
512     {
513       if (unformat (line_input, "del"))
514         is_add = false;
515       if (unformat (line_input, "security-check"))
516         security_check = true;
517       else
518         if (unformat
519             (line_input, "ip6-pfx %U/%d", unformat_ip6_address, &ip6_prefix,
520              &ip6_prefix_len))
521         num_m_args++;
522       else if (unformat (line_input, "ip4-pfx %U/%d", unformat_ip4_address,
523                          &ip4_prefix, &ip4_prefix_len))
524         num_m_args++;
525       else
526         if (unformat
527             (line_input, "ip4-src %U", unformat_ip4_address, &ip4_src))
528         num_m_args++;
529       else if (unformat (line_input, "mtu %d", &mtu))
530         num_m_args++;
531       else if (unformat (line_input, "fib-id %d", &fib_index))
532         ;
533       else
534         {
535           error =
536             clib_error_return (0, "unknown input `%U'", format_unformat_error,
537                                line_input);
538           goto done;
539         }
540     }
541
542   if (num_m_args < 3)
543     {
544       error = clib_error_return (0, "mandatory argument(s) missing");
545       goto done;
546     }
547   if (is_add)
548     {
549       int rv = sixrd_create_tunnel (&ip6_prefix, ip6_prefix_len, &ip4_prefix,
550                                     ip4_prefix_len, &ip4_src, mtu,
551                                     security_check,
552                                     fib_index, &sixrd_tunnel_index);
553       if (rv)
554         error = clib_error_return (0, "adding tunnel failed");
555     }
556   else
557     {
558       sixrd_tunnel_t *t = find_tunnel (&ip6_prefix);
559       if (t)
560         {
561           sixrd_delete_tunnel (t->sw_if_index);
562         }
563     }
564
565 done:
566   unformat_free (line_input);
567
568   return error;
569 }
570
571 /* *INDENT-OFF* */
572 VLIB_CLI_COMMAND (sixrd_add_del_tunnel_command, static) =
573 {
574   .path = "create 6rd tunnel",
575   .short_help = "create 6rd tunnel ip6-pfx <ip6-pfx> ip4-pfx <ip4-pfx> "
576   "ip4-src <ip4-addr> [del]",
577   .function = sixrd_add_del_tunnel_command_fn,
578 };
579 /* *INDENT-ON* */
580
581 static void
582 vl_api_sixrd_add_tunnel_t_handler (vl_api_sixrd_add_tunnel_t * mp)
583 {
584   sixrd_main_t *sm = &sixrd_main;
585   vl_api_sixrd_add_tunnel_reply_t *rmp;
586   u32 sixrd_tunnel_index;
587
588   int rv = sixrd_create_tunnel ((ip6_address_t *) & mp->ip6_prefix,
589                                 mp->ip6_prefix_len,
590                                 (ip4_address_t *) & mp->ip4_prefix,
591                                 mp->ip4_prefix_len,
592                                 (ip4_address_t *) & mp->ip4_src,
593                                 ntohs (mp->mtu),
594                                 mp->security_check, ntohl (mp->fib_index),
595                                 &sixrd_tunnel_index);
596
597   REPLY_MACRO2 (VL_API_SIXRD_ADD_TUNNEL_REPLY, (
598                                                  {
599                                                  rmp->sw_if_index =
600                                                  htonl (sixrd_tunnel_index);
601                                                  }));
602 }
603
604 static void
605 vl_api_sixrd_del_tunnel_t_handler (vl_api_sixrd_del_tunnel_t * mp)
606 {
607   sixrd_main_t *sm = &sixrd_main;
608   vl_api_sixrd_del_tunnel_reply_t *rmp;
609
610   int rv = sixrd_delete_tunnel (ntohl (mp->sw_if_index));
611
612   REPLY_MACRO (VL_API_SIXRD_DEL_TUNNEL_REPLY);
613 }
614
615 /* List of message types that this plugin understands */
616
617 #define foreach_sixrd_plugin_api_msg            \
618 _(SIXRD_ADD_TUNNEL, sixrd_add_tunnel)           \
619 _(SIXRD_DEL_TUNNEL, sixrd_del_tunnel)
620
621 /* *INDENT-OFF* */
622 VLIB_PLUGIN_REGISTER() = {
623     .version = VPP_BUILD_VER,
624     .description = "IPv6 Rapid Deployment on IPv4 Infrastructure (RFC5969)",
625 };
626 /* *INDENT-ON* */
627
628 /**
629  * @brief Set up the API message handling tables.
630  */
631 static clib_error_t *
632 sixrd_plugin_api_hookup (vlib_main_t * vm)
633 {
634   sixrd_main_t *sm = &sixrd_main;
635
636 #define _(N, n)                                                                \
637   vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), #n,                  \
638                           vl_api_##n##_t_handler, vl_noop_handler,             \
639                           vl_api_##n##_t_endian, vl_api_##n##_t_print,         \
640                           sizeof(vl_api_##n##_t), 1);
641   foreach_sixrd_plugin_api_msg;
642 #undef _
643
644   return 0;
645 }
646
647 #define vl_msg_name_crc_list
648 #include "sixrd_all_api_h.h"
649 #undef vl_msg_name_crc_list
650
651 static void
652 setup_message_id_table (sixrd_main_t * sm, api_main_t * am)
653 {
654 #define _(id, n, crc)                                                          \
655   vl_msg_api_add_msg_name_crc(am, #n "_" #crc, id + sm->msg_id_base);
656   foreach_vl_msg_name_crc_sixrd;
657 #undef _
658 }
659
660 static void
661 sixrd_adj_delegate_adj_deleted (adj_delegate_t * aed)
662 {
663   sixrd_adj_delegate_t *sixrd_ad;
664
665   sixrd_ad = sixrd_adj_from_base (aed);
666
667   fib_entry_child_remove (sixrd_ad->sixrd_fib_entry_index,
668                           sixrd_ad->sixrd_sibling);
669   fib_table_entry_delete_index (sixrd_ad->sixrd_fib_entry_index,
670                                 FIB_SOURCE_RR);
671
672   pool_put (sixrd_adj_delegate_pool, sixrd_ad);
673 }
674
675 static u8 *
676 sixrd_adj_delegate_format (const adj_delegate_t * aed, u8 * s)
677 {
678   const sixrd_adj_delegate_t *sixrd_ad;
679
680   sixrd_ad = sixrd_adj_from_const_base (aed);
681
682   s = format (s, "SIXRD:[fib-entry:%d]", sixrd_ad->sixrd_fib_entry_index);
683
684   return (s);
685 }
686
687 static void
688 sixrd_fib_node_last_lock_gone (fib_node_t * node)
689 {
690   // top of the dependency tree, locks not managed here.
691 }
692
693 static sixrd_adj_delegate_t *
694 sixrd_adj_delegate_from_fib_node (fib_node_t * node)
695 {
696   return ((sixrd_adj_delegate_t *) (((char *) node) -
697                                     STRUCT_OFFSET_OF (sixrd_adj_delegate_t,
698                                                       sixrd_node)));
699 }
700
701 static fib_node_back_walk_rc_t
702 sixrd_fib_node_back_walk_notify (fib_node_t * node,
703                                  fib_node_back_walk_ctx_t * ctx)
704 {
705   sixrd_adj_delegate_t *sixrd_ad;
706
707   sixrd_ad = sixrd_adj_delegate_from_fib_node (node);
708
709   ip6ip_tunnel_stack (sixrd_ad->adj_index, sixrd_ad->sixrd_fib_entry_index);
710
711   return (FIB_NODE_BACK_WALK_CONTINUE);
712 }
713
714 /**
715  * Function definition to get a FIB node from its index
716  */
717 static fib_node_t *
718 sixrd_fib_node_get (fib_node_index_t index)
719 {
720   sixrd_adj_delegate_t *sixrd_ad;
721
722   sixrd_ad = pool_elt_at_index (sixrd_adj_delegate_pool, index);
723
724   return (&sixrd_ad->sixrd_node);
725 }
726
727 /**
728  * VFT registered with the adjacency delegate
729  */
730 const static adj_delegate_vft_t sixrd_adj_delegate_vft = {
731   .adv_adj_deleted = sixrd_adj_delegate_adj_deleted,
732   .adv_format = sixrd_adj_delegate_format,
733 };
734
735 /**
736  * VFT registered with the FIB node for the adj delegate
737  */
738 const static fib_node_vft_t sixrd_fib_node_vft = {
739   .fnv_get = sixrd_fib_node_get,
740   .fnv_last_lock = sixrd_fib_node_last_lock_gone,
741   .fnv_back_walk = sixrd_fib_node_back_walk_notify,
742 };
743
744 static clib_error_t *
745 sixrd_init (vlib_main_t * vm)
746 {
747   sixrd_main_t *sm = &sixrd_main;
748   clib_error_t *error = 0;
749   u8 *name;
750
751   name = format (0, "sixrd_%08x%c", api_version, 0);
752
753   sm->msg_id_base =
754     vl_msg_api_get_msg_ids ((char *) name, VL_MSG_FIRST_AVAILABLE);
755   vec_free (name);
756   error = sixrd_plugin_api_hookup (vm);
757
758   setup_message_id_table (sm, &api_main);
759
760   sixrd_adj_delegate_type =
761     adj_delegate_register_new_type (&sixrd_adj_delegate_vft);
762   sixrd_fib_node_type = fib_node_register_new_type (&sixrd_fib_node_vft);
763
764   return error;
765 }
766
767 VLIB_INIT_FUNCTION (sixrd_init);
768
769 /*
770  * fd.io coding-style-patch-verification: ON
771  *
772  * Local Variables:
773  * eval: (c-set-style "gnu")
774  * End:
775  */