FD.io VPP  v19.08.3-2-gbabecb413
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/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
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
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) {
82  }
83  if (mp->flow_timeout == ~0) {
84  mp->flow_timeout = lbm->flow_timeout;
85  }
86 
90  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
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 
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 
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
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 *vl_api_lb_add_del_as_t_print
237 (vl_api_lb_add_del_as_t *mp, void * handle)
238 {
239  u8 * s;
240  ip46_address_t address;
241  s = format (0, "SCRIPT: lb_add_del_as ");
242  s = format (s, "%U ", format_vl_api_prefix,
243  &mp->pfx);
244  s = format(s, "%u ", mp->protocol);
245  if (ip_address_decode (&mp->as_address, &address) == IP46_TYPE_IP6)
246  s = format (s, "%U ", format_ip6_address,
247  (ip6_address_t *) & address.ip6);
248  else
249  s = format (s, "%U ", format_ip4_address,
250  (ip6_address_t *) & address.ip4);
251  s = format (s, "%s ", mp->is_del?"del":"add");
252  FINISH;
253 }
254 
255 static void
257 (vl_api_lb_vip_dump_t * mp)
258 {
259 
261  reg = vl_api_client_index_to_registration (mp->client_index);
262  if (!reg)
263  return;
264 
265  lb_main_t *lbm = &lb_main;
267  int msg_size = 0;
268  lb_vip_t *vip = 0;
269 
270  /* construct vip list */
271  pool_foreach(vip, lbm->vips, {
272  /* Hide dummy VIP */
273  if (vip != lbm->vips) {
274  msg_size = sizeof (*rmp);
275  rmp = vl_msg_api_alloc (msg_size);
276  memset (rmp, 0, msg_size);
277  rmp->_vl_msg_id =
278  htons (VL_API_LB_VIP_DETAILS + lbm->msg_id_base);
279  rmp->context = mp->context;
280 
281  ip_address_encode(&vip->prefix, IP46_TYPE_ANY, &rmp->vip.pfx.address);
282  rmp->vip.pfx.len = vip->plen;
283  rmp->vip.protocol = htonl (vip->protocol);
284  rmp->vip.port = htons(vip->port);
285  rmp->encap = htonl(vip->type);
286  rmp->dscp = vip->encap_args.dscp;
287  rmp->srv_type = vip->encap_args.srv_type;
288  rmp->target_port = htons(vip->encap_args.target_port);
289  rmp->flow_table_length = htonl(vip->new_flow_table_mask + 1);
290 
291  vl_api_send_msg (reg, (u8 *) rmp);
292  }
293  });
294 
295 
296 }
297 
298 static void send_lb_as_details
300 {
302  lb_main_t *lbm = &lb_main;
303  int msg_size = 0;
304  u32 *as_index;
305  u32 asindex = 0;
306 
307  /* construct as list under this vip */
308  lb_as_t *as;
309 
310  pool_foreach(as_index, vip->as_indexes, {
311  /* Hide dummy As for specific VIP */
312  if (*as_index != 0) {
313  as = &lbm->ass[*as_index];
314  msg_size = sizeof (*rmp);
315  rmp = vl_msg_api_alloc (msg_size);
316  memset (rmp, 0, msg_size);
317  rmp->_vl_msg_id =
318  htons (VL_API_LB_AS_DETAILS + lbm->msg_id_base);
319  rmp->context = context;
320  ip_address_encode(&vip->prefix, IP46_TYPE_ANY, (vl_api_address_t *)&rmp->vip.pfx.address);
321  rmp->vip.pfx.len = vip->plen;
322  rmp->vip.protocol = htonl (vip->protocol);
323  rmp->vip.port = htons(vip->port);
324  ip_address_encode(&as->address, IP46_TYPE_ANY, &rmp->app_srv);
325  rmp->flags = as->flags;
326  rmp->in_use_since = htonl(as->last_used);
327 
328  vl_api_send_msg (reg, (u8 *) rmp);
329  asindex++;
330  }
331  });
332 
333 
334 }
335 
336 static void
338 (vl_api_lb_as_dump_t * mp)
339 {
340  lb_main_t *lbm = &lb_main;
341  lb_vip_t *vip = 0;
342  u8 dump_all = 0;
343  ip46_address_t prefix;
344 
346  reg = vl_api_client_index_to_registration (mp->client_index);
347  if (!reg)
348  return;
349 
350  clib_memcpy(&prefix.ip6, mp->pfx.address.un.ip6, sizeof(mp->pfx.address.un.ip6));
351 
352  dump_all = (prefix.ip6.as_u64[0] == 0) && (prefix.ip6.as_u64[1] == 0);
353 
354  /* *INDENT-OFF* */
355  pool_foreach(vip, lbm->vips,
356  ({
357  if ( dump_all
358  || ((prefix.as_u64[0] == vip->prefix.as_u64[0])
359  && (prefix.as_u64[1] == vip->prefix.as_u64[1])
360  && (mp->protocol == vip->protocol)
361  && (mp->port == vip->port)) )
362  {
363  send_lb_as_details(reg, mp->context, vip);
364  }
365  }));
366  /* *INDENT-ON* */
367 }
368 
369 static void
372 {
373  lb_main_t *lbm = &lb_main;
374  int rv = 0;
375  ip46_address_t vip_prefix;
376  u8 vip_plen;
377  u32 vip_index;
378  vl_api_lb_flush_vip_reply_t * rmp;
379 
380  if (mp->port == 0)
381  {
382  mp->protocol = ~0;
383  }
384 
385  memcpy (&(vip_prefix.ip6), mp->pfx.address.un.ip6, sizeof(vip_prefix.ip6));
386 
387  vip_plen = mp->pfx.len;
388 
389  rv = lb_vip_find_index(&vip_prefix, vip_plen, mp->protocol,
390  ntohs(mp->port), &vip_index);
391 
392  rv = lb_flush_vip_as(vip_index, ~0);
393 
394  REPLY_MACRO (VL_API_LB_FLUSH_VIP_REPLY);
395 }
396 
397 static void *vl_api_lb_flush_vip_t_print
398 (vl_api_lb_flush_vip_t *mp, void * handle)
399 {
400  u8 * s;
401  s = format (0, "SCRIPT: lb_add_del_vip ");
402  s = format (s, "%U/%d", format_vl_api_address,
403  &mp->pfx.address, mp->pfx.len);
404  s = format (s, "protocol %u ", mp->protocol);
405  s = format (s, "port %u ", mp->port);
406 
407  FINISH;
408 }
409 
410 /* Set up the API message handling tables */
411 static clib_error_t *
413 {
414 lb_main_t *lbm = &lb_main;
415 #define _(N,n) \
416  vl_msg_api_set_handlers((VL_API_##N + lbm->msg_id_base), \
417  #n, \
418  vl_api_##n##_t_handler, \
419  vl_noop_handler, \
420  vl_api_##n##_t_endian, \
421  vl_api_##n##_t_print, \
422  sizeof(vl_api_##n##_t), 1);
424 #undef _
425 
426  return 0;
427 }
428 
429 #define vl_msg_name_crc_list
430 #include <lb/lb_all_api_h.h>
431 #undef vl_msg_name_crc_list
432 
433 static void
435 {
436 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + lmp->msg_id_base);
437  foreach_vl_msg_name_crc_lb ;
438 #undef _
439 }
440 
442 {
443  lb_main_t * lbm = &lb_main;
444  clib_error_t * error = 0;
445  u8 * name;
446 
447  lbm->vlib_main = vm;
448  lbm->vnet_main = vnet_get_main();
449 
450  name = format (0, "lb_%08x%c", api_version, 0);
451 
452  /* Ask for a correctly-sized block of API message decode slots */
454  ((char *) name, VL_MSG_FIRST_AVAILABLE);
455 
456  error = lb_plugin_api_hookup (vm);
457 
458  /* Add our API messages to the global name_crc hash table */
460 
461  vec_free (name);
462 
463  return error;
464 }
465 
int lb_vip_del_ass(u32 vip_index, ip46_address_t *addresses, u32 n, u8 flush)
Definition: lb.c:850
vl_api_address_t as_address
Definition: lb.api:70
static void vl_api_lb_conf_t_handler(vl_api_lb_conf_t *mp)
Definition: api.c:74
u32 flow_timeout
Definition: lb.api:21
typedef address
Definition: ip_types.api:83
static void * vl_api_lb_add_del_vip_t_print(vl_api_lb_add_del_vip_t *mp, void *handle)
Definition: api.c:173
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
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:473
int lb_vip_add_ass(u32 vip_index, ip46_address_t *addresses, u32 n)
Definition: lb.c:582
u32 new_flows_table_length[default=1024]
Definition: lb.api:50
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
vl_api_mprefix_t prefix
Definition: ip.api:456
static void * vl_api_lb_conf_t_print(vl_api_lb_conf_t *mp, void *handle)
Definition: api.c:96
Flush a given vip.
Definition: lb.api:82
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:237
#define clib_memcpy(d, s, n)
Definition: string.h:180
format_function_t format_ip4_address
Definition: format.h:75
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:493
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
vl_api_address_with_prefix_t pfx
Definition: lb.api:85
static void vl_api_lb_vip_dump_t_handler(vl_api_lb_vip_dump_t *mp)
Definition: api.c:257
lb_main_t lb_main
Definition: lb.c:29
u32 flow_timeout
Flow timeout in seconds.
Definition: lb.h:527
Definition: lb.h:470
unsigned int u32
Definition: types.h:88
static clib_error_t * lb_plugin_api_hookup(vlib_main_t *vm)
Definition: api.c:412
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
vl_api_fib_path_type_t type
Definition: fib_types.api:123
#define FINISH
Definition: api.c:66
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:19
int lb_vip_del(u32 vip_index)
Definition: lb.c:1199
#define foreach_lb_plugin_api_msg
Definition: api.c:56
vl_api_lb_srv_type_t type
Definition: lb.api:47
int lb_flush_vip_as(u32 vip_index, u32 as_index)
Definition: lb.c:759
#define REPLY_MACRO(t)
Add an application server for a given VIP.
Definition: lb.api:64
u8 name[64]
Definition: memclnt.api:152
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:203
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
format_function_t format_ip6_address
Definition: format.h:93
vlib_main_t * vm
Definition: buffer.c:323
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:341
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:20
Reply all configured vip.
Definition: lb.api:108
Add a virtual address (or prefix)
Definition: lb.api:39
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:42
u8 protocol[default=255]
Definition: lb.api:68
int lb_vip_find_index(ip46_address_t *prefix, u8 plen, u8 protocol, u16 port, u32 *vip_index)
Definition: lb.c:551
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:299
u16 target_port
Definition: lb.h:233
Configure Load-Balancer global parameters (unlike the CLI, both ip4_src_address and ip6_src_address n...
Definition: lb.api:14
int lb_vip_add(lb_vip_add_args_t args, u32 *vip_index)
Definition: lb.c:1045
vl_api_address_with_prefix_t pfx
Definition: lb.api:67
static void * vl_api_lb_flush_vip_t_print(vl_api_lb_flush_vip_t *mp, void *handle)
Definition: api.c:398
vl_api_ip4_address_t ip4_src_address
Definition: lb.api:18
static void vl_api_lb_as_dump_t_handler(vl_api_lb_as_dump_t *mp)
Definition: api.c:338
static void setup_message_id_table(lb_main_t *lmp, api_main_t *am)
Definition: api.c:434
u8 protocol[default=255]
Definition: lb.api:43
static void vl_api_lb_add_del_vip_t_handler(vl_api_lb_add_del_vip_t *mp)
Definition: api.c:110
static void vl_api_lb_flush_vip_t_handler(vl_api_lb_flush_vip_t *mp)
Definition: api.c:371
vl_api_lb_encap_type_t encap
Definition: lb.api:45
static void vl_api_lb_add_del_as_t_handler(vl_api_lb_add_del_as_t *mp)
Definition: api.c:206
lb_vip_type_t type
Definition: lb.h:578
u32 context
Definition: gre.api:45
api_main_t api_main
Definition: api_shared.c:35
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:441
lb_as_details
Definition: lb.api:138
u16 vl_msg_api_get_msg_ids(const char *name, int n)
Definition: api_shared.c:957