summaryrefslogtreecommitdiff
path: root/accel-dp/main.c
blob: bdf8163239b49f3ed53af774e9459b843c83bbbe (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/stat.h>
#include <sys/resource.h>
#include <sys/socket.h>

#include <rte_config.h>
#include <rte_memory.h>
#include <rte_memzone.h>
#include <rte_launch.h>
#include <rte_eal.h>
#include <rte_per_lcore.h>
#include <rte_lcore.h>
#include <rte_ethdev.h>
#include <rte_log.h>
#include <rte_errno.h>
#include <rte_debug.h>

#include "init.h"
#include "conf_file.h"
#include "event.h"

#define RTE_LOGTYPE_INIT RTE_LOGTYPE_USER1
#define MBUF_CACHE_SIZE 128

int sock_fd;

static struct rte_mempool *mbuf_pool;

static LIST_HEAD(init_list);

struct init {
	struct list_head entry;

	int order;
	void (*func)(void);
};

void register_init(int order, void (*func)(void))
{
	struct init *i1, *i = malloc(sizeof(*i));
	struct list_head *p = init_list.next;

	i->order = order;
	i->func = func;

	while (p != &init_list) {
		i1 = list_entry(p, typeof(*i1), entry);
		if (order < i1->order)
			break;
		p = p->next;
	}
	list_add_tail(&i->entry, p);
}

static void run_init(void)
{
	struct init *i;

	list_for_each_entry(i, &init_list, entry)
		i->func();
}

static void change_limits(void)
{
	FILE *f;
	struct rlimit lim;
	unsigned int nr_open = 1024*1024;

	f = fopen("/proc/sys/fs/nr_open", "r");
	if (f) {
		fscanf(f, "%d", &nr_open);
		fclose(f);
	}

	lim.rlim_cur = nr_open;
	lim.rlim_max = nr_open;
	setrlimit(RLIMIT_NOFILE, &lim);
}

static int bind_driver(const char *opt, char **val)
{
	char *ptr = strchr(opt, ':');
	char drv[128];
	int fd, r;
	char fname[1024];
	char bus_id[64];
	struct stat st;
	char vendor[16], device[16];

	if (!ptr)
		return -1;

	if (strchr(ptr + 1, ':')) {
		memcpy(drv, opt, ptr - opt);
		drv[ptr - opt] = 0;
		opt = ptr + 1;
	} else
		strcpy(drv, "uio_pci_generic");

	sprintf(bus_id, "0000:%s", opt);

	sprintf(fname, "/sys/bus/pci/devices/%s", bus_id);
	if (stat(fname, &st)) {
		fprintf(stderr, "%s: device not found\n", opt);
		return -1;
	}

	sprintf(fname, "/sys/bus/pci/devices/%s/vendor", bus_id);
	fd = open(fname, O_RDONLY);
	r = read(fd, vendor, sizeof(vendor));
	if (r <= 0) {
		fprintf(stderr, "%s: failed to read vendor\n", opt);
		close(fd);
		return -1;
	}
	vendor[r - 1] = 0;
	close(fd);

	sprintf(fname, "/sys/bus/pci/devices/%s/device", bus_id);
	fd = open(fname, O_RDONLY);
	r = read(fd, device, sizeof(device));
	if (r <= 0) {
		fprintf(stderr, "%s: failed to read device\n", opt);
		close(fd);
		return -1;
	}
	device[r - 1] = 0;
	close(fd);

	sprintf(fname, "modprobe -q %s", drv);
	system(fname);

	sprintf(fname, "/sys/bus/pci/drivers/%s", drv);
	if (stat(fname, &st)) {
		fprintf(stderr, "%s: driver '%s' is not loaded\n", opt, drv);
		return -1;
	}

	sprintf(fname, "/sys/bus/pci/devices/%s/driver", bus_id);

	r = readlink(fname, fname, sizeof(fname));
	if (r > 0) {
		fname[r] = 0;
		ptr = fname + r - 1;
		while (*ptr != '/')
			ptr--;

		if (strcmp(ptr + 1, drv)) {
			fprintf(stderr, "%s: unbind driver: %s\n", opt, ptr + 1);

			sprintf(fname, "/sys/bus/pci/devices/%s/driver/unbind", bus_id);

			fd = open(fname, O_WRONLY);
			if (fd < 0) {
				fprintf(stderr, "%s: failed to unbind driver: %s\n", opt, strerror(errno));
				return -1;
			}

			if (write(fd, bus_id, strlen(bus_id)) < 0) {
				fprintf(stderr, "%s: failed to unbind driver: %s\n", opt, strerror(errno));
				close(fd);
				return -1;
			}
			close(fd);
		} else
			goto out;
	}

	sprintf(fname, "/sys/bus/pci/drivers/%s/new_id", drv);
	fd = open(fname, O_WRONLY);
	if (fd < 0) {
		fprintf(stderr, "%s: failed to bind driver %s: %s\n", opt, drv, strerror(errno));
		return -1;
	}

	sprintf(fname, "%s %s", vendor, device);
	if (write(fd, fname, strlen(fname)) < 0) {
		fprintf(stderr, "%s: failed to bind driver %s: %s\n", opt, drv, strerror(errno));
		close(fd);
		return -1;
	}
	close(fd);

	/*sprintf(fname, "/sys/bus/pci/drivers/%s/bind", drv);
	fd = open(fname, O_WRONLY);
	if (fd < 0) {
		fprintf(stderr, "%s: failed to bind driver %s: %s\n", opt, drv, strerror(errno));
		close(fd);
		return -1;
	}

	if (write(fd, bus_id, strlen(bus_id)) < 0) {
		fprintf(stderr, "%s: failed to bind driver %s: %s\n", opt, drv, strerror(errno));
		close(fd);
		return -1;
	}
	close(fd);*/

out:
	*val = (char *)opt;
	return 0;
}

static int build_rte_args(char **argv)
{
	int i = 0;
	struct conf_sect *s = conf_get_sect("core");
	struct conf_opt *opt;

	if (!s)
		return 0;

	for (opt = s->opt; opt; opt = opt->next) {
		if (!strcmp(opt->name, "coremask")) {
			argv[i++] = "-c";
			argv[i++] = opt->val;
		} else if (!strcmp(opt->name, "corelist")) {
			argv[i++] = "-l";
			argv[i++] = opt->val;
		} else if (!strcmp(opt->name, "coremap")) {
			argv[i++] = "--lcores";
			argv[i++] = opt->val;
		} else if (!strcmp(opt->name, "master-lcore")) {
			argv[i++] = "--master-lcore";
			argv[i++] = opt->val;
		} else if (!strcmp(opt->name, "mem-channels")) {
			argv[i++] = "-n";
			argv[i++] = opt->val;
		} else if (!strcmp(opt->name, "mem-ranks")) {
			argv[i++] = "-r";
			argv[i++] = opt->val;
		} else if (!strcmp(opt->name, "socket-mem")) {
			argv[i++] = "-m";
			argv[i++] = opt->val;
		} else if (!strcmp(opt->name, "huge-dir")) {
			argv[i++] = "--huge-dir";
			argv[i++] = opt->val;
		}
	}

	s = conf_get_sect("interface");

	for (opt = s->opt; opt; opt = opt->next) {
		const char *busid = conf_get_subopt(opt, "busid");
		if (!busid) {
			fprintf(stderr, "%s: busid not specified\n", opt->name);
			return -1;
		}

		if (strcmp(busid, "kni")) {
			if (bind_driver(busid, &argv[i + 1]))
				return -1;
			argv[i++] = "-w";
			i++;
		}
	}

	return i;
}

static int init_pktmbuf_pool()
{
	int mbuf_cnt = 16*1024;
	const char *opt = conf_get_opt("core", "mbuf-count");

	if (opt)
		mbuf_cnt = atoi(opt);

	mbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", mbuf_cnt, MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
	if (!mbuf_pool) {
		fprintf(stderr, "%s\n", rte_strerror(rte_errno));
		return -1;
	}

	return 0;
}

int main(int argc, char **argv)
{
	unsigned lcore_id;
	int i;
	char *cf = NULL;
	char *pid_file = NULL;
	int goto_daemon = 0;
	int rte_argc;
	char *rte_argv[256];
	const char *opt;
	int dist_lcore;

	for (i = 1; i < argc; i++) {
		if (!strcmp(argv[i], "-c") && i + 1 < argc)
			cf = argv[++i];
		else if (!strcmp(argv[i], "-p") && i + 1 < argc)
			pid_file = argv[++i];
		else if (!strcmp(argv[i], "-d"))
			goto_daemon = 1;
	}

	if (!cf) {
		printf("usage: accel-dpdk [-d] [-p <pid file>] -c <config file>\n");
		return 1;
	}

	if (conf_load(cf))
		return 1;

	rte_argv[0] = argv[0];
	rte_argc = build_rte_args(rte_argv + 1) + 1;
	if (rte_argc == 0)
		return 1;

	if (rte_eal_init(rte_argc, rte_argv) < 0) {
		fprintf(stderr, "Cannot init EAL\n");
		return 1;
	}

	if (init_pktmbuf_pool())
		return -1;

	sock_fd = socket(AF_INET, SOCK_DGRAM, 0);

	run_init();

	if (ctrl_init())
		return 1;

	if (eth_dev_init(mbuf_pool))
		return -1;

	if (kni_dev_init(mbuf_pool))
		return -1;

	opt = conf_get_opt("core", "distributor-lcore");
	if (opt)
		dist_lcore = atoi(opt);
	else
		dist_lcore = -1;

	if (distributor_init(dist_lcore != -1))
		return -1;

	if (goto_daemon)
		daemon(0, 0);

	change_limits();

	if (pid_file) {
		FILE *f = fopen(pid_file, "w");
		if (f) {
			fprintf(f, "%i", getpid());
			fclose(f);
		}
	}

	/* call lcore_hello() on every slave lcore */
	RTE_LCORE_FOREACH_SLAVE(lcore_id) {
		printf("%i\n", lcore_id);
		if (lcore_id == dist_lcore)
			rte_eal_remote_launch(lcore_distributor, NULL, lcore_id);
		else
			rte_eal_remote_launch(lcore_worker, NULL, lcore_id);
	}

	if (dist_lcore == -1)
		distributor_loop(1);
	else
		event_loop();

	rte_eal_mp_wait_lcore();
	return 0;
}