FD.io VPP  v21.06-3-gbb25fbf28
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
62  vlib_frame_t * frame, int is_ip4)
63 {
64  u32 n_left_from, *from, *to_next;
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 
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);
113 
114  bi1 = from[1];
115  b1 = vlib_get_buffer (vm, bi1);
117 
118  cd_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
119  cd0 = classify_dpo_get (cd_index0);
120  table_index0 = cd0->cd_table_index;
121 
122  cd_index1 = vnet_buffer (b1)->ip.adj_index[VLIB_TX];
123  cd1 = classify_dpo_get (cd_index1);
124  table_index1 = cd1->cd_table_index;
125 
126  t0 = pool_elt_at_index (vcm->tables, table_index0);
127 
128  t1 = pool_elt_at_index (vcm->tables, table_index1);
129 
130  vnet_buffer (b0)->l2_classify.hash = vnet_classify_hash_packet (t0, h0);
131 
132  vnet_classify_prefetch_bucket (t0, vnet_buffer (b0)->l2_classify.hash);
133 
134  vnet_buffer (b1)->l2_classify.hash = vnet_classify_hash_packet (t1, h1);
135 
136  vnet_classify_prefetch_bucket (t1, vnet_buffer (b1)->l2_classify.hash);
137 
138  vnet_buffer (b0)->l2_classify.table_index = table_index0;
139 
140  vnet_buffer (b1)->l2_classify.table_index = table_index1;
141 
142  from += 2;
143  n_left_from -= 2;
144  }
145 
146  while (n_left_from > 0)
147  {
148  vlib_buffer_t *b0;
149  u32 bi0;
150  u8 *h0;
151  u32 cd_index0;
152  classify_dpo_t *cd0;
153  u32 table_index0;
155 
156  bi0 = from[0];
157  b0 = vlib_get_buffer (vm, bi0);
159 
160  cd_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
161  cd0 = classify_dpo_get (cd_index0);
162  table_index0 = cd0->cd_table_index;
163 
164  t0 = pool_elt_at_index (vcm->tables, table_index0);
165  vnet_buffer (b0)->l2_classify.hash = vnet_classify_hash_packet (t0, h0);
166 
167  vnet_buffer (b0)->l2_classify.table_index = table_index0;
168  vnet_classify_prefetch_bucket (t0, vnet_buffer (b0)->l2_classify.hash);
169 
170  from++;
171  n_left_from--;
172  }
173 
174  next_index = node->cached_next_index;
176  n_left_from = frame->n_vectors;
177 
178  while (n_left_from > 0)
179  {
180  u32 n_left_to_next;
181 
182  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
183 
184  /* Not enough load/store slots to dual loop... */
185  while (n_left_from > 0 && n_left_to_next > 0)
186  {
187  u32 bi0;
188  vlib_buffer_t *b0;
189  u32 next0 = IP_LOOKUP_NEXT_DROP;
190  u32 table_index0;
193  u64 hash0;
194  u8 *h0;
195 
196  /* Stride 3 seems to work best */
197  if (PREDICT_TRUE (n_left_from > 3))
198  {
199  vlib_buffer_t *p1 = vlib_get_buffer (vm, from[3]);
201  u32 table_index1;
202  u64 phash1;
203 
204  table_index1 = vnet_buffer (p1)->l2_classify.table_index;
205 
206  if (PREDICT_TRUE (table_index1 != ~0))
207  {
208  tp1 = pool_elt_at_index (vcm->tables, table_index1);
209  phash1 = vnet_buffer (p1)->l2_classify.hash;
210  vnet_classify_prefetch_entry (tp1, phash1);
211  }
212  }
213 
214  /* speculatively enqueue b0 to the current next frame */
215  bi0 = from[0];
216  to_next[0] = bi0;
217  from += 1;
218  to_next += 1;
219  n_left_from -= 1;
220  n_left_to_next -= 1;
221 
222  b0 = vlib_get_buffer (vm, bi0);
223  h0 = b0->data;
224  table_index0 = vnet_buffer (b0)->l2_classify.table_index;
225  e0 = 0;
226  t0 = 0;
227  vnet_buffer (b0)->l2_classify.opaque_index = ~0;
228 
229  if (PREDICT_TRUE (table_index0 != ~0))
230  {
231  hash0 = vnet_buffer (b0)->l2_classify.hash;
232  t0 = pool_elt_at_index (vcm->tables, table_index0);
233 
234  e0 = vnet_classify_find_entry (t0, h0, hash0, now);
235  if (e0)
236  {
237  vnet_buffer (b0)->l2_classify.opaque_index
238  = e0->opaque_index;
239  vlib_buffer_advance (b0, e0->advance);
240  next0 = (e0->next_index < node->n_next_nodes) ?
241  e0->next_index : next0;
242  hits++;
243  }
244  else
245  {
246  while (1)
247  {
248  if (t0->next_table_index != ~0)
249  t0 = pool_elt_at_index (vcm->tables,
250  t0->next_table_index);
251  else
252  {
253  next0 = (t0->miss_next_index < n_next) ?
254  t0->miss_next_index : next0;
255  misses++;
256  break;
257  }
258 
259  hash0 = vnet_classify_hash_packet (t0, h0);
260  e0 = vnet_classify_find_entry (t0, h0, hash0, now);
261  if (e0)
262  {
263  vnet_buffer (b0)->l2_classify.opaque_index
264  = e0->opaque_index;
265  vlib_buffer_advance (b0, e0->advance);
266  next0 = (e0->next_index < node->n_next_nodes) ?
267  e0->next_index : next0;
268  hits++;
269  chain_hits++;
270  break;
271  }
272  }
273  }
274  }
275 
276  if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
277  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
278  {
280  vlib_add_trace (vm, node, b0, sizeof (*t));
281  t->next_index = next0;
282  t->table_index = t0 ? t0 - vcm->tables : ~0;
283  t->entry_index = e0 ? e0->opaque_index : ~0;
284  }
285 
286  /* verify speculative enqueue, maybe switch current next frame */
288  to_next, n_left_to_next,
289  bi0, next0);
290  }
291 
292  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
293  }
294 
295  vlib_node_increment_counter (vm, node->node_index,
296  IP_CLASSIFY_ERROR_MISS, misses);
297  vlib_node_increment_counter (vm, node->node_index,
298  IP_CLASSIFY_ERROR_HIT, hits);
299  vlib_node_increment_counter (vm, node->node_index,
300  IP_CLASSIFY_ERROR_CHAIN_HIT, chain_hits);
301  return frame->n_vectors;
302 }
303 
307 {
308  return ip_classify_inline (vm, node, frame, 1 /* is_ip4 */ );
309 }
310 
311 
312 /* *INDENT-OFF* */
314  .name = "ip4-classify",
315  .vector_size = sizeof (u32),
316  .sibling_of = "ip4-lookup",
317  .format_trace = format_ip_classify_trace,
319  .error_strings = ip_classify_error_strings,
320 
321  .n_next_nodes = 0,
322 };
323 /* *INDENT-ON* */
324 
328 {
329  return ip_classify_inline (vm, node, frame, 0 /* is_ip4 */ );
330 }
331 
332 
333 /* *INDENT-OFF* */
335  .name = "ip6-classify",
336  .vector_size = sizeof (u32),
337  .sibling_of = "ip6-lookup",
338  .format_trace = format_ip_classify_trace,
340  .error_strings = ip_classify_error_strings,
341 
342  .n_next_nodes = 0,
343 };
344 /* *INDENT-ON* */
345 
346 #ifndef CLIB_MARCH_VARIANT
347 static clib_error_t *
349 {
350  return 0;
351 }
352 
354 #endif /* CLIB_MARCH_VARIANT */
355 
356 /*
357  * fd.io coding-style-patch-verification: ON
358  *
359  * Local Variables:
360  * eval: (c-set-style "gnu")
361  * End:
362  */
vnet_classify_entry_t
struct _vnet_classify_entry vnet_classify_entry_t
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
ip6_classify_node
vlib_node_registration_t ip6_classify_node
(constructor) VLIB_REGISTER_NODE (ip6_classify_node)
Definition: ip_classify.c:334
vnet_classify_table_t::miss_next_index
u32 miss_next_index
Definition: vnet_classify.h:176
next_index
nat44_ei_hairpin_src_next_t next_index
Definition: nat44_ei_hairpinning.c:412
ip_classify_trace_t::entry_index
u32 entry_index
Definition: ip_classify.c:24
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
pool_elt_at_index
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:553
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
ip_classify_trace_t::table_index
u32 table_index
Definition: ip_classify.c:23
foreach_ip_classify_error
#define foreach_ip_classify_error
Definition: ip_classify.c:40
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
classify_dpo_t::cd_table_index
u32 cd_table_index
Definition: classify_dpo.h:35
ip_classify_trace_t::next_index
u32 next_index
Definition: ip_classify.c:22
vlib_frame_t
Definition: node.h:372
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
ethernet.h
ip_classify_error_t
ip_classify_error_t
Definition: ip_classify.c:45
CLIB_PREFETCH
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:80
ip4_classify_node
vlib_node_registration_t ip4_classify_node
(constructor) VLIB_REGISTER_NODE (ip4_classify_node)
Definition: ip_classify.c:313
vlib_buffer_advance
static void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:276
IP_LOOKUP_NEXT_DROP
@ IP_LOOKUP_NEXT_DROP
Adjacency to drop this packet.
Definition: adj.h:53
VLIB_NODE_FN
#define VLIB_NODE_FN(node)
Definition: node.h:202
classify_dpo_t
A representation of an MPLS label for imposition in the data-path.
Definition: classify_dpo.h:26
CLIB_UNUSED
#define CLIB_UNUSED(x)
Definition: clib.h:90
vnet_buffer
#define vnet_buffer(b)
Definition: buffer.h:437
IP4_LOOKUP_N_NEXT
@ IP4_LOOKUP_N_NEXT
Definition: adj.h:96
VLIB_NODE_FLAG_TRACE
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:291
PREDICT_FALSE
#define PREDICT_FALSE(x)
Definition: clib.h:124
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
ethernet_buffer_header_size
#define ethernet_buffer_header_size(b)
Determine the size of the Ethernet headers of the current frame in the buffer.
Definition: ethernet.h:463
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
ip_classify_error_strings
static char * ip_classify_error_strings[]
Definition: ip_classify.c:53
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
vnet_classify_table_t
Definition: vnet_classify.h:147
ip_classify_trace_t
Definition: ip_classify.c:20
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
IP_CLASSIFY_N_ERROR
@ IP_CLASSIFY_N_ERROR
Definition: ip_classify.c:50
ip_lookup_next_t
ip_lookup_next_t
An adjacency is a representation of an attached L3 peer.
Definition: adj.h:50
vnet_classify_table_t::next_table_index
u32 next_table_index
Definition: vnet_classify.h:170
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
format_ip_classify_trace
static u8 * format_ip_classify_trace(u8 *s, va_list *args)
Definition: ip_classify.c:29
vlib_put_next_frame
vlib_put_next_frame(vm, node, next_index, 0)
classify_dpo_get
static classify_dpo_t * classify_dpo_get(index_t index)
Definition: classify_dpo.h:54
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)
IP6_LOOKUP_N_NEXT
@ IP6_LOOKUP_N_NEXT
Definition: adj.h:105
vnet_classify.h
now
f64 now
Definition: nat44_ei_out2in.c:710
vlib_main_t
Definition: main.h:102
vlib_node_t
Definition: node.h:247
ip_classify_init
static clib_error_t * ip_classify_init(vlib_main_t *vm)
Definition: ip_classify.c:348
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_buffer_get_current
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:257
vlib_init_function_t
clib_error_t *() vlib_init_function_t(struct vlib_main_t *vm)
Definition: init.h:51
vlib_buffer_t::data
u8 data[]
Packet data.
Definition: buffer.h:204
vlib_time_now
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:325
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
ip_classify_inline
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
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
VLIB_TX
@ VLIB_TX
Definition: defs.h:47
n_left_from
n_left_from
Definition: nat44_ei_hairpinning.c:416
classify_dpo.h
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