From: Vasu Dasari <vdasari@gmail.com>
[mpls-ldp-portable.git] / ldp / ldp_keepalive.c
bloba71f81fd42df4919b749f7a6c993c84ba1741adf
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_mesg.h"
12 #include "ldp_nortel.h"
13 #include "ldp_buf.h"
14 #include "ldp_keepalive.h"
15 #include "ldp_session.h"
16 #include "ldp_pdu_setup.h"
18 #include "mpls_assert.h"
19 #include "mpls_socket_impl.h"
20 #include "mpls_timer_impl.h"
21 #include "mpls_lock_impl.h"
22 #include "mpls_trace_impl.h"
24 void ldp_keepalive_timeout_callback(mpls_timer_handle timer, void *extra,
25 mpls_cfg_handle handle)
27 ldp_session *s = (ldp_session *) extra;
28 ldp_global *g = (ldp_global*)handle;
30 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_ALL, LDP_TRACE_FLAG_TIMER,
31 "Keepalive Timeout fired: session(%d)\n", s->index);
33 mpls_lock_get(g->global_lock);
35 s->shutdown_notif = LDP_NOTIF_KEEPALIVE_TIMER_EXPIRED;
36 s->shutdown_fatal = MPLS_BOOL_FALSE;
37 /* we should go into backoff, so don't completly kill the session */
38 ldp_session_shutdown(g, s, MPLS_BOOL_FALSE);
39 MPLS_REFCNT_RELEASE(s, ldp_session_delete);
41 mpls_lock_release(g->global_lock);
44 void ldp_keepalive_send_callback(mpls_timer_handle timer, void *extra,
45 mpls_cfg_handle handle)
47 ldp_session *s = (ldp_session *) extra;
48 ldp_global *g = (ldp_global*)handle;
50 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_ALL, LDP_TRACE_FLAG_TIMER,
51 "Keepalive Send fired: session(%d)\n", s->index);
53 mpls_lock_get(g->global_lock);
54 ldp_keepalive_send(g, s);
55 mpls_lock_release(g->global_lock);
58 ldp_mesg *ldp_keepalive_create(uint32_t msgid)
60 ldp_mesg *msg = NULL;
62 msg = ldp_mesg_create();
63 ldp_mesg_prepare(msg, MPLS_KEEPAL_MSGTYPE, msgid);
65 return msg;
68 void ldp_keepalive_set_message_id(ldp_mesg * msg, uint32_t msgid)
70 mplsLdpKeepAlMsg_t *keep;
72 MPLS_ASSERT(msg);
73 keep = &msg->u.keep;
74 setBaseMsgId(&(keep->baseMsg), msgid);
77 mpls_return_enum ldp_keepalive_send(ldp_global * g, ldp_session * s)
79 MPLS_ASSERT(s);
81 if (s->keepalive == NULL) {
82 if ((s->keepalive = ldp_keepalive_create(g->message_identifier++)) == NULL) {
83 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_ALL, LDP_TRACE_FLAG_ERROR,
84 "ldp_keepalive_send: error creating keepalve\n");
85 return MPLS_FAILURE;
87 } else {
88 ldp_keepalive_set_message_id(s->keepalive, g->message_identifier++);
91 if (mpls_timer_handle_verify(g->timer_handle, s->keepalive_recv_timer) ==
92 MPLS_BOOL_FALSE) {
93 MPLS_REFCNT_HOLD(s);
94 s->keepalive_recv_timer = mpls_timer_create(g->timer_handle, MPLS_UNIT_SEC,
95 s->oper_keepalive, (void *)s, g, ldp_keepalive_timeout_callback);
96 if (mpls_timer_handle_verify(g->timer_handle, s->keepalive_recv_timer) ==
97 MPLS_BOOL_FALSE) {
98 MPLS_REFCNT_RELEASE(s, ldp_session_delete);
99 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_ALL, LDP_TRACE_FLAG_ERROR,
100 "ldp_keepalive_send: error creating timer\n");
101 return MPLS_FAILURE;
103 mpls_timer_start(g->timer_handle, s->keepalive_recv_timer, MPLS_TIMER_ONESHOT);
106 if (mpls_timer_handle_verify(g->timer_handle, s->keepalive_send_timer) ==
107 MPLS_BOOL_FALSE) {
108 MPLS_REFCNT_HOLD(s);
109 s->keepalive_send_timer = mpls_timer_create(g->timer_handle, MPLS_UNIT_SEC,
110 s->oper_keepalive_interval, (void *)s, g, ldp_keepalive_send_callback);
111 if (mpls_timer_handle_verify(g->timer_handle, s->keepalive_send_timer) ==
112 MPLS_BOOL_FALSE) {
113 MPLS_REFCNT_RELEASE(s, ldp_session_delete);
114 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_ALL, LDP_TRACE_FLAG_ERROR,
115 "ldp_keepalive_send: error creating timer\n");
116 return MPLS_FAILURE;
118 mpls_timer_start(g->timer_handle, s->keepalive_send_timer, MPLS_TIMER_REOCCURRING);
121 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_SEND, LDP_TRACE_FLAG_PERIODIC,
122 "Keepalive Send: session(%d)\n", s->index);
124 ldp_mesg_send_tcp(g, s, s->keepalive);
126 return MPLS_SUCCESS;