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