FD.io VPP  v17.04-9-g99c0734
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 {
30 
31 static u8 *
32 format_flow_classify_trace (u8 * s, va_list * args)
33 {
34  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
35  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
36  flow_classify_trace_t * t = va_arg (*args, flow_classify_trace_t *);
37 
38  s = format (s, "FLOW_CLASSIFY: sw_if_index %d next %d table %d offset %d",
39  t->sw_if_index, t->next_index, t->table_index, t->offset);
40  return s;
41 }
42 
43 #define foreach_flow_classify_error \
44 _(MISS, "Flow classify misses") \
45 _(HIT, "Flow classify hits") \
46 _(CHAIN_HIT, "Flow classify hits after chain walk") \
47 _(DROP, "Flow classify action drop")
48 
49 typedef enum {
50 #define _(sym,str) FLOW_CLASSIFY_ERROR_##sym,
52 #undef _
55 
56 static char * flow_classify_error_strings[] = {
57 #define _(sym,string) string,
59 #undef _
60 };
61 
62 static inline uword
64  vlib_node_runtime_t * node,
65  vlib_frame_t * frame,
67 {
68  u32 n_left_from, * from, * to_next;
69  flow_classify_next_index_t next_index;
72  f64 now = vlib_time_now (vm);
73  u32 hits = 0;
74  u32 misses = 0;
75  u32 chain_hits = 0;
76  u32 drop = 0;
77 
78  from = vlib_frame_vector_args (frame);
79  n_left_from = frame->n_vectors;
80 
81  /* First pass: compute hashes */
82  while (n_left_from > 2)
83  {
84  vlib_buffer_t * b0, * b1;
85  u32 bi0, bi1;
86  u8 * h0, * h1;
87  u32 sw_if_index0, sw_if_index1;
88  u32 table_index0, table_index1;
89  vnet_classify_table_t * t0, * t1;
90 
91  /* Prefetch next iteration */
92  {
93  vlib_buffer_t * p1, * p2;
94 
95  p1 = vlib_get_buffer (vm, from[1]);
96  p2 = vlib_get_buffer (vm, from[2]);
97 
98  vlib_prefetch_buffer_header (p1, STORE);
100  vlib_prefetch_buffer_header (p2, STORE);
102  }
103 
104  bi0 = from[0];
105  b0 = vlib_get_buffer (vm, bi0);
106  h0 = b0->data;
107 
108  bi1 = from[1];
109  b1 = vlib_get_buffer (vm, bi1);
110  h1 = b1->data;
111 
112  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
113  table_index0 = fcm->classify_table_index_by_sw_if_index[tid][sw_if_index0];
114 
115  sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
116  table_index1 = fcm->classify_table_index_by_sw_if_index[tid][sw_if_index1];
117 
118  t0 = pool_elt_at_index (vcm->tables, table_index0);
119 
120  t1 = pool_elt_at_index (vcm->tables, table_index1);
121 
122  vnet_buffer(b0)->l2_classify.hash =
123  vnet_classify_hash_packet (t0, (u8 *) h0);
124 
125  vnet_classify_prefetch_bucket (t0, vnet_buffer(b0)->l2_classify.hash);
126 
127  vnet_buffer(b1)->l2_classify.hash =
128  vnet_classify_hash_packet (t1, (u8 *) h1);
129 
130  vnet_classify_prefetch_bucket (t1, vnet_buffer(b1)->l2_classify.hash);
131 
132  vnet_buffer(b0)->l2_classify.table_index = table_index0;
133 
134  vnet_buffer(b1)->l2_classify.table_index = table_index1;
135 
136  from += 2;
137  n_left_from -= 2;
138  }
139 
140  while (n_left_from > 0)
141  {
142  vlib_buffer_t * b0;
143  u32 bi0;
144  u8 * h0;
145  u32 sw_if_index0;
146  u32 table_index0;
148 
149  bi0 = from[0];
150  b0 = vlib_get_buffer (vm, bi0);
151  h0 = b0->data;
152 
153  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
154  table_index0 = fcm->classify_table_index_by_sw_if_index[tid][sw_if_index0];
155 
156  t0 = pool_elt_at_index (vcm->tables, table_index0);
157  vnet_buffer(b0)->l2_classify.hash =
158  vnet_classify_hash_packet (t0, (u8 *) h0);
159 
160  vnet_buffer(b0)->l2_classify.table_index = table_index0;
161  vnet_classify_prefetch_bucket (t0, vnet_buffer(b0)->l2_classify.hash);
162 
163  from++;
164  n_left_from--;
165  }
166 
167  next_index = node->cached_next_index;
168  from = vlib_frame_vector_args (frame);
169  n_left_from = frame->n_vectors;
170 
171  while (n_left_from > 0)
172  {
173  u32 n_left_to_next;
174 
175  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
176 
177  /* Not enough load/store slots to dual loop... */
178  while (n_left_from > 0 && n_left_to_next > 0)
179  {
180  u32 bi0;
181  vlib_buffer_t * b0;
183  u32 table_index0;
185  vnet_classify_entry_t * e0;
186  u64 hash0;
187  u8 * h0;
188 
189  /* Stride 3 seems to work best */
190  if (PREDICT_TRUE (n_left_from > 3))
191  {
192  vlib_buffer_t * p1 = vlib_get_buffer(vm, from[3]);
193  vnet_classify_table_t * tp1;
194  u32 table_index1;
195  u64 phash1;
196 
197  table_index1 = vnet_buffer(p1)->l2_classify.table_index;
198 
199  if (PREDICT_TRUE (table_index1 != ~0))
200  {
201  tp1 = pool_elt_at_index (vcm->tables, table_index1);
202  phash1 = vnet_buffer(p1)->l2_classify.hash;
203  vnet_classify_prefetch_entry (tp1, phash1);
204  }
205  }
206 
207  /* Speculatively enqueue b0 to the current next frame */
208  bi0 = from[0];
209  to_next[0] = bi0;
210  from += 1;
211  to_next += 1;
212  n_left_from -= 1;
213  n_left_to_next -= 1;
214 
215  b0 = vlib_get_buffer (vm, bi0);
216  h0 = b0->data;
217  table_index0 = vnet_buffer(b0)->l2_classify.table_index;
218  e0 = 0;
219  t0 = 0;
220 
223  &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, (u8 *) 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, (u8 *) h0, hash0, now);
242  }
243  }
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 */
256  vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
257  n_left_to_next, bi0, next0);
258  }
259 
260  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
261  }
262 
264  FLOW_CLASSIFY_ERROR_MISS,
265  misses);
267  FLOW_CLASSIFY_ERROR_HIT,
268  hits);
270  FLOW_CLASSIFY_ERROR_CHAIN_HIT,
271  chain_hits);
273  FLOW_CLASSIFY_ERROR_DROP,
274  drop);
275 
276  return frame->n_vectors;
277 }
278 
279 static uword
281  vlib_node_runtime_t * node,
282  vlib_frame_t * frame)
283 {
284  return flow_classify_inline(vm, node, frame, FLOW_CLASSIFY_TABLE_IP4);
285 }
286 
288  .function = ip4_flow_classify,
289  .name = "ip4-flow-classify",
290  .vector_size = sizeof (u32),
291  .format_trace = format_flow_classify_trace,
293  .error_strings = flow_classify_error_strings,
294  .n_next_nodes = FLOW_CLASSIFY_NEXT_INDEX_N_NEXT,
295  .next_nodes = {
296  [FLOW_CLASSIFY_NEXT_INDEX_DROP] = "error-drop",
297  },
298 };
299 
301 
302 static uword
304  vlib_node_runtime_t * node,
305  vlib_frame_t * frame)
306 {
307  return flow_classify_inline(vm, node, frame, FLOW_CLASSIFY_TABLE_IP6);
308 }
309 
311  .function = ip6_flow_classify,
312  .name = "ip6-flow-classify",
313  .vector_size = sizeof (u32),
314  .format_trace = format_flow_classify_trace,
316  .error_strings = flow_classify_error_strings,
317  .n_next_nodes = FLOW_CLASSIFY_NEXT_INDEX_N_NEXT,
318  .next_nodes = {
319  [FLOW_CLASSIFY_NEXT_INDEX_DROP] = "error-drop",
320  },
321 };
322 
324 
325 
326 static clib_error_t *
328 {
330 
331  fcm->vlib_main = vm;
332  fcm->vnet_main = vnet_get_main();
334 
335  return 0;
336 }
337 
u64 vnet_classify_hash_packet(vnet_classify_table_t *t, u8 *h)
vnet_config_main_t * vnet_config_main[FLOW_CLASSIFY_N_TABLES]
Definition: flow_classify.h:42
#define CLIB_UNUSED(x)
Definition: clib.h:79
static char * flow_classify_error_strings[]
u32 current_config_index
Used by feature subgraph arcs to visit enabled feature nodes.
Definition: buffer.h:116
VLIB_NODE_FUNCTION_MULTIARCH(ip4_flow_classify_node, ip4_flow_classify)
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
#define PREDICT_TRUE(x)
Definition: clib.h:98
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:185
void vlib_put_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, u32 n_vectors_left)
Release pointer to next frame vector data.
Definition: main.c:459
flow_classify_next_index_t
Definition: flow_classify.h:29
flow_classify_table_id_t
Definition: flow_classify.h:23
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:418
vlib_node_registration_t ip6_flow_classify_node
(constructor) VLIB_REGISTER_NODE (ip6_flow_classify_node)
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
static clib_error_t * flow_classify_init(vlib_main_t *vm)
u32 * classify_table_index_by_sw_if_index[FLOW_CLASSIFY_N_TABLES]
Definition: flow_classify.h:36
vlib_main_t * vlib_main
Definition: flow_classify.h:39
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:164
unsigned long u64
Definition: types.h:89
static void vnet_classify_prefetch_bucket(vnet_classify_table_t *t, u64 hash)
#define foreach_flow_classify_error
flow_classify_main_t flow_classify_main
Definition: flow_classify.h:45
static uword ip6_flow_classify(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
static void vnet_classify_prefetch_entry(vnet_classify_table_t *t, u64 hash)
int vnet_classify_add_del_session(vnet_classify_main_t *cm, u32 table_index, u8 *match, u32 hit_next_index, u32 opaque_index, i32 advance, u8 action, u32 metadata, int is_add)
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:397
static u8 * format_flow_classify_trace(u8 *s, va_list *args)
vnet_main_t * vnet_main
Definition: flow_classify.h:40
static void * vnet_get_config_data(vnet_config_main_t *cm, u32 *config_index, u32 *next_index, u32 n_data_bytes)
Definition: config.h:122
#define PREDICT_FALSE(x)
Definition: clib.h:97
u32 node_index
Node index.
Definition: node.h:436
static uword flow_classify_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, flow_classify_table_id_t tid)
#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:216
#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:350
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1112
static uword vnet_classify_get_offset(vnet_classify_table_t *t, vnet_classify_entry_t *v)
flow_classify_error_t
u16 n_vectors
Definition: node.h:344
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:82
vlib_main_t * vm
Definition: buffer.c:276
#define VLIB_BUFFER_IS_TRACED
Definition: buffer.h:85
#define ARRAY_LEN(x)
Definition: clib.h:59
struct _vnet_classify_main vnet_classify_main_t
Definition: vnet_classify.h:69
u16 cached_next_index
Next frame index that vector arguments were last enqueued to last time this node ran.
Definition: node.h:455
unsigned int u32
Definition: types.h:88
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:259
vnet_classify_main_t vnet_classify_main
Definition: vnet_classify.c:22
u64 uword
Definition: types.h:112
static void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace_funcs.h:55
vnet_classify_main_t * vnet_classify_main
Definition: flow_classify.h:41
vlib_node_registration_t ip4_flow_classify_node
(constructor) VLIB_REGISTER_NODE (ip4_flow_classify_node)
double f64
Definition: types.h:142
unsigned char u8
Definition: types.h:56
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:253
static uword ip4_flow_classify(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
#define vnet_buffer(b)
Definition: buffer.h:294
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
u8 data[0]
Packet data.
Definition: buffer.h:152
u16 flags
Copy of main node flags.
Definition: node.h:449
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
u32 flags
buffer flags: VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:74
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:57
vnet_classify_entry_t * vnet_classify_find_entry(vnet_classify_table_t *t, u8 *h, u64 hash, f64 now)
Definition: defs.h:46