c11 safe string handling support
[vpp.git] / src / plugins / lacp / lacp.c
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
25 lacp_main_t lacp_main;
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
44 lacp_send_ethernet_lacp_pdu (slave_if_t * sif)
45 {
46   lacp_main_t *lm = &lacp_main;
47   u32 *to_next;
48   ethernet_lacp_pdu_t *h0;
49   vnet_hw_interface_t *hw;
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    */
60   h0 = vlib_packet_template_get_packet
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;
78   b0->total_length_not_including_first_buffer = 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 */
84   f = vlib_get_frame_to_node (vm, hw->output_node_index);
85
86   to_next = vlib_frame_vector_args (f);
87   to_next[0] = bi0;
88   f->n_vectors = 1;
89
90   vlib_put_frame_to_node (vm, hw->output_node_index, f);
91
92   sif->last_lacpdu_time = vlib_time_now (lm->vlib_main);
93 }
94
95 /*
96  * Decide which lacp packet template to use
97  */
98 static int
99 lacp_pick_packet_template (slave_if_t * sif)
100 {
101   sif->packet_template_index = LACP_PACKET_TEMPLATE_ETHERNET;
102
103   return 0;
104 }
105
106 void
107 lacp_send_lacp_pdu (vlib_main_t * vm, slave_if_t * sif)
108 {
109   if (sif->mode != BOND_MODE_LACP)
110     {
111       lacp_stop_timer (&sif->periodic_timer);
112       return;
113     }
114
115   if (sif->packet_template_index == (u8) ~ 0)
116     {
117       /* If we don't know how to talk to this peer, don't try again */
118       if (lacp_pick_packet_template (sif))
119         {
120           lacp_stop_timer (&sif->periodic_timer);
121           return;
122         }
123     }
124
125   switch (sif->packet_template_index)
126     {
127     case LACP_PACKET_TEMPLATE_ETHERNET:
128       lacp_send_ethernet_lacp_pdu (sif);
129       break;
130
131     default:
132       ASSERT (0);
133     }
134 }
135
136 void
137 lacp_periodic (vlib_main_t * vm)
138 {
139   bond_main_t *bm = &bond_main;
140   lacp_main_t *lm = &lacp_main;
141   slave_if_t *sif;
142
143   /* *INDENT-OFF* */
144   pool_foreach (sif, bm->neighbors,
145   ({
146     if (sif->port_enabled == 0)
147       continue;
148
149     if (lacp_timer_is_running (sif->current_while_timer) &&
150         lacp_timer_is_expired (lm->vlib_main, sif->current_while_timer))
151       {
152         lacp_machine_dispatch (&lacp_rx_machine, vm, sif,
153                                LACP_RX_EVENT_TIMER_EXPIRED, &sif->rx_state);
154       }
155
156     if (lacp_timer_is_running (sif->periodic_timer) &&
157         lacp_timer_is_expired (lm->vlib_main, sif->periodic_timer))
158       {
159         lacp_machine_dispatch (&lacp_ptx_machine, vm, sif,
160                                LACP_PTX_EVENT_TIMER_EXPIRED, &sif->ptx_state);
161       }
162     if (lacp_timer_is_running (sif->wait_while_timer) &&
163         lacp_timer_is_expired (lm->vlib_main, sif->wait_while_timer))
164       {
165         sif->ready_n = 1;
166         lacp_stop_timer (&sif->wait_while_timer);
167         lacp_selection_logic (vm, sif);
168       }
169   }));
170   /* *INDENT-ON* */
171 }
172
173 static void
174 lacp_interface_enable_disable (vlib_main_t * vm, bond_if_t * bif,
175                                slave_if_t * sif, u8 enable)
176 {
177   lacp_main_t *lm = &lacp_main;
178   uword port_number;
179
180   if (enable)
181     {
182       port_number = clib_bitmap_first_clear (bif->port_number_bitmap);
183       bif->port_number_bitmap = clib_bitmap_set (bif->port_number_bitmap,
184                                                  port_number, 1);
185       // bitmap starts at 0. Our port number starts at 1.
186       lacp_init_neighbor (sif, bif->hw_address, port_number + 1, sif->group);
187       lacp_init_state_machines (vm, sif);
188       lm->lacp_int++;
189       if (lm->lacp_int == 1)
190         {
191           vlib_process_signal_event (vm, lm->lacp_process_node_index,
192                                      LACP_PROCESS_EVENT_START, 0);
193         }
194     }
195   else
196     {
197       lm->lacp_int--;
198       if (lm->lacp_int == 0)
199         {
200           vlib_process_signal_event (vm, lm->lacp_process_node_index,
201                                      LACP_PROCESS_EVENT_STOP, 0);
202         }
203     }
204 }
205
206 static clib_error_t *
207 lacp_periodic_init (vlib_main_t * vm)
208 {
209   lacp_main_t *lm = &lacp_main;
210   ethernet_lacp_pdu_t h;
211   ethernet_marker_pdu_t m;
212   u8 dst[] = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 };
213
214   /* initialize binary API */
215   lacp_plugin_api_hookup (vm);
216
217   /* Create the ethernet lacp packet template */
218
219   clib_memset (&h, 0, sizeof (h));
220
221   memcpy (h.ethernet.dst_address, dst, sizeof (h.ethernet.dst_address));
222
223   /* leave src address blank (fill in at send time) */
224
225   h.ethernet.type = htons (ETHERNET_TYPE_SLOW_PROTOCOLS);
226
227   h.lacp.subtype = LACP_SUBTYPE;
228   h.lacp.version_number = LACP_ACTOR_LACP_VERSION;
229
230   /* Actor TLV */
231   h.lacp.actor.tlv_type = LACP_ACTOR_INFORMATION;
232   h.lacp.actor.tlv_length = sizeof (lacp_actor_partner_t);
233
234   /* Partner TLV */
235   h.lacp.partner.tlv_type = LACP_PARTNER_INFORMATION;
236   h.lacp.partner.tlv_length = sizeof (lacp_actor_partner_t);
237
238   /* Collector TLV */
239   h.lacp.collector.tlv_type = LACP_COLLECTOR_INFORMATION;
240   h.lacp.collector.tlv_length = sizeof (lacp_collector_t);
241   h.lacp.collector.max_delay = 0;
242
243   /* Terminator TLV */
244   h.lacp.terminator.tlv_type = LACP_TERMINATOR_INFORMATION;
245   h.lacp.terminator.tlv_length = 0;
246
247   vlib_packet_template_init
248     (vm, &lm->packet_templates[LACP_PACKET_TEMPLATE_ETHERNET],
249      /* data */ &h,
250      sizeof (h),
251      /* alloc chunk size */ 8,
252      "lacp-ethernet");
253
254   /* Create the ethernet marker protocol packet template */
255
256   clib_memset (&m, 0, sizeof (m));
257
258   memcpy (m.ethernet.dst_address, dst, sizeof (m.ethernet.dst_address));
259
260   /* leave src address blank (fill in at send time) */
261
262   m.ethernet.type = htons (ETHERNET_TYPE_SLOW_PROTOCOLS);
263
264   m.marker.subtype = MARKER_SUBTYPE;
265   m.marker.version_number = MARKER_PROTOCOL_VERSION;
266
267   m.marker.marker_info.tlv_length = sizeof (marker_information_t);
268
269   /* Terminator TLV */
270   m.marker.terminator.tlv_type = MARKER_TERMINATOR_INFORMATION;
271   m.marker.terminator.tlv_length = 0;
272
273   vlib_packet_template_init
274     (vm, &lm->marker_packet_templates[MARKER_PACKET_TEMPLATE_ETHERNET],
275      /* data */ &m,
276      sizeof (m),
277      /* alloc chunk size */ 8,
278      "marker-ethernet");
279
280   bond_register_callback (lacp_interface_enable_disable);
281
282   return 0;
283 }
284
285 int
286 lacp_machine_dispatch (lacp_machine_t * machine, vlib_main_t * vm,
287                        slave_if_t * sif, int event, int *state)
288 {
289   lacp_fsm_state_t *transition;
290   int rc = 0;
291
292   transition = &machine->tables[*state].state_table[event];
293   LACP_DBG2 (sif, event, *state, machine, transition);
294   *state = transition->next_state;
295   if (transition->action)
296     rc = (*transition->action) ((void *) vm, (void *) sif);
297
298   return rc;
299 }
300
301 void
302 lacp_init_neighbor (slave_if_t * sif, u8 * hw_address, u16 port_number,
303                     u32 group)
304 {
305   lacp_stop_timer (&sif->wait_while_timer);
306   lacp_stop_timer (&sif->current_while_timer);
307   lacp_stop_timer (&sif->actor_churn_timer);
308   lacp_stop_timer (&sif->partner_churn_timer);
309   lacp_stop_timer (&sif->periodic_timer);
310   lacp_stop_timer (&sif->last_lacpdu_time);
311   sif->lacp_enabled = 1;
312   sif->loopback_port = 0;
313   sif->ready = 0;
314   sif->ready_n = 0;
315   sif->port_moved = 0;
316   sif->ntt = 0;
317   sif->selected = LACP_PORT_UNSELECTED;
318   sif->actor.state = LACP_STATE_AGGREGATION;
319   if (sif->ttl_in_seconds == LACP_SHORT_TIMOUT_TIME)
320     sif->actor.state |= LACP_STATE_LACP_TIMEOUT;
321   if (sif->is_passive == 0)
322     sif->actor.state |= LACP_STATE_LACP_ACTIVITY;
323   clib_memcpy (sif->actor.system, hw_address, 6);
324   sif->actor.system_priority = htons (LACP_DEFAULT_SYSTEM_PRIORITY);
325   sif->actor.key = htons (group);
326   sif->actor.port_number = htons (port_number);
327   sif->actor.port_priority = htons (LACP_DEFAULT_PORT_PRIORITY);
328
329   sif->partner.system_priority = htons (LACP_DEFAULT_SYSTEM_PRIORITY);
330   sif->partner.key = htons (group);
331   sif->partner.port_number = htons (port_number);
332   sif->partner.port_priority = htons (LACP_DEFAULT_PORT_PRIORITY);
333   sif->partner.key = htons (group);
334   sif->partner.state = LACP_STATE_LACP_ACTIVITY;
335
336   sif->actor_admin = sif->actor;
337   sif->partner_admin = sif->partner;
338 }
339
340 void
341 lacp_init_state_machines (vlib_main_t * vm, slave_if_t * sif)
342 {
343   lacp_init_tx_machine (vm, sif);
344   lacp_init_mux_machine (vm, sif);
345   lacp_init_ptx_machine (vm, sif);
346   lacp_init_rx_machine (vm, sif);
347 }
348
349 VLIB_INIT_FUNCTION (lacp_periodic_init);
350
351 static clib_error_t *
352 lacp_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
353 {
354   lacp_main_t *lm = &lacp_main;
355   slave_if_t *sif;
356   vlib_main_t *vm = lm->vlib_main;
357
358   sif = bond_get_slave_by_sw_if_index (sw_if_index);
359   if (sif)
360     {
361       sif->port_enabled = flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP;
362       if (sif->port_enabled == 0)
363         {
364           if (sif->lacp_enabled)
365             {
366               lacp_init_neighbor (sif, sif->actor_admin.system,
367                                   ntohs (sif->actor_admin.port_number),
368                                   ntohs (sif->actor_admin.key));
369               lacp_init_state_machines (vm, sif);
370             }
371         }
372     }
373
374   return 0;
375 }
376
377 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (lacp_sw_interface_up_down);
378
379 static clib_error_t *
380 lacp_hw_interface_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
381 {
382   lacp_main_t *lm = &lacp_main;
383   slave_if_t *sif;
384   vnet_sw_interface_t *sw;
385   vlib_main_t *vm = lm->vlib_main;
386
387   sw = vnet_get_hw_sw_interface (vnm, hw_if_index);
388   sif = bond_get_slave_by_sw_if_index (sw->sw_if_index);
389   if (sif)
390     {
391       if (!(flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
392         {
393           if (sif->lacp_enabled)
394             {
395               lacp_init_neighbor (sif, sif->actor_admin.system,
396                                   ntohs (sif->actor_admin.port_number),
397                                   ntohs (sif->actor_admin.key));
398               lacp_init_state_machines (vm, sif);
399             }
400         }
401     }
402
403   return 0;
404 }
405
406 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (lacp_hw_interface_up_down);
407
408 /* *INDENT-OFF* */
409 VLIB_PLUGIN_REGISTER () = {
410     .version = VPP_BUILD_VER,
411     .description = "Link Aggregation Control Protocol",
412 };
413 /* *INDENT-ON* */
414
415 /*
416  * fd.io coding-style-patch-verification: ON
417  *
418  * Local Variables:
419  * eval: (c-set-style "gnu")
420  * End:
421  */