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