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.
20 lb_vip_command_fn (vlib_main_t * vm,
21 unformat_input_t * input, vlib_cli_command_t * cmd)
23 unformat_input_t _line_input, *line_input = &_line_input;
24 lb_vip_add_args_t args;
29 u32 srv_type = LB_SRV_TYPE_CLUSTERIP;
33 clib_error_t *error = 0;
35 args.new_length = 1024;
37 if (!unformat_user (input, unformat_line_input, line_input))
40 if (!unformat(line_input, "%U", unformat_ip46_prefix, &(args.prefix),
41 &(args.plen), IP46_TYPE_ANY, &(args.plen))) {
42 error = clib_error_return (0, "invalid vip prefix: '%U'",
43 format_unformat_error, line_input);
47 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
49 if (unformat(line_input, "new_len %d", &(args.new_length)))
51 else if (unformat(line_input, "del"))
53 else if (unformat(line_input, "encap gre4"))
54 encap = LB_ENCAP_TYPE_GRE4;
55 else if (unformat(line_input, "encap gre6"))
56 encap = LB_ENCAP_TYPE_GRE6;
57 else if (unformat(line_input, "encap l3dsr"))
58 encap = LB_ENCAP_TYPE_L3DSR;
59 else if (unformat(line_input, "encap nat4"))
60 encap = LB_ENCAP_TYPE_NAT4;
61 else if (unformat(line_input, "encap nat6"))
62 encap = LB_ENCAP_TYPE_NAT6;
63 else if (unformat(line_input, "dscp %d", &dscp))
65 else if (unformat(line_input, "type clusterip"))
66 srv_type = LB_SRV_TYPE_CLUSTERIP;
67 else if (unformat(line_input, "type nodeport"))
68 srv_type = LB_SRV_TYPE_NODEPORT;
69 else if (unformat(line_input, "port %d", &port))
71 else if (unformat(line_input, "target_port %d", &target_port))
73 else if (unformat(line_input, "node_port %d", &node_port))
76 error = clib_error_return (0, "parse error: '%U'",
77 format_unformat_error, line_input);
82 if ((encap != LB_ENCAP_TYPE_L3DSR) && (dscp != ~0))
84 error = clib_error_return(0, "lb_vip_add error: "
85 "should not configure dscp for none L3DSR.");
89 if ((encap == LB_ENCAP_TYPE_L3DSR) && (dscp >= 64))
91 error = clib_error_return(0, "lb_vip_add error: "
92 "dscp for L3DSR should be less than 64.");
96 if (ip46_prefix_is_ip4(&(args.prefix), (args.plen)))
98 if (encap == LB_ENCAP_TYPE_GRE4)
99 args.type = LB_VIP_TYPE_IP4_GRE4;
100 else if (encap == LB_ENCAP_TYPE_GRE6)
101 args.type = LB_VIP_TYPE_IP4_GRE6;
102 else if (encap == LB_ENCAP_TYPE_L3DSR)
103 args.type = LB_VIP_TYPE_IP4_L3DSR;
104 else if (encap == LB_ENCAP_TYPE_NAT4)
105 args.type = LB_VIP_TYPE_IP4_NAT4;
106 else if (encap == LB_ENCAP_TYPE_NAT6)
108 error = clib_error_return(0, "currently does not support NAT46");
114 if (encap == LB_ENCAP_TYPE_GRE4)
115 args.type = LB_VIP_TYPE_IP6_GRE4;
116 else if (encap == LB_ENCAP_TYPE_GRE6)
117 args.type = LB_VIP_TYPE_IP6_GRE6;
118 else if (encap == LB_ENCAP_TYPE_NAT6)
119 args.type = LB_VIP_TYPE_IP6_NAT6;
120 else if (encap == LB_ENCAP_TYPE_NAT4)
122 error = clib_error_return(0, "currently does not support NAT64");
127 lb_garbage_collection();
131 if (encap == LB_ENCAP_TYPE_L3DSR) {
132 args.encap_args.dscp = (u8)(dscp & 0x3F);
134 else if ((encap == LB_ENCAP_TYPE_NAT4)
135 || (encap == LB_ENCAP_TYPE_NAT6))
137 args.encap_args.srv_type = (u8) srv_type;
138 args.encap_args.port = (u16) port;
139 args.encap_args.target_port = (u16) target_port;
140 args.encap_args.node_port = (u16) node_port;
143 if ((ret = lb_vip_add(args, &index))) {
144 error = clib_error_return (0, "lb_vip_add error %d", ret);
147 vlib_cli_output(vm, "lb_vip_add ok %d", index);
150 if ((ret = lb_vip_find_index(&(args.prefix), args.plen, &index))) {
151 error = clib_error_return (0, "lb_vip_find_index error %d", ret);
153 } else if ((ret = lb_vip_del(index))) {
154 error = clib_error_return (0, "lb_vip_del error %d", ret);
160 unformat_free (line_input);
165 VLIB_CLI_COMMAND (lb_vip_command, static) =
168 .short_help = "lb vip <prefix> [encap (gre6|gre4|l3dsr|nat4|nat6)] "
170 "[type (nodeport|clusterip) port <n> target_port <n> node_port <n>] "
171 "[new_len <n>] [del]",
172 .function = lb_vip_command_fn,
175 static clib_error_t *
176 lb_as_command_fn (vlib_main_t * vm,
177 unformat_input_t * input, vlib_cli_command_t * cmd)
179 unformat_input_t _line_input, *line_input = &_line_input;
180 ip46_address_t vip_prefix, as_addr;
182 ip46_address_t *as_array = 0;
186 clib_error_t *error = 0;
188 if (!unformat_user (input, unformat_line_input, line_input))
191 if (!unformat(line_input, "%U", unformat_ip46_prefix, &vip_prefix, &vip_plen, IP46_TYPE_ANY)) {
192 error = clib_error_return (0, "invalid as address: '%U'",
193 format_unformat_error, line_input);
197 if ((ret = lb_vip_find_index(&vip_prefix, vip_plen, &vip_index))) {
198 error = clib_error_return (0, "lb_vip_find_index error %d", ret);
202 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
204 if (unformat(line_input, "%U", unformat_ip46_address, &as_addr, IP46_TYPE_ANY)) {
205 vec_add1(as_array, as_addr);
206 } else if (unformat(line_input, "del")) {
209 error = clib_error_return (0, "parse error: '%U'",
210 format_unformat_error, line_input);
215 if (!vec_len(as_array)) {
216 error = clib_error_return (0, "No AS address provided");
220 lb_garbage_collection();
221 clib_warning("vip index is %d", vip_index);
224 if ((ret = lb_vip_del_ass(vip_index, as_array, vec_len(as_array)))) {
225 error = clib_error_return (0, "lb_vip_del_ass error %d", ret);
229 if ((ret = lb_vip_add_ass(vip_index, as_array, vec_len(as_array)))) {
230 error = clib_error_return (0, "lb_vip_add_ass error %d", ret);
236 unformat_free (line_input);
242 VLIB_CLI_COMMAND (lb_as_command, static) =
245 .short_help = "lb as <vip-prefix> [<address> [<address> [...]]] [del]",
246 .function = lb_as_command_fn,
249 static clib_error_t *
250 lb_conf_command_fn (vlib_main_t * vm,
251 unformat_input_t * input, vlib_cli_command_t * cmd)
253 lb_main_t *lbm = &lb_main;
254 unformat_input_t _line_input, *line_input = &_line_input;
255 ip4_address_t ip4 = lbm->ip4_src_address;
256 ip6_address_t ip6 = lbm->ip6_src_address;
257 u32 per_cpu_sticky_buckets = lbm->per_cpu_sticky_buckets;
258 u32 per_cpu_sticky_buckets_log2 = 0;
259 u32 flow_timeout = lbm->flow_timeout;
261 clib_error_t *error = 0;
263 if (!unformat_user (input, unformat_line_input, line_input))
266 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
268 if (unformat(line_input, "ip4-src-address %U", unformat_ip4_address, &ip4))
270 else if (unformat(line_input, "ip6-src-address %U", unformat_ip6_address, &ip6))
272 else if (unformat(line_input, "buckets %d", &per_cpu_sticky_buckets))
274 else if (unformat(line_input, "buckets-log2 %d", &per_cpu_sticky_buckets_log2)) {
275 if (per_cpu_sticky_buckets_log2 >= 32)
276 return clib_error_return (0, "buckets-log2 value is too high");
277 per_cpu_sticky_buckets = 1 << per_cpu_sticky_buckets_log2;
278 } else if (unformat(line_input, "timeout %d", &flow_timeout))
281 error = clib_error_return (0, "parse error: '%U'",
282 format_unformat_error, line_input);
287 lb_garbage_collection();
289 if ((ret = lb_conf(&ip4, &ip6, per_cpu_sticky_buckets, flow_timeout))) {
290 error = clib_error_return (0, "lb_conf error %d", ret);
295 unformat_free (line_input);
300 VLIB_CLI_COMMAND (lb_conf_command, static) =
303 .short_help = "lb conf [ip4-src-address <addr>] [ip6-src-address <addr>] [buckets <n>] [timeout <s>]",
304 .function = lb_conf_command_fn,
307 static clib_error_t *
308 lb_show_command_fn (vlib_main_t * vm,
309 unformat_input_t * input, vlib_cli_command_t * cmd)
311 vlib_cli_output(vm, "%U", format_lb_main);
316 VLIB_CLI_COMMAND (lb_show_command, static) =
319 .short_help = "show lb",
320 .function = lb_show_command_fn,
323 static clib_error_t *
324 lb_show_vips_command_fn (vlib_main_t * vm,
325 unformat_input_t * input, vlib_cli_command_t * cmd)
327 unformat_input_t line_input;
328 lb_main_t *lbm = &lb_main;
332 if (!unformat_user (input, unformat_line_input, &line_input))
335 if (unformat(&line_input, "verbose"))
338 pool_foreach(vip, lbm->vips, {
339 vlib_cli_output(vm, "%U\n", verbose?format_lb_vip_detailed:format_lb_vip, vip);
342 unformat_free (&line_input);
346 VLIB_CLI_COMMAND (lb_show_vips_command, static) =
348 .path = "show lb vips",
349 .short_help = "show lb vips [verbose]",
350 .function = lb_show_vips_command_fn,
353 static clib_error_t *
354 lb_set_interface_nat_command_fn (vlib_main_t * vm,
355 unformat_input_t * input,
356 vlib_cli_command_t * cmd,
359 unformat_input_t _line_input, *line_input = &_line_input;
360 vnet_main_t * vnm = vnet_get_main();
361 clib_error_t * error = 0;
362 u32 * sw_if_index = 0;
363 u32 * inside_sw_if_indices = 0;
366 /* Get a line of input. */
367 if (!unformat_user (input, unformat_line_input, line_input))
370 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
372 if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
374 vec_add1 (inside_sw_if_indices, *sw_if_index);
375 else if (unformat (line_input, "del"))
379 error = clib_error_return (0, "unknown input '%U'",
380 format_unformat_error, line_input);
385 vec_foreach (sw_if_index, inside_sw_if_indices)
389 if (lb_nat4_interface_add_del (*sw_if_index, is_del))
391 error = clib_error_return(
392 0, "%s %U failed", is_del ? "del" : "add",
393 format_vnet_sw_interface_name, vnm,
394 vnet_get_sw_interface (vnm, *sw_if_index));
400 if (lb_nat6_interface_add_del (*sw_if_index, is_del))
402 error = clib_error_return(
403 0, "%s %U failed", is_del ? "del" : "add",
404 format_vnet_sw_interface_name, vnm,
405 vnet_get_sw_interface (vnm, *sw_if_index));
412 unformat_free (line_input);
413 vec_free (inside_sw_if_indices);
418 static clib_error_t *
419 lb_set_interface_nat4_command_fn (vlib_main_t * vm,
420 unformat_input_t * input,
421 vlib_cli_command_t * cmd)
423 return lb_set_interface_nat_command_fn(vm, input, cmd, 0);
426 VLIB_CLI_COMMAND (lb_set_interface_nat4_command, static) = {
427 .path = "lb set interface nat4",
428 .function = lb_set_interface_nat4_command_fn,
429 .short_help = "lb set interface nat4 in <intfc> [del]",
432 static clib_error_t *
433 lb_set_interface_nat6_command_fn (vlib_main_t * vm,
434 unformat_input_t * input,
435 vlib_cli_command_t * cmd)
437 return lb_set_interface_nat_command_fn(vm, input, cmd, 1);
440 VLIB_CLI_COMMAND (lb_set_interface_nat6_command, static) = {
441 .path = "lb set interface nat6",
442 .function = lb_set_interface_nat6_command_fn,
443 .short_help = "lb set interface nat6 in <intfc> [del]",
446 static clib_error_t *
447 lb_flowtable_flush_command_fn (vlib_main_t * vm,
448 unformat_input_t * input, vlib_cli_command_t * cmd)
451 vlib_thread_main_t *tm = vlib_get_thread_main();
452 lb_main_t *lbm = &lb_main;
454 for(thread_index = 0; thread_index < tm->n_vlib_mains; thread_index++ ) {
455 lb_hash_t *h = lbm->per_cpu[thread_index].sticky_ht;
460 lb_hash_foreach_entry(h, b, i) {
461 vlib_refcount_add(&lbm->as_refcount, thread_index, b->value[i], -1);
462 vlib_refcount_add(&lbm->as_refcount, thread_index, 0, 1);
466 lbm->per_cpu[thread_index].sticky_ht = 0;
474 * flush all lb flowtables
475 * This is indented for debug and unit-tests purposes only
477 VLIB_CLI_COMMAND (lb_flowtable_flush_command, static) =
479 .path = "test lb flowtable flush",
480 .short_help = "test lb flowtable flush",
481 .function = lb_flowtable_flush_command_fn,