summaryrefslogtreecommitdiff
path: root/lib/nexthop_group.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/nexthop_group.c')
-rw-r--r--lib/nexthop_group.c138
1 files changed, 99 insertions, 39 deletions
diff --git a/lib/nexthop_group.c b/lib/nexthop_group.c
index b810d13a5a..0051cba625 100644
--- a/lib/nexthop_group.c
+++ b/lib/nexthop_group.c
@@ -42,6 +42,7 @@ struct nexthop_hold {
union sockunion *addr;
char *intf;
char *labels;
+ uint32_t weight;
};
struct nexthop_group_hooks {
@@ -243,25 +244,16 @@ void _nexthop_add(struct nexthop **target, struct nexthop *nexthop)
nexthop->prev = last;
}
-void nexthop_group_add_sorted(struct nexthop_group *nhg,
- struct nexthop *nexthop)
+/* Add nexthop to sorted list of nexthops */
+static void _nexthop_add_sorted(struct nexthop **head,
+ struct nexthop *nexthop)
{
- struct nexthop *position, *prev, *tail;
+ struct nexthop *position, *prev;
- /* Try to just append to the end first
- * This trust it is already sorted
- */
+ /* Ensure this gets set */
+ nexthop->next = NULL;
- tail = nexthop_group_tail(nhg);
-
- if (tail && (nexthop_cmp(tail, nexthop) < 0)) {
- tail->next = nexthop;
- nexthop->prev = tail;
-
- return;
- }
-
- for (position = nhg->nexthop, prev = NULL; position;
+ for (position = *head, prev = NULL; position;
prev = position, position = position->next) {
if (nexthop_cmp(position, nexthop) > 0) {
nexthop->next = position;
@@ -270,7 +262,7 @@ void nexthop_group_add_sorted(struct nexthop_group *nhg,
if (nexthop->prev)
nexthop->prev->next = nexthop;
else
- nhg->nexthop = nexthop;
+ *head = nexthop;
position->prev = nexthop;
return;
@@ -281,7 +273,27 @@ void nexthop_group_add_sorted(struct nexthop_group *nhg,
if (prev)
prev->next = nexthop;
else
- nhg->nexthop = nexthop;
+ *head = nexthop;
+}
+
+void nexthop_group_add_sorted(struct nexthop_group *nhg,
+ struct nexthop *nexthop)
+{
+ struct nexthop *tail;
+
+ /* Try to just append to the end first;
+ * trust the list is already sorted
+ */
+ tail = nexthop_group_tail(nhg);
+
+ if (tail && (nexthop_cmp(tail, nexthop) < 0)) {
+ tail->next = nexthop;
+ nexthop->prev = tail;
+
+ return;
+ }
+
+ _nexthop_add_sorted(&nhg->nexthop, nexthop);
}
/* Delete nexthop from a nexthop list. */
@@ -308,6 +320,40 @@ void _nexthop_del(struct nexthop_group *nhg, struct nexthop *nh)
nh->next = NULL;
}
+/*
+ * Copy a list of nexthops in 'nh' to an nhg, enforcing canonical sort order
+ */
+void nexthop_group_copy_nh_sorted(struct nexthop_group *nhg,
+ const struct nexthop *nh)
+{
+ struct nexthop *nexthop, *tail;
+ const struct nexthop *nh1;
+
+ /* We'll try to append to the end of the new list;
+ * if the original list in nh is already sorted, this eliminates
+ * lots of comparison operations.
+ */
+ tail = nexthop_group_tail(nhg);
+
+ for (nh1 = nh; nh1; nh1 = nh1->next) {
+ nexthop = nexthop_dup(nh1, NULL);
+
+ if (tail && (nexthop_cmp(tail, nexthop) < 0)) {
+ tail->next = nexthop;
+ nexthop->prev = tail;
+
+ tail = nexthop;
+ continue;
+ }
+
+ _nexthop_add_sorted(&nhg->nexthop, nexthop);
+
+ if (tail == NULL)
+ tail = nexthop;
+ }
+}
+
+/* Copy a list of nexthops, no effort made to sort or order them. */
void copy_nexthops(struct nexthop **tnh, const struct nexthop *nh,
struct nexthop *rparent)
{
@@ -526,8 +572,8 @@ DEFUN_NOSH(no_nexthop_group, no_nexthop_group_cmd, "no nexthop-group NHGNAME",
static void nexthop_group_save_nhop(struct nexthop_group_cmd *nhgc,
const char *nhvrf_name,
const union sockunion *addr,
- const char *intf,
- const char *labels)
+ const char *intf, const char *labels,
+ const uint32_t weight)
{
struct nexthop_hold *nh;
@@ -542,23 +588,26 @@ static void nexthop_group_save_nhop(struct nexthop_group_cmd *nhgc,
if (labels)
nh->labels = XSTRDUP(MTYPE_TMP, labels);
+ nh->weight = weight;
+
listnode_add_sort(nhgc->nhg_list, nh);
}
static void nexthop_group_unsave_nhop(struct nexthop_group_cmd *nhgc,
const char *nhvrf_name,
const union sockunion *addr,
- const char *intf,
- const char *labels)
+ const char *intf, const char *labels,
+ const uint32_t weight)
{
struct nexthop_hold *nh;
struct listnode *node;
for (ALL_LIST_ELEMENTS_RO(nhgc->nhg_list, node, nh)) {
- if (nhgc_cmp_helper(nhvrf_name, nh->nhvrf_name) == 0 &&
- nhgc_addr_cmp_helper(addr, nh->addr) == 0 &&
- nhgc_cmp_helper(intf, nh->intf) == 0 &&
- nhgc_cmp_helper(labels, nh->labels) == 0)
+ if (nhgc_cmp_helper(nhvrf_name, nh->nhvrf_name) == 0
+ && nhgc_addr_cmp_helper(addr, nh->addr) == 0
+ && nhgc_cmp_helper(intf, nh->intf) == 0
+ && nhgc_cmp_helper(labels, nh->labels) == 0
+ && weight == nh->weight)
break;
}
@@ -581,8 +630,8 @@ static void nexthop_group_unsave_nhop(struct nexthop_group_cmd *nhgc,
static bool nexthop_group_parse_nexthop(struct nexthop *nhop,
const union sockunion *addr,
const char *intf, const char *name,
- const char *labels,
- int *lbl_ret)
+ const char *labels, int *lbl_ret,
+ uint32_t weight)
{
int ret = 0;
struct vrf *vrf;
@@ -639,6 +688,8 @@ static bool nexthop_group_parse_nexthop(struct nexthop *nhop,
num, larray);
}
+ nhop->weight = weight;
+
return true;
}
@@ -648,11 +699,9 @@ static bool nexthop_group_parse_nexthop(struct nexthop *nhop,
static bool nexthop_group_parse_nhh(struct nexthop *nhop,
const struct nexthop_hold *nhh)
{
- return (nexthop_group_parse_nexthop(nhop, nhh->addr,
- nhh->intf,
- nhh->nhvrf_name,
- nhh->labels,
- NULL));
+ return (nexthop_group_parse_nexthop(nhop, nhh->addr, nhh->intf,
+ nhh->nhvrf_name, nhh->labels, NULL,
+ nhh->weight));
}
DEFPY(ecmp_nexthops, ecmp_nexthops_cmd,
@@ -664,6 +713,7 @@ DEFPY(ecmp_nexthops, ecmp_nexthops_cmd,
[{ \
nexthop-vrf NAME$vrf_name \
|label WORD \
+ |weight (1-255) \
}]",
NO_STR
"Specify one of the nexthops in this ECMP group\n"
@@ -674,7 +724,9 @@ DEFPY(ecmp_nexthops, ecmp_nexthops_cmd,
"If the nexthop is in a different vrf tell us\n"
"The nexthop-vrf Name\n"
"Specify label(s) for this nexthop\n"
- "One or more labels in the range (16-1048575) separated by '/'\n")
+ "One or more labels in the range (16-1048575) separated by '/'\n"
+ "Weight to be used by the nexthop for purposes of ECMP\n"
+ "Weight value to be used\n")
{
VTY_DECLVAR_CONTEXT(nexthop_group_cmd, nhgc);
struct nexthop nhop;
@@ -682,8 +734,8 @@ DEFPY(ecmp_nexthops, ecmp_nexthops_cmd,
int lbl_ret = 0;
bool legal;
- legal = nexthop_group_parse_nexthop(&nhop, addr, intf, vrf_name,
- label, &lbl_ret);
+ legal = nexthop_group_parse_nexthop(&nhop, addr, intf, vrf_name, label,
+ &lbl_ret, weight);
if (nhop.type == NEXTHOP_TYPE_IPV6
&& IN6_IS_ADDR_LINKLOCAL(&nhop.gate.ipv6)) {
@@ -716,7 +768,8 @@ DEFPY(ecmp_nexthops, ecmp_nexthops_cmd,
nh = nexthop_exists(&nhgc->nhg, &nhop);
if (no) {
- nexthop_group_unsave_nhop(nhgc, vrf_name, addr, intf, label);
+ nexthop_group_unsave_nhop(nhgc, vrf_name, addr, intf, label,
+ weight);
if (nh) {
_nexthop_del(&nhgc->nhg, nh);
@@ -734,7 +787,8 @@ DEFPY(ecmp_nexthops, ecmp_nexthops_cmd,
_nexthop_add(&nhgc->nhg.nexthop, nh);
}
- nexthop_group_save_nhop(nhgc, vrf_name, addr, intf, label);
+ nexthop_group_save_nhop(nhgc, vrf_name, addr, intf, label,
+ weight);
if (legal && nhg_hooks.add_nexthop)
nhg_hooks.add_nexthop(nhgc, nh);
@@ -743,7 +797,7 @@ DEFPY(ecmp_nexthops, ecmp_nexthops_cmd,
return CMD_SUCCESS;
}
-struct cmd_node nexthop_group_node = {
+static struct cmd_node nexthop_group_node = {
NH_GROUP_NODE,
"%s(config-nh-group)# ",
1
@@ -794,6 +848,9 @@ void nexthop_group_write_nexthop(struct vty *vty, struct nexthop *nh)
vty_out(vty, " label %s", buf);
}
+ if (nh->weight)
+ vty_out(vty, " weight %u", nh->weight);
+
vty_out(vty, "\n");
}
@@ -816,6 +873,9 @@ static void nexthop_group_write_nexthop_internal(struct vty *vty,
if (nh->labels)
vty_out(vty, " label %s", nh->labels);
+ if (nh->weight)
+ vty_out(vty, " weight %u", nh->weight);
+
vty_out(vty, "\n");
}