2 // Copyright (c) 2010-2017 Intel Corporation
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
17 #include "prox_malloc.h"
25 struct lcore_cfg *lcore_cfg;
26 /* only used at initialization time */
27 struct lcore_cfg lcore_cfg_init[RTE_MAX_LCORE];
29 static int core_targ_next_from(struct lcore_cfg **lconf, struct task_args **targ, struct lcore_cfg *lcore_cfg, const int with_master)
31 uint32_t lcore_id, task_id;
33 if (*lconf && *targ) {
34 lcore_id = *lconf - lcore_cfg;
35 task_id = *targ - lcore_cfg[lcore_id].targs;
37 if (task_id + 1 < lcore_cfg[lcore_id].n_tasks_all) {
38 *targ = &lcore_cfg[lcore_id].targs[task_id + 1];
41 if (prox_core_next(&lcore_id, with_master))
43 *lconf = &lcore_cfg[lcore_id];
44 *targ = &lcore_cfg[lcore_id].targs[0];
50 if (prox_core_next(&lcore_id, with_master))
52 *lconf = &lcore_cfg[lcore_id];
53 *targ = &lcore_cfg[lcore_id].targs[0];
58 int core_targ_next(struct lcore_cfg **lconf, struct task_args **targ, const int with_master)
60 return core_targ_next_from(lconf, targ, lcore_cfg, with_master);
63 int core_targ_next_early(struct lcore_cfg **lconf, struct task_args **targ, const int with_master)
65 return core_targ_next_from(lconf, targ, lcore_cfg_init, with_master);
68 struct task_args *core_targ_get(uint32_t lcore_id, uint32_t task_id)
70 return &lcore_cfg[lcore_id].targs[task_id];
73 void lcore_cfg_alloc_hp(void)
75 size_t mem_size = RTE_MAX_LCORE * sizeof(struct lcore_cfg);
77 lcore_cfg = prox_zmalloc(mem_size, rte_socket_id());
78 PROX_PANIC(lcore_cfg == NULL, "Could not allocate memory for core control structures\n");
79 rte_memcpy(lcore_cfg, lcore_cfg_init, mem_size);
81 /* get thread ID for master core */
82 lcore_cfg[rte_lcore_id()].thread_id = pthread_self();
85 int lconf_run(__attribute__((unused)) void *dummy)
87 uint32_t lcore_id = rte_lcore_id();
88 struct lcore_cfg *lconf = &lcore_cfg[lcore_id];
90 /* get thread ID, and set cancellation type to asynchronous */
91 lconf->thread_id = pthread_self();
92 int ret = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
94 plog_warn("pthread_setcanceltype() failed on core %u: %i\n", lcore_id, ret);
96 plog_info("Entering main loop on core %u\n", lcore_id);
97 return lconf->thread_x(lconf);
100 static void msg_stop(struct lcore_cfg *lconf)
103 struct task_base *t = NULL;
105 if (lconf->msg.task_id == -1) {
106 for (int i = 0; i < lconf->n_tasks_all; ++i) {
107 if (lconf->task_is_running[i]) {
108 lconf->task_is_running[i] = 0;
109 t = lconf->tasks_all[i];
114 lconf->n_tasks_run = 0;
116 if (t && t->aux->stop_last)
117 t->aux->stop_last(t);
120 for (int i = 0; i < lconf->n_tasks_run; ++i) {
121 if (lconf_get_task_id(lconf, lconf->tasks_run[i]) == lconf->msg.task_id) {
124 else if (idx != -1) {
125 lconf->tasks_run[idx] = lconf->tasks_run[i];
130 lconf->task_is_running[lconf->msg.task_id] = 0;
132 t = lconf->tasks_all[lconf->msg.task_id];
135 lconf->n_tasks_run--;
136 if (lconf->n_tasks_run == 0 && t->aux->stop_last)
137 t->aux->stop_last(t);
141 static void msg_start(struct lcore_cfg *lconf)
144 struct task_base *t = NULL;
146 if (lconf->msg.task_id == -1) {
147 for (int i = 0; i < lconf->n_tasks_all; ++i) {
148 t = lconf->tasks_run[i] = lconf->tasks_all[i];
149 lconf->task_is_running[i] = 1;
150 if (lconf->n_tasks_run == 0 && t->aux->start_first) {
151 t->aux->start_first(t);
152 lconf->n_tasks_run = 1;
157 lconf->n_tasks_run = lconf->n_tasks_all;
159 else if (lconf->n_tasks_run == 0) {
160 t = lconf->tasks_run[0] = lconf->tasks_all[lconf->msg.task_id];
161 lconf->n_tasks_run = 1;
162 lconf->task_is_running[lconf->msg.task_id] = 1;
164 if (t->aux->start_first)
165 t->aux->start_first(t);
170 for (int i = lconf->n_tasks_run - 1; i >= 0; --i) {
171 idx = lconf_get_task_id(lconf, lconf->tasks_run[i]);
172 if (idx == lconf->msg.task_id) {
175 else if (idx > lconf->msg.task_id) {
176 lconf->tasks_run[i + 1] = lconf->tasks_run[i];
178 lconf->tasks_run[i] = lconf->tasks_all[lconf->msg.task_id];
179 lconf->n_tasks_run++;
184 lconf->tasks_run[i + 1] = lconf->tasks_all[lconf->msg.task_id];
185 lconf->n_tasks_run++;
189 lconf->task_is_running[lconf->msg.task_id] = 1;
191 if (lconf->tasks_all[lconf->msg.task_id]->aux->start)
192 lconf->tasks_all[lconf->msg.task_id]->aux->start(lconf->tasks_all[lconf->msg.task_id]);
196 int lconf_do_flags(struct lcore_cfg *lconf)
201 switch (lconf->msg.type) {
206 case LCONF_MSG_START:
210 case LCONF_MSG_DUMP_RX:
211 case LCONF_MSG_DUMP_TX:
213 t = lconf->tasks_all[lconf->msg.task_id];
215 if (lconf->msg.val) {
216 if (lconf->msg.type == LCONF_MSG_DUMP ||
217 lconf->msg.type == LCONF_MSG_DUMP_RX) {
218 t->aux->task_rt_dump.n_print_rx = lconf->msg.val;
220 task_base_add_rx_pkt_function(t, rx_pkt_dump);
223 if (lconf->msg.type == LCONF_MSG_DUMP ||
224 lconf->msg.type == LCONF_MSG_DUMP_TX) {
225 t->aux->task_rt_dump.n_print_tx = lconf->msg.val;
226 if (t->tx_pkt == tx_pkt_l3) {
227 if (t->aux->tx_pkt_orig)
228 t->aux->tx_pkt_l2 = t->aux->tx_pkt_orig;
229 t->aux->tx_pkt_orig = t->aux->tx_pkt_l2;
230 t->aux->tx_pkt_l2 = tx_pkt_dump;
232 if (t->aux->tx_pkt_orig)
233 t->tx_pkt = t->aux->tx_pkt_orig;
234 t->aux->tx_pkt_orig = t->tx_pkt;
235 t->tx_pkt = tx_pkt_dump;
240 case LCONF_MSG_TRACE:
241 t = lconf->tasks_all[lconf->msg.task_id];
243 if (lconf->msg.val) {
244 t->aux->task_rt_dump.n_trace = lconf->msg.val;
246 if (task_base_get_original_rx_pkt_function(t) != rx_pkt_dummy) {
247 task_base_add_rx_pkt_function(t, rx_pkt_trace);
248 if (t->tx_pkt == tx_pkt_l3) {
249 if (t->aux->tx_pkt_orig)
250 t->aux->tx_pkt_l2 = t->aux->tx_pkt_orig;
251 t->aux->tx_pkt_orig = t->aux->tx_pkt_l2;
252 t->aux->tx_pkt_l2 = tx_pkt_trace;
254 if (t->aux->tx_pkt_orig)
255 t->tx_pkt = t->aux->tx_pkt_orig;
256 t->aux->tx_pkt_orig = t->tx_pkt;
257 t->tx_pkt = tx_pkt_trace;
260 t->aux->task_rt_dump.n_print_tx = lconf->msg.val;
261 if (t->tx_pkt == tx_pkt_l3) {
262 if (t->aux->tx_pkt_orig)
263 t->aux->tx_pkt_l2 = t->aux->tx_pkt_orig;
264 t->aux->tx_pkt_orig = t->aux->tx_pkt_l2;
265 t->aux->tx_pkt_l2 = tx_pkt_dump;
267 if (t->aux->tx_pkt_orig)
268 t->tx_pkt = t->aux->tx_pkt_orig;
269 t->aux->tx_pkt_orig = t->tx_pkt;
270 t->tx_pkt = tx_pkt_dump;
275 case LCONF_MSG_RX_DISTR_START:
276 for (uint8_t task_id = 0; task_id < lconf->n_tasks_all; ++task_id) {
277 t = lconf->tasks_all[task_id];
278 task_base_add_rx_pkt_function(t, rx_pkt_distr);
279 memset(t->aux->rx_bucket, 0, sizeof(t->aux->rx_bucket));
280 lconf->flags |= LCONF_FLAG_RX_DISTR_ACTIVE;
283 case LCONF_MSG_TX_DISTR_START:
284 for (uint8_t task_id = 0; task_id < lconf->n_tasks_all; ++task_id) {
285 t = lconf->tasks_all[task_id];
287 if (t->tx_pkt == tx_pkt_l3) {
288 t->aux->tx_pkt_orig = t->aux->tx_pkt_l2;
289 t->aux->tx_pkt_l2 = tx_pkt_distr;
291 t->aux->tx_pkt_orig = t->tx_pkt;
292 t->tx_pkt = tx_pkt_distr;
294 memset(t->aux->tx_bucket, 0, sizeof(t->aux->tx_bucket));
295 lconf->flags |= LCONF_FLAG_TX_DISTR_ACTIVE;
298 case LCONF_MSG_RX_DISTR_STOP:
299 for (uint8_t task_id = 0; task_id < lconf->n_tasks_all; ++task_id) {
300 t = lconf->tasks_all[task_id];
301 task_base_del_rx_pkt_function(t, rx_pkt_distr);
302 lconf->flags &= ~LCONF_FLAG_RX_DISTR_ACTIVE;
305 case LCONF_MSG_TX_DISTR_STOP:
306 for (uint8_t task_id = 0; task_id < lconf->n_tasks_all; ++task_id) {
307 t = lconf->tasks_all[task_id];
308 if (t->aux->tx_pkt_orig) {
309 if (t->tx_pkt == tx_pkt_l3) {
310 t->tx_pkt = t->aux->tx_pkt_orig;
311 t->aux->tx_pkt_orig = NULL;
313 t->aux->tx_pkt_l2 = t->aux->tx_pkt_orig;
314 t->aux->tx_pkt_orig = NULL;
316 lconf->flags &= ~LCONF_FLAG_TX_DISTR_ACTIVE;
320 case LCONF_MSG_RX_DISTR_RESET:
321 for (uint8_t task_id = 0; task_id < lconf->n_tasks_all; ++task_id) {
322 t = lconf->tasks_all[task_id];
324 memset(t->aux->rx_bucket, 0, sizeof(t->aux->rx_bucket));
327 case LCONF_MSG_TX_DISTR_RESET:
328 for (uint8_t task_id = 0; task_id < lconf->n_tasks_all; ++task_id) {
329 t = lconf->tasks_all[task_id];
331 memset(t->aux->tx_bucket, 0, sizeof(t->aux->tx_bucket));
334 case LCONF_MSG_RX_BW_START:
335 for (uint8_t task_id = 0; task_id < lconf->n_tasks_all; ++task_id) {
336 t = lconf->tasks_all[task_id];
337 task_base_add_rx_pkt_function(t, rx_pkt_bw);
338 lconf->flags |= LCONF_FLAG_RX_BW_ACTIVE;
341 case LCONF_MSG_RX_BW_STOP:
342 for (uint8_t task_id = 0; task_id < lconf->n_tasks_all; ++task_id) {
343 t = lconf->tasks_all[task_id];
344 task_base_del_rx_pkt_function(t, rx_pkt_bw);
345 lconf->flags &= ~LCONF_FLAG_RX_BW_ACTIVE;
348 case LCONF_MSG_TX_BW_START:
349 for (uint8_t task_id = 0; task_id < lconf->n_tasks_all; ++task_id) {
350 t = lconf->tasks_all[task_id];
352 if (t->tx_pkt == tx_pkt_l3) {
353 t->aux->tx_pkt_orig = t->aux->tx_pkt_l2;
354 t->aux->tx_pkt_l2 = tx_pkt_bw;
356 t->aux->tx_pkt_orig = t->tx_pkt;
357 t->tx_pkt = tx_pkt_bw;
359 lconf->flags |= LCONF_FLAG_TX_BW_ACTIVE;
362 case LCONF_MSG_TX_BW_STOP:
363 for (uint8_t task_id = 0; task_id < lconf->n_tasks_all; ++task_id) {
364 t = lconf->tasks_all[task_id];
365 if (t->aux->tx_pkt_orig) {
366 if (t->tx_pkt == tx_pkt_l3) {
367 t->aux->tx_pkt_l2 = t->aux->tx_pkt_orig;
368 t->aux->tx_pkt_orig = NULL;
370 t->tx_pkt = t->aux->tx_pkt_orig;
371 t->aux->tx_pkt_orig = NULL;
373 lconf->flags &= ~LCONF_FLAG_TX_BW_ACTIVE;
379 lconf_unset_req(lconf);
383 int lconf_get_task_id(const struct lcore_cfg *lconf, const struct task_base *task)
385 for (int i = 0; i < lconf->n_tasks_all; ++i) {
386 if (lconf->tasks_all[i] == task)
393 int lconf_task_is_running(const struct lcore_cfg *lconf, uint8_t task_id)
395 return lconf->task_is_running[task_id];