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