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