]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_router.c
Merge pull request #4525 from donaldsharp/some_cleanups
[mirror_frr.git] / zebra / zebra_router.c
1 /* Zebra Router Code.
2 * Copyright (C) 2018 Cumulus Networks, Inc.
3 * Donald Sharp
4 *
5 * This file is part of FRR.
6 *
7 * FRR 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 * FRR 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 *
17 * You should have received a copy of the GNU General Public License
18 * along with FRR; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 */
22 #include "zebra.h"
23
24 #include <pthread.h>
25 #include "lib/frratomic.h"
26
27 #include "zebra_router.h"
28 #include "zebra_memory.h"
29 #include "zebra_pbr.h"
30 #include "zebra_vxlan.h"
31 #include "zebra_mlag.h"
32 #include "zebra_nhg.h"
33 #include "debug.h"
34
35 DEFINE_MTYPE_STATIC(ZEBRA, RIB_TABLE_INFO, "RIB table info")
36
37 struct zebra_router zrouter = {
38 .multipath_num = MULTIPATH_NUM,
39 .ipv4_multicast_mode = MCAST_NO_CONFIG,
40 };
41
42 static inline int
43 zebra_router_table_entry_compare(const struct zebra_router_table *e1,
44 const struct zebra_router_table *e2);
45
46 RB_GENERATE(zebra_router_table_head, zebra_router_table,
47 zebra_router_table_entry, zebra_router_table_entry_compare);
48
49
50 static inline int
51 zebra_router_table_entry_compare(const struct zebra_router_table *e1,
52 const struct zebra_router_table *e2)
53 {
54 if (e1->tableid < e2->tableid)
55 return -1;
56 if (e1->tableid > e2->tableid)
57 return 1;
58 if (e1->ns_id < e2->ns_id)
59 return -1;
60 if (e1->ns_id > e2->ns_id)
61 return 1;
62 if (e1->afi < e2->afi)
63 return -1;
64 if (e1->afi > e2->afi)
65 return 1;
66 return (e1->safi - e2->safi);
67 }
68
69
70 struct route_table *zebra_router_find_table(struct zebra_vrf *zvrf,
71 uint32_t tableid, afi_t afi,
72 safi_t safi)
73 {
74 struct zebra_router_table finder;
75 struct zebra_router_table *zrt;
76
77 memset(&finder, 0, sizeof(finder));
78 finder.afi = afi;
79 finder.safi = safi;
80 finder.tableid = tableid;
81 finder.ns_id = zvrf->zns->ns_id;
82 zrt = RB_FIND(zebra_router_table_head, &zrouter.tables, &finder);
83
84 if (zrt)
85 return zrt->table;
86 else
87 return NULL;
88 }
89
90 struct route_table *zebra_router_get_table(struct zebra_vrf *zvrf,
91 uint32_t tableid, afi_t afi,
92 safi_t safi)
93 {
94 struct zebra_router_table finder;
95 struct zebra_router_table *zrt;
96 rib_table_info_t *info;
97
98 memset(&finder, 0, sizeof(finder));
99 finder.afi = afi;
100 finder.safi = safi;
101 finder.tableid = tableid;
102 finder.ns_id = zvrf->zns->ns_id;
103 zrt = RB_FIND(zebra_router_table_head, &zrouter.tables, &finder);
104
105 if (zrt)
106 return zrt->table;
107
108 zrt = XCALLOC(MTYPE_ZEBRA_NS, sizeof(*zrt));
109 zrt->tableid = tableid;
110 zrt->afi = afi;
111 zrt->safi = safi;
112 zrt->ns_id = zvrf->zns->ns_id;
113 zrt->table =
114 (afi == AFI_IP6) ? srcdest_table_init() : route_table_init();
115
116 info = XCALLOC(MTYPE_RIB_TABLE_INFO, sizeof(*info));
117 info->zvrf = zvrf;
118 info->afi = afi;
119 info->safi = safi;
120 route_table_set_info(zrt->table, info);
121 zrt->table->cleanup = zebra_rtable_node_cleanup;
122
123 RB_INSERT(zebra_router_table_head, &zrouter.tables, zrt);
124 return zrt->table;
125 }
126
127 void zebra_router_show_table_summary(struct vty *vty)
128 {
129 struct zebra_router_table *zrt;
130
131 vty_out(vty,
132 "VRF NS ID VRF ID AFI SAFI Table Count\n");
133 vty_out(vty,
134 "---------------------------------------------------------------------------\n");
135 RB_FOREACH (zrt, zebra_router_table_head, &zrouter.tables) {
136 rib_table_info_t *info = route_table_get_info(zrt->table);
137
138 vty_out(vty, "%-16s%5d %9d %7s %15s %8d %10lu\n", info->zvrf->vrf->name,
139 zrt->ns_id, info->zvrf->vrf->vrf_id,
140 afi2str(zrt->afi), safi2str(zrt->safi),
141 zrt->tableid,
142 zrt->table->count);
143 }
144 }
145
146 void zebra_router_sweep_route(void)
147 {
148 struct zebra_router_table *zrt;
149
150 RB_FOREACH (zrt, zebra_router_table_head, &zrouter.tables) {
151 if (zrt->ns_id != NS_DEFAULT)
152 continue;
153 rib_sweep_table(zrt->table);
154 }
155 }
156
157 static void zebra_router_free_table(struct zebra_router_table *zrt)
158 {
159 void *table_info;
160
161 table_info = route_table_get_info(zrt->table);
162 route_table_finish(zrt->table);
163 RB_REMOVE(zebra_router_table_head, &zrouter.tables, zrt);
164
165 XFREE(MTYPE_RIB_TABLE_INFO, table_info);
166 XFREE(MTYPE_ZEBRA_NS, zrt);
167 }
168
169 void zebra_router_release_table(struct zebra_vrf *zvrf, uint32_t tableid,
170 afi_t afi, safi_t safi)
171 {
172 struct zebra_router_table finder;
173 struct zebra_router_table *zrt;
174
175 memset(&finder, 0, sizeof(finder));
176 finder.afi = afi;
177 finder.safi = safi;
178 finder.tableid = tableid;
179 finder.ns_id = zvrf->zns->ns_id;
180 zrt = RB_FIND(zebra_router_table_head, &zrouter.tables, &finder);
181
182 if (!zrt)
183 return;
184
185 zebra_router_free_table(zrt);
186 }
187
188 uint32_t zebra_router_get_next_sequence(void)
189 {
190 return 1
191 + atomic_fetch_add_explicit(&zrouter.sequence_num, 1,
192 memory_order_relaxed);
193 }
194
195 void multicast_mode_ipv4_set(enum multicast_mode mode)
196 {
197 if (IS_ZEBRA_DEBUG_RIB)
198 zlog_debug("%s: multicast lookup mode set (%d)", __func__,
199 mode);
200 zrouter.ipv4_multicast_mode = mode;
201 }
202
203 enum multicast_mode multicast_mode_ipv4_get(void)
204 {
205 return zrouter.ipv4_multicast_mode;
206 }
207
208 void zebra_router_terminate(void)
209 {
210 struct zebra_router_table *zrt, *tmp;
211
212 RB_FOREACH_SAFE (zrt, zebra_router_table_head, &zrouter.tables, tmp)
213 zebra_router_free_table(zrt);
214
215 work_queue_free_and_null(&zrouter.ribq);
216 meta_queue_free(zrouter.mq);
217
218 zebra_vxlan_disable();
219 zebra_mlag_terminate();
220
221 hash_clean(zrouter.rules_hash, zebra_pbr_rules_free);
222 hash_free(zrouter.rules_hash);
223
224 hash_clean(zrouter.ipset_entry_hash, zebra_pbr_ipset_entry_free),
225 hash_clean(zrouter.ipset_hash, zebra_pbr_ipset_free);
226 hash_free(zrouter.ipset_hash);
227 hash_free(zrouter.ipset_entry_hash);
228 hash_clean(zrouter.iptable_hash, zebra_pbr_iptable_free);
229 hash_free(zrouter.iptable_hash);
230 }
231
232 void zebra_router_init(void)
233 {
234 zrouter.sequence_num = 0;
235
236 zrouter.packets_to_process = ZEBRA_ZAPI_PACKETS_TO_PROCESS;
237
238 zebra_vxlan_init();
239 zebra_mlag_init();
240
241 zrouter.rules_hash = hash_create_size(8, zebra_pbr_rules_hash_key,
242 zebra_pbr_rules_hash_equal,
243 "Rules Hash");
244
245 zrouter.ipset_hash =
246 hash_create_size(8, zebra_pbr_ipset_hash_key,
247 zebra_pbr_ipset_hash_equal, "IPset Hash");
248
249 zrouter.ipset_entry_hash = hash_create_size(
250 8, zebra_pbr_ipset_entry_hash_key,
251 zebra_pbr_ipset_entry_hash_equal, "IPset Hash Entry");
252
253 zrouter.iptable_hash = hash_create_size(8, zebra_pbr_iptable_hash_key,
254 zebra_pbr_iptable_hash_equal,
255 "IPtable Hash Entry");
256 }