blob: c8690c6ccaeaa54e785dd5a8e08b08ff49d0ef05 [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
Ole Troan01384fe2017-05-12 11:55:35 +020045#define vl_api_bridge_domain_details_t_endian vl_noop_handler
46#define vl_api_bridge_domain_details_t_print vl_noop_handler
47
Matus Fabianf468e232016-12-02 06:00:53 -080048/* instantiate all the print functions we know about */
49#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
50#define vl_printfun
51#include <vnet/vnet_all_api_h.h>
52#undef vl_printfun
53
54#include <vlibapi/api_helper_macros.h>
55
Pavel Kotucek0f971d82017-01-03 10:48:54 +010056#define foreach_vpe_api_msg \
57_(L2_XCONNECT_DUMP, l2_xconnect_dump) \
58_(L2_FIB_CLEAR_TABLE, l2_fib_clear_table) \
59_(L2_FIB_TABLE_DUMP, l2_fib_table_dump) \
Eyal Bari7537e712017-04-27 14:07:55 +030060_(L2FIB_FLUSH_ALL, l2fib_flush_all) \
Eyal Barif24991c2017-04-05 05:33:21 +030061_(L2FIB_FLUSH_INT, l2fib_flush_int) \
62_(L2FIB_FLUSH_BD, l2fib_flush_bd) \
Pavel Kotucek0f971d82017-01-03 10:48:54 +010063_(L2FIB_ADD_DEL, l2fib_add_del) \
John Lo8d00fff2017-08-03 00:35:36 -040064_(WANT_L2_MACS_EVENTS, want_l2_macs_events) \
Pavel Kotucek0f971d82017-01-03 10:48:54 +010065_(L2_FLAGS, l2_flags) \
Neale Rannsb8d44812017-11-10 06:53:54 -080066_(SW_INTERFACE_SET_L2_XCONNECT, sw_interface_set_l2_xconnect) \
67_(SW_INTERFACE_SET_L2_BRIDGE, sw_interface_set_l2_bridge) \
68_(L2_PATCH_ADD_DEL, l2_patch_add_del) \
69_(L2_INTERFACE_EFP_FILTER, l2_interface_efp_filter) \
70_(BD_IP_MAC_ADD_DEL, bd_ip_mac_add_del) \
John Loe26c81f2019-01-07 15:16:33 -050071_(BD_IP_MAC_FLUSH, bd_ip_mac_flush) \
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +020072_(BD_IP_MAC_DUMP, bd_ip_mac_dump) \
Neale Rannsb8d44812017-11-10 06:53:54 -080073_(BRIDGE_DOMAIN_ADD_DEL, bridge_domain_add_del) \
74_(BRIDGE_DOMAIN_DUMP, bridge_domain_dump) \
75_(BRIDGE_FLAGS, bridge_flags) \
Pavel Kotucekadec5872017-01-25 08:50:53 +010076_(L2_INTERFACE_VLAN_TAG_REWRITE, l2_interface_vlan_tag_rewrite) \
Neale Rannsb8d44812017-11-10 06:53:54 -080077_(L2_INTERFACE_PBB_TAG_REWRITE, l2_interface_pbb_tag_rewrite) \
78_(BRIDGE_DOMAIN_SET_MAC_AGE, bridge_domain_set_mac_age) \
Neale Ranns192b13f2019-03-15 02:16:20 -070079_(SW_INTERFACE_SET_VPATH, sw_interface_set_vpath) \
80_(BVI_CREATE, bvi_create) \
Neale Rannscbe25aa2019-09-30 10:53:31 +000081_(BVI_DELETE, bvi_delete) \
82_(WANT_L2_ARP_TERM_EVENTS, want_l2_arp_term_events)
Matus Fabianf468e232016-12-02 06:00:53 -080083
84static void
Florin Coras6c4dae22018-01-09 06:39:23 -080085send_l2_xconnect_details (vl_api_registration_t * reg, u32 context,
Matus Fabianf468e232016-12-02 06:00:53 -080086 u32 rx_sw_if_index, u32 tx_sw_if_index)
87{
88 vl_api_l2_xconnect_details_t *mp;
89
90 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -040091 clib_memset (mp, 0, sizeof (*mp));
Matus Fabianf468e232016-12-02 06:00:53 -080092 mp->_vl_msg_id = ntohs (VL_API_L2_XCONNECT_DETAILS);
93 mp->context = context;
94 mp->rx_sw_if_index = htonl (rx_sw_if_index);
95 mp->tx_sw_if_index = htonl (tx_sw_if_index);
96
Florin Coras6c4dae22018-01-09 06:39:23 -080097 vl_api_send_msg (reg, (u8 *) mp);
Matus Fabianf468e232016-12-02 06:00:53 -080098}
99
100static void
101vl_api_l2_xconnect_dump_t_handler (vl_api_l2_xconnect_dump_t * mp)
102{
Florin Coras6c4dae22018-01-09 06:39:23 -0800103 vl_api_registration_t *reg;
Matus Fabianf468e232016-12-02 06:00:53 -0800104 vnet_main_t *vnm = vnet_get_main ();
105 vnet_interface_main_t *im = &vnm->interface_main;
106 l2input_main_t *l2im = &l2input_main;
107 vnet_sw_interface_t *swif;
108 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* */
115 pool_foreach (swif, im->sw_interfaces,
116 ({
117 config = vec_elt_at_index (l2im->configs, swif->sw_if_index);
Neale Ranns47a3d992020-09-29 15:38:51 +0000118 if (l2_input_is_xconnect(config))
Florin Coras6c4dae22018-01-09 06:39:23 -0800119 send_l2_xconnect_details (reg, mp->context, swif->sw_if_index,
Matus Fabianf468e232016-12-02 06:00:53 -0800120 config->output_sw_if_index);
121 }));
122 /* *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
John Lo8d00fff2017-08-03 00:35:36 -0400265vl_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)
277 {
278 lm->client_pid = pid;
279 lm->client_index = mp->client_index;
280
281 if (mp->max_macs_in_event)
282 fm->max_macs_in_event = mp->max_macs_in_event * 10;
283 else
Jakub Grajciar145e3302019-10-24 13:52:42 +0200284 {
285 rv = VNET_API_ERROR_INVALID_VALUE;
286 goto exit;
287 }
John Lo8d00fff2017-08-03 00:35:36 -0400288
289 if (mp->scan_delay)
290 fm->event_scan_delay = (f64) (mp->scan_delay) * 10e-3;
291 else
Jakub Grajciar145e3302019-10-24 13:52:42 +0200292 {
293 rv = VNET_API_ERROR_INVALID_VALUE;
294 goto exit;
295 }
John Lo8d00fff2017-08-03 00:35:36 -0400296
297 /* change learn limit and flush all learned MACs */
298 if (learn_limit && (learn_limit < L2LEARN_DEFAULT_LIMIT))
299 lm->global_learn_limit = learn_limit;
300 else
Jakub Grajciar145e3302019-10-24 13:52:42 +0200301 {
302 rv = VNET_API_ERROR_INVALID_VALUE;
303 goto exit;
304 }
John Lo8d00fff2017-08-03 00:35:36 -0400305
306 l2fib_flush_all_mac (vlib_get_main ());
307 }
308 else if (lm->client_pid != pid)
309 {
310 rv = VNET_API_ERROR_L2_MACS_EVENT_CLINET_PRESENT;
311 goto exit;
312 }
313 }
314 else if (lm->client_pid)
315 {
316 lm->client_pid = 0;
317 lm->client_index = 0;
John Lodbfa5742017-09-02 08:27:36 -0400318 if (learn_limit && (learn_limit < L2LEARN_DEFAULT_LIMIT))
John Loe531f4c2017-08-22 09:16:50 -0400319 lm->global_learn_limit = learn_limit;
320 else
321 lm->global_learn_limit = L2LEARN_DEFAULT_LIMIT;
John Lo8d00fff2017-08-03 00:35:36 -0400322 }
323
324exit:
325 REPLY_MACRO (VL_API_WANT_L2_MACS_EVENTS_REPLY);
326}
327
328static void
Eyal Barif24991c2017-04-05 05:33:21 +0300329vl_api_l2fib_flush_int_t_handler (vl_api_l2fib_flush_int_t * mp)
330{
331 int rv = 0;
332 vlib_main_t *vm = vlib_get_main ();
333 vl_api_l2fib_flush_int_reply_t *rmp;
334
335 VALIDATE_SW_IF_INDEX (mp);
336
337 u32 sw_if_index = ntohl (mp->sw_if_index);
338 l2fib_flush_int_mac (vm, sw_if_index);
339
340 BAD_SW_IF_INDEX_LABEL;
341 REPLY_MACRO (VL_API_L2FIB_FLUSH_INT_REPLY);
342}
343
344static void
Eyal Bari7537e712017-04-27 14:07:55 +0300345vl_api_l2fib_flush_all_t_handler (vl_api_l2fib_flush_all_t * mp)
346{
347 int rv = 0;
348 vl_api_l2fib_flush_all_reply_t *rmp;
349
350 l2fib_flush_all_mac (vlib_get_main ());
351 REPLY_MACRO (VL_API_L2FIB_FLUSH_ALL_REPLY);
352}
353
354static void
Eyal Barif24991c2017-04-05 05:33:21 +0300355vl_api_l2fib_flush_bd_t_handler (vl_api_l2fib_flush_bd_t * mp)
356{
357 int rv = 0;
358 vlib_main_t *vm = vlib_get_main ();
359 bd_main_t *bdm = &bd_main;
360 vl_api_l2fib_flush_bd_reply_t *rmp;
361
362 u32 bd_id = ntohl (mp->bd_id);
363 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
364 if (p == 0)
365 {
366 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
367 goto out;
368 }
369 l2fib_flush_bd_mac (vm, *p);
370out:
371 REPLY_MACRO (VL_API_L2FIB_FLUSH_BD_REPLY);
372}
373
374static void
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100375vl_api_l2_flags_t_handler (vl_api_l2_flags_t * mp)
376{
377 vl_api_l2_flags_reply_t *rmp;
378 int rv = 0;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100379 u32 rbm = 0;
380
381 VALIDATE_SW_IF_INDEX (mp);
382
Eyal Barifead6702017-04-04 04:46:32 +0300383 u32 sw_if_index = ntohl (mp->sw_if_index);
John Lo8d00fff2017-08-03 00:35:36 -0400384 u32 flags = ntohl (mp->feature_bitmap);
385 u32 bitmap = 0;
386
387 if (flags & L2_LEARN)
388 bitmap |= L2INPUT_FEAT_LEARN;
389
390 if (flags & L2_FWD)
391 bitmap |= L2INPUT_FEAT_FWD;
392
393 if (flags & L2_FLOOD)
394 bitmap |= L2INPUT_FEAT_FLOOD;
395
396 if (flags & L2_UU_FLOOD)
397 bitmap |= L2INPUT_FEAT_UU_FLOOD;
398
399 if (flags & L2_ARP_TERM)
400 bitmap |= L2INPUT_FEAT_ARP_TERM;
401
402 rbm = l2input_intf_bitmap_enable (sw_if_index, bitmap, mp->is_set);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100403
404 BAD_SW_IF_INDEX_LABEL;
405
406 /* *INDENT-OFF* */
407 REPLY_MACRO2(VL_API_L2_FLAGS_REPLY,
408 ({
409 rmp->resulting_feature_bitmap = ntohl(rbm);
410 }));
411 /* *INDENT-ON* */
412}
413
414static void
Eyal Barifead6702017-04-04 04:46:32 +0300415vl_api_bridge_domain_set_mac_age_t_handler (vl_api_bridge_domain_set_mac_age_t
416 * mp)
417{
418 vlib_main_t *vm = vlib_get_main ();
419 bd_main_t *bdm = &bd_main;
420 vl_api_bridge_domain_set_mac_age_reply_t *rmp;
421 int rv = 0;
422 u32 bd_id = ntohl (mp->bd_id);
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500423 uword *p;
424
425 if (bd_id == 0)
426 {
427 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
428 goto out;
429 }
430
431 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
Eyal Barifead6702017-04-04 04:46:32 +0300432 if (p == 0)
433 {
434 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
435 goto out;
436 }
437 bd_set_mac_age (vm, *p, mp->mac_age);
438out:
439 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_SET_MAC_AGE_REPLY);
440}
441
442static void
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100443vl_api_bridge_domain_add_del_t_handler (vl_api_bridge_domain_add_del_t * mp)
444{
Eyal Barib1352ed2017-04-07 23:14:17 +0300445 l2_bridge_domain_add_del_args_t a = {
446 .is_add = mp->is_add,
447 .flood = mp->flood,
448 .uu_flood = mp->uu_flood,
449 .forward = mp->forward,
450 .learn = mp->learn,
451 .arp_term = mp->arp_term,
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +0200452 .arp_ufwd = mp->arp_ufwd,
Eyal Barib1352ed2017-04-07 23:14:17 +0300453 .mac_age = mp->mac_age,
454 .bd_id = ntohl (mp->bd_id),
Jerome Tollet48304142017-09-05 12:13:22 +0100455 .bd_tag = mp->bd_tag
Eyal Barib1352ed2017-04-07 23:14:17 +0300456 };
457
458 int rv = bd_add_del (&a);
459
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100460 vl_api_bridge_domain_add_del_reply_t *rmp;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100461 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_ADD_DEL_REPLY);
462}
463
464static void
Ole Troan01384fe2017-05-12 11:55:35 +0200465send_bridge_domain_details (l2input_main_t * l2im,
Florin Coras6c4dae22018-01-09 06:39:23 -0800466 vl_api_registration_t * reg,
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100467 l2_bridge_domain_t * bd_config,
468 u32 n_sw_ifs, u32 context)
469{
470 vl_api_bridge_domain_details_t *mp;
Ole Troan01384fe2017-05-12 11:55:35 +0200471 l2_flood_member_t *m;
472 vl_api_bridge_domain_sw_if_t *sw_ifs;
473 l2_input_config_t *input_cfg;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100474
Ole Troan01384fe2017-05-12 11:55:35 +0200475 mp = vl_msg_api_alloc (sizeof (*mp) +
476 (n_sw_ifs * sizeof (vl_api_bridge_domain_sw_if_t)));
Dave Barachb7b92992018-10-17 10:38:51 -0400477 clib_memset (mp, 0, sizeof (*mp));
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100478 mp->_vl_msg_id = ntohs (VL_API_BRIDGE_DOMAIN_DETAILS);
479 mp->bd_id = ntohl (bd_config->bd_id);
480 mp->flood = bd_feature_flood (bd_config);
481 mp->uu_flood = bd_feature_uu_flood (bd_config);
482 mp->forward = bd_feature_forward (bd_config);
483 mp->learn = bd_feature_learn (bd_config);
484 mp->arp_term = bd_feature_arp_term (bd_config);
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +0200485 mp->arp_ufwd = bd_feature_arp_ufwd (bd_config);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100486 mp->bvi_sw_if_index = ntohl (bd_config->bvi_sw_if_index);
Neale Rannsb4743802018-09-05 09:13:57 -0700487 mp->uu_fwd_sw_if_index = ntohl (bd_config->uu_fwd_sw_if_index);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100488 mp->mac_age = bd_config->mac_age;
Jerome Tollet48304142017-09-05 12:13:22 +0100489 if (bd_config->bd_tag)
490 {
491 strncpy ((char *) mp->bd_tag, (char *) bd_config->bd_tag,
492 ARRAY_LEN (mp->bd_tag) - 1);
493 mp->bd_tag[ARRAY_LEN (mp->bd_tag) - 1] = 0;
494 }
495
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100496 mp->context = context;
497
Ole Troan01384fe2017-05-12 11:55:35 +0200498 sw_ifs = (vl_api_bridge_domain_sw_if_t *) mp->sw_if_details;
499 vec_foreach (m, bd_config->members)
500 {
501 sw_ifs->sw_if_index = ntohl (m->sw_if_index);
502 input_cfg = vec_elt_at_index (l2im->configs, m->sw_if_index);
503 sw_ifs->shg = input_cfg->shg;
504 sw_ifs++;
505 mp->n_sw_ifs++;
506 }
507 mp->n_sw_ifs = htonl (mp->n_sw_ifs);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100508
Florin Coras6c4dae22018-01-09 06:39:23 -0800509 vl_api_send_msg (reg, (u8 *) mp);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100510}
511
512static void
513vl_api_bridge_domain_dump_t_handler (vl_api_bridge_domain_dump_t * mp)
514{
515 bd_main_t *bdm = &bd_main;
516 l2input_main_t *l2im = &l2input_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800517 vl_api_registration_t *reg;
Jakub Grajciar145e3302019-10-24 13:52:42 +0200518 u32 bd_id, bd_index, end, filter_sw_if_index;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100519
Florin Coras6c4dae22018-01-09 06:39:23 -0800520 reg = vl_api_client_index_to_registration (mp->client_index);
521 if (!reg)
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100522 return;
523
Jakub Grajciar145e3302019-10-24 13:52:42 +0200524 filter_sw_if_index = ntohl (mp->sw_if_index);
525 if (filter_sw_if_index != ~0)
526 return; /* UNIMPLEMENTED */
527
Florin Coras6c4dae22018-01-09 06:39:23 -0800528 bd_id = ntohl (mp->bd_id);
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500529 if (bd_id == 0)
530 return;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100531
Eyal Bari259fca72017-05-14 10:38:39 +0300532 if (bd_id == ~0)
533 bd_index = 0, end = vec_len (l2im->bd_configs);
534 else
535 {
536 bd_index = bd_find_index (bdm, bd_id);
537 if (bd_index == ~0)
538 return;
539
540 end = bd_index + 1;
541 }
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500542
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100543 for (; bd_index < end; bd_index++)
544 {
Eyal Bari259fca72017-05-14 10:38:39 +0300545 l2_bridge_domain_t *bd_config =
546 l2input_bd_config_from_index (l2im, bd_index);
Dave Barach11fb09e2020-08-06 12:10:09 -0400547 /* skip placeholder bd_id 0 */
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100548 if (bd_config && (bd_config->bd_id > 0))
Florin Coras6c4dae22018-01-09 06:39:23 -0800549 send_bridge_domain_details (l2im, reg, bd_config,
Ole Troan01384fe2017-05-12 11:55:35 +0200550 vec_len (bd_config->members),
551 mp->context);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100552 }
553}
554
Neale Rannsb4743802018-09-05 09:13:57 -0700555static bd_flags_t
556bd_flags_decode (vl_api_bd_flags_t v)
557{
558 bd_flags_t f = L2_NONE;
559
560 v = ntohl (v);
561
562 if (v & BRIDGE_API_FLAG_LEARN)
563 f |= L2_LEARN;
564 if (v & BRIDGE_API_FLAG_FWD)
565 f |= L2_FWD;
566 if (v & BRIDGE_API_FLAG_FLOOD)
567 f |= L2_FLOOD;
568 if (v & BRIDGE_API_FLAG_UU_FLOOD)
569 f |= L2_UU_FLOOD;
570 if (v & BRIDGE_API_FLAG_ARP_TERM)
571 f |= L2_ARP_TERM;
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +0200572 if (v & BRIDGE_API_FLAG_ARP_UFWD)
573 f |= L2_ARP_UFWD;
Neale Rannsb4743802018-09-05 09:13:57 -0700574
575 return (f);
576}
577
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100578static void
579vl_api_bridge_flags_t_handler (vl_api_bridge_flags_t * mp)
580{
581 vlib_main_t *vm = vlib_get_main ();
582 bd_main_t *bdm = &bd_main;
583 vl_api_bridge_flags_reply_t *rmp;
Dave Barach98d6b612019-01-03 09:30:00 -0500584 u32 bitmap = 0;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100585 int rv = 0;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100586
Neale Rannsb4743802018-09-05 09:13:57 -0700587 bd_flags_t flags = bd_flags_decode (mp->flags);
Eyal Bari259fca72017-05-14 10:38:39 +0300588 u32 bd_id = ntohl (mp->bd_id);
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500589 if (bd_id == 0)
590 {
591 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
592 goto out;
593 }
594
Eyal Bari259fca72017-05-14 10:38:39 +0300595 u32 bd_index = bd_find_index (bdm, bd_id);
596 if (bd_index == ~0)
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100597 {
598 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
599 goto out;
600 }
601
Dave Barach98d6b612019-01-03 09:30:00 -0500602 bitmap = bd_set_flags (vm, bd_index, flags, mp->is_set);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100603
604out:
605 /* *INDENT-OFF* */
606 REPLY_MACRO2(VL_API_BRIDGE_FLAGS_REPLY,
607 ({
John Lo8d00fff2017-08-03 00:35:36 -0400608 rmp->resulting_feature_bitmap = ntohl(bitmap);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100609 }));
610 /* *INDENT-ON* */
611}
Matus Fabianf468e232016-12-02 06:00:53 -0800612
Pavel Kotucekadec5872017-01-25 08:50:53 +0100613static void
614 vl_api_l2_interface_vlan_tag_rewrite_t_handler
615 (vl_api_l2_interface_vlan_tag_rewrite_t * mp)
616{
617 int rv = 0;
618 vl_api_l2_interface_vlan_tag_rewrite_reply_t *rmp;
619 vnet_main_t *vnm = vnet_get_main ();
620 vlib_main_t *vm = vlib_get_main ();
621 u32 vtr_op;
622
623 VALIDATE_SW_IF_INDEX (mp);
624
625 vtr_op = ntohl (mp->vtr_op);
626
627 /* The L2 code is unsuspicious */
628 switch (vtr_op)
629 {
630 case L2_VTR_DISABLED:
631 case L2_VTR_PUSH_1:
632 case L2_VTR_PUSH_2:
633 case L2_VTR_POP_1:
634 case L2_VTR_POP_2:
635 case L2_VTR_TRANSLATE_1_1:
636 case L2_VTR_TRANSLATE_1_2:
637 case L2_VTR_TRANSLATE_2_1:
638 case L2_VTR_TRANSLATE_2_2:
639 break;
640
641 default:
642 rv = VNET_API_ERROR_INVALID_VALUE;
643 goto bad_sw_if_index;
644 }
645
646 rv = l2vtr_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
647 ntohl (mp->push_dot1q), ntohl (mp->tag1),
648 ntohl (mp->tag2));
649
650 BAD_SW_IF_INDEX_LABEL;
651
652 REPLY_MACRO (VL_API_L2_INTERFACE_VLAN_TAG_REWRITE_REPLY);
653}
654
655static void
656 vl_api_l2_interface_pbb_tag_rewrite_t_handler
657 (vl_api_l2_interface_pbb_tag_rewrite_t * mp)
658{
659 vl_api_l2_interface_pbb_tag_rewrite_reply_t *rmp;
660 vnet_main_t *vnm = vnet_get_main ();
661 vlib_main_t *vm = vlib_get_main ();
662 u32 vtr_op;
663 int rv = 0;
Jakub Grajciar145e3302019-10-24 13:52:42 +0200664 mac_address_t b_dmac, b_smac;
Pavel Kotucekadec5872017-01-25 08:50:53 +0100665
666 VALIDATE_SW_IF_INDEX (mp);
667
668 vtr_op = ntohl (mp->vtr_op);
669
670 switch (vtr_op)
671 {
672 case L2_VTR_DISABLED:
673 case L2_VTR_PUSH_2:
674 case L2_VTR_POP_2:
675 case L2_VTR_TRANSLATE_2_1:
676 break;
677
678 default:
679 rv = VNET_API_ERROR_INVALID_VALUE;
680 goto bad_sw_if_index;
681 }
682
Jakub Grajciar145e3302019-10-24 13:52:42 +0200683 mac_address_decode (mp->b_dmac, &b_dmac);
684 mac_address_decode (mp->b_smac, &b_smac);
685
Pavel Kotucekadec5872017-01-25 08:50:53 +0100686 rv = l2pbb_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
Jakub Grajciar145e3302019-10-24 13:52:42 +0200687 b_dmac.bytes, b_smac.bytes, ntohs (mp->b_vlanid),
Pavel Kotucekadec5872017-01-25 08:50:53 +0100688 ntohl (mp->i_sid), ntohs (mp->outer_tag));
689
690 BAD_SW_IF_INDEX_LABEL;
691
692 REPLY_MACRO (VL_API_L2_INTERFACE_PBB_TAG_REWRITE_REPLY);
693}
694
Neale Rannsb8d44812017-11-10 06:53:54 -0800695static void
696 vl_api_sw_interface_set_l2_xconnect_t_handler
697 (vl_api_sw_interface_set_l2_xconnect_t * mp)
698{
699 vl_api_sw_interface_set_l2_xconnect_reply_t *rmp;
700 int rv = 0;
701 u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
702 u32 tx_sw_if_index = ntohl (mp->tx_sw_if_index);
703 vlib_main_t *vm = vlib_get_main ();
704 vnet_main_t *vnm = vnet_get_main ();
705
706 VALIDATE_RX_SW_IF_INDEX (mp);
707
708 if (mp->enable)
709 {
710 VALIDATE_TX_SW_IF_INDEX (mp);
711 rv = set_int_l2_mode (vm, vnm, MODE_L2_XC,
Neale Rannsb4743802018-09-05 09:13:57 -0700712 rx_sw_if_index, 0,
713 L2_BD_PORT_TYPE_NORMAL, 0, tx_sw_if_index);
Neale Rannsb8d44812017-11-10 06:53:54 -0800714 }
715 else
716 {
Neale Rannsb4743802018-09-05 09:13:57 -0700717 rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0,
718 L2_BD_PORT_TYPE_NORMAL, 0, 0);
Neale Rannsb8d44812017-11-10 06:53:54 -0800719 }
720
Alexander Chernavine178b272018-09-24 05:42:01 -0400721 switch (rv)
722 {
723 case MODE_ERROR_ETH:
724 rv = VNET_API_ERROR_NON_ETHERNET;
725 break;
726 case MODE_ERROR_BVI_DEF:
727 rv = VNET_API_ERROR_BD_ALREADY_HAS_BVI;
728 break;
729 }
730
Neale Rannsb8d44812017-11-10 06:53:54 -0800731 BAD_RX_SW_IF_INDEX_LABEL;
732 BAD_TX_SW_IF_INDEX_LABEL;
733
734 REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_XCONNECT_REPLY);
735}
736
Neale Rannsb4743802018-09-05 09:13:57 -0700737static int
738l2_bd_port_type_decode (vl_api_l2_port_type_t v, l2_bd_port_type_t * l)
739{
740 v = clib_net_to_host_u32 (v);
741
742 switch (v)
743 {
744 case L2_API_PORT_TYPE_NORMAL:
745 *l = L2_BD_PORT_TYPE_NORMAL;
746 return 0;
747 case L2_API_PORT_TYPE_BVI:
748 *l = L2_BD_PORT_TYPE_BVI;
749 return 0;
750 case L2_API_PORT_TYPE_UU_FWD:
751 *l = L2_BD_PORT_TYPE_UU_FWD;
752 return 0;
753 }
754
755 return (VNET_API_ERROR_INVALID_VALUE);
756}
757
Neale Rannsb8d44812017-11-10 06:53:54 -0800758static void
759 vl_api_sw_interface_set_l2_bridge_t_handler
760 (vl_api_sw_interface_set_l2_bridge_t * mp)
761{
762 bd_main_t *bdm = &bd_main;
763 vl_api_sw_interface_set_l2_bridge_reply_t *rmp;
764 int rv = 0;
765 vlib_main_t *vm = vlib_get_main ();
766 vnet_main_t *vnm = vnet_get_main ();
Neale Rannsb4743802018-09-05 09:13:57 -0700767 l2_bd_port_type_t pt;
Neale Rannsb8d44812017-11-10 06:53:54 -0800768
769 VALIDATE_RX_SW_IF_INDEX (mp);
770 u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
Neale Rannsb4743802018-09-05 09:13:57 -0700771 rv = l2_bd_port_type_decode (mp->port_type, &pt);
Neale Rannsb8d44812017-11-10 06:53:54 -0800772
Neale Rannsb4743802018-09-05 09:13:57 -0700773 if (0 != rv)
774 goto out;
Neale Rannsb8d44812017-11-10 06:53:54 -0800775 if (mp->enable)
776 {
777 VALIDATE_BD_ID (mp);
778 u32 bd_id = ntohl (mp->bd_id);
779 u32 bd_index = bd_find_or_add_bd_index (bdm, bd_id);
Neale Rannsb4743802018-09-05 09:13:57 -0700780
Neale Rannsb8d44812017-11-10 06:53:54 -0800781 rv = set_int_l2_mode (vm, vnm, MODE_L2_BRIDGE,
Neale Rannsb4743802018-09-05 09:13:57 -0700782 rx_sw_if_index, bd_index, pt, mp->shg, 0);
Neale Rannsb8d44812017-11-10 06:53:54 -0800783 }
784 else
785 {
Neale Rannsb4743802018-09-05 09:13:57 -0700786 rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0, pt, 0, 0);
Neale Rannsb8d44812017-11-10 06:53:54 -0800787 }
788
Alexander Chernavine178b272018-09-24 05:42:01 -0400789 switch (rv)
790 {
791 case MODE_ERROR_ETH:
792 rv = VNET_API_ERROR_NON_ETHERNET;
793 break;
794 case MODE_ERROR_BVI_DEF:
795 rv = VNET_API_ERROR_BD_ALREADY_HAS_BVI;
796 break;
797 }
798
Neale Rannsb8d44812017-11-10 06:53:54 -0800799 BAD_RX_SW_IF_INDEX_LABEL;
800 BAD_BD_ID_LABEL;
Neale Rannsb4743802018-09-05 09:13:57 -0700801out:
Neale Rannsb8d44812017-11-10 06:53:54 -0800802 REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_BRIDGE_REPLY);
803}
804
805static void
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200806send_bd_ip_mac_entry (vpe_api_main_t * am,
807 vl_api_registration_t * reg,
Neale Rannsbc764c82019-06-19 07:07:13 -0700808 u32 bd_id,
809 const ip46_address_t * ip,
810 ip46_type_t itype,
811 const mac_address_t * mac, u32 context)
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200812{
813 vl_api_bd_ip_mac_details_t *mp;
814
815 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400816 clib_memset (mp, 0, sizeof (*mp));
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200817 mp->_vl_msg_id = ntohs (VL_API_BD_IP_MAC_DETAILS);
818
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200819 mp->context = context;
Neale Rannsbc764c82019-06-19 07:07:13 -0700820 mp->entry.bd_id = ntohl (bd_id);
821
822 ip_address_encode (ip, itype, &mp->entry.ip);
823 mac_address_encode (mac, mp->entry.mac);
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200824
825 vl_api_send_msg (reg, (u8 *) mp);
826}
827
828static void
829vl_api_bd_ip_mac_dump_t_handler (vl_api_bd_ip_mac_dump_t * mp)
830{
831 vpe_api_main_t *am = &vpe_api_main;
832 bd_main_t *bdm = &bd_main;
833 l2_bridge_domain_t *bd_config;
834 u32 bd_id = ntohl (mp->bd_id);
Mohsin Kazmib24dfec2018-08-23 12:24:19 +0200835 u32 bd_index, start, end;
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200836 vl_api_registration_t *reg;
837 uword *p;
838
839 reg = vl_api_client_index_to_registration (mp->client_index);
840 if (!reg)
841 return;
842
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200843 /* see bd_id: ~0 means "any" */
844 if (bd_id == ~0)
Mohsin Kazmib24dfec2018-08-23 12:24:19 +0200845 {
846 start = 1;
847 end = vec_len (l2input_main.bd_configs);
848 }
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200849 else
850 {
851 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
852 if (p == 0)
853 return;
854
855 bd_index = p[0];
856 vec_validate (l2input_main.bd_configs, bd_index);
857 start = bd_index;
858 end = start + 1;
859 }
860
861 for (bd_index = start; bd_index < end; bd_index++)
862 {
863 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
864 if (bd_is_valid (bd_config))
865 {
866 ip4_address_t ip4_addr;
867 ip6_address_t *ip6_addr;
Neale Rannsbc764c82019-06-19 07:07:13 -0700868 mac_address_t mac;
869 u64 mac64;
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200870 bd_id = bd_config->bd_id;
871
872 /* *INDENT-OFF* */
Neale Rannsbc764c82019-06-19 07:07:13 -0700873 hash_foreach (ip4_addr.as_u32, mac64, bd_config->mac_by_ip4,
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200874 ({
Neale Rannsbc764c82019-06-19 07:07:13 -0700875 ip46_address_t ip = {
876 .ip4 = ip4_addr,
877 };
878 mac_address_from_u64(&mac, mac64);
879
880 send_bd_ip_mac_entry (am, reg, bd_id, &ip, IP46_TYPE_IP4,
881 &mac, mp->context);
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200882 }));
883
Neale Rannsbc764c82019-06-19 07:07:13 -0700884 hash_foreach_mem (ip6_addr, mac64, bd_config->mac_by_ip6,
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200885 ({
Neale Rannsbc764c82019-06-19 07:07:13 -0700886 ip46_address_t ip = {
887 .ip6 = *ip6_addr,
888 };
889 mac_address_from_u64(&mac, mac64);
890
891 send_bd_ip_mac_entry (am, reg, bd_id, &ip, IP46_TYPE_IP6,
892 &mac, mp->context);
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200893 }));
894 /* *INDENT-ON* */
895 }
896 }
897}
898
899static void
Neale Rannsb8d44812017-11-10 06:53:54 -0800900vl_api_bd_ip_mac_add_del_t_handler (vl_api_bd_ip_mac_add_del_t * mp)
901{
Neale Ranns4d5b9172018-10-24 02:57:49 -0700902 ip46_address_t ip_addr = ip46_address_initializer;
Neale Rannsb8d44812017-11-10 06:53:54 -0800903 vl_api_bd_ip_mac_add_del_reply_t *rmp;
Neale Ranns4d5b9172018-10-24 02:57:49 -0700904 bd_main_t *bdm = &bd_main;
905 u32 bd_index, bd_id;
906 mac_address_t mac;
907 ip46_type_t type;
Neale Rannsb8d44812017-11-10 06:53:54 -0800908 int rv = 0;
Neale Rannsb8d44812017-11-10 06:53:54 -0800909 uword *p;
910
Neale Rannsbc764c82019-06-19 07:07:13 -0700911 bd_id = ntohl (mp->entry.bd_id);
Neale Ranns4d5b9172018-10-24 02:57:49 -0700912
Neale Rannsb8d44812017-11-10 06:53:54 -0800913 if (bd_id == 0)
914 {
915 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
916 goto out;
917 }
918
919 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
920 if (p == 0)
921 {
922 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
923 goto out;
924 }
Neale Rannsb8d44812017-11-10 06:53:54 -0800925 bd_index = p[0];
Neale Ranns4d5b9172018-10-24 02:57:49 -0700926
Neale Rannsbc764c82019-06-19 07:07:13 -0700927 type = ip_address_decode (&mp->entry.ip, &ip_addr);
928 mac_address_decode (mp->entry.mac, &mac);
Neale Ranns4d5b9172018-10-24 02:57:49 -0700929
930 if (bd_add_del_ip_mac (bd_index, type, &ip_addr, &mac, mp->is_add))
Neale Rannsb8d44812017-11-10 06:53:54 -0800931 rv = VNET_API_ERROR_UNSPECIFIED;
932
933out:
934 REPLY_MACRO (VL_API_BD_IP_MAC_ADD_DEL_REPLY);
935}
936
John Loe26c81f2019-01-07 15:16:33 -0500937static void
938vl_api_bd_ip_mac_flush_t_handler (vl_api_bd_ip_mac_flush_t * mp)
939{
940 vl_api_bd_ip_mac_flush_reply_t *rmp;
941 bd_main_t *bdm = &bd_main;
942 u32 bd_index, bd_id;
943 int rv = 0;
944 uword *p;
945
946 bd_id = ntohl (mp->bd_id);
947
948 if (bd_id == 0)
949 {
950 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
951 goto out;
952 }
953
954 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
955 if (p == 0)
956 {
957 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
958 goto out;
959 }
960 bd_index = p[0];
961
962 bd_flush_ip_mac (bd_index);
963
964out:
965 REPLY_MACRO (VL_API_BD_IP_MAC_FLUSH_REPLY);
966}
967
Neale Rannsb8d44812017-11-10 06:53:54 -0800968extern void l2_efp_filter_configure (vnet_main_t * vnet_main,
Neale Ranns6b9b41c2018-07-23 05:47:09 -0400969 u32 sw_if_index, u8 enable);
Neale Rannsb8d44812017-11-10 06:53:54 -0800970
971static void
972vl_api_l2_interface_efp_filter_t_handler (vl_api_l2_interface_efp_filter_t *
973 mp)
974{
975 int rv;
976 vl_api_l2_interface_efp_filter_reply_t *rmp;
977 vnet_main_t *vnm = vnet_get_main ();
978
Neale Ranns6b9b41c2018-07-23 05:47:09 -0400979 VALIDATE_SW_IF_INDEX (mp);
980
Neale Rannsb8d44812017-11-10 06:53:54 -0800981 // enable/disable the feature
Neale Ranns6b9b41c2018-07-23 05:47:09 -0400982 l2_efp_filter_configure (vnm, ntohl (mp->sw_if_index), mp->enable_disable);
Neale Rannsb8d44812017-11-10 06:53:54 -0800983 rv = vnm->api_errno;
984
Neale Ranns6b9b41c2018-07-23 05:47:09 -0400985 BAD_SW_IF_INDEX_LABEL;
Neale Rannsb8d44812017-11-10 06:53:54 -0800986 REPLY_MACRO (VL_API_L2_INTERFACE_EFP_FILTER_REPLY);
987}
988
989static void
990vl_api_l2_patch_add_del_t_handler (vl_api_l2_patch_add_del_t * mp)
991{
992 extern int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
993 int is_add);
994 vl_api_l2_patch_add_del_reply_t *rmp;
995 int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
996 int is_add);
997 int rv = 0;
998
999 VALIDATE_RX_SW_IF_INDEX (mp);
1000 VALIDATE_TX_SW_IF_INDEX (mp);
1001
1002 rv = vnet_l2_patch_add_del (ntohl (mp->rx_sw_if_index),
1003 ntohl (mp->tx_sw_if_index),
1004 (int) (mp->is_add != 0));
1005
1006 BAD_RX_SW_IF_INDEX_LABEL;
1007 BAD_TX_SW_IF_INDEX_LABEL;
1008
1009 REPLY_MACRO (VL_API_L2_PATCH_ADD_DEL_REPLY);
1010}
1011
1012static void
1013vl_api_sw_interface_set_vpath_t_handler (vl_api_sw_interface_set_vpath_t * mp)
1014{
1015 vl_api_sw_interface_set_vpath_reply_t *rmp;
1016 int rv = 0;
1017 u32 sw_if_index = ntohl (mp->sw_if_index);
1018
1019 VALIDATE_SW_IF_INDEX (mp);
1020
1021 l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_VPATH, mp->enable);
1022 vnet_feature_enable_disable ("ip4-unicast", "vpath-input-ip4",
1023 sw_if_index, mp->enable, 0, 0);
1024 vnet_feature_enable_disable ("ip4-multicast", "vpath-input-ip4",
1025 sw_if_index, mp->enable, 0, 0);
1026 vnet_feature_enable_disable ("ip6-unicast", "vpath-input-ip6",
1027 sw_if_index, mp->enable, 0, 0);
1028 vnet_feature_enable_disable ("ip6-multicast", "vpath-input-ip6",
1029 sw_if_index, mp->enable, 0, 0);
1030
1031 BAD_SW_IF_INDEX_LABEL;
1032
1033 REPLY_MACRO (VL_API_SW_INTERFACE_SET_VPATH_REPLY);
1034}
1035
Neale Ranns192b13f2019-03-15 02:16:20 -07001036static void
1037vl_api_bvi_create_t_handler (vl_api_bvi_create_t * mp)
1038{
1039 vl_api_bvi_create_reply_t *rmp;
1040 mac_address_t mac;
1041 u32 sw_if_index;
1042 int rv;
1043
1044 mac_address_decode (mp->mac, &mac);
1045
1046 rv = l2_bvi_create (ntohl (mp->user_instance), &mac, &sw_if_index);
1047
1048 /* *INDENT-OFF* */
1049 REPLY_MACRO2(VL_API_BVI_CREATE_REPLY,
1050 ({
1051 rmp->sw_if_index = ntohl (sw_if_index);
1052 }));
1053 /* *INDENT-ON* */
1054}
1055
1056static void
1057vl_api_bvi_delete_t_handler (vl_api_bvi_delete_t * mp)
1058{
1059 vl_api_bvi_delete_reply_t *rmp;
1060 int rv;
1061
1062 rv = l2_bvi_delete (ntohl (mp->sw_if_index));
1063
1064 REPLY_MACRO (VL_API_BVI_DELETE_REPLY);
1065}
1066
Neale Rannscbe25aa2019-09-30 10:53:31 +00001067static bool
1068l2_arp_term_publish_event_is_equal (const l2_arp_term_publish_event_t * e1,
1069 const l2_arp_term_publish_event_t * e2)
1070{
1071 if (e1 == NULL || e2 == NULL)
1072 return false;
1073 return (ip46_address_is_equal (&e1->ip, &e2->ip) &&
1074 (e1->sw_if_index == e2->sw_if_index) &&
1075 (mac_address_equal (&e1->mac, &e2->mac)));
1076}
1077
1078static uword
1079l2_arp_term_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1080 vlib_frame_t * f)
1081{
1082 /* These cross the longjmp boundary (vlib_process_wait_for_event)
1083 * and need to be volatile - to prevent them from being optimized into
1084 * a register - which could change during suspension */
1085 volatile f64 last = vlib_time_now (vm);
1086 volatile l2_arp_term_publish_event_t last_event = { };
1087
1088 l2_arp_term_main_t *l2am = &l2_arp_term_main;
1089
1090 while (1)
1091 {
1092 uword event_type = L2_ARP_TERM_EVENT_PUBLISH;
1093 vpe_client_registration_t *reg;
1094 f64 now;
1095
1096 vlib_process_wait_for_event (vm);
1097
1098 vlib_process_get_event_data (vm, &event_type);
1099 now = vlib_time_now (vm);
1100
1101 if (event_type == L2_ARP_TERM_EVENT_PUBLISH)
1102 {
1103 l2_arp_term_publish_event_t *event;
1104
1105 vec_foreach (event, l2am->publish_events)
1106 {
1107 /* dampen duplicate events - cast away volatile */
1108 if (l2_arp_term_publish_event_is_equal
1109 (event, (l2_arp_term_publish_event_t *) & last_event) &&
1110 (now - last) < 10.0)
1111 {
1112 continue;
1113 }
1114 last_event = *event;
1115 last = now;
1116
1117 /* *INDENT-OFF* */
1118 pool_foreach(reg, vpe_api_main.l2_arp_term_events_registrations,
1119 ({
1120 vl_api_registration_t *vl_reg;
1121 vl_reg = vl_api_client_index_to_registration (reg->client_index);
Dave Barachffd15462020-02-18 10:12:23 -05001122 ALWAYS_ASSERT (vl_reg != NULL);
Neale Rannscbe25aa2019-09-30 10:53:31 +00001123
1124 if (reg && vl_api_can_send_msg (vl_reg))
1125 {
1126 vl_api_l2_arp_term_event_t * vevent;
1127 vevent = vl_msg_api_alloc (sizeof *vevent);
1128 clib_memset (vevent, 0, sizeof *vevent);
1129 vevent->_vl_msg_id = htons (VL_API_L2_ARP_TERM_EVENT);
1130 vevent->client_index = reg->client_index;
1131 vevent->pid = reg->client_pid;
1132 ip_address_encode(&event->ip,
1133 event->type,
1134 &vevent->ip);
1135 vevent->sw_if_index = htonl(event->sw_if_index);
1136 mac_address_encode(&event->mac, vevent->mac);
1137 vl_api_send_msg (vl_reg, (u8 *) vevent);
1138 }
1139 }));
1140 /* *INDENT-ON* */
1141 }
1142 vec_reset_length (l2am->publish_events);
1143 }
1144 }
1145
1146 return 0;
1147}
1148
1149/* *INDENT-OFF* */
1150VLIB_REGISTER_NODE (l2_arp_term_process_node) = {
1151 .function = l2_arp_term_process,
1152 .type = VLIB_NODE_TYPE_PROCESS,
1153 .name = "l2-arp-term-publisher",
1154};
1155/* *INDENT-ON* */
1156
1157static void
1158vl_api_want_l2_arp_term_events_t_handler (vl_api_want_l2_arp_term_events_t *
1159 mp)
1160{
1161 vl_api_want_l2_arp_term_events_reply_t *rmp;
1162 vpe_api_main_t *am = &vpe_api_main;
1163 vpe_client_registration_t *rp;
1164 int rv = 0;
1165 uword *p;
1166
1167 p = hash_get (am->l2_arp_term_events_registration_hash, mp->client_index);
1168
1169 if (p)
1170 {
1171 if (mp->enable)
1172 {
1173 clib_warning ("pid %d: already enabled...", mp->pid);
1174 rv = VNET_API_ERROR_INVALID_REGISTRATION;
1175 goto reply;
1176 }
1177 else
1178 {
1179 rp = pool_elt_at_index (am->l2_arp_term_events_registrations, p[0]);
1180 pool_put (am->l2_arp_term_events_registrations, rp);
1181 hash_unset (am->l2_arp_term_events_registration_hash,
1182 mp->client_index);
1183 if (pool_elts (am->l2_arp_term_events_registrations) == 0)
1184 l2_arp_term_set_publisher_node (false);
1185 goto reply;
1186 }
1187 }
1188 if (mp->enable == 0)
1189 {
1190 clib_warning ("pid %d: already disabled...", mp->pid);
1191 rv = VNET_API_ERROR_INVALID_REGISTRATION;
1192 goto reply;
1193 }
1194 pool_get (am->l2_arp_term_events_registrations, rp);
1195 rp->client_index = mp->client_index;
1196 rp->client_pid = mp->pid;
1197 hash_set (am->l2_arp_term_events_registration_hash, rp->client_index,
1198 rp - am->l2_arp_term_events_registrations);
1199 l2_arp_term_set_publisher_node (true);
1200
1201reply:
1202 REPLY_MACRO (VL_API_WANT_L2_ARP_TERM_EVENTS_REPLY);
1203}
1204
1205static clib_error_t *
1206want_l2_arp_term_events_reaper (u32 client_index)
1207{
1208 vpe_client_registration_t *rp;
1209 vpe_api_main_t *am;
1210 uword *p;
1211
1212 am = &vpe_api_main;
1213
1214 /* remove from the registration hash */
1215 p = hash_get (am->l2_arp_term_events_registration_hash, client_index);
1216
1217 if (p)
1218 {
1219 rp = pool_elt_at_index (am->l2_arp_term_events_registrations, p[0]);
1220 pool_put (am->l2_arp_term_events_registrations, rp);
1221 hash_unset (am->l2_arp_term_events_registration_hash, client_index);
1222 if (pool_elts (am->l2_arp_term_events_registrations) == 0)
1223 l2_arp_term_set_publisher_node (false);
1224 }
1225 return (NULL);
1226}
1227
1228VL_MSG_API_REAPER_FUNCTION (want_l2_arp_term_events_reaper);
1229
Matus Fabianf468e232016-12-02 06:00:53 -08001230/*
Pavel Kotucek0f971d82017-01-03 10:48:54 +01001231 * l2_api_hookup
Matus Fabianf468e232016-12-02 06:00:53 -08001232 * Add vpe's API message handlers to the table.
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -07001233 * vlib has already mapped shared memory and
Matus Fabianf468e232016-12-02 06:00:53 -08001234 * added the client registration handlers.
1235 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
1236 */
1237#define vl_msg_name_crc_list
1238#include <vnet/vnet_all_api_h.h>
1239#undef vl_msg_name_crc_list
1240
1241static void
1242setup_message_id_table (api_main_t * am)
1243{
1244#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1245 foreach_vl_msg_name_crc_l2;
1246#undef _
1247}
1248
1249static clib_error_t *
1250l2_api_hookup (vlib_main_t * vm)
1251{
Dave Barach39d69112019-11-27 11:42:13 -05001252 api_main_t *am = vlibapi_get_main ();
Matus Fabianf468e232016-12-02 06:00:53 -08001253
1254#define _(N,n) \
1255 vl_msg_api_set_handlers(VL_API_##N, #n, \
1256 vl_api_##n##_t_handler, \
1257 vl_noop_handler, \
1258 vl_api_##n##_t_endian, \
1259 vl_api_##n##_t_print, \
1260 sizeof(vl_api_##n##_t), 1);
1261 foreach_vpe_api_msg;
1262#undef _
1263
Steven Luongc5fa2b82019-04-25 11:19:49 -07001264 /* Mark VL_API_BRIDGE_DOMAIN_DUMP as mp safe */
1265 am->is_mp_safe[VL_API_BRIDGE_DOMAIN_DUMP] = 1;
1266
Matus Fabianf468e232016-12-02 06:00:53 -08001267 /*
1268 * Set up the (msg_name, crc, message-id) table
1269 */
1270 setup_message_id_table (am);
1271
1272 return 0;
1273}
1274
1275VLIB_API_INIT_FUNCTION (l2_api_hookup);
1276
1277/*
1278 * fd.io coding-style-patch-verification: ON
1279 *
1280 * Local Variables:
1281 * eval: (c-set-style "gnu")
1282 * End:
1283 */