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#define _GNU_SOURCE
17#include <vnet/bonding/node.h>
18#include <lacp/node.h>
19
20int
21lacp_dump_ifs (lacp_interface_details_t ** out_lacpifs)
22{
23  vnet_main_t *vnm = vnet_get_main ();
24  bond_main_t *bm = &bond_main;
25  slave_if_t *sif;
26  bond_if_t *bif;
27  vnet_hw_interface_t *hi;
28  lacp_interface_details_t *r_lacpifs = NULL;
29  lacp_interface_details_t *lacpif = NULL;
30
31  /* *INDENT-OFF* */
32  pool_foreach (sif, bm->neighbors,
33    if (sif->lacp_enabled == 0)
34      continue;
35    vec_add2(r_lacpifs, lacpif, 1);
36    clib_memset (lacpif, 0, sizeof (*lacpif));
37    lacpif->sw_if_index = sif->sw_if_index;
38    hi = vnet_get_hw_interface (vnm, sif->hw_if_index);
39    clib_memcpy(lacpif->interface_name, hi->name,
40                MIN (ARRAY_LEN (lacpif->interface_name) - 1,
41                     strlen ((const char *) hi->name)));
42    bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
43    hi = vnet_get_hw_interface (vnm, bif->hw_if_index);
44    clib_memcpy(lacpif->bond_interface_name, hi->name,
45                MIN (ARRAY_LEN (lacpif->bond_interface_name) - 1,
46                     strlen ((const char *) hi->name)));
47    clib_memcpy (lacpif->actor_system, sif->actor.system, 6);
48    lacpif->actor_system_priority = sif->actor.system_priority;
49    lacpif->actor_key = sif->actor.key;
50    lacpif->actor_port_priority = sif->actor.port_priority;
51    lacpif->actor_port_number = sif->actor.port_number;
52    lacpif->actor_state = sif->actor.state;
53    clib_memcpy (lacpif->partner_system, sif->partner.system, 6);
54    lacpif->partner_system_priority = sif->partner.system_priority;
55    lacpif->partner_key = sif->partner.key;
56    lacpif->partner_port_priority = sif->partner.port_priority;
57    lacpif->partner_port_number = sif->partner.port_number;
58    lacpif->partner_state = sif->partner.state;
59    lacpif->rx_state = sif->rx_state;
60    lacpif->tx_state = sif->tx_state;
61    lacpif->ptx_state = sif->ptx_state;
62    lacpif->mux_state = sif->mux_state;
63  );
64  /* *INDENT-ON* */
65
66  *out_lacpifs = r_lacpifs;
67
68  return 0;
69}
70
71static void
72show_lacp (vlib_main_t * vm, u32 * sw_if_indices)
73{
74  int i;
75  slave_if_t *sif;
76  bond_if_t *bif;
77
78  if (!sw_if_indices)
79    return;
80
81  vlib_cli_output (vm, "%-55s %-32s %-32s", " ", "actor state",
82		   "partner state");
83  vlib_cli_output (vm, "%-25s %-12s %-16s %-31s  %-31s", "interface name",
84		   "sw_if_index", "bond interface",
85		   "exp/def/dis/col/syn/agg/tim/act",
86		   "exp/def/dis/col/syn/agg/tim/act");
87
88  for (i = 0; i < vec_len (sw_if_indices); i++)
89    {
90      sif = bond_get_slave_by_sw_if_index (sw_if_indices[i]);
91      if (!sif || (sif->lacp_enabled == 0))
92	continue;
93      bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
94      vlib_cli_output (vm,
95		       "%-25U %-12d %-16U %3x %3x %3x %3x %3x %3x %3x %3x "
96		       "%4x %3x %3x %3x %3x %3x %3x %3x",
97		       format_vnet_sw_if_index_name, vnet_get_main (),
98		       sif->sw_if_index, sif->sw_if_index,
99		       format_vnet_sw_if_index_name, vnet_get_main (),
100		       bif->sw_if_index, lacp_bit_test (sif->actor.state, 7),
101		       lacp_bit_test (sif->actor.state, 6),
102		       lacp_bit_test (sif->actor.state, 5),
103		       lacp_bit_test (sif->actor.state, 4),
104		       lacp_bit_test (sif->actor.state, 3),
105		       lacp_bit_test (sif->actor.state, 2),
106		       lacp_bit_test (sif->actor.state, 1),
107		       lacp_bit_test (sif->actor.state, 0),
108		       lacp_bit_test (sif->partner.state, 7),
109		       lacp_bit_test (sif->partner.state, 6),
110		       lacp_bit_test (sif->partner.state, 5),
111		       lacp_bit_test (sif->partner.state, 4),
112		       lacp_bit_test (sif->partner.state, 3),
113		       lacp_bit_test (sif->partner.state, 2),
114		       lacp_bit_test (sif->partner.state, 1),
115		       lacp_bit_test (sif->partner.state, 0));
116      vlib_cli_output (vm,
117		       "  LAG ID: "
118		       "[(%04x,%02x-%02x-%02x-%02x-%02x-%02x,%04x,%04x,%04x), "
119		       "(%04x,%02x-%02x-%02x-%02x-%02x-%02x,%04x,%04x,%04x)]",
120		       ntohs (sif->actor.system_priority),
121		       sif->actor.system[0], sif->actor.system[1],
122		       sif->actor.system[2], sif->actor.system[3],
123		       sif->actor.system[4], sif->actor.system[5],
124		       ntohs (sif->actor.key),
125		       ntohs (sif->actor.port_priority),
126		       ntohs (sif->actor.port_number),
127		       ntohs (sif->partner.system_priority),
128		       sif->partner.system[0], sif->partner.system[1],
129		       sif->partner.system[2], sif->partner.system[3],
130		       sif->partner.system[4], sif->partner.system[5],
131		       ntohs (sif->partner.key),
132		       ntohs (sif->partner.port_priority),
133		       ntohs (sif->partner.port_number));
134      vlib_cli_output (vm,
135		       "  RX-state: %U, TX-state: %U, "
136		       "MUX-state: %U, PTX-state: %U",
137		       format_rx_sm_state, sif->rx_state, format_tx_sm_state,
138		       sif->tx_state, format_mux_sm_state, sif->mux_state,
139		       format_ptx_sm_state, sif->ptx_state);
140    }
141}
142
143static void
144show_lacp_details (vlib_main_t * vm, u32 * sw_if_indices)
145{
146  lacp_main_t *lm = &lacp_main;
147  slave_if_t *sif;
148  lacp_state_struct *state_entry;
149  int i;
150  f64 now;
151
152  vlib_cli_output (vm, "Number of interfaces: %d", lm->lacp_int);
153  if (!sw_if_indices)
154    return;
155
156  now = vlib_time_now (vm);
157  for (i = 0; i < vec_len (sw_if_indices); i++)
158    {
159      sif = bond_get_slave_by_sw_if_index (sw_if_indices[i]);
160      if (!sif || (sif->lacp_enabled == 0))
161	continue;
162      vlib_cli_output (vm, "  %U", format_vnet_sw_if_index_name,
163		       vnet_get_main (), sif->sw_if_index);
164      vlib_cli_output (vm, "    Good LACP PDUs received: %llu",
165		       sif->pdu_received);
166      vlib_cli_output (vm, "    Bad LACP PDUs received: %llu",
167		       sif->bad_pdu_received);
168      vlib_cli_output (vm, "    LACP PDUs sent: %llu", sif->pdu_sent);
169      if (lacp_timer_is_running (sif->last_lacpdu_recd_time))
170	vlib_cli_output (vm,
171			 "    last LACP PDU received: %10.2f seconds ago",
172			 now - sif->last_lacpdu_recd_time);
173      if (lacp_timer_is_running (sif->last_lacpdu_sent_time))
174	vlib_cli_output (vm, "    last LACP PDU sent: %10.2f seconds ago",
175			 now - sif->last_lacpdu_sent_time);
176      vlib_cli_output (vm, "    Good Marker PDUs received: %llu",
177		       sif->marker_pdu_received);
178      vlib_cli_output (vm, "    Bad Marker PDUs received: %llu",
179		       sif->marker_bad_pdu_received);
180      if (lacp_timer_is_running (sif->last_marker_pdu_recd_time))
181	vlib_cli_output (vm,
182			 "    last Marker PDU received: %10.2f seconds ago",
183			 now - sif->last_marker_pdu_recd_time);
184      if (lacp_timer_is_running (sif->last_marker_pdu_sent_time))
185	vlib_cli_output (vm, "    last Marker PDU sent: %10.2f seconds ago",
186			 now - sif->last_marker_pdu_sent_time);
187      vlib_cli_output (vm, "    debug: %d", sif->debug);
188      vlib_cli_output (vm, "    loopback port: %d", sif->loopback_port);
189      vlib_cli_output (vm, "    port_enabled: %d", sif->port_enabled);
190      vlib_cli_output (vm, "    port moved: %d", sif->port_moved);
191      vlib_cli_output (vm, "    ready_n: %d", sif->ready_n);
192      vlib_cli_output (vm, "    ready: %d", sif->ready);
193      vlib_cli_output (vm, "    Actor");
194      vlib_cli_output (vm, "      system: %U",
195		       format_ethernet_address, sif->actor.system);
196      vlib_cli_output (vm, "      system priority: %u",
197		       ntohs (sif->actor.system_priority));
198      vlib_cli_output (vm, "      key: %u", ntohs (sif->actor.key));
199      vlib_cli_output (vm, "      port priority: %u",
200		       ntohs (sif->actor.port_priority));
201      vlib_cli_output (vm, "      port number: %u",
202		       ntohs (sif->actor.port_number));
203      vlib_cli_output (vm, "      state: 0x%x", sif->actor.state);
204
205      state_entry = (lacp_state_struct *) & lacp_state_array;
206      while (state_entry->str)
207	{
208	  if (sif->actor.state & (1 << state_entry->bit))
209	    vlib_cli_output (vm, "        %s (%d)", state_entry->str,
210			     state_entry->bit);
211	  state_entry++;
212	}
213
214      vlib_cli_output (vm, "    Partner");
215      vlib_cli_output (vm, "      system: %U",
216		       format_ethernet_address, sif->partner.system);
217      vlib_cli_output (vm, "      system priority: %u",
218		       ntohs (sif->partner.system_priority));
219      vlib_cli_output (vm, "      key: %u", ntohs (sif->partner.key));
220      vlib_cli_output (vm, "      port priority: %u",
221		       ntohs (sif->partner.port_priority));
222      vlib_cli_output (vm, "      port number: %u",
223		       ntohs (sif->partner.port_number));
224      vlib_cli_output (vm, "      state: 0x%x", sif->partner.state);
225
226      state_entry = (lacp_state_struct *) & lacp_state_array;
227      while (state_entry->str)
228	{
229	  if (sif->partner.state & (1 << state_entry->bit))
230	    vlib_cli_output (vm, "        %s (%d)", state_entry->str,
231			     state_entry->bit);
232	  state_entry++;
233	}
234
235      if (!lacp_timer_is_running (sif->wait_while_timer))
236	vlib_cli_output (vm, "      wait while timer: not running");
237      else
238	vlib_cli_output (vm, "      wait while timer: %10.2f seconds",
239			 sif->wait_while_timer - now);
240      if (!lacp_timer_is_running (sif->current_while_timer))
241	vlib_cli_output (vm, "      current while timer: not running");
242      else
243	vlib_cli_output (vm, "      current while timer: %10.2f seconds",
244			 sif->current_while_timer - now);
245      if (!lacp_timer_is_running (sif->periodic_timer))
246	vlib_cli_output (vm, "      periodic timer: not running");
247      else
248	vlib_cli_output (vm, "      periodic timer: %10.2f seconds",
249			 sif->periodic_timer - now);
250      vlib_cli_output (vm, "    RX-state: %U", format_rx_sm_state,
251		       sif->rx_state);
252      vlib_cli_output (vm, "    TX-state: %U", format_tx_sm_state,
253		       sif->tx_state);
254      vlib_cli_output (vm, "    MUX-state: %U", format_mux_sm_state,
255		       sif->mux_state);
256      vlib_cli_output (vm, "    PTX-state: %U", format_ptx_sm_state,
257		       sif->ptx_state);
258      vlib_cli_output (vm, "\n");
259    }
260}
261
262static clib_error_t *
263show_lacp_fn (vlib_main_t * vm, unformat_input_t * input,
264	      vlib_cli_command_t * cmd)
265{
266  bond_main_t *bm = &bond_main;
267  vnet_main_t *vnm = &vnet_main;
268  slave_if_t *sif;
269  clib_error_t *error = 0;
270  u8 details = 0;
271  u32 sw_if_index, *sw_if_indices = 0;
272
273  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
274    {
275      if (unformat
276	  (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
277	{
278	  sif = bond_get_slave_by_sw_if_index (sw_if_index);
279	  if (!sif)
280	    {
281	      error = clib_error_return (0, "interface is not enslaved");
282	      goto done;
283	    }
284	  vec_add1 (sw_if_indices, sif->sw_if_index);
285	}
286      else if (unformat (input, "details"))
287	details = 1;
288      else
289	{
290	  error = clib_error_return (0, "unknown input `%U'",
291				     format_unformat_error, input);
292	  goto done;
293	}
294    }
295
296  if (vec_len (sw_if_indices) == 0)
297    {
298      pool_foreach (sif, bm->neighbors,
299		    vec_add1 (sw_if_indices, sif->sw_if_index);
300	);
301    }
302
303  if (details)
304    show_lacp_details (vm, sw_if_indices);
305  else
306    show_lacp (vm, sw_if_indices);
307
308done:
309  vec_free (sw_if_indices);
310  return error;
311}
312
313/* *INDENT-OFF* */
314VLIB_CLI_COMMAND (show_lacp_command, static) = {
315  .path = "show lacp",
316  .short_help = "show lacp [<interface>] [details]",
317  .function = show_lacp_fn,
318  .is_mp_safe = 1,
319};
320/* *INDENT-ON* */
321
322static clib_error_t *
323debug_lacp_command_fn (vlib_main_t * vm, unformat_input_t * input,
324		       vlib_cli_command_t * cmd)
325{
326  unformat_input_t _line_input, *line_input = &_line_input;
327  clib_error_t *error = NULL;
328  lacp_main_t *lm = &lacp_main;
329  u8 onoff = 0;
330  u8 input_found = 0;
331  u32 sw_if_index = ~0;
332  slave_if_t *sif;
333  vnet_main_t *vnm = vnet_get_main ();
334
335  /* Get a line of input. */
336  if (!unformat_user (input, unformat_line_input, line_input))
337    return clib_error_return (0, "missing argument");
338
339  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
340    {
341      if (unformat (line_input, "%U",
342		    unformat_vnet_sw_interface, vnm, &sw_if_index))
343	;
344      if (input_found)
345	{
346	  error = clib_error_return (0, "unknown input `%U'",
347				     format_unformat_error, line_input);
348	  goto done;
349	}
350      else if (unformat (line_input, "on"))
351	{
352	  input_found = 1;
353	  onoff = 1;
354	}
355      else if (unformat (line_input, "off"))
356	{
357	  input_found = 1;
358	  onoff = 0;
359	}
360      else
361	{
362	  error = clib_error_return (0, "unknown input `%U'",
363				     format_unformat_error, line_input);
364	  goto done;
365	}
366    }
367
368  if (!input_found)
369    return clib_error_return (0, "must specify on or off");
370
371  if (sw_if_index != ~0)
372    {
373      sif = bond_get_slave_by_sw_if_index (sw_if_index);
374      if (!sif)
375	return (clib_error_return (0, "Please enslave the interface first"));
376      sif->debug = onoff;
377    }
378  else
379    lm->debug = onoff;
380
381done:
382  unformat_free (line_input);
383
384  return error;
385}
386
387/* *INDENT-OFF* */
388VLIB_CLI_COMMAND (debug_lacp_command, static) = {
389    .path = "debug lacp",
390    .short_help = "debug lacp <interface> <on | off>",
391    .function = debug_lacp_command_fn,
392};
393/* *INDENT-ON* */
394
395clib_error_t *
396lacp_cli_init (vlib_main_t * vm)
397{
398  lacp_main_t *lm = &lacp_main;
399
400  lm->vlib_main = vm;
401  lm->vnet_main = vnet_get_main ();
402
403  return 0;
404}
405
406VLIB_INIT_FUNCTION (lacp_cli_init);
407
408/*
409 * fd.io coding-style-patch-verification: ON
410 *
411 * Local Variables:
412 * eval: (c-set-style "gnu")
413 * End:
414 */
415