linux-cp: add support for table flush if multiple interfaces
[vpp.git] / src / plugins / linux-cp / lcp_router.c
1 /*
2  * Copyright (c) 2019 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 <sys/socket.h>
17 #include <linux/if.h>
18
19 //#include <vlib/vlib.h>
20 #include <vlib/unix/plugin.h>
21 #include <linux-cp/lcp_nl.h>
22 #include <linux-cp/lcp_interface.h>
23
24 #include <netlink/msg.h>
25 #include <netlink/netlink.h>
26 #include <netlink/socket.h>
27 #include <netlink/route/link.h>
28 #include <netlink/route/route.h>
29 #include <netlink/route/neighbour.h>
30 #include <netlink/route/addr.h>
31 #include <netlink/route/link/vlan.h>
32
33 #include <vnet/fib/fib_table.h>
34 #include <vnet/mfib/mfib_table.h>
35 #include <vnet/ip/ip6_ll_table.h>
36 #include <vnet/ip-neighbor/ip_neighbor.h>
37 #include <vnet/ip/ip6_link.h>
38
39 typedef struct lcp_router_table_t_
40 {
41   uint32_t nlt_id;
42   fib_protocol_t nlt_proto;
43   u32 nlt_fib_index;
44   u32 nlt_mfib_index;
45   u32 nlt_refs;
46 } lcp_router_table_t;
47
48 static uword *lcp_router_table_db[FIB_PROTOCOL_MAX];
49 static lcp_router_table_t *lcp_router_table_pool;
50 static vlib_log_class_t lcp_router_logger;
51
52 const static fib_prefix_t pfx_all1s = {
53   .fp_addr = {
54     .ip4 = {
55       .as_u32 = 0xffffffff,
56     }
57   },
58   .fp_proto = FIB_PROTOCOL_IP4,
59   .fp_len = 32,
60 };
61
62 static fib_source_t lcp_rt_fib_src;
63 static fib_source_t lcp_rt_fib_src_dynamic;
64
65 #define LCP_ROUTER_DBG(...) vlib_log_debug (lcp_router_logger, __VA_ARGS__);
66
67 #define LCP_ROUTER_INFO(...) vlib_log_notice (lcp_router_logger, __VA_ARGS__);
68
69 #define LCP_ROUTER_ERROR(...) vlib_log_err (lcp_router_logger, __VA_ARGS__);
70
71 static const mfib_prefix_t ip4_specials[] = {
72   /* ALL prefixes are in network order */
73   {
74    /* (*,224.0.0.0)/24 - all local subnet */
75    .fp_grp_addr = {
76                    .ip4.data_u32 = 0x000000e0,
77                    },
78    .fp_len = 24,
79    .fp_proto = FIB_PROTOCOL_IP4,
80    },
81 };
82
83 static const mfib_prefix_t ip6_specials[] = {
84   /* ALL prefixes are in network order */
85   {
86    /* (*,ff00::)/8 - all local subnet */
87    .fp_grp_addr = {
88                    .ip6.as_u64[0] = 0x00000000000000ff,
89                    },
90    .fp_len = 8,
91    .fp_proto = FIB_PROTOCOL_IP6,
92    },
93 };
94
95 /* VIF to PHY DB of managed interfaces */
96 static uword *lcp_routing_itf_db;
97
98 static u32
99 lcp_router_intf_h2p (u32 host)
100 {
101   lcp_itf_pair_t *lip;
102   index_t lipi;
103   uword *p;
104
105   /*
106    * first check the linux side created interface (i.e. vlans, tunnels etc)
107    */
108   p = hash_get (lcp_routing_itf_db, host);
109
110   if (p)
111     return p[0];
112
113   /*
114    * then check the paired phys
115    */
116   lipi = lcp_itf_pair_find_by_vif (host);
117
118   if (INDEX_INVALID == lipi)
119     return (~0);
120
121   lip = lcp_itf_pair_get (lipi);
122
123   return lip->lip_phy_sw_if_index;
124 }
125
126 /*
127  * Check timestamps on netlink message and interface pair to decide whether
128  * the message should be applied. See the declaration of nl_msg_info_t for
129  * an explanation on why this is necessary.
130  * If timestamps are good (message ts is newer than intf pair ts), return 0.
131  * Else, return -1.
132  */
133 static int
134 lcp_router_lip_ts_check (nl_msg_info_t *msg_info, lcp_itf_pair_t *lip)
135 {
136   if (!msg_info)
137     return 0;
138
139   if (msg_info->ts > lip->lip_create_ts)
140     return 0;
141
142   LCP_ROUTER_INFO ("Early message received for %U",
143                    format_vnet_sw_if_index_name, vnet_get_main (),
144                    lip->lip_phy_sw_if_index);
145   return -1;
146 }
147
148 static void
149 lcp_router_link_del (struct rtnl_link *rl, void *ctx)
150 {
151   index_t lipi;
152
153   if (!lcp_auto_subint ())
154     return;
155
156   lipi = lcp_itf_pair_find_by_vif (rtnl_link_get_ifindex (rl));
157
158   if (INDEX_INVALID != lipi)
159     {
160       lcp_itf_pair_t *lip;
161
162       lip = lcp_itf_pair_get (lipi);
163
164       if (lcp_router_lip_ts_check ((nl_msg_info_t *) ctx, lip))
165         return;
166
167       LCP_ROUTER_INFO ("delete link: %s - %U", rtnl_link_get_type (rl),
168                        format_vnet_sw_if_index_name, vnet_get_main (),
169                        lip->lip_phy_sw_if_index);
170       lcp_itf_pair_delete (lip->lip_phy_sw_if_index);
171
172       if (rtnl_link_is_vlan (rl))
173         {
174           LCP_ROUTER_INFO ("delete vlan: %s -> %U", rtnl_link_get_name (rl),
175                            format_vnet_sw_if_index_name, vnet_get_main (),
176                            lip->lip_phy_sw_if_index);
177           vnet_delete_sub_interface (lip->lip_phy_sw_if_index);
178           vnet_delete_sub_interface (lip->lip_host_sw_if_index);
179         }
180     }
181   else
182     LCP_ROUTER_INFO ("ignore link del: %s - %s", rtnl_link_get_type (rl),
183                      rtnl_link_get_name (rl));
184 }
185
186 static void
187 lcp_router_ip4_mroutes_add_del (u32 sw_if_index, u8 is_add)
188 {
189   const fib_route_path_t path = {
190     .frp_proto = DPO_PROTO_IP4,
191     .frp_addr = zero_addr,
192     .frp_sw_if_index = sw_if_index,
193     .frp_fib_index = ~0,
194     .frp_weight = 1,
195     .frp_mitf_flags = MFIB_ITF_FLAG_ACCEPT,
196   };
197   u32 mfib_index;
198   int ii;
199
200   mfib_index =
201     mfib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index);
202
203   for (ii = 0; ii < ARRAY_LEN (ip4_specials); ii++)
204     {
205       if (is_add)
206         {
207           mfib_table_entry_path_update (mfib_index, &ip4_specials[ii],
208                                         MFIB_SOURCE_PLUGIN_LOW,
209                                         MFIB_ENTRY_FLAG_NONE, &path);
210         }
211       else
212         {
213           mfib_table_entry_path_remove (mfib_index, &ip4_specials[ii],
214                                         MFIB_SOURCE_PLUGIN_LOW, &path);
215         }
216     }
217 }
218
219 static void
220 lcp_router_ip6_mroutes_add_del (u32 sw_if_index, u8 is_add)
221 {
222   const fib_route_path_t path = {
223     .frp_proto = DPO_PROTO_IP6,
224     .frp_addr = zero_addr,
225     .frp_sw_if_index = sw_if_index,
226     .frp_fib_index = ~0,
227     .frp_weight = 1,
228     .frp_mitf_flags = MFIB_ITF_FLAG_ACCEPT,
229   };
230   u32 mfib_index;
231   int ii;
232
233   mfib_index =
234     mfib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6, sw_if_index);
235
236   for (ii = 0; ii < ARRAY_LEN (ip6_specials); ii++)
237     {
238       if (is_add)
239         {
240           mfib_table_entry_path_update (mfib_index, &ip6_specials[ii],
241                                         MFIB_SOURCE_PLUGIN_LOW,
242                                         MFIB_ENTRY_FLAG_NONE, &path);
243         }
244       else
245         {
246           mfib_table_entry_path_remove (mfib_index, &ip6_specials[ii],
247                                         MFIB_SOURCE_PLUGIN_LOW, &path);
248         }
249     }
250 }
251
252 static void
253 lcp_router_link_mtu (struct rtnl_link *rl, u32 sw_if_index)
254 {
255   vnet_main_t *vnm = vnet_get_main ();
256   u32 mtu;
257   vnet_sw_interface_t *sw;
258   vnet_hw_interface_t *hw;
259
260   mtu = rtnl_link_get_mtu (rl);
261   if (!mtu)
262     return;
263
264   sw = vnet_get_sw_interface (vnm, sw_if_index);
265   hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
266
267   /* If HW interface, try to change hw link */
268   if ((sw->type == sw->sup_sw_if_index) &&
269       (hw->hw_class_index == ethernet_hw_interface_class.index))
270     vnet_hw_interface_set_mtu (vnm, hw->hw_if_index, mtu);
271   else
272     vnet_sw_interface_set_mtu (vnm, sw->sw_if_index, mtu);
273 }
274
275 static void
276 lcp_router_link_addr (struct rtnl_link *rl, lcp_itf_pair_t *lip)
277 {
278   vnet_main_t *vnm = vnet_get_main ();
279   struct nl_addr *mac_addr;
280   vnet_sw_interface_t *sw;
281   vnet_hw_interface_t *hw;
282   void *mac_addr_bytes;
283
284   mac_addr = rtnl_link_get_addr (rl);
285   if (!mac_addr || (nl_addr_get_family (mac_addr) != AF_LLC))
286     return;
287
288   sw = vnet_get_sw_interface (vnm, lip->lip_phy_sw_if_index);
289
290   /* can only change address on hw interface */
291   if (sw->sw_if_index != sw->sup_sw_if_index)
292     return;
293
294   hw = vnet_get_sup_hw_interface (vnm, lip->lip_phy_sw_if_index);
295   if (!vec_len (hw->hw_address))
296     return;
297
298   mac_addr_bytes = nl_addr_get_binary_addr (mac_addr);
299   if (clib_memcmp (mac_addr_bytes, hw->hw_address, nl_addr_get_len (mac_addr)))
300     vnet_hw_interface_change_mac_address (vnm, hw->hw_if_index,
301                                           mac_addr_bytes);
302
303   /* mcast adjacencies need to be updated */
304   vnet_update_adjacency_for_sw_interface (vnm, lip->lip_phy_sw_if_index,
305                                           lip->lip_phy_adjs.adj_index[AF_IP4]);
306   vnet_update_adjacency_for_sw_interface (vnm, lip->lip_phy_sw_if_index,
307                                           lip->lip_phy_adjs.adj_index[AF_IP6]);
308 }
309
310 static void lcp_router_table_flush (lcp_router_table_t *nlt,
311                                     u32 *sw_if_index_to_bool,
312                                     fib_source_t source);
313
314 static void
315 lcp_router_link_add (struct rtnl_link *rl, void *ctx)
316 {
317   index_t lipi;
318   int up;
319   vnet_main_t *vnm = vnet_get_main ();
320
321   lipi = lcp_itf_pair_find_by_vif (rtnl_link_get_ifindex (rl));
322   up = IFF_UP & rtnl_link_get_flags (rl);
323
324   if (INDEX_INVALID != lipi)
325     {
326       lcp_itf_pair_t *lip;
327       u32 sw_if_flags;
328       u32 sw_if_up;
329
330       lip = lcp_itf_pair_get (lipi);
331       if (!vnet_get_sw_interface (vnm, lip->lip_phy_sw_if_index))
332         return;
333
334       if (lcp_router_lip_ts_check ((nl_msg_info_t *) ctx, lip))
335         return;
336
337       sw_if_flags =
338         vnet_sw_interface_get_flags (vnm, lip->lip_phy_sw_if_index);
339       sw_if_up = (sw_if_flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP);
340
341       if (!sw_if_up && up)
342         {
343           vnet_sw_interface_admin_up (vnet_get_main (),
344                                       lip->lip_phy_sw_if_index);
345         }
346       else if (sw_if_up && !up)
347         {
348           vnet_sw_interface_admin_down (vnet_get_main (),
349                                         lip->lip_phy_sw_if_index);
350
351           /* When an interface is brought down administratively, the kernel
352            * removes routes which resolve through that interface. For IPv4
353            * routes, the kernel will not send any explicit RTM_DELROUTE
354            * messages about removing them. In order to synchronize with the
355            * kernel, affected IPv4 routes need to be manually removed from the
356            * FIB. The behavior is different for IPv6 routes. Explicit
357            * RTM_DELROUTE messages are sent about IPv6 routes being removed.
358            */
359           u32 fib_index;
360           lcp_router_table_t *nlt;
361
362           fib_index = fib_table_get_index_for_sw_if_index (
363             FIB_PROTOCOL_IP4, lip->lip_phy_sw_if_index);
364
365           pool_foreach (nlt, lcp_router_table_pool)
366             {
367               if (fib_index == nlt->nlt_fib_index &&
368                   FIB_PROTOCOL_IP4 == nlt->nlt_proto)
369                 {
370                   u32 *sw_if_index_to_bool = NULL;
371
372                   vec_validate_init_empty (sw_if_index_to_bool,
373                                            lip->lip_phy_sw_if_index, false);
374                   sw_if_index_to_bool[lip->lip_phy_sw_if_index] = true;
375
376                   lcp_router_table_flush (nlt, sw_if_index_to_bool,
377                                           lcp_rt_fib_src);
378                   lcp_router_table_flush (nlt, sw_if_index_to_bool,
379                                           lcp_rt_fib_src_dynamic);
380
381                   vec_free (sw_if_index_to_bool);
382                   break;
383                 }
384             }
385         }
386
387       LCP_ROUTER_DBG ("link: %s (%d) -> %U/%U %s", rtnl_link_get_name (rl),
388                       rtnl_link_get_ifindex (rl), format_vnet_sw_if_index_name,
389                       vnm, lip->lip_phy_sw_if_index,
390                       format_vnet_sw_if_index_name, vnm,
391                       lip->lip_host_sw_if_index, (up ? "up" : "down"));
392
393       lcp_router_link_mtu (rl, lip->lip_phy_sw_if_index);
394       lcp_router_link_addr (rl, lip);
395     }
396   else if (lcp_auto_subint () && rtnl_link_is_vlan (rl))
397     {
398       /* Find the pair based on the parent VIF */
399       lipi = lcp_itf_pair_find_by_vif (rtnl_link_get_link (rl));
400
401       if (INDEX_INVALID != lipi)
402         {
403           u32 sub_phy_sw_if_index, sub_host_sw_if_index;
404           const lcp_itf_pair_t *lip;
405           int vlan;
406           u8 *ns = 0; /* FIXME */
407
408           lip = lcp_itf_pair_get (lipi);
409
410           vlan = rtnl_link_vlan_get_id (rl);
411
412           /* create the vlan interface on the parent phy */
413           if (vnet_create_sub_interface (lip->lip_phy_sw_if_index, vlan, 18, 0,
414                                          vlan, &sub_phy_sw_if_index))
415             {
416               LCP_ROUTER_INFO ("failed create phy vlan: %s on %U",
417                                rtnl_link_get_name (rl),
418                                format_vnet_sw_if_index_name, vnet_get_main (),
419                                lip->lip_phy_sw_if_index);
420               return;
421             }
422           /* create the vlan interface on the parent host */
423           if (vnet_create_sub_interface (lip->lip_host_sw_if_index, vlan, 18,
424                                          0, vlan, &sub_host_sw_if_index))
425             {
426               LCP_ROUTER_INFO ("failed create vlan: %s on %U",
427                                rtnl_link_get_name (rl),
428                                format_vnet_sw_if_index_name, vnet_get_main (),
429                                lip->lip_host_sw_if_index);
430               return;
431             }
432
433           char *if_name;
434           u8 *if_namev = 0;
435
436           LCP_ROUTER_INFO (
437             "create vlan: %s -> (%U, %U) : (%U, %U)", rtnl_link_get_name (rl),
438             format_vnet_sw_if_index_name, vnet_get_main (),
439             lip->lip_phy_sw_if_index, format_vnet_sw_if_index_name,
440             vnet_get_main (), sub_phy_sw_if_index,
441             format_vnet_sw_if_index_name, vnet_get_main (),
442             lip->lip_host_sw_if_index, format_vnet_sw_if_index_name,
443             vnet_get_main (), sub_host_sw_if_index);
444
445           if ((if_name = rtnl_link_get_name (rl)) != NULL)
446             vec_validate_init_c_string (if_namev, if_name,
447                                         strnlen (if_name, IFNAMSIZ));
448           lcp_itf_pair_add (sub_host_sw_if_index, sub_phy_sw_if_index,
449                             if_namev, rtnl_link_get_ifindex (rl),
450                             lip->lip_host_type, ns);
451           if (up)
452             vnet_sw_interface_admin_up (vnet_get_main (), sub_phy_sw_if_index);
453           vnet_sw_interface_admin_up (vnet_get_main (), sub_host_sw_if_index);
454
455           vec_free (if_namev);
456         }
457       else
458         {
459           LCP_ROUTER_INFO ("ignore parent-link add: %s - %s",
460                            rtnl_link_get_type (rl), rtnl_link_get_name (rl));
461         }
462     }
463   else
464     LCP_ROUTER_INFO ("ignore link add: %s - %s", rtnl_link_get_type (rl),
465                      rtnl_link_get_name (rl));
466 }
467
468 static void
469 lcp_router_link_sync_begin (void)
470 {
471   LCP_ROUTER_INFO ("Begin synchronization of interface configurations");
472 }
473
474 static void
475 lcp_router_link_sync_end (void)
476 {
477   LCP_ROUTER_INFO ("End synchronization of interface configurations");
478 }
479
480 static clib_error_t *
481 lcp_router_link_up_down (vnet_main_t *vnm, u32 hw_if_index, u32 flags)
482 {
483   vnet_hw_interface_t *hi;
484   index_t lipi;
485
486   hi = vnet_get_hw_interface_or_null (vnm, hw_if_index);
487   if (!hi)
488     return 0;
489
490   lipi = lcp_itf_pair_find_by_phy (hi->sw_if_index);
491   if (lipi == INDEX_INVALID)
492     return 0;
493
494   /* When the link goes down on an interface, the kernel processes routes which
495    * resolve through that interface depending on how they were created:
496    *   - Legacy Route API: the kernel retains the routes and marks them as
497    *     "linkdown";
498    *   - Nexthop API: the kernel removes the next-hop objects and the routes
499    *     which reference them.
500    *
501    * For IPv4 routes created with Nexthop API, the kernel will not send any
502    * explicit RTM_DELROUTE messages about removing them. In order to
503    * synchronize with the kernel, affected routes need to be manually removed
504    * from the FIB.
505    *
506    * The behavior is different for IPv6 routes created with Nexthop API. The
507    * kernel will send explicit RTM_DELROUTE messages about IPv6 routes being
508    * removed.
509    */
510   if (!(flags & VNET_HW_INTERFACE_FLAG_LINK_UP) &&
511       (lcp_get_del_static_on_link_down () ||
512        lcp_get_del_dynamic_on_link_down ()))
513     {
514       u32 fib_index;
515       lcp_router_table_t *nlt;
516
517       fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
518                                                        hi->sw_if_index);
519
520       pool_foreach (nlt, lcp_router_table_pool)
521         {
522           if (fib_index == nlt->nlt_fib_index &&
523               FIB_PROTOCOL_IP4 == nlt->nlt_proto)
524             {
525               u32 *sw_if_index_to_bool = NULL;
526
527               vec_validate_init_empty (sw_if_index_to_bool, hi->sw_if_index,
528                                        false);
529               sw_if_index_to_bool[hi->sw_if_index] = true;
530
531               if (lcp_get_del_static_on_link_down ())
532                 lcp_router_table_flush (nlt, sw_if_index_to_bool,
533                                         lcp_rt_fib_src);
534               if (lcp_get_del_dynamic_on_link_down ())
535                 lcp_router_table_flush (nlt, sw_if_index_to_bool,
536                                         lcp_rt_fib_src_dynamic);
537
538               vec_free (sw_if_index_to_bool);
539               break;
540             }
541         }
542     }
543
544   return 0;
545 }
546
547 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (lcp_router_link_up_down);
548
549 static fib_protocol_t
550 lcp_router_proto_k2f (uint32_t k)
551 {
552   if (AF_INET6 == k)
553     return (FIB_PROTOCOL_IP6);
554   return (FIB_PROTOCOL_IP4);
555 }
556
557 static void
558 lcp_router_mk_addr (const struct nl_addr *rna, ip_address_t *ia)
559 {
560   fib_protocol_t fproto;
561
562   ip_address_reset (ia);
563   fproto = lcp_router_proto_k2f (nl_addr_get_family (rna));
564
565   ip_address_set (ia, nl_addr_get_binary_addr (rna),
566                   FIB_PROTOCOL_IP4 == fproto ? AF_IP4 : AF_IP6);
567 }
568
569 static fib_protocol_t
570 lcp_router_mk_addr46 (const struct nl_addr *rna, ip46_address_t *ia)
571 {
572   fib_protocol_t fproto;
573
574   fproto = lcp_router_proto_k2f (nl_addr_get_family (rna));
575   ip46_address_reset (ia);
576   if (FIB_PROTOCOL_IP4 == fproto)
577     memcpy (&ia->ip4, nl_addr_get_binary_addr (rna), nl_addr_get_len (rna));
578   else
579     memcpy (&ia->ip6, nl_addr_get_binary_addr (rna), nl_addr_get_len (rna));
580
581   return (fproto);
582 }
583
584 static void
585 lcp_router_link_addr_add_del (struct rtnl_addr *rla, int is_del)
586 {
587   u32 sw_if_index;
588
589   sw_if_index = lcp_router_intf_h2p (rtnl_addr_get_ifindex (rla));
590
591   if (~0 != sw_if_index)
592     {
593       ip_address_t nh;
594
595       lcp_router_mk_addr (rtnl_addr_get_local (rla), &nh);
596
597       if (AF_IP4 == ip_addr_version (&nh))
598         {
599           ip4_add_del_interface_address (
600             vlib_get_main (), sw_if_index, &ip_addr_v4 (&nh),
601             rtnl_addr_get_prefixlen (rla), is_del);
602           lcp_router_ip4_mroutes_add_del (sw_if_index, !is_del);
603         }
604       else if (AF_IP6 == ip_addr_version (&nh))
605         {
606           if (ip6_address_is_link_local_unicast (&ip_addr_v6 (&nh)))
607             if (is_del)
608               ip6_link_disable (sw_if_index);
609             else
610               {
611                 ip6_link_enable (sw_if_index, NULL);
612                 ip6_link_set_local_address (sw_if_index, &ip_addr_v6 (&nh));
613               }
614           else
615             ip6_add_del_interface_address (
616               vlib_get_main (), sw_if_index, &ip_addr_v6 (&nh),
617               rtnl_addr_get_prefixlen (rla), is_del);
618           lcp_router_ip6_mroutes_add_del (sw_if_index, !is_del);
619         }
620
621       LCP_ROUTER_DBG ("link-addr: %U %U/%d", format_vnet_sw_if_index_name,
622                       vnet_get_main (), sw_if_index, format_ip_address, &nh,
623                       rtnl_addr_get_prefixlen (rla));
624     }
625 }
626
627 static void
628 lcp_router_link_addr_del (struct rtnl_addr *la)
629 {
630   lcp_router_link_addr_add_del (la, 1);
631 }
632
633 static void
634 lcp_router_link_addr_add (struct rtnl_addr *la)
635 {
636   lcp_router_link_addr_add_del (la, 0);
637 }
638
639 static void
640 lcp_router_link_addr_sync_begin (void)
641 {
642   ip_interface_address_mark ();
643
644   LCP_ROUTER_INFO ("Begin synchronization of interface addresses");
645 }
646
647 static void
648 lcp_router_link_addr_sync_end (void)
649 {
650   ip_interface_address_sweep ();
651
652   LCP_ROUTER_INFO ("End synchronization of interface addresses");
653 }
654
655 static void
656 lcp_router_mk_mac_addr (const struct nl_addr *rna, mac_address_t *mac)
657 {
658   mac_address_from_bytes (mac, nl_addr_get_binary_addr (rna));
659 }
660
661 static void
662 lcp_router_neigh_del (struct rtnl_neigh *rn)
663 {
664   u32 sw_if_index;
665
666   sw_if_index = lcp_router_intf_h2p (rtnl_neigh_get_ifindex (rn));
667
668   if (~0 != sw_if_index)
669     {
670       ip_address_t nh;
671       int rv;
672
673       lcp_router_mk_addr (rtnl_neigh_get_dst (rn), &nh);
674
675       if (ip46_address_is_multicast (&ip_addr_46 (&nh)))
676         {
677           LCP_ROUTER_DBG ("ignore neighbor del: %U %U", format_ip_address, &nh,
678                           format_vnet_sw_if_index_name, vnet_get_main (),
679                           sw_if_index);
680           return;
681         }
682
683       rv = ip_neighbor_del (&nh, sw_if_index);
684
685       if (rv)
686         {
687           LCP_ROUTER_ERROR (
688             "Failed to delete neighbor: %U %U", format_ip_address, &nh,
689             format_vnet_sw_if_index_name, vnet_get_main (), sw_if_index);
690         }
691       else
692         {
693           LCP_ROUTER_DBG ("neighbor del: %U %U", format_ip_address, &nh,
694                           format_vnet_sw_if_index_name, vnet_get_main (),
695                           sw_if_index);
696         }
697     }
698   else
699     LCP_ROUTER_INFO ("ignore neighbour del on: %d",
700                      rtnl_neigh_get_ifindex (rn));
701 }
702
703 #ifndef NUD_VALID
704 #define NUD_VALID                                                             \
705   (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE | NUD_PROBE | NUD_STALE |        \
706    NUD_DELAY)
707 #endif
708
709 static void
710 lcp_router_neigh_add (struct rtnl_neigh *rn)
711 {
712   u32 sw_if_index;
713
714   sw_if_index = lcp_router_intf_h2p (rtnl_neigh_get_ifindex (rn));
715
716   if (~0 != sw_if_index)
717     {
718       struct nl_addr *ll;
719       ip_address_t nh;
720       int state;
721
722       lcp_router_mk_addr (rtnl_neigh_get_dst (rn), &nh);
723
724       if (ip46_address_is_multicast (&ip_addr_46 (&nh)))
725         {
726           LCP_ROUTER_DBG ("ignore neighbor add: %U %U", format_ip_address, &nh,
727                           format_vnet_sw_if_index_name, vnet_get_main (),
728                           sw_if_index);
729           return;
730         }
731
732       ll = rtnl_neigh_get_lladdr (rn);
733       state = rtnl_neigh_get_state (rn);
734
735       if (ll && (state & NUD_VALID))
736         {
737           mac_address_t mac;
738           ip_neighbor_flags_t flags;
739           int rv;
740
741           lcp_router_mk_mac_addr (ll, &mac);
742
743           if (state & (NUD_NOARP | NUD_PERMANENT))
744             flags = IP_NEIGHBOR_FLAG_STATIC;
745           else
746             flags = IP_NEIGHBOR_FLAG_DYNAMIC;
747
748           rv = ip_neighbor_add (&nh, &mac, sw_if_index, flags, NULL);
749
750           if (rv)
751             {
752               LCP_ROUTER_ERROR (
753                 "Failed to create neighbor: %U %U", format_ip_address, &nh,
754                 format_vnet_sw_if_index_name, vnet_get_main (), sw_if_index);
755             }
756           else
757             {
758               LCP_ROUTER_DBG ("neighbor add: %U %U", format_ip_address, &nh,
759                               format_vnet_sw_if_index_name, vnet_get_main (),
760                               sw_if_index);
761             }
762         }
763       else
764         /* It's a delete */
765         lcp_router_neigh_del (rn);
766     }
767   else
768     LCP_ROUTER_INFO ("ignore neighbour add on: %d",
769                      rtnl_neigh_get_ifindex (rn));
770 }
771
772 static void
773 lcp_router_neigh_sync_begin (void)
774 {
775   ip_neighbor_mark (AF_IP4);
776   ip_neighbor_mark (AF_IP6);
777
778   LCP_ROUTER_INFO ("Begin synchronization of neighbors");
779 }
780
781 static void
782 lcp_router_neigh_sync_end (void)
783 {
784   ip_neighbor_sweep (AF_IP4);
785   ip_neighbor_sweep (AF_IP6);
786
787   LCP_ROUTER_INFO ("End synchronization of neighbors");
788 }
789
790 static lcp_router_table_t *
791 lcp_router_table_find (uint32_t id, fib_protocol_t fproto)
792 {
793   uword *p;
794
795   p = hash_get (lcp_router_table_db[fproto], id);
796
797   if (p)
798     return pool_elt_at_index (lcp_router_table_pool, p[0]);
799
800   return (NULL);
801 }
802
803 static uint32_t
804 lcp_router_table_k2f (uint32_t k)
805 {
806   // the kernel's table ID 255 is the default table
807   if (k == 255 || k == 254)
808     return 0;
809   return k;
810 }
811
812 static lcp_router_table_t *
813 lcp_router_table_add_or_lock (uint32_t id, fib_protocol_t fproto)
814 {
815   lcp_router_table_t *nlt;
816
817   id = lcp_router_table_k2f (id);
818   nlt = lcp_router_table_find (id, fproto);
819
820   if (NULL == nlt)
821     {
822       pool_get_zero (lcp_router_table_pool, nlt);
823
824       nlt->nlt_id = id;
825       nlt->nlt_proto = fproto;
826
827       nlt->nlt_fib_index = fib_table_find_or_create_and_lock (
828         nlt->nlt_proto, nlt->nlt_id, lcp_rt_fib_src);
829       nlt->nlt_mfib_index = mfib_table_find_or_create_and_lock (
830         nlt->nlt_proto, nlt->nlt_id, MFIB_SOURCE_PLUGIN_LOW);
831
832       hash_set (lcp_router_table_db[fproto], nlt->nlt_id,
833                 nlt - lcp_router_table_pool);
834
835       if (FIB_PROTOCOL_IP4 == fproto)
836         {
837           /* Set the all 1s address in this table to punt */
838           fib_table_entry_special_add (nlt->nlt_fib_index, &pfx_all1s,
839                                        lcp_rt_fib_src, FIB_ENTRY_FLAG_LOCAL);
840
841           const fib_route_path_t path = {
842             .frp_proto = DPO_PROTO_IP4,
843             .frp_addr = zero_addr,
844             .frp_sw_if_index = ~0,
845             .frp_fib_index = ~0,
846             .frp_weight = 1,
847             .frp_mitf_flags = MFIB_ITF_FLAG_FORWARD,
848             .frp_flags = FIB_ROUTE_PATH_LOCAL,
849           };
850           int ii;
851
852           for (ii = 0; ii < ARRAY_LEN (ip4_specials); ii++)
853             {
854               mfib_table_entry_path_update (
855                 nlt->nlt_mfib_index, &ip4_specials[ii], MFIB_SOURCE_PLUGIN_LOW,
856                 MFIB_ENTRY_FLAG_NONE, &path);
857             }
858         }
859       else if (FIB_PROTOCOL_IP6 == fproto)
860         {
861           const fib_route_path_t path = {
862             .frp_proto = DPO_PROTO_IP6,
863             .frp_addr = zero_addr,
864             .frp_sw_if_index = ~0,
865             .frp_fib_index = ~0,
866             .frp_weight = 1,
867             .frp_mitf_flags = MFIB_ITF_FLAG_FORWARD,
868             .frp_flags = FIB_ROUTE_PATH_LOCAL,
869           };
870           int ii;
871
872           for (ii = 0; ii < ARRAY_LEN (ip6_specials); ii++)
873             {
874               mfib_table_entry_path_update (
875                 nlt->nlt_mfib_index, &ip6_specials[ii], MFIB_SOURCE_PLUGIN_LOW,
876                 MFIB_ENTRY_FLAG_NONE, &path);
877             }
878         }
879     }
880
881   nlt->nlt_refs++;
882
883   return (nlt);
884 }
885
886 static void
887 lcp_router_table_unlock (lcp_router_table_t *nlt)
888 {
889   nlt->nlt_refs--;
890
891   if (0 == nlt->nlt_refs)
892     {
893       if (FIB_PROTOCOL_IP4 == nlt->nlt_proto)
894         {
895           /* Set the all 1s address in this table to punt */
896           fib_table_entry_special_remove (nlt->nlt_fib_index, &pfx_all1s,
897                                           lcp_rt_fib_src);
898         }
899
900       fib_table_unlock (nlt->nlt_fib_index, nlt->nlt_proto, lcp_rt_fib_src);
901
902       hash_unset (lcp_router_table_db[nlt->nlt_proto], nlt->nlt_id);
903       pool_put (lcp_router_table_pool, nlt);
904     }
905 }
906
907 static void
908 lcp_router_route_mk_prefix (struct rtnl_route *r, fib_prefix_t *p)
909 {
910   const struct nl_addr *addr = rtnl_route_get_dst (r);
911
912   p->fp_len = nl_addr_get_prefixlen (addr);
913   p->fp_proto = lcp_router_mk_addr46 (addr, &p->fp_addr);
914 }
915
916 static void
917 lcp_router_route_mk_mprefix (struct rtnl_route *r, mfib_prefix_t *p)
918 {
919   const struct nl_addr *addr;
920
921   addr = rtnl_route_get_dst (r);
922
923   p->fp_len = nl_addr_get_prefixlen (addr);
924   p->fp_proto = lcp_router_mk_addr46 (addr, &p->fp_grp_addr);
925
926   addr = rtnl_route_get_src (r);
927   if (addr)
928     p->fp_proto = lcp_router_mk_addr46 (addr, &p->fp_src_addr);
929 }
930
931 typedef struct lcp_router_route_path_parse_t_
932 {
933   fib_route_path_t *paths;
934   fib_protocol_t route_proto;
935   bool is_mcast;
936   fib_route_path_flags_t type_flags;
937   u8 preference;
938 } lcp_router_route_path_parse_t;
939
940 static void
941 lcp_router_route_path_parse (struct rtnl_nexthop *rnh, void *arg)
942 {
943   lcp_router_route_path_parse_t *ctx = arg;
944   fib_route_path_t *path;
945   u32 sw_if_index;
946
947   sw_if_index = lcp_router_intf_h2p (rtnl_route_nh_get_ifindex (rnh));
948
949   if (~0 != sw_if_index)
950     {
951       fib_protocol_t fproto;
952       struct nl_addr *addr;
953
954       vec_add2 (ctx->paths, path, 1);
955
956       path->frp_flags = FIB_ROUTE_PATH_FLAG_NONE | ctx->type_flags;
957       path->frp_sw_if_index = sw_if_index;
958       path->frp_weight = rtnl_route_nh_get_weight (rnh);
959       path->frp_preference = ctx->preference;
960
961       addr = rtnl_route_nh_get_gateway (rnh);
962
963       if (addr)
964         fproto = lcp_router_mk_addr46 (rtnl_route_nh_get_gateway (rnh),
965                                        &path->frp_addr);
966       else
967         fproto = ctx->route_proto;
968
969       path->frp_proto = fib_proto_to_dpo (fproto);
970
971       if (ctx->is_mcast)
972         path->frp_mitf_flags = MFIB_ITF_FLAG_FORWARD;
973
974       LCP_ROUTER_DBG (" path:[%U]", format_fib_route_path, path);
975     }
976 }
977
978 /*
979  * blackhole, unreachable, prohibit will not have a next hop in an
980  * RTM_NEWROUTE. Add a path for them.
981  */
982 static void
983 lcp_router_route_path_add_special (struct rtnl_route *rr,
984                                    lcp_router_route_path_parse_t *ctx)
985 {
986   fib_route_path_t *path;
987
988   if (rtnl_route_get_type (rr) < RTN_BLACKHOLE)
989     return;
990
991   /* if it already has a path, it does not need us to add one */
992   if (vec_len (ctx->paths) > 0)
993     return;
994
995   vec_add2 (ctx->paths, path, 1);
996
997   path->frp_flags = FIB_ROUTE_PATH_FLAG_NONE | ctx->type_flags;
998   path->frp_sw_if_index = ~0;
999   path->frp_proto = fib_proto_to_dpo (ctx->route_proto);
1000   path->frp_preference = ctx->preference;
1001
1002   LCP_ROUTER_DBG (" path:[%U]", format_fib_route_path, path);
1003 }
1004
1005 /*
1006  * Map of supported route types. Some types are omitted:
1007  * RTN_LOCAL - interface address addition creates these automatically
1008  * RTN_BROADCAST - same as RTN_LOCAL
1009  * RTN_UNSPEC, RTN_ANYCAST, RTN_THROW, RTN_NAT, RTN_XRESOLVE -
1010  *   There's not a VPP equivalent for these currently.
1011  */
1012 static const u8 lcp_router_route_type_valid[__RTN_MAX] = {
1013   [RTN_UNICAST] = 1,     [RTN_MULTICAST] = 1, [RTN_BLACKHOLE] = 1,
1014   [RTN_UNREACHABLE] = 1, [RTN_PROHIBIT] = 1,
1015 };
1016
1017 /* Map of fib entry flags by route type */
1018 static const fib_entry_flag_t lcp_router_route_type_feflags[__RTN_MAX] = {
1019   [RTN_LOCAL] = FIB_ENTRY_FLAG_LOCAL | FIB_ENTRY_FLAG_CONNECTED,
1020   [RTN_BROADCAST] = FIB_ENTRY_FLAG_DROP | FIB_ENTRY_FLAG_LOOSE_URPF_EXEMPT,
1021   [RTN_BLACKHOLE] = FIB_ENTRY_FLAG_DROP,
1022 };
1023
1024 /* Map of fib route path flags by route type */
1025 static const fib_route_path_flags_t
1026   lcp_router_route_type_frpflags[__RTN_MAX] = {
1027     [RTN_UNREACHABLE] = FIB_ROUTE_PATH_ICMP_UNREACH,
1028     [RTN_PROHIBIT] = FIB_ROUTE_PATH_ICMP_PROHIBIT,
1029     [RTN_BLACKHOLE] = FIB_ROUTE_PATH_DROP,
1030   };
1031
1032 static inline fib_source_t
1033 lcp_router_proto_fib_source (u8 rt_proto)
1034 {
1035   return (rt_proto <= RTPROT_STATIC) ? lcp_rt_fib_src : lcp_rt_fib_src_dynamic;
1036 }
1037
1038 static fib_entry_flag_t
1039 lcp_router_route_mk_entry_flags (uint8_t rtype, int table_id, uint8_t rproto)
1040 {
1041   fib_entry_flag_t fef = FIB_ENTRY_FLAG_NONE;
1042
1043   fef |= lcp_router_route_type_feflags[rtype];
1044   if ((rproto == RTPROT_KERNEL) || PREDICT_FALSE (255 == table_id))
1045     /* kernel proto is interface prefixes, 255 is linux's 'local' table */
1046     fef |= FIB_ENTRY_FLAG_ATTACHED | FIB_ENTRY_FLAG_CONNECTED;
1047
1048   return (fef);
1049 }
1050
1051 static void
1052 lcp_router_route_del (struct rtnl_route *rr)
1053 {
1054   fib_entry_flag_t entry_flags;
1055   uint32_t table_id;
1056   fib_prefix_t pfx;
1057   lcp_router_table_t *nlt;
1058   uint8_t rtype, rproto;
1059
1060   rtype = rtnl_route_get_type (rr);
1061   table_id = rtnl_route_get_table (rr);
1062   rproto = rtnl_route_get_protocol (rr);
1063
1064   /* skip unsupported route types and local table */
1065   if (!lcp_router_route_type_valid[rtype] || (table_id == 255))
1066     return;
1067
1068   lcp_router_route_mk_prefix (rr, &pfx);
1069   entry_flags = lcp_router_route_mk_entry_flags (rtype, table_id, rproto);
1070   nlt = lcp_router_table_find (lcp_router_table_k2f (table_id), pfx.fp_proto);
1071
1072   LCP_ROUTER_DBG ("route del: %d:%U %U", rtnl_route_get_table (rr),
1073                   format_fib_prefix, &pfx, format_fib_entry_flags,
1074                   entry_flags);
1075
1076   if (NULL == nlt)
1077     return;
1078
1079   lcp_router_route_path_parse_t np = {
1080     .route_proto = pfx.fp_proto,
1081     .type_flags = lcp_router_route_type_frpflags[rtype],
1082   };
1083
1084   rtnl_route_foreach_nexthop (rr, lcp_router_route_path_parse, &np);
1085   lcp_router_route_path_add_special (rr, &np);
1086
1087   if (0 != vec_len (np.paths))
1088     {
1089       fib_source_t fib_src;
1090
1091       fib_src = lcp_router_proto_fib_source (rproto);
1092
1093       if (pfx.fp_proto == FIB_PROTOCOL_IP6)
1094         fib_table_entry_delete (nlt->nlt_fib_index, &pfx, fib_src);
1095       else
1096         fib_table_entry_path_remove2 (nlt->nlt_fib_index, &pfx, fib_src,
1097                                       np.paths);
1098     }
1099
1100   vec_free (np.paths);
1101
1102   lcp_router_table_unlock (nlt);
1103 }
1104
1105 static void
1106 lcp_router_route_add (struct rtnl_route *rr)
1107 {
1108   fib_entry_flag_t entry_flags;
1109   uint32_t table_id;
1110   fib_prefix_t pfx;
1111   lcp_router_table_t *nlt;
1112   uint8_t rtype, rproto;
1113
1114   rtype = rtnl_route_get_type (rr);
1115   table_id = rtnl_route_get_table (rr);
1116   rproto = rtnl_route_get_protocol (rr);
1117
1118   /* skip unsupported route types and local table */
1119   if (!lcp_router_route_type_valid[rtype] || (table_id == 255))
1120     return;
1121
1122   lcp_router_route_mk_prefix (rr, &pfx);
1123   entry_flags = lcp_router_route_mk_entry_flags (rtype, table_id, rproto);
1124
1125   nlt = lcp_router_table_add_or_lock (table_id, pfx.fp_proto);
1126   /* Skip any kernel routes and IPv6 LL or multicast routes */
1127   if (rproto == RTPROT_KERNEL ||
1128       (FIB_PROTOCOL_IP6 == pfx.fp_proto &&
1129        (ip6_address_is_multicast (&pfx.fp_addr.ip6) ||
1130         ip6_address_is_link_local_unicast (&pfx.fp_addr.ip6))))
1131     {
1132       LCP_ROUTER_DBG ("route skip: %d:%U %U", rtnl_route_get_table (rr),
1133                       format_fib_prefix, &pfx, format_fib_entry_flags,
1134                       entry_flags);
1135     }
1136   else
1137     {
1138       LCP_ROUTER_DBG ("route add: %d:%U %U", rtnl_route_get_table (rr),
1139                       format_fib_prefix, &pfx, format_fib_entry_flags,
1140                       entry_flags);
1141
1142       lcp_router_route_path_parse_t np = {
1143         .route_proto = pfx.fp_proto,
1144         .is_mcast = (rtype == RTN_MULTICAST),
1145         .type_flags = lcp_router_route_type_frpflags[rtype],
1146         .preference = (u8) rtnl_route_get_priority (rr),
1147       };
1148
1149       rtnl_route_foreach_nexthop (rr, lcp_router_route_path_parse, &np);
1150       lcp_router_route_path_add_special (rr, &np);
1151
1152       if (0 != vec_len (np.paths))
1153         {
1154           if (rtype == RTN_MULTICAST)
1155             {
1156               /* it's not clear to me how linux expresses the RPF paramters
1157                * so we'll allow from all interfaces and hope for the best */
1158               mfib_prefix_t mpfx = {};
1159
1160               lcp_router_route_mk_mprefix (rr, &mpfx);
1161
1162               mfib_table_entry_update (
1163                 nlt->nlt_mfib_index, &mpfx, MFIB_SOURCE_PLUGIN_LOW,
1164                 MFIB_RPF_ID_NONE, MFIB_ENTRY_FLAG_ACCEPT_ALL_ITF);
1165
1166               mfib_table_entry_paths_update (nlt->nlt_mfib_index, &mpfx,
1167                                              MFIB_SOURCE_PLUGIN_LOW,
1168                                              MFIB_ENTRY_FLAG_NONE, np.paths);
1169             }
1170           else
1171             {
1172               fib_source_t fib_src;
1173
1174               fib_src = lcp_router_proto_fib_source (rproto);
1175
1176               if (pfx.fp_proto == FIB_PROTOCOL_IP6)
1177                 fib_table_entry_path_add2 (nlt->nlt_fib_index, &pfx, fib_src,
1178                                            entry_flags, np.paths);
1179               else
1180                 fib_table_entry_update (nlt->nlt_fib_index, &pfx, fib_src,
1181                                         entry_flags, np.paths);
1182             }
1183         }
1184       else
1185         LCP_ROUTER_DBG ("no paths for route add: %d:%U %U",
1186                         rtnl_route_get_table (rr), format_fib_prefix, &pfx,
1187                         format_fib_entry_flags, entry_flags);
1188       vec_free (np.paths);
1189     }
1190 }
1191
1192 static void
1193 lcp_router_route_sync_begin (void)
1194 {
1195   lcp_router_table_t *nlt;
1196
1197   pool_foreach (nlt, lcp_router_table_pool)
1198     {
1199       fib_table_mark (nlt->nlt_fib_index, nlt->nlt_proto, lcp_rt_fib_src);
1200       fib_table_mark (nlt->nlt_fib_index, nlt->nlt_proto,
1201                       lcp_rt_fib_src_dynamic);
1202
1203       LCP_ROUTER_INFO ("Begin synchronization of %U routes in table %u",
1204                        format_fib_protocol, nlt->nlt_proto,
1205                        nlt->nlt_fib_index);
1206     }
1207 }
1208
1209 static void
1210 lcp_router_route_sync_end (void)
1211 {
1212   lcp_router_table_t *nlt;
1213
1214   pool_foreach (nlt, lcp_router_table_pool)
1215     {
1216       fib_table_sweep (nlt->nlt_fib_index, nlt->nlt_proto, lcp_rt_fib_src);
1217       fib_table_sweep (nlt->nlt_fib_index, nlt->nlt_proto,
1218                        lcp_rt_fib_src_dynamic);
1219
1220       LCP_ROUTER_INFO ("End synchronization of %U routes in table %u",
1221                        format_fib_protocol, nlt->nlt_proto,
1222                        nlt->nlt_fib_index);
1223     }
1224 }
1225
1226 typedef struct lcp_router_table_flush_ctx_t_
1227 {
1228   fib_node_index_t *lrtf_entries;
1229   u32 *lrtf_sw_if_index_to_bool;
1230   fib_source_t lrtf_source;
1231 } lcp_router_table_flush_ctx_t;
1232
1233 static fib_table_walk_rc_t
1234 lcp_router_table_flush_cb (fib_node_index_t fib_entry_index, void *arg)
1235 {
1236   lcp_router_table_flush_ctx_t *ctx = arg;
1237   u32 sw_if_index;
1238
1239   sw_if_index = fib_entry_get_resolving_interface_for_source (
1240     fib_entry_index, ctx->lrtf_source);
1241
1242   if (sw_if_index < vec_len (ctx->lrtf_sw_if_index_to_bool) &&
1243       ctx->lrtf_sw_if_index_to_bool[sw_if_index])
1244     {
1245       vec_add1 (ctx->lrtf_entries, fib_entry_index);
1246     }
1247   return (FIB_TABLE_WALK_CONTINUE);
1248 }
1249
1250 static void
1251 lcp_router_table_flush (lcp_router_table_t *nlt, u32 *sw_if_index_to_bool,
1252                         fib_source_t source)
1253 {
1254   fib_node_index_t *fib_entry_index;
1255   lcp_router_table_flush_ctx_t ctx = {
1256     .lrtf_entries = NULL,
1257     .lrtf_sw_if_index_to_bool = sw_if_index_to_bool,
1258     .lrtf_source = source,
1259   };
1260
1261   LCP_ROUTER_DBG (
1262     "Flush table: proto %U, fib-index %u, max sw_if_index %u, source %U",
1263     format_fib_protocol, nlt->nlt_proto, nlt->nlt_fib_index,
1264     vec_len (sw_if_index_to_bool) - 1, format_fib_source, source);
1265
1266   fib_table_walk (nlt->nlt_fib_index, nlt->nlt_proto,
1267                   lcp_router_table_flush_cb, &ctx);
1268
1269   LCP_ROUTER_DBG ("Flush table: entries number to delete %u",
1270                   vec_len (ctx.lrtf_entries));
1271
1272   vec_foreach (fib_entry_index, ctx.lrtf_entries)
1273     {
1274       fib_table_entry_delete_index (*fib_entry_index, source);
1275       lcp_router_table_unlock (nlt);
1276     }
1277
1278   vec_free (ctx.lrtf_entries);
1279 }
1280
1281 const nl_vft_t lcp_router_vft = {
1282   .nvl_rt_link_add = { .is_mp_safe = 0, .cb = lcp_router_link_add },
1283   .nvl_rt_link_del = { .is_mp_safe = 0, .cb = lcp_router_link_del },
1284   .nvl_rt_link_sync_begin = { .is_mp_safe = 0,
1285                               .cb = lcp_router_link_sync_begin },
1286   .nvl_rt_link_sync_end = { .is_mp_safe = 0, .cb = lcp_router_link_sync_end },
1287   .nvl_rt_addr_add = { .is_mp_safe = 0, .cb = lcp_router_link_addr_add },
1288   .nvl_rt_addr_del = { .is_mp_safe = 0, .cb = lcp_router_link_addr_del },
1289   .nvl_rt_addr_sync_begin = { .is_mp_safe = 0,
1290                               .cb = lcp_router_link_addr_sync_begin },
1291   .nvl_rt_addr_sync_end = { .is_mp_safe = 0,
1292                             .cb = lcp_router_link_addr_sync_end },
1293   .nvl_rt_neigh_add = { .is_mp_safe = 0, .cb = lcp_router_neigh_add },
1294   .nvl_rt_neigh_del = { .is_mp_safe = 0, .cb = lcp_router_neigh_del },
1295   .nvl_rt_neigh_sync_begin = { .is_mp_safe = 0,
1296                                .cb = lcp_router_neigh_sync_begin },
1297   .nvl_rt_neigh_sync_end = { .is_mp_safe = 0,
1298                              .cb = lcp_router_neigh_sync_end },
1299   .nvl_rt_route_add = { .is_mp_safe = 1, .cb = lcp_router_route_add },
1300   .nvl_rt_route_del = { .is_mp_safe = 1, .cb = lcp_router_route_del },
1301   .nvl_rt_route_sync_begin = { .is_mp_safe = 0,
1302                                .cb = lcp_router_route_sync_begin },
1303   .nvl_rt_route_sync_end = { .is_mp_safe = 0,
1304                              .cb = lcp_router_route_sync_end },
1305 };
1306
1307 static clib_error_t *
1308 lcp_router_init (vlib_main_t *vm)
1309 {
1310   lcp_router_logger = vlib_log_register_class ("linux-cp", "router");
1311
1312   nl_register_vft (&lcp_router_vft);
1313
1314   /*
1315    * allocate 2 route sources. The low priority source will be for
1316    * dynamic routes. If a dynamic route daemon (FRR) tries to remove its
1317    * route, it will use the low priority source to ensure it will not
1318    * remove static routes which were added with the higher priority source.
1319    */
1320   lcp_rt_fib_src =
1321     fib_source_allocate ("lcp-rt", FIB_SOURCE_PRIORITY_HI, FIB_SOURCE_BH_API);
1322
1323   lcp_rt_fib_src_dynamic = fib_source_allocate (
1324     "lcp-rt-dynamic", FIB_SOURCE_PRIORITY_HI + 1, FIB_SOURCE_BH_API);
1325
1326   return (NULL);
1327 }
1328
1329 VLIB_INIT_FUNCTION (lcp_router_init) = {
1330   .runs_before = VLIB_INITS ("lcp_nl_init"),
1331 };
1332
1333 /*
1334  * fd.io coding-style-patch-verification: ON
1335  *
1336  * Local Variables:
1337  * eval: (c-set-style "gnu")
1338  * End:
1339  */