L2FIB:add l2fib_flush_bd l2fib_flush_int apis
[vpp.git] / src / vnet / l2 / l2_api.c
1 /*
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>
26 #include <vnet/l2/l2_fib.h>
27 #include <vnet/l2/l2_vtr.h>
28
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
47 #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 _(L2FIB_FLUSH_INT, l2fib_flush_int)                         \
52 _(L2FIB_FLUSH_BD, l2fib_flush_bd)                           \
53 _(L2FIB_ADD_DEL, l2fib_add_del)                             \
54 _(L2_FLAGS, l2_flags)                                       \
55 _(BRIDGE_DOMAIN_ADD_DEL, bridge_domain_add_del)             \
56 _(BRIDGE_DOMAIN_DUMP, bridge_domain_dump)                   \
57 _(BRIDGE_FLAGS, bridge_flags)                               \
58 _(L2_INTERFACE_VLAN_TAG_REWRITE, l2_interface_vlan_tag_rewrite) \
59 _(L2_INTERFACE_PBB_TAG_REWRITE, l2_interface_pbb_tag_rewrite) \
60 _(BRIDGE_DOMAIN_SET_MAC_AGE, bridge_domain_set_mac_age)
61
62 static void
63 send_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
78 static void
79 vl_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
103 static void
104 vl_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
118 static void
119 send_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
143 static void
144 vl_api_l2_fib_table_dump_t_handler (vl_api_l2_fib_table_dump_t * mp)
145 {
146   vpe_api_main_t *am = &vpe_api_main;
147   bd_main_t *bdm = &bd_main;
148   l2fib_entry_key_t *l2fe_key = NULL;
149   l2fib_entry_result_t *l2fe_res = NULL;
150   u32 ni, bd_id = ntohl (mp->bd_id);
151   u32 bd_index;
152   unix_shared_memory_queue_t *q;
153   uword *p;
154
155   q = vl_api_client_index_to_input_queue (mp->client_index);
156   if (q == 0)
157     return;
158
159   /* see l2fib_table_dump: ~0 means "any" */
160   if (bd_id == ~0)
161     bd_index = ~0;
162   else
163     {
164       p = hash_get (bdm->bd_index_by_bd_id, bd_id);
165       if (p == 0)
166         return;
167
168       bd_index = p[0];
169     }
170
171   l2fib_table_dump (bd_index, &l2fe_key, &l2fe_res);
172
173   vec_foreach_index (ni, l2fe_key)
174   {
175     send_l2fib_table_entry (am, q, vec_elt_at_index (l2fe_key, ni),
176                             vec_elt_at_index (l2fe_res, ni), mp->context);
177   }
178   vec_free (l2fe_key);
179   vec_free (l2fe_res);
180 }
181
182 static void
183 vl_api_l2fib_add_del_t_handler (vl_api_l2fib_add_del_t * mp)
184 {
185   bd_main_t *bdm = &bd_main;
186   l2input_main_t *l2im = &l2input_main;
187   vl_api_l2fib_add_del_reply_t *rmp;
188   int rv = 0;
189   u64 mac = 0;
190   u32 sw_if_index = ntohl (mp->sw_if_index);
191   u32 bd_id = ntohl (mp->bd_id);
192   u32 bd_index;
193   u32 static_mac;
194   u32 filter_mac;
195   u32 bvi_mac;
196   uword *p;
197
198   mac = mp->mac;
199
200   p = hash_get (bdm->bd_index_by_bd_id, bd_id);
201   if (!p)
202     {
203       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
204       goto bad_sw_if_index;
205     }
206   bd_index = p[0];
207
208   if (mp->is_add)
209     {
210       filter_mac = mp->filter_mac ? 1 : 0;
211       if (filter_mac == 0)
212         {
213           VALIDATE_SW_IF_INDEX (mp);
214           if (vec_len (l2im->configs) <= sw_if_index)
215             {
216               rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
217               goto bad_sw_if_index;
218             }
219           else
220             {
221               l2_input_config_t *config;
222               config = vec_elt_at_index (l2im->configs, sw_if_index);
223               if (config->bridge == 0)
224                 {
225                   rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
226                   goto bad_sw_if_index;
227                 }
228             }
229         }
230       static_mac = mp->static_mac ? 1 : 0;
231       bvi_mac = mp->bvi_mac ? 1 : 0;
232       l2fib_add_entry (mac, bd_index, sw_if_index, static_mac, filter_mac,
233                        bvi_mac);
234     }
235   else
236     {
237       l2fib_del_entry (mac, bd_index);
238     }
239
240   BAD_SW_IF_INDEX_LABEL;
241
242   REPLY_MACRO (VL_API_L2FIB_ADD_DEL_REPLY);
243 }
244
245 static void
246 vl_api_l2fib_flush_int_t_handler (vl_api_l2fib_flush_int_t * mp)
247 {
248   int rv = 0;
249   vlib_main_t *vm = vlib_get_main ();
250   vl_api_l2fib_flush_int_reply_t *rmp;
251
252   VALIDATE_SW_IF_INDEX (mp);
253
254   u32 sw_if_index = ntohl (mp->sw_if_index);
255   l2fib_flush_int_mac (vm, sw_if_index);
256
257   BAD_SW_IF_INDEX_LABEL;
258   REPLY_MACRO (VL_API_L2FIB_FLUSH_INT_REPLY);
259 }
260
261 static void
262 vl_api_l2fib_flush_bd_t_handler (vl_api_l2fib_flush_bd_t * mp)
263 {
264   int rv = 0;
265   vlib_main_t *vm = vlib_get_main ();
266   bd_main_t *bdm = &bd_main;
267   vl_api_l2fib_flush_bd_reply_t *rmp;
268
269   u32 bd_id = ntohl (mp->bd_id);
270   uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
271   if (p == 0)
272     {
273       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
274       goto out;
275     }
276   l2fib_flush_bd_mac (vm, *p);
277 out:
278   REPLY_MACRO (VL_API_L2FIB_FLUSH_BD_REPLY);
279 }
280
281 static void
282 vl_api_l2_flags_t_handler (vl_api_l2_flags_t * mp)
283 {
284   vl_api_l2_flags_reply_t *rmp;
285   int rv = 0;
286   u32 rbm = 0;
287
288   VALIDATE_SW_IF_INDEX (mp);
289
290   u32 sw_if_index = ntohl (mp->sw_if_index);
291   u32 flags = ntohl (mp->feature_bitmap) & L2INPUT_VALID_MASK;
292   rbm = l2input_intf_bitmap_enable (sw_if_index, flags, mp->is_set);
293
294   BAD_SW_IF_INDEX_LABEL;
295
296   /* *INDENT-OFF* */
297   REPLY_MACRO2(VL_API_L2_FLAGS_REPLY,
298   ({
299     rmp->resulting_feature_bitmap = ntohl(rbm);
300   }));
301   /* *INDENT-ON* */
302 }
303
304 static void
305 vl_api_bridge_domain_set_mac_age_t_handler (vl_api_bridge_domain_set_mac_age_t
306                                             * mp)
307 {
308   vlib_main_t *vm = vlib_get_main ();
309   bd_main_t *bdm = &bd_main;
310   vl_api_bridge_domain_set_mac_age_reply_t *rmp;
311   int rv = 0;
312   u32 bd_id = ntohl (mp->bd_id);
313   uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
314   if (p == 0)
315     {
316       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
317       goto out;
318     }
319   bd_set_mac_age (vm, *p, mp->mac_age);
320 out:
321   REPLY_MACRO (VL_API_BRIDGE_DOMAIN_SET_MAC_AGE_REPLY);
322 }
323
324 static void
325 vl_api_bridge_domain_add_del_t_handler (vl_api_bridge_domain_add_del_t * mp)
326 {
327   vlib_main_t *vm = vlib_get_main ();
328   bd_main_t *bdm = &bd_main;
329   vl_api_bridge_domain_add_del_reply_t *rmp;
330   int rv = 0;
331   u32 enable_flags = 0, disable_flags = 0;
332   u32 bd_id = ntohl (mp->bd_id);
333   u32 bd_index;
334
335   if (mp->is_add)
336     {
337       bd_index = bd_find_or_add_bd_index (bdm, bd_id);
338
339       if (mp->flood)
340         enable_flags |= L2_FLOOD;
341       else
342         disable_flags |= L2_FLOOD;
343
344       if (mp->uu_flood)
345         enable_flags |= L2_UU_FLOOD;
346       else
347         disable_flags |= L2_UU_FLOOD;
348
349       if (mp->forward)
350         enable_flags |= L2_FWD;
351       else
352         disable_flags |= L2_FWD;
353
354       if (mp->arp_term)
355         enable_flags |= L2_ARP_TERM;
356       else
357         disable_flags |= L2_ARP_TERM;
358
359       if (mp->learn)
360         enable_flags |= L2_LEARN;
361       else
362         disable_flags |= L2_LEARN;
363
364       if (enable_flags)
365         bd_set_flags (vm, bd_index, enable_flags, 1 /* enable */ );
366
367       if (disable_flags)
368         bd_set_flags (vm, bd_index, disable_flags, 0 /* disable */ );
369
370       bd_set_mac_age (vm, bd_index, mp->mac_age);
371     }
372   else
373     rv = bd_delete_bd_index (bdm, bd_id);
374
375   REPLY_MACRO (VL_API_BRIDGE_DOMAIN_ADD_DEL_REPLY);
376 }
377
378 static void
379 send_bridge_domain_details (unix_shared_memory_queue_t * q,
380                             l2_bridge_domain_t * bd_config,
381                             u32 n_sw_ifs, u32 context)
382 {
383   vl_api_bridge_domain_details_t *mp;
384
385   mp = vl_msg_api_alloc (sizeof (*mp));
386   memset (mp, 0, sizeof (*mp));
387   mp->_vl_msg_id = ntohs (VL_API_BRIDGE_DOMAIN_DETAILS);
388   mp->bd_id = ntohl (bd_config->bd_id);
389   mp->flood = bd_feature_flood (bd_config);
390   mp->uu_flood = bd_feature_uu_flood (bd_config);
391   mp->forward = bd_feature_forward (bd_config);
392   mp->learn = bd_feature_learn (bd_config);
393   mp->arp_term = bd_feature_arp_term (bd_config);
394   mp->bvi_sw_if_index = ntohl (bd_config->bvi_sw_if_index);
395   mp->mac_age = bd_config->mac_age;
396   mp->n_sw_ifs = ntohl (n_sw_ifs);
397   mp->context = context;
398
399   vl_msg_api_send_shmem (q, (u8 *) & mp);
400 }
401
402 static void
403 send_bd_sw_if_details (l2input_main_t * l2im,
404                        unix_shared_memory_queue_t * q,
405                        l2_flood_member_t * member, u32 bd_id, u32 context)
406 {
407   vl_api_bridge_domain_sw_if_details_t *mp;
408   l2_input_config_t *input_cfg;
409
410   mp = vl_msg_api_alloc (sizeof (*mp));
411   memset (mp, 0, sizeof (*mp));
412   mp->_vl_msg_id = ntohs (VL_API_BRIDGE_DOMAIN_SW_IF_DETAILS);
413   mp->bd_id = ntohl (bd_id);
414   mp->sw_if_index = ntohl (member->sw_if_index);
415   input_cfg = vec_elt_at_index (l2im->configs, member->sw_if_index);
416   mp->shg = input_cfg->shg;
417   mp->context = context;
418
419   vl_msg_api_send_shmem (q, (u8 *) & mp);
420 }
421
422 static void
423 vl_api_bridge_domain_dump_t_handler (vl_api_bridge_domain_dump_t * mp)
424 {
425   bd_main_t *bdm = &bd_main;
426   l2input_main_t *l2im = &l2input_main;
427   unix_shared_memory_queue_t *q;
428   l2_bridge_domain_t *bd_config;
429   u32 bd_id, bd_index;
430   u32 end;
431
432   q = vl_api_client_index_to_input_queue (mp->client_index);
433
434   if (q == 0)
435     return;
436
437   bd_id = ntohl (mp->bd_id);
438
439   bd_index = (bd_id == ~0) ? 0 : bd_find_or_add_bd_index (bdm, bd_id);
440   end = (bd_id == ~0) ? vec_len (l2im->bd_configs) : bd_index + 1;
441   for (; bd_index < end; bd_index++)
442     {
443       bd_config = l2input_bd_config_from_index (l2im, bd_index);
444       /* skip dummy bd_id 0 */
445       if (bd_config && (bd_config->bd_id > 0))
446         {
447           u32 n_sw_ifs;
448           l2_flood_member_t *m;
449
450           n_sw_ifs = vec_len (bd_config->members);
451           send_bridge_domain_details (q, bd_config, n_sw_ifs, mp->context);
452
453           vec_foreach (m, bd_config->members)
454           {
455             send_bd_sw_if_details (l2im, q, m, bd_config->bd_id, mp->context);
456           }
457         }
458     }
459 }
460
461 static void
462 vl_api_bridge_flags_t_handler (vl_api_bridge_flags_t * mp)
463 {
464   vlib_main_t *vm = vlib_get_main ();
465   bd_main_t *bdm = &bd_main;
466   vl_api_bridge_flags_reply_t *rmp;
467   int rv = 0;
468   u32 bd_id = ntohl (mp->bd_id);
469   u32 bd_index;
470   u32 flags = ntohl (mp->feature_bitmap);
471   uword *p;
472
473   p = hash_get (bdm->bd_index_by_bd_id, bd_id);
474   if (p == 0)
475     {
476       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
477       goto out;
478     }
479
480   bd_index = p[0];
481
482   bd_set_flags (vm, bd_index, flags, mp->is_set);
483
484 out:
485   /* *INDENT-OFF* */
486   REPLY_MACRO2(VL_API_BRIDGE_FLAGS_REPLY,
487   ({
488     rmp->resulting_feature_bitmap = ntohl(flags);
489   }));
490   /* *INDENT-ON* */
491 }
492
493 static void
494   vl_api_l2_interface_vlan_tag_rewrite_t_handler
495   (vl_api_l2_interface_vlan_tag_rewrite_t * mp)
496 {
497   int rv = 0;
498   vl_api_l2_interface_vlan_tag_rewrite_reply_t *rmp;
499   vnet_main_t *vnm = vnet_get_main ();
500   vlib_main_t *vm = vlib_get_main ();
501   u32 vtr_op;
502
503   VALIDATE_SW_IF_INDEX (mp);
504
505   vtr_op = ntohl (mp->vtr_op);
506
507   /* The L2 code is unsuspicious */
508   switch (vtr_op)
509     {
510     case L2_VTR_DISABLED:
511     case L2_VTR_PUSH_1:
512     case L2_VTR_PUSH_2:
513     case L2_VTR_POP_1:
514     case L2_VTR_POP_2:
515     case L2_VTR_TRANSLATE_1_1:
516     case L2_VTR_TRANSLATE_1_2:
517     case L2_VTR_TRANSLATE_2_1:
518     case L2_VTR_TRANSLATE_2_2:
519       break;
520
521     default:
522       rv = VNET_API_ERROR_INVALID_VALUE;
523       goto bad_sw_if_index;
524     }
525
526   rv = l2vtr_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
527                         ntohl (mp->push_dot1q), ntohl (mp->tag1),
528                         ntohl (mp->tag2));
529
530   BAD_SW_IF_INDEX_LABEL;
531
532   REPLY_MACRO (VL_API_L2_INTERFACE_VLAN_TAG_REWRITE_REPLY);
533 }
534
535 static void
536   vl_api_l2_interface_pbb_tag_rewrite_t_handler
537   (vl_api_l2_interface_pbb_tag_rewrite_t * mp)
538 {
539   vl_api_l2_interface_pbb_tag_rewrite_reply_t *rmp;
540   vnet_main_t *vnm = vnet_get_main ();
541   vlib_main_t *vm = vlib_get_main ();
542   u32 vtr_op;
543   int rv = 0;
544
545   VALIDATE_SW_IF_INDEX (mp);
546
547   vtr_op = ntohl (mp->vtr_op);
548
549   switch (vtr_op)
550     {
551     case L2_VTR_DISABLED:
552     case L2_VTR_PUSH_2:
553     case L2_VTR_POP_2:
554     case L2_VTR_TRANSLATE_2_1:
555       break;
556
557     default:
558       rv = VNET_API_ERROR_INVALID_VALUE;
559       goto bad_sw_if_index;
560     }
561
562   rv = l2pbb_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
563                         mp->b_dmac, mp->b_smac, ntohs (mp->b_vlanid),
564                         ntohl (mp->i_sid), ntohs (mp->outer_tag));
565
566   BAD_SW_IF_INDEX_LABEL;
567
568   REPLY_MACRO (VL_API_L2_INTERFACE_PBB_TAG_REWRITE_REPLY);
569 }
570
571 /*
572  * l2_api_hookup
573  * Add vpe's API message handlers to the table.
574  * vlib has alread mapped shared memory and
575  * added the client registration handlers.
576  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
577  */
578 #define vl_msg_name_crc_list
579 #include <vnet/vnet_all_api_h.h>
580 #undef vl_msg_name_crc_list
581
582 static void
583 setup_message_id_table (api_main_t * am)
584 {
585 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
586   foreach_vl_msg_name_crc_l2;
587 #undef _
588 }
589
590 static clib_error_t *
591 l2_api_hookup (vlib_main_t * vm)
592 {
593   api_main_t *am = &api_main;
594
595 #define _(N,n)                                                  \
596     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
597                            vl_api_##n##_t_handler,              \
598                            vl_noop_handler,                     \
599                            vl_api_##n##_t_endian,               \
600                            vl_api_##n##_t_print,                \
601                            sizeof(vl_api_##n##_t), 1);
602   foreach_vpe_api_msg;
603 #undef _
604
605   /*
606    * Set up the (msg_name, crc, message-id) table
607    */
608   setup_message_id_table (am);
609
610   return 0;
611 }
612
613 VLIB_API_INIT_FUNCTION (l2_api_hookup);
614
615 /*
616  * fd.io coding-style-patch-verification: ON
617  *
618  * Local Variables:
619  * eval: (c-set-style "gnu")
620  * End:
621  */