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
, IFDESC
, "Intf Desc");
42 DEFINE_MTYPE_STATIC(LIB
, CONNECTED
, "Connected");
43 DEFINE_MTYPE_STATIC(LIB
, NBR_CONNECTED
, "Neighbor Connected");
44 DEFINE_MTYPE(LIB
, CONNECTED_LABEL
, "Connected interface label");
45 DEFINE_MTYPE_STATIC(LIB
, IF_LINK_PARAMS
, "Informational Link Parameters");
47 static void if_set_name(struct interface
*ifp
, const char *name
);
48 static struct interface
*if_lookup_by_ifindex(ifindex_t ifindex
,
50 static struct interface
*if_lookup_by_index_all_vrf(ifindex_t ifindex
);
51 static int if_cmp_func(const struct interface
*, const struct interface
*);
52 static int if_cmp_index_func(const struct interface
*ifp1
,
53 const struct interface
*ifp2
);
54 RB_GENERATE(if_name_head
, interface
, name_entry
, if_cmp_func
);
55 RB_GENERATE(if_index_head
, interface
, index_entry
, if_cmp_index_func
);
57 DEFINE_QOBJ_TYPE(interface
);
59 DEFINE_HOOK(if_add
, (struct interface
* ifp
), (ifp
));
60 DEFINE_KOOH(if_del
, (struct interface
* ifp
), (ifp
));
62 static struct interface_master
{
63 int (*create_hook
)(struct interface
*ifp
);
64 int (*up_hook
)(struct interface
*ifp
);
65 int (*down_hook
)(struct interface
*ifp
);
66 int (*destroy_hook
)(struct interface
*ifp
);
67 } ifp_master
= { 0, };
69 /* Compare interface names, returning an integer greater than, equal to, or
70 * less than 0, (following the strcmp convention), according to the
71 * relationship between ifp1 and ifp2. Interface names consist of an
72 * alphabetic prefix and a numeric suffix. The primary sort key is
73 * lexicographic by name, and then numeric by number. No number sorts
74 * before all numbers. Examples: de0 < de1, de100 < fxp0 < xl0, devpty <
77 int if_cmp_name_func(const char *p1
, const char *p2
)
86 /* look up to any number */
87 l1
= strcspn(p1
, "0123456789");
88 l2
= strcspn(p2
, "0123456789");
90 /* name lengths are different -> compare names */
92 return (strcmp(p1
, p2
));
94 /* Note that this relies on all numbers being less than all
98 res
= strncmp(p1
, p2
, l1
);
100 /* names are different -> compare them */
104 /* with identical name part, go to numeric part */
115 x1
= strtol(p1
, (char **)&tmp1
, 10);
116 x2
= strtol(p2
, (char **)&tmp2
, 10);
118 /* let's compare numbers now */
124 /* Compare string if numbers are equal (distinguish foo-1 from foo-001) */
125 l1
= strspn(p1
, "0123456789");
126 l2
= strspn(p2
, "0123456789");
128 return (strcmp(p1
, p2
));
130 /* Continue to parse the rest of the string */
131 p1
= (const char *)tmp1
;
132 p2
= (const char *)tmp2
;
134 /* numbers were equal, lets do it again..
135 (it happens with name like "eth123.456:789") */
144 static int if_cmp_func(const struct interface
*ifp1
,
145 const struct interface
*ifp2
)
147 return if_cmp_name_func(ifp1
->name
, ifp2
->name
);
150 static int if_cmp_index_func(const struct interface
*ifp1
,
151 const struct interface
*ifp2
)
153 if (ifp1
->ifindex
== ifp2
->ifindex
)
155 else if (ifp1
->ifindex
> ifp2
->ifindex
)
161 static void ifp_connected_free(void *arg
)
163 struct connected
*c
= arg
;
168 /* Create new interface structure. */
169 static struct interface
*if_new(struct vrf
*vrf
)
171 struct interface
*ifp
;
175 ifp
= XCALLOC(MTYPE_IF
, sizeof(struct interface
));
177 ifp
->ifindex
= IFINDEX_INTERNAL
;
182 ifp
->connected
= list_new();
183 ifp
->connected
->del
= ifp_connected_free
;
185 ifp
->nbr_connected
= list_new();
186 ifp
->nbr_connected
->del
= (void (*)(void *))nbr_connected_free
;
188 /* Enable Link-detection by default */
189 SET_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
);
191 QOBJ_REG(ifp
, interface
);
195 void if_new_via_zapi(struct interface
*ifp
)
197 if (ifp_master
.create_hook
)
198 (*ifp_master
.create_hook
)(ifp
);
201 void if_destroy_via_zapi(struct interface
*ifp
)
203 if (ifp_master
.destroy_hook
)
204 (*ifp_master
.destroy_hook
)(ifp
);
206 ifp
->oldifindex
= ifp
->ifindex
;
207 if_set_index(ifp
, IFINDEX_INTERNAL
);
209 if (!ifp
->configured
)
213 void if_up_via_zapi(struct interface
*ifp
)
215 if (ifp_master
.up_hook
)
216 (*ifp_master
.up_hook
)(ifp
);
219 void if_down_via_zapi(struct interface
*ifp
)
221 if (ifp_master
.down_hook
)
222 (*ifp_master
.down_hook
)(ifp
);
225 static struct interface
*if_create_name(const char *name
, struct vrf
*vrf
)
227 struct interface
*ifp
;
231 if_set_name(ifp
, name
);
233 hook_call(if_add
, ifp
);
237 /* Create new interface structure. */
238 void if_update_to_new_vrf(struct interface
*ifp
, vrf_id_t vrf_id
)
240 struct vrf
*old_vrf
, *vrf
;
242 /* remove interface from old master vrf list */
245 if (ifp
->name
[0] != '\0')
246 IFNAME_RB_REMOVE(old_vrf
, ifp
);
248 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
249 IFINDEX_RB_REMOVE(old_vrf
, ifp
);
251 vrf
= vrf_get(vrf_id
, NULL
);
254 if (ifp
->name
[0] != '\0')
255 IFNAME_RB_INSERT(vrf
, ifp
);
257 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
258 IFINDEX_RB_INSERT(vrf
, ifp
);
262 /* Delete interface structure. */
263 void if_delete_retain(struct interface
*ifp
)
265 hook_call(if_del
, ifp
);
268 /* Free connected address list */
269 list_delete_all_node(ifp
->connected
);
271 /* Free connected nbr address list */
272 list_delete_all_node(ifp
->nbr_connected
);
275 /* Delete and free interface structure. */
276 void if_delete(struct interface
**ifp
)
278 struct interface
*ptr
= *ifp
;
279 struct vrf
*vrf
= ptr
->vrf
;
281 IFNAME_RB_REMOVE(vrf
, ptr
);
282 if (ptr
->ifindex
!= IFINDEX_INTERNAL
)
283 IFINDEX_RB_REMOVE(vrf
, ptr
);
285 if_delete_retain(ptr
);
287 list_delete(&ptr
->connected
);
288 list_delete(&ptr
->nbr_connected
);
290 if_link_params_free(ptr
);
292 XFREE(MTYPE_IFDESC
, ptr
->desc
);
294 XFREE(MTYPE_IF
, ptr
);
298 /* Used only internally to check within VRF only */
299 static struct interface
*if_lookup_by_ifindex(ifindex_t ifindex
,
303 struct interface if_tmp
;
305 vrf
= vrf_lookup_by_id(vrf_id
);
309 if_tmp
.ifindex
= ifindex
;
310 return RB_FIND(if_index_head
, &vrf
->ifaces_by_index
, &if_tmp
);
313 /* Interface existence check by index. */
314 struct interface
*if_lookup_by_index(ifindex_t ifindex
, vrf_id_t vrf_id
)
316 switch (vrf_get_backend()) {
317 case VRF_BACKEND_UNKNOWN
:
318 case VRF_BACKEND_NETNS
:
319 return(if_lookup_by_ifindex(ifindex
, vrf_id
));
320 case VRF_BACKEND_VRF_LITE
:
321 return(if_lookup_by_index_all_vrf(ifindex
));
326 /* Interface existence check by index. */
327 struct interface
*if_vrf_lookup_by_index_next(ifindex_t ifindex
,
330 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
331 struct interface
*tmp_ifp
;
338 tmp_ifp
= RB_MIN(if_index_head
, &vrf
->ifaces_by_index
);
339 /* skip the vrf interface */
340 if (tmp_ifp
&& if_is_vrf(tmp_ifp
))
341 ifindex
= tmp_ifp
->ifindex
;
346 RB_FOREACH (tmp_ifp
, if_index_head
, &vrf
->ifaces_by_index
) {
348 /* skip the vrf interface */
349 if (tmp_ifp
&& if_is_vrf(tmp_ifp
))
354 if (tmp_ifp
->ifindex
== ifindex
)
360 const char *ifindex2ifname(ifindex_t ifindex
, vrf_id_t vrf_id
)
362 struct interface
*ifp
;
364 return ((ifp
= if_lookup_by_index(ifindex
, vrf_id
)) != NULL
)
369 ifindex_t
ifname2ifindex(const char *name
, vrf_id_t vrf_id
)
371 struct interface
*ifp
;
373 return ((ifp
= if_lookup_by_name(name
, vrf_id
)) != NULL
)
378 /* Interface existence check by interface name. */
379 struct interface
*if_lookup_by_name(const char *name
, vrf_id_t vrf_id
)
381 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
382 struct interface if_tmp
;
385 || strnlen(name
, INTERFACE_NAMSIZ
) == INTERFACE_NAMSIZ
)
388 strlcpy(if_tmp
.name
, name
, sizeof(if_tmp
.name
));
389 return RB_FIND(if_name_head
, &vrf
->ifaces_by_name
, &if_tmp
);
392 struct interface
*if_lookup_by_name_vrf(const char *name
, struct vrf
*vrf
)
394 struct interface if_tmp
;
396 if (!name
|| strnlen(name
, INTERFACE_NAMSIZ
) == INTERFACE_NAMSIZ
)
399 strlcpy(if_tmp
.name
, name
, sizeof(if_tmp
.name
));
400 return RB_FIND(if_name_head
, &vrf
->ifaces_by_name
, &if_tmp
);
403 static struct interface
*if_lookup_by_name_all_vrf(const char *name
)
406 struct interface
*ifp
;
408 if (!name
|| strnlen(name
, INTERFACE_NAMSIZ
) == INTERFACE_NAMSIZ
)
411 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
412 ifp
= if_lookup_by_name_vrf(name
, vrf
);
420 static struct interface
*if_lookup_by_index_all_vrf(ifindex_t ifindex
)
423 struct interface
*ifp
;
425 if (ifindex
== IFINDEX_INTERNAL
)
428 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
429 ifp
= if_lookup_by_ifindex(ifindex
, vrf
->vrf_id
);
437 /* Lookup interface by IP address.
439 * supersedes if_lookup_exact_address(), which didn't care about up/down
440 * state. but all users we have either only care if the address is local
441 * (=> use if_address_is_local() please), or care about UP interfaces before
444 * to accept only UP interfaces, check if_is_up() on the returned ifp.
446 struct interface
*if_lookup_address_local(const void *src
, int family
,
449 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
450 struct listnode
*cnode
;
451 struct interface
*ifp
, *best_down
= NULL
;
455 if (family
!= AF_INET
&& family
!= AF_INET6
)
458 FOR_ALL_INTERFACES (vrf
, ifp
) {
459 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
462 if (!p
|| p
->family
!= family
)
465 if (family
== AF_INET
) {
466 if (!IPV4_ADDR_SAME(&p
->u
.prefix4
,
467 (struct in_addr
*)src
))
469 } else if (family
== AF_INET6
) {
470 if (!IPV6_ADDR_SAME(&p
->u
.prefix6
,
471 (struct in6_addr
*)src
))
484 /* Lookup interface by IP address. */
485 struct connected
*if_lookup_address(const void *matchaddr
, int family
,
488 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
491 struct listnode
*cnode
;
492 struct interface
*ifp
;
494 struct connected
*match
;
496 if (family
== AF_INET
) {
497 addr
.family
= AF_INET
;
498 addr
.u
.prefix4
= *((struct in_addr
*)matchaddr
);
499 addr
.prefixlen
= IPV4_MAX_BITLEN
;
500 } else if (family
== AF_INET6
) {
501 addr
.family
= AF_INET6
;
502 addr
.u
.prefix6
= *((struct in6_addr
*)matchaddr
);
503 addr
.prefixlen
= IPV6_MAX_BITLEN
;
505 assert(!"Attempted lookup of family not supported");
509 FOR_ALL_INTERFACES (vrf
, ifp
) {
510 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
511 if (c
->address
&& (c
->address
->family
== AF_INET
)
512 && prefix_match(CONNECTED_PREFIX(c
), &addr
)
513 && (c
->address
->prefixlen
> bestlen
)) {
514 bestlen
= c
->address
->prefixlen
;
522 /* Lookup interface by prefix */
523 struct interface
*if_lookup_prefix(const struct prefix
*prefix
, vrf_id_t vrf_id
)
525 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
526 struct listnode
*cnode
;
527 struct interface
*ifp
;
530 FOR_ALL_INTERFACES (vrf
, ifp
) {
531 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
532 if (prefix_cmp(c
->address
, prefix
) == 0) {
540 size_t if_lookup_by_hwaddr(const uint8_t *hw_addr
, size_t addrsz
,
541 struct interface
***result
, vrf_id_t vrf_id
)
543 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
545 struct list
*rs
= list_new();
546 struct interface
*ifp
;
548 FOR_ALL_INTERFACES (vrf
, ifp
) {
549 if (ifp
->hw_addr_len
== (int)addrsz
550 && !memcmp(hw_addr
, ifp
->hw_addr
, addrsz
))
551 listnode_add(rs
, ifp
);
555 *result
= XCALLOC(MTYPE_TMP
,
556 sizeof(struct interface
*) * rs
->count
);
557 list_to_array(rs
, (void **)*result
, rs
->count
);
560 int count
= rs
->count
;
567 /* Get the VRF loopback interface, i.e. the loopback on the default VRF
568 * or the VRF interface.
570 struct interface
*if_get_vrf_loopback(vrf_id_t vrf_id
)
572 struct interface
*ifp
= NULL
;
573 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
575 FOR_ALL_INTERFACES (vrf
, ifp
)
576 if (if_is_loopback(ifp
))
582 /* Get interface by name if given name interface doesn't exist create
585 struct interface
*if_get_by_name(const char *name
, vrf_id_t vrf_id
,
586 const char *vrf_name
)
588 struct interface
*ifp
= NULL
;
591 switch (vrf_get_backend()) {
592 case VRF_BACKEND_UNKNOWN
:
593 case VRF_BACKEND_NETNS
:
594 vrf
= vrf_get(vrf_id
, vrf_name
);
597 ifp
= if_lookup_by_name_vrf(name
, vrf
);
599 /* If it came from the kernel or by way of zclient,
600 * believe it and update the ifp accordingly.
602 if (ifp
->vrf
->vrf_id
!= vrf_id
&& vrf_id
!= VRF_UNKNOWN
)
603 if_update_to_new_vrf(ifp
, vrf_id
);
609 case VRF_BACKEND_VRF_LITE
:
610 ifp
= if_lookup_by_name_all_vrf(name
);
612 /* If it came from the kernel or by way of zclient,
613 * believe it and update the ifp accordingly.
615 if (ifp
->vrf
->vrf_id
!= vrf_id
&& vrf_id
!= VRF_UNKNOWN
)
616 if_update_to_new_vrf(ifp
, vrf_id
);
621 vrf
= vrf_get(vrf_id
, vrf_name
);
629 return if_create_name(name
, vrf
);
632 int if_set_index(struct interface
*ifp
, ifindex_t ifindex
)
634 if (ifp
->ifindex
== ifindex
)
638 * If there is already an interface with this ifindex, we will collide
639 * on insertion, so don't even try.
641 if (if_lookup_by_ifindex(ifindex
, ifp
->vrf
->vrf_id
))
644 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
645 IFINDEX_RB_REMOVE(ifp
->vrf
, ifp
);
647 ifp
->ifindex
= ifindex
;
649 if (ifp
->ifindex
!= IFINDEX_INTERNAL
) {
651 * This should never happen, since we checked if there was
652 * already an interface with the desired ifindex at the top of
653 * the function. Nevertheless.
655 if (IFINDEX_RB_INSERT(ifp
->vrf
, ifp
))
662 static void if_set_name(struct interface
*ifp
, const char *name
)
664 if (if_cmp_name_func(ifp
->name
, name
) == 0)
667 if (ifp
->name
[0] != '\0')
668 IFNAME_RB_REMOVE(ifp
->vrf
, ifp
);
670 strlcpy(ifp
->name
, name
, sizeof(ifp
->name
));
672 if (ifp
->name
[0] != '\0')
673 IFNAME_RB_INSERT(ifp
->vrf
, ifp
);
676 /* Does interface up ? */
677 int if_is_up(const struct interface
*ifp
)
679 return ifp
->flags
& IFF_UP
;
682 /* Is interface running? */
683 int if_is_running(const struct interface
*ifp
)
685 return ifp
->flags
& IFF_RUNNING
;
688 /* Is the interface operative, eg. either UP & RUNNING
689 or UP & !ZEBRA_INTERFACE_LINK_DETECTION and
690 if ptm checking is enabled, then ptm check has passed */
691 int if_is_operative(const struct interface
*ifp
)
693 return ((ifp
->flags
& IFF_UP
)
694 && (((ifp
->flags
& IFF_RUNNING
)
695 && (ifp
->ptm_status
|| !ifp
->ptm_enable
))
696 || !CHECK_FLAG(ifp
->status
,
697 ZEBRA_INTERFACE_LINKDETECTION
)));
700 /* Is the interface operative, eg. either UP & RUNNING
701 or UP & !ZEBRA_INTERFACE_LINK_DETECTION, without PTM check */
702 int if_is_no_ptm_operative(const struct interface
*ifp
)
704 return ((ifp
->flags
& IFF_UP
)
705 && ((ifp
->flags
& IFF_RUNNING
)
706 || !CHECK_FLAG(ifp
->status
,
707 ZEBRA_INTERFACE_LINKDETECTION
)));
710 /* Is this loopback interface ? */
711 int if_is_loopback_exact(const struct interface
*ifp
)
713 /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M
714 * but Y on platform N?
716 return (ifp
->flags
& (IFF_LOOPBACK
| IFF_NOXMIT
| IFF_VIRTUAL
));
719 /* Check interface is VRF */
720 int if_is_vrf(const struct interface
*ifp
)
722 return CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
725 /* Should this interface be treated as a loopback? */
726 bool if_is_loopback(const struct interface
*ifp
)
728 if (if_is_loopback_exact(ifp
) || if_is_vrf(ifp
))
734 /* Does this interface support broadcast ? */
735 int if_is_broadcast(const struct interface
*ifp
)
737 return ifp
->flags
& IFF_BROADCAST
;
740 /* Does this interface support pointopoint ? */
741 int if_is_pointopoint(const struct interface
*ifp
)
743 return ifp
->flags
& IFF_POINTOPOINT
;
746 /* Does this interface support multicast ? */
747 int if_is_multicast(const struct interface
*ifp
)
749 return ifp
->flags
& IFF_MULTICAST
;
752 /* Printout flag information into log */
753 const char *if_flag_dump(unsigned long flag
)
756 static char logbuf
[BUFSIZ
];
758 #define IFF_OUT_LOG(X, STR) \
761 strlcat(logbuf, ",", sizeof(logbuf)); \
764 strlcat(logbuf, STR, sizeof(logbuf)); \
767 strlcpy(logbuf
, "<", BUFSIZ
);
768 IFF_OUT_LOG(IFF_UP
, "UP");
769 IFF_OUT_LOG(IFF_BROADCAST
, "BROADCAST");
770 IFF_OUT_LOG(IFF_DEBUG
, "DEBUG");
771 IFF_OUT_LOG(IFF_LOOPBACK
, "LOOPBACK");
772 IFF_OUT_LOG(IFF_POINTOPOINT
, "POINTOPOINT");
773 IFF_OUT_LOG(IFF_NOTRAILERS
, "NOTRAILERS");
774 IFF_OUT_LOG(IFF_RUNNING
, "RUNNING");
775 IFF_OUT_LOG(IFF_NOARP
, "NOARP");
776 IFF_OUT_LOG(IFF_PROMISC
, "PROMISC");
777 IFF_OUT_LOG(IFF_ALLMULTI
, "ALLMULTI");
778 IFF_OUT_LOG(IFF_OACTIVE
, "OACTIVE");
779 IFF_OUT_LOG(IFF_SIMPLEX
, "SIMPLEX");
780 IFF_OUT_LOG(IFF_LINK0
, "LINK0");
781 IFF_OUT_LOG(IFF_LINK1
, "LINK1");
782 IFF_OUT_LOG(IFF_LINK2
, "LINK2");
783 IFF_OUT_LOG(IFF_MULTICAST
, "MULTICAST");
784 IFF_OUT_LOG(IFF_NOXMIT
, "NOXMIT");
785 IFF_OUT_LOG(IFF_NORTEXCH
, "NORTEXCH");
786 IFF_OUT_LOG(IFF_VIRTUAL
, "VIRTUAL");
787 IFF_OUT_LOG(IFF_IPV4
, "IPv4");
788 IFF_OUT_LOG(IFF_IPV6
, "IPv6");
790 strlcat(logbuf
, ">", sizeof(logbuf
));
797 static void if_dump(const struct interface
*ifp
)
799 struct listnode
*node
;
800 struct connected
*c
__attribute__((unused
));
802 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, c
))
804 "Interface %s vrf %s(%u) index %d metric %d mtu %d mtu6 %d %s",
805 ifp
->name
, ifp
->vrf
->name
, ifp
->vrf
->vrf_id
,
806 ifp
->ifindex
, ifp
->metric
, ifp
->mtu
, ifp
->mtu6
,
807 if_flag_dump(ifp
->flags
));
810 /* Interface printing for all interface. */
811 void if_dump_all(void)
816 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
817 FOR_ALL_INTERFACES (vrf
, ifp
)
821 /* Allocate connected structure. */
822 struct connected
*connected_new(void)
824 return XCALLOC(MTYPE_CONNECTED
, sizeof(struct connected
));
827 /* Allocate nbr connected structure. */
828 struct nbr_connected
*nbr_connected_new(void)
830 return XCALLOC(MTYPE_NBR_CONNECTED
, sizeof(struct nbr_connected
));
833 /* Free connected structure. */
834 void connected_free(struct connected
**connected
)
836 struct connected
*ptr
= *connected
;
838 prefix_free(&ptr
->address
);
839 prefix_free(&ptr
->destination
);
841 XFREE(MTYPE_CONNECTED_LABEL
, ptr
->label
);
843 XFREE(MTYPE_CONNECTED
, ptr
);
847 /* Free nbr connected structure. */
848 void nbr_connected_free(struct nbr_connected
*connected
)
850 if (connected
->address
)
851 prefix_free(&connected
->address
);
853 XFREE(MTYPE_NBR_CONNECTED
, connected
);
856 /* If same interface nbr address already exists... */
857 struct nbr_connected
*nbr_connected_check(struct interface
*ifp
,
860 struct nbr_connected
*ifc
;
861 struct listnode
*node
;
863 for (ALL_LIST_ELEMENTS_RO(ifp
->nbr_connected
, node
, ifc
))
864 if (prefix_same(ifc
->address
, p
))
870 /* Print if_addr structure. */
871 static void __attribute__((unused
))
872 connected_log(struct connected
*connected
, char *str
)
875 struct interface
*ifp
;
879 ifp
= connected
->ifp
;
880 p
= connected
->address
;
882 snprintf(logbuf
, sizeof(logbuf
), "%s interface %s vrf %s(%u) %s %pFX ",
883 str
, ifp
->name
, ifp
->vrf
->name
, ifp
->vrf
->vrf_id
,
884 prefix_family_str(p
), p
);
886 p
= connected
->destination
;
888 strlcat(logbuf
, inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
891 zlog_info("%s", logbuf
);
894 /* Print if_addr structure. */
895 static void __attribute__((unused
))
896 nbr_connected_log(struct nbr_connected
*connected
, char *str
)
899 struct interface
*ifp
;
902 ifp
= connected
->ifp
;
903 p
= connected
->address
;
905 snprintf(logbuf
, sizeof(logbuf
), "%s interface %s %s %pFX ", str
,
906 ifp
->name
, prefix_family_str(p
), p
);
908 zlog_info("%s", logbuf
);
911 /* If two connected address has same prefix return 1. */
912 static int connected_same_prefix(const struct prefix
*p1
,
913 const struct prefix
*p2
)
915 if (p1
->family
== p2
->family
) {
916 if (p1
->family
== AF_INET
917 && IPV4_ADDR_SAME(&p1
->u
.prefix4
, &p2
->u
.prefix4
))
919 if (p1
->family
== AF_INET6
920 && IPV6_ADDR_SAME(&p1
->u
.prefix6
, &p2
->u
.prefix6
))
926 /* count the number of connected addresses that are in the given family */
927 unsigned int connected_count_by_family(struct interface
*ifp
, int family
)
929 struct listnode
*cnode
;
930 struct connected
*connected
;
931 unsigned int cnt
= 0;
933 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
))
934 if (connected
->address
->family
== family
)
940 struct connected
*connected_lookup_prefix_exact(struct interface
*ifp
,
941 const struct prefix
*p
)
943 struct listnode
*node
;
944 struct listnode
*next
;
945 struct connected
*ifc
;
947 for (node
= listhead(ifp
->connected
); node
; node
= next
) {
948 ifc
= listgetdata(node
);
951 if (connected_same_prefix(ifc
->address
, p
))
957 struct connected
*connected_delete_by_prefix(struct interface
*ifp
,
960 struct listnode
*node
;
961 struct listnode
*next
;
962 struct connected
*ifc
;
964 /* In case of same prefix come, replace it with new one. */
965 for (node
= listhead(ifp
->connected
); node
; node
= next
) {
966 ifc
= listgetdata(node
);
969 if (connected_same_prefix(ifc
->address
, p
)) {
970 listnode_delete(ifp
->connected
, ifc
);
977 /* Find the address on our side that will be used when packets
979 struct connected
*connected_lookup_prefix(struct interface
*ifp
,
980 const struct prefix
*addr
)
982 struct listnode
*cnode
;
984 struct connected
*match
;
988 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
989 if (c
->address
&& (c
->address
->family
== addr
->family
)
990 && prefix_match(CONNECTED_PREFIX(c
), addr
)
992 || (c
->address
->prefixlen
> match
->address
->prefixlen
)))
998 struct connected
*connected_add_by_prefix(struct interface
*ifp
,
1000 struct prefix
*destination
)
1002 struct connected
*ifc
;
1004 /* Allocate new connected address. */
1005 ifc
= connected_new();
1008 /* Fetch interface address */
1009 ifc
->address
= prefix_new();
1010 memcpy(ifc
->address
, p
, sizeof(struct prefix
));
1012 /* Fetch dest address */
1014 ifc
->destination
= prefix_new();
1015 memcpy(ifc
->destination
, destination
, sizeof(struct prefix
));
1018 /* Add connected address to the interface. */
1019 listnode_add(ifp
->connected
, ifc
);
1023 struct connected
*connected_get_linklocal(struct interface
*ifp
)
1026 struct connected
*c
= NULL
;
1028 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, n
, c
)) {
1029 if (c
->address
->family
== AF_INET6
1030 && IN6_IS_ADDR_LINKLOCAL(&c
->address
->u
.prefix6
))
1036 void if_terminate(struct vrf
*vrf
)
1038 struct interface
*ifp
;
1040 while (!RB_EMPTY(if_name_head
, &vrf
->ifaces_by_name
)) {
1041 ifp
= RB_ROOT(if_name_head
, &vrf
->ifaces_by_name
);
1044 ifp
->node
->info
= NULL
;
1045 route_unlock_node(ifp
->node
);
1051 const char *if_link_type_str(enum zebra_link_type llt
)
1054 #define llts(T,S) case (T): return (S)
1055 llts(ZEBRA_LLT_UNKNOWN
, "Unknown");
1056 llts(ZEBRA_LLT_ETHER
, "Ethernet");
1057 llts(ZEBRA_LLT_EETHER
, "Experimental Ethernet");
1058 llts(ZEBRA_LLT_AX25
, "AX.25 Level 2");
1059 llts(ZEBRA_LLT_PRONET
, "PROnet token ring");
1060 llts(ZEBRA_LLT_IEEE802
, "IEEE 802.2 Ethernet/TR/TB");
1061 llts(ZEBRA_LLT_ARCNET
, "ARCnet");
1062 llts(ZEBRA_LLT_APPLETLK
, "AppleTalk");
1063 llts(ZEBRA_LLT_DLCI
, "Frame Relay DLCI");
1064 llts(ZEBRA_LLT_ATM
, "ATM");
1065 llts(ZEBRA_LLT_METRICOM
, "Metricom STRIP");
1066 llts(ZEBRA_LLT_IEEE1394
, "IEEE 1394 IPv4");
1067 llts(ZEBRA_LLT_EUI64
, "EUI-64");
1068 llts(ZEBRA_LLT_INFINIBAND
, "InfiniBand");
1069 llts(ZEBRA_LLT_SLIP
, "SLIP");
1070 llts(ZEBRA_LLT_CSLIP
, "Compressed SLIP");
1071 llts(ZEBRA_LLT_SLIP6
, "SLIPv6");
1072 llts(ZEBRA_LLT_CSLIP6
, "Compressed SLIPv6");
1073 llts(ZEBRA_LLT_RSRVD
, "Reserved");
1074 llts(ZEBRA_LLT_ADAPT
, "Adapt");
1075 llts(ZEBRA_LLT_ROSE
, "ROSE packet radio");
1076 llts(ZEBRA_LLT_X25
, "CCITT X.25");
1077 llts(ZEBRA_LLT_PPP
, "PPP");
1078 llts(ZEBRA_LLT_CHDLC
, "Cisco HDLC");
1079 llts(ZEBRA_LLT_RAWHDLC
, "Raw HDLC");
1080 llts(ZEBRA_LLT_LAPB
, "LAPB");
1081 llts(ZEBRA_LLT_IPIP
, "IPIP Tunnel");
1082 llts(ZEBRA_LLT_IPIP6
, "IPIP6 Tunnel");
1083 llts(ZEBRA_LLT_FRAD
, "FRAD");
1084 llts(ZEBRA_LLT_SKIP
, "SKIP vif");
1085 llts(ZEBRA_LLT_LOOPBACK
, "Loopback");
1086 llts(ZEBRA_LLT_LOCALTLK
, "Localtalk");
1087 llts(ZEBRA_LLT_FDDI
, "FDDI");
1088 llts(ZEBRA_LLT_SIT
, "IPv6-in-IPv4 SIT");
1089 llts(ZEBRA_LLT_IPDDP
, "IP-in-DDP tunnel");
1090 llts(ZEBRA_LLT_IPGRE
, "GRE over IP");
1091 llts(ZEBRA_LLT_IP6GRE
, "GRE over IPv6");
1092 llts(ZEBRA_LLT_PIMREG
, "PIMSM registration");
1093 llts(ZEBRA_LLT_HIPPI
, "HiPPI");
1094 llts(ZEBRA_LLT_ECONET
, "Acorn Econet");
1095 llts(ZEBRA_LLT_IRDA
, "IrDA");
1096 llts(ZEBRA_LLT_FCPP
, "Fibre-Channel PtP");
1097 llts(ZEBRA_LLT_FCAL
, "Fibre-Channel Arbitrated Loop");
1098 llts(ZEBRA_LLT_FCPL
, "Fibre-Channel Public Loop");
1099 llts(ZEBRA_LLT_FCFABRIC
, "Fibre-Channel Fabric");
1100 llts(ZEBRA_LLT_IEEE802_TR
, "IEEE 802.2 Token Ring");
1101 llts(ZEBRA_LLT_IEEE80211
, "IEEE 802.11");
1102 llts(ZEBRA_LLT_IEEE80211_RADIOTAP
, "IEEE 802.11 Radiotap");
1103 llts(ZEBRA_LLT_IEEE802154
, "IEEE 802.15.4");
1104 llts(ZEBRA_LLT_IEEE802154_PHY
, "IEEE 802.15.4 Phy");
1110 struct if_link_params
*if_link_params_get(struct interface
*ifp
)
1112 return ifp
->link_params
;
1115 struct if_link_params
*if_link_params_enable(struct interface
*ifp
)
1117 struct if_link_params
*iflp
;
1120 iflp
= if_link_params_init(ifp
);
1122 /* Compute default bandwidth based on interface */
1124 ((ifp
->bandwidth
? ifp
->bandwidth
: DEFAULT_BANDWIDTH
)
1125 * TE_MEGA_BIT
/ TE_BYTE
);
1127 /* Set Max, Reservable and Unreserved Bandwidth */
1128 iflp
->max_bw
= iflp
->default_bw
;
1129 iflp
->max_rsv_bw
= iflp
->default_bw
;
1130 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1131 iflp
->unrsv_bw
[i
] = iflp
->default_bw
;
1133 /* Update Link parameters status */
1134 iflp
->lp_status
= LP_MAX_BW
| LP_MAX_RSV_BW
| LP_UNRSV_BW
;
1136 /* Set TE metric equal to standard metric only if it is set */
1137 if (ifp
->metric
!= 0) {
1138 iflp
->te_metric
= ifp
->metric
;
1139 iflp
->lp_status
|= LP_TE_METRIC
;
1142 /* Finally attach newly created Link Parameters */
1143 ifp
->link_params
= iflp
;
1148 struct if_link_params
*if_link_params_init(struct interface
*ifp
)
1150 struct if_link_params
*iflp
= if_link_params_get(ifp
);
1155 iflp
= XCALLOC(MTYPE_IF_LINK_PARAMS
, sizeof(struct if_link_params
));
1157 ifp
->link_params
= iflp
;
1162 void if_link_params_free(struct interface
*ifp
)
1164 XFREE(MTYPE_IF_LINK_PARAMS
, ifp
->link_params
);
1167 /* ----------- CLI commands ----------- */
1169 /* Guess the VRF of an interface. */
1170 static int vrfname_by_ifname(const char *ifname
, const char **vrfname
)
1173 struct interface
*ifp
;
1176 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
1177 FOR_ALL_INTERFACES (vrf
, ifp
) {
1178 if (strmatch(ifp
->name
, ifname
)) {
1179 *vrfname
= vrf
->name
;
1189 * XPath: /frr-interface:lib/interface
1191 DEFPY_YANG_NOSH (interface
,
1193 "interface IFNAME [vrf NAME$vrf_name]",
1194 "Select an interface to configure\n"
1195 "Interface's name\n"
1198 char xpath_list
[XPATH_MAXLEN
];
1199 struct interface
*ifp
;
1203 if (vrf_is_backend_netns()) {
1205 * For backward compatibility, if the VRF name is not specified
1206 * and there is exactly one interface with this name in the
1207 * system, use its VRF. Otherwise fallback to the default VRF.
1210 count
= vrfname_by_ifname(ifname
, &vrf_name
);
1212 vrf_name
= VRF_DEFAULT_NAME
;
1215 snprintf(xpath_list
, XPATH_MAXLEN
,
1216 "/frr-interface:lib/interface[name='%s:%s']", vrf_name
,
1219 snprintf(xpath_list
, XPATH_MAXLEN
,
1220 "/frr-interface:lib/interface[name='%s']", ifname
);
1223 nb_cli_enqueue_change(vty
, ".", NB_OP_CREATE
, NULL
);
1224 ret
= nb_cli_apply_changes_clear_pending(vty
, xpath_list
);
1225 if (ret
== CMD_SUCCESS
) {
1226 VTY_PUSH_XPATH(INTERFACE_NODE
, xpath_list
);
1229 * For backward compatibility with old commands we still need
1230 * to use the qobj infrastructure. This can be removed once
1231 * all interface-level commands are converted to the new
1234 if (vrf_is_backend_netns()) {
1235 vrf
= vrf_lookup_by_name(vrf_name
);
1237 ifp
= if_lookup_by_name_vrf(ifname
, vrf
);
1241 ifp
= if_lookup_by_name_all_vrf(ifname
);
1244 VTY_PUSH_CONTEXT(INTERFACE_NODE
, ifp
);
1250 DEFPY_YANG (no_interface
,
1252 "no interface IFNAME [vrf NAME$vrf_name]",
1254 "Delete a pseudo interface's configuration\n"
1255 "Interface's name\n"
1258 char xpath_list
[XPATH_MAXLEN
];
1261 if (vrf_is_backend_netns()) {
1263 * For backward compatibility, if the VRF name is not specified
1264 * and there is exactly one interface with this name in the
1265 * system, use its VRF. Otherwise fallback to the default VRF.
1268 count
= vrfname_by_ifname(ifname
, &vrf_name
);
1270 vrf_name
= VRF_DEFAULT_NAME
;
1273 snprintf(xpath_list
, XPATH_MAXLEN
,
1274 "/frr-interface:lib/interface[name='%s:%s']", vrf_name
,
1277 snprintf(xpath_list
, XPATH_MAXLEN
,
1278 "/frr-interface:lib/interface[name='%s']", ifname
);
1281 nb_cli_enqueue_change(vty
, ".", NB_OP_DESTROY
, NULL
);
1283 return nb_cli_apply_changes(vty
, xpath_list
);
1286 static void netns_ifname_split(const char *xpath
, char *ifname
, char *vrfname
)
1291 assert(vrf_is_backend_netns());
1293 delim
= strchr(xpath
, ':');
1296 len
= delim
- xpath
;
1297 memcpy(vrfname
, xpath
, len
);
1300 strlcpy(ifname
, delim
+ 1, XPATH_MAXLEN
);
1303 static void cli_show_interface(struct vty
*vty
, const struct lyd_node
*dnode
,
1306 vty_out(vty
, "!\n");
1308 if (vrf_is_backend_netns()) {
1309 char ifname
[XPATH_MAXLEN
];
1310 char vrfname
[XPATH_MAXLEN
];
1312 netns_ifname_split(yang_dnode_get_string(dnode
, "./name"),
1315 vty_out(vty
, "interface %s", ifname
);
1316 if (!strmatch(vrfname
, VRF_DEFAULT_NAME
))
1317 vty_out(vty
, " vrf %s", vrfname
);
1319 const char *ifname
= yang_dnode_get_string(dnode
, "./name");
1321 vty_out(vty
, "interface %s", ifname
);
1327 static void cli_show_interface_end(struct vty
*vty
,
1328 const struct lyd_node
*dnode
)
1330 vty_out(vty
, "exit\n");
1333 void if_vty_config_start(struct vty
*vty
, struct interface
*ifp
)
1335 vty_frame(vty
, "!\n");
1336 vty_frame(vty
, "interface %s", ifp
->name
);
1338 if (vrf_is_backend_netns() && strcmp(ifp
->vrf
->name
, VRF_DEFAULT_NAME
))
1339 vty_frame(vty
, " vrf %s", ifp
->vrf
->name
);
1341 vty_frame(vty
, "\n");
1344 void if_vty_config_end(struct vty
*vty
)
1346 vty_endframe(vty
, "exit\n!\n");
1350 * XPath: /frr-interface:lib/interface/description
1352 DEFPY_YANG (interface_desc
,
1354 "description LINE...",
1355 "Interface specific description\n"
1356 "Characters describing this interface\n")
1361 desc
= argv_concat(argv
, argc
, 1);
1362 nb_cli_enqueue_change(vty
, "./description", NB_OP_MODIFY
, desc
);
1363 ret
= nb_cli_apply_changes(vty
, NULL
);
1364 XFREE(MTYPE_TMP
, desc
);
1369 DEFPY_YANG (no_interface_desc
,
1370 no_interface_desc_cmd
,
1373 "Interface specific description\n")
1375 nb_cli_enqueue_change(vty
, "./description", NB_OP_DESTROY
, NULL
);
1377 return nb_cli_apply_changes(vty
, NULL
);
1380 static void cli_show_interface_desc(struct vty
*vty
,
1381 const struct lyd_node
*dnode
,
1384 vty_out(vty
, " description %s\n", yang_dnode_get_string(dnode
, NULL
));
1387 /* Interface autocomplete. */
1388 static void if_autocomplete(vector comps
, struct cmd_token
*token
)
1390 struct interface
*ifp
;
1393 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
1394 FOR_ALL_INTERFACES (vrf
, ifp
) {
1395 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, ifp
->name
));
1400 static const struct cmd_variable_handler if_var_handlers
[] = {
1401 {/* "interface NAME" */
1402 .varname
= "interface",
1403 .completions
= if_autocomplete
},
1404 {.tokenname
= "IFNAME", .completions
= if_autocomplete
},
1405 {.tokenname
= "INTERFACE", .completions
= if_autocomplete
},
1406 {.completions
= NULL
}};
1408 static struct cmd_node interface_node
= {
1409 .name
= "interface",
1410 .node
= INTERFACE_NODE
,
1411 .parent_node
= CONFIG_NODE
,
1412 .prompt
= "%s(config-if)# ",
1415 static int if_config_write_single(const struct lyd_node
*dnode
, void *arg
)
1417 nb_cli_show_dnode_cmds(arg
, dnode
, false);
1419 return YANG_ITER_CONTINUE
;
1422 static int if_nb_config_write(struct vty
*vty
)
1424 yang_dnode_iterate(if_config_write_single
, vty
, running_config
->dnode
,
1425 "/frr-interface:lib/interface");
1429 void if_cmd_init(int (*config_write
)(struct vty
*))
1431 cmd_variable_handler_register(if_var_handlers
);
1433 interface_node
.config_write
= config_write
;
1434 install_node(&interface_node
);
1436 install_element(CONFIG_NODE
, &interface_cmd
);
1437 install_element(CONFIG_NODE
, &no_interface_cmd
);
1439 install_default(INTERFACE_NODE
);
1440 install_element(INTERFACE_NODE
, &interface_desc_cmd
);
1441 install_element(INTERFACE_NODE
, &no_interface_desc_cmd
);
1444 void if_cmd_init_default(void)
1446 if_cmd_init(if_nb_config_write
);
1449 void if_zapi_callbacks(int (*create
)(struct interface
*ifp
),
1450 int (*up
)(struct interface
*ifp
),
1451 int (*down
)(struct interface
*ifp
),
1452 int (*destroy
)(struct interface
*ifp
))
1454 ifp_master
.create_hook
= create
;
1455 ifp_master
.up_hook
= up
;
1456 ifp_master
.down_hook
= down
;
1457 ifp_master
.destroy_hook
= destroy
;
1460 /* ------- Northbound callbacks ------- */
1463 * XPath: /frr-interface:lib/interface
1465 static int lib_interface_create(struct nb_cb_create_args
*args
)
1468 struct interface
*ifp
;
1470 ifname
= yang_dnode_get_string(args
->dnode
, "./name");
1472 switch (args
->event
) {
1473 case NB_EV_VALIDATE
:
1474 if (vrf_is_backend_netns()) {
1475 char ifname_ns
[XPATH_MAXLEN
];
1476 char vrfname_ns
[XPATH_MAXLEN
];
1478 netns_ifname_split(ifname
, ifname_ns
, vrfname_ns
);
1480 if (strlen(ifname_ns
) > 16) {
1482 args
->errmsg
, args
->errmsg_len
,
1483 "Maximum interface name length is 16 characters");
1484 return NB_ERR_VALIDATION
;
1486 if (strlen(vrfname_ns
) > 36) {
1488 args
->errmsg
, args
->errmsg_len
,
1489 "Maximum VRF name length is 36 characters");
1490 return NB_ERR_VALIDATION
;
1493 if (strlen(ifname
) > 16) {
1495 args
->errmsg
, args
->errmsg_len
,
1496 "Maximum interface name length is 16 characters");
1497 return NB_ERR_VALIDATION
;
1505 if (vrf_is_backend_netns()) {
1506 char ifname_ns
[XPATH_MAXLEN
];
1507 char vrfname_ns
[XPATH_MAXLEN
];
1509 netns_ifname_split(ifname
, ifname_ns
, vrfname_ns
);
1511 ifp
= if_get_by_name(ifname_ns
, VRF_UNKNOWN
,
1514 ifp
= if_get_by_name(ifname
, VRF_UNKNOWN
,
1518 ifp
->configured
= true;
1519 nb_running_set_entry(args
->dnode
, ifp
);
1526 static int lib_interface_destroy(struct nb_cb_destroy_args
*args
)
1528 struct interface
*ifp
;
1531 switch (args
->event
) {
1532 case NB_EV_VALIDATE
:
1533 ifp
= nb_running_get_entry(args
->dnode
, NULL
, true);
1534 if (CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
)) {
1535 snprintf(args
->errmsg
, args
->errmsg_len
,
1536 "only inactive interfaces can be deleted");
1537 return NB_ERR_VALIDATION
;
1544 ifp
= nb_running_unset_entry(args
->dnode
);
1547 ifp
->configured
= false;
1550 if (!vrf_is_enabled(vrf
))
1559 * XPath: /frr-interface:lib/interface
1561 static const void *lib_interface_get_next(struct nb_cb_get_next_args
*args
)
1564 struct interface
*pif
= (struct interface
*)args
->list_entry
;
1566 if (args
->list_entry
== NULL
) {
1567 vrf
= RB_MIN(vrf_name_head
, &vrfs_by_name
);
1569 pif
= RB_MIN(if_name_head
, &vrf
->ifaces_by_name
);
1572 pif
= RB_NEXT(if_name_head
, pif
);
1573 /* if no more interfaces, switch to next vrf */
1574 while (pif
== NULL
) {
1575 vrf
= RB_NEXT(vrf_name_head
, vrf
);
1578 pif
= RB_MIN(if_name_head
, &vrf
->ifaces_by_name
);
1585 static int lib_interface_get_keys(struct nb_cb_get_keys_args
*args
)
1587 const struct interface
*ifp
= args
->list_entry
;
1589 args
->keys
->num
= 1;
1591 if (vrf_is_backend_netns())
1592 snprintf(args
->keys
->key
[0], sizeof(args
->keys
->key
[0]),
1593 "%s:%s", ifp
->vrf
->name
, ifp
->name
);
1595 snprintf(args
->keys
->key
[0], sizeof(args
->keys
->key
[0]), "%s",
1602 lib_interface_lookup_entry(struct nb_cb_lookup_entry_args
*args
)
1604 if (vrf_is_backend_netns()) {
1605 char ifname
[XPATH_MAXLEN
];
1606 char vrfname
[XPATH_MAXLEN
];
1609 netns_ifname_split(args
->keys
->key
[0], ifname
, vrfname
);
1611 vrf
= vrf_lookup_by_name(vrfname
);
1613 return vrf
? if_lookup_by_name(ifname
, vrf
->vrf_id
) : NULL
;
1615 return if_lookup_by_name_all_vrf(args
->keys
->key
[0]);
1620 * XPath: /frr-interface:lib/interface/description
1622 static int lib_interface_description_modify(struct nb_cb_modify_args
*args
)
1624 struct interface
*ifp
;
1625 const char *description
;
1627 if (args
->event
!= NB_EV_APPLY
)
1630 ifp
= nb_running_get_entry(args
->dnode
, NULL
, true);
1631 XFREE(MTYPE_IFDESC
, ifp
->desc
);
1632 description
= yang_dnode_get_string(args
->dnode
, NULL
);
1633 ifp
->desc
= XSTRDUP(MTYPE_IFDESC
, description
);
1638 static int lib_interface_description_destroy(struct nb_cb_destroy_args
*args
)
1640 struct interface
*ifp
;
1642 if (args
->event
!= NB_EV_APPLY
)
1645 ifp
= nb_running_get_entry(args
->dnode
, NULL
, true);
1646 XFREE(MTYPE_IFDESC
, ifp
->desc
);
1652 * XPath: /frr-interface:lib/interface/vrf
1654 static struct yang_data
*
1655 lib_interface_vrf_get_elem(struct nb_cb_get_elem_args
*args
)
1657 const struct interface
*ifp
= args
->list_entry
;
1659 return yang_data_new_string(args
->xpath
, ifp
->vrf
->name
);
1663 * XPath: /frr-interface:lib/interface/state/if-index
1665 static struct yang_data
*
1666 lib_interface_state_if_index_get_elem(struct nb_cb_get_elem_args
*args
)
1668 const struct interface
*ifp
= args
->list_entry
;
1670 return yang_data_new_int32(args
->xpath
, ifp
->ifindex
);
1674 * XPath: /frr-interface:lib/interface/state/mtu
1676 static struct yang_data
*
1677 lib_interface_state_mtu_get_elem(struct nb_cb_get_elem_args
*args
)
1679 const struct interface
*ifp
= args
->list_entry
;
1681 return yang_data_new_uint16(args
->xpath
, ifp
->mtu
);
1685 * XPath: /frr-interface:lib/interface/state/mtu6
1687 static struct yang_data
*
1688 lib_interface_state_mtu6_get_elem(struct nb_cb_get_elem_args
*args
)
1690 const struct interface
*ifp
= args
->list_entry
;
1692 return yang_data_new_uint32(args
->xpath
, ifp
->mtu6
);
1696 * XPath: /frr-interface:lib/interface/state/speed
1698 static struct yang_data
*
1699 lib_interface_state_speed_get_elem(struct nb_cb_get_elem_args
*args
)
1701 const struct interface
*ifp
= args
->list_entry
;
1703 return yang_data_new_uint32(args
->xpath
, ifp
->speed
);
1707 * XPath: /frr-interface:lib/interface/state/metric
1709 static struct yang_data
*
1710 lib_interface_state_metric_get_elem(struct nb_cb_get_elem_args
*args
)
1712 const struct interface
*ifp
= args
->list_entry
;
1714 return yang_data_new_uint32(args
->xpath
, ifp
->metric
);
1718 * XPath: /frr-interface:lib/interface/state/flags
1720 static struct yang_data
*
1721 lib_interface_state_flags_get_elem(struct nb_cb_get_elem_args
*args
)
1723 /* TODO: implement me. */
1728 * XPath: /frr-interface:lib/interface/state/type
1730 static struct yang_data
*
1731 lib_interface_state_type_get_elem(struct nb_cb_get_elem_args
*args
)
1733 /* TODO: implement me. */
1738 * XPath: /frr-interface:lib/interface/state/phy-address
1740 static struct yang_data
*
1741 lib_interface_state_phy_address_get_elem(struct nb_cb_get_elem_args
*args
)
1743 const struct interface
*ifp
= args
->list_entry
;
1744 struct ethaddr macaddr
;
1746 memcpy(&macaddr
.octet
, ifp
->hw_addr
, ETH_ALEN
);
1748 return yang_data_new_mac(args
->xpath
, &macaddr
);
1751 /* clang-format off */
1752 const struct frr_yang_module_info frr_interface_info
= {
1753 .name
= "frr-interface",
1756 .xpath
= "/frr-interface:lib/interface",
1758 .create
= lib_interface_create
,
1759 .destroy
= lib_interface_destroy
,
1760 .cli_show
= cli_show_interface
,
1761 .cli_show_end
= cli_show_interface_end
,
1762 .get_next
= lib_interface_get_next
,
1763 .get_keys
= lib_interface_get_keys
,
1764 .lookup_entry
= lib_interface_lookup_entry
,
1768 .xpath
= "/frr-interface:lib/interface/description",
1770 .modify
= lib_interface_description_modify
,
1771 .destroy
= lib_interface_description_destroy
,
1772 .cli_show
= cli_show_interface_desc
,
1776 .xpath
= "/frr-interface:lib/interface/vrf",
1778 .get_elem
= lib_interface_vrf_get_elem
,
1782 .xpath
= "/frr-interface:lib/interface/state/if-index",
1784 .get_elem
= lib_interface_state_if_index_get_elem
,
1788 .xpath
= "/frr-interface:lib/interface/state/mtu",
1790 .get_elem
= lib_interface_state_mtu_get_elem
,
1794 .xpath
= "/frr-interface:lib/interface/state/mtu6",
1796 .get_elem
= lib_interface_state_mtu6_get_elem
,
1800 .xpath
= "/frr-interface:lib/interface/state/speed",
1802 .get_elem
= lib_interface_state_speed_get_elem
,
1806 .xpath
= "/frr-interface:lib/interface/state/metric",
1808 .get_elem
= lib_interface_state_metric_get_elem
,
1812 .xpath
= "/frr-interface:lib/interface/state/flags",
1814 .get_elem
= lib_interface_state_flags_get_elem
,
1818 .xpath
= "/frr-interface:lib/interface/state/type",
1820 .get_elem
= lib_interface_state_type_get_elem
,
1824 .xpath
= "/frr-interface:lib/interface/state/phy-address",
1826 .get_elem
= lib_interface_state_phy_address_get_elem
,