blob: a8a9327b892dd1e94c0e43124312d34c0dd58079 [file] [log] [blame]
Filip Tehlarea257222021-10-18 09:02:37 +00001/* SPDX-License-Identifier: Apache-2.0
2 * Copyright(c) 2021 Cisco Systems, Inc.
3 */
4
5#include <vat/vat.h>
6#include <vlibapi/api.h>
7#include <vlibmemory/api.h>
8#include <vppinfra/error.h>
9#include <vpp/api/types.h>
10
11#include <vnet/ip/ip_types_api.h>
12
13#define __plugin_msg_base session_test_main.msg_id_base
14#include <vlibapi/vat_helper_macros.h>
15
16#include <vlibmemory/vlib.api_enum.h>
17#include <vlibmemory/vlib.api_types.h>
18
19/* Declare message IDs */
20#include <vnet/format_fns.h>
21#include <vnet/session/session.api_enum.h>
22#include <vnet/session/session.api_types.h>
23
24#define vl_endianfun /* define message structures */
25#include <vnet/session/session.api.h>
26#undef vl_endianfun
27
28typedef struct
29{
30 /* API message ID base */
31 u16 msg_id_base;
32 u32 ping_id;
33 vat_main_t *vat_main;
34} session_test_main_t;
35
36static session_test_main_t session_test_main;
37
38static int
39api_session_rule_add_del (vat_main_t *vam)
40{
41 vl_api_session_rule_add_del_t *mp;
42 unformat_input_t *i = vam->input;
43 u32 proto = ~0, lcl_port, rmt_port, action = 0, lcl_plen, rmt_plen;
44 u32 appns_index = 0, scope = 0;
45 ip4_address_t lcl_ip4, rmt_ip4;
46 ip6_address_t lcl_ip6, rmt_ip6;
47 u8 is_ip4 = 1, conn_set = 0;
48 u8 is_add = 1, *tag = 0;
49 int ret;
50 fib_prefix_t lcl, rmt;
51
52 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
53 {
54 if (unformat (i, "del"))
55 is_add = 0;
56 else if (unformat (i, "add"))
57 ;
58 else if (unformat (i, "proto tcp"))
59 proto = 0;
60 else if (unformat (i, "proto udp"))
61 proto = 1;
62 else if (unformat (i, "appns %d", &appns_index))
63 ;
64 else if (unformat (i, "scope %d", &scope))
65 ;
66 else if (unformat (i, "tag %_%v%_", &tag))
67 ;
68 else if (unformat (i, "%U/%d %d %U/%d %d", unformat_ip4_address,
69 &lcl_ip4, &lcl_plen, &lcl_port, unformat_ip4_address,
70 &rmt_ip4, &rmt_plen, &rmt_port))
71 {
72 is_ip4 = 1;
73 conn_set = 1;
74 }
75 else if (unformat (i, "%U/%d %d %U/%d %d", unformat_ip6_address,
76 &lcl_ip6, &lcl_plen, &lcl_port, unformat_ip6_address,
77 &rmt_ip6, &rmt_plen, &rmt_port))
78 {
79 is_ip4 = 0;
80 conn_set = 1;
81 }
82 else if (unformat (i, "action %d", &action))
83 ;
84 else
85 break;
86 }
87 if (proto == ~0 || !conn_set || action == ~0)
88 {
89 errmsg ("transport proto, connection and action must be set");
90 return -99;
91 }
92
93 if (scope > 3)
94 {
95 errmsg ("scope should be 0-3");
96 return -99;
97 }
98
99 M (SESSION_RULE_ADD_DEL, mp);
100
101 clib_memset (&lcl, 0, sizeof (lcl));
102 clib_memset (&rmt, 0, sizeof (rmt));
103 if (is_ip4)
104 {
105 ip_set (&lcl.fp_addr, &lcl_ip4, 1);
106 ip_set (&rmt.fp_addr, &rmt_ip4, 1);
107 lcl.fp_len = lcl_plen;
108 rmt.fp_len = rmt_plen;
109 }
110 else
111 {
112 ip_set (&lcl.fp_addr, &lcl_ip6, 0);
113 ip_set (&rmt.fp_addr, &rmt_ip6, 0);
114 lcl.fp_len = lcl_plen;
115 rmt.fp_len = rmt_plen;
116 }
117
118 ip_prefix_encode (&lcl, &mp->lcl);
119 ip_prefix_encode (&rmt, &mp->rmt);
120 mp->lcl_port = clib_host_to_net_u16 ((u16) lcl_port);
121 mp->rmt_port = clib_host_to_net_u16 ((u16) rmt_port);
122 mp->transport_proto =
123 proto ? TRANSPORT_PROTO_API_UDP : TRANSPORT_PROTO_API_TCP;
124 mp->action_index = clib_host_to_net_u32 (action);
125 mp->appns_index = clib_host_to_net_u32 (appns_index);
126 mp->scope = scope;
127 mp->is_add = is_add;
128 if (tag)
129 {
130 clib_memcpy (mp->tag, tag, vec_len (tag));
131 vec_free (tag);
132 }
133
134 S (mp);
135 W (ret);
136 return ret;
137}
138
139static void
140vl_api_app_attach_reply_t_handler (vl_api_app_attach_reply_t *mp)
141{
142}
143
144static void
145vl_api_app_add_cert_key_pair_reply_t_handler (
146 vl_api_app_add_cert_key_pair_reply_t *mp)
147{
148}
149
150static int
151api_app_attach (vat_main_t *vat)
152{
153 return -1;
154}
155
156static int
157api_application_detach (vat_main_t *vat)
158{
159 return -1;
160}
161
162static int
163api_app_del_cert_key_pair (vat_main_t *vat)
164{
165 return -1;
166}
167
168static int
169api_app_add_cert_key_pair (vat_main_t *vat)
170{
171 return -1;
172}
173
174static int
175api_session_rules_dump (vat_main_t *vam)
176{
177 vl_api_session_rules_dump_t *mp;
178 vl_api_control_ping_t *mp_ping;
179 int ret;
180
181 if (!vam->json_output)
182 {
183 print (vam->ofp, "%=20s", "Session Rules");
184 }
185
186 M (SESSION_RULES_DUMP, mp);
187 /* send it... */
188 S (mp);
189
190 /* Use a control ping for synchronization */
191 PING (&session_test_main, mp_ping);
192 S (mp_ping);
193
194 /* Wait for a reply... */
195 W (ret);
196 return ret;
197}
198
199static void
200vl_api_session_rules_details_t_handler (vl_api_session_rules_details_t *mp)
201{
202 vat_main_t *vam = &vat_main;
203 fib_prefix_t lcl, rmt;
204
205 ip_prefix_decode (&mp->lcl, &lcl);
206 ip_prefix_decode (&mp->rmt, &rmt);
207
208 if (lcl.fp_proto == FIB_PROTOCOL_IP4)
209 {
210 print (vam->ofp,
211 "appns %u tp %u scope %d %U/%d %d %U/%d %d action: %d tag: %s",
212 clib_net_to_host_u32 (mp->appns_index), mp->transport_proto,
213 mp->scope, format_ip4_address, &lcl.fp_addr.ip4, lcl.fp_len,
214 clib_net_to_host_u16 (mp->lcl_port), format_ip4_address,
215 &rmt.fp_addr.ip4, rmt.fp_len, clib_net_to_host_u16 (mp->rmt_port),
216 clib_net_to_host_u32 (mp->action_index), mp->tag);
217 }
218 else
219 {
220 print (vam->ofp,
221 "appns %u tp %u scope %d %U/%d %d %U/%d %d action: %d tag: %s",
222 clib_net_to_host_u32 (mp->appns_index), mp->transport_proto,
223 mp->scope, format_ip6_address, &lcl.fp_addr.ip6, lcl.fp_len,
224 clib_net_to_host_u16 (mp->lcl_port), format_ip6_address,
225 &rmt.fp_addr.ip6, rmt.fp_len, clib_net_to_host_u16 (mp->rmt_port),
226 clib_net_to_host_u32 (mp->action_index), mp->tag);
227 }
228}
229
230static void
231vl_api_app_namespace_add_del_reply_t_handler (
232 vl_api_app_namespace_add_del_reply_t *mp)
233{
234 vat_main_t *vam = &vat_main;
235 i32 retval = ntohl (mp->retval);
236 if (vam->async_mode)
237 {
238 vam->async_errors += (retval < 0);
239 }
240 else
241 {
242 vam->retval = retval;
243 if (retval == 0)
244 errmsg ("app ns index %d\n", ntohl (mp->appns_index));
245 vam->result_ready = 1;
246 }
247}
248
249static void
250vl_api_app_namespace_add_del_v2_reply_t_handler (
251 vl_api_app_namespace_add_del_v2_reply_t *vat)
252{
253}
254
255static void
256vl_api_app_worker_add_del_reply_t_handler (
257 vl_api_app_worker_add_del_reply_t *vat)
258{
259}
260
261static int
262api_app_namespace_add_del_v2 (vat_main_t *vat)
263{
264 return -1;
265}
266
267static int
268api_session_enable_disable (vat_main_t *vat)
269{
270 return -1;
271}
272
273static int
274api_app_worker_add_del (vat_main_t *vat)
275{
276 return -1;
277}
278
279static int
280api_application_tls_key_add (vat_main_t *vat)
281{
282 return -1;
283}
284
285static int
286api_app_namespace_add_del (vat_main_t *vam)
287{
288 vl_api_app_namespace_add_del_t *mp;
289 unformat_input_t *i = vam->input;
290 u8 *ns_id = 0, secret_set = 0, sw_if_index_set = 0;
291 u32 sw_if_index, ip4_fib_id, ip6_fib_id;
292 u64 secret;
293 int ret;
294
295 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
296 {
297 if (unformat (i, "id %_%v%_", &ns_id))
298 ;
299 else if (unformat (i, "secret %lu", &secret))
300 secret_set = 1;
301 else if (unformat (i, "sw_if_index %d", &sw_if_index))
302 sw_if_index_set = 1;
303 else if (unformat (i, "ip4_fib_id %d", &ip4_fib_id))
304 ;
305 else if (unformat (i, "ip6_fib_id %d", &ip6_fib_id))
306 ;
307 else
308 break;
309 }
310 if (!ns_id || !secret_set || !sw_if_index_set)
311 {
312 errmsg ("namespace id, secret and sw_if_index must be set");
313 return -99;
314 }
315 if (vec_len (ns_id) > 64)
316 {
317 errmsg ("namespace id too long");
318 return -99;
319 }
320 M (APP_NAMESPACE_ADD_DEL, mp);
321
322 vl_api_vec_to_api_string (ns_id, &mp->namespace_id);
323 mp->secret = clib_host_to_net_u64 (secret);
324 mp->sw_if_index = clib_host_to_net_u32 (sw_if_index);
325 mp->ip4_fib_id = clib_host_to_net_u32 (ip4_fib_id);
326 mp->ip6_fib_id = clib_host_to_net_u32 (ip6_fib_id);
327 vec_free (ns_id);
328 S (mp);
329 W (ret);
330 return ret;
331}
332
333static int
334api_application_tls_cert_add (vat_main_t *vat)
335{
336 return -1;
337}
338
339static void
340vl_api_app_namespace_add_del_v3_reply_t_handler (
341 vl_api_app_namespace_add_del_v3_reply_t *mp)
342{
343}
344
345static int
346api_app_namespace_add_del_v3 (vat_main_t *vat)
347{
348 return -1;
349}
350
351static int
352api_session_sapi_enable_disable (vat_main_t *vat)
353{
354 return -1;
355}
356
357#include <vnet/session/session.api_test.c>
358
359/*
360 * Local Variables:
361 * eval: (c-set-style "gnu")
362 * End:
363 */