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>
29 #ifndef VTYSH_EXTRACT_PL
30 #include "lib/nexthop_group_clippy.c"
33 DEFINE_MTYPE_STATIC(LIB
, NEXTHOP_GROUP
, "Nexthop Group")
35 struct nexthop_group_hooks
{
36 void (*new)(const char *name
);
37 void (*add_nexthop
)(const struct nexthop_group_cmd
*nhg
,
38 const struct nexthop
*nhop
);
39 void (*del_nexthop
)(const struct nexthop_group_cmd
*nhg
,
40 const struct nexthop
*nhop
);
41 void (*delete)(const char *name
);
44 static struct nexthop_group_hooks nhg_hooks
;
47 nexthop_group_cmd_compare(const struct nexthop_group_cmd
*nhgc1
,
48 const struct nexthop_group_cmd
*nhgc2
);
49 RB_GENERATE(nhgc_entry_head
, nexthop_group_cmd
, nhgc_entry
,
50 nexthop_group_cmd_compare
)
52 static struct nhgc_entry_head nhgc_entries
;
55 nexthop_group_cmd_compare(const struct nexthop_group_cmd
*nhgc1
,
56 const struct nexthop_group_cmd
*nhgc2
)
58 return strcmp(nhgc1
->name
, nhgc2
->name
);
61 struct nexthop
*nexthop_exists(struct nexthop_group
*nhg
, struct nexthop
*nh
)
63 struct nexthop
*nexthop
;
65 for (nexthop
= nhg
->nexthop
; nexthop
; nexthop
= nexthop
->next
) {
66 if (nexthop_same(nh
, nexthop
))
73 struct nexthop_group
*nexthop_group_new(void)
75 return XCALLOC(MTYPE_NEXTHOP_GROUP
, sizeof(struct nexthop_group
));
78 void nexthop_group_delete(struct nexthop_group
**nhg
)
80 XFREE(MTYPE_NEXTHOP_GROUP
, *nhg
);
83 /* Add nexthop to the end of a nexthop list. */
84 void nexthop_add(struct nexthop
**target
, struct nexthop
*nexthop
)
88 for (last
= *target
; last
&& last
->next
; last
= last
->next
)
97 /* Delete nexthop from a nexthop list. */
98 void nexthop_del(struct nexthop_group
*nhg
, struct nexthop
*nh
)
100 struct nexthop
*nexthop
;
102 for (nexthop
= nhg
->nexthop
; nexthop
; nexthop
= nexthop
->next
) {
103 if (nexthop_same(nh
, nexthop
))
110 nexthop
->prev
->next
= nexthop
->next
;
112 nhg
->nexthop
= nexthop
->next
;
115 nexthop
->next
->prev
= nexthop
->prev
;
121 void copy_nexthops(struct nexthop
**tnh
, struct nexthop
*nh
,
122 struct nexthop
*rparent
)
124 struct nexthop
*nexthop
;
127 for (nh1
= nh
; nh1
; nh1
= nh1
->next
) {
128 nexthop
= nexthop_new();
129 nexthop
->vrf_id
= nh1
->vrf_id
;
130 nexthop
->ifindex
= nh1
->ifindex
;
131 nexthop
->type
= nh1
->type
;
132 nexthop
->flags
= nh1
->flags
;
133 memcpy(&nexthop
->gate
, &nh1
->gate
, sizeof(nh1
->gate
));
134 memcpy(&nexthop
->src
, &nh1
->src
, sizeof(nh1
->src
));
135 memcpy(&nexthop
->rmap_src
, &nh1
->rmap_src
,
136 sizeof(nh1
->rmap_src
));
137 nexthop
->rparent
= rparent
;
139 nexthop_add_labels(nexthop
, nh1
->nh_label_type
,
140 nh1
->nh_label
->num_labels
,
141 &nh1
->nh_label
->label
[0]);
142 nexthop_add(tnh
, nexthop
);
144 if (CHECK_FLAG(nh1
->flags
, NEXTHOP_FLAG_RECURSIVE
))
145 copy_nexthops(&nexthop
->resolved
, nh1
->resolved
,
150 static void nhgc_delete_nexthops(struct nexthop_group_cmd
*nhgc
)
152 struct nexthop
*nexthop
;
154 nexthop
= nhgc
->nhg
.nexthop
;
156 struct nexthop
*next
= nexthop_next(nexthop
);
158 nexthop_del(&nhgc
->nhg
, nexthop
);
159 if (nhg_hooks
.del_nexthop
)
160 nhg_hooks
.del_nexthop(nhgc
, nexthop
);
162 nexthop_free(nexthop
);
168 struct nexthop_group_cmd
*nhgc_find(const char *name
)
170 struct nexthop_group_cmd find
;
172 strlcpy(find
.name
, name
, sizeof(find
.name
));
174 return RB_FIND(nhgc_entry_head
, &nhgc_entries
, &find
);
177 static int nhgc_cmp_helper(const char *a
, const char *b
)
191 static int nhgc_addr_cmp_helper(const union sockunion
*a
, const union sockunion
*b
)
202 return sockunion_cmp(a
, b
);
205 static int nhgl_cmp(struct nexthop_hold
*nh1
, struct nexthop_hold
*nh2
)
209 ret
= nhgc_addr_cmp_helper(nh1
->addr
, nh2
->addr
);
213 ret
= nhgc_cmp_helper(nh1
->intf
, nh2
->intf
);
217 return nhgc_cmp_helper(nh1
->nhvrf_name
, nh2
->nhvrf_name
);
220 static void nhgl_delete(struct nexthop_hold
*nh
)
222 XFREE(MTYPE_TMP
, nh
->intf
);
224 XFREE(MTYPE_TMP
, nh
->nhvrf_name
);
227 sockunion_free(nh
->addr
);
229 XFREE(MTYPE_TMP
, nh
);
232 static struct nexthop_group_cmd
*nhgc_get(const char *name
)
234 struct nexthop_group_cmd
*nhgc
;
236 nhgc
= nhgc_find(name
);
238 nhgc
= XCALLOC(MTYPE_TMP
, sizeof(*nhgc
));
239 strlcpy(nhgc
->name
, name
, sizeof(nhgc
->name
));
241 QOBJ_REG(nhgc
, nexthop_group_cmd
);
242 RB_INSERT(nhgc_entry_head
, &nhgc_entries
, nhgc
);
244 nhgc
->nhg_list
= list_new();
245 nhgc
->nhg_list
->cmp
= (int (*)(void *, void *))nhgl_cmp
;
246 nhgc
->nhg_list
->del
= (void (*)(void *))nhgl_delete
;
255 static void nhgc_delete(struct nexthop_group_cmd
*nhgc
)
257 nhgc_delete_nexthops(nhgc
);
259 if (nhg_hooks
.delete)
260 nhg_hooks
.delete(nhgc
->name
);
262 RB_REMOVE(nhgc_entry_head
, &nhgc_entries
, nhgc
);
264 list_delete(&nhgc
->nhg_list
);
266 XFREE(MTYPE_TMP
, nhgc
);
269 DEFINE_QOBJ_TYPE(nexthop_group_cmd
)
271 DEFUN_NOSH(nexthop_group
, nexthop_group_cmd
, "nexthop-group NAME",
272 "Enter into the nexthop-group submode\n"
273 "Specify the NAME of the nexthop-group\n")
275 const char *nhg_name
= argv
[1]->arg
;
276 struct nexthop_group_cmd
*nhgc
= NULL
;
278 nhgc
= nhgc_get(nhg_name
);
279 VTY_PUSH_CONTEXT(NH_GROUP_NODE
, nhgc
);
284 DEFUN_NOSH(no_nexthop_group
, no_nexthop_group_cmd
, "no nexthop-group NAME",
286 "Delete the nexthop-group\n"
287 "Specify the NAME of the nexthop-group\n")
289 const char *nhg_name
= argv
[2]->arg
;
290 struct nexthop_group_cmd
*nhgc
= NULL
;
292 nhgc
= nhgc_find(nhg_name
);
299 static void nexthop_group_save_nhop(struct nexthop_group_cmd
*nhgc
,
300 const char *nhvrf_name
,
301 const union sockunion
*addr
,
304 struct nexthop_hold
*nh
;
306 nh
= XCALLOC(MTYPE_TMP
, sizeof(*nh
));
309 nh
->nhvrf_name
= XSTRDUP(MTYPE_TMP
, nhvrf_name
);
311 nh
->intf
= XSTRDUP(MTYPE_TMP
, intf
);
313 nh
->addr
= sockunion_dup(addr
);
315 listnode_add_sort(nhgc
->nhg_list
, nh
);
318 static void nexthop_group_unsave_nhop(struct nexthop_group_cmd
*nhgc
,
319 const char *nhvrf_name
,
320 const union sockunion
*addr
,
323 struct nexthop_hold
*nh
;
324 struct listnode
*node
;
326 for (ALL_LIST_ELEMENTS_RO(nhgc
->nhg_list
, node
, nh
)) {
327 if (nhgc_cmp_helper(nhvrf_name
, nh
->nhvrf_name
) == 0 &&
328 nhgc_addr_cmp_helper(addr
, nh
->addr
) == 0 &&
329 nhgc_cmp_helper(intf
, nh
->intf
) == 0)
334 * Something has gone seriously wrong, fail gracefully
339 list_delete_node(nhgc
->nhg_list
, node
);
343 static bool nexthop_group_parse_nexthop(struct nexthop
*nhop
,
344 const union sockunion
*addr
,
345 const char *intf
, const char *name
)
349 memset(nhop
, 0, sizeof(*nhop
));
352 vrf
= vrf_lookup_by_name(name
);
354 vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
359 nhop
->vrf_id
= vrf
->vrf_id
;
362 nhop
->ifindex
= ifname2ifindex(intf
, vrf
->vrf_id
);
363 if (nhop
->ifindex
== IFINDEX_INTERNAL
)
368 if (addr
->sa
.sa_family
== AF_INET
) {
369 nhop
->gate
.ipv4
.s_addr
= addr
->sin
.sin_addr
.s_addr
;
371 nhop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
373 nhop
->type
= NEXTHOP_TYPE_IPV4
;
375 nhop
->gate
.ipv6
= addr
->sin6
.sin6_addr
;
377 nhop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
379 nhop
->type
= NEXTHOP_TYPE_IPV6
;
382 nhop
->type
= NEXTHOP_TYPE_IFINDEX
;
387 DEFPY(ecmp_nexthops
, ecmp_nexthops_cmd
,
390 <A.B.C.D|X:X::X:X>$addr [INTERFACE$intf]\
393 [nexthop-vrf NAME$name]",
395 "Specify one of the nexthops in this ECMP group\n"
400 "If the nexthop is in a different vrf tell us\n"
401 "The nexthop-vrf Name\n")
403 VTY_DECLVAR_CONTEXT(nexthop_group_cmd
, nhgc
);
408 legal
= nexthop_group_parse_nexthop(&nhop
, addr
, intf
, name
);
410 if (nhop
.type
== NEXTHOP_TYPE_IPV6
411 && IN6_IS_ADDR_LINKLOCAL(&nhop
.gate
.ipv6
)) {
413 "Specified a v6 LL with no interface, rejecting\n");
414 return CMD_WARNING_CONFIG_FAILED
;
417 nh
= nexthop_exists(&nhgc
->nhg
, &nhop
);
420 nexthop_group_unsave_nhop(nhgc
, name
, addr
, intf
);
422 nexthop_del(&nhgc
->nhg
, nh
);
424 if (nhg_hooks
.del_nexthop
)
425 nhg_hooks
.del_nexthop(nhgc
, nh
);
430 /* must be adding new nexthop since !no and !nexthop_exists */
434 memcpy(nh
, &nhop
, sizeof(nhop
));
435 nexthop_add(&nhgc
->nhg
.nexthop
, nh
);
438 nexthop_group_save_nhop(nhgc
, name
, addr
, intf
);
440 if (legal
&& nhg_hooks
.add_nexthop
)
441 nhg_hooks
.add_nexthop(nhgc
, nh
);
447 struct cmd_node nexthop_group_node
= {
449 "%s(config-nh-group)# ",
453 void nexthop_group_write_nexthop(struct vty
*vty
, struct nexthop
*nh
)
458 vty_out(vty
, "nexthop ");
461 case NEXTHOP_TYPE_IFINDEX
:
462 vty_out(vty
, "%s", ifindex2ifname(nh
->ifindex
, nh
->vrf_id
));
464 case NEXTHOP_TYPE_IPV4
:
465 vty_out(vty
, "%s", inet_ntoa(nh
->gate
.ipv4
));
467 case NEXTHOP_TYPE_IPV4_IFINDEX
:
468 vty_out(vty
, "%s %s", inet_ntoa(nh
->gate
.ipv4
),
469 ifindex2ifname(nh
->ifindex
, nh
->vrf_id
));
471 case NEXTHOP_TYPE_IPV6
:
473 inet_ntop(AF_INET6
, &nh
->gate
.ipv6
, buf
, sizeof(buf
)));
475 case NEXTHOP_TYPE_IPV6_IFINDEX
:
476 vty_out(vty
, "%s %s",
477 inet_ntop(AF_INET6
, &nh
->gate
.ipv6
, buf
, sizeof(buf
)),
478 ifindex2ifname(nh
->ifindex
, nh
->vrf_id
));
480 case NEXTHOP_TYPE_BLACKHOLE
:
484 if (nh
->vrf_id
!= VRF_DEFAULT
) {
485 vrf
= vrf_lookup_by_id(nh
->vrf_id
);
486 vty_out(vty
, " nexthop-vrf %s", vrf
->name
);
491 static void nexthop_group_write_nexthop_internal(struct vty
*vty
,
492 struct nexthop_hold
*nh
)
496 vty_out(vty
, "nexthop");
499 vty_out(vty
, " %s", sockunion2str(nh
->addr
, buf
, sizeof(buf
)));
502 vty_out(vty
, " %s", nh
->intf
);
505 vty_out(vty
, " nexthop-vrf %s", nh
->nhvrf_name
);
510 static int nexthop_group_write(struct vty
*vty
)
512 struct nexthop_group_cmd
*nhgc
;
513 struct nexthop_hold
*nh
;
515 RB_FOREACH (nhgc
, nhgc_entry_head
, &nhgc_entries
) {
516 struct listnode
*node
;
518 vty_out(vty
, "nexthop-group %s\n", nhgc
->name
);
520 for (ALL_LIST_ELEMENTS_RO(nhgc
->nhg_list
, node
, nh
)) {
522 nexthop_group_write_nexthop_internal(vty
, nh
);
531 void nexthop_group_enable_vrf(struct vrf
*vrf
)
533 struct nexthop_group_cmd
*nhgc
;
534 struct nexthop_hold
*nhh
;
536 RB_FOREACH (nhgc
, nhgc_entry_head
, &nhgc_entries
) {
537 struct listnode
*node
;
539 for (ALL_LIST_ELEMENTS_RO(nhgc
->nhg_list
, node
, nhh
)) {
543 if (!nexthop_group_parse_nexthop(&nhop
, nhh
->addr
,
548 nh
= nexthop_exists(&nhgc
->nhg
, &nhop
);
553 if (nhop
.vrf_id
!= vrf
->vrf_id
)
558 memcpy(nh
, &nhop
, sizeof(nhop
));
559 nexthop_add(&nhgc
->nhg
.nexthop
, nh
);
561 if (nhg_hooks
.add_nexthop
)
562 nhg_hooks
.add_nexthop(nhgc
, nh
);
567 void nexthop_group_disable_vrf(struct vrf
*vrf
)
569 struct nexthop_group_cmd
*nhgc
;
570 struct nexthop_hold
*nhh
;
572 RB_FOREACH (nhgc
, nhgc_entry_head
, &nhgc_entries
) {
573 struct listnode
*node
;
575 for (ALL_LIST_ELEMENTS_RO(nhgc
->nhg_list
, node
, nhh
)) {
579 if (!nexthop_group_parse_nexthop(&nhop
, nhh
->addr
,
584 nh
= nexthop_exists(&nhgc
->nhg
, &nhop
);
589 if (nh
->vrf_id
!= vrf
->vrf_id
)
592 nexthop_del(&nhgc
->nhg
, nh
);
594 if (nhg_hooks
.del_nexthop
)
595 nhg_hooks
.del_nexthop(nhgc
, nh
);
602 void nexthop_group_interface_state_change(struct interface
*ifp
,
603 ifindex_t oldifindex
)
605 struct nexthop_group_cmd
*nhgc
;
606 struct nexthop_hold
*nhh
;
608 RB_FOREACH (nhgc
, nhgc_entry_head
, &nhgc_entries
) {
609 struct listnode
*node
;
613 for (ALL_LIST_ELEMENTS_RO(nhgc
->nhg_list
, node
, nhh
)) {
616 if (!nexthop_group_parse_nexthop(
617 &nhop
, nhh
->addr
, nhh
->intf
,
622 case NEXTHOP_TYPE_IPV4
:
623 case NEXTHOP_TYPE_IPV6
:
624 case NEXTHOP_TYPE_BLACKHOLE
:
626 case NEXTHOP_TYPE_IFINDEX
:
627 case NEXTHOP_TYPE_IPV4_IFINDEX
:
628 case NEXTHOP_TYPE_IPV6_IFINDEX
:
631 nh
= nexthop_exists(&nhgc
->nhg
, &nhop
);
636 if (ifp
->ifindex
!= nhop
.ifindex
)
641 memcpy(nh
, &nhop
, sizeof(nhop
));
642 nexthop_add(&nhgc
->nhg
.nexthop
, nh
);
644 if (nhg_hooks
.add_nexthop
)
645 nhg_hooks
.add_nexthop(nhgc
, nh
);
648 struct nexthop
*next_nh
;
650 for (nh
= nhgc
->nhg
.nexthop
; nh
; nh
= next_nh
) {
653 case NEXTHOP_TYPE_IPV4
:
654 case NEXTHOP_TYPE_IPV6
:
655 case NEXTHOP_TYPE_BLACKHOLE
:
657 case NEXTHOP_TYPE_IFINDEX
:
658 case NEXTHOP_TYPE_IPV4_IFINDEX
:
659 case NEXTHOP_TYPE_IPV6_IFINDEX
:
663 if (oldifindex
!= nh
->ifindex
)
666 nexthop_del(&nhgc
->nhg
, nh
);
668 if (nhg_hooks
.del_nexthop
)
669 nhg_hooks
.del_nexthop(nhgc
, nh
);
677 void nexthop_group_init(void (*new)(const char *name
),
678 void (*add_nexthop
)(const struct nexthop_group_cmd
*nhg
,
679 const struct nexthop
*nhop
),
680 void (*del_nexthop
)(const struct nexthop_group_cmd
*nhg
,
681 const struct nexthop
*nhop
),
682 void (*delete)(const char *name
))
684 RB_INIT(nhgc_entry_head
, &nhgc_entries
);
686 install_node(&nexthop_group_node
, nexthop_group_write
);
687 install_element(CONFIG_NODE
, &nexthop_group_cmd
);
688 install_element(CONFIG_NODE
, &no_nexthop_group_cmd
);
690 install_default(NH_GROUP_NODE
);
691 install_element(NH_GROUP_NODE
, &ecmp_nexthops_cmd
);
693 memset(&nhg_hooks
, 0, sizeof(nhg_hooks
));
698 nhg_hooks
.add_nexthop
= add_nexthop
;
700 nhg_hooks
.del_nexthop
= del_nexthop
;
702 nhg_hooks
.delete = delete;