4 * Copyright (c) 2013, 2016 Renato Westphal <renato@openbsd.org>
5 * Copyright (c) 2004, 2005 Claudio Jeker <claudio@openbsd.org>
6 * Copyright (c) 2004 Esben Norby <norby@openbsd.org>
7 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
29 #include "ldp_debug.h"
36 #include <lib/linklist.h>
41 static void lde_shutdown(void);
42 static int lde_dispatch_imsg(struct thread
*);
43 static int lde_dispatch_parent(struct thread
*);
44 static __inline
int lde_nbr_compare(const struct lde_nbr
*,
45 const struct lde_nbr
*);
46 static struct lde_nbr
*lde_nbr_new(uint32_t, struct lde_nbr
*);
47 static void lde_nbr_del(struct lde_nbr
*);
48 static struct lde_nbr
*lde_nbr_find(uint32_t);
49 static void lde_nbr_clear(void);
50 static void lde_nbr_addr_update(struct lde_nbr
*,
51 struct lde_addr
*, int);
52 static __inline
int lde_map_compare(const struct lde_map
*,
53 const struct lde_map
*);
54 static void lde_map_free(void *);
55 static int lde_address_add(struct lde_nbr
*, struct lde_addr
*);
56 static int lde_address_del(struct lde_nbr
*, struct lde_addr
*);
57 static void lde_address_list_free(struct lde_nbr
*);
58 static void zclient_sync_init(u_short instance
);
59 static void lde_label_list_init(void);
60 static int lde_get_label_chunk(void);
61 static void on_get_label_chunk_response(uint32_t start
, uint32_t end
);
62 static uint32_t lde_get_next_label(void);
64 RB_GENERATE(nbr_tree
, lde_nbr
, entry
, lde_nbr_compare
)
65 RB_GENERATE(lde_map_head
, lde_map
, entry
, lde_map_compare
)
67 struct ldpd_conf
*ldeconf
;
68 struct nbr_tree lde_nbrs
= RB_INITIALIZER(&lde_nbrs
);
70 static struct imsgev
*iev_ldpe
;
71 static struct imsgev
*iev_main
, *iev_main_sync
;
73 /* Master of threads. */
74 struct thread_master
*master
;
77 static zebra_capabilities_t _caps_p
[] =
82 static struct zebra_privs_t lde_privs
=
84 #if defined(VTY_GROUP)
85 .vty_group
= VTY_GROUP
,
88 .cap_num_p
= array_size(_caps_p
),
92 /* List of chunks of labels externally assigned by Zebra */
93 static struct list
*label_chunk_list
;
94 static struct listnode
*current_label_chunk
;
96 /* Synchronous zclient to request labels */
97 static struct zclient
*zclient_sync
;
99 /* SIGINT / SIGTERM handler. */
106 static struct quagga_signal_t lde_signals
[] =
122 /* label decision engine */
126 struct thread thread
;
128 #ifdef HAVE_SETPROCTITLE
129 setproctitle("label decision engine");
131 ldpd_process
= PROC_LDE_ENGINE
;
132 log_procname
= log_procnames
[PROC_LDE_ENGINE
];
134 master
= thread_master_create();
136 /* setup signal handler */
137 signal_init(master
, array_size(lde_signals
), lde_signals
);
139 /* setup pipes and event handlers to the parent process */
140 if ((iev_main
= calloc(1, sizeof(struct imsgev
))) == NULL
)
142 imsg_init(&iev_main
->ibuf
, LDPD_FD_ASYNC
);
143 iev_main
->handler_read
= lde_dispatch_parent
;
144 iev_main
->ev_read
= NULL
;
145 thread_add_read(master
, iev_main
->handler_read
, iev_main
, iev_main
->ibuf
.fd
,
147 iev_main
->handler_write
= ldp_write_handler
;
149 if ((iev_main_sync
= calloc(1, sizeof(struct imsgev
))) == NULL
)
151 imsg_init(&iev_main_sync
->ibuf
, LDPD_FD_SYNC
);
153 /* create base configuration */
154 ldeconf
= config_new_empty();
156 /* Fetch next active thread. */
157 while (thread_fetch(master
, &thread
))
158 thread_call(&thread
);
162 lde_init(struct ldpd_init
*init
)
164 /* drop privileges */
165 lde_privs
.user
= init
->user
;
166 lde_privs
.group
= init
->group
;
167 zprivs_init(&lde_privs
);
169 /* start the LIB garbage collector */
170 lde_gc_start_timer();
172 /* Init synchronous zclient and label list */
173 zclient_serv_path_set(init
->zclient_serv_path
);
174 zclient_sync_init(init
->instance
);
175 lde_label_list_init();
183 msgbuf_clear(&iev_ldpe
->ibuf
.w
);
184 close(iev_ldpe
->ibuf
.fd
);
186 msgbuf_clear(&iev_main
->ibuf
.w
);
187 close(iev_main
->ibuf
.fd
);
188 msgbuf_clear(&iev_main_sync
->ibuf
.w
);
189 close(iev_main_sync
->ibuf
.fd
);
195 config_clear(ldeconf
);
202 log_info("label decision engine exiting");
208 lde_imsg_compose_parent(int type
, pid_t pid
, void *data
, uint16_t datalen
)
210 return (imsg_compose_event(iev_main
, type
, 0, pid
, -1, data
, datalen
));
214 lde_imsg_compose_parent_sync(int type
, pid_t pid
, void *data
, uint16_t datalen
)
216 imsg_compose_event(iev_main_sync
, type
, 0, pid
, -1, data
, datalen
);
217 imsg_flush(&iev_main_sync
->ibuf
);
221 lde_imsg_compose_ldpe(int type
, uint32_t peerid
, pid_t pid
, void *data
,
224 return (imsg_compose_event(iev_ldpe
, type
, peerid
, pid
,
230 lde_dispatch_imsg(struct thread
*thread
)
232 struct imsgev
*iev
= THREAD_ARG(thread
);
233 struct imsgbuf
*ibuf
= &iev
->ibuf
;
237 struct lde_addr
*lde_addr
;
238 struct notify_msg
*nm
;
244 if ((n
= imsg_read(ibuf
)) == -1 && errno
!= EAGAIN
)
245 fatal("imsg_read error");
246 if (n
== 0) /* connection closed */
250 if ((n
= imsg_get(ibuf
, &imsg
)) == -1)
251 fatal("lde_dispatch_imsg: imsg_get error");
255 switch (imsg
.hdr
.type
) {
256 case IMSG_LABEL_MAPPING_FULL
:
257 ln
= lde_nbr_find(imsg
.hdr
.peerid
);
259 log_debug("%s: cannot find lde neighbor",
266 case IMSG_LABEL_MAPPING
:
267 case IMSG_LABEL_REQUEST
:
268 case IMSG_LABEL_RELEASE
:
269 case IMSG_LABEL_WITHDRAW
:
270 case IMSG_LABEL_ABORT
:
271 if (imsg
.hdr
.len
- IMSG_HEADER_SIZE
!=
273 fatalx("lde_dispatch_imsg: wrong imsg len");
276 ln
= lde_nbr_find(imsg
.hdr
.peerid
);
278 log_debug("%s: cannot find lde neighbor",
283 switch (imsg
.hdr
.type
) {
284 case IMSG_LABEL_MAPPING
:
285 lde_check_mapping(map
, ln
);
287 case IMSG_LABEL_REQUEST
:
288 lde_check_request(map
, ln
);
290 case IMSG_LABEL_RELEASE
:
291 lde_check_release(map
, ln
);
293 case IMSG_LABEL_WITHDRAW
:
294 lde_check_withdraw(map
, ln
);
296 case IMSG_LABEL_ABORT
:
301 case IMSG_ADDRESS_ADD
:
302 if (imsg
.hdr
.len
- IMSG_HEADER_SIZE
!=
303 sizeof(struct lde_addr
))
304 fatalx("lde_dispatch_imsg: wrong imsg len");
305 lde_addr
= imsg
.data
;
307 ln
= lde_nbr_find(imsg
.hdr
.peerid
);
309 log_debug("%s: cannot find lde neighbor",
313 if (lde_address_add(ln
, lde_addr
) < 0) {
314 log_debug("%s: cannot add address %s, it "
315 "already exists", __func__
,
316 log_addr(lde_addr
->af
, &lde_addr
->addr
));
319 case IMSG_ADDRESS_DEL
:
320 if (imsg
.hdr
.len
- IMSG_HEADER_SIZE
!=
321 sizeof(struct lde_addr
))
322 fatalx("lde_dispatch_imsg: wrong imsg len");
323 lde_addr
= imsg
.data
;
325 ln
= lde_nbr_find(imsg
.hdr
.peerid
);
327 log_debug("%s: cannot find lde neighbor",
331 if (lde_address_del(ln
, lde_addr
) < 0) {
332 log_debug("%s: cannot delete address %s, it "
333 "does not exist", __func__
,
334 log_addr(lde_addr
->af
, &lde_addr
->addr
));
337 case IMSG_NOTIFICATION
:
338 if (imsg
.hdr
.len
- IMSG_HEADER_SIZE
!=
339 sizeof(struct notify_msg
))
340 fatalx("lde_dispatch_imsg: wrong imsg len");
343 ln
= lde_nbr_find(imsg
.hdr
.peerid
);
345 log_debug("%s: cannot find lde neighbor",
350 switch (nm
->status_code
) {
352 l2vpn_recv_pw_status(ln
, nm
);
356 * Do nothing for now. Should be useful in
357 * the future when we implement LDP-IGP
358 * Synchronization (RFC 5443) and Graceful
359 * Restart (RFC 3478).
365 case IMSG_NEIGHBOR_UP
:
366 if (imsg
.hdr
.len
- IMSG_HEADER_SIZE
!=
367 sizeof(struct lde_nbr
))
368 fatalx("lde_dispatch_imsg: wrong imsg len");
370 if (lde_nbr_find(imsg
.hdr
.peerid
))
371 fatalx("lde_dispatch_imsg: "
372 "neighbor already exists");
373 lde_nbr_new(imsg
.hdr
.peerid
, imsg
.data
);
375 case IMSG_NEIGHBOR_DOWN
:
376 lde_nbr_del(lde_nbr_find(imsg
.hdr
.peerid
));
378 case IMSG_CTL_SHOW_LIB
:
379 rt_dump(imsg
.hdr
.pid
);
381 lde_imsg_compose_ldpe(IMSG_CTL_END
, 0,
382 imsg
.hdr
.pid
, NULL
, 0);
384 case IMSG_CTL_SHOW_L2VPN_PW
:
385 l2vpn_pw_ctl(imsg
.hdr
.pid
);
387 lde_imsg_compose_ldpe(IMSG_CTL_END
, 0,
388 imsg
.hdr
.pid
, NULL
, 0);
390 case IMSG_CTL_SHOW_L2VPN_BINDING
:
391 l2vpn_binding_ctl(imsg
.hdr
.pid
);
393 lde_imsg_compose_ldpe(IMSG_CTL_END
, 0,
394 imsg
.hdr
.pid
, NULL
, 0);
397 log_debug("%s: unexpected imsg %d", __func__
,
406 /* this pipe is dead, so remove the event handlers and exit */
407 THREAD_READ_OFF(iev
->ev_read
);
408 THREAD_WRITE_OFF(iev
->ev_write
);
417 lde_dispatch_parent(struct thread
*thread
)
419 static struct ldpd_conf
*nconf
;
420 struct iface
*iface
, *niface
;
422 struct nbr_params
*nnbrp
;
423 static struct l2vpn
*l2vpn
, *nl2vpn
;
424 struct l2vpn_if
*lif
, *nlif
;
425 struct l2vpn_pw
*pw
, *npw
;
429 int fd
= THREAD_FD(thread
);
430 struct imsgev
*iev
= THREAD_ARG(thread
);
431 struct imsgbuf
*ibuf
= &iev
->ibuf
;
438 if ((n
= imsg_read(ibuf
)) == -1 && errno
!= EAGAIN
)
439 fatal("imsg_read error");
440 if (n
== 0) /* connection closed */
444 if ((n
= imsg_get(ibuf
, &imsg
)) == -1)
445 fatal("lde_dispatch_parent: imsg_get error");
449 switch (imsg
.hdr
.type
) {
451 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
453 fatalx("IFSTATUS imsg with wrong len");
456 iface
= if_lookup_name(ldeconf
, kif
->ifname
);
458 if_update_info(iface
, kif
);
462 RB_FOREACH(l2vpn
, l2vpn_head
, &ldeconf
->l2vpn_tree
) {
463 lif
= l2vpn_if_find(l2vpn
, kif
->ifname
);
465 l2vpn_if_update_info(lif
, kif
);
468 pw
= l2vpn_pw_find(l2vpn
, kif
->ifname
);
470 l2vpn_pw_update_info(pw
, kif
);
475 case IMSG_NETWORK_ADD
:
476 case IMSG_NETWORK_UPDATE
:
477 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
478 sizeof(struct kroute
)) {
479 log_warnx("%s: wrong imsg len", __func__
);
486 fec
.type
= FEC_TYPE_IPV4
;
487 fec
.u
.ipv4
.prefix
= kr
->prefix
.v4
;
488 fec
.u
.ipv4
.prefixlen
= kr
->prefixlen
;
491 fec
.type
= FEC_TYPE_IPV6
;
492 fec
.u
.ipv6
.prefix
= kr
->prefix
.v6
;
493 fec
.u
.ipv6
.prefixlen
= kr
->prefixlen
;
496 fatalx("lde_dispatch_parent: unknown af");
499 switch (imsg
.hdr
.type
) {
500 case IMSG_NETWORK_ADD
:
501 lde_kernel_insert(&fec
, kr
->af
, &kr
->nexthop
,
502 kr
->ifindex
, kr
->priority
,
503 kr
->flags
& F_CONNECTED
, NULL
);
505 case IMSG_NETWORK_UPDATE
:
506 lde_kernel_update(&fec
);
510 case IMSG_SOCKET_IPC
:
512 log_warnx("%s: received unexpected imsg fd "
513 "to ldpe", __func__
);
516 if ((fd
= imsg
.fd
) == -1) {
517 log_warnx("%s: expected to receive imsg fd to "
518 "ldpe but didn't receive any", __func__
);
522 if ((iev_ldpe
= malloc(sizeof(struct imsgev
))) == NULL
)
524 imsg_init(&iev_ldpe
->ibuf
, fd
);
525 iev_ldpe
->handler_read
= lde_dispatch_imsg
;
526 iev_ldpe
->ev_read
= NULL
;
527 thread_add_read(master
, iev_ldpe
->handler_read
, iev_ldpe
, iev_ldpe
->ibuf
.fd
,
529 iev_ldpe
->handler_write
= ldp_write_handler
;
530 iev_ldpe
->ev_write
= NULL
;
533 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
534 sizeof(struct ldpd_init
))
535 fatalx("INIT imsg with wrong len");
537 memcpy(&init
, imsg
.data
, sizeof(init
));
540 case IMSG_RECONF_CONF
:
541 if ((nconf
= malloc(sizeof(struct ldpd_conf
))) ==
544 memcpy(nconf
, imsg
.data
, sizeof(struct ldpd_conf
));
546 RB_INIT(iface_head
, &nconf
->iface_tree
);
547 RB_INIT(tnbr_head
, &nconf
->tnbr_tree
);
548 RB_INIT(nbrp_head
, &nconf
->nbrp_tree
);
549 RB_INIT(l2vpn_head
, &nconf
->l2vpn_tree
);
551 case IMSG_RECONF_IFACE
:
552 if ((niface
= malloc(sizeof(struct iface
))) == NULL
)
554 memcpy(niface
, imsg
.data
, sizeof(struct iface
));
556 RB_INSERT(iface_head
, &nconf
->iface_tree
, niface
);
558 case IMSG_RECONF_TNBR
:
559 if ((ntnbr
= malloc(sizeof(struct tnbr
))) == NULL
)
561 memcpy(ntnbr
, imsg
.data
, sizeof(struct tnbr
));
563 RB_INSERT(tnbr_head
, &nconf
->tnbr_tree
, ntnbr
);
565 case IMSG_RECONF_NBRP
:
566 if ((nnbrp
= malloc(sizeof(struct nbr_params
))) == NULL
)
568 memcpy(nnbrp
, imsg
.data
, sizeof(struct nbr_params
));
570 RB_INSERT(nbrp_head
, &nconf
->nbrp_tree
, nnbrp
);
572 case IMSG_RECONF_L2VPN
:
573 if ((nl2vpn
= malloc(sizeof(struct l2vpn
))) == NULL
)
575 memcpy(nl2vpn
, imsg
.data
, sizeof(struct l2vpn
));
577 RB_INIT(l2vpn_if_head
, &nl2vpn
->if_tree
);
578 RB_INIT(l2vpn_pw_head
, &nl2vpn
->pw_tree
);
579 RB_INIT(l2vpn_pw_head
, &nl2vpn
->pw_inactive_tree
);
581 RB_INSERT(l2vpn_head
, &nconf
->l2vpn_tree
, nl2vpn
);
583 case IMSG_RECONF_L2VPN_IF
:
584 if ((nlif
= malloc(sizeof(struct l2vpn_if
))) == NULL
)
586 memcpy(nlif
, imsg
.data
, sizeof(struct l2vpn_if
));
588 RB_INSERT(l2vpn_if_head
, &nl2vpn
->if_tree
, nlif
);
590 case IMSG_RECONF_L2VPN_PW
:
591 if ((npw
= malloc(sizeof(struct l2vpn_pw
))) == NULL
)
593 memcpy(npw
, imsg
.data
, sizeof(struct l2vpn_pw
));
595 RB_INSERT(l2vpn_pw_head
, &nl2vpn
->pw_tree
, npw
);
597 case IMSG_RECONF_L2VPN_IPW
:
598 if ((npw
= malloc(sizeof(struct l2vpn_pw
))) == NULL
)
600 memcpy(npw
, imsg
.data
, sizeof(struct l2vpn_pw
));
602 RB_INSERT(l2vpn_pw_head
, &nl2vpn
->pw_inactive_tree
, npw
);
604 case IMSG_RECONF_END
:
605 merge_config(ldeconf
, nconf
);
606 ldp_clear_config(nconf
);
609 case IMSG_DEBUG_UPDATE
:
610 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
612 log_warnx("%s: wrong imsg len", __func__
);
615 memcpy(&ldp_debug
, imsg
.data
, sizeof(ldp_debug
));
618 log_debug("%s: unexpected imsg %d", __func__
,
627 /* this pipe is dead, so remove the event handlers and exit */
628 THREAD_READ_OFF(iev
->ev_read
);
629 THREAD_WRITE_OFF(iev
->ev_write
);
637 lde_acl_check(char *acl_name
, int af
, union ldpd_addr
*addr
, uint8_t prefixlen
)
639 return ldp_acl_request(iev_main_sync
, acl_name
, af
, addr
, prefixlen
);
643 lde_update_label(struct fec_node
*fn
)
648 LIST_FOREACH(fnh
, &fn
->nexthops
, entry
) {
649 if (fnh
->flags
& F_FEC_NH_CONNECTED
) {
655 /* should we allocate a label for this fec? */
656 switch (fn
->fec
.type
) {
658 if ((ldeconf
->ipv4
.flags
& F_LDPD_AF_ALLOCHOSTONLY
) &&
659 fn
->fec
.u
.ipv4
.prefixlen
!= 32)
661 if (lde_acl_check(ldeconf
->ipv4
.acl_label_allocate_for
,
662 AF_INET
, (union ldpd_addr
*)&fn
->fec
.u
.ipv4
.prefix
,
663 fn
->fec
.u
.ipv4
.prefixlen
) != FILTER_PERMIT
)
667 if ((ldeconf
->ipv6
.flags
& F_LDPD_AF_ALLOCHOSTONLY
) &&
668 fn
->fec
.u
.ipv6
.prefixlen
!= 128)
670 if (lde_acl_check(ldeconf
->ipv6
.acl_label_allocate_for
,
671 AF_INET6
, (union ldpd_addr
*)&fn
->fec
.u
.ipv6
.prefix
,
672 fn
->fec
.u
.ipv6
.prefixlen
) != FILTER_PERMIT
)
680 /* choose implicit or explicit-null depending on configuration */
681 switch (fn
->fec
.type
) {
683 if (!(ldeconf
->ipv4
.flags
& F_LDPD_AF_EXPNULL
))
684 return (MPLS_LABEL_IMPLNULL
);
685 if (lde_acl_check(ldeconf
->ipv4
.acl_label_expnull_for
,
686 AF_INET
, (union ldpd_addr
*)&fn
->fec
.u
.ipv4
.prefix
,
687 fn
->fec
.u
.ipv4
.prefixlen
) != FILTER_PERMIT
)
688 return (MPLS_LABEL_IMPLNULL
);
689 return (MPLS_LABEL_IPV4NULL
);
691 if (!(ldeconf
->ipv6
.flags
& F_LDPD_AF_EXPNULL
))
692 return (MPLS_LABEL_IMPLNULL
);
693 if (lde_acl_check(ldeconf
->ipv6
.acl_label_expnull_for
,
694 AF_INET6
, (union ldpd_addr
*)&fn
->fec
.u
.ipv6
.prefix
,
695 fn
->fec
.u
.ipv6
.prefixlen
) != FILTER_PERMIT
)
696 return (MPLS_LABEL_IMPLNULL
);
697 return (MPLS_LABEL_IPV6NULL
);
699 fatalx("lde_update_label: unexpected fec type");
704 /* preserve current label if there's no need to update it */
705 if (fn
->local_label
!= NO_LABEL
&&
706 fn
->local_label
> MPLS_LABEL_RESERVED_MAX
)
707 return (fn
->local_label
);
709 return (lde_get_next_label());
713 lde_send_change_klabel(struct fec_node
*fn
, struct fec_nh
*fnh
)
719 switch (fn
->fec
.type
) {
721 memset(&kr
, 0, sizeof(kr
));
723 kr
.prefix
.v4
= fn
->fec
.u
.ipv4
.prefix
;
724 kr
.prefixlen
= fn
->fec
.u
.ipv4
.prefixlen
;
725 kr
.nexthop
.v4
= fnh
->nexthop
.v4
;
726 kr
.ifindex
= fnh
->ifindex
;
727 kr
.local_label
= fn
->local_label
;
728 kr
.remote_label
= fnh
->remote_label
;
729 kr
.priority
= fnh
->priority
;
731 lde_imsg_compose_parent(IMSG_KLABEL_CHANGE
, 0, &kr
,
735 memset(&kr
, 0, sizeof(kr
));
737 kr
.prefix
.v6
= fn
->fec
.u
.ipv6
.prefix
;
738 kr
.prefixlen
= fn
->fec
.u
.ipv6
.prefixlen
;
739 kr
.nexthop
.v6
= fnh
->nexthop
.v6
;
740 kr
.ifindex
= fnh
->ifindex
;
741 kr
.local_label
= fn
->local_label
;
742 kr
.remote_label
= fnh
->remote_label
;
743 kr
.priority
= fnh
->priority
;
745 lde_imsg_compose_parent(IMSG_KLABEL_CHANGE
, 0, &kr
,
749 if (fn
->local_label
== NO_LABEL
||
750 fnh
->remote_label
== NO_LABEL
)
753 pw
= (struct l2vpn_pw
*) fn
->data
;
754 pw
->flags
|= F_PW_STATUS_UP
;
756 memset(&kpw
, 0, sizeof(kpw
));
757 kpw
.ifindex
= pw
->ifindex
;
758 kpw
.pw_type
= fn
->fec
.u
.pwid
.type
;
760 kpw
.nexthop
= pw
->addr
;
761 kpw
.local_label
= fn
->local_label
;
762 kpw
.remote_label
= fnh
->remote_label
;
763 kpw
.flags
= pw
->flags
;
765 lde_imsg_compose_parent(IMSG_KPWLABEL_CHANGE
, 0, &kpw
,
772 lde_send_delete_klabel(struct fec_node
*fn
, struct fec_nh
*fnh
)
778 switch (fn
->fec
.type
) {
780 memset(&kr
, 0, sizeof(kr
));
782 kr
.prefix
.v4
= fn
->fec
.u
.ipv4
.prefix
;
783 kr
.prefixlen
= fn
->fec
.u
.ipv4
.prefixlen
;
784 kr
.nexthop
.v4
= fnh
->nexthop
.v4
;
785 kr
.ifindex
= fnh
->ifindex
;
786 kr
.local_label
= fn
->local_label
;
787 kr
.remote_label
= fnh
->remote_label
;
788 kr
.priority
= fnh
->priority
;
790 lde_imsg_compose_parent(IMSG_KLABEL_DELETE
, 0, &kr
,
794 memset(&kr
, 0, sizeof(kr
));
796 kr
.prefix
.v6
= fn
->fec
.u
.ipv6
.prefix
;
797 kr
.prefixlen
= fn
->fec
.u
.ipv6
.prefixlen
;
798 kr
.nexthop
.v6
= fnh
->nexthop
.v6
;
799 kr
.ifindex
= fnh
->ifindex
;
800 kr
.local_label
= fn
->local_label
;
801 kr
.remote_label
= fnh
->remote_label
;
802 kr
.priority
= fnh
->priority
;
804 lde_imsg_compose_parent(IMSG_KLABEL_DELETE
, 0, &kr
,
808 pw
= (struct l2vpn_pw
*) fn
->data
;
809 if (!(pw
->flags
& F_PW_STATUS_UP
))
811 pw
->flags
&= ~F_PW_STATUS_UP
;
813 memset(&kpw
, 0, sizeof(kpw
));
814 kpw
.ifindex
= pw
->ifindex
;
815 kpw
.pw_type
= fn
->fec
.u
.pwid
.type
;
817 kpw
.nexthop
= pw
->addr
;
818 kpw
.local_label
= fn
->local_label
;
819 kpw
.remote_label
= fnh
->remote_label
;
820 kpw
.flags
= pw
->flags
;
822 lde_imsg_compose_parent(IMSG_KPWLABEL_DELETE
, 0, &kpw
,
829 lde_fec2map(struct fec
*fec
, struct map
*map
)
831 memset(map
, 0, sizeof(*map
));
835 map
->type
= MAP_TYPE_PREFIX
;
836 map
->fec
.prefix
.af
= AF_INET
;
837 map
->fec
.prefix
.prefix
.v4
= fec
->u
.ipv4
.prefix
;
838 map
->fec
.prefix
.prefixlen
= fec
->u
.ipv4
.prefixlen
;
841 map
->type
= MAP_TYPE_PREFIX
;
842 map
->fec
.prefix
.af
= AF_INET6
;
843 map
->fec
.prefix
.prefix
.v6
= fec
->u
.ipv6
.prefix
;
844 map
->fec
.prefix
.prefixlen
= fec
->u
.ipv6
.prefixlen
;
847 map
->type
= MAP_TYPE_PWID
;
848 map
->fec
.pwid
.type
= fec
->u
.pwid
.type
;
849 map
->fec
.pwid
.group_id
= 0;
850 map
->flags
|= F_MAP_PW_ID
;
851 map
->fec
.pwid
.pwid
= fec
->u
.pwid
.pwid
;
857 lde_map2fec(struct map
*map
, struct in_addr lsr_id
, struct fec
*fec
)
859 memset(fec
, 0, sizeof(*fec
));
862 case MAP_TYPE_PREFIX
:
863 switch (map
->fec
.prefix
.af
) {
865 fec
->type
= FEC_TYPE_IPV4
;
866 fec
->u
.ipv4
.prefix
= map
->fec
.prefix
.prefix
.v4
;
867 fec
->u
.ipv4
.prefixlen
= map
->fec
.prefix
.prefixlen
;
870 fec
->type
= FEC_TYPE_IPV6
;
871 fec
->u
.ipv6
.prefix
= map
->fec
.prefix
.prefix
.v6
;
872 fec
->u
.ipv6
.prefixlen
= map
->fec
.prefix
.prefixlen
;
875 fatalx("lde_map2fec: unknown af");
880 fec
->type
= FEC_TYPE_PWID
;
881 fec
->u
.pwid
.type
= map
->fec
.pwid
.type
;
882 fec
->u
.pwid
.pwid
= map
->fec
.pwid
.pwid
;
883 fec
->u
.pwid
.lsr_id
= lsr_id
;
889 lde_send_labelmapping(struct lde_nbr
*ln
, struct fec_node
*fn
, int single
)
891 struct lde_wdraw
*lw
;
898 * We shouldn't send a new label mapping if we have a pending
899 * label release to receive. In this case, schedule to send a
900 * label mapping as soon as a label release is received.
902 lw
= (struct lde_wdraw
*)fec_find(&ln
->sent_wdraw
, &fn
->fec
);
904 if (!fec_find(&ln
->sent_map_pending
, &fn
->fec
))
905 lde_map_pending_add(ln
, fn
);
910 * This function skips SL.1 - 3 and SL.9 - 14 because the label
911 * allocation is done way earlier (because of the merging nature of
915 lde_fec2map(&fn
->fec
, &map
);
916 switch (fn
->fec
.type
) {
920 if (lde_acl_check(ldeconf
->ipv4
.acl_label_advertise_to
,
921 AF_INET
, (union ldpd_addr
*)&ln
->id
, 32) != FILTER_PERMIT
)
923 if (lde_acl_check(ldeconf
->ipv4
.acl_label_advertise_for
,
924 AF_INET
, (union ldpd_addr
*)&fn
->fec
.u
.ipv4
.prefix
,
925 fn
->fec
.u
.ipv4
.prefixlen
) != FILTER_PERMIT
)
931 if (lde_acl_check(ldeconf
->ipv6
.acl_label_advertise_to
,
932 AF_INET
, (union ldpd_addr
*)&ln
->id
, 32) != FILTER_PERMIT
)
934 if (lde_acl_check(ldeconf
->ipv6
.acl_label_advertise_for
,
935 AF_INET6
, (union ldpd_addr
*)&fn
->fec
.u
.ipv6
.prefix
,
936 fn
->fec
.u
.ipv6
.prefixlen
) != FILTER_PERMIT
)
940 pw
= (struct l2vpn_pw
*) fn
->data
;
941 if (pw
== NULL
|| pw
->lsr_id
.s_addr
!= ln
->id
.s_addr
)
942 /* not the remote end of the pseudowire */
945 map
.flags
|= F_MAP_PW_IFMTU
;
946 map
.fec
.pwid
.ifmtu
= pw
->l2vpn
->mtu
;
947 if (pw
->flags
& F_PW_CWORD
)
948 map
.flags
|= F_MAP_PW_CWORD
;
949 if (pw
->flags
& F_PW_STATUSTLV
) {
950 map
.flags
|= F_MAP_PW_STATUS
;
951 /* VPLS are always up */
952 map
.pw_status
= PW_FORWARDING
;
956 map
.label
= fn
->local_label
;
958 /* SL.6: is there a pending request for this mapping? */
959 lre
= (struct lde_req
*)fec_find(&ln
->recv_req
, &fn
->fec
);
961 /* set label request msg id in the mapping response. */
962 map
.requestid
= lre
->msg_id
;
963 map
.flags
= F_MAP_REQ_ID
;
965 /* SL.7: delete record of pending request */
966 lde_req_del(ln
, lre
, 0);
969 /* SL.4: send label mapping */
970 lde_imsg_compose_ldpe(IMSG_MAPPING_ADD
, ln
->peerid
, 0,
973 lde_imsg_compose_ldpe(IMSG_MAPPING_ADD_END
, ln
->peerid
, 0,
976 /* SL.5: record sent label mapping */
977 me
= (struct lde_map
*)fec_find(&ln
->sent_map
, &fn
->fec
);
979 me
= lde_map_add(ln
, fn
, 1);
984 lde_send_labelwithdraw(struct lde_nbr
*ln
, struct fec_node
*fn
,
985 struct map
*wcard
, struct status_tlv
*st
)
987 struct lde_wdraw
*lw
;
993 lde_fec2map(&fn
->fec
, &map
);
994 switch (fn
->fec
.type
) {
1000 if (!ln
->v6_enabled
)
1004 pw
= (struct l2vpn_pw
*) fn
->data
;
1005 if (pw
== NULL
|| pw
->lsr_id
.s_addr
!= ln
->id
.s_addr
)
1006 /* not the remote end of the pseudowire */
1009 if (pw
->flags
& F_PW_CWORD
)
1010 map
.flags
|= F_MAP_PW_CWORD
;
1013 map
.label
= fn
->local_label
;
1015 memcpy(&map
, wcard
, sizeof(map
));
1018 map
.st
.status_code
= st
->status_code
;
1019 map
.st
.msg_id
= st
->msg_id
;
1020 map
.st
.msg_type
= st
->msg_type
;
1021 map
.flags
|= F_MAP_STATUS
;
1024 /* SWd.1: send label withdraw. */
1025 lde_imsg_compose_ldpe(IMSG_WITHDRAW_ADD
, ln
->peerid
, 0,
1027 lde_imsg_compose_ldpe(IMSG_WITHDRAW_ADD_END
, ln
->peerid
, 0, NULL
, 0);
1029 /* SWd.2: record label withdraw. */
1031 lw
= (struct lde_wdraw
*)fec_find(&ln
->sent_wdraw
, &fn
->fec
);
1033 lw
= lde_wdraw_add(ln
, fn
);
1034 lw
->label
= map
.label
;
1038 RB_FOREACH(f
, fec_tree
, &ft
) {
1039 fn
= (struct fec_node
*)f
;
1040 me
= (struct lde_map
*)fec_find(&ln
->sent_map
, &fn
->fec
);
1041 if (lde_wildcard_apply(wcard
, &fn
->fec
, me
) == 0)
1044 lw
= (struct lde_wdraw
*)fec_find(&ln
->sent_wdraw
,
1047 lw
= lde_wdraw_add(ln
, fn
);
1048 lw
->label
= map
.label
;
1054 lde_send_labelwithdraw_wcard(struct lde_nbr
*ln
, uint32_t label
)
1058 memset(&wcard
, 0, sizeof(wcard
));
1059 wcard
.type
= MAP_TYPE_WILDCARD
;
1060 wcard
.label
= label
;
1061 lde_send_labelwithdraw(ln
, NULL
, &wcard
, NULL
);
1065 lde_send_labelwithdraw_twcard_prefix(struct lde_nbr
*ln
, uint16_t af
,
1070 memset(&wcard
, 0, sizeof(wcard
));
1071 wcard
.type
= MAP_TYPE_TYPED_WCARD
;
1072 wcard
.fec
.twcard
.type
= MAP_TYPE_PREFIX
;
1073 wcard
.fec
.twcard
.u
.prefix_af
= af
;
1074 wcard
.label
= label
;
1075 lde_send_labelwithdraw(ln
, NULL
, &wcard
, NULL
);
1079 lde_send_labelwithdraw_twcard_pwid(struct lde_nbr
*ln
, uint16_t pw_type
,
1084 memset(&wcard
, 0, sizeof(wcard
));
1085 wcard
.type
= MAP_TYPE_TYPED_WCARD
;
1086 wcard
.fec
.twcard
.type
= MAP_TYPE_PWID
;
1087 wcard
.fec
.twcard
.u
.pw_type
= pw_type
;
1088 wcard
.label
= label
;
1089 lde_send_labelwithdraw(ln
, NULL
, &wcard
, NULL
);
1093 lde_send_labelwithdraw_pwid_wcard(struct lde_nbr
*ln
, uint16_t pw_type
,
1098 memset(&wcard
, 0, sizeof(wcard
));
1099 wcard
.type
= MAP_TYPE_PWID
;
1100 wcard
.fec
.pwid
.type
= pw_type
;
1101 wcard
.fec
.pwid
.group_id
= group_id
;
1102 /* we can not append a Label TLV when using PWid group wildcards. */
1103 wcard
.label
= NO_LABEL
;
1104 lde_send_labelwithdraw(ln
, NULL
, &wcard
, NULL
);
1108 lde_send_labelrelease(struct lde_nbr
*ln
, struct fec_node
*fn
,
1109 struct map
*wcard
, uint32_t label
)
1112 struct l2vpn_pw
*pw
;
1115 lde_fec2map(&fn
->fec
, &map
);
1116 switch (fn
->fec
.type
) {
1118 if (!ln
->v4_enabled
)
1122 if (!ln
->v6_enabled
)
1126 pw
= (struct l2vpn_pw
*) fn
->data
;
1127 if (pw
== NULL
|| pw
->lsr_id
.s_addr
!= ln
->id
.s_addr
)
1128 /* not the remote end of the pseudowire */
1131 if (pw
->flags
& F_PW_CWORD
)
1132 map
.flags
|= F_MAP_PW_CWORD
;
1136 memcpy(&map
, wcard
, sizeof(map
));
1139 lde_imsg_compose_ldpe(IMSG_RELEASE_ADD
, ln
->peerid
, 0,
1141 lde_imsg_compose_ldpe(IMSG_RELEASE_ADD_END
, ln
->peerid
, 0, NULL
, 0);
1145 lde_send_notification(struct lde_nbr
*ln
, uint32_t status_code
, uint32_t msg_id
,
1148 struct notify_msg nm
;
1150 memset(&nm
, 0, sizeof(nm
));
1151 nm
.status_code
= status_code
;
1152 /* 'msg_id' and 'msg_type' should be in network byte order */
1154 nm
.msg_type
= msg_type
;
1156 lde_imsg_compose_ldpe(IMSG_NOTIFICATION_SEND
, ln
->peerid
, 0,
1161 lde_send_notification_eol_prefix(struct lde_nbr
*ln
, int af
)
1163 struct notify_msg nm
;
1165 memset(&nm
, 0, sizeof(nm
));
1166 nm
.status_code
= S_ENDOFLIB
;
1167 nm
.fec
.type
= MAP_TYPE_TYPED_WCARD
;
1168 nm
.fec
.fec
.twcard
.type
= MAP_TYPE_PREFIX
;
1169 nm
.fec
.fec
.twcard
.u
.prefix_af
= af
;
1170 nm
.flags
|= F_NOTIF_FEC
;
1172 lde_imsg_compose_ldpe(IMSG_NOTIFICATION_SEND
, ln
->peerid
, 0,
1177 lde_send_notification_eol_pwid(struct lde_nbr
*ln
, uint16_t pw_type
)
1179 struct notify_msg nm
;
1181 memset(&nm
, 0, sizeof(nm
));
1182 nm
.status_code
= S_ENDOFLIB
;
1183 nm
.fec
.type
= MAP_TYPE_TYPED_WCARD
;
1184 nm
.fec
.fec
.twcard
.type
= MAP_TYPE_PWID
;
1185 nm
.fec
.fec
.twcard
.u
.pw_type
= pw_type
;
1186 nm
.flags
|= F_NOTIF_FEC
;
1188 lde_imsg_compose_ldpe(IMSG_NOTIFICATION_SEND
, ln
->peerid
, 0,
1193 lde_nbr_compare(const struct lde_nbr
*a
, const struct lde_nbr
*b
)
1195 return (a
->peerid
- b
->peerid
);
1198 static struct lde_nbr
*
1199 lde_nbr_new(uint32_t peerid
, struct lde_nbr
*new)
1203 if ((ln
= calloc(1, sizeof(*ln
))) == NULL
)
1207 ln
->v4_enabled
= new->v4_enabled
;
1208 ln
->v6_enabled
= new->v6_enabled
;
1209 ln
->flags
= new->flags
;
1210 ln
->peerid
= peerid
;
1211 fec_init(&ln
->recv_map
);
1212 fec_init(&ln
->sent_map
);
1213 fec_init(&ln
->sent_map_pending
);
1214 fec_init(&ln
->recv_req
);
1215 fec_init(&ln
->sent_req
);
1216 fec_init(&ln
->sent_wdraw
);
1218 TAILQ_INIT(&ln
->addr_list
);
1220 if (RB_INSERT(nbr_tree
, &lde_nbrs
, ln
) != NULL
)
1221 fatalx("lde_nbr_new: RB_INSERT failed");
1227 lde_nbr_del(struct lde_nbr
*ln
)
1230 struct fec_node
*fn
;
1232 struct l2vpn_pw
*pw
;
1237 /* uninstall received mappings */
1238 RB_FOREACH(f
, fec_tree
, &ft
) {
1239 fn
= (struct fec_node
*)f
;
1241 LIST_FOREACH(fnh
, &fn
->nexthops
, entry
) {
1245 if (!lde_address_find(ln
, fnh
->af
,
1250 if (f
->u
.pwid
.lsr_id
.s_addr
!= ln
->id
.s_addr
)
1252 pw
= (struct l2vpn_pw
*) fn
->data
;
1260 lde_send_delete_klabel(fn
, fnh
);
1261 fnh
->remote_label
= NO_LABEL
;
1265 lde_address_list_free(ln
);
1267 fec_clear(&ln
->recv_map
, lde_map_free
);
1268 fec_clear(&ln
->sent_map
, lde_map_free
);
1269 fec_clear(&ln
->sent_map_pending
, free
);
1270 fec_clear(&ln
->recv_req
, free
);
1271 fec_clear(&ln
->sent_req
, free
);
1272 fec_clear(&ln
->sent_wdraw
, free
);
1274 RB_REMOVE(nbr_tree
, &lde_nbrs
, ln
);
1279 static struct lde_nbr
*
1280 lde_nbr_find(uint32_t peerid
)
1286 return (RB_FIND(nbr_tree
, &lde_nbrs
, &ln
));
1290 lde_nbr_find_by_lsrid(struct in_addr addr
)
1294 RB_FOREACH(ln
, nbr_tree
, &lde_nbrs
)
1295 if (ln
->id
.s_addr
== addr
.s_addr
)
1302 lde_nbr_find_by_addr(int af
, union ldpd_addr
*addr
)
1306 RB_FOREACH(ln
, nbr_tree
, &lde_nbrs
)
1307 if (lde_address_find(ln
, af
, addr
) != NULL
)
1318 while ((ln
= RB_ROOT(nbr_tree
, &lde_nbrs
)) != NULL
)
1323 lde_nbr_addr_update(struct lde_nbr
*ln
, struct lde_addr
*lde_addr
, int removed
)
1326 struct fec_node
*fn
;
1330 RB_FOREACH(fec
, fec_tree
, &ln
->recv_map
) {
1331 fn
= (struct fec_node
*)fec_find(&ft
, fec
);
1332 switch (fec
->type
) {
1334 if (lde_addr
->af
!= AF_INET
)
1338 if (lde_addr
->af
!= AF_INET6
)
1345 LIST_FOREACH(fnh
, &fn
->nexthops
, entry
) {
1346 if (ldp_addrcmp(fnh
->af
, &fnh
->nexthop
,
1351 lde_send_delete_klabel(fn
, fnh
);
1352 fnh
->remote_label
= NO_LABEL
;
1354 me
= (struct lde_map
*)fec
;
1355 fnh
->remote_label
= me
->map
.label
;
1356 lde_send_change_klabel(fn
, fnh
);
1364 lde_map_compare(const struct lde_map
*a
, const struct lde_map
*b
)
1366 return (ldp_addrcmp(AF_INET
, (union ldpd_addr
*)&a
->nexthop
->id
,
1367 (union ldpd_addr
*)&b
->nexthop
->id
));
1371 lde_map_add(struct lde_nbr
*ln
, struct fec_node
*fn
, int sent
)
1375 me
= calloc(1, sizeof(*me
));
1383 RB_INSERT(lde_map_head
, &fn
->upstream
, me
);
1384 me
->head
= &fn
->upstream
;
1385 if (fec_insert(&ln
->sent_map
, &me
->fec
))
1386 log_warnx("failed to add %s to sent map",
1388 /* XXX on failure more cleanup is needed */
1390 RB_INSERT(lde_map_head
, &fn
->downstream
, me
);
1391 me
->head
= &fn
->downstream
;
1392 if (fec_insert(&ln
->recv_map
, &me
->fec
))
1393 log_warnx("failed to add %s to recv map",
1401 lde_map_del(struct lde_nbr
*ln
, struct lde_map
*me
, int sent
)
1404 fec_remove(&ln
->sent_map
, &me
->fec
);
1406 fec_remove(&ln
->recv_map
, &me
->fec
);
1412 lde_map_free(void *ptr
)
1414 struct lde_map
*map
= ptr
;
1416 RB_REMOVE(lde_map_head
, map
->head
, map
);
1421 lde_map_pending_add(struct lde_nbr
*ln
, struct fec_node
*fn
)
1425 map
= calloc(1, sizeof(*map
));
1430 if (fec_insert(&ln
->sent_map_pending
, map
))
1431 log_warnx("failed to add %s to sent map (pending)",
1438 lde_map_pending_del(struct lde_nbr
*ln
, struct fec
*map
)
1440 fec_remove(&ln
->sent_map_pending
, map
);
1445 lde_req_add(struct lde_nbr
*ln
, struct fec
*fec
, int sent
)
1448 struct lde_req
*lre
;
1450 t
= sent
? &ln
->sent_req
: &ln
->recv_req
;
1452 lre
= calloc(1, sizeof(*lre
));
1456 if (fec_insert(t
, &lre
->fec
)) {
1457 log_warnx("failed to add %s to %s req",
1458 log_fec(&lre
->fec
), sent
? "sent" : "recv");
1468 lde_req_del(struct lde_nbr
*ln
, struct lde_req
*lre
, int sent
)
1471 fec_remove(&ln
->sent_req
, &lre
->fec
);
1473 fec_remove(&ln
->recv_req
, &lre
->fec
);
1479 lde_wdraw_add(struct lde_nbr
*ln
, struct fec_node
*fn
)
1481 struct lde_wdraw
*lw
;
1483 lw
= calloc(1, sizeof(*lw
));
1489 if (fec_insert(&ln
->sent_wdraw
, &lw
->fec
))
1490 log_warnx("failed to add %s to sent wdraw",
1497 lde_wdraw_del(struct lde_nbr
*ln
, struct lde_wdraw
*lw
)
1499 fec_remove(&ln
->sent_wdraw
, &lw
->fec
);
1504 lde_change_egress_label(int af
)
1508 struct fec_node
*fn
;
1510 /* explicitly withdraw all null labels */
1511 RB_FOREACH(ln
, nbr_tree
, &lde_nbrs
) {
1512 lde_send_labelwithdraw_wcard(ln
, MPLS_LABEL_IMPLNULL
);
1514 lde_send_labelwithdraw_wcard(ln
, MPLS_LABEL_IPV4NULL
);
1516 lde_send_labelwithdraw_wcard(ln
, MPLS_LABEL_IPV6NULL
);
1519 /* update label of connected routes */
1520 RB_FOREACH(f
, fec_tree
, &ft
) {
1521 fn
= (struct fec_node
*)f
;
1522 if (fn
->local_label
> MPLS_LABEL_RESERVED_MAX
)
1527 if (fn
->fec
.type
!= FEC_TYPE_IPV4
)
1531 if (fn
->fec
.type
!= FEC_TYPE_IPV6
)
1535 fatalx("lde_change_egress_label: unknown af");
1538 fn
->local_label
= lde_update_label(fn
);
1539 if (fn
->local_label
!= NO_LABEL
)
1540 RB_FOREACH(ln
, nbr_tree
, &lde_nbrs
)
1541 lde_send_labelmapping(ln
, fn
, 0);
1543 RB_FOREACH(ln
, nbr_tree
, &lde_nbrs
)
1544 lde_imsg_compose_ldpe(IMSG_MAPPING_ADD_END
, ln
->peerid
, 0,
1549 lde_address_add(struct lde_nbr
*ln
, struct lde_addr
*lde_addr
)
1551 struct lde_addr
*new;
1553 if (lde_address_find(ln
, lde_addr
->af
, &lde_addr
->addr
) != NULL
)
1556 if ((new = calloc(1, sizeof(*new))) == NULL
)
1559 new->af
= lde_addr
->af
;
1560 new->addr
= lde_addr
->addr
;
1561 TAILQ_INSERT_TAIL(&ln
->addr_list
, new, entry
);
1563 /* reevaluate the previously received mappings from this neighbor */
1564 lde_nbr_addr_update(ln
, lde_addr
, 0);
1570 lde_address_del(struct lde_nbr
*ln
, struct lde_addr
*lde_addr
)
1572 lde_addr
= lde_address_find(ln
, lde_addr
->af
, &lde_addr
->addr
);
1573 if (lde_addr
== NULL
)
1576 /* reevaluate the previously received mappings from this neighbor */
1577 lde_nbr_addr_update(ln
, lde_addr
, 1);
1579 TAILQ_REMOVE(&ln
->addr_list
, lde_addr
, entry
);
1586 lde_address_find(struct lde_nbr
*ln
, int af
, union ldpd_addr
*addr
)
1588 struct lde_addr
*lde_addr
;
1590 TAILQ_FOREACH(lde_addr
, &ln
->addr_list
, entry
)
1591 if (lde_addr
->af
== af
&&
1592 ldp_addrcmp(af
, &lde_addr
->addr
, addr
) == 0)
1599 lde_address_list_free(struct lde_nbr
*ln
)
1601 struct lde_addr
*lde_addr
;
1603 while ((lde_addr
= TAILQ_FIRST(&ln
->addr_list
)) != NULL
) {
1604 TAILQ_REMOVE(&ln
->addr_list
, lde_addr
, entry
);
1610 zclient_sync_init(u_short instance
)
1612 /* Initialize special zclient for synchronous message exchanges. */
1613 log_debug("Initializing synchronous zclient for label manager");
1614 zclient_sync
= zclient_new(master
);
1615 zclient_sync
->sock
= -1;
1616 zclient_sync
->redist_default
= ZEBRA_ROUTE_LDP
;
1617 zclient_sync
->instance
= instance
;
1618 while (zclient_socket_connect(zclient_sync
) < 0) {
1619 log_warnx("Error connecting synchronous zclient!");
1622 /* make socket non-blocking */
1623 sock_set_nonblock(zclient_sync
->sock
);
1625 /* Connect to label manager */
1626 while (lm_label_manager_connect(zclient_sync
) != 0) {
1627 log_warnx("Error connecting to label manager!");
1633 lde_del_label_chunk(void *val
)
1639 lde_get_label_chunk(void)
1642 uint32_t start
, end
;
1644 log_debug("Getting label chunk");
1645 ret
= lm_get_label_chunk(zclient_sync
, 0, CHUNK_SIZE
, &start
, &end
);
1647 log_warnx("Error getting label chunk!");
1651 on_get_label_chunk_response(start
, end
);
1657 lde_label_list_init(void)
1659 label_chunk_list
= list_new();
1660 label_chunk_list
->del
= lde_del_label_chunk
;
1662 /* get first chunk */
1663 while (lde_get_label_chunk () != 0) {
1664 log_warnx("Error getting first label chunk!");
1670 on_get_label_chunk_response(uint32_t start
, uint32_t end
)
1672 struct label_chunk
*new_label_chunk
;
1674 log_debug("Label Chunk assign: %u - %u", start
, end
);
1676 new_label_chunk
= calloc(1, sizeof(struct label_chunk
));
1677 if (!new_label_chunk
) {
1678 log_warn("Error trying to allocate label chunk %u - %u", start
, end
);
1682 new_label_chunk
->start
= start
;
1683 new_label_chunk
->end
= end
;
1684 new_label_chunk
->used_mask
= 0;
1686 listnode_add(label_chunk_list
, (void *)new_label_chunk
);
1688 /* let's update current if needed */
1689 if (!current_label_chunk
)
1690 current_label_chunk
= listtail(label_chunk_list
);
1694 lde_get_next_label(void)
1696 struct label_chunk
*label_chunk
;
1697 uint32_t i
, pos
, size
;
1698 uint32_t label
= NO_LABEL
;
1700 while (current_label_chunk
) {
1701 label_chunk
= listgetdata(current_label_chunk
);
1705 /* try to get next free label in currently used label chunk */
1706 size
= label_chunk
->end
- label_chunk
->start
+ 1;
1707 for (i
= 0, pos
= 1; i
< size
; i
++, pos
<<= 1) {
1708 if (!(pos
& label_chunk
->used_mask
)) {
1709 label_chunk
->used_mask
|= pos
;
1710 label
= label_chunk
->start
+ i
;
1714 current_label_chunk
= listnextnode(current_label_chunk
);
1718 /* we moved till the last chunk, or were not able to find a label,
1719 so let's ask for another one */
1720 if (!current_label_chunk
||
1721 current_label_chunk
== listtail(label_chunk_list
) ||
1722 label
== NO_LABEL
) {
1723 if (lde_get_label_chunk() != 0)
1724 log_warn("%s: Error getting label chunk!", __func__
);