API refactoring : dpdk
[vpp.git] / src / vnet / devices / dpdk / dpdk_api.c
1 /*
2  *------------------------------------------------------------------
3  * dpdk_api.c - dpdk interface 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 #if DPDK > 0
24 #include <vnet/devices/dpdk/dpdk.h>
25 #endif
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
43 #include <vlibapi/api_helper_macros.h>
44
45 #define foreach_vpe_api_msg                                               \
46 _(SW_INTERFACE_SET_DPDK_HQOS_PIPE, sw_interface_set_dpdk_hqos_pipe)       \
47 _(SW_INTERFACE_SET_DPDK_HQOS_SUBPORT, sw_interface_set_dpdk_hqos_subport) \
48 _(SW_INTERFACE_SET_DPDK_HQOS_TCTBL, sw_interface_set_dpdk_hqos_tctbl)
49
50 static void
51   vl_api_sw_interface_set_dpdk_hqos_pipe_t_handler
52   (vl_api_sw_interface_set_dpdk_hqos_pipe_t * mp)
53 {
54   vl_api_sw_interface_set_dpdk_hqos_pipe_reply_t *rmp;
55   int rv = 0;
56
57 #if DPDK > 0
58   dpdk_main_t *dm = &dpdk_main;
59   dpdk_device_t *xd;
60
61   u32 sw_if_index = ntohl (mp->sw_if_index);
62   u32 subport = ntohl (mp->subport);
63   u32 pipe = ntohl (mp->pipe);
64   u32 profile = ntohl (mp->profile);
65   vnet_hw_interface_t *hw;
66
67   VALIDATE_SW_IF_INDEX (mp);
68
69   /* hw_if & dpdk device */
70   hw = vnet_get_sup_hw_interface (dm->vnet_main, sw_if_index);
71
72   xd = vec_elt_at_index (dm->devices, hw->dev_instance);
73
74   rv = rte_sched_pipe_config (xd->hqos_ht->hqos, subport, pipe, profile);
75
76   BAD_SW_IF_INDEX_LABEL;
77 #else
78   clib_warning ("setting HQoS pipe parameters without DPDK not implemented");
79   rv = VNET_API_ERROR_UNIMPLEMENTED;
80 #endif /* DPDK */
81
82   REPLY_MACRO (VL_API_SW_INTERFACE_SET_DPDK_HQOS_PIPE_REPLY);
83 }
84
85 static void
86   vl_api_sw_interface_set_dpdk_hqos_subport_t_handler
87   (vl_api_sw_interface_set_dpdk_hqos_subport_t * mp)
88 {
89   vl_api_sw_interface_set_dpdk_hqos_subport_reply_t *rmp;
90   int rv = 0;
91
92 #if DPDK > 0
93   dpdk_main_t *dm = &dpdk_main;
94   dpdk_device_t *xd;
95   struct rte_sched_subport_params p;
96
97   u32 sw_if_index = ntohl (mp->sw_if_index);
98   u32 subport = ntohl (mp->subport);
99   p.tb_rate = ntohl (mp->tb_rate);
100   p.tb_size = ntohl (mp->tb_size);
101   p.tc_rate[0] = ntohl (mp->tc_rate[0]);
102   p.tc_rate[1] = ntohl (mp->tc_rate[1]);
103   p.tc_rate[2] = ntohl (mp->tc_rate[2]);
104   p.tc_rate[3] = ntohl (mp->tc_rate[3]);
105   p.tc_period = ntohl (mp->tc_period);
106
107   vnet_hw_interface_t *hw;
108
109   VALIDATE_SW_IF_INDEX (mp);
110
111   /* hw_if & dpdk device */
112   hw = vnet_get_sup_hw_interface (dm->vnet_main, sw_if_index);
113
114   xd = vec_elt_at_index (dm->devices, hw->dev_instance);
115
116   rv = rte_sched_subport_config (xd->hqos_ht->hqos, subport, &p);
117
118   BAD_SW_IF_INDEX_LABEL;
119 #else
120   clib_warning
121     ("setting HQoS subport parameters without DPDK not implemented");
122   rv = VNET_API_ERROR_UNIMPLEMENTED;
123 #endif /* DPDK */
124
125   REPLY_MACRO (VL_API_SW_INTERFACE_SET_DPDK_HQOS_SUBPORT_REPLY);
126 }
127
128 static void
129   vl_api_sw_interface_set_dpdk_hqos_tctbl_t_handler
130   (vl_api_sw_interface_set_dpdk_hqos_tctbl_t * mp)
131 {
132   vl_api_sw_interface_set_dpdk_hqos_tctbl_reply_t *rmp;
133   int rv = 0;
134
135 #if DPDK > 0
136   dpdk_main_t *dm = &dpdk_main;
137   vlib_thread_main_t *tm = vlib_get_thread_main ();
138   dpdk_device_t *xd;
139
140   u32 sw_if_index = ntohl (mp->sw_if_index);
141   u32 entry = ntohl (mp->entry);
142   u32 tc = ntohl (mp->tc);
143   u32 queue = ntohl (mp->queue);
144   u32 val, i;
145
146   vnet_hw_interface_t *hw;
147
148   VALIDATE_SW_IF_INDEX (mp);
149
150   /* hw_if & dpdk device */
151   hw = vnet_get_sup_hw_interface (dm->vnet_main, sw_if_index);
152
153   xd = vec_elt_at_index (dm->devices, hw->dev_instance);
154
155   if (tc >= RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE)
156     {
157       clib_warning ("invalid traffic class !!");
158       rv = VNET_API_ERROR_INVALID_VALUE;
159       goto done;
160     }
161   if (queue >= RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS)
162     {
163       clib_warning ("invalid queue !!");
164       rv = VNET_API_ERROR_INVALID_VALUE;
165       goto done;
166     }
167
168   /* Detect the set of worker threads */
169   uword *p = hash_get_mem (tm->thread_registrations_by_name, "workers");
170
171   if (p == 0)
172     {
173       clib_warning ("worker thread registration AWOL !!");
174       rv = VNET_API_ERROR_INVALID_VALUE_2;
175       goto done;
176     }
177
178   vlib_thread_registration_t *tr = (vlib_thread_registration_t *) p[0];
179   int worker_thread_first = tr->first_index;
180   int worker_thread_count = tr->count;
181
182   val = tc * RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS + queue;
183   for (i = 0; i < worker_thread_count; i++)
184     xd->hqos_wt[worker_thread_first + i].hqos_tc_table[entry] = val;
185
186   BAD_SW_IF_INDEX_LABEL;
187 done:
188 #else
189   clib_warning ("setting HQoS DSCP table entry without DPDK not implemented");
190   rv = VNET_API_ERROR_UNIMPLEMENTED;
191 #endif /* DPDK */
192
193   REPLY_MACRO (VL_API_SW_INTERFACE_SET_DPDK_HQOS_TCTBL_REPLY);
194 }
195
196 /*
197  * dpdk_api_hookup
198  * Add vpe's API message handlers to the table.
199  * vlib has alread mapped shared memory and
200  * added the client registration handlers.
201  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
202  */
203 #define vl_msg_name_crc_list
204 #include <vnet/vnet_all_api_h.h>
205 #undef vl_msg_name_crc_list
206
207 static void
208 setup_message_id_table (api_main_t * am)
209 {
210 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
211   foreach_vl_msg_name_crc_dpdk;
212 #undef _
213 }
214
215 static clib_error_t *
216 dpdk_api_hookup (vlib_main_t * vm)
217 {
218   api_main_t *am = &api_main;
219
220 #define _(N,n)                                                  \
221     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
222                            vl_api_##n##_t_handler,              \
223                            vl_noop_handler,                     \
224                            vl_api_##n##_t_endian,               \
225                            vl_api_##n##_t_print,                \
226                            sizeof(vl_api_##n##_t), 1);
227   foreach_vpe_api_msg;
228 #undef _
229
230   /*
231    * Set up the (msg_name, crc, message-id) table
232    */
233   setup_message_id_table (am);
234
235   return 0;
236 }
237
238 VLIB_API_INIT_FUNCTION (dpdk_api_hookup);
239
240 /*
241  * fd.io coding-style-patch-verification: ON
242  *
243  * Local Variables:
244  * eval: (c-set-style "gnu")
245  * End:
246  */