builtinurl: mark api as deprecated
[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 #include <vlib/stats/stats.h>
25
26 lacp_main_t lacp_main;
27
28 /*
29  * Generate lacp pdu
30  */
31 static void
32 lacp_fill_pdu (lacp_pdu_t * lacpdu, member_if_t * mif)
33 {
34   /* Actor TLV */
35   lacpdu->actor.port_info = mif->actor;
36
37   /* Partner TLV */
38   lacpdu->partner.port_info = mif->partner;
39 }
40
41 /*
42  * send a lacp pkt on an ethernet interface
43  */
44 static void
45 lacp_send_ethernet_lacp_pdu (vlib_main_t * vm, member_if_t * mif)
46 {
47   lacp_main_t *lm = &lacp_main;
48   u32 *to_next;
49   ethernet_lacp_pdu_t *h0;
50   vnet_hw_interface_t *hw;
51   u32 bi0;
52   vlib_buffer_t *b0;
53   vlib_frame_t *f;
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[mif->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, mif->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, mif);
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   mif->last_lacpdu_sent_time = vlib_time_now (vm);
93   mif->pdu_sent++;
94 }
95
96 /*
97  * Decide which lacp packet template to use
98  */
99 static int
100 lacp_pick_packet_template (member_if_t * mif)
101 {
102   mif->packet_template_index = LACP_PACKET_TEMPLATE_ETHERNET;
103
104   return 0;
105 }
106
107 void
108 lacp_send_lacp_pdu (vlib_main_t * vm, member_if_t * mif)
109 {
110   if ((mif->mode != BOND_MODE_LACP) || (mif->port_enabled == 0))
111     {
112       lacp_stop_timer (&mif->periodic_timer);
113       return;
114     }
115
116   if (mif->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 (mif))
120         {
121           lacp_stop_timer (&mif->periodic_timer);
122           return;
123         }
124     }
125
126   switch (mif->packet_template_index)
127     {
128     case LACP_PACKET_TEMPLATE_ETHERNET:
129       lacp_send_ethernet_lacp_pdu (vm, mif);
130       break;
131
132     default:
133       ASSERT (0);
134     }
135 }
136
137 void
138 lacp_periodic (vlib_main_t * vm)
139 {
140   bond_main_t *bm = &bond_main;
141   member_if_t *mif;
142   bond_if_t *bif;
143   u8 actor_state, partner_state;
144
145   pool_foreach (mif, bm->neighbors)
146    {
147     if (mif->port_enabled == 0)
148       continue;
149
150     actor_state = mif->actor.state;
151     partner_state = mif->partner.state;
152     if (lacp_timer_is_running (mif->current_while_timer) &&
153         lacp_timer_is_expired (vm, mif->current_while_timer))
154       {
155         lacp_machine_dispatch (&lacp_rx_machine, vm, mif,
156                                LACP_RX_EVENT_TIMER_EXPIRED, &mif->rx_state);
157       }
158
159     if (lacp_timer_is_running (mif->periodic_timer) &&
160         lacp_timer_is_expired (vm, mif->periodic_timer))
161       {
162         lacp_machine_dispatch (&lacp_ptx_machine, vm, mif,
163                                LACP_PTX_EVENT_TIMER_EXPIRED, &mif->ptx_state);
164       }
165     if (lacp_timer_is_running (mif->wait_while_timer) &&
166         lacp_timer_is_expired (vm, mif->wait_while_timer))
167       {
168         mif->ready_n = 1;
169         lacp_stop_timer (&mif->wait_while_timer);
170         lacp_selection_logic (vm, mif);
171       }
172     if (actor_state != mif->actor.state)
173       {
174         bif = bond_get_bond_if_by_dev_instance (mif->bif_dev_instance);
175         vlib_stats_set_gauge (
176           bm->stats[bif->sw_if_index][mif->sw_if_index].actor_state,
177           mif->actor.state);
178       }
179     if (partner_state != mif->partner.state)
180       {
181         bif = bond_get_bond_if_by_dev_instance (mif->bif_dev_instance);
182         vlib_stats_set_gauge (
183           bm->stats[bif->sw_if_index][mif->sw_if_index].partner_state,
184           mif->partner.state);
185       }
186   }
187 }
188
189 static void
190 lacp_interface_enable_disable (vlib_main_t * vm, bond_if_t * bif,
191                                member_if_t * mif, u8 enable)
192 {
193   lacp_main_t *lm = &lacp_main;
194   uword port_number;
195
196   if (enable)
197     {
198       lacp_create_periodic_process ();
199       port_number = clib_bitmap_first_clear (bif->port_number_bitmap);
200       bif->port_number_bitmap = clib_bitmap_set (bif->port_number_bitmap,
201                                                  port_number, 1);
202       // bitmap starts at 0. Our port number starts at 1.
203       lacp_init_neighbor (mif, bif->hw_address, port_number + 1, mif->group);
204       lacp_init_state_machines (vm, mif);
205       lm->lacp_int++;
206       if (lm->lacp_int == 1)
207         {
208           vlib_process_signal_event (vm, lm->lacp_process_node_index,
209                                      LACP_PROCESS_EVENT_START, 0);
210         }
211     }
212   else
213     {
214       ASSERT (lm->lacp_int >= 1);
215       if (lm->lacp_int == 0)
216         {
217           ELOG_TYPE_DECLARE (e) =
218             {
219               .format = "lacp-int-en-dis: BUG lacp_int == 0",
220             };
221           ELOG_DATA (&vlib_global_main.elog_main, e);
222         }
223       else
224         {
225           lm->lacp_int--;
226           if (lm->lacp_int == 0)
227             vlib_process_signal_event (vm, lm->lacp_process_node_index,
228                                        LACP_PROCESS_EVENT_STOP, 0);
229         }
230     }
231 }
232
233 static clib_error_t *
234 lacp_periodic_init (vlib_main_t * vm)
235 {
236   lacp_main_t *lm = &lacp_main;
237   ethernet_lacp_pdu_t h;
238   ethernet_marker_pdu_t m;
239   u8 dst[] = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 };
240
241   /* initialize binary API */
242   lacp_plugin_api_hookup (vm);
243
244   /* Create the ethernet lacp packet template */
245
246   clib_memset (&h, 0, sizeof (h));
247
248   memcpy (h.ethernet.dst_address, dst, sizeof (h.ethernet.dst_address));
249
250   /* leave src address blank (fill in at send time) */
251
252   h.ethernet.type = htons (ETHERNET_TYPE_SLOW_PROTOCOLS);
253
254   h.lacp.subtype = LACP_SUBTYPE;
255   h.lacp.version_number = LACP_ACTOR_LACP_VERSION;
256
257   /* Actor TLV */
258   h.lacp.actor.tlv_type = LACP_ACTOR_INFORMATION;
259   h.lacp.actor.tlv_length = sizeof (lacp_actor_partner_t);
260
261   /* Partner TLV */
262   h.lacp.partner.tlv_type = LACP_PARTNER_INFORMATION;
263   h.lacp.partner.tlv_length = sizeof (lacp_actor_partner_t);
264
265   /* Collector TLV */
266   h.lacp.collector.tlv_type = LACP_COLLECTOR_INFORMATION;
267   h.lacp.collector.tlv_length = sizeof (lacp_collector_t);
268   h.lacp.collector.max_delay = 0;
269
270   /* Terminator TLV */
271   h.lacp.terminator.tlv_type = LACP_TERMINATOR_INFORMATION;
272   h.lacp.terminator.tlv_length = 0;
273
274   vlib_packet_template_init
275     (vm, &lm->packet_templates[LACP_PACKET_TEMPLATE_ETHERNET],
276      /* data */ &h,
277      sizeof (h),
278      /* alloc chunk size */ 8,
279      "lacp-ethernet");
280
281   /* Create the ethernet marker protocol packet template */
282
283   clib_memset (&m, 0, sizeof (m));
284
285   memcpy (m.ethernet.dst_address, dst, sizeof (m.ethernet.dst_address));
286
287   /* leave src address blank (fill in at send time) */
288
289   m.ethernet.type = htons (ETHERNET_TYPE_SLOW_PROTOCOLS);
290
291   m.marker.subtype = MARKER_SUBTYPE;
292   m.marker.version_number = MARKER_PROTOCOL_VERSION;
293
294   m.marker.marker_info.tlv_length = sizeof (marker_information_t);
295
296   /* Terminator TLV */
297   m.marker.terminator.tlv_type = MARKER_TERMINATOR_INFORMATION;
298   m.marker.terminator.tlv_length = 0;
299
300   vlib_packet_template_init
301     (vm, &lm->marker_packet_templates[MARKER_PACKET_TEMPLATE_ETHERNET],
302      /* data */ &m,
303      sizeof (m),
304      /* alloc chunk size */ 8,
305      "marker-ethernet");
306
307   bond_register_callback (lacp_interface_enable_disable);
308
309   return 0;
310 }
311
312 int
313 lacp_machine_dispatch (lacp_machine_t * machine, vlib_main_t * vm,
314                        member_if_t * mif, int event, int *state)
315 {
316   lacp_fsm_state_t *transition;
317   int rc = 0;
318
319   transition = &machine->tables[*state].state_table[event];
320   LACP_DBG2 (mif, event, *state, machine, transition);
321   *state = transition->next_state;
322   if (transition->action)
323     rc = (*transition->action) ((void *) vm, (void *) mif);
324
325   return rc;
326 }
327
328 void
329 lacp_init_neighbor (member_if_t * mif, u8 * hw_address, u16 port_number,
330                     u32 group)
331 {
332   lacp_stop_timer (&mif->wait_while_timer);
333   lacp_stop_timer (&mif->current_while_timer);
334   lacp_stop_timer (&mif->actor_churn_timer);
335   lacp_stop_timer (&mif->partner_churn_timer);
336   lacp_stop_timer (&mif->periodic_timer);
337   lacp_stop_timer (&mif->last_lacpdu_sent_time);
338   lacp_stop_timer (&mif->last_lacpdu_recd_time);
339   lacp_stop_timer (&mif->last_marker_pdu_sent_time);
340   lacp_stop_timer (&mif->last_marker_pdu_recd_time);
341   mif->lacp_enabled = 1;
342   mif->loopback_port = 0;
343   mif->ready = 0;
344   mif->ready_n = 0;
345   mif->port_moved = 0;
346   mif->ntt = 0;
347   mif->selected = LACP_PORT_UNSELECTED;
348   mif->actor.state = LACP_STATE_AGGREGATION;
349   if (mif->ttl_in_seconds == LACP_SHORT_TIMOUT_TIME)
350     mif->actor.state |= LACP_STATE_LACP_TIMEOUT;
351   if (mif->is_passive == 0)
352     mif->actor.state |= LACP_STATE_LACP_ACTIVITY;
353   clib_memcpy (mif->actor.system, hw_address, 6);
354   mif->actor.system_priority = htons (LACP_DEFAULT_SYSTEM_PRIORITY);
355   mif->actor.key = htons (group);
356   mif->actor.port_number = htons (port_number);
357   mif->actor.port_priority = htons (LACP_DEFAULT_PORT_PRIORITY);
358
359   mif->partner.system_priority = htons (LACP_DEFAULT_SYSTEM_PRIORITY);
360   mif->partner.key = htons (group);
361   mif->partner.port_number = htons (port_number);
362   mif->partner.port_priority = htons (LACP_DEFAULT_PORT_PRIORITY);
363   mif->partner.state = 0;
364
365   mif->actor_admin = mif->actor;
366   mif->partner_admin = mif->partner;
367 }
368
369 void
370 lacp_init_state_machines (vlib_main_t * vm, member_if_t * mif)
371 {
372   bond_main_t *bm = &bond_main;
373   bond_if_t *bif = bond_get_bond_if_by_dev_instance (mif->bif_dev_instance);
374
375   lacp_init_tx_machine (vm, mif);
376   lacp_init_mux_machine (vm, mif);
377   lacp_init_ptx_machine (vm, mif);
378   lacp_init_rx_machine (vm, mif);
379   vlib_stats_set_gauge (
380     bm->stats[bif->sw_if_index][mif->sw_if_index].actor_state,
381     mif->actor.state);
382   vlib_stats_set_gauge (
383     bm->stats[bif->sw_if_index][mif->sw_if_index].partner_state,
384     mif->partner.state);
385 }
386
387 VLIB_INIT_FUNCTION (lacp_periodic_init);
388
389 static clib_error_t *
390 lacp_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
391 {
392   lacp_main_t *lm = &lacp_main;
393   member_if_t *mif;
394   vlib_main_t *vm = lm->vlib_main;
395
396   mif = bond_get_member_by_sw_if_index (sw_if_index);
397   if (mif)
398     {
399       if (mif->lacp_enabled == 0)
400         return 0;
401
402       /* port_enabled is both admin up and hw link up */
403       mif->port_enabled = ((flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) &&
404                            vnet_sw_interface_is_link_up (vnm, sw_if_index));
405       if (mif->port_enabled == 0)
406         {
407           lacp_init_neighbor (mif, mif->actor_admin.system,
408                               ntohs (mif->actor_admin.port_number),
409                               ntohs (mif->actor_admin.key));
410           lacp_init_state_machines (vm, mif);
411         }
412     }
413
414   return 0;
415 }
416
417 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (lacp_sw_interface_up_down);
418
419 static clib_error_t *
420 lacp_hw_interface_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
421 {
422   lacp_main_t *lm = &lacp_main;
423   member_if_t *mif;
424   vnet_sw_interface_t *sw;
425   vlib_main_t *vm = lm->vlib_main;
426
427   sw = vnet_get_hw_sw_interface (vnm, hw_if_index);
428   mif = bond_get_member_by_sw_if_index (sw->sw_if_index);
429   if (mif)
430     {
431       if (mif->lacp_enabled == 0)
432         return 0;
433
434       /* port_enabled is both admin up and hw link up */
435       mif->port_enabled = ((flags & VNET_HW_INTERFACE_FLAG_LINK_UP) &&
436                            vnet_sw_interface_is_admin_up (vnm,
437                                                           sw->sw_if_index));
438       if (mif->port_enabled == 0)
439         {
440           lacp_init_neighbor (mif, mif->actor_admin.system,
441                               ntohs (mif->actor_admin.port_number),
442                               ntohs (mif->actor_admin.key));
443           lacp_init_state_machines (vm, mif);
444         }
445     }
446
447   return 0;
448 }
449
450 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (lacp_hw_interface_up_down);
451
452 VLIB_PLUGIN_REGISTER () = {
453     .version = VPP_BUILD_VER,
454     .description = "Link Aggregation Control Protocol (LACP)",
455 };
456
457 /*
458  * fd.io coding-style-patch-verification: ON
459  *
460  * Local Variables:
461  * eval: (c-set-style "gnu")
462  * End:
463  */