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