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.
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>
28 #include "prox_malloc.h"
36 #include "prox_args.h"
37 #include "prox_assert.h"
39 #include "prox_shared.h"
40 #include "prox_port_cfg.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"
49 #include "handle_master.h"
51 #if RTE_VERSION < RTE_VERSION_NUM(1,8,0,0)
52 #define RTE_CACHE_LINE_SIZE CACHE_LINE_SIZE
55 uint8_t lb_nb_txrings = 0xff;
56 struct rte_ring *ctrl_rings[RTE_MAX_LCORE*MAX_TASKS_PER_CORE];
58 static void __attribute__((noreturn)) prox_usage(const char *prgname)
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"
86 static void check_mixed_normal_pipeline(void)
88 struct lcore_cfg *lconf = NULL;
89 uint32_t lcore_id = -1;
91 while (prox_core_next(&lcore_id, 0) == 0) {
92 lconf = &lcore_cfg[lcore_id];
94 int all_thread_nop = 1;
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;
104 pipeline = pipeline || targ->task_init->thread_x == thread_pipeline;
105 generic = generic || targ->task_init->thread_x == thread_generic || l3;
107 PROX_PANIC(generic && pipeline, "Can't run both pipeline and normal thread on same core\n");
110 lconf->thread_x = thread_nop;
112 lconf->thread_x = thread_generic;
117 static void check_zero_rx(void)
119 struct lcore_cfg *lconf = NULL;
120 struct task_args *targ;
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);
130 static void check_missing_rx(void)
132 struct lcore_cfg *lconf = NULL, *rx_lconf = NULL, *tx_lconf = NULL;
133 struct task_args *targ, *rx_targ = NULL, *tx_targ = NULL;
134 struct prox_port_cfg *port;
135 uint8_t port_id, rx_port_id, ok;
137 while (core_targ_next(&lconf, &targ, 0) == 0) {
138 PROX_PANIC((targ->flags & TASK_ARG_RX_RING) && targ->rx_rings[0] == 0 && !targ->tx_opt_ring_task,
139 "Configuration Error - Core %u task %u Receiving from ring, but nobody xmitting to this ring\n", lconf->id, targ->id);
140 if (targ->nb_rxports == 0 && targ->nb_rxrings == 0) {
141 PROX_PANIC(!task_init_flag_set(targ->task_init, TASK_FEATURE_NO_RX),
142 "\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);
147 while (core_targ_next(&lconf, &targ, 0) == 0) {
148 if (strcmp(targ->sub_mode_str, "l3") != 0)
151 PROX_PANIC((targ->nb_rxports == 0) && (targ->nb_txports == 0), "L3 task must have a RX or a TX port\n");
152 // If the L3 sub_mode receives from a port, check that there is at least one core/task
153 // transmitting to this port in L3 sub_mode
154 for (uint8_t i = 0; i < targ->nb_rxports; ++i) {
155 rx_port_id = targ->rx_port_queue[i].port;
158 while (core_targ_next(&tx_lconf, &tx_targ, 0) == 0) {
159 if ((port_id = tx_targ->tx_port_queue[0].port) == OUT_DISCARD)
161 if ((rx_port_id == port_id) && (tx_targ->flags & TASK_ARG_L3)){
166 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);
169 // If the L3 sub_mode transmits to a port, check that there is at least one core/task
170 // receiving from that port in L3 sub_mode.
171 if ((port_id = targ->tx_port_queue[0].port) == OUT_DISCARD)
175 plog_info("\tCore %d task %d transmitting to port %d in L3 mode\n", lconf->id, targ->id, port_id);
176 while (core_targ_next(&rx_lconf, &rx_targ, 0) == 0) {
177 for (uint8_t i = 0; i < rx_targ->nb_rxports; ++i) {
178 rx_port_id = rx_targ->rx_port_queue[i].port;
179 if ((rx_port_id == port_id) && (rx_targ->flags & TASK_ARG_L3)){
185 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);
189 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);
193 static void check_cfg_consistent(void)
197 check_mixed_normal_pipeline();
200 static void plog_all_rings(void)
202 struct lcore_cfg *lconf = NULL;
203 struct task_args *targ;
205 while (core_targ_next(&lconf, &targ, 0) == 0) {
206 for (uint8_t ring_idx = 0; ring_idx < targ->nb_rxrings; ++ring_idx) {
207 plog_info("\tCore %u, task %u, rx_ring[%u] %p\n", lconf->id, targ->id, ring_idx, targ->rx_rings[ring_idx]);
212 static int chain_flag_state(struct task_args *targ, uint64_t flag, int is_set)
214 if (task_init_flag_set(targ->task_init, flag) == is_set)
219 for (uint32_t i = 0; i < targ->n_prev_tasks; ++i) {
220 ret = chain_flag_state(targ->prev_tasks[i], flag, is_set);
227 static void configure_if_tx_queues(struct task_args *targ, uint8_t socket)
231 for (uint8_t i = 0; i < targ->nb_txports; ++i) {
232 if_port = targ->tx_port_queue[i].port;
234 PROX_PANIC(if_port == OUT_DISCARD, "port misconfigured, exiting\n");
236 PROX_PANIC(!prox_port_cfg[if_port].active, "\tPort %u not used, skipping...\n", if_port);
238 int dsocket = prox_port_cfg[if_port].socket;
239 if (dsocket != -1 && dsocket != socket) {
240 plog_warn("TX core on socket %d while device on socket %d\n", socket, dsocket);
243 if (prox_port_cfg[if_port].tx_ring[0] == '\0') { // Rings-backed port can use single queue
244 targ->tx_port_queue[i].queue = prox_port_cfg[if_port].n_txq;
245 prox_port_cfg[if_port].n_txq++;
247 prox_port_cfg[if_port].n_txq = 1;
248 targ->tx_port_queue[i].queue = 0;
250 /* Set the ETH_TXQ_FLAGS_NOREFCOUNT flag if none of
251 the tasks up to the task transmitting to the port
253 if (!chain_flag_state(targ, TASK_FEATURE_TXQ_FLAGS_REFCOUNT, 1)) {
254 prox_port_cfg[if_port].tx_conf.txq_flags |= ETH_TXQ_FLAGS_NOREFCOUNT;
255 plog_info("\t\tEnabling No refcnt on port %d\n", if_port);
258 plog_info("\t\tRefcnt used on port %d\n", if_port);
261 /* By default OFFLOAD is enabled, but if the whole
262 chain has NOOFFLOADS set all the way until the
263 first task that receives from a port, it will be
264 disabled for the destination port. */
265 if (!chain_flag_state(targ, TASK_FEATURE_TXQ_FLAGS_NOOFFLOADS, 0)) {
266 prox_port_cfg[if_port].tx_conf.txq_flags |= ETH_TXQ_FLAGS_NOOFFLOADS;
267 plog_info("\t\tDisabling TX offloads on port %d\n", if_port);
269 plog_info("\t\tEnabling TX offloads on port %d\n", if_port);
272 /* By default NOMULTSEGS is disabled, as drivers/NIC might split packets on RX
273 It should only be enabled when we know for sure that the RX does not split packets.
274 Set the ETH_TXQ_FLAGS_NOMULTSEGS flag if all of the tasks up to the task
275 transmitting to the port use no_multsegs. */
276 if (!chain_flag_state(targ, TASK_FEATURE_TXQ_FLAGS_NOMULTSEGS, 0)) {
277 prox_port_cfg[if_port].tx_conf.txq_flags |= ETH_TXQ_FLAGS_NOMULTSEGS;
278 plog_info("\t\tEnabling No MultiSegs on port %d\n", if_port);
281 plog_info("\t\tMultiSegs used on port %d\n", if_port);
286 static void configure_if_rx_queues(struct task_args *targ, uint8_t socket)
288 struct prox_port_cfg *port;
289 for (int i = 0; i < targ->nb_rxports; i++) {
290 uint8_t if_port = targ->rx_port_queue[i].port;
292 if (if_port == OUT_DISCARD) {
296 PROX_PANIC(!prox_port_cfg[if_port].active, "Port %u not used, aborting...\n", if_port);
297 port = &prox_port_cfg[if_port];
299 if(port->rx_ring[0] != '\0') {
303 // Force multi segment support if mbuf size is not big enough.
304 // This is usually the case when setting a big mtu size i.e. enabling jumbo frames.
305 uint16_t max_frame_size = port->mtu + ETHER_HDR_LEN + ETHER_CRC_LEN + 2 * PROX_VLAN_TAG_SIZE;
306 if (max_frame_size + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM > targ->mbuf_size) {
307 targ->task_init->flag_features &= ~TASK_FEATURE_TXQ_FLAGS_NOMULTSEGS;
308 plog_info("\t\tDisabling No MultSegs on port %u as %lu > %u\n", if_port, max_frame_size + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM, targ->mbuf_size);
310 targ->rx_port_queue[i].queue = prox_port_cfg[if_port].n_rxq;
311 port->pool[targ->rx_port_queue[i].queue] = targ->pool;
312 port->pool_size[targ->rx_port_queue[i].queue] = targ->nb_mbuf - 1;
315 int dsocket = prox_port_cfg[if_port].socket;
316 if (dsocket != -1 && dsocket != socket) {
317 plog_warn("RX core on socket %d while device on socket %d\n", socket, dsocket);
322 static void configure_if_queues(void)
324 struct lcore_cfg *lconf = NULL;
325 struct task_args *targ;
328 while (core_targ_next(&lconf, &targ, 0) == 0) {
329 socket = rte_lcore_to_socket_id(lconf->id);
331 configure_if_rx_queues(targ, socket);
332 configure_if_tx_queues(targ, socket);
336 static const char *gen_ring_name(void)
338 static char retval[] = "XX";
339 static const char* ring_names =
340 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
341 "abcdefghijklmnopqrstuvwxyz"
342 "[\\]^_`!\"#$%&'()*+,-./:;<="
348 retval[0] = ring_names[idx % strlen(ring_names)];
349 idx /= strlen(ring_names);
350 retval[1] = idx ? ring_names[(idx - 1) % strlen(ring_names)] : 0;
357 struct ring_init_stats {
358 uint32_t n_pkt_rings;
359 uint32_t n_ctrl_rings;
360 uint32_t n_opt_rings;
363 static uint32_t ring_init_stats_total(const struct ring_init_stats *ris)
365 return ris->n_pkt_rings + ris->n_ctrl_rings + ris->n_opt_rings;
368 static uint32_t count_incoming_tasks(uint32_t lcore_worker, uint32_t dest_task)
370 struct lcore_cfg *lconf = NULL;
371 struct task_args *targ;
375 while (core_targ_next(&lconf, &targ, 0) == 0) {
376 for (uint8_t idxx = 0; idxx < MAX_PROTOCOLS; ++idxx) {
377 for (uint8_t ridx = 0; ridx < targ->core_task_set[idxx].n_elems; ++ridx) {
378 ct = targ->core_task_set[idxx].core_task[ridx];
380 if (dest_task == ct.task && lcore_worker == ct.core)
388 static struct rte_ring *get_existing_ring(uint32_t lcore_id, uint32_t task_id)
390 if (!prox_core_active(lcore_id, 0))
393 struct lcore_cfg *lconf = &lcore_cfg[lcore_id];
395 if (task_id >= lconf->n_tasks_all)
398 if (lconf->targs[task_id].nb_rxrings == 0)
401 return lconf->targs[task_id].rx_rings[0];
404 static struct rte_ring *init_ring_between_tasks(struct lcore_cfg *lconf, struct task_args *starg,
405 const struct core_task ct, uint8_t ring_idx, int idx,
406 struct ring_init_stats *ris)
409 struct rte_ring *ring = NULL;
410 struct lcore_cfg *lworker;
411 struct task_args *dtarg;
413 PROX_ASSERT(prox_core_active(ct.core, 0));
414 lworker = &lcore_cfg[ct.core];
416 /* socket used is the one that the sending core resides on */
417 socket = rte_lcore_to_socket_id(lconf->id);
419 plog_info("\t\tCreating ring on socket %u with size %u\n"
420 "\t\t\tsource core, task and socket = %u, %u, %u\n"
421 "\t\t\tdestination core, task and socket = %u, %u, %u\n"
422 "\t\t\tdestination worker id = %u\n",
423 socket, starg->ring_size,
424 lconf->id, starg->id, socket,
425 ct.core, ct.task, rte_lcore_to_socket_id(ct.core),
429 struct rte_ring **dring = NULL;
431 if (ct.type == CTRL_TYPE_MSG)
432 dring = &lworker->ctrl_rings_m[ct.task];
433 else if (ct.type == CTRL_TYPE_PKT) {
434 dring = &lworker->ctrl_rings_p[ct.task];
435 starg->flags |= TASK_ARG_CTRL_RINGS_P;
439 ring = rte_ring_create(gen_ring_name(), starg->ring_size, socket, RING_F_SC_DEQ);
442 PROX_PANIC(ring == NULL, "Cannot create ring to connect I/O core %u with worker core %u\n", lconf->id, ct.core);
444 starg->tx_rings[starg->tot_n_txrings_inited] = ring;
445 starg->tot_n_txrings_inited++;
447 if (lconf->id == prox_cfg.master) {
448 ctrl_rings[ct.core*MAX_TASKS_PER_CORE + ct.task] = ring;
449 } else if (ct.core == prox_cfg.master) {
450 starg->ctrl_plane_ring = ring;
453 plog_info("\t\tCore %u task %u to -> core %u task %u ctrl_ring %s %p %s\n",
454 lconf->id, starg->id, ct.core, ct.task, ct.type == CTRL_TYPE_PKT?
455 "pkt" : "msg", ring, ring->name);
460 dtarg = &lworker->targs[ct.task];
461 lworker->targs[ct.task].worker_thread_id = ring_idx;
462 PROX_ASSERT(dtarg->flags & TASK_ARG_RX_RING);
463 PROX_ASSERT(ct.task < lworker->n_tasks_all);
465 /* If all the following conditions are met, the ring can be
467 if (!task_is_master(starg) && !task_is_master(dtarg) && starg->lconf->id == dtarg->lconf->id &&
468 starg->nb_txrings == 1 && idx == 0 && dtarg->task &&
469 dtarg->tot_rxrings == 1 && starg->task == dtarg->task - 1) {
470 plog_info("\t\tOptimizing away ring on core %u from task %u to task %u\n",
471 dtarg->lconf->id, starg->task, dtarg->task);
472 /* No need to set up ws_mbuf. */
473 starg->tx_opt_ring = 1;
474 /* During init of destination task, the buffer in the
475 source task will be initialized. */
476 dtarg->tx_opt_ring_task = starg;
482 int ring_created = 1;
483 /* Only create multi-producer rings if configured to do so AND
484 there is only one task sending to the task */
485 if ((prox_cfg.flags & DSF_MP_RINGS && count_incoming_tasks(ct.core, ct.task) > 1)
486 || (prox_cfg.flags & DSF_ENABLE_BYPASS)) {
487 ring = get_existing_ring(ct.core, ct.task);
490 plog_info("\t\tCore %u task %u creatign MP ring %p to core %u task %u\n",
491 lconf->id, starg->id, ring, ct.core, ct.task);
495 ring = rte_ring_create(gen_ring_name(), starg->ring_size, socket, RING_F_SC_DEQ);
496 plog_info("\t\tCore %u task %u using MP ring %p from core %u task %u\n",
497 lconf->id, starg->id, ring, ct.core, ct.task);
501 ring = rte_ring_create(gen_ring_name(), starg->ring_size, socket, RING_F_SP_ENQ | RING_F_SC_DEQ);
503 PROX_PANIC(ring == NULL, "Cannot create ring to connect I/O core %u with worker core %u\n", lconf->id, ct.core);
505 starg->tx_rings[starg->tot_n_txrings_inited] = ring;
506 starg->tot_n_txrings_inited++;
509 PROX_ASSERT(dtarg->nb_rxrings < MAX_RINGS_PER_TASK);
510 dtarg->rx_rings[dtarg->nb_rxrings] = ring;
513 dtarg->nb_slave_threads = starg->core_task_set[idx].n_elems;
514 dtarg->lb_friend_core = lconf->id;
515 dtarg->lb_friend_task = starg->id;
516 plog_info("\t\tWorker thread %d has core %d, task %d as a lb friend\n", ct.core, lconf->id, starg->id);
517 plog_info("\t\tCore %u task %u tx_ring[%u] -> core %u task %u rx_ring[%u] %p %s %u WT\n",
518 lconf->id, starg->id, ring_idx, ct.core, ct.task, dtarg->nb_rxrings, ring, ring->name,
519 dtarg->nb_slave_threads);
524 static void init_rings(void)
526 struct lcore_cfg *lconf = NULL;
527 struct task_args *starg;
528 struct ring_init_stats ris = {0};
530 while (core_targ_next(&lconf, &starg, 1) == 0) {
531 plog_info("\t*** Initializing rings on core %u, task %u ***\n", lconf->id, starg->id);
532 for (uint8_t idx = 0; idx < MAX_PROTOCOLS; ++idx) {
533 for (uint8_t ring_idx = 0; ring_idx < starg->core_task_set[idx].n_elems; ++ring_idx) {
534 PROX_ASSERT(ring_idx < MAX_WT_PER_LB);
535 PROX_ASSERT(starg->tot_n_txrings_inited < MAX_RINGS_PER_TASK);
537 struct core_task ct = starg->core_task_set[idx].core_task[ring_idx];
538 init_ring_between_tasks(lconf, starg, ct, ring_idx, idx, &ris);
543 plog_info("\tInitialized %d rings:\n"
544 "\t\tNumber of packet rings: %u\n"
545 "\t\tNumber of control rings: %u\n"
546 "\t\tNumber of optimized rings: %u\n",
547 ring_init_stats_total(&ris),
553 struct prox_port_cfg *port;
554 while (core_targ_next(&lconf, &starg, 1) == 0) {
555 if ((starg->task_init) && (starg->flags & TASK_ARG_L3)) {
557 ct.core = prox_cfg.master;
559 ct.type = CTRL_TYPE_PKT;
560 struct rte_ring *rx_ring = init_ring_between_tasks(lconf, starg, ct, 0, 0, &ris);
563 ct.task = starg->id;;
564 struct rte_ring *tx_ring = init_ring_between_tasks(lcore_cfg, lcore_cfg[prox_cfg.master].targs, ct, 0, 0, &ris);
569 static void shuffle_mempool(struct rte_mempool* mempool, uint32_t nb_mbuf)
571 struct rte_mbuf** pkts = prox_zmalloc(nb_mbuf * sizeof(*pkts), rte_socket_id());
574 while (rte_mempool_get_bulk(mempool, (void**)(pkts + got), 1) == 0)
580 idx = rand() % nb_mbuf - 1;
581 } while (pkts[idx] == 0);
583 rte_mempool_put_bulk(mempool, (void**)&pkts[idx], 1);
590 static void set_mbuf_size(struct task_args *targ)
592 /* mbuf size can be set
593 * - from config file (highest priority, overwriting any other config) - should only be used as workaround
594 * - through each 'mode', overwriting the default mbuf_size
595 * - defaulted to MBUF_SIZE i.e. 1518 Bytes
596 * Except if set explicitely, ensure that size is big enough for vmxnet3 driver
598 if (targ->mbuf_size_set_explicitely)
601 if (targ->task_init->mbuf_size != 0) {
602 /* mbuf_size not set through config file but set through mode */
603 targ->mbuf_size = targ->task_init->mbuf_size;
606 struct prox_port_cfg *port;
607 uint16_t max_frame_size = 0;
608 for (int i = 0; i < targ->nb_rxports; i++) {
609 uint8_t if_port = targ->rx_port_queue[i].port;
611 if (if_port == OUT_DISCARD) {
614 port = &prox_port_cfg[if_port];
615 if (max_frame_size < port->mtu + ETHER_HDR_LEN + ETHER_CRC_LEN + 2 * PROX_VLAN_TAG_SIZE)
616 max_frame_size = port->mtu + ETHER_HDR_LEN + ETHER_CRC_LEN + 2 * PROX_VLAN_TAG_SIZE;
618 if (strcmp(port->short_name, "vmxnet3") == 0) {
619 if (targ->mbuf_size < MBUF_SIZE + RTE_PKTMBUF_HEADROOM)
620 targ->mbuf_size = MBUF_SIZE + RTE_PKTMBUF_HEADROOM;
621 if (targ->mbuf_size < max_frame_size)
622 targ->mbuf_size = max_frame_size + RTE_PKTMBUF_HEADROOM;
625 if (max_frame_size) {
626 // i40e supports a maximum of 5 descriptors chained
627 uint16_t required_mbuf_size = RTE_ALIGN(max_frame_size / 5, 128) + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM;
628 if (required_mbuf_size > targ->mbuf_size) {
629 targ->mbuf_size = required_mbuf_size;
630 plog_info("\t\tSetting mbuf_size to %u to support frame_size %u (mtu %u)\n", targ->mbuf_size, max_frame_size, port->mtu);
636 static void setup_mempools_unique_per_socket(void)
640 struct lcore_cfg *lconf = NULL;
641 struct task_args *targ;
643 struct rte_mempool *pool[MAX_SOCKETS];
644 uint32_t mbuf_count[MAX_SOCKETS] = {0};
645 uint32_t nb_cache_mbuf[MAX_SOCKETS] = {0};
646 uint32_t mbuf_size[MAX_SOCKETS] = {0};
648 while (core_targ_next_early(&lconf, &targ, 0) == 0) {
649 PROX_PANIC(targ->task_init == NULL, "task_init = NULL, is mode specified for core %d, task %d ?\n", lconf->id, targ->id);
650 uint8_t socket = rte_lcore_to_socket_id(lconf->id);
651 PROX_ASSERT(socket < MAX_SOCKETS);
654 if (targ->rx_port_queue[0].port != OUT_DISCARD) {
655 struct prox_port_cfg* port_cfg = &prox_port_cfg[targ->rx_port_queue[0].port];
656 PROX_ASSERT(targ->nb_mbuf != 0);
657 mbuf_count[socket] += targ->nb_mbuf;
658 if (nb_cache_mbuf[socket] == 0)
659 nb_cache_mbuf[socket] = targ->nb_cache_mbuf;
661 PROX_PANIC(nb_cache_mbuf[socket] != targ->nb_cache_mbuf,
662 "all mbuf_cache must have the same size if using a unique mempool per socket\n");
664 if (mbuf_size[socket] == 0)
665 mbuf_size[socket] = targ->mbuf_size;
667 PROX_PANIC(mbuf_size[socket] != targ->mbuf_size,
668 "all mbuf_size must have the same size if using a unique mempool per socket\n");
672 for (int i = 0 ; i < MAX_SOCKETS; i++) {
673 if (mbuf_count[i] != 0) {
674 sprintf(name, "socket_%u_pool", i);
675 pool[i] = rte_mempool_create(name,
676 mbuf_count[i] - 1, mbuf_size[i],
678 sizeof(struct rte_pktmbuf_pool_private),
679 rte_pktmbuf_pool_init, NULL,
680 prox_pktmbuf_init, NULL,
682 PROX_PANIC(pool[i] == NULL, "\t\tError: cannot create mempool for socket %u\n", i);
683 plog_info("\t\tMempool %p size = %u * %u cache %u, socket %d\n", pool[i],
684 mbuf_count[i], mbuf_size[i], nb_cache_mbuf[i], i);
686 if (prox_cfg.flags & DSF_SHUFFLE) {
687 shuffle_mempool(pool[i], mbuf_count[i]);
693 while (core_targ_next_early(&lconf, &targ, 0) == 0) {
694 uint8_t socket = rte_lcore_to_socket_id(lconf->id);
696 if (targ->rx_port_queue[0].port != OUT_DISCARD) {
697 /* use this pool for the interface that the core is receiving from */
698 /* If one core receives from multiple ports, all the ports use the same mempool */
699 targ->pool = pool[socket];
700 /* Set the number of mbuf to the number of the unique mempool, so that the used and free work */
701 targ->nb_mbuf = mbuf_count[socket];
702 plog_info("\t\tMempool %p size = %u * %u cache %u, socket %d\n", targ->pool,
703 targ->nb_mbuf, mbuf_size[socket], targ->nb_cache_mbuf, socket);
708 static void setup_mempool_for_rx_task(struct lcore_cfg *lconf, struct task_args *targ)
710 const uint8_t socket = rte_lcore_to_socket_id(lconf->id);
711 struct prox_port_cfg *port_cfg = &prox_port_cfg[targ->rx_port_queue[0].port];
712 const struct rte_memzone *mz;
713 struct rte_mempool *mp = NULL;
715 char memzone_name[64];
720 /* allocate memory pool for packets */
721 PROX_ASSERT(targ->nb_mbuf != 0);
723 if (targ->pool_name[0] == '\0') {
724 sprintf(name, "core_%u_port_%u_pool", lconf->id, targ->id);
727 snprintf(memzone_name, sizeof(memzone_name)-1, "MP_%s", targ->pool_name);
728 mz = rte_memzone_lookup(memzone_name);
731 mp = (struct rte_mempool*)mz->addr;
733 targ->nb_mbuf = mp->size;
737 #ifdef RTE_LIBRTE_IVSHMEM_FALSE
738 if (mz != NULL && mp != NULL && mp->phys_addr != mz->ioremap_addr) {
739 /* Init mbufs with ioremap_addr for dma */
740 mp->phys_addr = mz->ioremap_addr;
741 mp->elt_pa[0] = mp->phys_addr + (mp->elt_va_start - (uintptr_t)mp);
743 struct prox_pktmbuf_reinit_args init_args;
745 init_args.lconf = lconf;
747 uint32_t elt_sz = mp->elt_size + mp->header_size + mp->trailer_size;
748 rte_mempool_obj_iter((void*)mp->elt_va_start, mp->size, elt_sz, 1,
749 mp->elt_pa, mp->pg_num, mp->pg_shift, prox_pktmbuf_reinit, &init_args);
753 /* Use this pool for the interface that the core is
754 receiving from if one core receives from multiple
755 ports, all the ports use the same mempool */
756 if (targ->pool == NULL) {
757 plog_info("\t\tCreating mempool with name '%s'\n", name);
758 targ->pool = rte_mempool_create(name,
759 targ->nb_mbuf - 1, targ->mbuf_size,
761 sizeof(struct rte_pktmbuf_pool_private),
762 rte_pktmbuf_pool_init, NULL,
763 prox_pktmbuf_init, lconf,
767 PROX_PANIC(targ->pool == NULL,
768 "\t\tError: cannot create mempool for core %u port %u: %s\n", lconf->id, targ->id, rte_strerror(rte_errno));
770 plog_info("\t\tMempool %p size = %u * %u cache %u, socket %d\n", targ->pool,
771 targ->nb_mbuf, targ->mbuf_size, targ->nb_cache_mbuf, socket);
772 if (prox_cfg.flags & DSF_SHUFFLE) {
773 shuffle_mempool(targ->pool, targ->nb_mbuf);
777 static void setup_mempools_multiple_per_socket(void)
779 struct lcore_cfg *lconf = NULL;
780 struct task_args *targ;
782 while (core_targ_next_early(&lconf, &targ, 0) == 0) {
783 PROX_PANIC(targ->task_init == NULL, "task_init = NULL, is mode specified for core %d, task %d ?\n", lconf->id, targ->id);
784 if (targ->rx_port_queue[0].port == OUT_DISCARD)
786 setup_mempool_for_rx_task(lconf, targ);
790 static void setup_mempools(void)
792 if (prox_cfg.flags & UNIQUE_MEMPOOL_PER_SOCKET)
793 setup_mempools_unique_per_socket();
795 setup_mempools_multiple_per_socket();
798 static void set_task_lconf(void)
800 struct lcore_cfg *lconf;
801 uint32_t lcore_id = -1;
803 while(prox_core_next(&lcore_id, 1) == 0) {
804 lconf = &lcore_cfg[lcore_id];
805 for (uint8_t task_id = 0; task_id < lconf->n_tasks_all; ++task_id) {
806 lconf->targs[task_id].lconf = lconf;
811 static void set_dest_threads(void)
813 struct lcore_cfg *lconf = NULL;
814 struct task_args *targ;
816 while (core_targ_next(&lconf, &targ, 0) == 0) {
817 for (uint8_t idx = 0; idx < MAX_PROTOCOLS; ++idx) {
818 for (uint8_t ring_idx = 0; ring_idx < targ->core_task_set[idx].n_elems; ++ring_idx) {
819 struct core_task ct = targ->core_task_set[idx].core_task[ring_idx];
821 struct task_args *dest_task = core_targ_get(ct.core, ct.task);
822 dest_task->prev_tasks[dest_task->n_prev_tasks++] = targ;
828 static void setup_all_task_structs_early_init(void)
830 struct lcore_cfg *lconf = NULL;
831 struct task_args *targ;
833 plog_info("\t*** Calling early init on all tasks ***\n");
834 while (core_targ_next(&lconf, &targ, 0) == 0) {
835 if (targ->task_init->early_init) {
836 targ->task_init->early_init(targ);
841 static void setup_all_task_structs(void)
843 struct lcore_cfg *lconf;
844 uint32_t lcore_id = -1;
845 struct task_base *tmaster = NULL;
847 while(prox_core_next(&lcore_id, 1) == 0) {
848 lconf = &lcore_cfg[lcore_id];
849 for (uint8_t task_id = 0; task_id < lconf->n_tasks_all; ++task_id) {
850 if (task_is_master(&lconf->targs[task_id])) {
851 plog_info("\tInitializing MASTER struct for core %d task %d\n", lcore_id, task_id);
852 lconf->tasks_all[task_id] = init_task_struct(&lconf->targs[task_id]);
853 tmaster = lconf->tasks_all[task_id];
857 PROX_PANIC(tmaster == NULL, "Can't initialize master task\n");
860 while(prox_core_next(&lcore_id, 1) == 0) {
861 lconf = &lcore_cfg[lcore_id];
862 plog_info("\tInitializing struct for core %d with %d task\n", lcore_id, lconf->n_tasks_all);
863 for (uint8_t task_id = 0; task_id < lconf->n_tasks_all; ++task_id) {
864 if (!task_is_master(&lconf->targs[task_id])) {
865 plog_info("\tInitializing struct for core %d task %d\n", lcore_id, task_id);
866 lconf->targs[task_id].tmaster = tmaster;
867 lconf->tasks_all[task_id] = init_task_struct(&lconf->targs[task_id]);
873 static void init_port_activate(void)
875 struct lcore_cfg *lconf = NULL;
876 struct task_args *targ;
879 while (core_targ_next_early(&lconf, &targ, 0) == 0) {
880 for (int i = 0; i < targ->nb_rxports; i++) {
881 port_id = targ->rx_port_queue[i].port;
882 prox_port_cfg[port_id].active = 1;
885 for (int i = 0; i < targ->nb_txports; i++) {
886 port_id = targ->tx_port_queue[i].port;
887 prox_port_cfg[port_id].active = 1;
892 /* Initialize cores and allocate mempools */
893 static void init_lcores(void)
895 struct lcore_cfg *lconf = 0;
896 uint32_t lcore_id = -1;
898 while(prox_core_next(&lcore_id, 0) == 0) {
899 uint8_t socket = rte_lcore_to_socket_id(lcore_id);
900 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);
903 /* need to allocate mempools as the first thing to use the lowest possible address range */
904 plog_info("=== Initializing mempools ===\n");
907 lcore_cfg_alloc_hp();
912 plog_info("=== Initializing port addresses ===\n");
915 plog_info("=== Initializing queue numbers on cores ===\n");
916 configure_if_queues();
918 plog_info("=== Initializing rings on cores ===\n");
921 plog_info("=== Checking configuration consistency ===\n");
922 check_cfg_consistent();
926 setup_all_task_structs_early_init();
927 plog_info("=== Initializing tasks ===\n");
928 setup_all_task_structs();
931 static int setup_prox(int argc, char **argv)
933 if (prox_read_config_file() != 0 ||
934 prox_setup_rte(argv[0]) != 0) {
938 if (prox_cfg.flags & DSF_CHECK_SYNTAX) {
939 plog_info("=== Configuration file syntax has been checked ===\n\n");
943 init_port_activate();
944 plog_info("=== Initializing rte devices ===\n");
945 if (!(prox_cfg.flags & DSF_USE_DUMMY_DEVICES))
947 init_rte_dev(prox_cfg.flags & DSF_USE_DUMMY_DEVICES);
948 plog_info("=== Calibrating TSC overhead ===\n");
950 plog_info("\tTSC running at %"PRIu64" Hz\n", rte_get_tsc_hz());
953 plog_info("=== Initializing ports ===\n");
956 if (prox_cfg.logbuf_size) {
957 prox_cfg.logbuf = prox_zmalloc(prox_cfg.logbuf_size, rte_socket_id());
958 PROX_PANIC(prox_cfg.logbuf == NULL, "Failed to allocate memory for logbuf with size = %d\n", prox_cfg.logbuf_size);
961 if (prox_cfg.flags & DSF_CHECK_INIT) {
962 plog_info("=== Initialization sequence completed ===\n\n");
966 /* Current way that works to disable DPDK logging */
967 FILE *f = fopen("/dev/null", "r");
968 rte_openlog_stream(f);
969 plog_info("=== PROX started ===\n");
973 static int success = 0;
974 static void siguser_handler(int signal)
976 if (signal == SIGUSR1)
982 static void sigabrt_handler(__attribute__((unused)) int signum)
984 /* restore default disposition for SIGABRT and SIGPIPE */
985 signal(SIGABRT, SIG_DFL);
986 signal(SIGPIPE, SIG_DFL);
988 /* ignore further Ctrl-C */
989 signal(SIGINT, SIG_IGN);
991 /* more drastic exit on tedious termination signal */
992 plog_info("Aborting...\n");
993 if (lcore_cfg != NULL) {
995 pthread_t thread_id, tid0, tid = pthread_self();
996 memset(&tid0, 0, sizeof(tid0));
998 /* cancel all threads except current one */
1000 while (prox_core_next(&lcore_id, 1) == 0) {
1001 thread_id = lcore_cfg[lcore_id].thread_id;
1002 if (pthread_equal(thread_id, tid0))
1004 if (pthread_equal(thread_id, tid))
1006 pthread_cancel(thread_id);
1009 /* wait for cancelled threads to terminate */
1011 while (prox_core_next(&lcore_id, 1) == 0) {
1012 thread_id = lcore_cfg[lcore_id].thread_id;
1013 if (pthread_equal(thread_id, tid0))
1015 if (pthread_equal(thread_id, tid))
1017 pthread_join(thread_id, NULL);
1024 /* close ports on termination signal */
1025 close_ports_atexit();
1031 static void sigterm_handler(int signum)
1033 /* abort on second Ctrl-C */
1034 if (signum == SIGINT)
1035 signal(SIGINT, sigabrt_handler);
1037 /* gracefully quit on harmless termination signal */
1038 /* ports will subsequently get closed at resulting exit */
1042 int main(int argc, char **argv)
1044 /* set en_US locale to print big numbers with ',' */
1045 setlocale(LC_NUMERIC, "en_US.utf-8");
1047 if (prox_parse_args(argc, argv) != 0){
1048 prox_usage(argv[0]);
1051 plog_init(prox_cfg.log_name, prox_cfg.log_name_pid);
1052 plog_info("=== " PROGRAM_NAME " " VERSION_STR " ===\n");
1053 plog_info("\tUsing DPDK %s\n", rte_version() + sizeof(RTE_VER_PREFIX));
1056 if (prox_cfg.flags & DSF_LIST_TASK_MODES) {
1057 /* list supported task modes and exit */
1059 return EXIT_SUCCESS;
1062 /* close ports at normal exit */
1063 atexit(close_ports_atexit);
1064 /* gracefully quit on harmless termination signals */
1065 signal(SIGHUP, sigterm_handler);
1066 signal(SIGINT, sigterm_handler);
1067 signal(SIGQUIT, sigterm_handler);
1068 signal(SIGTERM, sigterm_handler);
1069 signal(SIGUSR1, sigterm_handler);
1070 signal(SIGUSR2, sigterm_handler);
1071 /* more drastic exit on tedious termination signals */
1072 signal(SIGABRT, sigabrt_handler);
1073 signal(SIGPIPE, sigabrt_handler);
1075 if (prox_cfg.flags & DSF_DAEMON) {
1076 signal(SIGUSR1, siguser_handler);
1077 signal(SIGUSR2, siguser_handler);
1078 plog_info("=== Running in Daemon mode ===\n");
1079 plog_info("\tForking child and waiting for setup completion\n");
1081 pid_t ppid = getpid();
1084 plog_err("Failed to fork process to run in daemon mode\n");
1085 return EXIT_FAILURE;
1093 kill(ppid, SIGUSR2);
1094 return EXIT_FAILURE;
1096 if (setup_prox(argc, argv) != 0) {
1097 kill(ppid, SIGUSR2);
1098 return EXIT_FAILURE;
1101 kill(ppid, SIGUSR1);
1102 run(prox_cfg.flags);
1103 return EXIT_SUCCESS;
1107 /* Before exiting the parent, wait until the
1108 child process has finished setting up */
1110 if (prox_cfg.logbuf) {
1111 file_print(prox_cfg.logbuf);
1113 return success? EXIT_SUCCESS : EXIT_FAILURE;
1117 if (setup_prox(argc, argv) != 0)
1118 return EXIT_FAILURE;
1119 run(prox_cfg.flags);
1120 return EXIT_SUCCESS;