FD.io VPP  v19.04-6-g6f05f72
Vector Packet Processing
ip_classify.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 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 #include <vnet/ip/ip.h>
16 #include <vnet/ethernet/ethernet.h> /* for ethernet_header_t */
18 #include <vnet/dpo/classify_dpo.h>
19 
20 typedef struct
21 {
26 
27 /* packet trace format function */
28 static u8 *
29 format_ip_classify_trace (u8 * s, va_list * args)
30 {
31  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
32  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
33  ip_classify_trace_t *t = va_arg (*args, ip_classify_trace_t *);
34 
35  s = format (s, "IP_CLASSIFY: next_index %d, table %d, entry %d",
37  return s;
38 }
39 
40 #define foreach_ip_classify_error \
41 _(MISS, "Classify misses") \
42 _(HIT, "Classify hits") \
43 _(CHAIN_HIT, "Classify hits after chain walk")
44 
45 typedef enum
46 {
47 #define _(sym,str) IP_CLASSIFY_ERROR_##sym,
49 #undef _
52 
53 static char *ip_classify_error_strings[] = {
54 #define _(sym,string) string,
56 #undef _
57 };
58 
59 static inline uword
61  vlib_node_runtime_t * node,
62  vlib_frame_t * frame, int is_ip4)
63 {
64  u32 n_left_from, *from, *to_next;
65  ip_lookup_next_t next_index;
67  f64 now = vlib_time_now (vm);
68  u32 hits = 0;
69  u32 misses = 0;
70  u32 chain_hits = 0;
71  u32 n_next;
72 
73  if (is_ip4)
74  {
75  n_next = IP4_LOOKUP_N_NEXT;
76  }
77  else
78  {
79  n_next = IP6_LOOKUP_N_NEXT;
80  }
81 
82  from = vlib_frame_vector_args (frame);
83  n_left_from = frame->n_vectors;
84 
85  /* First pass: compute hashes */
86 
87  while (n_left_from > 2)
88  {
89  vlib_buffer_t *b0, *b1;
90  u32 bi0, bi1;
91  u8 *h0, *h1;
92  u32 cd_index0, cd_index1;
93  classify_dpo_t *cd0, *cd1;
94  u32 table_index0, table_index1;
95  vnet_classify_table_t *t0, *t1;
96 
97  /* prefetch next iteration */
98  {
99  vlib_buffer_t *p1, *p2;
100 
101  p1 = vlib_get_buffer (vm, from[1]);
102  p2 = vlib_get_buffer (vm, from[2]);
103 
104  vlib_prefetch_buffer_header (p1, STORE);
106  vlib_prefetch_buffer_header (p2, STORE);
108  }
109 
110  bi0 = from[0];
111  b0 = vlib_get_buffer (vm, bi0);
112  h0 = (void *) vlib_buffer_get_current (b0) -
114 
115  bi1 = from[1];
116  b1 = vlib_get_buffer (vm, bi1);
117  h1 = (void *) vlib_buffer_get_current (b1) -
119 
120  cd_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
121  cd0 = classify_dpo_get (cd_index0);
122  table_index0 = cd0->cd_table_index;
123 
124  cd_index1 = vnet_buffer (b1)->ip.adj_index[VLIB_TX];
125  cd1 = classify_dpo_get (cd_index1);
126  table_index1 = cd1->cd_table_index;
127 
128  t0 = pool_elt_at_index (vcm->tables, table_index0);
129 
130  t1 = pool_elt_at_index (vcm->tables, table_index1);
131 
132  vnet_buffer (b0)->l2_classify.hash =
133  vnet_classify_hash_packet (t0, (u8 *) h0);
134 
135  vnet_classify_prefetch_bucket (t0, vnet_buffer (b0)->l2_classify.hash);
136 
137  vnet_buffer (b1)->l2_classify.hash =
138  vnet_classify_hash_packet (t1, (u8 *) h1);
139 
140  vnet_classify_prefetch_bucket (t1, vnet_buffer (b1)->l2_classify.hash);
141 
142  vnet_buffer (b0)->l2_classify.table_index = table_index0;
143 
144  vnet_buffer (b1)->l2_classify.table_index = table_index1;
145 
146  from += 2;
147  n_left_from -= 2;
148  }
149 
150  while (n_left_from > 0)
151  {
152  vlib_buffer_t *b0;
153  u32 bi0;
154  u8 *h0;
155  u32 cd_index0;
156  classify_dpo_t *cd0;
157  u32 table_index0;
159 
160  bi0 = from[0];
161  b0 = vlib_get_buffer (vm, bi0);
162  h0 = (void *) vlib_buffer_get_current (b0) -
164 
165  cd_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
166  cd0 = classify_dpo_get (cd_index0);
167  table_index0 = cd0->cd_table_index;
168 
169  t0 = pool_elt_at_index (vcm->tables, table_index0);
170  vnet_buffer (b0)->l2_classify.hash =
171  vnet_classify_hash_packet (t0, (u8 *) h0);
172 
173  vnet_buffer (b0)->l2_classify.table_index = table_index0;
174  vnet_classify_prefetch_bucket (t0, vnet_buffer (b0)->l2_classify.hash);
175 
176  from++;
177  n_left_from--;
178  }
179 
180  next_index = node->cached_next_index;
181  from = vlib_frame_vector_args (frame);
182  n_left_from = frame->n_vectors;
183 
184  while (n_left_from > 0)
185  {
186  u32 n_left_to_next;
187 
188  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
189 
190  /* Not enough load/store slots to dual loop... */
191  while (n_left_from > 0 && n_left_to_next > 0)
192  {
193  u32 bi0;
194  vlib_buffer_t *b0;
195  u32 next0 = IP_LOOKUP_NEXT_DROP;
196  u32 table_index0;
198  vnet_classify_entry_t *e0;
199  u64 hash0;
200  u8 *h0;
201 
202  /* Stride 3 seems to work best */
203  if (PREDICT_TRUE (n_left_from > 3))
204  {
205  vlib_buffer_t *p1 = vlib_get_buffer (vm, from[3]);
207  u32 table_index1;
208  u64 phash1;
209 
210  table_index1 = vnet_buffer (p1)->l2_classify.table_index;
211 
212  if (PREDICT_TRUE (table_index1 != ~0))
213  {
214  tp1 = pool_elt_at_index (vcm->tables, table_index1);
215  phash1 = vnet_buffer (p1)->l2_classify.hash;
216  vnet_classify_prefetch_entry (tp1, phash1);
217  }
218  }
219 
220  /* speculatively enqueue b0 to the current next frame */
221  bi0 = from[0];
222  to_next[0] = bi0;
223  from += 1;
224  to_next += 1;
225  n_left_from -= 1;
226  n_left_to_next -= 1;
227 
228  b0 = vlib_get_buffer (vm, bi0);
229  h0 = b0->data;
230  table_index0 = vnet_buffer (b0)->l2_classify.table_index;
231  e0 = 0;
232  t0 = 0;
233  vnet_buffer (b0)->l2_classify.opaque_index = ~0;
234 
235  if (PREDICT_TRUE (table_index0 != ~0))
236  {
237  hash0 = vnet_buffer (b0)->l2_classify.hash;
238  t0 = pool_elt_at_index (vcm->tables, table_index0);
239 
240  e0 = vnet_classify_find_entry (t0, (u8 *) h0, hash0, now);
241  if (e0)
242  {
243  vnet_buffer (b0)->l2_classify.opaque_index
244  = e0->opaque_index;
245  vlib_buffer_advance (b0, e0->advance);
246  next0 = (e0->next_index < node->n_next_nodes) ?
247  e0->next_index : next0;
248  hits++;
249  }
250  else
251  {
252  while (1)
253  {
254  if (t0->next_table_index != ~0)
255  t0 = pool_elt_at_index (vcm->tables,
256  t0->next_table_index);
257  else
258  {
259  next0 = (t0->miss_next_index < n_next) ?
260  t0->miss_next_index : next0;
261  misses++;
262  break;
263  }
264 
265  hash0 = vnet_classify_hash_packet (t0, (u8 *) h0);
267  (t0, (u8 *) h0, hash0, now);
268  if (e0)
269  {
270  vnet_buffer (b0)->l2_classify.opaque_index
271  = e0->opaque_index;
272  vlib_buffer_advance (b0, e0->advance);
273  next0 = (e0->next_index < node->n_next_nodes) ?
274  e0->next_index : next0;
275  hits++;
276  chain_hits++;
277  break;
278  }
279  }
280  }
281  }
282 
284  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
285  {
287  vlib_add_trace (vm, node, b0, sizeof (*t));
288  t->next_index = next0;
289  t->table_index = t0 ? t0 - vcm->tables : ~0;
290  t->entry_index = e0 ? e0 - t0->entries : ~0;
291  }
292 
293  /* verify speculative enqueue, maybe switch current next frame */
294  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
295  to_next, n_left_to_next,
296  bi0, next0);
297  }
298 
299  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
300  }
301 
303  IP_CLASSIFY_ERROR_MISS, misses);
305  IP_CLASSIFY_ERROR_HIT, hits);
307  IP_CLASSIFY_ERROR_CHAIN_HIT, chain_hits);
308  return frame->n_vectors;
309 }
310 
312  vlib_node_runtime_t * node,
313  vlib_frame_t * frame)
314 {
315  return ip_classify_inline (vm, node, frame, 1 /* is_ip4 */ );
316 }
317 
318 
319 /* *INDENT-OFF* */
321  .name = "ip4-classify",
322  .vector_size = sizeof (u32),
323  .sibling_of = "ip4-lookup",
324  .format_trace = format_ip_classify_trace,
326  .error_strings = ip_classify_error_strings,
327 
328  .n_next_nodes = 0,
329 };
330 /* *INDENT-ON* */
331 
333  vlib_node_runtime_t * node,
334  vlib_frame_t * frame)
335 {
336  return ip_classify_inline (vm, node, frame, 0 /* is_ip4 */ );
337 }
338 
339 
340 /* *INDENT-OFF* */
342  .name = "ip6-classify",
343  .vector_size = sizeof (u32),
344  .sibling_of = "ip6-lookup",
345  .format_trace = format_ip_classify_trace,
347  .error_strings = ip_classify_error_strings,
348 
349  .n_next_nodes = 0,
350 };
351 /* *INDENT-ON* */
352 
353 #ifndef CLIB_MARCH_VARIANT
354 static clib_error_t *
356 {
357  return 0;
358 }
359 
361 #endif /* CLIB_MARCH_VARIANT */
362 
363 /*
364  * fd.io coding-style-patch-verification: ON
365  *
366  * Local Variables:
367  * eval: (c-set-style "gnu")
368  * End:
369  */
u64 vnet_classify_hash_packet(vnet_classify_table_t *t, u8 *h)
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:124
#define foreach_ip_classify_error
Definition: ip_classify.c:40
static clib_error_t * ip_classify_init(vlib_main_t *vm)
Definition: ip_classify.c:355
#define CLIB_UNUSED(x)
Definition: clib.h:82
#define PREDICT_TRUE(x)
Definition: clib.h:112
unsigned long u64
Definition: types.h:89
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:255
#define ethernet_buffer_header_size(b)
Determine the size of the Ethernet headers of the current frame in the buffer.
Definition: ethernet.h:420
u8 data[0]
Packet data.
Definition: buffer.h:181
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
#define VLIB_NODE_FN(node)
Definition: node.h:201
unsigned char u8
Definition: types.h:56
ip_lookup_next_t
An adjacency is a representation of an attached L3 peer.
Definition: adj.h:50
vlib_node_registration_t ip4_classify_node
(constructor) VLIB_REGISTER_NODE (ip4_classify_node)
Definition: ip_classify.c:320
double f64
Definition: types.h:142
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:163
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:203
A representation of an MPLS label for imposition in the data-path.
Definition: classify_dpo.h:26
unsigned int u32
Definition: types.h:88
static void vnet_classify_prefetch_bucket(vnet_classify_table_t *t, u64 hash)
static void vnet_classify_prefetch_entry(vnet_classify_table_t *t, u64 hash)
static uword ip_classify_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, int is_ip4)
Definition: ip_classify.c:60
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:514
vnet_classify_entry_t * entries
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:229
#define PREDICT_FALSE(x)
Definition: clib.h:111
u32 node_index
Node index.
Definition: node.h:495
#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:218
#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:368
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1180
Adjacency to drop this packet.
Definition: adj.h:53
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
u16 n_vectors
Definition: node.h:395
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:80
vlib_main_t * vm
Definition: buffer.c:312
#define ARRAY_LEN(x)
Definition: clib.h:62
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:465
struct _vnet_classify_main vnet_classify_main_t
Definition: vnet_classify.h:67
u16 cached_next_index
Next frame index that vector arguments were last enqueued to last time this node ran.
Definition: node.h:514
static char * ip_classify_error_strings[]
Definition: ip_classify.c:53
static void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:248
vnet_classify_main_t vnet_classify_main
Definition: vnet_classify.c:22
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:57
Definition: defs.h:47
ip_classify_error_t
Definition: ip_classify.c:45
VLIB buffer representation.
Definition: buffer.h:102
u64 uword
Definition: types.h:112
static u8 * format_ip_classify_trace(u8 *s, va_list *args)
Definition: ip_classify.c:29
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:274
static classify_dpo_t * classify_dpo_get(index_t index)
Definition: classify_dpo.h:54
#define vnet_buffer(b)
Definition: buffer.h:369
u16 flags
Copy of main node flags.
Definition: node.h:508
vlib_node_registration_t ip6_classify_node
(constructor) VLIB_REGISTER_NODE (ip6_classify_node)
Definition: ip_classify.c:341
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:301
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:85
vnet_classify_entry_t * vnet_classify_find_entry(vnet_classify_table_t *t, u8 *h, u64 hash, f64 now)