aboutsummaryrefslogtreecommitdiffstats
path: root/lib/libtle_glue/fd.c
blob: cc855f9b5d27c1dacbe47cde418a30244055a2b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
 * Copyright (c) 2018 Ant Financial Services Group.
 * 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 <sys/time.h>
#include <sys/resource.h>
#include <string.h>

#include "fd.h"
#include "log.h"
#include "util.h"
#include "config.h"

bool fd_table_initialized;

struct fd_table fd_table = { .fd_base = INT_MAX, };

static int
get_ulimit_nofile(void)
{
	struct rlimit rlim;

#define GLUE_BASE_FD 1024
	if (getrlimit(RLIMIT_NOFILE, &rlim) < 0)
		return GLUE_BASE_FD;

	return rlim.rlim_cur; /* soft limit, rlim_max is the hard limit */
}

static void
fd_num_set(int *fd_base, int *fd_num)
{
	int limit = get_ulimit_nofile();

	/* fix me: alignment of power of two */
	/* fix me: use dup2 to occupy these fds */
	*fd_num = limit / 2;
	*fd_num = RTE_MIN(MAX_STREAMS_PER_CORE * 2 * MAX_NB_CTX, *fd_num);

	*fd_base = limit - *fd_num;
	GLUE_LOG(INFO, "fd_base = %d, fd_num = %d", *fd_base, *fd_num);
}

static void
add_fd(struct rte_mempool *mp __rte_unused, void *opaque __rte_unused,
       void *obj, unsigned obj_idx)
{
	((struct sock *)obj)->fd = obj_idx + fd_table.fd_base;
	fd_table.socks[obj_idx] = obj;
}

void
fd_init(void)
{
	int ret;
	size_t sz;
	uint32_t socket_id;
	int fd_base, fd_num;
	struct rte_mempool *mp = NULL;
	char name[RTE_MEMPOOL_NAMESIZE];

	socket_id = get_socket_id();

	fd_num_set(&fd_base, &fd_num);

	sz = sizeof(fd_table.socks[0]) * fd_num;
	fd_table.socks = rte_zmalloc_socket("fdtable", sz,
				RTE_CACHE_LINE_SIZE, socket_id);
	if (fd_table.socks == NULL) {
		GLUE_LOG(ERR, "Failed to malloc fd table");
		goto err;
	}

	snprintf(name, RTE_MEMPOOL_NAMESIZE, "mp_fd_%d_%d", fd_base, fd_num);
	mp = rte_mempool_create_empty(name, fd_num - 1, sizeof(struct sock),
				      32, 0, socket_id, MEMPOOL_F_DYNAMIC);
	if (mp == NULL) {
		GLUE_LOG(ERR, "Failed to create mp for fd table");
		goto err;
	}

	GLUE_LOG(INFO, "sizeof(struct sock): %lu, elt_size of fd table = %u",
		 sizeof(struct sock), mp->elt_size);

	ret = rte_mempool_set_ops_byname(mp, "ring_mp_mc", NULL);
	if (ret != 0) {
		GLUE_LOG(ERR, "Failed to set mp ops: %d", ret);
		goto err;
	}

	rte_mempool_set_dynamic_size(mp, 1024);
	rte_mempool_set_dynamic_cb(mp, add_fd);

	fd_table.mp = mp;
	fd_table.fd_base = fd_base;
	fd_table.fd_num = fd_num;

	/* should populate after fd_table is set */
	ret = rte_mempool_populate_default(mp);
	if (ret < 0) {
		GLUE_LOG(ERR, "Failed to populate mp: %d", ret);
		goto err;
	}

	fd_table_initialized = true;

	return;
err:
	rte_mempool_free(mp);
	rte_panic("Failed to init fd_table");
}