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 #ifndef VTYSH_EXTRACT_PL
39 #include "lib/if_clippy.c"
42 DEFINE_MTYPE_STATIC(LIB
, IF
, "Interface")
43 DEFINE_MTYPE_STATIC(LIB
, CONNECTED
, "Connected")
44 DEFINE_MTYPE_STATIC(LIB
, NBR_CONNECTED
, "Neighbor Connected")
45 DEFINE_MTYPE(LIB
, CONNECTED_LABEL
, "Connected interface label")
46 DEFINE_MTYPE_STATIC(LIB
, IF_LINK_PARAMS
, "Informational Link Parameters")
48 static struct interface
*if_lookup_by_ifindex(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
)
83 /* look up to any number */
84 l1
= strcspn(p1
, "0123456789");
85 l2
= strcspn(p2
, "0123456789");
87 /* name lengths are different -> compare names */
89 return (strcmp(p1
, p2
));
91 /* Note that this relies on all numbers being less than all
95 res
= strncmp(p1
, p2
, l1
);
97 /* names are different -> compare them */
101 /* with identical name part, go to numeric part */
112 x1
= strtol(p1
, (char **)&p1
, 10);
113 x2
= strtol(p2
, (char **)&p2
, 10);
115 /* let's compare numbers now */
121 /* numbers were equal, lets do it again..
122 (it happens with name like "eth123.456:789") */
131 static int if_cmp_func(const struct interface
*ifp1
,
132 const struct interface
*ifp2
)
134 return if_cmp_name_func(ifp1
->name
, ifp2
->name
);
137 static int if_cmp_index_func(const struct interface
*ifp1
,
138 const struct interface
*ifp2
)
140 if (ifp1
->ifindex
== ifp2
->ifindex
)
142 else if (ifp1
->ifindex
> ifp2
->ifindex
)
148 static void ifp_connected_free(void *arg
)
150 struct connected
*c
= arg
;
155 /* Create new interface structure. */
156 static struct interface
*if_new(vrf_id_t vrf_id
)
158 struct interface
*ifp
;
160 ifp
= XCALLOC(MTYPE_IF
, sizeof(struct interface
));
162 ifp
->ifindex
= IFINDEX_INTERNAL
;
165 ifp
->vrf_id
= vrf_id
;
167 ifp
->connected
= list_new();
168 ifp
->connected
->del
= ifp_connected_free
;
170 ifp
->nbr_connected
= list_new();
171 ifp
->nbr_connected
->del
= (void (*)(void *))nbr_connected_free
;
173 /* Enable Link-detection by default */
174 SET_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
);
176 QOBJ_REG(ifp
, interface
);
180 void if_new_via_zapi(struct interface
*ifp
)
182 if (ifp_master
.create_hook
)
183 (*ifp_master
.create_hook
)(ifp
);
186 void if_destroy_via_zapi(struct interface
*ifp
)
188 if (ifp_master
.destroy_hook
)
189 (*ifp_master
.destroy_hook
)(ifp
);
191 if_set_index(ifp
, IFINDEX_INTERNAL
);
192 if (!ifp
->configured
)
196 void if_up_via_zapi(struct interface
*ifp
)
198 if (ifp_master
.up_hook
)
199 (*ifp_master
.up_hook
)(ifp
);
202 void if_down_via_zapi(struct interface
*ifp
)
204 if (ifp_master
.down_hook
)
205 (*ifp_master
.down_hook
)(ifp
);
208 struct interface
*if_create_name(const char *name
, vrf_id_t vrf_id
)
210 struct interface
*ifp
;
212 ifp
= if_new(vrf_id
);
214 if_set_name(ifp
, name
);
216 hook_call(if_add
, ifp
);
220 struct interface
*if_create_ifindex(ifindex_t ifindex
, vrf_id_t vrf_id
,
223 struct interface
*ifp
;
225 ifp
= if_new(vrf_id
);
227 if_set_index(ifp
, ifindex
);
229 if_set_name(ifp
, optional_name
);
230 hook_call(if_add
, ifp
);
234 /* Create new interface structure. */
235 void if_update_to_new_vrf(struct interface
*ifp
, vrf_id_t vrf_id
)
237 struct vrf
*old_vrf
, *vrf
;
239 /* remove interface from old master vrf list */
240 old_vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
242 if (ifp
->name
[0] != '\0')
243 IFNAME_RB_REMOVE(old_vrf
, ifp
);
245 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
246 IFINDEX_RB_REMOVE(old_vrf
, ifp
);
249 ifp
->vrf_id
= vrf_id
;
250 vrf
= vrf_get(ifp
->vrf_id
, NULL
);
252 if (ifp
->name
[0] != '\0')
253 IFNAME_RB_INSERT(vrf
, ifp
);
255 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
256 IFINDEX_RB_INSERT(vrf
, ifp
);
259 * HACK: Change the interface VRF in the running configuration directly,
260 * bypassing the northbound layer. This is necessary to avoid deleting
261 * the interface and readding it in the new VRF, which would have
262 * several implications.
264 if (yang_module_find("frr-interface")) {
265 struct lyd_node
*if_dnode
;
267 if_dnode
= yang_dnode_get(
268 running_config
->dnode
,
269 "/frr-interface:lib/interface[name='%s'][vrf='%s']/vrf",
270 ifp
->name
, old_vrf
->name
);
272 nb_running_unset_entry(if_dnode
->parent
);
273 yang_dnode_change_leaf(if_dnode
, vrf
->name
);
274 nb_running_set_entry(if_dnode
->parent
, ifp
);
275 running_config
->version
++;
281 /* Delete interface structure. */
282 void if_delete_retain(struct interface
*ifp
)
284 hook_call(if_del
, ifp
);
287 /* Free connected address list */
288 list_delete_all_node(ifp
->connected
);
290 /* Free connected nbr address list */
291 list_delete_all_node(ifp
->nbr_connected
);
294 /* Delete and free interface structure. */
295 void if_delete(struct interface
**ifp
)
297 struct interface
*ptr
= *ifp
;
300 vrf
= vrf_lookup_by_id(ptr
->vrf_id
);
303 IFNAME_RB_REMOVE(vrf
, ptr
);
304 if (ptr
->ifindex
!= IFINDEX_INTERNAL
)
305 IFINDEX_RB_REMOVE(vrf
, ptr
);
307 if_delete_retain(ptr
);
309 list_delete(&ptr
->connected
);
310 list_delete(&ptr
->nbr_connected
);
312 if_link_params_free(ptr
);
314 XFREE(MTYPE_TMP
, ptr
->desc
);
316 XFREE(MTYPE_IF
, ptr
);
320 /* Used only internally to check within VRF only */
321 static struct interface
*if_lookup_by_ifindex(ifindex_t ifindex
,
325 struct interface if_tmp
;
327 vrf
= vrf_lookup_by_id(vrf_id
);
331 if_tmp
.ifindex
= ifindex
;
332 return RB_FIND(if_index_head
, &vrf
->ifaces_by_index
, &if_tmp
);
335 /* Interface existance check by index. */
336 struct interface
*if_lookup_by_index(ifindex_t ifindex
, vrf_id_t vrf_id
)
338 switch (vrf_get_backend()) {
339 case VRF_BACKEND_UNKNOWN
:
340 case VRF_BACKEND_NETNS
:
341 return(if_lookup_by_ifindex(ifindex
, vrf_id
));
342 case VRF_BACKEND_VRF_LITE
:
343 return(if_lookup_by_index_all_vrf(ifindex
));
348 const char *ifindex2ifname(ifindex_t ifindex
, vrf_id_t vrf_id
)
350 struct interface
*ifp
;
352 return ((ifp
= if_lookup_by_index(ifindex
, vrf_id
)) != NULL
)
357 ifindex_t
ifname2ifindex(const char *name
, vrf_id_t vrf_id
)
359 struct interface
*ifp
;
361 return ((ifp
= if_lookup_by_name(name
, vrf_id
)) != NULL
)
366 /* Interface existance check by interface name. */
367 struct interface
*if_lookup_by_name(const char *name
, vrf_id_t vrf_id
)
369 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
370 struct interface if_tmp
;
373 || strnlen(name
, INTERFACE_NAMSIZ
) == INTERFACE_NAMSIZ
)
376 strlcpy(if_tmp
.name
, name
, sizeof(if_tmp
.name
));
377 return RB_FIND(if_name_head
, &vrf
->ifaces_by_name
, &if_tmp
);
380 struct interface
*if_lookup_by_name_all_vrf(const char *name
)
383 struct interface
*ifp
;
385 if (!name
|| strnlen(name
, INTERFACE_NAMSIZ
) == INTERFACE_NAMSIZ
)
388 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
389 ifp
= if_lookup_by_name(name
, vrf
->vrf_id
);
397 struct interface
*if_lookup_by_index_all_vrf(ifindex_t ifindex
)
400 struct interface
*ifp
;
402 if (ifindex
== IFINDEX_INTERNAL
)
405 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
406 ifp
= if_lookup_by_ifindex(ifindex
, vrf
->vrf_id
);
414 /* Lookup interface by IP address. */
415 struct interface
*if_lookup_exact_address(const void *src
, int family
,
418 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
419 struct listnode
*cnode
;
420 struct interface
*ifp
;
424 FOR_ALL_INTERFACES (vrf
, ifp
) {
425 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
428 if (p
&& (p
->family
== family
)) {
429 if (family
== AF_INET
) {
432 (struct in_addr
*)src
))
434 } else if (family
== AF_INET6
) {
437 (struct in6_addr
*)src
))
446 /* Lookup interface by IP address. */
447 struct connected
*if_lookup_address(const void *matchaddr
, int family
,
450 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
453 struct listnode
*cnode
;
454 struct interface
*ifp
;
456 struct connected
*match
;
458 if (family
== AF_INET
) {
459 addr
.family
= AF_INET
;
460 addr
.u
.prefix4
= *((struct in_addr
*)matchaddr
);
461 addr
.prefixlen
= IPV4_MAX_BITLEN
;
462 } else if (family
== AF_INET6
) {
463 addr
.family
= AF_INET6
;
464 addr
.u
.prefix6
= *((struct in6_addr
*)matchaddr
);
465 addr
.prefixlen
= IPV6_MAX_BITLEN
;
470 FOR_ALL_INTERFACES (vrf
, ifp
) {
471 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
472 if (c
->address
&& (c
->address
->family
== AF_INET
)
473 && prefix_match(CONNECTED_PREFIX(c
), &addr
)
474 && (c
->address
->prefixlen
> bestlen
)) {
475 bestlen
= c
->address
->prefixlen
;
483 /* Lookup interface by prefix */
484 struct interface
*if_lookup_prefix(const struct prefix
*prefix
, vrf_id_t vrf_id
)
486 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
487 struct listnode
*cnode
;
488 struct interface
*ifp
;
491 FOR_ALL_INTERFACES (vrf
, ifp
) {
492 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
493 if (prefix_cmp(c
->address
, prefix
) == 0) {
501 size_t if_lookup_by_hwaddr(const uint8_t *hw_addr
, size_t addrsz
,
502 struct interface
***result
, vrf_id_t vrf_id
)
504 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
506 struct list
*rs
= list_new();
507 struct interface
*ifp
;
509 FOR_ALL_INTERFACES (vrf
, ifp
) {
510 if (ifp
->hw_addr_len
== (int)addrsz
511 && !memcmp(hw_addr
, ifp
->hw_addr
, addrsz
))
512 listnode_add(rs
, ifp
);
516 *result
= XCALLOC(MTYPE_TMP
,
517 sizeof(struct interface
*) * rs
->count
);
518 list_to_array(rs
, (void **)*result
, rs
->count
);
521 int count
= rs
->count
;
529 /* Get interface by name if given name interface doesn't exist create
531 struct interface
*if_get_by_name(const char *name
, vrf_id_t vrf_id
)
533 struct interface
*ifp
;
535 switch (vrf_get_backend()) {
536 case VRF_BACKEND_UNKNOWN
:
537 case VRF_BACKEND_NETNS
:
538 ifp
= if_lookup_by_name(name
, vrf_id
);
541 return if_create_name(name
, vrf_id
);
542 case VRF_BACKEND_VRF_LITE
:
543 ifp
= if_lookup_by_name_all_vrf(name
);
545 if (ifp
->vrf_id
== vrf_id
)
547 /* If it came from the kernel or by way of zclient,
548 * believe it and update the ifp accordingly.
550 if_update_to_new_vrf(ifp
, vrf_id
);
553 return if_create_name(name
, vrf_id
);
559 struct interface
*if_get_by_ifindex(ifindex_t ifindex
, vrf_id_t vrf_id
,
562 struct interface
*ifp
;
564 switch (vrf_get_backend()) {
565 case VRF_BACKEND_UNKNOWN
:
566 case VRF_BACKEND_NETNS
:
567 ifp
= if_lookup_by_ifindex(ifindex
, vrf_id
);
570 return if_create_ifindex(ifindex
, vrf_id
, optional_name
);
571 case VRF_BACKEND_VRF_LITE
:
572 ifp
= if_lookup_by_index_all_vrf(ifindex
);
574 if (ifp
->vrf_id
== vrf_id
)
576 /* If it came from the kernel or by way of zclient,
577 * believe it and update the ifp accordingly.
579 if_update_to_new_vrf(ifp
, vrf_id
);
582 return if_create_ifindex(ifindex
, vrf_id
, optional_name
);
588 int if_set_index(struct interface
*ifp
, ifindex_t ifindex
)
592 if (ifp
->ifindex
== ifindex
)
595 vrf
= vrf_get(ifp
->vrf_id
, NULL
);
599 * If there is already an interface with this ifindex, we will collide
600 * on insertion, so don't even try.
602 if (if_lookup_by_ifindex(ifindex
, ifp
->vrf_id
))
605 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
606 IFINDEX_RB_REMOVE(vrf
, ifp
);
608 ifp
->ifindex
= ifindex
;
610 if (ifp
->ifindex
!= IFINDEX_INTERNAL
) {
612 * This should never happen, since we checked if there was
613 * already an interface with the desired ifindex at the top of
614 * the function. Nevertheless.
616 if (IFINDEX_RB_INSERT(vrf
, ifp
))
623 void if_set_name(struct interface
*ifp
, const char *name
)
627 vrf
= vrf_get(ifp
->vrf_id
, NULL
);
630 if (if_cmp_name_func(ifp
->name
, name
) == 0)
633 if (ifp
->name
[0] != '\0')
634 IFNAME_RB_REMOVE(vrf
, ifp
);
636 strlcpy(ifp
->name
, name
, sizeof(ifp
->name
));
638 if (ifp
->name
[0] != '\0')
639 IFNAME_RB_INSERT(vrf
, ifp
);
642 /* Does interface up ? */
643 int if_is_up(const struct interface
*ifp
)
645 return ifp
->flags
& IFF_UP
;
648 /* Is interface running? */
649 int if_is_running(const struct interface
*ifp
)
651 return ifp
->flags
& IFF_RUNNING
;
654 /* Is the interface operative, eg. either UP & RUNNING
655 or UP & !ZEBRA_INTERFACE_LINK_DETECTION and
656 if ptm checking is enabled, then ptm check has passed */
657 int if_is_operative(const struct interface
*ifp
)
659 return ((ifp
->flags
& IFF_UP
)
660 && (((ifp
->flags
& IFF_RUNNING
)
661 && (ifp
->ptm_status
|| !ifp
->ptm_enable
))
662 || !CHECK_FLAG(ifp
->status
,
663 ZEBRA_INTERFACE_LINKDETECTION
)));
666 /* Is the interface operative, eg. either UP & RUNNING
667 or UP & !ZEBRA_INTERFACE_LINK_DETECTION, without PTM check */
668 int if_is_no_ptm_operative(const struct interface
*ifp
)
670 return ((ifp
->flags
& IFF_UP
)
671 && ((ifp
->flags
& IFF_RUNNING
)
672 || !CHECK_FLAG(ifp
->status
,
673 ZEBRA_INTERFACE_LINKDETECTION
)));
676 /* Is this loopback interface ? */
677 int if_is_loopback(const struct interface
*ifp
)
679 /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M
680 * but Y on platform N?
682 return (ifp
->flags
& (IFF_LOOPBACK
| IFF_NOXMIT
| IFF_VIRTUAL
));
685 /* Check interface is VRF */
686 int if_is_vrf(const struct interface
*ifp
)
688 return CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
691 bool if_is_loopback_or_vrf(const struct interface
*ifp
)
693 if (if_is_loopback(ifp
) || if_is_vrf(ifp
))
699 /* Does this interface support broadcast ? */
700 int if_is_broadcast(const struct interface
*ifp
)
702 return ifp
->flags
& IFF_BROADCAST
;
705 /* Does this interface support broadcast ? */
706 int if_is_pointopoint(const struct interface
*ifp
)
708 return ifp
->flags
& IFF_POINTOPOINT
;
711 /* Does this interface support multicast ? */
712 int if_is_multicast(const struct interface
*ifp
)
714 return ifp
->flags
& IFF_MULTICAST
;
717 /* Printout flag information into log */
718 const char *if_flag_dump(unsigned long flag
)
721 static char logbuf
[BUFSIZ
];
723 #define IFF_OUT_LOG(X, STR) \
726 strlcat(logbuf, ",", sizeof(logbuf)); \
729 strlcat(logbuf, STR, sizeof(logbuf)); \
732 strlcpy(logbuf
, "<", BUFSIZ
);
733 IFF_OUT_LOG(IFF_UP
, "UP");
734 IFF_OUT_LOG(IFF_BROADCAST
, "BROADCAST");
735 IFF_OUT_LOG(IFF_DEBUG
, "DEBUG");
736 IFF_OUT_LOG(IFF_LOOPBACK
, "LOOPBACK");
737 IFF_OUT_LOG(IFF_POINTOPOINT
, "POINTOPOINT");
738 IFF_OUT_LOG(IFF_NOTRAILERS
, "NOTRAILERS");
739 IFF_OUT_LOG(IFF_RUNNING
, "RUNNING");
740 IFF_OUT_LOG(IFF_NOARP
, "NOARP");
741 IFF_OUT_LOG(IFF_PROMISC
, "PROMISC");
742 IFF_OUT_LOG(IFF_ALLMULTI
, "ALLMULTI");
743 IFF_OUT_LOG(IFF_OACTIVE
, "OACTIVE");
744 IFF_OUT_LOG(IFF_SIMPLEX
, "SIMPLEX");
745 IFF_OUT_LOG(IFF_LINK0
, "LINK0");
746 IFF_OUT_LOG(IFF_LINK1
, "LINK1");
747 IFF_OUT_LOG(IFF_LINK2
, "LINK2");
748 IFF_OUT_LOG(IFF_MULTICAST
, "MULTICAST");
749 IFF_OUT_LOG(IFF_NOXMIT
, "NOXMIT");
750 IFF_OUT_LOG(IFF_NORTEXCH
, "NORTEXCH");
751 IFF_OUT_LOG(IFF_VIRTUAL
, "VIRTUAL");
752 IFF_OUT_LOG(IFF_IPV4
, "IPv4");
753 IFF_OUT_LOG(IFF_IPV6
, "IPv6");
755 strlcat(logbuf
, ">", sizeof(logbuf
));
762 static void if_dump(const struct interface
*ifp
)
764 struct listnode
*node
;
765 struct connected
*c
__attribute__((unused
));
767 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, c
)) {
768 struct vrf
*vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
771 "Interface %s vrf %s(%u) index %d metric %d mtu %d "
773 ifp
->name
, VRF_LOGNAME(vrf
), ifp
->vrf_id
, ifp
->ifindex
,
774 ifp
->metric
, ifp
->mtu
, ifp
->mtu6
,
775 if_flag_dump(ifp
->flags
));
779 /* Interface printing for all interface. */
780 void if_dump_all(void)
785 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
786 FOR_ALL_INTERFACES (vrf
, ifp
)
791 /* Need to handle upgrade from SUNWzebra to Quagga. SUNWzebra created
792 * a seperate struct interface for each logical interface, so config
793 * file may be full of 'interface fooX:Y'. Solaris however does not
794 * expose logical interfaces via PF_ROUTE, so trying to track logical
795 * interfaces can be fruitless, for that reason Quagga only tracks
796 * the primary IP interface.
798 * We try accomodate SUNWzebra by:
799 * - looking up the interface name, to see whether it exists, if so
801 * - for protocol daemons, this could only because zebra told us of
803 * - for zebra, only because it learnt from kernel
805 * - search the name to see if it contains a sub-ipif / logical interface
806 * seperator, the ':' char. If it does:
807 * - text up to that char must be the primary name - get that name.
809 * - no idea, just get the name in its entirety.
811 static struct interface
*if_sunwzebra_get(const char *name
, vrf_id_t vrf_id
)
813 struct interface
*ifp
;
816 if ((ifp
= if_lookup_by_name(name
, vrf_id
)) != NULL
)
819 /* hunt the primary interface name... */
820 cp
= strchr(name
, ':');
824 return if_get_by_name(name
, vrf_id
);
829 /* For debug purpose. */
832 "show address [vrf NAME]",
838 struct listnode
*node
;
839 struct interface
*ifp
;
840 struct connected
*ifc
;
842 vrf_id_t vrf_id
= VRF_DEFAULT
;
845 VRF_GET_ID (vrf_id
, argv
[idx_vrf
]->arg
);
847 FOR_ALL_INTERFACES (vrf
, ifp
) {
848 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node
, ifc
)) {
851 if (p
->family
== AF_INET
)
852 vty_out (vty
, "%s/%d\n", inet_ntoa (p
->u
.prefix4
), p
->prefixlen
);
858 DEFUN (show_address_vrf_all
,
859 show_address_vrf_all_cmd
,
860 "show address vrf all",
863 VRF_ALL_CMD_HELP_STR
)
866 struct listnode
*node
;
867 struct interface
*ifp
;
868 struct connected
*ifc
;
871 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
873 if (RB_EMPTY (if_name_head
, &vrf
->ifaces_by_name
))
876 vty_out (vty
, "\nVRF %s(%u)\n\n",
877 VRF_LOGNAME(vrf
), vrf
->vrf_id
);
879 FOR_ALL_INTERFACES (vrf
, ifp
) {
880 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node
, ifc
)) {
883 if (p
->family
== AF_INET
)
884 vty_out (vty
, "%s/%d\n", inet_ntoa (p
->u
.prefix4
), p
->prefixlen
);
892 /* Allocate connected structure. */
893 struct connected
*connected_new(void)
895 return XCALLOC(MTYPE_CONNECTED
, sizeof(struct connected
));
898 /* Allocate nbr connected structure. */
899 struct nbr_connected
*nbr_connected_new(void)
901 return XCALLOC(MTYPE_NBR_CONNECTED
, sizeof(struct nbr_connected
));
904 /* Free connected structure. */
905 void connected_free(struct connected
**connected
)
907 struct connected
*ptr
= *connected
;
909 prefix_free(&ptr
->address
);
910 prefix_free(&ptr
->destination
);
912 XFREE(MTYPE_CONNECTED_LABEL
, ptr
->label
);
914 XFREE(MTYPE_CONNECTED
, ptr
);
918 /* Free nbr connected structure. */
919 void nbr_connected_free(struct nbr_connected
*connected
)
921 if (connected
->address
)
922 prefix_free(&connected
->address
);
924 XFREE(MTYPE_NBR_CONNECTED
, connected
);
927 /* If same interface nbr address already exists... */
928 struct nbr_connected
*nbr_connected_check(struct interface
*ifp
,
931 struct nbr_connected
*ifc
;
932 struct listnode
*node
;
934 for (ALL_LIST_ELEMENTS_RO(ifp
->nbr_connected
, node
, ifc
))
935 if (prefix_same(ifc
->address
, p
))
941 /* Print if_addr structure. */
942 static void __attribute__((unused
))
943 connected_log(struct connected
*connected
, char *str
)
946 struct interface
*ifp
;
951 ifp
= connected
->ifp
;
952 p
= connected
->address
;
954 vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
955 snprintf(logbuf
, sizeof(logbuf
), "%s interface %s vrf %s(%u) %s %s/%d ",
956 str
, ifp
->name
, VRF_LOGNAME(vrf
), ifp
->vrf_id
,
957 prefix_family_str(p
),
958 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
), p
->prefixlen
);
960 p
= connected
->destination
;
962 strncat(logbuf
, inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
963 BUFSIZ
- strlen(logbuf
));
965 zlog_info("%s", logbuf
);
968 /* Print if_addr structure. */
969 static void __attribute__((unused
))
970 nbr_connected_log(struct nbr_connected
*connected
, char *str
)
973 struct interface
*ifp
;
977 ifp
= connected
->ifp
;
978 p
= connected
->address
;
980 snprintf(logbuf
, sizeof(logbuf
), "%s interface %s %s %s/%d ", str
,
981 ifp
->name
, prefix_family_str(p
),
982 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
), p
->prefixlen
);
984 zlog_info("%s", logbuf
);
987 /* If two connected address has same prefix return 1. */
988 static int connected_same_prefix(const struct prefix
*p1
,
989 const struct prefix
*p2
)
991 if (p1
->family
== p2
->family
) {
992 if (p1
->family
== AF_INET
993 && IPV4_ADDR_SAME(&p1
->u
.prefix4
, &p2
->u
.prefix4
))
995 if (p1
->family
== AF_INET6
996 && IPV6_ADDR_SAME(&p1
->u
.prefix6
, &p2
->u
.prefix6
))
1002 /* count the number of connected addresses that are in the given family */
1003 unsigned int connected_count_by_family(struct interface
*ifp
, int family
)
1005 struct listnode
*cnode
;
1006 struct connected
*connected
;
1007 unsigned int cnt
= 0;
1009 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
))
1010 if (connected
->address
->family
== family
)
1016 struct connected
*connected_lookup_prefix_exact(struct interface
*ifp
,
1017 const struct prefix
*p
)
1019 struct listnode
*node
;
1020 struct listnode
*next
;
1021 struct connected
*ifc
;
1023 for (node
= listhead(ifp
->connected
); node
; node
= next
) {
1024 ifc
= listgetdata(node
);
1027 if (connected_same_prefix(ifc
->address
, p
))
1033 struct connected
*connected_delete_by_prefix(struct interface
*ifp
,
1036 struct listnode
*node
;
1037 struct listnode
*next
;
1038 struct connected
*ifc
;
1040 /* In case of same prefix come, replace it with new one. */
1041 for (node
= listhead(ifp
->connected
); node
; node
= next
) {
1042 ifc
= listgetdata(node
);
1045 if (connected_same_prefix(ifc
->address
, p
)) {
1046 listnode_delete(ifp
->connected
, ifc
);
1053 /* Find the address on our side that will be used when packets
1055 struct connected
*connected_lookup_prefix(struct interface
*ifp
,
1056 const struct prefix
*addr
)
1058 struct listnode
*cnode
;
1059 struct connected
*c
;
1060 struct connected
*match
;
1064 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
1065 if (c
->address
&& (c
->address
->family
== addr
->family
)
1066 && prefix_match(CONNECTED_PREFIX(c
), addr
)
1068 || (c
->address
->prefixlen
> match
->address
->prefixlen
)))
1074 struct connected
*connected_add_by_prefix(struct interface
*ifp
,
1076 struct prefix
*destination
)
1078 struct connected
*ifc
;
1080 /* Allocate new connected address. */
1081 ifc
= connected_new();
1084 /* Fetch interface address */
1085 ifc
->address
= prefix_new();
1086 memcpy(ifc
->address
, p
, sizeof(struct prefix
));
1088 /* Fetch dest address */
1090 ifc
->destination
= prefix_new();
1091 memcpy(ifc
->destination
, destination
, sizeof(struct prefix
));
1094 /* Add connected address to the interface. */
1095 listnode_add(ifp
->connected
, ifc
);
1099 struct connected
*connected_get_linklocal(struct interface
*ifp
)
1102 struct connected
*c
= NULL
;
1104 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, n
, c
)) {
1105 if (c
->address
->family
== AF_INET6
1106 && IN6_IS_ADDR_LINKLOCAL(&c
->address
->u
.prefix6
))
1112 #if 0 /* this route_table of struct connected's is unused \
1113 * however, it would be good to use a route_table rather than \
1116 /* Interface looking up by interface's address. */
1117 /* Interface's IPv4 address reverse lookup table. */
1118 struct route_table
*ifaddr_ipv4_table
;
1119 /* struct route_table *ifaddr_ipv6_table; */
1122 ifaddr_ipv4_add (struct in_addr
*ifaddr
, struct interface
*ifp
)
1124 struct route_node
*rn
;
1125 struct prefix_ipv4 p
;
1128 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1131 rn
= route_node_get (ifaddr_ipv4_table
, (struct prefix
*) &p
);
1134 route_unlock_node (rn
);
1135 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
1136 inet_ntoa (*ifaddr
));
1143 ifaddr_ipv4_delete (struct in_addr
*ifaddr
, struct interface
*ifp
)
1145 struct route_node
*rn
;
1146 struct prefix_ipv4 p
;
1149 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1152 rn
= route_node_lookup (ifaddr_ipv4_table
, (struct prefix
*) &p
);
1155 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
1156 inet_ntoa (*ifaddr
));
1160 route_unlock_node (rn
);
1161 route_unlock_node (rn
);
1164 /* Lookup interface by interface's IP address or interface index. */
1165 static struct interface
*
1166 ifaddr_ipv4_lookup (struct in_addr
*addr
, ifindex_t ifindex
)
1168 struct prefix_ipv4 p
;
1169 struct route_node
*rn
;
1170 struct interface
*ifp
;
1175 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1178 rn
= route_node_lookup (ifaddr_ipv4_table
, (struct prefix
*) &p
);
1183 route_unlock_node (rn
);
1187 return if_lookup_by_index(ifindex
, VRF_DEFAULT
);
1189 #endif /* ifaddr_ipv4_table */
1191 void if_terminate(struct vrf
*vrf
)
1193 struct interface
*ifp
;
1195 while (!RB_EMPTY(if_name_head
, &vrf
->ifaces_by_name
)) {
1196 ifp
= RB_ROOT(if_name_head
, &vrf
->ifaces_by_name
);
1199 ifp
->node
->info
= NULL
;
1200 route_unlock_node(ifp
->node
);
1206 const char *if_link_type_str(enum zebra_link_type llt
)
1209 #define llts(T,S) case (T): return (S)
1210 llts(ZEBRA_LLT_UNKNOWN
, "Unknown");
1211 llts(ZEBRA_LLT_ETHER
, "Ethernet");
1212 llts(ZEBRA_LLT_EETHER
, "Experimental Ethernet");
1213 llts(ZEBRA_LLT_AX25
, "AX.25 Level 2");
1214 llts(ZEBRA_LLT_PRONET
, "PROnet token ring");
1215 llts(ZEBRA_LLT_IEEE802
, "IEEE 802.2 Ethernet/TR/TB");
1216 llts(ZEBRA_LLT_ARCNET
, "ARCnet");
1217 llts(ZEBRA_LLT_APPLETLK
, "AppleTalk");
1218 llts(ZEBRA_LLT_DLCI
, "Frame Relay DLCI");
1219 llts(ZEBRA_LLT_ATM
, "ATM");
1220 llts(ZEBRA_LLT_METRICOM
, "Metricom STRIP");
1221 llts(ZEBRA_LLT_IEEE1394
, "IEEE 1394 IPv4");
1222 llts(ZEBRA_LLT_EUI64
, "EUI-64");
1223 llts(ZEBRA_LLT_INFINIBAND
, "InfiniBand");
1224 llts(ZEBRA_LLT_SLIP
, "SLIP");
1225 llts(ZEBRA_LLT_CSLIP
, "Compressed SLIP");
1226 llts(ZEBRA_LLT_SLIP6
, "SLIPv6");
1227 llts(ZEBRA_LLT_CSLIP6
, "Compressed SLIPv6");
1228 llts(ZEBRA_LLT_ROSE
, "ROSE packet radio");
1229 llts(ZEBRA_LLT_X25
, "CCITT X.25");
1230 llts(ZEBRA_LLT_PPP
, "PPP");
1231 llts(ZEBRA_LLT_CHDLC
, "Cisco HDLC");
1232 llts(ZEBRA_LLT_RAWHDLC
, "Raw HDLC");
1233 llts(ZEBRA_LLT_LAPB
, "LAPB");
1234 llts(ZEBRA_LLT_IPIP
, "IPIP Tunnel");
1235 llts(ZEBRA_LLT_IPIP6
, "IPIP6 Tunnel");
1236 llts(ZEBRA_LLT_FRAD
, "FRAD");
1237 llts(ZEBRA_LLT_SKIP
, "SKIP vif");
1238 llts(ZEBRA_LLT_LOOPBACK
, "Loopback");
1239 llts(ZEBRA_LLT_LOCALTLK
, "Localtalk");
1240 llts(ZEBRA_LLT_FDDI
, "FDDI");
1241 llts(ZEBRA_LLT_SIT
, "IPv6-in-IPv4 SIT");
1242 llts(ZEBRA_LLT_IPDDP
, "IP-in-DDP tunnel");
1243 llts(ZEBRA_LLT_IPGRE
, "GRE over IP");
1244 llts(ZEBRA_LLT_PIMREG
, "PIMSM registration");
1245 llts(ZEBRA_LLT_HIPPI
, "HiPPI");
1246 llts(ZEBRA_LLT_IRDA
, "IrDA");
1247 llts(ZEBRA_LLT_FCPP
, "Fibre-Channel PtP");
1248 llts(ZEBRA_LLT_FCAL
, "Fibre-Channel Arbitrated Loop");
1249 llts(ZEBRA_LLT_FCPL
, "Fibre-Channel Public Loop");
1250 llts(ZEBRA_LLT_FCFABRIC
, "Fibre-Channel Fabric");
1251 llts(ZEBRA_LLT_IEEE802_TR
, "IEEE 802.2 Token Ring");
1252 llts(ZEBRA_LLT_IEEE80211
, "IEEE 802.11");
1253 llts(ZEBRA_LLT_IEEE80211_RADIOTAP
, "IEEE 802.11 Radiotap");
1254 llts(ZEBRA_LLT_IEEE802154
, "IEEE 802.15.4");
1255 llts(ZEBRA_LLT_IEEE802154_PHY
, "IEEE 802.15.4 Phy");
1257 flog_err(EC_LIB_DEVELOPMENT
, "Unknown value %d", llt
);
1258 return "Unknown type!";
1264 struct if_link_params
*if_link_params_get(struct interface
*ifp
)
1268 if (ifp
->link_params
!= NULL
)
1269 return ifp
->link_params
;
1271 struct if_link_params
*iflp
=
1272 XCALLOC(MTYPE_IF_LINK_PARAMS
, sizeof(struct if_link_params
));
1274 /* Set TE metric equal to standard metric */
1275 iflp
->te_metric
= ifp
->metric
;
1277 /* Compute default bandwidth based on interface */
1279 ((ifp
->bandwidth
? ifp
->bandwidth
: DEFAULT_BANDWIDTH
)
1280 * TE_MEGA_BIT
/ TE_BYTE
);
1282 /* Set Max, Reservable and Unreserved Bandwidth */
1283 iflp
->max_bw
= iflp
->default_bw
;
1284 iflp
->max_rsv_bw
= iflp
->default_bw
;
1285 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1286 iflp
->unrsv_bw
[i
] = iflp
->default_bw
;
1288 /* Update Link parameters status */
1290 LP_TE_METRIC
| LP_MAX_BW
| LP_MAX_RSV_BW
| LP_UNRSV_BW
;
1292 /* Finally attach newly created Link Parameters */
1293 ifp
->link_params
= iflp
;
1298 void if_link_params_free(struct interface
*ifp
)
1300 XFREE(MTYPE_IF_LINK_PARAMS
, ifp
->link_params
);
1303 /* ----------- CLI commands ----------- */
1306 * XPath: /frr-interface:lib/interface
1308 DEFPY_NOSH (interface
,
1310 "interface IFNAME [vrf NAME$vrf_name]",
1311 "Select an interface to configure\n"
1312 "Interface's name\n"
1315 char xpath_list
[XPATH_MAXLEN
];
1317 struct interface
*ifp
;
1321 vrf_name
= VRF_DEFAULT_NAME
;
1324 * This command requires special handling to maintain backward
1325 * compatibility. If a VRF name is not specified, it means we're willing
1326 * to accept any interface with the given name on any VRF. If no
1327 * interface is found, then a new one should be created on the default
1330 VRF_GET_ID(vrf_id
, vrf_name
, false);
1331 ifp
= if_lookup_by_name_all_vrf(ifname
);
1332 if (ifp
&& ifp
->vrf_id
!= vrf_id
) {
1336 * Special case 1: a VRF name was specified, but the found
1337 * interface is associated to different VRF. Reject the command.
1339 if (vrf_id
!= VRF_DEFAULT
) {
1340 vty_out(vty
, "%% interface %s not in %s vrf\n", ifname
,
1342 return CMD_WARNING_CONFIG_FAILED
;
1346 * Special case 2: a VRF name was *not* specified, and the found
1347 * interface is associated to a VRF other than the default one.
1348 * Update vrf_id and vrf_name to account for that.
1350 vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
1352 vrf_id
= ifp
->vrf_id
;
1353 vrf_name
= vrf
->name
;
1356 snprintf(xpath_list
, sizeof(xpath_list
),
1357 "/frr-interface:lib/interface[name='%s'][vrf='%s']", ifname
,
1360 nb_cli_enqueue_change(vty
, ".", NB_OP_CREATE
, NULL
);
1361 ret
= nb_cli_apply_changes(vty
, xpath_list
);
1362 if (ret
== CMD_SUCCESS
) {
1363 VTY_PUSH_XPATH(INTERFACE_NODE
, xpath_list
);
1366 * For backward compatibility with old commands we still need
1367 * to use the qobj infrastructure. This can be removed once
1368 * all interface-level commands are converted to the new
1371 ifp
= if_lookup_by_name(ifname
, vrf_id
);
1373 VTY_PUSH_CONTEXT(INTERFACE_NODE
, ifp
);
1379 DEFPY (no_interface
,
1381 "no interface IFNAME [vrf NAME$vrf_name]",
1383 "Delete a pseudo interface's configuration\n"
1384 "Interface's name\n"
1388 vrf_name
= VRF_DEFAULT_NAME
;
1390 nb_cli_enqueue_change(vty
, ".", NB_OP_DESTROY
, NULL
);
1392 return nb_cli_apply_changes(
1393 vty
, "/frr-interface:lib/interface[name='%s'][vrf='%s']",
1397 static void cli_show_interface(struct vty
*vty
, struct lyd_node
*dnode
,
1402 vrf
= yang_dnode_get_string(dnode
, "./vrf");
1404 vty_out(vty
, "!\n");
1405 vty_out(vty
, "interface %s", yang_dnode_get_string(dnode
, "./name"));
1406 if (!strmatch(vrf
, VRF_DEFAULT_NAME
))
1407 vty_out(vty
, " vrf %s", vrf
);
1412 * XPath: /frr-interface:lib/interface/description
1414 DEFPY (interface_desc
,
1416 "description LINE...",
1417 "Interface specific description\n"
1418 "Characters describing this interface\n")
1423 desc
= argv_concat(argv
, argc
, 1);
1424 nb_cli_enqueue_change(vty
, "./description", NB_OP_MODIFY
, desc
);
1425 ret
= nb_cli_apply_changes(vty
, NULL
);
1426 XFREE(MTYPE_TMP
, desc
);
1431 DEFPY (no_interface_desc
,
1432 no_interface_desc_cmd
,
1435 "Interface specific description\n")
1437 nb_cli_enqueue_change(vty
, "./description", NB_OP_DESTROY
, NULL
);
1439 return nb_cli_apply_changes(vty
, NULL
);
1442 static void cli_show_interface_desc(struct vty
*vty
, struct lyd_node
*dnode
,
1445 vty_out(vty
, " description %s\n", yang_dnode_get_string(dnode
, NULL
));
1448 /* Interface autocomplete. */
1449 static void if_autocomplete(vector comps
, struct cmd_token
*token
)
1451 struct interface
*ifp
;
1454 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
1455 FOR_ALL_INTERFACES (vrf
, ifp
) {
1456 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, ifp
->name
));
1461 static const struct cmd_variable_handler if_var_handlers
[] = {
1462 {/* "interface NAME" */
1463 .varname
= "interface",
1464 .completions
= if_autocomplete
},
1465 {.tokenname
= "IFNAME", .completions
= if_autocomplete
},
1466 {.tokenname
= "INTERFACE", .completions
= if_autocomplete
},
1467 {.completions
= NULL
}};
1469 void if_cmd_init(void)
1471 cmd_variable_handler_register(if_var_handlers
);
1473 install_element(CONFIG_NODE
, &interface_cmd
);
1474 install_element(CONFIG_NODE
, &no_interface_cmd
);
1476 install_default(INTERFACE_NODE
);
1477 install_element(INTERFACE_NODE
, &interface_desc_cmd
);
1478 install_element(INTERFACE_NODE
, &no_interface_desc_cmd
);
1481 void if_zapi_callbacks(int (*create
)(struct interface
*ifp
),
1482 int (*up
)(struct interface
*ifp
),
1483 int (*down
)(struct interface
*ifp
),
1484 int (*destroy
)(struct interface
*ifp
))
1486 ifp_master
.create_hook
= create
;
1487 ifp_master
.up_hook
= up
;
1488 ifp_master
.down_hook
= down
;
1489 ifp_master
.destroy_hook
= destroy
;
1492 /* ------- Northbound callbacks ------- */
1495 * XPath: /frr-interface:lib/interface
1497 static int lib_interface_create(struct nb_cb_create_args
*args
)
1500 const char *vrfname
;
1502 struct interface
*ifp
;
1504 ifname
= yang_dnode_get_string(args
->dnode
, "./name");
1505 vrfname
= yang_dnode_get_string(args
->dnode
, "./vrf");
1507 switch (args
->event
) {
1508 case NB_EV_VALIDATE
:
1509 vrf
= vrf_lookup_by_name(vrfname
);
1511 zlog_warn("%s: VRF %s doesn't exist", __func__
,
1513 return NB_ERR_VALIDATION
;
1515 if (vrf
->vrf_id
== VRF_UNKNOWN
) {
1516 zlog_warn("%s: VRF %s is not active", __func__
,
1518 return NB_ERR_VALIDATION
;
1521 /* if VRF is netns or not yet known - init for instance
1522 * then assumption is that passed config is exact
1523 * then the user intent was not to use an other iface
1525 if (vrf_get_backend() == VRF_BACKEND_VRF_LITE
) {
1526 ifp
= if_lookup_by_name_all_vrf(ifname
);
1527 if (ifp
&& ifp
->vrf_id
!= vrf
->vrf_id
) {
1529 "%s: interface %s already exists in another VRF",
1530 __func__
, ifp
->name
);
1531 return NB_ERR_VALIDATION
;
1539 vrf
= vrf_lookup_by_name(vrfname
);
1542 ifp
= if_sunwzebra_get(ifname
, vrf
->vrf_id
);
1544 ifp
= if_get_by_name(ifname
, vrf
->vrf_id
);
1545 #endif /* SUNOS_5 */
1547 ifp
->configured
= true;
1548 nb_running_set_entry(args
->dnode
, ifp
);
1555 static int lib_interface_destroy(struct nb_cb_destroy_args
*args
)
1557 struct interface
*ifp
;
1560 switch (args
->event
) {
1561 case NB_EV_VALIDATE
:
1562 ifp
= nb_running_get_entry(args
->dnode
, NULL
, true);
1563 if (CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
)) {
1564 zlog_warn("%s: only inactive interfaces can be deleted",
1566 return NB_ERR_VALIDATION
;
1573 ifp
= nb_running_unset_entry(args
->dnode
);
1575 ifp
->configured
= false;
1584 * XPath: /frr-interface:lib/interface
1586 static const void *lib_interface_get_next(struct nb_cb_get_next_args
*args
)
1589 struct interface
*pif
= (struct interface
*)args
->list_entry
;
1591 if (args
->list_entry
== NULL
) {
1592 vrf
= RB_MIN(vrf_name_head
, &vrfs_by_name
);
1594 pif
= RB_MIN(if_name_head
, &vrf
->ifaces_by_name
);
1596 vrf
= vrf_lookup_by_id(pif
->vrf_id
);
1597 pif
= RB_NEXT(if_name_head
, pif
);
1598 /* if no more interfaces, switch to next vrf */
1599 while (pif
== NULL
) {
1600 vrf
= RB_NEXT(vrf_name_head
, vrf
);
1603 pif
= RB_MIN(if_name_head
, &vrf
->ifaces_by_name
);
1610 static int lib_interface_get_keys(struct nb_cb_get_keys_args
*args
)
1612 const struct interface
*ifp
= args
->list_entry
;
1614 struct vrf
*vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
1618 args
->keys
->num
= 2;
1619 strlcpy(args
->keys
->key
[0], ifp
->name
, sizeof(args
->keys
->key
[0]));
1620 strlcpy(args
->keys
->key
[1], vrf
->name
, sizeof(args
->keys
->key
[1]));
1626 lib_interface_lookup_entry(struct nb_cb_lookup_entry_args
*args
)
1628 const char *ifname
= args
->keys
->key
[0];
1629 const char *vrfname
= args
->keys
->key
[1];
1630 struct vrf
*vrf
= vrf_lookup_by_name(vrfname
);
1632 return vrf
? if_lookup_by_name(ifname
, vrf
->vrf_id
) : NULL
;
1636 * XPath: /frr-interface:lib/interface/description
1638 static int lib_interface_description_modify(struct nb_cb_modify_args
*args
)
1640 struct interface
*ifp
;
1641 const char *description
;
1643 if (args
->event
!= NB_EV_APPLY
)
1646 ifp
= nb_running_get_entry(args
->dnode
, NULL
, true);
1647 XFREE(MTYPE_TMP
, ifp
->desc
);
1648 description
= yang_dnode_get_string(args
->dnode
, NULL
);
1649 ifp
->desc
= XSTRDUP(MTYPE_TMP
, description
);
1654 static int lib_interface_description_destroy(struct nb_cb_destroy_args
*args
)
1656 struct interface
*ifp
;
1658 if (args
->event
!= NB_EV_APPLY
)
1661 ifp
= nb_running_get_entry(args
->dnode
, NULL
, true);
1662 XFREE(MTYPE_TMP
, ifp
->desc
);
1668 * XPath: /frr-interface:lib/interface/state/if-index
1670 static struct yang_data
*
1671 lib_interface_state_if_index_get_elem(struct nb_cb_get_elem_args
*args
)
1673 const struct interface
*ifp
= args
->list_entry
;
1675 return yang_data_new_int32(args
->xpath
, ifp
->ifindex
);
1679 * XPath: /frr-interface:lib/interface/state/mtu
1681 static struct yang_data
*
1682 lib_interface_state_mtu_get_elem(struct nb_cb_get_elem_args
*args
)
1684 const struct interface
*ifp
= args
->list_entry
;
1686 return yang_data_new_uint16(args
->xpath
, ifp
->mtu
);
1690 * XPath: /frr-interface:lib/interface/state/mtu6
1692 static struct yang_data
*
1693 lib_interface_state_mtu6_get_elem(struct nb_cb_get_elem_args
*args
)
1695 const struct interface
*ifp
= args
->list_entry
;
1697 return yang_data_new_uint32(args
->xpath
, ifp
->mtu6
);
1701 * XPath: /frr-interface:lib/interface/state/speed
1703 static struct yang_data
*
1704 lib_interface_state_speed_get_elem(struct nb_cb_get_elem_args
*args
)
1706 const struct interface
*ifp
= args
->list_entry
;
1708 return yang_data_new_uint32(args
->xpath
, ifp
->speed
);
1712 * XPath: /frr-interface:lib/interface/state/metric
1714 static struct yang_data
*
1715 lib_interface_state_metric_get_elem(struct nb_cb_get_elem_args
*args
)
1717 const struct interface
*ifp
= args
->list_entry
;
1719 return yang_data_new_uint32(args
->xpath
, ifp
->metric
);
1723 * XPath: /frr-interface:lib/interface/state/flags
1725 static struct yang_data
*
1726 lib_interface_state_flags_get_elem(struct nb_cb_get_elem_args
*args
)
1728 /* TODO: implement me. */
1733 * XPath: /frr-interface:lib/interface/state/type
1735 static struct yang_data
*
1736 lib_interface_state_type_get_elem(struct nb_cb_get_elem_args
*args
)
1738 /* TODO: implement me. */
1743 * XPath: /frr-interface:lib/interface/state/phy-address
1745 static struct yang_data
*
1746 lib_interface_state_phy_address_get_elem(struct nb_cb_get_elem_args
*args
)
1748 const struct interface
*ifp
= args
->list_entry
;
1749 struct ethaddr macaddr
;
1751 memcpy(&macaddr
.octet
, ifp
->hw_addr
, ETH_ALEN
);
1753 return yang_data_new_mac(args
->xpath
, &macaddr
);
1756 /* clang-format off */
1757 const struct frr_yang_module_info frr_interface_info
= {
1758 .name
= "frr-interface",
1761 .xpath
= "/frr-interface:lib/interface",
1763 .create
= lib_interface_create
,
1764 .destroy
= lib_interface_destroy
,
1765 .cli_show
= cli_show_interface
,
1766 .get_next
= lib_interface_get_next
,
1767 .get_keys
= lib_interface_get_keys
,
1768 .lookup_entry
= lib_interface_lookup_entry
,
1772 .xpath
= "/frr-interface:lib/interface/description",
1774 .modify
= lib_interface_description_modify
,
1775 .destroy
= lib_interface_description_destroy
,
1776 .cli_show
= cli_show_interface_desc
,
1780 .xpath
= "/frr-interface:lib/interface/state/if-index",
1782 .get_elem
= lib_interface_state_if_index_get_elem
,
1786 .xpath
= "/frr-interface:lib/interface/state/mtu",
1788 .get_elem
= lib_interface_state_mtu_get_elem
,
1792 .xpath
= "/frr-interface:lib/interface/state/mtu6",
1794 .get_elem
= lib_interface_state_mtu6_get_elem
,
1798 .xpath
= "/frr-interface:lib/interface/state/speed",
1800 .get_elem
= lib_interface_state_speed_get_elem
,
1804 .xpath
= "/frr-interface:lib/interface/state/metric",
1806 .get_elem
= lib_interface_state_metric_get_elem
,
1810 .xpath
= "/frr-interface:lib/interface/state/flags",
1812 .get_elem
= lib_interface_state_flags_get_elem
,
1816 .xpath
= "/frr-interface:lib/interface/state/type",
1818 .get_elem
= lib_interface_state_type_get_elem
,
1822 .xpath
= "/frr-interface:lib/interface/state/phy-address",
1824 .get_elem
= lib_interface_state_phy_address_get_elem
,