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