]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/rfapi/vnc_zebra.c
bgpd: Rework code to use `const struct prefix`
[mirror_frr.git] / bgpd / rfapi / vnc_zebra.c
CommitLineData
d62a17ae 1/*
65efcfce
LB
2 *
3 * Copyright 2009-2016, LabN Consulting, L.L.C.
4 *
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
896014f4
DL
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
65efcfce
LB
19 */
20
21/*
22 * File: vnc_zebra.c
23 * Purpose: Handle exchange of routes between VNC and Zebra
24 */
25
f8b6f499
LB
26#include "lib/zebra.h"
27#include "lib/prefix.h"
fe08ba7e 28#include "lib/agg_table.h"
f8b6f499
LB
29#include "lib/log.h"
30#include "lib/command.h"
31#include "lib/zclient.h"
32#include "lib/stream.h"
74ffbfe6 33#include "lib/ringbuf.h"
f8b6f499 34#include "lib/memory.h"
02705213 35#include "lib/lib_errors.h"
65efcfce 36
f8b6f499
LB
37#include "bgpd/bgpd.h"
38#include "bgpd/bgp_ecommunity.h"
39#include "bgpd/bgp_route.h"
40#include "bgpd/bgp_debug.h"
41#include "bgpd/bgp_advertise.h"
65efcfce 42
f8b6f499
LB
43#include "bgpd/rfapi/bgp_rfapi_cfg.h"
44#include "bgpd/rfapi/rfapi.h"
45#include "bgpd/rfapi/rfapi_import.h"
46#include "bgpd/rfapi/rfapi_private.h"
47#include "bgpd/rfapi/vnc_zebra.h"
48#include "bgpd/rfapi/rfapi_vty.h"
49#include "bgpd/rfapi/rfapi_backend.h"
a3b55c25 50#include "bgpd/rfapi/vnc_debug.h"
65efcfce 51
d62a17ae 52static struct rfapi_descriptor vncHD1VR; /* Single-VR export dummy nve descr */
65efcfce
LB
53static struct zclient *zclient_vnc = NULL;
54
55/***********************************************************************
56 * REDISTRIBUTE: Zebra sends updates/withdraws to BGPD
57 ***********************************************************************/
58
59/*
60 * Routes coming from zebra get added to VNC here
61 */
d7c0a89a 62static void vnc_redistribute_add(struct prefix *p, uint32_t metric,
74489921 63 uint8_t type)
65efcfce 64{
d62a17ae 65 struct bgp *bgp = bgp_get_default();
66 struct prefix_rd prd;
67 struct rfapi_ip_addr vnaddr;
68 afi_t afi;
69 uint32_t local_pref =
70 rfp_cost_to_localpref(metric > 255 ? 255 : metric);
71
72 if (!bgp)
73 return;
74
75 if (!bgp->rfapi_cfg) {
76 vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
77 __func__);
78 return;
79 }
80
81 afi = family2afi(p->family);
82 if (!afi) {
83 vnc_zlog_debug_verbose("%s: unknown prefix address family %d",
84 __func__, p->family);
85 return;
86 }
87
88 if (!bgp->rfapi_cfg->redist[afi][type]) {
89 vnc_zlog_debug_verbose(
90 "%s: bgp->rfapi_cfg->redist[afi=%d][type=%d] is 0, skipping",
91 __func__, afi, type);
92 return;
93 }
94 if (!bgp->rfapi_cfg->rfg_redist) {
95 vnc_zlog_debug_verbose("%s: no redist nve group, skipping",
96 __func__);
97 return;
98 }
99
100 /*
101 * Assume nve group's configured VN address prefix is a host
102 * route which also happens to give the NVE VN address to use
103 * for redistributing into VNC.
104 */
105 vnaddr.addr_family = bgp->rfapi_cfg->rfg_redist->vn_prefix.family;
106 switch (bgp->rfapi_cfg->rfg_redist->vn_prefix.family) {
107 case AF_INET:
108 if (bgp->rfapi_cfg->rfg_redist->vn_prefix.prefixlen != 32) {
109 vnc_zlog_debug_verbose(
110 "%s: redist nve group VN prefix len (%d) != 32, skipping",
111 __func__,
112 bgp->rfapi_cfg->rfg_redist->vn_prefix
113 .prefixlen);
114 return;
115 }
116 vnaddr.addr.v4 =
117 bgp->rfapi_cfg->rfg_redist->vn_prefix.u.prefix4;
118 break;
119 case AF_INET6:
120 if (bgp->rfapi_cfg->rfg_redist->vn_prefix.prefixlen != 128) {
121 vnc_zlog_debug_verbose(
122 "%s: redist nve group VN prefix len (%d) != 128, skipping",
123 __func__,
124 bgp->rfapi_cfg->rfg_redist->vn_prefix
125 .prefixlen);
126 return;
127 }
128 vnaddr.addr.v6 =
129 bgp->rfapi_cfg->rfg_redist->vn_prefix.u.prefix6;
130 break;
131 default:
132 vnc_zlog_debug_verbose(
133 "%s: no redist nve group VN host prefix configured, skipping",
134 __func__);
135 return;
136 }
137
138 /*
139 * Assume nve group's configured UN address prefix is a host
140 * route which also happens to give the NVE UN address to use
141 * for redistributing into VNC.
142 */
143
144 /*
145 * Set UN address in dummy nve descriptor so add_vnc_route
146 * can use it in VNC tunnel SubTLV
147 */
148 {
149 struct rfapi_ip_prefix pfx_un;
150
151 rfapiQprefix2Rprefix(&bgp->rfapi_cfg->rfg_redist->un_prefix,
152 &pfx_un);
153
154 switch (pfx_un.prefix.addr_family) {
155 case AF_INET:
156 if (pfx_un.length != 32) {
157 vnc_zlog_debug_verbose(
158 "%s: redist nve group UN prefix len (%d) != 32, skipping",
159 __func__, pfx_un.length);
160 return;
161 }
162 break;
163 case AF_INET6:
164 if (pfx_un.length != 128) {
165 vnc_zlog_debug_verbose(
166 "%s: redist nve group UN prefix len (%d) != 128, skipping",
167 __func__, pfx_un.length);
168 return;
169 }
170 break;
171 default:
172 vnc_zlog_debug_verbose(
173 "%s: no redist nve group UN host prefix configured, skipping",
174 __func__);
175 return;
176 }
177
178 vncHD1VR.un_addr = pfx_un.prefix;
179
180 if (!vncHD1VR.peer) {
181 /*
182 * Same setup as in rfapi_open()
183 */
184 vncHD1VR.peer = peer_new(bgp);
185 vncHD1VR.peer->status =
186 Established; /* keep bgp core happy */
187 bgp_sync_delete(vncHD1VR.peer); /* don't need these */
424ab01d 188
becedef6
QY
189 /*
190 * since this peer is not on the I/O thread, this lock
191 * is not strictly necessary, but serves as a reminder
192 * to those who may meddle...
193 */
00dffa8c 194 frr_with_mutex(&vncHD1VR.peer->io_mtx) {
424ab01d
QY
195 // we don't need any I/O related facilities
196 if (vncHD1VR.peer->ibuf)
197 stream_fifo_free(vncHD1VR.peer->ibuf);
198 if (vncHD1VR.peer->obuf)
199 stream_fifo_free(vncHD1VR.peer->obuf);
200
201 if (vncHD1VR.peer->ibuf_work)
74ffbfe6 202 ringbuf_del(vncHD1VR.peer->ibuf_work);
424ab01d
QY
203 if (vncHD1VR.peer->obuf_work)
204 stream_free(vncHD1VR.peer->obuf_work);
205
d62a17ae 206 vncHD1VR.peer->ibuf = NULL;
d62a17ae 207 vncHD1VR.peer->obuf = NULL;
424ab01d
QY
208 vncHD1VR.peer->obuf_work = NULL;
209 vncHD1VR.peer->ibuf_work = NULL;
d62a17ae 210 }
424ab01d 211
d62a17ae 212 /* base code assumes have valid host pointer */
213 vncHD1VR.peer->host =
214 XSTRDUP(MTYPE_BGP_PEER_HOST, ".zebra.");
215
216 /* Mark peer as belonging to HD */
217 SET_FLAG(vncHD1VR.peer->flags, PEER_FLAG_IS_RFAPI_HD);
218 }
219 }
220
221 memset(&prd, 0, sizeof(prd));
222 prd = bgp->rfapi_cfg->rfg_redist->rd;
223 prd.family = AF_UNSPEC;
224 prd.prefixlen = 64;
225
226 add_vnc_route(&vncHD1VR, /* cookie + UN addr */
227 bgp, SAFI_MPLS_VPN, p, &prd, &vnaddr, &local_pref,
228 &(bgp->rfapi_cfg->redist_lifetime),
229 NULL, /* RFP options */
230 NULL, /* struct rfapi_un_option */
231 NULL, /* struct rfapi_vn_option */
232 bgp->rfapi_cfg->rfg_redist->rt_export_list, NULL,
233 NULL, /* label: default */
234 type, BGP_ROUTE_REDISTRIBUTE, 0); /* flags */
65efcfce
LB
235}
236
237/*
238 * Route deletions from zebra propagate to VNC here
239 */
d62a17ae 240static void vnc_redistribute_delete(struct prefix *p, uint8_t type)
65efcfce 241{
d62a17ae 242 struct bgp *bgp = bgp_get_default();
243 struct prefix_rd prd;
244 afi_t afi;
245
246 if (!bgp)
247 return;
248
249 if (!bgp->rfapi_cfg) {
250 vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
251 __func__);
252 return;
253 }
254 afi = family2afi(p->family);
255 if (!afi) {
256 vnc_zlog_debug_verbose("%s: unknown prefix address family %d",
257 __func__, p->family);
258 return;
259 }
260 if (!bgp->rfapi_cfg->redist[afi][type]) {
261 vnc_zlog_debug_verbose(
262 "%s: bgp->rfapi_cfg->redist[afi=%d][type=%d] is 0, skipping",
263 __func__, afi, type);
264 return;
265 }
266 if (!bgp->rfapi_cfg->rfg_redist) {
267 vnc_zlog_debug_verbose("%s: no redist nve group, skipping",
268 __func__);
269 return;
270 }
271
272 memset(&prd, 0, sizeof(prd));
273 prd = bgp->rfapi_cfg->rfg_redist->rd;
274 prd.family = AF_UNSPEC;
275 prd.prefixlen = 64;
276
277 del_vnc_route(&vncHD1VR, /* use dummy ptr as cookie */
278 vncHD1VR.peer, bgp, SAFI_MPLS_VPN, p, &prd, type,
279 BGP_ROUTE_REDISTRIBUTE, NULL, 0);
65efcfce
LB
280}
281
282/*
283 * Flush all redistributed routes of type <type>
284 */
d62a17ae 285static void vnc_redistribute_withdraw(struct bgp *bgp, afi_t afi, uint8_t type)
65efcfce 286{
d62a17ae 287 struct prefix_rd prd;
288 struct bgp_table *table;
289 struct bgp_node *prn;
290 struct bgp_node *rn;
291
292 vnc_zlog_debug_verbose("%s: entry", __func__);
293
294 if (!bgp)
295 return;
296 if (!bgp->rfapi_cfg) {
297 vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
298 __func__);
299 return;
300 }
301
302 /*
303 * Loop over all the RDs
304 */
305 for (prn = bgp_table_top(bgp->rib[afi][SAFI_MPLS_VPN]); prn;
306 prn = bgp_route_next(prn)) {
307 memset(&prd, 0, sizeof(prd));
308 prd.family = AF_UNSPEC;
309 prd.prefixlen = 64;
310 memcpy(prd.val, prn->p.u.val, 8);
311
312 /* This is the per-RD table of prefixes */
67009e22 313 table = bgp_node_get_bgp_table_info(prn);
2eab1324
RW
314 if (!table)
315 continue;
d62a17ae 316
317 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
318
4b7e6066 319 struct bgp_path_info *ri;
d62a17ae 320
6f94b685
DS
321 for (ri = bgp_node_get_bgp_path_info(rn); ri;
322 ri = ri->next) {
d62a17ae 323 if (ri->type
324 == type) { /* has matching redist type */
325 break;
326 }
327 }
328 if (ri) {
329 del_vnc_route(
330 &vncHD1VR, /* use dummy ptr as cookie */
331 vncHD1VR.peer, bgp, SAFI_MPLS_VPN,
332 &(rn->p), &prd, type,
333 BGP_ROUTE_REDISTRIBUTE, NULL, 0);
334 }
335 }
336 }
337 vnc_zlog_debug_verbose("%s: return", __func__);
65efcfce
LB
338}
339
340/*
341 * Zebra route add and delete treatment.
342 *
343 * Assumes 1 nexthop
344 */
121f9dee 345static int vnc_zebra_read_route(ZAPI_CALLBACK_ARGS)
65efcfce 346{
74489921
RW
347 struct zapi_route api;
348 int add;
d62a17ae 349
74489921
RW
350 if (zapi_route_decode(zclient->ibuf, &api) < 0)
351 return -1;
65efcfce 352
74489921
RW
353 /* we completely ignore srcdest routes for now. */
354 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
d62a17ae 355 return 0;
356
121f9dee 357 add = (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD);
74489921
RW
358 if (add)
359 vnc_redistribute_add(&api.prefix, api.metric, api.type);
d62a17ae 360 else
74489921 361 vnc_redistribute_delete(&api.prefix, api.type);
d62a17ae 362
74489921
RW
363 if (BGP_DEBUG(zebra, ZEBRA)) {
364 char buf[PREFIX_STRLEN];
d62a17ae 365
74489921
RW
366 prefix2str(&api.prefix, buf, sizeof(buf));
367 vnc_zlog_debug_verbose(
368 "%s: Zebra rcvd: route delete %s %s metric %u",
369 __func__, zebra_route_string(api.type), buf,
370 api.metric);
d62a17ae 371 }
372
373 return 0;
65efcfce
LB
374}
375
376/***********************************************************************
377 * vnc_bgp_zebra_*: VNC sends updates/withdraws to Zebra
378 ***********************************************************************/
379
380/*
381 * low-level message builder
382 */
a74e593b
RW
383static void vnc_zebra_route_msg(struct prefix *p, unsigned int nhp_count,
384 void *nhp_ary, int add) /* 1 = add, 0 = del */
65efcfce 385{
2ad4f093
RW
386 struct zapi_route api;
387 struct zapi_nexthop *api_nh;
388 int i;
b40c5062
LB
389 struct in_addr **nhp_ary4 = nhp_ary;
390 struct in6_addr **nhp_ary6 = nhp_ary;
2ad4f093 391
d62a17ae 392 if (!nhp_count) {
393 vnc_zlog_debug_verbose("%s: empty nexthop list, skipping",
394 __func__);
395 return;
396 }
397
2ad4f093
RW
398 memset(&api, 0, sizeof(api));
399 api.vrf_id = VRF_DEFAULT;
400 api.type = ZEBRA_ROUTE_VNC;
401 api.safi = SAFI_UNICAST;
402 api.prefix = *p;
403
404 /* Nexthops */
405 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
a74e593b
RW
406 api.nexthop_num = MIN(nhp_count, multipath_num);
407 for (i = 0; i < api.nexthop_num; i++) {
2ad4f093
RW
408
409 api_nh = &api.nexthops[i];
4a7371e9 410 api_nh->vrf_id = VRF_DEFAULT;
2ad4f093
RW
411 switch (p->family) {
412 case AF_INET:
b40c5062 413 memcpy(&api_nh->gate.ipv4, nhp_ary4[i],
9913029c
RW
414 sizeof(api_nh->gate.ipv4));
415 api_nh->type = NEXTHOP_TYPE_IPV4;
2ad4f093
RW
416 break;
417 case AF_INET6:
b40c5062 418 memcpy(&api_nh->gate.ipv6, nhp_ary6[i],
9913029c
RW
419 sizeof(api_nh->gate.ipv6));
420 api_nh->type = NEXTHOP_TYPE_IPV6;
2ad4f093 421 break;
9913029c 422 }
2ad4f093 423 }
9913029c 424
2ad4f093
RW
425 if (BGP_DEBUG(zebra, ZEBRA)) {
426 char buf[PREFIX_STRLEN];
d62a17ae 427
2ad4f093 428 prefix2str(&api.prefix, buf, sizeof(buf));
d62a17ae 429 vnc_zlog_debug_verbose(
2ad4f093
RW
430 "%s: Zebra send: route %s %s, nhp_count=%d", __func__,
431 (add ? "add" : "del"), buf, nhp_count);
d62a17ae 432 }
2ad4f093
RW
433
434 zclient_route_send((add ? ZEBRA_ROUTE_ADD : ZEBRA_ROUTE_DELETE),
435 zclient_vnc, &api);
65efcfce
LB
436}
437
438
439static void
d7c0a89a 440nve_list_to_nh_array(uint8_t family, struct list *nve_list,
a74e593b 441 unsigned int *nh_count_ret,
d62a17ae 442 void **nh_ary_ret, /* returned address array */
443 void **nhp_ary_ret) /* returned pointer array */
65efcfce 444{
d62a17ae 445 int nve_count = listcount(nve_list);
65efcfce 446
d62a17ae 447 *nh_count_ret = 0;
448 *nh_ary_ret = NULL;
449 *nhp_ary_ret = NULL;
65efcfce 450
d62a17ae 451 if (!nve_count) {
452 vnc_zlog_debug_verbose("%s: empty nve_list, skipping",
453 __func__);
454 return;
455 }
65efcfce 456
d62a17ae 457 if (family == AF_INET) {
458 struct listnode *ln;
459 struct in_addr *iap;
460 struct in_addr **v;
65efcfce 461
d62a17ae 462 /*
463 * Array of nexthop addresses
464 */
465 *nh_ary_ret =
466 XCALLOC(MTYPE_TMP, nve_count * sizeof(struct in_addr));
65efcfce 467
d62a17ae 468 /*
469 * Array of pointers to nexthop addresses
470 */
471 *nhp_ary_ret = XCALLOC(MTYPE_TMP,
472 nve_count * sizeof(struct in_addr *));
473 iap = *nh_ary_ret;
474 v = *nhp_ary_ret;
65efcfce 475
d62a17ae 476 for (ln = listhead(nve_list); ln; ln = listnextnode(ln)) {
65efcfce 477
d62a17ae 478 struct rfapi_descriptor *irfd;
479 struct prefix nhp;
65efcfce 480
d62a17ae 481 irfd = listgetdata(ln);
65efcfce 482
d62a17ae 483 if (rfapiRaddr2Qprefix(&irfd->vn_addr, &nhp))
484 continue;
65efcfce 485
d62a17ae 486 *iap = nhp.u.prefix4;
487 *v = iap;
488 vnc_zlog_debug_verbose(
489 "%s: ipadr: (%p)<-0x%x, ptr: (%p)<-%p",
490 __func__, iap, nhp.u.prefix4.s_addr, v, iap);
65efcfce 491
d62a17ae 492 ++iap;
493 ++v;
494 ++*nh_count_ret;
495 }
65efcfce 496
d62a17ae 497 } else if (family == AF_INET6) {
65efcfce 498
d62a17ae 499 struct listnode *ln;
65efcfce 500
d62a17ae 501 *nh_ary_ret =
502 XCALLOC(MTYPE_TMP, nve_count * sizeof(struct in6_addr));
65efcfce 503
d62a17ae 504 *nhp_ary_ret = XCALLOC(MTYPE_TMP,
505 nve_count * sizeof(struct in6_addr *));
65efcfce 506
d62a17ae 507 for (ln = listhead(nve_list); ln; ln = listnextnode(ln)) {
65efcfce 508
d62a17ae 509 struct rfapi_descriptor *irfd;
510 struct in6_addr *iap = *nh_ary_ret;
511 struct in6_addr **v = *nhp_ary_ret;
512 struct prefix nhp;
65efcfce 513
d62a17ae 514 irfd = listgetdata(ln);
65efcfce 515
d62a17ae 516 if (rfapiRaddr2Qprefix(&irfd->vn_addr, &nhp))
517 continue;
65efcfce 518
d62a17ae 519 *iap = nhp.u.prefix6;
520 *v = iap;
65efcfce 521
d62a17ae 522 ++iap;
523 ++v;
524 ++*nh_count_ret;
525 }
526 }
65efcfce
LB
527}
528
d62a17ae 529static void import_table_to_nve_list_zebra(struct bgp *bgp,
530 struct rfapi_import_table *it,
531 struct list **nves, uint8_t family)
65efcfce 532{
d62a17ae 533 struct listnode *node;
534 struct rfapi_rfg_name *rfgn;
535
536 /*
537 * Loop over the list of NVE-Groups configured for
538 * exporting to direct-bgp.
539 *
540 * Build a list of NVEs that use this import table
541 */
542 *nves = NULL;
543 for (ALL_LIST_ELEMENTS_RO(bgp->rfapi_cfg->rfg_export_zebra_l, node,
544 rfgn)) {
545
546 /*
547 * If this NVE-Group's import table matches the current one
548 */
549 if (rfgn->rfg && rfgn->rfg->nves
550 && rfgn->rfg->rfapi_import_table == it) {
551
552 nve_group_to_nve_list(rfgn->rfg, nves, family);
553 }
554 }
65efcfce
LB
555}
556
d62a17ae 557static void vnc_zebra_add_del_prefix(struct bgp *bgp,
558 struct rfapi_import_table *import_table,
fe08ba7e 559 struct agg_node *rn,
d62a17ae 560 int add) /* !0 = add, 0 = del */
65efcfce 561{
d62a17ae 562 struct list *nves;
563
a74e593b 564 unsigned int nexthop_count = 0;
d62a17ae 565 void *nh_ary = NULL;
566 void *nhp_ary = NULL;
567
568 vnc_zlog_debug_verbose("%s: entry, add=%d", __func__, add);
569
570 if (zclient_vnc->sock < 0)
571 return;
572
573 if (rn->p.family != AF_INET && rn->p.family != AF_INET6) {
450971aa 574 flog_err(EC_LIB_DEVELOPMENT,
1c50c1c0 575 "%s: invalid route node addr family", __func__);
d62a17ae 576 return;
577 }
578
9f2337c9
RW
579 if (!vrf_bitmap_check(zclient_vnc->redist[family2afi(rn->p.family)]
580 [ZEBRA_ROUTE_VNC],
581 VRF_DEFAULT))
d62a17ae 582 return;
583
584 if (!bgp->rfapi_cfg) {
585 vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
586 __func__);
587 return;
588 }
589 if (!listcount(bgp->rfapi_cfg->rfg_export_zebra_l)) {
590 vnc_zlog_debug_verbose(
591 "%s: no zebra export nve group, skipping", __func__);
592 return;
593 }
594
595 import_table_to_nve_list_zebra(bgp, import_table, &nves, rn->p.family);
596
597 if (nves) {
598 nve_list_to_nh_array(rn->p.family, nves, &nexthop_count,
599 &nh_ary, &nhp_ary);
600
6a154c88 601 list_delete(&nves);
d62a17ae 602
603 if (nexthop_count)
604 vnc_zebra_route_msg(&rn->p, nexthop_count, nhp_ary,
605 add);
606 }
607
0a22ddfb
QY
608 XFREE(MTYPE_TMP, nhp_ary);
609 XFREE(MTYPE_TMP, nh_ary);
65efcfce
LB
610}
611
d62a17ae 612void vnc_zebra_add_prefix(struct bgp *bgp,
613 struct rfapi_import_table *import_table,
fe08ba7e 614 struct agg_node *rn)
65efcfce 615{
d62a17ae 616 vnc_zebra_add_del_prefix(bgp, import_table, rn, 1);
65efcfce
LB
617}
618
d62a17ae 619void vnc_zebra_del_prefix(struct bgp *bgp,
620 struct rfapi_import_table *import_table,
fe08ba7e 621 struct agg_node *rn)
65efcfce 622{
d62a17ae 623 vnc_zebra_add_del_prefix(bgp, import_table, rn, 0);
65efcfce
LB
624}
625
626
d62a17ae 627static void vnc_zebra_add_del_nve(struct bgp *bgp, struct rfapi_descriptor *rfd,
628 int add) /* 0 = del, !0 = add */
65efcfce 629{
d62a17ae 630 struct listnode *node;
631 struct rfapi_rfg_name *rfgn;
632 struct rfapi_nve_group_cfg *rfg = rfd->rfg;
633 afi_t afi = family2afi(rfd->vn_addr.addr_family);
634 struct prefix nhp;
635 // struct prefix *nhpp;
636 void *pAddr;
637
638 vnc_zlog_debug_verbose("%s: entry, add=%d", __func__, add);
639
640 if (zclient_vnc->sock < 0)
641 return;
642
9f2337c9
RW
643 if (!vrf_bitmap_check(zclient_vnc->redist[afi][ZEBRA_ROUTE_VNC],
644 VRF_DEFAULT))
d62a17ae 645 return;
646
647 if (afi != AFI_IP && afi != AFI_IP6) {
450971aa 648 flog_err(EC_LIB_DEVELOPMENT, "%s: invalid vn addr family",
1c50c1c0 649 __func__);
d62a17ae 650 return;
651 }
652
653 if (!bgp)
654 return;
655 if (!bgp->rfapi_cfg) {
656 vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
657 __func__);
658 return;
659 }
660
661 if (rfapiRaddr2Qprefix(&rfd->vn_addr, &nhp)) {
662 vnc_zlog_debug_verbose("%s: can't convert vn address, skipping",
663 __func__);
664 return;
665 }
666
667 pAddr = &nhp.u.prefix4;
668
669 /*
670 * Loop over the list of NVE-Groups configured for
671 * exporting to zebra and see if this new NVE's
672 * group is among them.
673 */
674 for (ALL_LIST_ELEMENTS_RO(bgp->rfapi_cfg->rfg_export_zebra_l, node,
675 rfgn)) {
676
677 /*
678 * Yes, this NVE's group is configured for export to zebra
679 */
680 if (rfgn->rfg == rfg) {
681
fe08ba7e
DS
682 struct agg_table *rt = NULL;
683 struct agg_node *rn;
d62a17ae 684 struct rfapi_import_table *import_table;
685 import_table = rfg->rfapi_import_table;
686
687 vnc_zlog_debug_verbose(
688 "%s: this nve's group is in zebra export list",
689 __func__);
690
691 rt = import_table->imported_vpn[afi];
692
693 /*
694 * Walk the NVE-Group's VNC Import table
695 */
fe08ba7e
DS
696 for (rn = agg_route_top(rt); rn;
697 rn = agg_route_next(rn)) {
d62a17ae 698
699 if (rn->info) {
700
701 vnc_zlog_debug_verbose(
702 "%s: sending %s", __func__,
703 (add ? "add" : "del"));
704 vnc_zebra_route_msg(&rn->p, 1, &pAddr,
705 add);
706 }
707 }
708 }
709 }
65efcfce
LB
710}
711
d62a17ae 712void vnc_zebra_add_nve(struct bgp *bgp, struct rfapi_descriptor *rfd)
65efcfce 713{
d62a17ae 714 vnc_zebra_add_del_nve(bgp, rfd, 1);
65efcfce
LB
715}
716
d62a17ae 717void vnc_zebra_del_nve(struct bgp *bgp, struct rfapi_descriptor *rfd)
65efcfce 718{
d62a17ae 719 vnc_zebra_add_del_nve(bgp, rfd, 0);
65efcfce
LB
720}
721
d62a17ae 722static void vnc_zebra_add_del_group_afi(struct bgp *bgp,
723 struct rfapi_nve_group_cfg *rfg,
724 afi_t afi, int add)
65efcfce 725{
fe08ba7e
DS
726 struct agg_table *rt = NULL;
727 struct agg_node *rn;
d62a17ae 728 struct rfapi_import_table *import_table;
729 uint8_t family = afi2family(afi);
730
731 struct list *nves = NULL;
a74e593b 732 unsigned int nexthop_count = 0;
d62a17ae 733 void *nh_ary = NULL;
734 void *nhp_ary = NULL;
735
736 vnc_zlog_debug_verbose("%s: entry", __func__);
737 import_table = rfg->rfapi_import_table;
738 if (!import_table) {
739 vnc_zlog_debug_verbose(
740 "%s: import table not defined, returning", __func__);
741 return;
742 }
743
744 if (afi == AFI_IP || afi == AFI_IP6) {
745 rt = import_table->imported_vpn[afi];
746 } else {
450971aa 747 flog_err(EC_LIB_DEVELOPMENT, "%s: bad afi %d", __func__, afi);
d62a17ae 748 return;
749 }
750
751 if (!family) {
450971aa 752 flog_err(EC_LIB_DEVELOPMENT, "%s: computed bad family: %d",
1c50c1c0 753 __func__, family);
d62a17ae 754 return;
755 }
756
757 if (!rfg->nves) {
758 /* avoid segfault below if list doesn't exist */
759 vnc_zlog_debug_verbose("%s: no NVEs in this group", __func__);
760 return;
761 }
762
763 nve_group_to_nve_list(rfg, &nves, family);
764 if (nves) {
765 vnc_zlog_debug_verbose("%s: have nves", __func__);
766 nve_list_to_nh_array(family, nves, &nexthop_count, &nh_ary,
767 &nhp_ary);
768
769 vnc_zlog_debug_verbose("%s: family: %d, nve count: %d",
770 __func__, family, nexthop_count);
771
6a154c88 772 list_delete(&nves);
d62a17ae 773
774 if (nexthop_count) {
775 /*
776 * Walk the NVE-Group's VNC Import table
777 */
fe08ba7e
DS
778 for (rn = agg_route_top(rt); rn;
779 rn = agg_route_next(rn)) {
d62a17ae 780 if (rn->info) {
781 vnc_zebra_route_msg(&rn->p,
782 nexthop_count,
783 nhp_ary, add);
784 }
785 }
786 }
0a22ddfb
QY
787 XFREE(MTYPE_TMP, nhp_ary);
788 XFREE(MTYPE_TMP, nh_ary);
d62a17ae 789 }
65efcfce
LB
790}
791
d62a17ae 792void vnc_zebra_add_group(struct bgp *bgp, struct rfapi_nve_group_cfg *rfg)
65efcfce 793{
d62a17ae 794 vnc_zebra_add_del_group_afi(bgp, rfg, AFI_IP, 1);
795 vnc_zebra_add_del_group_afi(bgp, rfg, AFI_IP6, 1);
65efcfce
LB
796}
797
d62a17ae 798void vnc_zebra_del_group(struct bgp *bgp, struct rfapi_nve_group_cfg *rfg)
65efcfce 799{
d62a17ae 800 vnc_zlog_debug_verbose("%s: entry", __func__);
801 vnc_zebra_add_del_group_afi(bgp, rfg, AFI_IP, 0);
802 vnc_zebra_add_del_group_afi(bgp, rfg, AFI_IP6, 0);
65efcfce
LB
803}
804
d62a17ae 805void vnc_zebra_reexport_group_afi(struct bgp *bgp,
806 struct rfapi_nve_group_cfg *rfg, afi_t afi)
65efcfce 807{
d62a17ae 808 struct listnode *node;
809 struct rfapi_rfg_name *rfgn;
810
811 for (ALL_LIST_ELEMENTS_RO(bgp->rfapi_cfg->rfg_export_zebra_l, node,
812 rfgn)) {
813
814 if (rfgn->rfg == rfg) {
815 vnc_zebra_add_del_group_afi(bgp, rfg, afi, 0);
816 vnc_zebra_add_del_group_afi(bgp, rfg, afi, 1);
817 break;
818 }
819 }
65efcfce
LB
820}
821
822
823/***********************************************************************
824 * CONTROL INTERFACE
825 ***********************************************************************/
826
827
828/* Other routes redistribution into BGP. */
d62a17ae 829int vnc_redistribute_set(struct bgp *bgp, afi_t afi, int type)
65efcfce 830{
d62a17ae 831 if (!bgp->rfapi_cfg) {
832 return CMD_WARNING_CONFIG_FAILED;
833 }
65efcfce 834
d62a17ae 835 /* Set flag to BGP instance. */
836 bgp->rfapi_cfg->redist[afi][type] = 1;
65efcfce 837
d62a17ae 838 // bgp->redist[afi][type] = 1;
65efcfce 839
d62a17ae 840 /* Return if already redistribute flag is set. */
9f2337c9 841 if (vrf_bitmap_check(zclient_vnc->redist[afi][type], VRF_DEFAULT))
d62a17ae 842 return CMD_WARNING_CONFIG_FAILED;
65efcfce 843
d62a17ae 844 vrf_bitmap_set(zclient_vnc->redist[afi][type], VRF_DEFAULT);
65efcfce 845
9f2337c9 846 // vrf_bitmap_set(zclient_vnc->redist[afi][type], VRF_DEFAULT);
65efcfce 847
d62a17ae 848 /* Return if zebra connection is not established. */
849 if (zclient_vnc->sock < 0)
850 return CMD_WARNING_CONFIG_FAILED;
65efcfce 851
d62a17ae 852 if (BGP_DEBUG(zebra, ZEBRA))
853 vnc_zlog_debug_verbose("Zebra send: redistribute add %s",
854 zebra_route_string(type));
65efcfce 855
d62a17ae 856 /* Send distribute add message to zebra. */
857 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient_vnc, afi, type,
858 0, VRF_DEFAULT);
65efcfce 859
d62a17ae 860 return CMD_SUCCESS;
65efcfce
LB
861}
862
863/* Unset redistribution. */
d62a17ae 864int vnc_redistribute_unset(struct bgp *bgp, afi_t afi, int type)
65efcfce 865{
d62a17ae 866 vnc_zlog_debug_verbose("%s: type=%d entry", __func__, type);
867
868 if (!bgp->rfapi_cfg) {
869 vnc_zlog_debug_verbose("%s: return (no rfapi_cfg)", __func__);
870 return CMD_WARNING_CONFIG_FAILED;
871 }
872
873 /* Unset flag from BGP instance. */
874 bgp->rfapi_cfg->redist[afi][type] = 0;
875
876 /* Return if zebra connection is disabled. */
9f2337c9 877 if (!vrf_bitmap_check(zclient_vnc->redist[afi][type], VRF_DEFAULT))
d62a17ae 878 return CMD_WARNING_CONFIG_FAILED;
9f2337c9 879 vrf_bitmap_unset(zclient_vnc->redist[afi][type], VRF_DEFAULT);
d62a17ae 880
881 if (bgp->rfapi_cfg->redist[AFI_IP][type] == 0
882 && bgp->rfapi_cfg->redist[AFI_IP6][type] == 0
883 && zclient_vnc->sock >= 0) {
884 /* Send distribute delete message to zebra. */
885 if (BGP_DEBUG(zebra, ZEBRA))
886 vnc_zlog_debug_verbose(
887 "Zebra send: redistribute delete %s",
888 zebra_route_string(type));
889 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient_vnc,
890 afi, type, 0, VRF_DEFAULT);
891 }
892
893 /* Withdraw redistributed routes from current BGP's routing table. */
894 vnc_redistribute_withdraw(bgp, afi, type);
895
896 vnc_zlog_debug_verbose("%s: return", __func__);
897
898 return CMD_SUCCESS;
65efcfce
LB
899}
900
342213ea 901extern struct zebra_privs_t bgpd_privs;
65efcfce
LB
902
903/*
904 * Modeled after bgp_zebra.c'bgp_zebra_init()
905 * Charriere asks, "Is it possible to carry two?"
906 */
d62a17ae 907void vnc_zebra_init(struct thread_master *master)
65efcfce 908{
d62a17ae 909 /* Set default values. */
26f63a1e 910 zclient_vnc = zclient_new(master, &zclient_options_default);
342213ea 911 zclient_init(zclient_vnc, ZEBRA_ROUTE_VNC, 0, &bgpd_privs);
d62a17ae 912
74489921
RW
913 zclient_vnc->redistribute_route_add = vnc_zebra_read_route;
914 zclient_vnc->redistribute_route_del = vnc_zebra_read_route;
65efcfce
LB
915}
916
d62a17ae 917void vnc_zebra_destroy(void)
65efcfce 918{
d62a17ae 919 if (zclient_vnc == NULL)
920 return;
921 zclient_stop(zclient_vnc);
922 zclient_free(zclient_vnc);
923 zclient_vnc = NULL;
65efcfce 924}