FD.io VPP  v18.04-17-g3a0d853
Vector Packet Processing
cli.c
Go to the documentation of this file.
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2017 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17 
18 #include <stdint.h>
19 #include <sys/ioctl.h>
20 #include <inttypes.h>
21 
22 #include <vlib/vlib.h>
23 #include <vlib/unix/unix.h>
24 #include <vnet/ip/ip.h>
25 #include <vnet/fib/fib_entry.h>
26 #include <vnet/fib/fib_table.h>
27 #include <vnet/mfib/mfib_table.h>
28 
29 #include <igmp/igmp.h>
30 
31 static clib_error_t *
33  vlib_cli_command_t * cmd)
34 {
35  unformat_input_t _line_input, *line_input = &_line_input;
36  clib_error_t *error = NULL;
37  vnet_main_t *vnm = vnet_get_main ();
38  u32 sw_if_index;
39 
40  igmp_main_t *im = &igmp_main;
41  igmp_config_t *config;
42 
43  if (!unformat_user (input, unformat_line_input, line_input))
44  {
45  error =
46  clib_error_return (0, "'help clear igmp' or 'clear igmp ?' for help");
47  return error;
48  }
49 
50  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
51  {
52  if (unformat
53  (line_input, "int %U", unformat_vnet_sw_interface, vnm,
54  &sw_if_index));
55  else
56  {
57  error =
58  clib_error_return (0, "unknown input '%U'", format_unformat_error,
59  line_input);
60  goto done;
61  }
62  }
63 
64  config = igmp_config_lookup (im, sw_if_index);
65  if (config)
66  igmp_clear_config (config);
67 
68 done:
69  unformat_free (line_input);
70  return error;
71 }
72 
73 /* *INDENT-OFF* */
74 VLIB_CLI_COMMAND (igmp_clear_interface_command, static) = {
75  .path = "clear igmp",
76  .short_help = "clear igmp int <interface>",
78 };
79 /* *INDENT-ON* */
80 
81 static clib_error_t *
83  vlib_cli_command_t * cmd)
84 {
85  unformat_input_t _line_input, *line_input = &_line_input;
86  clib_error_t *error = NULL;
87  u8 enable = 1;
88  ip46_address_t saddr, gaddr;
89  vnet_main_t *vnm = vnet_get_main ();
90  u32 sw_if_index;
91  int rv;
92 
93  if (!unformat_user (input, unformat_line_input, line_input))
94  {
95  error =
97  "'help igmp listen' or 'igmp listen ?' for help");
98  return error;
99  }
100 
101  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
102  {
103  if (unformat (line_input, "enable"))
104  enable = 1;
105  else if (unformat (line_input, "disable"))
106  enable = 0;
107  else
108  if (unformat
109  (line_input, "int %U", unformat_vnet_sw_interface, vnm,
110  &sw_if_index));
111  else
112  if (unformat (line_input, "saddr %U", unformat_ip46_address, &saddr));
113  else
114  if (unformat (line_input, "gaddr %U", unformat_ip46_address, &gaddr));
115  else
116  {
117  error =
118  clib_error_return (0, "unknown input '%U'", format_unformat_error,
119  line_input);
120  goto done;
121  }
122  }
123 
124  if ((vnet_sw_interface_get_flags (vnm, sw_if_index)
126  {
127  error = clib_error_return (0, "Interface is down");
128  goto done;
129  }
130 
131  rv = igmp_listen (vm, enable, sw_if_index, saddr, gaddr,
132  /* cli_api_listen */ 1);
133  if (rv == -1)
134  {
135  if (enable)
136  error =
137  clib_error_return (0, "This igmp configuration already exists");
138  else
139  error =
140  clib_error_return (0, "This igmp configuration does not nexist");
141  }
142  else if (rv == -2)
143  error =
145  "Failed to add configuration, interface is in router mode");
146 
147 done:
148  unformat_free (line_input);
149  return error;
150 }
151 
152 /* *INDENT-OFF* */
153 VLIB_CLI_COMMAND (igmp_listen_command, static) = {
154  .path = "igmp listen",
155  .short_help = "igmp listen [<enable|disable>] "
156  "int <interface> saddr <ip4-address> gaddr <ip4-address>",
157  .function = igmp_listen_command_fn,
158 };
159 /* *INDENT-ON* */
160 
161 static clib_error_t *
163  vlib_cli_command_t * cmd)
164 {
165  clib_error_t *error = NULL;
166  igmp_main_t *im = &igmp_main;
167  vnet_main_t *vnm = vnet_get_main ();
168  igmp_config_t *config;
169  igmp_sg_t *sg;
170 
171  /* *INDENT-OFF* */
172  pool_foreach (config, im->configs, (
173  {
174  vlib_cli_output (vm, "interface: %U", format_vnet_sw_if_index_name,
175  vnm, config->sw_if_index);
176  pool_foreach (sg, config->sg, (
177  {
178  vlib_cli_output (vm, "\t(S,G): %U:%U:%U", format_ip46_address,
179  &sg->saddr, ip46_address_is_ip4 (&sg->saddr),
180  format_ip46_address, &sg->gaddr, ip46_address_is_ip4
181  (&sg->gaddr), format_igmp_report_type, sg->group_type);
182  }));
183  }));
184  /* *INDENT-ON* */
185 
186  return error;
187 }
188 
189 /* *INDENT-OFF* */
190 VLIB_CLI_COMMAND (igmp_show_command, static) = {
191  .path = "show igmp config",
192  .short_help = "show igmp config",
193  .function = igmp_show_command_fn,
194 };
195 /* *INDENT-ON* */
196 
197 clib_error_t *
199 {
200  return 0;
201 }
202 
204 
205 /*
206  * fd.io coding-style-patch-verification: ON
207  *
208  * Local Variables:
209  * eval: (c-set-style "gnu")
210  * End:
211  */
vnet_main_t * vnet_get_main(void)
Definition: misc.c:47
igmp_main_t igmp_main
Definition: igmp.c:34
#define NULL
Definition: clib.h:55
static clib_error_t * igmp_show_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:162
igmp_config_t * configs
pool of igmp configurations
Definition: igmp.h:141
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:983
unformat_function_t unformat_vnet_sw_interface
static clib_error_t * igmp_listen_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:82
void igmp_clear_config(igmp_config_t *config)
Definition: igmp.c:38
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:440
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
#define clib_error_return(e, args...)
Definition: error.h:99
unformat_function_t unformat_line_input
Definition: format.h:281
clib_error_t * igmp_cli_init(vlib_main_t *vm)
Definition: cli.c:198
struct _unformat_input_t unformat_input_t
#define UNFORMAT_END_OF_INPUT
Definition: format.h:143
vlib_main_t * vm
Definition: buffer.c:294
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:154
#define VNET_SW_INTERFACE_FLAG_ADMIN_UP
Definition: interface.h:588
unsigned int u32
Definition: types.h:88
unformat_function_t unformat_ip46_address
Definition: format.h:71
int igmp_listen(vlib_main_t *vm, u8 enable, u32 sw_if_index, ip46_address_t saddr, ip46_address_t gaddr, u8 cli_api_configured)
Definition: igmp.c:595
static igmp_config_t * igmp_config_lookup(igmp_main_t *im, u32 sw_if_index)
Definition: igmp.h:233
static clib_error_t * igmp_clear_interface_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:32
unsigned char u8
Definition: types.h:56
static void unformat_free(unformat_input_t *i)
Definition: format.h:161
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
static uword vnet_sw_interface_get_flags(vnet_main_t *vnm, u32 sw_if_index)
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:169