blob: 85e9c31e2fb8e9e40f503ef4163d0e4870d82f7f [file] [log] [blame]
Matus Fabianf468e232016-12-02 06:00:53 -08001/*
2 *------------------------------------------------------------------
3 * l2_api.c - layer 2 forwarding api
4 *
5 * Copyright (c) 2016 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
18 */
19
20#include <vnet/vnet.h>
21#include <vlibmemory/api.h>
22
23#include <vnet/interface.h>
24#include <vnet/api_errno.h>
25#include <vnet/l2/l2_input.h>
Pavel Kotucek0f971d82017-01-03 10:48:54 +010026#include <vnet/l2/l2_fib.h>
Pavel Kotucekadec5872017-01-25 08:50:53 +010027#include <vnet/l2/l2_vtr.h>
John Lo8d00fff2017-08-03 00:35:36 -040028#include <vnet/l2/l2_learn.h>
Neale Rannsb4743802018-09-05 09:13:57 -070029#include <vnet/l2/l2_bd.h>
Neale Ranns192b13f2019-03-15 02:16:20 -070030#include <vnet/l2/l2_bvi.h>
Neale Rannscbe25aa2019-09-30 10:53:31 +000031#include <vnet/l2/l2_arp_term.h>
Neale Ranns4d5b9172018-10-24 02:57:49 -070032#include <vnet/ip/ip_types_api.h>
33#include <vnet/ethernet/ethernet_types_api.h>
Matus Fabianf468e232016-12-02 06:00:53 -080034
35#include <vnet/vnet_msg_enum.h>
36
37#define vl_typedefs /* define message structures */
38#include <vnet/vnet_all_api_h.h>
39#undef vl_typedefs
40
41#define vl_endianfun /* define message structures */
42#include <vnet/vnet_all_api_h.h>
43#undef vl_endianfun
44
45/* instantiate all the print functions we know about */
46#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
47#define vl_printfun
48#include <vnet/vnet_all_api_h.h>
49#undef vl_printfun
50
51#include <vlibapi/api_helper_macros.h>
52
Jerome Tollet5f93e3b2020-12-18 09:44:24 +010053#define foreach_vpe_api_msg \
54 _ (L2_XCONNECT_DUMP, l2_xconnect_dump) \
55 _ (L2_FIB_CLEAR_TABLE, l2_fib_clear_table) \
56 _ (L2_FIB_TABLE_DUMP, l2_fib_table_dump) \
57 _ (L2FIB_FLUSH_ALL, l2fib_flush_all) \
58 _ (L2FIB_FLUSH_INT, l2fib_flush_int) \
59 _ (L2FIB_FLUSH_BD, l2fib_flush_bd) \
60 _ (L2FIB_ADD_DEL, l2fib_add_del) \
Jerome Tollet0f8d1002021-01-07 12:44:17 +010061 _ (L2FIB_SET_SCAN_DELAY, l2fib_set_scan_delay) \
Jerome Tollet5f93e3b2020-12-18 09:44:24 +010062 _ (WANT_L2_MACS_EVENTS, want_l2_macs_events) \
Jerome Tollet0f8d1002021-01-07 12:44:17 +010063 _ (WANT_L2_MACS_EVENTS2, want_l2_macs_events2) \
Jerome Tollet5f93e3b2020-12-18 09:44:24 +010064 _ (L2_FLAGS, l2_flags) \
65 _ (SW_INTERFACE_SET_L2_XCONNECT, sw_interface_set_l2_xconnect) \
66 _ (SW_INTERFACE_SET_L2_BRIDGE, sw_interface_set_l2_bridge) \
67 _ (L2_PATCH_ADD_DEL, l2_patch_add_del) \
68 _ (L2_INTERFACE_EFP_FILTER, l2_interface_efp_filter) \
69 _ (BD_IP_MAC_ADD_DEL, bd_ip_mac_add_del) \
70 _ (BD_IP_MAC_FLUSH, bd_ip_mac_flush) \
71 _ (BD_IP_MAC_DUMP, bd_ip_mac_dump) \
72 _ (BRIDGE_DOMAIN_ADD_DEL, bridge_domain_add_del) \
73 _ (BRIDGE_DOMAIN_DUMP, bridge_domain_dump) \
74 _ (BRIDGE_FLAGS, bridge_flags) \
75 _ (L2_INTERFACE_VLAN_TAG_REWRITE, l2_interface_vlan_tag_rewrite) \
76 _ (L2_INTERFACE_PBB_TAG_REWRITE, l2_interface_pbb_tag_rewrite) \
77 _ (BRIDGE_DOMAIN_SET_MAC_AGE, bridge_domain_set_mac_age) \
78 _ (SW_INTERFACE_SET_VPATH, sw_interface_set_vpath) \
79 _ (BVI_CREATE, bvi_create) \
80 _ (BVI_DELETE, bvi_delete) \
81 _ (WANT_L2_ARP_TERM_EVENTS, want_l2_arp_term_events) \
82 _ (BRIDGE_DOMAIN_SET_LEARN_LIMIT, bridge_domain_set_learn_limit) \
83 _ (BRIDGE_DOMAIN_SET_DEFAULT_LEARN_LIMIT, \
84 bridge_domain_set_default_learn_limit)
Matus Fabianf468e232016-12-02 06:00:53 -080085
86static void
Florin Coras6c4dae22018-01-09 06:39:23 -080087send_l2_xconnect_details (vl_api_registration_t * reg, u32 context,
Matus Fabianf468e232016-12-02 06:00:53 -080088 u32 rx_sw_if_index, u32 tx_sw_if_index)
89{
90 vl_api_l2_xconnect_details_t *mp;
91
92 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -040093 clib_memset (mp, 0, sizeof (*mp));
Matus Fabianf468e232016-12-02 06:00:53 -080094 mp->_vl_msg_id = ntohs (VL_API_L2_XCONNECT_DETAILS);
95 mp->context = context;
96 mp->rx_sw_if_index = htonl (rx_sw_if_index);
97 mp->tx_sw_if_index = htonl (tx_sw_if_index);
98
Florin Coras6c4dae22018-01-09 06:39:23 -080099 vl_api_send_msg (reg, (u8 *) mp);
Matus Fabianf468e232016-12-02 06:00:53 -0800100}
101
102static void
103vl_api_l2_xconnect_dump_t_handler (vl_api_l2_xconnect_dump_t * mp)
104{
Florin Coras6c4dae22018-01-09 06:39:23 -0800105 vl_api_registration_t *reg;
Matus Fabianf468e232016-12-02 06:00:53 -0800106 vnet_main_t *vnm = vnet_get_main ();
107 vnet_interface_main_t *im = &vnm->interface_main;
108 l2input_main_t *l2im = &l2input_main;
109 vnet_sw_interface_t *swif;
110 l2_input_config_t *config;
111
Florin Coras6c4dae22018-01-09 06:39:23 -0800112 reg = vl_api_client_index_to_registration (mp->client_index);
113 if (!reg)
Matus Fabianf468e232016-12-02 06:00:53 -0800114 return;
115
116 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100117 pool_foreach (swif, im->sw_interfaces)
118 {
Matus Fabianf468e232016-12-02 06:00:53 -0800119 config = vec_elt_at_index (l2im->configs, swif->sw_if_index);
Neale Ranns47a3d992020-09-29 15:38:51 +0000120 if (l2_input_is_xconnect(config))
Florin Coras6c4dae22018-01-09 06:39:23 -0800121 send_l2_xconnect_details (reg, mp->context, swif->sw_if_index,
Matus Fabianf468e232016-12-02 06:00:53 -0800122 config->output_sw_if_index);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100123 }
Matus Fabianf468e232016-12-02 06:00:53 -0800124 /* *INDENT-ON* */
125}
126
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100127static void
128vl_api_l2_fib_clear_table_t_handler (vl_api_l2_fib_clear_table_t * mp)
129{
130 int rv = 0;
131 vl_api_l2_fib_clear_table_reply_t *rmp;
132
Eyal Bari7537e712017-04-27 14:07:55 +0300133 /* Clear all MACs including static MACs */
134 l2fib_clear_table ();
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100135
136 REPLY_MACRO (VL_API_L2_FIB_CLEAR_TABLE_REPLY);
137}
138
139static void
140send_l2fib_table_entry (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -0800141 vl_api_registration_t * reg,
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100142 l2fib_entry_key_t * l2fe_key,
143 l2fib_entry_result_t * l2fe_res, u32 context)
144{
Ole Troan01384fe2017-05-12 11:55:35 +0200145 vl_api_l2_fib_table_details_t *mp;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100146
147 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400148 clib_memset (mp, 0, sizeof (*mp));
Ole Troan01384fe2017-05-12 11:55:35 +0200149 mp->_vl_msg_id = ntohs (VL_API_L2_FIB_TABLE_DETAILS);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100150
151 mp->bd_id =
152 ntohl (l2input_main.bd_configs[l2fe_key->fields.bd_index].bd_id);
153
Jakub Grajciar145e3302019-10-24 13:52:42 +0200154 mac_address_encode ((mac_address_t *) l2fe_key->fields.mac, mp->mac);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100155 mp->sw_if_index = ntohl (l2fe_res->fields.sw_if_index);
Neale Ranns7341b6d2018-09-19 04:07:02 -0700156 mp->static_mac = (l2fib_entry_result_is_set_STATIC (l2fe_res) ? 1 : 0);
157 mp->filter_mac = (l2fib_entry_result_is_set_FILTER (l2fe_res) ? 1 : 0);
158 mp->bvi_mac = (l2fib_entry_result_is_set_BVI (l2fe_res) ? 1 : 0);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100159 mp->context = context;
160
Florin Coras6c4dae22018-01-09 06:39:23 -0800161 vl_api_send_msg (reg, (u8 *) mp);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100162}
163
164static void
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100165vl_api_l2_fib_table_dump_t_handler (vl_api_l2_fib_table_dump_t * mp)
166{
167 vpe_api_main_t *am = &vpe_api_main;
168 bd_main_t *bdm = &bd_main;
169 l2fib_entry_key_t *l2fe_key = NULL;
170 l2fib_entry_result_t *l2fe_res = NULL;
171 u32 ni, bd_id = ntohl (mp->bd_id);
172 u32 bd_index;
Florin Coras6c4dae22018-01-09 06:39:23 -0800173 vl_api_registration_t *reg;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100174 uword *p;
175
Florin Coras6c4dae22018-01-09 06:39:23 -0800176 reg = vl_api_client_index_to_registration (mp->client_index);
177 if (!reg)
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100178 return;
179
180 /* see l2fib_table_dump: ~0 means "any" */
181 if (bd_id == ~0)
182 bd_index = ~0;
183 else
184 {
185 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
186 if (p == 0)
187 return;
188
189 bd_index = p[0];
190 }
191
192 l2fib_table_dump (bd_index, &l2fe_key, &l2fe_res);
193
194 vec_foreach_index (ni, l2fe_key)
195 {
Florin Coras6c4dae22018-01-09 06:39:23 -0800196 send_l2fib_table_entry (am, reg, vec_elt_at_index (l2fe_key, ni),
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100197 vec_elt_at_index (l2fe_res, ni), mp->context);
198 }
199 vec_free (l2fe_key);
200 vec_free (l2fe_res);
201}
202
203static void
204vl_api_l2fib_add_del_t_handler (vl_api_l2fib_add_del_t * mp)
205{
206 bd_main_t *bdm = &bd_main;
207 l2input_main_t *l2im = &l2input_main;
208 vl_api_l2fib_add_del_reply_t *rmp;
209 int rv = 0;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100210 u32 bd_id = ntohl (mp->bd_id);
Eyal Bari31a71ab2017-06-25 14:42:33 +0300211 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100212
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100213 if (!p)
214 {
215 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
216 goto bad_sw_if_index;
217 }
Eyal Bari31a71ab2017-06-25 14:42:33 +0300218 u32 bd_index = p[0];
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100219
Jakub Grajciar145e3302019-10-24 13:52:42 +0200220 mac_address_t mac;
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200221
Jakub Grajciar145e3302019-10-24 13:52:42 +0200222 mac_address_decode (mp->mac, &mac);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100223 if (mp->is_add)
224 {
Eyal Bari31a71ab2017-06-25 14:42:33 +0300225 if (mp->filter_mac)
Jakub Grajciar145e3302019-10-24 13:52:42 +0200226 l2fib_add_filter_entry (mac.bytes, bd_index);
Eyal Bari31a71ab2017-06-25 14:42:33 +0300227 else
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100228 {
Neale Rannsb54d0812018-09-06 06:22:56 -0700229 l2fib_entry_result_flags_t flags = L2FIB_ENTRY_RESULT_FLAG_NONE;
Eyal Bari31a71ab2017-06-25 14:42:33 +0300230 u32 sw_if_index = ntohl (mp->sw_if_index);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100231 VALIDATE_SW_IF_INDEX (mp);
232 if (vec_len (l2im->configs) <= sw_if_index)
233 {
234 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
235 goto bad_sw_if_index;
236 }
237 else
238 {
239 l2_input_config_t *config;
240 config = vec_elt_at_index (l2im->configs, sw_if_index);
Neale Ranns47a3d992020-09-29 15:38:51 +0000241 if (!l2_input_is_bridge (config))
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100242 {
243 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
244 goto bad_sw_if_index;
245 }
246 }
Neale Rannsb54d0812018-09-06 06:22:56 -0700247 if (mp->static_mac)
248 flags |= L2FIB_ENTRY_RESULT_FLAG_STATIC;
249 if (mp->bvi_mac)
250 flags |= L2FIB_ENTRY_RESULT_FLAG_BVI;
Jakub Grajciar145e3302019-10-24 13:52:42 +0200251 l2fib_add_entry (mac.bytes, bd_index, sw_if_index, flags);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100252 }
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100253 }
254 else
255 {
John Lo7dbd7262018-05-31 10:25:18 -0400256 u32 sw_if_index = ntohl (mp->sw_if_index);
Jakub Grajciar145e3302019-10-24 13:52:42 +0200257 if (l2fib_del_entry (mac.bytes, bd_index, sw_if_index))
John Lo7dbd7262018-05-31 10:25:18 -0400258 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100259 }
260
261 BAD_SW_IF_INDEX_LABEL;
262
263 REPLY_MACRO (VL_API_L2FIB_ADD_DEL_REPLY);
264}
265
266static void
Jerome Tollet0f8d1002021-01-07 12:44:17 +0100267vl_api_want_l2_macs_events2_t_handler (vl_api_want_l2_macs_events2_t *mp)
John Lo8d00fff2017-08-03 00:35:36 -0400268{
269 int rv = 0;
Jerome Tollet0f8d1002021-01-07 12:44:17 +0100270 vl_api_want_l2_macs_events2_reply_t *rmp;
John Lo8d00fff2017-08-03 00:35:36 -0400271 l2learn_main_t *lm = &l2learn_main;
272 l2fib_main_t *fm = &l2fib_main;
273 u32 pid = ntohl (mp->pid);
John Lo8d00fff2017-08-03 00:35:36 -0400274
275 if (mp->enable_disable)
276 {
Jerome Tollet0f8d1002021-01-07 12:44:17 +0100277 if ((lm->client_pid == 0) || (lm->client_pid == pid))
John Lo8d00fff2017-08-03 00:35:36 -0400278 {
John Lo8d00fff2017-08-03 00:35:36 -0400279 if (mp->max_macs_in_event)
280 fm->max_macs_in_event = mp->max_macs_in_event * 10;
281 else
Jakub Grajciar145e3302019-10-24 13:52:42 +0200282 {
283 rv = VNET_API_ERROR_INVALID_VALUE;
284 goto exit;
285 }
John Lo8d00fff2017-08-03 00:35:36 -0400286
Jerome Tollet0f8d1002021-01-07 12:44:17 +0100287 /* if scan_delay was not set before */
288 if (fm->event_scan_delay == 0.0)
289 fm->event_scan_delay = (f64) (10) * 10e-3;
John Lo8d00fff2017-08-03 00:35:36 -0400290
Jerome Tollet0f8d1002021-01-07 12:44:17 +0100291 lm->client_pid = pid;
292 lm->client_index = mp->client_index;
John Lo8d00fff2017-08-03 00:35:36 -0400293 l2fib_flush_all_mac (vlib_get_main ());
294 }
295 else if (lm->client_pid != pid)
296 {
297 rv = VNET_API_ERROR_L2_MACS_EVENT_CLINET_PRESENT;
298 goto exit;
299 }
300 }
301 else if (lm->client_pid)
302 {
303 lm->client_pid = 0;
304 lm->client_index = 0;
Jerome Tollet0f8d1002021-01-07 12:44:17 +0100305 }
306
307exit:
308 REPLY_MACRO (VL_API_WANT_L2_MACS_EVENTS2_REPLY);
309}
310
311static void
312vl_api_want_l2_macs_events_t_handler (vl_api_want_l2_macs_events_t *mp)
313{
314 int rv = 0;
315 vl_api_want_l2_macs_events_reply_t *rmp;
316 l2learn_main_t *lm = &l2learn_main;
317 l2fib_main_t *fm = &l2fib_main;
318 u32 pid = ntohl (mp->pid);
319 u32 learn_limit = ntohl (mp->learn_limit);
320
321 if (mp->enable_disable)
322 {
323 if ((lm->client_pid == 0) || (lm->client_pid == pid))
324 {
325 if ((mp->max_macs_in_event == 0) || (mp->scan_delay == 0) ||
326 (learn_limit == 0) || (learn_limit > L2LEARN_DEFAULT_LIMIT))
327 {
328 rv = VNET_API_ERROR_INVALID_VALUE;
329 goto exit;
330 }
331 lm->client_pid = pid;
332 lm->client_index = mp->client_index;
333
334 fm->max_macs_in_event = mp->max_macs_in_event * 10;
335 fm->event_scan_delay = (f64) (mp->scan_delay) * 10e-3;
336
337 /* change learn limit and flush all learned MACs */
338 lm->global_learn_limit = learn_limit;
339 l2fib_flush_all_mac (vlib_get_main ());
340 }
341 else if (lm->client_pid != pid)
342 {
343 rv = VNET_API_ERROR_L2_MACS_EVENT_CLINET_PRESENT;
344 goto exit;
345 }
346 }
347 else if (lm->client_pid)
348 {
349 lm->client_pid = 0;
350 lm->client_index = 0;
351 if (learn_limit && (learn_limit <= L2LEARN_DEFAULT_LIMIT))
John Loe531f4c2017-08-22 09:16:50 -0400352 lm->global_learn_limit = learn_limit;
353 else
354 lm->global_learn_limit = L2LEARN_DEFAULT_LIMIT;
John Lo8d00fff2017-08-03 00:35:36 -0400355 }
356
357exit:
358 REPLY_MACRO (VL_API_WANT_L2_MACS_EVENTS_REPLY);
359}
360
361static void
Eyal Barif24991c2017-04-05 05:33:21 +0300362vl_api_l2fib_flush_int_t_handler (vl_api_l2fib_flush_int_t * mp)
363{
364 int rv = 0;
365 vlib_main_t *vm = vlib_get_main ();
366 vl_api_l2fib_flush_int_reply_t *rmp;
367
368 VALIDATE_SW_IF_INDEX (mp);
369
370 u32 sw_if_index = ntohl (mp->sw_if_index);
371 l2fib_flush_int_mac (vm, sw_if_index);
372
373 BAD_SW_IF_INDEX_LABEL;
374 REPLY_MACRO (VL_API_L2FIB_FLUSH_INT_REPLY);
375}
376
377static void
Eyal Bari7537e712017-04-27 14:07:55 +0300378vl_api_l2fib_flush_all_t_handler (vl_api_l2fib_flush_all_t * mp)
379{
380 int rv = 0;
381 vl_api_l2fib_flush_all_reply_t *rmp;
382
383 l2fib_flush_all_mac (vlib_get_main ());
384 REPLY_MACRO (VL_API_L2FIB_FLUSH_ALL_REPLY);
385}
386
387static void
Eyal Barif24991c2017-04-05 05:33:21 +0300388vl_api_l2fib_flush_bd_t_handler (vl_api_l2fib_flush_bd_t * mp)
389{
390 int rv = 0;
391 vlib_main_t *vm = vlib_get_main ();
392 bd_main_t *bdm = &bd_main;
393 vl_api_l2fib_flush_bd_reply_t *rmp;
394
395 u32 bd_id = ntohl (mp->bd_id);
396 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
397 if (p == 0)
398 {
399 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
400 goto out;
401 }
402 l2fib_flush_bd_mac (vm, *p);
403out:
404 REPLY_MACRO (VL_API_L2FIB_FLUSH_BD_REPLY);
405}
406
407static void
Jerome Tollet0f8d1002021-01-07 12:44:17 +0100408vl_api_l2fib_set_scan_delay_t_handler (vl_api_l2fib_set_scan_delay_t *mp)
409{
410 int rv = 0;
411 l2fib_main_t *fm = &l2fib_main;
412 vl_api_l2fib_set_scan_delay_reply_t *rmp;
413 u16 scan_delay = ntohs (mp->scan_delay);
414
415 if (mp->scan_delay)
416 {
417 fm->event_scan_delay = (f64) (scan_delay) *10e-3;
418 l2fib_flush_all_mac (vlib_get_main ());
419 }
420 else
421 {
422 rv = VNET_API_ERROR_INVALID_VALUE;
423 goto exit;
424 }
425
426exit:
427 REPLY_MACRO (VL_API_L2FIB_SET_SCAN_DELAY_REPLY);
428}
429
430static void
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100431vl_api_l2_flags_t_handler (vl_api_l2_flags_t * mp)
432{
433 vl_api_l2_flags_reply_t *rmp;
434 int rv = 0;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100435 u32 rbm = 0;
436
437 VALIDATE_SW_IF_INDEX (mp);
438
Eyal Barifead6702017-04-04 04:46:32 +0300439 u32 sw_if_index = ntohl (mp->sw_if_index);
John Lo8d00fff2017-08-03 00:35:36 -0400440 u32 flags = ntohl (mp->feature_bitmap);
441 u32 bitmap = 0;
442
443 if (flags & L2_LEARN)
444 bitmap |= L2INPUT_FEAT_LEARN;
445
446 if (flags & L2_FWD)
447 bitmap |= L2INPUT_FEAT_FWD;
448
449 if (flags & L2_FLOOD)
450 bitmap |= L2INPUT_FEAT_FLOOD;
451
452 if (flags & L2_UU_FLOOD)
453 bitmap |= L2INPUT_FEAT_UU_FLOOD;
454
455 if (flags & L2_ARP_TERM)
456 bitmap |= L2INPUT_FEAT_ARP_TERM;
457
458 rbm = l2input_intf_bitmap_enable (sw_if_index, bitmap, mp->is_set);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100459
460 BAD_SW_IF_INDEX_LABEL;
461
462 /* *INDENT-OFF* */
463 REPLY_MACRO2(VL_API_L2_FLAGS_REPLY,
464 ({
465 rmp->resulting_feature_bitmap = ntohl(rbm);
466 }));
467 /* *INDENT-ON* */
468}
469
470static void
Jerome Tollet5f93e3b2020-12-18 09:44:24 +0100471vl_api_bridge_domain_set_default_learn_limit_t_handler (
472 vl_api_bridge_domain_set_default_learn_limit_t *mp)
473{
474 vl_api_bridge_domain_set_default_learn_limit_reply_t *rmp;
475 int rv = 0;
476
477 l2learn_main.bd_default_learn_limit = ntohl (mp->learn_limit);
478 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_SET_DEFAULT_LEARN_LIMIT_REPLY);
479}
480
481static void
482vl_api_bridge_domain_set_learn_limit_t_handler (
483 vl_api_bridge_domain_set_learn_limit_t *mp)
484{
485 vlib_main_t *vm = vlib_get_main ();
486 bd_main_t *bdm = &bd_main;
487 vl_api_bridge_domain_set_learn_limit_reply_t *rmp;
488 int rv = 0;
489 u32 bd_id = ntohl (mp->bd_id);
490 uword *p;
491
492 if (bd_id == 0)
493 {
494 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
495 goto out;
496 }
497
498 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
499 if (p == 0)
500 {
501 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
502 goto out;
503 }
504 bd_set_learn_limit (vm, *p, ntohl (mp->learn_limit));
505out:
506 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_SET_LEARN_LIMIT_REPLY);
507}
508
509static void
Eyal Barifead6702017-04-04 04:46:32 +0300510vl_api_bridge_domain_set_mac_age_t_handler (vl_api_bridge_domain_set_mac_age_t
511 * mp)
512{
513 vlib_main_t *vm = vlib_get_main ();
514 bd_main_t *bdm = &bd_main;
515 vl_api_bridge_domain_set_mac_age_reply_t *rmp;
516 int rv = 0;
517 u32 bd_id = ntohl (mp->bd_id);
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500518 uword *p;
519
520 if (bd_id == 0)
521 {
522 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
523 goto out;
524 }
525
526 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
Eyal Barifead6702017-04-04 04:46:32 +0300527 if (p == 0)
528 {
529 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
530 goto out;
531 }
532 bd_set_mac_age (vm, *p, mp->mac_age);
533out:
534 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_SET_MAC_AGE_REPLY);
535}
536
537static void
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100538vl_api_bridge_domain_add_del_t_handler (vl_api_bridge_domain_add_del_t * mp)
539{
Eyal Barib1352ed2017-04-07 23:14:17 +0300540 l2_bridge_domain_add_del_args_t a = {
541 .is_add = mp->is_add,
542 .flood = mp->flood,
543 .uu_flood = mp->uu_flood,
544 .forward = mp->forward,
545 .learn = mp->learn,
546 .arp_term = mp->arp_term,
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +0200547 .arp_ufwd = mp->arp_ufwd,
Eyal Barib1352ed2017-04-07 23:14:17 +0300548 .mac_age = mp->mac_age,
549 .bd_id = ntohl (mp->bd_id),
Jerome Tollet48304142017-09-05 12:13:22 +0100550 .bd_tag = mp->bd_tag
Eyal Barib1352ed2017-04-07 23:14:17 +0300551 };
552
553 int rv = bd_add_del (&a);
554
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100555 vl_api_bridge_domain_add_del_reply_t *rmp;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100556 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_ADD_DEL_REPLY);
557}
558
559static void
Ole Troan01384fe2017-05-12 11:55:35 +0200560send_bridge_domain_details (l2input_main_t * l2im,
Florin Coras6c4dae22018-01-09 06:39:23 -0800561 vl_api_registration_t * reg,
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100562 l2_bridge_domain_t * bd_config,
563 u32 n_sw_ifs, u32 context)
564{
565 vl_api_bridge_domain_details_t *mp;
Ole Troan01384fe2017-05-12 11:55:35 +0200566 l2_flood_member_t *m;
567 vl_api_bridge_domain_sw_if_t *sw_ifs;
568 l2_input_config_t *input_cfg;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100569
Ole Troan01384fe2017-05-12 11:55:35 +0200570 mp = vl_msg_api_alloc (sizeof (*mp) +
571 (n_sw_ifs * sizeof (vl_api_bridge_domain_sw_if_t)));
Dave Barachb7b92992018-10-17 10:38:51 -0400572 clib_memset (mp, 0, sizeof (*mp));
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100573 mp->_vl_msg_id = ntohs (VL_API_BRIDGE_DOMAIN_DETAILS);
574 mp->bd_id = ntohl (bd_config->bd_id);
575 mp->flood = bd_feature_flood (bd_config);
576 mp->uu_flood = bd_feature_uu_flood (bd_config);
577 mp->forward = bd_feature_forward (bd_config);
578 mp->learn = bd_feature_learn (bd_config);
579 mp->arp_term = bd_feature_arp_term (bd_config);
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +0200580 mp->arp_ufwd = bd_feature_arp_ufwd (bd_config);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100581 mp->bvi_sw_if_index = ntohl (bd_config->bvi_sw_if_index);
Neale Rannsb4743802018-09-05 09:13:57 -0700582 mp->uu_fwd_sw_if_index = ntohl (bd_config->uu_fwd_sw_if_index);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100583 mp->mac_age = bd_config->mac_age;
Jerome Tollet48304142017-09-05 12:13:22 +0100584 if (bd_config->bd_tag)
585 {
586 strncpy ((char *) mp->bd_tag, (char *) bd_config->bd_tag,
587 ARRAY_LEN (mp->bd_tag) - 1);
588 mp->bd_tag[ARRAY_LEN (mp->bd_tag) - 1] = 0;
589 }
590
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100591 mp->context = context;
592
Ole Troan01384fe2017-05-12 11:55:35 +0200593 sw_ifs = (vl_api_bridge_domain_sw_if_t *) mp->sw_if_details;
594 vec_foreach (m, bd_config->members)
595 {
596 sw_ifs->sw_if_index = ntohl (m->sw_if_index);
597 input_cfg = vec_elt_at_index (l2im->configs, m->sw_if_index);
598 sw_ifs->shg = input_cfg->shg;
599 sw_ifs++;
600 mp->n_sw_ifs++;
601 }
602 mp->n_sw_ifs = htonl (mp->n_sw_ifs);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100603
Florin Coras6c4dae22018-01-09 06:39:23 -0800604 vl_api_send_msg (reg, (u8 *) mp);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100605}
606
607static void
608vl_api_bridge_domain_dump_t_handler (vl_api_bridge_domain_dump_t * mp)
609{
610 bd_main_t *bdm = &bd_main;
611 l2input_main_t *l2im = &l2input_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800612 vl_api_registration_t *reg;
Jakub Grajciar145e3302019-10-24 13:52:42 +0200613 u32 bd_id, bd_index, end, filter_sw_if_index;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100614
Florin Coras6c4dae22018-01-09 06:39:23 -0800615 reg = vl_api_client_index_to_registration (mp->client_index);
616 if (!reg)
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100617 return;
618
Jakub Grajciar145e3302019-10-24 13:52:42 +0200619 filter_sw_if_index = ntohl (mp->sw_if_index);
620 if (filter_sw_if_index != ~0)
621 return; /* UNIMPLEMENTED */
622
Florin Coras6c4dae22018-01-09 06:39:23 -0800623 bd_id = ntohl (mp->bd_id);
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500624 if (bd_id == 0)
625 return;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100626
Eyal Bari259fca72017-05-14 10:38:39 +0300627 if (bd_id == ~0)
628 bd_index = 0, end = vec_len (l2im->bd_configs);
629 else
630 {
631 bd_index = bd_find_index (bdm, bd_id);
632 if (bd_index == ~0)
633 return;
634
635 end = bd_index + 1;
636 }
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500637
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100638 for (; bd_index < end; bd_index++)
639 {
Eyal Bari259fca72017-05-14 10:38:39 +0300640 l2_bridge_domain_t *bd_config =
641 l2input_bd_config_from_index (l2im, bd_index);
Dave Barach11fb09e2020-08-06 12:10:09 -0400642 /* skip placeholder bd_id 0 */
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100643 if (bd_config && (bd_config->bd_id > 0))
Florin Coras6c4dae22018-01-09 06:39:23 -0800644 send_bridge_domain_details (l2im, reg, bd_config,
Ole Troan01384fe2017-05-12 11:55:35 +0200645 vec_len (bd_config->members),
646 mp->context);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100647 }
648}
649
Neale Rannsb4743802018-09-05 09:13:57 -0700650static bd_flags_t
651bd_flags_decode (vl_api_bd_flags_t v)
652{
653 bd_flags_t f = L2_NONE;
654
655 v = ntohl (v);
656
657 if (v & BRIDGE_API_FLAG_LEARN)
658 f |= L2_LEARN;
659 if (v & BRIDGE_API_FLAG_FWD)
660 f |= L2_FWD;
661 if (v & BRIDGE_API_FLAG_FLOOD)
662 f |= L2_FLOOD;
663 if (v & BRIDGE_API_FLAG_UU_FLOOD)
664 f |= L2_UU_FLOOD;
665 if (v & BRIDGE_API_FLAG_ARP_TERM)
666 f |= L2_ARP_TERM;
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +0200667 if (v & BRIDGE_API_FLAG_ARP_UFWD)
668 f |= L2_ARP_UFWD;
Neale Rannsb4743802018-09-05 09:13:57 -0700669
670 return (f);
671}
672
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100673static void
674vl_api_bridge_flags_t_handler (vl_api_bridge_flags_t * mp)
675{
676 vlib_main_t *vm = vlib_get_main ();
677 bd_main_t *bdm = &bd_main;
678 vl_api_bridge_flags_reply_t *rmp;
Dave Barach98d6b612019-01-03 09:30:00 -0500679 u32 bitmap = 0;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100680 int rv = 0;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100681
Neale Rannsb4743802018-09-05 09:13:57 -0700682 bd_flags_t flags = bd_flags_decode (mp->flags);
Eyal Bari259fca72017-05-14 10:38:39 +0300683 u32 bd_id = ntohl (mp->bd_id);
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500684 if (bd_id == 0)
685 {
686 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
687 goto out;
688 }
689
Eyal Bari259fca72017-05-14 10:38:39 +0300690 u32 bd_index = bd_find_index (bdm, bd_id);
691 if (bd_index == ~0)
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100692 {
693 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
694 goto out;
695 }
696
Dave Barach98d6b612019-01-03 09:30:00 -0500697 bitmap = bd_set_flags (vm, bd_index, flags, mp->is_set);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100698
699out:
700 /* *INDENT-OFF* */
701 REPLY_MACRO2(VL_API_BRIDGE_FLAGS_REPLY,
702 ({
John Lo8d00fff2017-08-03 00:35:36 -0400703 rmp->resulting_feature_bitmap = ntohl(bitmap);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100704 }));
705 /* *INDENT-ON* */
706}
Matus Fabianf468e232016-12-02 06:00:53 -0800707
Pavel Kotucekadec5872017-01-25 08:50:53 +0100708static void
709 vl_api_l2_interface_vlan_tag_rewrite_t_handler
710 (vl_api_l2_interface_vlan_tag_rewrite_t * mp)
711{
712 int rv = 0;
713 vl_api_l2_interface_vlan_tag_rewrite_reply_t *rmp;
714 vnet_main_t *vnm = vnet_get_main ();
715 vlib_main_t *vm = vlib_get_main ();
716 u32 vtr_op;
717
718 VALIDATE_SW_IF_INDEX (mp);
719
720 vtr_op = ntohl (mp->vtr_op);
721
722 /* The L2 code is unsuspicious */
723 switch (vtr_op)
724 {
725 case L2_VTR_DISABLED:
726 case L2_VTR_PUSH_1:
727 case L2_VTR_PUSH_2:
728 case L2_VTR_POP_1:
729 case L2_VTR_POP_2:
730 case L2_VTR_TRANSLATE_1_1:
731 case L2_VTR_TRANSLATE_1_2:
732 case L2_VTR_TRANSLATE_2_1:
733 case L2_VTR_TRANSLATE_2_2:
734 break;
735
736 default:
737 rv = VNET_API_ERROR_INVALID_VALUE;
738 goto bad_sw_if_index;
739 }
740
741 rv = l2vtr_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
742 ntohl (mp->push_dot1q), ntohl (mp->tag1),
743 ntohl (mp->tag2));
744
745 BAD_SW_IF_INDEX_LABEL;
746
747 REPLY_MACRO (VL_API_L2_INTERFACE_VLAN_TAG_REWRITE_REPLY);
748}
749
750static void
751 vl_api_l2_interface_pbb_tag_rewrite_t_handler
752 (vl_api_l2_interface_pbb_tag_rewrite_t * mp)
753{
754 vl_api_l2_interface_pbb_tag_rewrite_reply_t *rmp;
755 vnet_main_t *vnm = vnet_get_main ();
756 vlib_main_t *vm = vlib_get_main ();
757 u32 vtr_op;
758 int rv = 0;
Jakub Grajciar145e3302019-10-24 13:52:42 +0200759 mac_address_t b_dmac, b_smac;
Pavel Kotucekadec5872017-01-25 08:50:53 +0100760
761 VALIDATE_SW_IF_INDEX (mp);
762
763 vtr_op = ntohl (mp->vtr_op);
764
765 switch (vtr_op)
766 {
767 case L2_VTR_DISABLED:
768 case L2_VTR_PUSH_2:
769 case L2_VTR_POP_2:
770 case L2_VTR_TRANSLATE_2_1:
771 break;
772
773 default:
774 rv = VNET_API_ERROR_INVALID_VALUE;
775 goto bad_sw_if_index;
776 }
777
Jakub Grajciar145e3302019-10-24 13:52:42 +0200778 mac_address_decode (mp->b_dmac, &b_dmac);
779 mac_address_decode (mp->b_smac, &b_smac);
780
Pavel Kotucekadec5872017-01-25 08:50:53 +0100781 rv = l2pbb_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
Jakub Grajciar145e3302019-10-24 13:52:42 +0200782 b_dmac.bytes, b_smac.bytes, ntohs (mp->b_vlanid),
Pavel Kotucekadec5872017-01-25 08:50:53 +0100783 ntohl (mp->i_sid), ntohs (mp->outer_tag));
784
785 BAD_SW_IF_INDEX_LABEL;
786
787 REPLY_MACRO (VL_API_L2_INTERFACE_PBB_TAG_REWRITE_REPLY);
788}
789
Neale Rannsb8d44812017-11-10 06:53:54 -0800790static void
791 vl_api_sw_interface_set_l2_xconnect_t_handler
792 (vl_api_sw_interface_set_l2_xconnect_t * mp)
793{
794 vl_api_sw_interface_set_l2_xconnect_reply_t *rmp;
795 int rv = 0;
796 u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
797 u32 tx_sw_if_index = ntohl (mp->tx_sw_if_index);
798 vlib_main_t *vm = vlib_get_main ();
799 vnet_main_t *vnm = vnet_get_main ();
800
801 VALIDATE_RX_SW_IF_INDEX (mp);
802
803 if (mp->enable)
804 {
805 VALIDATE_TX_SW_IF_INDEX (mp);
806 rv = set_int_l2_mode (vm, vnm, MODE_L2_XC,
Neale Rannsb4743802018-09-05 09:13:57 -0700807 rx_sw_if_index, 0,
808 L2_BD_PORT_TYPE_NORMAL, 0, tx_sw_if_index);
Neale Rannsb8d44812017-11-10 06:53:54 -0800809 }
810 else
811 {
Neale Rannsb4743802018-09-05 09:13:57 -0700812 rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0,
813 L2_BD_PORT_TYPE_NORMAL, 0, 0);
Neale Rannsb8d44812017-11-10 06:53:54 -0800814 }
815
Alexander Chernavine178b272018-09-24 05:42:01 -0400816 switch (rv)
817 {
818 case MODE_ERROR_ETH:
819 rv = VNET_API_ERROR_NON_ETHERNET;
820 break;
821 case MODE_ERROR_BVI_DEF:
822 rv = VNET_API_ERROR_BD_ALREADY_HAS_BVI;
823 break;
824 }
825
Neale Rannsb8d44812017-11-10 06:53:54 -0800826 BAD_RX_SW_IF_INDEX_LABEL;
827 BAD_TX_SW_IF_INDEX_LABEL;
828
829 REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_XCONNECT_REPLY);
830}
831
Neale Rannsb4743802018-09-05 09:13:57 -0700832static int
833l2_bd_port_type_decode (vl_api_l2_port_type_t v, l2_bd_port_type_t * l)
834{
835 v = clib_net_to_host_u32 (v);
836
837 switch (v)
838 {
839 case L2_API_PORT_TYPE_NORMAL:
840 *l = L2_BD_PORT_TYPE_NORMAL;
841 return 0;
842 case L2_API_PORT_TYPE_BVI:
843 *l = L2_BD_PORT_TYPE_BVI;
844 return 0;
845 case L2_API_PORT_TYPE_UU_FWD:
846 *l = L2_BD_PORT_TYPE_UU_FWD;
847 return 0;
848 }
849
850 return (VNET_API_ERROR_INVALID_VALUE);
851}
852
Neale Rannsb8d44812017-11-10 06:53:54 -0800853static void
854 vl_api_sw_interface_set_l2_bridge_t_handler
855 (vl_api_sw_interface_set_l2_bridge_t * mp)
856{
857 bd_main_t *bdm = &bd_main;
858 vl_api_sw_interface_set_l2_bridge_reply_t *rmp;
859 int rv = 0;
860 vlib_main_t *vm = vlib_get_main ();
861 vnet_main_t *vnm = vnet_get_main ();
Neale Rannsb4743802018-09-05 09:13:57 -0700862 l2_bd_port_type_t pt;
Neale Rannsb8d44812017-11-10 06:53:54 -0800863
864 VALIDATE_RX_SW_IF_INDEX (mp);
865 u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
Neale Rannsb4743802018-09-05 09:13:57 -0700866 rv = l2_bd_port_type_decode (mp->port_type, &pt);
Neale Rannsb8d44812017-11-10 06:53:54 -0800867
Neale Rannsb4743802018-09-05 09:13:57 -0700868 if (0 != rv)
869 goto out;
Neale Rannsb8d44812017-11-10 06:53:54 -0800870 if (mp->enable)
871 {
872 VALIDATE_BD_ID (mp);
873 u32 bd_id = ntohl (mp->bd_id);
874 u32 bd_index = bd_find_or_add_bd_index (bdm, bd_id);
Neale Rannsb4743802018-09-05 09:13:57 -0700875
Neale Rannsb8d44812017-11-10 06:53:54 -0800876 rv = set_int_l2_mode (vm, vnm, MODE_L2_BRIDGE,
Neale Rannsb4743802018-09-05 09:13:57 -0700877 rx_sw_if_index, bd_index, pt, mp->shg, 0);
Neale Rannsb8d44812017-11-10 06:53:54 -0800878 }
879 else
880 {
Neale Rannsb4743802018-09-05 09:13:57 -0700881 rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0, pt, 0, 0);
Neale Rannsb8d44812017-11-10 06:53:54 -0800882 }
883
Alexander Chernavine178b272018-09-24 05:42:01 -0400884 switch (rv)
885 {
886 case MODE_ERROR_ETH:
887 rv = VNET_API_ERROR_NON_ETHERNET;
888 break;
889 case MODE_ERROR_BVI_DEF:
890 rv = VNET_API_ERROR_BD_ALREADY_HAS_BVI;
891 break;
892 }
893
Neale Rannsb8d44812017-11-10 06:53:54 -0800894 BAD_RX_SW_IF_INDEX_LABEL;
895 BAD_BD_ID_LABEL;
Neale Rannsb4743802018-09-05 09:13:57 -0700896out:
Neale Rannsb8d44812017-11-10 06:53:54 -0800897 REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_BRIDGE_REPLY);
898}
899
900static void
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200901send_bd_ip_mac_entry (vpe_api_main_t * am,
902 vl_api_registration_t * reg,
Neale Rannsbc764c82019-06-19 07:07:13 -0700903 u32 bd_id,
904 const ip46_address_t * ip,
905 ip46_type_t itype,
906 const mac_address_t * mac, u32 context)
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200907{
908 vl_api_bd_ip_mac_details_t *mp;
909
910 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400911 clib_memset (mp, 0, sizeof (*mp));
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200912 mp->_vl_msg_id = ntohs (VL_API_BD_IP_MAC_DETAILS);
913
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200914 mp->context = context;
Neale Rannsbc764c82019-06-19 07:07:13 -0700915 mp->entry.bd_id = ntohl (bd_id);
916
917 ip_address_encode (ip, itype, &mp->entry.ip);
918 mac_address_encode (mac, mp->entry.mac);
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200919
920 vl_api_send_msg (reg, (u8 *) mp);
921}
922
923static void
924vl_api_bd_ip_mac_dump_t_handler (vl_api_bd_ip_mac_dump_t * mp)
925{
926 vpe_api_main_t *am = &vpe_api_main;
927 bd_main_t *bdm = &bd_main;
928 l2_bridge_domain_t *bd_config;
929 u32 bd_id = ntohl (mp->bd_id);
Mohsin Kazmib24dfec2018-08-23 12:24:19 +0200930 u32 bd_index, start, end;
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200931 vl_api_registration_t *reg;
932 uword *p;
933
934 reg = vl_api_client_index_to_registration (mp->client_index);
935 if (!reg)
936 return;
937
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200938 /* see bd_id: ~0 means "any" */
939 if (bd_id == ~0)
Mohsin Kazmib24dfec2018-08-23 12:24:19 +0200940 {
941 start = 1;
942 end = vec_len (l2input_main.bd_configs);
943 }
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200944 else
945 {
946 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
947 if (p == 0)
948 return;
949
950 bd_index = p[0];
951 vec_validate (l2input_main.bd_configs, bd_index);
952 start = bd_index;
953 end = start + 1;
954 }
955
956 for (bd_index = start; bd_index < end; bd_index++)
957 {
958 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
959 if (bd_is_valid (bd_config))
960 {
961 ip4_address_t ip4_addr;
962 ip6_address_t *ip6_addr;
Neale Rannsbc764c82019-06-19 07:07:13 -0700963 mac_address_t mac;
964 u64 mac64;
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200965 bd_id = bd_config->bd_id;
966
967 /* *INDENT-OFF* */
Neale Rannsbc764c82019-06-19 07:07:13 -0700968 hash_foreach (ip4_addr.as_u32, mac64, bd_config->mac_by_ip4,
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200969 ({
Neale Rannsbc764c82019-06-19 07:07:13 -0700970 ip46_address_t ip = {
971 .ip4 = ip4_addr,
972 };
973 mac_address_from_u64(&mac, mac64);
974
975 send_bd_ip_mac_entry (am, reg, bd_id, &ip, IP46_TYPE_IP4,
976 &mac, mp->context);
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200977 }));
978
Neale Rannsbc764c82019-06-19 07:07:13 -0700979 hash_foreach_mem (ip6_addr, mac64, bd_config->mac_by_ip6,
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200980 ({
Neale Rannsbc764c82019-06-19 07:07:13 -0700981 ip46_address_t ip = {
982 .ip6 = *ip6_addr,
983 };
984 mac_address_from_u64(&mac, mac64);
985
986 send_bd_ip_mac_entry (am, reg, bd_id, &ip, IP46_TYPE_IP6,
987 &mac, mp->context);
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200988 }));
989 /* *INDENT-ON* */
990 }
991 }
992}
993
994static void
Neale Rannsb8d44812017-11-10 06:53:54 -0800995vl_api_bd_ip_mac_add_del_t_handler (vl_api_bd_ip_mac_add_del_t * mp)
996{
Neale Ranns4d5b9172018-10-24 02:57:49 -0700997 ip46_address_t ip_addr = ip46_address_initializer;
Neale Rannsb8d44812017-11-10 06:53:54 -0800998 vl_api_bd_ip_mac_add_del_reply_t *rmp;
Neale Ranns4d5b9172018-10-24 02:57:49 -0700999 bd_main_t *bdm = &bd_main;
1000 u32 bd_index, bd_id;
1001 mac_address_t mac;
1002 ip46_type_t type;
Neale Rannsb8d44812017-11-10 06:53:54 -08001003 int rv = 0;
Neale Rannsb8d44812017-11-10 06:53:54 -08001004 uword *p;
1005
Neale Rannsbc764c82019-06-19 07:07:13 -07001006 bd_id = ntohl (mp->entry.bd_id);
Neale Ranns4d5b9172018-10-24 02:57:49 -07001007
Neale Rannsb8d44812017-11-10 06:53:54 -08001008 if (bd_id == 0)
1009 {
1010 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
1011 goto out;
1012 }
1013
1014 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
1015 if (p == 0)
1016 {
1017 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1018 goto out;
1019 }
Neale Rannsb8d44812017-11-10 06:53:54 -08001020 bd_index = p[0];
Neale Ranns4d5b9172018-10-24 02:57:49 -07001021
Neale Rannsbc764c82019-06-19 07:07:13 -07001022 type = ip_address_decode (&mp->entry.ip, &ip_addr);
1023 mac_address_decode (mp->entry.mac, &mac);
Neale Ranns4d5b9172018-10-24 02:57:49 -07001024
1025 if (bd_add_del_ip_mac (bd_index, type, &ip_addr, &mac, mp->is_add))
Neale Rannsb8d44812017-11-10 06:53:54 -08001026 rv = VNET_API_ERROR_UNSPECIFIED;
1027
1028out:
1029 REPLY_MACRO (VL_API_BD_IP_MAC_ADD_DEL_REPLY);
1030}
1031
John Loe26c81f2019-01-07 15:16:33 -05001032static void
1033vl_api_bd_ip_mac_flush_t_handler (vl_api_bd_ip_mac_flush_t * mp)
1034{
1035 vl_api_bd_ip_mac_flush_reply_t *rmp;
1036 bd_main_t *bdm = &bd_main;
1037 u32 bd_index, bd_id;
1038 int rv = 0;
1039 uword *p;
1040
1041 bd_id = ntohl (mp->bd_id);
1042
1043 if (bd_id == 0)
1044 {
1045 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
1046 goto out;
1047 }
1048
1049 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
1050 if (p == 0)
1051 {
1052 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1053 goto out;
1054 }
1055 bd_index = p[0];
1056
1057 bd_flush_ip_mac (bd_index);
1058
1059out:
1060 REPLY_MACRO (VL_API_BD_IP_MAC_FLUSH_REPLY);
1061}
1062
Neale Rannsb8d44812017-11-10 06:53:54 -08001063extern void l2_efp_filter_configure (vnet_main_t * vnet_main,
Neale Ranns6b9b41c2018-07-23 05:47:09 -04001064 u32 sw_if_index, u8 enable);
Neale Rannsb8d44812017-11-10 06:53:54 -08001065
1066static void
1067vl_api_l2_interface_efp_filter_t_handler (vl_api_l2_interface_efp_filter_t *
1068 mp)
1069{
1070 int rv;
1071 vl_api_l2_interface_efp_filter_reply_t *rmp;
1072 vnet_main_t *vnm = vnet_get_main ();
1073
Neale Ranns6b9b41c2018-07-23 05:47:09 -04001074 VALIDATE_SW_IF_INDEX (mp);
1075
Neale Rannsb8d44812017-11-10 06:53:54 -08001076 // enable/disable the feature
Neale Ranns6b9b41c2018-07-23 05:47:09 -04001077 l2_efp_filter_configure (vnm, ntohl (mp->sw_if_index), mp->enable_disable);
Neale Rannsb8d44812017-11-10 06:53:54 -08001078 rv = vnm->api_errno;
1079
Neale Ranns6b9b41c2018-07-23 05:47:09 -04001080 BAD_SW_IF_INDEX_LABEL;
Neale Rannsb8d44812017-11-10 06:53:54 -08001081 REPLY_MACRO (VL_API_L2_INTERFACE_EFP_FILTER_REPLY);
1082}
1083
1084static void
1085vl_api_l2_patch_add_del_t_handler (vl_api_l2_patch_add_del_t * mp)
1086{
1087 extern int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
1088 int is_add);
1089 vl_api_l2_patch_add_del_reply_t *rmp;
1090 int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
1091 int is_add);
1092 int rv = 0;
1093
1094 VALIDATE_RX_SW_IF_INDEX (mp);
1095 VALIDATE_TX_SW_IF_INDEX (mp);
1096
1097 rv = vnet_l2_patch_add_del (ntohl (mp->rx_sw_if_index),
1098 ntohl (mp->tx_sw_if_index),
1099 (int) (mp->is_add != 0));
1100
1101 BAD_RX_SW_IF_INDEX_LABEL;
1102 BAD_TX_SW_IF_INDEX_LABEL;
1103
1104 REPLY_MACRO (VL_API_L2_PATCH_ADD_DEL_REPLY);
1105}
1106
1107static void
1108vl_api_sw_interface_set_vpath_t_handler (vl_api_sw_interface_set_vpath_t * mp)
1109{
1110 vl_api_sw_interface_set_vpath_reply_t *rmp;
1111 int rv = 0;
1112 u32 sw_if_index = ntohl (mp->sw_if_index);
1113
1114 VALIDATE_SW_IF_INDEX (mp);
1115
1116 l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_VPATH, mp->enable);
1117 vnet_feature_enable_disable ("ip4-unicast", "vpath-input-ip4",
1118 sw_if_index, mp->enable, 0, 0);
1119 vnet_feature_enable_disable ("ip4-multicast", "vpath-input-ip4",
1120 sw_if_index, mp->enable, 0, 0);
1121 vnet_feature_enable_disable ("ip6-unicast", "vpath-input-ip6",
1122 sw_if_index, mp->enable, 0, 0);
1123 vnet_feature_enable_disable ("ip6-multicast", "vpath-input-ip6",
1124 sw_if_index, mp->enable, 0, 0);
1125
1126 BAD_SW_IF_INDEX_LABEL;
1127
1128 REPLY_MACRO (VL_API_SW_INTERFACE_SET_VPATH_REPLY);
1129}
1130
Neale Ranns192b13f2019-03-15 02:16:20 -07001131static void
1132vl_api_bvi_create_t_handler (vl_api_bvi_create_t * mp)
1133{
1134 vl_api_bvi_create_reply_t *rmp;
1135 mac_address_t mac;
1136 u32 sw_if_index;
1137 int rv;
1138
1139 mac_address_decode (mp->mac, &mac);
1140
1141 rv = l2_bvi_create (ntohl (mp->user_instance), &mac, &sw_if_index);
1142
1143 /* *INDENT-OFF* */
1144 REPLY_MACRO2(VL_API_BVI_CREATE_REPLY,
1145 ({
1146 rmp->sw_if_index = ntohl (sw_if_index);
1147 }));
1148 /* *INDENT-ON* */
1149}
1150
1151static void
1152vl_api_bvi_delete_t_handler (vl_api_bvi_delete_t * mp)
1153{
1154 vl_api_bvi_delete_reply_t *rmp;
1155 int rv;
1156
1157 rv = l2_bvi_delete (ntohl (mp->sw_if_index));
1158
1159 REPLY_MACRO (VL_API_BVI_DELETE_REPLY);
1160}
1161
Neale Rannscbe25aa2019-09-30 10:53:31 +00001162static bool
1163l2_arp_term_publish_event_is_equal (const l2_arp_term_publish_event_t * e1,
1164 const l2_arp_term_publish_event_t * e2)
1165{
1166 if (e1 == NULL || e2 == NULL)
1167 return false;
1168 return (ip46_address_is_equal (&e1->ip, &e2->ip) &&
1169 (e1->sw_if_index == e2->sw_if_index) &&
1170 (mac_address_equal (&e1->mac, &e2->mac)));
1171}
1172
1173static uword
1174l2_arp_term_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1175 vlib_frame_t * f)
1176{
1177 /* These cross the longjmp boundary (vlib_process_wait_for_event)
1178 * and need to be volatile - to prevent them from being optimized into
1179 * a register - which could change during suspension */
1180 volatile f64 last = vlib_time_now (vm);
1181 volatile l2_arp_term_publish_event_t last_event = { };
1182
1183 l2_arp_term_main_t *l2am = &l2_arp_term_main;
1184
1185 while (1)
1186 {
1187 uword event_type = L2_ARP_TERM_EVENT_PUBLISH;
1188 vpe_client_registration_t *reg;
1189 f64 now;
1190
1191 vlib_process_wait_for_event (vm);
1192
1193 vlib_process_get_event_data (vm, &event_type);
1194 now = vlib_time_now (vm);
1195
1196 if (event_type == L2_ARP_TERM_EVENT_PUBLISH)
1197 {
1198 l2_arp_term_publish_event_t *event;
1199
1200 vec_foreach (event, l2am->publish_events)
1201 {
1202 /* dampen duplicate events - cast away volatile */
1203 if (l2_arp_term_publish_event_is_equal
1204 (event, (l2_arp_term_publish_event_t *) & last_event) &&
1205 (now - last) < 10.0)
1206 {
1207 continue;
1208 }
1209 last_event = *event;
1210 last = now;
1211
1212 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001213 pool_foreach (reg, vpe_api_main.l2_arp_term_events_registrations)
1214 {
Neale Rannscbe25aa2019-09-30 10:53:31 +00001215 vl_api_registration_t *vl_reg;
1216 vl_reg = vl_api_client_index_to_registration (reg->client_index);
Dave Barachffd15462020-02-18 10:12:23 -05001217 ALWAYS_ASSERT (vl_reg != NULL);
Neale Rannscbe25aa2019-09-30 10:53:31 +00001218
1219 if (reg && vl_api_can_send_msg (vl_reg))
1220 {
1221 vl_api_l2_arp_term_event_t * vevent;
1222 vevent = vl_msg_api_alloc (sizeof *vevent);
1223 clib_memset (vevent, 0, sizeof *vevent);
1224 vevent->_vl_msg_id = htons (VL_API_L2_ARP_TERM_EVENT);
1225 vevent->client_index = reg->client_index;
1226 vevent->pid = reg->client_pid;
1227 ip_address_encode(&event->ip,
1228 event->type,
1229 &vevent->ip);
1230 vevent->sw_if_index = htonl(event->sw_if_index);
1231 mac_address_encode(&event->mac, vevent->mac);
1232 vl_api_send_msg (vl_reg, (u8 *) vevent);
1233 }
Damjan Marionb2c31b62020-12-13 21:47:40 +01001234 }
Neale Rannscbe25aa2019-09-30 10:53:31 +00001235 /* *INDENT-ON* */
1236 }
1237 vec_reset_length (l2am->publish_events);
1238 }
1239 }
1240
1241 return 0;
1242}
1243
1244/* *INDENT-OFF* */
1245VLIB_REGISTER_NODE (l2_arp_term_process_node) = {
1246 .function = l2_arp_term_process,
1247 .type = VLIB_NODE_TYPE_PROCESS,
1248 .name = "l2-arp-term-publisher",
1249};
1250/* *INDENT-ON* */
1251
1252static void
1253vl_api_want_l2_arp_term_events_t_handler (vl_api_want_l2_arp_term_events_t *
1254 mp)
1255{
1256 vl_api_want_l2_arp_term_events_reply_t *rmp;
1257 vpe_api_main_t *am = &vpe_api_main;
1258 vpe_client_registration_t *rp;
1259 int rv = 0;
1260 uword *p;
1261
1262 p = hash_get (am->l2_arp_term_events_registration_hash, mp->client_index);
1263
1264 if (p)
1265 {
1266 if (mp->enable)
1267 {
1268 clib_warning ("pid %d: already enabled...", mp->pid);
1269 rv = VNET_API_ERROR_INVALID_REGISTRATION;
1270 goto reply;
1271 }
1272 else
1273 {
1274 rp = pool_elt_at_index (am->l2_arp_term_events_registrations, p[0]);
1275 pool_put (am->l2_arp_term_events_registrations, rp);
1276 hash_unset (am->l2_arp_term_events_registration_hash,
1277 mp->client_index);
1278 if (pool_elts (am->l2_arp_term_events_registrations) == 0)
1279 l2_arp_term_set_publisher_node (false);
1280 goto reply;
1281 }
1282 }
1283 if (mp->enable == 0)
1284 {
1285 clib_warning ("pid %d: already disabled...", mp->pid);
1286 rv = VNET_API_ERROR_INVALID_REGISTRATION;
1287 goto reply;
1288 }
1289 pool_get (am->l2_arp_term_events_registrations, rp);
1290 rp->client_index = mp->client_index;
1291 rp->client_pid = mp->pid;
1292 hash_set (am->l2_arp_term_events_registration_hash, rp->client_index,
1293 rp - am->l2_arp_term_events_registrations);
1294 l2_arp_term_set_publisher_node (true);
1295
1296reply:
1297 REPLY_MACRO (VL_API_WANT_L2_ARP_TERM_EVENTS_REPLY);
1298}
1299
1300static clib_error_t *
1301want_l2_arp_term_events_reaper (u32 client_index)
1302{
1303 vpe_client_registration_t *rp;
1304 vpe_api_main_t *am;
1305 uword *p;
1306
1307 am = &vpe_api_main;
1308
1309 /* remove from the registration hash */
1310 p = hash_get (am->l2_arp_term_events_registration_hash, client_index);
1311
1312 if (p)
1313 {
1314 rp = pool_elt_at_index (am->l2_arp_term_events_registrations, p[0]);
1315 pool_put (am->l2_arp_term_events_registrations, rp);
1316 hash_unset (am->l2_arp_term_events_registration_hash, client_index);
1317 if (pool_elts (am->l2_arp_term_events_registrations) == 0)
1318 l2_arp_term_set_publisher_node (false);
1319 }
1320 return (NULL);
1321}
1322
1323VL_MSG_API_REAPER_FUNCTION (want_l2_arp_term_events_reaper);
1324
Matus Fabianf468e232016-12-02 06:00:53 -08001325/*
Pavel Kotucek0f971d82017-01-03 10:48:54 +01001326 * l2_api_hookup
Matus Fabianf468e232016-12-02 06:00:53 -08001327 * Add vpe's API message handlers to the table.
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -07001328 * vlib has already mapped shared memory and
Matus Fabianf468e232016-12-02 06:00:53 -08001329 * added the client registration handlers.
1330 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
1331 */
1332#define vl_msg_name_crc_list
1333#include <vnet/vnet_all_api_h.h>
1334#undef vl_msg_name_crc_list
1335
1336static void
1337setup_message_id_table (api_main_t * am)
1338{
1339#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1340 foreach_vl_msg_name_crc_l2;
1341#undef _
1342}
1343
1344static clib_error_t *
1345l2_api_hookup (vlib_main_t * vm)
1346{
Dave Barach39d69112019-11-27 11:42:13 -05001347 api_main_t *am = vlibapi_get_main ();
Matus Fabianf468e232016-12-02 06:00:53 -08001348
1349#define _(N,n) \
1350 vl_msg_api_set_handlers(VL_API_##N, #n, \
1351 vl_api_##n##_t_handler, \
1352 vl_noop_handler, \
1353 vl_api_##n##_t_endian, \
1354 vl_api_##n##_t_print, \
1355 sizeof(vl_api_##n##_t), 1);
1356 foreach_vpe_api_msg;
1357#undef _
1358
Steven Luongc5fa2b82019-04-25 11:19:49 -07001359 /* Mark VL_API_BRIDGE_DOMAIN_DUMP as mp safe */
1360 am->is_mp_safe[VL_API_BRIDGE_DOMAIN_DUMP] = 1;
1361
Matus Fabianf468e232016-12-02 06:00:53 -08001362 /*
1363 * Set up the (msg_name, crc, message-id) table
1364 */
1365 setup_message_id_table (am);
1366
1367 return 0;
1368}
1369
1370VLIB_API_INIT_FUNCTION (l2_api_hookup);
1371
1372/*
1373 * fd.io coding-style-patch-verification: ON
1374 *
1375 * Local Variables:
1376 * eval: (c-set-style "gnu")
1377 * End:
1378 */