1a9643ea8Slogwang /*- 2a9643ea8Slogwang * BSD LICENSE 3a9643ea8Slogwang * 4a9643ea8Slogwang * Copyright(c) 2010-2014 Intel Corporation. All rights reserved. 5a9643ea8Slogwang * All rights reserved. 6a9643ea8Slogwang * 7a9643ea8Slogwang * Redistribution and use in source and binary forms, with or without 8a9643ea8Slogwang * modification, are permitted provided that the following conditions 9a9643ea8Slogwang * are met: 10a9643ea8Slogwang * 11a9643ea8Slogwang * * Redistributions of source code must retain the above copyright 12a9643ea8Slogwang * notice, this list of conditions and the following disclaimer. 13a9643ea8Slogwang * * Redistributions in binary form must reproduce the above copyright 14a9643ea8Slogwang * notice, this list of conditions and the following disclaimer in 15a9643ea8Slogwang * the documentation and/or other materials provided with the 16a9643ea8Slogwang * distribution. 17a9643ea8Slogwang * * Neither the name of Intel Corporation nor the names of its 18a9643ea8Slogwang * contributors may be used to endorse or promote products derived 19a9643ea8Slogwang * from this software without specific prior written permission. 20a9643ea8Slogwang * 21a9643ea8Slogwang * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22a9643ea8Slogwang * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23a9643ea8Slogwang * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24a9643ea8Slogwang * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25a9643ea8Slogwang * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26a9643ea8Slogwang * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27a9643ea8Slogwang * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28a9643ea8Slogwang * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29a9643ea8Slogwang * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30a9643ea8Slogwang * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31a9643ea8Slogwang * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32a9643ea8Slogwang */ 33a9643ea8Slogwang 34a9643ea8Slogwang #include <string.h> 35a9643ea8Slogwang #include <stdio.h> 36a9643ea8Slogwang 37a9643ea8Slogwang #include <rte_common.h> 38a9643ea8Slogwang #include <rte_mbuf.h> 39a9643ea8Slogwang #include <rte_memory.h> 40a9643ea8Slogwang #include <rte_malloc.h> 41a9643ea8Slogwang #include <rte_log.h> 42a9643ea8Slogwang 43a9643ea8Slogwang #include "rte_table_array.h" 44a9643ea8Slogwang 45a9643ea8Slogwang #ifdef RTE_TABLE_STATS_COLLECT 46a9643ea8Slogwang 47a9643ea8Slogwang #define RTE_TABLE_ARRAY_STATS_PKTS_IN_ADD(table, val) \ 48a9643ea8Slogwang table->stats.n_pkts_in += val 49a9643ea8Slogwang #define RTE_TABLE_ARRAY_STATS_PKTS_LOOKUP_MISS(table, val) \ 50a9643ea8Slogwang table->stats.n_pkts_lookup_miss += val 51a9643ea8Slogwang 52a9643ea8Slogwang #else 53a9643ea8Slogwang 54a9643ea8Slogwang #define RTE_TABLE_ARRAY_STATS_PKTS_IN_ADD(table, val) 55a9643ea8Slogwang #define RTE_TABLE_ARRAY_STATS_PKTS_LOOKUP_MISS(table, val) 56a9643ea8Slogwang 57a9643ea8Slogwang #endif 58a9643ea8Slogwang 59a9643ea8Slogwang struct rte_table_array { 60a9643ea8Slogwang struct rte_table_stats stats; 61a9643ea8Slogwang 62a9643ea8Slogwang /* Input parameters */ 63a9643ea8Slogwang uint32_t entry_size; 64a9643ea8Slogwang uint32_t n_entries; 65a9643ea8Slogwang uint32_t offset; 66a9643ea8Slogwang 67a9643ea8Slogwang /* Internal fields */ 68a9643ea8Slogwang uint32_t entry_pos_mask; 69a9643ea8Slogwang 70a9643ea8Slogwang /* Internal table */ 71a9643ea8Slogwang uint8_t array[0] __rte_cache_aligned; 72a9643ea8Slogwang } __rte_cache_aligned; 73a9643ea8Slogwang 74a9643ea8Slogwang static void * 75a9643ea8Slogwang rte_table_array_create(void *params, int socket_id, uint32_t entry_size) 76a9643ea8Slogwang { 77*2bfe3f2eSlogwang struct rte_table_array_params *p = params; 78a9643ea8Slogwang struct rte_table_array *t; 79a9643ea8Slogwang uint32_t total_cl_size, total_size; 80a9643ea8Slogwang 81a9643ea8Slogwang /* Check input parameters */ 82a9643ea8Slogwang if ((p == NULL) || 83a9643ea8Slogwang (p->n_entries == 0) || 84a9643ea8Slogwang (!rte_is_power_of_2(p->n_entries))) 85a9643ea8Slogwang return NULL; 86a9643ea8Slogwang 87a9643ea8Slogwang /* Memory allocation */ 88a9643ea8Slogwang total_cl_size = (sizeof(struct rte_table_array) + 89a9643ea8Slogwang RTE_CACHE_LINE_SIZE) / RTE_CACHE_LINE_SIZE; 90a9643ea8Slogwang total_cl_size += (p->n_entries * entry_size + 91a9643ea8Slogwang RTE_CACHE_LINE_SIZE) / RTE_CACHE_LINE_SIZE; 92a9643ea8Slogwang total_size = total_cl_size * RTE_CACHE_LINE_SIZE; 93a9643ea8Slogwang t = rte_zmalloc_socket("TABLE", total_size, RTE_CACHE_LINE_SIZE, socket_id); 94a9643ea8Slogwang if (t == NULL) { 95a9643ea8Slogwang RTE_LOG(ERR, TABLE, 96a9643ea8Slogwang "%s: Cannot allocate %u bytes for array table\n", 97a9643ea8Slogwang __func__, total_size); 98a9643ea8Slogwang return NULL; 99a9643ea8Slogwang } 100a9643ea8Slogwang 101a9643ea8Slogwang /* Memory initialization */ 102a9643ea8Slogwang t->entry_size = entry_size; 103a9643ea8Slogwang t->n_entries = p->n_entries; 104a9643ea8Slogwang t->offset = p->offset; 105a9643ea8Slogwang t->entry_pos_mask = t->n_entries - 1; 106a9643ea8Slogwang 107a9643ea8Slogwang return t; 108a9643ea8Slogwang } 109a9643ea8Slogwang 110a9643ea8Slogwang static int 111a9643ea8Slogwang rte_table_array_free(void *table) 112a9643ea8Slogwang { 113*2bfe3f2eSlogwang struct rte_table_array *t = table; 114a9643ea8Slogwang 115a9643ea8Slogwang /* Check input parameters */ 116a9643ea8Slogwang if (t == NULL) { 117a9643ea8Slogwang RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__); 118a9643ea8Slogwang return -EINVAL; 119a9643ea8Slogwang } 120a9643ea8Slogwang 121a9643ea8Slogwang /* Free previously allocated resources */ 122a9643ea8Slogwang rte_free(t); 123a9643ea8Slogwang 124a9643ea8Slogwang return 0; 125a9643ea8Slogwang } 126a9643ea8Slogwang 127a9643ea8Slogwang static int 128a9643ea8Slogwang rte_table_array_entry_add( 129a9643ea8Slogwang void *table, 130a9643ea8Slogwang void *key, 131a9643ea8Slogwang void *entry, 132a9643ea8Slogwang int *key_found, 133a9643ea8Slogwang void **entry_ptr) 134a9643ea8Slogwang { 135*2bfe3f2eSlogwang struct rte_table_array *t = table; 136*2bfe3f2eSlogwang struct rte_table_array_key *k = key; 137a9643ea8Slogwang uint8_t *table_entry; 138a9643ea8Slogwang 139a9643ea8Slogwang /* Check input parameters */ 140a9643ea8Slogwang if (table == NULL) { 141a9643ea8Slogwang RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__); 142a9643ea8Slogwang return -EINVAL; 143a9643ea8Slogwang } 144a9643ea8Slogwang if (key == NULL) { 145a9643ea8Slogwang RTE_LOG(ERR, TABLE, "%s: key parameter is NULL\n", __func__); 146a9643ea8Slogwang return -EINVAL; 147a9643ea8Slogwang } 148a9643ea8Slogwang if (entry == NULL) { 149a9643ea8Slogwang RTE_LOG(ERR, TABLE, "%s: entry parameter is NULL\n", __func__); 150a9643ea8Slogwang return -EINVAL; 151a9643ea8Slogwang } 152a9643ea8Slogwang if (key_found == NULL) { 153a9643ea8Slogwang RTE_LOG(ERR, TABLE, "%s: key_found parameter is NULL\n", 154a9643ea8Slogwang __func__); 155a9643ea8Slogwang return -EINVAL; 156a9643ea8Slogwang } 157a9643ea8Slogwang if (entry_ptr == NULL) { 158a9643ea8Slogwang RTE_LOG(ERR, TABLE, "%s: entry_ptr parameter is NULL\n", 159a9643ea8Slogwang __func__); 160a9643ea8Slogwang return -EINVAL; 161a9643ea8Slogwang } 162a9643ea8Slogwang 163a9643ea8Slogwang table_entry = &t->array[k->pos * t->entry_size]; 164a9643ea8Slogwang memcpy(table_entry, entry, t->entry_size); 165a9643ea8Slogwang *key_found = 1; 166a9643ea8Slogwang *entry_ptr = (void *) table_entry; 167a9643ea8Slogwang 168a9643ea8Slogwang return 0; 169a9643ea8Slogwang } 170a9643ea8Slogwang 171a9643ea8Slogwang static int 172a9643ea8Slogwang rte_table_array_lookup( 173a9643ea8Slogwang void *table, 174a9643ea8Slogwang struct rte_mbuf **pkts, 175a9643ea8Slogwang uint64_t pkts_mask, 176a9643ea8Slogwang uint64_t *lookup_hit_mask, 177a9643ea8Slogwang void **entries) 178a9643ea8Slogwang { 179a9643ea8Slogwang struct rte_table_array *t = (struct rte_table_array *) table; 180a9643ea8Slogwang __rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask); 181a9643ea8Slogwang RTE_TABLE_ARRAY_STATS_PKTS_IN_ADD(t, n_pkts_in); 182a9643ea8Slogwang *lookup_hit_mask = pkts_mask; 183a9643ea8Slogwang 184a9643ea8Slogwang if ((pkts_mask & (pkts_mask + 1)) == 0) { 185a9643ea8Slogwang uint64_t n_pkts = __builtin_popcountll(pkts_mask); 186a9643ea8Slogwang uint32_t i; 187a9643ea8Slogwang 188a9643ea8Slogwang for (i = 0; i < n_pkts; i++) { 189a9643ea8Slogwang struct rte_mbuf *pkt = pkts[i]; 190a9643ea8Slogwang uint32_t entry_pos = RTE_MBUF_METADATA_UINT32(pkt, 191a9643ea8Slogwang t->offset) & t->entry_pos_mask; 192a9643ea8Slogwang 193a9643ea8Slogwang entries[i] = (void *) &t->array[entry_pos * 194a9643ea8Slogwang t->entry_size]; 195a9643ea8Slogwang } 196a9643ea8Slogwang } else { 197a9643ea8Slogwang for ( ; pkts_mask; ) { 198a9643ea8Slogwang uint32_t pkt_index = __builtin_ctzll(pkts_mask); 199a9643ea8Slogwang uint64_t pkt_mask = 1LLU << pkt_index; 200a9643ea8Slogwang struct rte_mbuf *pkt = pkts[pkt_index]; 201a9643ea8Slogwang uint32_t entry_pos = RTE_MBUF_METADATA_UINT32(pkt, 202a9643ea8Slogwang t->offset) & t->entry_pos_mask; 203a9643ea8Slogwang 204a9643ea8Slogwang entries[pkt_index] = (void *) &t->array[entry_pos * 205a9643ea8Slogwang t->entry_size]; 206a9643ea8Slogwang pkts_mask &= ~pkt_mask; 207a9643ea8Slogwang } 208a9643ea8Slogwang } 209a9643ea8Slogwang 210a9643ea8Slogwang return 0; 211a9643ea8Slogwang } 212a9643ea8Slogwang 213a9643ea8Slogwang static int 214a9643ea8Slogwang rte_table_array_stats_read(void *table, struct rte_table_stats *stats, int clear) 215a9643ea8Slogwang { 216*2bfe3f2eSlogwang struct rte_table_array *array = table; 217a9643ea8Slogwang 218a9643ea8Slogwang if (stats != NULL) 219a9643ea8Slogwang memcpy(stats, &array->stats, sizeof(array->stats)); 220a9643ea8Slogwang 221a9643ea8Slogwang if (clear) 222a9643ea8Slogwang memset(&array->stats, 0, sizeof(array->stats)); 223a9643ea8Slogwang 224a9643ea8Slogwang return 0; 225a9643ea8Slogwang } 226a9643ea8Slogwang 227a9643ea8Slogwang struct rte_table_ops rte_table_array_ops = { 228a9643ea8Slogwang .f_create = rte_table_array_create, 229a9643ea8Slogwang .f_free = rte_table_array_free, 230a9643ea8Slogwang .f_add = rte_table_array_entry_add, 231a9643ea8Slogwang .f_delete = NULL, 232a9643ea8Slogwang .f_add_bulk = NULL, 233a9643ea8Slogwang .f_delete_bulk = NULL, 234a9643ea8Slogwang .f_lookup = rte_table_array_lookup, 235a9643ea8Slogwang .f_stats = rte_table_array_stats_read, 236a9643ea8Slogwang }; 237