FD.io VPP  v16.09
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 #include <vlibsocket/api.h>
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 /* Macro to finish up custom dump fns */
51 #define FINISH \
52  vec_add1 (s, 0); \
53  vl_print (handle, (char *)s); \
54  vec_free (s); \
55  return handle;
56 
57 /*
58  * A handy macro to set up a message reply.
59  * Assumes that the following variables are available:
60  * mp - pointer to request message
61  * rmp - pointer to reply message type
62  * rv - return value
63  */
64 
65 #define REPLY_MACRO(t) \
66 do { \
67  unix_shared_memory_queue_t * q = \
68  vl_api_client_index_to_input_queue (mp->client_index); \
69  if (!q) \
70  return; \
71  \
72  rmp = vl_msg_api_alloc (sizeof (*rmp)); \
73  rmp->_vl_msg_id = ntohs((t)+lbm->msg_id_base); \
74  rmp->context = mp->context; \
75  rmp->retval = ntohl(rv); \
76  \
77  vl_msg_api_send_shmem (q, (u8 *)&rmp); \
78 } while(0);
79 
80 static void
83 {
84  lb_main_t *lbm = &lb_main;
86  int rv = 0;
87 
91  mp->flow_timeout);
92 
93  REPLY_MACRO (VL_API_LB_CONF_REPLY);
94 }
95 
96 static void *vl_api_lb_conf_t_print
97 (vl_api_lb_conf_t *mp, void * handle)
98 {
99  u8 * s;
100  s = format (0, "SCRIPT: lb_conf ");
101  s = format (s, "%U ", format_ip4_address, (ip4_address_t *)&mp->ip4_src_address);
102  s = format (s, "%U ", format_ip6_address, (ip6_address_t *)mp->ip6_src_address);
103  s = format (s, "%u ", mp->sticky_buckets_per_core);
104  s = format (s, "%u ", mp->flow_timeout);
105  FINISH;
106 }
107 
108 
109 static void
112 {
113  lb_main_t *lbm = &lb_main;
115  int rv = 0;
116  ip46_address_t prefix;
117  memcpy(&prefix.ip6, mp->ip_prefix, sizeof(prefix.ip6));
118 
119  if (mp->is_del) {
120  u32 vip_index;
121  if (!(rv = lb_vip_find_index(&prefix, mp->prefix_length, &vip_index)))
122  rv = lb_vip_del(vip_index);
123  } else {
124  u32 vip_index;
126  if (ip46_prefix_is_ip4(&prefix, mp->prefix_length)) {
128  } else {
130  }
131 
132  rv = lb_vip_add(&prefix, mp->prefix_length, type,
133  mp->new_flows_table_length, &vip_index);
134  }
135  REPLY_MACRO (VL_API_LB_CONF_REPLY);
136 }
137 
139 (vl_api_lb_add_del_vip_t *mp, void * handle)
140 {
141  u8 * s;
142  s = format (0, "SCRIPT: lb_add_del_vip ");
143  s = format (s, "%U ", format_ip46_prefix,
144  (ip46_address_t *)mp->ip_prefix, mp->prefix_length, IP46_TYPE_ANY);
145  s = format (s, "%s ", mp->is_gre4?"gre4":"gre6");
146  s = format (s, "%u ", mp->new_flows_table_length);
147  s = format (s, "%s ", mp->is_del?"del":"add");
148  FINISH;
149 }
150 
151 static void
154 {
155  lb_main_t *lbm = &lb_main;
157  int rv = 0;
158  u32 vip_index;
159  if ((rv = lb_vip_find_index((ip46_address_t *)mp->vip_ip_prefix,
160  mp->vip_prefix_length, &vip_index)))
161  goto done;
162 
163  if (mp->is_del)
164  rv = lb_vip_del_ass(vip_index, (ip46_address_t *)mp->as_address, 1);
165  else
166  rv = lb_vip_add_ass(vip_index, (ip46_address_t *)mp->as_address, 1);
167 
168 done:
169  REPLY_MACRO (VL_API_LB_CONF_REPLY);
170 }
171 
172 static void *vl_api_lb_add_del_as_t_print
173 (vl_api_lb_add_del_as_t *mp, void * handle)
174 {
175  u8 * s;
176  s = format (0, "SCRIPT: lb_add_del_as ");
177  s = format (s, "%U ", format_ip46_prefix,
178  (ip46_address_t *)mp->vip_ip_prefix, mp->vip_prefix_length, IP46_TYPE_ANY);
179  s = format (s, "%U ", format_ip46_address,
180  (ip46_address_t *)mp->as_address, IP46_TYPE_ANY);
181  s = format (s, "%s ", mp->is_del?"del":"add");
182  FINISH;
183 }
184 
185 /* List of message types that this plugin understands */
186 #define foreach_lb_plugin_api_msg \
187 _(LB_CONF, lb_conf) \
188 _(LB_ADD_DEL_VIP, lb_add_del_vip) \
189 _(LB_ADD_DEL_AS, lb_add_del_as)
190 
192 {
193  lb_main_t *lbm = &lb_main;
194  u8 *name = format (0, "lb_%08x%c", api_version, 0);
196  ((char *) name, VL_MSG_FIRST_AVAILABLE);
197 
198 #define _(N,n) \
199  vl_msg_api_set_handlers((VL_API_##N + lbm->msg_id_base), \
200  #n, \
201  vl_api_##n##_t_handler, \
202  vl_noop_handler, \
203  vl_api_##n##_t_endian, \
204  vl_api_##n##_t_print, \
205  sizeof(vl_api_##n##_t), 1);
207 #undef _
208 
209  return 0;
210 }
211 
format_function_t format_ip46_address
Definition: format.h:54
static void vl_api_lb_conf_t_handler(vl_api_lb_conf_t *mp)
Definition: api.c:82
u32 flow_timeout
Definition: lb.api:18
format_function_t format_ip6_address
Definition: format.h:87
static clib_error_t * lb_api_init(vlib_main_t *vm)
Definition: api.c:191
bad routing header type(not 4)") sr_error (NO_MORE_SEGMENTS
u16 msg_id_base
API dynamically registered base ID.
Definition: lb.h:266
u8 * format_ip46_prefix(u8 *s, va_list *args)
Definition: util.c:54
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:335
int lb_vip_add_ass(u32 vip_index, ip46_address_t *addresses, u32 n)
Definition: lb.c:398
format_function_t format_ip4_address
Definition: format.h:71
u8 ip6_src_address[16]
Definition: lb.api:16
#define foreach_lb_plugin_api_msg
Definition: api.c:186
static void * vl_api_lb_conf_t_print(vl_api_lb_conf_t *mp, void *handle)
Definition: api.c:97
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
lb_main_t lb_main
Definition: lb.c:26
Definition: lb.h:207
static void * vl_api_lb_add_del_as_t_print(vl_api_lb_add_del_as_t *mp, void *handle)
Definition: api.c:173
static void vl_api_lb_add_del_vip_t_handler(vl_api_lb_add_del_vip_t *mp)
Definition: api.c:111
static void * vl_api_lb_add_del_vip_t_print(vl_api_lb_add_del_vip_t *mp, void *handle)
Definition: api.c:139
int lb_vip_del(u32 vip_index)
Definition: lb.c:723
Add an application server for a given VIP.
Definition: lb.api:59
u8 vip_ip_prefix[16]
Definition: lb.api:62
#define ip46_prefix_is_ip4(ip46, len)
Definition: util.h:27
int lb_vip_add(ip46_address_t *prefix, u8 plen, lb_vip_type_t type, u32 new_length, u32 *vip_index)
Definition: lb.c:665
#define REPLY_MACRO(t)
Definition: api.c:65
u32 new_flows_table_length
Definition: lb.api:42
#define FINISH
Definition: api.c:51
int lb_vip_del_ass(u32 vip_index, ip46_address_t *addresses, u32 n)
Definition: lb.c:530
u32 sticky_buckets_per_core
Definition: lb.api:17
Add a virtual address (or prefix)
Definition: lb.api:36
unsigned int u32
Definition: types.h:88
lb_vip_type_t
The load balancer supports IPv4 and IPv6 traffic and GRE4 and GRE6 encap.
Definition: lb.h:112
u8 as_address[16]
Definition: lb.api:64
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:418
int lb_vip_find_index(ip46_address_t *prefix, u8 plen, u32 *vip_index)
Definition: lb.c:371
vl_msg_id_t
Definition: api.c:25
static void vl_api_lb_add_del_as_t_handler(vl_api_lb_add_del_as_t *mp)
Definition: api.c:153
Configure Load-Balancer global parameters.
Definition: lb.api:11
unsigned char u8
Definition: types.h:56
u32 ip4_src_address
Definition: lb.api:15
u16 vl_msg_api_get_msg_ids(char *name, int n)
Definition: api_shared.c:1269