FD.io VPP  v20.01-46-g7006026de
Vector Packet Processing
api.c
Go to the documentation of this file.
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 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
34 
35 #define REPLY_MSG_ID_BASE lbm->msg_id_base
37 
38 /* Macro to finish up custom dump fns */
39 #define FINISH \
40  vec_add1 (s, 0); \
41  vl_print (handle, (char *)s); \
42  vec_free (s); \
43  return handle;
44 
45 static void
48 {
49  lb_main_t *lbm = &lb_main;
50  vl_api_lb_conf_reply_t * rmp;
51  u32 sticky_buckets_per_core, flow_timeout;
52  int rv = 0;
53 
54  sticky_buckets_per_core = mp->sticky_buckets_per_core == ~0
56  : ntohl(mp->sticky_buckets_per_core);
57  flow_timeout = mp->flow_timeout == ~0
58  ? lbm->flow_timeout
59  : ntohl(mp->flow_timeout);
60 
63  sticky_buckets_per_core, flow_timeout);
64 
65  REPLY_MACRO (VL_API_LB_CONF_REPLY);
66 }
67 
68 static void *vl_api_lb_conf_t_print
69 (vl_api_lb_conf_t *mp, void * handle)
70 {
71  u8 * s;
72  s = format (0, "SCRIPT: lb_conf ");
73  s = format (s, "%U ", format_ip4_address, (ip4_address_t *)&mp->ip4_src_address);
74  s = format (s, "%U ", format_ip6_address, (ip6_address_t *)&mp->ip6_src_address);
75  s = format (s, "%u ", mp->sticky_buckets_per_core);
76  s = format (s, "%u ", mp->flow_timeout);
77  FINISH;
78 }
79 
80 
81 static void
84 {
85  lb_main_t *lbm = &lb_main;
86  vl_api_lb_conf_reply_t * rmp;
87  int rv = 0;
88  lb_vip_add_args_t args;
89 
90  /* if port == 0, it means all-port VIP */
91  if (mp->port == 0)
92  {
93  mp->protocol = ~0;
94  }
95 
96  memcpy (&(args.prefix.ip6), &mp->pfx.address.un.ip6, sizeof(args.prefix.ip6));
97 
98  if (mp->is_del) {
99  u32 vip_index;
100  if (!(rv = lb_vip_find_index(&(args.prefix), mp->pfx.len,
101  mp->protocol, ntohs(mp->port), &vip_index)))
102  rv = lb_vip_del(vip_index);
103  } else {
104  u32 vip_index;
105  lb_vip_type_t type = 0;
106 
107  if (ip46_prefix_is_ip4(&(args.prefix), mp->pfx.len)) {
108  if (mp->encap == LB_API_ENCAP_TYPE_GRE4)
109  type = LB_VIP_TYPE_IP4_GRE4;
110  else if (mp->encap == LB_API_ENCAP_TYPE_GRE6)
111  type = LB_VIP_TYPE_IP4_GRE6;
112  else if (mp->encap == LB_API_ENCAP_TYPE_L3DSR)
113  type = LB_VIP_TYPE_IP4_L3DSR;
114  else if (mp->encap == LB_API_ENCAP_TYPE_NAT4)
115  type = LB_VIP_TYPE_IP4_NAT4;
116  } else {
117  if (mp->encap == LB_API_ENCAP_TYPE_GRE4)
118  type = LB_VIP_TYPE_IP6_GRE4;
119  else if (mp->encap == LB_API_ENCAP_TYPE_GRE6)
120  type = LB_VIP_TYPE_IP6_GRE6;
121  else if (mp->encap == LB_API_ENCAP_TYPE_NAT6)
122  type = LB_VIP_TYPE_IP6_NAT6;
123  }
124 
125  args.plen = mp->pfx.len;
126  args.protocol = mp->protocol;
127  args.port = ntohs(mp->port);
128  args.type = type;
129  args.new_length = ntohl(mp->new_flows_table_length);
130 
131  if (mp->encap == LB_API_ENCAP_TYPE_L3DSR) {
132  args.encap_args.dscp = (u8)(mp->dscp & 0x3F);
133  }
134  else if ((mp->encap == LB_API_ENCAP_TYPE_NAT4)
135  ||(mp->encap == LB_API_ENCAP_TYPE_NAT6)) {
136  args.encap_args.srv_type = mp->type;
137  args.encap_args.target_port = ntohs(mp->target_port);
138  }
139 
140  rv = lb_vip_add(args, &vip_index);
141  }
142  REPLY_MACRO (VL_API_LB_ADD_DEL_VIP_REPLY);
143 }
144 
146 (vl_api_lb_add_del_vip_t *mp, void * handle)
147 {
148  u8 * s;
149  s = format (0, "SCRIPT: lb_add_del_vip ");
150  s = format (s, "%U", format_vl_api_prefix,
151  &mp->pfx);
152 
153  s = format (s, "%s ", (mp->encap == LB_API_ENCAP_TYPE_GRE4)? "gre4"
154  : (mp->encap == LB_API_ENCAP_TYPE_GRE6)? "gre6"
155  : (mp->encap == LB_API_ENCAP_TYPE_NAT4)? "nat4"
156  : (mp->encap == LB_API_ENCAP_TYPE_NAT6)? "nat6"
157  : "l3dsr");
158 
160  {
161  s = format (s, "dscp %u ", mp->dscp);
162  }
163 
164  if ((mp->encap==LB_API_ENCAP_TYPE_NAT4)
165  || (mp->encap==LB_API_ENCAP_TYPE_NAT6))
166  {
167  s = format (s, "type %u ", mp->type);
168  s = format (s, "port %u ", mp->port);
169  s = format (s, "target_port %u ", mp->target_port);
170  }
171 
172  s = format (s, "%u ", mp->new_flows_table_length);
173  s = format (s, "%s ", mp->is_del?"del":"add");
174  FINISH;
175 }
176 
177 static void
180 {
181  lb_main_t *lbm = &lb_main;
182  vl_api_lb_conf_reply_t * rmp;
183  int rv = 0;
184  u32 vip_index;
185  ip46_address_t vip_ip_prefix;
186 
187  /* if port == 0, it means all-port VIP */
188  if (mp->port == 0)
189  {
190  mp->protocol = ~0;
191  }
192 
193  memcpy(&vip_ip_prefix.ip6, &mp->pfx.address.un.ip6,
194  sizeof(vip_ip_prefix.ip6));
195 
196  ip46_address_t as_address;
197 
198  memcpy(&as_address.ip6, &mp->as_address.un.ip6,
199  sizeof(as_address.ip6));
200 
201  if ((rv = lb_vip_find_index(&vip_ip_prefix, mp->pfx.len,
202  mp->protocol, ntohs(mp->port), &vip_index)))
203  goto done;
204 
205  if (mp->is_del)
206  rv = lb_vip_del_ass(vip_index, &as_address, 1, mp->is_flush);
207  else
208  rv = lb_vip_add_ass(vip_index, &as_address, 1);
209 
210 done:
211  REPLY_MACRO (VL_API_LB_ADD_DEL_AS_REPLY);
212 }
213 
214 static void *vl_api_lb_add_del_as_t_print
215 (vl_api_lb_add_del_as_t *mp, void * handle)
216 {
217  u8 * s;
218  ip46_address_t address;
219  s = format (0, "SCRIPT: lb_add_del_as ");
220  s = format (s, "%U ", format_vl_api_prefix,
221  &mp->pfx);
222  s = format(s, "%u ", mp->protocol);
223  if (ip_address_decode (&mp->as_address, &address) == IP46_TYPE_IP6)
224  s = format (s, "%U ", format_ip6_address,
225  (ip6_address_t *) & address.ip6);
226  else
227  s = format (s, "%U ", format_ip4_address,
228  (ip6_address_t *) & address.ip4);
229  s = format (s, "%s ", mp->is_del?"del":"add");
230  FINISH;
231 }
232 
233 static void
235 (vl_api_lb_vip_dump_t * mp)
236 {
237 
239  reg = vl_api_client_index_to_registration (mp->client_index);
240  if (!reg)
241  return;
242 
243  lb_main_t *lbm = &lb_main;
245  int msg_size = 0;
246  lb_vip_t *vip = 0;
247 
248  /* construct vip list */
249  pool_foreach(vip, lbm->vips, {
250  /* Hide dummy VIP */
251  if (vip != lbm->vips) {
252  msg_size = sizeof (*rmp);
253  rmp = vl_msg_api_alloc (msg_size);
254  memset (rmp, 0, msg_size);
255  rmp->_vl_msg_id =
256  htons (VL_API_LB_VIP_DETAILS + lbm->msg_id_base);
257  rmp->context = mp->context;
258 
259  ip_address_encode(&vip->prefix, IP46_TYPE_ANY, &rmp->vip.pfx.address);
260  rmp->vip.pfx.len = vip->plen;
261  rmp->vip.protocol = htonl (vip->protocol);
262  rmp->vip.port = htons(vip->port);
263  rmp->encap = htonl(vip->type);
264  rmp->dscp = vip->encap_args.dscp;
265  rmp->srv_type = vip->encap_args.srv_type;
266  rmp->target_port = htons(vip->encap_args.target_port);
267  rmp->flow_table_length = htonl(vip->new_flow_table_mask + 1);
268 
269  vl_api_send_msg (reg, (u8 *) rmp);
270  }
271  });
272 
273 
274 }
275 
276 static void send_lb_as_details
277  (vl_api_registration_t * reg, u32 context, lb_vip_t * vip)
278 {
280  lb_main_t *lbm = &lb_main;
281  int msg_size = 0;
282  u32 *as_index;
283  u32 asindex = 0;
284 
285  /* construct as list under this vip */
286  lb_as_t *as;
287 
288  pool_foreach(as_index, vip->as_indexes, {
289  /* Hide dummy As for specific VIP */
290  if (*as_index != 0) {
291  as = &lbm->ass[*as_index];
292  msg_size = sizeof (*rmp);
293  rmp = vl_msg_api_alloc (msg_size);
294  memset (rmp, 0, msg_size);
295  rmp->_vl_msg_id =
296  htons (VL_API_LB_AS_DETAILS + lbm->msg_id_base);
297  rmp->context = context;
298  ip_address_encode(&vip->prefix, IP46_TYPE_ANY, (vl_api_address_t *)&rmp->vip.pfx.address);
299  rmp->vip.pfx.len = vip->plen;
300  rmp->vip.protocol = htonl (vip->protocol);
301  rmp->vip.port = htons(vip->port);
302  ip_address_encode(&as->address, IP46_TYPE_ANY, &rmp->app_srv);
303  rmp->flags = as->flags;
304  rmp->in_use_since = htonl(as->last_used);
305 
306  vl_api_send_msg (reg, (u8 *) rmp);
307  asindex++;
308  }
309  });
310 
311 
312 }
313 
314 static void
316 (vl_api_lb_as_dump_t * mp)
317 {
318  lb_main_t *lbm = &lb_main;
319  lb_vip_t *vip = 0;
320  u8 dump_all = 0;
321  ip46_address_t prefix;
322 
324  reg = vl_api_client_index_to_registration (mp->client_index);
325  if (!reg)
326  return;
327 
328  clib_memcpy(&prefix.ip6, mp->pfx.address.un.ip6, sizeof(mp->pfx.address.un.ip6));
329 
330  dump_all = (prefix.ip6.as_u64[0] == 0) && (prefix.ip6.as_u64[1] == 0);
331 
332  /* *INDENT-OFF* */
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  /* *INDENT-ON* */
345 }
346 
347 static void
350 {
351  lb_main_t *lbm = &lb_main;
352  int rv = 0;
353  ip46_address_t vip_prefix;
354  u8 vip_plen;
355  u32 vip_index;
356  vl_api_lb_flush_vip_reply_t * rmp;
357 
358  if (mp->port == 0)
359  {
360  mp->protocol = ~0;
361  }
362 
363  memcpy (&(vip_prefix.ip6), mp->pfx.address.un.ip6, sizeof(vip_prefix.ip6));
364 
365  vip_plen = mp->pfx.len;
366 
367  rv = lb_vip_find_index(&vip_prefix, vip_plen, mp->protocol,
368  ntohs(mp->port), &vip_index);
369 
370  rv = lb_flush_vip_as(vip_index, ~0);
371 
372  REPLY_MACRO (VL_API_LB_FLUSH_VIP_REPLY);
373 }
374 
377 {
378  lb_main_t *lbm = &lb_main;
379  vl_api_lb_add_del_intf_nat4_reply_t *rmp;
380  u32 sw_if_index = ntohl (mp->sw_if_index);
381  u8 is_del;
382  int rv = 0;
383 
384  is_del = !mp->is_add;
385 
387 
388  rv = lb_nat4_interface_add_del(sw_if_index, is_del);
389 
391 
392  REPLY_MACRO (VL_API_LB_ADD_DEL_INTF_NAT4_REPLY);
393 }
394 
397 {
398  lb_main_t *lbm = &lb_main;
399  vl_api_lb_add_del_intf_nat6_reply_t *rmp;
400  u32 sw_if_index = ntohl (mp->sw_if_index);
401  u8 is_del;
402  int rv = 0;
403 
404  is_del = !mp->is_add;
405 
407 
408  rv = lb_nat6_interface_add_del(sw_if_index, is_del);
409 
411 
412  REPLY_MACRO (VL_API_LB_ADD_DEL_INTF_NAT6_REPLY);
413 }
414 
415 static void *vl_api_lb_flush_vip_t_print
416 (vl_api_lb_flush_vip_t *mp, void * handle)
417 {
418  u8 * s;
419  s = format (0, "SCRIPT: lb_add_del_vip ");
420  s = format (s, "%U/%d", format_vl_api_address,
421  &mp->pfx.address, mp->pfx.len);
422  s = format (s, "protocol %u ", mp->protocol);
423  s = format (s, "port %u ", mp->port);
424 
425  FINISH;
426 }
427 
428 #include <lb/lb.api.c>
430 {
431  lb_main_t * lbm = &lb_main;
432 
433  lbm->vlib_main = vm;
434  lbm->vnet_main = vnet_get_main();
435 
436  /* Ask for a correctly-sized block of API message decode slots */
438 
439  return 0;
440 }
441 
int lb_vip_del_ass(u32 vip_index, ip46_address_t *addresses, u32 n, u8 flush)
Definition: lb.c:853
vl_api_address_t as_address
Definition: lb.api:73
static void vl_api_lb_conf_t_handler(vl_api_lb_conf_t *mp)
Definition: api.c:47
u32 flow_timeout
Definition: lb.api:22
int lb_nat4_interface_add_del(u32 sw_if_index, int is_del)
Definition: lb.c:1328
static void * vl_api_lb_add_del_vip_t_print(vl_api_lb_add_del_vip_t *mp, void *handle)
Definition: api.c:146
vnet_main_t * vnet_main
Definition: lb.h:569
Each VIP is configured with a set of application server.
Definition: lb.h:108
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
u32 per_cpu_sticky_buckets
Number of buckets in the per-cpu sticky hash table.
Definition: lb.h:522
Optimized string handling code, including c11-compliant "safe C library" variants.
u16 msg_id_base
API dynamically registered base ID.
Definition: lb.h:563
Enable/disable NAT6 feature on the interface.
Definition: lb.api:170
u8 * format_vl_api_address(u8 *s, va_list *args)
Definition: types.c:39
int lb_conf(ip4_address_t *ip4_address, ip6_address_t *ip6_address, u32 per_cpu_sticky_buckets, u32 flow_timeout)
Fix global load-balancer parameters.
Definition: lb.c:476
int lb_vip_add_ass(u32 vip_index, ip46_address_t *addresses, u32 n)
Definition: lb.c:585
u32 new_flows_table_length[default=1024]
Definition: lb.api:52
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
vl_api_prefix_t prefix
Definition: ip.api:144
static void * vl_api_lb_conf_t_print(vl_api_lb_conf_t *mp, void *handle)
Definition: api.c:69
static void vl_api_lb_add_del_intf_nat6_t_handler(vl_api_lb_add_del_intf_nat6_t *mp)
Definition: api.c:396
Flush a given vip.
Definition: lb.api:86
unsigned char u8
Definition: types.h:56
static void * vl_api_lb_add_del_as_t_print(vl_api_lb_add_del_as_t *mp, void *handle)
Definition: api.c:215
#define clib_memcpy(d, s, n)
Definition: string.h:180
format_function_t format_ip4_address
Definition: format.h:73
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:498
vl_api_interface_index_t sw_if_index
Definition: gre.api:59
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
vl_api_address_with_prefix_t pfx
Definition: lb.api:89
static void vl_api_lb_vip_dump_t_handler(vl_api_lb_vip_dump_t *mp)
Definition: api.c:235
lb_main_t lb_main
Definition: lb.c:32
u32 flow_timeout
Flow timeout in seconds.
Definition: lb.h:527
Definition: lb.h:470
unsigned int u32
Definition: types.h:88
ip46_type_t ip_address_decode(const vl_api_address_t *in, ip46_address_t *out)
Definition: ip_types_api.c:161
lb_vip_t * vips
Pool of all Virtual IPs.
Definition: lb.h:474
Enable/disable NAT4 feature on the interface.
Definition: lb.api:157
vl_api_fib_path_type_t type
Definition: fib_types.api:123
#define FINISH
Definition: api.c:39
u8 * format_vl_api_prefix(u8 *s, va_list *args)
Definition: types.c:87
vl_api_ip6_address_t ip6_src_address
Definition: lb.api:20
int lb_vip_del(u32 vip_index)
Definition: lb.c:1200
vl_api_lb_srv_type_t type
Definition: lb.api:49
int lb_flush_vip_as(u32 vip_index, u32 as_index)
Definition: lb.c:762
#define REPLY_MACRO(t)
Add an application server for a given VIP.
Definition: lb.api:67
vlib_main_t * vm
Definition: in2out_ed.c:1810
u32 new_length
Definition: lb.h:579
#define ip46_prefix_is_ip4(ip46, len)
Definition: util.h:27
An API client registration, only in vpp/vlib.
Definition: api_common.h:46
#define BAD_SW_IF_INDEX_LABEL
format_function_t format_ip6_address
Definition: format.h:91
static void vl_api_lb_add_del_intf_nat4_t_handler(vl_api_lb_add_del_intf_nat4_t *mp)
Definition: api.c:376
vlib_main_t * vlib_main
Definition: lb.h:568
static vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
Definition: api.h:57
u32 sticky_buckets_per_core
Definition: lb.api:21
Reply all configured vip.
Definition: lb.api:112
Add a virtual address (or prefix)
Definition: lb.api:41
manual_print typedef address
Definition: ip_types.api:84
lb_vip_type_t
The load balancer supports IPv4 and IPv6 traffic and GRE4, GRE6, L3DSR and NAT4, NAT6 encap...
Definition: lb.h:208
vl_api_address_with_prefix_t pfx
Definition: lb.api:44
static void setup_message_id_table(api_main_t *am)
Definition: api.c:802
u8 protocol[default=255]
Definition: lb.api:71
int lb_vip_find_index(ip46_address_t *prefix, u8 plen, u8 protocol, u16 port, u32 *vip_index)
Definition: lb.c:554
ip46_address_t prefix
Definition: lb.h:574
lb_vip_encap_args_t encap_args
Definition: lb.h:580
static void send_lb_as_details(vl_api_registration_t *reg, u32 context, lb_vip_t *vip)
Definition: api.c:277
vl_api_interface_index_t sw_if_index
Definition: lb.api:174
u16 target_port
Definition: lb.h:233
int lb_nat6_interface_add_del(u32 sw_if_index, int is_del)
Definition: lb.c:1344
Configure Load-Balancer global parameters (unlike the CLI, both ip4_src_address and ip6_src_address n...
Definition: lb.api:15
int lb_vip_add(lb_vip_add_args_t args, u32 *vip_index)
Definition: lb.c:1046
vl_api_address_with_prefix_t pfx
Definition: lb.api:70
static void * vl_api_lb_flush_vip_t_print(vl_api_lb_flush_vip_t *mp, void *handle)
Definition: api.c:416
vl_api_ip4_address_t ip4_src_address
Definition: lb.api:19
vl_api_interface_index_t sw_if_index
Definition: lb.api:161
static void vl_api_lb_as_dump_t_handler(vl_api_lb_as_dump_t *mp)
Definition: api.c:316
u8 protocol[default=255]
Definition: lb.api:45
static void vl_api_lb_add_del_vip_t_handler(vl_api_lb_add_del_vip_t *mp)
Definition: api.c:83
static void vl_api_lb_flush_vip_t_handler(vl_api_lb_flush_vip_t *mp)
Definition: api.c:349
vl_api_lb_encap_type_t encap
Definition: lb.api:47
static void vl_api_lb_add_del_as_t_handler(vl_api_lb_add_del_as_t *mp)
Definition: api.c:179
lb_vip_type_t type
Definition: lb.h:578
Load balancing service is provided per VIP+protocol+port.
Definition: lb.h:262
u32 * as_indexes
Pool of AS indexes used for this VIP.
Definition: lb.h:332
static clib_error_t * lb_api_init(vlib_main_t *vm)
Definition: api.c:429
lb_as_details
Definition: lb.api:143
#define VALIDATE_SW_IF_INDEX(mp)