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;
30 u32 srv_type = LB_SRV_TYPE_CLUSTERIP;
32 clib_error_t *error = 0;
34 args.new_length = 1024;
35 args.src_ip_sticky = 0;
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, "src_ip_sticky"))
54 args.src_ip_sticky = 1;
55 else if (unformat(line_input, "protocol tcp"))
57 args.protocol = (u8)IP_PROTOCOL_TCP;
59 else if (unformat(line_input, "protocol udp"))
61 args.protocol = (u8)IP_PROTOCOL_UDP;
63 else if (unformat(line_input, "port %d", &port))
65 else if (unformat(line_input, "encap gre4"))
66 encap = LB_ENCAP_TYPE_GRE4;
67 else if (unformat(line_input, "encap gre6"))
68 encap = LB_ENCAP_TYPE_GRE6;
69 else if (unformat(line_input, "encap l3dsr"))
70 encap = LB_ENCAP_TYPE_L3DSR;
71 else if (unformat(line_input, "encap nat4"))
72 encap = LB_ENCAP_TYPE_NAT4;
73 else if (unformat(line_input, "encap nat6"))
74 encap = LB_ENCAP_TYPE_NAT6;
75 else if (unformat(line_input, "dscp %d", &dscp))
77 else if (unformat(line_input, "type clusterip"))
78 srv_type = LB_SRV_TYPE_CLUSTERIP;
79 else if (unformat(line_input, "type nodeport"))
80 srv_type = LB_SRV_TYPE_NODEPORT;
81 else if (unformat(line_input, "target_port %d", &target_port))
84 error = clib_error_return (0, "parse error: '%U'",
85 format_unformat_error, line_input);
90 /* if port == 0, it means all-port VIP */
98 args.port = (u16)port;
101 if ((encap != LB_ENCAP_TYPE_L3DSR) && (dscp != ~0))
103 error = clib_error_return(0, "lb_vip_add error: "
104 "should not configure dscp for none L3DSR.");
108 if ((encap == LB_ENCAP_TYPE_L3DSR) && (dscp >= 64))
110 error = clib_error_return(0, "lb_vip_add error: "
111 "dscp for L3DSR should be less than 64.");
115 if (ip46_prefix_is_ip4(&(args.prefix), (args.plen)))
117 if (encap == LB_ENCAP_TYPE_GRE4)
118 args.type = LB_VIP_TYPE_IP4_GRE4;
119 else if (encap == LB_ENCAP_TYPE_GRE6)
120 args.type = LB_VIP_TYPE_IP4_GRE6;
121 else if (encap == LB_ENCAP_TYPE_L3DSR)
122 args.type = LB_VIP_TYPE_IP4_L3DSR;
123 else if (encap == LB_ENCAP_TYPE_NAT4)
124 args.type = LB_VIP_TYPE_IP4_NAT4;
125 else if (encap == LB_ENCAP_TYPE_NAT6)
127 error = clib_error_return(0, "currently does not support NAT46");
133 if (encap == LB_ENCAP_TYPE_GRE4)
134 args.type = LB_VIP_TYPE_IP6_GRE4;
135 else if (encap == LB_ENCAP_TYPE_GRE6)
136 args.type = LB_VIP_TYPE_IP6_GRE6;
137 else if (encap == LB_ENCAP_TYPE_NAT6)
138 args.type = LB_VIP_TYPE_IP6_NAT6;
139 else if (encap == LB_ENCAP_TYPE_NAT4)
141 error = clib_error_return(0, "currently does not support NAT64");
146 lb_garbage_collection();
150 if (encap == LB_ENCAP_TYPE_L3DSR) {
151 args.encap_args.dscp = (u8)(dscp & 0x3F);
153 else if ((encap == LB_ENCAP_TYPE_NAT4)
154 || (encap == LB_ENCAP_TYPE_NAT6))
156 args.encap_args.srv_type = (u8) srv_type;
157 args.encap_args.target_port = (u16) target_port;
160 if ((ret = lb_vip_add(args, &index))) {
161 error = clib_error_return (0, "lb_vip_add error %d", ret);
164 vlib_cli_output(vm, "lb_vip_add ok %d", index);
167 if ((ret = lb_vip_find_index(&(args.prefix), args.plen,
168 args.protocol, args.port, &index))) {
169 error = clib_error_return (0, "lb_vip_find_index error %d", ret);
171 } else if ((ret = lb_vip_del(index))) {
172 error = clib_error_return (0, "lb_vip_del error %d", ret);
178 unformat_free (line_input);
183 /* clang-format off */
184 VLIB_CLI_COMMAND (lb_vip_command, static) =
187 .short_help = "lb vip <prefix> "
188 "[protocol (tcp|udp) port <n>] "
189 "[encap (gre6|gre4|l3dsr|nat4|nat6)] "
191 "[type (nodeport|clusterip) target_port <n>] "
192 "[new_len <n>] [src_ip_sticky] [del]",
193 .function = lb_vip_command_fn,
195 /* clang-format on */
197 static clib_error_t *
198 lb_as_command_fn (vlib_main_t * vm,
199 unformat_input_t * input, vlib_cli_command_t * cmd)
201 unformat_input_t _line_input, *line_input = &_line_input;
202 ip46_address_t vip_prefix, as_addr;
204 ip46_address_t *as_array = 0;
211 clib_error_t *error = 0;
213 if (!unformat_user (input, unformat_line_input, line_input))
216 if (!unformat(line_input, "%U", unformat_ip46_prefix,
217 &vip_prefix, &vip_plen, IP46_TYPE_ANY))
219 error = clib_error_return (0, "invalid as address: '%U'",
220 format_unformat_error, line_input);
224 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
226 if (unformat(line_input, "%U", unformat_ip46_address,
227 &as_addr, IP46_TYPE_ANY))
229 vec_add1(as_array, as_addr);
231 else if (unformat(line_input, "del"))
235 else if (unformat(line_input, "flush"))
239 else if (unformat(line_input, "protocol tcp"))
241 protocol = (u8)IP_PROTOCOL_TCP;
243 else if (unformat(line_input, "protocol udp"))
245 protocol = (u8)IP_PROTOCOL_UDP;
247 else if (unformat(line_input, "port %d", &port))
250 error = clib_error_return (0, "parse error: '%U'",
251 format_unformat_error, line_input);
256 /* If port == 0, it means all-port VIP */
262 if ((ret = lb_vip_find_index(&vip_prefix, vip_plen, protocol,
263 (u16)port, &vip_index))){
264 error = clib_error_return (0, "lb_vip_find_index error %d", ret);
268 if (!vec_len(as_array)) {
269 error = clib_error_return (0, "No AS address provided");
273 lb_garbage_collection();
274 clib_warning("vip index is %d", vip_index);
277 if ((ret = lb_vip_del_ass(vip_index, as_array, vec_len(as_array), flush)))
279 error = clib_error_return (0, "lb_vip_del_ass error %d", ret);
283 if ((ret = lb_vip_add_ass(vip_index, as_array, vec_len(as_array))))
285 error = clib_error_return (0, "lb_vip_add_ass error %d", ret);
291 unformat_free (line_input);
297 VLIB_CLI_COMMAND (lb_as_command, static) =
300 .short_help = "lb as <vip-prefix> [protocol (tcp|udp) port <n>]"
301 " [<address> [<address> [...]]] [del] [flush]",
302 .function = lb_as_command_fn,
305 static clib_error_t *
306 lb_conf_command_fn (vlib_main_t * vm,
307 unformat_input_t * input, vlib_cli_command_t * cmd)
309 lb_main_t *lbm = &lb_main;
310 unformat_input_t _line_input, *line_input = &_line_input;
311 ip4_address_t ip4 = lbm->ip4_src_address;
312 ip6_address_t ip6 = lbm->ip6_src_address;
313 u32 per_cpu_sticky_buckets = lbm->per_cpu_sticky_buckets;
314 u32 per_cpu_sticky_buckets_log2 = 0;
315 u32 flow_timeout = lbm->flow_timeout;
317 clib_error_t *error = 0;
319 if (!unformat_user (input, unformat_line_input, line_input))
322 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
324 if (unformat(line_input, "ip4-src-address %U", unformat_ip4_address, &ip4))
326 else if (unformat(line_input, "ip6-src-address %U", unformat_ip6_address, &ip6))
328 else if (unformat(line_input, "buckets %d", &per_cpu_sticky_buckets))
330 else if (unformat(line_input, "buckets-log2 %d", &per_cpu_sticky_buckets_log2)) {
331 if (per_cpu_sticky_buckets_log2 >= 32)
332 return clib_error_return (0, "buckets-log2 value is too high");
333 per_cpu_sticky_buckets = 1 << per_cpu_sticky_buckets_log2;
334 } else if (unformat(line_input, "timeout %d", &flow_timeout))
337 error = clib_error_return (0, "parse error: '%U'",
338 format_unformat_error, line_input);
343 lb_garbage_collection();
345 if ((ret = lb_conf(&ip4, &ip6, per_cpu_sticky_buckets, flow_timeout))) {
346 error = clib_error_return (0, "lb_conf error %d", ret);
351 unformat_free (line_input);
356 VLIB_CLI_COMMAND (lb_conf_command, static) =
359 .short_help = "lb conf [ip4-src-address <addr>] [ip6-src-address <addr>] [buckets <n>] [timeout <s>]",
360 .function = lb_conf_command_fn,
363 static clib_error_t *
364 lb_show_command_fn (vlib_main_t * vm,
365 unformat_input_t * input, vlib_cli_command_t * cmd)
367 vlib_cli_output(vm, "%U", format_lb_main);
372 VLIB_CLI_COMMAND (lb_show_command, static) =
375 .short_help = "show lb",
376 .function = lb_show_command_fn,
379 static clib_error_t *
380 lb_show_vips_command_fn (vlib_main_t * vm,
381 unformat_input_t * input, vlib_cli_command_t * cmd)
383 unformat_input_t line_input;
384 lb_main_t *lbm = &lb_main;
388 if (!unformat_user (input, unformat_line_input, &line_input))
391 if (unformat(&line_input, "verbose"))
394 /* Hide placeholder VIP */
395 pool_foreach (vip, lbm->vips) {
396 if (vip != lbm->vips) {
397 vlib_cli_output(vm, "%U\n", verbose?format_lb_vip_detailed:format_lb_vip, vip);
401 unformat_free (&line_input);
405 VLIB_CLI_COMMAND (lb_show_vips_command, static) =
407 .path = "show lb vips",
408 .short_help = "show lb vips [verbose]",
409 .function = lb_show_vips_command_fn,
412 static clib_error_t *
413 lb_set_interface_nat_command_fn (vlib_main_t * vm,
414 unformat_input_t * input,
415 vlib_cli_command_t * cmd,
418 unformat_input_t _line_input, *line_input = &_line_input;
419 vnet_main_t * vnm = vnet_get_main();
420 clib_error_t * error = 0;
421 u32 _sw_if_index, *sw_if_index = &_sw_if_index;
422 u32 * inside_sw_if_indices = 0;
425 /* Get a line of input. */
426 if (!unformat_user (input, unformat_line_input, line_input))
429 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
431 if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
433 vec_add1 (inside_sw_if_indices, *sw_if_index);
434 else if (unformat (line_input, "del"))
438 error = clib_error_return (0, "unknown input '%U'",
439 format_unformat_error, line_input);
444 vec_foreach (sw_if_index, inside_sw_if_indices)
448 if (lb_nat4_interface_add_del (*sw_if_index, is_del))
450 error = clib_error_return (
451 0, "%s %U failed", is_del ? "del" : "add",
452 format_vnet_sw_if_index_name, vnm, *sw_if_index);
458 if (lb_nat6_interface_add_del (*sw_if_index, is_del))
460 error = clib_error_return (
461 0, "%s %U failed", is_del ? "del" : "add",
462 format_vnet_sw_if_index_name, vnm, *sw_if_index);
469 unformat_free (line_input);
470 vec_free (inside_sw_if_indices);
475 static clib_error_t *
476 lb_set_interface_nat4_command_fn (vlib_main_t * vm,
477 unformat_input_t * input,
478 vlib_cli_command_t * cmd)
480 return lb_set_interface_nat_command_fn(vm, input, cmd, 0);
483 VLIB_CLI_COMMAND (lb_set_interface_nat4_command, static) = {
484 .path = "lb set interface nat4",
485 .function = lb_set_interface_nat4_command_fn,
486 .short_help = "lb set interface nat4 in <intfc> [del]",
489 static clib_error_t *
490 lb_set_interface_nat6_command_fn (vlib_main_t * vm,
491 unformat_input_t * input,
492 vlib_cli_command_t * cmd)
494 return lb_set_interface_nat_command_fn(vm, input, cmd, 1);
497 VLIB_CLI_COMMAND (lb_set_interface_nat6_command, static) = {
498 .path = "lb set interface nat6",
499 .function = lb_set_interface_nat6_command_fn,
500 .short_help = "lb set interface nat6 in <intfc> [del]",
503 static clib_error_t *
504 lb_flowtable_flush_command_fn (vlib_main_t * vm,
505 unformat_input_t * input, vlib_cli_command_t * cmd)
507 lb_flush_vip_as(~0, 0);
512 static clib_error_t *
513 lb_flush_vip_command_fn (vlib_main_t * vm,
514 unformat_input_t * input,
515 vlib_cli_command_t * cmd)
517 unformat_input_t _line_input, *line_input = &_line_input;
519 ip46_address_t vip_prefix;
524 clib_error_t *error = 0;
526 if (!unformat_user (input, unformat_line_input, line_input))
529 if (!unformat(line_input, "%U", unformat_ip46_prefix, &vip_prefix,
530 &vip_plen, IP46_TYPE_ANY, &vip_plen)) {
531 error = clib_error_return (0, "invalid vip prefix: '%U'",
532 format_unformat_error, line_input);
536 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
538 if (unformat(line_input, "protocol tcp"))
540 protocol = (u8)IP_PROTOCOL_TCP;
542 else if (unformat(line_input, "protocol udp"))
544 protocol = (u8)IP_PROTOCOL_UDP;
546 else if (unformat(line_input, "port %d", &port))
555 if ((ret = lb_vip_find_index(&vip_prefix, vip_plen, protocol,
556 (u16)port, &vip_index))){
557 error = clib_error_return (0, "lb_vip_find_index error %d", ret);
561 if ((ret = lb_flush_vip_as(vip_index, ~0)))
563 error = clib_error_return (0, "lb_flush_vip error %d", ret);
567 vlib_cli_output(vm, "lb_flush_vip ok %d", vip_index);
571 unformat_free (line_input);
577 * flush lb flowtable as per vip
579 VLIB_CLI_COMMAND (lb_flush_vip_command, static) =
581 .path = "lb flush vip",
582 .short_help = "lb flush vip <prefix> "
583 "[protocol (tcp|udp) port <n>]",
584 .function = lb_flush_vip_command_fn,
588 * flush all lb flowtables
589 * This is indented for debug and unit-tests purposes only
591 VLIB_CLI_COMMAND (lb_flowtable_flush_command, static) =
593 .path = "test lb flowtable flush",
594 .short_help = "test lb flowtable flush",
595 .function = lb_flowtable_flush_command_fn,