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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
25 lacp_main_t lacp_main;
31 lacp_fill_pdu (lacp_pdu_t * lacpdu, slave_if_t * sif)
34 lacpdu->actor.port_info = sif->actor;
37 lacpdu->partner.port_info = sif->partner;
41 * send a lacp pkt on an ethernet interface
44 lacp_send_ethernet_lacp_pdu (slave_if_t * sif)
46 lacp_main_t *lm = &lacp_main;
48 ethernet_lacp_pdu_t *h0;
49 vnet_hw_interface_t *hw;
53 vlib_main_t *vm = lm->vlib_main;
54 vnet_main_t *vnm = lm->vnet_main;
57 * see lacp_periodic_init() to understand what's already painted
58 * into the buffer by the packet template mechanism
60 h0 = vlib_packet_template_get_packet
61 (vm, &lm->packet_templates[sif->packet_template_index], &bi0);
66 /* Add the interface's ethernet source address */
67 hw = vnet_get_sup_hw_interface (vnm, sif->sw_if_index);
69 clib_memcpy (h0->ethernet.src_address, hw->hw_address,
70 vec_len (hw->hw_address));
72 lacp_fill_pdu (&h0->lacp, sif);
74 /* Set the outbound packet length */
75 b0 = vlib_get_buffer (vm, bi0);
76 b0->current_length = sizeof (ethernet_lacp_pdu_t);
78 b0->total_length_not_including_first_buffer = 0;
80 /* And the outbound interface */
81 vnet_buffer (b0)->sw_if_index[VLIB_TX] = hw->sw_if_index;
83 /* And output the packet on the correct interface */
84 f = vlib_get_frame_to_node (vm, hw->output_node_index);
86 to_next = vlib_frame_vector_args (f);
90 vlib_put_frame_to_node (vm, hw->output_node_index, f);
92 sif->last_lacpdu_time = vlib_time_now (vm);
96 * Decide which lacp packet template to use
99 lacp_pick_packet_template (slave_if_t * sif)
101 sif->packet_template_index = LACP_PACKET_TEMPLATE_ETHERNET;
107 lacp_send_lacp_pdu (vlib_main_t * vm, slave_if_t * sif)
109 lacp_main_t *lm = &lacp_main;
111 if (sif->mode != BOND_MODE_LACP)
113 lacp_stop_timer (&sif->periodic_timer);
117 if (sif->packet_template_index == (u8) ~ 0)
119 /* If we don't know how to talk to this peer, don't try again */
120 if (lacp_pick_packet_template (sif))
122 lacp_stop_timer (&sif->periodic_timer);
127 switch (sif->packet_template_index)
129 case LACP_PACKET_TEMPLATE_ETHERNET:
130 lacp_send_ethernet_lacp_pdu (sif);
137 lacp_start_periodic_timer (lm->vlib_main, sif, sif->is_long_timeout ?
138 LACP_SLOW_PERIODIC_TIMER :
139 LACP_FAST_PERIODIC_TIMER);
143 lacp_periodic (vlib_main_t * vm)
145 bond_main_t *bm = &bond_main;
146 lacp_main_t *lm = &lacp_main;
150 pool_foreach (sif, bm->neighbors,
152 if (sif->port_enabled == 0)
155 if (lacp_timer_is_running (sif->current_while_timer) &&
156 lacp_timer_is_expired (lm->vlib_main, sif->current_while_timer))
158 lacp_machine_dispatch (&lacp_rx_machine, vm, sif,
159 LACP_RX_EVENT_TIMER_EXPIRED, &sif->rx_state);
162 if (lacp_timer_is_running (sif->periodic_timer) &&
163 lacp_timer_is_expired (lm->vlib_main, sif->periodic_timer))
165 lacp_machine_dispatch (&lacp_ptx_machine, vm, sif,
166 LACP_PTX_EVENT_TIMER_EXPIRED, &sif->ptx_state);
168 if (lacp_timer_is_running (sif->wait_while_timer) &&
169 lacp_timer_is_expired (lm->vlib_main, sif->wait_while_timer))
172 lacp_stop_timer (&sif->wait_while_timer);
173 lacp_selection_logic (vm, sif);
180 lacp_interface_enable_disable (vlib_main_t * vm, bond_if_t * bif,
181 slave_if_t * sif, u8 enable)
183 lacp_main_t *lm = &lacp_main;
188 port_number = clib_bitmap_first_clear (bif->port_number_bitmap);
189 bif->port_number_bitmap = clib_bitmap_set (bif->port_number_bitmap,
191 // bitmap starts at 0. Our port number starts at 1.
192 lacp_init_neighbor (sif, bif->hw_address, port_number + 1, sif->group);
193 lacp_init_state_machines (vm, sif);
195 if (lm->lacp_int == 1)
197 vlib_process_signal_event (vm, lm->lacp_process_node_index,
198 LACP_PROCESS_EVENT_START, 0);
204 if (lm->lacp_int == 0)
206 vlib_process_signal_event (vm, lm->lacp_process_node_index,
207 LACP_PROCESS_EVENT_STOP, 0);
212 static clib_error_t *
213 lacp_periodic_init (vlib_main_t * vm)
215 lacp_main_t *lm = &lacp_main;
216 ethernet_lacp_pdu_t h;
217 ethernet_marker_pdu_t m;
218 u8 dst[] = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 };
220 /* initialize binary API */
221 lacp_plugin_api_hookup (vm);
223 /* Create the ethernet lacp packet template */
225 memset (&h, 0, sizeof (h));
227 memcpy (h.ethernet.dst_address, dst, sizeof (h.ethernet.dst_address));
229 /* leave src address blank (fill in at send time) */
231 h.ethernet.type = htons (ETHERNET_TYPE_SLOW_PROTOCOLS);
233 h.lacp.subtype = LACP_SUBTYPE;
234 h.lacp.version_number = LACP_ACTOR_LACP_VERSION;
237 h.lacp.actor.tlv_type = LACP_ACTOR_INFORMATION;
238 h.lacp.actor.tlv_length = sizeof (lacp_actor_partner_t);
241 h.lacp.partner.tlv_type = LACP_PARTNER_INFORMATION;
242 h.lacp.partner.tlv_length = sizeof (lacp_actor_partner_t);
245 h.lacp.collector.tlv_type = LACP_COLLECTOR_INFORMATION;
246 h.lacp.collector.tlv_length = sizeof (lacp_collector_t);
247 h.lacp.collector.max_delay = 0;
250 h.lacp.terminator.tlv_type = LACP_TERMINATOR_INFORMATION;
251 h.lacp.terminator.tlv_length = 0;
253 vlib_packet_template_init
254 (vm, &lm->packet_templates[LACP_PACKET_TEMPLATE_ETHERNET],
257 /* alloc chunk size */ 8,
260 /* Create the ethernet marker protocol packet template */
262 memset (&m, 0, sizeof (m));
264 memcpy (m.ethernet.dst_address, dst, sizeof (m.ethernet.dst_address));
266 /* leave src address blank (fill in at send time) */
268 m.ethernet.type = htons (ETHERNET_TYPE_SLOW_PROTOCOLS);
270 m.marker.subtype = MARKER_SUBTYPE;
271 m.marker.version_number = MARKER_PROTOCOL_VERSION;
273 m.marker.marker_info.tlv_length = sizeof (marker_information_t);
276 m.marker.terminator.tlv_type = MARKER_TERMINATOR_INFORMATION;
277 m.marker.terminator.tlv_length = 0;
279 vlib_packet_template_init
280 (vm, &lm->marker_packet_templates[MARKER_PACKET_TEMPLATE_ETHERNET],
283 /* alloc chunk size */ 8,
286 bond_register_callback (lacp_interface_enable_disable);
292 lacp_machine_dispatch (lacp_machine_t * machine, vlib_main_t * vm,
293 slave_if_t * sif, int event, int *state)
295 lacp_fsm_state_t *transition;
298 transition = &machine->tables[*state].state_table[event];
299 LACP_DBG2 (sif, event, *state, machine, transition);
300 *state = transition->next_state;
301 if (transition->action)
302 rc = (*transition->action) ((void *) vm, (void *) sif);
308 lacp_init_neighbor (slave_if_t * sif, u8 * hw_address, u16 port_number,
311 lacp_stop_timer (&sif->wait_while_timer);
312 lacp_stop_timer (&sif->current_while_timer);
313 lacp_stop_timer (&sif->actor_churn_timer);
314 lacp_stop_timer (&sif->partner_churn_timer);
315 lacp_stop_timer (&sif->periodic_timer);
316 lacp_stop_timer (&sif->last_lacpdu_time);
317 sif->lacp_enabled = 1;
318 sif->loopback_port = 0;
323 sif->selected = LACP_PORT_UNSELECTED;
324 sif->actor.state = LACP_STATE_AGGREGATION;
325 if (sif->ttl_in_seconds == LACP_SHORT_TIMOUT_TIME)
326 sif->actor.state |= LACP_STATE_LACP_TIMEOUT;
327 if (sif->is_passive == 0)
328 sif->actor.state |= LACP_STATE_LACP_ACTIVITY;
329 clib_memcpy (sif->actor.system, hw_address, 6);
330 sif->actor.system_priority = htons (LACP_DEFAULT_SYSTEM_PRIORITY);
331 sif->actor.key = htons (group);
332 sif->actor.port_number = htons (port_number);
333 sif->actor.port_priority = htons (LACP_DEFAULT_PORT_PRIORITY);
335 sif->partner.system_priority = htons (LACP_DEFAULT_SYSTEM_PRIORITY);
336 sif->partner.key = htons (group);
337 sif->partner.port_number = htons (port_number);
338 sif->partner.port_priority = htons (LACP_DEFAULT_PORT_PRIORITY);
339 sif->partner.key = htons (group);
340 sif->partner.state = LACP_STATE_LACP_ACTIVITY;
342 sif->actor_admin = sif->actor;
343 sif->partner_admin = sif->partner;
347 lacp_init_state_machines (vlib_main_t * vm, slave_if_t * sif)
349 lacp_init_tx_machine (vm, sif);
350 lacp_init_mux_machine (vm, sif);
351 lacp_init_ptx_machine (vm, sif);
352 lacp_init_rx_machine (vm, sif);
355 VLIB_INIT_FUNCTION (lacp_periodic_init);
357 static clib_error_t *
358 lacp_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
360 lacp_main_t *lm = &lacp_main;
362 vlib_main_t *vm = lm->vlib_main;
364 sif = bond_get_slave_by_sw_if_index (sw_if_index);
367 sif->port_enabled = flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP;
368 if (sif->port_enabled == 0)
370 if (sif->lacp_enabled)
372 lacp_init_state_machines (vm, sif);
373 lacp_init_neighbor (sif, sif->actor_admin.system,
374 ntohs (sif->actor_admin.port_number),
375 ntohs (sif->actor_admin.key));
383 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (lacp_sw_interface_up_down);
385 static clib_error_t *
386 lacp_hw_interface_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
388 lacp_main_t *lm = &lacp_main;
390 vnet_sw_interface_t *sw;
391 vlib_main_t *vm = lm->vlib_main;
392 vnet_interface_main_t *im = &vnm->interface_main;
394 sw = pool_elt_at_index (im->sw_interfaces, hw_if_index);
395 sif = bond_get_slave_by_sw_if_index (sw->sw_if_index);
398 if (!(flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
400 if (sif->lacp_enabled)
402 lacp_init_state_machines (vm, sif);
403 lacp_init_neighbor (sif, sif->actor_admin.system,
404 ntohs (sif->actor_admin.port_number),
405 ntohs (sif->actor_admin.key));
413 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (lacp_hw_interface_up_down);
416 VLIB_PLUGIN_REGISTER () = {
417 .version = VPP_BUILD_VER,
418 .description = "Link Aggregation Control Protocol",
423 * fd.io coding-style-patch-verification: ON
426 * eval: (c-set-style "gnu")