misc: api move continued
[vpp.git] / src / plugins / tracedump / graph_test.c
1 /*
2  * Copyright (c) 2020 cisco
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 <vat/vat.h>
17 #include <vlibapi/api.h>
18 #include <vlibmemory/api.h>
19 #include <vppinfra/error.h>
20 #include <vppinfra/time_range.h>
21 #include <vnet/ethernet/ethernet.h>
22 #include <vpp-api/client/stat_client.h>
23
24 #define __plugin_msg_base graph_test_main.msg_id_base
25 #include <vlibapi/vat_helper_macros.h>
26
27 #include <vnet/format_fns.h>
28 #include <tracedump/graph.api_enum.h>
29 #include <tracedump/graph.api_types.h>
30 #include <vlibmemory/vlib.api_types.h>
31
32 typedef struct
33 {
34   u16 msg_id_base;
35   u32 ping_id;
36   vat_main_t *vat_main;
37 } graph_test_main_t;
38
39 graph_test_main_t graph_test_main;
40
41
42 uword
43 api_unformat_node_index (unformat_input_t * input, va_list * args)
44 {
45   u32 *result = va_arg (*args, u32 *);
46
47   return unformat (input, "%u", result);
48 }
49
50
51 static void
52 vl_api_graph_node_get_reply_t_handler (vl_api_graph_node_get_reply_t * mp)
53 {
54   vat_main_t *vam = &vat_main;
55
56   clib_warning ("Next node index: %u\n", mp->cursor);
57   vam->result_ready = 1;
58 }
59
60 int
61 api_graph_node_get (vat_main_t * vam)
62 {
63   graph_test_main_t *gtm = &graph_test_main;
64   unformat_input_t *i = vam->input;
65   vl_api_graph_node_get_t *mp;
66   vl_api_control_ping_t *mp_ping;
67   u32 node_index;
68   char *node_name;
69   u32 flags;
70   bool want_arcs;
71
72   if (vam->json_output)
73     {
74       clib_warning ("JSON output not supported for graph_node_get");
75       return -99;
76     }
77
78   node_index = ~0;
79   node_name = 0;
80   flags = 0;
81   want_arcs = false;
82
83   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
84     {
85       if (unformat (i, "node_index %u", &node_index))
86         ;
87       else if (unformat (i, "node_name %s", &node_name))
88         ;
89       else if (unformat (i, "want_arcs"))
90         want_arcs = true;
91       else if (unformat (i, "trace_supported"))
92         flags |= NODE_FLAG_TRACE_SUPPORTED;
93       else if (unformat (i, "input"))
94         flags |= NODE_FLAG_TRACE_SUPPORTED;
95       else if (unformat (i, "drop"))
96         flags |= NODE_FLAG_IS_DROP;
97       else if (unformat (i, "ouptput"))
98         flags |= NODE_FLAG_IS_OUTPUT;
99       else if (unformat (i, "punt"))
100         flags |= NODE_FLAG_IS_PUNT;
101       else if (unformat (i, "handoff"))
102         flags |= NODE_FLAG_IS_HANDOFF;
103       else if (unformat (i, "no_free"))
104         flags |= NODE_FLAG_FRAME_NO_FREE_AFTER_DISPATCH;
105       else if (unformat (i, "polling"))
106         flags |= NODE_FLAG_SWITCH_FROM_INTERRUPT_TO_POLLING_MODE;
107       else if (unformat (i, "interrupt"))
108         flags |= NODE_FLAG_SWITCH_FROM_POLLING_TO_INTERRUPT_MODE;
109       else
110         {
111           clib_warning ("Unknown input: %U\n", format_unformat_error, i);
112           return -99;
113         }
114     }
115
116   M (GRAPH_NODE_GET, mp);
117   mp->index = htonl (node_index);
118   mp->flags = htonl (flags);
119   mp->want_arcs = want_arcs;
120
121   if (node_name && node_name[0])
122     clib_strncpy ((char *) mp->name, node_name, sizeof (mp->name) - 1);
123
124   int ret = 0;
125   S (mp);
126
127   if (!gtm->ping_id)
128     gtm->ping_id =
129       vl_msg_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC));
130
131   mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping));
132   mp_ping->_vl_msg_id = htons (gtm->ping_id);
133   mp_ping->client_index = vam->my_client_index;
134
135   S (mp_ping);
136   W (ret);
137
138   return ret;
139 }
140
141 void
142 vl_api_graph_node_details_t_handler (vl_api_graph_node_details_t * mp)
143 {
144   vat_main_t *vam = &vat_main;
145   u32 n_arcs;
146   int i;
147
148   fformat (vam->ofp,
149            "Node: %s  Index:%d  Flags:0x%x\n",
150            mp->name, ntohl (mp->index), ntohl (mp->flags));
151
152   n_arcs = ntohl (mp->n_arcs);
153   for (i = 0; i < n_arcs; ++i)
154     {
155       u32 node_index = ntohl (mp->arcs_out[i]);
156       fformat (vam->ofp, "    next: %d\n", node_index);
157     }
158 }
159
160 void
161 vl_api_graph_node_details_t_handler_json (vl_api_graph_node_details_t * mp)
162 {
163   clib_error ("graph_node_details JSON not supported");
164 }
165
166 /* Override generated plugin register symbol */
167 #define vat_plugin_register graph_test_vat_plugin_register
168 #include <tracedump/graph.api_test.c>
169
170 static clib_error_t *
171 graph_api_hookup_shim (vlib_main_t * vm)
172 {
173   graph_test_vat_plugin_register (&vat_main);
174   return 0;
175 }
176
177 VLIB_API_INIT_FUNCTION (graph_api_hookup_shim);
178
179 /*
180  * fd.io coding-style-patch-verification: ON
181  *
182  * Local Variables:
183  * eval: (c-set-style "gnu")
184  * End:
185  */