blob: 3e7bdd81bbfab8b12b3bd4167c6918d22c0d8dc1 [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
36ip4_create_mfib_with_table_id (u32 table_id)
37{
38 mfib_table_t *mfib_table;
39
40 pool_get_aligned(ip4_main.mfibs, mfib_table, CLIB_CACHE_LINE_BYTES);
41 memset(mfib_table, 0, sizeof(*mfib_table));
42
43 mfib_table->mft_proto = FIB_PROTOCOL_IP4;
44 mfib_table->mft_index =
45 mfib_table->v4.index =
46 (mfib_table - ip4_main.mfibs);
47
48 hash_set (ip4_main.mfib_index_by_table_id,
49 table_id,
50 mfib_table->mft_index);
51
52 mfib_table->mft_table_id =
53 mfib_table->v4.table_id =
54 table_id;
55
56 mfib_table_lock(mfib_table->mft_index, FIB_PROTOCOL_IP4);
57
58 /*
59 * add the special entries into the new FIB
60 */
61 int ii;
62
63 for (ii = 0; ii < ARRAY_LEN(ip4_specials); ii++)
64 {
65 mfib_prefix_t prefix = ip4_specials[ii];
66
67 prefix.fp_src_addr.ip4.data_u32 =
68 clib_host_to_net_u32(prefix.fp_src_addr.ip4.data_u32);
69 prefix.fp_grp_addr.ip4.data_u32 =
70 clib_host_to_net_u32(prefix.fp_grp_addr.ip4.data_u32);
71
72 mfib_table_entry_update(mfib_table->mft_index,
73 &prefix,
74 MFIB_SOURCE_DEFAULT_ROUTE,
75 MFIB_ENTRY_FLAG_DROP);
76 }
77
78 return (mfib_table->mft_index);
79}
80
81void
82ip4_mfib_table_destroy (ip4_mfib_t *mfib)
83{
84 mfib_table_t *mfib_table = (mfib_table_t*)mfib;
85 int ii;
86
87 /*
88 * remove all the specials we added when the table was created.
89 */
90 for (ii = 0; ii < ARRAY_LEN(ip4_specials); ii++)
91 {
92 fib_node_index_t mfei;
93 mfib_prefix_t prefix = ip4_specials[ii];
94
95 prefix.fp_src_addr.ip4.data_u32 =
96 clib_host_to_net_u32(prefix.fp_src_addr.ip4.data_u32);
97 prefix.fp_grp_addr.ip4.data_u32 =
98 clib_host_to_net_u32(prefix.fp_grp_addr.ip4.data_u32);
99
100 mfei = mfib_table_lookup(mfib_table->mft_index, &prefix);
101 mfib_table_entry_delete_index(mfei, MFIB_SOURCE_DEFAULT_ROUTE);
102 }
103
104 /*
105 * validate no more routes.
106 */
107 ASSERT(0 == mfib_table->mft_total_route_counts);
108 ASSERT(~0 != mfib_table->mft_table_id);
109
110 hash_unset (ip4_main.mfib_index_by_table_id, mfib_table->mft_table_id);
111 pool_put(ip4_main.mfibs, mfib_table);
112}
113
114u32
115ip4_mfib_table_find_or_create_and_lock (u32 table_id)
116{
117 u32 index;
118
119 index = ip4_mfib_index_from_table_id(table_id);
120 if (~0 == index)
121 return ip4_create_mfib_with_table_id(table_id);
122 mfib_table_lock(index, FIB_PROTOCOL_IP4);
123
124 return (index);
125}
126
127u32
128ip4_mfib_table_get_index_for_sw_if_index (u32 sw_if_index)
129{
130 if (sw_if_index >= vec_len(ip4_main.mfib_index_by_sw_if_index))
131 {
132 /*
133 * This is the case for interfaces that are not yet mapped to
134 * a IP table
135 */
136 return (~0);
137 }
138 return (ip4_main.mfib_index_by_sw_if_index[sw_if_index]);
139}
140
141#define IPV4_MFIB_GRP_LEN(_len)\
142 (_len > 32 ? 32 : _len)
143
144#define IP4_MFIB_MK_KEY(_grp, _src, _len, _key) \
145{ \
146 _key = ((u64)(_grp->data_u32 & \
147 ip4_main.fib_masks[IPV4_MFIB_GRP_LEN(_len)])) << 32; \
148 _key |= _src->data_u32; \
149}
150#define IP4_MFIB_MK_GRP_KEY(_grp, _len, _key) \
151{ \
152 _key = ((u64)(_grp->data_u32 & \
153 ip4_main.fib_masks[IPV4_MFIB_GRP_LEN(_len)])) << 32; \
154}
155
156/*
157 * ip4_fib_table_lookup_exact_match
158 *
159 * Exact match prefix lookup
160 */
161fib_node_index_t
162ip4_mfib_table_lookup_exact_match (const ip4_mfib_t *mfib,
163 const ip4_address_t *grp,
164 const ip4_address_t *src,
165 u32 len)
166{
167 uword * hash, * result;
168 u64 key;
169
170 hash = mfib->fib_entry_by_dst_address[len];
171 IP4_MFIB_MK_KEY(grp, src, len, key);
172
173 result = hash_get(hash, key);
174
175 if (NULL != result) {
176 return (result[0]);
177 }
178 return (FIB_NODE_INDEX_INVALID);
179}
180
181/*
182 * ip4_fib_table_lookup
183 *
184 * Longest prefix match
185 */
186fib_node_index_t
187ip4_mfib_table_lookup (const ip4_mfib_t *mfib,
188 const ip4_address_t *src,
189 const ip4_address_t *grp,
190 u32 len)
191{
192 uword * hash, * result;
193 i32 mask_len;
194 u64 key;
195
196 mask_len = len;
197
198 if (PREDICT_TRUE(64 == mask_len))
199 {
200 hash = mfib->fib_entry_by_dst_address[mask_len];
201 IP4_MFIB_MK_KEY(grp, src, mask_len, key);
202
203 result = hash_get (hash, key);
204
205 if (NULL != result) {
206 return (result[0]);
207 }
208 }
209
210 for (mask_len = 32; mask_len >= 0; mask_len--)
211 {
212 hash = mfib->fib_entry_by_dst_address[mask_len];
213 IP4_MFIB_MK_GRP_KEY(grp, mask_len, key);
214
215 result = hash_get (hash, key);
216
217 if (NULL != result) {
218 return (result[0]);
219 }
220 }
221 return (FIB_NODE_INDEX_INVALID);
222}
223
224void
225ip4_mfib_table_entry_insert (ip4_mfib_t *mfib,
226 const ip4_address_t *grp,
227 const ip4_address_t *src,
228 u32 len,
229 fib_node_index_t fib_entry_index)
230{
231 uword * hash, * result;
232 u64 key;
233
234 IP4_MFIB_MK_KEY(grp, src, len, key);
235 hash = mfib->fib_entry_by_dst_address[len];
236 result = hash_get (hash, key);
237
238 if (NULL == result) {
239 /*
240 * adding a new entry
241 */
242 if (NULL == hash) {
243 hash = hash_create (32 /* elts */, sizeof (uword));
244 hash_set_flags (hash, HASH_FLAG_NO_AUTO_SHRINK);
245 }
246 hash = hash_set(hash, key, fib_entry_index);
247 mfib->fib_entry_by_dst_address[len] = hash;
248 }
249 else
250 {
251 ASSERT(0);
252 }
253}
254
255void
256ip4_mfib_table_entry_remove (ip4_mfib_t *mfib,
257 const ip4_address_t *grp,
258 const ip4_address_t *src,
259 u32 len)
260{
261 uword * hash, * result;
262 u64 key;
263
264 IP4_MFIB_MK_KEY(grp, src, len, key);
265 hash = mfib->fib_entry_by_dst_address[len];
266 result = hash_get (hash, key);
267
268 if (NULL == result)
269 {
270 /*
271 * removing a non-existant entry. i'll allow it.
272 */
273 }
274 else
275 {
276 hash_unset(hash, key);
277 }
278
279 mfib->fib_entry_by_dst_address[len] = hash;
280}
281
282static void
283ip4_mfib_table_show_all (ip4_mfib_t *mfib,
284 vlib_main_t * vm)
285{
286 fib_node_index_t *mfib_entry_indicies;
287 fib_node_index_t *mfib_entry_index;
288 int i;
289
290 mfib_entry_indicies = NULL;
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 vec_add1(mfib_entry_indicies, p->value[0]);
303 }));
304 }
305 }
306
307 vec_sort_with_function(mfib_entry_indicies, mfib_entry_cmp_for_sort);
308
309 vec_foreach(mfib_entry_index, mfib_entry_indicies)
310 {
311 vlib_cli_output(vm, "%U",
312 format_mfib_entry,
313 *mfib_entry_index,
314 MFIB_ENTRY_FORMAT_BRIEF);
315 }
316
317 vec_free(mfib_entry_indicies);
318}
319
320static void
321ip4_mfib_table_show_one (ip4_mfib_t *mfib,
322 vlib_main_t * vm,
323 ip4_address_t *src,
324 ip4_address_t *grp,
325 u32 mask_len)
326{
327 vlib_cli_output(vm, "%U",
328 format_mfib_entry,
329 ip4_mfib_table_lookup(mfib, src, grp, mask_len),
330 MFIB_ENTRY_FORMAT_DETAIL);
331}
332
333static clib_error_t *
334ip4_show_mfib (vlib_main_t * vm,
335 unformat_input_t * input,
336 vlib_cli_command_t * cmd)
337{
338 ip4_main_t * im4 = &ip4_main;
339 mfib_table_t *mfib_table;
340 int verbose, matching;
341 ip4_address_t grp, src = {{0}};
342 u32 mask = 32;
343 int i, table_id = -1, fib_index = ~0;
344
345 verbose = 1;
346 matching = 0;
347
348 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
349 {
350 if (unformat (input, "brief") || unformat (input, "summary")
351 || unformat (input, "sum"))
352 verbose = 0;
353
354 else if (unformat (input, "%U %U",
355 unformat_ip4_address, &src,
356 unformat_ip4_address, &grp))
357 {
358 matching = 1;
359 mask = 64;
360 }
361 else if (unformat (input, "%U", unformat_ip4_address, &grp))
362 {
363 matching = 1;
364 mask = 32;
365 }
366 else if (unformat (input, "%U/%d",
367 unformat_ip4_address, &grp, &mask))
368 matching = 1;
369 else if (unformat (input, "table %d", &table_id))
370 ;
371 else if (unformat (input, "index %d", &fib_index))
372 ;
373 else
374 break;
375 }
376
377 pool_foreach (mfib_table, im4->mfibs,
378 ({
379 ip4_mfib_t *mfib = &mfib_table->v4;
380
381 if (table_id >= 0 && table_id != (int)mfib->table_id)
382 continue;
383 if (fib_index != ~0 && fib_index != (int)mfib->index)
384 continue;
385
386 vlib_cli_output (vm, "%U, fib_index %d",
387 format_mfib_table_name, mfib->index, FIB_PROTOCOL_IP4,
388 mfib->index);
389
390 /* Show summary? */
391 if (! verbose)
392 {
393 vlib_cli_output (vm, "%=20s%=16s", "Prefix length", "Count");
394 for (i = 0; i < ARRAY_LEN (mfib->fib_entry_by_dst_address); i++)
395 {
396 uword * hash = mfib->fib_entry_by_dst_address[i];
397 uword n_elts = hash_elts (hash);
398 if (n_elts > 0)
399 vlib_cli_output (vm, "%20d%16d", i, n_elts);
400 }
401 continue;
402 }
403
404 if (!matching)
405 {
406 ip4_mfib_table_show_all(mfib, vm);
407 }
408 else
409 {
410 ip4_mfib_table_show_one(mfib, vm, &src, &grp, mask);
411 }
412 }));
413
414 return 0;
415}
416
417/*?
418 * This command displays the IPv4 MulticasrFIB Tables (VRF Tables) and
419 * the route entries for each table.
420 *
421 * @note This command will run for a long time when the FIB tables are
422 * comprised of millions of entries. For those senarios, consider displaying
423 * a single table or summary mode.
424 *
425 * @cliexpar
426 * Example of how to display all the IPv4 Multicast FIB tables:
427 * @cliexstart{show ip fib}
428 * ipv4-VRF:0, fib_index 0
429 * (*, 0.0.0.0/0): flags:D,
430 * Interfaces:
431 * multicast-ip4-chain
432 * [@1]: dpo-drop ip4
433 * (*, 232.1.1.1/32):
434 * Interfaces:
435 * test-eth1: Forward,
436 * test-eth2: Forward,
437 * test-eth0: Accept,
438 * multicast-ip4-chain
439 * [@2]: dpo-replicate: [index:1 buckets:2 to:[0:0]]
440 * [0] [@1]: ipv4-mcast: test-eth1: IP4: d0:d1:d2:d3:d4:01 -> 01:00:05:00:00:00
441 * [1] [@1]: ipv4-mcast: test-eth2: IP4: d0:d1:d2:d3:d4:02 -> 01:00:05:00:00:00
442 *
443 * @cliexend
444 * Example of how to display a summary of all IPv4 FIB tables:
445 * @cliexstart{show ip fib summary}
446 * ipv4-VRF:0, fib_index 0, flow hash: src dst sport dport proto
447 * Prefix length Count
448 * 0 1
449 * 8 2
450 * 32 4
451 * ipv4-VRF:7, fib_index 1, flow hash: src dst sport dport proto
452 * Prefix length Count
453 * 0 1
454 * 8 2
455 * 24 2
456 * 32 4
457 * @cliexend
458 ?*/
459/* *INDENT-OFF* */
Neale Rannsc756c1c2017-02-08 09:11:57 -0800460VLIB_CLI_COMMAND (ip4_show_mfib_command, static) = {
Neale Ranns32e1c012016-11-22 17:07:28 +0000461 .path = "show ip mfib",
Neale Rannsc756c1c2017-02-08 09:11:57 -0800462 .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 +0000463 .function = ip4_show_mfib,
464};
465/* *INDENT-ON* */