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