]> git.proxmox.com Git - mirror_frr.git/blame - lib/if.c
Merge pull request #10398 from patrasar/pim_debug_fix
[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
bf8d3d6a
DL
42DEFINE_MTYPE_STATIC(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
f60a1188 48static void if_set_name(struct interface *ifp, const char *name);
47b474b5
DD
49static struct interface *if_lookup_by_ifindex(ifindex_t ifindex,
50 vrf_id_t vrf_id);
35367f03 51static struct interface *if_lookup_by_index_all_vrf(ifindex_t ifindex);
f4e14fdb 52static int if_cmp_func(const struct interface *, const struct interface *);
ff880b78
RW
53static int if_cmp_index_func(const struct interface *ifp1,
54 const struct interface *ifp2);
f4e14fdb 55RB_GENERATE(if_name_head, interface, name_entry, if_cmp_func);
ff880b78 56RB_GENERATE(if_index_head, interface, index_entry, if_cmp_index_func);
f4e14fdb 57
96244aca 58DEFINE_QOBJ_TYPE(interface);
e80e7cce 59
8451921b
DL
60DEFINE_HOOK(if_add, (struct interface * ifp), (ifp));
61DEFINE_KOOH(if_del, (struct interface * ifp), (ifp));
ce19a04a 62
1b3e9a21 63static struct interface_master{
138c5a74
DS
64 int (*create_hook)(struct interface *ifp);
65 int (*up_hook)(struct interface *ifp);
66 int (*down_hook)(struct interface *ifp);
67 int (*destroy_hook)(struct interface *ifp);
68} ifp_master = { 0, };
69
3a0391a9 70/* Compare interface names, returning an integer greater than, equal to, or
71 * less than 0, (following the strcmp convention), according to the
72 * relationship between ifp1 and ifp2. Interface names consist of an
73 * alphabetic prefix and a numeric suffix. The primary sort key is
74 * lexicographic by name, and then numeric by number. No number sorts
75 * before all numbers. Examples: de0 < de1, de100 < fxp0 < xl0, devpty <
76 * devpty0, de0 < del0
d62a17ae 77 */
36de6e0e 78int if_cmp_name_func(const char *p1, const char *p2)
106d2fd5 79{
d62a17ae 80 unsigned int l1, l2;
81 long int x1, x2;
82 int res;
83
84 while (*p1 && *p2) {
85 /* look up to any number */
86 l1 = strcspn(p1, "0123456789");
87 l2 = strcspn(p2, "0123456789");
88
89 /* name lengths are different -> compare names */
90 if (l1 != l2)
91 return (strcmp(p1, p2));
92
93 /* Note that this relies on all numbers being less than all
94 * letters, so
95 * that de0 < del0.
96 */
97 res = strncmp(p1, p2, l1);
98
99 /* names are different -> compare them */
100 if (res)
101 return res;
102
103 /* with identical name part, go to numeric part */
104 p1 += l1;
105 p2 += l1;
106
c9cbbb40
RW
107 if (!*p1 && !*p2)
108 return 0;
d62a17ae 109 if (!*p1)
110 return -1;
111 if (!*p2)
112 return 1;
113
36de6e0e
A
114 x1 = strtol(p1, (char **)&p1, 10);
115 x2 = strtol(p2, (char **)&p2, 10);
d62a17ae 116
117 /* let's compare numbers now */
118 if (x1 < x2)
119 return -1;
120 if (x1 > x2)
121 return 1;
122
123 /* numbers were equal, lets do it again..
124 (it happens with name like "eth123.456:789") */
125 }
126 if (*p1)
127 return 1;
128 if (*p2)
129 return -1;
130 return 0;
106d2fd5 131}
132
f4e14fdb
RW
133static int if_cmp_func(const struct interface *ifp1,
134 const struct interface *ifp2)
974fc9d2 135{
36de6e0e 136 return if_cmp_name_func(ifp1->name, ifp2->name);
974fc9d2
DS
137}
138
ff880b78
RW
139static int if_cmp_index_func(const struct interface *ifp1,
140 const struct interface *ifp2)
141{
e33d7b6a
QY
142 if (ifp1->ifindex == ifp2->ifindex)
143 return 0;
144 else if (ifp1->ifindex > ifp2->ifindex)
145 return 1;
146 else
147 return -1;
ff880b78
RW
148}
149
721c0857
DS
150static void ifp_connected_free(void *arg)
151{
152 struct connected *c = arg;
153
154 connected_free(&c);
155}
156
718e3744 157/* Create new interface structure. */
f60a1188 158static struct interface *if_new(struct vrf *vrf)
718e3744 159{
d62a17ae 160 struct interface *ifp;
d62a17ae 161
f60a1188
IR
162 assert(vrf);
163
d62a17ae 164 ifp = XCALLOC(MTYPE_IF, sizeof(struct interface));
ea7ec261 165
d5c65bf1
SW
166 ifp->ifindex = IFINDEX_INTERNAL;
167 ifp->name[0] = '\0';
ea7ec261 168
f60a1188 169 ifp->vrf = vrf;
ea7ec261 170
d62a17ae 171 ifp->connected = list_new();
721c0857 172 ifp->connected->del = ifp_connected_free;
d62a17ae 173
174 ifp->nbr_connected = list_new();
175 ifp->nbr_connected->del = (void (*)(void *))nbr_connected_free;
176
177 /* Enable Link-detection by default */
178 SET_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION);
179
180 QOBJ_REG(ifp, interface);
d62a17ae 181 return ifp;
718e3744 182}
183
ef7bd2a3
DS
184void if_new_via_zapi(struct interface *ifp)
185{
186 if (ifp_master.create_hook)
187 (*ifp_master.create_hook)(ifp);
188}
189
3c3c3252
DS
190void if_destroy_via_zapi(struct interface *ifp)
191{
192 if (ifp_master.destroy_hook)
193 (*ifp_master.destroy_hook)(ifp);
194
e7ff0253 195 ifp->oldifindex = ifp->ifindex;
3c3c3252 196 if_set_index(ifp, IFINDEX_INTERNAL);
e7ff0253 197
26f8f6fe 198 if (!ifp->configured)
f609709a 199 if_delete(&ifp);
3c3c3252
DS
200}
201
ddbf3e60
DS
202void if_up_via_zapi(struct interface *ifp)
203{
204 if (ifp_master.up_hook)
205 (*ifp_master.up_hook)(ifp);
206}
207
b0b69e59
DS
208void if_down_via_zapi(struct interface *ifp)
209{
210 if (ifp_master.down_hook)
211 (*ifp_master.down_hook)(ifp);
212}
213
f60a1188 214static struct interface *if_create_name(const char *name, struct vrf *vrf)
ea7ec261 215{
d5c65bf1
SW
216 struct interface *ifp;
217
f60a1188 218 ifp = if_new(vrf);
d5c65bf1
SW
219
220 if_set_name(ifp, name);
221
222 hook_call(if_add, ifp);
223 return ifp;
ea7ec261
DD
224}
225
a36898e7
DS
226/* Create new interface structure. */
227void if_update_to_new_vrf(struct interface *ifp, vrf_id_t vrf_id)
f93e3f69 228{
a36898e7 229 struct vrf *old_vrf, *vrf;
d62a17ae 230
231 /* remove interface from old master vrf list */
f60a1188 232 old_vrf = ifp->vrf;
9685b1e4 233
f60a1188
IR
234 if (ifp->name[0] != '\0')
235 IFNAME_RB_REMOVE(old_vrf, ifp);
236
237 if (ifp->ifindex != IFINDEX_INTERNAL)
238 IFINDEX_RB_REMOVE(old_vrf, ifp);
d62a17ae 239
096f7609 240 vrf = vrf_get(vrf_id, NULL);
f60a1188 241 ifp->vrf = vrf;
ff880b78 242
9685b1e4
SW
243 if (ifp->name[0] != '\0')
244 IFNAME_RB_INSERT(vrf, ifp);
245
ff880b78
RW
246 if (ifp->ifindex != IFINDEX_INTERNAL)
247 IFINDEX_RB_INSERT(vrf, ifp);
f93e3f69
DS
248}
249
8685be73 250
d2fc8896 251/* Delete interface structure. */
d62a17ae 252void if_delete_retain(struct interface *ifp)
718e3744 253{
996c9314 254 hook_call(if_del, ifp);
d62a17ae 255 QOBJ_UNREG(ifp);
e80e7cce 256
d62a17ae 257 /* Free connected address list */
258 list_delete_all_node(ifp->connected);
a80beece 259
d62a17ae 260 /* Free connected nbr address list */
261 list_delete_all_node(ifp->nbr_connected);
d2fc8896 262}
263
264/* Delete and free interface structure. */
f609709a 265void if_delete(struct interface **ifp)
d2fc8896 266{
f609709a 267 struct interface *ptr = *ifp;
f60a1188 268 struct vrf *vrf = ptr->vrf;
f4e14fdb 269
f609709a
DS
270 IFNAME_RB_REMOVE(vrf, ptr);
271 if (ptr->ifindex != IFINDEX_INTERNAL)
272 IFINDEX_RB_REMOVE(vrf, ptr);
d2fc8896 273
f609709a 274 if_delete_retain(ptr);
718e3744 275
f609709a
DS
276 list_delete(&ptr->connected);
277 list_delete(&ptr->nbr_connected);
2dd04c5d 278
f609709a 279 if_link_params_free(ptr);
16f1b9ee 280
f609709a 281 XFREE(MTYPE_TMP, ptr->desc);
c7974c0f 282
f609709a
DS
283 XFREE(MTYPE_IF, ptr);
284 *ifp = NULL;
718e3744 285}
286
47b474b5
DD
287/* Used only internally to check within VRF only */
288static struct interface *if_lookup_by_ifindex(ifindex_t ifindex,
289 vrf_id_t vrf_id)
718e3744 290{
5b8524f5 291 struct vrf *vrf;
ff880b78 292 struct interface if_tmp;
f4e14fdb 293
5b8524f5
RW
294 vrf = vrf_lookup_by_id(vrf_id);
295 if (!vrf)
296 return NULL;
297
ff880b78
RW
298 if_tmp.ifindex = ifindex;
299 return RB_FIND(if_index_head, &vrf->ifaces_by_index, &if_tmp);
718e3744 300}
301
214d8a60 302/* Interface existence check by index. */
47b474b5
DD
303struct interface *if_lookup_by_index(ifindex_t ifindex, vrf_id_t vrf_id)
304{
305 switch (vrf_get_backend()) {
306 case VRF_BACKEND_UNKNOWN:
307 case VRF_BACKEND_NETNS:
308 return(if_lookup_by_ifindex(ifindex, vrf_id));
309 case VRF_BACKEND_VRF_LITE:
310 return(if_lookup_by_index_all_vrf(ifindex));
311 }
312 return NULL;
313}
314
214d8a60 315/* Interface existence check by index. */
0760a74d
PR
316struct interface *if_vrf_lookup_by_index_next(ifindex_t ifindex,
317 vrf_id_t vrf_id)
318{
319 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
320 struct interface *tmp_ifp;
321 bool found = false;
322
323 if (!vrf)
324 return NULL;
325
326 if (ifindex == 0) {
327 tmp_ifp = RB_MIN(if_index_head, &vrf->ifaces_by_index);
328 /* skip the vrf interface */
329 if (tmp_ifp && if_is_vrf(tmp_ifp))
330 ifindex = tmp_ifp->ifindex;
331 else
332 return tmp_ifp;
333 }
334
335 RB_FOREACH (tmp_ifp, if_index_head, &vrf->ifaces_by_index) {
336 if (found) {
337 /* skip the vrf interface */
338 if (tmp_ifp && if_is_vrf(tmp_ifp))
339 continue;
340 else
341 return tmp_ifp;
342 }
343 if (tmp_ifp->ifindex == ifindex)
344 found = true;
345 }
346 return NULL;
347}
348
d62a17ae 349const char *ifindex2ifname(ifindex_t ifindex, vrf_id_t vrf_id)
718e3744 350{
d62a17ae 351 struct interface *ifp;
718e3744 352
d62a17ae 353 return ((ifp = if_lookup_by_index(ifindex, vrf_id)) != NULL)
354 ? ifp->name
355 : "unknown";
d2fc8896 356}
357
d62a17ae 358ifindex_t ifname2ifindex(const char *name, vrf_id_t vrf_id)
d2fc8896 359{
d62a17ae 360 struct interface *ifp;
d2fc8896 361
a36898e7 362 return ((ifp = if_lookup_by_name(name, vrf_id)) != NULL)
d62a17ae 363 ? ifp->ifindex
364 : IFINDEX_INTERNAL;
718e3744 365}
366
214d8a60 367/* Interface existence check by interface name. */
a36898e7 368struct interface *if_lookup_by_name(const char *name, vrf_id_t vrf_id)
718e3744 369{
a36898e7 370 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
f4e14fdb 371 struct interface if_tmp;
d62a17ae 372
5b8524f5
RW
373 if (!vrf || !name
374 || strnlen(name, INTERFACE_NAMSIZ) == INTERFACE_NAMSIZ)
bcc24579 375 return NULL;
f8962871 376
f4e14fdb
RW
377 strlcpy(if_tmp.name, name, sizeof(if_tmp.name));
378 return RB_FIND(if_name_head, &vrf->ifaces_by_name, &if_tmp);
f8962871
DS
379}
380
a2719d0e
IR
381struct interface *if_lookup_by_name_vrf(const char *name, struct vrf *vrf)
382{
383 struct interface if_tmp;
384
385 if (!name || strnlen(name, INTERFACE_NAMSIZ) == INTERFACE_NAMSIZ)
386 return NULL;
387
388 strlcpy(if_tmp.name, name, sizeof(if_tmp.name));
389 return RB_FIND(if_name_head, &vrf->ifaces_by_name, &if_tmp);
390}
391
0df2e188 392static struct interface *if_lookup_by_name_all_vrf(const char *name)
a349198f 393{
bcc24579 394 struct vrf *vrf;
d62a17ae 395 struct interface *ifp;
a349198f 396
bcc24579 397 if (!name || strnlen(name, INTERFACE_NAMSIZ) == INTERFACE_NAMSIZ)
d62a17ae 398 return NULL;
a349198f 399
f60a1188 400 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
5393f4db 401 ifp = if_lookup_by_name_vrf(name, vrf);
bcc24579 402 if (ifp)
d62a17ae 403 return ifp;
404 }
bcc24579 405
d62a17ae 406 return NULL;
a349198f 407}
408
35367f03 409static struct interface *if_lookup_by_index_all_vrf(ifindex_t ifindex)
ea7ec261
DD
410{
411 struct vrf *vrf;
412 struct interface *ifp;
413
414 if (ifindex == IFINDEX_INTERNAL)
415 return NULL;
416
417 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
47b474b5 418 ifp = if_lookup_by_ifindex(ifindex, vrf->vrf_id);
ea7ec261
DD
419 if (ifp)
420 return ifp;
421 }
422
423 return NULL;
424}
425
1e9044be
DL
426/* Lookup interface by IP address.
427 *
428 * supersedes if_lookup_exact_address(), which didn't care about up/down
429 * state. but all users we have either only care if the address is local
430 * (=> use if_address_is_local() please), or care about UP interfaces before
431 * anything else
432 *
433 * to accept only UP interfaces, check if_is_up() on the returned ifp.
434 */
435struct interface *if_lookup_address_local(const void *src, int family,
d62a17ae 436 vrf_id_t vrf_id)
718e3744 437{
f4e14fdb 438 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
d62a17ae 439 struct listnode *cnode;
1e9044be 440 struct interface *ifp, *best_down = NULL;
d62a17ae 441 struct prefix *p;
442 struct connected *c;
443
1e9044be
DL
444 if (family != AF_INET && family != AF_INET6)
445 return NULL;
446
451fda4f 447 FOR_ALL_INTERFACES (vrf, ifp) {
d62a17ae 448 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, c)) {
449 p = c->address;
450
1e9044be
DL
451 if (!p || p->family != family)
452 continue;
453
454 if (family == AF_INET) {
455 if (!IPV4_ADDR_SAME(&p->u.prefix4,
d62a17ae 456 (struct in_addr *)src))
1e9044be
DL
457 continue;
458 } else if (family == AF_INET6) {
459 if (!IPV6_ADDR_SAME(&p->u.prefix6,
d62a17ae 460 (struct in6_addr *)src))
1e9044be 461 continue;
d62a17ae 462 }
1e9044be
DL
463
464 if (if_is_up(ifp))
465 return ifp;
466 if (!best_down)
467 best_down = ifp;
0aabccc0 468 }
718e3744 469 }
1e9044be 470 return best_down;
718e3744 471}
472
3923b6e3 473/* Lookup interface by IP address. */
0b700537 474struct connected *if_lookup_address(const void *matchaddr, int family,
d62a17ae 475 vrf_id_t vrf_id)
718e3744 476{
f4e14fdb 477 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
d62a17ae 478 struct prefix addr;
479 int bestlen = 0;
480 struct listnode *cnode;
481 struct interface *ifp;
482 struct connected *c;
483 struct connected *match;
484
485 if (family == AF_INET) {
486 addr.family = AF_INET;
487 addr.u.prefix4 = *((struct in_addr *)matchaddr);
488 addr.prefixlen = IPV4_MAX_BITLEN;
489 } else if (family == AF_INET6) {
490 addr.family = AF_INET6;
491 addr.u.prefix6 = *((struct in6_addr *)matchaddr);
492 addr.prefixlen = IPV6_MAX_BITLEN;
493 }
718e3744 494
d62a17ae 495 match = NULL;
718e3744 496
451fda4f 497 FOR_ALL_INTERFACES (vrf, ifp) {
d62a17ae 498 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, c)) {
499 if (c->address && (c->address->family == AF_INET)
500 && prefix_match(CONNECTED_PREFIX(c), &addr)
501 && (c->address->prefixlen > bestlen)) {
502 bestlen = c->address->prefixlen;
503 match = c;
504 }
505 }
718e3744 506 }
d62a17ae 507 return match;
718e3744 508}
509
b81e97a8 510/* Lookup interface by prefix */
0b700537 511struct interface *if_lookup_prefix(const struct prefix *prefix, vrf_id_t vrf_id)
b81e97a8 512{
f4e14fdb 513 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
d62a17ae 514 struct listnode *cnode;
515 struct interface *ifp;
516 struct connected *c;
517
451fda4f 518 FOR_ALL_INTERFACES (vrf, ifp) {
d62a17ae 519 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, c)) {
520 if (prefix_cmp(c->address, prefix) == 0) {
521 return ifp;
522 }
523 }
524 }
525 return NULL;
b81e97a8
DD
526}
527
dad18a2f
QY
528size_t if_lookup_by_hwaddr(const uint8_t *hw_addr, size_t addrsz,
529 struct interface ***result, vrf_id_t vrf_id)
530{
531 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
532
533 struct list *rs = list_new();
534 struct interface *ifp;
535
536 FOR_ALL_INTERFACES (vrf, ifp) {
537 if (ifp->hw_addr_len == (int)addrsz
538 && !memcmp(hw_addr, ifp->hw_addr, addrsz))
539 listnode_add(rs, ifp);
540 }
541
542 if (rs->count) {
543 *result = XCALLOC(MTYPE_TMP,
544 sizeof(struct interface *) * rs->count);
545 list_to_array(rs, (void **)*result, rs->count);
546 }
547
548 int count = rs->count;
549
550 list_delete(&rs);
551
552 return count;
553}
554
555
718e3744 556/* Get interface by name if given name interface doesn't exist create
557 one. */
f60a1188
IR
558struct interface *if_get_by_name(const char *name, vrf_id_t vrf_id,
559 const char *vrf_name)
718e3744 560{
f60a1188
IR
561 struct interface *ifp = NULL;
562 struct vrf *vrf;
718e3744 563
8f90d89b 564 switch (vrf_get_backend()) {
b0b97a7f 565 case VRF_BACKEND_UNKNOWN:
8f90d89b 566 case VRF_BACKEND_NETNS:
f60a1188
IR
567 vrf = vrf_get(vrf_id, vrf_name);
568 assert(vrf);
569
570 ifp = if_lookup_by_name_vrf(name, vrf);
8f90d89b 571 if (ifp) {
8f90d89b
RW
572 /* If it came from the kernel or by way of zclient,
573 * believe it and update the ifp accordingly.
574 */
096f7609 575 if (ifp->vrf->vrf_id != vrf_id && vrf_id != VRF_UNKNOWN)
f60a1188
IR
576 if_update_to_new_vrf(ifp, vrf_id);
577
8f90d89b 578 return ifp;
ee2f2c23 579 }
8f90d89b 580
f60a1188 581 break;
ea7ec261 582 case VRF_BACKEND_VRF_LITE:
f60a1188 583 ifp = if_lookup_by_name_all_vrf(name);
ea7ec261 584 if (ifp) {
ea7ec261
DD
585 /* If it came from the kernel or by way of zclient,
586 * believe it and update the ifp accordingly.
587 */
096f7609 588 if (ifp->vrf->vrf_id != vrf_id && vrf_id != VRF_UNKNOWN)
f60a1188
IR
589 if_update_to_new_vrf(ifp, vrf_id);
590
ea7ec261
DD
591 return ifp;
592 }
f60a1188
IR
593
594 vrf = vrf_get(vrf_id, vrf_name);
595 assert(vrf);
596
597 break;
598 default:
599 return NULL;
ea7ec261
DD
600 }
601
f60a1188 602 return if_create_name(name, vrf);
ea7ec261
DD
603}
604
67f81586 605int if_set_index(struct interface *ifp, ifindex_t ifindex)
ff880b78 606{
67f81586
QY
607 if (ifp->ifindex == ifindex)
608 return 0;
609
67f81586
QY
610 /*
611 * If there is already an interface with this ifindex, we will collide
612 * on insertion, so don't even try.
613 */
096f7609 614 if (if_lookup_by_ifindex(ifindex, ifp->vrf->vrf_id))
67f81586 615 return -1;
ff880b78
RW
616
617 if (ifp->ifindex != IFINDEX_INTERNAL)
f60a1188 618 IFINDEX_RB_REMOVE(ifp->vrf, ifp);
ff880b78
RW
619
620 ifp->ifindex = ifindex;
621
67f81586
QY
622 if (ifp->ifindex != IFINDEX_INTERNAL) {
623 /*
624 * This should never happen, since we checked if there was
625 * already an interface with the desired ifindex at the top of
626 * the function. Nevertheless.
627 */
f60a1188 628 if (IFINDEX_RB_INSERT(ifp->vrf, ifp))
67f81586
QY
629 return -1;
630 }
631
632 return 0;
ff880b78
RW
633}
634
f60a1188 635static void if_set_name(struct interface *ifp, const char *name)
d5c65bf1 636{
d5c65bf1
SW
637 if (if_cmp_name_func(ifp->name, name) == 0)
638 return;
639
640 if (ifp->name[0] != '\0')
f60a1188 641 IFNAME_RB_REMOVE(ifp->vrf, ifp);
d5c65bf1
SW
642
643 strlcpy(ifp->name, name, sizeof(ifp->name));
644
645 if (ifp->name[0] != '\0')
f60a1188 646 IFNAME_RB_INSERT(ifp->vrf, ifp);
d5c65bf1
SW
647}
648
718e3744 649/* Does interface up ? */
6339042c 650int if_is_up(const struct interface *ifp)
718e3744 651{
d62a17ae 652 return ifp->flags & IFF_UP;
718e3744 653}
654
2e3b2e47 655/* Is interface running? */
6339042c 656int if_is_running(const struct interface *ifp)
2e3b2e47 657{
d62a17ae 658 return ifp->flags & IFF_RUNNING;
2e3b2e47 659}
660
661/* Is the interface operative, eg. either UP & RUNNING
244c1cdc
DS
662 or UP & !ZEBRA_INTERFACE_LINK_DETECTION and
663 if ptm checking is enabled, then ptm check has passed */
6339042c 664int if_is_operative(const struct interface *ifp)
2e3b2e47 665{
d62a17ae 666 return ((ifp->flags & IFF_UP)
667 && (((ifp->flags & IFF_RUNNING)
668 && (ifp->ptm_status || !ifp->ptm_enable))
669 || !CHECK_FLAG(ifp->status,
670 ZEBRA_INTERFACE_LINKDETECTION)));
244c1cdc
DS
671}
672
673/* Is the interface operative, eg. either UP & RUNNING
674 or UP & !ZEBRA_INTERFACE_LINK_DETECTION, without PTM check */
6339042c 675int if_is_no_ptm_operative(const struct interface *ifp)
244c1cdc 676{
d62a17ae 677 return ((ifp->flags & IFF_UP)
678 && ((ifp->flags & IFF_RUNNING)
679 || !CHECK_FLAG(ifp->status,
680 ZEBRA_INTERFACE_LINKDETECTION)));
2e3b2e47 681}
682
718e3744 683/* Is this loopback interface ? */
608c8870 684int if_is_loopback_exact(const struct interface *ifp)
718e3744 685{
d62a17ae 686 /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M
687 * but Y on platform N?
688 */
689 return (ifp->flags & (IFF_LOOPBACK | IFF_NOXMIT | IFF_VIRTUAL));
718e3744 690}
691
0c74bbe0 692/* Check interface is VRF */
6339042c 693int if_is_vrf(const struct interface *ifp)
0c74bbe0
CS
694{
695 return CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK);
696}
697
608c8870
IR
698/* Should this interface be treated as a loopback? */
699bool if_is_loopback(const struct interface *ifp)
fec4ca19 700{
608c8870 701 if (if_is_loopback_exact(ifp) || if_is_vrf(ifp))
fec4ca19
DS
702 return true;
703
704 return false;
705}
706
718e3744 707/* Does this interface support broadcast ? */
6339042c 708int if_is_broadcast(const struct interface *ifp)
718e3744 709{
d62a17ae 710 return ifp->flags & IFF_BROADCAST;
718e3744 711}
712
713/* Does this interface support broadcast ? */
6339042c 714int if_is_pointopoint(const struct interface *ifp)
718e3744 715{
d62a17ae 716 return ifp->flags & IFF_POINTOPOINT;
718e3744 717}
718
719/* Does this interface support multicast ? */
6339042c 720int if_is_multicast(const struct interface *ifp)
718e3744 721{
d62a17ae 722 return ifp->flags & IFF_MULTICAST;
718e3744 723}
724
725/* Printout flag information into log */
d62a17ae 726const char *if_flag_dump(unsigned long flag)
718e3744 727{
d62a17ae 728 int separator = 0;
729 static char logbuf[BUFSIZ];
730
731#define IFF_OUT_LOG(X, STR) \
732 if (flag & (X)) { \
733 if (separator) \
3393df5c 734 strlcat(logbuf, ",", sizeof(logbuf)); \
d62a17ae 735 else \
736 separator = 1; \
3393df5c 737 strlcat(logbuf, STR, sizeof(logbuf)); \
d62a17ae 738 }
718e3744 739
d62a17ae 740 strlcpy(logbuf, "<", BUFSIZ);
741 IFF_OUT_LOG(IFF_UP, "UP");
742 IFF_OUT_LOG(IFF_BROADCAST, "BROADCAST");
743 IFF_OUT_LOG(IFF_DEBUG, "DEBUG");
744 IFF_OUT_LOG(IFF_LOOPBACK, "LOOPBACK");
745 IFF_OUT_LOG(IFF_POINTOPOINT, "POINTOPOINT");
746 IFF_OUT_LOG(IFF_NOTRAILERS, "NOTRAILERS");
747 IFF_OUT_LOG(IFF_RUNNING, "RUNNING");
748 IFF_OUT_LOG(IFF_NOARP, "NOARP");
749 IFF_OUT_LOG(IFF_PROMISC, "PROMISC");
750 IFF_OUT_LOG(IFF_ALLMULTI, "ALLMULTI");
751 IFF_OUT_LOG(IFF_OACTIVE, "OACTIVE");
752 IFF_OUT_LOG(IFF_SIMPLEX, "SIMPLEX");
753 IFF_OUT_LOG(IFF_LINK0, "LINK0");
754 IFF_OUT_LOG(IFF_LINK1, "LINK1");
755 IFF_OUT_LOG(IFF_LINK2, "LINK2");
756 IFF_OUT_LOG(IFF_MULTICAST, "MULTICAST");
757 IFF_OUT_LOG(IFF_NOXMIT, "NOXMIT");
758 IFF_OUT_LOG(IFF_NORTEXCH, "NORTEXCH");
759 IFF_OUT_LOG(IFF_VIRTUAL, "VIRTUAL");
760 IFF_OUT_LOG(IFF_IPV4, "IPv4");
761 IFF_OUT_LOG(IFF_IPV6, "IPv6");
762
3393df5c 763 strlcat(logbuf, ">", sizeof(logbuf));
d62a17ae 764
765 return logbuf;
630c97ce 766#undef IFF_OUT_LOG
718e3744 767}
768
769/* For debugging */
d62a17ae 770static void if_dump(const struct interface *ifp)
718e3744 771{
d62a17ae 772 struct listnode *node;
773 struct connected *c __attribute__((unused));
774
f60a1188 775 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, c))
d62a17ae 776 zlog_info(
3efd0893 777 "Interface %s vrf %s(%u) index %d metric %d mtu %d mtu6 %d %s",
096f7609
IR
778 ifp->name, ifp->vrf->name, ifp->vrf->vrf_id,
779 ifp->ifindex, ifp->metric, ifp->mtu, ifp->mtu6,
a94fbcca 780 if_flag_dump(ifp->flags));
718e3744 781}
782
783/* Interface printing for all interface. */
d62a17ae 784void if_dump_all(void)
718e3744 785{
d62a17ae 786 struct vrf *vrf;
f4e14fdb 787 void *ifp;
d62a17ae 788
a2addae8 789 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id)
451fda4f 790 FOR_ALL_INTERFACES (vrf, ifp)
f4e14fdb 791 if_dump(ifp);
718e3744 792}
793
718e3744 794/* Allocate connected structure. */
d62a17ae 795struct connected *connected_new(void)
718e3744 796{
d62a17ae 797 return XCALLOC(MTYPE_CONNECTED, sizeof(struct connected));
718e3744 798}
799
a80beece 800/* Allocate nbr connected structure. */
d62a17ae 801struct nbr_connected *nbr_connected_new(void)
a80beece 802{
d62a17ae 803 return XCALLOC(MTYPE_NBR_CONNECTED, sizeof(struct nbr_connected));
a80beece
DS
804}
805
718e3744 806/* Free connected structure. */
721c0857 807void connected_free(struct connected **connected)
718e3744 808{
721c0857
DS
809 struct connected *ptr = *connected;
810
8fa77bc6
DA
811 prefix_free(&ptr->address);
812 prefix_free(&ptr->destination);
718e3744 813
721c0857 814 XFREE(MTYPE_CONNECTED_LABEL, ptr->label);
718e3744 815
721c0857
DS
816 XFREE(MTYPE_CONNECTED, ptr);
817 *connected = NULL;
718e3744 818}
819
a80beece 820/* Free nbr connected structure. */
d62a17ae 821void nbr_connected_free(struct nbr_connected *connected)
a80beece 822{
d62a17ae 823 if (connected->address)
63265b5c 824 prefix_free(&connected->address);
a80beece 825
d62a17ae 826 XFREE(MTYPE_NBR_CONNECTED, connected);
a80beece
DS
827}
828
829/* If same interface nbr address already exists... */
d62a17ae 830struct nbr_connected *nbr_connected_check(struct interface *ifp,
831 struct prefix *p)
a80beece 832{
d62a17ae 833 struct nbr_connected *ifc;
834 struct listnode *node;
a80beece 835
d62a17ae 836 for (ALL_LIST_ELEMENTS_RO(ifp->nbr_connected, node, ifc))
837 if (prefix_same(ifc->address, p))
838 return ifc;
a80beece 839
d62a17ae 840 return NULL;
a80beece
DS
841}
842
718e3744 843/* Print if_addr structure. */
d62a17ae 844static void __attribute__((unused))
845connected_log(struct connected *connected, char *str)
718e3744 846{
d62a17ae 847 struct prefix *p;
848 struct interface *ifp;
849 char logbuf[BUFSIZ];
850 char buf[BUFSIZ];
851
852 ifp = connected->ifp;
853 p = connected->address;
854
b219dda1 855 snprintf(logbuf, sizeof(logbuf), "%s interface %s vrf %s(%u) %s %pFX ",
096f7609 856 str, ifp->name, ifp->vrf->name, ifp->vrf->vrf_id,
b219dda1 857 prefix_family_str(p), p);
d62a17ae 858
859 p = connected->destination;
860 if (p) {
fd396924
CH
861 strlcat(logbuf, inet_ntop(p->family, &p->u.prefix, buf, BUFSIZ),
862 BUFSIZ);
d62a17ae 863 }
864 zlog_info("%s", logbuf);
718e3744 865}
866
a80beece 867/* Print if_addr structure. */
d62a17ae 868static void __attribute__((unused))
869nbr_connected_log(struct nbr_connected *connected, char *str)
a80beece 870{
d62a17ae 871 struct prefix *p;
872 struct interface *ifp;
873 char logbuf[BUFSIZ];
a80beece 874
d62a17ae 875 ifp = connected->ifp;
876 p = connected->address;
a80beece 877
b219dda1
DS
878 snprintf(logbuf, sizeof(logbuf), "%s interface %s %s %pFX ", str,
879 ifp->name, prefix_family_str(p), p);
a80beece 880
d62a17ae 881 zlog_info("%s", logbuf);
a80beece
DS
882}
883
718e3744 884/* If two connected address has same prefix return 1. */
0b700537
RW
885static int connected_same_prefix(const struct prefix *p1,
886 const struct prefix *p2)
718e3744 887{
d62a17ae 888 if (p1->family == p2->family) {
889 if (p1->family == AF_INET
890 && IPV4_ADDR_SAME(&p1->u.prefix4, &p2->u.prefix4))
891 return 1;
892 if (p1->family == AF_INET6
893 && IPV6_ADDR_SAME(&p1->u.prefix6, &p2->u.prefix6))
894 return 1;
895 }
896 return 0;
718e3744 897}
898
457ea8d4
GN
899/* count the number of connected addresses that are in the given family */
900unsigned int connected_count_by_family(struct interface *ifp, int family)
901{
902 struct listnode *cnode;
903 struct connected *connected;
904 unsigned int cnt = 0;
905
906 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected))
907 if (connected->address->family == family)
908 cnt++;
909
910 return cnt;
911}
912
d62a17ae 913struct connected *connected_lookup_prefix_exact(struct interface *ifp,
0b700537 914 const struct prefix *p)
38485402 915{
d62a17ae 916 struct listnode *node;
917 struct listnode *next;
918 struct connected *ifc;
38485402 919
d62a17ae 920 for (node = listhead(ifp->connected); node; node = next) {
921 ifc = listgetdata(node);
922 next = node->next;
38485402 923
d62a17ae 924 if (connected_same_prefix(ifc->address, p))
925 return ifc;
926 }
927 return NULL;
38485402
DS
928}
929
d62a17ae 930struct connected *connected_delete_by_prefix(struct interface *ifp,
931 struct prefix *p)
718e3744 932{
d62a17ae 933 struct listnode *node;
934 struct listnode *next;
935 struct connected *ifc;
936
937 /* In case of same prefix come, replace it with new one. */
938 for (node = listhead(ifp->connected); node; node = next) {
939 ifc = listgetdata(node);
940 next = node->next;
941
942 if (connected_same_prefix(ifc->address, p)) {
943 listnode_delete(ifp->connected, ifc);
944 return ifc;
945 }
718e3744 946 }
d62a17ae 947 return NULL;
718e3744 948}
949
bd40c341 950/* Find the address on our side that will be used when packets
727d104b 951 are sent to dst. */
d62a17ae 952struct connected *connected_lookup_prefix(struct interface *ifp,
0b700537 953 const struct prefix *addr)
727d104b 954{
d62a17ae 955 struct listnode *cnode;
956 struct connected *c;
957 struct connected *match;
958
959 match = NULL;
960
961 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, c)) {
962 if (c->address && (c->address->family == addr->family)
963 && prefix_match(CONNECTED_PREFIX(c), addr)
964 && (!match
965 || (c->address->prefixlen > match->address->prefixlen)))
966 match = c;
967 }
968 return match;
727d104b 969}
970
d62a17ae 971struct connected *connected_add_by_prefix(struct interface *ifp,
972 struct prefix *p,
973 struct prefix *destination)
4a7aac1b 974{
d62a17ae 975 struct connected *ifc;
4a7aac1b 976
d62a17ae 977 /* Allocate new connected address. */
978 ifc = connected_new();
979 ifc->ifp = ifp;
4a7aac1b 980
d62a17ae 981 /* Fetch interface address */
982 ifc->address = prefix_new();
983 memcpy(ifc->address, p, sizeof(struct prefix));
4a7aac1b 984
d62a17ae 985 /* Fetch dest address */
986 if (destination) {
987 ifc->destination = prefix_new();
988 memcpy(ifc->destination, destination, sizeof(struct prefix));
989 }
4a7aac1b 990
d62a17ae 991 /* Add connected address to the interface. */
992 listnode_add(ifp->connected, ifc);
993 return ifc;
4a7aac1b 994}
995
667179ca
QY
996struct connected *connected_get_linklocal(struct interface *ifp)
997{
998 struct listnode *n;
999 struct connected *c = NULL;
1000
1001 for (ALL_LIST_ELEMENTS_RO(ifp->connected, n, c)) {
1002 if (c->address->family == AF_INET6
1003 && IN6_IS_ADDR_LINKLOCAL(&c->address->u.prefix6))
1004 break;
1005 }
1006 return c;
1007}
1008
f4e14fdb 1009void if_terminate(struct vrf *vrf)
4bd045d5 1010{
f4e14fdb 1011 struct interface *ifp;
4bd045d5 1012
55cd0f61
DS
1013 while (!RB_EMPTY(if_name_head, &vrf->ifaces_by_name)) {
1014 ifp = RB_ROOT(if_name_head, &vrf->ifaces_by_name);
1015
f60a1188
IR
1016 if (ifp->node) {
1017 ifp->node->info = NULL;
1018 route_unlock_node(ifp->node);
d62a17ae 1019 }
f60a1188 1020 if_delete(&ifp);
d62a17ae 1021 }
4bd045d5 1022}
8ccc7e80 1023
d62a17ae 1024const char *if_link_type_str(enum zebra_link_type llt)
8ccc7e80 1025{
d62a17ae 1026 switch (llt) {
8ccc7e80 1027#define llts(T,S) case (T): return (S)
d62a17ae 1028 llts(ZEBRA_LLT_UNKNOWN, "Unknown");
1029 llts(ZEBRA_LLT_ETHER, "Ethernet");
1030 llts(ZEBRA_LLT_EETHER, "Experimental Ethernet");
1031 llts(ZEBRA_LLT_AX25, "AX.25 Level 2");
1032 llts(ZEBRA_LLT_PRONET, "PROnet token ring");
1033 llts(ZEBRA_LLT_IEEE802, "IEEE 802.2 Ethernet/TR/TB");
1034 llts(ZEBRA_LLT_ARCNET, "ARCnet");
1035 llts(ZEBRA_LLT_APPLETLK, "AppleTalk");
1036 llts(ZEBRA_LLT_DLCI, "Frame Relay DLCI");
1037 llts(ZEBRA_LLT_ATM, "ATM");
1038 llts(ZEBRA_LLT_METRICOM, "Metricom STRIP");
1039 llts(ZEBRA_LLT_IEEE1394, "IEEE 1394 IPv4");
1040 llts(ZEBRA_LLT_EUI64, "EUI-64");
1041 llts(ZEBRA_LLT_INFINIBAND, "InfiniBand");
1042 llts(ZEBRA_LLT_SLIP, "SLIP");
1043 llts(ZEBRA_LLT_CSLIP, "Compressed SLIP");
1044 llts(ZEBRA_LLT_SLIP6, "SLIPv6");
1045 llts(ZEBRA_LLT_CSLIP6, "Compressed SLIPv6");
ba62a5c5
DS
1046 llts(ZEBRA_LLT_RSRVD, "Reserved");
1047 llts(ZEBRA_LLT_ADAPT, "Adapt");
d62a17ae 1048 llts(ZEBRA_LLT_ROSE, "ROSE packet radio");
1049 llts(ZEBRA_LLT_X25, "CCITT X.25");
1050 llts(ZEBRA_LLT_PPP, "PPP");
1051 llts(ZEBRA_LLT_CHDLC, "Cisco HDLC");
1052 llts(ZEBRA_LLT_RAWHDLC, "Raw HDLC");
1053 llts(ZEBRA_LLT_LAPB, "LAPB");
1054 llts(ZEBRA_LLT_IPIP, "IPIP Tunnel");
1055 llts(ZEBRA_LLT_IPIP6, "IPIP6 Tunnel");
1056 llts(ZEBRA_LLT_FRAD, "FRAD");
1057 llts(ZEBRA_LLT_SKIP, "SKIP vif");
1058 llts(ZEBRA_LLT_LOOPBACK, "Loopback");
1059 llts(ZEBRA_LLT_LOCALTLK, "Localtalk");
1060 llts(ZEBRA_LLT_FDDI, "FDDI");
1061 llts(ZEBRA_LLT_SIT, "IPv6-in-IPv4 SIT");
1062 llts(ZEBRA_LLT_IPDDP, "IP-in-DDP tunnel");
1063 llts(ZEBRA_LLT_IPGRE, "GRE over IP");
ba62a5c5 1064 llts(ZEBRA_LLT_IP6GRE, "GRE over IPv6");
d62a17ae 1065 llts(ZEBRA_LLT_PIMREG, "PIMSM registration");
1066 llts(ZEBRA_LLT_HIPPI, "HiPPI");
ba62a5c5 1067 llts(ZEBRA_LLT_ECONET, "Acorn Econet");
d62a17ae 1068 llts(ZEBRA_LLT_IRDA, "IrDA");
1069 llts(ZEBRA_LLT_FCPP, "Fibre-Channel PtP");
1070 llts(ZEBRA_LLT_FCAL, "Fibre-Channel Arbitrated Loop");
1071 llts(ZEBRA_LLT_FCPL, "Fibre-Channel Public Loop");
1072 llts(ZEBRA_LLT_FCFABRIC, "Fibre-Channel Fabric");
1073 llts(ZEBRA_LLT_IEEE802_TR, "IEEE 802.2 Token Ring");
1074 llts(ZEBRA_LLT_IEEE80211, "IEEE 802.11");
1075 llts(ZEBRA_LLT_IEEE80211_RADIOTAP, "IEEE 802.11 Radiotap");
1076 llts(ZEBRA_LLT_IEEE802154, "IEEE 802.15.4");
1077 llts(ZEBRA_LLT_IEEE802154_PHY, "IEEE 802.15.4 Phy");
8ccc7e80 1078#undef llts
d62a17ae 1079 }
1080 return NULL;
8ccc7e80 1081}
16f1b9ee 1082
d62a17ae 1083struct if_link_params *if_link_params_get(struct interface *ifp)
16f1b9ee 1084{
d62a17ae 1085 int i;
16f1b9ee 1086
d62a17ae 1087 if (ifp->link_params != NULL)
1088 return ifp->link_params;
16f1b9ee 1089
d62a17ae 1090 struct if_link_params *iflp =
1091 XCALLOC(MTYPE_IF_LINK_PARAMS, sizeof(struct if_link_params));
16f1b9ee 1092
d62a17ae 1093 /* Set TE metric equal to standard metric */
1094 iflp->te_metric = ifp->metric;
16f1b9ee 1095
d62a17ae 1096 /* Compute default bandwidth based on interface */
1097 iflp->default_bw =
1098 ((ifp->bandwidth ? ifp->bandwidth : DEFAULT_BANDWIDTH)
5eb567ed 1099 * TE_MEGA_BIT / TE_BYTE);
16f1b9ee 1100
d62a17ae 1101 /* Set Max, Reservable and Unreserved Bandwidth */
1102 iflp->max_bw = iflp->default_bw;
1103 iflp->max_rsv_bw = iflp->default_bw;
1104 for (i = 0; i < MAX_CLASS_TYPE; i++)
1105 iflp->unrsv_bw[i] = iflp->default_bw;
16f1b9ee 1106
d62a17ae 1107 /* Update Link parameters status */
1108 iflp->lp_status =
1109 LP_TE_METRIC | LP_MAX_BW | LP_MAX_RSV_BW | LP_UNRSV_BW;
16f1b9ee 1110
d62a17ae 1111 /* Finally attach newly created Link Parameters */
1112 ifp->link_params = iflp;
16f1b9ee 1113
d62a17ae 1114 return iflp;
16f1b9ee
OD
1115}
1116
d62a17ae 1117void if_link_params_free(struct interface *ifp)
16f1b9ee 1118{
d62a17ae 1119 XFREE(MTYPE_IF_LINK_PARAMS, ifp->link_params);
16f1b9ee 1120}
a4bed468 1121
8f90d89b
RW
1122/* ----------- CLI commands ----------- */
1123
f60a1188
IR
1124/* Guess the VRF of an interface. */
1125static int vrfname_by_ifname(const char *ifname, const char **vrfname)
1126{
1127 struct vrf *vrf;
1128 struct interface *ifp;
1129 int count = 0;
1130
1131 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
1132 FOR_ALL_INTERFACES (vrf, ifp) {
1133 if (strmatch(ifp->name, ifname)) {
1134 *vrfname = vrf->name;
1135 count++;
1136 }
1137 }
1138 }
1139
1140 return count;
1141}
1142
8f90d89b
RW
1143/*
1144 * XPath: /frr-interface:lib/interface
1145 */
ca77b518 1146DEFPY_YANG_NOSH (interface,
8f90d89b 1147 interface_cmd,
e429a2a0 1148 "interface IFNAME [vrf NAME$vrf_name]",
8f90d89b
RW
1149 "Select an interface to configure\n"
1150 "Interface's name\n"
1151 VRF_CMD_HELP_STR)
1152{
1153 char xpath_list[XPATH_MAXLEN];
a36898e7 1154 struct interface *ifp;
f60a1188
IR
1155 struct vrf *vrf;
1156 int ret, count;
8f90d89b 1157
f60a1188 1158 if (vrf_is_backend_netns()) {
8f90d89b 1159 /*
f60a1188
IR
1160 * For backward compatibility, if the VRF name is not specified
1161 * and there is exactly one interface with this name in the
1162 * system, use its VRF. Otherwise fallback to the default VRF.
8f90d89b 1163 */
f60a1188
IR
1164 if (!vrf_name) {
1165 count = vrfname_by_ifname(ifname, &vrf_name);
1166 if (count != 1)
1167 vrf_name = VRF_DEFAULT_NAME;
8f90d89b 1168 }
574445ec
IR
1169
1170 snprintf(xpath_list, XPATH_MAXLEN,
1171 "/frr-interface:lib/interface[name='%s:%s']", vrf_name,
1172 ifname);
f60a1188 1173 } else {
574445ec
IR
1174 snprintf(xpath_list, XPATH_MAXLEN,
1175 "/frr-interface:lib/interface[name='%s']", ifname);
8f90d89b
RW
1176 }
1177
a6233bfc 1178 nb_cli_enqueue_change(vty, ".", NB_OP_CREATE, NULL);
fd396924 1179 ret = nb_cli_apply_changes_clear_pending(vty, xpath_list);
8f90d89b
RW
1180 if (ret == CMD_SUCCESS) {
1181 VTY_PUSH_XPATH(INTERFACE_NODE, xpath_list);
1182
1183 /*
1184 * For backward compatibility with old commands we still need
1185 * to use the qobj infrastructure. This can be removed once
1186 * all interface-level commands are converted to the new
1187 * northbound model.
1188 */
f60a1188
IR
1189 if (vrf_is_backend_netns()) {
1190 vrf = vrf_lookup_by_name(vrf_name);
1191 if (vrf)
1192 ifp = if_lookup_by_name_vrf(ifname, vrf);
1193 else
1194 ifp = NULL;
1195 } else {
1196 ifp = if_lookup_by_name_all_vrf(ifname);
1197 }
8f90d89b
RW
1198 if (ifp)
1199 VTY_PUSH_CONTEXT(INTERFACE_NODE, ifp);
1200 }
1201
1202 return ret;
1203}
1204
ca77b518 1205DEFPY_YANG (no_interface,
8f90d89b 1206 no_interface_cmd,
e429a2a0 1207 "no interface IFNAME [vrf NAME$vrf_name]",
8f90d89b
RW
1208 NO_STR
1209 "Delete a pseudo interface's configuration\n"
1210 "Interface's name\n"
1211 VRF_CMD_HELP_STR)
1212{
574445ec
IR
1213 char xpath_list[XPATH_MAXLEN];
1214 int count;
1215
1216 if (vrf_is_backend_netns()) {
1217 /*
1218 * For backward compatibility, if the VRF name is not specified
1219 * and there is exactly one interface with this name in the
1220 * system, use its VRF. Otherwise fallback to the default VRF.
1221 */
1222 if (!vrf_name) {
1223 count = vrfname_by_ifname(ifname, &vrf_name);
1224 if (count != 1)
1225 vrf_name = VRF_DEFAULT_NAME;
1226 }
1227
1228 snprintf(xpath_list, XPATH_MAXLEN,
1229 "/frr-interface:lib/interface[name='%s:%s']", vrf_name,
1230 ifname);
1231 } else {
1232 snprintf(xpath_list, XPATH_MAXLEN,
1233 "/frr-interface:lib/interface[name='%s']", ifname);
1234 }
8f90d89b 1235
95ce849b 1236 nb_cli_enqueue_change(vty, ".", NB_OP_DESTROY, NULL);
8f90d89b 1237
574445ec
IR
1238 return nb_cli_apply_changes(vty, xpath_list);
1239}
1240
1241static void netns_ifname_split(const char *xpath, char *ifname, char *vrfname)
1242{
1243 char *delim;
1244 int len;
1245
1246 assert(vrf_is_backend_netns());
1247
1248 delim = strchr(xpath, ':');
1249 assert(delim);
1250
1251 len = delim - xpath;
1252 memcpy(vrfname, xpath, len);
1253 vrfname[len] = 0;
1254
1255 strlcpy(ifname, delim + 1, XPATH_MAXLEN);
8f90d89b
RW
1256}
1257
25605051
IR
1258static void cli_show_interface(struct vty *vty, const struct lyd_node *dnode,
1259 bool show_defaults)
8f90d89b 1260{
574445ec 1261 vty_out(vty, "!\n");
8f90d89b 1262
574445ec
IR
1263 if (vrf_is_backend_netns()) {
1264 char ifname[XPATH_MAXLEN];
1265 char vrfname[XPATH_MAXLEN];
1266
1267 netns_ifname_split(yang_dnode_get_string(dnode, "./name"),
1268 ifname, vrfname);
1269
1270 vty_out(vty, "interface %s", ifname);
1271 if (!strmatch(vrfname, VRF_DEFAULT_NAME))
1272 vty_out(vty, " vrf %s", vrfname);
1273 } else {
1274 const char *ifname = yang_dnode_get_string(dnode, "./name");
1275
1276 vty_out(vty, "interface %s", ifname);
1277 }
8f90d89b 1278
8f90d89b
RW
1279 vty_out(vty, "\n");
1280}
1281
25605051
IR
1282static void cli_show_interface_end(struct vty *vty,
1283 const struct lyd_node *dnode)
07679ad9
IR
1284{
1285 vty_out(vty, "exit\n");
1286}
1287
8f90d89b
RW
1288/*
1289 * XPath: /frr-interface:lib/interface/description
1290 */
ca77b518 1291DEFPY_YANG (interface_desc,
8f90d89b
RW
1292 interface_desc_cmd,
1293 "description LINE...",
1294 "Interface specific description\n"
1295 "Characters describing this interface\n")
1296{
8f90d89b
RW
1297 char *desc;
1298 int ret;
1299
1300 desc = argv_concat(argv, argc, 1);
a6233bfc
RW
1301 nb_cli_enqueue_change(vty, "./description", NB_OP_MODIFY, desc);
1302 ret = nb_cli_apply_changes(vty, NULL);
8f90d89b
RW
1303 XFREE(MTYPE_TMP, desc);
1304
1305 return ret;
1306}
1307
ca77b518 1308DEFPY_YANG (no_interface_desc,
8f90d89b
RW
1309 no_interface_desc_cmd,
1310 "no description",
1311 NO_STR
1312 "Interface specific description\n")
1313{
95ce849b 1314 nb_cli_enqueue_change(vty, "./description", NB_OP_DESTROY, NULL);
a6233bfc
RW
1315
1316 return nb_cli_apply_changes(vty, NULL);
8f90d89b
RW
1317}
1318
25605051
IR
1319static void cli_show_interface_desc(struct vty *vty,
1320 const struct lyd_node *dnode,
1321 bool show_defaults)
8f90d89b
RW
1322{
1323 vty_out(vty, " description %s\n", yang_dnode_get_string(dnode, NULL));
1324}
1325
1326/* Interface autocomplete. */
1327static void if_autocomplete(vector comps, struct cmd_token *token)
1328{
1329 struct interface *ifp;
1330 struct vrf *vrf;
1331
1332 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
1333 FOR_ALL_INTERFACES (vrf, ifp) {
1334 vector_set(comps, XSTRDUP(MTYPE_COMPLETION, ifp->name));
1335 }
1336 }
1337}
1338
1339static const struct cmd_variable_handler if_var_handlers[] = {
1340 {/* "interface NAME" */
1341 .varname = "interface",
1342 .completions = if_autocomplete},
1343 {.tokenname = "IFNAME", .completions = if_autocomplete},
1344 {.tokenname = "INTERFACE", .completions = if_autocomplete},
1345 {.completions = NULL}};
1346
9da01b0b
IR
1347static struct cmd_node interface_node = {
1348 .name = "interface",
1349 .node = INTERFACE_NODE,
1350 .parent_node = CONFIG_NODE,
1351 .prompt = "%s(config-if)# ",
1352};
1353
104fd767
IR
1354static int if_config_write_single(const struct lyd_node *dnode, void *arg)
1355{
1356 nb_cli_show_dnode_cmds(arg, dnode, false);
1357
1358 return YANG_ITER_CONTINUE;
1359}
1360
1361static int if_nb_config_write(struct vty *vty)
1362{
1363 yang_dnode_iterate(if_config_write_single, vty, running_config->dnode,
1364 "/frr-interface:lib/interface");
1365 return 1;
1366}
1367
9da01b0b 1368void if_cmd_init(int (*config_write)(struct vty *))
8f90d89b
RW
1369{
1370 cmd_variable_handler_register(if_var_handlers);
1371
9da01b0b
IR
1372 interface_node.config_write = config_write;
1373 install_node(&interface_node);
1374
8f90d89b
RW
1375 install_element(CONFIG_NODE, &interface_cmd);
1376 install_element(CONFIG_NODE, &no_interface_cmd);
1377
1378 install_default(INTERFACE_NODE);
1379 install_element(INTERFACE_NODE, &interface_desc_cmd);
1380 install_element(INTERFACE_NODE, &no_interface_desc_cmd);
1381}
1382
104fd767
IR
1383void if_cmd_init_default(void)
1384{
1385 if_cmd_init(if_nb_config_write);
1386}
1387
138c5a74
DS
1388void if_zapi_callbacks(int (*create)(struct interface *ifp),
1389 int (*up)(struct interface *ifp),
1390 int (*down)(struct interface *ifp),
1391 int (*destroy)(struct interface *ifp))
1392{
1393 ifp_master.create_hook = create;
1394 ifp_master.up_hook = up;
1395 ifp_master.down_hook = down;
1396 ifp_master.destroy_hook = destroy;
1397}
1398
a4bed468
RW
1399/* ------- Northbound callbacks ------- */
1400
1401/*
1402 * XPath: /frr-interface:lib/interface
1403 */
60ee8be1 1404static int lib_interface_create(struct nb_cb_create_args *args)
a4bed468 1405{
8f90d89b 1406 const char *ifname;
a36898e7 1407 struct interface *ifp;
8f90d89b 1408
60ee8be1 1409 ifname = yang_dnode_get_string(args->dnode, "./name");
8f90d89b 1410
60ee8be1 1411 switch (args->event) {
8f90d89b 1412 case NB_EV_VALIDATE:
574445ec
IR
1413 if (vrf_is_backend_netns()) {
1414 char ifname_ns[XPATH_MAXLEN];
1415 char vrfname_ns[XPATH_MAXLEN];
1416
1417 netns_ifname_split(ifname, ifname_ns, vrfname_ns);
1418
1419 if (strlen(ifname_ns) > 16) {
1420 snprintf(
1421 args->errmsg, args->errmsg_len,
1422 "Maximum interface name length is 16 characters");
1423 return NB_ERR_VALIDATION;
1424 }
1425 if (strlen(vrfname_ns) > 36) {
1426 snprintf(
1427 args->errmsg, args->errmsg_len,
1428 "Maximum VRF name length is 36 characters");
1429 return NB_ERR_VALIDATION;
1430 }
1431 } else {
1432 if (strlen(ifname) > 16) {
1433 snprintf(
1434 args->errmsg, args->errmsg_len,
1435 "Maximum interface name length is 16 characters");
1436 return NB_ERR_VALIDATION;
1437 }
1438 }
1439 break;
8f90d89b
RW
1440 case NB_EV_PREPARE:
1441 case NB_EV_ABORT:
1442 break;
1443 case NB_EV_APPLY:
574445ec
IR
1444 if (vrf_is_backend_netns()) {
1445 char ifname_ns[XPATH_MAXLEN];
1446 char vrfname_ns[XPATH_MAXLEN];
1447
1448 netns_ifname_split(ifname, ifname_ns, vrfname_ns);
1449
1450 ifp = if_get_by_name(ifname_ns, VRF_UNKNOWN,
1451 vrfname_ns);
1452 } else {
1453 ifp = if_get_by_name(ifname, VRF_UNKNOWN,
1454 VRF_DEFAULT_NAME);
1455 }
1d311a05
DS
1456
1457 ifp->configured = true;
60ee8be1 1458 nb_running_set_entry(args->dnode, ifp);
8f90d89b
RW
1459 break;
1460 }
1461
a4bed468
RW
1462 return NB_OK;
1463}
1464
60ee8be1 1465static int lib_interface_destroy(struct nb_cb_destroy_args *args)
a4bed468 1466{
8f90d89b 1467 struct interface *ifp;
ce27a13e 1468 struct vrf *vrf;
8f90d89b 1469
60ee8be1 1470 switch (args->event) {
8f90d89b 1471 case NB_EV_VALIDATE:
60ee8be1 1472 ifp = nb_running_get_entry(args->dnode, NULL, true);
8f90d89b 1473 if (CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
10bdc68f
RW
1474 snprintf(args->errmsg, args->errmsg_len,
1475 "only inactive interfaces can be deleted");
8f90d89b
RW
1476 return NB_ERR_VALIDATION;
1477 }
1478 break;
1479 case NB_EV_PREPARE:
1480 case NB_EV_ABORT:
1481 break;
1482 case NB_EV_APPLY:
60ee8be1 1483 ifp = nb_running_unset_entry(args->dnode);
ce27a13e 1484 vrf = ifp->vrf;
1d311a05
DS
1485
1486 ifp->configured = false;
f609709a 1487 if_delete(&ifp);
ce27a13e
IR
1488
1489 if (!vrf_is_enabled(vrf))
1490 vrf_delete(vrf);
8f90d89b
RW
1491 break;
1492 }
1493
a4bed468
RW
1494 return NB_OK;
1495}
1496
f88647ef
QY
1497/*
1498 * XPath: /frr-interface:lib/interface
1499 */
60ee8be1 1500static const void *lib_interface_get_next(struct nb_cb_get_next_args *args)
f88647ef
QY
1501{
1502 struct vrf *vrf;
60ee8be1 1503 struct interface *pif = (struct interface *)args->list_entry;
f88647ef 1504
60ee8be1 1505 if (args->list_entry == NULL) {
f88647ef
QY
1506 vrf = RB_MIN(vrf_name_head, &vrfs_by_name);
1507 assert(vrf);
1508 pif = RB_MIN(if_name_head, &vrf->ifaces_by_name);
1509 } else {
f60a1188 1510 vrf = pif->vrf;
f88647ef
QY
1511 pif = RB_NEXT(if_name_head, pif);
1512 /* if no more interfaces, switch to next vrf */
1513 while (pif == NULL) {
1514 vrf = RB_NEXT(vrf_name_head, vrf);
1515 if (!vrf)
1516 return NULL;
1517 pif = RB_MIN(if_name_head, &vrf->ifaces_by_name);
1518 }
1519 }
1520
1521 return pif;
1522}
1523
60ee8be1 1524static int lib_interface_get_keys(struct nb_cb_get_keys_args *args)
f88647ef 1525{
60ee8be1 1526 const struct interface *ifp = args->list_entry;
f88647ef 1527
574445ec
IR
1528 args->keys->num = 1;
1529
1530 if (vrf_is_backend_netns())
1531 snprintf(args->keys->key[0], sizeof(args->keys->key[0]),
1532 "%s:%s", ifp->vrf->name, ifp->name);
1533 else
1534 snprintf(args->keys->key[0], sizeof(args->keys->key[0]), "%s",
1535 ifp->name);
f88647ef
QY
1536
1537 return NB_OK;
1538}
1539
60ee8be1
RW
1540static const void *
1541lib_interface_lookup_entry(struct nb_cb_lookup_entry_args *args)
f88647ef 1542{
574445ec
IR
1543 if (vrf_is_backend_netns()) {
1544 char ifname[XPATH_MAXLEN];
1545 char vrfname[XPATH_MAXLEN];
1546 struct vrf *vrf;
1547
1548 netns_ifname_split(args->keys->key[0], ifname, vrfname);
f88647ef 1549
574445ec
IR
1550 vrf = vrf_lookup_by_name(vrfname);
1551
1552 return vrf ? if_lookup_by_name(ifname, vrf->vrf_id) : NULL;
1553 } else {
1554 return if_lookup_by_name_all_vrf(args->keys->key[0]);
1555 }
f88647ef
QY
1556}
1557
a4bed468
RW
1558/*
1559 * XPath: /frr-interface:lib/interface/description
1560 */
60ee8be1 1561static int lib_interface_description_modify(struct nb_cb_modify_args *args)
a4bed468 1562{
8f90d89b
RW
1563 struct interface *ifp;
1564 const char *description;
1565
60ee8be1 1566 if (args->event != NB_EV_APPLY)
8f90d89b
RW
1567 return NB_OK;
1568
60ee8be1 1569 ifp = nb_running_get_entry(args->dnode, NULL, true);
0a22ddfb 1570 XFREE(MTYPE_TMP, ifp->desc);
60ee8be1 1571 description = yang_dnode_get_string(args->dnode, NULL);
8f90d89b
RW
1572 ifp->desc = XSTRDUP(MTYPE_TMP, description);
1573
a4bed468
RW
1574 return NB_OK;
1575}
1576
60ee8be1 1577static int lib_interface_description_destroy(struct nb_cb_destroy_args *args)
a4bed468 1578{
8f90d89b
RW
1579 struct interface *ifp;
1580
60ee8be1 1581 if (args->event != NB_EV_APPLY)
8f90d89b
RW
1582 return NB_OK;
1583
60ee8be1 1584 ifp = nb_running_get_entry(args->dnode, NULL, true);
0a22ddfb 1585 XFREE(MTYPE_TMP, ifp->desc);
8f90d89b 1586
a4bed468
RW
1587 return NB_OK;
1588}
1589
574445ec
IR
1590/*
1591 * XPath: /frr-interface:lib/interface/vrf
1592 */
1593static struct yang_data *
1594lib_interface_vrf_get_elem(struct nb_cb_get_elem_args *args)
1595{
1596 const struct interface *ifp = args->list_entry;
1597
1598 return yang_data_new_string(args->xpath, ifp->vrf->name);
1599}
1600
4e0a7355
CS
1601/*
1602 * XPath: /frr-interface:lib/interface/state/if-index
1603 */
60ee8be1
RW
1604static struct yang_data *
1605lib_interface_state_if_index_get_elem(struct nb_cb_get_elem_args *args)
4e0a7355 1606{
60ee8be1 1607 const struct interface *ifp = args->list_entry;
4e0a7355 1608
60ee8be1 1609 return yang_data_new_int32(args->xpath, ifp->ifindex);
4e0a7355
CS
1610}
1611
1612/*
1613 * XPath: /frr-interface:lib/interface/state/mtu
1614 */
60ee8be1
RW
1615static struct yang_data *
1616lib_interface_state_mtu_get_elem(struct nb_cb_get_elem_args *args)
4e0a7355 1617{
60ee8be1 1618 const struct interface *ifp = args->list_entry;
4e0a7355 1619
60ee8be1 1620 return yang_data_new_uint16(args->xpath, ifp->mtu);
4e0a7355
CS
1621}
1622
1623/*
1624 * XPath: /frr-interface:lib/interface/state/mtu6
1625 */
60ee8be1
RW
1626static struct yang_data *
1627lib_interface_state_mtu6_get_elem(struct nb_cb_get_elem_args *args)
4e0a7355 1628{
60ee8be1 1629 const struct interface *ifp = args->list_entry;
4e0a7355 1630
60ee8be1 1631 return yang_data_new_uint32(args->xpath, ifp->mtu6);
4e0a7355
CS
1632}
1633
1634/*
1635 * XPath: /frr-interface:lib/interface/state/speed
1636 */
60ee8be1
RW
1637static struct yang_data *
1638lib_interface_state_speed_get_elem(struct nb_cb_get_elem_args *args)
4e0a7355 1639{
60ee8be1 1640 const struct interface *ifp = args->list_entry;
4e0a7355 1641
60ee8be1 1642 return yang_data_new_uint32(args->xpath, ifp->speed);
4e0a7355
CS
1643}
1644
1645/*
1646 * XPath: /frr-interface:lib/interface/state/metric
1647 */
60ee8be1
RW
1648static struct yang_data *
1649lib_interface_state_metric_get_elem(struct nb_cb_get_elem_args *args)
4e0a7355 1650{
60ee8be1 1651 const struct interface *ifp = args->list_entry;
4e0a7355 1652
60ee8be1 1653 return yang_data_new_uint32(args->xpath, ifp->metric);
4e0a7355
CS
1654}
1655
1656/*
1657 * XPath: /frr-interface:lib/interface/state/flags
1658 */
60ee8be1
RW
1659static struct yang_data *
1660lib_interface_state_flags_get_elem(struct nb_cb_get_elem_args *args)
4e0a7355
CS
1661{
1662 /* TODO: implement me. */
1663 return NULL;
1664}
1665
1666/*
1667 * XPath: /frr-interface:lib/interface/state/type
1668 */
60ee8be1
RW
1669static struct yang_data *
1670lib_interface_state_type_get_elem(struct nb_cb_get_elem_args *args)
4e0a7355
CS
1671{
1672 /* TODO: implement me. */
1673 return NULL;
1674}
1675
1676/*
1677 * XPath: /frr-interface:lib/interface/state/phy-address
1678 */
60ee8be1
RW
1679static struct yang_data *
1680lib_interface_state_phy_address_get_elem(struct nb_cb_get_elem_args *args)
4e0a7355 1681{
60ee8be1 1682 const struct interface *ifp = args->list_entry;
4e0a7355
CS
1683 struct ethaddr macaddr;
1684
1685 memcpy(&macaddr.octet, ifp->hw_addr, ETH_ALEN);
1686
60ee8be1 1687 return yang_data_new_mac(args->xpath, &macaddr);
4e0a7355
CS
1688}
1689
a4bed468
RW
1690/* clang-format off */
1691const struct frr_yang_module_info frr_interface_info = {
1692 .name = "frr-interface",
1693 .nodes = {
1694 {
1695 .xpath = "/frr-interface:lib/interface",
53280f93
DL
1696 .cbs = {
1697 .create = lib_interface_create,
1698 .destroy = lib_interface_destroy,
1699 .cli_show = cli_show_interface,
07679ad9 1700 .cli_show_end = cli_show_interface_end,
f88647ef
QY
1701 .get_next = lib_interface_get_next,
1702 .get_keys = lib_interface_get_keys,
1703 .lookup_entry = lib_interface_lookup_entry,
53280f93 1704 },
a4bed468
RW
1705 },
1706 {
1707 .xpath = "/frr-interface:lib/interface/description",
53280f93
DL
1708 .cbs = {
1709 .modify = lib_interface_description_modify,
1710 .destroy = lib_interface_description_destroy,
1711 .cli_show = cli_show_interface_desc,
1712 },
a4bed468 1713 },
574445ec
IR
1714 {
1715 .xpath = "/frr-interface:lib/interface/vrf",
1716 .cbs = {
1717 .get_elem = lib_interface_vrf_get_elem,
1718 }
1719 },
4e0a7355
CS
1720 {
1721 .xpath = "/frr-interface:lib/interface/state/if-index",
1722 .cbs = {
1723 .get_elem = lib_interface_state_if_index_get_elem,
1724 }
1725 },
1726 {
1727 .xpath = "/frr-interface:lib/interface/state/mtu",
1728 .cbs = {
1729 .get_elem = lib_interface_state_mtu_get_elem,
1730 }
1731 },
1732 {
1733 .xpath = "/frr-interface:lib/interface/state/mtu6",
1734 .cbs = {
1735 .get_elem = lib_interface_state_mtu6_get_elem,
1736 }
1737 },
1738 {
1739 .xpath = "/frr-interface:lib/interface/state/speed",
1740 .cbs = {
1741 .get_elem = lib_interface_state_speed_get_elem,
1742 }
1743 },
1744 {
1745 .xpath = "/frr-interface:lib/interface/state/metric",
1746 .cbs = {
1747 .get_elem = lib_interface_state_metric_get_elem,
1748 }
1749 },
1750 {
1751 .xpath = "/frr-interface:lib/interface/state/flags",
1752 .cbs = {
1753 .get_elem = lib_interface_state_flags_get_elem,
1754 }
1755 },
1756 {
1757 .xpath = "/frr-interface:lib/interface/state/type",
1758 .cbs = {
1759 .get_elem = lib_interface_state_type_get_elem,
1760 }
1761 },
1762 {
1763 .xpath = "/frr-interface:lib/interface/state/phy-address",
1764 .cbs = {
1765 .get_elem = lib_interface_state_phy_address_get_elem,
1766 }
1767 },
a4bed468
RW
1768 {
1769 .xpath = NULL,
1770 },
1771 }
1772};