tests: update test_lb.py to use api call lb_conf.
[vpp.git] / src / plugins / lb / api.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 <lb/lb.h>
17
18 #include <vppinfra/byte_order.h>
19 #include <vppinfra/string.h>
20 #include <vlibapi/api.h>
21 #include <vlibapi/api.h>
22 #include <vlibmemory/api.h>
23
24
25 #define vl_msg_id(n,h) n,
26 typedef enum {
27 #include <lb/lb.api.h>
28     /* We'll want to know how many messages IDs we need... */
29     VL_MSG_FIRST_AVAILABLE,
30 } vl_msg_id_t;
31 #undef vl_msg_id
32
33
34 /* define message structures */
35 #define vl_typedefs
36 #include <lb/lb.api.h>
37 #undef vl_typedefs
38
39 /* define generated endian-swappers */
40 #define vl_endianfun
41 #include <lb/lb.api.h>
42 #undef vl_endianfun
43
44 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
45
46 /* Get the API version number */
47 #define vl_api_version(n,v) static u32 api_version=(v);
48 #include <lb/lb.api.h>
49 #undef vl_api_version
50
51 #define vl_msg_name_crc_list
52 #include <lb/lb.api.h>
53 #undef vl_msg_name_crc_list
54
55
56 #define REPLY_MSG_ID_BASE lbm->msg_id_base
57 #include <vlibapi/api_helper_macros.h>
58
59 static void
60 setup_message_id_table (lb_main_t * lbm, api_main_t * am)
61 {
62 #define _(id,n,crc) \
63   vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + lbm->msg_id_base);
64   foreach_vl_msg_name_crc_lb;
65 #undef _
66 }
67
68 /* Macro to finish up custom dump fns */
69 #define FINISH                                  \
70     vec_add1 (s, 0);                            \
71     vl_print (handle, (char *)s);               \
72     vec_free (s);                               \
73     return handle;
74
75 static void
76 vl_api_lb_conf_t_handler
77 (vl_api_lb_conf_t * mp)
78 {
79   lb_main_t *lbm = &lb_main;
80   vl_api_lb_conf_reply_t * rmp;
81   int rv = 0;
82
83   if (mp->sticky_buckets_per_core == ~0) {
84     mp->sticky_buckets_per_core = lbm->per_cpu_sticky_buckets;
85   }
86   if (mp->flow_timeout == ~0) {
87     mp->flow_timeout = lbm->flow_timeout;
88   }
89
90   rv = lb_conf((ip4_address_t *)&mp->ip4_src_address,
91                (ip6_address_t *)&mp->ip6_src_address,
92                mp->sticky_buckets_per_core,
93                mp->flow_timeout);
94
95  REPLY_MACRO (VL_API_LB_CONF_REPLY);
96 }
97
98 static void *vl_api_lb_conf_t_print
99 (vl_api_lb_conf_t *mp, void * handle)
100 {
101   u8 * s;
102   s = format (0, "SCRIPT: lb_conf ");
103   s = format (s, "%U ", format_ip4_address, (ip4_address_t *)&mp->ip4_src_address);
104   s = format (s, "%U ", format_ip6_address, (ip6_address_t *)&mp->ip6_src_address);
105   s = format (s, "%u ", mp->sticky_buckets_per_core);
106   s = format (s, "%u ", mp->flow_timeout);
107   FINISH;
108 }
109
110
111 static void
112 vl_api_lb_add_del_vip_t_handler
113 (vl_api_lb_add_del_vip_t * mp)
114 {
115   lb_main_t *lbm = &lb_main;
116   vl_api_lb_conf_reply_t * rmp;
117   int rv = 0;
118   lb_vip_add_args_t args;
119
120   /* if port == 0, it means all-port VIP */
121   if (mp->port == 0)
122     {
123       mp->protocol = ~0;
124     }
125
126   memcpy (&(args.prefix.ip6), mp->ip_prefix, sizeof(args.prefix.ip6));
127
128   if (mp->is_del) {
129     u32 vip_index;
130     if (!(rv = lb_vip_find_index(&(args.prefix), mp->prefix_length,
131                                  mp->protocol, ntohs(mp->port), &vip_index)))
132       rv = lb_vip_del(vip_index);
133   } else {
134     u32 vip_index;
135     lb_vip_type_t type = 0;
136
137     if (ip46_prefix_is_ip4(&(args.prefix), mp->prefix_length)) {
138         if (mp->encap == LB_ENCAP_TYPE_GRE4)
139             type = LB_VIP_TYPE_IP4_GRE4;
140         else if (mp->encap == LB_ENCAP_TYPE_GRE6)
141             type = LB_VIP_TYPE_IP4_GRE6;
142         else if (mp->encap == LB_ENCAP_TYPE_L3DSR)
143             type = LB_VIP_TYPE_IP4_L3DSR;
144         else if (mp->encap == LB_ENCAP_TYPE_NAT4)
145             type = LB_VIP_TYPE_IP4_NAT4;
146     } else {
147         if (mp->encap == LB_ENCAP_TYPE_GRE4)
148             type = LB_VIP_TYPE_IP6_GRE4;
149         else if (mp->encap == LB_ENCAP_TYPE_GRE6)
150             type = LB_VIP_TYPE_IP6_GRE6;
151         else if (mp->encap == LB_ENCAP_TYPE_NAT6)
152             type = LB_VIP_TYPE_IP6_NAT6;
153     }
154
155     args.plen = mp->prefix_length;
156     args.protocol = mp->protocol;
157     args.port = ntohs(mp->port);
158     args.type = type;
159     args.new_length = ntohl(mp->new_flows_table_length);
160
161     if (mp->encap == LB_ENCAP_TYPE_L3DSR) {
162         args.encap_args.dscp = (u8)(mp->dscp & 0x3F);
163       }
164     else if ((mp->encap == LB_ENCAP_TYPE_NAT4)
165             ||(mp->encap == LB_ENCAP_TYPE_NAT6)) {
166         args.encap_args.srv_type = mp->type;
167         args.encap_args.target_port = ntohs(mp->target_port);
168       }
169
170     rv = lb_vip_add(args, &vip_index);
171   }
172  REPLY_MACRO (VL_API_LB_ADD_DEL_VIP_REPLY);
173 }
174
175 static void *vl_api_lb_add_del_vip_t_print
176 (vl_api_lb_add_del_vip_t *mp, void * handle)
177 {
178   u8 * s;
179   s = format (0, "SCRIPT: lb_add_del_vip ");
180   s = format (s, "%U ", format_ip46_prefix,
181               (ip46_address_t *)mp->ip_prefix, mp->prefix_length, IP46_TYPE_ANY);
182
183   s = format (s, "%s ", (mp->encap == LB_ENCAP_TYPE_GRE4)? "gre4"
184               : (mp->encap == LB_ENCAP_TYPE_GRE6)? "gre6"
185               : (mp->encap == LB_ENCAP_TYPE_NAT4)? "nat4"
186               : (mp->encap == LB_ENCAP_TYPE_NAT6)? "nat6"
187               : "l3dsr");
188
189   if (mp->encap==LB_ENCAP_TYPE_L3DSR)
190     {
191       s = format (s, "dscp %u ", mp->dscp);
192     }
193
194   if ((mp->encap==LB_ENCAP_TYPE_NAT4)
195       || (mp->encap==LB_ENCAP_TYPE_NAT6))
196     {
197       s = format (s, "type %u ", mp->type);
198       s = format (s, "port %u ", mp->port);
199       s = format (s, "target_port %u ", mp->target_port);
200     }
201
202   s = format (s, "%u ", mp->new_flows_table_length);
203   s = format (s, "%s ", mp->is_del?"del":"add");
204   FINISH;
205 }
206
207 static void
208 vl_api_lb_add_del_as_t_handler
209 (vl_api_lb_add_del_as_t * mp)
210 {
211   lb_main_t *lbm = &lb_main;
212   vl_api_lb_conf_reply_t * rmp;
213   int rv = 0;
214   u32 vip_index;
215   ip46_address_t vip_ip_prefix;
216
217   memcpy(&vip_ip_prefix.ip6, mp->vip_ip_prefix,
218          sizeof(vip_ip_prefix.ip6));
219
220   ip46_address_t as_address;
221
222   memcpy(&as_address.ip6, mp->as_address,
223          sizeof(as_address.ip6));
224
225   if ((rv = lb_vip_find_index(&vip_ip_prefix, mp->vip_prefix_length,
226                               mp->protocol, ntohs(mp->port), &vip_index)))
227     goto done;
228
229   if (mp->is_del)
230     rv = lb_vip_del_ass(vip_index, &as_address, 1, mp->is_flush);
231   else
232     rv = lb_vip_add_ass(vip_index, &as_address, 1);
233
234 done:
235  REPLY_MACRO (VL_API_LB_ADD_DEL_AS_REPLY);
236 }
237
238 static void *vl_api_lb_add_del_as_t_print
239 (vl_api_lb_add_del_as_t *mp, void * handle)
240 {
241   u8 * s;
242   s = format (0, "SCRIPT: lb_add_del_as ");
243   s = format (s, "%U ", format_ip46_prefix,
244               (ip46_address_t *)mp->vip_ip_prefix, mp->vip_prefix_length, IP46_TYPE_ANY);
245   s = format (s, "%U ", format_ip46_address,
246                 (ip46_address_t *)mp->as_address, IP46_TYPE_ANY);
247   s = format (s, "%s ", mp->is_del?"del":"add");
248   FINISH;
249 }
250
251 static void
252 vl_api_lb_flush_vip_t_handler
253 (vl_api_lb_flush_vip_t * mp)
254 {
255   lb_main_t *lbm = &lb_main;
256   int rv = 0;
257   ip46_address_t vip_prefix;
258   u8 vip_plen;
259   u32 vip_index;
260   vl_api_lb_flush_vip_reply_t * rmp;
261
262   if (mp->port == 0)
263     {
264       mp->protocol = ~0;
265     }
266
267   memcpy (&(vip_prefix.ip6), mp->ip_prefix, sizeof(vip_prefix.ip6));
268
269   vip_plen = mp->prefix_length;
270
271   rv = lb_vip_find_index(&vip_prefix, vip_plen, mp->protocol,
272                          ntohs(mp->port), &vip_index);
273
274   rv = lb_flush_vip_as(vip_index, ~0);
275
276  REPLY_MACRO (VL_API_LB_FLUSH_VIP_REPLY);
277 }
278
279 static void *vl_api_lb_flush_vip_t_print
280 (vl_api_lb_flush_vip_t *mp, void * handle)
281 {
282   u8 * s;
283   s = format (0, "SCRIPT: lb_add_del_vip ");
284   s = format (s, "%U ", format_ip46_prefix,
285               (ip46_address_t *)mp->ip_prefix, mp->prefix_length, IP46_TYPE_ANY);
286
287   s = format (s, "protocol %u ", mp->protocol);
288   s = format (s, "port %u ", mp->port);
289
290   FINISH;
291 }
292
293 /* List of message types that this plugin understands */
294 #define foreach_lb_plugin_api_msg            \
295 _(LB_CONF, lb_conf)                          \
296 _(LB_ADD_DEL_VIP, lb_add_del_vip)            \
297 _(LB_ADD_DEL_AS, lb_add_del_as)              \
298 _(LB_FLUSH_VIP, lb_flush_vip)
299
300 static clib_error_t * lb_api_init (vlib_main_t * vm)
301 {
302   lb_main_t *lbm = &lb_main;
303   u8 *name = format (0, "lb_%08x%c", api_version, 0);
304   lbm->msg_id_base = vl_msg_api_get_msg_ids
305       ((char *) name, VL_MSG_FIRST_AVAILABLE);
306
307 #define _(N,n)                                                  \
308     vl_msg_api_set_handlers((VL_API_##N + lbm->msg_id_base),     \
309                            #n,                  \
310                            vl_api_##n##_t_handler,              \
311                            vl_noop_handler,                     \
312                            vl_api_##n##_t_endian,               \
313                            vl_api_##n##_t_print,                \
314                            sizeof(vl_api_##n##_t), 1);
315   foreach_lb_plugin_api_msg;
316 #undef _
317
318   /* Add our API messages to the global name_crc hash table */
319   setup_message_id_table (lbm, &api_main);
320
321   return 0;
322 }
323
324 VLIB_INIT_FUNCTION (lb_api_init);