blob: 5a0432de43d99175f7b35ac571ee7a6d6f627f69 [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
Filip Tehlar5a9d2a12021-06-22 21:20:29 +000035#include <vnet/format_fns.h>
36#include <vnet/l2/l2.api_enum.h>
37#include <vnet/l2/l2.api_types.h>
Matus Fabianf468e232016-12-02 06:00:53 -080038
Filip Tehlar5a9d2a12021-06-22 21:20:29 +000039#define REPLY_MSG_ID_BASE l2input_main.msg_id_base
Matus Fabianf468e232016-12-02 06:00:53 -080040#include <vlibapi/api_helper_macros.h>
41
Matus Fabianf468e232016-12-02 06:00:53 -080042static void
Florin Coras6c4dae22018-01-09 06:39:23 -080043send_l2_xconnect_details (vl_api_registration_t * reg, u32 context,
Matus Fabianf468e232016-12-02 06:00:53 -080044 u32 rx_sw_if_index, u32 tx_sw_if_index)
45{
46 vl_api_l2_xconnect_details_t *mp;
47
48 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -040049 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar5a9d2a12021-06-22 21:20:29 +000050 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_L2_XCONNECT_DETAILS);
Matus Fabianf468e232016-12-02 06:00:53 -080051 mp->context = context;
52 mp->rx_sw_if_index = htonl (rx_sw_if_index);
53 mp->tx_sw_if_index = htonl (tx_sw_if_index);
54
Florin Coras6c4dae22018-01-09 06:39:23 -080055 vl_api_send_msg (reg, (u8 *) mp);
Matus Fabianf468e232016-12-02 06:00:53 -080056}
57
58static void
59vl_api_l2_xconnect_dump_t_handler (vl_api_l2_xconnect_dump_t * mp)
60{
Florin Coras6c4dae22018-01-09 06:39:23 -080061 vl_api_registration_t *reg;
Matus Fabianf468e232016-12-02 06:00:53 -080062 l2input_main_t *l2im = &l2input_main;
Steven Luong16f08652021-02-08 23:48:30 -080063 u32 sw_if_index;
Matus Fabianf468e232016-12-02 06:00:53 -080064 l2_input_config_t *config;
65
Florin Coras6c4dae22018-01-09 06:39:23 -080066 reg = vl_api_client_index_to_registration (mp->client_index);
67 if (!reg)
Matus Fabianf468e232016-12-02 06:00:53 -080068 return;
69
70 /* *INDENT-OFF* */
Steven Luong16f08652021-02-08 23:48:30 -080071 vec_foreach_index (sw_if_index, l2im->configs)
72 {
73 config = vec_elt_at_index (l2im->configs, sw_if_index);
74 if (l2_input_is_xconnect (config))
75 send_l2_xconnect_details (reg, mp->context, sw_if_index,
76 config->output_sw_if_index);
77 }
Matus Fabianf468e232016-12-02 06:00:53 -080078 /* *INDENT-ON* */
79}
80
Pavel Kotucek0f971d82017-01-03 10:48:54 +010081static void
82vl_api_l2_fib_clear_table_t_handler (vl_api_l2_fib_clear_table_t * mp)
83{
84 int rv = 0;
85 vl_api_l2_fib_clear_table_reply_t *rmp;
86
Eyal Bari7537e712017-04-27 14:07:55 +030087 /* Clear all MACs including static MACs */
88 l2fib_clear_table ();
Pavel Kotucek0f971d82017-01-03 10:48:54 +010089
90 REPLY_MACRO (VL_API_L2_FIB_CLEAR_TABLE_REPLY);
91}
92
93static void
94send_l2fib_table_entry (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -080095 vl_api_registration_t * reg,
Pavel Kotucek0f971d82017-01-03 10:48:54 +010096 l2fib_entry_key_t * l2fe_key,
97 l2fib_entry_result_t * l2fe_res, u32 context)
98{
Ole Troan01384fe2017-05-12 11:55:35 +020099 vl_api_l2_fib_table_details_t *mp;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100100
101 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400102 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar5a9d2a12021-06-22 21:20:29 +0000103 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_L2_FIB_TABLE_DETAILS);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100104
105 mp->bd_id =
106 ntohl (l2input_main.bd_configs[l2fe_key->fields.bd_index].bd_id);
107
Jakub Grajciar145e3302019-10-24 13:52:42 +0200108 mac_address_encode ((mac_address_t *) l2fe_key->fields.mac, mp->mac);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100109 mp->sw_if_index = ntohl (l2fe_res->fields.sw_if_index);
Neale Ranns7341b6d2018-09-19 04:07:02 -0700110 mp->static_mac = (l2fib_entry_result_is_set_STATIC (l2fe_res) ? 1 : 0);
111 mp->filter_mac = (l2fib_entry_result_is_set_FILTER (l2fe_res) ? 1 : 0);
112 mp->bvi_mac = (l2fib_entry_result_is_set_BVI (l2fe_res) ? 1 : 0);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100113 mp->context = context;
114
Florin Coras6c4dae22018-01-09 06:39:23 -0800115 vl_api_send_msg (reg, (u8 *) mp);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100116}
117
118static void
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100119vl_api_l2_fib_table_dump_t_handler (vl_api_l2_fib_table_dump_t * mp)
120{
121 vpe_api_main_t *am = &vpe_api_main;
122 bd_main_t *bdm = &bd_main;
123 l2fib_entry_key_t *l2fe_key = NULL;
124 l2fib_entry_result_t *l2fe_res = NULL;
125 u32 ni, bd_id = ntohl (mp->bd_id);
126 u32 bd_index;
Florin Coras6c4dae22018-01-09 06:39:23 -0800127 vl_api_registration_t *reg;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100128 uword *p;
129
Florin Coras6c4dae22018-01-09 06:39:23 -0800130 reg = vl_api_client_index_to_registration (mp->client_index);
131 if (!reg)
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100132 return;
133
134 /* see l2fib_table_dump: ~0 means "any" */
135 if (bd_id == ~0)
136 bd_index = ~0;
137 else
138 {
139 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
140 if (p == 0)
141 return;
142
143 bd_index = p[0];
144 }
145
146 l2fib_table_dump (bd_index, &l2fe_key, &l2fe_res);
147
148 vec_foreach_index (ni, l2fe_key)
149 {
Florin Coras6c4dae22018-01-09 06:39:23 -0800150 send_l2fib_table_entry (am, reg, vec_elt_at_index (l2fe_key, ni),
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100151 vec_elt_at_index (l2fe_res, ni), mp->context);
152 }
153 vec_free (l2fe_key);
154 vec_free (l2fe_res);
155}
156
157static void
158vl_api_l2fib_add_del_t_handler (vl_api_l2fib_add_del_t * mp)
159{
160 bd_main_t *bdm = &bd_main;
161 l2input_main_t *l2im = &l2input_main;
162 vl_api_l2fib_add_del_reply_t *rmp;
163 int rv = 0;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100164 u32 bd_id = ntohl (mp->bd_id);
Eyal Bari31a71ab2017-06-25 14:42:33 +0300165 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100166
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100167 if (!p)
168 {
169 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
170 goto bad_sw_if_index;
171 }
Eyal Bari31a71ab2017-06-25 14:42:33 +0300172 u32 bd_index = p[0];
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100173
Jakub Grajciar145e3302019-10-24 13:52:42 +0200174 mac_address_t mac;
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200175
Jakub Grajciar145e3302019-10-24 13:52:42 +0200176 mac_address_decode (mp->mac, &mac);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100177 if (mp->is_add)
178 {
Eyal Bari31a71ab2017-06-25 14:42:33 +0300179 if (mp->filter_mac)
Jakub Grajciar145e3302019-10-24 13:52:42 +0200180 l2fib_add_filter_entry (mac.bytes, bd_index);
Eyal Bari31a71ab2017-06-25 14:42:33 +0300181 else
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100182 {
Neale Rannsb54d0812018-09-06 06:22:56 -0700183 l2fib_entry_result_flags_t flags = L2FIB_ENTRY_RESULT_FLAG_NONE;
Eyal Bari31a71ab2017-06-25 14:42:33 +0300184 u32 sw_if_index = ntohl (mp->sw_if_index);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100185 VALIDATE_SW_IF_INDEX (mp);
186 if (vec_len (l2im->configs) <= sw_if_index)
187 {
188 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
189 goto bad_sw_if_index;
190 }
191 else
192 {
193 l2_input_config_t *config;
194 config = vec_elt_at_index (l2im->configs, sw_if_index);
Neale Ranns47a3d992020-09-29 15:38:51 +0000195 if (!l2_input_is_bridge (config))
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100196 {
197 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
198 goto bad_sw_if_index;
199 }
200 }
Neale Rannsb54d0812018-09-06 06:22:56 -0700201 if (mp->static_mac)
202 flags |= L2FIB_ENTRY_RESULT_FLAG_STATIC;
203 if (mp->bvi_mac)
204 flags |= L2FIB_ENTRY_RESULT_FLAG_BVI;
Jakub Grajciar145e3302019-10-24 13:52:42 +0200205 l2fib_add_entry (mac.bytes, bd_index, sw_if_index, flags);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100206 }
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100207 }
208 else
209 {
John Lo7dbd7262018-05-31 10:25:18 -0400210 u32 sw_if_index = ntohl (mp->sw_if_index);
Jakub Grajciar145e3302019-10-24 13:52:42 +0200211 if (l2fib_del_entry (mac.bytes, bd_index, sw_if_index))
John Lo7dbd7262018-05-31 10:25:18 -0400212 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100213 }
214
215 BAD_SW_IF_INDEX_LABEL;
216
217 REPLY_MACRO (VL_API_L2FIB_ADD_DEL_REPLY);
218}
219
220static void
Jerome Tollet0f8d1002021-01-07 12:44:17 +0100221vl_api_want_l2_macs_events2_t_handler (vl_api_want_l2_macs_events2_t *mp)
John Lo8d00fff2017-08-03 00:35:36 -0400222{
223 int rv = 0;
Jerome Tollet0f8d1002021-01-07 12:44:17 +0100224 vl_api_want_l2_macs_events2_reply_t *rmp;
John Lo8d00fff2017-08-03 00:35:36 -0400225 l2learn_main_t *lm = &l2learn_main;
226 l2fib_main_t *fm = &l2fib_main;
227 u32 pid = ntohl (mp->pid);
John Lo8d00fff2017-08-03 00:35:36 -0400228
229 if (mp->enable_disable)
230 {
Jerome Tollet0f8d1002021-01-07 12:44:17 +0100231 if ((lm->client_pid == 0) || (lm->client_pid == pid))
John Lo8d00fff2017-08-03 00:35:36 -0400232 {
John Lo8d00fff2017-08-03 00:35:36 -0400233 if (mp->max_macs_in_event)
234 fm->max_macs_in_event = mp->max_macs_in_event * 10;
235 else
Jakub Grajciar145e3302019-10-24 13:52:42 +0200236 {
237 rv = VNET_API_ERROR_INVALID_VALUE;
238 goto exit;
239 }
John Lo8d00fff2017-08-03 00:35:36 -0400240
Jerome Tollet0f8d1002021-01-07 12:44:17 +0100241 /* if scan_delay was not set before */
242 if (fm->event_scan_delay == 0.0)
243 fm->event_scan_delay = (f64) (10) * 10e-3;
John Lo8d00fff2017-08-03 00:35:36 -0400244
Jerome Tollet0f8d1002021-01-07 12:44:17 +0100245 lm->client_pid = pid;
246 lm->client_index = mp->client_index;
John Lo8d00fff2017-08-03 00:35:36 -0400247 l2fib_flush_all_mac (vlib_get_main ());
248 }
249 else if (lm->client_pid != pid)
250 {
251 rv = VNET_API_ERROR_L2_MACS_EVENT_CLINET_PRESENT;
252 goto exit;
253 }
254 }
255 else if (lm->client_pid)
256 {
257 lm->client_pid = 0;
258 lm->client_index = 0;
Jerome Tollet0f8d1002021-01-07 12:44:17 +0100259 }
260
261exit:
262 REPLY_MACRO (VL_API_WANT_L2_MACS_EVENTS2_REPLY);
263}
264
265static void
266vl_api_want_l2_macs_events_t_handler (vl_api_want_l2_macs_events_t *mp)
267{
268 int rv = 0;
269 vl_api_want_l2_macs_events_reply_t *rmp;
270 l2learn_main_t *lm = &l2learn_main;
271 l2fib_main_t *fm = &l2fib_main;
272 u32 pid = ntohl (mp->pid);
273 u32 learn_limit = ntohl (mp->learn_limit);
274
275 if (mp->enable_disable)
276 {
277 if ((lm->client_pid == 0) || (lm->client_pid == pid))
278 {
279 if ((mp->max_macs_in_event == 0) || (mp->scan_delay == 0) ||
280 (learn_limit == 0) || (learn_limit > L2LEARN_DEFAULT_LIMIT))
281 {
282 rv = VNET_API_ERROR_INVALID_VALUE;
283 goto exit;
284 }
285 lm->client_pid = pid;
286 lm->client_index = mp->client_index;
287
288 fm->max_macs_in_event = mp->max_macs_in_event * 10;
289 fm->event_scan_delay = (f64) (mp->scan_delay) * 10e-3;
290
291 /* change learn limit and flush all learned MACs */
292 lm->global_learn_limit = learn_limit;
293 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;
305 if (learn_limit && (learn_limit <= L2LEARN_DEFAULT_LIMIT))
John Loe531f4c2017-08-22 09:16:50 -0400306 lm->global_learn_limit = learn_limit;
307 else
308 lm->global_learn_limit = L2LEARN_DEFAULT_LIMIT;
John Lo8d00fff2017-08-03 00:35:36 -0400309 }
310
311exit:
312 REPLY_MACRO (VL_API_WANT_L2_MACS_EVENTS_REPLY);
313}
314
315static void
Eyal Barif24991c2017-04-05 05:33:21 +0300316vl_api_l2fib_flush_int_t_handler (vl_api_l2fib_flush_int_t * mp)
317{
318 int rv = 0;
319 vlib_main_t *vm = vlib_get_main ();
320 vl_api_l2fib_flush_int_reply_t *rmp;
321
322 VALIDATE_SW_IF_INDEX (mp);
323
324 u32 sw_if_index = ntohl (mp->sw_if_index);
325 l2fib_flush_int_mac (vm, sw_if_index);
326
327 BAD_SW_IF_INDEX_LABEL;
328 REPLY_MACRO (VL_API_L2FIB_FLUSH_INT_REPLY);
329}
330
331static void
Eyal Bari7537e712017-04-27 14:07:55 +0300332vl_api_l2fib_flush_all_t_handler (vl_api_l2fib_flush_all_t * mp)
333{
334 int rv = 0;
335 vl_api_l2fib_flush_all_reply_t *rmp;
336
337 l2fib_flush_all_mac (vlib_get_main ());
338 REPLY_MACRO (VL_API_L2FIB_FLUSH_ALL_REPLY);
339}
340
341static void
Eyal Barif24991c2017-04-05 05:33:21 +0300342vl_api_l2fib_flush_bd_t_handler (vl_api_l2fib_flush_bd_t * mp)
343{
344 int rv = 0;
345 vlib_main_t *vm = vlib_get_main ();
346 bd_main_t *bdm = &bd_main;
347 vl_api_l2fib_flush_bd_reply_t *rmp;
348
349 u32 bd_id = ntohl (mp->bd_id);
350 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
351 if (p == 0)
352 {
353 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
354 goto out;
355 }
356 l2fib_flush_bd_mac (vm, *p);
357out:
358 REPLY_MACRO (VL_API_L2FIB_FLUSH_BD_REPLY);
359}
360
361static void
Jerome Tollet0f8d1002021-01-07 12:44:17 +0100362vl_api_l2fib_set_scan_delay_t_handler (vl_api_l2fib_set_scan_delay_t *mp)
363{
364 int rv = 0;
365 l2fib_main_t *fm = &l2fib_main;
366 vl_api_l2fib_set_scan_delay_reply_t *rmp;
367 u16 scan_delay = ntohs (mp->scan_delay);
368
369 if (mp->scan_delay)
370 {
371 fm->event_scan_delay = (f64) (scan_delay) *10e-3;
372 l2fib_flush_all_mac (vlib_get_main ());
373 }
374 else
375 {
376 rv = VNET_API_ERROR_INVALID_VALUE;
377 goto exit;
378 }
379
380exit:
381 REPLY_MACRO (VL_API_L2FIB_SET_SCAN_DELAY_REPLY);
382}
383
384static void
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100385vl_api_l2_flags_t_handler (vl_api_l2_flags_t * mp)
386{
387 vl_api_l2_flags_reply_t *rmp;
388 int rv = 0;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100389 u32 rbm = 0;
390
391 VALIDATE_SW_IF_INDEX (mp);
392
Eyal Barifead6702017-04-04 04:46:32 +0300393 u32 sw_if_index = ntohl (mp->sw_if_index);
John Lo8d00fff2017-08-03 00:35:36 -0400394 u32 flags = ntohl (mp->feature_bitmap);
395 u32 bitmap = 0;
396
397 if (flags & L2_LEARN)
398 bitmap |= L2INPUT_FEAT_LEARN;
399
400 if (flags & L2_FWD)
401 bitmap |= L2INPUT_FEAT_FWD;
402
403 if (flags & L2_FLOOD)
404 bitmap |= L2INPUT_FEAT_FLOOD;
405
406 if (flags & L2_UU_FLOOD)
407 bitmap |= L2INPUT_FEAT_UU_FLOOD;
408
409 if (flags & L2_ARP_TERM)
410 bitmap |= L2INPUT_FEAT_ARP_TERM;
411
412 rbm = l2input_intf_bitmap_enable (sw_if_index, bitmap, mp->is_set);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100413
414 BAD_SW_IF_INDEX_LABEL;
415
416 /* *INDENT-OFF* */
417 REPLY_MACRO2(VL_API_L2_FLAGS_REPLY,
418 ({
419 rmp->resulting_feature_bitmap = ntohl(rbm);
420 }));
421 /* *INDENT-ON* */
422}
423
424static void
Jerome Tollet5f93e3b2020-12-18 09:44:24 +0100425vl_api_bridge_domain_set_default_learn_limit_t_handler (
426 vl_api_bridge_domain_set_default_learn_limit_t *mp)
427{
428 vl_api_bridge_domain_set_default_learn_limit_reply_t *rmp;
429 int rv = 0;
430
431 l2learn_main.bd_default_learn_limit = ntohl (mp->learn_limit);
432 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_SET_DEFAULT_LEARN_LIMIT_REPLY);
433}
434
435static void
436vl_api_bridge_domain_set_learn_limit_t_handler (
437 vl_api_bridge_domain_set_learn_limit_t *mp)
438{
439 vlib_main_t *vm = vlib_get_main ();
440 bd_main_t *bdm = &bd_main;
441 vl_api_bridge_domain_set_learn_limit_reply_t *rmp;
442 int rv = 0;
443 u32 bd_id = ntohl (mp->bd_id);
444 uword *p;
445
446 if (bd_id == 0)
447 {
448 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
449 goto out;
450 }
451
452 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
453 if (p == 0)
454 {
455 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
456 goto out;
457 }
458 bd_set_learn_limit (vm, *p, ntohl (mp->learn_limit));
459out:
460 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_SET_LEARN_LIMIT_REPLY);
461}
462
463static void
Eyal Barifead6702017-04-04 04:46:32 +0300464vl_api_bridge_domain_set_mac_age_t_handler (vl_api_bridge_domain_set_mac_age_t
465 * mp)
466{
467 vlib_main_t *vm = vlib_get_main ();
468 bd_main_t *bdm = &bd_main;
469 vl_api_bridge_domain_set_mac_age_reply_t *rmp;
470 int rv = 0;
471 u32 bd_id = ntohl (mp->bd_id);
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500472 uword *p;
473
474 if (bd_id == 0)
475 {
476 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
477 goto out;
478 }
479
480 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
Eyal Barifead6702017-04-04 04:46:32 +0300481 if (p == 0)
482 {
483 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
484 goto out;
485 }
486 bd_set_mac_age (vm, *p, mp->mac_age);
487out:
488 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_SET_MAC_AGE_REPLY);
489}
490
491static void
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100492vl_api_bridge_domain_add_del_t_handler (vl_api_bridge_domain_add_del_t * mp)
493{
Eyal Barib1352ed2017-04-07 23:14:17 +0300494 l2_bridge_domain_add_del_args_t a = {
495 .is_add = mp->is_add,
496 .flood = mp->flood,
497 .uu_flood = mp->uu_flood,
498 .forward = mp->forward,
499 .learn = mp->learn,
500 .arp_term = mp->arp_term,
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +0200501 .arp_ufwd = mp->arp_ufwd,
Eyal Barib1352ed2017-04-07 23:14:17 +0300502 .mac_age = mp->mac_age,
503 .bd_id = ntohl (mp->bd_id),
Jerome Tollet48304142017-09-05 12:13:22 +0100504 .bd_tag = mp->bd_tag
Eyal Barib1352ed2017-04-07 23:14:17 +0300505 };
506
507 int rv = bd_add_del (&a);
508
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100509 vl_api_bridge_domain_add_del_reply_t *rmp;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100510 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_ADD_DEL_REPLY);
511}
512
513static void
Ole Troan01384fe2017-05-12 11:55:35 +0200514send_bridge_domain_details (l2input_main_t * l2im,
Florin Coras6c4dae22018-01-09 06:39:23 -0800515 vl_api_registration_t * reg,
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100516 l2_bridge_domain_t * bd_config,
517 u32 n_sw_ifs, u32 context)
518{
519 vl_api_bridge_domain_details_t *mp;
Ole Troan01384fe2017-05-12 11:55:35 +0200520 l2_flood_member_t *m;
521 vl_api_bridge_domain_sw_if_t *sw_ifs;
522 l2_input_config_t *input_cfg;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100523
Ole Troan01384fe2017-05-12 11:55:35 +0200524 mp = vl_msg_api_alloc (sizeof (*mp) +
525 (n_sw_ifs * sizeof (vl_api_bridge_domain_sw_if_t)));
Dave Barachb7b92992018-10-17 10:38:51 -0400526 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar5a9d2a12021-06-22 21:20:29 +0000527 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_BRIDGE_DOMAIN_DETAILS);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100528 mp->bd_id = ntohl (bd_config->bd_id);
529 mp->flood = bd_feature_flood (bd_config);
530 mp->uu_flood = bd_feature_uu_flood (bd_config);
531 mp->forward = bd_feature_forward (bd_config);
532 mp->learn = bd_feature_learn (bd_config);
533 mp->arp_term = bd_feature_arp_term (bd_config);
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +0200534 mp->arp_ufwd = bd_feature_arp_ufwd (bd_config);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100535 mp->bvi_sw_if_index = ntohl (bd_config->bvi_sw_if_index);
Neale Rannsb4743802018-09-05 09:13:57 -0700536 mp->uu_fwd_sw_if_index = ntohl (bd_config->uu_fwd_sw_if_index);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100537 mp->mac_age = bd_config->mac_age;
Jerome Tollet48304142017-09-05 12:13:22 +0100538 if (bd_config->bd_tag)
539 {
540 strncpy ((char *) mp->bd_tag, (char *) bd_config->bd_tag,
541 ARRAY_LEN (mp->bd_tag) - 1);
542 mp->bd_tag[ARRAY_LEN (mp->bd_tag) - 1] = 0;
543 }
544
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100545 mp->context = context;
546
Ole Troan01384fe2017-05-12 11:55:35 +0200547 sw_ifs = (vl_api_bridge_domain_sw_if_t *) mp->sw_if_details;
548 vec_foreach (m, bd_config->members)
549 {
550 sw_ifs->sw_if_index = ntohl (m->sw_if_index);
551 input_cfg = vec_elt_at_index (l2im->configs, m->sw_if_index);
552 sw_ifs->shg = input_cfg->shg;
553 sw_ifs++;
554 mp->n_sw_ifs++;
555 }
556 mp->n_sw_ifs = htonl (mp->n_sw_ifs);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100557
Florin Coras6c4dae22018-01-09 06:39:23 -0800558 vl_api_send_msg (reg, (u8 *) mp);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100559}
560
561static void
562vl_api_bridge_domain_dump_t_handler (vl_api_bridge_domain_dump_t * mp)
563{
564 bd_main_t *bdm = &bd_main;
565 l2input_main_t *l2im = &l2input_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800566 vl_api_registration_t *reg;
Jakub Grajciar145e3302019-10-24 13:52:42 +0200567 u32 bd_id, bd_index, end, filter_sw_if_index;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100568
Florin Coras6c4dae22018-01-09 06:39:23 -0800569 reg = vl_api_client_index_to_registration (mp->client_index);
570 if (!reg)
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100571 return;
572
Jakub Grajciar145e3302019-10-24 13:52:42 +0200573 filter_sw_if_index = ntohl (mp->sw_if_index);
574 if (filter_sw_if_index != ~0)
575 return; /* UNIMPLEMENTED */
576
Florin Coras6c4dae22018-01-09 06:39:23 -0800577 bd_id = ntohl (mp->bd_id);
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500578 if (bd_id == 0)
579 return;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100580
Eyal Bari259fca72017-05-14 10:38:39 +0300581 if (bd_id == ~0)
582 bd_index = 0, end = vec_len (l2im->bd_configs);
583 else
584 {
585 bd_index = bd_find_index (bdm, bd_id);
586 if (bd_index == ~0)
587 return;
588
589 end = bd_index + 1;
590 }
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500591
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100592 for (; bd_index < end; bd_index++)
593 {
Eyal Bari259fca72017-05-14 10:38:39 +0300594 l2_bridge_domain_t *bd_config =
595 l2input_bd_config_from_index (l2im, bd_index);
Dave Barach11fb09e2020-08-06 12:10:09 -0400596 /* skip placeholder bd_id 0 */
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100597 if (bd_config && (bd_config->bd_id > 0))
Florin Coras6c4dae22018-01-09 06:39:23 -0800598 send_bridge_domain_details (l2im, reg, bd_config,
Ole Troan01384fe2017-05-12 11:55:35 +0200599 vec_len (bd_config->members),
600 mp->context);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100601 }
602}
603
Neale Rannsb4743802018-09-05 09:13:57 -0700604static bd_flags_t
605bd_flags_decode (vl_api_bd_flags_t v)
606{
607 bd_flags_t f = L2_NONE;
608
609 v = ntohl (v);
610
611 if (v & BRIDGE_API_FLAG_LEARN)
612 f |= L2_LEARN;
613 if (v & BRIDGE_API_FLAG_FWD)
614 f |= L2_FWD;
615 if (v & BRIDGE_API_FLAG_FLOOD)
616 f |= L2_FLOOD;
617 if (v & BRIDGE_API_FLAG_UU_FLOOD)
618 f |= L2_UU_FLOOD;
619 if (v & BRIDGE_API_FLAG_ARP_TERM)
620 f |= L2_ARP_TERM;
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +0200621 if (v & BRIDGE_API_FLAG_ARP_UFWD)
622 f |= L2_ARP_UFWD;
Neale Rannsb4743802018-09-05 09:13:57 -0700623
624 return (f);
625}
626
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100627static void
628vl_api_bridge_flags_t_handler (vl_api_bridge_flags_t * mp)
629{
630 vlib_main_t *vm = vlib_get_main ();
631 bd_main_t *bdm = &bd_main;
632 vl_api_bridge_flags_reply_t *rmp;
Dave Barach98d6b612019-01-03 09:30:00 -0500633 u32 bitmap = 0;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100634 int rv = 0;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100635
Neale Rannsb4743802018-09-05 09:13:57 -0700636 bd_flags_t flags = bd_flags_decode (mp->flags);
Eyal Bari259fca72017-05-14 10:38:39 +0300637 u32 bd_id = ntohl (mp->bd_id);
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500638 if (bd_id == 0)
639 {
640 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
641 goto out;
642 }
643
Eyal Bari259fca72017-05-14 10:38:39 +0300644 u32 bd_index = bd_find_index (bdm, bd_id);
645 if (bd_index == ~0)
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100646 {
647 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
648 goto out;
649 }
650
Dave Barach98d6b612019-01-03 09:30:00 -0500651 bitmap = bd_set_flags (vm, bd_index, flags, mp->is_set);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100652
653out:
654 /* *INDENT-OFF* */
655 REPLY_MACRO2(VL_API_BRIDGE_FLAGS_REPLY,
656 ({
John Lo8d00fff2017-08-03 00:35:36 -0400657 rmp->resulting_feature_bitmap = ntohl(bitmap);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100658 }));
659 /* *INDENT-ON* */
660}
Matus Fabianf468e232016-12-02 06:00:53 -0800661
Pavel Kotucekadec5872017-01-25 08:50:53 +0100662static void
663 vl_api_l2_interface_vlan_tag_rewrite_t_handler
664 (vl_api_l2_interface_vlan_tag_rewrite_t * mp)
665{
666 int rv = 0;
667 vl_api_l2_interface_vlan_tag_rewrite_reply_t *rmp;
668 vnet_main_t *vnm = vnet_get_main ();
669 vlib_main_t *vm = vlib_get_main ();
670 u32 vtr_op;
671
672 VALIDATE_SW_IF_INDEX (mp);
673
674 vtr_op = ntohl (mp->vtr_op);
675
676 /* The L2 code is unsuspicious */
677 switch (vtr_op)
678 {
679 case L2_VTR_DISABLED:
680 case L2_VTR_PUSH_1:
681 case L2_VTR_PUSH_2:
682 case L2_VTR_POP_1:
683 case L2_VTR_POP_2:
684 case L2_VTR_TRANSLATE_1_1:
685 case L2_VTR_TRANSLATE_1_2:
686 case L2_VTR_TRANSLATE_2_1:
687 case L2_VTR_TRANSLATE_2_2:
688 break;
689
690 default:
691 rv = VNET_API_ERROR_INVALID_VALUE;
692 goto bad_sw_if_index;
693 }
694
695 rv = l2vtr_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
696 ntohl (mp->push_dot1q), ntohl (mp->tag1),
697 ntohl (mp->tag2));
698
699 BAD_SW_IF_INDEX_LABEL;
700
701 REPLY_MACRO (VL_API_L2_INTERFACE_VLAN_TAG_REWRITE_REPLY);
702}
703
704static void
705 vl_api_l2_interface_pbb_tag_rewrite_t_handler
706 (vl_api_l2_interface_pbb_tag_rewrite_t * mp)
707{
708 vl_api_l2_interface_pbb_tag_rewrite_reply_t *rmp;
709 vnet_main_t *vnm = vnet_get_main ();
710 vlib_main_t *vm = vlib_get_main ();
711 u32 vtr_op;
712 int rv = 0;
Jakub Grajciar145e3302019-10-24 13:52:42 +0200713 mac_address_t b_dmac, b_smac;
Pavel Kotucekadec5872017-01-25 08:50:53 +0100714
715 VALIDATE_SW_IF_INDEX (mp);
716
717 vtr_op = ntohl (mp->vtr_op);
718
719 switch (vtr_op)
720 {
721 case L2_VTR_DISABLED:
722 case L2_VTR_PUSH_2:
723 case L2_VTR_POP_2:
724 case L2_VTR_TRANSLATE_2_1:
725 break;
726
727 default:
728 rv = VNET_API_ERROR_INVALID_VALUE;
729 goto bad_sw_if_index;
730 }
731
Jakub Grajciar145e3302019-10-24 13:52:42 +0200732 mac_address_decode (mp->b_dmac, &b_dmac);
733 mac_address_decode (mp->b_smac, &b_smac);
734
Pavel Kotucekadec5872017-01-25 08:50:53 +0100735 rv = l2pbb_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
Jakub Grajciar145e3302019-10-24 13:52:42 +0200736 b_dmac.bytes, b_smac.bytes, ntohs (mp->b_vlanid),
Pavel Kotucekadec5872017-01-25 08:50:53 +0100737 ntohl (mp->i_sid), ntohs (mp->outer_tag));
738
739 BAD_SW_IF_INDEX_LABEL;
740
741 REPLY_MACRO (VL_API_L2_INTERFACE_PBB_TAG_REWRITE_REPLY);
742}
743
Neale Rannsb8d44812017-11-10 06:53:54 -0800744static void
745 vl_api_sw_interface_set_l2_xconnect_t_handler
746 (vl_api_sw_interface_set_l2_xconnect_t * mp)
747{
748 vl_api_sw_interface_set_l2_xconnect_reply_t *rmp;
749 int rv = 0;
750 u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
751 u32 tx_sw_if_index = ntohl (mp->tx_sw_if_index);
752 vlib_main_t *vm = vlib_get_main ();
753 vnet_main_t *vnm = vnet_get_main ();
754
755 VALIDATE_RX_SW_IF_INDEX (mp);
756
757 if (mp->enable)
758 {
759 VALIDATE_TX_SW_IF_INDEX (mp);
760 rv = set_int_l2_mode (vm, vnm, MODE_L2_XC,
Neale Rannsb4743802018-09-05 09:13:57 -0700761 rx_sw_if_index, 0,
762 L2_BD_PORT_TYPE_NORMAL, 0, tx_sw_if_index);
Neale Rannsb8d44812017-11-10 06:53:54 -0800763 }
764 else
765 {
Neale Rannsb4743802018-09-05 09:13:57 -0700766 rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0,
767 L2_BD_PORT_TYPE_NORMAL, 0, 0);
Neale Rannsb8d44812017-11-10 06:53:54 -0800768 }
769
Alexander Chernavine178b272018-09-24 05:42:01 -0400770 switch (rv)
771 {
772 case MODE_ERROR_ETH:
773 rv = VNET_API_ERROR_NON_ETHERNET;
774 break;
775 case MODE_ERROR_BVI_DEF:
776 rv = VNET_API_ERROR_BD_ALREADY_HAS_BVI;
777 break;
778 }
779
Neale Rannsb8d44812017-11-10 06:53:54 -0800780 BAD_RX_SW_IF_INDEX_LABEL;
781 BAD_TX_SW_IF_INDEX_LABEL;
782
783 REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_XCONNECT_REPLY);
784}
785
Neale Rannsb4743802018-09-05 09:13:57 -0700786static int
787l2_bd_port_type_decode (vl_api_l2_port_type_t v, l2_bd_port_type_t * l)
788{
789 v = clib_net_to_host_u32 (v);
790
791 switch (v)
792 {
793 case L2_API_PORT_TYPE_NORMAL:
794 *l = L2_BD_PORT_TYPE_NORMAL;
795 return 0;
796 case L2_API_PORT_TYPE_BVI:
797 *l = L2_BD_PORT_TYPE_BVI;
798 return 0;
799 case L2_API_PORT_TYPE_UU_FWD:
800 *l = L2_BD_PORT_TYPE_UU_FWD;
801 return 0;
802 }
803
804 return (VNET_API_ERROR_INVALID_VALUE);
805}
806
Neale Rannsb8d44812017-11-10 06:53:54 -0800807static void
808 vl_api_sw_interface_set_l2_bridge_t_handler
809 (vl_api_sw_interface_set_l2_bridge_t * mp)
810{
811 bd_main_t *bdm = &bd_main;
812 vl_api_sw_interface_set_l2_bridge_reply_t *rmp;
813 int rv = 0;
814 vlib_main_t *vm = vlib_get_main ();
815 vnet_main_t *vnm = vnet_get_main ();
Neale Rannsb4743802018-09-05 09:13:57 -0700816 l2_bd_port_type_t pt;
Neale Rannsb8d44812017-11-10 06:53:54 -0800817
818 VALIDATE_RX_SW_IF_INDEX (mp);
819 u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
Neale Rannsb4743802018-09-05 09:13:57 -0700820 rv = l2_bd_port_type_decode (mp->port_type, &pt);
Neale Rannsb8d44812017-11-10 06:53:54 -0800821
Neale Rannsb4743802018-09-05 09:13:57 -0700822 if (0 != rv)
823 goto out;
Neale Rannsb8d44812017-11-10 06:53:54 -0800824 if (mp->enable)
825 {
826 VALIDATE_BD_ID (mp);
827 u32 bd_id = ntohl (mp->bd_id);
828 u32 bd_index = bd_find_or_add_bd_index (bdm, bd_id);
Neale Rannsb4743802018-09-05 09:13:57 -0700829
Neale Rannsb8d44812017-11-10 06:53:54 -0800830 rv = set_int_l2_mode (vm, vnm, MODE_L2_BRIDGE,
Neale Rannsb4743802018-09-05 09:13:57 -0700831 rx_sw_if_index, bd_index, pt, mp->shg, 0);
Neale Rannsb8d44812017-11-10 06:53:54 -0800832 }
833 else
834 {
Neale Rannsb4743802018-09-05 09:13:57 -0700835 rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0, pt, 0, 0);
Neale Rannsb8d44812017-11-10 06:53:54 -0800836 }
837
Alexander Chernavine178b272018-09-24 05:42:01 -0400838 switch (rv)
839 {
840 case MODE_ERROR_ETH:
841 rv = VNET_API_ERROR_NON_ETHERNET;
842 break;
843 case MODE_ERROR_BVI_DEF:
844 rv = VNET_API_ERROR_BD_ALREADY_HAS_BVI;
845 break;
846 }
847
Neale Rannsb8d44812017-11-10 06:53:54 -0800848 BAD_RX_SW_IF_INDEX_LABEL;
849 BAD_BD_ID_LABEL;
Neale Rannsb4743802018-09-05 09:13:57 -0700850out:
Neale Rannsb8d44812017-11-10 06:53:54 -0800851 REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_BRIDGE_REPLY);
852}
853
854static void
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200855send_bd_ip_mac_entry (vpe_api_main_t * am,
856 vl_api_registration_t * reg,
Neale Rannsbc764c82019-06-19 07:07:13 -0700857 u32 bd_id,
858 const ip46_address_t * ip,
859 ip46_type_t itype,
860 const mac_address_t * mac, u32 context)
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200861{
862 vl_api_bd_ip_mac_details_t *mp;
863
864 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400865 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar5a9d2a12021-06-22 21:20:29 +0000866 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_BD_IP_MAC_DETAILS);
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200867
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200868 mp->context = context;
Neale Rannsbc764c82019-06-19 07:07:13 -0700869 mp->entry.bd_id = ntohl (bd_id);
870
871 ip_address_encode (ip, itype, &mp->entry.ip);
872 mac_address_encode (mac, mp->entry.mac);
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200873
874 vl_api_send_msg (reg, (u8 *) mp);
875}
876
877static void
878vl_api_bd_ip_mac_dump_t_handler (vl_api_bd_ip_mac_dump_t * mp)
879{
880 vpe_api_main_t *am = &vpe_api_main;
881 bd_main_t *bdm = &bd_main;
882 l2_bridge_domain_t *bd_config;
883 u32 bd_id = ntohl (mp->bd_id);
Mohsin Kazmib24dfec2018-08-23 12:24:19 +0200884 u32 bd_index, start, end;
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200885 vl_api_registration_t *reg;
886 uword *p;
887
888 reg = vl_api_client_index_to_registration (mp->client_index);
889 if (!reg)
890 return;
891
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200892 /* see bd_id: ~0 means "any" */
893 if (bd_id == ~0)
Mohsin Kazmib24dfec2018-08-23 12:24:19 +0200894 {
895 start = 1;
896 end = vec_len (l2input_main.bd_configs);
897 }
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200898 else
899 {
900 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
901 if (p == 0)
902 return;
903
904 bd_index = p[0];
905 vec_validate (l2input_main.bd_configs, bd_index);
906 start = bd_index;
907 end = start + 1;
908 }
909
910 for (bd_index = start; bd_index < end; bd_index++)
911 {
912 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
913 if (bd_is_valid (bd_config))
914 {
915 ip4_address_t ip4_addr;
916 ip6_address_t *ip6_addr;
Neale Rannsbc764c82019-06-19 07:07:13 -0700917 mac_address_t mac;
918 u64 mac64;
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200919 bd_id = bd_config->bd_id;
920
921 /* *INDENT-OFF* */
Neale Rannsbc764c82019-06-19 07:07:13 -0700922 hash_foreach (ip4_addr.as_u32, mac64, bd_config->mac_by_ip4,
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200923 ({
Neale Rannsbc764c82019-06-19 07:07:13 -0700924 ip46_address_t ip = {
925 .ip4 = ip4_addr,
926 };
927 mac_address_from_u64(&mac, mac64);
928
929 send_bd_ip_mac_entry (am, reg, bd_id, &ip, IP46_TYPE_IP4,
930 &mac, mp->context);
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200931 }));
932
Neale Rannsbc764c82019-06-19 07:07:13 -0700933 hash_foreach_mem (ip6_addr, mac64, bd_config->mac_by_ip6,
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200934 ({
Neale Rannsbc764c82019-06-19 07:07:13 -0700935 ip46_address_t ip = {
936 .ip6 = *ip6_addr,
937 };
938 mac_address_from_u64(&mac, mac64);
939
940 send_bd_ip_mac_entry (am, reg, bd_id, &ip, IP46_TYPE_IP6,
941 &mac, mp->context);
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200942 }));
943 /* *INDENT-ON* */
944 }
945 }
946}
947
948static void
Neale Rannsb8d44812017-11-10 06:53:54 -0800949vl_api_bd_ip_mac_add_del_t_handler (vl_api_bd_ip_mac_add_del_t * mp)
950{
Neale Ranns4d5b9172018-10-24 02:57:49 -0700951 ip46_address_t ip_addr = ip46_address_initializer;
Neale Rannsb8d44812017-11-10 06:53:54 -0800952 vl_api_bd_ip_mac_add_del_reply_t *rmp;
Neale Ranns4d5b9172018-10-24 02:57:49 -0700953 bd_main_t *bdm = &bd_main;
954 u32 bd_index, bd_id;
955 mac_address_t mac;
956 ip46_type_t type;
Neale Rannsb8d44812017-11-10 06:53:54 -0800957 int rv = 0;
Neale Rannsb8d44812017-11-10 06:53:54 -0800958 uword *p;
959
Neale Rannsbc764c82019-06-19 07:07:13 -0700960 bd_id = ntohl (mp->entry.bd_id);
Neale Ranns4d5b9172018-10-24 02:57:49 -0700961
Neale Rannsb8d44812017-11-10 06:53:54 -0800962 if (bd_id == 0)
963 {
964 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
965 goto out;
966 }
967
968 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
969 if (p == 0)
970 {
971 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
972 goto out;
973 }
Neale Rannsb8d44812017-11-10 06:53:54 -0800974 bd_index = p[0];
Neale Ranns4d5b9172018-10-24 02:57:49 -0700975
Neale Rannsbc764c82019-06-19 07:07:13 -0700976 type = ip_address_decode (&mp->entry.ip, &ip_addr);
977 mac_address_decode (mp->entry.mac, &mac);
Neale Ranns4d5b9172018-10-24 02:57:49 -0700978
979 if (bd_add_del_ip_mac (bd_index, type, &ip_addr, &mac, mp->is_add))
Neale Rannsb8d44812017-11-10 06:53:54 -0800980 rv = VNET_API_ERROR_UNSPECIFIED;
981
982out:
983 REPLY_MACRO (VL_API_BD_IP_MAC_ADD_DEL_REPLY);
984}
985
John Loe26c81f2019-01-07 15:16:33 -0500986static void
987vl_api_bd_ip_mac_flush_t_handler (vl_api_bd_ip_mac_flush_t * mp)
988{
989 vl_api_bd_ip_mac_flush_reply_t *rmp;
990 bd_main_t *bdm = &bd_main;
991 u32 bd_index, bd_id;
992 int rv = 0;
993 uword *p;
994
995 bd_id = ntohl (mp->bd_id);
996
997 if (bd_id == 0)
998 {
999 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
1000 goto out;
1001 }
1002
1003 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
1004 if (p == 0)
1005 {
1006 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1007 goto out;
1008 }
1009 bd_index = p[0];
1010
1011 bd_flush_ip_mac (bd_index);
1012
1013out:
1014 REPLY_MACRO (VL_API_BD_IP_MAC_FLUSH_REPLY);
1015}
1016
Neale Rannsb8d44812017-11-10 06:53:54 -08001017extern void l2_efp_filter_configure (vnet_main_t * vnet_main,
Neale Ranns6b9b41c2018-07-23 05:47:09 -04001018 u32 sw_if_index, u8 enable);
Neale Rannsb8d44812017-11-10 06:53:54 -08001019
1020static void
1021vl_api_l2_interface_efp_filter_t_handler (vl_api_l2_interface_efp_filter_t *
1022 mp)
1023{
1024 int rv;
1025 vl_api_l2_interface_efp_filter_reply_t *rmp;
1026 vnet_main_t *vnm = vnet_get_main ();
1027
Neale Ranns6b9b41c2018-07-23 05:47:09 -04001028 VALIDATE_SW_IF_INDEX (mp);
1029
Neale Rannsb8d44812017-11-10 06:53:54 -08001030 // enable/disable the feature
Neale Ranns6b9b41c2018-07-23 05:47:09 -04001031 l2_efp_filter_configure (vnm, ntohl (mp->sw_if_index), mp->enable_disable);
Neale Rannsb8d44812017-11-10 06:53:54 -08001032 rv = vnm->api_errno;
1033
Neale Ranns6b9b41c2018-07-23 05:47:09 -04001034 BAD_SW_IF_INDEX_LABEL;
Neale Rannsb8d44812017-11-10 06:53:54 -08001035 REPLY_MACRO (VL_API_L2_INTERFACE_EFP_FILTER_REPLY);
1036}
1037
1038static void
1039vl_api_l2_patch_add_del_t_handler (vl_api_l2_patch_add_del_t * mp)
1040{
1041 extern int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
1042 int is_add);
1043 vl_api_l2_patch_add_del_reply_t *rmp;
1044 int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
1045 int is_add);
1046 int rv = 0;
1047
1048 VALIDATE_RX_SW_IF_INDEX (mp);
1049 VALIDATE_TX_SW_IF_INDEX (mp);
1050
1051 rv = vnet_l2_patch_add_del (ntohl (mp->rx_sw_if_index),
1052 ntohl (mp->tx_sw_if_index),
1053 (int) (mp->is_add != 0));
1054
1055 BAD_RX_SW_IF_INDEX_LABEL;
1056 BAD_TX_SW_IF_INDEX_LABEL;
1057
1058 REPLY_MACRO (VL_API_L2_PATCH_ADD_DEL_REPLY);
1059}
1060
1061static void
1062vl_api_sw_interface_set_vpath_t_handler (vl_api_sw_interface_set_vpath_t * mp)
1063{
1064 vl_api_sw_interface_set_vpath_reply_t *rmp;
1065 int rv = 0;
1066 u32 sw_if_index = ntohl (mp->sw_if_index);
1067
1068 VALIDATE_SW_IF_INDEX (mp);
1069
1070 l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_VPATH, mp->enable);
1071 vnet_feature_enable_disable ("ip4-unicast", "vpath-input-ip4",
1072 sw_if_index, mp->enable, 0, 0);
1073 vnet_feature_enable_disable ("ip4-multicast", "vpath-input-ip4",
1074 sw_if_index, mp->enable, 0, 0);
1075 vnet_feature_enable_disable ("ip6-unicast", "vpath-input-ip6",
1076 sw_if_index, mp->enable, 0, 0);
1077 vnet_feature_enable_disable ("ip6-multicast", "vpath-input-ip6",
1078 sw_if_index, mp->enable, 0, 0);
1079
1080 BAD_SW_IF_INDEX_LABEL;
1081
1082 REPLY_MACRO (VL_API_SW_INTERFACE_SET_VPATH_REPLY);
1083}
1084
Neale Ranns192b13f2019-03-15 02:16:20 -07001085static void
1086vl_api_bvi_create_t_handler (vl_api_bvi_create_t * mp)
1087{
1088 vl_api_bvi_create_reply_t *rmp;
1089 mac_address_t mac;
1090 u32 sw_if_index;
1091 int rv;
1092
1093 mac_address_decode (mp->mac, &mac);
1094
1095 rv = l2_bvi_create (ntohl (mp->user_instance), &mac, &sw_if_index);
1096
1097 /* *INDENT-OFF* */
1098 REPLY_MACRO2(VL_API_BVI_CREATE_REPLY,
1099 ({
1100 rmp->sw_if_index = ntohl (sw_if_index);
1101 }));
1102 /* *INDENT-ON* */
1103}
1104
1105static void
1106vl_api_bvi_delete_t_handler (vl_api_bvi_delete_t * mp)
1107{
1108 vl_api_bvi_delete_reply_t *rmp;
1109 int rv;
1110
1111 rv = l2_bvi_delete (ntohl (mp->sw_if_index));
1112
1113 REPLY_MACRO (VL_API_BVI_DELETE_REPLY);
1114}
1115
Neale Rannscbe25aa2019-09-30 10:53:31 +00001116static bool
1117l2_arp_term_publish_event_is_equal (const l2_arp_term_publish_event_t * e1,
1118 const l2_arp_term_publish_event_t * e2)
1119{
1120 if (e1 == NULL || e2 == NULL)
1121 return false;
1122 return (ip46_address_is_equal (&e1->ip, &e2->ip) &&
1123 (e1->sw_if_index == e2->sw_if_index) &&
1124 (mac_address_equal (&e1->mac, &e2->mac)));
1125}
1126
1127static uword
1128l2_arp_term_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1129 vlib_frame_t * f)
1130{
1131 /* These cross the longjmp boundary (vlib_process_wait_for_event)
1132 * and need to be volatile - to prevent them from being optimized into
1133 * a register - which could change during suspension */
1134 volatile f64 last = vlib_time_now (vm);
1135 volatile l2_arp_term_publish_event_t last_event = { };
1136
1137 l2_arp_term_main_t *l2am = &l2_arp_term_main;
1138
1139 while (1)
1140 {
1141 uword event_type = L2_ARP_TERM_EVENT_PUBLISH;
1142 vpe_client_registration_t *reg;
1143 f64 now;
1144
1145 vlib_process_wait_for_event (vm);
1146
1147 vlib_process_get_event_data (vm, &event_type);
1148 now = vlib_time_now (vm);
1149
1150 if (event_type == L2_ARP_TERM_EVENT_PUBLISH)
1151 {
1152 l2_arp_term_publish_event_t *event;
1153
1154 vec_foreach (event, l2am->publish_events)
1155 {
1156 /* dampen duplicate events - cast away volatile */
1157 if (l2_arp_term_publish_event_is_equal
1158 (event, (l2_arp_term_publish_event_t *) & last_event) &&
1159 (now - last) < 10.0)
1160 {
1161 continue;
1162 }
1163 last_event = *event;
1164 last = now;
1165
Damjan Marionb2c31b62020-12-13 21:47:40 +01001166 pool_foreach (reg, vpe_api_main.l2_arp_term_events_registrations)
Steven Luonge46bd4c2021-02-15 09:25:10 -08001167 {
1168 vl_api_registration_t *vl_reg;
1169 vl_reg =
1170 vl_api_client_index_to_registration (reg->client_index);
1171 ALWAYS_ASSERT (vl_reg != NULL);
Neale Rannscbe25aa2019-09-30 10:53:31 +00001172
Steven Luonge46bd4c2021-02-15 09:25:10 -08001173 if (vl_reg && vl_api_can_send_msg (vl_reg))
1174 {
1175 vl_api_l2_arp_term_event_t *vevent;
1176 vevent = vl_msg_api_alloc (sizeof *vevent);
1177 clib_memset (vevent, 0, sizeof *vevent);
Filip Tehlar5a9d2a12021-06-22 21:20:29 +00001178 vevent->_vl_msg_id =
1179 htons (REPLY_MSG_ID_BASE + VL_API_L2_ARP_TERM_EVENT);
Steven Luonge46bd4c2021-02-15 09:25:10 -08001180 vevent->client_index = reg->client_index;
1181 vevent->pid = reg->client_pid;
1182 ip_address_encode (&event->ip, event->type, &vevent->ip);
1183 vevent->sw_if_index = htonl (event->sw_if_index);
1184 mac_address_encode (&event->mac, vevent->mac);
1185 vl_api_send_msg (vl_reg, (u8 *) vevent);
1186 }
1187 }
Neale Rannscbe25aa2019-09-30 10:53:31 +00001188 }
1189 vec_reset_length (l2am->publish_events);
1190 }
1191 }
1192
1193 return 0;
1194}
1195
1196/* *INDENT-OFF* */
1197VLIB_REGISTER_NODE (l2_arp_term_process_node) = {
1198 .function = l2_arp_term_process,
1199 .type = VLIB_NODE_TYPE_PROCESS,
1200 .name = "l2-arp-term-publisher",
1201};
1202/* *INDENT-ON* */
1203
1204static void
1205vl_api_want_l2_arp_term_events_t_handler (vl_api_want_l2_arp_term_events_t *
1206 mp)
1207{
1208 vl_api_want_l2_arp_term_events_reply_t *rmp;
1209 vpe_api_main_t *am = &vpe_api_main;
1210 vpe_client_registration_t *rp;
1211 int rv = 0;
1212 uword *p;
1213
1214 p = hash_get (am->l2_arp_term_events_registration_hash, mp->client_index);
1215
1216 if (p)
1217 {
1218 if (mp->enable)
1219 {
1220 clib_warning ("pid %d: already enabled...", mp->pid);
1221 rv = VNET_API_ERROR_INVALID_REGISTRATION;
1222 goto reply;
1223 }
1224 else
1225 {
1226 rp = pool_elt_at_index (am->l2_arp_term_events_registrations, p[0]);
1227 pool_put (am->l2_arp_term_events_registrations, rp);
1228 hash_unset (am->l2_arp_term_events_registration_hash,
1229 mp->client_index);
1230 if (pool_elts (am->l2_arp_term_events_registrations) == 0)
1231 l2_arp_term_set_publisher_node (false);
1232 goto reply;
1233 }
1234 }
1235 if (mp->enable == 0)
1236 {
1237 clib_warning ("pid %d: already disabled...", mp->pid);
1238 rv = VNET_API_ERROR_INVALID_REGISTRATION;
1239 goto reply;
1240 }
1241 pool_get (am->l2_arp_term_events_registrations, rp);
1242 rp->client_index = mp->client_index;
1243 rp->client_pid = mp->pid;
1244 hash_set (am->l2_arp_term_events_registration_hash, rp->client_index,
1245 rp - am->l2_arp_term_events_registrations);
1246 l2_arp_term_set_publisher_node (true);
1247
1248reply:
1249 REPLY_MACRO (VL_API_WANT_L2_ARP_TERM_EVENTS_REPLY);
1250}
1251
1252static clib_error_t *
1253want_l2_arp_term_events_reaper (u32 client_index)
1254{
1255 vpe_client_registration_t *rp;
1256 vpe_api_main_t *am;
1257 uword *p;
1258
1259 am = &vpe_api_main;
1260
1261 /* remove from the registration hash */
1262 p = hash_get (am->l2_arp_term_events_registration_hash, client_index);
1263
1264 if (p)
1265 {
1266 rp = pool_elt_at_index (am->l2_arp_term_events_registrations, p[0]);
1267 pool_put (am->l2_arp_term_events_registrations, rp);
1268 hash_unset (am->l2_arp_term_events_registration_hash, client_index);
1269 if (pool_elts (am->l2_arp_term_events_registrations) == 0)
1270 l2_arp_term_set_publisher_node (false);
1271 }
1272 return (NULL);
1273}
1274
1275VL_MSG_API_REAPER_FUNCTION (want_l2_arp_term_events_reaper);
1276
Filip Tehlar5a9d2a12021-06-22 21:20:29 +00001277#include <vnet/l2/l2.api.c>
Matus Fabianf468e232016-12-02 06:00:53 -08001278static clib_error_t *
1279l2_api_hookup (vlib_main_t * vm)
1280{
Dave Barach39d69112019-11-27 11:42:13 -05001281 api_main_t *am = vlibapi_get_main ();
Matus Fabianf468e232016-12-02 06:00:53 -08001282
Steven Luongc5fa2b82019-04-25 11:19:49 -07001283 /* Mark VL_API_BRIDGE_DOMAIN_DUMP as mp safe */
1284 am->is_mp_safe[VL_API_BRIDGE_DOMAIN_DUMP] = 1;
1285
Matus Fabianf468e232016-12-02 06:00:53 -08001286 /*
1287 * Set up the (msg_name, crc, message-id) table
1288 */
Filip Tehlar5a9d2a12021-06-22 21:20:29 +00001289 REPLY_MSG_ID_BASE = setup_message_id_table ();
Matus Fabianf468e232016-12-02 06:00:53 -08001290
1291 return 0;
1292}
1293
1294VLIB_API_INIT_FUNCTION (l2_api_hookup);
1295
1296/*
1297 * fd.io coding-style-patch-verification: ON
1298 *
1299 * Local Variables:
1300 * eval: (c-set-style "gnu")
1301 * End:
1302 */