Fix compile errors reported by clang
[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_id,
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_id, 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_id; /* 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_id, 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_id; /* 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_id)
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_id, 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_N_NEXT,
330 } lgpe_ip4_lookup_next_t;
331
332 always_inline void
333 ip4_src_fib_lookup_one (lisp_gpe_main_t * lgm, u32 src_fib_index0,
334                         ip4_address_t * addr0, u32 * src_adj_index0)
335 {
336   ip4_fib_mtrie_leaf_t leaf0, leaf1;
337   ip4_fib_mtrie_t * mtrie0;
338
339   mtrie0 = &vec_elt_at_index(lgm->src_fibs, src_fib_index0)->mtrie;
340
341   leaf0 = leaf1 = IP4_FIB_MTRIE_LEAF_ROOT;
342   leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, addr0, 0);
343   leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, addr0, 1);
344   leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, addr0, 2);
345   leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, addr0, 3);
346
347   /* Handle default route. */
348   leaf0 = (leaf0 == IP4_FIB_MTRIE_LEAF_EMPTY ? mtrie0->default_leaf : leaf0);
349   src_adj_index0[0] = ip4_fib_mtrie_leaf_get_adj_index (leaf0);
350 }
351
352 always_inline void
353 ip4_src_fib_lookup_two (lisp_gpe_main_t * lgm, u32 src_fib_index0,
354                         u32 src_fib_index1, ip4_address_t * addr0,
355                         ip4_address_t * addr1, u32 * src_adj_index0,
356                         u32 * src_adj_index1)
357 {
358   ip4_fib_mtrie_leaf_t leaf0, leaf1;
359   ip4_fib_mtrie_t * mtrie0, * mtrie1;
360
361   mtrie0 = &vec_elt_at_index(lgm->src_fibs, src_fib_index0)->mtrie;
362   mtrie1 = &vec_elt_at_index(lgm->src_fibs, src_fib_index1)->mtrie;
363
364   leaf0 = leaf1 = IP4_FIB_MTRIE_LEAF_ROOT;
365
366   leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, addr0, 0);
367   leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, addr1, 0);
368
369   leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, addr0, 1);
370   leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, addr1, 1);
371
372   leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, addr0, 2);
373   leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, addr1, 2);
374
375   leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, addr0, 3);
376   leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, addr1, 3);
377
378   /* Handle default route. */
379   leaf0 = (leaf0 == IP4_FIB_MTRIE_LEAF_EMPTY ? mtrie0->default_leaf : leaf0);
380   leaf1 = (leaf1 == IP4_FIB_MTRIE_LEAF_EMPTY ? mtrie1->default_leaf : leaf1);
381   src_adj_index0[0] = ip4_fib_mtrie_leaf_get_adj_index (leaf0);
382   src_adj_index1[0] = ip4_fib_mtrie_leaf_get_adj_index (leaf1);
383 }
384
385 always_inline uword
386 lgpe_ip4_lookup (vlib_main_t * vm, vlib_node_runtime_t * node,
387                  vlib_frame_t * from_frame)
388 {
389   u32 n_left_from, next_index, * from, * to_next;
390   lisp_gpe_main_t * lgm = &lisp_gpe_main;
391
392   from = vlib_frame_vector_args (from_frame);
393   n_left_from = from_frame->n_vectors;
394
395   next_index = node->cached_next_index;
396
397   while (n_left_from > 0)
398     {
399       u32 n_left_to_next;
400
401       vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
402
403       while (n_left_from >= 4 && n_left_to_next >= 2)
404         {
405           u32 bi0, bi1;
406           vlib_buffer_t * b0, * b1;
407           ip4_header_t * ip0, * ip1;
408           u32 dst_adj_index0, src_adj_index0, src_fib_index0, dst_adj_index1,
409               src_adj_index1, src_fib_index1;
410           ip_adjacency_t * dst_adj0, * src_adj0, * dst_adj1, * src_adj1;
411           u32 next0, next1;
412
413           next0 = next1 = LGPE_IP4_LOOKUP_NEXT_LISP_CP_LOOKUP;
414
415           /* Prefetch next iteration. */
416           {
417             vlib_buffer_t * p2, * p3;
418
419             p2 = vlib_get_buffer (vm, from[2]);
420             p3 = vlib_get_buffer (vm, from[3]);
421
422             vlib_prefetch_buffer_header (p2, LOAD);
423             vlib_prefetch_buffer_header (p3, LOAD);
424
425             CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
426             CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
427           }
428
429           bi0 = from[0];
430           bi1 = from[1];
431           to_next[0] = bi0;
432           to_next[1] = bi1;
433           from += 2;
434           to_next += 2;
435           n_left_to_next -= 2;
436           n_left_from -= 2;
437
438           b0 = vlib_get_buffer (vm, bi0);
439           b1 = vlib_get_buffer (vm, bi1);
440
441           ip0 = vlib_buffer_get_current (b0);
442           ip1 = vlib_buffer_get_current (b1);
443
444           /* dst lookup was done by ip4 lookup */
445           dst_adj_index0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
446           dst_adj_index1 = vnet_buffer(b1)->ip.adj_index[VLIB_TX];
447
448           dst_adj0 = ip_get_adjacency (lgm->lookup_main, dst_adj_index0);
449           dst_adj1 = ip_get_adjacency (lgm->lookup_main, dst_adj_index1);
450
451           src_fib_index0 = dst_adj0->rewrite_header.sw_if_index;
452           src_fib_index1 = dst_adj1->rewrite_header.sw_if_index;
453
454           /* if default route not hit in ip4 lookup */
455           if (PREDICT_TRUE(src_fib_index0 != (u32) ~0
456                            && src_fib_index1 != (u32) ~0))
457             {
458               ip4_src_fib_lookup_two (lgm, src_fib_index0, src_fib_index1,
459                                       &ip0->src_address, &ip1->src_address,
460                                       &src_adj_index0, &src_adj_index1);
461
462               vnet_buffer(b0)->ip.adj_index[VLIB_TX] = src_adj_index0;
463               vnet_buffer(b1)->ip.adj_index[VLIB_TX] = src_adj_index1;
464
465               src_adj0 = ip_get_adjacency (lgm->lookup_main, src_adj_index0);
466               src_adj1 = ip_get_adjacency (lgm->lookup_main, src_adj_index1);
467
468               next0 = src_adj0->lookup_next_index;
469               next1 = src_adj1->lookup_next_index;
470
471               /* prepare buffer for lisp-gpe output node */
472               vnet_buffer (b0)->sw_if_index[VLIB_TX] =
473                   src_adj0->rewrite_header.sw_if_index;
474               vnet_buffer (b1)->sw_if_index[VLIB_TX] =
475                   src_adj1->rewrite_header.sw_if_index;
476             }
477           else
478             {
479               if (src_fib_index0 != (u32) ~0)
480                 {
481                   ip4_src_fib_lookup_one (lgm, src_fib_index0,
482                                           &ip0->src_address, &src_adj_index0);
483                   vnet_buffer(b0)->ip.adj_index[VLIB_TX] = src_adj_index0;
484                   src_adj0 = ip_get_adjacency (lgm->lookup_main,
485                                                src_adj_index0);
486                   next0 = src_adj0->lookup_next_index;
487                   vnet_buffer (b0)->sw_if_index[VLIB_TX] = src_adj_index0;
488                 }
489               if (src_fib_index1 != (u32) ~0)
490                 {
491                   ip4_src_fib_lookup_one (lgm, src_fib_index1,
492                                           &ip1->src_address, &src_adj_index1);
493                   vnet_buffer(b1)->ip.adj_index[VLIB_TX] = src_adj_index1;
494                   src_adj1 = ip_get_adjacency (lgm->lookup_main,
495                                                src_adj_index1);
496                   next1 = src_adj1->lookup_next_index;
497                   vnet_buffer (b1)->sw_if_index[VLIB_TX] = src_adj_index1;
498                 }
499             }
500
501           vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next,
502                                           n_left_to_next, bi0, bi1, next0,
503                                           next1);
504         }
505
506       while (n_left_from > 0 && n_left_to_next > 0)
507         {
508           vlib_buffer_t * b0;
509           ip4_header_t * ip0;
510           u32 bi0, dst_adj_index0, src_adj_index0, src_fib_index0;
511           u32 next0 = LGPE_IP4_LOOKUP_NEXT_LISP_CP_LOOKUP;
512           ip_adjacency_t * dst_adj0, * src_adj0;
513
514           bi0 = from[0];
515           to_next[0] = bi0;
516           from += 1;
517           to_next += 1;
518           n_left_from -= 1;
519           n_left_to_next -= 1;
520
521           b0 = vlib_get_buffer (vm, bi0);
522           ip0 = vlib_buffer_get_current (b0);
523
524           /* dst lookup was done by ip4 lookup */
525           dst_adj_index0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
526           dst_adj0 = ip_get_adjacency (lgm->lookup_main, dst_adj_index0);
527           src_fib_index0 = dst_adj0->rewrite_header.sw_if_index;
528
529           /* default route hit in ip4 lookup, send to lisp control plane */
530           if (src_fib_index0 == (u32) ~0)
531             goto done;
532
533           /* src lookup we do here */
534           ip4_src_fib_lookup_one (lgm, src_fib_index0, &ip0->src_address,
535                                   &src_adj_index0);
536           vnet_buffer(b0)->ip.adj_index[VLIB_TX] = src_adj_index0;
537           src_adj0 = ip_get_adjacency (lgm->lookup_main, src_adj_index0);
538           next0 = src_adj0->lookup_next_index;
539
540           /* prepare packet for lisp-gpe output node */
541           vnet_buffer (b0)->sw_if_index[VLIB_TX] =
542               src_adj0->rewrite_header.sw_if_index;
543         done:
544           vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
545                                           n_left_to_next, bi0, next0);
546         }
547       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
548     }
549   return from_frame->n_vectors;
550 }
551
552
553 VLIB_REGISTER_NODE (lgpe_ip4_lookup_node) = {
554   .function = lgpe_ip4_lookup,
555   .name = "lgpe-ip4-lookup",
556   .vector_size = sizeof (u32),
557
558   .type = VLIB_NODE_TYPE_INTERNAL,
559
560   .n_next_nodes = LGPE_IP4_LOOKUP_N_NEXT,
561   .next_nodes = {
562       [LGPE_IP4_LOOKUP_NEXT_DROP] = "error-drop",
563       [LGPE_IP4_LOOKUP_NEXT_LISP_CP_LOOKUP] = "lisp-cp-lookup",
564   },
565 };
566
567 static int
568 lisp_gpe_rewrite (lisp_gpe_tunnel_t * t)
569 {
570   u8 *rw = 0;
571   ip4_header_t * ip0;
572   lisp_gpe_header_t * lisp0;
573   ip4_udp_lisp_gpe_header_t * h0;
574   int len;
575
576   len = sizeof(*h0);
577
578   vec_validate_aligned(rw, len - 1, CLIB_CACHE_LINE_BYTES);
579
580   h0 = (ip4_udp_lisp_gpe_header_t *) rw;
581
582   /* Fixed portion of the (outer) ip4 header */
583   ip0 = &h0->ip4;
584   ip0->ip_version_and_header_length = 0x45;
585   ip0->ttl = 254;
586   ip0->protocol = IP_PROTOCOL_UDP;
587
588   /* we fix up the ip4 header length and checksum after-the-fact */
589   ip0->src_address.as_u32 = t->src.as_u32;
590   ip0->dst_address.as_u32 = t->dst.as_u32;
591   ip0->checksum = ip4_header_checksum (ip0);
592
593   /* UDP header, randomize src port on something, maybe? */
594   h0->udp.src_port = clib_host_to_net_u16 (4341);
595   h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_lisp_gpe);
596
597   /* LISP-gpe header */
598   lisp0 = &h0->lisp;
599
600   lisp0->flags = t->flags;
601   lisp0->ver_res = t->ver_res;
602   lisp0->res = t->res;
603   lisp0->next_protocol = t->next_protocol;
604   lisp0->iid = clib_host_to_net_u32 (t->vni);
605
606   t->rewrite = rw;
607   return 0;
608 }
609
610 /* TODO remove */
611 int
612 vnet_lisp_gpe_add_del_tunnel (vnet_lisp_gpe_add_del_tunnel_args_t *a,
613                               u32 * sw_if_indexp)
614 {
615   clib_warning ("UNSUPPORTED! Use vnet_lisp_gpe_add_del_fwd_entry");
616   return 0;
617 }
618
619 #define foreach_copy_field                      \
620 _(encap_fib_index)                              \
621 _(decap_fib_index)                              \
622 _(decap_next_index)                             \
623 _(flags)                                        \
624 _(next_protocol)                                \
625 _(ver_res)                                      \
626 _(res)                                          \
627 _(vni)
628
629 static u32
630 add_del_tunnel (vnet_lisp_gpe_add_del_fwd_entry_args_t *a, u32 * tun_index_res)
631 {
632   lisp_gpe_main_t * lgm = &lisp_gpe_main;
633   lisp_gpe_tunnel_t *t = 0;
634   uword * p;
635   int rv;
636   lisp_gpe_tunnel_key_t key;
637
638   memset(&key, 0, sizeof(key));
639   gid_address_copy(&key.eid, &a->deid);
640   key.dst_loc = ip_addr_v4(&a->dlocator).as_u32;
641   key.iid = clib_host_to_net_u32 (a->vni);
642
643   p = mhash_get (&lgm->lisp_gpe_tunnel_by_key, &key);
644
645   if (a->is_add)
646     {
647       /* adding a tunnel: tunnel must not already exist */
648       if (p)
649         return VNET_API_ERROR_INVALID_VALUE;
650
651       if (a->decap_next_index >= LISP_GPE_INPUT_N_NEXT)
652         return VNET_API_ERROR_INVALID_DECAP_NEXT;
653
654       pool_get_aligned (lgm->tunnels, t, CLIB_CACHE_LINE_BYTES);
655       memset (t, 0, sizeof (*t));
656
657       /* copy from arg structure */
658 #define _(x) t->x = a->x;
659       foreach_copy_field;
660 #undef _
661
662       t->src = ip_addr_v4(&a->slocator);
663       t->dst = ip_addr_v4(&a->dlocator);
664
665       rv = lisp_gpe_rewrite (t);
666
667       if (rv)
668         {
669           pool_put(lgm->tunnels, t);
670           return rv;
671         }
672
673       mhash_set(&lgm->lisp_gpe_tunnel_by_key, &key, t - lgm->tunnels, 0);
674
675       /* return tunnel index */
676       if (tun_index_res)
677         tun_index_res[0] = t - lgm->tunnels;
678     }
679   else
680     {
681       /* deleting a tunnel: tunnel must exist */
682       if (!p)
683         {
684           clib_warning("Tunnel for eid %U doesn't exist!", format_gid_address,
685                        &a->deid);
686           return VNET_API_ERROR_NO_SUCH_ENTRY;
687         }
688
689       t = pool_elt_at_index(lgm->tunnels, p[0]);
690
691       mhash_unset(&lgm->lisp_gpe_tunnel_by_key, &key, 0);
692
693       vec_free(t->rewrite);
694       pool_put(lgm->tunnels, t);
695     }
696
697   return 0;
698 }
699
700 static int
701 add_del_negative_fwd_entry (lisp_gpe_main_t * lgm,
702                             vnet_lisp_gpe_add_del_fwd_entry_args_t * a)
703 {
704   ip_adjacency_t adj;
705   /* setup adjacency for eid */
706   memset (&adj, 0, sizeof(adj));
707   adj.n_adj = 1;
708   adj.explicit_fib_index = ~0;
709
710   ip_prefix_t * dpref = &gid_address_ippref(&a->deid);
711   ip_prefix_t * spref = &gid_address_ippref(&a->seid);
712
713   switch (a->action)
714     {
715     case NO_ACTION:
716       /* TODO update timers? */
717     case FORWARD_NATIVE:
718       /* TODO check if route/next-hop for eid exists in fib and add
719        * more specific for the eid with the next-hop found */
720     case SEND_MAP_REQUEST:
721       /* TODO insert tunnel that always sends map-request */
722     case DROP:
723       /* for drop fwd entries, just add route, no need to add encap tunnel */
724       adj.lookup_next_index = (u16) LGPE_IP4_LOOKUP_NEXT_DROP;
725
726       /* add/delete route for prefix */
727       return ip4_sd_fib_add_del_route (lgm, dpref, spref, a->table_id, &adj,
728                                        a->is_add);
729       break;
730     default:
731       return -1;
732     }
733 }
734
735 int
736 vnet_lisp_gpe_add_del_fwd_entry (vnet_lisp_gpe_add_del_fwd_entry_args_t * a,
737                                  u32 * hw_if_indexp)
738 {
739   lisp_gpe_main_t * lgm = &lisp_gpe_main;
740   ip_adjacency_t adj, * adjp;
741   u32 * adj_index, rv, tun_index = ~0;
742   ip_prefix_t * dpref, * spref;
743   uword * lookup_next_index, * lgpe_sw_if_index;
744
745   /* treat negative fwd entries separately */
746   if (a->is_negative)
747     return add_del_negative_fwd_entry (lgm, a);
748
749   /* add/del tunnel to tunnels pool and prepares rewrite */
750   rv = add_del_tunnel (a, &tun_index);
751   if (rv)
752     return rv;
753
754   dpref = &gid_address_ippref(&a->deid);
755   spref = &gid_address_ippref(&a->seid);
756
757   /* setup adjacency for eid */
758   memset (&adj, 0, sizeof(adj));
759   adj.n_adj = 1;
760   adj.explicit_fib_index = ~0;
761
762   if (a->is_add)
763     {
764       /* send packets that hit this adj to lisp-gpe interface output node in
765        * requested vrf. */
766       lookup_next_index = hash_get(lgm->lgpe_ip4_lookup_next_index_by_table_id,
767                                    a->table_id);
768       lgpe_sw_if_index = hash_get(lgm->lisp_gpe_hw_if_index_by_table_id,
769                                   a->table_id);
770
771       /* the assumption is that the interface must've been created before
772        * programming the dp */
773       ASSERT(lookup_next_index != 0);
774       ASSERT(lgpe_sw_if_index != 0);
775
776       adj.lookup_next_index = lookup_next_index[0];
777       adj.rewrite_header.node_index = tun_index;
778       adj.rewrite_header.sw_if_index = lgpe_sw_if_index[0];
779     }
780
781   /* add/delete route for prefix */
782   rv = ip4_sd_fib_add_del_route (lgm, dpref, spref, a->table_id, &adj,
783                                  a->is_add);
784
785   /* check that everything worked */
786   if (CLIB_DEBUG && a->is_add)
787     {
788       adj_index = ip4_sd_fib_get_route (lgm, dpref, spref, a->table_id);
789       ASSERT(adj_index != 0);
790
791       adjp = ip_get_adjacency (lgm->lookup_main, adj_index[0]);
792
793       ASSERT(adjp != 0);
794       ASSERT(adjp->rewrite_header.node_index == tun_index);
795     }
796
797   return rv;
798 }
799
800 static clib_error_t *
801 lisp_gpe_add_del_fwd_entry_command_fn (vlib_main_t * vm,
802                                        unformat_input_t * input,
803                                        vlib_cli_command_t * cmd)
804 {
805   unformat_input_t _line_input, * line_input = &_line_input;
806   u8 is_add = 1;
807   ip_address_t slocator, dlocator, *slocators = 0, *dlocators = 0;
808   ip_prefix_t * prefp;
809   gid_address_t * eids = 0, eid;
810   clib_error_t * error = 0;
811   u32 i;
812
813   prefp = &gid_address_ippref(&eid);
814
815   /* Get a line of input. */
816   if (! unformat_user (input, unformat_line_input, line_input))
817     return 0;
818
819   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
820     {
821       if (unformat (line_input, "del"))
822         is_add = 0;
823       else if (unformat (line_input, "add"))
824         is_add = 1;
825       else if (unformat (line_input, "eid %U slocator %U dlocator %U",
826                          unformat_ip_prefix, prefp,
827                          unformat_ip_address, &slocator,
828                          unformat_ip_address, &dlocator))
829         {
830           vec_add1 (eids, eid);
831           vec_add1 (slocators, slocator);
832           vec_add1 (dlocators, dlocator);
833         }
834       else
835         {
836           error = unformat_parse_error (line_input);
837           goto done;
838         }
839     }
840   unformat_free (line_input);
841
842   if (vec_len (eids) + vec_len (slocators) == 0)
843     {
844       error = clib_error_return (0, "expected ip4/ip6 eids/locators.");
845       goto done;
846     }
847
848   if (vec_len (eids) != vec_len (slocators))
849     {
850       error = clib_error_return (0, "number of eids not equal to that of "
851           "locators.");
852       goto done;
853     }
854
855   for (i = 0; i < vec_len(eids); i++)
856     {
857       vnet_lisp_gpe_add_del_fwd_entry_args_t a;
858       memset (&a, 0, sizeof(a));
859
860       a.is_add = is_add;
861       a.deid = eids[i];
862       a.slocator = slocators[i];
863       a.dlocator = dlocators[i];
864       prefp = &gid_address_ippref(&a.deid);
865       a.decap_next_index = (ip_prefix_version(prefp) == IP4) ?
866               LISP_GPE_INPUT_NEXT_IP4_INPUT : LISP_GPE_INPUT_NEXT_IP6_INPUT;
867       vnet_lisp_gpe_add_del_fwd_entry (&a, 0);
868     }
869
870  done:
871   vec_free(eids);
872   vec_free(slocators);
873   return error;
874 }
875
876 VLIB_CLI_COMMAND (add_del_lisp_gpe_mapping_tunnel_command, static) = {
877   .path = "lisp gpe maptunnel",
878   .short_help = "lisp gpe maptunnel eid <eid> sloc <src-locator> "
879       "dloc <dst-locator> [del]",
880   .function = lisp_gpe_add_del_fwd_entry_command_fn,
881 };
882
883 static u8 *
884 format_decap_next (u8 * s, va_list * args)
885 {
886   u32 next_index = va_arg (*args, u32);
887
888   switch (next_index)
889     {
890     case LISP_GPE_INPUT_NEXT_DROP:
891       return format (s, "drop");
892     case LISP_GPE_INPUT_NEXT_IP4_INPUT:
893       return format (s, "ip4");
894     case LISP_GPE_INPUT_NEXT_IP6_INPUT:
895       return format (s, "ip6");
896     default:
897       return format (s, "unknown %d", next_index);
898     }
899   return s;
900 }
901
902 u8 *
903 format_lisp_gpe_tunnel (u8 * s, va_list * args)
904 {
905   lisp_gpe_tunnel_t * t = va_arg (*args, lisp_gpe_tunnel_t *);
906   lisp_gpe_main_t * lgm = &lisp_gpe_main;
907
908   s = format (s,
909               "[%d] %U (src) %U (dst) fibs: encap %d, decap %d",
910               t - lgm->tunnels,
911               format_ip4_address, &t->src,
912               format_ip4_address, &t->dst,
913               t->encap_fib_index,
914               t->decap_fib_index);
915
916   s = format (s, " decap next %U\n", format_decap_next, t->decap_next_index);
917   s = format (s, "lisp ver %d ", (t->ver_res>>6));
918
919 #define _(n,v) if (t->flags & v) s = format (s, "%s-bit ", #n);
920   foreach_lisp_gpe_flag_bit;
921 #undef _
922
923   s = format (s, "next_protocol %d ver_res %x res %x\n",
924               t->next_protocol, t->ver_res, t->res);
925
926   s = format (s, "iid %d (0x%x)\n", t->vni, t->vni);
927   return s;
928 }
929
930 static clib_error_t *
931 show_lisp_gpe_tunnel_command_fn (vlib_main_t * vm,
932                                 unformat_input_t * input,
933                                 vlib_cli_command_t * cmd)
934 {
935   lisp_gpe_main_t * lgm = &lisp_gpe_main;
936   lisp_gpe_tunnel_t * t;
937   
938   if (pool_elts (lgm->tunnels) == 0)
939     vlib_cli_output (vm, "No lisp-gpe tunnels configured...");
940
941   pool_foreach (t, lgm->tunnels,
942   ({
943     vlib_cli_output (vm, "%U", format_lisp_gpe_tunnel, t);
944   }));
945   
946   return 0;
947 }
948
949 VLIB_CLI_COMMAND (show_lisp_gpe_tunnel_command, static) = {
950     .path = "show lisp gpe tunnel",
951     .function = show_lisp_gpe_tunnel_command_fn,
952 };
953
954 clib_error_t *
955 vnet_lisp_gpe_enable_disable (vnet_lisp_gpe_enable_disable_args_t * a)
956 {
957   lisp_gpe_main_t * lgm = &lisp_gpe_main;
958   vnet_main_t * vnm = lgm->vnet_main;
959
960   if (a->is_en)
961     {
962       /* add lgpe_ip4_lookup as possible next_node for ip4 lookup */
963       if (lgm->ip4_lookup_next_lgpe_ip4_lookup == ~0)
964         {
965           lgm->ip4_lookup_next_lgpe_ip4_lookup = vlib_node_add_next (
966               vnm->vlib_main, ip4_lookup_node.index,
967               lgpe_ip4_lookup_node.index);
968         }
969       else
970         {
971           /* ask cp to re-add ifaces and defaults */
972         }
973     }
974   else
975     {
976       CLIB_UNUSED(uword * val);
977       hash_pair_t * p;
978       u32 * table_ids = 0, * table_id;
979       lisp_gpe_tunnel_key_t * tunnels = 0, * tunnel;
980       vnet_lisp_gpe_add_del_fwd_entry_args_t _at, * at = &_at;
981       vnet_lisp_gpe_add_del_iface_args_t _ai, * ai= &_ai;
982
983       /* remove all tunnels */
984       mhash_foreach(tunnel, val, &lgm->lisp_gpe_tunnel_by_key, ({
985         vec_add1(tunnels, tunnel[0]);
986       }));
987
988       vec_foreach(tunnel, tunnels) {
989         memset(at, 0, sizeof(at[0]));
990         at->is_add = 0;
991         gid_address_copy(&at->deid, &tunnel->eid);
992         ip_addr_v4(&at->dlocator).as_u32= tunnel->dst_loc;
993         vnet_lisp_gpe_add_del_fwd_entry (at, 0);
994       }
995       vec_free(tunnels);
996
997       /* disable all ifaces */
998       hash_foreach_pair(p, lgm->lisp_gpe_hw_if_index_by_table_id, ({
999         vec_add1(table_ids, p->key);
1000       }));
1001
1002       vec_foreach(table_id, table_ids) {
1003         ai->is_add = 0;
1004         ai->table_id = table_id[0];
1005
1006         /* disables interface and removes defaults */
1007         vnet_lisp_gpe_add_del_iface(ai, 0);
1008       }
1009       vec_free(table_ids);
1010     }
1011
1012   return 0;
1013 }
1014
1015 static clib_error_t *
1016 lisp_gpe_enable_disable_command_fn (vlib_main_t * vm, unformat_input_t * input,
1017                                     vlib_cli_command_t * cmd)
1018 {
1019   unformat_input_t _line_input, * line_input = &_line_input;
1020   u8 is_en = 1;
1021   vnet_lisp_gpe_enable_disable_args_t _a, * a = &_a;
1022
1023   /* Get a line of input. */
1024   if (! unformat_user (input, unformat_line_input, line_input))
1025     return 0;
1026
1027   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1028     {
1029       if (unformat (line_input, "enable"))
1030         is_en = 1;
1031       else if (unformat (line_input, "disable"))
1032         is_en = 0;
1033       else
1034         {
1035           return clib_error_return (0, "parse error: '%U'",
1036                                    format_unformat_error, line_input);
1037         }
1038     }
1039   a->is_en = is_en;
1040   return vnet_lisp_gpe_enable_disable (a);
1041 }
1042
1043 VLIB_CLI_COMMAND (enable_disable_lisp_gpe_command, static) = {
1044   .path = "lisp gpe",
1045   .short_help = "lisp gpe [enable|disable]",
1046   .function = lisp_gpe_enable_disable_command_fn,
1047 };
1048
1049 clib_error_t *
1050 lisp_gpe_init (vlib_main_t *vm)
1051 {
1052   lisp_gpe_main_t * lgm = &lisp_gpe_main;
1053   clib_error_t * error = 0;
1054
1055   if ((error = vlib_call_init_function (vm, ip_main_init)))
1056     return error;
1057
1058   if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
1059     return error;
1060
1061   lgm->vnet_main = vnet_get_main();
1062   lgm->vlib_main = vm;
1063   lgm->im4 = &ip4_main;
1064   lgm->lookup_main = &ip4_main.lookup_main;
1065   lgm->ip4_lookup_next_lgpe_ip4_lookup = ~0;
1066
1067   mhash_init (&lgm->lisp_gpe_tunnel_by_key, sizeof(uword),
1068               sizeof(lisp_gpe_tunnel_key_t));
1069
1070   udp_register_dst_port (vm, UDP_DST_PORT_lisp_gpe, 
1071                          lisp_gpe_input_node.index, 1 /* is_ip4 */);
1072   return 0;
1073 }
1074
1075 VLIB_INIT_FUNCTION(lisp_gpe_init);