nsh_test.c revision e566590e
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
90
91/* M: construct, but don't yet send a message */
92
93#define M(T,t)                                                  \
94do {                                                            \
95    vam->result_ready = 0;                                      \
96    mp = vl_msg_api_alloc(sizeof(*mp));                         \
97    memset (mp, 0, sizeof (*mp));                               \
98    mp->_vl_msg_id = ntohs (VL_API_##T + sm->msg_id_base);      \
99    mp->client_index = vam->my_client_index;                    \
100} while(0);
101
102#define M2(T,t,n)                                               \
103do {                                                            \
104    vam->result_ready = 0;                                      \
105    mp = vl_msg_api_alloc(sizeof(*mp)+(n));                     \
106    memset (mp, 0, sizeof (*mp));                               \
107    mp->_vl_msg_id = ntohs (VL_API_##T + sm->msg_id_base);      \
108    mp->client_index = vam->my_client_index;                    \
109} while(0);
110
111/* S: send a message */
112#define S (vl_msg_api_send_shmem (vam->vl_input_queue, (u8 *)&mp))
113
114/* W: wait for results, with timeout */
115#define W                                       \
116do {                                            \
117    timeout = vat_time_now (vam) + 1.0;         \
118                                                \
119    while (vat_time_now (vam) < timeout) {      \
120        if (vam->result_ready == 1) {           \
121            return (vam->retval);               \
122        }                                       \
123    }                                           \
124    return -99;                                 \
125} while(0);
126
127static int api_nsh_add_del_entry (vat_main_t * vam)
128{
129    nsh_test_main_t * sm = &nsh_test_main;
130    unformat_input_t * line_input = vam->input;
131    f64 timeout;
132    u8 is_add = 1;
133    u8 ver_o_c = 0;
134    u8 length = 0;
135    u8 md_type = 0;
136    u8 next_protocol = 1; /* default: ip4 */
137    u32 nsp;
138    u8 nsp_set = 0;
139    u32 nsi;
140    u8 nsi_set = 0;
141    u32 nsp_nsi;
142    u32 c1 = 0;
143    u32 c2 = 0;
144    u32 c3 = 0;
145    u32 c4 = 0;
146    u32 *tlvs = 0;
147    u32 tmp;
148    vl_api_nsh_add_del_entry_t * mp;
149
150    while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
151      if (unformat (line_input, "del"))
152	is_add = 0;
153      else if (unformat (line_input, "version %d", &tmp))
154	ver_o_c |= (tmp & 3) << 6;
155      else if (unformat (line_input, "o-bit %d", &tmp))
156	ver_o_c |= (tmp & 1) << 5;
157      else if (unformat (line_input, "c-bit %d", &tmp))
158	ver_o_c |= (tmp & 1) << 4;
159      else if (unformat (line_input, "md-type %d", &tmp))
160	md_type = tmp;
161      else if (unformat(line_input, "next-ip4"))
162	next_protocol = 1;
163      else if (unformat(line_input, "next-ip6"))
164	next_protocol = 2;
165      else if (unformat(line_input, "next-ethernet"))
166	next_protocol = 3;
167      else if (unformat (line_input, "c1 %d", &c1))
168	;
169      else if (unformat (line_input, "c2 %d", &c2))
170	;
171      else if (unformat (line_input, "c3 %d", &c3))
172	;
173      else if (unformat (line_input, "c4 %d", &c4))
174	;
175      else if (unformat (line_input, "nsp %d", &nsp))
176	nsp_set = 1;
177      else if (unformat (line_input, "nsi %d", &nsi))
178	nsi_set = 1;
179      else if (unformat (line_input, "tlv %x"))
180        vec_add1 (tlvs, tmp);
181      else
182	return -99; // PARSE ERROR;
183    }
184
185    unformat_free (line_input);
186
187    if (nsp_set == 0)
188      return -1; //TODO Error type for this cond: clib_error_return (0, "nsp not specified");
189
190    if (nsi_set == 0)
191      return -2; //TODO Error type for this cond:clib_error_return (0, "nsi not specified");
192
193    if (md_type != 1)
194      return -3; //TODO Error type for this cond: clib_error_return (0, "md-type 1 only supported at this time");
195
196    //TODO sort out TLVS and MD Type2 support
197    md_type = 1;
198    length = 6;
199
200    nsp_nsi = (nsp<<8) | nsi;
201
202    /* Construct the API message */
203    M(NSH_ADD_DEL_ENTRY, nsh_add_del_entry);
204    mp->is_add = is_add;
205
206#define _(x) mp->x = x;
207    foreach_copy_nshhdr_field;
208#undef _
209
210    mp->tlvs[0] = 0 ; // TODO FIXME this shouldn't be set 0 - in NSH_SFC project
211
212    /* send it... */
213    S;
214
215    /* Wait for a reply... */
216    W;
217}
218
219static void vl_api_nsh_entry_details_t_handler
220(vl_api_nsh_entry_details_t * mp)
221{
222    vat_main_t * vam = &vat_main;
223
224    fformat(vam->ofp, "%11d%11d%11d%11d%14d%14d%14d%14d%14d\n",
225            mp->ver_o_c,
226            mp->length,
227	    mp->md_type,
228	    mp->next_protocol,
229            ntohl(mp->nsp_nsi),
230	    ntohl(mp->c1),
231	    ntohl(mp->c2),
232	    ntohl(mp->c3),
233            ntohl(mp->c4));
234}
235
236static int api_nsh_entry_dump (vat_main_t * vam)
237{
238    nsh_test_main_t * sm = &nsh_test_main;
239    vl_api_nsh_entry_dump_t *mp;
240    f64 timeout;
241
242    if (!vam->json_output) {
243        fformat(vam->ofp, "%11s%11s%15s%14s%14s%13s%13s%13s%13s\n",
244                "ver_o_c", "length", "md_type", "next_protocol",
245                "nsp_nsi", "c1", "c2", "c3", "c4");
246    }
247
248    /* Get list of nsh entries */
249    M(NSH_ENTRY_DUMP, nsh_entry_dump);
250
251    /* send it... */
252    S;
253
254    /* Wait for a reply... */
255    W;
256}
257
258static int api_nsh_add_del_map (vat_main_t * vam)
259{
260    nsh_test_main_t * sm = &nsh_test_main;
261    unformat_input_t * line_input = vam->input;
262    f64 timeout;
263    u8 is_add = 1;
264    u32 nsp, nsi, mapped_nsp, mapped_nsi;
265    int nsp_set = 0, nsi_set = 0, mapped_nsp_set = 0, mapped_nsi_set = 0;
266    u32 next_node = ~0;
267    u32 sw_if_index = ~0; // temporary requirement to get this moved over to NSHSFC
268    vl_api_nsh_add_del_map_t * mp;
269
270
271    while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
272      if (unformat (line_input, "del"))
273	is_add = 0;
274      else if (unformat (line_input, "nsp %d", &nsp))
275	nsp_set = 1;
276      else if (unformat (line_input, "nsi %d", &nsi))
277	nsi_set = 1;
278      else if (unformat (line_input, "mapped-nsp %d", &mapped_nsp))
279	mapped_nsp_set = 1;
280      else if (unformat (line_input, "mapped-nsi %d", &mapped_nsi))
281	mapped_nsi_set = 1;
282      else if (unformat (line_input, "encap-gre-intf %d", &sw_if_index))
283	next_node = NSH_INPUT_NEXT_ENCAP_GRE;
284      else if (unformat (line_input, "encap-vxlan-gpe-intf %d", &sw_if_index))
285	next_node = NSH_INPUT_NEXT_ENCAP_VXLANGPE;
286      else if (unformat (line_input, "encap-none"))
287	next_node = NSH_INPUT_NEXT_DROP; // Once moved to NSHSFC see nsh.h:foreach_nsh_input_next to handle this case
288      else
289	return -99; //TODO clib_error_return (0, "parse error: '%U'",
290    }
291
292    unformat_free (line_input);
293
294    if (nsp_set == 0 || nsi_set == 0)
295      return -1; // TODO create return value: clib_error_return (0, "nsp nsi pair required. Key: for NSH entry");
296
297    if (mapped_nsp_set == 0 || mapped_nsi_set == 0)
298      return -2; // TODO create return valuee clib_error_return (0, "mapped-nsp mapped-nsi pair required. Key: for NSH entry");
299
300    if (next_node == ~0)
301      return -3; //TODO clib_error_return (0, "must specific action: [encap-gre-intf <nn> | encap-vxlan-gpe-intf <nn> | encap-none]");
302
303
304    M(NSH_ADD_DEL_MAP, nsh_add_del_map);
305    /* set args structure */
306    mp->is_add = is_add;
307    mp->nsp_nsi = (nsp<< NSH_NSP_SHIFT) | nsi;
308    mp->mapped_nsp_nsi = (mapped_nsp<< NSH_NSP_SHIFT) | mapped_nsi;
309    mp->sw_if_index = sw_if_index;
310    mp->next_node = next_node;
311
312    /* send it... */
313    S;
314
315    /* Wait for a reply... */
316    W;
317
318
319}
320
321static void vl_api_nsh_map_details_t_handler
322(vl_api_nsh_map_details_t * mp)
323{
324    vat_main_t * vam = &vat_main;
325
326    fformat(vam->ofp, "%14d%14d%14d%14d\n",
327            ntohl(mp->nsp_nsi),
328	    ntohl(mp->mapped_nsp_nsi),
329	    ntohl(mp->sw_if_index),
330	    ntohl(mp->next_node));
331}
332
333static int api_nsh_map_dump (vat_main_t * vam)
334{
335    nsh_test_main_t * sm = &nsh_test_main;
336    vl_api_nsh_map_dump_t *mp;
337    f64 timeout;
338
339    if (!vam->json_output) {
340        fformat(vam->ofp, "%16s%16s%13s%13s\n",
341                "nsp_nsi", "mapped_nsp_nsi", "sw_if_index", "next_node");
342    }
343
344    /* Get list of nsh entries */
345    M(NSH_MAP_DUMP, nsh_map_dump);
346
347    /* send it... */
348    S;
349
350    /* Wait for a reply... */
351    W;
352}
353
354/*
355 * List of messages that the api test plugin sends,
356 * and that the data plane plugin processes
357 */
358#define foreach_vpe_api_msg \
359_(nsh_add_del_entry, "{nsp <nn> nsi <nn>} c1 <nn> c2 <nn> c3 <nn> c4 <nn> [md-type <nn>] [tlv <xx>] [del]") \
360_(nsh_entry_dump, "")   \
361_(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]")  \
362_(nsh_map_dump, "")
363
364void vat_api_hookup (vat_main_t *vam)
365{
366    nsh_test_main_t * sm = &nsh_test_main;
367    /* Hook up handlers for replies from the data plane plug-in */
368#define _(N,n)                                                  \
369    vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
370                           #n,                                  \
371                           vl_api_##n##_t_handler,              \
372                           vl_noop_handler,                     \
373                           vl_api_##n##_t_endian,               \
374                           vl_api_##n##_t_print,                \
375                           sizeof(vl_api_##n##_t), 1);
376    foreach_vpe_api_reply_msg;
377#undef _
378
379    /* API messages we can send */
380#define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
381    foreach_vpe_api_msg;
382#undef _
383
384    /* Help strings */
385#define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
386    foreach_vpe_api_msg;
387#undef _
388}
389
390clib_error_t * vat_plugin_register (vat_main_t *vam)
391{
392  nsh_test_main_t * sm = &nsh_test_main;
393  u8 * name;
394
395  sm->vat_main = vam;
396
397  /* Ask the vpp engine for the first assigned message-id */
398  name = format (0, "nsh_%08x%c", api_version, 0);
399  sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
400
401  if (sm->msg_id_base != (u16) ~0)
402    vat_api_hookup (vam);
403
404  vec_free(name);
405
406  return 0;
407}
408