Create a ldp_nexthop which mirrors the functionality of
[mpls-ldp-portable.git] / ldp / ldp_label_request.c
blobe47944a2756dfc8da1bc97525112b93af2478ab3
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_attr.h"
12 #include "ldp_fec.h"
13 #include "ldp_mesg.h"
14 #include "ldp_pdu_setup.h"
15 #include "ldp_notif.h"
16 #include "ldp_session.h"
17 #include "ldp_entity.h"
18 #include "ldp_label_mapping.h"
19 #include "ldp_label_request.h"
21 #include "mpls_timer_impl.h"
22 #include "mpls_policy_impl.h"
23 #include "mpls_tree_impl.h"
24 #include "mpls_trace_impl.h"
25 #include "mpls_fib_impl.h"
26 #include "mpls_lock_impl.h"
28 mpls_return_enum ldp_label_request_for_xc(ldp_global * g, ldp_session * s,
29 mpls_fec * fec, ldp_attr * us_attr, ldp_attr ** ds_attr)
32 LDP_ENTER(g->user_data, "ldp_label_request_for_xc");
34 if (!(*ds_attr)) {
35 if (!((*ds_attr) = ldp_attr_create(fec))) {
36 return MPLS_FATAL;
39 Prepare_Label_Request_Attributes(g, s, fec, (*ds_attr), us_attr);
40 (*ds_attr)->state = LDP_LSP_STATE_REQ_SENT;
41 if (ldp_label_request_send(g, s, us_attr, ds_attr) != MPLS_SUCCESS) {
42 return MPLS_FAILURE;
45 LDP_EXIT(g->user_data, "ldp_label_request_for_xc");
47 return MPLS_SUCCESS;
50 void ldp_label_request_prepare_msg(ldp_mesg * msg, uint32_t msgid,
51 ldp_attr * s_attr)
53 mplsLdpLblReqMsg_t *req = NULL;
54 int i;
56 ldp_mesg_prepare(msg, MPLS_LBLREQ_MSGTYPE, msgid);
57 req = &msg->u.request;
59 if (s_attr->fecTlvExists) {
60 req->fecTlvExists = 1;
61 req->baseMsg.msgLength += setupFecTlv(&req->fecTlv);
62 req->baseMsg.msgLength += addFecElem2FecTlv(&req->fecTlv,
63 &s_attr->fecTlv.fecElArray[0]);
65 if (s_attr->hopCountTlvExists) {
66 req->hopCountTlvExists = 1;
67 req->baseMsg.msgLength += setupHopCountTlv(&req->hopCountTlv,
68 s_attr->hopCountTlv.hcValue);
70 if (s_attr->pathVecTlvExists) {
71 req->pathVecTlvExists = 1;
72 req->baseMsg.msgLength += setupPathTlv(&req->pathVecTlv);
73 for (i = 0; i < MPLS_MAXHOPSNUMBER; i++) {
74 if (s_attr->pathVecTlv.lsrId[i]) {
75 req->baseMsg.msgLength += addLsrId2PathTlv(&req->pathVecTlv,
76 s_attr->pathVecTlv.lsrId[i]);
82 mpls_return_enum ldp_label_request_send(ldp_global * g, ldp_session * s,
83 ldp_attr * us_attr, ldp_attr ** ds_attr)
85 ldp_attr *ds_temp;
86 mpls_fec fec;
88 LDP_ENTER(g->user_data, "ldp_label_request_send");
89 MPLS_ASSERT(ds_attr && *ds_attr);
91 fec_tlv2mpls_fec(&((*ds_attr)->fecTlv), 0, &fec);
93 if ((ds_temp = ldp_attr_find_downstream_state(g, s, &fec,
94 LDP_LSP_STATE_REQ_SENT)) != NULL) { /* SLRq.1 */
96 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_SEND, LDP_TRACE_FLAG_LABEL,
97 "Label Request Send: request already pending(%d)\n", ds_temp->index);
99 ldp_attr_add_us2ds(us_attr, ds_temp);
101 /* we do not need the one passed in, but make sure that the caller
102 is using this one from here forth */
103 ldp_attr_remove_complete(g, *ds_attr, MPLS_BOOL_TRUE);
104 *ds_attr = ds_temp;
105 return MPLS_SUCCESS;
108 if (s->no_label_resource_recv == MPLS_BOOL_TRUE) { /* SLRq.2 */
109 goto ldp_label_request_send_error;
112 (*ds_attr)->msg_id = g->message_identifier++;
113 ldp_label_request_prepare_msg(s->tx_message, (*ds_attr)->msg_id, *ds_attr);
115 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_SEND, LDP_TRACE_FLAG_LABEL,
116 "Label Request Sent: session(%d)\n", s->index);
118 if (ldp_mesg_send_tcp(g, s, s->tx_message) == MPLS_FAILURE) { /* SLRq.3 */
119 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_SEND, LDP_TRACE_FLAG_ERROR,
120 "Label Request send failed\n");
121 goto ldp_label_request_send_error;
124 (*ds_attr)->state = LDP_LSP_STATE_REQ_SENT;
125 if (ldp_attr_insert_downstream(g, s, (*ds_attr)) == MPLS_FAILURE) { /* SLRq.4 */
126 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_SEND, LDP_TRACE_FLAG_ERROR,
127 "Couldn't insert sent attributes in tree\n");
128 goto ldp_label_request_send_error;
130 if (us_attr) {
131 ldp_attr_add_us2ds(us_attr, *ds_attr);
134 LDP_EXIT(g->user_data, "ldp_label_request_send");
136 return MPLS_SUCCESS; /* SLRq.5 */
138 ldp_label_request_send_error:
140 LDP_PRINT(g->user_data, "SLRq.6\n");
141 (*ds_attr)->state = LDP_LSP_STATE_NO_LABEL_RESOURCE_SENT;
142 ldp_attr_insert_downstream(g, s, (*ds_attr)); /* SLRq.6 */
144 LDP_EXIT(g->user_data, "ldp_label_request_send-error");
146 return MPLS_FAILURE; /* SLRq.7 */
149 void req2attr(mplsLdpLblReqMsg_t * req, ldp_attr * attr, uint32_t flag)
151 attr->msg_id = req->baseMsg.msgId;
153 if (req->fecTlvExists && flag & LDP_ATTR_FEC) {
154 memcpy(&attr->fecTlv, &req->fecTlv, sizeof(mplsLdpFecTlv_t));
155 attr->fecTlvExists = 1;
157 if (req->hopCountTlvExists && flag & LDP_ATTR_HOPCOUNT) {
158 memcpy(&attr->hopCountTlv, &req->hopCountTlv, sizeof(mplsLdpHopTlv_t));
159 attr->hopCountTlvExists = 1;
161 if (req->pathVecTlvExists && flag & LDP_ATTR_PATH) {
162 memcpy(&attr->pathVecTlv, &req->pathVecTlv, sizeof(mplsLdpPathTlv_t));
163 attr->pathVecTlvExists = 1;
165 if (req->lblMsgIdTlvExists && flag & LDP_ATTR_MSGID) {
166 memcpy(&attr->lblMsgIdTlv, &req->lblMsgIdTlv, sizeof(mplsLdpLblMsgIdTlv_t));
167 attr->lblMsgIdTlvExists = 1;
169 if (req->lspidTlvExists && flag & LDP_ATTR_LSPID) {
170 memcpy(&attr->lspidTlv, &req->lspidTlv, sizeof(mplsLdpLspIdTlv_t));
171 attr->lspidTlvExists = 1;
173 if (req->trafficTlvExists && flag & LDP_ATTR_TRAFFIC) {
174 memcpy(&attr->trafficTlv, &req->trafficTlv, sizeof(mplsLdpTrafficTlv_t));
175 attr->trafficTlvExists = 1;
179 void ldp_label_request_initial_callback(mpls_timer_handle timer, void *extra,
180 mpls_cfg_handle handle)
182 ldp_session *s = (ldp_session *) extra;
183 ldp_global *g = (ldp_global*)handle;
184 ldp_session *nh_session = NULL;
185 mpls_bool done = MPLS_BOOL_FALSE;
186 mpls_fec dest;
188 LDP_ENTER(g->user_data, "ldp_label_request_initial_callback");
190 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_ALL, LDP_TRACE_FLAG_TIMER,
191 "Initial Label Request Callback fired: session(%d)\n", s->index);
193 mpls_lock_get(g->global_lock);
195 mpls_timer_stop(g->timer_handle, timer);
197 if (mpls_fib_getfirst_route(g->fib_handle, &dest) == MPLS_SUCCESS) {
198 do {
200 ldp_fec *fnode = _ldp_attr_get_fec2(g, &dest, MPLS_BOOL_FALSE);
201 ldp_attr *attr = NULL;
202 ldp_fs *fs = NULL;
203 ldp_attr *ds_attr = NULL;
204 mpls_return_enum result;
206 /* check to see if export policy allows us to 'see' this route */
207 if (mpls_policy_export_check(g->user_data, &dest, &dest.nh)
208 == MPLS_BOOL_FALSE) {
209 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_ALL, LDP_TRACE_FLAG_DEBUG,
210 "Rejected by export policy\n");
211 continue;
214 /* find the next hop session corresponding to this FEC */
215 result = ldp_get_next_hop_session_for_fec(g,&dest,&nh_session);
217 /* do we have a valid next hop session, and is the nexp hop session
218 * this session? */
219 if (result != MPLS_SUCCESS || nh_session->index != s->index) {
220 continue;
223 /* have we already sent a label request to this peer for this FEC? */
224 if (ldp_attr_find_downstream_state(g, s, &dest, LDP_LSP_STATE_REQ_SENT)) {
225 continue;
228 /* clear out info from the last FEC */
229 ds_attr = NULL;
231 /* jleu: duplicate code from ldp_attr_find_upstream_state_any */
232 fnode = _ldp_attr_get_fec2(g, &dest, MPLS_BOOL_FALSE);
233 if (!fnode) {
234 continue;
237 fs = MPLS_LIST_HEAD(&fnode->fs_root_us);
238 while (fs) {
239 attr = MPLS_LIST_HEAD(&fs->attr_root);
240 while (attr) {
241 if (attr->state == LDP_LSP_STATE_REQ_RECV ||
242 attr->state == LDP_LSP_STATE_MAP_SENT) {
243 if (!ds_attr) {
244 /* this is not neccessarily going to be XC'd to something */
245 ldp_label_request_for_xc(g, s, &dest, attr, &ds_attr);
248 attr = MPLS_LIST_NEXT(&fs->attr_root, attr, _fs);
250 fs = MPLS_LIST_NEXT(&fnode->fs_root_us, fs, _fec);
253 if (!ds_attr) {
255 * we did not find any received requests or sent mappings so
256 * send a request and xc it to nothing
258 ldp_label_request_for_xc(g, s, &dest, NULL, &ds_attr);
261 } while (mpls_fib_getnext_route(g->fib_handle, &dest) == MPLS_SUCCESS);
262 done = MPLS_BOOL_TRUE;
265 if (done == MPLS_BOOL_TRUE) {
266 mpls_timer_delete(g->timer_handle, timer);
267 MPLS_REFCNT_RELEASE(s, ldp_session_delete);
268 s->initial_distribution_timer = (mpls_timer_handle) 0;
269 } else {
270 mpls_timer_start(g->timer_handle, timer, MPLS_TIMER_ONESHOT);
271 /* need to mark the session with where it left off */
274 mpls_lock_release(g->global_lock);
276 LDP_EXIT(g->user_data, "ldp_label_request_initial_callback");
279 void Prepare_Label_Request_Attributes(ldp_global * g, ldp_session * s,
280 mpls_fec * fec, ldp_attr * r_attr, ldp_attr * s_attr)
282 int i;
284 MPLS_ASSERT(s && r_attr);
286 if (!(s->oper_loop_detection == LDP_LOOP_HOPCOUNT ||
287 s->oper_loop_detection == LDP_LOOP_HOPCOUNT_PATHVECTOR ||
288 r_attr->hopCountTlvExists)) { /* PRqA.1 */
289 return;
292 /* is this LSR allowed to be an LER for FEC? *//* PRqA.2 */
293 /* some policy gunk needs to be checked here */
294 /* if not goto PRqA.6 */
296 s_attr->hopCountTlvExists = 1; /* PRqA.3 */
297 s_attr->hopCountTlv.hcValue = 1;
299 if (s->oper_loop_detection == LDP_LOOP_NONE) { /* PRqA.4 */
300 return;
303 if (g->label_merge == MPLS_BOOL_TRUE) { /* PRqA.5 */
304 return;
306 goto Prepare_Label_Request_Attributes_13;
308 if (r_attr && r_attr->hopCountTlvExists) { /* PRqA.6 */
309 s_attr->hopCountTlvExists = 1; /* PRqA.7 */
310 s_attr->hopCountTlv.hcValue = (r_attr->hopCountTlv.hcValue) ?
311 (r_attr->hopCountTlv.hcValue + 1) : 0;
312 } else {
313 s_attr->hopCountTlvExists = 1; /* PRqA.8 */
314 s_attr->hopCountTlv.hcValue = 0;
317 if (s->oper_loop_detection == LDP_LOOP_NONE) { /* PRqA.9 */
318 return;
321 if (r_attr && r_attr->pathVecTlvExists) { /* PRqA.10 */
322 goto Prepare_Label_Request_Attributes_12;
325 if (g->label_merge == MPLS_BOOL_TRUE) { /* PRqA.11 */
326 return;
328 goto Prepare_Label_Request_Attributes_13;
330 Prepare_Label_Request_Attributes_12:
331 /* we only get to PRqA.12 if we have verified we have a r_attr */
332 s_attr->pathVecTlvExists = 1;
333 s_attr->pathVecTlv.lsrId[0] = g->lsr_identifier.u.ipv4;
334 for (i = 1; i < (MPLS_MAXHOPSNUMBER - 1); i++) {
335 if (r_attr->pathVecTlv.lsrId[i - 1]) {
336 s_attr->pathVecTlv.lsrId[i] = r_attr->pathVecTlv.lsrId[i - 1];
339 return;
341 Prepare_Label_Request_Attributes_13:
342 s_attr->pathVecTlvExists = 1;
343 s_attr->pathVecTlv.lsrId[0] = g->lsr_identifier.u.ipv4;
346 mpls_return_enum ldp_label_request_process(ldp_global * g, ldp_session * s,
347 ldp_adj * a, ldp_entity * e, ldp_attr * us_attr, mpls_fec * fec)
349 ldp_session *nh_session = NULL;
350 ldp_attr_list *us_list = NULL;
351 mpls_bool egress = MPLS_BOOL_FALSE;
352 ldp_attr *ds_attr = NULL;
353 ldp_attr *us_temp = NULL;
355 if (Check_Received_Attributes(g, s, us_attr, MPLS_LBLREQ_MSGTYPE) != MPLS_SUCCESS) { /* LRp.1 */
356 goto LRq_13;
359 switch (ldp_get_next_hop_session_for_fec(g, fec, &nh_session)) {
360 case MPLS_SUCCESS: /* LRq.2 */
362 if (nh_session == NULL) {
363 egress = MPLS_BOOL_TRUE;
365 if (nh_session != NULL && s->index == nh_session->index) { /* LRq.3 */
366 ldp_notif_send(g, s, us_attr, LDP_NOTIF_LOOP_DETECTED); /* LRq.4 */
367 goto LRq_13;
369 break;
371 case MPLS_FAILURE:
372 case MPLS_NO_ROUTE:
374 ldp_notif_send(g, s, us_attr, LDP_NOTIF_NO_ROUTE); /* LRq.5 */
375 goto LRq_13;
377 default:
379 MPLS_ASSERT(0);
383 if ((us_list = ldp_attr_find_upstream_all(g, s, fec)) != NULL) {
384 us_temp = MPLS_LIST_HEAD(us_list);
385 while (us_temp != NULL) {
386 if (us_temp->state == LDP_LSP_STATE_REQ_RECV && /* LRq.6 */
387 us_temp->msg_id == us_attr->msg_id) { /* LRq.7 */
388 goto LRq_13;
390 us_temp = MPLS_LIST_NEXT(us_list, us_temp, _fs);
394 us_attr->state = LDP_LSP_STATE_REQ_RECV; /* LRq.8 */
396 if (ldp_attr_insert_upstream(g, s, us_attr) != MPLS_SUCCESS) {
397 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_RECV, LDP_TRACE_FLAG_ERROR,
398 "Couldn't insert recv attributes in tree\n");
399 goto ldp_label_request_process_error;
402 if (nh_session) {
403 ds_attr = ldp_attr_find_downstream_state(g, nh_session, fec,
404 LDP_LSP_STATE_MAP_RECV);
405 } else {
406 ds_attr = NULL;
409 if (g->lsp_control_mode == LDP_CONTROL_INDEPENDENT) { /* LRq.9 */
410 if (ldp_label_mapping_with_xc(g, s, fec, &us_attr, ds_attr) != MPLS_SUCCESS) {
411 goto ldp_label_request_process_error;
414 if (egress == MPLS_BOOL_TRUE || ds_attr) {
415 goto LRq_11;
417 } else {
418 if ((!(egress == MPLS_BOOL_TRUE || ds_attr)) || (g->label_merge == MPLS_BOOL_FALSE)) {
419 goto LRq_10;
422 if (ldp_label_mapping_with_xc(g, s, fec, &us_attr, ds_attr) != MPLS_SUCCESS) {
423 goto ldp_label_request_process_error;
425 goto LRq_11;
428 LRq_10:
429 ds_attr = NULL;
430 if (ldp_label_request_for_xc(g, nh_session, fec, us_attr, &ds_attr) !=
431 MPLS_SUCCESS) {
432 goto ldp_label_request_process_error;
435 LRq_11:
436 /* the work done by LRq_11 is handled in ldp_label_mapping_with_xc() */
437 LRq_13:
438 if (ds_attr != NULL && ds_attr->in_tree == MPLS_BOOL_FALSE) {
439 ldp_attr_remove_complete(g, ds_attr, MPLS_BOOL_FALSE);
441 return MPLS_SUCCESS;
443 ldp_label_request_process_error:
444 return MPLS_FAILURE;