aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/kubeproxy/kp_cli.c
blob: 43c5c51ae53ab4742f9095c4dec7a7425285b6f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
 * Copyright (c) 2016 Intel and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "POD IS" BPODIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <kubeproxy/kp.h>


static clib_error_t *
kp_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;
  u32 port = 0;
  u32 target_port = 0;
  u32 node_port = 0;
  u32 del = 0;
  int ret;
  u32 nat4 = 0;
  kp_vip_type_t type;
  clib_error_t *error = 0;

  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)) {
    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))
      ;
    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 if (unformat(line_input, "del"))
      del = 1;
    else if (unformat(line_input, "nat4"))
      nat4 = 1;
    else if (unformat(line_input, "nat6"))
      nat4 = 0;
    else {
      error = clib_error_return (0, "parse error: '%U'",
                                format_unformat_error, line_input);
      goto done;
    }
  }


  if (ip46_prefix_is_ip4(&prefix, plen)) {
    type = (nat4)?KP_VIP_TYPE_IP4_NAT44:KP_VIP_TYPE_IP4_NAT46;
  } else {
    type = (nat4)?KP_VIP_TYPE_IP6_NAT64:KP_VIP_TYPE_IP6_NAT66;
  }

  kp_garbage_collection();

  u32 index;
  if (!del) {
    if ((ret = kp_vip_add(&prefix, plen, type, new_len, &index,
			  (u16)port, (u16)target_port, (u16)node_port))) {
      error = clib_error_return (0, "kp_vip_add error %d", ret);
      goto done;
    } else {
      vlib_cli_output(vm, "kp_vip_add ok %d", index);
    }
  } else {
    if ((ret = kp_vip_find_index(&prefix, plen, &index))) {
      error = clib_error_return (0, "kp_vip_find_index error %d", ret);
      goto done;
    } else if ((ret = kp_vip_del(index))) {
      error = clib_error_return (0, "kp_vip_del error %d", ret);
      goto done;
    }
  }

done:
  unformat_free (line_input);

  return error;
}

VLIB_CLI_COMMAND (kp_vip_command, static) =
{
  .path = "kube-proxy vip",
  .short_help = "kube-proxy vip <prefix> port <n> target_port <n>"
                " node_port <n> [nat4|nat6)] [new_len <n>] [del]",
  .function = kp_vip_command_fn,
};

static clib_error_t *
kp_pod_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 vip_prefix, pod_addr;
  u8 vip_plen;
  ip46_address_t *pod_array = 0;
  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)) {
    error = clib_error_return (0, "invalid pod address: '%U'",
                               format_unformat_error, line_input);
    goto done;
  }

  if ((ret = kp_vip_find_index(&vip_prefix, vip_plen, &vip_index))) {
    error = clib_error_return (0, "kp_vip_find_index error %d", ret);
    goto done;
  }

  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
  {
    if (unformat(line_input, "%U", unformat_ip46_address, &pod_addr, IP46_TYPE_ANY)) {
      vec_add1(pod_array, pod_addr);
    } else if (unformat(line_input, "del")) {
      del = 1;
    } else {
      error = clib_error_return (0, "parse error: '%U'",
                                 format_unformat_error, line_input);
      goto done;
    }
  }

  if (!vec_len(pod_array)) {
    error = clib_error_return (0, "No POD address provided");
    goto done;
  }

  kp_garbage_collection();
  clib_warning("vip index is %d", vip_index);

  if (del) {
    if ((ret = kp_vip_del_pods(vip_index, pod_array, vec_len(pod_array)))) {
      error = clib_error_return (0, "kp_vip_del_pods error %d", ret);
      goto done;
    }
  } else {
    if ((ret = kp_vip_add_pods(vip_index, pod_array, vec_len(pod_array)))) {
      error = clib_error_return (0, "kp_vip_add_pods error %d", ret);
      goto done;
    }
  }

done:
  unformat_free (line_input);
  vec_free(pod_array);

  return error;
}

VLIB_CLI_COMMAND (kp_pod_command, static) =
{
  .path = "kube-proxy pod",
  .short_help =
      "kube-proxy pod <vip-prefix> [<address> [<address> [...]]] [del]",
  .function = kp_pod_command_fn,
};

static clib_error_t *
kp_conf_command_fn (vlib_main_t * vm,
              unformat_input_t * input, vlib_cli_command_t * cmd)
{
  kp_main_t *kpm = &kp_main;
  unformat_input_t _line_input, *line_input = &_line_input;
  u32 per_cpu_sticky_buckets = kpm->per_cpu_sticky_buckets;
  u32 per_cpu_sticky_buckets_log2 = 0;
  u32 flow_timeout = kpm->flow_timeout;
  int ret;
  clib_error_t *error = 0;

  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, "buckets %d", &per_cpu_sticky_buckets))
      ;
    else if (unformat(line_input, "buckets-log2 %d", &per_cpu_sticky_buckets_log2)) {
      if (per_cpu_sticky_buckets_log2 >= 32)
        return clib_error_return (0, "buckets-log2 value is too high");
      per_cpu_sticky_buckets = 1 << per_cpu_sticky_buckets_log2;
    } else if (unformat(line_input, "timeout %d", &flow_timeout))
      ;
    else {
      error = clib_error_return (0, "parse error: '%U'",
                                 format_unformat_error, line_input);
      goto done;
    }
  }

  kp_garbage_collection();

  if ((ret = kp_conf(per_cpu_sticky_buckets, flow_timeout))) {
    error = clib_error_return (0, "kp_conf error %d", ret);
    goto done;
  }

done:
  unformat_free (line_input);

  return error;
}

VLIB_CLI_COMMAND (kp_conf_command, static) =
{
  .path = "kube-proxy conf",
  .short_help = "kube-proxy conf [buckets <n>] [timeout <s>]",
  .function = kp_conf_command_fn,
};

static clib_error_t *
kp_show_command_fn (vlib_main_t * vm,
              unformat_input_t * input, vlib_cli_command_t * cmd)
{
  vlib_cli_output(vm, "%U", format_kp_main);
  return NULL;
}


VLIB_CLI_COMMAND (kp_show_command, static) =
{
  .path = "show kube-proxy",
  .short_help = "show kube-proxy",
  .function = kp_show_command_fn,
};

static clib_error_t *
kp_show_vips_command_fn (vlib_main_t * vm,
              unformat_input_t * input, vlib_cli_command_t * cmd)
{
  unformat_input_t line_input;
  kp_main_t *kpm = &kp_main;
  kp_vip_t *vip;
  u8 verbose = 0;

  if (!unformat_user (input, unformat_line_input, &line_input))
      return 0;

  if (unformat(&line_input, "verbose"))
    verbose = 1;

  pool_foreach(vip, kpm->vips, {
      vlib_cli_output(vm, "%U\n", verbose?format_kp_vip_detailed:format_kp_vip, vip);
  });

  unformat_free (&line_input);
  return NULL;
}

VLIB_CLI_COMMAND (kp_show_vips_command, static) =
{
  .path = "show kube-proxy vips",
  .short_help = "show kube-proxy vips [verbose]",
  .function = kp_show_vips_command_fn,
};

static clib_error_t *
kp_set_interface_nat4_command_fn (vlib_main_t * vm,
                                  unformat_input_t * input,
                                  vlib_cli_command_t * cmd)
{
  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;
  u32 * inside_sw_if_indices = 0;
  int is_del = 0;
  int i;

  sw_if_index = ~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;
        }
    }

  if (vec_len (inside_sw_if_indices))
    {
      for (i = 0; i < vec_len(inside_sw_if_indices); i++)
        {
          sw_if_index = inside_sw_if_indices[i];

	  if (kp_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;
	    }
        }
    }

done:
  unformat_free (line_input);
  vec_free (inside_sw_if_indices);

  return error;
}

VLIB_CLI_COMMAND (kp_set_interface_nat4_command, static) = {
  .path = "kube-proxy set interface nat4",
  .function = kp_set_interface_nat4_command_fn,
  .short_help = "kube-proxy set interface nat4 in <intfc> [del]",
};

static clib_error_t *
kp_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();
  kp_main_t *kpm = &kp_main;

  for(thread_index = 0; thread_index < tm->n_vlib_mains; thread_index++ ) {
    kp_hash_t *h = kpm->per_cpu[thread_index].sticky_ht;
    if (h != NULL) {
        kp_hash_bucket_t *b;
        u32 i;
        kp_hash_foreach_entry(h, b, i) {
          vlib_refcount_add(&kpm->pod_refcount, thread_index, b->value[i], -1);
          vlib_refcount_add(&kpm->pod_refcount, thread_index, 0, 1);
        }

        kp_hash_free(h);
        kpm->per_cpu[thread_index].sticky_ht = NULL;
    }
  }

  return NULL;
}

/*
 * flush all kube-proxy flowtables
 * This is indented for debug and unit-tests purposes only
 */
VLIB_CLI_COMMAND (kp_flowtable_flush_command, static) = {
  .path = "test kube-proxy flowtable flush",
  .short_help = "test kube-proxy flowtable flush",
  .function = kp_flowtable_flush_command_fn,
};