Missing plugin binary API command fns found after brief search
[vpp.git] / src / vpp / api / api_main.c
1 #include "vat.h"
2
3 vat_main_t vat_main;
4
5 void
6 vat_suspend (vlib_main_t * vm, f64 interval)
7 {
8   vlib_process_suspend (vm, interval);
9 }
10
11 static u8 *
12 format_api_error (u8 * s, va_list * args)
13 {
14   vat_main_t *vam = va_arg (*args, vat_main_t *);
15   i32 error = va_arg (*args, u32);
16   uword *p;
17
18   p = hash_get (vam->error_string_by_error_number, -error);
19
20   if (p)
21     s = format (s, "%s", p[0]);
22   else
23     s = format (s, "%d", error);
24   return s;
25 }
26
27
28 static void
29 init_error_string_table (vat_main_t * vam)
30 {
31
32   vam->error_string_by_error_number = hash_create (0, sizeof (uword));
33
34 #define _(n,v,s) hash_set (vam->error_string_by_error_number, -v, s);
35   foreach_vnet_api_error;
36 #undef _
37
38   hash_set (vam->error_string_by_error_number, 99, "Misc");
39 }
40
41 static clib_error_t *
42 api_main_init (vlib_main_t * vm)
43 {
44   vat_main_t *vam = &vat_main;
45   int rv;
46   int vat_plugin_init (vat_main_t * vam);
47
48   vam->vlib_main = vm;
49   vam->my_client_index = (u32) ~ 0;
50   init_error_string_table (vam);
51   rv = vat_plugin_init (vam);
52   if (rv)
53     clib_warning ("vat_plugin_init returned %d", rv);
54   return 0;
55 }
56
57 VLIB_INIT_FUNCTION (api_main_init);
58
59 void
60 vat_plugin_hash_create (void)
61 {
62   vat_main_t *vam = &vat_main;
63
64   vam->sw_if_index_by_interface_name = hash_create_string (0, sizeof (uword));
65   vam->function_by_name = hash_create_string (0, sizeof (uword));
66   vam->help_by_name = hash_create_string (0, sizeof (uword));
67 }
68
69 static clib_error_t *
70 api_command_fn (vlib_main_t * vm,
71                 unformat_input_t * input, vlib_cli_command_t * cmd)
72 {
73   vat_main_t *vam = &vat_main;
74   unformat_input_t _input;
75   uword c;
76   u8 *cmdp, *argsp, *this_cmd;
77   uword *p;
78   u32 arg_len;
79   int rv;
80   int (*fp) (vat_main_t *);
81   api_main_t *am = &api_main;
82
83   vam->vl_input_queue = am->shmem_hdr->vl_input_queue;
84
85   vec_reset_length (vam->inbuf);
86   vam->input = &_input;
87
88   while (((c = unformat_get_input (input)) != '\n') &&
89          (c != UNFORMAT_END_OF_INPUT))
90     vec_add1 (vam->inbuf, c);
91
92   /* Add 1 octet's worth of extra space in case there are no args... */
93   vec_add1 (vam->inbuf, 0);
94
95   /*$$$$ reinstall macro evaluator */
96
97   /* Split input into cmd + args */
98   this_cmd = cmdp = vam->inbuf;
99
100   /* Skip leading whitespace */
101   while (cmdp < (this_cmd + vec_len (this_cmd)))
102     {
103       if (*cmdp == ' ' || *cmdp == '\t' || *cmdp == '\n')
104         {
105           cmdp++;
106         }
107       else
108         break;
109     }
110
111   argsp = cmdp;
112
113   /* Advance past the command */
114   while (argsp < (this_cmd + vec_len (this_cmd)))
115     {
116       if (*argsp != ' ' && *argsp != '\t' && *argsp != '\n' && argsp != 0)
117         {
118           argsp++;
119         }
120       else
121         break;
122     }
123   /* NULL terminate the command */
124   *argsp++ = 0;
125
126   while (argsp < (this_cmd + vec_len (this_cmd)))
127     {
128       if (*argsp == ' ' || *argsp == '\t' || *argsp == '\n')
129         {
130           argsp++;
131         }
132       else
133         break;
134     }
135
136   /* Blank input line? */
137   if (*cmdp == 0)
138     return 0;
139
140   p = hash_get_mem (vam->function_by_name, cmdp);
141   if (p == 0)
142     {
143       return clib_error_return (0, "'%s': function not found\n", cmdp);
144     }
145
146   arg_len = strlen ((char *) argsp);
147
148   unformat_init_string (vam->input, (char *) argsp, arg_len);
149   fp = (void *) p[0];
150
151   rv = (*fp) (vam);
152
153   if (rv < 0)
154     {
155       unformat_free (vam->input);
156       return clib_error_return (0,
157                                 "%s error: %U\n", cmdp,
158                                 format_api_error, vam, rv);
159
160     }
161   if (vam->regenerate_interface_table)
162     {
163       vam->regenerate_interface_table = 0;
164       api_sw_interface_dump (vam);
165     }
166   unformat_free (vam->input);
167   return 0;
168 }
169
170 /* *INDENT-OFF* */
171 VLIB_CLI_COMMAND (api_command, static) =
172 {
173   .path = "binary-api",
174   .short_help = "binary-api [help] <name> [<args>]",
175   .function = api_command_fn,
176 };
177 /* *INDENT-ON* */
178
179 void
180 api_cli_output (void *notused, const char *fmt, ...)
181 {
182   va_list va;
183   vat_main_t *vam = &vat_main;
184   vlib_main_t *vm = vam->vlib_main;
185   vlib_process_t *cp = vlib_get_current_process (vm);
186   u8 *s;
187
188   va_start (va, fmt);
189   s = va_format (0, fmt, &va);
190   va_end (va);
191
192   /* Terminate with \n if not present. */
193   if (vec_len (s) > 0 && s[vec_len (s) - 1] != '\n')
194     vec_add1 (s, '\n');
195
196   if ((!cp) || (!cp->output_function))
197     fformat (stdout, "%v", s);
198   else
199     cp->output_function (cp->output_function_arg, s, vec_len (s));
200
201   vec_free (s);
202 }
203
204 u16
205 vl_client_get_first_plugin_msg_id (char *plugin_name)
206 {
207   api_main_t *am = &api_main;
208   vl_api_msg_range_t *rp;
209   uword *p;
210
211   p = hash_get_mem (am->msg_range_by_name, plugin_name);
212   if (p == 0)
213     return ~0;
214
215   rp = vec_elt_at_index (am->msg_ranges, p[0]);
216
217   return (rp->first_msg_id);
218 }
219
220 uword
221 unformat_sw_if_index (unformat_input_t * input, va_list * args)
222 {
223   u32 *result = va_arg (*args, u32 *);
224   vnet_main_t *vnm = vnet_get_main ();
225   u32 sw_if_index = ~0;
226   u8 *if_name;
227   uword *p;
228
229   if (unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
230     {
231       *result = sw_if_index;
232       return 1;
233     }
234   return 0;
235 }
236
237 /*
238  * fd.io coding-style-patch-verification: ON
239  *
240  * Local Variables:
241  * eval: (c-set-style "gnu")
242  * End:
243  */