]>
Commit | Line | Data |
---|---|---|
8a71d93d DS |
1 | /* |
2 | * Zebra connect code. | |
3 | * Copyright (C) Cumulus Networks, Inc. | |
4 | * Donald Sharp | |
5 | * | |
6 | * This file is part of FRR. | |
7 | * | |
8 | * FRR is free software; you can redistribute it and/or modify it | |
9 | * under the terms of the GNU General Public License as published by the | |
10 | * Free Software Foundation; either version 2, or (at your option) any | |
11 | * later version. | |
12 | * | |
13 | * FRR is distributed in the hope that it will be useful, but | |
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License along | |
19 | * with this program; see the file COPYING; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
21 | */ | |
22 | #include <zebra.h> | |
23 | ||
24 | #include "thread.h" | |
25 | #include "command.h" | |
26 | #include "network.h" | |
27 | #include "prefix.h" | |
28 | #include "routemap.h" | |
29 | #include "table.h" | |
30 | #include "stream.h" | |
31 | #include "memory.h" | |
32 | #include "zclient.h" | |
33 | #include "filter.h" | |
34 | #include "plist.h" | |
35 | #include "log.h" | |
36 | #include "nexthop.h" | |
694b242f | 37 | #include "nexthop_group.h" |
8a71d93d | 38 | |
547dc642 | 39 | #include "sharp_globals.h" |
86da53ab | 40 | #include "sharp_nht.h" |
8a71d93d DS |
41 | #include "sharp_zebra.h" |
42 | ||
43 | /* Zebra structure to hold current status. */ | |
44 | struct zclient *zclient = NULL; | |
45 | ||
46 | /* For registering threads. */ | |
47 | extern struct thread_master *master; | |
48 | ||
8a71d93d | 49 | /* Inteface addition message from zebra. */ |
ef7bd2a3 | 50 | static int sharp_ifp_create(struct interface *ifp) |
8a71d93d | 51 | { |
8a71d93d DS |
52 | return 0; |
53 | } | |
54 | ||
121f9dee | 55 | static int interface_delete(ZAPI_CALLBACK_ARGS) |
8a71d93d DS |
56 | { |
57 | struct interface *ifp; | |
58 | struct stream *s; | |
59 | ||
60 | s = zclient->ibuf; | |
61 | /* zebra_interface_state_read () updates interface structure in iflist | |
62 | */ | |
63 | ifp = zebra_interface_state_read(s, vrf_id); | |
64 | ||
65 | if (ifp == NULL) | |
66 | return 0; | |
67 | ||
68 | if_set_index(ifp, IFINDEX_INTERNAL); | |
69 | ||
70 | return 0; | |
71 | } | |
72 | ||
121f9dee | 73 | static int interface_address_add(ZAPI_CALLBACK_ARGS) |
8a71d93d DS |
74 | { |
75 | ||
121f9dee | 76 | zebra_interface_address_read(cmd, zclient->ibuf, vrf_id); |
8a71d93d DS |
77 | |
78 | return 0; | |
79 | } | |
80 | ||
121f9dee | 81 | static int interface_address_delete(ZAPI_CALLBACK_ARGS) |
8a71d93d DS |
82 | { |
83 | struct connected *c; | |
84 | ||
121f9dee | 85 | c = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id); |
8a71d93d DS |
86 | |
87 | if (!c) | |
88 | return 0; | |
89 | ||
90 | connected_free(c); | |
91 | return 0; | |
92 | } | |
93 | ||
ddbf3e60 | 94 | static int sharp_ifp_up(struct interface *ifp) |
8a71d93d | 95 | { |
8a71d93d DS |
96 | return 0; |
97 | } | |
98 | ||
b0b69e59 | 99 | static int sharp_ifp_down(struct interface *ifp) |
8a71d93d | 100 | { |
8a71d93d DS |
101 | return 0; |
102 | } | |
103 | ||
0cf08685 DS |
104 | void sharp_install_routes_helper(struct prefix *p, vrf_id_t vrf_id, |
105 | uint8_t instance, struct nexthop_group *nhg, | |
6b98d34f DS |
106 | uint32_t routes) |
107 | { | |
108 | uint32_t temp, i; | |
dbc1bf46 | 109 | bool v4 = false; |
6b98d34f DS |
110 | |
111 | zlog_debug("Inserting %u routes", routes); | |
112 | ||
dbc1bf46 DS |
113 | if (p->family == AF_INET) { |
114 | v4 = true; | |
115 | temp = ntohl(p->u.prefix4.s_addr); | |
116 | } else | |
117 | temp = ntohl(p->u.val32[3]); | |
118 | ||
547dc642 | 119 | monotime(&sg.r.t_start); |
6b98d34f | 120 | for (i = 0; i < routes; i++) { |
0cf08685 | 121 | route_add(p, vrf_id, (uint8_t)instance, nhg); |
dbc1bf46 DS |
122 | if (v4) |
123 | p->u.prefix4.s_addr = htonl(++temp); | |
124 | else | |
125 | p->u.val32[3] = htonl(++temp); | |
6b98d34f DS |
126 | } |
127 | } | |
128 | ||
0cf08685 DS |
129 | void sharp_remove_routes_helper(struct prefix *p, vrf_id_t vrf_id, |
130 | uint8_t instance, uint32_t routes) | |
6b98d34f DS |
131 | { |
132 | uint32_t temp, i; | |
dbc1bf46 | 133 | bool v4 = false; |
6b98d34f DS |
134 | |
135 | zlog_debug("Removing %u routes", routes); | |
136 | ||
dbc1bf46 DS |
137 | if (p->family == AF_INET) { |
138 | v4 = true; | |
139 | temp = ntohl(p->u.prefix4.s_addr); | |
140 | } else | |
141 | temp = ntohl(p->u.val32[3]); | |
142 | ||
547dc642 | 143 | monotime(&sg.r.t_start); |
6b98d34f | 144 | for (i = 0; i < routes; i++) { |
0cf08685 | 145 | route_delete(p, vrf_id, (uint8_t)instance); |
dbc1bf46 DS |
146 | if (v4) |
147 | p->u.prefix4.s_addr = htonl(++temp); | |
148 | else | |
149 | p->u.val32[3] = htonl(++temp); | |
6b98d34f DS |
150 | } |
151 | } | |
152 | ||
b939f6ff | 153 | static void handle_repeated(bool installed) |
6b98d34f | 154 | { |
547dc642 DS |
155 | struct prefix p = sg.r.orig_prefix; |
156 | sg.r.repeat--; | |
6b98d34f | 157 | |
547dc642 | 158 | if (sg.r.repeat <= 0) |
6b98d34f DS |
159 | return; |
160 | ||
161 | if (installed) { | |
547dc642 | 162 | sg.r.removed_routes = 0; |
0cf08685 DS |
163 | sharp_remove_routes_helper(&p, sg.r.vrf_id, |
164 | sg.r.inst, sg.r.total_routes); | |
6b98d34f DS |
165 | } |
166 | ||
f54f37c1 | 167 | if (!installed) { |
547dc642 | 168 | sg.r.installed_routes = 0; |
0cf08685 DS |
169 | sharp_install_routes_helper(&p, sg.r.vrf_id, sg.r.inst, |
170 | &sg.r.nhop_group, | |
547dc642 | 171 | sg.r.total_routes); |
6b98d34f DS |
172 | } |
173 | } | |
174 | ||
121f9dee | 175 | static int route_notify_owner(ZAPI_CALLBACK_ARGS) |
8a71d93d | 176 | { |
25c84d86 | 177 | struct timeval r; |
8a71d93d DS |
178 | struct prefix p; |
179 | enum zapi_route_notify_owner note; | |
28610f7e | 180 | uint32_t table; |
8a71d93d | 181 | |
28610f7e | 182 | if (!zapi_route_notify_decode(zclient->ibuf, &p, &table, ¬e)) |
8a71d93d DS |
183 | return -1; |
184 | ||
5e54c602 DS |
185 | switch (note) { |
186 | case ZAPI_ROUTE_INSTALLED: | |
547dc642 DS |
187 | sg.r.installed_routes++; |
188 | if (sg.r.total_routes == sg.r.installed_routes) { | |
189 | monotime(&sg.r.t_end); | |
190 | timersub(&sg.r.t_end, &sg.r.t_start, &r); | |
051a0be4 DL |
191 | zlog_debug("Installed All Items %jd.%ld", |
192 | (intmax_t)r.tv_sec, (long)r.tv_usec); | |
6b98d34f DS |
193 | handle_repeated(true); |
194 | } | |
5e54c602 DS |
195 | break; |
196 | case ZAPI_ROUTE_FAIL_INSTALL: | |
197 | zlog_debug("Failed install of route"); | |
198 | break; | |
199 | case ZAPI_ROUTE_BETTER_ADMIN_WON: | |
200 | zlog_debug("Better Admin Distance won over us"); | |
201 | break; | |
202 | case ZAPI_ROUTE_REMOVED: | |
547dc642 DS |
203 | sg.r.removed_routes++; |
204 | if (sg.r.total_routes == sg.r.removed_routes) { | |
205 | monotime(&sg.r.t_end); | |
206 | timersub(&sg.r.t_end, &sg.r.t_start, &r); | |
051a0be4 DL |
207 | zlog_debug("Removed all Items %jd.%ld", |
208 | (intmax_t)r.tv_sec, (long)r.tv_usec); | |
6b98d34f DS |
209 | handle_repeated(false); |
210 | } | |
5e54c602 DS |
211 | break; |
212 | case ZAPI_ROUTE_REMOVE_FAIL: | |
213 | zlog_debug("Route removal Failure"); | |
214 | break; | |
215 | } | |
8a71d93d DS |
216 | return 0; |
217 | } | |
218 | ||
219 | static void zebra_connected(struct zclient *zclient) | |
220 | { | |
221 | zclient_send_reg_requests(zclient, VRF_DEFAULT); | |
222 | } | |
223 | ||
7d061b3c | 224 | void vrf_label_add(vrf_id_t vrf_id, afi_t afi, mpls_label_t label) |
ab18a495 | 225 | { |
7d061b3c | 226 | zclient_send_vrf_label(zclient, vrf_id, afi, label, ZEBRA_LSP_SHARP); |
ab18a495 DS |
227 | } |
228 | ||
0cf08685 DS |
229 | void route_add(struct prefix *p, vrf_id_t vrf_id, |
230 | uint8_t instance, struct nexthop_group *nhg) | |
8a71d93d DS |
231 | { |
232 | struct zapi_route api; | |
233 | struct zapi_nexthop *api_nh; | |
694b242f DS |
234 | struct nexthop *nh; |
235 | int i = 0; | |
8a71d93d DS |
236 | |
237 | memset(&api, 0, sizeof(api)); | |
0cf08685 | 238 | api.vrf_id = vrf_id; |
8a71d93d | 239 | api.type = ZEBRA_ROUTE_SHARP; |
ae252c02 | 240 | api.instance = instance; |
8a71d93d DS |
241 | api.safi = SAFI_UNICAST; |
242 | memcpy(&api.prefix, p, sizeof(*p)); | |
243 | ||
a3896844 | 244 | SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION); |
8a71d93d DS |
245 | SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP); |
246 | ||
694b242f DS |
247 | for (ALL_NEXTHOPS_PTR(nhg, nh)) { |
248 | api_nh = &api.nexthops[i]; | |
0cf08685 | 249 | api_nh->vrf_id = nh->vrf_id; |
694b242f DS |
250 | api_nh->type = nh->type; |
251 | switch (nh->type) { | |
252 | case NEXTHOP_TYPE_IPV4: | |
253 | api_nh->gate = nh->gate; | |
254 | break; | |
255 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
256 | api_nh->gate = nh->gate; | |
257 | api_nh->ifindex = nh->ifindex; | |
258 | break; | |
259 | case NEXTHOP_TYPE_IFINDEX: | |
260 | api_nh->ifindex = nh->ifindex; | |
261 | break; | |
262 | case NEXTHOP_TYPE_IPV6: | |
263 | memcpy(&api_nh->gate.ipv6, &nh->gate.ipv6, 16); | |
264 | break; | |
265 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
266 | api_nh->ifindex = nh->ifindex; | |
267 | memcpy(&api_nh->gate.ipv6, &nh->gate.ipv6, 16); | |
268 | break; | |
269 | case NEXTHOP_TYPE_BLACKHOLE: | |
270 | api_nh->bh_type = nh->bh_type; | |
271 | break; | |
272 | } | |
273 | i++; | |
274 | } | |
275 | api.nexthop_num = i; | |
8a71d93d DS |
276 | |
277 | zclient_route_send(ZEBRA_ROUTE_ADD, zclient, &api); | |
278 | } | |
279 | ||
0cf08685 | 280 | void route_delete(struct prefix *p, vrf_id_t vrf_id, uint8_t instance) |
8a71d93d DS |
281 | { |
282 | struct zapi_route api; | |
283 | ||
284 | memset(&api, 0, sizeof(api)); | |
0cf08685 | 285 | api.vrf_id = vrf_id; |
8a71d93d DS |
286 | api.type = ZEBRA_ROUTE_SHARP; |
287 | api.safi = SAFI_UNICAST; | |
ae252c02 | 288 | api.instance = instance; |
8a71d93d DS |
289 | memcpy(&api.prefix, p, sizeof(*p)); |
290 | zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api); | |
291 | ||
292 | return; | |
293 | } | |
294 | ||
91529dc8 | 295 | void sharp_zebra_nexthop_watch(struct prefix *p, vrf_id_t vrf_id, bool import, |
b47dc61a | 296 | bool watch, bool connected) |
0ae8130d | 297 | { |
b47dc61a | 298 | int command; |
0ae8130d | 299 | |
b47dc61a DS |
300 | if (!import) { |
301 | command = ZEBRA_NEXTHOP_REGISTER; | |
302 | ||
303 | if (!watch) | |
304 | command = ZEBRA_NEXTHOP_UNREGISTER; | |
305 | } else { | |
306 | command = ZEBRA_IMPORT_ROUTE_REGISTER; | |
307 | ||
308 | if (!watch) | |
309 | command = ZEBRA_IMPORT_ROUTE_UNREGISTER; | |
310 | } | |
0ae8130d | 311 | |
91529dc8 | 312 | if (zclient_send_rnh(zclient, command, p, connected, vrf_id) < 0) |
b3beaea0 A |
313 | zlog_warn("%s: Failure to send nexthop to zebra", |
314 | __PRETTY_FUNCTION__); | |
0ae8130d DS |
315 | } |
316 | ||
121f9dee | 317 | static int sharp_nexthop_update(ZAPI_CALLBACK_ARGS) |
0ae8130d | 318 | { |
86da53ab | 319 | struct sharp_nh_tracker *nht; |
0ae8130d DS |
320 | struct zapi_route nhr; |
321 | char buf[PREFIX_STRLEN]; | |
322 | int i; | |
323 | ||
324 | if (!zapi_nexthop_update_decode(zclient->ibuf, &nhr)) { | |
325 | zlog_warn("%s: Decode of update failed", __PRETTY_FUNCTION__); | |
326 | ||
327 | return 0; | |
328 | } | |
329 | ||
330 | zlog_debug("Received update for %s", | |
331 | prefix2str(&nhr.prefix, buf, sizeof(buf))); | |
86da53ab DS |
332 | |
333 | nht = sharp_nh_tracker_get(&nhr.prefix); | |
334 | nht->nhop_num = nhr.nexthop_num; | |
335 | nht->updates++; | |
336 | ||
0ae8130d DS |
337 | for (i = 0; i < nhr.nexthop_num; i++) { |
338 | struct zapi_nexthop *znh = &nhr.nexthops[i]; | |
339 | ||
340 | switch (znh->type) { | |
341 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
342 | case NEXTHOP_TYPE_IPV4: | |
343 | zlog_debug( | |
344 | "\tNexthop %s, type: %d, ifindex: %d, vrf: %d, label_num: %d", | |
345 | inet_ntop(AF_INET, &znh->gate.ipv4.s_addr, buf, | |
346 | sizeof(buf)), | |
347 | znh->type, znh->ifindex, znh->vrf_id, | |
348 | znh->label_num); | |
349 | break; | |
350 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
351 | case NEXTHOP_TYPE_IPV6: | |
352 | zlog_debug( | |
353 | "\tNexthop %s, type: %d, ifindex: %d, vrf: %d, label_num: %d", | |
354 | inet_ntop(AF_INET6, &znh->gate.ipv6, buf, | |
355 | sizeof(buf)), | |
356 | znh->type, znh->ifindex, znh->vrf_id, | |
357 | znh->label_num); | |
358 | break; | |
359 | case NEXTHOP_TYPE_IFINDEX: | |
360 | zlog_debug("\tNexthop IFINDEX: %d, ifindex: %d", | |
361 | znh->type, znh->ifindex); | |
362 | break; | |
363 | case NEXTHOP_TYPE_BLACKHOLE: | |
364 | zlog_debug("\tNexthop blackhole"); | |
365 | break; | |
366 | } | |
367 | } | |
368 | return 0; | |
369 | } | |
370 | ||
138c5a74 DS |
371 | static int sharp_ifp_destroy(struct interface *ifp) |
372 | { | |
373 | return 0; | |
374 | } | |
375 | ||
8a71d93d DS |
376 | extern struct zebra_privs_t sharp_privs; |
377 | ||
378 | void sharp_zebra_init(void) | |
379 | { | |
996c9314 | 380 | struct zclient_options opt = {.receive_notify = true}; |
8a71d93d | 381 | |
138c5a74 DS |
382 | if_zapi_callbacks(sharp_ifp_create, sharp_ifp_up, |
383 | sharp_ifp_down, sharp_ifp_destroy); | |
384 | ||
26f63a1e | 385 | zclient = zclient_new(master, &opt); |
8a71d93d DS |
386 | |
387 | zclient_init(zclient, ZEBRA_ROUTE_SHARP, 0, &sharp_privs); | |
388 | zclient->zebra_connected = zebra_connected; | |
8a71d93d | 389 | zclient->interface_delete = interface_delete; |
8a71d93d DS |
390 | zclient->interface_address_add = interface_address_add; |
391 | zclient->interface_address_delete = interface_address_delete; | |
28b11f81 | 392 | zclient->route_notify_owner = route_notify_owner; |
0ae8130d | 393 | zclient->nexthop_update = sharp_nexthop_update; |
b47dc61a | 394 | zclient->import_check_update = sharp_nexthop_update; |
8a71d93d | 395 | } |