Follow up to the previous submission
[mpls-ldp-portable.git] / ldp / ldp_fec.c
blobf6201f93b3c7840d4437d000b29af4bd014071a3
2 /*
3 * Copyright (C) James R. Leu 2000
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_attr.h"
14 #include "ldp_session.h"
15 #include "ldp_inlabel.h"
16 #include "ldp_outlabel.h"
17 #include "ldp_global.h"
18 #include "ldp_label_mapping.h"
19 #include "ldp_label_request.h"
20 #include "ldp_label_abort.h"
21 #include "ldp_label_rel_with.h"
22 #include "mpls_mm_impl.h"
23 #include "mpls_policy_impl.h"
24 #include "mpls_trace_impl.h"
26 #if MPLS_USE_LSR
27 #include "lsr_cfg.h"
28 #else
29 #include "mpls_mpls_impl.h"
30 #endif
32 mpls_return_enum Recognize_New_Fec(ldp_global * g, mpls_fec * f)
34 ldp_session *peer = NULL;
35 ldp_session *nh_session = NULL;
36 ldp_attr *ds_attr = NULL;
37 ldp_attr *us_attr = NULL;
38 mpls_bool egress = MPLS_BOOL_FALSE;
39 ldp_addr *nh_addr;
40 ldp_outlabel *out;
42 LDP_ENTER(g->user_data, "Recognize_New_Fec");
45 * find the info about the next hop for this FEC
47 switch (ldp_get_next_hop_session_for_fec(g, f, &nh_addr, &nh_session)) {
48 case MPLS_SUCCESS:
49 if (nh_addr == NULL) {
51 * we found the route, but no next hop
53 egress = MPLS_BOOL_TRUE;
55 break;
56 case MPLS_FAILURE:
57 case MPLS_NO_ROUTE:
58 return MPLS_FAILURE;
59 default:
60 MPLS_ASSERT(0);
63 if (nh_session) {
64 ds_attr = ldp_attr_find_downstream_state(g, nh_session, f,
65 LDP_LSP_STATE_MAP_RECV);
66 if (ds_attr && !ds_attr->outlabel) {
67 out = ldp_outlabel_create_complete(g, nh_session, nh_addr, ds_attr);
68 if (!out) {
69 return MPLS_FAILURE;
71 ds_attr->outlabel = out;
76 * for every peer except the nh hop peer, check to see if we need to
77 * send a mapping
79 peer = MPLS_LIST_HEAD(&g->session);
80 while (peer != NULL) { /* FEC.1 */
81 if ((peer->state != LDP_STATE_OPERATIONAL) ||
82 (nh_session && peer->index == nh_session->index)) {
83 goto next_peer;
85 /* have I already sent a mapping for FEC to peer */
86 if ((us_attr = ldp_attr_find_upstream_state(g, peer, f,
87 LDP_LSP_STATE_MAP_SENT))) {
88 /* yep, don't send another */
89 goto next_peer;
91 if (peer->oper_distribution_mode == LDP_DISTRIBUTION_UNSOLICITED) {
92 if (g->lsp_control_mode == LDP_CONTROL_INDEPENDENT) {
93 us_attr =
94 ldp_attr_find_upstream_state(g, peer, f, LDP_LSP_STATE_REQ_RECV);
96 /* FEC.1.DUI3,4 */
97 if (ldp_label_mapping_with_xc(g, peer, f, &us_attr, ds_attr) !=
98 MPLS_SUCCESS) {
99 if (!us_attr->in_tree) {
100 ldp_attr_remove_complete(g, us_attr, MPLS_BOOL_FALSE);
102 goto next_peer;
104 } else {
106 *LDP_CONTROL_ORDERED
109 if (ds_attr || egress == MPLS_BOOL_TRUE) { /* FEC.1.DUO2 */
110 if (!(us_attr = ldp_attr_create(f))) {
111 return MPLS_FAILURE;
113 /* FEC.1.DUO3-4 */
114 if ((egress == MPLS_BOOL_TRUE) && (mpls_policy_egress_check(
115 g->user_data, f, &f->nh) == MPLS_BOOL_TRUE) &&
116 (egress == MPLS_BOOL_TRUE)) {
117 goto next_peer;
120 if (ldp_label_mapping_with_xc(g, peer, f, &us_attr, ds_attr) !=
121 MPLS_SUCCESS) {
122 return MPLS_FAILURE;
127 next_peer:
128 peer = MPLS_LIST_NEXT(&g->session, peer, _global);
131 if (ds_attr) { /* FEC.2 */
132 if (ldp_label_mapping_process(g, nh_session, NULL, NULL, ds_attr, f) ==
133 MPLS_FAILURE) { /* FEC.5 */
134 return MPLS_FAILURE;
136 return MPLS_SUCCESS;
140 * LDP_DISTRIBUTION_ONDEMAND
142 /* FEC.3 */
143 if (nh_session &&
144 nh_session->oper_distribution_mode == LDP_DISTRIBUTION_ONDEMAND) {
145 /* assume we're always "request when needed" */
146 ds_attr = NULL;
147 if (ldp_label_request_for_xc(g, nh_session, f, NULL, &ds_attr) ==
148 MPLS_FAILURE) { /* FEC.4 */
149 return MPLS_FAILURE;
153 LDP_EXIT(g->user_data, "Recognize_New_Fec");
155 return MPLS_SUCCESS; /* FEC.6 */
158 mpls_return_enum Detect_Change_Fec_Next_Hop(ldp_global * g, mpls_fec * f,
159 ldp_session * nh_old)
161 ldp_session *peer = NULL;
162 ldp_attr *us_attr = NULL;
163 ldp_attr *ds_attr = NULL;
164 ldp_session *nh_new = NULL;
165 mpls_return_enum result;
167 LDP_ENTER(g->user_data, "Detect_Change_Fec_Next_Hop");
169 result = ldp_get_next_hop_session_for_fec(g, f, NULL, &nh_new);
172 * NH 1-5 decide if we need to release an existing mapping
174 ds_attr =
175 ldp_attr_find_downstream_state(g, nh_old, f, LDP_LSP_STATE_MAP_RECV);
176 if (!ds_attr) { /* NH.1 */
177 goto Detect_Change_Fec_Next_Hop_6;
180 if (ds_attr->ingress == MPLS_BOOL_TRUE) {
182 #if MPLS_USE_LSR
183 lsr_ftn ftn;
184 ftn.outsegment_index = ds_attr->outlabel->info.handle;
185 memcpy(&ftn.fec, f, sizeof(mpls_fec));
186 lsr_cfg_ftn_set2(g->lsr_handle, &ftn, LSR_CFG_DEL);
187 #else
188 mpls_mpls_fec2out_del(g->mpls_handle, f, &ds_attr->outlabel->info);
189 #endif
190 ds_attr->ingress = MPLS_BOOL_FALSE;
191 ds_attr->outlabel->merge_count--;
194 if (g->label_retention_mode == LDP_RETENTION_LIBERAL) { /* NH.3 */
195 ldp_attr *us_temp;
196 us_attr = MPLS_LIST_HEAD(&ds_attr->us_attr_root);
197 while (us_attr) {
198 /* need to walk the list in such a way as not to
199 * "pull the rug out from under me self"
201 us_temp = MPLS_LIST_NEXT(&ds_attr->us_attr_root, us_attr, _ds_attr);
202 if (us_attr->state == LDP_LSP_STATE_MAP_SENT) {
203 ldp_inlabel_del_outlabel(g, us_attr->inlabel); /* NH.2 */
204 ldp_attr_del_us2ds(us_attr, ds_attr);
206 us_attr = us_temp;
208 goto Detect_Change_Fec_Next_Hop_6;
211 ldp_label_release_send(g, nh_old, ds_attr, LDP_NOTIF_NONE); /* NH.4 */
212 ldp_attr_remove_complete(g, ds_attr, MPLS_BOOL_FALSE); /* NH.2,5 */
214 Detect_Change_Fec_Next_Hop_6:
217 * NH 6-9 decides is we need to send a label request abort
219 ds_attr =
220 ldp_attr_find_downstream_state(g, nh_old, f, LDP_LSP_STATE_REQ_SENT);
221 if (ds_attr) { /* NH.6 */
222 if (g->label_retention_mode != LDP_RETENTION_CONSERVATIVE) { /* NH.7 */
223 /* NH.8,9 */
224 if (ldp_label_abort_send(g, nh_old, ds_attr) != MPLS_SUCCESS) {
225 return MPLS_FAILURE;
231 * NH 10-12 decides if we can use a mapping from our database
233 if (!nh_new) {
234 goto Detect_Change_Fec_Next_Hop_16;
237 ds_attr =
238 ldp_attr_find_downstream_state(g, nh_new, f, LDP_LSP_STATE_MAP_RECV);
239 if (!ds_attr) { /* NH.11 */
240 goto Detect_Change_Fec_Next_Hop_13;
243 if (ldp_label_mapping_process(g, nh_new, NULL, NULL, ds_attr, f) !=
244 MPLS_SUCCESS) { /* NH.12 */
245 return MPLS_FAILURE;
247 goto Detect_Change_Fec_Next_Hop_20;
249 Detect_Change_Fec_Next_Hop_13:
252 * NH 13-15 decides if we need to make a label request
254 if (nh_new->oper_distribution_mode == LDP_DISTRIBUTION_ONDEMAND &&
255 g->label_retention_mode == LDP_RETENTION_CONSERVATIVE) {
256 /* NH.14-15 */
257 if (ldp_label_request_for_xc(g, nh_new, f, NULL, &ds_attr) !=
258 MPLS_SUCCESS) {
259 return MPLS_FAILURE;
262 goto Detect_Change_Fec_Next_Hop_20;
264 Detect_Change_Fec_Next_Hop_16:
266 peer = MPLS_LIST_HEAD(&g->session);
267 while (peer) {
268 if (peer->state == LDP_STATE_OPERATIONAL) {
269 us_attr = ldp_attr_find_upstream_state(g, peer, f,
270 LDP_LSP_STATE_MAP_SENT);
271 if (us_attr) { /* NH.17 */
272 if (ldp_label_withdraw_send(g, peer, us_attr, LDP_NOTIF_NONE) !=
273 MPLS_SUCCESS) { /* NH.18 */
274 ldp_attr_remove_complete(g, us_attr, MPLS_BOOL_FALSE);
275 return MPLS_FAILURE;
279 peer = MPLS_LIST_NEXT(&g->session, peer, _global);
282 Detect_Change_Fec_Next_Hop_20:
284 LDP_EXIT(g->user_data, "Detect_Change_Fec_Next_Hop");
286 return MPLS_SUCCESS;
289 ldp_fec *ldp_fec_create()
291 ldp_fec *fec = (ldp_fec *) mpls_malloc(sizeof(ldp_fec));
293 if (fec != NULL) {
294 memset(fec, 0, sizeof(ldp_fec));
295 MPLS_REFCNT_INIT(fec, 0);
296 MPLS_LIST_ELEM_INIT(fec, _global);
297 MPLS_LIST_ELEM_INIT(fec, _inlabel);
298 MPLS_LIST_ELEM_INIT(fec, _outlabel);
299 MPLS_LIST_ELEM_INIT(fec, _fec);
300 MPLS_LIST_ELEM_INIT(fec, _nh);
301 MPLS_LIST_INIT(&fec->fs_root_us, ldp_fs);
302 MPLS_LIST_INIT(&fec->fs_root_ds, ldp_fs);
303 fec->info.type = MPLS_FEC_NONE;
304 fec->info.nh.type = MPLS_NH_NONE;
305 fec->info.nh.attached = MPLS_BOOL_FALSE;
307 return fec;
310 void mpls_fec2ldp_fec(mpls_fec * a, ldp_fec * b)
312 memcpy(&b->info, a, sizeof(mpls_fec));
315 ldp_fec *ldp_fec_create_host(mpls_inet_addr * host)
317 ldp_fec *fec = ldp_fec_create();
319 if (fec != NULL) {
320 fec->info.type = MPLS_FEC_HOST;
321 memcpy(&fec->info.u.host, host, sizeof(mpls_inet_addr));
323 return fec;
326 ldp_fec *ldp_fec_create_prefix(mpls_inet_addr * prefix, int prefix_len)
328 ldp_fec *fec = ldp_fec_create();
330 if (fec != NULL) {
331 fec->info.type = MPLS_FEC_PREFIX;
332 memcpy(&fec->info.u.prefix.network, prefix, sizeof(mpls_inet_addr));
333 fec->info.u.prefix.length = prefix_len;
335 return fec;
338 void ldp_fec_delete(ldp_fec * fec)
340 mpls_free(fec);
343 void mpls_fec2fec_tlv(mpls_fec * lf, mplsLdpFecTlv_t * tlv, int i)
345 tlv->fecElArray[i].addressEl.addressFam = 1;
347 switch (lf->type) {
348 case MPLS_FEC_PREFIX:
349 tlv->fecElArray[i].addressEl.type = MPLS_PREFIX_FEC;
350 tlv->fecElArray[i].addressEl.preLen = lf->u.prefix.length;
351 tlv->fecElArray[i].addressEl.address = lf->u.prefix.network.u.ipv4;
352 tlv->fecElemTypes[i] = MPLS_PREFIX_FEC;
353 break;
354 case MPLS_FEC_HOST:
355 tlv->fecElArray[i].addressEl.type = MPLS_HOSTADR_FEC;
356 tlv->fecElArray[i].addressEl.preLen = MPLS_IPv4LEN;
357 tlv->fecElArray[i].addressEl.address = lf->u.host.u.ipv4;
358 tlv->fecElemTypes[i] = MPLS_HOSTADR_FEC;
359 break;
360 default:
361 MPLS_ASSERT(0);
365 void fec_tlv2mpls_fec(mplsLdpFecTlv_t * tlv, int i, mpls_fec * lf) {
366 switch (tlv->fecElemTypes[i]) {
367 case MPLS_PREFIX_FEC:
368 lf->type = MPLS_FEC_PREFIX;
369 lf->u.prefix.length = tlv->fecElArray[i].addressEl.preLen;
370 lf->u.prefix.network.u.ipv4 = tlv->fecElArray[i].addressEl.address;
371 lf->u.prefix.network.type = MPLS_FAMILY_IPV4;
372 break;
373 case MPLS_HOSTADR_FEC:
374 lf->type = MPLS_FEC_HOST;
375 lf->u.host.u.ipv4 = tlv->fecElArray[i].addressEl.address;
376 lf->u.host.type = MPLS_FAMILY_IPV4;
377 break;
378 default:
379 MPLS_ASSERT(0);