teib: Add adj-fibs for peers/adjacencies on p2mp interface
[vpp.git] / src / vnet / teib / teib.c
1 /*
2  * teib.h: next-hop resolution
3  *
4  * Copyright (c) 2016 Cisco and/or its affiliates.
5  * 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>
22 #include <vnet/ip/ip6_ll_table.h>
23
24 typedef struct teib_key_t_
25 {
26   ip46_address_t tk_peer;
27   u32 tk_sw_if_index;
28   fib_protocol_t tk_proto;
29 } __clib_packed teib_key_t;
30
31 struct teib_entry_t_
32 {
33   teib_key_t *te_key;
34   fib_prefix_t te_nh;
35   u32 te_fib_index;
36 };
37
38 static uword *teib_db[FIB_PROTOCOL_IP_MAX];
39 static teib_entry_t *teib_pool;
40 static teib_vft_t *teib_vfts;
41 static vlib_log_class_t teib_logger;
42
43 #define TEIB_NOTIFY(_te, _fn) {                  \
44   teib_vft_t *_vft;                              \
45   vec_foreach(_vft, teib_vfts) {                 \
46     if (_vft->_fn) {                             \
47       _vft->_fn(_te);                            \
48     }                                            \
49   }                                              \
50 }
51
52 #define TEIB_DBG(...)                           \
53     vlib_log_debug (teib_logger, __VA_ARGS__);
54
55 #define TEIB_INFO(...)                          \
56     vlib_log_notice (teib_logger, __VA_ARGS__);
57
58 #define TEIB_TE_DBG(_te, _fmt, _args...)                      \
59   vlib_log_debug (teib_logger, "[%U]: " _fmt, format_teib_entry, _te - teib_pool, ##_args)
60 #define TEIB_TE_INFO(_te, _fmt, _args...)                      \
61   vlib_log_notice (teib_logger, "[%U]: " _fmt, format_teib_entry, _te - teib_pool, ##_args)
62
63 u32
64 teib_entry_get_sw_if_index (const teib_entry_t * te)
65 {
66   return (te->te_key->tk_sw_if_index);
67 }
68
69 fib_protocol_t
70 teib_entry_get_proto (const teib_entry_t * te)
71 {
72   return (te->te_key->tk_proto);
73 }
74
75 u32
76 teib_entry_get_fib_index (const teib_entry_t * te)
77 {
78   return (te->te_fib_index);
79 }
80
81 const ip46_address_t *
82 teib_entry_get_peer (const teib_entry_t * te)
83 {
84   return (&te->te_key->tk_peer);
85 }
86
87 const fib_prefix_t *
88 teib_entry_get_nh (const teib_entry_t * te)
89 {
90   return (&te->te_nh);
91 }
92
93 void
94 teib_entry_adj_stack (const teib_entry_t * te, adj_index_t ai)
95 {
96   adj_midchain_delegate_stack (ai, te->te_fib_index, &te->te_nh);
97 }
98
99 teib_entry_t *
100 teib_entry_get (index_t tei)
101 {
102   return pool_elt_at_index (teib_pool, tei);
103 }
104
105 teib_entry_t *
106 teib_entry_find (u32 sw_if_index,
107                  fib_protocol_t fproto, const ip46_address_t * peer)
108 {
109   teib_key_t nk = {
110     .tk_peer = *peer,
111     .tk_proto = fproto,
112     .tk_sw_if_index = sw_if_index,
113   };
114   uword *p;
115
116   p = hash_get_mem (teib_db[fproto], &nk);
117
118   if (NULL != p)
119     return teib_entry_get (p[0]);
120
121   return (NULL);
122 }
123
124 static void
125 teib_adj_fib_add (fib_protocol_t fproto,
126                   const ip46_address_t * ip, u32 sw_if_index, u32 fib_index)
127 {
128   if (FIB_PROTOCOL_IP6 == fproto &&
129       ip6_address_is_link_local_unicast (&ip->ip6))
130     {
131       ip6_ll_prefix_t pfx = {
132         .ilp_addr = ip->ip6,
133         .ilp_sw_if_index = sw_if_index,
134       };
135       ip6_ll_table_entry_update (&pfx, FIB_ROUTE_PATH_FLAG_NONE);
136     }
137   else
138     {
139       fib_prefix_t pfx = {
140         .fp_len = (FIB_PROTOCOL_IP4 == fproto ? 32 : 128),
141         .fp_proto = fproto,
142         .fp_addr = *ip,
143       };
144       fib_table_entry_path_add (fib_index, &pfx, FIB_SOURCE_ADJ,
145                                 FIB_ENTRY_FLAG_ATTACHED,
146                                 fib_proto_to_dpo (pfx.fp_proto),
147                                 &pfx.fp_addr,
148                                 sw_if_index,
149                                 ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
150
151
152       if (1 == hash_elts (teib_db[pfx.fp_proto]))
153         fib_table_lock (fib_index, pfx.fp_proto, FIB_SOURCE_ADJ);
154     }
155 }
156
157 static void
158 teib_adj_fib_remove (fib_protocol_t fproto,
159                      ip46_address_t * ip, u32 sw_if_index, u32 fib_index)
160 {
161   if (FIB_PROTOCOL_IP6 == fproto &&
162       ip6_address_is_link_local_unicast (&ip->ip6))
163     {
164       ip6_ll_prefix_t pfx = {
165         .ilp_addr = ip->ip6,
166         .ilp_sw_if_index = sw_if_index,
167       };
168       ip6_ll_table_entry_delete (&pfx);
169     }
170   else
171     {
172       fib_prefix_t pfx = {
173         .fp_len = (FIB_PROTOCOL_IP4 == fproto ? 32 : 128),
174         .fp_proto = fproto,
175         .fp_addr = *ip,
176       };
177
178       fib_table_entry_path_remove (fib_index, &pfx, FIB_SOURCE_ADJ,
179                                    fib_proto_to_dpo (pfx.fp_proto),
180                                    &pfx.fp_addr,
181                                    sw_if_index,
182                                    ~0, 1, FIB_ROUTE_PATH_FLAG_NONE);
183
184       if (0 == hash_elts (teib_db[pfx.fp_proto]))
185         fib_table_unlock (fib_index, pfx.fp_proto, FIB_SOURCE_ADJ);
186     }
187 }
188
189 int
190 teib_entry_add (u32 sw_if_index,
191                 fib_protocol_t fproto,
192                 const ip46_address_t * peer,
193                 u32 nh_table_id, const ip46_address_t * nh)
194 {
195   fib_protocol_t nh_proto;
196   teib_entry_t *te;
197   u32 fib_index;
198   index_t tei;
199
200   nh_proto = (ip46_address_is_ip4 (nh) ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6);
201
202   fib_index = fib_table_find (nh_proto, nh_table_id);
203
204   if (~0 == fib_index)
205     {
206       return (VNET_API_ERROR_NO_SUCH_FIB);
207     }
208
209   te = teib_entry_find (sw_if_index, fproto, peer);
210
211   if (NULL == te)
212     {
213       teib_key_t nk = {
214         .tk_peer = *peer,
215         .tk_proto = fproto,
216         .tk_sw_if_index = sw_if_index,
217       };
218       teib_entry_t *te;
219       u32 fib_index;
220
221       fib_index = fib_table_get_index_for_sw_if_index (fproto, sw_if_index);
222
223       pool_get_zero (teib_pool, te);
224
225       tei = te - teib_pool;
226       te->te_key = clib_mem_alloc (sizeof (*te->te_key));
227       clib_memcpy (te->te_key, &nk, sizeof (*te->te_key));
228
229       ip46_address_copy (&te->te_nh.fp_addr, nh);
230       te->te_nh.fp_proto = fproto;
231       te->te_nh.fp_len = (te->te_nh.fp_proto == FIB_PROTOCOL_IP4 ? 32 : 128);
232       te->te_fib_index = fib_index;
233
234       hash_set_mem (teib_db[fproto], te->te_key, tei);
235
236       /* we how have a /32 in the overlay, add an adj-fib */
237       teib_adj_fib_add (te->te_key->tk_proto,
238                         &te->te_key->tk_peer, sw_if_index, fib_index);
239
240       TEIB_NOTIFY (te, nv_added);
241       TEIB_TE_INFO (te, "created");
242     }
243   else
244     {
245       TEIB_TE_INFO (te, "exists");
246       return (VNET_API_ERROR_ENTRY_ALREADY_EXISTS);
247     }
248   return 0;
249 }
250
251 int
252 teib_entry_del (u32 sw_if_index,
253                 fib_protocol_t fproto, const ip46_address_t * peer)
254 {
255   teib_entry_t *te;
256
257   te = teib_entry_find (sw_if_index, fproto, peer);
258
259   if (te != NULL)
260     {
261       TEIB_TE_INFO (te, "removed");
262
263       u32 fib_index;
264
265       fib_index = fib_table_get_index_for_sw_if_index (fproto, sw_if_index);
266
267       teib_adj_fib_remove (te->te_key->tk_proto,
268                            &te->te_key->tk_peer, sw_if_index, fib_index);
269
270       hash_unset_mem (teib_db[fproto], te->te_key);
271
272       TEIB_NOTIFY (te, nv_deleted);
273
274       clib_mem_free (te->te_key);
275       pool_put (teib_pool, te);
276     }
277   else
278     {
279       TEIB_INFO ("no such entry: %U, %U, %U",
280                  format_vnet_sw_if_index_name,
281                  vnet_get_main (), sw_if_index,
282                  format_fib_protocol, fproto,
283                  format_ip46_address, peer, IP46_TYPE_ANY);
284       return (VNET_API_ERROR_NO_SUCH_ENTRY);
285     }
286   return 0;
287 }
288
289 u8 *
290 format_teib_entry (u8 * s, va_list * args)
291 {
292   index_t tei = va_arg (*args, index_t);
293   vnet_main_t *vnm = vnet_get_main ();
294   teib_entry_t *te;
295
296   te = teib_entry_get (tei);
297
298   s = format (s, "[%d] ", tei);
299   s = format (s, "%U:", format_vnet_sw_if_index_name,
300               vnm, te->te_key->tk_sw_if_index);
301   s = format (s, " %U:", format_fib_protocol, te->te_key->tk_proto);
302   s = format (s, "%U", format_ip46_address,
303               &te->te_key->tk_peer, IP46_TYPE_ANY);
304   s = format (s, " via [%d]:%U",
305               fib_table_get_table_id (te->te_fib_index, te->te_nh.fp_proto),
306               format_fib_prefix, &te->te_nh);
307
308   return (s);
309 }
310
311 void
312 teib_walk (teib_walk_cb_t fn, void *ctx)
313 {
314   index_t tei;
315
316   /* *INDENT-OFF* */
317   pool_foreach_index(tei, teib_pool,
318   ({
319     fn(tei, ctx);
320   }));
321   /* *INDENT-ON* */
322 }
323
324 void
325 teib_walk_itf (u32 sw_if_index, teib_walk_cb_t fn, void *ctx)
326 {
327   index_t tei;
328
329   /* *INDENT-OFF* */
330   pool_foreach_index(tei, teib_pool,
331   ({
332     if (sw_if_index == teib_entry_get_sw_if_index(teib_entry_get(tei)))
333       fn(tei, ctx);
334   }));
335   /* *INDENT-ON* */
336 }
337
338 static void
339 teib_walk_itf_proto (u32 sw_if_index,
340                      fib_protocol_t fproto, teib_walk_cb_t fn, void *ctx)
341 {
342   index_t tei;
343
344   /* *INDENT-OFF* */
345   pool_foreach_index(tei, teib_pool,
346   ({
347     if (sw_if_index == teib_entry_get_sw_if_index(teib_entry_get(tei)) &&
348         fproto == teib_entry_get_proto(teib_entry_get(tei)))
349       fn(tei, ctx);
350   }));
351   /* *INDENT-ON* */
352 }
353
354 typedef struct teib_table_bind_ctx_t_
355 {
356   u32 new_fib_index;
357   u32 old_fib_index;
358 } teib_table_bind_ctx_t;
359
360 static walk_rc_t
361 teib_walk_table_bind (index_t tei, void *arg)
362 {
363   teib_table_bind_ctx_t *ctx = arg;
364   teib_entry_t *te;
365
366   te = teib_entry_get (tei);
367
368   TEIB_TE_INFO (te, "bind: %d -> %d", ctx->old_fib_index, ctx->new_fib_index);
369
370   teib_adj_fib_remove (te->te_key->tk_proto,
371                        &te->te_key->tk_peer,
372                        te->te_key->tk_sw_if_index, ctx->old_fib_index);
373   teib_adj_fib_add (te->te_key->tk_proto,
374                     &te->te_key->tk_peer,
375                     te->te_key->tk_sw_if_index, ctx->new_fib_index);
376
377   return (WALK_CONTINUE);
378 }
379
380 static void
381 teib_table_bind_v4 (ip4_main_t * im,
382                     uword opaque,
383                     u32 sw_if_index, u32 new_fib_index, u32 old_fib_index)
384 {
385   teib_table_bind_ctx_t ctx = {
386     .old_fib_index = old_fib_index,
387     .new_fib_index = new_fib_index,
388   };
389
390   teib_walk_itf_proto (sw_if_index,
391                        FIB_PROTOCOL_IP4, teib_walk_table_bind, &ctx);
392 }
393
394 static void
395 teib_table_bind_v6 (ip6_main_t * im,
396                     uword opaque,
397                     u32 sw_if_index, u32 new_fib_index, u32 old_fib_index)
398 {
399   teib_table_bind_ctx_t ctx = {
400     .old_fib_index = old_fib_index,
401     .new_fib_index = new_fib_index,
402   };
403
404   teib_walk_itf_proto (sw_if_index,
405                        FIB_PROTOCOL_IP6, teib_walk_table_bind, &ctx);
406 }
407
408 void
409 teib_register (const teib_vft_t * vft)
410 {
411   vec_add1 (teib_vfts, *vft);
412 }
413
414 static clib_error_t *
415 teib_init (vlib_main_t * vm)
416 {
417   fib_protocol_t fproto;
418
419   FOR_EACH_FIB_IP_PROTOCOL (fproto)
420     teib_db[fproto] = hash_create_mem (0, sizeof (teib_key_t), sizeof (u32));
421
422   ip4_table_bind_callback_t cb4 = {
423     .function = teib_table_bind_v4,
424   };
425   vec_add1 (ip4_main.table_bind_callbacks, cb4);
426
427   ip6_table_bind_callback_t cb6 = {
428     .function = teib_table_bind_v6,
429   };
430   vec_add1 (ip6_main.table_bind_callbacks, cb6);
431
432   teib_logger = vlib_log_register_class ("teib", "teib");
433
434   return (NULL);
435 }
436
437 VLIB_INIT_FUNCTION (teib_init);
438
439 /*
440  * fd.io coding-style-patch-verification: ON
441  *
442  * Local Variables:
443  * eval: (c-set-style "gnu")
444  * End:
445  */