dpdk: xstats vecor stuck at 0 elements
[vpp.git] / src / plugins / nsh / nsh_test.c
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 #include <nsh/nsh.api_types.h>
23 #include <nsh/nsh.api_enum.h>
24 uword unformat_sw_if_index (unformat_input_t * input, va_list * args);
25
26 /* define message structures */
27 #define vl_typedefs
28 #include <nsh/nsh.api.h>
29 #undef vl_typedefs
30
31 typedef struct {
32     /* API message ID base */
33     u16 msg_id_base;
34     vat_main_t *vat_main;
35 } nsh_test_main_t;
36
37 nsh_test_main_t nsh_test_main;
38
39 #define foreach_standard_reply_retval_handler   \
40 _(nsh_add_del_entry_reply)                      \
41 _(nsh_add_del_map_reply)                        \
42
43 #define _(n)                                            \
44     static void vl_api_##n##_t_handler                  \
45     (vl_api_##n##_t * mp)                               \
46     {                                                   \
47         vat_main_t * vam = nsh_test_main.vat_main;   \
48         i32 retval = ntohl(mp->retval);                 \
49         if (vam->async_mode) {                          \
50             vam->async_errors += (retval < 0);          \
51         } else {                                        \
52             vam->retval = retval;                       \
53             vam->result_ready = 1;                      \
54         }                                               \
55     }
56 foreach_standard_reply_retval_handler;
57 #undef _
58
59 /*
60  * Table of message reply handlers, must include boilerplate handlers
61  * we just generated
62  */
63 #define foreach_vpe_api_reply_msg                                       \
64 _(NSH_ADD_DEL_ENTRY_REPLY, nsh_add_del_entry_reply)                     \
65 _(NSH_ENTRY_DETAILS, nsh_entry_details)                                 \
66 _(NSH_ADD_DEL_MAP_REPLY, nsh_add_del_map_reply)                         \
67 _(NSH_MAP_DETAILS, nsh_map_details)
68
69
70 /* M: construct, but don't yet send a message */
71
72 #define M(T,t)                                                  \
73 do {                                                            \
74     vam->result_ready = 0;                                      \
75     mp = vl_msg_api_alloc(sizeof(*mp));                         \
76     clib_memset (mp, 0, sizeof (*mp));                               \
77     mp->_vl_msg_id = ntohs (VL_API_##T + sm->msg_id_base);      \
78     mp->client_index = vam->my_client_index;                    \
79 } while(0);
80
81 #define M2(T,t,n)                                               \
82 do {                                                            \
83     vam->result_ready = 0;                                      \
84     mp = vl_msg_api_alloc(sizeof(*mp)+(n));                     \
85     clib_memset (mp, 0, sizeof (*mp));                               \
86     mp->_vl_msg_id = ntohs (VL_API_##T + sm->msg_id_base);      \
87     mp->client_index = vam->my_client_index;                    \
88 } while(0);
89
90 /* S: send a message */
91 #define S (vl_msg_api_send_shmem (vam->vl_input_queue, (u8 *)&mp))
92
93 /* W: wait for results, with timeout */
94 #define W                                       \
95 do {                                            \
96     timeout = vat_time_now (vam) + 1.0;         \
97                                                 \
98     while (vat_time_now (vam) < timeout) {      \
99         if (vam->result_ready == 1) {           \
100             return (vam->retval);               \
101         }                                       \
102     }                                           \
103     return -99;                                 \
104 } while(0);
105
106 static int api_nsh_add_del_entry (vat_main_t * vam)
107 {
108     nsh_test_main_t * sm = &nsh_test_main;
109     unformat_input_t * line_input = vam->input;
110     f64 timeout;
111     u8 is_add = 1;
112     u8 ver_o_c = 0;
113     u8 length = 0;
114     u8 md_type = 0;
115     u8 next_protocol = 1; /* default: ip4 */
116     u32 nsp;
117     u8 nsp_set = 0;
118     u32 nsi;
119     u8 nsi_set = 0;
120     u32 nsp_nsi;
121     u32 c1 = 0;
122     u32 c2 = 0;
123     u32 c3 = 0;
124     u32 c4 = 0;
125     u32 tmp;
126     vl_api_nsh_add_del_entry_t * mp;
127
128     while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
129       if (unformat (line_input, "del"))
130         is_add = 0;
131       else if (unformat (line_input, "version %d", &tmp))
132         ver_o_c |= (tmp & 3) << 6;
133       else if (unformat (line_input, "o-bit %d", &tmp))
134         ver_o_c |= (tmp & 1) << 5;
135       else if (unformat (line_input, "c-bit %d", &tmp))
136         ver_o_c |= (tmp & 1) << 4;
137       else if (unformat (line_input, "md-type %d", &tmp))
138         md_type = tmp;
139       else if (unformat(line_input, "next-ip4"))
140         next_protocol = 1;
141       else if (unformat(line_input, "next-ip6"))
142         next_protocol = 2;
143       else if (unformat(line_input, "next-ethernet"))
144         next_protocol = 3;
145       else if (unformat (line_input, "c1 %d", &c1))
146         ;
147       else if (unformat (line_input, "c2 %d", &c2))
148         ;
149       else if (unformat (line_input, "c3 %d", &c3))
150         ;
151       else if (unformat (line_input, "c4 %d", &c4))
152         ;
153       else if (unformat (line_input, "nsp %d", &nsp))
154         nsp_set = 1;
155       else if (unformat (line_input, "nsi %d", &nsi))
156         nsi_set = 1;
157       else
158         return -99; // PARSE ERROR;
159     }
160
161     unformat_free (line_input);
162
163     if (nsp_set == 0)
164       return -1; //TODO Error type for this cond: clib_error_return (0, "nsp not specified");
165
166     if (nsi_set == 0)
167       return -2; //TODO Error type for this cond:clib_error_return (0, "nsi not specified");
168
169     if (md_type == 1)
170       length = 6;
171     else if (md_type == 2)
172       length = 2;  /* base header length */
173
174     nsp_nsi = (nsp<<8) | nsi;
175
176     /* Construct the API message */
177     M(NSH_ADD_DEL_ENTRY, nsh_add_del_entry);
178     mp->is_add = is_add;
179
180 #define _(x) mp->x = x;
181     foreach_copy_nsh_base_hdr_field;
182 #undef _
183
184
185     /* send it... */
186     S;
187
188     /* Wait for a reply... */
189     W;
190 }
191
192 static void vl_api_nsh_entry_details_t_handler
193 (vl_api_nsh_entry_details_t * mp)
194 {
195     vat_main_t * vam = &vat_main;
196
197     fformat(vam->ofp, "%11d%11d%11d%11d%14d%14d%14d%14d%14d\n",
198             mp->ver_o_c,
199             mp->length,
200             mp->md_type,
201             mp->next_protocol,
202             ntohl(mp->nsp_nsi),
203             ntohl(mp->c1),
204             ntohl(mp->c2),
205             ntohl(mp->c3),
206             ntohl(mp->c4));
207 }
208
209 static int api_nsh_entry_dump (vat_main_t * vam)
210 {
211     nsh_test_main_t * sm = &nsh_test_main;
212     vl_api_nsh_entry_dump_t *mp;
213     f64 timeout;
214
215     if (!vam->json_output) {
216         fformat(vam->ofp, "%11s%11s%15s%14s%14s%13s%13s%13s%13s\n",
217                 "ver_o_c", "length", "md_type", "next_protocol",
218                 "nsp_nsi", "c1", "c2", "c3", "c4");
219     }
220
221     /* Get list of nsh entries */
222     M(NSH_ENTRY_DUMP, nsh_entry_dump);
223
224     /* send it... */
225     S;
226
227     /* Wait for a reply... */
228     W;
229 }
230
231 static int api_nsh_add_del_map (vat_main_t * vam)
232 {
233     nsh_test_main_t * sm = &nsh_test_main;
234     unformat_input_t * line_input = vam->input;
235     f64 timeout;
236     u8 is_add = 1;
237     u32 nsp, nsi, mapped_nsp, mapped_nsi;
238     int nsp_set = 0, nsi_set = 0, mapped_nsp_set = 0, mapped_nsi_set = 0;
239     u32 next_node = ~0;
240     u32 sw_if_index = ~0; // temporary requirement to get this moved over to NSHSFC
241     vl_api_nsh_add_del_map_t * mp;
242
243
244     while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
245       if (unformat (line_input, "del"))
246         is_add = 0;
247       else if (unformat (line_input, "nsp %d", &nsp))
248         nsp_set = 1;
249       else if (unformat (line_input, "nsi %d", &nsi))
250         nsi_set = 1;
251       else if (unformat (line_input, "mapped-nsp %d", &mapped_nsp))
252         mapped_nsp_set = 1;
253       else if (unformat (line_input, "mapped-nsi %d", &mapped_nsi))
254         mapped_nsi_set = 1;
255       else if (unformat (line_input, "encap-gre4-intf %d", &sw_if_index))
256         next_node = NSH_NODE_NEXT_ENCAP_GRE4;
257       else if (unformat (line_input, "encap-gre6-intf %d", &sw_if_index))
258         next_node = NSH_NODE_NEXT_ENCAP_GRE6;
259       else if (unformat (line_input, "encap-vxlan-gpe-intf %d", &sw_if_index))
260         next_node = NSH_NODE_NEXT_ENCAP_VXLANGPE;
261       else if (unformat (line_input, "encap-none"))
262         next_node = NSH_NODE_NEXT_DROP; // Once moved to NSHSFC see nsh.h:foreach_nsh_input_next to handle this case
263       else
264         return -99; //TODO clib_error_return (0, "parse error: '%U'",
265     }
266
267     unformat_free (line_input);
268
269     if (nsp_set == 0 || nsi_set == 0)
270       return -1; // TODO create return value: clib_error_return (0, "nsp nsi pair required. Key: for NSH entry");
271
272     if (mapped_nsp_set == 0 || mapped_nsi_set == 0)
273       return -2; // TODO create return valuee clib_error_return (0, "mapped-nsp mapped-nsi pair required. Key: for NSH entry");
274
275     if (next_node == ~0)
276       return -3; //TODO clib_error_return (0, "must specific action: [encap-gre-intf <nn> | encap-vxlan-gpe-intf <nn> | encap-none]");
277
278
279     M(NSH_ADD_DEL_MAP, nsh_add_del_map);
280     /* set args structure */
281     mp->is_add = is_add;
282     mp->nsp_nsi = (nsp<< NSH_NSP_SHIFT) | nsi;
283     mp->mapped_nsp_nsi = (mapped_nsp<< NSH_NSP_SHIFT) | mapped_nsi;
284     mp->sw_if_index = sw_if_index;
285     mp->next_node = next_node;
286
287     /* send it... */
288     S;
289
290     /* Wait for a reply... */
291     W;
292
293
294 }
295
296 static void vl_api_nsh_map_details_t_handler
297 (vl_api_nsh_map_details_t * mp)
298 {
299     vat_main_t * vam = &vat_main;
300
301     fformat(vam->ofp, "%14d%14d%14d%14d\n",
302             ntohl(mp->nsp_nsi),
303             ntohl(mp->mapped_nsp_nsi),
304             ntohl(mp->sw_if_index),
305             ntohl(mp->next_node));
306 }
307
308 static int api_nsh_map_dump (vat_main_t * vam)
309 {
310     nsh_test_main_t * sm = &nsh_test_main;
311     vl_api_nsh_map_dump_t *mp;
312     f64 timeout;
313
314     if (!vam->json_output) {
315         fformat(vam->ofp, "%16s%16s%13s%13s\n",
316                 "nsp_nsi", "mapped_nsp_nsi", "sw_if_index", "next_node");
317     }
318
319     /* Get list of nsh entries */
320     M(NSH_MAP_DUMP, nsh_map_dump);
321
322     /* send it... */
323     S;
324
325     /* Wait for a reply... */
326     W;
327 }
328
329 #include <nsh/nsh.api_test.c>