Repair Doxygen build infrastructure
[vpp.git] / src / plugins / lb / lb_test.c
1 /*
2  * Copyright (c) 2016 Cisco 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 "AS IS" BASIS,
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 <vlibsocket/api.h>
20 #include <vppinfra/error.h>
21 #include <lb/lb.h>
22
23 //TODO: Move that to vat/plugin_api.c
24 //////////////////////////
25 uword unformat_ip46_address (unformat_input_t * input, va_list * args)
26 {
27   ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
28   ip46_type_t type = va_arg (*args, ip46_type_t);
29   if ((type != IP46_TYPE_IP6) &&
30       unformat(input, "%U", unformat_ip4_address, &ip46->ip4)) {
31     ip46_address_mask_ip4(ip46);
32     return 1;
33   } else if ((type != IP46_TYPE_IP4) &&
34       unformat(input, "%U", unformat_ip6_address, &ip46->ip6)) {
35     return 1;
36   }
37   return 0;
38 }
39 uword unformat_ip46_prefix (unformat_input_t * input, va_list * args)
40 {
41   ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
42   u8 *len = va_arg (*args, u8 *);
43   ip46_type_t type = va_arg (*args, ip46_type_t);
44
45   u32 l;
46   if ((type != IP46_TYPE_IP6) && unformat(input, "%U/%u", unformat_ip4_address, &ip46->ip4, &l)) {
47     if (l > 32)
48       return 0;
49     *len = l + 96;
50     ip46->pad[0] = ip46->pad[1] = ip46->pad[2] = 0;
51   } else if ((type != IP46_TYPE_IP4) && unformat(input, "%U/%u", unformat_ip6_address, &ip46->ip6, &l)) {
52     if (l > 128)
53       return 0;
54     *len = l;
55   } else {
56     return 0;
57   }
58   return 1;
59 }
60 /////////////////////////
61
62 #define vl_msg_id(n,h) n,
63 typedef enum {
64 #include <lb/lb.api.h>
65     /* We'll want to know how many messages IDs we need... */
66     VL_MSG_FIRST_AVAILABLE,
67 } vl_msg_id_t;
68 #undef vl_msg_id
69
70 /* define message structures */
71 #define vl_typedefs
72 #include <lb/lb.api.h>
73 #undef vl_typedefs
74
75 /* declare message handlers for each api */
76
77 #define vl_endianfun             /* define message structures */
78 #include <lb/lb.api.h>
79 #undef vl_endianfun
80
81 /* instantiate all the print functions we know about */
82 #define vl_print(handle, ...)
83 #define vl_printfun
84 #include <lb/lb.api.h>
85 #undef vl_printfun
86
87 /* Get the API version number. */
88 #define vl_api_version(n,v) static u32 api_version=(v);
89 #include <lb/lb.api.h>
90 #undef vl_api_version
91
92 typedef struct {
93     /* API message ID base */
94     u16 msg_id_base;
95     vat_main_t *vat_main;
96 } lb_test_main_t;
97
98 lb_test_main_t lb_test_main;
99
100 #define foreach_standard_reply_retval_handler   \
101 _(lb_conf_reply)                 \
102 _(lb_add_del_vip_reply)          \
103 _(lb_add_del_as_reply)
104
105 #define _(n)                                            \
106     static void vl_api_##n##_t_handler                  \
107     (vl_api_##n##_t * mp)                               \
108     {                                                   \
109         vat_main_t * vam = lb_test_main.vat_main;   \
110         i32 retval = ntohl(mp->retval);                 \
111         if (vam->async_mode) {                          \
112             vam->async_errors += (retval < 0);          \
113         } else {                                        \
114             vam->retval = retval;                       \
115             vam->result_ready = 1;                      \
116         }                                               \
117     }
118 foreach_standard_reply_retval_handler;
119 #undef _
120
121 /*
122  * Table of message reply handlers, must include boilerplate handlers
123  * we just generated
124  */
125 #define foreach_vpe_api_reply_msg                               \
126   _(LB_CONF_REPLY, lb_conf_reply)                                     \
127   _(LB_ADD_DEL_VIP_REPLY, lb_add_del_vip_reply)                       \
128   _(LB_ADD_DEL_AS_REPLY, lb_add_del_as_reply)
129
130 /* M: construct, but don't yet send a message */
131 #define M(T,t)                                                  \
132 do {                                                            \
133     vam->result_ready = 0;                                      \
134     mp = vl_msg_api_alloc(sizeof(*mp));                         \
135     memcpy (mp, &mps, sizeof (*mp));                               \
136     mp->_vl_msg_id = ntohs (VL_API_##T + lbtm->msg_id_base);      \
137     mp->client_index = vam->my_client_index;                    \
138 } while(0);
139
140 /* S: send a message */
141 #define S (vl_msg_api_send_shmem (vam->vl_input_queue, (u8 *)&mp))
142
143 /* W: wait for results, with timeout */
144 #define W                                       \
145 do {                                            \
146     timeout = vat_time_now (vam) + 1.0;         \
147                                                 \
148     while (vat_time_now (vam) < timeout) {      \
149         if (vam->result_ready == 1) {           \
150             return (vam->retval);               \
151         }                                       \
152     }                                           \
153     return -99;                                 \
154 } while(0);
155
156 static int api_lb_conf (vat_main_t * vam)
157 {
158   lb_test_main_t *lbtm = &lb_test_main;
159   unformat_input_t *i = vam->input;
160   f64 timeout;
161   vl_api_lb_conf_t mps, *mp;
162
163   if (!unformat(i, "%U %U %u %u",
164                unformat_ip4_address, &mps.ip4_src_address,
165                unformat_ip6_address, mps.ip6_src_address,
166                &mps.sticky_buckets_per_core,
167                &mps.flow_timeout)) {
168     errmsg ("invalid arguments\n");
169     return -99;
170   }
171
172   M(LB_CONF, lb_conf); S; W;
173
174   /* NOTREACHED */
175   return 0;
176 }
177
178 static int api_lb_add_del_vip (vat_main_t * vam)
179 {
180   lb_test_main_t *lbtm = &lb_test_main;
181   unformat_input_t * i = vam->input;
182   f64 timeout;
183   vl_api_lb_add_del_vip_t mps, *mp;
184   mps.is_del = 0;
185   mps.is_gre4 = 0;
186
187   if (!unformat(i, "%U",
188                 unformat_ip46_prefix, mps.ip_prefix, &mps.prefix_length, IP46_TYPE_ANY)) {
189     errmsg ("invalid prefix\n");
190     return -99;
191   }
192
193   if (unformat(i, "gre4")) {
194     mps.is_gre4 = 1;
195   } else if (unformat(i, "gre6")) {
196     mps.is_gre4 = 0;
197   } else {
198     errmsg ("no encap\n");
199     return -99;
200   }
201
202   if (!unformat(i, "%d", &mps.new_flows_table_length)) {
203     errmsg ("no table lentgh\n");
204     return -99;
205   }
206
207   if (unformat(i, "del")) {
208     mps.is_del = 1;
209   }
210
211   M(LB_ADD_DEL_VIP, lb_add_del_vip); S; W;
212   /* NOTREACHED */
213   return 0;
214 }
215
216 static int api_lb_add_del_as (vat_main_t * vam)
217 {
218   lb_test_main_t *lbtm = &lb_test_main;
219   unformat_input_t * i = vam->input;
220   f64 timeout;
221   vl_api_lb_add_del_as_t mps, *mp;
222   mps.is_del = 0;
223
224   if (!unformat(i, "%U %U",
225                 unformat_ip46_prefix, mps.vip_ip_prefix, &mps.vip_prefix_length, IP46_TYPE_ANY,
226                 unformat_ip46_address, mps.as_address)) {
227     errmsg ("invalid prefix or address\n");
228     return -99;
229   }
230
231   if (unformat(i, "del")) {
232     mps.is_del = 1;
233   }
234
235   M(LB_ADD_DEL_AS, lb_add_del_as); S; W;
236   /* NOTREACHED */
237   return 0;
238 }
239
240 /*
241  * List of messages that the api test plugin sends,
242  * and that the data plane plugin processes
243  */
244 #define foreach_vpe_api_msg                             \
245 _(lb_conf, "<ip4-src-addr> <ip6-src-address> <sticky_buckets_per_core> <flow_timeout>") \
246 _(lb_add_del_vip, "<ip-prefix> [gre4|gre6] <new_table_len> [del]") \
247 _(lb_add_del_as, "<vip-ip-prefix> <address> [del]")
248
249 void vat_api_hookup (vat_main_t *vam)
250 {
251   lb_test_main_t * lbtm = &lb_test_main;
252   /* Hook up handlers for replies from the data plane plug-in */
253 #define _(N,n)                                                  \
254   vl_msg_api_set_handlers((VL_API_##N + lbtm->msg_id_base),       \
255                           #n,                                   \
256                           vl_api_##n##_t_handler,               \
257                           vl_noop_handler,                      \
258                           vl_api_##n##_t_endian,                \
259                           vl_api_##n##_t_print,                 \
260                           sizeof(vl_api_##n##_t), 1);
261   foreach_vpe_api_reply_msg;
262 #undef _
263
264   /* API messages we can send */
265 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
266   foreach_vpe_api_msg;
267 #undef _
268
269   /* Help strings */
270 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
271   foreach_vpe_api_msg;
272 #undef _
273 }
274
275 clib_error_t * vat_plugin_register (vat_main_t *vam)
276 {
277   lb_test_main_t * lbtm = &lb_test_main;
278
279   u8 * name;
280
281   lbtm->vat_main = vam;
282
283   /* Ask the vpp engine for the first assigned message-id */
284   name = format (0, "lb_%08x%c", api_version, 0);
285   lbtm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
286
287   if (lbtm->msg_id_base != (u16) ~0)
288     vat_api_hookup (vam);
289
290   vec_free(name);
291
292   return 0;
293 }