FD.io VPP  v17.04-9-g99c0734
Vector Packet Processing
ip4_mfib.c
Go to the documentation of this file.
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
37 {
38  mfib_table_t *mfib_table;
39 
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 
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 
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 
73  &prefix,
76  }
77 
78  return (mfib_table->mft_index);
79 }
80 
81 void
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);
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 
111  pool_put(ip4_main.mfibs, mfib_table);
112 }
113 
114 u32
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);
123 
124  return (index);
125 }
126 
127 u32
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  */
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  */
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 
224 void
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));
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 
255 void
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 
282 void
285  void *ctx)
286 {
287  int i;
288 
289  for (i = 0; i < ARRAY_LEN (mfib->fib_entry_by_dst_address); i++)
290  {
291  uword * hash = mfib->fib_entry_by_dst_address[i];
292 
293  if (NULL != hash)
294  {
295  hash_pair_t * p;
296 
297  hash_foreach_pair (p, hash,
298  ({
299  fn(p->value[0], ctx);
300  }));
301  }
302  }
303 }
304 
305 static void
307  vlib_main_t * vm)
308 {
309  fib_node_index_t *mfib_entry_indicies;
310  fib_node_index_t *mfib_entry_index;
311  int i;
312 
313  mfib_entry_indicies = NULL;
314 
315  for (i = 0; i < ARRAY_LEN (mfib->fib_entry_by_dst_address); i++)
316  {
317  uword * hash = mfib->fib_entry_by_dst_address[i];
318 
319  if (NULL != hash)
320  {
321  hash_pair_t * p;
322 
323  hash_foreach_pair (p, hash,
324  ({
325  vec_add1(mfib_entry_indicies, p->value[0]);
326  }));
327  }
328  }
329 
330  vec_sort_with_function(mfib_entry_indicies, mfib_entry_cmp_for_sort);
331 
332  vec_foreach(mfib_entry_index, mfib_entry_indicies)
333  {
334  vlib_cli_output(vm, "%U",
336  *mfib_entry_index,
338  }
339 
340  vec_free(mfib_entry_indicies);
341 }
342 
343 static void
345  vlib_main_t * vm,
346  ip4_address_t *src,
347  ip4_address_t *grp,
348  u32 mask_len)
349 {
350  vlib_cli_output(vm, "%U",
352  ip4_mfib_table_lookup(mfib, src, grp, mask_len),
354 }
355 
356 static clib_error_t *
358  unformat_input_t * input,
359  vlib_cli_command_t * cmd)
360 {
361  ip4_main_t * im4 = &ip4_main;
362  mfib_table_t *mfib_table;
363  int verbose, matching;
364  ip4_address_t grp, src = {{0}};
365  u32 mask = 32;
366  int i, table_id = -1, fib_index = ~0;
367 
368  verbose = 1;
369  matching = 0;
370 
372  {
373  if (unformat (input, "brief") || unformat (input, "summary")
374  || unformat (input, "sum"))
375  verbose = 0;
376 
377  else if (unformat (input, "%U %U",
378  unformat_ip4_address, &src,
379  unformat_ip4_address, &grp))
380  {
381  matching = 1;
382  mask = 64;
383  }
384  else if (unformat (input, "%U", unformat_ip4_address, &grp))
385  {
386  matching = 1;
387  mask = 32;
388  }
389  else if (unformat (input, "%U/%d",
390  unformat_ip4_address, &grp, &mask))
391  matching = 1;
392  else if (unformat (input, "table %d", &table_id))
393  ;
394  else if (unformat (input, "index %d", &fib_index))
395  ;
396  else
397  break;
398  }
399 
400  pool_foreach (mfib_table, im4->mfibs,
401  ({
402  ip4_mfib_t *mfib = &mfib_table->v4;
403 
404  if (table_id >= 0 && table_id != (int)mfib->table_id)
405  continue;
406  if (fib_index != ~0 && fib_index != (int)mfib->index)
407  continue;
408 
409  vlib_cli_output (vm, "%U, fib_index %d",
410  format_mfib_table_name, mfib->index, FIB_PROTOCOL_IP4,
411  mfib->index);
412 
413  /* Show summary? */
414  if (! verbose)
415  {
416  vlib_cli_output (vm, "%=20s%=16s", "Prefix length", "Count");
417  for (i = 0; i < ARRAY_LEN (mfib->fib_entry_by_dst_address); i++)
418  {
419  uword * hash = mfib->fib_entry_by_dst_address[i];
420  uword n_elts = hash_elts (hash);
421  if (n_elts > 0)
422  vlib_cli_output (vm, "%20d%16d", i, n_elts);
423  }
424  continue;
425  }
426 
427  if (!matching)
428  {
429  ip4_mfib_table_show_all(mfib, vm);
430  }
431  else
432  {
433  ip4_mfib_table_show_one(mfib, vm, &src, &grp, mask);
434  }
435  }));
436 
437  return 0;
438 }
439 
440 /*?
441  * This command displays the IPv4 MulticasrFIB Tables (VRF Tables) and
442  * the route entries for each table.
443  *
444  * @note This command will run for a long time when the FIB tables are
445  * comprised of millions of entries. For those senarios, consider displaying
446  * a single table or summary mode.
447  *
448  * @cliexpar
449  * Example of how to display all the IPv4 Multicast FIB tables:
450  * @cliexstart{show ip fib}
451  * ipv4-VRF:0, fib_index 0
452  * (*, 0.0.0.0/0): flags:D,
453  * Interfaces:
454  * multicast-ip4-chain
455  * [@1]: dpo-drop ip4
456  * (*, 232.1.1.1/32):
457  * Interfaces:
458  * test-eth1: Forward,
459  * test-eth2: Forward,
460  * test-eth0: Accept,
461  * multicast-ip4-chain
462  * [@2]: dpo-replicate: [index:1 buckets:2 to:[0:0]]
463  * [0] [@1]: ipv4-mcast: test-eth1: IP4: d0:d1:d2:d3:d4:01 -> 01:00:05:00:00:00
464  * [1] [@1]: ipv4-mcast: test-eth2: IP4: d0:d1:d2:d3:d4:02 -> 01:00:05:00:00:00
465  *
466  * @cliexend
467  * Example of how to display a summary of all IPv4 FIB tables:
468  * @cliexstart{show ip fib summary}
469  * ipv4-VRF:0, fib_index 0, flow hash: src dst sport dport proto
470  * Prefix length Count
471  * 0 1
472  * 8 2
473  * 32 4
474  * ipv4-VRF:7, fib_index 1, flow hash: src dst sport dport proto
475  * Prefix length Count
476  * 0 1
477  * 8 2
478  * 24 2
479  * 32 4
480  * @cliexend
481  ?*/
482 /* *INDENT-OFF* */
483 VLIB_CLI_COMMAND (ip4_show_mfib_command, static) = {
484  .path = "show ip mfib",
485  .short_help = "show ip mfib [summary] [table <table-id>] [index <fib-id>] [<grp-addr>[/<mask>]] [<grp-addr>] [<src-addr> <grp-addr>]",
486  .function = ip4_show_mfib,
487 };
488 /* *INDENT-ON* */
#define MFIB_ENTRY_FORMAT_DETAIL
Definition: mfib_entry.h:84
static clib_error_t * ip4_show_mfib(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: ip4_mfib.c:357
ip46_address_t fp_src_addr
Definition: mfib_types.h:47
#define hash_set(h, key, value)
Definition: hash.h:254
fib_node_index_t mfib_table_lookup(u32 fib_index, const mfib_prefix_t *prefix)
Perfom a longest prefix match in the non-forwarding table.
Definition: mfib_table.c:65
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:343
#define hash_unset(h, key)
Definition: hash.h:260
u32 index
Definition: ip4.h:80
u32 ip4_mfib_table_find_or_create_and_lock(u32 table_id)
Get or create an IPv4 fib.
Definition: ip4_mfib.c:115
#define PREDICT_TRUE(x)
Definition: clib.h:98
u32 ip4_mfib_table_get_index_for_sw_if_index(u32 sw_if_index)
Definition: ip4_mfib.c:128
#define NULL
Definition: clib.h:55
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:522
static void ip4_mfib_table_show_one(ip4_mfib_t *mfib, vlib_main_t *vm, ip4_address_t *src, ip4_address_t *grp, u32 mask_len)
Definition: ip4_mfib.c:344
u32 mft_total_route_counts
Total route counters.
Definition: mfib_table.h:65
#define MFIB_ENTRY_FORMAT_BRIEF
Definition: mfib_entry.h:83
uword value[0]
Definition: hash.h:164
u32 * mfib_index_by_sw_if_index
Table index indexed by software interface.
Definition: ip4.h:123
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:376
unformat_function_t unformat_ip4_address
Definition: format.h:76
fib_node_index_t mft_index
Index into FIB vector.
Definition: mfib_table.h:60
void mfib_table_entry_delete_index(fib_node_index_t mfib_entry_index, mfib_source_t source)
Delete a FIB entry.
Definition: mfib_table.c:374
#define IP4_MFIB_MK_GRP_KEY(_grp, _len, _key)
Definition: ip4_mfib.c:150
int i32
Definition: types.h:81
fib_node_index_t mfib_table_entry_update(u32 fib_index, const mfib_prefix_t *prefix, mfib_source_t source, mfib_entry_flags_t entry_flags)
Add a new (with no replication) or lock an existing entry.
Definition: mfib_table.c:165
unsigned long u64
Definition: types.h:89
static void hash_set_flags(void *v, uword flags)
Definition: hash.h:152
struct mfib_table_t_ * mfibs
Vector of MFIBs.
Definition: ip4.h:115
void ip4_mfib_table_walk(ip4_mfib_t *mfib, mfib_table_walk_fn_t fn, void *ctx)
Walk the IP4 mfib table.
Definition: ip4_mfib.c:283
#define hash_get(h, key)
Definition: hash.h:248
void ip4_mfib_table_entry_remove(ip4_mfib_t *mfib, const ip4_address_t *grp, const ip4_address_t *src, u32 len)
Definition: ip4_mfib.c:256
struct _unformat_input_t unformat_input_t
#define IP4_MFIB_MK_KEY(_grp, _src, _len, _key)
Definition: ip4_mfib.c:144
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:241
Definition: ip4.h:71
int mfib_entry_cmp_for_sort(void *i1, void *i2)
Definition: mfib_entry.c:987
#define pool_get_aligned(P, E, A)
Allocate an object E from a pool P (general version).
Definition: pool.h:169
#define UNFORMAT_END_OF_INPUT
Definition: format.h:143
vlib_main_t * vm
Definition: buffer.c:276
fib_node_index_t ip4_mfib_table_lookup_exact_match(const ip4_mfib_t *mfib, const ip4_address_t *grp, const ip4_address_t *src, u32 len)
Definition: ip4_mfib.c:162
u8 * format_mfib_entry(u8 *s, va_list *args)
Definition: mfib_entry.c:127
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:340
u32 fib_node_index_t
A typedef of a node index.
Definition: fib_types.h:28
#define ARRAY_LEN(x)
Definition: clib.h:59
Aggregrate type for a prefix.
Definition: mfib_types.h:24
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:154
uword * fib_entry_by_dst_address[65]
Definition: ip4.h:74
#define hash_create(elts, value_bytes)
Definition: hash.h:658
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
IPv4 main type.
Definition: ip4.h:107
fib_node_index_t ip4_mfib_table_lookup(const ip4_mfib_t *mfib, const ip4_address_t *src, const ip4_address_t *grp, u32 len)
The IPv4 Multicast-FIB.
Definition: ip4_mfib.c:187
void ip4_mfib_table_entry_insert(ip4_mfib_t *mfib, const ip4_address_t *grp, const ip4_address_t *src, u32 len, fib_node_index_t fib_entry_index)
Definition: ip4_mfib.c:225
fib_protocol_t mft_proto
Which protocol this table serves.
Definition: mfib_table.h:45
u32 mft_table_id
Table ID (hash key) for this FIB.
Definition: mfib_table.h:55
static u32 ip4_create_mfib_with_table_id(u32 table_id)
Definition: ip4_mfib.c:36
u64 uword
Definition: types.h:112
static void ip4_mfib_table_show_all(ip4_mfib_t *mfib, vlib_main_t *vm)
Definition: ip4_mfib.c:306
#define FIB_NODE_INDEX_INVALID
Definition: fib_types.h:29
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
#define hash_foreach_pair(p, v, body)
Iterate over hash pairs.
Definition: hash.h:349
#define vec_sort_with_function(vec, f)
Sort a vector using the supplied element comparison function.
Definition: vec.h:960
void mfib_table_lock(u32 fib_index, fib_protocol_t proto)
Release a reference counting lock on the table.
Definition: mfib_table.c:483
int(* mfib_table_walk_fn_t)(fib_node_index_t fei, void *ctx)
Call back function when walking entries in a FIB table.
Definition: mfib_table.h:366
ip4_mfib_t v4
Definition: mfib_table.h:38
A protocol Independent IP multicast FIB table.
Definition: mfib_table.h:29
ip4_main_t ip4_main
Global ip4 main structure.
Definition: ip4_forward.c:1117
#define vec_foreach(var, vec)
Vector iterator.
u32 table_id
Definition: ip4.h:77
uword * mfib_index_by_table_id
Hash table mapping table id to multicast fib index.
Definition: ip4.h:134
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
static u32 ip4_mfib_index_from_table_id(u32 table_id)
Definition: ip4_mfib.h:79
#define HASH_FLAG_NO_AUTO_SHRINK
Definition: hash.h:64
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:577
void ip4_mfib_table_destroy(ip4_mfib_t *mfib)
Definition: ip4_mfib.c:82
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:971
ip46_address_t fp_grp_addr
The address type is not deriveable from the fp_addr member.
Definition: mfib_types.h:46
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:169