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>
21 #include <vnet/ethernet/mac_address.h>
23 mfib_itf_t *mfib_itf_pool;
26 mfib_itf_create (fib_node_index_t path_index,
27 mfib_itf_flags_t mfi_flags)
31 pool_get_aligned(mfib_itf_pool, mfib_itf,
32 CLIB_CACHE_LINE_BYTES);
34 mfib_itf->mfi_sw_if_index = fib_path_get_resolving_interface(path_index);
35 mfib_itf->mfi_si = INDEX_INVALID;
38 * add the path index to the per-path hash
40 mfib_itf->mfi_hash = hash_set(mfib_itf->mfi_hash, path_index, mfi_flags);
43 * the combined flags from all the paths is from just the one contributor
45 mfib_itf->mfi_flags = mfi_flags;
47 return (mfib_itf - mfib_itf_pool);
50 static mfib_itf_flags_t
51 mfib_itf_mk_flags (const mfib_itf_t *mfib_itf)
53 mfib_itf_flags_t combined_flags, flags;
54 fib_node_index_t *path_index;
56 combined_flags = MFIB_ITF_FLAG_NONE;
58 hash_foreach(path_index, flags, mfib_itf->mfi_hash,
60 combined_flags |= flags;
63 return (combined_flags);
67 mfib_itf_update (mfib_itf_t *mfib_itf,
68 fib_node_index_t path_index,
69 mfib_itf_flags_t mfi_flags)
72 * add or remove the path index to the per-path hash
74 if (MFIB_ITF_FLAG_NONE == mfi_flags)
76 hash_unset(mfib_itf->mfi_hash, path_index);
80 mfib_itf->mfi_hash = hash_set(mfib_itf->mfi_hash,
86 * re-generate the combined flags from all the paths.
88 mfib_itf->mfi_flags = mfib_itf_mk_flags(mfib_itf);
91 * The interface can be removed if there are no more flags
93 return (MFIB_ITF_FLAG_NONE == mfib_itf->mfi_flags);
97 mfib_itf_hash_flush (mfib_itf_t *mfi)
99 fib_node_index_t path_index, *path_indexp, *all = NULL;
100 mfib_itf_flags_t flags;
102 hash_foreach(path_index, flags, mfi->mfi_hash,
104 vec_add1(all, path_index);
107 vec_foreach(path_indexp, all)
109 hash_unset(mfi->mfi_hash, *path_indexp);
114 mfib_itf_prefix4_to_mac (const mfib_prefix_t *pfx,
117 mac->bytes[0] = 0x01;
119 mac->bytes[2] = 0x5e;
120 mac->bytes[3] = pfx->fp_grp_addr.ip4.as_u8[1] & 0x7f;
121 mac->bytes[4] = pfx->fp_grp_addr.ip4.as_u8[2];
122 mac->bytes[5] = pfx->fp_grp_addr.ip4.as_u8[3];
126 mfib_itf_prefix6_to_mac (const mfib_prefix_t *pfx,
129 mac->bytes[0] = 0x33;
130 mac->bytes[1] = 0x33;
131 mac->bytes[2] = pfx->fp_grp_addr.ip6.as_u8[12];
132 mac->bytes[3] = pfx->fp_grp_addr.ip6.as_u8[13];
133 mac->bytes[4] = pfx->fp_grp_addr.ip6.as_u8[14];
134 mac->bytes[5] = pfx->fp_grp_addr.ip6.as_u8[15];
138 mfib_itf_prefix_to_mac (const mfib_prefix_t *pfx,
141 switch (pfx->fp_proto)
143 case FIB_PROTOCOL_IP4:
144 mfib_itf_prefix4_to_mac(pfx, mac);
146 case FIB_PROTOCOL_IP6:
147 mfib_itf_prefix6_to_mac(pfx, mac);
149 case FIB_PROTOCOL_MPLS:
155 mfib_itf_mac_add_del (mfib_itf_t *itf,
156 const mfib_prefix_t *pfx,
159 vnet_sw_interface_t *si;
163 vnm = vnet_get_main();
164 mfib_itf_prefix_to_mac(pfx, &mac);
166 si = vnet_get_sw_interface(vnm, itf->mfi_sw_if_index);
167 vnet_hw_interface_add_del_mac_address (vnet_get_main(),
173 mfib_itf_mac_add (mfib_itf_t *itf,
174 const mfib_prefix_t *pfx)
176 mfib_itf_mac_add_del(itf, pfx, 1);
180 mfib_itf_mac_del (mfib_itf_t *itf,
181 const mfib_prefix_t *pfx)
183 mfib_itf_mac_add_del(itf, pfx, 0);
187 mfib_itf_delete (mfib_itf_t *mfi)
189 mfib_itf_hash_flush(mfi);
190 mfib_signal_remove_itf(mfi);
191 pool_put(mfib_itf_pool, mfi);
195 format_mfib_itf (u8 * s, va_list * args)
197 mfib_itf_t *mfib_itf;
201 mfi = va_arg (*args, index_t);
203 vnm = vnet_get_main();
204 mfib_itf = mfib_itf_get(mfi);
206 if (~0 != mfib_itf->mfi_sw_if_index)
208 return (format(s, " %U: %U",
209 format_vnet_sw_interface_name,
211 vnet_get_sw_interface(vnm,
212 mfib_itf->mfi_sw_if_index),
213 format_mfib_itf_flags, mfib_itf->mfi_flags));
217 return (format(s, " local: %U",
218 format_mfib_itf_flags, mfib_itf->mfi_flags));
223 static clib_error_t *
224 show_mfib_itf_command (vlib_main_t * vm,
225 unformat_input_t * input,
226 vlib_cli_command_t * cmd)
230 if (unformat (input, "%d", &mfii))
235 if (!pool_is_free_index(mfib_itf_pool, mfii))
237 vlib_cli_output (vm, "%d@%U",
239 format_mfib_itf, mfii);
243 vlib_cli_output (vm, "itf %d invalid", mfii);
251 vlib_cli_output (vm, "mFIB interfaces::");
252 pool_foreach_index (mfii, mfib_itf_pool)
254 vlib_cli_output (vm, "%d@%U",
256 format_mfib_itf, mfii);
264 * This command displays an MFIB interface, or all interfaces, indexed by their
265 * unique numerical identifier.
267 VLIB_CLI_COMMAND (show_mfib_itf, static) = {
268 .path = "show mfib interface",
269 .function = show_mfib_itf_command,
270 .short_help = "show mfib interface",