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>
31 #include <vnet/vnet_msg_enum.h>
33 #define vl_typedefs /* define message structures */
34 #include <vnet/vnet_all_api_h.h>
37 #define vl_endianfun /* define message structures */
38 #include <vnet/vnet_all_api_h.h>
41 /* instantiate all the print functions we know about */
42 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
44 #include <vnet/vnet_all_api_h.h>
47 #include <vlibapi/api_helper_macros.h>
49 #define foreach_vpe_api_msg \
50 _(CLASSIFY_ADD_DEL_TABLE, classify_add_del_table) \
51 _(CLASSIFY_ADD_DEL_SESSION, classify_add_del_session) \
52 _(CLASSIFY_TABLE_IDS,classify_table_ids) \
53 _(CLASSIFY_TABLE_BY_INTERFACE, classify_table_by_interface) \
54 _(CLASSIFY_TABLE_INFO,classify_table_info) \
55 _(CLASSIFY_SESSION_DUMP,classify_session_dump) \
56 _(POLICER_CLASSIFY_SET_INTERFACE, policer_classify_set_interface) \
57 _(POLICER_CLASSIFY_DUMP, policer_classify_dump) \
58 _(FLOW_CLASSIFY_SET_INTERFACE, flow_classify_set_interface) \
59 _(FLOW_CLASSIFY_DUMP, flow_classify_dump)
61 #define foreach_classify_add_del_table_field \
69 _(current_data_flag) \
70 _(current_data_offset)
72 static void vl_api_classify_add_del_table_t_handler
73 (vl_api_classify_add_del_table_t * mp)
75 vl_api_classify_add_del_table_reply_t *rmp;
76 vnet_classify_main_t *cm = &vnet_classify_main;
77 vnet_classify_table_t *t;
81 foreach_classify_add_del_table_field;
84 #define _(a) a = ntohl(mp->a);
85 foreach_classify_add_del_table_field;
88 /* The underlying API fails silently, on purpose, so check here */
89 if (mp->is_add == 0) /* delete */
91 if (pool_is_free_index (cm->tables, table_index))
93 rv = VNET_API_ERROR_NO_SUCH_TABLE;
97 else /* add or update */
99 if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
103 rv = vnet_classify_add_del_table
104 (cm, mp->mask, nbuckets, memory_size,
105 skip_n_vectors, match_n_vectors,
106 next_table_index, miss_next_index, &table_index,
107 current_data_flag, current_data_offset, mp->is_add, mp->del_chain);
111 REPLY_MACRO2(VL_API_CLASSIFY_ADD_DEL_TABLE_REPLY,
113 if (rv == 0 && mp->is_add)
115 t = pool_elt_at_index (cm->tables, table_index);
116 rmp->skip_n_vectors = ntohl(t->skip_n_vectors);
117 rmp->match_n_vectors = ntohl(t->match_n_vectors);
118 rmp->new_table_index = ntohl(table_index);
122 rmp->skip_n_vectors = ~0;
123 rmp->match_n_vectors = ~0;
124 rmp->new_table_index = ~0;
130 static void vl_api_classify_add_del_session_t_handler
131 (vl_api_classify_add_del_session_t * mp)
133 vnet_classify_main_t *cm = &vnet_classify_main;
134 vl_api_classify_add_del_session_reply_t *rmp;
136 u32 table_index, hit_next_index, opaque_index, metadata;
140 table_index = ntohl (mp->table_index);
141 hit_next_index = ntohl (mp->hit_next_index);
142 opaque_index = ntohl (mp->opaque_index);
143 advance = ntohl (mp->advance);
145 metadata = ntohl (mp->metadata);
147 rv = vnet_classify_add_del_session
148 (cm, table_index, mp->match, hit_next_index, opaque_index,
149 advance, action, metadata, mp->is_add);
151 REPLY_MACRO (VL_API_CLASSIFY_ADD_DEL_SESSION_REPLY);
155 vl_api_policer_classify_set_interface_t_handler
156 (vl_api_policer_classify_set_interface_t * mp)
158 vlib_main_t *vm = vlib_get_main ();
159 vl_api_policer_classify_set_interface_reply_t *rmp;
161 u32 sw_if_index, ip4_table_index, ip6_table_index, l2_table_index;
163 ip4_table_index = ntohl (mp->ip4_table_index);
164 ip6_table_index = ntohl (mp->ip6_table_index);
165 l2_table_index = ntohl (mp->l2_table_index);
166 sw_if_index = ntohl (mp->sw_if_index);
168 VALIDATE_SW_IF_INDEX (mp);
170 rv = vnet_set_policer_classify_intfc (vm, sw_if_index, ip4_table_index,
171 ip6_table_index, l2_table_index,
174 BAD_SW_IF_INDEX_LABEL;
176 REPLY_MACRO (VL_API_POLICER_CLASSIFY_SET_INTERFACE_REPLY);
180 send_policer_classify_details (u32 sw_if_index,
182 unix_shared_memory_queue_t * q, u32 context)
184 vl_api_policer_classify_details_t *mp;
186 mp = vl_msg_api_alloc (sizeof (*mp));
187 memset (mp, 0, sizeof (*mp));
188 mp->_vl_msg_id = ntohs (VL_API_POLICER_CLASSIFY_DETAILS);
189 mp->context = context;
190 mp->sw_if_index = htonl (sw_if_index);
191 mp->table_index = htonl (table_index);
193 vl_msg_api_send_shmem (q, (u8 *) & mp);
197 vl_api_policer_classify_dump_t_handler (vl_api_policer_classify_dump_t * mp)
199 unix_shared_memory_queue_t *q;
200 policer_classify_main_t *pcm = &policer_classify_main;
204 q = vl_api_client_index_to_input_queue (mp->client_index);
208 vec_tbl = pcm->classify_table_index_by_sw_if_index[mp->type];
210 if (vec_len (vec_tbl))
212 for (i = 0; i < vec_len (vec_tbl); i++)
214 if (vec_elt (vec_tbl, i) == ~0)
217 send_policer_classify_details (i, vec_elt (vec_tbl, i), q,
224 vl_api_classify_table_ids_t_handler (vl_api_classify_table_ids_t * mp)
226 unix_shared_memory_queue_t *q;
228 q = vl_api_client_index_to_input_queue (mp->client_index);
232 vnet_classify_main_t *cm = &vnet_classify_main;
233 vnet_classify_table_t *t;
238 pool_foreach (t, cm->tables,
240 vec_add1 (table_ids, ntohl(t - cm->tables));
243 count = vec_len (table_ids);
245 vl_api_classify_table_ids_reply_t *rmp;
246 rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp) + count * sizeof (u32));
247 rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_TABLE_IDS_REPLY);
248 rmp->context = mp->context;
249 rmp->count = ntohl (count);
250 clib_memcpy (rmp->ids, table_ids, count * sizeof (u32));
253 vl_msg_api_send_shmem (q, (u8 *) & rmp);
255 vec_free (table_ids);
259 vl_api_classify_table_by_interface_t_handler
260 (vl_api_classify_table_by_interface_t * mp)
262 vl_api_classify_table_by_interface_reply_t *rmp;
265 u32 sw_if_index = ntohl (mp->sw_if_index);
268 vec_validate (acl, INPUT_ACL_N_TABLES - 1);
271 VALIDATE_SW_IF_INDEX (mp);
273 input_acl_main_t *am = &input_acl_main;
278 for (type = 0; type < INPUT_ACL_N_TABLES; type++)
280 u32 *vec_tbl = am->classify_table_index_by_sw_if_index[type];
281 if (vec_len (vec_tbl))
283 for (if_idx = 0; if_idx < vec_len (vec_tbl); if_idx++)
285 if (vec_elt (vec_tbl, if_idx) == ~0 || sw_if_index != if_idx)
289 acl[type] = vec_elt (vec_tbl, if_idx);
294 BAD_SW_IF_INDEX_LABEL;
297 REPLY_MACRO2(VL_API_CLASSIFY_TABLE_BY_INTERFACE_REPLY,
299 rmp->sw_if_index = ntohl(sw_if_index);
300 rmp->l2_table_id = ntohl(acl[INPUT_ACL_TABLE_L2]);
301 rmp->ip4_table_id = ntohl(acl[INPUT_ACL_TABLE_IP4]);
302 rmp->ip6_table_id = ntohl(acl[INPUT_ACL_TABLE_IP6]);
309 vl_api_classify_table_info_t_handler (vl_api_classify_table_info_t * mp)
311 unix_shared_memory_queue_t *q;
313 q = vl_api_client_index_to_input_queue (mp->client_index);
317 vl_api_classify_table_info_reply_t *rmp = 0;
319 vnet_classify_main_t *cm = &vnet_classify_main;
320 u32 table_id = ntohl (mp->table_id);
321 vnet_classify_table_t *t;
324 pool_foreach (t, cm->tables,
326 if (table_id == t - cm->tables)
328 rmp = vl_msg_api_alloc_as_if_client
329 (sizeof (*rmp) + t->match_n_vectors * sizeof (u32x4));
330 rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_TABLE_INFO_REPLY);
331 rmp->context = mp->context;
332 rmp->table_id = ntohl(table_id);
333 rmp->nbuckets = ntohl(t->nbuckets);
334 rmp->match_n_vectors = ntohl(t->match_n_vectors);
335 rmp->skip_n_vectors = ntohl(t->skip_n_vectors);
336 rmp->active_sessions = ntohl(t->active_elements);
337 rmp->next_table_index = ntohl(t->next_table_index);
338 rmp->miss_next_index = ntohl(t->miss_next_index);
339 rmp->mask_length = ntohl(t->match_n_vectors * sizeof (u32x4));
340 clib_memcpy(rmp->mask, t->mask, t->match_n_vectors * sizeof(u32x4));
349 rmp = vl_msg_api_alloc (sizeof (*rmp));
350 rmp->_vl_msg_id = ntohs ((VL_API_CLASSIFY_TABLE_INFO_REPLY));
351 rmp->context = mp->context;
352 rmp->retval = ntohl (VNET_API_ERROR_CLASSIFY_TABLE_NOT_FOUND);
355 vl_msg_api_send_shmem (q, (u8 *) & rmp);
359 send_classify_session_details (unix_shared_memory_queue_t * q,
362 vnet_classify_entry_t * e, u32 context)
364 vl_api_classify_session_details_t *rmp;
366 rmp = vl_msg_api_alloc (sizeof (*rmp));
367 memset (rmp, 0, sizeof (*rmp));
368 rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_SESSION_DETAILS);
369 rmp->context = context;
370 rmp->table_id = ntohl (table_id);
371 rmp->hit_next_index = ntohl (e->next_index);
372 rmp->advance = ntohl (e->advance);
373 rmp->opaque_index = ntohl (e->opaque_index);
374 rmp->match_length = ntohl (match_length);
375 clib_memcpy (rmp->match, e->key, match_length);
377 vl_msg_api_send_shmem (q, (u8 *) & rmp);
381 vl_api_classify_session_dump_t_handler (vl_api_classify_session_dump_t * mp)
383 vnet_classify_main_t *cm = &vnet_classify_main;
384 unix_shared_memory_queue_t *q;
386 u32 table_id = ntohl (mp->table_id);
387 vnet_classify_table_t *t;
389 q = vl_api_client_index_to_input_queue (mp->client_index);
394 pool_foreach (t, cm->tables,
396 if (table_id == t - cm->tables)
398 vnet_classify_bucket_t * b;
399 vnet_classify_entry_t * v, * save_v;
402 for (i = 0; i < t->nbuckets; i++)
408 save_v = vnet_classify_get_entry (t, b->offset);
409 for (j = 0; j < (1<<b->log2_pages); j++)
411 for (k = 0; k < t->entries_per_page; k++)
413 v = vnet_classify_entry_at_index
414 (t, save_v, j*t->entries_per_page + k);
415 if (vnet_classify_entry_is_free (v))
418 send_classify_session_details
419 (q, table_id, t->match_n_vectors * sizeof (u32x4),
431 vl_api_flow_classify_set_interface_t_handler
432 (vl_api_flow_classify_set_interface_t * mp)
434 vlib_main_t *vm = vlib_get_main ();
435 vl_api_flow_classify_set_interface_reply_t *rmp;
437 u32 sw_if_index, ip4_table_index, ip6_table_index;
439 ip4_table_index = ntohl (mp->ip4_table_index);
440 ip6_table_index = ntohl (mp->ip6_table_index);
441 sw_if_index = ntohl (mp->sw_if_index);
443 VALIDATE_SW_IF_INDEX (mp);
445 rv = vnet_set_flow_classify_intfc (vm, sw_if_index, ip4_table_index,
446 ip6_table_index, mp->is_add);
448 BAD_SW_IF_INDEX_LABEL;
450 REPLY_MACRO (VL_API_FLOW_CLASSIFY_SET_INTERFACE_REPLY);
454 send_flow_classify_details (u32 sw_if_index,
456 unix_shared_memory_queue_t * q, u32 context)
458 vl_api_flow_classify_details_t *mp;
460 mp = vl_msg_api_alloc (sizeof (*mp));
461 memset (mp, 0, sizeof (*mp));
462 mp->_vl_msg_id = ntohs (VL_API_FLOW_CLASSIFY_DETAILS);
463 mp->context = context;
464 mp->sw_if_index = htonl (sw_if_index);
465 mp->table_index = htonl (table_index);
467 vl_msg_api_send_shmem (q, (u8 *) & mp);
471 vl_api_flow_classify_dump_t_handler (vl_api_flow_classify_dump_t * mp)
473 unix_shared_memory_queue_t *q;
474 flow_classify_main_t *pcm = &flow_classify_main;
478 q = vl_api_client_index_to_input_queue (mp->client_index);
482 vec_tbl = pcm->classify_table_index_by_sw_if_index[mp->type];
484 if (vec_len (vec_tbl))
486 for (i = 0; i < vec_len (vec_tbl); i++)
488 if (vec_elt (vec_tbl, i) == ~0)
491 send_flow_classify_details (i, vec_elt (vec_tbl, i), q,
498 * classify_api_hookup
499 * Add vpe's API message handlers to the table.
500 * vlib has alread mapped shared memory and
501 * added the client registration handlers.
502 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
504 #define vl_msg_name_crc_list
505 #include <vnet/vnet_all_api_h.h>
506 #undef vl_msg_name_crc_list
509 setup_message_id_table (api_main_t * am)
511 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
512 foreach_vl_msg_name_crc_classify;
516 static clib_error_t *
517 classify_api_hookup (vlib_main_t * vm)
519 api_main_t *am = &api_main;
522 vl_msg_api_set_handlers(VL_API_##N, #n, \
523 vl_api_##n##_t_handler, \
525 vl_api_##n##_t_endian, \
526 vl_api_##n##_t_print, \
527 sizeof(vl_api_##n##_t), 1);
532 * Set up the (msg_name, crc, message-id) table
534 setup_message_id_table (am);
539 VLIB_API_INIT_FUNCTION (classify_api_hookup);
542 * fd.io coding-style-patch-verification: ON
545 * eval: (c-set-style "gnu")