pg: A Tunnel mode variant of a pg interface
[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 #define foreach_pg_api_msg                                                    \
44   _ (PG_CREATE_INTERFACE, pg_create_interface)                                \
45   _ (PG_CREATE_INTERFACE_V2, pg_create_interface_v2)                          \
46   _ (PG_CAPTURE, pg_capture)                                                  \
47   _ (PG_ENABLE_DISABLE, pg_enable_disable)                                    \
48   _ (PG_INTERFACE_ENABLE_DISABLE_COALESCE,                                    \
49      pg_interface_enable_disable_coalesce)
50
51 static void
52 vl_api_pg_create_interface_t_handler (vl_api_pg_create_interface_t * mp)
53 {
54   vl_api_pg_create_interface_reply_t *rmp;
55   int rv = 0;
56
57   pg_main_t *pg = &pg_main;
58   u32 pg_if_id =
59     pg_interface_add_or_get (pg, ntohl (mp->interface_id), mp->gso_enabled,
60                              ntohl (mp->gso_size), 0, PG_MODE_ETHERNET);
61   pg_interface_t *pi = pool_elt_at_index (pg->interfaces, pg_if_id);
62
63   /* *INDENT-OFF* */
64   REPLY_MACRO2(VL_API_PG_CREATE_INTERFACE_REPLY,
65   ({
66     rmp->sw_if_index = ntohl(pi->sw_if_index);
67   }));
68   /* *INDENT-ON* */
69 }
70
71 static void
72 vl_api_pg_create_interface_v2_t_handler (vl_api_pg_create_interface_v2_t *mp)
73 {
74   vl_api_pg_create_interface_v2_reply_t *rmp;
75   int rv = 0;
76
77   pg_main_t *pg = &pg_main;
78   u32 pg_if_id =
79     pg_interface_add_or_get (pg, ntohl (mp->interface_id), mp->gso_enabled,
80                              ntohl (mp->gso_size), 0, (u8) mp->mode);
81   pg_interface_t *pi = pool_elt_at_index (pg->interfaces, pg_if_id);
82
83   REPLY_MACRO2 (VL_API_PG_CREATE_INTERFACE_V2_REPLY,
84                 ({ rmp->sw_if_index = ntohl (pi->sw_if_index); }));
85 }
86
87 static void
88   vl_api_pg_interface_enable_disable_coalesce_t_handler
89   (vl_api_pg_interface_enable_disable_coalesce_t * mp)
90 {
91   vl_api_pg_interface_enable_disable_coalesce_reply_t *rmp;
92   int rv = 0;
93
94   VALIDATE_SW_IF_INDEX (mp);
95
96   u32 sw_if_index = ntohl (mp->sw_if_index);
97
98   pg_main_t *pg = &pg_main;
99   vnet_main_t *vnm = vnet_get_main ();
100   vnet_hw_interface_t *hw =
101     vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
102
103   if (hw)
104     {
105       pg_interface_t *pi =
106         pool_elt_at_index (pg->interfaces, hw->dev_instance);
107       if (pi->gso_enabled)
108         pg_interface_enable_disable_coalesce (pi, mp->coalesce_enabled,
109                                               hw->tx_node_index);
110       else
111         rv = VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE;
112     }
113   else
114     {
115       rv = VNET_API_ERROR_NO_MATCHING_INTERFACE;
116     }
117
118   BAD_SW_IF_INDEX_LABEL;
119   REPLY_MACRO (VL_API_PG_INTERFACE_ENABLE_DISABLE_COALESCE_REPLY);
120 }
121
122 static void
123 vl_api_pg_capture_t_handler (vl_api_pg_capture_t * mp)
124 {
125   vl_api_pg_capture_reply_t *rmp;
126   int rv = 0;
127
128   vnet_main_t *vnm = vnet_get_main ();
129   vnet_interface_main_t *im = &vnm->interface_main;
130   vnet_hw_interface_t *hi = 0;
131
132   u8 *intf_name = format (0, "pg%d", ntohl (mp->interface_id), 0);
133   vec_terminate_c_string (intf_name);
134   u32 hw_if_index = ~0;
135   uword *p = hash_get_mem (im->hw_interface_by_name, intf_name);
136   if (p)
137     hw_if_index = *p;
138   vec_free (intf_name);
139
140   if (hw_if_index != ~0)
141     {
142       pg_capture_args_t _a, *a = &_a;
143       char *pcap_file_name =
144         vl_api_from_api_to_new_c_string (&mp->pcap_file_name);
145
146       hi = vnet_get_sup_hw_interface (vnm, hw_if_index);
147       a->hw_if_index = hw_if_index;
148       a->dev_instance = hi->dev_instance;
149       a->is_enabled = mp->is_enabled;
150       a->pcap_file_name = pcap_file_name;
151       a->count = ntohl (mp->count);
152
153       clib_error_t *e = pg_capture (a);
154       if (e)
155         {
156           clib_error_report (e);
157           rv = VNET_API_ERROR_CANNOT_CREATE_PCAP_FILE;
158         }
159
160       vec_free (pcap_file_name);
161     }
162   REPLY_MACRO (VL_API_PG_CAPTURE_REPLY);
163 }
164
165 static void
166 vl_api_pg_enable_disable_t_handler (vl_api_pg_enable_disable_t * mp)
167 {
168   vl_api_pg_enable_disable_reply_t *rmp;
169   int rv = 0;
170
171   pg_main_t *pg = &pg_main;
172   u32 stream_index = ~0;
173
174   int is_enable = mp->is_enabled != 0;
175
176   if (vl_api_string_len (&mp->stream_name) > 0)
177     {
178       u8 *stream_name = vl_api_from_api_to_new_vec (mp, &mp->stream_name);
179       uword *p = hash_get_mem (pg->stream_index_by_name, stream_name);
180       if (p)
181         stream_index = *p;
182       vec_free (stream_name);
183     }
184
185   pg_enable_disable (stream_index, is_enable);
186
187   REPLY_MACRO (VL_API_PG_ENABLE_DISABLE_REPLY);
188 }
189
190 #define vl_msg_name_crc_list
191 #include <vnet/pg/pg.api.h>
192 #undef vl_msg_name_crc_list
193
194 static void
195 setup_message_id_table (api_main_t * am)
196 {
197 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
198   foreach_vl_msg_name_crc_pg;
199 #undef _
200 }
201
202 static clib_error_t *
203 pg_api_hookup (vlib_main_t * vm)
204 {
205   api_main_t *am = vlibapi_get_main ();
206
207 #define _(N,n)                                                  \
208     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
209                            vl_api_##n##_t_handler,              \
210                            vl_noop_handler,                     \
211                            vl_api_##n##_t_endian,               \
212                            vl_api_##n##_t_print,                \
213                            sizeof(vl_api_##n##_t), 1);
214   foreach_pg_api_msg;
215 #undef _
216
217   /*
218    * Set up the (msg_name, crc, message-id) table
219    */
220   setup_message_id_table (am);
221
222   return 0;
223 }
224
225 VLIB_API_INIT_FUNCTION (pg_api_hookup);
226
227 /*
228  * fd.io coding-style-patch-verification: ON
229  *
230  * Local Variables:
231  * eval: (c-set-style "gnu")
232  * End:
233  */