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