/* * Copyright (c) 2016 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include #include typedef struct { vnet_hash_fn_t hash_fn; uword *workers_bitmap; u32 *workers; } per_inteface_handoff_data_t; typedef struct { u32 cached_next_index; u32 num_workers; u32 first_worker_index; per_inteface_handoff_data_t *if_data; /* Worker handoff index */ u32 frame_queue_index; } handoff_main_t; extern handoff_main_t handoff_main; #ifndef CLIB_MARCH_VARIANT handoff_main_t handoff_main; #endif /* CLIB_MARCH_VARIANT */ typedef struct { u32 sw_if_index; u32 next_worker_index; u32 buffer_index; } worker_handoff_trace_t; #define foreach_worker_handoff_error \ _(CONGESTION_DROP, "congestion drop") typedef enum { #define _(sym,str) WORKER_HANDOFF_ERROR_##sym, foreach_worker_handoff_error #undef _ WORKER_HANDOFF_N_ERROR, } worker_handoff_error_t; static char *worker_handoff_error_strings[] = { #define _(sym,string) string, foreach_worker_handoff_error #undef _ }; /* packet trace format function */ static u8 * format_worker_handoff_trace (u8 * s, va_list * args) { CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); worker_handoff_trace_t *t = va_arg (*args, worker_handoff_trace_t *); s = format (s, "worker-handoff: sw_if_index %d, next_worker %d, buffer 0x%x", t->sw_if_index, t->next_worker_index, t->buffer_index); return s; } static void worker_handoff_trace_frame (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_buffer_t **bufs, u16 *threads, u32 n_vectors) { worker_handoff_trace_t *t; vlib_buffer_t **b; u16 *ti; b = bufs; ti = threads; while (n_vectors) { t = vlib_add_trace (vm, node, b[0], sizeof (*t)); t->sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_RX]; t->next_worker_index = ti[0]; t->buffer_index = vlib_get_buffer_index (vm, b[0]); b += 1; ti += 1; n_vectors -= 1; } } VLIB_NODE_FN (worker_handoff_node) (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) { handoff_main_t *hm = &handoff_main; vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b; u32 n_enq, n_left_from, *from; u16 thread_indices[VLIB_FRAME_SIZE], *ti; from = vlib_frame_vector_args (frame); n_left_from = frame->n_vectors; vlib_get_buffers (vm, from, bufs, n_left_from); b = bufs; ti = thread_indices; while (n_left_from > 0) { per_inteface_handoff_data_t *ihd0; u32 sw_if_index0, hash, index0; void *data; sw_if_index0 = vnet_buffer (b[0])->sw_if_index[VLIB_RX]; ihd0 = vec_elt_at_index (hm->if_data, sw_if_index0); /* Compute ingress LB hash */ data = vlib_buffer_get_current (b[0]); ihd0->hash_fn (&data, &hash, 1); /* if input node did not specify next index, then packet should go to ethernet-input */ if (PREDICT_TRUE (is_pow2 (vec_len (ihd0->workers)))) index0 = hash & (vec_len (ihd0->workers) - 1); else index0 = hash % vec_len (ihd0->workers); ti[0] = hm->first_worker_index + ihd0->workers[index0]; /* next */ n_left_from -= 1; ti += 1; b += 1; } if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE)) worker_handoff_trace_frame (vm, node, bufs, thread_indices, frame->n_vectors); n_enq = vlib_buffer_enqueue_to_thread (vm, node, hm->frame_queue_index, from, thread_indices, frame->n_vectors, 1); if (n_enq < frame->n_vectors) vlib_node_increment_counter (vm, node->node_index, WORKER_HANDOFF_ERROR_CONGESTION_DROP, frame->n_vectors - n_enq); return frame->n_vectors; } VLIB_REGISTER_NODE (worker_handoff_node) = { .name = "worker-handoff", .vector_size = sizeof (u32), .format_trace = format_worker_handoff_trace, .type = VLIB_NODE_TYPE_INTERNAL, .n_errors = ARRAY_LEN(worker_handoff_error_strings), .error_strings = worker_handoff_error_strings, .n_next_nodes = 1, .next_nodes = { [0] = "error-drop", }, }; #ifndef CLIB_MARCH_VARIANT int interface_handoff_enable_disable (vlib_main_t *vm, u32 sw_if_index, uword *bitmap, u8 is_sym, int is_l4, int enable_disable) { handoff_main_t *hm = &handoff_