summaryrefslogtreecommitdiffstats
path: root/common/vnf_common/thread_fe.c
blob: 7029620df35fe7a0b91bb4410b414ff7b3ec450b (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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
// Copyright (c) 2017 Intel Corporation
//
// 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 <rte_common.h>
#include <rte_ring.h>
#include <rte_malloc.h>
#include <cmdline_rdline.h>
#include <cmdline_parse.h>
#include <cmdline_parse_num.h>
#include <cmdline_parse_string.h>
#include <cmdline_parse_ipaddr.h>
#include <cmdline_parse_etheraddr.h>
#include <cmdline_socket.h>
#include <cmdline.h>

#include "thread.h"
#include "thread_fe.h"
#include "pipeline.h"
#include "pipeline_common_fe.h"
#include "app.h"

static inline void *
thread_msg_send_recv(struct app_params *app,
	uint32_t socket_id, uint32_t core_id, uint32_t ht_id,
	void *msg,
	uint32_t timeout_ms)
{
	struct rte_ring *r_req = app_thread_msgq_in_get(app,
		socket_id, core_id, ht_id);
	if(r_req == NULL)
		return NULL;
	struct rte_ring *r_rsp = app_thread_msgq_out_get(app,
		socket_id, core_id, ht_id);
	if(r_rsp == NULL)
		return NULL;
	uint64_t hz = rte_get_tsc_hz();
	void *msg_recv;
	uint64_t deadline;
	int status;

	/* send */
	do {
		status = rte_ring_sp_enqueue(r_req, (void *) msg);
	} while (status == -ENOBUFS);

	/* recv */
	deadline = (timeout_ms) ?
		(rte_rdtsc() + ((hz * timeout_ms) / 1000)) :
		UINT64_MAX;

	do {
		if (rte_rdtsc() > deadline)
			return NULL;

		status = rte_ring_sc_dequeue(r_rsp, &msg_recv);
	} while (status != 0);

	return msg_recv;
}

int
app_pipeline_enable(struct app_params *app,
		uint32_t socket_id,
		uint32_t core_id,
		uint32_t hyper_th_id,
		uint32_t pipeline_id)
{
	struct thread_pipeline_enable_msg_req *req;
	struct thread_pipeline_enable_msg_rsp *rsp;
	int thread_id;
	struct app_pipeline_data *p;
	struct app_pipeline_params *p_params;
	struct pipeline_type *p_type;
	int status;

	if (app == NULL)
		return -1;

	thread_id = cpu_core_map_get_lcore_id(app->core_map,
			socket_id,
			core_id,
			hyper_th_id);

	if ((thread_id < 0) || !app_core_is_enabled(app, thread_id))
		return -1;

	if (app_pipeline_data(app, pipeline_id) == NULL)
		return -1;

	p = &app->pipeline_data[pipeline_id];
	p_params = &app->pipeline_params[pipeline_id];
	p_type = app_pipeline_type_find(app, p_params->type);
	if (p_type == NULL)
		return -1;

	if (p->enabled == 1)
		return -1;

	req = app_msg_alloc(app);
	if (req == NULL)
		return -1;

	req->type = THREAD_MSG_REQ_PIPELINE_ENABLE;
	req->pipeline_id = pipeline_id;
	req->be = p->be;
	req->f_run = p_type->be_ops->f_run;
	req->f_timer = p_type->be_ops->f_timer;
	req->timer_period = p->timer_period;

	rsp = thread_msg_send_recv(app,
		socket_id, core_id, hyper_th_id, req, MSG_TIMEOUT_DEFAULT);
	if (rsp == NULL)
		return -1;

	status = rsp->status;
	app_msg_free(app, rsp);

	if (status != 0)
		return -1;

	p->enabled = 1;
	return 0;
}

int
app_pipeline_disable(struct app_params *app,
		uint32_t socket_id,
		uint32_t core_id,
		uint32_t hyper_th_id,
		uint32_t pipeline_id)
{
	struct thread_pipeline_disable_msg_req *req;
	struct thread_pipeline_disable_msg_rsp *rsp;
	int thread_id;
	struct app_pipeline_data *p;
	int status;

	if (app == NULL)
		return -1;

	thread_id = cpu_core_map_get_lcore_id(app->core_map,
			socket_id,
			core_id,
			hyper_th_id);

	if ((thread_id < 0) || !app_core_is_enabled(app, thread_id))
		return -1;

	if (app_pipeline_data(app, pipeline_id) == NULL)
		return -1;

	p = &app->pipeline_data[pipeline_id];

	if (p->enabled == 0)
		return -1;

	req = app_msg_alloc(app);
	if (req == NULL)
		return -1;

	req->type = THREAD_MSG_REQ_PIPELINE_DISABLE;
	req->pipeline_id = pipeline_id;

	rsp = thread_msg_send_recv(app,
		socket_id, core_id, hyper_th_id, req, MSG_TIMEOUT_DEFAULT);

	if (rsp == NULL)
		return -1;

	status = rsp->status;
	app_msg_free(app, rsp);

	if (status != 0)
		return -1;

	p->enabled = 0;
	return 0;
}

int
app_thread_headroom(struct app_params *app,
		uint32_t socket_id,
		uint32_t core_id,
		uint32_t hyper_th_id)
{
	struct thread_headroom_read_msg_req *req;
	struct thread_headroom_read_msg_rsp *rsp;
	int thread_id;
	int status;

	if (app == NULL)
		return -1;

	thread_id = cpu_core_map_get_lcore_id(app->core_map,
			socket_id,
			core_id,
			hyper_th_id);

	if ((thread_id < 0) || !app_core_is_enabled(app, thread_id))
		return -1;

	req = app_msg_alloc(app);
	if (req == NULL)
		return -1;

	req->type = THREAD_MSG_REQ_HEADROOM_READ;

	rsp = thread_msg_send_recv(app,
		socket_id, core_id, hyper_th_id, req, MSG_TIMEOUT_DEFAULT);

	if (rsp == NULL)
		return -1;

	status = rsp->status;

	if (status != 0)
		return -1;

	printf("%.3f%%\n", rsp->headroom_ratio * 100);


	app_msg_free(app, rsp);

	return 0;
}

/*
 * pipeline enable
 */

struct cmd_pipeline_enable_result {
	cmdline_fixed_string_t t_string;
	cmdline_fixed_string_t t_id_string;
	cmdline_fixed_string_t pipeline_string;
	uint32_t pipeline_id;
	cmdline_fixed_string_t enable_string;
};

static void
cmd_pipeline_enable_parsed(
	void *parsed_result,
	__rte_unused struct cmdline *cl,
	 void *data)
{
	struct cmd_pipeline_enable_result *params = parsed_result;
	struct app_params *app = data;
	int status;
	uint32_t core_id, socket_id, hyper_th_id;

	if (parse_pipeline_core(&socket_id,
			&core_id,
			&hyper_th_id,
			params->t_id_string) != 0) {
		printf("Command failed\n");
		return;
	}

	status = app_pipeline_enable(app,
			socket_id,
			core_id,
			hyper_th_id,
			params->pipeline_id);

	if (status != 0)
		printf("Command failed\n");
}

cmdline_parse_token_string_t cmd_pipeline_enable_t_string =
	TOKEN_STRING_INITIALIZER(struct cmd_pipeline_enable_result, t_string, "t");

cmdline_parse_token_string_t cmd_pipeline_enable_t_id_string =
	TOKEN_STRING_INITIALIZER(struct cmd_pipeline_enable_result, t_id_string,
		NULL);

cmdline_parse_token_string_t cmd_pipeline_enable_pipeline_string =
	TOKEN_STRING_INITIALIZER(struct cmd_pipeline_enable_result, pipeline_string,
		"pipeline");

cmdline_parse_token_num_t cmd_pipeline_enable_pipeline_id =
	TOKEN_NUM_INITIALIZER(struct cmd_pipeline_enable_result, pipeline_id,
		UINT32);

cmdline_parse_token_string_t cmd_pipeline_enable_enable_string =
	TOKEN_STRING_INITIALIZER(struct cmd_pipeline_enable_result, enable_string,
		"enable");

cmdline_parse_inst_t cmd_pipeline_enable = {
	.f = cmd_pipeline_enable_parsed,
	.data = NULL,
	.help_str = "Enable pipeline on specified core",
	.tokens = {
		(void *)&cmd_pipeline_enable_t_string,
		(void *)&cmd_pipeline_enable_t_id_string,
		(void *)&cmd_pipeline_enable_pipeline_string,
		(void *)&cmd_pipeline_enable_pipeline_id,
		(void *)&cmd_pipeline_enable_enable_string,
		NULL,
	},
};

/*
 * pipeline disable
 */

struct cmd_pipeline_disable_result {
	cmdline_fixed_string_t t_string;
	cmdline_fixed_string_t t_id_string;
	cmdline_fixed_string_t pipeline_string;
	uint32_t pipeline_id;
	cmdline_fixed_string_t disable_string;
};

static void
cmd_pipeline_disable_parsed(
	void *parsed_result,
	__rte_unused struct cmdline *cl,
	 void *data)
{
	struct cmd_pipeline_disable_result *params = parsed_result;
	struct app_params *app = data;
	int status;
	uint32_t core_id, socket_id, hyper_th_id;

	if (parse_pipeline_core(&socket_id,
			&core_id,
			&hyper_th_id,
			params->t_id_string) != 0) {
		printf("Command failed\n");
		return;
	}

	status = app_pipeline_disable(app,
			socket_id,
			core_id,
			hyper_th_id,
			params->pipeline_id);

	if (status != 0)
		printf("Command failed\n");
}

cmdline_parse_token_string_t cmd_pipeline_disable_t_string =
	TOKEN_STRING_INITIALIZER(struct cmd_pipeline_disable_result, t_string, "t");

cmdline_parse_token_string_t cmd_pipeline_disable_t_id_string =
	TOKEN_STRING_INITIALIZER(struct cmd_pipeline_disable_result, t_id_string,
		NULL);

cmdline_parse_token_string_t cmd_pipeline_disable_pipeline_string =
	TOKEN_STRING_INITIALIZER(struct cmd_pipeline_disable_result,
		pipeline_string, "pipeline");

cmdline_parse_token_num_t cmd_pipeline_disable_pipeline_id =
	TOKEN_NUM_INITIALIZER(struct cmd_pipeline_disable_result, pipeline_id,
		UINT32);

cmdline_parse_token_string_t cmd_pipeline_disable_disable_string =
	TOKEN_STRING_INITIALIZER(struct cmd_pipeline_disable_result, disable_string,
		"disable");

cmdline_parse_inst_t cmd_pipeline_disable = {
	.f = cmd_pipeline_disable_parsed,
	.data = NULL,
	.help_str = "Disable pipeline on specified core",
	.tokens = {
		(void *)&cmd_pipeline_disable_t_string,
		(void *)&cmd_pipeline_disable_t_id_string,
		(void *)&cmd_pipeline_disable_pipeline_string,
		(void *)&cmd_pipeline_disable_pipeline_id,
		(void *)&cmd_pipeline_disable_disable_string,
		NULL,
	},
};


/*
 * thread headroom
 */

struct cmd_thread_headroom_result {
	cmdline_fixed_string_t t_string;
	cmdline_fixed_string_t t_id_string;
	cmdline_fixed_string_t headroom_string;
};

static void
cmd_thread_headroom_parsed(
	void *parsed_result,
	__rte_unused struct cmdline *cl,
	 void *data)
{
	struct cmd_thread_headroom_result *params = parsed_result;
	struct app_params *app = data;
	int status;
	uint32_t core_id, socket_id, hyper_th_id;

	if (parse_pipeline_core(&socket_id,
			&core_id,
			&hyper_th_id,
			params->t_id_string) != 0) {
		printf("Command failed\n");
		return;
	}

	status = app_thread_headroom(app,
			socket_id,
			core_id,
			hyper_th_id);

	if (status != 0)
		printf("Command failed\n");
}

cmdline_parse_token_string_t cmd_thread_headroom_t_string =
	TOKEN_STRING_INITIALIZER(struct cmd_thread_headroom_result,
	t_string, "t");

cmdline_parse_token_string_t cmd_thread_headroom_t_id_string =
	TOKEN_STRING_INITIALIZER(struct cmd_thread_headroom_result,
	t_id_string, NULL);

cmdline_parse_token_string_t cmd_thread_headroom_headroom_string =
	TOKEN_STRING_INITIALIZER(struct cmd_thread_headroom_result,
		headroom_string, "headroom");

cmdline_parse_inst_t cmd_thread_headroom = {
	.f = cmd_thread_headroom_parsed,
	.data = NULL,
	.help_str = "Display thread headroom",
	.tokens = {
		(void *)&cmd_thread_headroom_t_string,
		(void *)&cmd_thread_headroom_t_id_string,
		(void *)&cmd_thread_headroom_headroom_string,
		NULL,
	},
};


static cmdline_parse_ctx_t thread_cmds[] = {
	(cmdline_parse_inst_t *) &cmd_pipeline_enable,
	(cmdline_parse_inst_t *) &cmd_pipeline_disable,
	(cmdline_parse_inst_t *) &cmd_thread_headroom,
	NULL,
};

int
app_pipeline_thread_cmd_push(struct app_params *app)
{
	uint32_t n_cmds, i;

	/* Check for available slots in the application commands array */
	n_cmds = RTE_DIM(thread_cmds) - 1;
	if (n_cmds > APP_MAX_CMDS - app->n_cmds)
		return -ENOMEM;

	/* Push thread commands into the application */
	memcpy(&app->cmds[app->n_cmds], thread_cmds,
		n_cmds * sizeof(cmdline_parse_ctx_t));

	for (i = 0; i < n_cmds; i++)
		app->cmds[app->n_cmds + i]->data = app;

	app->n_cmds += n_cmds;
	app->cmds[app->n_cmds] = NULL;

	return 0;
}