blob: 03421210d037c168f7941742d338bb9880560759 [file] [log] [blame]
Dave Barach9137e542019-09-13 17:47:50 -04001/*
2 * trace_classify.h - Use the classifier to decide if a packet is traced
3 *
4 * Copyright (c) 2019 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17#include <vlib/vlib.h>
18#include <vnet/vnet.h>
19#include <vppinfra/error.h>
20#include <vnet/classify/vnet_classify.h>
21
22/** @file trace_classify.h
23 * Use the vpp classifier to decide whether to trace packets
24 */
25
26/** @brief vnet_is_packet_traced
27 * @param vlib_buffer_t *b - packet to classify
28 * @param int func - 0 => use classifier w/ supplied table index
29 * @param u32 classify_table_index - classifier table index
30 * @return 0 => no trace, 1 => trace, -1 => error
31 */
Mohammed Hawariccd30702023-06-09 16:50:56 +020032int vnet_is_packet_traced (vlib_buffer_t *b, u32 classify_table_index,
33 int func);
Dave Barach9137e542019-09-13 17:47:50 -040034
35static inline int
36vnet_is_packet_traced_inline (vlib_buffer_t * b,
37 u32 classify_table_index, int func)
38{
39 vnet_classify_main_t *vcm = &vnet_classify_main;
40 vnet_classify_table_t *t;
41 vnet_classify_entry_t *e;
42 u64 hash;
43
44 /*$$$ add custom classifiers here, if any */
45 if (func != 0)
46 return -1;
47
Mohammed Hawaribc67e9f2023-12-08 11:16:54 +010048 if (classify_table_index == ~0)
49 return -1;
50
Dave Barach9137e542019-09-13 17:47:50 -040051 /* This will happen... */
52 if (pool_is_free_index (vcm->tables, classify_table_index))
53 return -1;
54
55 /* Get the table */
56 t = pool_elt_at_index (vcm->tables, classify_table_index);
57
58 /* Hash the packet */
59 hash = vnet_classify_hash_packet (t, vlib_buffer_get_current (b));
60
61 /* See if there's a matching entry */
62 e = vnet_classify_find_entry (t, vlib_buffer_get_current (b), hash,
63 0 /* time = 0, disables hit-counter */ );
64 /* Hit means trace the packet... */
65 if (e)
66 {
67 /* Manual hit accounting */
68 e->hits++;
69 return 1;
70 }
71
72 /*
73 * Look for a hit in a less-specific table.
74 * Performance hint: for this use-case, don't go there.
75 */
76 while (1)
77 {
78 /* Most likely, we're done right now */
79 if (PREDICT_TRUE (t->next_table_index == ~0))
80 return 0;
81 t = pool_elt_at_index (vcm->tables, t->next_table_index);
82
83 /* Compute hash for this table */
84 hash = vnet_classify_hash_packet (t, vlib_buffer_get_current (b));
85
86 /* See if there's a matching entry */
87 e = vnet_classify_find_entry (t, vlib_buffer_get_current (b), hash,
88 0 /* time = 0, disables hit-counter */ );
89 if (e)
90 {
91 /* Manual hit accounting */
92 e->hits++;
93 return 1;
94 }
95 }
96 /* NOTREACHED */
97}
98
99/*
100 * fd.io coding-style-patch-verification: ON
101 *
102 * Local Variables:
103 * eval: (c-set-style "gnu")
104 * End:
105 */