misc: fix issues reported by clang-15
[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_as_t_handler
132 (vl_api_lb_add_del_as_t * mp)
133 {
134   lb_main_t *lbm = &lb_main;
135   vl_api_lb_conf_reply_t * rmp;
136   int rv = 0;
137   u32 vip_index;
138   ip46_address_t vip_ip_prefix;
139   ip46_address_t as_address;
140
141   /* if port == 0, it means all-port VIP */
142   if (mp->port == 0)
143     {
144       mp->protocol = ~0;
145     }
146   ip_address_decode (&mp->pfx.address, &vip_ip_prefix);
147   ip_address_decode (&mp->as_address, &as_address);
148
149   if ((rv = lb_vip_find_index(&vip_ip_prefix, mp->pfx.len,
150                               mp->protocol, ntohs(mp->port), &vip_index)))
151     goto done;
152
153   if (mp->is_del)
154     rv = lb_vip_del_ass(vip_index, &as_address, 1, mp->is_flush);
155   else
156     rv = lb_vip_add_ass(vip_index, &as_address, 1);
157
158 done:
159  REPLY_MACRO (VL_API_LB_ADD_DEL_AS_REPLY);
160 }
161
162 static void
163 vl_api_lb_vip_dump_t_handler
164 (vl_api_lb_vip_dump_t * mp)
165 {
166
167   vl_api_registration_t *reg;
168   reg = vl_api_client_index_to_registration (mp->client_index);
169   if (!reg)
170     return;
171
172   lb_main_t *lbm = &lb_main;
173   vl_api_lb_vip_details_t * rmp;
174   int msg_size = 0;
175   lb_vip_t *vip = 0;
176
177   /* construct vip list */
178   pool_foreach (vip, lbm->vips) {
179       /* Hide placeholder VIP */
180       if (vip != lbm->vips) {
181         msg_size = sizeof (*rmp);
182         rmp = vl_msg_api_alloc (msg_size);
183         memset (rmp, 0, msg_size);
184         rmp->_vl_msg_id =
185         htons (VL_API_LB_VIP_DETAILS + lbm->msg_id_base);
186         rmp->context = mp->context;
187
188         ip_address_encode(&vip->prefix, IP46_TYPE_ANY, &rmp->vip.pfx.address);
189         rmp->vip.pfx.len = vip->plen;
190         rmp->vip.protocol = htonl (vip->protocol);
191         rmp->vip.port = htons(vip->port);
192         rmp->encap = htonl(vip->type);
193         rmp->dscp = vip->encap_args.dscp;
194         rmp->srv_type = vip->encap_args.srv_type;
195         rmp->target_port = htons(vip->encap_args.target_port);
196         rmp->flow_table_length = htonl(vip->new_flow_table_mask + 1);
197
198         vl_api_send_msg (reg, (u8 *) rmp);
199       }
200   }
201
202
203 }
204
205 static void send_lb_as_details
206   (vl_api_registration_t * reg, u32 context, lb_vip_t * vip)
207 {
208   vl_api_lb_as_details_t *rmp;
209   lb_main_t *lbm = &lb_main;
210   int msg_size = 0;
211   u32 *as_index;
212
213   /* construct as list under this vip */
214   lb_as_t *as;
215
216   pool_foreach (as_index, vip->as_indexes) {
217       /* Hide placeholder As for specific VIP */
218       if (*as_index != 0) {
219         as = &lbm->ass[*as_index];
220         msg_size = sizeof (*rmp);
221         rmp = vl_msg_api_alloc (msg_size);
222         memset (rmp, 0, msg_size);
223         rmp->_vl_msg_id =
224           htons (VL_API_LB_AS_DETAILS + lbm->msg_id_base);
225         rmp->context = context;
226         ip_address_encode(&vip->prefix, IP46_TYPE_ANY, (vl_api_address_t *)&rmp->vip.pfx.address);
227         rmp->vip.pfx.len = vip->plen;
228         rmp->vip.protocol = htonl (vip->protocol);
229         rmp->vip.port = htons(vip->port);
230         ip_address_encode(&as->address, IP46_TYPE_ANY, &rmp->app_srv);
231         rmp->flags = as->flags;
232         rmp->in_use_since = htonl(as->last_used);
233
234         vl_api_send_msg (reg, (u8 *) rmp);
235       }
236   }
237
238
239 }
240
241 static void
242 vl_api_lb_as_dump_t_handler
243 (vl_api_lb_as_dump_t * mp)
244 {
245   lb_main_t *lbm = &lb_main;
246   lb_vip_t *vip = 0;
247   u8 dump_all = 0;
248   ip46_address_t prefix;
249
250   vl_api_registration_t *reg;
251   reg = vl_api_client_index_to_registration (mp->client_index);
252   if (!reg)
253     return;
254
255   clib_memcpy(&prefix.ip6, mp->pfx.address.un.ip6, sizeof(mp->pfx.address.un.ip6));
256
257   dump_all = (prefix.ip6.as_u64[0] == 0) && (prefix.ip6.as_u64[1] == 0);
258
259   /* *INDENT-OFF* */
260   pool_foreach (vip, lbm->vips)
261    {
262     if ( dump_all
263         || ((prefix.as_u64[0] == vip->prefix.as_u64[0])
264         && (prefix.as_u64[1] == vip->prefix.as_u64[1])
265         && (mp->protocol == vip->protocol)
266         && (mp->port == vip->port)) )
267       {
268         send_lb_as_details(reg, mp->context, vip);
269       }
270   }
271   /* *INDENT-ON* */
272 }
273
274 static void
275 vl_api_lb_flush_vip_t_handler
276 (vl_api_lb_flush_vip_t * mp)
277 {
278   lb_main_t *lbm = &lb_main;
279   int rv = 0;
280   ip46_address_t vip_prefix;
281   u8 vip_plen;
282   u32 vip_index;
283   vl_api_lb_flush_vip_reply_t * rmp;
284
285   if (mp->port == 0)
286     {
287       mp->protocol = ~0;
288     }
289
290   memcpy (&(vip_prefix.ip6), mp->pfx.address.un.ip6, sizeof(vip_prefix.ip6));
291
292   vip_plen = mp->pfx.len;
293
294   rv = lb_vip_find_index(&vip_prefix, vip_plen, mp->protocol,
295                          ntohs(mp->port), &vip_index);
296
297   rv = lb_flush_vip_as(vip_index, ~0);
298
299  REPLY_MACRO (VL_API_LB_FLUSH_VIP_REPLY);
300 }
301
302 static void vl_api_lb_add_del_intf_nat4_t_handler
303   (vl_api_lb_add_del_intf_nat4_t * mp)
304 {
305   lb_main_t *lbm = &lb_main;
306   vl_api_lb_add_del_intf_nat4_reply_t *rmp;
307   u32 sw_if_index = ntohl (mp->sw_if_index);
308   u8 is_del;
309   int rv = 0;
310
311   is_del = !mp->is_add;
312
313   VALIDATE_SW_IF_INDEX (mp);
314
315   rv = lb_nat4_interface_add_del(sw_if_index, is_del);
316
317   BAD_SW_IF_INDEX_LABEL;
318
319   REPLY_MACRO (VL_API_LB_ADD_DEL_INTF_NAT4_REPLY);
320 }
321
322 static void vl_api_lb_add_del_intf_nat6_t_handler
323   (vl_api_lb_add_del_intf_nat6_t * mp)
324 {
325   lb_main_t *lbm = &lb_main;
326   vl_api_lb_add_del_intf_nat6_reply_t *rmp;
327   u32 sw_if_index = ntohl (mp->sw_if_index);
328   u8 is_del;
329   int rv = 0;
330
331   is_del = !mp->is_add;
332
333   VALIDATE_SW_IF_INDEX (mp);
334
335   rv = lb_nat6_interface_add_del(sw_if_index, is_del);
336
337   BAD_SW_IF_INDEX_LABEL;
338
339   REPLY_MACRO (VL_API_LB_ADD_DEL_INTF_NAT6_REPLY);
340 }
341
342 #include <lb/lb.api.c>
343 static clib_error_t * lb_api_init (vlib_main_t * vm)
344 {
345   lb_main_t * lbm = &lb_main;
346
347   lbm->vlib_main = vm;
348   lbm->vnet_main = vnet_get_main();
349
350   /* Ask for a correctly-sized block of API message decode slots */
351   lbm->msg_id_base = setup_message_id_table ();
352
353   return 0;
354 }
355
356 VLIB_INIT_FUNCTION (lb_api_init);