]> git.proxmox.com Git - mirror_frr.git/blame - lib/vrf.c
Merge pull request #1324 from donaldsharp/bgp_aspath
[mirror_frr.git] / lib / vrf.c
CommitLineData
b72ede27
FL
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 *
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
b72ede27
FL
20 */
21
22#include <zebra.h>
23
6a69b354 24#include "if.h"
b72ede27 25#include "vrf.h"
7922fc65 26#include "vrf_int.h"
b72ede27
FL
27#include "prefix.h"
28#include "table.h"
29#include "log.h"
30#include "memory.h"
19dc275e
DS
31#include "command.h"
32
d62a17ae 33DEFINE_MTYPE_STATIC(LIB, VRF, "VRF")
4a1ab8e4
DL
34DEFINE_MTYPE_STATIC(LIB, VRF_BITMAP, "VRF bit-map")
35
e80e7cce
DL
36DEFINE_QOBJ_TYPE(vrf)
37
d62a17ae 38static __inline int vrf_id_compare(const struct vrf *, const struct vrf *);
39static __inline int vrf_name_compare(const struct vrf *, const struct vrf *);
1a1a7065 40
d62a17ae 41RB_GENERATE(vrf_id_head, vrf, id_entry, vrf_id_compare);
42RB_GENERATE(vrf_name_head, vrf, name_entry, vrf_name_compare);
1a1a7065 43
d62a17ae 44struct vrf_id_head vrfs_by_id = RB_INITIALIZER(&vrfs_by_id);
45struct vrf_name_head vrfs_by_name = RB_INITIALIZER(&vrfs_by_name);
1a1a7065 46
19dc275e
DS
47/*
48 * Turn on/off debug code
49 * for vrf.
50 */
51int debug_vrf = 0;
b72ede27 52
b72ede27 53/* Holding VRF hooks */
d62a17ae 54struct 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};
b72ede27 62
d62a17ae 63static int vrf_is_enabled(struct vrf *vrf);
64static void vrf_disable(struct vrf *vrf);
e5bf3e1e 65
216b18ef 66/* VRF list existance check by name. */
d62a17ae 67struct vrf *vrf_lookup_by_name(const char *name)
216b18ef 68{
d62a17ae 69 struct vrf vrf;
70 strlcpy(vrf.name, name, sizeof(vrf.name));
71 return (RB_FIND(vrf_name_head, &vrfs_by_name, &vrf));
216b18ef 72}
216b18ef 73
d62a17ae 74static __inline int vrf_id_compare(const struct vrf *a, const struct vrf *b)
b72ede27 75{
d62a17ae 76 return (a->vrf_id - b->vrf_id);
216b18ef
DS
77}
78
d62a17ae 79static int vrf_name_compare(const struct vrf *a, const struct vrf *b)
b72ede27 80{
d62a17ae 81 return strcmp(a->name, b->name);
b72ede27
FL
82}
83
216b18ef 84/* Get a VRF. If not found, create one.
34f8e6af
DS
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
216b18ef 88 * Description: Please note that this routine can be called with just the name
34f8e6af
DS
89 * and 0 vrf-id
90 */
d62a17ae 91struct 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(%d)", 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;
f4e14fdb 112 RB_INIT(if_name_head, &vrf->ifaces_by_name);
ff880b78 113 RB_INIT(if_index_head, &vrf->ifaces_by_index);
d62a17ae 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;
b72ede27
FL
142}
143
144/* Delete a VRF. This is called in vrf_terminate(). */
d62a17ae 145void vrf_delete(struct vrf *vrf)
b72ede27 146{
d62a17ae 147 if (debug_vrf)
148 zlog_debug("VRF %u is to be deleted.", vrf->vrf_id);
b72ede27 149
d62a17ae 150 if (vrf_is_enabled(vrf))
151 vrf_disable(vrf);
e5bf3e1e 152
d62a17ae 153 if (vrf_master.vrf_delete_hook)
154 (*vrf_master.vrf_delete_hook)(vrf);
216b18ef 155
d62a17ae 156 QOBJ_UNREG(vrf);
f4e14fdb 157 if_terminate(vrf);
b72ede27 158
d62a17ae 159 if (vrf->vrf_id != VRF_UNKNOWN)
160 RB_REMOVE(vrf_id_head, &vrfs_by_id, vrf);
161 if (vrf->name[0] != '\0')
162 RB_REMOVE(vrf_name_head, &vrfs_by_name, vrf);
b72ede27 163
d62a17ae 164 XFREE(MTYPE_VRF, vrf);
b72ede27
FL
165}
166
167/* Look up a VRF by identifier. */
d62a17ae 168struct vrf *vrf_lookup_by_id(vrf_id_t vrf_id)
b72ede27 169{
d62a17ae 170 struct vrf vrf;
171 vrf.vrf_id = vrf_id;
172 return (RB_FIND(vrf_id_head, &vrfs_by_id, &vrf));
b72ede27
FL
173}
174
e5bf3e1e 175/*
05e8e11e 176 * Check whether the VRF is enabled.
e5bf3e1e 177 */
d62a17ae 178static int vrf_is_enabled(struct vrf *vrf)
e5bf3e1e 179{
d62a17ae 180 return vrf && CHECK_FLAG(vrf->status, VRF_ACTIVE);
e5bf3e1e
FL
181}
182
183/*
184 * Enable a VRF - that is, let the VRF be ready to use.
185 * The VRF_ENABLE_HOOK callback will be called to inform
186 * that they can allocate resources in this VRF.
187 *
188 * RETURN: 1 - enabled successfully; otherwise, 0.
189 */
d62a17ae 190int vrf_enable(struct vrf *vrf)
e5bf3e1e 191{
d62a17ae 192 if (vrf_is_enabled(vrf))
193 return 1;
05e8e11e 194
d62a17ae 195 if (debug_vrf)
196 zlog_debug("VRF %u is enabled.", vrf->vrf_id);
e5bf3e1e 197
d62a17ae 198 SET_FLAG(vrf->status, VRF_ACTIVE);
e5bf3e1e 199
d62a17ae 200 if (vrf_master.vrf_enable_hook)
201 (*vrf_master.vrf_enable_hook)(vrf);
e5bf3e1e 202
d62a17ae 203 return 1;
e5bf3e1e
FL
204}
205
206/*
207 * Disable a VRF - that is, let the VRF be unusable.
208 * The VRF_DELETE_HOOK callback will be called to inform
209 * that they must release the resources in the VRF.
210 */
d62a17ae 211static void vrf_disable(struct vrf *vrf)
e5bf3e1e 212{
d62a17ae 213 if (!vrf_is_enabled(vrf))
214 return;
a647bfa8 215
d62a17ae 216 UNSET_FLAG(vrf->status, VRF_ACTIVE);
e5bf3e1e 217
d62a17ae 218 if (debug_vrf)
219 zlog_debug("VRF %u is to be disabled.", vrf->vrf_id);
e5bf3e1e 220
d62a17ae 221 /* Till now, nothing to be done for the default VRF. */
222 // Pending: see why this statement.
e74f14fc 223
d62a17ae 224 if (vrf_master.vrf_disable_hook)
225 (*vrf_master.vrf_disable_hook)(vrf);
e5bf3e1e
FL
226}
227
d62a17ae 228vrf_id_t vrf_name_to_id(const char *name)
216b18ef 229{
d62a17ae 230 struct vrf *vrf;
231 vrf_id_t vrf_id = VRF_DEFAULT; // Pending: need a way to return invalid
232 // id/ routine not used.
216b18ef 233
d62a17ae 234 vrf = vrf_lookup_by_name(name);
235 if (vrf)
236 vrf_id = vrf->vrf_id;
216b18ef 237
d62a17ae 238 return vrf_id;
216b18ef
DS
239}
240
b72ede27 241/* Get the data pointer of the specified VRF. If not found, create one. */
d62a17ae 242void *vrf_info_get(vrf_id_t vrf_id)
b72ede27 243{
d62a17ae 244 struct vrf *vrf = vrf_get(vrf_id, NULL);
245 return vrf->info;
b72ede27
FL
246}
247
248/* Look up the data pointer of the specified VRF. */
d62a17ae 249void *vrf_info_lookup(vrf_id_t vrf_id)
b72ede27 250{
d62a17ae 251 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
252 return vrf ? vrf->info : NULL;
b72ede27
FL
253}
254
7076bb2f
FL
255/*
256 * VRF bit-map
257 */
258
259#define VRF_BITMAP_NUM_OF_GROUPS 8
d62a17ae 260#define VRF_BITMAP_NUM_OF_BITS_IN_GROUP (UINT16_MAX / VRF_BITMAP_NUM_OF_GROUPS)
261#define VRF_BITMAP_NUM_OF_BYTES_IN_GROUP \
262 (VRF_BITMAP_NUM_OF_BITS_IN_GROUP / CHAR_BIT + 1) /* +1 for ensure */
263
264#define VRF_BITMAP_GROUP(_id) ((_id) / VRF_BITMAP_NUM_OF_BITS_IN_GROUP)
265#define VRF_BITMAP_BIT_OFFSET(_id) ((_id) % VRF_BITMAP_NUM_OF_BITS_IN_GROUP)
266
267#define VRF_BITMAP_INDEX_IN_GROUP(_bit_offset) ((_bit_offset) / CHAR_BIT)
268#define VRF_BITMAP_FLAG(_bit_offset) (((u_char)1) << ((_bit_offset) % CHAR_BIT))
269
270struct vrf_bitmap {
271 u_char *groups[VRF_BITMAP_NUM_OF_GROUPS];
7076bb2f
FL
272};
273
d62a17ae 274vrf_bitmap_t vrf_bitmap_init(void)
7076bb2f 275{
d62a17ae 276 return (vrf_bitmap_t)XCALLOC(MTYPE_VRF_BITMAP,
277 sizeof(struct vrf_bitmap));
7076bb2f
FL
278}
279
d62a17ae 280void vrf_bitmap_free(vrf_bitmap_t bmap)
7076bb2f 281{
d62a17ae 282 struct vrf_bitmap *bm = (struct vrf_bitmap *)bmap;
283 int i;
7076bb2f 284
d62a17ae 285 if (bmap == VRF_BITMAP_NULL)
286 return;
7076bb2f 287
d62a17ae 288 for (i = 0; i < VRF_BITMAP_NUM_OF_GROUPS; i++)
289 if (bm->groups[i])
290 XFREE(MTYPE_VRF_BITMAP, bm->groups[i]);
7076bb2f 291
d62a17ae 292 XFREE(MTYPE_VRF_BITMAP, bm);
7076bb2f
FL
293}
294
d62a17ae 295void vrf_bitmap_set(vrf_bitmap_t bmap, vrf_id_t vrf_id)
7076bb2f 296{
d62a17ae 297 struct vrf_bitmap *bm = (struct vrf_bitmap *)bmap;
298 u_char group = VRF_BITMAP_GROUP(vrf_id);
299 u_char offset = VRF_BITMAP_BIT_OFFSET(vrf_id);
7076bb2f 300
d62a17ae 301 if (bmap == VRF_BITMAP_NULL || vrf_id == VRF_UNKNOWN)
302 return;
7076bb2f 303
d62a17ae 304 if (bm->groups[group] == NULL)
305 bm->groups[group] = XCALLOC(MTYPE_VRF_BITMAP,
306 VRF_BITMAP_NUM_OF_BYTES_IN_GROUP);
7076bb2f 307
d62a17ae 308 SET_FLAG(bm->groups[group][VRF_BITMAP_INDEX_IN_GROUP(offset)],
309 VRF_BITMAP_FLAG(offset));
7076bb2f
FL
310}
311
d62a17ae 312void vrf_bitmap_unset(vrf_bitmap_t bmap, vrf_id_t vrf_id)
7076bb2f 313{
d62a17ae 314 struct vrf_bitmap *bm = (struct vrf_bitmap *)bmap;
315 u_char group = VRF_BITMAP_GROUP(vrf_id);
316 u_char offset = VRF_BITMAP_BIT_OFFSET(vrf_id);
7076bb2f 317
d62a17ae 318 if (bmap == VRF_BITMAP_NULL || vrf_id == VRF_UNKNOWN
319 || bm->groups[group] == NULL)
320 return;
7076bb2f 321
d62a17ae 322 UNSET_FLAG(bm->groups[group][VRF_BITMAP_INDEX_IN_GROUP(offset)],
323 VRF_BITMAP_FLAG(offset));
7076bb2f
FL
324}
325
d62a17ae 326int vrf_bitmap_check(vrf_bitmap_t bmap, vrf_id_t vrf_id)
7076bb2f 327{
d62a17ae 328 struct vrf_bitmap *bm = (struct vrf_bitmap *)bmap;
329 u_char group = VRF_BITMAP_GROUP(vrf_id);
330 u_char offset = VRF_BITMAP_BIT_OFFSET(vrf_id);
7076bb2f 331
d62a17ae 332 if (bmap == VRF_BITMAP_NULL || vrf_id == VRF_UNKNOWN
333 || bm->groups[group] == NULL)
334 return 0;
7076bb2f 335
d62a17ae 336 return CHECK_FLAG(bm->groups[group][VRF_BITMAP_INDEX_IN_GROUP(offset)],
337 VRF_BITMAP_FLAG(offset))
338 ? 1
339 : 0;
7076bb2f
FL
340}
341
d62a17ae 342static void vrf_autocomplete(vector comps, struct cmd_token *token)
d617d5fe 343{
d62a17ae 344 struct vrf *vrf = NULL;
d617d5fe 345
a2addae8 346 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
d62a17ae 347 if (vrf->vrf_id != 0)
348 vector_set(comps, XSTRDUP(MTYPE_COMPLETION, vrf->name));
349 }
d617d5fe
DS
350}
351
352static const struct cmd_variable_handler vrf_var_handlers[] = {
d62a17ae 353 {
354 .varname = "vrf",
355 .completions = vrf_autocomplete,
356 },
357 {.completions = NULL},
d617d5fe
DS
358};
359
b72ede27 360/* Initialize VRF module. */
d62a17ae 361void vrf_init(int (*create)(struct vrf *), int (*enable)(struct vrf *),
362 int (*disable)(struct vrf *), int (*delete)(struct vrf *))
363{
364 struct vrf *default_vrf;
365
366 if (debug_vrf)
367 zlog_debug("%s: Initializing VRF subsystem",
368 __PRETTY_FUNCTION__);
369
370 vrf_master.vrf_new_hook = create;
371 vrf_master.vrf_enable_hook = enable;
372 vrf_master.vrf_disable_hook = disable;
373 vrf_master.vrf_delete_hook = delete;
374
375 /* The default VRF always exists. */
376 default_vrf = vrf_get(VRF_DEFAULT, VRF_DEFAULT_NAME);
377 if (!default_vrf) {
378 zlog_err("vrf_init: failed to create the default VRF!");
379 exit(1);
380 }
381
382 /* Enable the default VRF. */
383 if (!vrf_enable(default_vrf)) {
384 zlog_err("vrf_init: failed to enable the default VRF!");
385 exit(1);
386 }
387
388 cmd_variable_handler_register(vrf_var_handlers);
b72ede27
FL
389}
390
391/* Terminate VRF module. */
d62a17ae 392void vrf_terminate(void)
b72ede27 393{
d62a17ae 394 struct vrf *vrf;
b72ede27 395
d62a17ae 396 if (debug_vrf)
397 zlog_debug("%s: Shutting down vrf subsystem",
398 __PRETTY_FUNCTION__);
19dc275e 399
d62a17ae 400 while ((vrf = RB_ROOT(vrf_id_head, &vrfs_by_id)) != NULL)
401 vrf_delete(vrf);
402 while ((vrf = RB_ROOT(vrf_name_head, &vrfs_by_name)) != NULL)
403 vrf_delete(vrf);
b72ede27
FL
404}
405
e5bf3e1e 406/* Create a socket for the VRF. */
d62a17ae 407int vrf_socket(int domain, int type, int protocol, vrf_id_t vrf_id)
e5bf3e1e 408{
d62a17ae 409 int ret = -1;
e5bf3e1e 410
d62a17ae 411 ret = socket(domain, type, protocol);
e5bf3e1e 412
d62a17ae 413 return ret;
e5bf3e1e
FL
414}
415
f30c50b9 416/* vrf CLI commands */
505e5056 417DEFUN_NOSH (vrf,
f30c50b9 418 vrf_cmd,
d7a75a6c 419 "vrf NAME",
f30c50b9
RW
420 "Select a VRF to configure\n"
421 "VRF's name\n")
422{
d62a17ae 423 int idx_name = 1;
424 const char *vrfname = argv[idx_name]->arg;
425 struct vrf *vrfp;
f30c50b9 426
d62a17ae 427 if (strlen(vrfname) > VRF_NAMSIZ) {
428 vty_out(vty,
429 "%% VRF name %s is invalid: length exceeds "
430 "%d characters\n",
431 vrfname, VRF_NAMSIZ);
432 return CMD_WARNING_CONFIG_FAILED;
433 }
f30c50b9 434
d62a17ae 435 vrfp = vrf_get(VRF_UNKNOWN, vrfname);
f30c50b9 436
d62a17ae 437 VTY_PUSH_CONTEXT(VRF_NODE, vrfp);
f30c50b9 438
d62a17ae 439 return CMD_SUCCESS;
f30c50b9
RW
440}
441
442DEFUN_NOSH (no_vrf,
443 no_vrf_cmd,
d7a75a6c 444 "no vrf NAME",
f30c50b9
RW
445 NO_STR
446 "Delete a pseudo VRF's configuration\n"
447 "VRF's name\n")
448{
d62a17ae 449 const char *vrfname = argv[2]->arg;
53dc2b05 450
d62a17ae 451 struct vrf *vrfp;
f30c50b9 452
d62a17ae 453 vrfp = vrf_lookup_by_name(vrfname);
f30c50b9 454
d62a17ae 455 if (vrfp == NULL) {
456 vty_out(vty, "%% VRF %s does not exist\n", vrfname);
457 return CMD_WARNING_CONFIG_FAILED;
458 }
f30c50b9 459
d62a17ae 460 if (CHECK_FLAG(vrfp->status, VRF_ACTIVE)) {
461 vty_out(vty, "%% Only inactive VRFs can be deleted\n");
462 return CMD_WARNING_CONFIG_FAILED;
463 }
f30c50b9 464
d62a17ae 465 vrf_delete(vrfp);
f30c50b9 466
d62a17ae 467 return CMD_SUCCESS;
f30c50b9
RW
468}
469
53dc2b05 470
d62a17ae 471struct cmd_node vrf_node = {VRF_NODE, "%s(config-vrf)# ", 1};
7ddcfca4 472
19dc275e
DS
473/*
474 * Debug CLI for vrf's
475 */
476DEFUN (vrf_debug,
477 vrf_debug_cmd,
478 "debug vrf",
479 DEBUG_STR
480 "VRF Debugging\n")
481{
d62a17ae 482 debug_vrf = 1;
19dc275e 483
d62a17ae 484 return CMD_SUCCESS;
19dc275e
DS
485}
486
487DEFUN (no_vrf_debug,
488 no_vrf_debug_cmd,
489 "no debug vrf",
490 NO_STR
491 DEBUG_STR
492 "VRF Debugging\n")
493{
d62a17ae 494 debug_vrf = 0;
19dc275e 495
d62a17ae 496 return CMD_SUCCESS;
19dc275e
DS
497}
498
d62a17ae 499static int vrf_write_host(struct vty *vty)
19dc275e 500{
d62a17ae 501 if (debug_vrf)
502 vty_out(vty, "debug vrf\n");
19dc275e 503
d62a17ae 504 return 1;
19dc275e
DS
505}
506
d62a17ae 507static struct cmd_node vrf_debug_node = {VRF_DEBUG_NODE, "", 1};
19dc275e 508
d62a17ae 509void vrf_install_commands(void)
19dc275e 510{
d62a17ae 511 install_node(&vrf_debug_node, vrf_write_host);
19dc275e 512
d62a17ae 513 install_element(CONFIG_NODE, &vrf_debug_cmd);
514 install_element(ENABLE_NODE, &vrf_debug_cmd);
515 install_element(CONFIG_NODE, &no_vrf_debug_cmd);
516 install_element(ENABLE_NODE, &no_vrf_debug_cmd);
19dc275e 517}
53dc2b05 518
d62a17ae 519void vrf_cmd_init(int (*writefunc)(struct vty *vty))
7ddcfca4 520{
d62a17ae 521 install_element(CONFIG_NODE, &vrf_cmd);
522 install_element(CONFIG_NODE, &no_vrf_cmd);
523 install_node(&vrf_node, writefunc);
524 install_default(VRF_NODE);
19dc275e 525}