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