FD.io VPP  v19.04.2-12-g66b1689
Vector Packet Processing
rx_machine.c
Go to the documentation of this file.
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 = INITIALIZE
23  */
24 static lacp_fsm_state_t lacp_rx_state_initialize[] = {
25  {LACP_ACTION_INITIALIZE, LACP_RX_STATE_PORT_DISABLED}, // event 0 BEGIN
26  {LACP_ACTION_INITIALIZE, LACP_RX_STATE_PORT_DISABLED}, // event 1 PORT_DISABLED
27  {LACP_ACTION_INITIALIZE, LACP_RX_STATE_PORT_DISABLED}, // event 2 PORT_MOVED
28  {LACP_NOACTION, LACP_RX_STATE_INITIALIZE}, // event 3 LACP_ENABLED
29  {LACP_NOACTION, LACP_RX_STATE_INITIALIZE}, // event 4 LACP_DISABLED
30  {LACP_NOACTION, LACP_RX_STATE_INITIALIZE}, // event 5 PDU_RECEIVED
31  {LACP_NOACTION, LACP_RX_STATE_INITIALIZE}, // event 6 TIMER_EXPIRED
32 };
33 
34 /*
35  * LACP State = PORT_DISABLED
36  */
37 static lacp_fsm_state_t lacp_rx_state_port_disabled[] = {
38  {LACP_ACTION_PORT_DISABLED, LACP_RX_STATE_PORT_DISABLED}, // event 0 BEGIN
39  {LACP_ACTION_PORT_DISABLED, LACP_RX_STATE_PORT_DISABLED}, // event 1 PORT_DISABLED
40  {LACP_ACTION_INITIALIZE, LACP_RX_STATE_INITIALIZE}, // event 2 PORT_MOVED
41  {LACP_ACTION_EXPIRED, LACP_RX_STATE_EXPIRED}, // event 3 LACP_ENABLED
42  {LACP_ACTION_LACP_DISABLED, LACP_RX_STATE_LACP_DISABLED}, // event 4 LACP_DISABLED
43  {LACP_NOACTION, LACP_RX_STATE_PORT_DISABLED}, // event 5 PDU_RECEIVED
44  {LACP_NOACTION, LACP_RX_STATE_PORT_DISABLED}, // event 6 TIMER_EXPIRED
45 };
46 
47 /*
48  * LACP State = EXPIRED
49  */
50 static lacp_fsm_state_t lacp_rx_state_expired[] = {
51  {LACP_ACTION_INITIALIZE, LACP_RX_STATE_INITIALIZE}, // event 0 BEGIN
52  {LACP_NOACTION, LACP_RX_STATE_EXPIRED}, // event 1 PORT_DISABLED
53  {LACP_NOACTION, LACP_RX_STATE_EXPIRED}, // event 2 PORT_MOVED
54  {LACP_NOACTION, LACP_RX_STATE_EXPIRED}, // event 3 LACP_ENABLED
55  {LACP_NOACTION, LACP_RX_STATE_EXPIRED}, // event 4 LACP_DISABLED
56  {LACP_ACTION_CURRENT, LACP_RX_STATE_CURRENT}, // event 5 PDU_RECEIVED
57  {LACP_ACTION_DEFAULTED, LACP_RX_STATE_DEFAULTED}, // event 6 TIMER_EXPIRED
58 };
59 
60 /*
61  * LACP State = LACP_DISABLED
62  */
63 static lacp_fsm_state_t lacp_rx_state_lacp_disabled[] = {
64  {LACP_ACTION_INITIALIZE, LACP_RX_STATE_INITIALIZE}, // event 0 BEGIN
65  {LACP_NOACTION, LACP_RX_STATE_LACP_DISABLED}, // event 1 PORT_DISABLED
66  {LACP_NOACTION, LACP_RX_STATE_LACP_DISABLED}, // event 2 PORT_MOVED
67  {LACP_ACTION_EXPIRED, LACP_RX_STATE_EXPIRED}, // event 3 LACP_ENABLED XXX
68  {LACP_ACTION_LACP_DISABLED, LACP_RX_STATE_LACP_DISABLED}, // event 4 LACP_DISABLED
69  {LACP_NOACTION, LACP_RX_STATE_LACP_DISABLED}, // event 5 PDU_RECEIVED
70  {LACP_NOACTION, LACP_RX_STATE_LACP_DISABLED}, // event 6 TIMER_EXPIRED
71 };
72 
73 /*
74  * LACP State = DEFAULTED
75  */
76 static lacp_fsm_state_t lacp_rx_state_defaulted[] = {
77  {LACP_ACTION_INITIALIZE, LACP_RX_STATE_INITIALIZE}, // event 0 BEGIN
78  {LACP_NOACTION, LACP_RX_STATE_DEFAULTED}, // event 1 PORT_DISABLED
79  {LACP_NOACTION, LACP_RX_STATE_DEFAULTED}, // event 2 PORT_MOVED
80  {LACP_NOACTION, LACP_RX_STATE_DEFAULTED}, // event 3 LACP_ENABLED
81  {LACP_ACTION_LACP_DISABLED, LACP_RX_STATE_LACP_DISABLED}, // event 4 LACP_DISABLED
82  {LACP_ACTION_CURRENT, LACP_RX_STATE_CURRENT}, // event 5 PDU_RECEIVED
83  {LACP_ACTION_DEFAULTED, LACP_RX_STATE_DEFAULTED}, // event 6 TIMER_EXPIRED
84 };
85 
86 /*
87  * LACP State = CURRENT
88  */
89 static lacp_fsm_state_t lacp_rx_state_current[] = {
90  {LACP_ACTION_INITIALIZE, LACP_RX_STATE_INITIALIZE}, // event 0 BEGIN
91  {LACP_NOACTION, LACP_RX_STATE_CURRENT}, // event 1 PORT_DISABLED
92  {LACP_NOACTION, LACP_RX_STATE_CURRENT}, // event 1 PORT_MOVED
93  {LACP_NOACTION, LACP_RX_STATE_CURRENT}, // event 2 LACP_ENABLED
94  {LACP_ACTION_LACP_DISABLED, LACP_RX_STATE_LACP_DISABLED}, // event 3 LACP_DISABLED
95  {LACP_ACTION_CURRENT, LACP_RX_STATE_CURRENT}, // event 4 PDU_RECEIVED
96  {LACP_ACTION_EXPIRED, LACP_RX_STATE_EXPIRED}, // event 5 TIMER_EXPIRED
97 };
98 
99 static lacp_fsm_machine_t lacp_rx_fsm_table[] = {
100  {lacp_rx_state_initialize},
101  {lacp_rx_state_port_disabled},
102  {lacp_rx_state_expired},
103  {lacp_rx_state_lacp_disabled},
104  {lacp_rx_state_defaulted},
105  {lacp_rx_state_current},
106 };
107 
108 lacp_machine_t lacp_rx_machine = {
111 };
112 
113 static void
115 {
116  sif->selected = LACP_PORT_UNSELECTED;
117 
118  switch (sif->mux_state)
119  {
120  case LACP_MUX_STATE_DETACHED:
121  break;
122  case LACP_MUX_STATE_WAITING:
123  break;
124  case LACP_MUX_STATE_ATTACHED:
125  return;
126  break;
127  case LACP_MUX_STATE_COLLECTING_DISTRIBUTING:
128  if (sif->partner.state & LACP_STATE_SYNCHRONIZATION)
129  return;
130  break;
131  default:
132  break;
133  }
135  LACP_MUX_EVENT_UNSELECTED, &sif->mux_state);
136 }
137 
138 static void
140 {
141  if ((sif->partner_admin.state & LACP_STATE_AGGREGATION) !=
142  (sif->partner.state & LACP_STATE_AGGREGATION) ||
143  memcmp (&sif->partner, &sif->partner_admin,
144  sizeof (sif->partner) - sizeof (sif->partner.state)))
145  {
146  lacp_set_port_unselected (vm, sif);
147  }
148 }
149 
150 static void
152 {
153  sif->partner = sif->partner_admin;
154  sif->actor.state |= LACP_STATE_DEFAULTED;
155 }
156 
157 static void
159 {
160  lacp_pdu_t *lacpdu = (lacp_pdu_t *) sif->last_rx_pkt;
161 
162  if ((lacpdu->actor.port_info.state & LACP_STATE_AGGREGATION) !=
163  (sif->partner.state & LACP_STATE_AGGREGATION) ||
164  memcmp (&sif->partner, &lacpdu->actor.port_info,
165  sizeof (sif->partner) - sizeof (sif->partner.state)))
166  {
167  lacp_set_port_unselected (vm, sif);
168  }
169 }
170 
171 static void
173 {
174  lacp_pdu_t *lacpdu = (lacp_pdu_t *) sif->last_rx_pkt;
175  u8 states = LACP_STATE_LACP_ACTIVITY | LACP_STATE_LACP_TIMEOUT |
176  LACP_STATE_SYNCHRONIZATION | LACP_STATE_AGGREGATION;
177  lacp_main_t *lm = &lacp_main;
178 
179  if ((states & lacpdu->partner.port_info.state) !=
180  (states & sif->actor.state)
181  || memcmp (&sif->actor, &lacpdu->partner.port_info,
182  sizeof (sif->actor) - sizeof (sif->actor.state)))
183  {
184  sif->ntt = 1;
185  lacp_start_periodic_timer (lm->vlib_main, sif, 0);
186  }
187 }
188 
189 /*
190  * compare lacpdu partner info against sif->partner. Return 1 if they match, 0
191  * otherwise.
192  */
193 static u8
195 {
196  lacp_pdu_t *lacpdu = (lacp_pdu_t *) sif->last_rx_pkt;
197 
198  if ((!memcmp (&sif->partner, &lacpdu->actor.port_info,
199  sizeof (sif->partner) - sizeof (sif->partner.state)) &&
200  ((sif->actor.state & LACP_STATE_AGGREGATION) ==
201  (lacpdu->partner.port_info.state & LACP_STATE_AGGREGATION))) ||
202  ((lacpdu->actor.port_info.state & LACP_STATE_AGGREGATION) == 0))
203  return 1;
204 
205  return 0;
206 }
207 
208 static void
210 {
211  lacp_pdu_t *lacpdu = (lacp_pdu_t *) sif->last_rx_pkt;
212  u8 match;
213 
214  /* Transition PTX out of NO_PERIODIC if needed */
215  if (!(sif->partner.state & LACP_STATE_LACP_ACTIVITY) &&
216  (lacpdu->actor.port_info.state & LACP_STATE_LACP_ACTIVITY))
218  match = lacp_compare_partner (sif);
219  sif->partner = lacpdu->actor.port_info;
220  sif->actor.state &= ~LACP_STATE_DEFAULTED;
221  if (match && (lacpdu->actor.port_info.state & LACP_STATE_SYNCHRONIZATION))
222  sif->partner.state |= LACP_STATE_SYNCHRONIZATION;
223  else
224  sif->partner.state &= ~LACP_STATE_SYNCHRONIZATION;
225 }
226 
227 static void
229 {
230  sif->port_moved = val;
231 
232  if (sif->port_moved)
233  lacp_machine_dispatch (&lacp_rx_machine, vm, sif,
234  LACP_RX_EVENT_PORT_MOVED, &sif->rx_state);
235  else if (!sif->port_enabled)
236  lacp_machine_dispatch (&lacp_rx_machine, vm, sif,
237  LACP_RX_EVENT_PORT_DISABLED, &sif->rx_state);
238 }
239 
240 int
241 lacp_rx_action_initialize (void *p1, void *p2)
242 {
243  vlib_main_t *vm = (vlib_main_t *) p1;
244  slave_if_t *sif = (slave_if_t *) p2;
245 
246  lacp_set_port_unselected (vm, sif);
247  lacp_record_default (sif);
248  sif->actor.state &= ~LACP_STATE_EXPIRED;
249  lacp_set_port_moved (vm, sif, 0);
250  /* UCT */
251  lacp_machine_dispatch (&lacp_rx_machine, vm, sif,
252  LACP_RX_EVENT_BEGIN, &sif->rx_state);
253 
254  return 0;
255 }
256 
257 int
258 lacp_rx_action_port_disabled (void *p1, void *p2)
259 {
260  vlib_main_t *vm = (vlib_main_t *) p1;
261  slave_if_t *sif = (slave_if_t *) p2;
262 
263  sif->partner.state &= ~LACP_STATE_SYNCHRONIZATION;
264  if (sif->port_moved)
265  {
266  lacp_machine_dispatch (&lacp_rx_machine, vm, sif,
267  LACP_RX_EVENT_PORT_MOVED, &sif->rx_state);
268  }
269  if (sif->port_enabled)
270  {
271  if (sif->lacp_enabled)
272  lacp_machine_dispatch (&lacp_rx_machine, vm, sif,
273  LACP_RX_EVENT_LACP_ENABLED, &sif->rx_state);
274  else
275  lacp_machine_dispatch (&lacp_rx_machine, vm, sif,
276  LACP_RX_EVENT_LACP_DISABLED, &sif->rx_state);
277  }
278 
279  return 0;
280 }
281 
282 int
283 lacp_rx_action_expired (void *p1, void *p2)
284 {
285  vlib_main_t *vm = (vlib_main_t *) p1;
286  slave_if_t *sif = (slave_if_t *) p2;
287  u8 timer_expired;
288  lacp_main_t *lm = &lacp_main;
289 
290  sif->partner.state &= ~LACP_STATE_SYNCHRONIZATION;
291  sif->partner.state |= LACP_STATE_LACP_TIMEOUT;
295  timer_expired = 1;
296  else
297  timer_expired = 0;
299  sif->actor.state |= LACP_STATE_EXPIRED;
300  if (timer_expired)
301  lacp_machine_dispatch (&lacp_rx_machine, vm, sif,
302  LACP_RX_EVENT_TIMER_EXPIRED, &sif->rx_state);
303  if (sif->last_rx_pkt && vec_len (sif->last_rx_pkt))
304  lacp_machine_dispatch (&lacp_rx_machine, vm, sif,
305  LACP_RX_EVENT_PDU_RECEIVED, &sif->rx_state);
306 
307  return 0;
308 }
309 
310 int
311 lacp_rx_action_lacp_disabled (void *p1, void *p2)
312 {
313  vlib_main_t *vm = (vlib_main_t *) p1;
314  slave_if_t *sif = (slave_if_t *) p2;
315 
316  lacp_set_port_unselected (vm, sif);
317  lacp_record_default (sif);
318  sif->partner.state &= ~LACP_STATE_AGGREGATION;
319  sif->actor.state &= ~LACP_STATE_EXPIRED;
320 
321  return 0;
322 }
323 
324 int
325 lacp_rx_action_defaulted (void *p1, void *p2)
326 {
327  vlib_main_t *vm = (vlib_main_t *) p1;
328  slave_if_t *sif = (slave_if_t *) p2;
329 
332  lacp_record_default (sif);
333  sif->actor.state &= ~LACP_STATE_EXPIRED;
334  if (sif->last_rx_pkt && vec_len (sif->last_rx_pkt))
335  lacp_machine_dispatch (&lacp_rx_machine, vm, sif,
336  LACP_RX_EVENT_PDU_RECEIVED, &sif->rx_state);
337 
338  return 0;
339 }
340 
341 static int
343 {
344  bond_main_t *bm = &bond_main;
345  slave_if_t *sif2;
346  lacp_pdu_t *lacpdu = (lacp_pdu_t *) sif->last_rx_pkt;
347 
348  /* *INDENT-OFF* */
349  pool_foreach (sif2, bm->neighbors, {
350  {
351  if ((sif != sif2) && (sif2->rx_state == LACP_RX_STATE_PORT_DISABLED) &&
352  !memcmp (sif2->partner.system,
353  lacpdu->partner.port_info.system, 6) &&
354  (sif2->partner.port_number == lacpdu->partner.port_info.port_number))
355  return 1;
356  }
357  });
358  /* *INDENT-ON* */
359 
360  return 0;
361 }
362 
363 int
364 lacp_rx_action_current (void *p1, void *p2)
365 {
366  vlib_main_t *vm = (vlib_main_t *) p1;
367  slave_if_t *sif = (slave_if_t *) p2;
368  lacp_main_t *lm = &lacp_main;
369 
370  lacp_update_selected (vm, sif);
371  lacp_update_ntt (vm, sif);
372  lacp_record_pdu (vm, sif);
374  sif->actor.state &= ~LACP_STATE_EXPIRED;
375  if (lacp_port_is_moved (vm, sif))
376  lacp_set_port_moved (vm, sif, 1);
377  lacp_selection_logic (vm, sif);
378 
379  return 0;
380 }
381 
382 static u8 *
383 format_rx_event (u8 * s, va_list * args)
384 {
385  static lacp_event_struct lacp_rx_event_array[] = {
386 #define _(b, s, n) {.bit = b, .str = #s, },
388 #undef _
389  {.str = NULL}
390  };
391  int e = va_arg (*args, int);
392  lacp_event_struct *event_entry =
393  (lacp_event_struct *) & lacp_rx_event_array;
394 
395  if (e >= (sizeof (lacp_rx_event_array) / sizeof (*event_entry)))
396  s = format (s, "Bad event %d", e);
397  else
398  s = format (s, "%s", event_entry[e].str);
399 
400  return s;
401 }
402 
403 void
404 lacp_rx_debug_func (slave_if_t * sif, int event, int state,
405  lacp_fsm_state_t * transition)
406 {
407  clib_warning ("%U-RX: event %U, old state %U, new state %U",
410  event, format_rx_sm_state, state, format_rx_sm_state,
411  transition->next_state);
412 }
413 
414 void
416 {
417  lacp_machine_dispatch (&lacp_rx_machine, vm, sif, LACP_RX_EVENT_BEGIN,
418  &sif->rx_state);
419  lacp_machine_dispatch (&lacp_rx_machine, vm, sif,
420  LACP_RX_EVENT_LACP_ENABLED, &sif->rx_state);
421 }
422 
423 /*
424  * fd.io coding-style-patch-verification: ON
425  *
426  * Local Variables:
427  * eval: (c-set-style "gnu")
428  * End:
429  */
#define LACP_ACTION_LACP_DISABLED
Definition: rx_machine.h:71
static lacp_fsm_machine_t lacp_rx_fsm_table[]
Definition: rx_machine.c:99
vlib_main_t * vlib_main
Definition: node.h:119
int selected
Definition: node.h:257
vnet_main_t * vnet_get_main(void)
Definition: misc.c:47
static void lacp_update_default_selected(vlib_main_t *vm, slave_if_t *sif)
Definition: rx_machine.c:139
#define NULL
Definition: clib.h:58
void lacp_init_rx_machine(vlib_main_t *vm, slave_if_t *sif)
Definition: rx_machine.c:415
static void lacp_record_pdu(vlib_main_t *vm, slave_if_t *sif)
Definition: rx_machine.c:209
bond_main_t bond_main
Definition: node.c:25
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
f64 current_while_timer
Definition: node.h:263
format_function_t format_vnet_sw_if_index_name
unsigned char u8
Definition: types.h:56
static u8 * format_rx_event(u8 *s, va_list *args)
Definition: rx_machine.c:383
u8 ntt
Definition: node.h:239
#define LACP_ACTION_EXPIRED
Definition: rx_machine.h:69
f32 ttl_in_seconds
Definition: node.h:201
static u8 * format_rx_sm_state(u8 *s, va_list *args)
Definition: node.h:177
static u8 lacp_timer_is_running(f64 timer)
Definition: node.h:163
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:493
int lacp_rx_action_current(void *p1, void *p2)
Definition: rx_machine.c:364
lacp_port_info_t actor
Definition: node.h:232
int lacp_rx_action_expired(void *p1, void *p2)
Definition: rx_machine.c:283
static u8 lacp_timer_is_expired(vlib_main_t *vm, f64 timer)
Definition: node.h:169
static void lacp_set_port_moved(vlib_main_t *vm, slave_if_t *sif, u8 val)
Definition: rx_machine.c:228
vhost_vring_state_t state
Definition: vhost_user.h:120
#define LACP_NOACTION
Definition: machine.h:21
u8 * last_rx_pkt
Definition: node.h:219
static void lacp_stop_timer(f64 *timer)
Definition: node.h:157
u8 port_moved
Definition: node.h:260
#define LACP_ACTION_INITIALIZE
Definition: rx_machine.h:65
int lacp_rx_action_defaulted(void *p1, void *p2)
Definition: rx_machine.c:325
#define LACP_ACTION_CURRENT
Definition: rx_machine.h:74
void lacp_rx_debug_func(slave_if_t *sif, int event, int state, lacp_fsm_state_t *transition)
Definition: rx_machine.c:404
u32 sw_if_index
Definition: node.h:198
vlib_main_t * vm
Definition: buffer.c:312
lacp_port_info_t partner
Definition: node.h:231
#define clib_warning(format, args...)
Definition: error.h:59
lacp_machine_t lacp_mux_machine
Definition: mux_machine.c:77
int lacp_rx_action_initialize(void *p1, void *p2)
Definition: rx_machine.c:241
int lacp_rx_action_lacp_disabled(void *p1, void *p2)
Definition: rx_machine.c:311
int rx_state
Definition: node.h:290
lacp_main_t lacp_main
Definition: lacp.c:25
#define LACP_ACTION_PORT_DISABLED
Definition: rx_machine.h:67
static void lacp_update_selected(vlib_main_t *vm, slave_if_t *sif)
Definition: rx_machine.c:158
static u8 lacp_compare_partner(slave_if_t *sif)
Definition: rx_machine.c:194
void lacp_selection_logic(vlib_main_t *vm, slave_if_t *sif)
Definition: selection.c:56
u8 lacp_enabled
Definition: node.h:248
#define LACP_ACTION_DEFAULTED
Definition: rx_machine.h:73
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
static void lacp_update_ntt(vlib_main_t *vm, slave_if_t *sif)
Definition: rx_machine.c:172
lacp_port_info_t partner_admin
Definition: node.h:232
static void lacp_start_periodic_timer(vlib_main_t *vm, slave_if_t *sif, u8 expiration)
Definition: ptx_machine.h:67
int lacp_rx_action_port_disabled(void *p1, void *p2)
Definition: rx_machine.c:258
static void lacp_start_current_while_timer(vlib_main_t *vm, slave_if_t *sif, u8 expiration)
Definition: rx_machine.h:77
slave_if_t * neighbors
Definition: node.h:338
u8 port_enabled
Definition: node.h:242
static void lacp_set_port_unselected(vlib_main_t *vm, slave_if_t *sif)
Definition: rx_machine.c:114
static void lacp_record_default(slave_if_t *sif)
Definition: rx_machine.c:151
static void lacp_ptx_post_short_timeout_event(vlib_main_t *vm, slave_if_t *sif)
Definition: ptx_machine.h:90
int mux_state
Definition: node.h:292
static int lacp_port_is_moved(vlib_main_t *vm, slave_if_t *sif)
Definition: rx_machine.c:342
int lacp_machine_dispatch(lacp_machine_t *machine, vlib_main_t *vm, slave_if_t *sif, int event, int *state)
Definition: lacp.c:287