FD.io VPP  v17.01.1-3-gc6833f8
Vector Packet Processing
cli.c
Go to the documentation of this file.
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 *
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;
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)) {
59  } else {
61  }
62 
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 *
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 
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 *
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 
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 *
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 *
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 };
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:966
bad routing header type(not 4)") sr_error (NO_MORE_SEGMENTS
u32 per_cpu_sticky_buckets
Number of buckets in the per-cpu sticky hash table.
Definition: lb.h:272
static clib_error_t * lb_conf_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:155
#define UNFORMAT_END_OF_INPUT
Definition: format.h:143
#define NULL
Definition: clib.h:55
int lb_conf(ip4_address_t *ip4_address, ip6_address_t *ip6_address, u32 per_cpu_sticky_buckets, u32 flow_timeout)
Fix global load-balancer parameters.
Definition: lb.c:362
int lb_vip_add_ass(u32 vip_index, ip46_address_t *addresses, u32 n)
Definition: lb.c:425
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:482
uword unformat_ip46_prefix(unformat_input_t *input, va_list *args)
Definition: util.c:32
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:348
lb_main_t lb_main
Definition: lb.c:26
u32 flow_timeout
Flow timeout in seconds.
Definition: lb.h:277
Definition: lb.h:228
static void unformat_free(unformat_input_t *i)
Definition: format.h:161
#define clib_warning(format, args...)
Definition: error.h:59
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:977
static clib_error_t * lb_show_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:207
static clib_error_t * lb_vip_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:20
lb_vip_t * vips
Pool of all Virtual IPs.
Definition: lb.h:232
ip4_address_t ip4_src_address
Source address used for IPv4 encapsulated traffic.
Definition: lb.h:267
unformat_function_t unformat_ip4_address
Definition: format.h:76
int lb_vip_del(u32 vip_index)
Definition: lb.c:696
static clib_error_t * lb_as_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:89
format_function_t format_lb_main
Definition: lb.h:331
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:576
unformat_function_t unformat_ip6_address
Definition: format.h:94
unformat_function_t unformat_ip46_address
Definition: format.h:71
#define ip46_prefix_is_ip4(ip46, len)
Definition: util.h:27
int lb_vip_add(ip46_address_t *prefix, u8 plen, lb_vip_type_t type, u32 new_length, u32 *vip_index)
Definition: lb.c:638
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:300
static clib_error_t * lb_show_vips_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:223
int lb_vip_del_ass(u32 vip_index, ip46_address_t *addresses, u32 n)
Definition: lb.c:583
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:154
unsigned int u32
Definition: types.h:88
lb_vip_type_t
The load balancer supports IPv4 and IPv6 traffic and GRE4 and GRE6 encap.
Definition: lb.h:135
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
int lb_vip_find_index(ip46_address_t *prefix, u8 plen, u32 *vip_index)
Definition: lb.c:398
ip6_address_t ip6_src_address
Source address used in IPv6 encapsulated traffic.
Definition: lb.h:262
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
void lb_garbage_collection()
Definition: lb.c:233
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:169
#define clib_error_return(e, args...)
Definition: error.h:111
struct _unformat_input_t unformat_input_t
unformat_function_t unformat_line_input
Definition: format.h:281
Load balancing service is provided per VIP.
Definition: lb.h:154