]> git.proxmox.com Git - mirror_frr.git/blob - staticd/static_zebra.c
*: convert zclient callbacks to table
[mirror_frr.git] / staticd / static_zebra.c
1 /*
2 * Zebra connect code.
3 * Copyright (C) 2018 Cumulus Networks, Inc.
4 * Donald Sharp
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 #include <zebra.h>
21
22 #include "thread.h"
23 #include "command.h"
24 #include "network.h"
25 #include "prefix.h"
26 #include "routemap.h"
27 #include "table.h"
28 #include "srcdest_table.h"
29 #include "stream.h"
30 #include "memory.h"
31 #include "zclient.h"
32 #include "filter.h"
33 #include "plist.h"
34 #include "log.h"
35 #include "nexthop.h"
36 #include "nexthop_group.h"
37 #include "hash.h"
38 #include "jhash.h"
39
40 #include "static_vrf.h"
41 #include "static_routes.h"
42 #include "static_zebra.h"
43 #include "static_nht.h"
44 #include "static_vty.h"
45 #include "static_debug.h"
46
47 /* Zebra structure to hold current status. */
48 struct zclient *zclient;
49 static struct hash *static_nht_hash;
50
51 /* Inteface addition message from zebra. */
52 static int static_ifp_create(struct interface *ifp)
53 {
54 static_ifindex_update(ifp, true);
55
56 return 0;
57 }
58
59 static int static_ifp_destroy(struct interface *ifp)
60 {
61 static_ifindex_update(ifp, false);
62 return 0;
63 }
64
65 static int interface_address_add(ZAPI_CALLBACK_ARGS)
66 {
67 zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
68
69 return 0;
70 }
71
72 static int interface_address_delete(ZAPI_CALLBACK_ARGS)
73 {
74 struct connected *c;
75
76 c = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
77
78 if (!c)
79 return 0;
80
81 connected_free(&c);
82 return 0;
83 }
84
85 static int static_ifp_up(struct interface *ifp)
86 {
87 if (if_is_vrf(ifp)) {
88 struct static_vrf *svrf = static_vrf_lookup_by_id(ifp->vrf_id);
89
90 static_fixup_vrf_ids(svrf);
91 }
92
93 /* Install any static reliant on this interface coming up */
94 static_install_intf_nh(ifp);
95 static_ifindex_update(ifp, true);
96
97 return 0;
98 }
99
100 static int static_ifp_down(struct interface *ifp)
101 {
102 static_ifindex_update(ifp, false);
103
104 return 0;
105 }
106
107 static int route_notify_owner(ZAPI_CALLBACK_ARGS)
108 {
109 struct prefix p;
110 enum zapi_route_notify_owner note;
111 uint32_t table_id;
112
113 if (!zapi_route_notify_decode(zclient->ibuf, &p, &table_id, &note,
114 NULL, NULL))
115 return -1;
116
117 switch (note) {
118 case ZAPI_ROUTE_FAIL_INSTALL:
119 static_nht_mark_state(&p, vrf_id, STATIC_NOT_INSTALLED);
120 zlog_warn("%s: Route %pFX failed to install for table: %u",
121 __func__, &p, table_id);
122 break;
123 case ZAPI_ROUTE_BETTER_ADMIN_WON:
124 static_nht_mark_state(&p, vrf_id, STATIC_NOT_INSTALLED);
125 zlog_warn(
126 "%s: Route %pFX over-ridden by better route for table: %u",
127 __func__, &p, table_id);
128 break;
129 case ZAPI_ROUTE_INSTALLED:
130 static_nht_mark_state(&p, vrf_id, STATIC_INSTALLED);
131 break;
132 case ZAPI_ROUTE_REMOVED:
133 static_nht_mark_state(&p, vrf_id, STATIC_NOT_INSTALLED);
134 break;
135 case ZAPI_ROUTE_REMOVE_FAIL:
136 static_nht_mark_state(&p, vrf_id, STATIC_INSTALLED);
137 zlog_warn("%s: Route %pFX failure to remove for table: %u",
138 __func__, &p, table_id);
139 break;
140 }
141
142 return 0;
143 }
144 static void zebra_connected(struct zclient *zclient)
145 {
146 zclient_send_reg_requests(zclient, VRF_DEFAULT);
147 }
148
149 struct static_nht_data {
150 struct prefix *nh;
151
152 vrf_id_t nh_vrf_id;
153
154 uint32_t refcount;
155 uint8_t nh_num;
156 };
157
158 /* API to check whether the configured nexthop address is
159 * one of its local connected address or not.
160 */
161 static bool
162 static_nexthop_is_local(vrf_id_t vrfid, struct prefix *addr, int family)
163 {
164 if (family == AF_INET) {
165 if (if_lookup_exact_address(&addr->u.prefix4,
166 AF_INET,
167 vrfid))
168 return true;
169 } else if (family == AF_INET6) {
170 if (if_lookup_exact_address(&addr->u.prefix6,
171 AF_INET6,
172 vrfid))
173 return true;
174 }
175 return false;
176 }
177 static int static_zebra_nexthop_update(ZAPI_CALLBACK_ARGS)
178 {
179 struct static_nht_data *nhtd, lookup;
180 struct zapi_route nhr;
181 afi_t afi = AFI_IP;
182
183 if (!zapi_nexthop_update_decode(zclient->ibuf, &nhr)) {
184 zlog_err("Failure to decode nexthop update message");
185 return 1;
186 }
187
188 if (nhr.prefix.family == AF_INET6)
189 afi = AFI_IP6;
190
191 if (nhr.type == ZEBRA_ROUTE_CONNECT) {
192 if (static_nexthop_is_local(vrf_id, &nhr.prefix,
193 nhr.prefix.family))
194 nhr.nexthop_num = 0;
195 }
196
197 memset(&lookup, 0, sizeof(lookup));
198 lookup.nh = &nhr.prefix;
199 lookup.nh_vrf_id = vrf_id;
200
201 nhtd = hash_lookup(static_nht_hash, &lookup);
202
203 if (nhtd) {
204 nhtd->nh_num = nhr.nexthop_num;
205
206 static_nht_reset_start(&nhr.prefix, afi, nhtd->nh_vrf_id);
207 static_nht_update(NULL, &nhr.prefix, nhr.nexthop_num, afi,
208 nhtd->nh_vrf_id);
209 } else
210 zlog_err("No nhtd?");
211
212 return 1;
213 }
214
215 static void static_zebra_capabilities(struct zclient_capabilities *cap)
216 {
217 mpls_enabled = cap->mpls_enabled;
218 }
219
220 static unsigned int static_nht_hash_key(const void *data)
221 {
222 const struct static_nht_data *nhtd = data;
223 unsigned int key = 0;
224
225 key = prefix_hash_key(nhtd->nh);
226 return jhash_1word(nhtd->nh_vrf_id, key);
227 }
228
229 static bool static_nht_hash_cmp(const void *d1, const void *d2)
230 {
231 const struct static_nht_data *nhtd1 = d1;
232 const struct static_nht_data *nhtd2 = d2;
233
234 if (nhtd1->nh_vrf_id != nhtd2->nh_vrf_id)
235 return false;
236
237 return prefix_same(nhtd1->nh, nhtd2->nh);
238 }
239
240 static void *static_nht_hash_alloc(void *data)
241 {
242 struct static_nht_data *copy = data;
243 struct static_nht_data *new;
244
245 new = XMALLOC(MTYPE_TMP, sizeof(*new));
246
247 new->nh = prefix_new();
248 prefix_copy(new->nh, copy->nh);
249 new->refcount = 0;
250 new->nh_num = 0;
251 new->nh_vrf_id = copy->nh_vrf_id;
252
253 return new;
254 }
255
256 static void static_nht_hash_free(void *data)
257 {
258 struct static_nht_data *nhtd = data;
259
260 prefix_free(&nhtd->nh);
261 XFREE(MTYPE_TMP, nhtd);
262 }
263
264 void static_zebra_nht_register(struct static_nexthop *nh, bool reg)
265 {
266 struct static_path *pn = nh->pn;
267 struct route_node *rn = pn->rn;
268 struct static_nht_data *nhtd, lookup;
269 uint32_t cmd;
270 struct prefix p;
271 afi_t afi = AFI_IP;
272
273 cmd = (reg) ?
274 ZEBRA_NEXTHOP_REGISTER : ZEBRA_NEXTHOP_UNREGISTER;
275
276 if (nh->nh_registered && reg)
277 return;
278
279 if (!nh->nh_registered && !reg)
280 return;
281
282 memset(&p, 0, sizeof(p));
283 switch (nh->type) {
284 case STATIC_IFNAME:
285 case STATIC_BLACKHOLE:
286 return;
287 case STATIC_IPV4_GATEWAY:
288 case STATIC_IPV4_GATEWAY_IFNAME:
289 p.family = AF_INET;
290 p.prefixlen = IPV4_MAX_BITLEN;
291 p.u.prefix4 = nh->addr.ipv4;
292 afi = AFI_IP;
293 break;
294 case STATIC_IPV6_GATEWAY:
295 case STATIC_IPV6_GATEWAY_IFNAME:
296 p.family = AF_INET6;
297 p.prefixlen = IPV6_MAX_BITLEN;
298 p.u.prefix6 = nh->addr.ipv6;
299 afi = AFI_IP6;
300 break;
301 }
302
303 memset(&lookup, 0, sizeof(lookup));
304 lookup.nh = &p;
305 lookup.nh_vrf_id = nh->nh_vrf_id;
306
307 nh->nh_registered = reg;
308
309 if (reg) {
310 nhtd = hash_get(static_nht_hash, &lookup,
311 static_nht_hash_alloc);
312 nhtd->refcount++;
313
314 DEBUGD(&static_dbg_route,
315 "Registered nexthop(%pFX) for %pRN %d", &p, rn,
316 nhtd->nh_num);
317 if (nhtd->refcount > 1 && nhtd->nh_num) {
318 static_nht_update(&rn->p, nhtd->nh, nhtd->nh_num, afi,
319 nh->nh_vrf_id);
320 return;
321 }
322 } else {
323 nhtd = hash_lookup(static_nht_hash, &lookup);
324 if (!nhtd)
325 return;
326
327 nhtd->refcount--;
328 if (nhtd->refcount >= 1)
329 return;
330
331 hash_release(static_nht_hash, nhtd);
332 static_nht_hash_free(nhtd);
333 }
334
335 if (zclient_send_rnh(zclient, cmd, &p, false, false, nh->nh_vrf_id)
336 == ZCLIENT_SEND_FAILURE)
337 zlog_warn("%s: Failure to send nexthop to zebra", __func__);
338 }
339 /*
340 * When nexthop gets updated via configuration then use the
341 * already registered NH and resend the route to zebra
342 */
343 int static_zebra_nh_update(struct static_nexthop *nh)
344 {
345 struct static_path *pn = nh->pn;
346 struct route_node *rn = pn->rn;
347 struct static_nht_data *nhtd, lookup = {};
348 struct prefix p = {};
349 afi_t afi = AFI_IP;
350
351 if (!nh->nh_registered)
352 return 0;
353
354 switch (nh->type) {
355 case STATIC_IFNAME:
356 case STATIC_BLACKHOLE:
357 return 0;
358 case STATIC_IPV4_GATEWAY:
359 case STATIC_IPV4_GATEWAY_IFNAME:
360 p.family = AF_INET;
361 p.prefixlen = IPV4_MAX_BITLEN;
362 p.u.prefix4 = nh->addr.ipv4;
363 afi = AFI_IP;
364 break;
365 case STATIC_IPV6_GATEWAY:
366 case STATIC_IPV6_GATEWAY_IFNAME:
367 p.family = AF_INET6;
368 p.prefixlen = IPV6_MAX_BITLEN;
369 p.u.prefix6 = nh->addr.ipv6;
370 afi = AFI_IP6;
371 break;
372 }
373
374 lookup.nh = &p;
375 lookup.nh_vrf_id = nh->nh_vrf_id;
376
377 nhtd = hash_lookup(static_nht_hash, &lookup);
378 if (nhtd && nhtd->nh_num) {
379 nh->state = STATIC_START;
380 static_nht_update(&rn->p, nhtd->nh, nhtd->nh_num, afi,
381 nh->nh_vrf_id);
382 return 1;
383 }
384 return 0;
385 }
386
387 extern void static_zebra_route_add(struct static_path *pn, bool install)
388 {
389 struct route_node *rn = pn->rn;
390 struct static_route_info *si = rn->info;
391 struct static_nexthop *nh;
392 const struct prefix *p, *src_pp;
393 struct zapi_nexthop *api_nh;
394 struct zapi_route api;
395 uint32_t nh_num = 0;
396
397 p = src_pp = NULL;
398 srcdest_rnode_prefixes(rn, &p, &src_pp);
399
400 memset(&api, 0, sizeof(api));
401 api.vrf_id = si->svrf->vrf->vrf_id;
402 api.type = ZEBRA_ROUTE_STATIC;
403 api.safi = si->safi;
404 memcpy(&api.prefix, p, sizeof(api.prefix));
405
406 if (src_pp) {
407 SET_FLAG(api.message, ZAPI_MESSAGE_SRCPFX);
408 memcpy(&api.src_prefix, src_pp, sizeof(api.src_prefix));
409 }
410 SET_FLAG(api.flags, ZEBRA_FLAG_RR_USE_DISTANCE);
411 SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION);
412 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
413 if (pn->distance) {
414 SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
415 api.distance = pn->distance;
416 }
417 if (pn->tag) {
418 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
419 api.tag = pn->tag;
420 }
421 if (pn->table_id != 0) {
422 SET_FLAG(api.message, ZAPI_MESSAGE_TABLEID);
423 api.tableid = pn->table_id;
424 }
425 frr_each(static_nexthop_list, &pn->nexthop_list, nh) {
426 api_nh = &api.nexthops[nh_num];
427 if (nh->nh_vrf_id == VRF_UNKNOWN)
428 continue;
429
430 api_nh->vrf_id = nh->nh_vrf_id;
431 if (nh->onlink)
432 SET_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_ONLINK);
433 if (nh->color != 0) {
434 SET_FLAG(api.message, ZAPI_MESSAGE_SRTE);
435 api_nh->srte_color = nh->color;
436 }
437
438 nh->state = STATIC_SENT_TO_ZEBRA;
439
440 switch (nh->type) {
441 case STATIC_IFNAME:
442 if (nh->ifindex == IFINDEX_INTERNAL)
443 continue;
444 api_nh->ifindex = nh->ifindex;
445 api_nh->type = NEXTHOP_TYPE_IFINDEX;
446 break;
447 case STATIC_IPV4_GATEWAY:
448 if (!nh->nh_valid)
449 continue;
450 api_nh->type = NEXTHOP_TYPE_IPV4;
451 api_nh->gate = nh->addr;
452 break;
453 case STATIC_IPV4_GATEWAY_IFNAME:
454 if (nh->ifindex == IFINDEX_INTERNAL)
455 continue;
456 api_nh->ifindex = nh->ifindex;
457 api_nh->type = NEXTHOP_TYPE_IPV4_IFINDEX;
458 api_nh->gate = nh->addr;
459 break;
460 case STATIC_IPV6_GATEWAY:
461 if (!nh->nh_valid)
462 continue;
463 api_nh->type = NEXTHOP_TYPE_IPV6;
464 api_nh->gate = nh->addr;
465 break;
466 case STATIC_IPV6_GATEWAY_IFNAME:
467 if (nh->ifindex == IFINDEX_INTERNAL)
468 continue;
469 api_nh->type = NEXTHOP_TYPE_IPV6_IFINDEX;
470 api_nh->ifindex = nh->ifindex;
471 api_nh->gate = nh->addr;
472 break;
473 case STATIC_BLACKHOLE:
474 api_nh->type = NEXTHOP_TYPE_BLACKHOLE;
475 switch (nh->bh_type) {
476 case STATIC_BLACKHOLE_DROP:
477 case STATIC_BLACKHOLE_NULL:
478 api_nh->bh_type = BLACKHOLE_NULL;
479 break;
480 case STATIC_BLACKHOLE_REJECT:
481 api_nh->bh_type = BLACKHOLE_REJECT;
482 }
483 break;
484 }
485
486 if (nh->snh_label.num_labels) {
487 int i;
488
489 SET_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_LABEL);
490 api_nh->label_num = nh->snh_label.num_labels;
491 for (i = 0; i < api_nh->label_num; i++)
492 api_nh->labels[i] = nh->snh_label.label[i];
493 }
494 nh_num++;
495 }
496
497 api.nexthop_num = nh_num;
498
499 /*
500 * If we have been given an install but nothing is valid
501 * go ahead and delete the route for double plus fun
502 */
503 if (!nh_num && install)
504 install = false;
505
506 zclient_route_send(install ?
507 ZEBRA_ROUTE_ADD : ZEBRA_ROUTE_DELETE,
508 zclient, &api);
509 }
510
511 static zclient_handler *const static_handlers[] = {
512 [ZEBRA_INTERFACE_ADDRESS_ADD] = interface_address_add,
513 [ZEBRA_INTERFACE_ADDRESS_DELETE] = interface_address_delete,
514 [ZEBRA_ROUTE_NOTIFY_OWNER] = route_notify_owner,
515 [ZEBRA_NEXTHOP_UPDATE] = static_zebra_nexthop_update,
516 };
517
518 void static_zebra_init(void)
519 {
520 struct zclient_options opt = { .receive_notify = true };
521
522 if_zapi_callbacks(static_ifp_create, static_ifp_up,
523 static_ifp_down, static_ifp_destroy);
524
525 zclient = zclient_new(master, &opt, static_handlers,
526 array_size(static_handlers));
527
528 zclient_init(zclient, ZEBRA_ROUTE_STATIC, 0, &static_privs);
529 zclient->zebra_capabilities = static_zebra_capabilities;
530 zclient->zebra_connected = zebra_connected;
531
532 static_nht_hash = hash_create(static_nht_hash_key,
533 static_nht_hash_cmp,
534 "Static Nexthop Tracking hash");
535 }
536
537 /* static_zebra_stop used by tests/lib/test_grpc.cpp */
538 void static_zebra_stop(void)
539 {
540 if (!zclient)
541 return;
542 zclient_stop(zclient);
543 zclient_free(zclient);
544 zclient = NULL;
545 }
546
547 void static_zebra_vrf_register(struct vrf *vrf)
548 {
549 if (vrf->vrf_id == VRF_DEFAULT)
550 return;
551 zclient_send_reg_requests(zclient, vrf->vrf_id);
552 }
553
554 void static_zebra_vrf_unregister(struct vrf *vrf)
555 {
556 if (vrf->vrf_id == VRF_DEFAULT)
557 return;
558 zclient_send_dereg_requests(zclient, vrf->vrf_id);
559 }