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