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.
17 #include <vlibapi/api.h>
18 #include <vlibmemory/api.h>
19 #include <vnet/ip/ip_types_api.h>
21 #include <vppinfra/error.h>
24 #define __plugin_msg_base lb_test_main.msg_id_base
25 #include <vlibapi/vat_helper_macros.h>
27 #include <vnet/format_fns.h>
28 #include <lb/lb.api_enum.h>
29 #include <lb/lb.api_types.h>
31 //TODO: Move that to vat/plugin_api.c
32 //////////////////////////
33 uword unformat_ip46_address (unformat_input_t * input, va_list * args)
35 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
36 ip46_type_t type = va_arg (*args, ip46_type_t);
37 if ((type != IP46_TYPE_IP6) &&
38 unformat(input, "%U", unformat_ip4_address, &ip46->ip4)) {
39 ip46_address_mask_ip4(ip46);
41 } else if ((type != IP46_TYPE_IP4) &&
42 unformat(input, "%U", unformat_ip6_address, &ip46->ip6)) {
47 uword unformat_ip46_prefix (unformat_input_t * input, va_list * args)
49 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
50 u8 *len = va_arg (*args, u8 *);
51 ip46_type_t type = va_arg (*args, ip46_type_t);
54 if ((type != IP46_TYPE_IP6) && unformat(input, "%U/%u", unformat_ip4_address, &ip46->ip4, &l)) {
58 ip46->pad[0] = ip46->pad[1] = ip46->pad[2] = 0;
59 } else if ((type != IP46_TYPE_IP4) && unformat(input, "%U/%u", unformat_ip6_address, &ip46->ip6, &l)) {
68 /////////////////////////
71 /* API message ID base */
76 lb_test_main_t lb_test_main;
78 static int api_lb_conf (vat_main_t * vam)
80 unformat_input_t *line_input = vam->input;
82 u32 ip4_src_address = 0xffffffff;
83 ip46_address_t ip6_src_address;
84 u32 sticky_buckets_per_core = LB_DEFAULT_PER_CPU_STICKY_BUCKETS;
85 u32 flow_timeout = LB_DEFAULT_FLOW_TIMEOUT;
88 ip6_src_address.as_u64[0] = 0xffffffffffffffffL;
89 ip6_src_address.as_u64[1] = 0xffffffffffffffffL;
91 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
93 if (unformat(line_input, "ip4-src-address %U", unformat_ip4_address, &ip4_src_address))
95 else if (unformat(line_input, "ip6-src-address %U", unformat_ip6_address, &ip6_src_address))
97 else if (unformat(line_input, "buckets %d", &sticky_buckets_per_core))
99 else if (unformat(line_input, "timeout %d", &flow_timeout))
102 errmsg ("invalid arguments\n");
108 clib_memcpy (&(mp->ip4_src_address), &ip4_src_address, sizeof (ip4_src_address));
109 clib_memcpy (&(mp->ip6_src_address), &ip6_src_address, sizeof (ip6_src_address));
110 mp->sticky_buckets_per_core = htonl (sticky_buckets_per_core);
111 mp->flow_timeout = htonl (flow_timeout);
118 static int api_lb_add_del_vip (vat_main_t * vam)
120 unformat_input_t *line_input = vam->input;
121 vl_api_lb_add_del_vip_t *mp;
123 ip46_address_t ip_prefix;
124 u8 prefix_length = 0;
129 u32 srv_type = LB_SRV_TYPE_CLUSTERIP;
131 u32 new_length = 1024;
134 if (!unformat(line_input, "%U", unformat_ip46_prefix, &ip_prefix,
135 &prefix_length, IP46_TYPE_ANY, &prefix_length)) {
136 errmsg ("lb_add_del_vip: invalid vip prefix\n");
140 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
142 if (unformat(line_input, "new_len %d", &new_length))
144 else if (unformat(line_input, "del"))
146 else if (unformat(line_input, "protocol tcp"))
148 protocol = IP_PROTOCOL_TCP;
150 else if (unformat(line_input, "protocol udp"))
152 protocol = IP_PROTOCOL_UDP;
154 else if (unformat(line_input, "port %d", &port))
156 else if (unformat(line_input, "encap gre4"))
157 encap = LB_ENCAP_TYPE_GRE4;
158 else if (unformat(line_input, "encap gre6"))
159 encap = LB_ENCAP_TYPE_GRE6;
160 else if (unformat(line_input, "encap l3dsr"))
161 encap = LB_ENCAP_TYPE_L3DSR;
162 else if (unformat(line_input, "encap nat4"))
163 encap = LB_ENCAP_TYPE_NAT4;
164 else if (unformat(line_input, "encap nat6"))
165 encap = LB_ENCAP_TYPE_NAT6;
166 else if (unformat(line_input, "dscp %d", &dscp))
168 else if (unformat(line_input, "type clusterip"))
169 srv_type = LB_SRV_TYPE_CLUSTERIP;
170 else if (unformat(line_input, "type nodeport"))
171 srv_type = LB_SRV_TYPE_NODEPORT;
172 else if (unformat(line_input, "target_port %d", &target_port))
175 errmsg ("invalid arguments\n");
180 if ((encap != LB_ENCAP_TYPE_L3DSR) && (dscp != ~0))
182 errmsg("lb_vip_add error: should not configure dscp for none L3DSR.");
186 if ((encap == LB_ENCAP_TYPE_L3DSR) && (dscp >= 64))
188 errmsg("lb_vip_add error: dscp for L3DSR should be less than 64.");
192 M(LB_ADD_DEL_VIP, mp);
193 ip_address_encode(&ip_prefix, IP46_TYPE_ANY, &mp->pfx.address);
194 mp->pfx.len = prefix_length;
195 mp->protocol = (u8)protocol;
196 mp->port = htons((u16)port);
197 mp->encap = (u8)encap;
199 mp->type = (u8)srv_type;
200 mp->target_port = htons((u16)target_port);
201 mp->node_port = htons((u16)target_port);
202 mp->new_flows_table_length = htonl(new_length);
211 api_lb_add_del_vip_v2 (vat_main_t *vam)
213 unformat_input_t *line_input = vam->input;
214 vl_api_lb_add_del_vip_v2_t *mp;
216 ip46_address_t ip_prefix;
217 u8 prefix_length = 0;
222 u32 srv_type = LB_SRV_TYPE_CLUSTERIP;
224 u32 new_length = 1024;
225 u8 src_ip_sticky = 0;
228 if (!unformat (line_input, "%U", unformat_ip46_prefix, &ip_prefix,
229 &prefix_length, IP46_TYPE_ANY, &prefix_length))
231 errmsg ("lb_add_del_vip: invalid vip prefix\n");
235 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
237 if (unformat (line_input, "new_len %d", &new_length))
239 else if (unformat (line_input, "del"))
241 else if (unformat (line_input, "src_ip_sticky"))
243 else if (unformat (line_input, "protocol tcp"))
245 protocol = IP_PROTOCOL_TCP;
247 else if (unformat (line_input, "protocol udp"))
249 protocol = IP_PROTOCOL_UDP;
251 else if (unformat (line_input, "port %d", &port))
253 else if (unformat (line_input, "encap gre4"))
254 encap = LB_ENCAP_TYPE_GRE4;
255 else if (unformat (line_input, "encap gre6"))
256 encap = LB_ENCAP_TYPE_GRE6;
257 else if (unformat (line_input, "encap l3dsr"))
258 encap = LB_ENCAP_TYPE_L3DSR;
259 else if (unformat (line_input, "encap nat4"))
260 encap = LB_ENCAP_TYPE_NAT4;
261 else if (unformat (line_input, "encap nat6"))
262 encap = LB_ENCAP_TYPE_NAT6;
263 else if (unformat (line_input, "dscp %d", &dscp))
265 else if (unformat (line_input, "type clusterip"))
266 srv_type = LB_SRV_TYPE_CLUSTERIP;
267 else if (unformat (line_input, "type nodeport"))
268 srv_type = LB_SRV_TYPE_NODEPORT;
269 else if (unformat (line_input, "target_port %d", &target_port))
273 errmsg ("invalid arguments\n");
278 if ((encap != LB_ENCAP_TYPE_L3DSR) && (dscp != ~0))
280 errmsg ("lb_vip_add error: should not configure dscp for none L3DSR.");
284 if ((encap == LB_ENCAP_TYPE_L3DSR) && (dscp >= 64))
286 errmsg ("lb_vip_add error: dscp for L3DSR should be less than 64.");
290 M (LB_ADD_DEL_VIP, mp);
291 ip_address_encode (&ip_prefix, IP46_TYPE_ANY, &mp->pfx.address);
292 mp->pfx.len = prefix_length;
293 mp->protocol = (u8) protocol;
294 mp->port = htons ((u16) port);
295 mp->encap = (u8) encap;
296 mp->dscp = (u8) dscp;
297 mp->type = (u8) srv_type;
298 mp->target_port = htons ((u16) target_port);
299 mp->node_port = htons ((u16) target_port);
300 mp->new_flows_table_length = htonl (new_length);
302 mp->src_ip_sticky = src_ip_sticky;
309 static int api_lb_add_del_as (vat_main_t * vam)
312 unformat_input_t *line_input = vam->input;
313 vl_api_lb_add_del_as_t *mp;
315 ip46_address_t vip_prefix, as_addr;
317 ip46_address_t *as_array = 0;
323 if (!unformat(line_input, "%U", unformat_ip46_prefix,
324 &vip_prefix, &vip_plen, IP46_TYPE_ANY))
326 errmsg ("lb_add_del_as: invalid vip prefix\n");
330 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
332 if (unformat(line_input, "%U", unformat_ip46_address,
333 &as_addr, IP46_TYPE_ANY))
335 vec_add1(as_array, as_addr);
337 else if (unformat(line_input, "del"))
341 else if (unformat(line_input, "flush"))
345 else if (unformat(line_input, "protocol tcp"))
347 protocol = IP_PROTOCOL_TCP;
349 else if (unformat(line_input, "protocol udp"))
351 protocol = IP_PROTOCOL_UDP;
353 else if (unformat(line_input, "port %d", &port))
356 errmsg ("invalid arguments\n");
361 if (!vec_len(as_array)) {
362 errmsg ("No AS address provided \n");
366 M(LB_ADD_DEL_AS, mp);
367 ip_address_encode(&vip_prefix, IP46_TYPE_ANY, &mp->pfx.address);
368 mp->pfx.len = vip_plen;
369 mp->protocol = (u8)protocol;
370 mp->port = htons((u16)port);
371 ip_address_encode(&as_addr, IP46_TYPE_ANY, &mp->as_address);
373 mp->is_flush = is_flush;
380 static int api_lb_flush_vip (vat_main_t * vam)
383 unformat_input_t *line_input = vam->input;
384 vl_api_lb_flush_vip_t *mp;
386 ip46_address_t vip_prefix;
389 if (!unformat(line_input, "%U", unformat_ip46_prefix,
390 &vip_prefix, &vip_plen, IP46_TYPE_ANY))
392 errmsg ("lb_add_del_as: invalid vip prefix\n");
397 clib_memcpy (mp->pfx.address.un.ip6, &vip_prefix.ip6, sizeof (vip_prefix.ip6));
398 mp->pfx.len = vip_plen;
403 static int api_lb_add_del_intf_nat4 (vat_main_t * vam)
405 // Not yet implemented
409 static int api_lb_add_del_intf_nat6 (vat_main_t * vam)
411 // Not yet implemented
415 static void vl_api_lb_vip_details_t_handler
416 (vl_api_lb_vip_details_t * mp)
418 vat_main_t *vam = &vat_main;
420 print (vam->ofp, "%24U%14d%14d%18d",
421 format_ip46_address, &mp->vip.pfx.address, IP46_TYPE_ANY,
424 ntohs (mp->vip.port));
426 lb_main_t *lbm = &lb_main;
429 u32 vip_count = pool_len(lbm->vips);
431 print (vam->ofp, "%11d", vip_count);
433 for (i=0; i<vip_count; i--)
435 print (vam->ofp, "%24U%14d%14d%18d",
436 format_ip46_address, &mp->vip.pfx.address, IP46_TYPE_ANY,
439 ntohs (mp->vip.port));
444 static int api_lb_vip_dump (vat_main_t * vam)
446 vl_api_lb_vip_dump_t *mp;
456 static void vl_api_lb_as_details_t_handler
457 (vl_api_lb_as_details_t * mp)
459 vat_main_t *vam = &vat_main;
461 print (vam->ofp, "%24U%14d%14d%18d%d%d",
462 format_ip46_address, &mp->vip.pfx.address, IP46_TYPE_ANY,
465 ntohs (mp->vip.port),
472 lb_main_t *lbm = &lb_main;
473 print (vam->ofp, "%11d", pool_len(lbm->ass));
474 for (i=0; i<pool_len(lbm->ass); i--)
476 print (vam->ofp, "%24U%14d%14d%18d",
477 format_ip46_address, &mp->pfx.address, IP46_TYPE_ANY,
480 ntohs (mp->pfx.port),
488 static int api_lb_as_dump (vat_main_t * vam)
491 unformat_input_t *line_input = vam->input;
492 vl_api_lb_as_dump_t *mp;
494 ip46_address_t vip_prefix, as_addr;
496 ip46_address_t *as_array = 0;
500 if (!unformat(line_input, "%U", unformat_ip46_prefix,
501 &vip_prefix, &vip_plen, IP46_TYPE_ANY))
503 errmsg ("lb_add_del_as: invalid vip prefix\n");
507 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
509 if (unformat(line_input, "%U", unformat_ip46_address,
510 &as_addr, IP46_TYPE_ANY))
512 vec_add1(as_array, as_addr);
514 else if (unformat(line_input, "protocol tcp"))
516 protocol = IP_PROTOCOL_TCP;
518 else if (unformat(line_input, "protocol udp"))
520 protocol = IP_PROTOCOL_UDP;
522 else if (unformat(line_input, "port %d", &port))
525 errmsg ("invalid arguments\n");
530 if (!vec_len(as_array)) {
531 errmsg ("No AS address provided \n");
536 clib_memcpy (mp->pfx.address.un.ip6, &vip_prefix.ip6, sizeof (vip_prefix.ip6));
537 mp->pfx.len = vip_plen;
538 mp->protocol = (u8)protocol;
539 mp->port = htons((u16)port);
546 #include <lb/lb.api_test.c>