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/in_out_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) \
64 _(OUTPUT_ACL_SET_INTERFACE, output_acl_set_interface)
66 #define foreach_classify_add_del_table_field \
74 _(current_data_flag) \
75 _(current_data_offset) \
78 static void vl_api_classify_add_del_table_t_handler
79 (vl_api_classify_add_del_table_t * mp)
81 vl_api_classify_add_del_table_reply_t *rmp;
82 vnet_classify_main_t *cm = &vnet_classify_main;
83 vnet_classify_table_t *t;
87 foreach_classify_add_del_table_field;
90 #define _(a) a = ntohl(mp->a);
91 foreach_classify_add_del_table_field;
94 if (mask_len != match_n_vectors * sizeof (u32x4))
96 rv = VNET_API_ERROR_INVALID_VALUE;
100 /* The underlying API fails silently, on purpose, so check here */
101 if (mp->is_add == 0) /* delete */
103 if (pool_is_free_index (cm->tables, table_index))
105 rv = VNET_API_ERROR_NO_SUCH_TABLE;
109 else /* add or update */
111 if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
115 rv = vnet_classify_add_del_table
116 (cm, mp->mask, nbuckets, memory_size,
117 skip_n_vectors, match_n_vectors,
118 next_table_index, miss_next_index, &table_index,
119 current_data_flag, current_data_offset, mp->is_add, mp->del_chain);
123 REPLY_MACRO2(VL_API_CLASSIFY_ADD_DEL_TABLE_REPLY,
125 if (rv == 0 && mp->is_add)
127 t = pool_elt_at_index (cm->tables, table_index);
128 rmp->skip_n_vectors = ntohl(t->skip_n_vectors);
129 rmp->match_n_vectors = ntohl(t->match_n_vectors);
130 rmp->new_table_index = ntohl(table_index);
134 rmp->skip_n_vectors = ~0;
135 rmp->match_n_vectors = ~0;
136 rmp->new_table_index = ~0;
142 static void vl_api_classify_add_del_session_t_handler
143 (vl_api_classify_add_del_session_t * mp)
145 vnet_classify_main_t *cm = &vnet_classify_main;
146 vl_api_classify_add_del_session_reply_t *rmp;
148 u32 table_index, hit_next_index, opaque_index, metadata, match_len;
151 vnet_classify_table_t *t;
153 table_index = ntohl (mp->table_index);
154 hit_next_index = ntohl (mp->hit_next_index);
155 opaque_index = ntohl (mp->opaque_index);
156 advance = ntohl (mp->advance);
158 metadata = ntohl (mp->metadata);
159 match_len = ntohl (mp->match_len);
161 if (pool_is_free_index (cm->tables, table_index))
163 rv = VNET_API_ERROR_NO_SUCH_TABLE;
167 t = pool_elt_at_index (cm->tables, table_index);
169 if (match_len != (t->skip_n_vectors + t->match_n_vectors) * sizeof (u32x4))
171 rv = VNET_API_ERROR_INVALID_VALUE;
175 rv = vnet_classify_add_del_session
176 (cm, table_index, mp->match, hit_next_index, opaque_index,
177 advance, action, metadata, mp->is_add);
180 REPLY_MACRO (VL_API_CLASSIFY_ADD_DEL_SESSION_REPLY);
184 vl_api_policer_classify_set_interface_t_handler
185 (vl_api_policer_classify_set_interface_t * mp)
187 vlib_main_t *vm = vlib_get_main ();
188 vl_api_policer_classify_set_interface_reply_t *rmp;
190 u32 sw_if_index, ip4_table_index, ip6_table_index, l2_table_index;
192 ip4_table_index = ntohl (mp->ip4_table_index);
193 ip6_table_index = ntohl (mp->ip6_table_index);
194 l2_table_index = ntohl (mp->l2_table_index);
195 sw_if_index = ntohl (mp->sw_if_index);
197 VALIDATE_SW_IF_INDEX (mp);
199 rv = vnet_set_policer_classify_intfc (vm, sw_if_index, ip4_table_index,
200 ip6_table_index, l2_table_index,
203 BAD_SW_IF_INDEX_LABEL;
205 REPLY_MACRO (VL_API_POLICER_CLASSIFY_SET_INTERFACE_REPLY);
209 send_policer_classify_details (u32 sw_if_index,
210 u32 table_index, vl_api_registration_t * reg,
213 vl_api_policer_classify_details_t *mp;
215 mp = vl_msg_api_alloc (sizeof (*mp));
216 clib_memset (mp, 0, sizeof (*mp));
217 mp->_vl_msg_id = ntohs (VL_API_POLICER_CLASSIFY_DETAILS);
218 mp->context = context;
219 mp->sw_if_index = htonl (sw_if_index);
220 mp->table_index = htonl (table_index);
222 vl_api_send_msg (reg, (u8 *) mp);
226 vl_api_policer_classify_dump_t_handler (vl_api_policer_classify_dump_t * mp)
228 vl_api_registration_t *reg;
229 policer_classify_main_t *pcm = &policer_classify_main;
233 reg = vl_api_client_index_to_registration (mp->client_index);
237 vec_tbl = pcm->classify_table_index_by_sw_if_index[mp->type];
239 if (vec_len (vec_tbl))
241 for (i = 0; i < vec_len (vec_tbl); i++)
243 if (vec_elt (vec_tbl, i) == ~0)
246 send_policer_classify_details (i, vec_elt (vec_tbl, i), reg,
253 vl_api_classify_table_ids_t_handler (vl_api_classify_table_ids_t * mp)
255 vl_api_registration_t *reg;
257 reg = vl_api_client_index_to_registration (mp->client_index);
261 vnet_classify_main_t *cm = &vnet_classify_main;
262 vnet_classify_table_t *t;
267 pool_foreach (t, cm->tables,
269 vec_add1 (table_ids, ntohl(t - cm->tables));
272 count = vec_len (table_ids);
274 vl_api_classify_table_ids_reply_t *rmp;
275 rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp) + count * sizeof (u32));
276 rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_TABLE_IDS_REPLY);
277 rmp->context = mp->context;
278 rmp->count = ntohl (count);
279 clib_memcpy (rmp->ids, table_ids, count * sizeof (u32));
282 vl_api_send_msg (reg, (u8 *) rmp);
284 vec_free (table_ids);
288 vl_api_classify_table_by_interface_t_handler
289 (vl_api_classify_table_by_interface_t * mp)
291 vl_api_classify_table_by_interface_reply_t *rmp;
294 u32 sw_if_index = ntohl (mp->sw_if_index);
297 vec_validate (acl, IN_OUT_ACL_N_TABLES - 1);
300 VALIDATE_SW_IF_INDEX (mp);
302 in_out_acl_main_t *am = &in_out_acl_main;
307 for (type = 0; type < IN_OUT_ACL_N_TABLES; type++)
310 am->classify_table_index_by_sw_if_index[IN_OUT_ACL_INPUT_TABLE_GROUP]
312 if (vec_len (vec_tbl))
314 for (if_idx = 0; if_idx < vec_len (vec_tbl); if_idx++)
316 if (vec_elt (vec_tbl, if_idx) == ~0 || sw_if_index != if_idx)
320 acl[type] = vec_elt (vec_tbl, if_idx);
325 BAD_SW_IF_INDEX_LABEL;
328 REPLY_MACRO2(VL_API_CLASSIFY_TABLE_BY_INTERFACE_REPLY,
330 rmp->sw_if_index = ntohl(sw_if_index);
331 rmp->l2_table_id = ntohl(acl[IN_OUT_ACL_TABLE_L2]);
332 rmp->ip4_table_id = ntohl(acl[IN_OUT_ACL_TABLE_IP4]);
333 rmp->ip6_table_id = ntohl(acl[IN_OUT_ACL_TABLE_IP6]);
340 vl_api_classify_table_info_t_handler (vl_api_classify_table_info_t * mp)
342 vl_api_registration_t *reg;
344 reg = vl_api_client_index_to_registration (mp->client_index);
348 vl_api_classify_table_info_reply_t *rmp = 0;
350 vnet_classify_main_t *cm = &vnet_classify_main;
351 u32 table_id = ntohl (mp->table_id);
352 vnet_classify_table_t *t;
355 pool_foreach (t, cm->tables,
357 if (table_id == t - cm->tables)
359 rmp = vl_msg_api_alloc_as_if_client
360 (sizeof (*rmp) + t->match_n_vectors * sizeof (u32x4));
361 rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_TABLE_INFO_REPLY);
362 rmp->context = mp->context;
363 rmp->table_id = ntohl(table_id);
364 rmp->nbuckets = ntohl(t->nbuckets);
365 rmp->match_n_vectors = ntohl(t->match_n_vectors);
366 rmp->skip_n_vectors = ntohl(t->skip_n_vectors);
367 rmp->active_sessions = ntohl(t->active_elements);
368 rmp->next_table_index = ntohl(t->next_table_index);
369 rmp->miss_next_index = ntohl(t->miss_next_index);
370 rmp->mask_length = ntohl(t->match_n_vectors * sizeof (u32x4));
371 clib_memcpy(rmp->mask, t->mask, t->match_n_vectors * sizeof(u32x4));
380 rmp = vl_msg_api_alloc (sizeof (*rmp));
381 rmp->_vl_msg_id = ntohs ((VL_API_CLASSIFY_TABLE_INFO_REPLY));
382 rmp->context = mp->context;
383 rmp->retval = ntohl (VNET_API_ERROR_CLASSIFY_TABLE_NOT_FOUND);
386 vl_api_send_msg (reg, (u8 *) rmp);
390 send_classify_session_details (vl_api_registration_t * reg,
393 vnet_classify_entry_t * e, u32 context)
395 vl_api_classify_session_details_t *rmp;
397 rmp = vl_msg_api_alloc (sizeof (*rmp));
398 clib_memset (rmp, 0, sizeof (*rmp));
399 rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_SESSION_DETAILS);
400 rmp->context = context;
401 rmp->table_id = ntohl (table_id);
402 rmp->hit_next_index = ntohl (e->next_index);
403 rmp->advance = ntohl (e->advance);
404 rmp->opaque_index = ntohl (e->opaque_index);
405 rmp->match_length = ntohl (match_length);
406 clib_memcpy (rmp->match, e->key, match_length);
408 vl_api_send_msg (reg, (u8 *) rmp);
412 vl_api_classify_session_dump_t_handler (vl_api_classify_session_dump_t * mp)
414 vnet_classify_main_t *cm = &vnet_classify_main;
415 vl_api_registration_t *reg;
417 u32 table_id = ntohl (mp->table_id);
418 vnet_classify_table_t *t;
420 reg = vl_api_client_index_to_registration (mp->client_index);
425 pool_foreach (t, cm->tables,
427 if (table_id == t - cm->tables)
429 vnet_classify_bucket_t * b;
430 vnet_classify_entry_t * v, * save_v;
433 for (i = 0; i < t->nbuckets; i++)
439 save_v = vnet_classify_get_entry (t, b->offset);
440 for (j = 0; j < (1<<b->log2_pages); j++)
442 for (k = 0; k < t->entries_per_page; k++)
444 v = vnet_classify_entry_at_index
445 (t, save_v, j*t->entries_per_page + k);
446 if (vnet_classify_entry_is_free (v))
449 send_classify_session_details
450 (reg, table_id, t->match_n_vectors * sizeof (u32x4),
462 vl_api_flow_classify_set_interface_t_handler
463 (vl_api_flow_classify_set_interface_t * mp)
465 vlib_main_t *vm = vlib_get_main ();
466 vl_api_flow_classify_set_interface_reply_t *rmp;
468 u32 sw_if_index, ip4_table_index, ip6_table_index;
470 ip4_table_index = ntohl (mp->ip4_table_index);
471 ip6_table_index = ntohl (mp->ip6_table_index);
472 sw_if_index = ntohl (mp->sw_if_index);
474 VALIDATE_SW_IF_INDEX (mp);
476 rv = vnet_set_flow_classify_intfc (vm, sw_if_index, ip4_table_index,
477 ip6_table_index, mp->is_add);
479 BAD_SW_IF_INDEX_LABEL;
481 REPLY_MACRO (VL_API_FLOW_CLASSIFY_SET_INTERFACE_REPLY);
485 send_flow_classify_details (u32 sw_if_index,
486 u32 table_index, vl_api_registration_t * reg,
489 vl_api_flow_classify_details_t *mp;
491 mp = vl_msg_api_alloc (sizeof (*mp));
492 clib_memset (mp, 0, sizeof (*mp));
493 mp->_vl_msg_id = ntohs (VL_API_FLOW_CLASSIFY_DETAILS);
494 mp->context = context;
495 mp->sw_if_index = htonl (sw_if_index);
496 mp->table_index = htonl (table_index);
498 vl_api_send_msg (reg, (u8 *) mp);
502 vl_api_flow_classify_dump_t_handler (vl_api_flow_classify_dump_t * mp)
504 vl_api_registration_t *reg;
505 flow_classify_main_t *pcm = &flow_classify_main;
509 reg = vl_api_client_index_to_registration (mp->client_index);
513 vec_tbl = pcm->classify_table_index_by_sw_if_index[mp->type];
515 if (vec_len (vec_tbl))
517 for (i = 0; i < vec_len (vec_tbl); i++)
519 if (vec_elt (vec_tbl, i) == ~0)
522 send_flow_classify_details (i, vec_elt (vec_tbl, i), reg,
528 static void vl_api_classify_set_interface_ip_table_t_handler
529 (vl_api_classify_set_interface_ip_table_t * mp)
531 vlib_main_t *vm = vlib_get_main ();
532 vl_api_classify_set_interface_ip_table_reply_t *rmp;
535 VALIDATE_SW_IF_INDEX (mp);
537 u32 table_index = ntohl (mp->table_index);
538 u32 sw_if_index = ntohl (mp->sw_if_index);
541 rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
543 rv = vnet_set_ip4_classify_intfc (vm, sw_if_index, table_index);
545 BAD_SW_IF_INDEX_LABEL;
547 REPLY_MACRO (VL_API_CLASSIFY_SET_INTERFACE_IP_TABLE_REPLY);
550 static void vl_api_classify_set_interface_l2_tables_t_handler
551 (vl_api_classify_set_interface_l2_tables_t * mp)
553 vl_api_classify_set_interface_l2_tables_reply_t *rmp;
555 u32 sw_if_index, ip4_table_index, ip6_table_index, other_table_index;
558 ip4_table_index = ntohl (mp->ip4_table_index);
559 ip6_table_index = ntohl (mp->ip6_table_index);
560 other_table_index = ntohl (mp->other_table_index);
561 sw_if_index = ntohl (mp->sw_if_index);
563 VALIDATE_SW_IF_INDEX (mp);
566 rv = vnet_l2_input_classify_set_tables (sw_if_index, ip4_table_index,
570 rv = vnet_l2_output_classify_set_tables (sw_if_index, ip4_table_index,
576 if (ip4_table_index != ~0 || ip6_table_index != ~0
577 || other_table_index != ~0)
583 vnet_l2_input_classify_enable_disable (sw_if_index, enable);
585 vnet_l2_output_classify_enable_disable (sw_if_index, enable);
588 BAD_SW_IF_INDEX_LABEL;
590 REPLY_MACRO (VL_API_CLASSIFY_SET_INTERFACE_L2_TABLES_REPLY);
593 static void vl_api_input_acl_set_interface_t_handler
594 (vl_api_input_acl_set_interface_t * mp)
596 vlib_main_t *vm = vlib_get_main ();
597 vl_api_input_acl_set_interface_reply_t *rmp;
600 VALIDATE_SW_IF_INDEX (mp);
602 u32 ip4_table_index = ntohl (mp->ip4_table_index);
603 u32 ip6_table_index = ntohl (mp->ip6_table_index);
604 u32 l2_table_index = ntohl (mp->l2_table_index);
605 u32 sw_if_index = ntohl (mp->sw_if_index);
607 rv = vnet_set_input_acl_intfc (vm, sw_if_index, ip4_table_index,
608 ip6_table_index, l2_table_index, mp->is_add);
610 BAD_SW_IF_INDEX_LABEL;
612 REPLY_MACRO (VL_API_INPUT_ACL_SET_INTERFACE_REPLY);
615 static void vl_api_output_acl_set_interface_t_handler
616 (vl_api_output_acl_set_interface_t * mp)
618 vlib_main_t *vm = vlib_get_main ();
619 vl_api_output_acl_set_interface_reply_t *rmp;
622 VALIDATE_SW_IF_INDEX (mp);
624 u32 ip4_table_index = ntohl (mp->ip4_table_index);
625 u32 ip6_table_index = ntohl (mp->ip6_table_index);
626 u32 l2_table_index = ntohl (mp->l2_table_index);
627 u32 sw_if_index = ntohl (mp->sw_if_index);
629 rv = vnet_set_output_acl_intfc (vm, sw_if_index, ip4_table_index,
630 ip6_table_index, l2_table_index,
633 BAD_SW_IF_INDEX_LABEL;
635 REPLY_MACRO (VL_API_OUTPUT_ACL_SET_INTERFACE_REPLY);
639 * classify_api_hookup
640 * Add vpe's API message handlers to the table.
641 * vlib has alread mapped shared memory and
642 * added the client registration handlers.
643 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
645 #define vl_msg_name_crc_list
646 #include <vnet/vnet_all_api_h.h>
647 #undef vl_msg_name_crc_list
650 setup_message_id_table (api_main_t * am)
652 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
653 foreach_vl_msg_name_crc_classify;
657 static clib_error_t *
658 classify_api_hookup (vlib_main_t * vm)
660 api_main_t *am = &api_main;
663 vl_msg_api_set_handlers(VL_API_##N, #n, \
664 vl_api_##n##_t_handler, \
666 vl_api_##n##_t_endian, \
667 vl_api_##n##_t_print, \
668 sizeof(vl_api_##n##_t), 1);
673 * Set up the (msg_name, crc, message-id) table
675 setup_message_id_table (am);
680 VLIB_API_INIT_FUNCTION (classify_api_hookup);
683 * fd.io coding-style-patch-verification: ON
686 * eval: (c-set-style "gnu")