FD.io VPP  v19.04.2-12-g66b1689
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 <lb/lb.h>
17 
18 #include <vppinfra/byte_order.h>
19 #include <vppinfra/string.h>
20 #include <vlibapi/api.h>
21 #include <vlibapi/api.h>
22 #include <vlibmemory/api.h>
23 
24 
25 #define vl_msg_id(n,h) n,
26 typedef enum {
27 #include <lb/lb.api.h>
28  /* We'll want to know how many messages IDs we need... */
30 } vl_msg_id_t;
31 #undef vl_msg_id
32 
33 
34 /* define message structures */
35 #define vl_typedefs
36 #include <lb/lb.api.h>
37 #undef vl_typedefs
38 
39 /* define generated endian-swappers */
40 #define vl_endianfun
41 #include <lb/lb.api.h>
42 #undef vl_endianfun
43 
44 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
45 
46 /* Get the API version number */
47 #define vl_api_version(n,v) static u32 api_version=(v);
48 #include <lb/lb.api.h>
49 #undef vl_api_version
50 
51 #define vl_msg_name_crc_list
52 #include <lb/lb.api.h>
53 #undef vl_msg_name_crc_list
54 
55 
56 #define REPLY_MSG_ID_BASE lbm->msg_id_base
58 
59 static void
61 {
62 #define _(id,n,crc) \
63  vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + lbm->msg_id_base);
64  foreach_vl_msg_name_crc_lb;
65 #undef _
66 }
67 
68 /* Macro to finish up custom dump fns */
69 #define FINISH \
70  vec_add1 (s, 0); \
71  vl_print (handle, (char *)s); \
72  vec_free (s); \
73  return handle;
74 
75 static void
78 {
79  lb_main_t *lbm = &lb_main;
80  vl_api_lb_conf_reply_t * rmp;
81  int rv = 0;
82 
83  if (mp->sticky_buckets_per_core == ~0) {
85  }
86  if (mp->flow_timeout == ~0) {
87  mp->flow_timeout = lbm->flow_timeout;
88  }
89 
93  mp->flow_timeout);
94 
95  REPLY_MACRO (VL_API_LB_CONF_REPLY);
96 }
97 
98 static void *vl_api_lb_conf_t_print
99 (vl_api_lb_conf_t *mp, void * handle)
100 {
101  u8 * s;
102  s = format (0, "SCRIPT: lb_conf ");
103  s = format (s, "%U ", format_ip4_address, (ip4_address_t *)&mp->ip4_src_address);
104  s = format (s, "%U ", format_ip6_address, (ip6_address_t *)&mp->ip6_src_address);
105  s = format (s, "%u ", mp->sticky_buckets_per_core);
106  s = format (s, "%u ", mp->flow_timeout);
107  FINISH;
108 }
109 
110 
111 static void
114 {
115  lb_main_t *lbm = &lb_main;
116  vl_api_lb_conf_reply_t * rmp;
117  int rv = 0;
118  lb_vip_add_args_t args;
119 
120  /* if port == 0, it means all-port VIP */
121  if (mp->port == 0)
122  {
123  mp->protocol = ~0;
124  }
125 
126  memcpy (&(args.prefix.ip6), mp->ip_prefix, sizeof(args.prefix.ip6));
127 
128  if (mp->is_del) {
129  u32 vip_index;
130  if (!(rv = lb_vip_find_index(&(args.prefix), mp->prefix_length,
131  mp->protocol, ntohs(mp->port), &vip_index)))
132  rv = lb_vip_del(vip_index);
133  } else {
134  u32 vip_index;
135  lb_vip_type_t type = 0;
136 
137  if (ip46_prefix_is_ip4(&(args.prefix), mp->prefix_length)) {
138  if (mp->encap == LB_ENCAP_TYPE_GRE4)
139  type = LB_VIP_TYPE_IP4_GRE4;
140  else if (mp->encap == LB_ENCAP_TYPE_GRE6)
141  type = LB_VIP_TYPE_IP4_GRE6;
142  else if (mp->encap == LB_ENCAP_TYPE_L3DSR)
143  type = LB_VIP_TYPE_IP4_L3DSR;
144  else if (mp->encap == LB_ENCAP_TYPE_NAT4)
145  type = LB_VIP_TYPE_IP4_NAT4;
146  } else {
147  if (mp->encap == LB_ENCAP_TYPE_GRE4)
148  type = LB_VIP_TYPE_IP6_GRE4;
149  else if (mp->encap == LB_ENCAP_TYPE_GRE6)
150  type = LB_VIP_TYPE_IP6_GRE6;
151  else if (mp->encap == LB_ENCAP_TYPE_NAT6)
152  type = LB_VIP_TYPE_IP6_NAT6;
153  }
154 
155  args.plen = mp->prefix_length;
156  args.protocol = mp->protocol;
157  args.port = ntohs(mp->port);
158  args.type = type;
159  args.new_length = ntohl(mp->new_flows_table_length);
160 
161  if (mp->encap == LB_ENCAP_TYPE_L3DSR) {
162  args.encap_args.dscp = (u8)(mp->dscp & 0x3F);
163  }
164  else if ((mp->encap == LB_ENCAP_TYPE_NAT4)
165  ||(mp->encap == LB_ENCAP_TYPE_NAT6)) {
166  args.encap_args.srv_type = mp->type;
167  args.encap_args.target_port = ntohs(mp->target_port);
168  }
169 
170  rv = lb_vip_add(args, &vip_index);
171  }
172  REPLY_MACRO (VL_API_LB_ADD_DEL_VIP_REPLY);
173 }
174 
176 (vl_api_lb_add_del_vip_t *mp, void * handle)
177 {
178  u8 * s;
179  s = format (0, "SCRIPT: lb_add_del_vip ");
180  s = format (s, "%U ", format_ip46_prefix,
181  (ip46_address_t *)mp->ip_prefix, mp->prefix_length, IP46_TYPE_ANY);
182 
183  s = format (s, "%s ", (mp->encap == LB_ENCAP_TYPE_GRE4)? "gre4"
184  : (mp->encap == LB_ENCAP_TYPE_GRE6)? "gre6"
185  : (mp->encap == LB_ENCAP_TYPE_NAT4)? "nat4"
186  : (mp->encap == LB_ENCAP_TYPE_NAT6)? "nat6"
187  : "l3dsr");
188 
189  if (mp->encap==LB_ENCAP_TYPE_L3DSR)
190  {
191  s = format (s, "dscp %u ", mp->dscp);
192  }
193 
194  if ((mp->encap==LB_ENCAP_TYPE_NAT4)
195  || (mp->encap==LB_ENCAP_TYPE_NAT6))
196  {
197  s = format (s, "type %u ", mp->type);
198  s = format (s, "port %u ", mp->port);
199  s = format (s, "target_port %u ", mp->target_port);
200  }
201 
202  s = format (s, "%u ", mp->new_flows_table_length);
203  s = format (s, "%s ", mp->is_del?"del":"add");
204  FINISH;
205 }
206 
207 static void
210 {
211  lb_main_t *lbm = &lb_main;
212  vl_api_lb_conf_reply_t * rmp;
213  int rv = 0;
214  u32 vip_index;
215  ip46_address_t vip_ip_prefix;
216 
217  memcpy(&vip_ip_prefix.ip6, mp->vip_ip_prefix,
218  sizeof(vip_ip_prefix.ip6));
219 
220  ip46_address_t as_address;
221 
222  memcpy(&as_address.ip6, mp->as_address,
223  sizeof(as_address.ip6));
224 
225  if ((rv = lb_vip_find_index(&vip_ip_prefix, mp->vip_prefix_length,
226  mp->protocol, ntohs(mp->port), &vip_index)))
227  goto done;
228 
229  if (mp->is_del)
230  rv = lb_vip_del_ass(vip_index, &as_address, 1, mp->is_flush);
231  else
232  rv = lb_vip_add_ass(vip_index, &as_address, 1);
233 
234 done:
235  REPLY_MACRO (VL_API_LB_ADD_DEL_AS_REPLY);
236 }
237 
238 static void *vl_api_lb_add_del_as_t_print
239 (vl_api_lb_add_del_as_t *mp, void * handle)
240 {
241  u8 * s;
242  s = format (0, "SCRIPT: lb_add_del_as ");
243  s = format (s, "%U ", format_ip46_prefix,
244  (ip46_address_t *)mp->vip_ip_prefix, mp->vip_prefix_length, IP46_TYPE_ANY);
245  s = format (s, "%U ", format_ip46_address,
246  (ip46_address_t *)mp->as_address, IP46_TYPE_ANY);
247  s = format (s, "%s ", mp->is_del?"del":"add");
248  FINISH;
249 }
250 
251 static void
254 {
255  lb_main_t *lbm = &lb_main;
256  int rv = 0;
257  ip46_address_t vip_prefix;
258  u8 vip_plen;
259  u32 vip_index;
260  vl_api_lb_flush_vip_reply_t * rmp;
261 
262  if (mp->port == 0)
263  {
264  mp->protocol = ~0;
265  }
266 
267  memcpy (&(vip_prefix.ip6), mp->ip_prefix, sizeof(vip_prefix.ip6));
268 
269  vip_plen = mp->prefix_length;
270 
271  rv = lb_vip_find_index(&vip_prefix, vip_plen, mp->protocol,
272  ntohs(mp->port), &vip_index);
273 
274  rv = lb_flush_vip_as(vip_index, ~0);
275 
276  REPLY_MACRO (VL_API_LB_FLUSH_VIP_REPLY);
277 }
278 
279 static void *vl_api_lb_flush_vip_t_print
280 (vl_api_lb_flush_vip_t *mp, void * handle)
281 {
282  u8 * s;
283  s = format (0, "SCRIPT: lb_add_del_vip ");
284  s = format (s, "%U ", format_ip46_prefix,
285  (ip46_address_t *)mp->ip_prefix, mp->prefix_length, IP46_TYPE_ANY);
286 
287  s = format (s, "protocol %u ", mp->protocol);
288  s = format (s, "port %u ", mp->port);
289 
290  FINISH;
291 }
292 
293 /* List of message types that this plugin understands */
294 #define foreach_lb_plugin_api_msg \
295 _(LB_CONF, lb_conf) \
296 _(LB_ADD_DEL_VIP, lb_add_del_vip) \
297 _(LB_ADD_DEL_AS, lb_add_del_as) \
298 _(LB_FLUSH_VIP, lb_flush_vip)
299 
301 {
302  lb_main_t *lbm = &lb_main;
303  u8 *name = format (0, "lb_%08x%c", api_version, 0);
305  ((char *) name, VL_MSG_FIRST_AVAILABLE);
306 
307 #define _(N,n) \
308  vl_msg_api_set_handlers((VL_API_##N + lbm->msg_id_base), \
309  #n, \
310  vl_api_##n##_t_handler, \
311  vl_noop_handler, \
312  vl_api_##n##_t_endian, \
313  vl_api_##n##_t_print, \
314  sizeof(vl_api_##n##_t), 1);
316 #undef _
317 
318  /* Add our API messages to the global name_crc hash table */
320 
321  return 0;
322 }
323 
int lb_vip_del_ass(u32 vip_index, ip46_address_t *addresses, u32 n, u8 flush)
Definition: lb.c:847
static void vl_api_lb_conf_t_handler(vl_api_lb_conf_t *mp)
Definition: api.c:77
u32 flow_timeout
Definition: lb.api:21
static void * vl_api_lb_add_del_vip_t_print(vl_api_lb_add_del_vip_t *mp, void *handle)
Definition: api.c:176
u32 per_cpu_sticky_buckets
Number of buckets in the per-cpu sticky hash table.
Definition: lb.h:521
Optimized string handling code, including c11-compliant "safe C library" variants.
u16 msg_id_base
API dynamically registered base ID.
Definition: lb.h:562
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:472
u8 * format_ip46_prefix(u8 *s, va_list *args)
Definition: util.c:54
int lb_vip_add_ass(u32 vip_index, ip46_address_t *addresses, u32 n)
Definition: lb.c:579
format_function_t format_ip46_address
Definition: format.h:61
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
static void * vl_api_lb_conf_t_print(vl_api_lb_conf_t *mp, void *handle)
Definition: api.c:99
Flush a given vip.
Definition: lb.api:87
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:239
vl_msg_id_t
Definition: api.c:26
format_function_t format_ip4_address
Definition: format.h:75
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:163
lb_main_t lb_main
Definition: lb.c:28
u32 flow_timeout
Flow timeout in seconds.
Definition: lb.h:526
Definition: lb.h:469
unsigned int u32
Definition: types.h:88
#define FINISH
Definition: api.c:69
vl_api_ip6_address_t ip6_src_address
Definition: lb.api:19
int lb_vip_del(u32 vip_index)
Definition: lb.c:1194
#define foreach_lb_plugin_api_msg
Definition: api.c:294
int lb_flush_vip_as(u32 vip_index, u32 as_index)
Definition: lb.c:756
#define REPLY_MACRO(t)
Add an application server for a given VIP.
Definition: lb.api:67
u8 name[64]
Definition: memclnt.api:152
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:202
u32 new_length
Definition: lb.h:578
u8 vip_ip_prefix[16]
Definition: lb.api:70
#define ip46_prefix_is_ip4(ip46, len)
Definition: util.h:27
u8 ip_prefix[16]
Definition: lb.api:90
format_function_t format_ip6_address
Definition: format.h:93
vlib_main_t * vm
Definition: buffer.c:312
u32 new_flows_table_length
Definition: lb.api:52
u32 sticky_buckets_per_core
Definition: lb.api:20
Add a virtual address (or prefix)
Definition: lb.api:40
lb_vip_type_t
The load balancer supports IPv4 and IPv6 traffic and GRE4, GRE6, L3DSR and NAT4, NAT6 encap...
Definition: lb.h:207
u8 as_address[16]
Definition: lb.api:74
int lb_vip_find_index(ip46_address_t *prefix, u8 plen, u8 protocol, u16 port, u32 *vip_index)
Definition: lb.c:549
ip46_address_t prefix
Definition: lb.h:573
lb_vip_encap_args_t encap_args
Definition: lb.h:579
u16 target_port
Definition: lb.h:232
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:1040
static void * vl_api_lb_flush_vip_t_print(vl_api_lb_flush_vip_t *mp, void *handle)
Definition: api.c:280
vl_api_ip4_address_t ip4_src_address
Definition: lb.api:18
static void vl_api_lb_add_del_vip_t_handler(vl_api_lb_add_del_vip_t *mp)
Definition: api.c:113
static void vl_api_lb_flush_vip_t_handler(vl_api_lb_flush_vip_t *mp)
Definition: api.c:253
static void vl_api_lb_add_del_as_t_handler(vl_api_lb_add_del_as_t *mp)
Definition: api.c:209
static void setup_message_id_table(lb_main_t *lbm, api_main_t *am)
Definition: api.c:60
lb_vip_type_t type
Definition: lb.h:577
api_main_t api_main
Definition: api_shared.c:35
static clib_error_t * lb_api_init(vlib_main_t *vm)
Definition: api.c:300
u16 vl_msg_api_get_msg_ids(const char *name, int n)
Definition: api_shared.c:880