FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
flow_classify_node.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <stdint.h>
17 
18 #include <vlib/vlib.h>
19 #include <vnet/vnet.h>
20 #include <vnet/ip/ip.h>
23 
24 typedef struct
25 {
31 
32 static u8 *
33 format_flow_classify_trace (u8 * s, va_list * args)
34 {
35  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
36  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
37  flow_classify_trace_t *t = va_arg (*args, flow_classify_trace_t *);
38 
39  s = format (s, "FLOW_CLASSIFY: sw_if_index %d next %d table %d offset %d",
40  t->sw_if_index, t->next_index, t->table_index, t->offset);
41  return s;
42 }
43 
44 #define foreach_flow_classify_error \
45 _(MISS, "Flow classify misses") \
46 _(HIT, "Flow classify hits") \
47 _(CHAIN_HIT, "Flow classify hits after chain walk") \
48 _(DROP, "Flow classify action drop")
49 
50 typedef enum
51 {
52 #define _(sym,str) FLOW_CLASSIFY_ERROR_##sym,
54 #undef _
57 
58 static char *flow_classify_error_strings[] = {
59 #define _(sym,string) string,
61 #undef _
62 };
63 
64 static inline uword
68 {
69  u32 n_left_from, *from, *to_next;
73  f64 now = vlib_time_now (vm);
74  u32 hits = 0;
75  u32 misses = 0;
76  u32 chain_hits = 0;
77  u32 drop = 0;
78 
80  n_left_from = frame->n_vectors;
81 
82  /* First pass: compute hashes */
83  while (n_left_from > 2)
84  {
85  vlib_buffer_t *b0, *b1;
86  u32 bi0, bi1;
87  u8 *h0, *h1;
88  u32 sw_if_index0, sw_if_index1;
89  u32 table_index0, table_index1;
90  vnet_classify_table_t *t0, *t1;
91 
92  /* Prefetch next iteration */
93  {
94  vlib_buffer_t *p1, *p2;
95 
96  p1 = vlib_get_buffer (vm, from[1]);
97  p2 = vlib_get_buffer (vm, from[2]);
98 
99  vlib_prefetch_buffer_header (p1, STORE);
101  vlib_prefetch_buffer_header (p2, STORE);
103  }
104 
105  bi0 = from[0];
106  b0 = vlib_get_buffer (vm, bi0);
107  h0 = b0->data;
108 
109  bi1 = from[1];
110  b1 = vlib_get_buffer (vm, bi1);
111  h1 = b1->data;
112 
113  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
114  table_index0 =
115  fcm->classify_table_index_by_sw_if_index[tid][sw_if_index0];
116 
117  sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
118  table_index1 =
119  fcm->classify_table_index_by_sw_if_index[tid][sw_if_index1];
120 
121  t0 = pool_elt_at_index (vcm->tables, table_index0);
122 
123  t1 = pool_elt_at_index (vcm->tables, table_index1);
124 
125  vnet_buffer (b0)->l2_classify.hash = vnet_classify_hash_packet (t0, h0);
126 
127  vnet_classify_prefetch_bucket (t0, vnet_buffer (b0)->l2_classify.hash);
128 
129  vnet_buffer (b1)->l2_classify.hash = vnet_classify_hash_packet (t1, h1);
130 
131  vnet_classify_prefetch_bucket (t1, vnet_buffer (b1)->l2_classify.hash);
132 
133  vnet_buffer (b0)->l2_classify.table_index = table_index0;
134 
135  vnet_buffer (b1)->l2_classify.table_index = table_index1;
136 
137  from += 2;
138  n_left_from -= 2;
139  }
140 
141  while (n_left_from > 0)
142  {
143  vlib_buffer_t *b0;
144  u32 bi0;
145  u8 *h0;
146  u32 sw_if_index0;
147  u32 table_index0;
149 
150  bi0 = from[0];
151  b0 = vlib_get_buffer (vm, bi0);
152  h0 = b0->data;
153 
154  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
155  table_index0 =
156  fcm->classify_table_index_by_sw_if_index[tid][sw_if_index0];
157 
158  t0 = pool_elt_at_index (vcm->tables, table_index0);
159  vnet_buffer (b0)->l2_classify.hash = vnet_classify_hash_packet (t0, h0);
160 
161  vnet_buffer (b0)->l2_classify.table_index = table_index0;
162  vnet_classify_prefetch_bucket (t0, vnet_buffer (b0)->l2_classify.hash);
163 
164  from++;
165  n_left_from--;
166  }
167 
168  next_index = node->cached_next_index;
170  n_left_from = frame->n_vectors;
171 
172  while (n_left_from > 0)
173  {
174  u32 n_left_to_next;
175 
176  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
177 
178  /* Not enough load/store slots to dual loop... */
179  while (n_left_from > 0 && n_left_to_next > 0)
180  {
181  u32 bi0;
182  vlib_buffer_t *b0;
184  u32 table_index0;
187  u64 hash0;
188  u8 *h0;
189 
190  /* Stride 3 seems to work best */
191  if (PREDICT_TRUE (n_left_from > 3))
192  {
193  vlib_buffer_t *p1 = vlib_get_buffer (vm, from[3]);
195  u32 table_index1;
196  u64 phash1;
197 
198  table_index1 = vnet_buffer (p1)->l2_classify.table_index;
199 
200  if (PREDICT_TRUE (table_index1 != ~0))
201  {
202  tp1 = pool_elt_at_index (vcm->tables, table_index1);
203  phash1 = vnet_buffer (p1)->l2_classify.hash;
204  vnet_classify_prefetch_entry (tp1, phash1);
205  }
206  }
207 
208  /* Speculatively enqueue b0 to the current next frame */
209  bi0 = from[0];
210  to_next[0] = bi0;
211  from += 1;
212  to_next += 1;
213  n_left_from -= 1;
214  n_left_to_next -= 1;
215 
216  b0 = vlib_get_buffer (vm, bi0);
217  h0 = b0->data;
218  table_index0 = vnet_buffer (b0)->l2_classify.table_index;
219  e0 = 0;
220  t0 = 0;
221 
223  &b0->current_config_index, &next0,
224  /* # bytes of config data */ 0);
225 
226  if (PREDICT_TRUE (table_index0 != ~0))
227  {
228  hash0 = vnet_buffer (b0)->l2_classify.hash;
229  t0 = pool_elt_at_index (vcm->tables, table_index0);
230  e0 = vnet_classify_find_entry (t0, h0, hash0, now);
231  if (e0)
232  {
233  hits++;
234  }
235  else
236  {
237  misses++;
238  vnet_classify_add_del_session (vcm, table_index0,
239  h0, ~0, 0, 0, 0, 0, 1);
240  /* increment counter */
241  vnet_classify_find_entry (t0, h0, hash0, now);
242  }
243  }
244  if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
245  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
246  {
248  vlib_add_trace (vm, node, b0, sizeof (*t));
249  t->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
250  t->next_index = next0;
251  t->table_index = t0 ? t0 - vcm->tables : ~0;
252  t->offset = (t0 && e0) ? vnet_classify_get_offset (t0, e0) : ~0;
253  }
254 
255  /* Verify speculative enqueue, maybe switch current next frame */
257  n_left_to_next, bi0, next0);
258  }
259 
260  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
261  }
262 
263  vlib_node_increment_counter (vm, node->node_index,
264  FLOW_CLASSIFY_ERROR_MISS, misses);
265  vlib_node_increment_counter (vm, node->node_index,
266  FLOW_CLASSIFY_ERROR_HIT, hits);
267  vlib_node_increment_counter (vm, node->node_index,
268  FLOW_CLASSIFY_ERROR_CHAIN_HIT, chain_hits);
269  vlib_node_increment_counter (vm, node->node_index,
270  FLOW_CLASSIFY_ERROR_DROP, drop);
271 
272  return frame->n_vectors;
273 }
274 
278 {
280 }
281 
282 /* *INDENT-OFF* */
284  .name = "ip4-flow-classify",
285  .vector_size = sizeof (u32),
286  .format_trace = format_flow_classify_trace,
288  .error_strings = flow_classify_error_strings,
289  .n_next_nodes = FLOW_CLASSIFY_NEXT_INDEX_N_NEXT,
290  .next_nodes = {
291  [FLOW_CLASSIFY_NEXT_INDEX_DROP] = "error-drop",
292  },
293 };
294 /* *INDENT-ON* */
295 
299 {
301 }
302 
303 /* *INDENT-OFF* */
305  .name = "ip6-flow-classify",
306  .vector_size = sizeof (u32),
307  .format_trace = format_flow_classify_trace,
309  .error_strings = flow_classify_error_strings,
310  .n_next_nodes = FLOW_CLASSIFY_NEXT_INDEX_N_NEXT,
311  .next_nodes = {
312  [FLOW_CLASSIFY_NEXT_INDEX_DROP] = "error-drop",
313  },
314 };
315 
316 /* *INDENT-ON* */
317 
318 
319 static clib_error_t *
321 {
323 
324  fcm->vlib_main = vm;
325  fcm->vnet_main = vnet_get_main ();
327 
328  return 0;
329 }
330 
332 
333 /*
334  * fd.io coding-style-patch-verification: ON
335  *
336  * Local Variables:
337  * eval: (c-set-style "gnu")
338  * End:
339  */
vlib.h
flow_classify_trace_t::offset
u32 offset
Definition: flow_classify_node.c:29
flow_classify_main_t::vnet_main
vnet_main_t * vnet_main
Definition: flow_classify.h:43
flow_classify_error_t
flow_classify_error_t
Definition: flow_classify_node.c:50
FLOW_CLASSIFY_TABLE_IP4
@ FLOW_CLASSIFY_TABLE_IP4
Definition: flow_classify.h:25
FLOW_CLASSIFY_NEXT_INDEX_N_NEXT
@ FLOW_CLASSIFY_NEXT_INDEX_N_NEXT
Definition: flow_classify.h:33
vnet_classify_entry_t
struct _vnet_classify_entry vnet_classify_entry_t
vnet_classify_add_del_session
int vnet_classify_add_del_session(vnet_classify_main_t *cm, u32 table_index, const u8 *match, u32 hit_next_index, u32 opaque_index, i32 advance, u8 action, u16 metadata, int is_add)
Definition: vnet_classify.c:2745
vlib_prefetch_buffer_header
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:231
frame
vlib_main_t vlib_node_runtime_t vlib_frame_t * frame
Definition: nat44_ei.c:3048
next_index
nat44_ei_hairpin_src_next_t next_index
Definition: nat44_ei_hairpinning.c:412
vlib_get_buffer
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:111
flow_classify_next_index_t
flow_classify_next_index_t
Definition: flow_classify.h:30
flow_classify_trace_t::sw_if_index
u32 sw_if_index
Definition: flow_classify_node.c:26
pool_elt_at_index
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:553
flow_classify_error_strings
static char * flow_classify_error_strings[]
Definition: flow_classify_node.c:58
node
vlib_main_t vlib_node_runtime_t * node
Definition: nat44_ei.c:3047
vnet_classify_main
vnet_classify_main_t vnet_classify_main
Definition: vnet_classify.c:32
flow_classify_main_t::classify_table_index_by_sw_if_index
u32 * classify_table_index_by_sw_if_index[FLOW_CLASSIFY_N_TABLES]
Definition: flow_classify.h:39
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
VLIB_RX
@ VLIB_RX
Definition: defs.h:46
flow_classify_table_id_t
flow_classify_table_id_t
Definition: flow_classify.h:23
vlib_frame_t
Definition: node.h:372
flow_classify_main_t::vnet_classify_main
vnet_classify_main_t * vnet_classify_main
Definition: flow_classify.h:44
vnet_classify_find_entry
vnet_classify_entry_t * vnet_classify_find_entry(vnet_classify_table_t *t, u8 *h, u64 hash, f64 now)
Definition: vnet_classify.c:657
CLIB_PREFETCH
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:80
flow_classify.h
VLIB_NODE_FN
#define VLIB_NODE_FN(node)
Definition: node.h:202
ip6_flow_classify_node
vlib_node_registration_t ip6_flow_classify_node
(constructor) VLIB_REGISTER_NODE (ip6_flow_classify_node)
Definition: flow_classify_node.c:304
CLIB_UNUSED
#define CLIB_UNUSED(x)
Definition: clib.h:90
vnet_buffer
#define vnet_buffer(b)
Definition: buffer.h:437
vnet_get_main
vnet_main_t * vnet_get_main(void)
Definition: pnat_test_stubs.h:56
flow_classify_main_t::vlib_main
vlib_main_t * vlib_main
Definition: flow_classify.h:42
VLIB_NODE_FLAG_TRACE
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:291
PREDICT_FALSE
#define PREDICT_FALSE(x)
Definition: clib.h:124
foreach_flow_classify_error
#define foreach_flow_classify_error
Definition: flow_classify_node.c:44
ARRAY_LEN
#define ARRAY_LEN(x)
Definition: clib.h:70
vlib_frame_vector_args
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:301
flow_classify_inline
static uword flow_classify_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, flow_classify_table_id_t tid)
Definition: flow_classify_node.c:65
flow_classify_trace_t::table_index
u32 table_index
Definition: flow_classify_node.c:28
vnet_classify_prefetch_entry
static void vnet_classify_prefetch_entry(vnet_classify_table_t *t, u64 hash)
Definition: vnet_classify.h:371
uword
u64 uword
Definition: types.h:112
vnet_classify_prefetch_bucket
static void vnet_classify_prefetch_bucket(vnet_classify_table_t *t, u64 hash)
Definition: vnet_classify.h:323
vlib_node_increment_counter
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1244
f64
double f64
Definition: types.h:142
flow_classify_trace_t
Definition: flow_classify_node.c:24
vnet_classify_table_t
Definition: vnet_classify.h:147
CLIB_CACHE_LINE_BYTES
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
vlib_node_registration_t
struct _vlib_node_registration vlib_node_registration_t
FLOW_CLASSIFY_N_ERROR
@ FLOW_CLASSIFY_N_ERROR
Definition: flow_classify_node.c:55
flow_classify_main_t
Definition: flow_classify.h:36
vlib_buffer_t::current_config_index
u32 current_config_index
Used by feature subgraph arcs to visit enabled feature nodes.
Definition: buffer.h:156
vlib_validate_buffer_enqueue_x1
#define vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, n_left_to_next, bi0, next0)
Finish enqueueing one buffer forward in the graph.
Definition: buffer_node.h:224
u64
unsigned long u64
Definition: types.h:89
format
description fragment has unexpected format
Definition: map.api:433
ip4_flow_classify_node
vlib_node_registration_t ip4_flow_classify_node
(constructor) VLIB_REGISTER_NODE (ip4_flow_classify_node)
Definition: flow_classify_node.c:283
vlib_put_next_frame
vlib_put_next_frame(vm, node, next_index, 0)
ip.h
u32
unsigned int u32
Definition: types.h:88
VLIB_INIT_FUNCTION
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:172
vnet_classify_main_t
struct _vnet_classify_main vnet_classify_main_t
Definition: vnet_classify.h:61
vnet_classify_hash_packet
u64 vnet_classify_hash_packet(vnet_classify_table_t *t, u8 *h)
flow_classify_trace_t::next_index
u32 next_index
Definition: flow_classify_node.c:27
vnet_classify.h
vnet_classify_get_offset
static uword vnet_classify_get_offset(vnet_classify_table_t *t, vnet_classify_entry_t *v)
Definition: vnet_classify.h:344
now
f64 now
Definition: nat44_ei_out2in.c:710
FLOW_CLASSIFY_NEXT_INDEX_DROP
@ FLOW_CLASSIFY_NEXT_INDEX_DROP
Definition: flow_classify.h:32
vlib_main_t
Definition: main.h:102
flow_classify_init
static clib_error_t * flow_classify_init(vlib_main_t *vm)
Definition: flow_classify_node.c:320
vlib_node_t
Definition: node.h:247
vlib_add_trace
void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace.c:628
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
vlib_init_function_t
clib_error_t *() vlib_init_function_t(struct vlib_main_t *vm)
Definition: init.h:51
vnet_get_config_data
static void * vnet_get_config_data(vnet_config_main_t *cm, u32 *config_index, u32 *next_index, u32 n_data_bytes)
Definition: config.h:123
vlib_buffer_t::data
u8 data[]
Packet data.
Definition: buffer.h:204
FLOW_CLASSIFY_TABLE_IP6
@ FLOW_CLASSIFY_TABLE_IP6
Definition: flow_classify.h:26
flow_classify_main_t::vnet_config_main
vnet_config_main_t * vnet_config_main[FLOW_CLASSIFY_N_TABLES]
Definition: flow_classify.h:45
vlib_time_now
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:325
vnet.h
vlib_node_runtime_t
Definition: node.h:454
from
from
Definition: nat44_ei_hairpinning.c:415
PREDICT_TRUE
#define PREDICT_TRUE(x)
Definition: clib.h:125
vlib_get_next_frame
#define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)
Get pointer to next frame vector data by (vlib_node_runtime_t, next_index).
Definition: node_funcs.h:395
format_flow_classify_trace
static u8 * format_flow_classify_trace(u8 *s, va_list *args)
Definition: flow_classify_node.c:33
n_left_from
n_left_from
Definition: nat44_ei_hairpinning.c:416
flow_classify_main
flow_classify_main_t flow_classify_main
Definition: flow_classify.c:19
vlib_buffer_t::flags
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index,...
Definition: buffer.h:133
vlib_buffer_t
VLIB buffer representation.
Definition: buffer.h:111
VLIB_REGISTER_NODE
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169