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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vnet/vnet.h>
18 #include <vnet/mfib/mfib_itf.h>
19 #include <vnet/mfib/mfib_signal.h>
20 #include <vnet/fib/fib_path.h>
22 mfib_itf_t *mfib_itf_pool;
25 mfib_itf_create (fib_node_index_t path_index,
26 mfib_itf_flags_t mfi_flags)
30 pool_get_aligned(mfib_itf_pool, mfib_itf,
31 CLIB_CACHE_LINE_BYTES);
33 mfib_itf->mfi_sw_if_index = fib_path_get_resolving_interface(path_index);
34 mfib_itf->mfi_si = INDEX_INVALID;
37 * add the path index to the per-path hash
39 mfib_itf->mfi_hash = hash_set(mfib_itf->mfi_hash, path_index, mfi_flags);
42 * the combined flags from all the paths is from just the one contributor
44 mfib_itf->mfi_flags = mfi_flags;
46 return (mfib_itf - mfib_itf_pool);
49 static mfib_itf_flags_t
50 mfib_itf_mk_flags (const mfib_itf_t *mfib_itf)
52 mfib_itf_flags_t combined_flags, flags;
53 fib_node_index_t *path_index;
55 combined_flags = MFIB_ITF_FLAG_NONE;
57 hash_foreach(path_index, flags, mfib_itf->mfi_hash,
59 combined_flags |= flags;
62 return (combined_flags);
66 mfib_itf_update (mfib_itf_t *mfib_itf,
67 fib_node_index_t path_index,
68 mfib_itf_flags_t mfi_flags)
71 * add or remove the path index to the per-path hash
73 if (MFIB_ITF_FLAG_NONE == mfi_flags)
75 hash_unset(mfib_itf->mfi_hash, path_index);
79 mfib_itf->mfi_hash = hash_set(mfib_itf->mfi_hash,
85 * re-generate the combined flags from all the paths.
87 mfib_itf->mfi_flags = mfib_itf_mk_flags(mfib_itf);
90 * The interface can be removed if there are no more flags
92 return (MFIB_ITF_FLAG_NONE == mfib_itf->mfi_flags);
96 mfib_itf_hash_flush (mfib_itf_t *mfi)
98 fib_node_index_t path_index, *path_indexp, *all = NULL;
99 mfib_itf_flags_t flags;
101 hash_foreach(path_index, flags, mfi->mfi_hash,
103 vec_add1(all, path_index);
106 vec_foreach(path_indexp, all)
108 hash_unset(mfi->mfi_hash, *path_indexp);
113 mfib_itf_delete (mfib_itf_t *mfi)
115 mfib_itf_hash_flush(mfi);
116 mfib_signal_remove_itf(mfi);
117 pool_put(mfib_itf_pool, mfi);
121 format_mfib_itf (u8 * s, va_list * args)
123 mfib_itf_t *mfib_itf;
127 mfi = va_arg (*args, index_t);
129 vnm = vnet_get_main();
130 mfib_itf = mfib_itf_get(mfi);
132 if (~0 != mfib_itf->mfi_sw_if_index)
134 return (format(s, " %U: %U",
135 format_vnet_sw_interface_name,
137 vnet_get_sw_interface(vnm,
138 mfib_itf->mfi_sw_if_index),
139 format_mfib_itf_flags, mfib_itf->mfi_flags));
143 return (format(s, " local: %U",
144 format_mfib_itf_flags, mfib_itf->mfi_flags));
149 static clib_error_t *
150 show_mfib_itf_command (vlib_main_t * vm,
151 unformat_input_t * input,
152 vlib_cli_command_t * cmd)
156 if (unformat (input, "%d", &mfii))
161 if (!pool_is_free_index(mfib_itf_pool, mfii))
163 vlib_cli_output (vm, "%d@%U",
165 format_mfib_itf, mfii);
169 vlib_cli_output (vm, "itf %d invalid", mfii);
177 vlib_cli_output (vm, "mFIB interfaces::");
178 pool_foreach_index(mfii, mfib_itf_pool,
180 vlib_cli_output (vm, "%d@%U",
182 format_mfib_itf, mfii);
190 * This commnad displays an MFIB interface, or all interfaces, indexed by their unique
191 * numerical indentifier.
193 VLIB_CLI_COMMAND (show_mfib_itf, static) = {
194 .path = "show mfib interface",
195 .function = show_mfib_itf_command,
196 .short_help = "show mfib interface",