api: refactor vlibmemory
[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 #include <vnet/l2/l2_learn.h>
29
30 #include <vnet/vnet_msg_enum.h>
31
32 #define vl_typedefs             /* define message structures */
33 #include <vnet/vnet_all_api_h.h>
34 #undef vl_typedefs
35
36 #define vl_endianfun            /* define message structures */
37 #include <vnet/vnet_all_api_h.h>
38 #undef vl_endianfun
39
40 #define vl_api_bridge_domain_details_t_endian vl_noop_handler
41 #define vl_api_bridge_domain_details_t_print vl_noop_handler
42
43 /* instantiate all the print functions we know about */
44 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
45 #define vl_printfun
46 #include <vnet/vnet_all_api_h.h>
47 #undef vl_printfun
48
49 #include <vlibapi/api_helper_macros.h>
50
51 #define foreach_vpe_api_msg                                 \
52 _(L2_XCONNECT_DUMP, l2_xconnect_dump)                       \
53 _(L2_FIB_CLEAR_TABLE, l2_fib_clear_table)                   \
54 _(L2_FIB_TABLE_DUMP, l2_fib_table_dump)                     \
55 _(L2FIB_FLUSH_ALL, l2fib_flush_all)                         \
56 _(L2FIB_FLUSH_INT, l2fib_flush_int)                         \
57 _(L2FIB_FLUSH_BD, l2fib_flush_bd)                           \
58 _(L2FIB_ADD_DEL, l2fib_add_del)                             \
59 _(WANT_L2_MACS_EVENTS, want_l2_macs_events)                 \
60 _(L2_FLAGS, l2_flags)                                       \
61 _(SW_INTERFACE_SET_L2_XCONNECT, sw_interface_set_l2_xconnect)   \
62 _(SW_INTERFACE_SET_L2_BRIDGE, sw_interface_set_l2_bridge)       \
63 _(L2_PATCH_ADD_DEL, l2_patch_add_del)                           \
64 _(L2_INTERFACE_EFP_FILTER, l2_interface_efp_filter)             \
65 _(BD_IP_MAC_ADD_DEL, bd_ip_mac_add_del)                         \
66 _(BRIDGE_DOMAIN_ADD_DEL, bridge_domain_add_del)                 \
67 _(BRIDGE_DOMAIN_DUMP, bridge_domain_dump)                       \
68 _(BRIDGE_FLAGS, bridge_flags)                                   \
69 _(L2_INTERFACE_VLAN_TAG_REWRITE, l2_interface_vlan_tag_rewrite) \
70 _(L2_INTERFACE_PBB_TAG_REWRITE, l2_interface_pbb_tag_rewrite)   \
71 _(BRIDGE_DOMAIN_SET_MAC_AGE, bridge_domain_set_mac_age)         \
72 _(SW_INTERFACE_SET_VPATH, sw_interface_set_vpath)
73
74 static void
75 send_l2_xconnect_details (svm_queue_t * q, u32 context,
76                           u32 rx_sw_if_index, u32 tx_sw_if_index)
77 {
78   vl_api_l2_xconnect_details_t *mp;
79
80   mp = vl_msg_api_alloc (sizeof (*mp));
81   memset (mp, 0, sizeof (*mp));
82   mp->_vl_msg_id = ntohs (VL_API_L2_XCONNECT_DETAILS);
83   mp->context = context;
84   mp->rx_sw_if_index = htonl (rx_sw_if_index);
85   mp->tx_sw_if_index = htonl (tx_sw_if_index);
86
87   vl_msg_api_send_shmem (q, (u8 *) & mp);
88 }
89
90 static void
91 vl_api_l2_xconnect_dump_t_handler (vl_api_l2_xconnect_dump_t * mp)
92 {
93   svm_queue_t *q;
94   vnet_main_t *vnm = vnet_get_main ();
95   vnet_interface_main_t *im = &vnm->interface_main;
96   l2input_main_t *l2im = &l2input_main;
97   vnet_sw_interface_t *swif;
98   l2_input_config_t *config;
99
100   q = vl_api_client_index_to_input_queue (mp->client_index);
101   if (q == 0)
102     return;
103
104   /* *INDENT-OFF* */
105   pool_foreach (swif, im->sw_interfaces,
106   ({
107     config = vec_elt_at_index (l2im->configs, swif->sw_if_index);
108     if (config->xconnect)
109       send_l2_xconnect_details (q, mp->context, swif->sw_if_index,
110                                 config->output_sw_if_index);
111   }));
112   /* *INDENT-ON* */
113 }
114
115 static void
116 vl_api_l2_fib_clear_table_t_handler (vl_api_l2_fib_clear_table_t * mp)
117 {
118   int rv = 0;
119   vl_api_l2_fib_clear_table_reply_t *rmp;
120
121   /* Clear all MACs including static MACs  */
122   l2fib_clear_table ();
123
124   REPLY_MACRO (VL_API_L2_FIB_CLEAR_TABLE_REPLY);
125 }
126
127 static void
128 send_l2fib_table_entry (vpe_api_main_t * am,
129                         svm_queue_t * q,
130                         l2fib_entry_key_t * l2fe_key,
131                         l2fib_entry_result_t * l2fe_res, u32 context)
132 {
133   vl_api_l2_fib_table_details_t *mp;
134
135   mp = vl_msg_api_alloc (sizeof (*mp));
136   memset (mp, 0, sizeof (*mp));
137   mp->_vl_msg_id = ntohs (VL_API_L2_FIB_TABLE_DETAILS);
138
139   mp->bd_id =
140     ntohl (l2input_main.bd_configs[l2fe_key->fields.bd_index].bd_id);
141
142   clib_memcpy (mp->mac, l2fe_key->fields.mac, 6);
143   mp->sw_if_index = ntohl (l2fe_res->fields.sw_if_index);
144   mp->static_mac = l2fe_res->fields.static_mac;
145   mp->filter_mac = l2fe_res->fields.filter;
146   mp->bvi_mac = l2fe_res->fields.bvi;
147   mp->context = context;
148
149   vl_msg_api_send_shmem (q, (u8 *) & mp);
150 }
151
152 static void
153 vl_api_l2_fib_table_dump_t_handler (vl_api_l2_fib_table_dump_t * mp)
154 {
155   vpe_api_main_t *am = &vpe_api_main;
156   bd_main_t *bdm = &bd_main;
157   l2fib_entry_key_t *l2fe_key = NULL;
158   l2fib_entry_result_t *l2fe_res = NULL;
159   u32 ni, bd_id = ntohl (mp->bd_id);
160   u32 bd_index;
161   svm_queue_t *q;
162   uword *p;
163
164   q = vl_api_client_index_to_input_queue (mp->client_index);
165   if (q == 0)
166     return;
167
168   /* see l2fib_table_dump: ~0 means "any" */
169   if (bd_id == ~0)
170     bd_index = ~0;
171   else
172     {
173       p = hash_get (bdm->bd_index_by_bd_id, bd_id);
174       if (p == 0)
175         return;
176
177       bd_index = p[0];
178     }
179
180   l2fib_table_dump (bd_index, &l2fe_key, &l2fe_res);
181
182   vec_foreach_index (ni, l2fe_key)
183   {
184     send_l2fib_table_entry (am, q, vec_elt_at_index (l2fe_key, ni),
185                             vec_elt_at_index (l2fe_res, ni), mp->context);
186   }
187   vec_free (l2fe_key);
188   vec_free (l2fe_res);
189 }
190
191 static void
192 vl_api_l2fib_add_del_t_handler (vl_api_l2fib_add_del_t * mp)
193 {
194   bd_main_t *bdm = &bd_main;
195   l2input_main_t *l2im = &l2input_main;
196   vl_api_l2fib_add_del_reply_t *rmp;
197   int rv = 0;
198   u32 bd_id = ntohl (mp->bd_id);
199   uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
200
201   if (!p)
202     {
203       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
204       goto bad_sw_if_index;
205     }
206   u32 bd_index = p[0];
207
208   u8 mac[6];
209
210   clib_memcpy (mac, mp->mac, 6);
211   if (mp->is_add)
212     {
213       if (mp->filter_mac)
214         l2fib_add_filter_entry (mac, bd_index);
215       else
216         {
217           u32 sw_if_index = ntohl (mp->sw_if_index);
218           VALIDATE_SW_IF_INDEX (mp);
219           if (vec_len (l2im->configs) <= sw_if_index)
220             {
221               rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
222               goto bad_sw_if_index;
223             }
224           else
225             {
226               l2_input_config_t *config;
227               config = vec_elt_at_index (l2im->configs, sw_if_index);
228               if (config->bridge == 0)
229                 {
230                   rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
231                   goto bad_sw_if_index;
232                 }
233             }
234           u8 static_mac = mp->static_mac ? 1 : 0;
235           u8 bvi_mac = mp->bvi_mac ? 1 : 0;
236           l2fib_add_fwd_entry (mac, bd_index, sw_if_index, static_mac,
237                                bvi_mac);
238         }
239     }
240   else
241     {
242       l2fib_del_entry (mac, bd_index);
243     }
244
245   BAD_SW_IF_INDEX_LABEL;
246
247   REPLY_MACRO (VL_API_L2FIB_ADD_DEL_REPLY);
248 }
249
250 static void
251 vl_api_want_l2_macs_events_t_handler (vl_api_want_l2_macs_events_t * mp)
252 {
253   int rv = 0;
254   vl_api_want_l2_macs_events_reply_t *rmp;
255   l2learn_main_t *lm = &l2learn_main;
256   l2fib_main_t *fm = &l2fib_main;
257   u32 pid = ntohl (mp->pid);
258   u32 learn_limit = ntohl (mp->learn_limit);
259
260   if (mp->enable_disable)
261     {
262       if (lm->client_pid == 0)
263         {
264           lm->client_pid = pid;
265           lm->client_index = mp->client_index;
266
267           if (mp->max_macs_in_event)
268             fm->max_macs_in_event = mp->max_macs_in_event * 10;
269           else
270             fm->max_macs_in_event = L2FIB_EVENT_MAX_MACS_DEFAULT;
271
272           if (mp->scan_delay)
273             fm->event_scan_delay = (f64) (mp->scan_delay) * 10e-3;
274           else
275             fm->event_scan_delay = L2FIB_EVENT_SCAN_DELAY_DEFAULT;
276
277           /* change learn limit and flush all learned MACs */
278           if (learn_limit && (learn_limit < L2LEARN_DEFAULT_LIMIT))
279             lm->global_learn_limit = learn_limit;
280           else
281             lm->global_learn_limit = L2FIB_EVENT_LEARN_LIMIT_DEFAULT;
282
283           l2fib_flush_all_mac (vlib_get_main ());
284         }
285       else if (lm->client_pid != pid)
286         {
287           rv = VNET_API_ERROR_L2_MACS_EVENT_CLINET_PRESENT;
288           goto exit;
289         }
290     }
291   else if (lm->client_pid)
292     {
293       lm->client_pid = 0;
294       lm->client_index = 0;
295       if (learn_limit && (learn_limit < L2LEARN_DEFAULT_LIMIT))
296         lm->global_learn_limit = learn_limit;
297       else
298         lm->global_learn_limit = L2LEARN_DEFAULT_LIMIT;
299     }
300
301 exit:
302   REPLY_MACRO (VL_API_WANT_L2_MACS_EVENTS_REPLY);
303 }
304
305 static void
306 vl_api_l2fib_flush_int_t_handler (vl_api_l2fib_flush_int_t * mp)
307 {
308   int rv = 0;
309   vlib_main_t *vm = vlib_get_main ();
310   vl_api_l2fib_flush_int_reply_t *rmp;
311
312   VALIDATE_SW_IF_INDEX (mp);
313
314   u32 sw_if_index = ntohl (mp->sw_if_index);
315   l2fib_flush_int_mac (vm, sw_if_index);
316
317   BAD_SW_IF_INDEX_LABEL;
318   REPLY_MACRO (VL_API_L2FIB_FLUSH_INT_REPLY);
319 }
320
321 static void
322 vl_api_l2fib_flush_all_t_handler (vl_api_l2fib_flush_all_t * mp)
323 {
324   int rv = 0;
325   vl_api_l2fib_flush_all_reply_t *rmp;
326
327   l2fib_flush_all_mac (vlib_get_main ());
328   REPLY_MACRO (VL_API_L2FIB_FLUSH_ALL_REPLY);
329 }
330
331 static void
332 vl_api_l2fib_flush_bd_t_handler (vl_api_l2fib_flush_bd_t * mp)
333 {
334   int rv = 0;
335   vlib_main_t *vm = vlib_get_main ();
336   bd_main_t *bdm = &bd_main;
337   vl_api_l2fib_flush_bd_reply_t *rmp;
338
339   u32 bd_id = ntohl (mp->bd_id);
340   uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
341   if (p == 0)
342     {
343       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
344       goto out;
345     }
346   l2fib_flush_bd_mac (vm, *p);
347 out:
348   REPLY_MACRO (VL_API_L2FIB_FLUSH_BD_REPLY);
349 }
350
351 static void
352 vl_api_l2_flags_t_handler (vl_api_l2_flags_t * mp)
353 {
354   vl_api_l2_flags_reply_t *rmp;
355   int rv = 0;
356   u32 rbm = 0;
357
358   VALIDATE_SW_IF_INDEX (mp);
359
360   u32 sw_if_index = ntohl (mp->sw_if_index);
361   u32 flags = ntohl (mp->feature_bitmap);
362   u32 bitmap = 0;
363
364   if (flags & L2_LEARN)
365     bitmap |= L2INPUT_FEAT_LEARN;
366
367   if (flags & L2_FWD)
368     bitmap |= L2INPUT_FEAT_FWD;
369
370   if (flags & L2_FLOOD)
371     bitmap |= L2INPUT_FEAT_FLOOD;
372
373   if (flags & L2_UU_FLOOD)
374     bitmap |= L2INPUT_FEAT_UU_FLOOD;
375
376   if (flags & L2_ARP_TERM)
377     bitmap |= L2INPUT_FEAT_ARP_TERM;
378
379   rbm = l2input_intf_bitmap_enable (sw_if_index, bitmap, mp->is_set);
380
381   BAD_SW_IF_INDEX_LABEL;
382
383   /* *INDENT-OFF* */
384   REPLY_MACRO2(VL_API_L2_FLAGS_REPLY,
385   ({
386     rmp->resulting_feature_bitmap = ntohl(rbm);
387   }));
388   /* *INDENT-ON* */
389 }
390
391 static void
392 vl_api_bridge_domain_set_mac_age_t_handler (vl_api_bridge_domain_set_mac_age_t
393                                             * mp)
394 {
395   vlib_main_t *vm = vlib_get_main ();
396   bd_main_t *bdm = &bd_main;
397   vl_api_bridge_domain_set_mac_age_reply_t *rmp;
398   int rv = 0;
399   u32 bd_id = ntohl (mp->bd_id);
400   uword *p;
401
402   if (bd_id == 0)
403     {
404       rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
405       goto out;
406     }
407
408   p = hash_get (bdm->bd_index_by_bd_id, bd_id);
409   if (p == 0)
410     {
411       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
412       goto out;
413     }
414   bd_set_mac_age (vm, *p, mp->mac_age);
415 out:
416   REPLY_MACRO (VL_API_BRIDGE_DOMAIN_SET_MAC_AGE_REPLY);
417 }
418
419 static void
420 vl_api_bridge_domain_add_del_t_handler (vl_api_bridge_domain_add_del_t * mp)
421 {
422   l2_bridge_domain_add_del_args_t a = {
423     .is_add = mp->is_add,
424     .flood = mp->flood,
425     .uu_flood = mp->uu_flood,
426     .forward = mp->forward,
427     .learn = mp->learn,
428     .arp_term = mp->arp_term,
429     .mac_age = mp->mac_age,
430     .bd_id = ntohl (mp->bd_id),
431     .bd_tag = mp->bd_tag
432   };
433
434   int rv = bd_add_del (&a);
435
436   vl_api_bridge_domain_add_del_reply_t *rmp;
437   REPLY_MACRO (VL_API_BRIDGE_DOMAIN_ADD_DEL_REPLY);
438 }
439
440 static void
441 send_bridge_domain_details (l2input_main_t * l2im,
442                             svm_queue_t * q,
443                             l2_bridge_domain_t * bd_config,
444                             u32 n_sw_ifs, u32 context)
445 {
446   vl_api_bridge_domain_details_t *mp;
447   l2_flood_member_t *m;
448   vl_api_bridge_domain_sw_if_t *sw_ifs;
449   l2_input_config_t *input_cfg;
450
451   mp = vl_msg_api_alloc (sizeof (*mp) +
452                          (n_sw_ifs * sizeof (vl_api_bridge_domain_sw_if_t)));
453   memset (mp, 0, sizeof (*mp));
454   mp->_vl_msg_id = ntohs (VL_API_BRIDGE_DOMAIN_DETAILS);
455   mp->bd_id = ntohl (bd_config->bd_id);
456   mp->flood = bd_feature_flood (bd_config);
457   mp->uu_flood = bd_feature_uu_flood (bd_config);
458   mp->forward = bd_feature_forward (bd_config);
459   mp->learn = bd_feature_learn (bd_config);
460   mp->arp_term = bd_feature_arp_term (bd_config);
461   mp->bvi_sw_if_index = ntohl (bd_config->bvi_sw_if_index);
462   mp->mac_age = bd_config->mac_age;
463   if (bd_config->bd_tag)
464     {
465       strncpy ((char *) mp->bd_tag, (char *) bd_config->bd_tag,
466                ARRAY_LEN (mp->bd_tag) - 1);
467       mp->bd_tag[ARRAY_LEN (mp->bd_tag) - 1] = 0;
468     }
469
470   mp->context = context;
471
472   sw_ifs = (vl_api_bridge_domain_sw_if_t *) mp->sw_if_details;
473   vec_foreach (m, bd_config->members)
474   {
475     sw_ifs->sw_if_index = ntohl (m->sw_if_index);
476     input_cfg = vec_elt_at_index (l2im->configs, m->sw_if_index);
477     sw_ifs->shg = input_cfg->shg;
478     sw_ifs++;
479     mp->n_sw_ifs++;
480   }
481   mp->n_sw_ifs = htonl (mp->n_sw_ifs);
482
483   vl_msg_api_send_shmem (q, (u8 *) & mp);
484 }
485
486 static void
487 vl_api_bridge_domain_dump_t_handler (vl_api_bridge_domain_dump_t * mp)
488 {
489   bd_main_t *bdm = &bd_main;
490   l2input_main_t *l2im = &l2input_main;
491
492   svm_queue_t *q = vl_api_client_index_to_input_queue (mp->client_index);
493   if (q == 0)
494     return;
495
496   u32 bd_id = ntohl (mp->bd_id);
497   if (bd_id == 0)
498     return;
499
500   u32 bd_index, end;
501   if (bd_id == ~0)
502     bd_index = 0, end = vec_len (l2im->bd_configs);
503   else
504     {
505       bd_index = bd_find_index (bdm, bd_id);
506       if (bd_index == ~0)
507         return;
508
509       end = bd_index + 1;
510     }
511
512   for (; bd_index < end; bd_index++)
513     {
514       l2_bridge_domain_t *bd_config =
515         l2input_bd_config_from_index (l2im, bd_index);
516       /* skip dummy bd_id 0 */
517       if (bd_config && (bd_config->bd_id > 0))
518         send_bridge_domain_details (l2im, q, bd_config,
519                                     vec_len (bd_config->members),
520                                     mp->context);
521     }
522 }
523
524 static void
525 vl_api_bridge_flags_t_handler (vl_api_bridge_flags_t * mp)
526 {
527   vlib_main_t *vm = vlib_get_main ();
528   bd_main_t *bdm = &bd_main;
529   vl_api_bridge_flags_reply_t *rmp;
530   int rv = 0;
531
532   u32 flags = ntohl (mp->feature_bitmap);
533   u32 bd_id = ntohl (mp->bd_id);
534   if (bd_id == 0)
535     {
536       rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
537       goto out;
538     }
539
540   u32 bd_index = bd_find_index (bdm, bd_id);
541   if (bd_index == ~0)
542     {
543       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
544       goto out;
545     }
546
547   u32 bitmap = bd_set_flags (vm, bd_index, flags, mp->is_set);
548
549 out:
550   /* *INDENT-OFF* */
551   REPLY_MACRO2(VL_API_BRIDGE_FLAGS_REPLY,
552   ({
553     rmp->resulting_feature_bitmap = ntohl(bitmap);
554   }));
555   /* *INDENT-ON* */
556 }
557
558 static void
559   vl_api_l2_interface_vlan_tag_rewrite_t_handler
560   (vl_api_l2_interface_vlan_tag_rewrite_t * mp)
561 {
562   int rv = 0;
563   vl_api_l2_interface_vlan_tag_rewrite_reply_t *rmp;
564   vnet_main_t *vnm = vnet_get_main ();
565   vlib_main_t *vm = vlib_get_main ();
566   u32 vtr_op;
567
568   VALIDATE_SW_IF_INDEX (mp);
569
570   vtr_op = ntohl (mp->vtr_op);
571
572   /* The L2 code is unsuspicious */
573   switch (vtr_op)
574     {
575     case L2_VTR_DISABLED:
576     case L2_VTR_PUSH_1:
577     case L2_VTR_PUSH_2:
578     case L2_VTR_POP_1:
579     case L2_VTR_POP_2:
580     case L2_VTR_TRANSLATE_1_1:
581     case L2_VTR_TRANSLATE_1_2:
582     case L2_VTR_TRANSLATE_2_1:
583     case L2_VTR_TRANSLATE_2_2:
584       break;
585
586     default:
587       rv = VNET_API_ERROR_INVALID_VALUE;
588       goto bad_sw_if_index;
589     }
590
591   rv = l2vtr_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
592                         ntohl (mp->push_dot1q), ntohl (mp->tag1),
593                         ntohl (mp->tag2));
594
595   BAD_SW_IF_INDEX_LABEL;
596
597   REPLY_MACRO (VL_API_L2_INTERFACE_VLAN_TAG_REWRITE_REPLY);
598 }
599
600 static void
601   vl_api_l2_interface_pbb_tag_rewrite_t_handler
602   (vl_api_l2_interface_pbb_tag_rewrite_t * mp)
603 {
604   vl_api_l2_interface_pbb_tag_rewrite_reply_t *rmp;
605   vnet_main_t *vnm = vnet_get_main ();
606   vlib_main_t *vm = vlib_get_main ();
607   u32 vtr_op;
608   int rv = 0;
609
610   VALIDATE_SW_IF_INDEX (mp);
611
612   vtr_op = ntohl (mp->vtr_op);
613
614   switch (vtr_op)
615     {
616     case L2_VTR_DISABLED:
617     case L2_VTR_PUSH_2:
618     case L2_VTR_POP_2:
619     case L2_VTR_TRANSLATE_2_1:
620       break;
621
622     default:
623       rv = VNET_API_ERROR_INVALID_VALUE;
624       goto bad_sw_if_index;
625     }
626
627   rv = l2pbb_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
628                         mp->b_dmac, mp->b_smac, ntohs (mp->b_vlanid),
629                         ntohl (mp->i_sid), ntohs (mp->outer_tag));
630
631   BAD_SW_IF_INDEX_LABEL;
632
633   REPLY_MACRO (VL_API_L2_INTERFACE_PBB_TAG_REWRITE_REPLY);
634 }
635
636 static void
637   vl_api_sw_interface_set_l2_xconnect_t_handler
638   (vl_api_sw_interface_set_l2_xconnect_t * mp)
639 {
640   vl_api_sw_interface_set_l2_xconnect_reply_t *rmp;
641   int rv = 0;
642   u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
643   u32 tx_sw_if_index = ntohl (mp->tx_sw_if_index);
644   vlib_main_t *vm = vlib_get_main ();
645   vnet_main_t *vnm = vnet_get_main ();
646
647   VALIDATE_RX_SW_IF_INDEX (mp);
648
649   if (mp->enable)
650     {
651       VALIDATE_TX_SW_IF_INDEX (mp);
652       rv = set_int_l2_mode (vm, vnm, MODE_L2_XC,
653                             rx_sw_if_index, 0, 0, 0, tx_sw_if_index);
654     }
655   else
656     {
657       rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0, 0, 0, 0);
658     }
659
660   BAD_RX_SW_IF_INDEX_LABEL;
661   BAD_TX_SW_IF_INDEX_LABEL;
662
663   REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_XCONNECT_REPLY);
664 }
665
666 static void
667   vl_api_sw_interface_set_l2_bridge_t_handler
668   (vl_api_sw_interface_set_l2_bridge_t * mp)
669 {
670   bd_main_t *bdm = &bd_main;
671   vl_api_sw_interface_set_l2_bridge_reply_t *rmp;
672   int rv = 0;
673   vlib_main_t *vm = vlib_get_main ();
674   vnet_main_t *vnm = vnet_get_main ();
675
676   VALIDATE_RX_SW_IF_INDEX (mp);
677   u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
678
679
680   if (mp->enable)
681     {
682       VALIDATE_BD_ID (mp);
683       u32 bd_id = ntohl (mp->bd_id);
684       u32 bd_index = bd_find_or_add_bd_index (bdm, bd_id);
685       u32 bvi = mp->bvi;
686       u8 shg = mp->shg;
687       rv = set_int_l2_mode (vm, vnm, MODE_L2_BRIDGE,
688                             rx_sw_if_index, bd_index, bvi, shg, 0);
689     }
690   else
691     {
692       rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0, 0, 0, 0);
693     }
694
695   BAD_RX_SW_IF_INDEX_LABEL;
696   BAD_BD_ID_LABEL;
697
698   REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_BRIDGE_REPLY);
699 }
700
701 static void
702 vl_api_bd_ip_mac_add_del_t_handler (vl_api_bd_ip_mac_add_del_t * mp)
703 {
704   bd_main_t *bdm = &bd_main;
705   vl_api_bd_ip_mac_add_del_reply_t *rmp;
706   int rv = 0;
707   u32 bd_id = ntohl (mp->bd_id);
708   u32 bd_index;
709   uword *p;
710
711   if (bd_id == 0)
712     {
713       rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
714       goto out;
715     }
716
717   p = hash_get (bdm->bd_index_by_bd_id, bd_id);
718   if (p == 0)
719     {
720       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
721       goto out;
722     }
723
724   bd_index = p[0];
725   if (bd_add_del_ip_mac (bd_index, mp->ip_address,
726                          mp->mac_address, mp->is_ipv6, mp->is_add))
727     rv = VNET_API_ERROR_UNSPECIFIED;
728
729 out:
730   REPLY_MACRO (VL_API_BD_IP_MAC_ADD_DEL_REPLY);
731 }
732
733 extern void l2_efp_filter_configure (vnet_main_t * vnet_main,
734                                      u32 sw_if_index, u32 enable);
735
736 static void
737 vl_api_l2_interface_efp_filter_t_handler (vl_api_l2_interface_efp_filter_t *
738                                           mp)
739 {
740   int rv;
741   vl_api_l2_interface_efp_filter_reply_t *rmp;
742   vnet_main_t *vnm = vnet_get_main ();
743
744   // enable/disable the feature
745   l2_efp_filter_configure (vnm, mp->sw_if_index, mp->enable_disable);
746   rv = vnm->api_errno;
747
748   REPLY_MACRO (VL_API_L2_INTERFACE_EFP_FILTER_REPLY);
749 }
750
751 static void
752 vl_api_l2_patch_add_del_t_handler (vl_api_l2_patch_add_del_t * mp)
753 {
754   extern int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
755                                     int is_add);
756   vl_api_l2_patch_add_del_reply_t *rmp;
757   int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
758                              int is_add);
759   int rv = 0;
760
761   VALIDATE_RX_SW_IF_INDEX (mp);
762   VALIDATE_TX_SW_IF_INDEX (mp);
763
764   rv = vnet_l2_patch_add_del (ntohl (mp->rx_sw_if_index),
765                               ntohl (mp->tx_sw_if_index),
766                               (int) (mp->is_add != 0));
767
768   BAD_RX_SW_IF_INDEX_LABEL;
769   BAD_TX_SW_IF_INDEX_LABEL;
770
771   REPLY_MACRO (VL_API_L2_PATCH_ADD_DEL_REPLY);
772 }
773
774 static void
775 vl_api_sw_interface_set_vpath_t_handler (vl_api_sw_interface_set_vpath_t * mp)
776 {
777   vl_api_sw_interface_set_vpath_reply_t *rmp;
778   int rv = 0;
779   u32 sw_if_index = ntohl (mp->sw_if_index);
780
781   VALIDATE_SW_IF_INDEX (mp);
782
783   l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_VPATH, mp->enable);
784   vnet_feature_enable_disable ("ip4-unicast", "vpath-input-ip4",
785                                sw_if_index, mp->enable, 0, 0);
786   vnet_feature_enable_disable ("ip4-multicast", "vpath-input-ip4",
787                                sw_if_index, mp->enable, 0, 0);
788   vnet_feature_enable_disable ("ip6-unicast", "vpath-input-ip6",
789                                sw_if_index, mp->enable, 0, 0);
790   vnet_feature_enable_disable ("ip6-multicast", "vpath-input-ip6",
791                                sw_if_index, mp->enable, 0, 0);
792
793   BAD_SW_IF_INDEX_LABEL;
794
795   REPLY_MACRO (VL_API_SW_INTERFACE_SET_VPATH_REPLY);
796 }
797
798 /*
799  * l2_api_hookup
800  * Add vpe's API message handlers to the table.
801  * vlib has alread mapped shared memory and
802  * added the client registration handlers.
803  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
804  */
805 #define vl_msg_name_crc_list
806 #include <vnet/vnet_all_api_h.h>
807 #undef vl_msg_name_crc_list
808
809 static void
810 setup_message_id_table (api_main_t * am)
811 {
812 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
813   foreach_vl_msg_name_crc_l2;
814 #undef _
815 }
816
817 static clib_error_t *
818 l2_api_hookup (vlib_main_t * vm)
819 {
820   api_main_t *am = &api_main;
821
822 #define _(N,n)                                                  \
823     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
824                            vl_api_##n##_t_handler,              \
825                            vl_noop_handler,                     \
826                            vl_api_##n##_t_endian,               \
827                            vl_api_##n##_t_print,                \
828                            sizeof(vl_api_##n##_t), 1);
829   foreach_vpe_api_msg;
830 #undef _
831
832   /*
833    * Set up the (msg_name, crc, message-id) table
834    */
835   setup_message_id_table (am);
836
837   return 0;
838 }
839
840 VLIB_API_INIT_FUNCTION (l2_api_hookup);
841
842 /*
843  * fd.io coding-style-patch-verification: ON
844  *
845  * Local Variables:
846  * eval: (c-set-style "gnu")
847  * End:
848  */