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