/* * 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 #include typedef struct { 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; /* convenience variables */ vlib_main_t *vlib_main; vnet_main_t *vnet_main; u64 (*hash_fn) (ethernet_header_t *); } handoff_main_t; handoff_main_t handoff_main; vlib_node_registration_t handoff_dispatch_node; typedef struct { u32 sw_if_index; u32 next_worker_index; u32 buffer_index; } worker_handoff_trace_t; /* 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; } vlib_node_registration_t handoff_node; static uword worker_handoff_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) { handoff_main_t *hm = &handoff_main; vlib_thread_main_t *tm = vlib_get_thread_main (); u32 n_left_from, *from; static __thread vlib_frame_queue_elt_t **handoff_queue_elt_by_worker_index; static __thread vlib_frame_queue_t **congested_handoff_queue_by_worker_index = 0; vlib_frame_queue_elt_t *hf = 0; int i; u32 n_left_to_next_worker = 0, *to_next_worker = 0; u32 next_worker_index = 0; u32 current_worker_index = ~0; if (PREDICT_FALSE (handoff_queue_elt_by_worker_index == 0)) { vec_validate (handoff_queue_elt_by_worker_index, tm->n_vlib_mains - 1); vec_validate_init_empty (congested_handoff_queue_by_worker_index, hm->first_worker_index + hm->num_workers - 1, (vlib_frame_queue_t *) (~0)); } from = vlib_frame_vector_args (frame); n_left_from = frame->n_vectors; while (n_left_from > 0) { u32 bi0; vlib_buffer_t *b0; u32 sw_if_index0; u32 hash; u64 hash_key; per_inteface_handoff_data_t *ihd0; u32 index0; bi0 = from[0]; from += 1; n_left_from -= 1; b0 = vlib_get_buffer (vm, bi0); sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX]; ASSERT (hm->if_data); ihd0 = vec_elt_at_index (hm->if_data, sw_if_index0); next_worker_index = hm->first_worker_index; /* * Force unknown traffic onto worker 0, * and into ethernet-input. $$$$ add more hashes. */ /* Compute ingress LB hash */ hash_key = hm->hash_fn ((ethernet_header_t *) b0->data); hash = (u32) clib_xxhash (hash_key); /* if input node did not specify next index, then packet should go to eternet-input */ if (PREDICT_FALSE ((b0->flags & VNET_BUFFER_F_HANDOFF_NEXT_VALID) == 0)) vnet_buffer (b0)->handoff.next_index = HANDOFF_DISPATCH_NEXT_ETHERNET_INPUT; else if (vnet_buffer (b0)->handoff.next_index == HANDOFF_DISPATCH_NEXT_IP4_INPUT || vnet_buffer (b0)->handoff.next_index == HANDOFF_DISPATCH_NEXT_IP6_INPUT || vnet_buffer (b0)->handoff.next_index == HANDOFF_DISPATCH_NEXT_MPLS_INPUT) vlib_buffer_advance (b0, (sizeof (ethernet_header_t))); if (PREDICT_TRUE (is_pow2 (vec_len (ihd0->workers)))) index0 = hash & (vec_len (ihd0->workers) - 1); else index0 = hash % vec_len (ihd0->workers); next_worker_index += ihd0->workers[index0]; if (next_worker_index != current_worker_index) { if (hf) hf->n_vectors = VLIB_FRAME_SIZE - n_left_to_next_worker; hf = vlib_get_worker_handoff_queue_elt (hm->frame_queue_index, next_worker_index, handoff_queue_elt_by_worker_index); n_left_to_next_worker = VLIB_FRAME_SIZE - hf->n_vectors; to_next_worker = &hf->buffer_index[hf->n_vectors]; current_worker_index = next_worker_index; } /* enqueue to correct worker thread */ to_next_worker[0] = bi0; to_next_worker++; n_left_to_next_worker--; if (n_left_to_next_worker == 0) { hf->n_vectors = VLIB_FRAME_SIZE; vlib_put_frame_queue_elt (hf); current_worker_index = ~0; handoff_queue_elt_by_worker_index[next_worker_index] = 0; hf = 0; } if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) && (b0->flags & VLIB_BUFFER_IS_TRACED))) { worker_handoff_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t)); t->sw_if_index = sw_if_index0; t->next_worker_index = next_worker_index - hm->first_worker_index; t->buffer_index = bi0; } } if (hf) hf->n_vectors = VLIB_FRAME_SIZE - n_left_to_next_worker; /* Ship frames to the worker nodes */ for (i = 0; i < vec_len (handoff_queue_elt_by_worker_index); i++) { if (handoff_queue_elt_by_worker_index[i]) { hf = handoff_queue_elt_by_worker_index[i]; /* * It works better to let the handoff node * rate-adapt, always ship the handoff queue element. */ if (1 || hf->n_vectors == hf->last_n_vectors) { vlib_put_frame_queue_elt (hf); handoff_queue_elt_by_worker_index[i] = 0; } else hf->last_n_vectors = hf->n_vectors; } congested_handoff_queue_by_worker_index[i] = (vlib_frame_queue_t *) (~0); } hf = 0; current_worker_index = ~0; return frame->n_vectors; } /* *INDENT-OFF* */ VLIB_REGISTER_NODE (worker_handoff_node) = { .function = worker_handoff_node_fn, .name = "worker-handoff", .vector_size = sizeof (u32), .format_trace = format_worker_handoff_trace, .type = VLIB_NODE_TYPE_INTERNAL, .n_next_nodes = 1, .next_nodes = { [0] = "error-drop", }, }; VLIB_NODE_FUNCTION_MULTIARCH (worker_handoff_node, worker_handoff_node_fn) /* *INDE
/* Hey Emacs use -*- mode: C -*- */
/*
 * 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 agre