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>
31 #include <vnet/ip/ip6.h>
32 #include <vnet/ip/ip4.h>
34 #include <vnet/vnet_msg_enum.h>
36 #define vl_typedefs /* define message structures */
37 #include <vnet/vnet_all_api_h.h>
40 #define vl_endianfun /* define message structures */
41 #include <vnet/vnet_all_api_h.h>
44 /* instantiate all the print functions we know about */
45 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
47 #include <vnet/vnet_all_api_h.h>
50 #include <vlibapi/api_helper_macros.h>
52 #define foreach_vpe_api_msg \
53 _(CLASSIFY_ADD_DEL_TABLE, classify_add_del_table) \
54 _(CLASSIFY_ADD_DEL_SESSION, classify_add_del_session) \
55 _(CLASSIFY_TABLE_IDS,classify_table_ids) \
56 _(CLASSIFY_TABLE_BY_INTERFACE, classify_table_by_interface) \
57 _(CLASSIFY_TABLE_INFO,classify_table_info) \
58 _(CLASSIFY_SESSION_DUMP,classify_session_dump) \
59 _(POLICER_CLASSIFY_SET_INTERFACE, policer_classify_set_interface) \
60 _(POLICER_CLASSIFY_DUMP, policer_classify_dump) \
61 _(FLOW_CLASSIFY_SET_INTERFACE, flow_classify_set_interface) \
62 _(FLOW_CLASSIFY_DUMP, flow_classify_dump) \
63 _(INPUT_ACL_SET_INTERFACE, input_acl_set_interface) \
64 _(CLASSIFY_SET_INTERFACE_IP_TABLE, classify_set_interface_ip_table) \
65 _(CLASSIFY_SET_INTERFACE_L2_TABLES, classify_set_interface_l2_tables) \
66 _(OUTPUT_ACL_SET_INTERFACE, output_acl_set_interface)
68 #define foreach_classify_add_del_table_field \
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 u8 current_data_flag = mp->current_data_flag;
116 i16 current_data_offset = clib_net_to_host_i16 (mp->current_data_offset);
118 rv = vnet_classify_add_del_table
119 (cm, mp->mask, nbuckets, memory_size,
120 skip_n_vectors, match_n_vectors,
121 next_table_index, miss_next_index, &table_index,
122 current_data_flag, current_data_offset, mp->is_add, mp->del_chain);
126 REPLY_MACRO2(VL_API_CLASSIFY_ADD_DEL_TABLE_REPLY,
128 if (rv == 0 && mp->is_add)
130 t = pool_elt_at_index (cm->tables, table_index);
131 rmp->skip_n_vectors = htonl(t->skip_n_vectors);
132 rmp->match_n_vectors = htonl(t->match_n_vectors);
133 rmp->new_table_index = htonl(table_index);
137 rmp->skip_n_vectors = ~0;
138 rmp->match_n_vectors = ~0;
139 rmp->new_table_index = ~0;
145 static void vl_api_classify_add_del_session_t_handler
146 (vl_api_classify_add_del_session_t * mp)
148 vnet_classify_main_t *cm = &vnet_classify_main;
149 vl_api_classify_add_del_session_reply_t *rmp;
151 u32 table_index, hit_next_index, opaque_index, metadata, match_len;
154 vnet_classify_table_t *t;
156 table_index = ntohl (mp->table_index);
157 hit_next_index = ntohl (mp->hit_next_index);
158 opaque_index = ntohl (mp->opaque_index);
159 advance = ntohl (mp->advance);
161 metadata = ntohl (mp->metadata);
162 match_len = ntohl (mp->match_len);
164 if (pool_is_free_index (cm->tables, table_index))
166 rv = VNET_API_ERROR_NO_SUCH_TABLE;
170 t = pool_elt_at_index (cm->tables, table_index);
172 if (match_len != (t->skip_n_vectors + t->match_n_vectors) * sizeof (u32x4))
174 rv = VNET_API_ERROR_INVALID_VALUE;
178 rv = vnet_classify_add_del_session
179 (cm, table_index, mp->match, hit_next_index, opaque_index,
180 advance, action, metadata, mp->is_add);
183 REPLY_MACRO (VL_API_CLASSIFY_ADD_DEL_SESSION_REPLY);
187 vl_api_policer_classify_set_interface_t_handler
188 (vl_api_policer_classify_set_interface_t * mp)
190 vlib_main_t *vm = vlib_get_main ();
191 vl_api_policer_classify_set_interface_reply_t *rmp;
193 u32 sw_if_index, ip4_table_index, ip6_table_index, l2_table_index;
195 ip4_table_index = ntohl (mp->ip4_table_index);
196 ip6_table_index = ntohl (mp->ip6_table_index);
197 l2_table_index = ntohl (mp->l2_table_index);
198 sw_if_index = ntohl (mp->sw_if_index);
200 VALIDATE_SW_IF_INDEX (mp);
202 rv = vnet_set_policer_classify_intfc (vm, sw_if_index, ip4_table_index,
203 ip6_table_index, l2_table_index,
206 BAD_SW_IF_INDEX_LABEL;
208 REPLY_MACRO (VL_API_POLICER_CLASSIFY_SET_INTERFACE_REPLY);
212 send_policer_classify_details (u32 sw_if_index,
213 u32 table_index, vl_api_registration_t * reg,
216 vl_api_policer_classify_details_t *mp;
218 mp = vl_msg_api_alloc (sizeof (*mp));
219 clib_memset (mp, 0, sizeof (*mp));
220 mp->_vl_msg_id = ntohs (VL_API_POLICER_CLASSIFY_DETAILS);
221 mp->context = context;
222 mp->sw_if_index = htonl (sw_if_index);
223 mp->table_index = htonl (table_index);
225 vl_api_send_msg (reg, (u8 *) mp);
229 vl_api_policer_classify_dump_t_handler (vl_api_policer_classify_dump_t * mp)
231 vl_api_registration_t *reg;
232 policer_classify_main_t *pcm = &policer_classify_main;
235 u32 filter_sw_if_index;
237 reg = vl_api_client_index_to_registration (mp->client_index);
241 filter_sw_if_index = ntohl (mp->sw_if_index);
242 if (filter_sw_if_index
243 >= vec_len (pcm->classify_table_index_by_sw_if_index[mp->type]))
246 if (filter_sw_if_index != ~0)
248 &pcm->classify_table_index_by_sw_if_index[mp->type][filter_sw_if_index];
250 vec_tbl = pcm->classify_table_index_by_sw_if_index[mp->type];
252 if (vec_len (vec_tbl))
254 for (i = 0; i < vec_len (vec_tbl); i++)
256 if (vec_elt (vec_tbl, i) == ~0)
259 send_policer_classify_details (i, vec_elt (vec_tbl, i), reg,
266 vl_api_classify_table_ids_t_handler (vl_api_classify_table_ids_t * mp)
268 vl_api_registration_t *reg;
270 reg = vl_api_client_index_to_registration (mp->client_index);
274 vnet_classify_main_t *cm = &vnet_classify_main;
275 vnet_classify_table_t *t;
280 pool_foreach (t, cm->tables,
282 vec_add1 (table_ids, ntohl(t - cm->tables));
285 count = vec_len (table_ids);
287 vl_api_classify_table_ids_reply_t *rmp;
288 rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp) + count * sizeof (u32));
289 rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_TABLE_IDS_REPLY);
290 rmp->context = mp->context;
291 rmp->count = ntohl (count);
292 clib_memcpy (rmp->ids, table_ids, count * sizeof (u32));
295 vl_api_send_msg (reg, (u8 *) rmp);
297 vec_free (table_ids);
301 vl_api_classify_table_by_interface_t_handler
302 (vl_api_classify_table_by_interface_t * mp)
304 vl_api_classify_table_by_interface_reply_t *rmp;
307 u32 sw_if_index = ntohl (mp->sw_if_index);
310 vec_validate (acl, IN_OUT_ACL_N_TABLES - 1);
313 VALIDATE_SW_IF_INDEX (mp);
315 in_out_acl_main_t *am = &in_out_acl_main;
320 for (type = 0; type < IN_OUT_ACL_N_TABLES; type++)
323 am->classify_table_index_by_sw_if_index[IN_OUT_ACL_INPUT_TABLE_GROUP]
325 if (vec_len (vec_tbl))
327 for (if_idx = 0; if_idx < vec_len (vec_tbl); if_idx++)
329 if (vec_elt (vec_tbl, if_idx) == ~0 || sw_if_index != if_idx)
333 acl[type] = vec_elt (vec_tbl, if_idx);
338 BAD_SW_IF_INDEX_LABEL;
341 REPLY_MACRO2(VL_API_CLASSIFY_TABLE_BY_INTERFACE_REPLY,
343 rmp->sw_if_index = ntohl(sw_if_index);
344 rmp->l2_table_id = ntohl(acl[IN_OUT_ACL_TABLE_L2]);
345 rmp->ip4_table_id = ntohl(acl[IN_OUT_ACL_TABLE_IP4]);
346 rmp->ip6_table_id = ntohl(acl[IN_OUT_ACL_TABLE_IP6]);
353 vl_api_classify_table_info_t_handler (vl_api_classify_table_info_t * mp)
355 vl_api_registration_t *reg;
357 reg = vl_api_client_index_to_registration (mp->client_index);
361 vl_api_classify_table_info_reply_t *rmp = 0;
363 vnet_classify_main_t *cm = &vnet_classify_main;
364 u32 table_id = ntohl (mp->table_id);
365 vnet_classify_table_t *t;
368 pool_foreach (t, cm->tables,
370 if (table_id == t - cm->tables)
372 rmp = vl_msg_api_alloc_as_if_client
373 (sizeof (*rmp) + t->match_n_vectors * sizeof (u32x4));
374 rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_TABLE_INFO_REPLY);
375 rmp->context = mp->context;
376 rmp->table_id = ntohl(table_id);
377 rmp->nbuckets = ntohl(t->nbuckets);
378 rmp->match_n_vectors = ntohl(t->match_n_vectors);
379 rmp->skip_n_vectors = ntohl(t->skip_n_vectors);
380 rmp->active_sessions = ntohl(t->active_elements);
381 rmp->next_table_index = ntohl(t->next_table_index);
382 rmp->miss_next_index = ntohl(t->miss_next_index);
383 rmp->mask_length = ntohl(t->match_n_vectors * sizeof (u32x4));
384 clib_memcpy(rmp->mask, t->mask, t->match_n_vectors * sizeof(u32x4));
393 rmp = vl_msg_api_alloc (sizeof (*rmp));
394 rmp->_vl_msg_id = ntohs ((VL_API_CLASSIFY_TABLE_INFO_REPLY));
395 rmp->context = mp->context;
396 rmp->retval = ntohl (VNET_API_ERROR_CLASSIFY_TABLE_NOT_FOUND);
399 vl_api_send_msg (reg, (u8 *) rmp);
403 send_classify_session_details (vl_api_registration_t * reg,
406 vnet_classify_entry_t * e, u32 context)
408 vl_api_classify_session_details_t *rmp;
410 rmp = vl_msg_api_alloc (sizeof (*rmp));
411 clib_memset (rmp, 0, sizeof (*rmp));
412 rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_SESSION_DETAILS);
413 rmp->context = context;
414 rmp->table_id = ntohl (table_id);
415 rmp->hit_next_index = ntohl (e->next_index);
416 rmp->advance = ntohl (e->advance);
417 rmp->opaque_index = ntohl (e->opaque_index);
418 rmp->match_length = ntohl (match_length);
419 clib_memcpy (rmp->match, e->key, match_length);
421 vl_api_send_msg (reg, (u8 *) rmp);
425 vl_api_classify_session_dump_t_handler (vl_api_classify_session_dump_t * mp)
427 vnet_classify_main_t *cm = &vnet_classify_main;
428 vl_api_registration_t *reg;
430 u32 table_id = ntohl (mp->table_id);
431 vnet_classify_table_t *t;
433 reg = vl_api_client_index_to_registration (mp->client_index);
438 pool_foreach (t, cm->tables,
440 if (table_id == t - cm->tables)
442 vnet_classify_bucket_t * b;
443 vnet_classify_entry_t * v, * save_v;
446 for (i = 0; i < t->nbuckets; i++)
452 save_v = vnet_classify_get_entry (t, b->offset);
453 for (j = 0; j < (1<<b->log2_pages); j++)
455 for (k = 0; k < t->entries_per_page; k++)
457 v = vnet_classify_entry_at_index
458 (t, save_v, j*t->entries_per_page + k);
459 if (vnet_classify_entry_is_free (v))
462 send_classify_session_details
463 (reg, table_id, t->match_n_vectors * sizeof (u32x4),
475 vl_api_flow_classify_set_interface_t_handler
476 (vl_api_flow_classify_set_interface_t * mp)
478 vlib_main_t *vm = vlib_get_main ();
479 vl_api_flow_classify_set_interface_reply_t *rmp;
481 u32 sw_if_index, ip4_table_index, ip6_table_index;
483 ip4_table_index = ntohl (mp->ip4_table_index);
484 ip6_table_index = ntohl (mp->ip6_table_index);
485 sw_if_index = ntohl (mp->sw_if_index);
487 VALIDATE_SW_IF_INDEX (mp);
489 rv = vnet_set_flow_classify_intfc (vm, sw_if_index, ip4_table_index,
490 ip6_table_index, mp->is_add);
492 BAD_SW_IF_INDEX_LABEL;
494 REPLY_MACRO (VL_API_FLOW_CLASSIFY_SET_INTERFACE_REPLY);
498 send_flow_classify_details (u32 sw_if_index,
499 u32 table_index, vl_api_registration_t * reg,
502 vl_api_flow_classify_details_t *mp;
504 mp = vl_msg_api_alloc (sizeof (*mp));
505 clib_memset (mp, 0, sizeof (*mp));
506 mp->_vl_msg_id = ntohs (VL_API_FLOW_CLASSIFY_DETAILS);
507 mp->context = context;
508 mp->sw_if_index = htonl (sw_if_index);
509 mp->table_index = htonl (table_index);
511 vl_api_send_msg (reg, (u8 *) mp);
515 vl_api_flow_classify_dump_t_handler (vl_api_flow_classify_dump_t * mp)
517 vl_api_registration_t *reg;
518 flow_classify_main_t *pcm = &flow_classify_main;
521 u32 filter_sw_if_index;
523 reg = vl_api_client_index_to_registration (mp->client_index);
527 filter_sw_if_index = ntohl (mp->sw_if_index);
528 if (filter_sw_if_index
529 >= vec_len (pcm->classify_table_index_by_sw_if_index[mp->type]))
532 if (filter_sw_if_index != ~0)
534 &pcm->classify_table_index_by_sw_if_index[mp->type][filter_sw_if_index];
536 vec_tbl = pcm->classify_table_index_by_sw_if_index[mp->type];
538 if (vec_len (vec_tbl))
540 for (i = 0; i < vec_len (vec_tbl); i++)
542 if (vec_elt (vec_tbl, i) == ~0)
545 send_flow_classify_details (i, vec_elt (vec_tbl, i), reg,
551 static void vl_api_classify_set_interface_ip_table_t_handler
552 (vl_api_classify_set_interface_ip_table_t * mp)
554 vlib_main_t *vm = vlib_get_main ();
555 vl_api_classify_set_interface_ip_table_reply_t *rmp;
558 VALIDATE_SW_IF_INDEX (mp);
560 u32 table_index = ntohl (mp->table_index);
561 u32 sw_if_index = ntohl (mp->sw_if_index);
564 rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
566 rv = vnet_set_ip4_classify_intfc (vm, sw_if_index, table_index);
568 BAD_SW_IF_INDEX_LABEL;
570 REPLY_MACRO (VL_API_CLASSIFY_SET_INTERFACE_IP_TABLE_REPLY);
573 static void vl_api_classify_set_interface_l2_tables_t_handler
574 (vl_api_classify_set_interface_l2_tables_t * mp)
576 vl_api_classify_set_interface_l2_tables_reply_t *rmp;
578 u32 sw_if_index, ip4_table_index, ip6_table_index, other_table_index;
581 ip4_table_index = ntohl (mp->ip4_table_index);
582 ip6_table_index = ntohl (mp->ip6_table_index);
583 other_table_index = ntohl (mp->other_table_index);
584 sw_if_index = ntohl (mp->sw_if_index);
586 VALIDATE_SW_IF_INDEX (mp);
589 rv = vnet_l2_input_classify_set_tables (sw_if_index, ip4_table_index,
593 rv = vnet_l2_output_classify_set_tables (sw_if_index, ip4_table_index,
599 if (ip4_table_index != ~0 || ip6_table_index != ~0
600 || other_table_index != ~0)
606 vnet_l2_input_classify_enable_disable (sw_if_index, enable);
608 vnet_l2_output_classify_enable_disable (sw_if_index, enable);
611 BAD_SW_IF_INDEX_LABEL;
613 REPLY_MACRO (VL_API_CLASSIFY_SET_INTERFACE_L2_TABLES_REPLY);
616 static void vl_api_input_acl_set_interface_t_handler
617 (vl_api_input_acl_set_interface_t * mp)
619 vlib_main_t *vm = vlib_get_main ();
620 vl_api_input_acl_set_interface_reply_t *rmp;
623 VALIDATE_SW_IF_INDEX (mp);
625 u32 ip4_table_index = ntohl (mp->ip4_table_index);
626 u32 ip6_table_index = ntohl (mp->ip6_table_index);
627 u32 l2_table_index = ntohl (mp->l2_table_index);
628 u32 sw_if_index = ntohl (mp->sw_if_index);
630 rv = vnet_set_input_acl_intfc (vm, sw_if_index, ip4_table_index,
631 ip6_table_index, l2_table_index, mp->is_add);
633 BAD_SW_IF_INDEX_LABEL;
635 REPLY_MACRO (VL_API_INPUT_ACL_SET_INTERFACE_REPLY);
638 static void vl_api_output_acl_set_interface_t_handler
639 (vl_api_output_acl_set_interface_t * mp)
641 vlib_main_t *vm = vlib_get_main ();
642 vl_api_output_acl_set_interface_reply_t *rmp;
645 VALIDATE_SW_IF_INDEX (mp);
647 u32 ip4_table_index = ntohl (mp->ip4_table_index);
648 u32 ip6_table_index = ntohl (mp->ip6_table_index);
649 u32 l2_table_index = ntohl (mp->l2_table_index);
650 u32 sw_if_index = ntohl (mp->sw_if_index);
652 rv = vnet_set_output_acl_intfc (vm, sw_if_index, ip4_table_index,
653 ip6_table_index, l2_table_index,
656 BAD_SW_IF_INDEX_LABEL;
658 REPLY_MACRO (VL_API_OUTPUT_ACL_SET_INTERFACE_REPLY);
662 * classify_api_hookup
663 * Add vpe's API message handlers to the table.
664 * vlib has already mapped shared memory and
665 * added the client registration handlers.
666 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
668 #define vl_msg_name_crc_list
669 #include <vnet/vnet_all_api_h.h>
670 #undef vl_msg_name_crc_list
673 setup_message_id_table (api_main_t * am)
675 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
676 foreach_vl_msg_name_crc_classify;
680 static clib_error_t *
681 classify_api_hookup (vlib_main_t * vm)
683 api_main_t *am = vlibapi_get_main ();
686 vl_msg_api_set_handlers(VL_API_##N, #n, \
687 vl_api_##n##_t_handler, \
689 vl_api_##n##_t_endian, \
690 vl_api_##n##_t_print, \
691 sizeof(vl_api_##n##_t), 1);
696 * Set up the (msg_name, crc, message-id) table
698 setup_message_id_table (am);
703 VLIB_API_INIT_FUNCTION (classify_api_hookup);
706 * fd.io coding-style-patch-verification: ON
709 * eval: (c-set-style "gnu")