FD.io VPP  v19.08.3-2-gbabecb413
Vector Packet Processing
lacp.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 #include <stdint.h>
17 #include <vlib/vlib.h>
18 #include <vlib/unix/unix.h>
19 #include <vnet/plugin/plugin.h>
20 #include <vpp/app/version.h>
21 #include <vppinfra/hash.h>
22 #include <vnet/bonding/node.h>
23 #include <lacp/node.h>
24 
26 
27 /*
28  * Generate lacp pdu
29  */
30 static void
31 lacp_fill_pdu (lacp_pdu_t * lacpdu, slave_if_t * sif)
32 {
33  /* Actor TLV */
34  lacpdu->actor.port_info = sif->actor;
35 
36  /* Partner TLV */
37  lacpdu->partner.port_info = sif->partner;
38 }
39 
40 /*
41  * send a lacp pkt on an ethernet interface
42  */
43 static void
45 {
46  lacp_main_t *lm = &lacp_main;
47  u32 *to_next;
48  ethernet_lacp_pdu_t *h0;
50  u32 bi0;
51  vlib_buffer_t *b0;
52  vlib_frame_t *f;
53  vlib_main_t *vm = lm->vlib_main;
54  vnet_main_t *vnm = lm->vnet_main;
55 
56  /*
57  * see lacp_periodic_init() to understand what's already painted
58  * into the buffer by the packet template mechanism
59  */
61  (vm, &lm->packet_templates[sif->packet_template_index], &bi0);
62 
63  if (!h0)
64  return;
65 
66  /* Add the interface's ethernet source address */
67  hw = vnet_get_sup_hw_interface (vnm, sif->sw_if_index);
68 
69  clib_memcpy (h0->ethernet.src_address, hw->hw_address,
70  vec_len (hw->hw_address));
71 
72  lacp_fill_pdu (&h0->lacp, sif);
73 
74  /* Set the outbound packet length */
75  b0 = vlib_get_buffer (vm, bi0);
76  b0->current_length = sizeof (ethernet_lacp_pdu_t);
77  b0->current_data = 0;
79 
80  /* And the outbound interface */
81  vnet_buffer (b0)->sw_if_index[VLIB_TX] = hw->sw_if_index;
82 
83  /* And output the packet on the correct interface */
85 
86  to_next = vlib_frame_vector_args (f);
87  to_next[0] = bi0;
88  f->n_vectors = 1;
89 
91 
93  sif->pdu_sent++;
94 }
95 
96 /*
97  * Decide which lacp packet template to use
98  */
99 static int
101 {
103 
104  return 0;
105 }
106 
107 void
109 {
110  if ((sif->mode != BOND_MODE_LACP) || (sif->port_enabled == 0))
111  {
113  return;
114  }
115 
116  if (sif->packet_template_index == (u8) ~ 0)
117  {
118  /* If we don't know how to talk to this peer, don't try again */
119  if (lacp_pick_packet_template (sif))
120  {
122  return;
123  }
124  }
125 
126  switch (sif->packet_template_index)
127  {
130  break;
131 
132  default:
133  ASSERT (0);
134  }
135 }
136 
137 void
139 {
140  bond_main_t *bm = &bond_main;
141  lacp_main_t *lm = &lacp_main;
142  slave_if_t *sif;
143 
144  /* *INDENT-OFF* */
145  pool_foreach (sif, bm->neighbors,
146  ({
147  if (sif->port_enabled == 0)
148  continue;
149 
150  if (lacp_timer_is_running (sif->current_while_timer) &&
151  lacp_timer_is_expired (lm->vlib_main, sif->current_while_timer))
152  {
153  lacp_machine_dispatch (&lacp_rx_machine, vm, sif,
154  LACP_RX_EVENT_TIMER_EXPIRED, &sif->rx_state);
155  }
156 
159  {
160  lacp_machine_dispatch (&lacp_ptx_machine, vm, sif,
161  LACP_PTX_EVENT_TIMER_EXPIRED, &sif->ptx_state);
162  }
165  {
166  sif->ready_n = 1;
167  lacp_stop_timer (&sif->wait_while_timer);
168  lacp_selection_logic (vm, sif);
169  }
170  }));
171  /* *INDENT-ON* */
172 }
173 
174 static void
176  slave_if_t * sif, u8 enable)
177 {
178  lacp_main_t *lm = &lacp_main;
179  uword port_number;
180 
181  if (enable)
182  {
184  port_number = clib_bitmap_first_clear (bif->port_number_bitmap);
186  port_number, 1);
187  // bitmap starts at 0. Our port number starts at 1.
188  lacp_init_neighbor (sif, bif->hw_address, port_number + 1, sif->group);
189  lacp_init_state_machines (vm, sif);
190  lm->lacp_int++;
191  if (lm->lacp_int == 1)
192  {
195  }
196  }
197  else
198  {
199  ASSERT (lm->lacp_int >= 1);
200  if (lm->lacp_int == 0)
201  {
202  /* *INDENT-OFF* */
203  ELOG_TYPE_DECLARE (e) =
204  {
205  .format = "lacp-int-en-dis: BUG lacp_int == 0",
206  };
207  /* *INDENT-ON* */
209  }
210  else
211  {
212  lm->lacp_int--;
213  if (lm->lacp_int == 0)
216  }
217  }
218 }
219 
220 static clib_error_t *
222 {
223  lacp_main_t *lm = &lacp_main;
224  ethernet_lacp_pdu_t h;
225  ethernet_marker_pdu_t m;
226  u8 dst[] = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 };
227 
228  /* initialize binary API */
230 
231  /* Create the ethernet lacp packet template */
232 
233  clib_memset (&h, 0, sizeof (h));
234 
235  memcpy (h.ethernet.dst_address, dst, sizeof (h.ethernet.dst_address));
236 
237  /* leave src address blank (fill in at send time) */
238 
239  h.ethernet.type = htons (ETHERNET_TYPE_SLOW_PROTOCOLS);
240 
241  h.lacp.subtype = LACP_SUBTYPE;
242  h.lacp.version_number = LACP_ACTOR_LACP_VERSION;
243 
244  /* Actor TLV */
245  h.lacp.actor.tlv_type = LACP_ACTOR_INFORMATION;
246  h.lacp.actor.tlv_length = sizeof (lacp_actor_partner_t);
247 
248  /* Partner TLV */
249  h.lacp.partner.tlv_type = LACP_PARTNER_INFORMATION;
250  h.lacp.partner.tlv_length = sizeof (lacp_actor_partner_t);
251 
252  /* Collector TLV */
253  h.lacp.collector.tlv_type = LACP_COLLECTOR_INFORMATION;
254  h.lacp.collector.tlv_length = sizeof (lacp_collector_t);
255  h.lacp.collector.max_delay = 0;
256 
257  /* Terminator TLV */
258  h.lacp.terminator.tlv_type = LACP_TERMINATOR_INFORMATION;
259  h.lacp.terminator.tlv_length = 0;
260 
263  /* data */ &h,
264  sizeof (h),
265  /* alloc chunk size */ 8,
266  "lacp-ethernet");
267 
268  /* Create the ethernet marker protocol packet template */
269 
270  clib_memset (&m, 0, sizeof (m));
271 
272  memcpy (m.ethernet.dst_address, dst, sizeof (m.ethernet.dst_address));
273 
274  /* leave src address blank (fill in at send time) */
275 
276  m.ethernet.type = htons (ETHERNET_TYPE_SLOW_PROTOCOLS);
277 
278  m.marker.subtype = MARKER_SUBTYPE;
279  m.marker.version_number = MARKER_PROTOCOL_VERSION;
280 
281  m.marker.marker_info.tlv_length = sizeof (marker_information_t);
282 
283  /* Terminator TLV */
284  m.marker.terminator.tlv_type = MARKER_TERMINATOR_INFORMATION;
285  m.marker.terminator.tlv_length = 0;
286 
289  /* data */ &m,
290  sizeof (m),
291  /* alloc chunk size */ 8,
292  "marker-ethernet");
293 
295 
296  return 0;
297 }
298 
299 int
301  slave_if_t * sif, int event, int *state)
302 {
303  lacp_fsm_state_t *transition;
304  int rc = 0;
305 
306  transition = &machine->tables[*state].state_table[event];
307  LACP_DBG2 (sif, event, *state, machine, transition);
308  *state = transition->next_state;
309  if (transition->action)
310  rc = (*transition->action) ((void *) vm, (void *) sif);
311 
312  return rc;
313 }
314 
315 void
316 lacp_init_neighbor (slave_if_t * sif, u8 * hw_address, u16 port_number,
317  u32 group)
318 {
328  sif->lacp_enabled = 1;
329  sif->loopback_port = 0;
330  sif->ready = 0;
331  sif->ready_n = 0;
332  sif->port_moved = 0;
333  sif->ntt = 0;
334  sif->selected = LACP_PORT_UNSELECTED;
335  sif->actor.state = LACP_STATE_AGGREGATION;
337  sif->actor.state |= LACP_STATE_LACP_TIMEOUT;
338  if (sif->is_passive == 0)
339  sif->actor.state |= LACP_STATE_LACP_ACTIVITY;
340  clib_memcpy (sif->actor.system, hw_address, 6);
341  sif->actor.system_priority = htons (LACP_DEFAULT_SYSTEM_PRIORITY);
342  sif->actor.key = htons (group);
343  sif->actor.port_number = htons (port_number);
344  sif->actor.port_priority = htons (LACP_DEFAULT_PORT_PRIORITY);
345 
346  sif->partner.system_priority = htons (LACP_DEFAULT_SYSTEM_PRIORITY);
347  sif->partner.key = htons (group);
348  sif->partner.port_number = htons (port_number);
349  sif->partner.port_priority = htons (LACP_DEFAULT_PORT_PRIORITY);
350  sif->partner.state = 0;
351 
352  sif->actor_admin = sif->actor;
353  sif->partner_admin = sif->partner;
354 }
355 
356 void
358 {
359  lacp_init_tx_machine (vm, sif);
360  lacp_init_mux_machine (vm, sif);
361  lacp_init_ptx_machine (vm, sif);
362  lacp_init_rx_machine (vm, sif);
363 }
364 
366 
367 static clib_error_t *
369 {
370  lacp_main_t *lm = &lacp_main;
371  slave_if_t *sif;
372  vlib_main_t *vm = lm->vlib_main;
373 
374  sif = bond_get_slave_by_sw_if_index (sw_if_index);
375  if (sif)
376  {
377  if (sif->lacp_enabled == 0)
378  return 0;
379 
380  /* port_enabled is both admin up and hw link up */
381  sif->port_enabled = ((flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) &&
382  vnet_sw_interface_is_link_up (vnm, sw_if_index));
383  if (sif->port_enabled == 0)
384  {
385  lacp_init_neighbor (sif, sif->actor_admin.system,
386  ntohs (sif->actor_admin.port_number),
387  ntohs (sif->actor_admin.key));
388  lacp_init_state_machines (vm, sif);
389  }
390  }
391 
392  return 0;
393 }
394 
396 
397 static clib_error_t *
399 {
400  lacp_main_t *lm = &lacp_main;
401  slave_if_t *sif;
403  vlib_main_t *vm = lm->vlib_main;
404 
405  sw = vnet_get_hw_sw_interface (vnm, hw_if_index);
407  if (sif)
408  {
409  if (sif->lacp_enabled == 0)
410  return 0;
411 
412  /* port_enabled is both admin up and hw link up */
413  sif->port_enabled = ((flags & VNET_HW_INTERFACE_FLAG_LINK_UP) &&
415  sw->sw_if_index));
416  if (sif->port_enabled == 0)
417  {
418  lacp_init_neighbor (sif, sif->actor_admin.system,
419  ntohs (sif->actor_admin.port_number),
420  ntohs (sif->actor_admin.key));
421  lacp_init_state_machines (vm, sif);
422  }
423  }
424 
425  return 0;
426 }
427 
429 
430 /* *INDENT-OFF* */
432  .version = VPP_BUILD_VER,
433  .description = "Link Aggregation Control Protocol (LACP)",
434 };
435 /* *INDENT-ON* */
436 
437 /*
438  * fd.io coding-style-patch-verification: ON
439  *
440  * Local Variables:
441  * eval: (c-set-style "gnu")
442  * End:
443  */
void lacp_send_lacp_pdu(vlib_main_t *vm, slave_if_t *sif)
Definition: lacp.c:108
void lacp_init_mux_machine(vlib_main_t *vm, slave_if_t *sif)
Definition: mux_machine.c:238
vlib_main_t * vlib_main
Definition: node.h:121
vlib_main_t vlib_global_main
Definition: main.c:1940
u32 flags
Definition: vhost_user.h:141
void lacp_init_ptx_machine(vlib_main_t *vm, slave_if_t *sif)
Definition: ptx_machine.c:223
int selected
Definition: node.h:271
static vnet_hw_interface_t * vnet_get_sup_hw_interface(vnet_main_t *vnm, u32 sw_if_index)
volatile u32 lacp_int
Definition: node.h:134
f64 last_lacpdu_recd_time
Definition: node.h:286
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:110
static void lacp_send_ethernet_lacp_pdu(slave_if_t *sif)
Definition: lacp.c:44
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:279
void lacp_init_neighbor(slave_if_t *sif, u8 *hw_address, u16 port_number, u32 group)
Definition: lacp.c:316
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:113
bond_main_t bond_main
Definition: node.c:25
static uword * clib_bitmap_set(uword *ai, uword i, uword value)
Sets the ith bit of a bitmap to new_value Removes trailing zeros from the bitmap. ...
Definition: bitmap.h:167
f64 current_while_timer
Definition: node.h:277
u8 mode
Definition: node.h:319
static uword vnet_sw_interface_is_link_up(vnet_main_t *vnm, u32 sw_if_index)
unsigned char u8
Definition: types.h:56
lacp_fsm_state_t * state_table
Definition: machine.h:37
u8 ntt
Definition: node.h:253
static vnet_sw_interface_t * vnet_get_hw_sw_interface(vnet_main_t *vnm, u32 hw_if_index)
#define clib_memcpy(d, s, n)
Definition: string.h:180
f64 periodic_timer
Definition: node.h:295
void lacp_create_periodic_process(void)
Definition: node.c:229
static void bond_register_callback(lacp_enable_disable_func func)
Definition: node.h:473
f32 ttl_in_seconds
Definition: node.h:215
void * vlib_packet_template_get_packet(vlib_main_t *vm, vlib_packet_template_t *t, u32 *bi_result)
Definition: buffer.c:400
static u8 lacp_timer_is_running(f64 timer)
Definition: node.h:166
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:493
vl_api_interface_index_t sw_if_index
Definition: gre.api:50
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
vnet_main_t * vnet_main
Definition: node.h:122
VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION(lacp_sw_interface_up_down)
vlib_packet_template_t packet_templates[LACP_N_PACKET_TEMPLATES]
Definition: node.h:128
f64 last_marker_pdu_recd_time
Definition: node.h:292
action_func action
Definition: machine.h:28
lacp_port_info_t actor
Definition: node.h:246
static u8 lacp_timer_is_expired(vlib_main_t *vm, f64 timer)
Definition: node.h:172
vlib_frame_t * vlib_get_frame_to_node(vlib_main_t *vm, u32 to_node_index)
Definition: main.c:185
vhost_vring_state_t state
Definition: vhost_user.h:146
unsigned int u32
Definition: types.h:88
f64 partner_churn_timer
Definition: node.h:298
static void lacp_fill_pdu(lacp_pdu_t *lacpdu, slave_if_t *sif)
Definition: lacp.c:31
#define LACP_DEFAULT_SYSTEM_PRIORITY
Definition: protocol.h:99
void lacp_periodic(vlib_main_t *vm)
Definition: lacp.c:138
#define MARKER_SUBTYPE
Definition: protocol.h:132
u8 ready
Definition: node.h:268
void vlib_packet_template_init(vlib_main_t *vm, vlib_packet_template_t *t, void *packet_data, uword n_packet_data_bytes, uword min_n_buffers_each_alloc, char *fmt,...)
Definition: buffer.c:378
static void vlib_process_signal_event(vlib_main_t *vm, uword node_index, uword type_opaque, uword data)
Definition: node_funcs.h:934
f64 last_marker_pdu_sent_time
Definition: node.h:289
static void lacp_stop_timer(f64 *timer)
Definition: node.h:160
VLIB_PLUGIN_REGISTER()
unsigned short u16
Definition: types.h:57
u8 port_moved
Definition: node.h:274
void vlib_put_frame_to_node(vlib_main_t *vm, u32 to_node_index, vlib_frame_t *f)
Definition: main.c:194
static void lacp_interface_enable_disable(vlib_main_t *vm, bond_if_t *bif, slave_if_t *sif, u8 enable)
Definition: lacp.c:175
#define LACP_SUBTYPE
Definition: protocol.h:25
#define ELOG_DATA(em, f)
Definition: elog.h:484
#define LACP_SHORT_TIMOUT_TIME
Definition: node.h:26
vl_api_address_t dst
Definition: gre.api:52
#define MARKER_PROTOCOL_VERSION
Definition: protocol.h:133
static int lacp_pick_packet_template(slave_if_t *sif)
Definition: lacp.c:100
static clib_error_t * lacp_periodic_init(vlib_main_t *vm)
Definition: lacp.c:221
u16 n_vectors
Definition: node.h:397
u8 is_passive
Definition: node.h:242
u32 sw_if_index
Definition: node.h:212
vlib_main_t * vm
Definition: buffer.c:323
lacp_port_info_t partner
Definition: node.h:245
#define LACP_DEFAULT_PORT_PRIORITY
Definition: protocol.h:98
u32 group
Definition: node.h:310
elog_main_t elog_main
Definition: main.h:193
void lacp_init_rx_machine(vlib_main_t *vm, slave_if_t *sif)
Definition: rx_machine.c:428
lacp_main_t lacp_main
Definition: lacp.c:25
#define ELOG_TYPE_DECLARE(f)
Definition: elog.h:442
clib_error_t * lacp_plugin_api_hookup(vlib_main_t *vm)
Definition: lacp_api.c:169
f64 last_lacpdu_sent_time
Definition: node.h:283
u8 loopback_port
Definition: node.h:316
#define ASSERT(truth)
#define LACP_DBG2(n, e, s, m, t)
Definition: node.h:53
static clib_error_t * lacp_hw_interface_up_down(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
Definition: lacp.c:398
vlib_packet_template_t marker_packet_templates[MARKER_N_PACKET_TEMPLATES]
Definition: node.h:131
u8 ready_n
Definition: node.h:265
static uword vnet_sw_interface_is_admin_up(vnet_main_t *vnm, u32 sw_if_index)
void lacp_init_tx_machine(vlib_main_t *vm, slave_if_t *sif)
Definition: tx_machine.c:109
u8 lacp_enabled
Definition: node.h:262
Definition: defs.h:47
u64 pdu_sent
Definition: node.h:328
u32 lacp_process_node_index
Definition: node.h:125
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
VLIB buffer representation.
Definition: buffer.h:102
uword * port_number_bitmap
Definition: node.h:200
u64 uword
Definition: types.h:112
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:244
lacp_port_info_t partner_admin
Definition: node.h:246
VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION(lacp_hw_interface_up_down)
#define vnet_buffer(b)
Definition: buffer.h:365
slave_if_t * neighbors
Definition: node.h:352
u8 hw_address[6]
Definition: node.h:202
f64 actor_churn_timer
Definition: node.h:280
u8 port_enabled
Definition: node.h:256
static slave_if_t * bond_get_slave_by_sw_if_index(u32 sw_if_index)
Definition: node.h:504
lacp_port_info_t actor_admin
Definition: node.h:250
static uword clib_bitmap_first_clear(uword *ai)
Return the lowest numbered clear bit in a bitmap.
Definition: bitmap.h:445
u32 total_length_not_including_first_buffer
Only valid for first buffer in chain.
Definition: buffer.h:167
u8 packet_template_index
Definition: node.h:224
void lacp_init_state_machines(vlib_main_t *vm, slave_if_t *sif)
Definition: lacp.c:357
f64 wait_while_timer
Definition: node.h:301
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:85
lacp_fsm_machine_t * tables
Definition: machine.h:42
static clib_error_t * lacp_sw_interface_up_down(vnet_main_t *vnm, u32 sw_if_index, u32 flags)
Definition: lacp.c:368
int lacp_machine_dispatch(lacp_machine_t *machine, vlib_main_t *vm, slave_if_t *sif, int event, int *state)
Definition: lacp.c:300
#define LACP_ACTOR_LACP_VERSION
Definition: protocol.h:26