lb: remove api boilerplate
[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 <vnet/vnet.h>
17 #include <vnet/plugin/plugin.h>
18 #include <lb/lb.h>
19
20 #include <vppinfra/byte_order.h>
21 #include <vppinfra/string.h>
22 #include <vpp/api/types.h>
23 #include <vlibapi/api.h>
24 #include <vlibmemory/api.h>
25 #include <vpp/app/version.h>
26 #include <vnet/format_fns.h>
27 #include <vnet/ip/ip_types_api.h>
28
29 /* define message IDs */
30 #include <lb/lb.api_enum.h>
31 #include <lb/lb.api_types.h>
32
33 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
34
35 #define REPLY_MSG_ID_BASE lbm->msg_id_base
36 #include <vlibapi/api_helper_macros.h>
37
38 /* Macro to finish up custom dump fns */
39 #define FINISH                                  \
40     vec_add1 (s, 0);                            \
41     vl_print (handle, (char *)s);               \
42     vec_free (s);                               \
43     return handle;
44
45 static void
46 vl_api_lb_conf_t_handler
47 (vl_api_lb_conf_t * mp)
48 {
49   lb_main_t *lbm = &lb_main;
50   vl_api_lb_conf_reply_t * rmp;
51   int rv = 0;
52
53   if (mp->sticky_buckets_per_core == ~0) {
54     mp->sticky_buckets_per_core = htonl(lbm->per_cpu_sticky_buckets);
55   }
56   if (mp->flow_timeout == ~0) {
57     mp->flow_timeout = htonl(lbm->flow_timeout);
58   }
59
60   rv = lb_conf((ip4_address_t *)&mp->ip4_src_address,
61                (ip6_address_t *)&mp->ip6_src_address,
62                ntohl(mp->sticky_buckets_per_core),
63                ntohl(mp->flow_timeout));
64
65  REPLY_MACRO (VL_API_LB_CONF_REPLY);
66 }
67
68 static void *vl_api_lb_conf_t_print
69 (vl_api_lb_conf_t *mp, void * handle)
70 {
71   u8 * s;
72   s = format (0, "SCRIPT: lb_conf ");
73   s = format (s, "%U ", format_ip4_address, (ip4_address_t *)&mp->ip4_src_address);
74   s = format (s, "%U ", format_ip6_address, (ip6_address_t *)&mp->ip6_src_address);
75   s = format (s, "%u ", mp->sticky_buckets_per_core);
76   s = format (s, "%u ", mp->flow_timeout);
77   FINISH;
78 }
79
80
81 static void
82 vl_api_lb_add_del_vip_t_handler
83 (vl_api_lb_add_del_vip_t * mp)
84 {
85   lb_main_t *lbm = &lb_main;
86   vl_api_lb_conf_reply_t * rmp;
87   int rv = 0;
88   lb_vip_add_args_t args;
89
90   /* if port == 0, it means all-port VIP */
91   if (mp->port == 0)
92     {
93       mp->protocol = ~0;
94     }
95
96   ip_address_decode (&mp->pfx.address, &(args.prefix));
97
98   if (mp->is_del) {
99     u32 vip_index;
100     if (!(rv = lb_vip_find_index(&(args.prefix), mp->pfx.len,
101                                  mp->protocol, ntohs(mp->port), &vip_index)))
102       rv = lb_vip_del(vip_index);
103   } else {
104     u32 vip_index;
105     lb_vip_type_t type = 0;
106
107     if (ip46_prefix_is_ip4(&(args.prefix), mp->pfx.len)) {
108         if (mp->encap == LB_API_ENCAP_TYPE_GRE4)
109             type = LB_VIP_TYPE_IP4_GRE4;
110         else if (mp->encap == LB_API_ENCAP_TYPE_GRE6)
111             type = LB_VIP_TYPE_IP4_GRE6;
112         else if (mp->encap == LB_API_ENCAP_TYPE_L3DSR)
113             type = LB_VIP_TYPE_IP4_L3DSR;
114         else if (mp->encap == LB_API_ENCAP_TYPE_NAT4)
115             type = LB_VIP_TYPE_IP4_NAT4;
116     } else {
117         if (mp->encap == LB_API_ENCAP_TYPE_GRE4)
118             type = LB_VIP_TYPE_IP6_GRE4;
119         else if (mp->encap == LB_API_ENCAP_TYPE_GRE6)
120             type = LB_VIP_TYPE_IP6_GRE6;
121         else if (mp->encap == LB_API_ENCAP_TYPE_NAT6)
122             type = LB_VIP_TYPE_IP6_NAT6;
123     }
124
125     args.plen = mp->pfx.len;
126     args.protocol = mp->protocol;
127     args.port = ntohs(mp->port);
128     args.type = type;
129     args.new_length = ntohl(mp->new_flows_table_length);
130
131     if (mp->encap == LB_API_ENCAP_TYPE_L3DSR) {
132         args.encap_args.dscp = (u8)(mp->dscp & 0x3F);
133       }
134     else if ((mp->encap == LB_API_ENCAP_TYPE_NAT4)
135             ||(mp->encap == LB_API_ENCAP_TYPE_NAT6)) {
136         args.encap_args.srv_type = mp->type;
137         args.encap_args.target_port = ntohs(mp->target_port);
138       }
139
140     rv = lb_vip_add(args, &vip_index);
141   }
142  REPLY_MACRO (VL_API_LB_ADD_DEL_VIP_REPLY);
143 }
144
145 static void *vl_api_lb_add_del_vip_t_print
146 (vl_api_lb_add_del_vip_t *mp, void * handle)
147 {
148   u8 * s;
149   s = format (0, "SCRIPT: lb_add_del_vip ");
150   s = format (s, "%U", format_vl_api_prefix,
151        &mp->pfx);
152
153   s = format (s, "%s ", (mp->encap == LB_API_ENCAP_TYPE_GRE4)? "gre4"
154               : (mp->encap == LB_API_ENCAP_TYPE_GRE6)? "gre6"
155               : (mp->encap == LB_API_ENCAP_TYPE_NAT4)? "nat4"
156               : (mp->encap == LB_API_ENCAP_TYPE_NAT6)? "nat6"
157               : "l3dsr");
158
159   if (mp->encap==LB_API_ENCAP_TYPE_L3DSR)
160     {
161       s = format (s, "dscp %u ", mp->dscp);
162     }
163
164   if ((mp->encap==LB_API_ENCAP_TYPE_NAT4)
165       || (mp->encap==LB_API_ENCAP_TYPE_NAT6))
166     {
167       s = format (s, "type %u ", mp->type);
168       s = format (s, "port %u ", mp->port);
169       s = format (s, "target_port %u ", mp->target_port);
170     }
171
172   s = format (s, "%u ", mp->new_flows_table_length);
173   s = format (s, "%s ", mp->is_del?"del":"add");
174   FINISH;
175 }
176
177 static void
178 vl_api_lb_add_del_as_t_handler
179 (vl_api_lb_add_del_as_t * mp)
180 {
181   lb_main_t *lbm = &lb_main;
182   vl_api_lb_conf_reply_t * rmp;
183   int rv = 0;
184   u32 vip_index;
185   ip46_address_t vip_ip_prefix;
186   ip46_address_t as_address;
187
188   ip_address_decode (&mp->pfx.address, &vip_ip_prefix);
189   ip_address_decode (&mp->as_address, &as_address);
190
191   if ((rv = lb_vip_find_index(&vip_ip_prefix, mp->pfx.len,
192                               mp->protocol, ntohs(mp->port), &vip_index)))
193     goto done;
194
195   if (mp->is_del)
196     rv = lb_vip_del_ass(vip_index, &as_address, 1, mp->is_flush);
197   else
198     rv = lb_vip_add_ass(vip_index, &as_address, 1);
199
200 done:
201  REPLY_MACRO (VL_API_LB_ADD_DEL_AS_REPLY);
202 }
203
204 static void *vl_api_lb_add_del_as_t_print
205 (vl_api_lb_add_del_as_t *mp, void * handle)
206 {
207   u8 * s;
208   ip46_address_t address;
209   s = format (0, "SCRIPT: lb_add_del_as ");
210   s = format (s, "%U ", format_vl_api_prefix,
211        &mp->pfx);
212   s = format(s, "%u ", mp->protocol);
213   if (ip_address_decode (&mp->as_address, &address) == IP46_TYPE_IP6)
214   s = format (s, "%U ", format_ip6_address,
215                 (ip6_address_t *) & address.ip6);
216   else
217   s = format (s, "%U ", format_ip4_address,
218                 (ip6_address_t *) & address.ip4);
219   s = format (s, "%s ", mp->is_del?"del":"add");
220   FINISH;
221 }
222
223 static void
224 vl_api_lb_vip_dump_t_handler
225 (vl_api_lb_vip_dump_t * mp)
226 {
227
228   vl_api_registration_t *reg;
229   reg = vl_api_client_index_to_registration (mp->client_index);
230   if (!reg)
231     return;
232
233   lb_main_t *lbm = &lb_main;
234   vl_api_lb_vip_details_t * rmp;
235   int msg_size = 0;
236   lb_vip_t *vip = 0;
237
238   /* construct vip list */
239   pool_foreach(vip, lbm->vips, {
240       /* Hide dummy VIP */
241       if (vip != lbm->vips) {
242         msg_size = sizeof (*rmp);
243         rmp = vl_msg_api_alloc (msg_size);
244         memset (rmp, 0, msg_size);
245         rmp->_vl_msg_id =
246         htons (VL_API_LB_VIP_DETAILS + lbm->msg_id_base);
247         rmp->context = mp->context;
248
249         ip_address_encode(&vip->prefix, IP46_TYPE_ANY, &rmp->vip.pfx.address);
250         rmp->vip.pfx.len = vip->plen;
251         rmp->vip.protocol = htonl (vip->protocol);
252         rmp->vip.port = htons(vip->port);
253         rmp->encap = htonl(vip->type);
254         rmp->dscp = vip->encap_args.dscp;
255         rmp->srv_type = vip->encap_args.srv_type;
256         rmp->target_port = htons(vip->encap_args.target_port);
257         rmp->flow_table_length = htonl(vip->new_flow_table_mask + 1);
258
259         vl_api_send_msg (reg, (u8 *) rmp);
260       }
261   });
262
263
264 }
265
266 static void send_lb_as_details
267   (vl_api_registration_t * reg, u32 context, lb_vip_t * vip)
268 {
269   vl_api_lb_as_details_t *rmp;
270   lb_main_t *lbm = &lb_main;
271   int msg_size = 0;
272   u32 *as_index;
273   u32 asindex = 0;
274
275   /* construct as list under this vip */
276   lb_as_t *as;
277
278   pool_foreach(as_index, vip->as_indexes, {
279       /* Hide dummy As for specific VIP */
280       if (*as_index != 0) {
281         as = &lbm->ass[*as_index];
282         msg_size = sizeof (*rmp);
283         rmp = vl_msg_api_alloc (msg_size);
284         memset (rmp, 0, msg_size);
285         rmp->_vl_msg_id =
286           htons (VL_API_LB_AS_DETAILS + lbm->msg_id_base);
287         rmp->context = context;
288         ip_address_encode(&vip->prefix, IP46_TYPE_ANY, (vl_api_address_t *)&rmp->vip.pfx.address);
289         rmp->vip.pfx.len = vip->plen;
290         rmp->vip.protocol = htonl (vip->protocol);
291         rmp->vip.port = htons(vip->port);
292         ip_address_encode(&as->address, IP46_TYPE_ANY, &rmp->app_srv);
293         rmp->flags = as->flags;
294         rmp->in_use_since = htonl(as->last_used);
295
296         vl_api_send_msg (reg, (u8 *) rmp);
297         asindex++;
298       }
299   });
300
301
302 }
303
304 static void
305 vl_api_lb_as_dump_t_handler
306 (vl_api_lb_as_dump_t * mp)
307 {
308   lb_main_t *lbm = &lb_main;
309   lb_vip_t *vip = 0;
310   u8 dump_all = 0;
311   ip46_address_t prefix;
312
313   vl_api_registration_t *reg;
314   reg = vl_api_client_index_to_registration (mp->client_index);
315   if (!reg)
316     return;
317
318   clib_memcpy(&prefix.ip6, mp->pfx.address.un.ip6, sizeof(mp->pfx.address.un.ip6));
319
320   dump_all = (prefix.ip6.as_u64[0] == 0) && (prefix.ip6.as_u64[1] == 0);
321
322   /* *INDENT-OFF* */
323   pool_foreach(vip, lbm->vips,
324   ({
325     if ( dump_all
326         || ((prefix.as_u64[0] == vip->prefix.as_u64[0])
327         && (prefix.as_u64[1] == vip->prefix.as_u64[1])
328         && (mp->protocol == vip->protocol)
329         && (mp->port == vip->port)) )
330       {
331         send_lb_as_details(reg, mp->context, vip);
332       }
333   }));
334   /* *INDENT-ON* */
335 }
336
337 static void
338 vl_api_lb_flush_vip_t_handler
339 (vl_api_lb_flush_vip_t * mp)
340 {
341   lb_main_t *lbm = &lb_main;
342   int rv = 0;
343   ip46_address_t vip_prefix;
344   u8 vip_plen;
345   u32 vip_index;
346   vl_api_lb_flush_vip_reply_t * rmp;
347
348   if (mp->port == 0)
349     {
350       mp->protocol = ~0;
351     }
352
353   memcpy (&(vip_prefix.ip6), mp->pfx.address.un.ip6, sizeof(vip_prefix.ip6));
354
355   vip_plen = mp->pfx.len;
356
357   rv = lb_vip_find_index(&vip_prefix, vip_plen, mp->protocol,
358                          ntohs(mp->port), &vip_index);
359
360   rv = lb_flush_vip_as(vip_index, ~0);
361
362  REPLY_MACRO (VL_API_LB_FLUSH_VIP_REPLY);
363 }
364
365 static void vl_api_lb_add_del_intf_nat4_t_handler
366   (vl_api_lb_add_del_intf_nat4_t * mp)
367 {
368   lb_main_t *lbm = &lb_main;
369   vl_api_lb_add_del_intf_nat4_reply_t *rmp;
370   u32 sw_if_index = ntohl (mp->sw_if_index);
371   u8 is_del;
372   int rv = 0;
373
374   is_del = !mp->is_add;
375
376   VALIDATE_SW_IF_INDEX (mp);
377
378   rv = lb_nat4_interface_add_del(sw_if_index, is_del);
379
380   BAD_SW_IF_INDEX_LABEL;
381
382   REPLY_MACRO (VL_API_LB_ADD_DEL_INTF_NAT4_REPLY);
383 }
384
385 static void vl_api_lb_add_del_intf_nat6_t_handler
386   (vl_api_lb_add_del_intf_nat6_t * mp)
387 {
388   lb_main_t *lbm = &lb_main;
389   vl_api_lb_add_del_intf_nat6_reply_t *rmp;
390   u32 sw_if_index = ntohl (mp->sw_if_index);
391   u8 is_del;
392   int rv = 0;
393
394   is_del = !mp->is_add;
395
396   VALIDATE_SW_IF_INDEX (mp);
397
398   rv = lb_nat6_interface_add_del(sw_if_index, is_del);
399
400   BAD_SW_IF_INDEX_LABEL;
401
402   REPLY_MACRO (VL_API_LB_ADD_DEL_INTF_NAT6_REPLY);
403 }
404
405 static void *vl_api_lb_flush_vip_t_print
406 (vl_api_lb_flush_vip_t *mp, void * handle)
407 {
408   u8 * s;
409   s = format (0, "SCRIPT: lb_add_del_vip ");
410   s = format (s, "%U/%d", format_vl_api_address,
411        &mp->pfx.address, mp->pfx.len);
412   s = format (s, "protocol %u ", mp->protocol);
413   s = format (s, "port %u ", mp->port);
414
415   FINISH;
416 }
417
418 #include <lb/lb.api.c>
419 static clib_error_t * lb_api_init (vlib_main_t * vm)
420 {
421   lb_main_t * lbm = &lb_main;
422
423   lbm->vlib_main = vm;
424   lbm->vnet_main = vnet_get_main();
425
426   /* Ask for a correctly-sized block of API message decode slots */
427   lbm->msg_id_base = setup_message_id_table ();
428
429   return 0;
430 }
431
432 VLIB_INIT_FUNCTION (lb_api_init);