]> git.proxmox.com Git - mirror_frr.git/blob - lib/nexthop.h
Merge pull request #11302 from punith-shivakumar/master
[mirror_frr.git] / lib / nexthop.h
1 /*
2 * Nexthop structure definition.
3 * Copyright (C) 1997, 98, 99, 2001 Kunihiro Ishiguro
4 * Copyright (C) 2013 Cumulus Networks, Inc.
5 *
6 * This file is part of Quagga.
7 *
8 * Quagga is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
11 * later version.
12 *
13 * Quagga is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; see the file COPYING; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #ifndef _LIB_NEXTHOP_H
24 #define _LIB_NEXTHOP_H
25
26 #include "prefix.h"
27 #include "mpls.h"
28 #include "vxlan.h"
29 #include "srv6.h"
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 /* Maximum next hop string length - gateway + ifindex */
36 #define NEXTHOP_STRLEN (INET6_ADDRSTRLEN + 30)
37
38 union g_addr {
39 struct in_addr ipv4;
40 struct in6_addr ipv6;
41 };
42
43 enum nexthop_types_t {
44 NEXTHOP_TYPE_IFINDEX = 1, /* Directly connected. */
45 NEXTHOP_TYPE_IPV4, /* IPv4 nexthop. */
46 NEXTHOP_TYPE_IPV4_IFINDEX, /* IPv4 nexthop with ifindex. */
47 NEXTHOP_TYPE_IPV6, /* IPv6 nexthop. */
48 NEXTHOP_TYPE_IPV6_IFINDEX, /* IPv6 nexthop with ifindex. */
49 NEXTHOP_TYPE_BLACKHOLE, /* Null0 nexthop. */
50 };
51
52 enum blackhole_type {
53 BLACKHOLE_UNSPEC = 0,
54 BLACKHOLE_NULL,
55 BLACKHOLE_REJECT,
56 BLACKHOLE_ADMINPROHIB,
57 };
58
59 enum nh_encap_type {
60 NET_VXLAN = 100, /* value copied from FPM_NH_ENCAP_VXLAN. */
61 };
62
63 /* Fixed limit on the number of backup nexthops per primary nexthop */
64 #define NEXTHOP_MAX_BACKUPS 8
65
66 /* Backup index value is limited */
67 #define NEXTHOP_BACKUP_IDX_MAX 255
68
69 /* Nexthop structure. */
70 struct nexthop {
71 struct nexthop *next;
72 struct nexthop *prev;
73
74 /*
75 * What vrf is this nexthop associated with?
76 */
77 vrf_id_t vrf_id;
78
79 /* Interface index. */
80 ifindex_t ifindex;
81
82 enum nexthop_types_t type;
83
84 uint16_t flags;
85 #define NEXTHOP_FLAG_ACTIVE (1 << 0) /* This nexthop is alive. */
86 #define NEXTHOP_FLAG_FIB (1 << 1) /* FIB nexthop. */
87 #define NEXTHOP_FLAG_RECURSIVE (1 << 2) /* Recursive nexthop. */
88 #define NEXTHOP_FLAG_ONLINK (1 << 3) /* Nexthop should be installed
89 * onlink.
90 */
91 #define NEXTHOP_FLAG_DUPLICATE (1 << 4) /* nexthop duplicates another
92 * active one
93 */
94 #define NEXTHOP_FLAG_RNH_FILTERED (1 << 5) /* rmap filtered, used by rnh */
95 #define NEXTHOP_FLAG_HAS_BACKUP (1 << 6) /* Backup nexthop index is set */
96 #define NEXTHOP_FLAG_SRTE (1 << 7) /* SR-TE color used for BGP traffic */
97 #define NEXTHOP_FLAG_EVPN (1 << 8) /* nexthop is EVPN */
98 #define NEXTHOP_FLAG_LINKDOWN (1 << 9) /* is not removed on link down */
99
100 #define NEXTHOP_IS_ACTIVE(flags) \
101 (CHECK_FLAG(flags, NEXTHOP_FLAG_ACTIVE) \
102 && !CHECK_FLAG(flags, NEXTHOP_FLAG_DUPLICATE))
103
104 /* Nexthop address */
105 union {
106 union g_addr gate;
107 enum blackhole_type bh_type;
108 };
109 union g_addr src;
110 union g_addr rmap_src; /* Src is set via routemap */
111
112 /* Nexthops obtained by recursive resolution.
113 *
114 * If the nexthop struct needs to be resolved recursively,
115 * NEXTHOP_FLAG_RECURSIVE will be set in flags and the nexthops
116 * obtained by recursive resolution will be added to `resolved'.
117 */
118 struct nexthop *resolved;
119 /* Recursive parent */
120 struct nexthop *rparent;
121
122 /* Type of label(s), if any */
123 enum lsp_types_t nh_label_type;
124
125 /* Label(s) associated with this nexthop. */
126 struct mpls_label_stack *nh_label;
127
128 /* Weight of the nexthop ( for unequal cost ECMP ) */
129 uint8_t weight;
130
131 /* Count and index of corresponding backup nexthop(s) in a backup list;
132 * only meaningful if the HAS_BACKUP flag is set.
133 */
134 uint8_t backup_num;
135 uint8_t backup_idx[NEXTHOP_MAX_BACKUPS];
136
137 /* Encapsulation information. */
138 enum nh_encap_type nh_encap_type;
139 union {
140 vni_t vni;
141 } nh_encap;
142
143 /* SR-TE color used for matching SR-TE policies */
144 uint32_t srte_color;
145
146 /* SRv6 information */
147 struct nexthop_srv6 *nh_srv6;
148 };
149
150 /* Utility to append one nexthop to another. */
151 #define NEXTHOP_APPEND(to, new) \
152 do { \
153 (to)->next = (new); \
154 (new)->prev = (to); \
155 (new)->next = NULL; \
156 } while (0)
157
158 struct nexthop *nexthop_new(void);
159
160 void nexthop_free(struct nexthop *nexthop);
161 void nexthops_free(struct nexthop *nexthop);
162
163 void nexthop_add_labels(struct nexthop *nexthop, enum lsp_types_t ltype,
164 uint8_t num_labels, const mpls_label_t *labels);
165 void nexthop_del_labels(struct nexthop *);
166 void nexthop_add_srv6_seg6local(struct nexthop *nexthop, uint32_t action,
167 const struct seg6local_context *ctx);
168 void nexthop_del_srv6_seg6local(struct nexthop *nexthop);
169 void nexthop_add_srv6_seg6(struct nexthop *nexthop,
170 const struct in6_addr *segs);
171 void nexthop_del_srv6_seg6(struct nexthop *nexthop);
172
173 /*
174 * Allocate a new nexthop object and initialize it from various args.
175 */
176 struct nexthop *nexthop_from_ifindex(ifindex_t ifindex, vrf_id_t vrf_id);
177 struct nexthop *nexthop_from_ipv4(const struct in_addr *ipv4,
178 const struct in_addr *src,
179 vrf_id_t vrf_id);
180 struct nexthop *nexthop_from_ipv4_ifindex(const struct in_addr *ipv4,
181 const struct in_addr *src,
182 ifindex_t ifindex, vrf_id_t vrf_id);
183 struct nexthop *nexthop_from_ipv6(const struct in6_addr *ipv6,
184 vrf_id_t vrf_id);
185 struct nexthop *nexthop_from_ipv6_ifindex(const struct in6_addr *ipv6,
186 ifindex_t ifindex, vrf_id_t vrf_id);
187 struct nexthop *nexthop_from_blackhole(enum blackhole_type bh_type,
188 vrf_id_t nh_vrf_id);
189
190 /*
191 * Hash a nexthop. Suitable for use with hash tables.
192 *
193 * This function uses the following values when computing the hash:
194 * - vrf_id
195 * - ifindex
196 * - type
197 * - gate
198 *
199 * nexthop
200 * The nexthop to hash
201 *
202 * Returns:
203 * 32-bit hash of nexthop
204 */
205 uint32_t nexthop_hash(const struct nexthop *nexthop);
206 /*
207 * Hash a nexthop only on word-sized attributes:
208 * - vrf_id
209 * - ifindex
210 * - type
211 * - (some) flags
212 */
213 uint32_t nexthop_hash_quick(const struct nexthop *nexthop);
214
215 extern bool nexthop_same(const struct nexthop *nh1, const struct nexthop *nh2);
216 extern bool nexthop_same_no_labels(const struct nexthop *nh1,
217 const struct nexthop *nh2);
218 extern int nexthop_cmp(const struct nexthop *nh1, const struct nexthop *nh2);
219 extern int nexthop_g_addr_cmp(enum nexthop_types_t type,
220 const union g_addr *addr1,
221 const union g_addr *addr2);
222
223 /* More-limited comparison function used to detect duplicate nexthops.
224 * Returns -1, 0, 1
225 */
226 int nexthop_cmp_basic(const struct nexthop *nh1, const struct nexthop *nh2);
227
228 extern const char *nexthop_type_to_str(enum nexthop_types_t nh_type);
229 extern bool nexthop_labels_match(const struct nexthop *nh1,
230 const struct nexthop *nh2);
231
232 extern const char *nexthop2str(const struct nexthop *nexthop,
233 char *str, int size);
234 extern struct nexthop *nexthop_next(const struct nexthop *nexthop);
235 extern struct nexthop *
236 nexthop_next_active_resolved(const struct nexthop *nexthop);
237 extern unsigned int nexthop_level(const struct nexthop *nexthop);
238 /* Copies to an already allocated nexthop struct */
239 extern void nexthop_copy(struct nexthop *copy, const struct nexthop *nexthop,
240 struct nexthop *rparent);
241 /* Copies to an already allocated nexthop struct, not including recurse info */
242 extern void nexthop_copy_no_recurse(struct nexthop *copy,
243 const struct nexthop *nexthop,
244 struct nexthop *rparent);
245 /* Duplicates a nexthop and returns the newly allocated nexthop */
246 extern struct nexthop *nexthop_dup(const struct nexthop *nexthop,
247 struct nexthop *rparent);
248 /* Duplicates a nexthop and returns the newly allocated nexthop */
249 extern struct nexthop *nexthop_dup_no_recurse(const struct nexthop *nexthop,
250 struct nexthop *rparent);
251
252 /*
253 * Parse one or more backup index values, as comma-separated numbers,
254 * into caller's array of uint8_ts. The array must be NEXTHOP_MAX_BACKUPS
255 * in size. Mails back the number of values converted, and returns 0 on
256 * success, <0 if an error in parsing.
257 */
258 int nexthop_str2backups(const char *str, int *num_backups,
259 uint8_t *backups);
260
261 #ifdef _FRR_ATTRIBUTE_PRINTFRR
262 #pragma FRR printfrr_ext "%pNH" (struct nexthop *)
263 #endif
264
265 ssize_t printfrr_nhs(struct fbuf *buf, const struct nexthop *nh);
266 #ifdef __cplusplus
267 }
268 #endif
269
270 #endif /*_LIB_NEXTHOP_H */