blob: 1a8e1dd41e5b07274b92d6be154a228a04b86a9e [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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 */
17#include <vnet/classify/vnet_classify.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010018#include <vnet/dpo/classify_dpo.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070019
khemendra kumard7bfa0e2017-11-27 15:15:53 +053020typedef struct
21{
Ed Warnickecb9cada2015-12-08 15:45:58 -070022 u32 next_index;
23 u32 table_index;
24 u32 entry_index;
25} ip_classify_trace_t;
26
27/* packet trace format function */
khemendra kumard7bfa0e2017-11-27 15:15:53 +053028static u8 *
29format_ip_classify_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070030{
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 *);
khemendra kumard7bfa0e2017-11-27 15:15:53 +053033 ip_classify_trace_t *t = va_arg (*args, ip_classify_trace_t *);
34
Ed Warnickecb9cada2015-12-08 15:45:58 -070035 s = format (s, "IP_CLASSIFY: next_index %d, table %d, entry %d",
khemendra kumard7bfa0e2017-11-27 15:15:53 +053036 t->next_index, t->table_index, t->entry_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -070037 return s;
38}
39
40vlib_node_registration_t ip4_classify_node;
41vlib_node_registration_t ip6_classify_node;
42
43#define foreach_ip_classify_error \
44_(MISS, "Classify misses") \
45_(HIT, "Classify hits") \
46_(CHAIN_HIT, "Classify hits after chain walk")
47
khemendra kumard7bfa0e2017-11-27 15:15:53 +053048typedef enum
49{
Ed Warnickecb9cada2015-12-08 15:45:58 -070050#define _(sym,str) IP_CLASSIFY_ERROR_##sym,
51 foreach_ip_classify_error
52#undef _
khemendra kumard7bfa0e2017-11-27 15:15:53 +053053 IP_CLASSIFY_N_ERROR,
Ed Warnickecb9cada2015-12-08 15:45:58 -070054} ip_classify_error_t;
55
khemendra kumard7bfa0e2017-11-27 15:15:53 +053056static char *ip_classify_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -070057#define _(sym,string) string,
58 foreach_ip_classify_error
59#undef _
60};
61
62static inline uword
63ip_classify_inline (vlib_main_t * vm,
khemendra kumard7bfa0e2017-11-27 15:15:53 +053064 vlib_node_runtime_t * node,
65 vlib_frame_t * frame, int is_ip4)
Ed Warnickecb9cada2015-12-08 15:45:58 -070066{
khemendra kumard7bfa0e2017-11-27 15:15:53 +053067 u32 n_left_from, *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -070068 ip_lookup_next_t next_index;
khemendra kumard7bfa0e2017-11-27 15:15:53 +053069 vnet_classify_main_t *vcm = &vnet_classify_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -070070 f64 now = vlib_time_now (vm);
71 u32 hits = 0;
72 u32 misses = 0;
73 u32 chain_hits = 0;
Ole Troanf0f85222016-06-14 21:12:32 +020074 u32 n_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -070075
khemendra kumard7bfa0e2017-11-27 15:15:53 +053076 if (is_ip4)
77 {
78 n_next = IP4_LOOKUP_N_NEXT;
79 }
80 else
81 {
82 n_next = IP6_LOOKUP_N_NEXT;
83 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070084
85 from = vlib_frame_vector_args (frame);
86 n_left_from = frame->n_vectors;
87
88 /* First pass: compute hashes */
89
90 while (n_left_from > 2)
91 {
khemendra kumard7bfa0e2017-11-27 15:15:53 +053092 vlib_buffer_t *b0, *b1;
Ed Warnickecb9cada2015-12-08 15:45:58 -070093 u32 bi0, bi1;
khemendra kumard7bfa0e2017-11-27 15:15:53 +053094 u8 *h0, *h1;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010095 u32 cd_index0, cd_index1;
khemendra kumard7bfa0e2017-11-27 15:15:53 +053096 classify_dpo_t *cd0, *cd1;
Ed Warnickecb9cada2015-12-08 15:45:58 -070097 u32 table_index0, table_index1;
khemendra kumard7bfa0e2017-11-27 15:15:53 +053098 vnet_classify_table_t *t0, *t1;
Ed Warnickecb9cada2015-12-08 15:45:58 -070099
100 /* prefetch next iteration */
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530101 {
102 vlib_buffer_t *p1, *p2;
103
104 p1 = vlib_get_buffer (vm, from[1]);
105 p2 = vlib_get_buffer (vm, from[2]);
106
107 vlib_prefetch_buffer_header (p1, STORE);
108 CLIB_PREFETCH (p1->data, CLIB_CACHE_LINE_BYTES, STORE);
109 vlib_prefetch_buffer_header (p2, STORE);
110 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
111 }
112
Ed Warnickecb9cada2015-12-08 15:45:58 -0700113 bi0 = from[0];
114 b0 = vlib_get_buffer (vm, bi0);
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530115 h0 = (void *) vlib_buffer_get_current (b0) -
116 ethernet_buffer_header_size (b0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700117
118 bi1 = from[1];
119 b1 = vlib_get_buffer (vm, bi1);
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530120 h1 = (void *) vlib_buffer_get_current (b1) -
121 ethernet_buffer_header_size (b1);
122
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100123 cd_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530124 cd0 = classify_dpo_get (cd_index0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100125 table_index0 = cd0->cd_table_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700126
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100127 cd_index1 = vnet_buffer (b1)->ip.adj_index[VLIB_TX];
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530128 cd1 = classify_dpo_get (cd_index1);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100129 table_index1 = cd1->cd_table_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700130
131 t0 = pool_elt_at_index (vcm->tables, table_index0);
132
133 t1 = pool_elt_at_index (vcm->tables, table_index1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530135 vnet_buffer (b0)->l2_classify.hash =
136 vnet_classify_hash_packet (t0, (u8 *) h0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700137
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530138 vnet_classify_prefetch_bucket (t0, vnet_buffer (b0)->l2_classify.hash);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700139
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530140 vnet_buffer (b1)->l2_classify.hash =
141 vnet_classify_hash_packet (t1, (u8 *) h1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700142
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530143 vnet_classify_prefetch_bucket (t1, vnet_buffer (b1)->l2_classify.hash);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700144
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530145 vnet_buffer (b0)->l2_classify.table_index = table_index0;
146
147 vnet_buffer (b1)->l2_classify.table_index = table_index1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700148
149 from += 2;
150 n_left_from -= 2;
151 }
152
153 while (n_left_from > 0)
154 {
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530155 vlib_buffer_t *b0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700156 u32 bi0;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530157 u8 *h0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100158 u32 cd_index0;
159 classify_dpo_t *cd0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700160 u32 table_index0;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530161 vnet_classify_table_t *t0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700162
163 bi0 = from[0];
164 b0 = vlib_get_buffer (vm, bi0);
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530165 h0 = (void *) vlib_buffer_get_current (b0) -
166 ethernet_buffer_header_size (b0);
167
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100168 cd_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530169 cd0 = classify_dpo_get (cd_index0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100170 table_index0 = cd0->cd_table_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700171
172 t0 = pool_elt_at_index (vcm->tables, table_index0);
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530173 vnet_buffer (b0)->l2_classify.hash =
174 vnet_classify_hash_packet (t0, (u8 *) h0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530176 vnet_buffer (b0)->l2_classify.table_index = table_index0;
177 vnet_classify_prefetch_bucket (t0, vnet_buffer (b0)->l2_classify.hash);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700178
179 from++;
180 n_left_from--;
181 }
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530182
Ed Warnickecb9cada2015-12-08 15:45:58 -0700183 next_index = node->cached_next_index;
184 from = vlib_frame_vector_args (frame);
185 n_left_from = frame->n_vectors;
186
187 while (n_left_from > 0)
188 {
189 u32 n_left_to_next;
190
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530191 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700192
193 /* Not enough load/store slots to dual loop... */
194 while (n_left_from > 0 && n_left_to_next > 0)
195 {
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530196 u32 bi0;
197 vlib_buffer_t *b0;
198 u32 next0 = IP_LOOKUP_NEXT_DROP;
199 u32 table_index0;
200 vnet_classify_table_t *t0;
201 vnet_classify_entry_t *e0;
202 u64 hash0;
203 u8 *h0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700204
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530205 /* Stride 3 seems to work best */
206 if (PREDICT_TRUE (n_left_from > 3))
207 {
208 vlib_buffer_t *p1 = vlib_get_buffer (vm, from[3]);
209 vnet_classify_table_t *tp1;
210 u32 table_index1;
211 u64 phash1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700212
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530213 table_index1 = vnet_buffer (p1)->l2_classify.table_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700214
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530215 if (PREDICT_TRUE (table_index1 != ~0))
216 {
217 tp1 = pool_elt_at_index (vcm->tables, table_index1);
218 phash1 = vnet_buffer (p1)->l2_classify.hash;
219 vnet_classify_prefetch_entry (tp1, phash1);
220 }
221 }
222
223 /* speculatively enqueue b0 to the current next frame */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700224 bi0 = from[0];
225 to_next[0] = bi0;
226 from += 1;
227 to_next += 1;
228 n_left_from -= 1;
229 n_left_to_next -= 1;
230
231 b0 = vlib_get_buffer (vm, bi0);
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530232 h0 = b0->data;
233 table_index0 = vnet_buffer (b0)->l2_classify.table_index;
234 e0 = 0;
235 t0 = 0;
236 vnet_buffer (b0)->l2_classify.opaque_index = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530238 if (PREDICT_TRUE (table_index0 != ~0))
239 {
240 hash0 = vnet_buffer (b0)->l2_classify.hash;
241 t0 = pool_elt_at_index (vcm->tables, table_index0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700242
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530243 e0 = vnet_classify_find_entry (t0, (u8 *) h0, hash0, now);
244 if (e0)
245 {
246 vnet_buffer (b0)->l2_classify.opaque_index
247 = e0->opaque_index;
248 vlib_buffer_advance (b0, e0->advance);
249 next0 = (e0->next_index < node->n_next_nodes) ?
250 e0->next_index : next0;
251 hits++;
252 }
253 else
254 {
255 while (1)
256 {
257 if (t0->next_table_index != ~0)
258 t0 = pool_elt_at_index (vcm->tables,
259 t0->next_table_index);
260 else
261 {
262 next0 = (t0->miss_next_index < n_next) ?
263 t0->miss_next_index : next0;
264 misses++;
265 break;
266 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700267
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530268 hash0 = vnet_classify_hash_packet (t0, (u8 *) h0);
269 e0 = vnet_classify_find_entry
270 (t0, (u8 *) h0, hash0, now);
271 if (e0)
272 {
273 vnet_buffer (b0)->l2_classify.opaque_index
274 = e0->opaque_index;
275 vlib_buffer_advance (b0, e0->advance);
276 next0 = (e0->next_index < node->n_next_nodes) ?
277 e0->next_index : next0;
278 hits++;
279 chain_hits++;
280 break;
281 }
282 }
283 }
284 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700285
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530286 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
287 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
288 {
289 ip_classify_trace_t *t =
290 vlib_add_trace (vm, node, b0, sizeof (*t));
291 t->next_index = next0;
292 t->table_index = t0 ? t0 - vcm->tables : ~0;
293 t->entry_index = e0 ? e0 - t0->entries : ~0;
294 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700295
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530296 /* verify speculative enqueue, maybe switch current next frame */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700297 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
298 to_next, n_left_to_next,
299 bi0, next0);
300 }
301
302 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
303 }
304
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530305 vlib_node_increment_counter (vm, node->node_index,
306 IP_CLASSIFY_ERROR_MISS, misses);
307 vlib_node_increment_counter (vm, node->node_index,
308 IP_CLASSIFY_ERROR_HIT, hits);
309 vlib_node_increment_counter (vm, node->node_index,
310 IP_CLASSIFY_ERROR_CHAIN_HIT, chain_hits);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700311 return frame->n_vectors;
312}
313
314static uword
315ip4_classify (vlib_main_t * vm,
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530316 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700317{
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530318 return ip_classify_inline (vm, node, frame, 1 /* is_ip4 */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700319}
320
321
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530322/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700323VLIB_REGISTER_NODE (ip4_classify_node) = {
324 .function = ip4_classify,
325 .name = "ip4-classify",
326 .vector_size = sizeof (u32),
Ole Troanf0f85222016-06-14 21:12:32 +0200327 .sibling_of = "ip4-lookup",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700328 .format_trace = format_ip_classify_trace,
329 .n_errors = ARRAY_LEN(ip_classify_error_strings),
330 .error_strings = ip_classify_error_strings,
331
Ole Troanf0f85222016-06-14 21:12:32 +0200332 .n_next_nodes = 0,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700333};
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530334/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700335
Damjan Marion1c80e832016-05-11 23:07:18 +0200336VLIB_NODE_FUNCTION_MULTIARCH (ip4_classify_node, ip4_classify)
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530337 static uword
338 ip6_classify (vlib_main_t * vm,
339 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700340{
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530341 return ip_classify_inline (vm, node, frame, 0 /* is_ip4 */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700342}
343
344
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530345/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700346VLIB_REGISTER_NODE (ip6_classify_node) = {
347 .function = ip6_classify,
348 .name = "ip6-classify",
349 .vector_size = sizeof (u32),
Ole Troanf0f85222016-06-14 21:12:32 +0200350 .sibling_of = "ip6-lookup",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700351 .format_trace = format_ip_classify_trace,
352 .n_errors = ARRAY_LEN(ip_classify_error_strings),
353 .error_strings = ip_classify_error_strings,
354
Ole Troanf0f85222016-06-14 21:12:32 +0200355 .n_next_nodes = 0,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700356};
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530357/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700358
Damjan Marion1c80e832016-05-11 23:07:18 +0200359VLIB_NODE_FUNCTION_MULTIARCH (ip6_classify_node, ip6_classify)
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530360 static clib_error_t *ip_classify_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700361{
362 return 0;
363}
364
365VLIB_INIT_FUNCTION (ip_classify_init);
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530366
367/*
368 * fd.io coding-style-patch-verification: ON
369 *
370 * Local Variables:
371 * eval: (c-set-style "gnu")
372 * End:
373 */