qos: Store function
[vpp.git] / src / vnet / qos / qos_api.c
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2018 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17
18 #include <vnet/vnet.h>
19 #include <vlibmemory/api.h>
20 #include <vnet/api_errno.h>
21
22 #include <vnet/qos/qos_record.h>
23 #include <vnet/qos/qos_store.h>
24 #include <vnet/qos/qos_mark.h>
25 #include <vnet/qos/qos_egress_map.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
43 #include <vlibapi/api_helper_macros.h>
44
45
46 #define foreach_qos_api_msg                                             \
47   _(QOS_RECORD_ENABLE_DISABLE, qos_record_enable_disable)               \
48   _(QOS_RECORD_DUMP, qos_record_dump)                                   \
49   _(QOS_STORE_ENABLE_DISABLE, qos_store_enable_disable)                 \
50   _(QOS_STORE_DUMP, qos_store_dump)                                     \
51   _(QOS_EGRESS_MAP_DELETE, qos_egress_map_delete)                       \
52   _(QOS_EGRESS_MAP_UPDATE, qos_egress_map_update)                       \
53   _(QOS_EGRESS_MAP_DUMP, qos_egress_map_dump)                           \
54   _(QOS_MARK_ENABLE_DISABLE, qos_mark_enable_disable)                   \
55   _(QOS_MARK_DUMP, qos_mark_dump)
56
57 static int
58 qos_source_decode (vl_api_qos_source_t v, qos_source_t * q)
59 {
60   switch (v)
61     {
62     case QOS_API_SOURCE_EXT:
63       *q = QOS_SOURCE_EXT;
64       return 0;
65     case QOS_API_SOURCE_VLAN:
66       *q = QOS_SOURCE_VLAN;
67       return 0;
68     case QOS_API_SOURCE_MPLS:
69       *q = QOS_SOURCE_MPLS;
70       return 0;
71     case QOS_API_SOURCE_IP:
72       *q = QOS_SOURCE_IP;
73       return 0;
74     }
75
76   return (VNET_API_ERROR_INVALID_VALUE);
77 }
78
79 static vl_api_qos_source_t
80 qos_source_encode (qos_source_t q)
81 {
82   return ((vl_api_qos_source_t) q);
83 }
84
85 void
86 vl_api_qos_record_enable_disable_t_handler (vl_api_qos_record_enable_disable_t
87                                             * mp)
88 {
89   vl_api_qos_record_enable_disable_reply_t *rmp;
90   qos_source_t qs;
91   int rv = 0;
92
93   VALIDATE_SW_IF_INDEX (&(mp->record));
94
95   rv = qos_source_decode (mp->record.input_source, &qs);
96
97   if (0 == rv)
98     {
99       if (mp->enable)
100         rv = qos_record_enable (ntohl (mp->record.sw_if_index), qs);
101       else
102         rv = qos_record_disable (ntohl (mp->record.sw_if_index), qs);
103     }
104
105   BAD_SW_IF_INDEX_LABEL;
106   REPLY_MACRO (VL_API_QOS_RECORD_ENABLE_DISABLE_REPLY);
107 }
108
109 typedef struct qos_record_send_walk_ctx_t_
110 {
111   vl_api_registration_t *reg;
112   u32 context;
113 } qos_record_send_walk_ctx_t;
114
115 static walk_rc_t
116 send_qos_record_details (u32 sw_if_index, qos_source_t input_source, void *c)
117 {
118   qos_record_send_walk_ctx_t *ctx;
119   vl_api_qos_record_details_t *mp;
120
121   ctx = c;
122   mp = vl_msg_api_alloc_zero (sizeof (*mp));
123
124   mp->_vl_msg_id = ntohs (VL_API_QOS_RECORD_DETAILS);
125   mp->context = ctx->context;
126   mp->record.sw_if_index = htonl (sw_if_index);
127   mp->record.input_source = qos_source_encode (input_source);
128
129   vl_api_send_msg (ctx->reg, (u8 *) mp);
130
131   return (WALK_CONTINUE);
132 }
133
134 static void
135 vl_api_qos_record_dump_t_handler (vl_api_qos_record_dump_t * mp)
136 {
137   vl_api_registration_t *reg;
138
139   reg = vl_api_client_index_to_registration (mp->client_index);
140   if (!reg)
141     return;
142
143   qos_record_send_walk_ctx_t ctx = {
144     .reg = reg,
145     .context = mp->context,
146   };
147   qos_record_walk (send_qos_record_details, &ctx);
148 }
149
150 void
151 vl_api_qos_store_enable_disable_t_handler (vl_api_qos_store_enable_disable_t
152                                            * mp)
153 {
154   vl_api_qos_store_enable_disable_reply_t *rmp;
155   qos_source_t qs;
156   int rv = 0;
157
158   VALIDATE_SW_IF_INDEX (&(mp->store));
159
160   rv = qos_source_decode (mp->store.input_source, &qs);
161
162   if (0 == rv)
163     {
164       if (mp->enable)
165         rv = qos_store_enable (ntohl (mp->store.sw_if_index), qs,
166                                mp->store.value);
167       else
168         rv = qos_store_disable (ntohl (mp->store.sw_if_index), qs);
169     }
170
171   BAD_SW_IF_INDEX_LABEL;
172   REPLY_MACRO (VL_API_QOS_STORE_ENABLE_DISABLE_REPLY);
173 }
174
175 typedef struct qos_store_send_walk_ctx_t_
176 {
177   vl_api_registration_t *reg;
178   u32 context;
179 } qos_store_send_walk_ctx_t;
180
181 static walk_rc_t
182 send_qos_store_details (u32 sw_if_index,
183                         qos_source_t input_source, qos_bits_t value, void *c)
184 {
185   qos_store_send_walk_ctx_t *ctx;
186   vl_api_qos_store_details_t *mp;
187
188   ctx = c;
189   mp = vl_msg_api_alloc_zero (sizeof (*mp));
190
191   mp->_vl_msg_id = ntohs (VL_API_QOS_STORE_DETAILS);
192   mp->context = ctx->context;
193   mp->store.sw_if_index = htonl (sw_if_index);
194   mp->store.input_source = qos_source_encode (input_source);
195   mp->store.value = value;
196
197   vl_api_send_msg (ctx->reg, (u8 *) mp);
198
199   return (WALK_CONTINUE);
200 }
201
202 static void
203 vl_api_qos_store_dump_t_handler (vl_api_qos_store_dump_t * mp)
204 {
205   vl_api_registration_t *reg;
206
207   reg = vl_api_client_index_to_registration (mp->client_index);
208   if (!reg)
209     return;
210
211   qos_store_send_walk_ctx_t ctx = {
212     .reg = reg,
213     .context = mp->context,
214   };
215   qos_store_walk (send_qos_store_details, &ctx);
216 }
217
218 void
219 vl_api_qos_egress_map_update_t_handler (vl_api_qos_egress_map_update_t * mp)
220 {
221   vl_api_qos_egress_map_update_reply_t *rmp;
222   qos_source_t qs;
223   int rv = 0;
224
225   FOR_EACH_QOS_SOURCE (qs)
226   {
227     qos_egress_map_update (ntohl (mp->map.id), qs,
228                            &mp->map.rows[qs].outputs[0]);
229   }
230
231   REPLY_MACRO (VL_API_QOS_EGRESS_MAP_UPDATE_REPLY);
232 }
233
234 void
235 vl_api_qos_egress_map_delete_t_handler (vl_api_qos_egress_map_delete_t * mp)
236 {
237   vl_api_qos_egress_map_delete_reply_t *rmp;
238   int rv = 0;
239
240   qos_egress_map_delete (ntohl (mp->id));
241
242   REPLY_MACRO (VL_API_QOS_EGRESS_MAP_DELETE_REPLY);
243 }
244
245 typedef struct qos_egress_map_send_walk_ctx_t_
246 {
247   vl_api_registration_t *reg;
248   u32 context;
249 } qos_egress_map_send_walk_ctx_t;
250
251 static walk_rc_t
252 send_qos_egress_map_details (qos_egress_map_id_t id,
253                              const qos_egress_map_t * m, void *c)
254 {
255   qos_egress_map_send_walk_ctx_t *ctx;
256   vl_api_qos_egress_map_details_t *mp;
257   u8 ii;
258
259   ctx = c;
260   mp = vl_msg_api_alloc_zero (sizeof (*mp));
261
262   mp->_vl_msg_id = ntohs (VL_API_QOS_EGRESS_MAP_DETAILS);
263   mp->context = ctx->context;
264   mp->map.id = htonl (id);
265
266   for (ii = 0; ii < 4; ii++)
267     clib_memcpy (mp->map.rows[ii].outputs, m->qem_output[ii], 256);
268
269   vl_api_send_msg (ctx->reg, (u8 *) mp);
270
271   return (WALK_CONTINUE);
272 }
273
274 static void
275 vl_api_qos_egress_map_dump_t_handler (vl_api_qos_egress_map_dump_t * mp)
276 {
277   vl_api_registration_t *reg;
278
279   reg = vl_api_client_index_to_registration (mp->client_index);
280   if (!reg)
281     return;
282
283   qos_egress_map_send_walk_ctx_t ctx = {
284     .reg = reg,
285     .context = mp->context,
286   };
287   qos_egress_map_walk (send_qos_egress_map_details, &ctx);
288 }
289
290 void
291 vl_api_qos_mark_enable_disable_t_handler (vl_api_qos_mark_enable_disable_t *
292                                           mp)
293 {
294   vl_api_qos_mark_enable_disable_reply_t *rmp;
295   qos_source_t qs;
296   int rv = 0;
297
298   rv = qos_source_decode (mp->mark.output_source, &qs);
299
300   if (0 == rv)
301     {
302       if (mp->enable)
303         rv = qos_mark_enable (ntohl (mp->mark.sw_if_index),
304                               qs, ntohl (mp->mark.map_id));
305       else
306         rv = qos_mark_disable (ntohl (mp->mark.sw_if_index), qs);
307     }
308
309   REPLY_MACRO (VL_API_QOS_MARK_ENABLE_DISABLE_REPLY);
310 }
311
312 typedef struct qos_mark_send_walk_ctx_t_
313 {
314   vl_api_registration_t *reg;
315   u32 context;
316 } qos_mark_send_walk_ctx_t;
317
318 static walk_rc_t
319 send_qos_mark_details (u32 sw_if_index,
320                        u32 map_id, qos_source_t output_source, void *c)
321 {
322   qos_mark_send_walk_ctx_t *ctx;
323   vl_api_qos_mark_details_t *mp;
324
325   ctx = c;
326   mp = vl_msg_api_alloc_zero (sizeof (*mp));
327
328   mp->_vl_msg_id = ntohs (VL_API_QOS_MARK_DETAILS);
329   mp->context = ctx->context;
330   mp->mark.sw_if_index = htonl (sw_if_index);
331   mp->mark.output_source = qos_source_encode (output_source);
332   mp->mark.map_id = htonl (map_id);
333
334   vl_api_send_msg (ctx->reg, (u8 *) mp);
335
336   return (WALK_CONTINUE);
337 }
338
339 static void
340 vl_api_qos_mark_dump_t_handler (vl_api_qos_mark_dump_t * mp)
341 {
342   vl_api_registration_t *reg;
343
344   reg = vl_api_client_index_to_registration (mp->client_index);
345   if (!reg)
346     return;
347
348   qos_mark_send_walk_ctx_t ctx = {
349     .reg = reg,
350     .context = mp->context,
351   };
352   qos_mark_walk (send_qos_mark_details, &ctx);
353 }
354
355 #define vl_msg_name_crc_list
356 #include <vnet/qos/qos.api.h>
357 #undef vl_msg_name_crc_list
358
359 static void
360 setup_message_id_table (api_main_t * am)
361 {
362 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
363   foreach_vl_msg_name_crc_qos;
364 #undef _
365 }
366
367 static clib_error_t *
368 qos_api_hookup (vlib_main_t * vm)
369 {
370   api_main_t *am = &api_main;
371
372 #define _(N,n)                                                  \
373     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
374                            vl_api_##n##_t_handler,              \
375                            vl_noop_handler,                     \
376                            vl_api_##n##_t_endian,               \
377                            vl_api_##n##_t_print,                \
378                            sizeof(vl_api_##n##_t), 1);
379   foreach_qos_api_msg;
380 #undef _
381
382   /*
383    * Set up the (msg_name, crc, message-id) table
384    */
385   setup_message_id_table (am);
386
387   return 0;
388 }
389
390 VLIB_API_INIT_FUNCTION (qos_api_hookup);
391
392 /*
393  * fd.io coding-style-patch-verification: ON
394  *
395  * Local Variables:
396  * eval: (c-set-style "gnu")
397  * End:
398  */