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>
30 #ifndef VTYSH_EXTRACT_PL
31 #include "lib/nexthop_group_clippy.c"
34 DEFINE_MTYPE_STATIC(LIB
, NEXTHOP_GROUP
, "Nexthop Group")
36 struct nexthop_group_hooks
{
37 void (*new)(const char *name
);
38 void (*add_nexthop
)(const struct nexthop_group_cmd
*nhg
,
39 const struct nexthop
*nhop
);
40 void (*del_nexthop
)(const struct nexthop_group_cmd
*nhg
,
41 const struct nexthop
*nhop
);
42 void (*delete)(const char *name
);
45 static struct nexthop_group_hooks nhg_hooks
;
48 nexthop_group_cmd_compare(const struct nexthop_group_cmd
*nhgc1
,
49 const struct nexthop_group_cmd
*nhgc2
);
50 RB_GENERATE(nhgc_entry_head
, nexthop_group_cmd
, nhgc_entry
,
51 nexthop_group_cmd_compare
)
53 static struct nhgc_entry_head nhgc_entries
;
56 nexthop_group_cmd_compare(const struct nexthop_group_cmd
*nhgc1
,
57 const struct nexthop_group_cmd
*nhgc2
)
59 return strcmp(nhgc1
->name
, nhgc2
->name
);
62 uint8_t nexthop_group_nexthop_num(const struct nexthop_group
*nhg
)
67 for (ALL_NEXTHOPS_PTR(nhg
, nhop
))
73 uint8_t nexthop_group_active_nexthop_num(const struct nexthop_group
*nhg
)
78 for (ALL_NEXTHOPS_PTR(nhg
, nhop
)) {
79 if (CHECK_FLAG(nhop
->flags
, NEXTHOP_FLAG_ACTIVE
))
86 struct nexthop
*nexthop_exists(struct nexthop_group
*nhg
, struct nexthop
*nh
)
88 struct nexthop
*nexthop
;
90 for (nexthop
= nhg
->nexthop
; nexthop
; nexthop
= nexthop
->next
) {
91 if (nexthop_same(nh
, nexthop
))
98 struct nexthop_group
*nexthop_group_new(void)
100 return XCALLOC(MTYPE_NEXTHOP_GROUP
, sizeof(struct nexthop_group
));
103 void nexthop_group_copy(struct nexthop_group
*to
, struct nexthop_group
*from
)
105 /* Copy everything, including recursive info */
106 copy_nexthops(&to
->nexthop
, from
->nexthop
, NULL
);
109 void nexthop_group_delete(struct nexthop_group
**nhg
)
111 XFREE(MTYPE_NEXTHOP_GROUP
, *nhg
);
114 /* Add nexthop to the end of a nexthop list. */
115 void nexthop_add(struct nexthop
**target
, struct nexthop
*nexthop
)
117 struct nexthop
*last
;
119 for (last
= *target
; last
&& last
->next
; last
= last
->next
)
122 last
->next
= nexthop
;
125 nexthop
->prev
= last
;
128 void nexthop_group_add_sorted(struct nexthop_group
*nhg
,
129 struct nexthop
*nexthop
)
131 struct nexthop
*position
, *prev
;
133 for (position
= nhg
->nexthop
, prev
= NULL
; position
;
134 prev
= position
, position
= position
->next
) {
135 if (nexthop_cmp(position
, nexthop
) > 0) {
136 nexthop
->next
= position
;
137 nexthop
->prev
= prev
;
140 nexthop
->prev
->next
= nexthop
;
142 nhg
->nexthop
= nexthop
;
144 position
->prev
= nexthop
;
149 nexthop
->prev
= prev
;
151 prev
->next
= nexthop
;
153 nhg
->nexthop
= nexthop
;
157 /* Delete nexthop from a nexthop list. */
158 void nexthop_del(struct nexthop_group
*nhg
, struct nexthop
*nh
)
160 struct nexthop
*nexthop
;
162 for (nexthop
= nhg
->nexthop
; nexthop
; nexthop
= nexthop
->next
) {
163 if (nexthop_same(nh
, nexthop
))
170 nexthop
->prev
->next
= nexthop
->next
;
172 nhg
->nexthop
= nexthop
->next
;
175 nexthop
->next
->prev
= nexthop
->prev
;
181 void copy_nexthops(struct nexthop
**tnh
, const struct nexthop
*nh
,
182 struct nexthop
*rparent
)
184 struct nexthop
*nexthop
;
185 const struct nexthop
*nh1
;
187 for (nh1
= nh
; nh1
; nh1
= nh1
->next
) {
188 nexthop
= nexthop_new();
189 nexthop
->vrf_id
= nh1
->vrf_id
;
190 nexthop
->ifindex
= nh1
->ifindex
;
191 nexthop
->type
= nh1
->type
;
192 nexthop
->flags
= nh1
->flags
;
193 memcpy(&nexthop
->gate
, &nh1
->gate
, sizeof(nh1
->gate
));
194 memcpy(&nexthop
->src
, &nh1
->src
, sizeof(nh1
->src
));
195 memcpy(&nexthop
->rmap_src
, &nh1
->rmap_src
,
196 sizeof(nh1
->rmap_src
));
197 nexthop
->rparent
= rparent
;
199 nexthop_add_labels(nexthop
, nh1
->nh_label_type
,
200 nh1
->nh_label
->num_labels
,
201 &nh1
->nh_label
->label
[0]);
202 nexthop_add(tnh
, nexthop
);
204 if (CHECK_FLAG(nh1
->flags
, NEXTHOP_FLAG_RECURSIVE
))
205 copy_nexthops(&nexthop
->resolved
, nh1
->resolved
,
210 uint32_t nexthop_group_hash(const struct nexthop_group
*nhg
)
216 * We are not interested in hashing over any recursively
219 for (nh
= nhg
->nexthop
; nh
; nh
= nh
->next
)
220 key
= jhash_1word(nexthop_hash(nh
), key
);
225 static void nhgc_delete_nexthops(struct nexthop_group_cmd
*nhgc
)
227 struct nexthop
*nexthop
;
229 nexthop
= nhgc
->nhg
.nexthop
;
231 struct nexthop
*next
= nexthop_next(nexthop
);
233 nexthop_del(&nhgc
->nhg
, nexthop
);
234 if (nhg_hooks
.del_nexthop
)
235 nhg_hooks
.del_nexthop(nhgc
, nexthop
);
237 nexthop_free(nexthop
);
243 struct nexthop_group_cmd
*nhgc_find(const char *name
)
245 struct nexthop_group_cmd find
;
247 strlcpy(find
.name
, name
, sizeof(find
.name
));
249 return RB_FIND(nhgc_entry_head
, &nhgc_entries
, &find
);
252 static int nhgc_cmp_helper(const char *a
, const char *b
)
266 static int nhgc_addr_cmp_helper(const union sockunion
*a
, const union sockunion
*b
)
277 return sockunion_cmp(a
, b
);
280 static int nhgl_cmp(struct nexthop_hold
*nh1
, struct nexthop_hold
*nh2
)
284 ret
= nhgc_addr_cmp_helper(nh1
->addr
, nh2
->addr
);
288 ret
= nhgc_cmp_helper(nh1
->intf
, nh2
->intf
);
292 return nhgc_cmp_helper(nh1
->nhvrf_name
, nh2
->nhvrf_name
);
295 static void nhgl_delete(struct nexthop_hold
*nh
)
297 XFREE(MTYPE_TMP
, nh
->intf
);
299 XFREE(MTYPE_TMP
, nh
->nhvrf_name
);
302 sockunion_free(nh
->addr
);
304 XFREE(MTYPE_TMP
, nh
);
307 static struct nexthop_group_cmd
*nhgc_get(const char *name
)
309 struct nexthop_group_cmd
*nhgc
;
311 nhgc
= nhgc_find(name
);
313 nhgc
= XCALLOC(MTYPE_TMP
, sizeof(*nhgc
));
314 strlcpy(nhgc
->name
, name
, sizeof(nhgc
->name
));
316 QOBJ_REG(nhgc
, nexthop_group_cmd
);
317 RB_INSERT(nhgc_entry_head
, &nhgc_entries
, nhgc
);
319 nhgc
->nhg_list
= list_new();
320 nhgc
->nhg_list
->cmp
= (int (*)(void *, void *))nhgl_cmp
;
321 nhgc
->nhg_list
->del
= (void (*)(void *))nhgl_delete
;
330 static void nhgc_delete(struct nexthop_group_cmd
*nhgc
)
332 nhgc_delete_nexthops(nhgc
);
334 if (nhg_hooks
.delete)
335 nhg_hooks
.delete(nhgc
->name
);
337 RB_REMOVE(nhgc_entry_head
, &nhgc_entries
, nhgc
);
339 list_delete(&nhgc
->nhg_list
);
341 XFREE(MTYPE_TMP
, nhgc
);
344 DEFINE_QOBJ_TYPE(nexthop_group_cmd
)
346 DEFUN_NOSH(nexthop_group
, nexthop_group_cmd
, "nexthop-group NHGNAME",
347 "Enter into the nexthop-group submode\n"
348 "Specify the NAME of the nexthop-group\n")
350 const char *nhg_name
= argv
[1]->arg
;
351 struct nexthop_group_cmd
*nhgc
= NULL
;
353 nhgc
= nhgc_get(nhg_name
);
354 VTY_PUSH_CONTEXT(NH_GROUP_NODE
, nhgc
);
359 DEFUN_NOSH(no_nexthop_group
, no_nexthop_group_cmd
, "no nexthop-group NHGNAME",
361 "Delete the nexthop-group\n"
362 "Specify the NAME of the nexthop-group\n")
364 const char *nhg_name
= argv
[2]->arg
;
365 struct nexthop_group_cmd
*nhgc
= NULL
;
367 nhgc
= nhgc_find(nhg_name
);
374 static void nexthop_group_save_nhop(struct nexthop_group_cmd
*nhgc
,
375 const char *nhvrf_name
,
376 const union sockunion
*addr
,
379 struct nexthop_hold
*nh
;
381 nh
= XCALLOC(MTYPE_TMP
, sizeof(*nh
));
384 nh
->nhvrf_name
= XSTRDUP(MTYPE_TMP
, nhvrf_name
);
386 nh
->intf
= XSTRDUP(MTYPE_TMP
, intf
);
388 nh
->addr
= sockunion_dup(addr
);
390 listnode_add_sort(nhgc
->nhg_list
, nh
);
393 static void nexthop_group_unsave_nhop(struct nexthop_group_cmd
*nhgc
,
394 const char *nhvrf_name
,
395 const union sockunion
*addr
,
398 struct nexthop_hold
*nh
;
399 struct listnode
*node
;
401 for (ALL_LIST_ELEMENTS_RO(nhgc
->nhg_list
, node
, nh
)) {
402 if (nhgc_cmp_helper(nhvrf_name
, nh
->nhvrf_name
) == 0 &&
403 nhgc_addr_cmp_helper(addr
, nh
->addr
) == 0 &&
404 nhgc_cmp_helper(intf
, nh
->intf
) == 0)
409 * Something has gone seriously wrong, fail gracefully
414 list_delete_node(nhgc
->nhg_list
, node
);
418 static bool nexthop_group_parse_nexthop(struct nexthop
*nhop
,
419 const union sockunion
*addr
,
420 const char *intf
, const char *name
)
424 memset(nhop
, 0, sizeof(*nhop
));
427 vrf
= vrf_lookup_by_name(name
);
429 vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
434 nhop
->vrf_id
= vrf
->vrf_id
;
437 nhop
->ifindex
= ifname2ifindex(intf
, vrf
->vrf_id
);
438 if (nhop
->ifindex
== IFINDEX_INTERNAL
)
443 if (addr
->sa
.sa_family
== AF_INET
) {
444 nhop
->gate
.ipv4
.s_addr
= addr
->sin
.sin_addr
.s_addr
;
446 nhop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
448 nhop
->type
= NEXTHOP_TYPE_IPV4
;
450 nhop
->gate
.ipv6
= addr
->sin6
.sin6_addr
;
452 nhop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
454 nhop
->type
= NEXTHOP_TYPE_IPV6
;
457 nhop
->type
= NEXTHOP_TYPE_IFINDEX
;
462 DEFPY(ecmp_nexthops
, ecmp_nexthops_cmd
,
465 <A.B.C.D|X:X::X:X>$addr [INTERFACE$intf]\
468 [nexthop-vrf NAME$name]",
470 "Specify one of the nexthops in this ECMP group\n"
475 "If the nexthop is in a different vrf tell us\n"
476 "The nexthop-vrf Name\n")
478 VTY_DECLVAR_CONTEXT(nexthop_group_cmd
, nhgc
);
483 legal
= nexthop_group_parse_nexthop(&nhop
, addr
, intf
, name
);
485 if (nhop
.type
== NEXTHOP_TYPE_IPV6
486 && IN6_IS_ADDR_LINKLOCAL(&nhop
.gate
.ipv6
)) {
488 "Specified a v6 LL with no interface, rejecting\n");
489 return CMD_WARNING_CONFIG_FAILED
;
492 nh
= nexthop_exists(&nhgc
->nhg
, &nhop
);
495 nexthop_group_unsave_nhop(nhgc
, name
, addr
, intf
);
497 nexthop_del(&nhgc
->nhg
, nh
);
499 if (nhg_hooks
.del_nexthop
)
500 nhg_hooks
.del_nexthop(nhgc
, nh
);
505 /* must be adding new nexthop since !no and !nexthop_exists */
509 memcpy(nh
, &nhop
, sizeof(nhop
));
510 nexthop_add(&nhgc
->nhg
.nexthop
, nh
);
513 nexthop_group_save_nhop(nhgc
, name
, addr
, intf
);
515 if (legal
&& nhg_hooks
.add_nexthop
)
516 nhg_hooks
.add_nexthop(nhgc
, nh
);
522 struct cmd_node nexthop_group_node
= {
524 "%s(config-nh-group)# ",
528 void nexthop_group_write_nexthop(struct vty
*vty
, struct nexthop
*nh
)
533 vty_out(vty
, "nexthop ");
536 case NEXTHOP_TYPE_IFINDEX
:
537 vty_out(vty
, "%s", ifindex2ifname(nh
->ifindex
, nh
->vrf_id
));
539 case NEXTHOP_TYPE_IPV4
:
540 vty_out(vty
, "%s", inet_ntoa(nh
->gate
.ipv4
));
542 case NEXTHOP_TYPE_IPV4_IFINDEX
:
543 vty_out(vty
, "%s %s", inet_ntoa(nh
->gate
.ipv4
),
544 ifindex2ifname(nh
->ifindex
, nh
->vrf_id
));
546 case NEXTHOP_TYPE_IPV6
:
548 inet_ntop(AF_INET6
, &nh
->gate
.ipv6
, buf
, sizeof(buf
)));
550 case NEXTHOP_TYPE_IPV6_IFINDEX
:
551 vty_out(vty
, "%s %s",
552 inet_ntop(AF_INET6
, &nh
->gate
.ipv6
, buf
, sizeof(buf
)),
553 ifindex2ifname(nh
->ifindex
, nh
->vrf_id
));
555 case NEXTHOP_TYPE_BLACKHOLE
:
559 if (nh
->vrf_id
!= VRF_DEFAULT
) {
560 vrf
= vrf_lookup_by_id(nh
->vrf_id
);
561 vty_out(vty
, " nexthop-vrf %s", vrf
->name
);
566 static void nexthop_group_write_nexthop_internal(struct vty
*vty
,
567 struct nexthop_hold
*nh
)
571 vty_out(vty
, "nexthop");
574 vty_out(vty
, " %s", sockunion2str(nh
->addr
, buf
, sizeof(buf
)));
577 vty_out(vty
, " %s", nh
->intf
);
580 vty_out(vty
, " nexthop-vrf %s", nh
->nhvrf_name
);
585 static int nexthop_group_write(struct vty
*vty
)
587 struct nexthop_group_cmd
*nhgc
;
588 struct nexthop_hold
*nh
;
590 RB_FOREACH (nhgc
, nhgc_entry_head
, &nhgc_entries
) {
591 struct listnode
*node
;
593 vty_out(vty
, "nexthop-group %s\n", nhgc
->name
);
595 for (ALL_LIST_ELEMENTS_RO(nhgc
->nhg_list
, node
, nh
)) {
597 nexthop_group_write_nexthop_internal(vty
, nh
);
606 void nexthop_group_enable_vrf(struct vrf
*vrf
)
608 struct nexthop_group_cmd
*nhgc
;
609 struct nexthop_hold
*nhh
;
611 RB_FOREACH (nhgc
, nhgc_entry_head
, &nhgc_entries
) {
612 struct listnode
*node
;
614 for (ALL_LIST_ELEMENTS_RO(nhgc
->nhg_list
, node
, nhh
)) {
618 if (!nexthop_group_parse_nexthop(&nhop
, nhh
->addr
,
623 nh
= nexthop_exists(&nhgc
->nhg
, &nhop
);
628 if (nhop
.vrf_id
!= vrf
->vrf_id
)
633 memcpy(nh
, &nhop
, sizeof(nhop
));
634 nexthop_add(&nhgc
->nhg
.nexthop
, nh
);
636 if (nhg_hooks
.add_nexthop
)
637 nhg_hooks
.add_nexthop(nhgc
, nh
);
642 void nexthop_group_disable_vrf(struct vrf
*vrf
)
644 struct nexthop_group_cmd
*nhgc
;
645 struct nexthop_hold
*nhh
;
647 RB_FOREACH (nhgc
, nhgc_entry_head
, &nhgc_entries
) {
648 struct listnode
*node
;
650 for (ALL_LIST_ELEMENTS_RO(nhgc
->nhg_list
, node
, nhh
)) {
654 if (!nexthop_group_parse_nexthop(&nhop
, nhh
->addr
,
659 nh
= nexthop_exists(&nhgc
->nhg
, &nhop
);
664 if (nh
->vrf_id
!= vrf
->vrf_id
)
667 nexthop_del(&nhgc
->nhg
, nh
);
669 if (nhg_hooks
.del_nexthop
)
670 nhg_hooks
.del_nexthop(nhgc
, nh
);
677 void nexthop_group_interface_state_change(struct interface
*ifp
,
678 ifindex_t oldifindex
)
680 struct nexthop_group_cmd
*nhgc
;
681 struct nexthop_hold
*nhh
;
683 RB_FOREACH (nhgc
, nhgc_entry_head
, &nhgc_entries
) {
684 struct listnode
*node
;
688 for (ALL_LIST_ELEMENTS_RO(nhgc
->nhg_list
, node
, nhh
)) {
691 if (!nexthop_group_parse_nexthop(
692 &nhop
, nhh
->addr
, nhh
->intf
,
697 case NEXTHOP_TYPE_IPV4
:
698 case NEXTHOP_TYPE_IPV6
:
699 case NEXTHOP_TYPE_BLACKHOLE
:
701 case NEXTHOP_TYPE_IFINDEX
:
702 case NEXTHOP_TYPE_IPV4_IFINDEX
:
703 case NEXTHOP_TYPE_IPV6_IFINDEX
:
706 nh
= nexthop_exists(&nhgc
->nhg
, &nhop
);
711 if (ifp
->ifindex
!= nhop
.ifindex
)
716 memcpy(nh
, &nhop
, sizeof(nhop
));
717 nexthop_add(&nhgc
->nhg
.nexthop
, nh
);
719 if (nhg_hooks
.add_nexthop
)
720 nhg_hooks
.add_nexthop(nhgc
, nh
);
723 struct nexthop
*next_nh
;
725 for (nh
= nhgc
->nhg
.nexthop
; nh
; nh
= next_nh
) {
728 case NEXTHOP_TYPE_IPV4
:
729 case NEXTHOP_TYPE_IPV6
:
730 case NEXTHOP_TYPE_BLACKHOLE
:
732 case NEXTHOP_TYPE_IFINDEX
:
733 case NEXTHOP_TYPE_IPV4_IFINDEX
:
734 case NEXTHOP_TYPE_IPV6_IFINDEX
:
738 if (oldifindex
!= nh
->ifindex
)
741 nexthop_del(&nhgc
->nhg
, nh
);
743 if (nhg_hooks
.del_nexthop
)
744 nhg_hooks
.del_nexthop(nhgc
, nh
);
752 static void nhg_name_autocomplete(vector comps
, struct cmd_token
*token
)
754 struct nexthop_group_cmd
*nhgc
;
756 RB_FOREACH (nhgc
, nhgc_entry_head
, &nhgc_entries
) {
757 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, nhgc
->name
));
761 static const struct cmd_variable_handler nhg_name_handlers
[] = {
762 {.tokenname
= "NHGNAME", .completions
= nhg_name_autocomplete
},
763 {.completions
= NULL
}};
765 void nexthop_group_init(void (*new)(const char *name
),
766 void (*add_nexthop
)(const struct nexthop_group_cmd
*nhg
,
767 const struct nexthop
*nhop
),
768 void (*del_nexthop
)(const struct nexthop_group_cmd
*nhg
,
769 const struct nexthop
*nhop
),
770 void (*delete)(const char *name
))
772 RB_INIT(nhgc_entry_head
, &nhgc_entries
);
774 cmd_variable_handler_register(nhg_name_handlers
);
776 install_node(&nexthop_group_node
, nexthop_group_write
);
777 install_element(CONFIG_NODE
, &nexthop_group_cmd
);
778 install_element(CONFIG_NODE
, &no_nexthop_group_cmd
);
780 install_default(NH_GROUP_NODE
);
781 install_element(NH_GROUP_NODE
, &ecmp_nexthops_cmd
);
783 memset(&nhg_hooks
, 0, sizeof(nhg_hooks
));
788 nhg_hooks
.add_nexthop
= add_nexthop
;
790 nhg_hooks
.del_nexthop
= del_nexthop
;
792 nhg_hooks
.delete = delete;