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