1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Nexthop Group structure definition.
4 * Copyright (C) 2018 Cumulus Networks, Inc.
8 #ifndef __NEXTHOP_GROUP__
9 #define __NEXTHOP_GROUP__
18 struct nhg_resilience
{
21 uint32_t unbalanced_timer
;
22 uint64_t unbalanced_time
;
26 * What is a nexthop group?
28 * A nexthop group is a collection of nexthops that make up
29 * the ECMP path for the route.
31 * This module provides a proper abstraction to this idea.
33 struct nexthop_group
{
34 struct nexthop
*nexthop
;
36 struct nhg_resilience nhgr
;
39 struct nexthop_group
*nexthop_group_new(void);
40 void nexthop_group_delete(struct nexthop_group
**nhg
);
42 void nexthop_group_copy(struct nexthop_group
*to
,
43 const struct nexthop_group
*from
);
46 * Copy a list of nexthops in 'nh' to an nhg, enforcing canonical sort order
48 void nexthop_group_copy_nh_sorted(struct nexthop_group
*nhg
,
49 const struct nexthop
*nh
);
51 void copy_nexthops(struct nexthop
**tnh
, const struct nexthop
*nh
,
52 struct nexthop
*rparent
);
54 uint32_t nexthop_group_hash_no_recurse(const struct nexthop_group
*nhg
);
55 uint32_t nexthop_group_hash(const struct nexthop_group
*nhg
);
56 void nexthop_group_mark_duplicates(struct nexthop_group
*nhg
);
58 /* Add a nexthop to a list, enforcing the canonical sort order. */
59 void nexthop_group_add_sorted(struct nexthop_group
*nhg
,
60 struct nexthop
*nexthop
);
62 /* The following for loop allows to iterate over the nexthop
63 * structure of routes.
65 * head: The pointer to the first nexthop in the chain.
67 * nexthop: The pointer to the current nexthop, either in the
68 * top-level chain or in a resolved chain.
70 #define ALL_NEXTHOPS(head, nhop) \
71 (nhop) = (head.nexthop); \
73 (nhop) = nexthop_next(nhop)
75 #define ALL_NEXTHOPS_PTR(head, nhop) \
76 (nhop) = ((head)->nexthop); \
78 (nhop) = nexthop_next(nhop)
81 #define NHGC_NAME_SIZE 80
83 struct nexthop_group_cmd
{
85 RB_ENTRY(nexthop_group_cmd
) nhgc_entry
;
87 char name
[NHGC_NAME_SIZE
];
89 /* Name of group containing backup nexthops (if set) */
90 char backup_list_name
[NHGC_NAME_SIZE
];
92 struct nexthop_group nhg
;
94 struct list
*nhg_list
;
98 RB_HEAD(nhgc_entry_head
, nexthp_group_cmd
);
99 RB_PROTOTYPE(nhgc_entry_head
, nexthop_group_cmd
, nhgc_entry
,
100 nexthop_group_cmd_compare
)
101 DECLARE_QOBJ_TYPE(nexthop_group_cmd
);
104 * Initialize nexthop_groups. If you are interested in when
105 * a nexthop_group is added/deleted/modified, then set the
106 * appropriate callback functions to handle it in your
109 * create - The creation of the nexthop group
110 * modify - Modification of the nexthop group when not changing a nexthop
111 * ( resilience as an example )
112 * add_nexthop - A nexthop is added to the NHG
113 * del_nexthop - A nexthop is deleted from the NHG
114 * destroy - The NHG is deleted
116 void nexthop_group_init(
117 void (*create
)(const char *name
),
118 void (*modify
)(const struct nexthop_group_cmd
*nhgc
),
119 void (*add_nexthop
)(const struct nexthop_group_cmd
*nhgc
,
120 const struct nexthop
*nhop
),
121 void (*del_nexthop
)(const struct nexthop_group_cmd
*nhgc
,
122 const struct nexthop
*nhop
),
123 void (*destroy
)(const char *name
));
125 void nexthop_group_enable_vrf(struct vrf
*vrf
);
126 void nexthop_group_disable_vrf(struct vrf
*vrf
);
127 void nexthop_group_interface_state_change(struct interface
*ifp
,
128 ifindex_t oldifindex
);
130 extern struct nexthop
*nexthop_exists(const struct nexthop_group
*nhg
,
131 const struct nexthop
*nh
);
132 /* This assumes ordered */
133 extern bool nexthop_group_equal_no_recurse(const struct nexthop_group
*nhg1
,
134 const struct nexthop_group
*nhg2
);
136 /* This assumes ordered */
137 extern bool nexthop_group_equal(const struct nexthop_group
*nhg1
,
138 const struct nexthop_group
*nhg2
);
140 extern struct nexthop_group_cmd
*nhgc_find(const char *name
);
142 extern void nexthop_group_write_nexthop_simple(struct vty
*vty
,
143 const struct nexthop
*nh
,
145 extern void nexthop_group_write_nexthop(struct vty
*vty
,
146 const struct nexthop
*nh
);
148 extern void nexthop_group_json_nexthop(json_object
*j
,
149 const struct nexthop
*nh
);
151 /* Return the number of nexthops in this nhg */
152 extern uint8_t nexthop_group_nexthop_num(const struct nexthop_group
*nhg
);
154 nexthop_group_nexthop_num_no_recurse(const struct nexthop_group
*nhg
);
156 nexthop_group_active_nexthop_num(const struct nexthop_group
*nhg
);
158 nexthop_group_active_nexthop_num_no_recurse(const struct nexthop_group
*nhg
);
160 extern bool nexthop_group_has_label(const struct nexthop_group
*nhg
);