vlib: improvement to automatic core pinning
[vpp.git] / src / vnet / session / session_test.c
1 /* 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
28 typedef 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
36 static session_test_main_t session_test_main;
37
38 static int
39 api_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
139 static void
140 vl_api_app_attach_reply_t_handler (vl_api_app_attach_reply_t *mp)
141 {
142 }
143
144 static void
145 vl_api_app_add_cert_key_pair_reply_t_handler (
146   vl_api_app_add_cert_key_pair_reply_t *mp)
147 {
148 }
149
150 static int
151 api_app_attach (vat_main_t *vat)
152 {
153   return -1;
154 }
155
156 static int
157 api_application_detach (vat_main_t *vat)
158 {
159   return -1;
160 }
161
162 static int
163 api_app_del_cert_key_pair (vat_main_t *vat)
164 {
165   return -1;
166 }
167
168 static int
169 api_app_add_cert_key_pair (vat_main_t *vat)
170 {
171   return -1;
172 }
173
174 static int
175 api_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
199 static void
200 vl_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
230 static void
231 vl_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
249 static void
250 vl_api_app_namespace_add_del_v2_reply_t_handler (
251   vl_api_app_namespace_add_del_v2_reply_t *vat)
252 {
253 }
254
255 static void
256 vl_api_app_worker_add_del_reply_t_handler (
257   vl_api_app_worker_add_del_reply_t *vat)
258 {
259 }
260
261 static int
262 api_app_namespace_add_del_v2 (vat_main_t *vat)
263 {
264   return -1;
265 }
266
267 static int
268 api_session_enable_disable (vat_main_t *vat)
269 {
270   return -1;
271 }
272
273 static int
274 api_app_worker_add_del (vat_main_t *vat)
275 {
276   return -1;
277 }
278
279 static int
280 api_app_namespace_add_del (vat_main_t *vam)
281 {
282   vl_api_app_namespace_add_del_t *mp;
283   unformat_input_t *i = vam->input;
284   u8 *ns_id = 0, secret_set = 0, sw_if_index_set = 0;
285   u32 sw_if_index, ip4_fib_id, ip6_fib_id;
286   u64 secret;
287   int ret;
288
289   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
290     {
291       if (unformat (i, "id %_%v%_", &ns_id))
292         ;
293       else if (unformat (i, "secret %lu", &secret))
294         secret_set = 1;
295       else if (unformat (i, "sw_if_index %d", &sw_if_index))
296         sw_if_index_set = 1;
297       else if (unformat (i, "ip4_fib_id %d", &ip4_fib_id))
298         ;
299       else if (unformat (i, "ip6_fib_id %d", &ip6_fib_id))
300         ;
301       else
302         break;
303     }
304   if (!ns_id || !secret_set || !sw_if_index_set)
305     {
306       errmsg ("namespace id, secret and sw_if_index must be set");
307       return -99;
308     }
309   if (vec_len (ns_id) > 64)
310     {
311       errmsg ("namespace id too long");
312       return -99;
313     }
314   M (APP_NAMESPACE_ADD_DEL, mp);
315
316   vl_api_vec_to_api_string (ns_id, &mp->namespace_id);
317   mp->secret = clib_host_to_net_u64 (secret);
318   mp->sw_if_index = clib_host_to_net_u32 (sw_if_index);
319   mp->ip4_fib_id = clib_host_to_net_u32 (ip4_fib_id);
320   mp->ip6_fib_id = clib_host_to_net_u32 (ip6_fib_id);
321   vec_free (ns_id);
322   S (mp);
323   W (ret);
324   return ret;
325 }
326
327 static void
328 vl_api_app_namespace_add_del_v4_reply_t_handler (
329   vl_api_app_namespace_add_del_v4_reply_t *mp)
330 {
331 }
332
333 static int
334 api_app_namespace_add_del_v4 (vat_main_t *vat)
335 {
336   return -1;
337 }
338
339 static void
340 vl_api_app_namespace_add_del_v3_reply_t_handler (
341   vl_api_app_namespace_add_del_v3_reply_t *mp)
342 {
343 }
344
345 static int
346 api_app_namespace_add_del_v3 (vat_main_t *vat)
347 {
348   return -1;
349 }
350
351 static int
352 api_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  */