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