]> git.proxmox.com Git - mirror_frr.git/blame - sharpd/sharp_zebra.c
lib,sharpd: add a SHARP_STR alias
[mirror_frr.git] / sharpd / sharp_zebra.c
CommitLineData
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. */
44struct zclient *zclient = NULL;
45
46/* For registering threads. */
47extern struct thread_master *master;
48
8a71d93d 49/* Inteface addition message from zebra. */
ef7bd2a3 50static int sharp_ifp_create(struct interface *ifp)
8a71d93d 51{
8a71d93d
DS
52 return 0;
53}
54
3c3c3252 55static int sharp_ifp_destroy(struct interface *ifp)
8a71d93d 56{
8a71d93d
DS
57 return 0;
58}
59
121f9dee 60static int interface_address_add(ZAPI_CALLBACK_ARGS)
8a71d93d 61{
121f9dee 62 zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
8a71d93d
DS
63
64 return 0;
65}
66
121f9dee 67static int interface_address_delete(ZAPI_CALLBACK_ARGS)
8a71d93d
DS
68{
69 struct connected *c;
70
121f9dee 71 c = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
8a71d93d
DS
72
73 if (!c)
74 return 0;
75
721c0857 76 connected_free(&c);
8a71d93d
DS
77 return 0;
78}
79
ddbf3e60 80static int sharp_ifp_up(struct interface *ifp)
8a71d93d 81{
8a71d93d
DS
82 return 0;
83}
84
b0b69e59 85static int sharp_ifp_down(struct interface *ifp)
8a71d93d 86{
8a71d93d
DS
87 return 0;
88}
89
c9e5adba
MS
90int sharp_install_lsps_helper(bool install_p, const struct prefix *p,
91 uint8_t type, int instance, uint32_t in_label,
665edffd
MS
92 const struct nexthop_group *nhg,
93 const struct nexthop_group *backup_nhg)
c9e5adba
MS
94{
95 struct zapi_labels zl = {};
96 struct zapi_nexthop *znh;
97 const struct nexthop *nh;
98 int i, ret;
99
100 zl.type = ZEBRA_LSP_SHARP;
101 zl.local_label = in_label;
102
103 if (p) {
104 SET_FLAG(zl.message, ZAPI_LABELS_FTN);
105 prefix_copy(&zl.route.prefix, p);
106 zl.route.type = type;
107 zl.route.instance = instance;
108 }
109
665edffd 110 /* List of nexthops is optional for delete */
c9e5adba 111 i = 0;
665edffd
MS
112 if (nhg) {
113 for (ALL_NEXTHOPS_PTR(nhg, nh)) {
114 znh = &zl.nexthops[i];
115
116 /* Must have labels to be useful */
117 if (nh->nh_label == NULL ||
118 nh->nh_label->num_labels == 0)
119 continue;
120
121 if (nh->type == NEXTHOP_TYPE_IFINDEX ||
122 nh->type == NEXTHOP_TYPE_BLACKHOLE)
123 /* Hmm - can't really deal with these types */
124 continue;
125
126 ret = zapi_nexthop_from_nexthop(znh, nh);
127 if (ret < 0)
128 return -1;
129
130 i++;
131 }
132 }
c9e5adba 133
665edffd
MS
134 /* Whoops - no nexthops isn't very useful for install */
135 if (i == 0 && install_p)
136 return -1;
c9e5adba 137
665edffd 138 zl.nexthop_num = i;
c9e5adba 139
665edffd
MS
140 /* Add optional backup nexthop info. Since these are used by index,
141 * we can't just skip over an invalid backup nexthop: we will
142 * invalidate the entire operation.
143 */
144 if (backup_nhg != NULL) {
145 i = 0;
146 for (ALL_NEXTHOPS_PTR(backup_nhg, nh)) {
147 znh = &zl.backup_nexthops[i];
c9e5adba 148
665edffd
MS
149 /* Must have labels to be useful */
150 if (nh->nh_label == NULL ||
151 nh->nh_label->num_labels == 0)
152 return -1;
c9e5adba 153
665edffd
MS
154 if (nh->type == NEXTHOP_TYPE_IFINDEX ||
155 nh->type == NEXTHOP_TYPE_BLACKHOLE)
156 /* Hmm - can't really deal with these types */
157 return -1;
c9e5adba 158
665edffd
MS
159 ret = zapi_nexthop_from_nexthop(znh, nh);
160 if (ret < 0)
161 return -1;
162
163 i++;
164 }
165
166 if (i > 0)
167 SET_FLAG(zl.message, ZAPI_LABELS_HAS_BACKUPS);
168
169 zl.backup_nexthop_num = i;
170 }
c9e5adba
MS
171
172 if (install_p)
173 ret = zebra_send_mpls_labels(zclient, ZEBRA_MPLS_LABELS_ADD,
174 &zl);
175 else
176 ret = zebra_send_mpls_labels(zclient, ZEBRA_MPLS_LABELS_DELETE,
177 &zl);
178
179 return ret;
180}
181
0cf08685 182void sharp_install_routes_helper(struct prefix *p, vrf_id_t vrf_id,
1df3b1dc
MS
183 uint8_t instance,
184 const struct nexthop_group *nhg,
185 const struct nexthop_group *backup_nhg,
6b98d34f
DS
186 uint32_t routes)
187{
188 uint32_t temp, i;
dbc1bf46 189 bool v4 = false;
6b98d34f
DS
190
191 zlog_debug("Inserting %u routes", routes);
192
dbc1bf46
DS
193 if (p->family == AF_INET) {
194 v4 = true;
195 temp = ntohl(p->u.prefix4.s_addr);
196 } else
197 temp = ntohl(p->u.val32[3]);
198
1df3b1dc
MS
199 /* Only use backup route/nexthops if present */
200 if (backup_nhg && (backup_nhg->nexthop == NULL))
201 backup_nhg = NULL;
202
547dc642 203 monotime(&sg.r.t_start);
6b98d34f 204 for (i = 0; i < routes; i++) {
1df3b1dc 205 route_add(p, vrf_id, (uint8_t)instance, nhg, backup_nhg);
dbc1bf46
DS
206 if (v4)
207 p->u.prefix4.s_addr = htonl(++temp);
208 else
209 p->u.val32[3] = htonl(++temp);
6b98d34f
DS
210 }
211}
212
0cf08685
DS
213void sharp_remove_routes_helper(struct prefix *p, vrf_id_t vrf_id,
214 uint8_t instance, uint32_t routes)
6b98d34f
DS
215{
216 uint32_t temp, i;
dbc1bf46 217 bool v4 = false;
6b98d34f
DS
218
219 zlog_debug("Removing %u routes", routes);
220
dbc1bf46
DS
221 if (p->family == AF_INET) {
222 v4 = true;
223 temp = ntohl(p->u.prefix4.s_addr);
224 } else
225 temp = ntohl(p->u.val32[3]);
226
547dc642 227 monotime(&sg.r.t_start);
6b98d34f 228 for (i = 0; i < routes; i++) {
0cf08685 229 route_delete(p, vrf_id, (uint8_t)instance);
dbc1bf46
DS
230 if (v4)
231 p->u.prefix4.s_addr = htonl(++temp);
232 else
233 p->u.val32[3] = htonl(++temp);
6b98d34f
DS
234 }
235}
236
b939f6ff 237static void handle_repeated(bool installed)
6b98d34f 238{
547dc642
DS
239 struct prefix p = sg.r.orig_prefix;
240 sg.r.repeat--;
6b98d34f 241
547dc642 242 if (sg.r.repeat <= 0)
6b98d34f
DS
243 return;
244
245 if (installed) {
547dc642 246 sg.r.removed_routes = 0;
0cf08685
DS
247 sharp_remove_routes_helper(&p, sg.r.vrf_id,
248 sg.r.inst, sg.r.total_routes);
6b98d34f
DS
249 }
250
f54f37c1 251 if (!installed) {
547dc642 252 sg.r.installed_routes = 0;
0cf08685
DS
253 sharp_install_routes_helper(&p, sg.r.vrf_id, sg.r.inst,
254 &sg.r.nhop_group,
1df3b1dc 255 &sg.r.backup_nhop_group,
547dc642 256 sg.r.total_routes);
6b98d34f
DS
257 }
258}
259
121f9dee 260static int route_notify_owner(ZAPI_CALLBACK_ARGS)
8a71d93d 261{
25c84d86 262 struct timeval r;
8a71d93d
DS
263 struct prefix p;
264 enum zapi_route_notify_owner note;
28610f7e 265 uint32_t table;
8a71d93d 266
28610f7e 267 if (!zapi_route_notify_decode(zclient->ibuf, &p, &table, &note))
8a71d93d
DS
268 return -1;
269
5e54c602
DS
270 switch (note) {
271 case ZAPI_ROUTE_INSTALLED:
547dc642
DS
272 sg.r.installed_routes++;
273 if (sg.r.total_routes == sg.r.installed_routes) {
274 monotime(&sg.r.t_end);
275 timersub(&sg.r.t_end, &sg.r.t_start, &r);
051a0be4
DL
276 zlog_debug("Installed All Items %jd.%ld",
277 (intmax_t)r.tv_sec, (long)r.tv_usec);
6b98d34f
DS
278 handle_repeated(true);
279 }
5e54c602
DS
280 break;
281 case ZAPI_ROUTE_FAIL_INSTALL:
282 zlog_debug("Failed install of route");
283 break;
284 case ZAPI_ROUTE_BETTER_ADMIN_WON:
285 zlog_debug("Better Admin Distance won over us");
286 break;
287 case ZAPI_ROUTE_REMOVED:
547dc642
DS
288 sg.r.removed_routes++;
289 if (sg.r.total_routes == sg.r.removed_routes) {
290 monotime(&sg.r.t_end);
291 timersub(&sg.r.t_end, &sg.r.t_start, &r);
051a0be4
DL
292 zlog_debug("Removed all Items %jd.%ld",
293 (intmax_t)r.tv_sec, (long)r.tv_usec);
6b98d34f
DS
294 handle_repeated(false);
295 }
5e54c602
DS
296 break;
297 case ZAPI_ROUTE_REMOVE_FAIL:
298 zlog_debug("Route removal Failure");
299 break;
300 }
8a71d93d
DS
301 return 0;
302}
303
304static void zebra_connected(struct zclient *zclient)
305{
306 zclient_send_reg_requests(zclient, VRF_DEFAULT);
67a9eda9
DS
307
308 /*
309 * Do not actually turn this on yet
310 * This is just the start of the infrastructure needed here
311 * This can be fixed at a later time.
312 *
313 * zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP,
314 * ZEBRA_ROUTE_ALL, 0, VRF_DEFAULT);
315 */
8a71d93d
DS
316}
317
7d061b3c 318void vrf_label_add(vrf_id_t vrf_id, afi_t afi, mpls_label_t label)
ab18a495 319{
7d061b3c 320 zclient_send_vrf_label(zclient, vrf_id, afi, label, ZEBRA_LSP_SHARP);
ab18a495
DS
321}
322
1df3b1dc
MS
323void route_add(const struct prefix *p, vrf_id_t vrf_id,
324 uint8_t instance, const struct nexthop_group *nhg,
325 const struct nexthop_group *backup_nhg)
8a71d93d
DS
326{
327 struct zapi_route api;
328 struct zapi_nexthop *api_nh;
694b242f
DS
329 struct nexthop *nh;
330 int i = 0;
8a71d93d
DS
331
332 memset(&api, 0, sizeof(api));
0cf08685 333 api.vrf_id = vrf_id;
8a71d93d 334 api.type = ZEBRA_ROUTE_SHARP;
ae252c02 335 api.instance = instance;
8a71d93d
DS
336 api.safi = SAFI_UNICAST;
337 memcpy(&api.prefix, p, sizeof(*p));
338
a3896844 339 SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION);
8a71d93d
DS
340 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
341
694b242f
DS
342 for (ALL_NEXTHOPS_PTR(nhg, nh)) {
343 api_nh = &api.nexthops[i];
054af19a 344
c9e5adba 345 zapi_nexthop_from_nexthop(api_nh, nh);
1df3b1dc 346
694b242f
DS
347 i++;
348 }
349 api.nexthop_num = i;
8a71d93d 350
1df3b1dc
MS
351 /* Include backup nexthops, if present */
352 if (backup_nhg && backup_nhg->nexthop) {
353 SET_FLAG(api.message, ZAPI_MESSAGE_BACKUP_NEXTHOPS);
354
355 i = 0;
356 for (ALL_NEXTHOPS_PTR(backup_nhg, nh)) {
357 api_nh = &api.backup_nexthops[i];
358
359 zapi_backup_nexthop_from_nexthop(api_nh, nh);
360
361 i++;
362 }
363
364 api.backup_nexthop_num = i;
365 }
366
8a71d93d
DS
367 zclient_route_send(ZEBRA_ROUTE_ADD, zclient, &api);
368}
369
0cf08685 370void route_delete(struct prefix *p, vrf_id_t vrf_id, uint8_t instance)
8a71d93d
DS
371{
372 struct zapi_route api;
373
374 memset(&api, 0, sizeof(api));
0cf08685 375 api.vrf_id = vrf_id;
8a71d93d
DS
376 api.type = ZEBRA_ROUTE_SHARP;
377 api.safi = SAFI_UNICAST;
ae252c02 378 api.instance = instance;
8a71d93d
DS
379 memcpy(&api.prefix, p, sizeof(*p));
380 zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
381
382 return;
383}
384
91529dc8 385void sharp_zebra_nexthop_watch(struct prefix *p, vrf_id_t vrf_id, bool import,
b47dc61a 386 bool watch, bool connected)
0ae8130d 387{
b47dc61a 388 int command;
0ae8130d 389
b47dc61a
DS
390 if (!import) {
391 command = ZEBRA_NEXTHOP_REGISTER;
392
393 if (!watch)
394 command = ZEBRA_NEXTHOP_UNREGISTER;
395 } else {
396 command = ZEBRA_IMPORT_ROUTE_REGISTER;
397
398 if (!watch)
399 command = ZEBRA_IMPORT_ROUTE_UNREGISTER;
400 }
0ae8130d 401
91529dc8 402 if (zclient_send_rnh(zclient, command, p, connected, vrf_id) < 0)
15569c58 403 zlog_warn("%s: Failure to send nexthop to zebra", __func__);
0ae8130d
DS
404}
405
67a9eda9 406static int sharp_debug_nexthops(struct zapi_route *api)
0ae8130d 407{
0ae8130d 408 int i;
67a9eda9 409 char buf[PREFIX_STRLEN];
0ae8130d 410
67a9eda9
DS
411 for (i = 0; i < api->nexthop_num; i++) {
412 struct zapi_nexthop *znh = &api->nexthops[i];
0ae8130d
DS
413
414 switch (znh->type) {
415 case NEXTHOP_TYPE_IPV4_IFINDEX:
416 case NEXTHOP_TYPE_IPV4:
417 zlog_debug(
d6951e5e 418 " Nexthop %s, type: %d, ifindex: %d, vrf: %d, label_num: %d",
0ae8130d
DS
419 inet_ntop(AF_INET, &znh->gate.ipv4.s_addr, buf,
420 sizeof(buf)),
421 znh->type, znh->ifindex, znh->vrf_id,
422 znh->label_num);
423 break;
424 case NEXTHOP_TYPE_IPV6_IFINDEX:
425 case NEXTHOP_TYPE_IPV6:
426 zlog_debug(
d6951e5e 427 " Nexthop %s, type: %d, ifindex: %d, vrf: %d, label_num: %d",
0ae8130d
DS
428 inet_ntop(AF_INET6, &znh->gate.ipv6, buf,
429 sizeof(buf)),
430 znh->type, znh->ifindex, znh->vrf_id,
431 znh->label_num);
432 break;
433 case NEXTHOP_TYPE_IFINDEX:
d6951e5e 434 zlog_debug(" Nexthop IFINDEX: %d, ifindex: %d",
0ae8130d
DS
435 znh->type, znh->ifindex);
436 break;
437 case NEXTHOP_TYPE_BLACKHOLE:
d6951e5e 438 zlog_debug(" Nexthop blackhole");
0ae8130d
DS
439 break;
440 }
441 }
67a9eda9
DS
442
443 return i;
444}
445static int sharp_nexthop_update(ZAPI_CALLBACK_ARGS)
446{
447 struct sharp_nh_tracker *nht;
448 struct zapi_route nhr;
449
450 if (!zapi_nexthop_update_decode(zclient->ibuf, &nhr)) {
15569c58 451 zlog_warn("%s: Decode of update failed", __func__);
67a9eda9
DS
452
453 return 0;
454 }
455
456 zlog_debug("Received update for %pFX", &nhr.prefix);
457
458 nht = sharp_nh_tracker_get(&nhr.prefix);
459 nht->nhop_num = nhr.nexthop_num;
460 nht->updates++;
461
462 sharp_debug_nexthops(&nhr);
463
464 return 0;
465}
466
467static int sharp_redistribute_route(ZAPI_CALLBACK_ARGS)
468{
469 struct zapi_route api;
470
471 if (zapi_route_decode(zclient->ibuf, &api) < 0)
15569c58 472 zlog_warn("%s: Decode of redistribute failed: %d", __func__,
67a9eda9
DS
473 ZEBRA_REDISTRIBUTE_ROUTE_ADD);
474
475 zlog_debug("%s: %pFX (%s)", zserv_command_string(cmd),
476 &api.prefix, zebra_route_string(api.type));
477
478 sharp_debug_nexthops(&api);
479
0ae8130d
DS
480 return 0;
481}
482
7f5ac773
MS
483/* Handler for opaque messages */
484static int sharp_opaque_handler(ZAPI_CALLBACK_ARGS)
485{
7f5ac773 486 struct stream *s;
387831ff 487 struct zapi_opaque_msg info;
7f5ac773
MS
488
489 s = zclient->ibuf;
490
387831ff
MS
491 if (zclient_opaque_decode(s, &info) != 0)
492 return -1;
7f5ac773 493
387831ff 494 zlog_debug("%s: received opaque type %u", __func__, info.type);
7f5ac773
MS
495
496 return 0;
497}
498
2ac6c90d
MS
499/*
500 * Send OPAQUE messages, using subtype 'type'.
501 */
502void sharp_opaque_send(uint32_t type, uint32_t count)
503{
7f5ac773 504 uint8_t buf[32];
2ac6c90d
MS
505 int ret;
506 uint32_t i;
507
508 /* Prepare a small payload */
509 for (i = 0; i < sizeof(buf); i++) {
510 if (type < 255)
511 buf[i] = type;
512 else
513 buf[i] = 255;
514 }
515
516 /* Send some messages */
517 for (i = 0; i < count; i++) {
518 ret = zclient_send_opaque(zclient, type, buf, sizeof(buf));
519 if (ret < 0) {
520 zlog_debug("%s: send_opaque() failed => %d",
521 __func__, ret);
522 break;
523 }
524 }
525
526}
527
939b2339
MS
528/*
529 * Send OPAQUE registration messages, using subtype 'type'.
530 */
531void sharp_opaque_reg_send(bool is_reg, uint32_t proto, uint32_t instance,
532 uint32_t session_id, uint32_t type)
533{
534 struct stream *s;
535
536 s = zclient->obuf;
537 stream_reset(s);
538
539 if (is_reg)
540 zclient_create_header(s, ZEBRA_OPAQUE_REGISTER, VRF_DEFAULT);
541 else
542 zclient_create_header(s, ZEBRA_OPAQUE_UNREGISTER, VRF_DEFAULT);
543
544 /* Send sub-type */
545 stream_putl(s, type);
546
547 /* Add zclient info */
548 stream_putc(s, proto);
549 stream_putw(s, instance);
550 stream_putl(s, session_id);
551
552 /* Put length at the first point of the stream. */
553 stream_putw_at(s, 0, stream_get_endp(s));
554
555 (void)zclient_send_message(zclient);
556
557}
558
8a71d93d
DS
559extern struct zebra_privs_t sharp_privs;
560
561void sharp_zebra_init(void)
562{
996c9314 563 struct zclient_options opt = {.receive_notify = true};
8a71d93d 564
138c5a74
DS
565 if_zapi_callbacks(sharp_ifp_create, sharp_ifp_up,
566 sharp_ifp_down, sharp_ifp_destroy);
567
26f63a1e 568 zclient = zclient_new(master, &opt);
8a71d93d
DS
569
570 zclient_init(zclient, ZEBRA_ROUTE_SHARP, 0, &sharp_privs);
571 zclient->zebra_connected = zebra_connected;
8a71d93d
DS
572 zclient->interface_address_add = interface_address_add;
573 zclient->interface_address_delete = interface_address_delete;
28b11f81 574 zclient->route_notify_owner = route_notify_owner;
0ae8130d 575 zclient->nexthop_update = sharp_nexthop_update;
b47dc61a 576 zclient->import_check_update = sharp_nexthop_update;
67a9eda9
DS
577
578 zclient->redistribute_route_add = sharp_redistribute_route;
579 zclient->redistribute_route_del = sharp_redistribute_route;
7f5ac773 580 zclient->opaque_msg_handler = sharp_opaque_handler;
8a71d93d 581}