Merge "PROX generator: performance optimization (2/4)"
[samplevnf.git] / VNFs / DPPD-PROX / lconf.c
1 /*
2 // Copyright (c) 2010-2017 Intel Corporation
3 //
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
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 */
16
17 #include "prox_malloc.h"
18 #include "lconf.h"
19 #include "rx_pkt.h"
20 #include "tx_pkt.h"
21 #include "log.h"
22 #include "quit.h"
23 #include "prox_cfg.h"
24
25 struct lcore_cfg *lcore_cfg;
26 /* only used at initialization time */
27 struct lcore_cfg  lcore_cfg_init[RTE_MAX_LCORE];
28
29 static int core_targ_next_from(struct lcore_cfg **lconf, struct task_args **targ, struct lcore_cfg *lcore_cfg, const int with_master)
30 {
31         uint32_t lcore_id, task_id;
32
33         if (*lconf && *targ) {
34                 lcore_id = *lconf - lcore_cfg;
35                 task_id = *targ - lcore_cfg[lcore_id].targs;
36
37                 if (task_id + 1 < lcore_cfg[lcore_id].n_tasks_all) {
38                         *targ = &lcore_cfg[lcore_id].targs[task_id + 1];
39                         return 0;
40                 } else {
41                         if (prox_core_next(&lcore_id, with_master))
42                                 return -1;
43                         *lconf = &lcore_cfg[lcore_id];
44                         *targ = &lcore_cfg[lcore_id].targs[0];
45                         return 0;
46                 }
47         } else {
48                 lcore_id = -1;
49
50                 if (prox_core_next(&lcore_id, with_master))
51                         return -1;
52                 *lconf = &lcore_cfg[lcore_id];
53                 *targ = &lcore_cfg[lcore_id].targs[0];
54                 return 0;
55         }
56 }
57
58 int core_targ_next(struct lcore_cfg **lconf, struct task_args **targ, const int with_master)
59 {
60         return core_targ_next_from(lconf, targ, lcore_cfg, with_master);
61 }
62
63 int core_targ_next_early(struct lcore_cfg **lconf, struct task_args **targ, const int with_master)
64 {
65         return core_targ_next_from(lconf, targ, lcore_cfg_init, with_master);
66 }
67
68 struct task_args *core_targ_get(uint32_t lcore_id, uint32_t task_id)
69 {
70         return &lcore_cfg[lcore_id].targs[task_id];
71 }
72
73 void lcore_cfg_alloc_hp(void)
74 {
75         size_t mem_size = RTE_MAX_LCORE * sizeof(struct lcore_cfg);
76
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);
80
81         /* get thread ID for master core */
82         lcore_cfg[rte_lcore_id()].thread_id = pthread_self();
83 }
84
85 int lconf_run(__attribute__((unused)) void *dummy)
86 {
87         uint32_t lcore_id = rte_lcore_id();
88         struct lcore_cfg *lconf = &lcore_cfg[lcore_id];
89
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);
93         if (ret != 0)
94                 plog_warn("pthread_setcanceltype() failed on core %u: %i\n", lcore_id, ret);
95
96         plog_info("Entering main loop on core %u\n", lcore_id);
97         return lconf->thread_x(lconf);
98 }
99
100 static void msg_stop(struct lcore_cfg *lconf)
101 {
102         int idx = -1;
103         struct task_base *t = NULL;
104
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];
110                                 if (t->aux->stop)
111                                     t->aux->stop(t);
112                         }
113                 }
114                 lconf->n_tasks_run = 0;
115
116                 if (t && t->aux->stop_last)
117                         t->aux->stop_last(t);
118         }
119         else {
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) {
122                                 idx = i;
123                         }
124                         else if (idx != -1) {
125                                 lconf->tasks_run[idx] = lconf->tasks_run[i];
126
127                                 idx++;
128                         }
129                 }
130                 lconf->task_is_running[lconf->msg.task_id] = 0;
131
132                 t = lconf->tasks_all[lconf->msg.task_id];
133                 if (t->aux->stop)
134                         t->aux->stop(t);
135                 lconf->n_tasks_run--;
136                 if (lconf->n_tasks_run == 0 && t->aux->stop_last)
137                         t->aux->stop_last(t);
138         }
139 }
140
141 static void msg_start(struct lcore_cfg *lconf)
142 {
143         int idx = 1;
144         struct task_base *t = NULL;
145
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;
153                         }
154                         if (t->aux->start)
155                                 t->aux->start(t);
156                 }
157                 lconf->n_tasks_run = lconf->n_tasks_all;
158         }
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;
163
164                 if (t->aux->start_first)
165                         t->aux->start_first(t);
166                 if (t->aux->start)
167                         t->aux->start(t);
168         }
169         else {
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) {
173                                 break;
174                         }
175                         else if (idx > lconf->msg.task_id) {
176                                 lconf->tasks_run[i + 1] = lconf->tasks_run[i];
177                                 if (i == 0) {
178                                         lconf->tasks_run[i] = lconf->tasks_all[lconf->msg.task_id];
179                                         lconf->n_tasks_run++;
180                                         break;
181                                 }
182                         }
183                         else {
184                                 lconf->tasks_run[i + 1] = lconf->tasks_all[lconf->msg.task_id];
185                                 lconf->n_tasks_run++;
186                                 break;
187                         }
188                 }
189                 lconf->task_is_running[lconf->msg.task_id] = 1;
190
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]);
193         }
194 }
195
196 int lconf_do_flags(struct lcore_cfg *lconf)
197 {
198         struct task_base *t;
199         int ret = 0;
200
201         if ((lconf->msg.type == LCONF_MSG_TRACE) && (lconf->tasks_all[lconf->msg.task_id]->tx_pkt == tx_pkt_drop_all)) {
202                 /* We are asked to dump packets through command dump.
203                  * This usually means map RX and TX packets before printing them.
204                  * However we do not transmit the packets in this case => use the DUMP_RX function.
205                  * This will prevent seeing the received packets also printed as TX[255] (= dropped)
206                  */
207                 lconf->msg.type = LCONF_MSG_DUMP_RX;
208         }
209
210         switch (lconf->msg.type) {
211         case LCONF_MSG_STOP:
212                 msg_stop(lconf);
213                 ret = -1;
214                 break;
215         case LCONF_MSG_START:
216                 msg_start(lconf);
217                 ret = -1;
218                 break;
219         case LCONF_MSG_DUMP_RX:
220         case LCONF_MSG_DUMP_TX:
221         case LCONF_MSG_DUMP:
222                 t = lconf->tasks_all[lconf->msg.task_id];
223
224                 if (lconf->msg.val) {
225                         if (lconf->msg.type == LCONF_MSG_DUMP ||
226                             lconf->msg.type == LCONF_MSG_DUMP_RX) {
227                                 t->aux->task_rt_dump.n_print_rx = lconf->msg.val;
228
229                                 task_base_add_rx_pkt_function(t, rx_pkt_dump);
230                         }
231
232                         if (lconf->msg.type == LCONF_MSG_DUMP ||
233                             lconf->msg.type == LCONF_MSG_DUMP_TX) {
234                                 t->aux->task_rt_dump.n_print_tx = lconf->msg.val;
235                                 if (t->tx_pkt == tx_pkt_l3) {
236                                         if (t->aux->tx_pkt_orig)
237                                                 t->aux->tx_pkt_l2 = t->aux->tx_pkt_orig;
238                                         t->aux->tx_pkt_orig = t->aux->tx_pkt_l2;
239                                         t->aux->tx_pkt_l2 = tx_pkt_dump;
240                                 } else {
241                                         if (t->aux->tx_pkt_orig)
242                                                 t->tx_pkt = t->aux->tx_pkt_orig;
243                                         t->aux->tx_pkt_orig = t->tx_pkt;
244                                         t->tx_pkt = tx_pkt_dump;
245                                 }
246                         }
247                 }
248                 break;
249         case LCONF_MSG_TRACE:
250                 t = lconf->tasks_all[lconf->msg.task_id];
251
252                 if (lconf->msg.val) {
253                         t->aux->task_rt_dump.n_trace = lconf->msg.val;
254
255                         if (task_base_get_original_rx_pkt_function(t) != rx_pkt_dummy) {
256                                 task_base_add_rx_pkt_function(t, rx_pkt_trace);
257                                 if (t->tx_pkt == tx_pkt_l3) {
258                                         if (t->aux->tx_pkt_orig)
259                                                 t->aux->tx_pkt_l2 = t->aux->tx_pkt_orig;
260                                         t->aux->tx_pkt_orig = t->aux->tx_pkt_l2;
261                                         t->aux->tx_pkt_l2 = tx_pkt_trace;
262                                 } else {
263                                         if (t->aux->tx_pkt_orig)
264                                                 t->tx_pkt = t->aux->tx_pkt_orig;
265                                         t->aux->tx_pkt_orig = t->tx_pkt;
266                                         t->tx_pkt = tx_pkt_trace;
267                                 }
268                         } else {
269                                 t->aux->task_rt_dump.n_print_tx = lconf->msg.val;
270                                 if (t->tx_pkt == tx_pkt_l3) {
271                                         if (t->aux->tx_pkt_orig)
272                                                 t->aux->tx_pkt_l2 = t->aux->tx_pkt_orig;
273                                         t->aux->tx_pkt_orig = t->aux->tx_pkt_l2;
274                                         t->aux->tx_pkt_l2 = tx_pkt_dump;
275                                 } else {
276                                         if (t->aux->tx_pkt_orig)
277                                                 t->tx_pkt = t->aux->tx_pkt_orig;
278                                         t->aux->tx_pkt_orig = t->tx_pkt;
279                                         t->tx_pkt = tx_pkt_dump;
280                                 }
281                         }
282                 }
283                 break;
284         case LCONF_MSG_RX_DISTR_START:
285                 for (uint8_t task_id = 0; task_id < lconf->n_tasks_all; ++task_id) {
286                         t = lconf->tasks_all[task_id];
287                         task_base_add_rx_pkt_function(t, rx_pkt_distr);
288                         memset(t->aux->rx_bucket, 0, sizeof(t->aux->rx_bucket));
289                         lconf->flags |= LCONF_FLAG_RX_DISTR_ACTIVE;
290                 }
291                 break;
292         case LCONF_MSG_TX_DISTR_START:
293                 for (uint8_t task_id = 0; task_id < lconf->n_tasks_all; ++task_id) {
294                         t = lconf->tasks_all[task_id];
295
296                         if (t->tx_pkt == tx_pkt_l3) {
297                                 t->aux->tx_pkt_orig = t->aux->tx_pkt_l2;
298                                 t->aux->tx_pkt_l2 = tx_pkt_distr;
299                         } else {
300                                 t->aux->tx_pkt_orig = t->tx_pkt;
301                                 t->tx_pkt = tx_pkt_distr;
302                         }
303                         memset(t->aux->tx_bucket, 0, sizeof(t->aux->tx_bucket));
304                         lconf->flags |= LCONF_FLAG_TX_DISTR_ACTIVE;
305                 }
306                 break;
307         case LCONF_MSG_RX_DISTR_STOP:
308                 for (uint8_t task_id = 0; task_id < lconf->n_tasks_all; ++task_id) {
309                         t = lconf->tasks_all[task_id];
310                         task_base_del_rx_pkt_function(t, rx_pkt_distr);
311                         lconf->flags &= ~LCONF_FLAG_RX_DISTR_ACTIVE;
312                 }
313                 break;
314         case LCONF_MSG_TX_DISTR_STOP:
315                 for (uint8_t task_id = 0; task_id < lconf->n_tasks_all; ++task_id) {
316                         t = lconf->tasks_all[task_id];
317                         if (t->aux->tx_pkt_orig) {
318                                 if (t->tx_pkt == tx_pkt_l3) {
319                                         t->aux->tx_pkt_l2 = t->aux->tx_pkt_orig;
320                                         t->aux->tx_pkt_orig = NULL;
321                                 } else {
322                                         t->tx_pkt = t->aux->tx_pkt_orig;
323                                         t->aux->tx_pkt_orig = NULL;
324                                 }
325                                 lconf->flags &= ~LCONF_FLAG_TX_DISTR_ACTIVE;
326                         }
327                 }
328                 break;
329         case LCONF_MSG_RX_DISTR_RESET:
330                 for (uint8_t task_id = 0; task_id < lconf->n_tasks_all; ++task_id) {
331                         t = lconf->tasks_all[task_id];
332
333                         memset(t->aux->rx_bucket, 0, sizeof(t->aux->rx_bucket));
334                 }
335                 break;
336         case LCONF_MSG_TX_DISTR_RESET:
337                 for (uint8_t task_id = 0; task_id < lconf->n_tasks_all; ++task_id) {
338                         t = lconf->tasks_all[task_id];
339
340                         memset(t->aux->tx_bucket, 0, sizeof(t->aux->tx_bucket));
341                 }
342                 break;
343         case LCONF_MSG_RX_BW_START:
344                 for (uint8_t task_id = 0; task_id < lconf->n_tasks_all; ++task_id) {
345                         t = lconf->tasks_all[task_id];
346                         task_base_add_rx_pkt_function(t, rx_pkt_bw);
347                         lconf->flags |= LCONF_FLAG_RX_BW_ACTIVE;
348                 }
349                 break;
350         case LCONF_MSG_RX_BW_STOP:
351                 for (uint8_t task_id = 0; task_id < lconf->n_tasks_all; ++task_id) {
352                         t = lconf->tasks_all[task_id];
353                         task_base_del_rx_pkt_function(t, rx_pkt_bw);
354                         lconf->flags &= ~LCONF_FLAG_RX_BW_ACTIVE;
355                 }
356                 break;
357         case LCONF_MSG_TX_BW_START:
358                 for (uint8_t task_id = 0; task_id < lconf->n_tasks_all; ++task_id) {
359                         t = lconf->tasks_all[task_id];
360
361                         if (t->tx_pkt == tx_pkt_l3) {
362                                 t->aux->tx_pkt_orig = t->aux->tx_pkt_l2;
363                                 t->aux->tx_pkt_l2 = tx_pkt_bw;
364                         } else {
365                                 t->aux->tx_pkt_orig = t->tx_pkt;
366                                 t->tx_pkt = tx_pkt_bw;
367                         }
368                         lconf->flags |= LCONF_FLAG_TX_BW_ACTIVE;
369                 }
370                 break;
371         case LCONF_MSG_TX_BW_STOP:
372                 for (uint8_t task_id = 0; task_id < lconf->n_tasks_all; ++task_id) {
373                         t = lconf->tasks_all[task_id];
374                         if (t->aux->tx_pkt_orig) {
375                                 if (t->tx_pkt == tx_pkt_l3) {
376                                         t->aux->tx_pkt_l2 = t->aux->tx_pkt_orig;
377                                         t->aux->tx_pkt_orig = NULL;
378                                 } else {
379                                         t->tx_pkt = t->aux->tx_pkt_orig;
380                                         t->aux->tx_pkt_orig = NULL;
381                                 }
382                                 lconf->flags &= ~LCONF_FLAG_TX_BW_ACTIVE;
383                         }
384                 }
385                 break;
386         }
387
388         lconf_unset_req(lconf);
389         return ret;
390 }
391
392 int lconf_get_task_id(const struct lcore_cfg *lconf, const struct task_base *task)
393 {
394         for (int i = 0; i < lconf->n_tasks_all; ++i) {
395                 if (lconf->tasks_all[i] == task)
396                         return i;
397         }
398
399         return -1;
400 }
401
402 int lconf_task_is_running(const struct lcore_cfg *lconf, uint8_t task_id)
403 {
404         return lconf->task_is_running[task_id];
405 }