FD.io VPP  v18.01-8-g0eacf49
Vector Packet Processing
span_api.c
Go to the documentation of this file.
1 /*
2  *------------------------------------------------------------------
3  * span_api.c - span mirroring 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/span/span.h>
26 
27 #include <vnet/vnet_msg_enum.h>
28 
29 #define vl_typedefs /* define message structures */
30 #include <vnet/vnet_all_api_h.h>
31 #undef vl_typedefs
32 
33 #define vl_endianfun /* define message structures */
34 #include <vnet/vnet_all_api_h.h>
35 #undef vl_endianfun
36 
37 /* instantiate all the print functions we know about */
38 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
39 #define vl_printfun
40 #include <vnet/vnet_all_api_h.h>
41 #undef vl_printfun
42 
44 
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) \
48 
49 static void
52 {
53  vl_api_sw_interface_span_enable_disable_reply_t *rmp;
54  int rv;
55 
57 
58  rv = span_add_delete_entry (vm, ntohl (mp->sw_if_index_from),
59  ntohl (mp->sw_if_index_to), mp->state,
61 
62  REPLY_MACRO (VL_API_SW_INTERFACE_SPAN_ENABLE_DISABLE_REPLY);
63 }
64 
65 static void
67 {
68 
70  span_interface_t *si;
72  span_main_t *sm = &span_main;
73 
75  if (!q)
76  return;
77 
79  /* *INDENT-OFF* */
80  vec_foreach (si, sm->interfaces)
81  {
82  span_mirror_t * rxm = &si->mirror_rxtx[sf][VLIB_RX];
83  span_mirror_t * txm = &si->mirror_rxtx[sf][VLIB_TX];
84  if (rxm->num_mirror_ports || txm->num_mirror_ports)
85  {
86  clib_bitmap_t *b;
87  u32 i;
89  clib_bitmap_foreach (i, b, (
90  {
91  rmp = vl_msg_api_alloc (sizeof (*rmp));
92  memset (rmp, 0, sizeof (*rmp));
93  rmp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_SPAN_DETAILS);
94  rmp->context = mp->context;
95 
96  rmp->sw_if_index_from = htonl (si - sm->interfaces);
97  rmp->sw_if_index_to = htonl (i);
98  rmp->state = (u8) (clib_bitmap_get (rxm->mirror_ports, i) +
99  clib_bitmap_get (txm->mirror_ports, i) * 2);
100 
101  vl_msg_api_send_shmem (q, (u8 *) & rmp);
102  }));
103  clib_bitmap_free (b);
104  }
105  }
106  /* *INDENT-ON* */
107 }
108 
109 /*
110  * vpe_api_hookup
111  * Add vpe's API message handlers to the table.
112  * vlib has alread mapped shared memory and
113  * added the client registration handlers.
114  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
115  */
116 #define vl_msg_name_crc_list
117 #include <vnet/vnet_all_api_h.h>
118 #undef vl_msg_name_crc_list
119 
120 static void
122 {
123 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
124  foreach_vl_msg_name_crc_span;
125 #undef _
126 }
127 
128 static clib_error_t *
130 {
131  api_main_t *am = &api_main;
132 
133 #define _(N,n) \
134  vl_msg_api_set_handlers(VL_API_##N, #n, \
135  vl_api_##n##_t_handler, \
136  vl_noop_handler, \
137  vl_api_##n##_t_endian, \
138  vl_api_##n##_t_print, \
139  sizeof(vl_api_##n##_t), 1);
141 #undef _
142 
143  /*
144  * Set up the (msg_name, crc, message-id) table
145  */
147 
148  return 0;
149 }
150 
152 
153 /*
154  * fd.io coding-style-patch-verification: ON
155  *
156  * Local Variables:
157  * eval: (c-set-style "gnu")
158  * End:
159  */
span_main_t span_main
Definition: span.c:24
static void setup_message_id_table(api_main_t *am)
Definition: span_api.c:121
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:337
VLIB_API_INIT_FUNCTION(span_api_hookup)
span_mirror_t mirror_rxtx[SPAN_FEAT_N][VLIB_N_RX_TX]
Definition: span.h:38
#define foreach_vpe_api_msg
Definition: span_api.c:45
static clib_error_t * span_api_hookup(vlib_main_t *vm)
Definition: span_api.c:129
static void vl_api_sw_interface_span_dump_t_handler(vl_api_sw_interface_span_dump_t *mp)
Definition: span_api.c:66
u32 num_mirror_ports
Definition: span.h:33
clib_bitmap_t * mirror_ports
Definition: span.h:32
void * vl_msg_api_alloc(int nbytes)
#define clib_bitmap_foreach(i, ai, body)
Macro to iterate across set bits in a bitmap.
Definition: bitmap.h:361
Enable/Disable span to mirror traffic from one interface to another.
Definition: span.api:27
span_interface_t * interfaces
Definition: span.h:48
#define REPLY_MACRO(t)
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:198
static void vl_api_sw_interface_span_enable_disable_t_handler(vl_api_sw_interface_span_enable_disable_t *mp)
Definition: span_api.c:51
api_main_t api_main
Definition: api_shared.c:35
int span_add_delete_entry(vlib_main_t *vm, u32 src_sw_if_index, u32 dst_sw_if_index, u8 state, span_feat_t sf)
Definition: span.c:52
vlib_main_t * vm
Definition: buffer.c:283
static uword * clib_bitmap_dup_or(uword *ai, uword *bi)
Logical operator across two bitmaps which duplicates the first bitmap.
SPAN dump request.
Definition: span.api:41
unix_shared_memory_queue_t * vl_api_client_index_to_input_queue(u32 index)
static uword clib_bitmap_get(uword *ai, uword i)
Gets the ith bit value from a bitmap.
Definition: bitmap.h:197
void vl_msg_api_send_shmem(unix_shared_memory_queue_t *q, u8 *elem)
unsigned int u32
Definition: types.h:88
#define clib_bitmap_free(v)
Free a bitmap.
Definition: bitmap.h:92
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
Definition: defs.h:47
unsigned char u8
Definition: types.h:56
span_feat_t
Definition: span.h:23
uword clib_bitmap_t
Definition: bitmap.h:50
#define vec_foreach(var, vec)
Vector iterator.
Definition: defs.h:46
struct _unix_shared_memory_queue unix_shared_memory_queue_t
Reply to SPAN dump request.
Definition: span.api:53