]>
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 | ||
24 | #include "zebra_router.h" | |
25 | #include "zebra_memory.h" | |
7f0ea8a4 | 26 | #include "zebra_pbr.h" |
89272910 DS |
27 | |
28 | struct zebra_router zrouter; | |
29 | ||
30 | static inline int | |
31 | zebra_router_table_entry_compare(const struct zebra_router_table *e1, | |
32 | const struct zebra_router_table *e2); | |
33 | ||
34 | RB_GENERATE(zebra_router_table_head, zebra_router_table, | |
35 | zebra_router_table_entry, zebra_router_table_entry_compare); | |
36 | ||
37 | ||
38 | static inline int | |
39 | zebra_router_table_entry_compare(const struct zebra_router_table *e1, | |
40 | const struct zebra_router_table *e2) | |
41 | { | |
42 | if (e1->tableid < e2->tableid) | |
43 | return -1; | |
44 | if (e1->tableid > e2->tableid) | |
45 | return 1; | |
46 | if (e1->ns_id < e2->ns_id) | |
47 | return -1; | |
48 | if (e1->ns_id > e2->ns_id) | |
49 | return 1; | |
50 | if (e1->afi < e2->afi) | |
51 | return -1; | |
52 | if (e1->afi > e2->afi) | |
53 | return 1; | |
54 | return (e1->safi - e2->safi); | |
55 | } | |
56 | ||
57 | ||
58 | struct route_table *zebra_router_find_table(struct zebra_vrf *zvrf, | |
59 | uint32_t tableid, afi_t afi, | |
60 | safi_t safi) | |
61 | { | |
62 | struct zebra_router_table finder; | |
63 | struct zebra_router_table *zrt; | |
64 | ||
65 | memset(&finder, 0, sizeof(finder)); | |
66 | finder.afi = afi; | |
67 | finder.safi = safi; | |
68 | finder.tableid = tableid; | |
69 | finder.ns_id = zvrf->zns->ns_id; | |
70 | zrt = RB_FIND(zebra_router_table_head, &zrouter.tables, &finder); | |
71 | ||
72 | if (zrt) | |
73 | return zrt->table; | |
74 | else | |
75 | return NULL; | |
76 | } | |
77 | ||
78 | struct route_table *zebra_router_get_table(struct zebra_vrf *zvrf, | |
79 | uint32_t tableid, afi_t afi, | |
80 | safi_t safi) | |
81 | { | |
82 | struct zebra_router_table finder; | |
83 | struct zebra_router_table *zrt; | |
84 | rib_table_info_t *info; | |
85 | ||
86 | memset(&finder, 0, sizeof(finder)); | |
87 | finder.afi = afi; | |
88 | finder.safi = safi; | |
89 | finder.tableid = tableid; | |
90 | finder.ns_id = zvrf->zns->ns_id; | |
91 | zrt = RB_FIND(zebra_router_table_head, &zrouter.tables, &finder); | |
92 | ||
93 | if (zrt) | |
94 | return zrt->table; | |
95 | ||
96 | zrt = XCALLOC(MTYPE_ZEBRA_NS, sizeof(*zrt)); | |
97 | zrt->tableid = tableid; | |
98 | zrt->afi = afi; | |
99b2c423 | 99 | zrt->safi = safi; |
89272910 DS |
100 | zrt->ns_id = zvrf->zns->ns_id; |
101 | zrt->table = | |
102 | (afi == AFI_IP6) ? srcdest_table_init() : route_table_init(); | |
103 | ||
104 | info = XCALLOC(MTYPE_RIB_TABLE_INFO, sizeof(*info)); | |
105 | info->zvrf = zvrf; | |
106 | info->afi = afi; | |
107 | info->safi = SAFI_UNICAST; | |
108 | route_table_set_info(zrt->table, info); | |
109 | zrt->table->cleanup = zebra_rtable_node_cleanup; | |
110 | ||
111 | RB_INSERT(zebra_router_table_head, &zrouter.tables, zrt); | |
112 | return zrt->table; | |
113 | } | |
114 | ||
115 | unsigned long zebra_router_score_proto(uint8_t proto, unsigned short instance) | |
116 | { | |
117 | struct zebra_router_table *zrt; | |
118 | unsigned long cnt = 0; | |
119 | ||
120 | RB_FOREACH (zrt, zebra_router_table_head, &zrouter.tables) { | |
121 | if (zrt->ns_id != NS_DEFAULT) | |
122 | continue; | |
123 | cnt += rib_score_proto_table(proto, instance, zrt->table); | |
124 | } | |
125 | return cnt; | |
126 | } | |
127 | ||
128 | void zebra_router_sweep_route(void) | |
129 | { | |
130 | struct zebra_router_table *zrt; | |
131 | ||
132 | RB_FOREACH (zrt, zebra_router_table_head, &zrouter.tables) { | |
133 | if (zrt->ns_id != NS_DEFAULT) | |
134 | continue; | |
135 | rib_sweep_table(zrt->table); | |
136 | } | |
137 | } | |
138 | ||
139 | static void zebra_router_free_table(struct zebra_router_table *zrt) | |
140 | { | |
141 | void *table_info; | |
142 | ||
143 | rib_close_table(zrt->table); | |
144 | ||
145 | table_info = route_table_get_info(zrt->table); | |
146 | route_table_finish(zrt->table); | |
147 | XFREE(MTYPE_RIB_TABLE_INFO, table_info); | |
148 | XFREE(MTYPE_ZEBRA_NS, zrt); | |
149 | } | |
150 | ||
151 | void zebra_router_terminate(void) | |
152 | { | |
153 | struct zebra_router_table *zrt, *tmp; | |
154 | ||
155 | RB_FOREACH_SAFE (zrt, zebra_router_table_head, &zrouter.tables, tmp) { | |
156 | RB_REMOVE(zebra_router_table_head, &zrouter.tables, zrt); | |
157 | zebra_router_free_table(zrt); | |
158 | } | |
7f0ea8a4 DS |
159 | |
160 | hash_clean(zrouter.rules_hash, zebra_pbr_rules_free); | |
161 | hash_free(zrouter.rules_hash); | |
62f20a52 DS |
162 | |
163 | hash_clean(zrouter.ipset_entry_hash, zebra_pbr_ipset_entry_free), | |
164 | hash_clean(zrouter.ipset_hash, zebra_pbr_ipset_free); | |
165 | hash_free(zrouter.ipset_hash); | |
166 | hash_free(zrouter.ipset_entry_hash); | |
167 | hash_clean(zrouter.iptable_hash, zebra_pbr_iptable_free); | |
168 | hash_free(zrouter.iptable_hash); | |
89272910 DS |
169 | } |
170 | ||
171 | void zebra_router_init(void) | |
172 | { | |
173 | zrouter.l3vni_table = NULL; | |
7f0ea8a4 DS |
174 | |
175 | zrouter.rules_hash = hash_create_size(8, zebra_pbr_rules_hash_key, | |
176 | zebra_pbr_rules_hash_equal, | |
177 | "Rules Hash"); | |
62f20a52 DS |
178 | |
179 | zrouter.ipset_hash = | |
180 | hash_create_size(8, zebra_pbr_ipset_hash_key, | |
181 | zebra_pbr_ipset_hash_equal, "IPset Hash"); | |
182 | ||
183 | zrouter.ipset_entry_hash = hash_create_size( | |
184 | 8, zebra_pbr_ipset_entry_hash_key, | |
185 | zebra_pbr_ipset_entry_hash_equal, "IPset Hash Entry"); | |
186 | ||
187 | zrouter.iptable_hash = hash_create_size(8, zebra_pbr_iptable_hash_key, | |
188 | zebra_pbr_iptable_hash_equal, | |
189 | "IPtable Hash Entry"); | |
89272910 | 190 | } |