1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Nexthop structure definition.
4 * Copyright (C) 1997, 98, 99, 2001 Kunihiro Ishiguro
5 * Copyright (C) 2013 Cumulus Networks, Inc.
20 /* Maximum next hop string length - gateway + ifindex */
21 #define NEXTHOP_STRLEN (INET6_ADDRSTRLEN + 30)
28 enum nexthop_types_t
{
29 NEXTHOP_TYPE_IFINDEX
= 1, /* Directly connected. */
30 NEXTHOP_TYPE_IPV4
, /* IPv4 nexthop. */
31 NEXTHOP_TYPE_IPV4_IFINDEX
, /* IPv4 nexthop with ifindex. */
32 NEXTHOP_TYPE_IPV6
, /* IPv6 nexthop. */
33 NEXTHOP_TYPE_IPV6_IFINDEX
, /* IPv6 nexthop with ifindex. */
34 NEXTHOP_TYPE_BLACKHOLE
, /* Null0 nexthop. */
41 BLACKHOLE_ADMINPROHIB
,
45 NET_VXLAN
= 100, /* value copied from FPM_NH_ENCAP_VXLAN. */
48 /* Fixed limit on the number of backup nexthops per primary nexthop */
49 #define NEXTHOP_MAX_BACKUPS 8
51 /* Backup index value is limited */
52 #define NEXTHOP_BACKUP_IDX_MAX 255
54 /* Nexthop structure. */
60 * What vrf is this nexthop associated with?
64 /* Interface index. */
67 enum nexthop_types_t type
;
70 #define NEXTHOP_FLAG_ACTIVE (1 << 0) /* This nexthop is alive. */
71 #define NEXTHOP_FLAG_FIB (1 << 1) /* FIB nexthop. */
72 #define NEXTHOP_FLAG_RECURSIVE (1 << 2) /* Recursive nexthop. */
73 #define NEXTHOP_FLAG_ONLINK (1 << 3) /* Nexthop should be installed
76 #define NEXTHOP_FLAG_DUPLICATE (1 << 4) /* nexthop duplicates another
79 #define NEXTHOP_FLAG_RNH_FILTERED (1 << 5) /* rmap filtered, used by rnh */
80 #define NEXTHOP_FLAG_HAS_BACKUP (1 << 6) /* Backup nexthop index is set */
81 #define NEXTHOP_FLAG_SRTE (1 << 7) /* SR-TE color used for BGP traffic */
82 #define NEXTHOP_FLAG_EVPN (1 << 8) /* nexthop is EVPN */
83 #define NEXTHOP_FLAG_LINKDOWN (1 << 9) /* is not removed on link down */
85 #define NEXTHOP_IS_ACTIVE(flags) \
86 (CHECK_FLAG(flags, NEXTHOP_FLAG_ACTIVE) \
87 && !CHECK_FLAG(flags, NEXTHOP_FLAG_DUPLICATE))
92 enum blackhole_type bh_type
;
95 union g_addr rmap_src
; /* Src is set via routemap */
97 /* Nexthops obtained by recursive resolution.
99 * If the nexthop struct needs to be resolved recursively,
100 * NEXTHOP_FLAG_RECURSIVE will be set in flags and the nexthops
101 * obtained by recursive resolution will be added to `resolved'.
103 struct nexthop
*resolved
;
104 /* Recursive parent */
105 struct nexthop
*rparent
;
107 /* Type of label(s), if any */
108 enum lsp_types_t nh_label_type
;
110 /* Label(s) associated with this nexthop. */
111 struct mpls_label_stack
*nh_label
;
113 /* Weight of the nexthop ( for unequal cost ECMP ) */
116 /* Count and index of corresponding backup nexthop(s) in a backup list;
117 * only meaningful if the HAS_BACKUP flag is set.
120 uint8_t backup_idx
[NEXTHOP_MAX_BACKUPS
];
122 /* Encapsulation information. */
123 enum nh_encap_type nh_encap_type
;
128 /* SR-TE color used for matching SR-TE policies */
131 /* SRv6 information */
132 struct nexthop_srv6
*nh_srv6
;
135 /* Utility to append one nexthop to another. */
136 #define NEXTHOP_APPEND(to, new) \
138 (to)->next = (new); \
139 (new)->prev = (to); \
140 (new)->next = NULL; \
143 struct nexthop
*nexthop_new(void);
145 void nexthop_free(struct nexthop
*nexthop
);
146 void nexthops_free(struct nexthop
*nexthop
);
148 void nexthop_add_labels(struct nexthop
*nexthop
, enum lsp_types_t ltype
,
149 uint8_t num_labels
, const mpls_label_t
*labels
);
150 void nexthop_del_labels(struct nexthop
*);
151 void nexthop_add_srv6_seg6local(struct nexthop
*nexthop
, uint32_t action
,
152 const struct seg6local_context
*ctx
);
153 void nexthop_del_srv6_seg6local(struct nexthop
*nexthop
);
154 void nexthop_add_srv6_seg6(struct nexthop
*nexthop
,
155 const struct in6_addr
*segs
);
156 void nexthop_del_srv6_seg6(struct nexthop
*nexthop
);
159 * Allocate a new nexthop object and initialize it from various args.
161 struct nexthop
*nexthop_from_ifindex(ifindex_t ifindex
, vrf_id_t vrf_id
);
162 struct nexthop
*nexthop_from_ipv4(const struct in_addr
*ipv4
,
163 const struct in_addr
*src
,
165 struct nexthop
*nexthop_from_ipv4_ifindex(const struct in_addr
*ipv4
,
166 const struct in_addr
*src
,
167 ifindex_t ifindex
, vrf_id_t vrf_id
);
168 struct nexthop
*nexthop_from_ipv6(const struct in6_addr
*ipv6
,
170 struct nexthop
*nexthop_from_ipv6_ifindex(const struct in6_addr
*ipv6
,
171 ifindex_t ifindex
, vrf_id_t vrf_id
);
172 struct nexthop
*nexthop_from_blackhole(enum blackhole_type bh_type
,
176 * Hash a nexthop. Suitable for use with hash tables.
178 * This function uses the following values when computing the hash:
185 * The nexthop to hash
188 * 32-bit hash of nexthop
190 uint32_t nexthop_hash(const struct nexthop
*nexthop
);
192 * Hash a nexthop only on word-sized attributes:
198 uint32_t nexthop_hash_quick(const struct nexthop
*nexthop
);
200 extern bool nexthop_same(const struct nexthop
*nh1
, const struct nexthop
*nh2
);
201 extern bool nexthop_same_no_labels(const struct nexthop
*nh1
,
202 const struct nexthop
*nh2
);
203 extern int nexthop_cmp(const struct nexthop
*nh1
, const struct nexthop
*nh2
);
204 extern int nexthop_g_addr_cmp(enum nexthop_types_t type
,
205 const union g_addr
*addr1
,
206 const union g_addr
*addr2
);
208 /* More-limited comparison function used to detect duplicate nexthops.
211 int nexthop_cmp_basic(const struct nexthop
*nh1
, const struct nexthop
*nh2
);
213 extern const char *nexthop_type_to_str(enum nexthop_types_t nh_type
);
214 extern bool nexthop_labels_match(const struct nexthop
*nh1
,
215 const struct nexthop
*nh2
);
217 extern const char *nexthop2str(const struct nexthop
*nexthop
,
218 char *str
, int size
);
219 extern struct nexthop
*nexthop_next(const struct nexthop
*nexthop
);
220 extern struct nexthop
*
221 nexthop_next_active_resolved(const struct nexthop
*nexthop
);
222 extern unsigned int nexthop_level(const struct nexthop
*nexthop
);
223 /* Copies to an already allocated nexthop struct */
224 extern void nexthop_copy(struct nexthop
*copy
, const struct nexthop
*nexthop
,
225 struct nexthop
*rparent
);
226 /* Copies to an already allocated nexthop struct, not including recurse info */
227 extern void nexthop_copy_no_recurse(struct nexthop
*copy
,
228 const struct nexthop
*nexthop
,
229 struct nexthop
*rparent
);
230 /* Duplicates a nexthop and returns the newly allocated nexthop */
231 extern struct nexthop
*nexthop_dup(const struct nexthop
*nexthop
,
232 struct nexthop
*rparent
);
233 /* Duplicates a nexthop and returns the newly allocated nexthop */
234 extern struct nexthop
*nexthop_dup_no_recurse(const struct nexthop
*nexthop
,
235 struct nexthop
*rparent
);
237 /* Check nexthop of IFINDEX type */
238 extern bool nexthop_is_ifindex_type(const struct nexthop
*nh
);
241 * Parse one or more backup index values, as comma-separated numbers,
242 * into caller's array of uint8_ts. The array must be NEXTHOP_MAX_BACKUPS
243 * in size. Mails back the number of values converted, and returns 0 on
244 * success, <0 if an error in parsing.
246 int nexthop_str2backups(const char *str
, int *num_backups
,
249 #ifdef _FRR_ATTRIBUTE_PRINTFRR
250 #pragma FRR printfrr_ext "%pNH" (struct nexthop *)
253 ssize_t
printfrr_nhs(struct fbuf
*buf
, const struct nexthop
*nh
);
258 #endif /*_LIB_NEXTHOP_H */