API:support hidden sw interfaces
[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   /* Ensure that vam->inbuf is never NULL */
51   vec_validate (vam->inbuf, 0);
52   init_error_string_table (vam);
53   rv = vat_plugin_init (vam);
54   if (rv)
55     clib_warning ("vat_plugin_init returned %d", rv);
56   return 0;
57 }
58
59 VLIB_INIT_FUNCTION (api_main_init);
60
61 void
62 vat_plugin_hash_create (void)
63 {
64   vat_main_t *vam = &vat_main;
65
66   vam->sw_if_index_by_interface_name = hash_create_string (0, sizeof (uword));
67   vam->function_by_name = hash_create_string (0, sizeof (uword));
68   vam->help_by_name = hash_create_string (0, sizeof (uword));
69 }
70
71 static clib_error_t *
72 api_command_fn (vlib_main_t * vm,
73                 unformat_input_t * input, vlib_cli_command_t * cmd)
74 {
75   vat_main_t *vam = &vat_main;
76   unformat_input_t _input;
77   uword c;
78   u8 *cmdp, *argsp, *this_cmd;
79   uword *p;
80   u32 arg_len;
81   int rv;
82   int (*fp) (vat_main_t *);
83   api_main_t *am = &api_main;
84
85   vam->vl_input_queue = am->shmem_hdr->vl_input_queue;
86
87 #ifdef __COVERITY__
88   /*
89    * Convince Coverity that it's not a NULL pointer...
90    * Done once for real below, since we never vec_free(vam->inbuf);
91    */
92   vec_validate (vam->inbuf, 0);
93 #endif
94
95   vec_reset_length (vam->inbuf);
96   vam->input = &_input;
97
98   while (((c = unformat_get_input (input)) != '\n') &&
99          (c != UNFORMAT_END_OF_INPUT))
100     vec_add1 (vam->inbuf, c);
101
102   /* Add 1 octet's worth of extra space in case there are no args... */
103   vec_add1 (vam->inbuf, 0);
104
105   /*$$$$ reinstall macro evaluator */
106
107   /* Split input into cmd + args */
108   this_cmd = cmdp = vam->inbuf;
109
110   /* Skip leading whitespace */
111   while (cmdp < (this_cmd + vec_len (this_cmd)))
112     {
113       if (*cmdp == ' ' || *cmdp == '\t' || *cmdp == '\n')
114         {
115           cmdp++;
116         }
117       else
118         break;
119     }
120
121   argsp = cmdp;
122
123   /* Advance past the command */
124   while (argsp < (this_cmd + vec_len (this_cmd)))
125     {
126       if (*argsp != ' ' && *argsp != '\t' && *argsp != '\n' && argsp != 0)
127         {
128           argsp++;
129         }
130       else
131         break;
132     }
133   /* NULL terminate the command */
134   *argsp++ = 0;
135
136   while (argsp < (this_cmd + vec_len (this_cmd)))
137     {
138       if (*argsp == ' ' || *argsp == '\t' || *argsp == '\n')
139         {
140           argsp++;
141         }
142       else
143         break;
144     }
145
146   /* Blank input line? */
147   if (*cmdp == 0)
148     return 0;
149
150   p = hash_get_mem (vam->function_by_name, cmdp);
151   if (p == 0)
152     {
153       return clib_error_return (0, "'%s': function not found\n", cmdp);
154     }
155
156   arg_len = strlen ((char *) argsp);
157
158   unformat_init_string (vam->input, (char *) argsp, arg_len);
159   fp = (void *) p[0];
160
161   rv = (*fp) (vam);
162
163   if (rv < 0)
164     {
165       unformat_free (vam->input);
166       return clib_error_return (0,
167                                 "%s error: %U\n", cmdp,
168                                 format_api_error, vam, rv);
169
170     }
171   if (vam->regenerate_interface_table)
172     {
173       vam->regenerate_interface_table = 0;
174       api_sw_interface_dump (vam);
175     }
176   unformat_free (vam->input);
177   return 0;
178 }
179
180 /* *INDENT-OFF* */
181 VLIB_CLI_COMMAND (api_command, static) =
182 {
183   .path = "binary-api",
184   .short_help = "binary-api [help] <name> [<args>]",
185   .function = api_command_fn,
186 };
187 /* *INDENT-ON* */
188
189 void
190 api_cli_output (void *notused, const char *fmt, ...)
191 {
192   va_list va;
193   vat_main_t *vam = &vat_main;
194   vlib_main_t *vm = vam->vlib_main;
195   vlib_process_t *cp = vlib_get_current_process (vm);
196   u8 *s;
197
198   va_start (va, fmt);
199   s = va_format (0, fmt, &va);
200   va_end (va);
201
202   /* Terminate with \n if not present. */
203   if (vec_len (s) > 0 && s[vec_len (s) - 1] != '\n')
204     vec_add1 (s, '\n');
205
206   if ((!cp) || (!cp->output_function))
207     fformat (stdout, "%v", s);
208   else
209     cp->output_function (cp->output_function_arg, s, vec_len (s));
210
211   vec_free (s);
212 }
213
214 u16
215 vl_client_get_first_plugin_msg_id (char *plugin_name)
216 {
217   api_main_t *am = &api_main;
218   vl_api_msg_range_t *rp;
219   uword *p;
220
221   p = hash_get_mem (am->msg_range_by_name, plugin_name);
222   if (p == 0)
223     return ~0;
224
225   rp = vec_elt_at_index (am->msg_ranges, p[0]);
226
227   return (rp->first_msg_id);
228 }
229
230 uword
231 unformat_sw_if_index (unformat_input_t * input, va_list * args)
232 {
233   u32 *result = va_arg (*args, u32 *);
234   vnet_main_t *vnm = vnet_get_main ();
235   u32 sw_if_index = ~0;
236   u8 *if_name;
237   uword *p;
238
239   if (unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
240     {
241       *result = sw_if_index;
242       return 1;
243     }
244   return 0;
245 }
246
247 /*
248  * fd.io coding-style-patch-verification: ON
249  *
250  * Local Variables:
251  * eval: (c-set-style "gnu")
252  * End:
253  */