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