]> git.proxmox.com Git - mirror_frr.git/blame - lib/nexthop.h
lib: add possibility to search non-recursively for NB node entries
[mirror_frr.git] / lib / nexthop.h
CommitLineData
fb018d25
DS
1/*
2 * Nexthop structure definition.
a399694f 3 * Copyright (C) 1997, 98, 99, 2001 Kunihiro Ishiguro
fb018d25
DS
4 * Copyright (C) 2013 Cumulus Networks, Inc.
5 *
a399694f 6 * This file is part of Quagga.
fb018d25 7 *
a399694f 8 * Quagga is free software; you can redistribute it and/or modify it
fb018d25
DS
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 *
a399694f 13 * Quagga is distributed in the hope that it will be useful, but
fb018d25
DS
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 *
896014f4
DL
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
fb018d25
DS
21 */
22
23#ifndef _LIB_NEXTHOP_H
24#define _LIB_NEXTHOP_H
25
26#include "prefix.h"
54d48ea1 27#include "mpls.h"
f2a0ba3a 28#include "vxlan.h"
fb018d25 29
5e244469
RW
30#ifdef __cplusplus
31extern "C" {
32#endif
33
80c2442a 34/* Maximum next hop string length - gateway + ifindex */
35#define NEXTHOP_STRLEN (INET6_ADDRSTRLEN + 30)
36
fb018d25 37union g_addr {
d62a17ae 38 struct in_addr ipv4;
39 struct in6_addr ipv6;
fb018d25
DS
40};
41
d62a17ae 42enum 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. */
fb018d25 49};
09a484dd
DL
50
51enum blackhole_type {
52 BLACKHOLE_UNSPEC = 0,
53 BLACKHOLE_NULL,
54 BLACKHOLE_REJECT,
55 BLACKHOLE_ADMINPROHIB,
56};
fb018d25 57
25b9cb0c 58/* IPV[46] -> IPV[46]_IFINDEX */
996c9314
LB
59#define NEXTHOP_FIRSTHOPTYPE(type) \
60 ((type) == NEXTHOP_TYPE_IFINDEX || (type) == NEXTHOP_TYPE_BLACKHOLE) \
61 ? (type) \
62 : ((type) | 1)
25b9cb0c 63
f2a0ba3a
RZ
64enum nh_encap_type {
65 NET_VXLAN = 100, /* value copied from FPM_NH_ENCAP_VXLAN. */
66};
67
474aebd9
MS
68/* Fixed limit on the number of backup nexthops per primary nexthop */
69#define NEXTHOP_MAX_BACKUPS 8
70
71/* Backup index value is limited */
72#define NEXTHOP_BACKUP_IDX_MAX 255
73
fb018d25 74/* Nexthop structure. */
d62a17ae 75struct nexthop {
76 struct nexthop *next;
77 struct nexthop *prev;
fb018d25 78
4a7371e9
DS
79 /*
80 * What vrf is this nexthop associated with?
81 */
82 vrf_id_t vrf_id;
83
d62a17ae 84 /* Interface index. */
85 ifindex_t ifindex;
fb018d25 86
d62a17ae 87 enum nexthop_types_t type;
fb018d25 88
d7c0a89a 89 uint8_t flags;
fb018d25
DS
90#define NEXTHOP_FLAG_ACTIVE (1 << 0) /* This nexthop is alive. */
91#define NEXTHOP_FLAG_FIB (1 << 1) /* FIB nexthop. */
92#define NEXTHOP_FLAG_RECURSIVE (1 << 2) /* Recursive nexthop. */
0641a955
MS
93#define NEXTHOP_FLAG_ONLINK (1 << 3) /* Nexthop should be installed
94 * onlink.
95 */
96#define NEXTHOP_FLAG_DUPLICATE (1 << 4) /* nexthop duplicates another
97 * active one
98 */
99#define NEXTHOP_FLAG_RNH_FILTERED (1 << 5) /* rmap filtered, used by rnh */
defd2ea4
MS
100#define NEXTHOP_FLAG_HAS_BACKUP (1 << 6) /* Backup nexthop index is set */
101
996c9314
LB
102#define NEXTHOP_IS_ACTIVE(flags) \
103 (CHECK_FLAG(flags, NEXTHOP_FLAG_ACTIVE) \
104 && !CHECK_FLAG(flags, NEXTHOP_FLAG_DUPLICATE))
fb018d25 105
d62a17ae 106 /* Nexthop address */
a8309422
DL
107 union {
108 union g_addr gate;
109 enum blackhole_type bh_type;
110 };
d62a17ae 111 union g_addr src;
112 union g_addr rmap_src; /* Src is set via routemap */
113
114 /* Nexthops obtained by recursive resolution.
115 *
116 * If the nexthop struct needs to be resolved recursively,
117 * NEXTHOP_FLAG_RECURSIVE will be set in flags and the nexthops
118 * obtained by recursive resolution will be added to `resolved'.
119 */
120 struct nexthop *resolved;
121 /* Recursive parent */
122 struct nexthop *rparent;
123
124 /* Type of label(s), if any */
125 enum lsp_types_t nh_label_type;
126
127 /* Label(s) associated with this nexthop. */
8ecdb26e 128 struct mpls_label_stack *nh_label;
df7fb580
DS
129
130 /* Weight of the nexthop ( for unequal cost ECMP ) */
131 uint8_t weight;
defd2ea4 132
474aebd9 133 /* Count and index of corresponding backup nexthop(s) in a backup list;
defd2ea4
MS
134 * only meaningful if the HAS_BACKUP flag is set.
135 */
474aebd9
MS
136 uint8_t backup_num;
137 uint8_t backup_idx[NEXTHOP_MAX_BACKUPS];
f2a0ba3a
RZ
138
139 /* Encapsulation information. */
140 enum nh_encap_type nh_encap_type;
141 union {
142 vni_t vni;
143 } nh_encap;
fb018d25
DS
144};
145
e4a1ec74
MS
146/* Utility to append one nexthop to another. */
147#define NEXTHOP_APPEND(to, new) \
148 do { \
149 (to)->next = (new); \
150 (new)->prev = (to); \
151 (new)->next = NULL; \
152 } while (0)
153
d62a17ae 154struct nexthop *nexthop_new(void);
a399694f 155
d62a17ae 156void nexthop_free(struct nexthop *nexthop);
157void nexthops_free(struct nexthop *nexthop);
a399694f 158
e4a1ec74
MS
159void nexthop_add_labels(struct nexthop *nexthop, enum lsp_types_t ltype,
160 uint8_t num_labels, const mpls_label_t *labels);
d62a17ae 161void nexthop_del_labels(struct nexthop *);
40c7bdb0 162
f3323df2
MS
163/*
164 * Allocate a new nexthop object and initialize it from various args.
165 */
166struct nexthop *nexthop_from_ifindex(ifindex_t ifindex, vrf_id_t vrf_id);
167struct nexthop *nexthop_from_ipv4(const struct in_addr *ipv4,
168 const struct in_addr *src,
169 vrf_id_t vrf_id);
170struct nexthop *nexthop_from_ipv4_ifindex(const struct in_addr *ipv4,
171 const struct in_addr *src,
172 ifindex_t ifindex, vrf_id_t vrf_id);
173struct nexthop *nexthop_from_ipv6(const struct in6_addr *ipv6,
174 vrf_id_t vrf_id);
175struct nexthop *nexthop_from_ipv6_ifindex(const struct in6_addr *ipv6,
176 ifindex_t ifindex, vrf_id_t vrf_id);
177struct nexthop *nexthop_from_blackhole(enum blackhole_type bh_type);
178
d36d0d57
QY
179/*
180 * Hash a nexthop. Suitable for use with hash tables.
181 *
182 * This function uses the following values when computing the hash:
183 * - vrf_id
184 * - ifindex
185 * - type
186 * - gate
187 *
188 * nexthop
189 * The nexthop to hash
190 *
191 * Returns:
192 * 32-bit hash of nexthop
193 */
1b1fe1c4 194uint32_t nexthop_hash(const struct nexthop *nexthop);
73a38187
SW
195/*
196 * Hash a nexthop only on word-sized attributes:
197 * - vrf_id
198 * - ifindex
199 * - type
200 * - (some) flags
201 */
202uint32_t nexthop_hash_quick(const struct nexthop *nexthop);
d36d0d57 203
31919191 204extern bool nexthop_same(const struct nexthop *nh1, const struct nexthop *nh2);
a5a2d802
SW
205extern bool nexthop_same_no_labels(const struct nexthop *nh1,
206 const struct nexthop *nh2);
776c3e90 207extern int nexthop_cmp(const struct nexthop *nh1, const struct nexthop *nh2);
3c6e0bd4
SW
208extern int nexthop_g_addr_cmp(enum nexthop_types_t type,
209 const union g_addr *addr1,
210 const union g_addr *addr2);
31919191 211
d62a17ae 212extern const char *nexthop_type_to_str(enum nexthop_types_t nh_type);
89dc3160
SW
213extern bool nexthop_labels_match(const struct nexthop *nh1,
214 const struct nexthop *nh2);
0e8542ee
MS
215extern bool nexthop_same_firsthop(const struct nexthop *next1,
216 const struct nexthop *next2);
fb018d25 217
55f93d4b
MS
218extern const char *nexthop2str(const struct nexthop *nexthop,
219 char *str, int size);
17c25e03
SW
220extern struct nexthop *nexthop_next(const struct nexthop *nexthop);
221extern struct nexthop *
222nexthop_next_active_resolved(const struct nexthop *nexthop);
850c85b9 223extern unsigned int nexthop_level(const struct nexthop *nexthop);
504d0a40 224/* Copies to an already allocated nexthop struct */
e7addf02
SW
225extern void nexthop_copy(struct nexthop *copy, const struct nexthop *nexthop,
226 struct nexthop *rparent);
77bf9504
SW
227/* Copies to an already allocated nexthop struct, not including recurse info */
228extern void nexthop_copy_no_recurse(struct nexthop *copy,
229 const struct nexthop *nexthop,
230 struct nexthop *rparent);
504d0a40
SW
231/* Duplicates a nexthop and returns the newly allocated nexthop */
232extern struct nexthop *nexthop_dup(const struct nexthop *nexthop,
233 struct nexthop *rparent);
77bf9504
SW
234/* Duplicates a nexthop and returns the newly allocated nexthop */
235extern struct nexthop *nexthop_dup_no_recurse(const struct nexthop *nexthop,
236 struct nexthop *rparent);
5e244469 237
0cac0cf4
MS
238/*
239 * Parse one or more backup index values, as comma-separated numbers,
240 * into caller's array of uint8_ts. The array must be NEXTHOP_MAX_BACKUPS
241 * in size. Mails back the number of values converted, and returns 0 on
242 * success, <0 if an error in parsing.
243 */
244int nexthop_str2backups(const char *str, int *num_backups,
245 uint8_t *backups);
246
07ef3e34
DL
247#ifdef _FRR_ATTRIBUTE_PRINTFRR
248#pragma FRR printfrr_ext "%pNH" (struct nexthop *)
249#endif
250
5e244469
RW
251#ifdef __cplusplus
252}
253#endif
254
fb018d25 255#endif /*_LIB_NEXTHOP_H */