stream_table.c revision aa97dd1c
1/*
2 * Copyright (c) 2016  Intel Corporation.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#include <string.h>
16#include <rte_malloc.h>
17#include <rte_errno.h>
18
19#include "stream_table.h"
20
21void
22stbl_fini(struct stbl *st)
23{
24	uint32_t i;
25
26	for (i = 0; i != RTE_DIM(st->ht); i++) {
27		rte_hash_free(st->ht[i].t);
28		rte_free(st->ht[i].ent);
29	}
30
31	memset(st, 0, sizeof(*st));
32}
33
34int
35stbl_init(struct stbl *st, uint32_t num, int32_t socket)
36{
37	int32_t rc;
38	size_t i, sz;
39	struct rte_hash_parameters hprm;
40	char buf[RTE_HASH_NAMESIZE];
41
42	num = RTE_MAX(5 * num / 4, 0x10U);
43
44	memset(&hprm, 0, sizeof(hprm));
45	hprm.name = buf;
46	hprm.entries = num;
47	hprm.socket_id = socket;
48
49	rc = 0;
50
51	snprintf(buf, sizeof(buf), "stbl4@%p", st);
52	hprm.key_len = sizeof(struct stbl4_key);
53	st->ht[TLE_V4].t = rte_hash_create(&hprm);
54	if (st->ht[TLE_V4].t == NULL)
55		rc = (rte_errno != 0) ? -rte_errno : -ENOMEM;
56
57	if (rc == 0) {
58		snprintf(buf, sizeof(buf), "stbl6@%p", st);
59		hprm.key_len = sizeof(struct stbl6_key);
60		st->ht[TLE_V6].t = rte_hash_create(&hprm);
61		if (st->ht[TLE_V6].t == NULL)
62			rc = (rte_errno != 0) ? -rte_errno : -ENOMEM;
63	}
64
65	for (i = 0; i != RTE_DIM(st->ht) && rc == 0; i++) {
66
67		sz = sizeof(*st->ht[i].ent) * num;
68		st->ht[i].ent = rte_zmalloc_socket(NULL, sz,
69			RTE_CACHE_LINE_SIZE, socket);
70		if (st->ht[i].ent == NULL)
71			rc = -ENOMEM;
72		else
73			st->ht[i].nb_ent = num;
74	}
75
76	if (rc != 0)
77		stbl_fini(st);
78
79	return rc;
80}
81