]>
Commit | Line | Data |
---|---|---|
acddc0ed | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
b72ede27 FL |
2 | /* |
3 | * VRF functions. | |
4 | * Copyright (C) 2014 6WIND S.A. | |
b72ede27 FL |
5 | */ |
6 | ||
7 | #include <zebra.h> | |
8 | ||
6a69b354 | 9 | #include "if.h" |
b72ede27 | 10 | #include "vrf.h" |
7922fc65 | 11 | #include "vrf_int.h" |
b72ede27 FL |
12 | #include "prefix.h" |
13 | #include "table.h" | |
14 | #include "log.h" | |
15 | #include "memory.h" | |
19dc275e | 16 | #include "command.h" |
b95c1883 | 17 | #include "ns.h" |
3bc34908 | 18 | #include "privs.h" |
98cbbaea | 19 | #include "nexthop_group.h" |
b66d022e | 20 | #include "lib_errors.h" |
bc867a5d | 21 | #include "northbound.h" |
8b4cb7a6 | 22 | #include "northbound_cli.h" |
19dc275e | 23 | |
1eb92f06 | 24 | /* default VRF name value used when VRF backend is not NETNS */ |
dd114702 | 25 | #define VRF_DEFAULT_NAME_INTERNAL "default" |
ec31f30d | 26 | |
bf8d3d6a DL |
27 | DEFINE_MTYPE_STATIC(LIB, VRF, "VRF"); |
28 | DEFINE_MTYPE_STATIC(LIB, VRF_BITMAP, "VRF bit-map"); | |
4a1ab8e4 | 29 | |
96244aca | 30 | DEFINE_QOBJ_TYPE(vrf); |
e80e7cce | 31 | |
d62a17ae | 32 | static __inline int vrf_id_compare(const struct vrf *, const struct vrf *); |
33 | static __inline int vrf_name_compare(const struct vrf *, const struct vrf *); | |
1a1a7065 | 34 | |
d62a17ae | 35 | RB_GENERATE(vrf_id_head, vrf, id_entry, vrf_id_compare); |
36 | RB_GENERATE(vrf_name_head, vrf, name_entry, vrf_name_compare); | |
1a1a7065 | 37 | |
d62a17ae | 38 | struct vrf_id_head vrfs_by_id = RB_INITIALIZER(&vrfs_by_id); |
39 | struct vrf_name_head vrfs_by_name = RB_INITIALIZER(&vrfs_by_name); | |
1a1a7065 | 40 | |
78dd30b2 | 41 | static int vrf_backend; |
72261ecd | 42 | static int vrf_backend_configured; |
c200f5e1 | 43 | static char vrf_default_name[VRF_NAMSIZ] = VRF_DEFAULT_NAME_INTERNAL; |
78dd30b2 | 44 | |
19dc275e DS |
45 | /* |
46 | * Turn on/off debug code | |
47 | * for vrf. | |
48 | */ | |
c17faa4b | 49 | static int debug_vrf = 0; |
b72ede27 | 50 | |
b72ede27 | 51 | /* Holding VRF hooks */ |
1b3e9a21 | 52 | static struct vrf_master { |
d62a17ae | 53 | int (*vrf_new_hook)(struct vrf *); |
54 | int (*vrf_delete_hook)(struct vrf *); | |
55 | int (*vrf_enable_hook)(struct vrf *); | |
56 | int (*vrf_disable_hook)(struct vrf *); | |
57 | } vrf_master = { | |
58 | 0, | |
59 | }; | |
b72ede27 | 60 | |
d62a17ae | 61 | static int vrf_is_enabled(struct vrf *vrf); |
e5bf3e1e | 62 | |
216b18ef | 63 | /* VRF list existance check by name. */ |
d62a17ae | 64 | struct vrf *vrf_lookup_by_name(const char *name) |
216b18ef | 65 | { |
d62a17ae | 66 | struct vrf vrf; |
67 | strlcpy(vrf.name, name, sizeof(vrf.name)); | |
68 | return (RB_FIND(vrf_name_head, &vrfs_by_name, &vrf)); | |
216b18ef | 69 | } |
216b18ef | 70 | |
d62a17ae | 71 | static __inline int vrf_id_compare(const struct vrf *a, const struct vrf *b) |
b72ede27 | 72 | { |
d62a17ae | 73 | return (a->vrf_id - b->vrf_id); |
216b18ef DS |
74 | } |
75 | ||
d62a17ae | 76 | static int vrf_name_compare(const struct vrf *a, const struct vrf *b) |
b72ede27 | 77 | { |
d62a17ae | 78 | return strcmp(a->name, b->name); |
b72ede27 FL |
79 | } |
80 | ||
ce1be369 PG |
81 | int vrf_switch_to_netns(vrf_id_t vrf_id) |
82 | { | |
83 | char *name; | |
84 | struct vrf *vrf = vrf_lookup_by_id(vrf_id); | |
85 | ||
ce1be369 | 86 | /* VRF is default VRF. silently ignore */ |
e26aedbe | 87 | if (!vrf || vrf->vrf_id == VRF_DEFAULT) |
9dff1132 | 88 | return 1; /* 1 = default */ |
e26aedbe PG |
89 | /* VRF has no NETNS backend. silently ignore */ |
90 | if (vrf->data.l.netns_name[0] == '\0') | |
9dff1132 | 91 | return 2; /* 2 = no netns */ |
ce1be369 PG |
92 | name = ns_netns_pathname(NULL, vrf->data.l.netns_name); |
93 | if (debug_vrf) | |
94 | zlog_debug("VRF_SWITCH: %s(%u)", name, vrf->vrf_id); | |
95 | return ns_switch_to_netns(name); | |
96 | } | |
97 | ||
98 | int vrf_switchback_to_initial(void) | |
99 | { | |
100 | int ret = ns_switchback_to_initial(); | |
101 | ||
102 | if (ret == 0 && debug_vrf) | |
103 | zlog_debug("VRF_SWITCHBACK"); | |
104 | return ret; | |
105 | } | |
106 | ||
216b18ef | 107 | /* Get a VRF. If not found, create one. |
34f8e6af DS |
108 | * Arg: |
109 | * name - The name of the vrf. May be NULL if unknown. | |
110 | * vrf_id - The vrf_id of the vrf. May be VRF_UNKNOWN if unknown | |
216b18ef | 111 | * Description: Please note that this routine can be called with just the name |
34f8e6af DS |
112 | * and 0 vrf-id |
113 | */ | |
d62a17ae | 114 | struct vrf *vrf_get(vrf_id_t vrf_id, const char *name) |
115 | { | |
116 | struct vrf *vrf = NULL; | |
117 | int new = 0; | |
118 | ||
d62a17ae | 119 | /* Nothing to see, move along here */ |
120 | if (!name && vrf_id == VRF_UNKNOWN) | |
121 | return NULL; | |
122 | ||
0c2bac38 PG |
123 | /* attempt to find already available VRF |
124 | */ | |
125 | if (name) | |
126 | vrf = vrf_lookup_by_name(name); | |
dd114702 PG |
127 | if (vrf && vrf_id != VRF_UNKNOWN |
128 | && vrf->vrf_id != VRF_UNKNOWN | |
129 | && vrf->vrf_id != vrf_id) { | |
130 | zlog_debug("VRF_GET: avoid %s creation(%u), same name exists (%u)", | |
131 | name, vrf_id, vrf->vrf_id); | |
132 | return NULL; | |
133 | } | |
d62a17ae | 134 | /* Try to find VRF both by ID and name */ |
0c2bac38 | 135 | if (!vrf && vrf_id != VRF_UNKNOWN) |
d62a17ae | 136 | vrf = vrf_lookup_by_id(vrf_id); |
d62a17ae | 137 | |
138 | if (vrf == NULL) { | |
139 | vrf = XCALLOC(MTYPE_VRF, sizeof(struct vrf)); | |
140 | vrf->vrf_id = VRF_UNKNOWN; | |
d62a17ae | 141 | QOBJ_REG(vrf, vrf); |
142 | new = 1; | |
143 | ||
144 | if (debug_vrf) | |
145 | zlog_debug("VRF(%u) %s is created.", vrf_id, | |
146 | (name) ? name : "(NULL)"); | |
147 | } | |
148 | ||
149 | /* Set identifier */ | |
150 | if (vrf_id != VRF_UNKNOWN && vrf->vrf_id == VRF_UNKNOWN) { | |
151 | vrf->vrf_id = vrf_id; | |
152 | RB_INSERT(vrf_id_head, &vrfs_by_id, vrf); | |
153 | } | |
154 | ||
155 | /* Set name */ | |
156 | if (name && vrf->name[0] != '\0' && strcmp(name, vrf->name)) { | |
87272aff | 157 | /* update the vrf name */ |
d62a17ae | 158 | RB_REMOVE(vrf_name_head, &vrfs_by_name, vrf); |
87272aff PG |
159 | strlcpy(vrf->data.l.netns_name, |
160 | name, NS_NAMSIZ); | |
d62a17ae | 161 | strlcpy(vrf->name, name, sizeof(vrf->name)); |
162 | RB_INSERT(vrf_name_head, &vrfs_by_name, vrf); | |
163 | } else if (name && vrf->name[0] == '\0') { | |
164 | strlcpy(vrf->name, name, sizeof(vrf->name)); | |
165 | RB_INSERT(vrf_name_head, &vrfs_by_name, vrf); | |
166 | } | |
d62a17ae | 167 | if (new &&vrf_master.vrf_new_hook) |
168 | (*vrf_master.vrf_new_hook)(vrf); | |
169 | ||
170 | return vrf; | |
b72ede27 FL |
171 | } |
172 | ||
75d26fb3 | 173 | /* Update a VRF. If not found, create one. |
174 | * Arg: | |
175 | * name - The name of the vrf. | |
176 | * vrf_id - The vrf_id of the vrf. | |
177 | * Description: This function first finds the vrf using its name. If the vrf is | |
178 | * found and the vrf-id of the existing vrf does not match the new vrf id, it | |
179 | * will disable the existing vrf and update it with new vrf-id. If the vrf is | |
180 | * not found, it will create the vrf with given name and the new vrf id. | |
181 | */ | |
182 | struct vrf *vrf_update(vrf_id_t new_vrf_id, const char *name) | |
183 | { | |
184 | struct vrf *vrf = NULL; | |
185 | ||
186 | /*Treat VRF add for existing vrf as update | |
187 | * Update VRF ID and also update in VRF ID table | |
188 | */ | |
189 | if (name) | |
190 | vrf = vrf_lookup_by_name(name); | |
191 | if (vrf && new_vrf_id != VRF_UNKNOWN && vrf->vrf_id != VRF_UNKNOWN | |
192 | && vrf->vrf_id != new_vrf_id) { | |
193 | if (debug_vrf) { | |
194 | zlog_debug( | |
195 | "Vrf Update event: %s old id: %u, new id: %u", | |
196 | name, vrf->vrf_id, new_vrf_id); | |
197 | } | |
198 | ||
199 | /*Disable the vrf to simulate implicit delete | |
200 | * so that all stale routes are deleted | |
201 | * This vrf will be enabled down the line | |
202 | */ | |
203 | vrf_disable(vrf); | |
204 | ||
205 | ||
206 | RB_REMOVE(vrf_id_head, &vrfs_by_id, vrf); | |
207 | vrf->vrf_id = new_vrf_id; | |
208 | RB_INSERT(vrf_id_head, &vrfs_by_id, vrf); | |
209 | ||
210 | } else { | |
211 | ||
212 | /* | |
213 | * vrf_get is implied creation if it does not exist | |
214 | */ | |
215 | vrf = vrf_get(new_vrf_id, name); | |
216 | } | |
217 | return vrf; | |
218 | } | |
219 | ||
84915b0a | 220 | /* Delete a VRF. This is called when the underlying VRF goes away, a |
221 | * pre-configured VRF is deleted or when shutting down (vrf_terminate()). | |
222 | */ | |
d62a17ae | 223 | void vrf_delete(struct vrf *vrf) |
b72ede27 | 224 | { |
d62a17ae | 225 | if (debug_vrf) |
c7384cf8 DS |
226 | zlog_debug("VRF %s(%u) is to be deleted.", vrf->name, |
227 | vrf->vrf_id); | |
b72ede27 | 228 | |
d62a17ae | 229 | if (vrf_is_enabled(vrf)) |
230 | vrf_disable(vrf); | |
e5bf3e1e | 231 | |
f60a1188 IR |
232 | if (vrf->vrf_id != VRF_UNKNOWN) { |
233 | RB_REMOVE(vrf_id_head, &vrfs_by_id, vrf); | |
234 | vrf->vrf_id = VRF_UNKNOWN; | |
235 | } | |
236 | ||
84915b0a | 237 | /* If the VRF is user configured, it'll stick around, just remove |
238 | * the ID mapping. Interfaces assigned to this VRF should've been | |
239 | * removed already as part of the VRF going down. | |
240 | */ | |
9acc98c8 | 241 | if (vrf_is_user_cfged(vrf)) |
84915b0a | 242 | return; |
84915b0a | 243 | |
f60a1188 IR |
244 | /* Do not delete the VRF if it has interfaces configured in it. */ |
245 | if (!RB_EMPTY(if_name_head, &vrf->ifaces_by_name)) | |
246 | return; | |
247 | ||
d62a17ae | 248 | if (vrf_master.vrf_delete_hook) |
249 | (*vrf_master.vrf_delete_hook)(vrf); | |
216b18ef | 250 | |
d62a17ae | 251 | QOBJ_UNREG(vrf); |
b72ede27 | 252 | |
d62a17ae | 253 | if (vrf->name[0] != '\0') |
254 | RB_REMOVE(vrf_name_head, &vrfs_by_name, vrf); | |
b72ede27 | 255 | |
d62a17ae | 256 | XFREE(MTYPE_VRF, vrf); |
b72ede27 FL |
257 | } |
258 | ||
259 | /* Look up a VRF by identifier. */ | |
d62a17ae | 260 | struct vrf *vrf_lookup_by_id(vrf_id_t vrf_id) |
b72ede27 | 261 | { |
d62a17ae | 262 | struct vrf vrf; |
263 | vrf.vrf_id = vrf_id; | |
264 | return (RB_FIND(vrf_id_head, &vrfs_by_id, &vrf)); | |
b72ede27 FL |
265 | } |
266 | ||
e5bf3e1e FL |
267 | /* |
268 | * Enable a VRF - that is, let the VRF be ready to use. | |
269 | * The VRF_ENABLE_HOOK callback will be called to inform | |
270 | * that they can allocate resources in this VRF. | |
271 | * | |
272 | * RETURN: 1 - enabled successfully; otherwise, 0. | |
273 | */ | |
d62a17ae | 274 | int vrf_enable(struct vrf *vrf) |
e5bf3e1e | 275 | { |
d62a17ae | 276 | if (vrf_is_enabled(vrf)) |
277 | return 1; | |
05e8e11e | 278 | |
d62a17ae | 279 | if (debug_vrf) |
c7384cf8 | 280 | zlog_debug("VRF %s(%u) is enabled.", vrf->name, vrf->vrf_id); |
e5bf3e1e | 281 | |
d62a17ae | 282 | SET_FLAG(vrf->status, VRF_ACTIVE); |
e5bf3e1e | 283 | |
d62a17ae | 284 | if (vrf_master.vrf_enable_hook) |
285 | (*vrf_master.vrf_enable_hook)(vrf); | |
e5bf3e1e | 286 | |
98cbbaea DS |
287 | /* |
288 | * If we have any nexthop group entries that | |
289 | * are awaiting vrf initialization then | |
290 | * let's let people know about it | |
291 | */ | |
292 | nexthop_group_enable_vrf(vrf); | |
293 | ||
d62a17ae | 294 | return 1; |
e5bf3e1e FL |
295 | } |
296 | ||
297 | /* | |
298 | * Disable a VRF - that is, let the VRF be unusable. | |
299 | * The VRF_DELETE_HOOK callback will be called to inform | |
300 | * that they must release the resources in the VRF. | |
301 | */ | |
697d3ec7 | 302 | void vrf_disable(struct vrf *vrf) |
e5bf3e1e | 303 | { |
d62a17ae | 304 | if (!vrf_is_enabled(vrf)) |
305 | return; | |
a647bfa8 | 306 | |
d62a17ae | 307 | UNSET_FLAG(vrf->status, VRF_ACTIVE); |
e5bf3e1e | 308 | |
d62a17ae | 309 | if (debug_vrf) |
c7384cf8 DS |
310 | zlog_debug("VRF %s(%u) is to be disabled.", vrf->name, |
311 | vrf->vrf_id); | |
e5bf3e1e | 312 | |
d62a17ae | 313 | /* Till now, nothing to be done for the default VRF. */ |
314 | // Pending: see why this statement. | |
e74f14fc | 315 | |
0cbee799 DS |
316 | |
317 | /* | |
318 | * When the vrf is disabled let's | |
319 | * handle all nexthop-groups associated | |
320 | * with this vrf | |
321 | */ | |
322 | nexthop_group_disable_vrf(vrf); | |
323 | ||
d62a17ae | 324 | if (vrf_master.vrf_disable_hook) |
325 | (*vrf_master.vrf_disable_hook)(vrf); | |
e5bf3e1e FL |
326 | } |
327 | ||
b7cfce93 MK |
328 | const char *vrf_id_to_name(vrf_id_t vrf_id) |
329 | { | |
330 | struct vrf *vrf; | |
331 | ||
7c1119cb CG |
332 | if (vrf_id == VRF_DEFAULT) |
333 | return VRF_DEFAULT_NAME; | |
334 | ||
b7cfce93 | 335 | vrf = vrf_lookup_by_id(vrf_id); |
bd47f3a3 | 336 | return VRF_LOGNAME(vrf); |
b7cfce93 MK |
337 | } |
338 | ||
b72ede27 | 339 | /* Look up the data pointer of the specified VRF. */ |
d62a17ae | 340 | void *vrf_info_lookup(vrf_id_t vrf_id) |
b72ede27 | 341 | { |
d62a17ae | 342 | struct vrf *vrf = vrf_lookup_by_id(vrf_id); |
343 | return vrf ? vrf->info : NULL; | |
b72ede27 FL |
344 | } |
345 | ||
7076bb2f | 346 | /* |
4a8bf858 | 347 | * VRF hash for storing set or not. |
7076bb2f | 348 | */ |
4a8bf858 DS |
349 | struct vrf_bit_set { |
350 | vrf_id_t vrf_id; | |
351 | bool set; | |
352 | }; | |
7076bb2f | 353 | |
d8b87afe | 354 | static unsigned int vrf_hash_bitmap_key(const void *data) |
4a8bf858 | 355 | { |
d8b87afe | 356 | const struct vrf_bit_set *bit = data; |
d62a17ae | 357 | |
4a8bf858 DS |
358 | return bit->vrf_id; |
359 | } | |
d62a17ae | 360 | |
74df8d6d | 361 | static bool vrf_hash_bitmap_cmp(const void *a, const void *b) |
4a8bf858 DS |
362 | { |
363 | const struct vrf_bit_set *bit1 = a; | |
364 | const struct vrf_bit_set *bit2 = b; | |
d62a17ae | 365 | |
4a8bf858 DS |
366 | return bit1->vrf_id == bit2->vrf_id; |
367 | } | |
368 | ||
369 | static void *vrf_hash_bitmap_alloc(void *data) | |
370 | { | |
371 | struct vrf_bit_set *copy = data; | |
372 | struct vrf_bit_set *bit; | |
373 | ||
374 | bit = XMALLOC(MTYPE_VRF_BITMAP, sizeof(*bit)); | |
375 | bit->vrf_id = copy->vrf_id; | |
376 | ||
377 | return bit; | |
378 | } | |
379 | ||
380 | static void vrf_hash_bitmap_free(void *data) | |
381 | { | |
382 | struct vrf_bit_set *bit = data; | |
383 | ||
384 | XFREE(MTYPE_VRF_BITMAP, bit); | |
385 | } | |
7076bb2f | 386 | |
d62a17ae | 387 | vrf_bitmap_t vrf_bitmap_init(void) |
7076bb2f | 388 | { |
4a8bf858 DS |
389 | return hash_create_size(32, vrf_hash_bitmap_key, vrf_hash_bitmap_cmp, |
390 | "VRF BIT HASH"); | |
7076bb2f FL |
391 | } |
392 | ||
d62a17ae | 393 | void vrf_bitmap_free(vrf_bitmap_t bmap) |
7076bb2f | 394 | { |
4a8bf858 | 395 | struct hash *vrf_hash = bmap; |
7076bb2f | 396 | |
4a8bf858 | 397 | if (vrf_hash == NULL) |
d62a17ae | 398 | return; |
7076bb2f | 399 | |
4a8bf858 DS |
400 | hash_clean(vrf_hash, vrf_hash_bitmap_free); |
401 | hash_free(vrf_hash); | |
7076bb2f FL |
402 | } |
403 | ||
d62a17ae | 404 | void vrf_bitmap_set(vrf_bitmap_t bmap, vrf_id_t vrf_id) |
7076bb2f | 405 | { |
4a8bf858 DS |
406 | struct vrf_bit_set lookup = { .vrf_id = vrf_id }; |
407 | struct hash *vrf_hash = bmap; | |
408 | struct vrf_bit_set *bit; | |
7076bb2f | 409 | |
4a8bf858 | 410 | if (vrf_hash == NULL || vrf_id == VRF_UNKNOWN) |
d62a17ae | 411 | return; |
7076bb2f | 412 | |
4a8bf858 DS |
413 | bit = hash_get(vrf_hash, &lookup, vrf_hash_bitmap_alloc); |
414 | bit->set = true; | |
7076bb2f FL |
415 | } |
416 | ||
d62a17ae | 417 | void vrf_bitmap_unset(vrf_bitmap_t bmap, vrf_id_t vrf_id) |
7076bb2f | 418 | { |
4a8bf858 DS |
419 | struct vrf_bit_set lookup = { .vrf_id = vrf_id }; |
420 | struct hash *vrf_hash = bmap; | |
421 | struct vrf_bit_set *bit; | |
7076bb2f | 422 | |
4a8bf858 | 423 | if (vrf_hash == NULL || vrf_id == VRF_UNKNOWN) |
d62a17ae | 424 | return; |
7076bb2f | 425 | |
4a8bf858 DS |
426 | bit = hash_get(vrf_hash, &lookup, vrf_hash_bitmap_alloc); |
427 | bit->set = false; | |
7076bb2f FL |
428 | } |
429 | ||
d62a17ae | 430 | int vrf_bitmap_check(vrf_bitmap_t bmap, vrf_id_t vrf_id) |
7076bb2f | 431 | { |
4a8bf858 DS |
432 | struct vrf_bit_set lookup = { .vrf_id = vrf_id }; |
433 | struct hash *vrf_hash = bmap; | |
434 | struct vrf_bit_set *bit; | |
7076bb2f | 435 | |
4a8bf858 | 436 | if (vrf_hash == NULL || vrf_id == VRF_UNKNOWN) |
d62a17ae | 437 | return 0; |
7076bb2f | 438 | |
4a8bf858 DS |
439 | bit = hash_lookup(vrf_hash, &lookup); |
440 | if (bit) | |
441 | return bit->set; | |
442 | ||
443 | return 0; | |
7076bb2f FL |
444 | } |
445 | ||
d62a17ae | 446 | static void vrf_autocomplete(vector comps, struct cmd_token *token) |
d617d5fe | 447 | { |
d62a17ae | 448 | struct vrf *vrf = NULL; |
d617d5fe | 449 | |
723001fc PG |
450 | RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) |
451 | vector_set(comps, XSTRDUP(MTYPE_COMPLETION, vrf->name)); | |
d617d5fe DS |
452 | } |
453 | ||
454 | static const struct cmd_variable_handler vrf_var_handlers[] = { | |
d62a17ae | 455 | { |
456 | .varname = "vrf", | |
457 | .completions = vrf_autocomplete, | |
458 | }, | |
e429a2a0 IR |
459 | { |
460 | .varname = "vrf_name", | |
461 | .completions = vrf_autocomplete, | |
462 | }, | |
463 | { | |
464 | .varname = "nexthop_vrf", | |
465 | .completions = vrf_autocomplete, | |
466 | }, | |
d62a17ae | 467 | {.completions = NULL}, |
d617d5fe DS |
468 | }; |
469 | ||
b72ede27 | 470 | /* Initialize VRF module. */ |
d62a17ae | 471 | void vrf_init(int (*create)(struct vrf *), int (*enable)(struct vrf *), |
ac2cb9bf | 472 | int (*disable)(struct vrf *), int (*destroy)(struct vrf *)) |
d62a17ae | 473 | { |
474 | struct vrf *default_vrf; | |
475 | ||
e26aedbe PG |
476 | /* initialise NS, in case VRF backend if NETNS */ |
477 | ns_init(); | |
d62a17ae | 478 | if (debug_vrf) |
15569c58 | 479 | zlog_debug("%s: Initializing VRF subsystem", __func__); |
d62a17ae | 480 | |
481 | vrf_master.vrf_new_hook = create; | |
482 | vrf_master.vrf_enable_hook = enable; | |
483 | vrf_master.vrf_disable_hook = disable; | |
d01b92fd | 484 | vrf_master.vrf_delete_hook = destroy; |
d62a17ae | 485 | |
486 | /* The default VRF always exists. */ | |
eb6934d5 | 487 | default_vrf = vrf_get(VRF_DEFAULT, VRF_DEFAULT_NAME); |
d62a17ae | 488 | if (!default_vrf) { |
450971aa | 489 | flog_err(EC_LIB_VRF_START, |
1c50c1c0 | 490 | "vrf_init: failed to create the default VRF!"); |
d62a17ae | 491 | exit(1); |
492 | } | |
20c87e98 TC |
493 | if (vrf_is_backend_netns()) { |
494 | struct ns *ns; | |
495 | ||
fdafe17e | 496 | strlcpy(default_vrf->data.l.netns_name, |
eb6934d5 | 497 | VRF_DEFAULT_NAME, NS_NAMSIZ); |
1eb92f06 | 498 | ns = ns_lookup(NS_DEFAULT); |
c3568c4d TC |
499 | ns->vrf_ctxt = default_vrf; |
500 | default_vrf->ns_ctxt = ns; | |
20c87e98 | 501 | } |
d62a17ae | 502 | |
503 | /* Enable the default VRF. */ | |
504 | if (!vrf_enable(default_vrf)) { | |
450971aa | 505 | flog_err(EC_LIB_VRF_START, |
1c50c1c0 | 506 | "vrf_init: failed to enable the default VRF!"); |
d62a17ae | 507 | exit(1); |
508 | } | |
509 | ||
510 | cmd_variable_handler_register(vrf_var_handlers); | |
b72ede27 FL |
511 | } |
512 | ||
ea0d70b1 SW |
513 | static void vrf_terminate_single(struct vrf *vrf) |
514 | { | |
515 | /* Clear configured flag and invoke delete. */ | |
b14fa9c4 | 516 | vrf_disable(vrf); |
ea0d70b1 | 517 | UNSET_FLAG(vrf->status, VRF_CONFIGURED); |
f60a1188 | 518 | if_terminate(vrf); |
ce27a13e | 519 | vrf_delete(vrf); |
ea0d70b1 SW |
520 | } |
521 | ||
b72ede27 | 522 | /* Terminate VRF module. */ |
d62a17ae | 523 | void vrf_terminate(void) |
b72ede27 | 524 | { |
ea0d70b1 | 525 | struct vrf *vrf, *tmp; |
b72ede27 | 526 | |
d62a17ae | 527 | if (debug_vrf) |
15569c58 | 528 | zlog_debug("%s: Shutting down vrf subsystem", __func__); |
19dc275e | 529 | |
ea0d70b1 SW |
530 | RB_FOREACH_SAFE (vrf, vrf_id_head, &vrfs_by_id, tmp) { |
531 | if (vrf->vrf_id == VRF_DEFAULT) | |
532 | continue; | |
55cd0f61 | 533 | |
ea0d70b1 | 534 | vrf_terminate_single(vrf); |
65c3a7c4 | 535 | } |
55cd0f61 | 536 | |
ea0d70b1 SW |
537 | RB_FOREACH_SAFE (vrf, vrf_name_head, &vrfs_by_name, tmp) { |
538 | if (vrf->vrf_id == VRF_DEFAULT) | |
539 | continue; | |
55cd0f61 | 540 | |
ea0d70b1 | 541 | vrf_terminate_single(vrf); |
65c3a7c4 | 542 | } |
ea0d70b1 SW |
543 | |
544 | /* Finally terminate default VRF */ | |
545 | vrf = vrf_lookup_by_id(VRF_DEFAULT); | |
b8984d4e DL |
546 | if (vrf) |
547 | vrf_terminate_single(vrf); | |
b72ede27 FL |
548 | } |
549 | ||
0f4977c6 | 550 | int vrf_socket(int domain, int type, int protocol, vrf_id_t vrf_id, |
02fe07c7 | 551 | const char *interfacename) |
e5bf3e1e | 552 | { |
2e0d2b3d | 553 | int ret, save_errno, ret2; |
e5bf3e1e | 554 | |
2e0d2b3d PG |
555 | ret = vrf_switch_to_netns(vrf_id); |
556 | if (ret < 0) | |
450971aa | 557 | flog_err_sys(EC_LIB_SOCKET, "%s: Can't switch to VRF %u (%s)", |
09c866e3 | 558 | __func__, vrf_id, safe_strerror(errno)); |
b66d022e | 559 | |
d62a17ae | 560 | ret = socket(domain, type, protocol); |
2e0d2b3d PG |
561 | save_errno = errno; |
562 | ret2 = vrf_switchback_to_initial(); | |
563 | if (ret2 < 0) | |
450971aa | 564 | flog_err_sys(EC_LIB_SOCKET, |
09c866e3 QY |
565 | "%s: Can't switchback from VRF %u (%s)", __func__, |
566 | vrf_id, safe_strerror(errno)); | |
2e0d2b3d | 567 | errno = save_errno; |
0f4977c6 PG |
568 | if (ret <= 0) |
569 | return ret; | |
570 | ret2 = vrf_bind(vrf_id, ret, interfacename); | |
571 | if (ret2 < 0) { | |
572 | close(ret); | |
573 | ret = ret2; | |
574 | } | |
d62a17ae | 575 | return ret; |
e5bf3e1e FL |
576 | } |
577 | ||
78dd30b2 PG |
578 | int vrf_is_backend_netns(void) |
579 | { | |
580 | return (vrf_backend == VRF_BACKEND_NETNS); | |
581 | } | |
582 | ||
583 | int vrf_get_backend(void) | |
584 | { | |
72261ecd PG |
585 | if (!vrf_backend_configured) |
586 | return VRF_BACKEND_UNKNOWN; | |
78dd30b2 PG |
587 | return vrf_backend; |
588 | } | |
589 | ||
7239d3d9 | 590 | int vrf_configure_backend(enum vrf_backend_type backend) |
78dd30b2 | 591 | { |
f7d45925 QY |
592 | /* Work around issue in old gcc */ |
593 | switch (backend) { | |
594 | case VRF_BACKEND_UNKNOWN: | |
595 | case VRF_BACKEND_NETNS: | |
596 | case VRF_BACKEND_VRF_LITE: | |
597 | break; | |
bde30e78 | 598 | case VRF_BACKEND_MAX: |
7239d3d9 | 599 | return -1; |
f7d45925 | 600 | } |
7239d3d9 QY |
601 | |
602 | vrf_backend = backend; | |
72261ecd | 603 | vrf_backend_configured = 1; |
7239d3d9 QY |
604 | |
605 | return 0; | |
78dd30b2 PG |
606 | } |
607 | ||
697d3ec7 | 608 | /* vrf CLI commands */ |
16d6ea59 QY |
609 | DEFUN_NOSH(vrf_exit, |
610 | vrf_exit_cmd, | |
611 | "exit-vrf", | |
612 | "Exit current mode and down to previous mode\n") | |
613 | { | |
799a81df | 614 | cmd_exit(vty); |
16d6ea59 QY |
615 | return CMD_SUCCESS; |
616 | } | |
617 | ||
ca77b518 | 618 | DEFUN_YANG_NOSH (vrf, |
697d3ec7 PG |
619 | vrf_cmd, |
620 | "vrf NAME", | |
621 | "Select a VRF to configure\n" | |
622 | "VRF's name\n") | |
623 | { | |
624 | int idx_name = 1; | |
625 | const char *vrfname = argv[idx_name]->arg; | |
62d89c64 IR |
626 | char xpath_list[XPATH_MAXLEN]; |
627 | struct vrf *vrf; | |
628 | int ret; | |
697d3ec7 | 629 | |
62d89c64 IR |
630 | if (strlen(vrfname) > VRF_NAMSIZ) { |
631 | vty_out(vty, | |
632 | "%% VRF name %s invalid: length exceeds %d bytes\n", | |
633 | vrfname, VRF_NAMSIZ); | |
634 | return CMD_WARNING_CONFIG_FAILED; | |
635 | } | |
636 | ||
637 | snprintf(xpath_list, sizeof(xpath_list), FRR_VRF_KEY_XPATH, vrfname); | |
638 | ||
639 | nb_cli_enqueue_change(vty, xpath_list, NB_OP_CREATE, NULL); | |
ae08de9f | 640 | ret = nb_cli_apply_changes_clear_pending(vty, "%s", xpath_list); |
62d89c64 IR |
641 | if (ret == CMD_SUCCESS) { |
642 | VTY_PUSH_XPATH(VRF_NODE, xpath_list); | |
643 | vrf = vrf_lookup_by_name(vrfname); | |
644 | if (vrf) | |
645 | VTY_PUSH_CONTEXT(VRF_NODE, vrf); | |
646 | } | |
647 | ||
648 | return ret; | |
697d3ec7 PG |
649 | } |
650 | ||
ca77b518 | 651 | DEFUN_YANG (no_vrf, |
34c46274 RW |
652 | no_vrf_cmd, |
653 | "no vrf NAME", | |
654 | NO_STR | |
655 | "Delete a pseudo VRF's configuration\n" | |
656 | "VRF's name\n") | |
f30c50b9 | 657 | { |
d62a17ae | 658 | const char *vrfname = argv[2]->arg; |
8b4cb7a6 | 659 | char xpath_list[XPATH_MAXLEN]; |
53dc2b05 | 660 | |
d62a17ae | 661 | struct vrf *vrfp; |
f30c50b9 | 662 | |
d62a17ae | 663 | vrfp = vrf_lookup_by_name(vrfname); |
f30c50b9 | 664 | |
cd980d03 IR |
665 | if (vrfp == NULL) |
666 | return CMD_SUCCESS; | |
f30c50b9 | 667 | |
d62a17ae | 668 | if (CHECK_FLAG(vrfp->status, VRF_ACTIVE)) { |
669 | vty_out(vty, "%% Only inactive VRFs can be deleted\n"); | |
670 | return CMD_WARNING_CONFIG_FAILED; | |
671 | } | |
f30c50b9 | 672 | |
f5eef2d5 IR |
673 | if (vrf_get_backend() == VRF_BACKEND_VRF_LITE) { |
674 | /* | |
574445ec | 675 | * Remove the VRF interface config when removing the VRF. |
f5eef2d5 IR |
676 | */ |
677 | snprintf(xpath_list, sizeof(xpath_list), | |
574445ec | 678 | "/frr-interface:lib/interface[name='%s']", vrfname); |
f5eef2d5 IR |
679 | nb_cli_enqueue_change(vty, xpath_list, NB_OP_DESTROY, NULL); |
680 | } | |
681 | ||
09b150ef | 682 | snprintf(xpath_list, sizeof(xpath_list), FRR_VRF_KEY_XPATH, vrfname); |
f30c50b9 | 683 | |
8b4cb7a6 | 684 | nb_cli_enqueue_change(vty, xpath_list, NB_OP_DESTROY, NULL); |
f5eef2d5 | 685 | return nb_cli_apply_changes(vty, NULL); |
f30c50b9 RW |
686 | } |
687 | ||
53dc2b05 | 688 | |
62b346ee | 689 | static struct cmd_node vrf_node = { |
f4b8291f | 690 | .name = "vrf", |
62b346ee | 691 | .node = VRF_NODE, |
24389580 | 692 | .parent_node = CONFIG_NODE, |
62b346ee | 693 | .prompt = "%s(config-vrf)# ", |
62b346ee | 694 | }; |
7ddcfca4 | 695 | |
19dc275e DS |
696 | /* |
697 | * Debug CLI for vrf's | |
698 | */ | |
699 | DEFUN (vrf_debug, | |
700 | vrf_debug_cmd, | |
701 | "debug vrf", | |
702 | DEBUG_STR | |
703 | "VRF Debugging\n") | |
704 | { | |
d62a17ae | 705 | debug_vrf = 1; |
19dc275e | 706 | |
d62a17ae | 707 | return CMD_SUCCESS; |
19dc275e DS |
708 | } |
709 | ||
710 | DEFUN (no_vrf_debug, | |
711 | no_vrf_debug_cmd, | |
712 | "no debug vrf", | |
713 | NO_STR | |
714 | DEBUG_STR | |
715 | "VRF Debugging\n") | |
716 | { | |
d62a17ae | 717 | debug_vrf = 0; |
19dc275e | 718 | |
d62a17ae | 719 | return CMD_SUCCESS; |
19dc275e DS |
720 | } |
721 | ||
d62a17ae | 722 | static int vrf_write_host(struct vty *vty) |
19dc275e | 723 | { |
d62a17ae | 724 | if (debug_vrf) |
725 | vty_out(vty, "debug vrf\n"); | |
19dc275e | 726 | |
d62a17ae | 727 | return 1; |
19dc275e DS |
728 | } |
729 | ||
612c2c15 | 730 | static int vrf_write_host(struct vty *vty); |
62b346ee | 731 | static struct cmd_node vrf_debug_node = { |
f4b8291f | 732 | .name = "vrf debug", |
62b346ee DL |
733 | .node = VRF_DEBUG_NODE, |
734 | .prompt = "", | |
612c2c15 | 735 | .config_write = vrf_write_host, |
62b346ee | 736 | }; |
19dc275e | 737 | |
d62a17ae | 738 | void vrf_install_commands(void) |
19dc275e | 739 | { |
612c2c15 | 740 | install_node(&vrf_debug_node); |
19dc275e | 741 | |
d62a17ae | 742 | install_element(CONFIG_NODE, &vrf_debug_cmd); |
743 | install_element(ENABLE_NODE, &vrf_debug_cmd); | |
744 | install_element(CONFIG_NODE, &no_vrf_debug_cmd); | |
745 | install_element(ENABLE_NODE, &no_vrf_debug_cmd); | |
19dc275e | 746 | } |
53dc2b05 | 747 | |
cfc369c4 | 748 | void vrf_cmd_init(int (*writefunc)(struct vty *vty)) |
7ddcfca4 | 749 | { |
d62a17ae | 750 | install_element(CONFIG_NODE, &vrf_cmd); |
751 | install_element(CONFIG_NODE, &no_vrf_cmd); | |
612c2c15 DL |
752 | vrf_node.config_write = writefunc; |
753 | install_node(&vrf_node); | |
d62a17ae | 754 | install_default(VRF_NODE); |
16d6ea59 | 755 | install_element(VRF_NODE, &vrf_exit_cmd); |
19dc275e | 756 | } |
ec31f30d | 757 | |
ac2cb9bf | 758 | void vrf_set_default_name(const char *default_name) |
ec31f30d | 759 | { |
c200f5e1 | 760 | snprintf(vrf_default_name, VRF_NAMSIZ, "%s", default_name); |
c200f5e1 PG |
761 | } |
762 | ||
763 | const char *vrf_get_default_name(void) | |
764 | { | |
765 | return vrf_default_name; | |
766 | } | |
767 | ||
36eef858 | 768 | int vrf_bind(vrf_id_t vrf_id, int fd, const char *ifname) |
0f4977c6 PG |
769 | { |
770 | int ret = 0; | |
91f854f6 | 771 | struct interface *ifp; |
36eef858 IR |
772 | struct vrf *vrf; |
773 | ||
774 | if (fd < 0) | |
775 | return -1; | |
776 | ||
777 | if (vrf_id == VRF_UNKNOWN) | |
778 | return -1; | |
779 | ||
780 | /* can't bind to a VRF that doesn't exist */ | |
781 | vrf = vrf_lookup_by_id(vrf_id); | |
782 | if (!vrf_is_enabled(vrf)) | |
783 | return -1; | |
784 | ||
785 | if (ifname && strcmp(ifname, vrf->name)) { | |
786 | /* binding to a regular interface */ | |
787 | ||
788 | /* can't bind to an interface that doesn't exist */ | |
789 | ifp = if_lookup_by_name(ifname, vrf_id); | |
790 | if (!ifp) | |
791 | return -1; | |
792 | } else { | |
793 | /* binding to a VRF device */ | |
794 | ||
795 | /* nothing to do for netns */ | |
796 | if (vrf_is_backend_netns()) | |
797 | return 0; | |
798 | ||
799 | /* nothing to do for default vrf */ | |
800 | if (vrf_id == VRF_DEFAULT) | |
801 | return 0; | |
802 | ||
803 | ifname = vrf->name; | |
804 | } | |
0f4977c6 | 805 | |
0f4977c6 | 806 | #ifdef SO_BINDTODEVICE |
36eef858 IR |
807 | ret = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, ifname, |
808 | strlen(ifname) + 1); | |
0f4977c6 | 809 | if (ret < 0) |
36eef858 IR |
810 | zlog_err("bind to interface %s failed, errno=%d", ifname, |
811 | errno); | |
0f4977c6 PG |
812 | #endif /* SO_BINDTODEVICE */ |
813 | return ret; | |
814 | } | |
2e0d2b3d | 815 | int vrf_getaddrinfo(const char *node, const char *service, |
996c9314 LB |
816 | const struct addrinfo *hints, struct addrinfo **res, |
817 | vrf_id_t vrf_id) | |
2e0d2b3d PG |
818 | { |
819 | int ret, ret2, save_errno; | |
820 | ||
821 | ret = vrf_switch_to_netns(vrf_id); | |
822 | if (ret < 0) | |
450971aa | 823 | flog_err_sys(EC_LIB_SOCKET, "%s: Can't switch to VRF %u (%s)", |
09c866e3 | 824 | __func__, vrf_id, safe_strerror(errno)); |
2e0d2b3d PG |
825 | ret = getaddrinfo(node, service, hints, res); |
826 | save_errno = errno; | |
827 | ret2 = vrf_switchback_to_initial(); | |
828 | if (ret2 < 0) | |
450971aa | 829 | flog_err_sys(EC_LIB_SOCKET, |
09c866e3 QY |
830 | "%s: Can't switchback from VRF %u (%s)", __func__, |
831 | vrf_id, safe_strerror(errno)); | |
2e0d2b3d PG |
832 | errno = save_errno; |
833 | return ret; | |
834 | } | |
835 | ||
516d7591 PG |
836 | int vrf_ioctl(vrf_id_t vrf_id, int d, unsigned long request, char *params) |
837 | { | |
838 | int ret, saved_errno, rc; | |
839 | ||
840 | ret = vrf_switch_to_netns(vrf_id); | |
841 | if (ret < 0) { | |
450971aa | 842 | flog_err_sys(EC_LIB_SOCKET, "%s: Can't switch to VRF %u (%s)", |
09c866e3 | 843 | __func__, vrf_id, safe_strerror(errno)); |
516d7591 PG |
844 | return 0; |
845 | } | |
846 | rc = ioctl(d, request, params); | |
847 | saved_errno = errno; | |
848 | ret = vrf_switchback_to_initial(); | |
849 | if (ret < 0) | |
450971aa | 850 | flog_err_sys(EC_LIB_SOCKET, |
09c866e3 QY |
851 | "%s: Can't switchback from VRF %u (%s)", __func__, |
852 | vrf_id, safe_strerror(errno)); | |
516d7591 PG |
853 | errno = saved_errno; |
854 | return rc; | |
855 | } | |
856 | ||
0f4977c6 | 857 | int vrf_sockunion_socket(const union sockunion *su, vrf_id_t vrf_id, |
02fe07c7 | 858 | const char *interfacename) |
2e0d2b3d PG |
859 | { |
860 | int ret, save_errno, ret2; | |
861 | ||
862 | ret = vrf_switch_to_netns(vrf_id); | |
863 | if (ret < 0) | |
450971aa | 864 | flog_err_sys(EC_LIB_SOCKET, "%s: Can't switch to VRF %u (%s)", |
09c866e3 | 865 | __func__, vrf_id, safe_strerror(errno)); |
2e0d2b3d PG |
866 | ret = sockunion_socket(su); |
867 | save_errno = errno; | |
868 | ret2 = vrf_switchback_to_initial(); | |
869 | if (ret2 < 0) | |
450971aa | 870 | flog_err_sys(EC_LIB_SOCKET, |
09c866e3 QY |
871 | "%s: Can't switchback from VRF %u (%s)", __func__, |
872 | vrf_id, safe_strerror(errno)); | |
2e0d2b3d | 873 | errno = save_errno; |
0f4977c6 PG |
874 | |
875 | if (ret <= 0) | |
876 | return ret; | |
877 | ret2 = vrf_bind(vrf_id, ret, interfacename); | |
878 | if (ret2 < 0) { | |
879 | close(ret); | |
880 | ret = ret2; | |
881 | } | |
2e0d2b3d PG |
882 | return ret; |
883 | } | |
0b014ea6 | 884 | |
bc867a5d CS |
885 | /* ------- Northbound callbacks ------- */ |
886 | ||
887 | /* | |
888 | * XPath: /frr-vrf:lib/vrf | |
889 | */ | |
60ee8be1 | 890 | static int lib_vrf_create(struct nb_cb_create_args *args) |
bc867a5d CS |
891 | { |
892 | const char *vrfname; | |
893 | struct vrf *vrfp; | |
894 | ||
60ee8be1 | 895 | vrfname = yang_dnode_get_string(args->dnode, "./name"); |
bc867a5d | 896 | |
60ee8be1 | 897 | if (args->event != NB_EV_APPLY) |
bc867a5d CS |
898 | return NB_OK; |
899 | ||
900 | vrfp = vrf_get(VRF_UNKNOWN, vrfname); | |
901 | ||
b9b794db | 902 | SET_FLAG(vrfp->status, VRF_CONFIGURED); |
60ee8be1 | 903 | nb_running_set_entry(args->dnode, vrfp); |
bc867a5d CS |
904 | |
905 | return NB_OK; | |
906 | } | |
907 | ||
60ee8be1 | 908 | static int lib_vrf_destroy(struct nb_cb_destroy_args *args) |
bc867a5d CS |
909 | { |
910 | struct vrf *vrfp; | |
911 | ||
60ee8be1 | 912 | switch (args->event) { |
bc867a5d | 913 | case NB_EV_VALIDATE: |
60ee8be1 | 914 | vrfp = nb_running_get_entry(args->dnode, NULL, true); |
bc867a5d | 915 | if (CHECK_FLAG(vrfp->status, VRF_ACTIVE)) { |
10bdc68f RW |
916 | snprintf(args->errmsg, args->errmsg_len, |
917 | "Only inactive VRFs can be deleted"); | |
bc867a5d CS |
918 | return NB_ERR_VALIDATION; |
919 | } | |
920 | break; | |
921 | case NB_EV_PREPARE: | |
922 | case NB_EV_ABORT: | |
923 | break; | |
924 | case NB_EV_APPLY: | |
60ee8be1 | 925 | vrfp = nb_running_unset_entry(args->dnode); |
8b4cb7a6 | 926 | |
bc867a5d | 927 | /* Clear configured flag and invoke delete. */ |
b9b794db | 928 | UNSET_FLAG(vrfp->status, VRF_CONFIGURED); |
bc867a5d CS |
929 | vrf_delete(vrfp); |
930 | break; | |
931 | } | |
932 | ||
933 | return NB_OK; | |
934 | } | |
935 | ||
60ee8be1 | 936 | static const void *lib_vrf_get_next(struct nb_cb_get_next_args *args) |
bc867a5d | 937 | { |
60ee8be1 | 938 | struct vrf *vrfp = (struct vrf *)args->list_entry; |
bc867a5d | 939 | |
60ee8be1 | 940 | if (args->list_entry == NULL) { |
bc867a5d CS |
941 | vrfp = RB_MIN(vrf_name_head, &vrfs_by_name); |
942 | } else { | |
943 | vrfp = RB_NEXT(vrf_name_head, vrfp); | |
944 | } | |
945 | ||
946 | return vrfp; | |
947 | } | |
948 | ||
60ee8be1 | 949 | static int lib_vrf_get_keys(struct nb_cb_get_keys_args *args) |
bc867a5d | 950 | { |
60ee8be1 | 951 | struct vrf *vrfp = (struct vrf *)args->list_entry; |
bc867a5d | 952 | |
60ee8be1 RW |
953 | args->keys->num = 1; |
954 | strlcpy(args->keys->key[0], vrfp->name, sizeof(args->keys->key[0])); | |
bc867a5d CS |
955 | |
956 | return NB_OK; | |
957 | } | |
958 | ||
60ee8be1 | 959 | static const void *lib_vrf_lookup_entry(struct nb_cb_lookup_entry_args *args) |
bc867a5d | 960 | { |
60ee8be1 | 961 | const char *vrfname = args->keys->key[0]; |
bc867a5d CS |
962 | |
963 | struct vrf *vrf = vrf_lookup_by_name(vrfname); | |
964 | ||
965 | return vrf; | |
966 | } | |
967 | ||
968 | /* | |
969 | * XPath: /frr-vrf:lib/vrf/id | |
970 | */ | |
60ee8be1 RW |
971 | static struct yang_data * |
972 | lib_vrf_state_id_get_elem(struct nb_cb_get_elem_args *args) | |
bc867a5d | 973 | { |
60ee8be1 | 974 | struct vrf *vrfp = (struct vrf *)args->list_entry; |
bc867a5d | 975 | |
60ee8be1 | 976 | return yang_data_new_uint32(args->xpath, vrfp->vrf_id); |
bc867a5d CS |
977 | } |
978 | ||
979 | /* | |
980 | * XPath: /frr-vrf:lib/vrf/active | |
981 | */ | |
60ee8be1 RW |
982 | static struct yang_data * |
983 | lib_vrf_state_active_get_elem(struct nb_cb_get_elem_args *args) | |
bc867a5d | 984 | { |
60ee8be1 | 985 | struct vrf *vrfp = (struct vrf *)args->list_entry; |
bc867a5d CS |
986 | |
987 | if (vrfp->status == VRF_ACTIVE) | |
ff9232c8 | 988 | return yang_data_new_bool(args->xpath, true); |
bc867a5d CS |
989 | |
990 | return NULL; | |
991 | } | |
992 | ||
993 | /* clang-format off */ | |
994 | const struct frr_yang_module_info frr_vrf_info = { | |
995 | .name = "frr-vrf", | |
996 | .nodes = { | |
997 | { | |
998 | .xpath = "/frr-vrf:lib/vrf", | |
999 | .cbs = { | |
1000 | .create = lib_vrf_create, | |
1001 | .destroy = lib_vrf_destroy, | |
1002 | .get_next = lib_vrf_get_next, | |
1003 | .get_keys = lib_vrf_get_keys, | |
1004 | .lookup_entry = lib_vrf_lookup_entry, | |
3bb513c3 CH |
1005 | }, |
1006 | .priority = NB_DFLT_PRIORITY - 2, | |
bc867a5d CS |
1007 | }, |
1008 | { | |
1009 | .xpath = "/frr-vrf:lib/vrf/state/id", | |
1010 | .cbs = { | |
1011 | .get_elem = lib_vrf_state_id_get_elem, | |
1012 | } | |
1013 | }, | |
1014 | { | |
1015 | .xpath = "/frr-vrf:lib/vrf/state/active", | |
1016 | .cbs = { | |
1017 | .get_elem = lib_vrf_state_active_get_elem, | |
1018 | } | |
1019 | }, | |
1020 | { | |
1021 | .xpath = NULL, | |
1022 | }, | |
1023 | } | |
1024 | }; | |
1025 |