blob: 43ad2e8e8bb8fdd86c2d70b387e5078b47be411c [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
Ed Warnickecb9cada2015-12-08 15:45:58 -070040#define foreach_ip_classify_error \
41_(MISS, "Classify misses") \
42_(HIT, "Classify hits") \
43_(CHAIN_HIT, "Classify hits after chain walk")
44
khemendra kumard7bfa0e2017-11-27 15:15:53 +053045typedef enum
46{
Ed Warnickecb9cada2015-12-08 15:45:58 -070047#define _(sym,str) IP_CLASSIFY_ERROR_##sym,
48 foreach_ip_classify_error
49#undef _
khemendra kumard7bfa0e2017-11-27 15:15:53 +053050 IP_CLASSIFY_N_ERROR,
Ed Warnickecb9cada2015-12-08 15:45:58 -070051} ip_classify_error_t;
52
khemendra kumard7bfa0e2017-11-27 15:15:53 +053053static char *ip_classify_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -070054#define _(sym,string) string,
55 foreach_ip_classify_error
56#undef _
57};
58
59static inline uword
60ip_classify_inline (vlib_main_t * vm,
khemendra kumard7bfa0e2017-11-27 15:15:53 +053061 vlib_node_runtime_t * node,
62 vlib_frame_t * frame, int is_ip4)
Ed Warnickecb9cada2015-12-08 15:45:58 -070063{
khemendra kumard7bfa0e2017-11-27 15:15:53 +053064 u32 n_left_from, *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -070065 ip_lookup_next_t next_index;
khemendra kumard7bfa0e2017-11-27 15:15:53 +053066 vnet_classify_main_t *vcm = &vnet_classify_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -070067 f64 now = vlib_time_now (vm);
68 u32 hits = 0;
69 u32 misses = 0;
70 u32 chain_hits = 0;
Ole Troanf0f85222016-06-14 21:12:32 +020071 u32 n_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -070072
khemendra kumard7bfa0e2017-11-27 15:15:53 +053073 if (is_ip4)
74 {
75 n_next = IP4_LOOKUP_N_NEXT;
76 }
77 else
78 {
79 n_next = IP6_LOOKUP_N_NEXT;
80 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070081
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 {
khemendra kumard7bfa0e2017-11-27 15:15:53 +053089 vlib_buffer_t *b0, *b1;
Ed Warnickecb9cada2015-12-08 15:45:58 -070090 u32 bi0, bi1;
khemendra kumard7bfa0e2017-11-27 15:15:53 +053091 u8 *h0, *h1;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010092 u32 cd_index0, cd_index1;
khemendra kumard7bfa0e2017-11-27 15:15:53 +053093 classify_dpo_t *cd0, *cd1;
Ed Warnickecb9cada2015-12-08 15:45:58 -070094 u32 table_index0, table_index1;
khemendra kumard7bfa0e2017-11-27 15:15:53 +053095 vnet_classify_table_t *t0, *t1;
Ed Warnickecb9cada2015-12-08 15:45:58 -070096
97 /* prefetch next iteration */
khemendra kumard7bfa0e2017-11-27 15:15:53 +053098 {
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);
105 CLIB_PREFETCH (p1->data, CLIB_CACHE_LINE_BYTES, STORE);
106 vlib_prefetch_buffer_header (p2, STORE);
107 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
108 }
109
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110 bi0 = from[0];
111 b0 = vlib_get_buffer (vm, bi0);
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530112 h0 = (void *) vlib_buffer_get_current (b0) -
113 ethernet_buffer_header_size (b0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114
115 bi1 = from[1];
116 b1 = vlib_get_buffer (vm, bi1);
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530117 h1 = (void *) vlib_buffer_get_current (b1) -
118 ethernet_buffer_header_size (b1);
119
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100120 cd_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530121 cd0 = classify_dpo_get (cd_index0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100122 table_index0 = cd0->cd_table_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700123
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100124 cd_index1 = vnet_buffer (b1)->ip.adj_index[VLIB_TX];
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530125 cd1 = classify_dpo_get (cd_index1);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100126 table_index1 = cd1->cd_table_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700127
128 t0 = pool_elt_at_index (vcm->tables, table_index0);
129
130 t1 = pool_elt_at_index (vcm->tables, table_index1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700131
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530132 vnet_buffer (b0)->l2_classify.hash =
133 vnet_classify_hash_packet (t0, (u8 *) h0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530135 vnet_classify_prefetch_bucket (t0, vnet_buffer (b0)->l2_classify.hash);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700136
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530137 vnet_buffer (b1)->l2_classify.hash =
138 vnet_classify_hash_packet (t1, (u8 *) h1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700139
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530140 vnet_classify_prefetch_bucket (t1, vnet_buffer (b1)->l2_classify.hash);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700141
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530142 vnet_buffer (b0)->l2_classify.table_index = table_index0;
143
144 vnet_buffer (b1)->l2_classify.table_index = table_index1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700145
146 from += 2;
147 n_left_from -= 2;
148 }
149
150 while (n_left_from > 0)
151 {
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530152 vlib_buffer_t *b0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700153 u32 bi0;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530154 u8 *h0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100155 u32 cd_index0;
156 classify_dpo_t *cd0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700157 u32 table_index0;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530158 vnet_classify_table_t *t0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159
160 bi0 = from[0];
161 b0 = vlib_get_buffer (vm, bi0);
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530162 h0 = (void *) vlib_buffer_get_current (b0) -
163 ethernet_buffer_header_size (b0);
164
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100165 cd_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530166 cd0 = classify_dpo_get (cd_index0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100167 table_index0 = cd0->cd_table_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168
169 t0 = pool_elt_at_index (vcm->tables, table_index0);
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530170 vnet_buffer (b0)->l2_classify.hash =
171 vnet_classify_hash_packet (t0, (u8 *) h0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530173 vnet_buffer (b0)->l2_classify.table_index = table_index0;
174 vnet_classify_prefetch_bucket (t0, vnet_buffer (b0)->l2_classify.hash);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175
176 from++;
177 n_left_from--;
178 }
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530179
Ed Warnickecb9cada2015-12-08 15:45:58 -0700180 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
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530188 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700189
190 /* Not enough load/store slots to dual loop... */
191 while (n_left_from > 0 && n_left_to_next > 0)
192 {
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530193 u32 bi0;
194 vlib_buffer_t *b0;
195 u32 next0 = IP_LOOKUP_NEXT_DROP;
196 u32 table_index0;
197 vnet_classify_table_t *t0;
198 vnet_classify_entry_t *e0;
199 u64 hash0;
200 u8 *h0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700201
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530202 /* 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]);
206 vnet_classify_table_t *tp1;
207 u32 table_index1;
208 u64 phash1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700209
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530210 table_index1 = vnet_buffer (p1)->l2_classify.table_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700211
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530212 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 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700221 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);
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530229 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;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700234
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530235 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);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700239
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530240 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 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700264
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530265 hash0 = vnet_classify_hash_packet (t0, (u8 *) h0);
266 e0 = vnet_classify_find_entry
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 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700282
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530283 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
284 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
285 {
286 ip_classify_trace_t *t =
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 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700292
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530293 /* verify speculative enqueue, maybe switch current next frame */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700294 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
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530302 vlib_node_increment_counter (vm, node->node_index,
303 IP_CLASSIFY_ERROR_MISS, misses);
304 vlib_node_increment_counter (vm, node->node_index,
305 IP_CLASSIFY_ERROR_HIT, hits);
306 vlib_node_increment_counter (vm, node->node_index,
307 IP_CLASSIFY_ERROR_CHAIN_HIT, chain_hits);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700308 return frame->n_vectors;
309}
310
Filip Tehlare8cb5212019-03-06 04:50:34 -0800311VLIB_NODE_FN (ip4_classify_node) (vlib_main_t * vm,
312 vlib_node_runtime_t * node,
313 vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700314{
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530315 return ip_classify_inline (vm, node, frame, 1 /* is_ip4 */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700316}
317
318
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530319/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700320VLIB_REGISTER_NODE (ip4_classify_node) = {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700321 .name = "ip4-classify",
322 .vector_size = sizeof (u32),
Ole Troanf0f85222016-06-14 21:12:32 +0200323 .sibling_of = "ip4-lookup",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700324 .format_trace = format_ip_classify_trace,
325 .n_errors = ARRAY_LEN(ip_classify_error_strings),
326 .error_strings = ip_classify_error_strings,
327
Ole Troanf0f85222016-06-14 21:12:32 +0200328 .n_next_nodes = 0,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700329};
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530330/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700331
Filip Tehlare8cb5212019-03-06 04:50:34 -0800332VLIB_NODE_FN (ip6_classify_node) (vlib_main_t * vm,
333 vlib_node_runtime_t * node,
334 vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700335{
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530336 return ip_classify_inline (vm, node, frame, 0 /* is_ip4 */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700337}
338
339
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530340/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700341VLIB_REGISTER_NODE (ip6_classify_node) = {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700342 .name = "ip6-classify",
343 .vector_size = sizeof (u32),
Ole Troanf0f85222016-06-14 21:12:32 +0200344 .sibling_of = "ip6-lookup",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700345 .format_trace = format_ip_classify_trace,
346 .n_errors = ARRAY_LEN(ip_classify_error_strings),
347 .error_strings = ip_classify_error_strings,
348
Ole Troanf0f85222016-06-14 21:12:32 +0200349 .n_next_nodes = 0,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700350};
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530351/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700352
Filip Tehlare8cb5212019-03-06 04:50:34 -0800353#ifndef CLIB_MARCH_VARIANT
354static clib_error_t *
355ip_classify_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700356{
357 return 0;
358}
359
360VLIB_INIT_FUNCTION (ip_classify_init);
Filip Tehlare8cb5212019-03-06 04:50:34 -0800361#endif /* CLIB_MARCH_VARIANT */
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530362
363/*
364 * fd.io coding-style-patch-verification: ON
365 *
366 * Local Variables:
367 * eval: (c-set-style "gnu")
368 * End:
369 */