FD.io VPP  v20.05.1-6-gf53edbc3b
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 
178  if ((states & lacpdu->partner.port_info.state) !=
179  (states & sif->actor.state)
180  || memcmp (&sif->actor, &lacpdu->partner.port_info,
181  sizeof (sif->actor) - sizeof (sif->actor.state)))
182  {
183  sif->ntt = 1;
184  lacp_start_periodic_timer (vm, sif, 0);
185  }
186 }
187 
188 /*
189  * compare lacpdu partner info against sif->partner. Return 1 if they match, 0
190  * otherwise.
191  */
192 static u8
194 {
195  lacp_pdu_t *lacpdu = (lacp_pdu_t *) sif->last_rx_pkt;
196 
197  if ((!memcmp (&sif->partner, &lacpdu->actor.port_info,
198  sizeof (sif->partner) - sizeof (sif->partner.state)) &&
199  ((sif->actor.state & LACP_STATE_AGGREGATION) ==
200  (lacpdu->partner.port_info.state & LACP_STATE_AGGREGATION))) ||
201  ((lacpdu->actor.port_info.state & LACP_STATE_AGGREGATION) == 0))
202  return 1;
203 
204  return 0;
205 }
206 
207 static void
209 {
210  lacp_pdu_t *lacpdu = (lacp_pdu_t *) sif->last_rx_pkt;
211  u8 match;
212 
213  /* Transition PTX out of NO_PERIODIC if needed */
214  if (!(sif->partner.state & LACP_STATE_LACP_ACTIVITY) &&
215  (lacpdu->actor.port_info.state & LACP_STATE_LACP_ACTIVITY))
217  match = lacp_compare_partner (sif);
218  sif->partner = lacpdu->actor.port_info;
219  sif->actor.state &= ~LACP_STATE_DEFAULTED;
220  if (match && (lacpdu->actor.port_info.state & LACP_STATE_SYNCHRONIZATION))
221  sif->partner.state |= LACP_STATE_SYNCHRONIZATION;
222  else
223  sif->partner.state &= ~LACP_STATE_SYNCHRONIZATION;
224 }
225 
226 static void
228 {
229  sif->port_moved = val;
230 
231  if (sif->port_moved)
232  lacp_machine_dispatch (&lacp_rx_machine, vm, sif,
233  LACP_RX_EVENT_PORT_MOVED, &sif->rx_state);
234  else if (!sif->port_enabled)
235  lacp_machine_dispatch (&lacp_rx_machine, vm, sif,
236  LACP_RX_EVENT_PORT_DISABLED, &sif->rx_state);
237 }
238 
239 int
240 lacp_rx_action_initialize (void *p1, void *p2)
241 {
242  vlib_main_t *vm = p1;
243  slave_if_t *sif = p2;
244 
245  lacp_set_port_unselected (vm, sif);
246  lacp_record_default (sif);
247  sif->actor.state &= ~LACP_STATE_EXPIRED;
248  lacp_set_port_moved (vm, sif, 0);
249  /* UCT */
250  lacp_machine_dispatch (&lacp_rx_machine, vm, sif,
251  LACP_RX_EVENT_BEGIN, &sif->rx_state);
252 
253  return 0;
254 }
255 
256 int
257 lacp_rx_action_port_disabled (void *p1, void *p2)
258 {
259  vlib_main_t *vm = p1;
260  slave_if_t *sif = p2;
261 
262  sif->partner.state &= ~LACP_STATE_SYNCHRONIZATION;
263  if (sif->port_moved)
264  {
265  lacp_machine_dispatch (&lacp_rx_machine, vm, sif,
266  LACP_RX_EVENT_PORT_MOVED, &sif->rx_state);
267  }
268  if (sif->port_enabled)
269  {
270  if (sif->lacp_enabled)
271  lacp_machine_dispatch (&lacp_rx_machine, vm, sif,
272  LACP_RX_EVENT_LACP_ENABLED, &sif->rx_state);
273  else
274  lacp_machine_dispatch (&lacp_rx_machine, vm, sif,
275  LACP_RX_EVENT_LACP_DISABLED, &sif->rx_state);
276  }
277 
278  return 0;
279 }
280 
281 int
282 lacp_rx_action_expired (void *p1, void *p2)
283 {
284  vlib_main_t *vm = p1;
285  slave_if_t *sif = p2;
286  u8 timer_expired;
287 
288  sif->partner.state &= ~LACP_STATE_SYNCHRONIZATION;
289  sif->partner.state |= LACP_STATE_LACP_TIMEOUT;
293  timer_expired = 1;
294  else
295  timer_expired = 0;
297  sif->actor.state |= LACP_STATE_EXPIRED;
298  if (timer_expired)
299  lacp_machine_dispatch (&lacp_rx_machine, vm, sif,
300  LACP_RX_EVENT_TIMER_EXPIRED, &sif->rx_state);
301  if (sif->last_rx_pkt && vec_len (sif->last_rx_pkt))
302  lacp_machine_dispatch (&lacp_rx_machine, vm, sif,
303  LACP_RX_EVENT_PDU_RECEIVED, &sif->rx_state);
304 
305  return 0;
306 }
307 
308 int
309 lacp_rx_action_lacp_disabled (void *p1, void *p2)
310 {
311  vlib_main_t *vm = p1;
312  slave_if_t *sif = p2;
313 
314  lacp_set_port_unselected (vm, sif);
315  lacp_record_default (sif);
316  sif->partner.state &= ~LACP_STATE_AGGREGATION;
317  sif->actor.state &= ~LACP_STATE_EXPIRED;
318 
319  return 0;
320 }
321 
322 int
323 lacp_rx_action_defaulted (void *p1, void *p2)
324 {
325  vlib_main_t *vm = p1;
326  slave_if_t *sif = p2;
327 
330  lacp_record_default (sif);
331  sif->actor.state &= ~LACP_STATE_EXPIRED;
332  if (sif->last_rx_pkt && vec_len (sif->last_rx_pkt))
333  lacp_machine_dispatch (&lacp_rx_machine, vm, sif,
334  LACP_RX_EVENT_PDU_RECEIVED, &sif->rx_state);
335 
336  return 0;
337 }
338 
339 static int
341 {
342  bond_main_t *bm = &bond_main;
343  slave_if_t *sif2;
344  lacp_pdu_t *lacpdu = (lacp_pdu_t *) sif->last_rx_pkt;
345 
346  /* *INDENT-OFF* */
347  pool_foreach (sif2, bm->neighbors, {
348  {
349  if ((sif != sif2) && (sif2->rx_state == LACP_RX_STATE_PORT_DISABLED) &&
350  !memcmp (sif2->partner.system,
351  lacpdu->partner.port_info.system, 6) &&
352  (sif2->partner.port_number == lacpdu->partner.port_info.port_number))
353  return 1;
354  }
355  });
356  /* *INDENT-ON* */
357 
358  return 0;
359 }
360 
361 int
362 lacp_rx_action_current (void *p1, void *p2)
363 {
364  vlib_main_t *vm = p1;
365  slave_if_t *sif = p2;
366 
367  lacp_update_selected (vm, sif);
368  lacp_update_ntt (vm, sif);
369  lacp_record_pdu (vm, sif);
371  sif->actor.state &= ~LACP_STATE_EXPIRED;
372  if (lacp_port_is_moved (vm, sif))
373  lacp_set_port_moved (vm, sif, 1);
374  lacp_selection_logic (vm, sif);
375 
376  return 0;
377 }
378 
379 static u8 *
380 format_rx_event (u8 * s, va_list * args)
381 {
382  static lacp_event_struct lacp_rx_event_array[] = {
383 #define _(b, s, n) {.bit = b, .str = #s, },
385 #undef _
386  {.str = NULL}
387  };
388  int e = va_arg (*args, int);
389  lacp_event_struct *event_entry = lacp_rx_event_array;
390 
391  if (e >= (sizeof (lacp_rx_event_array) / sizeof (*event_entry)))
392  s = format (s, "Bad event %d", e);
393  else
394  s = format (s, "%s", event_entry[e].str);
395 
396  return s;
397 }
398 
399 void
400 lacp_rx_debug_func (slave_if_t * sif, int event, int state,
401  lacp_fsm_state_t * transition)
402 {
404  /* *INDENT-OFF* */
405  ELOG_TYPE_DECLARE (e) =
406  {
407  .format = "%s",
408  .format_args = "T4",
409  };
410  /* *INDENT-ON* */
411  struct
412  {
413  u32 event;
414  } *ed = 0;
415 
417  ed->event = elog_string (&vlib_global_main.elog_main, "%U-RX: %U, %U->%U%c",
419  sif->sw_if_index, format_rx_event, event,
421  transition->next_state, 0);
422 }
423 
424 void
426 {
427  lacp_machine_dispatch (&lacp_rx_machine, vm, sif, LACP_RX_EVENT_BEGIN,
428  &sif->rx_state);
429  lacp_machine_dispatch (&lacp_rx_machine, vm, sif,
430  LACP_RX_EVENT_LACP_ENABLED, &sif->rx_state);
431 }
432 
433 /*
434  * fd.io coding-style-patch-verification: ON
435  *
436  * Local Variables:
437  * eval: (c-set-style "gnu")
438  * End:
439  */
#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_global_main
Definition: main.c:1999
int selected
Definition: node.h:280
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
static void lacp_update_default_selected(vlib_main_t *vm, slave_if_t *sif)
Definition: rx_machine.c:139
void lacp_init_rx_machine(vlib_main_t *vm, slave_if_t *sif)
Definition: rx_machine.c:425
static void lacp_record_pdu(vlib_main_t *vm, slave_if_t *sif)
Definition: rx_machine.c:208
elog_track_t elog_track
Definition: threads.h:100
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
f64 current_while_timer
Definition: node.h:286
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:380
u8 ntt
Definition: node.h:262
#define LACP_ACTION_EXPIRED
Definition: rx_machine.h:69
f32 ttl_in_seconds
Definition: node.h:221
static u8 * format_rx_sm_state(u8 *s, va_list *args)
Definition: node.h:180
static u8 lacp_timer_is_running(f64 timer)
Definition: node.h:166
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:513
int lacp_rx_action_current(void *p1, void *p2)
Definition: rx_machine.c:362
lacp_port_info_t actor
Definition: node.h:255
int lacp_rx_action_expired(void *p1, void *p2)
Definition: rx_machine.c:282
static u8 lacp_timer_is_expired(vlib_main_t *vm, f64 timer)
Definition: node.h:172
static void lacp_set_port_moved(vlib_main_t *vm, slave_if_t *sif, u8 val)
Definition: rx_machine.c:227
unsigned int u32
Definition: types.h:88
#define LACP_NOACTION
Definition: machine.h:21
u8 * last_rx_pkt
Definition: node.h:239
vlib_worker_thread_t * vlib_worker_threads
Definition: threads.c:34
static void lacp_stop_timer(f64 *timer)
Definition: node.h:160
u8 port_moved
Definition: node.h:283
#define LACP_ACTION_INITIALIZE
Definition: rx_machine.h:65
vlib_main_t * vm
Definition: in2out_ed.c:1599
int lacp_rx_action_defaulted(void *p1, void *p2)
Definition: rx_machine.c:323
#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:400
u32 sw_if_index
Definition: node.h:218
lacp_port_info_t partner
Definition: node.h:254
lacp_machine_t lacp_mux_machine
Definition: mux_machine.c:77
int lacp_rx_action_initialize(void *p1, void *p2)
Definition: rx_machine.c:240
int lacp_rx_action_lacp_disabled(void *p1, void *p2)
Definition: rx_machine.c:309
elog_main_t elog_main
Definition: main.h:193
int rx_state
Definition: node.h:313
#define ELOG_TYPE_DECLARE(f)
Definition: elog.h:442
#define LACP_ACTION_PORT_DISABLED
Definition: rx_machine.h:67
#define ELOG_TRACK_DATA(em, f, track)
Definition: elog.h:478
bond_main_t bond_main
Definition: node.c:25
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:193
void lacp_selection_logic(vlib_main_t *vm, slave_if_t *sif)
Definition: selection.c:56
u8 lacp_enabled
Definition: node.h:271
u32 elog_string(elog_main_t *em, char *fmt,...)
add a string to the event-log string table
Definition: elog.c:562
#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:255
static_always_inline uword os_get_thread_index(void)
Definition: os.h:63
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:257
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:370
vl_api_dhcp_client_state_t state
Definition: dhcp.api:201
u8 port_enabled
Definition: node.h:265
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:315
static int lacp_port_is_moved(vlib_main_t *vm, slave_if_t *sif)
Definition: rx_machine.c:340
int lacp_machine_dispatch(lacp_machine_t *machine, vlib_main_t *vm, slave_if_t *sif, int event, int *state)
Definition: lacp.c:317