VPP-239 Tag packets that hit LISP negative adjacencies
[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 #define foreach_copy_field                      \
95 _(encap_fib_index)                              \
96 _(decap_fib_index)                              \
97 _(decap_next_index)                             \
98 _(vni)                                          \
99 _(action)
100
101 static int
102 add_del_ip_tunnel (vnet_lisp_gpe_add_del_fwd_entry_args_t *a, u8 is_l2,
103                    u32 * tun_index_res)
104 {
105   lisp_gpe_main_t * lgm = &lisp_gpe_main;
106   lisp_gpe_tunnel_t *t = 0;
107   lisp_gpe_tunnel_key_t key;
108   uword * p;
109   int rv;
110
111   /* prepare tunnel key */
112   memset(&key, 0, sizeof(key));
113
114   /* fill in the key's remote eid */
115   if (!is_l2)
116     ip_prefix_copy (&key.rmt.ippref, &gid_address_ippref(&a->rmt_eid));
117   else
118     mac_copy (&key.rmt.mac, &gid_address_mac(&a->rmt_eid));
119
120   key.vni = clib_host_to_net_u32 (a->vni);
121
122   p = mhash_get (&lgm->lisp_gpe_tunnel_by_key, &key);
123
124   if (a->is_add)
125     {
126       /* adding a tunnel: tunnel must not already exist */
127       if (p)
128         return VNET_API_ERROR_INVALID_VALUE;
129
130       if (a->decap_next_index >= LISP_GPE_INPUT_N_NEXT)
131         return VNET_API_ERROR_INVALID_DECAP_NEXT;
132
133       pool_get_aligned (lgm->tunnels, t, CLIB_CACHE_LINE_BYTES);
134       memset (t, 0, sizeof (*t));
135
136       /* copy from arg structure */
137 #define _(x) t->x = a->x;
138       foreach_copy_field;
139 #undef _
140
141       /* TODO multihoming */
142       if (!a->is_negative)
143         {
144           ip_address_copy (&t->src, &a->locator_pairs[0].lcl_loc);
145           ip_address_copy (&t->dst, &a->locator_pairs[0].rmt_loc);
146         }
147
148       /* if vni is non-default */
149       if (a->vni)
150         t->flags = LISP_GPE_FLAGS_I;
151
152       /* work in lisp-gpe not legacy mode */
153       t->flags |= LISP_GPE_FLAGS_P;
154
155       /* next proto */
156       if (!is_l2)
157         t->next_protocol = ip_prefix_version(&key.rmt.ippref) == IP4 ?
158                 LISP_GPE_NEXT_PROTO_IP4 : LISP_GPE_NEXT_PROTO_IP6;
159       else
160         t->next_protocol = LISP_GPE_NEXT_PROTO_ETHERNET;
161
162       /* compute rewrite */
163       rv = lisp_gpe_rewrite (t);
164
165       if (rv)
166         {
167           pool_put(lgm->tunnels, t);
168           return rv;
169         }
170
171       mhash_set(&lgm->lisp_gpe_tunnel_by_key, &key, t - lgm->tunnels, 0);
172
173       /* return tunnel index */
174       if (tun_index_res)
175         tun_index_res[0] = t - lgm->tunnels;
176     }
177   else
178     {
179       /* deleting a tunnel: tunnel must exist */
180       if (!p)
181         {
182           clib_warning("Tunnel for eid %U doesn't exist!", format_gid_address,
183                        &a->rmt_eid);
184           return VNET_API_ERROR_NO_SUCH_ENTRY;
185         }
186
187       t = pool_elt_at_index(lgm->tunnels, p[0]);
188
189       mhash_unset(&lgm->lisp_gpe_tunnel_by_key, &key, 0);
190
191       vec_free(t->rewrite);
192       pool_put(lgm->tunnels, t);
193     }
194
195   return 0;
196 }
197
198 static int
199 build_ip_adjacency (lisp_gpe_main_t * lgm, ip_adjacency_t * adj, u32 table_id,
200                     u32 vni, u32 tun_index, u8 is_negative, u8 action,
201                     u8 ip_ver)
202 {
203   uword * lookup_next_index, * lgpe_sw_if_index, * lnip;
204
205   memset(adj, 0, sizeof(adj[0]));
206   adj->n_adj = 1;
207   /* fill in lookup_next_index with a 'legal' value to avoid problems */
208   adj->lookup_next_index = (ip_ver == IP4) ?
209           lgm->ip4_lookup_next_lgpe_ip4_lookup :
210           lgm->ip6_lookup_next_lgpe_ip6_lookup;
211
212   /* positive mapping */
213   if (!is_negative)
214     {
215       /* send packets that hit this adj to lisp-gpe interface output node in
216        * requested vrf. */
217       lnip = (ip_ver == IP4) ?
218               lgm->lgpe_ip4_lookup_next_index_by_table_id :
219               lgm->lgpe_ip6_lookup_next_index_by_table_id;
220       lookup_next_index = hash_get(lnip, table_id);
221       lgpe_sw_if_index = hash_get(lgm->l3_ifaces.sw_if_index_by_vni, vni);
222
223       /* the assumption is that the interface must've been created before
224        * programming the dp */
225       ASSERT(lookup_next_index != 0);
226       ASSERT(lgpe_sw_if_index != 0);
227
228       /* hijack explicit fib index to store lisp interface node index and
229        * if_address_index for the tunnel index */
230       adj->explicit_fib_index = lookup_next_index[0];
231       adj->if_address_index = tun_index;
232       adj->rewrite_header.sw_if_index = lgpe_sw_if_index[0];
233     }
234   /* negative mapping */
235   else
236     {
237       adj->rewrite_header.sw_if_index = ~0;
238       adj->rewrite_header.next_index = ~0;
239       adj->if_address_index = tun_index;
240
241       switch (action)
242         {
243         case LISP_NO_ACTION:
244           /* TODO update timers? */
245         case LISP_FORWARD_NATIVE:
246           /* TODO check if route/next-hop for eid exists in fib and add
247            * more specific for the eid with the next-hop found */
248         case LISP_SEND_MAP_REQUEST:
249           /* insert tunnel that always sends map-request */
250           adj->explicit_fib_index = (ip_ver == IP4) ?
251                                    LGPE_IP4_LOOKUP_NEXT_LISP_CP_LOOKUP:
252                                    LGPE_IP6_LOOKUP_NEXT_LISP_CP_LOOKUP;
253           break;
254         case LISP_DROP:
255           /* for drop fwd entries, just add route, no need to add encap tunnel */
256           adj->explicit_fib_index =  (ip_ver == IP4 ?
257                   LGPE_IP4_LOOKUP_NEXT_DROP : LGPE_IP6_LOOKUP_NEXT_DROP);
258           break;
259         default:
260           return -1;
261         }
262     }
263   return 0;
264 }
265
266 static int
267 add_del_ip_fwd_entry (lisp_gpe_main_t * lgm,
268                       vnet_lisp_gpe_add_del_fwd_entry_args_t * a)
269 {
270   ip_adjacency_t adj, * adjp;
271   u32 rv, tun_index = ~0;
272   ip_prefix_t * rmt_pref, * lcl_pref;
273   u8 ip_ver;
274
275   rmt_pref = &gid_address_ippref(&a->rmt_eid);
276   lcl_pref = &gid_address_ippref(&a->lcl_eid);
277   ip_ver = ip_prefix_version(rmt_pref);
278
279   /* add/del tunnel to tunnels pool and prepares rewrite */
280   if (0 != a->locator_pairs)
281     {
282       rv = add_del_ip_tunnel (a, 0 /* is_l2 */, &tun_index);
283       if (rv)
284         {
285           clib_warning ("failed to build tunnel!");
286           return rv;
287         }
288     }
289
290   /* setup adjacency for eid */
291   rv = build_ip_adjacency (lgm, &adj, a->table_id, a->vni, tun_index,
292                            a->is_negative, a->action, ip_ver);
293
294   /* add/delete route for eid */
295   rv |= ip_sd_fib_add_del_route (lgm, rmt_pref, lcl_pref, a->table_id, &adj,
296                                 a->is_add);
297
298   if (rv)
299     {
300       clib_warning ("failed to insert route for tunnel!");
301       return rv;
302     }
303
304   /* check that everything worked */
305   if (CLIB_DEBUG && a->is_add)
306     {
307       u32 adj_index;
308       adj_index = ip_sd_fib_get_route (lgm, rmt_pref, lcl_pref, a->table_id);
309       ASSERT(adj_index != 0);
310
311       adjp = ip_get_adjacency ((ip_ver == IP4) ? lgm->lm4 : lgm->lm6,
312                                adj_index);
313
314       ASSERT(adjp != 0 && adjp->if_address_index == tun_index);
315     }
316
317   return rv;
318 }
319
320 static void
321 make_mac_fib_key (BVT(clib_bihash_kv) *kv, u16 bd_index, u8 src_mac[6],
322                   u8 dst_mac[6])
323 {
324   kv->key[0] = (((u64) bd_index) << 48) | (((u64 *)dst_mac)[0] & MAC_BIT_MASK);
325   kv->key[1] = ((u64 *)src_mac)[0] & MAC_BIT_MASK;
326   kv->key[2] = 0;
327 }
328
329 u32
330 lisp_l2_fib_lookup (lisp_gpe_main_t * lgm, u16 bd_index, u8 src_mac[6],
331                     u8 dst_mac[6])
332 {
333   int rv;
334   BVT(clib_bihash_kv) kv, value;
335
336   make_mac_fib_key(&kv, bd_index, src_mac, dst_mac);
337   rv = BV(clib_bihash_search_inline_2) (&lgm->l2_fib, &kv, &value);
338
339   /* no match, try with src 0, catch all for dst */
340   if (rv != 0)
341     {
342       kv.key[1] = 0;
343       rv = BV(clib_bihash_search_inline_2) (&lgm->l2_fib, &kv, &value);
344       if (rv == 0)
345         return value.value;
346     }
347
348   return ~0;
349 }
350
351 u32
352 lisp_l2_fib_add_del_entry (lisp_gpe_main_t * lgm, u16 bd_index, u8 src_mac[6],
353                            u8 dst_mac[6], u32 val, u8 is_add)
354 {
355   BVT(clib_bihash_kv) kv, value;
356   u32 old_val = ~0;
357
358   make_mac_fib_key(&kv, bd_index, src_mac, dst_mac);
359
360   if (BV(clib_bihash_search) (&lgm->l2_fib, &kv, &value) == 0)
361     old_val = value.value;
362
363   if (!is_add)
364     BV(clib_bihash_add_del) (&lgm->l2_fib, &kv, 0 /* is_add */);
365   else
366     {
367       kv.value = val;
368       BV(clib_bihash_add_del) (&lgm->l2_fib, &kv, 1 /* is_add */);
369     }
370   return old_val;
371 }
372
373 static void
374 l2_fib_init (lisp_gpe_main_t * lgm)
375 {
376   BV(clib_bihash_init) (&lgm->l2_fib, "l2 fib",
377                          1 << max_log2 (L2_FIB_DEFAULT_HASH_NUM_BUCKETS),
378                          L2_FIB_DEFAULT_HASH_MEMORY_SIZE);
379 }
380
381 static int
382 add_del_l2_fwd_entry (lisp_gpe_main_t * lgm,
383                       vnet_lisp_gpe_add_del_fwd_entry_args_t * a)
384 {
385   int rv;
386   u32 tun_index;
387   bd_main_t * bdm = &bd_main;
388   uword * bd_indexp;
389
390   /* create tunnel */
391   rv = add_del_ip_tunnel (a, 1 /* is_l2 */, &tun_index);
392   if (rv)
393     return rv;
394
395   bd_indexp = hash_get(bdm->bd_index_by_bd_id, a->bd_id);
396   if (!bd_indexp)
397     {
398       clib_warning("bridge domain %d doesn't exist", a->bd_id);
399       return -1;
400     }
401
402   /* add entry to l2 lisp fib */
403   lisp_l2_fib_add_del_entry (lgm, bd_indexp[0], gid_address_mac(&a->lcl_eid),
404                              gid_address_mac(&a->rmt_eid), tun_index,
405                              a->is_add);
406   return 0;
407 }
408
409
410 int
411 vnet_lisp_gpe_add_del_fwd_entry (vnet_lisp_gpe_add_del_fwd_entry_args_t * a,
412                                  u32 * hw_if_indexp)
413 {
414   lisp_gpe_main_t * lgm = &lisp_gpe_main;
415   u8 type;
416
417   if (vnet_lisp_gpe_enable_disable_status () == 0)
418     {
419       clib_warning ("LISP is disabled!");
420       return VNET_API_ERROR_LISP_DISABLED;
421     }
422
423   type = gid_address_type(&a->rmt_eid);
424   switch (type)
425     {
426     case GID_ADDR_IP_PREFIX:
427       return add_del_ip_fwd_entry (lgm, a);
428     case GID_ADDR_MAC:
429       return add_del_l2_fwd_entry (lgm, a);
430     default:
431       clib_warning("Forwarding entries for type %d not supported!", type);
432       return -1;
433     }
434 }
435
436 static clib_error_t *
437 lisp_gpe_add_del_fwd_entry_command_fn (vlib_main_t * vm,
438                                        unformat_input_t * input,
439                                        vlib_cli_command_t * cmd)
440 {
441   unformat_input_t _line_input, * line_input = &_line_input;
442   u8 is_add = 1;
443   ip_address_t lloc, rloc;
444   clib_error_t * error = 0;
445   gid_address_t _reid, * reid = &_reid, _leid, * leid = &_leid;
446   u8 reid_set = 0, leid_set = 0, is_negative = 0, vrf_set = 0, vni_set = 0;
447   u32 vni, vrf, action = ~0, p, w;
448   locator_pair_t pair, * pairs = 0;
449   int rv;
450
451   /* Get a line of input. */
452   if (! unformat_user (input, unformat_line_input, line_input))
453     return 0;
454
455   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
456     {
457       if (unformat (line_input, "del"))
458         is_add = 0;
459       else if (unformat (line_input, "add"))
460         is_add = 1;
461       else if (unformat (line_input, "leid %U",
462                          unformat_gid_address, leid))
463         {
464           leid_set = 1;
465         }
466       else if (unformat (line_input, "reid %U",
467                          unformat_gid_address, reid))
468         {
469           reid_set = 1;
470         }
471       else if (unformat (line_input, "vni %u", &vni))
472         {
473           gid_address_vni (leid) = vni;
474           gid_address_vni (reid) = vni;
475           vni_set = 1;
476         }
477       else if (unformat (line_input, "vrf %u", &vrf))
478         {
479           vrf_set = 1;
480         }
481       else if (unformat (line_input, "negative action %U",
482                          unformat_negative_mapping_action, &action))
483         {
484           is_negative = 1;
485         }
486       else if (unformat (line_input, "loc-pair %U %U p %d w %d",
487                          unformat_ip_address, &lloc,
488                          unformat_ip_address, &rloc, &p, &w))
489         {
490           pair.lcl_loc = lloc;
491           pair.rmt_loc = rloc;
492           pair.priority = p;
493           pair.weight = w;
494           vec_add1(pairs, pair);
495         }
496       else
497         {
498           error = unformat_parse_error (line_input);
499           goto done;
500         }
501     }
502   unformat_free (line_input);
503
504   if (!vni_set || !vrf_set)
505     {
506       error = clib_error_return(0, "vni and vrf must be set!");
507       goto done;
508     }
509
510   if (!reid_set)
511     {
512       error = clib_error_return(0, "remote eid must be set!");
513       goto done;
514     }
515
516   if (is_negative)
517     {
518       if (~0 == action)
519         {
520           error = clib_error_return(0, "no action set for negative tunnel!");
521           goto done;
522         }
523     }
524   else
525     {
526       if (vec_len (pairs) == 0)
527         {
528           error = clib_error_return (0, "expected ip4/ip6 locators.");
529           goto done;
530         }
531     }
532
533
534
535   if (!leid_set)
536     {
537       /* if leid not set, make sure it's the same AFI like reid */
538       gid_address_type(leid) = gid_address_type(reid);
539       if (GID_ADDR_IP_PREFIX == gid_address_type (reid))
540         gid_address_ip_version(leid) = gid_address_ip_version(reid);
541     }
542
543   /* add fwd entry */
544   vnet_lisp_gpe_add_del_fwd_entry_args_t _a, * a = &_a;
545   memset (a, 0, sizeof(a[0]));
546
547   a->is_add = is_add;
548   a->vni = vni;
549   a->table_id = vrf;
550   gid_address_copy(&a->lcl_eid, leid);
551   gid_address_copy(&a->rmt_eid, reid);
552   a->locator_pairs = pairs;
553
554   rv = vnet_lisp_gpe_add_del_fwd_entry (a, 0);
555   if (0 != rv)
556     {
557       error = clib_error_return(0, "failed to %s gpe tunnel!",
558                                 is_add ? "add" : "delete");
559     }
560
561  done:
562   vec_free(pairs);
563   return error;
564 }
565
566 VLIB_CLI_COMMAND (lisp_gpe_add_del_fwd_entry_command, static) = {
567   .path = "lisp gpe tunnel",
568   .short_help = "lisp gpe tunnel add/del vni <vni> vrf <vrf> [leid <leid>]"
569       "reid <reid> [lloc <sloc> rloc <rloc>] [negative action <action>]",
570   .function = lisp_gpe_add_del_fwd_entry_command_fn,
571 };
572
573 static u8 *
574 format_decap_next (u8 * s, va_list * args)
575 {
576   u32 next_index = va_arg (*args, u32);
577
578   switch (next_index)
579     {
580     case LISP_GPE_INPUT_NEXT_DROP:
581       return format (s, "drop");
582     case LISP_GPE_INPUT_NEXT_IP4_INPUT:
583       return format (s, "ip4");
584     case LISP_GPE_INPUT_NEXT_IP6_INPUT:
585       return format (s, "ip6");
586     default:
587       return format (s, "unknown %d", next_index);
588     }
589   return s;
590 }
591
592 u8 *
593 format_lisp_gpe_tunnel (u8 * s, va_list * args)
594 {
595   lisp_gpe_tunnel_t * t = va_arg (*args, lisp_gpe_tunnel_t *);
596   lisp_gpe_main_t * lgm = &lisp_gpe_main;
597
598   s = format (s,
599               "[%d] %U (src) %U (dst) fibs: encap %d, decap %d",
600               t - lgm->tunnels,
601               format_ip_address, &t->src,
602               format_ip_address, &t->dst,
603               t->encap_fib_index,
604               t->decap_fib_index);
605
606   s = format (s, " decap next %U\n", format_decap_next, t->decap_next_index);
607   s = format (s, "lisp ver %d ", (t->ver_res>>6));
608
609 #define _(n,v) if (t->flags & v) s = format (s, "%s-bit ", #n);
610   foreach_lisp_gpe_flag_bit;
611 #undef _
612
613   s = format (s, "next_protocol %d ver_res %x res %x\n",
614               t->next_protocol, t->ver_res, t->res);
615
616   s = format (s, "iid %d (0x%x)\n", t->vni, t->vni);
617   return s;
618 }
619
620 static clib_error_t *
621 show_lisp_gpe_tunnel_command_fn (vlib_main_t * vm,
622                                 unformat_input_t * input,
623                                 vlib_cli_command_t * cmd)
624 {
625   lisp_gpe_main_t * lgm = &lisp_gpe_main;
626   lisp_gpe_tunnel_t * t;
627   
628   if (pool_elts (lgm->tunnels) == 0)
629     vlib_cli_output (vm, "No lisp-gpe tunnels configured...");
630
631   pool_foreach (t, lgm->tunnels,
632   ({
633     vlib_cli_output (vm, "%U", format_lisp_gpe_tunnel, t);
634   }));
635   
636   return 0;
637 }
638
639 VLIB_CLI_COMMAND (show_lisp_gpe_tunnel_command, static) = {
640     .path = "show lisp gpe tunnel",
641     .function = show_lisp_gpe_tunnel_command_fn,
642 };
643
644 u8
645 vnet_lisp_gpe_enable_disable_status(void)
646 {
647   lisp_gpe_main_t * lgm = &lisp_gpe_main;
648
649   return lgm->is_en;
650 }
651
652 clib_error_t *
653 vnet_lisp_gpe_enable_disable (vnet_lisp_gpe_enable_disable_args_t * a)
654 {
655   lisp_gpe_main_t * lgm = &lisp_gpe_main;
656   vnet_main_t * vnm = lgm->vnet_main;
657
658   if (a->is_en)
659     {
660       /* add lgpe_ip4_lookup as possible next_node for ip4 lookup */
661       if (lgm->ip4_lookup_next_lgpe_ip4_lookup == ~0)
662         {
663           lgm->ip4_lookup_next_lgpe_ip4_lookup = vlib_node_add_next (
664               vnm->vlib_main, ip4_lookup_node.index,
665               lgpe_ip4_lookup_node.index);
666         }
667       /* add lgpe_ip6_lookup as possible next_node for ip6 lookup */
668       if (lgm->ip6_lookup_next_lgpe_ip6_lookup == ~0)
669         {
670           lgm->ip6_lookup_next_lgpe_ip6_lookup = vlib_node_add_next (
671               vnm->vlib_main, ip6_lookup_node.index,
672               lgpe_ip6_lookup_node.index);
673         }
674       else
675         {
676           /* ask cp to re-add ifaces and defaults */
677         }
678
679       lgm->is_en = 1;
680     }
681   else
682     {
683       CLIB_UNUSED(uword * val);
684       hash_pair_t * p;
685       u32 * dp_tables = 0, * dp_table;
686       lisp_gpe_tunnel_key_t * tunnels = 0, * tunnel;
687       vnet_lisp_gpe_add_del_fwd_entry_args_t _at, * at = &_at;
688       vnet_lisp_gpe_add_del_iface_args_t _ai, * ai= &_ai;
689
690       /* remove all tunnels */
691       mhash_foreach(tunnel, val, &lgm->lisp_gpe_tunnel_by_key, ({
692         vec_add1(tunnels, tunnel[0]);
693       }));
694
695       vec_foreach(tunnel, tunnels)
696       {
697         memset(at, 0, sizeof(at[0]));
698         at->is_add = 0;
699         if (tunnel->rmt.type == GID_ADDR_IP_PREFIX)
700           {
701             gid_address_type(&at->rmt_eid) = GID_ADDR_IP_PREFIX;
702             ip_prefix_copy(&gid_address_ippref(&at->rmt_eid), &tunnel->rmt.ippref);
703           }
704         else
705           {
706             gid_address_type(&at->rmt_eid) = GID_ADDR_MAC;
707             mac_copy(&gid_address_mac(&at->rmt_eid), &tunnel->rmt.mac);
708           }
709         vnet_lisp_gpe_add_del_fwd_entry (at, 0);
710       }
711       vec_free(tunnels);
712
713       /* disable all l3 ifaces */
714       hash_foreach_pair(p, lgm->l3_ifaces.hw_if_index_by_dp_table, ({
715         vec_add1(dp_tables, p->key);
716       }));
717
718       vec_foreach(dp_table, dp_tables) {
719         ai->is_add = 0;
720         ai->table_id = dp_table[0];
721         ai->is_l2 = 0;
722
723         /* disables interface and removes defaults */
724         vnet_lisp_gpe_add_del_iface(ai, 0);
725       }
726
727       /* disable all l2 ifaces */
728       _vec_len(dp_tables) = 0;
729       hash_foreach_pair(p, lgm->l2_ifaces.hw_if_index_by_dp_table, ({
730         vec_add1(dp_tables, p->key);
731       }));
732
733       vec_foreach(dp_table, dp_tables) {
734         ai->is_add = 0;
735         ai->bd_id = dp_table[0];
736         ai->is_l2 = 1;
737
738         /* disables interface and removes defaults */
739         vnet_lisp_gpe_add_del_iface(ai, 0);
740       }
741
742       vec_free(dp_tables);
743       lgm->is_en = 0;
744     }
745
746   return 0;
747 }
748
749 static clib_error_t *
750 lisp_gpe_enable_disable_command_fn (vlib_main_t * vm, unformat_input_t * input,
751                                     vlib_cli_command_t * cmd)
752 {
753   unformat_input_t _line_input, * line_input = &_line_input;
754   u8 is_en = 1;
755   vnet_lisp_gpe_enable_disable_args_t _a, * a = &_a;
756
757   /* Get a line of input. */
758   if (! unformat_user (input, unformat_line_input, line_input))
759     return 0;
760
761   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
762     {
763       if (unformat (line_input, "enable"))
764         is_en = 1;
765       else if (unformat (line_input, "disable"))
766         is_en = 0;
767       else
768         {
769           return clib_error_return (0, "parse error: '%U'",
770                                    format_unformat_error, line_input);
771         }
772     }
773   a->is_en = is_en;
774   return vnet_lisp_gpe_enable_disable (a);
775 }
776
777 VLIB_CLI_COMMAND (enable_disable_lisp_gpe_command, static) = {
778   .path = "lisp gpe",
779   .short_help = "lisp gpe [enable|disable]",
780   .function = lisp_gpe_enable_disable_command_fn,
781 };
782
783 static clib_error_t *
784 lisp_show_iface_command_fn (vlib_main_t * vm,
785                             unformat_input_t * input,
786                             vlib_cli_command_t * cmd)
787 {
788   lisp_gpe_main_t * lgm = &lisp_gpe_main;
789   hash_pair_t * p;
790
791   vlib_cli_output (vm, "%=10s%=12s", "vrf", "hw_if_index");
792   hash_foreach_pair (p, lgm->l3_ifaces.hw_if_index_by_dp_table, ({
793     vlib_cli_output (vm, "%=10d%=10d", p->key, p->value[0]);
794   }));
795
796   if (0 != lgm->l2_ifaces.hw_if_index_by_dp_table)
797     {
798       vlib_cli_output (vm, "%=10s%=12s", "bd_id", "hw_if_index");
799       hash_foreach_pair (p, lgm->l2_ifaces.hw_if_index_by_dp_table, ({
800         vlib_cli_output (vm, "%=10d%=10d", p->key, p->value[0]);
801       }));
802     }
803   return 0;
804 }
805
806 VLIB_CLI_COMMAND (lisp_show_iface_command) = {
807     .path = "show lisp gpe interface",
808     .short_help = "show lisp gpe interface",
809     .function = lisp_show_iface_command_fn,
810 };
811
812
813 u8 *
814 format_vnet_lisp_gpe_status (u8 * s, va_list * args)
815 {
816   lisp_gpe_main_t * lgm = &lisp_gpe_main;
817   return format (s, "%s", lgm->is_en ? "enabled" : "disabled");
818 }
819
820 clib_error_t *
821 lisp_gpe_init (vlib_main_t *vm)
822 {
823   lisp_gpe_main_t * lgm = &lisp_gpe_main;
824   clib_error_t * error = 0;
825
826   if ((error = vlib_call_init_function (vm, ip_main_init)))
827     return error;
828
829   if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
830     return error;
831
832   lgm->vnet_main = vnet_get_main();
833   lgm->vlib_main = vm;
834   lgm->im4 = &ip4_main;
835   lgm->im6 = &ip6_main;
836   lgm->lm4 = &ip4_main.lookup_main;
837   lgm->lm6 = &ip6_main.lookup_main;
838   lgm->ip4_lookup_next_lgpe_ip4_lookup = ~0;
839   lgm->ip6_lookup_next_lgpe_ip6_lookup = ~0;
840
841   mhash_init (&lgm->lisp_gpe_tunnel_by_key, sizeof(uword),
842               sizeof(lisp_gpe_tunnel_key_t));
843
844   l2_fib_init (lgm);
845
846   udp_register_dst_port (vm, UDP_DST_PORT_lisp_gpe, 
847                          lisp_gpe_ip4_input_node.index, 1 /* is_ip4 */);
848   udp_register_dst_port (vm, UDP_DST_PORT_lisp_gpe6,
849                          lisp_gpe_ip6_input_node.index, 0 /* is_ip4 */);
850   return 0;
851 }
852
853 VLIB_INIT_FUNCTION(lisp_gpe_init);