]>
git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_vrf.c
2 * Copyright (C) 2016 CumulusNetworks
5 * This file is part of Quagga
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
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.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
28 #include "zebra/debug.h"
29 #include "zebra/zserv.h"
30 #include "zebra/rib.h"
31 #include "zebra/zebra_vrf.h"
32 #include "zebra/router-id.h"
33 #include "zebra/zebra_memory.h"
34 #include "zebra/zebra_static.h"
35 #include "zebra/zebra_mpls.h"
37 extern struct zebra_t zebrad
;
38 struct list
*zvrf_list
;
40 /* VRF information update. */
42 zebra_vrf_add_update (struct zebra_vrf
*zvrf
)
44 struct listnode
*node
, *nnode
;
47 if (IS_ZEBRA_DEBUG_EVENT
)
48 zlog_debug ("MESSAGE: ZEBRA_VRF_ADD %s", zvrf
->name
);
50 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
51 zsend_vrf_add (client
, zvrf
);
55 zebra_vrf_delete_update (struct zebra_vrf
*zvrf
)
57 struct listnode
*node
, *nnode
;
60 if (IS_ZEBRA_DEBUG_EVENT
)
61 zlog_debug ("MESSAGE: ZEBRA_VRF_DELETE %s", zvrf
->name
);
63 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
64 zsend_vrf_delete (client
, zvrf
);
68 zebra_vrf_update_all (struct zserv
*client
)
73 for (iter
= vrf_first (); iter
!= VRF_ITER_INVALID
; iter
= vrf_next (iter
))
75 if ((vrf
= vrf_iter2vrf (iter
)) && vrf
->vrf_id
)
76 zsend_vrf_add (client
, vrf_info_lookup (vrf
->vrf_id
));
80 /* Callback upon creating a new VRF. */
82 zebra_vrf_new (vrf_id_t vrf_id
, const char *name
, void **info
)
84 struct zebra_vrf
*zvrf
= *info
;
86 if (IS_ZEBRA_DEBUG_EVENT
)
87 zlog_info ("ZVRF %s with id %u", name
, vrf_id
);
91 zvrf
= zebra_vrf_list_lookup_by_name (name
);
94 zvrf
= zebra_vrf_alloc (vrf_id
, name
);
95 zvrf
->zns
= zebra_ns_lookup (NS_DEFAULT
); /* Point to the global (single) NS */
97 router_id_init (zvrf
);
98 listnode_add_sort (zvrf_list
, zvrf
);
102 *info
= (void *)zvrf
;
103 router_id_init (zvrf
);
107 if (zvrf
->vrf_id
== VRF_UNKNOWN
)
108 zvrf
->vrf_id
= vrf_id
;
114 * Moving an interface amongst different vrf's
115 * causes the interface to get a new ifindex
116 * so we need to find static routes with
117 * the old ifindex and replace with new
118 * ifindex to insert back into the table
121 zebra_vrf_static_route_interface_fixup (struct interface
*ifp
)
125 struct zebra_vrf
*zvrf
= zebra_vrf_lookup (ifp
->vrf_id
);
126 struct route_table
*stable
= NULL
;
127 struct route_node
*rn
= NULL
;
128 struct static_route
*si
= NULL
;
133 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
135 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
137 stable
= zvrf
->stable
[afi
][safi
];
139 for (rn
= route_top (stable
); rn
; rn
= route_next (rn
))
144 if ((strcmp (si
->ifname
, ifp
->name
) == 0) &&
145 (si
->ifindex
!= ifp
->ifindex
))
147 si
->ifindex
= ifp
->ifindex
;
148 static_install_route (afi
, safi
, &rn
->p
, si
);
157 /* Callback upon enabling a VRF. */
159 zebra_vrf_enable (vrf_id_t vrf_id
, const char *name
, void **info
)
161 struct zebra_vrf
*zvrf
= (struct zebra_vrf
*) (*info
);
162 struct route_table
*stable
= NULL
;
163 struct route_node
*rn
= NULL
;
164 struct static_route
*si
= NULL
;
165 struct interface
*ifp
= NULL
;
171 zebra_vrf_add_update (zvrf
);
173 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
175 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
177 stable
= zvrf
->stable
[afi
][safi
];
180 for (rn
= route_top (stable
); rn
; rn
= route_next (rn
))
188 ifp
= if_lookup_by_name_vrf (si
->ifname
, si
->vrf_id
);
190 si
->ifindex
= ifp
->ifindex
;
194 static_install_route (afi
, safi
, &rn
->p
, si
);
203 /* Callback upon disabling a VRF. */
205 zebra_vrf_disable (vrf_id_t vrf_id
, const char *name
, void **info
)
207 struct zebra_vrf
*zvrf
= (struct zebra_vrf
*)(*info
);
208 struct route_table
*stable
= NULL
;
209 struct route_node
*rn
= NULL
;
213 if (IS_ZEBRA_DEBUG_KERNEL
)
214 zlog_debug ("VRF %s id %u is now disabled.",
215 zvrf
->name
, zvrf
->vrf_id
);
217 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
219 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
221 stable
= zvrf
->stable
[afi
][safi
];
224 for (rn
= route_top (stable
); rn
; rn
= route_next (rn
))
227 static_uninstall_route(afi
, safi
, &rn
->p
, rn
->info
);
236 zebra_vrf_delete (vrf_id_t vrf_id
, const char *name
, void **info
)
238 struct zebra_vrf
*zvrf
= (struct zebra_vrf
*) (*info
);
242 zebra_vrf_delete_update (zvrf
);
244 rib_close_table (zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
245 rib_close_table (zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
247 list_delete_all_node (zvrf
->rid_all_sorted_list
);
248 list_delete_all_node (zvrf
->rid_lo_sorted_list
);
250 zvrf
->vrf_id
= VRF_UNKNOWN
;
256 /* Lookup the routing table in a VRF based on both VRF-Id and table-id.
257 * NOTE: Table-id is relevant only in the Default VRF.
260 zebra_vrf_table_with_table_id (afi_t afi
, safi_t safi
,
261 vrf_id_t vrf_id
, u_int32_t table_id
)
263 struct route_table
*table
= NULL
;
265 if (afi
>= AFI_MAX
|| safi
>= SAFI_MAX
)
268 if (vrf_id
== VRF_DEFAULT
)
270 if (table_id
== RT_TABLE_MAIN
||
271 table_id
== zebrad
.rtm_table_default
)
272 table
= zebra_vrf_table (afi
, safi
, vrf_id
);
274 table
= zebra_vrf_other_route_table (afi
, table_id
, vrf_id
);
277 table
= zebra_vrf_table (afi
, safi
, vrf_id
);
283 * Create a routing table for the specific AFI/SAFI in the given VRF.
286 zebra_vrf_table_create (struct zebra_vrf
*zvrf
, afi_t afi
, safi_t safi
)
288 rib_table_info_t
*info
;
289 struct route_table
*table
;
291 assert (!zvrf
->table
[afi
][safi
]);
293 table
= route_table_init ();
294 zvrf
->table
[afi
][safi
] = table
;
296 info
= XCALLOC (MTYPE_RIB_TABLE_INFO
, sizeof (*info
));
303 /* Allocate new zebra VRF. */
305 zebra_vrf_alloc (vrf_id_t vrf_id
, const char *name
)
307 struct zebra_vrf
*zvrf
;
309 zvrf
= XCALLOC (MTYPE_ZEBRA_VRF
, sizeof (struct zebra_vrf
));
311 /* Allocate routing table and static table. */
312 zebra_vrf_table_create (zvrf
, AFI_IP
, SAFI_UNICAST
);
313 zebra_vrf_table_create (zvrf
, AFI_IP6
, SAFI_UNICAST
);
314 zvrf
->stable
[AFI_IP
][SAFI_UNICAST
] = route_table_init ();
315 zvrf
->stable
[AFI_IP6
][SAFI_UNICAST
] = route_table_init ();
316 zebra_vrf_table_create (zvrf
, AFI_IP
, SAFI_MULTICAST
);
317 zebra_vrf_table_create (zvrf
, AFI_IP6
, SAFI_MULTICAST
);
318 zvrf
->stable
[AFI_IP
][SAFI_MULTICAST
] = route_table_init ();
319 zvrf
->stable
[AFI_IP6
][SAFI_MULTICAST
] = route_table_init ();
321 zvrf
->rnh_table
[AFI_IP
] = route_table_init();
322 zvrf
->rnh_table
[AFI_IP6
] = route_table_init();
324 zvrf
->import_check_table
[AFI_IP
] = route_table_init();
325 zvrf
->import_check_table
[AFI_IP6
] = route_table_init();
328 zvrf
->vrf_id
= vrf_id
;
332 strncpy (zvrf
->name
, name
, strlen(name
));
333 zvrf
->name
[strlen(name
)] = '\0';
336 zebra_mpls_init_tables (zvrf
);
341 /* Lookup VRF by identifier. */
343 zebra_vrf_lookup (vrf_id_t vrf_id
)
345 return vrf_info_lookup (vrf_id
);
348 /* Lookup the zvrf in the zvrf_list. */
350 zebra_vrf_list_lookup_by_name (const char *name
)
352 struct listnode
*node
;
353 struct zebra_vrf
*zvrf
;
356 name
= VRF_DEFAULT_NAME
;
358 for (ALL_LIST_ELEMENTS_RO (zvrf_list
, node
, zvrf
))
360 if (strcmp(name
, zvrf
->name
) == 0)
366 /* Lookup the routing table in an enabled VRF. */
368 zebra_vrf_table (afi_t afi
, safi_t safi
, vrf_id_t vrf_id
)
370 struct zebra_vrf
*zvrf
= vrf_info_lookup (vrf_id
);
375 if (afi
>= AFI_MAX
|| safi
>= SAFI_MAX
)
378 return zvrf
->table
[afi
][safi
];
381 /* Lookup the static routing table in a VRF. */
383 zebra_vrf_static_table (afi_t afi
, safi_t safi
, struct zebra_vrf
*zvrf
)
388 if (afi
>= AFI_MAX
|| safi
>= SAFI_MAX
)
391 return zvrf
->stable
[afi
][safi
];
395 zebra_vrf_other_route_table (afi_t afi
, u_int32_t table_id
, vrf_id_t vrf_id
)
397 struct zebra_vrf
*zvrf
;
398 rib_table_info_t
*info
;
399 struct route_table
*table
;
401 zvrf
= vrf_info_lookup (vrf_id
);
408 if (table_id
>= ZEBRA_KERNEL_TABLE_MAX
)
411 if ((vrf_id
== VRF_DEFAULT
) && (table_id
!= RT_TABLE_MAIN
) && (table_id
!= zebrad
.rtm_table_default
))
413 if (zvrf
->other_table
[afi
][table_id
] == NULL
)
415 table
= route_table_init();
416 info
= XCALLOC (MTYPE_RIB_TABLE_INFO
, sizeof (*info
));
419 info
->safi
= SAFI_UNICAST
;
421 zvrf
->other_table
[afi
][table_id
] = table
;
424 return (zvrf
->other_table
[afi
][table_id
]);
427 return zvrf
->table
[afi
][SAFI_UNICAST
];
430 /* Zebra VRF initialization. */
432 zebra_vrf_init (void)
434 vrf_add_hook (VRF_NEW_HOOK
, zebra_vrf_new
);
435 vrf_add_hook (VRF_ENABLE_HOOK
, zebra_vrf_enable
);
436 vrf_add_hook (VRF_DISABLE_HOOK
, zebra_vrf_disable
);
437 vrf_add_hook (VRF_DELETE_HOOK
, zebra_vrf_delete
);
439 zvrf_list
= list_new ();