nsh_test.c revision 1a8e2a69
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_ADD_DEL_MAP_REPLY, nsh_add_del_map_reply)
87
88
89/* M: construct, but don't yet send a message */
90
91#define M(T,t)                                                  \
92do {                                                            \
93    vam->result_ready = 0;                                      \
94    mp = vl_msg_api_alloc(sizeof(*mp));                         \
95    memset (mp, 0, sizeof (*mp));                               \
96    mp->_vl_msg_id = ntohs (VL_API_##T + sm->msg_id_base);      \
97    mp->client_index = vam->my_client_index;                    \
98} while(0);
99
100#define M2(T,t,n)                                               \
101do {                                                            \
102    vam->result_ready = 0;                                      \
103    mp = vl_msg_api_alloc(sizeof(*mp)+(n));                     \
104    memset (mp, 0, sizeof (*mp));                               \
105    mp->_vl_msg_id = ntohs (VL_API_##T + sm->msg_id_base);      \
106    mp->client_index = vam->my_client_index;                    \
107} while(0);
108
109/* S: send a message */
110#define S (vl_msg_api_send_shmem (vam->vl_input_queue, (u8 *)&mp))
111
112/* W: wait for results, with timeout */
113#define W                                       \
114do {                                            \
115    timeout = vat_time_now (vam) + 1.0;         \
116                                                \
117    while (vat_time_now (vam) < timeout) {      \
118        if (vam->result_ready == 1) {           \
119            return (vam->retval);               \
120        }                                       \
121    }                                           \
122    return -99;                                 \
123} while(0);
124
125static int api_nsh_add_del_entry (vat_main_t * vam)
126{
127    nsh_test_main_t * sm = &nsh_test_main;
128    unformat_input_t * line_input = vam->input;
129    f64 timeout;
130    u8 is_add = 1;
131    u8 ver_o_c = 0;
132    u8 length = 0;
133    u8 md_type = 0;
134    u8 next_protocol = 1; /* default: ip4 */
135    u32 nsp;
136    u8 nsp_set = 0;
137    u32 nsi;
138    u8 nsi_set = 0;
139    u32 nsp_nsi;
140    u32 c1 = 0;
141    u32 c2 = 0;
142    u32 c3 = 0;
143    u32 c4 = 0;
144    u32 *tlvs = 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 if (unformat (line_input, "tlv %x"))
178        vec_add1 (tlvs, tmp);
179      else
180	return -99; // PARSE ERROR;
181    }
182
183    unformat_free (line_input);
184
185    if (nsp_set == 0)
186      return -1; //TODO Error type for this cond: clib_error_return (0, "nsp not specified");
187
188    if (nsi_set == 0)
189      return -2; //TODO Error type for this cond:clib_error_return (0, "nsi not specified");
190
191    if (md_type != 1)
192      return -3; //TODO Error type for this cond: clib_error_return (0, "md-type 1 only supported at this time");
193
194    //TODO sort out TLVS and MD Type2 support
195    md_type = 1;
196    length = 6;
197
198    nsp_nsi = (nsp<<8) | nsi;
199
200    /* Construct the API message */
201    M(NSH_ADD_DEL_ENTRY, nsh_add_del_entry);
202    mp->is_add = is_add;
203
204#define _(x) mp->x = x;
205    foreach_copy_nshhdr_field;
206#undef _
207
208    mp->tlvs[0] = 0 ; // TODO FIXME this shouldn't be set 0 - in NSH_SFC project
209
210    /* send it... */
211    S;
212
213    /* Wait for a reply... */
214    W;
215}
216
217static int api_nsh_add_del_map (vat_main_t * vam)
218{
219    nsh_test_main_t * sm = &nsh_test_main;
220    unformat_input_t * line_input = vam->input;
221    f64 timeout;
222    u8 is_add = 1;
223    u32 nsp, nsi, mapped_nsp, mapped_nsi;
224    int nsp_set = 0, nsi_set = 0, mapped_nsp_set = 0, mapped_nsi_set = 0;
225    u32 next_node = ~0;
226    u32 sw_if_index = ~0; // temporary requirement to get this moved over to NSHSFC
227    vl_api_nsh_add_del_map_t * mp;
228
229
230    while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
231      if (unformat (line_input, "del"))
232	is_add = 0;
233      else if (unformat (line_input, "nsp %d", &nsp))
234	nsp_set = 1;
235      else if (unformat (line_input, "nsi %d", &nsi))
236	nsi_set = 1;
237      else if (unformat (line_input, "mapped-nsp %d", &mapped_nsp))
238	mapped_nsp_set = 1;
239      else if (unformat (line_input, "mapped-nsi %d", &mapped_nsi))
240	mapped_nsi_set = 1;
241      else if (unformat (line_input, "encap-gre-intf %d", &sw_if_index))
242	next_node = NSH_INPUT_NEXT_ENCAP_GRE;
243      else if (unformat (line_input, "encap-vxlan-gpe-intf %d", &sw_if_index))
244	next_node = NSH_INPUT_NEXT_ENCAP_VXLANGPE;
245      else if (unformat (line_input, "encap-none"))
246	next_node = NSH_INPUT_NEXT_DROP; // Once moved to NSHSFC see nsh.h:foreach_nsh_input_next to handle this case
247      else
248	return -99; //TODO clib_error_return (0, "parse error: '%U'",
249    }
250
251    unformat_free (line_input);
252
253    if (nsp_set == 0 || nsi_set == 0)
254      return -1; // TODO create return value: clib_error_return (0, "nsp nsi pair required. Key: for NSH entry");
255
256    if (mapped_nsp_set == 0 || mapped_nsi_set == 0)
257      return -2; // TODO create return valuee clib_error_return (0, "mapped-nsp mapped-nsi pair required. Key: for NSH entry");
258
259    if (next_node == ~0)
260      return -3; //TODO clib_error_return (0, "must specific action: [encap-gre-intf <nn> | encap-vxlan-gpe-intf <nn> | encap-none]");
261
262
263    M(NSH_ADD_DEL_ENTRY, nsh_add_del_entry);
264    /* set args structure */
265    mp->is_add = is_add;
266    mp->nsp_nsi = (nsp<< NSH_NSP_SHIFT) | nsi;
267    mp->mapped_nsp_nsi = (mapped_nsp<< NSH_NSP_SHIFT) | mapped_nsi;
268    mp->sw_if_index = sw_if_index;
269    mp->next_node = next_node;
270
271    /* send it... */
272    S;
273
274    /* Wait for a reply... */
275    W;
276
277
278}
279/*
280 * List of messages that the api test plugin sends,
281 * and that the data plane plugin processes
282 */
283#define foreach_vpe_api_msg \
284_(nsh_add_del_entry, "{nsp <nn> nsi <nn>} c1 <nn> c2 <nn> c3 <nn> c4 <nn> [md-type <nn>] [tlv <xx>] [del]") \
285_(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]")
286
287void vat_api_hookup (vat_main_t *vam)
288{
289    nsh_test_main_t * sm = &nsh_test_main;
290    /* Hook up handlers for replies from the data plane plug-in */
291#define _(N,n)                                                  \
292    vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
293                           #n,                                  \
294                           vl_api_##n##_t_handler,              \
295                           vl_noop_handler,                     \
296                           vl_api_##n##_t_endian,               \
297                           vl_api_##n##_t_print,                \
298                           sizeof(vl_api_##n##_t), 1);
299    foreach_vpe_api_reply_msg;
300#undef _
301
302    /* API messages we can send */
303#define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
304    foreach_vpe_api_msg;
305#undef _
306
307    /* Help strings */
308#define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
309    foreach_vpe_api_msg;
310#undef _
311}
312
313clib_error_t * vat_plugin_register (vat_main_t *vam)
314{
315  nsh_test_main_t * sm = &nsh_test_main;
316  u8 * name;
317
318  sm->vat_main = vam;
319
320  /* Ask the vpp engine for the first assigned message-id */
321  name = format (0, "nsh_%08x%c", api_version, 0);
322  sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
323
324  if (sm->msg_id_base != (u16) ~0)
325    vat_api_hookup (vam);
326
327  vec_free(name);
328
329  return 0;
330}
331