2 * Copyright (c) 2016 Intel 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 "POD IS" BPODIS,
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.
16 #include <kubeproxy/kp.h>
20 kp_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 ip46_address_t prefix;
34 clib_error_t *error = 0;
36 if (!unformat_user (input, unformat_line_input, line_input))
39 if (!unformat(line_input, "%U", unformat_ip46_prefix, &prefix, &plen, IP46_TYPE_ANY, &plen)) {
40 error = clib_error_return (0, "invalid vip prefix: '%U'",
41 format_unformat_error, line_input);
45 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
47 if (unformat(line_input, "new_len %d", &new_len))
49 else if (unformat(line_input, "port %d", &port))
51 else if (unformat(line_input, "target_port %d", &target_port))
53 else if (unformat(line_input, "node_port %d", &node_port))
55 else if (unformat(line_input, "del"))
57 else if (unformat(line_input, "nat4"))
59 else if (unformat(line_input, "nat6"))
62 error = clib_error_return (0, "parse error: '%U'",
63 format_unformat_error, line_input);
69 if (ip46_prefix_is_ip4(&prefix, plen)) {
70 type = (nat4)?KP_VIP_TYPE_IP4_NAT44:KP_VIP_TYPE_IP4_NAT46;
72 type = (nat4)?KP_VIP_TYPE_IP6_NAT64:KP_VIP_TYPE_IP6_NAT66;
75 kp_garbage_collection();
79 if ((ret = kp_vip_add(&prefix, plen, type, new_len, &index,
80 (u16)port, (u16)target_port, (u16)node_port))) {
81 error = clib_error_return (0, "kp_vip_add error %d", ret);
84 vlib_cli_output(vm, "kp_vip_add ok %d", index);
87 if ((ret = kp_vip_find_index(&prefix, plen, &index))) {
88 error = clib_error_return (0, "kp_vip_find_index error %d", ret);
90 } else if ((ret = kp_vip_del(index))) {
91 error = clib_error_return (0, "kp_vip_del error %d", ret);
97 unformat_free (line_input);
102 VLIB_CLI_COMMAND (kp_vip_command, static) =
104 .path = "kube-proxy vip",
105 .short_help = "kube-proxy vip <prefix> port <n> target_port <n>"
106 " node_port <n> [nat4|nat6)] [new_len <n>] [del]",
107 .function = kp_vip_command_fn,
110 static clib_error_t *
111 kp_pod_command_fn (vlib_main_t * vm,
112 unformat_input_t * input, vlib_cli_command_t * cmd)
114 unformat_input_t _line_input, *line_input = &_line_input;
115 ip46_address_t vip_prefix, pod_addr;
117 ip46_address_t *pod_array = 0;
121 clib_error_t *error = 0;
123 if (!unformat_user (input, unformat_line_input, line_input))
126 if (!unformat(line_input, "%U", unformat_ip46_prefix, &vip_prefix, &vip_plen, IP46_TYPE_ANY)) {
127 error = clib_error_return (0, "invalid pod address: '%U'",
128 format_unformat_error, line_input);
132 if ((ret = kp_vip_find_index(&vip_prefix, vip_plen, &vip_index))) {
133 error = clib_error_return (0, "kp_vip_find_index error %d", ret);
137 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
139 if (unformat(line_input, "%U", unformat_ip46_address, &pod_addr, IP46_TYPE_ANY)) {
140 vec_add1(pod_array, pod_addr);
141 } else if (unformat(line_input, "del")) {
144 error = clib_error_return (0, "parse error: '%U'",
145 format_unformat_error, line_input);
150 if (!vec_len(pod_array)) {
151 error = clib_error_return (0, "No POD address provided");
155 kp_garbage_collection();
156 clib_warning("vip index is %d", vip_index);
159 if ((ret = kp_vip_del_pods(vip_index, pod_array, vec_len(pod_array)))) {
160 error = clib_error_return (0, "kp_vip_del_pods error %d", ret);
164 if ((ret = kp_vip_add_pods(vip_index, pod_array, vec_len(pod_array)))) {
165 error = clib_error_return (0, "kp_vip_add_pods error %d", ret);
171 unformat_free (line_input);
177 VLIB_CLI_COMMAND (kp_pod_command, static) =
179 .path = "kube-proxy pod",
181 "kube-proxy pod <vip-prefix> [<address> [<address> [...]]] [del]",
182 .function = kp_pod_command_fn,
185 static clib_error_t *
186 kp_conf_command_fn (vlib_main_t * vm,
187 unformat_input_t * input, vlib_cli_command_t * cmd)
189 kp_main_t *kpm = &kp_main;
190 unformat_input_t _line_input, *line_input = &_line_input;
191 u32 per_cpu_sticky_buckets = kpm->per_cpu_sticky_buckets;
192 u32 per_cpu_sticky_buckets_log2 = 0;
193 u32 flow_timeout = kpm->flow_timeout;
195 clib_error_t *error = 0;
197 if (!unformat_user (input, unformat_line_input, line_input))
200 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
202 if (unformat(line_input, "buckets %d", &per_cpu_sticky_buckets))
204 else if (unformat(line_input, "buckets-log2 %d", &per_cpu_sticky_buckets_log2)) {
205 if (per_cpu_sticky_buckets_log2 >= 32)
206 return clib_error_return (0, "buckets-log2 value is too high");
207 per_cpu_sticky_buckets = 1 << per_cpu_sticky_buckets_log2;
208 } else if (unformat(line_input, "timeout %d", &flow_timeout))
211 error = clib_error_return (0, "parse error: '%U'",
212 format_unformat_error, line_input);
217 kp_garbage_collection();
219 if ((ret = kp_conf(per_cpu_sticky_buckets, flow_timeout))) {
220 error = clib_error_return (0, "kp_conf error %d", ret);
225 unformat_free (line_input);
230 VLIB_CLI_COMMAND (kp_conf_command, static) =
232 .path = "kube-proxy conf",
233 .short_help = "kube-proxy conf [buckets <n>] [timeout <s>]",
234 .function = kp_conf_command_fn,
237 static clib_error_t *
238 kp_show_command_fn (vlib_main_t * vm,
239 unformat_input_t * input, vlib_cli_command_t * cmd)
241 vlib_cli_output(vm, "%U", format_kp_main);
246 VLIB_CLI_COMMAND (kp_show_command, static) =
248 .path = "show kube-proxy",
249 .short_help = "show kube-proxy",
250 .function = kp_show_command_fn,
253 static clib_error_t *
254 kp_show_vips_command_fn (vlib_main_t * vm,
255 unformat_input_t * input, vlib_cli_command_t * cmd)
257 unformat_input_t line_input;
258 kp_main_t *kpm = &kp_main;
262 if (!unformat_user (input, unformat_line_input, &line_input))
265 if (unformat(&line_input, "verbose"))
268 pool_foreach(vip, kpm->vips, {
269 vlib_cli_output(vm, "%U\n", verbose?format_kp_vip_detailed:format_kp_vip, vip);
272 unformat_free (&line_input);
276 VLIB_CLI_COMMAND (kp_show_vips_command, static) =
278 .path = "show kube-proxy vips",
279 .short_help = "show kube-proxy vips [verbose]",
280 .function = kp_show_vips_command_fn,
283 static clib_error_t *
284 kp_set_interface_nat4_command_fn (vlib_main_t * vm,
285 unformat_input_t * input,
286 vlib_cli_command_t * cmd)
288 unformat_input_t _line_input, *line_input = &_line_input;
289 vnet_main_t * vnm = vnet_get_main();
290 clib_error_t * error = 0;
292 u32 * inside_sw_if_indices = 0;
298 /* Get a line of input. */
299 if (!unformat_user (input, unformat_line_input, line_input))
302 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
304 if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
306 vec_add1 (inside_sw_if_indices, sw_if_index);
307 else if (unformat (line_input, "del"))
311 error = clib_error_return (0, "unknown input '%U'",
312 format_unformat_error, line_input);
317 if (vec_len (inside_sw_if_indices))
319 for (i = 0; i < vec_len(inside_sw_if_indices); i++)
321 sw_if_index = inside_sw_if_indices[i];
323 if (kp_nat4_interface_add_del (sw_if_index, is_del))
325 error = clib_error_return (0, "%s %U failed",
326 is_del ? "del" : "add",
327 format_vnet_sw_interface_name, vnm,
328 vnet_get_sw_interface (vnm,
336 unformat_free (line_input);
337 vec_free (inside_sw_if_indices);
342 VLIB_CLI_COMMAND (kp_set_interface_nat4_command, static) = {
343 .path = "kube-proxy set interface nat4",
344 .function = kp_set_interface_nat4_command_fn,
345 .short_help = "kube-proxy set interface nat4 in <intfc> [del]",
348 static clib_error_t *
349 kp_flowtable_flush_command_fn(vlib_main_t * vm,
350 unformat_input_t * input, vlib_cli_command_t * cmd)
353 vlib_thread_main_t *tm = vlib_get_thread_main();
354 kp_main_t *kpm = &kp_main;
356 for(thread_index = 0; thread_index < tm->n_vlib_mains; thread_index++ ) {
357 kp_hash_t *h = kpm->per_cpu[thread_index].sticky_ht;
361 kp_hash_foreach_entry(h, b, i) {
362 vlib_refcount_add(&kpm->pod_refcount, thread_index, b->value[i], -1);
363 vlib_refcount_add(&kpm->pod_refcount, thread_index, 0, 1);
367 kpm->per_cpu[thread_index].sticky_ht = NULL;
375 * flush all kube-proxy flowtables
376 * This is indented for debug and unit-tests purposes only
378 VLIB_CLI_COMMAND (kp_flowtable_flush_command, static) = {
379 .path = "test kube-proxy flowtable flush",
380 .short_help = "test kube-proxy flowtable flush",
381 .function = kp_flowtable_flush_command_fn,