]>
Commit | Line | Data |
---|---|---|
89272910 DS |
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 | ||
1485bbe7 DS |
24 | #include <pthread.h> |
25 | #include "lib/frratomic.h" | |
26 | ||
89272910 DS |
27 | #include "zebra_router.h" |
28 | #include "zebra_memory.h" | |
7f0ea8a4 | 29 | #include "zebra_pbr.h" |
6548050a | 30 | #include "zebra_vxlan.h" |
df395600 | 31 | #include "zebra_mlag.h" |
89272910 DS |
32 | |
33 | struct zebra_router zrouter; | |
34 | ||
35 | static inline int | |
36 | zebra_router_table_entry_compare(const struct zebra_router_table *e1, | |
37 | const struct zebra_router_table *e2); | |
38 | ||
39 | RB_GENERATE(zebra_router_table_head, zebra_router_table, | |
40 | zebra_router_table_entry, zebra_router_table_entry_compare); | |
41 | ||
42 | ||
43 | static inline int | |
44 | zebra_router_table_entry_compare(const struct zebra_router_table *e1, | |
45 | const struct zebra_router_table *e2) | |
46 | { | |
47 | if (e1->tableid < e2->tableid) | |
48 | return -1; | |
49 | if (e1->tableid > e2->tableid) | |
50 | return 1; | |
51 | if (e1->ns_id < e2->ns_id) | |
52 | return -1; | |
53 | if (e1->ns_id > e2->ns_id) | |
54 | return 1; | |
55 | if (e1->afi < e2->afi) | |
56 | return -1; | |
57 | if (e1->afi > e2->afi) | |
58 | return 1; | |
59 | return (e1->safi - e2->safi); | |
60 | } | |
61 | ||
62 | ||
63 | struct route_table *zebra_router_find_table(struct zebra_vrf *zvrf, | |
64 | uint32_t tableid, afi_t afi, | |
65 | safi_t safi) | |
66 | { | |
67 | struct zebra_router_table finder; | |
68 | struct zebra_router_table *zrt; | |
69 | ||
70 | memset(&finder, 0, sizeof(finder)); | |
71 | finder.afi = afi; | |
72 | finder.safi = safi; | |
73 | finder.tableid = tableid; | |
74 | finder.ns_id = zvrf->zns->ns_id; | |
75 | zrt = RB_FIND(zebra_router_table_head, &zrouter.tables, &finder); | |
76 | ||
77 | if (zrt) | |
78 | return zrt->table; | |
79 | else | |
80 | return NULL; | |
81 | } | |
82 | ||
83 | struct route_table *zebra_router_get_table(struct zebra_vrf *zvrf, | |
84 | uint32_t tableid, afi_t afi, | |
85 | safi_t safi) | |
86 | { | |
87 | struct zebra_router_table finder; | |
88 | struct zebra_router_table *zrt; | |
89 | rib_table_info_t *info; | |
90 | ||
91 | memset(&finder, 0, sizeof(finder)); | |
92 | finder.afi = afi; | |
93 | finder.safi = safi; | |
94 | finder.tableid = tableid; | |
95 | finder.ns_id = zvrf->zns->ns_id; | |
96 | zrt = RB_FIND(zebra_router_table_head, &zrouter.tables, &finder); | |
97 | ||
98 | if (zrt) | |
99 | return zrt->table; | |
100 | ||
101 | zrt = XCALLOC(MTYPE_ZEBRA_NS, sizeof(*zrt)); | |
102 | zrt->tableid = tableid; | |
103 | zrt->afi = afi; | |
99b2c423 | 104 | zrt->safi = safi; |
89272910 DS |
105 | zrt->ns_id = zvrf->zns->ns_id; |
106 | zrt->table = | |
107 | (afi == AFI_IP6) ? srcdest_table_init() : route_table_init(); | |
108 | ||
109 | info = XCALLOC(MTYPE_RIB_TABLE_INFO, sizeof(*info)); | |
110 | info->zvrf = zvrf; | |
111 | info->afi = afi; | |
ea66cec4 | 112 | info->safi = safi; |
89272910 DS |
113 | route_table_set_info(zrt->table, info); |
114 | zrt->table->cleanup = zebra_rtable_node_cleanup; | |
115 | ||
116 | RB_INSERT(zebra_router_table_head, &zrouter.tables, zrt); | |
117 | return zrt->table; | |
118 | } | |
119 | ||
120 | unsigned long zebra_router_score_proto(uint8_t proto, unsigned short instance) | |
121 | { | |
122 | struct zebra_router_table *zrt; | |
123 | unsigned long cnt = 0; | |
124 | ||
125 | RB_FOREACH (zrt, zebra_router_table_head, &zrouter.tables) { | |
126 | if (zrt->ns_id != NS_DEFAULT) | |
127 | continue; | |
128 | cnt += rib_score_proto_table(proto, instance, zrt->table); | |
129 | } | |
130 | return cnt; | |
131 | } | |
132 | ||
ac5aa23f DS |
133 | void zebra_router_show_table_summary(struct vty *vty) |
134 | { | |
135 | struct zebra_router_table *zrt; | |
136 | ||
137 | vty_out(vty, | |
138 | "VRF NS ID VRF ID AFI SAFI Table Count\n"); | |
139 | vty_out(vty, | |
140 | "---------------------------------------------------------------------------\n"); | |
141 | RB_FOREACH (zrt, zebra_router_table_head, &zrouter.tables) { | |
142 | rib_table_info_t *info = route_table_get_info(zrt->table); | |
143 | ||
144 | vty_out(vty, "%-16s%5d %9d %7s %15s %8d %10lu\n", info->zvrf->vrf->name, | |
145 | zrt->ns_id, info->zvrf->vrf->vrf_id, | |
146 | afi2str(zrt->afi), safi2str(zrt->safi), | |
147 | zrt->tableid, | |
148 | zrt->table->count); | |
149 | } | |
150 | } | |
151 | ||
89272910 DS |
152 | void zebra_router_sweep_route(void) |
153 | { | |
154 | struct zebra_router_table *zrt; | |
155 | ||
156 | RB_FOREACH (zrt, zebra_router_table_head, &zrouter.tables) { | |
157 | if (zrt->ns_id != NS_DEFAULT) | |
158 | continue; | |
159 | rib_sweep_table(zrt->table); | |
160 | } | |
161 | } | |
162 | ||
163 | static void zebra_router_free_table(struct zebra_router_table *zrt) | |
164 | { | |
165 | void *table_info; | |
166 | ||
167 | rib_close_table(zrt->table); | |
168 | ||
169 | table_info = route_table_get_info(zrt->table); | |
170 | route_table_finish(zrt->table); | |
171 | XFREE(MTYPE_RIB_TABLE_INFO, table_info); | |
172 | XFREE(MTYPE_ZEBRA_NS, zrt); | |
173 | } | |
174 | ||
1485bbe7 DS |
175 | uint32_t zebra_router_get_next_sequence(void) |
176 | { | |
177 | return 1 | |
178 | + atomic_fetch_add_explicit(&zrouter.sequence_num, 1, | |
179 | memory_order_relaxed); | |
180 | } | |
181 | ||
89272910 DS |
182 | void zebra_router_terminate(void) |
183 | { | |
184 | struct zebra_router_table *zrt, *tmp; | |
185 | ||
186 | RB_FOREACH_SAFE (zrt, zebra_router_table_head, &zrouter.tables, tmp) { | |
187 | RB_REMOVE(zebra_router_table_head, &zrouter.tables, zrt); | |
188 | zebra_router_free_table(zrt); | |
189 | } | |
7f0ea8a4 | 190 | |
489a9614 | 191 | work_queue_free_and_null(&zrouter.ribq); |
ea45a4e7 | 192 | meta_queue_free(zrouter.mq); |
489a9614 | 193 | |
6548050a | 194 | zebra_vxlan_disable(); |
df395600 DS |
195 | zebra_mlag_terminate(); |
196 | ||
7f0ea8a4 DS |
197 | hash_clean(zrouter.rules_hash, zebra_pbr_rules_free); |
198 | hash_free(zrouter.rules_hash); | |
62f20a52 DS |
199 | |
200 | hash_clean(zrouter.ipset_entry_hash, zebra_pbr_ipset_entry_free), | |
201 | hash_clean(zrouter.ipset_hash, zebra_pbr_ipset_free); | |
202 | hash_free(zrouter.ipset_hash); | |
203 | hash_free(zrouter.ipset_entry_hash); | |
204 | hash_clean(zrouter.iptable_hash, zebra_pbr_iptable_free); | |
205 | hash_free(zrouter.iptable_hash); | |
89272910 DS |
206 | } |
207 | ||
208 | void zebra_router_init(void) | |
209 | { | |
1485bbe7 DS |
210 | zrouter.sequence_num = 0; |
211 | ||
b3d43ff4 | 212 | zrouter.rtm_table_default = 0; |
5ec5a716 | 213 | zrouter.packets_to_process = ZEBRA_ZAPI_PACKETS_TO_PROCESS; |
b3d43ff4 | 214 | |
6548050a | 215 | zebra_vxlan_init(); |
df395600 DS |
216 | zebra_mlag_init(); |
217 | ||
7f0ea8a4 DS |
218 | zrouter.rules_hash = hash_create_size(8, zebra_pbr_rules_hash_key, |
219 | zebra_pbr_rules_hash_equal, | |
220 | "Rules Hash"); | |
62f20a52 DS |
221 | |
222 | zrouter.ipset_hash = | |
223 | hash_create_size(8, zebra_pbr_ipset_hash_key, | |
224 | zebra_pbr_ipset_hash_equal, "IPset Hash"); | |
225 | ||
226 | zrouter.ipset_entry_hash = hash_create_size( | |
227 | 8, zebra_pbr_ipset_entry_hash_key, | |
228 | zebra_pbr_ipset_entry_hash_equal, "IPset Hash Entry"); | |
229 | ||
230 | zrouter.iptable_hash = hash_create_size(8, zebra_pbr_iptable_hash_key, | |
231 | zebra_pbr_iptable_hash_equal, | |
232 | "IPtable Hash Entry"); | |
89272910 | 233 | } |