FD.io VPP  v16.09
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 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 *
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;
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)) {
100  } else {
102  }
103 
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 *
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 
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 *
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 
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 *
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 *
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 };
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:251
static clib_error_t * lb_conf_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:196
#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:335
int lb_vip_add_ass(u32 vip_index, ip46_address_t *addresses, u32 n)
Definition: lb.c:398
#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:256
Definition: lb.h:207
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:248
static clib_error_t * lb_vip_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:61
lb_vip_t * vips
Pool of all Virtual IPs.
Definition: lb.h:211
ip4_address_t ip4_src_address
Source address used for IPv4 encapsulated traffic.
Definition: lb.h:246
unformat_function_t unformat_ip4_address
Definition: format.h:68
int lb_vip_del(u32 vip_index)
Definition: lb.c:723
static clib_error_t * lb_as_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:130
format_function_t format_lb_main
Definition: lb.h:315
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:575
unformat_function_t unformat_ip6_address
Definition: format.h:86
unformat_function_t unformat_ip46_address
Definition: format.h:63
#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:665
#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:264
int lb_as_lookup_bypass(u32 vip_index, ip46_address_t *address, u8 is_disable)
Updates the adjacency index stored in the AS such that the second IP lookup (after encap) can be bypa...
Definition: lb.c:538
int lb_vip_del_ass(u32 vip_index, ip46_address_t *addresses, u32 n)
Definition: lb.c:530
static clib_error_t * lb_bypass_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:20
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:112
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:371
ip6_address_t ip6_src_address
Source address used in IPv6 encapsulated traffic.
Definition: lb.h:241
VLIB_CLI_COMMAND(set_interface_ip_source_and_port_range_check_command, static)
#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:206
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:131