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
;
266 char oldpath
[XPATH_MAXLEN
];
267 char newpath
[XPATH_MAXLEN
];
269 if_dnode
= yang_dnode_get(
270 running_config
->dnode
,
271 "/frr-interface:lib/interface[name='%s'][vrf='%s']/vrf",
272 ifp
->name
, old_vrf
->name
);
275 yang_dnode_get_path(if_dnode
->parent
, oldpath
,
277 yang_dnode_change_leaf(if_dnode
, vrf
->name
);
278 yang_dnode_get_path(if_dnode
->parent
, newpath
,
280 nb_running_move_tree(oldpath
, newpath
);
281 running_config
->version
++;
287 /* Delete interface structure. */
288 void if_delete_retain(struct interface
*ifp
)
290 hook_call(if_del
, ifp
);
293 /* Free connected address list */
294 list_delete_all_node(ifp
->connected
);
296 /* Free connected nbr address list */
297 list_delete_all_node(ifp
->nbr_connected
);
300 /* Delete and free interface structure. */
301 void if_delete(struct interface
**ifp
)
303 struct interface
*ptr
= *ifp
;
306 vrf
= vrf_lookup_by_id(ptr
->vrf_id
);
309 IFNAME_RB_REMOVE(vrf
, ptr
);
310 if (ptr
->ifindex
!= IFINDEX_INTERNAL
)
311 IFINDEX_RB_REMOVE(vrf
, ptr
);
313 if_delete_retain(ptr
);
315 list_delete(&ptr
->connected
);
316 list_delete(&ptr
->nbr_connected
);
318 if_link_params_free(ptr
);
320 XFREE(MTYPE_TMP
, ptr
->desc
);
322 XFREE(MTYPE_IF
, ptr
);
326 /* Used only internally to check within VRF only */
327 static struct interface
*if_lookup_by_ifindex(ifindex_t ifindex
,
331 struct interface if_tmp
;
333 vrf
= vrf_lookup_by_id(vrf_id
);
337 if_tmp
.ifindex
= ifindex
;
338 return RB_FIND(if_index_head
, &vrf
->ifaces_by_index
, &if_tmp
);
341 /* Interface existance check by index. */
342 struct interface
*if_lookup_by_index(ifindex_t ifindex
, vrf_id_t vrf_id
)
344 switch (vrf_get_backend()) {
345 case VRF_BACKEND_UNKNOWN
:
346 case VRF_BACKEND_NETNS
:
347 return(if_lookup_by_ifindex(ifindex
, vrf_id
));
348 case VRF_BACKEND_VRF_LITE
:
349 return(if_lookup_by_index_all_vrf(ifindex
));
354 const char *ifindex2ifname(ifindex_t ifindex
, vrf_id_t vrf_id
)
356 struct interface
*ifp
;
358 return ((ifp
= if_lookup_by_index(ifindex
, vrf_id
)) != NULL
)
363 ifindex_t
ifname2ifindex(const char *name
, vrf_id_t vrf_id
)
365 struct interface
*ifp
;
367 return ((ifp
= if_lookup_by_name(name
, vrf_id
)) != NULL
)
372 /* Interface existance check by interface name. */
373 struct interface
*if_lookup_by_name(const char *name
, vrf_id_t vrf_id
)
375 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
376 struct interface if_tmp
;
379 || strnlen(name
, INTERFACE_NAMSIZ
) == INTERFACE_NAMSIZ
)
382 strlcpy(if_tmp
.name
, name
, sizeof(if_tmp
.name
));
383 return RB_FIND(if_name_head
, &vrf
->ifaces_by_name
, &if_tmp
);
386 struct interface
*if_lookup_by_name_all_vrf(const char *name
)
389 struct interface
*ifp
;
391 if (!name
|| strnlen(name
, INTERFACE_NAMSIZ
) == INTERFACE_NAMSIZ
)
394 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
395 ifp
= if_lookup_by_name(name
, vrf
->vrf_id
);
403 struct interface
*if_lookup_by_index_all_vrf(ifindex_t ifindex
)
406 struct interface
*ifp
;
408 if (ifindex
== IFINDEX_INTERNAL
)
411 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
412 ifp
= if_lookup_by_ifindex(ifindex
, vrf
->vrf_id
);
420 /* Lookup interface by IP address. */
421 struct interface
*if_lookup_exact_address(const void *src
, int family
,
424 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
425 struct listnode
*cnode
;
426 struct interface
*ifp
;
430 FOR_ALL_INTERFACES (vrf
, ifp
) {
431 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
434 if (p
&& (p
->family
== family
)) {
435 if (family
== AF_INET
) {
438 (struct in_addr
*)src
))
440 } else if (family
== AF_INET6
) {
443 (struct in6_addr
*)src
))
452 /* Lookup interface by IP address. */
453 struct connected
*if_lookup_address(const void *matchaddr
, int family
,
456 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
459 struct listnode
*cnode
;
460 struct interface
*ifp
;
462 struct connected
*match
;
464 if (family
== AF_INET
) {
465 addr
.family
= AF_INET
;
466 addr
.u
.prefix4
= *((struct in_addr
*)matchaddr
);
467 addr
.prefixlen
= IPV4_MAX_BITLEN
;
468 } else if (family
== AF_INET6
) {
469 addr
.family
= AF_INET6
;
470 addr
.u
.prefix6
= *((struct in6_addr
*)matchaddr
);
471 addr
.prefixlen
= IPV6_MAX_BITLEN
;
476 FOR_ALL_INTERFACES (vrf
, ifp
) {
477 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
478 if (c
->address
&& (c
->address
->family
== AF_INET
)
479 && prefix_match(CONNECTED_PREFIX(c
), &addr
)
480 && (c
->address
->prefixlen
> bestlen
)) {
481 bestlen
= c
->address
->prefixlen
;
489 /* Lookup interface by prefix */
490 struct interface
*if_lookup_prefix(const struct prefix
*prefix
, vrf_id_t vrf_id
)
492 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
493 struct listnode
*cnode
;
494 struct interface
*ifp
;
497 FOR_ALL_INTERFACES (vrf
, ifp
) {
498 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
499 if (prefix_cmp(c
->address
, prefix
) == 0) {
507 size_t if_lookup_by_hwaddr(const uint8_t *hw_addr
, size_t addrsz
,
508 struct interface
***result
, vrf_id_t vrf_id
)
510 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
512 struct list
*rs
= list_new();
513 struct interface
*ifp
;
515 FOR_ALL_INTERFACES (vrf
, ifp
) {
516 if (ifp
->hw_addr_len
== (int)addrsz
517 && !memcmp(hw_addr
, ifp
->hw_addr
, addrsz
))
518 listnode_add(rs
, ifp
);
522 *result
= XCALLOC(MTYPE_TMP
,
523 sizeof(struct interface
*) * rs
->count
);
524 list_to_array(rs
, (void **)*result
, rs
->count
);
527 int count
= rs
->count
;
535 /* Get interface by name if given name interface doesn't exist create
537 struct interface
*if_get_by_name(const char *name
, vrf_id_t vrf_id
)
539 struct interface
*ifp
;
541 switch (vrf_get_backend()) {
542 case VRF_BACKEND_UNKNOWN
:
543 case VRF_BACKEND_NETNS
:
544 ifp
= if_lookup_by_name(name
, vrf_id
);
547 return if_create_name(name
, vrf_id
);
548 case VRF_BACKEND_VRF_LITE
:
549 ifp
= if_lookup_by_name_all_vrf(name
);
551 if (ifp
->vrf_id
== vrf_id
)
553 /* If it came from the kernel or by way of zclient,
554 * believe it and update the ifp accordingly.
556 if_update_to_new_vrf(ifp
, vrf_id
);
559 return if_create_name(name
, vrf_id
);
565 struct interface
*if_get_by_ifindex(ifindex_t ifindex
, vrf_id_t vrf_id
,
568 struct interface
*ifp
;
570 switch (vrf_get_backend()) {
571 case VRF_BACKEND_UNKNOWN
:
572 case VRF_BACKEND_NETNS
:
573 ifp
= if_lookup_by_ifindex(ifindex
, vrf_id
);
576 return if_create_ifindex(ifindex
, vrf_id
, optional_name
);
577 case VRF_BACKEND_VRF_LITE
:
578 ifp
= if_lookup_by_index_all_vrf(ifindex
);
580 if (ifp
->vrf_id
== vrf_id
)
582 /* If it came from the kernel or by way of zclient,
583 * believe it and update the ifp accordingly.
585 if_update_to_new_vrf(ifp
, vrf_id
);
588 return if_create_ifindex(ifindex
, vrf_id
, optional_name
);
594 int if_set_index(struct interface
*ifp
, ifindex_t ifindex
)
598 if (ifp
->ifindex
== ifindex
)
601 vrf
= vrf_get(ifp
->vrf_id
, NULL
);
605 * If there is already an interface with this ifindex, we will collide
606 * on insertion, so don't even try.
608 if (if_lookup_by_ifindex(ifindex
, ifp
->vrf_id
))
611 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
612 IFINDEX_RB_REMOVE(vrf
, ifp
);
614 ifp
->ifindex
= ifindex
;
616 if (ifp
->ifindex
!= IFINDEX_INTERNAL
) {
618 * This should never happen, since we checked if there was
619 * already an interface with the desired ifindex at the top of
620 * the function. Nevertheless.
622 if (IFINDEX_RB_INSERT(vrf
, ifp
))
629 void if_set_name(struct interface
*ifp
, const char *name
)
633 vrf
= vrf_get(ifp
->vrf_id
, NULL
);
636 if (if_cmp_name_func(ifp
->name
, name
) == 0)
639 if (ifp
->name
[0] != '\0')
640 IFNAME_RB_REMOVE(vrf
, ifp
);
642 strlcpy(ifp
->name
, name
, sizeof(ifp
->name
));
644 if (ifp
->name
[0] != '\0')
645 IFNAME_RB_INSERT(vrf
, ifp
);
648 /* Does interface up ? */
649 int if_is_up(const struct interface
*ifp
)
651 return ifp
->flags
& IFF_UP
;
654 /* Is interface running? */
655 int if_is_running(const struct interface
*ifp
)
657 return ifp
->flags
& IFF_RUNNING
;
660 /* Is the interface operative, eg. either UP & RUNNING
661 or UP & !ZEBRA_INTERFACE_LINK_DETECTION and
662 if ptm checking is enabled, then ptm check has passed */
663 int if_is_operative(const struct interface
*ifp
)
665 return ((ifp
->flags
& IFF_UP
)
666 && (((ifp
->flags
& IFF_RUNNING
)
667 && (ifp
->ptm_status
|| !ifp
->ptm_enable
))
668 || !CHECK_FLAG(ifp
->status
,
669 ZEBRA_INTERFACE_LINKDETECTION
)));
672 /* Is the interface operative, eg. either UP & RUNNING
673 or UP & !ZEBRA_INTERFACE_LINK_DETECTION, without PTM check */
674 int if_is_no_ptm_operative(const struct interface
*ifp
)
676 return ((ifp
->flags
& IFF_UP
)
677 && ((ifp
->flags
& IFF_RUNNING
)
678 || !CHECK_FLAG(ifp
->status
,
679 ZEBRA_INTERFACE_LINKDETECTION
)));
682 /* Is this loopback interface ? */
683 int if_is_loopback(const struct interface
*ifp
)
685 /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M
686 * but Y on platform N?
688 return (ifp
->flags
& (IFF_LOOPBACK
| IFF_NOXMIT
| IFF_VIRTUAL
));
691 /* Check interface is VRF */
692 int if_is_vrf(const struct interface
*ifp
)
694 return CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
697 bool if_is_loopback_or_vrf(const struct interface
*ifp
)
699 if (if_is_loopback(ifp
) || if_is_vrf(ifp
))
705 /* Does this interface support broadcast ? */
706 int if_is_broadcast(const struct interface
*ifp
)
708 return ifp
->flags
& IFF_BROADCAST
;
711 /* Does this interface support broadcast ? */
712 int if_is_pointopoint(const struct interface
*ifp
)
714 return ifp
->flags
& IFF_POINTOPOINT
;
717 /* Does this interface support multicast ? */
718 int if_is_multicast(const struct interface
*ifp
)
720 return ifp
->flags
& IFF_MULTICAST
;
723 /* Printout flag information into log */
724 const char *if_flag_dump(unsigned long flag
)
727 static char logbuf
[BUFSIZ
];
729 #define IFF_OUT_LOG(X, STR) \
732 strlcat(logbuf, ",", sizeof(logbuf)); \
735 strlcat(logbuf, STR, sizeof(logbuf)); \
738 strlcpy(logbuf
, "<", BUFSIZ
);
739 IFF_OUT_LOG(IFF_UP
, "UP");
740 IFF_OUT_LOG(IFF_BROADCAST
, "BROADCAST");
741 IFF_OUT_LOG(IFF_DEBUG
, "DEBUG");
742 IFF_OUT_LOG(IFF_LOOPBACK
, "LOOPBACK");
743 IFF_OUT_LOG(IFF_POINTOPOINT
, "POINTOPOINT");
744 IFF_OUT_LOG(IFF_NOTRAILERS
, "NOTRAILERS");
745 IFF_OUT_LOG(IFF_RUNNING
, "RUNNING");
746 IFF_OUT_LOG(IFF_NOARP
, "NOARP");
747 IFF_OUT_LOG(IFF_PROMISC
, "PROMISC");
748 IFF_OUT_LOG(IFF_ALLMULTI
, "ALLMULTI");
749 IFF_OUT_LOG(IFF_OACTIVE
, "OACTIVE");
750 IFF_OUT_LOG(IFF_SIMPLEX
, "SIMPLEX");
751 IFF_OUT_LOG(IFF_LINK0
, "LINK0");
752 IFF_OUT_LOG(IFF_LINK1
, "LINK1");
753 IFF_OUT_LOG(IFF_LINK2
, "LINK2");
754 IFF_OUT_LOG(IFF_MULTICAST
, "MULTICAST");
755 IFF_OUT_LOG(IFF_NOXMIT
, "NOXMIT");
756 IFF_OUT_LOG(IFF_NORTEXCH
, "NORTEXCH");
757 IFF_OUT_LOG(IFF_VIRTUAL
, "VIRTUAL");
758 IFF_OUT_LOG(IFF_IPV4
, "IPv4");
759 IFF_OUT_LOG(IFF_IPV6
, "IPv6");
761 strlcat(logbuf
, ">", sizeof(logbuf
));
768 static void if_dump(const struct interface
*ifp
)
770 struct listnode
*node
;
771 struct connected
*c
__attribute__((unused
));
773 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, c
)) {
774 struct vrf
*vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
777 "Interface %s vrf %s(%u) index %d metric %d mtu %d "
779 ifp
->name
, VRF_LOGNAME(vrf
), ifp
->vrf_id
, ifp
->ifindex
,
780 ifp
->metric
, ifp
->mtu
, ifp
->mtu6
,
781 if_flag_dump(ifp
->flags
));
785 /* Interface printing for all interface. */
786 void if_dump_all(void)
791 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
792 FOR_ALL_INTERFACES (vrf
, ifp
)
797 /* Need to handle upgrade from SUNWzebra to Quagga. SUNWzebra created
798 * a seperate struct interface for each logical interface, so config
799 * file may be full of 'interface fooX:Y'. Solaris however does not
800 * expose logical interfaces via PF_ROUTE, so trying to track logical
801 * interfaces can be fruitless, for that reason Quagga only tracks
802 * the primary IP interface.
804 * We try accomodate SUNWzebra by:
805 * - looking up the interface name, to see whether it exists, if so
807 * - for protocol daemons, this could only because zebra told us of
809 * - for zebra, only because it learnt from kernel
811 * - search the name to see if it contains a sub-ipif / logical interface
812 * seperator, the ':' char. If it does:
813 * - text up to that char must be the primary name - get that name.
815 * - no idea, just get the name in its entirety.
817 static struct interface
*if_sunwzebra_get(const char *name
, vrf_id_t vrf_id
)
819 struct interface
*ifp
;
822 if ((ifp
= if_lookup_by_name(name
, vrf_id
)) != NULL
)
825 /* hunt the primary interface name... */
826 cp
= strchr(name
, ':');
830 return if_get_by_name(name
, vrf_id
);
835 /* For debug purpose. */
838 "show address [vrf NAME]",
844 struct listnode
*node
;
845 struct interface
*ifp
;
846 struct connected
*ifc
;
848 vrf_id_t vrf_id
= VRF_DEFAULT
;
851 VRF_GET_ID (vrf_id
, argv
[idx_vrf
]->arg
);
853 FOR_ALL_INTERFACES (vrf
, ifp
) {
854 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node
, ifc
)) {
857 if (p
->family
== AF_INET
)
858 vty_out (vty
, "%s/%d\n", inet_ntoa (p
->u
.prefix4
), p
->prefixlen
);
864 DEFUN (show_address_vrf_all
,
865 show_address_vrf_all_cmd
,
866 "show address vrf all",
869 VRF_ALL_CMD_HELP_STR
)
872 struct listnode
*node
;
873 struct interface
*ifp
;
874 struct connected
*ifc
;
877 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
879 if (RB_EMPTY (if_name_head
, &vrf
->ifaces_by_name
))
882 vty_out (vty
, "\nVRF %s(%u)\n\n",
883 VRF_LOGNAME(vrf
), vrf
->vrf_id
);
885 FOR_ALL_INTERFACES (vrf
, ifp
) {
886 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node
, ifc
)) {
889 if (p
->family
== AF_INET
)
890 vty_out (vty
, "%s/%d\n", inet_ntoa (p
->u
.prefix4
), p
->prefixlen
);
898 /* Allocate connected structure. */
899 struct connected
*connected_new(void)
901 return XCALLOC(MTYPE_CONNECTED
, sizeof(struct connected
));
904 /* Allocate nbr connected structure. */
905 struct nbr_connected
*nbr_connected_new(void)
907 return XCALLOC(MTYPE_NBR_CONNECTED
, sizeof(struct nbr_connected
));
910 /* Free connected structure. */
911 void connected_free(struct connected
**connected
)
913 struct connected
*ptr
= *connected
;
915 prefix_free(&ptr
->address
);
916 prefix_free(&ptr
->destination
);
918 XFREE(MTYPE_CONNECTED_LABEL
, ptr
->label
);
920 XFREE(MTYPE_CONNECTED
, ptr
);
924 /* Free nbr connected structure. */
925 void nbr_connected_free(struct nbr_connected
*connected
)
927 if (connected
->address
)
928 prefix_free(&connected
->address
);
930 XFREE(MTYPE_NBR_CONNECTED
, connected
);
933 /* If same interface nbr address already exists... */
934 struct nbr_connected
*nbr_connected_check(struct interface
*ifp
,
937 struct nbr_connected
*ifc
;
938 struct listnode
*node
;
940 for (ALL_LIST_ELEMENTS_RO(ifp
->nbr_connected
, node
, ifc
))
941 if (prefix_same(ifc
->address
, p
))
947 /* Print if_addr structure. */
948 static void __attribute__((unused
))
949 connected_log(struct connected
*connected
, char *str
)
952 struct interface
*ifp
;
957 ifp
= connected
->ifp
;
958 p
= connected
->address
;
960 vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
961 snprintf(logbuf
, sizeof(logbuf
), "%s interface %s vrf %s(%u) %s %s/%d ",
962 str
, ifp
->name
, VRF_LOGNAME(vrf
), ifp
->vrf_id
,
963 prefix_family_str(p
),
964 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
), p
->prefixlen
);
966 p
= connected
->destination
;
968 strncat(logbuf
, inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
969 BUFSIZ
- strlen(logbuf
));
971 zlog_info("%s", logbuf
);
974 /* Print if_addr structure. */
975 static void __attribute__((unused
))
976 nbr_connected_log(struct nbr_connected
*connected
, char *str
)
979 struct interface
*ifp
;
983 ifp
= connected
->ifp
;
984 p
= connected
->address
;
986 snprintf(logbuf
, sizeof(logbuf
), "%s interface %s %s %s/%d ", str
,
987 ifp
->name
, prefix_family_str(p
),
988 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
), p
->prefixlen
);
990 zlog_info("%s", logbuf
);
993 /* If two connected address has same prefix return 1. */
994 static int connected_same_prefix(const struct prefix
*p1
,
995 const struct prefix
*p2
)
997 if (p1
->family
== p2
->family
) {
998 if (p1
->family
== AF_INET
999 && IPV4_ADDR_SAME(&p1
->u
.prefix4
, &p2
->u
.prefix4
))
1001 if (p1
->family
== AF_INET6
1002 && IPV6_ADDR_SAME(&p1
->u
.prefix6
, &p2
->u
.prefix6
))
1008 /* count the number of connected addresses that are in the given family */
1009 unsigned int connected_count_by_family(struct interface
*ifp
, int family
)
1011 struct listnode
*cnode
;
1012 struct connected
*connected
;
1013 unsigned int cnt
= 0;
1015 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
))
1016 if (connected
->address
->family
== family
)
1022 struct connected
*connected_lookup_prefix_exact(struct interface
*ifp
,
1023 const struct prefix
*p
)
1025 struct listnode
*node
;
1026 struct listnode
*next
;
1027 struct connected
*ifc
;
1029 for (node
= listhead(ifp
->connected
); node
; node
= next
) {
1030 ifc
= listgetdata(node
);
1033 if (connected_same_prefix(ifc
->address
, p
))
1039 struct connected
*connected_delete_by_prefix(struct interface
*ifp
,
1042 struct listnode
*node
;
1043 struct listnode
*next
;
1044 struct connected
*ifc
;
1046 /* In case of same prefix come, replace it with new one. */
1047 for (node
= listhead(ifp
->connected
); node
; node
= next
) {
1048 ifc
= listgetdata(node
);
1051 if (connected_same_prefix(ifc
->address
, p
)) {
1052 listnode_delete(ifp
->connected
, ifc
);
1059 /* Find the address on our side that will be used when packets
1061 struct connected
*connected_lookup_prefix(struct interface
*ifp
,
1062 const struct prefix
*addr
)
1064 struct listnode
*cnode
;
1065 struct connected
*c
;
1066 struct connected
*match
;
1070 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
1071 if (c
->address
&& (c
->address
->family
== addr
->family
)
1072 && prefix_match(CONNECTED_PREFIX(c
), addr
)
1074 || (c
->address
->prefixlen
> match
->address
->prefixlen
)))
1080 struct connected
*connected_add_by_prefix(struct interface
*ifp
,
1082 struct prefix
*destination
)
1084 struct connected
*ifc
;
1086 /* Allocate new connected address. */
1087 ifc
= connected_new();
1090 /* Fetch interface address */
1091 ifc
->address
= prefix_new();
1092 memcpy(ifc
->address
, p
, sizeof(struct prefix
));
1094 /* Fetch dest address */
1096 ifc
->destination
= prefix_new();
1097 memcpy(ifc
->destination
, destination
, sizeof(struct prefix
));
1100 /* Add connected address to the interface. */
1101 listnode_add(ifp
->connected
, ifc
);
1105 struct connected
*connected_get_linklocal(struct interface
*ifp
)
1108 struct connected
*c
= NULL
;
1110 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, n
, c
)) {
1111 if (c
->address
->family
== AF_INET6
1112 && IN6_IS_ADDR_LINKLOCAL(&c
->address
->u
.prefix6
))
1118 #if 0 /* this route_table of struct connected's is unused \
1119 * however, it would be good to use a route_table rather than \
1122 /* Interface looking up by interface's address. */
1123 /* Interface's IPv4 address reverse lookup table. */
1124 struct route_table
*ifaddr_ipv4_table
;
1125 /* struct route_table *ifaddr_ipv6_table; */
1128 ifaddr_ipv4_add (struct in_addr
*ifaddr
, struct interface
*ifp
)
1130 struct route_node
*rn
;
1131 struct prefix_ipv4 p
;
1134 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1137 rn
= route_node_get (ifaddr_ipv4_table
, (struct prefix
*) &p
);
1140 route_unlock_node (rn
);
1141 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
1142 inet_ntoa (*ifaddr
));
1149 ifaddr_ipv4_delete (struct in_addr
*ifaddr
, struct interface
*ifp
)
1151 struct route_node
*rn
;
1152 struct prefix_ipv4 p
;
1155 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1158 rn
= route_node_lookup (ifaddr_ipv4_table
, (struct prefix
*) &p
);
1161 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
1162 inet_ntoa (*ifaddr
));
1166 route_unlock_node (rn
);
1167 route_unlock_node (rn
);
1170 /* Lookup interface by interface's IP address or interface index. */
1171 static struct interface
*
1172 ifaddr_ipv4_lookup (struct in_addr
*addr
, ifindex_t ifindex
)
1174 struct prefix_ipv4 p
;
1175 struct route_node
*rn
;
1176 struct interface
*ifp
;
1181 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1184 rn
= route_node_lookup (ifaddr_ipv4_table
, (struct prefix
*) &p
);
1189 route_unlock_node (rn
);
1193 return if_lookup_by_index(ifindex
, VRF_DEFAULT
);
1195 #endif /* ifaddr_ipv4_table */
1197 void if_terminate(struct vrf
*vrf
)
1199 struct interface
*ifp
;
1201 while (!RB_EMPTY(if_name_head
, &vrf
->ifaces_by_name
)) {
1202 ifp
= RB_ROOT(if_name_head
, &vrf
->ifaces_by_name
);
1205 ifp
->node
->info
= NULL
;
1206 route_unlock_node(ifp
->node
);
1212 const char *if_link_type_str(enum zebra_link_type llt
)
1215 #define llts(T,S) case (T): return (S)
1216 llts(ZEBRA_LLT_UNKNOWN
, "Unknown");
1217 llts(ZEBRA_LLT_ETHER
, "Ethernet");
1218 llts(ZEBRA_LLT_EETHER
, "Experimental Ethernet");
1219 llts(ZEBRA_LLT_AX25
, "AX.25 Level 2");
1220 llts(ZEBRA_LLT_PRONET
, "PROnet token ring");
1221 llts(ZEBRA_LLT_IEEE802
, "IEEE 802.2 Ethernet/TR/TB");
1222 llts(ZEBRA_LLT_ARCNET
, "ARCnet");
1223 llts(ZEBRA_LLT_APPLETLK
, "AppleTalk");
1224 llts(ZEBRA_LLT_DLCI
, "Frame Relay DLCI");
1225 llts(ZEBRA_LLT_ATM
, "ATM");
1226 llts(ZEBRA_LLT_METRICOM
, "Metricom STRIP");
1227 llts(ZEBRA_LLT_IEEE1394
, "IEEE 1394 IPv4");
1228 llts(ZEBRA_LLT_EUI64
, "EUI-64");
1229 llts(ZEBRA_LLT_INFINIBAND
, "InfiniBand");
1230 llts(ZEBRA_LLT_SLIP
, "SLIP");
1231 llts(ZEBRA_LLT_CSLIP
, "Compressed SLIP");
1232 llts(ZEBRA_LLT_SLIP6
, "SLIPv6");
1233 llts(ZEBRA_LLT_CSLIP6
, "Compressed SLIPv6");
1234 llts(ZEBRA_LLT_ROSE
, "ROSE packet radio");
1235 llts(ZEBRA_LLT_X25
, "CCITT X.25");
1236 llts(ZEBRA_LLT_PPP
, "PPP");
1237 llts(ZEBRA_LLT_CHDLC
, "Cisco HDLC");
1238 llts(ZEBRA_LLT_RAWHDLC
, "Raw HDLC");
1239 llts(ZEBRA_LLT_LAPB
, "LAPB");
1240 llts(ZEBRA_LLT_IPIP
, "IPIP Tunnel");
1241 llts(ZEBRA_LLT_IPIP6
, "IPIP6 Tunnel");
1242 llts(ZEBRA_LLT_FRAD
, "FRAD");
1243 llts(ZEBRA_LLT_SKIP
, "SKIP vif");
1244 llts(ZEBRA_LLT_LOOPBACK
, "Loopback");
1245 llts(ZEBRA_LLT_LOCALTLK
, "Localtalk");
1246 llts(ZEBRA_LLT_FDDI
, "FDDI");
1247 llts(ZEBRA_LLT_SIT
, "IPv6-in-IPv4 SIT");
1248 llts(ZEBRA_LLT_IPDDP
, "IP-in-DDP tunnel");
1249 llts(ZEBRA_LLT_IPGRE
, "GRE over IP");
1250 llts(ZEBRA_LLT_PIMREG
, "PIMSM registration");
1251 llts(ZEBRA_LLT_HIPPI
, "HiPPI");
1252 llts(ZEBRA_LLT_IRDA
, "IrDA");
1253 llts(ZEBRA_LLT_FCPP
, "Fibre-Channel PtP");
1254 llts(ZEBRA_LLT_FCAL
, "Fibre-Channel Arbitrated Loop");
1255 llts(ZEBRA_LLT_FCPL
, "Fibre-Channel Public Loop");
1256 llts(ZEBRA_LLT_FCFABRIC
, "Fibre-Channel Fabric");
1257 llts(ZEBRA_LLT_IEEE802_TR
, "IEEE 802.2 Token Ring");
1258 llts(ZEBRA_LLT_IEEE80211
, "IEEE 802.11");
1259 llts(ZEBRA_LLT_IEEE80211_RADIOTAP
, "IEEE 802.11 Radiotap");
1260 llts(ZEBRA_LLT_IEEE802154
, "IEEE 802.15.4");
1261 llts(ZEBRA_LLT_IEEE802154_PHY
, "IEEE 802.15.4 Phy");
1263 flog_err(EC_LIB_DEVELOPMENT
, "Unknown value %d", llt
);
1264 return "Unknown type!";
1270 struct if_link_params
*if_link_params_get(struct interface
*ifp
)
1274 if (ifp
->link_params
!= NULL
)
1275 return ifp
->link_params
;
1277 struct if_link_params
*iflp
=
1278 XCALLOC(MTYPE_IF_LINK_PARAMS
, sizeof(struct if_link_params
));
1280 /* Set TE metric equal to standard metric */
1281 iflp
->te_metric
= ifp
->metric
;
1283 /* Compute default bandwidth based on interface */
1285 ((ifp
->bandwidth
? ifp
->bandwidth
: DEFAULT_BANDWIDTH
)
1286 * TE_MEGA_BIT
/ TE_BYTE
);
1288 /* Set Max, Reservable and Unreserved Bandwidth */
1289 iflp
->max_bw
= iflp
->default_bw
;
1290 iflp
->max_rsv_bw
= iflp
->default_bw
;
1291 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1292 iflp
->unrsv_bw
[i
] = iflp
->default_bw
;
1294 /* Update Link parameters status */
1296 LP_TE_METRIC
| LP_MAX_BW
| LP_MAX_RSV_BW
| LP_UNRSV_BW
;
1298 /* Finally attach newly created Link Parameters */
1299 ifp
->link_params
= iflp
;
1304 void if_link_params_free(struct interface
*ifp
)
1306 XFREE(MTYPE_IF_LINK_PARAMS
, ifp
->link_params
);
1309 /* ----------- CLI commands ----------- */
1312 * XPath: /frr-interface:lib/interface
1314 DEFPY_NOSH (interface
,
1316 "interface IFNAME [vrf NAME$vrf_name]",
1317 "Select an interface to configure\n"
1318 "Interface's name\n"
1321 char xpath_list
[XPATH_MAXLEN
];
1323 struct interface
*ifp
;
1327 vrf_name
= VRF_DEFAULT_NAME
;
1330 * This command requires special handling to maintain backward
1331 * compatibility. If a VRF name is not specified, it means we're willing
1332 * to accept any interface with the given name on any VRF. If no
1333 * interface is found, then a new one should be created on the default
1336 VRF_GET_ID(vrf_id
, vrf_name
, false);
1337 ifp
= if_lookup_by_name_all_vrf(ifname
);
1338 if (ifp
&& ifp
->vrf_id
!= vrf_id
) {
1342 * Special case 1: a VRF name was specified, but the found
1343 * interface is associated to different VRF. Reject the command.
1345 if (vrf_id
!= VRF_DEFAULT
) {
1346 vty_out(vty
, "%% interface %s not in %s vrf\n", ifname
,
1348 return CMD_WARNING_CONFIG_FAILED
;
1352 * Special case 2: a VRF name was *not* specified, and the found
1353 * interface is associated to a VRF other than the default one.
1354 * Update vrf_id and vrf_name to account for that.
1356 vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
1358 vrf_id
= ifp
->vrf_id
;
1359 vrf_name
= vrf
->name
;
1362 snprintf(xpath_list
, sizeof(xpath_list
),
1363 "/frr-interface:lib/interface[name='%s'][vrf='%s']", ifname
,
1366 nb_cli_enqueue_change(vty
, ".", NB_OP_CREATE
, NULL
);
1367 ret
= nb_cli_apply_changes(vty
, xpath_list
);
1368 if (ret
== CMD_SUCCESS
) {
1369 VTY_PUSH_XPATH(INTERFACE_NODE
, xpath_list
);
1372 * For backward compatibility with old commands we still need
1373 * to use the qobj infrastructure. This can be removed once
1374 * all interface-level commands are converted to the new
1377 ifp
= if_lookup_by_name(ifname
, vrf_id
);
1379 VTY_PUSH_CONTEXT(INTERFACE_NODE
, ifp
);
1385 DEFPY (no_interface
,
1387 "no interface IFNAME [vrf NAME$vrf_name]",
1389 "Delete a pseudo interface's configuration\n"
1390 "Interface's name\n"
1394 vrf_name
= VRF_DEFAULT_NAME
;
1396 nb_cli_enqueue_change(vty
, ".", NB_OP_DESTROY
, NULL
);
1398 return nb_cli_apply_changes(
1399 vty
, "/frr-interface:lib/interface[name='%s'][vrf='%s']",
1403 static void cli_show_interface(struct vty
*vty
, struct lyd_node
*dnode
,
1408 vrf
= yang_dnode_get_string(dnode
, "./vrf");
1410 vty_out(vty
, "!\n");
1411 vty_out(vty
, "interface %s", yang_dnode_get_string(dnode
, "./name"));
1412 if (!strmatch(vrf
, VRF_DEFAULT_NAME
))
1413 vty_out(vty
, " vrf %s", vrf
);
1418 * XPath: /frr-interface:lib/interface/description
1420 DEFPY (interface_desc
,
1422 "description LINE...",
1423 "Interface specific description\n"
1424 "Characters describing this interface\n")
1429 desc
= argv_concat(argv
, argc
, 1);
1430 nb_cli_enqueue_change(vty
, "./description", NB_OP_MODIFY
, desc
);
1431 ret
= nb_cli_apply_changes(vty
, NULL
);
1432 XFREE(MTYPE_TMP
, desc
);
1437 DEFPY (no_interface_desc
,
1438 no_interface_desc_cmd
,
1441 "Interface specific description\n")
1443 nb_cli_enqueue_change(vty
, "./description", NB_OP_DESTROY
, NULL
);
1445 return nb_cli_apply_changes(vty
, NULL
);
1448 static void cli_show_interface_desc(struct vty
*vty
, struct lyd_node
*dnode
,
1451 vty_out(vty
, " description %s\n", yang_dnode_get_string(dnode
, NULL
));
1454 /* Interface autocomplete. */
1455 static void if_autocomplete(vector comps
, struct cmd_token
*token
)
1457 struct interface
*ifp
;
1460 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
1461 FOR_ALL_INTERFACES (vrf
, ifp
) {
1462 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, ifp
->name
));
1467 static const struct cmd_variable_handler if_var_handlers
[] = {
1468 {/* "interface NAME" */
1469 .varname
= "interface",
1470 .completions
= if_autocomplete
},
1471 {.tokenname
= "IFNAME", .completions
= if_autocomplete
},
1472 {.tokenname
= "INTERFACE", .completions
= if_autocomplete
},
1473 {.completions
= NULL
}};
1475 void if_cmd_init(void)
1477 cmd_variable_handler_register(if_var_handlers
);
1479 install_element(CONFIG_NODE
, &interface_cmd
);
1480 install_element(CONFIG_NODE
, &no_interface_cmd
);
1482 install_default(INTERFACE_NODE
);
1483 install_element(INTERFACE_NODE
, &interface_desc_cmd
);
1484 install_element(INTERFACE_NODE
, &no_interface_desc_cmd
);
1487 void if_zapi_callbacks(int (*create
)(struct interface
*ifp
),
1488 int (*up
)(struct interface
*ifp
),
1489 int (*down
)(struct interface
*ifp
),
1490 int (*destroy
)(struct interface
*ifp
))
1492 ifp_master
.create_hook
= create
;
1493 ifp_master
.up_hook
= up
;
1494 ifp_master
.down_hook
= down
;
1495 ifp_master
.destroy_hook
= destroy
;
1498 /* ------- Northbound callbacks ------- */
1501 * XPath: /frr-interface:lib/interface
1503 static int lib_interface_create(struct nb_cb_create_args
*args
)
1506 const char *vrfname
;
1508 struct interface
*ifp
;
1510 ifname
= yang_dnode_get_string(args
->dnode
, "./name");
1511 vrfname
= yang_dnode_get_string(args
->dnode
, "./vrf");
1513 switch (args
->event
) {
1514 case NB_EV_VALIDATE
:
1515 vrf
= vrf_lookup_by_name(vrfname
);
1517 zlog_warn("%s: VRF %s doesn't exist", __func__
,
1519 return NB_ERR_VALIDATION
;
1521 if (vrf
->vrf_id
== VRF_UNKNOWN
) {
1522 zlog_warn("%s: VRF %s is not active", __func__
,
1524 return NB_ERR_VALIDATION
;
1527 /* if VRF is netns or not yet known - init for instance
1528 * then assumption is that passed config is exact
1529 * then the user intent was not to use an other iface
1531 if (vrf_get_backend() == VRF_BACKEND_VRF_LITE
) {
1532 ifp
= if_lookup_by_name_all_vrf(ifname
);
1533 if (ifp
&& ifp
->vrf_id
!= vrf
->vrf_id
) {
1535 "%s: interface %s already exists in another VRF",
1536 __func__
, ifp
->name
);
1537 return NB_ERR_VALIDATION
;
1545 vrf
= vrf_lookup_by_name(vrfname
);
1548 ifp
= if_sunwzebra_get(ifname
, vrf
->vrf_id
);
1550 ifp
= if_get_by_name(ifname
, vrf
->vrf_id
);
1551 #endif /* SUNOS_5 */
1553 ifp
->configured
= true;
1554 nb_running_set_entry(args
->dnode
, ifp
);
1561 static int lib_interface_destroy(struct nb_cb_destroy_args
*args
)
1563 struct interface
*ifp
;
1566 switch (args
->event
) {
1567 case NB_EV_VALIDATE
:
1568 ifp
= nb_running_get_entry(args
->dnode
, NULL
, true);
1569 if (CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
)) {
1570 zlog_warn("%s: only inactive interfaces can be deleted",
1572 return NB_ERR_VALIDATION
;
1579 ifp
= nb_running_unset_entry(args
->dnode
);
1581 ifp
->configured
= false;
1590 * XPath: /frr-interface:lib/interface
1592 static const void *lib_interface_get_next(struct nb_cb_get_next_args
*args
)
1595 struct interface
*pif
= (struct interface
*)args
->list_entry
;
1597 if (args
->list_entry
== NULL
) {
1598 vrf
= RB_MIN(vrf_name_head
, &vrfs_by_name
);
1600 pif
= RB_MIN(if_name_head
, &vrf
->ifaces_by_name
);
1602 vrf
= vrf_lookup_by_id(pif
->vrf_id
);
1603 pif
= RB_NEXT(if_name_head
, pif
);
1604 /* if no more interfaces, switch to next vrf */
1605 while (pif
== NULL
) {
1606 vrf
= RB_NEXT(vrf_name_head
, vrf
);
1609 pif
= RB_MIN(if_name_head
, &vrf
->ifaces_by_name
);
1616 static int lib_interface_get_keys(struct nb_cb_get_keys_args
*args
)
1618 const struct interface
*ifp
= args
->list_entry
;
1620 struct vrf
*vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
1624 args
->keys
->num
= 2;
1625 strlcpy(args
->keys
->key
[0], ifp
->name
, sizeof(args
->keys
->key
[0]));
1626 strlcpy(args
->keys
->key
[1], vrf
->name
, sizeof(args
->keys
->key
[1]));
1632 lib_interface_lookup_entry(struct nb_cb_lookup_entry_args
*args
)
1634 const char *ifname
= args
->keys
->key
[0];
1635 const char *vrfname
= args
->keys
->key
[1];
1636 struct vrf
*vrf
= vrf_lookup_by_name(vrfname
);
1638 return vrf
? if_lookup_by_name(ifname
, vrf
->vrf_id
) : NULL
;
1642 * XPath: /frr-interface:lib/interface/description
1644 static int lib_interface_description_modify(struct nb_cb_modify_args
*args
)
1646 struct interface
*ifp
;
1647 const char *description
;
1649 if (args
->event
!= NB_EV_APPLY
)
1652 ifp
= nb_running_get_entry(args
->dnode
, NULL
, true);
1653 XFREE(MTYPE_TMP
, ifp
->desc
);
1654 description
= yang_dnode_get_string(args
->dnode
, NULL
);
1655 ifp
->desc
= XSTRDUP(MTYPE_TMP
, description
);
1660 static int lib_interface_description_destroy(struct nb_cb_destroy_args
*args
)
1662 struct interface
*ifp
;
1664 if (args
->event
!= NB_EV_APPLY
)
1667 ifp
= nb_running_get_entry(args
->dnode
, NULL
, true);
1668 XFREE(MTYPE_TMP
, ifp
->desc
);
1674 * XPath: /frr-interface:lib/interface/state/if-index
1676 static struct yang_data
*
1677 lib_interface_state_if_index_get_elem(struct nb_cb_get_elem_args
*args
)
1679 const struct interface
*ifp
= args
->list_entry
;
1681 return yang_data_new_int32(args
->xpath
, ifp
->ifindex
);
1685 * XPath: /frr-interface:lib/interface/state/mtu
1687 static struct yang_data
*
1688 lib_interface_state_mtu_get_elem(struct nb_cb_get_elem_args
*args
)
1690 const struct interface
*ifp
= args
->list_entry
;
1692 return yang_data_new_uint16(args
->xpath
, ifp
->mtu
);
1696 * XPath: /frr-interface:lib/interface/state/mtu6
1698 static struct yang_data
*
1699 lib_interface_state_mtu6_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
->mtu6
);
1707 * XPath: /frr-interface:lib/interface/state/speed
1709 static struct yang_data
*
1710 lib_interface_state_speed_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
->speed
);
1718 * XPath: /frr-interface:lib/interface/state/metric
1720 static struct yang_data
*
1721 lib_interface_state_metric_get_elem(struct nb_cb_get_elem_args
*args
)
1723 const struct interface
*ifp
= args
->list_entry
;
1725 return yang_data_new_uint32(args
->xpath
, ifp
->metric
);
1729 * XPath: /frr-interface:lib/interface/state/flags
1731 static struct yang_data
*
1732 lib_interface_state_flags_get_elem(struct nb_cb_get_elem_args
*args
)
1734 /* TODO: implement me. */
1739 * XPath: /frr-interface:lib/interface/state/type
1741 static struct yang_data
*
1742 lib_interface_state_type_get_elem(struct nb_cb_get_elem_args
*args
)
1744 /* TODO: implement me. */
1749 * XPath: /frr-interface:lib/interface/state/phy-address
1751 static struct yang_data
*
1752 lib_interface_state_phy_address_get_elem(struct nb_cb_get_elem_args
*args
)
1754 const struct interface
*ifp
= args
->list_entry
;
1755 struct ethaddr macaddr
;
1757 memcpy(&macaddr
.octet
, ifp
->hw_addr
, ETH_ALEN
);
1759 return yang_data_new_mac(args
->xpath
, &macaddr
);
1762 /* clang-format off */
1763 const struct frr_yang_module_info frr_interface_info
= {
1764 .name
= "frr-interface",
1767 .xpath
= "/frr-interface:lib/interface",
1769 .create
= lib_interface_create
,
1770 .destroy
= lib_interface_destroy
,
1771 .cli_show
= cli_show_interface
,
1772 .get_next
= lib_interface_get_next
,
1773 .get_keys
= lib_interface_get_keys
,
1774 .lookup_entry
= lib_interface_lookup_entry
,
1778 .xpath
= "/frr-interface:lib/interface/description",
1780 .modify
= lib_interface_description_modify
,
1781 .destroy
= lib_interface_description_destroy
,
1782 .cli_show
= cli_show_interface_desc
,
1786 .xpath
= "/frr-interface:lib/interface/state/if-index",
1788 .get_elem
= lib_interface_state_if_index_get_elem
,
1792 .xpath
= "/frr-interface:lib/interface/state/mtu",
1794 .get_elem
= lib_interface_state_mtu_get_elem
,
1798 .xpath
= "/frr-interface:lib/interface/state/mtu6",
1800 .get_elem
= lib_interface_state_mtu6_get_elem
,
1804 .xpath
= "/frr-interface:lib/interface/state/speed",
1806 .get_elem
= lib_interface_state_speed_get_elem
,
1810 .xpath
= "/frr-interface:lib/interface/state/metric",
1812 .get_elem
= lib_interface_state_metric_get_elem
,
1816 .xpath
= "/frr-interface:lib/interface/state/flags",
1818 .get_elem
= lib_interface_state_flags_get_elem
,
1822 .xpath
= "/frr-interface:lib/interface/state/type",
1824 .get_elem
= lib_interface_state_type_get_elem
,
1828 .xpath
= "/frr-interface:lib/interface/state/phy-address",
1830 .get_elem
= lib_interface_state_phy_address_get_elem
,