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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include <vnet/l2/l2_classify.h>
17 #include <vlib/vlib.h>
18 #include <vnet/vnet.h>
19 #include <vnet/pg/pg.h>
20 #include <vnet/ip/ip.h>
21 #include <vnet/ip/ip_packet.h>
22 #include <vnet/ip/ip4_packet.h>
23 #include <vnet/ip/ip6_packet.h>
24 #include <vppinfra/error.h>
30 /* Not strictly needed, for show command */
32 } sticky_hash_session_t;
36 u32 cached_next_index;
38 /* next index added to l2_classify */
39 u32 fwd_miss_next_index;
42 sticky_hash_session_t *sessions;
44 /* Forward and reverse data session setup buffers */
45 u8 fdata[3 * sizeof (u32x4)];
46 u8 rdata[3 * sizeof (u32x4)];
48 /* convenience variables */
49 vlib_main_t *vlib_main;
50 vnet_main_t *vnet_main;
51 vnet_classify_main_t *vnet_classify_main;
52 l2_input_classify_main_t *l2_input_classify_main;
58 /* $$$$ fill in with per-pkt trace data */
61 } sticky_hash_miss_trace_t;
63 /* packet trace format function */
65 format_sticky_hash_miss_trace (u8 * s, va_list * args)
67 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
68 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
69 sticky_hash_miss_trace_t *t = va_arg (*args, sticky_hash_miss_trace_t *);
71 s = format (s, "STICKY_HASH_MISS: sw_if_index %d", t->sw_if_index);
75 typedef CLIB_PACKED (struct
77 ethernet_header_t eh; ip4_header_t ip;
78 }) classify_data_or_mask_t;
80 sticky_hash_main_t sticky_hash_main;
82 vlib_node_registration_t sticky_hash_miss_node;
84 #define foreach_sticky_hash_miss_error \
85 _(MISSES, "forward flow classify misses")
89 #define _(sym,str) STICKY_HASH_MISS_ERROR_##sym,
90 foreach_sticky_hash_miss_error
92 STICKY_HASH_MISS_N_ERROR,
93 } sticky_hash_miss_error_t;
95 static char *sticky_hash_miss_error_strings[] = {
96 #define _(sym,string) string,
97 foreach_sticky_hash_miss_error
102 * To drop a pkt and increment one of the previous counters:
104 * set b0->error = error_node->errors[STICKY_HASH_MISS_ERROR_EXAMPLE];
105 * set next0 to a disposition index bound to "error-drop".
107 * To manually increment the specific counter STICKY_HASH_MISS_ERROR_EXAMPLE:
109 * vlib_node_t *n = vlib_get_node (vm, sticky_hash_miss.index);
110 * u32 node_counter_base_index = n->error_heap_index;
111 * vlib_error_main_t * em = &vm->error_main;
112 * em->counters[node_counter_base_index + STICKY_HASH_MISS_ERROR_EXAMPLE] += 1;
118 STICKY_HASH_MISS_NEXT_IP4_INPUT,
119 STICKY_HASH_MISS_N_NEXT,
120 } sticky_hash_miss_next_t;
123 sticky_hash_miss_node_fn (vlib_main_t * vm,
124 vlib_node_runtime_t * node, vlib_frame_t * frame)
126 u32 n_left_from, *from, *to_next;
127 sticky_hash_miss_next_t next_index;
128 sticky_hash_main_t *mp = &sticky_hash_main;
129 vlib_node_t *n = vlib_get_node (vm, sticky_hash_miss_node.index);
130 u32 node_counter_base_index = n->error_heap_index;
131 vlib_error_main_t *em = &vm->error_main;
132 vnet_classify_main_t *cm = mp->vnet_classify_main;
133 ip4_main_t *im = &ip4_main;
135 from = vlib_frame_vector_args (frame);
136 n_left_from = frame->n_vectors;
137 next_index = node->cached_next_index;
139 while (n_left_from > 0)
143 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
145 while (n_left_from > 0 && n_left_to_next > 0)
151 u32 fib_index0, ft_index0, rt_index0;
152 vnet_classify_table_3_t *ft0, *rt0;
153 vnet_classify_entry_3_t *fe0, *re0;
154 classify_data_or_mask_t *h0;
157 sticky_hash_session_t *s;
160 /* speculatively enqueue b0 to the current next frame */
168 b0 = vlib_get_buffer (vm, bi0);
170 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
171 next0 = mp->cached_next_index;
173 h0 = vlib_buffer_get_current (b0);
175 /* Add forward and reverse entries for this flow */
176 clib_memcpy (mp->fdata, h0, sizeof (mp->fdata));
177 clib_memcpy (mp->rdata, h0, sizeof (mp->rdata));
179 h0 = (classify_data_or_mask_t *) (mp->rdata);
181 /* swap src + dst addresses to form reverse data */
182 tmp = h0->ip.src_address.as_u32;
183 h0->ip.src_address.as_u32 = h0->ip.dst_address.as_u32;
184 h0->ip.dst_address.as_u32 = tmp;
186 /* dig up fwd + rev tables */
187 fib_index0 = vec_elt (im->fib_index_by_sw_if_index, sw_if_index0);
188 fib0 = vec_elt_at_index (im->fibs, fib_index0);
190 ft_index0 = fib0->fwd_classify_table_index;
191 rt_index0 = fib0->rev_classify_table_index;
193 ft0 = (vnet_classify_table_3_t *)
194 pool_elt_at_index (cm->tables, ft_index0);
195 rt0 = (vnet_classify_table_3_t *)
196 pool_elt_at_index (cm->tables, rt_index0);
199 vnet_classify_find_or_add_entry_3 (ft0, mp->fdata, &was_found0);
200 fe0->next_index = L2_INPUT_CLASSIFY_NEXT_IP4_INPUT;
201 fe0->advance = sizeof (ethernet_header_t);
203 re0 = vnet_classify_find_or_add_entry_3 (rt0, mp->rdata, 0);
204 re0->next_index = L2_INPUT_CLASSIFY_NEXT_IP4_INPUT; /* $$$ FIXME */
205 re0->advance = sizeof (ethernet_header_t);
207 /* Note: we could get a whole vector of misses for the same sess */
210 pool_get (mp->sessions, s);
212 fe0->opaque_index = s - mp->sessions;
213 re0->opaque_index = s - mp->sessions;
215 s->fwd_entry_index = fe0 - ft0->entries;
216 s->rev_entry_index = re0 - rt0->entries;
217 s->fib_index = fib_index0;
220 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
221 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
223 sticky_hash_miss_trace_t *t =
224 vlib_add_trace (vm, node, b0, sizeof (*t));
225 t->sw_if_index = sw_if_index0;
226 t->next_index = next0;
229 em->counters[node_counter_base_index +
230 STICKY_HASH_MISS_ERROR_MISSES] += 1;
232 vlib_buffer_advance (b0, sizeof (ethernet_header_t));
234 /* verify speculative enqueue, maybe switch current next frame */
235 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
236 to_next, n_left_to_next,
240 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
243 return frame->n_vectors;
247 VLIB_REGISTER_NODE (sticky_hash_miss_node) = {
248 .function = sticky_hash_miss_node_fn,
249 .name = "sticky-hash-miss",
250 .vector_size = sizeof (u32),
251 .format_trace = format_sticky_hash_miss_trace,
252 .type = VLIB_NODE_TYPE_INTERNAL,
254 .n_errors = ARRAY_LEN(sticky_hash_miss_error_strings),
255 .error_strings = sticky_hash_miss_error_strings,
257 .n_next_nodes = STICKY_HASH_MISS_N_NEXT,
259 /* edit / add dispositions here */
261 [STICKY_HASH_MISS_NEXT_IP4_INPUT] = "ip4-input",
267 sticky_hash_miss_init (vlib_main_t * vm)
269 sticky_hash_main_t *mp = &sticky_hash_main;
272 mp->vnet_main = vnet_get_main ();
273 mp->vnet_classify_main = &vnet_classify_main;
274 mp->l2_input_classify_main = &l2_input_classify_main;
279 VLIB_INIT_FUNCTION (sticky_hash_miss_init);
281 static int ip4_sticky_hash_enable_disable
282 (sticky_hash_main_t * mp,
283 u32 fwd_sw_if_index, u8 * fwd_mask,
284 u32 rev_sw_if_index, u8 * rev_mask, u32 nbuckets, int enable_disable)
286 ip4_main_t *im = &ip4_main;
289 vnet_classify_main_t *cm = mp->vnet_classify_main;
290 l2_input_classify_main_t *l2cm = mp->l2_input_classify_main;
291 vnet_classify_table_3_t *ft, *rt;
293 fib_index = vec_elt (im->fib_index_by_sw_if_index, fwd_sw_if_index);
294 fib = vec_elt_at_index (im->fibs, fib_index);
296 if (fib->fwd_classify_table_index == ~0)
298 /* Set up forward table */
299 ft = (vnet_classify_table_3_t *)
300 vnet_classify_new_table (cm, fwd_mask, nbuckets,
301 0 /* skip */ , 3 /* match */ );
302 fib->fwd_classify_table_index
303 = ft - (vnet_classify_table_3_t *) cm->tables;
304 mp->fwd_miss_next_index =
305 vlib_node_add_next (mp->vlib_main, l2_input_classify_node.index,
306 sticky_hash_miss_node.index);
307 ft->miss_next_index = mp->fwd_miss_next_index;
309 /* Set up reverse table */
310 rt = (vnet_classify_table_3_t *)
311 vnet_classify_new_table (cm, rev_mask, nbuckets,
312 0 /* skip */ , 3 /* match */ );
313 fib->rev_classify_table_index
314 = rt - (vnet_classify_table_3_t *) cm->tables;
318 (l2cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_IP4],
322 (l2cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_IP6],
326 (l2cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_OTHER],
329 l2cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_IP4]
330 [fwd_sw_if_index] = fib->fwd_classify_table_index;
332 l2cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_IP6]
333 [fwd_sw_if_index] = ~0;
335 l2cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_OTHER]
336 [fwd_sw_if_index] = ~0;
340 (l2cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_IP4],
344 (l2cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_IP6],
348 (l2cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_OTHER],
352 l2cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_IP4]
353 [rev_sw_if_index] = fib->rev_classify_table_index;
355 l2cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_IP6]
356 [rev_sw_if_index] = ~0;
358 l2cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_OTHER]
359 [rev_sw_if_index] = ~0;
361 vnet_l2_input_classify_enable_disable (fwd_sw_if_index, enable_disable);
362 vnet_l2_input_classify_enable_disable (rev_sw_if_index, enable_disable);
366 static clib_error_t *
367 ip4_sticky_hash_init_command_fn (vlib_main_t * vm,
368 unformat_input_t * input,
369 vlib_cli_command_t * cmd)
371 u32 fwd_sw_if_index = ~0, rev_sw_if_index = ~0;
372 int enable_disable = 1;
375 sticky_hash_main_t *mp = &sticky_hash_main;
376 classify_data_or_mask_t fwd_mask, rev_mask;
379 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
382 (input, "fwd %U", unformat_vnet_sw_interface, mp->vnet_main,
386 (input, "rev %U", unformat_vnet_sw_interface, mp->vnet_main,
389 else if (unformat (input, "nbuckets %d", &nbuckets))
391 else if (unformat (input, "disable"))
398 nbuckets = 1 << max_log2 (nbuckets);
400 if (fwd_sw_if_index == ~0)
401 return clib_error_return (0, "fwd interface not set");
403 if (rev_sw_if_index == ~0)
404 return clib_error_return (0, "rev interface not set");
406 if (!is_pow2 (nbuckets))
407 return clib_error_return (0, "nbuckets %d not a power of 2", nbuckets);
409 ASSERT (sizeof (fwd_mask) <= 3 * sizeof (u32x4));
411 /* Mask on src/dst address, depending on direction */
412 memset (&fwd_mask, 0, sizeof (fwd_mask));
413 memset (&fwd_mask.ip.src_address, 0xff, 4);
415 memset (&rev_mask, 0, sizeof (rev_mask));
416 memset (&rev_mask.ip.dst_address, 0xff, 4);
418 vec_validate (fm, 3 * sizeof (u32x4) - 1);
419 vec_validate (rm, 3 * sizeof (u32x4) - 1);
421 clib_memcpy (fm, &fwd_mask, sizeof (fwd_mask));
422 clib_memcpy (rm, &rev_mask, sizeof (rev_mask));
424 rv = ip4_sticky_hash_enable_disable (mp, fwd_sw_if_index, fm,
426 nbuckets, enable_disable);
436 return clib_error_return (0,
437 "ip4_sticky_hash_enable_disable returned %d",
445 VLIB_CLI_COMMAND (sticky_hash_init_command, static) = {
446 .path = "ip sticky classify",
447 .short_help = "ip sticky classify fwd <intfc> rev <intfc> "
448 "[nbuckets <nn>][disable]",
449 .function = ip4_sticky_hash_init_command_fn,
455 format_sticky_hash_session (u8 * s, va_list * args)
457 sticky_hash_main_t *mp = va_arg (*args, sticky_hash_main_t *);
458 sticky_hash_session_t *session = va_arg (*args, sticky_hash_session_t *);
459 vnet_classify_table_3_t *t;
460 vnet_classify_entry_3_t *e;
461 ip4_main_t *im = &ip4_main;
462 vnet_classify_main_t *cm = mp->vnet_classify_main;
464 classify_data_or_mask_t *match;
466 fib = vec_elt_at_index (im->fibs, session->fib_index);
468 t = (vnet_classify_table_3_t *)
469 pool_elt_at_index (cm->tables, fib->fwd_classify_table_index);
470 e = pool_elt_at_index (t->entries, session->fwd_entry_index);
471 match = (classify_data_or_mask_t *) (e->key);
475 "[%6d] fwd src %U next index %d session %d fib %d\n"
476 " hits %lld last-heard %.6f\n",
478 format_ip4_address, &match->ip.src_address,
479 e->next_index, e->opaque_index, fib->table_id, e->hits, e->last_heard);
481 if (e->opaque_index != session - mp->sessions)
482 s = format (s, "WARNING: forward session index mismatch!\n");
484 t = (vnet_classify_table_3_t *)
485 pool_elt_at_index (cm->tables, fib->rev_classify_table_index);
486 e = pool_elt_at_index (t->entries, session->rev_entry_index);
487 match = (classify_data_or_mask_t *) (e->key);
491 "[%6d] rev dst %U next index %d session %d\n"
492 " hits %lld last-heard %.6f\n",
494 format_ip4_address, &match->ip.dst_address,
495 e->next_index, e->opaque_index, e->hits, e->last_heard);
497 if (e->opaque_index != session - mp->sessions)
498 s = format (s, "WARNING: reverse session index mismatch!\n");
499 s = format (s, "---------\n");
504 static clib_error_t *
505 show_ip4_sticky_hash_command_fn (vlib_main_t * vm,
506 unformat_input_t * input,
507 vlib_cli_command_t * cmd)
509 sticky_hash_main_t *mp = &sticky_hash_main;
510 sticky_hash_session_t *s;
512 int dump_classifier_tables = 0;
514 ip4_main_t *im4 = &ip4_main;
515 vnet_classify_main_t *cm = mp->vnet_classify_main;
517 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
519 if (unformat (input, "verbose"))
521 else if (unformat (input, "dump-tables")
522 || unformat (input, "dump-classifier-tables"))
523 dump_classifier_tables = 1;
528 if (pool_elts (mp->sessions) == 0)
529 vlib_cli_output (vm, "No ip sticky hash sessions");
532 vlib_cli_output (vm, "%d active sessions\n", pool_elts (mp->sessions));
534 vec_foreach (fib, im4->fibs)
536 if (fib->fwd_classify_table_index != ~0)
537 vlib_cli_output (vm, "fib %d fwd table: \n%U",
539 format_classify_table,
542 (cm->tables, fib->fwd_classify_table_index),
543 dump_classifier_tables);
544 if (fib->rev_classify_table_index != ~0)
545 vlib_cli_output (vm, "fib %d rev table: \n%U",
547 format_classify_table,
550 (cm->tables, fib->rev_classify_table_index),
551 dump_classifier_tables);
557 pool_foreach (s, mp->sessions,
559 vlib_cli_output (vm, "%U", format_sticky_hash_session, mp, s);
567 VLIB_CLI_COMMAND (show_sticky_hash_command, static) = {
568 .path = "show sticky classify",
569 .short_help = "Display sticky classifier tables",
570 .function = show_ip4_sticky_hash_command_fn,
576 * fd.io coding-style-patch-verification: ON
579 * eval: (c-set-style "gnu")