1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) 1997, 98 Kunihiro Ishiguro
11 #include "lib_errors.h"
16 #include "sockunion.h"
22 #include "northbound_cli.h"
23 #include "admin_group.h"
24 #include "lib/if_clippy.c"
26 DEFINE_MTYPE_STATIC(LIB
, IF
, "Interface");
27 DEFINE_MTYPE_STATIC(LIB
, IFDESC
, "Intf Desc");
28 DEFINE_MTYPE_STATIC(LIB
, CONNECTED
, "Connected");
29 DEFINE_MTYPE_STATIC(LIB
, NBR_CONNECTED
, "Neighbor Connected");
30 DEFINE_MTYPE(LIB
, CONNECTED_LABEL
, "Connected interface label");
31 DEFINE_MTYPE_STATIC(LIB
, IF_LINK_PARAMS
, "Informational Link Parameters");
33 static void if_set_name(struct interface
*ifp
, const char *name
);
34 static struct interface
*if_lookup_by_ifindex(ifindex_t ifindex
,
36 static struct interface
*if_lookup_by_index_all_vrf(ifindex_t ifindex
);
37 static int if_cmp_func(const struct interface
*, const struct interface
*);
38 static int if_cmp_index_func(const struct interface
*ifp1
,
39 const struct interface
*ifp2
);
40 RB_GENERATE(if_name_head
, interface
, name_entry
, if_cmp_func
);
41 RB_GENERATE(if_index_head
, interface
, index_entry
, if_cmp_index_func
);
43 DEFINE_QOBJ_TYPE(interface
);
45 DEFINE_HOOK(if_add
, (struct interface
* ifp
), (ifp
));
46 DEFINE_KOOH(if_del
, (struct interface
* ifp
), (ifp
));
48 static struct interface_master
{
49 int (*create_hook
)(struct interface
*ifp
);
50 int (*up_hook
)(struct interface
*ifp
);
51 int (*down_hook
)(struct interface
*ifp
);
52 int (*destroy_hook
)(struct interface
*ifp
);
53 } ifp_master
= { 0, };
55 /* Compare interface names, returning an integer greater than, equal to, or
56 * less than 0, (following the strcmp convention), according to the
57 * relationship between ifp1 and ifp2. Interface names consist of an
58 * alphabetic prefix and a numeric suffix. The primary sort key is
59 * lexicographic by name, and then numeric by number. No number sorts
60 * before all numbers. Examples: de0 < de1, de100 < fxp0 < xl0, devpty <
63 int if_cmp_name_func(const char *p1
, const char *p2
)
72 /* look up to any number */
73 l1
= strcspn(p1
, "0123456789");
74 l2
= strcspn(p2
, "0123456789");
76 /* name lengths are different -> compare names */
78 return (strcmp(p1
, p2
));
80 /* Note that this relies on all numbers being less than all
84 res
= strncmp(p1
, p2
, l1
);
86 /* names are different -> compare them */
90 /* with identical name part, go to numeric part */
101 x1
= strtol(p1
, (char **)&tmp1
, 10);
102 x2
= strtol(p2
, (char **)&tmp2
, 10);
104 /* let's compare numbers now */
110 /* Compare string if numbers are equal (distinguish foo-1 from foo-001) */
111 l1
= strspn(p1
, "0123456789");
112 l2
= strspn(p2
, "0123456789");
114 return (strcmp(p1
, p2
));
116 /* Continue to parse the rest of the string */
117 p1
= (const char *)tmp1
;
118 p2
= (const char *)tmp2
;
120 /* numbers were equal, lets do it again..
121 (it happens with name like "eth123.456:789") */
130 static int if_cmp_func(const struct interface
*ifp1
,
131 const struct interface
*ifp2
)
133 return if_cmp_name_func(ifp1
->name
, ifp2
->name
);
136 static int if_cmp_index_func(const struct interface
*ifp1
,
137 const struct interface
*ifp2
)
139 if (ifp1
->ifindex
== ifp2
->ifindex
)
141 else if (ifp1
->ifindex
> ifp2
->ifindex
)
147 static void ifp_connected_free(void *arg
)
149 struct connected
*c
= arg
;
154 /* Create new interface structure. */
155 static struct interface
*if_new(struct vrf
*vrf
)
157 struct interface
*ifp
;
161 ifp
= XCALLOC(MTYPE_IF
, sizeof(struct interface
));
163 ifp
->ifindex
= IFINDEX_INTERNAL
;
168 ifp
->connected
= list_new();
169 ifp
->connected
->del
= ifp_connected_free
;
171 ifp
->nbr_connected
= list_new();
172 ifp
->nbr_connected
->del
= (void (*)(void *))nbr_connected_free
;
174 /* Enable Link-detection by default */
175 SET_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
);
177 QOBJ_REG(ifp
, interface
);
181 void if_new_via_zapi(struct interface
*ifp
)
183 if (ifp_master
.create_hook
)
184 (*ifp_master
.create_hook
)(ifp
);
187 void if_destroy_via_zapi(struct interface
*ifp
)
189 if (ifp_master
.destroy_hook
)
190 (*ifp_master
.destroy_hook
)(ifp
);
192 ifp
->oldifindex
= ifp
->ifindex
;
193 if_set_index(ifp
, IFINDEX_INTERNAL
);
195 if (!ifp
->configured
)
199 void if_up_via_zapi(struct interface
*ifp
)
201 if (ifp_master
.up_hook
)
202 (*ifp_master
.up_hook
)(ifp
);
205 void if_down_via_zapi(struct interface
*ifp
)
207 if (ifp_master
.down_hook
)
208 (*ifp_master
.down_hook
)(ifp
);
211 static struct interface
*if_create_name(const char *name
, struct vrf
*vrf
)
213 struct interface
*ifp
;
217 if_set_name(ifp
, name
);
219 hook_call(if_add
, ifp
);
223 /* Create new interface structure. */
224 void if_update_to_new_vrf(struct interface
*ifp
, vrf_id_t vrf_id
)
226 struct vrf
*old_vrf
, *vrf
;
228 /* remove interface from old master vrf list */
231 if (ifp
->name
[0] != '\0')
232 IFNAME_RB_REMOVE(old_vrf
, ifp
);
234 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
235 IFINDEX_RB_REMOVE(old_vrf
, ifp
);
237 vrf
= vrf_get(vrf_id
, NULL
);
240 if (ifp
->name
[0] != '\0')
241 IFNAME_RB_INSERT(vrf
, ifp
);
243 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
244 IFINDEX_RB_INSERT(vrf
, ifp
);
248 /* Delete interface structure. */
249 void if_delete_retain(struct interface
*ifp
)
251 hook_call(if_del
, ifp
);
254 /* Free connected address list */
255 list_delete_all_node(ifp
->connected
);
257 /* Free connected nbr address list */
258 list_delete_all_node(ifp
->nbr_connected
);
261 /* Delete and free interface structure. */
262 void if_delete(struct interface
**ifp
)
264 struct interface
*ptr
= *ifp
;
265 struct vrf
*vrf
= ptr
->vrf
;
267 IFNAME_RB_REMOVE(vrf
, ptr
);
268 if (ptr
->ifindex
!= IFINDEX_INTERNAL
)
269 IFINDEX_RB_REMOVE(vrf
, ptr
);
271 if_delete_retain(ptr
);
273 list_delete(&ptr
->connected
);
274 list_delete(&ptr
->nbr_connected
);
276 if_link_params_free(ptr
);
278 XFREE(MTYPE_IFDESC
, ptr
->desc
);
280 XFREE(MTYPE_IF
, ptr
);
284 /* Used only internally to check within VRF only */
285 static struct interface
*if_lookup_by_ifindex(ifindex_t ifindex
,
289 struct interface if_tmp
;
291 vrf
= vrf_lookup_by_id(vrf_id
);
295 if_tmp
.ifindex
= ifindex
;
296 return RB_FIND(if_index_head
, &vrf
->ifaces_by_index
, &if_tmp
);
299 /* Interface existence check by index. */
300 struct interface
*if_lookup_by_index(ifindex_t ifindex
, vrf_id_t vrf_id
)
302 switch (vrf_get_backend()) {
303 case VRF_BACKEND_UNKNOWN
:
304 case VRF_BACKEND_NETNS
:
305 return(if_lookup_by_ifindex(ifindex
, vrf_id
));
306 case VRF_BACKEND_VRF_LITE
:
307 return(if_lookup_by_index_all_vrf(ifindex
));
312 /* Interface existence check by index. */
313 struct interface
*if_vrf_lookup_by_index_next(ifindex_t ifindex
,
316 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
317 struct interface
*tmp_ifp
;
324 tmp_ifp
= RB_MIN(if_index_head
, &vrf
->ifaces_by_index
);
325 /* skip the vrf interface */
326 if (tmp_ifp
&& if_is_vrf(tmp_ifp
))
327 ifindex
= tmp_ifp
->ifindex
;
332 RB_FOREACH (tmp_ifp
, if_index_head
, &vrf
->ifaces_by_index
) {
334 /* skip the vrf interface */
335 if (tmp_ifp
&& if_is_vrf(tmp_ifp
))
340 if (tmp_ifp
->ifindex
== ifindex
)
346 const char *ifindex2ifname(ifindex_t ifindex
, vrf_id_t vrf_id
)
348 struct interface
*ifp
;
350 return ((ifp
= if_lookup_by_index(ifindex
, vrf_id
)) != NULL
)
355 ifindex_t
ifname2ifindex(const char *name
, vrf_id_t vrf_id
)
357 struct interface
*ifp
;
359 return ((ifp
= if_lookup_by_name(name
, vrf_id
)) != NULL
)
364 /* Interface existence check by interface name. */
365 struct interface
*if_lookup_by_name(const char *name
, vrf_id_t vrf_id
)
367 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
368 struct interface if_tmp
;
371 || strnlen(name
, INTERFACE_NAMSIZ
) == INTERFACE_NAMSIZ
)
374 strlcpy(if_tmp
.name
, name
, sizeof(if_tmp
.name
));
375 return RB_FIND(if_name_head
, &vrf
->ifaces_by_name
, &if_tmp
);
378 struct interface
*if_lookup_by_name_vrf(const char *name
, struct vrf
*vrf
)
380 struct interface if_tmp
;
382 if (!name
|| strnlen(name
, INTERFACE_NAMSIZ
) == INTERFACE_NAMSIZ
)
385 strlcpy(if_tmp
.name
, name
, sizeof(if_tmp
.name
));
386 return RB_FIND(if_name_head
, &vrf
->ifaces_by_name
, &if_tmp
);
389 static struct interface
*if_lookup_by_name_all_vrf(const char *name
)
392 struct interface
*ifp
;
394 if (!name
|| strnlen(name
, INTERFACE_NAMSIZ
) == INTERFACE_NAMSIZ
)
397 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
398 ifp
= if_lookup_by_name_vrf(name
, vrf
);
406 static struct interface
*if_lookup_by_index_all_vrf(ifindex_t ifindex
)
409 struct interface
*ifp
;
411 if (ifindex
== IFINDEX_INTERNAL
)
414 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
415 ifp
= if_lookup_by_ifindex(ifindex
, vrf
->vrf_id
);
423 /* Lookup interface by IP address.
425 * supersedes if_lookup_exact_address(), which didn't care about up/down
426 * state. but all users we have either only care if the address is local
427 * (=> use if_address_is_local() please), or care about UP interfaces before
430 * to accept only UP interfaces, check if_is_up() on the returned ifp.
432 struct interface
*if_lookup_address_local(const void *src
, int family
,
435 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
436 struct listnode
*cnode
;
437 struct interface
*ifp
, *best_down
= NULL
;
441 if (family
!= AF_INET
&& family
!= AF_INET6
)
444 FOR_ALL_INTERFACES (vrf
, ifp
) {
445 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
448 if (!p
|| p
->family
!= family
)
451 if (family
== AF_INET
) {
452 if (!IPV4_ADDR_SAME(&p
->u
.prefix4
,
453 (struct in_addr
*)src
))
455 } else if (family
== AF_INET6
) {
456 if (!IPV6_ADDR_SAME(&p
->u
.prefix6
,
457 (struct in6_addr
*)src
))
470 /* Lookup interface by IP address. */
471 struct connected
*if_lookup_address(const void *matchaddr
, int family
,
474 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
477 struct listnode
*cnode
;
478 struct interface
*ifp
;
480 struct connected
*match
;
482 if (family
== AF_INET
) {
483 addr
.family
= AF_INET
;
484 addr
.u
.prefix4
= *((struct in_addr
*)matchaddr
);
485 addr
.prefixlen
= IPV4_MAX_BITLEN
;
486 } else if (family
== AF_INET6
) {
487 addr
.family
= AF_INET6
;
488 addr
.u
.prefix6
= *((struct in6_addr
*)matchaddr
);
489 addr
.prefixlen
= IPV6_MAX_BITLEN
;
491 assert(!"Attempted lookup of family not supported");
495 FOR_ALL_INTERFACES (vrf
, ifp
) {
496 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
497 if (c
->address
&& (c
->address
->family
== AF_INET
)
498 && prefix_match(CONNECTED_PREFIX(c
), &addr
)
499 && (c
->address
->prefixlen
> bestlen
)) {
500 bestlen
= c
->address
->prefixlen
;
508 /* Lookup interface by prefix */
509 struct interface
*if_lookup_prefix(const struct prefix
*prefix
, vrf_id_t vrf_id
)
511 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
512 struct listnode
*cnode
;
513 struct interface
*ifp
;
516 FOR_ALL_INTERFACES (vrf
, ifp
) {
517 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
518 if (prefix_cmp(c
->address
, prefix
) == 0) {
526 size_t if_lookup_by_hwaddr(const uint8_t *hw_addr
, size_t addrsz
,
527 struct interface
***result
, vrf_id_t vrf_id
)
529 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
531 struct list
*rs
= list_new();
532 struct interface
*ifp
;
534 FOR_ALL_INTERFACES (vrf
, ifp
) {
535 if (ifp
->hw_addr_len
== (int)addrsz
536 && !memcmp(hw_addr
, ifp
->hw_addr
, addrsz
))
537 listnode_add(rs
, ifp
);
541 *result
= XCALLOC(MTYPE_TMP
,
542 sizeof(struct interface
*) * rs
->count
);
543 list_to_array(rs
, (void **)*result
, rs
->count
);
546 int count
= rs
->count
;
553 /* Get the VRF loopback interface, i.e. the loopback on the default VRF
554 * or the VRF interface.
556 struct interface
*if_get_vrf_loopback(vrf_id_t vrf_id
)
558 struct interface
*ifp
= NULL
;
559 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
561 FOR_ALL_INTERFACES (vrf
, ifp
)
562 if (if_is_loopback(ifp
))
568 /* Get interface by name if given name interface doesn't exist create
570 struct interface
*if_get_by_name(const char *name
, vrf_id_t vrf_id
,
571 const char *vrf_name
)
573 struct interface
*ifp
= NULL
;
576 switch (vrf_get_backend()) {
577 case VRF_BACKEND_UNKNOWN
:
578 case VRF_BACKEND_NETNS
:
579 vrf
= vrf_get(vrf_id
, vrf_name
);
582 ifp
= if_lookup_by_name_vrf(name
, vrf
);
584 /* If it came from the kernel or by way of zclient,
585 * believe it and update the ifp accordingly.
587 if (ifp
->vrf
->vrf_id
!= vrf_id
&& vrf_id
!= VRF_UNKNOWN
)
588 if_update_to_new_vrf(ifp
, vrf_id
);
594 case VRF_BACKEND_VRF_LITE
:
595 ifp
= if_lookup_by_name_all_vrf(name
);
597 /* If it came from the kernel or by way of zclient,
598 * believe it and update the ifp accordingly.
600 if (ifp
->vrf
->vrf_id
!= vrf_id
&& vrf_id
!= VRF_UNKNOWN
)
601 if_update_to_new_vrf(ifp
, vrf_id
);
606 vrf
= vrf_get(vrf_id
, vrf_name
);
614 return if_create_name(name
, vrf
);
617 int if_set_index(struct interface
*ifp
, ifindex_t ifindex
)
619 if (ifp
->ifindex
== ifindex
)
623 * If there is already an interface with this ifindex, we will collide
624 * on insertion, so don't even try.
626 if (if_lookup_by_ifindex(ifindex
, ifp
->vrf
->vrf_id
))
629 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
630 IFINDEX_RB_REMOVE(ifp
->vrf
, ifp
);
632 ifp
->ifindex
= ifindex
;
634 if (ifp
->ifindex
!= IFINDEX_INTERNAL
) {
636 * This should never happen, since we checked if there was
637 * already an interface with the desired ifindex at the top of
638 * the function. Nevertheless.
640 if (IFINDEX_RB_INSERT(ifp
->vrf
, ifp
))
647 static void if_set_name(struct interface
*ifp
, const char *name
)
649 if (if_cmp_name_func(ifp
->name
, name
) == 0)
652 if (ifp
->name
[0] != '\0')
653 IFNAME_RB_REMOVE(ifp
->vrf
, ifp
);
655 strlcpy(ifp
->name
, name
, sizeof(ifp
->name
));
657 if (ifp
->name
[0] != '\0')
658 IFNAME_RB_INSERT(ifp
->vrf
, ifp
);
661 /* Does interface up ? */
662 int if_is_up(const struct interface
*ifp
)
664 return ifp
->flags
& IFF_UP
;
667 /* Is interface running? */
668 int if_is_running(const struct interface
*ifp
)
670 return ifp
->flags
& IFF_RUNNING
;
673 /* Is the interface operative, eg. either UP & RUNNING
674 or UP & !ZEBRA_INTERFACE_LINK_DETECTION and
675 if ptm checking is enabled, then ptm check has passed */
676 int if_is_operative(const struct interface
*ifp
)
678 return ((ifp
->flags
& IFF_UP
)
679 && (((ifp
->flags
& IFF_RUNNING
)
680 && (ifp
->ptm_status
|| !ifp
->ptm_enable
))
681 || !CHECK_FLAG(ifp
->status
,
682 ZEBRA_INTERFACE_LINKDETECTION
)));
685 /* Is the interface operative, eg. either UP & RUNNING
686 or UP & !ZEBRA_INTERFACE_LINK_DETECTION, without PTM check */
687 int if_is_no_ptm_operative(const struct interface
*ifp
)
689 return ((ifp
->flags
& IFF_UP
)
690 && ((ifp
->flags
& IFF_RUNNING
)
691 || !CHECK_FLAG(ifp
->status
,
692 ZEBRA_INTERFACE_LINKDETECTION
)));
695 /* Is this loopback interface ? */
696 int if_is_loopback_exact(const struct interface
*ifp
)
698 /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M
699 * but Y on platform N?
701 return (ifp
->flags
& (IFF_LOOPBACK
| IFF_NOXMIT
| IFF_VIRTUAL
));
704 /* Check interface is VRF */
705 int if_is_vrf(const struct interface
*ifp
)
707 return CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
710 /* Should this interface be treated as a loopback? */
711 bool if_is_loopback(const struct interface
*ifp
)
713 if (if_is_loopback_exact(ifp
) || if_is_vrf(ifp
))
719 /* Does this interface support broadcast ? */
720 int if_is_broadcast(const struct interface
*ifp
)
722 return ifp
->flags
& IFF_BROADCAST
;
725 /* Does this interface support pointopoint ? */
726 int if_is_pointopoint(const struct interface
*ifp
)
728 return ifp
->flags
& IFF_POINTOPOINT
;
731 /* Does this interface support multicast ? */
732 int if_is_multicast(const struct interface
*ifp
)
734 return ifp
->flags
& IFF_MULTICAST
;
737 /* Printout flag information into log */
738 const char *if_flag_dump(unsigned long flag
)
741 static char logbuf
[BUFSIZ
];
743 #define IFF_OUT_LOG(X, STR) \
746 strlcat(logbuf, ",", sizeof(logbuf)); \
749 strlcat(logbuf, STR, sizeof(logbuf)); \
752 strlcpy(logbuf
, "<", BUFSIZ
);
753 IFF_OUT_LOG(IFF_UP
, "UP");
754 IFF_OUT_LOG(IFF_BROADCAST
, "BROADCAST");
755 IFF_OUT_LOG(IFF_DEBUG
, "DEBUG");
756 IFF_OUT_LOG(IFF_LOOPBACK
, "LOOPBACK");
757 IFF_OUT_LOG(IFF_POINTOPOINT
, "POINTOPOINT");
758 IFF_OUT_LOG(IFF_NOTRAILERS
, "NOTRAILERS");
759 IFF_OUT_LOG(IFF_RUNNING
, "RUNNING");
760 IFF_OUT_LOG(IFF_NOARP
, "NOARP");
761 IFF_OUT_LOG(IFF_PROMISC
, "PROMISC");
762 IFF_OUT_LOG(IFF_ALLMULTI
, "ALLMULTI");
763 IFF_OUT_LOG(IFF_OACTIVE
, "OACTIVE");
764 IFF_OUT_LOG(IFF_SIMPLEX
, "SIMPLEX");
765 IFF_OUT_LOG(IFF_LINK0
, "LINK0");
766 IFF_OUT_LOG(IFF_LINK1
, "LINK1");
767 IFF_OUT_LOG(IFF_LINK2
, "LINK2");
768 IFF_OUT_LOG(IFF_MULTICAST
, "MULTICAST");
769 IFF_OUT_LOG(IFF_NOXMIT
, "NOXMIT");
770 IFF_OUT_LOG(IFF_NORTEXCH
, "NORTEXCH");
771 IFF_OUT_LOG(IFF_VIRTUAL
, "VIRTUAL");
772 IFF_OUT_LOG(IFF_IPV4
, "IPv4");
773 IFF_OUT_LOG(IFF_IPV6
, "IPv6");
775 strlcat(logbuf
, ">", sizeof(logbuf
));
782 static void if_dump(const struct interface
*ifp
)
784 struct listnode
*node
;
785 struct connected
*c
__attribute__((unused
));
787 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, c
))
789 "Interface %s vrf %s(%u) index %d metric %d mtu %d mtu6 %d %s",
790 ifp
->name
, ifp
->vrf
->name
, ifp
->vrf
->vrf_id
,
791 ifp
->ifindex
, ifp
->metric
, ifp
->mtu
, ifp
->mtu6
,
792 if_flag_dump(ifp
->flags
));
795 /* Interface printing for all interface. */
796 void if_dump_all(void)
801 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
802 FOR_ALL_INTERFACES (vrf
, ifp
)
806 /* Allocate connected structure. */
807 struct connected
*connected_new(void)
809 return XCALLOC(MTYPE_CONNECTED
, sizeof(struct connected
));
812 /* Allocate nbr connected structure. */
813 struct nbr_connected
*nbr_connected_new(void)
815 return XCALLOC(MTYPE_NBR_CONNECTED
, sizeof(struct nbr_connected
));
818 /* Free connected structure. */
819 void connected_free(struct connected
**connected
)
821 struct connected
*ptr
= *connected
;
823 prefix_free(&ptr
->address
);
824 prefix_free(&ptr
->destination
);
826 XFREE(MTYPE_CONNECTED_LABEL
, ptr
->label
);
828 XFREE(MTYPE_CONNECTED
, ptr
);
832 /* Free nbr connected structure. */
833 void nbr_connected_free(struct nbr_connected
*connected
)
835 if (connected
->address
)
836 prefix_free(&connected
->address
);
838 XFREE(MTYPE_NBR_CONNECTED
, connected
);
841 /* If same interface nbr address already exists... */
842 struct nbr_connected
*nbr_connected_check(struct interface
*ifp
,
845 struct nbr_connected
*ifc
;
846 struct listnode
*node
;
848 for (ALL_LIST_ELEMENTS_RO(ifp
->nbr_connected
, node
, ifc
))
849 if (prefix_same(ifc
->address
, p
))
855 /* Print if_addr structure. */
856 static void __attribute__((unused
))
857 connected_log(struct connected
*connected
, char *str
)
860 struct interface
*ifp
;
864 ifp
= connected
->ifp
;
865 p
= connected
->address
;
867 snprintf(logbuf
, sizeof(logbuf
), "%s interface %s vrf %s(%u) %s %pFX ",
868 str
, ifp
->name
, ifp
->vrf
->name
, ifp
->vrf
->vrf_id
,
869 prefix_family_str(p
), p
);
871 p
= connected
->destination
;
873 strlcat(logbuf
, inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
876 zlog_info("%s", logbuf
);
879 /* Print if_addr structure. */
880 static void __attribute__((unused
))
881 nbr_connected_log(struct nbr_connected
*connected
, char *str
)
884 struct interface
*ifp
;
887 ifp
= connected
->ifp
;
888 p
= connected
->address
;
890 snprintf(logbuf
, sizeof(logbuf
), "%s interface %s %s %pFX ", str
,
891 ifp
->name
, prefix_family_str(p
), p
);
893 zlog_info("%s", logbuf
);
896 /* If two connected address has same prefix return 1. */
897 static int connected_same_prefix(const struct prefix
*p1
,
898 const struct prefix
*p2
)
900 if (p1
->family
== p2
->family
) {
901 if (p1
->family
== AF_INET
902 && IPV4_ADDR_SAME(&p1
->u
.prefix4
, &p2
->u
.prefix4
))
904 if (p1
->family
== AF_INET6
905 && IPV6_ADDR_SAME(&p1
->u
.prefix6
, &p2
->u
.prefix6
))
911 /* count the number of connected addresses that are in the given family */
912 unsigned int connected_count_by_family(struct interface
*ifp
, int family
)
914 struct listnode
*cnode
;
915 struct connected
*connected
;
916 unsigned int cnt
= 0;
918 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
))
919 if (connected
->address
->family
== family
)
925 struct connected
*connected_lookup_prefix_exact(struct interface
*ifp
,
926 const struct prefix
*p
)
928 struct listnode
*node
;
929 struct listnode
*next
;
930 struct connected
*ifc
;
932 for (node
= listhead(ifp
->connected
); node
; node
= next
) {
933 ifc
= listgetdata(node
);
936 if (connected_same_prefix(ifc
->address
, p
))
942 struct connected
*connected_delete_by_prefix(struct interface
*ifp
,
945 struct listnode
*node
;
946 struct listnode
*next
;
947 struct connected
*ifc
;
949 /* In case of same prefix come, replace it with new one. */
950 for (node
= listhead(ifp
->connected
); node
; node
= next
) {
951 ifc
= listgetdata(node
);
954 if (connected_same_prefix(ifc
->address
, p
)) {
955 listnode_delete(ifp
->connected
, ifc
);
962 /* Find the address on our side that will be used when packets
964 struct connected
*connected_lookup_prefix(struct interface
*ifp
,
965 const struct prefix
*addr
)
967 struct listnode
*cnode
;
969 struct connected
*match
;
973 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
974 if (c
->address
&& (c
->address
->family
== addr
->family
)
975 && prefix_match(CONNECTED_PREFIX(c
), addr
)
977 || (c
->address
->prefixlen
> match
->address
->prefixlen
)))
983 struct connected
*connected_add_by_prefix(struct interface
*ifp
,
985 struct prefix
*destination
)
987 struct connected
*ifc
;
989 /* Allocate new connected address. */
990 ifc
= connected_new();
993 /* Fetch interface address */
994 ifc
->address
= prefix_new();
995 memcpy(ifc
->address
, p
, sizeof(struct prefix
));
997 /* Fetch dest address */
999 ifc
->destination
= prefix_new();
1000 memcpy(ifc
->destination
, destination
, sizeof(struct prefix
));
1003 /* Add connected address to the interface. */
1004 listnode_add(ifp
->connected
, ifc
);
1008 struct connected
*connected_get_linklocal(struct interface
*ifp
)
1011 struct connected
*c
= NULL
;
1013 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, n
, c
)) {
1014 if (c
->address
->family
== AF_INET6
1015 && IN6_IS_ADDR_LINKLOCAL(&c
->address
->u
.prefix6
))
1021 void if_terminate(struct vrf
*vrf
)
1023 struct interface
*ifp
;
1025 while (!RB_EMPTY(if_name_head
, &vrf
->ifaces_by_name
)) {
1026 ifp
= RB_ROOT(if_name_head
, &vrf
->ifaces_by_name
);
1029 ifp
->node
->info
= NULL
;
1030 route_unlock_node(ifp
->node
);
1036 const char *if_link_type_str(enum zebra_link_type llt
)
1039 #define llts(T,S) case (T): return (S)
1040 llts(ZEBRA_LLT_UNKNOWN
, "Unknown");
1041 llts(ZEBRA_LLT_ETHER
, "Ethernet");
1042 llts(ZEBRA_LLT_EETHER
, "Experimental Ethernet");
1043 llts(ZEBRA_LLT_AX25
, "AX.25 Level 2");
1044 llts(ZEBRA_LLT_PRONET
, "PROnet token ring");
1045 llts(ZEBRA_LLT_IEEE802
, "IEEE 802.2 Ethernet/TR/TB");
1046 llts(ZEBRA_LLT_ARCNET
, "ARCnet");
1047 llts(ZEBRA_LLT_APPLETLK
, "AppleTalk");
1048 llts(ZEBRA_LLT_DLCI
, "Frame Relay DLCI");
1049 llts(ZEBRA_LLT_ATM
, "ATM");
1050 llts(ZEBRA_LLT_METRICOM
, "Metricom STRIP");
1051 llts(ZEBRA_LLT_IEEE1394
, "IEEE 1394 IPv4");
1052 llts(ZEBRA_LLT_EUI64
, "EUI-64");
1053 llts(ZEBRA_LLT_INFINIBAND
, "InfiniBand");
1054 llts(ZEBRA_LLT_SLIP
, "SLIP");
1055 llts(ZEBRA_LLT_CSLIP
, "Compressed SLIP");
1056 llts(ZEBRA_LLT_SLIP6
, "SLIPv6");
1057 llts(ZEBRA_LLT_CSLIP6
, "Compressed SLIPv6");
1058 llts(ZEBRA_LLT_RSRVD
, "Reserved");
1059 llts(ZEBRA_LLT_ADAPT
, "Adapt");
1060 llts(ZEBRA_LLT_ROSE
, "ROSE packet radio");
1061 llts(ZEBRA_LLT_X25
, "CCITT X.25");
1062 llts(ZEBRA_LLT_PPP
, "PPP");
1063 llts(ZEBRA_LLT_CHDLC
, "Cisco HDLC");
1064 llts(ZEBRA_LLT_RAWHDLC
, "Raw HDLC");
1065 llts(ZEBRA_LLT_LAPB
, "LAPB");
1066 llts(ZEBRA_LLT_IPIP
, "IPIP Tunnel");
1067 llts(ZEBRA_LLT_IPIP6
, "IPIP6 Tunnel");
1068 llts(ZEBRA_LLT_FRAD
, "FRAD");
1069 llts(ZEBRA_LLT_SKIP
, "SKIP vif");
1070 llts(ZEBRA_LLT_LOOPBACK
, "Loopback");
1071 llts(ZEBRA_LLT_LOCALTLK
, "Localtalk");
1072 llts(ZEBRA_LLT_FDDI
, "FDDI");
1073 llts(ZEBRA_LLT_SIT
, "IPv6-in-IPv4 SIT");
1074 llts(ZEBRA_LLT_IPDDP
, "IP-in-DDP tunnel");
1075 llts(ZEBRA_LLT_IPGRE
, "GRE over IP");
1076 llts(ZEBRA_LLT_IP6GRE
, "GRE over IPv6");
1077 llts(ZEBRA_LLT_PIMREG
, "PIMSM registration");
1078 llts(ZEBRA_LLT_HIPPI
, "HiPPI");
1079 llts(ZEBRA_LLT_ECONET
, "Acorn Econet");
1080 llts(ZEBRA_LLT_IRDA
, "IrDA");
1081 llts(ZEBRA_LLT_FCPP
, "Fibre-Channel PtP");
1082 llts(ZEBRA_LLT_FCAL
, "Fibre-Channel Arbitrated Loop");
1083 llts(ZEBRA_LLT_FCPL
, "Fibre-Channel Public Loop");
1084 llts(ZEBRA_LLT_FCFABRIC
, "Fibre-Channel Fabric");
1085 llts(ZEBRA_LLT_IEEE802_TR
, "IEEE 802.2 Token Ring");
1086 llts(ZEBRA_LLT_IEEE80211
, "IEEE 802.11");
1087 llts(ZEBRA_LLT_IEEE80211_RADIOTAP
, "IEEE 802.11 Radiotap");
1088 llts(ZEBRA_LLT_IEEE802154
, "IEEE 802.15.4");
1089 llts(ZEBRA_LLT_IEEE802154_PHY
, "IEEE 802.15.4 Phy");
1095 bool if_link_params_cmp(struct if_link_params
*iflp1
,
1096 struct if_link_params
*iflp2
)
1098 struct if_link_params iflp1_copy
, iflp2_copy
;
1100 /* Extended admin-groups in if_link_params contain pointers.
1101 * They cannot be compared with memcpy.
1102 * Make copies of if_link_params without ext. admin-groups
1103 * and compare separately the ext. admin-groups.
1105 memcpy(&iflp1_copy
, iflp1
, sizeof(struct if_link_params
));
1106 memset(&iflp1_copy
.ext_admin_grp
, 0, sizeof(struct admin_group
));
1108 memcpy(&iflp2_copy
, iflp2
, sizeof(struct if_link_params
));
1109 memset(&iflp2_copy
.ext_admin_grp
, 0, sizeof(struct admin_group
));
1111 if (memcmp(&iflp1_copy
, &iflp2_copy
, sizeof(struct if_link_params
)))
1114 if (!admin_group_cmp(&iflp1
->ext_admin_grp
, &iflp2
->ext_admin_grp
))
1120 void if_link_params_copy(struct if_link_params
*dst
, struct if_link_params
*src
)
1122 struct admin_group dst_ag
;
1124 /* backup the admin_group structure that contains a pointer */
1125 memcpy(&dst_ag
, &dst
->ext_admin_grp
, sizeof(struct admin_group
));
1126 /* copy the if_link_params structure */
1127 memcpy(dst
, src
, sizeof(struct if_link_params
));
1128 /* restore the admin_group structure */
1129 memcpy(&dst
->ext_admin_grp
, &dst_ag
, sizeof(struct admin_group
));
1130 /* copy src->ext_admin_grp data to dst->ext_admin_grp data memory */
1131 admin_group_copy(&dst
->ext_admin_grp
, &src
->ext_admin_grp
);
1134 struct if_link_params
*if_link_params_get(struct interface
*ifp
)
1136 return ifp
->link_params
;
1139 struct if_link_params
*if_link_params_enable(struct interface
*ifp
)
1141 struct if_link_params
*iflp
;
1144 iflp
= if_link_params_init(ifp
);
1146 /* Compute default bandwidth based on interface */
1148 ((ifp
->bandwidth
? ifp
->bandwidth
: DEFAULT_BANDWIDTH
)
1149 * TE_MEGA_BIT
/ TE_BYTE
);
1151 /* Set Max, Reservable and Unreserved Bandwidth */
1152 iflp
->max_bw
= iflp
->default_bw
;
1153 iflp
->max_rsv_bw
= iflp
->default_bw
;
1154 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1155 iflp
->unrsv_bw
[i
] = iflp
->default_bw
;
1157 /* Update Link parameters status */
1158 iflp
->lp_status
= LP_MAX_BW
| LP_MAX_RSV_BW
| LP_UNRSV_BW
;
1160 /* Set TE metric equal to standard metric only if it is set */
1161 if (ifp
->metric
!= 0) {
1162 iflp
->te_metric
= ifp
->metric
;
1163 iflp
->lp_status
|= LP_TE_METRIC
;
1166 /* Finally attach newly created Link Parameters */
1167 ifp
->link_params
= iflp
;
1172 struct if_link_params
*if_link_params_init(struct interface
*ifp
)
1174 struct if_link_params
*iflp
= if_link_params_get(ifp
);
1179 iflp
= XCALLOC(MTYPE_IF_LINK_PARAMS
, sizeof(struct if_link_params
));
1181 admin_group_init(&iflp
->ext_admin_grp
);
1183 ifp
->link_params
= iflp
;
1188 void if_link_params_free(struct interface
*ifp
)
1190 if (!ifp
->link_params
)
1193 admin_group_term(&ifp
->link_params
->ext_admin_grp
);
1194 XFREE(MTYPE_IF_LINK_PARAMS
, ifp
->link_params
);
1197 /* ----------- CLI commands ----------- */
1199 /* Guess the VRF of an interface. */
1200 static int vrfname_by_ifname(const char *ifname
, const char **vrfname
)
1203 struct interface
*ifp
;
1206 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
1207 FOR_ALL_INTERFACES (vrf
, ifp
) {
1208 if (strmatch(ifp
->name
, ifname
)) {
1209 *vrfname
= vrf
->name
;
1219 * XPath: /frr-interface:lib/interface
1221 DEFPY_YANG_NOSH (interface
,
1223 "interface IFNAME [vrf NAME$vrf_name]",
1224 "Select an interface to configure\n"
1225 "Interface's name\n"
1228 char xpath_list
[XPATH_MAXLEN
];
1229 struct interface
*ifp
;
1233 if (vrf_is_backend_netns()) {
1235 * For backward compatibility, if the VRF name is not specified
1236 * and there is exactly one interface with this name in the
1237 * system, use its VRF. Otherwise fallback to the default VRF.
1240 count
= vrfname_by_ifname(ifname
, &vrf_name
);
1242 vrf_name
= VRF_DEFAULT_NAME
;
1245 snprintf(xpath_list
, XPATH_MAXLEN
,
1246 "/frr-interface:lib/interface[name='%s:%s']", vrf_name
,
1249 snprintf(xpath_list
, XPATH_MAXLEN
,
1250 "/frr-interface:lib/interface[name='%s']", ifname
);
1253 nb_cli_enqueue_change(vty
, ".", NB_OP_CREATE
, NULL
);
1254 ret
= nb_cli_apply_changes_clear_pending(vty
, "%s", xpath_list
);
1255 if (ret
== CMD_SUCCESS
) {
1256 VTY_PUSH_XPATH(INTERFACE_NODE
, xpath_list
);
1259 * For backward compatibility with old commands we still need
1260 * to use the qobj infrastructure. This can be removed once
1261 * all interface-level commands are converted to the new
1264 if (vrf_is_backend_netns()) {
1265 vrf
= vrf_lookup_by_name(vrf_name
);
1267 ifp
= if_lookup_by_name_vrf(ifname
, vrf
);
1271 ifp
= if_lookup_by_name_all_vrf(ifname
);
1274 VTY_PUSH_CONTEXT(INTERFACE_NODE
, ifp
);
1280 DEFPY_YANG (no_interface
,
1282 "no interface IFNAME [vrf NAME$vrf_name]",
1284 "Delete a pseudo interface's configuration\n"
1285 "Interface's name\n"
1288 char xpath_list
[XPATH_MAXLEN
];
1291 if (vrf_is_backend_netns()) {
1293 * For backward compatibility, if the VRF name is not specified
1294 * and there is exactly one interface with this name in the
1295 * system, use its VRF. Otherwise fallback to the default VRF.
1298 count
= vrfname_by_ifname(ifname
, &vrf_name
);
1300 vrf_name
= VRF_DEFAULT_NAME
;
1303 snprintf(xpath_list
, XPATH_MAXLEN
,
1304 "/frr-interface:lib/interface[name='%s:%s']", vrf_name
,
1307 snprintf(xpath_list
, XPATH_MAXLEN
,
1308 "/frr-interface:lib/interface[name='%s']", ifname
);
1311 nb_cli_enqueue_change(vty
, ".", NB_OP_DESTROY
, NULL
);
1313 return nb_cli_apply_changes(vty
, "%s", xpath_list
);
1316 static void netns_ifname_split(const char *xpath
, char *ifname
, char *vrfname
)
1321 assert(vrf_is_backend_netns());
1323 delim
= strchr(xpath
, ':');
1326 len
= delim
- xpath
;
1327 memcpy(vrfname
, xpath
, len
);
1330 strlcpy(ifname
, delim
+ 1, XPATH_MAXLEN
);
1333 static void cli_show_interface(struct vty
*vty
, const struct lyd_node
*dnode
,
1336 vty_out(vty
, "!\n");
1338 if (vrf_is_backend_netns()) {
1339 char ifname
[XPATH_MAXLEN
];
1340 char vrfname
[XPATH_MAXLEN
];
1342 netns_ifname_split(yang_dnode_get_string(dnode
, "./name"),
1345 vty_out(vty
, "interface %s", ifname
);
1346 if (!strmatch(vrfname
, VRF_DEFAULT_NAME
))
1347 vty_out(vty
, " vrf %s", vrfname
);
1349 const char *ifname
= yang_dnode_get_string(dnode
, "./name");
1351 vty_out(vty
, "interface %s", ifname
);
1357 static void cli_show_interface_end(struct vty
*vty
,
1358 const struct lyd_node
*dnode
)
1360 vty_out(vty
, "exit\n");
1363 void if_vty_config_start(struct vty
*vty
, struct interface
*ifp
)
1365 vty_frame(vty
, "!\n");
1366 vty_frame(vty
, "interface %s", ifp
->name
);
1368 if (vrf_is_backend_netns() && strcmp(ifp
->vrf
->name
, VRF_DEFAULT_NAME
))
1369 vty_frame(vty
, " vrf %s", ifp
->vrf
->name
);
1371 vty_frame(vty
, "\n");
1374 void if_vty_config_end(struct vty
*vty
)
1376 vty_endframe(vty
, "exit\n!\n");
1380 * XPath: /frr-interface:lib/interface/description
1382 DEFPY_YANG (interface_desc
,
1384 "description LINE...",
1385 "Interface specific description\n"
1386 "Characters describing this interface\n")
1391 desc
= argv_concat(argv
, argc
, 1);
1392 nb_cli_enqueue_change(vty
, "./description", NB_OP_MODIFY
, desc
);
1393 ret
= nb_cli_apply_changes(vty
, NULL
);
1394 XFREE(MTYPE_TMP
, desc
);
1399 DEFPY_YANG (no_interface_desc
,
1400 no_interface_desc_cmd
,
1403 "Interface specific description\n")
1405 nb_cli_enqueue_change(vty
, "./description", NB_OP_DESTROY
, NULL
);
1407 return nb_cli_apply_changes(vty
, NULL
);
1410 static void cli_show_interface_desc(struct vty
*vty
,
1411 const struct lyd_node
*dnode
,
1414 vty_out(vty
, " description %s\n", yang_dnode_get_string(dnode
, NULL
));
1417 /* Interface autocomplete. */
1418 static void if_autocomplete(vector comps
, struct cmd_token
*token
)
1420 struct interface
*ifp
;
1423 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
1424 FOR_ALL_INTERFACES (vrf
, ifp
) {
1425 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, ifp
->name
));
1430 static const struct cmd_variable_handler if_var_handlers
[] = {
1431 {/* "interface NAME" */
1432 .varname
= "interface",
1433 .completions
= if_autocomplete
},
1434 {.tokenname
= "IFNAME", .completions
= if_autocomplete
},
1435 {.tokenname
= "INTERFACE", .completions
= if_autocomplete
},
1436 {.completions
= NULL
}};
1438 static struct cmd_node interface_node
= {
1439 .name
= "interface",
1440 .node
= INTERFACE_NODE
,
1441 .parent_node
= CONFIG_NODE
,
1442 .prompt
= "%s(config-if)# ",
1445 static int if_config_write_single(const struct lyd_node
*dnode
, void *arg
)
1447 nb_cli_show_dnode_cmds(arg
, dnode
, false);
1449 return YANG_ITER_CONTINUE
;
1452 static int if_nb_config_write(struct vty
*vty
)
1454 yang_dnode_iterate(if_config_write_single
, vty
, running_config
->dnode
,
1455 "/frr-interface:lib/interface");
1459 void if_cmd_init(int (*config_write
)(struct vty
*))
1461 cmd_variable_handler_register(if_var_handlers
);
1463 interface_node
.config_write
= config_write
;
1464 install_node(&interface_node
);
1466 install_element(CONFIG_NODE
, &interface_cmd
);
1467 install_element(CONFIG_NODE
, &no_interface_cmd
);
1469 install_default(INTERFACE_NODE
);
1470 install_element(INTERFACE_NODE
, &interface_desc_cmd
);
1471 install_element(INTERFACE_NODE
, &no_interface_desc_cmd
);
1474 void if_cmd_init_default(void)
1476 if_cmd_init(if_nb_config_write
);
1479 void if_zapi_callbacks(int (*create
)(struct interface
*ifp
),
1480 int (*up
)(struct interface
*ifp
),
1481 int (*down
)(struct interface
*ifp
),
1482 int (*destroy
)(struct interface
*ifp
))
1484 ifp_master
.create_hook
= create
;
1485 ifp_master
.up_hook
= up
;
1486 ifp_master
.down_hook
= down
;
1487 ifp_master
.destroy_hook
= destroy
;
1490 /* ------- Northbound callbacks ------- */
1493 * XPath: /frr-interface:lib/interface
1495 static int lib_interface_create(struct nb_cb_create_args
*args
)
1498 struct interface
*ifp
;
1500 ifname
= yang_dnode_get_string(args
->dnode
, "./name");
1502 switch (args
->event
) {
1503 case NB_EV_VALIDATE
:
1504 if (vrf_is_backend_netns()) {
1505 char ifname_ns
[XPATH_MAXLEN
];
1506 char vrfname_ns
[XPATH_MAXLEN
];
1508 netns_ifname_split(ifname
, ifname_ns
, vrfname_ns
);
1510 if (strlen(ifname_ns
) > 16) {
1512 args
->errmsg
, args
->errmsg_len
,
1513 "Maximum interface name length is 16 characters");
1514 return NB_ERR_VALIDATION
;
1516 if (strlen(vrfname_ns
) > 36) {
1518 args
->errmsg
, args
->errmsg_len
,
1519 "Maximum VRF name length is 36 characters");
1520 return NB_ERR_VALIDATION
;
1523 if (strlen(ifname
) > 16) {
1525 args
->errmsg
, args
->errmsg_len
,
1526 "Maximum interface name length is 16 characters");
1527 return NB_ERR_VALIDATION
;
1535 if (vrf_is_backend_netns()) {
1536 char ifname_ns
[XPATH_MAXLEN
];
1537 char vrfname_ns
[XPATH_MAXLEN
];
1539 netns_ifname_split(ifname
, ifname_ns
, vrfname_ns
);
1541 ifp
= if_get_by_name(ifname_ns
, VRF_UNKNOWN
,
1544 ifp
= if_get_by_name(ifname
, VRF_UNKNOWN
,
1548 ifp
->configured
= true;
1549 nb_running_set_entry(args
->dnode
, ifp
);
1556 static int lib_interface_destroy(struct nb_cb_destroy_args
*args
)
1558 struct interface
*ifp
;
1561 switch (args
->event
) {
1562 case NB_EV_VALIDATE
:
1563 ifp
= nb_running_get_entry(args
->dnode
, NULL
, true);
1564 if (CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
)) {
1565 snprintf(args
->errmsg
, args
->errmsg_len
,
1566 "only inactive interfaces can be deleted");
1567 return NB_ERR_VALIDATION
;
1574 ifp
= nb_running_unset_entry(args
->dnode
);
1577 ifp
->configured
= false;
1580 if (!vrf_is_enabled(vrf
))
1589 * XPath: /frr-interface:lib/interface
1591 static const void *lib_interface_get_next(struct nb_cb_get_next_args
*args
)
1594 struct interface
*pif
= (struct interface
*)args
->list_entry
;
1596 if (args
->list_entry
== NULL
) {
1597 vrf
= RB_MIN(vrf_name_head
, &vrfs_by_name
);
1599 pif
= RB_MIN(if_name_head
, &vrf
->ifaces_by_name
);
1602 pif
= RB_NEXT(if_name_head
, pif
);
1603 /* if no more interfaces, switch to next vrf */
1604 while (pif
== NULL
) {
1605 vrf
= RB_NEXT(vrf_name_head
, vrf
);
1608 pif
= RB_MIN(if_name_head
, &vrf
->ifaces_by_name
);
1615 static int lib_interface_get_keys(struct nb_cb_get_keys_args
*args
)
1617 const struct interface
*ifp
= args
->list_entry
;
1619 args
->keys
->num
= 1;
1621 if (vrf_is_backend_netns())
1622 snprintf(args
->keys
->key
[0], sizeof(args
->keys
->key
[0]),
1623 "%s:%s", ifp
->vrf
->name
, ifp
->name
);
1625 snprintf(args
->keys
->key
[0], sizeof(args
->keys
->key
[0]), "%s",
1632 lib_interface_lookup_entry(struct nb_cb_lookup_entry_args
*args
)
1634 if (vrf_is_backend_netns()) {
1635 char ifname
[XPATH_MAXLEN
];
1636 char vrfname
[XPATH_MAXLEN
];
1639 netns_ifname_split(args
->keys
->key
[0], ifname
, vrfname
);
1641 vrf
= vrf_lookup_by_name(vrfname
);
1643 return vrf
? if_lookup_by_name(ifname
, vrf
->vrf_id
) : NULL
;
1645 return if_lookup_by_name_all_vrf(args
->keys
->key
[0]);
1650 * XPath: /frr-interface:lib/interface/description
1652 static int lib_interface_description_modify(struct nb_cb_modify_args
*args
)
1654 struct interface
*ifp
;
1655 const char *description
;
1657 if (args
->event
!= NB_EV_APPLY
)
1660 ifp
= nb_running_get_entry(args
->dnode
, NULL
, true);
1661 XFREE(MTYPE_IFDESC
, ifp
->desc
);
1662 description
= yang_dnode_get_string(args
->dnode
, NULL
);
1663 ifp
->desc
= XSTRDUP(MTYPE_IFDESC
, description
);
1668 static int lib_interface_description_destroy(struct nb_cb_destroy_args
*args
)
1670 struct interface
*ifp
;
1672 if (args
->event
!= NB_EV_APPLY
)
1675 ifp
= nb_running_get_entry(args
->dnode
, NULL
, true);
1676 XFREE(MTYPE_IFDESC
, ifp
->desc
);
1682 * XPath: /frr-interface:lib/interface/vrf
1684 static struct yang_data
*
1685 lib_interface_vrf_get_elem(struct nb_cb_get_elem_args
*args
)
1687 const struct interface
*ifp
= args
->list_entry
;
1689 return yang_data_new_string(args
->xpath
, ifp
->vrf
->name
);
1693 * XPath: /frr-interface:lib/interface/state/if-index
1695 static struct yang_data
*
1696 lib_interface_state_if_index_get_elem(struct nb_cb_get_elem_args
*args
)
1698 const struct interface
*ifp
= args
->list_entry
;
1700 return yang_data_new_int32(args
->xpath
, ifp
->ifindex
);
1704 * XPath: /frr-interface:lib/interface/state/mtu
1706 static struct yang_data
*
1707 lib_interface_state_mtu_get_elem(struct nb_cb_get_elem_args
*args
)
1709 const struct interface
*ifp
= args
->list_entry
;
1711 return yang_data_new_uint16(args
->xpath
, ifp
->mtu
);
1715 * XPath: /frr-interface:lib/interface/state/mtu6
1717 static struct yang_data
*
1718 lib_interface_state_mtu6_get_elem(struct nb_cb_get_elem_args
*args
)
1720 const struct interface
*ifp
= args
->list_entry
;
1722 return yang_data_new_uint32(args
->xpath
, ifp
->mtu6
);
1726 * XPath: /frr-interface:lib/interface/state/speed
1728 static struct yang_data
*
1729 lib_interface_state_speed_get_elem(struct nb_cb_get_elem_args
*args
)
1731 const struct interface
*ifp
= args
->list_entry
;
1733 return yang_data_new_uint32(args
->xpath
, ifp
->speed
);
1737 * XPath: /frr-interface:lib/interface/state/metric
1739 static struct yang_data
*
1740 lib_interface_state_metric_get_elem(struct nb_cb_get_elem_args
*args
)
1742 const struct interface
*ifp
= args
->list_entry
;
1744 return yang_data_new_uint32(args
->xpath
, ifp
->metric
);
1748 * XPath: /frr-interface:lib/interface/state/flags
1750 static struct yang_data
*
1751 lib_interface_state_flags_get_elem(struct nb_cb_get_elem_args
*args
)
1753 /* TODO: implement me. */
1758 * XPath: /frr-interface:lib/interface/state/type
1760 static struct yang_data
*
1761 lib_interface_state_type_get_elem(struct nb_cb_get_elem_args
*args
)
1763 /* TODO: implement me. */
1768 * XPath: /frr-interface:lib/interface/state/phy-address
1770 static struct yang_data
*
1771 lib_interface_state_phy_address_get_elem(struct nb_cb_get_elem_args
*args
)
1773 const struct interface
*ifp
= args
->list_entry
;
1774 struct ethaddr macaddr
;
1776 memcpy(&macaddr
.octet
, ifp
->hw_addr
, ETH_ALEN
);
1778 return yang_data_new_mac(args
->xpath
, &macaddr
);
1781 /* clang-format off */
1782 const struct frr_yang_module_info frr_interface_info
= {
1783 .name
= "frr-interface",
1786 .xpath
= "/frr-interface:lib/interface",
1788 .create
= lib_interface_create
,
1789 .destroy
= lib_interface_destroy
,
1790 .cli_show
= cli_show_interface
,
1791 .cli_show_end
= cli_show_interface_end
,
1792 .get_next
= lib_interface_get_next
,
1793 .get_keys
= lib_interface_get_keys
,
1794 .lookup_entry
= lib_interface_lookup_entry
,
1798 .xpath
= "/frr-interface:lib/interface/description",
1800 .modify
= lib_interface_description_modify
,
1801 .destroy
= lib_interface_description_destroy
,
1802 .cli_show
= cli_show_interface_desc
,
1806 .xpath
= "/frr-interface:lib/interface/vrf",
1808 .get_elem
= lib_interface_vrf_get_elem
,
1812 .xpath
= "/frr-interface:lib/interface/state/if-index",
1814 .get_elem
= lib_interface_state_if_index_get_elem
,
1818 .xpath
= "/frr-interface:lib/interface/state/mtu",
1820 .get_elem
= lib_interface_state_mtu_get_elem
,
1824 .xpath
= "/frr-interface:lib/interface/state/mtu6",
1826 .get_elem
= lib_interface_state_mtu6_get_elem
,
1830 .xpath
= "/frr-interface:lib/interface/state/speed",
1832 .get_elem
= lib_interface_state_speed_get_elem
,
1836 .xpath
= "/frr-interface:lib/interface/state/metric",
1838 .get_elem
= lib_interface_state_metric_get_elem
,
1842 .xpath
= "/frr-interface:lib/interface/state/flags",
1844 .get_elem
= lib_interface_state_flags_get_elem
,
1848 .xpath
= "/frr-interface:lib/interface/state/type",
1850 .get_elem
= lib_interface_state_type_get_elem
,
1854 .xpath
= "/frr-interface:lib/interface/state/phy-address",
1856 .get_elem
= lib_interface_state_phy_address_get_elem
,