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 | { |
Andrew Yourtchenko | aac6856 | 2022-08-18 12:38:00 +0000 | [diff] [blame] | 296 | u32 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; |
Benoît Ganne | b03eec9 | 2022-06-08 10:49:17 +0200 | [diff] [blame] | 427 | u32 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 | |
Benoît Ganne | b03eec9 | 2022-06-08 10:49:17 +0200 | [diff] [blame] | 650 | u32 |
| 651 | vnet_classify_hash_packet (const 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 * |
Benoît Ganne | b03eec9 | 2022-06-08 10:49:17 +0200 | [diff] [blame] | 657 | vnet_classify_find_entry (const vnet_classify_table_t *t, u8 *h, u32 hash, |
| 658 | 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 | |
Benoît Ganne | c629f90 | 2022-06-08 10:56:33 +0200 | [diff] [blame] | 663 | u8 * |
| 664 | format_classify_entry (u8 *s, va_list *args) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 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; |
Huawei LI | a6a01f1 | 2022-11-05 00:35:19 +0800 | [diff] [blame] | 780 | if (pool_is_free_index (cm->tables, *table_index)) |
| 781 | return VNET_API_ERROR_CLASSIFY_TABLE_NOT_FOUND; |
Steve Shin | 25e26dc | 2016-11-08 10:47:10 -0800 | [diff] [blame] | 782 | |
Huawei LI | a6a01f1 | 2022-11-05 00:35:19 +0800 | [diff] [blame] | 783 | t = pool_elt_at_index (cm->tables, *table_index); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 784 | t->next_table_index = next_table_index; |
| 785 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 786 | return 0; |
| 787 | } |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 788 | |
Juraj Sloboda | 288e893 | 2016-12-06 21:25:19 +0100 | [diff] [blame] | 789 | vnet_classify_delete_table_index (cm, *table_index, del_chain); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 790 | return 0; |
| 791 | } |
| 792 | |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 793 | #define foreach_tcp_proto_field \ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 794 | _(src) \ |
| 795 | _(dst) |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 796 | |
| 797 | #define foreach_udp_proto_field \ |
| 798 | _(src_port) \ |
| 799 | _(dst_port) |
| 800 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 801 | #define foreach_ip4_proto_field \ |
| 802 | _(src_address) \ |
| 803 | _(dst_address) \ |
| 804 | _(tos) \ |
| 805 | _(length) \ |
| 806 | _(fragment_id) \ |
| 807 | _(ttl) \ |
| 808 | _(protocol) \ |
| 809 | _(checksum) |
| 810 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 811 | uword |
| 812 | unformat_tcp_mask (unformat_input_t * input, va_list * args) |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 813 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 814 | u8 **maskp = va_arg (*args, u8 **); |
| 815 | u8 *mask = 0; |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 816 | u8 found_something = 0; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 817 | tcp_header_t *tcp; |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 818 | |
| 819 | #define _(a) u8 a=0; |
| 820 | foreach_tcp_proto_field; |
| 821 | #undef _ |
| 822 | |
| 823 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 824 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 825 | if (0); |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 826 | #define _(a) else if (unformat (input, #a)) a=1; |
| 827 | foreach_tcp_proto_field |
| 828 | #undef _ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 829 | else |
| 830 | break; |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | #define _(a) found_something += a; |
| 834 | foreach_tcp_proto_field; |
| 835 | #undef _ |
| 836 | |
| 837 | if (found_something == 0) |
| 838 | return 0; |
| 839 | |
| 840 | vec_validate (mask, sizeof (*tcp) - 1); |
| 841 | |
| 842 | tcp = (tcp_header_t *) mask; |
| 843 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 844 | #define _(a) if (a) clib_memset (&tcp->a, 0xff, sizeof (tcp->a)); |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 845 | foreach_tcp_proto_field; |
| 846 | #undef _ |
| 847 | |
| 848 | *maskp = mask; |
| 849 | return 1; |
| 850 | } |
| 851 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 852 | uword |
| 853 | unformat_udp_mask (unformat_input_t * input, va_list * args) |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 854 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 855 | u8 **maskp = va_arg (*args, u8 **); |
| 856 | u8 *mask = 0; |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 857 | u8 found_something = 0; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 858 | udp_header_t *udp; |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 859 | |
| 860 | #define _(a) u8 a=0; |
| 861 | foreach_udp_proto_field; |
| 862 | #undef _ |
| 863 | |
| 864 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 865 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 866 | if (0); |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 867 | #define _(a) else if (unformat (input, #a)) a=1; |
| 868 | foreach_udp_proto_field |
| 869 | #undef _ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 870 | else |
| 871 | break; |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | #define _(a) found_something += a; |
| 875 | foreach_udp_proto_field; |
| 876 | #undef _ |
| 877 | |
| 878 | if (found_something == 0) |
| 879 | return 0; |
| 880 | |
| 881 | vec_validate (mask, sizeof (*udp) - 1); |
| 882 | |
| 883 | udp = (udp_header_t *) mask; |
| 884 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 885 | #define _(a) if (a) clib_memset (&udp->a, 0xff, sizeof (udp->a)); |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 886 | foreach_udp_proto_field; |
| 887 | #undef _ |
| 888 | |
| 889 | *maskp = mask; |
| 890 | return 1; |
| 891 | } |
| 892 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 893 | typedef struct |
| 894 | { |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 895 | u16 src_port, dst_port; |
| 896 | } tcpudp_header_t; |
| 897 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 898 | uword |
| 899 | unformat_l4_mask (unformat_input_t * input, va_list * args) |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 900 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 901 | u8 **maskp = va_arg (*args, u8 **); |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 902 | u16 src_port = 0, dst_port = 0; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 903 | tcpudp_header_t *tcpudp; |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 904 | |
| 905 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 906 | { |
| 907 | if (unformat (input, "tcp %U", unformat_tcp_mask, maskp)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 908 | return 1; |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 909 | else if (unformat (input, "udp %U", unformat_udp_mask, maskp)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 910 | return 1; |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 911 | else if (unformat (input, "src_port")) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 912 | src_port = 0xFFFF; |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 913 | else if (unformat (input, "dst_port")) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 914 | dst_port = 0xFFFF; |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 915 | else |
Benoît Ganne | 8c45e51 | 2021-02-19 16:39:13 +0100 | [diff] [blame] | 916 | break; |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | if (!src_port && !dst_port) |
| 920 | return 0; |
| 921 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 922 | u8 *mask = 0; |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 923 | vec_validate (mask, sizeof (tcpudp_header_t) - 1); |
| 924 | |
| 925 | tcpudp = (tcpudp_header_t *) mask; |
| 926 | tcpudp->src_port = src_port; |
| 927 | tcpudp->dst_port = dst_port; |
| 928 | |
| 929 | *maskp = mask; |
| 930 | |
| 931 | return 1; |
| 932 | } |
| 933 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 934 | uword |
| 935 | unformat_ip4_mask (unformat_input_t * input, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 936 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 937 | u8 **maskp = va_arg (*args, u8 **); |
| 938 | u8 *mask = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 939 | u8 found_something = 0; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 940 | ip4_header_t *ip; |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 941 | u32 src_prefix_len = 32; |
| 942 | u32 src_prefix_mask = ~0; |
| 943 | u32 dst_prefix_len = 32; |
| 944 | u32 dst_prefix_mask = ~0; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 945 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 946 | #define _(a) u8 a=0; |
| 947 | foreach_ip4_proto_field; |
| 948 | #undef _ |
| 949 | u8 version = 0; |
| 950 | u8 hdr_length = 0; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 951 | |
| 952 | |
| 953 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 954 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 955 | if (unformat (input, "version")) |
| 956 | version = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 957 | else if (unformat (input, "hdr_length")) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 958 | hdr_length = 1; |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 959 | else if (unformat (input, "src/%d", &src_prefix_len)) |
| 960 | { |
| 961 | src_address = 1; |
| 962 | src_prefix_mask &= ~((1 << (32 - src_prefix_len)) - 1); |
| 963 | src_prefix_mask = clib_host_to_net_u32 (src_prefix_mask); |
| 964 | } |
| 965 | else if (unformat (input, "dst/%d", &dst_prefix_len)) |
| 966 | { |
| 967 | dst_address = 1; |
| 968 | dst_prefix_mask &= ~((1 << (32 - dst_prefix_len)) - 1); |
| 969 | dst_prefix_mask = clib_host_to_net_u32 (dst_prefix_mask); |
| 970 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 971 | else if (unformat (input, "src")) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 972 | src_address = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 973 | else if (unformat (input, "dst")) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 974 | dst_address = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 975 | else if (unformat (input, "proto")) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 976 | protocol = 1; |
| 977 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 978 | #define _(a) else if (unformat (input, #a)) a=1; |
| 979 | foreach_ip4_proto_field |
| 980 | #undef _ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 981 | else |
| 982 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 983 | } |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 984 | |
Benoît Ganne | 8c45e51 | 2021-02-19 16:39:13 +0100 | [diff] [blame] | 985 | found_something = version + hdr_length; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 986 | #define _(a) found_something += a; |
| 987 | foreach_ip4_proto_field; |
| 988 | #undef _ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 989 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 990 | if (found_something == 0) |
| 991 | return 0; |
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 | vec_validate (mask, sizeof (*ip) - 1); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 994 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 995 | ip = (ip4_header_t *) mask; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 996 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 997 | #define _(a) if (a) clib_memset (&ip->a, 0xff, sizeof (ip->a)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 998 | foreach_ip4_proto_field; |
| 999 | #undef _ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1000 | |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 1001 | if (src_address) |
| 1002 | ip->src_address.as_u32 = src_prefix_mask; |
| 1003 | |
| 1004 | if (dst_address) |
| 1005 | ip->dst_address.as_u32 = dst_prefix_mask; |
| 1006 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1007 | ip->ip_version_and_header_length = 0; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1008 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1009 | if (version) |
| 1010 | ip->ip_version_and_header_length |= 0xF0; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1011 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1012 | if (hdr_length) |
| 1013 | ip->ip_version_and_header_length |= 0x0F; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1014 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1015 | *maskp = mask; |
| 1016 | return 1; |
| 1017 | } |
| 1018 | |
| 1019 | #define foreach_ip6_proto_field \ |
| 1020 | _(src_address) \ |
| 1021 | _(dst_address) \ |
| 1022 | _(payload_length) \ |
| 1023 | _(hop_limit) \ |
| 1024 | _(protocol) |
| 1025 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1026 | uword |
| 1027 | unformat_ip6_mask (unformat_input_t * input, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1028 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1029 | u8 **maskp = va_arg (*args, u8 **); |
| 1030 | u8 *mask = 0; |
Dave Barach | 126c885 | 2020-06-30 08:28:06 -0400 | [diff] [blame] | 1031 | u8 found_something; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1032 | ip6_header_t *ip; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1033 | u32 ip_version_traffic_class_and_flow_label; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1034 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1035 | #define _(a) u8 a=0; |
| 1036 | foreach_ip6_proto_field; |
| 1037 | #undef _ |
| 1038 | u8 version = 0; |
| 1039 | u8 traffic_class = 0; |
| 1040 | u8 flow_label = 0; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1041 | |
| 1042 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1043 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1044 | if (unformat (input, "version")) |
| 1045 | version = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1046 | else if (unformat (input, "traffic-class")) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1047 | traffic_class = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1048 | else if (unformat (input, "flow-label")) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1049 | flow_label = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1050 | else if (unformat (input, "src")) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1051 | src_address = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1052 | else if (unformat (input, "dst")) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1053 | dst_address = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1054 | else if (unformat (input, "proto")) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1055 | protocol = 1; |
| 1056 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1057 | #define _(a) else if (unformat (input, #a)) a=1; |
| 1058 | foreach_ip6_proto_field |
| 1059 | #undef _ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1060 | else |
| 1061 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1062 | } |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1063 | |
Dave Barach | 126c885 | 2020-06-30 08:28:06 -0400 | [diff] [blame] | 1064 | /* Account for "special" field names */ |
| 1065 | found_something = version + traffic_class + flow_label |
| 1066 | + src_address + dst_address + protocol; |
| 1067 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1068 | #define _(a) found_something += a; |
| 1069 | foreach_ip6_proto_field; |
| 1070 | #undef _ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1071 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1072 | if (found_something == 0) |
| 1073 | return 0; |
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 | vec_validate (mask, sizeof (*ip) - 1); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1076 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1077 | ip = (ip6_header_t *) mask; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1078 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 1079 | #define _(a) if (a) clib_memset (&ip->a, 0xff, sizeof (ip->a)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1080 | foreach_ip6_proto_field; |
| 1081 | #undef _ |
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 | ip_version_traffic_class_and_flow_label = 0; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1084 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1085 | if (version) |
| 1086 | ip_version_traffic_class_and_flow_label |= 0xF0000000; |
| 1087 | |
| 1088 | if (traffic_class) |
| 1089 | ip_version_traffic_class_and_flow_label |= 0x0FF00000; |
| 1090 | |
| 1091 | if (flow_label) |
| 1092 | ip_version_traffic_class_and_flow_label |= 0x000FFFFF; |
| 1093 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1094 | ip->ip_version_traffic_class_and_flow_label = |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1095 | clib_host_to_net_u32 (ip_version_traffic_class_and_flow_label); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1096 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1097 | *maskp = mask; |
| 1098 | return 1; |
| 1099 | } |
| 1100 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1101 | uword |
| 1102 | unformat_l3_mask (unformat_input_t * input, va_list * args) |
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 | u8 **maskp = va_arg (*args, u8 **); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1105 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1106 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 1107 | { |
| 1108 | if (unformat (input, "ip4 %U", unformat_ip4_mask, maskp)) |
| 1109 | return 1; |
| 1110 | else if (unformat (input, "ip6 %U", unformat_ip6_mask, maskp)) |
| 1111 | return 1; |
| 1112 | else |
| 1113 | break; |
| 1114 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1115 | return 0; |
| 1116 | } |
| 1117 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1118 | uword |
| 1119 | unformat_l2_mask (unformat_input_t * input, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1120 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1121 | u8 **maskp = va_arg (*args, u8 **); |
| 1122 | u8 *mask = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1123 | u8 src = 0; |
| 1124 | u8 dst = 0; |
| 1125 | u8 proto = 0; |
| 1126 | u8 tag1 = 0; |
| 1127 | u8 tag2 = 0; |
| 1128 | u8 ignore_tag1 = 0; |
| 1129 | u8 ignore_tag2 = 0; |
| 1130 | u8 cos1 = 0; |
| 1131 | u8 cos2 = 0; |
| 1132 | u8 dot1q = 0; |
| 1133 | u8 dot1ad = 0; |
| 1134 | int len = 14; |
| 1135 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1136 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 1137 | { |
| 1138 | if (unformat (input, "src")) |
| 1139 | src = 1; |
| 1140 | else if (unformat (input, "dst")) |
| 1141 | dst = 1; |
| 1142 | else if (unformat (input, "proto")) |
| 1143 | proto = 1; |
| 1144 | else if (unformat (input, "tag1")) |
| 1145 | tag1 = 1; |
| 1146 | else if (unformat (input, "tag2")) |
| 1147 | tag2 = 1; |
| 1148 | else if (unformat (input, "ignore-tag1")) |
| 1149 | ignore_tag1 = 1; |
| 1150 | else if (unformat (input, "ignore-tag2")) |
| 1151 | ignore_tag2 = 1; |
| 1152 | else if (unformat (input, "cos1")) |
| 1153 | cos1 = 1; |
| 1154 | else if (unformat (input, "cos2")) |
| 1155 | cos2 = 1; |
| 1156 | else if (unformat (input, "dot1q")) |
| 1157 | dot1q = 1; |
| 1158 | else if (unformat (input, "dot1ad")) |
| 1159 | dot1ad = 1; |
| 1160 | else |
| 1161 | break; |
| 1162 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1163 | if ((src + dst + proto + tag1 + tag2 + dot1q + dot1ad + |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1164 | ignore_tag1 + ignore_tag2 + cos1 + cos2) == 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1165 | return 0; |
| 1166 | |
| 1167 | if (tag1 || ignore_tag1 || cos1 || dot1q) |
| 1168 | len = 18; |
| 1169 | if (tag2 || ignore_tag2 || cos2 || dot1ad) |
| 1170 | len = 22; |
| 1171 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1172 | vec_validate (mask, len - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1173 | |
| 1174 | if (dst) |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 1175 | clib_memset (mask, 0xff, 6); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1176 | |
| 1177 | if (src) |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 1178 | clib_memset (mask + 6, 0xff, 6); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1179 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1180 | if (tag2 || dot1ad) |
| 1181 | { |
| 1182 | /* inner vlan tag */ |
| 1183 | if (tag2) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1184 | { |
| 1185 | mask[19] = 0xff; |
| 1186 | mask[18] = 0x0f; |
| 1187 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1188 | if (cos2) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1189 | mask[18] |= 0xe0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1190 | if (proto) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1191 | mask[21] = mask[20] = 0xff; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1192 | if (tag1) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1193 | { |
| 1194 | mask[15] = 0xff; |
| 1195 | mask[14] = 0x0f; |
| 1196 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1197 | if (cos1) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1198 | mask[14] |= 0xe0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1199 | *maskp = mask; |
| 1200 | return 1; |
| 1201 | } |
| 1202 | if (tag1 | dot1q) |
| 1203 | { |
| 1204 | if (tag1) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1205 | { |
| 1206 | mask[15] = 0xff; |
| 1207 | mask[14] = 0x0f; |
| 1208 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1209 | if (cos1) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1210 | mask[14] |= 0xe0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1211 | if (proto) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1212 | mask[16] = mask[17] = 0xff; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1213 | *maskp = mask; |
| 1214 | return 1; |
| 1215 | } |
| 1216 | if (cos2) |
| 1217 | mask[18] |= 0xe0; |
| 1218 | if (cos1) |
| 1219 | mask[14] |= 0xe0; |
| 1220 | if (proto) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1221 | mask[12] = mask[13] = 0xff; |
| 1222 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1223 | *maskp = mask; |
| 1224 | return 1; |
| 1225 | } |
| 1226 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1227 | uword |
| 1228 | unformat_classify_mask (unformat_input_t * input, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1229 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1230 | u8 **maskp = va_arg (*args, u8 **); |
| 1231 | u32 *skipp = va_arg (*args, u32 *); |
| 1232 | u32 *matchp = va_arg (*args, u32 *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1233 | u32 match; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1234 | u8 *mask = 0; |
| 1235 | u8 *l2 = 0; |
| 1236 | u8 *l3 = 0; |
| 1237 | u8 *l4 = 0; |
Arthur de Kerhor | 9a63b6e | 2022-03-03 10:33:23 +0100 | [diff] [blame] | 1238 | u8 add_l2 = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1239 | int i; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1240 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1241 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 1242 | { |
| 1243 | if (unformat (input, "hex %U", unformat_hex_string, &mask)) |
| 1244 | ; |
Arthur de Kerhor | 9a63b6e | 2022-03-03 10:33:23 +0100 | [diff] [blame] | 1245 | else if (unformat (input, "l2 none")) |
| 1246 | /* Don't add the l2 header in the mask */ |
| 1247 | add_l2 = 0; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1248 | else if (unformat (input, "l2 %U", unformat_l2_mask, &l2)) |
| 1249 | ; |
| 1250 | else if (unformat (input, "l3 %U", unformat_l3_mask, &l3)) |
| 1251 | ; |
| 1252 | else if (unformat (input, "l4 %U", unformat_l4_mask, &l4)) |
| 1253 | ; |
| 1254 | else |
| 1255 | break; |
| 1256 | } |
| 1257 | |
Arthur de Kerhor | 9a63b6e | 2022-03-03 10:33:23 +0100 | [diff] [blame] | 1258 | if (l2 && !add_l2) |
| 1259 | { |
| 1260 | vec_free (mask); |
| 1261 | vec_free (l2); |
| 1262 | vec_free (l3); |
| 1263 | vec_free (l4); |
| 1264 | return 0; |
| 1265 | } |
| 1266 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1267 | if (l4 && !l3) |
| 1268 | { |
| 1269 | vec_free (mask); |
| 1270 | vec_free (l2); |
| 1271 | vec_free (l4); |
| 1272 | return 0; |
| 1273 | } |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 1274 | |
| 1275 | if (mask || l2 || l3 || l4) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1276 | { |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 1277 | if (l2 || l3 || l4) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1278 | { |
Arthur de Kerhor | 9a63b6e | 2022-03-03 10:33:23 +0100 | [diff] [blame] | 1279 | if (add_l2) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1280 | { |
Arthur de Kerhor | 9a63b6e | 2022-03-03 10:33:23 +0100 | [diff] [blame] | 1281 | /* "With a free Ethernet header in every package" */ |
| 1282 | if (l2 == 0) |
| 1283 | vec_validate (l2, 13); |
| 1284 | mask = l2; |
| 1285 | if (l3) |
| 1286 | { |
| 1287 | vec_append (mask, l3); |
| 1288 | vec_free (l3); |
| 1289 | } |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1290 | } |
Arthur de Kerhor | 9a63b6e | 2022-03-03 10:33:23 +0100 | [diff] [blame] | 1291 | else |
| 1292 | mask = l3; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1293 | if (l4) |
| 1294 | { |
| 1295 | vec_append (mask, l4); |
| 1296 | vec_free (l4); |
| 1297 | } |
| 1298 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1299 | |
| 1300 | /* Scan forward looking for the first significant mask octet */ |
| 1301 | for (i = 0; i < vec_len (mask); i++) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1302 | if (mask[i]) |
| 1303 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1304 | |
| 1305 | /* compute (skip, match) params */ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1306 | *skipp = i / sizeof (u32x4); |
| 1307 | vec_delete (mask, *skipp * sizeof (u32x4), 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1308 | |
| 1309 | /* Pad mask to an even multiple of the vector size */ |
| 1310 | while (vec_len (mask) % sizeof (u32x4)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1311 | vec_add1 (mask, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1312 | |
| 1313 | match = vec_len (mask) / sizeof (u32x4); |
| 1314 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1315 | for (i = match * sizeof (u32x4); i > 0; i -= sizeof (u32x4)) |
| 1316 | { |
| 1317 | u64 *tmp = (u64 *) (mask + (i - sizeof (u32x4))); |
| 1318 | if (*tmp || *(tmp + 1)) |
| 1319 | break; |
| 1320 | match--; |
| 1321 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1322 | if (match == 0) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1323 | clib_warning ("BUG: match 0"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1324 | |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 1325 | vec_set_len (mask, match * sizeof (u32x4)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1326 | |
| 1327 | *matchp = match; |
| 1328 | *maskp = mask; |
| 1329 | |
| 1330 | return 1; |
| 1331 | } |
| 1332 | |
| 1333 | return 0; |
| 1334 | } |
| 1335 | |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 1336 | #define foreach_l2_input_next \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1337 | _(drop, DROP) \ |
| 1338 | _(ethernet, ETHERNET_INPUT) \ |
| 1339 | _(ip4, IP4_INPUT) \ |
| 1340 | _(ip6, IP6_INPUT) \ |
| 1341 | _(li, LI) |
| 1342 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1343 | uword |
| 1344 | unformat_l2_input_next_index (unformat_input_t * input, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1345 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1346 | vnet_classify_main_t *cm = &vnet_classify_main; |
| 1347 | u32 *miss_next_indexp = va_arg (*args, u32 *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1348 | u32 next_index = 0; |
| 1349 | u32 tmp; |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 1350 | int i; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1351 | |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 1352 | /* First try registered unformat fns, allowing override... */ |
| 1353 | for (i = 0; i < vec_len (cm->unformat_l2_next_index_fns); i++) |
| 1354 | { |
| 1355 | if (unformat (input, "%U", cm->unformat_l2_next_index_fns[i], &tmp)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1356 | { |
| 1357 | next_index = tmp; |
| 1358 | goto out; |
| 1359 | } |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 1360 | } |
| 1361 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1362 | #define _(n,N) \ |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 1363 | if (unformat (input, #n)) { next_index = L2_INPUT_CLASSIFY_NEXT_##N; goto out;} |
| 1364 | foreach_l2_input_next; |
| 1365 | #undef _ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1366 | |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 1367 | if (unformat (input, "%d", &tmp)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1368 | { |
| 1369 | next_index = tmp; |
| 1370 | goto out; |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 1371 | } |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1372 | |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 1373 | return 0; |
| 1374 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1375 | out: |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 1376 | *miss_next_indexp = next_index; |
| 1377 | return 1; |
| 1378 | } |
| 1379 | |
| 1380 | #define foreach_l2_output_next \ |
| 1381 | _(drop, DROP) |
| 1382 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1383 | uword |
| 1384 | unformat_l2_output_next_index (unformat_input_t * input, va_list * args) |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 1385 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1386 | vnet_classify_main_t *cm = &vnet_classify_main; |
| 1387 | u32 *miss_next_indexp = va_arg (*args, u32 *); |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 1388 | u32 next_index = 0; |
| 1389 | u32 tmp; |
| 1390 | int i; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1391 | |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 1392 | /* First try registered unformat fns, allowing override... */ |
| 1393 | for (i = 0; i < vec_len (cm->unformat_l2_next_index_fns); i++) |
| 1394 | { |
| 1395 | if (unformat (input, "%U", cm->unformat_l2_next_index_fns[i], &tmp)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1396 | { |
| 1397 | next_index = tmp; |
| 1398 | goto out; |
| 1399 | } |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 1400 | } |
| 1401 | |
| 1402 | #define _(n,N) \ |
| 1403 | if (unformat (input, #n)) { next_index = L2_OUTPUT_CLASSIFY_NEXT_##N; goto out;} |
| 1404 | foreach_l2_output_next; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1405 | #undef _ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1406 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1407 | if (unformat (input, "%d", &tmp)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1408 | { |
| 1409 | next_index = tmp; |
| 1410 | goto out; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1411 | } |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1412 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1413 | return 0; |
| 1414 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1415 | out: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1416 | *miss_next_indexp = next_index; |
| 1417 | return 1; |
| 1418 | } |
| 1419 | |
| 1420 | #define foreach_ip_next \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1421 | _(drop, DROP) \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1422 | _(rewrite, REWRITE) |
| 1423 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1424 | uword |
| 1425 | unformat_ip_next_index (unformat_input_t * input, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1426 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1427 | u32 *miss_next_indexp = va_arg (*args, u32 *); |
| 1428 | vnet_classify_main_t *cm = &vnet_classify_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1429 | u32 next_index = 0; |
| 1430 | u32 tmp; |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 1431 | int i; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1432 | |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 1433 | /* First try registered unformat fns, allowing override... */ |
| 1434 | for (i = 0; i < vec_len (cm->unformat_ip_next_index_fns); i++) |
| 1435 | { |
| 1436 | if (unformat (input, "%U", cm->unformat_ip_next_index_fns[i], &tmp)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1437 | { |
| 1438 | next_index = tmp; |
| 1439 | goto out; |
| 1440 | } |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 1441 | } |
| 1442 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1443 | #define _(n,N) \ |
| 1444 | if (unformat (input, #n)) { next_index = IP_LOOKUP_NEXT_##N; goto out;} |
| 1445 | foreach_ip_next; |
| 1446 | #undef _ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1447 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1448 | if (unformat (input, "%d", &tmp)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1449 | { |
| 1450 | next_index = tmp; |
| 1451 | goto out; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1452 | } |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1453 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1454 | return 0; |
| 1455 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1456 | out: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1457 | *miss_next_indexp = next_index; |
| 1458 | return 1; |
| 1459 | } |
| 1460 | |
| 1461 | #define foreach_acl_next \ |
| 1462 | _(deny, DENY) |
| 1463 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1464 | uword |
| 1465 | unformat_acl_next_index (unformat_input_t * input, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1466 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1467 | u32 *next_indexp = va_arg (*args, u32 *); |
| 1468 | vnet_classify_main_t *cm = &vnet_classify_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1469 | u32 next_index = 0; |
| 1470 | u32 tmp; |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 1471 | int i; |
| 1472 | |
| 1473 | /* First try registered unformat fns, allowing override... */ |
| 1474 | for (i = 0; i < vec_len (cm->unformat_acl_next_index_fns); i++) |
| 1475 | { |
| 1476 | if (unformat (input, "%U", cm->unformat_acl_next_index_fns[i], &tmp)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1477 | { |
| 1478 | next_index = tmp; |
| 1479 | goto out; |
| 1480 | } |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 1481 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1482 | |
| 1483 | #define _(n,N) \ |
| 1484 | if (unformat (input, #n)) { next_index = ACL_NEXT_INDEX_##N; goto out;} |
| 1485 | foreach_acl_next; |
| 1486 | #undef _ |
| 1487 | |
| 1488 | if (unformat (input, "permit")) |
| 1489 | { |
| 1490 | next_index = ~0; |
| 1491 | goto out; |
| 1492 | } |
| 1493 | else if (unformat (input, "%d", &tmp)) |
| 1494 | { |
| 1495 | next_index = tmp; |
| 1496 | goto out; |
| 1497 | } |
| 1498 | |
| 1499 | return 0; |
| 1500 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1501 | out: |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 1502 | *next_indexp = next_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1503 | return 1; |
| 1504 | } |
| 1505 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1506 | uword |
| 1507 | unformat_policer_next_index (unformat_input_t * input, va_list * args) |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 1508 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1509 | u32 *next_indexp = va_arg (*args, u32 *); |
| 1510 | vnet_classify_main_t *cm = &vnet_classify_main; |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 1511 | u32 next_index = 0; |
| 1512 | u32 tmp; |
| 1513 | int i; |
| 1514 | |
| 1515 | /* First try registered unformat fns, allowing override... */ |
| 1516 | for (i = 0; i < vec_len (cm->unformat_policer_next_index_fns); i++) |
| 1517 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1518 | if (unformat |
| 1519 | (input, "%U", cm->unformat_policer_next_index_fns[i], &tmp)) |
| 1520 | { |
| 1521 | next_index = tmp; |
| 1522 | goto out; |
| 1523 | } |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 1524 | } |
| 1525 | |
| 1526 | if (unformat (input, "%d", &tmp)) |
| 1527 | { |
| 1528 | next_index = tmp; |
| 1529 | goto out; |
| 1530 | } |
| 1531 | |
| 1532 | return 0; |
| 1533 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1534 | out: |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 1535 | *next_indexp = next_index; |
| 1536 | return 1; |
| 1537 | } |
| 1538 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1539 | static clib_error_t * |
| 1540 | classify_table_command_fn (vlib_main_t * vm, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1541 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1542 | { |
| 1543 | u32 nbuckets = 2; |
| 1544 | u32 skip = ~0; |
| 1545 | u32 match = ~0; |
| 1546 | int is_add = 1; |
Juraj Sloboda | 288e893 | 2016-12-06 21:25:19 +0100 | [diff] [blame] | 1547 | int del_chain = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1548 | u32 table_index = ~0; |
| 1549 | u32 next_table_index = ~0; |
| 1550 | u32 miss_next_index = ~0; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1551 | u32 memory_size = 2 << 20; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1552 | u32 tmp; |
Steve Shin | 25e26dc | 2016-11-08 10:47:10 -0800 | [diff] [blame] | 1553 | u32 current_data_flag = 0; |
| 1554 | int current_data_offset = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1555 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1556 | u8 *mask = 0; |
| 1557 | vnet_classify_main_t *cm = &vnet_classify_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1558 | int rv; |
| 1559 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1560 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 1561 | { |
| 1562 | if (unformat (input, "del")) |
Juraj Sloboda | 288e893 | 2016-12-06 21:25:19 +0100 | [diff] [blame] | 1563 | is_add = 0; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1564 | else if (unformat (input, "del-chain")) |
| 1565 | { |
| 1566 | is_add = 0; |
| 1567 | del_chain = 1; |
| 1568 | } |
| 1569 | else if (unformat (input, "buckets %d", &nbuckets)) |
| 1570 | ; |
| 1571 | else if (unformat (input, "skip %d", &skip)) |
| 1572 | ; |
| 1573 | else if (unformat (input, "match %d", &match)) |
| 1574 | ; |
| 1575 | else if (unformat (input, "table %d", &table_index)) |
| 1576 | ; |
| 1577 | else if (unformat (input, "mask %U", unformat_classify_mask, |
| 1578 | &mask, &skip, &match)) |
| 1579 | ; |
| 1580 | else if (unformat (input, "memory-size %uM", &tmp)) |
| 1581 | memory_size = tmp << 20; |
| 1582 | else if (unformat (input, "memory-size %uG", &tmp)) |
| 1583 | memory_size = tmp << 30; |
| 1584 | else if (unformat (input, "next-table %d", &next_table_index)) |
| 1585 | ; |
| 1586 | else if (unformat (input, "miss-next %U", unformat_ip_next_index, |
| 1587 | &miss_next_index)) |
| 1588 | ; |
| 1589 | else |
| 1590 | if (unformat |
| 1591 | (input, "l2-input-miss-next %U", unformat_l2_input_next_index, |
| 1592 | &miss_next_index)) |
| 1593 | ; |
| 1594 | else |
| 1595 | if (unformat |
| 1596 | (input, "l2-output-miss-next %U", unformat_l2_output_next_index, |
| 1597 | &miss_next_index)) |
| 1598 | ; |
| 1599 | else if (unformat (input, "acl-miss-next %U", unformat_acl_next_index, |
| 1600 | &miss_next_index)) |
| 1601 | ; |
| 1602 | else if (unformat (input, "current-data-flag %d", ¤t_data_flag)) |
| 1603 | ; |
| 1604 | else |
| 1605 | if (unformat (input, "current-data-offset %d", ¤t_data_offset)) |
| 1606 | ; |
Steve Shin | 25e26dc | 2016-11-08 10:47:10 -0800 | [diff] [blame] | 1607 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1608 | else |
| 1609 | break; |
| 1610 | } |
| 1611 | |
Steve Shin | 25e26dc | 2016-11-08 10:47:10 -0800 | [diff] [blame] | 1612 | if (is_add && mask == 0 && table_index == ~0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1613 | return clib_error_return (0, "Mask required"); |
| 1614 | |
Steve Shin | 25e26dc | 2016-11-08 10:47:10 -0800 | [diff] [blame] | 1615 | if (is_add && skip == ~0 && table_index == ~0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1616 | return clib_error_return (0, "skip count required"); |
| 1617 | |
Steve Shin | 25e26dc | 2016-11-08 10:47:10 -0800 | [diff] [blame] | 1618 | if (is_add && match == ~0 && table_index == ~0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1619 | return clib_error_return (0, "match count required"); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1620 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1621 | if (!is_add && table_index == ~0) |
| 1622 | return clib_error_return (0, "table index required for delete"); |
| 1623 | |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 1624 | rv = vnet_classify_add_del_table (cm, mask, nbuckets, (u32) memory_size, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1625 | skip, match, next_table_index, |
| 1626 | miss_next_index, &table_index, |
| 1627 | current_data_flag, current_data_offset, |
| 1628 | is_add, del_chain); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1629 | switch (rv) |
| 1630 | { |
| 1631 | case 0: |
| 1632 | break; |
| 1633 | |
| 1634 | default: |
| 1635 | return clib_error_return (0, "vnet_classify_add_del_table returned %d", |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1636 | rv); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1637 | } |
| 1638 | return 0; |
| 1639 | } |
| 1640 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1641 | /* *INDENT-OFF* */ |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 1642 | VLIB_CLI_COMMAND (classify_table, static) = |
| 1643 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1644 | .path = "classify table", |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1645 | .short_help = |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1646 | "classify table [miss-next|l2-miss_next|acl-miss-next <next_index>]" |
Steve Shin | 25e26dc | 2016-11-08 10:47:10 -0800 | [diff] [blame] | 1647 | "\n mask <mask-value> buckets <nn> [skip <n>] [match <n>]" |
Juraj Sloboda | 288e893 | 2016-12-06 21:25:19 +0100 | [diff] [blame] | 1648 | "\n [current-data-flag <n>] [current-data-offset <n>] [table <n>]" |
Hongjun Ni | 8184ebd | 2017-10-25 20:47:56 +0800 | [diff] [blame] | 1649 | "\n [memory-size <nn>[M][G]] [next-table <n>]" |
Juraj Sloboda | 288e893 | 2016-12-06 21:25:19 +0100 | [diff] [blame] | 1650 | "\n [del] [del-chain]", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1651 | .function = classify_table_command_fn, |
| 1652 | }; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 1653 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1654 | |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 1655 | static int |
| 1656 | filter_table_mask_compare (void *a1, void *a2) |
| 1657 | { |
| 1658 | vnet_classify_main_t *cm = &vnet_classify_main; |
| 1659 | u32 *ti1 = a1; |
| 1660 | u32 *ti2 = a2; |
| 1661 | u32 n1 = 0, n2 = 0; |
| 1662 | vnet_classify_table_t *t1, *t2; |
| 1663 | u8 *m1, *m2; |
| 1664 | int i; |
| 1665 | |
| 1666 | t1 = pool_elt_at_index (cm->tables, *ti1); |
| 1667 | t2 = pool_elt_at_index (cm->tables, *ti2); |
| 1668 | |
| 1669 | m1 = (u8 *) (t1->mask); |
| 1670 | m2 = (u8 *) (t2->mask); |
| 1671 | |
Damjan Marion | 3bb2da9 | 2021-09-20 13:39:37 +0200 | [diff] [blame] | 1672 | for (i = 0; i < t1->match_n_vectors * sizeof (u32x4); i++) |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 1673 | { |
| 1674 | n1 += count_set_bits (m1[0]); |
| 1675 | m1++; |
| 1676 | } |
| 1677 | |
Damjan Marion | 3bb2da9 | 2021-09-20 13:39:37 +0200 | [diff] [blame] | 1678 | for (i = 0; i < t2->match_n_vectors * sizeof (u32x4); i++) |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 1679 | { |
| 1680 | n2 += count_set_bits (m2[0]); |
| 1681 | m2++; |
| 1682 | } |
| 1683 | |
| 1684 | /* Reverse sort: descending number of set bits */ |
| 1685 | if (n1 < n2) |
| 1686 | return 1; |
| 1687 | else if (n1 > n2) |
| 1688 | return -1; |
| 1689 | else |
| 1690 | return 0; |
| 1691 | } |
| 1692 | |
Jon Loeliger | 5c1e48c | 2020-10-15 14:41:36 -0400 | [diff] [blame] | 1693 | |
| 1694 | /* |
| 1695 | * Reorder the chain of tables starting with table_index such |
| 1696 | * that more more-specific masks come before less-specific masks. |
| 1697 | * Return the new head of the table chain. |
| 1698 | */ |
| 1699 | u32 |
| 1700 | classify_sort_table_chain (vnet_classify_main_t * cm, u32 table_index) |
| 1701 | { |
| 1702 | /* |
| 1703 | * Form a vector of all classifier tables in this chain. |
| 1704 | */ |
| 1705 | u32 *tables = 0; |
| 1706 | vnet_classify_table_t *t; |
| 1707 | u32 cti; |
| 1708 | for (cti = table_index; cti != ~0; cti = t->next_table_index) |
| 1709 | { |
| 1710 | vec_add1 (tables, cti); |
| 1711 | t = pool_elt_at_index (cm->tables, cti); |
| 1712 | } |
| 1713 | |
| 1714 | /* |
| 1715 | * Sort filter tables from most-specific mask to least-specific mask. |
| 1716 | */ |
| 1717 | vec_sort_with_function (tables, filter_table_mask_compare); |
| 1718 | |
| 1719 | /* |
| 1720 | * Relink tables via next_table_index fields. |
| 1721 | */ |
| 1722 | int i; |
| 1723 | for (i = 0; i < vec_len (tables); i++) |
| 1724 | { |
| 1725 | t = pool_elt_at_index (cm->tables, tables[i]); |
| 1726 | |
| 1727 | if ((i + 1) < vec_len (tables)) |
| 1728 | t->next_table_index = tables[i + 1]; |
| 1729 | else |
| 1730 | t->next_table_index = ~0; |
| 1731 | } |
| 1732 | |
| 1733 | table_index = tables[0]; |
| 1734 | vec_free (tables); |
| 1735 | |
| 1736 | return table_index; |
| 1737 | } |
| 1738 | |
| 1739 | |
| 1740 | u32 |
| 1741 | classify_get_trace_chain (void) |
| 1742 | { |
| 1743 | u32 table_index; |
| 1744 | |
| 1745 | table_index = vlib_global_main.trace_filter.classify_table_index; |
| 1746 | |
| 1747 | return table_index; |
| 1748 | } |
| 1749 | |
| 1750 | /* |
| 1751 | * Seting the Trace chain to ~0 is a request to delete and clear it. |
| 1752 | */ |
| 1753 | void |
| 1754 | classify_set_trace_chain (vnet_classify_main_t * cm, u32 table_index) |
| 1755 | { |
| 1756 | if (table_index == ~0) |
| 1757 | { |
| 1758 | u32 old_table_index; |
| 1759 | |
| 1760 | old_table_index = vlib_global_main.trace_filter.classify_table_index; |
| 1761 | vnet_classify_delete_table_index (cm, old_table_index, 1); |
| 1762 | } |
| 1763 | |
| 1764 | vlib_global_main.trace_filter.classify_table_index = table_index; |
| 1765 | } |
| 1766 | |
| 1767 | |
| 1768 | u32 |
| 1769 | classify_get_pcap_chain (vnet_classify_main_t * cm, u32 sw_if_index) |
| 1770 | { |
| 1771 | u32 table_index = ~0; |
| 1772 | |
| 1773 | if (sw_if_index != ~0 |
| 1774 | && (sw_if_index < vec_len (cm->classify_table_index_by_sw_if_index))) |
| 1775 | table_index = cm->classify_table_index_by_sw_if_index[sw_if_index]; |
| 1776 | |
| 1777 | return table_index; |
| 1778 | } |
| 1779 | |
| 1780 | void |
| 1781 | classify_set_pcap_chain (vnet_classify_main_t * cm, |
| 1782 | u32 sw_if_index, u32 table_index) |
| 1783 | { |
| 1784 | vnet_main_t *vnm = vnet_get_main (); |
| 1785 | |
| 1786 | if (sw_if_index != ~0 && table_index != ~0) |
| 1787 | vec_validate_init_empty (cm->classify_table_index_by_sw_if_index, |
| 1788 | sw_if_index, ~0); |
| 1789 | |
| 1790 | if (table_index == ~0) |
| 1791 | { |
| 1792 | u32 old_table_index = ~0; |
| 1793 | |
| 1794 | if (sw_if_index < vec_len (cm->classify_table_index_by_sw_if_index)) |
| 1795 | old_table_index = |
| 1796 | cm->classify_table_index_by_sw_if_index[sw_if_index]; |
| 1797 | |
| 1798 | vnet_classify_delete_table_index (cm, old_table_index, 1); |
| 1799 | } |
| 1800 | |
| 1801 | /* |
| 1802 | * Put the table index where device drivers can find them. |
| 1803 | * This table index will be either a valid table or a ~0 to clear it. |
| 1804 | */ |
Steven Luong | 7f1d780 | 2021-01-19 23:09:51 -0800 | [diff] [blame] | 1805 | if (vec_len (cm->classify_table_index_by_sw_if_index) > sw_if_index) |
| 1806 | 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] | 1807 | if (sw_if_index > 0) |
| 1808 | { |
| 1809 | vnet_hw_interface_t *hi; |
| 1810 | hi = vnet_get_sup_hw_interface (vnm, sw_if_index); |
| 1811 | hi->trace_classify_table_index = table_index; |
| 1812 | } |
| 1813 | } |
| 1814 | |
| 1815 | |
| 1816 | /* |
| 1817 | * Search for a mask-compatible Classify table within the given table chain. |
| 1818 | */ |
| 1819 | u32 |
| 1820 | classify_lookup_chain (u32 table_index, u8 * mask, u32 n_skip, u32 n_match) |
| 1821 | { |
| 1822 | vnet_classify_main_t *cm = &vnet_classify_main; |
| 1823 | vnet_classify_table_t *t; |
| 1824 | u32 cti; |
| 1825 | |
| 1826 | if (table_index == ~0) |
| 1827 | return ~0; |
| 1828 | |
| 1829 | for (cti = table_index; cti != ~0; cti = t->next_table_index) |
| 1830 | { |
| 1831 | t = pool_elt_at_index (cm->tables, cti); |
| 1832 | |
| 1833 | /* Classifier geometry mismatch, can't use this table. */ |
| 1834 | if (t->match_n_vectors != n_match || t->skip_n_vectors != n_skip) |
| 1835 | continue; |
| 1836 | |
| 1837 | /* Masks aren't congruent, can't use this table. */ |
Damjan Marion | 3bb2da9 | 2021-09-20 13:39:37 +0200 | [diff] [blame] | 1838 | if (t->match_n_vectors * sizeof (u32x4) != vec_len (mask)) |
Jon Loeliger | 5c1e48c | 2020-10-15 14:41:36 -0400 | [diff] [blame] | 1839 | continue; |
| 1840 | |
| 1841 | /* Masks aren't bit-for-bit identical, can't use this table. */ |
Damjan Marion | 3bb2da9 | 2021-09-20 13:39:37 +0200 | [diff] [blame] | 1842 | if (memcmp (t->mask, mask, t->match_n_vectors * sizeof (u32x4))) |
Jon Loeliger | 5c1e48c | 2020-10-15 14:41:36 -0400 | [diff] [blame] | 1843 | continue; |
| 1844 | |
| 1845 | /* Winner... */ |
| 1846 | return cti; |
| 1847 | } |
| 1848 | |
| 1849 | return ~0; |
| 1850 | } |
| 1851 | |
| 1852 | |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 1853 | static clib_error_t * |
| 1854 | classify_filter_command_fn (vlib_main_t * vm, |
| 1855 | unformat_input_t * input, |
| 1856 | vlib_cli_command_t * cmd) |
| 1857 | { |
| 1858 | u32 nbuckets = 8; |
| 1859 | vnet_main_t *vnm = vnet_get_main (); |
| 1860 | uword memory_size = (uword) (128 << 10); |
| 1861 | u32 skip = ~0; |
| 1862 | u32 match = ~0; |
| 1863 | u8 *match_vector; |
| 1864 | int is_add = 1; |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 1865 | u32 table_index = ~0; |
| 1866 | u32 next_table_index = ~0; |
| 1867 | u32 miss_next_index = ~0; |
| 1868 | u32 current_data_flag = 0; |
| 1869 | int current_data_offset = 0; |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 1870 | u32 sw_if_index = ~0; |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 1871 | int pkt_trace = 0; |
Dave Barach | 29c6132 | 2019-12-24 16:59:38 -0500 | [diff] [blame] | 1872 | int pcap = 0; |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 1873 | u8 *mask = 0; |
| 1874 | vnet_classify_main_t *cm = &vnet_classify_main; |
| 1875 | int rv = 0; |
Jon Loeliger | 5f7f47e | 2020-11-03 15:49:10 -0500 | [diff] [blame] | 1876 | clib_error_t *err = 0; |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 1877 | |
Jon Loeliger | 5f7f47e | 2020-11-03 15:49:10 -0500 | [diff] [blame] | 1878 | unformat_input_t _line_input, *line_input = &_line_input; |
| 1879 | |
| 1880 | /* Get a line of input. */ |
| 1881 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 1882 | return 0; |
| 1883 | |
| 1884 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 1885 | { |
Jon Loeliger | 5f7f47e | 2020-11-03 15:49:10 -0500 | [diff] [blame] | 1886 | if (unformat (line_input, "del")) |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 1887 | is_add = 0; |
Jon Loeliger | 5f7f47e | 2020-11-03 15:49:10 -0500 | [diff] [blame] | 1888 | else if (unformat (line_input, "pcap %=", &pcap, 1)) |
Dave Barach | 29c6132 | 2019-12-24 16:59:38 -0500 | [diff] [blame] | 1889 | sw_if_index = 0; |
Jon Loeliger | 5f7f47e | 2020-11-03 15:49:10 -0500 | [diff] [blame] | 1890 | else if (unformat (line_input, "trace")) |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 1891 | pkt_trace = 1; |
Jon Loeliger | 5f7f47e | 2020-11-03 15:49:10 -0500 | [diff] [blame] | 1892 | else if (unformat (line_input, "%U", |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 1893 | unformat_vnet_sw_interface, vnm, &sw_if_index)) |
Dave Barach | 29c6132 | 2019-12-24 16:59:38 -0500 | [diff] [blame] | 1894 | { |
| 1895 | if (sw_if_index == 0) |
| 1896 | return clib_error_return (0, "Local interface not supported..."); |
| 1897 | } |
Jon Loeliger | 5f7f47e | 2020-11-03 15:49:10 -0500 | [diff] [blame] | 1898 | else if (unformat (line_input, "buckets %d", &nbuckets)) |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 1899 | ; |
Jon Loeliger | 5f7f47e | 2020-11-03 15:49:10 -0500 | [diff] [blame] | 1900 | else if (unformat (line_input, "mask %U", unformat_classify_mask, |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 1901 | &mask, &skip, &match)) |
| 1902 | ; |
Jon Loeliger | 5f7f47e | 2020-11-03 15:49:10 -0500 | [diff] [blame] | 1903 | else if (unformat (line_input, "memory-size %U", unformat_memory_size, |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 1904 | &memory_size)) |
| 1905 | ; |
| 1906 | else |
| 1907 | break; |
| 1908 | } |
| 1909 | |
Benoît Ganne | 8c45e51 | 2021-02-19 16:39:13 +0100 | [diff] [blame] | 1910 | if (is_add && mask == 0) |
Jon Loeliger | 5f7f47e | 2020-11-03 15:49:10 -0500 | [diff] [blame] | 1911 | err = clib_error_return (0, "Mask required"); |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 1912 | |
Benoît Ganne | 8c45e51 | 2021-02-19 16:39:13 +0100 | [diff] [blame] | 1913 | else if (is_add && skip == ~0) |
Jon Loeliger | 5f7f47e | 2020-11-03 15:49:10 -0500 | [diff] [blame] | 1914 | err = clib_error_return (0, "skip count required"); |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 1915 | |
Benoît Ganne | 8c45e51 | 2021-02-19 16:39:13 +0100 | [diff] [blame] | 1916 | else if (is_add && match == ~0) |
Jon Loeliger | 5f7f47e | 2020-11-03 15:49:10 -0500 | [diff] [blame] | 1917 | err = clib_error_return (0, "match count required"); |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 1918 | |
Jon Loeliger | 5f7f47e | 2020-11-03 15:49:10 -0500 | [diff] [blame] | 1919 | else if (sw_if_index == ~0 && pkt_trace == 0 && pcap == 0) |
| 1920 | err = clib_error_return (0, "Must specify trace, pcap or interface..."); |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 1921 | |
Jon Loeliger | 5f7f47e | 2020-11-03 15:49:10 -0500 | [diff] [blame] | 1922 | else if (pkt_trace && pcap) |
| 1923 | err = clib_error_return |
Dave Barach | 196fce2 | 2020-01-27 09:56:58 -0500 | [diff] [blame] | 1924 | (0, "Packet trace and pcap are mutually exclusive..."); |
| 1925 | |
Jon Loeliger | 5f7f47e | 2020-11-03 15:49:10 -0500 | [diff] [blame] | 1926 | else if (pkt_trace && sw_if_index != ~0) |
| 1927 | err = clib_error_return (0, "Packet trace filter is per-system"); |
| 1928 | |
| 1929 | if (err) |
| 1930 | { |
| 1931 | unformat_free (line_input); |
| 1932 | return err; |
| 1933 | } |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 1934 | |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 1935 | if (!is_add) |
| 1936 | { |
Jon Loeliger | 5c1e48c | 2020-10-15 14:41:36 -0400 | [diff] [blame] | 1937 | /* |
| 1938 | * Delete an existing PCAP or trace classify table. |
| 1939 | */ |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 1940 | if (pkt_trace) |
Jon Loeliger | 5c1e48c | 2020-10-15 14:41:36 -0400 | [diff] [blame] | 1941 | classify_set_trace_chain (cm, ~0); |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 1942 | else |
Jon Loeliger | 5c1e48c | 2020-10-15 14:41:36 -0400 | [diff] [blame] | 1943 | classify_set_pcap_chain (cm, sw_if_index, ~0); |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 1944 | |
Jon Loeliger | 5c1e48c | 2020-10-15 14:41:36 -0400 | [diff] [blame] | 1945 | vec_free (mask); |
Jon Loeliger | 5f7f47e | 2020-11-03 15:49:10 -0500 | [diff] [blame] | 1946 | unformat_free (line_input); |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 1947 | |
Jon Loeliger | 5f7f47e | 2020-11-03 15:49:10 -0500 | [diff] [blame] | 1948 | return 0; |
| 1949 | } |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 1950 | |
Jon Loeliger | 5c1e48c | 2020-10-15 14:41:36 -0400 | [diff] [blame] | 1951 | /* |
| 1952 | * Find an existing compatible table or else make a new one. |
| 1953 | */ |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 1954 | if (pkt_trace) |
Jon Loeliger | 5c1e48c | 2020-10-15 14:41:36 -0400 | [diff] [blame] | 1955 | table_index = classify_get_trace_chain (); |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 1956 | else |
Jon Loeliger | 5c1e48c | 2020-10-15 14:41:36 -0400 | [diff] [blame] | 1957 | table_index = classify_get_pcap_chain (cm, sw_if_index); |
| 1958 | |
| 1959 | if (table_index != ~0) |
Benoît Ganne | 8c45e51 | 2021-02-19 16:39:13 +0100 | [diff] [blame] | 1960 | { |
| 1961 | /* |
| 1962 | * look for a compatible table in the existing chain |
| 1963 | * - if a compatible table is found, table_index is updated with it |
| 1964 | * - if not, table_index is updated to ~0 (aka nil) and because of that |
| 1965 | * we are going to create one (see below). We save the original head |
| 1966 | * in next_table_index so we can chain it with the newly created |
| 1967 | * table |
| 1968 | */ |
| 1969 | next_table_index = table_index; |
| 1970 | table_index = classify_lookup_chain (table_index, mask, skip, match); |
| 1971 | } |
Jon Loeliger | 5c1e48c | 2020-10-15 14:41:36 -0400 | [diff] [blame] | 1972 | |
| 1973 | /* |
| 1974 | * When no table is found, make one. |
| 1975 | */ |
| 1976 | if (table_index == ~0) |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 1977 | { |
Benoît Ganne | 8c45e51 | 2021-02-19 16:39:13 +0100 | [diff] [blame] | 1978 | u32 new_head_index; |
| 1979 | |
Jon Loeliger | 5c1e48c | 2020-10-15 14:41:36 -0400 | [diff] [blame] | 1980 | /* |
| 1981 | * Matching table wasn't found, so create a new one at the |
| 1982 | * head of the next_table_index chain. |
| 1983 | */ |
Jon Loeliger | 5c1e48c | 2020-10-15 14:41:36 -0400 | [diff] [blame] | 1984 | rv = vnet_classify_add_del_table (cm, mask, nbuckets, memory_size, |
| 1985 | skip, match, next_table_index, |
| 1986 | miss_next_index, &table_index, |
| 1987 | current_data_flag, |
| 1988 | current_data_offset, 1, 0); |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 1989 | |
Jon Loeliger | 5c1e48c | 2020-10-15 14:41:36 -0400 | [diff] [blame] | 1990 | if (rv != 0) |
| 1991 | { |
| 1992 | vec_free (mask); |
| 1993 | unformat_free (line_input); |
| 1994 | return clib_error_return (0, |
| 1995 | "vnet_classify_add_del_table returned %d", |
| 1996 | rv); |
| 1997 | } |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 1998 | |
Jon Loeliger | 5c1e48c | 2020-10-15 14:41:36 -0400 | [diff] [blame] | 1999 | /* |
| 2000 | * Reorder tables such that masks are most-specify to least-specific. |
| 2001 | */ |
Benoît Ganne | 8c45e51 | 2021-02-19 16:39:13 +0100 | [diff] [blame] | 2002 | new_head_index = classify_sort_table_chain (cm, table_index); |
Jon Loeliger | 5c1e48c | 2020-10-15 14:41:36 -0400 | [diff] [blame] | 2003 | |
| 2004 | /* |
| 2005 | * Put first classifier table in chain in a place where |
| 2006 | * other data structures expect to find and use it. |
| 2007 | */ |
| 2008 | if (pkt_trace) |
Benoît Ganne | 8c45e51 | 2021-02-19 16:39:13 +0100 | [diff] [blame] | 2009 | classify_set_trace_chain (cm, new_head_index); |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 2010 | else |
Benoît Ganne | 8c45e51 | 2021-02-19 16:39:13 +0100 | [diff] [blame] | 2011 | classify_set_pcap_chain (cm, sw_if_index, new_head_index); |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 2012 | } |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 2013 | |
Jon Loeliger | 5c1e48c | 2020-10-15 14:41:36 -0400 | [diff] [blame] | 2014 | vec_free (mask); |
Jon Loeliger | 362c666 | 2020-09-21 16:48:54 -0500 | [diff] [blame] | 2015 | |
Jon Loeliger | 5c1e48c | 2020-10-15 14:41:36 -0400 | [diff] [blame] | 2016 | /* |
| 2017 | * Now try to parse a and add a filter-match session. |
| 2018 | */ |
Jon Loeliger | 5f7f47e | 2020-11-03 15:49:10 -0500 | [diff] [blame] | 2019 | if (unformat (line_input, "match %U", unformat_classify_match, |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 2020 | cm, &match_vector, table_index) == 0) |
| 2021 | return 0; |
| 2022 | |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 2023 | /* |
| 2024 | * We use hit or miss to determine whether to trace or pcap pkts |
| 2025 | * so the session setup is very limited |
| 2026 | */ |
| 2027 | rv = vnet_classify_add_del_session (cm, table_index, |
| 2028 | match_vector, 0 /* hit_next_index */ , |
| 2029 | 0 /* opaque_index */ , |
| 2030 | 0 /* advance */ , |
| 2031 | 0 /* action */ , |
| 2032 | 0 /* metadata */ , |
| 2033 | 1 /* is_add */ ); |
| 2034 | |
| 2035 | vec_free (match_vector); |
| 2036 | |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 2037 | return 0; |
| 2038 | } |
| 2039 | |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 2040 | /** Enable / disable packet trace filter */ |
| 2041 | int |
| 2042 | vlib_enable_disable_pkt_trace_filter (int enable) |
| 2043 | { |
| 2044 | if (enable) |
| 2045 | { |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 2046 | vlib_global_main.trace_filter.trace_filter_enable = 1; |
| 2047 | } |
| 2048 | else |
| 2049 | { |
| 2050 | vlib_global_main.trace_filter.trace_filter_enable = 0; |
| 2051 | } |
| 2052 | return 0; |
| 2053 | } |
| 2054 | |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 2055 | /*? |
| 2056 | * Construct an arbitrary set of packet classifier tables for use with |
Nobuhiro MIKI | d346f39 | 2023-02-28 18:30:09 +0900 | [diff] [blame^] | 2057 | * "pcap trace rx | tx," and with the vpp packet tracer |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 2058 | * |
| 2059 | * Packets which match a rule in the classifier table chain |
| 2060 | * will be traced. The tables are automatically ordered so that |
| 2061 | * matches in the most specific table are tried first. |
| 2062 | * |
| 2063 | * It's reasonably likely that folks will configure a single |
| 2064 | * table with one or two matches. As a result, we configure |
| 2065 | * 8 hash buckets and 128K of match rule space. One can override |
Nathan Skrzypczak | 2c77ae4 | 2021-09-29 15:36:51 +0200 | [diff] [blame] | 2066 | * the defaults by specifying "buckets <nnn>" and "memory-size <xxx>" |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 2067 | * as desired. |
| 2068 | * |
| 2069 | * To build up complex filter chains, repeatedly issue the |
| 2070 | * classify filter debug CLI command. Each command must specify the desired |
| 2071 | * mask and match values. If a classifier table with a suitable mask |
| 2072 | * already exists, the CLI command adds a match rule to the existing table. |
| 2073 | * If not, the CLI command add a new table and the indicated mask rule |
| 2074 | * |
| 2075 | * Here is a terse description of the "mask <xxx>" syntax: |
| 2076 | * |
| 2077 | * l2 src dst proto tag1 tag2 ignore-tag1 ignore-tag2 cos1 cos2 dot1q dot1ad |
| 2078 | * |
| 2079 | * l3 ip4 <ip4-mask> ip6 <ip6-mask> |
| 2080 | * |
| 2081 | * <ip4-mask> version hdr_length src[/width] dst[/width] |
| 2082 | * tos length fragment_id ttl protocol checksum |
| 2083 | * |
| 2084 | * <ip6-mask> version traffic-class flow-label src dst proto |
| 2085 | * payload_length hop_limit protocol |
| 2086 | * |
| 2087 | * l4 tcp <tcp-mask> udp <udp_mask> src_port dst_port |
| 2088 | * |
| 2089 | * <tcp-mask> src dst # ports |
| 2090 | * |
| 2091 | * <udp-mask> src_port dst_port |
| 2092 | * |
| 2093 | * To construct matches, add the values to match after the indicated keywords: |
| 2094 | * in the match syntax. For example: |
| 2095 | * mask l3 ip4 src -> match l3 ip4 src 192.168.1.11 |
| 2096 | * |
| 2097 | * @cliexpar |
| 2098 | * Configuring the classify filter |
| 2099 | * |
Nobuhiro MIKI | d346f39 | 2023-02-28 18:30:09 +0900 | [diff] [blame^] | 2100 | * Configure a simple classify filter, and configure pcap trace rx to use it: |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 2101 | * |
Nathan Skrzypczak | 2c77ae4 | 2021-09-29 15:36:51 +0200 | [diff] [blame] | 2102 | * @cliexcmd{classify filter rx mask l3 ip4 src match l3 ip4 src 192.168.1.11} |
Nobuhiro MIKI | d346f39 | 2023-02-28 18:30:09 +0900 | [diff] [blame^] | 2103 | * <b><em>pcap trace rx max 100 filter</em></b> |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 2104 | * |
| 2105 | * Configure another fairly simple filter |
| 2106 | * |
Nathan Skrzypczak | 2c77ae4 | 2021-09-29 15:36:51 +0200 | [diff] [blame] | 2107 | * @cliexcmd{classify filter mask l3 ip4 src dst match l3 ip4 src 192.168.1.10 |
| 2108 | * dst 192.168.2.10} |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 2109 | * |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 2110 | * |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 2111 | * Configure a filter for use with the vpp packet tracer: |
Nathan Skrzypczak | 2c77ae4 | 2021-09-29 15:36:51 +0200 | [diff] [blame] | 2112 | * @cliexcmd{classify filter trace mask l3 ip4 src dst match l3 ip4 src |
| 2113 | * 192.168.1.10 dst 192.168.2.10} |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 2114 | * <b><em>trace add dpdk-input 100 filter</em></b> |
| 2115 | * |
| 2116 | * Clear classifier filters |
| 2117 | * |
| 2118 | * <b><em>classify filter [trace | rx | tx | <intfc>] del</em></b> |
| 2119 | * |
| 2120 | * To display the top-level classifier tables for each use case: |
Nathan Skrzypczak | 2c77ae4 | 2021-09-29 15:36:51 +0200 | [diff] [blame] | 2121 | * <b><em>show classify filter</em></b> |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 2122 | * |
| 2123 | * To inspect the classifier tables, use |
| 2124 | * |
| 2125 | * <b><em>show classify table [verbose]</em></b> |
| 2126 | * The verbose form displays all of the match rules, with hit-counters |
| 2127 | * @cliexend |
| 2128 | ?*/ |
| 2129 | /* *INDENT-OFF* */ |
| 2130 | VLIB_CLI_COMMAND (classify_filter, static) = |
| 2131 | { |
| 2132 | .path = "classify filter", |
| 2133 | .short_help = |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 2134 | "classify filter <intfc> | pcap mask <mask-value> match <match-value>\n" |
| 2135 | " | trace mask <mask-value> match <match-value> [del]\n" |
| 2136 | " [buckets <nn>] [memory-size <n>]", |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 2137 | .function = classify_filter_command_fn, |
| 2138 | }; |
| 2139 | /* *INDENT-ON* */ |
| 2140 | |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 2141 | static clib_error_t * |
| 2142 | show_classify_filter_command_fn (vlib_main_t * vm, |
| 2143 | unformat_input_t * input, |
| 2144 | vlib_cli_command_t * cmd) |
| 2145 | { |
| 2146 | vnet_classify_main_t *cm = &vnet_classify_main; |
| 2147 | vnet_main_t *vnm = vnet_get_main (); |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 2148 | u8 *name = 0; |
| 2149 | u8 *s = 0; |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 2150 | u32 table_index; |
| 2151 | int verbose = 0; |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 2152 | int i, j, limit; |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 2153 | |
| 2154 | (void) unformat (input, "verbose %=", &verbose, 1); |
| 2155 | |
| 2156 | vlib_cli_output (vm, "%-30s%s", "Filter Used By", " Table(s)"); |
| 2157 | vlib_cli_output (vm, "%-30s%s", "--------------", " --------"); |
| 2158 | |
Jon Loeliger | 5c1e48c | 2020-10-15 14:41:36 -0400 | [diff] [blame] | 2159 | limit = vec_len (cm->classify_table_index_by_sw_if_index); |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 2160 | |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 2161 | for (i = -1; i < limit; i++) |
| 2162 | { |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 2163 | switch (i) |
| 2164 | { |
| 2165 | case -1: |
Jon Loeliger | 5c1e48c | 2020-10-15 14:41:36 -0400 | [diff] [blame] | 2166 | table_index = vlib_global_main.trace_filter.classify_table_index; |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 2167 | name = format (0, "packet tracer:"); |
| 2168 | break; |
Jon Loeliger | 5c1e48c | 2020-10-15 14:41:36 -0400 | [diff] [blame] | 2169 | |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 2170 | case 0: |
Jon Loeliger | 5c1e48c | 2020-10-15 14:41:36 -0400 | [diff] [blame] | 2171 | table_index = cm->classify_table_index_by_sw_if_index[i]; |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 2172 | name = format (0, "pcap rx/tx/drop:"); |
| 2173 | break; |
Jon Loeliger | 5c1e48c | 2020-10-15 14:41:36 -0400 | [diff] [blame] | 2174 | |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 2175 | default: |
Jon Loeliger | 5c1e48c | 2020-10-15 14:41:36 -0400 | [diff] [blame] | 2176 | table_index = cm->classify_table_index_by_sw_if_index[i]; |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 2177 | name = format (0, "%U:", format_vnet_sw_if_index_name, vnm, i); |
| 2178 | break; |
| 2179 | } |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 2180 | |
| 2181 | if (verbose) |
| 2182 | { |
Jon Loeliger | 5c1e48c | 2020-10-15 14:41:36 -0400 | [diff] [blame] | 2183 | vnet_classify_table_t *t; |
| 2184 | j = table_index; |
| 2185 | do |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 2186 | { |
Jon Loeliger | 5c1e48c | 2020-10-15 14:41:36 -0400 | [diff] [blame] | 2187 | if (j == ~0) |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 2188 | s = format (s, " none"); |
Jon Loeliger | 5c1e48c | 2020-10-15 14:41:36 -0400 | [diff] [blame] | 2189 | else |
| 2190 | { |
| 2191 | s = format (s, " %u", j); |
| 2192 | t = pool_elt_at_index (cm->tables, j); |
| 2193 | j = t->next_table_index; |
| 2194 | } |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 2195 | } |
Jon Loeliger | 5c1e48c | 2020-10-15 14:41:36 -0400 | [diff] [blame] | 2196 | while (j != ~0); |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 2197 | |
Dave Barach | 3268a64 | 2019-11-29 08:40:58 -0500 | [diff] [blame] | 2198 | vlib_cli_output (vm, "%-30v table(s)%v", name, s); |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 2199 | vec_reset_length (s); |
| 2200 | } |
| 2201 | else |
| 2202 | { |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 2203 | if (table_index != ~0) |
| 2204 | s = format (s, " %u", table_index); |
| 2205 | else |
| 2206 | s = format (s, " none"); |
| 2207 | |
Dave Barach | 3268a64 | 2019-11-29 08:40:58 -0500 | [diff] [blame] | 2208 | vlib_cli_output (vm, "%-30v first table%v", name, s); |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 2209 | vec_reset_length (s); |
| 2210 | } |
| 2211 | vec_reset_length (name); |
| 2212 | } |
| 2213 | vec_free (s); |
| 2214 | vec_free (name); |
| 2215 | return 0; |
| 2216 | } |
| 2217 | |
| 2218 | |
| 2219 | /* *INDENT-OFF* */ |
| 2220 | VLIB_CLI_COMMAND (show_classify_filter, static) = |
| 2221 | { |
| 2222 | .path = "show classify filter", |
| 2223 | .short_help = "show classify filter [verbose [nn]]", |
| 2224 | .function = show_classify_filter_command_fn, |
| 2225 | }; |
| 2226 | /* *INDENT-ON* */ |
| 2227 | |
Neale Ranns | 1441a6c | 2020-12-14 16:02:17 +0000 | [diff] [blame] | 2228 | u8 * |
| 2229 | format_vnet_classify_table (u8 *s, va_list *args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2230 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2231 | vnet_classify_main_t *cm = va_arg (*args, vnet_classify_main_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2232 | int verbose = va_arg (*args, int); |
| 2233 | u32 index = va_arg (*args, u32); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2234 | vnet_classify_table_t *t; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2235 | |
| 2236 | if (index == ~0) |
| 2237 | { |
Dave Barach | eb2e1f9 | 2021-09-01 09:02:13 -0400 | [diff] [blame] | 2238 | s = format (s, "\n%10s%10s%10s%10s", "TableIdx", "Sessions", "NextTbl", |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2239 | "NextNode", verbose ? "Details" : ""); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2240 | return s; |
| 2241 | } |
| 2242 | |
| 2243 | t = pool_elt_at_index (cm->tables, index); |
| 2244 | s = format (s, "%10u%10d%10d%10d", index, t->active_elements, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2245 | t->next_table_index, t->miss_next_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2246 | |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 2247 | s = format (s, "\n Heap: %U", format_clib_mem_heap, t->mheap, |
| 2248 | 0 /*verbose */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2249 | |
Steve Shin | 25e26dc | 2016-11-08 10:47:10 -0800 | [diff] [blame] | 2250 | 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] | 2251 | t->nbuckets, t->skip_n_vectors, t->match_n_vectors, |
| 2252 | t->current_data_flag, t->current_data_offset); |
| 2253 | s = format (s, "\n mask %U", format_hex_bytes, t->mask, |
| 2254 | t->match_n_vectors * sizeof (u32x4)); |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 2255 | s = format (s, "\n linear-search buckets %d\n", t->linear_buckets); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2256 | |
| 2257 | if (verbose == 0) |
| 2258 | return s; |
| 2259 | |
| 2260 | s = format (s, "\n%U", format_classify_table, t, verbose); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2261 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2262 | return s; |
| 2263 | } |
| 2264 | |
| 2265 | static clib_error_t * |
| 2266 | show_classify_tables_command_fn (vlib_main_t * vm, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2267 | unformat_input_t * input, |
| 2268 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2269 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2270 | vnet_classify_main_t *cm = &vnet_classify_main; |
| 2271 | vnet_classify_table_t *t; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2272 | u32 match_index = ~0; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2273 | u32 *indices = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2274 | int verbose = 0; |
| 2275 | int i; |
| 2276 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2277 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2278 | { |
| 2279 | if (unformat (input, "index %d", &match_index)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2280 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2281 | else if (unformat (input, "verbose %d", &verbose)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2282 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2283 | else if (unformat (input, "verbose")) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2284 | verbose = 1; |
| 2285 | else |
| 2286 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2287 | } |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2288 | |
| 2289 | /* *INDENT-OFF* */ |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 2290 | pool_foreach (t, cm->tables) |
| 2291 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2292 | if (match_index == ~0 || (match_index == t - cm->tables)) |
| 2293 | vec_add1 (indices, t - cm->tables); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 2294 | } |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2295 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2296 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2297 | if (vec_len (indices)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2298 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2299 | for (i = 0; i < vec_len (indices); i++) |
Dave Barach | eb2e1f9 | 2021-09-01 09:02:13 -0400 | [diff] [blame] | 2300 | { |
| 2301 | vlib_cli_output (vm, "%U", format_vnet_classify_table, cm, verbose, |
| 2302 | ~0 /* hdr */); |
| 2303 | vlib_cli_output (vm, "%U", format_vnet_classify_table, cm, verbose, |
| 2304 | indices[i]); |
| 2305 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2306 | } |
| 2307 | else |
| 2308 | vlib_cli_output (vm, "No classifier tables configured"); |
| 2309 | |
| 2310 | vec_free (indices); |
| 2311 | |
| 2312 | return 0; |
| 2313 | } |
| 2314 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2315 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2316 | VLIB_CLI_COMMAND (show_classify_table_command, static) = { |
| 2317 | .path = "show classify tables", |
| 2318 | .short_help = "show classify tables [index <nn>]", |
| 2319 | .function = show_classify_tables_command_fn, |
| 2320 | }; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2321 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2322 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2323 | uword |
| 2324 | unformat_l4_match (unformat_input_t * input, va_list * args) |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 2325 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2326 | u8 **matchp = va_arg (*args, u8 **); |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 2327 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2328 | u8 *proto_header = 0; |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 2329 | int src_port = 0; |
| 2330 | int dst_port = 0; |
| 2331 | |
| 2332 | tcpudp_header_t h; |
| 2333 | |
| 2334 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 2335 | { |
| 2336 | if (unformat (input, "src_port %d", &src_port)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2337 | ; |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 2338 | else if (unformat (input, "dst_port %d", &dst_port)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2339 | ; |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 2340 | else |
Benoît Ganne | 4b9246a | 2021-08-04 18:48:41 +0200 | [diff] [blame] | 2341 | break; |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 2342 | } |
| 2343 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2344 | h.src_port = clib_host_to_net_u16 (src_port); |
| 2345 | h.dst_port = clib_host_to_net_u16 (dst_port); |
| 2346 | vec_validate (proto_header, sizeof (h) - 1); |
| 2347 | memcpy (proto_header, &h, sizeof (h)); |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 2348 | |
| 2349 | *matchp = proto_header; |
| 2350 | |
| 2351 | return 1; |
| 2352 | } |
| 2353 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2354 | uword |
| 2355 | unformat_ip4_match (unformat_input_t * input, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2356 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2357 | u8 **matchp = va_arg (*args, u8 **); |
| 2358 | u8 *match = 0; |
| 2359 | ip4_header_t *ip; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2360 | int version = 0; |
| 2361 | u32 version_val; |
| 2362 | int hdr_length = 0; |
| 2363 | u32 hdr_length_val; |
| 2364 | int src = 0, dst = 0; |
| 2365 | ip4_address_t src_val, dst_val; |
| 2366 | int proto = 0; |
| 2367 | u32 proto_val; |
| 2368 | int tos = 0; |
| 2369 | u32 tos_val; |
| 2370 | int length = 0; |
| 2371 | u32 length_val; |
| 2372 | int fragment_id = 0; |
| 2373 | u32 fragment_id_val; |
| 2374 | int ttl = 0; |
| 2375 | int ttl_val; |
| 2376 | int checksum = 0; |
| 2377 | u32 checksum_val; |
| 2378 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2379 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2380 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2381 | if (unformat (input, "version %d", &version_val)) |
| 2382 | version = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2383 | else if (unformat (input, "hdr_length %d", &hdr_length_val)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2384 | hdr_length = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2385 | else if (unformat (input, "src %U", unformat_ip4_address, &src_val)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2386 | src = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2387 | else if (unformat (input, "dst %U", unformat_ip4_address, &dst_val)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2388 | dst = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2389 | else if (unformat (input, "proto %d", &proto_val)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2390 | proto = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2391 | else if (unformat (input, "tos %d", &tos_val)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2392 | tos = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2393 | else if (unformat (input, "length %d", &length_val)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2394 | length = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2395 | else if (unformat (input, "fragment_id %d", &fragment_id_val)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2396 | fragment_id = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2397 | else if (unformat (input, "ttl %d", &ttl_val)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2398 | ttl = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2399 | else if (unformat (input, "checksum %d", &checksum_val)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2400 | checksum = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2401 | else |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2402 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2403 | } |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2404 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2405 | if (version + hdr_length + src + dst + proto + tos + length + fragment_id |
| 2406 | + ttl + checksum == 0) |
| 2407 | return 0; |
| 2408 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2409 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2410 | * Aligned because we use the real comparison functions |
| 2411 | */ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2412 | vec_validate_aligned (match, sizeof (*ip) - 1, sizeof (u32x4)); |
| 2413 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2414 | ip = (ip4_header_t *) match; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2415 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2416 | /* These are realistically matched in practice */ |
| 2417 | if (src) |
| 2418 | ip->src_address.as_u32 = src_val.as_u32; |
| 2419 | |
| 2420 | if (dst) |
| 2421 | ip->dst_address.as_u32 = dst_val.as_u32; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2422 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2423 | if (proto) |
| 2424 | ip->protocol = proto_val; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2425 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2426 | |
| 2427 | /* These are not, but they're included for completeness */ |
| 2428 | if (version) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2429 | ip->ip_version_and_header_length |= (version_val & 0xF) << 4; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2430 | |
| 2431 | if (hdr_length) |
| 2432 | ip->ip_version_and_header_length |= (hdr_length_val & 0xF); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2433 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2434 | if (tos) |
| 2435 | ip->tos = tos_val; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2436 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2437 | if (length) |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 2438 | ip->length = clib_host_to_net_u16 (length_val); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2439 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2440 | if (ttl) |
| 2441 | ip->ttl = ttl_val; |
| 2442 | |
| 2443 | if (checksum) |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 2444 | ip->checksum = clib_host_to_net_u16 (checksum_val); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2445 | |
| 2446 | *matchp = match; |
| 2447 | return 1; |
| 2448 | } |
| 2449 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2450 | uword |
| 2451 | unformat_ip6_match (unformat_input_t * input, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2452 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2453 | u8 **matchp = va_arg (*args, u8 **); |
| 2454 | u8 *match = 0; |
| 2455 | ip6_header_t *ip; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2456 | int version = 0; |
| 2457 | u32 version_val; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2458 | u8 traffic_class = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2459 | u32 traffic_class_val; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2460 | u8 flow_label = 0; |
| 2461 | u8 flow_label_val; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2462 | int src = 0, dst = 0; |
| 2463 | ip6_address_t src_val, dst_val; |
| 2464 | int proto = 0; |
| 2465 | u32 proto_val; |
| 2466 | int payload_length = 0; |
| 2467 | u32 payload_length_val; |
| 2468 | int hop_limit = 0; |
| 2469 | int hop_limit_val; |
| 2470 | u32 ip_version_traffic_class_and_flow_label; |
| 2471 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2472 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2473 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2474 | if (unformat (input, "version %d", &version_val)) |
| 2475 | version = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2476 | else if (unformat (input, "traffic_class %d", &traffic_class_val)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2477 | traffic_class = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2478 | else if (unformat (input, "flow_label %d", &flow_label_val)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2479 | flow_label = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2480 | else if (unformat (input, "src %U", unformat_ip6_address, &src_val)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2481 | src = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2482 | else if (unformat (input, "dst %U", unformat_ip6_address, &dst_val)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2483 | dst = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2484 | else if (unformat (input, "proto %d", &proto_val)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2485 | proto = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2486 | else if (unformat (input, "payload_length %d", &payload_length_val)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2487 | payload_length = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2488 | else if (unformat (input, "hop_limit %d", &hop_limit_val)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2489 | hop_limit = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2490 | else |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2491 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2492 | } |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2493 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2494 | if (version + traffic_class + flow_label + src + dst + proto + |
| 2495 | payload_length + hop_limit == 0) |
| 2496 | return 0; |
| 2497 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2498 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2499 | * Aligned because we use the real comparison functions |
| 2500 | */ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2501 | vec_validate_aligned (match, sizeof (*ip) - 1, sizeof (u32x4)); |
| 2502 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2503 | ip = (ip6_header_t *) match; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2504 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2505 | if (src) |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 2506 | clib_memcpy_fast (&ip->src_address, &src_val, sizeof (ip->src_address)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2507 | |
| 2508 | if (dst) |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 2509 | clib_memcpy_fast (&ip->dst_address, &dst_val, sizeof (ip->dst_address)); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2510 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2511 | if (proto) |
| 2512 | ip->protocol = proto_val; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2513 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2514 | ip_version_traffic_class_and_flow_label = 0; |
| 2515 | |
| 2516 | if (version) |
| 2517 | ip_version_traffic_class_and_flow_label |= (version_val & 0xF) << 28; |
| 2518 | |
| 2519 | if (traffic_class) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2520 | ip_version_traffic_class_and_flow_label |= |
| 2521 | (traffic_class_val & 0xFF) << 20; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2522 | |
| 2523 | if (flow_label) |
| 2524 | ip_version_traffic_class_and_flow_label |= (flow_label_val & 0xFFFFF); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2525 | |
| 2526 | ip->ip_version_traffic_class_and_flow_label = |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2527 | clib_host_to_net_u32 (ip_version_traffic_class_and_flow_label); |
| 2528 | |
| 2529 | if (payload_length) |
| 2530 | ip->payload_length = clib_host_to_net_u16 (payload_length_val); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2531 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2532 | if (hop_limit) |
| 2533 | ip->hop_limit = hop_limit_val; |
| 2534 | |
| 2535 | *matchp = match; |
| 2536 | return 1; |
| 2537 | } |
| 2538 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2539 | uword |
| 2540 | unformat_l3_match (unformat_input_t * input, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2541 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2542 | u8 **matchp = va_arg (*args, u8 **); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2543 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2544 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 2545 | { |
| 2546 | if (unformat (input, "ip4 %U", unformat_ip4_match, matchp)) |
| 2547 | return 1; |
| 2548 | else if (unformat (input, "ip6 %U", unformat_ip6_match, matchp)) |
| 2549 | return 1; |
| 2550 | /* $$$$ add mpls */ |
| 2551 | else |
| 2552 | break; |
| 2553 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2554 | return 0; |
| 2555 | } |
| 2556 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2557 | uword |
| 2558 | unformat_vlan_tag (unformat_input_t * input, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2559 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2560 | u8 *tagp = va_arg (*args, u8 *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2561 | u32 tag; |
| 2562 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2563 | if (unformat (input, "%d", &tag)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2564 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2565 | tagp[0] = (tag >> 8) & 0x0F; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2566 | tagp[1] = tag & 0xFF; |
| 2567 | return 1; |
| 2568 | } |
| 2569 | |
| 2570 | return 0; |
| 2571 | } |
| 2572 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2573 | uword |
| 2574 | unformat_l2_match (unformat_input_t * input, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2575 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2576 | u8 **matchp = va_arg (*args, u8 **); |
| 2577 | u8 *match = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2578 | u8 src = 0; |
| 2579 | u8 src_val[6]; |
| 2580 | u8 dst = 0; |
| 2581 | u8 dst_val[6]; |
| 2582 | u8 proto = 0; |
| 2583 | u16 proto_val; |
| 2584 | u8 tag1 = 0; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2585 | u8 tag1_val[2]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2586 | u8 tag2 = 0; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2587 | u8 tag2_val[2]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2588 | int len = 14; |
| 2589 | u8 ignore_tag1 = 0; |
| 2590 | u8 ignore_tag2 = 0; |
| 2591 | u8 cos1 = 0; |
| 2592 | u8 cos2 = 0; |
| 2593 | u32 cos1_val = 0; |
| 2594 | u32 cos2_val = 0; |
| 2595 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2596 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 2597 | { |
| 2598 | if (unformat (input, "src %U", unformat_ethernet_address, &src_val)) |
| 2599 | src = 1; |
| 2600 | else |
| 2601 | if (unformat (input, "dst %U", unformat_ethernet_address, &dst_val)) |
| 2602 | dst = 1; |
| 2603 | else if (unformat (input, "proto %U", |
| 2604 | unformat_ethernet_type_host_byte_order, &proto_val)) |
| 2605 | proto = 1; |
| 2606 | else if (unformat (input, "tag1 %U", unformat_vlan_tag, tag1_val)) |
| 2607 | tag1 = 1; |
| 2608 | else if (unformat (input, "tag2 %U", unformat_vlan_tag, tag2_val)) |
| 2609 | tag2 = 1; |
| 2610 | else if (unformat (input, "ignore-tag1")) |
| 2611 | ignore_tag1 = 1; |
| 2612 | else if (unformat (input, "ignore-tag2")) |
| 2613 | ignore_tag2 = 1; |
| 2614 | else if (unformat (input, "cos1 %d", &cos1_val)) |
| 2615 | cos1 = 1; |
| 2616 | else if (unformat (input, "cos2 %d", &cos2_val)) |
| 2617 | cos2 = 1; |
| 2618 | else |
| 2619 | break; |
| 2620 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2621 | if ((src + dst + proto + tag1 + tag2 + |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2622 | ignore_tag1 + ignore_tag2 + cos1 + cos2) == 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2623 | return 0; |
| 2624 | |
| 2625 | if (tag1 || ignore_tag1 || cos1) |
| 2626 | len = 18; |
| 2627 | if (tag2 || ignore_tag2 || cos2) |
| 2628 | len = 22; |
| 2629 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2630 | vec_validate_aligned (match, len - 1, sizeof (u32x4)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2631 | |
| 2632 | if (dst) |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 2633 | clib_memcpy_fast (match, dst_val, 6); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2634 | |
| 2635 | if (src) |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 2636 | clib_memcpy_fast (match + 6, src_val, 6); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2637 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2638 | if (tag2) |
| 2639 | { |
| 2640 | /* inner vlan tag */ |
| 2641 | match[19] = tag2_val[1]; |
| 2642 | match[18] = tag2_val[0]; |
| 2643 | if (cos2) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2644 | match[18] |= (cos2_val & 0x7) << 5; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2645 | if (proto) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2646 | { |
| 2647 | match[21] = proto_val & 0xff; |
| 2648 | match[20] = proto_val >> 8; |
| 2649 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2650 | if (tag1) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2651 | { |
| 2652 | match[15] = tag1_val[1]; |
| 2653 | match[14] = tag1_val[0]; |
| 2654 | } |
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 | *matchp = match; |
| 2658 | return 1; |
| 2659 | } |
| 2660 | if (tag1) |
| 2661 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2662 | match[15] = tag1_val[1]; |
| 2663 | match[14] = tag1_val[0]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2664 | if (proto) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2665 | { |
| 2666 | match[17] = proto_val & 0xff; |
| 2667 | match[16] = proto_val >> 8; |
| 2668 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2669 | if (cos1) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2670 | match[14] |= (cos1_val & 0x7) << 5; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2671 | |
| 2672 | *matchp = match; |
| 2673 | return 1; |
| 2674 | } |
| 2675 | if (cos2) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2676 | match[18] |= (cos2_val & 0x7) << 5; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2677 | if (cos1) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2678 | match[14] |= (cos1_val & 0x7) << 5; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2679 | if (proto) |
| 2680 | { |
| 2681 | match[13] = proto_val & 0xff; |
| 2682 | match[12] = proto_val >> 8; |
| 2683 | } |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2684 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2685 | *matchp = match; |
| 2686 | return 1; |
| 2687 | } |
| 2688 | |
| 2689 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2690 | uword |
| 2691 | unformat_classify_match (unformat_input_t * input, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2692 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2693 | vnet_classify_main_t *cm = va_arg (*args, vnet_classify_main_t *); |
| 2694 | u8 **matchp = va_arg (*args, u8 **); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2695 | u32 table_index = va_arg (*args, u32); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2696 | vnet_classify_table_t *t; |
| 2697 | |
| 2698 | u8 *match = 0; |
| 2699 | u8 *l2 = 0; |
| 2700 | u8 *l3 = 0; |
| 2701 | u8 *l4 = 0; |
Arthur de Kerhor | 9a63b6e | 2022-03-03 10:33:23 +0100 | [diff] [blame] | 2702 | u8 add_l2 = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2703 | |
| 2704 | if (pool_is_free_index (cm->tables, table_index)) |
| 2705 | return 0; |
| 2706 | |
| 2707 | t = pool_elt_at_index (cm->tables, table_index); |
| 2708 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2709 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 2710 | { |
| 2711 | if (unformat (input, "hex %U", unformat_hex_string, &match)) |
| 2712 | ; |
Arthur de Kerhor | 9a63b6e | 2022-03-03 10:33:23 +0100 | [diff] [blame] | 2713 | else if (unformat (input, "l2 none")) |
| 2714 | /* Don't add the l2 header in the mask */ |
| 2715 | add_l2 = 0; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2716 | else if (unformat (input, "l2 %U", unformat_l2_match, &l2)) |
| 2717 | ; |
| 2718 | else if (unformat (input, "l3 %U", unformat_l3_match, &l3)) |
| 2719 | ; |
| 2720 | else if (unformat (input, "l4 %U", unformat_l4_match, &l4)) |
| 2721 | ; |
| 2722 | else |
| 2723 | break; |
| 2724 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2725 | |
Arthur de Kerhor | 9a63b6e | 2022-03-03 10:33:23 +0100 | [diff] [blame] | 2726 | if (l2 && !add_l2) |
| 2727 | { |
| 2728 | vec_free (match); |
| 2729 | vec_free (l2); |
| 2730 | vec_free (l3); |
| 2731 | vec_free (l4); |
| 2732 | return 0; |
| 2733 | } |
| 2734 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2735 | if (l4 && !l3) |
| 2736 | { |
| 2737 | vec_free (match); |
| 2738 | vec_free (l2); |
| 2739 | vec_free (l4); |
| 2740 | return 0; |
| 2741 | } |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 2742 | |
| 2743 | if (match || l2 || l3 || l4) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2744 | { |
Juraj Sloboda | 51ffa81 | 2016-08-07 23:46:45 -0700 | [diff] [blame] | 2745 | if (l2 || l3 || l4) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2746 | { |
Arthur de Kerhor | 9a63b6e | 2022-03-03 10:33:23 +0100 | [diff] [blame] | 2747 | if (add_l2) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2748 | { |
Arthur de Kerhor | 9a63b6e | 2022-03-03 10:33:23 +0100 | [diff] [blame] | 2749 | /* "Win a free Ethernet header in every packet" */ |
| 2750 | if (l2 == 0) |
| 2751 | vec_validate_aligned (l2, 13, sizeof (u32x4)); |
| 2752 | match = l2; |
| 2753 | if (l3) |
| 2754 | { |
| 2755 | vec_append_aligned (match, l3, sizeof (u32x4)); |
| 2756 | vec_free (l3); |
| 2757 | } |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2758 | } |
Arthur de Kerhor | 9a63b6e | 2022-03-03 10:33:23 +0100 | [diff] [blame] | 2759 | else |
| 2760 | match = l3; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2761 | if (l4) |
| 2762 | { |
| 2763 | vec_append_aligned (match, l4, sizeof (u32x4)); |
| 2764 | vec_free (l4); |
| 2765 | } |
| 2766 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2767 | |
| 2768 | /* 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] | 2769 | vec_validate_aligned |
| 2770 | (match, |
| 2771 | ((t->match_n_vectors + t->skip_n_vectors) * sizeof (u32x4)) - 1, |
| 2772 | sizeof (u32x4)); |
| 2773 | |
| 2774 | /* Set size, include skipped vectors */ |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 2775 | vec_set_len (match, |
| 2776 | (t->match_n_vectors + t->skip_n_vectors) * sizeof (u32x4)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2777 | |
| 2778 | *matchp = match; |
| 2779 | |
| 2780 | return 1; |
| 2781 | } |
| 2782 | |
| 2783 | return 0; |
| 2784 | } |
| 2785 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2786 | int |
Neale Ranns | 1441a6c | 2020-12-14 16:02:17 +0000 | [diff] [blame] | 2787 | vnet_classify_add_del_session (vnet_classify_main_t *cm, u32 table_index, |
Benoît Ganne | bd9cde8 | 2022-12-01 15:58:36 +0100 | [diff] [blame] | 2788 | const u8 *match, u16 hit_next_index, |
Neale Ranns | 1441a6c | 2020-12-14 16:02:17 +0000 | [diff] [blame] | 2789 | u32 opaque_index, i32 advance, u8 action, |
Benoît Ganne | bd9cde8 | 2022-12-01 15:58:36 +0100 | [diff] [blame] | 2790 | u32 metadata, int is_add) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2791 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2792 | vnet_classify_table_t *t; |
| 2793 | vnet_classify_entry_5_t _max_e __attribute__ ((aligned (16))); |
| 2794 | vnet_classify_entry_t *e; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2795 | int i, rv; |
| 2796 | |
| 2797 | if (pool_is_free_index (cm->tables, table_index)) |
| 2798 | return VNET_API_ERROR_NO_SUCH_TABLE; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2799 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2800 | t = pool_elt_at_index (cm->tables, table_index); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2801 | |
| 2802 | e = (vnet_classify_entry_t *) & _max_e; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2803 | e->next_index = hit_next_index; |
| 2804 | e->opaque_index = opaque_index; |
| 2805 | e->advance = advance; |
| 2806 | e->hits = 0; |
| 2807 | e->last_heard = 0; |
| 2808 | e->flags = 0; |
Steve Shin | 25e26dc | 2016-11-08 10:47:10 -0800 | [diff] [blame] | 2809 | e->action = action; |
| 2810 | if (e->action == CLASSIFY_ACTION_SET_IP4_FIB_INDEX) |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 2811 | e->metadata = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2812 | metadata, |
| 2813 | FIB_SOURCE_CLASSIFY); |
Steve Shin | 25e26dc | 2016-11-08 10:47:10 -0800 | [diff] [blame] | 2814 | else if (e->action == CLASSIFY_ACTION_SET_IP6_FIB_INDEX) |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 2815 | e->metadata = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2816 | metadata, |
| 2817 | FIB_SOURCE_CLASSIFY); |
Dave Barach | 630a8e2 | 2017-11-18 06:58:34 -0500 | [diff] [blame] | 2818 | else if (e->action == CLASSIFY_ACTION_SET_METADATA) |
Gabriel Ganne | 8527f12 | 2017-10-02 11:41:24 +0200 | [diff] [blame] | 2819 | e->metadata = metadata; |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 2820 | else |
| 2821 | e->metadata = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2822 | |
| 2823 | /* Copy key data, honoring skip_n_vectors */ |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 2824 | clib_memcpy_fast (&e->key, match + t->skip_n_vectors * sizeof (u32x4), |
| 2825 | t->match_n_vectors * sizeof (u32x4)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2826 | |
| 2827 | /* Clear don't-care bits; likely when dynamically creating sessions */ |
| 2828 | for (i = 0; i < t->match_n_vectors; i++) |
| 2829 | e->key[i] &= t->mask[i]; |
| 2830 | |
| 2831 | rv = vnet_classify_add_del (t, e, is_add); |
Neale Ranns | 13eaf3e | 2017-05-23 06:10:33 -0700 | [diff] [blame] | 2832 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2833 | vnet_classify_entry_release_resource (e); |
Neale Ranns | 13eaf3e | 2017-05-23 06:10:33 -0700 | [diff] [blame] | 2834 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2835 | if (rv) |
| 2836 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
| 2837 | return 0; |
| 2838 | } |
| 2839 | |
| 2840 | static clib_error_t * |
| 2841 | classify_session_command_fn (vlib_main_t * vm, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2842 | unformat_input_t * input, |
| 2843 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2844 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2845 | vnet_classify_main_t *cm = &vnet_classify_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2846 | int is_add = 1; |
| 2847 | u32 table_index = ~0; |
| 2848 | u32 hit_next_index = ~0; |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2849 | u64 opaque_index = ~0; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2850 | u8 *match = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2851 | i32 advance = 0; |
Steve Shin | 25e26dc | 2016-11-08 10:47:10 -0800 | [diff] [blame] | 2852 | u32 action = 0; |
| 2853 | u32 metadata = 0; |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2854 | int i, rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2855 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2856 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2857 | { |
| 2858 | if (unformat (input, "del")) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2859 | is_add = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2860 | else if (unformat (input, "hit-next %U", unformat_ip_next_index, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2861 | &hit_next_index)) |
| 2862 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2863 | else |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2864 | if (unformat |
| 2865 | (input, "l2-input-hit-next %U", unformat_l2_input_next_index, |
| 2866 | &hit_next_index)) |
| 2867 | ; |
| 2868 | else |
| 2869 | if (unformat |
| 2870 | (input, "l2-output-hit-next %U", unformat_l2_output_next_index, |
| 2871 | &hit_next_index)) |
| 2872 | ; |
| 2873 | else if (unformat (input, "acl-hit-next %U", unformat_acl_next_index, |
| 2874 | &hit_next_index)) |
| 2875 | ; |
| 2876 | else if (unformat (input, "policer-hit-next %U", |
| 2877 | unformat_policer_next_index, &hit_next_index)) |
| 2878 | ; |
| 2879 | else if (unformat (input, "opaque-index %lld", &opaque_index)) |
| 2880 | ; |
| 2881 | else if (unformat (input, "match %U", unformat_classify_match, |
| 2882 | cm, &match, table_index)) |
| 2883 | ; |
| 2884 | else if (unformat (input, "advance %d", &advance)) |
| 2885 | ; |
| 2886 | else if (unformat (input, "table-index %d", &table_index)) |
| 2887 | ; |
| 2888 | else if (unformat (input, "action set-ip4-fib-id %d", &metadata)) |
| 2889 | action = 1; |
| 2890 | else if (unformat (input, "action set-ip6-fib-id %d", &metadata)) |
| 2891 | action = 2; |
| 2892 | else if (unformat (input, "action set-sr-policy-index %d", &metadata)) |
| 2893 | action = 3; |
| 2894 | else |
| 2895 | { |
| 2896 | /* Try registered opaque-index unformat fns */ |
| 2897 | for (i = 0; i < vec_len (cm->unformat_opaque_index_fns); i++) |
| 2898 | { |
| 2899 | if (unformat (input, "%U", cm->unformat_opaque_index_fns[i], |
| 2900 | &opaque_index)) |
| 2901 | goto found_opaque; |
| 2902 | } |
| 2903 | break; |
| 2904 | } |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2905 | found_opaque: |
| 2906 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2907 | } |
| 2908 | |
| 2909 | if (table_index == ~0) |
| 2910 | return clib_error_return (0, "Table index required"); |
| 2911 | |
| 2912 | if (is_add && match == 0) |
| 2913 | return clib_error_return (0, "Match value required"); |
| 2914 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2915 | rv = vnet_classify_add_del_session (cm, table_index, match, |
| 2916 | hit_next_index, |
| 2917 | opaque_index, advance, |
| 2918 | action, metadata, is_add); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2919 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2920 | switch (rv) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2921 | { |
| 2922 | case 0: |
| 2923 | break; |
| 2924 | |
| 2925 | default: |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2926 | return clib_error_return (0, |
| 2927 | "vnet_classify_add_del_session returned %d", |
| 2928 | rv); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2929 | } |
| 2930 | |
| 2931 | return 0; |
| 2932 | } |
| 2933 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2934 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2935 | VLIB_CLI_COMMAND (classify_session_command, static) = { |
| 2936 | .path = "classify session", |
Ole Troan | 1e66d5c | 2016-09-30 09:22:36 +0200 | [diff] [blame] | 2937 | .short_help = |
jackiechen1985 | e91e6de | 2018-12-14 01:43:21 +0800 | [diff] [blame] | 2938 | "classify session [hit-next|l2-input-hit-next|l2-output-hit-next|" |
Ole Troan | 1e66d5c | 2016-09-30 09:22:36 +0200 | [diff] [blame] | 2939 | "acl-hit-next <next_index>|policer-hit-next <policer_name>]" |
Steve Shin | 25e26dc | 2016-11-08 10:47:10 -0800 | [diff] [blame] | 2940 | "\n table-index <nn> match [hex] [l2] [l3 ip4] [opaque-index <index>]" |
Gabriel Ganne | 8527f12 | 2017-10-02 11:41:24 +0200 | [diff] [blame] | 2941 | "\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] | 2942 | .function = classify_session_command_fn, |
| 2943 | }; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2944 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2945 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2946 | static uword |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2947 | unformat_opaque_sw_if_index (unformat_input_t * input, va_list * args) |
| 2948 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2949 | u64 *opaquep = va_arg (*args, u64 *); |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2950 | u32 sw_if_index; |
| 2951 | |
| 2952 | if (unformat (input, "opaque-sw_if_index %U", unformat_vnet_sw_interface, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2953 | vnet_get_main (), &sw_if_index)) |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2954 | { |
| 2955 | *opaquep = sw_if_index; |
| 2956 | return 1; |
| 2957 | } |
| 2958 | return 0; |
| 2959 | } |
| 2960 | |
Ole Troan | 1e66d5c | 2016-09-30 09:22:36 +0200 | [diff] [blame] | 2961 | static uword |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2962 | unformat_ip_next_node (unformat_input_t * input, va_list * args) |
| 2963 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2964 | vnet_classify_main_t *cm = &vnet_classify_main; |
| 2965 | u32 *next_indexp = va_arg (*args, u32 *); |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2966 | u32 node_index; |
Ole Troan | 1e66d5c | 2016-09-30 09:22:36 +0200 | [diff] [blame] | 2967 | u32 next_index = ~0; |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2968 | |
Ole Troan | 1e66d5c | 2016-09-30 09:22:36 +0200 | [diff] [blame] | 2969 | if (unformat (input, "ip6-node %U", unformat_vlib_node, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2970 | cm->vlib_main, &node_index)) |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2971 | { |
Ole Troan | 1e66d5c | 2016-09-30 09:22:36 +0200 | [diff] [blame] | 2972 | next_index = vlib_node_add_next (cm->vlib_main, |
| 2973 | ip6_classify_node.index, node_index); |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2974 | } |
Ole Troan | 1e66d5c | 2016-09-30 09:22:36 +0200 | [diff] [blame] | 2975 | else if (unformat (input, "ip4-node %U", unformat_vlib_node, |
| 2976 | cm->vlib_main, &node_index)) |
| 2977 | { |
| 2978 | next_index = vlib_node_add_next (cm->vlib_main, |
| 2979 | ip4_classify_node.index, node_index); |
| 2980 | } |
| 2981 | else |
| 2982 | return 0; |
| 2983 | |
| 2984 | *next_indexp = next_index; |
| 2985 | return 1; |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2986 | } |
| 2987 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2988 | static uword |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2989 | unformat_acl_next_node (unformat_input_t * input, va_list * args) |
| 2990 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2991 | vnet_classify_main_t *cm = &vnet_classify_main; |
| 2992 | u32 *next_indexp = va_arg (*args, u32 *); |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2993 | u32 node_index; |
Ole Troan | 1e66d5c | 2016-09-30 09:22:36 +0200 | [diff] [blame] | 2994 | u32 next_index; |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2995 | |
Ole Troan | 1e66d5c | 2016-09-30 09:22:36 +0200 | [diff] [blame] | 2996 | if (unformat (input, "ip6-node %U", unformat_vlib_node, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 2997 | cm->vlib_main, &node_index)) |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 2998 | { |
Ole Troan | 1e66d5c | 2016-09-30 09:22:36 +0200 | [diff] [blame] | 2999 | next_index = vlib_node_add_next (cm->vlib_main, |
| 3000 | ip6_inacl_node.index, node_index); |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 3001 | } |
Ole Troan | 1e66d5c | 2016-09-30 09:22:36 +0200 | [diff] [blame] | 3002 | else if (unformat (input, "ip4-node %U", unformat_vlib_node, |
| 3003 | cm->vlib_main, &node_index)) |
| 3004 | { |
| 3005 | next_index = vlib_node_add_next (cm->vlib_main, |
| 3006 | ip4_inacl_node.index, node_index); |
| 3007 | } |
| 3008 | else |
| 3009 | return 0; |
| 3010 | |
| 3011 | *next_indexp = next_index; |
| 3012 | return 1; |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 3013 | } |
| 3014 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3015 | static uword |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 3016 | unformat_l2_input_next_node (unformat_input_t * input, va_list * args) |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 3017 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3018 | vnet_classify_main_t *cm = &vnet_classify_main; |
| 3019 | u32 *next_indexp = va_arg (*args, u32 *); |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 3020 | u32 node_index; |
| 3021 | u32 next_index; |
| 3022 | |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 3023 | if (unformat (input, "input-node %U", unformat_vlib_node, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3024 | cm->vlib_main, &node_index)) |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 3025 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3026 | next_index = vlib_node_add_next |
| 3027 | (cm->vlib_main, l2_input_classify_node.index, node_index); |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 3028 | |
| 3029 | *next_indexp = next_index; |
| 3030 | return 1; |
| 3031 | } |
| 3032 | return 0; |
| 3033 | } |
| 3034 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3035 | static uword |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 3036 | unformat_l2_output_next_node (unformat_input_t * input, va_list * args) |
| 3037 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3038 | vnet_classify_main_t *cm = &vnet_classify_main; |
| 3039 | u32 *next_indexp = va_arg (*args, u32 *); |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 3040 | u32 node_index; |
| 3041 | u32 next_index; |
| 3042 | |
| 3043 | if (unformat (input, "output-node %U", unformat_vlib_node, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3044 | cm->vlib_main, &node_index)) |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 3045 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3046 | next_index = vlib_node_add_next |
| 3047 | (cm->vlib_main, l2_output_classify_node.index, node_index); |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 3048 | |
| 3049 | *next_indexp = next_index; |
| 3050 | return 1; |
| 3051 | } |
| 3052 | return 0; |
| 3053 | } |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 3054 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3055 | static clib_error_t * |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 3056 | vnet_classify_init (vlib_main_t * vm) |
| 3057 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3058 | vnet_classify_main_t *cm = &vnet_classify_main; |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 3059 | |
| 3060 | cm->vlib_main = vm; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3061 | cm->vnet_main = vnet_get_main (); |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 3062 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3063 | vnet_classify_register_unformat_opaque_index_fn |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 3064 | (unformat_opaque_sw_if_index); |
| 3065 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3066 | vnet_classify_register_unformat_ip_next_index_fn (unformat_ip_next_node); |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 3067 | |
| 3068 | vnet_classify_register_unformat_l2_next_index_fn |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 3069 | (unformat_l2_input_next_node); |
| 3070 | |
| 3071 | vnet_classify_register_unformat_l2_next_index_fn |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 3072 | (unformat_l2_output_next_node); |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 3073 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3074 | vnet_classify_register_unformat_acl_next_index_fn (unformat_acl_next_node); |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 3075 | |
Jon Loeliger | 5c1e48c | 2020-10-15 14:41:36 -0400 | [diff] [blame] | 3076 | vlib_global_main.trace_filter.classify_table_index = ~0; |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 3077 | |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 3078 | return 0; |
| 3079 | } |
| 3080 | |
| 3081 | VLIB_INIT_FUNCTION (vnet_classify_init); |
| 3082 | |
Dave Barach | 87d24db | 2019-12-04 17:19:12 -0500 | [diff] [blame] | 3083 | int |
| 3084 | vnet_is_packet_traced (vlib_buffer_t * b, u32 classify_table_index, int func) |
| 3085 | { |
| 3086 | return vnet_is_packet_traced_inline (b, classify_table_index, func); |
| 3087 | } |
| 3088 | |
| 3089 | |
Dave Barach | 9137e54 | 2019-09-13 17:47:50 -0400 | [diff] [blame] | 3090 | #define TEST_CODE 0 |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3091 | |
| 3092 | #if TEST_CODE > 0 |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3093 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3094 | typedef struct |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3095 | { |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3096 | ip4_address_t addr; |
| 3097 | int in_table; |
| 3098 | } test_entry_t; |
| 3099 | |
| 3100 | typedef struct |
| 3101 | { |
| 3102 | test_entry_t *entries; |
| 3103 | |
| 3104 | /* test parameters */ |
| 3105 | u32 buckets; |
| 3106 | u32 sessions; |
| 3107 | u32 iterations; |
| 3108 | u32 memory_size; |
| 3109 | ip4_address_t src; |
| 3110 | vnet_classify_table_t *table; |
| 3111 | u32 table_index; |
| 3112 | int verbose; |
| 3113 | |
| 3114 | /* Random seed */ |
| 3115 | u32 seed; |
| 3116 | |
| 3117 | /* Test data */ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3118 | classify_data_or_mask_t *mask; |
| 3119 | classify_data_or_mask_t *data; |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3120 | |
| 3121 | /* convenience */ |
| 3122 | vnet_classify_main_t *classify_main; |
| 3123 | vlib_main_t *vlib_main; |
| 3124 | |
| 3125 | } test_classify_main_t; |
| 3126 | |
| 3127 | static test_classify_main_t test_classify_main; |
| 3128 | |
| 3129 | static clib_error_t * |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3130 | test_classify_churn (test_classify_main_t * tm) |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3131 | { |
| 3132 | classify_data_or_mask_t *mask, *data; |
| 3133 | vlib_main_t *vm = tm->vlib_main; |
| 3134 | test_entry_t *ep; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3135 | u8 *mp = 0, *dp = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3136 | u32 tmp; |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3137 | int i, rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3138 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3139 | vec_validate_aligned (mp, 3 * sizeof (u32x4), sizeof (u32x4)); |
| 3140 | vec_validate_aligned (dp, 3 * sizeof (u32x4), sizeof (u32x4)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3141 | |
| 3142 | mask = (classify_data_or_mask_t *) mp; |
| 3143 | data = (classify_data_or_mask_t *) dp; |
| 3144 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3145 | /* Mask on src address */ |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 3146 | clib_memset (&mask->ip.src_address, 0xff, 4); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3147 | |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3148 | tmp = clib_host_to_net_u32 (tm->src.as_u32); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3149 | |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3150 | for (i = 0; i < tm->sessions; i++) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3151 | { |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3152 | vec_add2 (tm->entries, ep, 1); |
| 3153 | ep->addr.as_u32 = clib_host_to_net_u32 (tmp); |
| 3154 | ep->in_table = 0; |
| 3155 | tmp++; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3156 | } |
| 3157 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3158 | tm->table = vnet_classify_new_table (tm->classify_main, |
| 3159 | (u8 *) mask, |
| 3160 | tm->buckets, |
| 3161 | tm->memory_size, 0 /* skip */ , |
| 3162 | 3 /* vectors to match */ ); |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3163 | tm->table->miss_next_index = IP_LOOKUP_NEXT_DROP; |
| 3164 | tm->table_index = tm->table - tm->classify_main->tables; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3165 | vlib_cli_output (vm, "Created table %d, buckets %d", |
| 3166 | tm->table_index, tm->buckets); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3167 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3168 | vlib_cli_output (vm, "Initialize: add %d (approx. half of %d sessions)...", |
| 3169 | tm->sessions / 2, tm->sessions); |
| 3170 | |
| 3171 | for (i = 0; i < tm->sessions / 2; i++) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3172 | { |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3173 | ep = vec_elt_at_index (tm->entries, i); |
| 3174 | |
| 3175 | data->ip.src_address.as_u32 = ep->addr.as_u32; |
| 3176 | ep->in_table = 1; |
| 3177 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3178 | rv = vnet_classify_add_del_session (tm->classify_main, |
| 3179 | tm->table_index, |
| 3180 | (u8 *) data, |
| 3181 | IP_LOOKUP_NEXT_DROP, |
| 3182 | i /* opaque_index */ , |
| 3183 | 0 /* advance */ , |
| 3184 | 0 /* action */ , |
| 3185 | 0 /* metadata */ , |
| 3186 | 1 /* is_add */ ); |
| 3187 | |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3188 | if (rv != 0) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3189 | clib_warning ("add: returned %d", rv); |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3190 | |
| 3191 | if (tm->verbose) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3192 | 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] | 3193 | } |
| 3194 | |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3195 | vlib_cli_output (vm, "Execute %d random add/delete operations", |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3196 | tm->iterations); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3197 | |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3198 | for (i = 0; i < tm->iterations; i++) |
| 3199 | { |
| 3200 | int index, is_add; |
| 3201 | |
| 3202 | /* Pick a random entry */ |
| 3203 | index = random_u32 (&tm->seed) % tm->sessions; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3204 | |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3205 | ep = vec_elt_at_index (tm->entries, index); |
| 3206 | |
| 3207 | data->ip.src_address.as_u32 = ep->addr.as_u32; |
| 3208 | |
| 3209 | /* If it's in the table, remove it. Else, add it */ |
| 3210 | is_add = !ep->in_table; |
| 3211 | |
| 3212 | if (tm->verbose) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3213 | vlib_cli_output (vm, "%s: %U", |
| 3214 | is_add ? "add" : "del", |
| 3215 | format_ip4_address, &ep->addr.as_u32); |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3216 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3217 | rv = vnet_classify_add_del_session (tm->classify_main, |
| 3218 | tm->table_index, |
| 3219 | (u8 *) data, |
| 3220 | IP_LOOKUP_NEXT_DROP, |
| 3221 | i /* opaque_index */ , |
| 3222 | 0 /* advance */ , |
| 3223 | 0 /* action */ , |
| 3224 | 0 /* metadata */ , |
| 3225 | is_add); |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3226 | if (rv != 0) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3227 | vlib_cli_output (vm, |
| 3228 | "%s[%d]: %U returned %d", is_add ? "add" : "del", |
| 3229 | index, format_ip4_address, &ep->addr.as_u32, rv); |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3230 | else |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3231 | ep->in_table = is_add; |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3232 | } |
| 3233 | |
| 3234 | vlib_cli_output (vm, "Remove remaining %d entries from the table", |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3235 | tm->table->active_elements); |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3236 | |
| 3237 | for (i = 0; i < tm->sessions; i++) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3238 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3239 | u8 *key_minus_skip; |
Benoît Ganne | b03eec9 | 2022-06-08 10:49:17 +0200 | [diff] [blame] | 3240 | u32 hash; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3241 | vnet_classify_entry_t *e; |
| 3242 | |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3243 | ep = tm->entries + i; |
| 3244 | if (ep->in_table == 0) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3245 | continue; |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3246 | |
| 3247 | data->ip.src_address.as_u32 = ep->addr.as_u32; |
| 3248 | |
| 3249 | hash = vnet_classify_hash_packet (tm->table, (u8 *) data); |
| 3250 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3251 | e = vnet_classify_find_entry (tm->table, |
| 3252 | (u8 *) data, hash, 0 /* time_now */ ); |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3253 | if (e == 0) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3254 | { |
| 3255 | clib_warning ("Couldn't find %U index %d which should be present", |
| 3256 | format_ip4_address, ep->addr, i); |
| 3257 | continue; |
| 3258 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3259 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3260 | key_minus_skip = (u8 *) e->key; |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3261 | key_minus_skip -= tm->table->skip_n_vectors * sizeof (u32x4); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3262 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3263 | rv = vnet_classify_add_del_session |
| 3264 | (tm->classify_main, |
| 3265 | tm->table_index, |
| 3266 | key_minus_skip, IP_LOOKUP_NEXT_DROP, i /* opaque_index */ , |
| 3267 | 0 /* advance */ , 0, 0, |
| 3268 | 0 /* is_add */ ); |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3269 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3270 | if (rv != 0) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3271 | clib_warning ("del: returned %d", rv); |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3272 | |
| 3273 | if (tm->verbose) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3274 | 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] | 3275 | } |
| 3276 | |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3277 | vlib_cli_output (vm, "%d entries remain, MUST be zero", |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3278 | tm->table->active_elements); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3279 | |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3280 | vlib_cli_output (vm, "Table after cleanup: \n%U\n", |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3281 | format_classify_table, tm->table, 0 /* verbose */ ); |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3282 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3283 | vec_free (mp); |
| 3284 | vec_free (dp); |
| 3285 | |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3286 | vnet_classify_delete_table_index (tm->classify_main, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3287 | tm->table_index, 1 /* del_chain */ ); |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3288 | tm->table = 0; |
| 3289 | tm->table_index = ~0; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3290 | vec_free (tm->entries); |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3291 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3292 | return 0; |
| 3293 | } |
| 3294 | |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3295 | static clib_error_t * |
| 3296 | test_classify_command_fn (vlib_main_t * vm, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3297 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3298 | { |
| 3299 | test_classify_main_t *tm = &test_classify_main; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3300 | vnet_classify_main_t *cm = &vnet_classify_main; |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3301 | u32 tmp; |
| 3302 | int which = 0; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3303 | clib_error_t *error = 0; |
| 3304 | |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3305 | tm->buckets = 1024; |
| 3306 | tm->sessions = 8192; |
| 3307 | tm->iterations = 8192; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3308 | tm->memory_size = 64 << 20; |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3309 | tm->src.as_u32 = clib_net_to_host_u32 (0x0100000A); |
| 3310 | tm->table = 0; |
| 3311 | tm->seed = 0xDEADDABE; |
| 3312 | tm->classify_main = cm; |
| 3313 | tm->vlib_main = vm; |
| 3314 | tm->verbose = 0; |
| 3315 | |
| 3316 | /* Default starting address 1.0.0.10 */ |
| 3317 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3318 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 3319 | { |
| 3320 | if (unformat (input, "sessions %d", &tmp)) |
| 3321 | tm->sessions = tmp; |
| 3322 | else |
| 3323 | if (unformat (input, "src %U", unformat_ip4_address, &tm->src.as_u32)) |
| 3324 | ; |
| 3325 | else if (unformat (input, "buckets %d", &tm->buckets)) |
| 3326 | ; |
| 3327 | else if (unformat (input, "memory-size %uM", &tmp)) |
| 3328 | tm->memory_size = tmp << 20; |
| 3329 | else if (unformat (input, "memory-size %uG", &tmp)) |
| 3330 | tm->memory_size = tmp << 30; |
| 3331 | else if (unformat (input, "seed %d", &tm->seed)) |
| 3332 | ; |
| 3333 | else if (unformat (input, "verbose")) |
| 3334 | tm->verbose = 1; |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3335 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3336 | else if (unformat (input, "iterations %d", &tm->iterations)) |
| 3337 | ; |
| 3338 | else if (unformat (input, "churn-test")) |
| 3339 | which = 0; |
| 3340 | else |
| 3341 | break; |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3342 | } |
| 3343 | |
| 3344 | switch (which) |
| 3345 | { |
| 3346 | case 0: |
| 3347 | error = test_classify_churn (tm); |
| 3348 | break; |
| 3349 | default: |
| 3350 | error = clib_error_return (0, "No such test"); |
| 3351 | break; |
| 3352 | } |
| 3353 | |
| 3354 | return error; |
| 3355 | } |
| 3356 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3357 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3358 | VLIB_CLI_COMMAND (test_classify_command, static) = { |
| 3359 | .path = "test classify", |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3360 | .short_help = |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 3361 | "test classify [src <ip>] [sessions <nn>] [buckets <nn>] [seed <nnn>]\n" |
| 3362 | " [memory-size <nn>[M|G]]\n" |
| 3363 | " [churn-test]", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3364 | .function = test_classify_command_fn, |
| 3365 | }; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3366 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3367 | #endif /* TEST_CODE */ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 3368 | |
| 3369 | /* |
| 3370 | * fd.io coding-style-patch-verification: ON |
| 3371 | * |
| 3372 | * Local Variables: |
| 3373 | * eval: (c-set-style "gnu") |
| 3374 | * End: |
| 3375 | */ |