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