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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vnet/lisp-gpe/lisp_gpe.h>
18 lisp_gpe_main_t lisp_gpe_main;
20 /* avoids calling route callbacks for src fib */
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,
26 ip_lookup_main_t * lm = lgm->lookup_main;
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;
35 /* Make sure adj index is valid. */
37 (void) ip_get_adjacency (lm, adj_index);
39 hash = fib->adj_index_by_dst_address[dst_address_length];
41 hash = _hash_set3 (hash, dst_address_u32,
43 fib->old_hash_values);
45 fib->adj_index_by_dst_address[dst_address_length] = hash;
48 /* copied from ip4_forward since it's static */
50 ip4_fib_init_adj_index_by_dst_address (ip_lookup_main_t * lm,
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);
60 fib->adj_index_by_dst_address[address_length] =
61 hash_create (32 /* elts */, lm->fib_result_n_words * sizeof (uword));
63 hash_set_flags (fib->adj_index_by_dst_address[address_length],
64 HASH_FLAG_NO_AUTO_SHRINK);
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;
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);
75 ip4_sd_fib_add_del_src_route (lisp_gpe_main_t * lgm,
76 ip4_add_del_route_args_t * a)
78 ip_lookup_main_t * lm = lgm->lookup_main;
80 u32 dst_address, dst_address_length, adj_index, old_adj_index;
83 /* Either create new adjacency or use given one depending on arguments. */
85 ip_add_adjacency (lm, a->add_adj, a->n_add_adj, &adj_index);
87 adj_index = a->adj_index;
89 dst_address = a->dst_address.data_u32;
90 dst_address_length = a->dst_address_length;
92 fib = pool_elt_at_index(lgm->src_fibs, a->table_index_or_table_id);
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);
97 hash = fib->adj_index_by_dst_address[dst_address_length];
99 is_del = (a->flags & IP4_ROUTE_FLAG_DEL) != 0;
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;
108 ip4_sd_fib_set_adj_index (lgm, fib, a->flags, dst_address,
109 dst_address_length, adj_index);
111 old_adj_index = fib->old_hash_values[0];
113 ip4_fib_mtrie_add_del_route (fib, a->dst_address, dst_address_length,
114 is_del ? old_adj_index : adj_index,
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);
125 ip4_sd_get_src_route (lisp_gpe_main_t * lgm, u32 src_fib_index,
126 ip4_address_t * src, u32 address_length)
128 ip4_fib_t * fib = pool_elt_at_index (lgm->src_fibs, src_fib_index);
131 hash = fib->adj_index_by_dst_address[address_length];
132 p = hash_get (hash, src->as_u32);
136 typedef CLIB_PACKED (struct {
137 ip4_address_t address;
138 u32 address_length : 6;
143 ip4_sd_fib_clear_src_fib (lisp_gpe_main_t * lgm, ip4_fib_t * fib)
145 ip4_route_t * routes = 0, * r;
148 vec_reset_length (routes);
150 for (i = 0; i < ARRAY_LEN (fib->adj_index_by_dst_address); i++) {
151 uword * hash = fib->adj_index_by_dst_address[i];
155 x.address_length = i;
157 hash_foreach_pair (p, hash,
159 x.address.data_u32 = p->key;
160 vec_add1 (routes, x);
164 vec_foreach (r, routes) {
165 ip4_add_del_route_args_t a;
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;
174 ip4_sd_fib_add_del_src_route (lgm, &a);
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)
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;
192 src = ip_prefix_v4(src_prefix);
193 src_address_length = ip_prefix_len(src_prefix);
196 memset(&src, 0, sizeof(src));
199 p = ip4_get_route (lgm->im4, table_id, 0, dst.as_u8, dst_address_length);
203 /* insert dst prefix to ip4 fib, if it's not in yet */
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;
210 memset(&a, 0, sizeof(a));
211 a.flags = IP4_ROUTE_FLAG_TABLE_ID;
212 a.table_index_or_table_id = table_id; /* vrf */
214 a.dst_address_length = dst_address_length;
216 a.flags |= IP4_ROUTE_FLAG_ADD;
217 a.add_adj = &dst_adj;
220 ip4_add_del_route (lgm->im4, &a);
222 /* lookup dst adj to obtain the adj index */
223 p = ip4_get_route (lgm->im4, table_id, 0, dst.as_u8,
227 clib_warning("Failed to insert dst route for eid %U!",
228 format_ip4_address_and_length, dst.as_u8,
233 /* allocate and init src ip4 fib */
234 pool_get(lgm->src_fibs, src_fib);
235 ip4_mtrie_init (&src_fib->mtrie);
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;
246 clib_warning("Trying to delete inexistent dst route for %U. Aborting",
247 format_ip4_address_and_length, dst.as_u8,
253 dst_adjp = ip_get_adjacency (lgm->lookup_main, p[0]);
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;
260 a.flags |= is_add ? IP4_ROUTE_FLAG_ADD : IP4_ROUTE_FLAG_DEL;
263 /* if src prefix is null, add 0/0 */
264 a.dst_address_length = src_address_length;
266 ip4_sd_fib_add_del_src_route (lgm, &a);
268 /* if a delete, check if there are elements left in the src fib */
271 src_fib = pool_elt_at_index(lgm->src_fibs,
272 dst_adjp->rewrite_header.sw_if_index);
276 /* if there's nothing left, clear src fib .. */
277 if (ARRAY_LEN(src_fib->adj_index_by_dst_address) == 0)
279 ip4_sd_fib_clear_src_fib (lgm, src_fib);
280 pool_put(lgm->src_fibs, src_fib);
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 */
288 a.dst_address_length = dst_address_length;
290 a.flags |= IP4_ROUTE_FLAG_DEL;
292 ip4_add_del_route (lgm->im4, &a);
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)
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;
309 src = ip_prefix_v4(src_prefix);
310 src_address_length = ip_prefix_len(src_prefix);
313 memset(&src, 0, sizeof(src));
316 p = ip4_get_route (lgm->im4, table_id, 0, dst.as_u8, dst_address_length);
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,
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;
333 ip4_src_fib_lookup_one (lisp_gpe_main_t * lgm, u32 src_fib_index0,
334 ip4_address_t * addr0, u32 * src_adj_index0)
336 ip4_fib_mtrie_leaf_t leaf0, leaf1;
337 ip4_fib_mtrie_t * mtrie0;
339 mtrie0 = &vec_elt_at_index(lgm->src_fibs, src_fib_index0)->mtrie;
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);
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);
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)
358 ip4_fib_mtrie_leaf_t leaf0, leaf1;
359 ip4_fib_mtrie_t * mtrie0, * mtrie1;
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;
364 leaf0 = leaf1 = IP4_FIB_MTRIE_LEAF_ROOT;
366 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, addr0, 0);
367 leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, addr1, 0);
369 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, addr0, 1);
370 leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, addr1, 1);
372 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, addr0, 2);
373 leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, addr1, 2);
375 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, addr0, 3);
376 leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, addr1, 3);
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);
386 lgpe_ip4_lookup (vlib_main_t * vm, vlib_node_runtime_t * node,
387 vlib_frame_t * from_frame)
389 u32 n_left_from, next_index, * from, * to_next;
390 lisp_gpe_main_t * lgm = &lisp_gpe_main;
392 from = vlib_frame_vector_args (from_frame);
393 n_left_from = from_frame->n_vectors;
395 next_index = node->cached_next_index;
397 while (n_left_from > 0)
401 vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
403 while (n_left_from >= 4 && n_left_to_next >= 2)
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;
413 next0 = next1 = LGPE_IP4_LOOKUP_NEXT_LISP_CP_LOOKUP;
415 /* Prefetch next iteration. */
417 vlib_buffer_t * p2, * p3;
419 p2 = vlib_get_buffer (vm, from[2]);
420 p3 = vlib_get_buffer (vm, from[3]);
422 vlib_prefetch_buffer_header (p2, LOAD);
423 vlib_prefetch_buffer_header (p3, LOAD);
425 CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
426 CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
438 b0 = vlib_get_buffer (vm, bi0);
439 b1 = vlib_get_buffer (vm, bi1);
441 ip0 = vlib_buffer_get_current (b0);
442 ip1 = vlib_buffer_get_current (b1);
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];
448 dst_adj0 = ip_get_adjacency (lgm->lookup_main, dst_adj_index0);
449 dst_adj1 = ip_get_adjacency (lgm->lookup_main, dst_adj_index1);
451 src_fib_index0 = dst_adj0->rewrite_header.sw_if_index;
452 src_fib_index1 = dst_adj1->rewrite_header.sw_if_index;
454 /* if default route not hit in ip4 lookup */
455 if (PREDICT_TRUE(src_fib_index0 != (u32) ~0
456 && src_fib_index1 != (u32) ~0))
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);
462 vnet_buffer(b0)->ip.adj_index[VLIB_TX] = src_adj_index0;
463 vnet_buffer(b1)->ip.adj_index[VLIB_TX] = src_adj_index1;
465 src_adj0 = ip_get_adjacency (lgm->lookup_main, src_adj_index0);
466 src_adj1 = ip_get_adjacency (lgm->lookup_main, src_adj_index1);
468 next0 = src_adj0->lookup_next_index;
469 next1 = src_adj1->lookup_next_index;
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;
479 if (src_fib_index0 != (u32) ~0)
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,
486 next0 = src_adj0->lookup_next_index;
487 vnet_buffer (b0)->sw_if_index[VLIB_TX] = src_adj_index0;
489 if (src_fib_index1 != (u32) ~0)
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,
496 next1 = src_adj1->lookup_next_index;
497 vnet_buffer (b1)->sw_if_index[VLIB_TX] = src_adj_index1;
501 vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next,
502 n_left_to_next, bi0, bi1, next0,
506 while (n_left_from > 0 && n_left_to_next > 0)
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;
521 b0 = vlib_get_buffer (vm, bi0);
522 ip0 = vlib_buffer_get_current (b0);
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;
529 /* default route hit in ip4 lookup, send to lisp control plane */
530 if (src_fib_index0 == (u32) ~0)
533 /* src lookup we do here */
534 ip4_src_fib_lookup_one (lgm, src_fib_index0, &ip0->src_address,
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;
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;
544 vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
545 n_left_to_next, bi0, next0);
547 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
549 return from_frame->n_vectors;
553 VLIB_REGISTER_NODE (lgpe_ip4_lookup_node) = {
554 .function = lgpe_ip4_lookup,
555 .name = "lgpe-ip4-lookup",
556 .vector_size = sizeof (u32),
558 .type = VLIB_NODE_TYPE_INTERNAL,
560 .n_next_nodes = LGPE_IP4_LOOKUP_N_NEXT,
562 [LGPE_IP4_LOOKUP_NEXT_DROP] = "error-drop",
563 [LGPE_IP4_LOOKUP_NEXT_LISP_CP_LOOKUP] = "lisp-cp-lookup",
568 lisp_gpe_rewrite (lisp_gpe_tunnel_t * t)
572 lisp_gpe_header_t * lisp0;
573 ip4_udp_lisp_gpe_header_t * h0;
578 vec_validate_aligned(rw, len - 1, CLIB_CACHE_LINE_BYTES);
580 h0 = (ip4_udp_lisp_gpe_header_t *) rw;
582 /* Fixed portion of the (outer) ip4 header */
584 ip0->ip_version_and_header_length = 0x45;
586 ip0->protocol = IP_PROTOCOL_UDP;
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);
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);
597 /* LISP-gpe header */
600 lisp0->flags = t->flags;
601 lisp0->ver_res = t->ver_res;
603 lisp0->next_protocol = t->next_protocol;
604 lisp0->iid = clib_host_to_net_u32 (t->vni);
612 vnet_lisp_gpe_add_del_tunnel (vnet_lisp_gpe_add_del_tunnel_args_t *a,
615 clib_warning ("UNSUPPORTED! Use vnet_lisp_gpe_add_del_fwd_entry");
619 #define foreach_copy_field \
622 _(decap_next_index) \
630 add_del_tunnel (vnet_lisp_gpe_add_del_fwd_entry_args_t *a, u32 * tun_index_res)
632 lisp_gpe_main_t * lgm = &lisp_gpe_main;
633 lisp_gpe_tunnel_t *t = 0;
636 lisp_gpe_tunnel_key_t key;
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);
643 p = mhash_get (&lgm->lisp_gpe_tunnel_by_key, &key);
647 /* adding a tunnel: tunnel must not already exist */
649 return VNET_API_ERROR_INVALID_VALUE;
651 if (a->decap_next_index >= LISP_GPE_INPUT_N_NEXT)
652 return VNET_API_ERROR_INVALID_DECAP_NEXT;
654 pool_get_aligned (lgm->tunnels, t, CLIB_CACHE_LINE_BYTES);
655 memset (t, 0, sizeof (*t));
657 /* copy from arg structure */
658 #define _(x) t->x = a->x;
662 t->src = ip_addr_v4(&a->slocator);
663 t->dst = ip_addr_v4(&a->dlocator);
665 rv = lisp_gpe_rewrite (t);
669 pool_put(lgm->tunnels, t);
673 mhash_set(&lgm->lisp_gpe_tunnel_by_key, &key, t - lgm->tunnels, 0);
675 /* return tunnel index */
677 tun_index_res[0] = t - lgm->tunnels;
681 /* deleting a tunnel: tunnel must exist */
684 clib_warning("Tunnel for eid %U doesn't exist!", format_gid_address,
686 return VNET_API_ERROR_NO_SUCH_ENTRY;
689 t = pool_elt_at_index(lgm->tunnels, p[0]);
691 mhash_unset(&lgm->lisp_gpe_tunnel_by_key, &key, 0);
693 vec_free(t->rewrite);
694 pool_put(lgm->tunnels, t);
701 add_del_negative_fwd_entry (lisp_gpe_main_t * lgm,
702 vnet_lisp_gpe_add_del_fwd_entry_args_t * a)
705 /* setup adjacency for eid */
706 memset (&adj, 0, sizeof(adj));
708 adj.explicit_fib_index = ~0;
710 ip_prefix_t * dpref = &gid_address_ippref(&a->deid);
711 ip_prefix_t * spref = &gid_address_ippref(&a->seid);
716 /* TODO update timers? */
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 */
723 /* for drop fwd entries, just add route, no need to add encap tunnel */
724 adj.lookup_next_index = LGPE_IP4_LOOKUP_NEXT_DROP;
726 /* add/delete route for prefix */
727 return ip4_sd_fib_add_del_route (lgm, dpref, spref, a->table_id, &adj,
736 vnet_lisp_gpe_add_del_fwd_entry (vnet_lisp_gpe_add_del_fwd_entry_args_t * a,
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;
745 /* treat negative fwd entries separately */
747 return add_del_negative_fwd_entry (lgm, a);
749 /* add/del tunnel to tunnels pool and prepares rewrite */
750 rv = add_del_tunnel (a, &tun_index);
754 dpref = &gid_address_ippref(&a->deid);
755 spref = &gid_address_ippref(&a->seid);
757 /* setup adjacency for eid */
758 memset (&adj, 0, sizeof(adj));
760 adj.explicit_fib_index = ~0;
764 /* send packets that hit this adj to lisp-gpe interface output node in
766 lookup_next_index = hash_get(lgm->lgpe_ip4_lookup_next_index_by_table_id,
768 lgpe_sw_if_index = hash_get(lgm->lisp_gpe_hw_if_index_by_table_id,
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);
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];
781 /* add/delete route for prefix */
782 rv = ip4_sd_fib_add_del_route (lgm, dpref, spref, a->table_id, &adj,
785 /* check that everything worked */
786 if (CLIB_DEBUG && a->is_add)
788 adj_index = ip4_sd_fib_get_route (lgm, dpref, spref, a->table_id);
789 ASSERT(adj_index != 0);
791 adjp = ip_get_adjacency (lgm->lookup_main, adj_index[0]);
794 ASSERT(adjp->rewrite_header.node_index == tun_index);
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)
805 unformat_input_t _line_input, * line_input = &_line_input;
807 ip_address_t slocator, dlocator, *slocators = 0, *dlocators = 0;
809 gid_address_t * eids = 0, eid;
810 clib_error_t * error = 0;
813 prefp = &gid_address_ippref(&eid);
815 /* Get a line of input. */
816 if (! unformat_user (input, unformat_line_input, line_input))
819 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
821 if (unformat (line_input, "del"))
823 else if (unformat (line_input, "add"))
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))
830 vec_add1 (eids, eid);
831 vec_add1 (slocators, slocator);
832 vec_add1 (dlocators, dlocator);
836 error = unformat_parse_error (line_input);
840 unformat_free (line_input);
842 if (vec_len (eids) + vec_len (slocators) == 0)
844 error = clib_error_return (0, "expected ip4/ip6 eids/locators.");
848 if (vec_len (eids) != vec_len (slocators))
850 error = clib_error_return (0, "number of eids not equal to that of "
855 for (i = 0; i < vec_len(eids); i++)
857 vnet_lisp_gpe_add_del_fwd_entry_args_t a;
858 memset (&a, 0, sizeof(a));
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);
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,
884 format_decap_next (u8 * s, va_list * args)
886 u32 next_index = va_arg (*args, u32);
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");
897 return format (s, "unknown %d", next_index);
903 format_lisp_gpe_tunnel (u8 * s, va_list * args)
905 lisp_gpe_tunnel_t * t = va_arg (*args, lisp_gpe_tunnel_t *);
906 lisp_gpe_main_t * lgm = &lisp_gpe_main;
909 "[%d] %U (src) %U (dst) fibs: encap %d, decap %d",
911 format_ip4_address, &t->src,
912 format_ip4_address, &t->dst,
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));
919 #define _(n,v) if (t->flags & v) s = format (s, "%s-bit ", #n);
920 foreach_lisp_gpe_flag_bit;
923 s = format (s, "next_protocol %d ver_res %x res %x\n",
924 t->next_protocol, t->ver_res, t->res);
926 s = format (s, "iid %d (0x%x)\n", t->vni, t->vni);
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)
935 lisp_gpe_main_t * lgm = &lisp_gpe_main;
936 lisp_gpe_tunnel_t * t;
938 if (pool_elts (lgm->tunnels) == 0)
939 vlib_cli_output (vm, "No lisp-gpe tunnels configured...");
941 pool_foreach (t, lgm->tunnels,
943 vlib_cli_output (vm, "%U", format_lisp_gpe_tunnel, t);
949 VLIB_CLI_COMMAND (show_lisp_gpe_tunnel_command, static) = {
950 .path = "show lisp gpe tunnel",
951 .function = show_lisp_gpe_tunnel_command_fn,
955 vnet_lisp_gpe_enable_disable (vnet_lisp_gpe_enable_disable_args_t * a)
957 lisp_gpe_main_t * lgm = &lisp_gpe_main;
958 vnet_main_t * vnm = lgm->vnet_main;
962 /* add lgpe_ip4_lookup as possible next_node for ip4 lookup */
963 if (lgm->ip4_lookup_next_lgpe_ip4_lookup == ~0)
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);
971 /* ask cp to re-add ifaces and defaults */
976 CLIB_UNUSED(uword * val);
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;
983 /* remove all tunnels */
984 mhash_foreach(tunnel, val, &lgm->lisp_gpe_tunnel_by_key, ({
985 vec_add1(tunnels, tunnel[0]);
988 vec_foreach(tunnel, tunnels) {
989 memset(at, 0, sizeof(at[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);
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);
1002 vec_foreach(table_id, table_ids) {
1004 ai->table_id = table_id[0];
1006 /* disables interface and removes defaults */
1007 vnet_lisp_gpe_add_del_iface(ai, 0);
1009 vec_free(table_ids);
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)
1019 unformat_input_t _line_input, * line_input = &_line_input;
1021 vnet_lisp_gpe_enable_disable_args_t _a, * a = &_a;
1023 /* Get a line of input. */
1024 if (! unformat_user (input, unformat_line_input, line_input))
1027 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1029 if (unformat (line_input, "enable"))
1031 else if (unformat (line_input, "disable"))
1035 return clib_error_return (0, "parse error: '%U'",
1036 format_unformat_error, line_input);
1040 return vnet_lisp_gpe_enable_disable (a);
1043 VLIB_CLI_COMMAND (enable_disable_lisp_gpe_command, static) = {
1045 .short_help = "lisp gpe [enable|disable]",
1046 .function = lisp_gpe_enable_disable_command_fn,
1050 lisp_gpe_init (vlib_main_t *vm)
1052 lisp_gpe_main_t * lgm = &lisp_gpe_main;
1053 clib_error_t * error = 0;
1055 if ((error = vlib_call_init_function (vm, ip_main_init)))
1058 if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
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;
1067 mhash_init (&lgm->lisp_gpe_tunnel_by_key, sizeof(uword),
1068 sizeof(lisp_gpe_tunnel_key_t));
1070 udp_register_dst_port (vm, UDP_DST_PORT_lisp_gpe,
1071 lisp_gpe_input_node.index, 1 /* is_ip4 */);
1075 VLIB_INIT_FUNCTION(lisp_gpe_init);