/* *------------------------------------------------------------------ * 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. *------------------------------------------------------------------ */ #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #define foreach_memif_input_error \ _ (BUFFER_ALLOC_FAIL, buffer_alloc, ERROR, "buffer allocation failed") \ _ (BAD_DESC, bad_desc, ERROR, "bad descriptor") \ _ (NOT_IP, not_ip, INFO, "not ip packet") typedef enum { #define _(f, n, s, d) MEMIF_INPUT_ERROR_##f, foreach_memif_input_error #undef _ MEMIF_INPUT_N_ERROR, } memif_input_error_t; static vlib_error_desc_t memif_input_error_counters[] = { #define _(f, n, s, d) { #n, d, VL_COUNTER_SEVERITY_##s }, foreach_memif_input_error #undef _ }; typedef struct { u32 next_index; u32 hw_if_index; u16 ring; } memif_input_trace_t; static __clib_unused u8 * format_memif_input_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 *); memif_input_trace_t *t = va_arg (*args, memif_input_trace_t *); u32 indent = format_get_indent (s); s = format (s, "memif: hw_if_index %d next-index %d", t->hw_if_index, t->next_index); s = format (s, "\n%Uslot: ring %u", format_white_space, indent + 2, t->ring); return s; } static_always_inline u32 memif_next_from_ip_hdr (vlib_node_runtime_t * node, vlib_buffer_t * b) { u8 *ptr = vlib_buffer_get_current (b); u8 v = *ptr & 0xf0; if (PREDICT_TRUE (v == 0x40)) return VNET_DEVICE_INPUT_NEXT_IP4_NCS_INPUT; else if (PREDICT_TRUE (v == 0x60)) return VNET_DEVICE_INPUT_NEXT_IP6_INPUT; b->error = node->errors[MEMIF_INPUT_ERROR_NOT_IP]; return VNET_DEVICE_INPUT_NEXT_DROP; } static_always_inline void memif_trace_buffer (vlib_main_t * vm, vlib_node_runtime_t * node, memif_if_t * mif, vlib_buffer_t * b, u32 next, u16 qid, uword * n_tracep) { if (PREDICT_TRUE (b != 0 && vlib_trace_buffer (vm, node, next, b, /* follow_chain */ 0))) { memif_input_trace_t *tr; vlib_set_trace_count (vm, node, --(*n_tracep)); tr = vlib_add_trace (vm, node, b, sizeof (*tr)); tr->next_index = next; tr->hw_if_index = mif->hw_if_index; tr->ring = qid; } } static_always_inline void memif_add_copy_op (memif_per_thread_data_t * ptd, void *data, u32 len, u16 buffer_offset, u16 buffer_vec_index) { memif_copy_op_t *co; vec_add2_aligned (ptd->copy_ops, co, 1, CLIB_CACHE_LINE_BYTES); co->data = data; co->data_len = len; co->buffer_offset = buffer_offset; co->buffer_vec_index = buffer_vec_index; } static_always_inline void memif_add_to_chain (vlib_main_t * vm, vlib_buffer_t * b, u32 * buffers, u32 buffer_size) { vlib_buffer_t *seg = b; i32 bytes_left = b->current_length - buffer_size + b->current_data; if (PREDICT_TRUE (bytes_left <= 0)) return; b->current_length -= bytes_left; b->total_length_not_including_first_buffer = bytes_left; while (bytes_left) { seg->flags |= VLIB_BUFFER_NEXT_PRESENT; seg->next_buffer = buffers[0]; seg = vlib_get_buffer (vm, buffers[0]); buffers++; seg->current_data = 0; seg->current_length = clib_min (buffer_size, bytes_left); bytes_left -= seg->current_length; } } static_always_inline uword memif_device_input_inline (vlib_main_t *vm, vlib_node_runtime_t *node, memif_if_t *mif, memif_ring_type_t type, u16 qid, memif_interface_mode_t mode) { vnet_main_t *vnm = vnet_get_main (); memif_main_t *mm = &memif_main; memif_ring_t *ring; memif_queue_t *mq; u16 buffer_size = vlib_buffer_get_default_data_size (vm); uword n_trace; u16 nexts[MEMIF_RX_VECTOR_SZ], *next = nexts; u32 _to_next_bufs[MEMIF_RX_VECTOR_SZ], *to_next_bufs = _to_next_bufs, *bi; u32 n_rx_packets = 0, n_rx_bytes = 0; u32 n_left, n_left_to_next; u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT; vlib_buffer_t *b0, *b1, *b2, *b3; u32 thread_index = vm->thread_index; memif_per_thread_data_t *ptd = vec_elt_at_index (mm->per_thread_data, thread_index); vlib_buffer_t bt; u16 cur_slot, last_slot, ring_size, n_slots, mask; i16 start_offset; u16 n_buffers = 0, n_alloc; memif_copy_op_t *co; memif_packet_op_t *po; memif_region_index_t last_region = ~0; void *last_region_shm = 0; void *last_region_max = 0; mq = vec_elt_at_index (mif->rx_queues, qid); ring = mq->ring; ring_size = 1 << mq->log2_ring_size; mask = ring_size - 1; /* assume that somebody will want to add ethernet header on the packet so start with IP header at offset 14 */ start_offset = (mode == MEMIF_INTERFACE_MODE_IP) ? 14 : 0; /* for S2M rings, we are consumers of packet buffers, and for M2S rings we are producers of empty buffers */ cur_slot = (type == MEMIF_RING_S2M) ? mq->last_head : mq->last_tail; if (type == MEMIF_RING_S2M) last_slot = __atomic_load_n (&ring->head, __ATOMIC_ACQUIRE); else last_slot = __atomic_load_n (&ring->tail, __ATOMIC_ACQUIRE); if (cur_slot == last_slot) goto refill; n_slots = last_slot - cur_slot; /* constru
/*
 * Copyright (c) 2018 Cisco and/or its affiliates.
 * Copyright (c) 2018 Arm Limited. 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.
 */

#ifndef included_clib_atomics_h
#define included_clib_atomics_h

/* Legacy __sync builtins */

/* Full Barrier */
#define clib_atomic_fetch_add(a, b) __sync_fetch_and_add(a, b)
#define clib_atomic_fetch_sub(a, b) __sync_fetch_and_sub(a, b)
#define clib_atomic_fetch_and(a, b) __sync_fetch_and_and(a, b)
#define clib_atomic_fetch_xor(a, b) __sync_fetch_and_xor(a, b)
#define clib_atomic_fetch_or(a, b) __sync_fetch_and_or(a, b)
#define clib_atomic_fetch_nand(a, b) __sync_fetch_nand(a, b)

#define clib_atomic_add_fetch(a, b) __sync_add_and_fetch(a, b)
#define clib_atomic_sub_fetch(a, b) __sync_sub_and_fetch(a, b)
#define clib_atomic_and_fetch(a, b) __sync_and_and_fetch(a, b)
#define clib_atomic_xor_fetch(a, b) __sync_xor_and_fetch(a, b)
#define clib_atomic_or_fetch(a, b) __sync_or_and_fetch(a, b)
#define clib_atomic_nand_fetch(a, b) __sync_nand_and_fetch(a, b)

#define clib_atomic_cmp_and_swap(addr,old,new) __sync_val_compare_and_swap(addr, old, new)
#define clib_atomic_bool_cmp_and_swap(addr,old,new) __sync_bool_compare_and_swap(addr, old, new)

/*Accquire Barrier*/
#define clib_atomic_test_and_set(a) __sync_lock_test_and_set(a, 1)
/*Release Barrier*/
#define clib_atomic_release(a) __sync_lock_release(a)

#endif /* included_clib_atomics_h */
t_to_next >= 4) { b0 = vlib_get_buffer (vm, buffers[4]); b1 = vlib_get_buffer (vm, buffers[5]); b2 = vlib_get_buffer (vm, buffers[6]); b3 = vlib_get_buffer (vm, buffers[7]); vlib_prefetch_buffer_header (b0, STORE); vlib_prefetch_buffer_header (b1, STORE); vlib_prefetch_buffer_header (b2, STORE); vlib_prefetch_buffer_header (b3, STORE); /* enqueue buffer */ to_next[0] = bi0 = buffers[0]; to_next[1] = bi1 = buffers[1]; to_next[2] = bi2 = buffers[2]; to_next[3] = bi3 = buffers[3]; to_next += 4; n_left_to_next -= 4; buffers += 4; b0 = vlib_get_buffer (vm, bi0); b1 = vlib_get_buffer (vm, bi1); b2 = vlib_get_buffer (vm, bi2); b3 = vlib_get_buffer (vm, bi3); vnet_buffer (b0)->sw_if_index[VLIB_RX] = mif->sw_if_index; vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0; vnet_buffer (b1)->sw_if_index[VLIB_RX] = mif->sw_if_index; vnet_buffer (b1)->sw_if_index[VLIB_TX] = ~0; vnet_buffer (b2)->sw_if_index[VLIB_RX] = mif->sw_if_index; vnet_buffer (b2)->sw_if_index[VLIB_TX] = ~0; vnet_buffer (b3)->sw_if_index[VLIB_RX] = mif->sw_if_index; vnet_buffer (b3)->sw_if_index[VLIB_TX] = ~0; if (mode == MEMIF_INTERFACE_MODE_IP) { next0 = memif_next_from_ip_hdr (node, b0); next1 = memif_next_from_ip_hdr (node, b1); next2 = memif_next_from_ip_hdr (node, b2); next3 = memif_next_from_ip_hdr (node, b3); } else if (mode == MEMIF_INTERFACE_MODE_ETHERNET) { if (PREDICT_FALSE (mif->per_interface_next_index != ~0)) { next0 = mif->per_interface_next_index; next1 = mif->per_interface_next_index; next2 = mif->per_interface_next_index; next3 = mif->per_interface_next_index; } else { next0 = next1 = next2 = next3 = next_index; /* redirect if feature path enabled */ vnet_feature_start_device_input_x1 (mif->sw_if_index, &next0, b0); vnet_feature_start_device_input_x1 (mif->sw_if_index, &next1, b1); vnet_feature_start_device_input_x1 (mif->sw_if_index, &next2, b2); vnet_feature_start_device_input_x1 (mif->sw_if_index, &next3, b3); } } /* trace */ if (PREDICT_FALSE (n_trace > 0)) { memif_trace_buffer (vm, node, mif, b0, next0, qid, &n_trace); if (PREDICT_FALSE (n_trace > 0)) memif_trace_buffer (vm, node, mif, b1, next1, qid, &n_trace); if (PREDICT_FALSE (n_trace > 0)) memif_trace_buffer (vm, node, mif, b2, next2, qid, &n_trace); if (PREDICT_FALSE (n_trace > 0)) memif_trace_buffer (vm, node, mif, b3, next3, qid, &n_trace); } /* enqueue */ vlib_validate_buffer_enqueue_x4 (vm, node, next_index, to_next, n_left_to_next, bi0, bi1, bi2, bi3, next0, next1, next2, next3); /* next */ n_from -= 4; } while (n_from && n_left_to_next) { /* enqueue buffer */ to_next[0] = bi0 = buffers[0]; to_next += 1; n_left_to_next--; buffers += 1; b0 = vlib_get_buffer (vm, bi0); vnet_buffer (b0)->sw_if_index[VLIB_RX] = mif->sw_if_index; vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0; if (mode == MEMIF_INTERFACE_MODE_IP) { next0 = memif_next_from_ip_hdr (node, b0); } else if (mode == MEMIF_INTERFACE_MODE_ETHERNET) { if (PREDICT_FALSE (mif->per_interface_next_index != ~0)) next0 = mif->per_interface_next_index; else { next0 = next_index; /* redirect if feature path enabled */ vnet_feature_start_device_input_x1 (mif->sw_if_index, &next0, b0); } } /* trace */ if (PREDICT_FALSE (n_trace > 0)) memif_trace_buffer (vm, node, mif, b0, next0, qid, &n_trace); /* enqueue */ vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, n_left_to_next, bi0, next0); /* next */ n_from--; } vlib_put_next_frame (vm, node, next_index, n_left_to_next); } vlib_increment_combined_counter (vnm->interface_main.combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX, thread_index, mif->sw_if_index, n_rx_packets, n_rx_bytes); /* refill ring with empty buffers */ refill: vec_reset_length (ptd->buffers); head = ring->head; n_slots = ring_size - head + mq->last_tail; slot = head & mask; n_slots &= ~7; if (n_slots < 32) goto done; memif_desc_t desc_template, *dt = &desc_template; clib_memset (dt, 0, sizeof (memif_desc_t)); dt->length = buffer_length; n_alloc = vlib_buffer_alloc_to_ring_from_pool ( vm, mq->buffers, slot, ring_size, n_slots, mq->buffer_pool_index); dt->region = mq->buffer_pool_index + 1; offset = (u64) mif->regions[dt->region].shm - start_offset; if (PREDICT_FALSE (n_alloc != n_slots)) vlib_error_count (vm, node->node_index, MEMIF_INPUT_ERROR_BUFFER_ALLOC_FAIL, 1); head += n_alloc; while (n_alloc) { memif_desc_t *d = ring->desc + slot; u32 *bi = mq->buffers + slot; if (PREDICT_FALSE (((slot + 7 > mask) || (n_alloc < 8)))) goto one_by_one; clib_memcpy_fast (d + 0, dt, sizeof (memif_desc_t)); clib_memcpy_fast (d + 1, dt, sizeof (memif_desc_t)); clib_memcpy_fast (d + 2, dt, sizeof (memif_desc_t)); clib_memcpy_fast (d + 3, dt, sizeof (memif_desc_t)); clib_memcpy_fast (d + 4, dt, sizeof (memif_desc_t)); clib_memcpy_fast (d + 5, dt, sizeof (memif_desc_t)); clib_memcpy_fast (d + 6, dt, sizeof (memif_desc_t)); clib_memcpy_fast (d + 7, dt, sizeof (memif_desc_t)); d[0].offset = (u64) vlib_get_buffer (vm, bi[0])->data - offset; d[1].offset = (u64) vlib_get_buffer (vm, bi[1])->data - offset; d[2].offset = (u64) vlib_get_buffer (vm, bi[2])->data - offset; d[3].offset = (u64) vlib_get_buffer (vm, bi[3])->data - offset; d[4].offset = (u64) vlib_get_buffer (vm, bi[4])->data - offset; d[5].offset = (u64) vlib_get_buffer (vm, bi[5])->data - offset; d[6].offset = (u64) vlib_get_buffer (vm, bi[6])->data - offset; d[7].offset = (u64) vlib_get_buffer (vm, bi[7])->data - offset; slot = (slot + 8) & mask; n_alloc -= 8; continue; one_by_one: clib_memcpy_fast (d, dt, sizeof (memif_desc_t)); d[0].offset = (u64) vlib_get_buffer (vm, bi[0])->data - offset; slot = (slot + 1) & mask; n_alloc -= 1; } __atomic_store_n (&ring->head, head, __ATOMIC_RELEASE); done: return n_rx_packets; } VLIB_NODE_FN (memif_input_node) (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) { u32 n_rx = 0; memif_main_t *mm = &memif_main; memif_interface_mode_t mode_ip = MEMIF_INTERFACE_MODE_IP; memif_interface_mode_t mode_eth = MEMIF_INTERFACE_MODE_ETHERNET; vnet_hw_if_rxq_poll_vector_t *pv; pv = vnet_hw_if_get_rxq_poll_vector (vm, node); for (int i = 0; i < vec_len (pv); i++) { memif_if_t *mif; u32 qid; mif = vec_elt_at_index (mm->interfaces, pv[i].dev_instance); qid = pv[i].queue_id; if ((mif->flags & MEMIF_IF_FLAG_ADMIN_UP) && (mif->flags & MEMIF_IF_FLAG_CONNECTED)) { if (mif->flags & MEMIF_IF_FLAG_ZERO_COPY) { if (mif->mode == MEMIF_INTERFACE_MODE_IP) n_rx += memif_device_input_zc_inline (vm, node, mif, qid, mode_ip); else n_rx += memif_device_input_zc_inline (vm, node, mif, qid, mode_eth); } else if (mif->flags & MEMIF_IF_FLAG_IS_SLAVE) { if (mif->mode == MEMIF_INTERFACE_MODE_IP) n_rx += memif_device_input_inline ( vm, node, mif, MEMIF_RING_M2S, qid, mode_ip); else n_rx += memif_device_input_inline ( vm, node, mif, MEMIF_RING_M2S, qid, mode_eth); } else { if (mif->mode == MEMIF_INTERFACE_MODE_IP) n_rx += memif_device_input_inline ( vm, node, mif, MEMIF_RING_S2M, qid, mode_ip); else n_rx += memif_device_input_inline ( vm, node, mif, MEMIF_RING_S2M, qid, mode_eth); } } } return n_rx; } /* *INDENT-OFF* */ VLIB_REGISTER_NODE (memif_input_node) = { .name = "memif-input", .flags = VLIB_NODE_FLAG_TRACE_SUPPORTED, .sibling_of = "device-input", .format_trace = format_memif_input_trace, .type = VLIB_NODE_TYPE_INPUT, .state = VLIB_NODE_STATE_INTERRUPT, .n_errors = MEMIF_INPUT_N_ERROR, .error_counters = memif_input_error_counters, }; /* *INDENT-ON* */ /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */