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