blob: eb554b396583091bf1dcf8aa74e24e7cbc5f94fc [file] [log] [blame]
Neale Ranns03ce4622020-02-03 10:55:09 +00001/*
Neale Rannse6b83052020-09-17 12:56:47 +00002 * teib.h: Tunnel Endpoint Information Base
Neale Ranns03ce4622020-02-03 10:55:09 +00003 *
Neale Rannse6b83052020-09-17 12:56:47 +00004 * Copyright (c) 2020 Cisco and/or its affiliates.
Neale Ranns03ce4622020-02-03 10:55:09 +00005 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
19#include <vnet/teib/teib.h>
20#include <vnet/fib/fib_table.h>
21#include <vnet/adj/adj_midchain.h>
Neale Ranns256b67b2020-09-02 14:46:53 +000022#include <vnet/ip/ip6_ll_table.h>
Neale Ranns03ce4622020-02-03 10:55:09 +000023
24typedef struct teib_key_t_
25{
Neale Rannse6b83052020-09-17 12:56:47 +000026 ip_address_t tk_peer;
27 u8 __pad[3];
Neale Ranns03ce4622020-02-03 10:55:09 +000028 u32 tk_sw_if_index;
Neale Ranns256b67b2020-09-02 14:46:53 +000029} __clib_packed teib_key_t;
Neale Ranns03ce4622020-02-03 10:55:09 +000030
Neale Rannse6b83052020-09-17 12:56:47 +000031STATIC_ASSERT_SIZEOF (teib_key_t, 24);
32
Neale Ranns03ce4622020-02-03 10:55:09 +000033struct teib_entry_t_
34{
35 teib_key_t *te_key;
36 fib_prefix_t te_nh;
Stanislav Zaikin51541242023-02-21 09:26:41 +010037 u32 te_nh_fib_index;
Neale Ranns03ce4622020-02-03 10:55:09 +000038};
39
Neale Rannse6b83052020-09-17 12:56:47 +000040typedef struct teib_db_t_
41{
42 u32 td_n_entries[N_AF];
43 uword *td_db;
44} teib_db_t;
45
46static teib_db_t teib_db;
Neale Ranns03ce4622020-02-03 10:55:09 +000047static teib_entry_t *teib_pool;
48static teib_vft_t *teib_vfts;
Neale Ranns256b67b2020-09-02 14:46:53 +000049static vlib_log_class_t teib_logger;
Neale Ranns03ce4622020-02-03 10:55:09 +000050
51#define TEIB_NOTIFY(_te, _fn) { \
52 teib_vft_t *_vft; \
53 vec_foreach(_vft, teib_vfts) { \
54 if (_vft->_fn) { \
55 _vft->_fn(_te); \
56 } \
57 } \
58}
59
Neale Ranns256b67b2020-09-02 14:46:53 +000060#define TEIB_DBG(...) \
61 vlib_log_debug (teib_logger, __VA_ARGS__);
62
63#define TEIB_INFO(...) \
64 vlib_log_notice (teib_logger, __VA_ARGS__);
65
66#define TEIB_TE_DBG(_te, _fmt, _args...) \
67 vlib_log_debug (teib_logger, "[%U]: " _fmt, format_teib_entry, _te - teib_pool, ##_args)
68#define TEIB_TE_INFO(_te, _fmt, _args...) \
69 vlib_log_notice (teib_logger, "[%U]: " _fmt, format_teib_entry, _te - teib_pool, ##_args)
70
Neale Ranns03ce4622020-02-03 10:55:09 +000071u32
72teib_entry_get_sw_if_index (const teib_entry_t * te)
73{
74 return (te->te_key->tk_sw_if_index);
75}
76
Neale Rannse6b83052020-09-17 12:56:47 +000077static ip_address_family_t
78teib_entry_get_af (const teib_entry_t * te)
Neale Ranns256b67b2020-09-02 14:46:53 +000079{
Neale Rannse6b83052020-09-17 12:56:47 +000080 return (ip_addr_version (&te->te_key->tk_peer));
Neale Ranns256b67b2020-09-02 14:46:53 +000081}
82
Neale Ranns03ce4622020-02-03 10:55:09 +000083u32
84teib_entry_get_fib_index (const teib_entry_t * te)
85{
Stanislav Zaikin51541242023-02-21 09:26:41 +010086 return (te->te_nh_fib_index);
Neale Ranns03ce4622020-02-03 10:55:09 +000087}
88
Neale Rannse6b83052020-09-17 12:56:47 +000089const ip_address_t *
Neale Ranns03ce4622020-02-03 10:55:09 +000090teib_entry_get_peer (const teib_entry_t * te)
91{
92 return (&te->te_key->tk_peer);
93}
94
95const fib_prefix_t *
96teib_entry_get_nh (const teib_entry_t * te)
97{
98 return (&te->te_nh);
99}
100
101void
102teib_entry_adj_stack (const teib_entry_t * te, adj_index_t ai)
103{
Stanislav Zaikin51541242023-02-21 09:26:41 +0100104 adj_midchain_delegate_stack (ai, te->te_nh_fib_index, &te->te_nh);
Neale Ranns03ce4622020-02-03 10:55:09 +0000105}
106
107teib_entry_t *
108teib_entry_get (index_t tei)
109{
110 return pool_elt_at_index (teib_pool, tei);
111}
112
113teib_entry_t *
Neale Rannse6b83052020-09-17 12:56:47 +0000114teib_entry_find (u32 sw_if_index, const ip_address_t * peer)
Neale Ranns03ce4622020-02-03 10:55:09 +0000115{
116 teib_key_t nk = {
117 .tk_peer = *peer,
118 .tk_sw_if_index = sw_if_index,
119 };
120 uword *p;
121
Neale Rannse6b83052020-09-17 12:56:47 +0000122 p = hash_get_mem (teib_db.td_db, &nk);
Neale Ranns03ce4622020-02-03 10:55:09 +0000123
124 if (NULL != p)
125 return teib_entry_get (p[0]);
126
127 return (NULL);
128}
129
Neale Rannse6b83052020-09-17 12:56:47 +0000130teib_entry_t *
131teib_entry_find_46 (u32 sw_if_index,
132 fib_protocol_t fproto, const ip46_address_t * peer)
Neale Ranns256b67b2020-09-02 14:46:53 +0000133{
Neale Rannse6b83052020-09-17 12:56:47 +0000134 ip_address_t ip;
135
136 ip_address_from_46 (peer, fproto, &ip);
137
138 return (teib_entry_find (sw_if_index, &ip));
139}
140
141static void
Stanislav Zaikin51541242023-02-21 09:26:41 +0100142teib_adj_fib_add (const ip_address_t *ip, u32 sw_if_index, u32 peer_fib_index)
Neale Rannse6b83052020-09-17 12:56:47 +0000143{
144 if (AF_IP6 == ip_addr_version (ip) &&
145 ip6_address_is_link_local_unicast (&ip_addr_v6 (ip)))
Neale Ranns256b67b2020-09-02 14:46:53 +0000146 {
147 ip6_ll_prefix_t pfx = {
Neale Rannse6b83052020-09-17 12:56:47 +0000148 .ilp_addr = ip_addr_v6 (ip),
Neale Ranns256b67b2020-09-02 14:46:53 +0000149 .ilp_sw_if_index = sw_if_index,
150 };
151 ip6_ll_table_entry_update (&pfx, FIB_ROUTE_PATH_FLAG_NONE);
152 }
153 else
154 {
Neale Rannse6b83052020-09-17 12:56:47 +0000155 fib_prefix_t pfx;
156
157 ip_address_to_fib_prefix (ip, &pfx);
Stanislav Zaikin51541242023-02-21 09:26:41 +0100158 fib_table_entry_path_add (
159 peer_fib_index, &pfx, FIB_SOURCE_ADJ, FIB_ENTRY_FLAG_ATTACHED,
160 fib_proto_to_dpo (pfx.fp_proto), &pfx.fp_addr, sw_if_index, ~0, 1,
161 NULL, FIB_ROUTE_PATH_FLAG_NONE);
Neale Ranns256b67b2020-09-02 14:46:53 +0000162
Neale Rannse6b83052020-09-17 12:56:47 +0000163 if (0 == teib_db.td_n_entries[ip_addr_version (ip)]++)
Stanislav Zaikin51541242023-02-21 09:26:41 +0100164 fib_table_lock (peer_fib_index, pfx.fp_proto, FIB_SOURCE_ADJ);
Neale Ranns256b67b2020-09-02 14:46:53 +0000165 }
166}
167
168static void
Stanislav Zaikin51541242023-02-21 09:26:41 +0100169teib_adj_fib_remove (ip_address_t *ip, u32 sw_if_index, u32 peer_fib_index)
Neale Ranns256b67b2020-09-02 14:46:53 +0000170{
Neale Rannse6b83052020-09-17 12:56:47 +0000171 if (AF_IP6 == ip_addr_version (ip) &&
172 ip6_address_is_link_local_unicast (&ip_addr_v6 (ip)))
Neale Ranns256b67b2020-09-02 14:46:53 +0000173 {
174 ip6_ll_prefix_t pfx = {
Neale Rannse6b83052020-09-17 12:56:47 +0000175 .ilp_addr = ip_addr_v6 (ip),
Neale Ranns256b67b2020-09-02 14:46:53 +0000176 .ilp_sw_if_index = sw_if_index,
177 };
178 ip6_ll_table_entry_delete (&pfx);
179 }
180 else
181 {
Neale Rannse6b83052020-09-17 12:56:47 +0000182 fib_prefix_t pfx;
Neale Ranns256b67b2020-09-02 14:46:53 +0000183
Neale Rannse6b83052020-09-17 12:56:47 +0000184 ip_address_to_fib_prefix (ip, &pfx);
Stanislav Zaikin51541242023-02-21 09:26:41 +0100185 fib_table_entry_path_remove (
186 peer_fib_index, &pfx, FIB_SOURCE_ADJ, fib_proto_to_dpo (pfx.fp_proto),
187 &pfx.fp_addr, sw_if_index, ~0, 1, FIB_ROUTE_PATH_FLAG_NONE);
Neale Ranns256b67b2020-09-02 14:46:53 +0000188
Neale Rannse6b83052020-09-17 12:56:47 +0000189 if (0 == --teib_db.td_n_entries[ip_addr_version (ip)])
Stanislav Zaikin51541242023-02-21 09:26:41 +0100190 fib_table_unlock (peer_fib_index, pfx.fp_proto, FIB_SOURCE_ADJ);
Neale Ranns256b67b2020-09-02 14:46:53 +0000191 }
192}
193
Neale Ranns03ce4622020-02-03 10:55:09 +0000194int
195teib_entry_add (u32 sw_if_index,
Neale Rannse6b83052020-09-17 12:56:47 +0000196 const ip_address_t * peer,
197 u32 nh_table_id, const ip_address_t * nh)
Neale Ranns03ce4622020-02-03 10:55:09 +0000198{
Neale Ranns256b67b2020-09-02 14:46:53 +0000199 fib_protocol_t nh_proto;
Neale Ranns03ce4622020-02-03 10:55:09 +0000200 teib_entry_t *te;
Stanislav Zaikin51541242023-02-21 09:26:41 +0100201 u32 nh_fib_index, peer_fib_index;
Neale Ranns03ce4622020-02-03 10:55:09 +0000202 index_t tei;
203
Neale Rannse6b83052020-09-17 12:56:47 +0000204 nh_proto = (AF_IP4 == ip_addr_version (nh) ?
205 FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6);
Neale Ranns03ce4622020-02-03 10:55:09 +0000206
Stanislav Zaikin51541242023-02-21 09:26:41 +0100207 peer_fib_index = fib_table_get_index_for_sw_if_index (
208 ip_address_family_to_fib_proto (peer->version), sw_if_index);
209 nh_fib_index = fib_table_find (nh_proto, nh_table_id);
Neale Ranns03ce4622020-02-03 10:55:09 +0000210
Stanislav Zaikin51541242023-02-21 09:26:41 +0100211 if (~0 == nh_fib_index)
Neale Ranns03ce4622020-02-03 10:55:09 +0000212 {
213 return (VNET_API_ERROR_NO_SUCH_FIB);
214 }
215
Neale Rannse6b83052020-09-17 12:56:47 +0000216 te = teib_entry_find (sw_if_index, peer);
Neale Ranns03ce4622020-02-03 10:55:09 +0000217
218 if (NULL == te)
219 {
220 teib_key_t nk = {
221 .tk_peer = *peer,
222 .tk_sw_if_index = sw_if_index,
223 };
224 teib_entry_t *te;
225
226 pool_get_zero (teib_pool, te);
227
228 tei = te - teib_pool;
229 te->te_key = clib_mem_alloc (sizeof (*te->te_key));
230 clib_memcpy (te->te_key, &nk, sizeof (*te->te_key));
231
Neale Rannse6b83052020-09-17 12:56:47 +0000232 ip_address_to_fib_prefix (nh, &te->te_nh);
Stanislav Zaikin51541242023-02-21 09:26:41 +0100233 te->te_nh_fib_index = nh_fib_index;
Neale Ranns03ce4622020-02-03 10:55:09 +0000234
Neale Rannse6b83052020-09-17 12:56:47 +0000235 hash_set_mem (teib_db.td_db, te->te_key, tei);
Neale Ranns256b67b2020-09-02 14:46:53 +0000236
237 /* we how have a /32 in the overlay, add an adj-fib */
Stanislav Zaikin51541242023-02-21 09:26:41 +0100238 teib_adj_fib_add (&te->te_key->tk_peer, sw_if_index, peer_fib_index);
Neale Ranns03ce4622020-02-03 10:55:09 +0000239
240 TEIB_NOTIFY (te, nv_added);
Neale Ranns256b67b2020-09-02 14:46:53 +0000241 TEIB_TE_INFO (te, "created");
Neale Ranns03ce4622020-02-03 10:55:09 +0000242 }
243 else
Neale Ranns256b67b2020-09-02 14:46:53 +0000244 {
245 TEIB_TE_INFO (te, "exists");
246 return (VNET_API_ERROR_ENTRY_ALREADY_EXISTS);
247 }
Neale Ranns03ce4622020-02-03 10:55:09 +0000248 return 0;
249}
250
251int
Neale Rannse6b83052020-09-17 12:56:47 +0000252teib_entry_del (u32 sw_if_index, const ip_address_t * peer)
Neale Ranns03ce4622020-02-03 10:55:09 +0000253{
254 teib_entry_t *te;
255
Neale Rannse6b83052020-09-17 12:56:47 +0000256 te = teib_entry_find (sw_if_index, peer);
Neale Ranns03ce4622020-02-03 10:55:09 +0000257
258 if (te != NULL)
259 {
Neale Ranns256b67b2020-09-02 14:46:53 +0000260 TEIB_TE_INFO (te, "removed");
261
Stanislav Zaikin51541242023-02-21 09:26:41 +0100262 u32 peer_fib_index;
Neale Ranns256b67b2020-09-02 14:46:53 +0000263
Stanislav Zaikin51541242023-02-21 09:26:41 +0100264 peer_fib_index = fib_table_get_index_for_sw_if_index (
265 ip_address_family_to_fib_proto (peer->version), sw_if_index);
Neale Ranns256b67b2020-09-02 14:46:53 +0000266
Stanislav Zaikin51541242023-02-21 09:26:41 +0100267 teib_adj_fib_remove (&te->te_key->tk_peer, sw_if_index, peer_fib_index);
Neale Ranns256b67b2020-09-02 14:46:53 +0000268
Neale Rannse6b83052020-09-17 12:56:47 +0000269 hash_unset_mem (teib_db.td_db, te->te_key);
Neale Ranns03ce4622020-02-03 10:55:09 +0000270
271 TEIB_NOTIFY (te, nv_deleted);
272
273 clib_mem_free (te->te_key);
274 pool_put (teib_pool, te);
275 }
276 else
Neale Ranns256b67b2020-09-02 14:46:53 +0000277 {
Matthew Smithde989062022-09-08 21:03:10 +0000278 TEIB_INFO ("no such entry: %U, %U", format_vnet_sw_if_index_name,
Neale Rannse6b83052020-09-17 12:56:47 +0000279 vnet_get_main (), sw_if_index, format_ip_address, peer);
Neale Ranns256b67b2020-09-02 14:46:53 +0000280 return (VNET_API_ERROR_NO_SUCH_ENTRY);
281 }
Neale Ranns03ce4622020-02-03 10:55:09 +0000282 return 0;
283}
284
285u8 *
286format_teib_entry (u8 * s, va_list * args)
287{
288 index_t tei = va_arg (*args, index_t);
289 vnet_main_t *vnm = vnet_get_main ();
290 teib_entry_t *te;
291
292 te = teib_entry_get (tei);
293
294 s = format (s, "[%d] ", tei);
295 s = format (s, "%U:", format_vnet_sw_if_index_name,
296 vnm, te->te_key->tk_sw_if_index);
Neale Rannse6b83052020-09-17 12:56:47 +0000297 s = format (s, "%U", format_ip_address,
Neale Ranns03ce4622020-02-03 10:55:09 +0000298 &te->te_key->tk_peer, IP46_TYPE_ANY);
299 s = format (s, " via [%d]:%U",
Stanislav Zaikin51541242023-02-21 09:26:41 +0100300 fib_table_get_table_id (te->te_nh_fib_index, te->te_nh.fp_proto),
Neale Ranns03ce4622020-02-03 10:55:09 +0000301 format_fib_prefix, &te->te_nh);
302
303 return (s);
304}
305
306void
307teib_walk (teib_walk_cb_t fn, void *ctx)
308{
309 index_t tei;
310
311 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100312 pool_foreach_index (tei, teib_pool)
313 {
Neale Ranns03ce4622020-02-03 10:55:09 +0000314 fn(tei, ctx);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100315 }
Neale Ranns03ce4622020-02-03 10:55:09 +0000316 /* *INDENT-ON* */
317}
318
319void
320teib_walk_itf (u32 sw_if_index, teib_walk_cb_t fn, void *ctx)
321{
322 index_t tei;
323
324 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100325 pool_foreach_index (tei, teib_pool)
326 {
Neale Ranns03ce4622020-02-03 10:55:09 +0000327 if (sw_if_index == teib_entry_get_sw_if_index(teib_entry_get(tei)))
328 fn(tei, ctx);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100329 }
Neale Ranns03ce4622020-02-03 10:55:09 +0000330 /* *INDENT-ON* */
331}
332
Neale Ranns256b67b2020-09-02 14:46:53 +0000333static void
334teib_walk_itf_proto (u32 sw_if_index,
Neale Rannse6b83052020-09-17 12:56:47 +0000335 ip_address_family_t af, teib_walk_cb_t fn, void *ctx)
Neale Ranns256b67b2020-09-02 14:46:53 +0000336{
337 index_t tei;
338
339 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100340 pool_foreach_index (tei, teib_pool)
341 {
Neale Ranns256b67b2020-09-02 14:46:53 +0000342 if (sw_if_index == teib_entry_get_sw_if_index(teib_entry_get(tei)) &&
Neale Rannse6b83052020-09-17 12:56:47 +0000343 af == teib_entry_get_af(teib_entry_get(tei)))
Neale Ranns256b67b2020-09-02 14:46:53 +0000344 fn(tei, ctx);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100345 }
Neale Ranns256b67b2020-09-02 14:46:53 +0000346 /* *INDENT-ON* */
347}
348
349typedef struct teib_table_bind_ctx_t_
350{
Stanislav Zaikin51541242023-02-21 09:26:41 +0100351 u32 new_peer_fib_index;
352 u32 old_peer_fib_index;
Neale Ranns256b67b2020-09-02 14:46:53 +0000353} teib_table_bind_ctx_t;
354
355static walk_rc_t
356teib_walk_table_bind (index_t tei, void *arg)
357{
358 teib_table_bind_ctx_t *ctx = arg;
359 teib_entry_t *te;
360
361 te = teib_entry_get (tei);
362
Stanislav Zaikin51541242023-02-21 09:26:41 +0100363 TEIB_TE_INFO (te, "bind: %d -> %d", ctx->old_peer_fib_index,
364 ctx->new_peer_fib_index);
Neale Ranns256b67b2020-09-02 14:46:53 +0000365
Stanislav Zaikin51541242023-02-21 09:26:41 +0100366 teib_adj_fib_remove (&te->te_key->tk_peer, te->te_key->tk_sw_if_index,
367 ctx->old_peer_fib_index);
368 teib_adj_fib_add (&te->te_key->tk_peer, te->te_key->tk_sw_if_index,
369 ctx->new_peer_fib_index);
Neale Ranns256b67b2020-09-02 14:46:53 +0000370
371 return (WALK_CONTINUE);
372}
373
374static void
375teib_table_bind_v4 (ip4_main_t * im,
376 uword opaque,
377 u32 sw_if_index, u32 new_fib_index, u32 old_fib_index)
378{
379 teib_table_bind_ctx_t ctx = {
Stanislav Zaikin51541242023-02-21 09:26:41 +0100380 .old_peer_fib_index = old_fib_index,
381 .new_peer_fib_index = new_fib_index,
Neale Ranns256b67b2020-09-02 14:46:53 +0000382 };
383
Neale Rannse6b83052020-09-17 12:56:47 +0000384 teib_walk_itf_proto (sw_if_index, AF_IP4, teib_walk_table_bind, &ctx);
Neale Ranns256b67b2020-09-02 14:46:53 +0000385}
386
387static void
388teib_table_bind_v6 (ip6_main_t * im,
389 uword opaque,
390 u32 sw_if_index, u32 new_fib_index, u32 old_fib_index)
391{
392 teib_table_bind_ctx_t ctx = {
Stanislav Zaikin51541242023-02-21 09:26:41 +0100393 .old_peer_fib_index = old_fib_index,
394 .new_peer_fib_index = new_fib_index,
Neale Ranns256b67b2020-09-02 14:46:53 +0000395 };
396
Neale Rannse6b83052020-09-17 12:56:47 +0000397 teib_walk_itf_proto (sw_if_index, AF_IP6, teib_walk_table_bind, &ctx);
Neale Ranns256b67b2020-09-02 14:46:53 +0000398}
399
Neale Ranns03ce4622020-02-03 10:55:09 +0000400void
401teib_register (const teib_vft_t * vft)
402{
403 vec_add1 (teib_vfts, *vft);
404}
405
406static clib_error_t *
407teib_init (vlib_main_t * vm)
408{
Neale Rannse6b83052020-09-17 12:56:47 +0000409 teib_db.td_db = hash_create_mem (0, sizeof (teib_key_t), sizeof (u32));
Neale Ranns256b67b2020-09-02 14:46:53 +0000410
411 ip4_table_bind_callback_t cb4 = {
412 .function = teib_table_bind_v4,
413 };
414 vec_add1 (ip4_main.table_bind_callbacks, cb4);
415
416 ip6_table_bind_callback_t cb6 = {
417 .function = teib_table_bind_v6,
418 };
419 vec_add1 (ip6_main.table_bind_callbacks, cb6);
420
421 teib_logger = vlib_log_register_class ("teib", "teib");
Neale Ranns03ce4622020-02-03 10:55:09 +0000422
423 return (NULL);
424}
425
426VLIB_INIT_FUNCTION (teib_init);
427
428/*
429 * fd.io coding-style-patch-verification: ON
430 *
431 * Local Variables:
432 * eval: (c-set-style "gnu")
433 * End:
434 */