lb: add source ip based sticky load balancing
[vpp.git] / src / plugins / lb / cli.c
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 lb_vip_command_fn (vlib_main_t * vm,
21               unformat_input_t * input, vlib_cli_command_t * cmd)
22 {
23   unformat_input_t _line_input, *line_input = &_line_input;
24   lb_vip_add_args_t args;
25   u8 del = 0;
26   int ret;
27   u32 port = 0;
28   u32 encap = 0;
29   u32 dscp = ~0;
30   u32 srv_type = LB_SRV_TYPE_CLUSTERIP;
31   u32 target_port = 0;
32   clib_error_t *error = 0;
33
34   args.new_length = 1024;
35   args.src_ip_sticky = 0;
36
37   if (!unformat_user (input, unformat_line_input, line_input))
38     return 0;
39
40   if (!unformat(line_input, "%U", unformat_ip46_prefix, &(args.prefix),
41                 &(args.plen), IP46_TYPE_ANY, &(args.plen))) {
42     error = clib_error_return (0, "invalid vip prefix: '%U'",
43                                format_unformat_error, line_input);
44     goto done;
45   }
46
47   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
48   {
49     if (unformat(line_input, "new_len %d", &(args.new_length)))
50       ;
51     else if (unformat(line_input, "del"))
52       del = 1;
53     else if (unformat (line_input, "src_ip_sticky"))
54       args.src_ip_sticky = 1;
55     else if (unformat(line_input, "protocol tcp"))
56       {
57         args.protocol = (u8)IP_PROTOCOL_TCP;
58       }
59     else if (unformat(line_input, "protocol udp"))
60       {
61         args.protocol = (u8)IP_PROTOCOL_UDP;
62       }
63     else if (unformat(line_input, "port %d", &port))
64       ;
65     else if (unformat(line_input, "encap gre4"))
66       encap = LB_ENCAP_TYPE_GRE4;
67     else if (unformat(line_input, "encap gre6"))
68       encap = LB_ENCAP_TYPE_GRE6;
69     else if (unformat(line_input, "encap l3dsr"))
70       encap = LB_ENCAP_TYPE_L3DSR;
71     else if (unformat(line_input, "encap nat4"))
72       encap = LB_ENCAP_TYPE_NAT4;
73     else if (unformat(line_input, "encap nat6"))
74       encap = LB_ENCAP_TYPE_NAT6;
75     else if (unformat(line_input, "dscp %d", &dscp))
76       ;
77     else if (unformat(line_input, "type clusterip"))
78       srv_type = LB_SRV_TYPE_CLUSTERIP;
79     else if (unformat(line_input, "type nodeport"))
80       srv_type = LB_SRV_TYPE_NODEPORT;
81     else if (unformat(line_input, "target_port %d", &target_port))
82       ;
83     else {
84       error = clib_error_return (0, "parse error: '%U'",
85                                 format_unformat_error, line_input);
86       goto done;
87     }
88   }
89
90   /* if port == 0, it means all-port VIP */
91   if (port == 0)
92     {
93       args.protocol = ~0;
94       args.port = 0;
95     }
96   else
97     {
98       args.port = (u16)port;
99     }
100
101   if ((encap != LB_ENCAP_TYPE_L3DSR) && (dscp != ~0))
102     {
103       error = clib_error_return(0, "lb_vip_add error: "
104                                 "should not configure dscp for none L3DSR.");
105       goto done;
106     }
107
108   if ((encap == LB_ENCAP_TYPE_L3DSR) && (dscp >= 64))
109     {
110       error = clib_error_return(0, "lb_vip_add error: "
111                                 "dscp for L3DSR should be less than 64.");
112       goto done;
113     }
114
115   if (ip46_prefix_is_ip4(&(args.prefix), (args.plen)))
116     {
117       if (encap == LB_ENCAP_TYPE_GRE4)
118         args.type = LB_VIP_TYPE_IP4_GRE4;
119       else if (encap == LB_ENCAP_TYPE_GRE6)
120         args.type = LB_VIP_TYPE_IP4_GRE6;
121       else if (encap == LB_ENCAP_TYPE_L3DSR)
122         args.type = LB_VIP_TYPE_IP4_L3DSR;
123       else if (encap == LB_ENCAP_TYPE_NAT4)
124         args.type = LB_VIP_TYPE_IP4_NAT4;
125       else if (encap == LB_ENCAP_TYPE_NAT6)
126         {
127           error = clib_error_return(0, "currently does not support NAT46");
128           goto done;
129         }
130     }
131   else
132     {
133       if (encap == LB_ENCAP_TYPE_GRE4)
134         args.type = LB_VIP_TYPE_IP6_GRE4;
135       else if (encap == LB_ENCAP_TYPE_GRE6)
136         args.type = LB_VIP_TYPE_IP6_GRE6;
137       else if (encap == LB_ENCAP_TYPE_NAT6)
138         args.type = LB_VIP_TYPE_IP6_NAT6;
139       else if (encap == LB_ENCAP_TYPE_NAT4)
140         {
141           error = clib_error_return(0, "currently does not support NAT64");
142           goto done;
143         }
144     }
145
146   lb_garbage_collection();
147
148   u32 index;
149   if (!del) {
150     if (encap == LB_ENCAP_TYPE_L3DSR) {
151         args.encap_args.dscp = (u8)(dscp & 0x3F);
152       }
153       else if ((encap == LB_ENCAP_TYPE_NAT4)
154                || (encap == LB_ENCAP_TYPE_NAT6))
155         {
156           args.encap_args.srv_type = (u8) srv_type;
157           args.encap_args.target_port = (u16) target_port;
158         }
159
160     if ((ret = lb_vip_add(args, &index))) {
161       error = clib_error_return (0, "lb_vip_add error %d", ret);
162       goto done;
163     } else {
164       vlib_cli_output(vm, "lb_vip_add ok %d", index);
165     }
166   } else {
167     if ((ret = lb_vip_find_index(&(args.prefix), args.plen,
168                                  args.protocol, args.port, &index))) {
169       error = clib_error_return (0, "lb_vip_find_index error %d", ret);
170       goto done;
171     } else if ((ret = lb_vip_del(index))) {
172       error = clib_error_return (0, "lb_vip_del error %d", ret);
173       goto done;
174     }
175   }
176
177 done:
178   unformat_free (line_input);
179
180   return error;
181 }
182
183 /* clang-format off */
184 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 lb_as_command_fn (vlib_main_t * vm,
199               unformat_input_t * input, vlib_cli_command_t * cmd)
200 {
201   unformat_input_t _line_input, *line_input = &_line_input;
202   ip46_address_t vip_prefix, as_addr;
203   u8 vip_plen;
204   ip46_address_t *as_array = 0;
205   u32 vip_index;
206   u32 port = 0;
207   u8 protocol = 0;
208   u8 del = 0;
209   u8 flush = 0;
210   int ret;
211   clib_error_t *error = 0;
212
213   if (!unformat_user (input, unformat_line_input, line_input))
214     return 0;
215
216   if (!unformat(line_input, "%U", unformat_ip46_prefix,
217                 &vip_prefix, &vip_plen, IP46_TYPE_ANY))
218   {
219     error = clib_error_return (0, "invalid as address: '%U'",
220                                format_unformat_error, line_input);
221     goto done;
222   }
223
224   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
225   {
226     if (unformat(line_input, "%U", unformat_ip46_address,
227                  &as_addr, IP46_TYPE_ANY))
228       {
229         vec_add1(as_array, as_addr);
230       }
231     else if (unformat(line_input, "del"))
232       {
233         del = 1;
234       }
235     else if (unformat(line_input, "flush"))
236       {
237         flush = 1;
238       }
239     else if (unformat(line_input, "protocol tcp"))
240       {
241           protocol = (u8)IP_PROTOCOL_TCP;
242       }
243     else if (unformat(line_input, "protocol udp"))
244       {
245           protocol = (u8)IP_PROTOCOL_UDP;
246       }
247     else if (unformat(line_input, "port %d", &port))
248       ;
249     else {
250       error = clib_error_return (0, "parse error: '%U'",
251                                  format_unformat_error, line_input);
252       goto done;
253     }
254   }
255
256   /* If port == 0, it means all-port VIP */
257   if (port == 0)
258     {
259       protocol = ~0;
260     }
261
262   if ((ret = lb_vip_find_index(&vip_prefix, vip_plen, protocol,
263                                (u16)port, &vip_index))){
264     error = clib_error_return (0, "lb_vip_find_index error %d", ret);
265     goto done;
266   }
267
268   if (!vec_len(as_array)) {
269     error = clib_error_return (0, "No AS address provided");
270     goto done;
271   }
272
273   lb_garbage_collection();
274   clib_warning("vip index is %d", vip_index);
275
276   if (del) {
277     if ((ret = lb_vip_del_ass(vip_index, as_array, vec_len(as_array), flush)))
278     {
279       error = clib_error_return (0, "lb_vip_del_ass error %d", ret);
280       goto done;
281     }
282   } else {
283     if ((ret = lb_vip_add_ass(vip_index, as_array, vec_len(as_array))))
284     {
285       error = clib_error_return (0, "lb_vip_add_ass error %d", ret);
286       goto done;
287     }
288   }
289
290 done:
291   unformat_free (line_input);
292   vec_free(as_array);
293
294   return error;
295 }
296
297 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 lb_conf_command_fn (vlib_main_t * vm,
307               unformat_input_t * input, vlib_cli_command_t * cmd)
308 {
309   lb_main_t *lbm = &lb_main;
310   unformat_input_t _line_input, *line_input = &_line_input;
311   ip4_address_t ip4 = lbm->ip4_src_address;
312   ip6_address_t ip6 = lbm->ip6_src_address;
313   u32 per_cpu_sticky_buckets = lbm->per_cpu_sticky_buckets;
314   u32 per_cpu_sticky_buckets_log2 = 0;
315   u32 flow_timeout = lbm->flow_timeout;
316   int ret;
317   clib_error_t *error = 0;
318
319   if (!unformat_user (input, unformat_line_input, line_input))
320     return 0;
321
322   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
323   {
324     if (unformat(line_input, "ip4-src-address %U", unformat_ip4_address, &ip4))
325       ;
326     else if (unformat(line_input, "ip6-src-address %U", unformat_ip6_address, &ip6))
327       ;
328     else if (unformat(line_input, "buckets %d", &per_cpu_sticky_buckets))
329       ;
330     else if (unformat(line_input, "buckets-log2 %d", &per_cpu_sticky_buckets_log2)) {
331       if (per_cpu_sticky_buckets_log2 >= 32)
332         return clib_error_return (0, "buckets-log2 value is too high");
333       per_cpu_sticky_buckets = 1 << per_cpu_sticky_buckets_log2;
334     } else if (unformat(line_input, "timeout %d", &flow_timeout))
335       ;
336     else {
337       error = clib_error_return (0, "parse error: '%U'",
338                                  format_unformat_error, line_input);
339       goto done;
340     }
341   }
342
343   lb_garbage_collection();
344
345   if ((ret = lb_conf(&ip4, &ip6, per_cpu_sticky_buckets, flow_timeout))) {
346     error = clib_error_return (0, "lb_conf error %d", ret);
347     goto done;
348   }
349
350 done:
351   unformat_free (line_input);
352
353   return error;
354 }
355
356 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 lb_show_command_fn (vlib_main_t * vm,
365               unformat_input_t * input, vlib_cli_command_t * cmd)
366 {
367   vlib_cli_output(vm, "%U", format_lb_main);
368   return NULL;
369 }
370
371
372 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 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   lb_main_t *lbm = &lb_main;
385   lb_vip_t *vip;
386   u8 verbose = 0;
387
388   if (!unformat_user (input, unformat_line_input, &line_input))
389       return 0;
390
391   if (unformat(&line_input, "verbose"))
392     verbose = 1;
393
394   /* Hide placeholder VIP */
395   pool_foreach (vip, lbm->vips) {
396     if (vip != lbm->vips) {
397       vlib_cli_output(vm, "%U\n", verbose?format_lb_vip_detailed:format_lb_vip, vip);
398     }
399   }
400
401   unformat_free (&line_input);
402   return NULL;
403 }
404
405 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 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   unformat_input_t _line_input, *line_input = &_line_input;
419   vnet_main_t * vnm = vnet_get_main();
420   clib_error_t * error = 0;
421   u32 _sw_if_index, *sw_if_index = &_sw_if_index;
422   u32 * inside_sw_if_indices = 0;
423   int is_del = 0;
424
425   /* Get a line of input. */
426   if (!unformat_user (input, unformat_line_input, line_input))
427     return 0;
428
429   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
430     {
431       if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
432                     vnm, sw_if_index))
433         vec_add1 (inside_sw_if_indices, *sw_if_index);
434       else if (unformat (line_input, "del"))
435         is_del = 1;
436       else
437         {
438           error = clib_error_return (0, "unknown input '%U'",
439             format_unformat_error, line_input);
440           goto done;
441         }
442     }
443
444     vec_foreach (sw_if_index, inside_sw_if_indices)
445     {
446       if (!is_nat6)
447         {
448           if (lb_nat4_interface_add_del (*sw_if_index, is_del))
449             {
450               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               goto done;
455             }
456         }
457       else
458         {
459           if (lb_nat6_interface_add_del (*sw_if_index, is_del))
460             {
461               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               goto done;
466             }
467         }
468     }
469
470 done:
471   unformat_free (line_input);
472   vec_free (inside_sw_if_indices);
473
474   return error;
475 }
476
477 static clib_error_t *
478 lb_set_interface_nat4_command_fn (vlib_main_t * vm,
479                                   unformat_input_t * input,
480                                   vlib_cli_command_t * cmd)
481 {
482   return lb_set_interface_nat_command_fn(vm, input, cmd, 0);
483 }
484
485 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 lb_set_interface_nat6_command_fn (vlib_main_t * vm,
493                                   unformat_input_t * input,
494                                   vlib_cli_command_t * cmd)
495 {
496   return lb_set_interface_nat_command_fn(vm, input, cmd, 1);
497 }
498
499 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 lb_flowtable_flush_command_fn (vlib_main_t * vm,
507               unformat_input_t * input, vlib_cli_command_t * cmd)
508 {
509   lb_flush_vip_as(~0, 0);
510
511   return NULL;
512 }
513
514 static clib_error_t *
515 lb_flush_vip_command_fn (vlib_main_t * vm,
516                          unformat_input_t * input,
517                          vlib_cli_command_t * cmd)
518 {
519   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   u8 protocol = 0;
525   u32 port = 0;
526   clib_error_t *error = 0;
527
528   if (!unformat_user (input, unformat_line_input, line_input))
529     return 0;
530
531   if (!unformat(line_input, "%U", unformat_ip46_prefix, &vip_prefix,
532                 &vip_plen, IP46_TYPE_ANY, &vip_plen)) {
533     error = clib_error_return (0, "invalid vip prefix: '%U'",
534                                format_unformat_error, line_input);
535     goto done;
536   }
537
538   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
539   {
540     if (unformat(line_input, "protocol tcp"))
541       {
542         protocol = (u8)IP_PROTOCOL_TCP;
543       }
544     else if (unformat(line_input, "protocol udp"))
545       {
546         protocol = (u8)IP_PROTOCOL_UDP;
547       }
548     else if (unformat(line_input, "port %d", &port))
549       ;
550   }
551
552   if (port == 0)
553     {
554       protocol = ~0;
555     }
556
557   if ((ret = lb_vip_find_index(&vip_prefix, vip_plen, protocol,
558                                (u16)port, &vip_index))){
559     error = clib_error_return (0, "lb_vip_find_index error %d", ret);
560     goto done;
561   }
562
563   if ((ret = lb_flush_vip_as(vip_index, ~0)))
564     {
565       error = clib_error_return (0, "lb_flush_vip error %d", ret);
566     }
567   else
568     {
569         vlib_cli_output(vm, "lb_flush_vip ok %d", vip_index);
570     }
571
572 done:
573   unformat_free (line_input);
574
575   return error;
576 }
577
578 /*
579  * flush lb flowtable as per vip
580  */
581 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 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 };