2 *------------------------------------------------------------------
3 * span_api.c - span mirroring api
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:
10 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
20 #include <vnet/vnet.h>
21 #include <vlibmemory/api.h>
23 #include <vnet/interface.h>
24 #include <vnet/api_errno.h>
25 #include <vnet/span/span.h>
27 #include <vnet/vnet_msg_enum.h>
29 #define vl_typedefs /* define message structures */
30 #include <vnet/vnet_all_api_h.h>
33 #define vl_endianfun /* define message structures */
34 #include <vnet/vnet_all_api_h.h>
37 /* instantiate all the print functions we know about */
38 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
40 #include <vnet/vnet_all_api_h.h>
43 #include <vlibapi/api_helper_macros.h>
45 #define foreach_vpe_api_msg \
46 _(SW_INTERFACE_SPAN_ENABLE_DISABLE, sw_interface_span_enable_disable) \
47 _(SW_INTERFACE_SPAN_DUMP, sw_interface_span_dump) \
50 vl_api_sw_interface_span_enable_disable_t_handler
51 (vl_api_sw_interface_span_enable_disable_t * mp)
53 vl_api_sw_interface_span_enable_disable_reply_t *rmp;
56 vlib_main_t *vm = vlib_get_main ();
58 rv = span_add_delete_entry (vm, ntohl (mp->sw_if_index_from),
59 ntohl (mp->sw_if_index_to), mp->state,
60 mp->is_l2 ? SPAN_FEAT_L2 : SPAN_FEAT_DEVICE);
62 REPLY_MACRO (VL_API_SW_INTERFACE_SPAN_ENABLE_DISABLE_REPLY);
66 vl_api_sw_interface_span_dump_t_handler (vl_api_sw_interface_span_dump_t * mp)
69 unix_shared_memory_queue_t *q;
71 vl_api_sw_interface_span_details_t *rmp;
72 span_main_t *sm = &span_main;
74 q = vl_api_client_index_to_input_queue (mp->client_index);
79 vec_foreach (si, sm->interfaces)
81 span_mirror_t * drxm = &si->mirror_rxtx[SPAN_FEAT_DEVICE][VLIB_RX];
82 span_mirror_t * dtxm = &si->mirror_rxtx[SPAN_FEAT_DEVICE][VLIB_TX];
83 if (drxm->num_mirror_ports || dtxm->num_mirror_ports)
87 b = clib_bitmap_dup_or (drxm->mirror_ports, dtxm->mirror_ports);
88 clib_bitmap_foreach (i, b, (
90 rmp = vl_msg_api_alloc (sizeof (*rmp));
91 memset (rmp, 0, sizeof (*rmp));
92 rmp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_SPAN_DETAILS);
93 rmp->context = mp->context;
95 rmp->sw_if_index_from = htonl (si - sm->interfaces);
96 rmp->sw_if_index_to = htonl (i);
97 rmp->state = (u8) (clib_bitmap_get (drxm->mirror_ports, i) +
98 clib_bitmap_get (dtxm->mirror_ports, i) * 2);
100 vl_msg_api_send_shmem (q, (u8 *) & rmp);
102 clib_bitmap_free (b);
110 * Add vpe's API message handlers to the table.
111 * vlib has alread mapped shared memory and
112 * added the client registration handlers.
113 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
115 #define vl_msg_name_crc_list
116 #include <vnet/vnet_all_api_h.h>
117 #undef vl_msg_name_crc_list
120 setup_message_id_table (api_main_t * am)
122 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
123 foreach_vl_msg_name_crc_span;
127 static clib_error_t *
128 span_api_hookup (vlib_main_t * vm)
130 api_main_t *am = &api_main;
133 vl_msg_api_set_handlers(VL_API_##N, #n, \
134 vl_api_##n##_t_handler, \
136 vl_api_##n##_t_endian, \
137 vl_api_##n##_t_print, \
138 sizeof(vl_api_##n##_t), 1);
143 * Set up the (msg_name, crc, message-id) table
145 setup_message_id_table (am);
150 VLIB_API_INIT_FUNCTION (span_api_hookup);
153 * fd.io coding-style-patch-verification: ON
156 * eval: (c-set-style "gnu")