No longer need mpls_ifmgr_get_address mpls_ifmgr_open_if and
[mpls-ldp-portable.git] / ldp / ldp_fec.c
blob17385458b202d1d53a7f8f98b96565339591e901
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_addr.h"
15 #include "ldp_nexthop.h"
16 #include "ldp_session.h"
17 #include "ldp_inlabel.h"
18 #include "ldp_outlabel.h"
19 #include "ldp_global.h"
20 #include "ldp_label_mapping.h"
21 #include "ldp_label_request.h"
22 #include "ldp_label_abort.h"
23 #include "ldp_label_rel_with.h"
24 #include "mpls_assert.h"
25 #include "mpls_compare.h"
26 #include "mpls_mm_impl.h"
27 #include "mpls_tree_impl.h"
28 #include "mpls_policy_impl.h"
29 #include "mpls_trace_impl.h"
31 #if MPLS_USE_LSR
32 #include "lsr_cfg.h"
33 #else
34 #include "mpls_mpls_impl.h"
35 #endif
37 static uint32_t _ldp_fec_next_index = 1;
38 static uint32_t _ldp_fec_get_next_index();
40 ldp_nexthop *ldp_fec_nexthop_find(ldp_fec *f, mpls_nexthop *n)
42 ldp_nexthop *nh = NULL;
44 MPLS_ASSERT(f && n);
46 nh = MPLS_LIST_HEAD(&f->nh_root);
47 while (nh) {
48 if (!mpls_nexthop_compare(&nh->info, n)) {
49 return nh;
51 nh = MPLS_LIST_NEXT(&f->nh_root, nh, _fec);
54 return NULL;
57 ldp_fec *ldp_fec_find(ldp_global *g, mpls_fec *fec)
59 ldp_fec *f = NULL;
60 uint32_t key;
61 uint8_t len;
63 switch(fec->type) {
64 case MPLS_FEC_PREFIX:
65 key = fec->u.prefix.network.u.ipv4;
66 len = fec->u.prefix.length;
67 break;
68 case MPLS_FEC_HOST:
69 key = fec->u.host.u.ipv4;
70 len = 32;
71 case MPLS_FEC_L2CC:
72 if (ldp_global_find_fec(g, fec, &f) == MPLS_SUCCESS) {
73 return f;
75 return NULL;
76 default:
77 MPLS_ASSERT(0);
80 if (mpls_tree_get(g->fec_tree, key, len, (void **)&f) != MPLS_SUCCESS) {
81 return NULL;
83 return f;
86 mpls_return_enum ldp_fec_insert2(ldp_global *g, ldp_fec * fec)
88 uint32_t key;
89 uint8_t len;
91 MPLS_ASSERT(MPLS_LIST_EMPTY(&fec->nh_root));
93 switch(fec->info.type) {
94 case MPLS_FEC_PREFIX:
95 key = fec->info.u.prefix.network.u.ipv4;
96 len = fec->info.u.prefix.length;
97 break;
98 case MPLS_FEC_HOST:
99 key = fec->info.u.host.u.ipv4;
100 len = 32;
101 case MPLS_FEC_L2CC:
102 /* they had better insert it into the global list */
103 return MPLS_SUCCESS;
104 default:
105 MPLS_ASSERT(0);
108 /* hold it since it is going in the tree */
109 MPLS_REFCNT_HOLD(fec);
111 if (mpls_tree_insert(g->fec_tree, key, len, (void *)fec) != MPLS_SUCCESS) {
112 LDP_PRINT(g->user_data, "ldp_fec_insert: error adding addr\n");
114 /* delete the refcnt we just took */
115 MPLS_REFCNT_RELEASE(fec, ldp_fec_delete);
116 goto ldp_fec_insert2_error;
118 return MPLS_SUCCESS;
120 ldp_fec_insert2_error:
121 return MPLS_FATAL;
124 ldp_fec *ldp_fec_insert(ldp_global *g, mpls_fec * fec)
126 ldp_fec *f = NULL;
128 if ((f = ldp_fec_create()) == NULL) {
129 LDP_PRINT(g->user_data, "ldp_fec_insert: error creating address\n");
130 return NULL;
133 mpls_fec2ldp_fec(fec,f);
135 MPLS_REFCNT_HOLD(f);
137 if (ldp_fec_insert2(g,f) != MPLS_SUCCESS) {
138 MPLS_REFCNT_RELEASE(f, ldp_fec_delete);
139 return NULL;
141 return f;
144 void ldp_fec_remove(ldp_global *g, mpls_fec *fec)
146 ldp_fec *f = NULL;
147 uint32_t key;
148 uint8_t len;
150 switch(fec->type) {
151 case MPLS_FEC_PREFIX:
152 key = fec->u.prefix.network.u.ipv4;
153 len = fec->u.prefix.length;
154 break;
155 case MPLS_FEC_HOST:
156 key = fec->u.host.u.ipv4;
157 len = 32;
158 case MPLS_FEC_L2CC:
159 /* they had better remove it from the global list */
160 return;
161 default:
162 MPLS_ASSERT(0);
165 mpls_tree_remove(g->fec_tree, key, len, (void **)&f);
166 if (!f) {
167 return;
170 MPLS_ASSERT(MPLS_LIST_EMPTY(&f->nh_root));
172 MPLS_REFCNT_RELEASE(f, ldp_fec_delete);
175 mpls_bool ldp_fec_empty(ldp_fec *fec)
177 if (MPLS_LIST_EMPTY(&fec->fs_root_us) &&
178 MPLS_LIST_EMPTY(&fec->fs_root_ds)) {
179 return MPLS_BOOL_TRUE;
181 return MPLS_BOOL_FALSE;
184 mpls_return_enum ldp_fec_find_nexthop_index(ldp_fec *f, int index,
185 ldp_nexthop **n)
187 ldp_nexthop *nh = NULL;
189 MPLS_ASSERT(f);
191 if (index > 0) {
193 /* because we sort our inserts by index, this lets us know
194 if we've "walked" past the end of the list */
196 nh = MPLS_LIST_TAIL(&f->nh_root);
197 if (!nh || nh->index < index) {
198 *n = NULL;
199 return MPLS_END_OF_LIST;
202 nh = MPLS_LIST_HEAD(&f->nh_root);
203 do {
204 if (nh->index == index) {
205 *n = nh;
206 return MPLS_SUCCESS;
208 } while((nh = MPLS_LIST_NEXT(&f->nh_root, nh, _fec)));
210 *n = NULL;
211 return MPLS_FAILURE;
214 mpls_return_enum ldp_fec_add_nexthop(ldp_global *g, ldp_fec * f,
215 ldp_nexthop * nh)
217 MPLS_ASSERT(f && nh);
219 MPLS_REFCNT_HOLD(nh);
220 MPLS_LIST_ADD_HEAD(&f->nh_root, nh, _fec, ldp_nexthop);
222 ldp_nexthop_add_fec(nh, f);
224 if (nh->type & MPLS_NH_IP) {
225 ldp_addr *addr = NULL;
226 if (!(addr = ldp_addr_find(g, &nh->info.ip))) {
227 if (!(addr = ldp_addr_insert(g, &nh->info.ip))) {
228 goto ldp_fec_add_nexthop_error;
232 ldp_addr_add_nexthop(addr, nh);
235 if (nh->type & MPLS_NH_IF) {
236 ldp_if *iff = NULL;
237 if (!(iff = ldp_global_find_if_handle(g, nh->info.if_handle))) {
238 nh->if_handle = nh->info.if_handle;
239 } else {
240 ldp_if_add_nexthop(iff, nh);
244 if (nh->type & MPLS_NH_OUTSEGMENT) {
245 ldp_outlabel *out = NULL;
246 MPLS_ASSERT((out = ldp_global_find_outlabel_handle(g,
247 nh->info.outsegment_handle)));
249 ldp_outlabel_add_nexthop(out, nh);
251 return MPLS_SUCCESS;
253 ldp_fec_add_nexthop_error:
255 ldp_fec_del_nexthop(g, f, nh);
256 return MPLS_FATAL;
259 void ldp_fec_del_nexthop(ldp_global *g, ldp_fec * f, ldp_nexthop *nh)
261 MPLS_ASSERT(f && nh);
263 if (nh->addr) {
264 ldp_addr_del_nexthop(g, nh->addr, nh);
266 if (nh->iff) {
267 ldp_if_del_nexthop(g, nh->iff, nh);
269 if (nh->outlabel) {
270 ldp_outlabel_del_nexthop(nh->outlabel, nh);
273 MPLS_LIST_REMOVE(&f->nh_root, nh, _fec);
274 ldp_nexthop_del_fec(nh);
276 MPLS_REFCNT_RELEASE(nh, ldp_nexthop_delete);
279 mpls_return_enum ldp_fec_process_add(ldp_global * g, ldp_fec * f,
280 ldp_nexthop *nh)
282 ldp_session *nh_session = NULL;
283 ldp_session *peer = NULL;
284 ldp_attr *ds_attr = NULL;
285 ldp_attr *us_attr = NULL;
286 mpls_bool egress = MPLS_BOOL_FALSE;
287 ldp_outlabel *out;
289 LDP_ENTER(g->user_data, "ldp_fec_process_add");
292 * find the info about the next hop for this FEC
294 nh_session = ldp_session_for_nexthop(nh);
296 if (nh_session) {
297 ds_attr = ldp_attr_find_downstream_state2(g, nh_session, f,
298 LDP_LSP_STATE_MAP_RECV);
299 if (ds_attr && !ds_attr->outlabel) {
300 out = ldp_outlabel_create_complete(g, nh_session, ds_attr, nh);
301 if (!out) {
302 return MPLS_FAILURE;
304 ds_attr->outlabel = out;
309 * for every peer except the nh hop peer, check to see if we need to
310 * send a mapping
312 peer = MPLS_LIST_HEAD(&g->session);
313 while (peer != NULL) { /* FEC.1 */
314 if ((peer->state != LDP_STATE_OPERATIONAL) ||
315 (nh_session && peer->index == nh_session->index)) {
316 goto next_peer;
318 /* have I already sent a mapping for FEC to peer */
319 if ((us_attr = ldp_attr_find_upstream_state2(g, peer, f,
320 LDP_LSP_STATE_MAP_SENT))) {
321 /* yep, don't send another */
322 if (ds_attr) {
323 if (ldp_inlabel_add_outlabel(g, us_attr->inlabel,
324 ds_attr->outlabel) != MPLS_SUCCESS) {
325 return MPLS_FAILURE;
328 goto next_peer;
331 if (peer->oper_distribution_mode == LDP_DISTRIBUTION_UNSOLICITED) {
332 if (g->lsp_control_mode == LDP_CONTROL_INDEPENDENT) {
333 us_attr =
334 ldp_attr_find_upstream_state2(g, peer, f, LDP_LSP_STATE_REQ_RECV);
336 /* FEC.1.DUI3,4 */
337 if (ldp_label_mapping_with_xc(g, peer, f, &us_attr, ds_attr) !=
338 MPLS_SUCCESS) {
339 if (!us_attr->in_tree) {
340 ldp_attr_remove_complete(g, us_attr, MPLS_BOOL_FALSE);
342 goto next_peer;
344 } else {
346 *LDP_CONTROL_ORDERED
349 if (ds_attr || egress == MPLS_BOOL_TRUE) { /* FEC.1.DUO2 */
350 if (!(us_attr = ldp_attr_create(&f->info))) {
351 return MPLS_FAILURE;
353 /* FEC.1.DUO3-4 */
354 if ((egress == MPLS_BOOL_TRUE) && (mpls_policy_egress_check(
355 g->user_data, &f->info, &nh->info) == MPLS_BOOL_TRUE)) {
356 goto next_peer;
359 if (ldp_label_mapping_with_xc(g, peer, f, &us_attr, ds_attr) !=
360 MPLS_SUCCESS) {
361 return MPLS_FAILURE;
366 next_peer:
367 peer = MPLS_LIST_NEXT(&g->session, peer, _global);
370 if (ds_attr) { /* FEC.2 */
371 if (ldp_label_mapping_process(g, nh_session, NULL, NULL, ds_attr, f) ==
372 MPLS_FAILURE) { /* FEC.5 */
373 return MPLS_FAILURE;
375 return MPLS_SUCCESS;
379 * LDP_DISTRIBUTION_ONDEMAND
381 /* FEC.3 */
382 if (nh_session &&
383 nh_session->oper_distribution_mode == LDP_DISTRIBUTION_ONDEMAND) {
384 /* assume we're always "request when needed" */
385 ds_attr = NULL;
386 if (ldp_label_request_for_xc(g, nh_session, &f->info, NULL, &ds_attr) ==
387 MPLS_FAILURE) { /* FEC.4 */
388 return MPLS_FAILURE;
392 LDP_EXIT(g->user_data, "ldp_fec_process_add");
394 return MPLS_SUCCESS; /* FEC.6 */
397 mpls_return_enum ldp_fec_process_change(ldp_global * g, ldp_fec * f,
398 ldp_nexthop *nh, ldp_session *nh_session_old) {
399 ldp_session *peer = NULL;
400 ldp_attr *us_attr = NULL;
401 ldp_attr *ds_attr = NULL;
402 ldp_session *nh_session_new = NULL;
404 LDP_ENTER(g->user_data, "ldp_fec_process_change");
407 * NH 1-5 decide if we need to release an existing mapping
409 ds_attr = ldp_attr_find_downstream_state2(g, nh_session_old, f,
410 LDP_LSP_STATE_MAP_RECV);
411 if (!ds_attr) { /* NH.1 */
412 goto Detect_Change_Fec_Next_Hop_6;
415 if (ds_attr->ingress == MPLS_BOOL_TRUE) {
417 #if MPLS_USE_LSR
418 lsr_ftn ftn;
419 ftn.outsegment_index = ds_attr->outlabel->info.handle;
420 memcpy(&ftn.fec, &f->info, sizeof(mpls_fec));
421 lsr_cfg_ftn_set2(g->lsr_handle, &ftn, LSR_CFG_DEL);
422 #else
423 mpls_mpls_fec2out_del(g->mpls_handle, &f->info, &ds_attr->outlabel->info);
424 #endif
425 ds_attr->ingress = MPLS_BOOL_FALSE;
426 ds_attr->outlabel->merge_count--;
429 if (g->label_retention_mode == LDP_RETENTION_LIBERAL) { /* NH.3 */
430 ldp_attr *us_temp;
431 us_attr = MPLS_LIST_HEAD(&ds_attr->us_attr_root);
432 while (us_attr) {
433 /* need to walk the list in such a way as not to
434 * "pull the rug out from under me self"
436 us_temp = MPLS_LIST_NEXT(&ds_attr->us_attr_root, us_attr, _ds_attr);
437 if (us_attr->state == LDP_LSP_STATE_MAP_SENT) {
438 ldp_inlabel_del_outlabel(g, us_attr->inlabel); /* NH.2 */
439 ldp_attr_del_us2ds(us_attr, ds_attr);
441 us_attr = us_temp;
443 goto Detect_Change_Fec_Next_Hop_6;
446 ldp_label_release_send(g, nh_session_old, ds_attr, LDP_NOTIF_NONE); /* NH.4 */
447 ldp_attr_remove_complete(g, ds_attr, MPLS_BOOL_FALSE); /* NH.2,5 */
449 Detect_Change_Fec_Next_Hop_6:
452 * NH 6-9 decides is we need to send a label request abort
454 ds_attr = ldp_attr_find_downstream_state2(g, nh_session_old, f,
455 LDP_LSP_STATE_REQ_SENT);
456 if (ds_attr) { /* NH.6 */
457 if (g->label_retention_mode != LDP_RETENTION_CONSERVATIVE) { /* NH.7 */
458 /* NH.8,9 */
459 if (ldp_label_abort_send(g, nh_session_old, ds_attr) != MPLS_SUCCESS) {
460 return MPLS_FAILURE;
466 * NH 10-12 decides if we can use a mapping from our database
468 if (!(nh_session_new = ldp_get_next_hop_session_for_fec2(f,nh))) {
469 goto Detect_Change_Fec_Next_Hop_16;
472 ds_attr = ldp_attr_find_downstream_state2(g, nh_session_new, f,
473 LDP_LSP_STATE_MAP_RECV);
474 if (!ds_attr) { /* NH.11 */
475 goto Detect_Change_Fec_Next_Hop_13;
478 if (ldp_label_mapping_process(g, nh_session_new, NULL, NULL, ds_attr, f) !=
479 MPLS_SUCCESS) { /* NH.12 */
480 return MPLS_FAILURE;
482 goto Detect_Change_Fec_Next_Hop_20;
484 Detect_Change_Fec_Next_Hop_13:
487 * NH 13-15 decides if we need to make a label request
489 if (nh_session_new->oper_distribution_mode == LDP_DISTRIBUTION_ONDEMAND &&
490 g->label_retention_mode == LDP_RETENTION_CONSERVATIVE) {
491 /* NH.14-15 */
492 if (ldp_label_request_for_xc(g, nh_session_new, &f->info, NULL, &ds_attr) !=
493 MPLS_SUCCESS) {
494 return MPLS_FAILURE;
497 goto Detect_Change_Fec_Next_Hop_20;
499 Detect_Change_Fec_Next_Hop_16:
501 peer = MPLS_LIST_HEAD(&g->session);
502 while (peer) {
503 if (peer->state == LDP_STATE_OPERATIONAL) {
504 us_attr = ldp_attr_find_upstream_state2(g, peer, f,
505 LDP_LSP_STATE_MAP_SENT);
506 if (us_attr) { /* NH.17 */
507 if (ldp_label_withdraw_send(g, peer, us_attr, LDP_NOTIF_NONE) !=
508 MPLS_SUCCESS) { /* NH.18 */
509 ldp_attr_remove_complete(g, us_attr, MPLS_BOOL_FALSE);
510 return MPLS_FAILURE;
514 peer = MPLS_LIST_NEXT(&g->session, peer, _global);
517 Detect_Change_Fec_Next_Hop_20:
519 LDP_EXIT(g->user_data, "ldp_fec_process_change");
521 return MPLS_SUCCESS;
524 ldp_fec *ldp_fec_create()
526 ldp_fec *fec = (ldp_fec *) mpls_malloc(sizeof(ldp_fec));
528 if (fec != NULL) {
529 memset(fec, 0, sizeof(ldp_fec));
530 MPLS_REFCNT_INIT(fec, 0);
531 MPLS_LIST_ELEM_INIT(fec, _global);
532 MPLS_LIST_ELEM_INIT(fec, _inlabel);
533 MPLS_LIST_ELEM_INIT(fec, _outlabel);
534 MPLS_LIST_ELEM_INIT(fec, _fec);
535 MPLS_LIST_INIT(&fec->nh_root, ldp_nexthop);
536 MPLS_LIST_INIT(&fec->fs_root_us, ldp_fs);
537 MPLS_LIST_INIT(&fec->fs_root_ds, ldp_fs);
538 fec->info.type = MPLS_FEC_NONE;
539 fec->index = _ldp_fec_get_next_index();
541 return fec;
544 void mpls_fec2ldp_fec(mpls_fec * a, ldp_fec * b)
546 memcpy(&b->info, a, sizeof(mpls_fec));
549 ldp_fec *ldp_fec_create_host(mpls_inet_addr * host)
551 ldp_fec *fec = ldp_fec_create();
553 if (fec != NULL) {
554 fec->info.type = MPLS_FEC_HOST;
555 memcpy(&fec->info.u.host, host, sizeof(mpls_inet_addr));
557 return fec;
560 ldp_fec *ldp_fec_create_prefix(mpls_inet_addr * prefix, int prefix_len)
562 ldp_fec *fec = ldp_fec_create();
564 if (fec != NULL) {
565 fec->info.type = MPLS_FEC_PREFIX;
566 memcpy(&fec->info.u.prefix.network, prefix, sizeof(mpls_inet_addr));
567 fec->info.u.prefix.length = prefix_len;
569 return fec;
572 void ldp_fec_delete(ldp_fec * fec)
574 mpls_free(fec);
577 void mpls_fec2fec_tlv(mpls_fec * lf, mplsLdpFecTlv_t * tlv, int i)
579 tlv->fecElArray[i].addressEl.addressFam = 1;
581 switch (lf->type) {
582 case MPLS_FEC_PREFIX:
583 tlv->fecElArray[i].addressEl.type = MPLS_PREFIX_FEC;
584 tlv->fecElArray[i].addressEl.preLen = lf->u.prefix.length;
585 tlv->fecElArray[i].addressEl.address = lf->u.prefix.network.u.ipv4;
586 tlv->fecElemTypes[i] = MPLS_PREFIX_FEC;
587 break;
588 case MPLS_FEC_HOST:
589 tlv->fecElArray[i].addressEl.type = MPLS_HOSTADR_FEC;
590 tlv->fecElArray[i].addressEl.preLen = MPLS_IPv4LEN;
591 tlv->fecElArray[i].addressEl.address = lf->u.host.u.ipv4;
592 tlv->fecElemTypes[i] = MPLS_HOSTADR_FEC;
593 break;
594 default:
595 MPLS_ASSERT(0);
599 void fec_tlv2mpls_fec(mplsLdpFecTlv_t * tlv, int i, mpls_fec * lf) {
600 switch (tlv->fecElemTypes[i]) {
601 case MPLS_PREFIX_FEC:
602 lf->type = MPLS_FEC_PREFIX;
603 lf->u.prefix.length = tlv->fecElArray[i].addressEl.preLen;
604 lf->u.prefix.network.u.ipv4 = tlv->fecElArray[i].addressEl.address;
605 lf->u.prefix.network.type = MPLS_FAMILY_IPV4;
606 break;
607 case MPLS_HOSTADR_FEC:
608 lf->type = MPLS_FEC_HOST;
609 lf->u.host.u.ipv4 = tlv->fecElArray[i].addressEl.address;
610 lf->u.host.type = MPLS_FAMILY_IPV4;
611 break;
612 default:
613 MPLS_ASSERT(0);
617 static uint32_t _ldp_fec_get_next_index()
619 uint32_t retval = _ldp_fec_next_index;
621 _ldp_fec_next_index++;
622 if (retval > _ldp_fec_next_index) {
623 _ldp_fec_next_index = 1;
625 return retval;