]>
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
27 #include "zebra/debug.h"
28 #include "zebra/zserv.h"
29 #include "zebra/rib.h"
30 #include "zebra/zebra_vrf.h"
31 #include "zebra/router-id.h"
33 extern struct zebra_t zebrad
;
35 /* VRF information update. */
37 zebra_vrf_add_update (struct zebra_vrf
*zvrf
)
39 struct listnode
*node
, *nnode
;
42 if (IS_ZEBRA_DEBUG_EVENT
)
43 zlog_debug ("MESSAGE: ZEBRA_VRF_ADD %s", zvrf
->name
);
45 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
46 zsend_vrf_add (client
, zvrf
);
50 zebra_vrf_delete_update (struct zebra_vrf
*zvrf
)
52 struct listnode
*node
, *nnode
;
55 if (IS_ZEBRA_DEBUG_EVENT
)
56 zlog_debug ("MESSAGE: ZEBRA_VRF_DELETE %s", zvrf
->name
);
58 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
59 zsend_vrf_delete (client
, zvrf
);
63 zebra_vrf_update_all (struct zserv
*client
)
68 for (iter
= vrf_first (); iter
!= VRF_ITER_INVALID
; iter
= vrf_next (iter
))
70 if ((vrf
= vrf_iter2vrf (iter
)) && vrf
->vrf_id
)
71 zsend_vrf_add (client
, vrf_info_lookup (vrf
->vrf_id
));
75 /* Callback upon creating a new VRF. */
77 zebra_vrf_new (vrf_id_t vrf_id
, const char *name
, void **info
)
79 struct zebra_vrf
*zvrf
= *info
;
81 zlog_info ("ZVRF %s with id %u", name
, vrf_id
);
85 zvrf
= zebra_vrf_alloc (vrf_id
, name
);
86 zvrf
->zns
= zebra_ns_lookup (NS_DEFAULT
); /* Point to the global (single) NS */
88 router_id_init (zvrf
);
91 if (zvrf
->vrf_id
== VRF_UNKNOWN
)
92 zvrf
->vrf_id
= vrf_id
;
98 * Moving an interface amongst different vrf's
99 * causes the interface to get a new ifindex
100 * so we need to find static routes with
101 * the old ifindex and replace with new
102 * ifindex to insert back into the table
105 zebra_vrf_static_route_interface_fixup (struct interface
*ifp
)
109 struct zebra_vrf
*zvrf
= zebra_vrf_lookup (ifp
->vrf_id
);
110 struct route_table
*stable
= NULL
;
111 struct route_node
*rn
= NULL
;
112 struct static_route
*si
= NULL
;
117 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
119 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
121 stable
= zvrf
->stable
[afi
][safi
];
123 for (rn
= route_top (stable
); rn
; rn
= route_next (rn
))
128 if ((strcmp (si
->ifname
, ifp
->name
) == 0) &&
129 (si
->ifindex
!= ifp
->ifindex
))
131 si
->ifindex
= ifp
->ifindex
;
132 static_install_route (afi
, safi
, &rn
->p
, si
);
141 /* Callback upon enabling a VRF. */
143 zebra_vrf_enable (vrf_id_t vrf_id
, const char *name
, void **info
)
145 struct zebra_vrf
*zvrf
= (struct zebra_vrf
*) (*info
);
146 struct route_table
*stable
= NULL
;
147 struct route_node
*rn
= NULL
;
148 struct static_route
*si
= NULL
;
149 struct interface
*ifp
= NULL
;
155 zebra_vrf_add_update (zvrf
);
157 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
159 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
161 stable
= zvrf
->stable
[afi
][safi
];
164 for (rn
= route_top (stable
); rn
; rn
= route_next (rn
))
172 ifp
= if_lookup_by_name_vrf (si
->ifname
, si
->vrf_id
);
174 si
->ifindex
= ifp
->ifindex
;
178 static_install_route (afi
, safi
, &rn
->p
, si
);
187 /* Callback upon disabling a VRF. */
189 zebra_vrf_disable (vrf_id_t vrf_id
, const char *name
, void **info
)
191 struct zebra_vrf
*zvrf
= (struct zebra_vrf
*)(*info
);
192 struct route_table
*stable
= NULL
;
193 struct route_node
*rn
= NULL
;
197 if (IS_ZEBRA_DEBUG_KERNEL
)
198 zlog_debug ("VRF %s id %u is now disabled.",
199 zvrf
->name
, zvrf
->vrf_id
);
201 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
203 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
205 stable
= zvrf
->stable
[afi
][safi
];
208 for (rn
= route_top (stable
); rn
; rn
= route_next (rn
))
211 static_uninstall_route(afi
, safi
, &rn
->p
, rn
->info
);
220 zebra_vrf_delete (vrf_id_t vrf_id
, const char *name
, void **info
)
222 struct zebra_vrf
*zvrf
= (struct zebra_vrf
*) (*info
);
226 zebra_vrf_delete_update (zvrf
);
228 rib_close_table (zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
229 rib_close_table (zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
231 list_delete_all_node (zvrf
->rid_all_sorted_list
);
232 list_delete_all_node (zvrf
->rid_lo_sorted_list
);
234 XFREE (MTYPE_ZEBRA_VRF
, zvrf
);
239 /* Lookup the routing table in a VRF based on both VRF-Id and table-id.
240 * NOTE: Table-id is relevant only in the Default VRF.
243 zebra_vrf_table_with_table_id (afi_t afi
, safi_t safi
,
244 vrf_id_t vrf_id
, u_int32_t table_id
)
246 struct route_table
*table
= NULL
;
248 if (afi
>= AFI_MAX
|| safi
>= SAFI_MAX
)
251 if (vrf_id
== VRF_DEFAULT
)
253 if (table_id
== RT_TABLE_MAIN
||
254 table_id
== zebrad
.rtm_table_default
)
255 table
= zebra_vrf_table (afi
, safi
, vrf_id
);
257 table
= zebra_vrf_other_route_table (afi
, table_id
, vrf_id
);
260 table
= zebra_vrf_table (afi
, safi
, vrf_id
);
266 * Create a routing table for the specific AFI/SAFI in the given VRF.
269 zebra_vrf_table_create (struct zebra_vrf
*zvrf
, afi_t afi
, safi_t safi
)
271 rib_table_info_t
*info
;
272 struct route_table
*table
;
274 assert (!zvrf
->table
[afi
][safi
]);
276 table
= route_table_init ();
277 zvrf
->table
[afi
][safi
] = table
;
279 info
= XCALLOC (MTYPE_RIB_TABLE_INFO
, sizeof (*info
));
286 /* Allocate new zebra VRF. */
288 zebra_vrf_alloc (vrf_id_t vrf_id
, const char *name
)
290 struct zebra_vrf
*zvrf
;
292 zvrf
= XCALLOC (MTYPE_ZEBRA_VRF
, sizeof (struct zebra_vrf
));
294 /* Allocate routing table and static table. */
295 zebra_vrf_table_create (zvrf
, AFI_IP
, SAFI_UNICAST
);
296 zebra_vrf_table_create (zvrf
, AFI_IP6
, SAFI_UNICAST
);
297 zvrf
->stable
[AFI_IP
][SAFI_UNICAST
] = route_table_init ();
298 zvrf
->stable
[AFI_IP6
][SAFI_UNICAST
] = route_table_init ();
299 zebra_vrf_table_create (zvrf
, AFI_IP
, SAFI_MULTICAST
);
300 zebra_vrf_table_create (zvrf
, AFI_IP6
, SAFI_MULTICAST
);
301 zvrf
->stable
[AFI_IP
][SAFI_MULTICAST
] = route_table_init ();
302 zvrf
->stable
[AFI_IP6
][SAFI_MULTICAST
] = route_table_init ();
304 zvrf
->rnh_table
[AFI_IP
] = route_table_init();
305 zvrf
->rnh_table
[AFI_IP6
] = route_table_init();
307 zvrf
->import_check_table
[AFI_IP
] = route_table_init();
308 zvrf
->import_check_table
[AFI_IP6
] = route_table_init();
311 zvrf
->vrf_id
= vrf_id
;
315 strncpy (zvrf
->name
, name
, strlen(name
));
316 zvrf
->name
[strlen(name
)] = '\0';
322 /* Lookup VRF by identifier. */
324 zebra_vrf_lookup (vrf_id_t vrf_id
)
326 return vrf_info_lookup (vrf_id
);
329 /* Lookup the routing table in an enabled VRF. */
331 zebra_vrf_table (afi_t afi
, safi_t safi
, vrf_id_t vrf_id
)
333 struct zebra_vrf
*zvrf
= vrf_info_lookup (vrf_id
);
338 if (afi
>= AFI_MAX
|| safi
>= SAFI_MAX
)
341 return zvrf
->table
[afi
][safi
];
344 /* Lookup the static routing table in a VRF. */
346 zebra_vrf_static_table (afi_t afi
, safi_t safi
, struct zebra_vrf
*zvrf
)
351 if (afi
>= AFI_MAX
|| safi
>= SAFI_MAX
)
354 return zvrf
->stable
[afi
][safi
];
358 zebra_vrf_other_route_table (afi_t afi
, u_int32_t table_id
, vrf_id_t vrf_id
)
360 struct zebra_vrf
*zvrf
;
361 rib_table_info_t
*info
;
362 struct route_table
*table
;
364 zvrf
= vrf_info_lookup (vrf_id
);
371 if (table_id
>= ZEBRA_KERNEL_TABLE_MAX
)
374 if ((vrf_id
== VRF_DEFAULT
) && (table_id
!= RT_TABLE_MAIN
) && (table_id
!= zebrad
.rtm_table_default
))
376 if (zvrf
->other_table
[afi
][table_id
] == NULL
)
378 table
= route_table_init();
379 info
= XCALLOC (MTYPE_RIB_TABLE_INFO
, sizeof (*info
));
382 info
->safi
= SAFI_UNICAST
;
384 zvrf
->other_table
[afi
][table_id
] = table
;
387 return (zvrf
->other_table
[afi
][table_id
]);
390 return zvrf
->table
[afi
][SAFI_UNICAST
];
393 /* Zebra VRF initialization. */
395 zebra_vrf_init (void)
397 vrf_add_hook (VRF_NEW_HOOK
, zebra_vrf_new
);
398 vrf_add_hook (VRF_ENABLE_HOOK
, zebra_vrf_enable
);
399 vrf_add_hook (VRF_DISABLE_HOOK
, zebra_vrf_disable
);
400 vrf_add_hook (VRF_DELETE_HOOK
, zebra_vrf_delete
);