3 * Copyright (C) James R. Leu 2003
6 * This software is covered under the LGPL, for more
7 * info check out http://www.gnu.org/copyleft/lgpl.html
10 #include "ldp_struct.h"
14 #include "ldp_session.h"
15 #include "ldp_outlabel.h"
16 #include "ldp_global.h"
17 #include "mpls_assert.h"
18 #include "mpls_compare.h"
19 #include "mpls_mm_impl.h"
20 #include "mpls_tree_impl.h"
21 #include "mpls_policy_impl.h"
22 #include "mpls_trace_impl.h"
27 #include "mpls_mpls_impl.h"
30 static uint32_t _ldp_nexthop_next_index
= 1;
31 static uint32_t _ldp_nexthop_get_next_index();
33 void mpls_nexthop2ldp_nexthop(mpls_nexthop
*mnh
, ldp_nexthop
*lnh
)
35 memcpy(&lnh
->info
, mnh
, sizeof(mpls_nexthop
));
38 ldp_nexthop
*ldp_nexthop_for_fec_session(ldp_fec
*fec
, ldp_session
*s
)
40 ldp_nexthop
*nh
= MPLS_LIST_HEAD(&fec
->nh_root
);
43 sp
= ldp_session_for_nexthop(nh
);
44 if (sp
&& (sp
->index
== s
->index
)) {
47 nh
= MPLS_LIST_NEXT(&fec
->nh_root
, nh
, _fec
);
52 void ldp_nexthop_delete(ldp_global
*g
, ldp_nexthop
*nh
)
54 fprintf(stderr
, "nexthop delete: %p\n", nh
);
55 MPLS_REFCNT_ASSERT(nh
, 0);
56 _ldp_global_del_nexthop(g
, nh
);
60 ldp_nexthop
*ldp_nexthop_create(ldp_global
*g
, mpls_nexthop
*n
)
62 ldp_nexthop
*nh
= (ldp_nexthop
*) mpls_malloc(sizeof(ldp_nexthop
));
65 memset(nh
, 0, sizeof(ldp_nexthop
));
66 MPLS_REFCNT_INIT(nh
, 0);
67 MPLS_LIST_INIT(&nh
->outlabel_root
, ldp_outlabel
);
68 MPLS_LIST_ELEM_INIT(nh
, _global
);
69 MPLS_LIST_ELEM_INIT(nh
, _fec
);
70 MPLS_LIST_ELEM_INIT(nh
, _addr
);
71 MPLS_LIST_ELEM_INIT(nh
, _if
);
72 MPLS_LIST_ELEM_INIT(nh
, _outlabel
);
73 nh
->index
= _ldp_nexthop_get_next_index();
74 mpls_nexthop2ldp_nexthop(n
, nh
);
75 _ldp_global_add_nexthop(g
, nh
);
80 void ldp_nexthop_add_if(ldp_nexthop
* nh
, ldp_if
* i
)
84 nh
->info
.if_handle
= i
->handle
;
88 void ldp_nexthop_del_if(ldp_global
*g
, ldp_nexthop
* nh
)
91 MPLS_REFCNT_RELEASE2(g
, nh
->iff
, ldp_if_delete
);
95 void ldp_nexthop_add_addr(ldp_nexthop
* nh
, ldp_addr
* a
)
102 void ldp_nexthop_del_addr(ldp_global
*g
, ldp_nexthop
* nh
)
105 MPLS_REFCNT_RELEASE2(g
, nh
->addr
, ldp_addr_delete
);
109 void ldp_nexthop_add_outlabel(ldp_nexthop
* nh
, ldp_outlabel
* o
)
111 MPLS_ASSERT(nh
&& o
);
116 void ldp_nexthop_del_outlabel(ldp_nexthop
* nh
)
119 MPLS_REFCNT_RELEASE(nh
->outlabel
, ldp_outlabel_delete
);
123 void ldp_nexthop_add_outlabel2(ldp_nexthop
* n
, ldp_outlabel
* o
)
127 MPLS_LIST_ADD_HEAD(&n
->outlabel_root
, o
, _nexthop
, ldp_outlabel
);
128 memcpy(&o
->info
.nexthop
, &n
->info
, sizeof(mpls_nexthop
));
131 void ldp_nexthop_del_outlabel2(ldp_global
*g
, ldp_nexthop
* n
, ldp_outlabel
* o
)
134 MPLS_LIST_REMOVE(&n
->outlabel_root
, o
, _nexthop
);
135 ldp_outlabel_del_nexthop2(g
, o
);
136 MPLS_REFCNT_RELEASE(o
, ldp_outlabel_delete
);
139 void ldp_nexthop_add_fec(ldp_nexthop
*nh
, ldp_fec
*f
)
141 MPLS_ASSERT(nh
&& f
);
146 void ldp_nexthop_del_fec(ldp_global
*g
, ldp_nexthop
* nh
)
149 MPLS_REFCNT_RELEASE2(g
, nh
->fec
, ldp_fec_delete
);
153 static uint32_t _ldp_nexthop_get_next_index()
155 uint32_t retval
= _ldp_nexthop_next_index
;
157 _ldp_nexthop_next_index
++;
158 if (retval
> _ldp_nexthop_next_index
) {
159 _ldp_nexthop_next_index
= 1;