]>
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 | ||
a36898e7 | 49 | static struct interface *zebra_interface_if_lookup(struct stream *s) |
8a71d93d DS |
50 | { |
51 | char ifname_tmp[INTERFACE_NAMSIZ]; | |
52 | ||
53 | /* Read interface name. */ | |
54 | stream_get(ifname_tmp, s, INTERFACE_NAMSIZ); | |
55 | ||
56 | /* And look it up. */ | |
a36898e7 | 57 | return if_lookup_by_name(ifname_tmp, VRF_DEFAULT); |
8a71d93d DS |
58 | } |
59 | ||
60 | /* Inteface addition message from zebra. */ | |
ef7bd2a3 | 61 | static int sharp_ifp_create(struct interface *ifp) |
8a71d93d | 62 | { |
8a71d93d DS |
63 | return 0; |
64 | } | |
65 | ||
121f9dee | 66 | static int interface_delete(ZAPI_CALLBACK_ARGS) |
8a71d93d DS |
67 | { |
68 | struct interface *ifp; | |
69 | struct stream *s; | |
70 | ||
71 | s = zclient->ibuf; | |
72 | /* zebra_interface_state_read () updates interface structure in iflist | |
73 | */ | |
74 | ifp = zebra_interface_state_read(s, vrf_id); | |
75 | ||
76 | if (ifp == NULL) | |
77 | return 0; | |
78 | ||
79 | if_set_index(ifp, IFINDEX_INTERNAL); | |
80 | ||
81 | return 0; | |
82 | } | |
83 | ||
121f9dee | 84 | static int interface_address_add(ZAPI_CALLBACK_ARGS) |
8a71d93d DS |
85 | { |
86 | ||
121f9dee | 87 | zebra_interface_address_read(cmd, zclient->ibuf, vrf_id); |
8a71d93d DS |
88 | |
89 | return 0; | |
90 | } | |
91 | ||
121f9dee | 92 | static int interface_address_delete(ZAPI_CALLBACK_ARGS) |
8a71d93d DS |
93 | { |
94 | struct connected *c; | |
95 | ||
121f9dee | 96 | c = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id); |
8a71d93d DS |
97 | |
98 | if (!c) | |
99 | return 0; | |
100 | ||
101 | connected_free(c); | |
102 | return 0; | |
103 | } | |
104 | ||
121f9dee | 105 | static int interface_state_up(ZAPI_CALLBACK_ARGS) |
8a71d93d DS |
106 | { |
107 | ||
a36898e7 | 108 | zebra_interface_if_lookup(zclient->ibuf); |
8a71d93d DS |
109 | |
110 | return 0; | |
111 | } | |
112 | ||
121f9dee | 113 | static int interface_state_down(ZAPI_CALLBACK_ARGS) |
8a71d93d DS |
114 | { |
115 | ||
116 | zebra_interface_state_read(zclient->ibuf, vrf_id); | |
117 | ||
118 | return 0; | |
119 | } | |
120 | ||
0cf08685 DS |
121 | void sharp_install_routes_helper(struct prefix *p, vrf_id_t vrf_id, |
122 | uint8_t instance, struct nexthop_group *nhg, | |
6b98d34f DS |
123 | uint32_t routes) |
124 | { | |
125 | uint32_t temp, i; | |
dbc1bf46 | 126 | bool v4 = false; |
6b98d34f DS |
127 | |
128 | zlog_debug("Inserting %u routes", routes); | |
129 | ||
dbc1bf46 DS |
130 | if (p->family == AF_INET) { |
131 | v4 = true; | |
132 | temp = ntohl(p->u.prefix4.s_addr); | |
133 | } else | |
134 | temp = ntohl(p->u.val32[3]); | |
135 | ||
547dc642 | 136 | monotime(&sg.r.t_start); |
6b98d34f | 137 | for (i = 0; i < routes; i++) { |
0cf08685 | 138 | route_add(p, vrf_id, (uint8_t)instance, nhg); |
dbc1bf46 DS |
139 | if (v4) |
140 | p->u.prefix4.s_addr = htonl(++temp); | |
141 | else | |
142 | p->u.val32[3] = htonl(++temp); | |
6b98d34f DS |
143 | } |
144 | } | |
145 | ||
0cf08685 DS |
146 | void sharp_remove_routes_helper(struct prefix *p, vrf_id_t vrf_id, |
147 | uint8_t instance, uint32_t routes) | |
6b98d34f DS |
148 | { |
149 | uint32_t temp, i; | |
dbc1bf46 | 150 | bool v4 = false; |
6b98d34f DS |
151 | |
152 | zlog_debug("Removing %u routes", routes); | |
153 | ||
dbc1bf46 DS |
154 | if (p->family == AF_INET) { |
155 | v4 = true; | |
156 | temp = ntohl(p->u.prefix4.s_addr); | |
157 | } else | |
158 | temp = ntohl(p->u.val32[3]); | |
159 | ||
547dc642 | 160 | monotime(&sg.r.t_start); |
6b98d34f | 161 | for (i = 0; i < routes; i++) { |
0cf08685 | 162 | route_delete(p, vrf_id, (uint8_t)instance); |
dbc1bf46 DS |
163 | if (v4) |
164 | p->u.prefix4.s_addr = htonl(++temp); | |
165 | else | |
166 | p->u.val32[3] = htonl(++temp); | |
6b98d34f DS |
167 | } |
168 | } | |
169 | ||
b939f6ff | 170 | static void handle_repeated(bool installed) |
6b98d34f | 171 | { |
547dc642 DS |
172 | struct prefix p = sg.r.orig_prefix; |
173 | sg.r.repeat--; | |
6b98d34f | 174 | |
547dc642 | 175 | if (sg.r.repeat <= 0) |
6b98d34f DS |
176 | return; |
177 | ||
178 | if (installed) { | |
547dc642 | 179 | sg.r.removed_routes = 0; |
0cf08685 DS |
180 | sharp_remove_routes_helper(&p, sg.r.vrf_id, |
181 | sg.r.inst, sg.r.total_routes); | |
6b98d34f DS |
182 | } |
183 | ||
f54f37c1 | 184 | if (!installed) { |
547dc642 | 185 | sg.r.installed_routes = 0; |
0cf08685 DS |
186 | sharp_install_routes_helper(&p, sg.r.vrf_id, sg.r.inst, |
187 | &sg.r.nhop_group, | |
547dc642 | 188 | sg.r.total_routes); |
6b98d34f DS |
189 | } |
190 | } | |
191 | ||
121f9dee | 192 | static int route_notify_owner(ZAPI_CALLBACK_ARGS) |
8a71d93d | 193 | { |
25c84d86 | 194 | struct timeval r; |
8a71d93d DS |
195 | struct prefix p; |
196 | enum zapi_route_notify_owner note; | |
28610f7e | 197 | uint32_t table; |
8a71d93d | 198 | |
28610f7e | 199 | if (!zapi_route_notify_decode(zclient->ibuf, &p, &table, ¬e)) |
8a71d93d DS |
200 | return -1; |
201 | ||
5e54c602 DS |
202 | switch (note) { |
203 | case ZAPI_ROUTE_INSTALLED: | |
547dc642 DS |
204 | sg.r.installed_routes++; |
205 | if (sg.r.total_routes == sg.r.installed_routes) { | |
206 | monotime(&sg.r.t_end); | |
207 | timersub(&sg.r.t_end, &sg.r.t_start, &r); | |
051a0be4 DL |
208 | zlog_debug("Installed All Items %jd.%ld", |
209 | (intmax_t)r.tv_sec, (long)r.tv_usec); | |
6b98d34f DS |
210 | handle_repeated(true); |
211 | } | |
5e54c602 DS |
212 | break; |
213 | case ZAPI_ROUTE_FAIL_INSTALL: | |
214 | zlog_debug("Failed install of route"); | |
215 | break; | |
216 | case ZAPI_ROUTE_BETTER_ADMIN_WON: | |
217 | zlog_debug("Better Admin Distance won over us"); | |
218 | break; | |
219 | case ZAPI_ROUTE_REMOVED: | |
547dc642 DS |
220 | sg.r.removed_routes++; |
221 | if (sg.r.total_routes == sg.r.removed_routes) { | |
222 | monotime(&sg.r.t_end); | |
223 | timersub(&sg.r.t_end, &sg.r.t_start, &r); | |
051a0be4 DL |
224 | zlog_debug("Removed all Items %jd.%ld", |
225 | (intmax_t)r.tv_sec, (long)r.tv_usec); | |
6b98d34f DS |
226 | handle_repeated(false); |
227 | } | |
5e54c602 DS |
228 | break; |
229 | case ZAPI_ROUTE_REMOVE_FAIL: | |
230 | zlog_debug("Route removal Failure"); | |
231 | break; | |
232 | } | |
8a71d93d DS |
233 | return 0; |
234 | } | |
235 | ||
236 | static void zebra_connected(struct zclient *zclient) | |
237 | { | |
238 | zclient_send_reg_requests(zclient, VRF_DEFAULT); | |
239 | } | |
240 | ||
7d061b3c | 241 | void vrf_label_add(vrf_id_t vrf_id, afi_t afi, mpls_label_t label) |
ab18a495 | 242 | { |
7d061b3c | 243 | zclient_send_vrf_label(zclient, vrf_id, afi, label, ZEBRA_LSP_SHARP); |
ab18a495 DS |
244 | } |
245 | ||
0cf08685 DS |
246 | void route_add(struct prefix *p, vrf_id_t vrf_id, |
247 | uint8_t instance, struct nexthop_group *nhg) | |
8a71d93d DS |
248 | { |
249 | struct zapi_route api; | |
250 | struct zapi_nexthop *api_nh; | |
694b242f DS |
251 | struct nexthop *nh; |
252 | int i = 0; | |
8a71d93d DS |
253 | |
254 | memset(&api, 0, sizeof(api)); | |
0cf08685 | 255 | api.vrf_id = vrf_id; |
8a71d93d | 256 | api.type = ZEBRA_ROUTE_SHARP; |
ae252c02 | 257 | api.instance = instance; |
8a71d93d DS |
258 | api.safi = SAFI_UNICAST; |
259 | memcpy(&api.prefix, p, sizeof(*p)); | |
260 | ||
a3896844 | 261 | SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION); |
8a71d93d DS |
262 | SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP); |
263 | ||
694b242f DS |
264 | for (ALL_NEXTHOPS_PTR(nhg, nh)) { |
265 | api_nh = &api.nexthops[i]; | |
0cf08685 | 266 | api_nh->vrf_id = nh->vrf_id; |
694b242f DS |
267 | api_nh->type = nh->type; |
268 | switch (nh->type) { | |
269 | case NEXTHOP_TYPE_IPV4: | |
270 | api_nh->gate = nh->gate; | |
271 | break; | |
272 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
273 | api_nh->gate = nh->gate; | |
274 | api_nh->ifindex = nh->ifindex; | |
275 | break; | |
276 | case NEXTHOP_TYPE_IFINDEX: | |
277 | api_nh->ifindex = nh->ifindex; | |
278 | break; | |
279 | case NEXTHOP_TYPE_IPV6: | |
280 | memcpy(&api_nh->gate.ipv6, &nh->gate.ipv6, 16); | |
281 | break; | |
282 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
283 | api_nh->ifindex = nh->ifindex; | |
284 | memcpy(&api_nh->gate.ipv6, &nh->gate.ipv6, 16); | |
285 | break; | |
286 | case NEXTHOP_TYPE_BLACKHOLE: | |
287 | api_nh->bh_type = nh->bh_type; | |
288 | break; | |
289 | } | |
290 | i++; | |
291 | } | |
292 | api.nexthop_num = i; | |
8a71d93d DS |
293 | |
294 | zclient_route_send(ZEBRA_ROUTE_ADD, zclient, &api); | |
295 | } | |
296 | ||
0cf08685 | 297 | void route_delete(struct prefix *p, vrf_id_t vrf_id, uint8_t instance) |
8a71d93d DS |
298 | { |
299 | struct zapi_route api; | |
300 | ||
301 | memset(&api, 0, sizeof(api)); | |
0cf08685 | 302 | api.vrf_id = vrf_id; |
8a71d93d DS |
303 | api.type = ZEBRA_ROUTE_SHARP; |
304 | api.safi = SAFI_UNICAST; | |
ae252c02 | 305 | api.instance = instance; |
8a71d93d DS |
306 | memcpy(&api.prefix, p, sizeof(*p)); |
307 | zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api); | |
308 | ||
309 | return; | |
310 | } | |
311 | ||
91529dc8 | 312 | void sharp_zebra_nexthop_watch(struct prefix *p, vrf_id_t vrf_id, bool import, |
b47dc61a | 313 | bool watch, bool connected) |
0ae8130d | 314 | { |
b47dc61a | 315 | int command; |
0ae8130d | 316 | |
b47dc61a DS |
317 | if (!import) { |
318 | command = ZEBRA_NEXTHOP_REGISTER; | |
319 | ||
320 | if (!watch) | |
321 | command = ZEBRA_NEXTHOP_UNREGISTER; | |
322 | } else { | |
323 | command = ZEBRA_IMPORT_ROUTE_REGISTER; | |
324 | ||
325 | if (!watch) | |
326 | command = ZEBRA_IMPORT_ROUTE_UNREGISTER; | |
327 | } | |
0ae8130d | 328 | |
91529dc8 | 329 | if (zclient_send_rnh(zclient, command, p, connected, vrf_id) < 0) |
b3beaea0 A |
330 | zlog_warn("%s: Failure to send nexthop to zebra", |
331 | __PRETTY_FUNCTION__); | |
0ae8130d DS |
332 | } |
333 | ||
121f9dee | 334 | static int sharp_nexthop_update(ZAPI_CALLBACK_ARGS) |
0ae8130d | 335 | { |
86da53ab | 336 | struct sharp_nh_tracker *nht; |
0ae8130d DS |
337 | struct zapi_route nhr; |
338 | char buf[PREFIX_STRLEN]; | |
339 | int i; | |
340 | ||
341 | if (!zapi_nexthop_update_decode(zclient->ibuf, &nhr)) { | |
342 | zlog_warn("%s: Decode of update failed", __PRETTY_FUNCTION__); | |
343 | ||
344 | return 0; | |
345 | } | |
346 | ||
347 | zlog_debug("Received update for %s", | |
348 | prefix2str(&nhr.prefix, buf, sizeof(buf))); | |
86da53ab DS |
349 | |
350 | nht = sharp_nh_tracker_get(&nhr.prefix); | |
351 | nht->nhop_num = nhr.nexthop_num; | |
352 | nht->updates++; | |
353 | ||
0ae8130d DS |
354 | for (i = 0; i < nhr.nexthop_num; i++) { |
355 | struct zapi_nexthop *znh = &nhr.nexthops[i]; | |
356 | ||
357 | switch (znh->type) { | |
358 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
359 | case NEXTHOP_TYPE_IPV4: | |
360 | zlog_debug( | |
361 | "\tNexthop %s, type: %d, ifindex: %d, vrf: %d, label_num: %d", | |
362 | inet_ntop(AF_INET, &znh->gate.ipv4.s_addr, buf, | |
363 | sizeof(buf)), | |
364 | znh->type, znh->ifindex, znh->vrf_id, | |
365 | znh->label_num); | |
366 | break; | |
367 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
368 | case NEXTHOP_TYPE_IPV6: | |
369 | zlog_debug( | |
370 | "\tNexthop %s, type: %d, ifindex: %d, vrf: %d, label_num: %d", | |
371 | inet_ntop(AF_INET6, &znh->gate.ipv6, buf, | |
372 | sizeof(buf)), | |
373 | znh->type, znh->ifindex, znh->vrf_id, | |
374 | znh->label_num); | |
375 | break; | |
376 | case NEXTHOP_TYPE_IFINDEX: | |
377 | zlog_debug("\tNexthop IFINDEX: %d, ifindex: %d", | |
378 | znh->type, znh->ifindex); | |
379 | break; | |
380 | case NEXTHOP_TYPE_BLACKHOLE: | |
381 | zlog_debug("\tNexthop blackhole"); | |
382 | break; | |
383 | } | |
384 | } | |
385 | return 0; | |
386 | } | |
387 | ||
138c5a74 DS |
388 | static int sharp_ifp_up(struct interface *ifp) |
389 | { | |
390 | return 0; | |
391 | } | |
392 | ||
393 | static int sharp_ifp_down(struct interface *ifp) | |
394 | { | |
395 | return 0; | |
396 | } | |
397 | ||
398 | static int sharp_ifp_destroy(struct interface *ifp) | |
399 | { | |
400 | return 0; | |
401 | } | |
402 | ||
8a71d93d DS |
403 | extern struct zebra_privs_t sharp_privs; |
404 | ||
405 | void sharp_zebra_init(void) | |
406 | { | |
996c9314 | 407 | struct zclient_options opt = {.receive_notify = true}; |
8a71d93d | 408 | |
138c5a74 DS |
409 | if_zapi_callbacks(sharp_ifp_create, sharp_ifp_up, |
410 | sharp_ifp_down, sharp_ifp_destroy); | |
411 | ||
26f63a1e | 412 | zclient = zclient_new(master, &opt); |
8a71d93d DS |
413 | |
414 | zclient_init(zclient, ZEBRA_ROUTE_SHARP, 0, &sharp_privs); | |
415 | zclient->zebra_connected = zebra_connected; | |
8a71d93d DS |
416 | zclient->interface_delete = interface_delete; |
417 | zclient->interface_up = interface_state_up; | |
418 | zclient->interface_down = interface_state_down; | |
419 | zclient->interface_address_add = interface_address_add; | |
420 | zclient->interface_address_delete = interface_address_delete; | |
28b11f81 | 421 | zclient->route_notify_owner = route_notify_owner; |
0ae8130d | 422 | zclient->nexthop_update = sharp_nexthop_update; |
b47dc61a | 423 | zclient->import_check_update = sharp_nexthop_update; |
8a71d93d | 424 | } |