blob: a3cc49bfb3217a90d9b0d839d680f12f95b1659c [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>
Matus Fabianf468e232016-12-02 06:00:53 -080028
29#include <vnet/vnet_msg_enum.h>
30
31#define vl_typedefs /* define message structures */
32#include <vnet/vnet_all_api_h.h>
33#undef vl_typedefs
34
35#define vl_endianfun /* define message structures */
36#include <vnet/vnet_all_api_h.h>
37#undef vl_endianfun
38
39/* instantiate all the print functions we know about */
40#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
41#define vl_printfun
42#include <vnet/vnet_all_api_h.h>
43#undef vl_printfun
44
45#include <vlibapi/api_helper_macros.h>
46
Pavel Kotucek0f971d82017-01-03 10:48:54 +010047#define foreach_vpe_api_msg \
48_(L2_XCONNECT_DUMP, l2_xconnect_dump) \
49_(L2_FIB_CLEAR_TABLE, l2_fib_clear_table) \
50_(L2_FIB_TABLE_DUMP, l2_fib_table_dump) \
51_(L2_FIB_TABLE_ENTRY, l2_fib_table_entry) \
52_(L2FIB_ADD_DEL, l2fib_add_del) \
53_(L2_FLAGS, l2_flags) \
54_(BRIDGE_DOMAIN_ADD_DEL, bridge_domain_add_del) \
55_(BRIDGE_DOMAIN_DUMP, bridge_domain_dump) \
56_(BRIDGE_DOMAIN_DETAILS, bridge_domain_details) \
57_(BRIDGE_DOMAIN_SW_IF_DETAILS, bridge_domain_sw_if_details) \
Pavel Kotucekadec5872017-01-25 08:50:53 +010058_(BRIDGE_FLAGS, bridge_flags) \
59_(L2_INTERFACE_VLAN_TAG_REWRITE, l2_interface_vlan_tag_rewrite) \
60_(L2_INTERFACE_PBB_TAG_REWRITE, l2_interface_pbb_tag_rewrite)
Matus Fabianf468e232016-12-02 06:00:53 -080061
62static void
63send_l2_xconnect_details (unix_shared_memory_queue_t * q, u32 context,
64 u32 rx_sw_if_index, u32 tx_sw_if_index)
65{
66 vl_api_l2_xconnect_details_t *mp;
67
68 mp = vl_msg_api_alloc (sizeof (*mp));
69 memset (mp, 0, sizeof (*mp));
70 mp->_vl_msg_id = ntohs (VL_API_L2_XCONNECT_DETAILS);
71 mp->context = context;
72 mp->rx_sw_if_index = htonl (rx_sw_if_index);
73 mp->tx_sw_if_index = htonl (tx_sw_if_index);
74
75 vl_msg_api_send_shmem (q, (u8 *) & mp);
76}
77
78static void
79vl_api_l2_xconnect_dump_t_handler (vl_api_l2_xconnect_dump_t * mp)
80{
81 unix_shared_memory_queue_t *q;
82 vnet_main_t *vnm = vnet_get_main ();
83 vnet_interface_main_t *im = &vnm->interface_main;
84 l2input_main_t *l2im = &l2input_main;
85 vnet_sw_interface_t *swif;
86 l2_input_config_t *config;
87
88 q = vl_api_client_index_to_input_queue (mp->client_index);
89 if (q == 0)
90 return;
91
92 /* *INDENT-OFF* */
93 pool_foreach (swif, im->sw_interfaces,
94 ({
95 config = vec_elt_at_index (l2im->configs, swif->sw_if_index);
96 if (config->xconnect)
97 send_l2_xconnect_details (q, mp->context, swif->sw_if_index,
98 config->output_sw_if_index);
99 }));
100 /* *INDENT-ON* */
101}
102
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100103static void
104vl_api_l2_fib_clear_table_t_handler (vl_api_l2_fib_clear_table_t * mp)
105{
106 int rv = 0;
107 vl_api_l2_fib_clear_table_reply_t *rmp;
108
109 /* DAW-FIXME: This API should only clear non-static l2fib entries, but
110 * that is not currently implemented. When that TODO is fixed
111 * this call should be changed to pass 1 instead of 0.
112 */
113 l2fib_clear_table (0);
114
115 REPLY_MACRO (VL_API_L2_FIB_CLEAR_TABLE_REPLY);
116}
117
118static void
119send_l2fib_table_entry (vpe_api_main_t * am,
120 unix_shared_memory_queue_t * q,
121 l2fib_entry_key_t * l2fe_key,
122 l2fib_entry_result_t * l2fe_res, u32 context)
123{
124 vl_api_l2_fib_table_entry_t *mp;
125
126 mp = vl_msg_api_alloc (sizeof (*mp));
127 memset (mp, 0, sizeof (*mp));
128 mp->_vl_msg_id = ntohs (VL_API_L2_FIB_TABLE_ENTRY);
129
130 mp->bd_id =
131 ntohl (l2input_main.bd_configs[l2fe_key->fields.bd_index].bd_id);
132
133 mp->mac = l2fib_make_key (l2fe_key->fields.mac, 0);
134 mp->sw_if_index = ntohl (l2fe_res->fields.sw_if_index);
135 mp->static_mac = l2fe_res->fields.static_mac;
136 mp->filter_mac = l2fe_res->fields.filter;
137 mp->bvi_mac = l2fe_res->fields.bvi;
138 mp->context = context;
139
140 vl_msg_api_send_shmem (q, (u8 *) & mp);
141}
142
143static void
144vl_api_l2_fib_table_entry_t_handler (vl_api_l2_fib_table_entry_t * mp)
145{
146 clib_warning ("BUG");
147}
148
149static void
150vl_api_l2_fib_table_dump_t_handler (vl_api_l2_fib_table_dump_t * mp)
151{
152 vpe_api_main_t *am = &vpe_api_main;
153 bd_main_t *bdm = &bd_main;
154 l2fib_entry_key_t *l2fe_key = NULL;
155 l2fib_entry_result_t *l2fe_res = NULL;
156 u32 ni, bd_id = ntohl (mp->bd_id);
157 u32 bd_index;
158 unix_shared_memory_queue_t *q;
159 uword *p;
160
161 q = vl_api_client_index_to_input_queue (mp->client_index);
162 if (q == 0)
163 return;
164
165 /* see l2fib_table_dump: ~0 means "any" */
166 if (bd_id == ~0)
167 bd_index = ~0;
168 else
169 {
170 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
171 if (p == 0)
172 return;
173
174 bd_index = p[0];
175 }
176
177 l2fib_table_dump (bd_index, &l2fe_key, &l2fe_res);
178
179 vec_foreach_index (ni, l2fe_key)
180 {
181 send_l2fib_table_entry (am, q, vec_elt_at_index (l2fe_key, ni),
182 vec_elt_at_index (l2fe_res, ni), mp->context);
183 }
184 vec_free (l2fe_key);
185 vec_free (l2fe_res);
186}
187
188static void
189vl_api_l2fib_add_del_t_handler (vl_api_l2fib_add_del_t * mp)
190{
191 bd_main_t *bdm = &bd_main;
192 l2input_main_t *l2im = &l2input_main;
193 vl_api_l2fib_add_del_reply_t *rmp;
194 int rv = 0;
195 u64 mac = 0;
196 u32 sw_if_index = ntohl (mp->sw_if_index);
197 u32 bd_id = ntohl (mp->bd_id);
198 u32 bd_index;
199 u32 static_mac;
200 u32 filter_mac;
201 u32 bvi_mac;
202 uword *p;
203
204 mac = mp->mac;
205
206 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
207 if (!p)
208 {
209 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
210 goto bad_sw_if_index;
211 }
212 bd_index = p[0];
213
214 if (mp->is_add)
215 {
216 filter_mac = mp->filter_mac ? 1 : 0;
217 if (filter_mac == 0)
218 {
219 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 }
235 }
236 static_mac = mp->static_mac ? 1 : 0;
237 bvi_mac = mp->bvi_mac ? 1 : 0;
238 l2fib_add_entry (mac, bd_index, sw_if_index, static_mac, filter_mac,
239 bvi_mac);
240 }
241 else
242 {
243 l2fib_del_entry (mac, bd_index);
244 }
245
246 BAD_SW_IF_INDEX_LABEL;
247
248 REPLY_MACRO (VL_API_L2FIB_ADD_DEL_REPLY);
249}
250
251static void
252vl_api_l2_flags_t_handler (vl_api_l2_flags_t * mp)
253{
254 vl_api_l2_flags_reply_t *rmp;
255 int rv = 0;
256 u32 sw_if_index = ntohl (mp->sw_if_index);
257 u32 flags = ntohl (mp->feature_bitmap);
258 u32 rbm = 0;
259
260 VALIDATE_SW_IF_INDEX (mp);
261
262#define _(a,b) \
263 if (flags & L2INPUT_FEAT_ ## a) \
264 rbm = l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_ ## a, mp->is_set);
265 foreach_l2input_feat;
266#undef _
267
268 BAD_SW_IF_INDEX_LABEL;
269
270 /* *INDENT-OFF* */
271 REPLY_MACRO2(VL_API_L2_FLAGS_REPLY,
272 ({
273 rmp->resulting_feature_bitmap = ntohl(rbm);
274 }));
275 /* *INDENT-ON* */
276}
277
278static void
279vl_api_bridge_domain_add_del_t_handler (vl_api_bridge_domain_add_del_t * mp)
280{
281 vlib_main_t *vm = vlib_get_main ();
282 bd_main_t *bdm = &bd_main;
283 vl_api_bridge_domain_add_del_reply_t *rmp;
284 int rv = 0;
285 u32 enable_flags = 0, disable_flags = 0;
286 u32 bd_id = ntohl (mp->bd_id);
287 u32 bd_index;
288
289 if (mp->is_add)
290 {
291 bd_index = bd_find_or_add_bd_index (bdm, bd_id);
292
293 if (mp->flood)
294 enable_flags |= L2_FLOOD;
295 else
296 disable_flags |= L2_FLOOD;
297
298 if (mp->uu_flood)
299 enable_flags |= L2_UU_FLOOD;
300 else
301 disable_flags |= L2_UU_FLOOD;
302
303 if (mp->forward)
304 enable_flags |= L2_FWD;
305 else
306 disable_flags |= L2_FWD;
307
308 if (mp->arp_term)
309 enable_flags |= L2_ARP_TERM;
310 else
311 disable_flags |= L2_ARP_TERM;
312
313 if (mp->learn)
314 enable_flags |= L2_LEARN;
315 else
316 disable_flags |= L2_LEARN;
317
318 if (enable_flags)
319 bd_set_flags (vm, bd_index, enable_flags, 1 /* enable */ );
320
321 if (disable_flags)
322 bd_set_flags (vm, bd_index, disable_flags, 0 /* disable */ );
323
324 bd_set_mac_age (vm, bd_index, mp->mac_age);
325 }
326 else
327 rv = bd_delete_bd_index (bdm, bd_id);
328
329 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_ADD_DEL_REPLY);
330}
331
332static void
333vl_api_bridge_domain_details_t_handler (vl_api_bridge_domain_details_t * mp)
334{
335 clib_warning ("BUG");
336}
337
338static void
339 vl_api_bridge_domain_sw_if_details_t_handler
340 (vl_api_bridge_domain_sw_if_details_t * mp)
341{
342 clib_warning ("BUG");
343}
344
345static void
346send_bridge_domain_details (unix_shared_memory_queue_t * q,
347 l2_bridge_domain_t * bd_config,
348 u32 n_sw_ifs, u32 context)
349{
350 vl_api_bridge_domain_details_t *mp;
351
352 mp = vl_msg_api_alloc (sizeof (*mp));
353 memset (mp, 0, sizeof (*mp));
354 mp->_vl_msg_id = ntohs (VL_API_BRIDGE_DOMAIN_DETAILS);
355 mp->bd_id = ntohl (bd_config->bd_id);
356 mp->flood = bd_feature_flood (bd_config);
357 mp->uu_flood = bd_feature_uu_flood (bd_config);
358 mp->forward = bd_feature_forward (bd_config);
359 mp->learn = bd_feature_learn (bd_config);
360 mp->arp_term = bd_feature_arp_term (bd_config);
361 mp->bvi_sw_if_index = ntohl (bd_config->bvi_sw_if_index);
362 mp->mac_age = bd_config->mac_age;
363 mp->n_sw_ifs = ntohl (n_sw_ifs);
364 mp->context = context;
365
366 vl_msg_api_send_shmem (q, (u8 *) & mp);
367}
368
369static void
370send_bd_sw_if_details (l2input_main_t * l2im,
371 unix_shared_memory_queue_t * q,
372 l2_flood_member_t * member, u32 bd_id, u32 context)
373{
374 vl_api_bridge_domain_sw_if_details_t *mp;
375 l2_input_config_t *input_cfg;
376
377 mp = vl_msg_api_alloc (sizeof (*mp));
378 memset (mp, 0, sizeof (*mp));
379 mp->_vl_msg_id = ntohs (VL_API_BRIDGE_DOMAIN_SW_IF_DETAILS);
380 mp->bd_id = ntohl (bd_id);
381 mp->sw_if_index = ntohl (member->sw_if_index);
382 input_cfg = vec_elt_at_index (l2im->configs, member->sw_if_index);
383 mp->shg = input_cfg->shg;
384 mp->context = context;
385
386 vl_msg_api_send_shmem (q, (u8 *) & mp);
387}
388
389static void
390vl_api_bridge_domain_dump_t_handler (vl_api_bridge_domain_dump_t * mp)
391{
392 bd_main_t *bdm = &bd_main;
393 l2input_main_t *l2im = &l2input_main;
394 unix_shared_memory_queue_t *q;
395 l2_bridge_domain_t *bd_config;
396 u32 bd_id, bd_index;
397 u32 end;
398
399 q = vl_api_client_index_to_input_queue (mp->client_index);
400
401 if (q == 0)
402 return;
403
404 bd_id = ntohl (mp->bd_id);
405
406 bd_index = (bd_id == ~0) ? 0 : bd_find_or_add_bd_index (bdm, bd_id);
407 end = (bd_id == ~0) ? vec_len (l2im->bd_configs) : bd_index + 1;
408 for (; bd_index < end; bd_index++)
409 {
410 bd_config = l2input_bd_config_from_index (l2im, bd_index);
411 /* skip dummy bd_id 0 */
412 if (bd_config && (bd_config->bd_id > 0))
413 {
414 u32 n_sw_ifs;
415 l2_flood_member_t *m;
416
417 n_sw_ifs = vec_len (bd_config->members);
418 send_bridge_domain_details (q, bd_config, n_sw_ifs, mp->context);
419
420 vec_foreach (m, bd_config->members)
421 {
422 send_bd_sw_if_details (l2im, q, m, bd_config->bd_id, mp->context);
423 }
424 }
425 }
426}
427
428static void
429vl_api_bridge_flags_t_handler (vl_api_bridge_flags_t * mp)
430{
431 vlib_main_t *vm = vlib_get_main ();
432 bd_main_t *bdm = &bd_main;
433 vl_api_bridge_flags_reply_t *rmp;
434 int rv = 0;
435 u32 bd_id = ntohl (mp->bd_id);
436 u32 bd_index;
437 u32 flags = ntohl (mp->feature_bitmap);
438 uword *p;
439
440 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
441 if (p == 0)
442 {
443 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
444 goto out;
445 }
446
447 bd_index = p[0];
448
449 bd_set_flags (vm, bd_index, flags, mp->is_set);
450
451out:
452 /* *INDENT-OFF* */
453 REPLY_MACRO2(VL_API_BRIDGE_FLAGS_REPLY,
454 ({
455 rmp->resulting_feature_bitmap = ntohl(flags);
456 }));
457 /* *INDENT-ON* */
458}
Matus Fabianf468e232016-12-02 06:00:53 -0800459
Pavel Kotucekadec5872017-01-25 08:50:53 +0100460static void
461 vl_api_l2_interface_vlan_tag_rewrite_t_handler
462 (vl_api_l2_interface_vlan_tag_rewrite_t * mp)
463{
464 int rv = 0;
465 vl_api_l2_interface_vlan_tag_rewrite_reply_t *rmp;
466 vnet_main_t *vnm = vnet_get_main ();
467 vlib_main_t *vm = vlib_get_main ();
468 u32 vtr_op;
469
470 VALIDATE_SW_IF_INDEX (mp);
471
472 vtr_op = ntohl (mp->vtr_op);
473
474 /* The L2 code is unsuspicious */
475 switch (vtr_op)
476 {
477 case L2_VTR_DISABLED:
478 case L2_VTR_PUSH_1:
479 case L2_VTR_PUSH_2:
480 case L2_VTR_POP_1:
481 case L2_VTR_POP_2:
482 case L2_VTR_TRANSLATE_1_1:
483 case L2_VTR_TRANSLATE_1_2:
484 case L2_VTR_TRANSLATE_2_1:
485 case L2_VTR_TRANSLATE_2_2:
486 break;
487
488 default:
489 rv = VNET_API_ERROR_INVALID_VALUE;
490 goto bad_sw_if_index;
491 }
492
493 rv = l2vtr_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
494 ntohl (mp->push_dot1q), ntohl (mp->tag1),
495 ntohl (mp->tag2));
496
497 BAD_SW_IF_INDEX_LABEL;
498
499 REPLY_MACRO (VL_API_L2_INTERFACE_VLAN_TAG_REWRITE_REPLY);
500}
501
502static void
503 vl_api_l2_interface_pbb_tag_rewrite_t_handler
504 (vl_api_l2_interface_pbb_tag_rewrite_t * mp)
505{
506 vl_api_l2_interface_pbb_tag_rewrite_reply_t *rmp;
507 vnet_main_t *vnm = vnet_get_main ();
508 vlib_main_t *vm = vlib_get_main ();
509 u32 vtr_op;
510 int rv = 0;
511
512 VALIDATE_SW_IF_INDEX (mp);
513
514 vtr_op = ntohl (mp->vtr_op);
515
516 switch (vtr_op)
517 {
518 case L2_VTR_DISABLED:
519 case L2_VTR_PUSH_2:
520 case L2_VTR_POP_2:
521 case L2_VTR_TRANSLATE_2_1:
522 break;
523
524 default:
525 rv = VNET_API_ERROR_INVALID_VALUE;
526 goto bad_sw_if_index;
527 }
528
529 rv = l2pbb_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
530 mp->b_dmac, mp->b_smac, ntohs (mp->b_vlanid),
531 ntohl (mp->i_sid), ntohs (mp->outer_tag));
532
533 BAD_SW_IF_INDEX_LABEL;
534
535 REPLY_MACRO (VL_API_L2_INTERFACE_PBB_TAG_REWRITE_REPLY);
536}
537
Matus Fabianf468e232016-12-02 06:00:53 -0800538/*
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100539 * l2_api_hookup
Matus Fabianf468e232016-12-02 06:00:53 -0800540 * Add vpe's API message handlers to the table.
541 * vlib has alread mapped shared memory and
542 * added the client registration handlers.
543 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
544 */
545#define vl_msg_name_crc_list
546#include <vnet/vnet_all_api_h.h>
547#undef vl_msg_name_crc_list
548
549static void
550setup_message_id_table (api_main_t * am)
551{
552#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
553 foreach_vl_msg_name_crc_l2;
554#undef _
555}
556
557static clib_error_t *
558l2_api_hookup (vlib_main_t * vm)
559{
560 api_main_t *am = &api_main;
561
562#define _(N,n) \
563 vl_msg_api_set_handlers(VL_API_##N, #n, \
564 vl_api_##n##_t_handler, \
565 vl_noop_handler, \
566 vl_api_##n##_t_endian, \
567 vl_api_##n##_t_print, \
568 sizeof(vl_api_##n##_t), 1);
569 foreach_vpe_api_msg;
570#undef _
571
572 /*
573 * Set up the (msg_name, crc, message-id) table
574 */
575 setup_message_id_table (am);
576
577 return 0;
578}
579
580VLIB_API_INIT_FUNCTION (l2_api_hookup);
581
582/*
583 * fd.io coding-style-patch-verification: ON
584 *
585 * Local Variables:
586 * eval: (c-set-style "gnu")
587 * End:
588 */