X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=src%2Fplugins%2Flb%2Fcli.c;h=76f05a586de96e4f83261e71de26b37e225ceebb;hb=54d0ac39f70b267f86e30c84d0e5b0c41773d1c4;hp=b59c642624128ef69a1356a078e8239387f3a3d6;hpb=cb034b9b374927c7552e36dcbc306d8456b2a0cb;p=vpp.git diff --git a/src/plugins/lb/cli.c b/src/plugins/lb/cli.c index b59c6426241..76f05a586de 100644 --- a/src/plugins/lb/cli.c +++ b/src/plugins/lb/cli.c @@ -21,67 +21,154 @@ lb_vip_command_fn (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { unformat_input_t _line_input, *line_input = &_line_input; - ip46_address_t prefix; - u8 plen; - u32 new_len = 1024; + lb_vip_add_args_t args; u8 del = 0; int ret; - u32 gre4 = 0; - lb_vip_type_t type; + u32 encap = 0; + u32 dscp = ~0; + u32 srv_type = LB_SRV_TYPE_CLUSTERIP; + u32 port = 0; + u32 target_port = 0; + u32 node_port = 0; + clib_error_t *error = 0; + + args.new_length = 1024; if (!unformat_user (input, unformat_line_input, line_input)) return 0; - if (!unformat(line_input, "%U", unformat_ip46_prefix, &prefix, &plen, IP46_TYPE_ANY, &plen)) - return clib_error_return (0, "invalid vip prefix: '%U'", - format_unformat_error, line_input); + if (!unformat(line_input, "%U", unformat_ip46_prefix, &(args.prefix), + &(args.plen), IP46_TYPE_ANY, &(args.plen))) { + error = clib_error_return (0, "invalid vip prefix: '%U'", + format_unformat_error, line_input); + goto done; + } while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) { - if (unformat(line_input, "new_len %d", &new_len)) + if (unformat(line_input, "new_len %d", &(args.new_length))) ; else if (unformat(line_input, "del")) del = 1; else if (unformat(line_input, "encap gre4")) - gre4 = 1; + encap = LB_ENCAP_TYPE_GRE4; else if (unformat(line_input, "encap gre6")) - gre4 = 0; - else - return clib_error_return (0, "parse error: '%U'", - format_unformat_error, line_input); + encap = LB_ENCAP_TYPE_GRE6; + else if (unformat(line_input, "encap l3dsr")) + encap = LB_ENCAP_TYPE_L3DSR; + else if (unformat(line_input, "encap nat4")) + encap = LB_ENCAP_TYPE_NAT4; + else if (unformat(line_input, "encap nat6")) + encap = LB_ENCAP_TYPE_NAT6; + else if (unformat(line_input, "dscp %d", &dscp)) + ; + else if (unformat(line_input, "type clusterip")) + srv_type = LB_SRV_TYPE_CLUSTERIP; + else if (unformat(line_input, "type nodeport")) + srv_type = LB_SRV_TYPE_NODEPORT; + else if (unformat(line_input, "port %d", &port)) + ; + else if (unformat(line_input, "target_port %d", &target_port)) + ; + else if (unformat(line_input, "node_port %d", &node_port)) + ; + else { + error = clib_error_return (0, "parse error: '%U'", + format_unformat_error, line_input); + goto done; + } } - unformat_free (line_input); + if ((encap != LB_ENCAP_TYPE_L3DSR) && (dscp != ~0)) + { + error = clib_error_return(0, "lb_vip_add error: " + "should not configure dscp for none L3DSR."); + goto done; + } + if ((encap == LB_ENCAP_TYPE_L3DSR) && (dscp >= 64)) + { + error = clib_error_return(0, "lb_vip_add error: " + "dscp for L3DSR should be less than 64."); + goto done; + } - if (ip46_prefix_is_ip4(&prefix, plen)) { - type = (gre4)?LB_VIP_TYPE_IP4_GRE4:LB_VIP_TYPE_IP4_GRE6; - } else { - type = (gre4)?LB_VIP_TYPE_IP6_GRE4:LB_VIP_TYPE_IP6_GRE6; - } + if (ip46_prefix_is_ip4(&(args.prefix), (args.plen))) + { + if (encap == LB_ENCAP_TYPE_GRE4) + args.type = LB_VIP_TYPE_IP4_GRE4; + else if (encap == LB_ENCAP_TYPE_GRE6) + args.type = LB_VIP_TYPE_IP4_GRE6; + else if (encap == LB_ENCAP_TYPE_L3DSR) + args.type = LB_VIP_TYPE_IP4_L3DSR; + else if (encap == LB_ENCAP_TYPE_NAT4) + args.type = LB_VIP_TYPE_IP4_NAT4; + else if (encap == LB_ENCAP_TYPE_NAT6) + { + error = clib_error_return(0, "currently does not support NAT46"); + goto done; + } + } + else + { + if (encap == LB_ENCAP_TYPE_GRE4) + args.type = LB_VIP_TYPE_IP6_GRE4; + else if (encap == LB_ENCAP_TYPE_GRE6) + args.type = LB_VIP_TYPE_IP6_GRE6; + else if (encap == LB_ENCAP_TYPE_NAT6) + args.type = LB_VIP_TYPE_IP6_NAT6; + else if (encap == LB_ENCAP_TYPE_NAT4) + { + error = clib_error_return(0, "currently does not support NAT64"); + goto done; + } + } lb_garbage_collection(); u32 index; if (!del) { - if ((ret = lb_vip_add(&prefix, plen, type, new_len, &index))) { - return clib_error_return (0, "lb_vip_add error %d", ret); + if (encap == LB_ENCAP_TYPE_L3DSR) { + args.encap_args.dscp = (u8)(dscp & 0x3F); + } + else if ((encap == LB_ENCAP_TYPE_NAT4) + || (encap == LB_ENCAP_TYPE_NAT6)) + { + args.encap_args.srv_type = (u8) srv_type; + args.encap_args.port = (u16) port; + args.encap_args.target_port = (u16) target_port; + args.encap_args.node_port = (u16) node_port; + } + + if ((ret = lb_vip_add(args, &index))) { + error = clib_error_return (0, "lb_vip_add error %d", ret); + goto done; } else { vlib_cli_output(vm, "lb_vip_add ok %d", index); } } else { - if ((ret = lb_vip_find_index(&prefix, plen, &index))) - return clib_error_return (0, "lb_vip_find_index error %d", ret); - else if ((ret = lb_vip_del(index))) - return clib_error_return (0, "lb_vip_del error %d", ret); + if ((ret = lb_vip_find_index(&(args.prefix), args.plen, &index))) { + error = clib_error_return (0, "lb_vip_find_index error %d", ret); + goto done; + } else if ((ret = lb_vip_del(index))) { + error = clib_error_return (0, "lb_vip_del error %d", ret); + goto done; + } } - return NULL; + +done: + unformat_free (line_input); + + return error; } VLIB_CLI_COMMAND (lb_vip_command, static) = { .path = "lb vip", - .short_help = "lb vip [encap (gre6|gre4)] [new_len ] [del]", + .short_help = "lb vip [encap (gre6|gre4|l3dsr|nat4|nat6)] " + "[dscp ] " + "[type (nodeport|clusterip) port target_port node_port ] " + "[new_len ] [del]", .function = lb_vip_command_fn, }; @@ -96,16 +183,21 @@ lb_as_command_fn (vlib_main_t * vm, u32 vip_index; u8 del = 0; int ret; + clib_error_t *error = 0; if (!unformat_user (input, unformat_line_input, line_input)) return 0; - if (!unformat(line_input, "%U", unformat_ip46_prefix, &vip_prefix, &vip_plen, IP46_TYPE_ANY)) - return clib_error_return (0, "invalid as address: '%U'", - format_unformat_error, line_input); + if (!unformat(line_input, "%U", unformat_ip46_prefix, &vip_prefix, &vip_plen, IP46_TYPE_ANY)) { + error = clib_error_return (0, "invalid as address: '%U'", + format_unformat_error, line_input); + goto done; + } - if ((ret = lb_vip_find_index(&vip_prefix, vip_plen, &vip_index))) - return clib_error_return (0, "lb_vip_find_index error %d", ret); + if ((ret = lb_vip_find_index(&vip_prefix, vip_plen, &vip_index))) { + error = clib_error_return (0, "lb_vip_find_index error %d", ret); + goto done; + } while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) { @@ -114,15 +206,15 @@ lb_as_command_fn (vlib_main_t * vm, } else if (unformat(line_input, "del")) { del = 1; } else { - vec_free(as_array); - return clib_error_return (0, "parse error: '%U'", - format_unformat_error, line_input); + error = clib_error_return (0, "parse error: '%U'", + format_unformat_error, line_input); + goto done; } } if (!vec_len(as_array)) { - vec_free(as_array); - return clib_error_return (0, "No AS address provided"); + error = clib_error_return (0, "No AS address provided"); + goto done; } lb_garbage_collection(); @@ -130,18 +222,21 @@ lb_as_command_fn (vlib_main_t * vm, if (del) { if ((ret = lb_vip_del_ass(vip_index, as_array, vec_len(as_array)))) { - vec_free(as_array); - return clib_error_return (0, "lb_vip_del_ass error %d", ret); + error = clib_error_return (0, "lb_vip_del_ass error %d", ret); + goto done; } } else { if ((ret = lb_vip_add_ass(vip_index, as_array, vec_len(as_array)))) { - vec_free(as_array); - return clib_error_return (0, "lb_vip_add_ass error %d", ret); + error = clib_error_return (0, "lb_vip_add_ass error %d", ret); + goto done; } } +done: + unformat_free (line_input); vec_free(as_array); - return 0; + + return error; } VLIB_CLI_COMMAND (lb_as_command, static) = @@ -163,6 +258,7 @@ lb_conf_command_fn (vlib_main_t * vm, u32 per_cpu_sticky_buckets_log2 = 0; u32 flow_timeout = lbm->flow_timeout; int ret; + clib_error_t *error = 0; if (!unformat_user (input, unformat_line_input, line_input)) return 0; @@ -181,19 +277,24 @@ lb_conf_command_fn (vlib_main_t * vm, per_cpu_sticky_buckets = 1 << per_cpu_sticky_buckets_log2; } else if (unformat(line_input, "timeout %d", &flow_timeout)) ; - else - return clib_error_return (0, "parse error: '%U'", - format_unformat_error, line_input); + else { + error = clib_error_return (0, "parse error: '%U'", + format_unformat_error, line_input); + goto done; + } } - unformat_free (line_input); - lb_garbage_collection(); - if ((ret = lb_conf(&ip4, &ip6, per_cpu_sticky_buckets, flow_timeout))) - return clib_error_return (0, "lb_conf error %d", ret); + if ((ret = lb_conf(&ip4, &ip6, per_cpu_sticky_buckets, flow_timeout))) { + error = clib_error_return (0, "lb_conf error %d", ret); + goto done; + } + +done: + unformat_free (line_input); - return NULL; + return error; } VLIB_CLI_COMMAND (lb_conf_command, static) = @@ -248,3 +349,134 @@ VLIB_CLI_COMMAND (lb_show_vips_command, static) = .short_help = "show lb vips [verbose]", .function = lb_show_vips_command_fn, }; + +static clib_error_t * +lb_set_interface_nat_command_fn (vlib_main_t * vm, + unformat_input_t * input, + vlib_cli_command_t * cmd, + u8 is_nat6) +{ + unformat_input_t _line_input, *line_input = &_line_input; + vnet_main_t * vnm = vnet_get_main(); + clib_error_t * error = 0; + u32 _sw_if_index, *sw_if_index = &_sw_if_index; + u32 * inside_sw_if_indices = 0; + int is_del = 0; + + /* Get a line of input. */ + if (!unformat_user (input, unformat_line_input, line_input)) + return 0; + + while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) + { + if (unformat (line_input, "in %U", unformat_vnet_sw_interface, + vnm, sw_if_index)) + vec_add1 (inside_sw_if_indices, *sw_if_index); + else if (unformat (line_input, "del")) + is_del = 1; + else + { + error = clib_error_return (0, "unknown input '%U'", + format_unformat_error, line_input); + goto done; + } + } + + vec_foreach (sw_if_index, inside_sw_if_indices) + { + if (!is_nat6) + { + if (lb_nat4_interface_add_del (*sw_if_index, is_del)) + { + error = clib_error_return( + 0, "%s %U failed", is_del ? "del" : "add", + format_vnet_sw_interface_name, vnm, + vnet_get_sw_interface (vnm, *sw_if_index)); + goto done; + } + } + else + { + if (lb_nat6_interface_add_del (*sw_if_index, is_del)) + { + error = clib_error_return( + 0, "%s %U failed", is_del ? "del" : "add", + format_vnet_sw_interface_name, vnm, + vnet_get_sw_interface (vnm, *sw_if_index)); + goto done; + } + } + } + +done: + unformat_free (line_input); + vec_free (inside_sw_if_indices); + + return error; +} + +static clib_error_t * +lb_set_interface_nat4_command_fn (vlib_main_t * vm, + unformat_input_t * input, + vlib_cli_command_t * cmd) +{ + return lb_set_interface_nat_command_fn(vm, input, cmd, 0); +} + +VLIB_CLI_COMMAND (lb_set_interface_nat4_command, static) = { + .path = "lb set interface nat4", + .function = lb_set_interface_nat4_command_fn, + .short_help = "lb set interface nat4 in [del]", +}; + +static clib_error_t * +lb_set_interface_nat6_command_fn (vlib_main_t * vm, + unformat_input_t * input, + vlib_cli_command_t * cmd) +{ + return lb_set_interface_nat_command_fn(vm, input, cmd, 1); +} + +VLIB_CLI_COMMAND (lb_set_interface_nat6_command, static) = { + .path = "lb set interface nat6", + .function = lb_set_interface_nat6_command_fn, + .short_help = "lb set interface nat6 in [del]", +}; + +static clib_error_t * +lb_flowtable_flush_command_fn (vlib_main_t * vm, + unformat_input_t * input, vlib_cli_command_t * cmd) +{ + u32 thread_index; + vlib_thread_main_t *tm = vlib_get_thread_main(); + lb_main_t *lbm = &lb_main; + + for(thread_index = 0; thread_index < tm->n_vlib_mains; thread_index++ ) { + lb_hash_t *h = lbm->per_cpu[thread_index].sticky_ht; + if (h != NULL) { + u32 i; + lb_hash_bucket_t *b; + + lb_hash_foreach_entry(h, b, i) { + vlib_refcount_add(&lbm->as_refcount, thread_index, b->value[i], -1); + vlib_refcount_add(&lbm->as_refcount, thread_index, 0, 1); + } + + lb_hash_free(h); + lbm->per_cpu[thread_index].sticky_ht = 0; + } + } + + return NULL; +} + +/* + * flush all lb flowtables + * This is indented for debug and unit-tests purposes only + */ +VLIB_CLI_COMMAND (lb_flowtable_flush_command, static) = +{ + .path = "test lb flowtable flush", + .short_help = "test lb flowtable flush", + .function = lb_flowtable_flush_command_fn, +};