3 * Copyright (C) 1997, 98 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2, or (at your
10 * option) any later version.
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "lib_errors.h"
31 #include "sockunion.h"
37 #include "northbound_cli.h"
38 #include "lib/if_clippy.c"
40 DEFINE_MTYPE_STATIC(LIB
, IF
, "Interface");
41 DEFINE_MTYPE_STATIC(LIB
, CONNECTED
, "Connected");
42 DEFINE_MTYPE_STATIC(LIB
, NBR_CONNECTED
, "Neighbor Connected");
43 DEFINE_MTYPE(LIB
, CONNECTED_LABEL
, "Connected interface label");
44 DEFINE_MTYPE_STATIC(LIB
, IF_LINK_PARAMS
, "Informational Link Parameters");
46 static void if_set_name(struct interface
*ifp
, const char *name
);
47 static struct interface
*if_lookup_by_ifindex(ifindex_t ifindex
,
49 static struct interface
*if_lookup_by_index_all_vrf(ifindex_t ifindex
);
50 static int if_cmp_func(const struct interface
*, const struct interface
*);
51 static int if_cmp_index_func(const struct interface
*ifp1
,
52 const struct interface
*ifp2
);
53 RB_GENERATE(if_name_head
, interface
, name_entry
, if_cmp_func
);
54 RB_GENERATE(if_index_head
, interface
, index_entry
, if_cmp_index_func
);
56 DEFINE_QOBJ_TYPE(interface
);
58 DEFINE_HOOK(if_add
, (struct interface
* ifp
), (ifp
));
59 DEFINE_KOOH(if_del
, (struct interface
* ifp
), (ifp
));
61 static struct interface_master
{
62 int (*create_hook
)(struct interface
*ifp
);
63 int (*up_hook
)(struct interface
*ifp
);
64 int (*down_hook
)(struct interface
*ifp
);
65 int (*destroy_hook
)(struct interface
*ifp
);
66 } ifp_master
= { 0, };
68 /* Compare interface names, returning an integer greater than, equal to, or
69 * less than 0, (following the strcmp convention), according to the
70 * relationship between ifp1 and ifp2. Interface names consist of an
71 * alphabetic prefix and a numeric suffix. The primary sort key is
72 * lexicographic by name, and then numeric by number. No number sorts
73 * before all numbers. Examples: de0 < de1, de100 < fxp0 < xl0, devpty <
76 int if_cmp_name_func(const char *p1
, const char *p2
)
85 /* look up to any number */
86 l1
= strcspn(p1
, "0123456789");
87 l2
= strcspn(p2
, "0123456789");
89 /* name lengths are different -> compare names */
91 return (strcmp(p1
, p2
));
93 /* Note that this relies on all numbers being less than all
97 res
= strncmp(p1
, p2
, l1
);
99 /* names are different -> compare them */
103 /* with identical name part, go to numeric part */
114 x1
= strtol(p1
, (char **)&tmp1
, 10);
115 x2
= strtol(p2
, (char **)&tmp2
, 10);
117 /* let's compare numbers now */
123 /* Compare string if numbers are equal (distinguish foo-1 from foo-001) */
124 l1
= strspn(p1
, "0123456789");
125 l2
= strspn(p2
, "0123456789");
127 return (strcmp(p1
, p2
));
129 /* Continue to parse the rest of the string */
130 p1
= (const char *)tmp1
;
131 p2
= (const char *)tmp2
;
133 /* numbers were equal, lets do it again..
134 (it happens with name like "eth123.456:789") */
143 static int if_cmp_func(const struct interface
*ifp1
,
144 const struct interface
*ifp2
)
146 return if_cmp_name_func(ifp1
->name
, ifp2
->name
);
149 static int if_cmp_index_func(const struct interface
*ifp1
,
150 const struct interface
*ifp2
)
152 if (ifp1
->ifindex
== ifp2
->ifindex
)
154 else if (ifp1
->ifindex
> ifp2
->ifindex
)
160 static void ifp_connected_free(void *arg
)
162 struct connected
*c
= arg
;
167 /* Create new interface structure. */
168 static struct interface
*if_new(struct vrf
*vrf
)
170 struct interface
*ifp
;
174 ifp
= XCALLOC(MTYPE_IF
, sizeof(struct interface
));
176 ifp
->ifindex
= IFINDEX_INTERNAL
;
181 ifp
->connected
= list_new();
182 ifp
->connected
->del
= ifp_connected_free
;
184 ifp
->nbr_connected
= list_new();
185 ifp
->nbr_connected
->del
= (void (*)(void *))nbr_connected_free
;
187 /* Enable Link-detection by default */
188 SET_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
);
190 QOBJ_REG(ifp
, interface
);
194 void if_new_via_zapi(struct interface
*ifp
)
196 if (ifp_master
.create_hook
)
197 (*ifp_master
.create_hook
)(ifp
);
200 void if_destroy_via_zapi(struct interface
*ifp
)
202 if (ifp_master
.destroy_hook
)
203 (*ifp_master
.destroy_hook
)(ifp
);
205 ifp
->oldifindex
= ifp
->ifindex
;
206 if_set_index(ifp
, IFINDEX_INTERNAL
);
208 if (!ifp
->configured
)
212 void if_up_via_zapi(struct interface
*ifp
)
214 if (ifp_master
.up_hook
)
215 (*ifp_master
.up_hook
)(ifp
);
218 void if_down_via_zapi(struct interface
*ifp
)
220 if (ifp_master
.down_hook
)
221 (*ifp_master
.down_hook
)(ifp
);
224 static struct interface
*if_create_name(const char *name
, struct vrf
*vrf
)
226 struct interface
*ifp
;
230 if_set_name(ifp
, name
);
232 hook_call(if_add
, ifp
);
236 /* Create new interface structure. */
237 void if_update_to_new_vrf(struct interface
*ifp
, vrf_id_t vrf_id
)
239 struct vrf
*old_vrf
, *vrf
;
241 /* remove interface from old master vrf list */
244 if (ifp
->name
[0] != '\0')
245 IFNAME_RB_REMOVE(old_vrf
, ifp
);
247 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
248 IFINDEX_RB_REMOVE(old_vrf
, ifp
);
250 vrf
= vrf_get(vrf_id
, NULL
);
253 if (ifp
->name
[0] != '\0')
254 IFNAME_RB_INSERT(vrf
, ifp
);
256 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
257 IFINDEX_RB_INSERT(vrf
, ifp
);
261 /* Delete interface structure. */
262 void if_delete_retain(struct interface
*ifp
)
264 hook_call(if_del
, ifp
);
267 /* Free connected address list */
268 list_delete_all_node(ifp
->connected
);
270 /* Free connected nbr address list */
271 list_delete_all_node(ifp
->nbr_connected
);
274 /* Delete and free interface structure. */
275 void if_delete(struct interface
**ifp
)
277 struct interface
*ptr
= *ifp
;
278 struct vrf
*vrf
= ptr
->vrf
;
280 IFNAME_RB_REMOVE(vrf
, ptr
);
281 if (ptr
->ifindex
!= IFINDEX_INTERNAL
)
282 IFINDEX_RB_REMOVE(vrf
, ptr
);
284 if_delete_retain(ptr
);
286 list_delete(&ptr
->connected
);
287 list_delete(&ptr
->nbr_connected
);
289 if_link_params_free(ptr
);
291 XFREE(MTYPE_TMP
, ptr
->desc
);
293 XFREE(MTYPE_IF
, ptr
);
297 /* Used only internally to check within VRF only */
298 static struct interface
*if_lookup_by_ifindex(ifindex_t ifindex
,
302 struct interface if_tmp
;
304 vrf
= vrf_lookup_by_id(vrf_id
);
308 if_tmp
.ifindex
= ifindex
;
309 return RB_FIND(if_index_head
, &vrf
->ifaces_by_index
, &if_tmp
);
312 /* Interface existence check by index. */
313 struct interface
*if_lookup_by_index(ifindex_t ifindex
, vrf_id_t vrf_id
)
315 switch (vrf_get_backend()) {
316 case VRF_BACKEND_UNKNOWN
:
317 case VRF_BACKEND_NETNS
:
318 return(if_lookup_by_ifindex(ifindex
, vrf_id
));
319 case VRF_BACKEND_VRF_LITE
:
320 return(if_lookup_by_index_all_vrf(ifindex
));
325 /* Interface existence check by index. */
326 struct interface
*if_vrf_lookup_by_index_next(ifindex_t ifindex
,
329 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
330 struct interface
*tmp_ifp
;
337 tmp_ifp
= RB_MIN(if_index_head
, &vrf
->ifaces_by_index
);
338 /* skip the vrf interface */
339 if (tmp_ifp
&& if_is_vrf(tmp_ifp
))
340 ifindex
= tmp_ifp
->ifindex
;
345 RB_FOREACH (tmp_ifp
, if_index_head
, &vrf
->ifaces_by_index
) {
347 /* skip the vrf interface */
348 if (tmp_ifp
&& if_is_vrf(tmp_ifp
))
353 if (tmp_ifp
->ifindex
== ifindex
)
359 const char *ifindex2ifname(ifindex_t ifindex
, vrf_id_t vrf_id
)
361 struct interface
*ifp
;
363 return ((ifp
= if_lookup_by_index(ifindex
, vrf_id
)) != NULL
)
368 ifindex_t
ifname2ifindex(const char *name
, vrf_id_t vrf_id
)
370 struct interface
*ifp
;
372 return ((ifp
= if_lookup_by_name(name
, vrf_id
)) != NULL
)
377 /* Interface existence check by interface name. */
378 struct interface
*if_lookup_by_name(const char *name
, vrf_id_t vrf_id
)
380 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
381 struct interface if_tmp
;
384 || strnlen(name
, INTERFACE_NAMSIZ
) == INTERFACE_NAMSIZ
)
387 strlcpy(if_tmp
.name
, name
, sizeof(if_tmp
.name
));
388 return RB_FIND(if_name_head
, &vrf
->ifaces_by_name
, &if_tmp
);
391 struct interface
*if_lookup_by_name_vrf(const char *name
, struct vrf
*vrf
)
393 struct interface if_tmp
;
395 if (!name
|| strnlen(name
, INTERFACE_NAMSIZ
) == INTERFACE_NAMSIZ
)
398 strlcpy(if_tmp
.name
, name
, sizeof(if_tmp
.name
));
399 return RB_FIND(if_name_head
, &vrf
->ifaces_by_name
, &if_tmp
);
402 static struct interface
*if_lookup_by_name_all_vrf(const char *name
)
405 struct interface
*ifp
;
407 if (!name
|| strnlen(name
, INTERFACE_NAMSIZ
) == INTERFACE_NAMSIZ
)
410 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
411 ifp
= if_lookup_by_name_vrf(name
, vrf
);
419 static struct interface
*if_lookup_by_index_all_vrf(ifindex_t ifindex
)
422 struct interface
*ifp
;
424 if (ifindex
== IFINDEX_INTERNAL
)
427 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
428 ifp
= if_lookup_by_ifindex(ifindex
, vrf
->vrf_id
);
436 /* Lookup interface by IP address.
438 * supersedes if_lookup_exact_address(), which didn't care about up/down
439 * state. but all users we have either only care if the address is local
440 * (=> use if_address_is_local() please), or care about UP interfaces before
443 * to accept only UP interfaces, check if_is_up() on the returned ifp.
445 struct interface
*if_lookup_address_local(const void *src
, int family
,
448 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
449 struct listnode
*cnode
;
450 struct interface
*ifp
, *best_down
= NULL
;
454 if (family
!= AF_INET
&& family
!= AF_INET6
)
457 FOR_ALL_INTERFACES (vrf
, ifp
) {
458 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
461 if (!p
|| p
->family
!= family
)
464 if (family
== AF_INET
) {
465 if (!IPV4_ADDR_SAME(&p
->u
.prefix4
,
466 (struct in_addr
*)src
))
468 } else if (family
== AF_INET6
) {
469 if (!IPV6_ADDR_SAME(&p
->u
.prefix6
,
470 (struct in6_addr
*)src
))
483 /* Lookup interface by IP address. */
484 struct connected
*if_lookup_address(const void *matchaddr
, int family
,
487 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
490 struct listnode
*cnode
;
491 struct interface
*ifp
;
493 struct connected
*match
;
495 if (family
== AF_INET
) {
496 addr
.family
= AF_INET
;
497 addr
.u
.prefix4
= *((struct in_addr
*)matchaddr
);
498 addr
.prefixlen
= IPV4_MAX_BITLEN
;
499 } else if (family
== AF_INET6
) {
500 addr
.family
= AF_INET6
;
501 addr
.u
.prefix6
= *((struct in6_addr
*)matchaddr
);
502 addr
.prefixlen
= IPV6_MAX_BITLEN
;
504 assert(!"Attempted lookup of family not supported");
508 FOR_ALL_INTERFACES (vrf
, ifp
) {
509 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
510 if (c
->address
&& (c
->address
->family
== AF_INET
)
511 && prefix_match(CONNECTED_PREFIX(c
), &addr
)
512 && (c
->address
->prefixlen
> bestlen
)) {
513 bestlen
= c
->address
->prefixlen
;
521 /* Lookup interface by prefix */
522 struct interface
*if_lookup_prefix(const struct prefix
*prefix
, vrf_id_t vrf_id
)
524 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
525 struct listnode
*cnode
;
526 struct interface
*ifp
;
529 FOR_ALL_INTERFACES (vrf
, ifp
) {
530 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
531 if (prefix_cmp(c
->address
, prefix
) == 0) {
539 size_t if_lookup_by_hwaddr(const uint8_t *hw_addr
, size_t addrsz
,
540 struct interface
***result
, vrf_id_t vrf_id
)
542 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
544 struct list
*rs
= list_new();
545 struct interface
*ifp
;
547 FOR_ALL_INTERFACES (vrf
, ifp
) {
548 if (ifp
->hw_addr_len
== (int)addrsz
549 && !memcmp(hw_addr
, ifp
->hw_addr
, addrsz
))
550 listnode_add(rs
, ifp
);
554 *result
= XCALLOC(MTYPE_TMP
,
555 sizeof(struct interface
*) * rs
->count
);
556 list_to_array(rs
, (void **)*result
, rs
->count
);
559 int count
= rs
->count
;
567 /* Get interface by name if given name interface doesn't exist create
569 struct interface
*if_get_by_name(const char *name
, vrf_id_t vrf_id
,
570 const char *vrf_name
)
572 struct interface
*ifp
= NULL
;
575 switch (vrf_get_backend()) {
576 case VRF_BACKEND_UNKNOWN
:
577 case VRF_BACKEND_NETNS
:
578 vrf
= vrf_get(vrf_id
, vrf_name
);
581 ifp
= if_lookup_by_name_vrf(name
, vrf
);
583 /* If it came from the kernel or by way of zclient,
584 * believe it and update the ifp accordingly.
586 if (ifp
->vrf
->vrf_id
!= vrf_id
&& vrf_id
!= VRF_UNKNOWN
)
587 if_update_to_new_vrf(ifp
, vrf_id
);
593 case VRF_BACKEND_VRF_LITE
:
594 ifp
= if_lookup_by_name_all_vrf(name
);
596 /* If it came from the kernel or by way of zclient,
597 * believe it and update the ifp accordingly.
599 if (ifp
->vrf
->vrf_id
!= vrf_id
&& vrf_id
!= VRF_UNKNOWN
)
600 if_update_to_new_vrf(ifp
, vrf_id
);
605 vrf
= vrf_get(vrf_id
, vrf_name
);
613 return if_create_name(name
, vrf
);
616 int if_set_index(struct interface
*ifp
, ifindex_t ifindex
)
618 if (ifp
->ifindex
== ifindex
)
622 * If there is already an interface with this ifindex, we will collide
623 * on insertion, so don't even try.
625 if (if_lookup_by_ifindex(ifindex
, ifp
->vrf
->vrf_id
))
628 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
629 IFINDEX_RB_REMOVE(ifp
->vrf
, ifp
);
631 ifp
->ifindex
= ifindex
;
633 if (ifp
->ifindex
!= IFINDEX_INTERNAL
) {
635 * This should never happen, since we checked if there was
636 * already an interface with the desired ifindex at the top of
637 * the function. Nevertheless.
639 if (IFINDEX_RB_INSERT(ifp
->vrf
, ifp
))
646 static void if_set_name(struct interface
*ifp
, const char *name
)
648 if (if_cmp_name_func(ifp
->name
, name
) == 0)
651 if (ifp
->name
[0] != '\0')
652 IFNAME_RB_REMOVE(ifp
->vrf
, ifp
);
654 strlcpy(ifp
->name
, name
, sizeof(ifp
->name
));
656 if (ifp
->name
[0] != '\0')
657 IFNAME_RB_INSERT(ifp
->vrf
, ifp
);
660 /* Does interface up ? */
661 int if_is_up(const struct interface
*ifp
)
663 return ifp
->flags
& IFF_UP
;
666 /* Is interface running? */
667 int if_is_running(const struct interface
*ifp
)
669 return ifp
->flags
& IFF_RUNNING
;
672 /* Is the interface operative, eg. either UP & RUNNING
673 or UP & !ZEBRA_INTERFACE_LINK_DETECTION and
674 if ptm checking is enabled, then ptm check has passed */
675 int if_is_operative(const struct interface
*ifp
)
677 return ((ifp
->flags
& IFF_UP
)
678 && (((ifp
->flags
& IFF_RUNNING
)
679 && (ifp
->ptm_status
|| !ifp
->ptm_enable
))
680 || !CHECK_FLAG(ifp
->status
,
681 ZEBRA_INTERFACE_LINKDETECTION
)));
684 /* Is the interface operative, eg. either UP & RUNNING
685 or UP & !ZEBRA_INTERFACE_LINK_DETECTION, without PTM check */
686 int if_is_no_ptm_operative(const struct interface
*ifp
)
688 return ((ifp
->flags
& IFF_UP
)
689 && ((ifp
->flags
& IFF_RUNNING
)
690 || !CHECK_FLAG(ifp
->status
,
691 ZEBRA_INTERFACE_LINKDETECTION
)));
694 /* Is this loopback interface ? */
695 int if_is_loopback_exact(const struct interface
*ifp
)
697 /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M
698 * but Y on platform N?
700 return (ifp
->flags
& (IFF_LOOPBACK
| IFF_NOXMIT
| IFF_VIRTUAL
));
703 /* Check interface is VRF */
704 int if_is_vrf(const struct interface
*ifp
)
706 return CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
709 /* Should this interface be treated as a loopback? */
710 bool if_is_loopback(const struct interface
*ifp
)
712 if (if_is_loopback_exact(ifp
) || if_is_vrf(ifp
))
718 /* Does this interface support broadcast ? */
719 int if_is_broadcast(const struct interface
*ifp
)
721 return ifp
->flags
& IFF_BROADCAST
;
724 /* Does this interface support pointopoint ? */
725 int if_is_pointopoint(const struct interface
*ifp
)
727 return ifp
->flags
& IFF_POINTOPOINT
;
730 /* Does this interface support multicast ? */
731 int if_is_multicast(const struct interface
*ifp
)
733 return ifp
->flags
& IFF_MULTICAST
;
736 /* Printout flag information into log */
737 const char *if_flag_dump(unsigned long flag
)
740 static char logbuf
[BUFSIZ
];
742 #define IFF_OUT_LOG(X, STR) \
745 strlcat(logbuf, ",", sizeof(logbuf)); \
748 strlcat(logbuf, STR, sizeof(logbuf)); \
751 strlcpy(logbuf
, "<", BUFSIZ
);
752 IFF_OUT_LOG(IFF_UP
, "UP");
753 IFF_OUT_LOG(IFF_BROADCAST
, "BROADCAST");
754 IFF_OUT_LOG(IFF_DEBUG
, "DEBUG");
755 IFF_OUT_LOG(IFF_LOOPBACK
, "LOOPBACK");
756 IFF_OUT_LOG(IFF_POINTOPOINT
, "POINTOPOINT");
757 IFF_OUT_LOG(IFF_NOTRAILERS
, "NOTRAILERS");
758 IFF_OUT_LOG(IFF_RUNNING
, "RUNNING");
759 IFF_OUT_LOG(IFF_NOARP
, "NOARP");
760 IFF_OUT_LOG(IFF_PROMISC
, "PROMISC");
761 IFF_OUT_LOG(IFF_ALLMULTI
, "ALLMULTI");
762 IFF_OUT_LOG(IFF_OACTIVE
, "OACTIVE");
763 IFF_OUT_LOG(IFF_SIMPLEX
, "SIMPLEX");
764 IFF_OUT_LOG(IFF_LINK0
, "LINK0");
765 IFF_OUT_LOG(IFF_LINK1
, "LINK1");
766 IFF_OUT_LOG(IFF_LINK2
, "LINK2");
767 IFF_OUT_LOG(IFF_MULTICAST
, "MULTICAST");
768 IFF_OUT_LOG(IFF_NOXMIT
, "NOXMIT");
769 IFF_OUT_LOG(IFF_NORTEXCH
, "NORTEXCH");
770 IFF_OUT_LOG(IFF_VIRTUAL
, "VIRTUAL");
771 IFF_OUT_LOG(IFF_IPV4
, "IPv4");
772 IFF_OUT_LOG(IFF_IPV6
, "IPv6");
774 strlcat(logbuf
, ">", sizeof(logbuf
));
781 static void if_dump(const struct interface
*ifp
)
783 struct listnode
*node
;
784 struct connected
*c
__attribute__((unused
));
786 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, c
))
788 "Interface %s vrf %s(%u) index %d metric %d mtu %d mtu6 %d %s",
789 ifp
->name
, ifp
->vrf
->name
, ifp
->vrf
->vrf_id
,
790 ifp
->ifindex
, ifp
->metric
, ifp
->mtu
, ifp
->mtu6
,
791 if_flag_dump(ifp
->flags
));
794 /* Interface printing for all interface. */
795 void if_dump_all(void)
800 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
801 FOR_ALL_INTERFACES (vrf
, ifp
)
805 /* Allocate connected structure. */
806 struct connected
*connected_new(void)
808 return XCALLOC(MTYPE_CONNECTED
, sizeof(struct connected
));
811 /* Allocate nbr connected structure. */
812 struct nbr_connected
*nbr_connected_new(void)
814 return XCALLOC(MTYPE_NBR_CONNECTED
, sizeof(struct nbr_connected
));
817 /* Free connected structure. */
818 void connected_free(struct connected
**connected
)
820 struct connected
*ptr
= *connected
;
822 prefix_free(&ptr
->address
);
823 prefix_free(&ptr
->destination
);
825 XFREE(MTYPE_CONNECTED_LABEL
, ptr
->label
);
827 XFREE(MTYPE_CONNECTED
, ptr
);
831 /* Free nbr connected structure. */
832 void nbr_connected_free(struct nbr_connected
*connected
)
834 if (connected
->address
)
835 prefix_free(&connected
->address
);
837 XFREE(MTYPE_NBR_CONNECTED
, connected
);
840 /* If same interface nbr address already exists... */
841 struct nbr_connected
*nbr_connected_check(struct interface
*ifp
,
844 struct nbr_connected
*ifc
;
845 struct listnode
*node
;
847 for (ALL_LIST_ELEMENTS_RO(ifp
->nbr_connected
, node
, ifc
))
848 if (prefix_same(ifc
->address
, p
))
854 /* Print if_addr structure. */
855 static void __attribute__((unused
))
856 connected_log(struct connected
*connected
, char *str
)
859 struct interface
*ifp
;
863 ifp
= connected
->ifp
;
864 p
= connected
->address
;
866 snprintf(logbuf
, sizeof(logbuf
), "%s interface %s vrf %s(%u) %s %pFX ",
867 str
, ifp
->name
, ifp
->vrf
->name
, ifp
->vrf
->vrf_id
,
868 prefix_family_str(p
), p
);
870 p
= connected
->destination
;
872 strlcat(logbuf
, inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
875 zlog_info("%s", logbuf
);
878 /* Print if_addr structure. */
879 static void __attribute__((unused
))
880 nbr_connected_log(struct nbr_connected
*connected
, char *str
)
883 struct interface
*ifp
;
886 ifp
= connected
->ifp
;
887 p
= connected
->address
;
889 snprintf(logbuf
, sizeof(logbuf
), "%s interface %s %s %pFX ", str
,
890 ifp
->name
, prefix_family_str(p
), p
);
892 zlog_info("%s", logbuf
);
895 /* If two connected address has same prefix return 1. */
896 static int connected_same_prefix(const struct prefix
*p1
,
897 const struct prefix
*p2
)
899 if (p1
->family
== p2
->family
) {
900 if (p1
->family
== AF_INET
901 && IPV4_ADDR_SAME(&p1
->u
.prefix4
, &p2
->u
.prefix4
))
903 if (p1
->family
== AF_INET6
904 && IPV6_ADDR_SAME(&p1
->u
.prefix6
, &p2
->u
.prefix6
))
910 /* count the number of connected addresses that are in the given family */
911 unsigned int connected_count_by_family(struct interface
*ifp
, int family
)
913 struct listnode
*cnode
;
914 struct connected
*connected
;
915 unsigned int cnt
= 0;
917 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
))
918 if (connected
->address
->family
== family
)
924 struct connected
*connected_lookup_prefix_exact(struct interface
*ifp
,
925 const struct prefix
*p
)
927 struct listnode
*node
;
928 struct listnode
*next
;
929 struct connected
*ifc
;
931 for (node
= listhead(ifp
->connected
); node
; node
= next
) {
932 ifc
= listgetdata(node
);
935 if (connected_same_prefix(ifc
->address
, p
))
941 struct connected
*connected_delete_by_prefix(struct interface
*ifp
,
944 struct listnode
*node
;
945 struct listnode
*next
;
946 struct connected
*ifc
;
948 /* In case of same prefix come, replace it with new one. */
949 for (node
= listhead(ifp
->connected
); node
; node
= next
) {
950 ifc
= listgetdata(node
);
953 if (connected_same_prefix(ifc
->address
, p
)) {
954 listnode_delete(ifp
->connected
, ifc
);
961 /* Find the address on our side that will be used when packets
963 struct connected
*connected_lookup_prefix(struct interface
*ifp
,
964 const struct prefix
*addr
)
966 struct listnode
*cnode
;
968 struct connected
*match
;
972 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
973 if (c
->address
&& (c
->address
->family
== addr
->family
)
974 && prefix_match(CONNECTED_PREFIX(c
), addr
)
976 || (c
->address
->prefixlen
> match
->address
->prefixlen
)))
982 struct connected
*connected_add_by_prefix(struct interface
*ifp
,
984 struct prefix
*destination
)
986 struct connected
*ifc
;
988 /* Allocate new connected address. */
989 ifc
= connected_new();
992 /* Fetch interface address */
993 ifc
->address
= prefix_new();
994 memcpy(ifc
->address
, p
, sizeof(struct prefix
));
996 /* Fetch dest address */
998 ifc
->destination
= prefix_new();
999 memcpy(ifc
->destination
, destination
, sizeof(struct prefix
));
1002 /* Add connected address to the interface. */
1003 listnode_add(ifp
->connected
, ifc
);
1007 struct connected
*connected_get_linklocal(struct interface
*ifp
)
1010 struct connected
*c
= NULL
;
1012 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, n
, c
)) {
1013 if (c
->address
->family
== AF_INET6
1014 && IN6_IS_ADDR_LINKLOCAL(&c
->address
->u
.prefix6
))
1020 void if_terminate(struct vrf
*vrf
)
1022 struct interface
*ifp
;
1024 while (!RB_EMPTY(if_name_head
, &vrf
->ifaces_by_name
)) {
1025 ifp
= RB_ROOT(if_name_head
, &vrf
->ifaces_by_name
);
1028 ifp
->node
->info
= NULL
;
1029 route_unlock_node(ifp
->node
);
1035 const char *if_link_type_str(enum zebra_link_type llt
)
1038 #define llts(T,S) case (T): return (S)
1039 llts(ZEBRA_LLT_UNKNOWN
, "Unknown");
1040 llts(ZEBRA_LLT_ETHER
, "Ethernet");
1041 llts(ZEBRA_LLT_EETHER
, "Experimental Ethernet");
1042 llts(ZEBRA_LLT_AX25
, "AX.25 Level 2");
1043 llts(ZEBRA_LLT_PRONET
, "PROnet token ring");
1044 llts(ZEBRA_LLT_IEEE802
, "IEEE 802.2 Ethernet/TR/TB");
1045 llts(ZEBRA_LLT_ARCNET
, "ARCnet");
1046 llts(ZEBRA_LLT_APPLETLK
, "AppleTalk");
1047 llts(ZEBRA_LLT_DLCI
, "Frame Relay DLCI");
1048 llts(ZEBRA_LLT_ATM
, "ATM");
1049 llts(ZEBRA_LLT_METRICOM
, "Metricom STRIP");
1050 llts(ZEBRA_LLT_IEEE1394
, "IEEE 1394 IPv4");
1051 llts(ZEBRA_LLT_EUI64
, "EUI-64");
1052 llts(ZEBRA_LLT_INFINIBAND
, "InfiniBand");
1053 llts(ZEBRA_LLT_SLIP
, "SLIP");
1054 llts(ZEBRA_LLT_CSLIP
, "Compressed SLIP");
1055 llts(ZEBRA_LLT_SLIP6
, "SLIPv6");
1056 llts(ZEBRA_LLT_CSLIP6
, "Compressed SLIPv6");
1057 llts(ZEBRA_LLT_RSRVD
, "Reserved");
1058 llts(ZEBRA_LLT_ADAPT
, "Adapt");
1059 llts(ZEBRA_LLT_ROSE
, "ROSE packet radio");
1060 llts(ZEBRA_LLT_X25
, "CCITT X.25");
1061 llts(ZEBRA_LLT_PPP
, "PPP");
1062 llts(ZEBRA_LLT_CHDLC
, "Cisco HDLC");
1063 llts(ZEBRA_LLT_RAWHDLC
, "Raw HDLC");
1064 llts(ZEBRA_LLT_LAPB
, "LAPB");
1065 llts(ZEBRA_LLT_IPIP
, "IPIP Tunnel");
1066 llts(ZEBRA_LLT_IPIP6
, "IPIP6 Tunnel");
1067 llts(ZEBRA_LLT_FRAD
, "FRAD");
1068 llts(ZEBRA_LLT_SKIP
, "SKIP vif");
1069 llts(ZEBRA_LLT_LOOPBACK
, "Loopback");
1070 llts(ZEBRA_LLT_LOCALTLK
, "Localtalk");
1071 llts(ZEBRA_LLT_FDDI
, "FDDI");
1072 llts(ZEBRA_LLT_SIT
, "IPv6-in-IPv4 SIT");
1073 llts(ZEBRA_LLT_IPDDP
, "IP-in-DDP tunnel");
1074 llts(ZEBRA_LLT_IPGRE
, "GRE over IP");
1075 llts(ZEBRA_LLT_IP6GRE
, "GRE over IPv6");
1076 llts(ZEBRA_LLT_PIMREG
, "PIMSM registration");
1077 llts(ZEBRA_LLT_HIPPI
, "HiPPI");
1078 llts(ZEBRA_LLT_ECONET
, "Acorn Econet");
1079 llts(ZEBRA_LLT_IRDA
, "IrDA");
1080 llts(ZEBRA_LLT_FCPP
, "Fibre-Channel PtP");
1081 llts(ZEBRA_LLT_FCAL
, "Fibre-Channel Arbitrated Loop");
1082 llts(ZEBRA_LLT_FCPL
, "Fibre-Channel Public Loop");
1083 llts(ZEBRA_LLT_FCFABRIC
, "Fibre-Channel Fabric");
1084 llts(ZEBRA_LLT_IEEE802_TR
, "IEEE 802.2 Token Ring");
1085 llts(ZEBRA_LLT_IEEE80211
, "IEEE 802.11");
1086 llts(ZEBRA_LLT_IEEE80211_RADIOTAP
, "IEEE 802.11 Radiotap");
1087 llts(ZEBRA_LLT_IEEE802154
, "IEEE 802.15.4");
1088 llts(ZEBRA_LLT_IEEE802154_PHY
, "IEEE 802.15.4 Phy");
1094 struct if_link_params
*if_link_params_get(struct interface
*ifp
)
1096 return ifp
->link_params
;
1099 struct if_link_params
*if_link_params_enable(struct interface
*ifp
)
1101 struct if_link_params
*iflp
;
1104 iflp
= if_link_params_init(ifp
);
1106 /* Compute default bandwidth based on interface */
1108 ((ifp
->bandwidth
? ifp
->bandwidth
: DEFAULT_BANDWIDTH
)
1109 * TE_MEGA_BIT
/ TE_BYTE
);
1111 /* Set Max, Reservable and Unreserved Bandwidth */
1112 iflp
->max_bw
= iflp
->default_bw
;
1113 iflp
->max_rsv_bw
= iflp
->default_bw
;
1114 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1115 iflp
->unrsv_bw
[i
] = iflp
->default_bw
;
1117 /* Update Link parameters status */
1118 iflp
->lp_status
= LP_MAX_BW
| LP_MAX_RSV_BW
| LP_UNRSV_BW
;
1120 /* Set TE metric equal to standard metric only if it is set */
1121 if (ifp
->metric
!= 0) {
1122 iflp
->te_metric
= ifp
->metric
;
1123 iflp
->lp_status
|= LP_TE_METRIC
;
1126 /* Finally attach newly created Link Parameters */
1127 ifp
->link_params
= iflp
;
1132 struct if_link_params
*if_link_params_init(struct interface
*ifp
)
1134 struct if_link_params
*iflp
= if_link_params_get(ifp
);
1139 iflp
= XCALLOC(MTYPE_IF_LINK_PARAMS
, sizeof(struct if_link_params
));
1141 ifp
->link_params
= iflp
;
1146 void if_link_params_free(struct interface
*ifp
)
1148 XFREE(MTYPE_IF_LINK_PARAMS
, ifp
->link_params
);
1151 /* ----------- CLI commands ----------- */
1153 /* Guess the VRF of an interface. */
1154 static int vrfname_by_ifname(const char *ifname
, const char **vrfname
)
1157 struct interface
*ifp
;
1160 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
1161 FOR_ALL_INTERFACES (vrf
, ifp
) {
1162 if (strmatch(ifp
->name
, ifname
)) {
1163 *vrfname
= vrf
->name
;
1173 * XPath: /frr-interface:lib/interface
1175 DEFPY_YANG_NOSH (interface
,
1177 "interface IFNAME [vrf NAME$vrf_name]",
1178 "Select an interface to configure\n"
1179 "Interface's name\n"
1182 char xpath_list
[XPATH_MAXLEN
];
1183 struct interface
*ifp
;
1187 if (vrf_is_backend_netns()) {
1189 * For backward compatibility, if the VRF name is not specified
1190 * and there is exactly one interface with this name in the
1191 * system, use its VRF. Otherwise fallback to the default VRF.
1194 count
= vrfname_by_ifname(ifname
, &vrf_name
);
1196 vrf_name
= VRF_DEFAULT_NAME
;
1199 snprintf(xpath_list
, XPATH_MAXLEN
,
1200 "/frr-interface:lib/interface[name='%s:%s']", vrf_name
,
1203 snprintf(xpath_list
, XPATH_MAXLEN
,
1204 "/frr-interface:lib/interface[name='%s']", ifname
);
1207 nb_cli_enqueue_change(vty
, ".", NB_OP_CREATE
, NULL
);
1208 ret
= nb_cli_apply_changes_clear_pending(vty
, xpath_list
);
1209 if (ret
== CMD_SUCCESS
) {
1210 VTY_PUSH_XPATH(INTERFACE_NODE
, xpath_list
);
1213 * For backward compatibility with old commands we still need
1214 * to use the qobj infrastructure. This can be removed once
1215 * all interface-level commands are converted to the new
1218 if (vrf_is_backend_netns()) {
1219 vrf
= vrf_lookup_by_name(vrf_name
);
1221 ifp
= if_lookup_by_name_vrf(ifname
, vrf
);
1225 ifp
= if_lookup_by_name_all_vrf(ifname
);
1228 VTY_PUSH_CONTEXT(INTERFACE_NODE
, ifp
);
1234 DEFPY_YANG (no_interface
,
1236 "no interface IFNAME [vrf NAME$vrf_name]",
1238 "Delete a pseudo interface's configuration\n"
1239 "Interface's name\n"
1242 char xpath_list
[XPATH_MAXLEN
];
1245 if (vrf_is_backend_netns()) {
1247 * For backward compatibility, if the VRF name is not specified
1248 * and there is exactly one interface with this name in the
1249 * system, use its VRF. Otherwise fallback to the default VRF.
1252 count
= vrfname_by_ifname(ifname
, &vrf_name
);
1254 vrf_name
= VRF_DEFAULT_NAME
;
1257 snprintf(xpath_list
, XPATH_MAXLEN
,
1258 "/frr-interface:lib/interface[name='%s:%s']", vrf_name
,
1261 snprintf(xpath_list
, XPATH_MAXLEN
,
1262 "/frr-interface:lib/interface[name='%s']", ifname
);
1265 nb_cli_enqueue_change(vty
, ".", NB_OP_DESTROY
, NULL
);
1267 return nb_cli_apply_changes(vty
, xpath_list
);
1270 static void netns_ifname_split(const char *xpath
, char *ifname
, char *vrfname
)
1275 assert(vrf_is_backend_netns());
1277 delim
= strchr(xpath
, ':');
1280 len
= delim
- xpath
;
1281 memcpy(vrfname
, xpath
, len
);
1284 strlcpy(ifname
, delim
+ 1, XPATH_MAXLEN
);
1287 static void cli_show_interface(struct vty
*vty
, const struct lyd_node
*dnode
,
1290 vty_out(vty
, "!\n");
1292 if (vrf_is_backend_netns()) {
1293 char ifname
[XPATH_MAXLEN
];
1294 char vrfname
[XPATH_MAXLEN
];
1296 netns_ifname_split(yang_dnode_get_string(dnode
, "./name"),
1299 vty_out(vty
, "interface %s", ifname
);
1300 if (!strmatch(vrfname
, VRF_DEFAULT_NAME
))
1301 vty_out(vty
, " vrf %s", vrfname
);
1303 const char *ifname
= yang_dnode_get_string(dnode
, "./name");
1305 vty_out(vty
, "interface %s", ifname
);
1311 static void cli_show_interface_end(struct vty
*vty
,
1312 const struct lyd_node
*dnode
)
1314 vty_out(vty
, "exit\n");
1317 void if_vty_config_start(struct vty
*vty
, struct interface
*ifp
)
1319 vty_frame(vty
, "!\n");
1320 vty_frame(vty
, "interface %s", ifp
->name
);
1322 if (vrf_is_backend_netns() && strcmp(ifp
->vrf
->name
, VRF_DEFAULT_NAME
))
1323 vty_frame(vty
, " vrf %s", ifp
->vrf
->name
);
1325 vty_frame(vty
, "\n");
1328 void if_vty_config_end(struct vty
*vty
)
1330 vty_endframe(vty
, "exit\n!\n");
1334 * XPath: /frr-interface:lib/interface/description
1336 DEFPY_YANG (interface_desc
,
1338 "description LINE...",
1339 "Interface specific description\n"
1340 "Characters describing this interface\n")
1345 desc
= argv_concat(argv
, argc
, 1);
1346 nb_cli_enqueue_change(vty
, "./description", NB_OP_MODIFY
, desc
);
1347 ret
= nb_cli_apply_changes(vty
, NULL
);
1348 XFREE(MTYPE_TMP
, desc
);
1353 DEFPY_YANG (no_interface_desc
,
1354 no_interface_desc_cmd
,
1357 "Interface specific description\n")
1359 nb_cli_enqueue_change(vty
, "./description", NB_OP_DESTROY
, NULL
);
1361 return nb_cli_apply_changes(vty
, NULL
);
1364 static void cli_show_interface_desc(struct vty
*vty
,
1365 const struct lyd_node
*dnode
,
1368 vty_out(vty
, " description %s\n", yang_dnode_get_string(dnode
, NULL
));
1371 /* Interface autocomplete. */
1372 static void if_autocomplete(vector comps
, struct cmd_token
*token
)
1374 struct interface
*ifp
;
1377 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
1378 FOR_ALL_INTERFACES (vrf
, ifp
) {
1379 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, ifp
->name
));
1384 static const struct cmd_variable_handler if_var_handlers
[] = {
1385 {/* "interface NAME" */
1386 .varname
= "interface",
1387 .completions
= if_autocomplete
},
1388 {.tokenname
= "IFNAME", .completions
= if_autocomplete
},
1389 {.tokenname
= "INTERFACE", .completions
= if_autocomplete
},
1390 {.completions
= NULL
}};
1392 static struct cmd_node interface_node
= {
1393 .name
= "interface",
1394 .node
= INTERFACE_NODE
,
1395 .parent_node
= CONFIG_NODE
,
1396 .prompt
= "%s(config-if)# ",
1399 static int if_config_write_single(const struct lyd_node
*dnode
, void *arg
)
1401 nb_cli_show_dnode_cmds(arg
, dnode
, false);
1403 return YANG_ITER_CONTINUE
;
1406 static int if_nb_config_write(struct vty
*vty
)
1408 yang_dnode_iterate(if_config_write_single
, vty
, running_config
->dnode
,
1409 "/frr-interface:lib/interface");
1413 void if_cmd_init(int (*config_write
)(struct vty
*))
1415 cmd_variable_handler_register(if_var_handlers
);
1417 interface_node
.config_write
= config_write
;
1418 install_node(&interface_node
);
1420 install_element(CONFIG_NODE
, &interface_cmd
);
1421 install_element(CONFIG_NODE
, &no_interface_cmd
);
1423 install_default(INTERFACE_NODE
);
1424 install_element(INTERFACE_NODE
, &interface_desc_cmd
);
1425 install_element(INTERFACE_NODE
, &no_interface_desc_cmd
);
1428 void if_cmd_init_default(void)
1430 if_cmd_init(if_nb_config_write
);
1433 void if_zapi_callbacks(int (*create
)(struct interface
*ifp
),
1434 int (*up
)(struct interface
*ifp
),
1435 int (*down
)(struct interface
*ifp
),
1436 int (*destroy
)(struct interface
*ifp
))
1438 ifp_master
.create_hook
= create
;
1439 ifp_master
.up_hook
= up
;
1440 ifp_master
.down_hook
= down
;
1441 ifp_master
.destroy_hook
= destroy
;
1444 /* ------- Northbound callbacks ------- */
1447 * XPath: /frr-interface:lib/interface
1449 static int lib_interface_create(struct nb_cb_create_args
*args
)
1452 struct interface
*ifp
;
1454 ifname
= yang_dnode_get_string(args
->dnode
, "./name");
1456 switch (args
->event
) {
1457 case NB_EV_VALIDATE
:
1458 if (vrf_is_backend_netns()) {
1459 char ifname_ns
[XPATH_MAXLEN
];
1460 char vrfname_ns
[XPATH_MAXLEN
];
1462 netns_ifname_split(ifname
, ifname_ns
, vrfname_ns
);
1464 if (strlen(ifname_ns
) > 16) {
1466 args
->errmsg
, args
->errmsg_len
,
1467 "Maximum interface name length is 16 characters");
1468 return NB_ERR_VALIDATION
;
1470 if (strlen(vrfname_ns
) > 36) {
1472 args
->errmsg
, args
->errmsg_len
,
1473 "Maximum VRF name length is 36 characters");
1474 return NB_ERR_VALIDATION
;
1477 if (strlen(ifname
) > 16) {
1479 args
->errmsg
, args
->errmsg_len
,
1480 "Maximum interface name length is 16 characters");
1481 return NB_ERR_VALIDATION
;
1489 if (vrf_is_backend_netns()) {
1490 char ifname_ns
[XPATH_MAXLEN
];
1491 char vrfname_ns
[XPATH_MAXLEN
];
1493 netns_ifname_split(ifname
, ifname_ns
, vrfname_ns
);
1495 ifp
= if_get_by_name(ifname_ns
, VRF_UNKNOWN
,
1498 ifp
= if_get_by_name(ifname
, VRF_UNKNOWN
,
1502 ifp
->configured
= true;
1503 nb_running_set_entry(args
->dnode
, ifp
);
1510 static int lib_interface_destroy(struct nb_cb_destroy_args
*args
)
1512 struct interface
*ifp
;
1515 switch (args
->event
) {
1516 case NB_EV_VALIDATE
:
1517 ifp
= nb_running_get_entry(args
->dnode
, NULL
, true);
1518 if (CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
)) {
1519 snprintf(args
->errmsg
, args
->errmsg_len
,
1520 "only inactive interfaces can be deleted");
1521 return NB_ERR_VALIDATION
;
1528 ifp
= nb_running_unset_entry(args
->dnode
);
1531 ifp
->configured
= false;
1534 if (!vrf_is_enabled(vrf
))
1543 * XPath: /frr-interface:lib/interface
1545 static const void *lib_interface_get_next(struct nb_cb_get_next_args
*args
)
1548 struct interface
*pif
= (struct interface
*)args
->list_entry
;
1550 if (args
->list_entry
== NULL
) {
1551 vrf
= RB_MIN(vrf_name_head
, &vrfs_by_name
);
1553 pif
= RB_MIN(if_name_head
, &vrf
->ifaces_by_name
);
1556 pif
= RB_NEXT(if_name_head
, pif
);
1557 /* if no more interfaces, switch to next vrf */
1558 while (pif
== NULL
) {
1559 vrf
= RB_NEXT(vrf_name_head
, vrf
);
1562 pif
= RB_MIN(if_name_head
, &vrf
->ifaces_by_name
);
1569 static int lib_interface_get_keys(struct nb_cb_get_keys_args
*args
)
1571 const struct interface
*ifp
= args
->list_entry
;
1573 args
->keys
->num
= 1;
1575 if (vrf_is_backend_netns())
1576 snprintf(args
->keys
->key
[0], sizeof(args
->keys
->key
[0]),
1577 "%s:%s", ifp
->vrf
->name
, ifp
->name
);
1579 snprintf(args
->keys
->key
[0], sizeof(args
->keys
->key
[0]), "%s",
1586 lib_interface_lookup_entry(struct nb_cb_lookup_entry_args
*args
)
1588 if (vrf_is_backend_netns()) {
1589 char ifname
[XPATH_MAXLEN
];
1590 char vrfname
[XPATH_MAXLEN
];
1593 netns_ifname_split(args
->keys
->key
[0], ifname
, vrfname
);
1595 vrf
= vrf_lookup_by_name(vrfname
);
1597 return vrf
? if_lookup_by_name(ifname
, vrf
->vrf_id
) : NULL
;
1599 return if_lookup_by_name_all_vrf(args
->keys
->key
[0]);
1604 * XPath: /frr-interface:lib/interface/description
1606 static int lib_interface_description_modify(struct nb_cb_modify_args
*args
)
1608 struct interface
*ifp
;
1609 const char *description
;
1611 if (args
->event
!= NB_EV_APPLY
)
1614 ifp
= nb_running_get_entry(args
->dnode
, NULL
, true);
1615 XFREE(MTYPE_TMP
, ifp
->desc
);
1616 description
= yang_dnode_get_string(args
->dnode
, NULL
);
1617 ifp
->desc
= XSTRDUP(MTYPE_TMP
, description
);
1622 static int lib_interface_description_destroy(struct nb_cb_destroy_args
*args
)
1624 struct interface
*ifp
;
1626 if (args
->event
!= NB_EV_APPLY
)
1629 ifp
= nb_running_get_entry(args
->dnode
, NULL
, true);
1630 XFREE(MTYPE_TMP
, ifp
->desc
);
1636 * XPath: /frr-interface:lib/interface/vrf
1638 static struct yang_data
*
1639 lib_interface_vrf_get_elem(struct nb_cb_get_elem_args
*args
)
1641 const struct interface
*ifp
= args
->list_entry
;
1643 return yang_data_new_string(args
->xpath
, ifp
->vrf
->name
);
1647 * XPath: /frr-interface:lib/interface/state/if-index
1649 static struct yang_data
*
1650 lib_interface_state_if_index_get_elem(struct nb_cb_get_elem_args
*args
)
1652 const struct interface
*ifp
= args
->list_entry
;
1654 return yang_data_new_int32(args
->xpath
, ifp
->ifindex
);
1658 * XPath: /frr-interface:lib/interface/state/mtu
1660 static struct yang_data
*
1661 lib_interface_state_mtu_get_elem(struct nb_cb_get_elem_args
*args
)
1663 const struct interface
*ifp
= args
->list_entry
;
1665 return yang_data_new_uint16(args
->xpath
, ifp
->mtu
);
1669 * XPath: /frr-interface:lib/interface/state/mtu6
1671 static struct yang_data
*
1672 lib_interface_state_mtu6_get_elem(struct nb_cb_get_elem_args
*args
)
1674 const struct interface
*ifp
= args
->list_entry
;
1676 return yang_data_new_uint32(args
->xpath
, ifp
->mtu6
);
1680 * XPath: /frr-interface:lib/interface/state/speed
1682 static struct yang_data
*
1683 lib_interface_state_speed_get_elem(struct nb_cb_get_elem_args
*args
)
1685 const struct interface
*ifp
= args
->list_entry
;
1687 return yang_data_new_uint32(args
->xpath
, ifp
->speed
);
1691 * XPath: /frr-interface:lib/interface/state/metric
1693 static struct yang_data
*
1694 lib_interface_state_metric_get_elem(struct nb_cb_get_elem_args
*args
)
1696 const struct interface
*ifp
= args
->list_entry
;
1698 return yang_data_new_uint32(args
->xpath
, ifp
->metric
);
1702 * XPath: /frr-interface:lib/interface/state/flags
1704 static struct yang_data
*
1705 lib_interface_state_flags_get_elem(struct nb_cb_get_elem_args
*args
)
1707 /* TODO: implement me. */
1712 * XPath: /frr-interface:lib/interface/state/type
1714 static struct yang_data
*
1715 lib_interface_state_type_get_elem(struct nb_cb_get_elem_args
*args
)
1717 /* TODO: implement me. */
1722 * XPath: /frr-interface:lib/interface/state/phy-address
1724 static struct yang_data
*
1725 lib_interface_state_phy_address_get_elem(struct nb_cb_get_elem_args
*args
)
1727 const struct interface
*ifp
= args
->list_entry
;
1728 struct ethaddr macaddr
;
1730 memcpy(&macaddr
.octet
, ifp
->hw_addr
, ETH_ALEN
);
1732 return yang_data_new_mac(args
->xpath
, &macaddr
);
1735 /* clang-format off */
1736 const struct frr_yang_module_info frr_interface_info
= {
1737 .name
= "frr-interface",
1740 .xpath
= "/frr-interface:lib/interface",
1742 .create
= lib_interface_create
,
1743 .destroy
= lib_interface_destroy
,
1744 .cli_show
= cli_show_interface
,
1745 .cli_show_end
= cli_show_interface_end
,
1746 .get_next
= lib_interface_get_next
,
1747 .get_keys
= lib_interface_get_keys
,
1748 .lookup_entry
= lib_interface_lookup_entry
,
1752 .xpath
= "/frr-interface:lib/interface/description",
1754 .modify
= lib_interface_description_modify
,
1755 .destroy
= lib_interface_description_destroy
,
1756 .cli_show
= cli_show_interface_desc
,
1760 .xpath
= "/frr-interface:lib/interface/vrf",
1762 .get_elem
= lib_interface_vrf_get_elem
,
1766 .xpath
= "/frr-interface:lib/interface/state/if-index",
1768 .get_elem
= lib_interface_state_if_index_get_elem
,
1772 .xpath
= "/frr-interface:lib/interface/state/mtu",
1774 .get_elem
= lib_interface_state_mtu_get_elem
,
1778 .xpath
= "/frr-interface:lib/interface/state/mtu6",
1780 .get_elem
= lib_interface_state_mtu6_get_elem
,
1784 .xpath
= "/frr-interface:lib/interface/state/speed",
1786 .get_elem
= lib_interface_state_speed_get_elem
,
1790 .xpath
= "/frr-interface:lib/interface/state/metric",
1792 .get_elem
= lib_interface_state_metric_get_elem
,
1796 .xpath
= "/frr-interface:lib/interface/state/flags",
1798 .get_elem
= lib_interface_state_flags_get_elem
,
1802 .xpath
= "/frr-interface:lib/interface/state/type",
1804 .get_elem
= lib_interface_state_type_get_elem
,
1808 .xpath
= "/frr-interface:lib/interface/state/phy-address",
1810 .get_elem
= lib_interface_state_phy_address_get_elem
,