fib: Register multicast MAC with interface for accepting interfaces
[vpp.git] / src / vnet / mfib / mfib_itf.c
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/vnet.h>
17
18 #include <vnet/mfib/mfib_itf.h>
19 #include <vnet/mfib/mfib_signal.h>
20 #include <vnet/fib/fib_path.h>
21
22 mfib_itf_t *mfib_itf_pool;
23
24 index_t
25 mfib_itf_create (fib_node_index_t path_index,
26                  mfib_itf_flags_t mfi_flags)
27 {
28     mfib_itf_t *mfib_itf;
29
30     pool_get_aligned(mfib_itf_pool, mfib_itf,
31                      CLIB_CACHE_LINE_BYTES);
32
33     mfib_itf->mfi_sw_if_index = fib_path_get_resolving_interface(path_index);
34     mfib_itf->mfi_si = INDEX_INVALID;
35
36     /*
37      * add the path index to the per-path hash
38      */
39     mfib_itf->mfi_hash = hash_set(mfib_itf->mfi_hash, path_index, mfi_flags);
40
41     /*
42      * the combined flags from all the paths is from just the one contributor
43      */
44     mfib_itf->mfi_flags = mfi_flags;
45
46     return (mfib_itf - mfib_itf_pool);
47 }
48
49 static mfib_itf_flags_t
50 mfib_itf_mk_flags (const mfib_itf_t *mfib_itf)
51 {
52     mfib_itf_flags_t combined_flags, flags;
53     fib_node_index_t *path_index;
54
55     combined_flags = MFIB_ITF_FLAG_NONE;
56
57     hash_foreach(path_index, flags, mfib_itf->mfi_hash,
58     {
59         combined_flags |= flags;
60     });
61
62     return (combined_flags);
63 }
64
65 int
66 mfib_itf_update (mfib_itf_t *mfib_itf,
67                  fib_node_index_t path_index,
68                  mfib_itf_flags_t mfi_flags)
69 {
70     /*
71      * add or remove the path index to the per-path hash
72      */
73     if (MFIB_ITF_FLAG_NONE == mfi_flags)
74     {
75         hash_unset(mfib_itf->mfi_hash, path_index);
76     }
77     else
78     {
79         mfib_itf->mfi_hash = hash_set(mfib_itf->mfi_hash,
80                                       path_index,
81                                       mfi_flags);
82     }
83
84     /*
85      * re-generate the combined flags from all the paths.
86      */
87     mfib_itf->mfi_flags = mfib_itf_mk_flags(mfib_itf);
88
89     /*
90      * The interface can be removed if there are no more flags
91      */
92     return (MFIB_ITF_FLAG_NONE == mfib_itf->mfi_flags);
93 }
94
95 static void
96 mfib_itf_hash_flush (mfib_itf_t *mfi)
97 {
98     fib_node_index_t path_index, *path_indexp, *all = NULL;
99     mfib_itf_flags_t flags;
100
101     hash_foreach(path_index, flags, mfi->mfi_hash,
102     {
103         vec_add1(all, path_index);
104     });
105
106     vec_foreach(path_indexp, all)
107     {
108         hash_unset(mfi->mfi_hash, *path_indexp);
109     };
110 }
111
112 static void
113 mfib_itf_prefix4_to_mac (const mfib_prefix_t *pfx,
114                          mac_address_t *mac)
115 {
116     mac->bytes[0] = 0x01;
117     mac->bytes[1] = 0x0;
118     mac->bytes[2] = 0x5e;
119     mac->bytes[3] = pfx->fp_grp_addr.ip4.as_u8[1] & 0x7f;
120     mac->bytes[4] = pfx->fp_grp_addr.ip4.as_u8[2];
121     mac->bytes[5] = pfx->fp_grp_addr.ip4.as_u8[3];
122 }
123
124 static void
125 mfib_itf_prefix6_to_mac (const mfib_prefix_t *pfx,
126                          mac_address_t *mac)
127 {
128     mac->bytes[0] = 0x33;
129     mac->bytes[1] = 0x33;
130     mac->bytes[2] = pfx->fp_grp_addr.ip6.as_u8[12];
131     mac->bytes[3] = pfx->fp_grp_addr.ip6.as_u8[13];
132     mac->bytes[4] = pfx->fp_grp_addr.ip6.as_u8[14];
133     mac->bytes[5] = pfx->fp_grp_addr.ip6.as_u8[15];
134 }
135
136 static void
137 mfib_itf_prefix_to_mac (const mfib_prefix_t *pfx,
138                         mac_address_t *mac)
139 {
140     switch (pfx->fp_proto)
141     {
142     case FIB_PROTOCOL_IP4:
143         mfib_itf_prefix4_to_mac(pfx, mac);
144         break;
145     case FIB_PROTOCOL_IP6:
146         mfib_itf_prefix6_to_mac(pfx, mac);
147         break;
148     case FIB_PROTOCOL_MPLS:
149         break;
150     }
151 }
152
153 static void
154 mfib_itf_mac_add_del (mfib_itf_t *itf,
155                       const mfib_prefix_t *pfx,
156                       int add)
157 {
158     vnet_sw_interface_t *si;
159     vnet_main_t *vnm;
160     mac_address_t mac;
161
162     vnm = vnet_get_main();
163     mfib_itf_prefix_to_mac(pfx, &mac);
164
165     si = vnet_get_sw_interface(vnm, itf->mfi_sw_if_index);
166     vnet_hw_interface_add_del_mac_address (vnet_get_main(),
167                                            si->hw_if_index,
168                                            mac.bytes, add);
169 }
170
171 void
172 mfib_itf_mac_add (mfib_itf_t *itf,
173                   const mfib_prefix_t *pfx)
174 {
175     mfib_itf_mac_add_del(itf, pfx, 1);
176 }
177
178 void
179 mfib_itf_mac_del (mfib_itf_t *itf,
180                   const mfib_prefix_t *pfx)
181 {
182     mfib_itf_mac_add_del(itf, pfx, 0);
183 }
184
185 void
186 mfib_itf_delete (mfib_itf_t *mfi)
187 {
188     mfib_itf_hash_flush(mfi);
189     mfib_signal_remove_itf(mfi);
190     pool_put(mfib_itf_pool, mfi);
191 }
192
193 u8 *
194 format_mfib_itf (u8 * s, va_list * args)
195 {
196     mfib_itf_t *mfib_itf;
197     vnet_main_t *vnm;
198     index_t mfi;
199
200     mfi = va_arg (*args, index_t);
201
202     vnm = vnet_get_main();
203     mfib_itf = mfib_itf_get(mfi);
204
205     if (~0 != mfib_itf->mfi_sw_if_index)
206     {
207         return (format(s, " %U: %U",
208                        format_vnet_sw_interface_name,
209                        vnm,
210                        vnet_get_sw_interface(vnm,
211                                              mfib_itf->mfi_sw_if_index),
212                        format_mfib_itf_flags, mfib_itf->mfi_flags));
213     }
214     else
215     {
216         return (format(s, " local: %U",
217                        format_mfib_itf_flags, mfib_itf->mfi_flags));
218     }
219     return (s);
220 }
221
222 static clib_error_t *
223 show_mfib_itf_command (vlib_main_t * vm,
224                         unformat_input_t * input,
225                         vlib_cli_command_t * cmd)
226 {
227     index_t mfii;
228
229     if (unformat (input, "%d", &mfii))
230     {
231         /*
232          * show one in detail
233          */
234         if (!pool_is_free_index(mfib_itf_pool, mfii))
235         {
236             vlib_cli_output (vm, "%d@%U",
237                              mfii,
238                              format_mfib_itf, mfii);
239         }
240         else
241         {
242             vlib_cli_output (vm, "itf %d invalid", mfii);
243         }
244     }
245     else
246     {
247         /*
248          * show all
249          */
250         vlib_cli_output (vm, "mFIB interfaces::");
251         pool_foreach_index(mfii, mfib_itf_pool,
252         ({
253             vlib_cli_output (vm, "%d@%U",
254                              mfii,
255                              format_mfib_itf, mfii);
256         }));
257     }
258
259     return (NULL);
260 }
261
262 /*?
263  * This commnad displays an MFIB interface, or all interfaces, indexed by their unique
264  * numerical indentifier.
265  ?*/
266 VLIB_CLI_COMMAND (show_mfib_itf, static) = {
267   .path = "show mfib interface",
268   .function = show_mfib_itf_command,
269   .short_help = "show mfib interface",
270 };