blob: b2482580678bce78284d9f0374dc61248709ce47 [file] [log] [blame]
Neale Ranns32e1c012016-11-22 17:07:28 +00001/*
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
21static 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
35static u32
Neale Ranns15002542017-09-10 04:39:11 -070036ip4_create_mfib_with_table_id (u32 table_id,
37 mfib_source_t src)
Neale Ranns32e1c012016-11-22 17:07:28 +000038{
39 mfib_table_t *mfib_table;
40
41 pool_get_aligned(ip4_main.mfibs, mfib_table, CLIB_CACHE_LINE_BYTES);
42 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
Neale Ranns15002542017-09-10 04:39:11 -070057 mfib_table_lock(mfib_table->mft_index, FIB_PROTOCOL_IP4, src);
Neale Ranns32e1c012016-11-22 17:07:28 +000058
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,
Neale Ranns0f26c5a2017-03-01 15:12:11 -080076 MFIB_RPF_ID_NONE,
Neale Ranns32e1c012016-11-22 17:07:28 +000077 MFIB_ENTRY_FLAG_DROP);
78 }
79
80 return (mfib_table->mft_index);
81}
82
83void
84ip4_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
116u32
Neale Ranns15002542017-09-10 04:39:11 -0700117ip4_mfib_table_find_or_create_and_lock (u32 table_id,
118 mfib_source_t src)
Neale Ranns32e1c012016-11-22 17:07:28 +0000119{
120 u32 index;
121
122 index = ip4_mfib_index_from_table_id(table_id);
123 if (~0 == index)
Neale Ranns15002542017-09-10 04:39:11 -0700124 return ip4_create_mfib_with_table_id(table_id, src);
125 mfib_table_lock(index, FIB_PROTOCOL_IP4, src);
Neale Ranns32e1c012016-11-22 17:07:28 +0000126
127 return (index);
128}
129
130u32
131ip4_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 */
164fib_node_index_t
165ip4_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 */
189fib_node_index_t
190ip4_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 = 32; 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
227void
228ip4_mfib_table_entry_insert (ip4_mfib_t *mfib,
229 const ip4_address_t *grp,
230 const ip4_address_t *src,
231 u32 len,
232 fib_node_index_t fib_entry_index)
233{
234 uword * hash, * result;
235 u64 key;
236
237 IP4_MFIB_MK_KEY(grp, src, len, key);
238 hash = mfib->fib_entry_by_dst_address[len];
239 result = hash_get (hash, key);
240
241 if (NULL == result) {
242 /*
243 * adding a new entry
244 */
245 if (NULL == hash) {
246 hash = hash_create (32 /* elts */, sizeof (uword));
247 hash_set_flags (hash, HASH_FLAG_NO_AUTO_SHRINK);
248 }
249 hash = hash_set(hash, key, fib_entry_index);
250 mfib->fib_entry_by_dst_address[len] = hash;
251 }
252 else
253 {
254 ASSERT(0);
255 }
256}
257
258void
259ip4_mfib_table_entry_remove (ip4_mfib_t *mfib,
260 const ip4_address_t *grp,
261 const ip4_address_t *src,
262 u32 len)
263{
264 uword * hash, * result;
265 u64 key;
266
267 IP4_MFIB_MK_KEY(grp, src, len, key);
268 hash = mfib->fib_entry_by_dst_address[len];
269 result = hash_get (hash, key);
270
271 if (NULL == result)
272 {
273 /*
274 * removing a non-existant entry. i'll allow it.
275 */
276 }
277 else
278 {
279 hash_unset(hash, key);
280 }
281
282 mfib->fib_entry_by_dst_address[len] = hash;
283}
284
Neale Ranns5a8123b2017-01-26 01:18:23 -0800285void
286ip4_mfib_table_walk (ip4_mfib_t *mfib,
287 mfib_table_walk_fn_t fn,
288 void *ctx)
289{
290 int i;
291
292 for (i = 0; i < ARRAY_LEN (mfib->fib_entry_by_dst_address); i++)
293 {
294 uword * hash = mfib->fib_entry_by_dst_address[i];
295
296 if (NULL != hash)
297 {
298 hash_pair_t * p;
299
300 hash_foreach_pair (p, hash,
301 ({
302 fn(p->value[0], ctx);
303 }));
304 }
305 }
306}
307
Neale Ranns32e1c012016-11-22 17:07:28 +0000308static void
309ip4_mfib_table_show_all (ip4_mfib_t *mfib,
310 vlib_main_t * vm)
311{
312 fib_node_index_t *mfib_entry_indicies;
313 fib_node_index_t *mfib_entry_index;
314 int i;
315
316 mfib_entry_indicies = NULL;
317
318 for (i = 0; i < ARRAY_LEN (mfib->fib_entry_by_dst_address); i++)
319 {
320 uword * hash = mfib->fib_entry_by_dst_address[i];
321
322 if (NULL != hash)
323 {
324 hash_pair_t * p;
325
326 hash_foreach_pair (p, hash,
327 ({
328 vec_add1(mfib_entry_indicies, p->value[0]);
329 }));
330 }
331 }
332
333 vec_sort_with_function(mfib_entry_indicies, mfib_entry_cmp_for_sort);
334
335 vec_foreach(mfib_entry_index, mfib_entry_indicies)
336 {
337 vlib_cli_output(vm, "%U",
338 format_mfib_entry,
339 *mfib_entry_index,
340 MFIB_ENTRY_FORMAT_BRIEF);
341 }
342
343 vec_free(mfib_entry_indicies);
344}
345
346static void
347ip4_mfib_table_show_one (ip4_mfib_t *mfib,
348 vlib_main_t * vm,
349 ip4_address_t *src,
350 ip4_address_t *grp,
351 u32 mask_len)
352{
353 vlib_cli_output(vm, "%U",
354 format_mfib_entry,
355 ip4_mfib_table_lookup(mfib, src, grp, mask_len),
356 MFIB_ENTRY_FORMAT_DETAIL);
357}
358
359static clib_error_t *
360ip4_show_mfib (vlib_main_t * vm,
361 unformat_input_t * input,
362 vlib_cli_command_t * cmd)
363{
364 ip4_main_t * im4 = &ip4_main;
365 mfib_table_t *mfib_table;
366 int verbose, matching;
367 ip4_address_t grp, src = {{0}};
368 u32 mask = 32;
369 int i, table_id = -1, fib_index = ~0;
370
371 verbose = 1;
372 matching = 0;
373
374 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
375 {
376 if (unformat (input, "brief") || unformat (input, "summary")
377 || unformat (input, "sum"))
378 verbose = 0;
379
380 else if (unformat (input, "%U %U",
381 unformat_ip4_address, &src,
382 unformat_ip4_address, &grp))
383 {
384 matching = 1;
385 mask = 64;
386 }
Neale Rannsc7409dc2017-05-19 02:54:32 -0700387 else if (unformat (input, "%U/%d", unformat_ip4_address, &grp, &mask))
388 {
389 memset(&src, 0, sizeof(src));
390 matching = 1;
391 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000392 else if (unformat (input, "%U", unformat_ip4_address, &grp))
393 {
Neale Rannsc7409dc2017-05-19 02:54:32 -0700394 memset(&src, 0, sizeof(src));
Neale Ranns32e1c012016-11-22 17:07:28 +0000395 matching = 1;
396 mask = 32;
397 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000398 else if (unformat (input, "table %d", &table_id))
399 ;
400 else if (unformat (input, "index %d", &fib_index))
401 ;
402 else
403 break;
404 }
405
406 pool_foreach (mfib_table, im4->mfibs,
407 ({
408 ip4_mfib_t *mfib = &mfib_table->v4;
409
410 if (table_id >= 0 && table_id != (int)mfib->table_id)
411 continue;
412 if (fib_index != ~0 && fib_index != (int)mfib->index)
413 continue;
414
415 vlib_cli_output (vm, "%U, fib_index %d",
416 format_mfib_table_name, mfib->index, FIB_PROTOCOL_IP4,
417 mfib->index);
418
419 /* Show summary? */
420 if (! verbose)
421 {
422 vlib_cli_output (vm, "%=20s%=16s", "Prefix length", "Count");
423 for (i = 0; i < ARRAY_LEN (mfib->fib_entry_by_dst_address); i++)
424 {
425 uword * hash = mfib->fib_entry_by_dst_address[i];
426 uword n_elts = hash_elts (hash);
427 if (n_elts > 0)
428 vlib_cli_output (vm, "%20d%16d", i, n_elts);
429 }
430 continue;
431 }
432
433 if (!matching)
434 {
435 ip4_mfib_table_show_all(mfib, vm);
436 }
437 else
438 {
439 ip4_mfib_table_show_one(mfib, vm, &src, &grp, mask);
440 }
441 }));
442
443 return 0;
444}
445
446/*?
447 * This command displays the IPv4 MulticasrFIB Tables (VRF Tables) and
448 * the route entries for each table.
449 *
450 * @note This command will run for a long time when the FIB tables are
451 * comprised of millions of entries. For those senarios, consider displaying
452 * a single table or summary mode.
453 *
454 * @cliexpar
455 * Example of how to display all the IPv4 Multicast FIB tables:
456 * @cliexstart{show ip fib}
457 * ipv4-VRF:0, fib_index 0
458 * (*, 0.0.0.0/0): flags:D,
459 * Interfaces:
460 * multicast-ip4-chain
461 * [@1]: dpo-drop ip4
462 * (*, 232.1.1.1/32):
463 * Interfaces:
464 * test-eth1: Forward,
465 * test-eth2: Forward,
466 * test-eth0: Accept,
467 * multicast-ip4-chain
468 * [@2]: dpo-replicate: [index:1 buckets:2 to:[0:0]]
469 * [0] [@1]: ipv4-mcast: test-eth1: IP4: d0:d1:d2:d3:d4:01 -> 01:00:05:00:00:00
470 * [1] [@1]: ipv4-mcast: test-eth2: IP4: d0:d1:d2:d3:d4:02 -> 01:00:05:00:00:00
471 *
472 * @cliexend
473 * Example of how to display a summary of all IPv4 FIB tables:
474 * @cliexstart{show ip fib summary}
475 * ipv4-VRF:0, fib_index 0, flow hash: src dst sport dport proto
476 * Prefix length Count
477 * 0 1
478 * 8 2
479 * 32 4
480 * ipv4-VRF:7, fib_index 1, flow hash: src dst sport dport proto
481 * Prefix length Count
482 * 0 1
483 * 8 2
484 * 24 2
485 * 32 4
486 * @cliexend
487 ?*/
488/* *INDENT-OFF* */
Neale Rannsc756c1c2017-02-08 09:11:57 -0800489VLIB_CLI_COMMAND (ip4_show_mfib_command, static) = {
Neale Ranns32e1c012016-11-22 17:07:28 +0000490 .path = "show ip mfib",
Neale Rannsc756c1c2017-02-08 09:11:57 -0800491 .short_help = "show ip mfib [summary] [table <table-id>] [index <fib-id>] [<grp-addr>[/<mask>]] [<grp-addr>] [<src-addr> <grp-addr>]",
Neale Ranns32e1c012016-11-22 17:07:28 +0000492 .function = ip4_show_mfib,
493};
494/* *INDENT-ON* */