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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vnet/vnet.h>
17 #include <vnet/plugin/plugin.h>
20 #include <vppinfra/byte_order.h>
21 #include <vppinfra/string.h>
22 #include <vlibapi/api.h>
23 #include <vlibapi/api.h>
24 #include <vlibmemory/api.h>
25 #include <vpp/app/version.h>
27 /* define message IDs */
28 #include <lb/lb_msg_enum.h>
30 /* define message structures */
32 #include <lb/lb_all_api_h.h>
35 /* define generated endian-swappers */
37 #include <lb/lb_all_api_h.h>
40 /* instantiate all the print functions we know about */
41 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
43 #include <lb/lb_all_api_h.h>
46 /* Get the API version number */
47 #define vl_api_version(n,v) static u32 api_version=(v);
48 #include <lb/lb_all_api_h.h>
51 #define REPLY_MSG_ID_BASE lbm->msg_id_base
52 #include <vlibapi/api_helper_macros.h>
54 /* List of message types that this plugin understands */
55 #define foreach_lb_plugin_api_msg \
57 _(LB_ADD_DEL_VIP, lb_add_del_vip) \
58 _(LB_ADD_DEL_AS, lb_add_del_as) \
59 _(LB_FLUSH_VIP, lb_flush_vip) \
62 /* Macro to finish up custom dump fns */
65 vl_print (handle, (char *)s); \
70 vl_api_lb_conf_t_handler
71 (vl_api_lb_conf_t * mp)
73 lb_main_t *lbm = &lb_main;
74 vl_api_lb_conf_reply_t * rmp;
77 if (mp->sticky_buckets_per_core == ~0) {
78 mp->sticky_buckets_per_core = lbm->per_cpu_sticky_buckets;
80 if (mp->flow_timeout == ~0) {
81 mp->flow_timeout = lbm->flow_timeout;
84 rv = lb_conf((ip4_address_t *)&mp->ip4_src_address,
85 (ip6_address_t *)&mp->ip6_src_address,
86 mp->sticky_buckets_per_core,
89 REPLY_MACRO (VL_API_LB_CONF_REPLY);
92 static void *vl_api_lb_conf_t_print
93 (vl_api_lb_conf_t *mp, void * handle)
96 s = format (0, "SCRIPT: lb_conf ");
97 s = format (s, "%U ", format_ip4_address, (ip4_address_t *)&mp->ip4_src_address);
98 s = format (s, "%U ", format_ip6_address, (ip6_address_t *)&mp->ip6_src_address);
99 s = format (s, "%u ", mp->sticky_buckets_per_core);
100 s = format (s, "%u ", mp->flow_timeout);
106 vl_api_lb_add_del_vip_t_handler
107 (vl_api_lb_add_del_vip_t * mp)
109 lb_main_t *lbm = &lb_main;
110 vl_api_lb_conf_reply_t * rmp;
112 lb_vip_add_args_t args;
114 /* if port == 0, it means all-port VIP */
120 memcpy (&(args.prefix.ip6), mp->ip_prefix, sizeof(args.prefix.ip6));
124 if (!(rv = lb_vip_find_index(&(args.prefix), mp->prefix_length,
125 mp->protocol, ntohs(mp->port), &vip_index)))
126 rv = lb_vip_del(vip_index);
129 lb_vip_type_t type = 0;
131 if (ip46_prefix_is_ip4(&(args.prefix), mp->prefix_length)) {
132 if (mp->encap == LB_ENCAP_TYPE_GRE4)
133 type = LB_VIP_TYPE_IP4_GRE4;
134 else if (mp->encap == LB_ENCAP_TYPE_GRE6)
135 type = LB_VIP_TYPE_IP4_GRE6;
136 else if (mp->encap == LB_ENCAP_TYPE_L3DSR)
137 type = LB_VIP_TYPE_IP4_L3DSR;
138 else if (mp->encap == LB_ENCAP_TYPE_NAT4)
139 type = LB_VIP_TYPE_IP4_NAT4;
141 if (mp->encap == LB_ENCAP_TYPE_GRE4)
142 type = LB_VIP_TYPE_IP6_GRE4;
143 else if (mp->encap == LB_ENCAP_TYPE_GRE6)
144 type = LB_VIP_TYPE_IP6_GRE6;
145 else if (mp->encap == LB_ENCAP_TYPE_NAT6)
146 type = LB_VIP_TYPE_IP6_NAT6;
149 args.plen = mp->prefix_length;
150 args.protocol = mp->protocol;
151 args.port = ntohs(mp->port);
153 args.new_length = ntohl(mp->new_flows_table_length);
155 if (mp->encap == LB_ENCAP_TYPE_L3DSR) {
156 args.encap_args.dscp = (u8)(mp->dscp & 0x3F);
158 else if ((mp->encap == LB_ENCAP_TYPE_NAT4)
159 ||(mp->encap == LB_ENCAP_TYPE_NAT6)) {
160 args.encap_args.srv_type = mp->type;
161 args.encap_args.target_port = ntohs(mp->target_port);
164 rv = lb_vip_add(args, &vip_index);
166 REPLY_MACRO (VL_API_LB_ADD_DEL_VIP_REPLY);
169 static void *vl_api_lb_add_del_vip_t_print
170 (vl_api_lb_add_del_vip_t *mp, void * handle)
173 s = format (0, "SCRIPT: lb_add_del_vip ");
174 s = format (s, "%U ", format_ip46_prefix,
175 (ip46_address_t *)mp->ip_prefix, mp->prefix_length, IP46_TYPE_ANY);
177 s = format (s, "%s ", (mp->encap == LB_ENCAP_TYPE_GRE4)? "gre4"
178 : (mp->encap == LB_ENCAP_TYPE_GRE6)? "gre6"
179 : (mp->encap == LB_ENCAP_TYPE_NAT4)? "nat4"
180 : (mp->encap == LB_ENCAP_TYPE_NAT6)? "nat6"
183 if (mp->encap==LB_ENCAP_TYPE_L3DSR)
185 s = format (s, "dscp %u ", mp->dscp);
188 if ((mp->encap==LB_ENCAP_TYPE_NAT4)
189 || (mp->encap==LB_ENCAP_TYPE_NAT6))
191 s = format (s, "type %u ", mp->type);
192 s = format (s, "port %u ", mp->port);
193 s = format (s, "target_port %u ", mp->target_port);
196 s = format (s, "%u ", mp->new_flows_table_length);
197 s = format (s, "%s ", mp->is_del?"del":"add");
202 vl_api_lb_add_del_as_t_handler
203 (vl_api_lb_add_del_as_t * mp)
205 lb_main_t *lbm = &lb_main;
206 vl_api_lb_conf_reply_t * rmp;
209 ip46_address_t vip_ip_prefix;
211 memcpy(&vip_ip_prefix.ip6, mp->vip_ip_prefix,
212 sizeof(vip_ip_prefix.ip6));
214 ip46_address_t as_address;
216 memcpy(&as_address.ip6, mp->as_address,
217 sizeof(as_address.ip6));
219 if ((rv = lb_vip_find_index(&vip_ip_prefix, mp->vip_prefix_length,
220 mp->protocol, ntohs(mp->port), &vip_index)))
224 rv = lb_vip_del_ass(vip_index, &as_address, 1, mp->is_flush);
226 rv = lb_vip_add_ass(vip_index, &as_address, 1);
229 REPLY_MACRO (VL_API_LB_ADD_DEL_AS_REPLY);
232 static void *vl_api_lb_add_del_as_t_print
233 (vl_api_lb_add_del_as_t *mp, void * handle)
236 s = format (0, "SCRIPT: lb_add_del_as ");
237 s = format (s, "%U ", format_ip46_prefix,
238 (ip46_address_t *)mp->vip_ip_prefix, mp->vip_prefix_length, IP46_TYPE_ANY);
239 s = format (s, "%U ", format_ip46_address,
240 (ip46_address_t *)mp->as_address, IP46_TYPE_ANY);
241 s = format (s, "%s ", mp->is_del?"del":"add");
246 vl_api_lb_flush_vip_t_handler
247 (vl_api_lb_flush_vip_t * mp)
249 lb_main_t *lbm = &lb_main;
251 ip46_address_t vip_prefix;
254 vl_api_lb_flush_vip_reply_t * rmp;
261 memcpy (&(vip_prefix.ip6), mp->ip_prefix, sizeof(vip_prefix.ip6));
263 vip_plen = mp->prefix_length;
265 rv = lb_vip_find_index(&vip_prefix, vip_plen, mp->protocol,
266 ntohs(mp->port), &vip_index);
268 rv = lb_flush_vip_as(vip_index, ~0);
270 REPLY_MACRO (VL_API_LB_FLUSH_VIP_REPLY);
273 static void *vl_api_lb_flush_vip_t_print
274 (vl_api_lb_flush_vip_t *mp, void * handle)
277 s = format (0, "SCRIPT: lb_add_del_vip ");
278 s = format (s, "%U ", format_ip46_prefix,
279 (ip46_address_t *)mp->ip_prefix, mp->prefix_length, IP46_TYPE_ANY);
281 s = format (s, "protocol %u ", mp->protocol);
282 s = format (s, "port %u ", mp->port);
287 /* Set up the API message handling tables */
288 static clib_error_t *
289 lb_plugin_api_hookup (vlib_main_t *vm)
291 lb_main_t *lbm = &lb_main;
293 vl_msg_api_set_handlers((VL_API_##N + lbm->msg_id_base), \
295 vl_api_##n##_t_handler, \
297 vl_api_##n##_t_endian, \
298 vl_api_##n##_t_print, \
299 sizeof(vl_api_##n##_t), 1);
300 foreach_lb_plugin_api_msg;
306 #define vl_msg_name_crc_list
307 #include <lb/lb_all_api_h.h>
308 #undef vl_msg_name_crc_list
311 setup_message_id_table (lb_main_t * lmp, api_main_t * am)
313 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + lmp->msg_id_base);
314 foreach_vl_msg_name_crc_lb ;
318 static clib_error_t * lb_api_init (vlib_main_t * vm)
320 lb_main_t * lbm = &lb_main;
321 clib_error_t * error = 0;
325 lbm->vnet_main = vnet_get_main();
327 name = format (0, "lb_%08x%c", api_version, 0);
329 /* Ask for a correctly-sized block of API message decode slots */
330 lbm->msg_id_base = vl_msg_api_get_msg_ids
331 ((char *) name, VL_MSG_FIRST_AVAILABLE);
333 error = lb_plugin_api_hookup (vm);
335 /* Add our API messages to the global name_crc hash table */
336 setup_message_id_table (lbm, &api_main);
343 VLIB_INIT_FUNCTION (lb_api_init);