1/*
2 * lacp VAT support
3 *
4 * Copyright (c) 2017 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include <inttypes.h>
19
20#include <vat/vat.h>
21#include <vlibapi/api.h>
22#include <vlibmemory/api.h>
23
24#include <vppinfra/error.h>
25#include <lacp/node.h>
26
27#define __plugin_msg_base lacp_test_main.msg_id_base
28#include <vlibapi/vat_helper_macros.h>
29
30/* declare message IDs */
31#include <vnet/format_fns.h>
32#include <lacp/lacp.api_enum.h>
33#include <lacp/lacp.api_types.h>
34#include <vpp/api/vpe.api_types.h>
35
36typedef struct
37{
38  /* API message ID base */
39  u16 msg_id_base;
40  u32 ping_id;
41  vat_main_t *vat_main;
42} lacp_test_main_t;
43
44lacp_test_main_t lacp_test_main;
45
46/* lacp-dump API */
47static void vl_api_sw_interface_lacp_details_t_handler
48  (vl_api_sw_interface_lacp_details_t * mp)
49{
50  vat_main_t *vam = &vat_main;
51
52  fformat (vam->ofp,
53	   "%-25s %-12d %-16s %3x %3x %3x %3x %3x %3x %3x %3x "
54	   "%4x %3x %3x %3x %3x %3x %3x %3x\n",
55	   mp->interface_name, ntohl (mp->sw_if_index),
56	   mp->bond_interface_name,
57	   lacp_bit_test (mp->actor_state, 7),
58	   lacp_bit_test (mp->actor_state, 6),
59	   lacp_bit_test (mp->actor_state, 5),
60	   lacp_bit_test (mp->actor_state, 4),
61	   lacp_bit_test (mp->actor_state, 3),
62	   lacp_bit_test (mp->actor_state, 2),
63	   lacp_bit_test (mp->actor_state, 1),
64	   lacp_bit_test (mp->actor_state, 0),
65	   lacp_bit_test (mp->partner_state, 7),
66	   lacp_bit_test (mp->partner_state, 6),
67	   lacp_bit_test (mp->partner_state, 5),
68	   lacp_bit_test (mp->partner_state, 4),
69	   lacp_bit_test (mp->partner_state, 3),
70	   lacp_bit_test (mp->partner_state, 2),
71	   lacp_bit_test (mp->partner_state, 1),
72	   lacp_bit_test (mp->partner_state, 0));
73  fformat (vam->ofp,
74	   "  LAG ID: [(%04x,%02x-%02x-%02x-%02x-%02x-%02x,%04x,%04x,%04x), "
75	   "(%04x,%02x-%02x-%02x-%02x-%02x-%02x,%04x,%04x,%04x)]\n",
76	   ntohs (mp->actor_system_priority), mp->actor_system[0],
77	   mp->actor_system[1], mp->actor_system[2], mp->actor_system[3],
78	   mp->actor_system[4], mp->actor_system[5], ntohs (mp->actor_key),
79	   ntohs (mp->actor_port_priority), ntohs (mp->actor_port_number),
80	   ntohs (mp->partner_system_priority), mp->partner_system[0],
81	   mp->partner_system[1], mp->partner_system[2],
82	   mp->partner_system[3], mp->partner_system[4],
83	   mp->partner_system[5], ntohs (mp->partner_key),
84	   ntohs (mp->partner_port_priority),
85	   ntohs (mp->partner_port_number));
86  fformat (vam->ofp,
87	   "  RX-state: %U, TX-state: %U, MUX-state: %U, PTX-state: %U\n",
88	   format_rx_sm_state, ntohl (mp->rx_state), format_tx_sm_state,
89	   ntohl (mp->tx_state), format_mux_sm_state, ntohl (mp->mux_state),
90	   format_ptx_sm_state, ntohl (mp->ptx_state));
91}
92
93static int
94api_sw_interface_lacp_dump (vat_main_t * vam)
95{
96  lacp_test_main_t *lm = &lacp_test_main;
97  vl_api_sw_interface_lacp_dump_t *mp;
98  vl_api_control_ping_t *mp_ping;
99  int ret;
100
101  if (vam->json_output)
102    {
103      clib_warning ("JSON output not supported for sw_interface_lacp_dump");
104      return -99;
105    }
106
107  fformat (vam->ofp, "%-55s %-32s %-32s\n", " ", "actor state",
108	   "partner state");
109  fformat (vam->ofp, "%-25s %-12s %-16s %-31s  %-31s\n", "interface name",
110	   "sw_if_index", "bond interface", "exp/def/dis/col/syn/agg/tim/act",
111	   "exp/def/dis/col/syn/agg/tim/act");
112
113  /* Get list of lacp interfaces */
114  M (SW_INTERFACE_LACP_DUMP, mp);
115  S (mp);
116
117  /* Use a control ping for synchronization */
118  if (!lm->ping_id)
119    lm->ping_id = vl_msg_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC));
120  mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping));
121  mp_ping->_vl_msg_id = htons (lm->ping_id);
122  mp_ping->client_index = vam->my_client_index;
123
124  fformat (vam->ofp, "Sending ping id=%d\n", lm->ping_id);
125
126  vam->result_ready = 0;
127  S (mp_ping);
128
129  W (ret);
130  return ret;
131}
132
133#include <lacp/lacp.api_test.c>
134
135/*
136 * fd.io coding-style-patch-verification: ON
137 *
138 * Local Variables:
139 * eval: (c-set-style "gnu")
140 * End:
141 */
142