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