blob: 39e52b1ecef61b19b7fa658824d250e56e008aa7 [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);
145 mp->static_mac = l2fe_res->fields.static_mac;
146 mp->filter_mac = l2fe_res->fields.filter;
147 mp->bvi_mac = l2fe_res->fields.bvi;
148 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 {
Eyal Bari31a71ab2017-06-25 14:42:33 +0300218 u32 sw_if_index = ntohl (mp->sw_if_index);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100219 VALIDATE_SW_IF_INDEX (mp);
220 if (vec_len (l2im->configs) <= sw_if_index)
221 {
222 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
223 goto bad_sw_if_index;
224 }
225 else
226 {
227 l2_input_config_t *config;
228 config = vec_elt_at_index (l2im->configs, sw_if_index);
229 if (config->bridge == 0)
230 {
231 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
232 goto bad_sw_if_index;
233 }
234 }
John Lo8d00fff2017-08-03 00:35:36 -0400235 u8 static_mac = mp->static_mac ? 1 : 0;
236 u8 bvi_mac = mp->bvi_mac ? 1 : 0;
Eyal Bari31a71ab2017-06-25 14:42:33 +0300237 l2fib_add_fwd_entry (mac, bd_index, sw_if_index, static_mac,
238 bvi_mac);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100239 }
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100240 }
241 else
242 {
John Lo7dbd7262018-05-31 10:25:18 -0400243 u32 sw_if_index = ntohl (mp->sw_if_index);
244 if (l2fib_del_entry (mac, bd_index, sw_if_index))
245 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100246 }
247
248 BAD_SW_IF_INDEX_LABEL;
249
250 REPLY_MACRO (VL_API_L2FIB_ADD_DEL_REPLY);
251}
252
253static void
John Lo8d00fff2017-08-03 00:35:36 -0400254vl_api_want_l2_macs_events_t_handler (vl_api_want_l2_macs_events_t * mp)
255{
256 int rv = 0;
257 vl_api_want_l2_macs_events_reply_t *rmp;
258 l2learn_main_t *lm = &l2learn_main;
259 l2fib_main_t *fm = &l2fib_main;
260 u32 pid = ntohl (mp->pid);
261 u32 learn_limit = ntohl (mp->learn_limit);
262
263 if (mp->enable_disable)
264 {
265 if (lm->client_pid == 0)
266 {
267 lm->client_pid = pid;
268 lm->client_index = mp->client_index;
269
270 if (mp->max_macs_in_event)
271 fm->max_macs_in_event = mp->max_macs_in_event * 10;
272 else
273 fm->max_macs_in_event = L2FIB_EVENT_MAX_MACS_DEFAULT;
274
275 if (mp->scan_delay)
276 fm->event_scan_delay = (f64) (mp->scan_delay) * 10e-3;
277 else
278 fm->event_scan_delay = L2FIB_EVENT_SCAN_DELAY_DEFAULT;
279
280 /* change learn limit and flush all learned MACs */
281 if (learn_limit && (learn_limit < L2LEARN_DEFAULT_LIMIT))
282 lm->global_learn_limit = learn_limit;
283 else
284 lm->global_learn_limit = L2FIB_EVENT_LEARN_LIMIT_DEFAULT;
285
286 l2fib_flush_all_mac (vlib_get_main ());
287 }
288 else if (lm->client_pid != pid)
289 {
290 rv = VNET_API_ERROR_L2_MACS_EVENT_CLINET_PRESENT;
291 goto exit;
292 }
293 }
294 else if (lm->client_pid)
295 {
296 lm->client_pid = 0;
297 lm->client_index = 0;
John Lodbfa5742017-09-02 08:27:36 -0400298 if (learn_limit && (learn_limit < L2LEARN_DEFAULT_LIMIT))
John Loe531f4c2017-08-22 09:16:50 -0400299 lm->global_learn_limit = learn_limit;
300 else
301 lm->global_learn_limit = L2LEARN_DEFAULT_LIMIT;
John Lo8d00fff2017-08-03 00:35:36 -0400302 }
303
304exit:
305 REPLY_MACRO (VL_API_WANT_L2_MACS_EVENTS_REPLY);
306}
307
308static void
Eyal Barif24991c2017-04-05 05:33:21 +0300309vl_api_l2fib_flush_int_t_handler (vl_api_l2fib_flush_int_t * mp)
310{
311 int rv = 0;
312 vlib_main_t *vm = vlib_get_main ();
313 vl_api_l2fib_flush_int_reply_t *rmp;
314
315 VALIDATE_SW_IF_INDEX (mp);
316
317 u32 sw_if_index = ntohl (mp->sw_if_index);
318 l2fib_flush_int_mac (vm, sw_if_index);
319
320 BAD_SW_IF_INDEX_LABEL;
321 REPLY_MACRO (VL_API_L2FIB_FLUSH_INT_REPLY);
322}
323
324static void
Eyal Bari7537e712017-04-27 14:07:55 +0300325vl_api_l2fib_flush_all_t_handler (vl_api_l2fib_flush_all_t * mp)
326{
327 int rv = 0;
328 vl_api_l2fib_flush_all_reply_t *rmp;
329
330 l2fib_flush_all_mac (vlib_get_main ());
331 REPLY_MACRO (VL_API_L2FIB_FLUSH_ALL_REPLY);
332}
333
334static void
Eyal Barif24991c2017-04-05 05:33:21 +0300335vl_api_l2fib_flush_bd_t_handler (vl_api_l2fib_flush_bd_t * mp)
336{
337 int rv = 0;
338 vlib_main_t *vm = vlib_get_main ();
339 bd_main_t *bdm = &bd_main;
340 vl_api_l2fib_flush_bd_reply_t *rmp;
341
342 u32 bd_id = ntohl (mp->bd_id);
343 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
344 if (p == 0)
345 {
346 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
347 goto out;
348 }
349 l2fib_flush_bd_mac (vm, *p);
350out:
351 REPLY_MACRO (VL_API_L2FIB_FLUSH_BD_REPLY);
352}
353
354static void
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100355vl_api_l2_flags_t_handler (vl_api_l2_flags_t * mp)
356{
357 vl_api_l2_flags_reply_t *rmp;
358 int rv = 0;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100359 u32 rbm = 0;
360
361 VALIDATE_SW_IF_INDEX (mp);
362
Eyal Barifead6702017-04-04 04:46:32 +0300363 u32 sw_if_index = ntohl (mp->sw_if_index);
John Lo8d00fff2017-08-03 00:35:36 -0400364 u32 flags = ntohl (mp->feature_bitmap);
365 u32 bitmap = 0;
366
367 if (flags & L2_LEARN)
368 bitmap |= L2INPUT_FEAT_LEARN;
369
370 if (flags & L2_FWD)
371 bitmap |= L2INPUT_FEAT_FWD;
372
373 if (flags & L2_FLOOD)
374 bitmap |= L2INPUT_FEAT_FLOOD;
375
376 if (flags & L2_UU_FLOOD)
377 bitmap |= L2INPUT_FEAT_UU_FLOOD;
378
379 if (flags & L2_ARP_TERM)
380 bitmap |= L2INPUT_FEAT_ARP_TERM;
381
382 rbm = l2input_intf_bitmap_enable (sw_if_index, bitmap, mp->is_set);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100383
384 BAD_SW_IF_INDEX_LABEL;
385
386 /* *INDENT-OFF* */
387 REPLY_MACRO2(VL_API_L2_FLAGS_REPLY,
388 ({
389 rmp->resulting_feature_bitmap = ntohl(rbm);
390 }));
391 /* *INDENT-ON* */
392}
393
394static void
Eyal Barifead6702017-04-04 04:46:32 +0300395vl_api_bridge_domain_set_mac_age_t_handler (vl_api_bridge_domain_set_mac_age_t
396 * mp)
397{
398 vlib_main_t *vm = vlib_get_main ();
399 bd_main_t *bdm = &bd_main;
400 vl_api_bridge_domain_set_mac_age_reply_t *rmp;
401 int rv = 0;
402 u32 bd_id = ntohl (mp->bd_id);
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500403 uword *p;
404
405 if (bd_id == 0)
406 {
407 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
408 goto out;
409 }
410
411 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
Eyal Barifead6702017-04-04 04:46:32 +0300412 if (p == 0)
413 {
414 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
415 goto out;
416 }
417 bd_set_mac_age (vm, *p, mp->mac_age);
418out:
419 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_SET_MAC_AGE_REPLY);
420}
421
422static void
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100423vl_api_bridge_domain_add_del_t_handler (vl_api_bridge_domain_add_del_t * mp)
424{
Eyal Barib1352ed2017-04-07 23:14:17 +0300425 l2_bridge_domain_add_del_args_t a = {
426 .is_add = mp->is_add,
427 .flood = mp->flood,
428 .uu_flood = mp->uu_flood,
429 .forward = mp->forward,
430 .learn = mp->learn,
431 .arp_term = mp->arp_term,
432 .mac_age = mp->mac_age,
433 .bd_id = ntohl (mp->bd_id),
Jerome Tollet48304142017-09-05 12:13:22 +0100434 .bd_tag = mp->bd_tag
Eyal Barib1352ed2017-04-07 23:14:17 +0300435 };
436
437 int rv = bd_add_del (&a);
438
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100439 vl_api_bridge_domain_add_del_reply_t *rmp;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100440 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_ADD_DEL_REPLY);
441}
442
443static void
Ole Troan01384fe2017-05-12 11:55:35 +0200444send_bridge_domain_details (l2input_main_t * l2im,
Florin Coras6c4dae22018-01-09 06:39:23 -0800445 vl_api_registration_t * reg,
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100446 l2_bridge_domain_t * bd_config,
447 u32 n_sw_ifs, u32 context)
448{
449 vl_api_bridge_domain_details_t *mp;
Ole Troan01384fe2017-05-12 11:55:35 +0200450 l2_flood_member_t *m;
451 vl_api_bridge_domain_sw_if_t *sw_ifs;
452 l2_input_config_t *input_cfg;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100453
Ole Troan01384fe2017-05-12 11:55:35 +0200454 mp = vl_msg_api_alloc (sizeof (*mp) +
455 (n_sw_ifs * sizeof (vl_api_bridge_domain_sw_if_t)));
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100456 memset (mp, 0, sizeof (*mp));
457 mp->_vl_msg_id = ntohs (VL_API_BRIDGE_DOMAIN_DETAILS);
458 mp->bd_id = ntohl (bd_config->bd_id);
459 mp->flood = bd_feature_flood (bd_config);
460 mp->uu_flood = bd_feature_uu_flood (bd_config);
461 mp->forward = bd_feature_forward (bd_config);
462 mp->learn = bd_feature_learn (bd_config);
463 mp->arp_term = bd_feature_arp_term (bd_config);
464 mp->bvi_sw_if_index = ntohl (bd_config->bvi_sw_if_index);
465 mp->mac_age = bd_config->mac_age;
Jerome Tollet48304142017-09-05 12:13:22 +0100466 if (bd_config->bd_tag)
467 {
468 strncpy ((char *) mp->bd_tag, (char *) bd_config->bd_tag,
469 ARRAY_LEN (mp->bd_tag) - 1);
470 mp->bd_tag[ARRAY_LEN (mp->bd_tag) - 1] = 0;
471 }
472
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100473 mp->context = context;
474
Ole Troan01384fe2017-05-12 11:55:35 +0200475 sw_ifs = (vl_api_bridge_domain_sw_if_t *) mp->sw_if_details;
476 vec_foreach (m, bd_config->members)
477 {
478 sw_ifs->sw_if_index = ntohl (m->sw_if_index);
479 input_cfg = vec_elt_at_index (l2im->configs, m->sw_if_index);
480 sw_ifs->shg = input_cfg->shg;
481 sw_ifs++;
482 mp->n_sw_ifs++;
483 }
484 mp->n_sw_ifs = htonl (mp->n_sw_ifs);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100485
Florin Coras6c4dae22018-01-09 06:39:23 -0800486 vl_api_send_msg (reg, (u8 *) mp);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100487}
488
489static void
490vl_api_bridge_domain_dump_t_handler (vl_api_bridge_domain_dump_t * mp)
491{
492 bd_main_t *bdm = &bd_main;
493 l2input_main_t *l2im = &l2input_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800494 vl_api_registration_t *reg;
495 u32 bd_id, bd_index, end;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100496
Florin Coras6c4dae22018-01-09 06:39:23 -0800497 reg = vl_api_client_index_to_registration (mp->client_index);
498 if (!reg)
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100499 return;
500
Florin Coras6c4dae22018-01-09 06:39:23 -0800501 bd_id = ntohl (mp->bd_id);
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500502 if (bd_id == 0)
503 return;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100504
Eyal Bari259fca72017-05-14 10:38:39 +0300505 if (bd_id == ~0)
506 bd_index = 0, end = vec_len (l2im->bd_configs);
507 else
508 {
509 bd_index = bd_find_index (bdm, bd_id);
510 if (bd_index == ~0)
511 return;
512
513 end = bd_index + 1;
514 }
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500515
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100516 for (; bd_index < end; bd_index++)
517 {
Eyal Bari259fca72017-05-14 10:38:39 +0300518 l2_bridge_domain_t *bd_config =
519 l2input_bd_config_from_index (l2im, bd_index);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100520 /* skip dummy bd_id 0 */
521 if (bd_config && (bd_config->bd_id > 0))
Florin Coras6c4dae22018-01-09 06:39:23 -0800522 send_bridge_domain_details (l2im, reg, bd_config,
Ole Troan01384fe2017-05-12 11:55:35 +0200523 vec_len (bd_config->members),
524 mp->context);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100525 }
526}
527
528static void
529vl_api_bridge_flags_t_handler (vl_api_bridge_flags_t * mp)
530{
531 vlib_main_t *vm = vlib_get_main ();
532 bd_main_t *bdm = &bd_main;
533 vl_api_bridge_flags_reply_t *rmp;
534 int rv = 0;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100535
Eyal Bari259fca72017-05-14 10:38:39 +0300536 u32 flags = ntohl (mp->feature_bitmap);
537 u32 bd_id = ntohl (mp->bd_id);
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500538 if (bd_id == 0)
539 {
540 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
541 goto out;
542 }
543
Eyal Bari259fca72017-05-14 10:38:39 +0300544 u32 bd_index = bd_find_index (bdm, bd_id);
545 if (bd_index == ~0)
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100546 {
547 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
548 goto out;
549 }
550
John Lo8d00fff2017-08-03 00:35:36 -0400551 u32 bitmap = bd_set_flags (vm, bd_index, flags, mp->is_set);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100552
553out:
554 /* *INDENT-OFF* */
555 REPLY_MACRO2(VL_API_BRIDGE_FLAGS_REPLY,
556 ({
John Lo8d00fff2017-08-03 00:35:36 -0400557 rmp->resulting_feature_bitmap = ntohl(bitmap);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100558 }));
559 /* *INDENT-ON* */
560}
Matus Fabianf468e232016-12-02 06:00:53 -0800561
Pavel Kotucekadec5872017-01-25 08:50:53 +0100562static void
563 vl_api_l2_interface_vlan_tag_rewrite_t_handler
564 (vl_api_l2_interface_vlan_tag_rewrite_t * mp)
565{
566 int rv = 0;
567 vl_api_l2_interface_vlan_tag_rewrite_reply_t *rmp;
568 vnet_main_t *vnm = vnet_get_main ();
569 vlib_main_t *vm = vlib_get_main ();
570 u32 vtr_op;
571
572 VALIDATE_SW_IF_INDEX (mp);
573
574 vtr_op = ntohl (mp->vtr_op);
575
576 /* The L2 code is unsuspicious */
577 switch (vtr_op)
578 {
579 case L2_VTR_DISABLED:
580 case L2_VTR_PUSH_1:
581 case L2_VTR_PUSH_2:
582 case L2_VTR_POP_1:
583 case L2_VTR_POP_2:
584 case L2_VTR_TRANSLATE_1_1:
585 case L2_VTR_TRANSLATE_1_2:
586 case L2_VTR_TRANSLATE_2_1:
587 case L2_VTR_TRANSLATE_2_2:
588 break;
589
590 default:
591 rv = VNET_API_ERROR_INVALID_VALUE;
592 goto bad_sw_if_index;
593 }
594
595 rv = l2vtr_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
596 ntohl (mp->push_dot1q), ntohl (mp->tag1),
597 ntohl (mp->tag2));
598
599 BAD_SW_IF_INDEX_LABEL;
600
601 REPLY_MACRO (VL_API_L2_INTERFACE_VLAN_TAG_REWRITE_REPLY);
602}
603
604static void
605 vl_api_l2_interface_pbb_tag_rewrite_t_handler
606 (vl_api_l2_interface_pbb_tag_rewrite_t * mp)
607{
608 vl_api_l2_interface_pbb_tag_rewrite_reply_t *rmp;
609 vnet_main_t *vnm = vnet_get_main ();
610 vlib_main_t *vm = vlib_get_main ();
611 u32 vtr_op;
612 int rv = 0;
613
614 VALIDATE_SW_IF_INDEX (mp);
615
616 vtr_op = ntohl (mp->vtr_op);
617
618 switch (vtr_op)
619 {
620 case L2_VTR_DISABLED:
621 case L2_VTR_PUSH_2:
622 case L2_VTR_POP_2:
623 case L2_VTR_TRANSLATE_2_1:
624 break;
625
626 default:
627 rv = VNET_API_ERROR_INVALID_VALUE;
628 goto bad_sw_if_index;
629 }
630
631 rv = l2pbb_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
632 mp->b_dmac, mp->b_smac, ntohs (mp->b_vlanid),
633 ntohl (mp->i_sid), ntohs (mp->outer_tag));
634
635 BAD_SW_IF_INDEX_LABEL;
636
637 REPLY_MACRO (VL_API_L2_INTERFACE_PBB_TAG_REWRITE_REPLY);
638}
639
Neale Rannsb8d44812017-11-10 06:53:54 -0800640static void
641 vl_api_sw_interface_set_l2_xconnect_t_handler
642 (vl_api_sw_interface_set_l2_xconnect_t * mp)
643{
644 vl_api_sw_interface_set_l2_xconnect_reply_t *rmp;
645 int rv = 0;
646 u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
647 u32 tx_sw_if_index = ntohl (mp->tx_sw_if_index);
648 vlib_main_t *vm = vlib_get_main ();
649 vnet_main_t *vnm = vnet_get_main ();
650
651 VALIDATE_RX_SW_IF_INDEX (mp);
652
653 if (mp->enable)
654 {
655 VALIDATE_TX_SW_IF_INDEX (mp);
656 rv = set_int_l2_mode (vm, vnm, MODE_L2_XC,
657 rx_sw_if_index, 0, 0, 0, tx_sw_if_index);
658 }
659 else
660 {
661 rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0, 0, 0, 0);
662 }
663
664 BAD_RX_SW_IF_INDEX_LABEL;
665 BAD_TX_SW_IF_INDEX_LABEL;
666
667 REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_XCONNECT_REPLY);
668}
669
670static void
671 vl_api_sw_interface_set_l2_bridge_t_handler
672 (vl_api_sw_interface_set_l2_bridge_t * mp)
673{
674 bd_main_t *bdm = &bd_main;
675 vl_api_sw_interface_set_l2_bridge_reply_t *rmp;
676 int rv = 0;
677 vlib_main_t *vm = vlib_get_main ();
678 vnet_main_t *vnm = vnet_get_main ();
679
680 VALIDATE_RX_SW_IF_INDEX (mp);
681 u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
682
683
684 if (mp->enable)
685 {
686 VALIDATE_BD_ID (mp);
687 u32 bd_id = ntohl (mp->bd_id);
688 u32 bd_index = bd_find_or_add_bd_index (bdm, bd_id);
689 u32 bvi = mp->bvi;
690 u8 shg = mp->shg;
691 rv = set_int_l2_mode (vm, vnm, MODE_L2_BRIDGE,
692 rx_sw_if_index, bd_index, bvi, shg, 0);
693 }
694 else
695 {
696 rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0, 0, 0, 0);
697 }
698
699 BAD_RX_SW_IF_INDEX_LABEL;
700 BAD_BD_ID_LABEL;
701
702 REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_BRIDGE_REPLY);
703}
704
705static void
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200706send_bd_ip_mac_entry (vpe_api_main_t * am,
707 vl_api_registration_t * reg,
708 u32 bd_id, u8 is_ipv6,
709 u8 * ip_address, u8 * mac_address, u32 context)
710{
711 vl_api_bd_ip_mac_details_t *mp;
712
713 mp = vl_msg_api_alloc (sizeof (*mp));
714 memset (mp, 0, sizeof (*mp));
715 mp->_vl_msg_id = ntohs (VL_API_BD_IP_MAC_DETAILS);
716
717 mp->bd_id = ntohl (bd_id);
718
719 clib_memcpy (mp->mac_address, mac_address, 6);
720 mp->is_ipv6 = is_ipv6;
721 clib_memcpy (mp->ip_address, ip_address, (is_ipv6) ? 16 : 4);
722 mp->context = context;
723
724 vl_api_send_msg (reg, (u8 *) mp);
725}
726
727static void
728vl_api_bd_ip_mac_dump_t_handler (vl_api_bd_ip_mac_dump_t * mp)
729{
730 vpe_api_main_t *am = &vpe_api_main;
731 bd_main_t *bdm = &bd_main;
732 l2_bridge_domain_t *bd_config;
733 u32 bd_id = ntohl (mp->bd_id);
Mohsin Kazmib24dfec2018-08-23 12:24:19 +0200734 u32 bd_index, start, end;
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200735 vl_api_registration_t *reg;
736 uword *p;
737
738 reg = vl_api_client_index_to_registration (mp->client_index);
739 if (!reg)
740 return;
741
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200742 /* see bd_id: ~0 means "any" */
743 if (bd_id == ~0)
Mohsin Kazmib24dfec2018-08-23 12:24:19 +0200744 {
745 start = 1;
746 end = vec_len (l2input_main.bd_configs);
747 }
Mohsin Kazmi5d82d2f2018-08-13 19:17:54 +0200748 else
749 {
750 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
751 if (p == 0)
752 return;
753
754 bd_index = p[0];
755 vec_validate (l2input_main.bd_configs, bd_index);
756 start = bd_index;
757 end = start + 1;
758 }
759
760 for (bd_index = start; bd_index < end; bd_index++)
761 {
762 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
763 if (bd_is_valid (bd_config))
764 {
765 ip4_address_t ip4_addr;
766 ip6_address_t *ip6_addr;
767 u64 mac_addr;
768 bd_id = bd_config->bd_id;
769
770 /* *INDENT-OFF* */
771 hash_foreach (ip4_addr.as_u32, mac_addr, bd_config->mac_by_ip4,
772 ({
773 send_bd_ip_mac_entry (am, reg, bd_id, 0, (u8 *) &(ip4_addr.as_u8), (u8 *) &mac_addr, mp->context);
774 }));
775
776 hash_foreach_mem (ip6_addr, mac_addr, bd_config->mac_by_ip6,
777 ({
778 send_bd_ip_mac_entry (am, reg, bd_id, 1, (u8 *) &(ip6_addr->as_u8), (u8 *) &mac_addr, mp->context);
779 }));
780 /* *INDENT-ON* */
781 }
782 }
783}
784
785static void
Neale Rannsb8d44812017-11-10 06:53:54 -0800786vl_api_bd_ip_mac_add_del_t_handler (vl_api_bd_ip_mac_add_del_t * mp)
787{
788 bd_main_t *bdm = &bd_main;
789 vl_api_bd_ip_mac_add_del_reply_t *rmp;
790 int rv = 0;
791 u32 bd_id = ntohl (mp->bd_id);
792 u32 bd_index;
793 uword *p;
794
795 if (bd_id == 0)
796 {
797 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
798 goto out;
799 }
800
801 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
802 if (p == 0)
803 {
804 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
805 goto out;
806 }
807
808 bd_index = p[0];
809 if (bd_add_del_ip_mac (bd_index, mp->ip_address,
810 mp->mac_address, mp->is_ipv6, mp->is_add))
811 rv = VNET_API_ERROR_UNSPECIFIED;
812
813out:
814 REPLY_MACRO (VL_API_BD_IP_MAC_ADD_DEL_REPLY);
815}
816
817extern void l2_efp_filter_configure (vnet_main_t * vnet_main,
Neale Ranns6b9b41c2018-07-23 05:47:09 -0400818 u32 sw_if_index, u8 enable);
Neale Rannsb8d44812017-11-10 06:53:54 -0800819
820static void
821vl_api_l2_interface_efp_filter_t_handler (vl_api_l2_interface_efp_filter_t *
822 mp)
823{
824 int rv;
825 vl_api_l2_interface_efp_filter_reply_t *rmp;
826 vnet_main_t *vnm = vnet_get_main ();
827
Neale Ranns6b9b41c2018-07-23 05:47:09 -0400828 VALIDATE_SW_IF_INDEX (mp);
829
Neale Rannsb8d44812017-11-10 06:53:54 -0800830 // enable/disable the feature
Neale Ranns6b9b41c2018-07-23 05:47:09 -0400831 l2_efp_filter_configure (vnm, ntohl (mp->sw_if_index), mp->enable_disable);
Neale Rannsb8d44812017-11-10 06:53:54 -0800832 rv = vnm->api_errno;
833
Neale Ranns6b9b41c2018-07-23 05:47:09 -0400834 BAD_SW_IF_INDEX_LABEL;
Neale Rannsb8d44812017-11-10 06:53:54 -0800835 REPLY_MACRO (VL_API_L2_INTERFACE_EFP_FILTER_REPLY);
836}
837
838static void
839vl_api_l2_patch_add_del_t_handler (vl_api_l2_patch_add_del_t * mp)
840{
841 extern int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
842 int is_add);
843 vl_api_l2_patch_add_del_reply_t *rmp;
844 int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
845 int is_add);
846 int rv = 0;
847
848 VALIDATE_RX_SW_IF_INDEX (mp);
849 VALIDATE_TX_SW_IF_INDEX (mp);
850
851 rv = vnet_l2_patch_add_del (ntohl (mp->rx_sw_if_index),
852 ntohl (mp->tx_sw_if_index),
853 (int) (mp->is_add != 0));
854
855 BAD_RX_SW_IF_INDEX_LABEL;
856 BAD_TX_SW_IF_INDEX_LABEL;
857
858 REPLY_MACRO (VL_API_L2_PATCH_ADD_DEL_REPLY);
859}
860
861static void
862vl_api_sw_interface_set_vpath_t_handler (vl_api_sw_interface_set_vpath_t * mp)
863{
864 vl_api_sw_interface_set_vpath_reply_t *rmp;
865 int rv = 0;
866 u32 sw_if_index = ntohl (mp->sw_if_index);
867
868 VALIDATE_SW_IF_INDEX (mp);
869
870 l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_VPATH, mp->enable);
871 vnet_feature_enable_disable ("ip4-unicast", "vpath-input-ip4",
872 sw_if_index, mp->enable, 0, 0);
873 vnet_feature_enable_disable ("ip4-multicast", "vpath-input-ip4",
874 sw_if_index, mp->enable, 0, 0);
875 vnet_feature_enable_disable ("ip6-unicast", "vpath-input-ip6",
876 sw_if_index, mp->enable, 0, 0);
877 vnet_feature_enable_disable ("ip6-multicast", "vpath-input-ip6",
878 sw_if_index, mp->enable, 0, 0);
879
880 BAD_SW_IF_INDEX_LABEL;
881
882 REPLY_MACRO (VL_API_SW_INTERFACE_SET_VPATH_REPLY);
883}
884
Matus Fabianf468e232016-12-02 06:00:53 -0800885/*
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100886 * l2_api_hookup
Matus Fabianf468e232016-12-02 06:00:53 -0800887 * Add vpe's API message handlers to the table.
888 * vlib has alread mapped shared memory and
889 * added the client registration handlers.
890 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
891 */
892#define vl_msg_name_crc_list
893#include <vnet/vnet_all_api_h.h>
894#undef vl_msg_name_crc_list
895
896static void
897setup_message_id_table (api_main_t * am)
898{
899#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
900 foreach_vl_msg_name_crc_l2;
901#undef _
902}
903
904static clib_error_t *
905l2_api_hookup (vlib_main_t * vm)
906{
907 api_main_t *am = &api_main;
908
909#define _(N,n) \
910 vl_msg_api_set_handlers(VL_API_##N, #n, \
911 vl_api_##n##_t_handler, \
912 vl_noop_handler, \
913 vl_api_##n##_t_endian, \
914 vl_api_##n##_t_print, \
915 sizeof(vl_api_##n##_t), 1);
916 foreach_vpe_api_msg;
917#undef _
918
919 /*
920 * Set up the (msg_name, crc, message-id) table
921 */
922 setup_message_id_table (am);
923
924 return 0;
925}
926
927VLIB_API_INIT_FUNCTION (l2_api_hookup);
928
929/*
930 * fd.io coding-style-patch-verification: ON
931 *
932 * Local Variables:
933 * eval: (c-set-style "gnu")
934 * End:
935 */