2 * nsh.c - skeleton vpp-api-test plug-in
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <vlibapi/api.h>
19 #include <vlibmemory/api.h>
20 #include <vppinfra/error.h>
23 uword unformat_sw_if_index (unformat_input_t * input, va_list * args);
25 /* define message IDs */
26 #define vl_msg_id(n,h) n,
28 #include <nsh/nsh.api.h>
29 /* We'll want to know how many messages IDs we need... */
30 VL_MSG_FIRST_AVAILABLE,
34 /* define message structures */
36 #include <nsh/nsh.api.h>
39 /* define generated endian-swappers */
41 #include <nsh/nsh.api.h>
44 /* instantiate all the print functions we know about */
45 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
47 #include <nsh/nsh.api.h>
50 /* Get the API version number */
51 #define vl_api_version(n,v) static u32 api_version=(v);
52 #include <nsh/nsh.api.h>
55 #define vl_msg_name_crc_list
56 #include <nsh/nsh.api.h>
57 #undef vl_msg_name_crc_list
61 /* API message ID base */
66 nsh_test_main_t nsh_test_main;
68 #define foreach_standard_reply_retval_handler \
69 _(nsh_add_del_entry_reply) \
70 _(nsh_add_del_map_reply) \
73 static void vl_api_##n##_t_handler \
74 (vl_api_##n##_t * mp) \
76 vat_main_t * vam = nsh_test_main.vat_main; \
77 i32 retval = ntohl(mp->retval); \
78 if (vam->async_mode) { \
79 vam->async_errors += (retval < 0); \
81 vam->retval = retval; \
82 vam->result_ready = 1; \
85 foreach_standard_reply_retval_handler;
89 * Table of message reply handlers, must include boilerplate handlers
92 #define foreach_vpe_api_reply_msg \
93 _(NSH_ADD_DEL_ENTRY_REPLY, nsh_add_del_entry_reply) \
94 _(NSH_ENTRY_DETAILS, nsh_entry_details) \
95 _(NSH_ADD_DEL_MAP_REPLY, nsh_add_del_map_reply) \
96 _(NSH_MAP_DETAILS, nsh_map_details)
99 /* M: construct, but don't yet send a message */
103 vam->result_ready = 0; \
104 mp = vl_msg_api_alloc(sizeof(*mp)); \
105 clib_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; \
112 vam->result_ready = 0; \
113 mp = vl_msg_api_alloc(sizeof(*mp)+(n)); \
114 clib_memset (mp, 0, sizeof (*mp)); \
115 mp->_vl_msg_id = ntohs (VL_API_##T + sm->msg_id_base); \
116 mp->client_index = vam->my_client_index; \
119 /* S: send a message */
120 #define S (vl_msg_api_send_shmem (vam->vl_input_queue, (u8 *)&mp))
122 /* W: wait for results, with timeout */
125 timeout = vat_time_now (vam) + 1.0; \
127 while (vat_time_now (vam) < timeout) { \
128 if (vam->result_ready == 1) { \
129 return (vam->retval); \
135 static int api_nsh_add_del_entry (vat_main_t * vam)
137 nsh_test_main_t * sm = &nsh_test_main;
138 unformat_input_t * line_input = vam->input;
144 u8 next_protocol = 1; /* default: ip4 */
155 vl_api_nsh_add_del_entry_t * mp;
157 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
158 if (unformat (line_input, "del"))
160 else if (unformat (line_input, "version %d", &tmp))
161 ver_o_c |= (tmp & 3) << 6;
162 else if (unformat (line_input, "o-bit %d", &tmp))
163 ver_o_c |= (tmp & 1) << 5;
164 else if (unformat (line_input, "c-bit %d", &tmp))
165 ver_o_c |= (tmp & 1) << 4;
166 else if (unformat (line_input, "md-type %d", &tmp))
168 else if (unformat(line_input, "next-ip4"))
170 else if (unformat(line_input, "next-ip6"))
172 else if (unformat(line_input, "next-ethernet"))
174 else if (unformat (line_input, "c1 %d", &c1))
176 else if (unformat (line_input, "c2 %d", &c2))
178 else if (unformat (line_input, "c3 %d", &c3))
180 else if (unformat (line_input, "c4 %d", &c4))
182 else if (unformat (line_input, "nsp %d", &nsp))
184 else if (unformat (line_input, "nsi %d", &nsi))
187 return -99; // PARSE ERROR;
190 unformat_free (line_input);
193 return -1; //TODO Error type for this cond: clib_error_return (0, "nsp not specified");
196 return -2; //TODO Error type for this cond:clib_error_return (0, "nsi not specified");
200 else if (md_type == 2)
201 length = 2; /* base header length */
203 nsp_nsi = (nsp<<8) | nsi;
205 /* Construct the API message */
206 M(NSH_ADD_DEL_ENTRY, nsh_add_del_entry);
209 #define _(x) mp->x = x;
210 foreach_copy_nsh_base_hdr_field;
217 /* Wait for a reply... */
221 static void vl_api_nsh_entry_details_t_handler
222 (vl_api_nsh_entry_details_t * mp)
224 vat_main_t * vam = &vat_main;
226 fformat(vam->ofp, "%11d%11d%11d%11d%14d%14d%14d%14d%14d\n",
238 static int api_nsh_entry_dump (vat_main_t * vam)
240 nsh_test_main_t * sm = &nsh_test_main;
241 vl_api_nsh_entry_dump_t *mp;
244 if (!vam->json_output) {
245 fformat(vam->ofp, "%11s%11s%15s%14s%14s%13s%13s%13s%13s\n",
246 "ver_o_c", "length", "md_type", "next_protocol",
247 "nsp_nsi", "c1", "c2", "c3", "c4");
250 /* Get list of nsh entries */
251 M(NSH_ENTRY_DUMP, nsh_entry_dump);
256 /* Wait for a reply... */
260 static int api_nsh_add_del_map (vat_main_t * vam)
262 nsh_test_main_t * sm = &nsh_test_main;
263 unformat_input_t * line_input = vam->input;
266 u32 nsp, nsi, mapped_nsp, mapped_nsi;
267 int nsp_set = 0, nsi_set = 0, mapped_nsp_set = 0, mapped_nsi_set = 0;
269 u32 sw_if_index = ~0; // temporary requirement to get this moved over to NSHSFC
270 vl_api_nsh_add_del_map_t * mp;
273 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
274 if (unformat (line_input, "del"))
276 else if (unformat (line_input, "nsp %d", &nsp))
278 else if (unformat (line_input, "nsi %d", &nsi))
280 else if (unformat (line_input, "mapped-nsp %d", &mapped_nsp))
282 else if (unformat (line_input, "mapped-nsi %d", &mapped_nsi))
284 else if (unformat (line_input, "encap-gre4-intf %d", &sw_if_index))
285 next_node = NSH_NODE_NEXT_ENCAP_GRE4;
286 else if (unformat (line_input, "encap-gre6-intf %d", &sw_if_index))
287 next_node = NSH_NODE_NEXT_ENCAP_GRE6;
288 else if (unformat (line_input, "encap-vxlan-gpe-intf %d", &sw_if_index))
289 next_node = NSH_NODE_NEXT_ENCAP_VXLANGPE;
290 else if (unformat (line_input, "encap-none"))
291 next_node = NSH_NODE_NEXT_DROP; // Once moved to NSHSFC see nsh.h:foreach_nsh_input_next to handle this case
293 return -99; //TODO clib_error_return (0, "parse error: '%U'",
296 unformat_free (line_input);
298 if (nsp_set == 0 || nsi_set == 0)
299 return -1; // TODO create return value: clib_error_return (0, "nsp nsi pair required. Key: for NSH entry");
301 if (mapped_nsp_set == 0 || mapped_nsi_set == 0)
302 return -2; // TODO create return valuee clib_error_return (0, "mapped-nsp mapped-nsi pair required. Key: for NSH entry");
305 return -3; //TODO clib_error_return (0, "must specific action: [encap-gre-intf <nn> | encap-vxlan-gpe-intf <nn> | encap-none]");
308 M(NSH_ADD_DEL_MAP, nsh_add_del_map);
309 /* set args structure */
311 mp->nsp_nsi = (nsp<< NSH_NSP_SHIFT) | nsi;
312 mp->mapped_nsp_nsi = (mapped_nsp<< NSH_NSP_SHIFT) | mapped_nsi;
313 mp->sw_if_index = sw_if_index;
314 mp->next_node = next_node;
319 /* Wait for a reply... */
325 static void vl_api_nsh_map_details_t_handler
326 (vl_api_nsh_map_details_t * mp)
328 vat_main_t * vam = &vat_main;
330 fformat(vam->ofp, "%14d%14d%14d%14d\n",
332 ntohl(mp->mapped_nsp_nsi),
333 ntohl(mp->sw_if_index),
334 ntohl(mp->next_node));
337 static int api_nsh_map_dump (vat_main_t * vam)
339 nsh_test_main_t * sm = &nsh_test_main;
340 vl_api_nsh_map_dump_t *mp;
343 if (!vam->json_output) {
344 fformat(vam->ofp, "%16s%16s%13s%13s\n",
345 "nsp_nsi", "mapped_nsp_nsi", "sw_if_index", "next_node");
348 /* Get list of nsh entries */
349 M(NSH_MAP_DUMP, nsh_map_dump);
354 /* Wait for a reply... */
359 * List of messages that the api test plugin sends,
360 * and that the data plane plugin processes
362 #define foreach_vpe_api_msg \
363 _(nsh_add_del_entry, "{nsp <nn> nsi <nn>} c1 <nn> c2 <nn> c3 <nn> c4 <nn> [md-type <nn>] [tlv <xx>] [del]") \
364 _(nsh_entry_dump, "") \
365 _(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]") \
369 nsh_vat_api_hookup (vat_main_t *vam)
371 nsh_test_main_t * sm = &nsh_test_main;
372 /* Hook up handlers for replies from the data plane plug-in */
374 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
376 vl_api_##n##_t_handler, \
378 vl_api_##n##_t_endian, \
379 vl_api_##n##_t_print, \
380 sizeof(vl_api_##n##_t), 1);
381 foreach_vpe_api_reply_msg;
384 /* API messages we can send */
385 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
390 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
395 clib_error_t * vat_plugin_register (vat_main_t *vam)
397 nsh_test_main_t * sm = &nsh_test_main;
402 /* Ask the vpp engine for the first assigned message-id */
403 name = format (0, "nsh_%08x%c", api_version, 0);
404 sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
407 if (sm->msg_id_base != (u16) ~0)
408 nsh_vat_api_hookup (vam);
410 return clib_error_return (0, "nsh plugin not loaded...");