Juraj Sloboda | 506b245 | 2016-08-07 23:45:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 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 | |
| 16 | #include <stdint.h> |
| 17 | |
| 18 | #include <vlib/vlib.h> |
| 19 | #include <vnet/vnet.h> |
| 20 | #include <vnet/ip/ip.h> |
| 21 | #include <vnet/classify/flow_classify.h> |
| 22 | #include <vnet/classify/vnet_classify.h> |
| 23 | |
| 24 | typedef struct { |
| 25 | u32 sw_if_index; |
| 26 | u32 next_index; |
| 27 | u32 table_index; |
| 28 | u32 offset; |
| 29 | } flow_classify_trace_t; |
| 30 | |
| 31 | static u8 * |
| 32 | format_flow_classify_trace (u8 * s, va_list * args) |
| 33 | { |
| 34 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 35 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 36 | flow_classify_trace_t * t = va_arg (*args, flow_classify_trace_t *); |
| 37 | |
| 38 | s = format (s, "FLOW_CLASSIFY: sw_if_index %d next %d table %d offset %d", |
| 39 | t->sw_if_index, t->next_index, t->table_index, t->offset); |
| 40 | return s; |
| 41 | } |
| 42 | |
| 43 | #define foreach_flow_classify_error \ |
| 44 | _(MISS, "Flow classify misses") \ |
| 45 | _(HIT, "Flow classify hits") \ |
| 46 | _(CHAIN_HIT, "Flow classify hits after chain walk") \ |
| 47 | _(DROP, "Flow classify action drop") |
| 48 | |
| 49 | typedef enum { |
| 50 | #define _(sym,str) FLOW_CLASSIFY_ERROR_##sym, |
| 51 | foreach_flow_classify_error |
| 52 | #undef _ |
| 53 | FLOW_CLASSIFY_N_ERROR, |
| 54 | } flow_classify_error_t; |
| 55 | |
| 56 | static char * flow_classify_error_strings[] = { |
| 57 | #define _(sym,string) string, |
| 58 | foreach_flow_classify_error |
| 59 | #undef _ |
| 60 | }; |
| 61 | |
| 62 | static inline uword |
| 63 | flow_classify_inline (vlib_main_t * vm, |
| 64 | vlib_node_runtime_t * node, |
| 65 | vlib_frame_t * frame, |
| 66 | flow_classify_table_id_t tid) |
| 67 | { |
| 68 | u32 n_left_from, * from, * to_next; |
| 69 | flow_classify_next_index_t next_index; |
| 70 | flow_classify_main_t * fcm = &flow_classify_main; |
| 71 | vnet_classify_main_t * vcm = fcm->vnet_classify_main; |
| 72 | f64 now = vlib_time_now (vm); |
| 73 | u32 hits = 0; |
| 74 | u32 misses = 0; |
| 75 | u32 chain_hits = 0; |
| 76 | u32 drop = 0; |
| 77 | |
| 78 | from = vlib_frame_vector_args (frame); |
| 79 | n_left_from = frame->n_vectors; |
| 80 | |
| 81 | /* First pass: compute hashes */ |
| 82 | while (n_left_from > 2) |
| 83 | { |
| 84 | vlib_buffer_t * b0, * b1; |
| 85 | u32 bi0, bi1; |
| 86 | u8 * h0, * h1; |
| 87 | u32 sw_if_index0, sw_if_index1; |
| 88 | u32 table_index0, table_index1; |
| 89 | vnet_classify_table_t * t0, * t1; |
| 90 | |
| 91 | /* Prefetch next iteration */ |
| 92 | { |
| 93 | vlib_buffer_t * p1, * p2; |
| 94 | |
| 95 | p1 = vlib_get_buffer (vm, from[1]); |
| 96 | p2 = vlib_get_buffer (vm, from[2]); |
| 97 | |
| 98 | vlib_prefetch_buffer_header (p1, STORE); |
| 99 | CLIB_PREFETCH (p1->data, CLIB_CACHE_LINE_BYTES, STORE); |
| 100 | vlib_prefetch_buffer_header (p2, STORE); |
| 101 | CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE); |
| 102 | } |
| 103 | |
| 104 | bi0 = from[0]; |
| 105 | b0 = vlib_get_buffer (vm, bi0); |
| 106 | h0 = b0->data; |
| 107 | |
| 108 | bi1 = from[1]; |
| 109 | b1 = vlib_get_buffer (vm, bi1); |
| 110 | h1 = b1->data; |
| 111 | |
| 112 | sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX]; |
| 113 | table_index0 = fcm->classify_table_index_by_sw_if_index[tid][sw_if_index0]; |
| 114 | |
| 115 | sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX]; |
| 116 | table_index1 = fcm->classify_table_index_by_sw_if_index[tid][sw_if_index1]; |
| 117 | |
| 118 | t0 = pool_elt_at_index (vcm->tables, table_index0); |
| 119 | |
| 120 | t1 = pool_elt_at_index (vcm->tables, table_index1); |
| 121 | |
| 122 | vnet_buffer(b0)->l2_classify.hash = |
| 123 | vnet_classify_hash_packet (t0, (u8 *) h0); |
| 124 | |
| 125 | vnet_classify_prefetch_bucket (t0, vnet_buffer(b0)->l2_classify.hash); |
| 126 | |
| 127 | vnet_buffer(b1)->l2_classify.hash = |
| 128 | vnet_classify_hash_packet (t1, (u8 *) h1); |
| 129 | |
| 130 | vnet_classify_prefetch_bucket (t1, vnet_buffer(b1)->l2_classify.hash); |
| 131 | |
| 132 | vnet_buffer(b0)->l2_classify.table_index = table_index0; |
| 133 | |
| 134 | vnet_buffer(b1)->l2_classify.table_index = table_index1; |
| 135 | |
| 136 | from += 2; |
| 137 | n_left_from -= 2; |
| 138 | } |
| 139 | |
| 140 | while (n_left_from > 0) |
| 141 | { |
| 142 | vlib_buffer_t * b0; |
| 143 | u32 bi0; |
| 144 | u8 * h0; |
| 145 | u32 sw_if_index0; |
| 146 | u32 table_index0; |
| 147 | vnet_classify_table_t * t0; |
| 148 | |
| 149 | bi0 = from[0]; |
| 150 | b0 = vlib_get_buffer (vm, bi0); |
| 151 | h0 = b0->data; |
| 152 | |
| 153 | sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX]; |
| 154 | table_index0 = fcm->classify_table_index_by_sw_if_index[tid][sw_if_index0]; |
| 155 | |
| 156 | t0 = pool_elt_at_index (vcm->tables, table_index0); |
| 157 | vnet_buffer(b0)->l2_classify.hash = |
| 158 | vnet_classify_hash_packet (t0, (u8 *) h0); |
| 159 | |
| 160 | vnet_buffer(b0)->l2_classify.table_index = table_index0; |
| 161 | vnet_classify_prefetch_bucket (t0, vnet_buffer(b0)->l2_classify.hash); |
| 162 | |
| 163 | from++; |
| 164 | n_left_from--; |
| 165 | } |
| 166 | |
| 167 | next_index = node->cached_next_index; |
| 168 | from = vlib_frame_vector_args (frame); |
| 169 | n_left_from = frame->n_vectors; |
| 170 | |
| 171 | while (n_left_from > 0) |
| 172 | { |
| 173 | u32 n_left_to_next; |
| 174 | |
| 175 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
| 176 | |
| 177 | /* Not enough load/store slots to dual loop... */ |
| 178 | while (n_left_from > 0 && n_left_to_next > 0) |
| 179 | { |
| 180 | u32 bi0; |
| 181 | vlib_buffer_t * b0; |
| 182 | u32 next0 = FLOW_CLASSIFY_NEXT_INDEX_DROP; |
| 183 | u32 table_index0; |
| 184 | vnet_classify_table_t * t0; |
| 185 | vnet_classify_entry_t * e0; |
| 186 | u64 hash0; |
| 187 | u8 * h0; |
| 188 | |
| 189 | /* Stride 3 seems to work best */ |
| 190 | if (PREDICT_TRUE (n_left_from > 3)) |
| 191 | { |
| 192 | vlib_buffer_t * p1 = vlib_get_buffer(vm, from[3]); |
| 193 | vnet_classify_table_t * tp1; |
| 194 | u32 table_index1; |
| 195 | u64 phash1; |
| 196 | |
| 197 | table_index1 = vnet_buffer(p1)->l2_classify.table_index; |
| 198 | |
| 199 | if (PREDICT_TRUE (table_index1 != ~0)) |
| 200 | { |
| 201 | tp1 = pool_elt_at_index (vcm->tables, table_index1); |
| 202 | phash1 = vnet_buffer(p1)->l2_classify.hash; |
| 203 | vnet_classify_prefetch_entry (tp1, phash1); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /* Speculatively enqueue b0 to the current next frame */ |
| 208 | bi0 = from[0]; |
| 209 | to_next[0] = bi0; |
| 210 | from += 1; |
| 211 | to_next += 1; |
| 212 | n_left_from -= 1; |
| 213 | n_left_to_next -= 1; |
| 214 | |
| 215 | b0 = vlib_get_buffer (vm, bi0); |
| 216 | h0 = b0->data; |
| 217 | table_index0 = vnet_buffer(b0)->l2_classify.table_index; |
| 218 | e0 = 0; |
| 219 | t0 = 0; |
| 220 | |
| 221 | vnet_get_config_data (fcm->vnet_config_main[tid], |
| 222 | &b0->current_config_index, |
| 223 | &next0, |
| 224 | /* # bytes of config data */ 0); |
| 225 | |
| 226 | if (PREDICT_TRUE(table_index0 != ~0)) |
| 227 | { |
| 228 | hash0 = vnet_buffer(b0)->l2_classify.hash; |
| 229 | t0 = pool_elt_at_index (vcm->tables, table_index0); |
| 230 | e0 = vnet_classify_find_entry (t0, (u8 *) h0, hash0, now); |
Juraj Sloboda | 506b245 | 2016-08-07 23:45:24 -0700 | [diff] [blame] | 231 | if (e0) |
| 232 | { |
| 233 | hits++; |
| 234 | } |
| 235 | else |
| 236 | { |
Juraj Sloboda | 7e9e6e1 | 2016-08-25 06:40:29 -0700 | [diff] [blame] | 237 | misses++; |
| 238 | vnet_classify_add_del_session (vcm, table_index0, |
| 239 | h0, ~0, 0, 0, 0, 0, 1); |
| 240 | /* increment counter */ |
| 241 | vnet_classify_find_entry (t0, (u8 *) h0, hash0, now); |
Juraj Sloboda | 506b245 | 2016-08-07 23:45:24 -0700 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE) |
| 245 | && (b0->flags & VLIB_BUFFER_IS_TRACED))) |
| 246 | { |
| 247 | flow_classify_trace_t * t = |
| 248 | vlib_add_trace (vm, node, b0, sizeof (*t)); |
| 249 | t->sw_if_index = vnet_buffer(b0)->sw_if_index[VLIB_RX]; |
| 250 | t->next_index = next0; |
| 251 | t->table_index = t0 ? t0 - vcm->tables : ~0; |
Dave Barach | 1946a12 | 2016-10-12 17:32:39 -0400 | [diff] [blame] | 252 | t->offset = (t0 && e0) ? vnet_classify_get_offset (t0, e0): ~0; |
Juraj Sloboda | 506b245 | 2016-08-07 23:45:24 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | /* Verify speculative enqueue, maybe switch current next frame */ |
| 256 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, |
| 257 | n_left_to_next, bi0, next0); |
| 258 | } |
| 259 | |
| 260 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 261 | } |
| 262 | |
| 263 | vlib_node_increment_counter (vm, node->node_index, |
| 264 | FLOW_CLASSIFY_ERROR_MISS, |
| 265 | misses); |
| 266 | vlib_node_increment_counter (vm, node->node_index, |
| 267 | FLOW_CLASSIFY_ERROR_HIT, |
| 268 | hits); |
| 269 | vlib_node_increment_counter (vm, node->node_index, |
| 270 | FLOW_CLASSIFY_ERROR_CHAIN_HIT, |
| 271 | chain_hits); |
| 272 | vlib_node_increment_counter (vm, node->node_index, |
| 273 | FLOW_CLASSIFY_ERROR_DROP, |
| 274 | drop); |
| 275 | |
| 276 | return frame->n_vectors; |
| 277 | } |
| 278 | |
| 279 | static uword |
| 280 | ip4_flow_classify (vlib_main_t * vm, |
| 281 | vlib_node_runtime_t * node, |
| 282 | vlib_frame_t * frame) |
| 283 | { |
| 284 | return flow_classify_inline(vm, node, frame, FLOW_CLASSIFY_TABLE_IP4); |
| 285 | } |
| 286 | |
| 287 | VLIB_REGISTER_NODE (ip4_flow_classify_node) = { |
| 288 | .function = ip4_flow_classify, |
| 289 | .name = "ip4-flow-classify", |
| 290 | .vector_size = sizeof (u32), |
| 291 | .format_trace = format_flow_classify_trace, |
| 292 | .n_errors = ARRAY_LEN(flow_classify_error_strings), |
| 293 | .error_strings = flow_classify_error_strings, |
| 294 | .n_next_nodes = FLOW_CLASSIFY_NEXT_INDEX_N_NEXT, |
| 295 | .next_nodes = { |
| 296 | [FLOW_CLASSIFY_NEXT_INDEX_DROP] = "error-drop", |
| 297 | }, |
| 298 | }; |
| 299 | |
| 300 | VLIB_NODE_FUNCTION_MULTIARCH (ip4_flow_classify_node, ip4_flow_classify); |
| 301 | |
| 302 | static uword |
| 303 | ip6_flow_classify (vlib_main_t * vm, |
| 304 | vlib_node_runtime_t * node, |
| 305 | vlib_frame_t * frame) |
| 306 | { |
| 307 | return flow_classify_inline(vm, node, frame, FLOW_CLASSIFY_TABLE_IP6); |
| 308 | } |
| 309 | |
| 310 | VLIB_REGISTER_NODE (ip6_flow_classify_node) = { |
| 311 | .function = ip6_flow_classify, |
| 312 | .name = "ip6-flow-classify", |
| 313 | .vector_size = sizeof (u32), |
| 314 | .format_trace = format_flow_classify_trace, |
| 315 | .n_errors = ARRAY_LEN(flow_classify_error_strings), |
| 316 | .error_strings = flow_classify_error_strings, |
| 317 | .n_next_nodes = FLOW_CLASSIFY_NEXT_INDEX_N_NEXT, |
| 318 | .next_nodes = { |
| 319 | [FLOW_CLASSIFY_NEXT_INDEX_DROP] = "error-drop", |
| 320 | }, |
| 321 | }; |
| 322 | |
| 323 | VLIB_NODE_FUNCTION_MULTIARCH (ip6_flow_classify_node, ip6_flow_classify); |
| 324 | |
| 325 | |
| 326 | static clib_error_t * |
| 327 | flow_classify_init (vlib_main_t *vm) |
| 328 | { |
| 329 | flow_classify_main_t * fcm = &flow_classify_main; |
| 330 | |
| 331 | fcm->vlib_main = vm; |
| 332 | fcm->vnet_main = vnet_get_main(); |
| 333 | fcm->vnet_classify_main = &vnet_classify_main; |
| 334 | |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | VLIB_INIT_FUNCTION (flow_classify_init); |