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