63e3645f14ae9b709df3828ff609ee4e652a8ca2
[vpp.git] / src / vnet / teib / teib_cli.c
1 /*
2  * Copyright (c) 2019 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 <vnet/teib/teib.h>
17
18 static clib_error_t *
19 teib_add (vlib_main_t * vm,
20           unformat_input_t * input, vlib_cli_command_t * cmd)
21 {
22   unformat_input_t _line_input, *line_input = &_line_input;
23   ip46_address_t nh = ip46_address_initializer;
24   ip_address_t peer = IP_ADDRESS_V6_ALL_0S;
25   u32 sw_if_index, nh_table_id;
26   clib_error_t *error = NULL;
27   int rv;
28
29   sw_if_index = ~0;
30   nh_table_id = 0;
31
32   /* Get a line of input. */
33   if (!unformat_user (input, unformat_line_input, line_input))
34     return 0;
35
36   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
37     {
38       if (unformat (line_input, "%U", unformat_vnet_sw_interface,
39                     vnet_get_main (), &sw_if_index))
40         ;
41       else if (unformat (line_input, "peer %U", unformat_ip_address, &peer))
42         ;
43       else if (unformat (line_input, "nh %U", unformat_ip46_address, &nh))
44         ;
45       else if (unformat (line_input, "nh-table-id %d", &nh_table_id))
46         ;
47       else
48         {
49           error = clib_error_return (0, "unknown input `%U'",
50                                      format_unformat_error, line_input);
51           goto done;
52         }
53     }
54
55   if (~0 == sw_if_index)
56     {
57       error = clib_error_return (0, "interface required'",
58                                  format_unformat_error, line_input);
59       goto done;
60     }
61   if (ip_address_is_zero (&peer))
62     {
63       error = clib_error_return (0, "peer required'",
64                                  format_unformat_error, line_input);
65       goto done;
66     }
67   if (ip46_address_is_zero (&nh))
68     {
69       error = clib_error_return (0, "next-hop required'",
70                                  format_unformat_error, line_input);
71       goto done;
72     }
73
74   rv = teib_entry_add (sw_if_index,
75                        ip_address_family_to_fib_proto (ip_addr_version
76                                                        (&peer)),
77                        &ip_addr_46 (&peer), nh_table_id, &nh);
78
79   if (rv)
80     {
81       error = clib_error_return_code (NULL, rv, 0,
82                                       "NRHP error",
83                                       format_unformat_error, line_input);
84     }
85
86 done:
87   unformat_free (line_input);
88
89   return error;
90 }
91
92 /* *INDENT-OFF* */
93 VLIB_CLI_COMMAND (teib_create_command, static) = {
94   .path = "create teib",
95   .short_help = "create teib <interface> peer <addr> nh <addr> [nh-table-id <ID>]",
96   .function = teib_add,
97 };
98 /* *INDENT-ON* */
99
100 static clib_error_t *
101 teib_del (vlib_main_t * vm,
102           unformat_input_t * input, vlib_cli_command_t * cmd)
103 {
104   unformat_input_t _line_input, *line_input = &_line_input;
105   ip_address_t peer = IP_ADDRESS_V6_ALL_0S;
106   clib_error_t *error = NULL;
107   u32 sw_if_index;
108   int rv;
109
110   sw_if_index = ~0;
111
112   /* Get a line of input. */
113   if (!unformat_user (input, unformat_line_input, line_input))
114     return 0;
115
116   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
117     {
118       if (unformat (line_input, "%U", unformat_vnet_sw_interface,
119                     vnet_get_main (), &sw_if_index))
120         ;
121       else if (unformat (line_input, "peer %U", unformat_ip_address, &peer))
122         ;
123       else
124         {
125           error = clib_error_return (0, "unknown input `%U'",
126                                      format_unformat_error, line_input);
127           goto done;
128         }
129     }
130
131   if (~0 == sw_if_index)
132     {
133       error = clib_error_return (0, "interface required'",
134                                  format_unformat_error, line_input);
135     }
136   if (ip_address_is_zero (&peer))
137     {
138       error = clib_error_return (0, "peer required'",
139                                  format_unformat_error, line_input);
140       goto done;
141     }
142
143   rv = teib_entry_del (sw_if_index,
144                        ip_address_family_to_fib_proto (ip_addr_version
145                                                        (&peer)),
146                        &ip_addr_46 (&peer));
147
148   if (rv)
149     {
150       error = clib_error_return_code (NULL, rv, 0,
151                                       "NRHP error",
152                                       format_unformat_error, line_input);
153     }
154
155 done:
156   unformat_free (line_input);
157
158   return error;
159 }
160
161 /* *INDENT-OFF* */
162 VLIB_CLI_COMMAND (teib_delete_command, static) = {
163   .path = "delete teib",
164   .short_help = "delete teib <interface> peer <addr>",
165   .function = teib_del,
166 };
167 /* *INDENT-ON* */
168
169 static walk_rc_t
170 teib_show_one (index_t nei, void *ctx)
171 {
172   vlib_cli_output (ctx, "%U", format_teib_entry, nei);
173
174   return (WALK_CONTINUE);
175 }
176
177
178 static clib_error_t *
179 teib_show (vlib_main_t * vm,
180            unformat_input_t * input, vlib_cli_command_t * cmd)
181 {
182   teib_walk (teib_show_one, vm);
183   return (NULL);
184 }
185
186 /* *INDENT-OFF* */
187 VLIB_CLI_COMMAND (teib_show_command, static) = {
188   .path = "show teib",
189   .short_help = "show teib",
190   .function = teib_show,
191 };
192 /* *INDENT-ON* */
193
194 /*
195  * fd.io coding-style-patch-verification: ON
196  *
197  * Local Variables:
198  * eval: (c-set-style "gnu")
199  * End:
200  */