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 <vppinfra/error.h>
17 #include <vppinfra/hash.h>
18 #include <vnet/vnet.h>
19 #include <vnet/ip/ip.h>
20 #include <vnet/ip/udp.h>
21 #include <vnet/ethernet/ethernet.h>
22 #include <vnet/lisp-gpe/lisp_gpe.h>
24 #define foreach_lisp_gpe_tx_next \
25 _(DROP, "error-drop") \
26 _(IP4_LOOKUP, "ip4-lookup") \
27 _(IP6_LOOKUP, "ip6-lookup")
31 #define _(sym,str) LISP_GPE_TX_NEXT_##sym,
32 foreach_lisp_gpe_tx_next
40 } lisp_gpe_tx_trace_t;
43 format_lisp_gpe_tx_trace (u8 * s, va_list * args)
45 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
46 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
47 lisp_gpe_tx_trace_t * t = va_arg (*args, lisp_gpe_tx_trace_t *);
49 s = format (s, "LISP-GPE-TX: tunnel %d", t->tunnel_index);
54 get_one_tunnel_inline (lisp_gpe_main_t * lgm, vlib_buffer_t * b0,
55 lisp_gpe_tunnel_t ** t0, u8 is_v4)
57 u32 adj_index0, tunnel_index0;
58 ip_adjacency_t * adj0;
60 /* Get adjacency and from it the tunnel_index */
61 adj_index0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
64 adj0 = ip_get_adjacency (lgm->lm4, adj_index0);
66 adj0 = ip_get_adjacency (lgm->lm6, adj_index0);
68 tunnel_index0 = adj0->if_address_index;
69 t0[0] = pool_elt_at_index(lgm->tunnels, tunnel_index0);
75 encap_one_inline (lisp_gpe_main_t * lgm, vlib_buffer_t * b0,
76 lisp_gpe_tunnel_t * t0, u32 * next0)
78 ASSERT(sizeof(ip4_udp_lisp_gpe_header_t) == 36);
79 ASSERT(sizeof(ip6_udp_lisp_gpe_header_t) == 56);
81 lisp_gpe_sub_tunnel_t * st0;
84 sti0 = vec_elt_at_index(t0->sub_tunnels_lbv,
85 vnet_buffer(b0)->ip.flow_hash % t0->sub_tunnels_lbv_count);
86 st0 = vec_elt_at_index(t0->sub_tunnels, sti0[0]);
89 ip_udp_encap_one (lgm->vlib_main, b0, st0->rewrite, 36, 1);
90 next0[0] = LISP_GPE_TX_NEXT_IP4_LOOKUP;
94 ip_udp_encap_one (lgm->vlib_main, b0, st0->rewrite, 56, 0);
95 next0[0] = LISP_GPE_TX_NEXT_IP6_LOOKUP;
98 /* Reset to look up tunnel partner in the configured FIB */
99 vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->encap_fib_index;
103 get_two_tunnels_inline (lisp_gpe_main_t * lgm, vlib_buffer_t * b0,
104 vlib_buffer_t * b1, lisp_gpe_tunnel_t ** t0,
105 lisp_gpe_tunnel_t ** t1, u8 is_v4)
107 u32 adj_index0, adj_index1, tunnel_index0, tunnel_index1;
108 ip_adjacency_t * adj0, * adj1;
110 /* Get adjacency and from it the tunnel_index */
111 adj_index0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
112 adj_index1 = vnet_buffer(b1)->ip.adj_index[VLIB_TX];
116 adj0 = ip_get_adjacency (lgm->lm4, adj_index0);
117 adj1 = ip_get_adjacency (lgm->lm4, adj_index1);
121 adj0 = ip_get_adjacency (lgm->lm6, adj_index0);
122 adj1 = ip_get_adjacency (lgm->lm6, adj_index1);
125 tunnel_index0 = adj0->if_address_index;
126 tunnel_index1 = adj1->if_address_index;
128 t0[0] = pool_elt_at_index(lgm->tunnels, tunnel_index0);
129 t1[0] = pool_elt_at_index(lgm->tunnels, tunnel_index1);
136 encap_two_inline (lisp_gpe_main_t * lgm, vlib_buffer_t * b0, vlib_buffer_t * b1,
137 lisp_gpe_tunnel_t * t0, lisp_gpe_tunnel_t * t1, u32 * next0,
140 ASSERT(sizeof(ip4_udp_lisp_gpe_header_t) == 36);
141 ASSERT(sizeof(ip6_udp_lisp_gpe_header_t) == 56);
143 lisp_gpe_sub_tunnel_t * st0, * st1;
145 sti0 = vec_elt_at_index(t0->sub_tunnels_lbv,
146 vnet_buffer(b0)->ip.flow_hash % t0->sub_tunnels_lbv_count);
147 sti1 = vec_elt_at_index(t1->sub_tunnels_lbv,
148 vnet_buffer(b1)->ip.flow_hash % t1->sub_tunnels_lbv_count);
149 st0 = vec_elt_at_index(t0->sub_tunnels, sti0[0]);
150 st1 = vec_elt_at_index(t1->sub_tunnels, sti1[0]);
152 if (PREDICT_TRUE(st0->is_ip4 == st1->is_ip4))
156 ip_udp_encap_one (lgm->vlib_main, b0, st0->rewrite, 36, 1);
157 ip_udp_encap_one (lgm->vlib_main, b1, st1->rewrite, 36, 1);
158 next0[0] = next1[0] = LISP_GPE_TX_NEXT_IP4_LOOKUP;
162 ip_udp_encap_one (lgm->vlib_main, b0, st0->rewrite, 56, 0);
163 ip_udp_encap_one (lgm->vlib_main, b1, st1->rewrite, 56, 0);
164 next0[0] = next1[0] = LISP_GPE_TX_NEXT_IP6_LOOKUP;
171 ip_udp_encap_one (lgm->vlib_main, b0, st0->rewrite, 36, 1);
172 ip_udp_encap_one (lgm->vlib_main, b1, st1->rewrite, 56, 1);
173 next0[0] = LISP_GPE_TX_NEXT_IP4_LOOKUP;
174 next1[0] = LISP_GPE_TX_NEXT_IP6_LOOKUP;
178 ip_udp_encap_one (lgm->vlib_main, b0, st0->rewrite, 56, 1);
179 ip_udp_encap_one (lgm->vlib_main, b1, st1->rewrite, 36, 1);
180 next0[0] = LISP_GPE_TX_NEXT_IP6_LOOKUP;
181 next1[0] = LISP_GPE_TX_NEXT_IP4_LOOKUP;
185 /* Reset to look up tunnel partner in the configured FIB */
186 vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->encap_fib_index;
187 vnet_buffer(b1)->sw_if_index[VLIB_TX] = t1->encap_fib_index;
190 #define is_v4_packet(_h) ((*(u8*) _h) & 0xF0) == 0x40
193 lisp_gpe_interface_tx (vlib_main_t * vm, vlib_node_runtime_t * node,
194 vlib_frame_t * from_frame)
196 u32 n_left_from, next_index, * from, * to_next;
197 lisp_gpe_main_t * lgm = &lisp_gpe_main;
199 from = vlib_frame_vector_args (from_frame);
200 n_left_from = from_frame->n_vectors;
202 next_index = node->cached_next_index;
204 while (n_left_from > 0)
208 vlib_get_next_frame (vm, node, next_index,
209 to_next, n_left_to_next);
211 while (n_left_from >= 4 && n_left_to_next >= 2)
214 vlib_buffer_t * b0, * b1;
216 lisp_gpe_tunnel_t * t0 = 0, * t1 = 0;
217 u8 is_v4_eid0, is_v4_eid1;
219 next0 = next1 = LISP_GPE_TX_NEXT_IP4_LOOKUP;
221 /* Prefetch next iteration. */
223 vlib_buffer_t * p2, *p3;
225 p2 = vlib_get_buffer (vm, from[2]);
226 p3 = vlib_get_buffer (vm, from[3]);
228 vlib_prefetch_buffer_header(p2, LOAD);
229 vlib_prefetch_buffer_header(p3, LOAD);
231 CLIB_PREFETCH(p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
232 CLIB_PREFETCH(p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
244 b0 = vlib_get_buffer (vm, bi0);
245 b1 = vlib_get_buffer (vm, bi1);
247 is_v4_eid0 = is_v4_packet(vlib_buffer_get_current (b0));
248 is_v4_eid1 = is_v4_packet(vlib_buffer_get_current (b1));
250 if (PREDICT_TRUE(is_v4_eid0 == is_v4_eid1))
252 get_two_tunnels_inline (lgm, b0, b1, &t0, &t1,
257 get_one_tunnel_inline (lgm, b0, &t0, is_v4_eid0 ? 1 : 0);
258 get_one_tunnel_inline (lgm, b1, &t1, is_v4_eid1 ? 1 : 0);
261 encap_two_inline (lgm, b0, b1, t0, t1, &next0, &next1);
263 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
265 lisp_gpe_tx_trace_t *tr = vlib_add_trace (vm, node, b0,
267 tr->tunnel_index = t0 - lgm->tunnels;
269 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
271 lisp_gpe_tx_trace_t *tr = vlib_add_trace (vm, node, b1,
273 tr->tunnel_index = t1 - lgm->tunnels;
276 vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next,
277 n_left_to_next, bi0, bi1, next0,
281 while (n_left_from > 0 && n_left_to_next > 0)
284 u32 bi0, next0 = LISP_GPE_TX_NEXT_IP4_LOOKUP;
285 lisp_gpe_tunnel_t * t0 = 0;
295 b0 = vlib_get_buffer (vm, bi0);
297 is_v4_0 = is_v4_packet(vlib_buffer_get_current (b0));
298 get_one_tunnel_inline (lgm, b0, &t0, is_v4_0 ? 1 : 0);
300 encap_one_inline (lgm, b0, t0, &next0);
302 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
304 lisp_gpe_tx_trace_t *tr = vlib_add_trace (vm, node, b0,
306 tr->tunnel_index = t0 - lgm->tunnels;
308 vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
309 n_left_to_next, bi0, next0);
312 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
315 return from_frame->n_vectors;
319 format_lisp_gpe_name (u8 * s, va_list * args)
321 u32 dev_instance = va_arg (*args, u32);
322 return format (s, "lisp_gpe%d", dev_instance);
325 VNET_DEVICE_CLASS (lisp_gpe_device_class,static) = {
327 .format_device_name = format_lisp_gpe_name,
328 .format_tx_trace = format_lisp_gpe_tx_trace,
329 .tx_function = lisp_gpe_interface_tx,
330 .no_flatten_output_chains = 1,
334 dummy_set_rewrite (vnet_main_t * vnm, u32 sw_if_index, u32 l3_type,
335 void * dst_address, void * rewrite, uword max_rewrite_bytes)
341 format_lisp_gpe_header_with_length (u8 * s, va_list * args)
343 lisp_gpe_header_t * h = va_arg (*args, lisp_gpe_header_t *);
344 u32 max_header_bytes = va_arg (*args, u32);
347 header_bytes = sizeof (h[0]);
348 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
349 return format (s, "lisp-gpe header truncated");
351 s = format (s, "flags: ");
352 #define _(n,v) if (h->flags & v) s = format (s, "%s ", #n);
353 foreach_lisp_gpe_flag_bit;
356 s = format (s, "\n ver_res %d res %d next_protocol %d iid %d(%x)",
357 h->ver_res, h->res, h->next_protocol,
358 clib_net_to_host_u32 (h->iid),
359 clib_net_to_host_u32 (h->iid));
363 VNET_HW_INTERFACE_CLASS (lisp_gpe_hw_class) = {
365 .format_header = format_lisp_gpe_header_with_length,
366 .set_rewrite = dummy_set_rewrite,
370 add_del_ip_prefix_route (ip_prefix_t * dst_prefix, u32 table_id,
371 ip_adjacency_t * add_adj, u8 is_add, u32 * adj_index)
375 if (ip_prefix_version(dst_prefix) == IP4)
377 ip4_main_t * im4 = &ip4_main;
378 ip4_add_del_route_args_t a;
379 ip4_address_t addr = ip_prefix_v4(dst_prefix);
381 memset(&a, 0, sizeof(a));
382 a.flags = IP4_ROUTE_FLAG_TABLE_ID;
383 a.table_index_or_table_id = table_id;
385 a.dst_address_length = ip_prefix_len(dst_prefix);
386 a.dst_address = addr;
387 a.flags |= is_add ? IP4_ROUTE_FLAG_ADD : IP4_ROUTE_FLAG_DEL;
389 a.n_add_adj = is_add ? 1 : 0;
391 ip4_add_del_route (im4, &a);
395 p = ip4_get_route (im4, table_id, 0, addr.as_u8,
396 ip_prefix_len(dst_prefix));
399 clib_warning("Failed to insert route for eid %U!",
400 format_ip4_address_and_length, addr.as_u8,
401 ip_prefix_len(dst_prefix));
409 ip6_main_t * im6 = &ip6_main;
410 ip6_add_del_route_args_t a;
411 ip6_address_t addr = ip_prefix_v6(dst_prefix);
413 memset(&a, 0, sizeof(a));
414 a.flags = IP6_ROUTE_FLAG_TABLE_ID;
415 a.table_index_or_table_id = table_id;
417 a.dst_address_length = ip_prefix_len(dst_prefix);
418 a.dst_address = addr;
419 a.flags |= is_add ? IP6_ROUTE_FLAG_ADD : IP6_ROUTE_FLAG_DEL;
421 a.n_add_adj = is_add ? 1 : 0;
423 ip6_add_del_route (im6, &a);
427 adj_index[0] = ip6_get_route (im6, table_id, 0, &addr,
428 ip_prefix_len(dst_prefix));
429 if (adj_index[0] == 0)
431 clib_warning("Failed to insert route for eid %U!",
432 format_ip6_address_and_length, addr.as_u8,
433 ip_prefix_len(dst_prefix));
442 add_del_lisp_gpe_default_route (u32 table_id, u8 is_v4, u8 is_add)
444 lisp_gpe_main_t * lgm = &lisp_gpe_main;
449 /* setup adjacency */
450 memset (&adj, 0, sizeof(adj));
453 adj.explicit_fib_index = ~0;
454 adj.lookup_next_index = is_v4 ? lgm->ip4_lookup_next_lgpe_ip4_lookup :
455 lgm->ip6_lookup_next_lgpe_ip6_lookup;
456 /* default route has tunnel_index ~0 */
457 adj.rewrite_header.sw_if_index = ~0;
459 /* set prefix to 0/0 */
460 memset(&prefix, 0, sizeof(prefix));
461 ip_prefix_version(&prefix) = is_v4 ? IP4 : IP6;
463 /* add/delete route for prefix */
464 add_del_ip_prefix_route (&prefix, table_id, &adj, is_add, &adj_index);
468 lisp_gpe_iface_set_table (u32 sw_if_index, u32 table_id, u8 is_ip4)
472 ip4_main_t * im4 = &ip4_main;
474 fib = find_ip4_fib_by_table_index_or_id (im4, table_id,
475 IP4_ROUTE_FLAG_TABLE_ID);
477 /* fib's created if it doesn't exist */
480 vec_validate(im4->fib_index_by_sw_if_index, sw_if_index);
481 im4->fib_index_by_sw_if_index[sw_if_index] = fib->index;
485 ip6_main_t * im6 = &ip6_main;
487 fib = find_ip6_fib_by_table_index_or_id (im6, table_id,
488 IP6_ROUTE_FLAG_TABLE_ID);
490 /* fib's created if it doesn't exist */
493 vec_validate(im6->fib_index_by_sw_if_index, sw_if_index);
494 im6->fib_index_by_sw_if_index[sw_if_index] = fib->index;
498 #define foreach_l2_lisp_gpe_tx_next \
499 _(DROP, "error-drop") \
500 _(IP4_LOOKUP, "ip4-lookup") \
501 _(IP6_LOOKUP, "ip6-lookup") \
502 _(LISP_CP_LOOKUP, "lisp-cp-lookup")
506 #define _(sym,str) L2_LISP_GPE_TX_NEXT_##sym,
507 foreach_l2_lisp_gpe_tx_next
509 L2_LISP_GPE_TX_N_NEXT,
510 } l2_lisp_gpe_tx_next_t;
515 } l2_lisp_gpe_tx_trace_t;
518 format_l2_lisp_gpe_tx_trace (u8 * s, va_list * args)
520 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
521 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
522 l2_lisp_gpe_tx_trace_t * t = va_arg (*args, l2_lisp_gpe_tx_trace_t *);
524 s = format (s, "L2-LISP-GPE-TX: tunnel %d", t->tunnel_index);
529 l2_process_tunnel_action (vlib_buffer_t * b0, u8 action, u32 * next0)
531 if (LISP_SEND_MAP_REQUEST == action)
533 next0[0] = L2_LISP_GPE_TX_NEXT_LISP_CP_LOOKUP;
534 vnet_buffer(b0)->lisp.overlay_afi = LISP_AFI_MAC;
538 next0[0] = L2_LISP_GPE_TX_NEXT_DROP;
543 ip_flow_hash (void * data)
545 ip4_header_t * iph = (ip4_header_t *) data;
547 if ((iph->ip_version_and_header_length & 0xF0) == 0x40)
548 return ip4_compute_flow_hash (iph, IP_FLOW_HASH_DEFAULT);
550 return ip6_compute_flow_hash ((ip6_header_t *) iph, IP_FLOW_HASH_DEFAULT);
554 l2_flow_hash (vlib_buffer_t * b0)
556 ethernet_header_t * eh;
558 uword is_ip, eh_size;
561 eh = vlib_buffer_get_current (b0);
562 eh_type = clib_net_to_host_u16(eh->type);
563 eh_size = ethernet_buffer_header_size(b0);
565 is_ip = (eh_type == ETHERNET_TYPE_IP4 || eh_type == ETHERNET_TYPE_IP6);
567 /* since we have 2 cache lines, use them */
569 a = ip_flow_hash ((u8 *) vlib_buffer_get_current (b0) + eh_size);
573 b = mac_to_u64((u8 *)eh->dst_address);
574 c = mac_to_u64((u8 *)eh->src_address);
575 hash_mix64 (a, b, c);
581 l2_process_one (lisp_gpe_main_t * lgm, vlib_buffer_t * b0, u32 ti0, u32 * next0)
583 lisp_gpe_tunnel_t * t0;
585 t0 = pool_elt_at_index(lgm->tunnels, ti0);
588 if (PREDICT_TRUE(LISP_NO_ACTION == t0->action))
590 /* compute 'flow' hash */
591 if (PREDICT_TRUE(t0->sub_tunnels_lbv_count > 1))
592 vnet_buffer(b0)->ip.flow_hash = l2_flow_hash (b0);
593 encap_one_inline (lgm, b0, t0, next0);
597 l2_process_tunnel_action(b0, t0->action, next0);
602 l2_process_two (lisp_gpe_main_t * lgm, vlib_buffer_t * b0, vlib_buffer_t * b1,
603 u32 ti0, u32 ti1, u32 * next0, u32 * next1)
605 lisp_gpe_tunnel_t * t0, * t1;
607 t0 = pool_elt_at_index(lgm->tunnels, ti0);
608 t1 = pool_elt_at_index(lgm->tunnels, ti1);
610 ASSERT(0 != t0 && 0 != t1);
612 if (PREDICT_TRUE(LISP_NO_ACTION == t0->action
613 && LISP_NO_ACTION == t1->action))
615 if (PREDICT_TRUE(t0->sub_tunnels_lbv_count > 1))
616 vnet_buffer(b0)->ip.flow_hash = l2_flow_hash(b0);
617 if (PREDICT_TRUE(t1->sub_tunnels_lbv_count > 1))
618 vnet_buffer(b1)->ip.flow_hash = l2_flow_hash(b1);
619 encap_two_inline (lgm, b0, b1, t0, t1, next0, next1);
623 if (LISP_NO_ACTION == t0->action)
625 if (PREDICT_TRUE(t0->sub_tunnels_lbv_count > 1))
626 vnet_buffer(b0)->ip.flow_hash = l2_flow_hash(b0);
627 encap_one_inline (lgm, b0, t0, next0);
628 l2_process_tunnel_action (b1, t1->action, next1);
630 else if (LISP_NO_ACTION == t1->action)
632 if (PREDICT_TRUE(t1->sub_tunnels_lbv_count > 1))
633 vnet_buffer(b1)->ip.flow_hash = l2_flow_hash(b1);
634 encap_one_inline (lgm, b1, t1, next1);
635 l2_process_tunnel_action (b0, t0->action, next0);
639 l2_process_tunnel_action (b0, t0->action, next0);
640 l2_process_tunnel_action (b1, t1->action, next1);
646 l2_lisp_gpe_interface_tx (vlib_main_t * vm, vlib_node_runtime_t * node,
647 vlib_frame_t * from_frame)
649 u32 n_left_from, next_index, * from, * to_next;
650 lisp_gpe_main_t * lgm = &lisp_gpe_main;
652 from = vlib_frame_vector_args (from_frame);
653 n_left_from = from_frame->n_vectors;
655 next_index = node->cached_next_index;
657 while (n_left_from > 0)
661 vlib_get_next_frame (vm, node, next_index,
662 to_next, n_left_to_next);
664 while (n_left_from >= 4 && n_left_to_next >= 2)
667 vlib_buffer_t * b0, * b1;
668 u32 next0, next1, ti0, ti1;
669 lisp_gpe_tunnel_t * t0 = 0, * t1 = 0;
670 ethernet_header_t * e0, * e1;
672 next0 = next1 = L2_LISP_GPE_TX_NEXT_LISP_CP_LOOKUP;
674 /* Prefetch next iteration. */
676 vlib_buffer_t * p2, *p3;
678 p2 = vlib_get_buffer (vm, from[2]);
679 p3 = vlib_get_buffer (vm, from[3]);
681 vlib_prefetch_buffer_header(p2, LOAD);
682 vlib_prefetch_buffer_header(p3, LOAD);
684 CLIB_PREFETCH(p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
685 CLIB_PREFETCH(p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
697 b0 = vlib_get_buffer (vm, bi0);
698 b1 = vlib_get_buffer (vm, bi1);
700 e0 = vlib_buffer_get_current (b0);
701 e1 = vlib_buffer_get_current (b1);
703 /* lookup dst + src mac */
704 ti0 = lisp_l2_fib_lookup (lgm, vnet_buffer(b0)->l2.bd_index,
705 e0->src_address, e0->dst_address);
706 ti1 = lisp_l2_fib_lookup (lgm, vnet_buffer(b1)->l2.bd_index,
707 e1->src_address, e1->dst_address);
709 if (PREDICT_TRUE((u32)~0 != ti0) && (u32)~0 != ti1)
711 /* process both tunnels */
712 l2_process_two (lgm, b0, b1, ti0, ti1, &next0, &next1);
718 /* process tunnel for b0 */
719 l2_process_one (lgm, b0, ti0, &next0);
721 /* no tunnel found for b1, send to control plane */
722 next1 = L2_LISP_GPE_TX_NEXT_LISP_CP_LOOKUP;
723 vnet_buffer(b1)->lisp.overlay_afi = LISP_AFI_MAC;
725 else if ((u32)~0 != ti1)
727 /* process tunnel for b1 */
728 l2_process_one (lgm, b1, ti1, &next1);
730 /* no tunnel found b0, send to control plane */
731 next0 = L2_LISP_GPE_TX_NEXT_LISP_CP_LOOKUP;
732 vnet_buffer(b0)->lisp.overlay_afi = LISP_AFI_MAC;
736 /* no tunnels found */
737 next0 = L2_LISP_GPE_TX_NEXT_LISP_CP_LOOKUP;
738 vnet_buffer(b0)->lisp.overlay_afi = LISP_AFI_MAC;
739 next1 = L2_LISP_GPE_TX_NEXT_LISP_CP_LOOKUP;
740 vnet_buffer(b1)->lisp.overlay_afi = LISP_AFI_MAC;
744 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
746 l2_lisp_gpe_tx_trace_t *tr = vlib_add_trace (vm, node, b0,
748 tr->tunnel_index = t0 - lgm->tunnels;
750 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
752 l2_lisp_gpe_tx_trace_t *tr = vlib_add_trace (vm, node, b1,
754 tr->tunnel_index = t1 - lgm->tunnels;
757 vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next,
758 n_left_to_next, bi0, bi1, next0,
762 while (n_left_from > 0 && n_left_to_next > 0)
765 u32 bi0, ti0, next0 = L2_LISP_GPE_TX_NEXT_LISP_CP_LOOKUP;
766 ethernet_header_t * e0;
775 b0 = vlib_get_buffer (vm, bi0);
776 e0 = vlib_buffer_get_current (b0);
778 /* lookup dst + src mac */
779 ti0 = lisp_l2_fib_lookup (lgm, vnet_buffer(b0)->l2.bd_index,
780 e0->src_address, e0->dst_address);
782 if (PREDICT_TRUE((u32)~0 != ti0))
784 l2_process_one (lgm, b0, ti0, &next0);
788 /* no tunnel found send to control plane */
789 next0 = L2_LISP_GPE_TX_NEXT_LISP_CP_LOOKUP;
790 vnet_buffer(b0)->lisp.overlay_afi = LISP_AFI_MAC;
793 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
795 l2_lisp_gpe_tx_trace_t *tr = vlib_add_trace (vm, node, b0,
797 tr->tunnel_index = ti0 ? ti0 : ~0;
799 vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
800 n_left_to_next, bi0, next0);
803 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
806 return from_frame->n_vectors;
810 format_l2_lisp_gpe_name (u8 * s, va_list * args)
812 u32 dev_instance = va_arg (*args, u32);
813 return format (s, "l2_lisp_gpe%d", dev_instance);
816 VNET_DEVICE_CLASS (l2_lisp_gpe_device_class,static) = {
817 .name = "L2_LISP_GPE",
818 .format_device_name = format_l2_lisp_gpe_name,
819 .format_tx_trace = format_lisp_gpe_tx_trace,
820 .tx_function = l2_lisp_gpe_interface_tx,
821 .no_flatten_output_chains = 1,
825 static vnet_hw_interface_t *
826 create_lisp_gpe_iface (lisp_gpe_main_t * lgm, u32 vni, u32 dp_table,
827 vnet_device_class_t * dev_class,
828 tunnel_lookup_t * tuns)
831 u32 hw_if_index = ~0;
833 vnet_hw_interface_t * hi;
834 vnet_main_t * vnm = lgm->vnet_main;
836 /* create hw lisp_gpeX iface if needed, otherwise reuse existing */
837 flen = vec_len(lgm->free_tunnel_hw_if_indices);
840 hw_if_index = lgm->free_tunnel_hw_if_indices[flen - 1];
841 _vec_len(lgm->free_tunnel_hw_if_indices) -= 1;
843 hi = vnet_get_hw_interface (vnm, hw_if_index);
845 /* rename interface */
846 new_name = format (0, "%U", dev_class->format_device_name,
849 vec_add1(new_name, 0);
850 vnet_rename_interface (vnm, hw_if_index, (char *) new_name);
853 /* clear old stats of freed interface before reuse */
854 vnet_interface_main_t * im = &vnm->interface_main;
855 vnet_interface_counter_lock (im);
856 vlib_zero_combined_counter (
857 &im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX],
859 vlib_zero_combined_counter (
860 &im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX],
862 vlib_zero_simple_counter (
863 &im->sw_if_counters[VNET_INTERFACE_COUNTER_DROP],
865 vnet_interface_counter_unlock (im);
869 hw_if_index = vnet_register_interface (vnm, dev_class->index, vni,
870 lisp_gpe_hw_class.index, 0);
871 hi = vnet_get_hw_interface (vnm, hw_if_index);
874 hash_set(tuns->hw_if_index_by_dp_table, dp_table, hw_if_index);
876 /* set tunnel termination: post decap, packets are tagged as having been
877 * originated by lisp-gpe interface */
878 hash_set(tuns->sw_if_index_by_vni, vni, hi->sw_if_index);
879 hash_set(tuns->vni_by_sw_if_index, hi->sw_if_index, vni);
885 remove_lisp_gpe_iface (lisp_gpe_main_t * lgm, u32 hi_index, u32 dp_table,
886 tunnel_lookup_t * tuns)
888 vnet_main_t * vnm = lgm->vnet_main;
889 vnet_hw_interface_t * hi;
892 hi = vnet_get_hw_interface (vnm, hi_index);
894 /* disable interface */
895 vnet_sw_interface_set_flags (vnm, hi->sw_if_index, 0/* down */);
896 vnet_hw_interface_set_flags (vnm, hi->hw_if_index, 0/* down */);
897 hash_unset(tuns->hw_if_index_by_dp_table, dp_table);
898 vec_add1(lgm->free_tunnel_hw_if_indices, hi->hw_if_index);
900 /* clean tunnel termination and vni to sw_if_index binding */
901 vnip = hash_get(tuns->vni_by_sw_if_index, hi->sw_if_index);
904 clib_warning ("No vni associated to interface %d", hi->sw_if_index);
907 hash_unset(tuns->sw_if_index_by_vni, vnip[0]);
908 hash_unset(tuns->vni_by_sw_if_index, hi->sw_if_index);
912 lisp_gpe_add_del_l3_iface (lisp_gpe_main_t * lgm,
913 vnet_lisp_gpe_add_del_iface_args_t * a)
915 vnet_main_t * vnm = lgm->vnet_main;
916 tunnel_lookup_t * l3_ifaces = &lgm->l3_ifaces;
917 vnet_hw_interface_t * hi;
918 u32 lookup_next_index4, lookup_next_index6;
921 hip = hash_get(l3_ifaces->hw_if_index_by_dp_table, a->table_id);
927 clib_warning ("vrf %d already mapped to a vni", a->table_id);
931 si = hash_get(l3_ifaces->sw_if_index_by_vni, a->vni);
934 clib_warning ("Interface for vni %d already exists", a->vni);
938 /* create lisp iface and populate tunnel tables */
939 hi = create_lisp_gpe_iface (lgm, a->vni, a->table_id,
940 &lisp_gpe_device_class, l3_ifaces);
942 /* set ingress arc from lgpe_ipX_lookup */
943 lookup_next_index4 = vlib_node_add_next (lgm->vlib_main,
944 lgpe_ip4_lookup_node.index,
945 hi->output_node_index);
946 lookup_next_index6 = vlib_node_add_next (lgm->vlib_main,
947 lgpe_ip6_lookup_node.index,
948 hi->output_node_index);
949 hash_set(lgm->lgpe_ip4_lookup_next_index_by_table_id, a->table_id,
951 hash_set(lgm->lgpe_ip6_lookup_next_index_by_table_id, a->table_id,
954 /* insert default routes that point to lgpe-ipx-lookup */
955 add_del_lisp_gpe_default_route (a->table_id, /* is_v4 */1, 1);
956 add_del_lisp_gpe_default_route (a->table_id, /* is_v4 */0, 1);
958 /* set egress arcs */
959 #define _(sym,str) vlib_node_add_named_next_with_slot (vnm->vlib_main, \
960 hi->tx_node_index, str, LISP_GPE_TX_NEXT_##sym);
961 foreach_lisp_gpe_tx_next
964 /* set interface in appropriate v4 and v6 FIBs */
965 lisp_gpe_iface_set_table (hi->sw_if_index, a->table_id, 1);
966 lisp_gpe_iface_set_table (hi->sw_if_index, a->table_id, 0);
968 /* enable interface */
969 vnet_sw_interface_set_flags (vnm, hi->sw_if_index,
970 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
971 vnet_hw_interface_set_flags (vnm, hi->hw_if_index,
972 VNET_HW_INTERFACE_FLAG_LINK_UP);
978 clib_warning("The interface for vrf %d doesn't exist", a->table_id);
982 remove_lisp_gpe_iface (lgm, hip[0], a->table_id, &lgm->l3_ifaces);
984 /* unset default routes */
985 add_del_lisp_gpe_default_route (a->table_id, /* is_v4 */1, 0);
986 add_del_lisp_gpe_default_route (a->table_id, /* is_v4 */0, 0);
993 lisp_gpe_add_del_l2_iface (lisp_gpe_main_t * lgm,
994 vnet_lisp_gpe_add_del_iface_args_t * a)
996 vnet_main_t * vnm = lgm->vnet_main;
997 tunnel_lookup_t * l2_ifaces = &lgm->l2_ifaces;
998 vnet_hw_interface_t * hi;
1002 bd_index = bd_find_or_add_bd_index(&bd_main, a->bd_id);
1003 hip = hash_get(l2_ifaces->hw_if_index_by_dp_table, bd_index);
1009 clib_warning("bridge domain %d already mapped to a vni", a->bd_id);
1013 si = hash_get(l2_ifaces->sw_if_index_by_vni, a->vni);
1016 clib_warning ("Interface for vni %d already exists", a->vni);
1020 /* create lisp iface and populate tunnel tables */
1021 hi = create_lisp_gpe_iface (lgm, a->vni, bd_index,
1022 &l2_lisp_gpe_device_class, &lgm->l2_ifaces);
1024 /* add iface to l2 bridge domain */
1025 set_int_l2_mode (lgm->vlib_main, vnm, MODE_L2_BRIDGE, hi->sw_if_index,
1028 /* set egress arcs */
1029 #define _(sym,str) vlib_node_add_named_next_with_slot (vnm->vlib_main, \
1030 hi->tx_node_index, str, L2_LISP_GPE_TX_NEXT_##sym);
1031 foreach_l2_lisp_gpe_tx_next
1034 /* enable interface */
1035 vnet_sw_interface_set_flags (vnm, hi->sw_if_index,
1036 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
1037 vnet_hw_interface_set_flags (vnm, hi->hw_if_index,
1038 VNET_HW_INTERFACE_FLAG_LINK_UP);
1044 clib_warning("The interface for bridge domain %d doesn't exist",
1048 remove_lisp_gpe_iface (lgm, hip[0], bd_index, &lgm->l2_ifaces);
1055 vnet_lisp_gpe_add_del_iface (vnet_lisp_gpe_add_del_iface_args_t * a,
1058 lisp_gpe_main_t * lgm = &lisp_gpe_main;
1060 if (vnet_lisp_gpe_enable_disable_status() == 0)
1062 clib_warning ("LISP is disabled!");
1063 return VNET_API_ERROR_LISP_DISABLED;
1067 return lisp_gpe_add_del_l3_iface (lgm, a);
1069 return lisp_gpe_add_del_l2_iface (lgm, a);
1072 static clib_error_t *
1073 lisp_gpe_add_del_iface_command_fn (vlib_main_t * vm, unformat_input_t * input,
1074 vlib_cli_command_t * cmd)
1076 unformat_input_t _line_input, * line_input = &_line_input;
1078 clib_error_t * error = 0;
1080 u32 table_id, vni, bd_id;
1081 u8 vni_is_set = 0, vrf_is_set = 0, bd_index_is_set = 0;
1083 vnet_lisp_gpe_add_del_iface_args_t _a, * a = &_a;
1085 /* Get a line of input. */
1086 if (! unformat_user (input, unformat_line_input, line_input))
1089 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1091 if (unformat (line_input, "add"))
1093 else if (unformat (line_input, "del"))
1095 else if (unformat (line_input, "vrf %d", &table_id))
1099 else if (unformat (line_input, "vni %d", &vni))
1103 else if (unformat (line_input, "bd %d", &bd_id))
1105 bd_index_is_set = 1;
1109 return clib_error_return (0, "parse error: '%U'",
1110 format_unformat_error, line_input);
1114 if (vrf_is_set && bd_index_is_set)
1115 return clib_error_return(0, "Cannot set both vrf and brdige domain index!");
1118 return clib_error_return(0, "vni must be set!");
1120 if (!vrf_is_set && !bd_index_is_set)
1121 return clib_error_return(0, "vrf or bridge domain index must be set!");
1124 a->dp_table = vrf_is_set ? table_id : bd_id;
1126 a->is_l2 = bd_index_is_set;
1128 rv = vnet_lisp_gpe_add_del_iface (a, 0);
1131 error = clib_error_return(0, "failed to %s gpe iface!",
1132 is_add ? "add" : "delete");
1138 VLIB_CLI_COMMAND (add_del_lisp_gpe_iface_command, static) = {
1139 .path = "lisp gpe iface",
1140 .short_help = "lisp gpe iface add/del vni <vni> vrf <vrf>",
1141 .function = lisp_gpe_add_del_iface_command_fn,