3 * ===================================
4 * HARP | Host ATM Research Platform
5 * ===================================
8 * This Host ATM Research Platform ("HARP") file (the "Software") is
9 * made available by Network Computing Services, Inc. ("NetworkCS")
10 * "AS IS". NetworkCS does not provide maintenance, improvements or
11 * support of any kind.
13 * NETWORKCS MAKES NO WARRANTIES OR REPRESENTATIONS, EXPRESS OR IMPLIED,
14 * INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
15 * AND FITNESS FOR A PARTICULAR PURPOSE, AS TO ANY ELEMENT OF THE
16 * SOFTWARE OR ANY SUPPORT PROVIDED IN CONNECTION WITH THIS SOFTWARE.
17 * In no event shall NetworkCS be responsible for any damages, including
18 * but not limited to consequential damages, arising from or relating to
19 * any use of the Software or related support.
21 * Copyright 1994-1998 Network Computing Services, Inc.
23 * Copies of this Software may be made, however, the above copyright
24 * notice must be reproduced on all copies.
26 * @(#) $FreeBSD: src/usr.sbin/atm/scspd/scsp_hfsm.c,v 1.3 1999/08/28 01:15:33 peter Exp $
27 * @(#) $DragonFly: src/usr.sbin/atm/scspd/scsp_hfsm.c,v 1.3 2003/11/03 19:31:35 eirikn Exp $
32 * Server Cache Synchronization Protocol (SCSP) Support
33 * ----------------------------------------------------
35 * HELLO finite state machine
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <netatm/queue.h>
45 #include <netatm/atm.h>
46 #include <netatm/atm_if.h>
47 #include <netatm/atm_sap.h>
48 #include <netatm/atm_sys.h>
49 #include <netatm/atm_ioctl.h>
63 #define HELLO_ACTION_CNT 7
64 int scsp_hello_act_00(Scsp_dcs *, Scsp_msg *);
65 int scsp_hello_act_01(Scsp_dcs *, Scsp_msg *);
66 int scsp_hello_act_02(Scsp_dcs *, Scsp_msg *);
67 int scsp_hello_act_03(Scsp_dcs *, Scsp_msg *);
68 int scsp_hello_act_04(Scsp_dcs *, Scsp_msg *);
69 int scsp_hello_act_05(Scsp_dcs *, Scsp_msg *);
70 int scsp_hello_act_06(Scsp_dcs *, Scsp_msg *);
72 static int (*scsp_action_vector[HELLO_ACTION_CNT])() = {
83 * HELLO FSM state table
85 static int hello_state_table[SCSP_HFSM_EVENT_CNT][SCSP_HFSM_STATE_CNT] = {
87 { 1, 1, 1, 1 }, /* 0 */
88 { 0, 2, 2, 2 }, /* 1 */
89 { 0, 3, 3, 3 }, /* 2 */
90 { 0, 0, 4, 4 }, /* 3 */
91 { 0, 5, 5, 6 }, /* 4 */
95 * HELLO finite state machine
98 * dcsp pointer to a DCS control block for the neighbor
99 * event the event which has occurred
100 * msg pointer to received message, if there is one
104 * errno error encountered
108 scsp_hfsm(dcsp, event, msg)
113 int action, rc, state;
116 * Select an action from the state table
118 state = dcsp->sd_hello_state;
119 action = hello_state_table[event][state];
120 if (scsp_trace_mode & SCSP_TRACE_HFSM) {
121 scsp_trace("HFSM: state=%d, event=%d, action=%d\n",
122 state, event, action);
124 if (action >= HELLO_ACTION_CNT || action <= 0) {
125 scsp_log(LOG_ERR, "Hello FSM--invalid action %d; state=%d, event=%d",
126 action, dcsp->sd_hello_state, event);
131 * Perform the selected action
133 rc = scsp_action_vector[action](dcsp, msg);
140 * HELLO finite state machine action 0
141 * Unexpected action -- log an error message
144 * dcsp pointer to DCS control block
145 * msg pointer to received message (ignored)
148 * EOPNOTSUPP always returns EOPNOTSUPP
152 scsp_hello_act_00(dcsp, msg)
156 scsp_log(LOG_ERR, "Hello FSM error--unexpected action, state=%d",
157 dcsp->sd_hello_state);
163 * HELLO finite state machine action 1
164 * VCC open -- send HELLO message, start hello timer, go to Waiting
168 * dcsp pointer to DCS control block
169 * msg pointer to received message (ignored)
173 * errno error encountered
177 scsp_hello_act_01(dcsp, msg)
184 * Cancel the VCC open timer if it's running
186 HARP_CANCEL(&dcsp->sd_open_t);
189 * Go to Waiting state
191 dcsp->sd_hello_state = SCSP_HFSM_WAITING;
194 * Send a Hello message
196 rc = scsp_send_hello(dcsp);
199 * Success--start the Hello timer
201 HARP_TIMER(&dcsp->sd_hello_h_t, SCSP_HELLO_Interval,
210 * HELLO finite state machine action 2
211 * VCC closed -- notify CA FSM, go to Down state, try to re-open VCC
214 * dcsp pointer to DCS control block
215 * msg pointer to received message (ignored)
219 * errno error encountered
223 scsp_hello_act_02(dcsp, msg)
230 * Cancel any current timers
232 HARP_CANCEL(&dcsp->sd_hello_h_t);
233 HARP_CANCEL(&dcsp->sd_hello_rcv_t);
236 * Log the loss of the VCC
238 if (dcsp->sd_hello_state > SCSP_HFSM_WAITING) {
239 scsp_log(LOG_ERR, "VC to %s closed",
240 format_atm_addr(&dcsp->sd_addr));
244 * Tell the CA FSM that the conection to the DCS is lost
246 rc = scsp_cafsm(dcsp, SCSP_CAFSM_HELLO_DOWN, (void *)0);
251 dcsp->sd_hello_state = SCSP_HFSM_DOWN;
254 * If our ID is lower than the DCS's, wait a second before
255 * trying to connect. This should keep both of us from
256 * trying to connect at the same time, resulting in two
259 if (scsp_cmp_id(&dcsp->sd_server->ss_lsid,
260 &dcsp->sd_dcsid) < 0) {
262 * Our ID is lower--start the VCC open timer for one
263 * second so we'll try to open the VCC if the DCS
264 * doesn't do it by then
266 HARP_TIMER(&dcsp->sd_open_t, 1, scsp_open_timeout);
269 * Our ID is higher--try to reopen the VCC immediately
271 if (scsp_dcs_connect(dcsp)) {
273 * Conncect failed -- set a timer and try
276 HARP_TIMER(&dcsp->sd_open_t, SCSP_Open_Interval,
286 * HELLO finite state machine action 3
287 * Hello timer expired -- send HELLO message, restart hello timer
290 * dcsp pointer to DCS control block
291 * msg pointer to received message (ignored)
295 * errno error encountered
299 scsp_hello_act_03(dcsp, msg)
306 * Send a Hello message
308 rc = scsp_send_hello(dcsp);
311 * Success--restart the Hello timer
313 HARP_TIMER(&dcsp->sd_hello_h_t, SCSP_HELLO_Interval,
322 * HELLO finite state machine action 4
323 * Receive timer expired -- if we haven't received any Hellos, notify
324 * CA FSM and go to Waiting state; if we've received Hellos, but we
325 * weren't in the receiver ID list, go to Unidirectional state
328 * dcsp pointer to DCS control block
329 * msg pointer to received message (ignored)
333 * errno error encountered
337 scsp_hello_act_04(dcsp, msg)
344 * Check whether we'ver received any Hellos lately
346 if (dcsp->sd_hello_rcvd) {
348 * We've had Hellos since the receive timer was
349 * started--go to Unidirectional state
351 dcsp->sd_hello_rcvd = 0;
352 dcsp->sd_hello_state = SCSP_HFSM_UNI_DIR;
355 * We haven't seen any Hellos at all from the DCS in
356 * hello_interval * dead_factor seconds--go to Waiting
359 dcsp->sd_hello_state = SCSP_HFSM_WAITING;
365 rc = scsp_cafsm(dcsp, SCSP_CAFSM_HELLO_DOWN, (void *)0);
372 * HELLO finite state machine action 5
373 * Message received -- Ignore all but HELLO messages; if local server
374 * is in receiver list, notify CA FSM and go to Bidirectional state;
375 * otherwise, go to Unidirectional state
378 * dcsp pointer to DCS control block
379 * msg pointer to received message
383 * errno error encountered
387 scsp_hello_act_05(dcsp, msg)
395 * Null message pointer means message decode failed, so
396 * message must have been invalid. Go to Waiting state.
398 if (msg == (Scsp_msg *)0) {
399 dcsp->sd_hello_state = SCSP_HFSM_WAITING;
400 HARP_CANCEL(&dcsp->sd_hello_rcv_t);
405 * Ignore the message if it isn't a Hello
407 if (msg->sc_msg_type != SCSP_HELLO_MSG) {
412 * Save relevant information about DCS, but don't let him give
413 * us zero for timeout values
415 if (msg->sc_hello->hello_int) {
416 dcsp->sd_hello_int = msg->sc_hello->hello_int;
418 dcsp->sd_hello_int = 1;
420 if (msg->sc_hello->dead_factor) {
421 dcsp->sd_hello_df = msg->sc_hello->dead_factor;
423 dcsp->sd_hello_df = 1;
425 dcsp->sd_dcsid = msg->sc_hello->hello_mcp.sid;
428 * Check the message for the local server's ID
430 for (ridp = &msg->sc_hello->hello_mcp.rid;
433 if (scsp_cmp_id(&dcsp->sd_server->ss_lsid, ridp) == 0) {
435 * Cancel and restart the receive timer
437 HARP_CANCEL(&dcsp->sd_hello_rcv_t);
438 HARP_TIMER(&dcsp->sd_hello_rcv_t,
439 dcsp->sd_hello_int * dcsp->sd_hello_df,
440 scsp_hello_rcv_timeout);
443 * Go to Bidirectional state and notify the
444 * CA FSM that the connection is up
446 dcsp->sd_hello_state = SCSP_HFSM_BI_DIR;
447 rc = scsp_cafsm(dcsp,
455 * We weren't in the receiver ID list, so go to
456 * Unidirectional state
458 dcsp->sd_hello_state = SCSP_HFSM_UNI_DIR;
465 * HELLO finite state machine action 6
466 * Message received -- if message is not a HELLO, pass it to the CA
467 * FSM; otherwise, if local server is not in receiver list, notify
468 * CA FSM and go to Unidirectional state
471 * dcsp pointer to DCS control block
472 * msg pointer to received message
476 * errno error encountered
480 scsp_hello_act_06(dcsp, msg)
484 int rc = 0, rcv_found;
488 * Null message pointer means message decode failed, so
489 * message must have been invalid. Go to Waiting state.
491 if (msg == (Scsp_msg *)0) {
492 HARP_CANCEL(&dcsp->sd_hello_rcv_t);
493 dcsp->sd_hello_state = SCSP_HFSM_WAITING;
494 rc = scsp_cafsm(dcsp, SCSP_CAFSM_HELLO_DOWN, (void *)0);
499 * Process the message depending on its type
501 switch(msg->sc_msg_type) {
503 rc = scsp_cafsm(dcsp, SCSP_CAFSM_CA_MSG, (void *)msg);
505 case SCSP_CSU_REQ_MSG:
506 rc = scsp_cafsm(dcsp, SCSP_CAFSM_CSU_REQ, (void *)msg);
508 case SCSP_CSU_REPLY_MSG:
509 rc = scsp_cafsm(dcsp, SCSP_CAFSM_CSU_REPLY,
513 rc = scsp_cafsm(dcsp, SCSP_CAFSM_CSUS_MSG, (void *)msg);
517 * Make sure DCS info is consistent. The sender ID,
518 * family ID, protocol ID, and server group ID are
521 if (scsp_cmp_id(&msg->sc_hello->hello_mcp.sid,
523 (msg->sc_hello->family_id !=
524 dcsp->sd_server->ss_fid) ||
525 (msg->sc_hello->hello_mcp.pid !=
526 dcsp->sd_server->ss_pid) ||
527 (msg->sc_hello->hello_mcp.sgid !=
528 dcsp->sd_server->ss_sgid)) {
530 * Bad info--revert to waiting state
532 HARP_CANCEL(&dcsp->sd_hello_rcv_t);
533 dcsp->sd_hello_state = SCSP_HFSM_WAITING;
534 rc = scsp_cafsm(dcsp,
535 SCSP_CAFSM_HELLO_DOWN,
541 * Mark the arrival of the Hello message
543 dcsp->sd_hello_rcvd = 1;
546 * Check the message for the local server's ID
548 for (ridp = &msg->sc_hello->hello_mcp.rid,
552 rcv_found = (scsp_cmp_id(ridp,
553 &dcsp->sd_server->ss_lsid) == 0);
558 * The LS ID was in the list of receiver IDs--
559 * Reset the Hello receive timer
561 dcsp->sd_hello_rcvd = 0;
562 HARP_CANCEL(&dcsp->sd_hello_rcv_t);
563 HARP_TIMER(&dcsp->sd_hello_rcv_t,
566 scsp_hello_rcv_timeout);