LISP GPE: initial CP commit and DP improvements
[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 /* avoids calling route callbacks for src fib */
21 static void
22 ip4_sd_fib_set_adj_index (lisp_gpe_main_t * lgm, ip4_fib_t * fib, u32 flags,
23                            u32 dst_address_u32, u32 dst_address_length,
24                            u32 adj_index)
25 {
26   ip_lookup_main_t * lm = lgm->lookup_main;
27   uword * hash;
28
29   if (vec_bytes(fib->old_hash_values))
30     memset (fib->old_hash_values, ~0, vec_bytes (fib->old_hash_values));
31   if (vec_bytes(fib->new_hash_values))
32     memset (fib->new_hash_values, ~0, vec_bytes (fib->new_hash_values));
33   fib->new_hash_values[0] = adj_index;
34
35   /* Make sure adj index is valid. */
36   if (CLIB_DEBUG > 0)
37     (void) ip_get_adjacency (lm, adj_index);
38
39   hash = fib->adj_index_by_dst_address[dst_address_length];
40
41   hash = _hash_set3 (hash, dst_address_u32,
42                      fib->new_hash_values,
43                      fib->old_hash_values);
44
45   fib->adj_index_by_dst_address[dst_address_length] = hash;
46 }
47
48 /* copied from ip4_forward since it's static */
49 static void
50 ip4_fib_init_adj_index_by_dst_address (ip_lookup_main_t * lm,
51                                        ip4_fib_t * fib,
52                                        u32 address_length)
53 {
54   hash_t * h;
55   uword max_index;
56
57   ASSERT (lm->fib_result_n_bytes >= sizeof (uword));
58   lm->fib_result_n_words = round_pow2 (lm->fib_result_n_bytes, sizeof (uword)) / sizeof (uword);
59
60   fib->adj_index_by_dst_address[address_length] =
61     hash_create (32 /* elts */, lm->fib_result_n_words * sizeof (uword));
62
63   hash_set_flags (fib->adj_index_by_dst_address[address_length],
64                   HASH_FLAG_NO_AUTO_SHRINK);
65
66   h = hash_header (fib->adj_index_by_dst_address[address_length]);
67   max_index = (hash_value_bytes (h) / sizeof (fib->new_hash_values[0])) - 1;
68
69   /* Initialize new/old hash value vectors. */
70   vec_validate_init_empty (fib->new_hash_values, max_index, ~0);
71   vec_validate_init_empty (fib->old_hash_values, max_index, ~0);
72 }
73
74 void
75 ip4_sd_fib_add_del_src_route (lisp_gpe_main_t * lgm,
76                               ip4_add_del_route_args_t * a)
77 {
78   ip_lookup_main_t * lm = lgm->lookup_main;
79   ip4_fib_t * fib;
80   u32 dst_address, dst_address_length, adj_index, old_adj_index;
81   uword * hash, is_del;
82
83   /* Either create new adjacency or use given one depending on arguments. */
84   if (a->n_add_adj > 0)
85       ip_add_adjacency (lm, a->add_adj, a->n_add_adj, &adj_index);
86   else
87     adj_index = a->adj_index;
88
89   dst_address = a->dst_address.data_u32;
90   dst_address_length = a->dst_address_length;
91
92   fib = pool_elt_at_index(lgm->src_fibs, a->table_index_or_table_id);
93
94   if (! fib->adj_index_by_dst_address[dst_address_length])
95     ip4_fib_init_adj_index_by_dst_address (lm, fib, dst_address_length);
96
97   hash = fib->adj_index_by_dst_address[dst_address_length];
98
99   is_del = (a->flags & IP4_ROUTE_FLAG_DEL) != 0;
100
101   if (is_del)
102     {
103       fib->old_hash_values[0] = ~0;
104       hash = _hash_unset (hash, dst_address, fib->old_hash_values);
105       fib->adj_index_by_dst_address[dst_address_length] = hash;
106     }
107   else
108     ip4_sd_fib_set_adj_index (lgm, fib, a->flags, dst_address,
109                               dst_address_length, adj_index);
110
111   old_adj_index = fib->old_hash_values[0];
112
113   ip4_fib_mtrie_add_del_route (fib, a->dst_address, dst_address_length,
114                                is_del ? old_adj_index : adj_index,
115                                is_del);
116
117   /* Delete old adjacency index if present and changed. */
118   if (! (a->flags & IP4_ROUTE_FLAG_KEEP_OLD_ADJACENCY)
119       && old_adj_index != ~0
120       && old_adj_index != adj_index)
121     ip_del_adjacency (lm, old_adj_index);
122 }
123
124 void *
125 ip4_sd_get_src_route (lisp_gpe_main_t * lgm, u32 src_fib_index,
126                       ip4_address_t * src, u32 address_length)
127 {
128   ip4_fib_t * fib = pool_elt_at_index (lgm->src_fibs, src_fib_index);
129   uword * hash, * p;
130
131   hash = fib->adj_index_by_dst_address[address_length];
132   p = hash_get (hash, src->as_u32);
133   return (void *) p;
134 }
135
136 typedef CLIB_PACKED (struct {
137   ip4_address_t address;
138   u32 address_length : 6;
139   u32 index : 26;
140 }) ip4_route_t;
141
142 static void
143 ip4_sd_fib_clear_src_fib (lisp_gpe_main_t * lgm, ip4_fib_t * fib)
144 {
145   ip4_route_t * routes = 0, * r;
146   u32 i;
147
148   vec_reset_length (routes);
149
150   for (i = 0; i < ARRAY_LEN (fib->adj_index_by_dst_address); i++) {
151       uword * hash = fib->adj_index_by_dst_address[i];
152       hash_pair_t * p;
153       ip4_route_t x;
154
155       x.address_length = i;
156
157       hash_foreach_pair (p, hash,
158       ({
159           x.address.data_u32 = p->key;
160           vec_add1 (routes, x);
161       }));
162   }
163
164   vec_foreach (r, routes) {
165       ip4_add_del_route_args_t a;
166
167       memset (&a, 0, sizeof (a));
168       a.flags = IP4_ROUTE_FLAG_FIB_INDEX | IP4_ROUTE_FLAG_DEL;
169       a.table_index_or_table_id = fib - lgm->src_fibs;
170       a.dst_address = r->address;
171       a.dst_address_length = r->address_length;
172       a.adj_index = ~0;
173
174       ip4_sd_fib_add_del_src_route (lgm, &a);
175   }
176 }
177
178 int
179 ip4_sd_fib_add_del_route (lisp_gpe_main_t * lgm, ip_prefix_t * dst_prefix,
180                           ip_prefix_t * src_prefix, u32 table_index,
181                           ip_adjacency_t * add_adj, u8 is_add)
182 {
183   uword * p;
184   ip4_add_del_route_args_t a;
185   ip_adjacency_t * dst_adjp, dst_adj;
186   ip4_address_t dst = ip_prefix_v4(dst_prefix), src;
187   u32 dst_address_length = ip_prefix_len(dst_prefix), src_address_length = 0;
188   ip4_fib_t * src_fib;
189
190   if (src_prefix)
191     {
192       src = ip_prefix_v4(src_prefix);
193       src_address_length = ip_prefix_len(src_prefix);
194     }
195   else
196     memset(&src, 0, sizeof(src));
197
198   /* lookup dst adj */
199   p = ip4_get_route (lgm->im4, table_index, 0, dst.as_u8, dst_address_length);
200
201   if (is_add)
202     {
203       /* insert dst prefix to ip4 fib, if it's not in yet */
204       if (p == 0)
205         {
206           /* dst adj should point to lisp gpe lookup */
207           dst_adj = add_adj[0];
208           dst_adj.lookup_next_index = lgm->ip4_lookup_next_lgpe_ip4_lookup;
209
210           memset(&a, 0, sizeof(a));
211           a.flags = IP4_ROUTE_FLAG_TABLE_ID;
212           a.table_index_or_table_id = table_index; /* vrf */
213           a.adj_index = ~0;
214           a.dst_address_length = dst_address_length;
215           a.dst_address = dst;
216           a.flags |= IP4_ROUTE_FLAG_ADD;
217           a.add_adj = &dst_adj;
218           a.n_add_adj = 1;
219
220           ip4_add_del_route (lgm->im4, &a);
221
222           /* lookup dst adj to obtain the adj index */
223           p = ip4_get_route (lgm->im4, table_index, 0, dst.as_u8,
224                              dst_address_length);
225           if (p == 0)
226             {
227               clib_warning("Failed to insert dst route for eid %U!",
228                            format_ip4_address_and_length, dst.as_u8,
229                            dst_address_length);
230               return -1;
231             }
232
233           /* allocate and init src ip4 fib */
234           pool_get(lgm->src_fibs, src_fib);
235           ip4_mtrie_init (&src_fib->mtrie);
236
237           /* reuse rewrite header to store pointer to src fib */
238           dst_adjp = ip_get_adjacency (lgm->lookup_main, p[0]);
239           dst_adjp->rewrite_header.sw_if_index = src_fib - lgm->src_fibs;
240         }
241     }
242   else
243     {
244       if (p == 0)
245         {
246           clib_warning("Trying to delete inexistent dst route for %U. Aborting",
247                        format_ip4_address_and_length, dst.as_u8,
248                        dst_address_length);
249           return -1;
250         }
251     }
252
253   dst_adjp = ip_get_adjacency (lgm->lookup_main, p[0]);
254
255   /* add/del src prefix to src fib */
256   memset(&a, 0, sizeof(a));
257   a.flags = IP4_ROUTE_FLAG_TABLE_ID;
258   a.table_index_or_table_id = dst_adjp->rewrite_header.sw_if_index;
259   a.adj_index = ~0;
260   a.flags |= is_add ? IP4_ROUTE_FLAG_ADD : IP4_ROUTE_FLAG_DEL;
261   a.add_adj = add_adj;
262   a.n_add_adj = 1;
263   /* if src prefix is null, add 0/0 */
264   a.dst_address_length = src_address_length;
265   a.dst_address = src;
266   ip4_sd_fib_add_del_src_route (lgm, &a);
267
268   /* if a delete, check if there are elements left in the src fib */
269   if (!is_add)
270     {
271       src_fib = pool_elt_at_index(lgm->src_fibs,
272                                   dst_adjp->rewrite_header.sw_if_index);
273       if (!src_fib)
274         return 0;
275
276       /* if there's nothing left, clear src fib .. */
277       if (ARRAY_LEN(src_fib->adj_index_by_dst_address) == 0)
278         {
279           ip4_sd_fib_clear_src_fib (lgm, src_fib);
280           pool_put(lgm->src_fibs, src_fib);
281         }
282
283       /* .. and remove dst route */
284       memset(&a, 0, sizeof(a));
285       a.flags = IP4_ROUTE_FLAG_TABLE_ID;
286       a.table_index_or_table_id = table_index; /* vrf */
287       a.adj_index = ~0;
288       a.dst_address_length = dst_address_length;
289       a.dst_address = dst;
290       a.flags |= IP4_ROUTE_FLAG_DEL;
291
292       ip4_add_del_route (lgm->im4, &a);
293     }
294
295   return 0;
296 }
297
298 static void *
299 ip4_sd_fib_get_route (lisp_gpe_main_t * lgm, ip_prefix_t * dst_prefix,
300                       ip_prefix_t * src_prefix, u32 table_index)
301 {
302   uword * p;
303   ip4_address_t dst = ip_prefix_v4(dst_prefix), src;
304   u32 dst_address_length = ip_prefix_len(dst_prefix), src_address_length = 0;
305   ip_adjacency_t * dst_adj;
306
307   if (src_prefix)
308     {
309       src = ip_prefix_v4(src_prefix);
310       src_address_length = ip_prefix_len(src_prefix);
311     }
312   else
313     memset(&src, 0, sizeof(src));
314
315   /* lookup dst adj */
316   p = ip4_get_route (lgm->im4, table_index, 0, dst.as_u8, dst_address_length);
317   if (p == 0)
318       return p;
319
320   dst_adj = ip_get_adjacency (lgm->lookup_main, p[0]);
321   return ip4_sd_get_src_route (lgm, dst_adj->rewrite_header.sw_if_index, &src,
322                                src_address_length);
323 }
324
325 typedef enum
326 {
327   LGPE_IP4_LOOKUP_NEXT_DROP,
328   LGPE_IP4_LOOKUP_NEXT_LISP_CP_LOOKUP,
329   LGPE_IP4_LOOKUP_NEXT_LGPE_ENCAP,
330   LGPE_IP4_LOOKUP_N_NEXT,
331 } lgpe_ip4_lookup_next_t;
332
333 always_inline void
334 ip4_src_fib_lookup_one (lisp_gpe_main_t * lgm, u32 src_fib_index0,
335                         ip4_address_t * addr0, u32 * src_adj_index0)
336 {
337   ip4_fib_mtrie_leaf_t leaf0, leaf1;
338   ip4_fib_mtrie_t * mtrie0;
339
340   mtrie0 = &vec_elt_at_index(lgm->src_fibs, src_fib_index0)->mtrie;
341
342   leaf0 = leaf1 = IP4_FIB_MTRIE_LEAF_ROOT;
343   leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, addr0, 0);
344   leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, addr0, 1);
345   leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, addr0, 2);
346   leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, addr0, 3);
347
348   /* Handle default route. */
349   leaf0 = (leaf0 == IP4_FIB_MTRIE_LEAF_EMPTY ? mtrie0->default_leaf : leaf0);
350   src_adj_index0[0] = ip4_fib_mtrie_leaf_get_adj_index (leaf0);
351 }
352
353 always_inline void
354 ip4_src_fib_lookup_two (lisp_gpe_main_t * lgm, u32 src_fib_index0,
355                         u32 src_fib_index1, ip4_address_t * addr0,
356                         ip4_address_t * addr1, u32 * src_adj_index0,
357                         u32 * src_adj_index1)
358 {
359   ip4_fib_mtrie_leaf_t leaf0, leaf1;
360   ip4_fib_mtrie_t * mtrie0, * mtrie1;
361
362   mtrie0 = &vec_elt_at_index(lgm->src_fibs, src_fib_index0)->mtrie;
363   mtrie1 = &vec_elt_at_index(lgm->src_fibs, src_fib_index1)->mtrie;
364
365   leaf0 = leaf1 = IP4_FIB_MTRIE_LEAF_ROOT;
366
367   leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, addr0, 0);
368   leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, addr1, 0);
369
370   leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, addr0, 1);
371   leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, addr1, 1);
372
373   leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, addr0, 2);
374   leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, addr1, 2);
375
376   leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, addr0, 3);
377   leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, addr1, 3);
378
379   /* Handle default route. */
380   leaf0 = (leaf0 == IP4_FIB_MTRIE_LEAF_EMPTY ? mtrie0->default_leaf : leaf0);
381   leaf1 = (leaf1 == IP4_FIB_MTRIE_LEAF_EMPTY ? mtrie1->default_leaf : leaf1);
382   src_adj_index0[0] = ip4_fib_mtrie_leaf_get_adj_index (leaf0);
383   src_adj_index1[0] = ip4_fib_mtrie_leaf_get_adj_index (leaf1);
384 }
385
386 always_inline uword
387 lgpe_ip4_lookup (vlib_main_t * vm, vlib_node_runtime_t * node,
388                  vlib_frame_t * from_frame)
389 {
390   u32 n_left_from, next_index, * from, * to_next;
391   lisp_gpe_main_t * lgm = &lisp_gpe_main;
392
393   from = vlib_frame_vector_args (from_frame);
394   n_left_from = from_frame->n_vectors;
395
396   next_index = node->cached_next_index;
397
398   while (n_left_from > 0)
399     {
400       u32 n_left_to_next;
401
402       vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
403
404       while (n_left_from >= 4 && n_left_to_next >= 2)
405         {
406           u32 bi0, bi1;
407           vlib_buffer_t * b0, * b1;
408           ip4_header_t * ip0, * ip1;
409           u32 dst_adj_index0, src_adj_index0, src_fib_index0, dst_adj_index1,
410               src_adj_index1, src_fib_index1;
411           ip_adjacency_t * dst_adj0, * src_adj0, * dst_adj1, * src_adj1;
412           u32 next0, next1;
413
414           next0 = next1 = LGPE_IP4_LOOKUP_NEXT_LISP_CP_LOOKUP;
415
416           /* Prefetch next iteration. */
417           {
418             vlib_buffer_t * p2, * p3;
419
420             p2 = vlib_get_buffer (vm, from[2]);
421             p3 = vlib_get_buffer (vm, from[3]);
422
423             vlib_prefetch_buffer_header (p2, LOAD);
424             vlib_prefetch_buffer_header (p3, LOAD);
425
426             CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
427             CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
428           }
429
430           bi0 = from[0];
431           bi1 = from[1];
432           to_next[0] = bi0;
433           to_next[1] = bi1;
434           from += 2;
435           to_next += 2;
436           n_left_to_next -= 2;
437           n_left_from -= 2;
438
439           b0 = vlib_get_buffer (vm, bi0);
440           b1 = vlib_get_buffer (vm, bi1);
441
442           ip0 = vlib_buffer_get_current (b0);
443           ip1 = vlib_buffer_get_current (b1);
444
445           /* dst lookup was done by ip4 lookup */
446           dst_adj_index0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
447           dst_adj_index1 = vnet_buffer(b1)->ip.adj_index[VLIB_TX];
448
449           dst_adj0 = ip_get_adjacency (lgm->lookup_main, dst_adj_index0);
450           dst_adj1 = ip_get_adjacency (lgm->lookup_main, dst_adj_index1);
451
452           src_fib_index0 = dst_adj0->rewrite_header.sw_if_index;
453           src_fib_index1 = dst_adj1->rewrite_header.sw_if_index;
454
455           /* if default route not hit in ip4 lookup */
456           if (PREDICT_TRUE(src_fib_index0 != (u32) ~0
457                            && src_fib_index1 != (u32) ~0))
458             {
459               ip4_src_fib_lookup_two (lgm, src_fib_index0, src_fib_index1,
460                                       &ip0->src_address, &ip1->src_address,
461                                       &src_adj_index0, &src_adj_index1);
462
463               vnet_buffer(b0)->ip.adj_index[VLIB_TX] = src_adj_index0;
464               vnet_buffer(b1)->ip.adj_index[VLIB_TX] = src_adj_index1;
465
466               src_adj0 = ip_get_adjacency (lgm->lookup_main, src_adj_index0);
467               src_adj1 = ip_get_adjacency (lgm->lookup_main, src_adj_index1);
468
469               next0 = src_adj0->lookup_next_index;
470               next1 = src_adj1->lookup_next_index;
471             }
472           else
473             {
474               if (src_fib_index0 != (u32) ~0)
475                 {
476                   ip4_src_fib_lookup_one (lgm, src_fib_index0,
477                                           &ip0->src_address, &src_adj_index0);
478                   vnet_buffer(b0)->ip.adj_index[VLIB_TX] = src_adj_index0;
479                   src_adj0 = ip_get_adjacency (lgm->lookup_main,
480                                                src_adj_index0);
481                   next0 = src_adj0->lookup_next_index;
482                 }
483               if (src_fib_index1 != (u32) ~0)
484                 {
485                   ip4_src_fib_lookup_one (lgm, src_fib_index1,
486                                           &ip1->src_address, &src_adj_index1);
487                   vnet_buffer(b1)->ip.adj_index[VLIB_TX] = src_adj_index1;
488                   src_adj1 = ip_get_adjacency (lgm->lookup_main,
489                                                src_adj_index1);
490                   next1 = src_adj1->lookup_next_index;
491                 }
492             }
493
494           vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next,
495                                           n_left_to_next, bi0, bi1, next0,
496                                           next1);
497         }
498
499       while (n_left_from > 0 && n_left_to_next > 0)
500         {
501           vlib_buffer_t * b0;
502           ip4_header_t * ip0;
503           u32 bi0, dst_adj_index0, src_adj_index0, src_fib_index0;
504           u32 next0 = LGPE_IP4_LOOKUP_NEXT_LISP_CP_LOOKUP;
505           ip_adjacency_t * dst_adj0, * src_adj0;
506
507           bi0 = from[0];
508           to_next[0] = bi0;
509           from += 1;
510           to_next += 1;
511           n_left_from -= 1;
512           n_left_to_next -= 1;
513
514           b0 = vlib_get_buffer (vm, bi0);
515           ip0 = vlib_buffer_get_current (b0);
516
517           /* dst lookup was done by ip4 lookup */
518           dst_adj_index0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
519           dst_adj0 = ip_get_adjacency (lgm->lookup_main, dst_adj_index0);
520           src_fib_index0 = dst_adj0->rewrite_header.sw_if_index;
521
522           /* default route hit in ip4 lookup, send to lisp control plane */
523           if (src_fib_index0 == (u32) ~0)
524             goto done;
525
526           /* src lookup we do here */
527           ip4_src_fib_lookup_one (lgm, src_fib_index0, &ip0->src_address,
528                                   &src_adj_index0);
529           vnet_buffer(b0)->ip.adj_index[VLIB_TX] = src_adj_index0;
530           src_adj0 = ip_get_adjacency (lgm->lookup_main, src_adj_index0);
531           next0 = src_adj0->lookup_next_index;
532
533         done:
534           vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
535                                           n_left_to_next, bi0, next0);
536         }
537       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
538     }
539   return from_frame->n_vectors;
540 }
541
542
543 VLIB_REGISTER_NODE (lgpe_ip4_lookup_node) = {
544   .function = lgpe_ip4_lookup,
545   .name = "lgpe-ip4-lookup",
546   .vector_size = sizeof (u32),
547
548   .type = VLIB_NODE_TYPE_INTERNAL,
549
550   .n_next_nodes = LGPE_IP4_LOOKUP_N_NEXT,
551   .next_nodes = {
552       [LGPE_IP4_LOOKUP_NEXT_DROP] = "error-drop",
553       [LGPE_IP4_LOOKUP_NEXT_LISP_CP_LOOKUP] = "lisp-cp-lookup",
554       [LGPE_IP4_LOOKUP_NEXT_LGPE_ENCAP] = "lisp-gpe-encap",
555   },
556 };
557
558 static int
559 lisp_gpe_rewrite (lisp_gpe_tunnel_t * t)
560 {
561   u8 *rw = 0;
562   ip4_header_t * ip0;
563   lisp_gpe_header_t * lisp0;
564   ip4_udp_lisp_gpe_header_t * h0;
565   int len;
566
567   len = sizeof(*h0);
568
569   vec_validate_aligned(rw, len - 1, CLIB_CACHE_LINE_BYTES);
570
571   h0 = (ip4_udp_lisp_gpe_header_t *) rw;
572
573   /* Fixed portion of the (outer) ip4 header */
574   ip0 = &h0->ip4;
575   ip0->ip_version_and_header_length = 0x45;
576   ip0->ttl = 254;
577   ip0->protocol = IP_PROTOCOL_UDP;
578
579   /* we fix up the ip4 header length and checksum after-the-fact */
580   ip0->src_address.as_u32 = t->src.as_u32;
581   ip0->dst_address.as_u32 = t->dst.as_u32;
582   ip0->checksum = ip4_header_checksum (ip0);
583
584   /* UDP header, randomize src port on something, maybe? */
585   h0->udp.src_port = clib_host_to_net_u16 (4341);
586   h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_lisp_gpe);
587
588   /* LISP-gpe header */
589   lisp0 = &h0->lisp;
590
591   lisp0->flags = t->flags;
592   lisp0->ver_res = t->ver_res;
593   lisp0->res = t->res;
594   lisp0->next_protocol = t->next_protocol;
595   lisp0->iid = clib_host_to_net_u32 (t->iid);
596
597   t->rewrite = rw;
598   return 0;
599 }
600
601 /* TODO remove */
602 int
603 vnet_lisp_gpe_add_del_tunnel (vnet_lisp_gpe_add_del_tunnel_args_t *a,
604                               u32 * sw_if_indexp)
605 {
606   clib_warning ("UNSUPPORTED! Use vnet_lisp_gpe_add_del_fwd_entry");
607   return 0;
608 }
609
610 #define foreach_copy_field                      \
611 _(encap_fib_index)                              \
612 _(decap_fib_index)                              \
613 _(decap_next_index)                             \
614 _(flags)                                        \
615 _(next_protocol)                                \
616 _(ver_res)                                      \
617 _(res)                                          \
618 _(iid)
619
620 static u32
621 add_del_tunnel (vnet_lisp_gpe_add_del_fwd_entry_args_t *a, u32 * tun_index_res)
622 {
623   lisp_gpe_main_t * lgm = &lisp_gpe_main;
624   lisp_gpe_tunnel_t *t = 0;
625   uword * p;
626   int rv;
627   lisp_gpe_tunnel_key_t key;
628
629   memset(&key, 0, sizeof(key));
630   gid_address_copy(&key.eid, &a->deid);
631   key.dst_loc = ip_addr_v4(&a->dlocator).as_u32;
632   key.iid = clib_host_to_net_u32 (a->iid);
633
634   p = mhash_get (&lgm->lisp_gpe_tunnel_by_key, &key);
635
636   if (a->is_add)
637     {
638       /* adding a tunnel: tunnel must not already exist */
639       if (p)
640         return VNET_API_ERROR_INVALID_VALUE;
641
642       if (a->decap_next_index >= LISP_GPE_INPUT_N_NEXT)
643         return VNET_API_ERROR_INVALID_DECAP_NEXT;
644
645       pool_get_aligned (lgm->tunnels, t, CLIB_CACHE_LINE_BYTES);
646       memset (t, 0, sizeof (*t));
647
648       /* copy from arg structure */
649 #define _(x) t->x = a->x;
650       foreach_copy_field;
651 #undef _
652
653       t->src = ip_addr_v4(&a->slocator);
654       t->dst = ip_addr_v4(&a->dlocator);
655
656       rv = lisp_gpe_rewrite (t);
657
658       if (rv)
659         {
660           pool_put(lgm->tunnels, t);
661           return rv;
662         }
663
664       mhash_set(&lgm->lisp_gpe_tunnel_by_key, &key, t - lgm->tunnels, 0);
665
666       /* return tunnel index */
667       if (tun_index_res)
668         tun_index_res[0] = t - lgm->tunnels;
669     }
670   else
671     {
672       /* deleting a tunnel: tunnel must exist */
673       if (!p)
674         {
675           clib_warning("Tunnel for eid %U doesn't exist!", format_gid_address,
676                        &a->deid);
677           return VNET_API_ERROR_NO_SUCH_ENTRY;
678         }
679
680       t = pool_elt_at_index(lgm->tunnels, p[0]);
681
682       mhash_unset(&lgm->lisp_gpe_tunnel_by_key, &key, 0);
683
684       vec_free(t->rewrite);
685       pool_put(lgm->tunnels, t);
686     }
687
688   return 0;
689 }
690
691 int
692 vnet_lisp_gpe_add_del_fwd_entry (vnet_lisp_gpe_add_del_fwd_entry_args_t * a,
693                                  u32 * hw_if_indexp)
694 {
695   lisp_gpe_main_t * lgm = &lisp_gpe_main;
696   ip_adjacency_t adj, * adjp;
697   u32 * adj_index, rv, tun_index = ~0;
698   ip_prefix_t * dpref = &gid_address_ippref(&a->deid);
699   ip_prefix_t * spref = &gid_address_ippref(&a->seid);
700
701   /* setup adjacency for eid */
702   memset (&adj, 0, sizeof(adj));
703   adj.n_adj = 1;
704   adj.explicit_fib_index = ~0;
705
706   /* treat negative fwd entries separately */
707   if (a->is_negative)
708     {
709       switch (a->action)
710         {
711         case NO_ACTION:
712           /* TODO update timers? */
713         case FORWARD_NATIVE:
714           /* TODO check if route/next-hop for eid exists in fib and add
715            * more specific for the eid with the next-hop found */
716         case SEND_MAP_REQUEST:
717           /* TODO insert tunnel that always sends map-request */
718         case DROP:
719           /* for drop fwd entries, just add route, no need to add encap tunnel */
720           adj.lookup_next_index = LGPE_IP4_LOOKUP_NEXT_DROP;
721
722           /* add/delete route for prefix */
723           rv = ip4_sd_fib_add_del_route (lgm, dpref, spref, a->iid, &adj,
724                                          a->is_add);
725           return rv;
726           break;
727         default:
728           return -1;
729         }
730     }
731
732   /* send packets that hit this adj to lisp-gpe encap */
733   adj.lookup_next_index = LGPE_IP4_LOOKUP_NEXT_LGPE_ENCAP;
734
735   /* add/delete route for prefix
736    * TODO use hash to decide fib instead of using iid in clear */
737   rv = ip4_sd_fib_add_del_route (lgm, dpref, spref, a->iid, &adj, a->is_add);
738
739   if (rv)
740     return rv;
741
742   /* add/del tunnel to tunnels pool */
743   rv = add_del_tunnel (a, &tun_index);
744
745   /* reuse sw_if_index for storing the tunnel index */
746   if (a->is_add)
747     {
748       adj_index = ip4_sd_fib_get_route(lgm, dpref, spref, a->iid);
749       if (!adj_index)
750         {
751           clib_warning("Failed to insert fwd entry! For %U",
752                        format_ip4_address_and_length, ip_prefix_v4(dpref),
753                        ip_prefix_len(dpref));
754           return -1;
755         }
756       adjp = ip_get_adjacency (lgm->lookup_main, adj_index[0]);
757       adjp->rewrite_header.sw_if_index = tun_index;
758     }
759
760   return rv;
761 }
762
763 static clib_error_t *
764 lisp_gpe_add_del_fwd_entry_command_fn (vlib_main_t * vm,
765                                        unformat_input_t * input,
766                                        vlib_cli_command_t * cmd)
767 {
768   unformat_input_t _line_input, * line_input = &_line_input;
769   u8 is_add = 1;
770   ip_address_t slocator, dlocator, *slocators = 0, *dlocators = 0;
771   ip_prefix_t * prefp;
772   gid_address_t * eids = 0, eid;
773   clib_error_t * error = 0;
774   u32 i;
775
776   prefp = &gid_address_ippref(&eid);
777
778   /* Get a line of input. */
779   if (! unformat_user (input, unformat_line_input, line_input))
780     return 0;
781
782   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
783     {
784       if (unformat (line_input, "del"))
785         is_add = 0;
786       else if (unformat (line_input, "add"))
787         is_add = 1;
788       else if (unformat (line_input, "eid %U slocator %U dlocator %U",
789                          unformat_ip_prefix, prefp,
790                          unformat_ip_address, &slocator,
791                          unformat_ip_address, &dlocator))
792         {
793           vec_add1 (eids, eid);
794           vec_add1 (slocators, slocator);
795           vec_add1 (dlocators, dlocator);
796         }
797       else
798         {
799           error = unformat_parse_error (line_input);
800           goto done;
801         }
802     }
803   unformat_free (line_input);
804
805   if (vec_len (eids) + vec_len (slocators) == 0)
806     {
807       error = clib_error_return (0, "expected ip4/ip6 eids/locators.");
808       goto done;
809     }
810
811   if (vec_len (eids) != vec_len (slocators))
812     {
813       error = clib_error_return (0, "number of eids not equal to that of locators.");
814       goto done;
815     }
816
817   for (i = 0; i < vec_len(eids); i++)
818     {
819       vnet_lisp_gpe_add_del_fwd_entry_args_t a;
820       memset (&a, 0, sizeof(a));
821
822       a.is_add = is_add;
823       a.deid = eids[i];
824       a.slocator = slocators[i];
825       a.dlocator = dlocators[i];
826       prefp = &gid_address_ippref(&a.deid);
827       a.decap_next_index = (ip_prefix_version(prefp) == IP4) ?
828               LISP_GPE_INPUT_NEXT_IP4_INPUT : LISP_GPE_INPUT_NEXT_IP6_INPUT;
829       vnet_lisp_gpe_add_del_fwd_entry (&a, 0);
830     }
831
832  done:
833   vec_free(eids);
834   vec_free(slocators);
835   return error;
836 }
837
838 VLIB_CLI_COMMAND (add_del_lisp_gpe_mapping_tunnel_command, static) = {
839   .path = "lisp gpe maptunnel",
840   .short_help = "lisp gpe maptunnel eid <eid> sloc <src-locator> dloc <dst-locator> [del]",
841   .function = lisp_gpe_add_del_fwd_entry_command_fn,
842 };
843
844 int
845 add_del_ip_prefix_route (ip_prefix_t * dst_prefix, u32 table_id,
846                          ip_adjacency_t * add_adj, u8 is_add, u32 * adj_index)
847 {
848   uword * p;
849
850   if (ip_prefix_version(dst_prefix) == IP4)
851     {
852       ip4_main_t * im4 = &ip4_main;
853       ip4_add_del_route_args_t a;
854       ip4_address_t addr = ip_prefix_v4(dst_prefix);
855
856       memset(&a, 0, sizeof(a));
857       a.flags = IP4_ROUTE_FLAG_TABLE_ID;
858       a.table_index_or_table_id = table_id;
859       a.adj_index = ~0;
860       a.dst_address_length = ip_prefix_len(dst_prefix);
861       a.dst_address = addr;
862       a.flags |= is_add ? IP4_ROUTE_FLAG_ADD : IP4_ROUTE_FLAG_DEL;
863       a.add_adj = add_adj;
864       a.n_add_adj = 1;
865       ip4_add_del_route (im4, &a);
866
867       if (is_add)
868         {
869           p = ip4_get_route (im4, table_id, 0, addr.as_u8,
870                              ip_prefix_len(dst_prefix));
871           if (p == 0)
872             {
873               clib_warning("Failed to insert route for eid %U!",
874                            format_ip4_address_and_length, addr.as_u8,
875                            ip_prefix_len(dst_prefix));
876               return -1;
877             }
878           adj_index[0] = p[0];
879         }
880     }
881   else
882     {
883       ip6_main_t * im6 = &ip6_main;
884       ip6_add_del_route_args_t a;
885       ip6_address_t addr = ip_prefix_v6(dst_prefix);
886
887       memset(&a, 0, sizeof(a));
888       a.flags = IP6_ROUTE_FLAG_TABLE_ID;
889       a.table_index_or_table_id = table_id;
890       a.adj_index = ~0;
891       a.dst_address_length = ip_prefix_len(dst_prefix);
892       a.dst_address = addr;
893       a.flags |= is_add ? IP6_ROUTE_FLAG_ADD : IP6_ROUTE_FLAG_DEL;
894       a.add_adj = add_adj;
895       a.n_add_adj = 1;
896
897       ip6_add_del_route (im6, &a);
898
899       if (is_add)
900         {
901           adj_index[0] = ip6_get_route (im6, table_id, 0, &addr,
902                                         ip_prefix_len(dst_prefix));
903           if (adj_index[0] == 0)
904             {
905               clib_warning("Failed to insert route for eid %U!",
906                            format_ip6_address_and_length, addr.as_u8,
907                            ip_prefix_len(dst_prefix));
908               return -1;
909             }
910         }
911     }
912   return 0;
913 }
914
915 static void
916 add_del_lisp_gpe_default_route (u8 is_v4, u8 is_add)
917 {
918   lisp_gpe_main_t * lgm = &lisp_gpe_main;
919   ip_adjacency_t adj;
920   ip_prefix_t prefix;
921   u32 adj_index = 0;
922
923   /* setup adjacency */
924   memset (&adj, 0, sizeof(adj));
925   adj.n_adj = 1;
926   adj.explicit_fib_index = ~0;
927   adj.lookup_next_index = lgm->ip4_lookup_next_lgpe_ip4_lookup;
928   /* default route has tunnel_index ~0 */
929   adj.rewrite_header.sw_if_index = ~0;
930
931   /* set prefix to 0/0 */
932   memset(&prefix, 0, sizeof(prefix));
933   ip_prefix_version(&prefix) = is_v4 ? IP4 : IP6;
934
935   /* add/delete route for prefix XXX default table only */
936   add_del_ip_prefix_route (&prefix, 0, &adj, is_add, &adj_index);
937 }
938
939 static u8 *
940 format_decap_next (u8 * s, va_list * args)
941 {
942   u32 next_index = va_arg (*args, u32);
943
944   switch (next_index)
945     {
946     case LISP_GPE_INPUT_NEXT_DROP:
947       return format (s, "drop");
948     case LISP_GPE_INPUT_NEXT_IP4_INPUT:
949       return format (s, "ip4");
950     case LISP_GPE_INPUT_NEXT_IP6_INPUT:
951       return format (s, "ip6");
952     case LISP_GPE_INPUT_NEXT_LISP_GPE_ENCAP:
953       return format (s, "nsh-lisp-gpe");
954     default:
955       return format (s, "unknown %d", next_index);
956     }
957   return s;
958 }
959
960 u8 *
961 format_lisp_gpe_tunnel (u8 * s, va_list * args)
962 {
963   lisp_gpe_tunnel_t * t = va_arg (*args, lisp_gpe_tunnel_t *);
964   lisp_gpe_main_t * lgm = &lisp_gpe_main;
965
966   s = format (s,
967               "[%d] %U (src) %U (dst) fibs: encap %d, decap %d",
968               t - lgm->tunnels,
969               format_ip4_address, &t->src,
970               format_ip4_address, &t->dst,
971               t->encap_fib_index,
972               t->decap_fib_index);
973
974   s = format (s, " decap next %U\n", format_decap_next, t->decap_next_index);
975   s = format (s, "lisp ver %d ", (t->ver_res>>6));
976
977 #define _(n,v) if (t->flags & v) s = format (s, "%s-bit ", #n);
978   foreach_lisp_gpe_flag_bit;
979 #undef _
980
981   s = format (s, "next_protocol %d ver_res %x res %x\n",
982               t->next_protocol, t->ver_res, t->res);
983
984   s = format (s, "iid %d (0x%x)\n", t->iid, t->iid);
985   return s;
986 }
987
988 static clib_error_t *
989 show_lisp_gpe_tunnel_command_fn (vlib_main_t * vm,
990                                 unformat_input_t * input,
991                                 vlib_cli_command_t * cmd)
992 {
993   lisp_gpe_main_t * lgm = &lisp_gpe_main;
994   lisp_gpe_tunnel_t * t;
995   
996   if (pool_elts (lgm->tunnels) == 0)
997     vlib_cli_output (vm, "No lisp-gpe tunnels configured...");
998
999   pool_foreach (t, lgm->tunnels,
1000   ({
1001     vlib_cli_output (vm, "%U", format_lisp_gpe_tunnel, t);
1002   }));
1003   
1004   return 0;
1005 }
1006
1007 VLIB_CLI_COMMAND (show_lisp_gpe_tunnel_command, static) = {
1008     .path = "show lisp gpe tunnel",
1009     .function = show_lisp_gpe_tunnel_command_fn,
1010 };
1011
1012 static u8 *
1013 format_lisp_gpe_name (u8 * s, va_list * args)
1014 {
1015   u32 dev_instance = va_arg (*args, u32);
1016   return format (s, "lisp_gpe_tunnel%d", dev_instance);
1017 }
1018
1019 static uword
1020 dummy_interface_tx (vlib_main_t * vm, vlib_node_runtime_t * node,
1021                     vlib_frame_t * frame)
1022 {
1023   clib_warning("you shouldn't be here, leaking buffers...");
1024   return frame->n_vectors;
1025 }
1026
1027 VNET_DEVICE_CLASS (lisp_gpe_device_class,static) = {
1028   .name = "LISP_GPE",
1029   .format_device_name = format_lisp_gpe_name,
1030   .format_tx_trace = format_lisp_gpe_encap_trace,
1031   .tx_function = dummy_interface_tx,
1032 };
1033
1034 static uword
1035 dummy_set_rewrite (vnet_main_t * vnm, u32 sw_if_index, u32 l3_type,
1036                    void * dst_address, void * rewrite, uword max_rewrite_bytes)
1037 {
1038   return 0;
1039 }
1040
1041 u8 *
1042 format_lisp_gpe_header_with_length (u8 * s, va_list * args)
1043 {
1044   lisp_gpe_header_t * h = va_arg (*args, lisp_gpe_header_t *);
1045   u32 max_header_bytes = va_arg (*args, u32);
1046   u32 header_bytes;
1047
1048   header_bytes = sizeof (h[0]);
1049   if (max_header_bytes != 0 && header_bytes > max_header_bytes)
1050     return format (s, "gre-nsh header truncated");
1051
1052   s = format (s, "flags: ");
1053 #define _(n,v) if (h->flags & v) s = format (s, "%s ", #n);
1054   foreach_lisp_gpe_flag_bit;
1055 #undef _
1056
1057   s = format (s, "\n  ver_res %d res %d next_protocol %d iid %d(%x)",
1058               h->ver_res, h->res, h->next_protocol,
1059               clib_net_to_host_u32 (h->iid),
1060               clib_net_to_host_u32 (h->iid));
1061   return s;
1062 }
1063
1064 VNET_HW_INTERFACE_CLASS (lisp_gpe_hw_class) = {
1065   .name = "LISP_GPE",
1066   .format_header = format_lisp_gpe_header_with_length,
1067   .set_rewrite = dummy_set_rewrite,
1068 };
1069
1070 void
1071 vnet_lisp_gpe_add_del_iface (vnet_lisp_gpe_add_del_iface_args_t * a,
1072                              u32 * hw_if_indexp)
1073 {
1074   lisp_gpe_main_t * lgm = &lisp_gpe_main;
1075   vnet_main_t * vnm = lgm->vnet_main;
1076   vnet_hw_interface_t * hi;
1077   u32 hw_if_index = ~0;
1078
1079   if (a->is_add)
1080     {
1081       /* create hw lisp_gpe0 iface */
1082       hw_if_index = vnet_register_interface (vnm, lisp_gpe_device_class.index, 0,
1083                                              lisp_gpe_hw_class.index, 0);
1084
1085       hi = vnet_get_hw_interface (vnm, hw_if_index);
1086       hi->output_node_index = lisp_gpe_encap_node.index;
1087       lgm->lisp_gpe_hw_if_index = hw_if_index;
1088
1089       /* add lgpe_ip4_lookup as possible next_node for ip4 lookup */
1090       lgm->ip4_lookup_next_lgpe_ip4_lookup = vlib_node_add_next (
1091           vnm->vlib_main, ip4_lookup_node.index, lgpe_ip4_lookup_node.index);
1092
1093       /* insert default routes that points at lisp-gpe-encap */
1094       add_del_lisp_gpe_default_route(/* is_v4 */1, 1);
1095       add_del_lisp_gpe_default_route(/* is_v4 */0, 1);
1096     }
1097   else
1098     {
1099       vnet_sw_interface_set_flags (vnm, lgm->lisp_gpe_hw_if_index,
1100                                    0 /* down */);
1101     }
1102 }
1103
1104 static clib_error_t *
1105 lisp_gpe_add_del_iface_command_fn (vlib_main_t * vm,
1106                                    unformat_input_t * input,
1107                                    vlib_cli_command_t * cmd)
1108 {
1109   unformat_input_t _line_input, * line_input = &_line_input;
1110   u8 is_add = 1;
1111   vnet_lisp_gpe_add_del_iface_args_t _a, * a = &_a;
1112
1113   /* Get a line of input. */
1114   if (! unformat_user (input, unformat_line_input, line_input))
1115     return 0;
1116
1117   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1118     {
1119       if (unformat (line_input, "up"))
1120         is_add = 1;
1121       else if (unformat (line_input, "down"))
1122         is_add = 0;
1123       else
1124         {
1125           return clib_error_return (0, "parse error: '%U'",
1126                                    format_unformat_error, line_input);
1127         }
1128     }
1129
1130   a->is_add = is_add;
1131   vnet_lisp_gpe_add_del_iface (a, 0);
1132   return 0;
1133 }
1134
1135 VLIB_CLI_COMMAND (add_del_lisp_gpe_iface_command, static) = {
1136   .path = "lisp gpe iface",
1137   .short_help = "lisp gpe iface [del]",
1138   .function = lisp_gpe_add_del_iface_command_fn,
1139 };
1140
1141 clib_error_t *
1142 lisp_gpe_init (vlib_main_t *vm)
1143 {
1144   lisp_gpe_main_t * lgm = &lisp_gpe_main;
1145   clib_error_t * error = 0;
1146
1147   if ((error = vlib_call_init_function (vm, ip_main_init)))
1148     return error;
1149
1150   if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
1151     return error;
1152
1153   lgm->vnet_main = vnet_get_main();
1154   lgm->vlib_main = vm;
1155   lgm->im4 = &ip4_main;
1156   lgm->lookup_main = &ip4_main.lookup_main;
1157   
1158   mhash_init (&lgm->lisp_gpe_tunnel_by_key, sizeof(uword),
1159               sizeof(lisp_gpe_tunnel_key_t));
1160
1161   udp_register_dst_port (vm, UDP_DST_PORT_lisp_gpe, 
1162                          lisp_gpe_input_node.index, 1 /* is_ip4 */);
1163   return 0;
1164 }
1165
1166 VLIB_INIT_FUNCTION(lisp_gpe_init);