Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | #include <vnet/classify/vnet_classify.h> |
Andrew Yourtchenko | 815d7d5 | 2018-02-07 11:37:02 +0100 | [diff] [blame] | 16 | #include <vnet/classify/in_out_acl.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 17 | #include <vnet/ip/ip.h> |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 18 | #include <vnet/api_errno.h> /* for API error numbers */ |
| 19 | #include <vnet/l2/l2_classify.h> /* for L2_INPUT_CLASSIFY_NEXT_xxx */ |
Steve Shin | 25e26dc | 2016-11-08 10:47:10 -0800 | [diff] [blame] | 20 | #include <vnet/fib/fib_table.h> |
jaszha03 | 0455c43 | 2019-06-12 16:01:19 -0500 | [diff] [blame] | 21 | #include <vppinfra/lock.h> |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 22 | #include <vnet/classify/trace_classify.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 23 | |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 24 | /** |
| 25 | * @file |
| 26 | * @brief N-tuple classifier |
| 27 | */ |
| 28 | |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 29 | vnet_classify_main_t vnet_classify_main; |
| 30 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 31 | #if VALIDATION_SCAFFOLDING |
| 32 | /* Validation scaffolding */ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 33 | void |
| 34 | mv (vnet_classify_table_t * t) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 35 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 36 | void *oldheap; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 37 | |
| 38 | oldheap = clib_mem_set_heap (t->mheap); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 39 | clib_mem_validate (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 40 | clib_mem_set_heap (oldheap); |
| 41 | } |
| 42 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 43 | void |
| 44 | rogue (vnet_classify_table_t * t) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 45 | { |
| 46 | int i, j, k; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 47 | vnet_classify_entry_t *v, *save_v; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 48 | u32 active_elements = 0; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 49 | vnet_classify_bucket_t *b; |
| 50 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 51 | for (i = 0; i < t->nbuckets; i++) |
| 52 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 53 | b = &t->buckets[i]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 54 | if (b->offset == 0) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 55 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 56 | save_v = vnet_classify_get_entry (t, b->offset); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 57 | for (j = 0; j < (1 << b->log2_pages); j++) |
| 58 | { |
| 59 | for (k = 0; k < t->entries_per_page; k++) |
| 60 | { |
| 61 | v = vnet_classify_entry_at_index |
| 62 | (t, save_v, j * t->entries_per_page + k); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 63 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 64 | if (vnet_classify_entry_is_busy (v)) |
| 65 | active_elements++; |
| 66 | } |
| 67 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | if (active_elements != t->active_elements) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 71 | clib_warning ("found %u expected %u elts", active_elements, |
| 72 | t->active_elements); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 73 | } |
| 74 | #else |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 75 | void |
| 76 | mv (vnet_classify_table_t * t) |
| 77 | { |
| 78 | } |
| 79 | |
| 80 | void |
| 81 | rogue (vnet_classify_table_t * t) |
| 82 | { |
| 83 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 84 | #endif |
| 85 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 86 | void |
| 87 | vnet_classify_register_unformat_l2_next_index_fn (unformat_function_t * fn) |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 88 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 89 | vnet_classify_main_t *cm = &vnet_classify_main; |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 90 | |
| 91 | vec_add1 (cm->unformat_l2_next_index_fns, fn); |
| 92 | } |
| 93 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 94 | void |
| 95 | vnet_classify_register_unformat_ip_next_index_fn (unformat_function_t * fn) |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 96 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 97 | vnet_classify_main_t *cm = &vnet_classify_main; |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 98 | |
| 99 | vec_add1 (cm->unformat_ip_next_index_fns, fn); |
| 100 | } |
| 101 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 102 | void |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 103 | vnet_classify_register_unformat_acl_next_index_fn (unformat_function_t * fn) |
| 104 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 105 | vnet_classify_main_t *cm = &vnet_classify_main; |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 106 | |
| 107 | vec_add1 (cm->unformat_acl_next_index_fns, fn); |
| 108 | } |
| 109 | |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 110 | void |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 111 | vnet_classify_register_unformat_policer_next_index_fn (unformat_function_t * |
| 112 | fn) |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 113 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 114 | vnet_classify_main_t *cm = &vnet_classify_main; |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 115 | |
| 116 | vec_add1 (cm->unformat_policer_next_index_fns, fn); |
| 117 | } |
| 118 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 119 | void |
| 120 | vnet_classify_register_unformat_opaque_index_fn (unformat_function_t * fn) |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 121 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 122 | vnet_classify_main_t *cm = &vnet_classify_main; |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 123 | |
| 124 | vec_add1 (cm->unformat_opaque_index_fns, fn); |
| 125 | } |
| 126 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 127 | vnet_classify_table_t * |
| 128 | vnet_classify_new_table (vnet_classify_main_t * cm, |
| 129 | u8 * mask, u32 nbuckets, u32 memory_size, |
| 130 | u32 skip_n_vectors, u32 match_n_vectors) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 131 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 132 | vnet_classify_table_t *t; |
| 133 | void *oldheap; |
| 134 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 135 | nbuckets = 1 << (max_log2 (nbuckets)); |
| 136 | |
| 137 | pool_get_aligned (cm->tables, t, CLIB_CACHE_LINE_BYTES); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 138 | clib_memset (t, 0, sizeof (*t)); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 139 | |
| 140 | vec_validate_aligned (t->mask, match_n_vectors - 1, sizeof (u32x4)); |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 141 | clib_memcpy_fast (t->mask, mask, match_n_vectors * sizeof (u32x4)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 142 | |
| 143 | t->next_table_index = ~0; |
| 144 | t->nbuckets = nbuckets; |
| 145 | t->log2_nbuckets = max_log2 (nbuckets); |
| 146 | t->match_n_vectors = match_n_vectors; |
| 147 | t->skip_n_vectors = skip_n_vectors; |
| 148 | t->entries_per_page = 2; |
| 149 | |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 150 | t->mheap = clib_mem_create_heap (0, memory_size, 1 /* locked */ , |
| 151 | "classify"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 152 | |
| 153 | vec_validate_aligned (t->buckets, nbuckets - 1, CLIB_CACHE_LINE_BYTES); |
| 154 | oldheap = clib_mem_set_heap (t->mheap); |
| 155 | |
jaszha03 | 5cdde5c | 2019-07-11 20:47:24 +0000 | [diff] [blame] | 156 | clib_spinlock_init (&t->writer_lock); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 157 | clib_mem_set_heap (oldheap); |
| 158 | return (t); |
| 159 | } |
| 160 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 161 | void |
| 162 | vnet_classify_delete_table_index (vnet_classify_main_t * cm, |
| 163 | u32 table_index, int del_chain) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 164 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 165 | vnet_classify_table_t *t; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 166 | |
| 167 | /* Tolerate multiple frees, up to a point */ |
| 168 | if (pool_is_free_index (cm->tables, table_index)) |
| 169 | return; |
| 170 | |
| 171 | t = pool_elt_at_index (cm->tables, table_index); |
Juraj Sloboda | 288e893 | 2016-12-06 21:25:19 +0100 | [diff] [blame] | 172 | if (del_chain && t->next_table_index != ~0) |
| 173 | /* Recursively delete the entire chain */ |
| 174 | vnet_classify_delete_table_index (cm, t->next_table_index, del_chain); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 175 | |
| 176 | vec_free (t->mask); |
| 177 | vec_free (t->buckets); |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 178 | clib_mem_destroy_heap (t->mheap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 179 | pool_put (cm->tables, t); |
| 180 | } |
| 181 | |
| 182 | static vnet_classify_entry_t * |
| 183 | vnet_classify_entry_alloc (vnet_classify_table_t * t, u32 log2_pages) |
| 184 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 185 | vnet_classify_entry_t *rv = 0; |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 186 | u32 required_length; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 187 | void *oldheap; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 188 | |
jaszha03 | 5cdde5c | 2019-07-11 20:47:24 +0000 | [diff] [blame] | 189 | CLIB_SPINLOCK_ASSERT_LOCKED (&t->writer_lock); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 190 | required_length = |
| 191 | (sizeof (vnet_classify_entry_t) + (t->match_n_vectors * sizeof (u32x4))) |
| 192 | * t->entries_per_page * (1 << log2_pages); |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 193 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 194 | if (log2_pages >= vec_len (t->freelists) || t->freelists[log2_pages] == 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 195 | { |
| 196 | oldheap = clib_mem_set_heap (t->mheap); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 197 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 198 | vec_validate (t->freelists, log2_pages); |
| 199 | |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 200 | rv = clib_mem_alloc_aligned (required_length, CLIB_CACHE_LINE_BYTES); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 201 | clib_mem_set_heap (oldheap); |
| 202 | goto initialize; |
| 203 | } |
| 204 | rv = t->freelists[log2_pages]; |
| 205 | t->freelists[log2_pages] = rv->next_free; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 206 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 207 | initialize: |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 208 | ASSERT (rv); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 209 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 210 | clib_memset (rv, 0xff, required_length); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 211 | return rv; |
| 212 | } |
| 213 | |
| 214 | static void |
| 215 | vnet_classify_entry_free (vnet_classify_table_t * t, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 216 | vnet_classify_entry_t * v, u32 log2_pages) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 217 | { |
jaszha03 | 5cdde5c | 2019-07-11 20:47:24 +0000 | [diff] [blame] | 218 | CLIB_SPINLOCK_ASSERT_LOCKED (&t->writer_lock); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 219 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 220 | ASSERT (vec_len (t->freelists) > log2_pages); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 221 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 222 | v->next_free = t->freelists[log2_pages]; |
| 223 | t->freelists[log2_pages] = v; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | static inline void make_working_copy |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 227 | (vnet_classify_table_t * t, vnet_classify_bucket_t * b) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 228 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 229 | vnet_classify_entry_t *v; |
| 230 | vnet_classify_bucket_t working_bucket __attribute__ ((aligned (8))); |
| 231 | void *oldheap; |
| 232 | vnet_classify_entry_t *working_copy; |
| 233 | u32 thread_index = vlib_get_thread_index (); |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 234 | int working_copy_length, required_length; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 235 | |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 236 | if (thread_index >= vec_len (t->working_copies)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 237 | { |
| 238 | oldheap = clib_mem_set_heap (t->mheap); |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 239 | vec_validate (t->working_copies, thread_index); |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 240 | vec_validate (t->working_copy_lengths, thread_index); |
| 241 | t->working_copy_lengths[thread_index] = -1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 242 | clib_mem_set_heap (oldheap); |
| 243 | } |
| 244 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 245 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 246 | * working_copies are per-cpu so that near-simultaneous |
| 247 | * updates from multiple threads will not result in sporadic, spurious |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 248 | * lookup failures. |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 249 | */ |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 250 | working_copy = t->working_copies[thread_index]; |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 251 | working_copy_length = t->working_copy_lengths[thread_index]; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 252 | required_length = |
| 253 | (sizeof (vnet_classify_entry_t) + (t->match_n_vectors * sizeof (u32x4))) |
| 254 | * t->entries_per_page * (1 << b->log2_pages); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 255 | |
| 256 | t->saved_bucket.as_u64 = b->as_u64; |
| 257 | oldheap = clib_mem_set_heap (t->mheap); |
| 258 | |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 259 | if (required_length > working_copy_length) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 260 | { |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 261 | if (working_copy) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 262 | clib_mem_free (working_copy); |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 263 | working_copy = |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 264 | clib_mem_alloc_aligned (required_length, CLIB_CACHE_LINE_BYTES); |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 265 | t->working_copies[thread_index] = working_copy; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 268 | clib_mem_set_heap (oldheap); |
| 269 | |
| 270 | v = vnet_classify_get_entry (t, b->offset); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 271 | |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 272 | clib_memcpy_fast (working_copy, v, required_length); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 273 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 274 | working_bucket.as_u64 = b->as_u64; |
| 275 | working_bucket.offset = vnet_classify_get_offset (t, working_copy); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 276 | CLIB_MEMORY_BARRIER (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 277 | b->as_u64 = working_bucket.as_u64; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 278 | t->working_copies[thread_index] = working_copy; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | static vnet_classify_entry_t * |
| 282 | split_and_rehash (vnet_classify_table_t * t, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 283 | vnet_classify_entry_t * old_values, u32 old_log2_pages, |
| 284 | u32 new_log2_pages) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 285 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 286 | vnet_classify_entry_t *new_values, *v, *new_v; |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 287 | int i, j, length_in_entries; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 288 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 289 | new_values = vnet_classify_entry_alloc (t, new_log2_pages); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 290 | length_in_entries = (1 << old_log2_pages) * t->entries_per_page; |
| 291 | |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 292 | for (i = 0; i < length_in_entries; i++) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 293 | { |
| 294 | u64 new_hash; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 295 | |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 296 | v = vnet_classify_entry_at_index (t, old_values, i); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 297 | |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 298 | if (vnet_classify_entry_is_busy (v)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 299 | { |
| 300 | /* Hack so we can use the packet hash routine */ |
| 301 | u8 *key_minus_skip; |
| 302 | key_minus_skip = (u8 *) v->key; |
| 303 | key_minus_skip -= t->skip_n_vectors * sizeof (u32x4); |
| 304 | |
| 305 | new_hash = vnet_classify_hash_packet (t, key_minus_skip); |
| 306 | new_hash >>= t->log2_nbuckets; |
| 307 | new_hash &= (1 << new_log2_pages) - 1; |
| 308 | |
| 309 | for (j = 0; j < t->entries_per_page; j++) |
| 310 | { |
| 311 | new_v = vnet_classify_entry_at_index (t, new_values, |
| 312 | new_hash + j); |
| 313 | |
| 314 | if (vnet_classify_entry_is_free (new_v)) |
| 315 | { |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 316 | clib_memcpy_fast (new_v, v, sizeof (vnet_classify_entry_t) |
| 317 | + (t->match_n_vectors * sizeof (u32x4))); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 318 | new_v->flags &= ~(VNET_CLASSIFY_ENTRY_FREE); |
| 319 | goto doublebreak; |
| 320 | } |
| 321 | } |
| 322 | /* Crap. Tell caller to try again */ |
| 323 | vnet_classify_entry_free (t, new_values, new_log2_pages); |
| 324 | return 0; |
| 325 | doublebreak: |
| 326 | ; |
| 327 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 328 | } |
| 329 | return new_values; |
| 330 | } |
| 331 | |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 332 | static vnet_classify_entry_t * |
| 333 | split_and_rehash_linear (vnet_classify_table_t * t, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 334 | vnet_classify_entry_t * old_values, |
| 335 | u32 old_log2_pages, u32 new_log2_pages) |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 336 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 337 | vnet_classify_entry_t *new_values, *v, *new_v; |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 338 | int i, j, new_length_in_entries, old_length_in_entries; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 339 | |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 340 | new_values = vnet_classify_entry_alloc (t, new_log2_pages); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 341 | new_length_in_entries = (1 << new_log2_pages) * t->entries_per_page; |
| 342 | old_length_in_entries = (1 << old_log2_pages) * t->entries_per_page; |
| 343 | |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 344 | j = 0; |
| 345 | for (i = 0; i < old_length_in_entries; i++) |
| 346 | { |
| 347 | v = vnet_classify_entry_at_index (t, old_values, i); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 348 | |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 349 | if (vnet_classify_entry_is_busy (v)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 350 | { |
| 351 | for (; j < new_length_in_entries; j++) |
| 352 | { |
| 353 | new_v = vnet_classify_entry_at_index (t, new_values, j); |
| 354 | |
| 355 | if (vnet_classify_entry_is_busy (new_v)) |
| 356 | { |
| 357 | clib_warning ("BUG: linear rehash new entry not free!"); |
| 358 | continue; |
| 359 | } |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 360 | clib_memcpy_fast (new_v, v, sizeof (vnet_classify_entry_t) |
| 361 | + (t->match_n_vectors * sizeof (u32x4))); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 362 | new_v->flags &= ~(VNET_CLASSIFY_ENTRY_FREE); |
| 363 | j++; |
| 364 | goto doublebreak; |
| 365 | } |
| 366 | /* |
| 367 | * Crap. Tell caller to try again. |
| 368 | * This should never happen... |
| 369 | */ |
| 370 | clib_warning ("BUG: linear rehash failed!"); |
| 371 | vnet_classify_entry_free (t, new_values, new_log2_pages); |
| 372 | return 0; |
| 373 | } |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 374 | doublebreak: |
| 375 | ; |
| 376 | } |
| 377 | |
| 378 | return new_values; |
| 379 | } |
| 380 | |
Neale Ranns | 13eaf3e | 2017-05-23 06:10:33 -0700 | [diff] [blame] | 381 | static void |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 382 | vnet_classify_entry_claim_resource (vnet_classify_entry_t * e) |
Neale Ranns | 13eaf3e | 2017-05-23 06:10:33 -0700 | [diff] [blame] | 383 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 384 | switch (e->action) |
Neale Ranns | 13eaf3e | 2017-05-23 06:10:33 -0700 | [diff] [blame] | 385 | { |
| 386 | case CLASSIFY_ACTION_SET_IP4_FIB_INDEX: |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 387 | fib_table_lock (e->metadata, FIB_PROTOCOL_IP4, FIB_SOURCE_CLASSIFY); |
| 388 | break; |
Neale Ranns | 13eaf3e | 2017-05-23 06:10:33 -0700 | [diff] [blame] | 389 | case CLASSIFY_ACTION_SET_IP6_FIB_INDEX: |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 390 | fib_table_lock (e->metadata, FIB_PROTOCOL_IP6, FIB_SOURCE_CLASSIFY); |
| 391 | break; |
Dave Barach | 630a8e2 | 2017-11-18 06:58:34 -0500 | [diff] [blame] | 392 | case CLASSIFY_ACTION_SET_METADATA: |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 393 | break; |
Neale Ranns | 13eaf3e | 2017-05-23 06:10:33 -0700 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | |
| 397 | static void |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 398 | vnet_classify_entry_release_resource (vnet_classify_entry_t * e) |
Neale Ranns | 13eaf3e | 2017-05-23 06:10:33 -0700 | [diff] [blame] | 399 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 400 | switch (e->action) |
Neale Ranns | 13eaf3e | 2017-05-23 06:10:33 -0700 | [diff] [blame] | 401 | { |
| 402 | case CLASSIFY_ACTION_SET_IP4_FIB_INDEX: |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 403 | fib_table_unlock (e->metadata, FIB_PROTOCOL_IP4, FIB_SOURCE_CLASSIFY); |
| 404 | break; |
Neale Ranns | 13eaf3e | 2017-05-23 06:10:33 -0700 | [diff] [blame] | 405 | case CLASSIFY_ACTION_SET_IP6_FIB_INDEX: |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 406 | fib_table_unlock (e->metadata, FIB_PROTOCOL_IP6, FIB_SOURCE_CLASSIFY); |
| 407 | break; |
Dave Barach | 630a8e2 | 2017-11-18 06:58:34 -0500 | [diff] [blame] | 408 | case CLASSIFY_ACTION_SET_METADATA: |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 409 | break; |
Neale Ranns | 13eaf3e | 2017-05-23 06:10:33 -0700 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 413 | int |
| 414 | vnet_classify_add_del (vnet_classify_table_t * t, |
| 415 | vnet_classify_entry_t * add_v, int is_add) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 416 | { |
| 417 | u32 bucket_index; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 418 | vnet_classify_bucket_t *b, tmp_b; |
| 419 | vnet_classify_entry_t *v, *new_v, *save_new_v, *working_copy, *save_v; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 420 | u32 value_index; |
| 421 | int rv = 0; |
| 422 | int i; |
| 423 | u64 hash, new_hash; |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 424 | u32 limit; |
| 425 | u32 old_log2_pages, new_log2_pages; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 426 | u32 thread_index = vlib_get_thread_index (); |
| 427 | u8 *key_minus_skip; |
Dave Barach | 48113e0 | 2017-06-07 08:32:51 -0400 | [diff] [blame] | 428 | int resplit_once = 0; |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 429 | int mark_bucket_linear; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 430 | |
| 431 | ASSERT ((add_v->flags & VNET_CLASSIFY_ENTRY_FREE) == 0); |
| 432 | |
| 433 | key_minus_skip = (u8 *) add_v->key; |
| 434 | key_minus_skip -= t->skip_n_vectors * sizeof (u32x4); |
| 435 | |
| 436 | hash = vnet_classify_hash_packet (t, key_minus_skip); |
| 437 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 438 | bucket_index = hash & (t->nbuckets - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 439 | b = &t->buckets[bucket_index]; |
| 440 | |
| 441 | hash >>= t->log2_nbuckets; |
| 442 | |
jaszha03 | 5cdde5c | 2019-07-11 20:47:24 +0000 | [diff] [blame] | 443 | clib_spinlock_lock (&t->writer_lock); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 444 | |
| 445 | /* First elt in the bucket? */ |
| 446 | if (b->offset == 0) |
| 447 | { |
| 448 | if (is_add == 0) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 449 | { |
| 450 | rv = -1; |
| 451 | goto unlock; |
| 452 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 453 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 454 | v = vnet_classify_entry_alloc (t, 0 /* new_log2_pages */ ); |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 455 | clib_memcpy_fast (v, add_v, sizeof (vnet_classify_entry_t) + |
| 456 | t->match_n_vectors * sizeof (u32x4)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 457 | v->flags &= ~(VNET_CLASSIFY_ENTRY_FREE); |
Neale Ranns | 13eaf3e | 2017-05-23 06:10:33 -0700 | [diff] [blame] | 458 | vnet_classify_entry_claim_resource (v); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 459 | |
| 460 | tmp_b.as_u64 = 0; |
| 461 | tmp_b.offset = vnet_classify_get_offset (t, v); |
| 462 | |
| 463 | b->as_u64 = tmp_b.as_u64; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 464 | t->active_elements++; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 465 | |
| 466 | goto unlock; |
| 467 | } |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 468 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 469 | make_working_copy (t, b); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 470 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 471 | save_v = vnet_classify_get_entry (t, t->saved_bucket.offset); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 472 | value_index = hash & ((1 << t->saved_bucket.log2_pages) - 1); |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 473 | limit = t->entries_per_page; |
| 474 | if (PREDICT_FALSE (b->linear_search)) |
| 475 | { |
| 476 | value_index = 0; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 477 | limit *= (1 << b->log2_pages); |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 478 | } |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 479 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 480 | if (is_add) |
| 481 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 482 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 483 | * For obvious (in hindsight) reasons, see if we're supposed to |
| 484 | * replace an existing key, then look for an empty slot. |
| 485 | */ |
| 486 | |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 487 | for (i = 0; i < limit; i++) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 488 | { |
| 489 | v = vnet_classify_entry_at_index (t, save_v, value_index + i); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 490 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 491 | if (!memcmp |
| 492 | (v->key, add_v->key, t->match_n_vectors * sizeof (u32x4))) |
| 493 | { |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 494 | clib_memcpy_fast (v, add_v, sizeof (vnet_classify_entry_t) + |
| 495 | t->match_n_vectors * sizeof (u32x4)); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 496 | v->flags &= ~(VNET_CLASSIFY_ENTRY_FREE); |
| 497 | vnet_classify_entry_claim_resource (v); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 498 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 499 | CLIB_MEMORY_BARRIER (); |
| 500 | /* Restore the previous (k,v) pairs */ |
| 501 | b->as_u64 = t->saved_bucket.as_u64; |
| 502 | goto unlock; |
| 503 | } |
| 504 | } |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 505 | for (i = 0; i < limit; i++) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 506 | { |
| 507 | v = vnet_classify_entry_at_index (t, save_v, value_index + i); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 508 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 509 | if (vnet_classify_entry_is_free (v)) |
| 510 | { |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 511 | clib_memcpy_fast (v, add_v, sizeof (vnet_classify_entry_t) + |
| 512 | t->match_n_vectors * sizeof (u32x4)); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 513 | v->flags &= ~(VNET_CLASSIFY_ENTRY_FREE); |
| 514 | vnet_classify_entry_claim_resource (v); |
Neale Ranns | 13eaf3e | 2017-05-23 06:10:33 -0700 | [diff] [blame] | 515 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 516 | CLIB_MEMORY_BARRIER (); |
| 517 | b->as_u64 = t->saved_bucket.as_u64; |
| 518 | t->active_elements++; |
| 519 | goto unlock; |
| 520 | } |
| 521 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 522 | /* no room at the inn... split case... */ |
| 523 | } |
| 524 | else |
| 525 | { |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 526 | for (i = 0; i < limit; i++) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 527 | { |
| 528 | v = vnet_classify_entry_at_index (t, save_v, value_index + i); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 529 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 530 | if (!memcmp |
| 531 | (v->key, add_v->key, t->match_n_vectors * sizeof (u32x4))) |
| 532 | { |
| 533 | vnet_classify_entry_release_resource (v); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 534 | clib_memset (v, 0xff, sizeof (vnet_classify_entry_t) + |
| 535 | t->match_n_vectors * sizeof (u32x4)); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 536 | v->flags |= VNET_CLASSIFY_ENTRY_FREE; |
Neale Ranns | 13eaf3e | 2017-05-23 06:10:33 -0700 | [diff] [blame] | 537 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 538 | CLIB_MEMORY_BARRIER (); |
| 539 | b->as_u64 = t->saved_bucket.as_u64; |
| 540 | t->active_elements--; |
| 541 | goto unlock; |
| 542 | } |
| 543 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 544 | rv = -3; |
| 545 | b->as_u64 = t->saved_bucket.as_u64; |
| 546 | goto unlock; |
| 547 | } |
| 548 | |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 549 | old_log2_pages = t->saved_bucket.log2_pages; |
| 550 | new_log2_pages = old_log2_pages + 1; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 551 | working_copy = t->working_copies[thread_index]; |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 552 | |
| 553 | if (t->saved_bucket.linear_search) |
| 554 | goto linear_resplit; |
| 555 | |
| 556 | mark_bucket_linear = 0; |
| 557 | |
| 558 | new_v = split_and_rehash (t, working_copy, old_log2_pages, new_log2_pages); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 559 | |
| 560 | if (new_v == 0) |
| 561 | { |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 562 | try_resplit: |
| 563 | resplit_once = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 564 | new_log2_pages++; |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 565 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 566 | new_v = split_and_rehash (t, working_copy, old_log2_pages, |
| 567 | new_log2_pages); |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 568 | if (new_v == 0) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 569 | { |
| 570 | mark_linear: |
| 571 | new_log2_pages--; |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 572 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 573 | linear_resplit: |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 574 | /* pinned collisions, use linear search */ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 575 | new_v = split_and_rehash_linear (t, working_copy, old_log2_pages, |
| 576 | new_log2_pages); |
| 577 | /* A new linear-search bucket? */ |
| 578 | if (!t->saved_bucket.linear_search) |
| 579 | t->linear_buckets++; |
| 580 | mark_bucket_linear = 1; |
| 581 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | /* Try to add the new entry */ |
| 585 | save_new_v = new_v; |
| 586 | |
| 587 | key_minus_skip = (u8 *) add_v->key; |
| 588 | key_minus_skip -= t->skip_n_vectors * sizeof (u32x4); |
| 589 | |
| 590 | new_hash = vnet_classify_hash_packet_inline (t, key_minus_skip); |
| 591 | new_hash >>= t->log2_nbuckets; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 592 | new_hash &= (1 << new_log2_pages) - 1; |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 593 | |
| 594 | limit = t->entries_per_page; |
| 595 | if (mark_bucket_linear) |
| 596 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 597 | limit *= (1 << new_log2_pages); |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 598 | new_hash = 0; |
| 599 | } |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 600 | |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 601 | for (i = 0; i < limit; i++) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 602 | { |
| 603 | new_v = vnet_classify_entry_at_index (t, save_new_v, new_hash + i); |
| 604 | |
| 605 | if (vnet_classify_entry_is_free (new_v)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 606 | { |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 607 | clib_memcpy_fast (new_v, add_v, sizeof (vnet_classify_entry_t) + |
| 608 | t->match_n_vectors * sizeof (u32x4)); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 609 | new_v->flags &= ~(VNET_CLASSIFY_ENTRY_FREE); |
| 610 | vnet_classify_entry_claim_resource (new_v); |
Neale Ranns | 13eaf3e | 2017-05-23 06:10:33 -0700 | [diff] [blame] | 611 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 612 | goto expand_ok; |
| 613 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 614 | } |
| 615 | /* Crap. Try again */ |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 616 | vnet_classify_entry_free (t, save_new_v, new_log2_pages); |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 617 | |
| 618 | if (resplit_once) |
| 619 | goto mark_linear; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 620 | else |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 621 | goto try_resplit; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 622 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 623 | expand_ok: |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 624 | tmp_b.log2_pages = new_log2_pages; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 625 | tmp_b.offset = vnet_classify_get_offset (t, save_new_v); |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 626 | tmp_b.linear_search = mark_bucket_linear; |
| 627 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 628 | CLIB_MEMORY_BARRIER (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 629 | b->as_u64 = tmp_b.as_u64; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 630 | t->active_elements++; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 631 | v = vnet_classify_get_entry (t, t->saved_bucket.offset); |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 632 | vnet_classify_entry_free (t, v, old_log2_pages); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 633 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 634 | unlock: |
jaszha03 | 5cdde5c | 2019-07-11 20:47:24 +0000 | [diff] [blame] | 635 | clib_spinlock_unlock (&t->writer_lock); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 636 | return rv; |
| 637 | } |
| 638 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 639 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 640 | typedef CLIB_PACKED(struct { |
| 641 | ethernet_header_t eh; |
| 642 | ip4_header_t ip; |
| 643 | }) classify_data_or_mask_t; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 644 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 645 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 646 | u64 |
| 647 | vnet_classify_hash_packet (vnet_classify_table_t * t, u8 * h) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 648 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 649 | return vnet_classify_hash_packet_inline (t, h); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 650 | } |
| 651 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 652 | vnet_classify_entry_t * |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 653 | vnet_classify_find_entry (vnet_classify_table_t * t, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 654 | u8 * h, u64 hash, f64 now) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 655 | { |
| 656 | return vnet_classify_find_entry_inline (t, h, hash, now); |
| 657 | } |
| 658 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 659 | static u8 * |
| 660 | format_classify_entry (u8 * s, va_list * args) |
| 661 | { |
| 662 | vnet_classify_table_t *t = va_arg (*args, vnet_classify_table_t *); |
| 663 | vnet_classify_entry_t *e = va_arg (*args, vnet_classify_entry_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 664 | |
| 665 | s = format |
Steve Shin | 25e26dc | 2016-11-08 10:47:10 -0800 | [diff] [blame] | 666 | (s, "[%u]: next_index %d advance %d opaque %d action %d metadata %d\n", |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 667 | vnet_classify_get_offset (t, e), e->next_index, e->advance, |
Steve Shin | 25e26dc | 2016-11-08 10:47:10 -0800 | [diff] [blame] | 668 | e->opaque_index, e->action, e->metadata); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 669 | |
| 670 | |
| 671 | s = format (s, " k: %U\n", format_hex_bytes, e->key, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 672 | t->match_n_vectors * sizeof (u32x4)); |
| 673 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 674 | if (vnet_classify_entry_is_busy (e)) |
| 675 | s = format (s, " hits %lld, last_heard %.2f\n", |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 676 | e->hits, e->last_heard); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 677 | else |
| 678 | s = format (s, " entry is free\n"); |
| 679 | return s; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | u8 * |
| 683 | format_classify_table (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 684 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 685 | vnet_classify_table_t *t = va_arg (*args, vnet_classify_table_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 686 | int verbose = va_arg (*args, int); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 687 | vnet_classify_bucket_t *b; |
| 688 | vnet_classify_entry_t *v, *save_v; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 689 | int i, j, k; |
| 690 | u64 active_elements = 0; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 691 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 692 | for (i = 0; i < t->nbuckets; i++) |
| 693 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 694 | b = &t->buckets[i]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 695 | if (b->offset == 0) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 696 | { |
| 697 | if (verbose > 1) |
| 698 | s = format (s, "[%d]: empty\n", i); |
| 699 | continue; |
| 700 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 701 | |
| 702 | if (verbose) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 703 | { |
| 704 | s = format (s, "[%d]: heap offset %d, elts %d, %s\n", i, |
| 705 | b->offset, (1 << b->log2_pages) * t->entries_per_page, |
| 706 | b->linear_search ? "LINEAR" : "normal"); |
| 707 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 708 | |
| 709 | save_v = vnet_classify_get_entry (t, b->offset); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 710 | for (j = 0; j < (1 << b->log2_pages); j++) |
| 711 | { |
| 712 | for (k = 0; k < t->entries_per_page; k++) |
| 713 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 714 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 715 | v = vnet_classify_entry_at_index (t, save_v, |
| 716 | j * t->entries_per_page + k); |
| 717 | |
| 718 | if (vnet_classify_entry_is_free (v)) |
| 719 | { |
| 720 | if (verbose > 1) |
| 721 | s = format (s, " %d: empty\n", |
| 722 | j * t->entries_per_page + k); |
| 723 | continue; |
| 724 | } |
| 725 | if (verbose) |
| 726 | { |
| 727 | s = format (s, " %d: %U\n", |
| 728 | j * t->entries_per_page + k, |
| 729 | format_classify_entry, t, v); |
| 730 | } |
| 731 | active_elements++; |
| 732 | } |
| 733 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | s = format (s, " %lld active elements\n", active_elements); |
| 737 | s = format (s, " %d free lists\n", vec_len (t->freelists)); |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 738 | s = format (s, " %d linear-search buckets\n", t->linear_buckets); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 739 | return s; |
| 740 | } |
| 741 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 742 | int |
| 743 | vnet_classify_add_del_table (vnet_classify_main_t * cm, |
| 744 | u8 * mask, |
| 745 | u32 nbuckets, |
| 746 | u32 memory_size, |
| 747 | u32 skip, |
| 748 | u32 match, |
| 749 | u32 next_table_index, |
| 750 | u32 miss_next_index, |
| 751 | u32 * table_index, |
| 752 | u8 current_data_flag, |
| 753 | i16 current_data_offset, |
| 754 | int is_add, int del_chain) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 755 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 756 | vnet_classify_table_t *t; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 757 | |
| 758 | if (is_add) |
| 759 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 760 | if (*table_index == ~0) /* add */ |
| 761 | { |
| 762 | if (memory_size == 0) |
| 763 | return VNET_API_ERROR_INVALID_MEMORY_SIZE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 764 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 765 | if (nbuckets == 0) |
| 766 | return VNET_API_ERROR_INVALID_VALUE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 767 | |
Benoît Ganne | 71a70d7 | 2019-12-10 12:44:46 +0100 | [diff] [blame] | 768 | if (match < 1 || match > 5) |
| 769 | return VNET_API_ERROR_INVALID_VALUE; |
| 770 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 771 | t = vnet_classify_new_table (cm, mask, nbuckets, memory_size, |
| 772 | skip, match); |
| 773 | t->next_table_index = next_table_index; |
| 774 | t->miss_next_index = miss_next_index; |
| 775 | t->current_data_flag = current_data_flag; |
| 776 | t->current_data_offset = current_data_offset; |
| 777 | *table_index = t - cm->tables; |
| 778 | } |
| 779 | else /* update */ |
| 780 | { |
| 781 | vnet_classify_main_t *cm = &vnet_classify_main; |
| 782 | t = pool_elt_at_index (cm->tables, *table_index); |
Steve Shin | 25e26dc | 2016-11-08 10:47:10 -0800 | [diff] [blame] | 783 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 784 | t->next_table_index = next_table_index; |
| 785 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 786 | return 0; |
| 787 | } |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 788 | |
Juraj Sloboda | 288e893 | 2016-12-06 21:25:19 +0100 | [diff] [blame] | 789 | vnet_classify_delete_table_index (cm, *table_index, del_chain); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 790 | return 0; |
| 791 | } |
| 792 | |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 793 | #define foreach_tcp_proto_field \ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 794 | _(src) \ |
| 795 | _(dst) |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 796 | |
| 797 | #define foreach_udp_proto_field \ |
| 798 | _(src_port) \ |
| 799 | _(dst_port) |
| 800 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 801 | #define foreach_ip4_proto_field \ |
| 802 | _(src_address) \ |
| 803 | _(dst_address) \ |
| 804 | _(tos) \ |
| 805 | _(length) \ |
| 806 | _(fragment_id) \ |
| 807 | _(ttl) \ |
| 808 | _(protocol) \ |
| 809 | _(checksum) |
| 810 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 811 | uword |
| 812 | unformat_tcp_mask (unformat_input_t * input, va_list * args) |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 813 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 814 | u8 **maskp = va_arg (*args, u8 **); |
| 815 | u8 *mask = 0; |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 816 | u8 found_something = 0; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 817 | tcp_header_t *tcp; |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 818 | |
| 819 | #define _(a) u8 a=0; |
| 820 | foreach_tcp_proto_field; |
| 821 | #undef _ |
| 822 | |
| 823 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 824 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 825 | if (0); |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 826 | #define _(a) else if (unformat (input, #a)) a=1; |
| 827 | foreach_tcp_proto_field |
| 828 | #undef _ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 829 | else |
| 830 | break; |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | #define _(a) found_something += a; |
| 834 | foreach_tcp_proto_field; |
| 835 | #undef _ |
| 836 | |
| 837 | if (found_something == 0) |
| 838 | return 0; |
| 839 | |
| 840 | vec_validate (mask, sizeof (*tcp) - 1); |
| 841 | |
| 842 | tcp = (tcp_header_t *) mask; |
| 843 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 844 | #define _(a) if (a) clib_memset (&tcp->a, 0xff, sizeof (tcp->a)); |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 845 | foreach_tcp_proto_field; |
| 846 | #undef _ |
| 847 | |
| 848 | *maskp = mask; |
| 849 | return 1; |
| 850 | } |
| 851 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 852 | uword |
| 853 | unformat_udp_mask (unformat_input_t * input, va_list * args) |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 854 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 855 | u8 **maskp = va_arg (*args, u8 **); |
| 856 | u8 *mask = 0; |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 857 | u8 found_something = 0; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 858 | udp_header_t *udp; |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 859 | |
| 860 | #define _(a) u8 a=0; |
| 861 | foreach_udp_proto_field; |
| 862 | #undef _ |
| 863 | |
| 864 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 865 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 866 | if (0); |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 867 | #define _(a) else if (unformat (input, #a)) a=1; |
| 868 | foreach_udp_proto_field |
| 869 | #undef _ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 870 | else |
| 871 | break; |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | #define _(a) found_something += a; |
| 875 | foreach_udp_proto_field; |
| 876 | #undef _ |
| 877 | |
| 878 | if (found_something == 0) |
| 879 | return 0; |
| 880 | |
| 881 | vec_validate (mask, sizeof (*udp) - 1); |
| 882 | |
| 883 | udp = (udp_header_t *) mask; |
| 884 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 885 | #define _(a) if (a) clib_memset (&udp->a, 0xff, sizeof (udp->a)); |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 886 | foreach_udp_proto_field; |
| 887 | #undef _ |
| 888 | |
| 889 | *maskp = mask; |
| 890 | return 1; |
| 891 | } |
| 892 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 893 | typedef struct |
| 894 | { |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 895 | u16 src_port, dst_port; |
| 896 | } tcpudp_header_t; |
| 897 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 898 | uword |
| 899 | unformat_l4_mask (unformat_input_t * input, va_list * args) |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 900 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 901 | u8 **maskp = va_arg (*args, u8 **); |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 902 | u16 src_port = 0, dst_port = 0; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 903 | tcpudp_header_t *tcpudp; |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 904 | |
| 905 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 906 | { |
| 907 | if (unformat (input, "tcp %U", unformat_tcp_mask, maskp)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 908 | return 1; |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 909 | else if (unformat (input, "udp %U", unformat_udp_mask, maskp)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 910 | return 1; |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 911 | else if (unformat (input, "src_port")) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 912 | src_port = 0xFFFF; |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 913 | else if (unformat (input, "dst_port")) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 914 | dst_port = 0xFFFF; |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 915 | else |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 916 | return 0; |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | if (!src_port && !dst_port) |
| 920 | return 0; |
| 921 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 922 | u8 *mask = 0; |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 923 | vec_validate (mask, sizeof (tcpudp_header_t) - 1); |
| 924 | |
| 925 | tcpudp = (tcpudp_header_t *) mask; |
| 926 | tcpudp->src_port = src_port; |
| 927 | tcpudp->dst_port = dst_port; |
| 928 | |
| 929 | *maskp = mask; |
| 930 | |
| 931 | return 1; |
| 932 | } |
| 933 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 934 | uword |
| 935 | unformat_ip4_mask (unformat_input_t * input, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 936 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 937 | u8 **maskp = va_arg (*args, u8 **); |
| 938 | u8 *mask = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 939 | u8 found_something = 0; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 940 | ip4_header_t *ip; |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 941 | u32 src_prefix_len = 32; |
| 942 | u32 src_prefix_mask = ~0; |
| 943 | u32 dst_prefix_len = 32; |
| 944 | u32 dst_prefix_mask = ~0; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 945 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 946 | #define _(a) u8 a=0; |
| 947 | foreach_ip4_proto_field; |
| 948 | #undef _ |
| 949 | u8 version = 0; |
| 950 | u8 hdr_length = 0; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 951 | |
| 952 | |
| 953 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 954 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 955 | if (unformat (input, "version")) |
| 956 | version = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 957 | else if (unformat (input, "hdr_length")) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 958 | hdr_length = 1; |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 959 | else if (unformat (input, "src/%d", &src_prefix_len)) |
| 960 | { |
| 961 | src_address = 1; |
| 962 | src_prefix_mask &= ~((1 << (32 - src_prefix_len)) - 1); |
| 963 | src_prefix_mask = clib_host_to_net_u32 (src_prefix_mask); |
| 964 | } |
| 965 | else if (unformat (input, "dst/%d", &dst_prefix_len)) |
| 966 | { |
| 967 | dst_address = 1; |
| 968 | dst_prefix_mask &= ~((1 << (32 - dst_prefix_len)) - 1); |
| 969 | dst_prefix_mask = clib_host_to_net_u32 (dst_prefix_mask); |
| 970 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 971 | else if (unformat (input, "src")) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 972 | src_address = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 973 | else if (unformat (input, "dst")) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 974 | dst_address = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 975 | else if (unformat (input, "proto")) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 976 | protocol = 1; |
| 977 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 978 | #define _(a) else if (unformat (input, #a)) a=1; |
| 979 | foreach_ip4_proto_field |
| 980 | #undef _ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 981 | else |
| 982 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 983 | } |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 984 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 985 | #define _(a) found_something += a; |
| 986 | foreach_ip4_proto_field; |
| 987 | #undef _ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 988 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 989 | if (found_something == 0) |
| 990 | return 0; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 991 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 992 | vec_validate (mask, sizeof (*ip) - 1); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 993 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 994 | ip = (ip4_header_t *) mask; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 995 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 996 | #define _(a) if (a) clib_memset (&ip->a, 0xff, sizeof (ip->a)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 997 | foreach_ip4_proto_field; |
| 998 | #undef _ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 999 | |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 1000 | if (src_address) |
| 1001 | ip->src_address.as_u32 = src_prefix_mask; |
| 1002 | |
| 1003 | if (dst_address) |
| 1004 | ip->dst_address.as_u32 = dst_prefix_mask; |
| 1005 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1006 | ip->ip_version_and_header_length = 0; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1007 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1008 | if (version) |
| 1009 | ip->ip_version_and_header_length |= 0xF0; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1010 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1011 | if (hdr_length) |
| 1012 | ip->ip_version_and_header_length |= 0x0F; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1013 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1014 | *maskp = mask; |
| 1015 | return 1; |
| 1016 | } |
| 1017 | |
| 1018 | #define foreach_ip6_proto_field \ |
| 1019 | _(src_address) \ |
| 1020 | _(dst_address) \ |
| 1021 | _(payload_length) \ |
| 1022 | _(hop_limit) \ |
| 1023 | _(protocol) |
| 1024 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1025 | uword |
| 1026 | unformat_ip6_mask (unformat_input_t * input, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1027 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1028 | u8 **maskp = va_arg (*args, u8 **); |
| 1029 | u8 *mask = 0; |
Dave Barach | 126c885 | 2020-06-30 08:28:06 -0400 | [diff] [blame] | 1030 | u8 found_something; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1031 | ip6_header_t *ip; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1032 | u32 ip_version_traffic_class_and_flow_label; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1033 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1034 | #define _(a) u8 a=0; |
| 1035 | foreach_ip6_proto_field; |
| 1036 | #undef _ |
| 1037 | u8 version = 0; |
| 1038 | u8 traffic_class = 0; |
| 1039 | u8 flow_label = 0; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1040 | |
| 1041 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1042 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1043 | if (unformat (input, "version")) |
| 1044 | version = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1045 | else if (unformat (input, "traffic-class")) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1046 | traffic_class = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1047 | else if (unformat (input, "flow-label")) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1048 | flow_label = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1049 | else if (unformat (input, "src")) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1050 | src_address = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1051 | else if (unformat (input, "dst")) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1052 | dst_address = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1053 | else if (unformat (input, "proto")) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1054 | protocol = 1; |
| 1055 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1056 | #define _(a) else if (unformat (input, #a)) a=1; |
| 1057 | foreach_ip6_proto_field |
| 1058 | #undef _ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1059 | else |
| 1060 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1061 | } |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1062 | |
Dave Barach | 126c885 | 2020-06-30 08:28:06 -0400 | [diff] [blame] | 1063 | /* Account for "special" field names */ |
| 1064 | found_something = version + traffic_class + flow_label |
| 1065 | + src_address + dst_address + protocol; |
| 1066 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1067 | #define _(a) found_something += a; |
| 1068 | foreach_ip6_proto_field; |
| 1069 | #undef _ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1070 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1071 | if (found_something == 0) |
| 1072 | return 0; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1073 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1074 | vec_validate (mask, sizeof (*ip) - 1); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1075 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1076 | ip = (ip6_header_t *) mask; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1077 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 1078 | #define _(a) if (a) clib_memset (&ip->a, 0xff, sizeof (ip->a)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1079 | foreach_ip6_proto_field; |
| 1080 | #undef _ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1081 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1082 | ip_version_traffic_class_and_flow_label = 0; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1083 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1084 | if (version) |
| 1085 | ip_version_traffic_class_and_flow_label |= 0xF0000000; |
| 1086 | |
| 1087 | if (traffic_class) |
| 1088 | ip_version_traffic_class_and_flow_label |= 0x0FF00000; |
| 1089 | |
| 1090 | if (flow_label) |
| 1091 | ip_version_traffic_class_and_flow_label |= 0x000FFFFF; |
| 1092 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1093 | ip->ip_version_traffic_class_and_flow_label = |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1094 | clib_host_to_net_u32 (ip_version_traffic_class_and_flow_label); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1095 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1096 | *maskp = mask; |
| 1097 | return 1; |
| 1098 | } |
| 1099 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1100 | uword |
| 1101 | unformat_l3_mask (unformat_input_t * input, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1102 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1103 | u8 **maskp = va_arg (*args, u8 **); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1104 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1105 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 1106 | { |
| 1107 | if (unformat (input, "ip4 %U", unformat_ip4_mask, maskp)) |
| 1108 | return 1; |
| 1109 | else if (unformat (input, "ip6 %U", unformat_ip6_mask, maskp)) |
| 1110 | return 1; |
| 1111 | else |
| 1112 | break; |
| 1113 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1114 | return 0; |
| 1115 | } |
| 1116 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1117 | uword |
| 1118 | unformat_l2_mask (unformat_input_t * input, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1119 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1120 | u8 **maskp = va_arg (*args, u8 **); |
| 1121 | u8 *mask = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1122 | u8 src = 0; |
| 1123 | u8 dst = 0; |
| 1124 | u8 proto = 0; |
| 1125 | u8 tag1 = 0; |
| 1126 | u8 tag2 = 0; |
| 1127 | u8 ignore_tag1 = 0; |
| 1128 | u8 ignore_tag2 = 0; |
| 1129 | u8 cos1 = 0; |
| 1130 | u8 cos2 = 0; |
| 1131 | u8 dot1q = 0; |
| 1132 | u8 dot1ad = 0; |
| 1133 | int len = 14; |
| 1134 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1135 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 1136 | { |
| 1137 | if (unformat (input, "src")) |
| 1138 | src = 1; |
| 1139 | else if (unformat (input, "dst")) |
| 1140 | dst = 1; |
| 1141 | else if (unformat (input, "proto")) |
| 1142 | proto = 1; |
| 1143 | else if (unformat (input, "tag1")) |
| 1144 | tag1 = 1; |
| 1145 | else if (unformat (input, "tag2")) |
| 1146 | tag2 = 1; |
| 1147 | else if (unformat (input, "ignore-tag1")) |
| 1148 | ignore_tag1 = 1; |
| 1149 | else if (unformat (input, "ignore-tag2")) |
| 1150 | ignore_tag2 = 1; |
| 1151 | else if (unformat (input, "cos1")) |
| 1152 | cos1 = 1; |
| 1153 | else if (unformat (input, "cos2")) |
| 1154 | cos2 = 1; |
| 1155 | else if (unformat (input, "dot1q")) |
| 1156 | dot1q = 1; |
| 1157 | else if (unformat (input, "dot1ad")) |
| 1158 | dot1ad = 1; |
| 1159 | else |
| 1160 | break; |
| 1161 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1162 | if ((src + dst + proto + tag1 + tag2 + dot1q + dot1ad + |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1163 | ignore_tag1 + ignore_tag2 + cos1 + cos2) == 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1164 | return 0; |
| 1165 | |
| 1166 | if (tag1 || ignore_tag1 || cos1 || dot1q) |
| 1167 | len = 18; |
| 1168 | if (tag2 || ignore_tag2 || cos2 || dot1ad) |
| 1169 | len = 22; |
| 1170 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1171 | vec_validate (mask, len - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1172 | |
| 1173 | if (dst) |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 1174 | clib_memset (mask, 0xff, 6); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1175 | |
| 1176 | if (src) |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 1177 | clib_memset (mask + 6, 0xff, 6); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1178 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1179 | if (tag2 || dot1ad) |
| 1180 | { |
| 1181 | /* inner vlan tag */ |
| 1182 | if (tag2) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1183 | { |
| 1184 | mask[19] = 0xff; |
| 1185 | mask[18] = 0x0f; |
| 1186 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1187 | if (cos2) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1188 | mask[18] |= 0xe0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1189 | if (proto) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1190 | mask[21] = mask[20] = 0xff; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1191 | if (tag1) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1192 | { |
| 1193 | mask[15] = 0xff; |
| 1194 | mask[14] = 0x0f; |
| 1195 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1196 | if (cos1) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1197 | mask[14] |= 0xe0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1198 | *maskp = mask; |
| 1199 | return 1; |
| 1200 | } |
| 1201 | if (tag1 | dot1q) |
| 1202 | { |
| 1203 | if (tag1) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1204 | { |
| 1205 | mask[15] = 0xff; |
| 1206 | mask[14] = 0x0f; |
| 1207 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1208 | if (cos1) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1209 | mask[14] |= 0xe0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1210 | if (proto) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1211 | mask[16] = mask[17] = 0xff; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1212 | *maskp = mask; |
| 1213 | return 1; |
| 1214 | } |
| 1215 | if (cos2) |
| 1216 | mask[18] |= 0xe0; |
| 1217 | if (cos1) |
| 1218 | mask[14] |= 0xe0; |
| 1219 | if (proto) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1220 | mask[12] = mask[13] = 0xff; |
| 1221 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1222 | *maskp = mask; |
| 1223 | return 1; |
| 1224 | } |
| 1225 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1226 | uword |
| 1227 | unformat_classify_mask (unformat_input_t * input, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1228 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1229 | u8 **maskp = va_arg (*args, u8 **); |
| 1230 | u32 *skipp = va_arg (*args, u32 *); |
| 1231 | u32 *matchp = va_arg (*args, u32 *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1232 | u32 match; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1233 | u8 *mask = 0; |
| 1234 | u8 *l2 = 0; |
| 1235 | u8 *l3 = 0; |
| 1236 | u8 *l4 = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1237 | int i; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1238 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1239 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 1240 | { |
| 1241 | if (unformat (input, "hex %U", unformat_hex_string, &mask)) |
| 1242 | ; |
| 1243 | else if (unformat (input, "l2 %U", unformat_l2_mask, &l2)) |
| 1244 | ; |
| 1245 | else if (unformat (input, "l3 %U", unformat_l3_mask, &l3)) |
| 1246 | ; |
| 1247 | else if (unformat (input, "l4 %U", unformat_l4_mask, &l4)) |
| 1248 | ; |
| 1249 | else |
| 1250 | break; |
| 1251 | } |
| 1252 | |
| 1253 | if (l4 && !l3) |
| 1254 | { |
| 1255 | vec_free (mask); |
| 1256 | vec_free (l2); |
| 1257 | vec_free (l4); |
| 1258 | return 0; |
| 1259 | } |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 1260 | |
| 1261 | if (mask || l2 || l3 || l4) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1262 | { |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 1263 | if (l2 || l3 || l4) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1264 | { |
| 1265 | /* "With a free Ethernet header in every package" */ |
| 1266 | if (l2 == 0) |
| 1267 | vec_validate (l2, 13); |
| 1268 | mask = l2; |
| 1269 | if (l3) |
| 1270 | { |
| 1271 | vec_append (mask, l3); |
| 1272 | vec_free (l3); |
| 1273 | } |
| 1274 | if (l4) |
| 1275 | { |
| 1276 | vec_append (mask, l4); |
| 1277 | vec_free (l4); |
| 1278 | } |
| 1279 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1280 | |
| 1281 | /* Scan forward looking for the first significant mask octet */ |
| 1282 | for (i = 0; i < vec_len (mask); i++) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1283 | if (mask[i]) |
| 1284 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1285 | |
| 1286 | /* compute (skip, match) params */ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1287 | *skipp = i / sizeof (u32x4); |
| 1288 | vec_delete (mask, *skipp * sizeof (u32x4), 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1289 | |
| 1290 | /* Pad mask to an even multiple of the vector size */ |
| 1291 | while (vec_len (mask) % sizeof (u32x4)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1292 | vec_add1 (mask, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1293 | |
| 1294 | match = vec_len (mask) / sizeof (u32x4); |
| 1295 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1296 | for (i = match * sizeof (u32x4); i > 0; i -= sizeof (u32x4)) |
| 1297 | { |
| 1298 | u64 *tmp = (u64 *) (mask + (i - sizeof (u32x4))); |
| 1299 | if (*tmp || *(tmp + 1)) |
| 1300 | break; |
| 1301 | match--; |
| 1302 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1303 | if (match == 0) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1304 | clib_warning ("BUG: match 0"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1305 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1306 | _vec_len (mask) = match * sizeof (u32x4); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1307 | |
| 1308 | *matchp = match; |
| 1309 | *maskp = mask; |
| 1310 | |
| 1311 | return 1; |
| 1312 | } |
| 1313 | |
| 1314 | return 0; |
| 1315 | } |
| 1316 | |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 1317 | #define foreach_l2_input_next \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1318 | _(drop, DROP) \ |
| 1319 | _(ethernet, ETHERNET_INPUT) \ |
| 1320 | _(ip4, IP4_INPUT) \ |
| 1321 | _(ip6, IP6_INPUT) \ |
| 1322 | _(li, LI) |
| 1323 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1324 | uword |
| 1325 | unformat_l2_input_next_index (unformat_input_t * input, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1326 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1327 | vnet_classify_main_t *cm = &vnet_classify_main; |
| 1328 | u32 *miss_next_indexp = va_arg (*args, u32 *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1329 | u32 next_index = 0; |
| 1330 | u32 tmp; |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 1331 | int i; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1332 | |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 1333 | /* First try registered unformat fns, allowing override... */ |
| 1334 | for (i = 0; i < vec_len (cm->unformat_l2_next_index_fns); i++) |
| 1335 | { |
| 1336 | if (unformat (input, "%U", cm->unformat_l2_next_index_fns[i], &tmp)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1337 | { |
| 1338 | next_index = tmp; |
| 1339 | goto out; |
| 1340 | } |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 1341 | } |
| 1342 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1343 | #define _(n,N) \ |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 1344 | if (unformat (input, #n)) { next_index = L2_INPUT_CLASSIFY_NEXT_##N; goto out;} |
| 1345 | foreach_l2_input_next; |
| 1346 | #undef _ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1347 | |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 1348 | if (unformat (input, "%d", &tmp)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1349 | { |
| 1350 | next_index = tmp; |
| 1351 | goto out; |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 1352 | } |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1353 | |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 1354 | return 0; |
| 1355 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1356 | out: |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 1357 | *miss_next_indexp = next_index; |
| 1358 | return 1; |
| 1359 | } |
| 1360 | |
| 1361 | #define foreach_l2_output_next \ |
| 1362 | _(drop, DROP) |
| 1363 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1364 | uword |
| 1365 | unformat_l2_output_next_index (unformat_input_t * input, va_list * args) |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 1366 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1367 | vnet_classify_main_t *cm = &vnet_classify_main; |
| 1368 | u32 *miss_next_indexp = va_arg (*args, u32 *); |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 1369 | u32 next_index = 0; |
| 1370 | u32 tmp; |
| 1371 | int i; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1372 | |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 1373 | /* First try registered unformat fns, allowing override... */ |
| 1374 | for (i = 0; i < vec_len (cm->unformat_l2_next_index_fns); i++) |
| 1375 | { |
| 1376 | if (unformat (input, "%U", cm->unformat_l2_next_index_fns[i], &tmp)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1377 | { |
| 1378 | next_index = tmp; |
| 1379 | goto out; |
| 1380 | } |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 1381 | } |
| 1382 | |
| 1383 | #define _(n,N) \ |
| 1384 | if (unformat (input, #n)) { next_index = L2_OUTPUT_CLASSIFY_NEXT_##N; goto out;} |
| 1385 | foreach_l2_output_next; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1386 | #undef _ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1387 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1388 | if (unformat (input, "%d", &tmp)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1389 | { |
| 1390 | next_index = tmp; |
| 1391 | goto out; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1392 | } |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1393 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1394 | return 0; |
| 1395 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1396 | out: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1397 | *miss_next_indexp = next_index; |
| 1398 | return 1; |
| 1399 | } |
| 1400 | |
| 1401 | #define foreach_ip_next \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1402 | _(drop, DROP) \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1403 | _(rewrite, REWRITE) |
| 1404 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1405 | uword |
| 1406 | unformat_ip_next_index (unformat_input_t * input, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1407 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1408 | u32 *miss_next_indexp = va_arg (*args, u32 *); |
| 1409 | vnet_classify_main_t *cm = &vnet_classify_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1410 | u32 next_index = 0; |
| 1411 | u32 tmp; |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 1412 | int i; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1413 | |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 1414 | /* First try registered unformat fns, allowing override... */ |
| 1415 | for (i = 0; i < vec_len (cm->unformat_ip_next_index_fns); i++) |
| 1416 | { |
| 1417 | if (unformat (input, "%U", cm->unformat_ip_next_index_fns[i], &tmp)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1418 | { |
| 1419 | next_index = tmp; |
| 1420 | goto out; |
| 1421 | } |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 1422 | } |
| 1423 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1424 | #define _(n,N) \ |
| 1425 | if (unformat (input, #n)) { next_index = IP_LOOKUP_NEXT_##N; goto out;} |
| 1426 | foreach_ip_next; |
| 1427 | #undef _ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1428 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1429 | if (unformat (input, "%d", &tmp)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1430 | { |
| 1431 | next_index = tmp; |
| 1432 | goto out; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1433 | } |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1434 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1435 | return 0; |
| 1436 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1437 | out: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1438 | *miss_next_indexp = next_index; |
| 1439 | return 1; |
| 1440 | } |
| 1441 | |
| 1442 | #define foreach_acl_next \ |
| 1443 | _(deny, DENY) |
| 1444 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1445 | uword |
| 1446 | unformat_acl_next_index (unformat_input_t * input, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1447 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1448 | u32 *next_indexp = va_arg (*args, u32 *); |
| 1449 | vnet_classify_main_t *cm = &vnet_classify_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1450 | u32 next_index = 0; |
| 1451 | u32 tmp; |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 1452 | int i; |
| 1453 | |
| 1454 | /* First try registered unformat fns, allowing override... */ |
| 1455 | for (i = 0; i < vec_len (cm->unformat_acl_next_index_fns); i++) |
| 1456 | { |
| 1457 | if (unformat (input, "%U", cm->unformat_acl_next_index_fns[i], &tmp)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1458 | { |
| 1459 | next_index = tmp; |
| 1460 | goto out; |
| 1461 | } |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 1462 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1463 | |
| 1464 | #define _(n,N) \ |
| 1465 | if (unformat (input, #n)) { next_index = ACL_NEXT_INDEX_##N; goto out;} |
| 1466 | foreach_acl_next; |
| 1467 | #undef _ |
| 1468 | |
| 1469 | if (unformat (input, "permit")) |
| 1470 | { |
| 1471 | next_index = ~0; |
| 1472 | goto out; |
| 1473 | } |
| 1474 | else if (unformat (input, "%d", &tmp)) |
| 1475 | { |
| 1476 | next_index = tmp; |
| 1477 | goto out; |
| 1478 | } |
| 1479 | |
| 1480 | return 0; |
| 1481 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1482 | out: |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 1483 | *next_indexp = next_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1484 | return 1; |
| 1485 | } |
| 1486 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1487 | uword |
| 1488 | unformat_policer_next_index (unformat_input_t * input, va_list * args) |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 1489 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1490 | u32 *next_indexp = va_arg (*args, u32 *); |
| 1491 | vnet_classify_main_t *cm = &vnet_classify_main; |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 1492 | u32 next_index = 0; |
| 1493 | u32 tmp; |
| 1494 | int i; |
| 1495 | |
| 1496 | /* First try registered unformat fns, allowing override... */ |
| 1497 | for (i = 0; i < vec_len (cm->unformat_policer_next_index_fns); i++) |
| 1498 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1499 | if (unformat |
| 1500 | (input, "%U", cm->unformat_policer_next_index_fns[i], &tmp)) |
| 1501 | { |
| 1502 | next_index = tmp; |
| 1503 | goto out; |
| 1504 | } |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 1505 | } |
| 1506 | |
| 1507 | if (unformat (input, "%d", &tmp)) |
| 1508 | { |
| 1509 | next_index = tmp; |
| 1510 | goto out; |
| 1511 | } |
| 1512 | |
| 1513 | return 0; |
| 1514 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1515 | out: |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 1516 | *next_indexp = next_index; |
| 1517 | return 1; |
| 1518 | } |
| 1519 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1520 | static clib_error_t * |
| 1521 | classify_table_command_fn (vlib_main_t * vm, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1522 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1523 | { |
| 1524 | u32 nbuckets = 2; |
| 1525 | u32 skip = ~0; |
| 1526 | u32 match = ~0; |
| 1527 | int is_add = 1; |
Juraj Sloboda | 288e893 | 2016-12-06 21:25:19 +0100 | [diff] [blame] | 1528 | int del_chain = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1529 | u32 table_index = ~0; |
| 1530 | u32 next_table_index = ~0; |
| 1531 | u32 miss_next_index = ~0; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1532 | u32 memory_size = 2 << 20; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1533 | u32 tmp; |
Steve Shin | 25e26dc | 2016-11-08 10:47:10 -0800 | [diff] [blame] | 1534 | u32 current_data_flag = 0; |
| 1535 | int current_data_offset = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1536 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1537 | u8 *mask = 0; |
| 1538 | vnet_classify_main_t *cm = &vnet_classify_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1539 | int rv; |
| 1540 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1541 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 1542 | { |
| 1543 | if (unformat (input, "del")) |
Juraj Sloboda | 288e893 | 2016-12-06 21:25:19 +0100 | [diff] [blame] | 1544 | is_add = 0; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1545 | else if (unformat (input, "del-chain")) |
| 1546 | { |
| 1547 | is_add = 0; |
| 1548 | del_chain = 1; |
| 1549 | } |
| 1550 | else if (unformat (input, "buckets %d", &nbuckets)) |
| 1551 | ; |
| 1552 | else if (unformat (input, "skip %d", &skip)) |
| 1553 | ; |
| 1554 | else if (unformat (input, "match %d", &match)) |
| 1555 | ; |
| 1556 | else if (unformat (input, "table %d", &table_index)) |
| 1557 | ; |
| 1558 | else if (unformat (input, "mask %U", unformat_classify_mask, |
| 1559 | &mask, &skip, &match)) |
| 1560 | ; |
| 1561 | else if (unformat (input, "memory-size %uM", &tmp)) |
| 1562 | memory_size = tmp << 20; |
| 1563 | else if (unformat (input, "memory-size %uG", &tmp)) |
| 1564 | memory_size = tmp << 30; |
| 1565 | else if (unformat (input, "next-table %d", &next_table_index)) |
| 1566 | ; |
| 1567 | else if (unformat (input, "miss-next %U", unformat_ip_next_index, |
| 1568 | &miss_next_index)) |
| 1569 | ; |
| 1570 | else |
| 1571 | if (unformat |
| 1572 | (input, "l2-input-miss-next %U", unformat_l2_input_next_index, |
| 1573 | &miss_next_index)) |
| 1574 | ; |
| 1575 | else |
| 1576 | if (unformat |
| 1577 | (input, "l2-output-miss-next %U", unformat_l2_output_next_index, |
| 1578 | &miss_next_index)) |
| 1579 | ; |
| 1580 | else if (unformat (input, "acl-miss-next %U", unformat_acl_next_index, |
| 1581 | &miss_next_index)) |
| 1582 | ; |
| 1583 | else if (unformat (input, "current-data-flag %d", ¤t_data_flag)) |
| 1584 | ; |
| 1585 | else |
| 1586 | if (unformat (input, "current-data-offset %d", ¤t_data_offset)) |
| 1587 | ; |
Steve Shin | 25e26dc | 2016-11-08 10:47:10 -0800 | [diff] [blame] | 1588 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1589 | else |
| 1590 | break; |
| 1591 | } |
| 1592 | |
Steve Shin | 25e26dc | 2016-11-08 10:47:10 -0800 | [diff] [blame] | 1593 | if (is_add && mask == 0 && table_index == ~0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1594 | return clib_error_return (0, "Mask required"); |
| 1595 | |
Steve Shin | 25e26dc | 2016-11-08 10:47:10 -0800 | [diff] [blame] | 1596 | if (is_add && skip == ~0 && table_index == ~0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1597 | return clib_error_return (0, "skip count required"); |
| 1598 | |
Steve Shin | 25e26dc | 2016-11-08 10:47:10 -0800 | [diff] [blame] | 1599 | if (is_add && match == ~0 && table_index == ~0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1600 | return clib_error_return (0, "match count required"); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1601 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1602 | if (!is_add && table_index == ~0) |
| 1603 | return clib_error_return (0, "table index required for delete"); |
| 1604 | |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 1605 | rv = vnet_classify_add_del_table (cm, mask, nbuckets, (u32) memory_size, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1606 | skip, match, next_table_index, |
| 1607 | miss_next_index, &table_index, |
| 1608 | current_data_flag, current_data_offset, |
| 1609 | is_add, del_chain); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1610 | switch (rv) |
| 1611 | { |
| 1612 | case 0: |
| 1613 | break; |
| 1614 | |
| 1615 | default: |
| 1616 | return clib_error_return (0, "vnet_classify_add_del_table returned %d", |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1617 | rv); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1618 | } |
| 1619 | return 0; |
| 1620 | } |
| 1621 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1622 | /* *INDENT-OFF* */ |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 1623 | VLIB_CLI_COMMAND (classify_table, static) = |
| 1624 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1625 | .path = "classify table", |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1626 | .short_help = |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1627 | "classify table [miss-next|l2-miss_next|acl-miss-next <next_index>]" |
Steve Shin | 25e26dc | 2016-11-08 10:47:10 -0800 | [diff] [blame] | 1628 | "\n mask <mask-value> buckets <nn> [skip <n>] [match <n>]" |
Juraj Sloboda | 288e893 | 2016-12-06 21:25:19 +0100 | [diff] [blame] | 1629 | "\n [current-data-flag <n>] [current-data-offset <n>] [table <n>]" |
Hongjun Ni | 8184ebd | 2017-10-25 20:47:56 +0800 | [diff] [blame] | 1630 | "\n [memory-size <nn>[M][G]] [next-table <n>]" |
Juraj Sloboda | 288e893 | 2016-12-06 21:25:19 +0100 | [diff] [blame] | 1631 | "\n [del] [del-chain]", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1632 | .function = classify_table_command_fn, |
| 1633 | }; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1634 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1635 | |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 1636 | static int |
| 1637 | filter_table_mask_compare (void *a1, void *a2) |
| 1638 | { |
| 1639 | vnet_classify_main_t *cm = &vnet_classify_main; |
| 1640 | u32 *ti1 = a1; |
| 1641 | u32 *ti2 = a2; |
| 1642 | u32 n1 = 0, n2 = 0; |
| 1643 | vnet_classify_table_t *t1, *t2; |
| 1644 | u8 *m1, *m2; |
| 1645 | int i; |
| 1646 | |
| 1647 | t1 = pool_elt_at_index (cm->tables, *ti1); |
| 1648 | t2 = pool_elt_at_index (cm->tables, *ti2); |
| 1649 | |
| 1650 | m1 = (u8 *) (t1->mask); |
| 1651 | m2 = (u8 *) (t2->mask); |
| 1652 | |
| 1653 | for (i = 0; i < vec_len (t1->mask) * sizeof (u32x4); i++) |
| 1654 | { |
| 1655 | n1 += count_set_bits (m1[0]); |
| 1656 | m1++; |
| 1657 | } |
| 1658 | |
| 1659 | for (i = 0; i < vec_len (t2->mask) * sizeof (u32x4); i++) |
| 1660 | { |
| 1661 | n2 += count_set_bits (m2[0]); |
| 1662 | m2++; |
| 1663 | } |
| 1664 | |
| 1665 | /* Reverse sort: descending number of set bits */ |
| 1666 | if (n1 < n2) |
| 1667 | return 1; |
| 1668 | else if (n1 > n2) |
| 1669 | return -1; |
| 1670 | else |
| 1671 | return 0; |
| 1672 | } |
| 1673 | |
| 1674 | static clib_error_t * |
| 1675 | classify_filter_command_fn (vlib_main_t * vm, |
| 1676 | unformat_input_t * input, |
| 1677 | vlib_cli_command_t * cmd) |
| 1678 | { |
| 1679 | u32 nbuckets = 8; |
| 1680 | vnet_main_t *vnm = vnet_get_main (); |
| 1681 | uword memory_size = (uword) (128 << 10); |
| 1682 | u32 skip = ~0; |
| 1683 | u32 match = ~0; |
| 1684 | u8 *match_vector; |
| 1685 | int is_add = 1; |
| 1686 | int del_chain = 0; |
| 1687 | u32 table_index = ~0; |
| 1688 | u32 next_table_index = ~0; |
| 1689 | u32 miss_next_index = ~0; |
| 1690 | u32 current_data_flag = 0; |
| 1691 | int current_data_offset = 0; |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 1692 | u32 sw_if_index = ~0; |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 1693 | int pkt_trace = 0; |
Dave Barach | 29c6132 | 2019-12-24 16:59:38 -0500 | [diff] [blame] | 1694 | int pcap = 0; |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 1695 | int i; |
| 1696 | vnet_classify_table_t *t; |
| 1697 | u8 *mask = 0; |
| 1698 | vnet_classify_main_t *cm = &vnet_classify_main; |
| 1699 | int rv = 0; |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 1700 | vnet_classify_filter_set_t *set = 0; |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 1701 | u32 set_index = ~0; |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 1702 | |
| 1703 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 1704 | { |
| 1705 | if (unformat (input, "del")) |
| 1706 | is_add = 0; |
Dave Barach | 29c6132 | 2019-12-24 16:59:38 -0500 | [diff] [blame] | 1707 | else if (unformat (input, "pcap %=", &pcap, 1)) |
| 1708 | sw_if_index = 0; |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 1709 | else if (unformat (input, "trace")) |
| 1710 | pkt_trace = 1; |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 1711 | else if (unformat (input, "%U", |
| 1712 | unformat_vnet_sw_interface, vnm, &sw_if_index)) |
Dave Barach | 29c6132 | 2019-12-24 16:59:38 -0500 | [diff] [blame] | 1713 | { |
| 1714 | if (sw_if_index == 0) |
| 1715 | return clib_error_return (0, "Local interface not supported..."); |
| 1716 | } |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 1717 | else if (unformat (input, "buckets %d", &nbuckets)) |
| 1718 | ; |
| 1719 | else if (unformat (input, "mask %U", unformat_classify_mask, |
| 1720 | &mask, &skip, &match)) |
| 1721 | ; |
| 1722 | else if (unformat (input, "memory-size %U", unformat_memory_size, |
| 1723 | &memory_size)) |
| 1724 | ; |
| 1725 | else |
| 1726 | break; |
| 1727 | } |
| 1728 | |
| 1729 | if (is_add && mask == 0 && table_index == ~0) |
| 1730 | return clib_error_return (0, "Mask required"); |
| 1731 | |
| 1732 | if (is_add && skip == ~0 && table_index == ~0) |
| 1733 | return clib_error_return (0, "skip count required"); |
| 1734 | |
| 1735 | if (is_add && match == ~0 && table_index == ~0) |
| 1736 | return clib_error_return (0, "match count required"); |
| 1737 | |
Dave Barach | 29c6132 | 2019-12-24 16:59:38 -0500 | [diff] [blame] | 1738 | if (sw_if_index == ~0 && pkt_trace == 0 && pcap == 0) |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 1739 | return clib_error_return (0, "Must specify trace, pcap or interface..."); |
| 1740 | |
Dave Barach | 196fce2 | 2020-01-27 09:56:58 -0500 | [diff] [blame] | 1741 | if (pkt_trace && pcap) |
| 1742 | return clib_error_return |
| 1743 | (0, "Packet trace and pcap are mutually exclusive..."); |
| 1744 | |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 1745 | if (pkt_trace && sw_if_index != ~0) |
| 1746 | return clib_error_return (0, "Packet trace filter is per-system"); |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 1747 | |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 1748 | if (!is_add) |
| 1749 | { |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 1750 | |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 1751 | if (pkt_trace) |
| 1752 | set_index = vlib_global_main.trace_filter.trace_filter_set_index; |
| 1753 | else if (sw_if_index < vec_len (cm->filter_set_by_sw_if_index)) |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 1754 | set_index = cm->filter_set_by_sw_if_index[sw_if_index]; |
| 1755 | |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 1756 | if (set_index == ~0) |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 1757 | { |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 1758 | if (pkt_trace) |
| 1759 | return clib_error_return (0, |
| 1760 | "No pkt trace classify filter set..."); |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 1761 | if (sw_if_index == 0) |
| 1762 | return clib_error_return (0, "No pcap classify filter set..."); |
| 1763 | else |
| 1764 | return clib_error_return (0, "No classify filter set for %U...", |
| 1765 | format_vnet_sw_if_index_name, vnm, |
| 1766 | sw_if_index); |
| 1767 | } |
| 1768 | |
| 1769 | set = pool_elt_at_index (cm->filter_sets, set_index); |
| 1770 | |
| 1771 | set->refcnt--; |
| 1772 | ASSERT (set->refcnt >= 0); |
| 1773 | if (set->refcnt == 0) |
| 1774 | { |
| 1775 | del_chain = 1; |
| 1776 | table_index = set->table_indices[0]; |
| 1777 | vec_reset_length (set->table_indices); |
| 1778 | pool_put (cm->filter_sets, set); |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 1779 | if (pkt_trace) |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 1780 | { |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 1781 | vlib_global_main.trace_filter.trace_filter_set_index = ~0; |
| 1782 | vlib_global_main.trace_filter.trace_classify_table_index = ~0; |
| 1783 | } |
| 1784 | else |
| 1785 | { |
| 1786 | cm->filter_set_by_sw_if_index[sw_if_index] = ~0; |
| 1787 | if (sw_if_index > 0) |
| 1788 | { |
| 1789 | vnet_hw_interface_t *hi = |
| 1790 | vnet_get_sup_hw_interface (vnm, sw_if_index); |
| 1791 | hi->trace_classify_table_index = ~0; |
| 1792 | } |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 1793 | } |
| 1794 | } |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 1795 | } |
| 1796 | |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 1797 | if (is_add) |
| 1798 | { |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 1799 | if (pkt_trace) |
| 1800 | set_index = vlib_global_main.trace_filter.trace_filter_set_index; |
| 1801 | else if (sw_if_index < vec_len (cm->filter_set_by_sw_if_index)) |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 1802 | set_index = cm->filter_set_by_sw_if_index[sw_if_index]; |
| 1803 | |
| 1804 | /* Do we have a filter set for this intfc / pcap yet? */ |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 1805 | if (set_index == ~0) |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 1806 | { |
| 1807 | pool_get (cm->filter_sets, set); |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 1808 | set_index = set - cm->filter_sets; |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 1809 | set->refcnt = 1; |
| 1810 | } |
| 1811 | else |
| 1812 | set = pool_elt_at_index (cm->filter_sets, set_index); |
| 1813 | |
Jon Loeliger | 362c666 | 2020-09-21 16:48:54 -0500 | [diff] [blame] | 1814 | ASSERT (set); |
| 1815 | |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 1816 | for (i = 0; i < vec_len (set->table_indices); i++) |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 1817 | { |
Jon Loeliger | 362c666 | 2020-09-21 16:48:54 -0500 | [diff] [blame] | 1818 | t = pool_elt_at_index (cm->tables, set->table_indices[i]); |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 1819 | /* classifier geometry mismatch, can't use this table */ |
| 1820 | if (t->match_n_vectors != match || t->skip_n_vectors != skip) |
| 1821 | continue; |
| 1822 | /* Masks aren't congruent, can't use this table */ |
| 1823 | if (vec_len (t->mask) != vec_len (mask)) |
| 1824 | continue; |
| 1825 | /* Masks aren't bit-for-bit identical, can't use this table */ |
| 1826 | if (memcmp (t->mask, mask, vec_len (mask))) |
| 1827 | continue; |
| 1828 | |
| 1829 | /* Winner... */ |
Jon Loeliger | 362c666 | 2020-09-21 16:48:54 -0500 | [diff] [blame] | 1830 | table_index = set->table_indices[i]; |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 1831 | goto found_table; |
| 1832 | } |
| 1833 | } |
| 1834 | |
| 1835 | rv = vnet_classify_add_del_table (cm, mask, nbuckets, memory_size, |
| 1836 | skip, match, next_table_index, |
| 1837 | miss_next_index, &table_index, |
| 1838 | current_data_flag, current_data_offset, |
| 1839 | is_add, del_chain); |
| 1840 | vec_free (mask); |
| 1841 | |
| 1842 | switch (rv) |
| 1843 | { |
| 1844 | case 0: |
| 1845 | break; |
| 1846 | |
| 1847 | default: |
| 1848 | return clib_error_return (0, "vnet_classify_add_del_table returned %d", |
| 1849 | rv); |
| 1850 | } |
| 1851 | |
| 1852 | if (is_add == 0) |
| 1853 | return 0; |
| 1854 | |
| 1855 | /* Remember the table */ |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 1856 | vec_add1 (set->table_indices, table_index); |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 1857 | |
| 1858 | if (pkt_trace) |
| 1859 | vlib_global_main.trace_filter.trace_filter_set_index = set_index; |
| 1860 | else |
| 1861 | { |
| 1862 | vec_validate_init_empty (cm->filter_set_by_sw_if_index, sw_if_index, |
| 1863 | ~0); |
| 1864 | cm->filter_set_by_sw_if_index[sw_if_index] = set - cm->filter_sets; |
| 1865 | } |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 1866 | |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 1867 | /* Sort filter tables from most-specific mask to least-specific mask */ |
| 1868 | vec_sort_with_function (set->table_indices, filter_table_mask_compare); |
| 1869 | |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 1870 | /* Setup next_table_index fields */ |
| 1871 | for (i = 0; i < vec_len (set->table_indices); i++) |
| 1872 | { |
| 1873 | t = pool_elt_at_index (cm->tables, set->table_indices[i]); |
| 1874 | |
| 1875 | if ((i + 1) < vec_len (set->table_indices)) |
| 1876 | t->next_table_index = set->table_indices[i + 1]; |
| 1877 | else |
| 1878 | t->next_table_index = ~0; |
| 1879 | } |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 1880 | |
Jon Loeliger | 362c666 | 2020-09-21 16:48:54 -0500 | [diff] [blame] | 1881 | /* Put top table index where device drivers can find them */ |
| 1882 | if (sw_if_index > 0 && pkt_trace == 0) |
| 1883 | { |
| 1884 | vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index); |
| 1885 | ASSERT (vec_len (set->table_indices) > 0); |
| 1886 | hi->trace_classify_table_index = set->table_indices[0]; |
| 1887 | } |
| 1888 | |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 1889 | found_table: |
| 1890 | |
| 1891 | /* Now try to parse a session */ |
| 1892 | if (unformat (input, "match %U", unformat_classify_match, |
| 1893 | cm, &match_vector, table_index) == 0) |
| 1894 | return 0; |
| 1895 | |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 1896 | /* |
| 1897 | * We use hit or miss to determine whether to trace or pcap pkts |
| 1898 | * so the session setup is very limited |
| 1899 | */ |
| 1900 | rv = vnet_classify_add_del_session (cm, table_index, |
| 1901 | match_vector, 0 /* hit_next_index */ , |
| 1902 | 0 /* opaque_index */ , |
| 1903 | 0 /* advance */ , |
| 1904 | 0 /* action */ , |
| 1905 | 0 /* metadata */ , |
| 1906 | 1 /* is_add */ ); |
| 1907 | |
| 1908 | vec_free (match_vector); |
| 1909 | |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 1910 | return 0; |
| 1911 | } |
| 1912 | |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 1913 | /** Enable / disable packet trace filter */ |
| 1914 | int |
| 1915 | vlib_enable_disable_pkt_trace_filter (int enable) |
| 1916 | { |
| 1917 | if (enable) |
| 1918 | { |
| 1919 | vnet_classify_main_t *cm = &vnet_classify_main; |
| 1920 | vnet_classify_filter_set_t *set; |
| 1921 | u32 set_index = vlib_global_main.trace_filter.trace_filter_set_index; |
| 1922 | |
| 1923 | if (set_index == ~0) |
| 1924 | return -1; |
| 1925 | |
| 1926 | set = pool_elt_at_index (cm->filter_sets, set_index); |
| 1927 | vlib_global_main.trace_filter.trace_classify_table_index = |
| 1928 | set->table_indices[0]; |
| 1929 | vlib_global_main.trace_filter.trace_filter_enable = 1; |
| 1930 | } |
| 1931 | else |
| 1932 | { |
| 1933 | vlib_global_main.trace_filter.trace_filter_enable = 0; |
| 1934 | } |
| 1935 | return 0; |
| 1936 | } |
| 1937 | |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 1938 | /*? |
| 1939 | * Construct an arbitrary set of packet classifier tables for use with |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 1940 | * "pcap rx | tx trace," and with the vpp packet tracer |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 1941 | * |
| 1942 | * Packets which match a rule in the classifier table chain |
| 1943 | * will be traced. The tables are automatically ordered so that |
| 1944 | * matches in the most specific table are tried first. |
| 1945 | * |
| 1946 | * It's reasonably likely that folks will configure a single |
| 1947 | * table with one or two matches. As a result, we configure |
| 1948 | * 8 hash buckets and 128K of match rule space. One can override |
| 1949 | * the defaults by specifiying "buckets <nnn>" and "memory-size <xxx>" |
| 1950 | * as desired. |
| 1951 | * |
| 1952 | * To build up complex filter chains, repeatedly issue the |
| 1953 | * classify filter debug CLI command. Each command must specify the desired |
| 1954 | * mask and match values. If a classifier table with a suitable mask |
| 1955 | * already exists, the CLI command adds a match rule to the existing table. |
| 1956 | * If not, the CLI command add a new table and the indicated mask rule |
| 1957 | * |
| 1958 | * Here is a terse description of the "mask <xxx>" syntax: |
| 1959 | * |
| 1960 | * l2 src dst proto tag1 tag2 ignore-tag1 ignore-tag2 cos1 cos2 dot1q dot1ad |
| 1961 | * |
| 1962 | * l3 ip4 <ip4-mask> ip6 <ip6-mask> |
| 1963 | * |
| 1964 | * <ip4-mask> version hdr_length src[/width] dst[/width] |
| 1965 | * tos length fragment_id ttl protocol checksum |
| 1966 | * |
| 1967 | * <ip6-mask> version traffic-class flow-label src dst proto |
| 1968 | * payload_length hop_limit protocol |
| 1969 | * |
| 1970 | * l4 tcp <tcp-mask> udp <udp_mask> src_port dst_port |
| 1971 | * |
| 1972 | * <tcp-mask> src dst # ports |
| 1973 | * |
| 1974 | * <udp-mask> src_port dst_port |
| 1975 | * |
| 1976 | * To construct matches, add the values to match after the indicated keywords: |
| 1977 | * in the match syntax. For example: |
| 1978 | * mask l3 ip4 src -> match l3 ip4 src 192.168.1.11 |
| 1979 | * |
| 1980 | * @cliexpar |
| 1981 | * Configuring the classify filter |
| 1982 | * |
| 1983 | * Configure a simple classify filter, and configure pcap rx trace to use it: |
| 1984 | * |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 1985 | * <b><em>classify filter rx mask l3 ip4 src match l3 ip4 src 192.168.1.11"</em></b><br> |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 1986 | * <b><em>pcap rx trace on max 100 filter</em></b> |
| 1987 | * |
| 1988 | * Configure another fairly simple filter |
| 1989 | * |
| 1990 | * <b><em>classify filter mask l3 ip4 src dst match l3 ip4 src 192.168.1.10 dst 192.168.2.10"</em></b> |
| 1991 | * |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 1992 | * |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 1993 | * Configure a filter for use with the vpp packet tracer: |
| 1994 | * <b><em>classify filter trace mask l3 ip4 src dst match l3 ip4 src 192.168.1.10 dst 192.168.2.10"</em></b> |
| 1995 | * <b><em>trace add dpdk-input 100 filter</em></b> |
| 1996 | * |
| 1997 | * Clear classifier filters |
| 1998 | * |
| 1999 | * <b><em>classify filter [trace | rx | tx | <intfc>] del</em></b> |
| 2000 | * |
| 2001 | * To display the top-level classifier tables for each use case: |
| 2002 | * <b><em>show classify filter</em/></b> |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 2003 | * |
| 2004 | * To inspect the classifier tables, use |
| 2005 | * |
| 2006 | * <b><em>show classify table [verbose]</em></b> |
| 2007 | * The verbose form displays all of the match rules, with hit-counters |
| 2008 | * @cliexend |
| 2009 | ?*/ |
| 2010 | /* *INDENT-OFF* */ |
| 2011 | VLIB_CLI_COMMAND (classify_filter, static) = |
| 2012 | { |
| 2013 | .path = "classify filter", |
| 2014 | .short_help = |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 2015 | "classify filter <intfc> | pcap mask <mask-value> match <match-value>\n" |
| 2016 | " | trace mask <mask-value> match <match-value> [del]\n" |
| 2017 | " [buckets <nn>] [memory-size <n>]", |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 2018 | .function = classify_filter_command_fn, |
| 2019 | }; |
| 2020 | /* *INDENT-ON* */ |
| 2021 | |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 2022 | static clib_error_t * |
| 2023 | show_classify_filter_command_fn (vlib_main_t * vm, |
| 2024 | unformat_input_t * input, |
| 2025 | vlib_cli_command_t * cmd) |
| 2026 | { |
| 2027 | vnet_classify_main_t *cm = &vnet_classify_main; |
| 2028 | vnet_main_t *vnm = vnet_get_main (); |
| 2029 | vnet_classify_filter_set_t *set; |
| 2030 | u8 *name = 0; |
| 2031 | u8 *s = 0; |
| 2032 | u32 set_index; |
| 2033 | u32 table_index; |
| 2034 | int verbose = 0; |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 2035 | int i, j, limit; |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 2036 | |
| 2037 | (void) unformat (input, "verbose %=", &verbose, 1); |
| 2038 | |
| 2039 | vlib_cli_output (vm, "%-30s%s", "Filter Used By", " Table(s)"); |
| 2040 | vlib_cli_output (vm, "%-30s%s", "--------------", " --------"); |
| 2041 | |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 2042 | limit = vec_len (cm->filter_set_by_sw_if_index); |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 2043 | |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 2044 | for (i = -1; i < limit; i++) |
| 2045 | { |
| 2046 | if (i < 0) |
| 2047 | set_index = vlib_global_main.trace_filter.trace_filter_set_index; |
| 2048 | else |
| 2049 | set_index = cm->filter_set_by_sw_if_index[i]; |
| 2050 | |
| 2051 | if (set_index == ~0) |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 2052 | continue; |
| 2053 | |
| 2054 | set = pool_elt_at_index (cm->filter_sets, set_index); |
| 2055 | |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 2056 | switch (i) |
| 2057 | { |
| 2058 | case -1: |
| 2059 | name = format (0, "packet tracer:"); |
| 2060 | break; |
| 2061 | case 0: |
| 2062 | name = format (0, "pcap rx/tx/drop:"); |
| 2063 | break; |
| 2064 | default: |
| 2065 | name = format (0, "%U:", format_vnet_sw_if_index_name, vnm, i); |
| 2066 | break; |
| 2067 | } |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 2068 | |
| 2069 | if (verbose) |
| 2070 | { |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 2071 | u32 table_index; |
| 2072 | |
| 2073 | for (j = 0; j < vec_len (set->table_indices); j++) |
| 2074 | { |
| 2075 | table_index = set->table_indices[j]; |
| 2076 | if (table_index != ~0) |
| 2077 | s = format (s, " %u", table_index); |
| 2078 | else |
| 2079 | s = format (s, " none"); |
| 2080 | } |
| 2081 | |
Dave Barach | 3268a64 | 2019-11-29 08:40:58 -0500 | [diff] [blame] | 2082 | vlib_cli_output (vm, "%-30v table(s)%v", name, s); |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 2083 | vec_reset_length (s); |
| 2084 | } |
| 2085 | else |
| 2086 | { |
Dave Barach | 104112f | 2020-02-12 14:42:57 -0500 | [diff] [blame] | 2087 | table_index = set->table_indices ? set->table_indices[0] : ~0; |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 2088 | |
| 2089 | if (table_index != ~0) |
| 2090 | s = format (s, " %u", table_index); |
| 2091 | else |
| 2092 | s = format (s, " none"); |
| 2093 | |
Dave Barach | 3268a64 | 2019-11-29 08:40:58 -0500 | [diff] [blame] | 2094 | vlib_cli_output (vm, "%-30v first table%v", name, s); |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 2095 | vec_reset_length (s); |
| 2096 | } |
| 2097 | vec_reset_length (name); |
| 2098 | } |
| 2099 | vec_free (s); |
| 2100 | vec_free (name); |
| 2101 | return 0; |
| 2102 | } |
| 2103 | |
| 2104 | |
| 2105 | /* *INDENT-OFF* */ |
| 2106 | VLIB_CLI_COMMAND (show_classify_filter, static) = |
| 2107 | { |
| 2108 | .path = "show classify filter", |
| 2109 | .short_help = "show classify filter [verbose [nn]]", |
| 2110 | .function = show_classify_filter_command_fn, |
| 2111 | }; |
| 2112 | /* *INDENT-ON* */ |
| 2113 | |
| 2114 | |
| 2115 | |
| 2116 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2117 | static u8 * |
| 2118 | format_vnet_classify_table (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2119 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2120 | vnet_classify_main_t *cm = va_arg (*args, vnet_classify_main_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2121 | int verbose = va_arg (*args, int); |
| 2122 | u32 index = va_arg (*args, u32); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2123 | vnet_classify_table_t *t; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2124 | |
| 2125 | if (index == ~0) |
| 2126 | { |
| 2127 | s = format (s, "%10s%10s%10s%10s", "TableIdx", "Sessions", "NextTbl", |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2128 | "NextNode", verbose ? "Details" : ""); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2129 | return s; |
| 2130 | } |
| 2131 | |
| 2132 | t = pool_elt_at_index (cm->tables, index); |
| 2133 | s = format (s, "%10u%10d%10d%10d", index, t->active_elements, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2134 | t->next_table_index, t->miss_next_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2135 | |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 2136 | s = format (s, "\n Heap: %U", format_clib_mem_heap, t->mheap, |
| 2137 | 0 /*verbose */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2138 | |
Steve Shin | 25e26dc | 2016-11-08 10:47:10 -0800 | [diff] [blame] | 2139 | s = format (s, "\n nbuckets %d, skip %d match %d flag %d offset %d", |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2140 | t->nbuckets, t->skip_n_vectors, t->match_n_vectors, |
| 2141 | t->current_data_flag, t->current_data_offset); |
| 2142 | s = format (s, "\n mask %U", format_hex_bytes, t->mask, |
| 2143 | t->match_n_vectors * sizeof (u32x4)); |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 2144 | s = format (s, "\n linear-search buckets %d\n", t->linear_buckets); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2145 | |
| 2146 | if (verbose == 0) |
| 2147 | return s; |
| 2148 | |
| 2149 | s = format (s, "\n%U", format_classify_table, t, verbose); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2150 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2151 | return s; |
| 2152 | } |
| 2153 | |
| 2154 | static clib_error_t * |
| 2155 | show_classify_tables_command_fn (vlib_main_t * vm, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2156 | unformat_input_t * input, |
| 2157 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2158 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2159 | vnet_classify_main_t *cm = &vnet_classify_main; |
| 2160 | vnet_classify_table_t *t; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2161 | u32 match_index = ~0; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2162 | u32 *indices = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2163 | int verbose = 0; |
| 2164 | int i; |
| 2165 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2166 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2167 | { |
| 2168 | if (unformat (input, "index %d", &match_index)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2169 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2170 | else if (unformat (input, "verbose %d", &verbose)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2171 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2172 | else if (unformat (input, "verbose")) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2173 | verbose = 1; |
| 2174 | else |
| 2175 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2176 | } |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2177 | |
| 2178 | /* *INDENT-OFF* */ |
| 2179 | pool_foreach (t, cm->tables, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2180 | ({ |
| 2181 | if (match_index == ~0 || (match_index == t - cm->tables)) |
| 2182 | vec_add1 (indices, t - cm->tables); |
| 2183 | })); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2184 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2185 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2186 | if (vec_len (indices)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2187 | { |
| 2188 | vlib_cli_output (vm, "%U", format_vnet_classify_table, cm, verbose, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2189 | ~0 /* hdr */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2190 | for (i = 0; i < vec_len (indices); i++) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2191 | vlib_cli_output (vm, "%U", format_vnet_classify_table, cm, |
| 2192 | verbose, indices[i]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2193 | } |
| 2194 | else |
| 2195 | vlib_cli_output (vm, "No classifier tables configured"); |
| 2196 | |
| 2197 | vec_free (indices); |
| 2198 | |
| 2199 | return 0; |
| 2200 | } |
| 2201 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2202 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2203 | VLIB_CLI_COMMAND (show_classify_table_command, static) = { |
| 2204 | .path = "show classify tables", |
| 2205 | .short_help = "show classify tables [index <nn>]", |
| 2206 | .function = show_classify_tables_command_fn, |
| 2207 | }; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2208 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2209 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2210 | uword |
| 2211 | unformat_l4_match (unformat_input_t * input, va_list * args) |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 2212 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2213 | u8 **matchp = va_arg (*args, u8 **); |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 2214 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2215 | u8 *proto_header = 0; |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 2216 | int src_port = 0; |
| 2217 | int dst_port = 0; |
| 2218 | |
| 2219 | tcpudp_header_t h; |
| 2220 | |
| 2221 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 2222 | { |
| 2223 | if (unformat (input, "src_port %d", &src_port)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2224 | ; |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 2225 | else if (unformat (input, "dst_port %d", &dst_port)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2226 | ; |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 2227 | else |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2228 | return 0; |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 2229 | } |
| 2230 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2231 | h.src_port = clib_host_to_net_u16 (src_port); |
| 2232 | h.dst_port = clib_host_to_net_u16 (dst_port); |
| 2233 | vec_validate (proto_header, sizeof (h) - 1); |
| 2234 | memcpy (proto_header, &h, sizeof (h)); |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 2235 | |
| 2236 | *matchp = proto_header; |
| 2237 | |
| 2238 | return 1; |
| 2239 | } |
| 2240 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2241 | uword |
| 2242 | unformat_ip4_match (unformat_input_t * input, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2243 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2244 | u8 **matchp = va_arg (*args, u8 **); |
| 2245 | u8 *match = 0; |
| 2246 | ip4_header_t *ip; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2247 | int version = 0; |
| 2248 | u32 version_val; |
| 2249 | int hdr_length = 0; |
| 2250 | u32 hdr_length_val; |
| 2251 | int src = 0, dst = 0; |
| 2252 | ip4_address_t src_val, dst_val; |
| 2253 | int proto = 0; |
| 2254 | u32 proto_val; |
| 2255 | int tos = 0; |
| 2256 | u32 tos_val; |
| 2257 | int length = 0; |
| 2258 | u32 length_val; |
| 2259 | int fragment_id = 0; |
| 2260 | u32 fragment_id_val; |
| 2261 | int ttl = 0; |
| 2262 | int ttl_val; |
| 2263 | int checksum = 0; |
| 2264 | u32 checksum_val; |
| 2265 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2266 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2267 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2268 | if (unformat (input, "version %d", &version_val)) |
| 2269 | version = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2270 | else if (unformat (input, "hdr_length %d", &hdr_length_val)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2271 | hdr_length = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2272 | else if (unformat (input, "src %U", unformat_ip4_address, &src_val)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2273 | src = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2274 | else if (unformat (input, "dst %U", unformat_ip4_address, &dst_val)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2275 | dst = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2276 | else if (unformat (input, "proto %d", &proto_val)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2277 | proto = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2278 | else if (unformat (input, "tos %d", &tos_val)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2279 | tos = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2280 | else if (unformat (input, "length %d", &length_val)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2281 | length = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2282 | else if (unformat (input, "fragment_id %d", &fragment_id_val)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2283 | fragment_id = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2284 | else if (unformat (input, "ttl %d", &ttl_val)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2285 | ttl = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2286 | else if (unformat (input, "checksum %d", &checksum_val)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2287 | checksum = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2288 | else |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2289 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2290 | } |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2291 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2292 | if (version + hdr_length + src + dst + proto + tos + length + fragment_id |
| 2293 | + ttl + checksum == 0) |
| 2294 | return 0; |
| 2295 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2296 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2297 | * Aligned because we use the real comparison functions |
| 2298 | */ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2299 | vec_validate_aligned (match, sizeof (*ip) - 1, sizeof (u32x4)); |
| 2300 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2301 | ip = (ip4_header_t *) match; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2302 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2303 | /* These are realistically matched in practice */ |
| 2304 | if (src) |
| 2305 | ip->src_address.as_u32 = src_val.as_u32; |
| 2306 | |
| 2307 | if (dst) |
| 2308 | ip->dst_address.as_u32 = dst_val.as_u32; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2309 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2310 | if (proto) |
| 2311 | ip->protocol = proto_val; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2312 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2313 | |
| 2314 | /* These are not, but they're included for completeness */ |
| 2315 | if (version) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2316 | ip->ip_version_and_header_length |= (version_val & 0xF) << 4; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2317 | |
| 2318 | if (hdr_length) |
| 2319 | ip->ip_version_and_header_length |= (hdr_length_val & 0xF); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2320 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2321 | if (tos) |
| 2322 | ip->tos = tos_val; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2323 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2324 | if (length) |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 2325 | ip->length = clib_host_to_net_u16 (length_val); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2326 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2327 | if (ttl) |
| 2328 | ip->ttl = ttl_val; |
| 2329 | |
| 2330 | if (checksum) |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 2331 | ip->checksum = clib_host_to_net_u16 (checksum_val); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2332 | |
| 2333 | *matchp = match; |
| 2334 | return 1; |
| 2335 | } |
| 2336 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2337 | uword |
| 2338 | unformat_ip6_match (unformat_input_t * input, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2339 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2340 | u8 **matchp = va_arg (*args, u8 **); |
| 2341 | u8 *match = 0; |
| 2342 | ip6_header_t *ip; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2343 | int version = 0; |
| 2344 | u32 version_val; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2345 | u8 traffic_class = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2346 | u32 traffic_class_val; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2347 | u8 flow_label = 0; |
| 2348 | u8 flow_label_val; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2349 | int src = 0, dst = 0; |
| 2350 | ip6_address_t src_val, dst_val; |
| 2351 | int proto = 0; |
| 2352 | u32 proto_val; |
| 2353 | int payload_length = 0; |
| 2354 | u32 payload_length_val; |
| 2355 | int hop_limit = 0; |
| 2356 | int hop_limit_val; |
| 2357 | u32 ip_version_traffic_class_and_flow_label; |
| 2358 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2359 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2360 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2361 | if (unformat (input, "version %d", &version_val)) |
| 2362 | version = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2363 | else if (unformat (input, "traffic_class %d", &traffic_class_val)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2364 | traffic_class = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2365 | else if (unformat (input, "flow_label %d", &flow_label_val)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2366 | flow_label = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2367 | else if (unformat (input, "src %U", unformat_ip6_address, &src_val)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2368 | src = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2369 | else if (unformat (input, "dst %U", unformat_ip6_address, &dst_val)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2370 | dst = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2371 | else if (unformat (input, "proto %d", &proto_val)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2372 | proto = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2373 | else if (unformat (input, "payload_length %d", &payload_length_val)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2374 | payload_length = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2375 | else if (unformat (input, "hop_limit %d", &hop_limit_val)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2376 | hop_limit = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2377 | else |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2378 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2379 | } |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2380 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2381 | if (version + traffic_class + flow_label + src + dst + proto + |
| 2382 | payload_length + hop_limit == 0) |
| 2383 | return 0; |
| 2384 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2385 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2386 | * Aligned because we use the real comparison functions |
| 2387 | */ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2388 | vec_validate_aligned (match, sizeof (*ip) - 1, sizeof (u32x4)); |
| 2389 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2390 | ip = (ip6_header_t *) match; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2391 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2392 | if (src) |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 2393 | clib_memcpy_fast (&ip->src_address, &src_val, sizeof (ip->src_address)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2394 | |
| 2395 | if (dst) |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 2396 | clib_memcpy_fast (&ip->dst_address, &dst_val, sizeof (ip->dst_address)); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2397 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2398 | if (proto) |
| 2399 | ip->protocol = proto_val; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2400 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2401 | ip_version_traffic_class_and_flow_label = 0; |
| 2402 | |
| 2403 | if (version) |
| 2404 | ip_version_traffic_class_and_flow_label |= (version_val & 0xF) << 28; |
| 2405 | |
| 2406 | if (traffic_class) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2407 | ip_version_traffic_class_and_flow_label |= |
| 2408 | (traffic_class_val & 0xFF) << 20; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2409 | |
| 2410 | if (flow_label) |
| 2411 | ip_version_traffic_class_and_flow_label |= (flow_label_val & 0xFFFFF); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2412 | |
| 2413 | ip->ip_version_traffic_class_and_flow_label = |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2414 | clib_host_to_net_u32 (ip_version_traffic_class_and_flow_label); |
| 2415 | |
| 2416 | if (payload_length) |
| 2417 | ip->payload_length = clib_host_to_net_u16 (payload_length_val); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2418 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2419 | if (hop_limit) |
| 2420 | ip->hop_limit = hop_limit_val; |
| 2421 | |
| 2422 | *matchp = match; |
| 2423 | return 1; |
| 2424 | } |
| 2425 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2426 | uword |
| 2427 | unformat_l3_match (unformat_input_t * input, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2428 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2429 | u8 **matchp = va_arg (*args, u8 **); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2430 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2431 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 2432 | { |
| 2433 | if (unformat (input, "ip4 %U", unformat_ip4_match, matchp)) |
| 2434 | return 1; |
| 2435 | else if (unformat (input, "ip6 %U", unformat_ip6_match, matchp)) |
| 2436 | return 1; |
| 2437 | /* $$$$ add mpls */ |
| 2438 | else |
| 2439 | break; |
| 2440 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2441 | return 0; |
| 2442 | } |
| 2443 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2444 | uword |
| 2445 | unformat_vlan_tag (unformat_input_t * input, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2446 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2447 | u8 *tagp = va_arg (*args, u8 *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2448 | u32 tag; |
| 2449 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2450 | if (unformat (input, "%d", &tag)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2451 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2452 | tagp[0] = (tag >> 8) & 0x0F; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2453 | tagp[1] = tag & 0xFF; |
| 2454 | return 1; |
| 2455 | } |
| 2456 | |
| 2457 | return 0; |
| 2458 | } |
| 2459 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2460 | uword |
| 2461 | unformat_l2_match (unformat_input_t * input, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2462 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2463 | u8 **matchp = va_arg (*args, u8 **); |
| 2464 | u8 *match = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2465 | u8 src = 0; |
| 2466 | u8 src_val[6]; |
| 2467 | u8 dst = 0; |
| 2468 | u8 dst_val[6]; |
| 2469 | u8 proto = 0; |
| 2470 | u16 proto_val; |
| 2471 | u8 tag1 = 0; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2472 | u8 tag1_val[2]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2473 | u8 tag2 = 0; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2474 | u8 tag2_val[2]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2475 | int len = 14; |
| 2476 | u8 ignore_tag1 = 0; |
| 2477 | u8 ignore_tag2 = 0; |
| 2478 | u8 cos1 = 0; |
| 2479 | u8 cos2 = 0; |
| 2480 | u32 cos1_val = 0; |
| 2481 | u32 cos2_val = 0; |
| 2482 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2483 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 2484 | { |
| 2485 | if (unformat (input, "src %U", unformat_ethernet_address, &src_val)) |
| 2486 | src = 1; |
| 2487 | else |
| 2488 | if (unformat (input, "dst %U", unformat_ethernet_address, &dst_val)) |
| 2489 | dst = 1; |
| 2490 | else if (unformat (input, "proto %U", |
| 2491 | unformat_ethernet_type_host_byte_order, &proto_val)) |
| 2492 | proto = 1; |
| 2493 | else if (unformat (input, "tag1 %U", unformat_vlan_tag, tag1_val)) |
| 2494 | tag1 = 1; |
| 2495 | else if (unformat (input, "tag2 %U", unformat_vlan_tag, tag2_val)) |
| 2496 | tag2 = 1; |
| 2497 | else if (unformat (input, "ignore-tag1")) |
| 2498 | ignore_tag1 = 1; |
| 2499 | else if (unformat (input, "ignore-tag2")) |
| 2500 | ignore_tag2 = 1; |
| 2501 | else if (unformat (input, "cos1 %d", &cos1_val)) |
| 2502 | cos1 = 1; |
| 2503 | else if (unformat (input, "cos2 %d", &cos2_val)) |
| 2504 | cos2 = 1; |
| 2505 | else |
| 2506 | break; |
| 2507 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2508 | if ((src + dst + proto + tag1 + tag2 + |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2509 | ignore_tag1 + ignore_tag2 + cos1 + cos2) == 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2510 | return 0; |
| 2511 | |
| 2512 | if (tag1 || ignore_tag1 || cos1) |
| 2513 | len = 18; |
| 2514 | if (tag2 || ignore_tag2 || cos2) |
| 2515 | len = 22; |
| 2516 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2517 | vec_validate_aligned (match, len - 1, sizeof (u32x4)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2518 | |
| 2519 | if (dst) |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 2520 | clib_memcpy_fast (match, dst_val, 6); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2521 | |
| 2522 | if (src) |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 2523 | clib_memcpy_fast (match + 6, src_val, 6); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2524 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2525 | if (tag2) |
| 2526 | { |
| 2527 | /* inner vlan tag */ |
| 2528 | match[19] = tag2_val[1]; |
| 2529 | match[18] = tag2_val[0]; |
| 2530 | if (cos2) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2531 | match[18] |= (cos2_val & 0x7) << 5; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2532 | if (proto) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2533 | { |
| 2534 | match[21] = proto_val & 0xff; |
| 2535 | match[20] = proto_val >> 8; |
| 2536 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2537 | if (tag1) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2538 | { |
| 2539 | match[15] = tag1_val[1]; |
| 2540 | match[14] = tag1_val[0]; |
| 2541 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2542 | if (cos1) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2543 | match[14] |= (cos1_val & 0x7) << 5; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2544 | *matchp = match; |
| 2545 | return 1; |
| 2546 | } |
| 2547 | if (tag1) |
| 2548 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2549 | match[15] = tag1_val[1]; |
| 2550 | match[14] = tag1_val[0]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2551 | if (proto) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2552 | { |
| 2553 | match[17] = proto_val & 0xff; |
| 2554 | match[16] = proto_val >> 8; |
| 2555 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2556 | if (cos1) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2557 | match[14] |= (cos1_val & 0x7) << 5; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2558 | |
| 2559 | *matchp = match; |
| 2560 | return 1; |
| 2561 | } |
| 2562 | if (cos2) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2563 | match[18] |= (cos2_val & 0x7) << 5; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2564 | if (cos1) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2565 | match[14] |= (cos1_val & 0x7) << 5; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2566 | if (proto) |
| 2567 | { |
| 2568 | match[13] = proto_val & 0xff; |
| 2569 | match[12] = proto_val >> 8; |
| 2570 | } |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2571 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2572 | *matchp = match; |
| 2573 | return 1; |
| 2574 | } |
| 2575 | |
| 2576 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2577 | uword |
| 2578 | unformat_classify_match (unformat_input_t * input, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2579 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2580 | vnet_classify_main_t *cm = va_arg (*args, vnet_classify_main_t *); |
| 2581 | u8 **matchp = va_arg (*args, u8 **); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2582 | u32 table_index = va_arg (*args, u32); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2583 | vnet_classify_table_t *t; |
| 2584 | |
| 2585 | u8 *match = 0; |
| 2586 | u8 *l2 = 0; |
| 2587 | u8 *l3 = 0; |
| 2588 | u8 *l4 = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2589 | |
| 2590 | if (pool_is_free_index (cm->tables, table_index)) |
| 2591 | return 0; |
| 2592 | |
| 2593 | t = pool_elt_at_index (cm->tables, table_index); |
| 2594 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2595 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 2596 | { |
| 2597 | if (unformat (input, "hex %U", unformat_hex_string, &match)) |
| 2598 | ; |
| 2599 | else if (unformat (input, "l2 %U", unformat_l2_match, &l2)) |
| 2600 | ; |
| 2601 | else if (unformat (input, "l3 %U", unformat_l3_match, &l3)) |
| 2602 | ; |
| 2603 | else if (unformat (input, "l4 %U", unformat_l4_match, &l4)) |
| 2604 | ; |
| 2605 | else |
| 2606 | break; |
| 2607 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2608 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2609 | if (l4 && !l3) |
| 2610 | { |
| 2611 | vec_free (match); |
| 2612 | vec_free (l2); |
| 2613 | vec_free (l4); |
| 2614 | return 0; |
| 2615 | } |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 2616 | |
| 2617 | if (match || l2 || l3 || l4) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2618 | { |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 2619 | if (l2 || l3 || l4) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2620 | { |
| 2621 | /* "Win a free Ethernet header in every packet" */ |
| 2622 | if (l2 == 0) |
| 2623 | vec_validate_aligned (l2, 13, sizeof (u32x4)); |
| 2624 | match = l2; |
| 2625 | if (l3) |
| 2626 | { |
| 2627 | vec_append_aligned (match, l3, sizeof (u32x4)); |
| 2628 | vec_free (l3); |
| 2629 | } |
| 2630 | if (l4) |
| 2631 | { |
| 2632 | vec_append_aligned (match, l4, sizeof (u32x4)); |
| 2633 | vec_free (l4); |
| 2634 | } |
| 2635 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2636 | |
| 2637 | /* Make sure the vector is big enough even if key is all 0's */ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2638 | vec_validate_aligned |
| 2639 | (match, |
| 2640 | ((t->match_n_vectors + t->skip_n_vectors) * sizeof (u32x4)) - 1, |
| 2641 | sizeof (u32x4)); |
| 2642 | |
| 2643 | /* Set size, include skipped vectors */ |
| 2644 | _vec_len (match) = |
| 2645 | (t->match_n_vectors + t->skip_n_vectors) * sizeof (u32x4); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2646 | |
| 2647 | *matchp = match; |
| 2648 | |
| 2649 | return 1; |
| 2650 | } |
| 2651 | |
| 2652 | return 0; |
| 2653 | } |
| 2654 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2655 | int |
| 2656 | vnet_classify_add_del_session (vnet_classify_main_t * cm, |
| 2657 | u32 table_index, |
| 2658 | u8 * match, |
| 2659 | u32 hit_next_index, |
| 2660 | u32 opaque_index, |
| 2661 | i32 advance, |
| 2662 | u8 action, u32 metadata, int is_add) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2663 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2664 | vnet_classify_table_t *t; |
| 2665 | vnet_classify_entry_5_t _max_e __attribute__ ((aligned (16))); |
| 2666 | vnet_classify_entry_t *e; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2667 | int i, rv; |
| 2668 | |
| 2669 | if (pool_is_free_index (cm->tables, table_index)) |
| 2670 | return VNET_API_ERROR_NO_SUCH_TABLE; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2671 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2672 | t = pool_elt_at_index (cm->tables, table_index); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2673 | |
| 2674 | e = (vnet_classify_entry_t *) & _max_e; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2675 | e->next_index = hit_next_index; |
| 2676 | e->opaque_index = opaque_index; |
| 2677 | e->advance = advance; |
| 2678 | e->hits = 0; |
| 2679 | e->last_heard = 0; |
| 2680 | e->flags = 0; |
Steve Shin | 25e26dc | 2016-11-08 10:47:10 -0800 | [diff] [blame] | 2681 | e->action = action; |
| 2682 | if (e->action == CLASSIFY_ACTION_SET_IP4_FIB_INDEX) |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 2683 | e->metadata = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2684 | metadata, |
| 2685 | FIB_SOURCE_CLASSIFY); |
Steve Shin | 25e26dc | 2016-11-08 10:47:10 -0800 | [diff] [blame] | 2686 | else if (e->action == CLASSIFY_ACTION_SET_IP6_FIB_INDEX) |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 2687 | e->metadata = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2688 | metadata, |
| 2689 | FIB_SOURCE_CLASSIFY); |
Dave Barach | 630a8e2 | 2017-11-18 06:58:34 -0500 | [diff] [blame] | 2690 | else if (e->action == CLASSIFY_ACTION_SET_METADATA) |
Gabriel Ganne | 8527f12 | 2017-10-02 11:41:24 +0200 | [diff] [blame] | 2691 | e->metadata = metadata; |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 2692 | else |
| 2693 | e->metadata = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2694 | |
| 2695 | /* Copy key data, honoring skip_n_vectors */ |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 2696 | clib_memcpy_fast (&e->key, match + t->skip_n_vectors * sizeof (u32x4), |
| 2697 | t->match_n_vectors * sizeof (u32x4)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2698 | |
| 2699 | /* Clear don't-care bits; likely when dynamically creating sessions */ |
| 2700 | for (i = 0; i < t->match_n_vectors; i++) |
| 2701 | e->key[i] &= t->mask[i]; |
| 2702 | |
| 2703 | rv = vnet_classify_add_del (t, e, is_add); |
Neale Ranns | 13eaf3e | 2017-05-23 06:10:33 -0700 | [diff] [blame] | 2704 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2705 | vnet_classify_entry_release_resource (e); |
Neale Ranns | 13eaf3e | 2017-05-23 06:10:33 -0700 | [diff] [blame] | 2706 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2707 | if (rv) |
| 2708 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
| 2709 | return 0; |
| 2710 | } |
| 2711 | |
| 2712 | static clib_error_t * |
| 2713 | classify_session_command_fn (vlib_main_t * vm, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2714 | unformat_input_t * input, |
| 2715 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2716 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2717 | vnet_classify_main_t *cm = &vnet_classify_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2718 | int is_add = 1; |
| 2719 | u32 table_index = ~0; |
| 2720 | u32 hit_next_index = ~0; |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2721 | u64 opaque_index = ~0; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2722 | u8 *match = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2723 | i32 advance = 0; |
Steve Shin | 25e26dc | 2016-11-08 10:47:10 -0800 | [diff] [blame] | 2724 | u32 action = 0; |
| 2725 | u32 metadata = 0; |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2726 | int i, rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2727 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2728 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2729 | { |
| 2730 | if (unformat (input, "del")) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2731 | is_add = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2732 | else if (unformat (input, "hit-next %U", unformat_ip_next_index, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2733 | &hit_next_index)) |
| 2734 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2735 | else |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2736 | if (unformat |
| 2737 | (input, "l2-input-hit-next %U", unformat_l2_input_next_index, |
| 2738 | &hit_next_index)) |
| 2739 | ; |
| 2740 | else |
| 2741 | if (unformat |
| 2742 | (input, "l2-output-hit-next %U", unformat_l2_output_next_index, |
| 2743 | &hit_next_index)) |
| 2744 | ; |
| 2745 | else if (unformat (input, "acl-hit-next %U", unformat_acl_next_index, |
| 2746 | &hit_next_index)) |
| 2747 | ; |
| 2748 | else if (unformat (input, "policer-hit-next %U", |
| 2749 | unformat_policer_next_index, &hit_next_index)) |
| 2750 | ; |
| 2751 | else if (unformat (input, "opaque-index %lld", &opaque_index)) |
| 2752 | ; |
| 2753 | else if (unformat (input, "match %U", unformat_classify_match, |
| 2754 | cm, &match, table_index)) |
| 2755 | ; |
| 2756 | else if (unformat (input, "advance %d", &advance)) |
| 2757 | ; |
| 2758 | else if (unformat (input, "table-index %d", &table_index)) |
| 2759 | ; |
| 2760 | else if (unformat (input, "action set-ip4-fib-id %d", &metadata)) |
| 2761 | action = 1; |
| 2762 | else if (unformat (input, "action set-ip6-fib-id %d", &metadata)) |
| 2763 | action = 2; |
| 2764 | else if (unformat (input, "action set-sr-policy-index %d", &metadata)) |
| 2765 | action = 3; |
| 2766 | else |
| 2767 | { |
| 2768 | /* Try registered opaque-index unformat fns */ |
| 2769 | for (i = 0; i < vec_len (cm->unformat_opaque_index_fns); i++) |
| 2770 | { |
| 2771 | if (unformat (input, "%U", cm->unformat_opaque_index_fns[i], |
| 2772 | &opaque_index)) |
| 2773 | goto found_opaque; |
| 2774 | } |
| 2775 | break; |
| 2776 | } |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2777 | found_opaque: |
| 2778 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2779 | } |
| 2780 | |
| 2781 | if (table_index == ~0) |
| 2782 | return clib_error_return (0, "Table index required"); |
| 2783 | |
| 2784 | if (is_add && match == 0) |
| 2785 | return clib_error_return (0, "Match value required"); |
| 2786 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2787 | rv = vnet_classify_add_del_session (cm, table_index, match, |
| 2788 | hit_next_index, |
| 2789 | opaque_index, advance, |
| 2790 | action, metadata, is_add); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2791 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2792 | switch (rv) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2793 | { |
| 2794 | case 0: |
| 2795 | break; |
| 2796 | |
| 2797 | default: |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2798 | return clib_error_return (0, |
| 2799 | "vnet_classify_add_del_session returned %d", |
| 2800 | rv); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2801 | } |
| 2802 | |
| 2803 | return 0; |
| 2804 | } |
| 2805 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2806 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2807 | VLIB_CLI_COMMAND (classify_session_command, static) = { |
| 2808 | .path = "classify session", |
Ole Troan | 1e66d5c | 2016-09-30 09:22:36 +0200 | [diff] [blame] | 2809 | .short_help = |
jackiechen1985 | e91e6de | 2018-12-14 01:43:21 +0800 | [diff] [blame] | 2810 | "classify session [hit-next|l2-input-hit-next|l2-output-hit-next|" |
Ole Troan | 1e66d5c | 2016-09-30 09:22:36 +0200 | [diff] [blame] | 2811 | "acl-hit-next <next_index>|policer-hit-next <policer_name>]" |
Steve Shin | 25e26dc | 2016-11-08 10:47:10 -0800 | [diff] [blame] | 2812 | "\n table-index <nn> match [hex] [l2] [l3 ip4] [opaque-index <index>]" |
Gabriel Ganne | 8527f12 | 2017-10-02 11:41:24 +0200 | [diff] [blame] | 2813 | "\n [action set-ip4-fib-id|set-ip6-fib-id|set-sr-policy-index <n>] [del]", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2814 | .function = classify_session_command_fn, |
| 2815 | }; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2816 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2817 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2818 | static uword |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2819 | unformat_opaque_sw_if_index (unformat_input_t * input, va_list * args) |
| 2820 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2821 | u64 *opaquep = va_arg (*args, u64 *); |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2822 | u32 sw_if_index; |
| 2823 | |
| 2824 | if (unformat (input, "opaque-sw_if_index %U", unformat_vnet_sw_interface, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2825 | vnet_get_main (), &sw_if_index)) |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2826 | { |
| 2827 | *opaquep = sw_if_index; |
| 2828 | return 1; |
| 2829 | } |
| 2830 | return 0; |
| 2831 | } |
| 2832 | |
Ole Troan | 1e66d5c | 2016-09-30 09:22:36 +0200 | [diff] [blame] | 2833 | static uword |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2834 | unformat_ip_next_node (unformat_input_t * input, va_list * args) |
| 2835 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2836 | vnet_classify_main_t *cm = &vnet_classify_main; |
| 2837 | u32 *next_indexp = va_arg (*args, u32 *); |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2838 | u32 node_index; |
Ole Troan | 1e66d5c | 2016-09-30 09:22:36 +0200 | [diff] [blame] | 2839 | u32 next_index = ~0; |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2840 | |
Ole Troan | 1e66d5c | 2016-09-30 09:22:36 +0200 | [diff] [blame] | 2841 | if (unformat (input, "ip6-node %U", unformat_vlib_node, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2842 | cm->vlib_main, &node_index)) |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2843 | { |
Ole Troan | 1e66d5c | 2016-09-30 09:22:36 +0200 | [diff] [blame] | 2844 | next_index = vlib_node_add_next (cm->vlib_main, |
| 2845 | ip6_classify_node.index, node_index); |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2846 | } |
Ole Troan | 1e66d5c | 2016-09-30 09:22:36 +0200 | [diff] [blame] | 2847 | else if (unformat (input, "ip4-node %U", unformat_vlib_node, |
| 2848 | cm->vlib_main, &node_index)) |
| 2849 | { |
| 2850 | next_index = vlib_node_add_next (cm->vlib_main, |
| 2851 | ip4_classify_node.index, node_index); |
| 2852 | } |
| 2853 | else |
| 2854 | return 0; |
| 2855 | |
| 2856 | *next_indexp = next_index; |
| 2857 | return 1; |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2858 | } |
| 2859 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2860 | static uword |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2861 | unformat_acl_next_node (unformat_input_t * input, va_list * args) |
| 2862 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2863 | vnet_classify_main_t *cm = &vnet_classify_main; |
| 2864 | u32 *next_indexp = va_arg (*args, u32 *); |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2865 | u32 node_index; |
Ole Troan | 1e66d5c | 2016-09-30 09:22:36 +0200 | [diff] [blame] | 2866 | u32 next_index; |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2867 | |
Ole Troan | 1e66d5c | 2016-09-30 09:22:36 +0200 | [diff] [blame] | 2868 | if (unformat (input, "ip6-node %U", unformat_vlib_node, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2869 | cm->vlib_main, &node_index)) |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2870 | { |
Ole Troan | 1e66d5c | 2016-09-30 09:22:36 +0200 | [diff] [blame] | 2871 | next_index = vlib_node_add_next (cm->vlib_main, |
| 2872 | ip6_inacl_node.index, node_index); |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2873 | } |
Ole Troan | 1e66d5c | 2016-09-30 09:22:36 +0200 | [diff] [blame] | 2874 | else if (unformat (input, "ip4-node %U", unformat_vlib_node, |
| 2875 | cm->vlib_main, &node_index)) |
| 2876 | { |
| 2877 | next_index = vlib_node_add_next (cm->vlib_main, |
| 2878 | ip4_inacl_node.index, node_index); |
| 2879 | } |
| 2880 | else |
| 2881 | return 0; |
| 2882 | |
| 2883 | *next_indexp = next_index; |
| 2884 | return 1; |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2885 | } |
| 2886 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2887 | static uword |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 2888 | unformat_l2_input_next_node (unformat_input_t * input, va_list * args) |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2889 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2890 | vnet_classify_main_t *cm = &vnet_classify_main; |
| 2891 | u32 *next_indexp = va_arg (*args, u32 *); |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2892 | u32 node_index; |
| 2893 | u32 next_index; |
| 2894 | |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 2895 | if (unformat (input, "input-node %U", unformat_vlib_node, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2896 | cm->vlib_main, &node_index)) |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2897 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2898 | next_index = vlib_node_add_next |
| 2899 | (cm->vlib_main, l2_input_classify_node.index, node_index); |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2900 | |
| 2901 | *next_indexp = next_index; |
| 2902 | return 1; |
| 2903 | } |
| 2904 | return 0; |
| 2905 | } |
| 2906 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2907 | static uword |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 2908 | unformat_l2_output_next_node (unformat_input_t * input, va_list * args) |
| 2909 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2910 | vnet_classify_main_t *cm = &vnet_classify_main; |
| 2911 | u32 *next_indexp = va_arg (*args, u32 *); |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 2912 | u32 node_index; |
| 2913 | u32 next_index; |
| 2914 | |
| 2915 | if (unformat (input, "output-node %U", unformat_vlib_node, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2916 | cm->vlib_main, &node_index)) |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 2917 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2918 | next_index = vlib_node_add_next |
| 2919 | (cm->vlib_main, l2_output_classify_node.index, node_index); |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 2920 | |
| 2921 | *next_indexp = next_index; |
| 2922 | return 1; |
| 2923 | } |
| 2924 | return 0; |
| 2925 | } |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2926 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2927 | static clib_error_t * |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2928 | vnet_classify_init (vlib_main_t * vm) |
| 2929 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2930 | vnet_classify_main_t *cm = &vnet_classify_main; |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 2931 | vnet_classify_filter_set_t *set; |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2932 | |
| 2933 | cm->vlib_main = vm; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2934 | cm->vnet_main = vnet_get_main (); |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2935 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2936 | vnet_classify_register_unformat_opaque_index_fn |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2937 | (unformat_opaque_sw_if_index); |
| 2938 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2939 | vnet_classify_register_unformat_ip_next_index_fn (unformat_ip_next_node); |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2940 | |
| 2941 | vnet_classify_register_unformat_l2_next_index_fn |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 2942 | (unformat_l2_input_next_node); |
| 2943 | |
| 2944 | vnet_classify_register_unformat_l2_next_index_fn |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 2945 | (unformat_l2_output_next_node); |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2946 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2947 | vnet_classify_register_unformat_acl_next_index_fn (unformat_acl_next_node); |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2948 | |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 2949 | /* Filter set 0 is grounded... */ |
Florin Coras | cd681ad | 2020-01-16 11:15:54 -0800 | [diff] [blame] | 2950 | pool_get_zero (cm->filter_sets, set); |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 2951 | set->refcnt = 0x7FFFFFFF; |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 2952 | /* Initialize the pcap filter set */ |
| 2953 | vec_validate (cm->filter_set_by_sw_if_index, 0); |
Florin Coras | cd681ad | 2020-01-16 11:15:54 -0800 | [diff] [blame] | 2954 | cm->filter_set_by_sw_if_index[0] = 0; |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 2955 | /* Initialize the packet tracer filter set */ |
| 2956 | vlib_global_main.trace_filter.trace_filter_set_index = ~0; |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 2957 | |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2958 | return 0; |
| 2959 | } |
| 2960 | |
| 2961 | VLIB_INIT_FUNCTION (vnet_classify_init); |
| 2962 | |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 2963 | int |
| 2964 | vnet_is_packet_traced (vlib_buffer_t * b, u32 classify_table_index, int func) |
| 2965 | { |
| 2966 | return vnet_is_packet_traced_inline (b, classify_table_index, func); |
| 2967 | } |
| 2968 | |
| 2969 | |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 2970 | #define TEST_CODE 0 |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2971 | |
| 2972 | #if TEST_CODE > 0 |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 2973 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2974 | typedef struct |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2975 | { |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 2976 | ip4_address_t addr; |
| 2977 | int in_table; |
| 2978 | } test_entry_t; |
| 2979 | |
| 2980 | typedef struct |
| 2981 | { |
| 2982 | test_entry_t *entries; |
| 2983 | |
| 2984 | /* test parameters */ |
| 2985 | u32 buckets; |
| 2986 | u32 sessions; |
| 2987 | u32 iterations; |
| 2988 | u32 memory_size; |
| 2989 | ip4_address_t src; |
| 2990 | vnet_classify_table_t *table; |
| 2991 | u32 table_index; |
| 2992 | int verbose; |
| 2993 | |
| 2994 | /* Random seed */ |
| 2995 | u32 seed; |
| 2996 | |
| 2997 | /* Test data */ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2998 | classify_data_or_mask_t *mask; |
| 2999 | classify_data_or_mask_t *data; |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3000 | |
| 3001 | /* convenience */ |
| 3002 | vnet_classify_main_t *classify_main; |
| 3003 | vlib_main_t *vlib_main; |
| 3004 | |
| 3005 | } test_classify_main_t; |
| 3006 | |
| 3007 | static test_classify_main_t test_classify_main; |
| 3008 | |
| 3009 | static clib_error_t * |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3010 | test_classify_churn (test_classify_main_t * tm) |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3011 | { |
| 3012 | classify_data_or_mask_t *mask, *data; |
| 3013 | vlib_main_t *vm = tm->vlib_main; |
| 3014 | test_entry_t *ep; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3015 | u8 *mp = 0, *dp = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3016 | u32 tmp; |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3017 | int i, rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3018 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3019 | vec_validate_aligned (mp, 3 * sizeof (u32x4), sizeof (u32x4)); |
| 3020 | vec_validate_aligned (dp, 3 * sizeof (u32x4), sizeof (u32x4)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3021 | |
| 3022 | mask = (classify_data_or_mask_t *) mp; |
| 3023 | data = (classify_data_or_mask_t *) dp; |
| 3024 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3025 | /* Mask on src address */ |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 3026 | clib_memset (&mask->ip.src_address, 0xff, 4); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3027 | |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3028 | tmp = clib_host_to_net_u32 (tm->src.as_u32); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3029 | |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3030 | for (i = 0; i < tm->sessions; i++) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3031 | { |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3032 | vec_add2 (tm->entries, ep, 1); |
| 3033 | ep->addr.as_u32 = clib_host_to_net_u32 (tmp); |
| 3034 | ep->in_table = 0; |
| 3035 | tmp++; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3036 | } |
| 3037 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3038 | tm->table = vnet_classify_new_table (tm->classify_main, |
| 3039 | (u8 *) mask, |
| 3040 | tm->buckets, |
| 3041 | tm->memory_size, 0 /* skip */ , |
| 3042 | 3 /* vectors to match */ ); |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3043 | tm->table->miss_next_index = IP_LOOKUP_NEXT_DROP; |
| 3044 | tm->table_index = tm->table - tm->classify_main->tables; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3045 | vlib_cli_output (vm, "Created table %d, buckets %d", |
| 3046 | tm->table_index, tm->buckets); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3047 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3048 | vlib_cli_output (vm, "Initialize: add %d (approx. half of %d sessions)...", |
| 3049 | tm->sessions / 2, tm->sessions); |
| 3050 | |
| 3051 | for (i = 0; i < tm->sessions / 2; i++) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3052 | { |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3053 | ep = vec_elt_at_index (tm->entries, i); |
| 3054 | |
| 3055 | data->ip.src_address.as_u32 = ep->addr.as_u32; |
| 3056 | ep->in_table = 1; |
| 3057 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3058 | rv = vnet_classify_add_del_session (tm->classify_main, |
| 3059 | tm->table_index, |
| 3060 | (u8 *) data, |
| 3061 | IP_LOOKUP_NEXT_DROP, |
| 3062 | i /* opaque_index */ , |
| 3063 | 0 /* advance */ , |
| 3064 | 0 /* action */ , |
| 3065 | 0 /* metadata */ , |
| 3066 | 1 /* is_add */ ); |
| 3067 | |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3068 | if (rv != 0) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3069 | clib_warning ("add: returned %d", rv); |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3070 | |
| 3071 | if (tm->verbose) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3072 | vlib_cli_output (vm, "add: %U", format_ip4_address, &ep->addr.as_u32); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3073 | } |
| 3074 | |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3075 | vlib_cli_output (vm, "Execute %d random add/delete operations", |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3076 | tm->iterations); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3077 | |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3078 | for (i = 0; i < tm->iterations; i++) |
| 3079 | { |
| 3080 | int index, is_add; |
| 3081 | |
| 3082 | /* Pick a random entry */ |
| 3083 | index = random_u32 (&tm->seed) % tm->sessions; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3084 | |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3085 | ep = vec_elt_at_index (tm->entries, index); |
| 3086 | |
| 3087 | data->ip.src_address.as_u32 = ep->addr.as_u32; |
| 3088 | |
| 3089 | /* If it's in the table, remove it. Else, add it */ |
| 3090 | is_add = !ep->in_table; |
| 3091 | |
| 3092 | if (tm->verbose) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3093 | vlib_cli_output (vm, "%s: %U", |
| 3094 | is_add ? "add" : "del", |
| 3095 | format_ip4_address, &ep->addr.as_u32); |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3096 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3097 | rv = vnet_classify_add_del_session (tm->classify_main, |
| 3098 | tm->table_index, |
| 3099 | (u8 *) data, |
| 3100 | IP_LOOKUP_NEXT_DROP, |
| 3101 | i /* opaque_index */ , |
| 3102 | 0 /* advance */ , |
| 3103 | 0 /* action */ , |
| 3104 | 0 /* metadata */ , |
| 3105 | is_add); |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3106 | if (rv != 0) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3107 | vlib_cli_output (vm, |
| 3108 | "%s[%d]: %U returned %d", is_add ? "add" : "del", |
| 3109 | index, format_ip4_address, &ep->addr.as_u32, rv); |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3110 | else |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3111 | ep->in_table = is_add; |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3112 | } |
| 3113 | |
| 3114 | vlib_cli_output (vm, "Remove remaining %d entries from the table", |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3115 | tm->table->active_elements); |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3116 | |
| 3117 | for (i = 0; i < tm->sessions; i++) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3118 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3119 | u8 *key_minus_skip; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3120 | u64 hash; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3121 | vnet_classify_entry_t *e; |
| 3122 | |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3123 | ep = tm->entries + i; |
| 3124 | if (ep->in_table == 0) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3125 | continue; |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3126 | |
| 3127 | data->ip.src_address.as_u32 = ep->addr.as_u32; |
| 3128 | |
| 3129 | hash = vnet_classify_hash_packet (tm->table, (u8 *) data); |
| 3130 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3131 | e = vnet_classify_find_entry (tm->table, |
| 3132 | (u8 *) data, hash, 0 /* time_now */ ); |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3133 | if (e == 0) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3134 | { |
| 3135 | clib_warning ("Couldn't find %U index %d which should be present", |
| 3136 | format_ip4_address, ep->addr, i); |
| 3137 | continue; |
| 3138 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3139 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3140 | key_minus_skip = (u8 *) e->key; |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3141 | key_minus_skip -= tm->table->skip_n_vectors * sizeof (u32x4); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3142 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3143 | rv = vnet_classify_add_del_session |
| 3144 | (tm->classify_main, |
| 3145 | tm->table_index, |
| 3146 | key_minus_skip, IP_LOOKUP_NEXT_DROP, i /* opaque_index */ , |
| 3147 | 0 /* advance */ , 0, 0, |
| 3148 | 0 /* is_add */ ); |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3149 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3150 | if (rv != 0) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3151 | clib_warning ("del: returned %d", rv); |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3152 | |
| 3153 | if (tm->verbose) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3154 | vlib_cli_output (vm, "del: %U", format_ip4_address, &ep->addr.as_u32); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3155 | } |
| 3156 | |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3157 | vlib_cli_output (vm, "%d entries remain, MUST be zero", |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3158 | tm->table->active_elements); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3159 | |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3160 | vlib_cli_output (vm, "Table after cleanup: \n%U\n", |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3161 | format_classify_table, tm->table, 0 /* verbose */ ); |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3162 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3163 | vec_free (mp); |
| 3164 | vec_free (dp); |
| 3165 | |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3166 | vnet_classify_delete_table_index (tm->classify_main, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3167 | tm->table_index, 1 /* del_chain */ ); |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3168 | tm->table = 0; |
| 3169 | tm->table_index = ~0; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3170 | vec_free (tm->entries); |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3171 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3172 | return 0; |
| 3173 | } |
| 3174 | |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3175 | static clib_error_t * |
| 3176 | test_classify_command_fn (vlib_main_t * vm, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3177 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3178 | { |
| 3179 | test_classify_main_t *tm = &test_classify_main; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3180 | vnet_classify_main_t *cm = &vnet_classify_main; |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3181 | u32 tmp; |
| 3182 | int which = 0; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3183 | clib_error_t *error = 0; |
| 3184 | |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3185 | tm->buckets = 1024; |
| 3186 | tm->sessions = 8192; |
| 3187 | tm->iterations = 8192; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3188 | tm->memory_size = 64 << 20; |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3189 | tm->src.as_u32 = clib_net_to_host_u32 (0x0100000A); |
| 3190 | tm->table = 0; |
| 3191 | tm->seed = 0xDEADDABE; |
| 3192 | tm->classify_main = cm; |
| 3193 | tm->vlib_main = vm; |
| 3194 | tm->verbose = 0; |
| 3195 | |
| 3196 | /* Default starting address 1.0.0.10 */ |
| 3197 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3198 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 3199 | { |
| 3200 | if (unformat (input, "sessions %d", &tmp)) |
| 3201 | tm->sessions = tmp; |
| 3202 | else |
| 3203 | if (unformat (input, "src %U", unformat_ip4_address, &tm->src.as_u32)) |
| 3204 | ; |
| 3205 | else if (unformat (input, "buckets %d", &tm->buckets)) |
| 3206 | ; |
| 3207 | else if (unformat (input, "memory-size %uM", &tmp)) |
| 3208 | tm->memory_size = tmp << 20; |
| 3209 | else if (unformat (input, "memory-size %uG", &tmp)) |
| 3210 | tm->memory_size = tmp << 30; |
| 3211 | else if (unformat (input, "seed %d", &tm->seed)) |
| 3212 | ; |
| 3213 | else if (unformat (input, "verbose")) |
| 3214 | tm->verbose = 1; |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3215 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3216 | else if (unformat (input, "iterations %d", &tm->iterations)) |
| 3217 | ; |
| 3218 | else if (unformat (input, "churn-test")) |
| 3219 | which = 0; |
| 3220 | else |
| 3221 | break; |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3222 | } |
| 3223 | |
| 3224 | switch (which) |
| 3225 | { |
| 3226 | case 0: |
| 3227 | error = test_classify_churn (tm); |
| 3228 | break; |
| 3229 | default: |
| 3230 | error = clib_error_return (0, "No such test"); |
| 3231 | break; |
| 3232 | } |
| 3233 | |
| 3234 | return error; |
| 3235 | } |
| 3236 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3237 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3238 | VLIB_CLI_COMMAND (test_classify_command, static) = { |
| 3239 | .path = "test classify", |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3240 | .short_help = |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3241 | "test classify [src <ip>] [sessions <nn>] [buckets <nn>] [seed <nnn>]\n" |
| 3242 | " [memory-size <nn>[M|G]]\n" |
| 3243 | " [churn-test]", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3244 | .function = test_classify_command_fn, |
| 3245 | }; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3246 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3247 | #endif /* TEST_CODE */ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3248 | |
| 3249 | /* |
| 3250 | * fd.io coding-style-patch-verification: ON |
| 3251 | * |
| 3252 | * Local Variables: |
| 3253 | * eval: (c-set-style "gnu") |
| 3254 | * End: |
| 3255 | */ |