2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
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 <vlib/vlib.h>
17 #include <vnet/vnet.h>
18 #include <vnet/ip/format.h>
19 #include <vnet/ip/ip.h>
20 #include <vnet/dpo/drop_dpo.h>
21 #include <vnet/dpo/receive_dpo.h>
22 #include <vnet/dpo/load_balance_map.h>
23 #include <vnet/dpo/lookup_dpo.h>
25 #include <vnet/adj/adj.h>
29 #include "fib_table.h"
30 #include "fib_entry.h"
31 #include "fib_path_list.h"
32 #include "fib_internal.h"
35 * Enurmeration of path types
37 typedef enum fib_path_type_t_ {
39 * Marker. Add new types after this one.
41 FIB_PATH_TYPE_FIRST = 0,
43 * Attached-nexthop. An interface and a nexthop are known.
45 FIB_PATH_TYPE_ATTACHED_NEXT_HOP = FIB_PATH_TYPE_FIRST,
47 * attached. Only the interface is known.
49 FIB_PATH_TYPE_ATTACHED,
51 * recursive. Only the next-hop is known.
53 FIB_PATH_TYPE_RECURSIVE,
55 * special. nothing is known. so we drop.
57 FIB_PATH_TYPE_SPECIAL,
59 * exclusive. user provided adj.
61 FIB_PATH_TYPE_EXCLUSIVE,
63 * deag. Link to a lookup adj in the next table
67 * receive. it's for-us.
69 FIB_PATH_TYPE_RECEIVE,
71 * Marker. Add new types before this one, then update it.
73 FIB_PATH_TYPE_LAST = FIB_PATH_TYPE_RECEIVE,
74 } __attribute__ ((packed)) fib_path_type_t;
77 * The maximum number of path_types
79 #define FIB_PATH_TYPE_MAX (FIB_PATH_TYPE_LAST + 1)
81 #define FIB_PATH_TYPES { \
82 [FIB_PATH_TYPE_ATTACHED_NEXT_HOP] = "attached-nexthop", \
83 [FIB_PATH_TYPE_ATTACHED] = "attached", \
84 [FIB_PATH_TYPE_RECURSIVE] = "recursive", \
85 [FIB_PATH_TYPE_SPECIAL] = "special", \
86 [FIB_PATH_TYPE_EXCLUSIVE] = "exclusive", \
87 [FIB_PATH_TYPE_DEAG] = "deag", \
88 [FIB_PATH_TYPE_RECEIVE] = "receive", \
91 #define FOR_EACH_FIB_PATH_TYPE(_item) \
92 for (_item = FIB_PATH_TYPE_FIRST; _item <= FIB_PATH_TYPE_LAST; _item++)
95 * Enurmeration of path operational (i.e. derived) attributes
97 typedef enum fib_path_oper_attribute_t_ {
99 * Marker. Add new types after this one.
101 FIB_PATH_OPER_ATTRIBUTE_FIRST = 0,
103 * The path forms part of a recursive loop.
105 FIB_PATH_OPER_ATTRIBUTE_RECURSIVE_LOOP = FIB_PATH_OPER_ATTRIBUTE_FIRST,
107 * The path is resolved
109 FIB_PATH_OPER_ATTRIBUTE_RESOLVED,
111 * The path has become a permanent drop.
113 FIB_PATH_OPER_ATTRIBUTE_DROP,
115 * Marker. Add new types before this one, then update it.
117 FIB_PATH_OPER_ATTRIBUTE_LAST = FIB_PATH_OPER_ATTRIBUTE_DROP,
118 } __attribute__ ((packed)) fib_path_oper_attribute_t;
121 * The maximum number of path operational attributes
123 #define FIB_PATH_OPER_ATTRIBUTE_MAX (FIB_PATH_OPER_ATTRIBUTE_LAST + 1)
125 #define FIB_PATH_OPER_ATTRIBUTES { \
126 [FIB_PATH_OPER_ATTRIBUTE_RECURSIVE_LOOP] = "recursive-loop", \
127 [FIB_PATH_OPER_ATTRIBUTE_RESOLVED] = "resolved", \
128 [FIB_PATH_OPER_ATTRIBUTE_DROP] = "drop", \
131 #define FOR_EACH_FIB_PATH_OPER_ATTRIBUTE(_item) \
132 for (_item = FIB_PATH_OPER_ATTRIBUTE_FIRST; \
133 _item <= FIB_PATH_OPER_ATTRIBUTE_LAST; \
137 * Path flags from the attributes
139 typedef enum fib_path_oper_flags_t_ {
140 FIB_PATH_OPER_FLAG_NONE = 0,
141 FIB_PATH_OPER_FLAG_RECURSIVE_LOOP = (1 << FIB_PATH_OPER_ATTRIBUTE_RECURSIVE_LOOP),
142 FIB_PATH_OPER_FLAG_DROP = (1 << FIB_PATH_OPER_ATTRIBUTE_DROP),
143 FIB_PATH_OPER_FLAG_RESOLVED = (1 << FIB_PATH_OPER_ATTRIBUTE_RESOLVED),
144 } __attribute__ ((packed)) fib_path_oper_flags_t;
149 typedef struct fib_path_t_ {
151 * A path is a node in the FIB graph.
156 * The index of the path-list to which this path belongs
161 * This marks the start of the memory area used to hash
164 STRUCT_MARK(path_hash_start);
167 * Configuration Flags
169 fib_path_cfg_flags_t fp_cfg_flags;
172 * The type of the path. This is the selector for the union
174 fib_path_type_t fp_type;
177 * The protocol of the next-hop, i.e. the address family of the
178 * next-hop's address. We can't derive this from the address itself
179 * since the address can be all zeros
181 fib_protocol_t fp_nh_proto;
184 * UCMP [unnormalised] weigt
189 * per-type union of the data required to resolve the path
196 ip46_address_t fp_nh;
212 ip46_address_t fp_nh;
214 * The FIB table index in which to find the next-hop.
215 * This needs to be fixed. We should lookup the adjacencies in
216 * a separate table of adjacencies, rather than from the FIB.
217 * Two reasons I can think of:
219 * int ip addr Gig0 10.0.0.1/24
220 * ip route 10.0.0.2/32 via Gig1 192.168.1.2
221 * ip route 1.1.1.1/32 via Gig0 10.0.0.2
222 * this is perfectly valid.
223 * Packets addressed to 10.0.0.2 should be sent via Gig1.
224 * Packets address to 1.1.1.1 should be sent via Gig0.
225 * when we perform the adj resolution from the FIB for the path
226 * "via Gig0 10.0.0.2" the lookup will result in the route via Gig1
227 * and so we will pick up the adj via Gig1 - which was not what the
229 * - we can only return link-type IPv4 and so not the link-type MPLS.
230 * more on this in a later commit.
232 * The table ID should only belong to a recursive path and indicate
233 * which FIB should be used to resolve the next-hop.
235 fib_node_index_t fp_tbl_id;
239 * The FIN index in which to perfom the next lookup
241 fib_node_index_t fp_tbl_id;
247 * The user provided 'exclusive' DPO
253 * The interface on which the local address is configured
259 ip46_address_t fp_addr;
262 STRUCT_MARK(path_hash_end);
265 * Memebers in this last section represent information that is
266 * dervied during resolution. It should not be copied to new paths
273 fib_path_oper_flags_t fp_oper_flags;
276 * the resolving via fib. not part of the union, since it it not part
277 * of the path's hash.
279 fib_node_index_t fp_via_fib;
282 * The Data-path objects through which this path resolves for IP.
287 * the index of this path in the parent's child list.
293 * Array of strings/names for the path types and attributes
295 static const char *fib_path_type_names[] = FIB_PATH_TYPES;
296 static const char *fib_path_oper_attribute_names[] = FIB_PATH_OPER_ATTRIBUTES;
297 static const char *fib_path_cfg_attribute_names[] = FIB_PATH_CFG_ATTRIBUTES;
300 * The memory pool from which we allocate all the paths
302 static fib_path_t *fib_path_pool;
308 #define FIB_PATH_DBG(_p, _fmt, _args...) \
311 _tmp = fib_path_format(fib_path_get_index(_p), _tmp); \
312 clib_warning("path:[%d:%s]:" _fmt, \
313 fib_path_get_index(_p), _tmp, \
318 #define FIB_PATH_DBG(_p, _fmt, _args...)
322 fib_path_get (fib_node_index_t index)
324 return (pool_elt_at_index(fib_path_pool, index));
327 static fib_node_index_t
328 fib_path_get_index (fib_path_t *path)
330 return (path - fib_path_pool);
334 fib_path_get_node (fib_node_index_t index)
336 return ((fib_node_t*)fib_path_get(index));
340 fib_path_from_fib_node (fib_node_t *node)
343 ASSERT(FIB_NODE_TYPE_PATH == node->fn_type);
345 return ((fib_path_t*)node);
349 format_fib_path (u8 * s, va_list * args)
351 fib_path_t *path = va_arg (*args, fib_path_t *);
352 vnet_main_t * vnm = vnet_get_main();
353 fib_path_oper_attribute_t oattr;
354 fib_path_cfg_attribute_t cattr;
356 s = format (s, " index:%d ", fib_path_get_index(path));
357 s = format (s, "pl-index:%d ", path->fp_pl_index);
358 s = format (s, "%U ", format_fib_protocol, path->fp_nh_proto);
359 s = format (s, "weight=%d ", path->fp_weight);
360 s = format (s, "%s: ", fib_path_type_names[path->fp_type]);
361 if (FIB_PATH_OPER_FLAG_NONE != path->fp_oper_flags) {
362 s = format(s, " oper-flags:");
363 FOR_EACH_FIB_PATH_OPER_ATTRIBUTE(oattr) {
364 if ((1<<oattr) & path->fp_oper_flags) {
365 s = format (s, "%s,", fib_path_oper_attribute_names[oattr]);
369 if (FIB_PATH_CFG_FLAG_NONE != path->fp_cfg_flags) {
370 s = format(s, " cfg-flags:");
371 FOR_EACH_FIB_PATH_CFG_ATTRIBUTE(cattr) {
372 if ((1<<cattr) & path->fp_cfg_flags) {
373 s = format (s, "%s,", fib_path_cfg_attribute_names[cattr]);
377 s = format(s, "\n ");
379 switch (path->fp_type)
381 case FIB_PATH_TYPE_ATTACHED_NEXT_HOP:
382 s = format (s, "%U", format_ip46_address,
383 &path->attached_next_hop.fp_nh,
385 if (path->fp_oper_flags & FIB_PATH_OPER_FLAG_DROP)
387 s = format (s, " if_index:%d", path->attached_next_hop.fp_interface);
391 s = format (s, " %U",
392 format_vnet_sw_interface_name,
394 vnet_get_sw_interface(
396 path->attached_next_hop.fp_interface));
397 if (vnet_sw_interface_is_p2p(vnet_get_main(),
398 path->attached_next_hop.fp_interface))
400 s = format (s, " (p2p)");
403 if (!dpo_id_is_valid(&path->fp_dpo))
405 s = format(s, "\n unresolved");
409 s = format(s, "\n %U",
414 case FIB_PATH_TYPE_ATTACHED:
415 if (path->fp_oper_flags & FIB_PATH_OPER_FLAG_DROP)
417 s = format (s, " if_index:%d", path->attached_next_hop.fp_interface);
421 s = format (s, " %U",
422 format_vnet_sw_interface_name,
424 vnet_get_sw_interface(
426 path->attached.fp_interface));
429 case FIB_PATH_TYPE_RECURSIVE:
430 s = format (s, "via %U",
432 &path->recursive.fp_nh,
434 s = format (s, " in fib:%d", path->recursive.fp_tbl_id, path->fp_via_fib);
435 s = format (s, " via-fib:%d", path->fp_via_fib);
436 s = format (s, " via-dpo:[%U:%d]",
437 format_dpo_type, path->fp_dpo.dpoi_type,
438 path->fp_dpo.dpoi_index);
441 case FIB_PATH_TYPE_RECEIVE:
442 case FIB_PATH_TYPE_SPECIAL:
443 case FIB_PATH_TYPE_DEAG:
444 case FIB_PATH_TYPE_EXCLUSIVE:
445 if (dpo_id_is_valid(&path->fp_dpo))
447 s = format(s, "%U", format_dpo_id,
456 fib_path_format (fib_node_index_t pi, u8 *s)
460 path = fib_path_get(pi);
461 ASSERT(NULL != path);
463 return (format (s, "%U", format_fib_path, path));
467 fib_path_adj_format (fib_node_index_t pi,
473 path = fib_path_get(pi);
474 ASSERT(NULL != path);
476 if (!dpo_id_is_valid(&path->fp_dpo))
478 s = format(s, " unresolved");
482 s = format(s, "%U", format_dpo_id,
490 * fib_path_last_lock_gone
492 * We don't share paths, we share path lists, so the [un]lock functions
496 fib_path_last_lock_gone (fib_node_t *node)
501 static const adj_index_t
502 fib_path_attached_next_hop_get_adj (fib_path_t *path,
505 if (vnet_sw_interface_is_p2p(vnet_get_main(),
506 path->attached_next_hop.fp_interface))
509 * if the interface is p2p then the adj for the specific
510 * neighbour on that link will never exist. on p2p links
511 * the subnet address (the attached route) links to the
512 * auto-adj (see below), we want that adj here too.
514 return (adj_nbr_add_or_lock(path->fp_nh_proto,
517 path->attached_next_hop.fp_interface));
521 return (adj_nbr_add_or_lock(path->fp_nh_proto,
523 &path->attached_next_hop.fp_nh,
524 path->attached_next_hop.fp_interface));
529 fib_path_attached_next_hop_set (fib_path_t *path)
532 * resolve directly via the adjacnecy discribed by the
533 * interface and next-hop
535 if (!vnet_sw_interface_is_admin_up(vnet_get_main(),
536 path->attached_next_hop.fp_interface))
538 path->fp_oper_flags &= ~FIB_PATH_OPER_FLAG_RESOLVED;
541 dpo_set(&path->fp_dpo,
543 fib_proto_to_dpo(path->fp_nh_proto),
544 fib_path_attached_next_hop_get_adj(
546 fib_proto_to_link(path->fp_nh_proto)));
549 * become a child of the adjacency so we receive updates
550 * when its rewrite changes
552 path->fp_sibling = adj_child_add(path->fp_dpo.dpoi_index,
554 fib_path_get_index(path));
558 * create of update the paths recursive adj
561 fib_path_recursive_adj_update (fib_path_t *path,
562 fib_forward_chain_type_t fct,
565 dpo_id_t via_dpo = DPO_NULL;
568 * get the DPO to resolve through from the via-entry
570 fib_entry_contribute_forwarding(path->fp_via_fib,
576 * hope for the best - clear if restrictions apply.
578 path->fp_oper_flags |= FIB_PATH_OPER_FLAG_RESOLVED;
581 * Validate any recursion constraints and over-ride the via
584 if (path->fp_oper_flags & FIB_PATH_OPER_FLAG_RECURSIVE_LOOP)
586 path->fp_oper_flags &= ~FIB_PATH_OPER_FLAG_RESOLVED;
587 dpo_copy(&via_dpo, drop_dpo_get(fib_proto_to_dpo(path->fp_nh_proto)));
589 else if (path->fp_cfg_flags & FIB_PATH_CFG_FLAG_RESOLVE_HOST)
592 * the via FIB must be a host route.
593 * note the via FIB just added will always be a host route
594 * since it is an RR source added host route. So what we need to
595 * check is whether the route has other sources. If it does then
596 * some other source has added it as a host route. If it doesn't
597 * then it was added only here and inherits forwarding from a cover.
598 * the cover is not a host route.
599 * The RR source is the lowest priority source, so we check if it
600 * is the best. if it is there are no other sources.
602 if (fib_entry_get_best_source(path->fp_via_fib) >= FIB_SOURCE_RR)
604 path->fp_oper_flags &= ~FIB_PATH_OPER_FLAG_RESOLVED;
605 dpo_copy(&via_dpo, drop_dpo_get(fib_proto_to_dpo(path->fp_nh_proto)));
608 * PIC edge trigger. let the load-balance maps know
610 load_balance_map_path_state_change(fib_path_get_index(path));
613 else if (path->fp_cfg_flags & FIB_PATH_CFG_FLAG_RESOLVE_ATTACHED)
616 * RR source entries inherit the flags from the cover, so
617 * we can check the via directly
619 if (!(FIB_ENTRY_FLAG_ATTACHED & fib_entry_get_flags(path->fp_via_fib)))
621 path->fp_oper_flags &= ~FIB_PATH_OPER_FLAG_RESOLVED;
622 dpo_copy(&via_dpo, drop_dpo_get(fib_proto_to_dpo(path->fp_nh_proto)));
625 * PIC edge trigger. let the load-balance maps know
627 load_balance_map_path_state_change(fib_path_get_index(path));
632 * update the path's contributed DPO
634 dpo_copy(dpo, &via_dpo);
636 FIB_PATH_DBG(path, "recursive update: %U",
637 fib_get_lookup_main(path->fp_nh_proto),
644 * fib_path_is_permanent_drop
646 * Return !0 if the path is configured to permanently drop,
647 * despite other attributes.
650 fib_path_is_permanent_drop (fib_path_t *path)
652 return ((path->fp_cfg_flags & FIB_PATH_CFG_FLAG_DROP) ||
653 (path->fp_oper_flags & FIB_PATH_OPER_FLAG_DROP));
659 * Remove our dependency on the resolution target
662 fib_path_unresolve (fib_path_t *path)
665 * the forced drop path does not need unresolving
667 if (fib_path_is_permanent_drop(path))
672 switch (path->fp_type)
674 case FIB_PATH_TYPE_RECURSIVE:
675 if (FIB_NODE_INDEX_INVALID != path->fp_via_fib)
679 fib_prefix_from_ip46_addr(&path->recursive.fp_nh, &pfx);
680 fib_entry_child_remove(path->fp_via_fib,
682 fib_table_entry_special_remove(path->recursive.fp_tbl_id,
685 path->fp_via_fib = FIB_NODE_INDEX_INVALID;
688 case FIB_PATH_TYPE_ATTACHED_NEXT_HOP:
689 case FIB_PATH_TYPE_ATTACHED:
690 adj_child_remove(path->fp_dpo.dpoi_index,
692 adj_unlock(path->fp_dpo.dpoi_index);
694 case FIB_PATH_TYPE_EXCLUSIVE:
695 dpo_reset(&path->exclusive.fp_ex_dpo);
697 case FIB_PATH_TYPE_SPECIAL:
698 case FIB_PATH_TYPE_RECEIVE:
699 case FIB_PATH_TYPE_DEAG:
701 * these hold only the path's DPO, which is reset below.
707 * release the adj we were holding and pick up the
710 dpo_reset(&path->fp_dpo);
711 path->fp_oper_flags &= ~FIB_PATH_OPER_FLAG_RESOLVED;
716 static fib_forward_chain_type_t
717 fib_path_proto_to_chain_type (fib_protocol_t proto)
721 case FIB_PROTOCOL_IP4:
722 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
723 case FIB_PROTOCOL_IP6:
724 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP6);
725 case FIB_PROTOCOL_MPLS:
726 return (FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS);
728 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
732 * fib_path_back_walk_notify
734 * A back walk has reach this path.
736 static fib_node_back_walk_rc_t
737 fib_path_back_walk_notify (fib_node_t *node,
738 fib_node_back_walk_ctx_t *ctx)
742 path = fib_path_from_fib_node(node);
744 switch (path->fp_type)
746 case FIB_PATH_TYPE_RECURSIVE:
747 if (FIB_NODE_BW_REASON_FLAG_EVALUATE & ctx->fnbw_reason)
750 * modify the recursive adjacency to use the new forwarding
752 * this update is visible to packets in flight in the DP.
754 fib_path_recursive_adj_update(
756 fib_path_proto_to_chain_type(path->fp_nh_proto),
760 case FIB_PATH_TYPE_ATTACHED_NEXT_HOP:
763 * ADJ_UPDATE backwalk pass silently through here and up to
764 * the path-list when the multipath adj collapse occurs.
765 * The reason we do this is that the assumtption is that VPP
766 * runs in an environment where the Control-Plane is remote
767 * and hence reacts slowly to link up down. In order to remove
768 * this down link from the ECMP set quickly, we back-walk.
769 * VPP also has dedicated CPUs, so we are not stealing resources
770 * from the CP to do so.
772 if (FIB_NODE_BW_REASON_FLAG_INTERFACE_UP & ctx->fnbw_reason)
774 path->fp_oper_flags |= FIB_PATH_OPER_FLAG_RESOLVED;
776 if (FIB_NODE_BW_REASON_FLAG_INTERFACE_DOWN & ctx->fnbw_reason)
778 path->fp_oper_flags &= ~FIB_PATH_OPER_FLAG_RESOLVED;
780 if (FIB_NODE_BW_REASON_FLAG_INTERFACE_DELETE & ctx->fnbw_reason)
783 * The interface this path resolves through has been deleted.
784 * This will leave the path in a permanent drop state. The route
785 * needs to be removed and readded (and hence the path-list deleted)
786 * before it can forward again.
788 fib_path_unresolve(path);
789 path->fp_oper_flags |= FIB_PATH_OPER_FLAG_DROP;
791 if (FIB_NODE_BW_REASON_FLAG_ADJ_UPDATE & ctx->fnbw_reason)
794 * restack the DPO to pick up the correct DPO sub-type
798 ai = fib_path_attached_next_hop_get_adj(
800 fib_proto_to_link(path->fp_nh_proto));
802 dpo_set(&path->fp_dpo, DPO_ADJACENCY,
803 fib_proto_to_dpo(path->fp_nh_proto),
808 case FIB_PATH_TYPE_ATTACHED:
810 * FIXME; this could schedule a lower priority walk, since attached
811 * routes are not usually in ECMP configurations so the backwalk to
812 * the FIB entry does not need to be high priority
814 if (FIB_NODE_BW_REASON_FLAG_INTERFACE_UP & ctx->fnbw_reason)
816 path->fp_oper_flags |= FIB_PATH_OPER_FLAG_RESOLVED;
818 if (FIB_NODE_BW_REASON_FLAG_INTERFACE_DOWN & ctx->fnbw_reason)
820 path->fp_oper_flags &= ~FIB_PATH_OPER_FLAG_RESOLVED;
822 if (FIB_NODE_BW_REASON_FLAG_INTERFACE_DELETE & ctx->fnbw_reason)
824 fib_path_unresolve(path);
825 path->fp_oper_flags |= FIB_PATH_OPER_FLAG_DROP;
828 case FIB_PATH_TYPE_DEAG:
830 * FIXME When VRF delete is allowed this will need a poke.
832 case FIB_PATH_TYPE_SPECIAL:
833 case FIB_PATH_TYPE_RECEIVE:
834 case FIB_PATH_TYPE_EXCLUSIVE:
836 * these path types have no parents. so to be
837 * walked from one is unexpected.
844 * propagate the backwalk further to the path-list
846 fib_path_list_back_walk(path->fp_pl_index, ctx);
848 return (FIB_NODE_BACK_WALK_CONTINUE);
852 * The FIB path's graph node virtual function table
854 static const fib_node_vft_t fib_path_vft = {
855 .fnv_get = fib_path_get_node,
856 .fnv_last_lock = fib_path_last_lock_gone,
857 .fnv_back_walk = fib_path_back_walk_notify,
860 static fib_path_cfg_flags_t
861 fib_path_route_flags_to_cfg_flags (const fib_route_path_t *rpath)
863 fib_path_cfg_flags_t cfg_flags = FIB_PATH_CFG_ATTRIBUTE_FIRST;
865 if (rpath->frp_flags & FIB_ROUTE_PATH_RESOLVE_VIA_HOST)
866 cfg_flags |= FIB_PATH_CFG_FLAG_RESOLVE_HOST;
867 if (rpath->frp_flags & FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED)
868 cfg_flags |= FIB_PATH_CFG_FLAG_RESOLVE_ATTACHED;
876 * Create and initialise a new path object.
877 * return the index of the path.
880 fib_path_create (fib_node_index_t pl_index,
881 fib_protocol_t nh_proto,
882 fib_path_cfg_flags_t flags,
883 const fib_route_path_t *rpath)
887 pool_get(fib_path_pool, path);
888 memset(path, 0, sizeof(*path));
890 fib_node_init(&path->fp_node,
893 dpo_reset(&path->fp_dpo);
894 path->fp_pl_index = pl_index;
895 path->fp_nh_proto = nh_proto;
896 path->fp_via_fib = FIB_NODE_INDEX_INVALID;
897 path->fp_weight = rpath->frp_weight;
898 path->fp_cfg_flags = flags;
899 path->fp_cfg_flags |= fib_path_route_flags_to_cfg_flags(rpath);
902 * deduce the path's tpye from the parementers and save what is needed.
904 if (~0 != rpath->frp_sw_if_index)
906 if (flags & FIB_PATH_CFG_FLAG_LOCAL)
908 path->fp_type = FIB_PATH_TYPE_RECEIVE;
909 path->receive.fp_interface = rpath->frp_sw_if_index;
910 path->receive.fp_addr = rpath->frp_addr;
914 if (ip46_address_is_zero(&rpath->frp_addr))
916 path->fp_type = FIB_PATH_TYPE_ATTACHED;
917 path->attached.fp_interface = rpath->frp_sw_if_index;
921 path->fp_type = FIB_PATH_TYPE_ATTACHED_NEXT_HOP;
922 path->attached_next_hop.fp_interface = rpath->frp_sw_if_index;
923 path->attached_next_hop.fp_nh = rpath->frp_addr;
929 if (ip46_address_is_zero(&rpath->frp_addr))
931 if (~0 == rpath->frp_fib_index)
933 path->fp_type = FIB_PATH_TYPE_SPECIAL;
937 path->fp_type = FIB_PATH_TYPE_DEAG;
938 path->deag.fp_tbl_id = rpath->frp_fib_index;
943 path->fp_type = FIB_PATH_TYPE_RECURSIVE;
944 path->recursive.fp_nh = rpath->frp_addr;
945 path->recursive.fp_tbl_id = rpath->frp_fib_index;
949 FIB_PATH_DBG(path, "create");
951 return (fib_path_get_index(path));
955 * fib_path_create_special
957 * Create and initialise a new path object.
958 * return the index of the path.
961 fib_path_create_special (fib_node_index_t pl_index,
962 fib_protocol_t nh_proto,
963 fib_path_cfg_flags_t flags,
968 pool_get(fib_path_pool, path);
969 memset(path, 0, sizeof(*path));
971 fib_node_init(&path->fp_node,
973 dpo_reset(&path->fp_dpo);
975 path->fp_pl_index = pl_index;
977 path->fp_nh_proto = nh_proto;
978 path->fp_via_fib = FIB_NODE_INDEX_INVALID;
979 path->fp_cfg_flags = flags;
981 if (FIB_PATH_CFG_FLAG_DROP & flags)
983 path->fp_type = FIB_PATH_TYPE_SPECIAL;
985 else if (FIB_PATH_CFG_FLAG_LOCAL & flags)
987 path->fp_type = FIB_PATH_TYPE_RECEIVE;
988 path->attached.fp_interface = FIB_NODE_INDEX_INVALID;
992 path->fp_type = FIB_PATH_TYPE_EXCLUSIVE;
994 dpo_copy(&path->exclusive.fp_ex_dpo, dpo);
997 return (fib_path_get_index(path));
1003 * Copy a path. return index of new path.
1006 fib_path_copy (fib_node_index_t path_index,
1007 fib_node_index_t path_list_index)
1009 fib_path_t *path, *orig_path;
1011 pool_get(fib_path_pool, path);
1013 orig_path = fib_path_get(path_index);
1014 ASSERT(NULL != orig_path);
1016 memcpy(path, orig_path, sizeof(*path));
1018 FIB_PATH_DBG(path, "create-copy:%d", path_index);
1021 * reset the dynamic section
1023 fib_node_init(&path->fp_node, FIB_NODE_TYPE_PATH);
1024 path->fp_oper_flags = FIB_PATH_OPER_FLAG_NONE;
1025 path->fp_pl_index = path_list_index;
1026 path->fp_via_fib = FIB_NODE_INDEX_INVALID;
1027 memset(&path->fp_dpo, 0, sizeof(path->fp_dpo));
1028 dpo_reset(&path->fp_dpo);
1030 return (fib_path_get_index(path));
1036 * destroy a path that is no longer required
1039 fib_path_destroy (fib_node_index_t path_index)
1043 path = fib_path_get(path_index);
1045 ASSERT(NULL != path);
1046 FIB_PATH_DBG(path, "destroy");
1048 fib_path_unresolve(path);
1050 fib_node_deinit(&path->fp_node);
1051 pool_put(fib_path_pool, path);
1057 * destroy a path that is no longer required
1060 fib_path_hash (fib_node_index_t path_index)
1064 path = fib_path_get(path_index);
1066 return (hash_memory(STRUCT_MARK_PTR(path, path_hash_start),
1067 (STRUCT_OFFSET_OF(fib_path_t, path_hash_end) -
1068 STRUCT_OFFSET_OF(fib_path_t, path_hash_start)),
1075 * Compare two paths for equivalence.
1078 fib_path_cmp_i (const fib_path_t *path1,
1079 const fib_path_t *path2)
1086 * paths of different types and protocol are not equal.
1087 * different weights only are the same path.
1089 if (path1->fp_type != path2->fp_type)
1091 res = (path1->fp_type - path2->fp_type);
1093 if (path1->fp_nh_proto != path2->fp_nh_proto)
1095 res = (path1->fp_nh_proto - path2->fp_nh_proto);
1100 * both paths are of the same type.
1101 * consider each type and its attributes in turn.
1103 switch (path1->fp_type)
1105 case FIB_PATH_TYPE_ATTACHED_NEXT_HOP:
1106 res = ip46_address_cmp(&path1->attached_next_hop.fp_nh,
1107 &path2->attached_next_hop.fp_nh);
1109 res = vnet_sw_interface_compare(
1111 path1->attached_next_hop.fp_interface,
1112 path2->attached_next_hop.fp_interface);
1115 case FIB_PATH_TYPE_ATTACHED:
1116 res = vnet_sw_interface_compare(
1118 path1->attached.fp_interface,
1119 path2->attached.fp_interface);
1121 case FIB_PATH_TYPE_RECURSIVE:
1122 res = ip46_address_cmp(&path1->recursive.fp_nh,
1123 &path2->recursive.fp_nh);
1127 res = (path1->recursive.fp_tbl_id - path2->recursive.fp_tbl_id);
1130 case FIB_PATH_TYPE_DEAG:
1131 res = (path1->deag.fp_tbl_id - path2->deag.fp_tbl_id);
1133 case FIB_PATH_TYPE_SPECIAL:
1134 case FIB_PATH_TYPE_RECEIVE:
1135 case FIB_PATH_TYPE_EXCLUSIVE:
1144 * fib_path_cmp_for_sort
1146 * Compare two paths for equivalence. Used during path sorting.
1147 * As usual 0 means equal.
1150 fib_path_cmp_for_sort (void * v1,
1153 fib_node_index_t *pi1 = v1, *pi2 = v2;
1154 fib_path_t *path1, *path2;
1156 path1 = fib_path_get(*pi1);
1157 path2 = fib_path_get(*pi2);
1159 return (fib_path_cmp_i(path1, path2));
1165 * Compare two paths for equivalence.
1168 fib_path_cmp (fib_node_index_t pi1,
1169 fib_node_index_t pi2)
1171 fib_path_t *path1, *path2;
1173 path1 = fib_path_get(pi1);
1174 path2 = fib_path_get(pi2);
1176 return (fib_path_cmp_i(path1, path2));
1180 fib_path_cmp_w_route_path (fib_node_index_t path_index,
1181 const fib_route_path_t *rpath)
1186 path = fib_path_get(path_index);
1190 if (path->fp_weight != rpath->frp_weight)
1192 res = (path->fp_weight - rpath->frp_weight);
1197 * both paths are of the same type.
1198 * consider each type and its attributes in turn.
1200 switch (path->fp_type)
1202 case FIB_PATH_TYPE_ATTACHED_NEXT_HOP:
1203 res = ip46_address_cmp(&path->attached_next_hop.fp_nh,
1207 res = vnet_sw_interface_compare(
1209 path->attached_next_hop.fp_interface,
1210 rpath->frp_sw_if_index);
1213 case FIB_PATH_TYPE_ATTACHED:
1214 res = vnet_sw_interface_compare(
1216 path->attached.fp_interface,
1217 rpath->frp_sw_if_index);
1219 case FIB_PATH_TYPE_RECURSIVE:
1220 res = ip46_address_cmp(&path->recursive.fp_nh,
1225 res = (path->recursive.fp_tbl_id - rpath->frp_fib_index);
1228 case FIB_PATH_TYPE_DEAG:
1229 res = (path->deag.fp_tbl_id - rpath->frp_fib_index);
1231 case FIB_PATH_TYPE_SPECIAL:
1232 case FIB_PATH_TYPE_RECEIVE:
1233 case FIB_PATH_TYPE_EXCLUSIVE:
1242 * fib_path_recursive_loop_detect
1244 * A forward walk of the FIB object graph to detect for a cycle/loop. This
1245 * walk is initiated when an entry is linking to a new path list or from an old.
1246 * The entry vector passed contains all the FIB entrys that are children of this
1247 * path (it is all the entries encountered on the walk so far). If this vector
1248 * contains the entry this path resolve via, then a loop is about to form.
1249 * The loop must be allowed to form, since we need the dependencies in place
1250 * so that we can track when the loop breaks.
1251 * However, we MUST not produce a loop in the forwarding graph (else packets
1252 * would loop around the switch path until the loop breaks), so we mark recursive
1253 * paths as looped so that they do not contribute forwarding information.
1254 * By marking the path as looped, an etry such as;
1256 * via a.a.a.a (looped)
1257 * via b.b.b.b (not looped)
1258 * can still forward using the info provided by b.b.b.b only
1261 fib_path_recursive_loop_detect (fib_node_index_t path_index,
1262 fib_node_index_t **entry_indicies)
1266 path = fib_path_get(path_index);
1269 * the forced drop path is never looped, cos it is never resolved.
1271 if (fib_path_is_permanent_drop(path))
1276 switch (path->fp_type)
1278 case FIB_PATH_TYPE_RECURSIVE:
1280 fib_node_index_t *entry_index, *entries;
1282 entries = *entry_indicies;
1284 vec_foreach(entry_index, entries) {
1285 if (*entry_index == path->fp_via_fib)
1288 * the entry that is about to link to this path-list (or
1289 * one of this path-list's children) is the same entry that
1290 * this recursive path resolves through. this is a cycle.
1300 FIB_PATH_DBG(path, "recursive loop formed");
1301 path->fp_oper_flags |= FIB_PATH_OPER_FLAG_RECURSIVE_LOOP;
1303 dpo_copy(&path->fp_dpo,
1304 drop_dpo_get(fib_proto_to_dpo(path->fp_nh_proto)));
1309 * no loop here yet. keep forward walking the graph.
1311 if (fib_entry_recursive_loop_detect(path->fp_via_fib, entry_indicies))
1313 FIB_PATH_DBG(path, "recursive loop formed");
1314 path->fp_oper_flags |= FIB_PATH_OPER_FLAG_RECURSIVE_LOOP;
1318 FIB_PATH_DBG(path, "recursive loop cleared");
1319 path->fp_oper_flags &= ~FIB_PATH_OPER_FLAG_RECURSIVE_LOOP;
1324 case FIB_PATH_TYPE_ATTACHED_NEXT_HOP:
1325 case FIB_PATH_TYPE_ATTACHED:
1326 case FIB_PATH_TYPE_SPECIAL:
1327 case FIB_PATH_TYPE_DEAG:
1328 case FIB_PATH_TYPE_RECEIVE:
1329 case FIB_PATH_TYPE_EXCLUSIVE:
1331 * these path types cannot be part of a loop, since they are the leaves
1337 return (fib_path_is_looped(path_index));
1341 fib_path_resolve (fib_node_index_t path_index)
1345 path = fib_path_get(path_index);
1348 * hope for the best.
1350 path->fp_oper_flags |= FIB_PATH_OPER_FLAG_RESOLVED;
1353 * the forced drop path resolves via the drop adj
1355 if (fib_path_is_permanent_drop(path))
1357 dpo_copy(&path->fp_dpo,
1358 drop_dpo_get(fib_proto_to_dpo(path->fp_nh_proto)));
1359 path->fp_oper_flags &= ~FIB_PATH_OPER_FLAG_RESOLVED;
1360 return (fib_path_is_resolved(path_index));
1363 switch (path->fp_type)
1365 case FIB_PATH_TYPE_ATTACHED_NEXT_HOP:
1366 fib_path_attached_next_hop_set(path);
1368 case FIB_PATH_TYPE_ATTACHED:
1370 * path->attached.fp_interface
1372 if (!vnet_sw_interface_is_admin_up(vnet_get_main(),
1373 path->attached.fp_interface))
1375 path->fp_oper_flags &= ~FIB_PATH_OPER_FLAG_RESOLVED;
1377 if (vnet_sw_interface_is_p2p(vnet_get_main(),
1378 path->attached.fp_interface))
1381 * point-2-point interfaces do not require a glean, since
1382 * there is nothing to ARP. Install a rewrite/nbr adj instead
1384 dpo_set(&path->fp_dpo,
1386 fib_proto_to_dpo(path->fp_nh_proto),
1387 adj_nbr_add_or_lock(
1389 fib_proto_to_link(path->fp_nh_proto),
1391 path->attached.fp_interface));
1395 dpo_set(&path->fp_dpo,
1396 DPO_ADJACENCY_GLEAN,
1397 fib_proto_to_dpo(path->fp_nh_proto),
1398 adj_glean_add_or_lock(path->fp_nh_proto,
1399 path->attached.fp_interface,
1403 * become a child of the adjacency so we receive updates
1404 * when the interface state changes
1406 path->fp_sibling = adj_child_add(path->fp_dpo.dpoi_index,
1408 fib_path_get_index(path));
1411 case FIB_PATH_TYPE_RECURSIVE:
1414 * Create a RR source entry in the table for the address
1415 * that this path recurses through.
1416 * This resolve action is recursive, hence we may create
1417 * more paths in the process. more creates mean maybe realloc
1420 fib_node_index_t fei;
1423 ASSERT(FIB_NODE_INDEX_INVALID == path->fp_via_fib);
1425 fib_prefix_from_ip46_addr(&path->recursive.fp_nh, &pfx);
1427 fei = fib_table_entry_special_add(path->recursive.fp_tbl_id,
1430 FIB_ENTRY_FLAG_NONE,
1433 path = fib_path_get(path_index);
1434 path->fp_via_fib = fei;
1437 * become a dependent child of the entry so the path is
1438 * informed when the forwarding for the entry changes.
1440 path->fp_sibling = fib_entry_child_add(path->fp_via_fib,
1442 fib_path_get_index(path));
1445 * create and configure the IP DPO
1447 fib_path_recursive_adj_update(
1449 fib_path_proto_to_chain_type(path->fp_nh_proto),
1454 case FIB_PATH_TYPE_SPECIAL:
1456 * Resolve via the drop
1458 dpo_copy(&path->fp_dpo,
1459 drop_dpo_get(fib_proto_to_dpo(path->fp_nh_proto)));
1461 case FIB_PATH_TYPE_DEAG:
1463 * Resolve via a lookup DPO.
1464 * FIXME. control plane should add routes with a table ID
1466 lookup_dpo_add_or_lock_w_fib_index(path->deag.fp_tbl_id,
1467 fib_proto_to_dpo(path->fp_nh_proto),
1468 LOOKUP_INPUT_DST_ADDR,
1469 LOOKUP_TABLE_FROM_CONFIG,
1472 case FIB_PATH_TYPE_RECEIVE:
1474 * Resolve via a receive DPO.
1476 receive_dpo_add_or_lock(fib_proto_to_dpo(path->fp_nh_proto),
1477 path->receive.fp_interface,
1478 &path->receive.fp_addr,
1481 case FIB_PATH_TYPE_EXCLUSIVE:
1483 * Resolve via the user provided DPO
1485 dpo_copy(&path->fp_dpo, &path->exclusive.fp_ex_dpo);
1489 return (fib_path_is_resolved(path_index));
1493 fib_path_get_resolving_interface (fib_node_index_t path_index)
1497 path = fib_path_get(path_index);
1499 switch (path->fp_type)
1501 case FIB_PATH_TYPE_ATTACHED_NEXT_HOP:
1502 return (path->attached_next_hop.fp_interface);
1503 case FIB_PATH_TYPE_ATTACHED:
1504 return (path->attached.fp_interface);
1505 case FIB_PATH_TYPE_RECEIVE:
1506 return (path->receive.fp_interface);
1507 case FIB_PATH_TYPE_RECURSIVE:
1508 return (fib_entry_get_resolving_interface(path->fp_via_fib));
1509 case FIB_PATH_TYPE_SPECIAL:
1510 case FIB_PATH_TYPE_DEAG:
1511 case FIB_PATH_TYPE_EXCLUSIVE:
1518 fib_path_get_adj (fib_node_index_t path_index)
1522 path = fib_path_get(path_index);
1524 ASSERT(dpo_is_adj(&path->fp_dpo));
1525 if (dpo_is_adj(&path->fp_dpo))
1527 return (path->fp_dpo.dpoi_index);
1529 return (ADJ_INDEX_INVALID);
1533 fib_path_get_weight (fib_node_index_t path_index)
1537 path = fib_path_get(path_index);
1541 return (path->fp_weight);
1545 fib_path_contribute_forwarding (fib_node_index_t path_index,
1546 fib_forward_chain_type_t fct,
1551 path = fib_path_get(path_index);
1554 ASSERT(FIB_FORW_CHAIN_TYPE_MPLS_EOS != fct);
1556 FIB_PATH_DBG(path, "contribute");
1559 * The DPO stored in the path was created when the path was resolved.
1560 * This then represents the path's 'native' protocol; IP.
1561 * For all others will need to go find something else.
1563 if (fib_path_proto_to_chain_type(path->fp_nh_proto) == fct)
1565 dpo_copy(dpo, &path->fp_dpo);
1569 switch (path->fp_type)
1571 case FIB_PATH_TYPE_ATTACHED_NEXT_HOP:
1574 case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
1575 case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
1576 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
1577 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
1578 case FIB_FORW_CHAIN_TYPE_ETHERNET:
1583 * get a MPLS link type adj.
1585 ai = fib_path_attached_next_hop_get_adj(
1587 fib_forw_chain_type_to_link_type(fct));
1588 dpo_set(dpo, DPO_ADJACENCY,
1589 fib_forw_chain_type_to_dpo_proto(fct), ai);
1596 case FIB_PATH_TYPE_RECURSIVE:
1599 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
1600 case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
1601 case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
1603 * Assume that EOS and IP forwarding is the same.
1606 dpo_copy(dpo, &path->fp_dpo);
1608 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
1609 fib_path_recursive_adj_update(path, fct, dpo);
1611 case FIB_FORW_CHAIN_TYPE_ETHERNET:
1616 case FIB_PATH_TYPE_DEAG:
1619 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
1620 lookup_dpo_add_or_lock_w_table_id(MPLS_FIB_DEFAULT_TABLE_ID,
1622 LOOKUP_INPUT_DST_ADDR,
1623 LOOKUP_TABLE_FROM_CONFIG,
1626 case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
1627 case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
1628 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
1629 dpo_copy(dpo, &path->fp_dpo);
1631 case FIB_FORW_CHAIN_TYPE_ETHERNET:
1636 case FIB_PATH_TYPE_EXCLUSIVE:
1637 dpo_copy(dpo, &path->exclusive.fp_ex_dpo);
1639 case FIB_PATH_TYPE_ATTACHED:
1640 case FIB_PATH_TYPE_RECEIVE:
1641 case FIB_PATH_TYPE_SPECIAL:
1649 load_balance_path_t *
1650 fib_path_append_nh_for_multipath_hash (fib_node_index_t path_index,
1651 fib_forward_chain_type_t fct,
1652 load_balance_path_t *hash_key)
1654 load_balance_path_t *mnh;
1657 path = fib_path_get(path_index);
1661 if (fib_path_is_resolved(path_index))
1663 vec_add2(hash_key, mnh, 1);
1665 mnh->path_weight = path->fp_weight;
1666 mnh->path_index = path_index;
1667 fib_path_contribute_forwarding(path_index, fct, &mnh->path_dpo);
1674 fib_path_is_recursive (fib_node_index_t path_index)
1678 path = fib_path_get(path_index);
1680 return (FIB_PATH_TYPE_RECURSIVE == path->fp_type);
1684 fib_path_is_exclusive (fib_node_index_t path_index)
1688 path = fib_path_get(path_index);
1690 return (FIB_PATH_TYPE_EXCLUSIVE == path->fp_type);
1694 fib_path_is_deag (fib_node_index_t path_index)
1698 path = fib_path_get(path_index);
1700 return (FIB_PATH_TYPE_DEAG == path->fp_type);
1704 fib_path_is_resolved (fib_node_index_t path_index)
1708 path = fib_path_get(path_index);
1710 return (dpo_id_is_valid(&path->fp_dpo) &&
1711 (path->fp_oper_flags & FIB_PATH_OPER_FLAG_RESOLVED) &&
1712 !fib_path_is_looped(path_index) &&
1713 !fib_path_is_permanent_drop(path));
1717 fib_path_is_looped (fib_node_index_t path_index)
1721 path = fib_path_get(path_index);
1723 return (path->fp_oper_flags & FIB_PATH_OPER_FLAG_RECURSIVE_LOOP);
1727 fib_path_module_init (void)
1729 fib_node_register_type (FIB_NODE_TYPE_PATH, &fib_path_vft);
1732 static clib_error_t *
1733 show_fib_path_command (vlib_main_t * vm,
1734 unformat_input_t * input,
1735 vlib_cli_command_t * cmd)
1739 vlib_cli_output (vm, "FIB Path Lists");
1740 pool_foreach(path, fib_path_pool,
1742 vlib_cli_output (vm, "%U", format_fib_path, path);
1748 VLIB_CLI_COMMAND (show_fib_path, static) = {
1749 .path = "show fib paths",
1750 .function = show_fib_path_command,
1751 .short_help = "show fib paths",