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