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)
77 static void vl_api_classify_add_del_table_t_handler
78 (vl_api_classify_add_del_table_t * mp)
80 vl_api_classify_add_del_table_reply_t *rmp;
81 vnet_classify_main_t *cm = &vnet_classify_main;
82 vnet_classify_table_t *t;
86 foreach_classify_add_del_table_field;
89 #define _(a) a = ntohl(mp->a);
90 foreach_classify_add_del_table_field;
93 /* The underlying API fails silently, on purpose, so check here */
94 if (mp->is_add == 0) /* delete */
96 if (pool_is_free_index (cm->tables, table_index))
98 rv = VNET_API_ERROR_NO_SUCH_TABLE;
102 else /* add or update */
104 if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
108 rv = vnet_classify_add_del_table
109 (cm, mp->mask, nbuckets, memory_size,
110 skip_n_vectors, match_n_vectors,
111 next_table_index, miss_next_index, &table_index,
112 current_data_flag, current_data_offset, mp->is_add, mp->del_chain);
116 REPLY_MACRO2(VL_API_CLASSIFY_ADD_DEL_TABLE_REPLY,
118 if (rv == 0 && mp->is_add)
120 t = pool_elt_at_index (cm->tables, table_index);
121 rmp->skip_n_vectors = ntohl(t->skip_n_vectors);
122 rmp->match_n_vectors = ntohl(t->match_n_vectors);
123 rmp->new_table_index = ntohl(table_index);
127 rmp->skip_n_vectors = ~0;
128 rmp->match_n_vectors = ~0;
129 rmp->new_table_index = ~0;
135 static void vl_api_classify_add_del_session_t_handler
136 (vl_api_classify_add_del_session_t * mp)
138 vnet_classify_main_t *cm = &vnet_classify_main;
139 vl_api_classify_add_del_session_reply_t *rmp;
141 u32 table_index, hit_next_index, opaque_index, metadata;
145 table_index = ntohl (mp->table_index);
146 hit_next_index = ntohl (mp->hit_next_index);
147 opaque_index = ntohl (mp->opaque_index);
148 advance = ntohl (mp->advance);
150 metadata = ntohl (mp->metadata);
152 rv = vnet_classify_add_del_session
153 (cm, table_index, mp->match, hit_next_index, opaque_index,
154 advance, action, metadata, mp->is_add);
156 REPLY_MACRO (VL_API_CLASSIFY_ADD_DEL_SESSION_REPLY);
160 vl_api_policer_classify_set_interface_t_handler
161 (vl_api_policer_classify_set_interface_t * mp)
163 vlib_main_t *vm = vlib_get_main ();
164 vl_api_policer_classify_set_interface_reply_t *rmp;
166 u32 sw_if_index, ip4_table_index, ip6_table_index, l2_table_index;
168 ip4_table_index = ntohl (mp->ip4_table_index);
169 ip6_table_index = ntohl (mp->ip6_table_index);
170 l2_table_index = ntohl (mp->l2_table_index);
171 sw_if_index = ntohl (mp->sw_if_index);
173 VALIDATE_SW_IF_INDEX (mp);
175 rv = vnet_set_policer_classify_intfc (vm, sw_if_index, ip4_table_index,
176 ip6_table_index, l2_table_index,
179 BAD_SW_IF_INDEX_LABEL;
181 REPLY_MACRO (VL_API_POLICER_CLASSIFY_SET_INTERFACE_REPLY);
185 send_policer_classify_details (u32 sw_if_index,
186 u32 table_index, vl_api_registration_t * reg,
189 vl_api_policer_classify_details_t *mp;
191 mp = vl_msg_api_alloc (sizeof (*mp));
192 memset (mp, 0, sizeof (*mp));
193 mp->_vl_msg_id = ntohs (VL_API_POLICER_CLASSIFY_DETAILS);
194 mp->context = context;
195 mp->sw_if_index = htonl (sw_if_index);
196 mp->table_index = htonl (table_index);
198 vl_api_send_msg (reg, (u8 *) mp);
202 vl_api_policer_classify_dump_t_handler (vl_api_policer_classify_dump_t * mp)
204 vl_api_registration_t *reg;
205 policer_classify_main_t *pcm = &policer_classify_main;
209 reg = vl_api_client_index_to_registration (mp->client_index);
213 vec_tbl = pcm->classify_table_index_by_sw_if_index[mp->type];
215 if (vec_len (vec_tbl))
217 for (i = 0; i < vec_len (vec_tbl); i++)
219 if (vec_elt (vec_tbl, i) == ~0)
222 send_policer_classify_details (i, vec_elt (vec_tbl, i), reg,
229 vl_api_classify_table_ids_t_handler (vl_api_classify_table_ids_t * mp)
231 vl_api_registration_t *reg;
233 reg = vl_api_client_index_to_registration (mp->client_index);
237 vnet_classify_main_t *cm = &vnet_classify_main;
238 vnet_classify_table_t *t;
243 pool_foreach (t, cm->tables,
245 vec_add1 (table_ids, ntohl(t - cm->tables));
248 count = vec_len (table_ids);
250 vl_api_classify_table_ids_reply_t *rmp;
251 rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp) + count * sizeof (u32));
252 rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_TABLE_IDS_REPLY);
253 rmp->context = mp->context;
254 rmp->count = ntohl (count);
255 clib_memcpy (rmp->ids, table_ids, count * sizeof (u32));
258 vl_api_send_msg (reg, (u8 *) rmp);
260 vec_free (table_ids);
264 vl_api_classify_table_by_interface_t_handler
265 (vl_api_classify_table_by_interface_t * mp)
267 vl_api_classify_table_by_interface_reply_t *rmp;
270 u32 sw_if_index = ntohl (mp->sw_if_index);
273 vec_validate (acl, IN_OUT_ACL_N_TABLES - 1);
276 VALIDATE_SW_IF_INDEX (mp);
278 in_out_acl_main_t *am = &in_out_acl_main;
283 for (type = 0; type < IN_OUT_ACL_N_TABLES; type++)
286 am->classify_table_index_by_sw_if_index[IN_OUT_ACL_INPUT_TABLE_GROUP]
288 if (vec_len (vec_tbl))
290 for (if_idx = 0; if_idx < vec_len (vec_tbl); if_idx++)
292 if (vec_elt (vec_tbl, if_idx) == ~0 || sw_if_index != if_idx)
296 acl[type] = vec_elt (vec_tbl, if_idx);
301 BAD_SW_IF_INDEX_LABEL;
304 REPLY_MACRO2(VL_API_CLASSIFY_TABLE_BY_INTERFACE_REPLY,
306 rmp->sw_if_index = ntohl(sw_if_index);
307 rmp->l2_table_id = ntohl(acl[IN_OUT_ACL_TABLE_L2]);
308 rmp->ip4_table_id = ntohl(acl[IN_OUT_ACL_TABLE_IP4]);
309 rmp->ip6_table_id = ntohl(acl[IN_OUT_ACL_TABLE_IP6]);
316 vl_api_classify_table_info_t_handler (vl_api_classify_table_info_t * mp)
318 vl_api_registration_t *reg;
320 reg = vl_api_client_index_to_registration (mp->client_index);
324 vl_api_classify_table_info_reply_t *rmp = 0;
326 vnet_classify_main_t *cm = &vnet_classify_main;
327 u32 table_id = ntohl (mp->table_id);
328 vnet_classify_table_t *t;
331 pool_foreach (t, cm->tables,
333 if (table_id == t - cm->tables)
335 rmp = vl_msg_api_alloc_as_if_client
336 (sizeof (*rmp) + t->match_n_vectors * sizeof (u32x4));
337 rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_TABLE_INFO_REPLY);
338 rmp->context = mp->context;
339 rmp->table_id = ntohl(table_id);
340 rmp->nbuckets = ntohl(t->nbuckets);
341 rmp->match_n_vectors = ntohl(t->match_n_vectors);
342 rmp->skip_n_vectors = ntohl(t->skip_n_vectors);
343 rmp->active_sessions = ntohl(t->active_elements);
344 rmp->next_table_index = ntohl(t->next_table_index);
345 rmp->miss_next_index = ntohl(t->miss_next_index);
346 rmp->mask_length = ntohl(t->match_n_vectors * sizeof (u32x4));
347 clib_memcpy(rmp->mask, t->mask, t->match_n_vectors * sizeof(u32x4));
356 rmp = vl_msg_api_alloc (sizeof (*rmp));
357 rmp->_vl_msg_id = ntohs ((VL_API_CLASSIFY_TABLE_INFO_REPLY));
358 rmp->context = mp->context;
359 rmp->retval = ntohl (VNET_API_ERROR_CLASSIFY_TABLE_NOT_FOUND);
362 vl_api_send_msg (reg, (u8 *) rmp);
366 send_classify_session_details (vl_api_registration_t * reg,
369 vnet_classify_entry_t * e, u32 context)
371 vl_api_classify_session_details_t *rmp;
373 rmp = vl_msg_api_alloc (sizeof (*rmp));
374 memset (rmp, 0, sizeof (*rmp));
375 rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_SESSION_DETAILS);
376 rmp->context = context;
377 rmp->table_id = ntohl (table_id);
378 rmp->hit_next_index = ntohl (e->next_index);
379 rmp->advance = ntohl (e->advance);
380 rmp->opaque_index = ntohl (e->opaque_index);
381 rmp->match_length = ntohl (match_length);
382 clib_memcpy (rmp->match, e->key, match_length);
384 vl_api_send_msg (reg, (u8 *) rmp);
388 vl_api_classify_session_dump_t_handler (vl_api_classify_session_dump_t * mp)
390 vnet_classify_main_t *cm = &vnet_classify_main;
391 vl_api_registration_t *reg;
393 u32 table_id = ntohl (mp->table_id);
394 vnet_classify_table_t *t;
396 reg = vl_api_client_index_to_registration (mp->client_index);
401 pool_foreach (t, cm->tables,
403 if (table_id == t - cm->tables)
405 vnet_classify_bucket_t * b;
406 vnet_classify_entry_t * v, * save_v;
409 for (i = 0; i < t->nbuckets; i++)
415 save_v = vnet_classify_get_entry (t, b->offset);
416 for (j = 0; j < (1<<b->log2_pages); j++)
418 for (k = 0; k < t->entries_per_page; k++)
420 v = vnet_classify_entry_at_index
421 (t, save_v, j*t->entries_per_page + k);
422 if (vnet_classify_entry_is_free (v))
425 send_classify_session_details
426 (reg, table_id, t->match_n_vectors * sizeof (u32x4),
438 vl_api_flow_classify_set_interface_t_handler
439 (vl_api_flow_classify_set_interface_t * mp)
441 vlib_main_t *vm = vlib_get_main ();
442 vl_api_flow_classify_set_interface_reply_t *rmp;
444 u32 sw_if_index, ip4_table_index, ip6_table_index;
446 ip4_table_index = ntohl (mp->ip4_table_index);
447 ip6_table_index = ntohl (mp->ip6_table_index);
448 sw_if_index = ntohl (mp->sw_if_index);
450 VALIDATE_SW_IF_INDEX (mp);
452 rv = vnet_set_flow_classify_intfc (vm, sw_if_index, ip4_table_index,
453 ip6_table_index, mp->is_add);
455 BAD_SW_IF_INDEX_LABEL;
457 REPLY_MACRO (VL_API_FLOW_CLASSIFY_SET_INTERFACE_REPLY);
461 send_flow_classify_details (u32 sw_if_index,
462 u32 table_index, vl_api_registration_t * reg,
465 vl_api_flow_classify_details_t *mp;
467 mp = vl_msg_api_alloc (sizeof (*mp));
468 memset (mp, 0, sizeof (*mp));
469 mp->_vl_msg_id = ntohs (VL_API_FLOW_CLASSIFY_DETAILS);
470 mp->context = context;
471 mp->sw_if_index = htonl (sw_if_index);
472 mp->table_index = htonl (table_index);
474 vl_api_send_msg (reg, (u8 *) mp);
478 vl_api_flow_classify_dump_t_handler (vl_api_flow_classify_dump_t * mp)
480 vl_api_registration_t *reg;
481 flow_classify_main_t *pcm = &flow_classify_main;
485 reg = vl_api_client_index_to_registration (mp->client_index);
489 vec_tbl = pcm->classify_table_index_by_sw_if_index[mp->type];
491 if (vec_len (vec_tbl))
493 for (i = 0; i < vec_len (vec_tbl); i++)
495 if (vec_elt (vec_tbl, i) == ~0)
498 send_flow_classify_details (i, vec_elt (vec_tbl, i), reg,
504 static void vl_api_classify_set_interface_ip_table_t_handler
505 (vl_api_classify_set_interface_ip_table_t * mp)
507 vlib_main_t *vm = vlib_get_main ();
508 vl_api_classify_set_interface_ip_table_reply_t *rmp;
511 VALIDATE_SW_IF_INDEX (mp);
513 u32 table_index = ntohl (mp->table_index);
514 u32 sw_if_index = ntohl (mp->sw_if_index);
517 rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
519 rv = vnet_set_ip4_classify_intfc (vm, sw_if_index, table_index);
521 BAD_SW_IF_INDEX_LABEL;
523 REPLY_MACRO (VL_API_CLASSIFY_SET_INTERFACE_IP_TABLE_REPLY);
526 static void vl_api_classify_set_interface_l2_tables_t_handler
527 (vl_api_classify_set_interface_l2_tables_t * mp)
529 vl_api_classify_set_interface_l2_tables_reply_t *rmp;
531 u32 sw_if_index, ip4_table_index, ip6_table_index, other_table_index;
534 ip4_table_index = ntohl (mp->ip4_table_index);
535 ip6_table_index = ntohl (mp->ip6_table_index);
536 other_table_index = ntohl (mp->other_table_index);
537 sw_if_index = ntohl (mp->sw_if_index);
539 VALIDATE_SW_IF_INDEX (mp);
542 rv = vnet_l2_input_classify_set_tables (sw_if_index, ip4_table_index,
546 rv = vnet_l2_output_classify_set_tables (sw_if_index, ip4_table_index,
552 if (ip4_table_index != ~0 || ip6_table_index != ~0
553 || other_table_index != ~0)
559 vnet_l2_input_classify_enable_disable (sw_if_index, enable);
561 vnet_l2_output_classify_enable_disable (sw_if_index, enable);
564 BAD_SW_IF_INDEX_LABEL;
566 REPLY_MACRO (VL_API_CLASSIFY_SET_INTERFACE_L2_TABLES_REPLY);
569 static void vl_api_input_acl_set_interface_t_handler
570 (vl_api_input_acl_set_interface_t * mp)
572 vlib_main_t *vm = vlib_get_main ();
573 vl_api_input_acl_set_interface_reply_t *rmp;
576 VALIDATE_SW_IF_INDEX (mp);
578 u32 ip4_table_index = ntohl (mp->ip4_table_index);
579 u32 ip6_table_index = ntohl (mp->ip6_table_index);
580 u32 l2_table_index = ntohl (mp->l2_table_index);
581 u32 sw_if_index = ntohl (mp->sw_if_index);
583 rv = vnet_set_input_acl_intfc (vm, sw_if_index, ip4_table_index,
584 ip6_table_index, l2_table_index, mp->is_add);
586 BAD_SW_IF_INDEX_LABEL;
588 REPLY_MACRO (VL_API_INPUT_ACL_SET_INTERFACE_REPLY);
591 static void vl_api_output_acl_set_interface_t_handler
592 (vl_api_output_acl_set_interface_t * mp)
594 vlib_main_t *vm = vlib_get_main ();
595 vl_api_output_acl_set_interface_reply_t *rmp;
598 VALIDATE_SW_IF_INDEX (mp);
600 u32 ip4_table_index = ntohl (mp->ip4_table_index);
601 u32 ip6_table_index = ntohl (mp->ip6_table_index);
602 u32 l2_table_index = ntohl (mp->l2_table_index);
603 u32 sw_if_index = ntohl (mp->sw_if_index);
605 rv = vnet_set_output_acl_intfc (vm, sw_if_index, ip4_table_index,
606 ip6_table_index, l2_table_index,
609 BAD_SW_IF_INDEX_LABEL;
611 REPLY_MACRO (VL_API_OUTPUT_ACL_SET_INTERFACE_REPLY);
615 * classify_api_hookup
616 * Add vpe's API message handlers to the table.
617 * vlib has alread mapped shared memory and
618 * added the client registration handlers.
619 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
621 #define vl_msg_name_crc_list
622 #include <vnet/vnet_all_api_h.h>
623 #undef vl_msg_name_crc_list
626 setup_message_id_table (api_main_t * am)
628 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
629 foreach_vl_msg_name_crc_classify;
633 static clib_error_t *
634 classify_api_hookup (vlib_main_t * vm)
636 api_main_t *am = &api_main;
639 vl_msg_api_set_handlers(VL_API_##N, #n, \
640 vl_api_##n##_t_handler, \
642 vl_api_##n##_t_endian, \
643 vl_api_##n##_t_print, \
644 sizeof(vl_api_##n##_t), 1);
649 * Set up the (msg_name, crc, message-id) table
651 setup_message_id_table (am);
656 VLIB_API_INIT_FUNCTION (classify_api_hookup);
659 * fd.io coding-style-patch-verification: ON
662 * eval: (c-set-style "gnu")