]> git.proxmox.com Git - mirror_frr.git/blob - lib/nexthop.h
Merge pull request #13484 from sri-mohan1/srib-ldpd
[mirror_frr.git] / lib / nexthop.h
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Nexthop structure definition.
4 * Copyright (C) 1997, 98, 99, 2001 Kunihiro Ishiguro
5 * Copyright (C) 2013 Cumulus Networks, Inc.
6 */
7
8 #ifndef _LIB_NEXTHOP_H
9 #define _LIB_NEXTHOP_H
10
11 #include "prefix.h"
12 #include "mpls.h"
13 #include "vxlan.h"
14 #include "srv6.h"
15
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19
20 /* Maximum next hop string length - gateway + ifindex */
21 #define NEXTHOP_STRLEN (INET6_ADDRSTRLEN + 30)
22
23 union g_addr {
24 struct in_addr ipv4;
25 struct in6_addr ipv6;
26 };
27
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. */
35 };
36
37 enum blackhole_type {
38 BLACKHOLE_UNSPEC = 0,
39 BLACKHOLE_NULL,
40 BLACKHOLE_REJECT,
41 BLACKHOLE_ADMINPROHIB,
42 };
43
44 enum nh_encap_type {
45 NET_VXLAN = 100, /* value copied from FPM_NH_ENCAP_VXLAN. */
46 };
47
48 /* Fixed limit on the number of backup nexthops per primary nexthop */
49 #define NEXTHOP_MAX_BACKUPS 8
50
51 /* Backup index value is limited */
52 #define NEXTHOP_BACKUP_IDX_MAX 255
53
54 /* Nexthop structure. */
55 struct nexthop {
56 struct nexthop *next;
57 struct nexthop *prev;
58
59 /*
60 * What vrf is this nexthop associated with?
61 */
62 vrf_id_t vrf_id;
63
64 /* Interface index. */
65 ifindex_t ifindex;
66
67 enum nexthop_types_t type;
68
69 uint16_t flags;
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
74 * onlink.
75 */
76 #define NEXTHOP_FLAG_DUPLICATE (1 << 4) /* nexthop duplicates another
77 * active one
78 */
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 */
84
85 #define NEXTHOP_IS_ACTIVE(flags) \
86 (CHECK_FLAG(flags, NEXTHOP_FLAG_ACTIVE) \
87 && !CHECK_FLAG(flags, NEXTHOP_FLAG_DUPLICATE))
88
89 /* Nexthop address */
90 union {
91 union g_addr gate;
92 enum blackhole_type bh_type;
93 };
94 union g_addr src;
95 union g_addr rmap_src; /* Src is set via routemap */
96
97 /* Nexthops obtained by recursive resolution.
98 *
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'.
102 */
103 struct nexthop *resolved;
104 /* Recursive parent */
105 struct nexthop *rparent;
106
107 /* Type of label(s), if any */
108 enum lsp_types_t nh_label_type;
109
110 /* Label(s) associated with this nexthop. */
111 struct mpls_label_stack *nh_label;
112
113 /* Weight of the nexthop ( for unequal cost ECMP ) */
114 uint8_t weight;
115
116 /* Count and index of corresponding backup nexthop(s) in a backup list;
117 * only meaningful if the HAS_BACKUP flag is set.
118 */
119 uint8_t backup_num;
120 uint8_t backup_idx[NEXTHOP_MAX_BACKUPS];
121
122 /* Encapsulation information. */
123 enum nh_encap_type nh_encap_type;
124 union {
125 vni_t vni;
126 } nh_encap;
127
128 /* SR-TE color used for matching SR-TE policies */
129 uint32_t srte_color;
130
131 /* SRv6 information */
132 struct nexthop_srv6 *nh_srv6;
133 };
134
135 /* Utility to append one nexthop to another. */
136 #define NEXTHOP_APPEND(to, new) \
137 do { \
138 (to)->next = (new); \
139 (new)->prev = (to); \
140 (new)->next = NULL; \
141 } while (0)
142
143 struct nexthop *nexthop_new(void);
144
145 void nexthop_free(struct nexthop *nexthop);
146 void nexthops_free(struct nexthop *nexthop);
147
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);
157
158 /*
159 * Allocate a new nexthop object and initialize it from various args.
160 */
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,
164 vrf_id_t vrf_id);
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,
169 vrf_id_t vrf_id);
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,
173 vrf_id_t nh_vrf_id);
174
175 /*
176 * Hash a nexthop. Suitable for use with hash tables.
177 *
178 * This function uses the following values when computing the hash:
179 * - vrf_id
180 * - ifindex
181 * - type
182 * - gate
183 *
184 * nexthop
185 * The nexthop to hash
186 *
187 * Returns:
188 * 32-bit hash of nexthop
189 */
190 uint32_t nexthop_hash(const struct nexthop *nexthop);
191 /*
192 * Hash a nexthop only on word-sized attributes:
193 * - vrf_id
194 * - ifindex
195 * - type
196 * - (some) flags
197 */
198 uint32_t nexthop_hash_quick(const struct nexthop *nexthop);
199
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);
207
208 /* More-limited comparison function used to detect duplicate nexthops.
209 * Returns -1, 0, 1
210 */
211 int nexthop_cmp_basic(const struct nexthop *nh1, const struct nexthop *nh2);
212
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);
216
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);
236
237 /* Check nexthop of IFINDEX type */
238 extern bool nexthop_is_ifindex_type(const struct nexthop *nh);
239
240 /*
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.
245 */
246 int nexthop_str2backups(const char *str, int *num_backups,
247 uint8_t *backups);
248
249 #ifdef _FRR_ATTRIBUTE_PRINTFRR
250 #pragma FRR printfrr_ext "%pNH" (struct nexthop *)
251 #endif
252
253 ssize_t printfrr_nhs(struct fbuf *buf, const struct nexthop *nh);
254 #ifdef __cplusplus
255 }
256 #endif
257
258 #endif /*_LIB_NEXTHOP_H */