]> git.proxmox.com Git - mirror_frr.git/blame - lib/vrf.c
bgpd: no need to initialise netns directly
[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
4691b65a
PG
24/* for basename */
25#include <libgen.h>
26
6a69b354 27#include "if.h"
b72ede27 28#include "vrf.h"
7922fc65 29#include "vrf_int.h"
b72ede27
FL
30#include "prefix.h"
31#include "table.h"
32#include "log.h"
33#include "memory.h"
19dc275e 34#include "command.h"
b95c1883 35#include "ns.h"
19dc275e 36
ec31f30d
PG
37/* default VRF ID value used when VRF backend is not NETNS */
38#define VRF_DEFAULT_INTERNAL 0
39
d62a17ae 40DEFINE_MTYPE_STATIC(LIB, VRF, "VRF")
4a1ab8e4
DL
41DEFINE_MTYPE_STATIC(LIB, VRF_BITMAP, "VRF bit-map")
42
e80e7cce
DL
43DEFINE_QOBJ_TYPE(vrf)
44
d62a17ae 45static __inline int vrf_id_compare(const struct vrf *, const struct vrf *);
46static __inline int vrf_name_compare(const struct vrf *, const struct vrf *);
1a1a7065 47
d62a17ae 48RB_GENERATE(vrf_id_head, vrf, id_entry, vrf_id_compare);
49RB_GENERATE(vrf_name_head, vrf, name_entry, vrf_name_compare);
1a1a7065 50
d62a17ae 51struct vrf_id_head vrfs_by_id = RB_INITIALIZER(&vrfs_by_id);
52struct vrf_name_head vrfs_by_name = RB_INITIALIZER(&vrfs_by_name);
1a1a7065 53
78dd30b2
PG
54static int vrf_backend;
55
19dc275e
DS
56/*
57 * Turn on/off debug code
58 * for vrf.
59 */
60int debug_vrf = 0;
b72ede27 61
b72ede27 62/* Holding VRF hooks */
d62a17ae 63struct vrf_master {
64 int (*vrf_new_hook)(struct vrf *);
65 int (*vrf_delete_hook)(struct vrf *);
66 int (*vrf_enable_hook)(struct vrf *);
67 int (*vrf_disable_hook)(struct vrf *);
68} vrf_master = {
69 0,
70};
b72ede27 71
d62a17ae 72static int vrf_is_enabled(struct vrf *vrf);
e5bf3e1e 73
216b18ef 74/* VRF list existance check by name. */
d62a17ae 75struct vrf *vrf_lookup_by_name(const char *name)
216b18ef 76{
d62a17ae 77 struct vrf vrf;
78 strlcpy(vrf.name, name, sizeof(vrf.name));
79 return (RB_FIND(vrf_name_head, &vrfs_by_name, &vrf));
216b18ef 80}
216b18ef 81
d62a17ae 82static __inline int vrf_id_compare(const struct vrf *a, const struct vrf *b)
b72ede27 83{
d62a17ae 84 return (a->vrf_id - b->vrf_id);
216b18ef
DS
85}
86
d62a17ae 87static int vrf_name_compare(const struct vrf *a, const struct vrf *b)
b72ede27 88{
d62a17ae 89 return strcmp(a->name, b->name);
b72ede27
FL
90}
91
e26aedbe
PG
92/* if ns_id is different and not VRF_UNKNOWN,
93 * then update vrf identifier, and enable VRF
94 */
95static void vrf_update_vrf_id(ns_id_t ns_id, void *opaqueptr)
96{
97 ns_id_t vrf_id = (vrf_id_t)ns_id;
98 vrf_id_t old_vrf_id;
99 struct vrf *vrf = (struct vrf *)opaqueptr;
100
101 if (!vrf)
102 return;
103 old_vrf_id = vrf->vrf_id;
104 if (vrf_id == vrf->vrf_id)
105 return;
106 if (vrf->vrf_id != VRF_UNKNOWN)
107 RB_REMOVE(vrf_id_head, &vrfs_by_id, vrf);
108 vrf->vrf_id = vrf_id;
109 RB_INSERT(vrf_id_head, &vrfs_by_id, vrf);
110 if (old_vrf_id == VRF_UNKNOWN)
111 vrf_enable((struct vrf *)vrf);
112}
113
ce1be369
PG
114int vrf_switch_to_netns(vrf_id_t vrf_id)
115{
116 char *name;
117 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
118
ce1be369 119 /* VRF is default VRF. silently ignore */
e26aedbe
PG
120 if (!vrf || vrf->vrf_id == VRF_DEFAULT)
121 return 0;
122 /* VRF has no NETNS backend. silently ignore */
123 if (vrf->data.l.netns_name[0] == '\0')
ce1be369
PG
124 return 0;
125 name = ns_netns_pathname(NULL, vrf->data.l.netns_name);
126 if (debug_vrf)
127 zlog_debug("VRF_SWITCH: %s(%u)", name, vrf->vrf_id);
128 return ns_switch_to_netns(name);
129}
130
131int vrf_switchback_to_initial(void)
132{
133 int ret = ns_switchback_to_initial();
134
135 if (ret == 0 && debug_vrf)
136 zlog_debug("VRF_SWITCHBACK");
137 return ret;
138}
139
216b18ef 140/* Get a VRF. If not found, create one.
34f8e6af
DS
141 * Arg:
142 * name - The name of the vrf. May be NULL if unknown.
143 * vrf_id - The vrf_id of the vrf. May be VRF_UNKNOWN if unknown
216b18ef 144 * Description: Please note that this routine can be called with just the name
34f8e6af
DS
145 * and 0 vrf-id
146 */
d62a17ae 147struct vrf *vrf_get(vrf_id_t vrf_id, const char *name)
148{
149 struct vrf *vrf = NULL;
150 int new = 0;
151
152 if (debug_vrf)
fe533c56
PG
153 zlog_debug("VRF_GET: %s(%u)",
154 name == NULL ? "(NULL)" : name, vrf_id);
d62a17ae 155
156 /* Nothing to see, move along here */
157 if (!name && vrf_id == VRF_UNKNOWN)
158 return NULL;
159
160 /* Try to find VRF both by ID and name */
161 if (vrf_id != VRF_UNKNOWN)
162 vrf = vrf_lookup_by_id(vrf_id);
163 if (!vrf && name)
164 vrf = vrf_lookup_by_name(name);
165
166 if (vrf == NULL) {
167 vrf = XCALLOC(MTYPE_VRF, sizeof(struct vrf));
168 vrf->vrf_id = VRF_UNKNOWN;
d62a17ae 169 QOBJ_REG(vrf, vrf);
170 new = 1;
171
172 if (debug_vrf)
173 zlog_debug("VRF(%u) %s is created.", vrf_id,
174 (name) ? name : "(NULL)");
175 }
176
177 /* Set identifier */
178 if (vrf_id != VRF_UNKNOWN && vrf->vrf_id == VRF_UNKNOWN) {
179 vrf->vrf_id = vrf_id;
180 RB_INSERT(vrf_id_head, &vrfs_by_id, vrf);
181 }
182
183 /* Set name */
184 if (name && vrf->name[0] != '\0' && strcmp(name, vrf->name)) {
185 RB_REMOVE(vrf_name_head, &vrfs_by_name, vrf);
186 strlcpy(vrf->name, name, sizeof(vrf->name));
187 RB_INSERT(vrf_name_head, &vrfs_by_name, vrf);
188 } else if (name && vrf->name[0] == '\0') {
189 strlcpy(vrf->name, name, sizeof(vrf->name));
190 RB_INSERT(vrf_name_head, &vrfs_by_name, vrf);
191 }
d62a17ae 192 if (new &&vrf_master.vrf_new_hook)
193 (*vrf_master.vrf_new_hook)(vrf);
194
195 return vrf;
b72ede27
FL
196}
197
84915b0a 198/* Delete a VRF. This is called when the underlying VRF goes away, a
199 * pre-configured VRF is deleted or when shutting down (vrf_terminate()).
200 */
d62a17ae 201void vrf_delete(struct vrf *vrf)
b72ede27 202{
d62a17ae 203 if (debug_vrf)
204 zlog_debug("VRF %u is to be deleted.", vrf->vrf_id);
b72ede27 205
d62a17ae 206 if (vrf_is_enabled(vrf))
207 vrf_disable(vrf);
e5bf3e1e 208
84915b0a 209 /* If the VRF is user configured, it'll stick around, just remove
210 * the ID mapping. Interfaces assigned to this VRF should've been
211 * removed already as part of the VRF going down.
212 */
213 if (vrf_is_user_cfged(vrf)) {
214 if (vrf->vrf_id != VRF_UNKNOWN) {
215 /* Delete any VRF interfaces - should be only
216 * the VRF itself, other interfaces should've
217 * been moved out of the VRF.
218 */
219 if_terminate(vrf);
220 RB_REMOVE(vrf_id_head, &vrfs_by_id, vrf);
221 vrf->vrf_id = VRF_UNKNOWN;
222 }
223 return;
224 }
225
d62a17ae 226 if (vrf_master.vrf_delete_hook)
227 (*vrf_master.vrf_delete_hook)(vrf);
216b18ef 228
d62a17ae 229 QOBJ_UNREG(vrf);
f4e14fdb 230 if_terminate(vrf);
b72ede27 231
d62a17ae 232 if (vrf->vrf_id != VRF_UNKNOWN)
233 RB_REMOVE(vrf_id_head, &vrfs_by_id, vrf);
234 if (vrf->name[0] != '\0')
235 RB_REMOVE(vrf_name_head, &vrfs_by_name, vrf);
b72ede27 236
d62a17ae 237 XFREE(MTYPE_VRF, vrf);
b72ede27
FL
238}
239
240/* Look up a VRF by identifier. */
d62a17ae 241struct vrf *vrf_lookup_by_id(vrf_id_t vrf_id)
b72ede27 242{
d62a17ae 243 struct vrf vrf;
244 vrf.vrf_id = vrf_id;
245 return (RB_FIND(vrf_id_head, &vrfs_by_id, &vrf));
b72ede27
FL
246}
247
e5bf3e1e
FL
248/*
249 * Enable a VRF - that is, let the VRF be ready to use.
250 * The VRF_ENABLE_HOOK callback will be called to inform
251 * that they can allocate resources in this VRF.
252 *
253 * RETURN: 1 - enabled successfully; otherwise, 0.
254 */
d62a17ae 255int vrf_enable(struct vrf *vrf)
e5bf3e1e 256{
d62a17ae 257 if (vrf_is_enabled(vrf))
258 return 1;
05e8e11e 259
d62a17ae 260 if (debug_vrf)
261 zlog_debug("VRF %u is enabled.", vrf->vrf_id);
e5bf3e1e 262
d62a17ae 263 SET_FLAG(vrf->status, VRF_ACTIVE);
e5bf3e1e 264
d62a17ae 265 if (vrf_master.vrf_enable_hook)
266 (*vrf_master.vrf_enable_hook)(vrf);
e5bf3e1e 267
d62a17ae 268 return 1;
e5bf3e1e
FL
269}
270
271/*
272 * Disable a VRF - that is, let the VRF be unusable.
273 * The VRF_DELETE_HOOK callback will be called to inform
274 * that they must release the resources in the VRF.
275 */
697d3ec7 276void vrf_disable(struct vrf *vrf)
e5bf3e1e 277{
d62a17ae 278 if (!vrf_is_enabled(vrf))
279 return;
a647bfa8 280
d62a17ae 281 UNSET_FLAG(vrf->status, VRF_ACTIVE);
e5bf3e1e 282
d62a17ae 283 if (debug_vrf)
284 zlog_debug("VRF %u is to be disabled.", vrf->vrf_id);
e5bf3e1e 285
d62a17ae 286 /* Till now, nothing to be done for the default VRF. */
287 // Pending: see why this statement.
e74f14fc 288
d62a17ae 289 if (vrf_master.vrf_disable_hook)
290 (*vrf_master.vrf_disable_hook)(vrf);
e5bf3e1e
FL
291}
292
b7cfce93
MK
293const char *vrf_id_to_name(vrf_id_t vrf_id)
294{
295 struct vrf *vrf;
296
297 vrf = vrf_lookup_by_id(vrf_id);
298 if (vrf)
299 return vrf->name;
300
181c08c6 301 return "n/a";
b7cfce93
MK
302}
303
d62a17ae 304vrf_id_t vrf_name_to_id(const char *name)
216b18ef 305{
d62a17ae 306 struct vrf *vrf;
307 vrf_id_t vrf_id = VRF_DEFAULT; // Pending: need a way to return invalid
308 // id/ routine not used.
216b18ef 309
d62a17ae 310 vrf = vrf_lookup_by_name(name);
311 if (vrf)
312 vrf_id = vrf->vrf_id;
216b18ef 313
d62a17ae 314 return vrf_id;
216b18ef
DS
315}
316
b72ede27 317/* Get the data pointer of the specified VRF. If not found, create one. */
d62a17ae 318void *vrf_info_get(vrf_id_t vrf_id)
b72ede27 319{
d62a17ae 320 struct vrf *vrf = vrf_get(vrf_id, NULL);
321 return vrf->info;
b72ede27
FL
322}
323
324/* Look up the data pointer of the specified VRF. */
d62a17ae 325void *vrf_info_lookup(vrf_id_t vrf_id)
b72ede27 326{
d62a17ae 327 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
328 return vrf ? vrf->info : NULL;
b72ede27
FL
329}
330
7076bb2f
FL
331/*
332 * VRF bit-map
333 */
334
3bd74754 335#define VRF_BITMAP_NUM_OF_GROUPS 1024
a9ff90c4 336#define VRF_BITMAP_NUM_OF_BITS_IN_GROUP (UINT32_MAX / VRF_BITMAP_NUM_OF_GROUPS)
d62a17ae 337#define VRF_BITMAP_NUM_OF_BYTES_IN_GROUP \
338 (VRF_BITMAP_NUM_OF_BITS_IN_GROUP / CHAR_BIT + 1) /* +1 for ensure */
339
340#define VRF_BITMAP_GROUP(_id) ((_id) / VRF_BITMAP_NUM_OF_BITS_IN_GROUP)
341#define VRF_BITMAP_BIT_OFFSET(_id) ((_id) % VRF_BITMAP_NUM_OF_BITS_IN_GROUP)
342
343#define VRF_BITMAP_INDEX_IN_GROUP(_bit_offset) ((_bit_offset) / CHAR_BIT)
344#define VRF_BITMAP_FLAG(_bit_offset) (((u_char)1) << ((_bit_offset) % CHAR_BIT))
345
346struct vrf_bitmap {
347 u_char *groups[VRF_BITMAP_NUM_OF_GROUPS];
7076bb2f
FL
348};
349
d62a17ae 350vrf_bitmap_t vrf_bitmap_init(void)
7076bb2f 351{
d62a17ae 352 return (vrf_bitmap_t)XCALLOC(MTYPE_VRF_BITMAP,
353 sizeof(struct vrf_bitmap));
7076bb2f
FL
354}
355
d62a17ae 356void vrf_bitmap_free(vrf_bitmap_t bmap)
7076bb2f 357{
d62a17ae 358 struct vrf_bitmap *bm = (struct vrf_bitmap *)bmap;
359 int i;
7076bb2f 360
d62a17ae 361 if (bmap == VRF_BITMAP_NULL)
362 return;
7076bb2f 363
d62a17ae 364 for (i = 0; i < VRF_BITMAP_NUM_OF_GROUPS; i++)
365 if (bm->groups[i])
366 XFREE(MTYPE_VRF_BITMAP, bm->groups[i]);
7076bb2f 367
d62a17ae 368 XFREE(MTYPE_VRF_BITMAP, bm);
7076bb2f
FL
369}
370
d62a17ae 371void vrf_bitmap_set(vrf_bitmap_t bmap, vrf_id_t vrf_id)
7076bb2f 372{
d62a17ae 373 struct vrf_bitmap *bm = (struct vrf_bitmap *)bmap;
374 u_char group = VRF_BITMAP_GROUP(vrf_id);
375 u_char offset = VRF_BITMAP_BIT_OFFSET(vrf_id);
7076bb2f 376
d62a17ae 377 if (bmap == VRF_BITMAP_NULL || vrf_id == VRF_UNKNOWN)
378 return;
7076bb2f 379
d62a17ae 380 if (bm->groups[group] == NULL)
381 bm->groups[group] = XCALLOC(MTYPE_VRF_BITMAP,
382 VRF_BITMAP_NUM_OF_BYTES_IN_GROUP);
7076bb2f 383
d62a17ae 384 SET_FLAG(bm->groups[group][VRF_BITMAP_INDEX_IN_GROUP(offset)],
385 VRF_BITMAP_FLAG(offset));
7076bb2f
FL
386}
387
d62a17ae 388void vrf_bitmap_unset(vrf_bitmap_t bmap, vrf_id_t vrf_id)
7076bb2f 389{
d62a17ae 390 struct vrf_bitmap *bm = (struct vrf_bitmap *)bmap;
391 u_char group = VRF_BITMAP_GROUP(vrf_id);
392 u_char offset = VRF_BITMAP_BIT_OFFSET(vrf_id);
7076bb2f 393
d62a17ae 394 if (bmap == VRF_BITMAP_NULL || vrf_id == VRF_UNKNOWN
395 || bm->groups[group] == NULL)
396 return;
7076bb2f 397
d62a17ae 398 UNSET_FLAG(bm->groups[group][VRF_BITMAP_INDEX_IN_GROUP(offset)],
399 VRF_BITMAP_FLAG(offset));
7076bb2f
FL
400}
401
d62a17ae 402int vrf_bitmap_check(vrf_bitmap_t bmap, vrf_id_t vrf_id)
7076bb2f 403{
d62a17ae 404 struct vrf_bitmap *bm = (struct vrf_bitmap *)bmap;
405 u_char group = VRF_BITMAP_GROUP(vrf_id);
406 u_char offset = VRF_BITMAP_BIT_OFFSET(vrf_id);
7076bb2f 407
d62a17ae 408 if (bmap == VRF_BITMAP_NULL || vrf_id == VRF_UNKNOWN
409 || bm->groups[group] == NULL)
410 return 0;
7076bb2f 411
d62a17ae 412 return CHECK_FLAG(bm->groups[group][VRF_BITMAP_INDEX_IN_GROUP(offset)],
413 VRF_BITMAP_FLAG(offset))
414 ? 1
415 : 0;
7076bb2f
FL
416}
417
d62a17ae 418static void vrf_autocomplete(vector comps, struct cmd_token *token)
d617d5fe 419{
d62a17ae 420 struct vrf *vrf = NULL;
d617d5fe 421
a2addae8 422 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
a9ff90c4 423 if (vrf->vrf_id != VRF_DEFAULT)
d62a17ae 424 vector_set(comps, XSTRDUP(MTYPE_COMPLETION, vrf->name));
425 }
d617d5fe
DS
426}
427
428static const struct cmd_variable_handler vrf_var_handlers[] = {
d62a17ae 429 {
430 .varname = "vrf",
431 .completions = vrf_autocomplete,
432 },
433 {.completions = NULL},
d617d5fe
DS
434};
435
b72ede27 436/* Initialize VRF module. */
d62a17ae 437void vrf_init(int (*create)(struct vrf *), int (*enable)(struct vrf *),
438 int (*disable)(struct vrf *), int (*delete)(struct vrf *))
439{
440 struct vrf *default_vrf;
441
e26aedbe
PG
442 /* initialise NS, in case VRF backend if NETNS */
443 ns_init();
d62a17ae 444 if (debug_vrf)
445 zlog_debug("%s: Initializing VRF subsystem",
446 __PRETTY_FUNCTION__);
447
448 vrf_master.vrf_new_hook = create;
449 vrf_master.vrf_enable_hook = enable;
450 vrf_master.vrf_disable_hook = disable;
451 vrf_master.vrf_delete_hook = delete;
452
453 /* The default VRF always exists. */
454 default_vrf = vrf_get(VRF_DEFAULT, VRF_DEFAULT_NAME);
455 if (!default_vrf) {
456 zlog_err("vrf_init: failed to create the default VRF!");
457 exit(1);
458 }
459
460 /* Enable the default VRF. */
461 if (!vrf_enable(default_vrf)) {
462 zlog_err("vrf_init: failed to enable the default VRF!");
463 exit(1);
464 }
465
466 cmd_variable_handler_register(vrf_var_handlers);
b72ede27
FL
467}
468
469/* Terminate VRF module. */
d62a17ae 470void vrf_terminate(void)
b72ede27 471{
d62a17ae 472 struct vrf *vrf;
b72ede27 473
d62a17ae 474 if (debug_vrf)
475 zlog_debug("%s: Shutting down vrf subsystem",
476 __PRETTY_FUNCTION__);
19dc275e 477
55cd0f61
DS
478 while (!RB_EMPTY(vrf_id_head, &vrfs_by_id)) {
479 vrf = RB_ROOT(vrf_id_head, &vrfs_by_id);
480
65c3a7c4 481 /* Clear configured flag and invoke delete. */
482 UNSET_FLAG(vrf->status, VRF_CONFIGURED);
d62a17ae 483 vrf_delete(vrf);
65c3a7c4 484 }
55cd0f61
DS
485
486 while (!RB_EMPTY(vrf_name_head, &vrfs_by_name)) {
487 vrf = RB_ROOT(vrf_name_head, &vrfs_by_name);
488
65c3a7c4 489 /* Clear configured flag and invoke delete. */
490 UNSET_FLAG(vrf->status, VRF_CONFIGURED);
d62a17ae 491 vrf_delete(vrf);
65c3a7c4 492 }
b72ede27
FL
493}
494
e5bf3e1e 495/* Create a socket for the VRF. */
d62a17ae 496int vrf_socket(int domain, int type, int protocol, vrf_id_t vrf_id)
e5bf3e1e 497{
2e0d2b3d 498 int ret, save_errno, ret2;
e5bf3e1e 499
2e0d2b3d
PG
500 ret = vrf_switch_to_netns(vrf_id);
501 if (ret < 0)
502 zlog_err("%s: Can't switch to VRF %u (%s)",
503 __func__, vrf_id, safe_strerror(errno));
d62a17ae 504 ret = socket(domain, type, protocol);
2e0d2b3d
PG
505 save_errno = errno;
506 ret2 = vrf_switchback_to_initial();
507 if (ret2 < 0)
508 zlog_err("%s: Can't switchback from VRF %u (%s)",
509 __func__, vrf_id, safe_strerror(errno));
510 errno = save_errno;
d62a17ae 511 return ret;
e5bf3e1e
FL
512}
513
78dd30b2
PG
514int vrf_is_backend_netns(void)
515{
516 return (vrf_backend == VRF_BACKEND_NETNS);
517}
518
519int vrf_get_backend(void)
520{
521 return vrf_backend;
522}
523
524void vrf_configure_backend(int vrf_backend_netns)
525{
526 vrf_backend = vrf_backend_netns;
527}
528
697d3ec7 529int vrf_handler_create(struct vty *vty, const char *vrfname, struct vrf **vrf)
f30c50b9 530{
d62a17ae 531 struct vrf *vrfp;
f30c50b9 532
d62a17ae 533 if (strlen(vrfname) > VRF_NAMSIZ) {
697d3ec7
PG
534 if (vty)
535 vty_out(vty,
536 "%% VRF name %s invalid: length exceeds %d bytes\n",
537 vrfname, VRF_NAMSIZ);
538 else
539 zlog_warn(
540 "%% VRF name %s invalid: length exceeds %d bytes\n",
541 vrfname, VRF_NAMSIZ);
d62a17ae 542 return CMD_WARNING_CONFIG_FAILED;
543 }
f30c50b9 544
d62a17ae 545 vrfp = vrf_get(VRF_UNKNOWN, vrfname);
f30c50b9 546
697d3ec7
PG
547 if (vty)
548 VTY_PUSH_CONTEXT(VRF_NODE, vrfp);
f30c50b9 549
697d3ec7
PG
550 if (vrf)
551 *vrf = vrfp;
d62a17ae 552 return CMD_SUCCESS;
f30c50b9
RW
553}
554
e26aedbe
PG
555int vrf_netns_handler_create(struct vty *vty, struct vrf *vrf,
556 char *pathname, ns_id_t ns_id)
557{
558 struct ns *ns = NULL;
559
560 if (!vrf)
561 return CMD_WARNING_CONFIG_FAILED;
562 if (vrf->vrf_id != VRF_UNKNOWN && vrf->ns_ctxt == NULL) {
563 if (vty)
564 vty_out(vty,
565 "VRF %u is already configured with VRF %s\n",
566 vrf->vrf_id, vrf->name);
567 else
568 zlog_warn("VRF %u is already configured with VRF %s\n",
569 vrf->vrf_id, vrf->name);
570 return CMD_WARNING_CONFIG_FAILED;
571 }
572 if (vrf->ns_ctxt != NULL) {
573 ns = (struct ns *) vrf->ns_ctxt;
574 if (ns && 0 != strcmp(ns->name, pathname)) {
575 if (vty)
576 vty_out(vty,
577 "VRF %u already configured with NETNS %s\n",
578 vrf->vrf_id, ns->name);
579 else
580 zlog_warn(
581 "VRF %u already configured with NETNS %s",
582 vrf->vrf_id, ns->name);
583 return CMD_WARNING_CONFIG_FAILED;
584 }
585 }
586 ns = ns_lookup_name(pathname);
587 if (ns && ns->vrf_ctxt) {
588 struct vrf *vrf2 = (struct vrf *)ns->vrf_ctxt;
589
590 if (vrf2 == vrf)
591 return CMD_SUCCESS;
592 if (vty)
593 vty_out(vty, "NS %s is already configured"
594 " with VRF %u(%s)\n",
595 ns->name, vrf2->vrf_id, vrf2->name);
596 else
597 zlog_warn("NS %s is already configured with VRF %u(%s)",
598 ns->name, vrf2->vrf_id, vrf2->name);
599 return CMD_WARNING_CONFIG_FAILED;
600 }
601 ns = ns_get_created(ns, pathname, ns_id);
602 ns->vrf_ctxt = (void *)vrf;
603 vrf->ns_ctxt = (void *)ns;
604 /* update VRF netns NAME */
605 if (vrf)
606 strlcpy(vrf->data.l.netns_name, basename(pathname), NS_NAMSIZ);
607
608 if (!ns_enable(ns, vrf_update_vrf_id)) {
609 if (vty)
610 vty_out(vty, "Can not associate NS %u with NETNS %s\n",
611 ns->ns_id, ns->name);
612 else
613 zlog_warn("Can not associate NS %u with NETNS %s",
614 ns->ns_id, ns->name);
615 return CMD_WARNING_CONFIG_FAILED;
616 }
617
618 return CMD_SUCCESS;
619}
620
ce1be369
PG
621int vrf_is_mapped_on_netns(vrf_id_t vrf_id)
622{
623 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
624
625 if (!vrf || vrf->data.l.netns_name[0] == '\0')
626 return 0;
627 if (vrf->vrf_id == VRF_DEFAULT)
628 return 0;
629 return 1;
630}
631
697d3ec7
PG
632/* vrf CLI commands */
633DEFUN_NOSH (vrf,
634 vrf_cmd,
635 "vrf NAME",
636 "Select a VRF to configure\n"
637 "VRF's name\n")
638{
639 int idx_name = 1;
640 const char *vrfname = argv[idx_name]->arg;
641
642 return vrf_handler_create(vty, vrfname, NULL);
643}
644
f30c50b9
RW
645DEFUN_NOSH (no_vrf,
646 no_vrf_cmd,
d7a75a6c 647 "no vrf NAME",
f30c50b9
RW
648 NO_STR
649 "Delete a pseudo VRF's configuration\n"
650 "VRF's name\n")
651{
d62a17ae 652 const char *vrfname = argv[2]->arg;
53dc2b05 653
d62a17ae 654 struct vrf *vrfp;
f30c50b9 655
d62a17ae 656 vrfp = vrf_lookup_by_name(vrfname);
f30c50b9 657
d62a17ae 658 if (vrfp == NULL) {
659 vty_out(vty, "%% VRF %s does not exist\n", vrfname);
660 return CMD_WARNING_CONFIG_FAILED;
661 }
f30c50b9 662
d62a17ae 663 if (CHECK_FLAG(vrfp->status, VRF_ACTIVE)) {
664 vty_out(vty, "%% Only inactive VRFs can be deleted\n");
665 return CMD_WARNING_CONFIG_FAILED;
666 }
f30c50b9 667
84915b0a 668 /* Clear configured flag and invoke delete. */
669 UNSET_FLAG(vrfp->status, VRF_CONFIGURED);
d62a17ae 670 vrf_delete(vrfp);
f30c50b9 671
d62a17ae 672 return CMD_SUCCESS;
f30c50b9
RW
673}
674
53dc2b05 675
d62a17ae 676struct cmd_node vrf_node = {VRF_NODE, "%s(config-vrf)# ", 1};
7ddcfca4 677
e26aedbe
PG
678DEFUN_NOSH (vrf_netns,
679 vrf_netns_cmd,
680 "netns NAME",
681 "Attach VRF to a Namespace\n"
682 "The file name in " NS_RUN_DIR ", or a full pathname\n")
683{
684 int idx_name = 1;
685 char *pathname = ns_netns_pathname(vty, argv[idx_name]->arg);
686
687 VTY_DECLVAR_CONTEXT(vrf, vrf);
688
689 if (!pathname)
690 return CMD_WARNING_CONFIG_FAILED;
691 return vrf_netns_handler_create(vty, vrf, pathname, NS_UNKNOWN);
692}
693
694DEFUN (no_vrf_netns,
695 no_vrf_netns_cmd,
696 "no netns [NAME]",
697 NO_STR
698 "Detach VRF from a Namespace\n"
699 "The file name in " NS_RUN_DIR ", or a full pathname\n")
700{
701 struct ns *ns = NULL;
702
703 VTY_DECLVAR_CONTEXT(vrf, vrf);
704
705 if (!vrf_is_backend_netns()) {
706 vty_out(vty, "VRF backend is not Netns. Aborting\n");
707 return CMD_WARNING_CONFIG_FAILED;
708 }
709 if (!vrf->ns_ctxt) {
710 vty_out(vty, "VRF %s(%u) is not configured with NetNS\n",
711 vrf->name, vrf->vrf_id);
712 return CMD_WARNING_CONFIG_FAILED;
713 }
714
715 ns = (struct ns *)vrf->ns_ctxt;
716
717 ns->vrf_ctxt = NULL;
718 vrf_disable(vrf);
719 /* vrf ID from VRF is necessary for Zebra
720 * so that propagate to other clients is done
721 */
722 ns_delete(ns);
723 vrf->ns_ctxt = NULL;
724 return CMD_SUCCESS;
725}
726
19dc275e
DS
727/*
728 * Debug CLI for vrf's
729 */
730DEFUN (vrf_debug,
731 vrf_debug_cmd,
732 "debug vrf",
733 DEBUG_STR
734 "VRF Debugging\n")
735{
d62a17ae 736 debug_vrf = 1;
19dc275e 737
d62a17ae 738 return CMD_SUCCESS;
19dc275e
DS
739}
740
741DEFUN (no_vrf_debug,
742 no_vrf_debug_cmd,
743 "no debug vrf",
744 NO_STR
745 DEBUG_STR
746 "VRF Debugging\n")
747{
d62a17ae 748 debug_vrf = 0;
19dc275e 749
d62a17ae 750 return CMD_SUCCESS;
19dc275e
DS
751}
752
d62a17ae 753static int vrf_write_host(struct vty *vty)
19dc275e 754{
d62a17ae 755 if (debug_vrf)
756 vty_out(vty, "debug vrf\n");
19dc275e 757
d62a17ae 758 return 1;
19dc275e
DS
759}
760
d62a17ae 761static struct cmd_node vrf_debug_node = {VRF_DEBUG_NODE, "", 1};
19dc275e 762
d62a17ae 763void vrf_install_commands(void)
19dc275e 764{
d62a17ae 765 install_node(&vrf_debug_node, vrf_write_host);
19dc275e 766
d62a17ae 767 install_element(CONFIG_NODE, &vrf_debug_cmd);
768 install_element(ENABLE_NODE, &vrf_debug_cmd);
769 install_element(CONFIG_NODE, &no_vrf_debug_cmd);
770 install_element(ENABLE_NODE, &no_vrf_debug_cmd);
19dc275e 771}
53dc2b05 772
d62a17ae 773void vrf_cmd_init(int (*writefunc)(struct vty *vty))
7ddcfca4 774{
d62a17ae 775 install_element(CONFIG_NODE, &vrf_cmd);
776 install_element(CONFIG_NODE, &no_vrf_cmd);
777 install_node(&vrf_node, writefunc);
778 install_default(VRF_NODE);
e26aedbe
PG
779 if (vrf_is_backend_netns() && ns_have_netns()) {
780 /* Install NS commands. */
781 install_element(VRF_NODE, &vrf_netns_cmd);
782 install_element(VRF_NODE, &no_vrf_netns_cmd);
783 }
19dc275e 784}
ec31f30d
PG
785
786vrf_id_t vrf_get_default_id(void)
787{
788 struct vrf *vrf = vrf_lookup_by_name(VRF_DEFAULT_NAME);
789
790 if (vrf)
791 return vrf->vrf_id;
792 if (vrf_is_backend_netns())
793 return ns_get_default_id();
794 else
795 return VRF_DEFAULT_INTERNAL;
796}
2e0d2b3d
PG
797
798int vrf_getaddrinfo(const char *node, const char *service,
799 const struct addrinfo *hints,
800 struct addrinfo **res, vrf_id_t vrf_id)
801{
802 int ret, ret2, save_errno;
803
804 ret = vrf_switch_to_netns(vrf_id);
805 if (ret < 0)
806 zlog_err("%s: Can't switch to VRF %u (%s)",
807 __func__, vrf_id, safe_strerror(errno));
808 ret = getaddrinfo(node, service, hints, res);
809 save_errno = errno;
810 ret2 = vrf_switchback_to_initial();
811 if (ret2 < 0)
812 zlog_err("%s: Can't switchback from VRF %u (%s)",
813 __func__, vrf_id, safe_strerror(errno));
814 errno = save_errno;
815 return ret;
816}
817
818int vrf_sockunion_socket(const union sockunion *su, vrf_id_t vrf_id)
819{
820 int ret, save_errno, ret2;
821
822 ret = vrf_switch_to_netns(vrf_id);
823 if (ret < 0)
824 zlog_err("%s: Can't switch to VRF %u (%s)",
825 __func__, vrf_id, safe_strerror(errno));
826 ret = sockunion_socket(su);
827 save_errno = errno;
828 ret2 = vrf_switchback_to_initial();
829 if (ret2 < 0)
830 zlog_err("%s: Can't switchback from VRF %u (%s)",
831 __func__, vrf_id, safe_strerror(errno));
832 errno = save_errno;
833 return ret;
834}