]> git.proxmox.com Git - mirror_frr.git/blame - isisd/isis_zebra.c
isisd: uninstall local routes that don't have any valid nexthop
[mirror_frr.git] / isisd / isis_zebra.c
CommitLineData
eb5d44eb 1/*
d62a17ae 2 * IS-IS Rout(e)ing protocol - isis_zebra.c
eb5d44eb 3 *
4 * Copyright (C) 2001,2002 Sampo Saaristo
d62a17ae 5 * Tampere University of Technology
eb5d44eb 6 * Institute of Communications Engineering
f3ccedaa 7 * Copyright (C) 2013-2015 Christian Franke <chris@opensourcerouting.org>
eb5d44eb 8 *
d62a17ae 9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public Licenseas published by the Free
11 * Software Foundation; either version 2 of the License, or (at your option)
eb5d44eb 12 * any later version.
13 *
d62a17ae 14 * This program is distributed in the hope that it will be useful,but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
eb5d44eb 17 * more details.
896014f4
DL
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; see the file COPYING; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
eb5d44eb 22 */
23
24#include <zebra.h>
eb5d44eb 25
26#include "thread.h"
27#include "command.h"
28#include "memory.h"
29#include "log.h"
363be4dd 30#include "lib_errors.h"
eb5d44eb 31#include "if.h"
32#include "network.h"
33#include "prefix.h"
34#include "zclient.h"
35#include "stream.h"
36#include "linklist.h"
f3ccedaa 37#include "nexthop.h"
7076bb2f 38#include "vrf.h"
8879bd22 39#include "libfrr.h"
eb5d44eb 40
41#include "isisd/isis_constants.h"
42#include "isisd/isis_common.h"
3f045a08
JB
43#include "isisd/isis_flags.h"
44#include "isisd/isis_misc.h"
45#include "isisd/isis_circuit.h"
c89c05dd 46#include "isisd/isisd.h"
eb5d44eb 47#include "isisd/isis_circuit.h"
48#include "isisd/isis_csm.h"
3f045a08 49#include "isisd/isis_lsp.h"
eb5d44eb 50#include "isisd/isis_route.h"
51#include "isisd/isis_zebra.h"
c3f7b406 52#include "isisd/isis_adjacency.h"
f8c06e2c 53#include "isisd/isis_te.h"
26f6acaf 54#include "isisd/isis_sr.h"
1cbf96a8 55#include "isisd/isis_ldp_sync.h"
eb5d44eb 56
26f6acaf
RW
57struct zclient *zclient;
58static struct zclient *zclient_sync;
59
18a6dce6 60/* Router-id update message from zebra. */
121f9dee 61static int isis_router_id_update_zebra(ZAPI_CALLBACK_ARGS)
18a6dce6 62{
d62a17ae 63 struct isis_area *area;
64 struct listnode *node;
65 struct prefix router_id;
eab88f36
K
66 struct isis *isis = NULL;
67
68 isis = isis_lookup_by_vrfid(vrf_id);
69
70 if (isis == NULL) {
71 return -1;
72 }
d62a17ae 73
d62a17ae 74 zebra_router_id_update_read(zclient->ibuf, &router_id);
75 if (isis->router_id == router_id.u.prefix4.s_addr)
76 return 0;
77
78 isis->router_id = router_id.u.prefix4.s_addr;
79 for (ALL_LIST_ELEMENTS_RO(isis->area_list, node, area))
80 if (listcount(area->area_addrs) > 0)
81 lsp_regenerate_schedule(area, area->is_type, 0);
82
83 return 0;
18a6dce6 84}
eb5d44eb 85
121f9dee 86static int isis_zebra_if_address_add(ZAPI_CALLBACK_ARGS)
eb5d44eb 87{
115f8f56 88 struct isis_circuit *circuit;
d62a17ae 89 struct connected *c;
eb5d44eb 90
d62a17ae 91 c = zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_ADD,
92 zclient->ibuf, vrf_id);
f390d2c7 93
d62a17ae 94 if (c == NULL)
95 return 0;
f390d2c7 96
62f30dcc 97#ifdef EXTREME_DEBUG
28315916 98 if (c->address->family == AF_INET)
2dbe669b 99 zlog_debug("connected IP address %pFX", c->address);
28315916 100 if (c->address->family == AF_INET6)
2dbe669b 101 zlog_debug("connected IPv6 address %pFX", c->address);
eb5d44eb 102#endif /* EXTREME_DEBUG */
115f8f56
IR
103
104 if (if_is_operative(c->ifp)) {
105 circuit = circuit_scan_by_ifp(c->ifp);
106 if (circuit)
107 isis_circuit_add_addr(circuit, c);
108 }
eb5d44eb 109
d62a17ae 110 return 0;
eb5d44eb 111}
112
121f9dee 113static int isis_zebra_if_address_del(ZAPI_CALLBACK_ARGS)
eb5d44eb 114{
115f8f56 115 struct isis_circuit *circuit;
d62a17ae 116 struct connected *c;
eb5d44eb 117
d62a17ae 118 c = zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_DELETE,
119 zclient->ibuf, vrf_id);
f390d2c7 120
d62a17ae 121 if (c == NULL)
122 return 0;
f390d2c7 123
f891f443 124#ifdef EXTREME_DEBUG
28315916 125 if (c->address->family == AF_INET)
2dbe669b 126 zlog_debug("disconnected IP address %pFX", c->address);
28315916 127 if (c->address->family == AF_INET6)
2dbe669b 128 zlog_debug("disconnected IPv6 address %pFX", c->address);
f891f443 129#endif /* EXTREME_DEBUG */
f390d2c7 130
115f8f56
IR
131 if (if_is_operative(c->ifp)) {
132 circuit = circuit_scan_by_ifp(c->ifp);
133 if (circuit)
134 isis_circuit_del_addr(circuit, c);
135 }
136
721c0857 137 connected_free(&c);
f390d2c7 138
d62a17ae 139 return 0;
eb5d44eb 140}
141
121f9dee 142static int isis_zebra_link_params(ZAPI_CALLBACK_ARGS)
f8c06e2c 143{
d62a17ae 144 struct interface *ifp;
f8c06e2c 145
edc12762 146 ifp = zebra_interface_link_params_read(zclient->ibuf, vrf_id);
f8c06e2c 147
d62a17ae 148 if (ifp == NULL)
149 return 0;
f8c06e2c 150
d62a17ae 151 /* Update TE TLV */
152 isis_mpls_te_update(ifp);
f8c06e2c 153
d62a17ae 154 return 0;
f8c06e2c
OD
155}
156
c951ee6e 157enum isis_zebra_nexthop_type {
d47d6089
RW
158 ISIS_NEXTHOP_MAIN = 0,
159 ISIS_NEXTHOP_BACKUP,
c951ee6e
RW
160};
161
162static int isis_zebra_add_nexthops(struct isis *isis, struct list *nexthops,
163 struct zapi_nexthop zapi_nexthops[],
164 enum isis_zebra_nexthop_type type,
d47d6089 165 bool mpls_lsp, uint8_t backup_nhs)
eb5d44eb 166{
d62a17ae 167 struct isis_nexthop *nexthop;
168 struct listnode *node;
c0721de4 169 int count = 0;
d62a17ae 170
f80dd32b 171 /* Nexthops */
c951ee6e
RW
172 for (ALL_LIST_ELEMENTS_RO(nexthops, node, nexthop)) {
173 struct zapi_nexthop *api_nh;
174
363be4dd
RW
175 if (count >= MULTIPATH_NUM)
176 break;
c951ee6e 177 api_nh = &zapi_nexthops[count];
363be4dd 178 if (fabricd)
68a02e06 179 SET_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_ONLINK);
dc18b3b0 180 api_nh->vrf_id = isis->vrf_id;
363be4dd
RW
181
182 switch (nexthop->family) {
183 case AF_INET:
f80dd32b 184 /* FIXME: can it be ? */
363be4dd 185 if (nexthop->ip.ipv4.s_addr != INADDR_ANY) {
f80dd32b 186 api_nh->type = NEXTHOP_TYPE_IPV4_IFINDEX;
363be4dd 187 api_nh->gate.ipv4 = nexthop->ip.ipv4;
f80dd32b
RW
188 } else {
189 api_nh->type = NEXTHOP_TYPE_IFINDEX;
190 }
363be4dd
RW
191 break;
192 case AF_INET6:
193 if (!IN6_IS_ADDR_LINKLOCAL(&nexthop->ip.ipv6)
194 && !IN6_IS_ADDR_UNSPECIFIED(&nexthop->ip.ipv6)) {
f80dd32b
RW
195 continue;
196 }
363be4dd 197 api_nh->gate.ipv6 = nexthop->ip.ipv6;
f80dd32b 198 api_nh->type = NEXTHOP_TYPE_IPV6_IFINDEX;
363be4dd
RW
199 break;
200 default:
201 flog_err(EC_LIB_DEVELOPMENT,
202 "%s: unknown address family [%d]", __func__,
203 nexthop->family);
204 exit(1);
d62a17ae 205 }
363be4dd
RW
206
207 api_nh->ifindex = nexthop->ifindex;
c951ee6e
RW
208
209 /* Add MPLS label(s). */
210 switch (type) {
d47d6089
RW
211 case ISIS_NEXTHOP_MAIN:
212 if (nexthop->sr.present) {
c951ee6e
RW
213 api_nh->label_num = 1;
214 api_nh->labels[0] = nexthop->sr.label;
d47d6089
RW
215 } else if (mpls_lsp)
216 /*
217 * Do not use non-SR enabled nexthops to prevent
218 * broken LSPs from being formed.
219 */
220 continue;
c951ee6e 221 break;
d47d6089 222 case ISIS_NEXTHOP_BACKUP:
c951ee6e
RW
223 if (nexthop->label_stack) {
224 api_nh->label_num =
225 nexthop->label_stack->num_labels;
226 memcpy(api_nh->labels,
227 nexthop->label_stack->label,
228 sizeof(mpls_label_t)
229 * api_nh->label_num);
d47d6089
RW
230 } else if (mpls_lsp) {
231 /*
232 * This is necessary because zebra requires
233 * the nexthops of MPLS LSPs to be labeled.
234 */
c951ee6e
RW
235 api_nh->label_num = 1;
236 api_nh->labels[0] = MPLS_LABEL_IMPLICIT_NULL;
237 }
238 break;
239 }
240
241 /* Backup nexthop handling. */
242 if (backup_nhs) {
243 SET_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_HAS_BACKUP);
244 /*
245 * If the backup has multiple nexthops, all of them
246 * protect the same primary nexthop since ECMP routes
247 * have no backups.
248 */
249 api_nh->backup_num = backup_nhs;
250 for (int i = 0; i < backup_nhs; i++)
251 api_nh->backup_idx[i] = i;
252 }
363be4dd 253 count++;
d62a17ae 254 }
c951ee6e
RW
255
256 return count;
257}
258
259void isis_zebra_route_add_route(struct isis *isis, struct prefix *prefix,
260 struct prefix_ipv6 *src_p,
261 struct isis_route_info *route_info)
262{
263 struct zapi_route api;
264 int count = 0;
265
e33b95b4 266 if (zclient->sock < 0)
c0721de4 267 return;
f390d2c7 268
e33b95b4
RW
269 /* Uninstall the route if it doesn't have any valid nexthop. */
270 if (list_isempty(route_info->nexthops)) {
271 isis_zebra_route_del_route(isis, prefix, src_p, route_info);
272 return;
273 }
274
c951ee6e
RW
275 memset(&api, 0, sizeof(api));
276 api.vrf_id = isis->vrf_id;
277 api.type = PROTO_TYPE;
278 api.safi = SAFI_UNICAST;
279 api.prefix = *prefix;
280 if (src_p && src_p->prefixlen) {
281 api.src_prefix = *src_p;
282 SET_FLAG(api.message, ZAPI_MESSAGE_SRCPFX);
283 }
284 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
285 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
286 api.metric = route_info->cost;
287
288 /* Add backup nexthops first. */
289 if (route_info->backup) {
290 count = isis_zebra_add_nexthops(
291 isis, route_info->backup->nexthops, api.backup_nexthops,
d47d6089 292 ISIS_NEXTHOP_BACKUP, false, 0);
c951ee6e
RW
293 if (count > 0) {
294 SET_FLAG(api.message, ZAPI_MESSAGE_BACKUP_NEXTHOPS);
295 api.backup_nexthop_num = count;
296 }
297 }
298
299 /* Add primary nexthops. */
300 count = isis_zebra_add_nexthops(isis, route_info->nexthops,
d47d6089 301 api.nexthops, ISIS_NEXTHOP_MAIN, false,
c951ee6e
RW
302 count);
303 if (!count)
304 return;
c0721de4 305 api.nexthop_num = count;
f390d2c7 306
c0721de4 307 zclient_route_send(ZEBRA_ROUTE_ADD, zclient, &api);
eb5d44eb 308}
309
dc18b3b0
K
310void isis_zebra_route_del_route(struct isis *isis,
311 struct prefix *prefix,
0a5f3f4f
RW
312 struct prefix_ipv6 *src_p,
313 struct isis_route_info *route_info)
eb5d44eb 314{
c0721de4 315 struct zapi_route api;
d62a17ae 316
0a5f3f4f 317 if (zclient->sock < 0)
d62a17ae 318 return;
319
c0721de4 320 memset(&api, 0, sizeof(api));
dc18b3b0 321 api.vrf_id = isis->vrf_id;
7c0cbd0e 322 api.type = PROTO_TYPE;
d62a17ae 323 api.safi = SAFI_UNICAST;
c0721de4 324 api.prefix = *prefix;
321c1bbb
CF
325 if (src_p && src_p->prefixlen) {
326 api.src_prefix = *src_p;
327 SET_FLAG(api.message, ZAPI_MESSAGE_SRCPFX);
328 }
f390d2c7 329
c0721de4 330 zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
eb5d44eb 331}
332
f2333421 333/**
d47d6089 334 * Install Prefix-SID label entry in the forwarding plane through Zebra.
f2333421 335 *
d47d6089
RW
336 * @param area IS-IS area
337 * @param prefix Route prefix
338 * @param rinfo Route information
339 * @param psid Prefix-SID information
f2333421 340 */
d47d6089
RW
341void isis_zebra_prefix_sid_install(struct isis_area *area,
342 struct prefix *prefix,
343 struct isis_route_info *rinfo,
344 struct isis_sr_psid_info *psid)
26f6acaf
RW
345{
346 struct zapi_labels zl;
c951ee6e 347 int count = 0;
26f6acaf 348
d47d6089
RW
349 sr_debug("ISIS-Sr (%s): update label %u for prefix %pFX",
350 area->area_tag, psid->label, prefix);
351
26f6acaf
RW
352 /* Prepare message. */
353 memset(&zl, 0, sizeof(zl));
354 zl.type = ZEBRA_LSP_ISIS_SR;
d47d6089
RW
355 zl.local_label = psid->label;
356
357 /* Local routes don't have any nexthop and require special handling. */
358 if (list_isempty(rinfo->nexthops)) {
359 struct zapi_nexthop *znh;
360 struct interface *ifp;
26f6acaf 361
26f6acaf
RW
362 ifp = if_lookup_by_name("lo", VRF_DEFAULT);
363 if (!ifp) {
364 zlog_warn(
365 "%s: couldn't install Prefix-SID %pFX: loopback interface not found",
d47d6089 366 __func__, prefix);
26f6acaf
RW
367 return;
368 }
369
370 znh = &zl.nexthops[zl.nexthop_num++];
371 znh->type = NEXTHOP_TYPE_IFINDEX;
372 znh->ifindex = ifp->ifindex;
e0e8a84f
OD
373 znh->label_num = 1;
374 znh->labels[0] = MPLS_LABEL_IMPLICIT_NULL;
d47d6089 375 } else {
c951ee6e
RW
376 /* Add backup nexthops first. */
377 if (rinfo->backup) {
378 count = isis_zebra_add_nexthops(
d47d6089
RW
379 area->isis, rinfo->backup->nexthops,
380 zl.backup_nexthops, ISIS_NEXTHOP_BACKUP, true,
c951ee6e
RW
381 0);
382 if (count > 0) {
383 SET_FLAG(zl.message, ZAPI_LABELS_HAS_BACKUPS);
384 zl.backup_nexthop_num = count;
385 }
26f6acaf 386 }
c951ee6e
RW
387
388 /* Add primary nexthops. */
d47d6089
RW
389 count = isis_zebra_add_nexthops(area->isis, rinfo->nexthops,
390 zl.nexthops, ISIS_NEXTHOP_MAIN,
391 true, count);
c951ee6e
RW
392 if (!count)
393 return;
394 zl.nexthop_num = count;
26f6acaf
RW
395 }
396
397 /* Send message to zebra. */
398 (void)zebra_send_mpls_labels(zclient, ZEBRA_MPLS_LABELS_REPLACE, &zl);
399}
400
f2333421 401/**
d47d6089 402 * Uninstall Prefix-SID label entry from the forwarding plane through Zebra.
f2333421 403 *
d47d6089
RW
404 * @param area IS-IS area
405 * @param prefix Route prefix
406 * @param rinfo Route information
407 * @param psid Prefix-SID information
f2333421 408 */
d47d6089
RW
409void isis_zebra_prefix_sid_uninstall(struct isis_area *area,
410 struct prefix *prefix,
411 struct isis_route_info *rinfo,
412 struct isis_sr_psid_info *psid)
26f6acaf
RW
413{
414 struct zapi_labels zl;
415
d47d6089
RW
416 sr_debug("ISIS-Sr (%s): delete label %u for prefix %pFX",
417 area->area_tag, psid->label, prefix);
418
26f6acaf
RW
419 /* Prepare message. */
420 memset(&zl, 0, sizeof(zl));
421 zl.type = ZEBRA_LSP_ISIS_SR;
d47d6089 422 zl.local_label = psid->label;
26f6acaf
RW
423
424 /* Send message to zebra. */
425 (void)zebra_send_mpls_labels(zclient, ZEBRA_MPLS_LABELS_DELETE, &zl);
426}
427
f2333421
OD
428/**
429 * Send (LAN)-Adjacency-SID to ZEBRA for installation or deletion.
430 *
431 * @param cmd ZEBRA_MPLS_LABELS_ADD or ZEBRA_ROUTE_DELETE
432 * @param sra Segment Routing Adjacency-SID
433 */
c3f7b406
OD
434void isis_zebra_send_adjacency_sid(int cmd, const struct sr_adjacency *sra)
435{
054fda12 436 struct isis *isis = sra->adj->circuit->area->isis;
c3f7b406
OD
437 struct zapi_labels zl;
438 struct zapi_nexthop *znh;
439
440 if (cmd != ZEBRA_MPLS_LABELS_ADD && cmd != ZEBRA_MPLS_LABELS_DELETE) {
441 flog_warn(EC_LIB_DEVELOPMENT, "%s: wrong ZEBRA command",
442 __func__);
443 return;
444 }
445
446 sr_debug(" |- %s label %u for interface %s",
447 cmd == ZEBRA_MPLS_LABELS_ADD ? "Add" : "Delete",
054fda12 448 sra->input_label, sra->adj->circuit->interface->name);
c3f7b406
OD
449
450 memset(&zl, 0, sizeof(zl));
451 zl.type = ZEBRA_LSP_ISIS_SR;
054fda12 452 zl.local_label = sra->input_label;
c3f7b406
OD
453 zl.nexthop_num = 1;
454 znh = &zl.nexthops[0];
455 znh->gate = sra->nexthop.address;
456 znh->type = (sra->nexthop.family == AF_INET)
457 ? NEXTHOP_TYPE_IPV4_IFINDEX
458 : NEXTHOP_TYPE_IPV6_IFINDEX;
459 znh->ifindex = sra->adj->circuit->interface->ifindex;
460 znh->label_num = 1;
461 znh->labels[0] = MPLS_LABEL_IMPLICIT_NULL;
462
054fda12
RW
463 /* Set backup nexthops. */
464 if (sra->type == ISIS_SR_LAN_BACKUP) {
465 int count;
466
467 count = isis_zebra_add_nexthops(isis, sra->backup_nexthops,
468 zl.backup_nexthops,
d47d6089 469 ISIS_NEXTHOP_BACKUP, true, 0);
054fda12
RW
470 if (count > 0) {
471 SET_FLAG(zl.message, ZAPI_LABELS_HAS_BACKUPS);
472 zl.backup_nexthop_num = count;
473
474 SET_FLAG(znh->flags, ZAPI_NEXTHOP_FLAG_HAS_BACKUP);
475 znh->backup_num = count;
476 for (int i = 0; i < count; i++)
477 znh->backup_idx[i] = i;
478 }
479 }
480
c3f7b406
OD
481 (void)zebra_send_mpls_labels(zclient, cmd, &zl);
482}
483
121f9dee 484static int isis_zebra_read(ZAPI_CALLBACK_ARGS)
eb5d44eb 485{
74489921 486 struct zapi_route api;
eab88f36
K
487 struct isis *isis = NULL;
488
489 isis = isis_lookup_by_vrfid(vrf_id);
490
491 if (isis == NULL)
492 return -1;
d62a17ae 493
74489921
RW
494 if (zapi_route_decode(zclient->ibuf, &api) < 0)
495 return -1;
d62a17ae 496
9fb2b879
DS
497 if (api.prefix.family == AF_INET6
498 && IN6_IS_ADDR_LINKLOCAL(&api.prefix.u.prefix6))
499 return 0;
500
d62a17ae 501 /*
502 * Avoid advertising a false default reachability. (A default
503 * route installed by IS-IS gets redistributed from zebra back
504 * into IS-IS causing us to start advertising default reachabity
505 * without this check)
506 */
d43d2df5
CF
507 if (api.prefix.prefixlen == 0
508 && api.src_prefix.prefixlen == 0
7c0cbd0e 509 && api.type == PROTO_TYPE) {
121f9dee 510 cmd = ZEBRA_REDISTRIBUTE_ROUTE_DEL;
d43d2df5 511 }
d62a17ae 512
121f9dee 513 if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD)
eab88f36 514 isis_redist_add(isis, api.type, &api.prefix, &api.src_prefix,
d43d2df5 515 api.distance, api.metric);
d62a17ae 516 else
eab88f36
K
517 isis_redist_delete(isis, api.type, &api.prefix,
518 &api.src_prefix);
d62a17ae 519
520 return 0;
eb5d44eb 521}
eb5d44eb 522
d62a17ae 523int isis_distribute_list_update(int routetype)
eb5d44eb 524{
d62a17ae 525 return 0;
eb5d44eb 526}
527
d62a17ae 528void isis_zebra_redistribute_set(afi_t afi, int type)
eb5d44eb 529{
d62a17ae 530 if (type == DEFAULT_ROUTE)
531 zclient_redistribute_default(ZEBRA_REDISTRIBUTE_DEFAULT_ADD,
49db7a7b 532 zclient, afi, VRF_DEFAULT);
d62a17ae 533 else
534 zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD, zclient, afi, type,
535 0, VRF_DEFAULT);
f3ccedaa
CF
536}
537
d62a17ae 538void isis_zebra_redistribute_unset(afi_t afi, int type)
f3ccedaa 539{
d62a17ae 540 if (type == DEFAULT_ROUTE)
541 zclient_redistribute_default(ZEBRA_REDISTRIBUTE_DEFAULT_DELETE,
49db7a7b 542 zclient, afi, VRF_DEFAULT);
d62a17ae 543 else
544 zclient_redistribute(ZEBRA_REDISTRIBUTE_DELETE, zclient, afi,
545 type, 0, VRF_DEFAULT);
eb5d44eb 546}
547
774e3570
OD
548/* Label Manager Functions */
549
58fbcdf2
OD
550/**
551 * Check if Label Manager is Ready or not.
552 *
553 * @return True if Label Manager is ready, False otherwise
554 */
555bool isis_zebra_label_manager_ready(void)
556{
557 return (zclient_sync->sock > 0);
558}
559
774e3570
OD
560/**
561 * Request Label Range to the Label Manager.
562 *
563 * @param base base label of the label range to request
564 * @param chunk_size size of the label range to request
565 *
566 * @return 0 on success, -1 on failure
567 */
26f6acaf
RW
568int isis_zebra_request_label_range(uint32_t base, uint32_t chunk_size)
569{
570 int ret;
571 uint32_t start, end;
572
58fbcdf2
OD
573 if (zclient_sync->sock < 0)
574 return -1;
26f6acaf
RW
575
576 ret = lm_get_label_chunk(zclient_sync, 0, base, chunk_size, &start,
577 &end);
578 if (ret < 0) {
579 zlog_warn("%s: error getting label range!", __func__);
580 return -1;
581 }
582
583 return 0;
584}
585
774e3570
OD
586/**
587 * Release Label Range to the Label Manager.
588 *
589 * @param start start of label range to release
590 * @param end end of label range to release
774e3570 591 *
58fbcdf2 592 * @return 0 on success, -1 otherwise
774e3570 593 */
58fbcdf2 594int isis_zebra_release_label_range(uint32_t start, uint32_t end)
26f6acaf
RW
595{
596 int ret;
26f6acaf 597
58fbcdf2 598 if (zclient_sync->sock < 0)
26f6acaf 599 return -1;
26f6acaf
RW
600
601 ret = lm_release_label_chunk(zclient_sync, start, end);
602 if (ret < 0) {
26f6acaf 603 zlog_warn("%s: error releasing label range!", __func__);
26f6acaf
RW
604 return -1;
605 }
606
607 return 0;
608}
609
774e3570
OD
610/**
611 * Connect to the Label Manager.
58fbcdf2
OD
612 *
613 * @return 0 on success, -1 otherwise
774e3570 614 */
58fbcdf2 615int isis_zebra_label_manager_connect(void)
26f6acaf
RW
616{
617 /* Connect to label manager. */
58fbcdf2
OD
618 if (zclient_socket_connect(zclient_sync) < 0) {
619 zlog_warn("%s: failed connecting synchronous zclient!",
26f6acaf 620 __func__);
58fbcdf2 621 return -1;
26f6acaf 622 }
774e3570 623 /* make socket non-blocking */
26f6acaf 624 set_nonblocking(zclient_sync->sock);
774e3570
OD
625
626 /* Send hello to notify zebra this is a synchronous client */
7cfdb485 627 if (zclient_send_hello(zclient_sync) == ZCLIENT_SEND_FAILURE) {
58fbcdf2
OD
628 zlog_warn("%s: failed sending hello for synchronous zclient!",
629 __func__);
630 close(zclient_sync->sock);
631 zclient_sync->sock = -1;
632 return -1;
774e3570
OD
633 }
634
635 /* Connect to label manager */
58fbcdf2
OD
636 if (lm_label_manager_connect(zclient_sync, 0) != 0) {
637 zlog_warn("%s: failed connecting to label manager!", __func__);
638 if (zclient_sync->sock > 0) {
639 close(zclient_sync->sock);
640 zclient_sync->sock = -1;
641 }
642 return -1;
26f6acaf
RW
643 }
644
58fbcdf2
OD
645 sr_debug("ISIS-Sr: Successfully connected to the Label Manager");
646
647 return 0;
26f6acaf
RW
648}
649
65251ce8 650void isis_zebra_vrf_register(struct isis *isis)
651{
652 if (!zclient || zclient->sock < 0 || !isis)
653 return;
654
655 if (isis->vrf_id != VRF_UNKNOWN) {
656 if (IS_DEBUG_EVENTS)
657 zlog_debug("%s: Register VRF %s id %u", __func__,
658 isis->name, isis->vrf_id);
659 zclient_send_reg_requests(zclient, isis->vrf_id);
660 }
661}
662
663
d62a17ae 664static void isis_zebra_connected(struct zclient *zclient)
7076bb2f 665{
d62a17ae 666 zclient_send_reg_requests(zclient, VRF_DEFAULT);
7076bb2f
FL
667}
668
1cbf96a8 669/*
670 * opaque messages between processes
671 */
672static int isis_opaque_msg_handler(ZAPI_CALLBACK_ARGS)
673{
674 struct stream *s;
675 struct zapi_opaque_msg info;
676 struct ldp_igp_sync_if_state state;
677 struct ldp_igp_sync_announce announce;
678 struct ldp_igp_sync_hello hello;
679 int ret = 0;
680
681 s = zclient->ibuf;
682 if (zclient_opaque_decode(s, &info) != 0)
683 return -1;
684
685 switch (info.type) {
686 case LDP_IGP_SYNC_IF_STATE_UPDATE:
687 STREAM_GET(&state, s, sizeof(state));
688 ret = isis_ldp_sync_state_update(state);
689 break;
690 case LDP_IGP_SYNC_ANNOUNCE_UPDATE:
691 STREAM_GET(&announce, s, sizeof(announce));
692 ret = isis_ldp_sync_announce_update(announce);
693 break;
694 case LDP_IGP_SYNC_HELLO_UPDATE:
695 STREAM_GET(&hello, s, sizeof(hello));
696 ret = isis_ldp_sync_hello_update(hello);
697 break;
698 default:
699 break;
700 }
701
702stream_failure:
703
704 return ret;
705}
706
26f6acaf 707void isis_zebra_init(struct thread_master *master, int instance)
eb5d44eb 708{
26f6acaf 709 /* Initialize asynchronous zclient. */
26f63a1e 710 zclient = zclient_new(master, &zclient_options_default);
7c0cbd0e 711 zclient_init(zclient, PROTO_TYPE, 0, &isisd_privs);
d62a17ae 712 zclient->zebra_connected = isis_zebra_connected;
713 zclient->router_id_update = isis_router_id_update_zebra;
d62a17ae 714 zclient->interface_address_add = isis_zebra_if_address_add;
715 zclient->interface_address_delete = isis_zebra_if_address_del;
716 zclient->interface_link_params = isis_zebra_link_params;
74489921
RW
717 zclient->redistribute_route_add = isis_zebra_read;
718 zclient->redistribute_route_del = isis_zebra_read;
d62a17ae 719
26f6acaf 720 /* Initialize special zclient for synchronous message exchanges. */
774e3570
OD
721 struct zclient_options options = zclient_options_default;
722 options.synchronous = true;
723 zclient_sync = zclient_new(master, &options);
26f6acaf
RW
724 zclient_sync->sock = -1;
725 zclient_sync->redist_default = ZEBRA_ROUTE_ISIS;
726 zclient_sync->instance = instance;
774e3570
OD
727 /*
728 * session_id must be different from default value (0) to distinguish
729 * the asynchronous socket from the synchronous one
730 */
731 zclient_sync->session_id = 1;
26f6acaf 732 zclient_sync->privs = &isisd_privs;
1cbf96a8 733
734 zclient->opaque_msg_handler = isis_opaque_msg_handler;
eb5d44eb 735}
8d429559 736
d62a17ae 737void isis_zebra_stop(void)
8d429559 738{
58fbcdf2
OD
739 zclient_stop(zclient_sync);
740 zclient_free(zclient_sync);
d62a17ae 741 zclient_stop(zclient);
742 zclient_free(zclient);
8879bd22 743 frr_fini();
8d429559 744}