blob: d8a5a45e252a5ea09184d6bb26d28f510037a825 [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/classify/vnet_classify.h>
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +010016#include <vnet/classify/in_out_acl.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070017#include <vnet/ip/ip.h>
khemendra kumard7bfa0e2017-11-27 15:15:53 +053018#include <vnet/api_errno.h> /* for API error numbers */
19#include <vnet/l2/l2_classify.h> /* for L2_INPUT_CLASSIFY_NEXT_xxx */
Steve Shin25e26dc2016-11-08 10:47:10 -080020#include <vnet/fib/fib_table.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070021
Dave Barachf39ff742016-03-20 10:14:45 -040022vnet_classify_main_t vnet_classify_main;
23
Ed Warnickecb9cada2015-12-08 15:45:58 -070024#if VALIDATION_SCAFFOLDING
25/* Validation scaffolding */
khemendra kumard7bfa0e2017-11-27 15:15:53 +053026void
27mv (vnet_classify_table_t * t)
Ed Warnickecb9cada2015-12-08 15:45:58 -070028{
khemendra kumard7bfa0e2017-11-27 15:15:53 +053029 void *oldheap;
Ed Warnickecb9cada2015-12-08 15:45:58 -070030
31 oldheap = clib_mem_set_heap (t->mheap);
khemendra kumard7bfa0e2017-11-27 15:15:53 +053032 clib_mem_validate ();
Ed Warnickecb9cada2015-12-08 15:45:58 -070033 clib_mem_set_heap (oldheap);
34}
35
khemendra kumard7bfa0e2017-11-27 15:15:53 +053036void
37rogue (vnet_classify_table_t * t)
Ed Warnickecb9cada2015-12-08 15:45:58 -070038{
39 int i, j, k;
khemendra kumard7bfa0e2017-11-27 15:15:53 +053040 vnet_classify_entry_t *v, *save_v;
Ed Warnickecb9cada2015-12-08 15:45:58 -070041 u32 active_elements = 0;
khemendra kumard7bfa0e2017-11-27 15:15:53 +053042 vnet_classify_bucket_t *b;
43
Ed Warnickecb9cada2015-12-08 15:45:58 -070044 for (i = 0; i < t->nbuckets; i++)
45 {
khemendra kumard7bfa0e2017-11-27 15:15:53 +053046 b = &t->buckets[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -070047 if (b->offset == 0)
khemendra kumard7bfa0e2017-11-27 15:15:53 +053048 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -070049 save_v = vnet_classify_get_entry (t, b->offset);
khemendra kumard7bfa0e2017-11-27 15:15:53 +053050 for (j = 0; j < (1 << b->log2_pages); j++)
51 {
52 for (k = 0; k < t->entries_per_page; k++)
53 {
54 v = vnet_classify_entry_at_index
55 (t, save_v, j * t->entries_per_page + k);
Ed Warnickecb9cada2015-12-08 15:45:58 -070056
khemendra kumard7bfa0e2017-11-27 15:15:53 +053057 if (vnet_classify_entry_is_busy (v))
58 active_elements++;
59 }
60 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070061 }
62
63 if (active_elements != t->active_elements)
khemendra kumard7bfa0e2017-11-27 15:15:53 +053064 clib_warning ("found %u expected %u elts", active_elements,
65 t->active_elements);
Ed Warnickecb9cada2015-12-08 15:45:58 -070066}
67#else
khemendra kumard7bfa0e2017-11-27 15:15:53 +053068void
69mv (vnet_classify_table_t * t)
70{
71}
72
73void
74rogue (vnet_classify_table_t * t)
75{
76}
Ed Warnickecb9cada2015-12-08 15:45:58 -070077#endif
78
khemendra kumard7bfa0e2017-11-27 15:15:53 +053079void
80vnet_classify_register_unformat_l2_next_index_fn (unformat_function_t * fn)
Dave Barachf39ff742016-03-20 10:14:45 -040081{
khemendra kumard7bfa0e2017-11-27 15:15:53 +053082 vnet_classify_main_t *cm = &vnet_classify_main;
Dave Barachf39ff742016-03-20 10:14:45 -040083
84 vec_add1 (cm->unformat_l2_next_index_fns, fn);
85}
86
khemendra kumard7bfa0e2017-11-27 15:15:53 +053087void
88vnet_classify_register_unformat_ip_next_index_fn (unformat_function_t * fn)
Dave Barachf39ff742016-03-20 10:14:45 -040089{
khemendra kumard7bfa0e2017-11-27 15:15:53 +053090 vnet_classify_main_t *cm = &vnet_classify_main;
Dave Barachf39ff742016-03-20 10:14:45 -040091
92 vec_add1 (cm->unformat_ip_next_index_fns, fn);
93}
94
khemendra kumard7bfa0e2017-11-27 15:15:53 +053095void
Dave Barachf39ff742016-03-20 10:14:45 -040096vnet_classify_register_unformat_acl_next_index_fn (unformat_function_t * fn)
97{
khemendra kumard7bfa0e2017-11-27 15:15:53 +053098 vnet_classify_main_t *cm = &vnet_classify_main;
Dave Barachf39ff742016-03-20 10:14:45 -040099
100 vec_add1 (cm->unformat_acl_next_index_fns, fn);
101}
102
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700103void
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530104vnet_classify_register_unformat_policer_next_index_fn (unformat_function_t *
105 fn)
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700106{
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530107 vnet_classify_main_t *cm = &vnet_classify_main;
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700108
109 vec_add1 (cm->unformat_policer_next_index_fns, fn);
110}
111
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530112void
113vnet_classify_register_unformat_opaque_index_fn (unformat_function_t * fn)
Dave Barachf39ff742016-03-20 10:14:45 -0400114{
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530115 vnet_classify_main_t *cm = &vnet_classify_main;
Dave Barachf39ff742016-03-20 10:14:45 -0400116
117 vec_add1 (cm->unformat_opaque_index_fns, fn);
118}
119
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530120vnet_classify_table_t *
121vnet_classify_new_table (vnet_classify_main_t * cm,
122 u8 * mask, u32 nbuckets, u32 memory_size,
123 u32 skip_n_vectors, u32 match_n_vectors)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124{
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530125 vnet_classify_table_t *t;
126 void *oldheap;
127
Ed Warnickecb9cada2015-12-08 15:45:58 -0700128 nbuckets = 1 << (max_log2 (nbuckets));
129
130 pool_get_aligned (cm->tables, t, CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -0400131 clib_memset (t, 0, sizeof (*t));
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530132
133 vec_validate_aligned (t->mask, match_n_vectors - 1, sizeof (u32x4));
Dave Barach178cf492018-11-13 16:34:13 -0500134 clib_memcpy_fast (t->mask, mask, match_n_vectors * sizeof (u32x4));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700135
136 t->next_table_index = ~0;
137 t->nbuckets = nbuckets;
138 t->log2_nbuckets = max_log2 (nbuckets);
139 t->match_n_vectors = match_n_vectors;
140 t->skip_n_vectors = skip_n_vectors;
141 t->entries_per_page = 2;
142
Dave Barach6a5adc32018-07-04 10:56:23 -0400143#if USE_DLMALLOC == 0
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530144 t->mheap = mheap_alloc (0 /* use VM */ , memory_size);
Dave Barach6a5adc32018-07-04 10:56:23 -0400145#else
146 t->mheap = create_mspace (memory_size, 1 /* locked */ );
Andrew Yourtchenkoa990a2e2019-03-18 10:49:56 +0100147 /* classifier requires the memory to be contiguous, so can not expand. */
148 mspace_disable_expand (t->mheap);
Dave Barach6a5adc32018-07-04 10:56:23 -0400149#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150
151 vec_validate_aligned (t->buckets, nbuckets - 1, CLIB_CACHE_LINE_BYTES);
152 oldheap = clib_mem_set_heap (t->mheap);
153
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530154 t->writer_lock = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
155 CLIB_CACHE_LINE_BYTES);
Pierre Pfistercb656302016-03-16 09:14:28 +0000156 t->writer_lock[0] = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700157
158 clib_mem_set_heap (oldheap);
159 return (t);
160}
161
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530162void
163vnet_classify_delete_table_index (vnet_classify_main_t * cm,
164 u32 table_index, int del_chain)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700165{
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530166 vnet_classify_table_t *t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700167
168 /* Tolerate multiple frees, up to a point */
169 if (pool_is_free_index (cm->tables, table_index))
170 return;
171
172 t = pool_elt_at_index (cm->tables, table_index);
Juraj Sloboda288e8932016-12-06 21:25:19 +0100173 if (del_chain && t->next_table_index != ~0)
174 /* Recursively delete the entire chain */
175 vnet_classify_delete_table_index (cm, t->next_table_index, del_chain);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700176
177 vec_free (t->mask);
178 vec_free (t->buckets);
Dave Barach6a5adc32018-07-04 10:56:23 -0400179#if USE_DLMALLOC == 0
Ed Warnickecb9cada2015-12-08 15:45:58 -0700180 mheap_free (t->mheap);
Dave Barach6a5adc32018-07-04 10:56:23 -0400181#else
182 destroy_mspace (t->mheap);
183#endif
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530184
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185 pool_put (cm->tables, t);
186}
187
188static vnet_classify_entry_t *
189vnet_classify_entry_alloc (vnet_classify_table_t * t, u32 log2_pages)
190{
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530191 vnet_classify_entry_t *rv = 0;
Dave Barachcada2a02017-05-18 19:16:47 -0400192 u32 required_length;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530193 void *oldheap;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700194
195 ASSERT (t->writer_lock[0]);
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530196 required_length =
197 (sizeof (vnet_classify_entry_t) + (t->match_n_vectors * sizeof (u32x4)))
198 * t->entries_per_page * (1 << log2_pages);
Dave Barachcada2a02017-05-18 19:16:47 -0400199
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530200 if (log2_pages >= vec_len (t->freelists) || t->freelists[log2_pages] == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700201 {
202 oldheap = clib_mem_set_heap (t->mheap);
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530203
Ed Warnickecb9cada2015-12-08 15:45:58 -0700204 vec_validate (t->freelists, log2_pages);
205
Dave Barachcada2a02017-05-18 19:16:47 -0400206 rv = clib_mem_alloc_aligned (required_length, CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700207 clib_mem_set_heap (oldheap);
208 goto initialize;
209 }
210 rv = t->freelists[log2_pages];
211 t->freelists[log2_pages] = rv->next_free;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530212
Ed Warnickecb9cada2015-12-08 15:45:58 -0700213initialize:
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530214 ASSERT (rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700215
Dave Barachb7b92992018-10-17 10:38:51 -0400216 clib_memset (rv, 0xff, required_length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700217 return rv;
218}
219
220static void
221vnet_classify_entry_free (vnet_classify_table_t * t,
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530222 vnet_classify_entry_t * v, u32 log2_pages)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700223{
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530224 ASSERT (t->writer_lock[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700225
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530226 ASSERT (vec_len (t->freelists) > log2_pages);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700227
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530228 v->next_free = t->freelists[log2_pages];
229 t->freelists[log2_pages] = v;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700230}
231
232static inline void make_working_copy
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530233 (vnet_classify_table_t * t, vnet_classify_bucket_t * b)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700234{
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530235 vnet_classify_entry_t *v;
236 vnet_classify_bucket_t working_bucket __attribute__ ((aligned (8)));
237 void *oldheap;
238 vnet_classify_entry_t *working_copy;
239 u32 thread_index = vlib_get_thread_index ();
Dave Barachcada2a02017-05-18 19:16:47 -0400240 int working_copy_length, required_length;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241
Damjan Marion586afd72017-04-05 19:18:20 +0200242 if (thread_index >= vec_len (t->working_copies))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700243 {
244 oldheap = clib_mem_set_heap (t->mheap);
Damjan Marion586afd72017-04-05 19:18:20 +0200245 vec_validate (t->working_copies, thread_index);
Dave Barachcada2a02017-05-18 19:16:47 -0400246 vec_validate (t->working_copy_lengths, thread_index);
247 t->working_copy_lengths[thread_index] = -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700248 clib_mem_set_heap (oldheap);
249 }
250
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530251 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700252 * working_copies are per-cpu so that near-simultaneous
253 * updates from multiple threads will not result in sporadic, spurious
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530254 * lookup failures.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700255 */
Damjan Marion586afd72017-04-05 19:18:20 +0200256 working_copy = t->working_copies[thread_index];
Dave Barachcada2a02017-05-18 19:16:47 -0400257 working_copy_length = t->working_copy_lengths[thread_index];
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530258 required_length =
259 (sizeof (vnet_classify_entry_t) + (t->match_n_vectors * sizeof (u32x4)))
260 * t->entries_per_page * (1 << b->log2_pages);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700261
262 t->saved_bucket.as_u64 = b->as_u64;
263 oldheap = clib_mem_set_heap (t->mheap);
264
Dave Barachcada2a02017-05-18 19:16:47 -0400265 if (required_length > working_copy_length)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700266 {
Dave Barachcada2a02017-05-18 19:16:47 -0400267 if (working_copy)
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530268 clib_mem_free (working_copy);
Dave Barachcada2a02017-05-18 19:16:47 -0400269 working_copy =
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530270 clib_mem_alloc_aligned (required_length, CLIB_CACHE_LINE_BYTES);
Damjan Marion586afd72017-04-05 19:18:20 +0200271 t->working_copies[thread_index] = working_copy;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700272 }
273
Ed Warnickecb9cada2015-12-08 15:45:58 -0700274 clib_mem_set_heap (oldheap);
275
276 v = vnet_classify_get_entry (t, b->offset);
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530277
Dave Barach178cf492018-11-13 16:34:13 -0500278 clib_memcpy_fast (working_copy, v, required_length);
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530279
Ed Warnickecb9cada2015-12-08 15:45:58 -0700280 working_bucket.as_u64 = b->as_u64;
281 working_bucket.offset = vnet_classify_get_offset (t, working_copy);
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530282 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700283 b->as_u64 = working_bucket.as_u64;
Damjan Marion586afd72017-04-05 19:18:20 +0200284 t->working_copies[thread_index] = working_copy;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700285}
286
287static vnet_classify_entry_t *
288split_and_rehash (vnet_classify_table_t * t,
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530289 vnet_classify_entry_t * old_values, u32 old_log2_pages,
290 u32 new_log2_pages)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700291{
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530292 vnet_classify_entry_t *new_values, *v, *new_v;
Dave Barachcada2a02017-05-18 19:16:47 -0400293 int i, j, length_in_entries;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530294
Ed Warnickecb9cada2015-12-08 15:45:58 -0700295 new_values = vnet_classify_entry_alloc (t, new_log2_pages);
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530296 length_in_entries = (1 << old_log2_pages) * t->entries_per_page;
297
Dave Barachcada2a02017-05-18 19:16:47 -0400298 for (i = 0; i < length_in_entries; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700299 {
300 u64 new_hash;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530301
Dave Barachcada2a02017-05-18 19:16:47 -0400302 v = vnet_classify_entry_at_index (t, old_values, i);
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530303
Dave Barachcada2a02017-05-18 19:16:47 -0400304 if (vnet_classify_entry_is_busy (v))
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530305 {
306 /* Hack so we can use the packet hash routine */
307 u8 *key_minus_skip;
308 key_minus_skip = (u8 *) v->key;
309 key_minus_skip -= t->skip_n_vectors * sizeof (u32x4);
310
311 new_hash = vnet_classify_hash_packet (t, key_minus_skip);
312 new_hash >>= t->log2_nbuckets;
313 new_hash &= (1 << new_log2_pages) - 1;
314
315 for (j = 0; j < t->entries_per_page; j++)
316 {
317 new_v = vnet_classify_entry_at_index (t, new_values,
318 new_hash + j);
319
320 if (vnet_classify_entry_is_free (new_v))
321 {
Dave Barach178cf492018-11-13 16:34:13 -0500322 clib_memcpy_fast (new_v, v, sizeof (vnet_classify_entry_t)
323 + (t->match_n_vectors * sizeof (u32x4)));
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530324 new_v->flags &= ~(VNET_CLASSIFY_ENTRY_FREE);
325 goto doublebreak;
326 }
327 }
328 /* Crap. Tell caller to try again */
329 vnet_classify_entry_free (t, new_values, new_log2_pages);
330 return 0;
331 doublebreak:
332 ;
333 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700334 }
335 return new_values;
336}
337
Dave Barachcada2a02017-05-18 19:16:47 -0400338static vnet_classify_entry_t *
339split_and_rehash_linear (vnet_classify_table_t * t,
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530340 vnet_classify_entry_t * old_values,
341 u32 old_log2_pages, u32 new_log2_pages)
Dave Barachcada2a02017-05-18 19:16:47 -0400342{
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530343 vnet_classify_entry_t *new_values, *v, *new_v;
Dave Barachcada2a02017-05-18 19:16:47 -0400344 int i, j, new_length_in_entries, old_length_in_entries;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530345
Dave Barachcada2a02017-05-18 19:16:47 -0400346 new_values = vnet_classify_entry_alloc (t, new_log2_pages);
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530347 new_length_in_entries = (1 << new_log2_pages) * t->entries_per_page;
348 old_length_in_entries = (1 << old_log2_pages) * t->entries_per_page;
349
Dave Barachcada2a02017-05-18 19:16:47 -0400350 j = 0;
351 for (i = 0; i < old_length_in_entries; i++)
352 {
353 v = vnet_classify_entry_at_index (t, old_values, i);
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530354
Dave Barachcada2a02017-05-18 19:16:47 -0400355 if (vnet_classify_entry_is_busy (v))
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530356 {
357 for (; j < new_length_in_entries; j++)
358 {
359 new_v = vnet_classify_entry_at_index (t, new_values, j);
360
361 if (vnet_classify_entry_is_busy (new_v))
362 {
363 clib_warning ("BUG: linear rehash new entry not free!");
364 continue;
365 }
Dave Barach178cf492018-11-13 16:34:13 -0500366 clib_memcpy_fast (new_v, v, sizeof (vnet_classify_entry_t)
367 + (t->match_n_vectors * sizeof (u32x4)));
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530368 new_v->flags &= ~(VNET_CLASSIFY_ENTRY_FREE);
369 j++;
370 goto doublebreak;
371 }
372 /*
373 * Crap. Tell caller to try again.
374 * This should never happen...
375 */
376 clib_warning ("BUG: linear rehash failed!");
377 vnet_classify_entry_free (t, new_values, new_log2_pages);
378 return 0;
379 }
Dave Barachcada2a02017-05-18 19:16:47 -0400380 doublebreak:
381 ;
382 }
383
384 return new_values;
385}
386
Neale Ranns13eaf3e2017-05-23 06:10:33 -0700387static void
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530388vnet_classify_entry_claim_resource (vnet_classify_entry_t * e)
Neale Ranns13eaf3e2017-05-23 06:10:33 -0700389{
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530390 switch (e->action)
Neale Ranns13eaf3e2017-05-23 06:10:33 -0700391 {
392 case CLASSIFY_ACTION_SET_IP4_FIB_INDEX:
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530393 fib_table_lock (e->metadata, FIB_PROTOCOL_IP4, FIB_SOURCE_CLASSIFY);
394 break;
Neale Ranns13eaf3e2017-05-23 06:10:33 -0700395 case CLASSIFY_ACTION_SET_IP6_FIB_INDEX:
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530396 fib_table_lock (e->metadata, FIB_PROTOCOL_IP6, FIB_SOURCE_CLASSIFY);
397 break;
Dave Barach630a8e22017-11-18 06:58:34 -0500398 case CLASSIFY_ACTION_SET_METADATA:
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530399 break;
Neale Ranns13eaf3e2017-05-23 06:10:33 -0700400 }
401}
402
403static void
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530404vnet_classify_entry_release_resource (vnet_classify_entry_t * e)
Neale Ranns13eaf3e2017-05-23 06:10:33 -0700405{
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530406 switch (e->action)
Neale Ranns13eaf3e2017-05-23 06:10:33 -0700407 {
408 case CLASSIFY_ACTION_SET_IP4_FIB_INDEX:
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530409 fib_table_unlock (e->metadata, FIB_PROTOCOL_IP4, FIB_SOURCE_CLASSIFY);
410 break;
Neale Ranns13eaf3e2017-05-23 06:10:33 -0700411 case CLASSIFY_ACTION_SET_IP6_FIB_INDEX:
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530412 fib_table_unlock (e->metadata, FIB_PROTOCOL_IP6, FIB_SOURCE_CLASSIFY);
413 break;
Dave Barach630a8e22017-11-18 06:58:34 -0500414 case CLASSIFY_ACTION_SET_METADATA:
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530415 break;
Neale Ranns13eaf3e2017-05-23 06:10:33 -0700416 }
417}
418
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530419int
420vnet_classify_add_del (vnet_classify_table_t * t,
421 vnet_classify_entry_t * add_v, int is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700422{
423 u32 bucket_index;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530424 vnet_classify_bucket_t *b, tmp_b;
425 vnet_classify_entry_t *v, *new_v, *save_new_v, *working_copy, *save_v;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700426 u32 value_index;
427 int rv = 0;
428 int i;
429 u64 hash, new_hash;
Dave Barachcada2a02017-05-18 19:16:47 -0400430 u32 limit;
431 u32 old_log2_pages, new_log2_pages;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530432 u32 thread_index = vlib_get_thread_index ();
433 u8 *key_minus_skip;
Dave Barach48113e02017-06-07 08:32:51 -0400434 int resplit_once = 0;
Dave Barachcada2a02017-05-18 19:16:47 -0400435 int mark_bucket_linear;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700436
437 ASSERT ((add_v->flags & VNET_CLASSIFY_ENTRY_FREE) == 0);
438
439 key_minus_skip = (u8 *) add_v->key;
440 key_minus_skip -= t->skip_n_vectors * sizeof (u32x4);
441
442 hash = vnet_classify_hash_packet (t, key_minus_skip);
443
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530444 bucket_index = hash & (t->nbuckets - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700445 b = &t->buckets[bucket_index];
446
447 hash >>= t->log2_nbuckets;
448
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000449 while (clib_atomic_test_and_set (t->writer_lock))
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530450 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700451
452 /* First elt in the bucket? */
453 if (b->offset == 0)
454 {
455 if (is_add == 0)
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530456 {
457 rv = -1;
458 goto unlock;
459 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700460
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530461 v = vnet_classify_entry_alloc (t, 0 /* new_log2_pages */ );
Dave Barach178cf492018-11-13 16:34:13 -0500462 clib_memcpy_fast (v, add_v, sizeof (vnet_classify_entry_t) +
463 t->match_n_vectors * sizeof (u32x4));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700464 v->flags &= ~(VNET_CLASSIFY_ENTRY_FREE);
Neale Ranns13eaf3e2017-05-23 06:10:33 -0700465 vnet_classify_entry_claim_resource (v);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700466
467 tmp_b.as_u64 = 0;
468 tmp_b.offset = vnet_classify_get_offset (t, v);
469
470 b->as_u64 = tmp_b.as_u64;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530471 t->active_elements++;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700472
473 goto unlock;
474 }
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530475
Ed Warnickecb9cada2015-12-08 15:45:58 -0700476 make_working_copy (t, b);
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530477
Ed Warnickecb9cada2015-12-08 15:45:58 -0700478 save_v = vnet_classify_get_entry (t, t->saved_bucket.offset);
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530479 value_index = hash & ((1 << t->saved_bucket.log2_pages) - 1);
Dave Barachcada2a02017-05-18 19:16:47 -0400480 limit = t->entries_per_page;
481 if (PREDICT_FALSE (b->linear_search))
482 {
483 value_index = 0;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530484 limit *= (1 << b->log2_pages);
Dave Barachcada2a02017-05-18 19:16:47 -0400485 }
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530486
Ed Warnickecb9cada2015-12-08 15:45:58 -0700487 if (is_add)
488 {
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530489 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700490 * For obvious (in hindsight) reasons, see if we're supposed to
491 * replace an existing key, then look for an empty slot.
492 */
493
Dave Barachcada2a02017-05-18 19:16:47 -0400494 for (i = 0; i < limit; i++)
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530495 {
496 v = vnet_classify_entry_at_index (t, save_v, value_index + i);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700497
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530498 if (!memcmp
499 (v->key, add_v->key, t->match_n_vectors * sizeof (u32x4)))
500 {
Dave Barach178cf492018-11-13 16:34:13 -0500501 clib_memcpy_fast (v, add_v, sizeof (vnet_classify_entry_t) +
502 t->match_n_vectors * sizeof (u32x4));
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530503 v->flags &= ~(VNET_CLASSIFY_ENTRY_FREE);
504 vnet_classify_entry_claim_resource (v);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700505
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530506 CLIB_MEMORY_BARRIER ();
507 /* Restore the previous (k,v) pairs */
508 b->as_u64 = t->saved_bucket.as_u64;
509 goto unlock;
510 }
511 }
Dave Barachcada2a02017-05-18 19:16:47 -0400512 for (i = 0; i < limit; i++)
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530513 {
514 v = vnet_classify_entry_at_index (t, save_v, value_index + i);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700515
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530516 if (vnet_classify_entry_is_free (v))
517 {
Dave Barach178cf492018-11-13 16:34:13 -0500518 clib_memcpy_fast (v, add_v, sizeof (vnet_classify_entry_t) +
519 t->match_n_vectors * sizeof (u32x4));
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530520 v->flags &= ~(VNET_CLASSIFY_ENTRY_FREE);
521 vnet_classify_entry_claim_resource (v);
Neale Ranns13eaf3e2017-05-23 06:10:33 -0700522
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530523 CLIB_MEMORY_BARRIER ();
524 b->as_u64 = t->saved_bucket.as_u64;
525 t->active_elements++;
526 goto unlock;
527 }
528 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700529 /* no room at the inn... split case... */
530 }
531 else
532 {
Dave Barachcada2a02017-05-18 19:16:47 -0400533 for (i = 0; i < limit; i++)
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530534 {
535 v = vnet_classify_entry_at_index (t, save_v, value_index + i);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700536
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530537 if (!memcmp
538 (v->key, add_v->key, t->match_n_vectors * sizeof (u32x4)))
539 {
540 vnet_classify_entry_release_resource (v);
Dave Barachb7b92992018-10-17 10:38:51 -0400541 clib_memset (v, 0xff, sizeof (vnet_classify_entry_t) +
542 t->match_n_vectors * sizeof (u32x4));
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530543 v->flags |= VNET_CLASSIFY_ENTRY_FREE;
Neale Ranns13eaf3e2017-05-23 06:10:33 -0700544
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530545 CLIB_MEMORY_BARRIER ();
546 b->as_u64 = t->saved_bucket.as_u64;
547 t->active_elements--;
548 goto unlock;
549 }
550 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700551 rv = -3;
552 b->as_u64 = t->saved_bucket.as_u64;
553 goto unlock;
554 }
555
Dave Barachcada2a02017-05-18 19:16:47 -0400556 old_log2_pages = t->saved_bucket.log2_pages;
557 new_log2_pages = old_log2_pages + 1;
Damjan Marion586afd72017-04-05 19:18:20 +0200558 working_copy = t->working_copies[thread_index];
Dave Barachcada2a02017-05-18 19:16:47 -0400559
560 if (t->saved_bucket.linear_search)
561 goto linear_resplit;
562
563 mark_bucket_linear = 0;
564
565 new_v = split_and_rehash (t, working_copy, old_log2_pages, new_log2_pages);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700566
567 if (new_v == 0)
568 {
Dave Barachcada2a02017-05-18 19:16:47 -0400569 try_resplit:
570 resplit_once = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700571 new_log2_pages++;
Dave Barachcada2a02017-05-18 19:16:47 -0400572
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530573 new_v = split_and_rehash (t, working_copy, old_log2_pages,
574 new_log2_pages);
Dave Barachcada2a02017-05-18 19:16:47 -0400575 if (new_v == 0)
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530576 {
577 mark_linear:
578 new_log2_pages--;
Dave Barachcada2a02017-05-18 19:16:47 -0400579
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530580 linear_resplit:
Dave Barachcada2a02017-05-18 19:16:47 -0400581 /* pinned collisions, use linear search */
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530582 new_v = split_and_rehash_linear (t, working_copy, old_log2_pages,
583 new_log2_pages);
584 /* A new linear-search bucket? */
585 if (!t->saved_bucket.linear_search)
586 t->linear_buckets++;
587 mark_bucket_linear = 1;
588 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700589 }
590
591 /* Try to add the new entry */
592 save_new_v = new_v;
593
594 key_minus_skip = (u8 *) add_v->key;
595 key_minus_skip -= t->skip_n_vectors * sizeof (u32x4);
596
597 new_hash = vnet_classify_hash_packet_inline (t, key_minus_skip);
598 new_hash >>= t->log2_nbuckets;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530599 new_hash &= (1 << new_log2_pages) - 1;
Dave Barachcada2a02017-05-18 19:16:47 -0400600
601 limit = t->entries_per_page;
602 if (mark_bucket_linear)
603 {
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530604 limit *= (1 << new_log2_pages);
Dave Barachcada2a02017-05-18 19:16:47 -0400605 new_hash = 0;
606 }
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530607
Dave Barachcada2a02017-05-18 19:16:47 -0400608 for (i = 0; i < limit; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700609 {
610 new_v = vnet_classify_entry_at_index (t, save_new_v, new_hash + i);
611
612 if (vnet_classify_entry_is_free (new_v))
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530613 {
Dave Barach178cf492018-11-13 16:34:13 -0500614 clib_memcpy_fast (new_v, add_v, sizeof (vnet_classify_entry_t) +
615 t->match_n_vectors * sizeof (u32x4));
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530616 new_v->flags &= ~(VNET_CLASSIFY_ENTRY_FREE);
617 vnet_classify_entry_claim_resource (new_v);
Neale Ranns13eaf3e2017-05-23 06:10:33 -0700618
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530619 goto expand_ok;
620 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700621 }
622 /* Crap. Try again */
Dave Barachcada2a02017-05-18 19:16:47 -0400623 vnet_classify_entry_free (t, save_new_v, new_log2_pages);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700624 new_log2_pages++;
Dave Barachcada2a02017-05-18 19:16:47 -0400625
626 if (resplit_once)
627 goto mark_linear;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530628 else
Dave Barachcada2a02017-05-18 19:16:47 -0400629 goto try_resplit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700630
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530631expand_ok:
Dave Barachcada2a02017-05-18 19:16:47 -0400632 tmp_b.log2_pages = new_log2_pages;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700633 tmp_b.offset = vnet_classify_get_offset (t, save_new_v);
Dave Barachcada2a02017-05-18 19:16:47 -0400634 tmp_b.linear_search = mark_bucket_linear;
635
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530636 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700637 b->as_u64 = tmp_b.as_u64;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530638 t->active_elements++;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700639 v = vnet_classify_get_entry (t, t->saved_bucket.offset);
Dave Barachcada2a02017-05-18 19:16:47 -0400640 vnet_classify_entry_free (t, v, old_log2_pages);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700641
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530642unlock:
643 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700644 t->writer_lock[0] = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700645 return rv;
646}
647
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530648/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700649typedef CLIB_PACKED(struct {
650 ethernet_header_t eh;
651 ip4_header_t ip;
652}) classify_data_or_mask_t;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530653/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700654
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530655u64
656vnet_classify_hash_packet (vnet_classify_table_t * t, u8 * h)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700657{
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530658 return vnet_classify_hash_packet_inline (t, h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700659}
660
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530661vnet_classify_entry_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700662vnet_classify_find_entry (vnet_classify_table_t * t,
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530663 u8 * h, u64 hash, f64 now)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700664{
665 return vnet_classify_find_entry_inline (t, h, hash, now);
666}
667
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530668static u8 *
669format_classify_entry (u8 * s, va_list * args)
670{
671 vnet_classify_table_t *t = va_arg (*args, vnet_classify_table_t *);
672 vnet_classify_entry_t *e = va_arg (*args, vnet_classify_entry_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700673
674 s = format
Steve Shin25e26dc2016-11-08 10:47:10 -0800675 (s, "[%u]: next_index %d advance %d opaque %d action %d metadata %d\n",
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530676 vnet_classify_get_offset (t, e), e->next_index, e->advance,
Steve Shin25e26dc2016-11-08 10:47:10 -0800677 e->opaque_index, e->action, e->metadata);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700678
679
680 s = format (s, " k: %U\n", format_hex_bytes, e->key,
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530681 t->match_n_vectors * sizeof (u32x4));
682
Ed Warnickecb9cada2015-12-08 15:45:58 -0700683 if (vnet_classify_entry_is_busy (e))
684 s = format (s, " hits %lld, last_heard %.2f\n",
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530685 e->hits, e->last_heard);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700686 else
687 s = format (s, " entry is free\n");
688 return s;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530689}
690
691u8 *
692format_classify_table (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700693{
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530694 vnet_classify_table_t *t = va_arg (*args, vnet_classify_table_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700695 int verbose = va_arg (*args, int);
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530696 vnet_classify_bucket_t *b;
697 vnet_classify_entry_t *v, *save_v;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700698 int i, j, k;
699 u64 active_elements = 0;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530700
Ed Warnickecb9cada2015-12-08 15:45:58 -0700701 for (i = 0; i < t->nbuckets; i++)
702 {
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530703 b = &t->buckets[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700704 if (b->offset == 0)
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530705 {
706 if (verbose > 1)
707 s = format (s, "[%d]: empty\n", i);
708 continue;
709 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700710
711 if (verbose)
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530712 {
713 s = format (s, "[%d]: heap offset %d, elts %d, %s\n", i,
714 b->offset, (1 << b->log2_pages) * t->entries_per_page,
715 b->linear_search ? "LINEAR" : "normal");
716 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700717
718 save_v = vnet_classify_get_entry (t, b->offset);
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530719 for (j = 0; j < (1 << b->log2_pages); j++)
720 {
721 for (k = 0; k < t->entries_per_page; k++)
722 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700723
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530724 v = vnet_classify_entry_at_index (t, save_v,
725 j * t->entries_per_page + k);
726
727 if (vnet_classify_entry_is_free (v))
728 {
729 if (verbose > 1)
730 s = format (s, " %d: empty\n",
731 j * t->entries_per_page + k);
732 continue;
733 }
734 if (verbose)
735 {
736 s = format (s, " %d: %U\n",
737 j * t->entries_per_page + k,
738 format_classify_entry, t, v);
739 }
740 active_elements++;
741 }
742 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700743 }
744
745 s = format (s, " %lld active elements\n", active_elements);
746 s = format (s, " %d free lists\n", vec_len (t->freelists));
Dave Barachcada2a02017-05-18 19:16:47 -0400747 s = format (s, " %d linear-search buckets\n", t->linear_buckets);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700748 return s;
749}
750
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530751int
752vnet_classify_add_del_table (vnet_classify_main_t * cm,
753 u8 * mask,
754 u32 nbuckets,
755 u32 memory_size,
756 u32 skip,
757 u32 match,
758 u32 next_table_index,
759 u32 miss_next_index,
760 u32 * table_index,
761 u8 current_data_flag,
762 i16 current_data_offset,
763 int is_add, int del_chain)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700764{
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530765 vnet_classify_table_t *t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700766
767 if (is_add)
768 {
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530769 if (*table_index == ~0) /* add */
770 {
771 if (memory_size == 0)
772 return VNET_API_ERROR_INVALID_MEMORY_SIZE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700773
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530774 if (nbuckets == 0)
775 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700776
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530777 t = vnet_classify_new_table (cm, mask, nbuckets, memory_size,
778 skip, match);
779 t->next_table_index = next_table_index;
780 t->miss_next_index = miss_next_index;
781 t->current_data_flag = current_data_flag;
782 t->current_data_offset = current_data_offset;
783 *table_index = t - cm->tables;
784 }
785 else /* update */
786 {
787 vnet_classify_main_t *cm = &vnet_classify_main;
788 t = pool_elt_at_index (cm->tables, *table_index);
Steve Shin25e26dc2016-11-08 10:47:10 -0800789
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530790 t->next_table_index = next_table_index;
791 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700792 return 0;
793 }
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530794
Juraj Sloboda288e8932016-12-06 21:25:19 +0100795 vnet_classify_delete_table_index (cm, *table_index, del_chain);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700796 return 0;
797}
798
Juraj Sloboda51ffa812016-08-07 23:46:45 -0700799#define foreach_tcp_proto_field \
Dave Barach68b0fb02017-02-28 15:15:56 -0500800_(src) \
801_(dst)
Juraj Sloboda51ffa812016-08-07 23:46:45 -0700802
803#define foreach_udp_proto_field \
804_(src_port) \
805_(dst_port)
806
Ed Warnickecb9cada2015-12-08 15:45:58 -0700807#define foreach_ip4_proto_field \
808_(src_address) \
809_(dst_address) \
810_(tos) \
811_(length) \
812_(fragment_id) \
813_(ttl) \
814_(protocol) \
815_(checksum)
816
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530817uword
818unformat_tcp_mask (unformat_input_t * input, va_list * args)
Juraj Sloboda51ffa812016-08-07 23:46:45 -0700819{
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530820 u8 **maskp = va_arg (*args, u8 **);
821 u8 *mask = 0;
Juraj Sloboda51ffa812016-08-07 23:46:45 -0700822 u8 found_something = 0;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530823 tcp_header_t *tcp;
Juraj Sloboda51ffa812016-08-07 23:46:45 -0700824
825#define _(a) u8 a=0;
826 foreach_tcp_proto_field;
827#undef _
828
829 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
830 {
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530831 if (0);
Juraj Sloboda51ffa812016-08-07 23:46:45 -0700832#define _(a) else if (unformat (input, #a)) a=1;
833 foreach_tcp_proto_field
834#undef _
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530835 else
836 break;
Juraj Sloboda51ffa812016-08-07 23:46:45 -0700837 }
838
839#define _(a) found_something += a;
840 foreach_tcp_proto_field;
841#undef _
842
843 if (found_something == 0)
844 return 0;
845
846 vec_validate (mask, sizeof (*tcp) - 1);
847
848 tcp = (tcp_header_t *) mask;
849
Dave Barachb7b92992018-10-17 10:38:51 -0400850#define _(a) if (a) clib_memset (&tcp->a, 0xff, sizeof (tcp->a));
Juraj Sloboda51ffa812016-08-07 23:46:45 -0700851 foreach_tcp_proto_field;
852#undef _
853
854 *maskp = mask;
855 return 1;
856}
857
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530858uword
859unformat_udp_mask (unformat_input_t * input, va_list * args)
Juraj Sloboda51ffa812016-08-07 23:46:45 -0700860{
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530861 u8 **maskp = va_arg (*args, u8 **);
862 u8 *mask = 0;
Juraj Sloboda51ffa812016-08-07 23:46:45 -0700863 u8 found_something = 0;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530864 udp_header_t *udp;
Juraj Sloboda51ffa812016-08-07 23:46:45 -0700865
866#define _(a) u8 a=0;
867 foreach_udp_proto_field;
868#undef _
869
870 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
871 {
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530872 if (0);
Juraj Sloboda51ffa812016-08-07 23:46:45 -0700873#define _(a) else if (unformat (input, #a)) a=1;
874 foreach_udp_proto_field
875#undef _
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530876 else
877 break;
Juraj Sloboda51ffa812016-08-07 23:46:45 -0700878 }
879
880#define _(a) found_something += a;
881 foreach_udp_proto_field;
882#undef _
883
884 if (found_something == 0)
885 return 0;
886
887 vec_validate (mask, sizeof (*udp) - 1);
888
889 udp = (udp_header_t *) mask;
890
Dave Barachb7b92992018-10-17 10:38:51 -0400891#define _(a) if (a) clib_memset (&udp->a, 0xff, sizeof (udp->a));
Juraj Sloboda51ffa812016-08-07 23:46:45 -0700892 foreach_udp_proto_field;
893#undef _
894
895 *maskp = mask;
896 return 1;
897}
898
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530899typedef struct
900{
Juraj Sloboda51ffa812016-08-07 23:46:45 -0700901 u16 src_port, dst_port;
902} tcpudp_header_t;
903
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530904uword
905unformat_l4_mask (unformat_input_t * input, va_list * args)
Juraj Sloboda51ffa812016-08-07 23:46:45 -0700906{
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530907 u8 **maskp = va_arg (*args, u8 **);
Juraj Sloboda51ffa812016-08-07 23:46:45 -0700908 u16 src_port = 0, dst_port = 0;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530909 tcpudp_header_t *tcpudp;
Juraj Sloboda51ffa812016-08-07 23:46:45 -0700910
911 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
912 {
913 if (unformat (input, "tcp %U", unformat_tcp_mask, maskp))
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530914 return 1;
Juraj Sloboda51ffa812016-08-07 23:46:45 -0700915 else if (unformat (input, "udp %U", unformat_udp_mask, maskp))
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530916 return 1;
Juraj Sloboda51ffa812016-08-07 23:46:45 -0700917 else if (unformat (input, "src_port"))
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530918 src_port = 0xFFFF;
Juraj Sloboda51ffa812016-08-07 23:46:45 -0700919 else if (unformat (input, "dst_port"))
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530920 dst_port = 0xFFFF;
Juraj Sloboda51ffa812016-08-07 23:46:45 -0700921 else
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530922 return 0;
Juraj Sloboda51ffa812016-08-07 23:46:45 -0700923 }
924
925 if (!src_port && !dst_port)
926 return 0;
927
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530928 u8 *mask = 0;
Juraj Sloboda51ffa812016-08-07 23:46:45 -0700929 vec_validate (mask, sizeof (tcpudp_header_t) - 1);
930
931 tcpudp = (tcpudp_header_t *) mask;
932 tcpudp->src_port = src_port;
933 tcpudp->dst_port = dst_port;
934
935 *maskp = mask;
936
937 return 1;
938}
939
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530940uword
941unformat_ip4_mask (unformat_input_t * input, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700942{
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530943 u8 **maskp = va_arg (*args, u8 **);
944 u8 *mask = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700945 u8 found_something = 0;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530946 ip4_header_t *ip;
947
Ed Warnickecb9cada2015-12-08 15:45:58 -0700948#define _(a) u8 a=0;
949 foreach_ip4_proto_field;
950#undef _
951 u8 version = 0;
952 u8 hdr_length = 0;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530953
954
955 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700956 {
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530957 if (unformat (input, "version"))
958 version = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700959 else if (unformat (input, "hdr_length"))
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530960 hdr_length = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700961 else if (unformat (input, "src"))
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530962 src_address = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700963 else if (unformat (input, "dst"))
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530964 dst_address = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700965 else if (unformat (input, "proto"))
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530966 protocol = 1;
967
Ed Warnickecb9cada2015-12-08 15:45:58 -0700968#define _(a) else if (unformat (input, #a)) a=1;
969 foreach_ip4_proto_field
970#undef _
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530971 else
972 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700973 }
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530974
Ed Warnickecb9cada2015-12-08 15:45:58 -0700975#define _(a) found_something += a;
976 foreach_ip4_proto_field;
977#undef _
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530978
Ed Warnickecb9cada2015-12-08 15:45:58 -0700979 if (found_something == 0)
980 return 0;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530981
Ed Warnickecb9cada2015-12-08 15:45:58 -0700982 vec_validate (mask, sizeof (*ip) - 1);
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530983
Ed Warnickecb9cada2015-12-08 15:45:58 -0700984 ip = (ip4_header_t *) mask;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530985
Dave Barachb7b92992018-10-17 10:38:51 -0400986#define _(a) if (a) clib_memset (&ip->a, 0xff, sizeof (ip->a));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700987 foreach_ip4_proto_field;
988#undef _
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530989
Ed Warnickecb9cada2015-12-08 15:45:58 -0700990 ip->ip_version_and_header_length = 0;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530991
Ed Warnickecb9cada2015-12-08 15:45:58 -0700992 if (version)
993 ip->ip_version_and_header_length |= 0xF0;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530994
Ed Warnickecb9cada2015-12-08 15:45:58 -0700995 if (hdr_length)
996 ip->ip_version_and_header_length |= 0x0F;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530997
Ed Warnickecb9cada2015-12-08 15:45:58 -0700998 *maskp = mask;
999 return 1;
1000}
1001
1002#define foreach_ip6_proto_field \
1003_(src_address) \
1004_(dst_address) \
1005_(payload_length) \
1006_(hop_limit) \
1007_(protocol)
1008
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301009uword
1010unformat_ip6_mask (unformat_input_t * input, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001011{
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301012 u8 **maskp = va_arg (*args, u8 **);
1013 u8 *mask = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001014 u8 found_something = 0;
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301015 ip6_header_t *ip;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001016 u32 ip_version_traffic_class_and_flow_label;
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301017
Ed Warnickecb9cada2015-12-08 15:45:58 -07001018#define _(a) u8 a=0;
1019 foreach_ip6_proto_field;
1020#undef _
1021 u8 version = 0;
1022 u8 traffic_class = 0;
1023 u8 flow_label = 0;
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301024
1025 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001026 {
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301027 if (unformat (input, "version"))
1028 version = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001029 else if (unformat (input, "traffic-class"))
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301030 traffic_class = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001031 else if (unformat (input, "flow-label"))
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301032 flow_label = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001033 else if (unformat (input, "src"))
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301034 src_address = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001035 else if (unformat (input, "dst"))
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301036 dst_address = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001037 else if (unformat (input, "proto"))
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301038 protocol = 1;
1039
Ed Warnickecb9cada2015-12-08 15:45:58 -07001040#define _(a) else if (unformat (input, #a)) a=1;
1041 foreach_ip6_proto_field
1042#undef _
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301043 else
1044 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001045 }
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301046
Ed Warnickecb9cada2015-12-08 15:45:58 -07001047#define _(a) found_something += a;
1048 foreach_ip6_proto_field;
1049#undef _
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301050
Ed Warnickecb9cada2015-12-08 15:45:58 -07001051 if (found_something == 0)
1052 return 0;
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301053
Ed Warnickecb9cada2015-12-08 15:45:58 -07001054 vec_validate (mask, sizeof (*ip) - 1);
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301055
Ed Warnickecb9cada2015-12-08 15:45:58 -07001056 ip = (ip6_header_t *) mask;
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301057
Dave Barachb7b92992018-10-17 10:38:51 -04001058#define _(a) if (a) clib_memset (&ip->a, 0xff, sizeof (ip->a));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001059 foreach_ip6_proto_field;
1060#undef _
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301061
Ed Warnickecb9cada2015-12-08 15:45:58 -07001062 ip_version_traffic_class_and_flow_label = 0;
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301063
Ed Warnickecb9cada2015-12-08 15:45:58 -07001064 if (version)
1065 ip_version_traffic_class_and_flow_label |= 0xF0000000;
1066
1067 if (traffic_class)
1068 ip_version_traffic_class_and_flow_label |= 0x0FF00000;
1069
1070 if (flow_label)
1071 ip_version_traffic_class_and_flow_label |= 0x000FFFFF;
1072
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301073 ip->ip_version_traffic_class_and_flow_label =
Ed Warnickecb9cada2015-12-08 15:45:58 -07001074 clib_host_to_net_u32 (ip_version_traffic_class_and_flow_label);
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301075
Ed Warnickecb9cada2015-12-08 15:45:58 -07001076 *maskp = mask;
1077 return 1;
1078}
1079
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301080uword
1081unformat_l3_mask (unformat_input_t * input, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001082{
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301083 u8 **maskp = va_arg (*args, u8 **);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001084
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301085 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1086 {
1087 if (unformat (input, "ip4 %U", unformat_ip4_mask, maskp))
1088 return 1;
1089 else if (unformat (input, "ip6 %U", unformat_ip6_mask, maskp))
1090 return 1;
1091 else
1092 break;
1093 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001094 return 0;
1095}
1096
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301097uword
1098unformat_l2_mask (unformat_input_t * input, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001099{
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301100 u8 **maskp = va_arg (*args, u8 **);
1101 u8 *mask = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001102 u8 src = 0;
1103 u8 dst = 0;
1104 u8 proto = 0;
1105 u8 tag1 = 0;
1106 u8 tag2 = 0;
1107 u8 ignore_tag1 = 0;
1108 u8 ignore_tag2 = 0;
1109 u8 cos1 = 0;
1110 u8 cos2 = 0;
1111 u8 dot1q = 0;
1112 u8 dot1ad = 0;
1113 int len = 14;
1114
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301115 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1116 {
1117 if (unformat (input, "src"))
1118 src = 1;
1119 else if (unformat (input, "dst"))
1120 dst = 1;
1121 else if (unformat (input, "proto"))
1122 proto = 1;
1123 else if (unformat (input, "tag1"))
1124 tag1 = 1;
1125 else if (unformat (input, "tag2"))
1126 tag2 = 1;
1127 else if (unformat (input, "ignore-tag1"))
1128 ignore_tag1 = 1;
1129 else if (unformat (input, "ignore-tag2"))
1130 ignore_tag2 = 1;
1131 else if (unformat (input, "cos1"))
1132 cos1 = 1;
1133 else if (unformat (input, "cos2"))
1134 cos2 = 1;
1135 else if (unformat (input, "dot1q"))
1136 dot1q = 1;
1137 else if (unformat (input, "dot1ad"))
1138 dot1ad = 1;
1139 else
1140 break;
1141 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001142 if ((src + dst + proto + tag1 + tag2 + dot1q + dot1ad +
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301143 ignore_tag1 + ignore_tag2 + cos1 + cos2) == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001144 return 0;
1145
1146 if (tag1 || ignore_tag1 || cos1 || dot1q)
1147 len = 18;
1148 if (tag2 || ignore_tag2 || cos2 || dot1ad)
1149 len = 22;
1150
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301151 vec_validate (mask, len - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001152
1153 if (dst)
Dave Barachb7b92992018-10-17 10:38:51 -04001154 clib_memset (mask, 0xff, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001155
1156 if (src)
Dave Barachb7b92992018-10-17 10:38:51 -04001157 clib_memset (mask + 6, 0xff, 6);
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301158
Ed Warnickecb9cada2015-12-08 15:45:58 -07001159 if (tag2 || dot1ad)
1160 {
1161 /* inner vlan tag */
1162 if (tag2)
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301163 {
1164 mask[19] = 0xff;
1165 mask[18] = 0x0f;
1166 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001167 if (cos2)
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301168 mask[18] |= 0xe0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001169 if (proto)
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301170 mask[21] = mask[20] = 0xff;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001171 if (tag1)
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301172 {
1173 mask[15] = 0xff;
1174 mask[14] = 0x0f;
1175 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001176 if (cos1)
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301177 mask[14] |= 0xe0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001178 *maskp = mask;
1179 return 1;
1180 }
1181 if (tag1 | dot1q)
1182 {
1183 if (tag1)
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301184 {
1185 mask[15] = 0xff;
1186 mask[14] = 0x0f;
1187 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001188 if (cos1)
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301189 mask[14] |= 0xe0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001190 if (proto)
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301191 mask[16] = mask[17] = 0xff;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001192 *maskp = mask;
1193 return 1;
1194 }
1195 if (cos2)
1196 mask[18] |= 0xe0;
1197 if (cos1)
1198 mask[14] |= 0xe0;
1199 if (proto)
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301200 mask[12] = mask[13] = 0xff;
1201
Ed Warnickecb9cada2015-12-08 15:45:58 -07001202 *maskp = mask;
1203 return 1;
1204}
1205
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301206uword
1207unformat_classify_mask (unformat_input_t * input, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001208{
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301209 u8 **maskp = va_arg (*args, u8 **);
1210 u32 *skipp = va_arg (*args, u32 *);
1211 u32 *matchp = va_arg (*args, u32 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001212 u32 match;
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301213 u8 *mask = 0;
1214 u8 *l2 = 0;
1215 u8 *l3 = 0;
1216 u8 *l4 = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001217 int i;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001218
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301219 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1220 {
1221 if (unformat (input, "hex %U", unformat_hex_string, &mask))
1222 ;
1223 else if (unformat (input, "l2 %U", unformat_l2_mask, &l2))
1224 ;
1225 else if (unformat (input, "l3 %U", unformat_l3_mask, &l3))
1226 ;
1227 else if (unformat (input, "l4 %U", unformat_l4_mask, &l4))
1228 ;
1229 else
1230 break;
1231 }
1232
1233 if (l4 && !l3)
1234 {
1235 vec_free (mask);
1236 vec_free (l2);
1237 vec_free (l4);
1238 return 0;
1239 }
Juraj Sloboda51ffa812016-08-07 23:46:45 -07001240
1241 if (mask || l2 || l3 || l4)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001242 {
Juraj Sloboda51ffa812016-08-07 23:46:45 -07001243 if (l2 || l3 || l4)
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301244 {
1245 /* "With a free Ethernet header in every package" */
1246 if (l2 == 0)
1247 vec_validate (l2, 13);
1248 mask = l2;
1249 if (l3)
1250 {
1251 vec_append (mask, l3);
1252 vec_free (l3);
1253 }
1254 if (l4)
1255 {
1256 vec_append (mask, l4);
1257 vec_free (l4);
1258 }
1259 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001260
1261 /* Scan forward looking for the first significant mask octet */
1262 for (i = 0; i < vec_len (mask); i++)
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301263 if (mask[i])
1264 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001265
1266 /* compute (skip, match) params */
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301267 *skipp = i / sizeof (u32x4);
1268 vec_delete (mask, *skipp * sizeof (u32x4), 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001269
1270 /* Pad mask to an even multiple of the vector size */
1271 while (vec_len (mask) % sizeof (u32x4))
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301272 vec_add1 (mask, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001273
1274 match = vec_len (mask) / sizeof (u32x4);
1275
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301276 for (i = match * sizeof (u32x4); i > 0; i -= sizeof (u32x4))
1277 {
1278 u64 *tmp = (u64 *) (mask + (i - sizeof (u32x4)));
1279 if (*tmp || *(tmp + 1))
1280 break;
1281 match--;
1282 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001283 if (match == 0)
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301284 clib_warning ("BUG: match 0");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001285
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301286 _vec_len (mask) = match * sizeof (u32x4);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001287
1288 *matchp = match;
1289 *maskp = mask;
1290
1291 return 1;
1292 }
1293
1294 return 0;
1295}
1296
Dave Barachb84a3e52016-08-30 17:01:52 -04001297#define foreach_l2_input_next \
Ed Warnickecb9cada2015-12-08 15:45:58 -07001298_(drop, DROP) \
1299_(ethernet, ETHERNET_INPUT) \
1300_(ip4, IP4_INPUT) \
1301_(ip6, IP6_INPUT) \
1302_(li, LI)
1303
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301304uword
1305unformat_l2_input_next_index (unformat_input_t * input, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001306{
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301307 vnet_classify_main_t *cm = &vnet_classify_main;
1308 u32 *miss_next_indexp = va_arg (*args, u32 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001309 u32 next_index = 0;
1310 u32 tmp;
Dave Barachf39ff742016-03-20 10:14:45 -04001311 int i;
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301312
Dave Barachf39ff742016-03-20 10:14:45 -04001313 /* First try registered unformat fns, allowing override... */
1314 for (i = 0; i < vec_len (cm->unformat_l2_next_index_fns); i++)
1315 {
1316 if (unformat (input, "%U", cm->unformat_l2_next_index_fns[i], &tmp))
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301317 {
1318 next_index = tmp;
1319 goto out;
1320 }
Dave Barachf39ff742016-03-20 10:14:45 -04001321 }
1322
Ed Warnickecb9cada2015-12-08 15:45:58 -07001323#define _(n,N) \
Dave Barachb84a3e52016-08-30 17:01:52 -04001324 if (unformat (input, #n)) { next_index = L2_INPUT_CLASSIFY_NEXT_##N; goto out;}
1325 foreach_l2_input_next;
1326#undef _
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301327
Dave Barachb84a3e52016-08-30 17:01:52 -04001328 if (unformat (input, "%d", &tmp))
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301329 {
1330 next_index = tmp;
1331 goto out;
Dave Barachb84a3e52016-08-30 17:01:52 -04001332 }
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301333
Dave Barachb84a3e52016-08-30 17:01:52 -04001334 return 0;
1335
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301336out:
Dave Barachb84a3e52016-08-30 17:01:52 -04001337 *miss_next_indexp = next_index;
1338 return 1;
1339}
1340
1341#define foreach_l2_output_next \
1342_(drop, DROP)
1343
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301344uword
1345unformat_l2_output_next_index (unformat_input_t * input, va_list * args)
Dave Barachb84a3e52016-08-30 17:01:52 -04001346{
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301347 vnet_classify_main_t *cm = &vnet_classify_main;
1348 u32 *miss_next_indexp = va_arg (*args, u32 *);
Dave Barachb84a3e52016-08-30 17:01:52 -04001349 u32 next_index = 0;
1350 u32 tmp;
1351 int i;
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301352
Dave Barachb84a3e52016-08-30 17:01:52 -04001353 /* First try registered unformat fns, allowing override... */
1354 for (i = 0; i < vec_len (cm->unformat_l2_next_index_fns); i++)
1355 {
1356 if (unformat (input, "%U", cm->unformat_l2_next_index_fns[i], &tmp))
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301357 {
1358 next_index = tmp;
1359 goto out;
1360 }
Dave Barachb84a3e52016-08-30 17:01:52 -04001361 }
1362
1363#define _(n,N) \
1364 if (unformat (input, #n)) { next_index = L2_OUTPUT_CLASSIFY_NEXT_##N; goto out;}
1365 foreach_l2_output_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001366#undef _
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301367
Ed Warnickecb9cada2015-12-08 15:45:58 -07001368 if (unformat (input, "%d", &tmp))
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301369 {
1370 next_index = tmp;
1371 goto out;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001372 }
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301373
Ed Warnickecb9cada2015-12-08 15:45:58 -07001374 return 0;
1375
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301376out:
Ed Warnickecb9cada2015-12-08 15:45:58 -07001377 *miss_next_indexp = next_index;
1378 return 1;
1379}
1380
1381#define foreach_ip_next \
Ed Warnickecb9cada2015-12-08 15:45:58 -07001382_(drop, DROP) \
Ed Warnickecb9cada2015-12-08 15:45:58 -07001383_(rewrite, REWRITE)
1384
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301385uword
1386unformat_ip_next_index (unformat_input_t * input, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001387{
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301388 u32 *miss_next_indexp = va_arg (*args, u32 *);
1389 vnet_classify_main_t *cm = &vnet_classify_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001390 u32 next_index = 0;
1391 u32 tmp;
Dave Barachf39ff742016-03-20 10:14:45 -04001392 int i;
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301393
Dave Barachf39ff742016-03-20 10:14:45 -04001394 /* First try registered unformat fns, allowing override... */
1395 for (i = 0; i < vec_len (cm->unformat_ip_next_index_fns); i++)
1396 {
1397 if (unformat (input, "%U", cm->unformat_ip_next_index_fns[i], &tmp))
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301398 {
1399 next_index = tmp;
1400 goto out;
1401 }
Dave Barachf39ff742016-03-20 10:14:45 -04001402 }
1403
Ed Warnickecb9cada2015-12-08 15:45:58 -07001404#define _(n,N) \
1405 if (unformat (input, #n)) { next_index = IP_LOOKUP_NEXT_##N; goto out;}
1406 foreach_ip_next;
1407#undef _
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301408
Ed Warnickecb9cada2015-12-08 15:45:58 -07001409 if (unformat (input, "%d", &tmp))
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301410 {
1411 next_index = tmp;
1412 goto out;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001413 }
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301414
Ed Warnickecb9cada2015-12-08 15:45:58 -07001415 return 0;
1416
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301417out:
Ed Warnickecb9cada2015-12-08 15:45:58 -07001418 *miss_next_indexp = next_index;
1419 return 1;
1420}
1421
1422#define foreach_acl_next \
1423_(deny, DENY)
1424
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301425uword
1426unformat_acl_next_index (unformat_input_t * input, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001427{
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301428 u32 *next_indexp = va_arg (*args, u32 *);
1429 vnet_classify_main_t *cm = &vnet_classify_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001430 u32 next_index = 0;
1431 u32 tmp;
Dave Barachf39ff742016-03-20 10:14:45 -04001432 int i;
1433
1434 /* First try registered unformat fns, allowing override... */
1435 for (i = 0; i < vec_len (cm->unformat_acl_next_index_fns); i++)
1436 {
1437 if (unformat (input, "%U", cm->unformat_acl_next_index_fns[i], &tmp))
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301438 {
1439 next_index = tmp;
1440 goto out;
1441 }
Dave Barachf39ff742016-03-20 10:14:45 -04001442 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001443
1444#define _(n,N) \
1445 if (unformat (input, #n)) { next_index = ACL_NEXT_INDEX_##N; goto out;}
1446 foreach_acl_next;
1447#undef _
1448
1449 if (unformat (input, "permit"))
1450 {
1451 next_index = ~0;
1452 goto out;
1453 }
1454 else if (unformat (input, "%d", &tmp))
1455 {
1456 next_index = tmp;
1457 goto out;
1458 }
1459
1460 return 0;
1461
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301462out:
Dave Barachf39ff742016-03-20 10:14:45 -04001463 *next_indexp = next_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001464 return 1;
1465}
1466
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301467uword
1468unformat_policer_next_index (unformat_input_t * input, va_list * args)
Matus Fabian70e6a8d2016-06-20 08:10:42 -07001469{
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301470 u32 *next_indexp = va_arg (*args, u32 *);
1471 vnet_classify_main_t *cm = &vnet_classify_main;
Matus Fabian70e6a8d2016-06-20 08:10:42 -07001472 u32 next_index = 0;
1473 u32 tmp;
1474 int i;
1475
1476 /* First try registered unformat fns, allowing override... */
1477 for (i = 0; i < vec_len (cm->unformat_policer_next_index_fns); i++)
1478 {
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301479 if (unformat
1480 (input, "%U", cm->unformat_policer_next_index_fns[i], &tmp))
1481 {
1482 next_index = tmp;
1483 goto out;
1484 }
Matus Fabian70e6a8d2016-06-20 08:10:42 -07001485 }
1486
1487 if (unformat (input, "%d", &tmp))
1488 {
1489 next_index = tmp;
1490 goto out;
1491 }
1492
1493 return 0;
1494
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301495out:
Matus Fabian70e6a8d2016-06-20 08:10:42 -07001496 *next_indexp = next_index;
1497 return 1;
1498}
1499
Ed Warnickecb9cada2015-12-08 15:45:58 -07001500static clib_error_t *
1501classify_table_command_fn (vlib_main_t * vm,
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301502 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001503{
1504 u32 nbuckets = 2;
1505 u32 skip = ~0;
1506 u32 match = ~0;
1507 int is_add = 1;
Juraj Sloboda288e8932016-12-06 21:25:19 +01001508 int del_chain = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001509 u32 table_index = ~0;
1510 u32 next_table_index = ~0;
1511 u32 miss_next_index = ~0;
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301512 u32 memory_size = 2 << 20;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001513 u32 tmp;
Steve Shin25e26dc2016-11-08 10:47:10 -08001514 u32 current_data_flag = 0;
1515 int current_data_offset = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001516
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301517 u8 *mask = 0;
1518 vnet_classify_main_t *cm = &vnet_classify_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001519 int rv;
1520
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301521 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1522 {
1523 if (unformat (input, "del"))
Juraj Sloboda288e8932016-12-06 21:25:19 +01001524 is_add = 0;
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301525 else if (unformat (input, "del-chain"))
1526 {
1527 is_add = 0;
1528 del_chain = 1;
1529 }
1530 else if (unformat (input, "buckets %d", &nbuckets))
1531 ;
1532 else if (unformat (input, "skip %d", &skip))
1533 ;
1534 else if (unformat (input, "match %d", &match))
1535 ;
1536 else if (unformat (input, "table %d", &table_index))
1537 ;
1538 else if (unformat (input, "mask %U", unformat_classify_mask,
1539 &mask, &skip, &match))
1540 ;
1541 else if (unformat (input, "memory-size %uM", &tmp))
1542 memory_size = tmp << 20;
1543 else if (unformat (input, "memory-size %uG", &tmp))
1544 memory_size = tmp << 30;
1545 else if (unformat (input, "next-table %d", &next_table_index))
1546 ;
1547 else if (unformat (input, "miss-next %U", unformat_ip_next_index,
1548 &miss_next_index))
1549 ;
1550 else
1551 if (unformat
1552 (input, "l2-input-miss-next %U", unformat_l2_input_next_index,
1553 &miss_next_index))
1554 ;
1555 else
1556 if (unformat
1557 (input, "l2-output-miss-next %U", unformat_l2_output_next_index,
1558 &miss_next_index))
1559 ;
1560 else if (unformat (input, "acl-miss-next %U", unformat_acl_next_index,
1561 &miss_next_index))
1562 ;
1563 else if (unformat (input, "current-data-flag %d", &current_data_flag))
1564 ;
1565 else
1566 if (unformat (input, "current-data-offset %d", &current_data_offset))
1567 ;
Steve Shin25e26dc2016-11-08 10:47:10 -08001568
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301569 else
1570 break;
1571 }
1572
Steve Shin25e26dc2016-11-08 10:47:10 -08001573 if (is_add && mask == 0 && table_index == ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001574 return clib_error_return (0, "Mask required");
1575
Steve Shin25e26dc2016-11-08 10:47:10 -08001576 if (is_add && skip == ~0 && table_index == ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001577 return clib_error_return (0, "skip count required");
1578
Steve Shin25e26dc2016-11-08 10:47:10 -08001579 if (is_add && match == ~0 && table_index == ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001580 return clib_error_return (0, "match count required");
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301581
Ed Warnickecb9cada2015-12-08 15:45:58 -07001582 if (!is_add && table_index == ~0)
1583 return clib_error_return (0, "table index required for delete");
1584
1585 rv = vnet_classify_add_del_table (cm, mask, nbuckets, memory_size,
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301586 skip, match, next_table_index,
1587 miss_next_index, &table_index,
1588 current_data_flag, current_data_offset,
1589 is_add, del_chain);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001590 switch (rv)
1591 {
1592 case 0:
1593 break;
1594
1595 default:
1596 return clib_error_return (0, "vnet_classify_add_del_table returned %d",
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301597 rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001598 }
1599 return 0;
1600}
1601
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301602/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001603VLIB_CLI_COMMAND (classify_table, static) = {
1604 .path = "classify table",
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301605 .short_help =
Ed Warnickecb9cada2015-12-08 15:45:58 -07001606 "classify table [miss-next|l2-miss_next|acl-miss-next <next_index>]"
Steve Shin25e26dc2016-11-08 10:47:10 -08001607 "\n mask <mask-value> buckets <nn> [skip <n>] [match <n>]"
Juraj Sloboda288e8932016-12-06 21:25:19 +01001608 "\n [current-data-flag <n>] [current-data-offset <n>] [table <n>]"
Hongjun Ni8184ebd2017-10-25 20:47:56 +08001609 "\n [memory-size <nn>[M][G]] [next-table <n>]"
Juraj Sloboda288e8932016-12-06 21:25:19 +01001610 "\n [del] [del-chain]",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001611 .function = classify_table_command_fn,
1612};
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301613/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001614
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301615static u8 *
1616format_vnet_classify_table (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001617{
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301618 vnet_classify_main_t *cm = va_arg (*args, vnet_classify_main_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001619 int verbose = va_arg (*args, int);
1620 u32 index = va_arg (*args, u32);
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301621 vnet_classify_table_t *t;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001622
1623 if (index == ~0)
1624 {
1625 s = format (s, "%10s%10s%10s%10s", "TableIdx", "Sessions", "NextTbl",
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301626 "NextNode", verbose ? "Details" : "");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001627 return s;
1628 }
1629
1630 t = pool_elt_at_index (cm->tables, index);
1631 s = format (s, "%10u%10d%10d%10d", index, t->active_elements,
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301632 t->next_table_index, t->miss_next_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001633
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301634 s = format (s, "\n Heap: %U", format_mheap, t->mheap, 0 /*verbose */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -07001635
Steve Shin25e26dc2016-11-08 10:47:10 -08001636 s = format (s, "\n nbuckets %d, skip %d match %d flag %d offset %d",
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301637 t->nbuckets, t->skip_n_vectors, t->match_n_vectors,
1638 t->current_data_flag, t->current_data_offset);
1639 s = format (s, "\n mask %U", format_hex_bytes, t->mask,
1640 t->match_n_vectors * sizeof (u32x4));
Dave Barachcada2a02017-05-18 19:16:47 -04001641 s = format (s, "\n linear-search buckets %d\n", t->linear_buckets);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001642
1643 if (verbose == 0)
1644 return s;
1645
1646 s = format (s, "\n%U", format_classify_table, t, verbose);
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301647
Ed Warnickecb9cada2015-12-08 15:45:58 -07001648 return s;
1649}
1650
1651static clib_error_t *
1652show_classify_tables_command_fn (vlib_main_t * vm,
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301653 unformat_input_t * input,
1654 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001655{
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301656 vnet_classify_main_t *cm = &vnet_classify_main;
1657 vnet_classify_table_t *t;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001658 u32 match_index = ~0;
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301659 u32 *indices = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001660 int verbose = 0;
1661 int i;
1662
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301663 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001664 {
1665 if (unformat (input, "index %d", &match_index))
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301666 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001667 else if (unformat (input, "verbose %d", &verbose))
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301668 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001669 else if (unformat (input, "verbose"))
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301670 verbose = 1;
1671 else
1672 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001673 }
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301674
1675 /* *INDENT-OFF* */
1676 pool_foreach (t, cm->tables,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001677 ({
1678 if (match_index == ~0 || (match_index == t - cm->tables))
1679 vec_add1 (indices, t - cm->tables);
1680 }));
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301681 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001682
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301683 if (vec_len (indices))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001684 {
1685 vlib_cli_output (vm, "%U", format_vnet_classify_table, cm, verbose,
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301686 ~0 /* hdr */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -07001687 for (i = 0; i < vec_len (indices); i++)
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301688 vlib_cli_output (vm, "%U", format_vnet_classify_table, cm,
1689 verbose, indices[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001690 }
1691 else
1692 vlib_cli_output (vm, "No classifier tables configured");
1693
1694 vec_free (indices);
1695
1696 return 0;
1697}
1698
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301699/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001700VLIB_CLI_COMMAND (show_classify_table_command, static) = {
1701 .path = "show classify tables",
1702 .short_help = "show classify tables [index <nn>]",
1703 .function = show_classify_tables_command_fn,
1704};
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301705/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001706
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301707uword
1708unformat_l4_match (unformat_input_t * input, va_list * args)
Juraj Sloboda51ffa812016-08-07 23:46:45 -07001709{
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301710 u8 **matchp = va_arg (*args, u8 **);
Juraj Sloboda51ffa812016-08-07 23:46:45 -07001711
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301712 u8 *proto_header = 0;
Juraj Sloboda51ffa812016-08-07 23:46:45 -07001713 int src_port = 0;
1714 int dst_port = 0;
1715
1716 tcpudp_header_t h;
1717
1718 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1719 {
1720 if (unformat (input, "src_port %d", &src_port))
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301721 ;
Juraj Sloboda51ffa812016-08-07 23:46:45 -07001722 else if (unformat (input, "dst_port %d", &dst_port))
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301723 ;
Juraj Sloboda51ffa812016-08-07 23:46:45 -07001724 else
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301725 return 0;
Juraj Sloboda51ffa812016-08-07 23:46:45 -07001726 }
1727
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301728 h.src_port = clib_host_to_net_u16 (src_port);
1729 h.dst_port = clib_host_to_net_u16 (dst_port);
1730 vec_validate (proto_header, sizeof (h) - 1);
1731 memcpy (proto_header, &h, sizeof (h));
Juraj Sloboda51ffa812016-08-07 23:46:45 -07001732
1733 *matchp = proto_header;
1734
1735 return 1;
1736}
1737
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301738uword
1739unformat_ip4_match (unformat_input_t * input, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001740{
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301741 u8 **matchp = va_arg (*args, u8 **);
1742 u8 *match = 0;
1743 ip4_header_t *ip;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001744 int version = 0;
1745 u32 version_val;
1746 int hdr_length = 0;
1747 u32 hdr_length_val;
1748 int src = 0, dst = 0;
1749 ip4_address_t src_val, dst_val;
1750 int proto = 0;
1751 u32 proto_val;
1752 int tos = 0;
1753 u32 tos_val;
1754 int length = 0;
1755 u32 length_val;
1756 int fragment_id = 0;
1757 u32 fragment_id_val;
1758 int ttl = 0;
1759 int ttl_val;
1760 int checksum = 0;
1761 u32 checksum_val;
1762
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301763 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001764 {
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301765 if (unformat (input, "version %d", &version_val))
1766 version = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001767 else if (unformat (input, "hdr_length %d", &hdr_length_val))
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301768 hdr_length = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001769 else if (unformat (input, "src %U", unformat_ip4_address, &src_val))
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301770 src = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001771 else if (unformat (input, "dst %U", unformat_ip4_address, &dst_val))
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301772 dst = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001773 else if (unformat (input, "proto %d", &proto_val))
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301774 proto = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001775 else if (unformat (input, "tos %d", &tos_val))
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301776 tos = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001777 else if (unformat (input, "length %d", &length_val))
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301778 length = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001779 else if (unformat (input, "fragment_id %d", &fragment_id_val))
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301780 fragment_id = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001781 else if (unformat (input, "ttl %d", &ttl_val))
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301782 ttl = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001783 else if (unformat (input, "checksum %d", &checksum_val))
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301784 checksum = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001785 else
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301786 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001787 }
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301788
Ed Warnickecb9cada2015-12-08 15:45:58 -07001789 if (version + hdr_length + src + dst + proto + tos + length + fragment_id
1790 + ttl + checksum == 0)
1791 return 0;
1792
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301793 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -07001794 * Aligned because we use the real comparison functions
1795 */
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301796 vec_validate_aligned (match, sizeof (*ip) - 1, sizeof (u32x4));
1797
Ed Warnickecb9cada2015-12-08 15:45:58 -07001798 ip = (ip4_header_t *) match;
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301799
Ed Warnickecb9cada2015-12-08 15:45:58 -07001800 /* These are realistically matched in practice */
1801 if (src)
1802 ip->src_address.as_u32 = src_val.as_u32;
1803
1804 if (dst)
1805 ip->dst_address.as_u32 = dst_val.as_u32;
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301806
Ed Warnickecb9cada2015-12-08 15:45:58 -07001807 if (proto)
1808 ip->protocol = proto_val;
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301809
Ed Warnickecb9cada2015-12-08 15:45:58 -07001810
1811 /* These are not, but they're included for completeness */
1812 if (version)
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301813 ip->ip_version_and_header_length |= (version_val & 0xF) << 4;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001814
1815 if (hdr_length)
1816 ip->ip_version_and_header_length |= (hdr_length_val & 0xF);
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301817
Ed Warnickecb9cada2015-12-08 15:45:58 -07001818 if (tos)
1819 ip->tos = tos_val;
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301820
Ed Warnickecb9cada2015-12-08 15:45:58 -07001821 if (length)
Juraj Sloboda51ffa812016-08-07 23:46:45 -07001822 ip->length = clib_host_to_net_u16 (length_val);
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301823
Ed Warnickecb9cada2015-12-08 15:45:58 -07001824 if (ttl)
1825 ip->ttl = ttl_val;
1826
1827 if (checksum)
Juraj Sloboda51ffa812016-08-07 23:46:45 -07001828 ip->checksum = clib_host_to_net_u16 (checksum_val);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001829
1830 *matchp = match;
1831 return 1;
1832}
1833
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301834uword
1835unformat_ip6_match (unformat_input_t * input, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001836{
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301837 u8 **matchp = va_arg (*args, u8 **);
1838 u8 *match = 0;
1839 ip6_header_t *ip;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001840 int version = 0;
1841 u32 version_val;
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301842 u8 traffic_class = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001843 u32 traffic_class_val;
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301844 u8 flow_label = 0;
1845 u8 flow_label_val;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001846 int src = 0, dst = 0;
1847 ip6_address_t src_val, dst_val;
1848 int proto = 0;
1849 u32 proto_val;
1850 int payload_length = 0;
1851 u32 payload_length_val;
1852 int hop_limit = 0;
1853 int hop_limit_val;
1854 u32 ip_version_traffic_class_and_flow_label;
1855
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301856 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001857 {
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301858 if (unformat (input, "version %d", &version_val))
1859 version = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001860 else if (unformat (input, "traffic_class %d", &traffic_class_val))
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301861 traffic_class = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001862 else if (unformat (input, "flow_label %d", &flow_label_val))
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301863 flow_label = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001864 else if (unformat (input, "src %U", unformat_ip6_address, &src_val))
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301865 src = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001866 else if (unformat (input, "dst %U", unformat_ip6_address, &dst_val))
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301867 dst = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001868 else if (unformat (input, "proto %d", &proto_val))
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301869 proto = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001870 else if (unformat (input, "payload_length %d", &payload_length_val))
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301871 payload_length = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001872 else if (unformat (input, "hop_limit %d", &hop_limit_val))
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301873 hop_limit = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001874 else
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301875 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001876 }
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301877
Ed Warnickecb9cada2015-12-08 15:45:58 -07001878 if (version + traffic_class + flow_label + src + dst + proto +
1879 payload_length + hop_limit == 0)
1880 return 0;
1881
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301882 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -07001883 * Aligned because we use the real comparison functions
1884 */
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301885 vec_validate_aligned (match, sizeof (*ip) - 1, sizeof (u32x4));
1886
Ed Warnickecb9cada2015-12-08 15:45:58 -07001887 ip = (ip6_header_t *) match;
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301888
Ed Warnickecb9cada2015-12-08 15:45:58 -07001889 if (src)
Dave Barach178cf492018-11-13 16:34:13 -05001890 clib_memcpy_fast (&ip->src_address, &src_val, sizeof (ip->src_address));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001891
1892 if (dst)
Dave Barach178cf492018-11-13 16:34:13 -05001893 clib_memcpy_fast (&ip->dst_address, &dst_val, sizeof (ip->dst_address));
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301894
Ed Warnickecb9cada2015-12-08 15:45:58 -07001895 if (proto)
1896 ip->protocol = proto_val;
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301897
Ed Warnickecb9cada2015-12-08 15:45:58 -07001898 ip_version_traffic_class_and_flow_label = 0;
1899
1900 if (version)
1901 ip_version_traffic_class_and_flow_label |= (version_val & 0xF) << 28;
1902
1903 if (traffic_class)
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301904 ip_version_traffic_class_and_flow_label |=
1905 (traffic_class_val & 0xFF) << 20;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001906
1907 if (flow_label)
1908 ip_version_traffic_class_and_flow_label |= (flow_label_val & 0xFFFFF);
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301909
1910 ip->ip_version_traffic_class_and_flow_label =
Ed Warnickecb9cada2015-12-08 15:45:58 -07001911 clib_host_to_net_u32 (ip_version_traffic_class_and_flow_label);
1912
1913 if (payload_length)
1914 ip->payload_length = clib_host_to_net_u16 (payload_length_val);
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301915
Ed Warnickecb9cada2015-12-08 15:45:58 -07001916 if (hop_limit)
1917 ip->hop_limit = hop_limit_val;
1918
1919 *matchp = match;
1920 return 1;
1921}
1922
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301923uword
1924unformat_l3_match (unformat_input_t * input, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001925{
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301926 u8 **matchp = va_arg (*args, u8 **);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001927
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301928 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1929 {
1930 if (unformat (input, "ip4 %U", unformat_ip4_match, matchp))
1931 return 1;
1932 else if (unformat (input, "ip6 %U", unformat_ip6_match, matchp))
1933 return 1;
1934 /* $$$$ add mpls */
1935 else
1936 break;
1937 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001938 return 0;
1939}
1940
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301941uword
1942unformat_vlan_tag (unformat_input_t * input, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001943{
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301944 u8 *tagp = va_arg (*args, u8 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001945 u32 tag;
1946
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301947 if (unformat (input, "%d", &tag))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001948 {
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301949 tagp[0] = (tag >> 8) & 0x0F;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001950 tagp[1] = tag & 0xFF;
1951 return 1;
1952 }
1953
1954 return 0;
1955}
1956
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301957uword
1958unformat_l2_match (unformat_input_t * input, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001959{
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301960 u8 **matchp = va_arg (*args, u8 **);
1961 u8 *match = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001962 u8 src = 0;
1963 u8 src_val[6];
1964 u8 dst = 0;
1965 u8 dst_val[6];
1966 u8 proto = 0;
1967 u16 proto_val;
1968 u8 tag1 = 0;
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301969 u8 tag1_val[2];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001970 u8 tag2 = 0;
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301971 u8 tag2_val[2];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001972 int len = 14;
1973 u8 ignore_tag1 = 0;
1974 u8 ignore_tag2 = 0;
1975 u8 cos1 = 0;
1976 u8 cos2 = 0;
1977 u32 cos1_val = 0;
1978 u32 cos2_val = 0;
1979
khemendra kumard7bfa0e2017-11-27 15:15:53 +05301980 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1981 {
1982 if (unformat (input, "src %U", unformat_ethernet_address, &src_val))
1983 src = 1;
1984 else
1985 if (unformat (input, "dst %U", unformat_ethernet_address, &dst_val))
1986 dst = 1;
1987 else if (unformat (input, "proto %U",
1988 unformat_ethernet_type_host_byte_order, &proto_val))
1989 proto = 1;
1990 else if (unformat (input, "tag1 %U", unformat_vlan_tag, tag1_val))
1991 tag1 = 1;
1992 else if (unformat (input, "tag2 %U", unformat_vlan_tag, tag2_val))
1993 tag2 = 1;
1994 else if (unformat (input, "ignore-tag1"))
1995 ignore_tag1 = 1;
1996 else if (unformat (input, "ignore-tag2"))
1997 ignore_tag2 = 1;
1998 else if (unformat (input, "cos1 %d", &cos1_val))
1999 cos1 = 1;
2000 else if (unformat (input, "cos2 %d", &cos2_val))
2001 cos2 = 1;
2002 else
2003 break;
2004 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002005 if ((src + dst + proto + tag1 + tag2 +
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302006 ignore_tag1 + ignore_tag2 + cos1 + cos2) == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002007 return 0;
2008
2009 if (tag1 || ignore_tag1 || cos1)
2010 len = 18;
2011 if (tag2 || ignore_tag2 || cos2)
2012 len = 22;
2013
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302014 vec_validate_aligned (match, len - 1, sizeof (u32x4));
Ed Warnickecb9cada2015-12-08 15:45:58 -07002015
2016 if (dst)
Dave Barach178cf492018-11-13 16:34:13 -05002017 clib_memcpy_fast (match, dst_val, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002018
2019 if (src)
Dave Barach178cf492018-11-13 16:34:13 -05002020 clib_memcpy_fast (match + 6, src_val, 6);
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302021
Ed Warnickecb9cada2015-12-08 15:45:58 -07002022 if (tag2)
2023 {
2024 /* inner vlan tag */
2025 match[19] = tag2_val[1];
2026 match[18] = tag2_val[0];
2027 if (cos2)
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302028 match[18] |= (cos2_val & 0x7) << 5;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002029 if (proto)
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302030 {
2031 match[21] = proto_val & 0xff;
2032 match[20] = proto_val >> 8;
2033 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002034 if (tag1)
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302035 {
2036 match[15] = tag1_val[1];
2037 match[14] = tag1_val[0];
2038 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002039 if (cos1)
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302040 match[14] |= (cos1_val & 0x7) << 5;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002041 *matchp = match;
2042 return 1;
2043 }
2044 if (tag1)
2045 {
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302046 match[15] = tag1_val[1];
2047 match[14] = tag1_val[0];
Ed Warnickecb9cada2015-12-08 15:45:58 -07002048 if (proto)
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302049 {
2050 match[17] = proto_val & 0xff;
2051 match[16] = proto_val >> 8;
2052 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002053 if (cos1)
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302054 match[14] |= (cos1_val & 0x7) << 5;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002055
2056 *matchp = match;
2057 return 1;
2058 }
2059 if (cos2)
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302060 match[18] |= (cos2_val & 0x7) << 5;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002061 if (cos1)
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302062 match[14] |= (cos1_val & 0x7) << 5;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002063 if (proto)
2064 {
2065 match[13] = proto_val & 0xff;
2066 match[12] = proto_val >> 8;
2067 }
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302068
Ed Warnickecb9cada2015-12-08 15:45:58 -07002069 *matchp = match;
2070 return 1;
2071}
2072
2073
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302074uword
2075unformat_classify_match (unformat_input_t * input, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002076{
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302077 vnet_classify_main_t *cm = va_arg (*args, vnet_classify_main_t *);
2078 u8 **matchp = va_arg (*args, u8 **);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002079 u32 table_index = va_arg (*args, u32);
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302080 vnet_classify_table_t *t;
2081
2082 u8 *match = 0;
2083 u8 *l2 = 0;
2084 u8 *l3 = 0;
2085 u8 *l4 = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002086
2087 if (pool_is_free_index (cm->tables, table_index))
2088 return 0;
2089
2090 t = pool_elt_at_index (cm->tables, table_index);
2091
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302092 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2093 {
2094 if (unformat (input, "hex %U", unformat_hex_string, &match))
2095 ;
2096 else if (unformat (input, "l2 %U", unformat_l2_match, &l2))
2097 ;
2098 else if (unformat (input, "l3 %U", unformat_l3_match, &l3))
2099 ;
2100 else if (unformat (input, "l4 %U", unformat_l4_match, &l4))
2101 ;
2102 else
2103 break;
2104 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002105
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302106 if (l4 && !l3)
2107 {
2108 vec_free (match);
2109 vec_free (l2);
2110 vec_free (l4);
2111 return 0;
2112 }
Juraj Sloboda51ffa812016-08-07 23:46:45 -07002113
2114 if (match || l2 || l3 || l4)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002115 {
Juraj Sloboda51ffa812016-08-07 23:46:45 -07002116 if (l2 || l3 || l4)
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302117 {
2118 /* "Win a free Ethernet header in every packet" */
2119 if (l2 == 0)
2120 vec_validate_aligned (l2, 13, sizeof (u32x4));
2121 match = l2;
2122 if (l3)
2123 {
2124 vec_append_aligned (match, l3, sizeof (u32x4));
2125 vec_free (l3);
2126 }
2127 if (l4)
2128 {
2129 vec_append_aligned (match, l4, sizeof (u32x4));
2130 vec_free (l4);
2131 }
2132 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002133
2134 /* Make sure the vector is big enough even if key is all 0's */
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302135 vec_validate_aligned
2136 (match,
2137 ((t->match_n_vectors + t->skip_n_vectors) * sizeof (u32x4)) - 1,
2138 sizeof (u32x4));
2139
2140 /* Set size, include skipped vectors */
2141 _vec_len (match) =
2142 (t->match_n_vectors + t->skip_n_vectors) * sizeof (u32x4);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002143
2144 *matchp = match;
2145
2146 return 1;
2147 }
2148
2149 return 0;
2150}
2151
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302152int
2153vnet_classify_add_del_session (vnet_classify_main_t * cm,
2154 u32 table_index,
2155 u8 * match,
2156 u32 hit_next_index,
2157 u32 opaque_index,
2158 i32 advance,
2159 u8 action, u32 metadata, int is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002160{
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302161 vnet_classify_table_t *t;
2162 vnet_classify_entry_5_t _max_e __attribute__ ((aligned (16)));
2163 vnet_classify_entry_t *e;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002164 int i, rv;
2165
2166 if (pool_is_free_index (cm->tables, table_index))
2167 return VNET_API_ERROR_NO_SUCH_TABLE;
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302168
Ed Warnickecb9cada2015-12-08 15:45:58 -07002169 t = pool_elt_at_index (cm->tables, table_index);
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302170
2171 e = (vnet_classify_entry_t *) & _max_e;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002172 e->next_index = hit_next_index;
2173 e->opaque_index = opaque_index;
2174 e->advance = advance;
2175 e->hits = 0;
2176 e->last_heard = 0;
2177 e->flags = 0;
Steve Shin25e26dc2016-11-08 10:47:10 -08002178 e->action = action;
2179 if (e->action == CLASSIFY_ACTION_SET_IP4_FIB_INDEX)
Neale Ranns15002542017-09-10 04:39:11 -07002180 e->metadata = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302181 metadata,
2182 FIB_SOURCE_CLASSIFY);
Steve Shin25e26dc2016-11-08 10:47:10 -08002183 else if (e->action == CLASSIFY_ACTION_SET_IP6_FIB_INDEX)
Neale Ranns15002542017-09-10 04:39:11 -07002184 e->metadata = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6,
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302185 metadata,
2186 FIB_SOURCE_CLASSIFY);
Dave Barach630a8e22017-11-18 06:58:34 -05002187 else if (e->action == CLASSIFY_ACTION_SET_METADATA)
Gabriel Ganne8527f122017-10-02 11:41:24 +02002188 e->metadata = metadata;
Dave Barachcada2a02017-05-18 19:16:47 -04002189 else
2190 e->metadata = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002191
2192 /* Copy key data, honoring skip_n_vectors */
Dave Barach178cf492018-11-13 16:34:13 -05002193 clib_memcpy_fast (&e->key, match + t->skip_n_vectors * sizeof (u32x4),
2194 t->match_n_vectors * sizeof (u32x4));
Ed Warnickecb9cada2015-12-08 15:45:58 -07002195
2196 /* Clear don't-care bits; likely when dynamically creating sessions */
2197 for (i = 0; i < t->match_n_vectors; i++)
2198 e->key[i] &= t->mask[i];
2199
2200 rv = vnet_classify_add_del (t, e, is_add);
Neale Ranns13eaf3e2017-05-23 06:10:33 -07002201
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302202 vnet_classify_entry_release_resource (e);
Neale Ranns13eaf3e2017-05-23 06:10:33 -07002203
Ed Warnickecb9cada2015-12-08 15:45:58 -07002204 if (rv)
2205 return VNET_API_ERROR_NO_SUCH_ENTRY;
2206 return 0;
2207}
2208
2209static clib_error_t *
2210classify_session_command_fn (vlib_main_t * vm,
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302211 unformat_input_t * input,
2212 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002213{
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302214 vnet_classify_main_t *cm = &vnet_classify_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002215 int is_add = 1;
2216 u32 table_index = ~0;
2217 u32 hit_next_index = ~0;
Dave Barachf39ff742016-03-20 10:14:45 -04002218 u64 opaque_index = ~0;
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302219 u8 *match = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002220 i32 advance = 0;
Steve Shin25e26dc2016-11-08 10:47:10 -08002221 u32 action = 0;
2222 u32 metadata = 0;
Dave Barachf39ff742016-03-20 10:14:45 -04002223 int i, rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002224
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302225 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002226 {
2227 if (unformat (input, "del"))
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302228 is_add = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002229 else if (unformat (input, "hit-next %U", unformat_ip_next_index,
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302230 &hit_next_index))
2231 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002232 else
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302233 if (unformat
2234 (input, "l2-input-hit-next %U", unformat_l2_input_next_index,
2235 &hit_next_index))
2236 ;
2237 else
2238 if (unformat
2239 (input, "l2-output-hit-next %U", unformat_l2_output_next_index,
2240 &hit_next_index))
2241 ;
2242 else if (unformat (input, "acl-hit-next %U", unformat_acl_next_index,
2243 &hit_next_index))
2244 ;
2245 else if (unformat (input, "policer-hit-next %U",
2246 unformat_policer_next_index, &hit_next_index))
2247 ;
2248 else if (unformat (input, "opaque-index %lld", &opaque_index))
2249 ;
2250 else if (unformat (input, "match %U", unformat_classify_match,
2251 cm, &match, table_index))
2252 ;
2253 else if (unformat (input, "advance %d", &advance))
2254 ;
2255 else if (unformat (input, "table-index %d", &table_index))
2256 ;
2257 else if (unformat (input, "action set-ip4-fib-id %d", &metadata))
2258 action = 1;
2259 else if (unformat (input, "action set-ip6-fib-id %d", &metadata))
2260 action = 2;
2261 else if (unformat (input, "action set-sr-policy-index %d", &metadata))
2262 action = 3;
2263 else
2264 {
2265 /* Try registered opaque-index unformat fns */
2266 for (i = 0; i < vec_len (cm->unformat_opaque_index_fns); i++)
2267 {
2268 if (unformat (input, "%U", cm->unformat_opaque_index_fns[i],
2269 &opaque_index))
2270 goto found_opaque;
2271 }
2272 break;
2273 }
Dave Barachf39ff742016-03-20 10:14:45 -04002274 found_opaque:
2275 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002276 }
2277
2278 if (table_index == ~0)
2279 return clib_error_return (0, "Table index required");
2280
2281 if (is_add && match == 0)
2282 return clib_error_return (0, "Match value required");
2283
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302284 rv = vnet_classify_add_del_session (cm, table_index, match,
2285 hit_next_index,
2286 opaque_index, advance,
2287 action, metadata, is_add);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002288
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302289 switch (rv)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002290 {
2291 case 0:
2292 break;
2293
2294 default:
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302295 return clib_error_return (0,
2296 "vnet_classify_add_del_session returned %d",
2297 rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002298 }
2299
2300 return 0;
2301}
2302
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302303/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002304VLIB_CLI_COMMAND (classify_session_command, static) = {
2305 .path = "classify session",
Ole Troan1e66d5c2016-09-30 09:22:36 +02002306 .short_help =
jackiechen1985e91e6de2018-12-14 01:43:21 +08002307 "classify session [hit-next|l2-input-hit-next|l2-output-hit-next|"
Ole Troan1e66d5c2016-09-30 09:22:36 +02002308 "acl-hit-next <next_index>|policer-hit-next <policer_name>]"
Steve Shin25e26dc2016-11-08 10:47:10 -08002309 "\n table-index <nn> match [hex] [l2] [l3 ip4] [opaque-index <index>]"
Gabriel Ganne8527f122017-10-02 11:41:24 +02002310 "\n [action set-ip4-fib-id|set-ip6-fib-id|set-sr-policy-index <n>] [del]",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002311 .function = classify_session_command_fn,
2312};
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302313/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002314
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302315static uword
Dave Barachf39ff742016-03-20 10:14:45 -04002316unformat_opaque_sw_if_index (unformat_input_t * input, va_list * args)
2317{
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302318 u64 *opaquep = va_arg (*args, u64 *);
Dave Barachf39ff742016-03-20 10:14:45 -04002319 u32 sw_if_index;
2320
2321 if (unformat (input, "opaque-sw_if_index %U", unformat_vnet_sw_interface,
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302322 vnet_get_main (), &sw_if_index))
Dave Barachf39ff742016-03-20 10:14:45 -04002323 {
2324 *opaquep = sw_if_index;
2325 return 1;
2326 }
2327 return 0;
2328}
2329
Ole Troan1e66d5c2016-09-30 09:22:36 +02002330static uword
Dave Barachf39ff742016-03-20 10:14:45 -04002331unformat_ip_next_node (unformat_input_t * input, va_list * args)
2332{
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302333 vnet_classify_main_t *cm = &vnet_classify_main;
2334 u32 *next_indexp = va_arg (*args, u32 *);
Dave Barachf39ff742016-03-20 10:14:45 -04002335 u32 node_index;
Ole Troan1e66d5c2016-09-30 09:22:36 +02002336 u32 next_index = ~0;
Dave Barachf39ff742016-03-20 10:14:45 -04002337
Ole Troan1e66d5c2016-09-30 09:22:36 +02002338 if (unformat (input, "ip6-node %U", unformat_vlib_node,
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302339 cm->vlib_main, &node_index))
Dave Barachf39ff742016-03-20 10:14:45 -04002340 {
Ole Troan1e66d5c2016-09-30 09:22:36 +02002341 next_index = vlib_node_add_next (cm->vlib_main,
2342 ip6_classify_node.index, node_index);
Dave Barachf39ff742016-03-20 10:14:45 -04002343 }
Ole Troan1e66d5c2016-09-30 09:22:36 +02002344 else if (unformat (input, "ip4-node %U", unformat_vlib_node,
2345 cm->vlib_main, &node_index))
2346 {
2347 next_index = vlib_node_add_next (cm->vlib_main,
2348 ip4_classify_node.index, node_index);
2349 }
2350 else
2351 return 0;
2352
2353 *next_indexp = next_index;
2354 return 1;
Dave Barachf39ff742016-03-20 10:14:45 -04002355}
2356
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302357static uword
Dave Barachf39ff742016-03-20 10:14:45 -04002358unformat_acl_next_node (unformat_input_t * input, va_list * args)
2359{
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302360 vnet_classify_main_t *cm = &vnet_classify_main;
2361 u32 *next_indexp = va_arg (*args, u32 *);
Dave Barachf39ff742016-03-20 10:14:45 -04002362 u32 node_index;
Ole Troan1e66d5c2016-09-30 09:22:36 +02002363 u32 next_index;
Dave Barachf39ff742016-03-20 10:14:45 -04002364
Ole Troan1e66d5c2016-09-30 09:22:36 +02002365 if (unformat (input, "ip6-node %U", unformat_vlib_node,
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302366 cm->vlib_main, &node_index))
Dave Barachf39ff742016-03-20 10:14:45 -04002367 {
Ole Troan1e66d5c2016-09-30 09:22:36 +02002368 next_index = vlib_node_add_next (cm->vlib_main,
2369 ip6_inacl_node.index, node_index);
Dave Barachf39ff742016-03-20 10:14:45 -04002370 }
Ole Troan1e66d5c2016-09-30 09:22:36 +02002371 else if (unformat (input, "ip4-node %U", unformat_vlib_node,
2372 cm->vlib_main, &node_index))
2373 {
2374 next_index = vlib_node_add_next (cm->vlib_main,
2375 ip4_inacl_node.index, node_index);
2376 }
2377 else
2378 return 0;
2379
2380 *next_indexp = next_index;
2381 return 1;
Dave Barachf39ff742016-03-20 10:14:45 -04002382}
2383
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302384static uword
Dave Barachb84a3e52016-08-30 17:01:52 -04002385unformat_l2_input_next_node (unformat_input_t * input, va_list * args)
Dave Barachf39ff742016-03-20 10:14:45 -04002386{
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302387 vnet_classify_main_t *cm = &vnet_classify_main;
2388 u32 *next_indexp = va_arg (*args, u32 *);
Dave Barachf39ff742016-03-20 10:14:45 -04002389 u32 node_index;
2390 u32 next_index;
2391
Dave Barachb84a3e52016-08-30 17:01:52 -04002392 if (unformat (input, "input-node %U", unformat_vlib_node,
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302393 cm->vlib_main, &node_index))
Dave Barachf39ff742016-03-20 10:14:45 -04002394 {
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302395 next_index = vlib_node_add_next
2396 (cm->vlib_main, l2_input_classify_node.index, node_index);
Dave Barachf39ff742016-03-20 10:14:45 -04002397
2398 *next_indexp = next_index;
2399 return 1;
2400 }
2401 return 0;
2402}
2403
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302404static uword
Dave Barachb84a3e52016-08-30 17:01:52 -04002405unformat_l2_output_next_node (unformat_input_t * input, va_list * args)
2406{
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302407 vnet_classify_main_t *cm = &vnet_classify_main;
2408 u32 *next_indexp = va_arg (*args, u32 *);
Dave Barachb84a3e52016-08-30 17:01:52 -04002409 u32 node_index;
2410 u32 next_index;
2411
2412 if (unformat (input, "output-node %U", unformat_vlib_node,
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302413 cm->vlib_main, &node_index))
Dave Barachb84a3e52016-08-30 17:01:52 -04002414 {
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302415 next_index = vlib_node_add_next
2416 (cm->vlib_main, l2_output_classify_node.index, node_index);
Dave Barachb84a3e52016-08-30 17:01:52 -04002417
2418 *next_indexp = next_index;
2419 return 1;
2420 }
2421 return 0;
2422}
Dave Barachf39ff742016-03-20 10:14:45 -04002423
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302424static clib_error_t *
Dave Barachf39ff742016-03-20 10:14:45 -04002425vnet_classify_init (vlib_main_t * vm)
2426{
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302427 vnet_classify_main_t *cm = &vnet_classify_main;
Dave Barachf39ff742016-03-20 10:14:45 -04002428
2429 cm->vlib_main = vm;
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302430 cm->vnet_main = vnet_get_main ();
Dave Barachf39ff742016-03-20 10:14:45 -04002431
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302432 vnet_classify_register_unformat_opaque_index_fn
Dave Barachf39ff742016-03-20 10:14:45 -04002433 (unformat_opaque_sw_if_index);
2434
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302435 vnet_classify_register_unformat_ip_next_index_fn (unformat_ip_next_node);
Dave Barachf39ff742016-03-20 10:14:45 -04002436
2437 vnet_classify_register_unformat_l2_next_index_fn
Dave Barachb84a3e52016-08-30 17:01:52 -04002438 (unformat_l2_input_next_node);
2439
2440 vnet_classify_register_unformat_l2_next_index_fn
Dave Barachb84a3e52016-08-30 17:01:52 -04002441 (unformat_l2_output_next_node);
Dave Barachf39ff742016-03-20 10:14:45 -04002442
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302443 vnet_classify_register_unformat_acl_next_index_fn (unformat_acl_next_node);
Dave Barachf39ff742016-03-20 10:14:45 -04002444
2445 return 0;
2446}
2447
2448VLIB_INIT_FUNCTION (vnet_classify_init);
2449
Ed Warnickecb9cada2015-12-08 15:45:58 -07002450#define TEST_CODE 1
2451
2452#if TEST_CODE > 0
Dave Barachcada2a02017-05-18 19:16:47 -04002453
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302454typedef struct
Ed Warnickecb9cada2015-12-08 15:45:58 -07002455{
Dave Barachcada2a02017-05-18 19:16:47 -04002456 ip4_address_t addr;
2457 int in_table;
2458} test_entry_t;
2459
2460typedef struct
2461{
2462 test_entry_t *entries;
2463
2464 /* test parameters */
2465 u32 buckets;
2466 u32 sessions;
2467 u32 iterations;
2468 u32 memory_size;
2469 ip4_address_t src;
2470 vnet_classify_table_t *table;
2471 u32 table_index;
2472 int verbose;
2473
2474 /* Random seed */
2475 u32 seed;
2476
2477 /* Test data */
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302478 classify_data_or_mask_t *mask;
2479 classify_data_or_mask_t *data;
Dave Barachcada2a02017-05-18 19:16:47 -04002480
2481 /* convenience */
2482 vnet_classify_main_t *classify_main;
2483 vlib_main_t *vlib_main;
2484
2485} test_classify_main_t;
2486
2487static test_classify_main_t test_classify_main;
2488
2489static clib_error_t *
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302490test_classify_churn (test_classify_main_t * tm)
Dave Barachcada2a02017-05-18 19:16:47 -04002491{
2492 classify_data_or_mask_t *mask, *data;
2493 vlib_main_t *vm = tm->vlib_main;
2494 test_entry_t *ep;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002495 u8 *mp = 0, *dp = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002496 u32 tmp;
Dave Barachcada2a02017-05-18 19:16:47 -04002497 int i, rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002498
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302499 vec_validate_aligned (mp, 3 * sizeof (u32x4), sizeof (u32x4));
2500 vec_validate_aligned (dp, 3 * sizeof (u32x4), sizeof (u32x4));
Ed Warnickecb9cada2015-12-08 15:45:58 -07002501
2502 mask = (classify_data_or_mask_t *) mp;
2503 data = (classify_data_or_mask_t *) dp;
2504
Ed Warnickecb9cada2015-12-08 15:45:58 -07002505 /* Mask on src address */
Dave Barachb7b92992018-10-17 10:38:51 -04002506 clib_memset (&mask->ip.src_address, 0xff, 4);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002507
Dave Barachcada2a02017-05-18 19:16:47 -04002508 tmp = clib_host_to_net_u32 (tm->src.as_u32);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002509
Dave Barachcada2a02017-05-18 19:16:47 -04002510 for (i = 0; i < tm->sessions; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002511 {
Dave Barachcada2a02017-05-18 19:16:47 -04002512 vec_add2 (tm->entries, ep, 1);
2513 ep->addr.as_u32 = clib_host_to_net_u32 (tmp);
2514 ep->in_table = 0;
2515 tmp++;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002516 }
2517
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302518 tm->table = vnet_classify_new_table (tm->classify_main,
2519 (u8 *) mask,
2520 tm->buckets,
2521 tm->memory_size, 0 /* skip */ ,
2522 3 /* vectors to match */ );
Dave Barachcada2a02017-05-18 19:16:47 -04002523 tm->table->miss_next_index = IP_LOOKUP_NEXT_DROP;
2524 tm->table_index = tm->table - tm->classify_main->tables;
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302525 vlib_cli_output (vm, "Created table %d, buckets %d",
2526 tm->table_index, tm->buckets);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002527
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302528 vlib_cli_output (vm, "Initialize: add %d (approx. half of %d sessions)...",
2529 tm->sessions / 2, tm->sessions);
2530
2531 for (i = 0; i < tm->sessions / 2; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002532 {
Dave Barachcada2a02017-05-18 19:16:47 -04002533 ep = vec_elt_at_index (tm->entries, i);
2534
2535 data->ip.src_address.as_u32 = ep->addr.as_u32;
2536 ep->in_table = 1;
2537
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302538 rv = vnet_classify_add_del_session (tm->classify_main,
2539 tm->table_index,
2540 (u8 *) data,
2541 IP_LOOKUP_NEXT_DROP,
2542 i /* opaque_index */ ,
2543 0 /* advance */ ,
2544 0 /* action */ ,
2545 0 /* metadata */ ,
2546 1 /* is_add */ );
2547
Dave Barachcada2a02017-05-18 19:16:47 -04002548 if (rv != 0)
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302549 clib_warning ("add: returned %d", rv);
Dave Barachcada2a02017-05-18 19:16:47 -04002550
2551 if (tm->verbose)
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302552 vlib_cli_output (vm, "add: %U", format_ip4_address, &ep->addr.as_u32);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002553 }
2554
Dave Barachcada2a02017-05-18 19:16:47 -04002555 vlib_cli_output (vm, "Execute %d random add/delete operations",
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302556 tm->iterations);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002557
Dave Barachcada2a02017-05-18 19:16:47 -04002558 for (i = 0; i < tm->iterations; i++)
2559 {
2560 int index, is_add;
2561
2562 /* Pick a random entry */
2563 index = random_u32 (&tm->seed) % tm->sessions;
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302564
Dave Barachcada2a02017-05-18 19:16:47 -04002565 ep = vec_elt_at_index (tm->entries, index);
2566
2567 data->ip.src_address.as_u32 = ep->addr.as_u32;
2568
2569 /* If it's in the table, remove it. Else, add it */
2570 is_add = !ep->in_table;
2571
2572 if (tm->verbose)
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302573 vlib_cli_output (vm, "%s: %U",
2574 is_add ? "add" : "del",
2575 format_ip4_address, &ep->addr.as_u32);
Dave Barachcada2a02017-05-18 19:16:47 -04002576
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302577 rv = vnet_classify_add_del_session (tm->classify_main,
2578 tm->table_index,
2579 (u8 *) data,
2580 IP_LOOKUP_NEXT_DROP,
2581 i /* opaque_index */ ,
2582 0 /* advance */ ,
2583 0 /* action */ ,
2584 0 /* metadata */ ,
2585 is_add);
Dave Barachcada2a02017-05-18 19:16:47 -04002586 if (rv != 0)
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302587 vlib_cli_output (vm,
2588 "%s[%d]: %U returned %d", is_add ? "add" : "del",
2589 index, format_ip4_address, &ep->addr.as_u32, rv);
Dave Barachcada2a02017-05-18 19:16:47 -04002590 else
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302591 ep->in_table = is_add;
Dave Barachcada2a02017-05-18 19:16:47 -04002592 }
2593
2594 vlib_cli_output (vm, "Remove remaining %d entries from the table",
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302595 tm->table->active_elements);
Dave Barachcada2a02017-05-18 19:16:47 -04002596
2597 for (i = 0; i < tm->sessions; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002598 {
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302599 u8 *key_minus_skip;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002600 u64 hash;
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302601 vnet_classify_entry_t *e;
2602
Dave Barachcada2a02017-05-18 19:16:47 -04002603 ep = tm->entries + i;
2604 if (ep->in_table == 0)
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302605 continue;
Dave Barachcada2a02017-05-18 19:16:47 -04002606
2607 data->ip.src_address.as_u32 = ep->addr.as_u32;
2608
2609 hash = vnet_classify_hash_packet (tm->table, (u8 *) data);
2610
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302611 e = vnet_classify_find_entry (tm->table,
2612 (u8 *) data, hash, 0 /* time_now */ );
Dave Barachcada2a02017-05-18 19:16:47 -04002613 if (e == 0)
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302614 {
2615 clib_warning ("Couldn't find %U index %d which should be present",
2616 format_ip4_address, ep->addr, i);
2617 continue;
2618 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002619
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302620 key_minus_skip = (u8 *) e->key;
Dave Barachcada2a02017-05-18 19:16:47 -04002621 key_minus_skip -= tm->table->skip_n_vectors * sizeof (u32x4);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002622
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302623 rv = vnet_classify_add_del_session
2624 (tm->classify_main,
2625 tm->table_index,
2626 key_minus_skip, IP_LOOKUP_NEXT_DROP, i /* opaque_index */ ,
2627 0 /* advance */ , 0, 0,
2628 0 /* is_add */ );
Dave Barachcada2a02017-05-18 19:16:47 -04002629
Ed Warnickecb9cada2015-12-08 15:45:58 -07002630 if (rv != 0)
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302631 clib_warning ("del: returned %d", rv);
Dave Barachcada2a02017-05-18 19:16:47 -04002632
2633 if (tm->verbose)
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302634 vlib_cli_output (vm, "del: %U", format_ip4_address, &ep->addr.as_u32);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002635 }
2636
Dave Barachcada2a02017-05-18 19:16:47 -04002637 vlib_cli_output (vm, "%d entries remain, MUST be zero",
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302638 tm->table->active_elements);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002639
Dave Barachcada2a02017-05-18 19:16:47 -04002640 vlib_cli_output (vm, "Table after cleanup: \n%U\n",
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302641 format_classify_table, tm->table, 0 /* verbose */ );
Dave Barachcada2a02017-05-18 19:16:47 -04002642
Ed Warnickecb9cada2015-12-08 15:45:58 -07002643 vec_free (mp);
2644 vec_free (dp);
2645
Dave Barachcada2a02017-05-18 19:16:47 -04002646 vnet_classify_delete_table_index (tm->classify_main,
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302647 tm->table_index, 1 /* del_chain */ );
Dave Barachcada2a02017-05-18 19:16:47 -04002648 tm->table = 0;
2649 tm->table_index = ~0;
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302650 vec_free (tm->entries);
Dave Barachcada2a02017-05-18 19:16:47 -04002651
Ed Warnickecb9cada2015-12-08 15:45:58 -07002652 return 0;
2653}
2654
Dave Barachcada2a02017-05-18 19:16:47 -04002655static clib_error_t *
2656test_classify_command_fn (vlib_main_t * vm,
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302657 unformat_input_t * input, vlib_cli_command_t * cmd)
Dave Barachcada2a02017-05-18 19:16:47 -04002658{
2659 test_classify_main_t *tm = &test_classify_main;
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302660 vnet_classify_main_t *cm = &vnet_classify_main;
Dave Barachcada2a02017-05-18 19:16:47 -04002661 u32 tmp;
2662 int which = 0;
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302663 clib_error_t *error = 0;
2664
Dave Barachcada2a02017-05-18 19:16:47 -04002665 tm->buckets = 1024;
2666 tm->sessions = 8192;
2667 tm->iterations = 8192;
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302668 tm->memory_size = 64 << 20;
Dave Barachcada2a02017-05-18 19:16:47 -04002669 tm->src.as_u32 = clib_net_to_host_u32 (0x0100000A);
2670 tm->table = 0;
2671 tm->seed = 0xDEADDABE;
2672 tm->classify_main = cm;
2673 tm->vlib_main = vm;
2674 tm->verbose = 0;
2675
2676 /* Default starting address 1.0.0.10 */
2677
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302678 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2679 {
2680 if (unformat (input, "sessions %d", &tmp))
2681 tm->sessions = tmp;
2682 else
2683 if (unformat (input, "src %U", unformat_ip4_address, &tm->src.as_u32))
2684 ;
2685 else if (unformat (input, "buckets %d", &tm->buckets))
2686 ;
2687 else if (unformat (input, "memory-size %uM", &tmp))
2688 tm->memory_size = tmp << 20;
2689 else if (unformat (input, "memory-size %uG", &tmp))
2690 tm->memory_size = tmp << 30;
2691 else if (unformat (input, "seed %d", &tm->seed))
2692 ;
2693 else if (unformat (input, "verbose"))
2694 tm->verbose = 1;
Dave Barachcada2a02017-05-18 19:16:47 -04002695
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302696 else if (unformat (input, "iterations %d", &tm->iterations))
2697 ;
2698 else if (unformat (input, "churn-test"))
2699 which = 0;
2700 else
2701 break;
Dave Barachcada2a02017-05-18 19:16:47 -04002702 }
2703
2704 switch (which)
2705 {
2706 case 0:
2707 error = test_classify_churn (tm);
2708 break;
2709 default:
2710 error = clib_error_return (0, "No such test");
2711 break;
2712 }
2713
2714 return error;
2715}
2716
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302717/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002718VLIB_CLI_COMMAND (test_classify_command, static) = {
2719 .path = "test classify",
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302720 .short_help =
Dave Barachcada2a02017-05-18 19:16:47 -04002721 "test classify [src <ip>] [sessions <nn>] [buckets <nn>] [seed <nnn>]\n"
2722 " [memory-size <nn>[M|G]]\n"
2723 " [churn-test]",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002724 .function = test_classify_command_fn,
2725};
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302726/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002727#endif /* TEST_CODE */
khemendra kumard7bfa0e2017-11-27 15:15:53 +05302728
2729/*
2730 * fd.io coding-style-patch-verification: ON
2731 *
2732 * Local Variables:
2733 * eval: (c-set-style "gnu")
2734 * End:
2735 */