lacp: passive mode support [VPP-1551]
[vpp.git] / src / plugins / lacp / tx_machine.c
1 /*
2  * Copyright (c) 2017 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #define _GNU_SOURCE
17
18 #include <vnet/bonding/node.h>
19 #include <lacp/node.h>
20
21 /*
22  *  LACP State = TRANSMIT
23  */
24 static lacp_fsm_state_t lacp_tx_state_transmit[] = {
25   {LACP_ACTION_TRANSMIT, LACP_TX_STATE_TRANSMIT},       // event 0 BEGIN
26   {LACP_ACTION_TRANSMIT, LACP_TX_STATE_TRANSMIT},       // event 1 NTT
27 };
28
29 static lacp_fsm_machine_t lacp_tx_fsm_table[] = {
30   {lacp_tx_state_transmit},
31 };
32
33 lacp_machine_t lacp_tx_machine = {
34   lacp_tx_fsm_table,
35   lacp_tx_debug_func,
36 };
37
38 int
39 lacp_tx_action_transmit (void *p1, void *p2)
40 {
41   vlib_main_t *vm = (vlib_main_t *) p1;
42   slave_if_t *sif = (slave_if_t *) p2;
43   lacp_main_t *lm = &lacp_main;
44   f64 now = vlib_time_now (lm->vlib_main);
45
46   if (!lacp_timer_is_running (sif->periodic_timer))
47     return 0;
48
49   // No more than 3 LACPDUs per fast interval
50   if (now <= (sif->last_lacpdu_sent_time + 0.333))
51     return 0;
52
53   if (sif->ntt)
54     {
55       lacp_send_lacp_pdu (vm, sif);
56       lacp_schedule_periodic_timer (lm->vlib_main, sif);
57     }
58   sif->ntt = 0;
59
60   return 0;
61 }
62
63 static u8 *
64 format_tx_event (u8 * s, va_list * args)
65 {
66   static lacp_event_struct lacp_tx_event_array[] = {
67 #define _(b, s, n) {.bit = b, .str = #s, },
68     foreach_lacp_tx_event
69 #undef _
70     {.str = NULL}
71   };
72   int e = va_arg (*args, int);
73   lacp_event_struct *event_entry =
74     (lacp_event_struct *) & lacp_tx_event_array;
75
76   if (e >= (sizeof (lacp_tx_event_array) / sizeof (*event_entry)))
77     s = format (s, "Bad event %d", e);
78   else
79     s = format (s, "%s", event_entry[e].str);
80
81   return s;
82 }
83
84 void
85 lacp_tx_debug_func (slave_if_t * sif, int event, int state,
86                     lacp_fsm_state_t * transition)
87 {
88   clib_warning ("%U-TX: event %U, old state %U, new state %U",
89                 format_vnet_sw_if_index_name, vnet_get_main (),
90                 sif->sw_if_index, format_tx_event,
91                 event, format_tx_sm_state, state, format_tx_sm_state,
92                 transition->next_state);
93 }
94
95 void
96 lacp_init_tx_machine (vlib_main_t * vm, slave_if_t * sif)
97 {
98   lacp_machine_dispatch (&lacp_tx_machine, vm, sif, LACP_TX_EVENT_BEGIN,
99                          &sif->tx_state);
100 }
101
102 /*
103  * fd.io coding-style-patch-verification: ON
104  *
105  * Local Variables:
106  * eval: (c-set-style "gnu")
107  * End:
108  */