3867289f737be085e6e2f1a7cdd4ce2a6bf2803b
[vpp.git] / src / plugins / nsh / nsh_test.c
1 /*
2  * nsh.c - skeleton vpp-api-test plug-in
3  *
4  * Copyright (c) <current-year> <your-organization>
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 #include <vat/vat.h>
18 #include <vlibapi/api.h>
19 #include <vlibmemory/api.h>
20 #include <vppinfra/error.h>
21 #include <nsh/nsh.h>
22
23 uword unformat_sw_if_index (unformat_input_t * input, va_list * args);
24
25 /* define message IDs */
26 #define vl_msg_id(n,h) n,
27 typedef enum {
28 #include <nsh/nsh.api.h>
29     /* We'll want to know how many messages IDs we need... */
30     VL_MSG_FIRST_AVAILABLE,
31 } vl_msg_id_t;
32 #undef vl_msg_id
33
34 /* define message structures */
35 #define vl_typedefs
36 #include <nsh/nsh.api.h>
37 #undef vl_typedefs
38
39 /* define generated endian-swappers */
40 #define vl_endianfun
41 #include <nsh/nsh.api.h>
42 #undef vl_endianfun
43
44 /* instantiate all the print functions we know about */
45 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
46 #define vl_printfun
47 #include <nsh/nsh.api.h>
48 #undef vl_printfun
49
50 /* Get the API version number */
51 #define vl_api_version(n,v) static u32 api_version=(v);
52 #include <nsh/nsh.api.h>
53 #undef vl_api_version
54
55 #define vl_msg_name_crc_list
56 #include <nsh/nsh.api.h>
57 #undef vl_msg_name_crc_list
58
59
60 typedef struct {
61     /* API message ID base */
62     u16 msg_id_base;
63     vat_main_t *vat_main;
64 } nsh_test_main_t;
65
66 nsh_test_main_t nsh_test_main;
67
68 #define foreach_standard_reply_retval_handler   \
69 _(nsh_add_del_entry_reply)                      \
70 _(nsh_add_del_map_reply)                        \
71
72 #define _(n)                                            \
73     static void vl_api_##n##_t_handler                  \
74     (vl_api_##n##_t * mp)                               \
75     {                                                   \
76         vat_main_t * vam = nsh_test_main.vat_main;   \
77         i32 retval = ntohl(mp->retval);                 \
78         if (vam->async_mode) {                          \
79             vam->async_errors += (retval < 0);          \
80         } else {                                        \
81             vam->retval = retval;                       \
82             vam->result_ready = 1;                      \
83         }                                               \
84     }
85 foreach_standard_reply_retval_handler;
86 #undef _
87
88 /*
89  * Table of message reply handlers, must include boilerplate handlers
90  * we just generated
91  */
92 #define foreach_vpe_api_reply_msg                                       \
93 _(NSH_ADD_DEL_ENTRY_REPLY, nsh_add_del_entry_reply)                     \
94 _(NSH_ENTRY_DETAILS, nsh_entry_details)                                 \
95 _(NSH_ADD_DEL_MAP_REPLY, nsh_add_del_map_reply)                         \
96 _(NSH_MAP_DETAILS, nsh_map_details)
97
98
99 /* M: construct, but don't yet send a message */
100
101 #define M(T,t)                                                  \
102 do {                                                            \
103     vam->result_ready = 0;                                      \
104     mp = vl_msg_api_alloc(sizeof(*mp));                         \
105     memset (mp, 0, sizeof (*mp));                               \
106     mp->_vl_msg_id = ntohs (VL_API_##T + sm->msg_id_base);      \
107     mp->client_index = vam->my_client_index;                    \
108 } while(0);
109
110 #define M2(T,t,n)                                               \
111 do {                                                            \
112     vam->result_ready = 0;                                      \
113     mp = vl_msg_api_alloc(sizeof(*mp)+(n));                     \
114     memset (mp, 0, sizeof (*mp));                               \
115     mp->_vl_msg_id = ntohs (VL_API_##T + sm->msg_id_base);      \
116     mp->client_index = vam->my_client_index;                    \
117 } while(0);
118
119 /* S: send a message */
120 #define S (vl_msg_api_send_shmem (vam->vl_input_queue, (u8 *)&mp))
121
122 /* W: wait for results, with timeout */
123 #define W                                       \
124 do {                                            \
125     timeout = vat_time_now (vam) + 1.0;         \
126                                                 \
127     while (vat_time_now (vam) < timeout) {      \
128         if (vam->result_ready == 1) {           \
129             return (vam->retval);               \
130         }                                       \
131     }                                           \
132     return -99;                                 \
133 } while(0);
134
135 static int api_nsh_add_del_entry (vat_main_t * vam)
136 {
137     nsh_test_main_t * sm = &nsh_test_main;
138     unformat_input_t * line_input = vam->input;
139     f64 timeout;
140     u8 is_add = 1;
141     u8 ver_o_c = 0;
142     u8 length = 0;
143     u8 md_type = 0;
144     u8 next_protocol = 1; /* default: ip4 */
145     u32 nsp;
146     u8 nsp_set = 0;
147     u32 nsi;
148     u8 nsi_set = 0;
149     u32 nsp_nsi;
150     u32 c1 = 0;
151     u32 c2 = 0;
152     u32 c3 = 0;
153     u32 c4 = 0;
154     u32 tmp;
155     vl_api_nsh_add_del_entry_t * mp;
156
157     while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
158       if (unformat (line_input, "del"))
159         is_add = 0;
160       else if (unformat (line_input, "version %d", &tmp))
161         ver_o_c |= (tmp & 3) << 6;
162       else if (unformat (line_input, "o-bit %d", &tmp))
163         ver_o_c |= (tmp & 1) << 5;
164       else if (unformat (line_input, "c-bit %d", &tmp))
165         ver_o_c |= (tmp & 1) << 4;
166       else if (unformat (line_input, "md-type %d", &tmp))
167         md_type = tmp;
168       else if (unformat(line_input, "next-ip4"))
169         next_protocol = 1;
170       else if (unformat(line_input, "next-ip6"))
171         next_protocol = 2;
172       else if (unformat(line_input, "next-ethernet"))
173         next_protocol = 3;
174       else if (unformat (line_input, "c1 %d", &c1))
175         ;
176       else if (unformat (line_input, "c2 %d", &c2))
177         ;
178       else if (unformat (line_input, "c3 %d", &c3))
179         ;
180       else if (unformat (line_input, "c4 %d", &c4))
181         ;
182       else if (unformat (line_input, "nsp %d", &nsp))
183         nsp_set = 1;
184       else if (unformat (line_input, "nsi %d", &nsi))
185         nsi_set = 1;
186       else
187         return -99; // PARSE ERROR;
188     }
189
190     unformat_free (line_input);
191
192     if (nsp_set == 0)
193       return -1; //TODO Error type for this cond: clib_error_return (0, "nsp not specified");
194
195     if (nsi_set == 0)
196       return -2; //TODO Error type for this cond:clib_error_return (0, "nsi not specified");
197
198     if (md_type == 1)
199       length = 6;
200     else if (md_type == 2)
201       length = 2;  /* base header length */
202
203     nsp_nsi = (nsp<<8) | nsi;
204
205     /* Construct the API message */
206     M(NSH_ADD_DEL_ENTRY, nsh_add_del_entry);
207     mp->is_add = is_add;
208
209 #define _(x) mp->x = x;
210     foreach_copy_nsh_base_hdr_field;
211 #undef _
212
213
214     /* send it... */
215     S;
216
217     /* Wait for a reply... */
218     W;
219 }
220
221 static void vl_api_nsh_entry_details_t_handler
222 (vl_api_nsh_entry_details_t * mp)
223 {
224     vat_main_t * vam = &vat_main;
225
226     fformat(vam->ofp, "%11d%11d%11d%11d%14d%14d%14d%14d%14d\n",
227             mp->ver_o_c,
228             mp->length,
229             mp->md_type,
230             mp->next_protocol,
231             ntohl(mp->nsp_nsi),
232             ntohl(mp->c1),
233             ntohl(mp->c2),
234             ntohl(mp->c3),
235             ntohl(mp->c4));
236 }
237
238 static int api_nsh_entry_dump (vat_main_t * vam)
239 {
240     nsh_test_main_t * sm = &nsh_test_main;
241     vl_api_nsh_entry_dump_t *mp;
242     f64 timeout;
243
244     if (!vam->json_output) {
245         fformat(vam->ofp, "%11s%11s%15s%14s%14s%13s%13s%13s%13s\n",
246                 "ver_o_c", "length", "md_type", "next_protocol",
247                 "nsp_nsi", "c1", "c2", "c3", "c4");
248     }
249
250     /* Get list of nsh entries */
251     M(NSH_ENTRY_DUMP, nsh_entry_dump);
252
253     /* send it... */
254     S;
255
256     /* Wait for a reply... */
257     W;
258 }
259
260 static int api_nsh_add_del_map (vat_main_t * vam)
261 {
262     nsh_test_main_t * sm = &nsh_test_main;
263     unformat_input_t * line_input = vam->input;
264     f64 timeout;
265     u8 is_add = 1;
266     u32 nsp, nsi, mapped_nsp, mapped_nsi;
267     int nsp_set = 0, nsi_set = 0, mapped_nsp_set = 0, mapped_nsi_set = 0;
268     u32 next_node = ~0;
269     u32 sw_if_index = ~0; // temporary requirement to get this moved over to NSHSFC
270     vl_api_nsh_add_del_map_t * mp;
271
272
273     while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
274       if (unformat (line_input, "del"))
275         is_add = 0;
276       else if (unformat (line_input, "nsp %d", &nsp))
277         nsp_set = 1;
278       else if (unformat (line_input, "nsi %d", &nsi))
279         nsi_set = 1;
280       else if (unformat (line_input, "mapped-nsp %d", &mapped_nsp))
281         mapped_nsp_set = 1;
282       else if (unformat (line_input, "mapped-nsi %d", &mapped_nsi))
283         mapped_nsi_set = 1;
284       else if (unformat (line_input, "encap-gre4-intf %d", &sw_if_index))
285         next_node = NSH_NODE_NEXT_ENCAP_GRE4;
286       else if (unformat (line_input, "encap-gre6-intf %d", &sw_if_index))
287         next_node = NSH_NODE_NEXT_ENCAP_GRE6;
288       else if (unformat (line_input, "encap-vxlan-gpe-intf %d", &sw_if_index))
289         next_node = NSH_NODE_NEXT_ENCAP_VXLANGPE;
290       else if (unformat (line_input, "encap-none"))
291         next_node = NSH_NODE_NEXT_DROP; // Once moved to NSHSFC see nsh.h:foreach_nsh_input_next to handle this case
292       else
293         return -99; //TODO clib_error_return (0, "parse error: '%U'",
294     }
295
296     unformat_free (line_input);
297
298     if (nsp_set == 0 || nsi_set == 0)
299       return -1; // TODO create return value: clib_error_return (0, "nsp nsi pair required. Key: for NSH entry");
300
301     if (mapped_nsp_set == 0 || mapped_nsi_set == 0)
302       return -2; // TODO create return valuee clib_error_return (0, "mapped-nsp mapped-nsi pair required. Key: for NSH entry");
303
304     if (next_node == ~0)
305       return -3; //TODO clib_error_return (0, "must specific action: [encap-gre-intf <nn> | encap-vxlan-gpe-intf <nn> | encap-none]");
306
307
308     M(NSH_ADD_DEL_MAP, nsh_add_del_map);
309     /* set args structure */
310     mp->is_add = is_add;
311     mp->nsp_nsi = (nsp<< NSH_NSP_SHIFT) | nsi;
312     mp->mapped_nsp_nsi = (mapped_nsp<< NSH_NSP_SHIFT) | mapped_nsi;
313     mp->sw_if_index = sw_if_index;
314     mp->next_node = next_node;
315
316     /* send it... */
317     S;
318
319     /* Wait for a reply... */
320     W;
321
322
323 }
324
325 static void vl_api_nsh_map_details_t_handler
326 (vl_api_nsh_map_details_t * mp)
327 {
328     vat_main_t * vam = &vat_main;
329
330     fformat(vam->ofp, "%14d%14d%14d%14d\n",
331             ntohl(mp->nsp_nsi),
332             ntohl(mp->mapped_nsp_nsi),
333             ntohl(mp->sw_if_index),
334             ntohl(mp->next_node));
335 }
336
337 static int api_nsh_map_dump (vat_main_t * vam)
338 {
339     nsh_test_main_t * sm = &nsh_test_main;
340     vl_api_nsh_map_dump_t *mp;
341     f64 timeout;
342
343     if (!vam->json_output) {
344         fformat(vam->ofp, "%16s%16s%13s%13s\n",
345                 "nsp_nsi", "mapped_nsp_nsi", "sw_if_index", "next_node");
346     }
347
348     /* Get list of nsh entries */
349     M(NSH_MAP_DUMP, nsh_map_dump);
350
351     /* send it... */
352     S;
353
354     /* Wait for a reply... */
355     W;
356 }
357
358 /*
359  * List of messages that the api test plugin sends,
360  * and that the data plane plugin processes
361  */
362 #define foreach_vpe_api_msg \
363 _(nsh_add_del_entry, "{nsp <nn> nsi <nn>} c1 <nn> c2 <nn> c3 <nn> c4 <nn> [md-type <nn>] [tlv <xx>] [del]") \
364 _(nsh_entry_dump, "")   \
365 _(nsh_add_del_map, "nsp <nn> nsi <nn> [del] mapped-nsp <nn> mapped-nsi <nn> [encap-gre-intf <nn> | encap-vxlan-gpe-intf <nn> | encap-none]")  \
366 _(nsh_map_dump, "")
367
368 static void
369 nsh_vat_api_hookup (vat_main_t *vam)
370 {
371     nsh_test_main_t * sm = &nsh_test_main;
372     /* Hook up handlers for replies from the data plane plug-in */
373 #define _(N,n)                                                  \
374     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
375                            #n,                                  \
376                            vl_api_##n##_t_handler,              \
377                            vl_noop_handler,                     \
378                            vl_api_##n##_t_endian,               \
379                            vl_api_##n##_t_print,                \
380                            sizeof(vl_api_##n##_t), 1);
381     foreach_vpe_api_reply_msg;
382 #undef _
383
384     /* API messages we can send */
385 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
386     foreach_vpe_api_msg;
387 #undef _
388
389     /* Help strings */
390 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
391     foreach_vpe_api_msg;
392 #undef _
393 }
394
395 clib_error_t * vat_plugin_register (vat_main_t *vam)
396 {
397   nsh_test_main_t * sm = &nsh_test_main;
398   u8 * name;
399
400   sm->vat_main = vam;
401
402   /* Ask the vpp engine for the first assigned message-id */
403   name = format (0, "nsh_%08x%c", api_version, 0);
404   sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
405
406   if (sm->msg_id_base != (u16) ~0)
407     nsh_vat_api_hookup (vam);
408
409   vec_free(name);
410
411   return 0;
412 }