]>
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 | ||
6b98d34f DS |
134 | void sharp_install_routes_helper(struct prefix *p, uint8_t instance, |
135 | struct nexthop_group *nhg, | |
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 DS |
150 | for (i = 0; i < routes; i++) { |
151 | route_add(p, (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 | ||
159 | void sharp_remove_routes_helper(struct prefix *p, uint8_t instance, | |
160 | uint32_t routes) | |
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 DS |
174 | for (i = 0; i < routes; i++) { |
175 | route_delete(p, (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 DS |
192 | sg.r.removed_routes = 0; |
193 | sharp_remove_routes_helper(&p, sg.r.inst, sg.r.total_routes); | |
6b98d34f DS |
194 | } |
195 | ||
547dc642 DS |
196 | if (installed) { |
197 | sg.r.installed_routes = 0; | |
198 | sharp_install_routes_helper(&p, sg.r.inst, &sg.r.nhop_group, | |
199 | sg.r.total_routes); | |
6b98d34f DS |
200 | } |
201 | } | |
202 | ||
28b11f81 DS |
203 | static int route_notify_owner(int command, struct zclient *zclient, |
204 | zebra_size_t length, vrf_id_t vrf_id) | |
8a71d93d | 205 | { |
25c84d86 | 206 | struct timeval r; |
8a71d93d DS |
207 | struct prefix p; |
208 | enum zapi_route_notify_owner note; | |
28610f7e | 209 | uint32_t table; |
8a71d93d | 210 | |
28610f7e | 211 | if (!zapi_route_notify_decode(zclient->ibuf, &p, &table, ¬e)) |
8a71d93d DS |
212 | return -1; |
213 | ||
5e54c602 DS |
214 | switch (note) { |
215 | case ZAPI_ROUTE_INSTALLED: | |
547dc642 DS |
216 | sg.r.installed_routes++; |
217 | if (sg.r.total_routes == sg.r.installed_routes) { | |
218 | monotime(&sg.r.t_end); | |
219 | timersub(&sg.r.t_end, &sg.r.t_start, &r); | |
25c84d86 DS |
220 | zlog_debug("Installed All Items %ld.%ld", r.tv_sec, |
221 | r.tv_usec); | |
6b98d34f DS |
222 | handle_repeated(true); |
223 | } | |
5e54c602 DS |
224 | break; |
225 | case ZAPI_ROUTE_FAIL_INSTALL: | |
226 | zlog_debug("Failed install of route"); | |
227 | break; | |
228 | case ZAPI_ROUTE_BETTER_ADMIN_WON: | |
229 | zlog_debug("Better Admin Distance won over us"); | |
230 | break; | |
231 | case ZAPI_ROUTE_REMOVED: | |
547dc642 DS |
232 | sg.r.removed_routes++; |
233 | if (sg.r.total_routes == sg.r.removed_routes) { | |
234 | monotime(&sg.r.t_end); | |
235 | timersub(&sg.r.t_end, &sg.r.t_start, &r); | |
25c84d86 DS |
236 | zlog_debug("Removed all Items %ld.%ld", r.tv_sec, |
237 | r.tv_usec); | |
6b98d34f DS |
238 | handle_repeated(false); |
239 | } | |
5e54c602 DS |
240 | break; |
241 | case ZAPI_ROUTE_REMOVE_FAIL: | |
242 | zlog_debug("Route removal Failure"); | |
243 | break; | |
244 | } | |
8a71d93d DS |
245 | return 0; |
246 | } | |
247 | ||
248 | static void zebra_connected(struct zclient *zclient) | |
249 | { | |
250 | zclient_send_reg_requests(zclient, VRF_DEFAULT); | |
251 | } | |
252 | ||
7d061b3c | 253 | void vrf_label_add(vrf_id_t vrf_id, afi_t afi, mpls_label_t label) |
ab18a495 | 254 | { |
7d061b3c | 255 | zclient_send_vrf_label(zclient, vrf_id, afi, label, ZEBRA_LSP_SHARP); |
ab18a495 DS |
256 | } |
257 | ||
694b242f | 258 | void route_add(struct prefix *p, uint8_t instance, struct nexthop_group *nhg) |
8a71d93d DS |
259 | { |
260 | struct zapi_route api; | |
261 | struct zapi_nexthop *api_nh; | |
694b242f DS |
262 | struct nexthop *nh; |
263 | int i = 0; | |
8a71d93d DS |
264 | |
265 | memset(&api, 0, sizeof(api)); | |
266 | api.vrf_id = VRF_DEFAULT; | |
267 | api.type = ZEBRA_ROUTE_SHARP; | |
ae252c02 | 268 | api.instance = instance; |
8a71d93d DS |
269 | api.safi = SAFI_UNICAST; |
270 | memcpy(&api.prefix, p, sizeof(*p)); | |
271 | ||
a3896844 | 272 | SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION); |
8a71d93d DS |
273 | SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP); |
274 | ||
694b242f DS |
275 | for (ALL_NEXTHOPS_PTR(nhg, nh)) { |
276 | api_nh = &api.nexthops[i]; | |
277 | api_nh->vrf_id = VRF_DEFAULT; | |
278 | api_nh->type = nh->type; | |
279 | switch (nh->type) { | |
280 | case NEXTHOP_TYPE_IPV4: | |
281 | api_nh->gate = nh->gate; | |
282 | break; | |
283 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
284 | api_nh->gate = nh->gate; | |
285 | api_nh->ifindex = nh->ifindex; | |
286 | break; | |
287 | case NEXTHOP_TYPE_IFINDEX: | |
288 | api_nh->ifindex = nh->ifindex; | |
289 | break; | |
290 | case NEXTHOP_TYPE_IPV6: | |
291 | memcpy(&api_nh->gate.ipv6, &nh->gate.ipv6, 16); | |
292 | break; | |
293 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
294 | api_nh->ifindex = nh->ifindex; | |
295 | memcpy(&api_nh->gate.ipv6, &nh->gate.ipv6, 16); | |
296 | break; | |
297 | case NEXTHOP_TYPE_BLACKHOLE: | |
298 | api_nh->bh_type = nh->bh_type; | |
299 | break; | |
300 | } | |
301 | i++; | |
302 | } | |
303 | api.nexthop_num = i; | |
8a71d93d DS |
304 | |
305 | zclient_route_send(ZEBRA_ROUTE_ADD, zclient, &api); | |
306 | } | |
307 | ||
ae252c02 | 308 | void route_delete(struct prefix *p, uint8_t instance) |
8a71d93d DS |
309 | { |
310 | struct zapi_route api; | |
311 | ||
312 | memset(&api, 0, sizeof(api)); | |
313 | api.vrf_id = VRF_DEFAULT; | |
314 | api.type = ZEBRA_ROUTE_SHARP; | |
315 | api.safi = SAFI_UNICAST; | |
ae252c02 | 316 | api.instance = instance; |
8a71d93d DS |
317 | memcpy(&api.prefix, p, sizeof(*p)); |
318 | zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api); | |
319 | ||
320 | return; | |
321 | } | |
322 | ||
a60ffbc9 | 323 | void sharp_zebra_nexthop_watch(struct prefix *p, bool watch, bool connected) |
0ae8130d DS |
324 | { |
325 | int command = ZEBRA_NEXTHOP_REGISTER; | |
326 | ||
327 | if (!watch) | |
328 | command = ZEBRA_NEXTHOP_UNREGISTER; | |
329 | ||
a60ffbc9 | 330 | if (zclient_send_rnh(zclient, command, p, connected, VRF_DEFAULT) < 0) |
b3beaea0 A |
331 | zlog_warn("%s: Failure to send nexthop to zebra", |
332 | __PRETTY_FUNCTION__); | |
0ae8130d DS |
333 | } |
334 | ||
335 | static int sharp_nexthop_update(int command, struct zclient *zclient, | |
336 | zebra_size_t length, vrf_id_t vrf_id) | |
337 | { | |
86da53ab | 338 | struct sharp_nh_tracker *nht; |
0ae8130d DS |
339 | struct zapi_route nhr; |
340 | char buf[PREFIX_STRLEN]; | |
341 | int i; | |
342 | ||
343 | if (!zapi_nexthop_update_decode(zclient->ibuf, &nhr)) { | |
344 | zlog_warn("%s: Decode of update failed", __PRETTY_FUNCTION__); | |
345 | ||
346 | return 0; | |
347 | } | |
348 | ||
349 | zlog_debug("Received update for %s", | |
350 | prefix2str(&nhr.prefix, buf, sizeof(buf))); | |
86da53ab DS |
351 | |
352 | nht = sharp_nh_tracker_get(&nhr.prefix); | |
353 | nht->nhop_num = nhr.nexthop_num; | |
354 | nht->updates++; | |
355 | ||
0ae8130d DS |
356 | for (i = 0; i < nhr.nexthop_num; i++) { |
357 | struct zapi_nexthop *znh = &nhr.nexthops[i]; | |
358 | ||
359 | switch (znh->type) { | |
360 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
361 | case NEXTHOP_TYPE_IPV4: | |
362 | zlog_debug( | |
363 | "\tNexthop %s, type: %d, ifindex: %d, vrf: %d, label_num: %d", | |
364 | inet_ntop(AF_INET, &znh->gate.ipv4.s_addr, buf, | |
365 | sizeof(buf)), | |
366 | znh->type, znh->ifindex, znh->vrf_id, | |
367 | znh->label_num); | |
368 | break; | |
369 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
370 | case NEXTHOP_TYPE_IPV6: | |
371 | zlog_debug( | |
372 | "\tNexthop %s, type: %d, ifindex: %d, vrf: %d, label_num: %d", | |
373 | inet_ntop(AF_INET6, &znh->gate.ipv6, buf, | |
374 | sizeof(buf)), | |
375 | znh->type, znh->ifindex, znh->vrf_id, | |
376 | znh->label_num); | |
377 | break; | |
378 | case NEXTHOP_TYPE_IFINDEX: | |
379 | zlog_debug("\tNexthop IFINDEX: %d, ifindex: %d", | |
380 | znh->type, znh->ifindex); | |
381 | break; | |
382 | case NEXTHOP_TYPE_BLACKHOLE: | |
383 | zlog_debug("\tNexthop blackhole"); | |
384 | break; | |
385 | } | |
386 | } | |
387 | return 0; | |
388 | } | |
389 | ||
8a71d93d DS |
390 | extern struct zebra_privs_t sharp_privs; |
391 | ||
392 | void sharp_zebra_init(void) | |
393 | { | |
996c9314 | 394 | struct zclient_options opt = {.receive_notify = true}; |
8a71d93d | 395 | |
26f63a1e | 396 | zclient = zclient_new(master, &opt); |
8a71d93d DS |
397 | |
398 | zclient_init(zclient, ZEBRA_ROUTE_SHARP, 0, &sharp_privs); | |
399 | zclient->zebra_connected = zebra_connected; | |
400 | zclient->interface_add = interface_add; | |
401 | zclient->interface_delete = interface_delete; | |
402 | zclient->interface_up = interface_state_up; | |
403 | zclient->interface_down = interface_state_down; | |
404 | zclient->interface_address_add = interface_address_add; | |
405 | zclient->interface_address_delete = interface_address_delete; | |
28b11f81 | 406 | zclient->route_notify_owner = route_notify_owner; |
0ae8130d | 407 | zclient->nexthop_update = sharp_nexthop_update; |
8a71d93d | 408 | } |