build: add -Wall and -fno-common, fix reported issues
[vpp.git] / src / plugins / lacp / node.h
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 #ifndef __included_lacp_node_h__
16 #define __included_lacp_node_h__
17
18 #include <vlib/vlib.h>
19 #include <vlib/unix/unix.h>
20 #include <vppinfra/format.h>
21 #include <vppinfra/hash.h>
22 #include <lacp/protocol.h>
23 #include <lacp/rx_machine.h>
24 #include <lacp/tx_machine.h>
25 #include <lacp/mux_machine.h>
26 #include <lacp/ptx_machine.h>
27
28 typedef enum
29 {
30   LACP_PACKET_TEMPLATE_ETHERNET,
31   LACP_N_PACKET_TEMPLATES,
32 } lacp_packet_template_id_t;
33
34 typedef enum
35 {
36   MARKER_PACKET_TEMPLATE_ETHERNET,
37   MARKER_N_PACKET_TEMPLATES,
38 } marker_packet_template_id_t;
39
40 typedef enum
41 {
42   LACP_PROCESS_EVENT_START = 1,
43   LACP_PROCESS_EVENT_STOP = 2,
44 } lacp_process_event_t;
45
46 #define LACP_DBG(n, args...)                    \
47   {                                             \
48     lacp_main_t *_lm = &lacp_main;              \
49     if (_lm->debug || n->debug)                 \
50       clib_warning (args);                      \
51   }
52
53 #define LACP_DBG2(n, e, s, m, t)                  \
54   {                                               \
55     lacp_main_t *_lm = &lacp_main;                \
56     if ((m)->debug && (_lm->debug || (n)->debug)) \
57       (*m->debug)(n, e, s, t);                    \
58   }
59
60 /* Packet counters */
61 #define foreach_lacp_error                                               \
62 _ (NONE, "good lacp packets -- consumed")                                \
63 _ (CACHE_HIT, "good lacp packets -- cache hit")                          \
64 _ (UNSUPPORTED, "unsupported slow protocol packets")                     \
65 _ (TOO_SMALL, "bad lacp packets -- packet too small")                    \
66 _ (BAD_TLV, "bad lacp packets -- bad TLV length")                        \
67 _ (DISABLED, "lacp packets received on disabled interfaces")
68
69 typedef enum
70 {
71 #define _(sym,str) LACP_ERROR_##sym,
72   foreach_lacp_error
73 #undef _
74     LACP_N_ERROR,
75 } lacp_error_t;
76
77 #define SECS_IN_A_DAY 86400.0
78
79 /* lacp packet trace capture */
80 typedef struct
81 {
82   u32 sw_if_index;
83   u32 len;
84   union
85   {
86     marker_pdu_t marker;
87     lacp_pdu_t lacpdu;
88   } pkt;
89 } lacp_input_trace_t;
90
91 /** LACP interface details struct */
92 typedef struct
93 {
94   u32 sw_if_index;
95   u8 interface_name[64];
96   u32 rx_state;
97   u32 tx_state;
98   u32 mux_state;
99   u32 ptx_state;
100   u8 bond_interface_name[64];
101   u16 actor_system_priority;
102   u8 actor_system[6];
103   u16 actor_key;
104   u16 actor_port_priority;
105   u16 actor_port_number;
106   u8 actor_state;
107   u16 partner_system_priority;
108   u8 partner_system[6];
109   u16 partner_key;
110   u16 partner_port_priority;
111   u16 partner_port_number;
112   u8 partner_state;
113 } lacp_interface_details_t;
114
115 typedef struct
116 {
117   /** API message ID base */
118   u16 msg_id_base;
119
120   /* convenience variables */
121   vlib_main_t *vlib_main;
122   vnet_main_t *vnet_main;
123
124   /* Background process node index */
125   u32 lacp_process_node_index;
126
127   /* Packet templates for different encap types */
128   vlib_packet_template_t packet_templates[LACP_N_PACKET_TEMPLATES];
129
130   /* Packet templates for different encap types */
131   vlib_packet_template_t marker_packet_templates[MARKER_N_PACKET_TEMPLATES];
132
133   /* LACP interface count */
134   volatile u32 lacp_int;
135
136   /* debug is on or off */
137   u8 debug;
138 } lacp_main_t;
139
140 extern lacp_state_struct lacp_state_array[];
141 extern lacp_main_t lacp_main;
142
143 void lacp_create_periodic_process (void);
144 clib_error_t *lacp_plugin_api_hookup (vlib_main_t * vm);
145 int lacp_dump_ifs (lacp_interface_details_t ** out_bondids);
146 lacp_error_t lacp_input (vlib_main_t * vm, vlib_buffer_t * b0, u32 bi0);
147 void lacp_periodic (vlib_main_t * vm);
148 u8 *lacp_input_format_trace (u8 * s, va_list * args);
149 void lacp_init_neighbor (slave_if_t * sif, u8 * hw_address,
150                          u16 port_number, u32 group);
151 void lacp_init_state_machines (vlib_main_t * vm, slave_if_t * sif);
152 void lacp_init_rx_machine (vlib_main_t * vm, slave_if_t * sif);
153 void lacp_init_tx_machine (vlib_main_t * vm, slave_if_t * sif);
154 void lacp_init_ptx_machine (vlib_main_t * vm, slave_if_t * sif);
155 void lacp_init_mux_machine (vlib_main_t * vm, slave_if_t * sif);
156 void lacp_selection_logic (vlib_main_t * vm, slave_if_t * sif);
157 void lacp_send_lacp_pdu (vlib_main_t * vm, slave_if_t * sif);
158
159 static inline void
160 lacp_stop_timer (f64 * timer)
161 {
162   *timer = 0.0;
163 }
164
165 static inline u8
166 lacp_timer_is_running (f64 timer)
167 {
168   return (timer != 0.0);
169 }
170
171 static inline u8
172 lacp_timer_is_expired (vlib_main_t * vm, f64 timer)
173 {
174   f64 now = vlib_time_now (vm);
175
176   return (now >= timer);
177 }
178
179 static inline u8 *
180 format_rx_sm_state (u8 * s, va_list * args)
181 {
182   lacp_state_struct lacp_rx_sm_state_array[] = {
183 #define _(b, s, n) {.bit = b, .str = #s, },
184     foreach_lacp_rx_sm_state
185 #undef _
186     {.str = NULL}
187   };
188   int state = va_arg (*args, int);
189   lacp_state_struct *state_entry =
190     (lacp_state_struct *) & lacp_rx_sm_state_array;
191
192   if (state >= (sizeof (lacp_rx_sm_state_array) / sizeof (*state_entry)))
193     s = format (s, "Bad state %d", state);
194   else
195     s = format (s, "%s", state_entry[state].str);
196
197   return s;
198 }
199
200 static inline u8 *
201 format_tx_sm_state (u8 * s, va_list * args)
202 {
203   lacp_state_struct lacp_tx_sm_state_array[] = {
204 #define _(b, s, n) {.bit = b, .str = #s, },
205     foreach_lacp_tx_sm_state
206 #undef _
207     {.str = NULL}
208   };
209   int state = va_arg (*args, int);
210   lacp_state_struct *state_entry =
211     (lacp_state_struct *) & lacp_tx_sm_state_array;
212
213   if (state >= (sizeof (lacp_tx_sm_state_array) / sizeof (*state_entry)))
214     s = format (s, "Bad state %d", state);
215   else
216     s = format (s, "%s", state_entry[state].str);
217
218   return s;
219 }
220
221 static inline u8 *
222 format_mux_sm_state (u8 * s, va_list * args)
223 {
224   lacp_state_struct lacp_mux_sm_state_array[] = {
225 #define _(b, s, n) {.bit = b, .str = #s, },
226     foreach_lacp_mux_sm_state
227 #undef _
228     {.str = NULL}
229   };
230   int state = va_arg (*args, int);
231   lacp_state_struct *state_entry =
232     (lacp_state_struct *) & lacp_mux_sm_state_array;
233
234   if (state >= (sizeof (lacp_mux_sm_state_array) / sizeof (*state_entry)))
235     s = format (s, "Bad state %d", state);
236   else
237     s = format (s, "%s", state_entry[state].str);
238
239   return s;
240 }
241
242 static inline u8 *
243 format_ptx_sm_state (u8 * s, va_list * args)
244 {
245   lacp_state_struct lacp_ptx_sm_state_array[] = {
246 #define _(b, s, n) {.bit = b, .str = #s, },
247     foreach_lacp_ptx_sm_state
248 #undef _
249     {.str = NULL}
250   };
251   int state = va_arg (*args, int);
252   lacp_state_struct *state_entry =
253     (lacp_state_struct *) & lacp_ptx_sm_state_array;
254
255   if (state >= (sizeof (lacp_ptx_sm_state_array) / sizeof (*state_entry)))
256     s = format (s, "Bad state %d", state);
257   else
258     s = format (s, "%s", state_entry[state].str);
259
260   return s;
261 }
262
263 static inline int
264 lacp_bit_test (u8 val, u8 bit)
265 {
266   if (val & (1 << bit))
267     return 1;
268   else
269     return 0;
270 }
271
272 #endif /* __included_lacp_node_h__ */
273
274 /*
275  * fd.io coding-style-patch-verification: ON
276  *
277  * Local Variables:
278  * eval: (c-set-style "gnu")
279  * End:
280  */