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);
210 static int api_lb_add_del_as (vat_main_t * vam)
213 unformat_input_t *line_input = vam->input;
214 vl_api_lb_add_del_as_t *mp;
216 ip46_address_t vip_prefix, as_addr;
218 ip46_address_t *as_array = 0;
224 if (!unformat(line_input, "%U", unformat_ip46_prefix,
225 &vip_prefix, &vip_plen, IP46_TYPE_ANY))
227 errmsg ("lb_add_del_as: invalid vip prefix\n");
231 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
233 if (unformat(line_input, "%U", unformat_ip46_address,
234 &as_addr, IP46_TYPE_ANY))
236 vec_add1(as_array, as_addr);
238 else if (unformat(line_input, "del"))
242 else if (unformat(line_input, "flush"))
246 else if (unformat(line_input, "protocol tcp"))
248 protocol = IP_PROTOCOL_TCP;
250 else if (unformat(line_input, "protocol udp"))
252 protocol = IP_PROTOCOL_UDP;
254 else if (unformat(line_input, "port %d", &port))
257 errmsg ("invalid arguments\n");
262 if (!vec_len(as_array)) {
263 errmsg ("No AS address provided \n");
267 M(LB_ADD_DEL_AS, mp);
268 ip_address_encode(&vip_prefix, IP46_TYPE_ANY, &mp->pfx.address);
269 mp->pfx.len = vip_plen;
270 mp->protocol = (u8)protocol;
271 mp->port = htons((u16)port);
272 ip_address_encode(&as_addr, IP46_TYPE_ANY, &mp->as_address);
274 mp->is_flush = is_flush;
281 static int api_lb_flush_vip (vat_main_t * vam)
284 unformat_input_t *line_input = vam->input;
285 vl_api_lb_flush_vip_t *mp;
287 ip46_address_t vip_prefix;
290 if (!unformat(line_input, "%U", unformat_ip46_prefix,
291 &vip_prefix, &vip_plen, IP46_TYPE_ANY))
293 errmsg ("lb_add_del_as: invalid vip prefix\n");
298 clib_memcpy (mp->pfx.address.un.ip6, &vip_prefix.ip6, sizeof (vip_prefix.ip6));
299 mp->pfx.len = vip_plen;
304 static int api_lb_add_del_intf_nat4 (vat_main_t * vam)
306 // Not yet implemented
310 static int api_lb_add_del_intf_nat6 (vat_main_t * vam)
312 // Not yet implemented
316 static void vl_api_lb_vip_details_t_handler
317 (vl_api_lb_vip_details_t * mp)
319 vat_main_t *vam = &vat_main;
321 print (vam->ofp, "%24U%14d%14d%18d",
322 format_ip46_address, &mp->vip.pfx.address, IP46_TYPE_ANY,
325 ntohs (mp->vip.port));
327 lb_main_t *lbm = &lb_main;
330 u32 vip_count = pool_len(lbm->vips);
332 print (vam->ofp, "%11d", vip_count);
334 for (i=0; i<vip_count; i--)
336 print (vam->ofp, "%24U%14d%14d%18d",
337 format_ip46_address, &mp->vip.pfx.address, IP46_TYPE_ANY,
340 ntohs (mp->vip.port));
345 static int api_lb_vip_dump (vat_main_t * vam)
347 vl_api_lb_vip_dump_t *mp;
357 static void vl_api_lb_as_details_t_handler
358 (vl_api_lb_as_details_t * mp)
360 vat_main_t *vam = &vat_main;
362 print (vam->ofp, "%24U%14d%14d%18d%d%d",
363 format_ip46_address, &mp->vip.pfx.address, IP46_TYPE_ANY,
366 ntohs (mp->vip.port),
373 lb_main_t *lbm = &lb_main;
374 print (vam->ofp, "%11d", pool_len(lbm->ass));
375 for (i=0; i<pool_len(lbm->ass); i--)
377 print (vam->ofp, "%24U%14d%14d%18d",
378 format_ip46_address, &mp->pfx.address, IP46_TYPE_ANY,
381 ntohs (mp->pfx.port),
389 static int api_lb_as_dump (vat_main_t * vam)
392 unformat_input_t *line_input = vam->input;
393 vl_api_lb_as_dump_t *mp;
395 ip46_address_t vip_prefix, as_addr;
397 ip46_address_t *as_array = 0;
401 if (!unformat(line_input, "%U", unformat_ip46_prefix,
402 &vip_prefix, &vip_plen, IP46_TYPE_ANY))
404 errmsg ("lb_add_del_as: invalid vip prefix\n");
408 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
410 if (unformat(line_input, "%U", unformat_ip46_address,
411 &as_addr, IP46_TYPE_ANY))
413 vec_add1(as_array, as_addr);
415 else if (unformat(line_input, "protocol tcp"))
417 protocol = IP_PROTOCOL_TCP;
419 else if (unformat(line_input, "protocol udp"))
421 protocol = IP_PROTOCOL_UDP;
423 else if (unformat(line_input, "port %d", &port))
426 errmsg ("invalid arguments\n");
431 if (!vec_len(as_array)) {
432 errmsg ("No AS address provided \n");
437 clib_memcpy (mp->pfx.address.un.ip6, &vip_prefix.ip6, sizeof (vip_prefix.ip6));
438 mp->pfx.len = vip_plen;
439 mp->protocol = (u8)protocol;
440 mp->port = htons((u16)port);
447 #include <lb/lb.api_test.c>