]>
Commit | Line | Data |
---|---|---|
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 | ||
30 | #ifdef __cplusplus | |
31 | extern "C" { | |
32 | #endif | |
33 | ||
34 | /* Maximum next hop string length - gateway + ifindex */ | |
35 | #define NEXTHOP_STRLEN (INET6_ADDRSTRLEN + 30) | |
36 | ||
37 | union g_addr { | |
38 | struct in_addr ipv4; | |
39 | struct in6_addr ipv6; | |
40 | }; | |
41 | ||
42 | enum nexthop_types_t { | |
43 | NEXTHOP_TYPE_IFINDEX = 1, /* Directly connected. */ | |
44 | NEXTHOP_TYPE_IPV4, /* IPv4 nexthop. */ | |
45 | NEXTHOP_TYPE_IPV4_IFINDEX, /* IPv4 nexthop with ifindex. */ | |
46 | NEXTHOP_TYPE_IPV6, /* IPv6 nexthop. */ | |
47 | NEXTHOP_TYPE_IPV6_IFINDEX, /* IPv6 nexthop with ifindex. */ | |
48 | NEXTHOP_TYPE_BLACKHOLE, /* Null0 nexthop. */ | |
49 | }; | |
50 | ||
51 | enum blackhole_type { | |
52 | BLACKHOLE_UNSPEC = 0, | |
53 | BLACKHOLE_NULL, | |
54 | BLACKHOLE_REJECT, | |
55 | BLACKHOLE_ADMINPROHIB, | |
56 | }; | |
57 | ||
58 | enum nh_encap_type { | |
59 | NET_VXLAN = 100, /* value copied from FPM_NH_ENCAP_VXLAN. */ | |
60 | }; | |
61 | ||
62 | /* Fixed limit on the number of backup nexthops per primary nexthop */ | |
63 | #define NEXTHOP_MAX_BACKUPS 8 | |
64 | ||
65 | /* Backup index value is limited */ | |
66 | #define NEXTHOP_BACKUP_IDX_MAX 255 | |
67 | ||
68 | /* Nexthop structure. */ | |
69 | struct nexthop { | |
70 | struct nexthop *next; | |
71 | struct nexthop *prev; | |
72 | ||
73 | /* | |
74 | * What vrf is this nexthop associated with? | |
75 | */ | |
76 | vrf_id_t vrf_id; | |
77 | ||
78 | /* Interface index. */ | |
79 | ifindex_t ifindex; | |
80 | ||
81 | enum nexthop_types_t type; | |
82 | ||
83 | uint8_t flags; | |
84 | #define NEXTHOP_FLAG_ACTIVE (1 << 0) /* This nexthop is alive. */ | |
85 | #define NEXTHOP_FLAG_FIB (1 << 1) /* FIB nexthop. */ | |
86 | #define NEXTHOP_FLAG_RECURSIVE (1 << 2) /* Recursive nexthop. */ | |
87 | #define NEXTHOP_FLAG_ONLINK (1 << 3) /* Nexthop should be installed | |
88 | * onlink. | |
89 | */ | |
90 | #define NEXTHOP_FLAG_DUPLICATE (1 << 4) /* nexthop duplicates another | |
91 | * active one | |
92 | */ | |
93 | #define NEXTHOP_FLAG_RNH_FILTERED (1 << 5) /* rmap filtered, used by rnh */ | |
94 | #define NEXTHOP_FLAG_HAS_BACKUP (1 << 6) /* Backup nexthop index is set */ | |
95 | #define NEXTHOP_FLAG_SRTE (1 << 7) /* SR-TE color used for BGP traffic */ | |
96 | ||
97 | #define NEXTHOP_IS_ACTIVE(flags) \ | |
98 | (CHECK_FLAG(flags, NEXTHOP_FLAG_ACTIVE) \ | |
99 | && !CHECK_FLAG(flags, NEXTHOP_FLAG_DUPLICATE)) | |
100 | ||
101 | /* Nexthop address */ | |
102 | union { | |
103 | union g_addr gate; | |
104 | enum blackhole_type bh_type; | |
105 | }; | |
106 | union g_addr src; | |
107 | union g_addr rmap_src; /* Src is set via routemap */ | |
108 | ||
109 | /* Nexthops obtained by recursive resolution. | |
110 | * | |
111 | * If the nexthop struct needs to be resolved recursively, | |
112 | * NEXTHOP_FLAG_RECURSIVE will be set in flags and the nexthops | |
113 | * obtained by recursive resolution will be added to `resolved'. | |
114 | */ | |
115 | struct nexthop *resolved; | |
116 | /* Recursive parent */ | |
117 | struct nexthop *rparent; | |
118 | ||
119 | /* Type of label(s), if any */ | |
120 | enum lsp_types_t nh_label_type; | |
121 | ||
122 | /* Label(s) associated with this nexthop. */ | |
123 | struct mpls_label_stack *nh_label; | |
124 | ||
125 | /* Weight of the nexthop ( for unequal cost ECMP ) */ | |
126 | uint8_t weight; | |
127 | ||
128 | /* Count and index of corresponding backup nexthop(s) in a backup list; | |
129 | * only meaningful if the HAS_BACKUP flag is set. | |
130 | */ | |
131 | uint8_t backup_num; | |
132 | uint8_t backup_idx[NEXTHOP_MAX_BACKUPS]; | |
133 | ||
134 | /* Encapsulation information. */ | |
135 | enum nh_encap_type nh_encap_type; | |
136 | union { | |
137 | vni_t vni; | |
138 | } nh_encap; | |
139 | ||
140 | /* SR-TE color used for matching SR-TE policies */ | |
141 | uint32_t srte_color; | |
142 | }; | |
143 | ||
144 | /* Utility to append one nexthop to another. */ | |
145 | #define NEXTHOP_APPEND(to, new) \ | |
146 | do { \ | |
147 | (to)->next = (new); \ | |
148 | (new)->prev = (to); \ | |
149 | (new)->next = NULL; \ | |
150 | } while (0) | |
151 | ||
152 | struct nexthop *nexthop_new(void); | |
153 | ||
154 | void nexthop_free(struct nexthop *nexthop); | |
155 | void nexthops_free(struct nexthop *nexthop); | |
156 | ||
157 | void nexthop_add_labels(struct nexthop *nexthop, enum lsp_types_t ltype, | |
158 | uint8_t num_labels, const mpls_label_t *labels); | |
159 | void nexthop_del_labels(struct nexthop *); | |
160 | ||
161 | /* | |
162 | * Allocate a new nexthop object and initialize it from various args. | |
163 | */ | |
164 | struct nexthop *nexthop_from_ifindex(ifindex_t ifindex, vrf_id_t vrf_id); | |
165 | struct nexthop *nexthop_from_ipv4(const struct in_addr *ipv4, | |
166 | const struct in_addr *src, | |
167 | vrf_id_t vrf_id); | |
168 | struct nexthop *nexthop_from_ipv4_ifindex(const struct in_addr *ipv4, | |
169 | const struct in_addr *src, | |
170 | ifindex_t ifindex, vrf_id_t vrf_id); | |
171 | struct nexthop *nexthop_from_ipv6(const struct in6_addr *ipv6, | |
172 | vrf_id_t vrf_id); | |
173 | struct nexthop *nexthop_from_ipv6_ifindex(const struct in6_addr *ipv6, | |
174 | ifindex_t ifindex, vrf_id_t vrf_id); | |
175 | struct nexthop *nexthop_from_blackhole(enum blackhole_type bh_type); | |
176 | ||
177 | /* | |
178 | * Hash a nexthop. Suitable for use with hash tables. | |
179 | * | |
180 | * This function uses the following values when computing the hash: | |
181 | * - vrf_id | |
182 | * - ifindex | |
183 | * - type | |
184 | * - gate | |
185 | * | |
186 | * nexthop | |
187 | * The nexthop to hash | |
188 | * | |
189 | * Returns: | |
190 | * 32-bit hash of nexthop | |
191 | */ | |
192 | uint32_t nexthop_hash(const struct nexthop *nexthop); | |
193 | /* | |
194 | * Hash a nexthop only on word-sized attributes: | |
195 | * - vrf_id | |
196 | * - ifindex | |
197 | * - type | |
198 | * - (some) flags | |
199 | */ | |
200 | uint32_t nexthop_hash_quick(const struct nexthop *nexthop); | |
201 | ||
202 | extern bool nexthop_same(const struct nexthop *nh1, const struct nexthop *nh2); | |
203 | extern bool nexthop_same_no_labels(const struct nexthop *nh1, | |
204 | const struct nexthop *nh2); | |
205 | extern int nexthop_cmp(const struct nexthop *nh1, const struct nexthop *nh2); | |
206 | extern int nexthop_g_addr_cmp(enum nexthop_types_t type, | |
207 | const union g_addr *addr1, | |
208 | const union g_addr *addr2); | |
209 | ||
210 | extern const char *nexthop_type_to_str(enum nexthop_types_t nh_type); | |
211 | extern bool nexthop_labels_match(const struct nexthop *nh1, | |
212 | const struct nexthop *nh2); | |
213 | ||
214 | extern const char *nexthop2str(const struct nexthop *nexthop, | |
215 | char *str, int size); | |
216 | extern struct nexthop *nexthop_next(const struct nexthop *nexthop); | |
217 | extern struct nexthop * | |
218 | nexthop_next_active_resolved(const struct nexthop *nexthop); | |
219 | extern unsigned int nexthop_level(const struct nexthop *nexthop); | |
220 | /* Copies to an already allocated nexthop struct */ | |
221 | extern void nexthop_copy(struct nexthop *copy, const struct nexthop *nexthop, | |
222 | struct nexthop *rparent); | |
223 | /* Copies to an already allocated nexthop struct, not including recurse info */ | |
224 | extern void nexthop_copy_no_recurse(struct nexthop *copy, | |
225 | const struct nexthop *nexthop, | |
226 | struct nexthop *rparent); | |
227 | /* Duplicates a nexthop and returns the newly allocated nexthop */ | |
228 | extern struct nexthop *nexthop_dup(const struct nexthop *nexthop, | |
229 | struct nexthop *rparent); | |
230 | /* Duplicates a nexthop and returns the newly allocated nexthop */ | |
231 | extern struct nexthop *nexthop_dup_no_recurse(const struct nexthop *nexthop, | |
232 | struct nexthop *rparent); | |
233 | ||
234 | /* | |
235 | * Parse one or more backup index values, as comma-separated numbers, | |
236 | * into caller's array of uint8_ts. The array must be NEXTHOP_MAX_BACKUPS | |
237 | * in size. Mails back the number of values converted, and returns 0 on | |
238 | * success, <0 if an error in parsing. | |
239 | */ | |
240 | int nexthop_str2backups(const char *str, int *num_backups, | |
241 | uint8_t *backups); | |
242 | ||
243 | #ifdef _FRR_ATTRIBUTE_PRINTFRR | |
244 | #pragma FRR printfrr_ext "%pNH" (struct nexthop *) | |
245 | #endif | |
246 | ||
247 | #ifdef __cplusplus | |
248 | } | |
249 | #endif | |
250 | ||
251 | #endif /*_LIB_NEXTHOP_H */ |