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