l2: Separating scan-delay and learn-limit into a separate API from want_l2_macs_events
[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 #include <vnet/l2/l2_bd.h>
30 #include <vnet/l2/l2_bvi.h>
31 #include <vnet/l2/l2_arp_term.h>
32 #include <vnet/ip/ip_types_api.h>
33 #include <vnet/ethernet/ethernet_types_api.h>
34
35 #include <vnet/vnet_msg_enum.h>
36
37 #define vl_typedefs             /* define message structures */
38 #include <vnet/vnet_all_api_h.h>
39 #undef vl_typedefs
40
41 #define vl_endianfun            /* define message structures */
42 #include <vnet/vnet_all_api_h.h>
43 #undef vl_endianfun
44
45 /* instantiate all the print functions we know about */
46 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
47 #define vl_printfun
48 #include <vnet/vnet_all_api_h.h>
49 #undef vl_printfun
50
51 #include <vlibapi/api_helper_macros.h>
52
53 #define foreach_vpe_api_msg                                                   \
54   _ (L2_XCONNECT_DUMP, l2_xconnect_dump)                                      \
55   _ (L2_FIB_CLEAR_TABLE, l2_fib_clear_table)                                  \
56   _ (L2_FIB_TABLE_DUMP, l2_fib_table_dump)                                    \
57   _ (L2FIB_FLUSH_ALL, l2fib_flush_all)                                        \
58   _ (L2FIB_FLUSH_INT, l2fib_flush_int)                                        \
59   _ (L2FIB_FLUSH_BD, l2fib_flush_bd)                                          \
60   _ (L2FIB_ADD_DEL, l2fib_add_del)                                            \
61   _ (L2FIB_SET_SCAN_DELAY, l2fib_set_scan_delay)                              \
62   _ (WANT_L2_MACS_EVENTS, want_l2_macs_events)                                \
63   _ (WANT_L2_MACS_EVENTS2, want_l2_macs_events2)                              \
64   _ (L2_FLAGS, l2_flags)                                                      \
65   _ (SW_INTERFACE_SET_L2_XCONNECT, sw_interface_set_l2_xconnect)              \
66   _ (SW_INTERFACE_SET_L2_BRIDGE, sw_interface_set_l2_bridge)                  \
67   _ (L2_PATCH_ADD_DEL, l2_patch_add_del)                                      \
68   _ (L2_INTERFACE_EFP_FILTER, l2_interface_efp_filter)                        \
69   _ (BD_IP_MAC_ADD_DEL, bd_ip_mac_add_del)                                    \
70   _ (BD_IP_MAC_FLUSH, bd_ip_mac_flush)                                        \
71   _ (BD_IP_MAC_DUMP, bd_ip_mac_dump)                                          \
72   _ (BRIDGE_DOMAIN_ADD_DEL, bridge_domain_add_del)                            \
73   _ (BRIDGE_DOMAIN_DUMP, bridge_domain_dump)                                  \
74   _ (BRIDGE_FLAGS, bridge_flags)                                              \
75   _ (L2_INTERFACE_VLAN_TAG_REWRITE, l2_interface_vlan_tag_rewrite)            \
76   _ (L2_INTERFACE_PBB_TAG_REWRITE, l2_interface_pbb_tag_rewrite)              \
77   _ (BRIDGE_DOMAIN_SET_MAC_AGE, bridge_domain_set_mac_age)                    \
78   _ (SW_INTERFACE_SET_VPATH, sw_interface_set_vpath)                          \
79   _ (BVI_CREATE, bvi_create)                                                  \
80   _ (BVI_DELETE, bvi_delete)                                                  \
81   _ (WANT_L2_ARP_TERM_EVENTS, want_l2_arp_term_events)                        \
82   _ (BRIDGE_DOMAIN_SET_LEARN_LIMIT, bridge_domain_set_learn_limit)            \
83   _ (BRIDGE_DOMAIN_SET_DEFAULT_LEARN_LIMIT,                                   \
84      bridge_domain_set_default_learn_limit)
85
86 static void
87 send_l2_xconnect_details (vl_api_registration_t * reg, u32 context,
88                           u32 rx_sw_if_index, u32 tx_sw_if_index)
89 {
90   vl_api_l2_xconnect_details_t *mp;
91
92   mp = vl_msg_api_alloc (sizeof (*mp));
93   clib_memset (mp, 0, sizeof (*mp));
94   mp->_vl_msg_id = ntohs (VL_API_L2_XCONNECT_DETAILS);
95   mp->context = context;
96   mp->rx_sw_if_index = htonl (rx_sw_if_index);
97   mp->tx_sw_if_index = htonl (tx_sw_if_index);
98
99   vl_api_send_msg (reg, (u8 *) mp);
100 }
101
102 static void
103 vl_api_l2_xconnect_dump_t_handler (vl_api_l2_xconnect_dump_t * mp)
104 {
105   vl_api_registration_t *reg;
106   vnet_main_t *vnm = vnet_get_main ();
107   vnet_interface_main_t *im = &vnm->interface_main;
108   l2input_main_t *l2im = &l2input_main;
109   vnet_sw_interface_t *swif;
110   l2_input_config_t *config;
111
112   reg = vl_api_client_index_to_registration (mp->client_index);
113   if (!reg)
114     return;
115
116   /* *INDENT-OFF* */
117   pool_foreach (swif, im->sw_interfaces)
118    {
119     config = vec_elt_at_index (l2im->configs, swif->sw_if_index);
120     if (l2_input_is_xconnect(config))
121       send_l2_xconnect_details (reg, mp->context, swif->sw_if_index,
122                                 config->output_sw_if_index);
123   }
124   /* *INDENT-ON* */
125 }
126
127 static void
128 vl_api_l2_fib_clear_table_t_handler (vl_api_l2_fib_clear_table_t * mp)
129 {
130   int rv = 0;
131   vl_api_l2_fib_clear_table_reply_t *rmp;
132
133   /* Clear all MACs including static MACs  */
134   l2fib_clear_table ();
135
136   REPLY_MACRO (VL_API_L2_FIB_CLEAR_TABLE_REPLY);
137 }
138
139 static void
140 send_l2fib_table_entry (vpe_api_main_t * am,
141                         vl_api_registration_t * reg,
142                         l2fib_entry_key_t * l2fe_key,
143                         l2fib_entry_result_t * l2fe_res, u32 context)
144 {
145   vl_api_l2_fib_table_details_t *mp;
146
147   mp = vl_msg_api_alloc (sizeof (*mp));
148   clib_memset (mp, 0, sizeof (*mp));
149   mp->_vl_msg_id = ntohs (VL_API_L2_FIB_TABLE_DETAILS);
150
151   mp->bd_id =
152     ntohl (l2input_main.bd_configs[l2fe_key->fields.bd_index].bd_id);
153
154   mac_address_encode ((mac_address_t *) l2fe_key->fields.mac, mp->mac);
155   mp->sw_if_index = ntohl (l2fe_res->fields.sw_if_index);
156   mp->static_mac = (l2fib_entry_result_is_set_STATIC (l2fe_res) ? 1 : 0);
157   mp->filter_mac = (l2fib_entry_result_is_set_FILTER (l2fe_res) ? 1 : 0);
158   mp->bvi_mac = (l2fib_entry_result_is_set_BVI (l2fe_res) ? 1 : 0);
159   mp->context = context;
160
161   vl_api_send_msg (reg, (u8 *) mp);
162 }
163
164 static void
165 vl_api_l2_fib_table_dump_t_handler (vl_api_l2_fib_table_dump_t * mp)
166 {
167   vpe_api_main_t *am = &vpe_api_main;
168   bd_main_t *bdm = &bd_main;
169   l2fib_entry_key_t *l2fe_key = NULL;
170   l2fib_entry_result_t *l2fe_res = NULL;
171   u32 ni, bd_id = ntohl (mp->bd_id);
172   u32 bd_index;
173   vl_api_registration_t *reg;
174   uword *p;
175
176   reg = vl_api_client_index_to_registration (mp->client_index);
177   if (!reg)
178     return;
179
180   /* see l2fib_table_dump: ~0 means "any" */
181   if (bd_id == ~0)
182     bd_index = ~0;
183   else
184     {
185       p = hash_get (bdm->bd_index_by_bd_id, bd_id);
186       if (p == 0)
187         return;
188
189       bd_index = p[0];
190     }
191
192   l2fib_table_dump (bd_index, &l2fe_key, &l2fe_res);
193
194   vec_foreach_index (ni, l2fe_key)
195   {
196     send_l2fib_table_entry (am, reg, vec_elt_at_index (l2fe_key, ni),
197                             vec_elt_at_index (l2fe_res, ni), mp->context);
198   }
199   vec_free (l2fe_key);
200   vec_free (l2fe_res);
201 }
202
203 static void
204 vl_api_l2fib_add_del_t_handler (vl_api_l2fib_add_del_t * mp)
205 {
206   bd_main_t *bdm = &bd_main;
207   l2input_main_t *l2im = &l2input_main;
208   vl_api_l2fib_add_del_reply_t *rmp;
209   int rv = 0;
210   u32 bd_id = ntohl (mp->bd_id);
211   uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
212
213   if (!p)
214     {
215       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
216       goto bad_sw_if_index;
217     }
218   u32 bd_index = p[0];
219
220   mac_address_t mac;
221
222   mac_address_decode (mp->mac, &mac);
223   if (mp->is_add)
224     {
225       if (mp->filter_mac)
226         l2fib_add_filter_entry (mac.bytes, bd_index);
227       else
228         {
229           l2fib_entry_result_flags_t flags = L2FIB_ENTRY_RESULT_FLAG_NONE;
230           u32 sw_if_index = ntohl (mp->sw_if_index);
231           VALIDATE_SW_IF_INDEX (mp);
232           if (vec_len (l2im->configs) <= sw_if_index)
233             {
234               rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
235               goto bad_sw_if_index;
236             }
237           else
238             {
239               l2_input_config_t *config;
240               config = vec_elt_at_index (l2im->configs, sw_if_index);
241               if (!l2_input_is_bridge (config))
242                 {
243                   rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
244                   goto bad_sw_if_index;
245                 }
246             }
247           if (mp->static_mac)
248             flags |= L2FIB_ENTRY_RESULT_FLAG_STATIC;
249           if (mp->bvi_mac)
250             flags |= L2FIB_ENTRY_RESULT_FLAG_BVI;
251           l2fib_add_entry (mac.bytes, bd_index, sw_if_index, flags);
252         }
253     }
254   else
255     {
256       u32 sw_if_index = ntohl (mp->sw_if_index);
257       if (l2fib_del_entry (mac.bytes, bd_index, sw_if_index))
258         rv = VNET_API_ERROR_NO_SUCH_ENTRY;
259     }
260
261   BAD_SW_IF_INDEX_LABEL;
262
263   REPLY_MACRO (VL_API_L2FIB_ADD_DEL_REPLY);
264 }
265
266 static void
267 vl_api_want_l2_macs_events2_t_handler (vl_api_want_l2_macs_events2_t *mp)
268 {
269   int rv = 0;
270   vl_api_want_l2_macs_events2_reply_t *rmp;
271   l2learn_main_t *lm = &l2learn_main;
272   l2fib_main_t *fm = &l2fib_main;
273   u32 pid = ntohl (mp->pid);
274
275   if (mp->enable_disable)
276     {
277       if ((lm->client_pid == 0) || (lm->client_pid == pid))
278         {
279           if (mp->max_macs_in_event)
280             fm->max_macs_in_event = mp->max_macs_in_event * 10;
281           else
282             {
283               rv = VNET_API_ERROR_INVALID_VALUE;
284               goto exit;
285             }
286
287           /* if scan_delay was not set before */
288           if (fm->event_scan_delay == 0.0)
289             fm->event_scan_delay = (f64) (10) * 10e-3;
290
291           lm->client_pid = pid;
292           lm->client_index = mp->client_index;
293           l2fib_flush_all_mac (vlib_get_main ());
294         }
295       else if (lm->client_pid != pid)
296         {
297           rv = VNET_API_ERROR_L2_MACS_EVENT_CLINET_PRESENT;
298           goto exit;
299         }
300     }
301   else if (lm->client_pid)
302     {
303       lm->client_pid = 0;
304       lm->client_index = 0;
305     }
306
307 exit:
308   REPLY_MACRO (VL_API_WANT_L2_MACS_EVENTS2_REPLY);
309 }
310
311 static void
312 vl_api_want_l2_macs_events_t_handler (vl_api_want_l2_macs_events_t *mp)
313 {
314   int rv = 0;
315   vl_api_want_l2_macs_events_reply_t *rmp;
316   l2learn_main_t *lm = &l2learn_main;
317   l2fib_main_t *fm = &l2fib_main;
318   u32 pid = ntohl (mp->pid);
319   u32 learn_limit = ntohl (mp->learn_limit);
320
321   if (mp->enable_disable)
322     {
323       if ((lm->client_pid == 0) || (lm->client_pid == pid))
324         {
325           if ((mp->max_macs_in_event == 0) || (mp->scan_delay == 0) ||
326               (learn_limit == 0) || (learn_limit > L2LEARN_DEFAULT_LIMIT))
327             {
328               rv = VNET_API_ERROR_INVALID_VALUE;
329               goto exit;
330             }
331           lm->client_pid = pid;
332           lm->client_index = mp->client_index;
333
334           fm->max_macs_in_event = mp->max_macs_in_event * 10;
335           fm->event_scan_delay = (f64) (mp->scan_delay) * 10e-3;
336
337           /* change learn limit and flush all learned MACs */
338           lm->global_learn_limit = learn_limit;
339           l2fib_flush_all_mac (vlib_get_main ());
340         }
341       else if (lm->client_pid != pid)
342         {
343           rv = VNET_API_ERROR_L2_MACS_EVENT_CLINET_PRESENT;
344           goto exit;
345         }
346     }
347   else if (lm->client_pid)
348     {
349       lm->client_pid = 0;
350       lm->client_index = 0;
351       if (learn_limit && (learn_limit <= L2LEARN_DEFAULT_LIMIT))
352         lm->global_learn_limit = learn_limit;
353       else
354         lm->global_learn_limit = L2LEARN_DEFAULT_LIMIT;
355     }
356
357 exit:
358   REPLY_MACRO (VL_API_WANT_L2_MACS_EVENTS_REPLY);
359 }
360
361 static void
362 vl_api_l2fib_flush_int_t_handler (vl_api_l2fib_flush_int_t * mp)
363 {
364   int rv = 0;
365   vlib_main_t *vm = vlib_get_main ();
366   vl_api_l2fib_flush_int_reply_t *rmp;
367
368   VALIDATE_SW_IF_INDEX (mp);
369
370   u32 sw_if_index = ntohl (mp->sw_if_index);
371   l2fib_flush_int_mac (vm, sw_if_index);
372
373   BAD_SW_IF_INDEX_LABEL;
374   REPLY_MACRO (VL_API_L2FIB_FLUSH_INT_REPLY);
375 }
376
377 static void
378 vl_api_l2fib_flush_all_t_handler (vl_api_l2fib_flush_all_t * mp)
379 {
380   int rv = 0;
381   vl_api_l2fib_flush_all_reply_t *rmp;
382
383   l2fib_flush_all_mac (vlib_get_main ());
384   REPLY_MACRO (VL_API_L2FIB_FLUSH_ALL_REPLY);
385 }
386
387 static void
388 vl_api_l2fib_flush_bd_t_handler (vl_api_l2fib_flush_bd_t * mp)
389 {
390   int rv = 0;
391   vlib_main_t *vm = vlib_get_main ();
392   bd_main_t *bdm = &bd_main;
393   vl_api_l2fib_flush_bd_reply_t *rmp;
394
395   u32 bd_id = ntohl (mp->bd_id);
396   uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
397   if (p == 0)
398     {
399       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
400       goto out;
401     }
402   l2fib_flush_bd_mac (vm, *p);
403 out:
404   REPLY_MACRO (VL_API_L2FIB_FLUSH_BD_REPLY);
405 }
406
407 static void
408 vl_api_l2fib_set_scan_delay_t_handler (vl_api_l2fib_set_scan_delay_t *mp)
409 {
410   int rv = 0;
411   l2fib_main_t *fm = &l2fib_main;
412   vl_api_l2fib_set_scan_delay_reply_t *rmp;
413   u16 scan_delay = ntohs (mp->scan_delay);
414
415   if (mp->scan_delay)
416     {
417       fm->event_scan_delay = (f64) (scan_delay) *10e-3;
418       l2fib_flush_all_mac (vlib_get_main ());
419     }
420   else
421     {
422       rv = VNET_API_ERROR_INVALID_VALUE;
423       goto exit;
424     }
425
426 exit:
427   REPLY_MACRO (VL_API_L2FIB_SET_SCAN_DELAY_REPLY);
428 }
429
430 static void
431 vl_api_l2_flags_t_handler (vl_api_l2_flags_t * mp)
432 {
433   vl_api_l2_flags_reply_t *rmp;
434   int rv = 0;
435   u32 rbm = 0;
436
437   VALIDATE_SW_IF_INDEX (mp);
438
439   u32 sw_if_index = ntohl (mp->sw_if_index);
440   u32 flags = ntohl (mp->feature_bitmap);
441   u32 bitmap = 0;
442
443   if (flags & L2_LEARN)
444     bitmap |= L2INPUT_FEAT_LEARN;
445
446   if (flags & L2_FWD)
447     bitmap |= L2INPUT_FEAT_FWD;
448
449   if (flags & L2_FLOOD)
450     bitmap |= L2INPUT_FEAT_FLOOD;
451
452   if (flags & L2_UU_FLOOD)
453     bitmap |= L2INPUT_FEAT_UU_FLOOD;
454
455   if (flags & L2_ARP_TERM)
456     bitmap |= L2INPUT_FEAT_ARP_TERM;
457
458   rbm = l2input_intf_bitmap_enable (sw_if_index, bitmap, mp->is_set);
459
460   BAD_SW_IF_INDEX_LABEL;
461
462   /* *INDENT-OFF* */
463   REPLY_MACRO2(VL_API_L2_FLAGS_REPLY,
464   ({
465     rmp->resulting_feature_bitmap = ntohl(rbm);
466   }));
467   /* *INDENT-ON* */
468 }
469
470 static void
471 vl_api_bridge_domain_set_default_learn_limit_t_handler (
472   vl_api_bridge_domain_set_default_learn_limit_t *mp)
473 {
474   vl_api_bridge_domain_set_default_learn_limit_reply_t *rmp;
475   int rv = 0;
476
477   l2learn_main.bd_default_learn_limit = ntohl (mp->learn_limit);
478   REPLY_MACRO (VL_API_BRIDGE_DOMAIN_SET_DEFAULT_LEARN_LIMIT_REPLY);
479 }
480
481 static void
482 vl_api_bridge_domain_set_learn_limit_t_handler (
483   vl_api_bridge_domain_set_learn_limit_t *mp)
484 {
485   vlib_main_t *vm = vlib_get_main ();
486   bd_main_t *bdm = &bd_main;
487   vl_api_bridge_domain_set_learn_limit_reply_t *rmp;
488   int rv = 0;
489   u32 bd_id = ntohl (mp->bd_id);
490   uword *p;
491
492   if (bd_id == 0)
493     {
494       rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
495       goto out;
496     }
497
498   p = hash_get (bdm->bd_index_by_bd_id, bd_id);
499   if (p == 0)
500     {
501       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
502       goto out;
503     }
504   bd_set_learn_limit (vm, *p, ntohl (mp->learn_limit));
505 out:
506   REPLY_MACRO (VL_API_BRIDGE_DOMAIN_SET_LEARN_LIMIT_REPLY);
507 }
508
509 static void
510 vl_api_bridge_domain_set_mac_age_t_handler (vl_api_bridge_domain_set_mac_age_t
511                                             * mp)
512 {
513   vlib_main_t *vm = vlib_get_main ();
514   bd_main_t *bdm = &bd_main;
515   vl_api_bridge_domain_set_mac_age_reply_t *rmp;
516   int rv = 0;
517   u32 bd_id = ntohl (mp->bd_id);
518   uword *p;
519
520   if (bd_id == 0)
521     {
522       rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
523       goto out;
524     }
525
526   p = hash_get (bdm->bd_index_by_bd_id, bd_id);
527   if (p == 0)
528     {
529       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
530       goto out;
531     }
532   bd_set_mac_age (vm, *p, mp->mac_age);
533 out:
534   REPLY_MACRO (VL_API_BRIDGE_DOMAIN_SET_MAC_AGE_REPLY);
535 }
536
537 static void
538 vl_api_bridge_domain_add_del_t_handler (vl_api_bridge_domain_add_del_t * mp)
539 {
540   l2_bridge_domain_add_del_args_t a = {
541     .is_add = mp->is_add,
542     .flood = mp->flood,
543     .uu_flood = mp->uu_flood,
544     .forward = mp->forward,
545     .learn = mp->learn,
546     .arp_term = mp->arp_term,
547     .arp_ufwd = mp->arp_ufwd,
548     .mac_age = mp->mac_age,
549     .bd_id = ntohl (mp->bd_id),
550     .bd_tag = mp->bd_tag
551   };
552
553   int rv = bd_add_del (&a);
554
555   vl_api_bridge_domain_add_del_reply_t *rmp;
556   REPLY_MACRO (VL_API_BRIDGE_DOMAIN_ADD_DEL_REPLY);
557 }
558
559 static void
560 send_bridge_domain_details (l2input_main_t * l2im,
561                             vl_api_registration_t * reg,
562                             l2_bridge_domain_t * bd_config,
563                             u32 n_sw_ifs, u32 context)
564 {
565   vl_api_bridge_domain_details_t *mp;
566   l2_flood_member_t *m;
567   vl_api_bridge_domain_sw_if_t *sw_ifs;
568   l2_input_config_t *input_cfg;
569
570   mp = vl_msg_api_alloc (sizeof (*mp) +
571                          (n_sw_ifs * sizeof (vl_api_bridge_domain_sw_if_t)));
572   clib_memset (mp, 0, sizeof (*mp));
573   mp->_vl_msg_id = ntohs (VL_API_BRIDGE_DOMAIN_DETAILS);
574   mp->bd_id = ntohl (bd_config->bd_id);
575   mp->flood = bd_feature_flood (bd_config);
576   mp->uu_flood = bd_feature_uu_flood (bd_config);
577   mp->forward = bd_feature_forward (bd_config);
578   mp->learn = bd_feature_learn (bd_config);
579   mp->arp_term = bd_feature_arp_term (bd_config);
580   mp->arp_ufwd = bd_feature_arp_ufwd (bd_config);
581   mp->bvi_sw_if_index = ntohl (bd_config->bvi_sw_if_index);
582   mp->uu_fwd_sw_if_index = ntohl (bd_config->uu_fwd_sw_if_index);
583   mp->mac_age = bd_config->mac_age;
584   if (bd_config->bd_tag)
585     {
586       strncpy ((char *) mp->bd_tag, (char *) bd_config->bd_tag,
587                ARRAY_LEN (mp->bd_tag) - 1);
588       mp->bd_tag[ARRAY_LEN (mp->bd_tag) - 1] = 0;
589     }
590
591   mp->context = context;
592
593   sw_ifs = (vl_api_bridge_domain_sw_if_t *) mp->sw_if_details;
594   vec_foreach (m, bd_config->members)
595   {
596     sw_ifs->sw_if_index = ntohl (m->sw_if_index);
597     input_cfg = vec_elt_at_index (l2im->configs, m->sw_if_index);
598     sw_ifs->shg = input_cfg->shg;
599     sw_ifs++;
600     mp->n_sw_ifs++;
601   }
602   mp->n_sw_ifs = htonl (mp->n_sw_ifs);
603
604   vl_api_send_msg (reg, (u8 *) mp);
605 }
606
607 static void
608 vl_api_bridge_domain_dump_t_handler (vl_api_bridge_domain_dump_t * mp)
609 {
610   bd_main_t *bdm = &bd_main;
611   l2input_main_t *l2im = &l2input_main;
612   vl_api_registration_t *reg;
613   u32 bd_id, bd_index, end, filter_sw_if_index;
614
615   reg = vl_api_client_index_to_registration (mp->client_index);
616   if (!reg)
617     return;
618
619   filter_sw_if_index = ntohl (mp->sw_if_index);
620   if (filter_sw_if_index != ~0)
621     return;                     /* UNIMPLEMENTED */
622
623   bd_id = ntohl (mp->bd_id);
624   if (bd_id == 0)
625     return;
626
627   if (bd_id == ~0)
628     bd_index = 0, end = vec_len (l2im->bd_configs);
629   else
630     {
631       bd_index = bd_find_index (bdm, bd_id);
632       if (bd_index == ~0)
633         return;
634
635       end = bd_index + 1;
636     }
637
638   for (; bd_index < end; bd_index++)
639     {
640       l2_bridge_domain_t *bd_config =
641         l2input_bd_config_from_index (l2im, bd_index);
642       /* skip placeholder bd_id 0 */
643       if (bd_config && (bd_config->bd_id > 0))
644         send_bridge_domain_details (l2im, reg, bd_config,
645                                     vec_len (bd_config->members),
646                                     mp->context);
647     }
648 }
649
650 static bd_flags_t
651 bd_flags_decode (vl_api_bd_flags_t v)
652 {
653   bd_flags_t f = L2_NONE;
654
655   v = ntohl (v);
656
657   if (v & BRIDGE_API_FLAG_LEARN)
658     f |= L2_LEARN;
659   if (v & BRIDGE_API_FLAG_FWD)
660     f |= L2_FWD;
661   if (v & BRIDGE_API_FLAG_FLOOD)
662     f |= L2_FLOOD;
663   if (v & BRIDGE_API_FLAG_UU_FLOOD)
664     f |= L2_UU_FLOOD;
665   if (v & BRIDGE_API_FLAG_ARP_TERM)
666     f |= L2_ARP_TERM;
667   if (v & BRIDGE_API_FLAG_ARP_UFWD)
668     f |= L2_ARP_UFWD;
669
670   return (f);
671 }
672
673 static void
674 vl_api_bridge_flags_t_handler (vl_api_bridge_flags_t * mp)
675 {
676   vlib_main_t *vm = vlib_get_main ();
677   bd_main_t *bdm = &bd_main;
678   vl_api_bridge_flags_reply_t *rmp;
679   u32 bitmap = 0;
680   int rv = 0;
681
682   bd_flags_t flags = bd_flags_decode (mp->flags);
683   u32 bd_id = ntohl (mp->bd_id);
684   if (bd_id == 0)
685     {
686       rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
687       goto out;
688     }
689
690   u32 bd_index = bd_find_index (bdm, bd_id);
691   if (bd_index == ~0)
692     {
693       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
694       goto out;
695     }
696
697   bitmap = bd_set_flags (vm, bd_index, flags, mp->is_set);
698
699 out:
700   /* *INDENT-OFF* */
701   REPLY_MACRO2(VL_API_BRIDGE_FLAGS_REPLY,
702   ({
703     rmp->resulting_feature_bitmap = ntohl(bitmap);
704   }));
705   /* *INDENT-ON* */
706 }
707
708 static void
709   vl_api_l2_interface_vlan_tag_rewrite_t_handler
710   (vl_api_l2_interface_vlan_tag_rewrite_t * mp)
711 {
712   int rv = 0;
713   vl_api_l2_interface_vlan_tag_rewrite_reply_t *rmp;
714   vnet_main_t *vnm = vnet_get_main ();
715   vlib_main_t *vm = vlib_get_main ();
716   u32 vtr_op;
717
718   VALIDATE_SW_IF_INDEX (mp);
719
720   vtr_op = ntohl (mp->vtr_op);
721
722   /* The L2 code is unsuspicious */
723   switch (vtr_op)
724     {
725     case L2_VTR_DISABLED:
726     case L2_VTR_PUSH_1:
727     case L2_VTR_PUSH_2:
728     case L2_VTR_POP_1:
729     case L2_VTR_POP_2:
730     case L2_VTR_TRANSLATE_1_1:
731     case L2_VTR_TRANSLATE_1_2:
732     case L2_VTR_TRANSLATE_2_1:
733     case L2_VTR_TRANSLATE_2_2:
734       break;
735
736     default:
737       rv = VNET_API_ERROR_INVALID_VALUE;
738       goto bad_sw_if_index;
739     }
740
741   rv = l2vtr_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
742                         ntohl (mp->push_dot1q), ntohl (mp->tag1),
743                         ntohl (mp->tag2));
744
745   BAD_SW_IF_INDEX_LABEL;
746
747   REPLY_MACRO (VL_API_L2_INTERFACE_VLAN_TAG_REWRITE_REPLY);
748 }
749
750 static void
751   vl_api_l2_interface_pbb_tag_rewrite_t_handler
752   (vl_api_l2_interface_pbb_tag_rewrite_t * mp)
753 {
754   vl_api_l2_interface_pbb_tag_rewrite_reply_t *rmp;
755   vnet_main_t *vnm = vnet_get_main ();
756   vlib_main_t *vm = vlib_get_main ();
757   u32 vtr_op;
758   int rv = 0;
759   mac_address_t b_dmac, b_smac;
760
761   VALIDATE_SW_IF_INDEX (mp);
762
763   vtr_op = ntohl (mp->vtr_op);
764
765   switch (vtr_op)
766     {
767     case L2_VTR_DISABLED:
768     case L2_VTR_PUSH_2:
769     case L2_VTR_POP_2:
770     case L2_VTR_TRANSLATE_2_1:
771       break;
772
773     default:
774       rv = VNET_API_ERROR_INVALID_VALUE;
775       goto bad_sw_if_index;
776     }
777
778   mac_address_decode (mp->b_dmac, &b_dmac);
779   mac_address_decode (mp->b_smac, &b_smac);
780
781   rv = l2pbb_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
782                         b_dmac.bytes, b_smac.bytes, ntohs (mp->b_vlanid),
783                         ntohl (mp->i_sid), ntohs (mp->outer_tag));
784
785   BAD_SW_IF_INDEX_LABEL;
786
787   REPLY_MACRO (VL_API_L2_INTERFACE_PBB_TAG_REWRITE_REPLY);
788 }
789
790 static void
791   vl_api_sw_interface_set_l2_xconnect_t_handler
792   (vl_api_sw_interface_set_l2_xconnect_t * mp)
793 {
794   vl_api_sw_interface_set_l2_xconnect_reply_t *rmp;
795   int rv = 0;
796   u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
797   u32 tx_sw_if_index = ntohl (mp->tx_sw_if_index);
798   vlib_main_t *vm = vlib_get_main ();
799   vnet_main_t *vnm = vnet_get_main ();
800
801   VALIDATE_RX_SW_IF_INDEX (mp);
802
803   if (mp->enable)
804     {
805       VALIDATE_TX_SW_IF_INDEX (mp);
806       rv = set_int_l2_mode (vm, vnm, MODE_L2_XC,
807                             rx_sw_if_index, 0,
808                             L2_BD_PORT_TYPE_NORMAL, 0, tx_sw_if_index);
809     }
810   else
811     {
812       rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0,
813                             L2_BD_PORT_TYPE_NORMAL, 0, 0);
814     }
815
816   switch (rv)
817     {
818     case MODE_ERROR_ETH:
819       rv = VNET_API_ERROR_NON_ETHERNET;
820       break;
821     case MODE_ERROR_BVI_DEF:
822       rv = VNET_API_ERROR_BD_ALREADY_HAS_BVI;
823       break;
824     }
825
826   BAD_RX_SW_IF_INDEX_LABEL;
827   BAD_TX_SW_IF_INDEX_LABEL;
828
829   REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_XCONNECT_REPLY);
830 }
831
832 static int
833 l2_bd_port_type_decode (vl_api_l2_port_type_t v, l2_bd_port_type_t * l)
834 {
835   v = clib_net_to_host_u32 (v);
836
837   switch (v)
838     {
839     case L2_API_PORT_TYPE_NORMAL:
840       *l = L2_BD_PORT_TYPE_NORMAL;
841       return 0;
842     case L2_API_PORT_TYPE_BVI:
843       *l = L2_BD_PORT_TYPE_BVI;
844       return 0;
845     case L2_API_PORT_TYPE_UU_FWD:
846       *l = L2_BD_PORT_TYPE_UU_FWD;
847       return 0;
848     }
849
850   return (VNET_API_ERROR_INVALID_VALUE);
851 }
852
853 static void
854   vl_api_sw_interface_set_l2_bridge_t_handler
855   (vl_api_sw_interface_set_l2_bridge_t * mp)
856 {
857   bd_main_t *bdm = &bd_main;
858   vl_api_sw_interface_set_l2_bridge_reply_t *rmp;
859   int rv = 0;
860   vlib_main_t *vm = vlib_get_main ();
861   vnet_main_t *vnm = vnet_get_main ();
862   l2_bd_port_type_t pt;
863
864   VALIDATE_RX_SW_IF_INDEX (mp);
865   u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
866   rv = l2_bd_port_type_decode (mp->port_type, &pt);
867
868   if (0 != rv)
869     goto out;
870   if (mp->enable)
871     {
872       VALIDATE_BD_ID (mp);
873       u32 bd_id = ntohl (mp->bd_id);
874       u32 bd_index = bd_find_or_add_bd_index (bdm, bd_id);
875
876       rv = set_int_l2_mode (vm, vnm, MODE_L2_BRIDGE,
877                             rx_sw_if_index, bd_index, pt, mp->shg, 0);
878     }
879   else
880     {
881       rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0, pt, 0, 0);
882     }
883
884   switch (rv)
885     {
886     case MODE_ERROR_ETH:
887       rv = VNET_API_ERROR_NON_ETHERNET;
888       break;
889     case MODE_ERROR_BVI_DEF:
890       rv = VNET_API_ERROR_BD_ALREADY_HAS_BVI;
891       break;
892     }
893
894   BAD_RX_SW_IF_INDEX_LABEL;
895   BAD_BD_ID_LABEL;
896 out:
897   REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_BRIDGE_REPLY);
898 }
899
900 static void
901 send_bd_ip_mac_entry (vpe_api_main_t * am,
902                       vl_api_registration_t * reg,
903                       u32 bd_id,
904                       const ip46_address_t * ip,
905                       ip46_type_t itype,
906                       const mac_address_t * mac, u32 context)
907 {
908   vl_api_bd_ip_mac_details_t *mp;
909
910   mp = vl_msg_api_alloc (sizeof (*mp));
911   clib_memset (mp, 0, sizeof (*mp));
912   mp->_vl_msg_id = ntohs (VL_API_BD_IP_MAC_DETAILS);
913
914   mp->context = context;
915   mp->entry.bd_id = ntohl (bd_id);
916
917   ip_address_encode (ip, itype, &mp->entry.ip);
918   mac_address_encode (mac, mp->entry.mac);
919
920   vl_api_send_msg (reg, (u8 *) mp);
921 }
922
923 static void
924 vl_api_bd_ip_mac_dump_t_handler (vl_api_bd_ip_mac_dump_t * mp)
925 {
926   vpe_api_main_t *am = &vpe_api_main;
927   bd_main_t *bdm = &bd_main;
928   l2_bridge_domain_t *bd_config;
929   u32 bd_id = ntohl (mp->bd_id);
930   u32 bd_index, start, end;
931   vl_api_registration_t *reg;
932   uword *p;
933
934   reg = vl_api_client_index_to_registration (mp->client_index);
935   if (!reg)
936     return;
937
938   /* see bd_id: ~0 means "any" */
939   if (bd_id == ~0)
940     {
941       start = 1;
942       end = vec_len (l2input_main.bd_configs);
943     }
944   else
945     {
946       p = hash_get (bdm->bd_index_by_bd_id, bd_id);
947       if (p == 0)
948         return;
949
950       bd_index = p[0];
951       vec_validate (l2input_main.bd_configs, bd_index);
952       start = bd_index;
953       end = start + 1;
954     }
955
956   for (bd_index = start; bd_index < end; bd_index++)
957     {
958       bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
959       if (bd_is_valid (bd_config))
960         {
961           ip4_address_t ip4_addr;
962           ip6_address_t *ip6_addr;
963           mac_address_t mac;
964           u64 mac64;
965           bd_id = bd_config->bd_id;
966
967          /* *INDENT-OFF* */
968          hash_foreach (ip4_addr.as_u32, mac64, bd_config->mac_by_ip4,
969          ({
970            ip46_address_t ip = {
971              .ip4 = ip4_addr,
972            };
973            mac_address_from_u64(&mac, mac64);
974
975            send_bd_ip_mac_entry (am, reg, bd_id, &ip, IP46_TYPE_IP4,
976                                  &mac, mp->context);
977          }));
978
979          hash_foreach_mem (ip6_addr, mac64, bd_config->mac_by_ip6,
980          ({
981            ip46_address_t ip = {
982              .ip6 = *ip6_addr,
983            };
984            mac_address_from_u64(&mac, mac64);
985
986            send_bd_ip_mac_entry (am, reg, bd_id, &ip, IP46_TYPE_IP6,
987                                  &mac, mp->context);
988          }));
989          /* *INDENT-ON* */
990         }
991     }
992 }
993
994 static void
995 vl_api_bd_ip_mac_add_del_t_handler (vl_api_bd_ip_mac_add_del_t * mp)
996 {
997   ip46_address_t ip_addr = ip46_address_initializer;
998   vl_api_bd_ip_mac_add_del_reply_t *rmp;
999   bd_main_t *bdm = &bd_main;
1000   u32 bd_index, bd_id;
1001   mac_address_t mac;
1002   ip46_type_t type;
1003   int rv = 0;
1004   uword *p;
1005
1006   bd_id = ntohl (mp->entry.bd_id);
1007
1008   if (bd_id == 0)
1009     {
1010       rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
1011       goto out;
1012     }
1013
1014   p = hash_get (bdm->bd_index_by_bd_id, bd_id);
1015   if (p == 0)
1016     {
1017       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1018       goto out;
1019     }
1020   bd_index = p[0];
1021
1022   type = ip_address_decode (&mp->entry.ip, &ip_addr);
1023   mac_address_decode (mp->entry.mac, &mac);
1024
1025   if (bd_add_del_ip_mac (bd_index, type, &ip_addr, &mac, mp->is_add))
1026     rv = VNET_API_ERROR_UNSPECIFIED;
1027
1028 out:
1029   REPLY_MACRO (VL_API_BD_IP_MAC_ADD_DEL_REPLY);
1030 }
1031
1032 static void
1033 vl_api_bd_ip_mac_flush_t_handler (vl_api_bd_ip_mac_flush_t * mp)
1034 {
1035   vl_api_bd_ip_mac_flush_reply_t *rmp;
1036   bd_main_t *bdm = &bd_main;
1037   u32 bd_index, bd_id;
1038   int rv = 0;
1039   uword *p;
1040
1041   bd_id = ntohl (mp->bd_id);
1042
1043   if (bd_id == 0)
1044     {
1045       rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
1046       goto out;
1047     }
1048
1049   p = hash_get (bdm->bd_index_by_bd_id, bd_id);
1050   if (p == 0)
1051     {
1052       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1053       goto out;
1054     }
1055   bd_index = p[0];
1056
1057   bd_flush_ip_mac (bd_index);
1058
1059 out:
1060   REPLY_MACRO (VL_API_BD_IP_MAC_FLUSH_REPLY);
1061 }
1062
1063 extern void l2_efp_filter_configure (vnet_main_t * vnet_main,
1064                                      u32 sw_if_index, u8 enable);
1065
1066 static void
1067 vl_api_l2_interface_efp_filter_t_handler (vl_api_l2_interface_efp_filter_t *
1068                                           mp)
1069 {
1070   int rv;
1071   vl_api_l2_interface_efp_filter_reply_t *rmp;
1072   vnet_main_t *vnm = vnet_get_main ();
1073
1074   VALIDATE_SW_IF_INDEX (mp);
1075
1076   // enable/disable the feature
1077   l2_efp_filter_configure (vnm, ntohl (mp->sw_if_index), mp->enable_disable);
1078   rv = vnm->api_errno;
1079
1080   BAD_SW_IF_INDEX_LABEL;
1081   REPLY_MACRO (VL_API_L2_INTERFACE_EFP_FILTER_REPLY);
1082 }
1083
1084 static void
1085 vl_api_l2_patch_add_del_t_handler (vl_api_l2_patch_add_del_t * mp)
1086 {
1087   extern int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
1088                                     int is_add);
1089   vl_api_l2_patch_add_del_reply_t *rmp;
1090   int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
1091                              int is_add);
1092   int rv = 0;
1093
1094   VALIDATE_RX_SW_IF_INDEX (mp);
1095   VALIDATE_TX_SW_IF_INDEX (mp);
1096
1097   rv = vnet_l2_patch_add_del (ntohl (mp->rx_sw_if_index),
1098                               ntohl (mp->tx_sw_if_index),
1099                               (int) (mp->is_add != 0));
1100
1101   BAD_RX_SW_IF_INDEX_LABEL;
1102   BAD_TX_SW_IF_INDEX_LABEL;
1103
1104   REPLY_MACRO (VL_API_L2_PATCH_ADD_DEL_REPLY);
1105 }
1106
1107 static void
1108 vl_api_sw_interface_set_vpath_t_handler (vl_api_sw_interface_set_vpath_t * mp)
1109 {
1110   vl_api_sw_interface_set_vpath_reply_t *rmp;
1111   int rv = 0;
1112   u32 sw_if_index = ntohl (mp->sw_if_index);
1113
1114   VALIDATE_SW_IF_INDEX (mp);
1115
1116   l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_VPATH, mp->enable);
1117   vnet_feature_enable_disable ("ip4-unicast", "vpath-input-ip4",
1118                                sw_if_index, mp->enable, 0, 0);
1119   vnet_feature_enable_disable ("ip4-multicast", "vpath-input-ip4",
1120                                sw_if_index, mp->enable, 0, 0);
1121   vnet_feature_enable_disable ("ip6-unicast", "vpath-input-ip6",
1122                                sw_if_index, mp->enable, 0, 0);
1123   vnet_feature_enable_disable ("ip6-multicast", "vpath-input-ip6",
1124                                sw_if_index, mp->enable, 0, 0);
1125
1126   BAD_SW_IF_INDEX_LABEL;
1127
1128   REPLY_MACRO (VL_API_SW_INTERFACE_SET_VPATH_REPLY);
1129 }
1130
1131 static void
1132 vl_api_bvi_create_t_handler (vl_api_bvi_create_t * mp)
1133 {
1134   vl_api_bvi_create_reply_t *rmp;
1135   mac_address_t mac;
1136   u32 sw_if_index;
1137   int rv;
1138
1139   mac_address_decode (mp->mac, &mac);
1140
1141   rv = l2_bvi_create (ntohl (mp->user_instance), &mac, &sw_if_index);
1142
1143   /* *INDENT-OFF* */
1144   REPLY_MACRO2(VL_API_BVI_CREATE_REPLY,
1145   ({
1146     rmp->sw_if_index = ntohl (sw_if_index);
1147   }));
1148   /* *INDENT-ON* */
1149 }
1150
1151 static void
1152 vl_api_bvi_delete_t_handler (vl_api_bvi_delete_t * mp)
1153 {
1154   vl_api_bvi_delete_reply_t *rmp;
1155   int rv;
1156
1157   rv = l2_bvi_delete (ntohl (mp->sw_if_index));
1158
1159   REPLY_MACRO (VL_API_BVI_DELETE_REPLY);
1160 }
1161
1162 static bool
1163 l2_arp_term_publish_event_is_equal (const l2_arp_term_publish_event_t * e1,
1164                                     const l2_arp_term_publish_event_t * e2)
1165 {
1166   if (e1 == NULL || e2 == NULL)
1167     return false;
1168   return (ip46_address_is_equal (&e1->ip, &e2->ip) &&
1169           (e1->sw_if_index == e2->sw_if_index) &&
1170           (mac_address_equal (&e1->mac, &e2->mac)));
1171 }
1172
1173 static uword
1174 l2_arp_term_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1175                      vlib_frame_t * f)
1176 {
1177   /* These cross the longjmp boundary (vlib_process_wait_for_event)
1178    * and need to be volatile - to prevent them from being optimized into
1179    * a register - which could change during suspension */
1180   volatile f64 last = vlib_time_now (vm);
1181   volatile l2_arp_term_publish_event_t last_event = { };
1182
1183   l2_arp_term_main_t *l2am = &l2_arp_term_main;
1184
1185   while (1)
1186     {
1187       uword event_type = L2_ARP_TERM_EVENT_PUBLISH;
1188       vpe_client_registration_t *reg;
1189       f64 now;
1190
1191       vlib_process_wait_for_event (vm);
1192
1193       vlib_process_get_event_data (vm, &event_type);
1194       now = vlib_time_now (vm);
1195
1196       if (event_type == L2_ARP_TERM_EVENT_PUBLISH)
1197         {
1198           l2_arp_term_publish_event_t *event;
1199
1200           vec_foreach (event, l2am->publish_events)
1201           {
1202             /* dampen duplicate events - cast away volatile */
1203             if (l2_arp_term_publish_event_is_equal
1204                 (event, (l2_arp_term_publish_event_t *) & last_event) &&
1205                 (now - last) < 10.0)
1206               {
1207                 continue;
1208               }
1209             last_event = *event;
1210             last = now;
1211
1212             /* *INDENT-OFF* */
1213             pool_foreach (reg, vpe_api_main.l2_arp_term_events_registrations)
1214              {
1215               vl_api_registration_t *vl_reg;
1216               vl_reg = vl_api_client_index_to_registration (reg->client_index);
1217               ALWAYS_ASSERT (vl_reg != NULL);
1218
1219               if (reg && vl_api_can_send_msg (vl_reg))
1220                 {
1221                   vl_api_l2_arp_term_event_t * vevent;
1222                   vevent = vl_msg_api_alloc (sizeof *vevent);
1223                   clib_memset (vevent, 0, sizeof *vevent);
1224                   vevent->_vl_msg_id = htons (VL_API_L2_ARP_TERM_EVENT);
1225                   vevent->client_index = reg->client_index;
1226                   vevent->pid = reg->client_pid;
1227                   ip_address_encode(&event->ip,
1228                                     event->type,
1229                                     &vevent->ip);
1230                   vevent->sw_if_index = htonl(event->sw_if_index);
1231                   mac_address_encode(&event->mac, vevent->mac);
1232                   vl_api_send_msg (vl_reg, (u8 *) vevent);
1233                 }
1234             }
1235             /* *INDENT-ON* */
1236           }
1237           vec_reset_length (l2am->publish_events);
1238         }
1239     }
1240
1241   return 0;
1242 }
1243
1244 /* *INDENT-OFF* */
1245 VLIB_REGISTER_NODE (l2_arp_term_process_node) = {
1246   .function = l2_arp_term_process,
1247   .type = VLIB_NODE_TYPE_PROCESS,
1248   .name = "l2-arp-term-publisher",
1249 };
1250 /* *INDENT-ON* */
1251
1252 static void
1253 vl_api_want_l2_arp_term_events_t_handler (vl_api_want_l2_arp_term_events_t *
1254                                           mp)
1255 {
1256   vl_api_want_l2_arp_term_events_reply_t *rmp;
1257   vpe_api_main_t *am = &vpe_api_main;
1258   vpe_client_registration_t *rp;
1259   int rv = 0;
1260   uword *p;
1261
1262   p = hash_get (am->l2_arp_term_events_registration_hash, mp->client_index);
1263
1264   if (p)
1265     {
1266       if (mp->enable)
1267         {
1268           clib_warning ("pid %d: already enabled...", mp->pid);
1269           rv = VNET_API_ERROR_INVALID_REGISTRATION;
1270           goto reply;
1271         }
1272       else
1273         {
1274           rp = pool_elt_at_index (am->l2_arp_term_events_registrations, p[0]);
1275           pool_put (am->l2_arp_term_events_registrations, rp);
1276           hash_unset (am->l2_arp_term_events_registration_hash,
1277                       mp->client_index);
1278           if (pool_elts (am->l2_arp_term_events_registrations) == 0)
1279             l2_arp_term_set_publisher_node (false);
1280           goto reply;
1281         }
1282     }
1283   if (mp->enable == 0)
1284     {
1285       clib_warning ("pid %d: already disabled...", mp->pid);
1286       rv = VNET_API_ERROR_INVALID_REGISTRATION;
1287       goto reply;
1288     }
1289   pool_get (am->l2_arp_term_events_registrations, rp);
1290   rp->client_index = mp->client_index;
1291   rp->client_pid = mp->pid;
1292   hash_set (am->l2_arp_term_events_registration_hash, rp->client_index,
1293             rp - am->l2_arp_term_events_registrations);
1294   l2_arp_term_set_publisher_node (true);
1295
1296 reply:
1297   REPLY_MACRO (VL_API_WANT_L2_ARP_TERM_EVENTS_REPLY);
1298 }
1299
1300 static clib_error_t *
1301 want_l2_arp_term_events_reaper (u32 client_index)
1302 {
1303   vpe_client_registration_t *rp;
1304   vpe_api_main_t *am;
1305   uword *p;
1306
1307   am = &vpe_api_main;
1308
1309   /* remove from the registration hash */
1310   p = hash_get (am->l2_arp_term_events_registration_hash, client_index);
1311
1312   if (p)
1313     {
1314       rp = pool_elt_at_index (am->l2_arp_term_events_registrations, p[0]);
1315       pool_put (am->l2_arp_term_events_registrations, rp);
1316       hash_unset (am->l2_arp_term_events_registration_hash, client_index);
1317       if (pool_elts (am->l2_arp_term_events_registrations) == 0)
1318         l2_arp_term_set_publisher_node (false);
1319     }
1320   return (NULL);
1321 }
1322
1323 VL_MSG_API_REAPER_FUNCTION (want_l2_arp_term_events_reaper);
1324
1325 /*
1326  * l2_api_hookup
1327  * Add vpe's API message handlers to the table.
1328  * vlib has already mapped shared memory and
1329  * added the client registration handlers.
1330  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
1331  */
1332 #define vl_msg_name_crc_list
1333 #include <vnet/vnet_all_api_h.h>
1334 #undef vl_msg_name_crc_list
1335
1336 static void
1337 setup_message_id_table (api_main_t * am)
1338 {
1339 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1340   foreach_vl_msg_name_crc_l2;
1341 #undef _
1342 }
1343
1344 static clib_error_t *
1345 l2_api_hookup (vlib_main_t * vm)
1346 {
1347   api_main_t *am = vlibapi_get_main ();
1348
1349 #define _(N,n)                                                  \
1350     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
1351                            vl_api_##n##_t_handler,              \
1352                            vl_noop_handler,                     \
1353                            vl_api_##n##_t_endian,               \
1354                            vl_api_##n##_t_print,                \
1355                            sizeof(vl_api_##n##_t), 1);
1356   foreach_vpe_api_msg;
1357 #undef _
1358
1359   /* Mark VL_API_BRIDGE_DOMAIN_DUMP as mp safe */
1360   am->is_mp_safe[VL_API_BRIDGE_DOMAIN_DUMP] = 1;
1361
1362   /*
1363    * Set up the (msg_name, crc, message-id) table
1364    */
1365   setup_message_id_table (am);
1366
1367   return 0;
1368 }
1369
1370 VLIB_API_INIT_FUNCTION (l2_api_hookup);
1371
1372 /*
1373  * fd.io coding-style-patch-verification: ON
1374  *
1375  * Local Variables:
1376  * eval: (c-set-style "gnu")
1377  * End:
1378  */