fib: Table Replace
[vpp.git] / src / vnet / mfib / ip4_mfib.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include <vnet/mfib/ip4_mfib.h>
17
18 #include <vnet/mfib/mfib_table.h>
19 #include <vnet/mfib/mfib_entry.h>
20
21 static const mfib_prefix_t ip4_specials[] = {
22     {
23         /* (*,*)/0 */
24         .fp_src_addr = {
25             .ip4.data_u32 = 0,
26         },
27         .fp_grp_addr = {
28             .ip4.data_u32 = 0,
29         },
30         .fp_len  = 0,
31         .fp_proto = FIB_PROTOCOL_IP4,
32     },
33 };
34
35 static u32
36 ip4_create_mfib_with_table_id (u32 table_id,
37                                mfib_source_t src)
38 {
39     mfib_table_t *mfib_table;
40
41     pool_get_aligned(ip4_main.mfibs, mfib_table, CLIB_CACHE_LINE_BYTES);
42     clib_memset(mfib_table, 0, sizeof(*mfib_table));
43
44     mfib_table->mft_proto = FIB_PROTOCOL_IP4;
45     mfib_table->mft_index =
46         mfib_table->v4.index =
47             (mfib_table - ip4_main.mfibs);
48
49     hash_set (ip4_main.mfib_index_by_table_id,
50               table_id,
51               mfib_table->mft_index);
52
53     mfib_table->mft_table_id =
54         mfib_table->v4.table_id =
55             table_id;
56
57     mfib_table_lock(mfib_table->mft_index, FIB_PROTOCOL_IP4, src);
58
59     /*
60      * add the special entries into the new FIB
61      */
62     int ii;
63
64     for (ii = 0; ii < ARRAY_LEN(ip4_specials); ii++)
65     {
66         mfib_prefix_t prefix = ip4_specials[ii];
67
68         prefix.fp_src_addr.ip4.data_u32 =
69             clib_host_to_net_u32(prefix.fp_src_addr.ip4.data_u32);
70         prefix.fp_grp_addr.ip4.data_u32 =
71             clib_host_to_net_u32(prefix.fp_grp_addr.ip4.data_u32);
72
73         mfib_table_entry_update(mfib_table->mft_index,
74                                 &prefix,
75                                 MFIB_SOURCE_DEFAULT_ROUTE,
76                                 MFIB_RPF_ID_NONE,
77                                 MFIB_ENTRY_FLAG_DROP);
78     }
79
80     return (mfib_table->mft_index);
81 }
82
83 void
84 ip4_mfib_table_destroy (ip4_mfib_t *mfib)
85 {
86     mfib_table_t *mfib_table = (mfib_table_t*)mfib;
87     int ii;
88
89     /*
90      * remove all the specials we added when the table was created.
91      */
92     for (ii = 0; ii < ARRAY_LEN(ip4_specials); ii++)
93     {
94         fib_node_index_t mfei;
95         mfib_prefix_t prefix = ip4_specials[ii];
96
97         prefix.fp_src_addr.ip4.data_u32 =
98             clib_host_to_net_u32(prefix.fp_src_addr.ip4.data_u32);
99         prefix.fp_grp_addr.ip4.data_u32 =
100             clib_host_to_net_u32(prefix.fp_grp_addr.ip4.data_u32);
101
102         mfei = mfib_table_lookup(mfib_table->mft_index, &prefix);
103         mfib_table_entry_delete_index(mfei, MFIB_SOURCE_DEFAULT_ROUTE);
104     }
105
106     /*
107      * validate no more routes.
108      */
109     ASSERT(0 == mfib_table->mft_total_route_counts);
110     ASSERT(~0 != mfib_table->mft_table_id);
111
112     hash_unset (ip4_main.mfib_index_by_table_id, mfib_table->mft_table_id);
113     pool_put(ip4_main.mfibs, mfib_table);
114 }
115
116 u32
117 ip4_mfib_table_find_or_create_and_lock (u32 table_id,
118                                         mfib_source_t src)
119 {
120     u32 index;
121
122     index = ip4_mfib_index_from_table_id(table_id);
123     if (~0 == index)
124         return ip4_create_mfib_with_table_id(table_id, src);
125     mfib_table_lock(index, FIB_PROTOCOL_IP4, src);
126
127     return (index);
128 }
129
130 u32
131 ip4_mfib_table_get_index_for_sw_if_index (u32 sw_if_index)
132 {
133     if (sw_if_index >= vec_len(ip4_main.mfib_index_by_sw_if_index))
134     {
135         /*
136          * This is the case for interfaces that are not yet mapped to
137          * a IP table
138          */
139         return (~0);
140     }
141     return (ip4_main.mfib_index_by_sw_if_index[sw_if_index]);
142 }
143
144 #define IPV4_MFIB_GRP_LEN(_len)\
145     (_len > 32 ? 32 : _len)
146
147 #define IP4_MFIB_MK_KEY(_grp, _src, _len, _key)                         \
148 {                                                                       \
149     _key  = ((u64)(_grp->data_u32 &                                     \
150                    ip4_main.fib_masks[IPV4_MFIB_GRP_LEN(_len)])) << 32; \
151     _key |= _src->data_u32;                                             \
152 }
153 #define IP4_MFIB_MK_GRP_KEY(_grp, _len, _key)                           \
154 {                                                                       \
155     _key  = ((u64)(_grp->data_u32 &                                     \
156                    ip4_main.fib_masks[IPV4_MFIB_GRP_LEN(_len)])) << 32; \
157 }
158
159 /*
160  * ip4_fib_table_lookup_exact_match
161  *
162  * Exact match prefix lookup
163  */
164 fib_node_index_t
165 ip4_mfib_table_lookup_exact_match (const ip4_mfib_t *mfib,
166                                    const ip4_address_t *grp,
167                                    const ip4_address_t *src,
168                                    u32 len)
169 {
170     uword * hash, * result;
171     u64 key;
172
173     hash = mfib->fib_entry_by_dst_address[len];
174     IP4_MFIB_MK_KEY(grp, src, len, key);
175
176     result = hash_get(hash, key);
177
178     if (NULL != result) {
179         return (result[0]);
180     }
181     return (FIB_NODE_INDEX_INVALID);
182 }
183
184 /*
185  * ip4_fib_table_lookup
186  *
187  * Longest prefix match
188  */
189 fib_node_index_t
190 ip4_mfib_table_lookup (const ip4_mfib_t *mfib,
191                        const ip4_address_t *src,
192                        const ip4_address_t *grp,
193                        u32 len)
194 {
195     uword * hash, * result;
196     i32 mask_len;
197     u64 key;
198
199     mask_len = len;
200
201     if (PREDICT_TRUE(64 == mask_len))
202     {
203         hash = mfib->fib_entry_by_dst_address[mask_len];
204         IP4_MFIB_MK_KEY(grp, src, mask_len, key);
205
206         result = hash_get (hash, key);
207
208         if (NULL != result) {
209             return (result[0]);
210         }
211     }
212
213     for (mask_len = (len == 64 ? 32 : len); mask_len >= 0; mask_len--)
214     {
215         hash = mfib->fib_entry_by_dst_address[mask_len];
216         IP4_MFIB_MK_GRP_KEY(grp, mask_len, key);
217
218         result = hash_get (hash, key);
219
220         if (NULL != result) {
221             return (result[0]);
222         }
223     }
224     return (FIB_NODE_INDEX_INVALID);
225 }
226
227 fib_node_index_t
228 ip4_mfib_table_get_less_specific (const ip4_mfib_t *mfib,
229                                   const ip4_address_t *src,
230                                   const ip4_address_t *grp,
231                                   u32 len)
232 {
233     u32 mask_len;
234
235     /*
236      * in the absence of a tree structure for the table that allows for an O(1)
237      * parent get, a cheeky way to find the cover is to LPM for the prefix with
238      * mask-1.
239      * there should always be a cover, though it may be the default route. the
240      * default route's cover is the default route.
241      */
242     if (len == 64)
243     {
244         /* go from (S,G) to (*,G*) */
245         mask_len = 32;
246     }
247     else if (len != 0)
248     {
249         mask_len = len - 1;
250     }
251     else
252     {
253         mask_len = len;
254     }
255
256     return (ip4_mfib_table_lookup(mfib, src, grp, mask_len));
257 }
258
259 void
260 ip4_mfib_table_entry_insert (ip4_mfib_t *mfib,
261                              const ip4_address_t *grp,
262                              const ip4_address_t *src,
263                              u32 len,
264                              fib_node_index_t fib_entry_index)
265 {
266     uword * hash, * result;
267     u64 key;
268
269     IP4_MFIB_MK_KEY(grp, src, len, key);
270     hash = mfib->fib_entry_by_dst_address[len];
271     result = hash_get (hash, key);
272
273     if (NULL == result) {
274         /*
275          * adding a new entry
276          */
277         if (NULL == hash) {
278             hash = hash_create (32 /* elts */, sizeof (uword));
279             hash_set_flags (hash, HASH_FLAG_NO_AUTO_SHRINK);
280         }
281         hash = hash_set(hash, key, fib_entry_index);
282         mfib->fib_entry_by_dst_address[len] = hash;
283     }
284     else
285     {
286         ASSERT(0);
287     }
288 }
289
290 void
291 ip4_mfib_table_entry_remove (ip4_mfib_t *mfib,
292                              const ip4_address_t *grp,
293                              const ip4_address_t *src,
294                              u32 len)
295 {
296     uword * hash, * result;
297     u64 key;
298
299     IP4_MFIB_MK_KEY(grp, src, len, key);
300     hash = mfib->fib_entry_by_dst_address[len];
301     result = hash_get (hash, key);
302
303     if (NULL == result)
304     {
305         /*
306          * removing a non-existent entry. i'll allow it.
307          */
308     }
309     else
310     {
311         hash_unset(hash, key);
312     }
313
314     mfib->fib_entry_by_dst_address[len] = hash;
315 }
316
317 void
318 ip4_mfib_table_walk (ip4_mfib_t *mfib,
319                      mfib_table_walk_fn_t fn,
320                      void *ctx)
321 {
322     int i;
323
324     for (i = 0; i < ARRAY_LEN (mfib->fib_entry_by_dst_address); i++)
325     {
326         uword * hash = mfib->fib_entry_by_dst_address[i];
327
328         if (NULL != hash)
329         {
330             hash_pair_t * p;
331
332             hash_foreach_pair (p, hash,
333             ({
334                 fn(p->value[0], ctx);
335             }));
336         }
337     }
338 }
339
340 u8 *
341 format_ip4_mfib_table_memory (u8 * s, va_list * args)
342 {
343     mfib_table_t *mfib_table;
344     u64 total_memory;
345
346     total_memory = 0;
347
348     pool_foreach (mfib_table, ip4_main.mfibs,
349     ({
350         ip4_mfib_t *mfib = &mfib_table->v4;
351         uword mfib_size;
352         int i;
353
354         mfib_size = 0;
355
356         for (i = 0; i < ARRAY_LEN (mfib->fib_entry_by_dst_address); i++)
357         {
358             uword * hash = mfib->fib_entry_by_dst_address[i];
359
360             if (NULL != hash)
361             {
362                 mfib_size += hash_bytes(hash);
363             }
364         }
365
366         total_memory += mfib_size;
367     }));
368
369     s = format(s, "%=30s %=6d %=12ld\n",
370                "IPv4 multicast",
371                pool_elts(ip4_main.mfibs), total_memory);
372
373     return (s);
374 }
375
376 static void
377 ip4_mfib_table_show_all (ip4_mfib_t *mfib,
378                          vlib_main_t * vm)
379 {
380     fib_node_index_t *mfib_entry_indicies;
381     fib_node_index_t *mfib_entry_index;
382     int i;
383
384     mfib_entry_indicies = NULL;
385
386     for (i = 0; i < ARRAY_LEN (mfib->fib_entry_by_dst_address); i++)
387     {
388         uword * hash = mfib->fib_entry_by_dst_address[i];
389
390         if (NULL != hash)
391         {
392             hash_pair_t * p;
393
394             hash_foreach_pair (p, hash,
395             ({
396                 vec_add1(mfib_entry_indicies, p->value[0]);
397             }));
398         }
399     }
400
401     vec_sort_with_function(mfib_entry_indicies, mfib_entry_cmp_for_sort);
402
403     vec_foreach(mfib_entry_index, mfib_entry_indicies)
404     {
405         vlib_cli_output(vm, "%U",
406                         format_mfib_entry,
407                         *mfib_entry_index,
408                         MFIB_ENTRY_FORMAT_BRIEF);
409     }
410
411     vec_free(mfib_entry_indicies);
412 }
413
414 static void
415 ip4_mfib_table_show_one (ip4_mfib_t *mfib,
416                          vlib_main_t * vm,
417                          ip4_address_t *src,
418                          ip4_address_t *grp,
419                          u32 mask_len)
420 {
421     vlib_cli_output(vm, "%U",
422                     format_mfib_entry,
423                     ip4_mfib_table_lookup(mfib, src, grp, mask_len),
424                     MFIB_ENTRY_FORMAT_DETAIL);
425 }
426
427 static clib_error_t *
428 ip4_show_mfib (vlib_main_t * vm,
429                unformat_input_t * input,
430                vlib_cli_command_t * cmd)
431 {
432     ip4_main_t * im4 = &ip4_main;
433     mfib_table_t *mfib_table;
434     int verbose, matching, memory;
435     ip4_address_t grp, src = {{0}};
436     u32 mask = 32;
437     u64 total_hash_memory;
438     int i, table_id = -1, fib_index = ~0;
439
440     verbose = 1;
441     memory = matching = 0;
442     total_hash_memory = 0;
443
444     while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
445     {
446         if (unformat (input, "brief") || unformat (input, "summary")
447             || unformat (input, "sum"))
448             verbose = 0;
449         else if (unformat (input, "mem") || unformat (input, "memory"))
450             memory = 1;
451         else if (unformat (input, "%U %U",
452                            unformat_ip4_address, &src,
453                            unformat_ip4_address, &grp))
454         {
455             matching = 1;
456             mask = 64;
457         }
458         else if (unformat (input, "%U/%d", unformat_ip4_address, &grp, &mask))
459         {
460             clib_memset(&src, 0, sizeof(src));
461             matching = 1;
462         }
463         else if (unformat (input, "%U", unformat_ip4_address, &grp))
464         {
465             clib_memset(&src, 0, sizeof(src));
466             matching = 1;
467             mask = 32;
468         }
469         else if (unformat (input, "table %d", &table_id))
470             ;
471         else if (unformat (input, "index %d", &fib_index))
472             ;
473         else
474             break;
475     }
476
477     pool_foreach (mfib_table, im4->mfibs,
478     ({
479         ip4_mfib_t *mfib = &mfib_table->v4;
480
481         if (table_id >= 0 && table_id != (int)mfib->table_id)
482             continue;
483         if (fib_index != ~0 && fib_index != (int)mfib->index)
484             continue;
485
486         if (memory)
487         {
488             uword hash_size;
489
490             hash_size = 0;
491
492             for (i = 0; i < ARRAY_LEN (mfib->fib_entry_by_dst_address); i++)
493             {
494                 uword * hash = mfib->fib_entry_by_dst_address[i];
495                 if (NULL != hash)
496                 {
497                     hash_size += hash_bytes(hash);
498                 }
499             }
500             if (verbose)
501                 vlib_cli_output (vm, "%U hash:%d",
502                                  format_mfib_table_name, mfib->index,
503                                  FIB_PROTOCOL_IP4,
504                                  hash_size);
505             total_hash_memory += hash_size;
506             continue;
507         }
508
509         vlib_cli_output (vm, "%U, fib_index:%d flags:%U",
510                          format_mfib_table_name, mfib->index, FIB_PROTOCOL_IP4,
511                          mfib->index,
512                          format_mfib_table_flags, mfib_table->mft_flags);
513
514         /* Show summary? */
515         if (! verbose)
516         {
517             vlib_cli_output (vm, "%=20s%=16s", "Prefix length", "Count");
518             for (i = 0; i < ARRAY_LEN (mfib->fib_entry_by_dst_address); i++)
519             {
520                 uword * hash = mfib->fib_entry_by_dst_address[i];
521                 uword n_elts = hash_elts (hash);
522                 if (n_elts > 0)
523                     vlib_cli_output (vm, "%20d%16d", i, n_elts);
524             }
525             continue;
526         }
527
528         if (!matching)
529         {
530             ip4_mfib_table_show_all(mfib, vm);
531         }
532         else
533         {
534             ip4_mfib_table_show_one(mfib, vm, &src, &grp, mask);
535         }
536     }));
537     if (memory)
538         vlib_cli_output (vm, "totals: hash:%ld", total_hash_memory);
539
540     return 0;
541 }
542
543 /*?
544  * This command displays the IPv4 MulticasrFIB Tables (VRF Tables) and
545  * the route entries for each table.
546  *
547  * @note This command will run for a long time when the FIB tables are
548  * comprised of millions of entries. For those senarios, consider displaying
549  * a single table or summary mode.
550  *
551  * @cliexpar
552  * Example of how to display all the IPv4 Multicast FIB tables:
553  * @cliexstart{show ip fib}
554  * ipv4-VRF:0, fib_index 0
555  * (*, 0.0.0.0/0):  flags:D,
556  *  Interfaces:
557  *  multicast-ip4-chain
558  *   [@1]: dpo-drop ip4
559  * (*, 232.1.1.1/32):
560  * Interfaces:
561  *  test-eth1: Forward,
562  *  test-eth2: Forward,
563  *  test-eth0: Accept,
564  * multicast-ip4-chain
565  * [@2]: dpo-replicate: [index:1 buckets:2 to:[0:0]]
566  *   [0] [@1]: ipv4-mcast: test-eth1: IP4: d0:d1:d2:d3:d4:01 -> 01:00:05:00:00:00
567  *   [1] [@1]: ipv4-mcast: test-eth2: IP4: d0:d1:d2:d3:d4:02 -> 01:00:05:00:00:00
568  *
569  * @cliexend
570  * Example of how to display a summary of all IPv4 FIB tables:
571  * @cliexstart{show ip fib summary}
572  * ipv4-VRF:0, fib_index 0, flow hash: src dst sport dport proto
573  *     Prefix length         Count
574  *                    0               1
575  *                    8               2
576  *                   32               4
577  * ipv4-VRF:7, fib_index 1, flow hash: src dst sport dport proto
578  *     Prefix length         Count
579  *                    0               1
580  *                    8               2
581  *                   24               2
582  *                   32               4
583  * @cliexend
584  ?*/
585 /* *INDENT-OFF* */
586 VLIB_CLI_COMMAND (ip4_show_mfib_command, static) = {
587     .path = "show ip mfib",
588     .short_help = "show ip mfib [summary] [table <table-id>] [index <fib-id>] [<grp-addr>[/<mask>]] [<grp-addr>] [<src-addr> <grp-addr>]",
589     .function = ip4_show_mfib,
590 };
591 /* *INDENT-ON* */