aboutsummaryrefslogtreecommitdiffstats
path: root/src/vnet/classify/trace_classify.h
blob: fea4ddee7e7dee34b64c62b3edfe8c03492143c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
 * trace_classify.h - Use the classifier to decide if a packet is traced
 *
 * Copyright (c) 2019 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <vlib/vlib.h>
#include <vnet/vnet.h>
#include <vppinfra/error.h>
#include <vnet/classify/vnet_classify.h>

/** @file trace_classify.h
 * Use the vpp classifier to decide whether to trace packets
 */

/** @brief vnet_is_packet_traced
 * @param vlib_buffer_t *b - packet to classify
 * @param int func - 0 => use classifier w/ supplied table index
 * @param u32 classify_table_index - classifier table index
 * @return 0 => no trace, 1 => trace, -1 => error
 */
int vnet_is_packet_traced (vlib_buffer_t *b, u32 classify_table_index,
			   int func);

static inline int
vnet_is_packet_traced_inline (vlib_buffer_t * b,
			      u32 classify_table_index, int func)
{
  vnet_classify_main_t *vcm = &vnet_classify_main;
  vnet_classify_table_t *t;
  vnet_classify_entry_t *e;
  u64 hash;

  /*$$$ add custom classifiers here, if any */
  if (func != 0)
    return -1;

  /* This will happen... */
  if (pool_is_free_index (vcm->tables, classify_table_index))
    return -1;

  /* Get the table */
  t = pool_elt_at_index (vcm->tables, classify_table_index);

  /* Hash the packet */
  hash = vnet_classify_hash_packet (t, vlib_buffer_get_current (b));

  /* See if there's a matching entry */
  e = vnet_classify_find_entry (t, vlib_buffer_get_current (b), hash,
				0 /* time = 0, disables hit-counter */ );
  /* Hit means trace the packet... */
  if (e)
    {
      /* Manual hit accounting */
      e->hits++;
      return 1;
    }

  /*
   * Look for a hit in a less-specific table.
   * Performance hint: for this use-case, don't go there.
   */
  while (1)
    {
      /* Most likely, we're done right now */
      if (PREDICT_TRUE (t->next_table_index == ~0))
	return 0;
      t = pool_elt_at_index (vcm->tables, t->next_table_index);

      /* Compute hash for this table */
      hash = vnet_classify_hash_packet (t, vlib_buffer_get_current (b));

      /* See if there's a matching entry */
      e = vnet_classify_find_entry (t, vlib_buffer_get_current (b), hash,
				    0 /* time = 0, disables hit-counter */ );
      if (e)
	{
	  /* Manual hit accounting */
	  e->hits++;
	  return 1;
	}
    }
  /* NOTREACHED */
}

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */