]> git.proxmox.com Git - mirror_frr.git/blame - sharpd/sharp_zebra.c
Merge pull request #6050 from sworleys/PBR-No-Fail-Same-VRF
[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,
92 const struct nexthop_group *nhg)
93{
94 struct zapi_labels zl = {};
95 struct zapi_nexthop *znh;
96 const struct nexthop *nh;
97 int i, ret;
98
99 zl.type = ZEBRA_LSP_SHARP;
100 zl.local_label = in_label;
101
102 if (p) {
103 SET_FLAG(zl.message, ZAPI_LABELS_FTN);
104 prefix_copy(&zl.route.prefix, p);
105 zl.route.type = type;
106 zl.route.instance = instance;
107 }
108
109 i = 0;
110 for (ALL_NEXTHOPS_PTR(nhg, nh)) {
111 znh = &zl.nexthops[i];
112
113 /* Must have labels to be useful */
114 if (nh->nh_label == NULL || nh->nh_label->num_labels == 0)
115 continue;
116
117 if (nh->type == NEXTHOP_TYPE_IFINDEX ||
118 nh->type == NEXTHOP_TYPE_BLACKHOLE)
119 /* Hmm - can't really deal with these types */
120 continue;
121
122 ret = zapi_nexthop_from_nexthop(znh, nh);
123 if (ret < 0)
124 return -1;
125
126 i++;
127 }
128
129 /* Whoops - no nexthops isn't very useful */
130 if (i == 0)
131 return -1;
132
133 zl.nexthop_num = i;
134
135 if (install_p)
136 ret = zebra_send_mpls_labels(zclient, ZEBRA_MPLS_LABELS_ADD,
137 &zl);
138 else
139 ret = zebra_send_mpls_labels(zclient, ZEBRA_MPLS_LABELS_DELETE,
140 &zl);
141
142 return ret;
143}
144
0cf08685
DS
145void sharp_install_routes_helper(struct prefix *p, vrf_id_t vrf_id,
146 uint8_t instance, struct nexthop_group *nhg,
6b98d34f
DS
147 uint32_t routes)
148{
149 uint32_t temp, i;
dbc1bf46 150 bool v4 = false;
6b98d34f
DS
151
152 zlog_debug("Inserting %u routes", routes);
153
dbc1bf46
DS
154 if (p->family == AF_INET) {
155 v4 = true;
156 temp = ntohl(p->u.prefix4.s_addr);
157 } else
158 temp = ntohl(p->u.val32[3]);
159
547dc642 160 monotime(&sg.r.t_start);
6b98d34f 161 for (i = 0; i < routes; i++) {
0cf08685 162 route_add(p, vrf_id, (uint8_t)instance, nhg);
dbc1bf46
DS
163 if (v4)
164 p->u.prefix4.s_addr = htonl(++temp);
165 else
166 p->u.val32[3] = htonl(++temp);
6b98d34f
DS
167 }
168}
169
0cf08685
DS
170void sharp_remove_routes_helper(struct prefix *p, vrf_id_t vrf_id,
171 uint8_t instance, uint32_t routes)
6b98d34f
DS
172{
173 uint32_t temp, i;
dbc1bf46 174 bool v4 = false;
6b98d34f
DS
175
176 zlog_debug("Removing %u routes", routes);
177
dbc1bf46
DS
178 if (p->family == AF_INET) {
179 v4 = true;
180 temp = ntohl(p->u.prefix4.s_addr);
181 } else
182 temp = ntohl(p->u.val32[3]);
183
547dc642 184 monotime(&sg.r.t_start);
6b98d34f 185 for (i = 0; i < routes; i++) {
0cf08685 186 route_delete(p, vrf_id, (uint8_t)instance);
dbc1bf46
DS
187 if (v4)
188 p->u.prefix4.s_addr = htonl(++temp);
189 else
190 p->u.val32[3] = htonl(++temp);
6b98d34f
DS
191 }
192}
193
b939f6ff 194static void handle_repeated(bool installed)
6b98d34f 195{
547dc642
DS
196 struct prefix p = sg.r.orig_prefix;
197 sg.r.repeat--;
6b98d34f 198
547dc642 199 if (sg.r.repeat <= 0)
6b98d34f
DS
200 return;
201
202 if (installed) {
547dc642 203 sg.r.removed_routes = 0;
0cf08685
DS
204 sharp_remove_routes_helper(&p, sg.r.vrf_id,
205 sg.r.inst, sg.r.total_routes);
6b98d34f
DS
206 }
207
f54f37c1 208 if (!installed) {
547dc642 209 sg.r.installed_routes = 0;
0cf08685
DS
210 sharp_install_routes_helper(&p, sg.r.vrf_id, sg.r.inst,
211 &sg.r.nhop_group,
547dc642 212 sg.r.total_routes);
6b98d34f
DS
213 }
214}
215
121f9dee 216static int route_notify_owner(ZAPI_CALLBACK_ARGS)
8a71d93d 217{
25c84d86 218 struct timeval r;
8a71d93d
DS
219 struct prefix p;
220 enum zapi_route_notify_owner note;
28610f7e 221 uint32_t table;
8a71d93d 222
28610f7e 223 if (!zapi_route_notify_decode(zclient->ibuf, &p, &table, &note))
8a71d93d
DS
224 return -1;
225
5e54c602
DS
226 switch (note) {
227 case ZAPI_ROUTE_INSTALLED:
547dc642
DS
228 sg.r.installed_routes++;
229 if (sg.r.total_routes == sg.r.installed_routes) {
230 monotime(&sg.r.t_end);
231 timersub(&sg.r.t_end, &sg.r.t_start, &r);
051a0be4
DL
232 zlog_debug("Installed All Items %jd.%ld",
233 (intmax_t)r.tv_sec, (long)r.tv_usec);
6b98d34f
DS
234 handle_repeated(true);
235 }
5e54c602
DS
236 break;
237 case ZAPI_ROUTE_FAIL_INSTALL:
238 zlog_debug("Failed install of route");
239 break;
240 case ZAPI_ROUTE_BETTER_ADMIN_WON:
241 zlog_debug("Better Admin Distance won over us");
242 break;
243 case ZAPI_ROUTE_REMOVED:
547dc642
DS
244 sg.r.removed_routes++;
245 if (sg.r.total_routes == sg.r.removed_routes) {
246 monotime(&sg.r.t_end);
247 timersub(&sg.r.t_end, &sg.r.t_start, &r);
051a0be4
DL
248 zlog_debug("Removed all Items %jd.%ld",
249 (intmax_t)r.tv_sec, (long)r.tv_usec);
6b98d34f
DS
250 handle_repeated(false);
251 }
5e54c602
DS
252 break;
253 case ZAPI_ROUTE_REMOVE_FAIL:
254 zlog_debug("Route removal Failure");
255 break;
256 }
8a71d93d
DS
257 return 0;
258}
259
260static void zebra_connected(struct zclient *zclient)
261{
262 zclient_send_reg_requests(zclient, VRF_DEFAULT);
67a9eda9
DS
263
264 /*
265 * Do not actually turn this on yet
266 * This is just the start of the infrastructure needed here
267 * This can be fixed at a later time.
268 *
269 * zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP,
270 * ZEBRA_ROUTE_ALL, 0, VRF_DEFAULT);
271 */
8a71d93d
DS
272}
273
7d061b3c 274void vrf_label_add(vrf_id_t vrf_id, afi_t afi, mpls_label_t label)
ab18a495 275{
7d061b3c 276 zclient_send_vrf_label(zclient, vrf_id, afi, label, ZEBRA_LSP_SHARP);
ab18a495
DS
277}
278
0cf08685
DS
279void route_add(struct prefix *p, vrf_id_t vrf_id,
280 uint8_t instance, struct nexthop_group *nhg)
8a71d93d
DS
281{
282 struct zapi_route api;
283 struct zapi_nexthop *api_nh;
694b242f
DS
284 struct nexthop *nh;
285 int i = 0;
8a71d93d
DS
286
287 memset(&api, 0, sizeof(api));
0cf08685 288 api.vrf_id = vrf_id;
8a71d93d 289 api.type = ZEBRA_ROUTE_SHARP;
ae252c02 290 api.instance = instance;
8a71d93d
DS
291 api.safi = SAFI_UNICAST;
292 memcpy(&api.prefix, p, sizeof(*p));
293
a3896844 294 SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION);
8a71d93d
DS
295 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
296
694b242f
DS
297 for (ALL_NEXTHOPS_PTR(nhg, nh)) {
298 api_nh = &api.nexthops[i];
054af19a 299
c9e5adba 300 zapi_nexthop_from_nexthop(api_nh, nh);
694b242f
DS
301 i++;
302 }
303 api.nexthop_num = i;
8a71d93d
DS
304
305 zclient_route_send(ZEBRA_ROUTE_ADD, zclient, &api);
306}
307
0cf08685 308void route_delete(struct prefix *p, vrf_id_t vrf_id, uint8_t instance)
8a71d93d
DS
309{
310 struct zapi_route api;
311
312 memset(&api, 0, sizeof(api));
0cf08685 313 api.vrf_id = vrf_id;
8a71d93d
DS
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
91529dc8 323void sharp_zebra_nexthop_watch(struct prefix *p, vrf_id_t vrf_id, bool import,
b47dc61a 324 bool watch, bool connected)
0ae8130d 325{
b47dc61a 326 int command;
0ae8130d 327
b47dc61a
DS
328 if (!import) {
329 command = ZEBRA_NEXTHOP_REGISTER;
330
331 if (!watch)
332 command = ZEBRA_NEXTHOP_UNREGISTER;
333 } else {
334 command = ZEBRA_IMPORT_ROUTE_REGISTER;
335
336 if (!watch)
337 command = ZEBRA_IMPORT_ROUTE_UNREGISTER;
338 }
0ae8130d 339
91529dc8 340 if (zclient_send_rnh(zclient, command, p, connected, vrf_id) < 0)
15569c58 341 zlog_warn("%s: Failure to send nexthop to zebra", __func__);
0ae8130d
DS
342}
343
67a9eda9 344static int sharp_debug_nexthops(struct zapi_route *api)
0ae8130d 345{
0ae8130d 346 int i;
67a9eda9 347 char buf[PREFIX_STRLEN];
0ae8130d 348
67a9eda9
DS
349 for (i = 0; i < api->nexthop_num; i++) {
350 struct zapi_nexthop *znh = &api->nexthops[i];
0ae8130d
DS
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 }
67a9eda9
DS
380
381 return i;
382}
383static int sharp_nexthop_update(ZAPI_CALLBACK_ARGS)
384{
385 struct sharp_nh_tracker *nht;
386 struct zapi_route nhr;
387
388 if (!zapi_nexthop_update_decode(zclient->ibuf, &nhr)) {
15569c58 389 zlog_warn("%s: Decode of update failed", __func__);
67a9eda9
DS
390
391 return 0;
392 }
393
394 zlog_debug("Received update for %pFX", &nhr.prefix);
395
396 nht = sharp_nh_tracker_get(&nhr.prefix);
397 nht->nhop_num = nhr.nexthop_num;
398 nht->updates++;
399
400 sharp_debug_nexthops(&nhr);
401
402 return 0;
403}
404
405static int sharp_redistribute_route(ZAPI_CALLBACK_ARGS)
406{
407 struct zapi_route api;
408
409 if (zapi_route_decode(zclient->ibuf, &api) < 0)
15569c58 410 zlog_warn("%s: Decode of redistribute failed: %d", __func__,
67a9eda9
DS
411 ZEBRA_REDISTRIBUTE_ROUTE_ADD);
412
413 zlog_debug("%s: %pFX (%s)", zserv_command_string(cmd),
414 &api.prefix, zebra_route_string(api.type));
415
416 sharp_debug_nexthops(&api);
417
0ae8130d
DS
418 return 0;
419}
420
8a71d93d
DS
421extern struct zebra_privs_t sharp_privs;
422
423void sharp_zebra_init(void)
424{
996c9314 425 struct zclient_options opt = {.receive_notify = true};
8a71d93d 426
138c5a74
DS
427 if_zapi_callbacks(sharp_ifp_create, sharp_ifp_up,
428 sharp_ifp_down, sharp_ifp_destroy);
429
26f63a1e 430 zclient = zclient_new(master, &opt);
8a71d93d
DS
431
432 zclient_init(zclient, ZEBRA_ROUTE_SHARP, 0, &sharp_privs);
433 zclient->zebra_connected = zebra_connected;
8a71d93d
DS
434 zclient->interface_address_add = interface_address_add;
435 zclient->interface_address_delete = interface_address_delete;
28b11f81 436 zclient->route_notify_owner = route_notify_owner;
0ae8130d 437 zclient->nexthop_update = sharp_nexthop_update;
b47dc61a 438 zclient->import_check_update = sharp_nexthop_update;
67a9eda9
DS
439
440 zclient->redistribute_route_add = sharp_redistribute_route;
441 zclient->redistribute_route_del = sharp_redistribute_route;
8a71d93d 442}