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
zebra_router_table_entry_compare(const struct zebra_router_table * e1,const struct zebra_router_table * e2)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 
zebra_router_find_zrt(struct zebra_vrf * zvrf,uint32_t tableid,afi_t afi,safi_t safi)69 struct zebra_router_table *zebra_router_find_zrt(struct zebra_vrf *zvrf,
70 						 uint32_t tableid, afi_t afi,
71 						 safi_t safi)
72 {
73 	struct zebra_router_table finder;
74 	struct zebra_router_table *zrt;
75 
76 	memset(&finder, 0, sizeof(finder));
77 	finder.afi = afi;
78 	finder.safi = safi;
79 	finder.tableid = tableid;
80 	finder.ns_id = zvrf->zns->ns_id;
81 	zrt = RB_FIND(zebra_router_table_head, &zrouter.tables, &finder);
82 
83 	return zrt;
84 }
85 
zebra_router_find_table(struct zebra_vrf * zvrf,uint32_t tableid,afi_t afi,safi_t safi)86 struct route_table *zebra_router_find_table(struct zebra_vrf *zvrf,
87 					    uint32_t tableid, afi_t afi,
88 					    safi_t safi)
89 {
90 	struct zebra_router_table finder;
91 	struct zebra_router_table *zrt;
92 
93 	memset(&finder, 0, sizeof(finder));
94 	finder.afi = afi;
95 	finder.safi = safi;
96 	finder.tableid = tableid;
97 	finder.ns_id = zvrf->zns->ns_id;
98 	zrt = RB_FIND(zebra_router_table_head, &zrouter.tables, &finder);
99 
100 	if (zrt)
101 		return zrt->table;
102 	else
103 		return NULL;
104 }
105 
zebra_router_get_table(struct zebra_vrf * zvrf,uint32_t tableid,afi_t afi,safi_t safi)106 struct route_table *zebra_router_get_table(struct zebra_vrf *zvrf,
107 					   uint32_t tableid, afi_t afi,
108 					   safi_t safi)
109 {
110 	struct zebra_router_table finder;
111 	struct zebra_router_table *zrt;
112 	struct rib_table_info *info;
113 
114 	memset(&finder, 0, sizeof(finder));
115 	finder.afi = afi;
116 	finder.safi = safi;
117 	finder.tableid = tableid;
118 	finder.ns_id = zvrf->zns->ns_id;
119 	zrt = RB_FIND(zebra_router_table_head, &zrouter.tables, &finder);
120 
121 	if (zrt)
122 		return zrt->table;
123 
124 	zrt = XCALLOC(MTYPE_ZEBRA_NS, sizeof(*zrt));
125 	zrt->tableid = tableid;
126 	zrt->afi = afi;
127 	zrt->safi = safi;
128 	zrt->ns_id = zvrf->zns->ns_id;
129 	zrt->table =
130 		(afi == AFI_IP6) ? srcdest_table_init() : route_table_init();
131 
132 	info = XCALLOC(MTYPE_RIB_TABLE_INFO, sizeof(*info));
133 	info->zvrf = zvrf;
134 	info->afi = afi;
135 	info->safi = safi;
136 	info->table_id = tableid;
137 	route_table_set_info(zrt->table, info);
138 	zrt->table->cleanup = zebra_rtable_node_cleanup;
139 
140 	RB_INSERT(zebra_router_table_head, &zrouter.tables, zrt);
141 	return zrt->table;
142 }
143 
zebra_router_show_table_summary(struct vty * vty)144 void zebra_router_show_table_summary(struct vty *vty)
145 {
146 	struct zebra_router_table *zrt;
147 
148 	vty_out(vty,
149 		"VRF             NS ID    VRF ID     AFI            SAFI    Table      Count\n");
150 	vty_out(vty,
151 		"---------------------------------------------------------------------------\n");
152 	RB_FOREACH (zrt, zebra_router_table_head, &zrouter.tables) {
153 		struct rib_table_info *info = route_table_get_info(zrt->table);
154 
155 		vty_out(vty, "%-16s%5d %9d %7s %15s %8d %10lu\n", info->zvrf->vrf->name,
156 			zrt->ns_id, info->zvrf->vrf->vrf_id,
157 			afi2str(zrt->afi), safi2str(zrt->safi),
158 			zrt->tableid,
159 			zrt->table->count);
160 	}
161 }
162 
zebra_router_sweep_route(void)163 void zebra_router_sweep_route(void)
164 {
165 	struct zebra_router_table *zrt;
166 
167 	RB_FOREACH (zrt, zebra_router_table_head, &zrouter.tables) {
168 		if (zrt->ns_id != NS_DEFAULT)
169 			continue;
170 		rib_sweep_table(zrt->table);
171 	}
172 }
173 
zebra_router_sweep_nhgs(void)174 void zebra_router_sweep_nhgs(void)
175 {
176 	zebra_nhg_sweep_table(zrouter.nhgs_id);
177 }
178 
zebra_router_free_table(struct zebra_router_table * zrt)179 static void zebra_router_free_table(struct zebra_router_table *zrt)
180 {
181 	void *table_info;
182 
183 	table_info = route_table_get_info(zrt->table);
184 	route_table_finish(zrt->table);
185 	RB_REMOVE(zebra_router_table_head, &zrouter.tables, zrt);
186 
187 	XFREE(MTYPE_RIB_TABLE_INFO, table_info);
188 	XFREE(MTYPE_ZEBRA_NS, zrt);
189 }
190 
zebra_router_release_table(struct zebra_vrf * zvrf,uint32_t tableid,afi_t afi,safi_t safi)191 void zebra_router_release_table(struct zebra_vrf *zvrf, uint32_t tableid,
192 				afi_t afi, safi_t safi)
193 {
194 	struct zebra_router_table finder;
195 	struct zebra_router_table *zrt;
196 
197 	memset(&finder, 0, sizeof(finder));
198 	finder.afi = afi;
199 	finder.safi = safi;
200 	finder.tableid = tableid;
201 	finder.ns_id = zvrf->zns->ns_id;
202 	zrt = RB_FIND(zebra_router_table_head, &zrouter.tables, &finder);
203 
204 	if (!zrt)
205 		return;
206 
207 	zebra_router_free_table(zrt);
208 }
209 
zebra_router_get_next_sequence(void)210 uint32_t zebra_router_get_next_sequence(void)
211 {
212 	return 1
213 	       + atomic_fetch_add_explicit(&zrouter.sequence_num, 1,
214 					   memory_order_relaxed);
215 }
216 
multicast_mode_ipv4_set(enum multicast_mode mode)217 void multicast_mode_ipv4_set(enum multicast_mode mode)
218 {
219 	if (IS_ZEBRA_DEBUG_RIB)
220 		zlog_debug("%s: multicast lookup mode set (%d)", __func__,
221 			   mode);
222 	zrouter.ipv4_multicast_mode = mode;
223 }
224 
multicast_mode_ipv4_get(void)225 enum multicast_mode multicast_mode_ipv4_get(void)
226 {
227 	return zrouter.ipv4_multicast_mode;
228 }
229 
zebra_router_terminate(void)230 void zebra_router_terminate(void)
231 {
232 	struct zebra_router_table *zrt, *tmp;
233 
234 	RB_FOREACH_SAFE (zrt, zebra_router_table_head, &zrouter.tables, tmp)
235 		zebra_router_free_table(zrt);
236 
237 	work_queue_free_and_null(&zrouter.ribq);
238 	meta_queue_free(zrouter.mq);
239 
240 	zebra_vxlan_disable();
241 	zebra_mlag_terminate();
242 
243 	/* Free NHE in ID table only since it has unhashable entries as well */
244 	hash_clean(zrouter.nhgs_id, zebra_nhg_hash_free);
245 	hash_free(zrouter.nhgs_id);
246 	hash_clean(zrouter.nhgs, NULL);
247 	hash_free(zrouter.nhgs);
248 
249 	hash_clean(zrouter.rules_hash, zebra_pbr_rules_free);
250 	hash_free(zrouter.rules_hash);
251 
252 	hash_clean(zrouter.ipset_entry_hash, zebra_pbr_ipset_entry_free),
253 		hash_clean(zrouter.ipset_hash, zebra_pbr_ipset_free);
254 	hash_free(zrouter.ipset_hash);
255 	hash_free(zrouter.ipset_entry_hash);
256 	hash_clean(zrouter.iptable_hash, zebra_pbr_iptable_free);
257 	hash_free(zrouter.iptable_hash);
258 }
259 
zebra_router_init(void)260 void zebra_router_init(void)
261 {
262 	zrouter.sequence_num = 0;
263 
264 	zrouter.packets_to_process = ZEBRA_ZAPI_PACKETS_TO_PROCESS;
265 
266 	zrouter.rtadv_sock = -1;
267 
268 	zebra_vxlan_init();
269 	zebra_mlag_init();
270 
271 	zrouter.rules_hash = hash_create_size(8, zebra_pbr_rules_hash_key,
272 					      zebra_pbr_rules_hash_equal,
273 					      "Rules Hash");
274 
275 	zrouter.ipset_hash =
276 		hash_create_size(8, zebra_pbr_ipset_hash_key,
277 				 zebra_pbr_ipset_hash_equal, "IPset Hash");
278 
279 	zrouter.ipset_entry_hash = hash_create_size(
280 		8, zebra_pbr_ipset_entry_hash_key,
281 		zebra_pbr_ipset_entry_hash_equal, "IPset Hash Entry");
282 
283 	zrouter.iptable_hash = hash_create_size(8, zebra_pbr_iptable_hash_key,
284 						zebra_pbr_iptable_hash_equal,
285 						"IPtable Hash Entry");
286 
287 	zrouter.nhgs =
288 		hash_create_size(8, zebra_nhg_hash_key, zebra_nhg_hash_equal,
289 				 "Zebra Router Nexthop Groups");
290 	zrouter.nhgs_id =
291 		hash_create_size(8, zebra_nhg_id_key, zebra_nhg_hash_id_equal,
292 				 "Zebra Router Nexthop Groups ID index");
293 }
294