]> git.proxmox.com Git - mirror_frr.git/blame - lib/if.c
Merge pull request #4423 from ton31337/feature/delete_prefix_list_by_sequence_number_7.1
[mirror_frr.git] / lib / if.c
CommitLineData
d62a17ae 1/*
718e3744 2 * Interface functions.
3 * Copyright (C) 1997, 98 Kunihiro Ishiguro
4 *
5 * This file is part of GNU Zebra.
896014f4 6 *
718e3744 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.
11 *
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.
16 *
896014f4
DL
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
718e3744 20 */
21
22#include <zebra.h>
23
24#include "linklist.h"
25#include "vector.h"
4d43f68a 26#include "lib_errors.h"
718e3744 27#include "vty.h"
28#include "command.h"
8736158a 29#include "vrf.h"
718e3744 30#include "if.h"
31#include "sockunion.h"
32#include "prefix.h"
718e3744 33#include "memory.h"
34#include "table.h"
35#include "buffer.h"
718e3744 36#include "log.h"
8f90d89b
RW
37#include "northbound_cli.h"
38#ifndef VTYSH_EXTRACT_PL
39#include "lib/if_clippy.c"
40#endif
6b0655a2 41
d62a17ae 42DEFINE_MTYPE(LIB, IF, "Interface")
43DEFINE_MTYPE_STATIC(LIB, CONNECTED, "Connected")
44DEFINE_MTYPE_STATIC(LIB, NBR_CONNECTED, "Neighbor Connected")
45DEFINE_MTYPE(LIB, CONNECTED_LABEL, "Connected interface label")
46DEFINE_MTYPE_STATIC(LIB, IF_LINK_PARAMS, "Informational Link Parameters")
4a1ab8e4 47
f4e14fdb 48static int if_cmp_func(const struct interface *, const struct interface *);
ff880b78
RW
49static int if_cmp_index_func(const struct interface *ifp1,
50 const struct interface *ifp2);
f4e14fdb 51RB_GENERATE(if_name_head, interface, name_entry, if_cmp_func);
ff880b78 52RB_GENERATE(if_index_head, interface, index_entry, if_cmp_index_func);
f4e14fdb 53
e80e7cce
DL
54DEFINE_QOBJ_TYPE(interface)
55
996c9314
LB
56DEFINE_HOOK(if_add, (struct interface * ifp), (ifp))
57DEFINE_KOOH(if_del, (struct interface * ifp), (ifp))
ce19a04a 58
3a0391a9 59/* Compare interface names, returning an integer greater than, equal to, or
60 * less than 0, (following the strcmp convention), according to the
61 * relationship between ifp1 and ifp2. Interface names consist of an
62 * alphabetic prefix and a numeric suffix. The primary sort key is
63 * lexicographic by name, and then numeric by number. No number sorts
64 * before all numbers. Examples: de0 < de1, de100 < fxp0 < xl0, devpty <
65 * devpty0, de0 < del0
d62a17ae 66 */
36de6e0e 67int if_cmp_name_func(const char *p1, const char *p2)
106d2fd5 68{
d62a17ae 69 unsigned int l1, l2;
70 long int x1, x2;
71 int res;
72
73 while (*p1 && *p2) {
74 /* look up to any number */
75 l1 = strcspn(p1, "0123456789");
76 l2 = strcspn(p2, "0123456789");
77
78 /* name lengths are different -> compare names */
79 if (l1 != l2)
80 return (strcmp(p1, p2));
81
82 /* Note that this relies on all numbers being less than all
83 * letters, so
84 * that de0 < del0.
85 */
86 res = strncmp(p1, p2, l1);
87
88 /* names are different -> compare them */
89 if (res)
90 return res;
91
92 /* with identical name part, go to numeric part */
93 p1 += l1;
94 p2 += l1;
95
c9cbbb40
RW
96 if (!*p1 && !*p2)
97 return 0;
d62a17ae 98 if (!*p1)
99 return -1;
100 if (!*p2)
101 return 1;
102
36de6e0e
A
103 x1 = strtol(p1, (char **)&p1, 10);
104 x2 = strtol(p2, (char **)&p2, 10);
d62a17ae 105
106 /* let's compare numbers now */
107 if (x1 < x2)
108 return -1;
109 if (x1 > x2)
110 return 1;
111
112 /* numbers were equal, lets do it again..
113 (it happens with name like "eth123.456:789") */
114 }
115 if (*p1)
116 return 1;
117 if (*p2)
118 return -1;
119 return 0;
106d2fd5 120}
121
f4e14fdb
RW
122static int if_cmp_func(const struct interface *ifp1,
123 const struct interface *ifp2)
974fc9d2 124{
36de6e0e 125 return if_cmp_name_func(ifp1->name, ifp2->name);
974fc9d2
DS
126}
127
ff880b78
RW
128static int if_cmp_index_func(const struct interface *ifp1,
129 const struct interface *ifp2)
130{
131 return ifp1->ifindex - ifp2->ifindex;
132}
133
718e3744 134/* Create new interface structure. */
bcc24579 135struct interface *if_create(const char *name, vrf_id_t vrf_id)
718e3744 136{
f4e14fdb 137 struct vrf *vrf = vrf_get(vrf_id, NULL);
d62a17ae 138 struct interface *ifp;
d62a17ae 139
140 ifp = XCALLOC(MTYPE_IF, sizeof(struct interface));
141 ifp->ifindex = IFINDEX_INTERNAL;
142
143 assert(name);
bcc24579 144 strlcpy(ifp->name, name, sizeof(ifp->name));
d62a17ae 145 ifp->vrf_id = vrf_id;
ff880b78 146 IFNAME_RB_INSERT(vrf, ifp);
d62a17ae 147 ifp->connected = list_new();
148 ifp->connected->del = (void (*)(void *))connected_free;
149
150 ifp->nbr_connected = list_new();
151 ifp->nbr_connected->del = (void (*)(void *))nbr_connected_free;
152
153 /* Enable Link-detection by default */
154 SET_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION);
155
156 QOBJ_REG(ifp, interface);
996c9314 157 hook_call(if_add, ifp);
d62a17ae 158 return ifp;
718e3744 159}
160
f93e3f69 161/* Create new interface structure. */
d62a17ae 162void if_update_to_new_vrf(struct interface *ifp, vrf_id_t vrf_id)
f93e3f69 163{
8f90d89b 164 struct vrf *old_vrf, *vrf;
d62a17ae 165
166 /* remove interface from old master vrf list */
8f90d89b
RW
167 old_vrf = vrf_lookup_by_id(ifp->vrf_id);
168 if (old_vrf) {
169 IFNAME_RB_REMOVE(old_vrf, ifp);
ff880b78 170 if (ifp->ifindex != IFINDEX_INTERNAL)
8f90d89b 171 IFINDEX_RB_REMOVE(old_vrf, ifp);
ff880b78 172 }
d62a17ae 173
174 ifp->vrf_id = vrf_id;
f4e14fdb 175 vrf = vrf_get(ifp->vrf_id, NULL);
ff880b78
RW
176
177 IFNAME_RB_INSERT(vrf, ifp);
178 if (ifp->ifindex != IFINDEX_INTERNAL)
179 IFINDEX_RB_INSERT(vrf, ifp);
8f90d89b
RW
180
181 /*
182 * HACK: Change the interface VRF in the running configuration directly,
183 * bypassing the northbound layer. This is necessary to avoid deleting
184 * the interface and readding it in the new VRF, which would have
185 * several implications.
186 */
187 if (yang_module_find("frr-interface")) {
188 struct lyd_node *if_dnode;
189
83981138
RW
190 pthread_rwlock_wrlock(&running_config->lock);
191 {
192 if_dnode = yang_dnode_get(
193 running_config->dnode,
194 "/frr-interface:lib/interface[name='%s'][vrf='%s']/vrf",
195 ifp->name, old_vrf->name);
196 if (if_dnode) {
197 yang_dnode_change_leaf(if_dnode, vrf->name);
198 running_config->version++;
199 }
8f90d89b 200 }
83981138 201 pthread_rwlock_unlock(&running_config->lock);
8f90d89b 202 }
f93e3f69
DS
203}
204
d2fc8896 205/* Delete interface structure. */
d62a17ae 206void if_delete_retain(struct interface *ifp)
718e3744 207{
996c9314 208 hook_call(if_del, ifp);
d62a17ae 209 QOBJ_UNREG(ifp);
e80e7cce 210
d62a17ae 211 /* Free connected address list */
212 list_delete_all_node(ifp->connected);
a80beece 213
d62a17ae 214 /* Free connected nbr address list */
215 list_delete_all_node(ifp->nbr_connected);
d2fc8896 216}
217
218/* Delete and free interface structure. */
d62a17ae 219void if_delete(struct interface *ifp)
d2fc8896 220{
5b8524f5
RW
221 struct vrf *vrf;
222
223 vrf = vrf_lookup_by_id(ifp->vrf_id);
224 assert(vrf);
f4e14fdb 225
ff880b78
RW
226 IFNAME_RB_REMOVE(vrf, ifp);
227 if (ifp->ifindex != IFINDEX_INTERNAL)
228 IFINDEX_RB_REMOVE(vrf, ifp);
d2fc8896 229
d62a17ae 230 if_delete_retain(ifp);
718e3744 231
6a154c88
DL
232 list_delete(&ifp->connected);
233 list_delete(&ifp->nbr_connected);
2dd04c5d 234
d62a17ae 235 if_link_params_free(ifp);
16f1b9ee 236
0a22ddfb 237 XFREE(MTYPE_TMP, ifp->desc);
c7974c0f 238
d62a17ae 239 XFREE(MTYPE_IF, ifp);
718e3744 240}
241
718e3744 242/* Interface existance check by index. */
d62a17ae 243struct interface *if_lookup_by_index(ifindex_t ifindex, vrf_id_t vrf_id)
718e3744 244{
5b8524f5 245 struct vrf *vrf;
ff880b78 246 struct interface if_tmp;
f4e14fdb 247
5b8524f5
RW
248 vrf = vrf_lookup_by_id(vrf_id);
249 if (!vrf)
250 return NULL;
251
ff880b78
RW
252 if_tmp.ifindex = ifindex;
253 return RB_FIND(if_index_head, &vrf->ifaces_by_index, &if_tmp);
718e3744 254}
255
d62a17ae 256const char *ifindex2ifname(ifindex_t ifindex, vrf_id_t vrf_id)
718e3744 257{
d62a17ae 258 struct interface *ifp;
718e3744 259
d62a17ae 260 return ((ifp = if_lookup_by_index(ifindex, vrf_id)) != NULL)
261 ? ifp->name
262 : "unknown";
d2fc8896 263}
264
d62a17ae 265ifindex_t ifname2ifindex(const char *name, vrf_id_t vrf_id)
d2fc8896 266{
d62a17ae 267 struct interface *ifp;
d2fc8896 268
d62a17ae 269 return ((ifp = if_lookup_by_name(name, vrf_id)) != NULL)
270 ? ifp->ifindex
271 : IFINDEX_INTERNAL;
718e3744 272}
273
274/* Interface existance check by interface name. */
d62a17ae 275struct interface *if_lookup_by_name(const char *name, vrf_id_t vrf_id)
718e3744 276{
f4e14fdb
RW
277 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
278 struct interface if_tmp;
d62a17ae 279
5b8524f5
RW
280 if (!vrf || !name
281 || strnlen(name, INTERFACE_NAMSIZ) == INTERFACE_NAMSIZ)
bcc24579 282 return NULL;
f8962871 283
f4e14fdb
RW
284 strlcpy(if_tmp.name, name, sizeof(if_tmp.name));
285 return RB_FIND(if_name_head, &vrf->ifaces_by_name, &if_tmp);
f8962871
DS
286}
287
bcc24579 288struct interface *if_lookup_by_name_all_vrf(const char *name)
a349198f 289{
bcc24579 290 struct vrf *vrf;
d62a17ae 291 struct interface *ifp;
a349198f 292
bcc24579 293 if (!name || strnlen(name, INTERFACE_NAMSIZ) == INTERFACE_NAMSIZ)
d62a17ae 294 return NULL;
a349198f 295
bcc24579
RW
296 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
297 ifp = if_lookup_by_name(name, vrf->vrf_id);
298 if (ifp)
d62a17ae 299 return ifp;
300 }
bcc24579 301
d62a17ae 302 return NULL;
a349198f 303}
304
718e3744 305/* Lookup interface by IPv4 address. */
d62a17ae 306struct interface *if_lookup_exact_address(void *src, int family,
307 vrf_id_t vrf_id)
718e3744 308{
f4e14fdb 309 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
d62a17ae 310 struct listnode *cnode;
311 struct interface *ifp;
312 struct prefix *p;
313 struct connected *c;
314
451fda4f 315 FOR_ALL_INTERFACES (vrf, ifp) {
d62a17ae 316 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, c)) {
317 p = c->address;
318
319 if (p && (p->family == family)) {
320 if (family == AF_INET) {
321 if (IPV4_ADDR_SAME(
322 &p->u.prefix4,
323 (struct in_addr *)src))
324 return ifp;
325 } else if (family == AF_INET6) {
326 if (IPV6_ADDR_SAME(
327 &p->u.prefix6,
328 (struct in6_addr *)src))
329 return ifp;
330 }
331 }
0aabccc0 332 }
718e3744 333 }
d62a17ae 334 return NULL;
718e3744 335}
336
337/* Lookup interface by IPv4 address. */
d62a17ae 338struct connected *if_lookup_address(void *matchaddr, int family,
339 vrf_id_t vrf_id)
718e3744 340{
f4e14fdb 341 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
d62a17ae 342 struct prefix addr;
343 int bestlen = 0;
344 struct listnode *cnode;
345 struct interface *ifp;
346 struct connected *c;
347 struct connected *match;
348
349 if (family == AF_INET) {
350 addr.family = AF_INET;
351 addr.u.prefix4 = *((struct in_addr *)matchaddr);
352 addr.prefixlen = IPV4_MAX_BITLEN;
353 } else if (family == AF_INET6) {
354 addr.family = AF_INET6;
355 addr.u.prefix6 = *((struct in6_addr *)matchaddr);
356 addr.prefixlen = IPV6_MAX_BITLEN;
357 }
718e3744 358
d62a17ae 359 match = NULL;
718e3744 360
451fda4f 361 FOR_ALL_INTERFACES (vrf, ifp) {
d62a17ae 362 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, c)) {
363 if (c->address && (c->address->family == AF_INET)
364 && prefix_match(CONNECTED_PREFIX(c), &addr)
365 && (c->address->prefixlen > bestlen)) {
366 bestlen = c->address->prefixlen;
367 match = c;
368 }
369 }
718e3744 370 }
d62a17ae 371 return match;
718e3744 372}
373
b81e97a8 374/* Lookup interface by prefix */
d62a17ae 375struct interface *if_lookup_prefix(struct prefix *prefix, vrf_id_t vrf_id)
b81e97a8 376{
f4e14fdb 377 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
d62a17ae 378 struct listnode *cnode;
379 struct interface *ifp;
380 struct connected *c;
381
451fda4f 382 FOR_ALL_INTERFACES (vrf, ifp) {
d62a17ae 383 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, c)) {
384 if (prefix_cmp(c->address, prefix) == 0) {
385 return ifp;
386 }
387 }
388 }
389 return NULL;
b81e97a8
DD
390}
391
718e3744 392/* Get interface by name if given name interface doesn't exist create
393 one. */
8f90d89b 394struct interface *if_get_by_name(const char *name, vrf_id_t vrf_id)
718e3744 395{
8f90d89b 396 struct interface *ifp;
718e3744 397
8f90d89b 398 switch (vrf_get_backend()) {
b0b97a7f 399 case VRF_BACKEND_UNKNOWN:
8f90d89b 400 case VRF_BACKEND_NETNS:
ee2f2c23
TC
401 ifp = if_lookup_by_name(name, vrf_id);
402 if (ifp)
403 return ifp;
e9e9b115 404 return if_create(name, vrf_id);
8f90d89b
RW
405 case VRF_BACKEND_VRF_LITE:
406 ifp = if_lookup_by_name_all_vrf(name);
407 if (ifp) {
408 if (ifp->vrf_id == vrf_id)
379eb245 409 return ifp;
8f90d89b
RW
410 /* If it came from the kernel or by way of zclient,
411 * believe it and update the ifp accordingly.
412 */
413 if_update_to_new_vrf(ifp, vrf_id);
414 return ifp;
ee2f2c23 415 }
8f90d89b 416 return if_create(name, vrf_id);
85f9da7f 417 }
8f90d89b
RW
418
419 return NULL;
8736158a
FL
420}
421
ff880b78
RW
422void if_set_index(struct interface *ifp, ifindex_t ifindex)
423{
5b8524f5
RW
424 struct vrf *vrf;
425
426 vrf = vrf_lookup_by_id(ifp->vrf_id);
427 assert(vrf);
ff880b78
RW
428
429 if (ifp->ifindex == ifindex)
430 return;
431
432 if (ifp->ifindex != IFINDEX_INTERNAL)
433 IFINDEX_RB_REMOVE(vrf, ifp)
434
435 ifp->ifindex = ifindex;
436
437 if (ifp->ifindex != IFINDEX_INTERNAL)
438 IFINDEX_RB_INSERT(vrf, ifp)
439}
440
718e3744 441/* Does interface up ? */
6339042c 442int if_is_up(const struct interface *ifp)
718e3744 443{
d62a17ae 444 return ifp->flags & IFF_UP;
718e3744 445}
446
2e3b2e47 447/* Is interface running? */
6339042c 448int if_is_running(const struct interface *ifp)
2e3b2e47 449{
d62a17ae 450 return ifp->flags & IFF_RUNNING;
2e3b2e47 451}
452
453/* Is the interface operative, eg. either UP & RUNNING
244c1cdc
DS
454 or UP & !ZEBRA_INTERFACE_LINK_DETECTION and
455 if ptm checking is enabled, then ptm check has passed */
6339042c 456int if_is_operative(const struct interface *ifp)
2e3b2e47 457{
d62a17ae 458 return ((ifp->flags & IFF_UP)
459 && (((ifp->flags & IFF_RUNNING)
460 && (ifp->ptm_status || !ifp->ptm_enable))
461 || !CHECK_FLAG(ifp->status,
462 ZEBRA_INTERFACE_LINKDETECTION)));
244c1cdc
DS
463}
464
465/* Is the interface operative, eg. either UP & RUNNING
466 or UP & !ZEBRA_INTERFACE_LINK_DETECTION, without PTM check */
6339042c 467int if_is_no_ptm_operative(const struct interface *ifp)
244c1cdc 468{
d62a17ae 469 return ((ifp->flags & IFF_UP)
470 && ((ifp->flags & IFF_RUNNING)
471 || !CHECK_FLAG(ifp->status,
472 ZEBRA_INTERFACE_LINKDETECTION)));
2e3b2e47 473}
474
718e3744 475/* Is this loopback interface ? */
6339042c 476int if_is_loopback(const struct interface *ifp)
718e3744 477{
d62a17ae 478 /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M
479 * but Y on platform N?
480 */
481 return (ifp->flags & (IFF_LOOPBACK | IFF_NOXMIT | IFF_VIRTUAL));
718e3744 482}
483
0c74bbe0 484/* Check interface is VRF */
6339042c 485int if_is_vrf(const struct interface *ifp)
0c74bbe0
CS
486{
487 return CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK);
488}
489
6339042c 490bool if_is_loopback_or_vrf(const struct interface *ifp)
fec4ca19
DS
491{
492 if (if_is_loopback(ifp) || if_is_vrf(ifp))
493 return true;
494
495 return false;
496}
497
718e3744 498/* Does this interface support broadcast ? */
6339042c 499int if_is_broadcast(const struct interface *ifp)
718e3744 500{
d62a17ae 501 return ifp->flags & IFF_BROADCAST;
718e3744 502}
503
504/* Does this interface support broadcast ? */
6339042c 505int if_is_pointopoint(const struct interface *ifp)
718e3744 506{
d62a17ae 507 return ifp->flags & IFF_POINTOPOINT;
718e3744 508}
509
510/* Does this interface support multicast ? */
6339042c 511int if_is_multicast(const struct interface *ifp)
718e3744 512{
d62a17ae 513 return ifp->flags & IFF_MULTICAST;
718e3744 514}
515
516/* Printout flag information into log */
d62a17ae 517const char *if_flag_dump(unsigned long flag)
718e3744 518{
d62a17ae 519 int separator = 0;
520 static char logbuf[BUFSIZ];
521
522#define IFF_OUT_LOG(X, STR) \
523 if (flag & (X)) { \
524 if (separator) \
3393df5c 525 strlcat(logbuf, ",", sizeof(logbuf)); \
d62a17ae 526 else \
527 separator = 1; \
3393df5c 528 strlcat(logbuf, STR, sizeof(logbuf)); \
d62a17ae 529 }
718e3744 530
d62a17ae 531 strlcpy(logbuf, "<", BUFSIZ);
532 IFF_OUT_LOG(IFF_UP, "UP");
533 IFF_OUT_LOG(IFF_BROADCAST, "BROADCAST");
534 IFF_OUT_LOG(IFF_DEBUG, "DEBUG");
535 IFF_OUT_LOG(IFF_LOOPBACK, "LOOPBACK");
536 IFF_OUT_LOG(IFF_POINTOPOINT, "POINTOPOINT");
537 IFF_OUT_LOG(IFF_NOTRAILERS, "NOTRAILERS");
538 IFF_OUT_LOG(IFF_RUNNING, "RUNNING");
539 IFF_OUT_LOG(IFF_NOARP, "NOARP");
540 IFF_OUT_LOG(IFF_PROMISC, "PROMISC");
541 IFF_OUT_LOG(IFF_ALLMULTI, "ALLMULTI");
542 IFF_OUT_LOG(IFF_OACTIVE, "OACTIVE");
543 IFF_OUT_LOG(IFF_SIMPLEX, "SIMPLEX");
544 IFF_OUT_LOG(IFF_LINK0, "LINK0");
545 IFF_OUT_LOG(IFF_LINK1, "LINK1");
546 IFF_OUT_LOG(IFF_LINK2, "LINK2");
547 IFF_OUT_LOG(IFF_MULTICAST, "MULTICAST");
548 IFF_OUT_LOG(IFF_NOXMIT, "NOXMIT");
549 IFF_OUT_LOG(IFF_NORTEXCH, "NORTEXCH");
550 IFF_OUT_LOG(IFF_VIRTUAL, "VIRTUAL");
551 IFF_OUT_LOG(IFF_IPV4, "IPv4");
552 IFF_OUT_LOG(IFF_IPV6, "IPv6");
553
3393df5c 554 strlcat(logbuf, ">", sizeof(logbuf));
d62a17ae 555
556 return logbuf;
630c97ce 557#undef IFF_OUT_LOG
718e3744 558}
559
560/* For debugging */
d62a17ae 561static void if_dump(const struct interface *ifp)
718e3744 562{
d62a17ae 563 struct listnode *node;
564 struct connected *c __attribute__((unused));
565
566 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, c))
567 zlog_info(
568 "Interface %s vrf %u index %d metric %d mtu %d "
569 "mtu6 %d %s",
570 ifp->name, ifp->vrf_id, ifp->ifindex, ifp->metric,
571 ifp->mtu, ifp->mtu6, if_flag_dump(ifp->flags));
718e3744 572}
573
574/* Interface printing for all interface. */
d62a17ae 575void if_dump_all(void)
718e3744 576{
d62a17ae 577 struct vrf *vrf;
f4e14fdb 578 void *ifp;
d62a17ae 579
a2addae8 580 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id)
451fda4f 581 FOR_ALL_INTERFACES (vrf, ifp)
f4e14fdb 582 if_dump(ifp);
718e3744 583}
584
98954844
PJ
585#ifdef SUNOS_5
586/* Need to handle upgrade from SUNWzebra to Quagga. SUNWzebra created
587 * a seperate struct interface for each logical interface, so config
588 * file may be full of 'interface fooX:Y'. Solaris however does not
589 * expose logical interfaces via PF_ROUTE, so trying to track logical
590 * interfaces can be fruitless, for that reason Quagga only tracks
591 * the primary IP interface.
592 *
593 * We try accomodate SUNWzebra by:
594 * - looking up the interface name, to see whether it exists, if so
595 * its useable
596 * - for protocol daemons, this could only because zebra told us of
597 * the interface
598 * - for zebra, only because it learnt from kernel
599 * - if not:
600 * - search the name to see if it contains a sub-ipif / logical interface
601 * seperator, the ':' char. If it does:
602 * - text up to that char must be the primary name - get that name.
603 * if not:
604 * - no idea, just get the name in its entirety.
605 */
ae9eebca 606static struct interface *if_sunwzebra_get(const char *name, vrf_id_t vrf_id)
98954844 607{
d62a17ae 608 struct interface *ifp;
bcc24579 609 char *cp;
d62a17ae 610
bcc24579 611 if ((ifp = if_lookup_by_name(name, vrf_id)) != NULL)
d62a17ae 612 return ifp;
613
614 /* hunt the primary interface name... */
bcc24579
RW
615 cp = strchr(name, ':');
616 if (cp)
617 *cp = '\0';
d62a17ae 618
8f90d89b 619 return if_get_by_name(name, vrf_id);
98954844
PJ
620}
621#endif /* SUNOS_5 */
6b0655a2 622
d7d73ffc 623#if 0
718e3744 624/* For debug purpose. */
625DEFUN (show_address,
626 show_address_cmd,
199d90a1 627 "show address [vrf NAME]",
718e3744 628 SHOW_STR
fcbee690
QY
629 "address\n"
630 VRF_CMD_HELP_STR)
718e3744 631{
abddf075 632 int idx_vrf = 3;
52dc7ee6 633 struct listnode *node;
718e3744 634 struct interface *ifp;
635 struct connected *ifc;
636 struct prefix *p;
8736158a 637 vrf_id_t vrf_id = VRF_DEFAULT;
718e3744 638
fcbee690 639 if (argc > 2)
58749582 640 VRF_GET_ID (vrf_id, argv[idx_vrf]->arg);
8736158a 641
451fda4f 642 FOR_ALL_INTERFACES (vrf, ifp)
718e3744 643 {
f4e14fdb 644 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, ifc))
718e3744 645 {
718e3744 646 p = ifc->address;
647
648 if (p->family == AF_INET)
55f70b67 649 vty_out (vty, "%s/%d\n", inet_ntoa (p->u.prefix4), p->prefixlen);
718e3744 650 }
651 }
652 return CMD_SUCCESS;
653}
654
8736158a
FL
655DEFUN (show_address_vrf_all,
656 show_address_vrf_all_cmd,
9ccf14f7 657 "show address vrf all",
8736158a
FL
658 SHOW_STR
659 "address\n"
660 VRF_ALL_CMD_HELP_STR)
661{
1a1a7065 662 struct vrf *vrf;
8736158a 663 struct listnode *node;
8736158a
FL
664 struct interface *ifp;
665 struct connected *ifc;
666 struct prefix *p;
8736158a 667
a62c4901 668 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name)
8736158a 669 {
f4e14fdb 670 if (RB_EMPTY (if_name_head, &vrf->ifaces_by_name))
8736158a
FL
671 continue;
672
55f70b67 673 vty_out (vty, "\nVRF %u\n\n", vrf->vrf_id);
8736158a 674
451fda4f 675 FOR_ALL_INTERFACES (vrf, ifp)
8736158a 676 {
f4e14fdb 677 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, ifc))
8736158a
FL
678 {
679 p = ifc->address;
680
681 if (p->family == AF_INET)
55f70b67 682 vty_out (vty, "%s/%d\n", inet_ntoa (p->u.prefix4), p->prefixlen);
8736158a
FL
683 }
684 }
685 }
686 return CMD_SUCCESS;
687}
d7d73ffc 688#endif
8736158a 689
718e3744 690/* Allocate connected structure. */
d62a17ae 691struct connected *connected_new(void)
718e3744 692{
d62a17ae 693 return XCALLOC(MTYPE_CONNECTED, sizeof(struct connected));
718e3744 694}
695
a80beece 696/* Allocate nbr connected structure. */
d62a17ae 697struct nbr_connected *nbr_connected_new(void)
a80beece 698{
d62a17ae 699 return XCALLOC(MTYPE_NBR_CONNECTED, sizeof(struct nbr_connected));
a80beece
DS
700}
701
718e3744 702/* Free connected structure. */
d62a17ae 703void connected_free(struct connected *connected)
718e3744 704{
d62a17ae 705 if (connected->address)
706 prefix_free(connected->address);
718e3744 707
d62a17ae 708 if (connected->destination)
709 prefix_free(connected->destination);
718e3744 710
0a22ddfb 711 XFREE(MTYPE_CONNECTED_LABEL, connected->label);
718e3744 712
d62a17ae 713 XFREE(MTYPE_CONNECTED, connected);
718e3744 714}
715
a80beece 716/* Free nbr connected structure. */
d62a17ae 717void nbr_connected_free(struct nbr_connected *connected)
a80beece 718{
d62a17ae 719 if (connected->address)
720 prefix_free(connected->address);
a80beece 721
d62a17ae 722 XFREE(MTYPE_NBR_CONNECTED, connected);
a80beece
DS
723}
724
725/* If same interface nbr address already exists... */
d62a17ae 726struct nbr_connected *nbr_connected_check(struct interface *ifp,
727 struct prefix *p)
a80beece 728{
d62a17ae 729 struct nbr_connected *ifc;
730 struct listnode *node;
a80beece 731
d62a17ae 732 for (ALL_LIST_ELEMENTS_RO(ifp->nbr_connected, node, ifc))
733 if (prefix_same(ifc->address, p))
734 return ifc;
a80beece 735
d62a17ae 736 return NULL;
a80beece
DS
737}
738
718e3744 739/* Print if_addr structure. */
d62a17ae 740static void __attribute__((unused))
741connected_log(struct connected *connected, char *str)
718e3744 742{
d62a17ae 743 struct prefix *p;
744 struct interface *ifp;
745 char logbuf[BUFSIZ];
746 char buf[BUFSIZ];
747
748 ifp = connected->ifp;
749 p = connected->address;
750
751 snprintf(logbuf, BUFSIZ, "%s interface %s vrf %u %s %s/%d ", str,
752 ifp->name, ifp->vrf_id, prefix_family_str(p),
753 inet_ntop(p->family, &p->u.prefix, buf, BUFSIZ), p->prefixlen);
754
755 p = connected->destination;
756 if (p) {
757 strncat(logbuf, inet_ntop(p->family, &p->u.prefix, buf, BUFSIZ),
758 BUFSIZ - strlen(logbuf));
759 }
760 zlog_info("%s", logbuf);
718e3744 761}
762
a80beece 763/* Print if_addr structure. */
d62a17ae 764static void __attribute__((unused))
765nbr_connected_log(struct nbr_connected *connected, char *str)
a80beece 766{
d62a17ae 767 struct prefix *p;
768 struct interface *ifp;
769 char logbuf[BUFSIZ];
770 char buf[BUFSIZ];
a80beece 771
d62a17ae 772 ifp = connected->ifp;
773 p = connected->address;
a80beece 774
d62a17ae 775 snprintf(logbuf, BUFSIZ, "%s interface %s %s %s/%d ", str, ifp->name,
776 prefix_family_str(p),
777 inet_ntop(p->family, &p->u.prefix, buf, BUFSIZ), p->prefixlen);
a80beece 778
d62a17ae 779 zlog_info("%s", logbuf);
a80beece
DS
780}
781
718e3744 782/* If two connected address has same prefix return 1. */
d62a17ae 783static int connected_same_prefix(struct prefix *p1, struct prefix *p2)
718e3744 784{
d62a17ae 785 if (p1->family == p2->family) {
786 if (p1->family == AF_INET
787 && IPV4_ADDR_SAME(&p1->u.prefix4, &p2->u.prefix4))
788 return 1;
789 if (p1->family == AF_INET6
790 && IPV6_ADDR_SAME(&p1->u.prefix6, &p2->u.prefix6))
791 return 1;
792 }
793 return 0;
718e3744 794}
795
d62a17ae 796struct connected *connected_lookup_prefix_exact(struct interface *ifp,
797 struct prefix *p)
38485402 798{
d62a17ae 799 struct listnode *node;
800 struct listnode *next;
801 struct connected *ifc;
38485402 802
d62a17ae 803 for (node = listhead(ifp->connected); node; node = next) {
804 ifc = listgetdata(node);
805 next = node->next;
38485402 806
d62a17ae 807 if (connected_same_prefix(ifc->address, p))
808 return ifc;
809 }
810 return NULL;
38485402
DS
811}
812
d62a17ae 813struct connected *connected_delete_by_prefix(struct interface *ifp,
814 struct prefix *p)
718e3744 815{
d62a17ae 816 struct listnode *node;
817 struct listnode *next;
818 struct connected *ifc;
819
820 /* In case of same prefix come, replace it with new one. */
821 for (node = listhead(ifp->connected); node; node = next) {
822 ifc = listgetdata(node);
823 next = node->next;
824
825 if (connected_same_prefix(ifc->address, p)) {
826 listnode_delete(ifp->connected, ifc);
827 return ifc;
828 }
718e3744 829 }
d62a17ae 830 return NULL;
718e3744 831}
832
bd40c341 833/* Find the address on our side that will be used when packets
727d104b 834 are sent to dst. */
d62a17ae 835struct connected *connected_lookup_prefix(struct interface *ifp,
836 struct prefix *addr)
727d104b 837{
d62a17ae 838 struct listnode *cnode;
839 struct connected *c;
840 struct connected *match;
841
842 match = NULL;
843
844 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, c)) {
845 if (c->address && (c->address->family == addr->family)
846 && prefix_match(CONNECTED_PREFIX(c), addr)
847 && (!match
848 || (c->address->prefixlen > match->address->prefixlen)))
849 match = c;
850 }
851 return match;
727d104b 852}
853
d62a17ae 854struct connected *connected_add_by_prefix(struct interface *ifp,
855 struct prefix *p,
856 struct prefix *destination)
4a7aac1b 857{
d62a17ae 858 struct connected *ifc;
4a7aac1b 859
d62a17ae 860 /* Allocate new connected address. */
861 ifc = connected_new();
862 ifc->ifp = ifp;
4a7aac1b 863
d62a17ae 864 /* Fetch interface address */
865 ifc->address = prefix_new();
866 memcpy(ifc->address, p, sizeof(struct prefix));
4a7aac1b 867
d62a17ae 868 /* Fetch dest address */
869 if (destination) {
870 ifc->destination = prefix_new();
871 memcpy(ifc->destination, destination, sizeof(struct prefix));
872 }
4a7aac1b 873
d62a17ae 874 /* Add connected address to the interface. */
875 listnode_add(ifp->connected, ifc);
876 return ifc;
4a7aac1b 877}
878
d62a17ae 879#if 0 /* this route_table of struct connected's is unused \
880 * however, it would be good to use a route_table rather than \
881 * a list.. \
882 */
718e3744 883/* Interface looking up by interface's address. */
718e3744 884/* Interface's IPv4 address reverse lookup table. */
885struct route_table *ifaddr_ipv4_table;
886/* struct route_table *ifaddr_ipv6_table; */
887
8cc4198f 888static void
718e3744 889ifaddr_ipv4_add (struct in_addr *ifaddr, struct interface *ifp)
890{
891 struct route_node *rn;
892 struct prefix_ipv4 p;
893
894 p.family = AF_INET;
895 p.prefixlen = IPV4_MAX_PREFIXLEN;
896 p.prefix = *ifaddr;
897
898 rn = route_node_get (ifaddr_ipv4_table, (struct prefix *) &p);
899 if (rn)
900 {
901 route_unlock_node (rn);
902 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
903 inet_ntoa (*ifaddr));
904 return;
905 }
906 rn->info = ifp;
907}
908
8cc4198f 909static void
718e3744 910ifaddr_ipv4_delete (struct in_addr *ifaddr, struct interface *ifp)
911{
912 struct route_node *rn;
913 struct prefix_ipv4 p;
914
915 p.family = AF_INET;
916 p.prefixlen = IPV4_MAX_PREFIXLEN;
917 p.prefix = *ifaddr;
918
919 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
920 if (! rn)
921 {
922 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
923 inet_ntoa (*ifaddr));
924 return;
925 }
926 rn->info = NULL;
927 route_unlock_node (rn);
928 route_unlock_node (rn);
929}
930
931/* Lookup interface by interface's IP address or interface index. */
8cc4198f 932static struct interface *
b892f1dd 933ifaddr_ipv4_lookup (struct in_addr *addr, ifindex_t ifindex)
718e3744 934{
935 struct prefix_ipv4 p;
936 struct route_node *rn;
937 struct interface *ifp;
718e3744 938
939 if (addr)
940 {
941 p.family = AF_INET;
942 p.prefixlen = IPV4_MAX_PREFIXLEN;
943 p.prefix = *addr;
944
945 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
946 if (! rn)
947 return NULL;
55cd0f61 948
718e3744 949 ifp = rn->info;
950 route_unlock_node (rn);
951 return ifp;
952 }
953 else
7e2b7603 954 return if_lookup_by_index(ifindex, VRF_DEFAULT);
718e3744 955}
8cc4198f 956#endif /* ifaddr_ipv4_table */
718e3744 957
f4e14fdb 958void if_terminate(struct vrf *vrf)
4bd045d5 959{
f4e14fdb 960 struct interface *ifp;
4bd045d5 961
55cd0f61
DS
962 while (!RB_EMPTY(if_name_head, &vrf->ifaces_by_name)) {
963 ifp = RB_ROOT(if_name_head, &vrf->ifaces_by_name);
964
d62a17ae 965 if (ifp->node) {
966 ifp->node->info = NULL;
967 route_unlock_node(ifp->node);
968 }
d62a17ae 969 if_delete(ifp);
970 }
4bd045d5 971}
8ccc7e80 972
d62a17ae 973const char *if_link_type_str(enum zebra_link_type llt)
8ccc7e80 974{
d62a17ae 975 switch (llt) {
8ccc7e80 976#define llts(T,S) case (T): return (S)
d62a17ae 977 llts(ZEBRA_LLT_UNKNOWN, "Unknown");
978 llts(ZEBRA_LLT_ETHER, "Ethernet");
979 llts(ZEBRA_LLT_EETHER, "Experimental Ethernet");
980 llts(ZEBRA_LLT_AX25, "AX.25 Level 2");
981 llts(ZEBRA_LLT_PRONET, "PROnet token ring");
982 llts(ZEBRA_LLT_IEEE802, "IEEE 802.2 Ethernet/TR/TB");
983 llts(ZEBRA_LLT_ARCNET, "ARCnet");
984 llts(ZEBRA_LLT_APPLETLK, "AppleTalk");
985 llts(ZEBRA_LLT_DLCI, "Frame Relay DLCI");
986 llts(ZEBRA_LLT_ATM, "ATM");
987 llts(ZEBRA_LLT_METRICOM, "Metricom STRIP");
988 llts(ZEBRA_LLT_IEEE1394, "IEEE 1394 IPv4");
989 llts(ZEBRA_LLT_EUI64, "EUI-64");
990 llts(ZEBRA_LLT_INFINIBAND, "InfiniBand");
991 llts(ZEBRA_LLT_SLIP, "SLIP");
992 llts(ZEBRA_LLT_CSLIP, "Compressed SLIP");
993 llts(ZEBRA_LLT_SLIP6, "SLIPv6");
994 llts(ZEBRA_LLT_CSLIP6, "Compressed SLIPv6");
995 llts(ZEBRA_LLT_ROSE, "ROSE packet radio");
996 llts(ZEBRA_LLT_X25, "CCITT X.25");
997 llts(ZEBRA_LLT_PPP, "PPP");
998 llts(ZEBRA_LLT_CHDLC, "Cisco HDLC");
999 llts(ZEBRA_LLT_RAWHDLC, "Raw HDLC");
1000 llts(ZEBRA_LLT_LAPB, "LAPB");
1001 llts(ZEBRA_LLT_IPIP, "IPIP Tunnel");
1002 llts(ZEBRA_LLT_IPIP6, "IPIP6 Tunnel");
1003 llts(ZEBRA_LLT_FRAD, "FRAD");
1004 llts(ZEBRA_LLT_SKIP, "SKIP vif");
1005 llts(ZEBRA_LLT_LOOPBACK, "Loopback");
1006 llts(ZEBRA_LLT_LOCALTLK, "Localtalk");
1007 llts(ZEBRA_LLT_FDDI, "FDDI");
1008 llts(ZEBRA_LLT_SIT, "IPv6-in-IPv4 SIT");
1009 llts(ZEBRA_LLT_IPDDP, "IP-in-DDP tunnel");
1010 llts(ZEBRA_LLT_IPGRE, "GRE over IP");
1011 llts(ZEBRA_LLT_PIMREG, "PIMSM registration");
1012 llts(ZEBRA_LLT_HIPPI, "HiPPI");
1013 llts(ZEBRA_LLT_IRDA, "IrDA");
1014 llts(ZEBRA_LLT_FCPP, "Fibre-Channel PtP");
1015 llts(ZEBRA_LLT_FCAL, "Fibre-Channel Arbitrated Loop");
1016 llts(ZEBRA_LLT_FCPL, "Fibre-Channel Public Loop");
1017 llts(ZEBRA_LLT_FCFABRIC, "Fibre-Channel Fabric");
1018 llts(ZEBRA_LLT_IEEE802_TR, "IEEE 802.2 Token Ring");
1019 llts(ZEBRA_LLT_IEEE80211, "IEEE 802.11");
1020 llts(ZEBRA_LLT_IEEE80211_RADIOTAP, "IEEE 802.11 Radiotap");
1021 llts(ZEBRA_LLT_IEEE802154, "IEEE 802.15.4");
1022 llts(ZEBRA_LLT_IEEE802154_PHY, "IEEE 802.15.4 Phy");
1023 default:
450971aa 1024 flog_err(EC_LIB_DEVELOPMENT, "Unknown value %d", llt);
d62a17ae 1025 return "Unknown type!";
8ccc7e80 1026#undef llts
d62a17ae 1027 }
1028 return NULL;
8ccc7e80 1029}
16f1b9ee 1030
d62a17ae 1031struct if_link_params *if_link_params_get(struct interface *ifp)
16f1b9ee 1032{
d62a17ae 1033 int i;
16f1b9ee 1034
d62a17ae 1035 if (ifp->link_params != NULL)
1036 return ifp->link_params;
16f1b9ee 1037
d62a17ae 1038 struct if_link_params *iflp =
1039 XCALLOC(MTYPE_IF_LINK_PARAMS, sizeof(struct if_link_params));
1040 if (iflp == NULL)
1041 return NULL;
16f1b9ee 1042
d62a17ae 1043 /* Set TE metric equal to standard metric */
1044 iflp->te_metric = ifp->metric;
16f1b9ee 1045
d62a17ae 1046 /* Compute default bandwidth based on interface */
1047 iflp->default_bw =
1048 ((ifp->bandwidth ? ifp->bandwidth : DEFAULT_BANDWIDTH)
1049 * TE_KILO_BIT / TE_BYTE);
16f1b9ee 1050
d62a17ae 1051 /* Set Max, Reservable and Unreserved Bandwidth */
1052 iflp->max_bw = iflp->default_bw;
1053 iflp->max_rsv_bw = iflp->default_bw;
1054 for (i = 0; i < MAX_CLASS_TYPE; i++)
1055 iflp->unrsv_bw[i] = iflp->default_bw;
16f1b9ee 1056
d62a17ae 1057 /* Update Link parameters status */
1058 iflp->lp_status =
1059 LP_TE_METRIC | LP_MAX_BW | LP_MAX_RSV_BW | LP_UNRSV_BW;
16f1b9ee 1060
d62a17ae 1061 /* Finally attach newly created Link Parameters */
1062 ifp->link_params = iflp;
16f1b9ee 1063
d62a17ae 1064 return iflp;
16f1b9ee
OD
1065}
1066
d62a17ae 1067void if_link_params_free(struct interface *ifp)
16f1b9ee 1068{
d62a17ae 1069 if (ifp->link_params == NULL)
1070 return;
1071 XFREE(MTYPE_IF_LINK_PARAMS, ifp->link_params);
1072 ifp->link_params = NULL;
16f1b9ee 1073}
a4bed468 1074
8f90d89b
RW
1075/* ----------- CLI commands ----------- */
1076
1077/*
1078 * XPath: /frr-interface:lib/interface
1079 */
1080DEFPY_NOSH (interface,
1081 interface_cmd,
1082 "interface IFNAME [vrf NAME$vrfname]",
1083 "Select an interface to configure\n"
1084 "Interface's name\n"
1085 VRF_CMD_HELP_STR)
1086{
1087 char xpath_list[XPATH_MAXLEN];
8f90d89b
RW
1088 vrf_id_t vrf_id;
1089 struct interface *ifp;
1090 int ret;
1091
1092 if (!vrfname)
1093 vrfname = VRF_DEFAULT_NAME;
1094
1095 /*
1096 * This command requires special handling to maintain backward
1097 * compatibility. If a VRF name is not specified, it means we're willing
1098 * to accept any interface with the given name on any VRF. If no
1099 * interface is found, then a new one should be created on the default
1100 * VRF.
1101 */
1102 VRF_GET_ID(vrf_id, vrfname, false);
1103 ifp = if_lookup_by_name_all_vrf(ifname);
1104 if (ifp && ifp->vrf_id != vrf_id) {
1105 struct vrf *vrf;
1106
1107 /*
1108 * Special case 1: a VRF name was specified, but the found
1109 * interface is associated to different VRF. Reject the command.
1110 */
1111 if (vrf_id != VRF_DEFAULT) {
1112 vty_out(vty, "%% interface %s not in %s vrf\n", ifname,
1113 vrfname);
1114 return CMD_WARNING_CONFIG_FAILED;
1115 }
1116
1117 /*
1118 * Special case 2: a VRF name was *not* specified, and the found
1119 * interface is associated to a VRF other than the default one.
1120 * Update vrf_id and vrfname to account for that.
1121 */
1122 vrf = vrf_lookup_by_id(ifp->vrf_id);
1123 assert(vrf);
1124 vrf_id = ifp->vrf_id;
1125 vrfname = vrf->name;
1126 }
1127
1128 snprintf(xpath_list, sizeof(xpath_list),
1129 "/frr-interface:lib/interface[name='%s'][vrf='%s']", ifname,
1130 vrfname);
1131
a6233bfc
RW
1132 nb_cli_enqueue_change(vty, ".", NB_OP_CREATE, NULL);
1133 ret = nb_cli_apply_changes(vty, xpath_list);
8f90d89b
RW
1134 if (ret == CMD_SUCCESS) {
1135 VTY_PUSH_XPATH(INTERFACE_NODE, xpath_list);
1136
1137 /*
1138 * For backward compatibility with old commands we still need
1139 * to use the qobj infrastructure. This can be removed once
1140 * all interface-level commands are converted to the new
1141 * northbound model.
1142 */
1143 ifp = if_lookup_by_name(ifname, vrf_id);
1144 if (ifp)
1145 VTY_PUSH_CONTEXT(INTERFACE_NODE, ifp);
1146 }
1147
1148 return ret;
1149}
1150
1151DEFPY (no_interface,
1152 no_interface_cmd,
1153 "no interface IFNAME [vrf NAME$vrfname]",
1154 NO_STR
1155 "Delete a pseudo interface's configuration\n"
1156 "Interface's name\n"
1157 VRF_CMD_HELP_STR)
1158{
8f90d89b
RW
1159 if (!vrfname)
1160 vrfname = VRF_DEFAULT_NAME;
1161
95ce849b 1162 nb_cli_enqueue_change(vty, ".", NB_OP_DESTROY, NULL);
8f90d89b 1163
a6233bfc
RW
1164 return nb_cli_apply_changes(
1165 vty, "/frr-interface:lib/interface[name='%s'][vrf='%s']",
1166 ifname, vrfname);
8f90d89b
RW
1167}
1168
1169static void cli_show_interface(struct vty *vty, struct lyd_node *dnode,
1170 bool show_defaults)
1171{
1172 const char *vrf;
1173
1174 vrf = yang_dnode_get_string(dnode, "./vrf");
1175
1176 vty_out(vty, "!\n");
1177 vty_out(vty, "interface %s", yang_dnode_get_string(dnode, "./name"));
1178 if (!strmatch(vrf, VRF_DEFAULT_NAME))
1179 vty_out(vty, " vrf %s", vrf);
1180 vty_out(vty, "\n");
1181}
1182
1183/*
1184 * XPath: /frr-interface:lib/interface/description
1185 */
1186DEFPY (interface_desc,
1187 interface_desc_cmd,
1188 "description LINE...",
1189 "Interface specific description\n"
1190 "Characters describing this interface\n")
1191{
8f90d89b
RW
1192 char *desc;
1193 int ret;
1194
1195 desc = argv_concat(argv, argc, 1);
a6233bfc
RW
1196 nb_cli_enqueue_change(vty, "./description", NB_OP_MODIFY, desc);
1197 ret = nb_cli_apply_changes(vty, NULL);
8f90d89b
RW
1198 XFREE(MTYPE_TMP, desc);
1199
1200 return ret;
1201}
1202
1203DEFPY (no_interface_desc,
1204 no_interface_desc_cmd,
1205 "no description",
1206 NO_STR
1207 "Interface specific description\n")
1208{
95ce849b 1209 nb_cli_enqueue_change(vty, "./description", NB_OP_DESTROY, NULL);
a6233bfc
RW
1210
1211 return nb_cli_apply_changes(vty, NULL);
8f90d89b
RW
1212}
1213
1214static void cli_show_interface_desc(struct vty *vty, struct lyd_node *dnode,
1215 bool show_defaults)
1216{
1217 vty_out(vty, " description %s\n", yang_dnode_get_string(dnode, NULL));
1218}
1219
1220/* Interface autocomplete. */
1221static void if_autocomplete(vector comps, struct cmd_token *token)
1222{
1223 struct interface *ifp;
1224 struct vrf *vrf;
1225
1226 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
1227 FOR_ALL_INTERFACES (vrf, ifp) {
1228 vector_set(comps, XSTRDUP(MTYPE_COMPLETION, ifp->name));
1229 }
1230 }
1231}
1232
1233static const struct cmd_variable_handler if_var_handlers[] = {
1234 {/* "interface NAME" */
1235 .varname = "interface",
1236 .completions = if_autocomplete},
1237 {.tokenname = "IFNAME", .completions = if_autocomplete},
1238 {.tokenname = "INTERFACE", .completions = if_autocomplete},
1239 {.completions = NULL}};
1240
1241void if_cmd_init(void)
1242{
1243 cmd_variable_handler_register(if_var_handlers);
1244
1245 install_element(CONFIG_NODE, &interface_cmd);
1246 install_element(CONFIG_NODE, &no_interface_cmd);
1247
1248 install_default(INTERFACE_NODE);
1249 install_element(INTERFACE_NODE, &interface_desc_cmd);
1250 install_element(INTERFACE_NODE, &no_interface_desc_cmd);
1251}
1252
a4bed468
RW
1253/* ------- Northbound callbacks ------- */
1254
1255/*
1256 * XPath: /frr-interface:lib/interface
1257 */
1258static int lib_interface_create(enum nb_event event,
1259 const struct lyd_node *dnode,
1260 union nb_resource *resource)
1261{
8f90d89b
RW
1262 const char *ifname;
1263 const char *vrfname;
1264 struct vrf *vrf;
1265 struct interface *ifp;
1266
1267 ifname = yang_dnode_get_string(dnode, "./name");
1268 vrfname = yang_dnode_get_string(dnode, "./vrf");
1269
1270 switch (event) {
1271 case NB_EV_VALIDATE:
1272 vrf = vrf_lookup_by_name(vrfname);
1273 if (!vrf) {
1274 zlog_warn("%s: VRF %s doesn't exist", __func__,
1275 vrfname);
1276 return NB_ERR_VALIDATION;
1277 }
1278 if (vrf->vrf_id == VRF_UNKNOWN) {
1279 zlog_warn("%s: VRF %s is not active", __func__,
1280 vrf->name);
1281 return NB_ERR_VALIDATION;
1282 }
72261ecd 1283
b0b97a7f
PG
1284 /* if VRF is netns or not yet known - init for instance
1285 * then assumption is that passed config is exact
1286 * then the user intent was not to use an other iface
1287 */
8f90d89b
RW
1288 if (vrf_get_backend() == VRF_BACKEND_VRF_LITE) {
1289 ifp = if_lookup_by_name_all_vrf(ifname);
1290 if (ifp && ifp->vrf_id != vrf->vrf_id) {
1291 zlog_warn(
1292 "%s: interface %s already exists in another VRF",
1293 __func__, ifp->name);
1294 return NB_ERR_VALIDATION;
1295 }
1296 }
1297 break;
1298 case NB_EV_PREPARE:
1299 case NB_EV_ABORT:
1300 break;
1301 case NB_EV_APPLY:
1302 vrf = vrf_lookup_by_name(vrfname);
1303 assert(vrf);
1304#ifdef SUNOS_5
1305 ifp = if_sunwzebra_get(ifname, vrf->vrf_id);
1306#else
1307 ifp = if_get_by_name(ifname, vrf->vrf_id);
1308#endif /* SUNOS_5 */
ccd43ada 1309 nb_running_set_entry(dnode, ifp);
8f90d89b
RW
1310 break;
1311 }
1312
a4bed468
RW
1313 return NB_OK;
1314}
1315
6a3fdeec
RW
1316static int lib_interface_destroy(enum nb_event event,
1317 const struct lyd_node *dnode)
a4bed468 1318{
8f90d89b
RW
1319 struct interface *ifp;
1320
8f90d89b
RW
1321
1322 switch (event) {
1323 case NB_EV_VALIDATE:
ccd43ada 1324 ifp = nb_running_get_entry(dnode, NULL, true);
8f90d89b
RW
1325 if (CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
1326 zlog_warn("%s: only inactive interfaces can be deleted",
1327 __func__);
1328 return NB_ERR_VALIDATION;
1329 }
1330 break;
1331 case NB_EV_PREPARE:
1332 case NB_EV_ABORT:
1333 break;
1334 case NB_EV_APPLY:
ccd43ada 1335 ifp = nb_running_unset_entry(dnode);
8f90d89b
RW
1336 if_delete(ifp);
1337 break;
1338 }
1339
a4bed468
RW
1340 return NB_OK;
1341}
1342
1343/*
1344 * XPath: /frr-interface:lib/interface/description
1345 */
1346static int lib_interface_description_modify(enum nb_event event,
1347 const struct lyd_node *dnode,
1348 union nb_resource *resource)
1349{
8f90d89b
RW
1350 struct interface *ifp;
1351 const char *description;
1352
1353 if (event != NB_EV_APPLY)
1354 return NB_OK;
1355
ccd43ada 1356 ifp = nb_running_get_entry(dnode, NULL, true);
0a22ddfb 1357 XFREE(MTYPE_TMP, ifp->desc);
8f90d89b
RW
1358 description = yang_dnode_get_string(dnode, NULL);
1359 ifp->desc = XSTRDUP(MTYPE_TMP, description);
1360
a4bed468
RW
1361 return NB_OK;
1362}
1363
6a3fdeec
RW
1364static int lib_interface_description_destroy(enum nb_event event,
1365 const struct lyd_node *dnode)
a4bed468 1366{
8f90d89b
RW
1367 struct interface *ifp;
1368
1369 if (event != NB_EV_APPLY)
1370 return NB_OK;
1371
ccd43ada 1372 ifp = nb_running_get_entry(dnode, NULL, true);
0a22ddfb 1373 XFREE(MTYPE_TMP, ifp->desc);
8f90d89b 1374
a4bed468
RW
1375 return NB_OK;
1376}
1377
1378/* clang-format off */
1379const struct frr_yang_module_info frr_interface_info = {
1380 .name = "frr-interface",
1381 .nodes = {
1382 {
1383 .xpath = "/frr-interface:lib/interface",
1384 .cbs.create = lib_interface_create,
6a3fdeec 1385 .cbs.destroy = lib_interface_destroy,
8f90d89b 1386 .cbs.cli_show = cli_show_interface,
a4bed468
RW
1387 },
1388 {
1389 .xpath = "/frr-interface:lib/interface/description",
1390 .cbs.modify = lib_interface_description_modify,
6a3fdeec 1391 .cbs.destroy = lib_interface_description_destroy,
8f90d89b 1392 .cbs.cli_show = cli_show_interface_desc,
a4bed468
RW
1393 },
1394 {
1395 .xpath = NULL,
1396 },
1397 }
1398};