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>
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);
26 /* define message structures */
28 #include <nsh/nsh.api.h>
32 /* API message ID base */
37 nsh_test_main_t nsh_test_main;
39 #define foreach_standard_reply_retval_handler \
40 _(nsh_add_del_entry_reply) \
41 _(nsh_add_del_map_reply) \
44 static void vl_api_##n##_t_handler \
45 (vl_api_##n##_t * mp) \
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); \
52 vam->retval = retval; \
53 vam->result_ready = 1; \
56 foreach_standard_reply_retval_handler;
60 * Table of message reply handlers, must include boilerplate handlers
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)
70 /* M: construct, but don't yet send a message */
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; \
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; \
90 /* S: send a message */
91 #define S (vl_msg_api_send_shmem (vam->vl_input_queue, (u8 *)&mp))
93 /* W: wait for results, with timeout */
96 timeout = vat_time_now (vam) + 1.0; \
98 while (vat_time_now (vam) < timeout) { \
99 if (vam->result_ready == 1) { \
100 return (vam->retval); \
106 static int api_nsh_add_del_entry (vat_main_t * vam)
108 nsh_test_main_t * sm = &nsh_test_main;
109 unformat_input_t * line_input = vam->input;
115 u8 next_protocol = 1; /* default: ip4 */
126 vl_api_nsh_add_del_entry_t * mp;
128 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
129 if (unformat (line_input, "del"))
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))
139 else if (unformat(line_input, "next-ip4"))
141 else if (unformat(line_input, "next-ip6"))
143 else if (unformat(line_input, "next-ethernet"))
145 else if (unformat (line_input, "c1 %d", &c1))
147 else if (unformat (line_input, "c2 %d", &c2))
149 else if (unformat (line_input, "c3 %d", &c3))
151 else if (unformat (line_input, "c4 %d", &c4))
153 else if (unformat (line_input, "nsp %d", &nsp))
155 else if (unformat (line_input, "nsi %d", &nsi))
158 return -99; // PARSE ERROR;
161 unformat_free (line_input);
164 return -1; //TODO Error type for this cond: clib_error_return (0, "nsp not specified");
167 return -2; //TODO Error type for this cond:clib_error_return (0, "nsi not specified");
171 else if (md_type == 2)
172 length = 2; /* base header length */
174 nsp_nsi = (nsp<<8) | nsi;
176 /* Construct the API message */
177 M(NSH_ADD_DEL_ENTRY, nsh_add_del_entry);
180 #define _(x) mp->x = x;
181 foreach_copy_nsh_base_hdr_field;
188 /* Wait for a reply... */
192 static void vl_api_nsh_entry_details_t_handler
193 (vl_api_nsh_entry_details_t * mp)
195 vat_main_t * vam = &vat_main;
197 fformat(vam->ofp, "%11d%11d%11d%11d%14d%14d%14d%14d%14d\n",
209 static int api_nsh_entry_dump (vat_main_t * vam)
211 nsh_test_main_t * sm = &nsh_test_main;
212 vl_api_nsh_entry_dump_t *mp;
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");
221 /* Get list of nsh entries */
222 M(NSH_ENTRY_DUMP, nsh_entry_dump);
227 /* Wait for a reply... */
231 static int api_nsh_add_del_map (vat_main_t * vam)
233 nsh_test_main_t * sm = &nsh_test_main;
234 unformat_input_t * line_input = vam->input;
237 u32 nsp, nsi, mapped_nsp, mapped_nsi;
238 int nsp_set = 0, nsi_set = 0, mapped_nsp_set = 0, mapped_nsi_set = 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;
244 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
245 if (unformat (line_input, "del"))
247 else if (unformat (line_input, "nsp %d", &nsp))
249 else if (unformat (line_input, "nsi %d", &nsi))
251 else if (unformat (line_input, "mapped-nsp %d", &mapped_nsp))
253 else if (unformat (line_input, "mapped-nsi %d", &mapped_nsi))
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
264 return -99; //TODO clib_error_return (0, "parse error: '%U'",
267 unformat_free (line_input);
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");
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");
276 return -3; //TODO clib_error_return (0, "must specific action: [encap-gre-intf <nn> | encap-vxlan-gpe-intf <nn> | encap-none]");
279 M(NSH_ADD_DEL_MAP, nsh_add_del_map);
280 /* set args structure */
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;
290 /* Wait for a reply... */
296 static void vl_api_nsh_map_details_t_handler
297 (vl_api_nsh_map_details_t * mp)
299 vat_main_t * vam = &vat_main;
301 fformat(vam->ofp, "%14d%14d%14d%14d\n",
303 ntohl(mp->mapped_nsp_nsi),
304 ntohl(mp->sw_if_index),
305 ntohl(mp->next_node));
308 static int api_nsh_map_dump (vat_main_t * vam)
310 nsh_test_main_t * sm = &nsh_test_main;
311 vl_api_nsh_map_dump_t *mp;
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");
319 /* Get list of nsh entries */
320 M(NSH_MAP_DUMP, nsh_map_dump);
325 /* Wait for a reply... */
329 #include <nsh/nsh.api_test.c>