Break up vpe.api
[vpp.git] / src / vnet / pg / pg_api.c
1 /*
2  *------------------------------------------------------------------
3  * pg_api.c - vnet pg 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/pg/pg.h>
24
25 #include <vnet/vnet_msg_enum.h>
26
27 #define vl_typedefs             /* define message structures */
28 #include <vnet/vnet_all_api_h.h>
29 #undef vl_typedefs
30
31 #define vl_endianfun            /* define message structures */
32 #include <vnet/vnet_all_api_h.h>
33 #undef vl_endianfun
34
35 /* instantiate all the print functions we know about */
36 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
37 #define vl_printfun
38 #include <vnet/vnet_all_api_h.h>
39 #undef vl_printfun
40
41 #include <vlibapi/api_helper_macros.h>
42
43
44 #define foreach_pg_api_msg                                              \
45 _(PG_CREATE_INTERFACE, pg_create_interface)                             \
46 _(PG_CAPTURE, pg_capture)                                               \
47 _(PG_ENABLE_DISABLE, pg_enable_disable)
48
49 extern void stats_dslock_with_hint (int hint, int tag);
50 extern void stats_dsunlock (void);
51
52 static void
53 vl_api_pg_create_interface_t_handler (vl_api_pg_create_interface_t * mp)
54 {
55   vl_api_pg_create_interface_reply_t *rmp;
56   int rv = 0;
57
58   pg_main_t *pg = &pg_main;
59   u32 pg_if_id = pg_interface_add_or_get (pg, ntohl (mp->interface_id));
60   pg_interface_t *pi = pool_elt_at_index (pg->interfaces, pg_if_id);
61
62   /* *INDENT-OFF* */
63   REPLY_MACRO2(VL_API_PG_CREATE_INTERFACE_REPLY,
64   ({
65     rmp->sw_if_index = ntohl(pi->sw_if_index);
66   }));
67   /* *INDENT-ON* */
68 }
69
70 static void
71 vl_api_pg_capture_t_handler (vl_api_pg_capture_t * mp)
72 {
73   vl_api_pg_capture_reply_t *rmp;
74   int rv = 0;
75
76   vnet_main_t *vnm = vnet_get_main ();
77   vnet_interface_main_t *im = &vnm->interface_main;
78   vnet_hw_interface_t *hi = 0;
79
80   u8 *intf_name = format (0, "pg%d", ntohl (mp->interface_id), 0);
81   vec_terminate_c_string (intf_name);
82   u32 hw_if_index = ~0;
83   uword *p = hash_get_mem (im->hw_interface_by_name, intf_name);
84   if (p)
85     hw_if_index = *p;
86   vec_free (intf_name);
87
88   if (hw_if_index != ~0)
89     {
90       pg_capture_args_t _a, *a = &_a;
91
92       u32 len = ntohl (mp->pcap_name_length);
93       u8 *pcap_file_name = vec_new (u8, len);
94       clib_memcpy (pcap_file_name, mp->pcap_file_name, len);
95
96       hi = vnet_get_sup_hw_interface (vnm, hw_if_index);
97       a->hw_if_index = hw_if_index;
98       a->dev_instance = hi->dev_instance;
99       a->is_enabled = mp->is_enabled;
100       a->pcap_file_name = pcap_file_name;
101       a->count = ntohl (mp->count);
102
103       clib_error_t *e = pg_capture (a);
104       if (e)
105         {
106           clib_error_report (e);
107           rv = VNET_API_ERROR_CANNOT_CREATE_PCAP_FILE;
108         }
109
110       vec_free (pcap_file_name);
111     }
112   REPLY_MACRO (VL_API_PG_CAPTURE_REPLY);
113 }
114
115 static void
116 vl_api_pg_enable_disable_t_handler (vl_api_pg_enable_disable_t * mp)
117 {
118   vl_api_pg_enable_disable_reply_t *rmp;
119   int rv = 0;
120
121   pg_main_t *pg = &pg_main;
122   u32 stream_index = ~0;
123
124   int is_enable = mp->is_enabled != 0;
125   u32 len = ntohl (mp->stream_name_length) - 1;
126
127   if (len > 0)
128     {
129       u8 *stream_name = vec_new (u8, len);
130       clib_memcpy (stream_name, mp->stream_name, len);
131       uword *p = hash_get_mem (pg->stream_index_by_name, stream_name);
132       if (p)
133         stream_index = *p;
134       vec_free (stream_name);
135     }
136
137   pg_enable_disable (stream_index, is_enable);
138
139   REPLY_MACRO (VL_API_PG_ENABLE_DISABLE_REPLY);
140 }
141
142 #define vl_msg_name_crc_list
143 #include <vnet/pg/pg.api.h>
144 #undef vl_msg_name_crc_list
145
146 static void
147 setup_message_id_table (api_main_t * am)
148 {
149 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
150   foreach_vl_msg_name_crc_pg;
151 #undef _
152 }
153
154 static clib_error_t *
155 pg_api_hookup (vlib_main_t * vm)
156 {
157   api_main_t *am = &api_main;
158
159 #define _(N,n)                                                  \
160     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
161                            vl_api_##n##_t_handler,              \
162                            vl_noop_handler,                     \
163                            vl_api_##n##_t_endian,               \
164                            vl_api_##n##_t_print,                \
165                            sizeof(vl_api_##n##_t), 1);
166   foreach_pg_api_msg;
167 #undef _
168
169   /*
170    * Set up the (msg_name, crc, message-id) table
171    */
172   setup_message_id_table (am);
173
174   return 0;
175 }
176
177 VLIB_API_INIT_FUNCTION (pg_api_hookup);
178
179 /*
180  * fd.io coding-style-patch-verification: ON
181  *
182  * Local Variables:
183  * eval: (c-set-style "gnu")
184  * End:
185  */