X-Git-Url: http://git.droids-corp.org/?a=blobdiff_plain;f=lib%2Flibrte_ipsec%2Fipsec_sad.c;h=3f9533c80a682d274572948354233234d534db6f;hb=35a7fe80c29446eab49ffd7e81a4f610fd1f98ce;hp=db2c44c8043ec53f151b6216d6285a99b5676940;hpb=b2ee269267755f3784efcc5da3bf65e6f4f3dda4;p=dpdk.git diff --git a/lib/librte_ipsec/ipsec_sad.c b/lib/librte_ipsec/ipsec_sad.c index db2c44c804..3f9533c80a 100644 --- a/lib/librte_ipsec/ipsec_sad.c +++ b/lib/librte_ipsec/ipsec_sad.c @@ -2,10 +2,12 @@ * Copyright(c) 2019 Intel Corporation */ +#include + #include #include #include -#include +#include #include #include #include @@ -20,12 +22,11 @@ * indicate presence of entries with the same SPI in DIP and DIP+SIP tables. */ -#define IPSEC_SAD_NAMESIZE 64 #define SAD_PREFIX "SAD_" /* "SAD_" */ #define SAD_FORMAT SAD_PREFIX "%s" -#define DEFAULT_HASH_FUNC rte_jhash +#define DEFAULT_HASH_FUNC rte_hash_crc #define MIN_HASH_ENTRIES 8U /* From rte_cuckoo_hash.h */ struct hash_cnt { @@ -34,8 +35,10 @@ struct hash_cnt { }; struct rte_ipsec_sad { - char name[IPSEC_SAD_NAMESIZE]; + char name[RTE_IPSEC_SAD_NAMESIZE]; struct rte_hash *hash[RTE_IPSEC_SAD_KEY_TYPE_MASK]; + uint32_t keysize[RTE_IPSEC_SAD_KEY_TYPE_MASK]; + uint32_t init_val; /* Array to track number of more specific rules * (spi_dip or spi_dip_sip). Used only in add/delete * as a helper struct. @@ -71,29 +74,38 @@ add_specific(struct rte_ipsec_sad *sad, const void *key, /* Check if the key is present in the table. * Need for further accaunting in cnt_arr */ - ret = rte_hash_lookup(sad->hash[key_type], key); + ret = rte_hash_lookup_with_hash(sad->hash[key_type], key, + rte_hash_crc(key, sad->keysize[key_type], sad->init_val)); notexist = (ret == -ENOENT); /* Add an SA to the corresponding table.*/ - ret = rte_hash_add_key_data(sad->hash[key_type], key, sa); + ret = rte_hash_add_key_with_hash_data(sad->hash[key_type], key, + rte_hash_crc(key, sad->keysize[key_type], sad->init_val), sa); if (ret != 0) return ret; /* Check if there is an entry in SPI only table with the same SPI */ - ret = rte_hash_lookup_data(sad->hash[RTE_IPSEC_SAD_SPI_ONLY], - key, &tmp_val); + ret = rte_hash_lookup_with_hash_data(sad->hash[RTE_IPSEC_SAD_SPI_ONLY], + key, rte_hash_crc(key, sad->keysize[RTE_IPSEC_SAD_SPI_ONLY], + sad->init_val), &tmp_val); if (ret < 0) tmp_val = NULL; tmp_val = SET_BIT(tmp_val, key_type); /* Add an entry into SPI only table */ - ret = rte_hash_add_key_data(sad->hash[RTE_IPSEC_SAD_SPI_ONLY], - key, tmp_val); + ret = rte_hash_add_key_with_hash_data( + sad->hash[RTE_IPSEC_SAD_SPI_ONLY], key, + rte_hash_crc(key, sad->keysize[RTE_IPSEC_SAD_SPI_ONLY], + sad->init_val), tmp_val); if (ret != 0) return ret; /* Update a counter for a given SPI */ - ret = rte_hash_lookup(sad->hash[RTE_IPSEC_SAD_SPI_ONLY], key); + ret = rte_hash_lookup_with_hash(sad->hash[RTE_IPSEC_SAD_SPI_ONLY], key, + rte_hash_crc(key, sad->keysize[RTE_IPSEC_SAD_SPI_ONLY], + sad->init_val)); + if (ret < 0) + return ret; if (key_type == RTE_IPSEC_SAD_SPI_DIP) sad->cnt_arr[ret].cnt_dip += notexist; else @@ -123,15 +135,17 @@ rte_ipsec_sad_add(struct rte_ipsec_sad *sad, */ switch (key_type) { case(RTE_IPSEC_SAD_SPI_ONLY): - ret = rte_hash_lookup_data(sad->hash[key_type], - key, &tmp_val); + ret = rte_hash_lookup_with_hash_data(sad->hash[key_type], + key, rte_hash_crc(key, sad->keysize[key_type], + sad->init_val), &tmp_val); if (ret >= 0) tmp_val = SET_BIT(sa, GET_BIT(tmp_val, RTE_IPSEC_SAD_KEY_TYPE_MASK)); else tmp_val = sa; - ret = rte_hash_add_key_data(sad->hash[key_type], - key, tmp_val); + ret = rte_hash_add_key_with_hash_data(sad->hash[key_type], + key, rte_hash_crc(key, sad->keysize[key_type], + sad->init_val), tmp_val); return ret; case(RTE_IPSEC_SAD_SPI_DIP): case(RTE_IPSEC_SAD_SPI_DIP_SIP): @@ -159,13 +173,15 @@ del_specific(struct rte_ipsec_sad *sad, const void *key, int key_type) uint32_t *cnt; /* Remove an SA from the corresponding table.*/ - ret = rte_hash_del_key(sad->hash[key_type], key); + ret = rte_hash_del_key_with_hash(sad->hash[key_type], key, + rte_hash_crc(key, sad->keysize[key_type], sad->init_val)); if (ret < 0) return ret; /* Get an index of cnt_arr entry for a given SPI */ - ret = rte_hash_lookup_data(sad->hash[RTE_IPSEC_SAD_SPI_ONLY], - key, &tmp_val); + ret = rte_hash_lookup_with_hash_data(sad->hash[RTE_IPSEC_SAD_SPI_ONLY], + key, rte_hash_crc(key, sad->keysize[RTE_IPSEC_SAD_SPI_ONLY], + sad->init_val), &tmp_val); if (ret < 0) return ret; cnt = (key_type == RTE_IPSEC_SAD_SPI_DIP) ? @@ -183,10 +199,15 @@ del_specific(struct rte_ipsec_sad *sad, const void *key, int key_type) * remove an entry from SPI_only table */ if (tmp_val == NULL) - ret = rte_hash_del_key(sad->hash[RTE_IPSEC_SAD_SPI_ONLY], key); + ret = rte_hash_del_key_with_hash( + sad->hash[RTE_IPSEC_SAD_SPI_ONLY], key, + rte_hash_crc(key, sad->keysize[RTE_IPSEC_SAD_SPI_ONLY], + sad->init_val)); else - ret = rte_hash_add_key_data(sad->hash[RTE_IPSEC_SAD_SPI_ONLY], - key, tmp_val); + ret = rte_hash_add_key_with_hash_data( + sad->hash[RTE_IPSEC_SAD_SPI_ONLY], key, + rte_hash_crc(key, sad->keysize[RTE_IPSEC_SAD_SPI_ONLY], + sad->init_val), tmp_val); if (ret < 0) return ret; return 0; @@ -204,19 +225,23 @@ rte_ipsec_sad_del(struct rte_ipsec_sad *sad, return -EINVAL; switch (key_type) { case(RTE_IPSEC_SAD_SPI_ONLY): - ret = rte_hash_lookup_data(sad->hash[key_type], - key, &tmp_val); + ret = rte_hash_lookup_with_hash_data(sad->hash[key_type], + key, rte_hash_crc(key, sad->keysize[key_type], + sad->init_val), &tmp_val); if (ret < 0) return ret; if (GET_BIT(tmp_val, RTE_IPSEC_SAD_KEY_TYPE_MASK) == 0) { - ret = rte_hash_del_key(sad->hash[key_type], - key); + ret = rte_hash_del_key_with_hash(sad->hash[key_type], + key, rte_hash_crc(key, sad->keysize[key_type], + sad->init_val)); ret = ret < 0 ? ret : 0; } else { tmp_val = GET_BIT(tmp_val, RTE_IPSEC_SAD_KEY_TYPE_MASK); - ret = rte_hash_add_key_data(sad->hash[key_type], - key, tmp_val); + ret = rte_hash_add_key_with_hash_data( + sad->hash[key_type], key, + rte_hash_crc(key, sad->keysize[key_type], + sad->init_val), tmp_val); } return ret; case(RTE_IPSEC_SAD_SPI_DIP): @@ -231,7 +256,7 @@ struct rte_ipsec_sad * rte_ipsec_sad_create(const char *name, const struct rte_ipsec_sad_conf *conf) { char hash_name[RTE_HASH_NAMESIZE]; - char sad_name[IPSEC_SAD_NAMESIZE]; + char sad_name[RTE_IPSEC_SAD_NAMESIZE]; struct rte_tailq_entry *te; struct rte_ipsec_sad_list *sad_list; struct rte_ipsec_sad *sad, *tmp_sad = NULL; @@ -249,8 +274,8 @@ rte_ipsec_sad_create(const char *name, const struct rte_ipsec_sad_conf *conf) return NULL; } - ret = snprintf(sad_name, IPSEC_SAD_NAMESIZE, SAD_FORMAT, name); - if (ret < 0 || ret >= IPSEC_SAD_NAMESIZE) { + ret = snprintf(sad_name, RTE_IPSEC_SAD_NAMESIZE, SAD_FORMAT, name); + if (ret < 0 || ret >= RTE_IPSEC_SAD_NAMESIZE) { rte_errno = ENAMETOOLONG; return NULL; } @@ -273,6 +298,7 @@ rte_ipsec_sad_create(const char *name, const struct rte_ipsec_sad_conf *conf) hash_params.hash_func = DEFAULT_HASH_FUNC; hash_params.hash_func_init_val = rte_rand(); + sad->init_val = hash_params.hash_func_init_val; hash_params.socket_id = conf->socket_id; hash_params.name = hash_name; if (conf->flags & RTE_IPSEC_SAD_FLAG_RW_CONCURRENCY) @@ -281,6 +307,7 @@ rte_ipsec_sad_create(const char *name, const struct rte_ipsec_sad_conf *conf) /** Init hash[RTE_IPSEC_SAD_SPI_ONLY] for SPI only */ snprintf(hash_name, sizeof(hash_name), "sad_1_%p", sad); hash_params.key_len = sizeof(((struct rte_ipsec_sadv4_key *)0)->spi); + sad->keysize[RTE_IPSEC_SAD_SPI_ONLY] = hash_params.key_len; hash_params.entries = sa_sum; sad->hash[RTE_IPSEC_SAD_SPI_ONLY] = rte_hash_create(&hash_params); if (sad->hash[RTE_IPSEC_SAD_SPI_ONLY] == NULL) { @@ -296,6 +323,7 @@ rte_ipsec_sad_create(const char *name, const struct rte_ipsec_sad_conf *conf) else hash_params.key_len += sizeof(((struct rte_ipsec_sadv4_key *)0)->dip); + sad->keysize[RTE_IPSEC_SAD_SPI_DIP] = hash_params.key_len; hash_params.entries = RTE_MAX(MIN_HASH_ENTRIES, conf->max_sa[RTE_IPSEC_SAD_SPI_DIP]); sad->hash[RTE_IPSEC_SAD_SPI_DIP] = rte_hash_create(&hash_params); @@ -312,6 +340,7 @@ rte_ipsec_sad_create(const char *name, const struct rte_ipsec_sad_conf *conf) else hash_params.key_len += sizeof(((struct rte_ipsec_sadv4_key *)0)->sip); + sad->keysize[RTE_IPSEC_SAD_SPI_DIP_SIP] = hash_params.key_len; hash_params.entries = RTE_MAX(MIN_HASH_ENTRIES, conf->max_sa[RTE_IPSEC_SAD_SPI_DIP_SIP]); sad->hash[RTE_IPSEC_SAD_SPI_DIP_SIP] = rte_hash_create(&hash_params); @@ -326,7 +355,8 @@ rte_ipsec_sad_create(const char *name, const struct rte_ipsec_sad_conf *conf) /* guarantee there's no existing */ TAILQ_FOREACH(te, sad_list, next) { tmp_sad = (struct rte_ipsec_sad *)te->data; - if (strncmp(sad_name, tmp_sad->name, IPSEC_SAD_NAMESIZE) == 0) + if (strncmp(sad_name, tmp_sad->name, + RTE_IPSEC_SAD_NAMESIZE) == 0) break; } if (te != NULL) { @@ -354,14 +384,14 @@ rte_ipsec_sad_create(const char *name, const struct rte_ipsec_sad_conf *conf) struct rte_ipsec_sad * rte_ipsec_sad_find_existing(const char *name) { - char sad_name[IPSEC_SAD_NAMESIZE]; + char sad_name[RTE_IPSEC_SAD_NAMESIZE]; struct rte_ipsec_sad *sad = NULL; struct rte_tailq_entry *te; struct rte_ipsec_sad_list *sad_list; int ret; - ret = snprintf(sad_name, IPSEC_SAD_NAMESIZE, SAD_FORMAT, name); - if (ret < 0 || ret >= IPSEC_SAD_NAMESIZE) { + ret = snprintf(sad_name, RTE_IPSEC_SAD_NAMESIZE, SAD_FORMAT, name); + if (ret < 0 || ret >= RTE_IPSEC_SAD_NAMESIZE) { rte_errno = ENAMETOOLONG; return NULL; } @@ -372,7 +402,7 @@ rte_ipsec_sad_find_existing(const char *name) rte_mcfg_tailq_read_lock(); TAILQ_FOREACH(te, sad_list, next) { sad = (struct rte_ipsec_sad *) te->data; - if (strncmp(sad_name, sad->name, IPSEC_SAD_NAMESIZE) == 0) + if (strncmp(sad_name, sad->name, RTE_IPSEC_SAD_NAMESIZE) == 0) break; } rte_mcfg_tailq_read_unlock(); @@ -440,15 +470,21 @@ __ipsec_sad_lookup(const struct rte_ipsec_sad *sad, uint32_t n_3 = 0; uint32_t i; int found = 0; + hash_sig_t hash_sig[RTE_HASH_LOOKUP_BULK_MAX]; + hash_sig_t hash_sig_2[RTE_HASH_LOOKUP_BULK_MAX]; + hash_sig_t hash_sig_3[RTE_HASH_LOOKUP_BULK_MAX]; - for (i = 0; i < n; i++) + for (i = 0; i < n; i++) { sa[i] = NULL; + hash_sig[i] = rte_hash_crc_4byte(keys[i]->v4.spi, + sad->init_val); + } /* * Lookup keys in SPI only hash table first. */ - rte_hash_lookup_bulk_data(sad->hash[RTE_IPSEC_SAD_SPI_ONLY], - (const void **)keys, n, &mask_1, sa); + rte_hash_lookup_with_hash_bulk_data(sad->hash[RTE_IPSEC_SAD_SPI_ONLY], + (const void **)keys, hash_sig, n, &mask_1, sa); for (map = mask_1; map; map &= (map - 1)) { i = rte_bsf64(map); /* @@ -457,10 +493,16 @@ __ipsec_sad_lookup(const struct rte_ipsec_sad *sad, */ if ((uintptr_t)sa[i] & RTE_IPSEC_SAD_SPI_DIP_SIP) { idx_3[n_3] = i; + hash_sig_3[n_3] = rte_hash_crc(keys[i], + sad->keysize[RTE_IPSEC_SAD_SPI_DIP_SIP], + sad->init_val); keys_3[n_3++] = keys[i]; } if ((uintptr_t)sa[i] & RTE_IPSEC_SAD_SPI_DIP) { idx_2[n_2] = i; + hash_sig_2[n_2] = rte_hash_crc(keys[i], + sad->keysize[RTE_IPSEC_SAD_SPI_DIP], + sad->init_val); keys_2[n_2++] = keys[i]; } /* clear 2 LSB's which indicate the presence @@ -471,8 +513,9 @@ __ipsec_sad_lookup(const struct rte_ipsec_sad *sad, /* Lookup for more specific rules in SPI_DIP table */ if (n_2 != 0) { - rte_hash_lookup_bulk_data(sad->hash[RTE_IPSEC_SAD_SPI_DIP], - keys_2, n_2, &mask_2, vals_2); + rte_hash_lookup_with_hash_bulk_data( + sad->hash[RTE_IPSEC_SAD_SPI_DIP], + keys_2, hash_sig_2, n_2, &mask_2, vals_2); for (map_spec = mask_2; map_spec; map_spec &= (map_spec - 1)) { i = rte_bsf64(map_spec); sa[idx_2[i]] = vals_2[i]; @@ -480,8 +523,9 @@ __ipsec_sad_lookup(const struct rte_ipsec_sad *sad, } /* Lookup for more specific rules in SPI_DIP_SIP table */ if (n_3 != 0) { - rte_hash_lookup_bulk_data(sad->hash[RTE_IPSEC_SAD_SPI_DIP_SIP], - keys_3, n_3, &mask_3, vals_3); + rte_hash_lookup_with_hash_bulk_data( + sad->hash[RTE_IPSEC_SAD_SPI_DIP_SIP], + keys_3, hash_sig_3, n_3, &mask_3, vals_3); for (map_spec = mask_3; map_spec; map_spec &= (map_spec - 1)) { i = rte_bsf64(map_spec); sa[idx_3[i]] = vals_3[i];