blob: ab3a7c6fd64722dc90a06131527e86813c1fa3e5 [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>
Matus Fabianf468e232016-12-02 06:00:53 -080029
30#include <vnet/vnet_msg_enum.h>
31
32#define vl_typedefs /* define message structures */
33#include <vnet/vnet_all_api_h.h>
34#undef vl_typedefs
35
36#define vl_endianfun /* define message structures */
37#include <vnet/vnet_all_api_h.h>
38#undef vl_endianfun
39
Ole Troan01384fe2017-05-12 11:55:35 +020040#define vl_api_bridge_domain_details_t_endian vl_noop_handler
41#define vl_api_bridge_domain_details_t_print vl_noop_handler
42
Matus Fabianf468e232016-12-02 06:00:53 -080043/* instantiate all the print functions we know about */
44#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
45#define vl_printfun
46#include <vnet/vnet_all_api_h.h>
47#undef vl_printfun
48
49#include <vlibapi/api_helper_macros.h>
50
Pavel Kotucek0f971d82017-01-03 10:48:54 +010051#define foreach_vpe_api_msg \
52_(L2_XCONNECT_DUMP, l2_xconnect_dump) \
53_(L2_FIB_CLEAR_TABLE, l2_fib_clear_table) \
54_(L2_FIB_TABLE_DUMP, l2_fib_table_dump) \
Eyal Bari7537e712017-04-27 14:07:55 +030055_(L2FIB_FLUSH_ALL, l2fib_flush_all) \
Eyal Barif24991c2017-04-05 05:33:21 +030056_(L2FIB_FLUSH_INT, l2fib_flush_int) \
57_(L2FIB_FLUSH_BD, l2fib_flush_bd) \
Pavel Kotucek0f971d82017-01-03 10:48:54 +010058_(L2FIB_ADD_DEL, l2fib_add_del) \
John Lo8d00fff2017-08-03 00:35:36 -040059_(WANT_L2_MACS_EVENTS, want_l2_macs_events) \
Pavel Kotucek0f971d82017-01-03 10:48:54 +010060_(L2_FLAGS, l2_flags) \
Neale Rannsb8d44812017-11-10 06:53:54 -080061_(SW_INTERFACE_SET_L2_XCONNECT, sw_interface_set_l2_xconnect) \
62_(SW_INTERFACE_SET_L2_BRIDGE, sw_interface_set_l2_bridge) \
63_(L2_PATCH_ADD_DEL, l2_patch_add_del) \
64_(L2_INTERFACE_EFP_FILTER, l2_interface_efp_filter) \
65_(BD_IP_MAC_ADD_DEL, bd_ip_mac_add_del) \
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +020066_(BD_IP_MAC_DUMP, bd_ip_mac_dump) \
Neale Rannsb8d44812017-11-10 06:53:54 -080067_(BRIDGE_DOMAIN_ADD_DEL, bridge_domain_add_del) \
68_(BRIDGE_DOMAIN_DUMP, bridge_domain_dump) \
69_(BRIDGE_FLAGS, bridge_flags) \
Pavel Kotucekadec5872017-01-25 08:50:53 +010070_(L2_INTERFACE_VLAN_TAG_REWRITE, l2_interface_vlan_tag_rewrite) \
Neale Rannsb8d44812017-11-10 06:53:54 -080071_(L2_INTERFACE_PBB_TAG_REWRITE, l2_interface_pbb_tag_rewrite) \
72_(BRIDGE_DOMAIN_SET_MAC_AGE, bridge_domain_set_mac_age) \
73_(SW_INTERFACE_SET_VPATH, sw_interface_set_vpath)
Matus Fabianf468e232016-12-02 06:00:53 -080074
75static void
Florin Coras6c4dae22018-01-09 06:39:23 -080076send_l2_xconnect_details (vl_api_registration_t * reg, u32 context,
Matus Fabianf468e232016-12-02 06:00:53 -080077 u32 rx_sw_if_index, u32 tx_sw_if_index)
78{
79 vl_api_l2_xconnect_details_t *mp;
80
81 mp = vl_msg_api_alloc (sizeof (*mp));
82 memset (mp, 0, sizeof (*mp));
83 mp->_vl_msg_id = ntohs (VL_API_L2_XCONNECT_DETAILS);
84 mp->context = context;
85 mp->rx_sw_if_index = htonl (rx_sw_if_index);
86 mp->tx_sw_if_index = htonl (tx_sw_if_index);
87
Florin Coras6c4dae22018-01-09 06:39:23 -080088 vl_api_send_msg (reg, (u8 *) mp);
Matus Fabianf468e232016-12-02 06:00:53 -080089}
90
91static void
92vl_api_l2_xconnect_dump_t_handler (vl_api_l2_xconnect_dump_t * mp)
93{
Florin Coras6c4dae22018-01-09 06:39:23 -080094 vl_api_registration_t *reg;
Matus Fabianf468e232016-12-02 06:00:53 -080095 vnet_main_t *vnm = vnet_get_main ();
96 vnet_interface_main_t *im = &vnm->interface_main;
97 l2input_main_t *l2im = &l2input_main;
98 vnet_sw_interface_t *swif;
99 l2_input_config_t *config;
100
Florin Coras6c4dae22018-01-09 06:39:23 -0800101 reg = vl_api_client_index_to_registration (mp->client_index);
102 if (!reg)
Matus Fabianf468e232016-12-02 06:00:53 -0800103 return;
104
105 /* *INDENT-OFF* */
106 pool_foreach (swif, im->sw_interfaces,
107 ({
108 config = vec_elt_at_index (l2im->configs, swif->sw_if_index);
109 if (config->xconnect)
Florin Coras6c4dae22018-01-09 06:39:23 -0800110 send_l2_xconnect_details (reg, mp->context, swif->sw_if_index,
Matus Fabianf468e232016-12-02 06:00:53 -0800111 config->output_sw_if_index);
112 }));
113 /* *INDENT-ON* */
114}
115
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100116static void
117vl_api_l2_fib_clear_table_t_handler (vl_api_l2_fib_clear_table_t * mp)
118{
119 int rv = 0;
120 vl_api_l2_fib_clear_table_reply_t *rmp;
121
Eyal Bari7537e712017-04-27 14:07:55 +0300122 /* Clear all MACs including static MACs */
123 l2fib_clear_table ();
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100124
125 REPLY_MACRO (VL_API_L2_FIB_CLEAR_TABLE_REPLY);
126}
127
128static void
129send_l2fib_table_entry (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -0800130 vl_api_registration_t * reg,
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100131 l2fib_entry_key_t * l2fe_key,
132 l2fib_entry_result_t * l2fe_res, u32 context)
133{
Ole Troan01384fe2017-05-12 11:55:35 +0200134 vl_api_l2_fib_table_details_t *mp;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100135
136 mp = vl_msg_api_alloc (sizeof (*mp));
137 memset (mp, 0, sizeof (*mp));
Ole Troan01384fe2017-05-12 11:55:35 +0200138 mp->_vl_msg_id = ntohs (VL_API_L2_FIB_TABLE_DETAILS);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100139
140 mp->bd_id =
141 ntohl (l2input_main.bd_configs[l2fe_key->fields.bd_index].bd_id);
142
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200143 clib_memcpy (mp->mac, l2fe_key->fields.mac, 6);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100144 mp->sw_if_index = ntohl (l2fe_res->fields.sw_if_index);
Neale Ranns7341b6d2018-09-19 04:07:02 -0700145 mp->static_mac = (l2fib_entry_result_is_set_STATIC (l2fe_res) ? 1 : 0);
146 mp->filter_mac = (l2fib_entry_result_is_set_FILTER (l2fe_res) ? 1 : 0);
147 mp->bvi_mac = (l2fib_entry_result_is_set_BVI (l2fe_res) ? 1 : 0);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100148 mp->context = context;
149
Florin Coras6c4dae22018-01-09 06:39:23 -0800150 vl_api_send_msg (reg, (u8 *) mp);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100151}
152
153static void
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100154vl_api_l2_fib_table_dump_t_handler (vl_api_l2_fib_table_dump_t * mp)
155{
156 vpe_api_main_t *am = &vpe_api_main;
157 bd_main_t *bdm = &bd_main;
158 l2fib_entry_key_t *l2fe_key = NULL;
159 l2fib_entry_result_t *l2fe_res = NULL;
160 u32 ni, bd_id = ntohl (mp->bd_id);
161 u32 bd_index;
Florin Coras6c4dae22018-01-09 06:39:23 -0800162 vl_api_registration_t *reg;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100163 uword *p;
164
Florin Coras6c4dae22018-01-09 06:39:23 -0800165 reg = vl_api_client_index_to_registration (mp->client_index);
166 if (!reg)
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100167 return;
168
169 /* see l2fib_table_dump: ~0 means "any" */
170 if (bd_id == ~0)
171 bd_index = ~0;
172 else
173 {
174 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
175 if (p == 0)
176 return;
177
178 bd_index = p[0];
179 }
180
181 l2fib_table_dump (bd_index, &l2fe_key, &l2fe_res);
182
183 vec_foreach_index (ni, l2fe_key)
184 {
Florin Coras6c4dae22018-01-09 06:39:23 -0800185 send_l2fib_table_entry (am, reg, vec_elt_at_index (l2fe_key, ni),
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100186 vec_elt_at_index (l2fe_res, ni), mp->context);
187 }
188 vec_free (l2fe_key);
189 vec_free (l2fe_res);
190}
191
192static void
193vl_api_l2fib_add_del_t_handler (vl_api_l2fib_add_del_t * mp)
194{
195 bd_main_t *bdm = &bd_main;
196 l2input_main_t *l2im = &l2input_main;
197 vl_api_l2fib_add_del_reply_t *rmp;
198 int rv = 0;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100199 u32 bd_id = ntohl (mp->bd_id);
Eyal Bari31a71ab2017-06-25 14:42:33 +0300200 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100201
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100202 if (!p)
203 {
204 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
205 goto bad_sw_if_index;
206 }
Eyal Bari31a71ab2017-06-25 14:42:33 +0300207 u32 bd_index = p[0];
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100208
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200209 u8 mac[6];
210
211 clib_memcpy (mac, mp->mac, 6);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100212 if (mp->is_add)
213 {
Eyal Bari31a71ab2017-06-25 14:42:33 +0300214 if (mp->filter_mac)
215 l2fib_add_filter_entry (mac, bd_index);
216 else
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100217 {
Neale Rannsb54d0812018-09-06 06:22:56 -0700218 l2fib_entry_result_flags_t flags = L2FIB_ENTRY_RESULT_FLAG_NONE;
Eyal Bari31a71ab2017-06-25 14:42:33 +0300219 u32 sw_if_index = ntohl (mp->sw_if_index);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100220 VALIDATE_SW_IF_INDEX (mp);
221 if (vec_len (l2im->configs) <= sw_if_index)
222 {
223 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
224 goto bad_sw_if_index;
225 }
226 else
227 {
228 l2_input_config_t *config;
229 config = vec_elt_at_index (l2im->configs, sw_if_index);
230 if (config->bridge == 0)
231 {
232 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
233 goto bad_sw_if_index;
234 }
235 }
Neale Rannsb54d0812018-09-06 06:22:56 -0700236 if (mp->static_mac)
237 flags |= L2FIB_ENTRY_RESULT_FLAG_STATIC;
238 if (mp->bvi_mac)
239 flags |= L2FIB_ENTRY_RESULT_FLAG_BVI;
240 l2fib_add_entry (mac, bd_index, sw_if_index, flags);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100241 }
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100242 }
243 else
244 {
John Lo7dbd7262018-05-31 10:25:18 -0400245 u32 sw_if_index = ntohl (mp->sw_if_index);
246 if (l2fib_del_entry (mac, bd_index, sw_if_index))
247 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100248 }
249
250 BAD_SW_IF_INDEX_LABEL;
251
252 REPLY_MACRO (VL_API_L2FIB_ADD_DEL_REPLY);
253}
254
255static void
John Lo8d00fff2017-08-03 00:35:36 -0400256vl_api_want_l2_macs_events_t_handler (vl_api_want_l2_macs_events_t * mp)
257{
258 int rv = 0;
259 vl_api_want_l2_macs_events_reply_t *rmp;
260 l2learn_main_t *lm = &l2learn_main;
261 l2fib_main_t *fm = &l2fib_main;
262 u32 pid = ntohl (mp->pid);
263 u32 learn_limit = ntohl (mp->learn_limit);
264
265 if (mp->enable_disable)
266 {
267 if (lm->client_pid == 0)
268 {
269 lm->client_pid = pid;
270 lm->client_index = mp->client_index;
271
272 if (mp->max_macs_in_event)
273 fm->max_macs_in_event = mp->max_macs_in_event * 10;
274 else
275 fm->max_macs_in_event = L2FIB_EVENT_MAX_MACS_DEFAULT;
276
277 if (mp->scan_delay)
278 fm->event_scan_delay = (f64) (mp->scan_delay) * 10e-3;
279 else
280 fm->event_scan_delay = L2FIB_EVENT_SCAN_DELAY_DEFAULT;
281
282 /* change learn limit and flush all learned MACs */
283 if (learn_limit && (learn_limit < L2LEARN_DEFAULT_LIMIT))
284 lm->global_learn_limit = learn_limit;
285 else
286 lm->global_learn_limit = L2FIB_EVENT_LEARN_LIMIT_DEFAULT;
287
288 l2fib_flush_all_mac (vlib_get_main ());
289 }
290 else if (lm->client_pid != pid)
291 {
292 rv = VNET_API_ERROR_L2_MACS_EVENT_CLINET_PRESENT;
293 goto exit;
294 }
295 }
296 else if (lm->client_pid)
297 {
298 lm->client_pid = 0;
299 lm->client_index = 0;
John Lodbfa5742017-09-02 08:27:36 -0400300 if (learn_limit && (learn_limit < L2LEARN_DEFAULT_LIMIT))
John Loe531f4c2017-08-22 09:16:50 -0400301 lm->global_learn_limit = learn_limit;
302 else
303 lm->global_learn_limit = L2LEARN_DEFAULT_LIMIT;
John Lo8d00fff2017-08-03 00:35:36 -0400304 }
305
306exit:
307 REPLY_MACRO (VL_API_WANT_L2_MACS_EVENTS_REPLY);
308}
309
310static void
Eyal Barif24991c2017-04-05 05:33:21 +0300311vl_api_l2fib_flush_int_t_handler (vl_api_l2fib_flush_int_t * mp)
312{
313 int rv = 0;
314 vlib_main_t *vm = vlib_get_main ();
315 vl_api_l2fib_flush_int_reply_t *rmp;
316
317 VALIDATE_SW_IF_INDEX (mp);
318
319 u32 sw_if_index = ntohl (mp->sw_if_index);
320 l2fib_flush_int_mac (vm, sw_if_index);
321
322 BAD_SW_IF_INDEX_LABEL;
323 REPLY_MACRO (VL_API_L2FIB_FLUSH_INT_REPLY);
324}
325
326static void
Eyal Bari7537e712017-04-27 14:07:55 +0300327vl_api_l2fib_flush_all_t_handler (vl_api_l2fib_flush_all_t * mp)
328{
329 int rv = 0;
330 vl_api_l2fib_flush_all_reply_t *rmp;
331
332 l2fib_flush_all_mac (vlib_get_main ());
333 REPLY_MACRO (VL_API_L2FIB_FLUSH_ALL_REPLY);
334}
335
336static void
Eyal Barif24991c2017-04-05 05:33:21 +0300337vl_api_l2fib_flush_bd_t_handler (vl_api_l2fib_flush_bd_t * mp)
338{
339 int rv = 0;
340 vlib_main_t *vm = vlib_get_main ();
341 bd_main_t *bdm = &bd_main;
342 vl_api_l2fib_flush_bd_reply_t *rmp;
343
344 u32 bd_id = ntohl (mp->bd_id);
345 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
346 if (p == 0)
347 {
348 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
349 goto out;
350 }
351 l2fib_flush_bd_mac (vm, *p);
352out:
353 REPLY_MACRO (VL_API_L2FIB_FLUSH_BD_REPLY);
354}
355
356static void
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100357vl_api_l2_flags_t_handler (vl_api_l2_flags_t * mp)
358{
359 vl_api_l2_flags_reply_t *rmp;
360 int rv = 0;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100361 u32 rbm = 0;
362
363 VALIDATE_SW_IF_INDEX (mp);
364
Eyal Barifead6702017-04-04 04:46:32 +0300365 u32 sw_if_index = ntohl (mp->sw_if_index);
John Lo8d00fff2017-08-03 00:35:36 -0400366 u32 flags = ntohl (mp->feature_bitmap);
367 u32 bitmap = 0;
368
369 if (flags & L2_LEARN)
370 bitmap |= L2INPUT_FEAT_LEARN;
371
372 if (flags & L2_FWD)
373 bitmap |= L2INPUT_FEAT_FWD;
374
375 if (flags & L2_FLOOD)
376 bitmap |= L2INPUT_FEAT_FLOOD;
377
378 if (flags & L2_UU_FLOOD)
379 bitmap |= L2INPUT_FEAT_UU_FLOOD;
380
381 if (flags & L2_ARP_TERM)
382 bitmap |= L2INPUT_FEAT_ARP_TERM;
383
384 rbm = l2input_intf_bitmap_enable (sw_if_index, bitmap, mp->is_set);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100385
386 BAD_SW_IF_INDEX_LABEL;
387
388 /* *INDENT-OFF* */
389 REPLY_MACRO2(VL_API_L2_FLAGS_REPLY,
390 ({
391 rmp->resulting_feature_bitmap = ntohl(rbm);
392 }));
393 /* *INDENT-ON* */
394}
395
396static void
Eyal Barifead6702017-04-04 04:46:32 +0300397vl_api_bridge_domain_set_mac_age_t_handler (vl_api_bridge_domain_set_mac_age_t
398 * mp)
399{
400 vlib_main_t *vm = vlib_get_main ();
401 bd_main_t *bdm = &bd_main;
402 vl_api_bridge_domain_set_mac_age_reply_t *rmp;
403 int rv = 0;
404 u32 bd_id = ntohl (mp->bd_id);
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500405 uword *p;
406
407 if (bd_id == 0)
408 {
409 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
410 goto out;
411 }
412
413 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
Eyal Barifead6702017-04-04 04:46:32 +0300414 if (p == 0)
415 {
416 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
417 goto out;
418 }
419 bd_set_mac_age (vm, *p, mp->mac_age);
420out:
421 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_SET_MAC_AGE_REPLY);
422}
423
424static void
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100425vl_api_bridge_domain_add_del_t_handler (vl_api_bridge_domain_add_del_t * mp)
426{
Eyal Barib1352ed2017-04-07 23:14:17 +0300427 l2_bridge_domain_add_del_args_t a = {
428 .is_add = mp->is_add,
429 .flood = mp->flood,
430 .uu_flood = mp->uu_flood,
431 .forward = mp->forward,
432 .learn = mp->learn,
433 .arp_term = mp->arp_term,
434 .mac_age = mp->mac_age,
435 .bd_id = ntohl (mp->bd_id),
Jerome Tollet48304142017-09-05 12:13:22 +0100436 .bd_tag = mp->bd_tag
Eyal Barib1352ed2017-04-07 23:14:17 +0300437 };
438
439 int rv = bd_add_del (&a);
440
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100441 vl_api_bridge_domain_add_del_reply_t *rmp;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100442 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_ADD_DEL_REPLY);
443}
444
445static void
Ole Troan01384fe2017-05-12 11:55:35 +0200446send_bridge_domain_details (l2input_main_t * l2im,
Florin Coras6c4dae22018-01-09 06:39:23 -0800447 vl_api_registration_t * reg,
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100448 l2_bridge_domain_t * bd_config,
449 u32 n_sw_ifs, u32 context)
450{
451 vl_api_bridge_domain_details_t *mp;
Ole Troan01384fe2017-05-12 11:55:35 +0200452 l2_flood_member_t *m;
453 vl_api_bridge_domain_sw_if_t *sw_ifs;
454 l2_input_config_t *input_cfg;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100455
Ole Troan01384fe2017-05-12 11:55:35 +0200456 mp = vl_msg_api_alloc (sizeof (*mp) +
457 (n_sw_ifs * sizeof (vl_api_bridge_domain_sw_if_t)));
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100458 memset (mp, 0, sizeof (*mp));
459 mp->_vl_msg_id = ntohs (VL_API_BRIDGE_DOMAIN_DETAILS);
460 mp->bd_id = ntohl (bd_config->bd_id);
461 mp->flood = bd_feature_flood (bd_config);
462 mp->uu_flood = bd_feature_uu_flood (bd_config);
463 mp->forward = bd_feature_forward (bd_config);
464 mp->learn = bd_feature_learn (bd_config);
465 mp->arp_term = bd_feature_arp_term (bd_config);
466 mp->bvi_sw_if_index = ntohl (bd_config->bvi_sw_if_index);
467 mp->mac_age = bd_config->mac_age;
Jerome Tollet48304142017-09-05 12:13:22 +0100468 if (bd_config->bd_tag)
469 {
470 strncpy ((char *) mp->bd_tag, (char *) bd_config->bd_tag,
471 ARRAY_LEN (mp->bd_tag) - 1);
472 mp->bd_tag[ARRAY_LEN (mp->bd_tag) - 1] = 0;
473 }
474
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100475 mp->context = context;
476
Ole Troan01384fe2017-05-12 11:55:35 +0200477 sw_ifs = (vl_api_bridge_domain_sw_if_t *) mp->sw_if_details;
478 vec_foreach (m, bd_config->members)
479 {
480 sw_ifs->sw_if_index = ntohl (m->sw_if_index);
481 input_cfg = vec_elt_at_index (l2im->configs, m->sw_if_index);
482 sw_ifs->shg = input_cfg->shg;
483 sw_ifs++;
484 mp->n_sw_ifs++;
485 }
486 mp->n_sw_ifs = htonl (mp->n_sw_ifs);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100487
Florin Coras6c4dae22018-01-09 06:39:23 -0800488 vl_api_send_msg (reg, (u8 *) mp);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100489}
490
491static void
492vl_api_bridge_domain_dump_t_handler (vl_api_bridge_domain_dump_t * mp)
493{
494 bd_main_t *bdm = &bd_main;
495 l2input_main_t *l2im = &l2input_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800496 vl_api_registration_t *reg;
497 u32 bd_id, bd_index, end;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100498
Florin Coras6c4dae22018-01-09 06:39:23 -0800499 reg = vl_api_client_index_to_registration (mp->client_index);
500 if (!reg)
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100501 return;
502
Florin Coras6c4dae22018-01-09 06:39:23 -0800503 bd_id = ntohl (mp->bd_id);
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500504 if (bd_id == 0)
505 return;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100506
Eyal Bari259fca72017-05-14 10:38:39 +0300507 if (bd_id == ~0)
508 bd_index = 0, end = vec_len (l2im->bd_configs);
509 else
510 {
511 bd_index = bd_find_index (bdm, bd_id);
512 if (bd_index == ~0)
513 return;
514
515 end = bd_index + 1;
516 }
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500517
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100518 for (; bd_index < end; bd_index++)
519 {
Eyal Bari259fca72017-05-14 10:38:39 +0300520 l2_bridge_domain_t *bd_config =
521 l2input_bd_config_from_index (l2im, bd_index);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100522 /* skip dummy bd_id 0 */
523 if (bd_config && (bd_config->bd_id > 0))
Florin Coras6c4dae22018-01-09 06:39:23 -0800524 send_bridge_domain_details (l2im, reg, bd_config,
Ole Troan01384fe2017-05-12 11:55:35 +0200525 vec_len (bd_config->members),
526 mp->context);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100527 }
528}
529
530static void
531vl_api_bridge_flags_t_handler (vl_api_bridge_flags_t * mp)
532{
533 vlib_main_t *vm = vlib_get_main ();
534 bd_main_t *bdm = &bd_main;
535 vl_api_bridge_flags_reply_t *rmp;
536 int rv = 0;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100537
Eyal Bari259fca72017-05-14 10:38:39 +0300538 u32 flags = ntohl (mp->feature_bitmap);
539 u32 bd_id = ntohl (mp->bd_id);
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500540 if (bd_id == 0)
541 {
542 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
543 goto out;
544 }
545
Eyal Bari259fca72017-05-14 10:38:39 +0300546 u32 bd_index = bd_find_index (bdm, bd_id);
547 if (bd_index == ~0)
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100548 {
549 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
550 goto out;
551 }
552
John Lo8d00fff2017-08-03 00:35:36 -0400553 u32 bitmap = bd_set_flags (vm, bd_index, flags, mp->is_set);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100554
555out:
556 /* *INDENT-OFF* */
557 REPLY_MACRO2(VL_API_BRIDGE_FLAGS_REPLY,
558 ({
John Lo8d00fff2017-08-03 00:35:36 -0400559 rmp->resulting_feature_bitmap = ntohl(bitmap);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100560 }));
561 /* *INDENT-ON* */
562}
Matus Fabianf468e232016-12-02 06:00:53 -0800563
Pavel Kotucekadec5872017-01-25 08:50:53 +0100564static void
565 vl_api_l2_interface_vlan_tag_rewrite_t_handler
566 (vl_api_l2_interface_vlan_tag_rewrite_t * mp)
567{
568 int rv = 0;
569 vl_api_l2_interface_vlan_tag_rewrite_reply_t *rmp;
570 vnet_main_t *vnm = vnet_get_main ();
571 vlib_main_t *vm = vlib_get_main ();
572 u32 vtr_op;
573
574 VALIDATE_SW_IF_INDEX (mp);
575
576 vtr_op = ntohl (mp->vtr_op);
577
578 /* The L2 code is unsuspicious */
579 switch (vtr_op)
580 {
581 case L2_VTR_DISABLED:
582 case L2_VTR_PUSH_1:
583 case L2_VTR_PUSH_2:
584 case L2_VTR_POP_1:
585 case L2_VTR_POP_2:
586 case L2_VTR_TRANSLATE_1_1:
587 case L2_VTR_TRANSLATE_1_2:
588 case L2_VTR_TRANSLATE_2_1:
589 case L2_VTR_TRANSLATE_2_2:
590 break;
591
592 default:
593 rv = VNET_API_ERROR_INVALID_VALUE;
594 goto bad_sw_if_index;
595 }
596
597 rv = l2vtr_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
598 ntohl (mp->push_dot1q), ntohl (mp->tag1),
599 ntohl (mp->tag2));
600
601 BAD_SW_IF_INDEX_LABEL;
602
603 REPLY_MACRO (VL_API_L2_INTERFACE_VLAN_TAG_REWRITE_REPLY);
604}
605
606static void
607 vl_api_l2_interface_pbb_tag_rewrite_t_handler
608 (vl_api_l2_interface_pbb_tag_rewrite_t * mp)
609{
610 vl_api_l2_interface_pbb_tag_rewrite_reply_t *rmp;
611 vnet_main_t *vnm = vnet_get_main ();
612 vlib_main_t *vm = vlib_get_main ();
613 u32 vtr_op;
614 int rv = 0;
615
616 VALIDATE_SW_IF_INDEX (mp);
617
618 vtr_op = ntohl (mp->vtr_op);
619
620 switch (vtr_op)
621 {
622 case L2_VTR_DISABLED:
623 case L2_VTR_PUSH_2:
624 case L2_VTR_POP_2:
625 case L2_VTR_TRANSLATE_2_1:
626 break;
627
628 default:
629 rv = VNET_API_ERROR_INVALID_VALUE;
630 goto bad_sw_if_index;
631 }
632
633 rv = l2pbb_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
634 mp->b_dmac, mp->b_smac, ntohs (mp->b_vlanid),
635 ntohl (mp->i_sid), ntohs (mp->outer_tag));
636
637 BAD_SW_IF_INDEX_LABEL;
638
639 REPLY_MACRO (VL_API_L2_INTERFACE_PBB_TAG_REWRITE_REPLY);
640}
641
Neale Rannsb8d44812017-11-10 06:53:54 -0800642static void
643 vl_api_sw_interface_set_l2_xconnect_t_handler
644 (vl_api_sw_interface_set_l2_xconnect_t * mp)
645{
646 vl_api_sw_interface_set_l2_xconnect_reply_t *rmp;
647 int rv = 0;
648 u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
649 u32 tx_sw_if_index = ntohl (mp->tx_sw_if_index);
650 vlib_main_t *vm = vlib_get_main ();
651 vnet_main_t *vnm = vnet_get_main ();
652
653 VALIDATE_RX_SW_IF_INDEX (mp);
654
655 if (mp->enable)
656 {
657 VALIDATE_TX_SW_IF_INDEX (mp);
658 rv = set_int_l2_mode (vm, vnm, MODE_L2_XC,
659 rx_sw_if_index, 0, 0, 0, tx_sw_if_index);
660 }
661 else
662 {
663 rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0, 0, 0, 0);
664 }
665
666 BAD_RX_SW_IF_INDEX_LABEL;
667 BAD_TX_SW_IF_INDEX_LABEL;
668
669 REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_XCONNECT_REPLY);
670}
671
672static void
673 vl_api_sw_interface_set_l2_bridge_t_handler
674 (vl_api_sw_interface_set_l2_bridge_t * mp)
675{
676 bd_main_t *bdm = &bd_main;
677 vl_api_sw_interface_set_l2_bridge_reply_t *rmp;
678 int rv = 0;
679 vlib_main_t *vm = vlib_get_main ();
680 vnet_main_t *vnm = vnet_get_main ();
681
682 VALIDATE_RX_SW_IF_INDEX (mp);
683 u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
684
685
686 if (mp->enable)
687 {
688 VALIDATE_BD_ID (mp);
689 u32 bd_id = ntohl (mp->bd_id);
690 u32 bd_index = bd_find_or_add_bd_index (bdm, bd_id);
691 u32 bvi = mp->bvi;
692 u8 shg = mp->shg;
693 rv = set_int_l2_mode (vm, vnm, MODE_L2_BRIDGE,
694 rx_sw_if_index, bd_index, bvi, shg, 0);
695 }
696 else
697 {
698 rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0, 0, 0, 0);
699 }
700
701 BAD_RX_SW_IF_INDEX_LABEL;
702 BAD_BD_ID_LABEL;
703
704 REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_BRIDGE_REPLY);
705}
706
707static void
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200708send_bd_ip_mac_entry (vpe_api_main_t * am,
709 vl_api_registration_t * reg,
710 u32 bd_id, u8 is_ipv6,
711 u8 * ip_address, u8 * mac_address, u32 context)
712{
713 vl_api_bd_ip_mac_details_t *mp;
714
715 mp = vl_msg_api_alloc (sizeof (*mp));
716 memset (mp, 0, sizeof (*mp));
717 mp->_vl_msg_id = ntohs (VL_API_BD_IP_MAC_DETAILS);
718
719 mp->bd_id = ntohl (bd_id);
720
721 clib_memcpy (mp->mac_address, mac_address, 6);
722 mp->is_ipv6 = is_ipv6;
723 clib_memcpy (mp->ip_address, ip_address, (is_ipv6) ? 16 : 4);
724 mp->context = context;
725
726 vl_api_send_msg (reg, (u8 *) mp);
727}
728
729static void
730vl_api_bd_ip_mac_dump_t_handler (vl_api_bd_ip_mac_dump_t * mp)
731{
732 vpe_api_main_t *am = &vpe_api_main;
733 bd_main_t *bdm = &bd_main;
734 l2_bridge_domain_t *bd_config;
735 u32 bd_id = ntohl (mp->bd_id);
Mohsin Kazmib24dfec2018-08-23 12:24:19 +0200736 u32 bd_index, start, end;
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200737 vl_api_registration_t *reg;
738 uword *p;
739
740 reg = vl_api_client_index_to_registration (mp->client_index);
741 if (!reg)
742 return;
743
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200744 /* see bd_id: ~0 means "any" */
745 if (bd_id == ~0)
Mohsin Kazmib24dfec2018-08-23 12:24:19 +0200746 {
747 start = 1;
748 end = vec_len (l2input_main.bd_configs);
749 }
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200750 else
751 {
752 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
753 if (p == 0)
754 return;
755
756 bd_index = p[0];
757 vec_validate (l2input_main.bd_configs, bd_index);
758 start = bd_index;
759 end = start + 1;
760 }
761
762 for (bd_index = start; bd_index < end; bd_index++)
763 {
764 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
765 if (bd_is_valid (bd_config))
766 {
767 ip4_address_t ip4_addr;
768 ip6_address_t *ip6_addr;
769 u64 mac_addr;
770 bd_id = bd_config->bd_id;
771
772 /* *INDENT-OFF* */
773 hash_foreach (ip4_addr.as_u32, mac_addr, bd_config->mac_by_ip4,
774 ({
775 send_bd_ip_mac_entry (am, reg, bd_id, 0, (u8 *) &(ip4_addr.as_u8), (u8 *) &mac_addr, mp->context);
776 }));
777
778 hash_foreach_mem (ip6_addr, mac_addr, bd_config->mac_by_ip6,
779 ({
780 send_bd_ip_mac_entry (am, reg, bd_id, 1, (u8 *) &(ip6_addr->as_u8), (u8 *) &mac_addr, mp->context);
781 }));
782 /* *INDENT-ON* */
783 }
784 }
785}
786
787static void
Neale Rannsb8d44812017-11-10 06:53:54 -0800788vl_api_bd_ip_mac_add_del_t_handler (vl_api_bd_ip_mac_add_del_t * mp)
789{
790 bd_main_t *bdm = &bd_main;
791 vl_api_bd_ip_mac_add_del_reply_t *rmp;
792 int rv = 0;
793 u32 bd_id = ntohl (mp->bd_id);
794 u32 bd_index;
795 uword *p;
796
797 if (bd_id == 0)
798 {
799 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
800 goto out;
801 }
802
803 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
804 if (p == 0)
805 {
806 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
807 goto out;
808 }
809
810 bd_index = p[0];
811 if (bd_add_del_ip_mac (bd_index, mp->ip_address,
812 mp->mac_address, mp->is_ipv6, mp->is_add))
813 rv = VNET_API_ERROR_UNSPECIFIED;
814
815out:
816 REPLY_MACRO (VL_API_BD_IP_MAC_ADD_DEL_REPLY);
817}
818
819extern void l2_efp_filter_configure (vnet_main_t * vnet_main,
Neale Ranns6b9b41c2018-07-23 05:47:09 -0400820 u32 sw_if_index, u8 enable);
Neale Rannsb8d44812017-11-10 06:53:54 -0800821
822static void
823vl_api_l2_interface_efp_filter_t_handler (vl_api_l2_interface_efp_filter_t *
824 mp)
825{
826 int rv;
827 vl_api_l2_interface_efp_filter_reply_t *rmp;
828 vnet_main_t *vnm = vnet_get_main ();
829
Neale Ranns6b9b41c2018-07-23 05:47:09 -0400830 VALIDATE_SW_IF_INDEX (mp);
831
Neale Rannsb8d44812017-11-10 06:53:54 -0800832 // enable/disable the feature
Neale Ranns6b9b41c2018-07-23 05:47:09 -0400833 l2_efp_filter_configure (vnm, ntohl (mp->sw_if_index), mp->enable_disable);
Neale Rannsb8d44812017-11-10 06:53:54 -0800834 rv = vnm->api_errno;
835
Neale Ranns6b9b41c2018-07-23 05:47:09 -0400836 BAD_SW_IF_INDEX_LABEL;
Neale Rannsb8d44812017-11-10 06:53:54 -0800837 REPLY_MACRO (VL_API_L2_INTERFACE_EFP_FILTER_REPLY);
838}
839
840static void
841vl_api_l2_patch_add_del_t_handler (vl_api_l2_patch_add_del_t * mp)
842{
843 extern int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
844 int is_add);
845 vl_api_l2_patch_add_del_reply_t *rmp;
846 int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
847 int is_add);
848 int rv = 0;
849
850 VALIDATE_RX_SW_IF_INDEX (mp);
851 VALIDATE_TX_SW_IF_INDEX (mp);
852
853 rv = vnet_l2_patch_add_del (ntohl (mp->rx_sw_if_index),
854 ntohl (mp->tx_sw_if_index),
855 (int) (mp->is_add != 0));
856
857 BAD_RX_SW_IF_INDEX_LABEL;
858 BAD_TX_SW_IF_INDEX_LABEL;
859
860 REPLY_MACRO (VL_API_L2_PATCH_ADD_DEL_REPLY);
861}
862
863static void
864vl_api_sw_interface_set_vpath_t_handler (vl_api_sw_interface_set_vpath_t * mp)
865{
866 vl_api_sw_interface_set_vpath_reply_t *rmp;
867 int rv = 0;
868 u32 sw_if_index = ntohl (mp->sw_if_index);
869
870 VALIDATE_SW_IF_INDEX (mp);
871
872 l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_VPATH, mp->enable);
873 vnet_feature_enable_disable ("ip4-unicast", "vpath-input-ip4",
874 sw_if_index, mp->enable, 0, 0);
875 vnet_feature_enable_disable ("ip4-multicast", "vpath-input-ip4",
876 sw_if_index, mp->enable, 0, 0);
877 vnet_feature_enable_disable ("ip6-unicast", "vpath-input-ip6",
878 sw_if_index, mp->enable, 0, 0);
879 vnet_feature_enable_disable ("ip6-multicast", "vpath-input-ip6",
880 sw_if_index, mp->enable, 0, 0);
881
882 BAD_SW_IF_INDEX_LABEL;
883
884 REPLY_MACRO (VL_API_SW_INTERFACE_SET_VPATH_REPLY);
885}
886
Matus Fabianf468e232016-12-02 06:00:53 -0800887/*
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100888 * l2_api_hookup
Matus Fabianf468e232016-12-02 06:00:53 -0800889 * Add vpe's API message handlers to the table.
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700890 * vlib has already mapped shared memory and
Matus Fabianf468e232016-12-02 06:00:53 -0800891 * added the client registration handlers.
892 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
893 */
894#define vl_msg_name_crc_list
895#include <vnet/vnet_all_api_h.h>
896#undef vl_msg_name_crc_list
897
898static void
899setup_message_id_table (api_main_t * am)
900{
901#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
902 foreach_vl_msg_name_crc_l2;
903#undef _
904}
905
906static clib_error_t *
907l2_api_hookup (vlib_main_t * vm)
908{
909 api_main_t *am = &api_main;
910
911#define _(N,n) \
912 vl_msg_api_set_handlers(VL_API_##N, #n, \
913 vl_api_##n##_t_handler, \
914 vl_noop_handler, \
915 vl_api_##n##_t_endian, \
916 vl_api_##n##_t_print, \
917 sizeof(vl_api_##n##_t), 1);
918 foreach_vpe_api_msg;
919#undef _
920
921 /*
922 * Set up the (msg_name, crc, message-id) table
923 */
924 setup_message_id_table (am);
925
926 return 0;
927}
928
929VLIB_API_INIT_FUNCTION (l2_api_hookup);
930
931/*
932 * fd.io coding-style-patch-verification: ON
933 *
934 * Local Variables:
935 * eval: (c-set-style "gnu")
936 * End:
937 */