vxlan:use bihash_16_8 for ipv4 lookup
[vpp.git] / src / plugins / 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_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>",
77   .function = igmp_clear_interface_command_fn,
78 };
79 /* *INDENT-ON* */
80
81 static clib_error_t *
82 igmp_listen_command_fn (vlib_main_t * vm, unformat_input_t * input,
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 =
96         clib_error_return (0,
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)
125        && VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0)
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                     IGMP_CONFIG_FLAG_CLI_API_CONFIGURED);
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 =
144       clib_error_return (0,
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 *
162 igmp_show_command_fn (vlib_main_t * vm, unformat_input_t * input,
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_group_t *group;
170   igmp_src_t *src;
171
172   /* *INDENT-OFF* */
173   pool_foreach (config, im->configs, (
174     {
175       vlib_cli_output (vm, "interface: %U", format_vnet_sw_if_index_name,
176                        vnm, config->sw_if_index);
177         pool_foreach (group, config->groups, (
178           {
179             vlib_cli_output (vm, "\t%U:%U", format_igmp_report_type, group->type, format_ip46_address, &group->addr, ip46_address_is_ip4 (&group->addr));
180             pool_foreach (src, group->srcs, (
181               {
182                 vlib_cli_output (vm, "\t\t%U", format_ip46_address, &src->addr, ip46_address_is_ip4 (&src->addr));
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 clib_error_t *
200 igmp_cli_init (vlib_main_t * vm)
201 {
202   return 0;
203 }
204
205 VLIB_INIT_FUNCTION (igmp_cli_init);
206
207 /*
208  * fd.io coding-style-patch-verification: ON
209  *
210  * Local Variables:
211  * eval: (c-set-style "gnu")
212  * End:
213  */