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 struct list
*label_chunk_list
;
93 struct listnode
*current_label_chunk
;
95 /* SIGINT / SIGTERM handler. */
102 static struct quagga_signal_t lde_signals
[] =
118 struct zclient
*zclient_sync
= NULL
;
120 zclient_sync_init(u_short instance
)
122 /* Initialize special zclient for synchronous message exchanges. */
123 log_debug("Initializing synchronous zclient for label manager");
124 zclient_sync
= zclient_new(master
);
125 zclient_sync
->sock
= -1;
126 zclient_sync
->redist_default
= ZEBRA_ROUTE_LDP
;
127 zclient_sync
->instance
= instance
;
128 while (zclient_socket_connect (zclient_sync
) < 0) {
129 log_warnx("Error connecting synchronous zclient!");
132 /* make socket non-blocking */
133 sock_set_nonblock(zclient_sync
->sock
);
135 /* Connect to label manager */
136 while (lm_label_manager_connect (zclient_sync
) != 0) {
137 log_warnx("Error connecting to label manager!");
142 /* label decision engine */
146 struct thread thread
;
148 #ifdef HAVE_SETPROCTITLE
149 setproctitle("label decision engine");
151 ldpd_process
= PROC_LDE_ENGINE
;
152 log_procname
= log_procnames
[PROC_LDE_ENGINE
];
154 master
= thread_master_create();
156 /* setup signal handler */
157 signal_init(master
, array_size(lde_signals
), lde_signals
);
159 /* setup pipes and event handlers to the parent process */
160 if ((iev_main
= calloc(1, sizeof(struct imsgev
))) == NULL
)
162 imsg_init(&iev_main
->ibuf
, LDPD_FD_ASYNC
);
163 iev_main
->handler_read
= lde_dispatch_parent
;
164 iev_main
->ev_read
= thread_add_read(master
, iev_main
->handler_read
,
165 iev_main
, iev_main
->ibuf
.fd
);
166 iev_main
->handler_write
= ldp_write_handler
;
168 if ((iev_main_sync
= calloc(1, sizeof(struct imsgev
))) == NULL
)
170 imsg_init(&iev_main_sync
->ibuf
, LDPD_FD_SYNC
);
172 /* create base configuration */
173 ldeconf
= config_new_empty();
175 /* Fetch next active thread. */
176 while (thread_fetch(master
, &thread
))
177 thread_call(&thread
);
181 lde_init(struct ldpd_init
*init
)
183 /* drop privileges */
184 lde_privs
.user
= init
->user
;
185 lde_privs
.group
= init
->group
;
186 zprivs_init(&lde_privs
);
188 /* start the LIB garbage collector */
189 lde_gc_start_timer();
191 /* Init synchronous zclient and label list */
192 zclient_serv_path_set(init
->zclient_serv_path
);
193 zclient_sync_init(init
->instance
);
194 lde_label_list_init();
202 msgbuf_clear(&iev_ldpe
->ibuf
.w
);
203 close(iev_ldpe
->ibuf
.fd
);
205 msgbuf_clear(&iev_main
->ibuf
.w
);
206 close(iev_main
->ibuf
.fd
);
207 msgbuf_clear(&iev_main_sync
->ibuf
.w
);
208 close(iev_main_sync
->ibuf
.fd
);
214 config_clear(ldeconf
);
221 log_info("label decision engine exiting");
227 lde_imsg_compose_parent(int type
, pid_t pid
, void *data
, uint16_t datalen
)
229 return (imsg_compose_event(iev_main
, type
, 0, pid
, -1, data
, datalen
));
233 lde_imsg_compose_parent_sync(int type
, pid_t pid
, void *data
, uint16_t datalen
)
235 imsg_compose_event(iev_main_sync
, type
, 0, pid
, -1, data
, datalen
);
236 imsg_flush(&iev_main_sync
->ibuf
);
240 lde_imsg_compose_ldpe(int type
, uint32_t peerid
, pid_t pid
, void *data
,
243 return (imsg_compose_event(iev_ldpe
, type
, peerid
, pid
,
249 lde_dispatch_imsg(struct thread
*thread
)
251 struct imsgev
*iev
= THREAD_ARG(thread
);
252 struct imsgbuf
*ibuf
= &iev
->ibuf
;
256 struct lde_addr
*lde_addr
;
257 struct notify_msg
*nm
;
263 if ((n
= imsg_read(ibuf
)) == -1 && errno
!= EAGAIN
)
264 fatal("imsg_read error");
265 if (n
== 0) /* connection closed */
269 if ((n
= imsg_get(ibuf
, &imsg
)) == -1)
270 fatal("lde_dispatch_imsg: imsg_get error");
274 switch (imsg
.hdr
.type
) {
275 case IMSG_LABEL_MAPPING_FULL
:
276 ln
= lde_nbr_find(imsg
.hdr
.peerid
);
278 log_debug("%s: cannot find lde neighbor",
285 case IMSG_LABEL_MAPPING
:
286 case IMSG_LABEL_REQUEST
:
287 case IMSG_LABEL_RELEASE
:
288 case IMSG_LABEL_WITHDRAW
:
289 case IMSG_LABEL_ABORT
:
290 if (imsg
.hdr
.len
- IMSG_HEADER_SIZE
!=
292 fatalx("lde_dispatch_imsg: wrong imsg len");
295 ln
= lde_nbr_find(imsg
.hdr
.peerid
);
297 log_debug("%s: cannot find lde neighbor",
302 switch (imsg
.hdr
.type
) {
303 case IMSG_LABEL_MAPPING
:
304 lde_check_mapping(map
, ln
);
306 case IMSG_LABEL_REQUEST
:
307 lde_check_request(map
, ln
);
309 case IMSG_LABEL_RELEASE
:
310 lde_check_release(map
, ln
);
312 case IMSG_LABEL_WITHDRAW
:
313 lde_check_withdraw(map
, ln
);
315 case IMSG_LABEL_ABORT
:
320 case IMSG_ADDRESS_ADD
:
321 if (imsg
.hdr
.len
- IMSG_HEADER_SIZE
!=
322 sizeof(struct lde_addr
))
323 fatalx("lde_dispatch_imsg: wrong imsg len");
324 lde_addr
= imsg
.data
;
326 ln
= lde_nbr_find(imsg
.hdr
.peerid
);
328 log_debug("%s: cannot find lde neighbor",
332 if (lde_address_add(ln
, lde_addr
) < 0) {
333 log_debug("%s: cannot add address %s, it "
334 "already exists", __func__
,
335 log_addr(lde_addr
->af
, &lde_addr
->addr
));
338 case IMSG_ADDRESS_DEL
:
339 if (imsg
.hdr
.len
- IMSG_HEADER_SIZE
!=
340 sizeof(struct lde_addr
))
341 fatalx("lde_dispatch_imsg: wrong imsg len");
342 lde_addr
= imsg
.data
;
344 ln
= lde_nbr_find(imsg
.hdr
.peerid
);
346 log_debug("%s: cannot find lde neighbor",
350 if (lde_address_del(ln
, lde_addr
) < 0) {
351 log_debug("%s: cannot delete address %s, it "
352 "does not exist", __func__
,
353 log_addr(lde_addr
->af
, &lde_addr
->addr
));
356 case IMSG_NOTIFICATION
:
357 if (imsg
.hdr
.len
- IMSG_HEADER_SIZE
!=
358 sizeof(struct notify_msg
))
359 fatalx("lde_dispatch_imsg: wrong imsg len");
362 ln
= lde_nbr_find(imsg
.hdr
.peerid
);
364 log_debug("%s: cannot find lde neighbor",
369 switch (nm
->status_code
) {
371 l2vpn_recv_pw_status(ln
, nm
);
375 * Do nothing for now. Should be useful in
376 * the future when we implement LDP-IGP
377 * Synchronization (RFC 5443) and Graceful
378 * Restart (RFC 3478).
384 case IMSG_NEIGHBOR_UP
:
385 if (imsg
.hdr
.len
- IMSG_HEADER_SIZE
!=
386 sizeof(struct lde_nbr
))
387 fatalx("lde_dispatch_imsg: wrong imsg len");
389 if (lde_nbr_find(imsg
.hdr
.peerid
))
390 fatalx("lde_dispatch_imsg: "
391 "neighbor already exists");
392 lde_nbr_new(imsg
.hdr
.peerid
, imsg
.data
);
394 case IMSG_NEIGHBOR_DOWN
:
395 lde_nbr_del(lde_nbr_find(imsg
.hdr
.peerid
));
397 case IMSG_CTL_SHOW_LIB
:
398 rt_dump(imsg
.hdr
.pid
);
400 lde_imsg_compose_ldpe(IMSG_CTL_END
, 0,
401 imsg
.hdr
.pid
, NULL
, 0);
403 case IMSG_CTL_SHOW_L2VPN_PW
:
404 l2vpn_pw_ctl(imsg
.hdr
.pid
);
406 lde_imsg_compose_ldpe(IMSG_CTL_END
, 0,
407 imsg
.hdr
.pid
, NULL
, 0);
409 case IMSG_CTL_SHOW_L2VPN_BINDING
:
410 l2vpn_binding_ctl(imsg
.hdr
.pid
);
412 lde_imsg_compose_ldpe(IMSG_CTL_END
, 0,
413 imsg
.hdr
.pid
, NULL
, 0);
416 log_debug("%s: unexpected imsg %d", __func__
,
425 /* this pipe is dead, so remove the event handlers and exit */
426 THREAD_READ_OFF(iev
->ev_read
);
427 THREAD_WRITE_OFF(iev
->ev_write
);
436 lde_dispatch_parent(struct thread
*thread
)
438 static struct ldpd_conf
*nconf
;
439 struct iface
*iface
, *niface
;
441 struct nbr_params
*nnbrp
;
442 static struct l2vpn
*l2vpn
, *nl2vpn
;
443 struct l2vpn_if
*lif
, *nlif
;
444 struct l2vpn_pw
*pw
, *npw
;
448 int fd
= THREAD_FD(thread
);
449 struct imsgev
*iev
= THREAD_ARG(thread
);
450 struct imsgbuf
*ibuf
= &iev
->ibuf
;
457 if ((n
= imsg_read(ibuf
)) == -1 && errno
!= EAGAIN
)
458 fatal("imsg_read error");
459 if (n
== 0) /* connection closed */
463 if ((n
= imsg_get(ibuf
, &imsg
)) == -1)
464 fatal("lde_dispatch_parent: imsg_get error");
468 switch (imsg
.hdr
.type
) {
470 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
472 fatalx("IFSTATUS imsg with wrong len");
475 iface
= if_lookup_name(ldeconf
, kif
->ifname
);
477 if_update_info(iface
, kif
);
481 RB_FOREACH(l2vpn
, l2vpn_head
, &ldeconf
->l2vpn_tree
) {
482 lif
= l2vpn_if_find(l2vpn
, kif
->ifname
);
484 l2vpn_if_update_info(lif
, kif
);
487 pw
= l2vpn_pw_find(l2vpn
, kif
->ifname
);
489 l2vpn_pw_update_info(pw
, kif
);
495 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
496 sizeof(struct zapi_pw_status
))
497 fatalx("PW_UPDATE imsg with wrong len");
499 if (l2vpn_pw_status_update(imsg
.data
) != 0)
500 log_warnx("%s: error updating PW status",
503 case IMSG_NETWORK_ADD
:
504 case IMSG_NETWORK_UPDATE
:
505 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
506 sizeof(struct kroute
)) {
507 log_warnx("%s: wrong imsg len", __func__
);
514 fec
.type
= FEC_TYPE_IPV4
;
515 fec
.u
.ipv4
.prefix
= kr
->prefix
.v4
;
516 fec
.u
.ipv4
.prefixlen
= kr
->prefixlen
;
519 fec
.type
= FEC_TYPE_IPV6
;
520 fec
.u
.ipv6
.prefix
= kr
->prefix
.v6
;
521 fec
.u
.ipv6
.prefixlen
= kr
->prefixlen
;
524 fatalx("lde_dispatch_parent: unknown af");
527 switch (imsg
.hdr
.type
) {
528 case IMSG_NETWORK_ADD
:
529 lde_kernel_insert(&fec
, kr
->af
, &kr
->nexthop
,
530 kr
->ifindex
, kr
->priority
,
531 kr
->flags
& F_CONNECTED
, NULL
);
533 case IMSG_NETWORK_UPDATE
:
534 lde_kernel_update(&fec
);
538 case IMSG_SOCKET_IPC
:
540 log_warnx("%s: received unexpected imsg fd "
541 "to ldpe", __func__
);
544 if ((fd
= imsg
.fd
) == -1) {
545 log_warnx("%s: expected to receive imsg fd to "
546 "ldpe but didn't receive any", __func__
);
550 if ((iev_ldpe
= malloc(sizeof(struct imsgev
))) == NULL
)
552 imsg_init(&iev_ldpe
->ibuf
, fd
);
553 iev_ldpe
->handler_read
= lde_dispatch_imsg
;
554 iev_ldpe
->ev_read
= thread_add_read(master
,
555 iev_ldpe
->handler_read
, iev_ldpe
, iev_ldpe
->ibuf
.fd
);
556 iev_ldpe
->handler_write
= ldp_write_handler
;
557 iev_ldpe
->ev_write
= NULL
;
560 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
561 sizeof(struct ldpd_init
))
562 fatalx("INIT imsg with wrong len");
564 memcpy(&init
, imsg
.data
, sizeof(init
));
567 case IMSG_RECONF_CONF
:
568 if ((nconf
= malloc(sizeof(struct ldpd_conf
))) ==
571 memcpy(nconf
, imsg
.data
, sizeof(struct ldpd_conf
));
573 RB_INIT(&nconf
->iface_tree
);
574 RB_INIT(&nconf
->tnbr_tree
);
575 RB_INIT(&nconf
->nbrp_tree
);
576 RB_INIT(&nconf
->l2vpn_tree
);
578 case IMSG_RECONF_IFACE
:
579 if ((niface
= malloc(sizeof(struct iface
))) == NULL
)
581 memcpy(niface
, imsg
.data
, sizeof(struct iface
));
583 RB_INSERT(iface_head
, &nconf
->iface_tree
, niface
);
585 case IMSG_RECONF_TNBR
:
586 if ((ntnbr
= malloc(sizeof(struct tnbr
))) == NULL
)
588 memcpy(ntnbr
, imsg
.data
, sizeof(struct tnbr
));
590 RB_INSERT(tnbr_head
, &nconf
->tnbr_tree
, ntnbr
);
592 case IMSG_RECONF_NBRP
:
593 if ((nnbrp
= malloc(sizeof(struct nbr_params
))) == NULL
)
595 memcpy(nnbrp
, imsg
.data
, sizeof(struct nbr_params
));
597 RB_INSERT(nbrp_head
, &nconf
->nbrp_tree
, nnbrp
);
599 case IMSG_RECONF_L2VPN
:
600 if ((nl2vpn
= malloc(sizeof(struct l2vpn
))) == NULL
)
602 memcpy(nl2vpn
, imsg
.data
, sizeof(struct l2vpn
));
604 RB_INIT(&nl2vpn
->if_tree
);
605 RB_INIT(&nl2vpn
->pw_tree
);
606 RB_INIT(&nl2vpn
->pw_inactive_tree
);
608 RB_INSERT(l2vpn_head
, &nconf
->l2vpn_tree
, nl2vpn
);
610 case IMSG_RECONF_L2VPN_IF
:
611 if ((nlif
= malloc(sizeof(struct l2vpn_if
))) == NULL
)
613 memcpy(nlif
, imsg
.data
, sizeof(struct l2vpn_if
));
615 RB_INSERT(l2vpn_if_head
, &nl2vpn
->if_tree
, nlif
);
617 case IMSG_RECONF_L2VPN_PW
:
618 if ((npw
= malloc(sizeof(struct l2vpn_pw
))) == NULL
)
620 memcpy(npw
, imsg
.data
, sizeof(struct l2vpn_pw
));
622 RB_INSERT(l2vpn_pw_head
, &nl2vpn
->pw_tree
, npw
);
624 case IMSG_RECONF_L2VPN_IPW
:
625 if ((npw
= malloc(sizeof(struct l2vpn_pw
))) == NULL
)
627 memcpy(npw
, imsg
.data
, sizeof(struct l2vpn_pw
));
629 RB_INSERT(l2vpn_pw_head
, &nl2vpn
->pw_inactive_tree
, npw
);
631 case IMSG_RECONF_END
:
632 merge_config(ldeconf
, nconf
);
633 ldp_clear_config(nconf
);
636 case IMSG_DEBUG_UPDATE
:
637 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
639 log_warnx("%s: wrong imsg len", __func__
);
642 memcpy(&ldp_debug
, imsg
.data
, sizeof(ldp_debug
));
645 log_debug("%s: unexpected imsg %d", __func__
,
654 /* this pipe is dead, so remove the event handlers and exit */
655 THREAD_READ_OFF(iev
->ev_read
);
656 THREAD_WRITE_OFF(iev
->ev_write
);
664 lde_acl_check(char *acl_name
, int af
, union ldpd_addr
*addr
, uint8_t prefixlen
)
666 return ldp_acl_request(iev_main_sync
, acl_name
, af
, addr
, prefixlen
);
670 lde_update_label(struct fec_node
*fn
)
675 LIST_FOREACH(fnh
, &fn
->nexthops
, entry
) {
676 if (fnh
->flags
& F_FEC_NH_CONNECTED
) {
682 /* should we allocate a label for this fec? */
683 switch (fn
->fec
.type
) {
685 if ((ldeconf
->ipv4
.flags
& F_LDPD_AF_ALLOCHOSTONLY
) &&
686 fn
->fec
.u
.ipv4
.prefixlen
!= 32)
688 if (lde_acl_check(ldeconf
->ipv4
.acl_label_allocate_for
,
689 AF_INET
, (union ldpd_addr
*)&fn
->fec
.u
.ipv4
.prefix
,
690 fn
->fec
.u
.ipv4
.prefixlen
) != FILTER_PERMIT
)
694 if ((ldeconf
->ipv6
.flags
& F_LDPD_AF_ALLOCHOSTONLY
) &&
695 fn
->fec
.u
.ipv6
.prefixlen
!= 128)
697 if (lde_acl_check(ldeconf
->ipv6
.acl_label_allocate_for
,
698 AF_INET6
, (union ldpd_addr
*)&fn
->fec
.u
.ipv6
.prefix
,
699 fn
->fec
.u
.ipv6
.prefixlen
) != FILTER_PERMIT
)
707 /* choose implicit or explicit-null depending on configuration */
708 switch (fn
->fec
.type
) {
710 if (!(ldeconf
->ipv4
.flags
& F_LDPD_AF_EXPNULL
))
711 return (MPLS_LABEL_IMPLNULL
);
712 if (lde_acl_check(ldeconf
->ipv4
.acl_label_expnull_for
,
713 AF_INET
, (union ldpd_addr
*)&fn
->fec
.u
.ipv4
.prefix
,
714 fn
->fec
.u
.ipv4
.prefixlen
) != FILTER_PERMIT
)
715 return (MPLS_LABEL_IMPLNULL
);
716 return (MPLS_LABEL_IPV4NULL
);
718 if (!(ldeconf
->ipv6
.flags
& F_LDPD_AF_EXPNULL
))
719 return (MPLS_LABEL_IMPLNULL
);
720 if (lde_acl_check(ldeconf
->ipv6
.acl_label_expnull_for
,
721 AF_INET6
, (union ldpd_addr
*)&fn
->fec
.u
.ipv6
.prefix
,
722 fn
->fec
.u
.ipv6
.prefixlen
) != FILTER_PERMIT
)
723 return (MPLS_LABEL_IMPLNULL
);
724 return (MPLS_LABEL_IPV6NULL
);
726 fatalx("lde_update_label: unexpected fec type");
731 /* preserve current label if there's no need to update it */
732 if (fn
->local_label
!= NO_LABEL
&&
733 fn
->local_label
> MPLS_LABEL_RESERVED_MAX
)
734 return (fn
->local_label
);
736 return lde_get_next_label ();
740 lde_send_change_klabel(struct fec_node
*fn
, struct fec_nh
*fnh
)
746 switch (fn
->fec
.type
) {
748 memset(&kr
, 0, sizeof(kr
));
750 kr
.prefix
.v4
= fn
->fec
.u
.ipv4
.prefix
;
751 kr
.prefixlen
= fn
->fec
.u
.ipv4
.prefixlen
;
752 kr
.nexthop
.v4
= fnh
->nexthop
.v4
;
753 kr
.ifindex
= fnh
->ifindex
;
754 kr
.local_label
= fn
->local_label
;
755 kr
.remote_label
= fnh
->remote_label
;
756 kr
.priority
= fnh
->priority
;
758 lde_imsg_compose_parent(IMSG_KLABEL_CHANGE
, 0, &kr
,
762 memset(&kr
, 0, sizeof(kr
));
764 kr
.prefix
.v6
= fn
->fec
.u
.ipv6
.prefix
;
765 kr
.prefixlen
= fn
->fec
.u
.ipv6
.prefixlen
;
766 kr
.nexthop
.v6
= fnh
->nexthop
.v6
;
767 kr
.ifindex
= fnh
->ifindex
;
768 kr
.local_label
= fn
->local_label
;
769 kr
.remote_label
= fnh
->remote_label
;
770 kr
.priority
= fnh
->priority
;
772 lde_imsg_compose_parent(IMSG_KLABEL_CHANGE
, 0, &kr
,
776 if (fn
->local_label
== NO_LABEL
||
777 fnh
->remote_label
== NO_LABEL
)
780 pw
= (struct l2vpn_pw
*) fn
->data
;
782 zpw
.local_label
= fn
->local_label
;
783 zpw
.remote_label
= fnh
->remote_label
;
784 lde_imsg_compose_parent(IMSG_KPW_SET
, 0, &zpw
, sizeof(zpw
));
790 lde_send_delete_klabel(struct fec_node
*fn
, struct fec_nh
*fnh
)
796 switch (fn
->fec
.type
) {
798 memset(&kr
, 0, sizeof(kr
));
800 kr
.prefix
.v4
= fn
->fec
.u
.ipv4
.prefix
;
801 kr
.prefixlen
= fn
->fec
.u
.ipv4
.prefixlen
;
802 kr
.nexthop
.v4
= fnh
->nexthop
.v4
;
803 kr
.ifindex
= fnh
->ifindex
;
804 kr
.local_label
= fn
->local_label
;
805 kr
.remote_label
= fnh
->remote_label
;
806 kr
.priority
= fnh
->priority
;
808 lde_imsg_compose_parent(IMSG_KLABEL_DELETE
, 0, &kr
,
812 memset(&kr
, 0, sizeof(kr
));
814 kr
.prefix
.v6
= fn
->fec
.u
.ipv6
.prefix
;
815 kr
.prefixlen
= fn
->fec
.u
.ipv6
.prefixlen
;
816 kr
.nexthop
.v6
= fnh
->nexthop
.v6
;
817 kr
.ifindex
= fnh
->ifindex
;
818 kr
.local_label
= fn
->local_label
;
819 kr
.remote_label
= fnh
->remote_label
;
820 kr
.priority
= fnh
->priority
;
822 lde_imsg_compose_parent(IMSG_KLABEL_DELETE
, 0, &kr
,
826 pw
= (struct l2vpn_pw
*) fn
->data
;
828 zpw
.local_label
= fn
->local_label
;
829 zpw
.remote_label
= fnh
->remote_label
;
830 lde_imsg_compose_parent(IMSG_KPW_UNSET
, 0, &zpw
, sizeof(zpw
));
836 lde_fec2map(struct fec
*fec
, struct map
*map
)
838 memset(map
, 0, sizeof(*map
));
842 map
->type
= MAP_TYPE_PREFIX
;
843 map
->fec
.prefix
.af
= AF_INET
;
844 map
->fec
.prefix
.prefix
.v4
= fec
->u
.ipv4
.prefix
;
845 map
->fec
.prefix
.prefixlen
= fec
->u
.ipv4
.prefixlen
;
848 map
->type
= MAP_TYPE_PREFIX
;
849 map
->fec
.prefix
.af
= AF_INET6
;
850 map
->fec
.prefix
.prefix
.v6
= fec
->u
.ipv6
.prefix
;
851 map
->fec
.prefix
.prefixlen
= fec
->u
.ipv6
.prefixlen
;
854 map
->type
= MAP_TYPE_PWID
;
855 map
->fec
.pwid
.type
= fec
->u
.pwid
.type
;
856 map
->fec
.pwid
.group_id
= 0;
857 map
->flags
|= F_MAP_PW_ID
;
858 map
->fec
.pwid
.pwid
= fec
->u
.pwid
.pwid
;
864 lde_map2fec(struct map
*map
, struct in_addr lsr_id
, struct fec
*fec
)
866 memset(fec
, 0, sizeof(*fec
));
869 case MAP_TYPE_PREFIX
:
870 switch (map
->fec
.prefix
.af
) {
872 fec
->type
= FEC_TYPE_IPV4
;
873 fec
->u
.ipv4
.prefix
= map
->fec
.prefix
.prefix
.v4
;
874 fec
->u
.ipv4
.prefixlen
= map
->fec
.prefix
.prefixlen
;
877 fec
->type
= FEC_TYPE_IPV6
;
878 fec
->u
.ipv6
.prefix
= map
->fec
.prefix
.prefix
.v6
;
879 fec
->u
.ipv6
.prefixlen
= map
->fec
.prefix
.prefixlen
;
882 fatalx("lde_map2fec: unknown af");
887 fec
->type
= FEC_TYPE_PWID
;
888 fec
->u
.pwid
.type
= map
->fec
.pwid
.type
;
889 fec
->u
.pwid
.pwid
= map
->fec
.pwid
.pwid
;
890 fec
->u
.pwid
.lsr_id
= lsr_id
;
896 lde_send_labelmapping(struct lde_nbr
*ln
, struct fec_node
*fn
, int single
)
898 struct lde_wdraw
*lw
;
905 * We shouldn't send a new label mapping if we have a pending
906 * label release to receive. In this case, schedule to send a
907 * label mapping as soon as a label release is received.
909 lw
= (struct lde_wdraw
*)fec_find(&ln
->sent_wdraw
, &fn
->fec
);
911 if (!fec_find(&ln
->sent_map_pending
, &fn
->fec
)) {
912 debug_evt("%s: FEC %s: scheduling to send label "
913 "mapping later (waiting for pending label release)",
914 __func__
, log_fec(&fn
->fec
));
915 lde_map_pending_add(ln
, fn
);
921 * This function skips SL.1 - 3 and SL.9 - 14 because the label
922 * allocation is done way earlier (because of the merging nature of
926 lde_fec2map(&fn
->fec
, &map
);
927 switch (fn
->fec
.type
) {
931 if (lde_acl_check(ldeconf
->ipv4
.acl_label_advertise_to
,
932 AF_INET
, (union ldpd_addr
*)&ln
->id
, 32) != FILTER_PERMIT
)
934 if (lde_acl_check(ldeconf
->ipv4
.acl_label_advertise_for
,
935 AF_INET
, (union ldpd_addr
*)&fn
->fec
.u
.ipv4
.prefix
,
936 fn
->fec
.u
.ipv4
.prefixlen
) != FILTER_PERMIT
)
942 if (lde_acl_check(ldeconf
->ipv6
.acl_label_advertise_to
,
943 AF_INET
, (union ldpd_addr
*)&ln
->id
, 32) != FILTER_PERMIT
)
945 if (lde_acl_check(ldeconf
->ipv6
.acl_label_advertise_for
,
946 AF_INET6
, (union ldpd_addr
*)&fn
->fec
.u
.ipv6
.prefix
,
947 fn
->fec
.u
.ipv6
.prefixlen
) != FILTER_PERMIT
)
951 pw
= (struct l2vpn_pw
*) fn
->data
;
952 if (pw
== NULL
|| pw
->lsr_id
.s_addr
!= ln
->id
.s_addr
)
953 /* not the remote end of the pseudowire */
956 map
.flags
|= F_MAP_PW_IFMTU
;
957 map
.fec
.pwid
.ifmtu
= pw
->l2vpn
->mtu
;
958 if (pw
->flags
& F_PW_CWORD
)
959 map
.flags
|= F_MAP_PW_CWORD
;
960 if (pw
->flags
& F_PW_STATUSTLV
) {
961 map
.flags
|= F_MAP_PW_STATUS
;
962 map
.pw_status
= pw
->local_status
;
966 map
.label
= fn
->local_label
;
968 /* SL.6: is there a pending request for this mapping? */
969 lre
= (struct lde_req
*)fec_find(&ln
->recv_req
, &fn
->fec
);
971 /* set label request msg id in the mapping response. */
972 map
.requestid
= lre
->msg_id
;
973 map
.flags
= F_MAP_REQ_ID
;
975 /* SL.7: delete record of pending request */
976 lde_req_del(ln
, lre
, 0);
979 /* SL.4: send label mapping */
980 lde_imsg_compose_ldpe(IMSG_MAPPING_ADD
, ln
->peerid
, 0,
983 lde_imsg_compose_ldpe(IMSG_MAPPING_ADD_END
, ln
->peerid
, 0,
986 /* SL.5: record sent label mapping */
987 me
= (struct lde_map
*)fec_find(&ln
->sent_map
, &fn
->fec
);
989 me
= lde_map_add(ln
, fn
, 1);
994 lde_send_labelwithdraw(struct lde_nbr
*ln
, struct fec_node
*fn
,
995 struct map
*wcard
, struct status_tlv
*st
)
997 struct lde_wdraw
*lw
;
1000 struct l2vpn_pw
*pw
;
1003 lde_fec2map(&fn
->fec
, &map
);
1004 switch (fn
->fec
.type
) {
1006 if (!ln
->v4_enabled
)
1010 if (!ln
->v6_enabled
)
1014 pw
= (struct l2vpn_pw
*) fn
->data
;
1015 if (pw
== NULL
|| pw
->lsr_id
.s_addr
!= ln
->id
.s_addr
)
1016 /* not the remote end of the pseudowire */
1019 if (pw
->flags
& F_PW_CWORD
)
1020 map
.flags
|= F_MAP_PW_CWORD
;
1023 map
.label
= fn
->local_label
;
1025 memcpy(&map
, wcard
, sizeof(map
));
1028 map
.st
.status_code
= st
->status_code
;
1029 map
.st
.msg_id
= st
->msg_id
;
1030 map
.st
.msg_type
= st
->msg_type
;
1031 map
.flags
|= F_MAP_STATUS
;
1034 /* SWd.1: send label withdraw. */
1035 lde_imsg_compose_ldpe(IMSG_WITHDRAW_ADD
, ln
->peerid
, 0,
1037 lde_imsg_compose_ldpe(IMSG_WITHDRAW_ADD_END
, ln
->peerid
, 0, NULL
, 0);
1039 /* SWd.2: record label withdraw. */
1041 lw
= (struct lde_wdraw
*)fec_find(&ln
->sent_wdraw
, &fn
->fec
);
1043 lw
= lde_wdraw_add(ln
, fn
);
1044 lw
->label
= map
.label
;
1048 RB_FOREACH(f
, fec_tree
, &ft
) {
1049 fn
= (struct fec_node
*)f
;
1050 me
= (struct lde_map
*)fec_find(&ln
->sent_map
, &fn
->fec
);
1051 if (lde_wildcard_apply(wcard
, &fn
->fec
, me
) == 0)
1054 lw
= (struct lde_wdraw
*)fec_find(&ln
->sent_wdraw
,
1057 lw
= lde_wdraw_add(ln
, fn
);
1058 lw
->label
= map
.label
;
1064 lde_send_labelwithdraw_wcard(struct lde_nbr
*ln
, uint32_t label
)
1068 memset(&wcard
, 0, sizeof(wcard
));
1069 wcard
.type
= MAP_TYPE_WILDCARD
;
1070 wcard
.label
= label
;
1071 lde_send_labelwithdraw(ln
, NULL
, &wcard
, NULL
);
1075 lde_send_labelwithdraw_twcard_prefix(struct lde_nbr
*ln
, uint16_t af
,
1080 memset(&wcard
, 0, sizeof(wcard
));
1081 wcard
.type
= MAP_TYPE_TYPED_WCARD
;
1082 wcard
.fec
.twcard
.type
= MAP_TYPE_PREFIX
;
1083 wcard
.fec
.twcard
.u
.prefix_af
= af
;
1084 wcard
.label
= label
;
1085 lde_send_labelwithdraw(ln
, NULL
, &wcard
, NULL
);
1089 lde_send_labelwithdraw_twcard_pwid(struct lde_nbr
*ln
, uint16_t pw_type
,
1094 memset(&wcard
, 0, sizeof(wcard
));
1095 wcard
.type
= MAP_TYPE_TYPED_WCARD
;
1096 wcard
.fec
.twcard
.type
= MAP_TYPE_PWID
;
1097 wcard
.fec
.twcard
.u
.pw_type
= pw_type
;
1098 wcard
.label
= label
;
1099 lde_send_labelwithdraw(ln
, NULL
, &wcard
, NULL
);
1103 lde_send_labelwithdraw_pwid_wcard(struct lde_nbr
*ln
, uint16_t pw_type
,
1108 memset(&wcard
, 0, sizeof(wcard
));
1109 wcard
.type
= MAP_TYPE_PWID
;
1110 wcard
.fec
.pwid
.type
= pw_type
;
1111 wcard
.fec
.pwid
.group_id
= group_id
;
1112 /* we can not append a Label TLV when using PWid group wildcards. */
1113 wcard
.label
= NO_LABEL
;
1114 lde_send_labelwithdraw(ln
, NULL
, &wcard
, NULL
);
1118 lde_send_labelrelease(struct lde_nbr
*ln
, struct fec_node
*fn
,
1119 struct map
*wcard
, uint32_t label
)
1122 struct l2vpn_pw
*pw
;
1125 lde_fec2map(&fn
->fec
, &map
);
1126 switch (fn
->fec
.type
) {
1128 if (!ln
->v4_enabled
)
1132 if (!ln
->v6_enabled
)
1136 pw
= (struct l2vpn_pw
*) fn
->data
;
1137 if (pw
== NULL
|| pw
->lsr_id
.s_addr
!= ln
->id
.s_addr
)
1138 /* not the remote end of the pseudowire */
1141 if (pw
->flags
& F_PW_CWORD
)
1142 map
.flags
|= F_MAP_PW_CWORD
;
1146 memcpy(&map
, wcard
, sizeof(map
));
1149 lde_imsg_compose_ldpe(IMSG_RELEASE_ADD
, ln
->peerid
, 0,
1151 lde_imsg_compose_ldpe(IMSG_RELEASE_ADD_END
, ln
->peerid
, 0, NULL
, 0);
1155 lde_send_notification(struct lde_nbr
*ln
, uint32_t status_code
, uint32_t msg_id
,
1158 struct notify_msg nm
;
1160 memset(&nm
, 0, sizeof(nm
));
1161 nm
.status_code
= status_code
;
1162 /* 'msg_id' and 'msg_type' should be in network byte order */
1164 nm
.msg_type
= msg_type
;
1166 lde_imsg_compose_ldpe(IMSG_NOTIFICATION_SEND
, ln
->peerid
, 0,
1171 lde_send_notification_eol_prefix(struct lde_nbr
*ln
, int af
)
1173 struct notify_msg nm
;
1175 memset(&nm
, 0, sizeof(nm
));
1176 nm
.status_code
= S_ENDOFLIB
;
1177 nm
.fec
.type
= MAP_TYPE_TYPED_WCARD
;
1178 nm
.fec
.fec
.twcard
.type
= MAP_TYPE_PREFIX
;
1179 nm
.fec
.fec
.twcard
.u
.prefix_af
= af
;
1180 nm
.flags
|= F_NOTIF_FEC
;
1182 lde_imsg_compose_ldpe(IMSG_NOTIFICATION_SEND
, ln
->peerid
, 0,
1187 lde_send_notification_eol_pwid(struct lde_nbr
*ln
, uint16_t pw_type
)
1189 struct notify_msg nm
;
1191 memset(&nm
, 0, sizeof(nm
));
1192 nm
.status_code
= S_ENDOFLIB
;
1193 nm
.fec
.type
= MAP_TYPE_TYPED_WCARD
;
1194 nm
.fec
.fec
.twcard
.type
= MAP_TYPE_PWID
;
1195 nm
.fec
.fec
.twcard
.u
.pw_type
= pw_type
;
1196 nm
.flags
|= F_NOTIF_FEC
;
1198 lde_imsg_compose_ldpe(IMSG_NOTIFICATION_SEND
, ln
->peerid
, 0,
1203 lde_nbr_compare(struct lde_nbr
*a
, struct lde_nbr
*b
)
1205 return (a
->peerid
- b
->peerid
);
1208 static struct lde_nbr
*
1209 lde_nbr_new(uint32_t peerid
, struct lde_nbr
*new)
1213 if ((ln
= calloc(1, sizeof(*ln
))) == NULL
)
1217 ln
->v4_enabled
= new->v4_enabled
;
1218 ln
->v6_enabled
= new->v6_enabled
;
1219 ln
->flags
= new->flags
;
1220 ln
->peerid
= peerid
;
1221 fec_init(&ln
->recv_map
);
1222 fec_init(&ln
->sent_map
);
1223 fec_init(&ln
->sent_map_pending
);
1224 fec_init(&ln
->recv_req
);
1225 fec_init(&ln
->sent_req
);
1226 fec_init(&ln
->sent_wdraw
);
1228 TAILQ_INIT(&ln
->addr_list
);
1230 if (RB_INSERT(nbr_tree
, &lde_nbrs
, ln
) != NULL
)
1231 fatalx("lde_nbr_new: RB_INSERT failed");
1237 lde_nbr_del(struct lde_nbr
*ln
)
1240 struct fec_node
*fn
;
1242 struct l2vpn_pw
*pw
;
1247 /* uninstall received mappings */
1248 RB_FOREACH(f
, fec_tree
, &ft
) {
1249 fn
= (struct fec_node
*)f
;
1251 LIST_FOREACH(fnh
, &fn
->nexthops
, entry
) {
1255 if (!lde_address_find(ln
, fnh
->af
,
1260 if (f
->u
.pwid
.lsr_id
.s_addr
!= ln
->id
.s_addr
)
1262 pw
= (struct l2vpn_pw
*) fn
->data
;
1270 lde_send_delete_klabel(fn
, fnh
);
1271 fnh
->remote_label
= NO_LABEL
;
1275 lde_address_list_free(ln
);
1277 fec_clear(&ln
->recv_map
, lde_map_free
);
1278 fec_clear(&ln
->sent_map
, lde_map_free
);
1279 fec_clear(&ln
->sent_map_pending
, free
);
1280 fec_clear(&ln
->recv_req
, free
);
1281 fec_clear(&ln
->sent_req
, free
);
1282 fec_clear(&ln
->sent_wdraw
, free
);
1284 RB_REMOVE(nbr_tree
, &lde_nbrs
, ln
);
1289 static struct lde_nbr
*
1290 lde_nbr_find(uint32_t peerid
)
1296 return (RB_FIND(nbr_tree
, &lde_nbrs
, &ln
));
1300 lde_nbr_find_by_lsrid(struct in_addr addr
)
1304 RB_FOREACH(ln
, nbr_tree
, &lde_nbrs
)
1305 if (ln
->id
.s_addr
== addr
.s_addr
)
1312 lde_nbr_find_by_addr(int af
, union ldpd_addr
*addr
)
1316 RB_FOREACH(ln
, nbr_tree
, &lde_nbrs
)
1317 if (lde_address_find(ln
, af
, addr
) != NULL
)
1328 while ((ln
= RB_ROOT(&lde_nbrs
)) != NULL
)
1333 lde_nbr_addr_update(struct lde_nbr
*ln
, struct lde_addr
*lde_addr
, int removed
)
1336 struct fec_node
*fn
;
1340 RB_FOREACH(fec
, fec_tree
, &ln
->recv_map
) {
1341 fn
= (struct fec_node
*)fec_find(&ft
, fec
);
1342 switch (fec
->type
) {
1344 if (lde_addr
->af
!= AF_INET
)
1348 if (lde_addr
->af
!= AF_INET6
)
1355 LIST_FOREACH(fnh
, &fn
->nexthops
, entry
) {
1356 if (ldp_addrcmp(fnh
->af
, &fnh
->nexthop
,
1361 lde_send_delete_klabel(fn
, fnh
);
1362 fnh
->remote_label
= NO_LABEL
;
1364 me
= (struct lde_map
*)fec
;
1365 fnh
->remote_label
= me
->map
.label
;
1366 lde_send_change_klabel(fn
, fnh
);
1374 lde_map_compare(struct lde_map
*a
, struct lde_map
*b
)
1376 return (ldp_addrcmp(AF_INET
, (union ldpd_addr
*)&a
->nexthop
->id
,
1377 (union ldpd_addr
*)&b
->nexthop
->id
));
1381 lde_map_add(struct lde_nbr
*ln
, struct fec_node
*fn
, int sent
)
1385 me
= calloc(1, sizeof(*me
));
1393 RB_INSERT(lde_map_head
, &fn
->upstream
, me
);
1394 me
->head
= &fn
->upstream
;
1395 if (fec_insert(&ln
->sent_map
, &me
->fec
))
1396 log_warnx("failed to add %s to sent map",
1398 /* XXX on failure more cleanup is needed */
1400 RB_INSERT(lde_map_head
, &fn
->downstream
, me
);
1401 me
->head
= &fn
->downstream
;
1402 if (fec_insert(&ln
->recv_map
, &me
->fec
))
1403 log_warnx("failed to add %s to recv map",
1411 lde_map_del(struct lde_nbr
*ln
, struct lde_map
*me
, int sent
)
1414 fec_remove(&ln
->sent_map
, &me
->fec
);
1416 fec_remove(&ln
->recv_map
, &me
->fec
);
1422 lde_map_free(void *ptr
)
1424 struct lde_map
*map
= ptr
;
1426 RB_REMOVE(lde_map_head
, map
->head
, map
);
1431 lde_map_pending_add(struct lde_nbr
*ln
, struct fec_node
*fn
)
1435 map
= calloc(1, sizeof(*map
));
1440 if (fec_insert(&ln
->sent_map_pending
, map
))
1441 log_warnx("failed to add %s to sent map (pending)",
1448 lde_map_pending_del(struct lde_nbr
*ln
, struct fec
*map
)
1450 fec_remove(&ln
->sent_map_pending
, map
);
1455 lde_req_add(struct lde_nbr
*ln
, struct fec
*fec
, int sent
)
1458 struct lde_req
*lre
;
1460 t
= sent
? &ln
->sent_req
: &ln
->recv_req
;
1462 lre
= calloc(1, sizeof(*lre
));
1466 if (fec_insert(t
, &lre
->fec
)) {
1467 log_warnx("failed to add %s to %s req",
1468 log_fec(&lre
->fec
), sent
? "sent" : "recv");
1478 lde_req_del(struct lde_nbr
*ln
, struct lde_req
*lre
, int sent
)
1481 fec_remove(&ln
->sent_req
, &lre
->fec
);
1483 fec_remove(&ln
->recv_req
, &lre
->fec
);
1489 lde_wdraw_add(struct lde_nbr
*ln
, struct fec_node
*fn
)
1491 struct lde_wdraw
*lw
;
1493 lw
= calloc(1, sizeof(*lw
));
1499 if (fec_insert(&ln
->sent_wdraw
, &lw
->fec
))
1500 log_warnx("failed to add %s to sent wdraw",
1507 lde_wdraw_del(struct lde_nbr
*ln
, struct lde_wdraw
*lw
)
1509 fec_remove(&ln
->sent_wdraw
, &lw
->fec
);
1514 lde_change_egress_label(int af
)
1518 struct fec_node
*fn
;
1520 /* explicitly withdraw all null labels */
1521 RB_FOREACH(ln
, nbr_tree
, &lde_nbrs
) {
1522 lde_send_labelwithdraw_wcard(ln
, MPLS_LABEL_IMPLNULL
);
1524 lde_send_labelwithdraw_wcard(ln
, MPLS_LABEL_IPV4NULL
);
1526 lde_send_labelwithdraw_wcard(ln
, MPLS_LABEL_IPV6NULL
);
1529 /* update label of connected routes */
1530 RB_FOREACH(f
, fec_tree
, &ft
) {
1531 fn
= (struct fec_node
*)f
;
1532 if (fn
->local_label
> MPLS_LABEL_RESERVED_MAX
)
1537 if (fn
->fec
.type
!= FEC_TYPE_IPV4
)
1541 if (fn
->fec
.type
!= FEC_TYPE_IPV6
)
1545 fatalx("lde_change_egress_label: unknown af");
1548 fn
->local_label
= lde_update_label(fn
);
1549 if (fn
->local_label
!= NO_LABEL
)
1550 RB_FOREACH(ln
, nbr_tree
, &lde_nbrs
)
1551 lde_send_labelmapping(ln
, fn
, 0);
1553 RB_FOREACH(ln
, nbr_tree
, &lde_nbrs
)
1554 lde_imsg_compose_ldpe(IMSG_MAPPING_ADD_END
, ln
->peerid
, 0,
1559 lde_address_add(struct lde_nbr
*ln
, struct lde_addr
*lde_addr
)
1561 struct lde_addr
*new;
1563 if (lde_address_find(ln
, lde_addr
->af
, &lde_addr
->addr
) != NULL
)
1566 if ((new = calloc(1, sizeof(*new))) == NULL
)
1569 new->af
= lde_addr
->af
;
1570 new->addr
= lde_addr
->addr
;
1571 TAILQ_INSERT_TAIL(&ln
->addr_list
, new, entry
);
1573 /* reevaluate the previously received mappings from this neighbor */
1574 lde_nbr_addr_update(ln
, lde_addr
, 0);
1580 lde_address_del(struct lde_nbr
*ln
, struct lde_addr
*lde_addr
)
1582 lde_addr
= lde_address_find(ln
, lde_addr
->af
, &lde_addr
->addr
);
1583 if (lde_addr
== NULL
)
1586 /* reevaluate the previously received mappings from this neighbor */
1587 lde_nbr_addr_update(ln
, lde_addr
, 1);
1589 TAILQ_REMOVE(&ln
->addr_list
, lde_addr
, entry
);
1596 lde_address_find(struct lde_nbr
*ln
, int af
, union ldpd_addr
*addr
)
1598 struct lde_addr
*lde_addr
;
1600 TAILQ_FOREACH(lde_addr
, &ln
->addr_list
, entry
)
1601 if (lde_addr
->af
== af
&&
1602 ldp_addrcmp(af
, &lde_addr
->addr
, addr
) == 0)
1609 lde_address_list_free(struct lde_nbr
*ln
)
1611 struct lde_addr
*lde_addr
;
1613 while ((lde_addr
= TAILQ_FIRST(&ln
->addr_list
)) != NULL
) {
1614 TAILQ_REMOVE(&ln
->addr_list
, lde_addr
, entry
);
1620 lde_del_label_chunk(void *val
)
1625 lde_get_label_chunk(void)
1628 uint32_t start
, end
;
1630 log_debug("Getting label chunk");
1631 ret
= lm_get_label_chunk(zclient_sync
, 0, CHUNK_SIZE
, &start
, &end
);
1634 log_warnx("Error getting label chunk!");
1638 on_get_label_chunk_response(start
, end
);
1643 lde_label_list_init(void)
1645 label_chunk_list
= list_new();
1646 label_chunk_list
->del
= lde_del_label_chunk
;
1648 /* get first chunk */
1649 while (lde_get_label_chunk () != 0) {
1650 log_warnx("Error getting first label chunk!");
1656 on_get_label_chunk_response(uint32_t start
, uint32_t end
)
1658 struct label_chunk
*new_label_chunk
;
1660 log_debug("Label Chunk assign: %u - %u", start
, end
);
1662 new_label_chunk
= calloc(1, sizeof(struct label_chunk
));
1663 if (!new_label_chunk
) {
1664 log_warn("Error trying to allocate label chunk %u - %u", start
, end
);
1668 new_label_chunk
->start
= start
;
1669 new_label_chunk
->end
= end
;
1670 new_label_chunk
->used_mask
= 0;
1672 listnode_add(label_chunk_list
, (void *)new_label_chunk
);
1674 /* let's update current if needed */
1675 if (!current_label_chunk
)
1676 current_label_chunk
= listtail(label_chunk_list
);
1680 lde_get_next_label(void)
1682 struct label_chunk
*label_chunk
;
1683 uint32_t i
, pos
, size
;
1684 uint32_t label
= NO_LABEL
;
1686 while (current_label_chunk
) {
1687 label_chunk
= listgetdata(current_label_chunk
);
1691 /* try to get next free label in currently used label chunk */
1692 size
= label_chunk
->end
- label_chunk
->start
+ 1;
1693 for (i
= 0, pos
= 1; i
< size
; i
++, pos
<<= 1) {
1694 if (!(pos
& label_chunk
->used_mask
)) {
1695 label_chunk
->used_mask
|= pos
;
1696 label
= label_chunk
->start
+ i
;
1700 current_label_chunk
= listnextnode(current_label_chunk
);
1704 /* we moved till the last chunk, or were not able to find a label,
1705 so let's ask for another one */
1706 if (!current_label_chunk
|| current_label_chunk
== listtail(label_chunk_list
)
1707 || label
== NO_LABEL
) {
1708 if (lde_get_label_chunk() != 0)
1709 log_warn("%s: Error getting label chunk!", __func__
);