2 *------------------------------------------------------------------
3 * classify_api.c - classify api
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:
10 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
20 #include <vnet/vnet.h>
21 #include <vlibmemory/api.h>
23 #include <vnet/interface.h>
24 #include <vnet/api_errno.h>
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>
31 #include <vnet/vnet_msg_enum.h>
33 #define vl_typedefs /* define message structures */
34 #include <vnet/vnet_all_api_h.h>
37 #define vl_endianfun /* define message structures */
38 #include <vnet/vnet_all_api_h.h>
41 /* instantiate all the print functions we know about */
42 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
44 #include <vnet/vnet_all_api_h.h>
47 #include <vlibapi/api_helper_macros.h>
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)
62 #define foreach_classify_add_del_table_field \
70 _(current_data_flag) \
71 _(current_data_offset)
73 static void vl_api_classify_add_del_table_t_handler
74 (vl_api_classify_add_del_table_t * mp)
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;
82 foreach_classify_add_del_table_field;
85 #define _(a) a = ntohl(mp->a);
86 foreach_classify_add_del_table_field;
89 /* The underlying API fails silently, on purpose, so check here */
90 if (mp->is_add == 0) /* delete */
92 if (pool_is_free_index (cm->tables, table_index))
94 rv = VNET_API_ERROR_NO_SUCH_TABLE;
98 else /* add or update */
100 if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
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);
112 REPLY_MACRO2(VL_API_CLASSIFY_ADD_DEL_TABLE_REPLY,
114 if (rv == 0 && mp->is_add)
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);
123 rmp->skip_n_vectors = ~0;
124 rmp->match_n_vectors = ~0;
125 rmp->new_table_index = ~0;
131 static void vl_api_classify_add_del_session_t_handler
132 (vl_api_classify_add_del_session_t * mp)
134 vnet_classify_main_t *cm = &vnet_classify_main;
135 vl_api_classify_add_del_session_reply_t *rmp;
137 u32 table_index, hit_next_index, opaque_index, metadata;
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);
146 metadata = ntohl (mp->metadata);
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);
152 REPLY_MACRO (VL_API_CLASSIFY_ADD_DEL_SESSION_REPLY);
156 vl_api_policer_classify_set_interface_t_handler
157 (vl_api_policer_classify_set_interface_t * mp)
159 vlib_main_t *vm = vlib_get_main ();
160 vl_api_policer_classify_set_interface_reply_t *rmp;
162 u32 sw_if_index, ip4_table_index, ip6_table_index, l2_table_index;
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);
169 VALIDATE_SW_IF_INDEX (mp);
171 rv = vnet_set_policer_classify_intfc (vm, sw_if_index, ip4_table_index,
172 ip6_table_index, l2_table_index,
175 BAD_SW_IF_INDEX_LABEL;
177 REPLY_MACRO (VL_API_POLICER_CLASSIFY_SET_INTERFACE_REPLY);
181 send_policer_classify_details (u32 sw_if_index,
183 unix_shared_memory_queue_t * q, u32 context)
185 vl_api_policer_classify_details_t *mp;
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);
194 vl_msg_api_send_shmem (q, (u8 *) & mp);
198 vl_api_policer_classify_dump_t_handler (vl_api_policer_classify_dump_t * mp)
200 unix_shared_memory_queue_t *q;
201 policer_classify_main_t *pcm = &policer_classify_main;
205 q = vl_api_client_index_to_input_queue (mp->client_index);
209 vec_tbl = pcm->classify_table_index_by_sw_if_index[mp->type];
211 if (vec_len (vec_tbl))
213 for (i = 0; i < vec_len (vec_tbl); i++)
215 if (vec_elt (vec_tbl, i) == ~0)
218 send_policer_classify_details (i, vec_elt (vec_tbl, i), q,
225 vl_api_classify_table_ids_t_handler (vl_api_classify_table_ids_t * mp)
227 unix_shared_memory_queue_t *q;
229 q = vl_api_client_index_to_input_queue (mp->client_index);
233 vnet_classify_main_t *cm = &vnet_classify_main;
234 vnet_classify_table_t *t;
239 pool_foreach (t, cm->tables,
241 vec_add1 (table_ids, ntohl(t - cm->tables));
244 count = vec_len (table_ids);
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));
254 vl_msg_api_send_shmem (q, (u8 *) & rmp);
256 vec_free (table_ids);
260 vl_api_classify_table_by_interface_t_handler
261 (vl_api_classify_table_by_interface_t * mp)
263 vl_api_classify_table_by_interface_reply_t *rmp;
266 u32 sw_if_index = ntohl (mp->sw_if_index);
269 vec_validate (acl, INPUT_ACL_N_TABLES - 1);
272 VALIDATE_SW_IF_INDEX (mp);
274 input_acl_main_t *am = &input_acl_main;
279 for (type = 0; type < INPUT_ACL_N_TABLES; type++)
281 u32 *vec_tbl = am->classify_table_index_by_sw_if_index[type];
282 if (vec_len (vec_tbl))
284 for (if_idx = 0; if_idx < vec_len (vec_tbl); if_idx++)
286 if (vec_elt (vec_tbl, if_idx) == ~0 || sw_if_index != if_idx)
290 acl[type] = vec_elt (vec_tbl, if_idx);
295 BAD_SW_IF_INDEX_LABEL;
298 REPLY_MACRO2(VL_API_CLASSIFY_TABLE_BY_INTERFACE_REPLY,
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]);
310 vl_api_classify_table_info_t_handler (vl_api_classify_table_info_t * mp)
312 unix_shared_memory_queue_t *q;
314 q = vl_api_client_index_to_input_queue (mp->client_index);
318 vl_api_classify_table_info_reply_t *rmp = 0;
320 vnet_classify_main_t *cm = &vnet_classify_main;
321 u32 table_id = ntohl (mp->table_id);
322 vnet_classify_table_t *t;
325 pool_foreach (t, cm->tables,
327 if (table_id == t - cm->tables)
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));
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);
356 vl_msg_api_send_shmem (q, (u8 *) & rmp);
360 vl_api_classify_session_details_t_handler (vl_api_classify_session_details_t *
363 clib_warning ("BUG");
367 send_classify_session_details (unix_shared_memory_queue_t * q,
370 vnet_classify_entry_t * e, u32 context)
372 vl_api_classify_session_details_t *rmp;
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);
385 vl_msg_api_send_shmem (q, (u8 *) & rmp);
389 vl_api_classify_session_dump_t_handler (vl_api_classify_session_dump_t * mp)
391 vnet_classify_main_t *cm = &vnet_classify_main;
392 unix_shared_memory_queue_t *q;
394 u32 table_id = ntohl (mp->table_id);
395 vnet_classify_table_t *t;
397 q = vl_api_client_index_to_input_queue (mp->client_index);
402 pool_foreach (t, cm->tables,
404 if (table_id == t - cm->tables)
406 vnet_classify_bucket_t * b;
407 vnet_classify_entry_t * v, * save_v;
410 for (i = 0; i < t->nbuckets; i++)
416 save_v = vnet_classify_get_entry (t, b->offset);
417 for (j = 0; j < (1<<b->log2_pages); j++)
419 for (k = 0; k < t->entries_per_page; k++)
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))
426 send_classify_session_details
427 (q, table_id, t->match_n_vectors * sizeof (u32x4),
439 vl_api_flow_classify_set_interface_t_handler
440 (vl_api_flow_classify_set_interface_t * mp)
442 vlib_main_t *vm = vlib_get_main ();
443 vl_api_flow_classify_set_interface_reply_t *rmp;
445 u32 sw_if_index, ip4_table_index, ip6_table_index;
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);
451 VALIDATE_SW_IF_INDEX (mp);
453 rv = vnet_set_flow_classify_intfc (vm, sw_if_index, ip4_table_index,
454 ip6_table_index, mp->is_add);
456 BAD_SW_IF_INDEX_LABEL;
458 REPLY_MACRO (VL_API_FLOW_CLASSIFY_SET_INTERFACE_REPLY);
462 send_flow_classify_details (u32 sw_if_index,
464 unix_shared_memory_queue_t * q, u32 context)
466 vl_api_flow_classify_details_t *mp;
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);
475 vl_msg_api_send_shmem (q, (u8 *) & mp);
479 vl_api_flow_classify_dump_t_handler (vl_api_flow_classify_dump_t * mp)
481 unix_shared_memory_queue_t *q;
482 flow_classify_main_t *pcm = &flow_classify_main;
486 q = vl_api_client_index_to_input_queue (mp->client_index);
490 vec_tbl = pcm->classify_table_index_by_sw_if_index[mp->type];
492 if (vec_len (vec_tbl))
494 for (i = 0; i < vec_len (vec_tbl); i++)
496 if (vec_elt (vec_tbl, i) == ~0)
499 send_flow_classify_details (i, vec_elt (vec_tbl, i), q,
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()
512 #define vl_msg_name_crc_list
513 #include <vnet/vnet_all_api_h.h>
514 #undef vl_msg_name_crc_list
517 setup_message_id_table (api_main_t * am)
519 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
520 foreach_vl_msg_name_crc_classify;
524 static clib_error_t *
525 classify_api_hookup (vlib_main_t * vm)
527 api_main_t *am = &api_main;
530 vl_msg_api_set_handlers(VL_API_##N, #n, \
531 vl_api_##n##_t_handler, \
533 vl_api_##n##_t_endian, \
534 vl_api_##n##_t_print, \
535 sizeof(vl_api_##n##_t), 1);
540 * Set up the (msg_name, crc, message-id) table
542 setup_message_id_table (am);
547 VLIB_API_INIT_FUNCTION (classify_api_hookup);
550 * fd.io coding-style-patch-verification: ON
553 * eval: (c-set-style "gnu")