build: add -Wall and -fno-common, fix reported issues
[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
20 #include <vppinfra/error.h>
21 #include <lb/lb.h>
22
23 #define __plugin_msg_base lb_test_main.msg_id_base
24 #include <vlibapi/vat_helper_macros.h>
25
26 //TODO: Move that to vat/plugin_api.c
27 //////////////////////////
28 uword unformat_ip46_address (unformat_input_t * input, va_list * args)
29 {
30   ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
31   ip46_type_t type = va_arg (*args, ip46_type_t);
32   if ((type != IP46_TYPE_IP6) &&
33       unformat(input, "%U", unformat_ip4_address, &ip46->ip4)) {
34     ip46_address_mask_ip4(ip46);
35     return 1;
36   } else if ((type != IP46_TYPE_IP4) &&
37       unformat(input, "%U", unformat_ip6_address, &ip46->ip6)) {
38     return 1;
39   }
40   return 0;
41 }
42 uword unformat_ip46_prefix (unformat_input_t * input, va_list * args)
43 {
44   ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
45   u8 *len = va_arg (*args, u8 *);
46   ip46_type_t type = va_arg (*args, ip46_type_t);
47
48   u32 l;
49   if ((type != IP46_TYPE_IP6) && unformat(input, "%U/%u", unformat_ip4_address, &ip46->ip4, &l)) {
50     if (l > 32)
51       return 0;
52     *len = l + 96;
53     ip46->pad[0] = ip46->pad[1] = ip46->pad[2] = 0;
54   } else if ((type != IP46_TYPE_IP4) && unformat(input, "%U/%u", unformat_ip6_address, &ip46->ip6, &l)) {
55     if (l > 128)
56       return 0;
57     *len = l;
58   } else {
59     return 0;
60   }
61   return 1;
62 }
63 /////////////////////////
64
65 #define vl_msg_id(n,h) n,
66 typedef enum {
67 #include <lb/lb.api.h>
68     /* We'll want to know how many messages IDs we need... */
69     VL_MSG_FIRST_AVAILABLE,
70 } vl_msg_id_t;
71 #undef vl_msg_id
72
73 /* define message structures */
74 #define vl_typedefs
75 #include <lb/lb.api.h>
76 #undef vl_typedefs
77
78 /* declare message handlers for each api */
79
80 #define vl_endianfun             /* define message structures */
81 #include <lb/lb.api.h>
82 #undef vl_endianfun
83
84 /* instantiate all the print functions we know about */
85 #define vl_print(handle, ...)
86 #define vl_printfun
87 #include <lb/lb.api.h>
88 #undef vl_printfun
89
90 /* Get the API version number. */
91 #define vl_api_version(n,v) static u32 api_version=(v);
92 #include <lb/lb.api.h>
93 #undef vl_api_version
94
95 typedef struct {
96     /* API message ID base */
97     u16 msg_id_base;
98     vat_main_t *vat_main;
99 } lb_test_main_t;
100
101 lb_test_main_t lb_test_main;
102
103 #define foreach_standard_reply_retval_handler   \
104 _(lb_conf_reply)                 \
105 _(lb_add_del_vip_reply)          \
106 _(lb_add_del_as_reply)
107
108 #define _(n)                                            \
109     static void vl_api_##n##_t_handler                  \
110     (vl_api_##n##_t * mp)                               \
111     {                                                   \
112         vat_main_t * vam = lb_test_main.vat_main;   \
113         i32 retval = ntohl(mp->retval);                 \
114         if (vam->async_mode) {                          \
115             vam->async_errors += (retval < 0);          \
116         } else {                                        \
117             vam->retval = retval;                       \
118             vam->result_ready = 1;                      \
119         }                                               \
120     }
121 foreach_standard_reply_retval_handler;
122 #undef _
123
124 /*
125  * Table of message reply handlers, must include boilerplate handlers
126  * we just generated
127  */
128 #define foreach_vpe_api_reply_msg                               \
129   _(LB_CONF_REPLY, lb_conf_reply)                               \
130   _(LB_ADD_DEL_VIP_REPLY, lb_add_del_vip_reply)                 \
131   _(LB_ADD_DEL_AS_REPLY, lb_add_del_as_reply)
132
133 static int api_lb_conf (vat_main_t * vam)
134 {
135   unformat_input_t *line_input = vam->input;
136   vl_api_lb_conf_t *mp;
137   u32 ip4_src_address = 0xffffffff;
138   ip46_address_t ip6_src_address;
139   u32 sticky_buckets_per_core = LB_DEFAULT_PER_CPU_STICKY_BUCKETS;
140   u32 flow_timeout = LB_DEFAULT_FLOW_TIMEOUT;
141   int ret;
142
143   ip6_src_address.as_u64[0] = 0xffffffffffffffffL;
144   ip6_src_address.as_u64[1] = 0xffffffffffffffffL;
145
146   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
147   {
148     if (unformat(line_input, "ip4-src-address %U", unformat_ip4_address, &ip4_src_address))
149       ;
150     else if (unformat(line_input, "ip6-src-address %U", unformat_ip6_address, &ip6_src_address))
151       ;
152     else if (unformat(line_input, "buckets %d", &sticky_buckets_per_core))
153       ;
154     else if (unformat(line_input, "timeout %d", &flow_timeout))
155       ;
156     else {
157         errmsg ("invalid arguments\n");
158         return -99;
159     }
160   }
161
162   M(LB_CONF, mp);
163   clib_memcpy (&(mp->ip4_src_address), &ip4_src_address, sizeof (ip4_src_address));
164   clib_memcpy (&(mp->ip6_src_address), &ip6_src_address, sizeof (ip6_src_address));
165   mp->sticky_buckets_per_core = htonl (sticky_buckets_per_core);
166   mp->flow_timeout = htonl (flow_timeout);
167
168   S(mp);
169   W (ret);
170   return ret;
171 }
172
173 static int api_lb_add_del_vip (vat_main_t * vam)
174 {
175   unformat_input_t *line_input = vam->input;
176   vl_api_lb_add_del_vip_t *mp;
177   int ret;
178   ip46_address_t ip_prefix;
179   u8 prefix_length = 0;
180   u8 protocol = 0;
181   u32 port = 0;
182   u32 encap = 0;
183   u32 dscp = ~0;
184   u32 srv_type = LB_SRV_TYPE_CLUSTERIP;
185   u32 target_port = 0;
186   u32 new_length = 1024;
187   int is_del = 0;
188
189   if (!unformat(line_input, "%U", unformat_ip46_prefix, &ip_prefix,
190                 &prefix_length, IP46_TYPE_ANY, &prefix_length)) {
191     errmsg ("lb_add_del_vip: invalid vip prefix\n");
192     return -99;
193   }
194
195   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
196   {
197     if (unformat(line_input, "new_len %d", &new_length))
198       ;
199     else if (unformat(line_input, "del"))
200       is_del = 1;
201     else if (unformat(line_input, "protocol tcp"))
202       {
203         protocol = IP_PROTOCOL_TCP;
204       }
205     else if (unformat(line_input, "protocol udp"))
206       {
207         protocol = IP_PROTOCOL_UDP;
208       }
209     else if (unformat(line_input, "port %d", &port))
210       ;
211     else if (unformat(line_input, "encap gre4"))
212       encap = LB_ENCAP_TYPE_GRE4;
213     else if (unformat(line_input, "encap gre6"))
214       encap = LB_ENCAP_TYPE_GRE6;
215     else if (unformat(line_input, "encap l3dsr"))
216       encap = LB_ENCAP_TYPE_L3DSR;
217     else if (unformat(line_input, "encap nat4"))
218       encap = LB_ENCAP_TYPE_NAT4;
219     else if (unformat(line_input, "encap nat6"))
220       encap = LB_ENCAP_TYPE_NAT6;
221     else if (unformat(line_input, "dscp %d", &dscp))
222       ;
223     else if (unformat(line_input, "type clusterip"))
224       srv_type = LB_SRV_TYPE_CLUSTERIP;
225     else if (unformat(line_input, "type nodeport"))
226       srv_type = LB_SRV_TYPE_NODEPORT;
227     else if (unformat(line_input, "target_port %d", &target_port))
228       ;
229     else {
230         errmsg ("invalid arguments\n");
231         return -99;
232     }
233   }
234
235   if ((encap != LB_ENCAP_TYPE_L3DSR) && (dscp != ~0))
236     {
237       errmsg("lb_vip_add error: should not configure dscp for none L3DSR.");
238       return -99;
239     }
240
241   if ((encap == LB_ENCAP_TYPE_L3DSR) && (dscp >= 64))
242     {
243       errmsg("lb_vip_add error: dscp for L3DSR should be less than 64.");
244       return -99;
245     }
246
247   M(LB_ADD_DEL_VIP, mp);
248   clib_memcpy (mp->ip_prefix, &ip_prefix, sizeof (ip_prefix));
249   mp->prefix_length = prefix_length;
250   mp->protocol = (u8)protocol;
251   mp->port = htons((u16)port);
252   mp->encap = (u8)encap;
253   mp->dscp = (u8)dscp;
254   mp->type = (u8)srv_type;
255   mp->target_port = htons((u16)target_port);
256   mp->node_port = htons((u16)target_port);
257   mp->new_flows_table_length = htonl(new_length);
258   mp->is_del = is_del;
259
260   S(mp);
261   W (ret);
262   return ret;
263 }
264
265 static int api_lb_add_del_as (vat_main_t * vam)
266 {
267
268   unformat_input_t *line_input = vam->input;
269   vl_api_lb_add_del_as_t *mp;
270   int ret;
271   ip46_address_t vip_prefix, as_addr;
272   u8 vip_plen;
273   ip46_address_t *as_array = 0;
274   u32 port = 0;
275   u8 protocol = 0;
276   u8 is_del = 0;
277   u8 is_flush = 0;
278
279   if (!unformat(line_input, "%U", unformat_ip46_prefix,
280                 &vip_prefix, &vip_plen, IP46_TYPE_ANY))
281   {
282       errmsg ("lb_add_del_as: invalid vip prefix\n");
283       return -99;
284   }
285
286   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
287   {
288     if (unformat(line_input, "%U", unformat_ip46_address,
289                  &as_addr, IP46_TYPE_ANY))
290       {
291         vec_add1(as_array, as_addr);
292       }
293     else if (unformat(line_input, "del"))
294       {
295         is_del = 1;
296       }
297     else if (unformat(line_input, "flush"))
298       {
299         is_flush = 1;
300       }
301     else if (unformat(line_input, "protocol tcp"))
302       {
303           protocol = IP_PROTOCOL_TCP;
304       }
305     else if (unformat(line_input, "protocol udp"))
306       {
307           protocol = IP_PROTOCOL_UDP;
308       }
309     else if (unformat(line_input, "port %d", &port))
310       ;
311     else {
312         errmsg ("invalid arguments\n");
313         return -99;
314     }
315   }
316
317   if (!vec_len(as_array)) {
318     errmsg ("No AS address provided \n");
319     return -99;
320   }
321
322   M(LB_ADD_DEL_AS, mp);
323   clib_memcpy (mp->vip_ip_prefix, &vip_prefix, sizeof (vip_prefix));
324   mp->vip_prefix_length = vip_plen;
325   mp->protocol = (u8)protocol;
326   mp->port = htons((u16)port);
327   clib_memcpy (mp->as_address, &as_addr, sizeof (as_addr));
328   mp->is_del = is_del;
329   mp->is_flush = is_flush;
330
331   S(mp);
332   W (ret);
333   return ret;
334 }
335
336 /*
337  * List of messages that the api test plugin sends,
338  * and that the data plane plugin processes
339  */
340 #define foreach_vpe_api_msg                             \
341 _(lb_conf, "[ip4-src-address <addr>] [ip6-src-address <addr>] " \
342            "[buckets <n>] [timeout <s>]")  \
343 _(lb_add_del_vip, "<prefix> "  \
344                   "[protocol (tcp|udp) port <n>] "  \
345                   "[encap (gre6|gre4|l3dsr|nat4|nat6)] " \
346                   "[dscp <n>] "  \
347                   "[type (nodeport|clusterip) target_port <n>] " \
348                   "[new_len <n>] [del]")  \
349 _(lb_add_del_as, "<vip-prefix> [protocol (tcp|udp) port <n>] "  \
350                  "[<address>] [del] [flush]")
351
352 static void 
353 lb_vat_api_hookup (vat_main_t *vam)
354 {
355   lb_test_main_t * lbtm = &lb_test_main;
356   /* Hook up handlers for replies from the data plane plug-in */
357 #define _(N,n)                                                  \
358   vl_msg_api_set_handlers((VL_API_##N + lbtm->msg_id_base),       \
359                           #n,                                   \
360                           vl_api_##n##_t_handler,               \
361                           vl_noop_handler,                      \
362                           vl_api_##n##_t_endian,                \
363                           vl_api_##n##_t_print,                 \
364                           sizeof(vl_api_##n##_t), 1);
365   foreach_vpe_api_reply_msg;
366 #undef _
367
368   /* API messages we can send */
369 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
370   foreach_vpe_api_msg;
371 #undef _
372
373   /* Help strings */
374 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
375   foreach_vpe_api_msg;
376 #undef _
377 }
378
379 clib_error_t * vat_plugin_register (vat_main_t *vam)
380 {
381   lb_test_main_t * lbtm = &lb_test_main;
382
383   u8 * name;
384
385   lbtm->vat_main = vam;
386
387   /* Ask the vpp engine for the first assigned message-id */
388   name = format (0, "lb_%08x%c", api_version, 0);
389   lbtm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
390
391   if (lbtm->msg_id_base != (u16) ~0)
392     lb_vat_api_hookup (vam);
393
394   vec_free(name);
395
396   return 0;
397 }