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