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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <sys/socket.h>
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>
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>
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>
39 typedef struct lcp_router_table_t_
42 fib_protocol_t nlt_proto;
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;
52 const static fib_prefix_t pfx_all1s = {
58 .fp_proto = FIB_PROTOCOL_IP4,
62 static fib_source_t lcp_rt_fib_src;
63 static fib_source_t lcp_rt_fib_src_dynamic;
65 #define LCP_ROUTER_DBG(...) vlib_log_debug (lcp_router_logger, __VA_ARGS__);
67 #define LCP_ROUTER_INFO(...) vlib_log_notice (lcp_router_logger, __VA_ARGS__);
69 #define LCP_ROUTER_ERROR(...) vlib_log_err (lcp_router_logger, __VA_ARGS__);
71 static const mfib_prefix_t ip4_specials[] = {
72 /* ALL prefixes are in network order */
74 /* (*,224.0.0.0)/24 - all local subnet */
76 .ip4.data_u32 = 0x000000e0,
79 .fp_proto = FIB_PROTOCOL_IP4,
83 static const mfib_prefix_t ip6_specials[] = {
84 /* ALL prefixes are in network order */
86 /* (*,ff00::)/8 - all local subnet */
88 .ip6.as_u64[0] = 0x00000000000000ff,
91 .fp_proto = FIB_PROTOCOL_IP6,
95 /* VIF to PHY DB of managed interfaces */
96 static uword *lcp_routing_itf_db;
99 lcp_router_intf_h2p (u32 host)
106 * first check the linux side created interface (i.e. vlans, tunnels etc)
108 p = hash_get (lcp_routing_itf_db, host);
114 * then check the paired phys
116 lipi = lcp_itf_pair_find_by_vif (host);
118 if (INDEX_INVALID == lipi)
121 lip = lcp_itf_pair_get (lipi);
123 return lip->lip_phy_sw_if_index;
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.
134 lcp_router_lip_ts_check (nl_msg_info_t *msg_info, lcp_itf_pair_t *lip)
139 if (msg_info->ts > lip->lip_create_ts)
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);
149 lcp_router_link_del (struct rtnl_link *rl, void *ctx)
153 if (!lcp_auto_subint ())
156 lipi = lcp_itf_pair_find_by_vif (rtnl_link_get_ifindex (rl));
158 if (INDEX_INVALID != lipi)
162 lip = lcp_itf_pair_get (lipi);
164 if (lcp_router_lip_ts_check ((nl_msg_info_t *) ctx, lip))
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);
172 if (rtnl_link_is_vlan (rl))
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);
182 LCP_ROUTER_INFO ("ignore link del: %s - %s", rtnl_link_get_type (rl),
183 rtnl_link_get_name (rl));
187 lcp_router_ip4_mroutes_add_del (u32 sw_if_index, u8 is_add)
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,
195 .frp_mitf_flags = MFIB_ITF_FLAG_ACCEPT,
201 mfib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index);
203 for (ii = 0; ii < ARRAY_LEN (ip4_specials); ii++)
207 mfib_table_entry_path_update (mfib_index, &ip4_specials[ii],
208 MFIB_SOURCE_PLUGIN_LOW,
209 MFIB_ENTRY_FLAG_NONE, &path);
213 mfib_table_entry_path_remove (mfib_index, &ip4_specials[ii],
214 MFIB_SOURCE_PLUGIN_LOW, &path);
220 lcp_router_ip6_mroutes_add_del (u32 sw_if_index, u8 is_add)
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,
228 .frp_mitf_flags = MFIB_ITF_FLAG_ACCEPT,
234 mfib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6, sw_if_index);
236 for (ii = 0; ii < ARRAY_LEN (ip6_specials); ii++)
240 mfib_table_entry_path_update (mfib_index, &ip6_specials[ii],
241 MFIB_SOURCE_PLUGIN_LOW,
242 MFIB_ENTRY_FLAG_NONE, &path);
246 mfib_table_entry_path_remove (mfib_index, &ip6_specials[ii],
247 MFIB_SOURCE_PLUGIN_LOW, &path);
253 lcp_router_link_mtu (struct rtnl_link *rl, u32 sw_if_index)
255 vnet_main_t *vnm = vnet_get_main ();
257 vnet_sw_interface_t *sw;
258 vnet_hw_interface_t *hw;
260 mtu = rtnl_link_get_mtu (rl);
264 sw = vnet_get_sw_interface (vnm, sw_if_index);
265 hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
267 /* If HW interface, try to change hw link */
268 if ((sw->sw_if_index == 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);
272 vnet_sw_interface_set_mtu (vnm, sw->sw_if_index, mtu);
276 lcp_router_link_addr (struct rtnl_link *rl, lcp_itf_pair_t *lip)
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;
284 mac_addr = rtnl_link_get_addr (rl);
285 if (!mac_addr || (nl_addr_get_family (mac_addr) != AF_LLC))
288 sw = vnet_get_sw_interface (vnm, lip->lip_phy_sw_if_index);
290 /* can only change address on hw interface */
291 if (sw->sw_if_index != sw->sup_sw_if_index)
294 hw = vnet_get_sup_hw_interface (vnm, lip->lip_phy_sw_if_index);
295 if (!vec_len (hw->hw_address))
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,
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]);
310 static void lcp_router_table_flush (lcp_router_table_t *nlt,
311 u32 *sw_if_index_to_bool,
312 fib_source_t source);
315 lcp_router_link_add (struct rtnl_link *rl, void *ctx)
319 vnet_main_t *vnm = vnet_get_main ();
321 lipi = lcp_itf_pair_find_by_vif (rtnl_link_get_ifindex (rl));
322 up = IFF_UP & rtnl_link_get_flags (rl);
324 if (INDEX_INVALID != lipi)
330 lip = lcp_itf_pair_get (lipi);
331 if (!vnet_get_sw_interface (vnm, lip->lip_phy_sw_if_index))
334 if (lcp_router_lip_ts_check ((nl_msg_info_t *) ctx, lip))
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);
343 vnet_sw_interface_admin_up (vnet_get_main (),
344 lip->lip_phy_sw_if_index);
346 else if (sw_if_up && !up)
348 vnet_sw_interface_admin_down (vnet_get_main (),
349 lip->lip_phy_sw_if_index);
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.
360 lcp_router_table_t *nlt;
362 fib_index = fib_table_get_index_for_sw_if_index (
363 FIB_PROTOCOL_IP4, lip->lip_phy_sw_if_index);
365 pool_foreach (nlt, lcp_router_table_pool)
367 if (fib_index == nlt->nlt_fib_index &&
368 FIB_PROTOCOL_IP4 == nlt->nlt_proto)
370 u32 *sw_if_index_to_bool = NULL;
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;
376 lcp_router_table_flush (nlt, sw_if_index_to_bool,
378 lcp_router_table_flush (nlt, sw_if_index_to_bool,
379 lcp_rt_fib_src_dynamic);
381 vec_free (sw_if_index_to_bool);
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"));
393 lcp_router_link_mtu (rl, lip->lip_phy_sw_if_index);
394 lcp_router_link_addr (rl, lip);
396 else if (lcp_auto_subint () && rtnl_link_is_vlan (rl))
398 /* Find the pair based on the parent VIF */
399 lipi = lcp_itf_pair_find_by_vif (rtnl_link_get_link (rl));
401 if (INDEX_INVALID != lipi)
403 u32 sub_phy_sw_if_index, sub_host_sw_if_index;
404 const lcp_itf_pair_t *lip;
406 u8 *ns = 0; /* FIXME */
408 lip = lcp_itf_pair_get (lipi);
410 vlan = rtnl_link_vlan_get_id (rl);
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))
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);
423 /* pool could grow during the previous operation */
424 lip = lcp_itf_pair_get (lipi);
426 /* create the vlan interface on the parent host */
427 if (vnet_create_sub_interface (lip->lip_host_sw_if_index, vlan, 18,
428 0, vlan, &sub_host_sw_if_index))
430 LCP_ROUTER_INFO ("failed create vlan: %s on %U",
431 rtnl_link_get_name (rl),
432 format_vnet_sw_if_index_name, vnet_get_main (),
433 lip->lip_host_sw_if_index);
441 "create vlan: %s -> (%U, %U) : (%U, %U)", rtnl_link_get_name (rl),
442 format_vnet_sw_if_index_name, vnet_get_main (),
443 lip->lip_phy_sw_if_index, format_vnet_sw_if_index_name,
444 vnet_get_main (), sub_phy_sw_if_index,
445 format_vnet_sw_if_index_name, vnet_get_main (),
446 lip->lip_host_sw_if_index, format_vnet_sw_if_index_name,
447 vnet_get_main (), sub_host_sw_if_index);
449 if ((if_name = rtnl_link_get_name (rl)) != NULL)
450 vec_validate_init_c_string (if_namev, if_name,
451 strnlen (if_name, IFNAMSIZ));
452 lcp_itf_pair_add (sub_host_sw_if_index, sub_phy_sw_if_index,
453 if_namev, rtnl_link_get_ifindex (rl),
454 lip->lip_host_type, ns);
456 vnet_sw_interface_admin_up (vnet_get_main (), sub_phy_sw_if_index);
457 vnet_sw_interface_admin_up (vnet_get_main (), sub_host_sw_if_index);
463 LCP_ROUTER_INFO ("ignore parent-link add: %s - %s",
464 rtnl_link_get_type (rl), rtnl_link_get_name (rl));
468 LCP_ROUTER_INFO ("ignore link add: %s - %s", rtnl_link_get_type (rl),
469 rtnl_link_get_name (rl));
473 lcp_router_link_sync_begin (void)
475 LCP_ROUTER_INFO ("Begin synchronization of interface configurations");
479 lcp_router_link_sync_end (void)
481 LCP_ROUTER_INFO ("End synchronization of interface configurations");
484 static clib_error_t *
485 lcp_router_link_up_down (vnet_main_t *vnm, u32 hw_if_index, u32 flags)
487 vnet_hw_interface_t *hi;
490 hi = vnet_get_hw_interface_or_null (vnm, hw_if_index);
494 lipi = lcp_itf_pair_find_by_phy (hi->sw_if_index);
495 if (lipi == INDEX_INVALID)
498 /* When the link goes down on an interface, the kernel processes routes which
499 * resolve through that interface depending on how they were created:
500 * - Legacy Route API: the kernel retains the routes and marks them as
502 * - Nexthop API: the kernel removes the next-hop objects and the routes
503 * which reference them.
505 * For IPv4 routes created with Nexthop API, the kernel will not send any
506 * explicit RTM_DELROUTE messages about removing them. In order to
507 * synchronize with the kernel, affected routes need to be manually removed
510 * The behavior is different for IPv6 routes created with Nexthop API. The
511 * kernel will send explicit RTM_DELROUTE messages about IPv6 routes being
514 if (!(flags & VNET_HW_INTERFACE_FLAG_LINK_UP) &&
515 (lcp_get_del_static_on_link_down () ||
516 lcp_get_del_dynamic_on_link_down ()))
519 u32 **fib_index_to_sw_if_index_to_bool = NULL;
521 lcp_router_table_t *nlt;
523 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
526 vec_validate_init_empty (fib_index_to_sw_if_index_to_bool, fib_index,
528 vec_validate_init_empty (fib_index_to_sw_if_index_to_bool[fib_index],
529 hi->sw_if_index, false);
530 fib_index_to_sw_if_index_to_bool[fib_index][hi->sw_if_index] = true;
532 /* clang-format off */
533 hash_foreach (id, sw_if_index, hi->sub_interface_sw_if_index_by_id,
535 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
537 vec_validate_init_empty (fib_index_to_sw_if_index_to_bool, fib_index,
539 vec_validate_init_empty (fib_index_to_sw_if_index_to_bool[fib_index],
541 fib_index_to_sw_if_index_to_bool[fib_index][sw_if_index] = true;
543 /* clang-format on */
545 vec_foreach_index (fib_index, fib_index_to_sw_if_index_to_bool)
547 u32 *sw_if_index_to_bool;
549 sw_if_index_to_bool = fib_index_to_sw_if_index_to_bool[fib_index];
550 if (NULL == sw_if_index_to_bool)
553 pool_foreach (nlt, lcp_router_table_pool)
555 if (fib_index == nlt->nlt_fib_index &&
556 FIB_PROTOCOL_IP4 == nlt->nlt_proto)
558 if (lcp_get_del_static_on_link_down ())
559 lcp_router_table_flush (nlt, sw_if_index_to_bool,
561 if (lcp_get_del_dynamic_on_link_down ())
562 lcp_router_table_flush (nlt, sw_if_index_to_bool,
563 lcp_rt_fib_src_dynamic);
568 vec_free (sw_if_index_to_bool);
571 vec_free (fib_index_to_sw_if_index_to_bool);
577 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (lcp_router_link_up_down);
579 static fib_protocol_t
580 lcp_router_proto_k2f (uint32_t k)
583 return (FIB_PROTOCOL_IP6);
584 return (FIB_PROTOCOL_IP4);
588 lcp_router_mk_addr (const struct nl_addr *rna, ip_address_t *ia)
590 fib_protocol_t fproto;
592 ip_address_reset (ia);
593 fproto = lcp_router_proto_k2f (nl_addr_get_family (rna));
595 ip_address_set (ia, nl_addr_get_binary_addr (rna),
596 FIB_PROTOCOL_IP4 == fproto ? AF_IP4 : AF_IP6);
599 static fib_protocol_t
600 lcp_router_mk_addr46 (const struct nl_addr *rna, ip46_address_t *ia)
602 fib_protocol_t fproto;
604 fproto = lcp_router_proto_k2f (nl_addr_get_family (rna));
605 ip46_address_reset (ia);
606 if (FIB_PROTOCOL_IP4 == fproto)
607 memcpy (&ia->ip4, nl_addr_get_binary_addr (rna), nl_addr_get_len (rna));
609 memcpy (&ia->ip6, nl_addr_get_binary_addr (rna), nl_addr_get_len (rna));
615 lcp_router_link_addr_add_del (struct rtnl_addr *rla, int is_del)
619 sw_if_index = lcp_router_intf_h2p (rtnl_addr_get_ifindex (rla));
621 if (~0 != sw_if_index)
625 lcp_router_mk_addr (rtnl_addr_get_local (rla), &nh);
627 if (AF_IP4 == ip_addr_version (&nh))
629 ip4_add_del_interface_address (
630 vlib_get_main (), sw_if_index, &ip_addr_v4 (&nh),
631 rtnl_addr_get_prefixlen (rla), is_del);
632 lcp_router_ip4_mroutes_add_del (sw_if_index, !is_del);
634 else if (AF_IP6 == ip_addr_version (&nh))
636 if (ip6_address_is_link_local_unicast (&ip_addr_v6 (&nh)))
638 ip6_link_disable (sw_if_index);
641 ip6_link_enable (sw_if_index, NULL);
642 ip6_link_set_local_address (sw_if_index, &ip_addr_v6 (&nh));
645 ip6_add_del_interface_address (
646 vlib_get_main (), sw_if_index, &ip_addr_v6 (&nh),
647 rtnl_addr_get_prefixlen (rla), is_del);
648 lcp_router_ip6_mroutes_add_del (sw_if_index, !is_del);
651 LCP_ROUTER_DBG ("link-addr: %U %U/%d", format_vnet_sw_if_index_name,
652 vnet_get_main (), sw_if_index, format_ip_address, &nh,
653 rtnl_addr_get_prefixlen (rla));
658 lcp_router_link_addr_del (struct rtnl_addr *la)
660 lcp_router_link_addr_add_del (la, 1);
664 lcp_router_link_addr_add (struct rtnl_addr *la)
666 lcp_router_link_addr_add_del (la, 0);
670 lcp_router_address_mark (index_t index, void *ctx)
672 vnet_main_t *vnm = vnet_get_main ();
674 lcp_itf_pair_t *lip = lcp_itf_pair_get (index);
676 return WALK_CONTINUE;
678 ip_interface_address_mark_one_interface (
679 vnm, vnet_get_sw_interface (vnm, lip->lip_phy_sw_if_index), 0);
681 return WALK_CONTINUE;
685 lcp_router_link_addr_sync_begin (void)
687 lcp_itf_pair_walk (lcp_router_address_mark, 0);
689 LCP_ROUTER_INFO ("Begin synchronization of interface addresses");
693 lcp_router_link_addr_sync_end (void)
695 ip_interface_address_sweep ();
697 LCP_ROUTER_INFO ("End synchronization of interface addresses");
701 lcp_router_mk_mac_addr (const struct nl_addr *rna, mac_address_t *mac)
703 mac_address_from_bytes (mac, nl_addr_get_binary_addr (rna));
707 lcp_router_neigh_del (struct rtnl_neigh *rn)
711 sw_if_index = lcp_router_intf_h2p (rtnl_neigh_get_ifindex (rn));
713 if (~0 != sw_if_index)
719 if ((rna = rtnl_neigh_get_dst (rn)) == NULL)
721 lcp_router_mk_addr (rna, &nh);
723 if (ip46_address_is_multicast (&ip_addr_46 (&nh)))
725 LCP_ROUTER_DBG ("ignore neighbor del: %U %U", format_ip_address, &nh,
726 format_vnet_sw_if_index_name, vnet_get_main (),
731 rv = ip_neighbor_del (&nh, sw_if_index);
736 "Failed to delete neighbor: %U %U", format_ip_address, &nh,
737 format_vnet_sw_if_index_name, vnet_get_main (), sw_if_index);
741 LCP_ROUTER_DBG ("neighbor del: %U %U", format_ip_address, &nh,
742 format_vnet_sw_if_index_name, vnet_get_main (),
747 LCP_ROUTER_INFO ("ignore neighbour del on: %d",
748 rtnl_neigh_get_ifindex (rn));
753 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE | NUD_PROBE | NUD_STALE | \
758 lcp_router_neigh_add (struct rtnl_neigh *rn)
762 sw_if_index = lcp_router_intf_h2p (rtnl_neigh_get_ifindex (rn));
764 if (~0 != sw_if_index)
771 if ((rna = rtnl_neigh_get_dst (rn)) == NULL)
773 lcp_router_mk_addr (rna, &nh);
775 if (ip46_address_is_multicast (&ip_addr_46 (&nh)))
777 LCP_ROUTER_DBG ("ignore neighbor add: %U %U", format_ip_address, &nh,
778 format_vnet_sw_if_index_name, vnet_get_main (),
783 ll = rtnl_neigh_get_lladdr (rn);
784 state = rtnl_neigh_get_state (rn);
786 if (ll && (state & NUD_VALID))
789 ip_neighbor_flags_t flags;
792 lcp_router_mk_mac_addr (ll, &mac);
794 if (state & (NUD_NOARP | NUD_PERMANENT))
795 flags = IP_NEIGHBOR_FLAG_STATIC;
797 flags = IP_NEIGHBOR_FLAG_DYNAMIC;
799 rv = ip_neighbor_add (&nh, &mac, sw_if_index, flags, NULL);
804 "Failed to create neighbor: %U %U", format_ip_address, &nh,
805 format_vnet_sw_if_index_name, vnet_get_main (), sw_if_index);
809 LCP_ROUTER_DBG ("neighbor add: %U %U", format_ip_address, &nh,
810 format_vnet_sw_if_index_name, vnet_get_main (),
816 lcp_router_neigh_del (rn);
819 LCP_ROUTER_INFO ("ignore neighbour add on: %d",
820 rtnl_neigh_get_ifindex (rn));
824 lcp_router_neighbor_mark (index_t index, void *ctx)
826 lcp_itf_pair_t *lip = lcp_itf_pair_get (index);
828 return WALK_CONTINUE;
830 ip_neighbor_walk (AF_IP4, lip->lip_phy_sw_if_index, ip_neighbor_mark_one, 0);
831 ip_neighbor_walk (AF_IP6, lip->lip_phy_sw_if_index, ip_neighbor_mark_one, 0);
833 return WALK_CONTINUE;
837 lcp_router_neigh_sync_begin (void)
839 lcp_itf_pair_walk (lcp_router_neighbor_mark, 0);
841 LCP_ROUTER_INFO ("Begin synchronization of neighbors");
845 lcp_router_neigh_sync_end (void)
847 ip_neighbor_sweep (AF_IP4);
848 ip_neighbor_sweep (AF_IP6);
850 LCP_ROUTER_INFO ("End synchronization of neighbors");
853 static lcp_router_table_t *
854 lcp_router_table_find (uint32_t id, fib_protocol_t fproto)
858 p = hash_get (lcp_router_table_db[fproto], id);
861 return pool_elt_at_index (lcp_router_table_pool, p[0]);
867 lcp_router_table_k2f (uint32_t k)
869 // the kernel's table ID 255 is the default table
870 if (k == 255 || k == 254)
875 static lcp_router_table_t *
876 lcp_router_table_add_or_lock (uint32_t id, fib_protocol_t fproto)
878 lcp_router_table_t *nlt;
880 id = lcp_router_table_k2f (id);
881 nlt = lcp_router_table_find (id, fproto);
885 pool_get_zero (lcp_router_table_pool, nlt);
888 nlt->nlt_proto = fproto;
890 nlt->nlt_fib_index = fib_table_find_or_create_and_lock (
891 nlt->nlt_proto, nlt->nlt_id, lcp_rt_fib_src);
892 nlt->nlt_mfib_index = mfib_table_find_or_create_and_lock (
893 nlt->nlt_proto, nlt->nlt_id, MFIB_SOURCE_PLUGIN_LOW);
895 hash_set (lcp_router_table_db[fproto], nlt->nlt_id,
896 nlt - lcp_router_table_pool);
898 if (FIB_PROTOCOL_IP4 == fproto)
900 /* Set the all 1s address in this table to punt */
901 fib_table_entry_special_add (nlt->nlt_fib_index, &pfx_all1s,
902 lcp_rt_fib_src, FIB_ENTRY_FLAG_LOCAL);
904 const fib_route_path_t path = {
905 .frp_proto = DPO_PROTO_IP4,
906 .frp_addr = zero_addr,
907 .frp_sw_if_index = ~0,
910 .frp_mitf_flags = MFIB_ITF_FLAG_FORWARD,
911 .frp_flags = FIB_ROUTE_PATH_LOCAL,
915 for (ii = 0; ii < ARRAY_LEN (ip4_specials); ii++)
917 mfib_table_entry_path_update (
918 nlt->nlt_mfib_index, &ip4_specials[ii], MFIB_SOURCE_PLUGIN_LOW,
919 MFIB_ENTRY_FLAG_NONE, &path);
922 else if (FIB_PROTOCOL_IP6 == fproto)
924 const fib_route_path_t path = {
925 .frp_proto = DPO_PROTO_IP6,
926 .frp_addr = zero_addr,
927 .frp_sw_if_index = ~0,
930 .frp_mitf_flags = MFIB_ITF_FLAG_FORWARD,
931 .frp_flags = FIB_ROUTE_PATH_LOCAL,
935 for (ii = 0; ii < ARRAY_LEN (ip6_specials); ii++)
937 mfib_table_entry_path_update (
938 nlt->nlt_mfib_index, &ip6_specials[ii], MFIB_SOURCE_PLUGIN_LOW,
939 MFIB_ENTRY_FLAG_NONE, &path);
950 lcp_router_table_unlock (lcp_router_table_t *nlt)
954 if (0 == nlt->nlt_refs)
956 if (FIB_PROTOCOL_IP4 == nlt->nlt_proto)
958 /* Set the all 1s address in this table to punt */
959 fib_table_entry_special_remove (nlt->nlt_fib_index, &pfx_all1s,
963 fib_table_unlock (nlt->nlt_fib_index, nlt->nlt_proto, lcp_rt_fib_src);
965 hash_unset (lcp_router_table_db[nlt->nlt_proto], nlt->nlt_id);
966 pool_put (lcp_router_table_pool, nlt);
971 lcp_router_route_mk_prefix (struct rtnl_route *r, fib_prefix_t *p)
973 const struct nl_addr *addr = rtnl_route_get_dst (r);
975 p->fp_len = nl_addr_get_prefixlen (addr);
976 p->fp_proto = lcp_router_mk_addr46 (addr, &p->fp_addr);
980 lcp_router_route_mk_mprefix (struct rtnl_route *r, mfib_prefix_t *p)
982 const struct nl_addr *addr;
984 addr = rtnl_route_get_dst (r);
986 p->fp_len = nl_addr_get_prefixlen (addr);
987 p->fp_proto = lcp_router_mk_addr46 (addr, &p->fp_grp_addr);
989 addr = rtnl_route_get_src (r);
991 p->fp_proto = lcp_router_mk_addr46 (addr, &p->fp_src_addr);
994 typedef struct lcp_router_route_path_parse_t_
996 fib_route_path_t *paths;
997 fib_protocol_t route_proto;
999 fib_route_path_flags_t type_flags;
1001 } lcp_router_route_path_parse_t;
1004 lcp_router_route_path_parse (struct rtnl_nexthop *rnh, void *arg)
1006 lcp_router_route_path_parse_t *ctx = arg;
1007 fib_route_path_t *path;
1010 sw_if_index = lcp_router_intf_h2p (rtnl_route_nh_get_ifindex (rnh));
1012 if (~0 != sw_if_index)
1014 fib_protocol_t fproto;
1015 struct nl_addr *addr;
1017 vec_add2 (ctx->paths, path, 1);
1019 path->frp_flags = FIB_ROUTE_PATH_FLAG_NONE | ctx->type_flags;
1020 path->frp_sw_if_index = sw_if_index;
1021 path->frp_weight = rtnl_route_nh_get_weight (rnh);
1022 path->frp_preference = ctx->preference;
1024 addr = rtnl_route_nh_get_gateway (rnh);
1026 addr = rtnl_route_nh_get_via (rnh);
1029 fproto = lcp_router_mk_addr46 (addr, &path->frp_addr);
1031 fproto = ctx->route_proto;
1033 path->frp_proto = fib_proto_to_dpo (fproto);
1036 path->frp_mitf_flags = MFIB_ITF_FLAG_FORWARD;
1038 LCP_ROUTER_DBG (" path:[%U]", format_fib_route_path, path);
1043 * blackhole, unreachable, prohibit will not have a next hop in an
1044 * RTM_NEWROUTE. Add a path for them.
1047 lcp_router_route_path_add_special (struct rtnl_route *rr,
1048 lcp_router_route_path_parse_t *ctx)
1050 fib_route_path_t *path;
1052 if (rtnl_route_get_type (rr) < RTN_BLACKHOLE)
1055 /* if it already has a path, it does not need us to add one */
1056 if (vec_len (ctx->paths) > 0)
1059 vec_add2 (ctx->paths, path, 1);
1061 path->frp_flags = FIB_ROUTE_PATH_FLAG_NONE | ctx->type_flags;
1062 path->frp_sw_if_index = ~0;
1063 path->frp_proto = fib_proto_to_dpo (ctx->route_proto);
1064 path->frp_preference = ctx->preference;
1066 LCP_ROUTER_DBG (" path:[%U]", format_fib_route_path, path);
1070 * Map of supported route types. Some types are omitted:
1071 * RTN_LOCAL - interface address addition creates these automatically
1072 * RTN_BROADCAST - same as RTN_LOCAL
1073 * RTN_UNSPEC, RTN_ANYCAST, RTN_THROW, RTN_NAT, RTN_XRESOLVE -
1074 * There's not a VPP equivalent for these currently.
1076 static const u8 lcp_router_route_type_valid[__RTN_MAX] = {
1077 [RTN_UNICAST] = 1, [RTN_MULTICAST] = 1, [RTN_BLACKHOLE] = 1,
1078 [RTN_UNREACHABLE] = 1, [RTN_PROHIBIT] = 1,
1081 /* Map of fib entry flags by route type */
1082 static const fib_entry_flag_t lcp_router_route_type_feflags[__RTN_MAX] = {
1083 [RTN_LOCAL] = FIB_ENTRY_FLAG_LOCAL | FIB_ENTRY_FLAG_CONNECTED,
1084 [RTN_BROADCAST] = FIB_ENTRY_FLAG_DROP | FIB_ENTRY_FLAG_LOOSE_URPF_EXEMPT,
1085 [RTN_BLACKHOLE] = FIB_ENTRY_FLAG_DROP,
1088 /* Map of fib route path flags by route type */
1089 static const fib_route_path_flags_t
1090 lcp_router_route_type_frpflags[__RTN_MAX] = {
1091 [RTN_UNREACHABLE] = FIB_ROUTE_PATH_ICMP_UNREACH,
1092 [RTN_PROHIBIT] = FIB_ROUTE_PATH_ICMP_PROHIBIT,
1093 [RTN_BLACKHOLE] = FIB_ROUTE_PATH_DROP,
1096 static inline fib_source_t
1097 lcp_router_proto_fib_source (u8 rt_proto)
1099 return (rt_proto <= RTPROT_STATIC) ? lcp_rt_fib_src : lcp_rt_fib_src_dynamic;
1102 static fib_entry_flag_t
1103 lcp_router_route_mk_entry_flags (uint8_t rtype, int table_id, uint8_t rproto)
1105 fib_entry_flag_t fef = FIB_ENTRY_FLAG_NONE;
1107 fef |= lcp_router_route_type_feflags[rtype];
1108 if ((rproto == RTPROT_KERNEL) || PREDICT_FALSE (255 == table_id))
1109 /* kernel proto is interface prefixes, 255 is linux's 'local' table */
1110 fef |= FIB_ENTRY_FLAG_ATTACHED | FIB_ENTRY_FLAG_CONNECTED;
1116 lcp_router_route_del (struct rtnl_route *rr)
1118 fib_entry_flag_t entry_flags;
1121 lcp_router_table_t *nlt;
1122 uint8_t rtype, rproto;
1124 rtype = rtnl_route_get_type (rr);
1125 table_id = rtnl_route_get_table (rr);
1126 rproto = rtnl_route_get_protocol (rr);
1128 /* skip unsupported route types and local table */
1129 if (!lcp_router_route_type_valid[rtype] || (table_id == 255))
1132 lcp_router_route_mk_prefix (rr, &pfx);
1133 entry_flags = lcp_router_route_mk_entry_flags (rtype, table_id, rproto);
1134 nlt = lcp_router_table_find (lcp_router_table_k2f (table_id), pfx.fp_proto);
1136 LCP_ROUTER_DBG ("route del: %d:%U %U", rtnl_route_get_table (rr),
1137 format_fib_prefix, &pfx, format_fib_entry_flags,
1143 lcp_router_route_path_parse_t np = {
1144 .route_proto = pfx.fp_proto,
1145 .type_flags = lcp_router_route_type_frpflags[rtype],
1148 rtnl_route_foreach_nexthop (rr, lcp_router_route_path_parse, &np);
1149 lcp_router_route_path_add_special (rr, &np);
1151 if (0 != vec_len (np.paths))
1153 fib_source_t fib_src;
1155 fib_src = lcp_router_proto_fib_source (rproto);
1157 if (pfx.fp_proto == FIB_PROTOCOL_IP6)
1158 fib_table_entry_delete (nlt->nlt_fib_index, &pfx, fib_src);
1160 fib_table_entry_path_remove2 (nlt->nlt_fib_index, &pfx, fib_src,
1164 vec_free (np.paths);
1166 lcp_router_table_unlock (nlt);
1170 lcp_router_route_add (struct rtnl_route *rr)
1172 fib_entry_flag_t entry_flags;
1175 lcp_router_table_t *nlt;
1176 uint8_t rtype, rproto;
1178 rtype = rtnl_route_get_type (rr);
1179 table_id = rtnl_route_get_table (rr);
1180 rproto = rtnl_route_get_protocol (rr);
1182 /* skip unsupported route types and local table */
1183 if (!lcp_router_route_type_valid[rtype] || (table_id == 255))
1186 lcp_router_route_mk_prefix (rr, &pfx);
1187 entry_flags = lcp_router_route_mk_entry_flags (rtype, table_id, rproto);
1189 nlt = lcp_router_table_add_or_lock (table_id, pfx.fp_proto);
1190 /* Skip any kernel routes and IPv6 LL or multicast routes */
1191 if (rproto == RTPROT_KERNEL ||
1192 (FIB_PROTOCOL_IP6 == pfx.fp_proto &&
1193 (ip6_address_is_multicast (&pfx.fp_addr.ip6) ||
1194 ip6_address_is_link_local_unicast (&pfx.fp_addr.ip6))))
1196 LCP_ROUTER_DBG ("route skip: %d:%U %U", rtnl_route_get_table (rr),
1197 format_fib_prefix, &pfx, format_fib_entry_flags,
1202 LCP_ROUTER_DBG ("route add: %d:%U %U", rtnl_route_get_table (rr),
1203 format_fib_prefix, &pfx, format_fib_entry_flags,
1206 lcp_router_route_path_parse_t np = {
1207 .route_proto = pfx.fp_proto,
1208 .is_mcast = (rtype == RTN_MULTICAST),
1209 .type_flags = lcp_router_route_type_frpflags[rtype],
1210 .preference = (u8) rtnl_route_get_priority (rr),
1213 rtnl_route_foreach_nexthop (rr, lcp_router_route_path_parse, &np);
1214 lcp_router_route_path_add_special (rr, &np);
1216 if (0 != vec_len (np.paths))
1218 if (rtype == RTN_MULTICAST)
1220 /* it's not clear to me how linux expresses the RPF paramters
1221 * so we'll allow from all interfaces and hope for the best */
1222 mfib_prefix_t mpfx = {};
1224 lcp_router_route_mk_mprefix (rr, &mpfx);
1226 mfib_table_entry_update (
1227 nlt->nlt_mfib_index, &mpfx, MFIB_SOURCE_PLUGIN_LOW,
1228 MFIB_RPF_ID_NONE, MFIB_ENTRY_FLAG_ACCEPT_ALL_ITF);
1230 mfib_table_entry_paths_update (nlt->nlt_mfib_index, &mpfx,
1231 MFIB_SOURCE_PLUGIN_LOW,
1232 MFIB_ENTRY_FLAG_NONE, np.paths);
1236 fib_source_t fib_src;
1237 const fib_route_path_t *rpath;
1239 vec_foreach (rpath, np.paths)
1241 if (fib_route_path_is_attached (rpath))
1243 entry_flags |= FIB_ENTRY_FLAG_ATTACHED;
1248 fib_src = lcp_router_proto_fib_source (rproto);
1250 if (pfx.fp_proto == FIB_PROTOCOL_IP6)
1251 fib_table_entry_path_add2 (nlt->nlt_fib_index, &pfx, fib_src,
1252 entry_flags, np.paths);
1254 fib_table_entry_update (nlt->nlt_fib_index, &pfx, fib_src,
1255 entry_flags, np.paths);
1259 LCP_ROUTER_DBG ("no paths for route add: %d:%U %U",
1260 rtnl_route_get_table (rr), format_fib_prefix, &pfx,
1261 format_fib_entry_flags, entry_flags);
1262 vec_free (np.paths);
1267 lcp_router_route_sync_begin (void)
1269 lcp_router_table_t *nlt;
1271 pool_foreach (nlt, lcp_router_table_pool)
1273 fib_table_mark (nlt->nlt_fib_index, nlt->nlt_proto, lcp_rt_fib_src);
1274 fib_table_mark (nlt->nlt_fib_index, nlt->nlt_proto,
1275 lcp_rt_fib_src_dynamic);
1277 LCP_ROUTER_INFO ("Begin synchronization of %U routes in table %u",
1278 format_fib_protocol, nlt->nlt_proto,
1279 nlt->nlt_fib_index);
1284 lcp_router_route_sync_end (void)
1286 lcp_router_table_t *nlt;
1288 pool_foreach (nlt, lcp_router_table_pool)
1290 fib_table_sweep (nlt->nlt_fib_index, nlt->nlt_proto, lcp_rt_fib_src);
1291 fib_table_sweep (nlt->nlt_fib_index, nlt->nlt_proto,
1292 lcp_rt_fib_src_dynamic);
1294 LCP_ROUTER_INFO ("End synchronization of %U routes in table %u",
1295 format_fib_protocol, nlt->nlt_proto,
1296 nlt->nlt_fib_index);
1300 typedef struct lcp_router_table_flush_ctx_t_
1302 fib_node_index_t *lrtf_entries;
1303 u32 *lrtf_sw_if_index_to_bool;
1304 fib_source_t lrtf_source;
1305 } lcp_router_table_flush_ctx_t;
1307 static fib_table_walk_rc_t
1308 lcp_router_table_flush_cb (fib_node_index_t fib_entry_index, void *arg)
1310 lcp_router_table_flush_ctx_t *ctx = arg;
1313 sw_if_index = fib_entry_get_resolving_interface_for_source (
1314 fib_entry_index, ctx->lrtf_source);
1316 if (sw_if_index < vec_len (ctx->lrtf_sw_if_index_to_bool) &&
1317 ctx->lrtf_sw_if_index_to_bool[sw_if_index])
1319 vec_add1 (ctx->lrtf_entries, fib_entry_index);
1321 return (FIB_TABLE_WALK_CONTINUE);
1325 lcp_router_table_flush (lcp_router_table_t *nlt, u32 *sw_if_index_to_bool,
1326 fib_source_t source)
1328 fib_node_index_t *fib_entry_index;
1329 lcp_router_table_flush_ctx_t ctx = {
1330 .lrtf_entries = NULL,
1331 .lrtf_sw_if_index_to_bool = sw_if_index_to_bool,
1332 .lrtf_source = source,
1336 "Flush table: proto %U, fib-index %u, max sw_if_index %u, source %U",
1337 format_fib_protocol, nlt->nlt_proto, nlt->nlt_fib_index,
1338 vec_len (sw_if_index_to_bool) - 1, format_fib_source, source);
1340 fib_table_walk (nlt->nlt_fib_index, nlt->nlt_proto,
1341 lcp_router_table_flush_cb, &ctx);
1343 LCP_ROUTER_DBG ("Flush table: entries number to delete %u",
1344 vec_len (ctx.lrtf_entries));
1346 vec_foreach (fib_entry_index, ctx.lrtf_entries)
1348 fib_table_entry_delete_index (*fib_entry_index, source);
1349 lcp_router_table_unlock (nlt);
1352 vec_free (ctx.lrtf_entries);
1355 const nl_vft_t lcp_router_vft = {
1356 .nvl_rt_link_add = { .is_mp_safe = 0, .cb = lcp_router_link_add },
1357 .nvl_rt_link_del = { .is_mp_safe = 0, .cb = lcp_router_link_del },
1358 .nvl_rt_link_sync_begin = { .is_mp_safe = 0,
1359 .cb = lcp_router_link_sync_begin },
1360 .nvl_rt_link_sync_end = { .is_mp_safe = 0, .cb = lcp_router_link_sync_end },
1361 .nvl_rt_addr_add = { .is_mp_safe = 0, .cb = lcp_router_link_addr_add },
1362 .nvl_rt_addr_del = { .is_mp_safe = 0, .cb = lcp_router_link_addr_del },
1363 .nvl_rt_addr_sync_begin = { .is_mp_safe = 0,
1364 .cb = lcp_router_link_addr_sync_begin },
1365 .nvl_rt_addr_sync_end = { .is_mp_safe = 0,
1366 .cb = lcp_router_link_addr_sync_end },
1367 .nvl_rt_neigh_add = { .is_mp_safe = 0, .cb = lcp_router_neigh_add },
1368 .nvl_rt_neigh_del = { .is_mp_safe = 0, .cb = lcp_router_neigh_del },
1369 .nvl_rt_neigh_sync_begin = { .is_mp_safe = 0,
1370 .cb = lcp_router_neigh_sync_begin },
1371 .nvl_rt_neigh_sync_end = { .is_mp_safe = 0,
1372 .cb = lcp_router_neigh_sync_end },
1373 .nvl_rt_route_add = { .is_mp_safe = 1, .cb = lcp_router_route_add },
1374 .nvl_rt_route_del = { .is_mp_safe = 1, .cb = lcp_router_route_del },
1375 .nvl_rt_route_sync_begin = { .is_mp_safe = 0,
1376 .cb = lcp_router_route_sync_begin },
1377 .nvl_rt_route_sync_end = { .is_mp_safe = 0,
1378 .cb = lcp_router_route_sync_end },
1381 static clib_error_t *
1382 lcp_router_init (vlib_main_t *vm)
1384 lcp_router_logger = vlib_log_register_class ("linux-cp", "router");
1386 nl_register_vft (&lcp_router_vft);
1389 * allocate 2 route sources. The low priority source will be for
1390 * dynamic routes. If a dynamic route daemon (FRR) tries to remove its
1391 * route, it will use the low priority source to ensure it will not
1392 * remove static routes which were added with the higher priority source.
1395 fib_source_allocate ("lcp-rt", FIB_SOURCE_PRIORITY_HI, FIB_SOURCE_BH_API);
1397 lcp_rt_fib_src_dynamic = fib_source_allocate (
1398 "lcp-rt-dynamic", FIB_SOURCE_PRIORITY_HI + 1, FIB_SOURCE_BH_API);
1403 VLIB_INIT_FUNCTION (lcp_router_init) = {
1404 .runs_before = VLIB_INITS ("lcp_nl_init"),
1408 * fd.io coding-style-patch-verification: ON
1411 * eval: (c-set-style "gnu")