FECs are now inserted in the tree upon creation
[mpls-ldp-portable.git] / ldp / ldp_nexthop.c
blobacf7798068f32ccd96fe17d19590f302c47123ef
2 /*
3 * Copyright (C) James R. Leu 2003
4 * jleu@mindspring.com
6 * This software is covered under the LGPL, for more
7 * info check out http://www.gnu.org/copyleft/lgpl.html
8 */
10 #include "ldp_struct.h"
11 #include "ldp_fec.h"
12 #include "ldp_if.h"
13 #include "ldp_addr.h"
14 #include "ldp_session.h"
15 #include "ldp_outlabel.h"
16 #include "mpls_assert.h"
17 #include "mpls_compare.h"
18 #include "mpls_mm_impl.h"
19 #include "mpls_tree_impl.h"
20 #include "mpls_policy_impl.h"
21 #include "mpls_trace_impl.h"
23 #if MPLS_USE_LSR
24 #include "lsr_cfg.h"
25 #else
26 #include "mpls_mpls_impl.h"
27 #endif
29 static uint32_t _ldp_nexthop_next_index = 1;
30 static uint32_t _ldp_nexthop_get_next_index();
32 ldp_nexthop *ldp_nexthop_for_fec_session(ldp_fec *fec, ldp_session *s)
34 ldp_nexthop *nh = MPLS_LIST_HEAD(&fec->nh_root);
35 ldp_session *sp;
36 while (nh) {
37 sp = ldp_session_for_nexthop(nh);
38 if (sp && (sp->index == s->index)) {
39 return nh;
41 nh = MPLS_LIST_NEXT(&fec->nh_root, nh, _fec);
43 return NULL;
46 ldp_nexthop *ldp_nexthop_create()
48 ldp_nexthop *nh = (ldp_nexthop *) mpls_malloc(sizeof(ldp_nexthop));
50 if (nh != NULL) {
51 memset(nh, 0, sizeof(ldp_nexthop));
52 MPLS_REFCNT_INIT(nh, 0);
53 MPLS_LIST_INIT(&nh->outlabel_root, ldp_outlabel);
54 MPLS_LIST_ELEM_INIT(nh, _fec);
55 MPLS_LIST_ELEM_INIT(nh, _addr);
56 MPLS_LIST_ELEM_INIT(nh, _if);
57 MPLS_LIST_ELEM_INIT(nh, _outlabel);
58 nh->index = _ldp_nexthop_get_next_index();
60 return nh;
63 void ldp_nexthop_delete(ldp_nexthop *nh)
65 mpls_free(nh);
68 void ldp_nexthop_add_if(ldp_nexthop * nh, ldp_if * i)
70 MPLS_ASSERT(nh && i);
71 MPLS_REFCNT_HOLD(i);
72 nh->info.if_handle = i->handle;
73 nh->iff = i;
76 void ldp_nexthop_del_if(ldp_global *g, ldp_nexthop * nh)
78 MPLS_ASSERT(nh);
79 MPLS_REFCNT_RELEASE2(g, nh->iff, ldp_if_delete);
80 nh->iff = NULL;
83 void ldp_nexthop_add_addr(ldp_nexthop * nh, ldp_addr * a)
85 MPLS_ASSERT(nh && a);
86 MPLS_REFCNT_HOLD(a);
87 nh->addr = a;
90 void ldp_nexthop_del_addr(ldp_global *g, ldp_nexthop * nh)
92 MPLS_ASSERT(nh);
93 MPLS_REFCNT_RELEASE2(g, nh->addr, ldp_addr_delete);
94 nh->addr = NULL;
97 void ldp_nexthop_add_outlabel(ldp_nexthop * nh, ldp_outlabel * o)
99 MPLS_ASSERT(nh && o);
100 MPLS_REFCNT_HOLD(o);
101 nh->outlabel = o;
104 void ldp_nexthop_del_outlabel(ldp_nexthop * nh)
106 MPLS_ASSERT(nh);
107 MPLS_REFCNT_RELEASE(nh->outlabel, ldp_outlabel_delete);
108 nh->outlabel = NULL;
111 void ldp_nexthop_add_outlabel2(ldp_nexthop * n, ldp_outlabel * o)
113 MPLS_ASSERT(n && o);
114 MPLS_REFCNT_HOLD(o);
115 MPLS_LIST_ADD_HEAD(&n->outlabel_root, o, _nexthop, ldp_outlabel);
116 memcpy(&o->info.nexthop, &n->info, sizeof(mpls_nexthop));
119 void ldp_nexthop_del_outlabel2(ldp_nexthop * n, ldp_outlabel * o)
121 MPLS_ASSERT(n && o);
122 MPLS_LIST_REMOVE(&n->outlabel_root, o, _nexthop);
123 ldp_outlabel_del_nexthop2(o);
124 MPLS_REFCNT_RELEASE(o, ldp_outlabel_delete);
127 void ldp_nexthop_add_fec(ldp_nexthop *nh, ldp_fec *f)
129 MPLS_ASSERT(nh && f);
130 MPLS_REFCNT_HOLD(f);
131 nh->fec = f;
134 void ldp_nexthop_del_fec(ldp_global *g, ldp_nexthop * nh)
136 MPLS_ASSERT(nh);
137 MPLS_REFCNT_RELEASE2(g, nh->fec, ldp_fec_delete);
138 nh->fec = NULL;
141 void mpls_nexthop2ldp_nexthop(mpls_nexthop *mnh, ldp_nexthop *lnh)
143 memcpy(&lnh->info, mnh, sizeof(mpls_nexthop));
146 static uint32_t _ldp_nexthop_get_next_index()
148 uint32_t retval = _ldp_nexthop_next_index;
150 _ldp_nexthop_next_index++;
151 if (retval > _ldp_nexthop_next_index) {
152 _ldp_nexthop_next_index = 1;
154 return retval;