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;
36 if (!unformat_user (input, unformat_line_input, line_input))
39 if (!unformat(line_input, "%U", unformat_ip46_prefix, &(args.prefix),
40 &(args.plen), IP46_TYPE_ANY, &(args.plen))) {
41 error = clib_error_return (0, "invalid vip prefix: '%U'",
42 format_unformat_error, line_input);
46 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
48 if (unformat(line_input, "new_len %d", &(args.new_length)))
50 else if (unformat(line_input, "del"))
52 else if (unformat(line_input, "protocol tcp"))
54 args.protocol = (u8)IP_PROTOCOL_TCP;
56 else if (unformat(line_input, "protocol udp"))
58 args.protocol = (u8)IP_PROTOCOL_UDP;
60 else if (unformat(line_input, "port %d", &port))
62 else if (unformat(line_input, "encap gre4"))
63 encap = LB_ENCAP_TYPE_GRE4;
64 else if (unformat(line_input, "encap gre6"))
65 encap = LB_ENCAP_TYPE_GRE6;
66 else if (unformat(line_input, "encap l3dsr"))
67 encap = LB_ENCAP_TYPE_L3DSR;
68 else if (unformat(line_input, "encap nat4"))
69 encap = LB_ENCAP_TYPE_NAT4;
70 else if (unformat(line_input, "encap nat6"))
71 encap = LB_ENCAP_TYPE_NAT6;
72 else if (unformat(line_input, "dscp %d", &dscp))
74 else if (unformat(line_input, "type clusterip"))
75 srv_type = LB_SRV_TYPE_CLUSTERIP;
76 else if (unformat(line_input, "type nodeport"))
77 srv_type = LB_SRV_TYPE_NODEPORT;
78 else if (unformat(line_input, "target_port %d", &target_port))
81 error = clib_error_return (0, "parse error: '%U'",
82 format_unformat_error, line_input);
87 /* if port == 0, it means all-port VIP */
95 args.port = (u16)port;
98 if ((encap != LB_ENCAP_TYPE_L3DSR) && (dscp != ~0))
100 error = clib_error_return(0, "lb_vip_add error: "
101 "should not configure dscp for none L3DSR.");
105 if ((encap == LB_ENCAP_TYPE_L3DSR) && (dscp >= 64))
107 error = clib_error_return(0, "lb_vip_add error: "
108 "dscp for L3DSR should be less than 64.");
112 if (ip46_prefix_is_ip4(&(args.prefix), (args.plen)))
114 if (encap == LB_ENCAP_TYPE_GRE4)
115 args.type = LB_VIP_TYPE_IP4_GRE4;
116 else if (encap == LB_ENCAP_TYPE_GRE6)
117 args.type = LB_VIP_TYPE_IP4_GRE6;
118 else if (encap == LB_ENCAP_TYPE_L3DSR)
119 args.type = LB_VIP_TYPE_IP4_L3DSR;
120 else if (encap == LB_ENCAP_TYPE_NAT4)
121 args.type = LB_VIP_TYPE_IP4_NAT4;
122 else if (encap == LB_ENCAP_TYPE_NAT6)
124 error = clib_error_return(0, "currently does not support NAT46");
130 if (encap == LB_ENCAP_TYPE_GRE4)
131 args.type = LB_VIP_TYPE_IP6_GRE4;
132 else if (encap == LB_ENCAP_TYPE_GRE6)
133 args.type = LB_VIP_TYPE_IP6_GRE6;
134 else if (encap == LB_ENCAP_TYPE_NAT6)
135 args.type = LB_VIP_TYPE_IP6_NAT6;
136 else if (encap == LB_ENCAP_TYPE_NAT4)
138 error = clib_error_return(0, "currently does not support NAT64");
143 lb_garbage_collection();
147 if (encap == LB_ENCAP_TYPE_L3DSR) {
148 args.encap_args.dscp = (u8)(dscp & 0x3F);
150 else if ((encap == LB_ENCAP_TYPE_NAT4)
151 || (encap == LB_ENCAP_TYPE_NAT6))
153 args.encap_args.srv_type = (u8) srv_type;
154 args.encap_args.target_port = (u16) target_port;
157 if ((ret = lb_vip_add(args, &index))) {
158 error = clib_error_return (0, "lb_vip_add error %d", ret);
161 vlib_cli_output(vm, "lb_vip_add ok %d", index);
164 if ((ret = lb_vip_find_index(&(args.prefix), args.plen,
165 args.protocol, args.port, &index))) {
166 error = clib_error_return (0, "lb_vip_find_index error %d", ret);
168 } else if ((ret = lb_vip_del(index))) {
169 error = clib_error_return (0, "lb_vip_del error %d", ret);
175 unformat_free (line_input);
180 VLIB_CLI_COMMAND (lb_vip_command, static) =
183 .short_help = "lb vip <prefix> "
184 "[protocol (tcp|udp) port <n>] "
185 "[encap (gre6|gre4|l3dsr|nat4|nat6)] "
187 "[type (nodeport|clusterip) target_port <n>] "
188 "[new_len <n>] [del]",
189 .function = lb_vip_command_fn,
192 static clib_error_t *
193 lb_as_command_fn (vlib_main_t * vm,
194 unformat_input_t * input, vlib_cli_command_t * cmd)
196 unformat_input_t _line_input, *line_input = &_line_input;
197 ip46_address_t vip_prefix, as_addr;
199 ip46_address_t *as_array = 0;
205 clib_error_t *error = 0;
207 if (!unformat_user (input, unformat_line_input, line_input))
210 if (!unformat(line_input, "%U", unformat_ip46_prefix,
211 &vip_prefix, &vip_plen, IP46_TYPE_ANY))
213 error = clib_error_return (0, "invalid as address: '%U'",
214 format_unformat_error, line_input);
218 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
220 if (unformat(line_input, "%U", unformat_ip46_address,
221 &as_addr, IP46_TYPE_ANY))
223 vec_add1(as_array, as_addr);
224 } else if (unformat(line_input, "del")) {
227 else if (unformat(line_input, "protocol tcp"))
229 protocol = (u8)IP_PROTOCOL_TCP;
231 else if (unformat(line_input, "protocol udp"))
233 protocol = (u8)IP_PROTOCOL_UDP;
235 else if (unformat(line_input, "port %d", &port))
238 error = clib_error_return (0, "parse error: '%U'",
239 format_unformat_error, line_input);
244 /* If port == 0, it means all-port VIP */
250 if ((ret = lb_vip_find_index(&vip_prefix, vip_plen, protocol,
251 (u16)port, &vip_index))){
252 error = clib_error_return (0, "lb_vip_find_index error %d", ret);
256 if (!vec_len(as_array)) {
257 error = clib_error_return (0, "No AS address provided");
261 lb_garbage_collection();
262 clib_warning("vip index is %d", vip_index);
265 if ((ret = lb_vip_del_ass(vip_index, as_array, vec_len(as_array))))
267 error = clib_error_return (0, "lb_vip_del_ass error %d", ret);
271 if ((ret = lb_vip_add_ass(vip_index, as_array, vec_len(as_array))))
273 error = clib_error_return (0, "lb_vip_add_ass error %d", ret);
279 unformat_free (line_input);
285 VLIB_CLI_COMMAND (lb_as_command, static) =
288 .short_help = "lb as <vip-prefix> [protocol (tcp|udp) port <n>]"
289 " [<address> [<address> [...]]] [del]",
290 .function = lb_as_command_fn,
293 static clib_error_t *
294 lb_conf_command_fn (vlib_main_t * vm,
295 unformat_input_t * input, vlib_cli_command_t * cmd)
297 lb_main_t *lbm = &lb_main;
298 unformat_input_t _line_input, *line_input = &_line_input;
299 ip4_address_t ip4 = lbm->ip4_src_address;
300 ip6_address_t ip6 = lbm->ip6_src_address;
301 u32 per_cpu_sticky_buckets = lbm->per_cpu_sticky_buckets;
302 u32 per_cpu_sticky_buckets_log2 = 0;
303 u32 flow_timeout = lbm->flow_timeout;
305 clib_error_t *error = 0;
307 if (!unformat_user (input, unformat_line_input, line_input))
310 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
312 if (unformat(line_input, "ip4-src-address %U", unformat_ip4_address, &ip4))
314 else if (unformat(line_input, "ip6-src-address %U", unformat_ip6_address, &ip6))
316 else if (unformat(line_input, "buckets %d", &per_cpu_sticky_buckets))
318 else if (unformat(line_input, "buckets-log2 %d", &per_cpu_sticky_buckets_log2)) {
319 if (per_cpu_sticky_buckets_log2 >= 32)
320 return clib_error_return (0, "buckets-log2 value is too high");
321 per_cpu_sticky_buckets = 1 << per_cpu_sticky_buckets_log2;
322 } else if (unformat(line_input, "timeout %d", &flow_timeout))
325 error = clib_error_return (0, "parse error: '%U'",
326 format_unformat_error, line_input);
331 lb_garbage_collection();
333 if ((ret = lb_conf(&ip4, &ip6, per_cpu_sticky_buckets, flow_timeout))) {
334 error = clib_error_return (0, "lb_conf error %d", ret);
339 unformat_free (line_input);
344 VLIB_CLI_COMMAND (lb_conf_command, static) =
347 .short_help = "lb conf [ip4-src-address <addr>] [ip6-src-address <addr>] [buckets <n>] [timeout <s>]",
348 .function = lb_conf_command_fn,
351 static clib_error_t *
352 lb_show_command_fn (vlib_main_t * vm,
353 unformat_input_t * input, vlib_cli_command_t * cmd)
355 vlib_cli_output(vm, "%U", format_lb_main);
360 VLIB_CLI_COMMAND (lb_show_command, static) =
363 .short_help = "show lb",
364 .function = lb_show_command_fn,
367 static clib_error_t *
368 lb_show_vips_command_fn (vlib_main_t * vm,
369 unformat_input_t * input, vlib_cli_command_t * cmd)
371 unformat_input_t line_input;
372 lb_main_t *lbm = &lb_main;
376 if (!unformat_user (input, unformat_line_input, &line_input))
379 if (unformat(&line_input, "verbose"))
383 pool_foreach(vip, lbm->vips, {
384 if (vip != lbm->vips) {
385 vlib_cli_output(vm, "%U\n", verbose?format_lb_vip_detailed:format_lb_vip, vip);
389 unformat_free (&line_input);
393 VLIB_CLI_COMMAND (lb_show_vips_command, static) =
395 .path = "show lb vips",
396 .short_help = "show lb vips [verbose]",
397 .function = lb_show_vips_command_fn,
400 static clib_error_t *
401 lb_set_interface_nat_command_fn (vlib_main_t * vm,
402 unformat_input_t * input,
403 vlib_cli_command_t * cmd,
406 unformat_input_t _line_input, *line_input = &_line_input;
407 vnet_main_t * vnm = vnet_get_main();
408 clib_error_t * error = 0;
409 u32 _sw_if_index, *sw_if_index = &_sw_if_index;
410 u32 * inside_sw_if_indices = 0;
413 /* Get a line of input. */
414 if (!unformat_user (input, unformat_line_input, line_input))
417 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
419 if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
421 vec_add1 (inside_sw_if_indices, *sw_if_index);
422 else if (unformat (line_input, "del"))
426 error = clib_error_return (0, "unknown input '%U'",
427 format_unformat_error, line_input);
432 vec_foreach (sw_if_index, inside_sw_if_indices)
436 if (lb_nat4_interface_add_del (*sw_if_index, is_del))
438 error = clib_error_return(
439 0, "%s %U failed", is_del ? "del" : "add",
440 format_vnet_sw_interface_name, vnm,
441 vnet_get_sw_interface (vnm, *sw_if_index));
447 if (lb_nat6_interface_add_del (*sw_if_index, is_del))
449 error = clib_error_return(
450 0, "%s %U failed", is_del ? "del" : "add",
451 format_vnet_sw_interface_name, vnm,
452 vnet_get_sw_interface (vnm, *sw_if_index));
459 unformat_free (line_input);
460 vec_free (inside_sw_if_indices);
465 static clib_error_t *
466 lb_set_interface_nat4_command_fn (vlib_main_t * vm,
467 unformat_input_t * input,
468 vlib_cli_command_t * cmd)
470 return lb_set_interface_nat_command_fn(vm, input, cmd, 0);
473 VLIB_CLI_COMMAND (lb_set_interface_nat4_command, static) = {
474 .path = "lb set interface nat4",
475 .function = lb_set_interface_nat4_command_fn,
476 .short_help = "lb set interface nat4 in <intfc> [del]",
479 static clib_error_t *
480 lb_set_interface_nat6_command_fn (vlib_main_t * vm,
481 unformat_input_t * input,
482 vlib_cli_command_t * cmd)
484 return lb_set_interface_nat_command_fn(vm, input, cmd, 1);
487 VLIB_CLI_COMMAND (lb_set_interface_nat6_command, static) = {
488 .path = "lb set interface nat6",
489 .function = lb_set_interface_nat6_command_fn,
490 .short_help = "lb set interface nat6 in <intfc> [del]",
493 static clib_error_t *
494 lb_flowtable_flush_command_fn (vlib_main_t * vm,
495 unformat_input_t * input, vlib_cli_command_t * cmd)
498 vlib_thread_main_t *tm = vlib_get_thread_main();
499 lb_main_t *lbm = &lb_main;
501 for(thread_index = 0; thread_index < tm->n_vlib_mains; thread_index++ ) {
502 lb_hash_t *h = lbm->per_cpu[thread_index].sticky_ht;
507 lb_hash_foreach_entry(h, b, i) {
508 vlib_refcount_add(&lbm->as_refcount, thread_index, b->value[i], -1);
509 vlib_refcount_add(&lbm->as_refcount, thread_index, 0, 1);
513 lbm->per_cpu[thread_index].sticky_ht = 0;
521 * flush all lb flowtables
522 * This is indented for debug and unit-tests purposes only
524 VLIB_CLI_COMMAND (lb_flowtable_flush_command, static) =
526 .path = "test lb flowtable flush",
527 .short_help = "test lb flowtable flush",
528 .function = lb_flowtable_flush_command_fn,