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>
30 #include <vnet/l2/l2_classify.h>
32 #include <vnet/vnet_msg_enum.h>
34 #define vl_typedefs /* define message structures */
35 #include <vnet/vnet_all_api_h.h>
38 #define vl_endianfun /* define message structures */
39 #include <vnet/vnet_all_api_h.h>
42 /* instantiate all the print functions we know about */
43 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
45 #include <vnet/vnet_all_api_h.h>
48 #include <vlibapi/api_helper_macros.h>
50 #define foreach_vpe_api_msg \
51 _(CLASSIFY_ADD_DEL_TABLE, classify_add_del_table) \
52 _(CLASSIFY_ADD_DEL_SESSION, classify_add_del_session) \
53 _(CLASSIFY_TABLE_IDS,classify_table_ids) \
54 _(CLASSIFY_TABLE_BY_INTERFACE, classify_table_by_interface) \
55 _(CLASSIFY_TABLE_INFO,classify_table_info) \
56 _(CLASSIFY_SESSION_DUMP,classify_session_dump) \
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 _(INPUT_ACL_SET_INTERFACE, input_acl_set_interface) \
62 _(CLASSIFY_SET_INTERFACE_IP_TABLE, classify_set_interface_ip_table) \
63 _(CLASSIFY_SET_INTERFACE_L2_TABLES, classify_set_interface_l2_tables)
65 #define foreach_classify_add_del_table_field \
73 _(current_data_flag) \
74 _(current_data_offset)
76 static void vl_api_classify_add_del_table_t_handler
77 (vl_api_classify_add_del_table_t * mp)
79 vl_api_classify_add_del_table_reply_t *rmp;
80 vnet_classify_main_t *cm = &vnet_classify_main;
81 vnet_classify_table_t *t;
85 foreach_classify_add_del_table_field;
88 #define _(a) a = ntohl(mp->a);
89 foreach_classify_add_del_table_field;
92 /* The underlying API fails silently, on purpose, so check here */
93 if (mp->is_add == 0) /* delete */
95 if (pool_is_free_index (cm->tables, table_index))
97 rv = VNET_API_ERROR_NO_SUCH_TABLE;
101 else /* add or update */
103 if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
107 rv = vnet_classify_add_del_table
108 (cm, mp->mask, nbuckets, memory_size,
109 skip_n_vectors, match_n_vectors,
110 next_table_index, miss_next_index, &table_index,
111 current_data_flag, current_data_offset, mp->is_add, mp->del_chain);
115 REPLY_MACRO2(VL_API_CLASSIFY_ADD_DEL_TABLE_REPLY,
117 if (rv == 0 && mp->is_add)
119 t = pool_elt_at_index (cm->tables, table_index);
120 rmp->skip_n_vectors = ntohl(t->skip_n_vectors);
121 rmp->match_n_vectors = ntohl(t->match_n_vectors);
122 rmp->new_table_index = ntohl(table_index);
126 rmp->skip_n_vectors = ~0;
127 rmp->match_n_vectors = ~0;
128 rmp->new_table_index = ~0;
134 static void vl_api_classify_add_del_session_t_handler
135 (vl_api_classify_add_del_session_t * mp)
137 vnet_classify_main_t *cm = &vnet_classify_main;
138 vl_api_classify_add_del_session_reply_t *rmp;
140 u32 table_index, hit_next_index, opaque_index, metadata;
144 table_index = ntohl (mp->table_index);
145 hit_next_index = ntohl (mp->hit_next_index);
146 opaque_index = ntohl (mp->opaque_index);
147 advance = ntohl (mp->advance);
149 metadata = ntohl (mp->metadata);
151 rv = vnet_classify_add_del_session
152 (cm, table_index, mp->match, hit_next_index, opaque_index,
153 advance, action, metadata, mp->is_add);
155 REPLY_MACRO (VL_API_CLASSIFY_ADD_DEL_SESSION_REPLY);
159 vl_api_policer_classify_set_interface_t_handler
160 (vl_api_policer_classify_set_interface_t * mp)
162 vlib_main_t *vm = vlib_get_main ();
163 vl_api_policer_classify_set_interface_reply_t *rmp;
165 u32 sw_if_index, ip4_table_index, ip6_table_index, l2_table_index;
167 ip4_table_index = ntohl (mp->ip4_table_index);
168 ip6_table_index = ntohl (mp->ip6_table_index);
169 l2_table_index = ntohl (mp->l2_table_index);
170 sw_if_index = ntohl (mp->sw_if_index);
172 VALIDATE_SW_IF_INDEX (mp);
174 rv = vnet_set_policer_classify_intfc (vm, sw_if_index, ip4_table_index,
175 ip6_table_index, l2_table_index,
178 BAD_SW_IF_INDEX_LABEL;
180 REPLY_MACRO (VL_API_POLICER_CLASSIFY_SET_INTERFACE_REPLY);
184 send_policer_classify_details (u32 sw_if_index,
185 u32 table_index, svm_queue_t * q, u32 context)
187 vl_api_policer_classify_details_t *mp;
189 mp = vl_msg_api_alloc (sizeof (*mp));
190 memset (mp, 0, sizeof (*mp));
191 mp->_vl_msg_id = ntohs (VL_API_POLICER_CLASSIFY_DETAILS);
192 mp->context = context;
193 mp->sw_if_index = htonl (sw_if_index);
194 mp->table_index = htonl (table_index);
196 vl_msg_api_send_shmem (q, (u8 *) & mp);
200 vl_api_policer_classify_dump_t_handler (vl_api_policer_classify_dump_t * mp)
203 policer_classify_main_t *pcm = &policer_classify_main;
207 q = vl_api_client_index_to_input_queue (mp->client_index);
211 vec_tbl = pcm->classify_table_index_by_sw_if_index[mp->type];
213 if (vec_len (vec_tbl))
215 for (i = 0; i < vec_len (vec_tbl); i++)
217 if (vec_elt (vec_tbl, i) == ~0)
220 send_policer_classify_details (i, vec_elt (vec_tbl, i), q,
227 vl_api_classify_table_ids_t_handler (vl_api_classify_table_ids_t * mp)
231 q = vl_api_client_index_to_input_queue (mp->client_index);
235 vnet_classify_main_t *cm = &vnet_classify_main;
236 vnet_classify_table_t *t;
241 pool_foreach (t, cm->tables,
243 vec_add1 (table_ids, ntohl(t - cm->tables));
246 count = vec_len (table_ids);
248 vl_api_classify_table_ids_reply_t *rmp;
249 rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp) + count * sizeof (u32));
250 rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_TABLE_IDS_REPLY);
251 rmp->context = mp->context;
252 rmp->count = ntohl (count);
253 clib_memcpy (rmp->ids, table_ids, count * sizeof (u32));
256 vl_msg_api_send_shmem (q, (u8 *) & rmp);
258 vec_free (table_ids);
262 vl_api_classify_table_by_interface_t_handler
263 (vl_api_classify_table_by_interface_t * mp)
265 vl_api_classify_table_by_interface_reply_t *rmp;
268 u32 sw_if_index = ntohl (mp->sw_if_index);
271 vec_validate (acl, INPUT_ACL_N_TABLES - 1);
274 VALIDATE_SW_IF_INDEX (mp);
276 input_acl_main_t *am = &input_acl_main;
281 for (type = 0; type < INPUT_ACL_N_TABLES; type++)
283 u32 *vec_tbl = am->classify_table_index_by_sw_if_index[type];
284 if (vec_len (vec_tbl))
286 for (if_idx = 0; if_idx < vec_len (vec_tbl); if_idx++)
288 if (vec_elt (vec_tbl, if_idx) == ~0 || sw_if_index != if_idx)
292 acl[type] = vec_elt (vec_tbl, if_idx);
297 BAD_SW_IF_INDEX_LABEL;
300 REPLY_MACRO2(VL_API_CLASSIFY_TABLE_BY_INTERFACE_REPLY,
302 rmp->sw_if_index = ntohl(sw_if_index);
303 rmp->l2_table_id = ntohl(acl[INPUT_ACL_TABLE_L2]);
304 rmp->ip4_table_id = ntohl(acl[INPUT_ACL_TABLE_IP4]);
305 rmp->ip6_table_id = ntohl(acl[INPUT_ACL_TABLE_IP6]);
312 vl_api_classify_table_info_t_handler (vl_api_classify_table_info_t * mp)
316 q = vl_api_client_index_to_input_queue (mp->client_index);
320 vl_api_classify_table_info_reply_t *rmp = 0;
322 vnet_classify_main_t *cm = &vnet_classify_main;
323 u32 table_id = ntohl (mp->table_id);
324 vnet_classify_table_t *t;
327 pool_foreach (t, cm->tables,
329 if (table_id == t - cm->tables)
331 rmp = vl_msg_api_alloc_as_if_client
332 (sizeof (*rmp) + t->match_n_vectors * sizeof (u32x4));
333 rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_TABLE_INFO_REPLY);
334 rmp->context = mp->context;
335 rmp->table_id = ntohl(table_id);
336 rmp->nbuckets = ntohl(t->nbuckets);
337 rmp->match_n_vectors = ntohl(t->match_n_vectors);
338 rmp->skip_n_vectors = ntohl(t->skip_n_vectors);
339 rmp->active_sessions = ntohl(t->active_elements);
340 rmp->next_table_index = ntohl(t->next_table_index);
341 rmp->miss_next_index = ntohl(t->miss_next_index);
342 rmp->mask_length = ntohl(t->match_n_vectors * sizeof (u32x4));
343 clib_memcpy(rmp->mask, t->mask, t->match_n_vectors * sizeof(u32x4));
352 rmp = vl_msg_api_alloc (sizeof (*rmp));
353 rmp->_vl_msg_id = ntohs ((VL_API_CLASSIFY_TABLE_INFO_REPLY));
354 rmp->context = mp->context;
355 rmp->retval = ntohl (VNET_API_ERROR_CLASSIFY_TABLE_NOT_FOUND);
358 vl_msg_api_send_shmem (q, (u8 *) & rmp);
362 send_classify_session_details (svm_queue_t * q,
365 vnet_classify_entry_t * e, u32 context)
367 vl_api_classify_session_details_t *rmp;
369 rmp = vl_msg_api_alloc (sizeof (*rmp));
370 memset (rmp, 0, sizeof (*rmp));
371 rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_SESSION_DETAILS);
372 rmp->context = context;
373 rmp->table_id = ntohl (table_id);
374 rmp->hit_next_index = ntohl (e->next_index);
375 rmp->advance = ntohl (e->advance);
376 rmp->opaque_index = ntohl (e->opaque_index);
377 rmp->match_length = ntohl (match_length);
378 clib_memcpy (rmp->match, e->key, match_length);
380 vl_msg_api_send_shmem (q, (u8 *) & rmp);
384 vl_api_classify_session_dump_t_handler (vl_api_classify_session_dump_t * mp)
386 vnet_classify_main_t *cm = &vnet_classify_main;
389 u32 table_id = ntohl (mp->table_id);
390 vnet_classify_table_t *t;
392 q = vl_api_client_index_to_input_queue (mp->client_index);
397 pool_foreach (t, cm->tables,
399 if (table_id == t - cm->tables)
401 vnet_classify_bucket_t * b;
402 vnet_classify_entry_t * v, * save_v;
405 for (i = 0; i < t->nbuckets; i++)
411 save_v = vnet_classify_get_entry (t, b->offset);
412 for (j = 0; j < (1<<b->log2_pages); j++)
414 for (k = 0; k < t->entries_per_page; k++)
416 v = vnet_classify_entry_at_index
417 (t, save_v, j*t->entries_per_page + k);
418 if (vnet_classify_entry_is_free (v))
421 send_classify_session_details
422 (q, table_id, t->match_n_vectors * sizeof (u32x4),
434 vl_api_flow_classify_set_interface_t_handler
435 (vl_api_flow_classify_set_interface_t * mp)
437 vlib_main_t *vm = vlib_get_main ();
438 vl_api_flow_classify_set_interface_reply_t *rmp;
440 u32 sw_if_index, ip4_table_index, ip6_table_index;
442 ip4_table_index = ntohl (mp->ip4_table_index);
443 ip6_table_index = ntohl (mp->ip6_table_index);
444 sw_if_index = ntohl (mp->sw_if_index);
446 VALIDATE_SW_IF_INDEX (mp);
448 rv = vnet_set_flow_classify_intfc (vm, sw_if_index, ip4_table_index,
449 ip6_table_index, mp->is_add);
451 BAD_SW_IF_INDEX_LABEL;
453 REPLY_MACRO (VL_API_FLOW_CLASSIFY_SET_INTERFACE_REPLY);
457 send_flow_classify_details (u32 sw_if_index,
458 u32 table_index, svm_queue_t * q, u32 context)
460 vl_api_flow_classify_details_t *mp;
462 mp = vl_msg_api_alloc (sizeof (*mp));
463 memset (mp, 0, sizeof (*mp));
464 mp->_vl_msg_id = ntohs (VL_API_FLOW_CLASSIFY_DETAILS);
465 mp->context = context;
466 mp->sw_if_index = htonl (sw_if_index);
467 mp->table_index = htonl (table_index);
469 vl_msg_api_send_shmem (q, (u8 *) & mp);
473 vl_api_flow_classify_dump_t_handler (vl_api_flow_classify_dump_t * mp)
476 flow_classify_main_t *pcm = &flow_classify_main;
480 q = vl_api_client_index_to_input_queue (mp->client_index);
484 vec_tbl = pcm->classify_table_index_by_sw_if_index[mp->type];
486 if (vec_len (vec_tbl))
488 for (i = 0; i < vec_len (vec_tbl); i++)
490 if (vec_elt (vec_tbl, i) == ~0)
493 send_flow_classify_details (i, vec_elt (vec_tbl, i), q,
499 static void vl_api_classify_set_interface_ip_table_t_handler
500 (vl_api_classify_set_interface_ip_table_t * mp)
502 vlib_main_t *vm = vlib_get_main ();
503 vl_api_classify_set_interface_ip_table_reply_t *rmp;
506 VALIDATE_SW_IF_INDEX (mp);
508 u32 table_index = ntohl (mp->table_index);
509 u32 sw_if_index = ntohl (mp->sw_if_index);
512 rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
514 rv = vnet_set_ip4_classify_intfc (vm, sw_if_index, table_index);
516 BAD_SW_IF_INDEX_LABEL;
518 REPLY_MACRO (VL_API_CLASSIFY_SET_INTERFACE_IP_TABLE_REPLY);
521 static void vl_api_classify_set_interface_l2_tables_t_handler
522 (vl_api_classify_set_interface_l2_tables_t * mp)
524 vl_api_classify_set_interface_l2_tables_reply_t *rmp;
526 u32 sw_if_index, ip4_table_index, ip6_table_index, other_table_index;
529 ip4_table_index = ntohl (mp->ip4_table_index);
530 ip6_table_index = ntohl (mp->ip6_table_index);
531 other_table_index = ntohl (mp->other_table_index);
532 sw_if_index = ntohl (mp->sw_if_index);
534 VALIDATE_SW_IF_INDEX (mp);
537 rv = vnet_l2_input_classify_set_tables (sw_if_index, ip4_table_index,
541 rv = vnet_l2_output_classify_set_tables (sw_if_index, ip4_table_index,
547 if (ip4_table_index != ~0 || ip6_table_index != ~0
548 || other_table_index != ~0)
554 vnet_l2_input_classify_enable_disable (sw_if_index, enable);
556 vnet_l2_output_classify_enable_disable (sw_if_index, enable);
559 BAD_SW_IF_INDEX_LABEL;
561 REPLY_MACRO (VL_API_CLASSIFY_SET_INTERFACE_L2_TABLES_REPLY);
564 static void vl_api_input_acl_set_interface_t_handler
565 (vl_api_input_acl_set_interface_t * mp)
567 vlib_main_t *vm = vlib_get_main ();
568 vl_api_input_acl_set_interface_reply_t *rmp;
571 VALIDATE_SW_IF_INDEX (mp);
573 u32 ip4_table_index = ntohl (mp->ip4_table_index);
574 u32 ip6_table_index = ntohl (mp->ip6_table_index);
575 u32 l2_table_index = ntohl (mp->l2_table_index);
576 u32 sw_if_index = ntohl (mp->sw_if_index);
578 rv = vnet_set_input_acl_intfc (vm, sw_if_index, ip4_table_index,
579 ip6_table_index, l2_table_index, mp->is_add);
581 BAD_SW_IF_INDEX_LABEL;
583 REPLY_MACRO (VL_API_INPUT_ACL_SET_INTERFACE_REPLY);
587 * classify_api_hookup
588 * Add vpe's API message handlers to the table.
589 * vlib has alread mapped shared memory and
590 * added the client registration handlers.
591 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
593 #define vl_msg_name_crc_list
594 #include <vnet/vnet_all_api_h.h>
595 #undef vl_msg_name_crc_list
598 setup_message_id_table (api_main_t * am)
600 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
601 foreach_vl_msg_name_crc_classify;
605 static clib_error_t *
606 classify_api_hookup (vlib_main_t * vm)
608 api_main_t *am = &api_main;
611 vl_msg_api_set_handlers(VL_API_##N, #n, \
612 vl_api_##n##_t_handler, \
614 vl_api_##n##_t_endian, \
615 vl_api_##n##_t_print, \
616 sizeof(vl_api_##n##_t), 1);
621 * Set up the (msg_name, crc, message-id) table
623 setup_message_id_table (am);
628 VLIB_API_INIT_FUNCTION (classify_api_hookup);
631 * fd.io coding-style-patch-verification: ON
634 * eval: (c-set-style "gnu")