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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include <vnet/ip/ip.h>
16 #include <vnet/ethernet/ethernet.h> /* for ethernet_header_t */
17 #include <vnet/classify/vnet_classify.h>
23 } ip_classify_trace_t;
25 /* packet trace format function */
26 static u8 * format_ip_classify_trace (u8 * s, va_list * args)
28 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
29 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
30 ip_classify_trace_t * t = va_arg (*args, ip_classify_trace_t *);
32 s = format (s, "IP_CLASSIFY: next_index %d, table %d, entry %d",
33 t->next_index, t->table_index, t->entry_index);
37 vlib_node_registration_t ip4_classify_node;
38 vlib_node_registration_t ip6_classify_node;
40 #define foreach_ip_classify_error \
41 _(MISS, "Classify misses") \
42 _(HIT, "Classify hits") \
43 _(CHAIN_HIT, "Classify hits after chain walk")
46 #define _(sym,str) IP_CLASSIFY_ERROR_##sym,
47 foreach_ip_classify_error
50 } ip_classify_error_t;
52 static char * ip_classify_error_strings[] = {
53 #define _(sym,string) string,
54 foreach_ip_classify_error
59 ip_classify_inline (vlib_main_t * vm,
60 vlib_node_runtime_t * node,
61 vlib_frame_t * frame, int is_ip4)
63 u32 n_left_from, * from, * to_next;
64 ip_lookup_next_t next_index;
65 vnet_classify_main_t * vcm = &vnet_classify_main;
66 ip_lookup_main_t * lm;
67 f64 now = vlib_time_now (vm);
74 lm = &ip4_main.lookup_main;
75 n_next = IP4_LOOKUP_N_NEXT;
77 lm = &ip6_main.lookup_main;
78 n_next = IP6_LOOKUP_N_NEXT;
81 from = vlib_frame_vector_args (frame);
82 n_left_from = frame->n_vectors;
84 /* First pass: compute hashes */
86 while (n_left_from > 2)
88 vlib_buffer_t * b0, * b1;
91 u32 adj_index0, adj_index1;
92 ip_adjacency_t * adj0, * adj1;
93 u32 table_index0, table_index1;
94 vnet_classify_table_t * t0, * t1;
96 /* prefetch next iteration */
98 vlib_buffer_t * p1, * p2;
100 p1 = vlib_get_buffer (vm, from[1]);
101 p2 = vlib_get_buffer (vm, from[2]);
103 vlib_prefetch_buffer_header (p1, STORE);
104 CLIB_PREFETCH (p1->data, CLIB_CACHE_LINE_BYTES, STORE);
105 vlib_prefetch_buffer_header (p2, STORE);
106 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
110 b0 = vlib_get_buffer (vm, bi0);
111 h0 = (void *)vlib_buffer_get_current(b0) -
112 ethernet_buffer_header_size(b0);
115 b1 = vlib_get_buffer (vm, bi1);
116 h1 = (void *)vlib_buffer_get_current(b1) -
117 ethernet_buffer_header_size(b1);
119 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
120 adj0 = ip_get_adjacency (lm, adj_index0);
121 table_index0 = adj0->classify.table_index;
123 adj_index1 = vnet_buffer (b1)->ip.adj_index[VLIB_TX];
124 adj1 = ip_get_adjacency (lm, adj_index1);
125 table_index1 = adj1->classify.table_index;
127 t0 = pool_elt_at_index (vcm->tables, table_index0);
129 t1 = pool_elt_at_index (vcm->tables, table_index1);
131 vnet_buffer(b0)->l2_classify.hash =
132 vnet_classify_hash_packet (t0, (u8 *) h0);
134 vnet_classify_prefetch_bucket (t0, vnet_buffer(b0)->l2_classify.hash);
136 vnet_buffer(b1)->l2_classify.hash =
137 vnet_classify_hash_packet (t1, (u8 *) h1);
139 vnet_classify_prefetch_bucket (t1, vnet_buffer(b1)->l2_classify.hash);
141 vnet_buffer(b0)->l2_classify.table_index = table_index0;
143 vnet_buffer(b1)->l2_classify.table_index = table_index1;
149 while (n_left_from > 0)
155 ip_adjacency_t * adj0;
157 vnet_classify_table_t * t0;
160 b0 = vlib_get_buffer (vm, bi0);
161 h0 = (void *)vlib_buffer_get_current(b0) -
162 ethernet_buffer_header_size(b0);
164 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
165 adj0 = ip_get_adjacency (lm, adj_index0);
166 table_index0 = adj0->classify.table_index;
168 t0 = pool_elt_at_index (vcm->tables, table_index0);
169 vnet_buffer(b0)->l2_classify.hash =
170 vnet_classify_hash_packet (t0, (u8 *) h0);
172 vnet_buffer(b0)->l2_classify.table_index = table_index0;
173 vnet_classify_prefetch_bucket (t0, vnet_buffer(b0)->l2_classify.hash);
179 next_index = node->cached_next_index;
180 from = vlib_frame_vector_args (frame);
181 n_left_from = frame->n_vectors;
183 while (n_left_from > 0)
187 vlib_get_next_frame (vm, node, next_index,
188 to_next, n_left_to_next);
190 /* Not enough load/store slots to dual loop... */
191 while (n_left_from > 0 && n_left_to_next > 0)
195 u32 next0 = IP_LOOKUP_NEXT_MISS;
197 vnet_classify_table_t * t0;
198 vnet_classify_entry_t * e0;
202 /* Stride 3 seems to work best */
203 if (PREDICT_TRUE (n_left_from > 3))
205 vlib_buffer_t * p1 = vlib_get_buffer(vm, from[3]);
206 vnet_classify_table_t * tp1;
210 table_index1 = vnet_buffer(p1)->l2_classify.table_index;
212 if (PREDICT_TRUE (table_index1 != ~0))
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);
220 /* speculatively enqueue b0 to the current next frame */
228 b0 = vlib_get_buffer (vm, bi0);
230 table_index0 = vnet_buffer(b0)->l2_classify.table_index;
233 vnet_buffer(b0)->l2_classify.opaque_index = ~0;
235 if (PREDICT_TRUE(table_index0 != ~0))
237 hash0 = vnet_buffer(b0)->l2_classify.hash;
238 t0 = pool_elt_at_index (vcm->tables, table_index0);
240 e0 = vnet_classify_find_entry (t0, (u8 *) h0, hash0,
244 vnet_buffer(b0)->l2_classify.opaque_index
246 vlib_buffer_advance (b0, e0->advance);
247 next0 = (e0->next_index < node->n_next_nodes)?
248 e0->next_index:next0;
255 if (t0->next_table_index != ~0)
256 t0 = pool_elt_at_index (vcm->tables,
257 t0->next_table_index);
260 next0 = (t0->miss_next_index < n_next) ?
261 t0->miss_next_index : next0;
266 hash0 = vnet_classify_hash_packet (t0, (u8 *) h0);
267 e0 = vnet_classify_find_entry
268 (t0, (u8 *) h0, hash0, now);
271 vnet_buffer(b0)->l2_classify.opaque_index
273 vlib_buffer_advance (b0, e0->advance);
274 next0 = (e0->next_index < node->n_next_nodes)?
275 e0->next_index:next0;
284 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
285 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
287 ip_classify_trace_t *t =
288 vlib_add_trace (vm, node, b0, sizeof (*t));
289 t->next_index = next0;
290 t->table_index = t0 ? t0 - vcm->tables : ~0;
291 t->entry_index = e0 ? e0 - t0->entries : ~0;
294 /* verify speculative enqueue, maybe switch current next frame */
295 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
296 to_next, n_left_to_next,
300 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
303 vlib_node_increment_counter (vm, node->node_index,
304 IP_CLASSIFY_ERROR_MISS,
306 vlib_node_increment_counter (vm, node->node_index,
307 IP_CLASSIFY_ERROR_HIT,
309 vlib_node_increment_counter (vm, node->node_index,
310 IP_CLASSIFY_ERROR_CHAIN_HIT,
312 return frame->n_vectors;
316 ip4_classify (vlib_main_t * vm,
317 vlib_node_runtime_t * node,
318 vlib_frame_t * frame)
320 return ip_classify_inline (vm, node, frame, 1 /* is_ip4 */);
324 VLIB_REGISTER_NODE (ip4_classify_node) = {
325 .function = ip4_classify,
326 .name = "ip4-classify",
327 .vector_size = sizeof (u32),
328 .sibling_of = "ip4-lookup",
329 .format_trace = format_ip_classify_trace,
330 .n_errors = ARRAY_LEN(ip_classify_error_strings),
331 .error_strings = ip_classify_error_strings,
336 VLIB_NODE_FUNCTION_MULTIARCH (ip4_classify_node, ip4_classify)
339 ip6_classify (vlib_main_t * vm,
340 vlib_node_runtime_t * node,
341 vlib_frame_t * frame)
343 return ip_classify_inline (vm, node, frame, 0 /* is_ip4 */);
347 VLIB_REGISTER_NODE (ip6_classify_node) = {
348 .function = ip6_classify,
349 .name = "ip6-classify",
350 .vector_size = sizeof (u32),
351 .sibling_of = "ip6-lookup",
352 .format_trace = format_ip_classify_trace,
353 .n_errors = ARRAY_LEN(ip_classify_error_strings),
354 .error_strings = ip_classify_error_strings,
359 VLIB_NODE_FUNCTION_MULTIARCH (ip6_classify_node, ip6_classify)
361 static clib_error_t *
362 ip_classify_init (vlib_main_t * vm)
367 VLIB_INIT_FUNCTION (ip_classify_init);