blob: 70d6eeff424f3a5b8f754ac337f37ff1241c9b98 [file] [log] [blame]
Matus Fabianf468e232016-12-02 06:00:53 -08001/*
2 *------------------------------------------------------------------
3 * l2_api.c - layer 2 forwarding api
4 *
5 * Copyright (c) 2016 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
18 */
19
20#include <vnet/vnet.h>
21#include <vlibmemory/api.h>
22
23#include <vnet/interface.h>
24#include <vnet/api_errno.h>
25#include <vnet/l2/l2_input.h>
Pavel Kotucek0f971d82017-01-03 10:48:54 +010026#include <vnet/l2/l2_fib.h>
Pavel Kotucekadec5872017-01-25 08:50:53 +010027#include <vnet/l2/l2_vtr.h>
John Lo8d00fff2017-08-03 00:35:36 -040028#include <vnet/l2/l2_learn.h>
Neale Rannsb4743802018-09-05 09:13:57 -070029#include <vnet/l2/l2_bd.h>
Neale Ranns192b13f2019-03-15 02:16:20 -070030#include <vnet/l2/l2_bvi.h>
Neale Rannscbe25aa2019-09-30 10:53:31 +000031#include <vnet/l2/l2_arp_term.h>
Neale Ranns4d5b9172018-10-24 02:57:49 -070032#include <vnet/ip/ip_types_api.h>
33#include <vnet/ethernet/ethernet_types_api.h>
Matus Fabianf468e232016-12-02 06:00:53 -080034
35#include <vnet/vnet_msg_enum.h>
36
37#define vl_typedefs /* define message structures */
38#include <vnet/vnet_all_api_h.h>
39#undef vl_typedefs
40
41#define vl_endianfun /* define message structures */
42#include <vnet/vnet_all_api_h.h>
43#undef vl_endianfun
44
45/* instantiate all the print functions we know about */
46#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
47#define vl_printfun
48#include <vnet/vnet_all_api_h.h>
49#undef vl_printfun
50
51#include <vlibapi/api_helper_macros.h>
52
Jerome Tollet5f93e3b2020-12-18 09:44:24 +010053#define foreach_vpe_api_msg \
54 _ (L2_XCONNECT_DUMP, l2_xconnect_dump) \
55 _ (L2_FIB_CLEAR_TABLE, l2_fib_clear_table) \
56 _ (L2_FIB_TABLE_DUMP, l2_fib_table_dump) \
57 _ (L2FIB_FLUSH_ALL, l2fib_flush_all) \
58 _ (L2FIB_FLUSH_INT, l2fib_flush_int) \
59 _ (L2FIB_FLUSH_BD, l2fib_flush_bd) \
60 _ (L2FIB_ADD_DEL, l2fib_add_del) \
Jerome Tollet0f8d1002021-01-07 12:44:17 +010061 _ (L2FIB_SET_SCAN_DELAY, l2fib_set_scan_delay) \
Jerome Tollet5f93e3b2020-12-18 09:44:24 +010062 _ (WANT_L2_MACS_EVENTS, want_l2_macs_events) \
Jerome Tollet0f8d1002021-01-07 12:44:17 +010063 _ (WANT_L2_MACS_EVENTS2, want_l2_macs_events2) \
Jerome Tollet5f93e3b2020-12-18 09:44:24 +010064 _ (L2_FLAGS, l2_flags) \
65 _ (SW_INTERFACE_SET_L2_XCONNECT, sw_interface_set_l2_xconnect) \
66 _ (SW_INTERFACE_SET_L2_BRIDGE, sw_interface_set_l2_bridge) \
67 _ (L2_PATCH_ADD_DEL, l2_patch_add_del) \
68 _ (L2_INTERFACE_EFP_FILTER, l2_interface_efp_filter) \
69 _ (BD_IP_MAC_ADD_DEL, bd_ip_mac_add_del) \
70 _ (BD_IP_MAC_FLUSH, bd_ip_mac_flush) \
71 _ (BD_IP_MAC_DUMP, bd_ip_mac_dump) \
72 _ (BRIDGE_DOMAIN_ADD_DEL, bridge_domain_add_del) \
73 _ (BRIDGE_DOMAIN_DUMP, bridge_domain_dump) \
74 _ (BRIDGE_FLAGS, bridge_flags) \
75 _ (L2_INTERFACE_VLAN_TAG_REWRITE, l2_interface_vlan_tag_rewrite) \
76 _ (L2_INTERFACE_PBB_TAG_REWRITE, l2_interface_pbb_tag_rewrite) \
77 _ (BRIDGE_DOMAIN_SET_MAC_AGE, bridge_domain_set_mac_age) \
78 _ (SW_INTERFACE_SET_VPATH, sw_interface_set_vpath) \
79 _ (BVI_CREATE, bvi_create) \
80 _ (BVI_DELETE, bvi_delete) \
81 _ (WANT_L2_ARP_TERM_EVENTS, want_l2_arp_term_events) \
82 _ (BRIDGE_DOMAIN_SET_LEARN_LIMIT, bridge_domain_set_learn_limit) \
83 _ (BRIDGE_DOMAIN_SET_DEFAULT_LEARN_LIMIT, \
84 bridge_domain_set_default_learn_limit)
Matus Fabianf468e232016-12-02 06:00:53 -080085
86static void
Florin Coras6c4dae22018-01-09 06:39:23 -080087send_l2_xconnect_details (vl_api_registration_t * reg, u32 context,
Matus Fabianf468e232016-12-02 06:00:53 -080088 u32 rx_sw_if_index, u32 tx_sw_if_index)
89{
90 vl_api_l2_xconnect_details_t *mp;
91
92 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -040093 clib_memset (mp, 0, sizeof (*mp));
Matus Fabianf468e232016-12-02 06:00:53 -080094 mp->_vl_msg_id = ntohs (VL_API_L2_XCONNECT_DETAILS);
95 mp->context = context;
96 mp->rx_sw_if_index = htonl (rx_sw_if_index);
97 mp->tx_sw_if_index = htonl (tx_sw_if_index);
98
Florin Coras6c4dae22018-01-09 06:39:23 -080099 vl_api_send_msg (reg, (u8 *) mp);
Matus Fabianf468e232016-12-02 06:00:53 -0800100}
101
102static void
103vl_api_l2_xconnect_dump_t_handler (vl_api_l2_xconnect_dump_t * mp)
104{
Florin Coras6c4dae22018-01-09 06:39:23 -0800105 vl_api_registration_t *reg;
Matus Fabianf468e232016-12-02 06:00:53 -0800106 l2input_main_t *l2im = &l2input_main;
Steven Luong16f08652021-02-08 23:48:30 -0800107 u32 sw_if_index;
Matus Fabianf468e232016-12-02 06:00:53 -0800108 l2_input_config_t *config;
109
Florin Coras6c4dae22018-01-09 06:39:23 -0800110 reg = vl_api_client_index_to_registration (mp->client_index);
111 if (!reg)
Matus Fabianf468e232016-12-02 06:00:53 -0800112 return;
113
114 /* *INDENT-OFF* */
Steven Luong16f08652021-02-08 23:48:30 -0800115 vec_foreach_index (sw_if_index, l2im->configs)
116 {
117 config = vec_elt_at_index (l2im->configs, sw_if_index);
118 if (l2_input_is_xconnect (config))
119 send_l2_xconnect_details (reg, mp->context, sw_if_index,
120 config->output_sw_if_index);
121 }
Matus Fabianf468e232016-12-02 06:00:53 -0800122 /* *INDENT-ON* */
123}
124
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100125static void
126vl_api_l2_fib_clear_table_t_handler (vl_api_l2_fib_clear_table_t * mp)
127{
128 int rv = 0;
129 vl_api_l2_fib_clear_table_reply_t *rmp;
130
Eyal Bari7537e712017-04-27 14:07:55 +0300131 /* Clear all MACs including static MACs */
132 l2fib_clear_table ();
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100133
134 REPLY_MACRO (VL_API_L2_FIB_CLEAR_TABLE_REPLY);
135}
136
137static void
138send_l2fib_table_entry (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -0800139 vl_api_registration_t * reg,
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100140 l2fib_entry_key_t * l2fe_key,
141 l2fib_entry_result_t * l2fe_res, u32 context)
142{
Ole Troan01384fe2017-05-12 11:55:35 +0200143 vl_api_l2_fib_table_details_t *mp;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100144
145 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400146 clib_memset (mp, 0, sizeof (*mp));
Ole Troan01384fe2017-05-12 11:55:35 +0200147 mp->_vl_msg_id = ntohs (VL_API_L2_FIB_TABLE_DETAILS);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100148
149 mp->bd_id =
150 ntohl (l2input_main.bd_configs[l2fe_key->fields.bd_index].bd_id);
151
Jakub Grajciar145e3302019-10-24 13:52:42 +0200152 mac_address_encode ((mac_address_t *) l2fe_key->fields.mac, mp->mac);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100153 mp->sw_if_index = ntohl (l2fe_res->fields.sw_if_index);
Neale Ranns7341b6d2018-09-19 04:07:02 -0700154 mp->static_mac = (l2fib_entry_result_is_set_STATIC (l2fe_res) ? 1 : 0);
155 mp->filter_mac = (l2fib_entry_result_is_set_FILTER (l2fe_res) ? 1 : 0);
156 mp->bvi_mac = (l2fib_entry_result_is_set_BVI (l2fe_res) ? 1 : 0);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100157 mp->context = context;
158
Florin Coras6c4dae22018-01-09 06:39:23 -0800159 vl_api_send_msg (reg, (u8 *) mp);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100160}
161
162static void
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100163vl_api_l2_fib_table_dump_t_handler (vl_api_l2_fib_table_dump_t * mp)
164{
165 vpe_api_main_t *am = &vpe_api_main;
166 bd_main_t *bdm = &bd_main;
167 l2fib_entry_key_t *l2fe_key = NULL;
168 l2fib_entry_result_t *l2fe_res = NULL;
169 u32 ni, bd_id = ntohl (mp->bd_id);
170 u32 bd_index;
Florin Coras6c4dae22018-01-09 06:39:23 -0800171 vl_api_registration_t *reg;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100172 uword *p;
173
Florin Coras6c4dae22018-01-09 06:39:23 -0800174 reg = vl_api_client_index_to_registration (mp->client_index);
175 if (!reg)
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100176 return;
177
178 /* see l2fib_table_dump: ~0 means "any" */
179 if (bd_id == ~0)
180 bd_index = ~0;
181 else
182 {
183 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
184 if (p == 0)
185 return;
186
187 bd_index = p[0];
188 }
189
190 l2fib_table_dump (bd_index, &l2fe_key, &l2fe_res);
191
192 vec_foreach_index (ni, l2fe_key)
193 {
Florin Coras6c4dae22018-01-09 06:39:23 -0800194 send_l2fib_table_entry (am, reg, vec_elt_at_index (l2fe_key, ni),
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100195 vec_elt_at_index (l2fe_res, ni), mp->context);
196 }
197 vec_free (l2fe_key);
198 vec_free (l2fe_res);
199}
200
201static void
202vl_api_l2fib_add_del_t_handler (vl_api_l2fib_add_del_t * mp)
203{
204 bd_main_t *bdm = &bd_main;
205 l2input_main_t *l2im = &l2input_main;
206 vl_api_l2fib_add_del_reply_t *rmp;
207 int rv = 0;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100208 u32 bd_id = ntohl (mp->bd_id);
Eyal Bari31a71ab2017-06-25 14:42:33 +0300209 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100210
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100211 if (!p)
212 {
213 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
214 goto bad_sw_if_index;
215 }
Eyal Bari31a71ab2017-06-25 14:42:33 +0300216 u32 bd_index = p[0];
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100217
Jakub Grajciar145e3302019-10-24 13:52:42 +0200218 mac_address_t mac;
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200219
Jakub Grajciar145e3302019-10-24 13:52:42 +0200220 mac_address_decode (mp->mac, &mac);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100221 if (mp->is_add)
222 {
Eyal Bari31a71ab2017-06-25 14:42:33 +0300223 if (mp->filter_mac)
Jakub Grajciar145e3302019-10-24 13:52:42 +0200224 l2fib_add_filter_entry (mac.bytes, bd_index);
Eyal Bari31a71ab2017-06-25 14:42:33 +0300225 else
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100226 {
Neale Rannsb54d0812018-09-06 06:22:56 -0700227 l2fib_entry_result_flags_t flags = L2FIB_ENTRY_RESULT_FLAG_NONE;
Eyal Bari31a71ab2017-06-25 14:42:33 +0300228 u32 sw_if_index = ntohl (mp->sw_if_index);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100229 VALIDATE_SW_IF_INDEX (mp);
230 if (vec_len (l2im->configs) <= sw_if_index)
231 {
232 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
233 goto bad_sw_if_index;
234 }
235 else
236 {
237 l2_input_config_t *config;
238 config = vec_elt_at_index (l2im->configs, sw_if_index);
Neale Ranns47a3d992020-09-29 15:38:51 +0000239 if (!l2_input_is_bridge (config))
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100240 {
241 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
242 goto bad_sw_if_index;
243 }
244 }
Neale Rannsb54d0812018-09-06 06:22:56 -0700245 if (mp->static_mac)
246 flags |= L2FIB_ENTRY_RESULT_FLAG_STATIC;
247 if (mp->bvi_mac)
248 flags |= L2FIB_ENTRY_RESULT_FLAG_BVI;
Jakub Grajciar145e3302019-10-24 13:52:42 +0200249 l2fib_add_entry (mac.bytes, bd_index, sw_if_index, flags);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100250 }
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100251 }
252 else
253 {
John Lo7dbd7262018-05-31 10:25:18 -0400254 u32 sw_if_index = ntohl (mp->sw_if_index);
Jakub Grajciar145e3302019-10-24 13:52:42 +0200255 if (l2fib_del_entry (mac.bytes, bd_index, sw_if_index))
John Lo7dbd7262018-05-31 10:25:18 -0400256 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100257 }
258
259 BAD_SW_IF_INDEX_LABEL;
260
261 REPLY_MACRO (VL_API_L2FIB_ADD_DEL_REPLY);
262}
263
264static void
Jerome Tollet0f8d1002021-01-07 12:44:17 +0100265vl_api_want_l2_macs_events2_t_handler (vl_api_want_l2_macs_events2_t *mp)
John Lo8d00fff2017-08-03 00:35:36 -0400266{
267 int rv = 0;
Jerome Tollet0f8d1002021-01-07 12:44:17 +0100268 vl_api_want_l2_macs_events2_reply_t *rmp;
John Lo8d00fff2017-08-03 00:35:36 -0400269 l2learn_main_t *lm = &l2learn_main;
270 l2fib_main_t *fm = &l2fib_main;
271 u32 pid = ntohl (mp->pid);
John Lo8d00fff2017-08-03 00:35:36 -0400272
273 if (mp->enable_disable)
274 {
Jerome Tollet0f8d1002021-01-07 12:44:17 +0100275 if ((lm->client_pid == 0) || (lm->client_pid == pid))
John Lo8d00fff2017-08-03 00:35:36 -0400276 {
John Lo8d00fff2017-08-03 00:35:36 -0400277 if (mp->max_macs_in_event)
278 fm->max_macs_in_event = mp->max_macs_in_event * 10;
279 else
Jakub Grajciar145e3302019-10-24 13:52:42 +0200280 {
281 rv = VNET_API_ERROR_INVALID_VALUE;
282 goto exit;
283 }
John Lo8d00fff2017-08-03 00:35:36 -0400284
Jerome Tollet0f8d1002021-01-07 12:44:17 +0100285 /* if scan_delay was not set before */
286 if (fm->event_scan_delay == 0.0)
287 fm->event_scan_delay = (f64) (10) * 10e-3;
John Lo8d00fff2017-08-03 00:35:36 -0400288
Jerome Tollet0f8d1002021-01-07 12:44:17 +0100289 lm->client_pid = pid;
290 lm->client_index = mp->client_index;
John Lo8d00fff2017-08-03 00:35:36 -0400291 l2fib_flush_all_mac (vlib_get_main ());
292 }
293 else if (lm->client_pid != pid)
294 {
295 rv = VNET_API_ERROR_L2_MACS_EVENT_CLINET_PRESENT;
296 goto exit;
297 }
298 }
299 else if (lm->client_pid)
300 {
301 lm->client_pid = 0;
302 lm->client_index = 0;
Jerome Tollet0f8d1002021-01-07 12:44:17 +0100303 }
304
305exit:
306 REPLY_MACRO (VL_API_WANT_L2_MACS_EVENTS2_REPLY);
307}
308
309static void
310vl_api_want_l2_macs_events_t_handler (vl_api_want_l2_macs_events_t *mp)
311{
312 int rv = 0;
313 vl_api_want_l2_macs_events_reply_t *rmp;
314 l2learn_main_t *lm = &l2learn_main;
315 l2fib_main_t *fm = &l2fib_main;
316 u32 pid = ntohl (mp->pid);
317 u32 learn_limit = ntohl (mp->learn_limit);
318
319 if (mp->enable_disable)
320 {
321 if ((lm->client_pid == 0) || (lm->client_pid == pid))
322 {
323 if ((mp->max_macs_in_event == 0) || (mp->scan_delay == 0) ||
324 (learn_limit == 0) || (learn_limit > L2LEARN_DEFAULT_LIMIT))
325 {
326 rv = VNET_API_ERROR_INVALID_VALUE;
327 goto exit;
328 }
329 lm->client_pid = pid;
330 lm->client_index = mp->client_index;
331
332 fm->max_macs_in_event = mp->max_macs_in_event * 10;
333 fm->event_scan_delay = (f64) (mp->scan_delay) * 10e-3;
334
335 /* change learn limit and flush all learned MACs */
336 lm->global_learn_limit = learn_limit;
337 l2fib_flush_all_mac (vlib_get_main ());
338 }
339 else if (lm->client_pid != pid)
340 {
341 rv = VNET_API_ERROR_L2_MACS_EVENT_CLINET_PRESENT;
342 goto exit;
343 }
344 }
345 else if (lm->client_pid)
346 {
347 lm->client_pid = 0;
348 lm->client_index = 0;
349 if (learn_limit && (learn_limit <= L2LEARN_DEFAULT_LIMIT))
John Loe531f4c2017-08-22 09:16:50 -0400350 lm->global_learn_limit = learn_limit;
351 else
352 lm->global_learn_limit = L2LEARN_DEFAULT_LIMIT;
John Lo8d00fff2017-08-03 00:35:36 -0400353 }
354
355exit:
356 REPLY_MACRO (VL_API_WANT_L2_MACS_EVENTS_REPLY);
357}
358
359static void
Eyal Barif24991c2017-04-05 05:33:21 +0300360vl_api_l2fib_flush_int_t_handler (vl_api_l2fib_flush_int_t * mp)
361{
362 int rv = 0;
363 vlib_main_t *vm = vlib_get_main ();
364 vl_api_l2fib_flush_int_reply_t *rmp;
365
366 VALIDATE_SW_IF_INDEX (mp);
367
368 u32 sw_if_index = ntohl (mp->sw_if_index);
369 l2fib_flush_int_mac (vm, sw_if_index);
370
371 BAD_SW_IF_INDEX_LABEL;
372 REPLY_MACRO (VL_API_L2FIB_FLUSH_INT_REPLY);
373}
374
375static void
Eyal Bari7537e712017-04-27 14:07:55 +0300376vl_api_l2fib_flush_all_t_handler (vl_api_l2fib_flush_all_t * mp)
377{
378 int rv = 0;
379 vl_api_l2fib_flush_all_reply_t *rmp;
380
381 l2fib_flush_all_mac (vlib_get_main ());
382 REPLY_MACRO (VL_API_L2FIB_FLUSH_ALL_REPLY);
383}
384
385static void
Eyal Barif24991c2017-04-05 05:33:21 +0300386vl_api_l2fib_flush_bd_t_handler (vl_api_l2fib_flush_bd_t * mp)
387{
388 int rv = 0;
389 vlib_main_t *vm = vlib_get_main ();
390 bd_main_t *bdm = &bd_main;
391 vl_api_l2fib_flush_bd_reply_t *rmp;
392
393 u32 bd_id = ntohl (mp->bd_id);
394 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
395 if (p == 0)
396 {
397 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
398 goto out;
399 }
400 l2fib_flush_bd_mac (vm, *p);
401out:
402 REPLY_MACRO (VL_API_L2FIB_FLUSH_BD_REPLY);
403}
404
405static void
Jerome Tollet0f8d1002021-01-07 12:44:17 +0100406vl_api_l2fib_set_scan_delay_t_handler (vl_api_l2fib_set_scan_delay_t *mp)
407{
408 int rv = 0;
409 l2fib_main_t *fm = &l2fib_main;
410 vl_api_l2fib_set_scan_delay_reply_t *rmp;
411 u16 scan_delay = ntohs (mp->scan_delay);
412
413 if (mp->scan_delay)
414 {
415 fm->event_scan_delay = (f64) (scan_delay) *10e-3;
416 l2fib_flush_all_mac (vlib_get_main ());
417 }
418 else
419 {
420 rv = VNET_API_ERROR_INVALID_VALUE;
421 goto exit;
422 }
423
424exit:
425 REPLY_MACRO (VL_API_L2FIB_SET_SCAN_DELAY_REPLY);
426}
427
428static void
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100429vl_api_l2_flags_t_handler (vl_api_l2_flags_t * mp)
430{
431 vl_api_l2_flags_reply_t *rmp;
432 int rv = 0;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100433 u32 rbm = 0;
434
435 VALIDATE_SW_IF_INDEX (mp);
436
Eyal Barifead6702017-04-04 04:46:32 +0300437 u32 sw_if_index = ntohl (mp->sw_if_index);
John Lo8d00fff2017-08-03 00:35:36 -0400438 u32 flags = ntohl (mp->feature_bitmap);
439 u32 bitmap = 0;
440
441 if (flags & L2_LEARN)
442 bitmap |= L2INPUT_FEAT_LEARN;
443
444 if (flags & L2_FWD)
445 bitmap |= L2INPUT_FEAT_FWD;
446
447 if (flags & L2_FLOOD)
448 bitmap |= L2INPUT_FEAT_FLOOD;
449
450 if (flags & L2_UU_FLOOD)
451 bitmap |= L2INPUT_FEAT_UU_FLOOD;
452
453 if (flags & L2_ARP_TERM)
454 bitmap |= L2INPUT_FEAT_ARP_TERM;
455
456 rbm = l2input_intf_bitmap_enable (sw_if_index, bitmap, mp->is_set);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100457
458 BAD_SW_IF_INDEX_LABEL;
459
460 /* *INDENT-OFF* */
461 REPLY_MACRO2(VL_API_L2_FLAGS_REPLY,
462 ({
463 rmp->resulting_feature_bitmap = ntohl(rbm);
464 }));
465 /* *INDENT-ON* */
466}
467
468static void
Jerome Tollet5f93e3b2020-12-18 09:44:24 +0100469vl_api_bridge_domain_set_default_learn_limit_t_handler (
470 vl_api_bridge_domain_set_default_learn_limit_t *mp)
471{
472 vl_api_bridge_domain_set_default_learn_limit_reply_t *rmp;
473 int rv = 0;
474
475 l2learn_main.bd_default_learn_limit = ntohl (mp->learn_limit);
476 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_SET_DEFAULT_LEARN_LIMIT_REPLY);
477}
478
479static void
480vl_api_bridge_domain_set_learn_limit_t_handler (
481 vl_api_bridge_domain_set_learn_limit_t *mp)
482{
483 vlib_main_t *vm = vlib_get_main ();
484 bd_main_t *bdm = &bd_main;
485 vl_api_bridge_domain_set_learn_limit_reply_t *rmp;
486 int rv = 0;
487 u32 bd_id = ntohl (mp->bd_id);
488 uword *p;
489
490 if (bd_id == 0)
491 {
492 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
493 goto out;
494 }
495
496 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
497 if (p == 0)
498 {
499 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
500 goto out;
501 }
502 bd_set_learn_limit (vm, *p, ntohl (mp->learn_limit));
503out:
504 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_SET_LEARN_LIMIT_REPLY);
505}
506
507static void
Eyal Barifead6702017-04-04 04:46:32 +0300508vl_api_bridge_domain_set_mac_age_t_handler (vl_api_bridge_domain_set_mac_age_t
509 * mp)
510{
511 vlib_main_t *vm = vlib_get_main ();
512 bd_main_t *bdm = &bd_main;
513 vl_api_bridge_domain_set_mac_age_reply_t *rmp;
514 int rv = 0;
515 u32 bd_id = ntohl (mp->bd_id);
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500516 uword *p;
517
518 if (bd_id == 0)
519 {
520 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
521 goto out;
522 }
523
524 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
Eyal Barifead6702017-04-04 04:46:32 +0300525 if (p == 0)
526 {
527 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
528 goto out;
529 }
530 bd_set_mac_age (vm, *p, mp->mac_age);
531out:
532 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_SET_MAC_AGE_REPLY);
533}
534
535static void
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100536vl_api_bridge_domain_add_del_t_handler (vl_api_bridge_domain_add_del_t * mp)
537{
Eyal Barib1352ed2017-04-07 23:14:17 +0300538 l2_bridge_domain_add_del_args_t a = {
539 .is_add = mp->is_add,
540 .flood = mp->flood,
541 .uu_flood = mp->uu_flood,
542 .forward = mp->forward,
543 .learn = mp->learn,
544 .arp_term = mp->arp_term,
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +0200545 .arp_ufwd = mp->arp_ufwd,
Eyal Barib1352ed2017-04-07 23:14:17 +0300546 .mac_age = mp->mac_age,
547 .bd_id = ntohl (mp->bd_id),
Jerome Tollet48304142017-09-05 12:13:22 +0100548 .bd_tag = mp->bd_tag
Eyal Barib1352ed2017-04-07 23:14:17 +0300549 };
550
551 int rv = bd_add_del (&a);
552
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100553 vl_api_bridge_domain_add_del_reply_t *rmp;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100554 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_ADD_DEL_REPLY);
555}
556
557static void
Ole Troan01384fe2017-05-12 11:55:35 +0200558send_bridge_domain_details (l2input_main_t * l2im,
Florin Coras6c4dae22018-01-09 06:39:23 -0800559 vl_api_registration_t * reg,
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100560 l2_bridge_domain_t * bd_config,
561 u32 n_sw_ifs, u32 context)
562{
563 vl_api_bridge_domain_details_t *mp;
Ole Troan01384fe2017-05-12 11:55:35 +0200564 l2_flood_member_t *m;
565 vl_api_bridge_domain_sw_if_t *sw_ifs;
566 l2_input_config_t *input_cfg;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100567
Ole Troan01384fe2017-05-12 11:55:35 +0200568 mp = vl_msg_api_alloc (sizeof (*mp) +
569 (n_sw_ifs * sizeof (vl_api_bridge_domain_sw_if_t)));
Dave Barachb7b92992018-10-17 10:38:51 -0400570 clib_memset (mp, 0, sizeof (*mp));
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100571 mp->_vl_msg_id = ntohs (VL_API_BRIDGE_DOMAIN_DETAILS);
572 mp->bd_id = ntohl (bd_config->bd_id);
573 mp->flood = bd_feature_flood (bd_config);
574 mp->uu_flood = bd_feature_uu_flood (bd_config);
575 mp->forward = bd_feature_forward (bd_config);
576 mp->learn = bd_feature_learn (bd_config);
577 mp->arp_term = bd_feature_arp_term (bd_config);
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +0200578 mp->arp_ufwd = bd_feature_arp_ufwd (bd_config);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100579 mp->bvi_sw_if_index = ntohl (bd_config->bvi_sw_if_index);
Neale Rannsb4743802018-09-05 09:13:57 -0700580 mp->uu_fwd_sw_if_index = ntohl (bd_config->uu_fwd_sw_if_index);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100581 mp->mac_age = bd_config->mac_age;
Jerome Tollet48304142017-09-05 12:13:22 +0100582 if (bd_config->bd_tag)
583 {
584 strncpy ((char *) mp->bd_tag, (char *) bd_config->bd_tag,
585 ARRAY_LEN (mp->bd_tag) - 1);
586 mp->bd_tag[ARRAY_LEN (mp->bd_tag) - 1] = 0;
587 }
588
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100589 mp->context = context;
590
Ole Troan01384fe2017-05-12 11:55:35 +0200591 sw_ifs = (vl_api_bridge_domain_sw_if_t *) mp->sw_if_details;
592 vec_foreach (m, bd_config->members)
593 {
594 sw_ifs->sw_if_index = ntohl (m->sw_if_index);
595 input_cfg = vec_elt_at_index (l2im->configs, m->sw_if_index);
596 sw_ifs->shg = input_cfg->shg;
597 sw_ifs++;
598 mp->n_sw_ifs++;
599 }
600 mp->n_sw_ifs = htonl (mp->n_sw_ifs);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100601
Florin Coras6c4dae22018-01-09 06:39:23 -0800602 vl_api_send_msg (reg, (u8 *) mp);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100603}
604
605static void
606vl_api_bridge_domain_dump_t_handler (vl_api_bridge_domain_dump_t * mp)
607{
608 bd_main_t *bdm = &bd_main;
609 l2input_main_t *l2im = &l2input_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800610 vl_api_registration_t *reg;
Jakub Grajciar145e3302019-10-24 13:52:42 +0200611 u32 bd_id, bd_index, end, filter_sw_if_index;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100612
Florin Coras6c4dae22018-01-09 06:39:23 -0800613 reg = vl_api_client_index_to_registration (mp->client_index);
614 if (!reg)
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100615 return;
616
Jakub Grajciar145e3302019-10-24 13:52:42 +0200617 filter_sw_if_index = ntohl (mp->sw_if_index);
618 if (filter_sw_if_index != ~0)
619 return; /* UNIMPLEMENTED */
620
Florin Coras6c4dae22018-01-09 06:39:23 -0800621 bd_id = ntohl (mp->bd_id);
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500622 if (bd_id == 0)
623 return;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100624
Eyal Bari259fca72017-05-14 10:38:39 +0300625 if (bd_id == ~0)
626 bd_index = 0, end = vec_len (l2im->bd_configs);
627 else
628 {
629 bd_index = bd_find_index (bdm, bd_id);
630 if (bd_index == ~0)
631 return;
632
633 end = bd_index + 1;
634 }
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500635
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100636 for (; bd_index < end; bd_index++)
637 {
Eyal Bari259fca72017-05-14 10:38:39 +0300638 l2_bridge_domain_t *bd_config =
639 l2input_bd_config_from_index (l2im, bd_index);
Dave Barach11fb09e2020-08-06 12:10:09 -0400640 /* skip placeholder bd_id 0 */
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100641 if (bd_config && (bd_config->bd_id > 0))
Florin Coras6c4dae22018-01-09 06:39:23 -0800642 send_bridge_domain_details (l2im, reg, bd_config,
Ole Troan01384fe2017-05-12 11:55:35 +0200643 vec_len (bd_config->members),
644 mp->context);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100645 }
646}
647
Neale Rannsb4743802018-09-05 09:13:57 -0700648static bd_flags_t
649bd_flags_decode (vl_api_bd_flags_t v)
650{
651 bd_flags_t f = L2_NONE;
652
653 v = ntohl (v);
654
655 if (v & BRIDGE_API_FLAG_LEARN)
656 f |= L2_LEARN;
657 if (v & BRIDGE_API_FLAG_FWD)
658 f |= L2_FWD;
659 if (v & BRIDGE_API_FLAG_FLOOD)
660 f |= L2_FLOOD;
661 if (v & BRIDGE_API_FLAG_UU_FLOOD)
662 f |= L2_UU_FLOOD;
663 if (v & BRIDGE_API_FLAG_ARP_TERM)
664 f |= L2_ARP_TERM;
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +0200665 if (v & BRIDGE_API_FLAG_ARP_UFWD)
666 f |= L2_ARP_UFWD;
Neale Rannsb4743802018-09-05 09:13:57 -0700667
668 return (f);
669}
670
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100671static void
672vl_api_bridge_flags_t_handler (vl_api_bridge_flags_t * mp)
673{
674 vlib_main_t *vm = vlib_get_main ();
675 bd_main_t *bdm = &bd_main;
676 vl_api_bridge_flags_reply_t *rmp;
Dave Barach98d6b612019-01-03 09:30:00 -0500677 u32 bitmap = 0;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100678 int rv = 0;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100679
Neale Rannsb4743802018-09-05 09:13:57 -0700680 bd_flags_t flags = bd_flags_decode (mp->flags);
Eyal Bari259fca72017-05-14 10:38:39 +0300681 u32 bd_id = ntohl (mp->bd_id);
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500682 if (bd_id == 0)
683 {
684 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
685 goto out;
686 }
687
Eyal Bari259fca72017-05-14 10:38:39 +0300688 u32 bd_index = bd_find_index (bdm, bd_id);
689 if (bd_index == ~0)
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100690 {
691 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
692 goto out;
693 }
694
Dave Barach98d6b612019-01-03 09:30:00 -0500695 bitmap = bd_set_flags (vm, bd_index, flags, mp->is_set);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100696
697out:
698 /* *INDENT-OFF* */
699 REPLY_MACRO2(VL_API_BRIDGE_FLAGS_REPLY,
700 ({
John Lo8d00fff2017-08-03 00:35:36 -0400701 rmp->resulting_feature_bitmap = ntohl(bitmap);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100702 }));
703 /* *INDENT-ON* */
704}
Matus Fabianf468e232016-12-02 06:00:53 -0800705
Pavel Kotucekadec5872017-01-25 08:50:53 +0100706static void
707 vl_api_l2_interface_vlan_tag_rewrite_t_handler
708 (vl_api_l2_interface_vlan_tag_rewrite_t * mp)
709{
710 int rv = 0;
711 vl_api_l2_interface_vlan_tag_rewrite_reply_t *rmp;
712 vnet_main_t *vnm = vnet_get_main ();
713 vlib_main_t *vm = vlib_get_main ();
714 u32 vtr_op;
715
716 VALIDATE_SW_IF_INDEX (mp);
717
718 vtr_op = ntohl (mp->vtr_op);
719
720 /* The L2 code is unsuspicious */
721 switch (vtr_op)
722 {
723 case L2_VTR_DISABLED:
724 case L2_VTR_PUSH_1:
725 case L2_VTR_PUSH_2:
726 case L2_VTR_POP_1:
727 case L2_VTR_POP_2:
728 case L2_VTR_TRANSLATE_1_1:
729 case L2_VTR_TRANSLATE_1_2:
730 case L2_VTR_TRANSLATE_2_1:
731 case L2_VTR_TRANSLATE_2_2:
732 break;
733
734 default:
735 rv = VNET_API_ERROR_INVALID_VALUE;
736 goto bad_sw_if_index;
737 }
738
739 rv = l2vtr_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
740 ntohl (mp->push_dot1q), ntohl (mp->tag1),
741 ntohl (mp->tag2));
742
743 BAD_SW_IF_INDEX_LABEL;
744
745 REPLY_MACRO (VL_API_L2_INTERFACE_VLAN_TAG_REWRITE_REPLY);
746}
747
748static void
749 vl_api_l2_interface_pbb_tag_rewrite_t_handler
750 (vl_api_l2_interface_pbb_tag_rewrite_t * mp)
751{
752 vl_api_l2_interface_pbb_tag_rewrite_reply_t *rmp;
753 vnet_main_t *vnm = vnet_get_main ();
754 vlib_main_t *vm = vlib_get_main ();
755 u32 vtr_op;
756 int rv = 0;
Jakub Grajciar145e3302019-10-24 13:52:42 +0200757 mac_address_t b_dmac, b_smac;
Pavel Kotucekadec5872017-01-25 08:50:53 +0100758
759 VALIDATE_SW_IF_INDEX (mp);
760
761 vtr_op = ntohl (mp->vtr_op);
762
763 switch (vtr_op)
764 {
765 case L2_VTR_DISABLED:
766 case L2_VTR_PUSH_2:
767 case L2_VTR_POP_2:
768 case L2_VTR_TRANSLATE_2_1:
769 break;
770
771 default:
772 rv = VNET_API_ERROR_INVALID_VALUE;
773 goto bad_sw_if_index;
774 }
775
Jakub Grajciar145e3302019-10-24 13:52:42 +0200776 mac_address_decode (mp->b_dmac, &b_dmac);
777 mac_address_decode (mp->b_smac, &b_smac);
778
Pavel Kotucekadec5872017-01-25 08:50:53 +0100779 rv = l2pbb_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
Jakub Grajciar145e3302019-10-24 13:52:42 +0200780 b_dmac.bytes, b_smac.bytes, ntohs (mp->b_vlanid),
Pavel Kotucekadec5872017-01-25 08:50:53 +0100781 ntohl (mp->i_sid), ntohs (mp->outer_tag));
782
783 BAD_SW_IF_INDEX_LABEL;
784
785 REPLY_MACRO (VL_API_L2_INTERFACE_PBB_TAG_REWRITE_REPLY);
786}
787
Neale Rannsb8d44812017-11-10 06:53:54 -0800788static void
789 vl_api_sw_interface_set_l2_xconnect_t_handler
790 (vl_api_sw_interface_set_l2_xconnect_t * mp)
791{
792 vl_api_sw_interface_set_l2_xconnect_reply_t *rmp;
793 int rv = 0;
794 u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
795 u32 tx_sw_if_index = ntohl (mp->tx_sw_if_index);
796 vlib_main_t *vm = vlib_get_main ();
797 vnet_main_t *vnm = vnet_get_main ();
798
799 VALIDATE_RX_SW_IF_INDEX (mp);
800
801 if (mp->enable)
802 {
803 VALIDATE_TX_SW_IF_INDEX (mp);
804 rv = set_int_l2_mode (vm, vnm, MODE_L2_XC,
Neale Rannsb4743802018-09-05 09:13:57 -0700805 rx_sw_if_index, 0,
806 L2_BD_PORT_TYPE_NORMAL, 0, tx_sw_if_index);
Neale Rannsb8d44812017-11-10 06:53:54 -0800807 }
808 else
809 {
Neale Rannsb4743802018-09-05 09:13:57 -0700810 rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0,
811 L2_BD_PORT_TYPE_NORMAL, 0, 0);
Neale Rannsb8d44812017-11-10 06:53:54 -0800812 }
813
Alexander Chernavine178b272018-09-24 05:42:01 -0400814 switch (rv)
815 {
816 case MODE_ERROR_ETH:
817 rv = VNET_API_ERROR_NON_ETHERNET;
818 break;
819 case MODE_ERROR_BVI_DEF:
820 rv = VNET_API_ERROR_BD_ALREADY_HAS_BVI;
821 break;
822 }
823
Neale Rannsb8d44812017-11-10 06:53:54 -0800824 BAD_RX_SW_IF_INDEX_LABEL;
825 BAD_TX_SW_IF_INDEX_LABEL;
826
827 REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_XCONNECT_REPLY);
828}
829
Neale Rannsb4743802018-09-05 09:13:57 -0700830static int
831l2_bd_port_type_decode (vl_api_l2_port_type_t v, l2_bd_port_type_t * l)
832{
833 v = clib_net_to_host_u32 (v);
834
835 switch (v)
836 {
837 case L2_API_PORT_TYPE_NORMAL:
838 *l = L2_BD_PORT_TYPE_NORMAL;
839 return 0;
840 case L2_API_PORT_TYPE_BVI:
841 *l = L2_BD_PORT_TYPE_BVI;
842 return 0;
843 case L2_API_PORT_TYPE_UU_FWD:
844 *l = L2_BD_PORT_TYPE_UU_FWD;
845 return 0;
846 }
847
848 return (VNET_API_ERROR_INVALID_VALUE);
849}
850
Neale Rannsb8d44812017-11-10 06:53:54 -0800851static void
852 vl_api_sw_interface_set_l2_bridge_t_handler
853 (vl_api_sw_interface_set_l2_bridge_t * mp)
854{
855 bd_main_t *bdm = &bd_main;
856 vl_api_sw_interface_set_l2_bridge_reply_t *rmp;
857 int rv = 0;
858 vlib_main_t *vm = vlib_get_main ();
859 vnet_main_t *vnm = vnet_get_main ();
Neale Rannsb4743802018-09-05 09:13:57 -0700860 l2_bd_port_type_t pt;
Neale Rannsb8d44812017-11-10 06:53:54 -0800861
862 VALIDATE_RX_SW_IF_INDEX (mp);
863 u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
Neale Rannsb4743802018-09-05 09:13:57 -0700864 rv = l2_bd_port_type_decode (mp->port_type, &pt);
Neale Rannsb8d44812017-11-10 06:53:54 -0800865
Neale Rannsb4743802018-09-05 09:13:57 -0700866 if (0 != rv)
867 goto out;
Neale Rannsb8d44812017-11-10 06:53:54 -0800868 if (mp->enable)
869 {
870 VALIDATE_BD_ID (mp);
871 u32 bd_id = ntohl (mp->bd_id);
872 u32 bd_index = bd_find_or_add_bd_index (bdm, bd_id);
Neale Rannsb4743802018-09-05 09:13:57 -0700873
Neale Rannsb8d44812017-11-10 06:53:54 -0800874 rv = set_int_l2_mode (vm, vnm, MODE_L2_BRIDGE,
Neale Rannsb4743802018-09-05 09:13:57 -0700875 rx_sw_if_index, bd_index, pt, mp->shg, 0);
Neale Rannsb8d44812017-11-10 06:53:54 -0800876 }
877 else
878 {
Neale Rannsb4743802018-09-05 09:13:57 -0700879 rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0, pt, 0, 0);
Neale Rannsb8d44812017-11-10 06:53:54 -0800880 }
881
Alexander Chernavine178b272018-09-24 05:42:01 -0400882 switch (rv)
883 {
884 case MODE_ERROR_ETH:
885 rv = VNET_API_ERROR_NON_ETHERNET;
886 break;
887 case MODE_ERROR_BVI_DEF:
888 rv = VNET_API_ERROR_BD_ALREADY_HAS_BVI;
889 break;
890 }
891
Neale Rannsb8d44812017-11-10 06:53:54 -0800892 BAD_RX_SW_IF_INDEX_LABEL;
893 BAD_BD_ID_LABEL;
Neale Rannsb4743802018-09-05 09:13:57 -0700894out:
Neale Rannsb8d44812017-11-10 06:53:54 -0800895 REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_BRIDGE_REPLY);
896}
897
898static void
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200899send_bd_ip_mac_entry (vpe_api_main_t * am,
900 vl_api_registration_t * reg,
Neale Rannsbc764c82019-06-19 07:07:13 -0700901 u32 bd_id,
902 const ip46_address_t * ip,
903 ip46_type_t itype,
904 const mac_address_t * mac, u32 context)
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200905{
906 vl_api_bd_ip_mac_details_t *mp;
907
908 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400909 clib_memset (mp, 0, sizeof (*mp));
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200910 mp->_vl_msg_id = ntohs (VL_API_BD_IP_MAC_DETAILS);
911
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200912 mp->context = context;
Neale Rannsbc764c82019-06-19 07:07:13 -0700913 mp->entry.bd_id = ntohl (bd_id);
914
915 ip_address_encode (ip, itype, &mp->entry.ip);
916 mac_address_encode (mac, mp->entry.mac);
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200917
918 vl_api_send_msg (reg, (u8 *) mp);
919}
920
921static void
922vl_api_bd_ip_mac_dump_t_handler (vl_api_bd_ip_mac_dump_t * mp)
923{
924 vpe_api_main_t *am = &vpe_api_main;
925 bd_main_t *bdm = &bd_main;
926 l2_bridge_domain_t *bd_config;
927 u32 bd_id = ntohl (mp->bd_id);
Mohsin Kazmib24dfec2018-08-23 12:24:19 +0200928 u32 bd_index, start, end;
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200929 vl_api_registration_t *reg;
930 uword *p;
931
932 reg = vl_api_client_index_to_registration (mp->client_index);
933 if (!reg)
934 return;
935
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200936 /* see bd_id: ~0 means "any" */
937 if (bd_id == ~0)
Mohsin Kazmib24dfec2018-08-23 12:24:19 +0200938 {
939 start = 1;
940 end = vec_len (l2input_main.bd_configs);
941 }
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200942 else
943 {
944 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
945 if (p == 0)
946 return;
947
948 bd_index = p[0];
949 vec_validate (l2input_main.bd_configs, bd_index);
950 start = bd_index;
951 end = start + 1;
952 }
953
954 for (bd_index = start; bd_index < end; bd_index++)
955 {
956 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
957 if (bd_is_valid (bd_config))
958 {
959 ip4_address_t ip4_addr;
960 ip6_address_t *ip6_addr;
Neale Rannsbc764c82019-06-19 07:07:13 -0700961 mac_address_t mac;
962 u64 mac64;
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200963 bd_id = bd_config->bd_id;
964
965 /* *INDENT-OFF* */
Neale Rannsbc764c82019-06-19 07:07:13 -0700966 hash_foreach (ip4_addr.as_u32, mac64, bd_config->mac_by_ip4,
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200967 ({
Neale Rannsbc764c82019-06-19 07:07:13 -0700968 ip46_address_t ip = {
969 .ip4 = ip4_addr,
970 };
971 mac_address_from_u64(&mac, mac64);
972
973 send_bd_ip_mac_entry (am, reg, bd_id, &ip, IP46_TYPE_IP4,
974 &mac, mp->context);
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200975 }));
976
Neale Rannsbc764c82019-06-19 07:07:13 -0700977 hash_foreach_mem (ip6_addr, mac64, bd_config->mac_by_ip6,
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200978 ({
Neale Rannsbc764c82019-06-19 07:07:13 -0700979 ip46_address_t ip = {
980 .ip6 = *ip6_addr,
981 };
982 mac_address_from_u64(&mac, mac64);
983
984 send_bd_ip_mac_entry (am, reg, bd_id, &ip, IP46_TYPE_IP6,
985 &mac, mp->context);
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200986 }));
987 /* *INDENT-ON* */
988 }
989 }
990}
991
992static void
Neale Rannsb8d44812017-11-10 06:53:54 -0800993vl_api_bd_ip_mac_add_del_t_handler (vl_api_bd_ip_mac_add_del_t * mp)
994{
Neale Ranns4d5b9172018-10-24 02:57:49 -0700995 ip46_address_t ip_addr = ip46_address_initializer;
Neale Rannsb8d44812017-11-10 06:53:54 -0800996 vl_api_bd_ip_mac_add_del_reply_t *rmp;
Neale Ranns4d5b9172018-10-24 02:57:49 -0700997 bd_main_t *bdm = &bd_main;
998 u32 bd_index, bd_id;
999 mac_address_t mac;
1000 ip46_type_t type;
Neale Rannsb8d44812017-11-10 06:53:54 -08001001 int rv = 0;
Neale Rannsb8d44812017-11-10 06:53:54 -08001002 uword *p;
1003
Neale Rannsbc764c82019-06-19 07:07:13 -07001004 bd_id = ntohl (mp->entry.bd_id);
Neale Ranns4d5b9172018-10-24 02:57:49 -07001005
Neale Rannsb8d44812017-11-10 06:53:54 -08001006 if (bd_id == 0)
1007 {
1008 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
1009 goto out;
1010 }
1011
1012 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
1013 if (p == 0)
1014 {
1015 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1016 goto out;
1017 }
Neale Rannsb8d44812017-11-10 06:53:54 -08001018 bd_index = p[0];
Neale Ranns4d5b9172018-10-24 02:57:49 -07001019
Neale Rannsbc764c82019-06-19 07:07:13 -07001020 type = ip_address_decode (&mp->entry.ip, &ip_addr);
1021 mac_address_decode (mp->entry.mac, &mac);
Neale Ranns4d5b9172018-10-24 02:57:49 -07001022
1023 if (bd_add_del_ip_mac (bd_index, type, &ip_addr, &mac, mp->is_add))
Neale Rannsb8d44812017-11-10 06:53:54 -08001024 rv = VNET_API_ERROR_UNSPECIFIED;
1025
1026out:
1027 REPLY_MACRO (VL_API_BD_IP_MAC_ADD_DEL_REPLY);
1028}
1029
John Loe26c81f2019-01-07 15:16:33 -05001030static void
1031vl_api_bd_ip_mac_flush_t_handler (vl_api_bd_ip_mac_flush_t * mp)
1032{
1033 vl_api_bd_ip_mac_flush_reply_t *rmp;
1034 bd_main_t *bdm = &bd_main;
1035 u32 bd_index, bd_id;
1036 int rv = 0;
1037 uword *p;
1038
1039 bd_id = ntohl (mp->bd_id);
1040
1041 if (bd_id == 0)
1042 {
1043 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
1044 goto out;
1045 }
1046
1047 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
1048 if (p == 0)
1049 {
1050 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1051 goto out;
1052 }
1053 bd_index = p[0];
1054
1055 bd_flush_ip_mac (bd_index);
1056
1057out:
1058 REPLY_MACRO (VL_API_BD_IP_MAC_FLUSH_REPLY);
1059}
1060
Neale Rannsb8d44812017-11-10 06:53:54 -08001061extern void l2_efp_filter_configure (vnet_main_t * vnet_main,
Neale Ranns6b9b41c2018-07-23 05:47:09 -04001062 u32 sw_if_index, u8 enable);
Neale Rannsb8d44812017-11-10 06:53:54 -08001063
1064static void
1065vl_api_l2_interface_efp_filter_t_handler (vl_api_l2_interface_efp_filter_t *
1066 mp)
1067{
1068 int rv;
1069 vl_api_l2_interface_efp_filter_reply_t *rmp;
1070 vnet_main_t *vnm = vnet_get_main ();
1071
Neale Ranns6b9b41c2018-07-23 05:47:09 -04001072 VALIDATE_SW_IF_INDEX (mp);
1073
Neale Rannsb8d44812017-11-10 06:53:54 -08001074 // enable/disable the feature
Neale Ranns6b9b41c2018-07-23 05:47:09 -04001075 l2_efp_filter_configure (vnm, ntohl (mp->sw_if_index), mp->enable_disable);
Neale Rannsb8d44812017-11-10 06:53:54 -08001076 rv = vnm->api_errno;
1077
Neale Ranns6b9b41c2018-07-23 05:47:09 -04001078 BAD_SW_IF_INDEX_LABEL;
Neale Rannsb8d44812017-11-10 06:53:54 -08001079 REPLY_MACRO (VL_API_L2_INTERFACE_EFP_FILTER_REPLY);
1080}
1081
1082static void
1083vl_api_l2_patch_add_del_t_handler (vl_api_l2_patch_add_del_t * mp)
1084{
1085 extern int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
1086 int is_add);
1087 vl_api_l2_patch_add_del_reply_t *rmp;
1088 int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
1089 int is_add);
1090 int rv = 0;
1091
1092 VALIDATE_RX_SW_IF_INDEX (mp);
1093 VALIDATE_TX_SW_IF_INDEX (mp);
1094
1095 rv = vnet_l2_patch_add_del (ntohl (mp->rx_sw_if_index),
1096 ntohl (mp->tx_sw_if_index),
1097 (int) (mp->is_add != 0));
1098
1099 BAD_RX_SW_IF_INDEX_LABEL;
1100 BAD_TX_SW_IF_INDEX_LABEL;
1101
1102 REPLY_MACRO (VL_API_L2_PATCH_ADD_DEL_REPLY);
1103}
1104
1105static void
1106vl_api_sw_interface_set_vpath_t_handler (vl_api_sw_interface_set_vpath_t * mp)
1107{
1108 vl_api_sw_interface_set_vpath_reply_t *rmp;
1109 int rv = 0;
1110 u32 sw_if_index = ntohl (mp->sw_if_index);
1111
1112 VALIDATE_SW_IF_INDEX (mp);
1113
1114 l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_VPATH, mp->enable);
1115 vnet_feature_enable_disable ("ip4-unicast", "vpath-input-ip4",
1116 sw_if_index, mp->enable, 0, 0);
1117 vnet_feature_enable_disable ("ip4-multicast", "vpath-input-ip4",
1118 sw_if_index, mp->enable, 0, 0);
1119 vnet_feature_enable_disable ("ip6-unicast", "vpath-input-ip6",
1120 sw_if_index, mp->enable, 0, 0);
1121 vnet_feature_enable_disable ("ip6-multicast", "vpath-input-ip6",
1122 sw_if_index, mp->enable, 0, 0);
1123
1124 BAD_SW_IF_INDEX_LABEL;
1125
1126 REPLY_MACRO (VL_API_SW_INTERFACE_SET_VPATH_REPLY);
1127}
1128
Neale Ranns192b13f2019-03-15 02:16:20 -07001129static void
1130vl_api_bvi_create_t_handler (vl_api_bvi_create_t * mp)
1131{
1132 vl_api_bvi_create_reply_t *rmp;
1133 mac_address_t mac;
1134 u32 sw_if_index;
1135 int rv;
1136
1137 mac_address_decode (mp->mac, &mac);
1138
1139 rv = l2_bvi_create (ntohl (mp->user_instance), &mac, &sw_if_index);
1140
1141 /* *INDENT-OFF* */
1142 REPLY_MACRO2(VL_API_BVI_CREATE_REPLY,
1143 ({
1144 rmp->sw_if_index = ntohl (sw_if_index);
1145 }));
1146 /* *INDENT-ON* */
1147}
1148
1149static void
1150vl_api_bvi_delete_t_handler (vl_api_bvi_delete_t * mp)
1151{
1152 vl_api_bvi_delete_reply_t *rmp;
1153 int rv;
1154
1155 rv = l2_bvi_delete (ntohl (mp->sw_if_index));
1156
1157 REPLY_MACRO (VL_API_BVI_DELETE_REPLY);
1158}
1159
Neale Rannscbe25aa2019-09-30 10:53:31 +00001160static bool
1161l2_arp_term_publish_event_is_equal (const l2_arp_term_publish_event_t * e1,
1162 const l2_arp_term_publish_event_t * e2)
1163{
1164 if (e1 == NULL || e2 == NULL)
1165 return false;
1166 return (ip46_address_is_equal (&e1->ip, &e2->ip) &&
1167 (e1->sw_if_index == e2->sw_if_index) &&
1168 (mac_address_equal (&e1->mac, &e2->mac)));
1169}
1170
1171static uword
1172l2_arp_term_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1173 vlib_frame_t * f)
1174{
1175 /* These cross the longjmp boundary (vlib_process_wait_for_event)
1176 * and need to be volatile - to prevent them from being optimized into
1177 * a register - which could change during suspension */
1178 volatile f64 last = vlib_time_now (vm);
1179 volatile l2_arp_term_publish_event_t last_event = { };
1180
1181 l2_arp_term_main_t *l2am = &l2_arp_term_main;
1182
1183 while (1)
1184 {
1185 uword event_type = L2_ARP_TERM_EVENT_PUBLISH;
1186 vpe_client_registration_t *reg;
1187 f64 now;
1188
1189 vlib_process_wait_for_event (vm);
1190
1191 vlib_process_get_event_data (vm, &event_type);
1192 now = vlib_time_now (vm);
1193
1194 if (event_type == L2_ARP_TERM_EVENT_PUBLISH)
1195 {
1196 l2_arp_term_publish_event_t *event;
1197
1198 vec_foreach (event, l2am->publish_events)
1199 {
1200 /* dampen duplicate events - cast away volatile */
1201 if (l2_arp_term_publish_event_is_equal
1202 (event, (l2_arp_term_publish_event_t *) & last_event) &&
1203 (now - last) < 10.0)
1204 {
1205 continue;
1206 }
1207 last_event = *event;
1208 last = now;
1209
1210 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001211 pool_foreach (reg, vpe_api_main.l2_arp_term_events_registrations)
1212 {
Neale Rannscbe25aa2019-09-30 10:53:31 +00001213 vl_api_registration_t *vl_reg;
1214 vl_reg = vl_api_client_index_to_registration (reg->client_index);
Dave Barachffd15462020-02-18 10:12:23 -05001215 ALWAYS_ASSERT (vl_reg != NULL);
Neale Rannscbe25aa2019-09-30 10:53:31 +00001216
1217 if (reg && vl_api_can_send_msg (vl_reg))
1218 {
1219 vl_api_l2_arp_term_event_t * vevent;
1220 vevent = vl_msg_api_alloc (sizeof *vevent);
1221 clib_memset (vevent, 0, sizeof *vevent);
1222 vevent->_vl_msg_id = htons (VL_API_L2_ARP_TERM_EVENT);
1223 vevent->client_index = reg->client_index;
1224 vevent->pid = reg->client_pid;
1225 ip_address_encode(&event->ip,
1226 event->type,
1227 &vevent->ip);
1228 vevent->sw_if_index = htonl(event->sw_if_index);
1229 mac_address_encode(&event->mac, vevent->mac);
1230 vl_api_send_msg (vl_reg, (u8 *) vevent);
1231 }
Damjan Marionb2c31b62020-12-13 21:47:40 +01001232 }
Neale Rannscbe25aa2019-09-30 10:53:31 +00001233 /* *INDENT-ON* */
1234 }
1235 vec_reset_length (l2am->publish_events);
1236 }
1237 }
1238
1239 return 0;
1240}
1241
1242/* *INDENT-OFF* */
1243VLIB_REGISTER_NODE (l2_arp_term_process_node) = {
1244 .function = l2_arp_term_process,
1245 .type = VLIB_NODE_TYPE_PROCESS,
1246 .name = "l2-arp-term-publisher",
1247};
1248/* *INDENT-ON* */
1249
1250static void
1251vl_api_want_l2_arp_term_events_t_handler (vl_api_want_l2_arp_term_events_t *
1252 mp)
1253{
1254 vl_api_want_l2_arp_term_events_reply_t *rmp;
1255 vpe_api_main_t *am = &vpe_api_main;
1256 vpe_client_registration_t *rp;
1257 int rv = 0;
1258 uword *p;
1259
1260 p = hash_get (am->l2_arp_term_events_registration_hash, mp->client_index);
1261
1262 if (p)
1263 {
1264 if (mp->enable)
1265 {
1266 clib_warning ("pid %d: already enabled...", mp->pid);
1267 rv = VNET_API_ERROR_INVALID_REGISTRATION;
1268 goto reply;
1269 }
1270 else
1271 {
1272 rp = pool_elt_at_index (am->l2_arp_term_events_registrations, p[0]);
1273 pool_put (am->l2_arp_term_events_registrations, rp);
1274 hash_unset (am->l2_arp_term_events_registration_hash,
1275 mp->client_index);
1276 if (pool_elts (am->l2_arp_term_events_registrations) == 0)
1277 l2_arp_term_set_publisher_node (false);
1278 goto reply;
1279 }
1280 }
1281 if (mp->enable == 0)
1282 {
1283 clib_warning ("pid %d: already disabled...", mp->pid);
1284 rv = VNET_API_ERROR_INVALID_REGISTRATION;
1285 goto reply;
1286 }
1287 pool_get (am->l2_arp_term_events_registrations, rp);
1288 rp->client_index = mp->client_index;
1289 rp->client_pid = mp->pid;
1290 hash_set (am->l2_arp_term_events_registration_hash, rp->client_index,
1291 rp - am->l2_arp_term_events_registrations);
1292 l2_arp_term_set_publisher_node (true);
1293
1294reply:
1295 REPLY_MACRO (VL_API_WANT_L2_ARP_TERM_EVENTS_REPLY);
1296}
1297
1298static clib_error_t *
1299want_l2_arp_term_events_reaper (u32 client_index)
1300{
1301 vpe_client_registration_t *rp;
1302 vpe_api_main_t *am;
1303 uword *p;
1304
1305 am = &vpe_api_main;
1306
1307 /* remove from the registration hash */
1308 p = hash_get (am->l2_arp_term_events_registration_hash, client_index);
1309
1310 if (p)
1311 {
1312 rp = pool_elt_at_index (am->l2_arp_term_events_registrations, p[0]);
1313 pool_put (am->l2_arp_term_events_registrations, rp);
1314 hash_unset (am->l2_arp_term_events_registration_hash, client_index);
1315 if (pool_elts (am->l2_arp_term_events_registrations) == 0)
1316 l2_arp_term_set_publisher_node (false);
1317 }
1318 return (NULL);
1319}
1320
1321VL_MSG_API_REAPER_FUNCTION (want_l2_arp_term_events_reaper);
1322
Matus Fabianf468e232016-12-02 06:00:53 -08001323/*
Pavel Kotucek0f971d82017-01-03 10:48:54 +01001324 * l2_api_hookup
Matus Fabianf468e232016-12-02 06:00:53 -08001325 * Add vpe's API message handlers to the table.
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -07001326 * vlib has already mapped shared memory and
Matus Fabianf468e232016-12-02 06:00:53 -08001327 * added the client registration handlers.
1328 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
1329 */
1330#define vl_msg_name_crc_list
1331#include <vnet/vnet_all_api_h.h>
1332#undef vl_msg_name_crc_list
1333
1334static void
1335setup_message_id_table (api_main_t * am)
1336{
1337#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1338 foreach_vl_msg_name_crc_l2;
1339#undef _
1340}
1341
1342static clib_error_t *
1343l2_api_hookup (vlib_main_t * vm)
1344{
Dave Barach39d69112019-11-27 11:42:13 -05001345 api_main_t *am = vlibapi_get_main ();
Matus Fabianf468e232016-12-02 06:00:53 -08001346
1347#define _(N,n) \
1348 vl_msg_api_set_handlers(VL_API_##N, #n, \
1349 vl_api_##n##_t_handler, \
1350 vl_noop_handler, \
1351 vl_api_##n##_t_endian, \
1352 vl_api_##n##_t_print, \
1353 sizeof(vl_api_##n##_t), 1);
1354 foreach_vpe_api_msg;
1355#undef _
1356
Steven Luongc5fa2b82019-04-25 11:19:49 -07001357 /* Mark VL_API_BRIDGE_DOMAIN_DUMP as mp safe */
1358 am->is_mp_safe[VL_API_BRIDGE_DOMAIN_DUMP] = 1;
1359
Matus Fabianf468e232016-12-02 06:00:53 -08001360 /*
1361 * Set up the (msg_name, crc, message-id) table
1362 */
1363 setup_message_id_table (am);
1364
1365 return 0;
1366}
1367
1368VLIB_API_INIT_FUNCTION (l2_api_hookup);
1369
1370/*
1371 * fd.io coding-style-patch-verification: ON
1372 *
1373 * Local Variables:
1374 * eval: (c-set-style "gnu")
1375 * End:
1376 */