IGMP improvements
[vpp.git] / src / plugins / igmp / igmp_cli.c
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 *
32 igmp_clear_interface_command_fn (vlib_main_t * vm, unformat_input_t * input,
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_config_t *config;
41
42   if (!unformat_user (input, unformat_line_input, line_input))
43     {
44       error =
45         clib_error_return (0, "'help clear igmp' or 'clear igmp ?' for help");
46       return error;
47     }
48
49   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
50     {
51       if (unformat
52           (line_input, "int %U", unformat_vnet_sw_interface, vnm,
53            &sw_if_index));
54       else
55         {
56           error =
57             clib_error_return (0, "unknown input '%U'", format_unformat_error,
58                                line_input);
59           goto done;
60         }
61     }
62
63   config = igmp_config_lookup (sw_if_index);
64   if (config)
65     igmp_clear_config (config);
66
67 done:
68   unformat_free (line_input);
69   return error;
70 }
71
72 /* *INDENT-OFF* */
73 VLIB_CLI_COMMAND (igmp_clear_interface_command, static) = {
74   .path = "clear igmp",
75   .short_help = "clear igmp int <interface>",
76   .function = igmp_clear_interface_command_fn,
77 };
78 /* *INDENT-ON* */
79
80 static clib_error_t *
81 igmp_listen_command_fn (vlib_main_t * vm, unformat_input_t * input,
82                         vlib_cli_command_t * cmd)
83 {
84   unformat_input_t _line_input, *line_input = &_line_input;
85   clib_error_t *error = NULL;
86   u8 enable = 1;
87   ip46_address_t saddr, gaddr;
88   vnet_main_t *vnm = vnet_get_main ();
89   u32 sw_if_index;
90   int rv;
91
92   if (!unformat_user (input, unformat_line_input, line_input))
93     {
94       error =
95         clib_error_return (0,
96                            "'help igmp listen' or 'igmp listen ?' for help");
97       return error;
98     }
99
100   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
101     {
102       if (unformat (line_input, "enable"))
103         enable = 1;
104       else if (unformat (line_input, "disable"))
105         enable = 0;
106       else
107         if (unformat
108             (line_input, "int %U", unformat_vnet_sw_interface, vnm,
109              &sw_if_index));
110       else
111         if (unformat (line_input, "saddr %U", unformat_ip46_address, &saddr));
112       else
113         if (unformat (line_input, "gaddr %U", unformat_ip46_address, &gaddr));
114       else
115         {
116           error =
117             clib_error_return (0, "unknown input '%U'", format_unformat_error,
118                                line_input);
119           goto done;
120         }
121     }
122
123   if ((vnet_sw_interface_get_flags (vnm, sw_if_index)
124        && VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0)
125     {
126       error = clib_error_return (0, "Interface is down");
127       goto done;
128     }
129
130   rv = igmp_listen (vm, enable, sw_if_index, &saddr, &gaddr);
131
132   if (rv == -1)
133     {
134       if (enable)
135         error =
136           clib_error_return (0, "This igmp configuration already exists");
137       else
138         error =
139           clib_error_return (0, "This igmp configuration does not nexist");
140     }
141   else if (rv == -2)
142     error =
143       clib_error_return (0,
144                          "Failed to add configuration, interface is in router mode");
145
146 done:
147   unformat_free (line_input);
148   return error;
149 }
150
151 /* *INDENT-OFF* */
152 VLIB_CLI_COMMAND (igmp_listen_command, static) = {
153   .path = "igmp listen",
154   .short_help = "igmp listen [<enable|disable>] "
155                 "int <interface> saddr <ip4-address> gaddr <ip4-address>",
156   .function = igmp_listen_command_fn,
157 };
158 /* *INDENT-ON* */
159
160 static clib_error_t *
161 igmp_show_command_fn (vlib_main_t * vm, unformat_input_t * input,
162                       vlib_cli_command_t * cmd)
163 {
164   clib_error_t *error = NULL;
165   igmp_main_t *im = &igmp_main;
166   vnet_main_t *vnm = vnet_get_main ();
167   igmp_config_t *config;
168   igmp_group_t *group;
169   igmp_src_t *src;
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
177       FOR_EACH_GROUP (group, config,
178         ({
179           vlib_cli_output (vm, "\t%U", format_igmp_key, group->key);
180           FOR_EACH_SRC (src, group, IGMP_FILTER_MODE_INCLUDE,
181           ({
182               vlib_cli_output (vm, "\t\t%U", format_igmp_key, src->key);
183             }));
184         }));
185     }));
186   /* *INDENT-ON* */
187
188   return error;
189 }
190
191 /* *INDENT-OFF* */
192 VLIB_CLI_COMMAND (igmp_show_command, static) = {
193   .path = "show igmp config",
194   .short_help = "show igmp config",
195   .function = igmp_show_command_fn,
196 };
197 /* *INDENT-ON* */
198
199 static clib_error_t *
200 igmp_show_timers_command_fn (vlib_main_t * vm,
201                              unformat_input_t * input,
202                              vlib_cli_command_t * cmd)
203 {
204 #define _(n,f) vlib_cli_output (vm, "%s: %d", #f, igmp_timer_type_get(n));
205   foreach_igmp_timer_type
206 #undef _
207     return (NULL);
208 }
209
210 /* *INDENT-OFF* */
211 VLIB_CLI_COMMAND (igmp_show_timers_command, static) = {
212   .path = "show igmp timers",
213   .short_help = "show igmp timers",
214   .function = igmp_show_timers_command_fn,
215 };
216 /* *INDENT-ON* */
217
218 static clib_error_t *
219 test_igmp_command_fn (vlib_main_t * vm,
220                       unformat_input_t * input, vlib_cli_command_t * cmd)
221 {
222   clib_error_t *error = NULL;
223   u32 value;
224
225   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
226     {
227       if (unformat (input, "query %d", &value))
228         igmp_timer_type_set (IGMP_TIMER_QUERY, value);
229       else if (unformat (input, "src %d", &value))
230         igmp_timer_type_set (IGMP_TIMER_SRC, value);
231       else if (unformat (input, "leave %d", &value))
232         igmp_timer_type_set (IGMP_TIMER_LEAVE, value);
233       else
234         error = clib_error_return (0, "query or src timers only");
235     }
236
237   return error;
238 }
239
240 /* *INDENT-OFF* */
241 VLIB_CLI_COMMAND (test_igmp_command, static) = {
242   .path = "test igmp timers",
243   .short_help = "Change the default values for IGMP timers - only sensible during unit tests",
244   .function = test_igmp_command_fn,
245 };
246 /* *INDENT-ON* */
247
248
249 clib_error_t *
250 igmp_cli_init (vlib_main_t * vm)
251 {
252   return 0;
253 }
254
255 VLIB_INIT_FUNCTION (igmp_cli_init);
256
257 /*
258  * fd.io coding-style-patch-verification: ON
259  *
260  * Local Variables:
261  * eval: (c-set-style "gnu")
262  * End:
263  */