Initial commit of vpp code.
[vpp.git] / vpp-api-test / vat / main.c
1 /*
2  * Copyright (c) 2015 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 #include "vat.h"
16 #include "plugin.h"
17
18 vat_main_t vat_main;
19
20 int connect_to_vpe(char *name)
21 {
22     vat_main_t * vam = &vat_main;
23     api_main_t * am = &api_main;
24
25     if (vl_client_connect_to_vlib("/vpe-api", name, 32) < 0)
26         return -1;
27     
28     vam->vl_input_queue = am->shmem_hdr->vl_input_queue;
29     vam->my_client_index = am->my_client_index;
30
31     return 0;
32 }
33
34 void vlib_cli_output(struct vlib_main_t * vm, char * fmt, ...)
35 { clib_warning ("BUG");} 
36
37
38 static u8 * format_api_error (u8 * s, va_list * args)
39 {
40     vat_main_t * vam = va_arg (*args, vat_main_t *);
41     i32 error = va_arg (*args, u32);
42     uword * p;
43
44     p = hash_get (vam->error_string_by_error_number, -error);
45
46     if (p)
47         s = format (s, "%s", p[0]);
48     else
49         s = format (s, "%d", error);
50     return s;
51 }
52
53 void do_one_file (vat_main_t * vam)
54 {
55     int rv;
56     int (*fp)(vat_main_t *vam);
57     int arg_len;
58     unformat_input_t _input;
59     u8 *cmdp, *argsp;
60     uword * p;
61     u8 * this_cmd = 0;
62
63     vam->input = &_input;
64
65     /* Used by the "quit" command handler */
66     if (setjmp (vam->jump_buf) != 0)
67         return;
68     
69     while (1) {
70         if (vam->ifp == stdin) {
71             if (vam->exec_mode == 0)
72                 rv = write (1, "vat# ", 5);
73             else
74                 rv = write (1, "exec# ", 6);
75         }
76             
77         _vec_len(vam->inbuf) = 4096;
78             
79         if (fgets ((char *)vam->inbuf, vec_len(vam->inbuf), vam->ifp) == 0)
80             break;
81             
82         vam->input_line_number ++;
83
84         vec_free (this_cmd);
85
86         this_cmd = (u8 *) clib_macro_eval (&vam->macro_main, (char *)vam->inbuf,
87                                            1 /* complain */);
88             
89         if (vam->exec_mode == 0) {
90             /* Split input into cmd + args */
91             cmdp = this_cmd;
92         
93             while (cmdp < (this_cmd + vec_len(this_cmd))) {
94                 if (*cmdp == ' ' || *cmdp == '\t' || *cmdp == '\n') {
95                     cmdp++;
96                 } else
97                     break;
98             }
99             argsp = cmdp;
100             while (argsp < (this_cmd + vec_len(this_cmd))) {
101                 if (*argsp != ' ' && *argsp != '\t' && *argsp != '\n') {
102                     argsp++;
103                 } else
104                     break;
105             }
106             *argsp++ = 0;
107             while (argsp < (this_cmd + vec_len(this_cmd))) {
108                 if (*argsp == ' ' || *argsp == '\t' || *argsp == '\n') {
109                     argsp++;
110                 } else
111                     break;
112             }
113             
114             
115             /* Blank input line? */
116             if (*cmdp == 0)
117                 continue;
118             
119             p = hash_get_mem (vam->function_by_name, cmdp);
120             if (p == 0) {
121                 errmsg ("'%s': function not found\n", cmdp);
122                 continue;
123             }
124             
125             arg_len = strlen((char *) argsp);
126             
127             unformat_init_string (vam->input, (char *)argsp, arg_len);
128             fp = (void *)p[0];
129         } else {
130             unformat_init_string (vam->input, (char *) this_cmd, 
131                                   strlen((char *) this_cmd));
132             cmdp = this_cmd;
133             fp = exec;
134         }
135
136         rv = (*fp)(vam);
137         if (rv < 0)
138             errmsg ("%s error: %U\n", cmdp, format_api_error, vam, rv);
139         unformat_free (vam->input);
140
141         if (vam->regenerate_interface_table) {
142             vam->regenerate_interface_table = 0;
143             api_sw_interface_dump (vam);
144         }
145     }
146 }
147
148 static void init_error_string_table (vat_main_t * vam)
149 {
150
151     vam->error_string_by_error_number = hash_create (0, sizeof(uword));
152
153 #define _(n,v,s) hash_set (vam->error_string_by_error_number, -v, s);
154     foreach_vnet_api_error;
155 #undef _
156
157     hash_set (vam->error_string_by_error_number, 99, "Misc");
158 }
159
160 static i8 *eval_current_file (macro_main_t *mm, i32 complain)
161 {
162     vat_main_t * vam = &vat_main;
163     return ((i8 *) format (0, "%s%c", vam->current_file, 0));
164 }
165
166 static i8 *eval_current_line (macro_main_t *mm, i32 complain)
167 {
168     vat_main_t * vam = &vat_main;
169     return ((i8 *) format (0, "%d%c", vam->input_line_number, 0));
170 }
171
172
173 int main (int argc, char ** argv)
174 {
175     vat_main_t * vam = &vat_main;
176     unformat_input_t _argv, *a = &_argv;
177     u8 **input_files = 0;
178     u8 *output_file = 0;
179     u8 *this_input_file;
180     u8 interactive = 1;
181     u8 json_output = 0;
182     u8 * heap;
183     mheap_t * h;
184     int i;
185
186     clib_mem_init (0, 128<<20);
187
188     heap = clib_mem_get_per_cpu_heap();
189     h = mheap_header (heap);
190       
191     /* make the main heap thread-safe */
192     h->flags |= MHEAP_FLAG_THREAD_SAFE;
193
194     clib_macro_init (&vam->macro_main);
195     clib_macro_add_builtin (&vam->macro_main, "current_file", eval_current_file);
196     clib_macro_add_builtin (&vam->macro_main, "current_line", eval_current_line);
197
198     init_error_string_table (vam);
199
200     unformat_init_command_line (a, argv);
201
202     while (unformat_check_input(a) != UNFORMAT_END_OF_INPUT) {
203         if (unformat (a, "in %s", &this_input_file))
204             vec_add1 (input_files, this_input_file);
205         else if (unformat (a, "out %s", &output_file))
206             ;
207         else if (unformat (a, "script"))
208             interactive = 0;
209         else if (unformat (a, "json"))
210             json_output = 1;
211         else if (unformat (a, "plugin_path %s", (u8 *)&vat_plugin_path))
212             vec_add1 (vat_plugin_path, 0);
213         else if (unformat (a, "plugin_name_filter %s", 
214                            (u8 *)&vat_plugin_name_filter))
215             vec_add1 (vat_plugin_name_filter, 0);
216         else {
217             fformat (stderr, 
218                      "%s: usage [in <f1> ... in <fn>] [out <fn>] [script] [json]\n");
219             exit (1);
220         }
221     }
222
223     if (output_file)
224         vam->ofp = fopen ((char *) output_file, "w");
225     else
226         vam->ofp = stdout;
227
228     if (vam->ofp == NULL) {
229         fformat (stderr, "Couldn't open output file %s\n",
230                  output_file ? (char *) output_file : "stdout");
231         exit (1);
232     }
233
234     clib_time_init (&vam->clib_time);
235
236     vat_api_hookup(vam);
237     vat_plugin_api_reference();
238
239     if (connect_to_vpe("vpe_api_test") < 0) {
240         svm_region_exit();
241         fformat (stderr, "Couldn't connect to vpe, exiting...\n");
242         exit (1);
243     }
244
245     vam->json_output = json_output;
246
247     if (!json_output) {
248         api_sw_interface_dump (vam);
249     }
250
251     vec_validate (vam->inbuf, 4096);
252
253     vam->current_file = (u8 *) "plugin-init";
254     vat_plugin_init (vam);
255
256     for (i = 0; i < vec_len (input_files); i++) {
257         vam->ifp = fopen ((char *) input_files[i], "r");
258         if (vam->ifp == NULL) {
259             fformat (stderr, "Couldn't open input file %s\n",
260                      input_files[i]);
261             continue;
262         }
263         vam->current_file = input_files[i];
264         vam->input_line_number = 0;
265         do_one_file (vam);
266         fclose (vam->ifp);
267     }
268
269     if (output_file)
270         fclose (vam->ofp);
271
272     if (interactive) {
273         vam->ifp = stdin;
274         vam->ofp = stdout;
275         vam->current_file = (u8 *) "interactive";
276         do_one_file (vam);
277         fclose(vam->ifp);
278     }
279
280     vl_client_disconnect_from_vlib();
281     exit (0);
282 }