Add buffer pointer-to-index and index-to-pointer array functions
[vpp.git] / src / plugins / kubeproxy / kp_test.c
1 /*
2  * Copyright (c) 2016 Intel and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "POD IS" BPODIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <vat/vat.h>
17 #include <vlibapi/api.h>
18 #include <vlibmemory/api.h>
19 #include <vppinfra/error.h>
20 #include <kubeproxy/kp.h>
21
22 #define __plugin_msg_base kp_test_main.msg_id_base
23 #include <vlibapi/vat_helper_macros.h>
24
25 //TODO: Move that to vat/plugin_api.c
26 //////////////////////////
27 uword unformat_ip46_address (unformat_input_t * input, va_list * args)
28 {
29   ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
30   ip46_type_t type = va_arg (*args, ip46_type_t);
31   if ((type != IP46_TYPE_IP6) &&
32       unformat(input, "%U", unformat_ip4_address, &ip46->ip4)) {
33     ip46_address_mask_ip4(ip46);
34     return 1;
35   } else if ((type != IP46_TYPE_IP4) &&
36       unformat(input, "%U", unformat_ip6_address, &ip46->ip6)) {
37     return 1;
38   }
39   return 0;
40 }
41 uword unformat_ip46_prefix (unformat_input_t * input, va_list * args)
42 {
43   ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
44   u8 *len = va_arg (*args, u8 *);
45   ip46_type_t type = va_arg (*args, ip46_type_t);
46
47   u32 l;
48   if ((type != IP46_TYPE_IP6) && unformat(input, "%U/%u", unformat_ip4_address, &ip46->ip4, &l)) {
49     if (l > 32)
50       return 0;
51     *len = l + 96;
52     ip46->pad[0] = ip46->pad[1] = ip46->pad[2] = 0;
53   } else if ((type != IP46_TYPE_IP4) && unformat(input, "%U/%u", unformat_ip6_address, &ip46->ip6, &l)) {
54     if (l > 128)
55       return 0;
56     *len = l;
57   } else {
58     return 0;
59   }
60   return 1;
61 }
62 /////////////////////////
63
64 #define vl_msg_id(n,h) n,
65 typedef enum {
66 #include <kubeproxy/kp.api.h>
67     /* We'll want to know how many messages IDs we need... */
68     VL_MSG_FIRST_AVAILABLE,
69 } vl_msg_id_t;
70 #undef vl_msg_id
71
72 /* define message structures */
73 #define vl_typedefs
74 #include <kubeproxy/kp.api.h>
75 #undef vl_typedefs
76
77 /* declare message handlers for each api */
78
79 #define vl_endianfun             /* define message structures */
80 #include <kubeproxy/kp.api.h>
81 #undef vl_endianfun
82
83 /* instantiate all the print functions we know about */
84 #define vl_print(handle, ...)
85 #define vl_printfun
86 #include <kubeproxy/kp.api.h>
87 #undef vl_printfun
88
89 /* Get the API version number. */
90 #define vl_api_version(n,v) static u32 api_version=(v);
91 #include <kubeproxy/kp.api.h>
92 #undef vl_api_version
93
94 typedef struct {
95     /* API message ID base */
96     u16 msg_id_base;
97     vat_main_t *vat_main;
98 } kp_test_main_t;
99
100 kp_test_main_t kp_test_main;
101
102 #define foreach_standard_reply_retval_handler   \
103 _(kp_conf_reply)                 \
104 _(kp_add_del_vip_reply)          \
105 _(kp_add_del_pod_reply)
106
107 #define _(n)                                            \
108     static void vl_api_##n##_t_handler                  \
109     (vl_api_##n##_t * mp)                               \
110     {                                                   \
111         vat_main_t * vam = kp_test_main.vat_main;   \
112         i32 retval = ntohl(mp->retval);                 \
113         if (vam->async_mode) {                          \
114             vam->async_errors += (retval < 0);          \
115         } else {                                        \
116             vam->retval = retval;                       \
117             vam->result_ready = 1;                      \
118         }                                               \
119     }
120 foreach_standard_reply_retval_handler;
121 #undef _
122
123 /*
124  * Table of message reply handlers, must include boilerplate handlers
125  * we just generated
126  */
127 #define foreach_vpe_api_reply_msg                               \
128   _(KP_CONF_REPLY, kp_conf_reply)                                     \
129   _(KP_ADD_DEL_VIP_REPLY, kp_add_del_vip_reply)                       \
130   _(KP_ADD_DEL_POD_REPLY, kp_add_del_pod_reply)
131
132 static int api_kp_conf (vat_main_t * vam)
133 {
134   unformat_input_t *i = vam->input;
135   vl_api_kp_conf_t mps, *mp;
136   int ret;
137
138   if (!unformat(i, "%u %u",
139                &mps.sticky_buckets_per_core,
140                &mps.flow_timeout)) {
141     errmsg ("invalid arguments\n");
142     return -99;
143   }
144
145   M(KP_CONF, mp);
146   S(mp);
147   W (ret);
148   return ret;
149 }
150
151 static int api_kp_add_del_vip (vat_main_t * vam)
152 {
153   unformat_input_t * i = vam->input;
154   vl_api_kp_add_del_vip_t mps, *mp;
155   int ret;
156   mps.is_del = 0;
157   mps.is_nat4 = 0;
158
159   if (!unformat(i, "%U",
160                 unformat_ip46_prefix, mps.ip_prefix, &mps.prefix_length, IP46_TYPE_ANY)) {
161     errmsg ("invalid prefix\n");
162     return -99;
163   }
164
165   if (unformat(i, "nat4")) {
166     mps.is_nat4 = 1;
167   } else if (unformat(i, "nat6")) {
168     mps.is_nat4 = 0;
169   } else {
170     errmsg ("no nat\n");
171     return -99;
172   }
173
174   if (!unformat(i, "%d", &mps.new_flows_table_length)) {
175     errmsg ("no table lentgh\n");
176     return -99;
177   }
178
179   if (unformat(i, "del")) {
180     mps.is_del = 1;
181   }
182
183   M(KP_ADD_DEL_VIP, mp);
184   S(mp);
185   W (ret);
186   return ret;
187 }
188
189 static int api_kp_add_del_pod (vat_main_t * vam)
190 {
191   unformat_input_t * i = vam->input;
192   vl_api_kp_add_del_pod_t mps, *mp;
193   int ret;
194   mps.is_del = 0;
195
196   if (!unformat(i, "%U %U",
197                 unformat_ip46_prefix, mps.vip_ip_prefix, &mps.vip_prefix_length, IP46_TYPE_ANY,
198                 unformat_ip46_address, mps.pod_address)) {
199     errmsg ("invalid prefix or address\n");
200     return -99;
201   }
202
203   if (unformat(i, "del")) {
204     mps.is_del = 1;
205   }
206
207   M(KP_ADD_DEL_POD, mp);
208   S(mp);
209   W (ret);
210   return ret;
211 }
212
213 /*
214  * List of messages that the api test plugin sends,
215  * and that the data plane plugin processes
216  */
217 #define foreach_vpe_api_msg                             \
218 _(kp_conf, "<sticky_buckets_per_core> <flow_timeout>") \
219 _(kp_add_del_vip, "<ip-prefix> <port> <target_port> <node_port> " \
220                   "[nat4|nat6] <new_table_len> [del]") \
221 _(kp_add_del_pod, "<vip-ip-prefix> <address> [del]")
222
223 static void 
224 kp_vat_api_hookup (vat_main_t *vam)
225 {
226   kp_test_main_t * kptm = &kp_test_main;
227   /* Hook up handlers for replies from the data plane plug-in */
228 #define _(N,n)                                                  \
229   vl_msg_api_set_handlers((VL_API_##N + kptm->msg_id_base),       \
230                           #n,                                   \
231                           vl_api_##n##_t_handler,               \
232                           vl_noop_handler,                      \
233                           vl_api_##n##_t_endian,                \
234                           vl_api_##n##_t_print,                 \
235                           sizeof(vl_api_##n##_t), 1);
236   foreach_vpe_api_reply_msg;
237 #undef _
238
239   /* API messages we can send */
240 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
241   foreach_vpe_api_msg;
242 #undef _
243
244   /* Help strings */
245 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
246   foreach_vpe_api_msg;
247 #undef _
248 }
249
250 clib_error_t * vat_plugin_register (vat_main_t *vam)
251 {
252   kp_test_main_t * kptm = &kp_test_main;
253
254   u8 * name;
255
256   kptm->vat_main = vam;
257
258   /* Ask the vpp engine for the first assigned message-id */
259   name = format (0, "kp_%08x%c", api_version, 0);
260   kptm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
261
262   if (kptm->msg_id_base != (u16) ~0)
263     kp_vat_api_hookup (vam);
264
265   vec_free(name);
266
267   return 0;
268 }