blob: 77a8b43464ae915bf923f9505ed296990b62a551 [file] [log] [blame]
Pavel Kotucekd2c97d92017-01-24 10:58:12 +01001/*
2 *------------------------------------------------------------------
3 * classify_api.c - classify 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
26#include <vnet/classify/vnet_classify.h>
27#include <vnet/classify/input_acl.h>
28#include <vnet/classify/policer_classify.h>
29#include <vnet/classify/flow_classify.h>
30
31#include <vnet/vnet_msg_enum.h>
32
33#define vl_typedefs /* define message structures */
34#include <vnet/vnet_all_api_h.h>
35#undef vl_typedefs
36
37#define vl_endianfun /* define message structures */
38#include <vnet/vnet_all_api_h.h>
39#undef vl_endianfun
40
41/* instantiate all the print functions we know about */
42#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
43#define vl_printfun
44#include <vnet/vnet_all_api_h.h>
45#undef vl_printfun
46
47#include <vlibapi/api_helper_macros.h>
48
49#define foreach_vpe_api_msg \
50_(CLASSIFY_ADD_DEL_TABLE, classify_add_del_table) \
51_(CLASSIFY_ADD_DEL_SESSION, classify_add_del_session) \
52_(CLASSIFY_TABLE_IDS,classify_table_ids) \
53_(CLASSIFY_TABLE_BY_INTERFACE, classify_table_by_interface) \
54_(CLASSIFY_TABLE_INFO,classify_table_info) \
55_(CLASSIFY_SESSION_DUMP,classify_session_dump) \
56_(CLASSIFY_SESSION_DETAILS,classify_session_details) \
57_(POLICER_CLASSIFY_SET_INTERFACE, policer_classify_set_interface) \
58_(POLICER_CLASSIFY_DUMP, policer_classify_dump) \
59_(FLOW_CLASSIFY_SET_INTERFACE, flow_classify_set_interface) \
60_(FLOW_CLASSIFY_DUMP, flow_classify_dump)
61
62#define foreach_classify_add_del_table_field \
63_(table_index) \
64_(nbuckets) \
65_(memory_size) \
66_(skip_n_vectors) \
67_(match_n_vectors) \
68_(next_table_index) \
69_(miss_next_index) \
70_(current_data_flag) \
71_(current_data_offset)
72
73static void vl_api_classify_add_del_table_t_handler
74 (vl_api_classify_add_del_table_t * mp)
75{
76 vl_api_classify_add_del_table_reply_t *rmp;
77 vnet_classify_main_t *cm = &vnet_classify_main;
78 vnet_classify_table_t *t;
79 int rv;
80
81#define _(a) u32 a;
82 foreach_classify_add_del_table_field;
83#undef _
84
85#define _(a) a = ntohl(mp->a);
86 foreach_classify_add_del_table_field;
87#undef _
88
89 /* The underlying API fails silently, on purpose, so check here */
90 if (mp->is_add == 0) /* delete */
91 {
92 if (pool_is_free_index (cm->tables, table_index))
93 {
94 rv = VNET_API_ERROR_NO_SUCH_TABLE;
95 goto out;
96 }
97 }
98 else /* add or update */
99 {
100 if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
101 table_index = ~0;
102 }
103
104 rv = vnet_classify_add_del_table
105 (cm, mp->mask, nbuckets, memory_size,
106 skip_n_vectors, match_n_vectors,
107 next_table_index, miss_next_index, &table_index,
108 current_data_flag, current_data_offset, mp->is_add, mp->del_chain);
109
110out:
111 /* *INDENT-OFF* */
112 REPLY_MACRO2(VL_API_CLASSIFY_ADD_DEL_TABLE_REPLY,
113 ({
114 if (rv == 0 && mp->is_add)
115 {
116 t = pool_elt_at_index (cm->tables, table_index);
117 rmp->skip_n_vectors = ntohl(t->skip_n_vectors);
118 rmp->match_n_vectors = ntohl(t->match_n_vectors);
119 rmp->new_table_index = ntohl(table_index);
120 }
121 else
122 {
123 rmp->skip_n_vectors = ~0;
124 rmp->match_n_vectors = ~0;
125 rmp->new_table_index = ~0;
126 }
127 }));
128 /* *INDENT-ON* */
129}
130
131static void vl_api_classify_add_del_session_t_handler
132 (vl_api_classify_add_del_session_t * mp)
133{
134 vnet_classify_main_t *cm = &vnet_classify_main;
135 vl_api_classify_add_del_session_reply_t *rmp;
136 int rv;
137 u32 table_index, hit_next_index, opaque_index, metadata;
138 i32 advance;
139 u8 action;
140
141 table_index = ntohl (mp->table_index);
142 hit_next_index = ntohl (mp->hit_next_index);
143 opaque_index = ntohl (mp->opaque_index);
144 advance = ntohl (mp->advance);
145 action = mp->action;
146 metadata = ntohl (mp->metadata);
147
148 rv = vnet_classify_add_del_session
149 (cm, table_index, mp->match, hit_next_index, opaque_index,
150 advance, action, metadata, mp->is_add);
151
152 REPLY_MACRO (VL_API_CLASSIFY_ADD_DEL_SESSION_REPLY);
153}
154
155static void
156 vl_api_policer_classify_set_interface_t_handler
157 (vl_api_policer_classify_set_interface_t * mp)
158{
159 vlib_main_t *vm = vlib_get_main ();
160 vl_api_policer_classify_set_interface_reply_t *rmp;
161 int rv;
162 u32 sw_if_index, ip4_table_index, ip6_table_index, l2_table_index;
163
164 ip4_table_index = ntohl (mp->ip4_table_index);
165 ip6_table_index = ntohl (mp->ip6_table_index);
166 l2_table_index = ntohl (mp->l2_table_index);
167 sw_if_index = ntohl (mp->sw_if_index);
168
169 VALIDATE_SW_IF_INDEX (mp);
170
171 rv = vnet_set_policer_classify_intfc (vm, sw_if_index, ip4_table_index,
172 ip6_table_index, l2_table_index,
173 mp->is_add);
174
175 BAD_SW_IF_INDEX_LABEL;
176
177 REPLY_MACRO (VL_API_POLICER_CLASSIFY_SET_INTERFACE_REPLY);
178}
179
180static void
181send_policer_classify_details (u32 sw_if_index,
182 u32 table_index,
183 unix_shared_memory_queue_t * q, u32 context)
184{
185 vl_api_policer_classify_details_t *mp;
186
187 mp = vl_msg_api_alloc (sizeof (*mp));
188 memset (mp, 0, sizeof (*mp));
189 mp->_vl_msg_id = ntohs (VL_API_POLICER_CLASSIFY_DETAILS);
190 mp->context = context;
191 mp->sw_if_index = htonl (sw_if_index);
192 mp->table_index = htonl (table_index);
193
194 vl_msg_api_send_shmem (q, (u8 *) & mp);
195}
196
197static void
198vl_api_policer_classify_dump_t_handler (vl_api_policer_classify_dump_t * mp)
199{
200 unix_shared_memory_queue_t *q;
201 policer_classify_main_t *pcm = &policer_classify_main;
202 u32 *vec_tbl;
203 int i;
204
205 q = vl_api_client_index_to_input_queue (mp->client_index);
206 if (q == 0)
207 return;
208
209 vec_tbl = pcm->classify_table_index_by_sw_if_index[mp->type];
210
211 if (vec_len (vec_tbl))
212 {
213 for (i = 0; i < vec_len (vec_tbl); i++)
214 {
215 if (vec_elt (vec_tbl, i) == ~0)
216 continue;
217
218 send_policer_classify_details (i, vec_elt (vec_tbl, i), q,
219 mp->context);
220 }
221 }
222}
223
224static void
225vl_api_classify_table_ids_t_handler (vl_api_classify_table_ids_t * mp)
226{
227 unix_shared_memory_queue_t *q;
228
229 q = vl_api_client_index_to_input_queue (mp->client_index);
230 if (q == 0)
231 return;
232
233 vnet_classify_main_t *cm = &vnet_classify_main;
234 vnet_classify_table_t *t;
235 u32 *table_ids = 0;
236 u32 count;
237
238 /* *INDENT-OFF* */
239 pool_foreach (t, cm->tables,
240 ({
241 vec_add1 (table_ids, ntohl(t - cm->tables));
242 }));
243 /* *INDENT-ON* */
244 count = vec_len (table_ids);
245
246 vl_api_classify_table_ids_reply_t *rmp;
247 rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp) + count * sizeof (u32));
248 rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_TABLE_IDS_REPLY);
249 rmp->context = mp->context;
250 rmp->count = ntohl (count);
251 clib_memcpy (rmp->ids, table_ids, count * sizeof (u32));
252 rmp->retval = 0;
253
254 vl_msg_api_send_shmem (q, (u8 *) & rmp);
255
256 vec_free (table_ids);
257}
258
259static void
260 vl_api_classify_table_by_interface_t_handler
261 (vl_api_classify_table_by_interface_t * mp)
262{
263 vl_api_classify_table_by_interface_reply_t *rmp;
264 int rv = 0;
265
266 u32 sw_if_index = ntohl (mp->sw_if_index);
267 u32 *acl = 0;
268
269 vec_validate (acl, INPUT_ACL_N_TABLES - 1);
270 vec_set (acl, ~0);
271
272 VALIDATE_SW_IF_INDEX (mp);
273
274 input_acl_main_t *am = &input_acl_main;
275
276 int if_idx;
277 u32 type;
278
279 for (type = 0; type < INPUT_ACL_N_TABLES; type++)
280 {
281 u32 *vec_tbl = am->classify_table_index_by_sw_if_index[type];
282 if (vec_len (vec_tbl))
283 {
284 for (if_idx = 0; if_idx < vec_len (vec_tbl); if_idx++)
285 {
286 if (vec_elt (vec_tbl, if_idx) == ~0 || sw_if_index != if_idx)
287 {
288 continue;
289 }
290 acl[type] = vec_elt (vec_tbl, if_idx);
291 }
292 }
293 }
294
295 BAD_SW_IF_INDEX_LABEL;
296
297 /* *INDENT-OFF* */
298 REPLY_MACRO2(VL_API_CLASSIFY_TABLE_BY_INTERFACE_REPLY,
299 ({
300 rmp->sw_if_index = ntohl(sw_if_index);
301 rmp->l2_table_id = ntohl(acl[INPUT_ACL_TABLE_L2]);
302 rmp->ip4_table_id = ntohl(acl[INPUT_ACL_TABLE_IP4]);
303 rmp->ip6_table_id = ntohl(acl[INPUT_ACL_TABLE_IP6]);
304 }));
305 /* *INDENT-ON* */
306 vec_free (acl);
307}
308
309static void
310vl_api_classify_table_info_t_handler (vl_api_classify_table_info_t * mp)
311{
312 unix_shared_memory_queue_t *q;
313
314 q = vl_api_client_index_to_input_queue (mp->client_index);
315 if (q == 0)
316 return;
317
318 vl_api_classify_table_info_reply_t *rmp = 0;
319
320 vnet_classify_main_t *cm = &vnet_classify_main;
321 u32 table_id = ntohl (mp->table_id);
322 vnet_classify_table_t *t;
323
324 /* *INDENT-OFF* */
325 pool_foreach (t, cm->tables,
326 ({
327 if (table_id == t - cm->tables)
328 {
329 rmp = vl_msg_api_alloc_as_if_client
330 (sizeof (*rmp) + t->match_n_vectors * sizeof (u32x4));
331 rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_TABLE_INFO_REPLY);
332 rmp->context = mp->context;
333 rmp->table_id = ntohl(table_id);
334 rmp->nbuckets = ntohl(t->nbuckets);
335 rmp->match_n_vectors = ntohl(t->match_n_vectors);
336 rmp->skip_n_vectors = ntohl(t->skip_n_vectors);
337 rmp->active_sessions = ntohl(t->active_elements);
338 rmp->next_table_index = ntohl(t->next_table_index);
339 rmp->miss_next_index = ntohl(t->miss_next_index);
340 rmp->mask_length = ntohl(t->match_n_vectors * sizeof (u32x4));
341 clib_memcpy(rmp->mask, t->mask, t->match_n_vectors * sizeof(u32x4));
342 rmp->retval = 0;
343 break;
344 }
345 }));
346 /* *INDENT-ON* */
347
348 if (rmp == 0)
349 {
350 rmp = vl_msg_api_alloc (sizeof (*rmp));
351 rmp->_vl_msg_id = ntohs ((VL_API_CLASSIFY_TABLE_INFO_REPLY));
352 rmp->context = mp->context;
353 rmp->retval = ntohl (VNET_API_ERROR_CLASSIFY_TABLE_NOT_FOUND);
354 }
355
356 vl_msg_api_send_shmem (q, (u8 *) & rmp);
357}
358
359static void
360vl_api_classify_session_details_t_handler (vl_api_classify_session_details_t *
361 mp)
362{
363 clib_warning ("BUG");
364}
365
366static void
367send_classify_session_details (unix_shared_memory_queue_t * q,
368 u32 table_id,
369 u32 match_length,
370 vnet_classify_entry_t * e, u32 context)
371{
372 vl_api_classify_session_details_t *rmp;
373
374 rmp = vl_msg_api_alloc (sizeof (*rmp));
375 memset (rmp, 0, sizeof (*rmp));
376 rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_SESSION_DETAILS);
377 rmp->context = context;
378 rmp->table_id = ntohl (table_id);
379 rmp->hit_next_index = ntohl (e->next_index);
380 rmp->advance = ntohl (e->advance);
381 rmp->opaque_index = ntohl (e->opaque_index);
382 rmp->match_length = ntohl (match_length);
383 clib_memcpy (rmp->match, e->key, match_length);
384
385 vl_msg_api_send_shmem (q, (u8 *) & rmp);
386}
387
388static void
389vl_api_classify_session_dump_t_handler (vl_api_classify_session_dump_t * mp)
390{
391 vnet_classify_main_t *cm = &vnet_classify_main;
392 unix_shared_memory_queue_t *q;
393
394 u32 table_id = ntohl (mp->table_id);
395 vnet_classify_table_t *t;
396
397 q = vl_api_client_index_to_input_queue (mp->client_index);
398 if (!q)
399 return;
400
401 /* *INDENT-OFF* */
402 pool_foreach (t, cm->tables,
403 ({
404 if (table_id == t - cm->tables)
405 {
406 vnet_classify_bucket_t * b;
407 vnet_classify_entry_t * v, * save_v;
408 int i, j, k;
409
410 for (i = 0; i < t->nbuckets; i++)
411 {
412 b = &t->buckets [i];
413 if (b->offset == 0)
414 continue;
415
416 save_v = vnet_classify_get_entry (t, b->offset);
417 for (j = 0; j < (1<<b->log2_pages); j++)
418 {
419 for (k = 0; k < t->entries_per_page; k++)
420 {
421 v = vnet_classify_entry_at_index
422 (t, save_v, j*t->entries_per_page + k);
423 if (vnet_classify_entry_is_free (v))
424 continue;
425
426 send_classify_session_details
427 (q, table_id, t->match_n_vectors * sizeof (u32x4),
428 v, mp->context);
429 }
430 }
431 }
432 break;
433 }
434 }));
435 /* *INDENT-ON* */
436}
437
438static void
439 vl_api_flow_classify_set_interface_t_handler
440 (vl_api_flow_classify_set_interface_t * mp)
441{
442 vlib_main_t *vm = vlib_get_main ();
443 vl_api_flow_classify_set_interface_reply_t *rmp;
444 int rv;
445 u32 sw_if_index, ip4_table_index, ip6_table_index;
446
447 ip4_table_index = ntohl (mp->ip4_table_index);
448 ip6_table_index = ntohl (mp->ip6_table_index);
449 sw_if_index = ntohl (mp->sw_if_index);
450
451 VALIDATE_SW_IF_INDEX (mp);
452
453 rv = vnet_set_flow_classify_intfc (vm, sw_if_index, ip4_table_index,
454 ip6_table_index, mp->is_add);
455
456 BAD_SW_IF_INDEX_LABEL;
457
458 REPLY_MACRO (VL_API_FLOW_CLASSIFY_SET_INTERFACE_REPLY);
459}
460
461static void
462send_flow_classify_details (u32 sw_if_index,
463 u32 table_index,
464 unix_shared_memory_queue_t * q, u32 context)
465{
466 vl_api_flow_classify_details_t *mp;
467
468 mp = vl_msg_api_alloc (sizeof (*mp));
469 memset (mp, 0, sizeof (*mp));
470 mp->_vl_msg_id = ntohs (VL_API_FLOW_CLASSIFY_DETAILS);
471 mp->context = context;
472 mp->sw_if_index = htonl (sw_if_index);
473 mp->table_index = htonl (table_index);
474
475 vl_msg_api_send_shmem (q, (u8 *) & mp);
476}
477
478static void
479vl_api_flow_classify_dump_t_handler (vl_api_flow_classify_dump_t * mp)
480{
481 unix_shared_memory_queue_t *q;
482 flow_classify_main_t *pcm = &flow_classify_main;
483 u32 *vec_tbl;
484 int i;
485
486 q = vl_api_client_index_to_input_queue (mp->client_index);
487 if (q == 0)
488 return;
489
490 vec_tbl = pcm->classify_table_index_by_sw_if_index[mp->type];
491
492 if (vec_len (vec_tbl))
493 {
494 for (i = 0; i < vec_len (vec_tbl); i++)
495 {
496 if (vec_elt (vec_tbl, i) == ~0)
497 continue;
498
499 send_flow_classify_details (i, vec_elt (vec_tbl, i), q,
500 mp->context);
501 }
502 }
503}
504
505/*
506 * classify_api_hookup
507 * Add vpe's API message handlers to the table.
508 * vlib has alread mapped shared memory and
509 * added the client registration handlers.
510 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
511 */
512#define vl_msg_name_crc_list
513#include <vnet/vnet_all_api_h.h>
514#undef vl_msg_name_crc_list
515
516static void
517setup_message_id_table (api_main_t * am)
518{
519#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
520 foreach_vl_msg_name_crc_classify;
521#undef _
522}
523
524static clib_error_t *
525classify_api_hookup (vlib_main_t * vm)
526{
527 api_main_t *am = &api_main;
528
529#define _(N,n) \
530 vl_msg_api_set_handlers(VL_API_##N, #n, \
531 vl_api_##n##_t_handler, \
532 vl_noop_handler, \
533 vl_api_##n##_t_endian, \
534 vl_api_##n##_t_print, \
535 sizeof(vl_api_##n##_t), 1);
536 foreach_vpe_api_msg;
537#undef _
538
539 /*
540 * Set up the (msg_name, crc, message-id) table
541 */
542 setup_message_id_table (am);
543
544 return 0;
545}
546
547VLIB_API_INIT_FUNCTION (classify_api_hookup);
548
549/*
550 * fd.io coding-style-patch-verification: ON
551 *
552 * Local Variables:
553 * eval: (c-set-style "gnu")
554 * End:
555 */