2 * Nexthop Group structure definition.
3 * Copyright (C) 2018 Cumulus Networks, Inc.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #ifndef __NEXTHOP_GROUP__
22 #define __NEXTHOP_GROUP__
31 struct nhg_resilience
{
34 uint32_t unbalanced_timer
;
35 uint64_t unbalanced_time
;
39 * What is a nexthop group?
41 * A nexthop group is a collection of nexthops that make up
42 * the ECMP path for the route.
44 * This module provides a proper abstraction to this idea.
46 struct nexthop_group
{
47 struct nexthop
*nexthop
;
49 struct nhg_resilience nhgr
;
52 struct nexthop_group
*nexthop_group_new(void);
53 void nexthop_group_delete(struct nexthop_group
**nhg
);
55 void nexthop_group_copy(struct nexthop_group
*to
,
56 const struct nexthop_group
*from
);
59 * Copy a list of nexthops in 'nh' to an nhg, enforcing canonical sort order
61 void nexthop_group_copy_nh_sorted(struct nexthop_group
*nhg
,
62 const struct nexthop
*nh
);
64 void copy_nexthops(struct nexthop
**tnh
, const struct nexthop
*nh
,
65 struct nexthop
*rparent
);
67 uint32_t nexthop_group_hash_no_recurse(const struct nexthop_group
*nhg
);
68 uint32_t nexthop_group_hash(const struct nexthop_group
*nhg
);
69 void nexthop_group_mark_duplicates(struct nexthop_group
*nhg
);
71 /* Add a nexthop to a list, enforcing the canonical sort order. */
72 void nexthop_group_add_sorted(struct nexthop_group
*nhg
,
73 struct nexthop
*nexthop
);
75 /* The following for loop allows to iterate over the nexthop
76 * structure of routes.
78 * head: The pointer to the first nexthop in the chain.
80 * nexthop: The pointer to the current nexthop, either in the
81 * top-level chain or in a resolved chain.
83 #define ALL_NEXTHOPS(head, nhop) \
84 (nhop) = (head.nexthop); \
86 (nhop) = nexthop_next(nhop)
88 #define ALL_NEXTHOPS_PTR(head, nhop) \
89 (nhop) = ((head)->nexthop); \
91 (nhop) = nexthop_next(nhop)
94 #define NHGC_NAME_SIZE 80
96 struct nexthop_group_cmd
{
98 RB_ENTRY(nexthop_group_cmd
) nhgc_entry
;
100 char name
[NHGC_NAME_SIZE
];
102 /* Name of group containing backup nexthops (if set) */
103 char backup_list_name
[NHGC_NAME_SIZE
];
105 struct nexthop_group nhg
;
107 struct list
*nhg_list
;
111 RB_HEAD(nhgc_entry_head
, nexthp_group_cmd
);
112 RB_PROTOTYPE(nhgc_entry_head
, nexthop_group_cmd
, nhgc_entry
,
113 nexthop_group_cmd_compare
)
114 DECLARE_QOBJ_TYPE(nexthop_group_cmd
);
117 * Initialize nexthop_groups. If you are interested in when
118 * a nexthop_group is added/deleted/modified, then set the
119 * appropriate callback functions to handle it in your
122 * create - The creation of the nexthop group
123 * modify - Modification of the nexthop group when not changing a nexthop
124 * ( resilience as an example )
125 * add_nexthop - A nexthop is added to the NHG
126 * del_nexthop - A nexthop is deleted from the NHG
127 * destroy - The NHG is deleted
129 void nexthop_group_init(
130 void (*create
)(const char *name
),
131 void (*modify
)(const struct nexthop_group_cmd
*nhgc
),
132 void (*add_nexthop
)(const struct nexthop_group_cmd
*nhgc
,
133 const struct nexthop
*nhop
),
134 void (*del_nexthop
)(const struct nexthop_group_cmd
*nhgc
,
135 const struct nexthop
*nhop
),
136 void (*destroy
)(const char *name
));
138 void nexthop_group_enable_vrf(struct vrf
*vrf
);
139 void nexthop_group_disable_vrf(struct vrf
*vrf
);
140 void nexthop_group_interface_state_change(struct interface
*ifp
,
141 ifindex_t oldifindex
);
143 extern struct nexthop
*nexthop_exists(const struct nexthop_group
*nhg
,
144 const struct nexthop
*nh
);
145 /* This assumes ordered */
146 extern bool nexthop_group_equal_no_recurse(const struct nexthop_group
*nhg1
,
147 const struct nexthop_group
*nhg2
);
149 /* This assumes ordered */
150 extern bool nexthop_group_equal(const struct nexthop_group
*nhg1
,
151 const struct nexthop_group
*nhg2
);
153 extern struct nexthop_group_cmd
*nhgc_find(const char *name
);
155 extern void nexthop_group_write_nexthop_simple(struct vty
*vty
,
156 const struct nexthop
*nh
,
158 extern void nexthop_group_write_nexthop(struct vty
*vty
,
159 const struct nexthop
*nh
);
161 extern void nexthop_group_json_nexthop(json_object
*j
,
162 const struct nexthop
*nh
);
164 /* Return the number of nexthops in this nhg */
165 extern uint8_t nexthop_group_nexthop_num(const struct nexthop_group
*nhg
);
167 nexthop_group_nexthop_num_no_recurse(const struct nexthop_group
*nhg
);
169 nexthop_group_active_nexthop_num(const struct nexthop_group
*nhg
);
171 nexthop_group_active_nexthop_num_no_recurse(const struct nexthop_group
*nhg
);