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
23 #include <sockunion.h>
25 #include <nexthop_group.h>
26 #include <nexthop_group_private.h>
31 #ifndef VTYSH_EXTRACT_PL
32 #include "lib/nexthop_group_clippy.c"
35 DEFINE_MTYPE_STATIC(LIB
, NEXTHOP_GROUP
, "Nexthop Group")
38 * Internal struct used to hold nhg config strings
42 union sockunion
*addr
;
48 struct nexthop_group_hooks
{
49 void (*new)(const char *name
);
50 void (*add_nexthop
)(const struct nexthop_group_cmd
*nhg
,
51 const struct nexthop
*nhop
);
52 void (*del_nexthop
)(const struct nexthop_group_cmd
*nhg
,
53 const struct nexthop
*nhop
);
54 void (*delete)(const char *name
);
57 static struct nexthop_group_hooks nhg_hooks
;
60 nexthop_group_cmd_compare(const struct nexthop_group_cmd
*nhgc1
,
61 const struct nexthop_group_cmd
*nhgc2
);
62 RB_GENERATE(nhgc_entry_head
, nexthop_group_cmd
, nhgc_entry
,
63 nexthop_group_cmd_compare
)
65 static struct nhgc_entry_head nhgc_entries
;
68 nexthop_group_cmd_compare(const struct nexthop_group_cmd
*nhgc1
,
69 const struct nexthop_group_cmd
*nhgc2
)
71 return strcmp(nhgc1
->name
, nhgc2
->name
);
74 static struct nexthop
*nexthop_group_tail(const struct nexthop_group
*nhg
)
76 struct nexthop
*nexthop
= nhg
->nexthop
;
78 while (nexthop
&& nexthop
->next
)
79 nexthop
= nexthop
->next
;
84 uint8_t nexthop_group_nexthop_num(const struct nexthop_group
*nhg
)
89 for (ALL_NEXTHOPS_PTR(nhg
, nhop
))
95 uint8_t nexthop_group_nexthop_num_no_recurse(const struct nexthop_group
*nhg
)
100 for (nhop
= nhg
->nexthop
; nhop
; nhop
= nhop
->next
)
106 uint8_t nexthop_group_active_nexthop_num(const struct nexthop_group
*nhg
)
108 struct nexthop
*nhop
;
111 for (ALL_NEXTHOPS_PTR(nhg
, nhop
)) {
112 if (CHECK_FLAG(nhop
->flags
, NEXTHOP_FLAG_ACTIVE
))
120 nexthop_group_active_nexthop_num_no_recurse(const struct nexthop_group
*nhg
)
122 struct nexthop
*nhop
;
125 for (nhop
= nhg
->nexthop
; nhop
; nhop
= nhop
->next
) {
126 if (CHECK_FLAG(nhop
->flags
, NEXTHOP_FLAG_ACTIVE
))
133 struct nexthop
*nexthop_exists(const struct nexthop_group
*nhg
,
134 const struct nexthop
*nh
)
136 struct nexthop
*nexthop
;
138 for (nexthop
= nhg
->nexthop
; nexthop
; nexthop
= nexthop
->next
) {
139 if (nexthop_same(nh
, nexthop
))
147 nexthop_group_equal_common(const struct nexthop_group
*nhg1
,
148 const struct nexthop_group
*nhg2
,
149 uint8_t (*nexthop_group_nexthop_num_func
)(
150 const struct nexthop_group
*nhg
))
161 if (nexthop_group_nexthop_num_func(nhg1
)
162 != nexthop_group_nexthop_num_func(nhg2
))
168 /* This assumes ordered */
169 bool nexthop_group_equal_no_recurse(const struct nexthop_group
*nhg1
,
170 const struct nexthop_group
*nhg2
)
172 struct nexthop
*nh1
= NULL
;
173 struct nexthop
*nh2
= NULL
;
175 if (!nexthop_group_equal_common(nhg1
, nhg2
,
176 &nexthop_group_nexthop_num_no_recurse
))
179 for (nh1
= nhg1
->nexthop
, nh2
= nhg2
->nexthop
; nh1
|| nh2
;
180 nh1
= nh1
->next
, nh2
= nh2
->next
) {
185 if (!nexthop_same(nh1
, nh2
))
192 /* This assumes ordered */
193 bool nexthop_group_equal(const struct nexthop_group
*nhg1
,
194 const struct nexthop_group
*nhg2
)
196 struct nexthop
*nh1
= NULL
;
197 struct nexthop
*nh2
= NULL
;
199 if (!nexthop_group_equal_common(nhg1
, nhg2
, &nexthop_group_nexthop_num
))
202 for (nh1
= nhg1
->nexthop
, nh2
= nhg2
->nexthop
; nh1
|| nh2
;
203 nh1
= nexthop_next(nh1
), nh2
= nexthop_next(nh2
)) {
208 if (!nexthop_same(nh1
, nh2
))
214 struct nexthop_group
*nexthop_group_new(void)
216 return XCALLOC(MTYPE_NEXTHOP_GROUP
, sizeof(struct nexthop_group
));
219 void nexthop_group_copy(struct nexthop_group
*to
, struct nexthop_group
*from
)
221 /* Copy everything, including recursive info */
222 copy_nexthops(&to
->nexthop
, from
->nexthop
, NULL
);
225 void nexthop_group_delete(struct nexthop_group
**nhg
)
228 nexthops_free((*nhg
)->nexthop
);
230 XFREE(MTYPE_NEXTHOP_GROUP
, *nhg
);
233 /* Add nexthop to the end of a nexthop list. */
234 void _nexthop_add(struct nexthop
**target
, struct nexthop
*nexthop
)
236 struct nexthop
*last
;
238 for (last
= *target
; last
&& last
->next
; last
= last
->next
)
241 last
->next
= nexthop
;
244 nexthop
->prev
= last
;
247 /* Add nexthop to sorted list of nexthops */
248 static void _nexthop_add_sorted(struct nexthop
**head
,
249 struct nexthop
*nexthop
)
251 struct nexthop
*position
, *prev
;
253 assert(!nexthop
->next
);
255 for (position
= *head
, prev
= NULL
; position
;
256 prev
= position
, position
= position
->next
) {
257 if (nexthop_cmp(position
, nexthop
) > 0) {
258 nexthop
->next
= position
;
259 nexthop
->prev
= prev
;
262 nexthop
->prev
->next
= nexthop
;
266 position
->prev
= nexthop
;
271 nexthop
->prev
= prev
;
273 prev
->next
= nexthop
;
278 void nexthop_group_add_sorted(struct nexthop_group
*nhg
,
279 struct nexthop
*nexthop
)
281 struct nexthop
*tail
;
283 assert(!nexthop
->next
);
285 /* Try to just append to the end first;
286 * trust the list is already sorted
288 tail
= nexthop_group_tail(nhg
);
290 if (tail
&& (nexthop_cmp(tail
, nexthop
) < 0)) {
291 tail
->next
= nexthop
;
292 nexthop
->prev
= tail
;
297 _nexthop_add_sorted(&nhg
->nexthop
, nexthop
);
300 /* Delete nexthop from a nexthop list. */
301 void _nexthop_del(struct nexthop_group
*nhg
, struct nexthop
*nh
)
303 struct nexthop
*nexthop
;
305 for (nexthop
= nhg
->nexthop
; nexthop
; nexthop
= nexthop
->next
) {
306 if (nexthop_same(nh
, nexthop
))
313 nexthop
->prev
->next
= nexthop
->next
;
315 nhg
->nexthop
= nexthop
->next
;
318 nexthop
->next
->prev
= nexthop
->prev
;
325 * Copy a list of nexthops in 'nh' to an nhg, enforcing canonical sort order
327 void nexthop_group_copy_nh_sorted(struct nexthop_group
*nhg
,
328 const struct nexthop
*nh
)
330 struct nexthop
*nexthop
, *tail
;
331 const struct nexthop
*nh1
;
333 /* We'll try to append to the end of the new list;
334 * if the original list in nh is already sorted, this eliminates
335 * lots of comparison operations.
337 tail
= nexthop_group_tail(nhg
);
339 for (nh1
= nh
; nh1
; nh1
= nh1
->next
) {
340 nexthop
= nexthop_dup(nh1
, NULL
);
342 if (tail
&& (nexthop_cmp(tail
, nexthop
) < 0)) {
343 tail
->next
= nexthop
;
344 nexthop
->prev
= tail
;
350 _nexthop_add_sorted(&nhg
->nexthop
, nexthop
);
357 /* Copy a list of nexthops, no effort made to sort or order them. */
358 void copy_nexthops(struct nexthop
**tnh
, const struct nexthop
*nh
,
359 struct nexthop
*rparent
)
361 struct nexthop
*nexthop
;
362 const struct nexthop
*nh1
;
364 for (nh1
= nh
; nh1
; nh1
= nh1
->next
) {
365 nexthop
= nexthop_dup(nh1
, rparent
);
366 _nexthop_add(tnh
, nexthop
);
370 uint32_t nexthop_group_hash_no_recurse(const struct nexthop_group
*nhg
)
376 * We are not interested in hashing over any recursively
379 for (nh
= nhg
->nexthop
; nh
; nh
= nh
->next
)
380 key
= jhash_1word(nexthop_hash(nh
), key
);
385 uint32_t nexthop_group_hash(const struct nexthop_group
*nhg
)
390 for (ALL_NEXTHOPS_PTR(nhg
, nh
))
391 key
= jhash_1word(nexthop_hash(nh
), key
);
396 void nexthop_group_mark_duplicates(struct nexthop_group
*nhg
)
398 struct nexthop
*nexthop
, *prev
;
400 for (ALL_NEXTHOPS_PTR(nhg
, nexthop
)) {
401 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_DUPLICATE
);
402 for (ALL_NEXTHOPS_PTR(nhg
, prev
)) {
405 if (nexthop_same_firsthop(nexthop
, prev
)) {
406 SET_FLAG(nexthop
->flags
,
407 NEXTHOP_FLAG_DUPLICATE
);
414 static void nhgc_delete_nexthops(struct nexthop_group_cmd
*nhgc
)
416 struct nexthop
*nexthop
;
418 nexthop
= nhgc
->nhg
.nexthop
;
420 struct nexthop
*next
= nexthop_next(nexthop
);
422 _nexthop_del(&nhgc
->nhg
, nexthop
);
423 if (nhg_hooks
.del_nexthop
)
424 nhg_hooks
.del_nexthop(nhgc
, nexthop
);
426 nexthop_free(nexthop
);
432 struct nexthop_group_cmd
*nhgc_find(const char *name
)
434 struct nexthop_group_cmd find
;
436 strlcpy(find
.name
, name
, sizeof(find
.name
));
438 return RB_FIND(nhgc_entry_head
, &nhgc_entries
, &find
);
441 static int nhgc_cmp_helper(const char *a
, const char *b
)
455 static int nhgc_addr_cmp_helper(const union sockunion
*a
, const union sockunion
*b
)
466 return sockunion_cmp(a
, b
);
469 static int nhgl_cmp(struct nexthop_hold
*nh1
, struct nexthop_hold
*nh2
)
473 ret
= nhgc_addr_cmp_helper(nh1
->addr
, nh2
->addr
);
477 ret
= nhgc_cmp_helper(nh1
->intf
, nh2
->intf
);
481 ret
= nhgc_cmp_helper(nh1
->nhvrf_name
, nh2
->nhvrf_name
);
485 return nhgc_cmp_helper(nh1
->labels
, nh2
->labels
);
488 static void nhgl_delete(struct nexthop_hold
*nh
)
490 XFREE(MTYPE_TMP
, nh
->intf
);
492 XFREE(MTYPE_TMP
, nh
->nhvrf_name
);
495 sockunion_free(nh
->addr
);
497 XFREE(MTYPE_TMP
, nh
->labels
);
499 XFREE(MTYPE_TMP
, nh
);
502 static struct nexthop_group_cmd
*nhgc_get(const char *name
)
504 struct nexthop_group_cmd
*nhgc
;
506 nhgc
= nhgc_find(name
);
508 nhgc
= XCALLOC(MTYPE_TMP
, sizeof(*nhgc
));
509 strlcpy(nhgc
->name
, name
, sizeof(nhgc
->name
));
511 QOBJ_REG(nhgc
, nexthop_group_cmd
);
512 RB_INSERT(nhgc_entry_head
, &nhgc_entries
, nhgc
);
514 nhgc
->nhg_list
= list_new();
515 nhgc
->nhg_list
->cmp
= (int (*)(void *, void *))nhgl_cmp
;
516 nhgc
->nhg_list
->del
= (void (*)(void *))nhgl_delete
;
525 static void nhgc_delete(struct nexthop_group_cmd
*nhgc
)
527 nhgc_delete_nexthops(nhgc
);
529 if (nhg_hooks
.delete)
530 nhg_hooks
.delete(nhgc
->name
);
532 RB_REMOVE(nhgc_entry_head
, &nhgc_entries
, nhgc
);
534 list_delete(&nhgc
->nhg_list
);
536 XFREE(MTYPE_TMP
, nhgc
);
539 DEFINE_QOBJ_TYPE(nexthop_group_cmd
)
541 DEFUN_NOSH(nexthop_group
, nexthop_group_cmd
, "nexthop-group NHGNAME",
542 "Enter into the nexthop-group submode\n"
543 "Specify the NAME of the nexthop-group\n")
545 const char *nhg_name
= argv
[1]->arg
;
546 struct nexthop_group_cmd
*nhgc
= NULL
;
548 nhgc
= nhgc_get(nhg_name
);
549 VTY_PUSH_CONTEXT(NH_GROUP_NODE
, nhgc
);
554 DEFUN_NOSH(no_nexthop_group
, no_nexthop_group_cmd
, "no nexthop-group NHGNAME",
556 "Delete the nexthop-group\n"
557 "Specify the NAME of the nexthop-group\n")
559 const char *nhg_name
= argv
[2]->arg
;
560 struct nexthop_group_cmd
*nhgc
= NULL
;
562 nhgc
= nhgc_find(nhg_name
);
569 static void nexthop_group_save_nhop(struct nexthop_group_cmd
*nhgc
,
570 const char *nhvrf_name
,
571 const union sockunion
*addr
,
572 const char *intf
, const char *labels
,
573 const uint32_t weight
)
575 struct nexthop_hold
*nh
;
577 nh
= XCALLOC(MTYPE_TMP
, sizeof(*nh
));
580 nh
->nhvrf_name
= XSTRDUP(MTYPE_TMP
, nhvrf_name
);
582 nh
->intf
= XSTRDUP(MTYPE_TMP
, intf
);
584 nh
->addr
= sockunion_dup(addr
);
586 nh
->labels
= XSTRDUP(MTYPE_TMP
, labels
);
590 listnode_add_sort(nhgc
->nhg_list
, nh
);
593 static void nexthop_group_unsave_nhop(struct nexthop_group_cmd
*nhgc
,
594 const char *nhvrf_name
,
595 const union sockunion
*addr
,
596 const char *intf
, const char *labels
,
597 const uint32_t weight
)
599 struct nexthop_hold
*nh
;
600 struct listnode
*node
;
602 for (ALL_LIST_ELEMENTS_RO(nhgc
->nhg_list
, node
, nh
)) {
603 if (nhgc_cmp_helper(nhvrf_name
, nh
->nhvrf_name
) == 0
604 && nhgc_addr_cmp_helper(addr
, nh
->addr
) == 0
605 && nhgc_cmp_helper(intf
, nh
->intf
) == 0
606 && nhgc_cmp_helper(labels
, nh
->labels
) == 0
607 && weight
== nh
->weight
)
612 * Something has gone seriously wrong, fail gracefully
617 list_delete_node(nhgc
->nhg_list
, node
);
622 * Parse the config strings we support for a single nexthop. This gets used
623 * in a couple of different ways, and we distinguish between transient
624 * failures - such as a still-unprocessed interface - and fatal errors
625 * from label-string parsing.
627 static bool nexthop_group_parse_nexthop(struct nexthop
*nhop
,
628 const union sockunion
*addr
,
629 const char *intf
, const char *name
,
630 const char *labels
, int *lbl_ret
,
636 memset(nhop
, 0, sizeof(*nhop
));
639 vrf
= vrf_lookup_by_name(name
);
641 vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
646 nhop
->vrf_id
= vrf
->vrf_id
;
649 nhop
->ifindex
= ifname2ifindex(intf
, vrf
->vrf_id
);
650 if (nhop
->ifindex
== IFINDEX_INTERNAL
)
655 if (addr
->sa
.sa_family
== AF_INET
) {
656 nhop
->gate
.ipv4
.s_addr
= addr
->sin
.sin_addr
.s_addr
;
658 nhop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
660 nhop
->type
= NEXTHOP_TYPE_IPV4
;
662 nhop
->gate
.ipv6
= addr
->sin6
.sin6_addr
;
664 nhop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
666 nhop
->type
= NEXTHOP_TYPE_IPV6
;
669 nhop
->type
= NEXTHOP_TYPE_IFINDEX
;
673 mpls_label_t larray
[MPLS_MAX_LABELS
];
675 ret
= mpls_str2label(labels
, &num
, larray
);
677 /* Return label parse result */
684 nexthop_add_labels(nhop
, ZEBRA_LSP_NONE
,
688 nhop
->weight
= weight
;
694 * Wrapper to parse the strings in a 'nexthop_hold'
696 static bool nexthop_group_parse_nhh(struct nexthop
*nhop
,
697 const struct nexthop_hold
*nhh
)
699 return (nexthop_group_parse_nexthop(nhop
, nhh
->addr
, nhh
->intf
,
700 nhh
->nhvrf_name
, nhh
->labels
, NULL
,
704 DEFPY(ecmp_nexthops
, ecmp_nexthops_cmd
,
707 <A.B.C.D|X:X::X:X>$addr [INTERFACE$intf]\
711 nexthop-vrf NAME$vrf_name \
716 "Specify one of the nexthops in this ECMP group\n"
721 "If the nexthop is in a different vrf tell us\n"
722 "The nexthop-vrf Name\n"
723 "Specify label(s) for this nexthop\n"
724 "One or more labels in the range (16-1048575) separated by '/'\n"
725 "Weight to be used by the nexthop for purposes of ECMP\n"
726 "Weight value to be used\n")
728 VTY_DECLVAR_CONTEXT(nexthop_group_cmd
, nhgc
);
734 legal
= nexthop_group_parse_nexthop(&nhop
, addr
, intf
, vrf_name
, label
,
737 if (nhop
.type
== NEXTHOP_TYPE_IPV6
738 && IN6_IS_ADDR_LINKLOCAL(&nhop
.gate
.ipv6
)) {
740 "Specified a v6 LL with no interface, rejecting\n");
741 return CMD_WARNING_CONFIG_FAILED
;
744 /* Handle label-string errors */
745 if (!legal
&& lbl_ret
< 0) {
748 vty_out(vty
, "%% Malformed label(s)\n");
752 "%% Cannot use reserved label(s) (%d-%d)\n",
753 MPLS_LABEL_RESERVED_MIN
,
754 MPLS_LABEL_RESERVED_MAX
);
758 "%% Too many labels. Enter %d or fewer\n",
762 return CMD_WARNING_CONFIG_FAILED
;
765 nh
= nexthop_exists(&nhgc
->nhg
, &nhop
);
768 nexthop_group_unsave_nhop(nhgc
, vrf_name
, addr
, intf
, label
,
771 _nexthop_del(&nhgc
->nhg
, nh
);
773 if (nhg_hooks
.del_nexthop
)
774 nhg_hooks
.del_nexthop(nhgc
, nh
);
779 /* must be adding new nexthop since !no and !nexthop_exists */
783 memcpy(nh
, &nhop
, sizeof(nhop
));
784 _nexthop_add(&nhgc
->nhg
.nexthop
, nh
);
787 nexthop_group_save_nhop(nhgc
, vrf_name
, addr
, intf
, label
,
790 if (legal
&& nhg_hooks
.add_nexthop
)
791 nhg_hooks
.add_nexthop(nhgc
, nh
);
797 static struct cmd_node nexthop_group_node
= {
799 "%s(config-nh-group)# ",
803 void nexthop_group_write_nexthop(struct vty
*vty
, struct nexthop
*nh
)
808 vty_out(vty
, "nexthop ");
811 case NEXTHOP_TYPE_IFINDEX
:
812 vty_out(vty
, "%s", ifindex2ifname(nh
->ifindex
, nh
->vrf_id
));
814 case NEXTHOP_TYPE_IPV4
:
815 vty_out(vty
, "%s", inet_ntoa(nh
->gate
.ipv4
));
817 case NEXTHOP_TYPE_IPV4_IFINDEX
:
818 vty_out(vty
, "%s %s", inet_ntoa(nh
->gate
.ipv4
),
819 ifindex2ifname(nh
->ifindex
, nh
->vrf_id
));
821 case NEXTHOP_TYPE_IPV6
:
823 inet_ntop(AF_INET6
, &nh
->gate
.ipv6
, buf
, sizeof(buf
)));
825 case NEXTHOP_TYPE_IPV6_IFINDEX
:
826 vty_out(vty
, "%s %s",
827 inet_ntop(AF_INET6
, &nh
->gate
.ipv6
, buf
, sizeof(buf
)),
828 ifindex2ifname(nh
->ifindex
, nh
->vrf_id
));
830 case NEXTHOP_TYPE_BLACKHOLE
:
834 if (nh
->vrf_id
!= VRF_DEFAULT
) {
835 vrf
= vrf_lookup_by_id(nh
->vrf_id
);
836 vty_out(vty
, " nexthop-vrf %s", vrf
->name
);
839 if (nh
->nh_label
&& nh
->nh_label
->num_labels
> 0) {
842 mpls_label2str(nh
->nh_label
->num_labels
,
844 buf
, sizeof(buf
), 0);
845 vty_out(vty
, " label %s", buf
);
849 vty_out(vty
, " weight %u", nh
->weight
);
854 static void nexthop_group_write_nexthop_internal(struct vty
*vty
,
855 struct nexthop_hold
*nh
)
859 vty_out(vty
, "nexthop");
862 vty_out(vty
, " %s", sockunion2str(nh
->addr
, buf
, sizeof(buf
)));
865 vty_out(vty
, " %s", nh
->intf
);
868 vty_out(vty
, " nexthop-vrf %s", nh
->nhvrf_name
);
871 vty_out(vty
, " label %s", nh
->labels
);
874 vty_out(vty
, " weight %u", nh
->weight
);
879 static int nexthop_group_write(struct vty
*vty
)
881 struct nexthop_group_cmd
*nhgc
;
882 struct nexthop_hold
*nh
;
884 RB_FOREACH (nhgc
, nhgc_entry_head
, &nhgc_entries
) {
885 struct listnode
*node
;
887 vty_out(vty
, "nexthop-group %s\n", nhgc
->name
);
889 for (ALL_LIST_ELEMENTS_RO(nhgc
->nhg_list
, node
, nh
)) {
891 nexthop_group_write_nexthop_internal(vty
, nh
);
900 void nexthop_group_enable_vrf(struct vrf
*vrf
)
902 struct nexthop_group_cmd
*nhgc
;
903 struct nexthop_hold
*nhh
;
905 RB_FOREACH (nhgc
, nhgc_entry_head
, &nhgc_entries
) {
906 struct listnode
*node
;
908 for (ALL_LIST_ELEMENTS_RO(nhgc
->nhg_list
, node
, nhh
)) {
912 if (!nexthop_group_parse_nhh(&nhop
, nhh
))
915 nh
= nexthop_exists(&nhgc
->nhg
, &nhop
);
920 if (nhop
.vrf_id
!= vrf
->vrf_id
)
925 memcpy(nh
, &nhop
, sizeof(nhop
));
926 _nexthop_add(&nhgc
->nhg
.nexthop
, nh
);
928 if (nhg_hooks
.add_nexthop
)
929 nhg_hooks
.add_nexthop(nhgc
, nh
);
934 void nexthop_group_disable_vrf(struct vrf
*vrf
)
936 struct nexthop_group_cmd
*nhgc
;
937 struct nexthop_hold
*nhh
;
939 RB_FOREACH (nhgc
, nhgc_entry_head
, &nhgc_entries
) {
940 struct listnode
*node
;
942 for (ALL_LIST_ELEMENTS_RO(nhgc
->nhg_list
, node
, nhh
)) {
946 if (!nexthop_group_parse_nhh(&nhop
, nhh
))
949 nh
= nexthop_exists(&nhgc
->nhg
, &nhop
);
954 if (nh
->vrf_id
!= vrf
->vrf_id
)
957 _nexthop_del(&nhgc
->nhg
, nh
);
959 if (nhg_hooks
.del_nexthop
)
960 nhg_hooks
.del_nexthop(nhgc
, nh
);
967 void nexthop_group_interface_state_change(struct interface
*ifp
,
968 ifindex_t oldifindex
)
970 struct nexthop_group_cmd
*nhgc
;
971 struct nexthop_hold
*nhh
;
973 RB_FOREACH (nhgc
, nhgc_entry_head
, &nhgc_entries
) {
974 struct listnode
*node
;
978 for (ALL_LIST_ELEMENTS_RO(nhgc
->nhg_list
, node
, nhh
)) {
981 if (!nexthop_group_parse_nhh(&nhop
, nhh
))
985 case NEXTHOP_TYPE_IPV4
:
986 case NEXTHOP_TYPE_IPV6
:
987 case NEXTHOP_TYPE_BLACKHOLE
:
989 case NEXTHOP_TYPE_IFINDEX
:
990 case NEXTHOP_TYPE_IPV4_IFINDEX
:
991 case NEXTHOP_TYPE_IPV6_IFINDEX
:
994 nh
= nexthop_exists(&nhgc
->nhg
, &nhop
);
999 if (ifp
->ifindex
!= nhop
.ifindex
)
1004 memcpy(nh
, &nhop
, sizeof(nhop
));
1005 _nexthop_add(&nhgc
->nhg
.nexthop
, nh
);
1007 if (nhg_hooks
.add_nexthop
)
1008 nhg_hooks
.add_nexthop(nhgc
, nh
);
1011 struct nexthop
*next_nh
;
1013 for (nh
= nhgc
->nhg
.nexthop
; nh
; nh
= next_nh
) {
1016 case NEXTHOP_TYPE_IPV4
:
1017 case NEXTHOP_TYPE_IPV6
:
1018 case NEXTHOP_TYPE_BLACKHOLE
:
1020 case NEXTHOP_TYPE_IFINDEX
:
1021 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1022 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1026 if (oldifindex
!= nh
->ifindex
)
1029 _nexthop_del(&nhgc
->nhg
, nh
);
1031 if (nhg_hooks
.del_nexthop
)
1032 nhg_hooks
.del_nexthop(nhgc
, nh
);
1040 static void nhg_name_autocomplete(vector comps
, struct cmd_token
*token
)
1042 struct nexthop_group_cmd
*nhgc
;
1044 RB_FOREACH (nhgc
, nhgc_entry_head
, &nhgc_entries
) {
1045 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, nhgc
->name
));
1049 static const struct cmd_variable_handler nhg_name_handlers
[] = {
1050 {.tokenname
= "NHGNAME", .completions
= nhg_name_autocomplete
},
1051 {.completions
= NULL
}};
1053 void nexthop_group_init(void (*new)(const char *name
),
1054 void (*add_nexthop
)(const struct nexthop_group_cmd
*nhg
,
1055 const struct nexthop
*nhop
),
1056 void (*del_nexthop
)(const struct nexthop_group_cmd
*nhg
,
1057 const struct nexthop
*nhop
),
1058 void (*delete)(const char *name
))
1060 RB_INIT(nhgc_entry_head
, &nhgc_entries
);
1062 cmd_variable_handler_register(nhg_name_handlers
);
1064 install_node(&nexthop_group_node
, nexthop_group_write
);
1065 install_element(CONFIG_NODE
, &nexthop_group_cmd
);
1066 install_element(CONFIG_NODE
, &no_nexthop_group_cmd
);
1068 install_default(NH_GROUP_NODE
);
1069 install_element(NH_GROUP_NODE
, &ecmp_nexthops_cmd
);
1071 memset(&nhg_hooks
, 0, sizeof(nhg_hooks
));
1074 nhg_hooks
.new = new;
1076 nhg_hooks
.add_nexthop
= add_nexthop
;
1078 nhg_hooks
.del_nexthop
= del_nexthop
;
1080 nhg_hooks
.delete = delete;