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,
186 unix_shared_memory_queue_t * q, u32 context)
188 vl_api_policer_classify_details_t *mp;
190 mp = vl_msg_api_alloc (sizeof (*mp));
191 memset (mp, 0, sizeof (*mp));
192 mp->_vl_msg_id = ntohs (VL_API_POLICER_CLASSIFY_DETAILS);
193 mp->context = context;
194 mp->sw_if_index = htonl (sw_if_index);
195 mp->table_index = htonl (table_index);
197 vl_msg_api_send_shmem (q, (u8 *) & mp);
201 vl_api_policer_classify_dump_t_handler (vl_api_policer_classify_dump_t * mp)
203 unix_shared_memory_queue_t *q;
204 policer_classify_main_t *pcm = &policer_classify_main;
208 q = vl_api_client_index_to_input_queue (mp->client_index);
212 vec_tbl = pcm->classify_table_index_by_sw_if_index[mp->type];
214 if (vec_len (vec_tbl))
216 for (i = 0; i < vec_len (vec_tbl); i++)
218 if (vec_elt (vec_tbl, i) == ~0)
221 send_policer_classify_details (i, vec_elt (vec_tbl, i), q,
228 vl_api_classify_table_ids_t_handler (vl_api_classify_table_ids_t * mp)
230 unix_shared_memory_queue_t *q;
232 q = vl_api_client_index_to_input_queue (mp->client_index);
236 vnet_classify_main_t *cm = &vnet_classify_main;
237 vnet_classify_table_t *t;
242 pool_foreach (t, cm->tables,
244 vec_add1 (table_ids, ntohl(t - cm->tables));
247 count = vec_len (table_ids);
249 vl_api_classify_table_ids_reply_t *rmp;
250 rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp) + count * sizeof (u32));
251 rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_TABLE_IDS_REPLY);
252 rmp->context = mp->context;
253 rmp->count = ntohl (count);
254 clib_memcpy (rmp->ids, table_ids, count * sizeof (u32));
257 vl_msg_api_send_shmem (q, (u8 *) & rmp);
259 vec_free (table_ids);
263 vl_api_classify_table_by_interface_t_handler
264 (vl_api_classify_table_by_interface_t * mp)
266 vl_api_classify_table_by_interface_reply_t *rmp;
269 u32 sw_if_index = ntohl (mp->sw_if_index);
272 vec_validate (acl, INPUT_ACL_N_TABLES - 1);
275 VALIDATE_SW_IF_INDEX (mp);
277 input_acl_main_t *am = &input_acl_main;
282 for (type = 0; type < INPUT_ACL_N_TABLES; type++)
284 u32 *vec_tbl = am->classify_table_index_by_sw_if_index[type];
285 if (vec_len (vec_tbl))
287 for (if_idx = 0; if_idx < vec_len (vec_tbl); if_idx++)
289 if (vec_elt (vec_tbl, if_idx) == ~0 || sw_if_index != if_idx)
293 acl[type] = vec_elt (vec_tbl, if_idx);
298 BAD_SW_IF_INDEX_LABEL;
301 REPLY_MACRO2(VL_API_CLASSIFY_TABLE_BY_INTERFACE_REPLY,
303 rmp->sw_if_index = ntohl(sw_if_index);
304 rmp->l2_table_id = ntohl(acl[INPUT_ACL_TABLE_L2]);
305 rmp->ip4_table_id = ntohl(acl[INPUT_ACL_TABLE_IP4]);
306 rmp->ip6_table_id = ntohl(acl[INPUT_ACL_TABLE_IP6]);
313 vl_api_classify_table_info_t_handler (vl_api_classify_table_info_t * mp)
315 unix_shared_memory_queue_t *q;
317 q = vl_api_client_index_to_input_queue (mp->client_index);
321 vl_api_classify_table_info_reply_t *rmp = 0;
323 vnet_classify_main_t *cm = &vnet_classify_main;
324 u32 table_id = ntohl (mp->table_id);
325 vnet_classify_table_t *t;
328 pool_foreach (t, cm->tables,
330 if (table_id == t - cm->tables)
332 rmp = vl_msg_api_alloc_as_if_client
333 (sizeof (*rmp) + t->match_n_vectors * sizeof (u32x4));
334 rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_TABLE_INFO_REPLY);
335 rmp->context = mp->context;
336 rmp->table_id = ntohl(table_id);
337 rmp->nbuckets = ntohl(t->nbuckets);
338 rmp->match_n_vectors = ntohl(t->match_n_vectors);
339 rmp->skip_n_vectors = ntohl(t->skip_n_vectors);
340 rmp->active_sessions = ntohl(t->active_elements);
341 rmp->next_table_index = ntohl(t->next_table_index);
342 rmp->miss_next_index = ntohl(t->miss_next_index);
343 rmp->mask_length = ntohl(t->match_n_vectors * sizeof (u32x4));
344 clib_memcpy(rmp->mask, t->mask, t->match_n_vectors * sizeof(u32x4));
353 rmp = vl_msg_api_alloc (sizeof (*rmp));
354 rmp->_vl_msg_id = ntohs ((VL_API_CLASSIFY_TABLE_INFO_REPLY));
355 rmp->context = mp->context;
356 rmp->retval = ntohl (VNET_API_ERROR_CLASSIFY_TABLE_NOT_FOUND);
359 vl_msg_api_send_shmem (q, (u8 *) & rmp);
363 send_classify_session_details (unix_shared_memory_queue_t * q,
366 vnet_classify_entry_t * e, u32 context)
368 vl_api_classify_session_details_t *rmp;
370 rmp = vl_msg_api_alloc (sizeof (*rmp));
371 memset (rmp, 0, sizeof (*rmp));
372 rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_SESSION_DETAILS);
373 rmp->context = context;
374 rmp->table_id = ntohl (table_id);
375 rmp->hit_next_index = ntohl (e->next_index);
376 rmp->advance = ntohl (e->advance);
377 rmp->opaque_index = ntohl (e->opaque_index);
378 rmp->match_length = ntohl (match_length);
379 clib_memcpy (rmp->match, e->key, match_length);
381 vl_msg_api_send_shmem (q, (u8 *) & rmp);
385 vl_api_classify_session_dump_t_handler (vl_api_classify_session_dump_t * mp)
387 vnet_classify_main_t *cm = &vnet_classify_main;
388 unix_shared_memory_queue_t *q;
390 u32 table_id = ntohl (mp->table_id);
391 vnet_classify_table_t *t;
393 q = vl_api_client_index_to_input_queue (mp->client_index);
398 pool_foreach (t, cm->tables,
400 if (table_id == t - cm->tables)
402 vnet_classify_bucket_t * b;
403 vnet_classify_entry_t * v, * save_v;
406 for (i = 0; i < t->nbuckets; i++)
412 save_v = vnet_classify_get_entry (t, b->offset);
413 for (j = 0; j < (1<<b->log2_pages); j++)
415 for (k = 0; k < t->entries_per_page; k++)
417 v = vnet_classify_entry_at_index
418 (t, save_v, j*t->entries_per_page + k);
419 if (vnet_classify_entry_is_free (v))
422 send_classify_session_details
423 (q, table_id, t->match_n_vectors * sizeof (u32x4),
435 vl_api_flow_classify_set_interface_t_handler
436 (vl_api_flow_classify_set_interface_t * mp)
438 vlib_main_t *vm = vlib_get_main ();
439 vl_api_flow_classify_set_interface_reply_t *rmp;
441 u32 sw_if_index, ip4_table_index, ip6_table_index;
443 ip4_table_index = ntohl (mp->ip4_table_index);
444 ip6_table_index = ntohl (mp->ip6_table_index);
445 sw_if_index = ntohl (mp->sw_if_index);
447 VALIDATE_SW_IF_INDEX (mp);
449 rv = vnet_set_flow_classify_intfc (vm, sw_if_index, ip4_table_index,
450 ip6_table_index, mp->is_add);
452 BAD_SW_IF_INDEX_LABEL;
454 REPLY_MACRO (VL_API_FLOW_CLASSIFY_SET_INTERFACE_REPLY);
458 send_flow_classify_details (u32 sw_if_index,
460 unix_shared_memory_queue_t * q, u32 context)
462 vl_api_flow_classify_details_t *mp;
464 mp = vl_msg_api_alloc (sizeof (*mp));
465 memset (mp, 0, sizeof (*mp));
466 mp->_vl_msg_id = ntohs (VL_API_FLOW_CLASSIFY_DETAILS);
467 mp->context = context;
468 mp->sw_if_index = htonl (sw_if_index);
469 mp->table_index = htonl (table_index);
471 vl_msg_api_send_shmem (q, (u8 *) & mp);
475 vl_api_flow_classify_dump_t_handler (vl_api_flow_classify_dump_t * mp)
477 unix_shared_memory_queue_t *q;
478 flow_classify_main_t *pcm = &flow_classify_main;
482 q = vl_api_client_index_to_input_queue (mp->client_index);
486 vec_tbl = pcm->classify_table_index_by_sw_if_index[mp->type];
488 if (vec_len (vec_tbl))
490 for (i = 0; i < vec_len (vec_tbl); i++)
492 if (vec_elt (vec_tbl, i) == ~0)
495 send_flow_classify_details (i, vec_elt (vec_tbl, i), q,
501 static void vl_api_classify_set_interface_ip_table_t_handler
502 (vl_api_classify_set_interface_ip_table_t * mp)
504 vlib_main_t *vm = vlib_get_main ();
505 vl_api_classify_set_interface_ip_table_reply_t *rmp;
508 VALIDATE_SW_IF_INDEX (mp);
510 u32 table_index = ntohl (mp->table_index);
511 u32 sw_if_index = ntohl (mp->sw_if_index);
514 rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
516 rv = vnet_set_ip4_classify_intfc (vm, sw_if_index, table_index);
518 BAD_SW_IF_INDEX_LABEL;
520 REPLY_MACRO (VL_API_CLASSIFY_SET_INTERFACE_IP_TABLE_REPLY);
523 static void vl_api_classify_set_interface_l2_tables_t_handler
524 (vl_api_classify_set_interface_l2_tables_t * mp)
526 vl_api_classify_set_interface_l2_tables_reply_t *rmp;
528 u32 sw_if_index, ip4_table_index, ip6_table_index, other_table_index;
531 ip4_table_index = ntohl (mp->ip4_table_index);
532 ip6_table_index = ntohl (mp->ip6_table_index);
533 other_table_index = ntohl (mp->other_table_index);
534 sw_if_index = ntohl (mp->sw_if_index);
536 VALIDATE_SW_IF_INDEX (mp);
539 rv = vnet_l2_input_classify_set_tables (sw_if_index, ip4_table_index,
543 rv = vnet_l2_output_classify_set_tables (sw_if_index, ip4_table_index,
549 if (ip4_table_index != ~0 || ip6_table_index != ~0
550 || other_table_index != ~0)
556 vnet_l2_input_classify_enable_disable (sw_if_index, enable);
558 vnet_l2_output_classify_enable_disable (sw_if_index, enable);
561 BAD_SW_IF_INDEX_LABEL;
563 REPLY_MACRO (VL_API_CLASSIFY_SET_INTERFACE_L2_TABLES_REPLY);
566 static void vl_api_input_acl_set_interface_t_handler
567 (vl_api_input_acl_set_interface_t * mp)
569 vlib_main_t *vm = vlib_get_main ();
570 vl_api_input_acl_set_interface_reply_t *rmp;
573 VALIDATE_SW_IF_INDEX (mp);
575 u32 ip4_table_index = ntohl (mp->ip4_table_index);
576 u32 ip6_table_index = ntohl (mp->ip6_table_index);
577 u32 l2_table_index = ntohl (mp->l2_table_index);
578 u32 sw_if_index = ntohl (mp->sw_if_index);
580 rv = vnet_set_input_acl_intfc (vm, sw_if_index, ip4_table_index,
581 ip6_table_index, l2_table_index, mp->is_add);
583 BAD_SW_IF_INDEX_LABEL;
585 REPLY_MACRO (VL_API_INPUT_ACL_SET_INTERFACE_REPLY);
589 * classify_api_hookup
590 * Add vpe's API message handlers to the table.
591 * vlib has alread mapped shared memory and
592 * added the client registration handlers.
593 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
595 #define vl_msg_name_crc_list
596 #include <vnet/vnet_all_api_h.h>
597 #undef vl_msg_name_crc_list
600 setup_message_id_table (api_main_t * am)
602 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
603 foreach_vl_msg_name_crc_classify;
607 static clib_error_t *
608 classify_api_hookup (vlib_main_t * vm)
610 api_main_t *am = &api_main;
613 vl_msg_api_set_handlers(VL_API_##N, #n, \
614 vl_api_##n##_t_handler, \
616 vl_api_##n##_t_endian, \
617 vl_api_##n##_t_print, \
618 sizeof(vl_api_##n##_t), 1);
623 * Set up the (msg_name, crc, message-id) table
625 setup_message_id_table (am);
630 VLIB_API_INIT_FUNCTION (classify_api_hookup);
633 * fd.io coding-style-patch-verification: ON
636 * eval: (c-set-style "gnu")