ONE-2: Add new LISP dump API for lisp gpe
[vpp.git] / vnet / 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 #include <vnet/lisp-gpe/lisp_gpe.h>
17
18 lisp_gpe_main_t lisp_gpe_main;
19
20 static int
21 lisp_gpe_rewrite (lisp_gpe_tunnel_t * t)
22 {
23   u8 *rw = 0;
24   lisp_gpe_header_t * lisp0;
25   int len;
26
27   if (ip_addr_version(&t->src) == IP4)
28     {
29       ip4_header_t * ip0;
30       ip4_udp_lisp_gpe_header_t * h0;
31       len = sizeof(*h0);
32
33       vec_validate_aligned(rw, len - 1, CLIB_CACHE_LINE_BYTES);
34
35       h0 = (ip4_udp_lisp_gpe_header_t *) rw;
36
37       /* Fixed portion of the (outer) ip4 header */
38       ip0 = &h0->ip4;
39       ip0->ip_version_and_header_length = 0x45;
40       ip0->ttl = 254;
41       ip0->protocol = IP_PROTOCOL_UDP;
42
43       /* we fix up the ip4 header length and checksum after-the-fact */
44       ip_address_copy_addr(&ip0->src_address, &t->src);
45       ip_address_copy_addr(&ip0->dst_address, &t->dst);
46       ip0->checksum = ip4_header_checksum (ip0);
47
48       /* UDP header, randomize src port on something, maybe? */
49       h0->udp.src_port = clib_host_to_net_u16 (4341);
50       h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_lisp_gpe);
51
52       /* LISP-gpe header */
53       lisp0 = &h0->lisp;
54     }
55   else
56     {
57       ip6_header_t * ip0;
58       ip6_udp_lisp_gpe_header_t * h0;
59       len = sizeof(*h0);
60
61       vec_validate_aligned(rw, len - 1, CLIB_CACHE_LINE_BYTES);
62
63       h0 = (ip6_udp_lisp_gpe_header_t *) rw;
64
65       /* Fixed portion of the (outer) ip6 header */
66       ip0 = &h0->ip6;
67       ip0->ip_version_traffic_class_and_flow_label =
68           clib_host_to_net_u32 (0x6 << 28);
69       ip0->hop_limit = 254;
70       ip0->protocol = IP_PROTOCOL_UDP;
71
72       /* we fix up the ip6 header length after-the-fact */
73       ip_address_copy_addr(&ip0->src_address, &t->src);
74       ip_address_copy_addr(&ip0->dst_address, &t->dst);
75
76       /* UDP header, randomize src port on something, maybe? */
77       h0->udp.src_port = clib_host_to_net_u16 (4341);
78       h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_lisp_gpe);
79
80       /* LISP-gpe header */
81       lisp0 = &h0->lisp;
82     }
83
84   lisp0->flags = t->flags;
85   lisp0->ver_res = t->ver_res;
86   lisp0->res = t->res;
87   lisp0->next_protocol = t->next_protocol;
88   lisp0->iid = clib_host_to_net_u32 (t->vni);
89
90   t->rewrite = rw;
91   return 0;
92 }
93
94 /* TODO remove */
95 int
96 vnet_lisp_gpe_add_del_tunnel (vnet_lisp_gpe_add_del_tunnel_args_t *a,
97                               u32 * sw_if_indexp)
98 {
99   clib_warning ("UNSUPPORTED! Use vnet_lisp_gpe_add_del_fwd_entry");
100   return 0;
101 }
102
103 #define foreach_copy_field                      \
104 _(encap_fib_index)                              \
105 _(decap_fib_index)                              \
106 _(decap_next_index)                             \
107 _(flags)                                        \
108 _(next_protocol)                                \
109 _(ver_res)                                      \
110 _(res)                                          \
111 _(vni)
112
113 static u32
114 add_del_tunnel (vnet_lisp_gpe_add_del_fwd_entry_args_t *a, u32 * tun_index_res)
115 {
116   lisp_gpe_main_t * lgm = &lisp_gpe_main;
117   lisp_gpe_tunnel_t *t = 0;
118   uword * p;
119   int rv;
120   lisp_gpe_tunnel_key_t key;
121
122   memset(&key, 0, sizeof(key));
123   ip_prefix_copy(&key.eid, &gid_address_ippref(&a->deid));
124   ip_address_copy(&key.dst_loc, &a->dlocator);
125   key.iid = clib_host_to_net_u32 (a->vni);
126
127   p = mhash_get (&lgm->lisp_gpe_tunnel_by_key, &key);
128
129   if (a->is_add)
130     {
131       /* adding a tunnel: tunnel must not already exist */
132       if (p)
133         return VNET_API_ERROR_INVALID_VALUE;
134
135       if (a->decap_next_index >= LISP_GPE_INPUT_N_NEXT)
136         return VNET_API_ERROR_INVALID_DECAP_NEXT;
137
138       pool_get_aligned (lgm->tunnels, t, CLIB_CACHE_LINE_BYTES);
139       memset (t, 0, sizeof (*t));
140
141       /* copy from arg structure */
142 #define _(x) t->x = a->x;
143       foreach_copy_field;
144 #undef _
145
146       ip_address_copy(&t->src, &a->slocator);
147       ip_address_copy(&t->dst, &a->dlocator);
148
149       rv = lisp_gpe_rewrite (t);
150
151       if (rv)
152         {
153           pool_put(lgm->tunnels, t);
154           return rv;
155         }
156
157       mhash_set(&lgm->lisp_gpe_tunnel_by_key, &key, t - lgm->tunnels, 0);
158
159       /* return tunnel index */
160       if (tun_index_res)
161         tun_index_res[0] = t - lgm->tunnels;
162     }
163   else
164     {
165       /* deleting a tunnel: tunnel must exist */
166       if (!p)
167         {
168           clib_warning("Tunnel for eid %U doesn't exist!", format_gid_address,
169                        &a->deid);
170           return VNET_API_ERROR_NO_SUCH_ENTRY;
171         }
172
173       t = pool_elt_at_index(lgm->tunnels, p[0]);
174
175       mhash_unset(&lgm->lisp_gpe_tunnel_by_key, &key, 0);
176
177       vec_free(t->rewrite);
178       pool_put(lgm->tunnels, t);
179     }
180
181   return 0;
182 }
183
184 static int
185 add_del_negative_fwd_entry (lisp_gpe_main_t * lgm,
186                             vnet_lisp_gpe_add_del_fwd_entry_args_t * a)
187 {
188   ip_adjacency_t adj;
189   /* setup adjacency for eid */
190   memset (&adj, 0, sizeof(adj));
191   adj.n_adj = 1;
192   adj.explicit_fib_index = ~0;
193
194   ip_prefix_t * dpref = &gid_address_ippref(&a->deid);
195   ip_prefix_t * spref = &gid_address_ippref(&a->seid);
196
197   switch (a->action)
198     {
199     case NO_ACTION:
200       /* TODO update timers? */
201     case FORWARD_NATIVE:
202       /* TODO check if route/next-hop for eid exists in fib and add
203        * more specific for the eid with the next-hop found */
204     case SEND_MAP_REQUEST:
205       /* TODO insert tunnel that always sends map-request */
206     case DROP:
207       /* for drop fwd entries, just add route, no need to add encap tunnel */
208       adj.lookup_next_index =  ip_prefix_version(dpref) == IP4 ?
209               LGPE_IP4_LOOKUP_NEXT_DROP : LGPE_IP6_LOOKUP_NEXT_DROP;
210
211       /* add/delete route for prefix */
212       return ip_sd_fib_add_del_route (lgm, dpref, spref, a->table_id, &adj,
213                                       a->is_add);
214       break;
215     default:
216       return -1;
217     }
218 }
219
220 int
221 vnet_lisp_gpe_add_del_fwd_entry (vnet_lisp_gpe_add_del_fwd_entry_args_t * a,
222                                  u32 * hw_if_indexp)
223 {
224   lisp_gpe_main_t * lgm = &lisp_gpe_main;
225   ip_adjacency_t adj, * adjp;
226   u32 adj_index, rv, tun_index = ~0;
227   ip_prefix_t * dpref, * spref;
228   uword * lookup_next_index, * lgpe_sw_if_index, * lnip;
229   u8 ip_ver;
230
231   /* treat negative fwd entries separately */
232   if (a->is_negative)
233     return add_del_negative_fwd_entry (lgm, a);
234
235   dpref = &gid_address_ippref(&a->deid);
236   spref = &gid_address_ippref(&a->seid);
237   ip_ver = ip_prefix_version(dpref);
238
239   a->next_protocol = ip_ver == IP4 ?
240       LISP_GPE_NEXT_PROTO_IP4 : LISP_GPE_NEXT_PROTO_IP6;
241   a->flags |= LISP_GPE_FLAGS_P;
242
243   /* add/del tunnel to tunnels pool and prepares rewrite */
244   rv = add_del_tunnel (a, &tun_index);
245   if (rv)
246     return rv;
247
248   /* setup adjacency for eid */
249   memset (&adj, 0, sizeof(adj));
250   adj.n_adj = 1;
251   adj.explicit_fib_index = ~0;
252
253   if (a->is_add)
254     {
255       /* send packets that hit this adj to lisp-gpe interface output node in
256        * requested vrf. */
257       lnip = ip_ver == IP4 ?
258               lgm->lgpe_ip4_lookup_next_index_by_table_id :
259               lgm->lgpe_ip6_lookup_next_index_by_table_id;
260       lookup_next_index = hash_get(lnip, a->table_id);
261       lgpe_sw_if_index = hash_get(lgm->lisp_gpe_hw_if_index_by_table_id,
262                                   a->table_id);
263
264       /* the assumption is that the interface must've been created before
265        * programming the dp */
266       ASSERT(lookup_next_index != 0);
267       ASSERT(lgpe_sw_if_index != 0);
268
269       adj.lookup_next_index = lookup_next_index[0];
270       adj.rewrite_header.node_index = tun_index;
271       adj.rewrite_header.sw_if_index = lgpe_sw_if_index[0];
272     }
273
274   /* add/delete route for prefix */
275   rv = ip_sd_fib_add_del_route (lgm, dpref, spref, a->table_id, &adj,
276                                 a->is_add);
277
278   /* check that everything worked */
279   if (CLIB_DEBUG && a->is_add)
280     {
281       adj_index = ip_sd_fib_get_route (lgm, dpref, spref, a->table_id);
282       ASSERT(adj_index != 0);
283
284       adjp = ip_get_adjacency ((ip_ver == IP4) ? lgm->lm4 : lgm->lm6,
285                                adj_index);
286
287       ASSERT(adjp != 0);
288       ASSERT(adjp->rewrite_header.node_index == tun_index);
289     }
290
291   return rv;
292 }
293
294 static clib_error_t *
295 lisp_gpe_add_del_fwd_entry_command_fn (vlib_main_t * vm,
296                                        unformat_input_t * input,
297                                        vlib_cli_command_t * cmd)
298 {
299   unformat_input_t _line_input, * line_input = &_line_input;
300   u8 is_add = 1;
301   ip_address_t slocator, dlocator, *slocators = 0, *dlocators = 0;
302   ip_prefix_t * prefp;
303   gid_address_t * eids = 0, eid;
304   clib_error_t * error = 0;
305   u32 i;
306
307   prefp = &gid_address_ippref(&eid);
308
309   /* Get a line of input. */
310   if (! unformat_user (input, unformat_line_input, line_input))
311     return 0;
312
313   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
314     {
315       if (unformat (line_input, "del"))
316         is_add = 0;
317       else if (unformat (line_input, "add"))
318         is_add = 1;
319       else if (unformat (line_input, "eid %U slocator %U dlocator %U",
320                          unformat_ip_prefix, prefp,
321                          unformat_ip_address, &slocator,
322                          unformat_ip_address, &dlocator))
323         {
324           vec_add1 (eids, eid);
325           vec_add1 (slocators, slocator);
326           vec_add1 (dlocators, dlocator);
327         }
328       else
329         {
330           error = unformat_parse_error (line_input);
331           goto done;
332         }
333     }
334   unformat_free (line_input);
335
336   if (vec_len (eids) + vec_len (slocators) == 0)
337     {
338       error = clib_error_return (0, "expected ip4/ip6 eids/locators.");
339       goto done;
340     }
341
342   if (vec_len (eids) != vec_len (slocators))
343     {
344       error = clib_error_return (0, "number of eids not equal to that of "
345           "locators.");
346       goto done;
347     }
348
349   for (i = 0; i < vec_len(eids); i++)
350     {
351       vnet_lisp_gpe_add_del_fwd_entry_args_t a;
352       memset (&a, 0, sizeof(a));
353
354       a.is_add = is_add;
355       a.deid = eids[i];
356       a.slocator = slocators[i];
357       a.dlocator = dlocators[i];
358       prefp = &gid_address_ippref(&a.deid);
359       a.decap_next_index = (ip_prefix_version(prefp) == IP4) ?
360               LISP_GPE_INPUT_NEXT_IP4_INPUT : LISP_GPE_INPUT_NEXT_IP6_INPUT;
361       vnet_lisp_gpe_add_del_fwd_entry (&a, 0);
362     }
363
364  done:
365   vec_free(eids);
366   vec_free(slocators);
367   return error;
368 }
369
370 VLIB_CLI_COMMAND (add_del_lisp_gpe_mapping_tunnel_command, static) = {
371   .path = "lisp gpe maptunnel",
372   .short_help = "lisp gpe maptunnel eid <eid> sloc <src-locator> "
373       "dloc <dst-locator> [del]",
374   .function = lisp_gpe_add_del_fwd_entry_command_fn,
375 };
376
377 static u8 *
378 format_decap_next (u8 * s, va_list * args)
379 {
380   u32 next_index = va_arg (*args, u32);
381
382   switch (next_index)
383     {
384     case LISP_GPE_INPUT_NEXT_DROP:
385       return format (s, "drop");
386     case LISP_GPE_INPUT_NEXT_IP4_INPUT:
387       return format (s, "ip4");
388     case LISP_GPE_INPUT_NEXT_IP6_INPUT:
389       return format (s, "ip6");
390     default:
391       return format (s, "unknown %d", next_index);
392     }
393   return s;
394 }
395
396 u8 *
397 format_lisp_gpe_tunnel (u8 * s, va_list * args)
398 {
399   lisp_gpe_tunnel_t * t = va_arg (*args, lisp_gpe_tunnel_t *);
400   lisp_gpe_main_t * lgm = &lisp_gpe_main;
401
402   s = format (s,
403               "[%d] %U (src) %U (dst) fibs: encap %d, decap %d",
404               t - lgm->tunnels,
405               format_ip_address, &t->src,
406               format_ip_address, &t->dst,
407               t->encap_fib_index,
408               t->decap_fib_index);
409
410   s = format (s, " decap next %U\n", format_decap_next, t->decap_next_index);
411   s = format (s, "lisp ver %d ", (t->ver_res>>6));
412
413 #define _(n,v) if (t->flags & v) s = format (s, "%s-bit ", #n);
414   foreach_lisp_gpe_flag_bit;
415 #undef _
416
417   s = format (s, "next_protocol %d ver_res %x res %x\n",
418               t->next_protocol, t->ver_res, t->res);
419
420   s = format (s, "iid %d (0x%x)\n", t->vni, t->vni);
421   return s;
422 }
423
424 static clib_error_t *
425 show_lisp_gpe_tunnel_command_fn (vlib_main_t * vm,
426                                 unformat_input_t * input,
427                                 vlib_cli_command_t * cmd)
428 {
429   lisp_gpe_main_t * lgm = &lisp_gpe_main;
430   lisp_gpe_tunnel_t * t;
431   
432   if (pool_elts (lgm->tunnels) == 0)
433     vlib_cli_output (vm, "No lisp-gpe tunnels configured...");
434
435   pool_foreach (t, lgm->tunnels,
436   ({
437     vlib_cli_output (vm, "%U", format_lisp_gpe_tunnel, t);
438   }));
439   
440   return 0;
441 }
442
443 VLIB_CLI_COMMAND (show_lisp_gpe_tunnel_command, static) = {
444     .path = "show lisp gpe tunnel",
445     .function = show_lisp_gpe_tunnel_command_fn,
446 };
447
448 u8
449 vnet_lisp_gpe_enable_disable_status(void)
450 {
451   lisp_gpe_main_t * lgm = &lisp_gpe_main;
452
453   return lgm->is_en;
454 }
455
456 clib_error_t *
457 vnet_lisp_gpe_enable_disable (vnet_lisp_gpe_enable_disable_args_t * a)
458 {
459   lisp_gpe_main_t * lgm = &lisp_gpe_main;
460   vnet_main_t * vnm = lgm->vnet_main;
461
462   if (a->is_en)
463     {
464       /* add lgpe_ip4_lookup as possible next_node for ip4 lookup */
465       if (lgm->ip4_lookup_next_lgpe_ip4_lookup == ~0)
466         {
467           lgm->ip4_lookup_next_lgpe_ip4_lookup = vlib_node_add_next (
468               vnm->vlib_main, ip4_lookup_node.index,
469               lgpe_ip4_lookup_node.index);
470         }
471       /* add lgpe_ip6_lookup as possible next_node for ip6 lookup */
472       if (lgm->ip6_lookup_next_lgpe_ip6_lookup == ~0)
473         {
474           lgm->ip6_lookup_next_lgpe_ip6_lookup = vlib_node_add_next (
475               vnm->vlib_main, ip6_lookup_node.index,
476               lgpe_ip6_lookup_node.index);
477         }
478       else
479         {
480           /* ask cp to re-add ifaces and defaults */
481         }
482
483       lgm->is_en = 1;
484     }
485   else
486     {
487       CLIB_UNUSED(uword * val);
488       hash_pair_t * p;
489       u32 * table_ids = 0, * table_id;
490       lisp_gpe_tunnel_key_t * tunnels = 0, * tunnel;
491       vnet_lisp_gpe_add_del_fwd_entry_args_t _at, * at = &_at;
492       vnet_lisp_gpe_add_del_iface_args_t _ai, * ai= &_ai;
493
494       /* remove all tunnels */
495       mhash_foreach(tunnel, val, &lgm->lisp_gpe_tunnel_by_key, ({
496         vec_add1(tunnels, tunnel[0]);
497       }));
498
499       vec_foreach(tunnel, tunnels) {
500         memset(at, 0, sizeof(at[0]));
501         at->is_add = 0;
502         gid_address_type(&at->deid) = GID_ADDR_IP_PREFIX;
503         ip_prefix_copy(&gid_address_ippref(&at->deid), &tunnel->eid);
504         ip_address_copy(&at->dlocator, &tunnel->dst_loc);
505         vnet_lisp_gpe_add_del_fwd_entry (at, 0);
506       }
507       vec_free(tunnels);
508
509       /* disable all ifaces */
510       hash_foreach_pair(p, lgm->lisp_gpe_hw_if_index_by_table_id, ({
511         vec_add1(table_ids, p->key);
512       }));
513
514       vec_foreach(table_id, table_ids) {
515         ai->is_add = 0;
516         ai->table_id = table_id[0];
517
518         /* disables interface and removes defaults */
519         vnet_lisp_gpe_add_del_iface(ai, 0);
520       }
521       vec_free(table_ids);
522       lgm->is_en = 0;
523     }
524
525   return 0;
526 }
527
528 static clib_error_t *
529 lisp_gpe_enable_disable_command_fn (vlib_main_t * vm, unformat_input_t * input,
530                                     vlib_cli_command_t * cmd)
531 {
532   unformat_input_t _line_input, * line_input = &_line_input;
533   u8 is_en = 1;
534   vnet_lisp_gpe_enable_disable_args_t _a, * a = &_a;
535
536   /* Get a line of input. */
537   if (! unformat_user (input, unformat_line_input, line_input))
538     return 0;
539
540   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
541     {
542       if (unformat (line_input, "enable"))
543         is_en = 1;
544       else if (unformat (line_input, "disable"))
545         is_en = 0;
546       else
547         {
548           return clib_error_return (0, "parse error: '%U'",
549                                    format_unformat_error, line_input);
550         }
551     }
552   a->is_en = is_en;
553   return vnet_lisp_gpe_enable_disable (a);
554 }
555
556 VLIB_CLI_COMMAND (enable_disable_lisp_gpe_command, static) = {
557   .path = "lisp gpe",
558   .short_help = "lisp gpe [enable|disable]",
559   .function = lisp_gpe_enable_disable_command_fn,
560 };
561
562 clib_error_t *
563 lisp_gpe_init (vlib_main_t *vm)
564 {
565   lisp_gpe_main_t * lgm = &lisp_gpe_main;
566   clib_error_t * error = 0;
567
568   if ((error = vlib_call_init_function (vm, ip_main_init)))
569     return error;
570
571   if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
572     return error;
573
574   lgm->vnet_main = vnet_get_main();
575   lgm->vlib_main = vm;
576   lgm->im4 = &ip4_main;
577   lgm->im6 = &ip6_main;
578   lgm->lm4 = &ip4_main.lookup_main;
579   lgm->lm6 = &ip6_main.lookup_main;
580   lgm->ip4_lookup_next_lgpe_ip4_lookup = ~0;
581   lgm->ip6_lookup_next_lgpe_ip6_lookup = ~0;
582
583   mhash_init (&lgm->lisp_gpe_tunnel_by_key, sizeof(uword),
584               sizeof(lisp_gpe_tunnel_key_t));
585
586   udp_register_dst_port (vm, UDP_DST_PORT_lisp_gpe, 
587                          lisp_gpe_ip4_input_node.index, 1 /* is_ip4 */);
588   udp_register_dst_port (vm, UDP_DST_PORT_lisp_gpe6,
589                          lisp_gpe_ip6_input_node.index, 0 /* is_ip4 */);
590   return 0;
591 }
592
593 VLIB_INIT_FUNCTION(lisp_gpe_init);