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 ldp_global
*ldp_global_create(mpls_instance_handle data
)
52 ldp_global
*g
= (ldp_global
*) mpls_malloc(sizeof(ldp_global
));
55 memset(g
, 0, sizeof(ldp_global
));
57 LDP_ENTER(g
->user_data
, "ldp_global_create");
59 g
->global_lock
= mpls_lock_create("_ldp_global_lock_");
60 mpls_lock_get(g
->global_lock
);
62 MPLS_LIST_INIT(&g
->hop_list
, ldp_hop_list
);
63 MPLS_LIST_INIT(&g
->outlabel
, ldp_outlabel
);
64 MPLS_LIST_INIT(&g
->resource
, ldp_resource
);
65 MPLS_LIST_INIT(&g
->inlabel
, ldp_inlabel
);
66 MPLS_LIST_INIT(&g
->session
, ldp_session
);
67 MPLS_LIST_INIT(&g
->nexthop
, ldp_nexthop
);
68 MPLS_LIST_INIT(&g
->tunnel
, ldp_tunnel
);
69 MPLS_LIST_INIT(&g
->entity
, ldp_entity
);
70 MPLS_LIST_INIT(&g
->addr
, ldp_addr
);
71 MPLS_LIST_INIT(&g
->attr
, ldp_attr
);
72 MPLS_LIST_INIT(&g
->peer
, ldp_peer
);
73 MPLS_LIST_INIT(&g
->fec
, ldp_fec
);
74 MPLS_LIST_INIT(&g
->adj
, ldp_adj
);
75 MPLS_LIST_INIT(&g
->iff
, ldp_if
);
77 g
->message_identifier
= 1;
78 g
->configuration_sequence_number
= 1;
79 g
->lsp_control_mode
= LDP_GLOBAL_DEF_CONTROL_MODE
;
80 g
->label_retention_mode
= LDP_GLOBAL_DEF_RETENTION_MODE
;
81 g
->lsp_repair_mode
= LDP_GLOBAL_DEF_REPAIR_MODE
;
82 g
->propagate_release
= LDP_GLOBAL_DEF_PROPOGATE_RELEASE
;
83 g
->label_merge
= LDP_GLOBAL_DEF_LABEL_MERGE
;
84 g
->loop_detection_mode
= LDP_GLOBAL_DEF_LOOP_DETECTION_MODE
;
85 g
->ttl_less_domain
= LDP_GLOBAL_DEF_TTLLESS_DOMAIN
;
86 g
->local_tcp_port
= LDP_GLOBAL_DEF_LOCAL_TCP_PORT
;
87 g
->local_udp_port
= LDP_GLOBAL_DEF_LOCAL_UDP_PORT
;
88 g
->send_address_messages
= LDP_GLOBAL_DEF_SEND_ADDR_MSG
;
89 g
->backoff_step
= LDP_GLOBAL_DEF_BACKOFF_STEP
;
90 g
->send_lsrid_mapping
= LDP_GLOBAL_DEF_SEND_LSRID_MAPPING
;
91 g
->no_route_to_peer_time
= LDP_GLOBAL_DEF_NO_ROUTE_RETRY_TIME
;
93 g
->keepalive_timer
= LDP_ENTITY_DEF_KEEPALIVE_TIMER
;
94 g
->keepalive_interval
= LDP_ENTITY_DEF_KEEPALIVE_INTERVAL
;
95 g
->hellotime_timer
= LDP_ENTITY_DEF_HELLOTIME_TIMER
;
96 g
->hellotime_interval
= LDP_ENTITY_DEF_HELLOTIME_INTERVAL
;
98 g
->admin_state
= MPLS_ADMIN_DISABLE
;
101 g
->addr_tree
= mpls_tree_create(32);
102 g
->fec_tree
= mpls_tree_create(32);
104 mpls_lock_release(g
->global_lock
);
106 LDP_EXIT(g
->user_data
, "ldp_global_create");
112 mpls_return_enum
ldp_global_startup(ldp_global
* g
)
114 ldp_entity
*e
= NULL
;
117 MPLS_ASSERT(g
!= NULL
);
119 LDP_ENTER(g
->user_data
, "ldp_global_startup");
121 if (g
->lsr_identifier
.type
== MPLS_FAMILY_NONE
) {
122 LDP_TRACE_LOG(g
->user_data
, MPLS_TRACE_STATE_ALL
, LDP_TRACE_FLAG_ERROR
,
123 "ldp_global_startup: invalid LSRID\n");
124 goto ldp_global_startup_cleanup
;
127 g
->timer_handle
= mpls_timer_open(g
->user_data
);
128 if (mpls_timer_mgr_handle_verify(g
->timer_handle
) == MPLS_BOOL_FALSE
) {
129 goto ldp_global_startup_cleanup
;
132 g
->socket_handle
= mpls_socket_mgr_open(g
->user_data
);
133 if (mpls_socket_mgr_handle_verify(g
->socket_handle
) == MPLS_BOOL_FALSE
) {
134 goto ldp_global_startup_cleanup
;
137 g
->ifmgr_handle
= mpls_ifmgr_open(g
->user_data
, g
);
138 if (mpls_ifmgr_handle_verify(g
->ifmgr_handle
) == MPLS_BOOL_FALSE
) {
139 goto ldp_global_startup_cleanup
;
142 g
->fib_handle
= mpls_fib_open(g
->user_data
, g
);
143 if (mpls_fib_handle_verify(g
->fib_handle
) == MPLS_BOOL_FALSE
) {
144 goto ldp_global_startup_cleanup
;
148 if (!g
->lsr_handle
) {
149 goto ldp_global_startup_cleanup
;
152 g
->mpls_handle
= mpls_mpls_open(g
->user_data
);
153 if (mpls_mpls_handle_verify(g
->mpls_handle
) == MPLS_BOOL_FALSE
) {
154 goto ldp_global_startup_cleanup
;
158 g
->hello_socket
= mpls_socket_create_udp(g
->socket_handle
);
159 if (mpls_socket_handle_verify(g
->socket_handle
, g
->hello_socket
) == MPLS_BOOL_FALSE
) {
160 LDP_TRACE_LOG(g
->user_data
, MPLS_TRACE_STATE_ALL
, LDP_TRACE_FLAG_DEBUG
,
161 "ldp_global_startup: error creating UDP socket\n");
162 goto ldp_global_startup_cleanup
;
165 dest
.addr
.type
= MPLS_FAMILY_IPV4
;
166 dest
.port
= g
->local_udp_port
;
167 dest
.addr
.u
.ipv4
= INADDR_ANY
;
168 // dest.addr.u.ipv4 = INADDR_ALLRTRS_GROUP;
170 if (mpls_socket_bind(g
->socket_handle
, g
->hello_socket
, &dest
) == MPLS_FAILURE
) {
171 LDP_TRACE_LOG(g
->user_data
, MPLS_TRACE_STATE_ALL
, LDP_TRACE_FLAG_DEBUG
,
172 "ldp_global_startup: error binding UDP socket\n");
173 goto ldp_global_startup_cleanup
;
176 if (mpls_socket_options(g
->socket_handle
, g
->hello_socket
,
177 MPLS_SOCKOP_NONBLOCK
| MPLS_SOCKOP_REUSE
) == MPLS_FAILURE
) {
178 LDP_TRACE_LOG(g
->user_data
, MPLS_TRACE_STATE_ALL
, LDP_TRACE_FLAG_DEBUG
,
179 "ldp_global_startup: error setting UDP socket options\n");
180 goto ldp_global_startup_cleanup
;
182 if (mpls_socket_multicast_options(g
->socket_handle
, g
->hello_socket
, 1, 0) ==
184 LDP_TRACE_LOG(g
->user_data
, MPLS_TRACE_STATE_ALL
, LDP_TRACE_FLAG_DEBUG
,
185 "ldp_global_startup: error setting UDP socket multicast options\n");
186 goto ldp_global_startup_cleanup
;
188 mpls_socket_readlist_add(g
->socket_handle
, g
->hello_socket
, 0, MPLS_SOCKET_UDP_DATA
);
190 g
->listen_socket
= mpls_socket_create_tcp(g
->socket_handle
);
191 if (mpls_socket_handle_verify(g
->socket_handle
, g
->listen_socket
) == MPLS_BOOL_FALSE
) {
192 LDP_TRACE_LOG(g
->user_data
, MPLS_TRACE_STATE_ALL
, LDP_TRACE_FLAG_DEBUG
,
193 "ldp_global_startup: error creating TCP socket\n");
194 goto ldp_global_startup_cleanup
;
196 if (mpls_socket_options(g
->socket_handle
, g
->listen_socket
,
197 MPLS_SOCKOP_NONBLOCK
| MPLS_SOCKOP_REUSE
) == MPLS_FAILURE
) {
198 LDP_TRACE_LOG(g
->user_data
, MPLS_TRACE_STATE_ALL
, LDP_TRACE_FLAG_DEBUG
,
199 "ldp_global_startup: error setting TCP socket options\n");
200 goto ldp_global_startup_cleanup
;
203 dest
.addr
.type
= MPLS_FAMILY_IPV4
;
204 dest
.port
= g
->local_tcp_port
;
205 dest
.addr
.u
.ipv4
= INADDR_ANY
;
207 if (mpls_socket_bind(g
->socket_handle
, g
->listen_socket
, &dest
) == MPLS_FAILURE
) {
208 LDP_TRACE_LOG(g
->user_data
, MPLS_TRACE_STATE_ALL
, LDP_TRACE_FLAG_DEBUG
,
209 "ldp_global_startup: error binding TCP socket\n");
210 goto ldp_global_startup_cleanup
;
213 if (mpls_socket_tcp_listen(g
->socket_handle
, g
->listen_socket
, 15) ==
215 LDP_TRACE_LOG(g
->user_data
, MPLS_TRACE_STATE_ALL
, LDP_TRACE_FLAG_DEBUG
,
216 "ldp_global_startup: error setting listen buffer for TCP socket\n");
217 goto ldp_global_startup_cleanup
;
220 mpls_socket_readlist_add(g
->socket_handle
, g
->listen_socket
, 0,
221 MPLS_SOCKET_TCP_LISTEN
);
223 g
->admin_state
= MPLS_ADMIN_ENABLE
;
225 e
= MPLS_LIST_HEAD(&g
->entity
);
227 ldp_entity_startup(g
, e
);
228 e
= MPLS_LIST_NEXT(&g
->entity
, e
, _global
);
231 LDP_EXIT(g
->user_data
, "ldp_global_startup");
234 ldp_global_startup_cleanup
:
235 ldp_global_shutdown(g
);
236 mpls_socket_close(g
->socket_handle
, g
->hello_socket
);
237 mpls_socket_close(g
->socket_handle
, g
->listen_socket
);
239 g
->listen_socket
= 0;
241 LDP_EXIT(g
->user_data
, "ldp_global_startup-error");
246 mpls_return_enum
ldp_global_shutdown(ldp_global
* g
)
248 ldp_entity
*e
= NULL
;
256 LDP_ENTER(g
->user_data
, "ldp_global_shutdown");
258 e
= MPLS_LIST_HEAD(&g
->entity
);
260 ldp_entity_shutdown(g
, e
, 1);
261 e
= MPLS_LIST_NEXT(&g
->entity
, e
, _global
);
264 g
->admin_state
= MPLS_ADMIN_DISABLE
;
266 mpls_socket_readlist_del(g
->socket_handle
, g
->hello_socket
);
267 mpls_socket_close(g
->socket_handle
, g
->hello_socket
);
269 mpls_socket_readlist_del(g
->socket_handle
, g
->listen_socket
);
270 mpls_socket_close(g
->socket_handle
, g
->listen_socket
);
272 mpls_lock_release(g
->global_lock
);
273 mpls_timer_close(g
->timer_handle
);
274 mpls_lock_get(g
->global_lock
);
276 mpls_socket_mgr_close(g
->socket_handle
);
277 mpls_ifmgr_close(g
->ifmgr_handle
);
278 mpls_fib_close(g
->fib_handle
);
282 mpls_mpls_close(g
->mpls_handle
);
285 LDP_EXIT(g
->user_data
, "ldp_global_shutdown");
290 mpls_return_enum
ldp_global_delete(ldp_global
* g
)
293 /* need to properly purge FECs/nexthops/interfaces/addresses */
294 mpls_tree_delete(g
->addr_tree
);
295 mpls_tree_delete(g
->fec_tree
);
297 mpls_lock_delete(g
->global_lock
);
298 LDP_PRINT(g
->user_data
, "global delete\n");
304 void _ldp_global_add_attr(ldp_global
* g
, ldp_attr
* a
)
310 ap
= MPLS_LIST_HEAD(&g
->attr
);
312 if (ap
->index
> a
->index
) {
313 MPLS_LIST_INSERT_BEFORE(&g
->attr
, ap
, a
, _global
);
316 ap
= MPLS_LIST_NEXT(&g
->attr
, ap
, _global
);
318 MPLS_LIST_ADD_TAIL(&g
->attr
, a
, _global
, ldp_attr
);
321 void _ldp_global_del_attr(ldp_global
* g
, ldp_attr
* a
)
324 MPLS_LIST_REMOVE(&g
->attr
, a
, _global
);
325 MPLS_REFCNT_RELEASE(a
, ldp_attr_delete
);
328 void _ldp_global_add_peer(ldp_global
* g
, ldp_peer
* p
)
334 pp
= MPLS_LIST_HEAD(&g
->peer
);
336 if (pp
->index
> p
->index
) {
337 MPLS_LIST_INSERT_BEFORE(&g
->peer
, pp
, p
, _global
);
340 pp
= MPLS_LIST_NEXT(&g
->peer
, pp
, _global
);
342 MPLS_LIST_ADD_TAIL(&g
->peer
, p
, _global
, ldp_peer
);
345 void _ldp_global_del_peer(ldp_global
* g
, ldp_peer
* p
)
348 MPLS_LIST_REMOVE(&g
->peer
, p
, _global
);
349 MPLS_REFCNT_RELEASE(p
, ldp_peer_delete
);
353 * _ldp_global_add_if/del_if and _ldp_global_add_addr/del_addr are
354 * not the same as the rest of the global_add/del functions. They
355 * do not hold refcnts, they are used as part of the create and delete
356 * process of these structures
359 void _ldp_global_add_if(ldp_global
* g
, ldp_if
* i
)
364 ip
= MPLS_LIST_HEAD(&g
->iff
);
366 if (ip
->index
> i
->index
) {
367 MPLS_LIST_INSERT_BEFORE(&g
->iff
, ip
, i
, _global
);
370 ip
= MPLS_LIST_NEXT(&g
->iff
, ip
, _global
);
372 MPLS_LIST_ADD_TAIL(&g
->iff
, i
, _global
, ldp_if
);
375 void _ldp_global_del_if(ldp_global
* g
, ldp_if
* i
)
378 MPLS_LIST_REMOVE(&g
->iff
, i
, _global
);
381 void _ldp_global_add_addr(ldp_global
* g
, ldp_addr
* a
)
386 ap
= MPLS_LIST_HEAD(&g
->addr
);
388 if (ap
->index
> a
->index
) {
389 MPLS_LIST_INSERT_BEFORE(&g
->addr
, ap
, a
, _global
);
392 ap
= MPLS_LIST_NEXT(&g
->addr
, ap
, _global
);
394 MPLS_LIST_ADD_TAIL(&g
->addr
, a
, _global
, ldp_addr
);
397 void _ldp_global_del_addr(ldp_global
* g
, ldp_addr
* a
)
400 MPLS_LIST_REMOVE(&g
->addr
, a
, _global
);
403 void _ldp_global_add_adj(ldp_global
* g
, ldp_adj
* a
)
409 ap
= MPLS_LIST_HEAD(&g
->adj
);
411 if (ap
->index
> a
->index
) {
412 MPLS_LIST_INSERT_BEFORE(&g
->adj
, ap
, a
, _global
);
415 ap
= MPLS_LIST_NEXT(&g
->adj
, ap
, _global
);
417 MPLS_LIST_ADD_TAIL(&g
->adj
, a
, _global
, ldp_adj
);
420 void _ldp_global_del_adj(ldp_global
* g
, ldp_adj
* a
)
423 MPLS_LIST_REMOVE(&g
->adj
, a
, _global
);
424 MPLS_REFCNT_RELEASE(a
, ldp_adj_delete
);
427 void _ldp_global_add_entity(ldp_global
* g
, ldp_entity
* e
)
429 ldp_entity
*ep
= NULL
;
433 ep
= MPLS_LIST_HEAD(&g
->entity
);
435 if (ep
->index
> e
->index
) {
436 MPLS_LIST_INSERT_BEFORE(&g
->entity
, ep
, e
, _global
);
439 ep
= MPLS_LIST_NEXT(&g
->entity
, ep
, _global
);
441 MPLS_LIST_ADD_TAIL(&g
->entity
, e
, _global
, ldp_entity
);
444 void _ldp_global_del_entity(ldp_global
* g
, ldp_entity
* e
)
447 MPLS_LIST_REMOVE(&g
->entity
, e
, _global
);
448 MPLS_REFCNT_RELEASE(e
, ldp_entity_delete
);
451 void _ldp_global_add_session(ldp_global
* g
, ldp_session
* s
)
453 ldp_session
*sp
= NULL
;
457 s
->on_global
= MPLS_BOOL_TRUE
;
458 sp
= MPLS_LIST_HEAD(&g
->session
);
460 if (sp
->index
> s
->index
) {
461 MPLS_LIST_INSERT_BEFORE(&g
->session
, sp
, s
, _global
);
464 sp
= MPLS_LIST_NEXT(&g
->session
, sp
, _global
);
466 MPLS_LIST_ADD_TAIL(&g
->session
, s
, _global
, ldp_session
);
469 void _ldp_global_del_session(ldp_global
* g
, ldp_session
* s
)
472 MPLS_ASSERT(s
->on_global
== MPLS_BOOL_TRUE
);
473 MPLS_LIST_REMOVE(&g
->session
, s
, _global
);
474 s
->on_global
= MPLS_BOOL_FALSE
;
475 MPLS_REFCNT_RELEASE(s
, ldp_session_delete
);
478 mpls_return_enum
_ldp_global_add_inlabel(ldp_global
* g
, ldp_inlabel
* i
)
480 ldp_inlabel
*ip
= NULL
;
481 mpls_return_enum result
;
488 memcpy(&iseg
.info
,&i
->info
,sizeof(mpls_insegment
));
489 result
= lsr_cfg_insegment_set(g
->lsr_handle
, &iseg
, LSR_CFG_ADD
|
490 LSR_INSEGMENT_CFG_NPOP
|LSR_INSEGMENT_CFG_FAMILY
|
491 LSR_INSEGMENT_CFG_LABELSPACE
|LSR_INSEGMENT_CFG_LABEL
|
492 LSR_INSEGMENT_CFG_OWNER
);
493 memcpy(&i
->info
, &iseg
.info
, sizeof(mpls_insegment
));
494 i
->info
.handle
= iseg
.index
;
497 result
= mpls_mpls_insegment_add(g
->mpls_handle
, &i
->info
);
499 if (result
!= MPLS_SUCCESS
) {
504 ip
= MPLS_LIST_HEAD(&g
->inlabel
);
506 if (ip
->index
> i
->index
) {
507 MPLS_LIST_INSERT_BEFORE(&g
->inlabel
, ip
, i
, _global
);
510 ip
= MPLS_LIST_NEXT(&g
->inlabel
, ip
, _global
);
512 MPLS_LIST_ADD_TAIL(&g
->inlabel
, i
, _global
, ldp_inlabel
);
516 mpls_return_enum
_ldp_global_del_inlabel(ldp_global
* g
, ldp_inlabel
* i
)
519 MPLS_ASSERT(i
->reuse_count
== 0);
523 iseg
.index
= i
->info
.handle
;
524 lsr_cfg_insegment_set(g
->lsr_handle
, &iseg
, LSR_CFG_DEL
);
527 mpls_mpls_insegment_del(g
->mpls_handle
, &i
->info
);
529 MPLS_LIST_REMOVE(&g
->inlabel
, i
, _global
);
530 MPLS_REFCNT_RELEASE(i
, ldp_inlabel_delete
);
534 mpls_return_enum
_ldp_global_add_outlabel(ldp_global
* g
, ldp_outlabel
* o
)
536 ldp_outlabel
*op
= NULL
;
537 mpls_return_enum result
;
543 memcpy(&oseg
.info
, &o
->info
, sizeof(mpls_outsegment
));
544 result
= lsr_cfg_outsegment_set(g
->lsr_handle
, &oseg
, LSR_CFG_ADD
|
545 LSR_OUTSEGMENT_CFG_PUSH_LABEL
|LSR_OUTSEGMENT_CFG_OWNER
|
546 LSR_OUTSEGMENT_CFG_INTERFACE
|LSR_OUTSEGMENT_CFG_LABEL
|
547 LSR_OUTSEGMENT_CFG_NEXTHOP
);
548 o
->info
.handle
= oseg
.index
;
551 result
= mpls_mpls_outsegment_add(g
->mpls_handle
, &o
->info
);
554 if (result
!= MPLS_SUCCESS
) {
559 o
->switching
= MPLS_BOOL_TRUE
;
560 op
= MPLS_LIST_HEAD(&g
->outlabel
);
562 if (op
->index
> o
->index
) {
563 MPLS_LIST_INSERT_BEFORE(&g
->outlabel
, op
, o
, _global
);
566 op
= MPLS_LIST_NEXT(&g
->outlabel
, op
, _global
);
568 MPLS_LIST_ADD_TAIL(&g
->outlabel
, o
, _global
, ldp_outlabel
);
572 mpls_return_enum
_ldp_global_del_outlabel(ldp_global
* g
, ldp_outlabel
* o
)
578 oseg
.index
= o
->info
.handle
;
579 lsr_cfg_outsegment_set(g
->lsr_handle
, &oseg
, LSR_CFG_DEL
);
582 mpls_mpls_outsegment_del(g
->mpls_handle
, &o
->info
);
585 o
->switching
= MPLS_BOOL_FALSE
;
586 MPLS_ASSERT(o
->merge_count
== 0);
587 MPLS_LIST_REMOVE(&g
->outlabel
, o
, _global
);
588 MPLS_REFCNT_RELEASE(o
, ldp_outlabel_delete
);
592 mpls_return_enum
ldp_global_find_attr_index(ldp_global
* g
, uint32_t index
,
597 if (g
&& index
> 0) {
599 /* because we sort our inserts by index, this lets us know
600 if we've "walked" past the end of the list */
602 a
= MPLS_LIST_TAIL(&g
->attr
);
603 if (a
== NULL
|| a
->index
< index
) {
604 return MPLS_END_OF_LIST
;
608 a
= MPLS_LIST_HEAD(&g
->attr
);
610 if (a
->index
== index
) {
614 a
= MPLS_LIST_NEXT(&g
->attr
, a
, _global
);
621 mpls_return_enum
ldp_global_find_session_index(ldp_global
* g
, uint32_t index
,
622 ldp_session
** session
)
624 ldp_session
*s
= NULL
;
626 if (g
&& index
> 0) {
628 /* because we sort our inserts by index, this lets us know
629 if we've "walked" past the end of the list */
631 s
= MPLS_LIST_TAIL(&g
->session
);
632 if (s
== NULL
|| s
->index
< index
) {
634 return MPLS_END_OF_LIST
;
637 s
= MPLS_LIST_HEAD(&g
->session
);
639 if (s
->index
== index
) {
643 s
= MPLS_LIST_NEXT(&g
->session
, s
, _global
);
650 mpls_return_enum
ldp_global_find_inlabel_index(ldp_global
* g
, uint32_t index
,
651 ldp_inlabel
** inlabel
)
653 ldp_inlabel
*i
= NULL
;
655 if (g
&& index
> 0) {
657 /* because we sort our inserts by index, this lets us know
658 if we've "walked" past the end of the list */
660 i
= MPLS_LIST_TAIL(&g
->inlabel
);
661 if (i
== NULL
|| i
->index
< index
) {
663 return MPLS_END_OF_LIST
;
666 i
= MPLS_LIST_HEAD(&g
->inlabel
);
668 if (i
->index
== index
) {
672 i
= MPLS_LIST_NEXT(&g
->inlabel
, i
, _global
);
679 mpls_return_enum
ldp_global_find_outlabel_index(ldp_global
* g
, uint32_t index
,
680 ldp_outlabel
** outlabel
)
682 ldp_outlabel
*o
= NULL
;
684 if (g
&& index
> 0) {
686 /* because we sort our inserts by index, this lets us know
687 if we've "walked" past the end of the list */
689 o
= MPLS_LIST_TAIL(&g
->outlabel
);
690 if (o
== NULL
|| o
->index
< index
) {
692 return MPLS_END_OF_LIST
;
695 o
= MPLS_LIST_HEAD(&g
->outlabel
);
697 if (o
->index
== index
) {
701 o
= MPLS_LIST_NEXT(&g
->outlabel
, o
, _global
);
708 ldp_outlabel
*ldp_global_find_outlabel_handle(ldp_global
* g
,
709 mpls_outsegment_handle handle
)
711 ldp_outlabel
*o
= MPLS_LIST_HEAD(&g
->outlabel
);
715 if (!mpls_outsegment_handle_compare(o
->info
.handle
, handle
))
718 o
= MPLS_LIST_NEXT(&g
->outlabel
, o
, _global
);
724 mpls_return_enum
ldp_global_find_entity_index(ldp_global
* g
, uint32_t index
,
725 ldp_entity
** entity
)
727 ldp_entity
*e
= NULL
;
729 if (g
&& index
> 0) {
731 /* because we sort our inserts by index, this lets us know
732 if we've "walked" past the end of the list */
734 e
= MPLS_LIST_TAIL(&g
->entity
);
735 if (e
== NULL
|| e
->index
< index
) {
737 return MPLS_END_OF_LIST
;
740 e
= MPLS_LIST_HEAD(&g
->entity
);
742 if (e
->index
== index
) {
746 e
= MPLS_LIST_NEXT(&g
->entity
, e
, _global
);
753 ldp_peer
*ldp_global_find_peer_addr(ldp_global
* g
, mpls_inet_addr
* addr
)
757 MPLS_ASSERT(g
&& addr
);
759 /* JLEU: we will need to add a tree to optimize this search,
760 known peers will be in tree, unknown will take a "slow path" to
761 verify them, then be added to tree */
763 p
= MPLS_LIST_HEAD(&g
->peer
);
765 LDP_PRINT(g
->user_data
,
766 "ldp_global_find_peer_lsrid: peer: %08x lsrid: %08x\n",
767 p
->dest
.addr
.u
.ipv4
, addr
->u
.ipv4
);
768 if (!mpls_inet_addr_compare(&p
->dest
.addr
, addr
)) {
771 p
= MPLS_LIST_NEXT(&g
->peer
, p
, _global
);
776 mpls_return_enum
ldp_global_find_adj_index(ldp_global
* g
, uint32_t index
,
781 if (g
&& index
> 0) {
782 /* because we sort our inserts by index, this lets us know
783 if we've "walked" past the end of the list */
785 a
= MPLS_LIST_TAIL(&g
->adj
);
786 if (a
== NULL
|| a
->index
< index
) {
787 return MPLS_END_OF_LIST
;
791 a
= MPLS_LIST_HEAD(&g
->adj
);
793 if (a
->index
== index
) {
797 a
= MPLS_LIST_NEXT(&g
->adj
, a
, _global
);
804 mpls_return_enum
ldp_global_find_peer_index(ldp_global
* g
, uint32_t index
,
809 if (g
&& index
> 0) {
810 /* because we sort our inserts by index, this lets us know
811 if we've "walked" past the end of the list */
813 p
= MPLS_LIST_TAIL(&g
->peer
);
814 if (p
== NULL
|| p
->index
< index
) {
816 return MPLS_END_OF_LIST
;
819 p
= MPLS_LIST_HEAD(&g
->peer
);
821 if (p
->index
== index
) {
825 p
= MPLS_LIST_NEXT(&g
->peer
, p
, _global
);
832 mpls_return_enum
ldp_global_find_fec_index(ldp_global
* g
, uint32_t index
,
837 if (g
&& index
> 0) {
838 /* because we sort our inserts by index, this lets us know
839 if we've "walked" past the end of the list */
841 f
= MPLS_LIST_TAIL(&g
->fec
);
842 if (f
== NULL
|| f
->index
< index
) {
844 return MPLS_END_OF_LIST
;
847 f
= MPLS_LIST_HEAD(&g
->fec
);
849 if (f
->index
== index
) {
853 f
= MPLS_LIST_NEXT(&g
->fec
, f
, _global
);
860 mpls_return_enum
ldp_global_find_fec(ldp_global
* g
, mpls_fec
* m
,
867 f
= MPLS_LIST_HEAD(&g
->fec
);
869 if (!mpls_fec_compare(m
, &f
->info
)) {
873 } while((f
= MPLS_LIST_NEXT(&g
->fec
, f
, _global
)));
878 mpls_return_enum
ldp_global_find_addr_index(ldp_global
* g
, uint32_t index
,
883 if (g
&& index
> 0) {
885 /* because we sort our inserts by index, this lets us know
886 if we've "walked" past the end of the list */
888 a
= MPLS_LIST_TAIL(&g
->addr
);
889 if (a
== NULL
|| a
->index
< index
) {
891 return MPLS_END_OF_LIST
;
894 a
= MPLS_LIST_HEAD(&g
->addr
);
896 if (a
->index
== index
) {
900 a
= MPLS_LIST_NEXT(&g
->addr
, a
, _global
);
907 mpls_return_enum
ldp_global_find_if_index(ldp_global
* g
, uint32_t index
,
912 if (g
&& index
> 0) {
914 /* because we sort our inserts by index, this lets us know
915 if we've "walked" past the end of the list */
917 i
= MPLS_LIST_TAIL(&g
->iff
);
918 if (i
== NULL
|| i
->index
< index
) {
920 return MPLS_END_OF_LIST
;
923 i
= MPLS_LIST_HEAD(&g
->iff
);
925 if (i
->index
== index
) {
929 i
= MPLS_LIST_NEXT(&g
->iff
, i
, _global
);
936 ldp_if
*ldp_global_find_if_handle(ldp_global
* g
, mpls_if_handle handle
)
938 ldp_if
*i
= MPLS_LIST_HEAD(&g
->iff
);
942 if (!mpls_if_handle_compare(i
->handle
, handle
))
945 i
= MPLS_LIST_NEXT(&g
->iff
, i
, _global
);
951 ldp_adj
*ldp_global_find_adj_ldpid(ldp_global
* g
, mpls_inet_addr
* lsraddr
,
955 ldp_adj
*a
= MPLS_LIST_HEAD(&g
->adj
);
958 if ((!mpls_inet_addr_compare(lsraddr
, &a
->remote_lsr_address
)) &&
959 labelspace
== a
->remote_label_space
)
962 a
= MPLS_LIST_NEXT(&g
->adj
, a
, _global
);
967 mpls_return_enum
ldp_global_find_tunnel_index(ldp_global
* g
, uint32_t index
,
968 ldp_tunnel
** tunnel
)
970 ldp_tunnel
*t
= NULL
;
972 if (g
&& index
> 0) {
973 /* because we sort our inserts by index, this lets us know
974 if we've "walked" past the end of the list */
976 t
= MPLS_LIST_TAIL(&g
->tunnel
);
977 if (t
== NULL
|| t
->index
< index
) {
979 return MPLS_END_OF_LIST
;
982 t
= MPLS_LIST_HEAD(&g
->tunnel
);
984 if (t
->index
== index
) {
988 t
= MPLS_LIST_NEXT(&g
->tunnel
, t
, _global
);
995 mpls_return_enum
ldp_global_find_resource_index(ldp_global
* g
, uint32_t index
,
996 ldp_resource
** resource
)
998 ldp_resource
*r
= NULL
;
1000 if (g
&& index
> 0) {
1001 /* because we sort our inserts by index, this lets us know
1002 if we've "walked" past the end of the list */
1004 r
= MPLS_LIST_TAIL(&g
->resource
);
1005 if (r
== NULL
|| r
->index
< index
) {
1007 return MPLS_END_OF_LIST
;
1010 r
= MPLS_LIST_HEAD(&g
->resource
);
1012 if (r
->index
== index
) {
1014 return MPLS_SUCCESS
;
1016 r
= MPLS_LIST_NEXT(&g
->resource
, r
, _global
);
1020 return MPLS_FAILURE
;
1023 mpls_return_enum
ldp_global_find_hop_list_index(ldp_global
* g
, uint32_t index
,
1024 ldp_hop_list
** hop_list
)
1026 ldp_hop_list
*h
= NULL
;
1028 if (g
&& index
> 0) {
1029 /* because we sort our inserts by index, this lets us know
1030 if we've "walked" past the end of the list */
1032 h
= MPLS_LIST_TAIL(&g
->hop_list
);
1033 if (h
== NULL
|| h
->index
< index
) {
1035 return MPLS_END_OF_LIST
;
1038 h
= MPLS_LIST_HEAD(&g
->hop_list
);
1040 if (h
->index
== index
) {
1042 return MPLS_SUCCESS
;
1044 h
= MPLS_LIST_NEXT(&g
->hop_list
, h
, _global
);
1048 return MPLS_FAILURE
;
1051 void _ldp_global_add_tunnel(ldp_global
* g
, ldp_tunnel
* t
)
1053 ldp_tunnel
*tp
= NULL
;
1055 MPLS_ASSERT(g
&& t
);
1056 MPLS_REFCNT_HOLD(t
);
1057 tp
= MPLS_LIST_HEAD(&g
->tunnel
);
1058 while (tp
!= NULL
) {
1059 if (tp
->index
> t
->index
) {
1060 MPLS_LIST_INSERT_BEFORE(&g
->tunnel
, tp
, t
, _global
);
1063 tp
= MPLS_LIST_NEXT(&g
->tunnel
, tp
, _global
);
1065 MPLS_LIST_ADD_TAIL(&g
->tunnel
, t
, _global
, ldp_tunnel
);
1068 void _ldp_global_del_tunnel(ldp_global
* g
, ldp_tunnel
* t
)
1070 MPLS_ASSERT(g
&& t
);
1071 MPLS_LIST_REMOVE(&g
->tunnel
, t
, _global
);
1072 MPLS_REFCNT_RELEASE(t
, ldp_tunnel_delete
);
1075 void _ldp_global_add_resource(ldp_global
* g
, ldp_resource
* r
)
1077 ldp_resource
*rp
= NULL
;
1079 MPLS_ASSERT(g
&& r
);
1080 MPLS_REFCNT_HOLD(r
);
1081 rp
= MPLS_LIST_HEAD(&g
->resource
);
1082 while (rp
!= NULL
) {
1083 if (rp
->index
> r
->index
) {
1084 MPLS_LIST_INSERT_BEFORE(&g
->resource
, rp
, r
, _global
);
1087 rp
= MPLS_LIST_NEXT(&g
->resource
, rp
, _global
);
1089 MPLS_LIST_ADD_TAIL(&g
->resource
, r
, _global
, ldp_resource
);
1092 void _ldp_global_del_resource(ldp_global
* g
, ldp_resource
* r
)
1094 MPLS_ASSERT(g
&& r
);
1095 MPLS_LIST_REMOVE(&g
->resource
, r
, _global
);
1096 MPLS_REFCNT_RELEASE(r
, ldp_resource_delete
);
1099 void _ldp_global_add_hop_list(ldp_global
* g
, ldp_hop_list
* h
)
1101 ldp_hop_list
*hp
= NULL
;
1103 MPLS_ASSERT(g
&& h
);
1104 MPLS_REFCNT_HOLD(h
);
1105 hp
= MPLS_LIST_HEAD(&g
->hop_list
);
1106 while (hp
!= NULL
) {
1107 if (hp
->index
> h
->index
) {
1108 MPLS_LIST_INSERT_BEFORE(&g
->hop_list
, hp
, h
, _global
);
1111 hp
= MPLS_LIST_NEXT(&g
->hop_list
, hp
, _global
);
1113 MPLS_LIST_ADD_TAIL(&g
->hop_list
, h
, _global
, ldp_hop_list
);
1116 void _ldp_global_del_hop_list(ldp_global
* g
, ldp_hop_list
* h
)
1118 MPLS_ASSERT(g
&& h
);
1119 MPLS_LIST_REMOVE(&g
->hop_list
, h
, _global
);
1120 MPLS_REFCNT_RELEASE(h
, ldp_hop_list_delete
);
1123 void _ldp_global_add_fec(ldp_global
* g
, ldp_fec
* f
)
1127 MPLS_ASSERT(g
&& f
);
1129 * TESTING: jleu 6/7/2004, since I want the FEC to be cleaned up
1130 * when it no longer has a nexthop, addr, or label, the only things that
1131 * should increment the ref are those (nh, addr, label etc), not global
1132 * nor inserting into the tree. I also added this comment in
1134 MPLS_REFCNT_HOLD(f);
1136 fp
= MPLS_LIST_HEAD(&g
->fec
);
1137 while (fp
!= NULL
) {
1138 if (fp
->index
> f
->index
) {
1139 MPLS_LIST_INSERT_BEFORE(&g
->fec
, fp
, f
, _global
);
1142 fp
= MPLS_LIST_NEXT(&g
->fec
, fp
, _global
);
1144 MPLS_LIST_ADD_TAIL(&g
->fec
, f
, _global
, ldp_fec
);
1147 void _ldp_global_del_fec(ldp_global
* g
, ldp_fec
* f
)
1149 MPLS_ASSERT(g
&& f
);
1150 MPLS_LIST_REMOVE(&g
->fec
, f
, _global
);
1153 void _ldp_global_add_nexthop(ldp_global
* g
, ldp_nexthop
* nh
)
1155 ldp_nexthop
*nhp
= NULL
;
1157 MPLS_ASSERT(g
&& nh
);
1158 nhp
= MPLS_LIST_HEAD(&g
->nexthop
);
1159 while (nhp
!= NULL
) {
1160 if (nhp
->index
> nh
->index
) {
1161 MPLS_LIST_INSERT_BEFORE(&g
->nexthop
, nhp
, nh
, _global
);
1164 nhp
= MPLS_LIST_NEXT(&g
->nexthop
, nhp
, _global
);
1166 MPLS_LIST_ADD_TAIL(&g
->nexthop
, nh
, _global
, ldp_nexthop
);
1169 void _ldp_global_del_nexthop(ldp_global
* g
, ldp_nexthop
* nh
)
1171 MPLS_ASSERT(g
&& nh
);
1172 MPLS_LIST_REMOVE(&g
->nexthop
, nh
, _global
);