Line data Source code
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 0 : 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 0 : static int api_nsh_add_del_entry (vat_main_t * vam)
107 : {
108 0 : nsh_test_main_t * sm = &nsh_test_main;
109 0 : unformat_input_t * line_input = vam->input;
110 : f64 timeout;
111 0 : u8 is_add = 1;
112 0 : u8 ver_o_c = 0;
113 0 : u8 length = 0;
114 0 : u8 md_type = 0;
115 0 : u8 next_protocol = 1; /* default: ip4 */
116 : u32 nsp;
117 0 : u8 nsp_set = 0;
118 : u32 nsi;
119 0 : u8 nsi_set = 0;
120 : u32 nsp_nsi;
121 0 : u32 c1 = 0;
122 0 : u32 c2 = 0;
123 0 : u32 c3 = 0;
124 0 : u32 c4 = 0;
125 : u32 tmp;
126 : vl_api_nsh_add_del_entry_t * mp;
127 :
128 0 : while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
129 0 : if (unformat (line_input, "del"))
130 0 : is_add = 0;
131 0 : else if (unformat (line_input, "version %d", &tmp))
132 0 : ver_o_c |= (tmp & 3) << 6;
133 0 : else if (unformat (line_input, "o-bit %d", &tmp))
134 0 : ver_o_c |= (tmp & 1) << 5;
135 0 : else if (unformat (line_input, "c-bit %d", &tmp))
136 0 : ver_o_c |= (tmp & 1) << 4;
137 0 : else if (unformat (line_input, "md-type %d", &tmp))
138 0 : md_type = tmp;
139 0 : else if (unformat(line_input, "next-ip4"))
140 0 : next_protocol = 1;
141 0 : else if (unformat(line_input, "next-ip6"))
142 0 : next_protocol = 2;
143 0 : else if (unformat(line_input, "next-ethernet"))
144 0 : next_protocol = 3;
145 0 : else if (unformat (line_input, "c1 %d", &c1))
146 : ;
147 0 : else if (unformat (line_input, "c2 %d", &c2))
148 : ;
149 0 : else if (unformat (line_input, "c3 %d", &c3))
150 : ;
151 0 : else if (unformat (line_input, "c4 %d", &c4))
152 : ;
153 0 : else if (unformat (line_input, "nsp %d", &nsp))
154 0 : nsp_set = 1;
155 0 : else if (unformat (line_input, "nsi %d", &nsi))
156 0 : nsi_set = 1;
157 : else
158 0 : return -99; // PARSE ERROR;
159 : }
160 :
161 0 : unformat_free (line_input);
162 :
163 0 : if (nsp_set == 0)
164 0 : return -1; //TODO Error type for this cond: clib_error_return (0, "nsp not specified");
165 :
166 0 : if (nsi_set == 0)
167 0 : return -2; //TODO Error type for this cond:clib_error_return (0, "nsi not specified");
168 :
169 0 : if (md_type == 1)
170 0 : length = 6;
171 0 : else if (md_type == 2)
172 0 : length = 2; /* base header length */
173 :
174 0 : nsp_nsi = (nsp<<8) | nsi;
175 :
176 : /* Construct the API message */
177 0 : M(NSH_ADD_DEL_ENTRY, nsh_add_del_entry);
178 0 : mp->is_add = is_add;
179 :
180 : #define _(x) mp->x = x;
181 0 : foreach_copy_nsh_base_hdr_field;
182 : #undef _
183 :
184 :
185 : /* send it... */
186 0 : S;
187 :
188 : /* Wait for a reply... */
189 0 : W;
190 : }
191 :
192 0 : static void vl_api_nsh_entry_details_t_handler
193 : (vl_api_nsh_entry_details_t * mp)
194 : {
195 0 : vat_main_t * vam = &vat_main;
196 :
197 0 : fformat(vam->ofp, "%11d%11d%11d%11d%14d%14d%14d%14d%14d\n",
198 0 : mp->ver_o_c,
199 0 : mp->length,
200 0 : mp->md_type,
201 0 : 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 0 : }
208 :
209 0 : static int api_nsh_entry_dump (vat_main_t * vam)
210 : {
211 0 : nsh_test_main_t * sm = &nsh_test_main;
212 : vl_api_nsh_entry_dump_t *mp;
213 : f64 timeout;
214 :
215 0 : if (!vam->json_output) {
216 0 : 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 0 : M(NSH_ENTRY_DUMP, nsh_entry_dump);
223 :
224 : /* send it... */
225 0 : S;
226 :
227 : /* Wait for a reply... */
228 0 : W;
229 : }
230 :
231 0 : static int api_nsh_add_del_map (vat_main_t * vam)
232 : {
233 0 : nsh_test_main_t * sm = &nsh_test_main;
234 0 : unformat_input_t * line_input = vam->input;
235 : f64 timeout;
236 0 : u8 is_add = 1;
237 : u32 nsp, nsi, mapped_nsp, mapped_nsi;
238 0 : int nsp_set = 0, nsi_set = 0, mapped_nsp_set = 0, mapped_nsi_set = 0;
239 0 : u32 next_node = ~0;
240 0 : 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 0 : while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
245 0 : if (unformat (line_input, "del"))
246 0 : is_add = 0;
247 0 : else if (unformat (line_input, "nsp %d", &nsp))
248 0 : nsp_set = 1;
249 0 : else if (unformat (line_input, "nsi %d", &nsi))
250 0 : nsi_set = 1;
251 0 : else if (unformat (line_input, "mapped-nsp %d", &mapped_nsp))
252 0 : mapped_nsp_set = 1;
253 0 : else if (unformat (line_input, "mapped-nsi %d", &mapped_nsi))
254 0 : mapped_nsi_set = 1;
255 0 : else if (unformat (line_input, "encap-gre4-intf %d", &sw_if_index))
256 0 : next_node = NSH_NODE_NEXT_ENCAP_GRE4;
257 0 : else if (unformat (line_input, "encap-gre6-intf %d", &sw_if_index))
258 0 : next_node = NSH_NODE_NEXT_ENCAP_GRE6;
259 0 : else if (unformat (line_input, "encap-vxlan-gpe-intf %d", &sw_if_index))
260 0 : next_node = NSH_NODE_NEXT_ENCAP_VXLANGPE;
261 0 : else if (unformat (line_input, "encap-none"))
262 0 : next_node = NSH_NODE_NEXT_DROP; // Once moved to NSHSFC see nsh.h:foreach_nsh_input_next to handle this case
263 : else
264 0 : return -99; //TODO clib_error_return (0, "parse error: '%U'",
265 : }
266 :
267 0 : unformat_free (line_input);
268 :
269 0 : if (nsp_set == 0 || nsi_set == 0)
270 0 : return -1; // TODO create return value: clib_error_return (0, "nsp nsi pair required. Key: for NSH entry");
271 :
272 0 : if (mapped_nsp_set == 0 || mapped_nsi_set == 0)
273 0 : return -2; // TODO create return valuee clib_error_return (0, "mapped-nsp mapped-nsi pair required. Key: for NSH entry");
274 :
275 0 : if (next_node == ~0)
276 0 : return -3; //TODO clib_error_return (0, "must specific action: [encap-gre-intf <nn> | encap-vxlan-gpe-intf <nn> | encap-none]");
277 :
278 :
279 0 : M(NSH_ADD_DEL_MAP, nsh_add_del_map);
280 : /* set args structure */
281 0 : mp->is_add = is_add;
282 0 : mp->nsp_nsi = (nsp<< NSH_NSP_SHIFT) | nsi;
283 0 : mp->mapped_nsp_nsi = (mapped_nsp<< NSH_NSP_SHIFT) | mapped_nsi;
284 0 : mp->sw_if_index = sw_if_index;
285 0 : mp->next_node = next_node;
286 :
287 : /* send it... */
288 0 : S;
289 :
290 : /* Wait for a reply... */
291 0 : W;
292 :
293 :
294 : }
295 :
296 0 : static void vl_api_nsh_map_details_t_handler
297 : (vl_api_nsh_map_details_t * mp)
298 : {
299 0 : vat_main_t * vam = &vat_main;
300 :
301 0 : 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 0 : }
307 :
308 0 : static int api_nsh_map_dump (vat_main_t * vam)
309 : {
310 0 : nsh_test_main_t * sm = &nsh_test_main;
311 : vl_api_nsh_map_dump_t *mp;
312 : f64 timeout;
313 :
314 0 : if (!vam->json_output) {
315 0 : 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 0 : M(NSH_MAP_DUMP, nsh_map_dump);
321 :
322 : /* send it... */
323 0 : S;
324 :
325 : /* Wait for a reply... */
326 0 : W;
327 : }
328 :
329 : #include <nsh/nsh.api_test.c>
|