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