PROX: update version
[samplevnf.git] / VNFs / DPPD-PROX / main.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 <string.h>
18 #include <locale.h>
19 #include <unistd.h>
20 #include <signal.h>
21
22 #include <rte_cycles.h>
23 #include <rte_atomic.h>
24 #include <rte_table_hash.h>
25 #include <rte_memzone.h>
26 #include <rte_errno.h>
27
28 #include "prox_malloc.h"
29 #include "run.h"
30 #include "main.h"
31 #include "log.h"
32 #include "quit.h"
33 #include "clock.h"
34 #include "defines.h"
35 #include "version.h"
36 #include "prox_args.h"
37 #include "prox_assert.h"
38 #include "prox_cfg.h"
39 #include "prox_shared.h"
40 #include "prox_port_cfg.h"
41 #include "toeplitz.h"
42 #include "hash_utils.h"
43 #include "handle_lb_net.h"
44 #include "prox_cksum.h"
45 #include "thread_nop.h"
46 #include "thread_generic.h"
47 #include "thread_pipeline.h"
48 #include "cqm.h"
49 #include "handle_master.h"
50
51 #if RTE_VERSION < RTE_VERSION_NUM(1,8,0,0)
52 #define RTE_CACHE_LINE_SIZE CACHE_LINE_SIZE
53 #endif
54
55 uint8_t lb_nb_txrings = 0xff;
56 struct rte_ring *ctrl_rings[RTE_MAX_LCORE*MAX_TASKS_PER_CORE];
57
58 static void __attribute__((noreturn)) prox_usage(const char *prgname)
59 {
60         plog_info("\nUsage: %s [-f CONFIG_FILE] [-a|-e] [-m|-s|-i] [-w DEF] [-u] [-t]\n"
61                   "\t-f CONFIG_FILE : configuration file to load, ./prox.cfg by default\n"
62                   "\t-l LOG_FILE : log file name, ./prox.log by default\n"
63                   "\t-p : include PID in log file name if default log file is used\n"
64                   "\t-o DISPLAY: Set display to use, can be 'curses' (default), 'cli' or 'none'\n"
65                   "\t-v verbosity : initial logging verbosity\n"
66                   "\t-a : autostart all cores (by default)\n"
67                   "\t-e : don't autostart\n"
68                   "\t-n : Create NULL devices instead of using PCI devices, useful together with -i\n"
69                   "\t-m : list supported task modes and exit\n"
70                   "\t-s : check configuration file syntax and exit\n"
71                   "\t-i : check initialization sequence and exit\n"
72                   "\t-u : Listen on UDS /tmp/prox.sock\n"
73                   "\t-t : Listen on TCP port 8474\n"
74                   "\t-q : Pass argument to Lua interpreter, useful to define variables\n"
75                   "\t-w : define variable using syntax varname=value\n"
76                   "\t     takes precedence over variables defined in CONFIG_FILE\n"
77                   "\t-k : Log statistics to file \"stats_dump\" in current directory\n"
78                   "\t-d : Run as daemon, the parent process will block until PROX is not initialized\n"
79                   "\t-z : Ignore CPU topology, implies -i\n"
80                   "\t-r : Change initial screen refresh rate. If set to a lower than 0.001 seconds,\n"
81                   "\t     screen refreshing will be disabled\n"
82                   , prgname);
83         exit(EXIT_FAILURE);
84 }
85
86 static void check_mixed_normal_pipeline(void)
87 {
88         struct lcore_cfg *lconf = NULL;
89         uint32_t lcore_id = -1;
90
91         while (prox_core_next(&lcore_id, 0) == 0) {
92                 lconf = &lcore_cfg[lcore_id];
93
94                 int all_thread_nop = 1;
95                 int generic = 0;
96                 int pipeline = 0;
97                 int l3 = 0;
98                 for (uint8_t task_id = 0; task_id < lconf->n_tasks_all; ++task_id) {
99                         struct task_args *targ = &lconf->targs[task_id];
100                         l3 = !strcmp("l3", targ->sub_mode_str);
101                         all_thread_nop = all_thread_nop && !l3 &&
102                                 targ->task_init->thread_x == thread_nop;
103
104                         pipeline = pipeline || targ->task_init->thread_x == thread_pipeline;
105                         generic = generic || targ->task_init->thread_x == thread_generic || l3;
106                 }
107                 PROX_PANIC(generic && pipeline, "Can't run both pipeline and normal thread on same core\n");
108
109                 if (all_thread_nop)
110                         lconf->thread_x = thread_nop;
111                 else {
112                         lconf->thread_x = thread_generic;
113                 }
114         }
115 }
116
117 static void check_zero_rx(void)
118 {
119         struct lcore_cfg *lconf = NULL;
120         struct task_args *targ;
121
122         while (core_targ_next(&lconf, &targ, 0) == 0) {
123                 if (targ->nb_rxports != 0) {
124                         PROX_PANIC(task_init_flag_set(targ->task_init, TASK_FEATURE_NO_RX),
125                            "\tCore %u task %u: rx_ports configured while mode %s does not use it\n", lconf->id, targ->id, targ->task_init->mode_str);
126                 }
127         }
128 }
129
130 static void check_nb_mbuf(void)
131 {
132         struct lcore_cfg *lconf = NULL;
133         struct task_args *targ = NULL;
134         uint8_t port_id;
135         int n_txd = 0, n_rxd = 0;
136
137         while (core_targ_next(&lconf, &targ, 0) == 0) {
138                 for (uint8_t i = 0; i < targ->nb_txports; ++i) {
139                         port_id = targ->tx_port_queue[i].port;
140                         n_txd = prox_port_cfg[port_id].n_txd;
141                 }
142                 for (uint8_t i = 0; i < targ->nb_rxports; ++i) {
143                         port_id = targ->rx_port_queue[i].port;
144                         n_rxd = prox_port_cfg[port_id].n_rxd;
145                 }
146                 if (targ->nb_mbuf <= n_rxd + n_txd + targ->nb_cache_mbuf + MAX_PKT_BURST) {
147                         plog_warn("Core %d, task %d might not have enough mbufs (%d) to support %d txd, %d rxd and %d cache_mbuf\n",
148                                 lconf->id, targ->id, targ->nb_mbuf, n_txd, n_rxd, targ->nb_cache_mbuf);
149                 }
150         }
151 }
152
153 static void check_missing_rx(void)
154 {
155         struct lcore_cfg *lconf = NULL, *rx_lconf = NULL, *tx_lconf = NULL;
156         struct task_args *targ, *rx_targ = NULL, *tx_targ = NULL;
157         uint8_t port_id, rx_port_id, ok;
158
159         while (core_targ_next(&lconf, &targ, 0) == 0) {
160                 PROX_PANIC((targ->flags & TASK_ARG_RX_RING) && targ->rx_rings[0] == 0 && !targ->tx_opt_ring_task,
161                            "Configuration Error - Core %u task %u Receiving from ring, but nobody xmitting to this ring\n", lconf->id, targ->id);
162                 if (targ->nb_rxports == 0 && targ->nb_rxrings == 0) {
163                         PROX_PANIC(!task_init_flag_set(targ->task_init, TASK_FEATURE_NO_RX),
164                                    "\tCore %u task %u: no rx_ports and no rx_rings configured while required by mode %s\n", lconf->id, targ->id, targ->task_init->mode_str);
165                 }
166         }
167
168         lconf = NULL;
169         while (core_targ_next(&lconf, &targ, 0) == 0) {
170                 if (strcmp(targ->sub_mode_str, "l3") != 0)
171                         continue;
172
173                 PROX_PANIC((targ->nb_rxports == 0) && (targ->nb_txports == 0), "L3 task must have a RX or a TX port\n");
174                 // If the L3 sub_mode receives from a port, check that there is at least one core/task
175                 // transmitting to this port in L3 sub_mode
176                 for (uint8_t i = 0; i < targ->nb_rxports; ++i) {
177                         rx_port_id = targ->rx_port_queue[i].port;
178                         ok = 0;
179                         tx_lconf = NULL;
180                         while (core_targ_next(&tx_lconf, &tx_targ, 0) == 0) {
181                                 if ((port_id = tx_targ->tx_port_queue[0].port) == OUT_DISCARD)
182                                         continue;
183                                 if ((rx_port_id == port_id) && (tx_targ->flags & TASK_ARG_L3)){
184                                         ok = 1;
185                                         break;
186                                 }
187                         }
188                         PROX_PANIC(ok == 0, "RX L3 sub mode for port %d on core %d task %d, but no core/task transmitting on that port\n", rx_port_id, lconf->id, targ->id);
189                 }
190
191                 // If the L3 sub_mode transmits to a port, check that there is at least one core/task
192                 // receiving from that port in L3 sub_mode.
193                 if ((port_id = targ->tx_port_queue[0].port) == OUT_DISCARD)
194                         continue;
195                 rx_lconf = NULL;
196                 ok = 0;
197                 plog_info("\tCore %d task %d transmitting to port %d in L3 mode\n", lconf->id, targ->id, port_id);
198                 while (core_targ_next(&rx_lconf, &rx_targ, 0) == 0) {
199                         for (uint8_t i = 0; i < rx_targ->nb_rxports; ++i) {
200                                 rx_port_id = rx_targ->rx_port_queue[i].port;
201                                 if ((rx_port_id == port_id) && (rx_targ->flags & TASK_ARG_L3)){
202                                         ok = 1;
203                                         break;
204                                 }
205                         }
206                         if (ok == 1) {
207                                 plog_info("\tCore %d task %d has found core %d task %d receiving from port %d\n", lconf->id, targ->id, rx_lconf->id, rx_targ->id, port_id);
208                                 break;
209                         }
210                 }
211                 PROX_PANIC(ok == 0, "L3 sub mode for port %d on core %d task %d, but no core/task receiving on that port\n", port_id, lconf->id, targ->id);
212         }
213 }
214
215 static void check_cfg_consistent(void)
216 {
217         check_nb_mbuf();
218         check_missing_rx();
219         check_zero_rx();
220         check_mixed_normal_pipeline();
221 }
222
223 static void plog_all_rings(void)
224 {
225         struct lcore_cfg *lconf = NULL;
226         struct task_args *targ;
227
228         while (core_targ_next(&lconf, &targ, 0) == 0) {
229                 for (uint8_t ring_idx = 0; ring_idx < targ->nb_rxrings; ++ring_idx) {
230                         plog_info("\tCore %u, task %u, rx_ring[%u] %p\n", lconf->id, targ->id, ring_idx, targ->rx_rings[ring_idx]);
231                 }
232         }
233 }
234
235 static int chain_flag_state(struct task_args *targ, uint64_t flag, int is_set)
236 {
237         if (task_init_flag_set(targ->task_init, flag) == is_set)
238                 return 1;
239
240         int ret = 0;
241
242         for (uint32_t i = 0; i < targ->n_prev_tasks; ++i) {
243                 ret = chain_flag_state(targ->prev_tasks[i], flag, is_set);
244                 if (ret)
245                         return 1;
246         }
247         return 0;
248 }
249
250 static int chain_flag_always_set(struct task_args *targ, uint64_t flag)
251 {
252         return (!chain_flag_state(targ, flag, 0));
253 }
254
255 static int chain_flag_never_set(struct task_args *targ, uint64_t flag)
256 {
257         return (!chain_flag_state(targ, flag, 1));
258 }
259
260 static int chain_flag_sometimes_set(struct task_args *targ, uint64_t flag)
261 {
262         return (chain_flag_state(targ, flag, 1));
263 }
264
265 static void configure_if_tx_queues(struct task_args *targ, uint8_t socket)
266 {
267         uint8_t if_port;
268
269         for (uint8_t i = 0; i < targ->nb_txports; ++i) {
270                 if_port = targ->tx_port_queue[i].port;
271
272                 PROX_PANIC(if_port == OUT_DISCARD, "port misconfigured, exiting\n");
273
274                 PROX_PANIC(!prox_port_cfg[if_port].active, "\tPort %u not used, skipping...\n", if_port);
275
276                 int dsocket = prox_port_cfg[if_port].socket;
277                 if (dsocket != -1 && dsocket != socket) {
278                         plog_warn("TX core on socket %d while device on socket %d\n", socket, dsocket);
279                 }
280
281                 if (prox_port_cfg[if_port].tx_ring[0] == '\0') {  // Rings-backed port can use single queue
282                         targ->tx_port_queue[i].queue = prox_port_cfg[if_port].n_txq;
283                         prox_port_cfg[if_port].n_txq++;
284                 } else {
285                         prox_port_cfg[if_port].n_txq = 1;
286                         targ->tx_port_queue[i].queue = 0;
287                 }
288                 /* By default OFFLOAD is enabled, but if the whole
289                    chain has NOOFFLOADS set all the way until the
290                    first task that receives from a port, it will be
291                    disabled for the destination port. */
292 #if RTE_VERSION < RTE_VERSION_NUM(18,8,0,1)
293                 if (chain_flag_always_set(targ, TASK_FEATURE_TXQ_FLAGS_NOOFFLOADS)) {
294                         prox_port_cfg[if_port].tx_conf.txq_flags |= ETH_TXQ_FLAGS_NOOFFLOADS;
295                 }
296 #else
297                 if (chain_flag_always_set(targ, TASK_FEATURE_TXQ_FLAGS_NOOFFLOADS)) {
298                         prox_port_cfg[if_port].requested_tx_offload &= ~(DEV_TX_OFFLOAD_IPV4_CKSUM | DEV_TX_OFFLOAD_UDP_CKSUM);
299                 }
300 #endif
301         }
302 }
303
304 static void configure_if_rx_queues(struct task_args *targ, uint8_t socket)
305 {
306         struct prox_port_cfg *port;
307         for (int i = 0; i < targ->nb_rxports; i++) {
308                 uint8_t if_port = targ->rx_port_queue[i].port;
309
310                 if (if_port == OUT_DISCARD) {
311                         return;
312                 }
313
314                 port = &prox_port_cfg[if_port];
315                 PROX_PANIC(!port->active, "Port %u not used, aborting...\n", if_port);
316
317                 if(port->rx_ring[0] != '\0') {
318                         port->n_rxq = 0;
319                 }
320
321                 // If the mbuf size (of the rx task) is not big enough, we might receive multiple segments
322                 // This is usually the case when setting a big mtu size i.e. enabling jumbo frames.
323                 // If the packets get transmitted, then multi segments will have to be enabled on the TX port
324                 uint16_t max_frame_size = port->mtu + ETHER_HDR_LEN + ETHER_CRC_LEN + 2 * PROX_VLAN_TAG_SIZE;
325                 if (max_frame_size + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM > targ->mbuf_size) {
326                         targ->task_init->flag_features |= TASK_FEATURE_TXQ_FLAGS_MULTSEGS;
327                 }
328                 targ->rx_port_queue[i].queue = port->n_rxq;
329                 port->pool[targ->rx_port_queue[i].queue] = targ->pool;
330                 port->pool_size[targ->rx_port_queue[i].queue] = targ->nb_mbuf - 1;
331                 port->n_rxq++;
332
333                 int dsocket = port->socket;
334                 if (dsocket != -1 && dsocket != socket) {
335                         plog_warn("RX core on socket %d while device on socket %d\n", socket, dsocket);
336                 }
337         }
338 }
339
340 static void configure_if_queues(void)
341 {
342         struct lcore_cfg *lconf = NULL;
343         struct task_args *targ;
344         uint8_t socket;
345
346         while (core_targ_next(&lconf, &targ, 0) == 0) {
347                 socket = rte_lcore_to_socket_id(lconf->id);
348
349                 configure_if_rx_queues(targ, socket);
350                 configure_if_tx_queues(targ, socket);
351         }
352 }
353
354 static void configure_tx_queue_flags(void)
355 {
356         struct lcore_cfg *lconf = NULL;
357         struct task_args *targ;
358         uint8_t socket;
359         uint8_t if_port;
360
361         while (core_targ_next(&lconf, &targ, 0) == 0) {
362                 socket = rte_lcore_to_socket_id(lconf->id);
363                 for (uint8_t i = 0; i < targ->nb_txports; ++i) {
364                         if_port = targ->tx_port_queue[i].port;
365 #if RTE_VERSION < RTE_VERSION_NUM(18,8,0,1)
366                         /* Set the ETH_TXQ_FLAGS_NOREFCOUNT flag if none of
367                         the tasks up to the task transmitting to the port
368                         use refcnt. */
369                         if (chain_flag_never_set(targ, TASK_FEATURE_TXQ_FLAGS_REFCOUNT)) {
370                                 prox_port_cfg[if_port].tx_conf.txq_flags |= ETH_TXQ_FLAGS_NOREFCOUNT;
371                         }
372 #else
373                         /* Set the DEV_TX_OFFLOAD_MBUF_FAST_FREE flag if none of
374                         the tasks up to the task transmitting to the port
375                         use refcnt and per-queue all mbufs comes from the same mempool. */
376                         if (chain_flag_never_set(targ, TASK_FEATURE_TXQ_FLAGS_REFCOUNT)) {
377                                 if (chain_flag_never_set(targ, TASK_FEATURE_TXQ_FLAGS_MULTIPLE_MEMPOOL))
378                                         prox_port_cfg[if_port].requested_tx_offload |= DEV_TX_OFFLOAD_MBUF_FAST_FREE;
379                         }
380 #endif
381                 }
382         }
383 }
384
385 static void configure_multi_segments(void)
386 {
387         struct lcore_cfg *lconf = NULL;
388         struct task_args *targ;
389         uint8_t if_port;
390
391         while (core_targ_next(&lconf, &targ, 0) == 0) {
392                 for (uint8_t i = 0; i < targ->nb_txports; ++i) {
393                         if_port = targ->tx_port_queue[i].port;
394                         // Multi segment is disabled for most tasks. It is only enabled for tasks requiring big packets.
395 #if RTE_VERSION < RTE_VERSION_NUM(18,8,0,1)
396                         // We can only enable "no multi segment" if no such task exists in the chain of tasks.
397                         if (chain_flag_never_set(targ, TASK_FEATURE_TXQ_FLAGS_MULTSEGS)) {
398                                 prox_port_cfg[if_port].tx_conf.txq_flags |= ETH_TXQ_FLAGS_NOMULTSEGS;
399                         }
400 #else
401                         // We enable "multi segment" if at least one task requires it in the chain of tasks.
402                         if (chain_flag_sometimes_set(targ, TASK_FEATURE_TXQ_FLAGS_MULTSEGS)) {
403                                 prox_port_cfg[if_port].requested_tx_offload |= DEV_TX_OFFLOAD_MULTI_SEGS;
404                         }
405 #endif
406                 }
407         }
408 }
409
410 static const char *gen_ring_name(void)
411 {
412         static char retval[] = "XX";
413         static const char* ring_names =
414                 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
415                 "abcdefghijklmnopqrstuvwxyz"
416                 "[\\]^_`!\"#$%&'()*+,-./:;<="
417                 ">?@{|}0123456789";
418         static int idx2 = 0;
419
420         int idx = idx2;
421
422         retval[0] = ring_names[idx % strlen(ring_names)];
423         idx /= strlen(ring_names);
424         retval[1] = idx ? ring_names[(idx - 1) % strlen(ring_names)] : 0;
425
426         idx2++;
427
428         return retval;
429 }
430
431 struct ring_init_stats {
432         uint32_t n_pkt_rings;
433         uint32_t n_ctrl_rings;
434         uint32_t n_opt_rings;
435 };
436
437 static uint32_t ring_init_stats_total(const struct ring_init_stats *ris)
438 {
439         return ris->n_pkt_rings + ris->n_ctrl_rings + ris->n_opt_rings;
440 }
441
442 static uint32_t count_incoming_tasks(uint32_t lcore_worker, uint32_t dest_task)
443 {
444         struct lcore_cfg *lconf = NULL;
445         struct task_args *targ;
446         uint32_t ret = 0;
447         struct core_task ct;
448
449         while (core_targ_next(&lconf, &targ, 0) == 0) {
450                 for (uint8_t idxx = 0; idxx < MAX_PROTOCOLS; ++idxx) {
451                         for (uint8_t ridx = 0; ridx < targ->core_task_set[idxx].n_elems; ++ridx) {
452                                 ct = targ->core_task_set[idxx].core_task[ridx];
453
454                                 if (dest_task == ct.task && lcore_worker == ct.core)
455                                         ret++;
456                         }
457                 }
458         }
459         return ret;
460 }
461
462 static struct rte_ring *get_existing_ring(uint32_t lcore_id, uint32_t task_id)
463 {
464         if (!prox_core_active(lcore_id, 0))
465                 return NULL;
466
467         struct lcore_cfg *lconf = &lcore_cfg[lcore_id];
468
469         if (task_id >= lconf->n_tasks_all)
470                 return NULL;
471
472         if (lconf->targs[task_id].nb_rxrings == 0)
473                 return NULL;
474
475         return lconf->targs[task_id].rx_rings[0];
476 }
477
478 static struct rte_ring *init_ring_between_tasks(struct lcore_cfg *lconf, struct task_args *starg,
479                                     const struct core_task ct, uint8_t ring_idx, int idx,
480                                     struct ring_init_stats *ris)
481 {
482         uint8_t socket;
483         struct rte_ring *ring = NULL;
484         struct lcore_cfg *lworker;
485         struct task_args *dtarg;
486
487         PROX_ASSERT(prox_core_active(ct.core, 0));
488         lworker = &lcore_cfg[ct.core];
489
490         /* socket used is the one that the sending core resides on */
491         socket = rte_lcore_to_socket_id(lconf->id);
492
493         plog_info("\t\tCreating ring on socket %u with size %u\n"
494                   "\t\t\tsource core, task and socket = %u, %u, %u\n"
495                   "\t\t\tdestination core, task and socket = %u, %u, %u\n"
496                   "\t\t\tdestination worker id = %u\n",
497                   socket, starg->ring_size,
498                   lconf->id, starg->id, socket,
499                   ct.core, ct.task, rte_lcore_to_socket_id(ct.core),
500                   ring_idx);
501
502         if (ct.type) {
503                 struct rte_ring **dring = NULL;
504
505                 if (ct.type == CTRL_TYPE_MSG)
506                         dring = &lworker->ctrl_rings_m[ct.task];
507                 else if (ct.type == CTRL_TYPE_PKT) {
508                         dring = &lworker->ctrl_rings_p[ct.task];
509                         starg->flags |= TASK_ARG_CTRL_RINGS_P;
510                 }
511
512                 if (*dring == NULL)
513                         ring = rte_ring_create(gen_ring_name(), starg->ring_size, socket, RING_F_SC_DEQ);
514                 else
515                         ring = *dring;
516                 PROX_PANIC(ring == NULL, "Cannot create ring to connect I/O core %u with worker core %u\n", lconf->id, ct.core);
517
518                 starg->tx_rings[starg->tot_n_txrings_inited] = ring;
519                 starg->tot_n_txrings_inited++;
520                 *dring = ring;
521                 if (lconf->id == prox_cfg.master) {
522                         ctrl_rings[ct.core*MAX_TASKS_PER_CORE + ct.task] = ring;
523                 } else if (ct.core == prox_cfg.master) {
524                         starg->ctrl_plane_ring = ring;
525                 }
526
527                 plog_info("\t\t\tCore %u task %u to -> core %u task %u ctrl_ring %s %p %s\n",
528                           lconf->id, starg->id, ct.core, ct.task, ct.type == CTRL_TYPE_PKT?
529                           "pkt" : "msg", ring, ring->name);
530                 ris->n_ctrl_rings++;
531                 return ring;
532         }
533
534         dtarg = &lworker->targs[ct.task];
535         lworker->targs[ct.task].worker_thread_id = ring_idx;
536         PROX_ASSERT(dtarg->flags & TASK_ARG_RX_RING);
537         PROX_ASSERT(ct.task < lworker->n_tasks_all);
538
539         /* If all the following conditions are met, the ring can be
540            optimized away. */
541         if (!task_is_master(starg) && !task_is_master(dtarg) && starg->lconf->id == dtarg->lconf->id &&
542             starg->nb_txrings == 1 && idx == 0 && dtarg->task &&
543             dtarg->tot_rxrings == 1 && starg->task == dtarg->task - 1) {
544                 plog_info("\t\tOptimizing away ring on core %u from task %u to task %u\n",
545                           dtarg->lconf->id, starg->task, dtarg->task);
546                 /* No need to set up ws_mbuf. */
547                 starg->tx_opt_ring = 1;
548                 /* During init of destination task, the buffer in the
549                    source task will be initialized. */
550                 dtarg->tx_opt_ring_task = starg;
551                 ris->n_opt_rings++;
552                 ++dtarg->nb_rxrings;
553                 return NULL;
554         }
555
556         int ring_created = 1;
557         /* Only create multi-producer rings if configured to do so AND
558            there is only one task sending to the task */
559         if ((prox_cfg.flags & DSF_MP_RINGS && count_incoming_tasks(ct.core, ct.task) > 1)
560                 || (prox_cfg.flags & DSF_ENABLE_BYPASS)) {
561                 ring = get_existing_ring(ct.core, ct.task);
562
563                 if (ring) {
564                         plog_info("\t\tCore %u task %u creatign MP ring %p to core %u task %u\n",
565                                   lconf->id, starg->id, ring, ct.core, ct.task);
566                         ring_created = 0;
567                 }
568                 else {
569                         ring = rte_ring_create(gen_ring_name(), starg->ring_size, socket, RING_F_SC_DEQ);
570                         plog_info("\t\tCore %u task %u using MP ring %p from core %u task %u\n",
571                                   lconf->id, starg->id, ring, ct.core, ct.task);
572                 }
573         }
574         else
575                 ring = rte_ring_create(gen_ring_name(), starg->ring_size, socket, RING_F_SP_ENQ | RING_F_SC_DEQ);
576
577         PROX_PANIC(ring == NULL, "Cannot create ring to connect I/O core %u with worker core %u\n", lconf->id, ct.core);
578
579         starg->tx_rings[starg->tot_n_txrings_inited] = ring;
580         starg->tot_n_txrings_inited++;
581
582         if (ring_created) {
583                 PROX_ASSERT(dtarg->nb_rxrings < MAX_RINGS_PER_TASK);
584                 dtarg->rx_rings[dtarg->nb_rxrings] = ring;
585                 ++dtarg->nb_rxrings;
586                 if (dtarg->nb_rxrings > 1)
587                         dtarg->task_init->flag_features |= TASK_FEATURE_TXQ_FLAGS_MULTIPLE_MEMPOOL;
588         }
589         dtarg->nb_slave_threads = starg->core_task_set[idx].n_elems;
590         dtarg->lb_friend_core = lconf->id;
591         dtarg->lb_friend_task = starg->id;
592         plog_info("\t\tWorker thread %d has core %d, task %d as a lb friend\n", ct.core, lconf->id, starg->id);
593         plog_info("\t\tCore %u task %u tx_ring[%u] -> core %u task %u rx_ring[%u] %p %s %u WT\n",
594                   lconf->id, starg->id, ring_idx, ct.core, ct.task, dtarg->nb_rxrings, ring, ring->name,
595                   dtarg->nb_slave_threads);
596         ++ris->n_pkt_rings;
597         return ring;
598 }
599
600 static void init_rings(void)
601 {
602         struct lcore_cfg *lconf = NULL;
603         struct task_args *starg;
604         struct ring_init_stats ris = {0};
605
606         while (core_targ_next(&lconf, &starg, 1) == 0) {
607                 plog_info("\t*** Initializing rings on core %u, task %u ***\n", lconf->id, starg->id);
608                 for (uint8_t idx = 0; idx < MAX_PROTOCOLS; ++idx) {
609                         for (uint8_t ring_idx = 0; ring_idx < starg->core_task_set[idx].n_elems; ++ring_idx) {
610                                 PROX_ASSERT(ring_idx < MAX_WT_PER_LB);
611                                 PROX_ASSERT(starg->tot_n_txrings_inited < MAX_RINGS_PER_TASK);
612
613                                 struct core_task ct = starg->core_task_set[idx].core_task[ring_idx];
614                                 init_ring_between_tasks(lconf, starg, ct, ring_idx, idx, &ris);
615                         }
616                 }
617         }
618
619         plog_info("\tInitialized %d rings:\n"
620                   "\t\tNumber of packet rings: %u\n"
621                   "\t\tNumber of control rings: %u\n"
622                   "\t\tNumber of optimized rings: %u\n",
623                   ring_init_stats_total(&ris),
624                   ris.n_pkt_rings,
625                   ris.n_ctrl_rings,
626                   ris.n_opt_rings);
627
628         lconf = NULL;
629         struct prox_port_cfg *port;
630         while (core_targ_next(&lconf, &starg, 1) == 0) {
631                 if ((starg->task_init) && (starg->flags & TASK_ARG_L3)) {
632                         struct core_task ct;
633                         ct.core = prox_cfg.master;
634                         ct.task = 0;
635                         ct.type = CTRL_TYPE_PKT;
636                         struct rte_ring *rx_ring = init_ring_between_tasks(lconf, starg, ct, 0, 0, &ris);
637
638                         ct.core = lconf->id;
639                         ct.task = starg->id;;
640                         struct rte_ring *tx_ring = init_ring_between_tasks(&lcore_cfg[prox_cfg.master], lcore_cfg[prox_cfg.master].targs, ct, 0, 0, &ris);
641                 }
642         }
643 }
644
645 static void shuffle_mempool(struct rte_mempool* mempool, uint32_t nb_mbuf)
646 {
647         struct rte_mbuf** pkts = prox_zmalloc(nb_mbuf * sizeof(*pkts), rte_socket_id());
648         uint64_t got = 0;
649
650         while ((got < nb_mbuf) && (rte_mempool_get_bulk(mempool, (void**)(pkts + got), 1) == 0))
651                 ++got;
652
653         nb_mbuf = got;
654         while (got) {
655                 int idx;
656                 do {
657                         idx = rand() % nb_mbuf;
658                 } while (pkts[idx] == 0);
659
660                 rte_mempool_put_bulk(mempool, (void**)&pkts[idx], 1);
661                 pkts[idx] = 0;
662                 --got;
663         };
664         prox_free(pkts);
665 }
666
667 static void set_mbuf_size(struct task_args *targ)
668 {
669         /* mbuf size can be set
670          *  - from config file (highest priority, overwriting any other config) - should only be used as workaround
671          *  - defaulted to MBUF_SIZE.
672          * Except if set explicitely, ensure that size is big enough for vmxnet3 driver
673          */
674         if (targ->mbuf_size)
675                 return;
676
677         targ->mbuf_size = MBUF_SIZE;
678         struct prox_port_cfg *port;
679         uint16_t max_frame_size = 0, min_buffer_size = 0;
680         int i40e = 0;
681         for (int i = 0; i < targ->nb_rxports; i++) {
682                 uint8_t if_port = targ->rx_port_queue[i].port;
683
684                 if (if_port == OUT_DISCARD) {
685                         continue;
686                 }
687                 port = &prox_port_cfg[if_port];
688                 if (max_frame_size < port->mtu + ETHER_HDR_LEN + ETHER_CRC_LEN + 2 * PROX_VLAN_TAG_SIZE)
689                         max_frame_size = port->mtu + ETHER_HDR_LEN + ETHER_CRC_LEN + 2 * PROX_VLAN_TAG_SIZE;
690                 if (min_buffer_size < port->min_rx_bufsize)
691                         min_buffer_size = port->min_rx_bufsize;
692
693                 // Check whether we receive from i40e. This driver have extra mbuf size requirements
694                 if (strcmp(port->short_name, "i40e") == 0)
695                         i40e = 1;
696         }
697         if (i40e) {
698                 // i40e supports a maximum of 5 descriptors chained
699                 uint16_t required_mbuf_size = RTE_ALIGN(max_frame_size / 5, 128) + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM;
700                 if (required_mbuf_size > targ->mbuf_size) {
701                         targ->mbuf_size = required_mbuf_size;
702                         plog_info("\t\tSetting mbuf_size to %u to support frame_size %u\n", targ->mbuf_size, max_frame_size);
703                 }
704         }
705         if (min_buffer_size > targ->mbuf_size) {
706                 plog_warn("Mbuf size might be too small. This might result in packet segmentation and memory leak\n");
707         }
708
709 }
710
711 static void setup_mempools_unique_per_socket(void)
712 {
713         uint32_t flags = 0;
714         char name[64];
715         struct lcore_cfg *lconf = NULL;
716         struct task_args *targ;
717
718         struct rte_mempool     *pool[MAX_SOCKETS];
719         uint32_t mbuf_count[MAX_SOCKETS] = {0};
720         uint32_t nb_cache_mbuf[MAX_SOCKETS] = {0};
721         uint32_t mbuf_size[MAX_SOCKETS] = {0};
722
723         while (core_targ_next_early(&lconf, &targ, 0) == 0) {
724                 PROX_PANIC(targ->task_init == NULL, "task_init = NULL, is mode specified for core %d, task %d ?\n", lconf->id, targ->id);
725                 uint8_t socket = rte_lcore_to_socket_id(lconf->id);
726                 PROX_ASSERT(socket < MAX_SOCKETS);
727
728                 set_mbuf_size(targ);
729                 if (targ->rx_port_queue[0].port != OUT_DISCARD) {
730                         struct prox_port_cfg* port_cfg = &prox_port_cfg[targ->rx_port_queue[0].port];
731                         PROX_ASSERT(targ->nb_mbuf != 0);
732                         mbuf_count[socket] += targ->nb_mbuf;
733                         if (nb_cache_mbuf[socket] == 0)
734                                 nb_cache_mbuf[socket] = targ->nb_cache_mbuf;
735                         else {
736                                 PROX_PANIC(nb_cache_mbuf[socket] != targ->nb_cache_mbuf,
737                                            "all mbuf_cache must have the same size if using a unique mempool per socket\n");
738                         }
739                         if (mbuf_size[socket] == 0)
740                                 mbuf_size[socket] = targ->mbuf_size;
741                         else {
742                                 PROX_PANIC(mbuf_size[socket] != targ->mbuf_size,
743                                            "all mbuf_size must have the same size if using a unique mempool per socket\n");
744                         }
745                 }
746         }
747         for (int i = 0 ; i < MAX_SOCKETS; i++) {
748                 if (mbuf_count[i] != 0) {
749                         sprintf(name, "socket_%u_pool", i);
750                         pool[i] = rte_mempool_create(name,
751                                                      mbuf_count[i] - 1, mbuf_size[i],
752                                                      nb_cache_mbuf[i],
753                                                      sizeof(struct rte_pktmbuf_pool_private),
754                                                      rte_pktmbuf_pool_init, NULL,
755                                                      prox_pktmbuf_init, NULL,
756                                                      i, flags);
757                         PROX_PANIC(pool[i] == NULL, "\t\tError: cannot create mempool for socket %u\n", i);
758                         plog_info("\t\tMempool %p size = %u * %u cache %u, socket %d\n", pool[i],
759                                   mbuf_count[i], mbuf_size[i], nb_cache_mbuf[i], i);
760
761                         if (prox_cfg.flags & DSF_SHUFFLE) {
762                                 shuffle_mempool(pool[i], mbuf_count[i]);
763                         }
764                 }
765         }
766
767         lconf = NULL;
768         while (core_targ_next_early(&lconf, &targ, 0) == 0) {
769                 uint8_t socket = rte_lcore_to_socket_id(lconf->id);
770
771                 if (targ->rx_port_queue[0].port != OUT_DISCARD) {
772                         /* use this pool for the interface that the core is receiving from */
773                         /* If one core receives from multiple ports, all the ports use the same mempool */
774                         targ->pool = pool[socket];
775                         /* Set the number of mbuf to the number of the unique mempool, so that the used and free work */
776                         targ->nb_mbuf = mbuf_count[socket];
777                         plog_info("\t\tMempool %p size = %u * %u cache %u, socket %d\n", targ->pool,
778                                   targ->nb_mbuf, mbuf_size[socket], targ->nb_cache_mbuf, socket);
779                 }
780         }
781 }
782
783 static void setup_mempool_for_rx_task(struct lcore_cfg *lconf, struct task_args *targ)
784 {
785         const uint8_t socket = rte_lcore_to_socket_id(lconf->id);
786         struct prox_port_cfg *port_cfg = &prox_port_cfg[targ->rx_port_queue[0].port];
787         const struct rte_memzone *mz;
788         struct rte_mempool *mp = NULL;
789         uint32_t flags = 0;
790         char memzone_name[64];
791         char name[64];
792
793         set_mbuf_size(targ);
794
795         /* allocate memory pool for packets */
796         PROX_ASSERT(targ->nb_mbuf != 0);
797
798         if (targ->pool_name[0] == '\0') {
799                 sprintf(name, "core_%u_port_%u_pool", lconf->id, targ->id);
800         }
801
802         snprintf(memzone_name, sizeof(memzone_name)-1, "MP_%s", targ->pool_name);
803         mz = rte_memzone_lookup(memzone_name);
804
805         if (mz != NULL) {
806                 mp = (struct rte_mempool*)mz->addr;
807
808                 targ->nb_mbuf = mp->size;
809                 targ->pool = mp;
810         }
811
812 #ifdef RTE_LIBRTE_IVSHMEM_FALSE
813         if (mz != NULL && mp != NULL && mp->phys_addr != mz->ioremap_addr) {
814                 /* Init mbufs with ioremap_addr for dma */
815                 mp->phys_addr = mz->ioremap_addr;
816                 mp->elt_pa[0] = mp->phys_addr + (mp->elt_va_start - (uintptr_t)mp);
817
818                 struct prox_pktmbuf_reinit_args init_args;
819                 init_args.mp = mp;
820                 init_args.lconf = lconf;
821
822                 uint32_t elt_sz = mp->elt_size + mp->header_size + mp->trailer_size;
823                 rte_mempool_obj_iter((void*)mp->elt_va_start, mp->size, elt_sz, 1,
824                                      mp->elt_pa, mp->pg_num, mp->pg_shift, prox_pktmbuf_reinit, &init_args);
825         }
826 #endif
827
828         /* Use this pool for the interface that the core is
829            receiving from if one core receives from multiple
830            ports, all the ports use the same mempool */
831         if (targ->pool == NULL) {
832                 plog_info("\t\tCreating mempool with name '%s'\n", name);
833                 targ->pool = rte_mempool_create(name,
834                                                 targ->nb_mbuf - 1, targ->mbuf_size,
835                                                 targ->nb_cache_mbuf,
836                                                 sizeof(struct rte_pktmbuf_pool_private),
837                                                 rte_pktmbuf_pool_init, NULL,
838                                                 prox_pktmbuf_init, lconf,
839                                                 socket, flags);
840         }
841
842         PROX_PANIC(targ->pool == NULL,
843                    "\t\tError: cannot create mempool for core %u port %u: %s\n", lconf->id, targ->id, rte_strerror(rte_errno));
844
845         plog_info("\t\tMempool %p size = %u * %u cache %u, socket %d\n", targ->pool,
846                   targ->nb_mbuf, targ->mbuf_size, targ->nb_cache_mbuf, socket);
847         if (prox_cfg.flags & DSF_SHUFFLE) {
848                 shuffle_mempool(targ->pool, targ->nb_mbuf);
849         }
850 }
851
852 static void setup_mempools_multiple_per_socket(void)
853 {
854         struct lcore_cfg *lconf = NULL;
855         struct task_args *targ;
856
857         while (core_targ_next_early(&lconf, &targ, 0) == 0) {
858                 PROX_PANIC(targ->task_init == NULL, "task_init = NULL, is mode specified for core %d, task %d ?\n", lconf->id, targ->id);
859                 if (targ->rx_port_queue[0].port == OUT_DISCARD)
860                         continue;
861                 setup_mempool_for_rx_task(lconf, targ);
862         }
863 }
864
865 static void setup_mempools(void)
866 {
867         if (prox_cfg.flags & UNIQUE_MEMPOOL_PER_SOCKET)
868                 setup_mempools_unique_per_socket();
869         else
870                 setup_mempools_multiple_per_socket();
871 }
872
873 static void set_task_lconf(void)
874 {
875         struct lcore_cfg *lconf;
876         uint32_t lcore_id = -1;
877
878         while(prox_core_next(&lcore_id, 1) == 0) {
879                 lconf = &lcore_cfg[lcore_id];
880                 for (uint8_t task_id = 0; task_id < lconf->n_tasks_all; ++task_id) {
881                         lconf->targs[task_id].lconf = lconf;
882                 }
883         }
884 }
885
886 static void set_dest_threads(void)
887 {
888         struct lcore_cfg *lconf = NULL;
889         struct task_args *targ;
890
891         while (core_targ_next(&lconf, &targ, 0) == 0) {
892                 for (uint8_t idx = 0; idx < MAX_PROTOCOLS; ++idx) {
893                         for (uint8_t ring_idx = 0; ring_idx < targ->core_task_set[idx].n_elems; ++ring_idx) {
894                                 struct core_task ct = targ->core_task_set[idx].core_task[ring_idx];
895
896                                 struct task_args *dest_task = core_targ_get(ct.core, ct.task);
897                                 dest_task->prev_tasks[dest_task->n_prev_tasks++] = targ;
898                         }
899                 }
900         }
901 }
902
903 static void setup_all_task_structs_early_init(void)
904 {
905         struct lcore_cfg *lconf = NULL;
906         struct task_args *targ;
907
908         plog_info("\t*** Calling early init on all tasks ***\n");
909         while (core_targ_next(&lconf, &targ, 0) == 0) {
910                 if (targ->task_init->early_init) {
911                         targ->task_init->early_init(targ);
912                 }
913         }
914 }
915
916 static void setup_all_task_structs(void)
917 {
918         struct lcore_cfg *lconf;
919         uint32_t lcore_id = -1;
920         struct task_base *tmaster = NULL;
921
922         while(prox_core_next(&lcore_id, 1) == 0) {
923                 lconf = &lcore_cfg[lcore_id];
924                 for (uint8_t task_id = 0; task_id < lconf->n_tasks_all; ++task_id) {
925                         if (task_is_master(&lconf->targs[task_id])) {
926                                 plog_info("\tInitializing MASTER struct for core %d task %d\n", lcore_id, task_id);
927                                 lconf->tasks_all[task_id] = init_task_struct(&lconf->targs[task_id]);
928                                 tmaster = lconf->tasks_all[task_id];
929                         }
930                 }
931         }
932         PROX_PANIC(tmaster == NULL, "Can't initialize master task\n");
933         lcore_id = -1;
934
935         while(prox_core_next(&lcore_id, 1) == 0) {
936                 lconf = &lcore_cfg[lcore_id];
937                 plog_info("\tInitializing struct for core %d with %d task\n", lcore_id, lconf->n_tasks_all);
938                 for (uint8_t task_id = 0; task_id < lconf->n_tasks_all; ++task_id) {
939                         if (!task_is_master(&lconf->targs[task_id])) {
940                                 plog_info("\tInitializing struct for core %d task %d\n", lcore_id, task_id);
941                                 lconf->targs[task_id].tmaster = tmaster;
942                                 lconf->tasks_all[task_id] = init_task_struct(&lconf->targs[task_id]);
943                         }
944                 }
945         }
946 }
947
948 static void init_port_activate(void)
949 {
950         struct lcore_cfg *lconf = NULL;
951         struct task_args *targ;
952         uint8_t port_id = 0;
953
954         while (core_targ_next_early(&lconf, &targ, 0) == 0) {
955                 for (int i = 0; i < targ->nb_rxports; i++) {
956                         port_id = targ->rx_port_queue[i].port;
957                         prox_port_cfg[port_id].active = 1;
958                 }
959
960                 for (int i = 0; i < targ->nb_txports; i++) {
961                         port_id = targ->tx_port_queue[i].port;
962                         prox_port_cfg[port_id].active = 1;
963                 }
964         }
965 }
966
967 /* Initialize cores and allocate mempools */
968 static void init_lcores(void)
969 {
970         struct lcore_cfg *lconf = 0;
971         uint32_t lcore_id = -1;
972
973         while(prox_core_next(&lcore_id, 0) == 0) {
974                 uint8_t socket = rte_lcore_to_socket_id(lcore_id);
975                 PROX_PANIC(socket + 1 > MAX_SOCKETS, "Can't configure core %u (on socket %u). MAX_SOCKET is set to %d\n", lcore_id, socket, MAX_SOCKETS);
976         }
977
978         /* need to allocate mempools as the first thing to use the lowest possible address range */
979         plog_info("=== Initializing mempools ===\n");
980         setup_mempools();
981
982         lcore_cfg_alloc_hp();
983
984         set_dest_threads();
985         set_task_lconf();
986
987         plog_info("=== Initializing port addresses ===\n");
988         init_port_addr();
989
990         plog_info("=== Initializing queue numbers on cores ===\n");
991         configure_if_queues();
992
993         plog_info("=== Initializing rings on cores ===\n");
994         init_rings();
995
996         configure_multi_segments();
997         configure_tx_queue_flags();
998
999         plog_info("=== Checking configuration consistency ===\n");
1000         check_cfg_consistent();
1001
1002         plog_all_rings();
1003 }
1004
1005 static int setup_prox(int argc, char **argv)
1006 {
1007         if (prox_read_config_file() != 0 ||
1008             prox_setup_rte(argv[0]) != 0) {
1009                 return -1;
1010         }
1011
1012         if (prox_cfg.flags & DSF_CHECK_SYNTAX) {
1013                 plog_info("=== Configuration file syntax has been checked ===\n\n");
1014                 exit(EXIT_SUCCESS);
1015         }
1016
1017         init_port_activate();
1018         plog_info("=== Initializing rte devices ===\n");
1019         if (!(prox_cfg.flags & DSF_USE_DUMMY_DEVICES))
1020                 init_rte_ring_dev();
1021         init_rte_dev(prox_cfg.flags & DSF_USE_DUMMY_DEVICES);
1022         plog_info("=== Calibrating TSC overhead ===\n");
1023         clock_init();
1024         plog_info("\tTSC running at %"PRIu64" Hz\n", rte_get_tsc_hz());
1025
1026         init_lcores();
1027         plog_info("=== Initializing ports ===\n");
1028         init_port_all();
1029
1030         setup_all_task_structs_early_init();
1031         plog_info("=== Initializing tasks ===\n");
1032         setup_all_task_structs();
1033
1034         if (prox_cfg.logbuf_size) {
1035                 prox_cfg.logbuf = prox_zmalloc(prox_cfg.logbuf_size, rte_socket_id());
1036                 PROX_PANIC(prox_cfg.logbuf == NULL, "Failed to allocate memory for logbuf with size = %d\n", prox_cfg.logbuf_size);
1037         }
1038
1039         if (prox_cfg.flags & DSF_CHECK_INIT) {
1040                 plog_info("=== Initialization sequence completed ===\n\n");
1041                 exit(EXIT_SUCCESS);
1042         }
1043
1044         /* Current way that works to disable DPDK logging */
1045         FILE *f = fopen("/dev/null", "r");
1046         rte_openlog_stream(f);
1047         plog_info("=== PROX started ===\n");
1048         return 0;
1049 }
1050
1051 static int success = 0;
1052 static void siguser_handler(int signal)
1053 {
1054         if (signal == SIGUSR1)
1055                 success = 1;
1056         else
1057                 success = 0;
1058 }
1059
1060 static void sigabrt_handler(__attribute__((unused)) int signum)
1061 {
1062         /* restore default disposition for SIGABRT and SIGPIPE */
1063         signal(SIGABRT, SIG_DFL);
1064         signal(SIGPIPE, SIG_DFL);
1065
1066         /* ignore further Ctrl-C */
1067         signal(SIGINT, SIG_IGN);
1068
1069         /* more drastic exit on tedious termination signal */
1070         plog_info("Aborting...\n");
1071         if (lcore_cfg != NULL) {
1072                 uint32_t lcore_id;
1073                 pthread_t thread_id, tid0, tid = pthread_self();
1074                 memset(&tid0, 0, sizeof(tid0));
1075
1076                 /* cancel all threads except current one */
1077                 lcore_id = -1;
1078                 while (prox_core_next(&lcore_id, 1) == 0) {
1079                         thread_id = lcore_cfg[lcore_id].thread_id;
1080                         if (pthread_equal(thread_id, tid0))
1081                                 continue;
1082                         if (pthread_equal(thread_id, tid))
1083                                 continue;
1084                         pthread_cancel(thread_id);
1085                 }
1086
1087                 /* wait for cancelled threads to terminate */
1088                 lcore_id = -1;
1089                 while (prox_core_next(&lcore_id, 1) == 0) {
1090                         thread_id = lcore_cfg[lcore_id].thread_id;
1091                         if (pthread_equal(thread_id, tid0))
1092                                 continue;
1093                         if (pthread_equal(thread_id, tid))
1094                                 continue;
1095                         pthread_join(thread_id, NULL);
1096                 }
1097         }
1098
1099         /* close ncurses */
1100         display_end();
1101
1102         /* close ports on termination signal */
1103         close_ports_atexit();
1104
1105         /* terminate now */
1106         abort();
1107 }
1108
1109 static void sigterm_handler(int signum)
1110 {
1111         /* abort on second Ctrl-C */
1112         if (signum == SIGINT)
1113                 signal(SIGINT, sigabrt_handler);
1114
1115         /* gracefully quit on harmless termination signal */
1116         /* ports will subsequently get closed at resulting exit */
1117         quit();
1118 }
1119
1120 int main(int argc, char **argv)
1121 {
1122         /* set en_US locale to print big numbers with ',' */
1123         setlocale(LC_NUMERIC, "en_US.utf-8");
1124
1125         if (prox_parse_args(argc, argv) != 0){
1126                 prox_usage(argv[0]);
1127         }
1128
1129         plog_init(prox_cfg.log_name, prox_cfg.log_name_pid);
1130         plog_info("=== " PROGRAM_NAME " %s ===\n", VERSION_STR());
1131         plog_info("\tUsing DPDK %s\n", rte_version() + sizeof(RTE_VER_PREFIX));
1132         read_rdt_info();
1133
1134         if (prox_cfg.flags & DSF_LIST_TASK_MODES) {
1135                 /* list supported task modes and exit */
1136                 tasks_list();
1137                 return EXIT_SUCCESS;
1138         }
1139
1140         /* close ports at normal exit */
1141         atexit(close_ports_atexit);
1142         /* gracefully quit on harmless termination signals */
1143         signal(SIGHUP, sigterm_handler);
1144         signal(SIGINT, sigterm_handler);
1145         signal(SIGQUIT, sigterm_handler);
1146         signal(SIGTERM, sigterm_handler);
1147         signal(SIGUSR1, sigterm_handler);
1148         signal(SIGUSR2, sigterm_handler);
1149         /* more drastic exit on tedious termination signals */
1150         signal(SIGABRT, sigabrt_handler);
1151         signal(SIGPIPE, sigabrt_handler);
1152
1153         if (prox_cfg.flags & DSF_DAEMON) {
1154                 signal(SIGUSR1, siguser_handler);
1155                 signal(SIGUSR2, siguser_handler);
1156                 plog_info("=== Running in Daemon mode ===\n");
1157                 plog_info("\tForking child and waiting for setup completion\n");
1158
1159                 pid_t ppid = getpid();
1160                 pid_t pid = fork();
1161                 if (pid < 0) {
1162                         plog_err("Failed to fork process to run in daemon mode\n");
1163                         return EXIT_FAILURE;
1164                 }
1165
1166                 if (pid == 0) {
1167                         fclose(stdin);
1168                         fclose(stdout);
1169                         fclose(stderr);
1170                         if (setsid() < 0) {
1171                                 kill(ppid, SIGUSR2);
1172                                 return EXIT_FAILURE;
1173                         }
1174                         if (setup_prox(argc, argv) != 0) {
1175                                 kill(ppid, SIGUSR2);
1176                                 return EXIT_FAILURE;
1177                         }
1178                         else {
1179                                 kill(ppid, SIGUSR1);
1180                                 run(prox_cfg.flags);
1181                                 return EXIT_SUCCESS;
1182                         }
1183                 }
1184                 else {
1185                         /* Before exiting the parent, wait until the
1186                            child process has finished setting up */
1187                         pause();
1188                         if (prox_cfg.logbuf) {
1189                                 file_print(prox_cfg.logbuf);
1190                         }
1191                         return success? EXIT_SUCCESS : EXIT_FAILURE;
1192                 }
1193         }
1194
1195         if (setup_prox(argc, argv) != 0)
1196                 return EXIT_FAILURE;
1197         run(prox_cfg.flags);
1198         return EXIT_SUCCESS;
1199 }