blob: 035542d298d9ad3e3c4bb6fbcd70e1dbc7991692 [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.
Laszlo Kiraly0f8f4352022-09-16 13:20:07 +02006 * Copyright (c) 2022 Nordix Foundation.
Matus Fabianf468e232016-12-02 06:00:53 -08007 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at:
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *------------------------------------------------------------------
19 */
20
21#include <vnet/vnet.h>
22#include <vlibmemory/api.h>
23
24#include <vnet/interface.h>
25#include <vnet/api_errno.h>
26#include <vnet/l2/l2_input.h>
Pavel Kotucek0f971d82017-01-03 10:48:54 +010027#include <vnet/l2/l2_fib.h>
Pavel Kotucekadec5872017-01-25 08:50:53 +010028#include <vnet/l2/l2_vtr.h>
John Lo8d00fff2017-08-03 00:35:36 -040029#include <vnet/l2/l2_learn.h>
Neale Rannsb4743802018-09-05 09:13:57 -070030#include <vnet/l2/l2_bd.h>
Neale Ranns192b13f2019-03-15 02:16:20 -070031#include <vnet/l2/l2_bvi.h>
Neale Rannscbe25aa2019-09-30 10:53:31 +000032#include <vnet/l2/l2_arp_term.h>
Neale Ranns4d5b9172018-10-24 02:57:49 -070033#include <vnet/ip/ip_types_api.h>
34#include <vnet/ethernet/ethernet_types_api.h>
Matus Fabianf468e232016-12-02 06:00:53 -080035
Filip Tehlar5a9d2a12021-06-22 21:20:29 +000036#include <vnet/format_fns.h>
37#include <vnet/l2/l2.api_enum.h>
38#include <vnet/l2/l2.api_types.h>
Matus Fabianf468e232016-12-02 06:00:53 -080039
Filip Tehlar5a9d2a12021-06-22 21:20:29 +000040#define REPLY_MSG_ID_BASE l2input_main.msg_id_base
Matus Fabianf468e232016-12-02 06:00:53 -080041#include <vlibapi/api_helper_macros.h>
42
Matus Fabianf468e232016-12-02 06:00:53 -080043static void
Florin Coras6c4dae22018-01-09 06:39:23 -080044send_l2_xconnect_details (vl_api_registration_t * reg, u32 context,
Matus Fabianf468e232016-12-02 06:00:53 -080045 u32 rx_sw_if_index, u32 tx_sw_if_index)
46{
47 vl_api_l2_xconnect_details_t *mp;
48
49 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -040050 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar5a9d2a12021-06-22 21:20:29 +000051 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_L2_XCONNECT_DETAILS);
Matus Fabianf468e232016-12-02 06:00:53 -080052 mp->context = context;
53 mp->rx_sw_if_index = htonl (rx_sw_if_index);
54 mp->tx_sw_if_index = htonl (tx_sw_if_index);
55
Florin Coras6c4dae22018-01-09 06:39:23 -080056 vl_api_send_msg (reg, (u8 *) mp);
Matus Fabianf468e232016-12-02 06:00:53 -080057}
58
59static void
60vl_api_l2_xconnect_dump_t_handler (vl_api_l2_xconnect_dump_t * mp)
61{
Florin Coras6c4dae22018-01-09 06:39:23 -080062 vl_api_registration_t *reg;
Matus Fabianf468e232016-12-02 06:00:53 -080063 l2input_main_t *l2im = &l2input_main;
Steven Luong16f08652021-02-08 23:48:30 -080064 u32 sw_if_index;
Matus Fabianf468e232016-12-02 06:00:53 -080065 l2_input_config_t *config;
66
Florin Coras6c4dae22018-01-09 06:39:23 -080067 reg = vl_api_client_index_to_registration (mp->client_index);
68 if (!reg)
Matus Fabianf468e232016-12-02 06:00:53 -080069 return;
70
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}
79
Pavel Kotucek0f971d82017-01-03 10:48:54 +010080static void
81vl_api_l2_fib_clear_table_t_handler (vl_api_l2_fib_clear_table_t * mp)
82{
83 int rv = 0;
84 vl_api_l2_fib_clear_table_reply_t *rmp;
85
Eyal Bari7537e712017-04-27 14:07:55 +030086 /* Clear all MACs including static MACs */
87 l2fib_clear_table ();
Pavel Kotucek0f971d82017-01-03 10:48:54 +010088
89 REPLY_MACRO (VL_API_L2_FIB_CLEAR_TABLE_REPLY);
90}
91
92static void
93send_l2fib_table_entry (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -080094 vl_api_registration_t * reg,
Pavel Kotucek0f971d82017-01-03 10:48:54 +010095 l2fib_entry_key_t * l2fe_key,
96 l2fib_entry_result_t * l2fe_res, u32 context)
97{
Ole Troan01384fe2017-05-12 11:55:35 +020098 vl_api_l2_fib_table_details_t *mp;
Pavel Kotucek0f971d82017-01-03 10:48:54 +010099
100 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400101 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar5a9d2a12021-06-22 21:20:29 +0000102 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_L2_FIB_TABLE_DETAILS);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100103
104 mp->bd_id =
105 ntohl (l2input_main.bd_configs[l2fe_key->fields.bd_index].bd_id);
106
Jakub Grajciar145e3302019-10-24 13:52:42 +0200107 mac_address_encode ((mac_address_t *) l2fe_key->fields.mac, mp->mac);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100108 mp->sw_if_index = ntohl (l2fe_res->fields.sw_if_index);
Neale Ranns7341b6d2018-09-19 04:07:02 -0700109 mp->static_mac = (l2fib_entry_result_is_set_STATIC (l2fe_res) ? 1 : 0);
110 mp->filter_mac = (l2fib_entry_result_is_set_FILTER (l2fe_res) ? 1 : 0);
111 mp->bvi_mac = (l2fib_entry_result_is_set_BVI (l2fe_res) ? 1 : 0);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100112 mp->context = context;
113
Florin Coras6c4dae22018-01-09 06:39:23 -0800114 vl_api_send_msg (reg, (u8 *) mp);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100115}
116
117static void
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100118vl_api_l2_fib_table_dump_t_handler (vl_api_l2_fib_table_dump_t * mp)
119{
120 vpe_api_main_t *am = &vpe_api_main;
121 bd_main_t *bdm = &bd_main;
122 l2fib_entry_key_t *l2fe_key = NULL;
123 l2fib_entry_result_t *l2fe_res = NULL;
124 u32 ni, bd_id = ntohl (mp->bd_id);
125 u32 bd_index;
Florin Coras6c4dae22018-01-09 06:39:23 -0800126 vl_api_registration_t *reg;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100127 uword *p;
128
Florin Coras6c4dae22018-01-09 06:39:23 -0800129 reg = vl_api_client_index_to_registration (mp->client_index);
130 if (!reg)
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100131 return;
132
133 /* see l2fib_table_dump: ~0 means "any" */
134 if (bd_id == ~0)
135 bd_index = ~0;
136 else
137 {
138 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
139 if (p == 0)
140 return;
141
142 bd_index = p[0];
143 }
144
145 l2fib_table_dump (bd_index, &l2fe_key, &l2fe_res);
146
147 vec_foreach_index (ni, l2fe_key)
148 {
Florin Coras6c4dae22018-01-09 06:39:23 -0800149 send_l2fib_table_entry (am, reg, vec_elt_at_index (l2fe_key, ni),
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100150 vec_elt_at_index (l2fe_res, ni), mp->context);
151 }
152 vec_free (l2fe_key);
153 vec_free (l2fe_res);
154}
155
156static void
157vl_api_l2fib_add_del_t_handler (vl_api_l2fib_add_del_t * mp)
158{
159 bd_main_t *bdm = &bd_main;
160 l2input_main_t *l2im = &l2input_main;
161 vl_api_l2fib_add_del_reply_t *rmp;
162 int rv = 0;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100163 u32 bd_id = ntohl (mp->bd_id);
Eyal Bari31a71ab2017-06-25 14:42:33 +0300164 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100165
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100166 if (!p)
167 {
168 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
169 goto bad_sw_if_index;
170 }
Eyal Bari31a71ab2017-06-25 14:42:33 +0300171 u32 bd_index = p[0];
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100172
Jakub Grajciar145e3302019-10-24 13:52:42 +0200173 mac_address_t mac;
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200174
Jakub Grajciar145e3302019-10-24 13:52:42 +0200175 mac_address_decode (mp->mac, &mac);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100176 if (mp->is_add)
177 {
Eyal Bari31a71ab2017-06-25 14:42:33 +0300178 if (mp->filter_mac)
Jakub Grajciar145e3302019-10-24 13:52:42 +0200179 l2fib_add_filter_entry (mac.bytes, bd_index);
Eyal Bari31a71ab2017-06-25 14:42:33 +0300180 else
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100181 {
Neale Rannsb54d0812018-09-06 06:22:56 -0700182 l2fib_entry_result_flags_t flags = L2FIB_ENTRY_RESULT_FLAG_NONE;
Eyal Bari31a71ab2017-06-25 14:42:33 +0300183 u32 sw_if_index = ntohl (mp->sw_if_index);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100184 VALIDATE_SW_IF_INDEX (mp);
185 if (vec_len (l2im->configs) <= sw_if_index)
186 {
187 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
188 goto bad_sw_if_index;
189 }
190 else
191 {
192 l2_input_config_t *config;
193 config = vec_elt_at_index (l2im->configs, sw_if_index);
Neale Ranns47a3d992020-09-29 15:38:51 +0000194 if (!l2_input_is_bridge (config))
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100195 {
196 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
197 goto bad_sw_if_index;
198 }
199 }
Neale Rannsb54d0812018-09-06 06:22:56 -0700200 if (mp->static_mac)
201 flags |= L2FIB_ENTRY_RESULT_FLAG_STATIC;
202 if (mp->bvi_mac)
203 flags |= L2FIB_ENTRY_RESULT_FLAG_BVI;
Jakub Grajciar145e3302019-10-24 13:52:42 +0200204 l2fib_add_entry (mac.bytes, bd_index, sw_if_index, flags);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100205 }
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100206 }
207 else
208 {
John Lo7dbd7262018-05-31 10:25:18 -0400209 u32 sw_if_index = ntohl (mp->sw_if_index);
Jakub Grajciar145e3302019-10-24 13:52:42 +0200210 if (l2fib_del_entry (mac.bytes, bd_index, sw_if_index))
John Lo7dbd7262018-05-31 10:25:18 -0400211 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100212 }
213
214 BAD_SW_IF_INDEX_LABEL;
215
216 REPLY_MACRO (VL_API_L2FIB_ADD_DEL_REPLY);
217}
218
219static void
Jerome Tollet0f8d1002021-01-07 12:44:17 +0100220vl_api_want_l2_macs_events2_t_handler (vl_api_want_l2_macs_events2_t *mp)
John Lo8d00fff2017-08-03 00:35:36 -0400221{
222 int rv = 0;
Jerome Tollet0f8d1002021-01-07 12:44:17 +0100223 vl_api_want_l2_macs_events2_reply_t *rmp;
John Lo8d00fff2017-08-03 00:35:36 -0400224 l2learn_main_t *lm = &l2learn_main;
225 l2fib_main_t *fm = &l2fib_main;
226 u32 pid = ntohl (mp->pid);
John Lo8d00fff2017-08-03 00:35:36 -0400227
228 if (mp->enable_disable)
229 {
Jerome Tollet0f8d1002021-01-07 12:44:17 +0100230 if ((lm->client_pid == 0) || (lm->client_pid == pid))
John Lo8d00fff2017-08-03 00:35:36 -0400231 {
John Lo8d00fff2017-08-03 00:35:36 -0400232 if (mp->max_macs_in_event)
233 fm->max_macs_in_event = mp->max_macs_in_event * 10;
234 else
Jakub Grajciar145e3302019-10-24 13:52:42 +0200235 {
236 rv = VNET_API_ERROR_INVALID_VALUE;
237 goto exit;
238 }
John Lo8d00fff2017-08-03 00:35:36 -0400239
Jerome Tollet0f8d1002021-01-07 12:44:17 +0100240 /* if scan_delay was not set before */
241 if (fm->event_scan_delay == 0.0)
242 fm->event_scan_delay = (f64) (10) * 10e-3;
John Lo8d00fff2017-08-03 00:35:36 -0400243
Jerome Tollet0f8d1002021-01-07 12:44:17 +0100244 lm->client_pid = pid;
245 lm->client_index = mp->client_index;
John Lo8d00fff2017-08-03 00:35:36 -0400246 l2fib_flush_all_mac (vlib_get_main ());
247 }
248 else if (lm->client_pid != pid)
249 {
250 rv = VNET_API_ERROR_L2_MACS_EVENT_CLINET_PRESENT;
251 goto exit;
252 }
253 }
254 else if (lm->client_pid)
255 {
256 lm->client_pid = 0;
257 lm->client_index = 0;
Jerome Tollet0f8d1002021-01-07 12:44:17 +0100258 }
259
260exit:
261 REPLY_MACRO (VL_API_WANT_L2_MACS_EVENTS2_REPLY);
262}
263
264static void
265vl_api_want_l2_macs_events_t_handler (vl_api_want_l2_macs_events_t *mp)
266{
267 int rv = 0;
268 vl_api_want_l2_macs_events_reply_t *rmp;
269 l2learn_main_t *lm = &l2learn_main;
270 l2fib_main_t *fm = &l2fib_main;
271 u32 pid = ntohl (mp->pid);
272 u32 learn_limit = ntohl (mp->learn_limit);
273
274 if (mp->enable_disable)
275 {
276 if ((lm->client_pid == 0) || (lm->client_pid == pid))
277 {
278 if ((mp->max_macs_in_event == 0) || (mp->scan_delay == 0) ||
279 (learn_limit == 0) || (learn_limit > L2LEARN_DEFAULT_LIMIT))
280 {
281 rv = VNET_API_ERROR_INVALID_VALUE;
282 goto exit;
283 }
284 lm->client_pid = pid;
285 lm->client_index = mp->client_index;
286
287 fm->max_macs_in_event = mp->max_macs_in_event * 10;
288 fm->event_scan_delay = (f64) (mp->scan_delay) * 10e-3;
289
290 /* change learn limit and flush all learned MACs */
291 lm->global_learn_limit = learn_limit;
292 l2fib_flush_all_mac (vlib_get_main ());
293 }
294 else if (lm->client_pid != pid)
295 {
296 rv = VNET_API_ERROR_L2_MACS_EVENT_CLINET_PRESENT;
297 goto exit;
298 }
299 }
300 else if (lm->client_pid)
301 {
302 lm->client_pid = 0;
303 lm->client_index = 0;
304 if (learn_limit && (learn_limit <= L2LEARN_DEFAULT_LIMIT))
John Loe531f4c2017-08-22 09:16:50 -0400305 lm->global_learn_limit = learn_limit;
306 else
307 lm->global_learn_limit = L2LEARN_DEFAULT_LIMIT;
John Lo8d00fff2017-08-03 00:35:36 -0400308 }
309
310exit:
311 REPLY_MACRO (VL_API_WANT_L2_MACS_EVENTS_REPLY);
312}
313
314static void
Eyal Barif24991c2017-04-05 05:33:21 +0300315vl_api_l2fib_flush_int_t_handler (vl_api_l2fib_flush_int_t * mp)
316{
317 int rv = 0;
318 vlib_main_t *vm = vlib_get_main ();
319 vl_api_l2fib_flush_int_reply_t *rmp;
320
321 VALIDATE_SW_IF_INDEX (mp);
322
323 u32 sw_if_index = ntohl (mp->sw_if_index);
324 l2fib_flush_int_mac (vm, sw_if_index);
325
326 BAD_SW_IF_INDEX_LABEL;
327 REPLY_MACRO (VL_API_L2FIB_FLUSH_INT_REPLY);
328}
329
330static void
Eyal Bari7537e712017-04-27 14:07:55 +0300331vl_api_l2fib_flush_all_t_handler (vl_api_l2fib_flush_all_t * mp)
332{
333 int rv = 0;
334 vl_api_l2fib_flush_all_reply_t *rmp;
335
336 l2fib_flush_all_mac (vlib_get_main ());
337 REPLY_MACRO (VL_API_L2FIB_FLUSH_ALL_REPLY);
338}
339
340static void
Eyal Barif24991c2017-04-05 05:33:21 +0300341vl_api_l2fib_flush_bd_t_handler (vl_api_l2fib_flush_bd_t * mp)
342{
343 int rv = 0;
344 vlib_main_t *vm = vlib_get_main ();
345 bd_main_t *bdm = &bd_main;
346 vl_api_l2fib_flush_bd_reply_t *rmp;
347
348 u32 bd_id = ntohl (mp->bd_id);
349 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
350 if (p == 0)
351 {
352 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
353 goto out;
354 }
355 l2fib_flush_bd_mac (vm, *p);
356out:
357 REPLY_MACRO (VL_API_L2FIB_FLUSH_BD_REPLY);
358}
359
360static void
Jerome Tollet0f8d1002021-01-07 12:44:17 +0100361vl_api_l2fib_set_scan_delay_t_handler (vl_api_l2fib_set_scan_delay_t *mp)
362{
363 int rv = 0;
364 l2fib_main_t *fm = &l2fib_main;
365 vl_api_l2fib_set_scan_delay_reply_t *rmp;
366 u16 scan_delay = ntohs (mp->scan_delay);
367
368 if (mp->scan_delay)
369 {
370 fm->event_scan_delay = (f64) (scan_delay) *10e-3;
371 l2fib_flush_all_mac (vlib_get_main ());
372 }
373 else
374 {
375 rv = VNET_API_ERROR_INVALID_VALUE;
376 goto exit;
377 }
378
379exit:
380 REPLY_MACRO (VL_API_L2FIB_SET_SCAN_DELAY_REPLY);
381}
382
383static void
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100384vl_api_l2_flags_t_handler (vl_api_l2_flags_t * mp)
385{
386 vl_api_l2_flags_reply_t *rmp;
387 int rv = 0;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100388 u32 rbm = 0;
389
390 VALIDATE_SW_IF_INDEX (mp);
391
Eyal Barifead6702017-04-04 04:46:32 +0300392 u32 sw_if_index = ntohl (mp->sw_if_index);
John Lo8d00fff2017-08-03 00:35:36 -0400393 u32 flags = ntohl (mp->feature_bitmap);
394 u32 bitmap = 0;
395
396 if (flags & L2_LEARN)
397 bitmap |= L2INPUT_FEAT_LEARN;
398
399 if (flags & L2_FWD)
400 bitmap |= L2INPUT_FEAT_FWD;
401
402 if (flags & L2_FLOOD)
403 bitmap |= L2INPUT_FEAT_FLOOD;
404
405 if (flags & L2_UU_FLOOD)
406 bitmap |= L2INPUT_FEAT_UU_FLOOD;
407
408 if (flags & L2_ARP_TERM)
409 bitmap |= L2INPUT_FEAT_ARP_TERM;
410
411 rbm = l2input_intf_bitmap_enable (sw_if_index, bitmap, mp->is_set);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100412
413 BAD_SW_IF_INDEX_LABEL;
414
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100415 REPLY_MACRO2(VL_API_L2_FLAGS_REPLY,
416 ({
417 rmp->resulting_feature_bitmap = ntohl(rbm);
418 }));
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100419}
420
421static void
Jerome Tollet5f93e3b2020-12-18 09:44:24 +0100422vl_api_bridge_domain_set_default_learn_limit_t_handler (
423 vl_api_bridge_domain_set_default_learn_limit_t *mp)
424{
425 vl_api_bridge_domain_set_default_learn_limit_reply_t *rmp;
426 int rv = 0;
427
428 l2learn_main.bd_default_learn_limit = ntohl (mp->learn_limit);
429 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_SET_DEFAULT_LEARN_LIMIT_REPLY);
430}
431
432static void
433vl_api_bridge_domain_set_learn_limit_t_handler (
434 vl_api_bridge_domain_set_learn_limit_t *mp)
435{
436 vlib_main_t *vm = vlib_get_main ();
437 bd_main_t *bdm = &bd_main;
438 vl_api_bridge_domain_set_learn_limit_reply_t *rmp;
439 int rv = 0;
440 u32 bd_id = ntohl (mp->bd_id);
441 uword *p;
442
443 if (bd_id == 0)
444 {
445 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
446 goto out;
447 }
448
449 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
450 if (p == 0)
451 {
452 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
453 goto out;
454 }
455 bd_set_learn_limit (vm, *p, ntohl (mp->learn_limit));
456out:
457 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_SET_LEARN_LIMIT_REPLY);
458}
459
460static void
Eyal Barifead6702017-04-04 04:46:32 +0300461vl_api_bridge_domain_set_mac_age_t_handler (vl_api_bridge_domain_set_mac_age_t
462 * mp)
463{
464 vlib_main_t *vm = vlib_get_main ();
465 bd_main_t *bdm = &bd_main;
466 vl_api_bridge_domain_set_mac_age_reply_t *rmp;
467 int rv = 0;
468 u32 bd_id = ntohl (mp->bd_id);
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500469 uword *p;
470
471 if (bd_id == 0)
472 {
473 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
474 goto out;
475 }
476
477 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
Eyal Barifead6702017-04-04 04:46:32 +0300478 if (p == 0)
479 {
480 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
481 goto out;
482 }
483 bd_set_mac_age (vm, *p, mp->mac_age);
484out:
485 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_SET_MAC_AGE_REPLY);
486}
487
488static void
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100489vl_api_bridge_domain_add_del_t_handler (vl_api_bridge_domain_add_del_t * mp)
490{
Eyal Barib1352ed2017-04-07 23:14:17 +0300491 l2_bridge_domain_add_del_args_t a = {
492 .is_add = mp->is_add,
493 .flood = mp->flood,
494 .uu_flood = mp->uu_flood,
495 .forward = mp->forward,
496 .learn = mp->learn,
497 .arp_term = mp->arp_term,
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +0200498 .arp_ufwd = mp->arp_ufwd,
Eyal Barib1352ed2017-04-07 23:14:17 +0300499 .mac_age = mp->mac_age,
500 .bd_id = ntohl (mp->bd_id),
Jerome Tollet48304142017-09-05 12:13:22 +0100501 .bd_tag = mp->bd_tag
Eyal Barib1352ed2017-04-07 23:14:17 +0300502 };
503
504 int rv = bd_add_del (&a);
505
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100506 vl_api_bridge_domain_add_del_reply_t *rmp;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100507 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_ADD_DEL_REPLY);
508}
509
510static void
Laszlo Kiraly0f8f4352022-09-16 13:20:07 +0200511vl_api_bridge_domain_add_del_v2_t_handler (
512 vl_api_bridge_domain_add_del_v2_t *mp)
513{
514 vl_api_bridge_domain_add_del_v2_reply_t *rmp;
515 u32 bd_id = ntohl (mp->bd_id);
516 int rv = 0;
517
518 if ((~0 == bd_id) && (mp->is_add))
519 bd_id = bd_get_unused_id ();
520
521 if ((~0 == bd_id) && (mp->is_add))
522 rv = VNET_API_ERROR_EAGAIN;
523 else
524 {
525 l2_bridge_domain_add_del_args_t a = { .is_add = mp->is_add,
526 .flood = mp->flood,
527 .uu_flood = mp->uu_flood,
528 .forward = mp->forward,
529 .learn = mp->learn,
530 .arp_term = mp->arp_term,
531 .arp_ufwd = mp->arp_ufwd,
532 .mac_age = mp->mac_age,
533 .bd_id = bd_id,
534 .bd_tag = mp->bd_tag };
535 rv = bd_add_del (&a);
536 }
537 REPLY_MACRO2 (VL_API_BRIDGE_DOMAIN_ADD_DEL_V2_REPLY,
538 ({ rmp->bd_id = htonl (bd_id); }));
539}
540
541static void
Ole Troan01384fe2017-05-12 11:55:35 +0200542send_bridge_domain_details (l2input_main_t * l2im,
Florin Coras6c4dae22018-01-09 06:39:23 -0800543 vl_api_registration_t * reg,
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100544 l2_bridge_domain_t * bd_config,
545 u32 n_sw_ifs, u32 context)
546{
547 vl_api_bridge_domain_details_t *mp;
Ole Troan01384fe2017-05-12 11:55:35 +0200548 l2_flood_member_t *m;
549 vl_api_bridge_domain_sw_if_t *sw_ifs;
550 l2_input_config_t *input_cfg;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100551
Ole Troan01384fe2017-05-12 11:55:35 +0200552 mp = vl_msg_api_alloc (sizeof (*mp) +
553 (n_sw_ifs * sizeof (vl_api_bridge_domain_sw_if_t)));
Dave Barachb7b92992018-10-17 10:38:51 -0400554 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar5a9d2a12021-06-22 21:20:29 +0000555 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_BRIDGE_DOMAIN_DETAILS);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100556 mp->bd_id = ntohl (bd_config->bd_id);
557 mp->flood = bd_feature_flood (bd_config);
558 mp->uu_flood = bd_feature_uu_flood (bd_config);
559 mp->forward = bd_feature_forward (bd_config);
560 mp->learn = bd_feature_learn (bd_config);
561 mp->arp_term = bd_feature_arp_term (bd_config);
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +0200562 mp->arp_ufwd = bd_feature_arp_ufwd (bd_config);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100563 mp->bvi_sw_if_index = ntohl (bd_config->bvi_sw_if_index);
Neale Rannsb4743802018-09-05 09:13:57 -0700564 mp->uu_fwd_sw_if_index = ntohl (bd_config->uu_fwd_sw_if_index);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100565 mp->mac_age = bd_config->mac_age;
Jerome Tollet48304142017-09-05 12:13:22 +0100566 if (bd_config->bd_tag)
567 {
568 strncpy ((char *) mp->bd_tag, (char *) bd_config->bd_tag,
569 ARRAY_LEN (mp->bd_tag) - 1);
570 mp->bd_tag[ARRAY_LEN (mp->bd_tag) - 1] = 0;
571 }
572
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100573 mp->context = context;
574
Ole Troan01384fe2017-05-12 11:55:35 +0200575 sw_ifs = (vl_api_bridge_domain_sw_if_t *) mp->sw_if_details;
576 vec_foreach (m, bd_config->members)
577 {
578 sw_ifs->sw_if_index = ntohl (m->sw_if_index);
579 input_cfg = vec_elt_at_index (l2im->configs, m->sw_if_index);
580 sw_ifs->shg = input_cfg->shg;
581 sw_ifs++;
582 mp->n_sw_ifs++;
583 }
584 mp->n_sw_ifs = htonl (mp->n_sw_ifs);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100585
Florin Coras6c4dae22018-01-09 06:39:23 -0800586 vl_api_send_msg (reg, (u8 *) mp);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100587}
588
589static void
590vl_api_bridge_domain_dump_t_handler (vl_api_bridge_domain_dump_t * mp)
591{
592 bd_main_t *bdm = &bd_main;
593 l2input_main_t *l2im = &l2input_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800594 vl_api_registration_t *reg;
Jakub Grajciar145e3302019-10-24 13:52:42 +0200595 u32 bd_id, bd_index, end, filter_sw_if_index;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100596
Florin Coras6c4dae22018-01-09 06:39:23 -0800597 reg = vl_api_client_index_to_registration (mp->client_index);
598 if (!reg)
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100599 return;
600
Jakub Grajciar145e3302019-10-24 13:52:42 +0200601 filter_sw_if_index = ntohl (mp->sw_if_index);
602 if (filter_sw_if_index != ~0)
603 return; /* UNIMPLEMENTED */
604
Florin Coras6c4dae22018-01-09 06:39:23 -0800605 bd_id = ntohl (mp->bd_id);
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500606 if (bd_id == 0)
607 return;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100608
Eyal Bari259fca72017-05-14 10:38:39 +0300609 if (bd_id == ~0)
610 bd_index = 0, end = vec_len (l2im->bd_configs);
611 else
612 {
613 bd_index = bd_find_index (bdm, bd_id);
614 if (bd_index == ~0)
615 return;
616
617 end = bd_index + 1;
618 }
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500619
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100620 for (; bd_index < end; bd_index++)
621 {
Eyal Bari259fca72017-05-14 10:38:39 +0300622 l2_bridge_domain_t *bd_config =
623 l2input_bd_config_from_index (l2im, bd_index);
Dave Barach11fb09e2020-08-06 12:10:09 -0400624 /* skip placeholder bd_id 0 */
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100625 if (bd_config && (bd_config->bd_id > 0))
Florin Coras6c4dae22018-01-09 06:39:23 -0800626 send_bridge_domain_details (l2im, reg, bd_config,
Ole Troan01384fe2017-05-12 11:55:35 +0200627 vec_len (bd_config->members),
628 mp->context);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100629 }
630}
631
Neale Rannsb4743802018-09-05 09:13:57 -0700632static bd_flags_t
633bd_flags_decode (vl_api_bd_flags_t v)
634{
635 bd_flags_t f = L2_NONE;
636
637 v = ntohl (v);
638
639 if (v & BRIDGE_API_FLAG_LEARN)
640 f |= L2_LEARN;
641 if (v & BRIDGE_API_FLAG_FWD)
642 f |= L2_FWD;
643 if (v & BRIDGE_API_FLAG_FLOOD)
644 f |= L2_FLOOD;
645 if (v & BRIDGE_API_FLAG_UU_FLOOD)
646 f |= L2_UU_FLOOD;
647 if (v & BRIDGE_API_FLAG_ARP_TERM)
648 f |= L2_ARP_TERM;
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +0200649 if (v & BRIDGE_API_FLAG_ARP_UFWD)
650 f |= L2_ARP_UFWD;
Neale Rannsb4743802018-09-05 09:13:57 -0700651
652 return (f);
653}
654
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100655static void
656vl_api_bridge_flags_t_handler (vl_api_bridge_flags_t * mp)
657{
658 vlib_main_t *vm = vlib_get_main ();
659 bd_main_t *bdm = &bd_main;
660 vl_api_bridge_flags_reply_t *rmp;
Dave Barach98d6b612019-01-03 09:30:00 -0500661 u32 bitmap = 0;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100662 int rv = 0;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100663
Neale Rannsb4743802018-09-05 09:13:57 -0700664 bd_flags_t flags = bd_flags_decode (mp->flags);
Eyal Bari259fca72017-05-14 10:38:39 +0300665 u32 bd_id = ntohl (mp->bd_id);
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500666 if (bd_id == 0)
667 {
668 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
669 goto out;
670 }
671
Eyal Bari259fca72017-05-14 10:38:39 +0300672 u32 bd_index = bd_find_index (bdm, bd_id);
673 if (bd_index == ~0)
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100674 {
675 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
676 goto out;
677 }
678
Dave Barach98d6b612019-01-03 09:30:00 -0500679 bitmap = bd_set_flags (vm, bd_index, flags, mp->is_set);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100680
681out:
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100682 REPLY_MACRO2(VL_API_BRIDGE_FLAGS_REPLY,
683 ({
John Lo8d00fff2017-08-03 00:35:36 -0400684 rmp->resulting_feature_bitmap = ntohl(bitmap);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100685 }));
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100686}
Matus Fabianf468e232016-12-02 06:00:53 -0800687
Pavel Kotucekadec5872017-01-25 08:50:53 +0100688static void
689 vl_api_l2_interface_vlan_tag_rewrite_t_handler
690 (vl_api_l2_interface_vlan_tag_rewrite_t * mp)
691{
692 int rv = 0;
693 vl_api_l2_interface_vlan_tag_rewrite_reply_t *rmp;
694 vnet_main_t *vnm = vnet_get_main ();
695 vlib_main_t *vm = vlib_get_main ();
696 u32 vtr_op;
697
698 VALIDATE_SW_IF_INDEX (mp);
699
700 vtr_op = ntohl (mp->vtr_op);
701
702 /* The L2 code is unsuspicious */
703 switch (vtr_op)
704 {
705 case L2_VTR_DISABLED:
706 case L2_VTR_PUSH_1:
707 case L2_VTR_PUSH_2:
708 case L2_VTR_POP_1:
709 case L2_VTR_POP_2:
710 case L2_VTR_TRANSLATE_1_1:
711 case L2_VTR_TRANSLATE_1_2:
712 case L2_VTR_TRANSLATE_2_1:
713 case L2_VTR_TRANSLATE_2_2:
714 break;
715
716 default:
717 rv = VNET_API_ERROR_INVALID_VALUE;
718 goto bad_sw_if_index;
719 }
720
721 rv = l2vtr_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
722 ntohl (mp->push_dot1q), ntohl (mp->tag1),
723 ntohl (mp->tag2));
724
725 BAD_SW_IF_INDEX_LABEL;
726
727 REPLY_MACRO (VL_API_L2_INTERFACE_VLAN_TAG_REWRITE_REPLY);
728}
729
730static void
731 vl_api_l2_interface_pbb_tag_rewrite_t_handler
732 (vl_api_l2_interface_pbb_tag_rewrite_t * mp)
733{
734 vl_api_l2_interface_pbb_tag_rewrite_reply_t *rmp;
735 vnet_main_t *vnm = vnet_get_main ();
736 vlib_main_t *vm = vlib_get_main ();
737 u32 vtr_op;
738 int rv = 0;
Jakub Grajciar145e3302019-10-24 13:52:42 +0200739 mac_address_t b_dmac, b_smac;
Pavel Kotucekadec5872017-01-25 08:50:53 +0100740
741 VALIDATE_SW_IF_INDEX (mp);
742
743 vtr_op = ntohl (mp->vtr_op);
744
745 switch (vtr_op)
746 {
747 case L2_VTR_DISABLED:
748 case L2_VTR_PUSH_2:
749 case L2_VTR_POP_2:
750 case L2_VTR_TRANSLATE_2_1:
751 break;
752
753 default:
754 rv = VNET_API_ERROR_INVALID_VALUE;
755 goto bad_sw_if_index;
756 }
757
Jakub Grajciar145e3302019-10-24 13:52:42 +0200758 mac_address_decode (mp->b_dmac, &b_dmac);
759 mac_address_decode (mp->b_smac, &b_smac);
760
Pavel Kotucekadec5872017-01-25 08:50:53 +0100761 rv = l2pbb_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
Jakub Grajciar145e3302019-10-24 13:52:42 +0200762 b_dmac.bytes, b_smac.bytes, ntohs (mp->b_vlanid),
Pavel Kotucekadec5872017-01-25 08:50:53 +0100763 ntohl (mp->i_sid), ntohs (mp->outer_tag));
764
765 BAD_SW_IF_INDEX_LABEL;
766
767 REPLY_MACRO (VL_API_L2_INTERFACE_PBB_TAG_REWRITE_REPLY);
768}
769
Neale Rannsb8d44812017-11-10 06:53:54 -0800770static void
771 vl_api_sw_interface_set_l2_xconnect_t_handler
772 (vl_api_sw_interface_set_l2_xconnect_t * mp)
773{
774 vl_api_sw_interface_set_l2_xconnect_reply_t *rmp;
775 int rv = 0;
776 u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
777 u32 tx_sw_if_index = ntohl (mp->tx_sw_if_index);
778 vlib_main_t *vm = vlib_get_main ();
779 vnet_main_t *vnm = vnet_get_main ();
780
781 VALIDATE_RX_SW_IF_INDEX (mp);
782
783 if (mp->enable)
784 {
785 VALIDATE_TX_SW_IF_INDEX (mp);
786 rv = set_int_l2_mode (vm, vnm, MODE_L2_XC,
Neale Rannsb4743802018-09-05 09:13:57 -0700787 rx_sw_if_index, 0,
788 L2_BD_PORT_TYPE_NORMAL, 0, tx_sw_if_index);
Neale Rannsb8d44812017-11-10 06:53:54 -0800789 }
790 else
791 {
Neale Rannsb4743802018-09-05 09:13:57 -0700792 rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0,
793 L2_BD_PORT_TYPE_NORMAL, 0, 0);
Neale Rannsb8d44812017-11-10 06:53:54 -0800794 }
795
Alexander Chernavine178b272018-09-24 05:42:01 -0400796 switch (rv)
797 {
798 case MODE_ERROR_ETH:
799 rv = VNET_API_ERROR_NON_ETHERNET;
800 break;
801 case MODE_ERROR_BVI_DEF:
802 rv = VNET_API_ERROR_BD_ALREADY_HAS_BVI;
803 break;
804 }
805
Neale Rannsb8d44812017-11-10 06:53:54 -0800806 BAD_RX_SW_IF_INDEX_LABEL;
807 BAD_TX_SW_IF_INDEX_LABEL;
808
809 REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_XCONNECT_REPLY);
810}
811
Neale Rannsb4743802018-09-05 09:13:57 -0700812static int
813l2_bd_port_type_decode (vl_api_l2_port_type_t v, l2_bd_port_type_t * l)
814{
815 v = clib_net_to_host_u32 (v);
816
817 switch (v)
818 {
819 case L2_API_PORT_TYPE_NORMAL:
820 *l = L2_BD_PORT_TYPE_NORMAL;
821 return 0;
822 case L2_API_PORT_TYPE_BVI:
823 *l = L2_BD_PORT_TYPE_BVI;
824 return 0;
825 case L2_API_PORT_TYPE_UU_FWD:
826 *l = L2_BD_PORT_TYPE_UU_FWD;
827 return 0;
828 }
829
830 return (VNET_API_ERROR_INVALID_VALUE);
831}
832
Neale Rannsb8d44812017-11-10 06:53:54 -0800833static void
834 vl_api_sw_interface_set_l2_bridge_t_handler
835 (vl_api_sw_interface_set_l2_bridge_t * mp)
836{
837 bd_main_t *bdm = &bd_main;
838 vl_api_sw_interface_set_l2_bridge_reply_t *rmp;
839 int rv = 0;
840 vlib_main_t *vm = vlib_get_main ();
841 vnet_main_t *vnm = vnet_get_main ();
Neale Rannsb4743802018-09-05 09:13:57 -0700842 l2_bd_port_type_t pt;
Neale Rannsb8d44812017-11-10 06:53:54 -0800843
844 VALIDATE_RX_SW_IF_INDEX (mp);
845 u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
Neale Rannsb4743802018-09-05 09:13:57 -0700846 rv = l2_bd_port_type_decode (mp->port_type, &pt);
Neale Rannsb8d44812017-11-10 06:53:54 -0800847
Neale Rannsb4743802018-09-05 09:13:57 -0700848 if (0 != rv)
849 goto out;
Neale Rannsb8d44812017-11-10 06:53:54 -0800850 if (mp->enable)
851 {
852 VALIDATE_BD_ID (mp);
853 u32 bd_id = ntohl (mp->bd_id);
854 u32 bd_index = bd_find_or_add_bd_index (bdm, bd_id);
Neale Rannsb4743802018-09-05 09:13:57 -0700855
Neale Rannsb8d44812017-11-10 06:53:54 -0800856 rv = set_int_l2_mode (vm, vnm, MODE_L2_BRIDGE,
Neale Rannsb4743802018-09-05 09:13:57 -0700857 rx_sw_if_index, bd_index, pt, mp->shg, 0);
Neale Rannsb8d44812017-11-10 06:53:54 -0800858 }
859 else
860 {
Neale Rannsb4743802018-09-05 09:13:57 -0700861 rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0, pt, 0, 0);
Neale Rannsb8d44812017-11-10 06:53:54 -0800862 }
863
Alexander Chernavine178b272018-09-24 05:42:01 -0400864 switch (rv)
865 {
866 case MODE_ERROR_ETH:
867 rv = VNET_API_ERROR_NON_ETHERNET;
868 break;
869 case MODE_ERROR_BVI_DEF:
870 rv = VNET_API_ERROR_BD_ALREADY_HAS_BVI;
871 break;
872 }
873
Neale Rannsb8d44812017-11-10 06:53:54 -0800874 BAD_RX_SW_IF_INDEX_LABEL;
875 BAD_BD_ID_LABEL;
Neale Rannsb4743802018-09-05 09:13:57 -0700876out:
Neale Rannsb8d44812017-11-10 06:53:54 -0800877 REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_BRIDGE_REPLY);
878}
879
880static void
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200881send_bd_ip_mac_entry (vpe_api_main_t * am,
882 vl_api_registration_t * reg,
Neale Rannsbc764c82019-06-19 07:07:13 -0700883 u32 bd_id,
884 const ip46_address_t * ip,
885 ip46_type_t itype,
886 const mac_address_t * mac, u32 context)
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200887{
888 vl_api_bd_ip_mac_details_t *mp;
889
890 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400891 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar5a9d2a12021-06-22 21:20:29 +0000892 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_BD_IP_MAC_DETAILS);
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200893
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200894 mp->context = context;
Neale Rannsbc764c82019-06-19 07:07:13 -0700895 mp->entry.bd_id = ntohl (bd_id);
896
897 ip_address_encode (ip, itype, &mp->entry.ip);
898 mac_address_encode (mac, mp->entry.mac);
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200899
900 vl_api_send_msg (reg, (u8 *) mp);
901}
902
903static void
904vl_api_bd_ip_mac_dump_t_handler (vl_api_bd_ip_mac_dump_t * mp)
905{
906 vpe_api_main_t *am = &vpe_api_main;
907 bd_main_t *bdm = &bd_main;
908 l2_bridge_domain_t *bd_config;
909 u32 bd_id = ntohl (mp->bd_id);
Mohsin Kazmib24dfec2018-08-23 12:24:19 +0200910 u32 bd_index, start, end;
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200911 vl_api_registration_t *reg;
912 uword *p;
913
914 reg = vl_api_client_index_to_registration (mp->client_index);
915 if (!reg)
916 return;
917
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200918 /* see bd_id: ~0 means "any" */
919 if (bd_id == ~0)
Mohsin Kazmib24dfec2018-08-23 12:24:19 +0200920 {
921 start = 1;
922 end = vec_len (l2input_main.bd_configs);
923 }
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200924 else
925 {
926 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
927 if (p == 0)
928 return;
929
930 bd_index = p[0];
931 vec_validate (l2input_main.bd_configs, bd_index);
932 start = bd_index;
933 end = start + 1;
934 }
935
936 for (bd_index = start; bd_index < end; bd_index++)
937 {
938 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
939 if (bd_is_valid (bd_config))
940 {
941 ip4_address_t ip4_addr;
942 ip6_address_t *ip6_addr;
Neale Rannsbc764c82019-06-19 07:07:13 -0700943 mac_address_t mac;
944 u64 mac64;
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200945 bd_id = bd_config->bd_id;
946
Neale Rannsbc764c82019-06-19 07:07:13 -0700947 hash_foreach (ip4_addr.as_u32, mac64, bd_config->mac_by_ip4,
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200948 ({
Neale Rannsbc764c82019-06-19 07:07:13 -0700949 ip46_address_t ip = {
950 .ip4 = ip4_addr,
951 };
952 mac_address_from_u64(&mac, mac64);
953
954 send_bd_ip_mac_entry (am, reg, bd_id, &ip, IP46_TYPE_IP4,
955 &mac, mp->context);
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200956 }));
957
Neale Rannsbc764c82019-06-19 07:07:13 -0700958 hash_foreach_mem (ip6_addr, mac64, bd_config->mac_by_ip6,
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200959 ({
Neale Rannsbc764c82019-06-19 07:07:13 -0700960 ip46_address_t ip = {
961 .ip6 = *ip6_addr,
962 };
963 mac_address_from_u64(&mac, mac64);
964
965 send_bd_ip_mac_entry (am, reg, bd_id, &ip, IP46_TYPE_IP6,
966 &mac, mp->context);
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200967 }));
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200968 }
969 }
970}
971
972static void
Neale Rannsb8d44812017-11-10 06:53:54 -0800973vl_api_bd_ip_mac_add_del_t_handler (vl_api_bd_ip_mac_add_del_t * mp)
974{
Neale Ranns4d5b9172018-10-24 02:57:49 -0700975 ip46_address_t ip_addr = ip46_address_initializer;
Neale Rannsb8d44812017-11-10 06:53:54 -0800976 vl_api_bd_ip_mac_add_del_reply_t *rmp;
Neale Ranns4d5b9172018-10-24 02:57:49 -0700977 bd_main_t *bdm = &bd_main;
978 u32 bd_index, bd_id;
979 mac_address_t mac;
980 ip46_type_t type;
Neale Rannsb8d44812017-11-10 06:53:54 -0800981 int rv = 0;
Neale Rannsb8d44812017-11-10 06:53:54 -0800982 uword *p;
983
Neale Rannsbc764c82019-06-19 07:07:13 -0700984 bd_id = ntohl (mp->entry.bd_id);
Neale Ranns4d5b9172018-10-24 02:57:49 -0700985
Neale Rannsb8d44812017-11-10 06:53:54 -0800986 if (bd_id == 0)
987 {
988 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
989 goto out;
990 }
991
992 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
993 if (p == 0)
994 {
995 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
996 goto out;
997 }
Neale Rannsb8d44812017-11-10 06:53:54 -0800998 bd_index = p[0];
Neale Ranns4d5b9172018-10-24 02:57:49 -0700999
Neale Rannsbc764c82019-06-19 07:07:13 -07001000 type = ip_address_decode (&mp->entry.ip, &ip_addr);
1001 mac_address_decode (mp->entry.mac, &mac);
Neale Ranns4d5b9172018-10-24 02:57:49 -07001002
1003 if (bd_add_del_ip_mac (bd_index, type, &ip_addr, &mac, mp->is_add))
Neale Rannsb8d44812017-11-10 06:53:54 -08001004 rv = VNET_API_ERROR_UNSPECIFIED;
1005
1006out:
1007 REPLY_MACRO (VL_API_BD_IP_MAC_ADD_DEL_REPLY);
1008}
1009
John Loe26c81f2019-01-07 15:16:33 -05001010static void
1011vl_api_bd_ip_mac_flush_t_handler (vl_api_bd_ip_mac_flush_t * mp)
1012{
1013 vl_api_bd_ip_mac_flush_reply_t *rmp;
1014 bd_main_t *bdm = &bd_main;
1015 u32 bd_index, bd_id;
1016 int rv = 0;
1017 uword *p;
1018
1019 bd_id = ntohl (mp->bd_id);
1020
1021 if (bd_id == 0)
1022 {
1023 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
1024 goto out;
1025 }
1026
1027 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
1028 if (p == 0)
1029 {
1030 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1031 goto out;
1032 }
1033 bd_index = p[0];
1034
1035 bd_flush_ip_mac (bd_index);
1036
1037out:
1038 REPLY_MACRO (VL_API_BD_IP_MAC_FLUSH_REPLY);
1039}
1040
Neale Rannsb8d44812017-11-10 06:53:54 -08001041extern void l2_efp_filter_configure (vnet_main_t * vnet_main,
Neale Ranns6b9b41c2018-07-23 05:47:09 -04001042 u32 sw_if_index, u8 enable);
Neale Rannsb8d44812017-11-10 06:53:54 -08001043
1044static void
1045vl_api_l2_interface_efp_filter_t_handler (vl_api_l2_interface_efp_filter_t *
1046 mp)
1047{
1048 int rv;
1049 vl_api_l2_interface_efp_filter_reply_t *rmp;
1050 vnet_main_t *vnm = vnet_get_main ();
1051
Neale Ranns6b9b41c2018-07-23 05:47:09 -04001052 VALIDATE_SW_IF_INDEX (mp);
1053
Neale Rannsb8d44812017-11-10 06:53:54 -08001054 // enable/disable the feature
Neale Ranns6b9b41c2018-07-23 05:47:09 -04001055 l2_efp_filter_configure (vnm, ntohl (mp->sw_if_index), mp->enable_disable);
Neale Rannsb8d44812017-11-10 06:53:54 -08001056 rv = vnm->api_errno;
1057
Neale Ranns6b9b41c2018-07-23 05:47:09 -04001058 BAD_SW_IF_INDEX_LABEL;
Neale Rannsb8d44812017-11-10 06:53:54 -08001059 REPLY_MACRO (VL_API_L2_INTERFACE_EFP_FILTER_REPLY);
1060}
1061
1062static void
1063vl_api_l2_patch_add_del_t_handler (vl_api_l2_patch_add_del_t * mp)
1064{
1065 extern int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
1066 int is_add);
1067 vl_api_l2_patch_add_del_reply_t *rmp;
1068 int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
1069 int is_add);
1070 int rv = 0;
1071
1072 VALIDATE_RX_SW_IF_INDEX (mp);
1073 VALIDATE_TX_SW_IF_INDEX (mp);
1074
1075 rv = vnet_l2_patch_add_del (ntohl (mp->rx_sw_if_index),
1076 ntohl (mp->tx_sw_if_index),
1077 (int) (mp->is_add != 0));
1078
1079 BAD_RX_SW_IF_INDEX_LABEL;
1080 BAD_TX_SW_IF_INDEX_LABEL;
1081
1082 REPLY_MACRO (VL_API_L2_PATCH_ADD_DEL_REPLY);
1083}
1084
1085static void
1086vl_api_sw_interface_set_vpath_t_handler (vl_api_sw_interface_set_vpath_t * mp)
1087{
1088 vl_api_sw_interface_set_vpath_reply_t *rmp;
1089 int rv = 0;
1090 u32 sw_if_index = ntohl (mp->sw_if_index);
1091
1092 VALIDATE_SW_IF_INDEX (mp);
1093
1094 l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_VPATH, mp->enable);
1095 vnet_feature_enable_disable ("ip4-unicast", "vpath-input-ip4",
1096 sw_if_index, mp->enable, 0, 0);
1097 vnet_feature_enable_disable ("ip4-multicast", "vpath-input-ip4",
1098 sw_if_index, mp->enable, 0, 0);
1099 vnet_feature_enable_disable ("ip6-unicast", "vpath-input-ip6",
1100 sw_if_index, mp->enable, 0, 0);
1101 vnet_feature_enable_disable ("ip6-multicast", "vpath-input-ip6",
1102 sw_if_index, mp->enable, 0, 0);
1103
1104 BAD_SW_IF_INDEX_LABEL;
1105
1106 REPLY_MACRO (VL_API_SW_INTERFACE_SET_VPATH_REPLY);
1107}
1108
Neale Ranns192b13f2019-03-15 02:16:20 -07001109static void
1110vl_api_bvi_create_t_handler (vl_api_bvi_create_t * mp)
1111{
1112 vl_api_bvi_create_reply_t *rmp;
1113 mac_address_t mac;
1114 u32 sw_if_index;
1115 int rv;
1116
1117 mac_address_decode (mp->mac, &mac);
1118
1119 rv = l2_bvi_create (ntohl (mp->user_instance), &mac, &sw_if_index);
1120
Neale Ranns192b13f2019-03-15 02:16:20 -07001121 REPLY_MACRO2(VL_API_BVI_CREATE_REPLY,
1122 ({
1123 rmp->sw_if_index = ntohl (sw_if_index);
1124 }));
Neale Ranns192b13f2019-03-15 02:16:20 -07001125}
1126
1127static void
1128vl_api_bvi_delete_t_handler (vl_api_bvi_delete_t * mp)
1129{
1130 vl_api_bvi_delete_reply_t *rmp;
1131 int rv;
1132
1133 rv = l2_bvi_delete (ntohl (mp->sw_if_index));
1134
1135 REPLY_MACRO (VL_API_BVI_DELETE_REPLY);
1136}
1137
Neale Rannscbe25aa2019-09-30 10:53:31 +00001138static bool
1139l2_arp_term_publish_event_is_equal (const l2_arp_term_publish_event_t * e1,
1140 const l2_arp_term_publish_event_t * e2)
1141{
1142 if (e1 == NULL || e2 == NULL)
1143 return false;
1144 return (ip46_address_is_equal (&e1->ip, &e2->ip) &&
1145 (e1->sw_if_index == e2->sw_if_index) &&
1146 (mac_address_equal (&e1->mac, &e2->mac)));
1147}
1148
1149static uword
1150l2_arp_term_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1151 vlib_frame_t * f)
1152{
1153 /* These cross the longjmp boundary (vlib_process_wait_for_event)
1154 * and need to be volatile - to prevent them from being optimized into
1155 * a register - which could change during suspension */
1156 volatile f64 last = vlib_time_now (vm);
1157 volatile l2_arp_term_publish_event_t last_event = { };
1158
1159 l2_arp_term_main_t *l2am = &l2_arp_term_main;
1160
1161 while (1)
1162 {
1163 uword event_type = L2_ARP_TERM_EVENT_PUBLISH;
1164 vpe_client_registration_t *reg;
1165 f64 now;
1166
1167 vlib_process_wait_for_event (vm);
1168
1169 vlib_process_get_event_data (vm, &event_type);
1170 now = vlib_time_now (vm);
1171
1172 if (event_type == L2_ARP_TERM_EVENT_PUBLISH)
1173 {
1174 l2_arp_term_publish_event_t *event;
1175
1176 vec_foreach (event, l2am->publish_events)
1177 {
1178 /* dampen duplicate events - cast away volatile */
1179 if (l2_arp_term_publish_event_is_equal
1180 (event, (l2_arp_term_publish_event_t *) & last_event) &&
1181 (now - last) < 10.0)
1182 {
1183 continue;
1184 }
1185 last_event = *event;
1186 last = now;
1187
Damjan Marionb2c31b62020-12-13 21:47:40 +01001188 pool_foreach (reg, vpe_api_main.l2_arp_term_events_registrations)
Steven Luonge46bd4c2021-02-15 09:25:10 -08001189 {
1190 vl_api_registration_t *vl_reg;
1191 vl_reg =
1192 vl_api_client_index_to_registration (reg->client_index);
1193 ALWAYS_ASSERT (vl_reg != NULL);
Neale Rannscbe25aa2019-09-30 10:53:31 +00001194
Steven Luonge46bd4c2021-02-15 09:25:10 -08001195 if (vl_reg && vl_api_can_send_msg (vl_reg))
1196 {
1197 vl_api_l2_arp_term_event_t *vevent;
1198 vevent = vl_msg_api_alloc (sizeof *vevent);
1199 clib_memset (vevent, 0, sizeof *vevent);
Filip Tehlar5a9d2a12021-06-22 21:20:29 +00001200 vevent->_vl_msg_id =
1201 htons (REPLY_MSG_ID_BASE + VL_API_L2_ARP_TERM_EVENT);
Steven Luonge46bd4c2021-02-15 09:25:10 -08001202 vevent->client_index = reg->client_index;
1203 vevent->pid = reg->client_pid;
1204 ip_address_encode (&event->ip, event->type, &vevent->ip);
1205 vevent->sw_if_index = htonl (event->sw_if_index);
1206 mac_address_encode (&event->mac, vevent->mac);
1207 vl_api_send_msg (vl_reg, (u8 *) vevent);
1208 }
1209 }
Neale Rannscbe25aa2019-09-30 10:53:31 +00001210 }
1211 vec_reset_length (l2am->publish_events);
1212 }
1213 }
1214
1215 return 0;
1216}
1217
Neale Rannscbe25aa2019-09-30 10:53:31 +00001218VLIB_REGISTER_NODE (l2_arp_term_process_node) = {
1219 .function = l2_arp_term_process,
1220 .type = VLIB_NODE_TYPE_PROCESS,
1221 .name = "l2-arp-term-publisher",
1222};
Neale Rannscbe25aa2019-09-30 10:53:31 +00001223
1224static void
1225vl_api_want_l2_arp_term_events_t_handler (vl_api_want_l2_arp_term_events_t *
1226 mp)
1227{
1228 vl_api_want_l2_arp_term_events_reply_t *rmp;
1229 vpe_api_main_t *am = &vpe_api_main;
1230 vpe_client_registration_t *rp;
1231 int rv = 0;
1232 uword *p;
1233
1234 p = hash_get (am->l2_arp_term_events_registration_hash, mp->client_index);
1235
1236 if (p)
1237 {
1238 if (mp->enable)
1239 {
1240 clib_warning ("pid %d: already enabled...", mp->pid);
1241 rv = VNET_API_ERROR_INVALID_REGISTRATION;
1242 goto reply;
1243 }
1244 else
1245 {
1246 rp = pool_elt_at_index (am->l2_arp_term_events_registrations, p[0]);
1247 pool_put (am->l2_arp_term_events_registrations, rp);
1248 hash_unset (am->l2_arp_term_events_registration_hash,
1249 mp->client_index);
1250 if (pool_elts (am->l2_arp_term_events_registrations) == 0)
1251 l2_arp_term_set_publisher_node (false);
1252 goto reply;
1253 }
1254 }
1255 if (mp->enable == 0)
1256 {
1257 clib_warning ("pid %d: already disabled...", mp->pid);
1258 rv = VNET_API_ERROR_INVALID_REGISTRATION;
1259 goto reply;
1260 }
1261 pool_get (am->l2_arp_term_events_registrations, rp);
1262 rp->client_index = mp->client_index;
1263 rp->client_pid = mp->pid;
1264 hash_set (am->l2_arp_term_events_registration_hash, rp->client_index,
1265 rp - am->l2_arp_term_events_registrations);
1266 l2_arp_term_set_publisher_node (true);
1267
1268reply:
1269 REPLY_MACRO (VL_API_WANT_L2_ARP_TERM_EVENTS_REPLY);
1270}
1271
1272static clib_error_t *
1273want_l2_arp_term_events_reaper (u32 client_index)
1274{
1275 vpe_client_registration_t *rp;
1276 vpe_api_main_t *am;
1277 uword *p;
1278
1279 am = &vpe_api_main;
1280
1281 /* remove from the registration hash */
1282 p = hash_get (am->l2_arp_term_events_registration_hash, client_index);
1283
1284 if (p)
1285 {
1286 rp = pool_elt_at_index (am->l2_arp_term_events_registrations, p[0]);
1287 pool_put (am->l2_arp_term_events_registrations, rp);
1288 hash_unset (am->l2_arp_term_events_registration_hash, client_index);
1289 if (pool_elts (am->l2_arp_term_events_registrations) == 0)
1290 l2_arp_term_set_publisher_node (false);
1291 }
1292 return (NULL);
1293}
1294
1295VL_MSG_API_REAPER_FUNCTION (want_l2_arp_term_events_reaper);
1296
Filip Tehlar5a9d2a12021-06-22 21:20:29 +00001297#include <vnet/l2/l2.api.c>
Matus Fabianf468e232016-12-02 06:00:53 -08001298static clib_error_t *
1299l2_api_hookup (vlib_main_t * vm)
1300{
Dave Barach39d69112019-11-27 11:42:13 -05001301 api_main_t *am = vlibapi_get_main ();
Matus Fabianf468e232016-12-02 06:00:53 -08001302
Matus Fabianf468e232016-12-02 06:00:53 -08001303 /*
1304 * Set up the (msg_name, crc, message-id) table
1305 */
Filip Tehlar5a9d2a12021-06-22 21:20:29 +00001306 REPLY_MSG_ID_BASE = setup_message_id_table ();
Matus Fabianf468e232016-12-02 06:00:53 -08001307
Vladislav Grishenkoe7c57c42023-03-19 02:57:01 +05001308 /* Mark VL_API_BRIDGE_DOMAIN_DUMP as mp safe */
1309 vl_api_set_msg_thread_safe (
1310 am, REPLY_MSG_ID_BASE + VL_API_BRIDGE_DOMAIN_DUMP, 1);
1311
Matus Fabianf468e232016-12-02 06:00:53 -08001312 return 0;
1313}
1314
1315VLIB_API_INIT_FUNCTION (l2_api_hookup);
1316
1317/*
1318 * fd.io coding-style-patch-verification: ON
1319 *
1320 * Local Variables:
1321 * eval: (c-set-style "gnu")
1322 * End:
1323 */