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