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 "ldp_struct.h"
14 #include "ldp_outlabel.h"
15 #include "ldp_inlabel.h"
16 #include "ldp_session.h"
17 #include "ldp_tunnel.h"
18 #include "ldp_global.h"
20 #include "mpls_mm_impl.h"
21 #include "mpls_trace_impl.h"
23 static uint32_t _ldp_outlabel_next_index
= 1;
25 ldp_outlabel
*ldp_outlabel_create()
27 ldp_outlabel
*o
= (ldp_outlabel
*) mpls_malloc(sizeof(ldp_outlabel
));
30 memset(o
, 0, sizeof(ldp_outlabel
));
31 MPLS_REFCNT_INIT(o
, 0);
32 MPLS_LIST_INIT(&o
->inlabel_root
, ldp_inlabel
);
33 MPLS_LIST_INIT(&o
->tunnel_root
, ldp_tunnel
);
34 MPLS_LIST_ELEM_INIT(o
, _global
);
35 MPLS_LIST_ELEM_INIT(o
, _session
);
36 o
->index
= _ldp_outlabel_get_next_index();
37 o
->info
.label
.type
= MPLS_LABEL_TYPE_NONE
;
38 o
->switching
= MPLS_BOOL_FALSE
;
43 ldp_outlabel
*ldp_outlabel_create_complete(ldp_global
* g
, ldp_session
* s
,
44 ldp_addr
* nh
, ldp_attr
* a
)
46 ldp_outlabel
*out
= ldp_outlabel_create();
47 /* JLEU: for now pick the first adj */
48 ldp_adj
*adj
= MPLS_LIST_HEAD(&s
->adj_root
);
49 ldp_entity
*e
= adj
->entity
;
52 ldp_outlabel_add_nexthop(out
, nh
);
53 ldp_session_add_outlabel(s
, out
);
55 out
->info
.push_label
= MPLS_BOOL_TRUE
;
56 out
->info
.owner
= MPLS_OWNER_LDP
;
58 memcpy(&out
->info
.nexthop
.ip
, &nh
->address
, sizeof(mpls_inet_addr
));
60 memcpy(&out
->info
.nexthop
.if_handle
,
61 &e
->p
.iff
->handle
, sizeof(mpls_if_handle
));
62 out
->info
.nexthop
.type
= MPLS_NH_IP
| MPLS_NH_IF
;
64 ldp_attr2mpls_label_struct(a
, &out
->info
.label
);
66 _ldp_global_add_outlabel(g
, out
);
68 ldp_attr_add_outlabel(a
, out
);
73 void ldp_outlabel_delete(ldp_outlabel
* o
)
75 LDP_PRINT(g
->user_data
,"outlabel delete\n");
76 MPLS_REFCNT_ASSERT(o
, 0);
80 void ldp_outlabel_delete_complete(ldp_global
* g
, ldp_outlabel
* out
)
82 ldp_attr_del_outlabel(out
->attr
);
84 ldp_session_del_outlabel(out
->session
, out
);
86 _ldp_global_del_outlabel(g
, out
);
87 ldp_outlabel_del_nexthop(out
);
90 mpls_return_enum
_ldp_outlabel_add_inlabel(ldp_outlabel
* o
, ldp_inlabel
* i
)
95 MPLS_LIST_ADD_HEAD(&o
->inlabel_root
, i
, _outlabel
, ldp_inlabel
);
101 mpls_return_enum
_ldp_outlabel_del_inlabel(ldp_outlabel
* o
, ldp_inlabel
* i
)
104 MPLS_LIST_REMOVE(&o
->inlabel_root
, i
, _outlabel
);
106 MPLS_REFCNT_RELEASE(i
, ldp_inlabel_delete
);
112 mpls_return_enum
_ldp_outlabel_add_attr(ldp_outlabel
* o
, ldp_attr
* a
)
122 mpls_return_enum
_ldp_outlabel_del_attr(ldp_outlabel
* o
)
125 MPLS_REFCNT_RELEASE(o
->attr
, ldp_attr_delete
);
132 mpls_return_enum
ldp_outlabel_add_nexthop(ldp_outlabel
* o
, ldp_addr
* nh
)
135 MPLS_REFCNT_HOLD(nh
);
142 mpls_return_enum
ldp_outlabel_del_nexthop(ldp_outlabel
* o
)
145 MPLS_REFCNT_RELEASE(o
->addr
, ldp_addr_delete
);
152 mpls_return_enum
_ldp_outlabel_add_session(ldp_outlabel
* o
, ldp_session
* s
)
162 mpls_return_enum
_ldp_outlabel_del_session(ldp_outlabel
* o
)
164 if (o
&& o
->session
) {
165 MPLS_REFCNT_RELEASE(o
->session
, ldp_session_delete
);
172 uint32_t _ldp_outlabel_get_next_index()
174 uint32_t retval
= _ldp_outlabel_next_index
;
176 _ldp_outlabel_next_index
++;
177 if (retval
> _ldp_outlabel_next_index
) {
178 _ldp_outlabel_next_index
= 1;
183 mpls_return_enum
_ldp_outlabel_add_tunnel(ldp_outlabel
* o
, ldp_tunnel
* t
)
188 MPLS_LIST_ADD_HEAD(&o
->tunnel_root
, t
, _outlabel
, ldp_tunnel
);
194 mpls_return_enum
_ldp_outlabel_del_tunnel(ldp_outlabel
* o
, ldp_tunnel
* t
)
197 MPLS_LIST_REMOVE(&o
->tunnel_root
, t
, _outlabel
);
199 MPLS_REFCNT_RELEASE(t
, ldp_tunnel_delete
);