LISP: Fix gpe API
[vpp.git] / src / vnet / lisp-gpe / lisp_gpe.c
1 /*
2  * Copyright (c) 2016 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  * @file
17  * @brief Common utility functions for IPv4, IPv6 and L2 LISP-GPE tunnels.
18  *
19  */
20
21 #include <vnet/lisp-gpe/lisp_gpe.h>
22 #include <vnet/lisp-gpe/lisp_gpe_fwd_entry.h>
23 #include <vnet/lisp-gpe/lisp_gpe_adjacency.h>
24 #include <vnet/lisp-gpe/lisp_gpe_tenant.h>
25
26 /** LISP-GPE global state */
27 lisp_gpe_main_t lisp_gpe_main;
28
29
30 /** CLI command to add/del forwarding entry. */
31 static clib_error_t *
32 lisp_gpe_add_del_fwd_entry_command_fn (vlib_main_t * vm,
33                                        unformat_input_t * input,
34                                        vlib_cli_command_t * cmd)
35 {
36   unformat_input_t _line_input, *line_input = &_line_input;
37   u8 is_add = 1;
38   ip_address_t lloc, rloc;
39   clib_error_t *error = 0;
40   gid_address_t _reid, *reid = &_reid, _leid, *leid = &_leid;
41   u8 reid_set = 0, leid_set = 0, is_negative = 0, dp_table_set = 0,
42     vni_set = 0;
43   u32 vni = 0, dp_table = 0, action = ~0, w;
44   locator_pair_t pair, *pairs = 0;
45   int rv;
46
47   memset (leid, 0, sizeof (*leid));
48   memset (reid, 0, sizeof (*reid));
49
50   /* Get a line of input. */
51   if (!unformat_user (input, unformat_line_input, line_input))
52     return 0;
53
54   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
55     {
56       if (unformat (line_input, "del"))
57         is_add = 0;
58       else if (unformat (line_input, "add"))
59         is_add = 1;
60       else if (unformat (line_input, "leid %U", unformat_gid_address, leid))
61         {
62           leid_set = 1;
63         }
64       else if (unformat (line_input, "reid %U", unformat_gid_address, reid))
65         {
66           reid_set = 1;
67         }
68       else if (unformat (line_input, "vni %u", &vni))
69         {
70           gid_address_vni (leid) = vni;
71           gid_address_vni (reid) = vni;
72           vni_set = 1;
73         }
74       else if (unformat (line_input, "vrf %u", &dp_table))
75         {
76           dp_table_set = 1;
77         }
78       else if (unformat (line_input, "bd %u", &dp_table))
79         {
80           dp_table_set = 1;
81         }
82       else if (unformat (line_input, "negative action %U",
83                          unformat_negative_mapping_action, &action))
84         {
85           is_negative = 1;
86         }
87       else if (unformat (line_input, "loc-pair %U %U w %d",
88                          unformat_ip_address, &lloc,
89                          unformat_ip_address, &rloc, &w))
90         {
91           pair.lcl_loc = lloc;
92           pair.rmt_loc = rloc;
93           pair.weight = w;
94           vec_add1 (pairs, pair);
95         }
96       else
97         {
98           error = unformat_parse_error (line_input);
99           vlib_cli_output (vm, "parse error: '%U'",
100                            format_unformat_error, line_input);
101           goto done;
102         }
103     }
104
105   if (!vni_set || !dp_table_set)
106     {
107       vlib_cli_output (vm, "vni and vrf/bd must be set!");
108       goto done;
109     }
110
111   if (!reid_set)
112     {
113       vlib_cli_output (vm, "remote eid must be set!");
114       goto done;
115     }
116
117   if (is_negative)
118     {
119       if (~0 == action)
120         {
121           vlib_cli_output (vm, "no action set for negative tunnel!");
122           goto done;
123         }
124     }
125   else
126     {
127       if (vec_len (pairs) == 0)
128         {
129           vlib_cli_output (vm, "expected ip4/ip6 locators");
130           goto done;
131         }
132     }
133
134   if (!leid_set)
135     {
136       /* if leid not set, make sure it's the same AFI like reid */
137       gid_address_type (leid) = gid_address_type (reid);
138       if (GID_ADDR_IP_PREFIX == gid_address_type (reid))
139         gid_address_ip_version (leid) = gid_address_ip_version (reid);
140     }
141
142   /* add fwd entry */
143   vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
144   memset (a, 0, sizeof (a[0]));
145
146   a->is_add = is_add;
147   a->is_negative = is_negative;
148   a->vni = vni;
149   a->table_id = dp_table;
150   gid_address_copy (&a->lcl_eid, leid);
151   gid_address_copy (&a->rmt_eid, reid);
152   a->locator_pairs = pairs;
153
154   rv = vnet_lisp_gpe_add_del_fwd_entry (a, 0);
155   if (0 != rv)
156     {
157       vlib_cli_output (vm, "failed to %s gpe tunnel!",
158                        is_add ? "add" : "delete");
159     }
160
161 done:
162   unformat_free (line_input);
163   vec_free (pairs);
164   return error;
165 }
166
167 /* *INDENT-OFF* */
168 VLIB_CLI_COMMAND (lisp_gpe_add_del_fwd_entry_command, static) = {
169   .path = "lisp gpe entry",
170   .short_help = "lisp gpe entry add/del vni <vni> vrf/bd <id> [leid <leid>]"
171       "reid <reid> [loc-pair <lloc> <rloc> w <weight>] "
172       "[negative action <action>]",
173   .function = lisp_gpe_add_del_fwd_entry_command_fn,
174 };
175 /* *INDENT-ON* */
176
177 /** Check if LISP-GPE is enabled. */
178 u8
179 vnet_lisp_gpe_enable_disable_status (void)
180 {
181   lisp_gpe_main_t *lgm = &lisp_gpe_main;
182
183   return lgm->is_en;
184 }
185
186 /** Enable/disable LISP-GPE. */
187 clib_error_t *
188 vnet_lisp_gpe_enable_disable (vnet_lisp_gpe_enable_disable_args_t * a)
189 {
190   lisp_gpe_main_t *lgm = &lisp_gpe_main;
191
192   if (a->is_en)
193     {
194       lgm->is_en = 1;
195     }
196   else
197     {
198       /* remove all entries */
199       vnet_lisp_gpe_fwd_entry_flush ();
200
201       /* disable all l3 ifaces */
202       lisp_gpe_tenant_flush ();
203
204       lgm->is_en = 0;
205     }
206
207   return 0;
208 }
209
210 /** CLI command to enable/disable LISP-GPE. */
211 static clib_error_t *
212 lisp_gpe_enable_disable_command_fn (vlib_main_t * vm,
213                                     unformat_input_t * input,
214                                     vlib_cli_command_t * cmd)
215 {
216   unformat_input_t _line_input, *line_input = &_line_input;
217   u8 is_en = 1;
218   vnet_lisp_gpe_enable_disable_args_t _a, *a = &_a;
219
220   /* Get a line of input. */
221   if (!unformat_user (input, unformat_line_input, line_input))
222     return 0;
223
224   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
225     {
226       if (unformat (line_input, "enable"))
227         is_en = 1;
228       else if (unformat (line_input, "disable"))
229         is_en = 0;
230       else
231         {
232           return clib_error_return (0, "parse error: '%U'",
233                                     format_unformat_error, line_input);
234         }
235     }
236   a->is_en = is_en;
237   return vnet_lisp_gpe_enable_disable (a);
238 }
239
240 /* *INDENT-OFF* */
241 VLIB_CLI_COMMAND (enable_disable_lisp_gpe_command, static) = {
242   .path = "lisp gpe",
243   .short_help = "lisp gpe [enable|disable]",
244   .function = lisp_gpe_enable_disable_command_fn,
245 };
246 /* *INDENT-ON* */
247
248 /** CLI command to show LISP-GPE interfaces. */
249 static clib_error_t *
250 lisp_show_iface_command_fn (vlib_main_t * vm,
251                             unformat_input_t * input,
252                             vlib_cli_command_t * cmd)
253 {
254   lisp_gpe_main_t *lgm = &lisp_gpe_main;
255   hash_pair_t *p;
256
257   vlib_cli_output (vm, "%=10s%=12s", "vrf", "hw_if_index");
258
259   /* *INDENT-OFF* */
260   hash_foreach_pair (p, lgm->l3_ifaces.hw_if_index_by_dp_table, ({
261     vlib_cli_output (vm, "%=10d%=10d", p->key, p->value[0]);
262   }));
263   /* *INDENT-ON* */
264
265   if (0 != lgm->l2_ifaces.hw_if_index_by_dp_table)
266     {
267       vlib_cli_output (vm, "%=10s%=12s", "bd_id", "hw_if_index");
268       /* *INDENT-OFF* */
269       hash_foreach_pair (p, lgm->l2_ifaces.hw_if_index_by_dp_table, ({
270         vlib_cli_output (vm, "%=10d%=10d", p->key, p->value[0]);
271       }));
272       /* *INDENT-ON* */
273     }
274   return 0;
275 }
276
277 /* *INDENT-OFF* */
278 VLIB_CLI_COMMAND (lisp_show_iface_command) = {
279     .path = "show lisp gpe interface",
280     .short_help = "show lisp gpe interface",
281     .function = lisp_show_iface_command_fn,
282 };
283 /* *INDENT-ON* */
284
285 /** Format LISP-GPE status. */
286 u8 *
287 format_vnet_lisp_gpe_status (u8 * s, va_list * args)
288 {
289   lisp_gpe_main_t *lgm = &lisp_gpe_main;
290   return format (s, "%s", lgm->is_en ? "enabled" : "disabled");
291 }
292
293
294 /** LISP-GPE init function. */
295 clib_error_t *
296 lisp_gpe_init (vlib_main_t * vm)
297 {
298   lisp_gpe_main_t *lgm = &lisp_gpe_main;
299   clib_error_t *error = 0;
300
301   if ((error = vlib_call_init_function (vm, ip_main_init)))
302     return error;
303
304   if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
305     return error;
306
307   lgm->vnet_main = vnet_get_main ();
308   lgm->vlib_main = vm;
309   lgm->im4 = &ip4_main;
310   lgm->im6 = &ip6_main;
311   lgm->lm4 = &ip4_main.lookup_main;
312   lgm->lm6 = &ip6_main.lookup_main;
313
314   lgm->lisp_gpe_fwd_entries =
315     hash_create_mem (0, sizeof (lisp_gpe_fwd_entry_key_t), sizeof (uword));
316
317   udp_register_dst_port (vm, UDP_DST_PORT_lisp_gpe,
318                          lisp_gpe_ip4_input_node.index, 1 /* is_ip4 */ );
319   udp_register_dst_port (vm, UDP_DST_PORT_lisp_gpe6,
320                          lisp_gpe_ip6_input_node.index, 0 /* is_ip4 */ );
321   return 0;
322 }
323
324 VLIB_INIT_FUNCTION (lisp_gpe_init);
325
326 /*
327  * fd.io coding-style-patch-verification: ON
328  *
329  * Local Variables:
330  * eval: (c-set-style "gnu")
331  * End:
332  */