Support lb on both vip and per-port-vip case
[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 *i = vam->input;
136   vl_api_lb_conf_t mps, *mp;
137   int ret;
138
139   if (!unformat(i, "%U %U %u %u",
140                unformat_ip4_address, &mps.ip4_src_address,
141                unformat_ip6_address, mps.ip6_src_address,
142                &mps.sticky_buckets_per_core,
143                &mps.flow_timeout)) {
144     errmsg ("invalid arguments\n");
145     return -99;
146   }
147
148   M(LB_CONF, mp);
149   S(mp);
150   W (ret);
151   return ret;
152 }
153
154 static int api_lb_add_del_vip (vat_main_t * vam)
155 {
156   unformat_input_t * i = vam->input;
157   vl_api_lb_add_del_vip_t mps, *mp;
158   int ret;
159   mps.is_del = 0;
160   mps.encap = LB_ENCAP_TYPE_GRE4;
161
162   if (!unformat(i, "%U",
163                 unformat_ip46_prefix, mps.ip_prefix, &mps.prefix_length, IP46_TYPE_ANY)) {
164     errmsg ("invalid prefix\n");
165     return -99;
166   }
167
168   if (unformat(i, "gre4")) {
169     mps.encap = LB_ENCAP_TYPE_GRE4;
170   } else if (unformat(i, "gre6")) {
171     mps.encap = LB_ENCAP_TYPE_GRE6;
172   } else if (unformat(i, "l3dsr")) {
173     mps.encap = LB_ENCAP_TYPE_L3DSR;
174   } else if (unformat(i, "nat4")) {
175     mps.encap = LB_ENCAP_TYPE_NAT4;
176   } else if (unformat(i, "nat6")) {
177     mps.encap = LB_ENCAP_TYPE_NAT6;
178   } else {
179     errmsg ("no encap\n");
180     return -99;
181   }
182
183   if (!unformat(i, "%d", &mps.new_flows_table_length)) {
184     errmsg ("no table lentgh\n");
185     return -99;
186   }
187
188   if (unformat(i, "del")) {
189     mps.is_del = 1;
190   }
191
192   M(LB_ADD_DEL_VIP, mp);
193   S(mp);
194   W (ret);
195   return ret;
196 }
197
198 static int api_lb_add_del_as (vat_main_t * vam)
199 {
200   unformat_input_t * i = vam->input;
201   vl_api_lb_add_del_as_t mps, *mp;
202   int ret;
203   mps.is_del = 0;
204
205   if (!unformat(i, "%U %U",
206                 unformat_ip46_prefix, mps.vip_ip_prefix, &mps.vip_prefix_length, IP46_TYPE_ANY,
207                 unformat_ip46_address, mps.as_address)) {
208     errmsg ("invalid prefix or address\n");
209     return -99;
210   }
211
212   if (unformat(i, "del")) {
213     mps.is_del = 1;
214   }
215
216   M(LB_ADD_DEL_AS, mp);
217   S(mp);
218   W (ret);
219   return ret;
220 }
221
222 /*
223  * List of messages that the api test plugin sends,
224  * and that the data plane plugin processes
225  */
226 #define foreach_vpe_api_msg                             \
227 _(lb_conf, "<ip4-src-addr> <ip6-src-address> <sticky_buckets_per_core> <flow_timeout>") \
228 _(lb_add_del_vip, "<ip-prefix> [gre4|gre6|l3dsr|nat4|nat6] " \
229                   "<dscp> <port> <target_port> " \
230                   "<new_table_len> [del]") \
231 _(lb_add_del_as, "<vip-ip-prefix> <address> [del]")
232
233 static void 
234 lb_vat_api_hookup (vat_main_t *vam)
235 {
236   lb_test_main_t * lbtm = &lb_test_main;
237   /* Hook up handlers for replies from the data plane plug-in */
238 #define _(N,n)                                                  \
239   vl_msg_api_set_handlers((VL_API_##N + lbtm->msg_id_base),       \
240                           #n,                                   \
241                           vl_api_##n##_t_handler,               \
242                           vl_noop_handler,                      \
243                           vl_api_##n##_t_endian,                \
244                           vl_api_##n##_t_print,                 \
245                           sizeof(vl_api_##n##_t), 1);
246   foreach_vpe_api_reply_msg;
247 #undef _
248
249   /* API messages we can send */
250 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
251   foreach_vpe_api_msg;
252 #undef _
253
254   /* Help strings */
255 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
256   foreach_vpe_api_msg;
257 #undef _
258 }
259
260 clib_error_t * vat_plugin_register (vat_main_t *vam)
261 {
262   lb_test_main_t * lbtm = &lb_test_main;
263
264   u8 * name;
265
266   lbtm->vat_main = vam;
267
268   /* Ask the vpp engine for the first assigned message-id */
269   name = format (0, "lb_%08x%c", api_version, 0);
270   lbtm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
271
272   if (lbtm->msg_id_base != (u16) ~0)
273     lb_vat_api_hookup (vam);
274
275   vec_free(name);
276
277   return 0;
278 }