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