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