19cd2d7a5SSteven/*
29cd2d7a5SSteven * Copyright (c) 2017 Cisco and/or its affiliates.
39cd2d7a5SSteven * Licensed under the Apache License, Version 2.0 (the "License");
49cd2d7a5SSteven * you may not use this file except in compliance with the License.
59cd2d7a5SSteven * You may obtain a copy of the License at:
69cd2d7a5SSteven *
79cd2d7a5SSteven *     http://www.apache.org/licenses/LICENSE-2.0
89cd2d7a5SSteven *
99cd2d7a5SSteven * Unless required by applicable law or agreed to in writing, software
109cd2d7a5SSteven * distributed under the License is distributed on an "AS IS" BASIS,
119cd2d7a5SSteven * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
129cd2d7a5SSteven * See the License for the specific language governing permissions and
139cd2d7a5SSteven * limitations under the License.
149cd2d7a5SSteven */
159cd2d7a5SSteven#ifndef __included_lacp_node_h__
169cd2d7a5SSteven#define __included_lacp_node_h__
179cd2d7a5SSteven
189cd2d7a5SSteven#include <vlib/vlib.h>
199cd2d7a5SSteven#include <vlib/unix/unix.h>
209cd2d7a5SSteven#include <vppinfra/format.h>
219cd2d7a5SSteven#include <vppinfra/hash.h>
229cd2d7a5SSteven#include <lacp/protocol.h>
239cd2d7a5SSteven#include <lacp/rx_machine.h>
249cd2d7a5SSteven#include <lacp/tx_machine.h>
259cd2d7a5SSteven#include <lacp/mux_machine.h>
269cd2d7a5SSteven#include <lacp/ptx_machine.h>
279cd2d7a5SSteven
289cd2d7a5SSteventypedef enum
299cd2d7a5SSteven{
309cd2d7a5SSteven  LACP_PACKET_TEMPLATE_ETHERNET,
319cd2d7a5SSteven  LACP_N_PACKET_TEMPLATES,
329cd2d7a5SSteven} lacp_packet_template_id_t;
339cd2d7a5SSteven
349cd2d7a5SSteventypedef enum
359cd2d7a5SSteven{
369cd2d7a5SSteven  MARKER_PACKET_TEMPLATE_ETHERNET,
379cd2d7a5SSteven  MARKER_N_PACKET_TEMPLATES,
389cd2d7a5SSteven} marker_packet_template_id_t;
399cd2d7a5SSteven
4049ee6844SBenoît Gannetypedef enum
419cd2d7a5SSteven{
429cd2d7a5SSteven  LACP_PROCESS_EVENT_START = 1,
439cd2d7a5SSteven  LACP_PROCESS_EVENT_STOP = 2,
449cd2d7a5SSteven} lacp_process_event_t;
459cd2d7a5SSteven
469cd2d7a5SSteven#define LACP_DBG(n, args...)			\
479cd2d7a5SSteven  {						\
489cd2d7a5SSteven    lacp_main_t *_lm = &lacp_main;              \
499cd2d7a5SSteven    if (_lm->debug || n->debug)			\
509cd2d7a5SSteven      clib_warning (args);			\
519cd2d7a5SSteven  }
529cd2d7a5SSteven
539cd2d7a5SSteven#define LACP_DBG2(n, e, s, m, t)   		  \
549cd2d7a5SSteven  {						  \
559cd2d7a5SSteven    lacp_main_t *_lm = &lacp_main;                \
569cd2d7a5SSteven    if ((m)->debug && (_lm->debug || (n)->debug)) \
579cd2d7a5SSteven      (*m->debug)(n, e, s, t);			  \
589cd2d7a5SSteven  }
599cd2d7a5SSteven
609cd2d7a5SSteven/* Packet counters */
619cd2d7a5SSteven#define foreach_lacp_error                                               \
629cd2d7a5SSteven_ (NONE, "good lacp packets -- consumed")	                         \
639cd2d7a5SSteven_ (CACHE_HIT, "good lacp packets -- cache hit")                          \
649cd2d7a5SSteven_ (UNSUPPORTED, "unsupported slow protocol packets")                     \
659cd2d7a5SSteven_ (TOO_SMALL, "bad lacp packets -- packet too small")                    \
669cd2d7a5SSteven_ (BAD_TLV, "bad lacp packets -- bad TLV length")                        \
679cd2d7a5SSteven_ (DISABLED, "lacp packets received on disabled interfaces")
689cd2d7a5SSteven
699cd2d7a5SSteventypedef enum
709cd2d7a5SSteven{
719cd2d7a5SSteven#define _(sym,str) LACP_ERROR_##sym,
729cd2d7a5SSteven  foreach_lacp_error
739cd2d7a5SSteven#undef _
749cd2d7a5SSteven    LACP_N_ERROR,
759cd2d7a5SSteven} lacp_error_t;
769cd2d7a5SSteven
774168c4d9SSteven Luong#define SECS_IN_A_DAY 86400.0
784168c4d9SSteven Luong
799cd2d7a5SSteven/* lacp packet trace capture */
809cd2d7a5SSteventypedef struct
819cd2d7a5SSteven{
82b2ffc697SSteven  u32 sw_if_index;
839cd2d7a5SSteven  u32 len;
849cd2d7a5SSteven  union
859cd2d7a5SSteven  {
869cd2d7a5SSteven    marker_pdu_t marker;
879cd2d7a5SSteven    lacp_pdu_t lacpdu;
889cd2d7a5SSteven  } pkt;
899cd2d7a5SSteven} lacp_input_trace_t;
909cd2d7a5SSteven
919cd2d7a5SSteven/** LACP interface details struct */
929cd2d7a5SSteventypedef struct
939cd2d7a5SSteven{
949cd2d7a5SSteven  u32 sw_if_index;
959cd2d7a5SSteven  u8 interface_name[64];
969cd2d7a5SSteven  u32 rx_state;
979cd2d7a5SSteven  u32 tx_state;
989cd2d7a5SSteven  u32 mux_state;
999cd2d7a5SSteven  u32 ptx_state;
1009cd2d7a5SSteven  u8 bond_interface_name[64];
1019cd2d7a5SSteven  u16 actor_system_priority;
1029cd2d7a5SSteven  u8 actor_system[6];
1039cd2d7a5SSteven  u16 actor_key;
1049cd2d7a5SSteven  u16 actor_port_priority;
1059cd2d7a5SSteven  u16 actor_port_number;
1069cd2d7a5SSteven  u8 actor_state;
1079cd2d7a5SSteven  u16 partner_system_priority;
1089cd2d7a5SSteven  u8 partner_system[6];
1099cd2d7a5SSteven  u16 partner_key;
1109cd2d7a5SSteven  u16 partner_port_priority;
1119cd2d7a5SSteven  u16 partner_port_number;
1129cd2d7a5SSteven  u8 partner_state;
1139cd2d7a5SSteven} lacp_interface_details_t;
1149cd2d7a5SSteven
1159cd2d7a5SSteventypedef struct
1169cd2d7a5SSteven{
1179cd2d7a5SSteven  /** API message ID base */
1189cd2d7a5SSteven  u16 msg_id_base;
1199cd2d7a5SSteven
1209cd2d7a5SSteven  /* convenience variables */
1219cd2d7a5SSteven  vlib_main_t *vlib_main;
1229cd2d7a5SSteven  vnet_main_t *vnet_main;
1239cd2d7a5SSteven
1249cd2d7a5SSteven  /* Background process node index */
1259cd2d7a5SSteven  u32 lacp_process_node_index;
1269cd2d7a5SSteven
1279cd2d7a5SSteven  /* Packet templates for different encap types */
1289cd2d7a5SSteven  vlib_packet_template_t packet_templates[LACP_N_PACKET_TEMPLATES];
1299cd2d7a5SSteven
1309cd2d7a5SSteven  /* Packet templates for different encap types */
1319cd2d7a5SSteven  vlib_packet_template_t marker_packet_templates[MARKER_N_PACKET_TEMPLATES];
1329cd2d7a5SSteven
1339cd2d7a5SSteven  /* LACP interface count */
1344168c4d9SSteven Luong  volatile u32 lacp_int;
1359cd2d7a5SSteven
1369cd2d7a5SSteven  /* debug is on or off */
1379cd2d7a5SSteven  u8 debug;
1389cd2d7a5SSteven} lacp_main_t;
1399cd2d7a5SSteven
1409cd2d7a5SStevenextern lacp_state_struct lacp_state_array[];
1419cd2d7a5SStevenextern lacp_main_t lacp_main;
1429cd2d7a5SSteven
1434168c4d9SSteven Luongvoid lacp_create_periodic_process (void);
1449cd2d7a5SStevenclib_error_t *lacp_plugin_api_hookup (vlib_main_t * vm);
1459cd2d7a5SStevenint lacp_dump_ifs (lacp_interface_details_t ** out_bondids);
1469cd2d7a5SStevenlacp_error_t lacp_input (vlib_main_t * vm, vlib_buffer_t * b0, u32 bi0);
1479cd2d7a5SStevenvoid lacp_periodic (vlib_main_t * vm);
1489cd2d7a5SStevenu8 *lacp_input_format_trace (u8 * s, va_list * args);
1499cd2d7a5SStevenvoid lacp_init_neighbor (slave_if_t * sif, u8 * hw_address,
1509cd2d7a5SSteven			 u16 port_number, u32 group);
1519cd2d7a5SStevenvoid lacp_init_state_machines (vlib_main_t * vm, slave_if_t * sif);
1529cd2d7a5SStevenvoid lacp_init_rx_machine (vlib_main_t * vm, slave_if_t * sif);
1539cd2d7a5SStevenvoid lacp_init_tx_machine (vlib_main_t * vm, slave_if_t * sif);
1549cd2d7a5SStevenvoid lacp_init_ptx_machine (vlib_main_t * vm, slave_if_t * sif);
1559cd2d7a5SStevenvoid lacp_init_mux_machine (vlib_main_t * vm, slave_if_t * sif);
1569cd2d7a5SStevenvoid lacp_selection_logic (vlib_main_t * vm, slave_if_t * sif);
1579cd2d7a5SStevenvoid lacp_send_lacp_pdu (vlib_main_t * vm, slave_if_t * sif);
1589cd2d7a5SSteven
1599cd2d7a5SStevenstatic inline void
1609cd2d7a5SStevenlacp_stop_timer (f64 * timer)
1619cd2d7a5SSteven{
1629cd2d7a5SSteven  *timer = 0.0;
1639cd2d7a5SSteven}
1649cd2d7a5SSteven
1659cd2d7a5SStevenstatic inline u8
1669cd2d7a5SStevenlacp_timer_is_running (f64 timer)
1679cd2d7a5SSteven{
1689cd2d7a5SSteven  return (timer != 0.0);
1699cd2d7a5SSteven}
1709cd2d7a5SSteven
1719cd2d7a5SStevenstatic inline u8
1729cd2d7a5SStevenlacp_timer_is_expired (vlib_main_t * vm, f64 timer)
1739cd2d7a5SSteven{
1749cd2d7a5SSteven  f64 now = vlib_time_now (vm);
1759cd2d7a5SSteven
1769cd2d7a5SSteven  return (now >= timer);
1779cd2d7a5SSteven}
1789cd2d7a5SSteven
1799cd2d7a5SStevenstatic inline u8 *
1809cd2d7a5SStevenformat_rx_sm_state (u8 * s, va_list * args)
1819cd2d7a5SSteven{
1829cd2d7a5SSteven  lacp_state_struct lacp_rx_sm_state_array[] = {
1839cd2d7a5SSteven#define _(b, s, n) {.bit = b, .str = #s, },
1849cd2d7a5SSteven    foreach_lacp_rx_sm_state
1859cd2d7a5SSteven#undef _
1869cd2d7a5SSteven    {.str = NULL}
1879cd2d7a5SSteven  };
1889cd2d7a5SSteven  int state = va_arg (*args, int);
18991c51291SZhiyong Yang  lacp_state_struct *state_entry = lacp_rx_sm_state_array;
1909cd2d7a5SSteven
1919cd2d7a5SSteven  if (state >= (sizeof (lacp_rx_sm_state_array) / sizeof (*state_entry)))
1929cd2d7a5SSteven    s = format (s, "Bad state %d", state);
1939cd2d7a5SSteven  else
1949cd2d7a5SSteven    s = format (s, "%s", state_entry[state].str);
1959cd2d7a5SSteven
1969cd2d7a5SSteven  return s;
1979cd2d7a5SSteven}
1989cd2d7a5SSteven
1999cd2d7a5SStevenstatic inline u8 *
2009cd2d7a5SStevenformat_tx_sm_state (u8 * s, va_list * args)
2019cd2d7a5SSteven{
2029cd2d7a5SSteven  lacp_state_struct lacp_tx_sm_state_array[] = {
2039cd2d7a5SSteven#define _(b, s, n) {.bit = b, .str = #s, },
2049cd2d7a5SSteven    foreach_lacp_tx_sm_state
2059cd2d7a5SSteven#undef _
2069cd2d7a5SSteven    {.str = NULL}
2079cd2d7a5SSteven  };
2089cd2d7a5SSteven  int state = va_arg (*args, int);
20991c51291SZhiyong Yang  lacp_state_struct *state_entry = lacp_tx_sm_state_array;
2109cd2d7a5SSteven
2119cd2d7a5SSteven  if (state >= (sizeof (lacp_tx_sm_state_array) / sizeof (*state_entry)))
2129cd2d7a5SSteven    s = format (s, "Bad state %d", state);
2139cd2d7a5SSteven  else
2149cd2d7a5SSteven    s = format (s, "%s", state_entry[state].str);
2159cd2d7a5SSteven
2169cd2d7a5SSteven  return s;
2179cd2d7a5SSteven}
2189cd2d7a5SSteven
2199cd2d7a5SStevenstatic inline u8 *
2209cd2d7a5SStevenformat_mux_sm_state (u8 * s, va_list * args)
2219cd2d7a5SSteven{
2229cd2d7a5SSteven  lacp_state_struct lacp_mux_sm_state_array[] = {
2239cd2d7a5SSteven#define _(b, s, n) {.bit = b, .str = #s, },
2249cd2d7a5SSteven    foreach_lacp_mux_sm_state
2259cd2d7a5SSteven#undef _
2269cd2d7a5SSteven    {.str = NULL}
2279cd2d7a5SSteven  };
2289cd2d7a5SSteven  int state = va_arg (*args, int);
22991c51291SZhiyong Yang  lacp_state_struct *state_entry = lacp_mux_sm_state_array;
2309cd2d7a5SSteven
2319cd2d7a5SSteven  if (state >= (sizeof (lacp_mux_sm_state_array) / sizeof (*state_entry)))
2329cd2d7a5SSteven    s = format (s, "Bad state %d", state);
2339cd2d7a5SSteven  else
2349cd2d7a5SSteven    s = format (s, "%s", state_entry[state].str);
2359cd2d7a5SSteven
2369cd2d7a5SSteven  return s;
2379cd2d7a5SSteven}
2389cd2d7a5SSteven
2399cd2d7a5SStevenstatic inline u8 *
2409cd2d7a5SStevenformat_ptx_sm_state (u8 * s, va_list * args)
2419cd2d7a5SSteven{
2429cd2d7a5SSteven  lacp_state_struct lacp_ptx_sm_state_array[] = {
2439cd2d7a5SSteven#define _(b, s, n) {.bit = b, .str = #s, },
2449cd2d7a5SSteven    foreach_lacp_ptx_sm_state
2459cd2d7a5SSteven#undef _
2469cd2d7a5SSteven    {.str = NULL}
2479cd2d7a5SSteven  };
2489cd2d7a5SSteven  int state = va_arg (*args, int);
24991c51291SZhiyong Yang  lacp_state_struct *state_entry = lacp_ptx_sm_state_array;
2509cd2d7a5SSteven
2519cd2d7a5SSteven  if (state >= (sizeof (lacp_ptx_sm_state_array) / sizeof (*state_entry)))
2529cd2d7a5SSteven    s = format (s, "Bad state %d", state);
2539cd2d7a5SSteven  else
2549cd2d7a5SSteven    s = format (s, "%s", state_entry[state].str);
2559cd2d7a5SSteven
2569cd2d7a5SSteven  return s;
2579cd2d7a5SSteven}
2589cd2d7a5SSteven
2599cd2d7a5SStevenstatic inline int
2609cd2d7a5SStevenlacp_bit_test (u8 val, u8 bit)
2619cd2d7a5SSteven{
2629cd2d7a5SSteven  if (val & (1 << bit))
2639cd2d7a5SSteven    return 1;
2649cd2d7a5SSteven  else
2659cd2d7a5SSteven    return 0;
2669cd2d7a5SSteven}
2679cd2d7a5SSteven
2689cd2d7a5SSteven#endif /* __included_lacp_node_h__ */
2699cd2d7a5SSteven
2709cd2d7a5SSteven/*
2719cd2d7a5SSteven * fd.io coding-style-patch-verification: ON
2729cd2d7a5SSteven *
2739cd2d7a5SSteven * Local Variables:
2749cd2d7a5SSteven * eval: (c-set-style "gnu")
2759cd2d7a5SSteven * End:
2769cd2d7a5SSteven */
277