Repair Doxygen build infrastructure
[vpp.git] / plugins / lb-plugin / 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   ip46_address_t prefix;
25   u8 plen;
26   u32 new_len = 1024;
27   u8 del = 0;
28   int ret;
29   u32 gre4 = 0;
30   lb_vip_type_t type;
31
32   if (!unformat_user (input, unformat_line_input, line_input))
33     return 0;
34
35   if (!unformat(line_input, "%U", unformat_ip46_prefix, &prefix, &plen, IP46_TYPE_ANY, &plen))
36     return clib_error_return (0, "invalid vip prefix: '%U'",
37                                   format_unformat_error, line_input);
38
39   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
40   {
41     if (unformat(line_input, "new_len %d", &new_len))
42       ;
43     else if (unformat(line_input, "del"))
44       del = 1;
45     else if (unformat(line_input, "encap gre4"))
46       gre4 = 1;
47     else if (unformat(line_input, "encap gre6"))
48       gre4 = 0;
49     else
50       return clib_error_return (0, "parse error: '%U'",
51                               format_unformat_error, line_input);
52   }
53
54   unformat_free (line_input);
55
56
57   if (ip46_prefix_is_ip4(&prefix, plen)) {
58     type = (gre4)?LB_VIP_TYPE_IP4_GRE4:LB_VIP_TYPE_IP4_GRE6;
59   } else {
60     type = (gre4)?LB_VIP_TYPE_IP6_GRE4:LB_VIP_TYPE_IP6_GRE6;
61   }
62
63   lb_garbage_collection();
64
65   u32 index;
66   if (!del) {
67     if ((ret = lb_vip_add(&prefix, plen, type, new_len, &index))) {
68       return clib_error_return (0, "lb_vip_add error %d", ret);
69     } else {
70       vlib_cli_output(vm, "lb_vip_add ok %d", index);
71     }
72   } else {
73     if ((ret = lb_vip_find_index(&prefix, plen, &index)))
74       return clib_error_return (0, "lb_vip_find_index error %d", ret);
75     else if ((ret = lb_vip_del(index)))
76       return clib_error_return (0, "lb_vip_del error %d", ret);
77   }
78   return NULL;
79 }
80
81 VLIB_CLI_COMMAND (lb_vip_command, static) =
82 {
83   .path = "lb vip",
84   .short_help = "lb vip <prefix> [encap (gre6|gre4)] [new_len <n>] [del]",
85   .function = lb_vip_command_fn,
86 };
87
88 static clib_error_t *
89 lb_as_command_fn (vlib_main_t * vm,
90               unformat_input_t * input, vlib_cli_command_t * cmd)
91 {
92   unformat_input_t _line_input, *line_input = &_line_input;
93   ip46_address_t vip_prefix, as_addr;
94   u8 vip_plen;
95   ip46_address_t *as_array = 0;
96   u32 vip_index;
97   u8 del = 0;
98   int ret;
99
100   if (!unformat_user (input, unformat_line_input, line_input))
101     return 0;
102
103   if (!unformat(line_input, "%U", unformat_ip46_prefix, &vip_prefix, &vip_plen, IP46_TYPE_ANY))
104     return clib_error_return (0, "invalid as address: '%U'",
105                               format_unformat_error, line_input);
106
107   if ((ret = lb_vip_find_index(&vip_prefix, vip_plen, &vip_index)))
108     return clib_error_return (0, "lb_vip_find_index error %d", ret);
109
110   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
111   {
112     if (unformat(line_input, "%U", unformat_ip46_address, &as_addr, IP46_TYPE_ANY)) {
113       vec_add1(as_array, as_addr);
114     } else if (unformat(line_input, "del")) {
115       del = 1;
116     } else {
117       vec_free(as_array);
118       return clib_error_return (0, "parse error: '%U'",
119                                 format_unformat_error, line_input);
120     }
121   }
122
123   if (!vec_len(as_array)) {
124     vec_free(as_array);
125     return clib_error_return (0, "No AS address provided");
126   }
127
128   lb_garbage_collection();
129   clib_warning("vip index is %d", vip_index);
130
131   if (del) {
132     if ((ret = lb_vip_del_ass(vip_index, as_array, vec_len(as_array)))) {
133       vec_free(as_array);
134       return clib_error_return (0, "lb_vip_del_ass error %d", ret);
135     }
136   } else {
137     if ((ret = lb_vip_add_ass(vip_index, as_array, vec_len(as_array)))) {
138       vec_free(as_array);
139       return clib_error_return (0, "lb_vip_add_ass error %d", ret);
140     }
141   }
142
143   vec_free(as_array);
144   return 0;
145 }
146
147 VLIB_CLI_COMMAND (lb_as_command, static) =
148 {
149   .path = "lb as",
150   .short_help = "lb as <vip-prefix> [<address> [<address> [...]]] [del]",
151   .function = lb_as_command_fn,
152 };
153
154 static clib_error_t *
155 lb_conf_command_fn (vlib_main_t * vm,
156               unformat_input_t * input, vlib_cli_command_t * cmd)
157 {
158   lb_main_t *lbm = &lb_main;
159   unformat_input_t _line_input, *line_input = &_line_input;
160   ip4_address_t ip4 = lbm->ip4_src_address;
161   ip6_address_t ip6 = lbm->ip6_src_address;
162   u32 per_cpu_sticky_buckets = lbm->per_cpu_sticky_buckets;
163   u32 per_cpu_sticky_buckets_log2 = 0;
164   u32 flow_timeout = lbm->flow_timeout;
165   int ret;
166
167   if (!unformat_user (input, unformat_line_input, line_input))
168     return 0;
169
170   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
171   {
172     if (unformat(line_input, "ip4-src-address %U", unformat_ip4_address, &ip4))
173       ;
174     else if (unformat(line_input, "ip6-src-address %U", unformat_ip6_address, &ip6))
175       ;
176     else if (unformat(line_input, "buckets %d", &per_cpu_sticky_buckets))
177       ;
178     else if (unformat(line_input, "buckets-log2 %d", &per_cpu_sticky_buckets_log2)) {
179       if (per_cpu_sticky_buckets_log2 >= 32)
180         return clib_error_return (0, "buckets-log2 value is too high");
181       per_cpu_sticky_buckets = 1 << per_cpu_sticky_buckets_log2;
182     } else if (unformat(line_input, "timeout %d", &flow_timeout))
183       ;
184     else
185       return clib_error_return (0, "parse error: '%U'",
186                                 format_unformat_error, line_input);
187   }
188
189   unformat_free (line_input);
190
191   lb_garbage_collection();
192
193   if ((ret = lb_conf(&ip4, &ip6, per_cpu_sticky_buckets, flow_timeout)))
194     return clib_error_return (0, "lb_conf error %d", ret);
195
196   return NULL;
197 }
198
199 VLIB_CLI_COMMAND (lb_conf_command, static) =
200 {
201   .path = "lb conf",
202   .short_help = "lb conf [ip4-src-address <addr>] [ip6-src-address <addr>] [buckets <n>] [timeout <s>]",
203   .function = lb_conf_command_fn,
204 };
205
206 static clib_error_t *
207 lb_show_command_fn (vlib_main_t * vm,
208               unformat_input_t * input, vlib_cli_command_t * cmd)
209 {
210   vlib_cli_output(vm, "%U", format_lb_main);
211   return NULL;
212 }
213
214
215 VLIB_CLI_COMMAND (lb_show_command, static) =
216 {
217   .path = "show lb",
218   .short_help = "show lb",
219   .function = lb_show_command_fn,
220 };
221
222 static clib_error_t *
223 lb_show_vips_command_fn (vlib_main_t * vm,
224               unformat_input_t * input, vlib_cli_command_t * cmd)
225 {
226   unformat_input_t line_input;
227   lb_main_t *lbm = &lb_main;
228   lb_vip_t *vip;
229   u8 verbose = 0;
230
231   if (!unformat_user (input, unformat_line_input, &line_input))
232       return 0;
233
234   if (unformat(&line_input, "verbose"))
235     verbose = 1;
236
237   pool_foreach(vip, lbm->vips, {
238       vlib_cli_output(vm, "%U\n", verbose?format_lb_vip_detailed:format_lb_vip, vip);
239   });
240
241   unformat_free (&line_input);
242   return NULL;
243 }
244
245 VLIB_CLI_COMMAND (lb_show_vips_command, static) =
246 {
247   .path = "show lb vips",
248   .short_help = "show lb vips [verbose]",
249   .function = lb_show_vips_command_fn,
250 };