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