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(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(struct lde_map
*, struct lde_map
*);
53 static void lde_map_free(void *);
54 static int lde_address_add(struct lde_nbr
*, struct lde_addr
*);
55 static int lde_address_del(struct lde_nbr
*, struct lde_addr
*);
56 static void lde_address_list_free(struct lde_nbr
*);
57 static void zclient_sync_init(u_short instance
);
58 static void lde_label_list_init(void);
59 static int lde_get_label_chunk(void);
60 static void on_get_label_chunk_response(uint32_t start
, uint32_t end
);
61 static uint32_t lde_get_next_label(void);
63 RB_GENERATE(nbr_tree
, lde_nbr
, entry
, lde_nbr_compare
)
64 RB_GENERATE(lde_map_head
, lde_map
, entry
, lde_map_compare
)
66 struct ldpd_conf
*ldeconf
;
67 struct nbr_tree lde_nbrs
= RB_INITIALIZER(&lde_nbrs
);
69 static struct imsgev
*iev_ldpe
;
70 static struct imsgev
*iev_main
, *iev_main_sync
;
72 /* Master of threads. */
73 struct thread_master
*master
;
76 static zebra_capabilities_t _caps_p
[] =
81 static struct zebra_privs_t lde_privs
=
83 #if defined(VTY_GROUP)
84 .vty_group
= VTY_GROUP
,
87 .cap_num_p
= array_size(_caps_p
),
91 /* List of chunks of labels externally assigned by Zebra */
92 static struct list
*label_chunk_list
;
93 static struct listnode
*current_label_chunk
;
95 /* Synchronous zclient to request labels */
96 static struct zclient
*zclient_sync
;
98 /* SIGINT / SIGTERM handler. */
105 static struct quagga_signal_t lde_signals
[] =
121 /* label decision engine */
125 struct thread thread
;
127 #ifdef HAVE_SETPROCTITLE
128 setproctitle("label decision engine");
130 ldpd_process
= PROC_LDE_ENGINE
;
131 log_procname
= log_procnames
[PROC_LDE_ENGINE
];
133 master
= thread_master_create();
135 /* setup signal handler */
136 signal_init(master
, array_size(lde_signals
), lde_signals
);
138 /* setup pipes and event handlers to the parent process */
139 if ((iev_main
= calloc(1, sizeof(struct imsgev
))) == NULL
)
141 imsg_init(&iev_main
->ibuf
, LDPD_FD_ASYNC
);
142 iev_main
->handler_read
= lde_dispatch_parent
;
143 iev_main
->ev_read
= thread_add_read(master
, iev_main
->handler_read
,
144 iev_main
, iev_main
->ibuf
.fd
);
145 iev_main
->handler_write
= ldp_write_handler
;
147 if ((iev_main_sync
= calloc(1, sizeof(struct imsgev
))) == NULL
)
149 imsg_init(&iev_main_sync
->ibuf
, LDPD_FD_SYNC
);
151 /* create base configuration */
152 ldeconf
= config_new_empty();
154 /* Fetch next active thread. */
155 while (thread_fetch(master
, &thread
))
156 thread_call(&thread
);
160 lde_init(struct ldpd_init
*init
)
162 /* drop privileges */
163 lde_privs
.user
= init
->user
;
164 lde_privs
.group
= init
->group
;
165 zprivs_init(&lde_privs
);
168 if (pledge("stdio recvfd unix", NULL
) == -1)
172 /* start the LIB garbage collector */
173 lde_gc_start_timer();
175 /* Init synchronous zclient and label list */
176 zclient_serv_path_set(init
->zclient_serv_path
);
177 zclient_sync_init(init
->instance
);
178 lde_label_list_init();
186 msgbuf_clear(&iev_ldpe
->ibuf
.w
);
187 close(iev_ldpe
->ibuf
.fd
);
189 msgbuf_clear(&iev_main
->ibuf
.w
);
190 close(iev_main
->ibuf
.fd
);
191 msgbuf_clear(&iev_main_sync
->ibuf
.w
);
192 close(iev_main_sync
->ibuf
.fd
);
198 config_clear(ldeconf
);
205 log_info("label decision engine exiting");
211 lde_imsg_compose_parent(int type
, pid_t pid
, void *data
, uint16_t datalen
)
213 return (imsg_compose_event(iev_main
, type
, 0, pid
, -1, data
, datalen
));
217 lde_imsg_compose_parent_sync(int type
, pid_t pid
, void *data
, uint16_t datalen
)
219 imsg_compose_event(iev_main_sync
, type
, 0, pid
, -1, data
, datalen
);
220 imsg_flush(&iev_main_sync
->ibuf
);
224 lde_imsg_compose_ldpe(int type
, uint32_t peerid
, pid_t pid
, void *data
,
227 return (imsg_compose_event(iev_ldpe
, type
, peerid
, pid
,
233 lde_dispatch_imsg(struct thread
*thread
)
235 struct imsgev
*iev
= THREAD_ARG(thread
);
236 struct imsgbuf
*ibuf
= &iev
->ibuf
;
240 struct lde_addr
*lde_addr
;
241 struct notify_msg
*nm
;
247 if ((n
= imsg_read(ibuf
)) == -1 && errno
!= EAGAIN
)
248 fatal("imsg_read error");
249 if (n
== 0) /* connection closed */
253 if ((n
= imsg_get(ibuf
, &imsg
)) == -1)
254 fatal("lde_dispatch_imsg: imsg_get error");
258 switch (imsg
.hdr
.type
) {
259 case IMSG_LABEL_MAPPING_FULL
:
260 ln
= lde_nbr_find(imsg
.hdr
.peerid
);
262 log_debug("%s: cannot find lde neighbor",
269 case IMSG_LABEL_MAPPING
:
270 case IMSG_LABEL_REQUEST
:
271 case IMSG_LABEL_RELEASE
:
272 case IMSG_LABEL_WITHDRAW
:
273 case IMSG_LABEL_ABORT
:
274 if (imsg
.hdr
.len
- IMSG_HEADER_SIZE
!=
276 fatalx("lde_dispatch_imsg: wrong imsg len");
279 ln
= lde_nbr_find(imsg
.hdr
.peerid
);
281 log_debug("%s: cannot find lde neighbor",
286 switch (imsg
.hdr
.type
) {
287 case IMSG_LABEL_MAPPING
:
288 lde_check_mapping(map
, ln
);
290 case IMSG_LABEL_REQUEST
:
291 lde_check_request(map
, ln
);
293 case IMSG_LABEL_RELEASE
:
294 lde_check_release(map
, ln
);
296 case IMSG_LABEL_WITHDRAW
:
297 lde_check_withdraw(map
, ln
);
299 case IMSG_LABEL_ABORT
:
304 case IMSG_ADDRESS_ADD
:
305 if (imsg
.hdr
.len
- IMSG_HEADER_SIZE
!=
306 sizeof(struct lde_addr
))
307 fatalx("lde_dispatch_imsg: wrong imsg len");
308 lde_addr
= imsg
.data
;
310 ln
= lde_nbr_find(imsg
.hdr
.peerid
);
312 log_debug("%s: cannot find lde neighbor",
316 if (lde_address_add(ln
, lde_addr
) < 0) {
317 log_debug("%s: cannot add address %s, it "
318 "already exists", __func__
,
319 log_addr(lde_addr
->af
, &lde_addr
->addr
));
322 case IMSG_ADDRESS_DEL
:
323 if (imsg
.hdr
.len
- IMSG_HEADER_SIZE
!=
324 sizeof(struct lde_addr
))
325 fatalx("lde_dispatch_imsg: wrong imsg len");
326 lde_addr
= imsg
.data
;
328 ln
= lde_nbr_find(imsg
.hdr
.peerid
);
330 log_debug("%s: cannot find lde neighbor",
334 if (lde_address_del(ln
, lde_addr
) < 0) {
335 log_debug("%s: cannot delete address %s, it "
336 "does not exist", __func__
,
337 log_addr(lde_addr
->af
, &lde_addr
->addr
));
340 case IMSG_NOTIFICATION
:
341 if (imsg
.hdr
.len
- IMSG_HEADER_SIZE
!=
342 sizeof(struct notify_msg
))
343 fatalx("lde_dispatch_imsg: wrong imsg len");
346 ln
= lde_nbr_find(imsg
.hdr
.peerid
);
348 log_debug("%s: cannot find lde neighbor",
353 switch (nm
->status_code
) {
355 l2vpn_recv_pw_status(ln
, nm
);
359 * Do nothing for now. Should be useful in
360 * the future when we implement LDP-IGP
361 * Synchronization (RFC 5443) and Graceful
362 * Restart (RFC 3478).
368 case IMSG_NEIGHBOR_UP
:
369 if (imsg
.hdr
.len
- IMSG_HEADER_SIZE
!=
370 sizeof(struct lde_nbr
))
371 fatalx("lde_dispatch_imsg: wrong imsg len");
373 if (lde_nbr_find(imsg
.hdr
.peerid
))
374 fatalx("lde_dispatch_imsg: "
375 "neighbor already exists");
376 lde_nbr_new(imsg
.hdr
.peerid
, imsg
.data
);
378 case IMSG_NEIGHBOR_DOWN
:
379 lde_nbr_del(lde_nbr_find(imsg
.hdr
.peerid
));
381 case IMSG_CTL_SHOW_LIB
:
382 rt_dump(imsg
.hdr
.pid
);
384 lde_imsg_compose_ldpe(IMSG_CTL_END
, 0,
385 imsg
.hdr
.pid
, NULL
, 0);
387 case IMSG_CTL_SHOW_L2VPN_PW
:
388 l2vpn_pw_ctl(imsg
.hdr
.pid
);
390 lde_imsg_compose_ldpe(IMSG_CTL_END
, 0,
391 imsg
.hdr
.pid
, NULL
, 0);
393 case IMSG_CTL_SHOW_L2VPN_BINDING
:
394 l2vpn_binding_ctl(imsg
.hdr
.pid
);
396 lde_imsg_compose_ldpe(IMSG_CTL_END
, 0,
397 imsg
.hdr
.pid
, NULL
, 0);
400 log_debug("%s: unexpected imsg %d", __func__
,
409 /* this pipe is dead, so remove the event handlers and exit */
410 THREAD_READ_OFF(iev
->ev_read
);
411 THREAD_WRITE_OFF(iev
->ev_write
);
420 lde_dispatch_parent(struct thread
*thread
)
422 static struct ldpd_conf
*nconf
;
423 struct iface
*iface
, *niface
;
425 struct nbr_params
*nnbrp
;
426 static struct l2vpn
*l2vpn
, *nl2vpn
;
427 struct l2vpn_if
*lif
, *nlif
;
428 struct l2vpn_pw
*pw
, *npw
;
432 int fd
= THREAD_FD(thread
);
433 struct imsgev
*iev
= THREAD_ARG(thread
);
434 struct imsgbuf
*ibuf
= &iev
->ibuf
;
441 if ((n
= imsg_read(ibuf
)) == -1 && errno
!= EAGAIN
)
442 fatal("imsg_read error");
443 if (n
== 0) /* connection closed */
447 if ((n
= imsg_get(ibuf
, &imsg
)) == -1)
448 fatal("lde_dispatch_parent: imsg_get error");
452 switch (imsg
.hdr
.type
) {
454 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
456 fatalx("IFSTATUS imsg with wrong len");
459 iface
= if_lookup_name(ldeconf
, kif
->ifname
);
461 if_update_info(iface
, kif
);
465 RB_FOREACH(l2vpn
, l2vpn_head
, &ldeconf
->l2vpn_tree
) {
466 lif
= l2vpn_if_find(l2vpn
, kif
->ifname
);
468 l2vpn_if_update_info(lif
, kif
);
471 pw
= l2vpn_pw_find(l2vpn
, kif
->ifname
);
473 l2vpn_pw_update_info(pw
, kif
);
478 case IMSG_NETWORK_ADD
:
479 case IMSG_NETWORK_UPDATE
:
480 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
481 sizeof(struct kroute
)) {
482 log_warnx("%s: wrong imsg len", __func__
);
489 fec
.type
= FEC_TYPE_IPV4
;
490 fec
.u
.ipv4
.prefix
= kr
->prefix
.v4
;
491 fec
.u
.ipv4
.prefixlen
= kr
->prefixlen
;
494 fec
.type
= FEC_TYPE_IPV6
;
495 fec
.u
.ipv6
.prefix
= kr
->prefix
.v6
;
496 fec
.u
.ipv6
.prefixlen
= kr
->prefixlen
;
499 fatalx("lde_dispatch_parent: unknown af");
502 switch (imsg
.hdr
.type
) {
503 case IMSG_NETWORK_ADD
:
504 lde_kernel_insert(&fec
, kr
->af
, &kr
->nexthop
,
505 kr
->ifindex
, kr
->priority
,
506 kr
->flags
& F_CONNECTED
, NULL
);
508 case IMSG_NETWORK_UPDATE
:
509 lde_kernel_update(&fec
);
513 case IMSG_SOCKET_IPC
:
515 log_warnx("%s: received unexpected imsg fd "
516 "to ldpe", __func__
);
519 if ((fd
= imsg
.fd
) == -1) {
520 log_warnx("%s: expected to receive imsg fd to "
521 "ldpe but didn't receive any", __func__
);
525 if ((iev_ldpe
= malloc(sizeof(struct imsgev
))) == NULL
)
527 imsg_init(&iev_ldpe
->ibuf
, fd
);
528 iev_ldpe
->handler_read
= lde_dispatch_imsg
;
529 iev_ldpe
->ev_read
= thread_add_read(master
,
530 iev_ldpe
->handler_read
, iev_ldpe
, iev_ldpe
->ibuf
.fd
);
531 iev_ldpe
->handler_write
= ldp_write_handler
;
532 iev_ldpe
->ev_write
= NULL
;
535 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
536 sizeof(struct ldpd_init
))
537 fatalx("INIT imsg with wrong len");
539 memcpy(&init
, imsg
.data
, sizeof(init
));
542 case IMSG_RECONF_CONF
:
543 if ((nconf
= malloc(sizeof(struct ldpd_conf
))) ==
546 memcpy(nconf
, imsg
.data
, sizeof(struct ldpd_conf
));
548 RB_INIT(&nconf
->iface_tree
);
549 RB_INIT(&nconf
->tnbr_tree
);
550 RB_INIT(&nconf
->nbrp_tree
);
551 RB_INIT(&nconf
->l2vpn_tree
);
553 case IMSG_RECONF_IFACE
:
554 if ((niface
= malloc(sizeof(struct iface
))) == NULL
)
556 memcpy(niface
, imsg
.data
, sizeof(struct iface
));
558 RB_INSERT(iface_head
, &nconf
->iface_tree
, niface
);
560 case IMSG_RECONF_TNBR
:
561 if ((ntnbr
= malloc(sizeof(struct tnbr
))) == NULL
)
563 memcpy(ntnbr
, imsg
.data
, sizeof(struct tnbr
));
565 RB_INSERT(tnbr_head
, &nconf
->tnbr_tree
, ntnbr
);
567 case IMSG_RECONF_NBRP
:
568 if ((nnbrp
= malloc(sizeof(struct nbr_params
))) == NULL
)
570 memcpy(nnbrp
, imsg
.data
, sizeof(struct nbr_params
));
572 RB_INSERT(nbrp_head
, &nconf
->nbrp_tree
, nnbrp
);
574 case IMSG_RECONF_L2VPN
:
575 if ((nl2vpn
= malloc(sizeof(struct l2vpn
))) == NULL
)
577 memcpy(nl2vpn
, imsg
.data
, sizeof(struct l2vpn
));
579 RB_INIT(&nl2vpn
->if_tree
);
580 RB_INIT(&nl2vpn
->pw_tree
);
581 RB_INIT(&nl2vpn
->pw_inactive_tree
);
583 RB_INSERT(l2vpn_head
, &nconf
->l2vpn_tree
, nl2vpn
);
585 case IMSG_RECONF_L2VPN_IF
:
586 if ((nlif
= malloc(sizeof(struct l2vpn_if
))) == NULL
)
588 memcpy(nlif
, imsg
.data
, sizeof(struct l2vpn_if
));
590 RB_INSERT(l2vpn_if_head
, &nl2vpn
->if_tree
, nlif
);
592 case IMSG_RECONF_L2VPN_PW
:
593 if ((npw
= malloc(sizeof(struct l2vpn_pw
))) == NULL
)
595 memcpy(npw
, imsg
.data
, sizeof(struct l2vpn_pw
));
597 RB_INSERT(l2vpn_pw_head
, &nl2vpn
->pw_tree
, npw
);
599 case IMSG_RECONF_L2VPN_IPW
:
600 if ((npw
= malloc(sizeof(struct l2vpn_pw
))) == NULL
)
602 memcpy(npw
, imsg
.data
, sizeof(struct l2vpn_pw
));
604 RB_INSERT(l2vpn_pw_head
, &nl2vpn
->pw_inactive_tree
, npw
);
606 case IMSG_RECONF_END
:
607 merge_config(ldeconf
, nconf
);
608 ldp_clear_config(nconf
);
611 case IMSG_DEBUG_UPDATE
:
612 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
614 log_warnx("%s: wrong imsg len", __func__
);
617 memcpy(&ldp_debug
, imsg
.data
, sizeof(ldp_debug
));
620 log_debug("%s: unexpected imsg %d", __func__
,
629 /* this pipe is dead, so remove the event handlers and exit */
630 THREAD_READ_OFF(iev
->ev_read
);
631 THREAD_WRITE_OFF(iev
->ev_write
);
639 lde_acl_check(char *acl_name
, int af
, union ldpd_addr
*addr
, uint8_t prefixlen
)
641 return ldp_acl_request(iev_main_sync
, acl_name
, af
, addr
, prefixlen
);
645 lde_update_label(struct fec_node
*fn
)
650 LIST_FOREACH(fnh
, &fn
->nexthops
, entry
) {
651 if (fnh
->flags
& F_FEC_NH_CONNECTED
) {
657 /* should we allocate a label for this fec? */
658 switch (fn
->fec
.type
) {
660 if ((ldeconf
->ipv4
.flags
& F_LDPD_AF_ALLOCHOSTONLY
) &&
661 fn
->fec
.u
.ipv4
.prefixlen
!= 32)
663 if (lde_acl_check(ldeconf
->ipv4
.acl_label_allocate_for
,
664 AF_INET
, (union ldpd_addr
*)&fn
->fec
.u
.ipv4
.prefix
,
665 fn
->fec
.u
.ipv4
.prefixlen
) != FILTER_PERMIT
)
669 if ((ldeconf
->ipv6
.flags
& F_LDPD_AF_ALLOCHOSTONLY
) &&
670 fn
->fec
.u
.ipv6
.prefixlen
!= 128)
672 if (lde_acl_check(ldeconf
->ipv6
.acl_label_allocate_for
,
673 AF_INET6
, (union ldpd_addr
*)&fn
->fec
.u
.ipv6
.prefix
,
674 fn
->fec
.u
.ipv6
.prefixlen
) != FILTER_PERMIT
)
682 /* choose implicit or explicit-null depending on configuration */
683 switch (fn
->fec
.type
) {
685 if (!(ldeconf
->ipv4
.flags
& F_LDPD_AF_EXPNULL
))
686 return (MPLS_LABEL_IMPLNULL
);
687 if (lde_acl_check(ldeconf
->ipv4
.acl_label_expnull_for
,
688 AF_INET
, (union ldpd_addr
*)&fn
->fec
.u
.ipv4
.prefix
,
689 fn
->fec
.u
.ipv4
.prefixlen
) != FILTER_PERMIT
)
690 return (MPLS_LABEL_IMPLNULL
);
691 return (MPLS_LABEL_IPV4NULL
);
693 if (!(ldeconf
->ipv6
.flags
& F_LDPD_AF_EXPNULL
))
694 return (MPLS_LABEL_IMPLNULL
);
695 if (lde_acl_check(ldeconf
->ipv6
.acl_label_expnull_for
,
696 AF_INET6
, (union ldpd_addr
*)&fn
->fec
.u
.ipv6
.prefix
,
697 fn
->fec
.u
.ipv6
.prefixlen
) != FILTER_PERMIT
)
698 return (MPLS_LABEL_IMPLNULL
);
699 return (MPLS_LABEL_IPV6NULL
);
701 fatalx("lde_update_label: unexpected fec type");
706 /* preserve current label if there's no need to update it */
707 if (fn
->local_label
!= NO_LABEL
&&
708 fn
->local_label
> MPLS_LABEL_RESERVED_MAX
)
709 return (fn
->local_label
);
711 return (lde_get_next_label());
715 lde_send_change_klabel(struct fec_node
*fn
, struct fec_nh
*fnh
)
721 switch (fn
->fec
.type
) {
723 memset(&kr
, 0, sizeof(kr
));
725 kr
.prefix
.v4
= fn
->fec
.u
.ipv4
.prefix
;
726 kr
.prefixlen
= fn
->fec
.u
.ipv4
.prefixlen
;
727 kr
.nexthop
.v4
= fnh
->nexthop
.v4
;
728 kr
.ifindex
= fnh
->ifindex
;
729 kr
.local_label
= fn
->local_label
;
730 kr
.remote_label
= fnh
->remote_label
;
731 kr
.priority
= fnh
->priority
;
733 lde_imsg_compose_parent(IMSG_KLABEL_CHANGE
, 0, &kr
,
737 memset(&kr
, 0, sizeof(kr
));
739 kr
.prefix
.v6
= fn
->fec
.u
.ipv6
.prefix
;
740 kr
.prefixlen
= fn
->fec
.u
.ipv6
.prefixlen
;
741 kr
.nexthop
.v6
= fnh
->nexthop
.v6
;
742 kr
.ifindex
= fnh
->ifindex
;
743 kr
.local_label
= fn
->local_label
;
744 kr
.remote_label
= fnh
->remote_label
;
745 kr
.priority
= fnh
->priority
;
747 lde_imsg_compose_parent(IMSG_KLABEL_CHANGE
, 0, &kr
,
751 if (fn
->local_label
== NO_LABEL
||
752 fnh
->remote_label
== NO_LABEL
)
755 pw
= (struct l2vpn_pw
*) fn
->data
;
756 pw
->flags
|= F_PW_STATUS_UP
;
758 memset(&kpw
, 0, sizeof(kpw
));
759 kpw
.ifindex
= pw
->ifindex
;
760 kpw
.pw_type
= fn
->fec
.u
.pwid
.type
;
762 kpw
.nexthop
= pw
->addr
;
763 kpw
.local_label
= fn
->local_label
;
764 kpw
.remote_label
= fnh
->remote_label
;
765 kpw
.flags
= pw
->flags
;
767 lde_imsg_compose_parent(IMSG_KPWLABEL_CHANGE
, 0, &kpw
,
774 lde_send_delete_klabel(struct fec_node
*fn
, struct fec_nh
*fnh
)
780 switch (fn
->fec
.type
) {
782 memset(&kr
, 0, sizeof(kr
));
784 kr
.prefix
.v4
= fn
->fec
.u
.ipv4
.prefix
;
785 kr
.prefixlen
= fn
->fec
.u
.ipv4
.prefixlen
;
786 kr
.nexthop
.v4
= fnh
->nexthop
.v4
;
787 kr
.ifindex
= fnh
->ifindex
;
788 kr
.local_label
= fn
->local_label
;
789 kr
.remote_label
= fnh
->remote_label
;
790 kr
.priority
= fnh
->priority
;
792 lde_imsg_compose_parent(IMSG_KLABEL_DELETE
, 0, &kr
,
796 memset(&kr
, 0, sizeof(kr
));
798 kr
.prefix
.v6
= fn
->fec
.u
.ipv6
.prefix
;
799 kr
.prefixlen
= fn
->fec
.u
.ipv6
.prefixlen
;
800 kr
.nexthop
.v6
= fnh
->nexthop
.v6
;
801 kr
.ifindex
= fnh
->ifindex
;
802 kr
.local_label
= fn
->local_label
;
803 kr
.remote_label
= fnh
->remote_label
;
804 kr
.priority
= fnh
->priority
;
806 lde_imsg_compose_parent(IMSG_KLABEL_DELETE
, 0, &kr
,
810 pw
= (struct l2vpn_pw
*) fn
->data
;
811 if (!(pw
->flags
& F_PW_STATUS_UP
))
813 pw
->flags
&= ~F_PW_STATUS_UP
;
815 memset(&kpw
, 0, sizeof(kpw
));
816 kpw
.ifindex
= pw
->ifindex
;
817 kpw
.pw_type
= fn
->fec
.u
.pwid
.type
;
819 kpw
.nexthop
= pw
->addr
;
820 kpw
.local_label
= fn
->local_label
;
821 kpw
.remote_label
= fnh
->remote_label
;
822 kpw
.flags
= pw
->flags
;
824 lde_imsg_compose_parent(IMSG_KPWLABEL_DELETE
, 0, &kpw
,
831 lde_fec2map(struct fec
*fec
, struct map
*map
)
833 memset(map
, 0, sizeof(*map
));
837 map
->type
= MAP_TYPE_PREFIX
;
838 map
->fec
.prefix
.af
= AF_INET
;
839 map
->fec
.prefix
.prefix
.v4
= fec
->u
.ipv4
.prefix
;
840 map
->fec
.prefix
.prefixlen
= fec
->u
.ipv4
.prefixlen
;
843 map
->type
= MAP_TYPE_PREFIX
;
844 map
->fec
.prefix
.af
= AF_INET6
;
845 map
->fec
.prefix
.prefix
.v6
= fec
->u
.ipv6
.prefix
;
846 map
->fec
.prefix
.prefixlen
= fec
->u
.ipv6
.prefixlen
;
849 map
->type
= MAP_TYPE_PWID
;
850 map
->fec
.pwid
.type
= fec
->u
.pwid
.type
;
851 map
->fec
.pwid
.group_id
= 0;
852 map
->flags
|= F_MAP_PW_ID
;
853 map
->fec
.pwid
.pwid
= fec
->u
.pwid
.pwid
;
859 lde_map2fec(struct map
*map
, struct in_addr lsr_id
, struct fec
*fec
)
861 memset(fec
, 0, sizeof(*fec
));
864 case MAP_TYPE_PREFIX
:
865 switch (map
->fec
.prefix
.af
) {
867 fec
->type
= FEC_TYPE_IPV4
;
868 fec
->u
.ipv4
.prefix
= map
->fec
.prefix
.prefix
.v4
;
869 fec
->u
.ipv4
.prefixlen
= map
->fec
.prefix
.prefixlen
;
872 fec
->type
= FEC_TYPE_IPV6
;
873 fec
->u
.ipv6
.prefix
= map
->fec
.prefix
.prefix
.v6
;
874 fec
->u
.ipv6
.prefixlen
= map
->fec
.prefix
.prefixlen
;
877 fatalx("lde_map2fec: unknown af");
882 fec
->type
= FEC_TYPE_PWID
;
883 fec
->u
.pwid
.type
= map
->fec
.pwid
.type
;
884 fec
->u
.pwid
.pwid
= map
->fec
.pwid
.pwid
;
885 fec
->u
.pwid
.lsr_id
= lsr_id
;
891 lde_send_labelmapping(struct lde_nbr
*ln
, struct fec_node
*fn
, int single
)
893 struct lde_wdraw
*lw
;
900 * We shouldn't send a new label mapping if we have a pending
901 * label release to receive. In this case, schedule to send a
902 * label mapping as soon as a label release is received.
904 lw
= (struct lde_wdraw
*)fec_find(&ln
->sent_wdraw
, &fn
->fec
);
906 if (!fec_find(&ln
->sent_map_pending
, &fn
->fec
))
907 lde_map_pending_add(ln
, fn
);
912 * This function skips SL.1 - 3 and SL.9 - 14 because the label
913 * allocation is done way earlier (because of the merging nature of
917 lde_fec2map(&fn
->fec
, &map
);
918 switch (fn
->fec
.type
) {
922 if (lde_acl_check(ldeconf
->ipv4
.acl_label_advertise_to
,
923 AF_INET
, (union ldpd_addr
*)&ln
->id
, 32) != FILTER_PERMIT
)
925 if (lde_acl_check(ldeconf
->ipv4
.acl_label_advertise_for
,
926 AF_INET
, (union ldpd_addr
*)&fn
->fec
.u
.ipv4
.prefix
,
927 fn
->fec
.u
.ipv4
.prefixlen
) != FILTER_PERMIT
)
933 if (lde_acl_check(ldeconf
->ipv6
.acl_label_advertise_to
,
934 AF_INET
, (union ldpd_addr
*)&ln
->id
, 32) != FILTER_PERMIT
)
936 if (lde_acl_check(ldeconf
->ipv6
.acl_label_advertise_for
,
937 AF_INET6
, (union ldpd_addr
*)&fn
->fec
.u
.ipv6
.prefix
,
938 fn
->fec
.u
.ipv6
.prefixlen
) != FILTER_PERMIT
)
942 pw
= (struct l2vpn_pw
*) fn
->data
;
943 if (pw
== NULL
|| pw
->lsr_id
.s_addr
!= ln
->id
.s_addr
)
944 /* not the remote end of the pseudowire */
947 map
.flags
|= F_MAP_PW_IFMTU
;
948 map
.fec
.pwid
.ifmtu
= pw
->l2vpn
->mtu
;
949 if (pw
->flags
& F_PW_CWORD
)
950 map
.flags
|= F_MAP_PW_CWORD
;
951 if (pw
->flags
& F_PW_STATUSTLV
) {
952 map
.flags
|= F_MAP_PW_STATUS
;
953 /* VPLS are always up */
954 map
.pw_status
= PW_FORWARDING
;
958 map
.label
= fn
->local_label
;
960 /* SL.6: is there a pending request for this mapping? */
961 lre
= (struct lde_req
*)fec_find(&ln
->recv_req
, &fn
->fec
);
963 /* set label request msg id in the mapping response. */
964 map
.requestid
= lre
->msg_id
;
965 map
.flags
= F_MAP_REQ_ID
;
967 /* SL.7: delete record of pending request */
968 lde_req_del(ln
, lre
, 0);
971 /* SL.4: send label mapping */
972 lde_imsg_compose_ldpe(IMSG_MAPPING_ADD
, ln
->peerid
, 0,
975 lde_imsg_compose_ldpe(IMSG_MAPPING_ADD_END
, ln
->peerid
, 0,
978 /* SL.5: record sent label mapping */
979 me
= (struct lde_map
*)fec_find(&ln
->sent_map
, &fn
->fec
);
981 me
= lde_map_add(ln
, fn
, 1);
986 lde_send_labelwithdraw(struct lde_nbr
*ln
, struct fec_node
*fn
,
987 struct map
*wcard
, struct status_tlv
*st
)
989 struct lde_wdraw
*lw
;
995 lde_fec2map(&fn
->fec
, &map
);
996 switch (fn
->fec
.type
) {
1002 if (!ln
->v6_enabled
)
1006 pw
= (struct l2vpn_pw
*) fn
->data
;
1007 if (pw
== NULL
|| pw
->lsr_id
.s_addr
!= ln
->id
.s_addr
)
1008 /* not the remote end of the pseudowire */
1011 if (pw
->flags
& F_PW_CWORD
)
1012 map
.flags
|= F_MAP_PW_CWORD
;
1015 map
.label
= fn
->local_label
;
1017 memcpy(&map
, wcard
, sizeof(map
));
1020 map
.st
.status_code
= st
->status_code
;
1021 map
.st
.msg_id
= st
->msg_id
;
1022 map
.st
.msg_type
= st
->msg_type
;
1023 map
.flags
|= F_MAP_STATUS
;
1026 /* SWd.1: send label withdraw. */
1027 lde_imsg_compose_ldpe(IMSG_WITHDRAW_ADD
, ln
->peerid
, 0,
1029 lde_imsg_compose_ldpe(IMSG_WITHDRAW_ADD_END
, ln
->peerid
, 0, NULL
, 0);
1031 /* SWd.2: record label withdraw. */
1033 lw
= (struct lde_wdraw
*)fec_find(&ln
->sent_wdraw
, &fn
->fec
);
1035 lw
= lde_wdraw_add(ln
, fn
);
1036 lw
->label
= map
.label
;
1040 RB_FOREACH(f
, fec_tree
, &ft
) {
1041 fn
= (struct fec_node
*)f
;
1042 me
= (struct lde_map
*)fec_find(&ln
->sent_map
, &fn
->fec
);
1043 if (lde_wildcard_apply(wcard
, &fn
->fec
, me
) == 0)
1046 lw
= (struct lde_wdraw
*)fec_find(&ln
->sent_wdraw
,
1049 lw
= lde_wdraw_add(ln
, fn
);
1050 lw
->label
= map
.label
;
1056 lde_send_labelwithdraw_wcard(struct lde_nbr
*ln
, uint32_t label
)
1060 memset(&wcard
, 0, sizeof(wcard
));
1061 wcard
.type
= MAP_TYPE_WILDCARD
;
1062 wcard
.label
= label
;
1063 lde_send_labelwithdraw(ln
, NULL
, &wcard
, NULL
);
1067 lde_send_labelwithdraw_twcard_prefix(struct lde_nbr
*ln
, uint16_t af
,
1072 memset(&wcard
, 0, sizeof(wcard
));
1073 wcard
.type
= MAP_TYPE_TYPED_WCARD
;
1074 wcard
.fec
.twcard
.type
= MAP_TYPE_PREFIX
;
1075 wcard
.fec
.twcard
.u
.prefix_af
= af
;
1076 wcard
.label
= label
;
1077 lde_send_labelwithdraw(ln
, NULL
, &wcard
, NULL
);
1081 lde_send_labelwithdraw_twcard_pwid(struct lde_nbr
*ln
, uint16_t pw_type
,
1086 memset(&wcard
, 0, sizeof(wcard
));
1087 wcard
.type
= MAP_TYPE_TYPED_WCARD
;
1088 wcard
.fec
.twcard
.type
= MAP_TYPE_PWID
;
1089 wcard
.fec
.twcard
.u
.pw_type
= pw_type
;
1090 wcard
.label
= label
;
1091 lde_send_labelwithdraw(ln
, NULL
, &wcard
, NULL
);
1095 lde_send_labelwithdraw_pwid_wcard(struct lde_nbr
*ln
, uint16_t pw_type
,
1100 memset(&wcard
, 0, sizeof(wcard
));
1101 wcard
.type
= MAP_TYPE_PWID
;
1102 wcard
.fec
.pwid
.type
= pw_type
;
1103 wcard
.fec
.pwid
.group_id
= group_id
;
1104 /* we can not append a Label TLV when using PWid group wildcards. */
1105 wcard
.label
= NO_LABEL
;
1106 lde_send_labelwithdraw(ln
, NULL
, &wcard
, NULL
);
1110 lde_send_labelrelease(struct lde_nbr
*ln
, struct fec_node
*fn
,
1111 struct map
*wcard
, uint32_t label
)
1114 struct l2vpn_pw
*pw
;
1117 lde_fec2map(&fn
->fec
, &map
);
1118 switch (fn
->fec
.type
) {
1120 if (!ln
->v4_enabled
)
1124 if (!ln
->v6_enabled
)
1128 pw
= (struct l2vpn_pw
*) fn
->data
;
1129 if (pw
== NULL
|| pw
->lsr_id
.s_addr
!= ln
->id
.s_addr
)
1130 /* not the remote end of the pseudowire */
1133 if (pw
->flags
& F_PW_CWORD
)
1134 map
.flags
|= F_MAP_PW_CWORD
;
1138 memcpy(&map
, wcard
, sizeof(map
));
1141 lde_imsg_compose_ldpe(IMSG_RELEASE_ADD
, ln
->peerid
, 0,
1143 lde_imsg_compose_ldpe(IMSG_RELEASE_ADD_END
, ln
->peerid
, 0, NULL
, 0);
1147 lde_send_notification(struct lde_nbr
*ln
, uint32_t status_code
, uint32_t msg_id
,
1150 struct notify_msg nm
;
1152 memset(&nm
, 0, sizeof(nm
));
1153 nm
.status_code
= status_code
;
1154 /* 'msg_id' and 'msg_type' should be in network byte order */
1156 nm
.msg_type
= msg_type
;
1158 lde_imsg_compose_ldpe(IMSG_NOTIFICATION_SEND
, ln
->peerid
, 0,
1163 lde_send_notification_eol_prefix(struct lde_nbr
*ln
, int af
)
1165 struct notify_msg nm
;
1167 memset(&nm
, 0, sizeof(nm
));
1168 nm
.status_code
= S_ENDOFLIB
;
1169 nm
.fec
.type
= MAP_TYPE_TYPED_WCARD
;
1170 nm
.fec
.fec
.twcard
.type
= MAP_TYPE_PREFIX
;
1171 nm
.fec
.fec
.twcard
.u
.prefix_af
= af
;
1172 nm
.flags
|= F_NOTIF_FEC
;
1174 lde_imsg_compose_ldpe(IMSG_NOTIFICATION_SEND
, ln
->peerid
, 0,
1179 lde_send_notification_eol_pwid(struct lde_nbr
*ln
, uint16_t pw_type
)
1181 struct notify_msg nm
;
1183 memset(&nm
, 0, sizeof(nm
));
1184 nm
.status_code
= S_ENDOFLIB
;
1185 nm
.fec
.type
= MAP_TYPE_TYPED_WCARD
;
1186 nm
.fec
.fec
.twcard
.type
= MAP_TYPE_PWID
;
1187 nm
.fec
.fec
.twcard
.u
.pw_type
= pw_type
;
1188 nm
.flags
|= F_NOTIF_FEC
;
1190 lde_imsg_compose_ldpe(IMSG_NOTIFICATION_SEND
, ln
->peerid
, 0,
1195 lde_nbr_compare(struct lde_nbr
*a
, struct lde_nbr
*b
)
1197 return (a
->peerid
- b
->peerid
);
1200 static struct lde_nbr
*
1201 lde_nbr_new(uint32_t peerid
, struct lde_nbr
*new)
1205 if ((ln
= calloc(1, sizeof(*ln
))) == NULL
)
1209 ln
->v4_enabled
= new->v4_enabled
;
1210 ln
->v6_enabled
= new->v6_enabled
;
1211 ln
->flags
= new->flags
;
1212 ln
->peerid
= peerid
;
1213 fec_init(&ln
->recv_map
);
1214 fec_init(&ln
->sent_map
);
1215 fec_init(&ln
->sent_map_pending
);
1216 fec_init(&ln
->recv_req
);
1217 fec_init(&ln
->sent_req
);
1218 fec_init(&ln
->sent_wdraw
);
1220 TAILQ_INIT(&ln
->addr_list
);
1222 if (RB_INSERT(nbr_tree
, &lde_nbrs
, ln
) != NULL
)
1223 fatalx("lde_nbr_new: RB_INSERT failed");
1229 lde_nbr_del(struct lde_nbr
*ln
)
1232 struct fec_node
*fn
;
1234 struct l2vpn_pw
*pw
;
1239 /* uninstall received mappings */
1240 RB_FOREACH(f
, fec_tree
, &ft
) {
1241 fn
= (struct fec_node
*)f
;
1243 LIST_FOREACH(fnh
, &fn
->nexthops
, entry
) {
1247 if (!lde_address_find(ln
, fnh
->af
,
1252 if (f
->u
.pwid
.lsr_id
.s_addr
!= ln
->id
.s_addr
)
1254 pw
= (struct l2vpn_pw
*) fn
->data
;
1262 lde_send_delete_klabel(fn
, fnh
);
1263 fnh
->remote_label
= NO_LABEL
;
1267 lde_address_list_free(ln
);
1269 fec_clear(&ln
->recv_map
, lde_map_free
);
1270 fec_clear(&ln
->sent_map
, lde_map_free
);
1271 fec_clear(&ln
->sent_map_pending
, free
);
1272 fec_clear(&ln
->recv_req
, free
);
1273 fec_clear(&ln
->sent_req
, free
);
1274 fec_clear(&ln
->sent_wdraw
, free
);
1276 RB_REMOVE(nbr_tree
, &lde_nbrs
, ln
);
1281 static struct lde_nbr
*
1282 lde_nbr_find(uint32_t peerid
)
1288 return (RB_FIND(nbr_tree
, &lde_nbrs
, &ln
));
1292 lde_nbr_find_by_lsrid(struct in_addr addr
)
1296 RB_FOREACH(ln
, nbr_tree
, &lde_nbrs
)
1297 if (ln
->id
.s_addr
== addr
.s_addr
)
1304 lde_nbr_find_by_addr(int af
, union ldpd_addr
*addr
)
1308 RB_FOREACH(ln
, nbr_tree
, &lde_nbrs
)
1309 if (lde_address_find(ln
, af
, addr
) != NULL
)
1320 while ((ln
= RB_ROOT(&lde_nbrs
)) != NULL
)
1325 lde_nbr_addr_update(struct lde_nbr
*ln
, struct lde_addr
*lde_addr
, int removed
)
1328 struct fec_node
*fn
;
1332 RB_FOREACH(fec
, fec_tree
, &ln
->recv_map
) {
1333 fn
= (struct fec_node
*)fec_find(&ft
, fec
);
1334 switch (fec
->type
) {
1336 if (lde_addr
->af
!= AF_INET
)
1340 if (lde_addr
->af
!= AF_INET6
)
1347 LIST_FOREACH(fnh
, &fn
->nexthops
, entry
) {
1348 if (ldp_addrcmp(fnh
->af
, &fnh
->nexthop
,
1353 lde_send_delete_klabel(fn
, fnh
);
1354 fnh
->remote_label
= NO_LABEL
;
1356 me
= (struct lde_map
*)fec
;
1357 fnh
->remote_label
= me
->map
.label
;
1358 lde_send_change_klabel(fn
, fnh
);
1366 lde_map_compare(struct lde_map
*a
, struct lde_map
*b
)
1368 return (ldp_addrcmp(AF_INET
, (union ldpd_addr
*)&a
->nexthop
->id
,
1369 (union ldpd_addr
*)&b
->nexthop
->id
));
1373 lde_map_add(struct lde_nbr
*ln
, struct fec_node
*fn
, int sent
)
1377 me
= calloc(1, sizeof(*me
));
1385 RB_INSERT(lde_map_head
, &fn
->upstream
, me
);
1386 me
->head
= &fn
->upstream
;
1387 if (fec_insert(&ln
->sent_map
, &me
->fec
))
1388 log_warnx("failed to add %s to sent map",
1390 /* XXX on failure more cleanup is needed */
1392 RB_INSERT(lde_map_head
, &fn
->downstream
, me
);
1393 me
->head
= &fn
->downstream
;
1394 if (fec_insert(&ln
->recv_map
, &me
->fec
))
1395 log_warnx("failed to add %s to recv map",
1403 lde_map_del(struct lde_nbr
*ln
, struct lde_map
*me
, int sent
)
1406 fec_remove(&ln
->sent_map
, &me
->fec
);
1408 fec_remove(&ln
->recv_map
, &me
->fec
);
1414 lde_map_free(void *ptr
)
1416 struct lde_map
*map
= ptr
;
1418 RB_REMOVE(lde_map_head
, map
->head
, map
);
1423 lde_map_pending_add(struct lde_nbr
*ln
, struct fec_node
*fn
)
1427 map
= calloc(1, sizeof(*map
));
1432 if (fec_insert(&ln
->sent_map_pending
, map
))
1433 log_warnx("failed to add %s to sent map (pending)",
1440 lde_map_pending_del(struct lde_nbr
*ln
, struct fec
*map
)
1442 fec_remove(&ln
->sent_map_pending
, map
);
1447 lde_req_add(struct lde_nbr
*ln
, struct fec
*fec
, int sent
)
1450 struct lde_req
*lre
;
1452 t
= sent
? &ln
->sent_req
: &ln
->recv_req
;
1454 lre
= calloc(1, sizeof(*lre
));
1458 if (fec_insert(t
, &lre
->fec
)) {
1459 log_warnx("failed to add %s to %s req",
1460 log_fec(&lre
->fec
), sent
? "sent" : "recv");
1470 lde_req_del(struct lde_nbr
*ln
, struct lde_req
*lre
, int sent
)
1473 fec_remove(&ln
->sent_req
, &lre
->fec
);
1475 fec_remove(&ln
->recv_req
, &lre
->fec
);
1481 lde_wdraw_add(struct lde_nbr
*ln
, struct fec_node
*fn
)
1483 struct lde_wdraw
*lw
;
1485 lw
= calloc(1, sizeof(*lw
));
1491 if (fec_insert(&ln
->sent_wdraw
, &lw
->fec
))
1492 log_warnx("failed to add %s to sent wdraw",
1499 lde_wdraw_del(struct lde_nbr
*ln
, struct lde_wdraw
*lw
)
1501 fec_remove(&ln
->sent_wdraw
, &lw
->fec
);
1506 lde_change_egress_label(int af
)
1510 struct fec_node
*fn
;
1512 /* explicitly withdraw all null labels */
1513 RB_FOREACH(ln
, nbr_tree
, &lde_nbrs
) {
1514 lde_send_labelwithdraw_wcard(ln
, MPLS_LABEL_IMPLNULL
);
1516 lde_send_labelwithdraw_wcard(ln
, MPLS_LABEL_IPV4NULL
);
1518 lde_send_labelwithdraw_wcard(ln
, MPLS_LABEL_IPV6NULL
);
1521 /* update label of connected routes */
1522 RB_FOREACH(f
, fec_tree
, &ft
) {
1523 fn
= (struct fec_node
*)f
;
1524 if (fn
->local_label
> MPLS_LABEL_RESERVED_MAX
)
1529 if (fn
->fec
.type
!= FEC_TYPE_IPV4
)
1533 if (fn
->fec
.type
!= FEC_TYPE_IPV6
)
1537 fatalx("lde_change_egress_label: unknown af");
1540 fn
->local_label
= lde_update_label(fn
);
1541 if (fn
->local_label
!= NO_LABEL
)
1542 RB_FOREACH(ln
, nbr_tree
, &lde_nbrs
)
1543 lde_send_labelmapping(ln
, fn
, 0);
1545 RB_FOREACH(ln
, nbr_tree
, &lde_nbrs
)
1546 lde_imsg_compose_ldpe(IMSG_MAPPING_ADD_END
, ln
->peerid
, 0,
1551 lde_address_add(struct lde_nbr
*ln
, struct lde_addr
*lde_addr
)
1553 struct lde_addr
*new;
1555 if (lde_address_find(ln
, lde_addr
->af
, &lde_addr
->addr
) != NULL
)
1558 if ((new = calloc(1, sizeof(*new))) == NULL
)
1561 new->af
= lde_addr
->af
;
1562 new->addr
= lde_addr
->addr
;
1563 TAILQ_INSERT_TAIL(&ln
->addr_list
, new, entry
);
1565 /* reevaluate the previously received mappings from this neighbor */
1566 lde_nbr_addr_update(ln
, lde_addr
, 0);
1572 lde_address_del(struct lde_nbr
*ln
, struct lde_addr
*lde_addr
)
1574 lde_addr
= lde_address_find(ln
, lde_addr
->af
, &lde_addr
->addr
);
1575 if (lde_addr
== NULL
)
1578 /* reevaluate the previously received mappings from this neighbor */
1579 lde_nbr_addr_update(ln
, lde_addr
, 1);
1581 TAILQ_REMOVE(&ln
->addr_list
, lde_addr
, entry
);
1588 lde_address_find(struct lde_nbr
*ln
, int af
, union ldpd_addr
*addr
)
1590 struct lde_addr
*lde_addr
;
1592 TAILQ_FOREACH(lde_addr
, &ln
->addr_list
, entry
)
1593 if (lde_addr
->af
== af
&&
1594 ldp_addrcmp(af
, &lde_addr
->addr
, addr
) == 0)
1601 lde_address_list_free(struct lde_nbr
*ln
)
1603 struct lde_addr
*lde_addr
;
1605 while ((lde_addr
= TAILQ_FIRST(&ln
->addr_list
)) != NULL
) {
1606 TAILQ_REMOVE(&ln
->addr_list
, lde_addr
, entry
);
1612 zclient_sync_init(u_short instance
)
1614 /* Initialize special zclient for synchronous message exchanges. */
1615 log_debug("Initializing synchronous zclient for label manager");
1616 zclient_sync
= zclient_new(master
);
1617 zclient_sync
->sock
= -1;
1618 zclient_sync
->redist_default
= ZEBRA_ROUTE_LDP
;
1619 zclient_sync
->instance
= instance
;
1620 while (zclient_socket_connect(zclient_sync
) < 0) {
1621 log_warnx("Error connecting synchronous zclient!");
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!");
1648 close(zclient_sync
->sock
);
1649 zclient_sync
->sock
= -1;
1653 on_get_label_chunk_response(start
, end
);
1659 lde_label_list_init(void)
1661 label_chunk_list
= list_new();
1662 label_chunk_list
->del
= lde_del_label_chunk
;
1664 /* get first chunk */
1665 while (lde_get_label_chunk () != 0) {
1666 log_warnx("Error getting first label chunk!");
1672 on_get_label_chunk_response(uint32_t start
, uint32_t end
)
1674 struct label_chunk
*new_label_chunk
;
1676 log_debug("Label Chunk assign: %u - %u", start
, end
);
1678 new_label_chunk
= calloc(1, sizeof(struct label_chunk
));
1679 if (!new_label_chunk
) {
1680 log_warn("Error trying to allocate label chunk %u - %u", start
, end
);
1684 new_label_chunk
->start
= start
;
1685 new_label_chunk
->end
= end
;
1686 new_label_chunk
->used_mask
= 0;
1688 listnode_add(label_chunk_list
, (void *)new_label_chunk
);
1690 /* let's update current if needed */
1691 if (!current_label_chunk
)
1692 current_label_chunk
= listtail(label_chunk_list
);
1696 lde_get_next_label(void)
1698 struct label_chunk
*label_chunk
;
1699 uint32_t i
, pos
, size
;
1700 uint32_t label
= NO_LABEL
;
1702 while (current_label_chunk
) {
1703 label_chunk
= listgetdata(current_label_chunk
);
1707 /* try to get next free label in currently used label chunk */
1708 size
= label_chunk
->end
- label_chunk
->start
+ 1;
1709 for (i
= 0, pos
= 1; i
< size
; i
++, pos
<<= 1) {
1710 if (!(pos
& label_chunk
->used_mask
)) {
1711 label_chunk
->used_mask
|= pos
;
1712 label
= label_chunk
->start
+ i
;
1716 current_label_chunk
= listnextnode(current_label_chunk
);
1720 /* we moved till the last chunk, or were not able to find a label,
1721 so let's ask for another one */
1722 if (!current_label_chunk
||
1723 current_label_chunk
== listtail(label_chunk_list
) ||
1724 label
== NO_LABEL
) {
1725 if (lde_get_label_chunk() != 0)
1726 log_warn("%s: Error getting label chunk!", __func__
);