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 <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>
28 /* define message IDs */
29 #include <lb/lb_msg_enum.h>
31 /* define message structures */
33 #include <lb/lb_all_api_h.h>
36 /* define generated endian-swappers */
38 #include <lb/lb_all_api_h.h>
41 /* instantiate all the print functions we know about */
42 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
44 #include <lb/lb_all_api_h.h>
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>
52 #define REPLY_MSG_ID_BASE lbm->msg_id_base
53 #include <vlibapi/api_helper_macros.h>
55 /* List of message types that this plugin understands */
56 #define foreach_lb_plugin_api_msg \
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) \
65 /* Macro to finish up custom dump fns */
68 vl_print (handle, (char *)s); \
73 vl_api_lb_conf_t_handler
74 (vl_api_lb_conf_t * mp)
76 lb_main_t *lbm = &lb_main;
77 vl_api_lb_conf_reply_t * rmp;
80 if (mp->sticky_buckets_per_core == ~0) {
81 mp->sticky_buckets_per_core = lbm->per_cpu_sticky_buckets;
83 if (mp->flow_timeout == ~0) {
84 mp->flow_timeout = lbm->flow_timeout;
87 rv = lb_conf((ip4_address_t *)&mp->ip4_src_address,
88 (ip6_address_t *)&mp->ip6_src_address,
89 mp->sticky_buckets_per_core,
92 REPLY_MACRO (VL_API_LB_CONF_REPLY);
95 static void *vl_api_lb_conf_t_print
96 (vl_api_lb_conf_t *mp, void * handle)
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);
109 vl_api_lb_add_del_vip_t_handler
110 (vl_api_lb_add_del_vip_t * mp)
112 lb_main_t *lbm = &lb_main;
113 vl_api_lb_conf_reply_t * rmp;
115 lb_vip_add_args_t args;
117 /* if port == 0, it means all-port VIP */
123 memcpy (&(args.prefix), &mp->pfx, sizeof(args.prefix));
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);
132 lb_vip_type_t type = 0;
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;
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;
152 args.plen = mp->pfx.len;
153 args.protocol = mp->protocol;
154 args.port = ntohs(mp->port);
156 args.new_length = ntohl(mp->new_flows_table_length);
158 if (mp->encap == LB_API_ENCAP_TYPE_L3DSR) {
159 args.encap_args.dscp = (u8)(mp->dscp & 0x3F);
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);
167 rv = lb_vip_add(args, &vip_index);
169 REPLY_MACRO (VL_API_LB_ADD_DEL_VIP_REPLY);
172 static void *vl_api_lb_add_del_vip_t_print
173 (vl_api_lb_add_del_vip_t *mp, void * handle)
176 s = format (0, "SCRIPT: lb_add_del_vip ");
177 s = format (s, "%U", format_vl_api_prefix,
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"
186 if (mp->encap==LB_API_ENCAP_TYPE_L3DSR)
188 s = format (s, "dscp %u ", mp->dscp);
191 if ((mp->encap==LB_API_ENCAP_TYPE_NAT4)
192 || (mp->encap==LB_API_ENCAP_TYPE_NAT6))
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);
199 s = format (s, "%u ", mp->new_flows_table_length);
200 s = format (s, "%s ", mp->is_del?"del":"add");
205 vl_api_lb_add_del_as_t_handler
206 (vl_api_lb_add_del_as_t * mp)
208 lb_main_t *lbm = &lb_main;
209 vl_api_lb_conf_reply_t * rmp;
212 ip46_address_t vip_ip_prefix;
214 memcpy(&vip_ip_prefix, &mp->pfx,
215 sizeof(vip_ip_prefix));
217 ip46_address_t as_address;
219 memcpy(&as_address, &mp->as_address,
222 if ((rv = lb_vip_find_index(&vip_ip_prefix, mp->pfx.len,
223 mp->protocol, ntohs(mp->port), &vip_index)))
227 rv = lb_vip_del_ass(vip_index, &as_address, 1, mp->is_flush);
229 rv = lb_vip_add_ass(vip_index, &as_address, 1);
232 REPLY_MACRO (VL_API_LB_ADD_DEL_AS_REPLY);
235 static void *vl_api_lb_add_del_as_t_print
236 (vl_api_lb_add_del_as_t *mp, void * handle)
239 ip46_address_t address;
240 s = format (0, "SCRIPT: lb_add_del_as ");
241 s = format (s, "%U ", format_vl_api_prefix,
243 s = format(s, "%u ", mp->protocol);
244 if (ip_address_decode (&mp->as_address, &address) == IP46_TYPE_IP6)
245 s = format (s, "%U ", format_ip6_address,
246 (ip6_address_t *) & address.ip6);
248 s = format (s, "%U ", format_ip4_address,
249 (ip6_address_t *) & address.ip4);
250 s = format (s, "%s ", mp->is_del?"del":"add");
255 vl_api_lb_vip_dump_t_handler
256 (vl_api_lb_vip_dump_t * mp)
259 vl_api_registration_t *reg;
260 reg = vl_api_client_index_to_registration (mp->client_index);
264 lb_main_t *lbm = &lb_main;
265 vl_api_lb_vip_details_t * rmp;
269 /* construct vip list */
270 pool_foreach(vip, lbm->vips, {
272 if (vip != lbm->vips) {
273 msg_size = sizeof (*rmp);
274 rmp = vl_msg_api_alloc (msg_size);
275 memset (rmp, 0, msg_size);
277 htons (VL_API_LB_VIP_DETAILS + lbm->msg_id_base);
278 rmp->context = mp->context;
280 ip_address_encode(&vip->prefix, IP46_TYPE_ANY, &rmp->vip.pfx.address);
281 rmp->vip.pfx.len = vip->plen;
282 rmp->vip.protocol = htonl (vip->protocol);
283 rmp->vip.port = htons(vip->port);
284 rmp->encap = htonl(vip->type);
285 rmp->dscp = vip->encap_args.dscp;
286 rmp->srv_type = vip->encap_args.srv_type;
287 rmp->target_port = htons(vip->encap_args.target_port);
288 rmp->flow_table_length = htonl(vip->new_flow_table_mask + 1);
290 vl_api_send_msg (reg, (u8 *) rmp);
297 static void send_lb_as_details
298 (vl_api_registration_t * reg, u32 context, lb_vip_t * vip)
300 vl_api_lb_as_details_t *rmp;
301 lb_main_t *lbm = &lb_main;
306 /* construct as list under this vip */
309 pool_foreach(as_index, vip->as_indexes, {
310 /* Hide dummy As for specific VIP */
311 if (*as_index != 0) {
312 as = &lbm->ass[*as_index];
313 msg_size = sizeof (*rmp);
314 rmp = vl_msg_api_alloc (msg_size);
315 memset (rmp, 0, msg_size);
317 htons (VL_API_LB_AS_DETAILS + lbm->msg_id_base);
318 rmp->context = context;
319 ip_address_encode(&vip->prefix, IP46_TYPE_ANY, (vl_api_address_t *)&rmp->vip.pfx.address);
320 rmp->vip.pfx.len = vip->plen;
321 rmp->vip.protocol = htonl (vip->protocol);
322 rmp->vip.port = htons(vip->port);
323 ip_address_encode(&as->address, IP46_TYPE_ANY, &rmp->app_srv);
324 rmp->flags = as->flags;
325 rmp->in_use_since = htonl(as->last_used);
327 vl_api_send_msg (reg, (u8 *) rmp);
336 vl_api_lb_as_dump_t_handler
337 (vl_api_lb_as_dump_t * mp)
339 lb_main_t *lbm = &lb_main;
342 ip46_address_t prefix;
344 vl_api_registration_t *reg;
345 reg = vl_api_client_index_to_registration (mp->client_index);
349 clib_memcpy(&prefix.ip6, mp->pfx.address.un.ip6, sizeof(mp->pfx.address.un.ip6));
351 dump_all = (prefix.ip6.as_u64[0] == 0) && (prefix.ip6.as_u64[1] == 0);
354 pool_foreach(vip, lbm->vips,
357 || ((prefix.as_u64[0] == vip->prefix.as_u64[0])
358 && (prefix.as_u64[1] == vip->prefix.as_u64[1])
359 && (mp->protocol == vip->protocol)
360 && (mp->port == vip->port)) )
362 send_lb_as_details(reg, mp->context, vip);
369 vl_api_lb_flush_vip_t_handler
370 (vl_api_lb_flush_vip_t * mp)
372 lb_main_t *lbm = &lb_main;
374 ip46_address_t vip_prefix;
377 vl_api_lb_flush_vip_reply_t * rmp;
384 memcpy (&(vip_prefix.ip6), mp->pfx.address.un.ip6, sizeof(vip_prefix.ip6));
386 vip_plen = mp->pfx.len;
388 rv = lb_vip_find_index(&vip_prefix, vip_plen, mp->protocol,
389 ntohs(mp->port), &vip_index);
391 rv = lb_flush_vip_as(vip_index, ~0);
393 REPLY_MACRO (VL_API_LB_FLUSH_VIP_REPLY);
396 static void *vl_api_lb_flush_vip_t_print
397 (vl_api_lb_flush_vip_t *mp, void * handle)
400 s = format (0, "SCRIPT: lb_add_del_vip ");
401 s = format (s, "%U/%d", format_vl_api_address,
402 &mp->pfx.address, mp->pfx.len);
403 s = format (s, "protocol %u ", mp->protocol);
404 s = format (s, "port %u ", mp->port);
409 /* Set up the API message handling tables */
410 static clib_error_t *
411 lb_plugin_api_hookup (vlib_main_t *vm)
413 lb_main_t *lbm = &lb_main;
415 vl_msg_api_set_handlers((VL_API_##N + lbm->msg_id_base), \
417 vl_api_##n##_t_handler, \
419 vl_api_##n##_t_endian, \
420 vl_api_##n##_t_print, \
421 sizeof(vl_api_##n##_t), 1);
422 foreach_lb_plugin_api_msg;
428 #define vl_msg_name_crc_list
429 #include <lb/lb_all_api_h.h>
430 #undef vl_msg_name_crc_list
433 setup_message_id_table (lb_main_t * lmp, api_main_t * am)
435 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + lmp->msg_id_base);
436 foreach_vl_msg_name_crc_lb ;
440 static clib_error_t * lb_api_init (vlib_main_t * vm)
442 lb_main_t * lbm = &lb_main;
443 clib_error_t * error = 0;
447 lbm->vnet_main = vnet_get_main();
449 name = format (0, "lb_%08x%c", api_version, 0);
451 /* Ask for a correctly-sized block of API message decode slots */
452 lbm->msg_id_base = vl_msg_api_get_msg_ids
453 ((char *) name, VL_MSG_FIRST_AVAILABLE);
455 error = lb_plugin_api_hookup (vm);
457 /* Add our API messages to the global name_crc hash table */
458 setup_message_id_table (lbm, &api_main);
465 VLIB_INIT_FUNCTION (lb_api_init);