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.
17 * @brief Common utility functions for IPv4, IPv6 and L2 LISP-GPE tunnels.
20 #include <vnet/lisp-gpe/lisp_gpe.h>
21 #include <vnet/lisp-gpe/lisp_gpe_tunnel.h>
22 #include <vnet/lisp-gpe/lisp_gpe_adjacency.h>
24 #include <vnet/fib/fib_table.h>
27 * @brief Pool of all LISP tunnels
29 static lisp_gpe_tunnel_t *lisp_gpe_tunnel_pool;
32 * @brief a DB of all tunnels
34 static uword *lisp_gpe_tunnel_db;
37 * @brief Compute IP-UDP-GPE sub-tunnel encap/rewrite header.
39 * @param[in] t Parent of the sub-tunnel.
40 * @param[in] st Sub-tunnel.
41 * @param[in] lp Local and remote locators used in the encap header.
43 * @return 0 on success.
46 lisp_gpe_tunnel_build_rewrite (const lisp_gpe_tunnel_t * lgt,
47 const lisp_gpe_adjacency_t * ladj,
48 lisp_gpe_next_protocol_e payload_proto)
50 lisp_gpe_header_t *lisp0;
53 gpe_encap_mode_t encap_mode = vnet_gpe_get_encap_mode ();
55 if (IP4 == ip_addr_version (&lgt->key->lcl))
57 ip4_udp_lisp_gpe_header_t *h0;
62 vec_validate_aligned (rw, len - 1, CLIB_CACHE_LINE_BYTES);
64 h0 = (ip4_udp_lisp_gpe_header_t *) rw;
66 /* Fixed portion of the (outer) ip4 header */
68 ip0->ip_version_and_header_length = 0x45;
70 ip0->protocol = IP_PROTOCOL_UDP;
72 /* we fix up the ip4 header length and checksum after-the-fact */
73 ip_address_copy_addr (&ip0->src_address, &lgt->key->lcl);
74 ip_address_copy_addr (&ip0->dst_address, &lgt->key->rmt);
75 ip0->checksum = ip4_header_checksum (ip0);
77 /* UDP header, randomize src port on something, maybe? */
78 h0->udp.src_port = clib_host_to_net_u16 (4341);
79 h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_lisp_gpe);
86 ip6_udp_lisp_gpe_header_t *h0;
91 vec_validate_aligned (rw, len - 1, CLIB_CACHE_LINE_BYTES);
93 h0 = (ip6_udp_lisp_gpe_header_t *) rw;
95 /* Fixed portion of the (outer) ip6 header */
97 ip0->ip_version_traffic_class_and_flow_label =
98 clib_host_to_net_u32 (0x6 << 28);
100 ip0->protocol = IP_PROTOCOL_UDP;
102 /* we fix up the ip6 header length after-the-fact */
103 ip_address_copy_addr (&ip0->src_address, &lgt->key->lcl);
104 ip_address_copy_addr (&ip0->dst_address, &lgt->key->rmt);
106 /* UDP header, randomize src port on something, maybe? */
107 h0->udp.src_port = clib_host_to_net_u16 (4341);
108 h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_lisp_gpe);
110 /* LISP-gpe header */
114 lisp0->flags = ladj->flags;
115 if (GPE_ENCAP_VXLAN == encap_mode)
117 lisp0->flags &= ~LISP_GPE_FLAGS_P;
121 lisp0->next_protocol = payload_proto;
122 lisp0->iid = clib_host_to_net_u32 (ladj->vni) >> 8; /* first 24 bits only */
127 static lisp_gpe_tunnel_t *
128 lisp_gpe_tunnel_db_find (const lisp_gpe_tunnel_key_t * key)
132 p = hash_get_mem (lisp_gpe_tunnel_db, (void *) key);
136 return (pool_elt_at_index (lisp_gpe_tunnel_pool, p[0]));
142 lisp_gpe_tunnel_get_i (index_t lgti)
144 return (pool_elt_at_index (lisp_gpe_tunnel_pool, lgti));
148 lisp_gpe_tunnel_find_or_create_and_lock (const locator_pair_t * pair,
151 lisp_gpe_tunnel_key_t key = {
152 .lcl = pair->lcl_loc,
153 .rmt = pair->rmt_loc,
154 .fib_index = rloc_fib_index,
156 lisp_gpe_tunnel_t *lgt;
159 lgt = lisp_gpe_tunnel_db_find (&key);
163 pool_get (lisp_gpe_tunnel_pool, lgt);
164 clib_memset (lgt, 0, sizeof (*lgt));
166 lgt->key = clib_mem_alloc (sizeof (*lgt->key));
167 clib_memset (lgt->key, 0, sizeof (*lgt->key));
169 lgt->key->rmt = pair->rmt_loc;
170 lgt->key->lcl = pair->lcl_loc;
171 lgt->key->fib_index = rloc_fib_index;
174 * source the FIB entry for the RLOC so we can track its forwarding
177 ip_address_to_fib_prefix (&lgt->key->rmt, &pfx);
179 lgt->fib_entry_index = fib_table_entry_special_add (rloc_fib_index,
182 FIB_ENTRY_FLAG_NONE);
184 hash_set_mem (lisp_gpe_tunnel_db, &lgt->key,
185 (lgt - lisp_gpe_tunnel_pool));
190 return (lgt - lisp_gpe_tunnel_pool);
194 lisp_gpe_tunnel_unlock (index_t lgti)
196 lisp_gpe_tunnel_t *lgt;
198 lgt = lisp_gpe_tunnel_get_i (lgti);
203 hash_unset_mem (lisp_gpe_tunnel_db, &lgt->key);
204 clib_mem_free (lgt->key);
205 pool_put (lisp_gpe_tunnel_pool, lgt);
209 const lisp_gpe_tunnel_t *
210 lisp_gpe_tunnel_get (index_t lgti)
212 return (lisp_gpe_tunnel_get_i (lgti));
215 /** Format LISP-GPE tunnel. */
217 format_lisp_gpe_tunnel (u8 * s, va_list * args)
219 lisp_gpe_tunnel_t *lgt = va_arg (*args, lisp_gpe_tunnel_t *);
221 s = format (s, "tunnel %d\n", lgt - lisp_gpe_tunnel_pool);
222 s = format (s, " fib-index: %d, locks:%d \n",
223 lgt->key->fib_index, lgt->locks);
224 s = format (s, " lisp ver 0\n");
226 s = format (s, " locator-pair:\n");
227 s = format (s, " local: %U remote: %U\n",
228 format_ip_address, &lgt->key->lcl,
229 format_ip_address, &lgt->key->rmt);
230 s = format (s, " RLOC FIB entry: %d\n", lgt->fib_entry_index);
236 * CLI command to show LISP-GPE tunnels.
238 static clib_error_t *
239 show_lisp_gpe_tunnel_command_fn (vlib_main_t * vm,
240 unformat_input_t * input,
241 vlib_cli_command_t * cmd)
243 lisp_gpe_tunnel_t *lgt;
246 if (pool_elts (lisp_gpe_tunnel_pool) == 0)
247 vlib_cli_output (vm, "No lisp-gpe tunnels configured...");
249 if (unformat (input, "%d", &index))
251 lgt = lisp_gpe_tunnel_get_i (index);
252 vlib_cli_output (vm, "%U", format_lisp_gpe_tunnel, lgt);
257 pool_foreach (lgt, lisp_gpe_tunnel_pool,
259 vlib_cli_output (vm, "%U", format_lisp_gpe_tunnel, lgt);
268 VLIB_CLI_COMMAND (show_lisp_gpe_tunnel_command, static) =
270 .path = "show gpe tunnel",
271 .function = show_lisp_gpe_tunnel_command_fn,
275 static clib_error_t *
276 lisp_gpe_tunnel_module_init (vlib_main_t * vm)
278 lisp_gpe_tunnel_db = hash_create_mem (0,
279 sizeof (lisp_gpe_tunnel_key_t),
285 VLIB_INIT_FUNCTION (lisp_gpe_tunnel_module_init);
288 * fd.io coding-style-patch-verification: ON
291 * eval: (c-set-style "gnu")