blob: fc57b006d37f93cf279183584cf172df74618940 [file] [log] [blame]
Pavel Kotucekd2c97d92017-01-24 10:58:12 +01001/*
2 *------------------------------------------------------------------
3 * classify_api.c - classify api
4 *
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:
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
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 *------------------------------------------------------------------
18 */
19
20#include <vnet/vnet.h>
21#include <vlibmemory/api.h>
22
23#include <vnet/interface.h>
24#include <vnet/api_errno.h>
25
26#include <vnet/classify/vnet_classify.h>
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +010027#include <vnet/classify/in_out_acl.h>
Pavel Kotucekd2c97d92017-01-24 10:58:12 +010028#include <vnet/classify/policer_classify.h>
29#include <vnet/classify/flow_classify.h>
Neale Rannsb8d44812017-11-10 06:53:54 -080030#include <vnet/l2/l2_classify.h>
Neale Rannse4031132020-10-26 13:00:06 +000031#include <vnet/ip/ip6.h>
32#include <vnet/ip/ip4.h>
Pavel Kotucekd2c97d92017-01-24 10:58:12 +010033
Filip Tehlar43428db2021-06-04 13:50:34 +000034#include <classify/classify.api_enum.h>
35#include <classify/classify.api_types.h>
Pavel Kotucekd2c97d92017-01-24 10:58:12 +010036
Filip Tehlar43428db2021-06-04 13:50:34 +000037#define REPLY_MSG_ID_BASE msg_id_base
Pavel Kotucekd2c97d92017-01-24 10:58:12 +010038#include <vlibapi/api_helper_macros.h>
39
Filip Tehlar43428db2021-06-04 13:50:34 +000040static u16 msg_id_base;
Pavel Kotucekd2c97d92017-01-24 10:58:12 +010041
42#define foreach_classify_add_del_table_field \
43_(table_index) \
44_(nbuckets) \
45_(memory_size) \
46_(skip_n_vectors) \
47_(match_n_vectors) \
48_(next_table_index) \
49_(miss_next_index) \
Juraj Sloboda75282452018-06-12 14:20:49 +020050_(mask_len)
Pavel Kotucekd2c97d92017-01-24 10:58:12 +010051
Jon Loeliger5c1e48c2020-10-15 14:41:36 -040052
53static void vl_api_classify_pcap_lookup_table_t_handler
54 (vl_api_classify_pcap_lookup_table_t * mp)
55{
56 vnet_classify_main_t *cm = &vnet_classify_main;
57 vl_api_registration_t *reg;
58 vl_api_classify_pcap_lookup_table_reply_t *rmp;
59 int rv = 0;
60 u32 table_index = ~0;
61
62 reg = vl_api_client_index_to_registration (mp->client_index);
63 if (!reg)
64 return;
65
66 u32 n_skip = ntohl (mp->skip_n_vectors);
67 u32 n_match = ntohl (mp->match_n_vectors);
68 u32 mask_len = ntohl (mp->mask_len);
69 u32 sw_if_index = ntohl (mp->sw_if_index);
70
Dave Barach99c6dc62021-02-15 12:46:47 -050071 if (n_skip > 5 || n_match == 0 || n_match > 5 ||
72 mask_len != n_match * sizeof (u32x4) || sw_if_index == ~0 ||
73 sw_if_index >= vec_len (cm->classify_table_index_by_sw_if_index))
Jon Loeliger5c1e48c2020-10-15 14:41:36 -040074 {
75 rv = VNET_API_ERROR_INVALID_VALUE;
76 goto out;
77 }
78
79 u32 table_chain;
80 table_chain = classify_get_pcap_chain (cm, sw_if_index);
81
82 u8 *mask_vec = 0;
83 vec_validate (mask_vec, mask_len - 1);
84 clib_memcpy (mask_vec, mp->mask, mask_len);
85
86 if (table_chain != ~0)
87 table_index = classify_lookup_chain (table_chain,
88 mask_vec, n_skip, n_match);
89
90 vec_free (mask_vec);
91
92out:
93 rmp = vl_msg_api_alloc (sizeof (*rmp));
Matthew Smithc773a7d2021-09-28 16:02:10 -050094 rmp->_vl_msg_id =
95 ntohs (REPLY_MSG_ID_BASE + VL_API_CLASSIFY_PCAP_LOOKUP_TABLE_REPLY);
Jon Loeliger5c1e48c2020-10-15 14:41:36 -040096 rmp->context = mp->context;
97 rmp->retval = ntohl (rv);
98 rmp->table_index = htonl (table_index);
99
100 vl_api_send_msg (reg, (u8 *) rmp);
101}
102
103static void vl_api_classify_pcap_set_table_t_handler
104 (vl_api_classify_pcap_set_table_t * mp)
105{
106 vnet_classify_main_t *cm = &vnet_classify_main;
107 vl_api_classify_pcap_set_table_reply_t *rmp;
108 vl_api_registration_t *reg;
109 int rv = 0;
110
111 reg = vl_api_client_index_to_registration (mp->client_index);
112 if (!reg)
113 return;
114
115 u32 table_index = ntohl (mp->table_index);
116 u32 sw_if_index = ntohl (mp->sw_if_index);
117
Maxime Peimddc16cf2023-01-13 08:04:55 +0000118 if (sw_if_index == ~0 ||
119 (table_index != ~0 && pool_is_free_index (cm->tables, table_index)))
Jon Loeliger5c1e48c2020-10-15 14:41:36 -0400120 {
121 rv = VNET_API_ERROR_INVALID_VALUE;
122 goto out;
123 }
124
125 /*
126 * Maybe reorder tables such that masks are most-specify to least-specific.
127 */
128 if (table_index != ~0 && mp->sort_masks)
129 table_index = classify_sort_table_chain (cm, table_index);
130
131 classify_set_pcap_chain (cm, sw_if_index, table_index);
132
133out:
134 rmp = vl_msg_api_alloc (sizeof (*rmp));
Matthew Smithc773a7d2021-09-28 16:02:10 -0500135 rmp->_vl_msg_id =
136 ntohs (REPLY_MSG_ID_BASE + VL_API_CLASSIFY_PCAP_SET_TABLE_REPLY);
Jon Loeliger5c1e48c2020-10-15 14:41:36 -0400137 rmp->context = mp->context;
138 rmp->retval = ntohl (rv);
139 rmp->table_index = htonl (table_index);
140
141 vl_api_send_msg (reg, (u8 *) rmp);
142}
143
144static void vl_api_classify_pcap_get_tables_t_handler
145 (vl_api_classify_pcap_get_tables_t * mp)
146{
147 vnet_classify_main_t *cm = &vnet_classify_main;
148 vl_api_classify_pcap_get_tables_reply_t *rmp;
149 vl_api_registration_t *reg;
150 int rv = 0;
151 u32 *tables = 0;
152 u32 count;
153
154 reg = vl_api_client_index_to_registration (mp->client_index);
155 if (!reg)
156 return;
157
158 u32 sw_if_index = ntohl (mp->sw_if_index);
159 if (sw_if_index == ~0
160 || sw_if_index >= vec_len (cm->classify_table_index_by_sw_if_index))
161 {
162 rv = VNET_API_ERROR_INVALID_VALUE;
163 goto out;
164 }
165
166 u32 table_index = classify_get_pcap_chain (cm, sw_if_index);
167 if (table_index == ~0)
168 goto out;
169
170 /*
171 * Form a vector of all classifier tables in this chain.
172 */
173 vnet_classify_table_t *t;
174 u32 i;
175
176 for (i = table_index; i != ~0; i = t->next_table_index)
177 {
178 vec_add1 (tables, i);
179 t = pool_elt_at_index (cm->tables, i);
180 }
181
182out:
183 count = vec_len (tables);
184 rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp) + count * sizeof (u32));
Matthew Smithc773a7d2021-09-28 16:02:10 -0500185 rmp->_vl_msg_id =
186 ntohs (REPLY_MSG_ID_BASE + VL_API_CLASSIFY_PCAP_GET_TABLES_REPLY);
Jon Loeliger5c1e48c2020-10-15 14:41:36 -0400187 rmp->context = mp->context;
188 rmp->retval = ntohl (rv);
189 rmp->count = htonl (count);
190
191 for (i = 0; i < count; ++i)
192 {
193 rmp->indices[i] = htonl (tables[i]);
194 }
195
196 vec_free (tables);
197
198 vl_api_send_msg (reg, (u8 *) rmp);
199}
200
201
202static void vl_api_classify_trace_lookup_table_t_handler
203 (vl_api_classify_trace_lookup_table_t * mp)
204{
205 vl_api_classify_trace_lookup_table_reply_t *rmp;
206 vl_api_registration_t *reg;
207 int rv = 0;
208 u32 table_index = ~0;
209
210 reg = vl_api_client_index_to_registration (mp->client_index);
211 if (!reg)
212 return;
213
214 u32 n_skip = ntohl (mp->skip_n_vectors);
215 u32 n_match = ntohl (mp->match_n_vectors);
216 u32 mask_len = ntohl (mp->mask_len);
217 if (n_skip > 5
218 || n_match == 0 || n_match > 5 || mask_len != n_match * sizeof (u32x4))
219 {
220 rv = VNET_API_ERROR_INVALID_VALUE;
221 goto out;
222 }
223
224 u32 table_chain;
225 table_chain = classify_get_trace_chain ();
226
227 u8 *mask_vec = 0;
228 vec_validate (mask_vec, mask_len - 1);
229 clib_memcpy (mask_vec, mp->mask, mask_len);
230
231 if (table_chain != ~0)
232 table_index = classify_lookup_chain (table_chain,
233 mask_vec, n_skip, n_match);
234 vec_free (mask_vec);
235
236out:
237 rmp = vl_msg_api_alloc (sizeof (*rmp));
Matthew Smithc773a7d2021-09-28 16:02:10 -0500238 rmp->_vl_msg_id =
239 ntohs ((REPLY_MSG_ID_BASE + VL_API_CLASSIFY_TRACE_LOOKUP_TABLE_REPLY));
Jon Loeliger5c1e48c2020-10-15 14:41:36 -0400240 rmp->context = mp->context;
241 rmp->retval = ntohl (rv);
242 rmp->table_index = htonl (table_index);
243
244 vl_api_send_msg (reg, (u8 *) rmp);
245}
246
247static void vl_api_classify_trace_set_table_t_handler
248 (vl_api_classify_trace_set_table_t * mp)
249{
250 vnet_classify_main_t *cm = &vnet_classify_main;
251 vl_api_classify_trace_set_table_reply_t *rmp;
252 vl_api_registration_t *reg;
253 int rv = 0;
254
255 reg = vl_api_client_index_to_registration (mp->client_index);
256 if (!reg)
257 return;
258
259 u32 table_index = ntohl (mp->table_index);
260 if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
261 {
262 rv = VNET_API_ERROR_INVALID_VALUE;
263 goto out;
264 }
265
266 /*
267 * Maybe reorder tables such that masks are most-specific to least-specific.
268 */
269 if (table_index != ~0 && mp->sort_masks)
270 table_index = classify_sort_table_chain (cm, table_index);
271
272 classify_set_trace_chain (cm, table_index);
273
274out:
275 rmp = vl_msg_api_alloc (sizeof (*rmp));
Matthew Smithc773a7d2021-09-28 16:02:10 -0500276 rmp->_vl_msg_id =
277 ntohs ((REPLY_MSG_ID_BASE + VL_API_CLASSIFY_TRACE_SET_TABLE_REPLY));
Jon Loeliger5c1e48c2020-10-15 14:41:36 -0400278 rmp->context = mp->context;
279 rmp->retval = ntohl (rv);
280 rmp->table_index = htonl (table_index);
281
282 vl_api_send_msg (reg, (u8 *) rmp);
283}
284
285static void vl_api_classify_trace_get_tables_t_handler
286 (vl_api_classify_trace_get_tables_t * mp)
287{
288 vnet_classify_main_t *cm = &vnet_classify_main;
289 vl_api_classify_trace_get_tables_reply_t *rmp;
290 vl_api_registration_t *reg;
291 int rv = 0;
292 u32 *tables = 0;
293 u32 count;
294
295 reg = vl_api_client_index_to_registration (mp->client_index);
296 if (!reg)
297 return;
298
299 u32 table_index = classify_get_trace_chain ();
300 if (table_index == ~0)
301 goto out;
302
303 /*
304 * Form a vector of all classifier tables in this chain.
305 */
306 vnet_classify_table_t *t;
307 u32 i;
308
309 for (i = table_index; i != ~0; i = t->next_table_index)
310 {
311 vec_add1 (tables, i);
312 t = pool_elt_at_index (cm->tables, i);
313 }
314
315out:
316 count = vec_len (tables);
317 rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp) + count * sizeof (u32));
Matthew Smithc773a7d2021-09-28 16:02:10 -0500318 rmp->_vl_msg_id =
319 ntohs (REPLY_MSG_ID_BASE + VL_API_CLASSIFY_TRACE_GET_TABLES_REPLY);
Jon Loeliger5c1e48c2020-10-15 14:41:36 -0400320 rmp->context = mp->context;
321 rmp->retval = ntohl (rv);
322 rmp->count = htonl (count);
323
324 for (i = 0; i < count; ++i)
325 {
326 rmp->indices[i] = htonl (tables[i]);
327 }
328
329 vec_free (tables);
330
331 vl_api_send_msg (reg, (u8 *) rmp);
332}
333
334
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100335static void vl_api_classify_add_del_table_t_handler
336 (vl_api_classify_add_del_table_t * mp)
337{
338 vl_api_classify_add_del_table_reply_t *rmp;
339 vnet_classify_main_t *cm = &vnet_classify_main;
340 vnet_classify_table_t *t;
341 int rv;
342
343#define _(a) u32 a;
344 foreach_classify_add_del_table_field;
345#undef _
346
347#define _(a) a = ntohl(mp->a);
348 foreach_classify_add_del_table_field;
349#undef _
350
Juraj Sloboda75282452018-06-12 14:20:49 +0200351 if (mask_len != match_n_vectors * sizeof (u32x4))
352 {
353 rv = VNET_API_ERROR_INVALID_VALUE;
354 goto out;
355 }
356
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100357 /* The underlying API fails silently, on purpose, so check here */
358 if (mp->is_add == 0) /* delete */
359 {
360 if (pool_is_free_index (cm->tables, table_index))
361 {
362 rv = VNET_API_ERROR_NO_SUCH_TABLE;
363 goto out;
364 }
365 }
366 else /* add or update */
367 {
368 if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
369 table_index = ~0;
370 }
371
Jakub Grajciar692bfc82019-08-27 10:16:44 +0200372 u8 current_data_flag = mp->current_data_flag;
373 i16 current_data_offset = clib_net_to_host_i16 (mp->current_data_offset);
374
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100375 rv = vnet_classify_add_del_table
376 (cm, mp->mask, nbuckets, memory_size,
377 skip_n_vectors, match_n_vectors,
378 next_table_index, miss_next_index, &table_index,
379 current_data_flag, current_data_offset, mp->is_add, mp->del_chain);
380
381out:
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100382 REPLY_MACRO2(VL_API_CLASSIFY_ADD_DEL_TABLE_REPLY,
383 ({
384 if (rv == 0 && mp->is_add)
385 {
386 t = pool_elt_at_index (cm->tables, table_index);
Jakub Grajciar692bfc82019-08-27 10:16:44 +0200387 rmp->skip_n_vectors = htonl(t->skip_n_vectors);
388 rmp->match_n_vectors = htonl(t->match_n_vectors);
389 rmp->new_table_index = htonl(table_index);
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100390 }
391 else
392 {
393 rmp->skip_n_vectors = ~0;
394 rmp->match_n_vectors = ~0;
395 rmp->new_table_index = ~0;
396 }
397 }));
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100398}
399
400static void vl_api_classify_add_del_session_t_handler
401 (vl_api_classify_add_del_session_t * mp)
402{
403 vnet_classify_main_t *cm = &vnet_classify_main;
404 vl_api_classify_add_del_session_reply_t *rmp;
405 int rv;
Juraj Sloboda75282452018-06-12 14:20:49 +0200406 u32 table_index, hit_next_index, opaque_index, metadata, match_len;
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100407 i32 advance;
408 u8 action;
Juraj Sloboda75282452018-06-12 14:20:49 +0200409 vnet_classify_table_t *t;
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100410
411 table_index = ntohl (mp->table_index);
412 hit_next_index = ntohl (mp->hit_next_index);
413 opaque_index = ntohl (mp->opaque_index);
414 advance = ntohl (mp->advance);
415 action = mp->action;
416 metadata = ntohl (mp->metadata);
Juraj Sloboda75282452018-06-12 14:20:49 +0200417 match_len = ntohl (mp->match_len);
418
419 if (pool_is_free_index (cm->tables, table_index))
420 {
421 rv = VNET_API_ERROR_NO_SUCH_TABLE;
422 goto out;
423 }
424
425 t = pool_elt_at_index (cm->tables, table_index);
426
427 if (match_len != (t->skip_n_vectors + t->match_n_vectors) * sizeof (u32x4))
428 {
429 rv = VNET_API_ERROR_INVALID_VALUE;
430 goto out;
431 }
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100432
433 rv = vnet_classify_add_del_session
434 (cm, table_index, mp->match, hit_next_index, opaque_index,
435 advance, action, metadata, mp->is_add);
436
Juraj Sloboda75282452018-06-12 14:20:49 +0200437out:
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100438 REPLY_MACRO (VL_API_CLASSIFY_ADD_DEL_SESSION_REPLY);
439}
440
441static void
442 vl_api_policer_classify_set_interface_t_handler
443 (vl_api_policer_classify_set_interface_t * mp)
444{
445 vlib_main_t *vm = vlib_get_main ();
446 vl_api_policer_classify_set_interface_reply_t *rmp;
447 int rv;
448 u32 sw_if_index, ip4_table_index, ip6_table_index, l2_table_index;
449
450 ip4_table_index = ntohl (mp->ip4_table_index);
451 ip6_table_index = ntohl (mp->ip6_table_index);
452 l2_table_index = ntohl (mp->l2_table_index);
453 sw_if_index = ntohl (mp->sw_if_index);
454
455 VALIDATE_SW_IF_INDEX (mp);
456
457 rv = vnet_set_policer_classify_intfc (vm, sw_if_index, ip4_table_index,
458 ip6_table_index, l2_table_index,
459 mp->is_add);
460
461 BAD_SW_IF_INDEX_LABEL;
462
463 REPLY_MACRO (VL_API_POLICER_CLASSIFY_SET_INTERFACE_REPLY);
464}
465
466static void
467send_policer_classify_details (u32 sw_if_index,
Florin Coras6c4dae22018-01-09 06:39:23 -0800468 u32 table_index, vl_api_registration_t * reg,
469 u32 context)
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100470{
471 vl_api_policer_classify_details_t *mp;
472
473 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400474 clib_memset (mp, 0, sizeof (*mp));
Matthew Smithc773a7d2021-09-28 16:02:10 -0500475 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_POLICER_CLASSIFY_DETAILS);
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100476 mp->context = context;
477 mp->sw_if_index = htonl (sw_if_index);
478 mp->table_index = htonl (table_index);
479
Florin Coras6c4dae22018-01-09 06:39:23 -0800480 vl_api_send_msg (reg, (u8 *) mp);
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100481}
482
483static void
484vl_api_policer_classify_dump_t_handler (vl_api_policer_classify_dump_t * mp)
485{
Florin Coras6c4dae22018-01-09 06:39:23 -0800486 vl_api_registration_t *reg;
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100487 policer_classify_main_t *pcm = &policer_classify_main;
488 u32 *vec_tbl;
489 int i;
Jakub Grajciar692bfc82019-08-27 10:16:44 +0200490 u32 filter_sw_if_index;
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100491
Florin Coras6c4dae22018-01-09 06:39:23 -0800492 reg = vl_api_client_index_to_registration (mp->client_index);
493 if (!reg)
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100494 return;
495
Jakub Grajciar692bfc82019-08-27 10:16:44 +0200496 filter_sw_if_index = ntohl (mp->sw_if_index);
Jon Loeliger95d2f302019-12-04 11:42:36 -0600497 if (filter_sw_if_index
498 >= vec_len (pcm->classify_table_index_by_sw_if_index[mp->type]))
499 return;
500
Jakub Grajciar692bfc82019-08-27 10:16:44 +0200501 if (filter_sw_if_index != ~0)
502 vec_tbl =
503 &pcm->classify_table_index_by_sw_if_index[mp->type][filter_sw_if_index];
504 else
505 vec_tbl = pcm->classify_table_index_by_sw_if_index[mp->type];
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100506
507 if (vec_len (vec_tbl))
508 {
509 for (i = 0; i < vec_len (vec_tbl); i++)
510 {
511 if (vec_elt (vec_tbl, i) == ~0)
512 continue;
513
Florin Coras6c4dae22018-01-09 06:39:23 -0800514 send_policer_classify_details (i, vec_elt (vec_tbl, i), reg,
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100515 mp->context);
516 }
517 }
518}
519
520static void
521vl_api_classify_table_ids_t_handler (vl_api_classify_table_ids_t * mp)
522{
Florin Coras6c4dae22018-01-09 06:39:23 -0800523 vl_api_registration_t *reg;
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100524
Florin Coras6c4dae22018-01-09 06:39:23 -0800525 reg = vl_api_client_index_to_registration (mp->client_index);
526 if (!reg)
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100527 return;
528
529 vnet_classify_main_t *cm = &vnet_classify_main;
530 vnet_classify_table_t *t;
531 u32 *table_ids = 0;
532 u32 count;
533
Damjan Marionb2c31b62020-12-13 21:47:40 +0100534 pool_foreach (t, cm->tables)
535 {
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100536 vec_add1 (table_ids, ntohl(t - cm->tables));
Damjan Marionb2c31b62020-12-13 21:47:40 +0100537 }
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100538 count = vec_len (table_ids);
539
540 vl_api_classify_table_ids_reply_t *rmp;
541 rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp) + count * sizeof (u32));
Matthew Smithc773a7d2021-09-28 16:02:10 -0500542 rmp->_vl_msg_id =
543 ntohs (REPLY_MSG_ID_BASE + VL_API_CLASSIFY_TABLE_IDS_REPLY);
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100544 rmp->context = mp->context;
545 rmp->count = ntohl (count);
546 clib_memcpy (rmp->ids, table_ids, count * sizeof (u32));
547 rmp->retval = 0;
548
Florin Coras6c4dae22018-01-09 06:39:23 -0800549 vl_api_send_msg (reg, (u8 *) rmp);
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100550
551 vec_free (table_ids);
552}
553
554static void
555 vl_api_classify_table_by_interface_t_handler
556 (vl_api_classify_table_by_interface_t * mp)
557{
558 vl_api_classify_table_by_interface_reply_t *rmp;
559 int rv = 0;
560
561 u32 sw_if_index = ntohl (mp->sw_if_index);
562 u32 *acl = 0;
563
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +0100564 vec_validate (acl, IN_OUT_ACL_N_TABLES - 1);
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100565 vec_set (acl, ~0);
566
567 VALIDATE_SW_IF_INDEX (mp);
568
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +0100569 in_out_acl_main_t *am = &in_out_acl_main;
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100570
571 int if_idx;
572 u32 type;
573
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +0100574 for (type = 0; type < IN_OUT_ACL_N_TABLES; type++)
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100575 {
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +0100576 u32 *vec_tbl =
577 am->classify_table_index_by_sw_if_index[IN_OUT_ACL_INPUT_TABLE_GROUP]
578 [type];
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100579 if (vec_len (vec_tbl))
580 {
581 for (if_idx = 0; if_idx < vec_len (vec_tbl); if_idx++)
582 {
583 if (vec_elt (vec_tbl, if_idx) == ~0 || sw_if_index != if_idx)
584 {
585 continue;
586 }
587 acl[type] = vec_elt (vec_tbl, if_idx);
588 }
589 }
590 }
591
592 BAD_SW_IF_INDEX_LABEL;
593
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100594 REPLY_MACRO2(VL_API_CLASSIFY_TABLE_BY_INTERFACE_REPLY,
595 ({
596 rmp->sw_if_index = ntohl(sw_if_index);
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +0100597 rmp->l2_table_id = ntohl(acl[IN_OUT_ACL_TABLE_L2]);
598 rmp->ip4_table_id = ntohl(acl[IN_OUT_ACL_TABLE_IP4]);
599 rmp->ip6_table_id = ntohl(acl[IN_OUT_ACL_TABLE_IP6]);
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100600 }));
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100601 vec_free (acl);
602}
603
604static void
605vl_api_classify_table_info_t_handler (vl_api_classify_table_info_t * mp)
606{
Florin Coras6c4dae22018-01-09 06:39:23 -0800607 vl_api_registration_t *reg;
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100608
Florin Coras6c4dae22018-01-09 06:39:23 -0800609 reg = vl_api_client_index_to_registration (mp->client_index);
610 if (!reg)
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100611 return;
612
613 vl_api_classify_table_info_reply_t *rmp = 0;
614
615 vnet_classify_main_t *cm = &vnet_classify_main;
616 u32 table_id = ntohl (mp->table_id);
617 vnet_classify_table_t *t;
618
Matthew Smithc773a7d2021-09-28 16:02:10 -0500619 pool_foreach (t, cm->tables)
Damjan Marionb2c31b62020-12-13 21:47:40 +0100620 {
Matthew Smithc773a7d2021-09-28 16:02:10 -0500621 if (table_id == t - cm->tables)
622 {
623 rmp = vl_msg_api_alloc_as_if_client (
624 sizeof (*rmp) + t->match_n_vectors * sizeof (u32x4));
625 rmp->_vl_msg_id =
626 ntohs (REPLY_MSG_ID_BASE + VL_API_CLASSIFY_TABLE_INFO_REPLY);
627 rmp->context = mp->context;
628 rmp->table_id = ntohl (table_id);
629 rmp->nbuckets = ntohl (t->nbuckets);
630 rmp->match_n_vectors = ntohl (t->match_n_vectors);
631 rmp->skip_n_vectors = ntohl (t->skip_n_vectors);
632 rmp->active_sessions = ntohl (t->active_elements);
633 rmp->next_table_index = ntohl (t->next_table_index);
634 rmp->miss_next_index = ntohl (t->miss_next_index);
635 rmp->mask_length = ntohl (t->match_n_vectors * sizeof (u32x4));
636 clib_memcpy (rmp->mask, t->mask,
637 t->match_n_vectors * sizeof (u32x4));
638 rmp->retval = 0;
639 break;
640 }
641 }
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100642
643 if (rmp == 0)
644 {
645 rmp = vl_msg_api_alloc (sizeof (*rmp));
Matthew Smithc773a7d2021-09-28 16:02:10 -0500646 rmp->_vl_msg_id =
647 ntohs (REPLY_MSG_ID_BASE + VL_API_CLASSIFY_TABLE_INFO_REPLY);
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100648 rmp->context = mp->context;
649 rmp->retval = ntohl (VNET_API_ERROR_CLASSIFY_TABLE_NOT_FOUND);
650 }
651
Florin Coras6c4dae22018-01-09 06:39:23 -0800652 vl_api_send_msg (reg, (u8 *) rmp);
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100653}
654
655static void
Florin Coras6c4dae22018-01-09 06:39:23 -0800656send_classify_session_details (vl_api_registration_t * reg,
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100657 u32 table_id,
658 u32 match_length,
659 vnet_classify_entry_t * e, u32 context)
660{
661 vl_api_classify_session_details_t *rmp;
662
Nathan Skrzypczakb376e922022-06-07 18:30:40 +0200663 rmp = vl_msg_api_alloc (sizeof (*rmp) + match_length);
Dave Barachb7b92992018-10-17 10:38:51 -0400664 clib_memset (rmp, 0, sizeof (*rmp));
Matthew Smithc773a7d2021-09-28 16:02:10 -0500665 rmp->_vl_msg_id =
666 ntohs (REPLY_MSG_ID_BASE + VL_API_CLASSIFY_SESSION_DETAILS);
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100667 rmp->context = context;
668 rmp->table_id = ntohl (table_id);
669 rmp->hit_next_index = ntohl (e->next_index);
670 rmp->advance = ntohl (e->advance);
671 rmp->opaque_index = ntohl (e->opaque_index);
672 rmp->match_length = ntohl (match_length);
673 clib_memcpy (rmp->match, e->key, match_length);
674
Florin Coras6c4dae22018-01-09 06:39:23 -0800675 vl_api_send_msg (reg, (u8 *) rmp);
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100676}
677
678static void
679vl_api_classify_session_dump_t_handler (vl_api_classify_session_dump_t * mp)
680{
681 vnet_classify_main_t *cm = &vnet_classify_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800682 vl_api_registration_t *reg;
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100683
684 u32 table_id = ntohl (mp->table_id);
685 vnet_classify_table_t *t;
686
Florin Coras6c4dae22018-01-09 06:39:23 -0800687 reg = vl_api_client_index_to_registration (mp->client_index);
688 if (!reg)
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100689 return;
690
Damjan Marionb2c31b62020-12-13 21:47:40 +0100691 pool_foreach (t, cm->tables)
692 {
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100693 if (table_id == t - cm->tables)
694 {
695 vnet_classify_bucket_t * b;
696 vnet_classify_entry_t * v, * save_v;
697 int i, j, k;
698
699 for (i = 0; i < t->nbuckets; i++)
700 {
701 b = &t->buckets [i];
702 if (b->offset == 0)
703 continue;
704
705 save_v = vnet_classify_get_entry (t, b->offset);
706 for (j = 0; j < (1<<b->log2_pages); j++)
707 {
708 for (k = 0; k < t->entries_per_page; k++)
709 {
710 v = vnet_classify_entry_at_index
711 (t, save_v, j*t->entries_per_page + k);
712 if (vnet_classify_entry_is_free (v))
713 continue;
714
715 send_classify_session_details
Florin Coras6c4dae22018-01-09 06:39:23 -0800716 (reg, table_id, t->match_n_vectors * sizeof (u32x4),
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100717 v, mp->context);
718 }
719 }
720 }
721 break;
722 }
Damjan Marionb2c31b62020-12-13 21:47:40 +0100723 }
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100724}
725
726static void
727 vl_api_flow_classify_set_interface_t_handler
728 (vl_api_flow_classify_set_interface_t * mp)
729{
730 vlib_main_t *vm = vlib_get_main ();
731 vl_api_flow_classify_set_interface_reply_t *rmp;
732 int rv;
733 u32 sw_if_index, ip4_table_index, ip6_table_index;
734
735 ip4_table_index = ntohl (mp->ip4_table_index);
736 ip6_table_index = ntohl (mp->ip6_table_index);
737 sw_if_index = ntohl (mp->sw_if_index);
738
739 VALIDATE_SW_IF_INDEX (mp);
740
741 rv = vnet_set_flow_classify_intfc (vm, sw_if_index, ip4_table_index,
742 ip6_table_index, mp->is_add);
743
744 BAD_SW_IF_INDEX_LABEL;
745
746 REPLY_MACRO (VL_API_FLOW_CLASSIFY_SET_INTERFACE_REPLY);
747}
748
749static void
750send_flow_classify_details (u32 sw_if_index,
Florin Coras6c4dae22018-01-09 06:39:23 -0800751 u32 table_index, vl_api_registration_t * reg,
752 u32 context)
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100753{
754 vl_api_flow_classify_details_t *mp;
755
756 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400757 clib_memset (mp, 0, sizeof (*mp));
Matthew Smithc773a7d2021-09-28 16:02:10 -0500758 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_FLOW_CLASSIFY_DETAILS);
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100759 mp->context = context;
760 mp->sw_if_index = htonl (sw_if_index);
761 mp->table_index = htonl (table_index);
762
Florin Coras6c4dae22018-01-09 06:39:23 -0800763 vl_api_send_msg (reg, (u8 *) mp);
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100764}
765
766static void
767vl_api_flow_classify_dump_t_handler (vl_api_flow_classify_dump_t * mp)
768{
Florin Coras6c4dae22018-01-09 06:39:23 -0800769 vl_api_registration_t *reg;
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100770 flow_classify_main_t *pcm = &flow_classify_main;
771 u32 *vec_tbl;
772 int i;
Jakub Grajciar692bfc82019-08-27 10:16:44 +0200773 u32 filter_sw_if_index;
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100774
Florin Coras6c4dae22018-01-09 06:39:23 -0800775 reg = vl_api_client_index_to_registration (mp->client_index);
776 if (!reg)
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100777 return;
778
Jakub Grajciar692bfc82019-08-27 10:16:44 +0200779 filter_sw_if_index = ntohl (mp->sw_if_index);
Jon Loeliger95d2f302019-12-04 11:42:36 -0600780 if (filter_sw_if_index
781 >= vec_len (pcm->classify_table_index_by_sw_if_index[mp->type]))
782 return;
783
Jakub Grajciar692bfc82019-08-27 10:16:44 +0200784 if (filter_sw_if_index != ~0)
785 vec_tbl =
786 &pcm->classify_table_index_by_sw_if_index[mp->type][filter_sw_if_index];
787 else
788 vec_tbl = pcm->classify_table_index_by_sw_if_index[mp->type];
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100789
790 if (vec_len (vec_tbl))
791 {
792 for (i = 0; i < vec_len (vec_tbl); i++)
793 {
794 if (vec_elt (vec_tbl, i) == ~0)
795 continue;
796
Florin Coras6c4dae22018-01-09 06:39:23 -0800797 send_flow_classify_details (i, vec_elt (vec_tbl, i), reg,
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100798 mp->context);
799 }
800 }
801}
802
Neale Rannsb8d44812017-11-10 06:53:54 -0800803static void vl_api_classify_set_interface_ip_table_t_handler
804 (vl_api_classify_set_interface_ip_table_t * mp)
805{
806 vlib_main_t *vm = vlib_get_main ();
807 vl_api_classify_set_interface_ip_table_reply_t *rmp;
808 int rv;
809
810 VALIDATE_SW_IF_INDEX (mp);
811
812 u32 table_index = ntohl (mp->table_index);
813 u32 sw_if_index = ntohl (mp->sw_if_index);
814
815 if (mp->is_ipv6)
816 rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
817 else
818 rv = vnet_set_ip4_classify_intfc (vm, sw_if_index, table_index);
819
820 BAD_SW_IF_INDEX_LABEL;
821
822 REPLY_MACRO (VL_API_CLASSIFY_SET_INTERFACE_IP_TABLE_REPLY);
823}
824
825static void vl_api_classify_set_interface_l2_tables_t_handler
826 (vl_api_classify_set_interface_l2_tables_t * mp)
827{
828 vl_api_classify_set_interface_l2_tables_reply_t *rmp;
829 int rv;
830 u32 sw_if_index, ip4_table_index, ip6_table_index, other_table_index;
831 int enable;
832
833 ip4_table_index = ntohl (mp->ip4_table_index);
834 ip6_table_index = ntohl (mp->ip6_table_index);
835 other_table_index = ntohl (mp->other_table_index);
836 sw_if_index = ntohl (mp->sw_if_index);
837
838 VALIDATE_SW_IF_INDEX (mp);
839
840 if (mp->is_input)
841 rv = vnet_l2_input_classify_set_tables (sw_if_index, ip4_table_index,
842 ip6_table_index,
843 other_table_index);
844 else
845 rv = vnet_l2_output_classify_set_tables (sw_if_index, ip4_table_index,
846 ip6_table_index,
847 other_table_index);
848
849 if (rv == 0)
850 {
851 if (ip4_table_index != ~0 || ip6_table_index != ~0
852 || other_table_index != ~0)
853 enable = 1;
854 else
855 enable = 0;
856
857 if (mp->is_input)
858 vnet_l2_input_classify_enable_disable (sw_if_index, enable);
859 else
860 vnet_l2_output_classify_enable_disable (sw_if_index, enable);
861 }
862
863 BAD_SW_IF_INDEX_LABEL;
864
865 REPLY_MACRO (VL_API_CLASSIFY_SET_INTERFACE_L2_TABLES_REPLY);
866}
867
868static void vl_api_input_acl_set_interface_t_handler
869 (vl_api_input_acl_set_interface_t * mp)
870{
871 vlib_main_t *vm = vlib_get_main ();
872 vl_api_input_acl_set_interface_reply_t *rmp;
873 int rv;
874
875 VALIDATE_SW_IF_INDEX (mp);
876
877 u32 ip4_table_index = ntohl (mp->ip4_table_index);
878 u32 ip6_table_index = ntohl (mp->ip6_table_index);
879 u32 l2_table_index = ntohl (mp->l2_table_index);
880 u32 sw_if_index = ntohl (mp->sw_if_index);
881
882 rv = vnet_set_input_acl_intfc (vm, sw_if_index, ip4_table_index,
883 ip6_table_index, l2_table_index, mp->is_add);
884
885 BAD_SW_IF_INDEX_LABEL;
886
887 REPLY_MACRO (VL_API_INPUT_ACL_SET_INTERFACE_REPLY);
888}
889
Benoît Ganneabb2a422021-09-30 13:41:00 +0200890static void
891vl_api_punt_acl_add_del_t_handler (vl_api_punt_acl_add_del_t *mp)
892{
893 vlib_main_t *vm = vlib_get_main ();
894 vl_api_punt_acl_add_del_reply_t *rmp;
895 int rv;
896
897 rv = vnet_set_in_out_acl_intfc (
898 vm, 0 /* sw_if_index */, ~0 /* ip4_table_index */,
899 ~0 /* ip6_table_index */, ~0 /* l2_table_index */,
900 ntohl (mp->ip4_table_index), ntohl (mp->ip6_table_index), mp->is_add,
901 0 /* is_output */);
902
903 REPLY_MACRO (VL_API_PUNT_ACL_ADD_DEL_REPLY);
904}
905
Benoît Ganne7fc0ee72021-10-13 19:16:07 +0200906static void
907vl_api_punt_acl_get_t_handler (vl_api_punt_acl_get_t *mp)
908{
909 vl_api_punt_acl_get_reply_t *rmp;
910 int rv = 0;
911
912 const in_out_acl_main_t *am = &in_out_acl_main;
913
914 u32 *const *tables =
915 am->classify_table_index_by_sw_if_index[IN_OUT_ACL_INPUT_TABLE_GROUP];
916 const u32 *ip4_table = tables[IN_OUT_ACL_TABLE_IP4_PUNT];
917 const u32 *ip6_table = tables[IN_OUT_ACL_TABLE_IP6_PUNT];
918 const u32 ip4_table_index = vec_len (ip4_table) ? ip4_table[0] : ~0;
919 const u32 ip6_table_index = vec_len (ip6_table) ? ip6_table[0] : ~0;
920
921 REPLY_MACRO2 (VL_API_PUNT_ACL_GET_REPLY, ({
922 rmp->ip4_table_index = ntohl (ip4_table_index);
923 rmp->ip6_table_index = ntohl (ip6_table_index);
924 }));
925}
926
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +0100927static void vl_api_output_acl_set_interface_t_handler
928 (vl_api_output_acl_set_interface_t * mp)
929{
930 vlib_main_t *vm = vlib_get_main ();
931 vl_api_output_acl_set_interface_reply_t *rmp;
932 int rv;
933
934 VALIDATE_SW_IF_INDEX (mp);
935
936 u32 ip4_table_index = ntohl (mp->ip4_table_index);
937 u32 ip6_table_index = ntohl (mp->ip6_table_index);
938 u32 l2_table_index = ntohl (mp->l2_table_index);
939 u32 sw_if_index = ntohl (mp->sw_if_index);
940
941 rv = vnet_set_output_acl_intfc (vm, sw_if_index, ip4_table_index,
942 ip6_table_index, l2_table_index,
943 mp->is_add);
944
945 BAD_SW_IF_INDEX_LABEL;
946
947 REPLY_MACRO (VL_API_OUTPUT_ACL_SET_INTERFACE_REPLY);
948}
949
Filip Tehlar43428db2021-06-04 13:50:34 +0000950#include <classify/classify.api.c>
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100951
952static clib_error_t *
953classify_api_hookup (vlib_main_t * vm)
954{
Ole Troan3459ece2021-09-27 17:11:34 +0200955 api_main_t *am = vlibapi_get_main ();
956
957 /*
958 * Trace space for classifier mask+match
959 */
Damjan Marioncada9eb2022-05-18 22:16:11 +0200960 vl_api_increase_msg_trace_size (am, VL_API_CLASSIFY_ADD_DEL_TABLE,
961 5 * sizeof (u32x4));
962 vl_api_increase_msg_trace_size (am, VL_API_CLASSIFY_ADD_DEL_SESSION,
963 5 * sizeof (u32x4));
Ole Troan3459ece2021-09-27 17:11:34 +0200964
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100965 /*
966 * Set up the (msg_name, crc, message-id) table
967 */
Filip Tehlar43428db2021-06-04 13:50:34 +0000968 msg_id_base = setup_message_id_table ();
Pavel Kotucekd2c97d92017-01-24 10:58:12 +0100969
970 return 0;
971}
972
973VLIB_API_INIT_FUNCTION (classify_api_hookup);
974
975/*
976 * fd.io coding-style-patch-verification: ON
977 *
978 * Local Variables:
979 * eval: (c-set-style "gnu")
980 * End:
981 */