]> git.proxmox.com Git - mirror_frr.git/blob - lib/vrf.c
Merge pull request #1756 from qlyoung/stylechecker
[mirror_frr.git] / lib / vrf.c
1 /*
2 * VRF functions.
3 * Copyright (C) 2014 6WIND S.A.
4 *
5 * This file is part of GNU Zebra.
6 *
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 *
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
20 */
21
22 #include <zebra.h>
23
24 #include "if.h"
25 #include "vrf.h"
26 #include "vrf_int.h"
27 #include "prefix.h"
28 #include "table.h"
29 #include "log.h"
30 #include "memory.h"
31 #include "command.h"
32
33 DEFINE_MTYPE_STATIC(LIB, VRF, "VRF")
34 DEFINE_MTYPE_STATIC(LIB, VRF_BITMAP, "VRF bit-map")
35
36 DEFINE_QOBJ_TYPE(vrf)
37
38 static __inline int vrf_id_compare(const struct vrf *, const struct vrf *);
39 static __inline int vrf_name_compare(const struct vrf *, const struct vrf *);
40
41 RB_GENERATE(vrf_id_head, vrf, id_entry, vrf_id_compare);
42 RB_GENERATE(vrf_name_head, vrf, name_entry, vrf_name_compare);
43
44 struct vrf_id_head vrfs_by_id = RB_INITIALIZER(&vrfs_by_id);
45 struct vrf_name_head vrfs_by_name = RB_INITIALIZER(&vrfs_by_name);
46
47 /*
48 * Turn on/off debug code
49 * for vrf.
50 */
51 int debug_vrf = 0;
52
53 /* Holding VRF hooks */
54 struct vrf_master {
55 int (*vrf_new_hook)(struct vrf *);
56 int (*vrf_delete_hook)(struct vrf *);
57 int (*vrf_enable_hook)(struct vrf *);
58 int (*vrf_disable_hook)(struct vrf *);
59 } vrf_master = {
60 0,
61 };
62
63 static int vrf_is_enabled(struct vrf *vrf);
64 static void vrf_disable(struct vrf *vrf);
65
66 /* VRF list existance check by name. */
67 struct vrf *vrf_lookup_by_name(const char *name)
68 {
69 struct vrf vrf;
70 strlcpy(vrf.name, name, sizeof(vrf.name));
71 return (RB_FIND(vrf_name_head, &vrfs_by_name, &vrf));
72 }
73
74 static __inline int vrf_id_compare(const struct vrf *a, const struct vrf *b)
75 {
76 return (a->vrf_id - b->vrf_id);
77 }
78
79 static int vrf_name_compare(const struct vrf *a, const struct vrf *b)
80 {
81 return strcmp(a->name, b->name);
82 }
83
84 /* Get a VRF. If not found, create one.
85 * Arg:
86 * name - The name of the vrf. May be NULL if unknown.
87 * vrf_id - The vrf_id of the vrf. May be VRF_UNKNOWN if unknown
88 * Description: Please note that this routine can be called with just the name
89 * and 0 vrf-id
90 */
91 struct vrf *vrf_get(vrf_id_t vrf_id, const char *name)
92 {
93 struct vrf *vrf = NULL;
94 int new = 0;
95
96 if (debug_vrf)
97 zlog_debug("VRF_GET: %s(%u)", name, vrf_id);
98
99 /* Nothing to see, move along here */
100 if (!name && vrf_id == VRF_UNKNOWN)
101 return NULL;
102
103 /* Try to find VRF both by ID and name */
104 if (vrf_id != VRF_UNKNOWN)
105 vrf = vrf_lookup_by_id(vrf_id);
106 if (!vrf && name)
107 vrf = vrf_lookup_by_name(name);
108
109 if (vrf == NULL) {
110 vrf = XCALLOC(MTYPE_VRF, sizeof(struct vrf));
111 vrf->vrf_id = VRF_UNKNOWN;
112 RB_INIT(if_name_head, &vrf->ifaces_by_name);
113 RB_INIT(if_index_head, &vrf->ifaces_by_index);
114 QOBJ_REG(vrf, vrf);
115 new = 1;
116
117 if (debug_vrf)
118 zlog_debug("VRF(%u) %s is created.", vrf_id,
119 (name) ? name : "(NULL)");
120 }
121
122 /* Set identifier */
123 if (vrf_id != VRF_UNKNOWN && vrf->vrf_id == VRF_UNKNOWN) {
124 vrf->vrf_id = vrf_id;
125 RB_INSERT(vrf_id_head, &vrfs_by_id, vrf);
126 }
127
128 /* Set name */
129 if (name && vrf->name[0] != '\0' && strcmp(name, vrf->name)) {
130 RB_REMOVE(vrf_name_head, &vrfs_by_name, vrf);
131 strlcpy(vrf->name, name, sizeof(vrf->name));
132 RB_INSERT(vrf_name_head, &vrfs_by_name, vrf);
133 } else if (name && vrf->name[0] == '\0') {
134 strlcpy(vrf->name, name, sizeof(vrf->name));
135 RB_INSERT(vrf_name_head, &vrfs_by_name, vrf);
136 }
137
138 if (new &&vrf_master.vrf_new_hook)
139 (*vrf_master.vrf_new_hook)(vrf);
140
141 return vrf;
142 }
143
144 /* Delete a VRF. This is called when the underlying VRF goes away, a
145 * pre-configured VRF is deleted or when shutting down (vrf_terminate()).
146 */
147 void vrf_delete(struct vrf *vrf)
148 {
149 if (debug_vrf)
150 zlog_debug("VRF %u is to be deleted.", vrf->vrf_id);
151
152 if (vrf_is_enabled(vrf))
153 vrf_disable(vrf);
154
155 /* If the VRF is user configured, it'll stick around, just remove
156 * the ID mapping. Interfaces assigned to this VRF should've been
157 * removed already as part of the VRF going down.
158 */
159 if (vrf_is_user_cfged(vrf)) {
160 if (vrf->vrf_id != VRF_UNKNOWN) {
161 /* Delete any VRF interfaces - should be only
162 * the VRF itself, other interfaces should've
163 * been moved out of the VRF.
164 */
165 if_terminate(vrf);
166 RB_REMOVE(vrf_id_head, &vrfs_by_id, vrf);
167 vrf->vrf_id = VRF_UNKNOWN;
168 }
169 return;
170 }
171
172 if (vrf_master.vrf_delete_hook)
173 (*vrf_master.vrf_delete_hook)(vrf);
174
175 QOBJ_UNREG(vrf);
176 if_terminate(vrf);
177
178 if (vrf->vrf_id != VRF_UNKNOWN)
179 RB_REMOVE(vrf_id_head, &vrfs_by_id, vrf);
180 if (vrf->name[0] != '\0')
181 RB_REMOVE(vrf_name_head, &vrfs_by_name, vrf);
182
183 XFREE(MTYPE_VRF, vrf);
184 }
185
186 /* Look up a VRF by identifier. */
187 struct vrf *vrf_lookup_by_id(vrf_id_t vrf_id)
188 {
189 struct vrf vrf;
190 vrf.vrf_id = vrf_id;
191 return (RB_FIND(vrf_id_head, &vrfs_by_id, &vrf));
192 }
193
194 /*
195 * Enable a VRF - that is, let the VRF be ready to use.
196 * The VRF_ENABLE_HOOK callback will be called to inform
197 * that they can allocate resources in this VRF.
198 *
199 * RETURN: 1 - enabled successfully; otherwise, 0.
200 */
201 int vrf_enable(struct vrf *vrf)
202 {
203 if (vrf_is_enabled(vrf))
204 return 1;
205
206 if (debug_vrf)
207 zlog_debug("VRF %u is enabled.", vrf->vrf_id);
208
209 SET_FLAG(vrf->status, VRF_ACTIVE);
210
211 if (vrf_master.vrf_enable_hook)
212 (*vrf_master.vrf_enable_hook)(vrf);
213
214 return 1;
215 }
216
217 /*
218 * Disable a VRF - that is, let the VRF be unusable.
219 * The VRF_DELETE_HOOK callback will be called to inform
220 * that they must release the resources in the VRF.
221 */
222 static void vrf_disable(struct vrf *vrf)
223 {
224 if (!vrf_is_enabled(vrf))
225 return;
226
227 UNSET_FLAG(vrf->status, VRF_ACTIVE);
228
229 if (debug_vrf)
230 zlog_debug("VRF %u is to be disabled.", vrf->vrf_id);
231
232 /* Till now, nothing to be done for the default VRF. */
233 // Pending: see why this statement.
234
235 if (vrf_master.vrf_disable_hook)
236 (*vrf_master.vrf_disable_hook)(vrf);
237 }
238
239 const char *vrf_id_to_name(vrf_id_t vrf_id)
240 {
241 struct vrf *vrf;
242
243 vrf = vrf_lookup_by_id(vrf_id);
244 if (vrf)
245 return vrf->name;
246
247 return "n/a";
248 }
249
250 vrf_id_t vrf_name_to_id(const char *name)
251 {
252 struct vrf *vrf;
253 vrf_id_t vrf_id = VRF_DEFAULT; // Pending: need a way to return invalid
254 // id/ routine not used.
255
256 vrf = vrf_lookup_by_name(name);
257 if (vrf)
258 vrf_id = vrf->vrf_id;
259
260 return vrf_id;
261 }
262
263 /* Get the data pointer of the specified VRF. If not found, create one. */
264 void *vrf_info_get(vrf_id_t vrf_id)
265 {
266 struct vrf *vrf = vrf_get(vrf_id, NULL);
267 return vrf->info;
268 }
269
270 /* Look up the data pointer of the specified VRF. */
271 void *vrf_info_lookup(vrf_id_t vrf_id)
272 {
273 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
274 return vrf ? vrf->info : NULL;
275 }
276
277 /*
278 * VRF bit-map
279 */
280
281 #define VRF_BITMAP_NUM_OF_GROUPS 1024
282 #define VRF_BITMAP_NUM_OF_BITS_IN_GROUP (UINT32_MAX / VRF_BITMAP_NUM_OF_GROUPS)
283 #define VRF_BITMAP_NUM_OF_BYTES_IN_GROUP \
284 (VRF_BITMAP_NUM_OF_BITS_IN_GROUP / CHAR_BIT + 1) /* +1 for ensure */
285
286 #define VRF_BITMAP_GROUP(_id) ((_id) / VRF_BITMAP_NUM_OF_BITS_IN_GROUP)
287 #define VRF_BITMAP_BIT_OFFSET(_id) ((_id) % VRF_BITMAP_NUM_OF_BITS_IN_GROUP)
288
289 #define VRF_BITMAP_INDEX_IN_GROUP(_bit_offset) ((_bit_offset) / CHAR_BIT)
290 #define VRF_BITMAP_FLAG(_bit_offset) (((u_char)1) << ((_bit_offset) % CHAR_BIT))
291
292 struct vrf_bitmap {
293 u_char *groups[VRF_BITMAP_NUM_OF_GROUPS];
294 };
295
296 vrf_bitmap_t vrf_bitmap_init(void)
297 {
298 return (vrf_bitmap_t)XCALLOC(MTYPE_VRF_BITMAP,
299 sizeof(struct vrf_bitmap));
300 }
301
302 void vrf_bitmap_free(vrf_bitmap_t bmap)
303 {
304 struct vrf_bitmap *bm = (struct vrf_bitmap *)bmap;
305 int i;
306
307 if (bmap == VRF_BITMAP_NULL)
308 return;
309
310 for (i = 0; i < VRF_BITMAP_NUM_OF_GROUPS; i++)
311 if (bm->groups[i])
312 XFREE(MTYPE_VRF_BITMAP, bm->groups[i]);
313
314 XFREE(MTYPE_VRF_BITMAP, bm);
315 }
316
317 void vrf_bitmap_set(vrf_bitmap_t bmap, vrf_id_t vrf_id)
318 {
319 struct vrf_bitmap *bm = (struct vrf_bitmap *)bmap;
320 u_char group = VRF_BITMAP_GROUP(vrf_id);
321 u_char offset = VRF_BITMAP_BIT_OFFSET(vrf_id);
322
323 if (bmap == VRF_BITMAP_NULL || vrf_id == VRF_UNKNOWN)
324 return;
325
326 if (bm->groups[group] == NULL)
327 bm->groups[group] = XCALLOC(MTYPE_VRF_BITMAP,
328 VRF_BITMAP_NUM_OF_BYTES_IN_GROUP);
329
330 SET_FLAG(bm->groups[group][VRF_BITMAP_INDEX_IN_GROUP(offset)],
331 VRF_BITMAP_FLAG(offset));
332 }
333
334 void vrf_bitmap_unset(vrf_bitmap_t bmap, vrf_id_t vrf_id)
335 {
336 struct vrf_bitmap *bm = (struct vrf_bitmap *)bmap;
337 u_char group = VRF_BITMAP_GROUP(vrf_id);
338 u_char offset = VRF_BITMAP_BIT_OFFSET(vrf_id);
339
340 if (bmap == VRF_BITMAP_NULL || vrf_id == VRF_UNKNOWN
341 || bm->groups[group] == NULL)
342 return;
343
344 UNSET_FLAG(bm->groups[group][VRF_BITMAP_INDEX_IN_GROUP(offset)],
345 VRF_BITMAP_FLAG(offset));
346 }
347
348 int vrf_bitmap_check(vrf_bitmap_t bmap, vrf_id_t vrf_id)
349 {
350 struct vrf_bitmap *bm = (struct vrf_bitmap *)bmap;
351 u_char group = VRF_BITMAP_GROUP(vrf_id);
352 u_char offset = VRF_BITMAP_BIT_OFFSET(vrf_id);
353
354 if (bmap == VRF_BITMAP_NULL || vrf_id == VRF_UNKNOWN
355 || bm->groups[group] == NULL)
356 return 0;
357
358 return CHECK_FLAG(bm->groups[group][VRF_BITMAP_INDEX_IN_GROUP(offset)],
359 VRF_BITMAP_FLAG(offset))
360 ? 1
361 : 0;
362 }
363
364 static void vrf_autocomplete(vector comps, struct cmd_token *token)
365 {
366 struct vrf *vrf = NULL;
367
368 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
369 if (vrf->vrf_id != VRF_DEFAULT)
370 vector_set(comps, XSTRDUP(MTYPE_COMPLETION, vrf->name));
371 }
372 }
373
374 static const struct cmd_variable_handler vrf_var_handlers[] = {
375 {
376 .varname = "vrf",
377 .completions = vrf_autocomplete,
378 },
379 {.completions = NULL},
380 };
381
382 /* Initialize VRF module. */
383 void vrf_init(int (*create)(struct vrf *), int (*enable)(struct vrf *),
384 int (*disable)(struct vrf *), int (*delete)(struct vrf *))
385 {
386 struct vrf *default_vrf;
387
388 if (debug_vrf)
389 zlog_debug("%s: Initializing VRF subsystem",
390 __PRETTY_FUNCTION__);
391
392 vrf_master.vrf_new_hook = create;
393 vrf_master.vrf_enable_hook = enable;
394 vrf_master.vrf_disable_hook = disable;
395 vrf_master.vrf_delete_hook = delete;
396
397 /* The default VRF always exists. */
398 default_vrf = vrf_get(VRF_DEFAULT, VRF_DEFAULT_NAME);
399 if (!default_vrf) {
400 zlog_err("vrf_init: failed to create the default VRF!");
401 exit(1);
402 }
403
404 /* Enable the default VRF. */
405 if (!vrf_enable(default_vrf)) {
406 zlog_err("vrf_init: failed to enable the default VRF!");
407 exit(1);
408 }
409
410 cmd_variable_handler_register(vrf_var_handlers);
411 }
412
413 /* Terminate VRF module. */
414 void vrf_terminate(void)
415 {
416 struct vrf *vrf;
417
418 if (debug_vrf)
419 zlog_debug("%s: Shutting down vrf subsystem",
420 __PRETTY_FUNCTION__);
421
422 while ((vrf = RB_ROOT(vrf_id_head, &vrfs_by_id)) != NULL) {
423 /* Clear configured flag and invoke delete. */
424 UNSET_FLAG(vrf->status, VRF_CONFIGURED);
425 vrf_delete(vrf);
426 }
427 while ((vrf = RB_ROOT(vrf_name_head, &vrfs_by_name)) != NULL) {
428 /* Clear configured flag and invoke delete. */
429 UNSET_FLAG(vrf->status, VRF_CONFIGURED);
430 vrf_delete(vrf);
431 }
432 }
433
434 /* Create a socket for the VRF. */
435 int vrf_socket(int domain, int type, int protocol, vrf_id_t vrf_id)
436 {
437 int ret = -1;
438
439 ret = socket(domain, type, protocol);
440
441 return ret;
442 }
443
444 /* vrf CLI commands */
445 DEFUN_NOSH (vrf,
446 vrf_cmd,
447 "vrf NAME",
448 "Select a VRF to configure\n"
449 "VRF's name\n")
450 {
451 int idx_name = 1;
452 const char *vrfname = argv[idx_name]->arg;
453 struct vrf *vrfp;
454
455 if (strlen(vrfname) > VRF_NAMSIZ) {
456 vty_out(vty,
457 "%% VRF name %s is invalid: length exceeds "
458 "%d characters\n",
459 vrfname, VRF_NAMSIZ);
460 return CMD_WARNING_CONFIG_FAILED;
461 }
462
463 vrfp = vrf_get(VRF_UNKNOWN, vrfname);
464
465 VTY_PUSH_CONTEXT(VRF_NODE, vrfp);
466
467 return CMD_SUCCESS;
468 }
469
470 DEFUN_NOSH (no_vrf,
471 no_vrf_cmd,
472 "no vrf NAME",
473 NO_STR
474 "Delete a pseudo VRF's configuration\n"
475 "VRF's name\n")
476 {
477 const char *vrfname = argv[2]->arg;
478
479 struct vrf *vrfp;
480
481 vrfp = vrf_lookup_by_name(vrfname);
482
483 if (vrfp == NULL) {
484 vty_out(vty, "%% VRF %s does not exist\n", vrfname);
485 return CMD_WARNING_CONFIG_FAILED;
486 }
487
488 if (CHECK_FLAG(vrfp->status, VRF_ACTIVE)) {
489 vty_out(vty, "%% Only inactive VRFs can be deleted\n");
490 return CMD_WARNING_CONFIG_FAILED;
491 }
492
493 /* Clear configured flag and invoke delete. */
494 UNSET_FLAG(vrfp->status, VRF_CONFIGURED);
495 vrf_delete(vrfp);
496
497 return CMD_SUCCESS;
498 }
499
500
501 struct cmd_node vrf_node = {VRF_NODE, "%s(config-vrf)# ", 1};
502
503 /*
504 * Debug CLI for vrf's
505 */
506 DEFUN (vrf_debug,
507 vrf_debug_cmd,
508 "debug vrf",
509 DEBUG_STR
510 "VRF Debugging\n")
511 {
512 debug_vrf = 1;
513
514 return CMD_SUCCESS;
515 }
516
517 DEFUN (no_vrf_debug,
518 no_vrf_debug_cmd,
519 "no debug vrf",
520 NO_STR
521 DEBUG_STR
522 "VRF Debugging\n")
523 {
524 debug_vrf = 0;
525
526 return CMD_SUCCESS;
527 }
528
529 static int vrf_write_host(struct vty *vty)
530 {
531 if (debug_vrf)
532 vty_out(vty, "debug vrf\n");
533
534 return 1;
535 }
536
537 static struct cmd_node vrf_debug_node = {VRF_DEBUG_NODE, "", 1};
538
539 void vrf_install_commands(void)
540 {
541 install_node(&vrf_debug_node, vrf_write_host);
542
543 install_element(CONFIG_NODE, &vrf_debug_cmd);
544 install_element(ENABLE_NODE, &vrf_debug_cmd);
545 install_element(CONFIG_NODE, &no_vrf_debug_cmd);
546 install_element(ENABLE_NODE, &no_vrf_debug_cmd);
547 }
548
549 void vrf_cmd_init(int (*writefunc)(struct vty *vty))
550 {
551 install_element(CONFIG_NODE, &vrf_cmd);
552 install_element(CONFIG_NODE, &no_vrf_cmd);
553 install_node(&vrf_node, writefunc);
554 install_default(VRF_NODE);
555 }