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