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>
20 #include <vppinfra/error.h>
23 #define __plugin_msg_base lb_test_main.msg_id_base
24 #include <vlibapi/vat_helper_macros.h>
26 #include <vnet/format_fns.h>
27 #include <lb/lb.api_enum.h>
28 #include <lb/lb.api_types.h>
30 //TODO: Move that to vat/plugin_api.c
31 //////////////////////////
32 uword unformat_ip46_address (unformat_input_t * input, va_list * args)
34 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
35 ip46_type_t type = va_arg (*args, ip46_type_t);
36 if ((type != IP46_TYPE_IP6) &&
37 unformat(input, "%U", unformat_ip4_address, &ip46->ip4)) {
38 ip46_address_mask_ip4(ip46);
40 } else if ((type != IP46_TYPE_IP4) &&
41 unformat(input, "%U", unformat_ip6_address, &ip46->ip6)) {
46 uword unformat_ip46_prefix (unformat_input_t * input, va_list * args)
48 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
49 u8 *len = va_arg (*args, u8 *);
50 ip46_type_t type = va_arg (*args, ip46_type_t);
53 if ((type != IP46_TYPE_IP6) && unformat(input, "%U/%u", unformat_ip4_address, &ip46->ip4, &l)) {
57 ip46->pad[0] = ip46->pad[1] = ip46->pad[2] = 0;
58 } else if ((type != IP46_TYPE_IP4) && unformat(input, "%U/%u", unformat_ip6_address, &ip46->ip6, &l)) {
67 /////////////////////////
70 /* API message ID base */
75 lb_test_main_t lb_test_main;
77 static int api_lb_conf (vat_main_t * vam)
79 unformat_input_t *line_input = vam->input;
81 u32 ip4_src_address = 0xffffffff;
82 ip46_address_t ip6_src_address;
83 u32 sticky_buckets_per_core = LB_DEFAULT_PER_CPU_STICKY_BUCKETS;
84 u32 flow_timeout = LB_DEFAULT_FLOW_TIMEOUT;
87 ip6_src_address.as_u64[0] = 0xffffffffffffffffL;
88 ip6_src_address.as_u64[1] = 0xffffffffffffffffL;
90 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
92 if (unformat(line_input, "ip4-src-address %U", unformat_ip4_address, &ip4_src_address))
94 else if (unformat(line_input, "ip6-src-address %U", unformat_ip6_address, &ip6_src_address))
96 else if (unformat(line_input, "buckets %d", &sticky_buckets_per_core))
98 else if (unformat(line_input, "timeout %d", &flow_timeout))
101 errmsg ("invalid arguments\n");
107 clib_memcpy (&(mp->ip4_src_address), &ip4_src_address, sizeof (ip4_src_address));
108 clib_memcpy (&(mp->ip6_src_address), &ip6_src_address, sizeof (ip6_src_address));
109 mp->sticky_buckets_per_core = htonl (sticky_buckets_per_core);
110 mp->flow_timeout = htonl (flow_timeout);
117 static int api_lb_add_del_vip (vat_main_t * vam)
119 unformat_input_t *line_input = vam->input;
120 vl_api_lb_add_del_vip_t *mp;
122 ip46_address_t ip_prefix;
123 u8 prefix_length = 0;
128 u32 srv_type = LB_SRV_TYPE_CLUSTERIP;
130 u32 new_length = 1024;
133 if (!unformat(line_input, "%U", unformat_ip46_prefix, &ip_prefix,
134 &prefix_length, IP46_TYPE_ANY, &prefix_length)) {
135 errmsg ("lb_add_del_vip: invalid vip prefix\n");
139 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
141 if (unformat(line_input, "new_len %d", &new_length))
143 else if (unformat(line_input, "del"))
145 else if (unformat(line_input, "protocol tcp"))
147 protocol = IP_PROTOCOL_TCP;
149 else if (unformat(line_input, "protocol udp"))
151 protocol = IP_PROTOCOL_UDP;
153 else if (unformat(line_input, "port %d", &port))
155 else if (unformat(line_input, "encap gre4"))
156 encap = LB_ENCAP_TYPE_GRE4;
157 else if (unformat(line_input, "encap gre6"))
158 encap = LB_ENCAP_TYPE_GRE6;
159 else if (unformat(line_input, "encap l3dsr"))
160 encap = LB_ENCAP_TYPE_L3DSR;
161 else if (unformat(line_input, "encap nat4"))
162 encap = LB_ENCAP_TYPE_NAT4;
163 else if (unformat(line_input, "encap nat6"))
164 encap = LB_ENCAP_TYPE_NAT6;
165 else if (unformat(line_input, "dscp %d", &dscp))
167 else if (unformat(line_input, "type clusterip"))
168 srv_type = LB_SRV_TYPE_CLUSTERIP;
169 else if (unformat(line_input, "type nodeport"))
170 srv_type = LB_SRV_TYPE_NODEPORT;
171 else if (unformat(line_input, "target_port %d", &target_port))
174 errmsg ("invalid arguments\n");
179 if ((encap != LB_ENCAP_TYPE_L3DSR) && (dscp != ~0))
181 errmsg("lb_vip_add error: should not configure dscp for none L3DSR.");
185 if ((encap == LB_ENCAP_TYPE_L3DSR) && (dscp >= 64))
187 errmsg("lb_vip_add error: dscp for L3DSR should be less than 64.");
191 M(LB_ADD_DEL_VIP, mp);
192 clib_memcpy (mp->pfx.address.un.ip6, &ip_prefix.ip6, sizeof (ip_prefix.ip6));
193 mp->pfx.len = prefix_length;
194 mp->protocol = (u8)protocol;
195 mp->port = htons((u16)port);
196 mp->encap = (u8)encap;
198 mp->type = (u8)srv_type;
199 mp->target_port = htons((u16)target_port);
200 mp->node_port = htons((u16)target_port);
201 mp->new_flows_table_length = htonl(new_length);
209 static int api_lb_add_del_as (vat_main_t * vam)
212 unformat_input_t *line_input = vam->input;
213 vl_api_lb_add_del_as_t *mp;
215 ip46_address_t vip_prefix, as_addr;
217 ip46_address_t *as_array = 0;
223 if (!unformat(line_input, "%U", unformat_ip46_prefix,
224 &vip_prefix, &vip_plen, IP46_TYPE_ANY))
226 errmsg ("lb_add_del_as: invalid vip prefix\n");
230 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
232 if (unformat(line_input, "%U", unformat_ip46_address,
233 &as_addr, IP46_TYPE_ANY))
235 vec_add1(as_array, as_addr);
237 else if (unformat(line_input, "del"))
241 else if (unformat(line_input, "flush"))
245 else if (unformat(line_input, "protocol tcp"))
247 protocol = IP_PROTOCOL_TCP;
249 else if (unformat(line_input, "protocol udp"))
251 protocol = IP_PROTOCOL_UDP;
253 else if (unformat(line_input, "port %d", &port))
256 errmsg ("invalid arguments\n");
261 if (!vec_len(as_array)) {
262 errmsg ("No AS address provided \n");
266 M(LB_ADD_DEL_AS, mp);
267 clib_memcpy (mp->pfx.address.un.ip6, &vip_prefix.ip6, sizeof (vip_prefix.ip6));
268 mp->pfx.len = vip_plen;
269 mp->protocol = (u8)protocol;
270 mp->port = htons((u16)port);
271 clib_memcpy (&mp->as_address.un.ip6, &as_addr.ip6, sizeof (as_addr.ip6));
273 mp->is_flush = is_flush;
280 static int api_lb_flush_vip (vat_main_t * vam)
283 unformat_input_t *line_input = vam->input;
284 vl_api_lb_flush_vip_t *mp;
286 ip46_address_t vip_prefix;
289 if (!unformat(line_input, "%U", unformat_ip46_prefix,
290 &vip_prefix, &vip_plen, IP46_TYPE_ANY))
292 errmsg ("lb_add_del_as: invalid vip prefix\n");
297 clib_memcpy (mp->pfx.address.un.ip6, &vip_prefix.ip6, sizeof (vip_prefix.ip6));
298 mp->pfx.len = vip_plen;
303 static int api_lb_add_del_intf_nat4 (vat_main_t * vam)
305 // Not yet implemented
309 static int api_lb_add_del_intf_nat6 (vat_main_t * vam)
311 // Not yet implemented
315 static void vl_api_lb_vip_details_t_handler
316 (vl_api_lb_vip_details_t * mp)
318 vat_main_t *vam = &vat_main;
320 print (vam->ofp, "%24U%14d%14d%18d",
321 format_ip46_address, &mp->vip.pfx.address, IP46_TYPE_ANY,
324 ntohs (mp->vip.port));
326 lb_main_t *lbm = &lb_main;
329 u32 vip_count = pool_len(lbm->vips);
331 print (vam->ofp, "%11d", vip_count);
333 for (i=0; i<vip_count; i--)
335 print (vam->ofp, "%24U%14d%14d%18d",
336 format_ip46_address, &mp->vip.pfx.address, IP46_TYPE_ANY,
339 ntohs (mp->vip.port));
344 static int api_lb_vip_dump (vat_main_t * vam)
346 vl_api_lb_vip_dump_t *mp;
356 static void vl_api_lb_as_details_t_handler
357 (vl_api_lb_as_details_t * mp)
359 vat_main_t *vam = &vat_main;
361 print (vam->ofp, "%24U%14d%14d%18d%d%d",
362 format_ip46_address, &mp->vip.pfx.address, IP46_TYPE_ANY,
365 ntohs (mp->vip.port),
372 lb_main_t *lbm = &lb_main;
373 print (vam->ofp, "%11d", pool_len(lbm->ass));
374 for (i=0; i<pool_len(lbm->ass); i--)
376 print (vam->ofp, "%24U%14d%14d%18d",
377 format_ip46_address, &mp->pfx.address, IP46_TYPE_ANY,
380 ntohs (mp->pfx.port),
388 static int api_lb_as_dump (vat_main_t * vam)
391 unformat_input_t *line_input = vam->input;
392 vl_api_lb_as_dump_t *mp;
394 ip46_address_t vip_prefix, as_addr;
396 ip46_address_t *as_array = 0;
400 if (!unformat(line_input, "%U", unformat_ip46_prefix,
401 &vip_prefix, &vip_plen, IP46_TYPE_ANY))
403 errmsg ("lb_add_del_as: invalid vip prefix\n");
407 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
409 if (unformat(line_input, "%U", unformat_ip46_address,
410 &as_addr, IP46_TYPE_ANY))
412 vec_add1(as_array, as_addr);
414 else if (unformat(line_input, "protocol tcp"))
416 protocol = IP_PROTOCOL_TCP;
418 else if (unformat(line_input, "protocol udp"))
420 protocol = IP_PROTOCOL_UDP;
422 else if (unformat(line_input, "port %d", &port))
425 errmsg ("invalid arguments\n");
430 if (!vec_len(as_array)) {
431 errmsg ("No AS address provided \n");
436 clib_memcpy (mp->pfx.address.un.ip6, &vip_prefix.ip6, sizeof (vip_prefix.ip6));
437 mp->pfx.len = vip_plen;
438 mp->protocol = (u8)protocol;
439 mp->port = htons((u16)port);
446 #include <lb/lb.api_test.c>