FD.io VPP  v21.10.1-2-g0a485f517
Vector Packet Processing
cli.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright(c) 2021 Cisco Systems, Inc.
3  */
4 
5 #include <vlib/vlib.h>
6 #include <vnet/vnet.h>
7 #include <snort/snort.h>
8 
9 static u8 *
10 format_snort_instance (u8 *s, va_list *args)
11 {
12  snort_instance_t *i = va_arg (*args, snort_instance_t *);
13  s = format (s, "%s [idx:%d sz:%d fd:%d]", i->name, i->index, i->shm_size,
14  i->shm_fd);
15 
16  return s;
17 }
18 
19 static clib_error_t *
21  vlib_cli_command_t *cmd)
22 {
23  unformat_input_t _line_input, *line_input = &_line_input;
24  clib_error_t *err = 0;
25  u8 *name = 0;
26  u32 queue_size = 1024;
27  u8 drop_on_diconnect = 1;
28 
29  /* Get a line of input. */
30  if (!unformat_user (input, unformat_line_input, line_input))
31  return 0;
32 
33  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
34  {
35  if (unformat (line_input, "queue-size %u", &queue_size))
36  ;
37  else if (unformat (line_input, "on-disconnect drop"))
38  drop_on_diconnect = 1;
39  else if (unformat (line_input, "on-disconnect pass"))
40  drop_on_diconnect = 0;
41  else if (unformat (line_input, "name %s", &name))
42  ;
43  else
44  {
45  err = clib_error_return (0, "unknown input `%U'",
46  format_unformat_error, input);
47  goto done;
48  }
49  }
50 
51  if (!is_pow2 (queue_size))
52  {
53  err = clib_error_return (0, "Queue size must be a power of two");
54  goto done;
55  }
56 
57  if (!name)
58  {
59  err = clib_error_return (0, "please specify instance name");
60  goto done;
61  }
62 
63  err = snort_instance_create (vm, (char *) name, min_log2 (queue_size),
64  drop_on_diconnect);
65 
66 done:
67  vec_free (name);
68  unformat_free (line_input);
69  return err;
70 }
71 
73  .path = "snort create-instance",
74  .short_help = "snort create-instaince name <name> [queue-size <size>] "
75  "[on-disconnect drop|pass]",
77 };
78 
79 static clib_error_t *
81  vlib_cli_command_t *cmd)
82 {
83  unformat_input_t _line_input, *line_input = &_line_input;
84  vnet_main_t *vnm = vnet_get_main ();
85  clib_error_t *err = 0;
86  u8 *name = 0;
87  u32 sw_if_index = ~0;
88 
89  /* Get a line of input. */
90  if (!unformat_user (input, unformat_line_input, line_input))
91  return 0;
92 
93  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
94  {
95  if (unformat (line_input, "interface %U", unformat_vnet_sw_interface,
96  vnm, &sw_if_index))
97  ;
98  else if (unformat (line_input, "instance %s", &name))
99  ;
100  else
101  {
102  err = clib_error_return (0, "unknown input `%U'",
103  format_unformat_error, input);
104  goto done;
105  }
106  }
107 
108  if (sw_if_index == ~0)
109  {
110  err = clib_error_return (0, "please specify interface");
111  goto done;
112  }
113 
114  if (!name)
115  {
116  err = clib_error_return (0, "please specify instance name");
117  goto done;
118  }
119 
120  err = snort_interface_enable_disable (vm, (char *) name, sw_if_index, 1);
121 
122 done:
123  vec_free (name);
124  unformat_free (line_input);
125  return err;
126 }
127 
129  .path = "snort attach",
130  .short_help = "snort attach instance <name> interface <if-name>",
131  .function = snort_attach_command_fn,
132 };
133 
134 static clib_error_t *
136  vlib_cli_command_t *cmd)
137 {
138  unformat_input_t _line_input, *line_input = &_line_input;
139  vnet_main_t *vnm = vnet_get_main ();
140  clib_error_t *err = 0;
141  u32 sw_if_index = ~0;
142 
143  /* Get a line of input. */
144  if (!unformat_user (input, unformat_line_input, line_input))
145  return 0;
146 
147  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
148  {
149  if (unformat (line_input, "interface %U", unformat_vnet_sw_interface,
150  vnm, &sw_if_index))
151  ;
152  else
153  {
154  err = clib_error_return (0, "unknown input `%U'",
155  format_unformat_error, input);
156  goto done;
157  }
158  }
159 
160  if (sw_if_index == ~0)
161  {
162  err = clib_error_return (0, "please specify interface");
163  goto done;
164  }
165 
167 
168 done:
169  unformat_free (line_input);
170  return err;
171 }
172 
174  .path = "snort detach",
175  .short_help = "snort detach interface <if-name>",
176  .function = snort_detach_command_fn,
177 };
178 
179 static clib_error_t *
181  vlib_cli_command_t *cmd)
182 {
183  snort_main_t *sm = &snort_main;
185 
186  pool_foreach (si, sm->instances)
188 
189  return 0;
190 }
191 
193  .path = "show snort instances",
194  .short_help = "show snort instances",
196 };
197 
198 static clib_error_t *
200  vlib_cli_command_t *cmd)
201 {
202  snort_main_t *sm = &snort_main;
203  vnet_main_t *vnm = vnet_get_main ();
205  u32 *index;
206 
207  vlib_cli_output (vm, "interface\tsnort instance");
209  {
210  if (index[0] != ~0)
211  {
212  si = vec_elt_at_index (sm->instances, index[0]);
214  index - sm->instance_by_sw_if_index, si->name);
215  }
216  }
217  return 0;
218 }
219 
221  .path = "show snort interfaces",
222  .short_help = "show snort interfaces",
224 };
225 
226 static clib_error_t *
228  vlib_cli_command_t *cmd)
229 {
230  snort_main_t *sm = &snort_main;
231  vlib_cli_output (vm, "number of clients: %d", pool_elts (sm->clients));
232  return 0;
233 }
234 
236  .path = "show snort clients",
237  .short_help = "show snort clients",
238  .function = snort_show_clients_command_fn,
239 };
240 
241 static clib_error_t *
243  vlib_cli_command_t *cmd)
244 {
245  return snort_set_node_mode (vm, VLIB_NODE_STATE_POLLING);
246 }
247 
248 static clib_error_t *
250  vlib_cli_command_t *cmd)
251 {
252  return snort_set_node_mode (vm, VLIB_NODE_STATE_INTERRUPT);
253 }
254 
256  .path = "snort mode polling",
257  .short_help = "snort mode polling|interrupt",
258  .function = snort_mode_polling_command_fn,
259 };
260 
262  .path = "snort mode interrupt",
263  .short_help = "snort mode polling|interrupt",
265 };
266 
267 static clib_error_t *
269  vlib_cli_command_t *cmd)
270 {
271  snort_main_t *sm = &snort_main;
272  char *mode =
273  sm->input_mode == VLIB_NODE_STATE_POLLING ? "polling" : "interrupt";
274  vlib_cli_output (vm, "input mode: %s", mode);
275  return 0;
276 }
277 
279  .path = "show snort mode",
280  .short_help = "show snort mode",
281  .function = snort_show_mode_command_fn,
282 };
vlib.h
snort_show_interfaces_command_fn
static clib_error_t * snort_show_interfaces_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:199
unformat_user
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:989
snort_set_node_mode
clib_error_t * snort_set_node_mode(vlib_main_t *vm, u32 mode)
Definition: main.c:465
snort_show_instances_command_fn
static clib_error_t * snort_show_instances_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:180
unformat_line_input
unformat_function_t unformat_line_input
Definition: format.h:275
name
string name[64]
Definition: fib.api:25
clib_error_return
#define clib_error_return(e, args...)
Definition: error.h:99
vlib_cli_command_t::path
char * path
Definition: cli.h:96
mode
vl_api_tunnel_mode_t mode
Definition: gre.api:48
snort_show_mode_command_fn
static clib_error_t * snort_show_mode_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:268
snort_create_instance_command
static vlib_cli_command_t snort_create_instance_command
(constructor) VLIB_CLI_COMMAND (snort_create_instance_command)
Definition: cli.c:72
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
snort.h
snort_show_clients_command
static vlib_cli_command_t snort_show_clients_command
(constructor) VLIB_CLI_COMMAND (snort_show_clients_command)
Definition: cli.c:235
unformat_input_t
struct _unformat_input_t unformat_input_t
snort_main_t::clients
snort_client_t * clients
Definition: snort.h:73
unformat
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
snort_mode_polling_command_fn
static clib_error_t * snort_mode_polling_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:242
pool_foreach
#define pool_foreach(VAR, POOL)
Iterate through pool.
Definition: pool.h:534
snort_main_t
Definition: snort.h:70
unformat_free
static void unformat_free(unformat_input_t *i)
Definition: format.h:155
vec_elt_at_index
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
Definition: vec_bootstrap.h:203
snort_show_instances_command
static vlib_cli_command_t snort_show_instances_command
(constructor) VLIB_CLI_COMMAND (snort_show_instances_command)
Definition: cli.c:192
vnet_get_main
vnet_main_t * vnet_get_main(void)
Definition: pnat_test_stubs.h:56
unformat_check_input
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:163
snort_main_t::instances
snort_instance_t * instances
Definition: snort.h:74
snort_create_instance_command_fn
static clib_error_t * snort_create_instance_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:20
snort_detach_command_fn
static clib_error_t * snort_detach_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:135
snort_detach_command
static vlib_cli_command_t snort_detach_command
(constructor) VLIB_CLI_COMMAND (snort_detach_command)
Definition: cli.c:173
snort_show_mode_command
static vlib_cli_command_t snort_show_mode_command
(constructor) VLIB_CLI_COMMAND (snort_show_mode_command)
Definition: cli.c:278
snort_mode_polling_command
static vlib_cli_command_t snort_mode_polling_command
(constructor) VLIB_CLI_COMMAND (snort_mode_polling_command)
Definition: cli.c:255
snort_show_clients_command_fn
static clib_error_t * snort_show_clients_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:227
format_unformat_error
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
VLIB_CLI_COMMAND
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:163
snort_mode_interrupt_command
static vlib_cli_command_t snort_mode_interrupt_command
(constructor) VLIB_CLI_COMMAND (snort_mode_interrupt_command)
Definition: cli.c:261
snort_main_t::instance_by_sw_if_index
u32 * instance_by_sw_if_index
Definition: snort.h:76
snort_mode_interrupt_command_fn
static clib_error_t * snort_mode_interrupt_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:249
vlib_cli_output
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:716
snort_instance_create
clib_error_t * snort_instance_create(vlib_main_t *vm, char *name, u8 log2_queue_sz, u8 drop_on_disconnect)
Definition: main.c:275
snort_main_t::input_mode
u32 input_mode
Definition: snort.h:79
vnet_main_t
Definition: vnet.h:76
snort_show_interfaces_command
static vlib_cli_command_t snort_show_interfaces_command
(constructor) VLIB_CLI_COMMAND (snort_show_interfaces_command)
Definition: cli.c:220
vec_free
#define vec_free(V)
Free vector's memory (no header).
Definition: vec.h:395
index
u32 index
Definition: flow_types.api:221
snort_instance_t
Definition: snort.h:37
format_vnet_sw_if_index_name
format_function_t format_vnet_sw_if_index_name
Definition: interface_funcs.h:458
unformat_vnet_sw_interface
unformat_function_t unformat_vnet_sw_interface
Definition: interface_funcs.h:462
format
description fragment has unexpected format
Definition: map.api:433
u32
unsigned int u32
Definition: types.h:88
si
vnet_sw_interface_t * si
Definition: interface_output.c:418
vec_foreach
#define vec_foreach(var, vec)
Vector iterator.
Definition: vec_bootstrap.h:213
pool_elts
static uword pool_elts(void *v)
Number of active elements in a pool.
Definition: pool.h:127
snort_interface_enable_disable
clib_error_t * snort_interface_enable_disable(vlib_main_t *vm, char *instance_name, u32 sw_if_index, int is_enable)
Definition: main.c:403
snort_attach_command_fn
static clib_error_t * snort_attach_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:80
snort_attach_command
static vlib_cli_command_t snort_attach_command
(constructor) VLIB_CLI_COMMAND (snort_attach_command)
Definition: cli.c:128
vlib_main_t
Definition: main.h:102
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
format_snort_instance
static u8 * format_snort_instance(u8 *s, va_list *args)
Definition: cli.c:10
i
int i
Definition: flowhash_template.h:376
vnet.h
min_log2
static uword min_log2(uword x)
Definition: clib.h:176
vlib_cli_command_t
Definition: cli.h:92
is_pow2
static uword is_pow2(uword x)
Definition: clib.h:267
sw_if_index
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:34
snort_main
snort_main_t snort_main
Definition: main.c:12
UNFORMAT_END_OF_INPUT
#define UNFORMAT_END_OF_INPUT
Definition: format.h:137