3 * Copyright (C) James R. Leu 2000
6 * This software is covered under the LGPL, for more
7 * info check out http://www.gnu.org/copyleft/lgpl.html
11 #include <netinet/in.h>
12 #include "ldp_struct.h"
13 #include "ldp_inet_addr.h"
14 #include "ldp_session.h"
15 #include "ldp_entity.h"
16 #include "ldp_global.h"
17 #include "ldp_nexthop.h"
18 #include "ldp_outlabel.h"
19 #include "ldp_inlabel.h"
20 #include "ldp_hello.h"
27 #include "ldp_label_mapping.h"
28 #include "ldp_tunnel.h"
29 #include "ldp_resource.h"
30 #include "ldp_hop_list.h"
32 #include "mpls_compare.h"
34 #include "mpls_socket_impl.h"
35 #include "mpls_timer_impl.h"
36 #include "mpls_ifmgr_impl.h"
37 #include "mpls_tree_impl.h"
38 #include "mpls_lock_impl.h"
39 #include "mpls_fib_impl.h"
40 #include "mpls_policy_impl.h"
41 #include "mpls_mm_impl.h"
42 #include "mpls_trace_impl.h"
47 #include "mpls_mpls_impl.h"
50 void _ldp_global_ifmgr_callback(mpls_cfg_handle handle
, const mpls_update_enum type
, mpls_inet_addr
*addr
)
52 ldp_session
*s
= NULL
;
53 ldp_global
*cfg
= (ldp_global
*)handle
;
55 LDP_ENTER(cfg
->user_data
, "_ldp_global_ifmgr_callback");
57 mpls_lock_get(cfg
->global_lock
);
59 if (mpls_policy_address_export_check(cfg
->user_data
, addr
) == MPLS_BOOL_TRUE
) {
60 s
= MPLS_LIST_HEAD(&cfg
->session
);
64 LDP_TRACE_LOG(cfg
->user_data
, MPLS_TRACE_STATE_ALL
,
65 LDP_TRACE_FLAG_EVENT
, "ADD\n");
66 ldp_addr_send(cfg
, s
, addr
);
69 LDP_TRACE_LOG(cfg
->user_data
, MPLS_TRACE_STATE_ALL
,
70 LDP_TRACE_FLAG_EVENT
, "DEL\n");
71 ldp_waddr_send(cfg
, s
, addr
);
76 s
= MPLS_LIST_NEXT(&cfg
->session
, s
, _global
);
80 mpls_lock_release(cfg
->global_lock
);
82 LDP_EXIT(cfg
->user_data
, "_ldp_global_ifmgr_callback");
85 ldp_global
*ldp_global_create(mpls_instance_handle data
)
87 ldp_global
*g
= (ldp_global
*) mpls_malloc(sizeof(ldp_global
));
90 memset(g
, 0, sizeof(ldp_global
));
92 LDP_ENTER(g
->user_data
, "ldp_global_create");
94 g
->global_lock
= mpls_lock_create("_ldp_global_lock_");
95 mpls_lock_get(g
->global_lock
);
97 MPLS_LIST_INIT(&g
->hop_list
, ldp_hop_list
);
98 MPLS_LIST_INIT(&g
->outlabel
, ldp_outlabel
);
99 MPLS_LIST_INIT(&g
->resource
, ldp_resource
);
100 MPLS_LIST_INIT(&g
->inlabel
, ldp_inlabel
);
101 MPLS_LIST_INIT(&g
->session
, ldp_session
);
102 MPLS_LIST_INIT(&g
->nexthop
, ldp_nexthop
);
103 MPLS_LIST_INIT(&g
->tunnel
, ldp_tunnel
);
104 MPLS_LIST_INIT(&g
->entity
, ldp_entity
);
105 MPLS_LIST_INIT(&g
->attr
, ldp_attr
);
106 MPLS_LIST_INIT(&g
->peer
, ldp_peer
);
107 MPLS_LIST_INIT(&g
->fec
, ldp_fec
);
108 MPLS_LIST_INIT(&g
->adj
, ldp_adj
);
109 MPLS_LIST_INIT(&g
->iff
, ldp_if
);
111 g
->message_identifier
= 1;
112 g
->configuration_sequence_number
= 1;
113 g
->lsp_control_mode
= LDP_GLOBAL_DEF_CONTROL_MODE
;
114 g
->label_retention_mode
= LDP_GLOBAL_DEF_RETENTION_MODE
;
115 g
->lsp_repair_mode
= LDP_GLOBAL_DEF_REPAIR_MODE
;
116 g
->propagate_release
= LDP_GLOBAL_DEF_PROPOGATE_RELEASE
;
117 g
->label_merge
= LDP_GLOBAL_DEF_LABEL_MERGE
;
118 g
->loop_detection_mode
= LDP_GLOBAL_DEF_LOOP_DETECTION_MODE
;
119 g
->ttl_less_domain
= LDP_GLOBAL_DEF_TTLLESS_DOMAIN
;
120 g
->local_tcp_port
= LDP_GLOBAL_DEF_LOCAL_TCP_PORT
;
121 g
->local_udp_port
= LDP_GLOBAL_DEF_LOCAL_UDP_PORT
;
122 g
->send_address_messages
= LDP_GLOBAL_DEF_SEND_ADDR_MSG
;
123 g
->backoff_step
= LDP_GLOBAL_DEF_BACKOFF_STEP
;
124 g
->send_lsrid_mapping
= LDP_GLOBAL_DEF_SEND_LSRID_MAPPING
;
125 g
->no_route_to_peer_time
= LDP_GLOBAL_DEF_NO_ROUTE_RETRY_TIME
;
127 g
->keepalive_timer
= LDP_ENTITY_DEF_KEEPALIVE_TIMER
;
128 g
->keepalive_interval
= LDP_ENTITY_DEF_KEEPALIVE_INTERVAL
;
129 g
->hellotime_timer
= LDP_ENTITY_DEF_HELLOTIME_TIMER
;
130 g
->hellotime_interval
= LDP_ENTITY_DEF_HELLOTIME_INTERVAL
;
132 g
->admin_state
= MPLS_ADMIN_DISABLE
;
135 mpls_lock_release(g
->global_lock
);
137 LDP_EXIT(g
->user_data
, "ldp_global_create");
143 mpls_return_enum
ldp_global_startup(ldp_global
* g
)
145 ldp_entity
*e
= NULL
;
148 MPLS_ASSERT(g
!= NULL
);
150 LDP_ENTER(g
->user_data
, "ldp_global_startup");
152 if (g
->lsr_identifier
.type
== MPLS_FAMILY_NONE
) {
153 LDP_TRACE_LOG(g
->user_data
, MPLS_TRACE_STATE_ALL
, LDP_TRACE_FLAG_ERROR
,
154 "ldp_global_startup: invalid LSRID\n");
155 goto ldp_global_startup_cleanup
;
158 g
->timer_handle
= mpls_timer_open(g
->user_data
);
159 if (mpls_timer_mgr_handle_verify(g
->timer_handle
) == MPLS_BOOL_FALSE
) {
160 goto ldp_global_startup_cleanup
;
163 g
->socket_handle
= mpls_socket_mgr_open(g
->user_data
);
164 if (mpls_socket_mgr_handle_verify(g
->socket_handle
) == MPLS_BOOL_FALSE
) {
165 goto ldp_global_startup_cleanup
;
168 g
->ifmgr_handle
= mpls_ifmgr_open(g
->user_data
, g
, _ldp_global_ifmgr_callback
);
169 if (mpls_ifmgr_handle_verify(g
->ifmgr_handle
) == MPLS_BOOL_FALSE
) {
170 goto ldp_global_startup_cleanup
;
173 g
->fib_handle
= mpls_fib_open(g
->user_data
, g
);
174 if (mpls_fib_handle_verify(g
->fib_handle
) == MPLS_BOOL_FALSE
) {
175 goto ldp_global_startup_cleanup
;
179 if (!g
->lsr_handle
) {
180 goto ldp_global_startup_cleanup
;
183 g
->mpls_handle
= mpls_mpls_open(g
->user_data
);
184 if (mpls_mpls_handle_verify(g
->mpls_handle
) == MPLS_BOOL_FALSE
) {
185 goto ldp_global_startup_cleanup
;
189 g
->addr_tree
= mpls_tree_create(32);
190 g
->fec_tree
= mpls_tree_create(32);
192 g
->hello_socket
= mpls_socket_create_udp(g
->socket_handle
);
193 if (mpls_socket_handle_verify(g
->socket_handle
, g
->hello_socket
) == MPLS_BOOL_FALSE
) {
194 LDP_TRACE_LOG(g
->user_data
, MPLS_TRACE_STATE_ALL
, LDP_TRACE_FLAG_DEBUG
,
195 "ldp_global_startup: error creating UDP socket\n");
196 goto ldp_global_startup_cleanup
;
199 dest
.addr
.type
= MPLS_FAMILY_IPV4
;
200 dest
.port
= g
->local_udp_port
;
201 dest
.addr
.u
.ipv4
= INADDR_ANY
;
202 // dest.addr.u.ipv4 = INADDR_ALLRTRS_GROUP;
204 if (mpls_socket_bind(g
->socket_handle
, g
->hello_socket
, &dest
) == MPLS_FAILURE
) {
205 LDP_TRACE_LOG(g
->user_data
, MPLS_TRACE_STATE_ALL
, LDP_TRACE_FLAG_DEBUG
,
206 "ldp_global_startup: error binding UDP socket\n");
207 goto ldp_global_startup_cleanup
;
210 if (mpls_socket_options(g
->socket_handle
, g
->hello_socket
,
211 MPLS_SOCKOP_NONBLOCK
| MPLS_SOCKOP_REUSE
) == MPLS_FAILURE
) {
212 LDP_TRACE_LOG(g
->user_data
, MPLS_TRACE_STATE_ALL
, LDP_TRACE_FLAG_DEBUG
,
213 "ldp_global_startup: error setting UDP socket options\n");
214 goto ldp_global_startup_cleanup
;
216 if (mpls_socket_multicast_options(g
->socket_handle
, g
->hello_socket
, 1, 0) ==
218 LDP_TRACE_LOG(g
->user_data
, MPLS_TRACE_STATE_ALL
, LDP_TRACE_FLAG_DEBUG
,
219 "ldp_global_startup: error setting UDP socket multicast options\n");
220 goto ldp_global_startup_cleanup
;
222 mpls_socket_readlist_add(g
->socket_handle
, g
->hello_socket
, 0, MPLS_SOCKET_UDP_DATA
);
224 g
->listen_socket
= mpls_socket_create_tcp(g
->socket_handle
);
225 if (mpls_socket_handle_verify(g
->socket_handle
, g
->listen_socket
) == MPLS_BOOL_FALSE
) {
226 LDP_TRACE_LOG(g
->user_data
, MPLS_TRACE_STATE_ALL
, LDP_TRACE_FLAG_DEBUG
,
227 "ldp_global_startup: error creating TCP socket\n");
228 goto ldp_global_startup_cleanup
;
230 if (mpls_socket_options(g
->socket_handle
, g
->listen_socket
,
231 MPLS_SOCKOP_NONBLOCK
| MPLS_SOCKOP_REUSE
) == MPLS_FAILURE
) {
232 LDP_TRACE_LOG(g
->user_data
, MPLS_TRACE_STATE_ALL
, LDP_TRACE_FLAG_DEBUG
,
233 "ldp_global_startup: error setting TCP socket options\n");
234 goto ldp_global_startup_cleanup
;
237 dest
.addr
.type
= MPLS_FAMILY_IPV4
;
238 dest
.port
= g
->local_tcp_port
;
239 dest
.addr
.u
.ipv4
= INADDR_ANY
;
241 if (mpls_socket_bind(g
->socket_handle
, g
->listen_socket
, &dest
) == MPLS_FAILURE
) {
242 LDP_TRACE_LOG(g
->user_data
, MPLS_TRACE_STATE_ALL
, LDP_TRACE_FLAG_DEBUG
,
243 "ldp_global_startup: error binding TCP socket\n");
244 goto ldp_global_startup_cleanup
;
247 if (mpls_socket_tcp_listen(g
->socket_handle
, g
->listen_socket
, 15) ==
249 LDP_TRACE_LOG(g
->user_data
, MPLS_TRACE_STATE_ALL
, LDP_TRACE_FLAG_DEBUG
,
250 "ldp_global_startup: error setting listen buffer for TCP socket\n");
251 goto ldp_global_startup_cleanup
;
254 mpls_socket_readlist_add(g
->socket_handle
, g
->listen_socket
, 0,
255 MPLS_SOCKET_TCP_LISTEN
);
257 e
= MPLS_LIST_HEAD(&g
->entity
);
259 ldp_entity_startup(g
, e
);
260 e
= MPLS_LIST_NEXT(&g
->entity
, e
, _global
);
263 g
->admin_state
= MPLS_ADMIN_ENABLE
;
265 LDP_EXIT(g
->user_data
, "ldp_global_startup");
268 ldp_global_startup_cleanup
:
269 ldp_global_shutdown(g
);
270 mpls_socket_close(g
->socket_handle
, g
->hello_socket
);
271 mpls_socket_close(g
->socket_handle
, g
->listen_socket
);
273 g
->listen_socket
= 0;
275 LDP_EXIT(g
->user_data
, "ldp_global_startup-error");
280 mpls_return_enum
ldp_global_shutdown(ldp_global
* g
)
282 ldp_entity
*e
= NULL
;
286 LDP_ENTER(g
->user_data
, "ldp_global_shutdown");
288 e
= MPLS_LIST_HEAD(&g
->entity
);
290 ldp_entity_shutdown(g
, e
, 1);
291 e
= MPLS_LIST_NEXT(&g
->entity
, e
, _global
);
294 g
->admin_state
= MPLS_ADMIN_DISABLE
;
296 mpls_socket_readlist_del(g
->socket_handle
, g
->hello_socket
);
297 mpls_socket_close(g
->socket_handle
, g
->hello_socket
);
299 mpls_socket_readlist_del(g
->socket_handle
, g
->listen_socket
);
300 mpls_socket_close(g
->socket_handle
, g
->listen_socket
);
302 mpls_tree_delete(g
->addr_tree
);
303 mpls_tree_delete(g
->fec_tree
);
305 mpls_lock_release(g
->global_lock
);
306 mpls_timer_close(g
->timer_handle
);
307 mpls_lock_get(g
->global_lock
);
309 mpls_socket_mgr_close(g
->socket_handle
);
310 mpls_ifmgr_close(g
->ifmgr_handle
);
311 mpls_fib_close(g
->fib_handle
);
315 mpls_mpls_close(g
->mpls_handle
);
318 LDP_EXIT(g
->user_data
, "ldp_global_shutdown");
323 mpls_return_enum
ldp_global_delete(ldp_global
* g
)
326 mpls_lock_delete(g
->global_lock
);
327 LDP_PRINT(g
->user_data
, "global delete\n");
333 void _ldp_global_add_attr(ldp_global
* g
, ldp_attr
* a
)
339 ap
= MPLS_LIST_HEAD(&g
->attr
);
341 if (ap
->index
> a
->index
) {
342 MPLS_LIST_INSERT_BEFORE(&g
->attr
, ap
, a
, _global
);
345 ap
= MPLS_LIST_NEXT(&g
->attr
, ap
, _global
);
347 MPLS_LIST_ADD_TAIL(&g
->attr
, a
, _global
, ldp_attr
);
350 void _ldp_global_del_attr(ldp_global
* g
, ldp_attr
* a
)
353 MPLS_LIST_REMOVE(&g
->attr
, a
, _global
);
354 MPLS_REFCNT_RELEASE(a
, ldp_attr_delete
);
357 void _ldp_global_add_peer(ldp_global
* g
, ldp_peer
* p
)
363 pp
= MPLS_LIST_HEAD(&g
->peer
);
365 if (pp
->index
> p
->index
) {
366 MPLS_LIST_INSERT_BEFORE(&g
->peer
, pp
, p
, _global
);
369 pp
= MPLS_LIST_NEXT(&g
->peer
, pp
, _global
);
371 MPLS_LIST_ADD_TAIL(&g
->peer
, p
, _global
, ldp_peer
);
374 void _ldp_global_del_peer(ldp_global
* g
, ldp_peer
* p
)
377 MPLS_LIST_REMOVE(&g
->peer
, p
, _global
);
378 MPLS_REFCNT_RELEASE(p
, ldp_peer_delete
);
382 * _ldp_global_add_if/del_if and _ldp_global_add_addr/del_addr are
383 * not the same as the rest of the global_add/del functions. They
384 * do not hold refcnts, they are used as part of the create and delete
385 * process of these structures
388 void _ldp_global_add_if(ldp_global
* g
, ldp_if
* i
)
393 ip
= MPLS_LIST_HEAD(&g
->iff
);
395 if (ip
->index
> i
->index
) {
396 MPLS_LIST_INSERT_BEFORE(&g
->iff
, ip
, i
, _global
);
399 ip
= MPLS_LIST_NEXT(&g
->iff
, ip
, _global
);
401 MPLS_LIST_ADD_TAIL(&g
->iff
, i
, _global
, ldp_if
);
404 void _ldp_global_del_if(ldp_global
* g
, ldp_if
* i
)
407 MPLS_LIST_REMOVE(&g
->iff
, i
, _global
);
410 void _ldp_global_add_addr(ldp_global
* g
, ldp_addr
* a
)
415 ap
= MPLS_LIST_HEAD(&g
->addr
);
417 if (ap
->index
> a
->index
) {
418 MPLS_LIST_INSERT_BEFORE(&g
->addr
, ap
, a
, _global
);
421 ap
= MPLS_LIST_NEXT(&g
->addr
, ap
, _global
);
423 MPLS_LIST_ADD_TAIL(&g
->addr
, a
, _global
, ldp_addr
);
426 void _ldp_global_del_addr(ldp_global
* g
, ldp_addr
* a
)
429 MPLS_LIST_REMOVE(&g
->addr
, a
, _global
);
434 void _ldp_global_add_adj(ldp_global
* g
, ldp_adj
* a
)
440 ap
= MPLS_LIST_HEAD(&g
->adj
);
442 if (ap
->index
> a
->index
) {
443 MPLS_LIST_INSERT_BEFORE(&g
->adj
, ap
, a
, _global
);
446 ap
= MPLS_LIST_NEXT(&g
->adj
, ap
, _global
);
448 MPLS_LIST_ADD_TAIL(&g
->adj
, a
, _global
, ldp_adj
);
451 void _ldp_global_del_adj(ldp_global
* g
, ldp_adj
* a
)
454 MPLS_LIST_REMOVE(&g
->adj
, a
, _global
);
455 MPLS_REFCNT_RELEASE(a
, ldp_adj_delete
);
458 void _ldp_global_add_entity(ldp_global
* g
, ldp_entity
* e
)
460 ldp_entity
*ep
= NULL
;
464 ep
= MPLS_LIST_HEAD(&g
->entity
);
466 if (ep
->index
> e
->index
) {
467 MPLS_LIST_INSERT_BEFORE(&g
->entity
, ep
, e
, _global
);
470 ep
= MPLS_LIST_NEXT(&g
->entity
, ep
, _global
);
472 MPLS_LIST_ADD_TAIL(&g
->entity
, e
, _global
, ldp_entity
);
475 void _ldp_global_del_entity(ldp_global
* g
, ldp_entity
* e
)
478 MPLS_LIST_REMOVE(&g
->entity
, e
, _global
);
479 MPLS_REFCNT_RELEASE(e
, ldp_entity_delete
);
482 void _ldp_global_add_session(ldp_global
* g
, ldp_session
* s
)
484 ldp_session
*sp
= NULL
;
488 s
->on_global
= MPLS_BOOL_TRUE
;
489 sp
= MPLS_LIST_HEAD(&g
->session
);
491 if (sp
->index
> s
->index
) {
492 MPLS_LIST_INSERT_BEFORE(&g
->session
, sp
, s
, _global
);
495 sp
= MPLS_LIST_NEXT(&g
->session
, sp
, _global
);
497 MPLS_LIST_ADD_TAIL(&g
->session
, s
, _global
, ldp_session
);
500 void _ldp_global_del_session(ldp_global
* g
, ldp_session
* s
)
503 MPLS_ASSERT(s
->on_global
== MPLS_BOOL_TRUE
);
504 MPLS_LIST_REMOVE(&g
->session
, s
, _global
);
505 s
->on_global
= MPLS_BOOL_FALSE
;
506 MPLS_REFCNT_RELEASE(s
, ldp_session_delete
);
509 mpls_return_enum
_ldp_global_add_inlabel(ldp_global
* g
, ldp_inlabel
* i
)
511 ldp_inlabel
*ip
= NULL
;
512 mpls_return_enum result
;
519 memcpy(&iseg
.info
,&i
->info
,sizeof(mpls_insegment
));
520 result
= lsr_cfg_insegment_set(g
->lsr_handle
, &iseg
, LSR_CFG_ADD
|
521 LSR_INSEGMENT_CFG_NPOP
|LSR_INSEGMENT_CFG_FAMILY
|
522 LSR_INSEGMENT_CFG_LABELSPACE
|LSR_INSEGMENT_CFG_LABEL
|
523 LSR_INSEGMENT_CFG_OWNER
);
524 memcpy(&i
->info
, &iseg
.info
, sizeof(mpls_insegment
));
525 i
->info
.handle
= iseg
.index
;
528 result
= mpls_mpls_insegment_add(g
->mpls_handle
, &i
->info
);
530 if (result
!= MPLS_SUCCESS
) {
535 ip
= MPLS_LIST_HEAD(&g
->inlabel
);
537 if (ip
->index
> i
->index
) {
538 MPLS_LIST_INSERT_BEFORE(&g
->inlabel
, ip
, i
, _global
);
541 ip
= MPLS_LIST_NEXT(&g
->inlabel
, ip
, _global
);
543 MPLS_LIST_ADD_TAIL(&g
->inlabel
, i
, _global
, ldp_inlabel
);
547 mpls_return_enum
_ldp_global_del_inlabel(ldp_global
* g
, ldp_inlabel
* i
)
550 MPLS_ASSERT(i
->reuse_count
== 0);
554 iseg
.index
= i
->info
.handle
;
555 lsr_cfg_insegment_set(g
->lsr_handle
, &iseg
, LSR_CFG_DEL
);
558 mpls_mpls_insegment_del(g
->mpls_handle
, &i
->info
);
560 MPLS_LIST_REMOVE(&g
->inlabel
, i
, _global
);
561 MPLS_REFCNT_RELEASE(i
, ldp_inlabel_delete
);
565 mpls_return_enum
_ldp_global_add_outlabel(ldp_global
* g
, ldp_outlabel
* o
)
567 ldp_outlabel
*op
= NULL
;
568 mpls_return_enum result
;
574 memcpy(&oseg
.info
, &o
->info
, sizeof(mpls_outsegment
));
575 result
= lsr_cfg_outsegment_set(g
->lsr_handle
, &oseg
, LSR_CFG_ADD
|
576 LSR_OUTSEGMENT_CFG_PUSH_LABEL
|LSR_OUTSEGMENT_CFG_OWNER
|
577 LSR_OUTSEGMENT_CFG_INTERFACE
|LSR_OUTSEGMENT_CFG_LABEL
|
578 LSR_OUTSEGMENT_CFG_NEXTHOP
);
579 o
->info
.handle
= oseg
.index
;
582 result
= mpls_mpls_outsegment_add(g
->mpls_handle
, &o
->info
);
585 if (result
!= MPLS_SUCCESS
) {
590 o
->switching
= MPLS_BOOL_TRUE
;
591 op
= MPLS_LIST_HEAD(&g
->outlabel
);
593 if (op
->index
> o
->index
) {
594 MPLS_LIST_INSERT_BEFORE(&g
->outlabel
, op
, o
, _global
);
597 op
= MPLS_LIST_NEXT(&g
->outlabel
, op
, _global
);
599 MPLS_LIST_ADD_TAIL(&g
->outlabel
, o
, _global
, ldp_outlabel
);
603 mpls_return_enum
_ldp_global_del_outlabel(ldp_global
* g
, ldp_outlabel
* o
)
609 oseg
.index
= o
->info
.handle
;
610 lsr_cfg_outsegment_set(g
->lsr_handle
, &oseg
, LSR_CFG_DEL
);
613 mpls_mpls_outsegment_del(g
->mpls_handle
, &o
->info
);
616 o
->switching
= MPLS_BOOL_FALSE
;
617 MPLS_ASSERT(o
->merge_count
== 0);
618 MPLS_LIST_REMOVE(&g
->outlabel
, o
, _global
);
619 MPLS_REFCNT_RELEASE(o
, ldp_outlabel_delete
);
623 mpls_return_enum
ldp_global_find_attr_index(ldp_global
* g
, uint32_t index
,
628 if (g
&& index
> 0) {
630 /* because we sort our inserts by index, this lets us know
631 if we've "walked" past the end of the list */
633 a
= MPLS_LIST_TAIL(&g
->attr
);
634 if (a
== NULL
|| a
->index
< index
) {
635 return MPLS_END_OF_LIST
;
639 a
= MPLS_LIST_HEAD(&g
->attr
);
641 if (a
->index
== index
) {
645 a
= MPLS_LIST_NEXT(&g
->attr
, a
, _global
);
652 mpls_return_enum
ldp_global_find_session_index(ldp_global
* g
, uint32_t index
,
653 ldp_session
** session
)
655 ldp_session
*s
= NULL
;
657 if (g
&& index
> 0) {
659 /* because we sort our inserts by index, this lets us know
660 if we've "walked" past the end of the list */
662 s
= MPLS_LIST_TAIL(&g
->session
);
663 if (s
== NULL
|| s
->index
< index
) {
665 return MPLS_END_OF_LIST
;
668 s
= MPLS_LIST_HEAD(&g
->session
);
670 if (s
->index
== index
) {
674 s
= MPLS_LIST_NEXT(&g
->session
, s
, _global
);
681 mpls_return_enum
ldp_global_find_inlabel_index(ldp_global
* g
, uint32_t index
,
682 ldp_inlabel
** inlabel
)
684 ldp_inlabel
*i
= NULL
;
686 if (g
&& index
> 0) {
688 /* because we sort our inserts by index, this lets us know
689 if we've "walked" past the end of the list */
691 i
= MPLS_LIST_TAIL(&g
->inlabel
);
692 if (i
== NULL
|| i
->index
< index
) {
694 return MPLS_END_OF_LIST
;
697 i
= MPLS_LIST_HEAD(&g
->inlabel
);
699 if (i
->index
== index
) {
703 i
= MPLS_LIST_NEXT(&g
->inlabel
, i
, _global
);
710 mpls_return_enum
ldp_global_find_outlabel_index(ldp_global
* g
, uint32_t index
,
711 ldp_outlabel
** outlabel
)
713 ldp_outlabel
*o
= NULL
;
715 if (g
&& index
> 0) {
717 /* because we sort our inserts by index, this lets us know
718 if we've "walked" past the end of the list */
720 o
= MPLS_LIST_TAIL(&g
->outlabel
);
721 if (o
== NULL
|| o
->index
< index
) {
723 return MPLS_END_OF_LIST
;
726 o
= MPLS_LIST_HEAD(&g
->outlabel
);
728 if (o
->index
== index
) {
732 o
= MPLS_LIST_NEXT(&g
->outlabel
, o
, _global
);
739 ldp_outlabel
*ldp_global_find_outlabel_handle(ldp_global
* g
,
740 mpls_outsegment_handle handle
)
742 ldp_outlabel
*o
= MPLS_LIST_HEAD(&g
->outlabel
);
746 if (!mpls_outsegment_handle_compare(o
->info
.handle
, handle
))
749 o
= MPLS_LIST_NEXT(&g
->outlabel
, o
, _global
);
755 mpls_return_enum
ldp_global_find_entity_index(ldp_global
* g
, uint32_t index
,
756 ldp_entity
** entity
)
758 ldp_entity
*e
= NULL
;
760 if (g
&& index
> 0) {
762 /* because we sort our inserts by index, this lets us know
763 if we've "walked" past the end of the list */
765 e
= MPLS_LIST_TAIL(&g
->entity
);
766 if (e
== NULL
|| e
->index
< index
) {
768 return MPLS_END_OF_LIST
;
771 e
= MPLS_LIST_HEAD(&g
->entity
);
773 if (e
->index
== index
) {
777 e
= MPLS_LIST_NEXT(&g
->entity
, e
, _global
);
784 ldp_peer
*ldp_global_find_peer_addr(ldp_global
* g
, mpls_inet_addr
* addr
)
788 MPLS_ASSERT(g
&& addr
);
790 /* JLEU: we will need to add a tree to optimize this search,
791 known peers will be in tree, unknown will take a "slow path" to
792 verify them, then be added to tree */
794 p
= MPLS_LIST_HEAD(&g
->peer
);
796 LDP_PRINT(g
->user_data
,
797 "ldp_global_find_peer_lsrid: peer: %08x lsrid: %08x\n",
798 p
->dest
.addr
.u
.ipv4
, addr
->u
.ipv4
);
799 if (!mpls_inet_addr_compare(&p
->dest
.addr
, addr
)) {
802 p
= MPLS_LIST_NEXT(&g
->peer
, p
, _global
);
807 mpls_return_enum
ldp_global_find_adj_index(ldp_global
* g
, uint32_t index
,
812 if (g
&& index
> 0) {
813 /* because we sort our inserts by index, this lets us know
814 if we've "walked" past the end of the list */
816 a
= MPLS_LIST_TAIL(&g
->adj
);
817 if (a
== NULL
|| a
->index
< index
) {
818 return MPLS_END_OF_LIST
;
822 a
= MPLS_LIST_HEAD(&g
->adj
);
824 if (a
->index
== index
) {
828 a
= MPLS_LIST_NEXT(&g
->adj
, a
, _global
);
835 mpls_return_enum
ldp_global_find_peer_index(ldp_global
* g
, uint32_t index
,
840 if (g
&& index
> 0) {
841 /* because we sort our inserts by index, this lets us know
842 if we've "walked" past the end of the list */
844 p
= MPLS_LIST_TAIL(&g
->peer
);
845 if (p
== NULL
|| p
->index
< index
) {
847 return MPLS_END_OF_LIST
;
850 p
= MPLS_LIST_HEAD(&g
->peer
);
852 if (p
->index
== index
) {
856 p
= MPLS_LIST_NEXT(&g
->peer
, p
, _global
);
863 mpls_return_enum
ldp_global_find_fec_index(ldp_global
* g
, uint32_t index
,
868 if (g
&& index
> 0) {
869 /* because we sort our inserts by index, this lets us know
870 if we've "walked" past the end of the list */
872 f
= MPLS_LIST_TAIL(&g
->fec
);
873 if (f
== NULL
|| f
->index
< index
) {
875 return MPLS_END_OF_LIST
;
878 f
= MPLS_LIST_HEAD(&g
->fec
);
880 if (f
->index
== index
) {
884 f
= MPLS_LIST_NEXT(&g
->fec
, f
, _global
);
891 mpls_return_enum
ldp_global_find_fec(ldp_global
* g
, mpls_fec
* m
,
898 f
= MPLS_LIST_HEAD(&g
->fec
);
900 if (!mpls_fec_compare(m
, &f
->info
)) {
904 } while((f
= MPLS_LIST_NEXT(&g
->fec
, f
, _global
)));
909 mpls_return_enum
ldp_global_find_if_index(ldp_global
* g
, uint32_t index
,
914 if (g
&& index
> 0) {
916 /* because we sort our inserts by index, this lets us know
917 if we've "walked" past the end of the list */
919 i
= MPLS_LIST_TAIL(&g
->iff
);
920 if (i
== NULL
|| i
->index
< index
) {
922 return MPLS_END_OF_LIST
;
925 i
= MPLS_LIST_HEAD(&g
->iff
);
927 if (i
->index
== index
) {
931 i
= MPLS_LIST_NEXT(&g
->iff
, i
, _global
);
938 ldp_if
*ldp_global_find_if_handle(ldp_global
* g
, mpls_if_handle handle
)
940 ldp_if
*i
= MPLS_LIST_HEAD(&g
->iff
);
944 if (!mpls_if_handle_compare(i
->handle
, handle
))
947 i
= MPLS_LIST_NEXT(&g
->iff
, i
, _global
);
953 ldp_adj
*ldp_global_find_adj_ldpid(ldp_global
* g
, mpls_inet_addr
* lsraddr
,
957 ldp_adj
*a
= MPLS_LIST_HEAD(&g
->adj
);
960 if ((!mpls_inet_addr_compare(lsraddr
, &a
->remote_lsr_address
)) &&
961 labelspace
== a
->remote_label_space
)
964 a
= MPLS_LIST_NEXT(&g
->adj
, a
, _global
);
969 mpls_return_enum
ldp_global_find_tunnel_index(ldp_global
* g
, uint32_t index
,
970 ldp_tunnel
** tunnel
)
972 ldp_tunnel
*t
= NULL
;
974 if (g
&& index
> 0) {
975 /* because we sort our inserts by index, this lets us know
976 if we've "walked" past the end of the list */
978 t
= MPLS_LIST_TAIL(&g
->tunnel
);
979 if (t
== NULL
|| t
->index
< index
) {
981 return MPLS_END_OF_LIST
;
984 t
= MPLS_LIST_HEAD(&g
->tunnel
);
986 if (t
->index
== index
) {
990 t
= MPLS_LIST_NEXT(&g
->tunnel
, t
, _global
);
997 mpls_return_enum
ldp_global_find_resource_index(ldp_global
* g
, uint32_t index
,
998 ldp_resource
** resource
)
1000 ldp_resource
*r
= NULL
;
1002 if (g
&& index
> 0) {
1003 /* because we sort our inserts by index, this lets us know
1004 if we've "walked" past the end of the list */
1006 r
= MPLS_LIST_TAIL(&g
->resource
);
1007 if (r
== NULL
|| r
->index
< index
) {
1009 return MPLS_END_OF_LIST
;
1012 r
= MPLS_LIST_HEAD(&g
->resource
);
1014 if (r
->index
== index
) {
1016 return MPLS_SUCCESS
;
1018 r
= MPLS_LIST_NEXT(&g
->resource
, r
, _global
);
1022 return MPLS_FAILURE
;
1025 mpls_return_enum
ldp_global_find_hop_list_index(ldp_global
* g
, uint32_t index
,
1026 ldp_hop_list
** hop_list
)
1028 ldp_hop_list
*h
= NULL
;
1030 if (g
&& index
> 0) {
1031 /* because we sort our inserts by index, this lets us know
1032 if we've "walked" past the end of the list */
1034 h
= MPLS_LIST_TAIL(&g
->hop_list
);
1035 if (h
== NULL
|| h
->index
< index
) {
1037 return MPLS_END_OF_LIST
;
1040 h
= MPLS_LIST_HEAD(&g
->hop_list
);
1042 if (h
->index
== index
) {
1044 return MPLS_SUCCESS
;
1046 h
= MPLS_LIST_NEXT(&g
->hop_list
, h
, _global
);
1050 return MPLS_FAILURE
;
1053 void _ldp_global_add_tunnel(ldp_global
* g
, ldp_tunnel
* t
)
1055 ldp_tunnel
*tp
= NULL
;
1057 MPLS_ASSERT(g
&& t
);
1058 MPLS_REFCNT_HOLD(t
);
1059 tp
= MPLS_LIST_HEAD(&g
->tunnel
);
1060 while (tp
!= NULL
) {
1061 if (tp
->index
> t
->index
) {
1062 MPLS_LIST_INSERT_BEFORE(&g
->tunnel
, tp
, t
, _global
);
1065 tp
= MPLS_LIST_NEXT(&g
->tunnel
, tp
, _global
);
1067 MPLS_LIST_ADD_TAIL(&g
->tunnel
, t
, _global
, ldp_tunnel
);
1070 void _ldp_global_del_tunnel(ldp_global
* g
, ldp_tunnel
* t
)
1072 MPLS_ASSERT(g
&& t
);
1073 MPLS_LIST_REMOVE(&g
->tunnel
, t
, _global
);
1074 MPLS_REFCNT_RELEASE(t
, ldp_tunnel_delete
);
1077 void _ldp_global_add_resource(ldp_global
* g
, ldp_resource
* r
)
1079 ldp_resource
*rp
= NULL
;
1081 MPLS_ASSERT(g
&& r
);
1082 MPLS_REFCNT_HOLD(r
);
1083 rp
= MPLS_LIST_HEAD(&g
->resource
);
1084 while (rp
!= NULL
) {
1085 if (rp
->index
> r
->index
) {
1086 MPLS_LIST_INSERT_BEFORE(&g
->resource
, rp
, r
, _global
);
1089 rp
= MPLS_LIST_NEXT(&g
->resource
, rp
, _global
);
1091 MPLS_LIST_ADD_TAIL(&g
->resource
, r
, _global
, ldp_resource
);
1094 void _ldp_global_del_resource(ldp_global
* g
, ldp_resource
* r
)
1096 MPLS_ASSERT(g
&& r
);
1097 MPLS_LIST_REMOVE(&g
->resource
, r
, _global
);
1098 MPLS_REFCNT_RELEASE(r
, ldp_resource_delete
);
1101 void _ldp_global_add_hop_list(ldp_global
* g
, ldp_hop_list
* h
)
1103 ldp_hop_list
*hp
= NULL
;
1105 MPLS_ASSERT(g
&& h
);
1106 MPLS_REFCNT_HOLD(h
);
1107 hp
= MPLS_LIST_HEAD(&g
->hop_list
);
1108 while (hp
!= NULL
) {
1109 if (hp
->index
> h
->index
) {
1110 MPLS_LIST_INSERT_BEFORE(&g
->hop_list
, hp
, h
, _global
);
1113 hp
= MPLS_LIST_NEXT(&g
->hop_list
, hp
, _global
);
1115 MPLS_LIST_ADD_TAIL(&g
->hop_list
, h
, _global
, ldp_hop_list
);
1118 void _ldp_global_del_hop_list(ldp_global
* g
, ldp_hop_list
* h
)
1120 MPLS_ASSERT(g
&& h
);
1121 MPLS_LIST_REMOVE(&g
->hop_list
, h
, _global
);
1122 MPLS_REFCNT_RELEASE(h
, ldp_hop_list_delete
);
1125 void _ldp_global_add_fec(ldp_global
* g
, ldp_fec
* f
)
1129 MPLS_ASSERT(g
&& f
);
1130 MPLS_REFCNT_HOLD(f
);
1131 fp
= MPLS_LIST_HEAD(&g
->fec
);
1132 while (fp
!= NULL
) {
1133 if (fp
->index
> f
->index
) {
1134 MPLS_LIST_INSERT_BEFORE(&g
->fec
, fp
, f
, _global
);
1137 fp
= MPLS_LIST_NEXT(&g
->fec
, fp
, _global
);
1139 MPLS_LIST_ADD_TAIL(&g
->fec
, f
, _global
, ldp_fec
);
1142 void _ldp_global_del_fec(ldp_global
* g
, ldp_fec
* f
)
1144 MPLS_ASSERT(g
&& f
);
1145 MPLS_LIST_REMOVE(&g
->fec
, f
, _global
);
1146 MPLS_REFCNT_RELEASE(f
, ldp_fec_delete
);
1149 void _ldp_global_add_nexthop(ldp_global
* g
, ldp_nexthop
* nh
)
1151 ldp_nexthop
*nhp
= NULL
;
1153 MPLS_ASSERT(g
&& nh
);
1154 MPLS_REFCNT_HOLD(nh
);
1155 nhp
= MPLS_LIST_HEAD(&g
->nexthop
);
1156 while (nhp
!= NULL
) {
1157 if (nhp
->index
> nh
->index
) {
1158 MPLS_LIST_INSERT_BEFORE(&g
->nexthop
, nhp
, nh
, _global
);
1161 nhp
= MPLS_LIST_NEXT(&g
->nexthop
, nhp
, _global
);
1163 MPLS_LIST_ADD_TAIL(&g
->nexthop
, nh
, _global
, ldp_nexthop
);
1166 void _ldp_global_del_nexthop(ldp_global
* g
, ldp_nexthop
* nh
)
1168 MPLS_ASSERT(g
&& nh
);
1169 MPLS_LIST_REMOVE(&g
->nexthop
, nh
, _global
);
1170 MPLS_REFCNT_RELEASE(nh
, ldp_nexthop_delete
);