]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_vrf.c
Merge pull request #11911 from mruprich/ospf-api
[mirror_frr.git] / zebra / zebra_vrf.c
CommitLineData
7c551956
DS
1/*
2 * Copyright (C) 2016 CumulusNetworks
3 * Donald Sharp
4 *
5 * This file is part of Quagga
6 *
7 * Quagga is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * Quagga 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
7c551956
DS
20 */
21#include <zebra.h>
22
62d89c64
IR
23/* for basename */
24#include <libgen.h>
25
7c551956
DS
26#include "log.h"
27#include "linklist.h"
f30c50b9 28#include "command.h"
4a1ab8e4 29#include "memory.h"
05737783 30#include "srcdest_table.h"
78dd30b2 31#include "vrf.h"
82f97584 32#include "vty.h"
78dd30b2 33
89272910 34#include "zebra/zebra_router.h"
df9c8c57 35#include "zebra/rtadv.h"
7c551956 36#include "zebra/debug.h"
bf094f69 37#include "zebra/zapi_msg.h"
7c551956
DS
38#include "zebra/rib.h"
39#include "zebra/zebra_vrf.h"
5a8dfcd8 40#include "zebra/zebra_rnh.h"
7c551956 41#include "zebra/router-id.h"
5a8dfcd8 42#include "zebra/interface.h"
7758e3f3 43#include "zebra/zebra_mpls.h"
13d60d35 44#include "zebra/zebra_vxlan.h"
3bc34908 45#include "zebra/zebra_netns_notify.h"
7cf16e19 46#include "zebra/zebra_routemap.h"
37cb0475
IR
47#ifndef VTYSH_EXTRACT_PL
48#include "zebra/zebra_vrf_clippy.c"
49#endif
42d4b30e 50#include "zebra/table_manager.h"
7c551956 51
9d97533e 52static void zebra_vrf_table_create(struct zebra_vrf *zvrf, afi_t afi,
53 safi_t safi);
54static void zebra_rnhtable_node_cleanup(struct route_table *table,
55 struct route_node *node);
56
bf8d3d6a
DL
57DEFINE_MTYPE_STATIC(ZEBRA, ZEBRA_VRF, "ZEBRA VRF");
58DEFINE_MTYPE_STATIC(ZEBRA, OTHER_TABLE, "Other Table");
d8612e65 59
7c551956 60/* VRF information update. */
d62a17ae 61static void zebra_vrf_add_update(struct zebra_vrf *zvrf)
7c551956 62{
d62a17ae 63 struct listnode *node, *nnode;
64 struct zserv *client;
7c551956 65
d62a17ae 66 if (IS_ZEBRA_DEBUG_EVENT)
67 zlog_debug("MESSAGE: ZEBRA_VRF_ADD %s", zvrf_name(zvrf));
7c551956 68
17da84a4
KS
69 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
70 /* Do not send unsolicited messages to synchronous clients. */
71 if (client->synchronous)
72 continue;
73
d62a17ae 74 zsend_vrf_add(client, zvrf);
17da84a4 75 }
7c551956
DS
76}
77
d62a17ae 78static void zebra_vrf_delete_update(struct zebra_vrf *zvrf)
7c551956 79{
d62a17ae 80 struct listnode *node, *nnode;
81 struct zserv *client;
7c551956 82
d62a17ae 83 if (IS_ZEBRA_DEBUG_EVENT)
84 zlog_debug("MESSAGE: ZEBRA_VRF_DELETE %s", zvrf_name(zvrf));
7c551956 85
17da84a4
KS
86 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
87 /* Do not send unsolicited messages to synchronous clients. */
88 if (client->synchronous)
89 continue;
90
d62a17ae 91 zsend_vrf_delete(client, zvrf);
17da84a4 92 }
7c551956
DS
93}
94
d62a17ae 95void zebra_vrf_update_all(struct zserv *client)
7c551956 96{
d62a17ae 97 struct vrf *vrf;
7c551956 98
a2addae8 99 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
4691b65a 100 if (vrf->vrf_id != VRF_UNKNOWN)
d62a17ae 101 zsend_vrf_add(client, vrf_info_lookup(vrf->vrf_id));
102 }
7c551956
DS
103}
104
105/* Callback upon creating a new VRF. */
d62a17ae 106static int zebra_vrf_new(struct vrf *vrf)
7c551956 107{
d62a17ae 108 struct zebra_vrf *zvrf;
7c551956 109
d62a17ae 110 if (IS_ZEBRA_DEBUG_EVENT)
14a4d9d0 111 zlog_debug("VRF %s created, id %u", vrf->name, vrf->vrf_id);
7c551956 112
ec64a634 113 zvrf = zebra_vrf_alloc(vrf);
91b1421e
PG
114 if (!vrf_is_backend_netns())
115 zvrf->zns = zebra_ns_lookup(NS_DEFAULT);
d8612e65
DS
116
117 otable_init(&zvrf->other_tables);
118
fbb65ff5 119 router_id_init(zvrf);
42d4b30e
PG
120
121 /* Initiate Table Manager per ZNS */
122 table_manager_enable(zvrf);
123
d62a17ae 124 return 0;
7c551956
DS
125}
126
127/* Callback upon enabling a VRF. */
d62a17ae 128static int zebra_vrf_enable(struct vrf *vrf)
7c551956 129{
d62a17ae 130 struct zebra_vrf *zvrf = vrf->info;
9d97533e 131 struct route_table *table;
d62a17ae 132 afi_t afi;
133 safi_t safi;
134
135 assert(zvrf);
84915b0a 136 if (IS_ZEBRA_DEBUG_EVENT)
996c9314
LB
137 zlog_debug("VRF %s id %u is now active", zvrf_name(zvrf),
138 zvrf_id(zvrf));
d62a17ae 139
fbb65ff5
PG
140 if (vrf_is_backend_netns())
141 zvrf->zns = zebra_ns_lookup((ns_id_t)vrf->vrf_id);
142 else
143 zvrf->zns = zebra_ns_lookup(NS_DEFAULT);
7ca9c407 144
7c2ddfb9 145 rtadv_vrf_init(zvrf);
df9c8c57 146
84915b0a 147 /* Inform clients that the VRF is now active. This is an
148 * add for the clients.
149 */
d62a17ae 150
8288a24f 151 zebra_vrf_add_update(zvrf);
9d97533e 152 /* Allocate tables */
153 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
154 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++)
155 zebra_vrf_table_create(zvrf, afi, safi);
156
157 table = route_table_init();
158 table->cleanup = zebra_rnhtable_node_cleanup;
159 zvrf->rnh_table[afi] = table;
160
161 table = route_table_init();
162 table->cleanup = zebra_rnhtable_node_cleanup;
a4598b97 163 zvrf->rnh_table_multicast[afi] = table;
9d97533e 164 }
165
84915b0a 166 /* Kick off any VxLAN-EVPN processing. */
167 zebra_vxlan_vrf_enable(zvrf);
168
d62a17ae 169 return 0;
7c551956
DS
170}
171
172/* Callback upon disabling a VRF. */
d62a17ae 173static int zebra_vrf_disable(struct vrf *vrf)
7c551956 174{
d62a17ae 175 struct zebra_vrf *zvrf = vrf->info;
9d97533e 176 struct interface *ifp;
d62a17ae 177 afi_t afi;
178 safi_t safi;
179
84915b0a 180 assert(zvrf);
181 if (IS_ZEBRA_DEBUG_EVENT)
996c9314
LB
182 zlog_debug("VRF %s id %u is now inactive", zvrf_name(zvrf),
183 zvrf_id(zvrf));
d62a17ae 184
84915b0a 185 /* Stop any VxLAN-EVPN processing. */
186 zebra_vxlan_vrf_disable(zvrf);
d62a17ae 187
aab5893a 188 rtadv_vrf_terminate(zvrf);
df9c8c57 189
84915b0a 190 /* Inform clients that the VRF is now inactive. This is a
191 * delete for the clients.
192 */
d62a17ae 193 zebra_vrf_delete_update(zvrf);
194
9d97533e 195 /* If asked to retain routes, there's nothing more to do. */
196 if (CHECK_FLAG(zvrf->flags, ZEBRA_VRF_RETAIN))
197 return 0;
198
199 /* Remove all routes. */
200 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
41dc8c14
DS
201 route_table_finish(zvrf->rnh_table[afi]);
202 zvrf->rnh_table[afi] = NULL;
a4598b97
DS
203 route_table_finish(zvrf->rnh_table_multicast[afi]);
204 zvrf->rnh_table_multicast[afi] = NULL;
41dc8c14 205
9d97533e 206 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++)
207 rib_close_table(zvrf->table[afi][safi]);
9d97533e 208 }
209
210 /* Cleanup Vxlan, MPLS and PW tables. */
211 zebra_vxlan_cleanup_tables(zvrf);
212 zebra_mpls_cleanup_tables(zvrf);
213 zebra_pw_exit(zvrf);
214
996c9314
LB
215 /* Remove link-local IPv4 addresses created for BGP unnumbered peering.
216 */
9d97533e 217 FOR_ALL_INTERFACES (vrf, ifp)
218 if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(ifp);
219
220 /* clean-up work queues */
a310ebc1 221 meta_queue_free(zrouter.mq, zvrf);
7c551956 222
9d97533e 223 /* Cleanup (free) routing tables and NHT tables. */
224 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
89272910
DS
225 /*
226 * Set the table pointer to NULL as that
227 * we no-longer need a copy of it, nor do we
228 * own this data, the zebra_router structure
229 * owns these tables. Once we've cleaned up the
230 * table, see rib_close_table above
231 * we no-longer need this pointer.
232 */
bd4fb615
DS
233 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++) {
234 zebra_router_release_table(zvrf, zvrf->table_id, afi,
235 safi);
9d97533e 236 zvrf->table[afi][safi] = NULL;
bd4fb615 237 }
5a8dfcd8
RW
238 }
239
d62a17ae 240 return 0;
7c551956
DS
241}
242
d62a17ae 243static int zebra_vrf_delete(struct vrf *vrf)
7c551956 244{
d62a17ae 245 struct zebra_vrf *zvrf = vrf->info;
d8612e65 246 struct other_route_table *otable;
d62a17ae 247
248 assert(zvrf);
84915b0a 249 if (IS_ZEBRA_DEBUG_EVENT)
996c9314
LB
250 zlog_debug("VRF %s id %u deleted", zvrf_name(zvrf),
251 zvrf_id(zvrf));
5a8dfcd8 252
49df0815
IR
253 table_manager_disable(zvrf);
254
d62a17ae 255 /* clean-up work queues */
a310ebc1 256 meta_queue_free(zrouter.mq, zvrf);
5a8dfcd8 257
84915b0a 258 /* Free Vxlan and MPLS. */
259 zebra_vxlan_close_tables(zvrf);
260 zebra_mpls_close_tables(zvrf);
261
d8612e65
DS
262 otable = otable_pop(&zvrf->other_tables);
263 while (otable) {
264 zebra_router_release_table(zvrf, otable->table_id,
265 otable->afi, otable->safi);
266 XFREE(MTYPE_OTHER_TABLE, otable);
267
268 otable = otable_pop(&zvrf->other_tables);
269 }
270
84915b0a 271 /* Cleanup EVPN states for vrf */
b7cfce93
MK
272 zebra_vxlan_vrf_delete(zvrf);
273
d62a17ae 274 list_delete_all_node(zvrf->rid_all_sorted_list);
275 list_delete_all_node(zvrf->rid_lo_sorted_list);
d8612e65 276
cdc09a4b
MS
277 list_delete_all_node(zvrf->rid6_all_sorted_list);
278 list_delete_all_node(zvrf->rid6_lo_sorted_list);
279
d8612e65 280 otable_fini(&zvrf->other_tables);
d62a17ae 281 XFREE(MTYPE_ZEBRA_VRF, zvrf);
282 vrf->info = NULL;
5a8dfcd8 283
d62a17ae 284 return 0;
7c551956
DS
285}
286
287/* Lookup the routing table in a VRF based on both VRF-Id and table-id.
e9748a89
PG
288 * NOTE: Table-id is relevant on two modes:
289 * - case VRF backend is default : on default VRF only
290 * - case VRF backend is netns : on all VRFs
7c551956 291 */
c7c0b007
SW
292struct route_table *zebra_vrf_lookup_table_with_table_id(afi_t afi, safi_t safi,
293 vrf_id_t vrf_id,
294 uint32_t table_id)
7c551956 295{
8ab39b7f 296 struct zebra_vrf *zvrf = vrf_info_lookup(vrf_id);
d8612e65 297 struct other_route_table ort, *otable;
8ab39b7f
DS
298
299 if (!zvrf)
300 return NULL;
d62a17ae 301
302 if (afi >= AFI_MAX || safi >= SAFI_MAX)
303 return NULL;
304
8ab39b7f
DS
305 if (table_id == zvrf->table_id)
306 return zebra_vrf_table(afi, safi, vrf_id);
d62a17ae 307
d8612e65
DS
308 ort.afi = afi;
309 ort.safi = safi;
310 ort.table_id = table_id;
311 otable = otable_find(&zvrf->other_tables, &ort);
c7c0b007 312
d8612e65
DS
313 if (otable)
314 return otable->table;
315
c7c0b007
SW
316 return NULL;
317}
318
319struct route_table *zebra_vrf_get_table_with_table_id(afi_t afi, safi_t safi,
320 vrf_id_t vrf_id,
321 uint32_t table_id)
322{
323 struct zebra_vrf *zvrf = vrf_info_lookup(vrf_id);
324 struct other_route_table *otable;
325 struct route_table *table;
326
327 table = zebra_vrf_lookup_table_with_table_id(afi, safi, vrf_id,
328 table_id);
329
330 if (table)
331 goto done;
332
333 /* Create it as an `other` table */
d8612e65
DS
334 table = zebra_router_get_table(zvrf, table_id, afi, safi);
335
336 otable = XCALLOC(MTYPE_OTHER_TABLE, sizeof(*otable));
337 otable->afi = afi;
338 otable->safi = safi;
339 otable->table_id = table_id;
340 otable->table = table;
341 otable_add(&zvrf->other_tables, otable);
d62a17ae 342
c7c0b007 343done:
d62a17ae 344 return table;
7c551956
DS
345}
346
d62a17ae 347static void zebra_rnhtable_node_cleanup(struct route_table *table,
348 struct route_node *node)
5a8dfcd8 349{
d62a17ae 350 if (node->info)
351 zebra_free_rnh(node->info);
5a8dfcd8
RW
352}
353
7c551956
DS
354/*
355 * Create a routing table for the specific AFI/SAFI in the given VRF.
356 */
d62a17ae 357static void zebra_vrf_table_create(struct zebra_vrf *zvrf, afi_t afi,
358 safi_t safi)
7c551956 359{
c86ba6c2
DS
360 struct route_node *rn;
361 struct prefix p;
362
d62a17ae 363 assert(!zvrf->table[afi][safi]);
364
ea66cec4
DS
365 zvrf->table[afi][safi] =
366 zebra_router_get_table(zvrf, zvrf->table_id, afi, safi);
c86ba6c2
DS
367
368 memset(&p, 0, sizeof(p));
369 p.family = afi2family(afi);
370
371 rn = srcdest_rnode_get(zvrf->table[afi][safi], &p, NULL);
372 zebra_rib_create_dest(rn);
7c551956
DS
373}
374
375/* Allocate new zebra VRF. */
ec64a634 376struct zebra_vrf *zebra_vrf_alloc(struct vrf *vrf)
7c551956 377{
d62a17ae 378 struct zebra_vrf *zvrf;
d62a17ae 379
380 zvrf = XCALLOC(MTYPE_ZEBRA_VRF, sizeof(struct zebra_vrf));
381
ec64a634
DS
382 zvrf->vrf = vrf;
383 vrf->info = zvrf;
384
d62a17ae 385 zebra_vxlan_init_tables(zvrf);
386 zebra_mpls_init_tables(zvrf);
6833ae01 387 zebra_pw_init(zvrf);
e9748a89
PG
388 zvrf->table_id = RT_TABLE_MAIN;
389 /* by default table ID is default one */
d62a17ae 390 return zvrf;
7c551956
DS
391}
392
393/* Lookup VRF by identifier. */
d62a17ae 394struct zebra_vrf *zebra_vrf_lookup_by_id(vrf_id_t vrf_id)
7c551956 395{
d62a17ae 396 return vrf_info_lookup(vrf_id);
7c551956
DS
397}
398
51bdc5f8 399/* Lookup VRF by name. */
d62a17ae 400struct zebra_vrf *zebra_vrf_lookup_by_name(const char *name)
871d39b3 401{
d62a17ae 402 struct vrf *vrf;
871d39b3 403
d62a17ae 404 if (!name)
405 name = VRF_DEFAULT_NAME;
a3d21ef3 406
d62a17ae 407 vrf = vrf_lookup_by_name(name);
408 if (vrf)
409 return ((struct zebra_vrf *)vrf->info);
51bdc5f8 410
d62a17ae 411 return NULL;
871d39b3
DS
412}
413
7c551956 414/* Lookup the routing table in an enabled VRF. */
d62a17ae 415struct route_table *zebra_vrf_table(afi_t afi, safi_t safi, vrf_id_t vrf_id)
7c551956 416{
d62a17ae 417 struct zebra_vrf *zvrf = vrf_info_lookup(vrf_id);
7c551956 418
d62a17ae 419 if (!zvrf)
420 return NULL;
7c551956 421
d62a17ae 422 if (afi >= AFI_MAX || safi >= SAFI_MAX)
423 return NULL;
7c551956 424
d62a17ae 425 return zvrf->table[afi][safi];
7c551956
DS
426}
427
d62a17ae 428static int vrf_config_write(struct vty *vty)
f30c50b9 429{
d62a17ae 430 struct vrf *vrf;
431 struct zebra_vrf *zvrf;
432
a2addae8 433 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
d62a17ae 434 zvrf = vrf->info;
1e9f448f
DS
435
436 if (!zvrf)
437 continue;
438
a5654735
MK
439 if (zvrf_id(zvrf) == VRF_DEFAULT) {
440 if (zvrf->l3vni)
8d0f01f1
CS
441 vty_out(vty, "vni %u%s\n", zvrf->l3vni,
442 is_l3vni_for_prefix_routes_only(
443 zvrf->l3vni)
444 ? " prefix-routes-only"
445 : "");
5a0bdc78
PG
446 if (zvrf->zebra_rnh_ip_default_route)
447 vty_out(vty, "ip nht resolve-via-default\n");
448
449 if (zvrf->zebra_rnh_ipv6_default_route)
450 vty_out(vty, "ipv6 nht resolve-via-default\n");
42d4b30e
PG
451
452 if (zvrf->tbl_mgr
453 && (zvrf->tbl_mgr->start || zvrf->tbl_mgr->end))
454 vty_out(vty, "ip table range %u %u\n",
455 zvrf->tbl_mgr->start,
456 zvrf->tbl_mgr->end);
c319e19d
QY
457 } else {
458 vty_frame(vty, "vrf %s\n", zvrf_name(zvrf));
22bd3e94 459 if (zvrf->l3vni)
996c9314
LB
460 vty_out(vty, " vni %u%s\n", zvrf->l3vni,
461 is_l3vni_for_prefix_routes_only(
462 zvrf->l3vni)
463 ? " prefix-routes-only"
464 : "");
b95c1883 465 zebra_ns_config_write(vty, (struct ns *)vrf->ns_ctxt);
5a0bdc78
PG
466 if (zvrf->zebra_rnh_ip_default_route)
467 vty_out(vty, " ip nht resolve-via-default\n");
468
469 if (zvrf->zebra_rnh_ipv6_default_route)
470 vty_out(vty, " ipv6 nht resolve-via-default\n");
42d4b30e
PG
471
472 if (zvrf->tbl_mgr && vrf_is_backend_netns()
473 && (zvrf->tbl_mgr->start || zvrf->tbl_mgr->end))
474 vty_out(vty, " ip table range %u %u\n",
475 zvrf->tbl_mgr->start,
476 zvrf->tbl_mgr->end);
22bd3e94 477 }
37728041 478
5a0bdc78 479
7cf16e19 480 zebra_routemap_config_write_protocol(vty, zvrf);
03fba42e 481 router_id_write(vty, zvrf);
7cf16e19 482
c319e19d 483 if (zvrf_id(zvrf) != VRF_DEFAULT)
07679ad9 484 vty_endframe(vty, "exit-vrf\n!\n");
7cf16e19 485 else
486 vty_out(vty, "!\n");
d62a17ae 487 }
488 return 0;
f30c50b9
RW
489}
490
37cb0475
IR
491DEFPY (vrf_netns,
492 vrf_netns_cmd,
493 "netns NAME$netns_name",
494 "Attach VRF to a Namespace\n"
495 "The file name in " NS_RUN_DIR ", or a full pathname\n")
496{
497 char *pathname = ns_netns_pathname(vty, netns_name);
498 int ret;
499
500 VTY_DECLVAR_CONTEXT(vrf, vrf);
501
502 if (!pathname)
503 return CMD_WARNING_CONFIG_FAILED;
504
505 frr_with_privs(&zserv_privs) {
62d89c64
IR
506 ret = zebra_vrf_netns_handler_create(
507 vty, vrf, pathname, NS_UNKNOWN, NS_UNKNOWN, NS_UNKNOWN);
37cb0475
IR
508 }
509
510 return ret;
511}
512
513DEFUN (no_vrf_netns,
514 no_vrf_netns_cmd,
515 "no netns [NAME]",
516 NO_STR
517 "Detach VRF from a Namespace\n"
518 "The file name in " NS_RUN_DIR ", or a full pathname\n")
519{
520 struct ns *ns = NULL;
521
522 VTY_DECLVAR_CONTEXT(vrf, vrf);
523
524 if (!vrf_is_backend_netns()) {
525 vty_out(vty, "VRF backend is not Netns. Aborting\n");
526 return CMD_WARNING_CONFIG_FAILED;
527 }
528 if (!vrf->ns_ctxt) {
529 vty_out(vty, "VRF %s(%u) is not configured with NetNS\n",
530 vrf->name, vrf->vrf_id);
531 return CMD_WARNING_CONFIG_FAILED;
532 }
533
534 ns = (struct ns *)vrf->ns_ctxt;
535
536 ns->vrf_ctxt = NULL;
537 vrf_disable(vrf);
538 /* vrf ID from VRF is necessary for Zebra
539 * so that propagate to other clients is done
540 */
541 ns_delete(ns);
542 vrf->ns_ctxt = NULL;
543 return CMD_SUCCESS;
544}
545
62d89c64
IR
546/* if ns_id is different and not VRF_UNKNOWN,
547 * then update vrf identifier, and enable VRF
548 */
549static void vrf_update_vrf_id(ns_id_t ns_id, void *opaqueptr)
550{
551 ns_id_t vrf_id = (vrf_id_t)ns_id;
552 vrf_id_t old_vrf_id;
553 struct vrf *vrf = (struct vrf *)opaqueptr;
554
555 if (!vrf)
556 return;
557 old_vrf_id = vrf->vrf_id;
558 if (vrf_id == vrf->vrf_id)
559 return;
560 if (vrf->vrf_id != VRF_UNKNOWN)
561 RB_REMOVE(vrf_id_head, &vrfs_by_id, vrf);
562 vrf->vrf_id = vrf_id;
563 RB_INSERT(vrf_id_head, &vrfs_by_id, vrf);
564 if (old_vrf_id == VRF_UNKNOWN)
565 vrf_enable(vrf);
566}
567
568int zebra_vrf_netns_handler_create(struct vty *vty, struct vrf *vrf,
569 char *pathname, ns_id_t ns_id,
570 ns_id_t internal_ns_id,
571 ns_id_t rel_def_ns_id)
572{
573 struct ns *ns = NULL;
574
575 if (!vrf)
576 return CMD_WARNING_CONFIG_FAILED;
577 if (vrf->vrf_id != VRF_UNKNOWN && vrf->ns_ctxt == NULL) {
578 if (vty)
579 vty_out(vty,
580 "VRF %u is already configured with VRF %s\n",
581 vrf->vrf_id, vrf->name);
582 else
583 zlog_info("VRF %u is already configured with VRF %s",
584 vrf->vrf_id, vrf->name);
585 return CMD_WARNING_CONFIG_FAILED;
586 }
587 if (vrf->ns_ctxt != NULL) {
588 ns = (struct ns *)vrf->ns_ctxt;
589 if (!strcmp(ns->name, pathname)) {
590 if (vty)
591 vty_out(vty,
592 "VRF %u already configured with NETNS %s\n",
593 vrf->vrf_id, ns->name);
594 else
595 zlog_info(
596 "VRF %u already configured with NETNS %s",
597 vrf->vrf_id, ns->name);
598 return CMD_WARNING_CONFIG_FAILED;
599 }
600 }
601 ns = ns_lookup_name(pathname);
602 if (ns && ns->vrf_ctxt) {
603 struct vrf *vrf2 = (struct vrf *)ns->vrf_ctxt;
604
605 if (vrf2 == vrf)
606 return CMD_SUCCESS;
607 if (vty)
608 vty_out(vty,
609 "NS %s is already configured with VRF %u(%s)\n",
610 ns->name, vrf2->vrf_id, vrf2->name);
611 else
612 zlog_info("NS %s is already configured with VRF %u(%s)",
613 ns->name, vrf2->vrf_id, vrf2->name);
614 return CMD_WARNING_CONFIG_FAILED;
615 }
616 ns = ns_get_created(ns, pathname, ns_id);
617 ns->internal_ns_id = internal_ns_id;
618 ns->relative_default_ns = rel_def_ns_id;
619 ns->vrf_ctxt = (void *)vrf;
620 vrf->ns_ctxt = (void *)ns;
621 /* update VRF netns NAME */
622 strlcpy(vrf->data.l.netns_name, basename(pathname), NS_NAMSIZ);
623
624 if (!ns_enable(ns, vrf_update_vrf_id)) {
625 if (vty)
626 vty_out(vty, "Can not associate NS %u with NETNS %s\n",
627 ns->ns_id, ns->name);
628 else
629 zlog_info("Can not associate NS %u with NETNS %s",
630 ns->ns_id, ns->name);
631 return CMD_WARNING_CONFIG_FAILED;
632 }
633
634 return CMD_SUCCESS;
635}
636
7c551956 637/* Zebra VRF initialization. */
d62a17ae 638void zebra_vrf_init(void)
7c551956 639{
996c9314 640 vrf_init(zebra_vrf_new, zebra_vrf_enable, zebra_vrf_disable,
ac2cb9bf 641 zebra_vrf_delete);
7c551956 642
903c6fa2
IR
643 hook_register(zserv_client_close, release_daemon_table_chunks);
644
cfc369c4 645 vrf_cmd_init(vrf_config_write);
37cb0475
IR
646
647 if (vrf_is_backend_netns() && ns_have_netns()) {
648 /* Install NS commands. */
649 install_element(VRF_NODE, &vrf_netns_cmd);
650 install_element(VRF_NODE, &no_vrf_netns_cmd);
651 }
7c551956 652}