Line data Source code
1 : /*
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:
6 : *
7 : * http://www.apache.org/licenses/LICENSE-2.0
8 : *
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.
14 : */
15 :
16 : #include <lb/lb.h>
17 : #include <lb/util.h>
18 :
19 : static clib_error_t *
20 29 : lb_vip_command_fn (vlib_main_t * vm,
21 : unformat_input_t * input, vlib_cli_command_t * cmd)
22 : {
23 29 : unformat_input_t _line_input, *line_input = &_line_input;
24 : lb_vip_add_args_t args;
25 29 : u8 del = 0;
26 : int ret;
27 29 : u32 port = 0;
28 29 : u32 encap = 0;
29 29 : u32 dscp = ~0;
30 29 : u32 srv_type = LB_SRV_TYPE_CLUSTERIP;
31 29 : u32 target_port = 0;
32 29 : clib_error_t *error = 0;
33 :
34 29 : args.new_length = 1024;
35 29 : args.src_ip_sticky = 0;
36 :
37 29 : if (!unformat_user (input, unformat_line_input, line_input))
38 0 : return 0;
39 :
40 29 : if (!unformat(line_input, "%U", unformat_ip46_prefix, &(args.prefix),
41 : &(args.plen), IP46_TYPE_ANY, &(args.plen))) {
42 0 : error = clib_error_return (0, "invalid vip prefix: '%U'",
43 : format_unformat_error, line_input);
44 0 : goto done;
45 : }
46 :
47 119 : while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
48 : {
49 90 : if (unformat(line_input, "new_len %d", &(args.new_length)))
50 : ;
51 90 : else if (unformat(line_input, "del"))
52 14 : del = 1;
53 76 : else if (unformat (line_input, "src_ip_sticky"))
54 2 : args.src_ip_sticky = 1;
55 74 : else if (unformat(line_input, "protocol tcp"))
56 : {
57 0 : args.protocol = (u8)IP_PROTOCOL_TCP;
58 : }
59 74 : else if (unformat(line_input, "protocol udp"))
60 : {
61 16 : args.protocol = (u8)IP_PROTOCOL_UDP;
62 : }
63 58 : else if (unformat(line_input, "port %d", &port))
64 : ;
65 42 : else if (unformat(line_input, "encap gre4"))
66 8 : encap = LB_ENCAP_TYPE_GRE4;
67 34 : else if (unformat(line_input, "encap gre6"))
68 10 : encap = LB_ENCAP_TYPE_GRE6;
69 24 : else if (unformat(line_input, "encap l3dsr"))
70 6 : encap = LB_ENCAP_TYPE_L3DSR;
71 18 : else if (unformat(line_input, "encap nat4"))
72 2 : encap = LB_ENCAP_TYPE_NAT4;
73 16 : else if (unformat(line_input, "encap nat6"))
74 2 : encap = LB_ENCAP_TYPE_NAT6;
75 14 : else if (unformat(line_input, "dscp %d", &dscp))
76 : ;
77 8 : else if (unformat(line_input, "type clusterip"))
78 4 : srv_type = LB_SRV_TYPE_CLUSTERIP;
79 4 : else if (unformat(line_input, "type nodeport"))
80 0 : srv_type = LB_SRV_TYPE_NODEPORT;
81 4 : else if (unformat(line_input, "target_port %d", &target_port))
82 : ;
83 : else {
84 0 : error = clib_error_return (0, "parse error: '%U'",
85 : format_unformat_error, line_input);
86 0 : goto done;
87 : }
88 : }
89 :
90 : /* if port == 0, it means all-port VIP */
91 29 : if (port == 0)
92 : {
93 13 : args.protocol = ~0;
94 13 : args.port = 0;
95 : }
96 : else
97 : {
98 16 : args.port = (u16)port;
99 : }
100 :
101 29 : if ((encap != LB_ENCAP_TYPE_L3DSR) && (dscp != ~0))
102 : {
103 0 : error = clib_error_return(0, "lb_vip_add error: "
104 : "should not configure dscp for none L3DSR.");
105 0 : goto done;
106 : }
107 :
108 29 : if ((encap == LB_ENCAP_TYPE_L3DSR) && (dscp >= 64))
109 : {
110 0 : error = clib_error_return(0, "lb_vip_add error: "
111 : "dscp for L3DSR should be less than 64.");
112 0 : goto done;
113 : }
114 :
115 29 : if (ip46_prefix_is_ip4(&(args.prefix), (args.plen)))
116 : {
117 16 : if (encap == LB_ENCAP_TYPE_GRE4)
118 4 : args.type = LB_VIP_TYPE_IP4_GRE4;
119 12 : else if (encap == LB_ENCAP_TYPE_GRE6)
120 4 : args.type = LB_VIP_TYPE_IP4_GRE6;
121 8 : else if (encap == LB_ENCAP_TYPE_L3DSR)
122 6 : args.type = LB_VIP_TYPE_IP4_L3DSR;
123 2 : else if (encap == LB_ENCAP_TYPE_NAT4)
124 2 : args.type = LB_VIP_TYPE_IP4_NAT4;
125 0 : else if (encap == LB_ENCAP_TYPE_NAT6)
126 : {
127 0 : error = clib_error_return(0, "currently does not support NAT46");
128 0 : goto done;
129 : }
130 : }
131 : else
132 : {
133 13 : if (encap == LB_ENCAP_TYPE_GRE4)
134 5 : args.type = LB_VIP_TYPE_IP6_GRE4;
135 8 : else if (encap == LB_ENCAP_TYPE_GRE6)
136 6 : args.type = LB_VIP_TYPE_IP6_GRE6;
137 2 : else if (encap == LB_ENCAP_TYPE_NAT6)
138 2 : args.type = LB_VIP_TYPE_IP6_NAT6;
139 0 : else if (encap == LB_ENCAP_TYPE_NAT4)
140 : {
141 0 : error = clib_error_return(0, "currently does not support NAT64");
142 0 : goto done;
143 : }
144 : }
145 :
146 29 : lb_garbage_collection();
147 :
148 : u32 index;
149 29 : if (!del) {
150 15 : if (encap == LB_ENCAP_TYPE_L3DSR) {
151 3 : args.encap_args.dscp = (u8)(dscp & 0x3F);
152 : }
153 12 : else if ((encap == LB_ENCAP_TYPE_NAT4)
154 11 : || (encap == LB_ENCAP_TYPE_NAT6))
155 : {
156 2 : args.encap_args.srv_type = (u8) srv_type;
157 2 : args.encap_args.target_port = (u16) target_port;
158 : }
159 :
160 15 : if ((ret = lb_vip_add(args, &index))) {
161 0 : error = clib_error_return (0, "lb_vip_add error %d", ret);
162 0 : goto done;
163 : } else {
164 15 : vlib_cli_output(vm, "lb_vip_add ok %d", index);
165 : }
166 : } else {
167 14 : if ((ret = lb_vip_find_index(&(args.prefix), args.plen,
168 14 : args.protocol, args.port, &index))) {
169 0 : error = clib_error_return (0, "lb_vip_find_index error %d", ret);
170 0 : goto done;
171 14 : } else if ((ret = lb_vip_del(index))) {
172 0 : error = clib_error_return (0, "lb_vip_del error %d", ret);
173 0 : goto done;
174 : }
175 : }
176 :
177 14 : done:
178 29 : unformat_free (line_input);
179 :
180 29 : return error;
181 : }
182 :
183 : /* clang-format off */
184 164807 : VLIB_CLI_COMMAND (lb_vip_command, static) =
185 : {
186 : .path = "lb vip",
187 : .short_help = "lb vip <prefix> "
188 : "[protocol (tcp|udp) port <n>] "
189 : "[encap (gre6|gre4|l3dsr|nat4|nat6)] "
190 : "[dscp <n>] "
191 : "[type (nodeport|clusterip) target_port <n>] "
192 : "[new_len <n>] [src_ip_sticky] [del]",
193 : .function = lb_vip_command_fn,
194 : };
195 : /* clang-format on */
196 :
197 : static clib_error_t *
198 131 : lb_as_command_fn (vlib_main_t * vm,
199 : unformat_input_t * input, vlib_cli_command_t * cmd)
200 : {
201 131 : unformat_input_t _line_input, *line_input = &_line_input;
202 : ip46_address_t vip_prefix, as_addr;
203 : u8 vip_plen;
204 131 : ip46_address_t *as_array = 0;
205 : u32 vip_index;
206 131 : u32 port = 0;
207 131 : u8 protocol = 0;
208 131 : u8 del = 0;
209 131 : u8 flush = 0;
210 : int ret;
211 131 : clib_error_t *error = 0;
212 :
213 131 : if (!unformat_user (input, unformat_line_input, line_input))
214 0 : return 0;
215 :
216 131 : if (!unformat(line_input, "%U", unformat_ip46_prefix,
217 : &vip_prefix, &vip_plen, IP46_TYPE_ANY))
218 : {
219 0 : error = clib_error_return (0, "invalid as address: '%U'",
220 : format_unformat_error, line_input);
221 0 : goto done;
222 : }
223 :
224 487 : while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
225 : {
226 356 : if (unformat(line_input, "%U", unformat_ip46_address,
227 : &as_addr, IP46_TYPE_ANY))
228 : {
229 131 : vec_add1(as_array, as_addr);
230 : }
231 225 : else if (unformat(line_input, "del"))
232 : {
233 65 : del = 1;
234 : }
235 160 : else if (unformat(line_input, "flush"))
236 : {
237 0 : flush = 1;
238 : }
239 160 : else if (unformat(line_input, "protocol tcp"))
240 : {
241 0 : protocol = (u8)IP_PROTOCOL_TCP;
242 : }
243 160 : else if (unformat(line_input, "protocol udp"))
244 : {
245 80 : protocol = (u8)IP_PROTOCOL_UDP;
246 : }
247 80 : else if (unformat(line_input, "port %d", &port))
248 : ;
249 : else {
250 0 : error = clib_error_return (0, "parse error: '%U'",
251 : format_unformat_error, line_input);
252 0 : goto done;
253 : }
254 : }
255 :
256 : /* If port == 0, it means all-port VIP */
257 131 : if (port == 0)
258 : {
259 51 : protocol = ~0;
260 : }
261 :
262 131 : if ((ret = lb_vip_find_index(&vip_prefix, vip_plen, protocol,
263 131 : (u16)port, &vip_index))){
264 0 : error = clib_error_return (0, "lb_vip_find_index error %d", ret);
265 0 : goto done;
266 : }
267 :
268 131 : if (!vec_len(as_array)) {
269 0 : error = clib_error_return (0, "No AS address provided");
270 0 : goto done;
271 : }
272 :
273 131 : lb_garbage_collection();
274 131 : clib_warning("vip index is %d", vip_index);
275 :
276 131 : if (del) {
277 65 : if ((ret = lb_vip_del_ass(vip_index, as_array, vec_len(as_array), flush)))
278 : {
279 0 : error = clib_error_return (0, "lb_vip_del_ass error %d", ret);
280 0 : goto done;
281 : }
282 : } else {
283 66 : if ((ret = lb_vip_add_ass(vip_index, as_array, vec_len(as_array))))
284 : {
285 0 : error = clib_error_return (0, "lb_vip_add_ass error %d", ret);
286 0 : goto done;
287 : }
288 : }
289 :
290 66 : done:
291 131 : unformat_free (line_input);
292 131 : vec_free(as_array);
293 :
294 131 : return error;
295 : }
296 :
297 164807 : VLIB_CLI_COMMAND (lb_as_command, static) =
298 : {
299 : .path = "lb as",
300 : .short_help = "lb as <vip-prefix> [protocol (tcp|udp) port <n>]"
301 : " [<address> [<address> [...]]] [del] [flush]",
302 : .function = lb_as_command_fn,
303 : };
304 :
305 : static clib_error_t *
306 0 : lb_conf_command_fn (vlib_main_t * vm,
307 : unformat_input_t * input, vlib_cli_command_t * cmd)
308 : {
309 0 : lb_main_t *lbm = &lb_main;
310 0 : unformat_input_t _line_input, *line_input = &_line_input;
311 0 : ip4_address_t ip4 = lbm->ip4_src_address;
312 0 : ip6_address_t ip6 = lbm->ip6_src_address;
313 0 : u32 per_cpu_sticky_buckets = lbm->per_cpu_sticky_buckets;
314 0 : u32 per_cpu_sticky_buckets_log2 = 0;
315 0 : u32 flow_timeout = lbm->flow_timeout;
316 : int ret;
317 0 : clib_error_t *error = 0;
318 :
319 0 : if (!unformat_user (input, unformat_line_input, line_input))
320 0 : return 0;
321 :
322 0 : while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
323 : {
324 0 : if (unformat(line_input, "ip4-src-address %U", unformat_ip4_address, &ip4))
325 : ;
326 0 : else if (unformat(line_input, "ip6-src-address %U", unformat_ip6_address, &ip6))
327 : ;
328 0 : else if (unformat(line_input, "buckets %d", &per_cpu_sticky_buckets))
329 : ;
330 0 : else if (unformat(line_input, "buckets-log2 %d", &per_cpu_sticky_buckets_log2)) {
331 0 : if (per_cpu_sticky_buckets_log2 >= 32)
332 0 : return clib_error_return (0, "buckets-log2 value is too high");
333 0 : per_cpu_sticky_buckets = 1 << per_cpu_sticky_buckets_log2;
334 0 : } else if (unformat(line_input, "timeout %d", &flow_timeout))
335 : ;
336 : else {
337 0 : error = clib_error_return (0, "parse error: '%U'",
338 : format_unformat_error, line_input);
339 0 : goto done;
340 : }
341 : }
342 :
343 0 : lb_garbage_collection();
344 :
345 0 : if ((ret = lb_conf(&ip4, &ip6, per_cpu_sticky_buckets, flow_timeout))) {
346 0 : error = clib_error_return (0, "lb_conf error %d", ret);
347 0 : goto done;
348 : }
349 :
350 0 : done:
351 0 : unformat_free (line_input);
352 :
353 0 : return error;
354 : }
355 :
356 164807 : VLIB_CLI_COMMAND (lb_conf_command, static) =
357 : {
358 : .path = "lb conf",
359 : .short_help = "lb conf [ip4-src-address <addr>] [ip6-src-address <addr>] [buckets <n>] [timeout <s>]",
360 : .function = lb_conf_command_fn,
361 : };
362 :
363 : static clib_error_t *
364 0 : lb_show_command_fn (vlib_main_t * vm,
365 : unformat_input_t * input, vlib_cli_command_t * cmd)
366 : {
367 0 : vlib_cli_output(vm, "%U", format_lb_main);
368 0 : return NULL;
369 : }
370 :
371 :
372 164807 : VLIB_CLI_COMMAND (lb_show_command, static) =
373 : {
374 : .path = "show lb",
375 : .short_help = "show lb",
376 : .function = lb_show_command_fn,
377 : };
378 :
379 : static clib_error_t *
380 13 : lb_show_vips_command_fn (vlib_main_t * vm,
381 : unformat_input_t * input, vlib_cli_command_t * cmd)
382 : {
383 : unformat_input_t line_input;
384 13 : lb_main_t *lbm = &lb_main;
385 : lb_vip_t *vip;
386 13 : u8 verbose = 0;
387 :
388 13 : if (!unformat_user (input, unformat_line_input, &line_input))
389 0 : return 0;
390 :
391 13 : if (unformat(&line_input, "verbose"))
392 13 : verbose = 1;
393 :
394 : /* Hide placeholder VIP */
395 117 : pool_foreach (vip, lbm->vips) {
396 104 : if (vip != lbm->vips) {
397 91 : vlib_cli_output(vm, "%U\n", verbose?format_lb_vip_detailed:format_lb_vip, vip);
398 : }
399 : }
400 :
401 13 : unformat_free (&line_input);
402 13 : return NULL;
403 : }
404 :
405 164807 : VLIB_CLI_COMMAND (lb_show_vips_command, static) =
406 : {
407 : .path = "show lb vips",
408 : .short_help = "show lb vips [verbose]",
409 : .function = lb_show_vips_command_fn,
410 : };
411 :
412 : static clib_error_t *
413 0 : lb_set_interface_nat_command_fn (vlib_main_t * vm,
414 : unformat_input_t * input,
415 : vlib_cli_command_t * cmd,
416 : u8 is_nat6)
417 : {
418 0 : unformat_input_t _line_input, *line_input = &_line_input;
419 0 : vnet_main_t * vnm = vnet_get_main();
420 0 : clib_error_t * error = 0;
421 0 : u32 _sw_if_index, *sw_if_index = &_sw_if_index;
422 0 : u32 * inside_sw_if_indices = 0;
423 0 : int is_del = 0;
424 :
425 : /* Get a line of input. */
426 0 : if (!unformat_user (input, unformat_line_input, line_input))
427 0 : return 0;
428 :
429 0 : while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
430 : {
431 0 : if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
432 : vnm, sw_if_index))
433 0 : vec_add1 (inside_sw_if_indices, *sw_if_index);
434 0 : else if (unformat (line_input, "del"))
435 0 : is_del = 1;
436 : else
437 : {
438 0 : error = clib_error_return (0, "unknown input '%U'",
439 : format_unformat_error, line_input);
440 0 : goto done;
441 : }
442 : }
443 :
444 0 : vec_foreach (sw_if_index, inside_sw_if_indices)
445 : {
446 0 : if (!is_nat6)
447 : {
448 0 : if (lb_nat4_interface_add_del (*sw_if_index, is_del))
449 : {
450 0 : error = clib_error_return(
451 : 0, "%s %U failed", is_del ? "del" : "add",
452 : format_vnet_sw_interface_name, vnm,
453 : vnet_get_sw_interface (vnm, *sw_if_index));
454 0 : goto done;
455 : }
456 : }
457 : else
458 : {
459 0 : if (lb_nat6_interface_add_del (*sw_if_index, is_del))
460 : {
461 0 : error = clib_error_return(
462 : 0, "%s %U failed", is_del ? "del" : "add",
463 : format_vnet_sw_interface_name, vnm,
464 : vnet_get_sw_interface (vnm, *sw_if_index));
465 0 : goto done;
466 : }
467 : }
468 : }
469 :
470 0 : done:
471 0 : unformat_free (line_input);
472 0 : vec_free (inside_sw_if_indices);
473 :
474 0 : return error;
475 : }
476 :
477 : static clib_error_t *
478 0 : lb_set_interface_nat4_command_fn (vlib_main_t * vm,
479 : unformat_input_t * input,
480 : vlib_cli_command_t * cmd)
481 : {
482 0 : return lb_set_interface_nat_command_fn(vm, input, cmd, 0);
483 : }
484 :
485 164807 : VLIB_CLI_COMMAND (lb_set_interface_nat4_command, static) = {
486 : .path = "lb set interface nat4",
487 : .function = lb_set_interface_nat4_command_fn,
488 : .short_help = "lb set interface nat4 in <intfc> [del]",
489 : };
490 :
491 : static clib_error_t *
492 0 : lb_set_interface_nat6_command_fn (vlib_main_t * vm,
493 : unformat_input_t * input,
494 : vlib_cli_command_t * cmd)
495 : {
496 0 : return lb_set_interface_nat_command_fn(vm, input, cmd, 1);
497 : }
498 :
499 164807 : VLIB_CLI_COMMAND (lb_set_interface_nat6_command, static) = {
500 : .path = "lb set interface nat6",
501 : .function = lb_set_interface_nat6_command_fn,
502 : .short_help = "lb set interface nat6 in <intfc> [del]",
503 : };
504 :
505 : static clib_error_t *
506 13 : lb_flowtable_flush_command_fn (vlib_main_t * vm,
507 : unformat_input_t * input, vlib_cli_command_t * cmd)
508 : {
509 13 : lb_flush_vip_as(~0, 0);
510 :
511 13 : return NULL;
512 : }
513 :
514 : static clib_error_t *
515 0 : lb_flush_vip_command_fn (vlib_main_t * vm,
516 : unformat_input_t * input,
517 : vlib_cli_command_t * cmd)
518 : {
519 0 : unformat_input_t _line_input, *line_input = &_line_input;
520 : int ret;
521 : ip46_address_t vip_prefix;
522 : u8 vip_plen;
523 : u32 vip_index;
524 0 : u8 protocol = 0;
525 0 : u32 port = 0;
526 0 : clib_error_t *error = 0;
527 :
528 0 : if (!unformat_user (input, unformat_line_input, line_input))
529 0 : return 0;
530 :
531 0 : if (!unformat(line_input, "%U", unformat_ip46_prefix, &vip_prefix,
532 : &vip_plen, IP46_TYPE_ANY, &vip_plen)) {
533 0 : error = clib_error_return (0, "invalid vip prefix: '%U'",
534 : format_unformat_error, line_input);
535 0 : goto done;
536 : }
537 :
538 0 : while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
539 : {
540 0 : if (unformat(line_input, "protocol tcp"))
541 : {
542 0 : protocol = (u8)IP_PROTOCOL_TCP;
543 : }
544 0 : else if (unformat(line_input, "protocol udp"))
545 : {
546 0 : protocol = (u8)IP_PROTOCOL_UDP;
547 : }
548 0 : else if (unformat(line_input, "port %d", &port))
549 : ;
550 : }
551 :
552 0 : if (port == 0)
553 : {
554 0 : protocol = ~0;
555 : }
556 :
557 0 : if ((ret = lb_vip_find_index(&vip_prefix, vip_plen, protocol,
558 0 : (u16)port, &vip_index))){
559 0 : error = clib_error_return (0, "lb_vip_find_index error %d", ret);
560 0 : goto done;
561 : }
562 :
563 0 : if ((ret = lb_flush_vip_as(vip_index, ~0)))
564 : {
565 0 : error = clib_error_return (0, "lb_flush_vip error %d", ret);
566 : }
567 : else
568 : {
569 0 : vlib_cli_output(vm, "lb_flush_vip ok %d", vip_index);
570 : }
571 :
572 0 : done:
573 0 : unformat_free (line_input);
574 :
575 0 : return error;
576 : }
577 :
578 : /*
579 : * flush lb flowtable as per vip
580 : */
581 164807 : VLIB_CLI_COMMAND (lb_flush_vip_command, static) =
582 : {
583 : .path = "lb flush vip",
584 : .short_help = "lb flush vip <prefix> "
585 : "[protocol (tcp|udp) port <n>]",
586 : .function = lb_flush_vip_command_fn,
587 : };
588 :
589 : /*
590 : * flush all lb flowtables
591 : * This is indented for debug and unit-tests purposes only
592 : */
593 164807 : VLIB_CLI_COMMAND (lb_flowtable_flush_command, static) =
594 : {
595 : .path = "test lb flowtable flush",
596 : .short_help = "test lb flowtable flush",
597 : .function = lb_flowtable_flush_command_fn,
598 : };
|