summaryrefslogtreecommitdiffstats
path: root/qemu/hw/i386/pc_q35.c
blob: 04aae8958c5de7997a21c33e46a53ff629353df4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
 * Q35 chipset based pc system emulator
 *
 * Copyright (c) 2003-2004 Fabrice Bellard
 * Copyright (c) 2009, 2010
 *               Isaku Yamahata <yamahata at valinux co jp>
 *               VA Linux Systems Japan K.K.
 * Copyright (C) 2012 Jason Baron <jbaron@redhat.com>
 *
 * This is based on pc.c, but heavily modified.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
#include "qemu/osdep.h"
#include "hw/hw.h"
#include "hw/loader.h"
#include "sysemu/arch_init.h"
#include "hw/i2c/smbus.h"
#include "hw/boards.h"
#include "hw/timer/mc146818rtc.h"
#include "hw/xen/xen.h"
#include "sysemu/kvm.h"
#include "hw/kvm/clock.h"
#include "hw/pci-host/q35.h"
#include "exec/address-spaces.h"
#include "hw/i386/pc.h"
#include "hw/i386/ich9.h"
#include "hw/smbios/smbios.h"
#include "hw/ide/pci.h"
#include "hw/ide/ahci.h"
#include "hw/usb.h"
#include "qemu/error-report.h"
#include "migration/migration.h"

/* ICH9 AHCI has 6 ports */
#define MAX_SATA_PORTS     6

/* PC hardware initialisation */
static void pc_q35_init(MachineState *machine)
{
    PCMachineState *pcms = PC_MACHINE(machine);
    PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
    Q35PCIHost *q35_host;
    PCIHostState *phb;
    PCIBus *host_bus;
    PCIDevice *lpc;
    BusState *idebus[MAX_SATA_PORTS];
    ISADevice *rtc_state;
    MemoryRegion *system_io = get_system_io();
    MemoryRegion *pci_memory;
    MemoryRegion *rom_memory;
    MemoryRegion *ram_memory;
    GSIState *gsi_state;
    ISABus *isa_bus;
    qemu_irq *gsi;
    qemu_irq *i8259;
    int i;
    ICH9LPCState *ich9_lpc;
    PCIDevice *ahci;
    ram_addr_t lowmem;
    DriveInfo *hd[MAX_SATA_PORTS];
    MachineClass *mc = MACHINE_GET_CLASS(machine);

    /* Check whether RAM fits below 4G (leaving 1/2 GByte for IO memory
     * and 256 Mbytes for PCI Express Enhanced Configuration Access Mapping
     * also known as MMCFG).
     * If it doesn't, we need to split it in chunks below and above 4G.
     * In any case, try to make sure that guest addresses aligned at
     * 1G boundaries get mapped to host addresses aligned at 1G boundaries.
     */
    if (machine->ram_size >= 0xb0000000) {
        lowmem = 0x80000000;
    } else {
        lowmem = 0xb0000000;
    }

    /* Handle the machine opt max-ram-below-4g.  It is basically doing
     * min(qemu limit, user limit).
     */
    if (lowmem > pcms->max_ram_below_4g) {
        lowmem = pcms->max_ram_below_4g;
        if (machine->ram_size - lowmem > lowmem &&
            lowmem & ((1ULL << 30) - 1)) {
            error_report("Warning: Large machine and max_ram_below_4g(%"PRIu64
                         ") not a multiple of 1G; possible bad performance.",
                         pcms->max_ram_below_4g);
        }
    }

    if (machine->ram_size >= lowmem) {
        pcms->above_4g_mem_size = machine->ram_size - lowmem;
        pcms->below_4g_mem_size = lowmem;
    } else {
        pcms->above_4g_mem_size = 0;
        pcms->below_4g_mem_size = machine->ram_size;
    }

    if (xen_enabled()) {
        xen_hvm_init(pcms, &ram_memory);
    }

    pc_cpus_init(pcms);

    kvmclock_create();

    /* pci enabled */
    if (pcmc->pci_enabled) {
        pci_memory = g_new(MemoryRegion, 1);
        memory_region_init(pci_memory, NULL, "pci", UINT64_MAX);
        rom_memory = pci_memory;
    } else {
        pci_memory = NULL;
        rom_memory = get_system_memory();
    }

    pc_guest_info_init(pcms);

    if (pcmc->smbios_defaults) {
        /* These values are guest ABI, do not change */
        smbios_set_defaults("QEMU", "Standard PC (Q35 + ICH9, 2009)",
                            mc->name, pcmc->smbios_legacy_mode,
                            pcmc->smbios_uuid_encoded,
                            SMBIOS_ENTRY_POINT_21);
    }

    /* allocate ram and load rom/bios */
    if (!xen_enabled()) {
        pc_memory_init(pcms, get_system_memory(),
                       rom_memory, &ram_memory);
    }

    /* irq lines */
    gsi_state = g_malloc0(sizeof(*gsi_state));
    if (kvm_ioapic_in_kernel()) {
        kvm_pc_setup_irq_routing(pcmc->pci_enabled);
        gsi = qemu_allocate_irqs(kvm_pc_gsi_handler, gsi_state,
                                 GSI_NUM_PINS);
    } else {
        gsi = qemu_allocate_irqs(gsi_handler, gsi_state, GSI_NUM_PINS);
    }

    /* create pci host bus */
    q35_host = Q35_HOST_DEVICE(qdev_create(NULL, TYPE_Q35_HOST_DEVICE));

    object_property_add_child(qdev_get_machine(), "q35", OBJECT(q35_host), NULL);
    q35_host->mch.ram_memory = ram_memory;
    q35_host->mch.pci_address_space = pci_memory;
    q35_host->mch.system_memory = get_system_memory();
    q35_host->mch.address_space_io = system_io;
    q35_host->mch.below_4g_mem_size = pcms->below_4g_mem_size;
    q35_host->mch.above_4g_mem_size = pcms->above_4g_mem_size;
    /* pci */
    qdev_init_nofail(DEVICE(q35_host));
    phb = PCI_HOST_BRIDGE(q35_host);
    host_bus = phb->bus;
    pcms->bus = phb->bus;
    /* create ISA bus */
    lpc = pci_create_simple_multifunction(host_bus, PCI_DEVFN(ICH9_LPC_DEV,
                                          ICH9_LPC_FUNC), true,
                                          TYPE_ICH9_LPC_DEVICE);

    object_property_add_link(OBJECT(machine), PC_MACHINE_ACPI_DEVICE_PROP,
                             TYPE_HOTPLUG_HANDLER,
                             (Object **)&pcms->acpi_dev,
                             object_property_allow_set_link,
                             OBJ_PROP_LINK_UNREF_ON_RELEASE, &error_abort);
    object_property_set_link(OBJECT(machine), OBJECT(lpc),
                             PC_MACHINE_ACPI_DEVICE_PROP, &error_abort);

    ich9_lpc = ICH9_LPC_DEVICE(lpc);
    ich9_lpc->pic = gsi;
    ich9_lpc->ioapic = gsi_state->ioapic_irq;
    pci_bus_irqs(host_bus, ich9_lpc_set_irq, ich9_lpc_map_irq, ich9_lpc,
                 ICH9_LPC_NB_PIRQS);
    pci_bus_set_route_irq_fn(host_bus, ich9_route_intx_pin_to_irq);
    isa_bus = ich9_lpc->isa_bus;

    /*end early*/
    isa_bus_irqs(isa_bus, gsi);

    if (kvm_pic_in_kernel()) {
        i8259 = kvm_i8259_init(isa_bus);
    } else if (xen_enabled()) {
        i8259 = xen_interrupt_controller_init();
    } else {
        i8259 = i8259_init(isa_bus, pc_allocate_cpu_irq());
    }

    for (i = 0; i < ISA_NUM_IRQS; i++) {
        gsi_state->i8259_irq[i] = i8259[i];
    }
    if (pcmc->pci_enabled) {
        ioapic_init_gsi(gsi_state, "q35");
    }

    pc_register_ferr_irq(gsi[13]);

    assert(pcms->vmport != ON_OFF_AUTO__MAX);
    if (pcms->vmport == ON_OFF_AUTO_AUTO) {
        pcms->vmport = xen_enabled() ? ON_OFF_AUTO_OFF : ON_OFF_AUTO_ON;
    }

    /* init basic PC hardware */
    pc_basic_device_init(isa_bus, gsi, &rtc_state, !mc->no_floppy,
                         (pcms->vmport != ON_OFF_AUTO_ON), 0xff0104);

    /* connect pm stuff to lpc */
    ich9_lpc_pm_init(lpc, pc_machine_is_smm_enabled(pcms));

    /* ahci and SATA device, for q35 1 ahci controller is built-in */
    ahci = pci_create_simple_multifunction(host_bus,
                                           PCI_DEVFN(ICH9_SATA1_DEV,
                                                     ICH9_SATA1_FUNC),
                                           true, "ich9-ahci");
    idebus[0] = qdev_get_child_bus(&ahci->qdev, "ide.0");
    idebus[1] = qdev_get_child_bus(&ahci->qdev, "ide.1");
    g_assert(MAX_SATA_PORTS == ICH_AHCI(ahci)->ahci.ports);
    ide_drive_get(hd, ICH_AHCI(ahci)->ahci.ports);
    ahci_ide_create_devs(ahci, hd);

    if (usb_enabled()) {
        /* Should we create 6 UHCI according to ich9 spec? */
        ehci_create_ich9_with_companions(host_bus, 0x1d);
    }

    /* TODO: Populate SPD eeprom data.  */
    smbus_eeprom_init(ich9_smb_init(host_bus,
                                    PCI_DEVFN(ICH9_SMB_DEV, ICH9_SMB_FUNC),
                                    0xb100),
                      8, NULL, 0);

    pc_cmos_init(pcms, idebus[0], idebus[1], rtc_state);

    /* the rest devices to which pci devfn is automatically assigned */
    pc_vga_init(isa_bus, host_bus);
    pc_nic_init(isa_bus, host_bus);
    if (pcmc->pci_enabled) {
        pc_pci_device_init(host_bus);
    }

    if (pcms->acpi_nvdimm_state.is_enabled) {
        nvdimm_init_acpi_state(&pcms->acpi_nvdimm_state, system_io,
                               pcms->fw_cfg, OBJECT(pcms));
    }
}

#define DEFINE_Q35_MACHINE(suffix, name, compatfn, optionfn) \
    static void pc_init_##suffix(MachineState *machine) \
    { \
        void (*compat)(MachineState *m) = (compatfn); \
        if (compat) { \
            compat(machine); \
        } \
        pc_q35_init(machine); \
    } \
    DEFINE_PC_MACHINE(suffix, name, pc_init_##suffix, optionfn)


static void pc_q35_machine_options(MachineClass *m)
{
    m->family = "pc_q35";
    m->desc = "Standard PC (Q35 + ICH9, 2009)";
    m->hot_add_cpu = pc_hot_add_cpu;
    m->units_per_default_bus = 1;
    m->default_machine_opts = "firmware=bios-256k.bin";
    m->default_display = "std";
    m->no_floppy = 1;
}

static void pc_q35_2_6_machine_options(MachineClass *m)
{
    pc_q35_machine_options(m);
    m->alias = "q35";
}

DEFINE_Q35_MACHINE(v2_6, "pc-q35-2.6", NULL,
                   pc_q35_2_6_machine_options);

static void pc_q35_2_5_machine_options(MachineClass *m)
{
    PCMachineClass *pcmc = PC_MACHINE_CLASS(m);
    pc_q35_2_6_machine_options(m);
    m->alias = NULL;
    pcmc->save_tsc_khz = false;
    m->legacy_fw_cfg_order = 1;
    SET_MACHINE_COMPAT(m, PC_COMPAT_2_5);
}

DEFINE_Q35_MACHINE(v2_5, "pc-q35-2.5", NULL,
                   pc_q35_2_5_machine_options);

static void pc_q35_2_4_machine_options(MachineClass *m)
{
    PCMachineClass *pcmc = PC_MACHINE_CLASS(m);
    pc_q35_2_5_machine_options(m);
    m->hw_version = "2.4.0";
    pcmc->broken_reserved_end = true;
    SET_MACHINE_COMPAT(m, PC_COMPAT_2_4);
}

DEFINE_Q35_MACHINE(v2_4, "pc-q35-2.4", NULL,
                   pc_q35_2_4_machine_options);
1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325
/*
// Copyright (c) 2010-2017 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
*/

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <rte_cycles.h>
#include <rte_version.h>

#include "input.h"
#include "cmd_parser.h"
#include "commands.h"
#include "run.h"
#include "display.h"
#include "log.h"
#include "prox_cfg.h"
#include "prox_port_cfg.h"
#include "task_base.h"
#include "lconf.h"
#include "main.h"
#include "parse_utils.h"
#include "stats_parser.h"
#include "stats_port.h"
#include "stats_latency.h"
#include "stats_global.h"
#include "stats_prio_task.h"

#include "handle_routing.h"
#include "handle_qinq_decap4.h"
#include "handle_lat.h"
#include "handle_arp.h"
#include "handle_gen.h"
#include "handle_acl.h"
#include "handle_irq.h"
#include "defines.h"
#include "prox_cfg.h"
#include "version.h"
#include "stats_latency.h"
#include "handle_cgnat.h"
#include "handle_impair.h"
#include "rx_pkt.h"
#include "prox_compat.h"
#include "igmp.h"

static int core_task_is_valid(int lcore_id, int task_id)
{
	if (lcore_id >= RTE_MAX_LCORE) {
		plog_err("Invalid core id %u (lcore ID above %d)\n", lcore_id, RTE_MAX_LCORE);
		return 0;
	}
	else if (!prox_core_active(lcore_id, 0)) {
		plog_err("Invalid core id %u (lcore is not active)\n", lcore_id);
		return 0;
	}
	else if (task_id >= lcore_cfg[lcore_id].n_tasks_all) {
		plog_err("Invalid task id (valid task IDs for core %u are below %u)\n",
			 lcore_id, lcore_cfg[lcore_id].n_tasks_all);
		return 0;
	}
	return 1;
}

static int cores_task_are_valid(unsigned int *lcores, int task_id, unsigned int nb_cores)
{
	unsigned int lcore_id;
	for (unsigned int i = 0; i < nb_cores; i++) {
		lcore_id = lcores[i];
		if (core_task_is_valid(lcore_id, task_id) == 0)
			return 0;
	}
	return 1;
}

static int parse_cores_task(const char *str, uint32_t *lcore_id, uint32_t *task_id, unsigned *nb_cores)
{
	char str_lcore_id[128];
	int ret;

	if (2 != sscanf(str, "%s %u", str_lcore_id, task_id))
		return -1;

	if ((ret = parse_list_set(lcore_id, str_lcore_id, RTE_MAX_LCORE)) <= 0) {
		plog_err("Invalid core while parsing command (%s)\n", get_parse_err());
		return -1;
	}
	*nb_cores = ret;

	return 0;
}

static int parse_cores_tasks(const char *str, uint32_t *lcore_id, uint32_t *task_id, unsigned *nb_cores, unsigned *nb_tasks)
{
	char str_lcore_id[128], str_task_id[128];
	int ret;

	if (2 != sscanf(str, "%s %s", str_lcore_id, str_task_id))
		return -1;

	if ((ret = parse_list_set(lcore_id, str_lcore_id, RTE_MAX_LCORE)) <= 0) {
		plog_err("Invalid core while parsing command (%s)\n", get_parse_err());
		return -1;
	}
	*nb_cores = ret;

	if ((ret = parse_list_set(task_id, str_task_id, MAX_TASKS_PER_CORE)) <= 0) {
		plog_err("Invalid task while parsing command (%s)\n", get_parse_err());
		return -1;
	}
	*nb_tasks = ret;

	return 0;
}

static const char *strchr_skip_twice(const char *str, int chr)
{
	str = strchr(str, chr);
	if (!str)
		return NULL;
	str = str + 1;

	str = strchr(str, chr);
	if (!str)
		return NULL;
	return str + 1;
}

static int parse_cmd_quit(const char *str, struct input *input)
{
	if (strcmp(str, "") != 0) {
		return -1;
	}

	quit();
	return 0;
}

static int parse_cmd_quit_force(const char *str, struct input *input)
{
	if (strcmp(str, "") != 0) {
		return -1;
	}

	abort();
}

static int parse_cmd_history(const char *str, struct input *input)
{
	if (strcmp(str, "") != 0) {
		return -1;
	}

	if (input->history) {
		input->history(input);
		return 0;
	}
	plog_err("Invalid history comand ");
	return -1;
}

static int parse_cmd_echo(const char *str, struct input *input)
{
	if (strcmp(str, "") == 0) {
		return -1;
	}

	char resolved[2048];

	if (parse_vars(resolved, sizeof(resolved), str)) {
		return 0;
	}

	if (input->reply) {
		if (strlen(resolved) + 2 < sizeof(resolved)) {
			resolved[strlen(resolved) + 1] = 0;
			resolved[strlen(resolved)] = '\n';
		}
		else
			return 0;

		input->reply(input, resolved, strlen(resolved));
	} else
		plog_info("%s\n", resolved);

	return 0;
}

static int parse_cmd_reset_stats(const char *str, struct input *input)
{
	if (strcmp(str, "") != 0) {
		return -1;
	}

	stats_reset();
	return 0;
}

static int parse_cmd_reset_lat_stats(const char *str, struct input *input)
{
	if (strcmp(str, "") != 0) {
		return -1;
	}

	stats_latency_reset();
	return 0;
}

static int parse_cmd_trace(const char *str, struct input *input)
{
	unsigned lcores[RTE_MAX_LCORE], task_id, nb_packets, nb_cores;

	if (parse_cores_task(str, lcores, &task_id, &nb_cores))
		return -1;
	if (!(str = strchr_skip_twice(str, ' ')))
		return -1;
	if (sscanf(str, "%u", &nb_packets) != 1)
		return -1;

	if (cores_task_are_valid(lcores, task_id, nb_cores)) {
		for (unsigned int i = 0; i < nb_cores; i++) {
			cmd_trace(lcores[i], task_id, nb_packets);
		}
	}
	return 0;
}

static int parse_cmd_dump_rx(const char *str, struct input *input)
{
	unsigned lcores[RTE_MAX_LCORE], task_id, nb_packets, nb_cores;

	if (parse_cores_task(str, lcores, &task_id, &nb_cores))
		return -1;
	if (!(str = strchr_skip_twice(str, ' ')))
		return -1;
	if (sscanf(str, "%u", &nb_packets) != 1) {
		return -1;
	}

	if (cores_task_are_valid(lcores, task_id, nb_cores)) {
		for (unsigned int i = 0; i < nb_cores; i++) {
			if (lcores[i] > RTE_MAX_LCORE) {
				plog_warn("core_id too high, maximum allowed is: %u\n", RTE_MAX_LCORE);
				return -1;
			} else if (task_id >= lcore_cfg[lcores[i]].n_tasks_all) {
				plog_warn("task_id too high, should be in [0, %u]\n", lcore_cfg[lcores[i]].n_tasks_all - 1);
				return -1;
			} else {
				struct lcore_cfg *lconf = &lcore_cfg[lcores[i]];
				struct task_base *tbase = lconf->tasks_all[task_id];
				int prev_count = tbase->aux->rx_prev_count;
				if (((prev_count) && (tbase->aux->rx_pkt_prev[prev_count - 1] == rx_pkt_dummy))
					|| (tbase->rx_pkt == rx_pkt_dummy)) {
					plog_warn("Unable to dump_rx as rx_pkt_dummy\n");
					return -1;
				}
			}
			cmd_dump(lcores[i], task_id, nb_packets, input, 1, 0);
		}
	}
	return 0;
}

static int parse_cmd_pps_unit(const char *str, struct input *input)
{
	uint32_t val;

	if (sscanf(str, "%u", &val) != 1) {
		return -1;
	}
	display_set_pps_unit(val);
	return 0;
}

static int parse_cmd_dump_tx(const char *str, struct input *input)
{
	unsigned lcores[RTE_MAX_LCORE], task_id, nb_packets, nb_cores;

	if (parse_cores_task(str, lcores, &task_id, &nb_cores))
		return -1;
	if (!(str = strchr_skip_twice(str, ' ')))
		return -1;
	if (sscanf(str, "%u", &nb_packets) != 1) {
		return -1;
	}

	if (cores_task_are_valid(lcores, task_id, nb_cores)) {
		for (unsigned int i = 0; i < nb_cores; i++) {
			cmd_dump(lcores[i], task_id, nb_packets, input, 0, 1);
		}
	}
	return 0;
}

static int parse_cmd_rate(const char *str, struct input *input)
{
	unsigned queue, port, rate;

	if (sscanf(str, "%u %u %u", &queue, &port, &rate) != 3) {
		return -1;
	}

	if (port > PROX_MAX_PORTS) {
		plog_err("Max port id allowed is %u (specified %u)\n", PROX_MAX_PORTS, port);
	}
	else if (!prox_port_cfg[port].active) {
		plog_err("Port %u not active\n", port);
	}
	else if (queue >= prox_port_cfg[port].n_txq) {
		plog_err("Number of active queues is %u\n",
			 prox_port_cfg[port].n_txq);
	}
	else if (rate > prox_port_cfg[port].link_speed) {
		plog_err("Max rate allowed on port %u queue %u is %u Mbps\n",
			 port, queue, prox_port_cfg[port].link_speed);
	}
	else {
		if (rate == 0) {
			plog_info("Disabling rate limiting on port %u queue %u\n",
				  port, queue);
		}
		else {
			plog_info("Setting rate limiting to %u Mbps on port %u queue %u\n",
				  rate, port, queue);
		}
		rte_eth_set_queue_rate_limit(port, queue, rate);
	}
	return 0;
}

int task_is_mode_and_submode(uint32_t lcore_id, uint32_t task_id, const char *mode, const char *sub_mode)
{
	struct task_args *targs = &lcore_cfg[lcore_id].targs[task_id];

	return !strcmp(targs->task_init->mode_str, mode) && !strcmp(targs->sub_mode_str, sub_mode);
}

int task_is_mode(uint32_t lcore_id, uint32_t task_id, const char *mode)
{
	struct task_init *t = lcore_cfg[lcore_id].targs[task_id].task_init;

	return !strcmp(t->mode_str, mode);
}

int task_is_sub_mode(uint32_t lcore_id, uint32_t task_id, const char *sub_mode)
{
	struct task_args *targs = &lcore_cfg[lcore_id].targs[task_id];

	return !strcmp(targs->sub_mode_str, sub_mode);
}

static void log_pkt_count(uint32_t count, uint32_t lcore_id, uint32_t task_id)
{
	if (count == UINT32_MAX)
		plog_info("Core %u task %u will keep sending packets\n", lcore_id, task_id);
	else if (count == 0)
		plog_info("Core %u task %u waits for next count command\n", lcore_id, task_id);
	else
		plog_info("Core %u task %u stopping after %u packets\n", lcore_id, task_id, count);
}

static int parse_cmd_count(const char *str, struct input *input)
{
	unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, count, nb_cores;

	if (parse_cores_task(str, lcores, &task_id, &nb_cores))
		return -1;
	if (!(str = strchr_skip_twice(str, ' ')))
		return -1;
	if (sscanf(str, "%u", &count) != 1)
		return -1;

	if (cores_task_are_valid(lcores, task_id, nb_cores)) {
		for (unsigned int i = 0; i < nb_cores; i++) {
			lcore_id = lcores[i];
			if ((!task_is_mode_and_submode(lcore_id, task_id, "gen", "")) && (!task_is_mode_and_submode(lcore_id, task_id, "gen", "l3"))) {
				plog_err("Core %u task %u is not generating packets\n", lcore_id, task_id);
			}
			else {
				struct task_base *task = lcore_cfg[lcore_id].tasks_all[task_id];

				log_pkt_count(count, lcore_id, task_id);
				task_gen_set_pkt_count(task, count);
			}
		}
	}
	return 0;
}

static int parse_cmd_set_probability(const char *str, struct input *input)
{
	unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;
	float probability;

	if (parse_cores_task(str, lcores, &task_id, &nb_cores))
		return -1;
	if (!(str = strchr_skip_twice(str, ' ')))
		return -1;
	if (sscanf(str, "%f", &probability) != 1)
		return -1;

	if (cores_task_are_valid(lcores, task_id, nb_cores)) {
		for (unsigned int i = 0; i < nb_cores; i++) {
			lcore_id = lcores[i];
			if ((!task_is_mode_and_submode(lcore_id, task_id, "impair", "")) && (!task_is_mode_and_submode(lcore_id, task_id, "impair", "l3"))){
				plog_err("Core %u task %u is not impairing packets\n", lcore_id, task_id);
			} else {
				struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
				task_impair_set_proba(tbase, probability);
			}
		}
	}
	return 0;
}

static int parse_cmd_delay_us(const char *str, struct input *input)
{
	unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, delay_us, nb_cores;

	if (parse_cores_task(str, lcores, &task_id, &nb_cores))
		return -1;
	if (!(str = strchr_skip_twice(str, ' ')))
		return -1;
	if (sscanf(str, "%d", &delay_us) != 1)
		return -1;

	if (cores_task_are_valid(lcores, task_id, nb_cores)) {
		for (unsigned int i = 0; i < nb_cores; i++) {
			lcore_id = lcores[i];
			if ((!task_is_mode_and_submode(lcore_id, task_id, "impair", "")) && (!task_is_mode_and_submode(lcore_id, task_id, "impair", "l3"))){
				plog_err("Core %u task %u is not impairing packets\n", lcore_id, task_id);
			} else {
				struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
				task_impair_set_delay_us(tbase, delay_us, 0);
			}
		}
	}
	return 0;
}

static int parse_cmd_random_delay_us(const char *str, struct input *input)
{
	unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, delay_us, nb_cores;

	if (parse_cores_task(str, lcores, &task_id, &nb_cores))
		return -1;
	if (!(str = strchr_skip_twice(str, ' ')))
		return -1;
	if (sscanf(str, "%d", &delay_us) != 1)
		return -1;

	if (cores_task_are_valid(lcores, task_id, nb_cores)) {
		for (unsigned int i = 0; i < nb_cores; i++) {
			lcore_id = lcores[i];
			if ((!task_is_mode_and_submode(lcore_id, task_id, "impair", "")) && (!task_is_mode_and_submode(lcore_id, task_id, "impair", "l3"))){
				plog_err("Core %u task %u is not impairing packets\n", lcore_id, task_id);
			} else {
				struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
				task_impair_set_delay_us(tbase, 0, delay_us);
			}
		}
	}
	return 0;
}

static int parse_cmd_bypass(const char *str, struct input *input)
{
	unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, pkt_size, nb_cores;

	if (parse_cores_task(str, lcores, &task_id, &nb_cores))
		return -1;
	if ((prox_cfg.flags & DSF_ENABLE_BYPASS) == 0) {
		plog_err("enable bypass not set => command not supported\n");
		return -1;
	}

	if (cores_task_are_valid(lcores, task_id, nb_cores)) {
		for (unsigned int i = 0; i < nb_cores; i++) {
			lcore_id = lcores[i];
			if (bypass_task(lcore_id, task_id) != 0)
				return -1;
		}
	}
	return 0;
}

static int parse_cmd_reconnect(const char *str, struct input *input)
{
	unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, pkt_size, nb_cores;

	if (parse_cores_task(str, lcores, &task_id, &nb_cores))
		return -1;
	if (cores_task_are_valid(lcores, task_id, nb_cores)) {
		for (unsigned int i = 0; i < nb_cores; i++) {
			lcore_id = lcores[i];
			if (reconnect_task(lcore_id, task_id) != 0)
				return -1;
		}
	}
	return 0;
}

static int parse_cmd_pkt_size(const char *str, struct input *input)
{
	unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, pkt_size, nb_cores;

	if (parse_cores_task(str, lcores, &task_id, &nb_cores))
		return -1;
	if (!(str = strchr_skip_twice(str, ' ')))
		return -1;
	if (sscanf(str, "%d", &pkt_size) != 1)
		return -1;

	if (cores_task_are_valid(lcores, task_id, nb_cores)) {
		for (unsigned int i = 0; i < nb_cores; i++) {
			lcore_id = lcores[i];
			if ((!task_is_mode_and_submode(lcore_id, task_id, "gen", "")) && (!task_is_mode_and_submode(lcore_id, task_id, "gen", "l3"))) {
				plog_err("Core %u task %u is not generating packets\n", lcore_id, task_id);
			} else {
				struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
				task_gen_set_pkt_size(tbase, pkt_size); /* error printed within function */
			}
		}
	}
	return 0;
}

static int parse_cmd_speed(const char *str, struct input *input)
{
	unsigned lcores[RTE_MAX_LCORE], task_id, lcore_id, nb_cores;
	float speed;
	unsigned i;

	if (parse_cores_task(str, lcores, &task_id, &nb_cores))
		return -1;
	if (!(str = strchr_skip_twice(str, ' ')))
		return -1;
	if (sscanf(str, "%f", &speed) != 1) {
		return -1;
	}

	if (!cores_task_are_valid(lcores, task_id, nb_cores)) {
		return 0;
	}

	for (i = 0; i < nb_cores; i++) {
		lcore_id = lcores[i];
		if ((!task_is_mode_and_submode(lcore_id, task_id, "gen", "")) && (!task_is_mode_and_submode(lcore_id, task_id, "gen", "l3"))) {
			plog_err("Core %u task %u is not generating packets\n", lcore_id, task_id);
		}
		else if (speed > 1000.0f || speed < 0.0f) {	// Up to 100 Gbps
			plog_err("Speed out of range (must be betweeen 0%% and 1000%%)\n");
		}
		else {
			struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
			uint64_t bps = speed * 12500000;

			plog_info("Setting rate to %"PRIu64" Bps\n", bps);

			task_gen_set_rate(tbase, bps);
		}
	}
	return 0;
}

static int parse_cmd_speed_byte(const char *str, struct input *input)
{
	unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;
	uint64_t bps;

	if (parse_cores_task(str, lcores, &task_id, &nb_cores))
		return -1;
	if (!(str = strchr_skip_twice(str, ' ')))
		return -1;
	if (sscanf(str, "%"PRIu64"", &bps) != 1)
		return -1;

	if (cores_task_are_valid(lcores, task_id, nb_cores)) {
		for (unsigned int i = 0; i < nb_cores; i++) {
			lcore_id = lcores[i];

			if ((!task_is_mode_and_submode(lcore_id, task_id, "gen", "")) && (!task_is_mode_and_submode(lcore_id, task_id, "gen", "l3"))) {
				plog_err("Core %u task %u is not generating packets\n", lcore_id, task_id);
			}
			else if (bps > 12500000000) {	// Up to 100Gbps
				plog_err("Speed out of range (must be <= 12500000000)\n");
			}
			else {
				struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];

				plog_info("Setting rate to %"PRIu64" Bps\n", bps);
				task_gen_set_rate(tbase, bps);
			}
		}
	}
	return 0;
}

static int parse_cmd_reset_randoms_all(const char *str, struct input *input)
{
	if (strcmp(str, "") != 0) {
		return -1;
	}

	unsigned task_id, lcore_id = -1;
	while (prox_core_next(&lcore_id, 0) == 0) {
		for (task_id = 0; task_id < lcore_cfg[lcore_id].n_tasks_all; task_id++) {
			if ((task_is_mode_and_submode(lcore_id, task_id, "gen", "")) || (task_is_mode_and_submode(lcore_id, task_id, "gen", "l3"))) {
				struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
				uint32_t n_rands = task_gen_get_n_randoms(tbase);

				plog_info("Resetting randoms on core %d task %d from %d randoms\n", lcore_id, task_id, n_rands);
				task_gen_reset_randoms(tbase);
			}
		}
	}
	return 0;
}

static int parse_cmd_reset_values_all(const char *str, struct input *input)
{
	if (strcmp(str, "") != 0) {
		return -1;
	}

	unsigned task_id, lcore_id = -1;
	while (prox_core_next(&lcore_id, 0) == 0) {
		for (task_id = 0; task_id < lcore_cfg[lcore_id].n_tasks_all; task_id++) {
			if ((task_is_mode_and_submode(lcore_id, task_id, "gen", "")) || (task_is_mode_and_submode(lcore_id, task_id, "gen", "l3"))) {
				struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];

				plog_info("Resetting values on core %d task %d\n", lcore_id, task_id);
				task_gen_reset_values(tbase);
			}
		}
	}
	return 0;
}

static int parse_cmd_reset_values(const char *str, struct input *input)
{
	unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;

	if (parse_cores_task(str, lcores, &task_id, &nb_cores))
		return -1;

	if (cores_task_are_valid(lcores, task_id, nb_cores)) {
		for (unsigned int i = 0; i < nb_cores; i++) {
			lcore_id = lcores[i];
			if ((!task_is_mode_and_submode(lcore_id, task_id, "gen", "")) && (!task_is_mode_and_submode(lcore_id, task_id, "gen", "l3"))) {
				plog_err("Core %u task %u is not generating packets\n", lcore_id, task_id);
			}
			else {
				struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];

				plog_info("Resetting values on core %d task %d\n", lcore_id, task_id);
				task_gen_reset_values(tbase);
			}
		}
	}
	return 0;
}

static int parse_cmd_set_value(const char *str, struct input *input)
{
	unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, value, nb_cores;
	unsigned short offset;
	uint8_t value_len;

	if (parse_cores_task(str, lcores, &task_id, &nb_cores))
		return -1;
	if (!(str = strchr_skip_twice(str, ' ')))
		return -1;
	if (sscanf(str, "%hu %u %hhu", &offset, &value, &value_len) != 3) {
		return -1;
	}

	if (cores_task_are_valid(lcores, task_id, nb_cores)) {
		for (unsigned int i = 0; i < nb_cores; i++) {
			lcore_id = lcores[i];
			if ((!task_is_mode_and_submode(lcore_id, task_id, "gen", "")) && (!task_is_mode_and_submode(lcore_id, task_id, "gen", "l3"))) {
				plog_err("Core %u task %u is not generating packets\n", lcore_id, task_id);
			}
			// do not check offset here - gen knows better than us the maximum frame size
			else if (value_len > 4) {
				plog_err("Length out of range (must be less then 4)\n");
			}
			else {
				struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];

				if (task_gen_set_value(tbase, value, offset, value_len))
					plog_info("Unable to set Byte %"PRIu16" to %"PRIu8" - invalid offset/len\n", offset, value);
				else
					plog_info("Setting Byte %"PRIu16" to %"PRIu32"\n", offset, value);
			}
		}
	}
	return 0;
}

static int parse_cmd_set_random(const char *str, struct input *input)
{
	unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;
	unsigned short offset;
	uint8_t value_len;
	char rand_str[64];
	int16_t rand_id = -1;

	if (parse_cores_task(str, lcores, &task_id, &nb_cores))
		return -1;
	if (!(str = strchr_skip_twice(str, ' ')))
		return -1;
	if (sscanf(str, "%hu %32s %hhu", &offset, rand_str, &value_len) != 3) {
		return -1;
	}

	if (cores_task_are_valid(lcores, task_id, nb_cores)) {
		for (unsigned int i = 0; i < nb_cores; i++) {
			lcore_id = lcores[i];
			if ((!task_is_mode_and_submode(lcore_id, task_id, "gen", "")) && (!task_is_mode_and_submode(lcore_id, task_id, "gen", "l3"))) {
				plog_err("Core %u task %u is not generating packets\n", lcore_id, task_id);
			}
			else if (offset > PROX_RTE_ETHER_MAX_LEN) {
				plog_err("Offset out of range (must be less then %u)\n", PROX_RTE_ETHER_MAX_LEN);
			}
			else if (value_len > 4) {
				plog_err("Length out of range (must be less then 4)\n");
			} else {
				struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];

				if (task_gen_add_rand(tbase, rand_str, offset, rand_id)) {
					plog_warn("Random not added on core %u task %u\n", lcore_id, task_id);
				}
			}
		}
	}
	return 0;
}

static int parse_cmd_thread_info(const char *str, struct input *input)
{
	unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;

	if (parse_cores_task(str, lcores, &task_id, &nb_cores))
		return -1;
	for (unsigned int i = 0; i < nb_cores; i++) {
		cmd_thread_info(lcores[i], task_id);
	}
	return 0;
}

static int parse_cmd_verbose(const char *str, struct input *input)
{
	unsigned id;

	if (sscanf(str, "%u", &id) != 1) {
		return -1;
	}

	if (plog_set_lvl(id) != 0) {
		plog_err("Cannot set log level to %u\n", id);
	}
	return 0;
}

static int parse_cmd_arp_add(const char *str, struct input *input)
{
	struct arp_msg amsg;
	struct arp_msg *pmsg = &amsg;
	unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;
	struct rte_ring *ring;

	if (parse_cores_task(str, lcores, &task_id, &nb_cores))
		return -1;
	if (!(str = strchr_skip_twice(str, ' ')))
		return -1;
	if (strcmp(str, ""))
		return -1;
	if (cores_task_are_valid(lcores, task_id, nb_cores)) {
		if (str_to_arp_msg(&amsg, str) == 0) {
			for (unsigned int i = 0; i < nb_cores; i++) {
				lcore_id = lcores[i];
				ring = ctrl_rings[lcore_id*MAX_TASKS_PER_CORE + task_id];
				if (!ring) {
					plog_err("No ring for control messages to core %u task %u\n", lcore_id, task_id);
				}
				else {
#if RTE_VERSION < RTE_VERSION_NUM(17,5,0,1)
					while (rte_ring_sp_enqueue_bulk(ring, (void *const *)&pmsg, 1));
#else
					while (rte_ring_sp_enqueue_bulk(ring, (void *const *)&pmsg, 1, NULL) == 0);
#endif
					while (!rte_ring_empty(ring));
				}
			}
			return 0;
		}
	}
	return -1;
}

static int parse_cmd_rule_add(const char *str, struct input *input)
{
	struct rte_ring *ring;
	unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;

	if (parse_cores_task(str, lcores, &task_id, &nb_cores))
		return -1;
	if (!(str = strchr_skip_twice(str, ' ')))
		return -1;
	if (strcmp(str, ""))
		return -1;
	char *fields[9];
	char str_cpy[255];
	prox_strncpy(str_cpy, str, 255);
	// example add rule command: rule add 15 0 1&0x0fff 1&0x0fff 0&0 128.0.0.0/1 128.0.0.0/1 5000-5000 5000-5000 allow
	int ret = rte_strsplit(str_cpy, 255, fields, 9, ' ');
	if (ret != 8) {
		return -1;
	}

	struct acl4_rule rule;
	struct acl4_rule *prule = &rule;
	if (cores_task_are_valid(lcores, task_id, nb_cores)) {
		if (str_to_rule(&rule, fields, -1, 1) == 0) {
			for (unsigned int i = 0; i < nb_cores; i++) {
				lcore_id = lcores[i];
				ring = ctrl_rings[lcore_id*MAX_TASKS_PER_CORE + task_id];
				if (!ring) {
					plog_err("No ring for control messages to core %u task %u\n", lcore_id, task_id);
				}
				else {
#if RTE_VERSION < RTE_VERSION_NUM(17,5,0,1)
					while (rte_ring_sp_enqueue_bulk(ring, (void *const *)&prule, 1));
#else
					while (rte_ring_sp_enqueue_bulk(ring, (void *const *)&prule, 1, NULL) == 0);
#endif
					while (!rte_ring_empty(ring));
				}
			}
			return 0;
		}
	}
	return -1;
}

static int parse_cmd_gateway_ip(const char *str, struct input *input)
{
	unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, ip[4], nb_cores, i;

	if (parse_cores_task(str, lcores, &task_id, &nb_cores))
		return -1;
	if (!(str = strchr_skip_twice(str, ' ')))
		return -1;
	if (!strcmp(str, ""))
		return -1;
	if (sscanf(str, "%u.%u.%u.%u", ip, ip + 1, ip + 2, ip + 3) != 4) {
		return -1;
	}
	for (i = 0; i < nb_cores; i++) {
		lcore_id = lcores[i];

		if (!task_is_sub_mode(lcore_id, task_id, "l3")) {
			plog_err("Core %u task %u is not in l3 mode\n", lcore_id, task_id);
		}
		else {
			uint32_t gateway_ip = ((ip[3] & 0xFF) << 24) | ((ip[2] & 0xFF) << 16) | ((ip[1] & 0xFF) << 8) | ((ip[0] & 0xFF) << 0);
			struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
			plog_info("Setting gateway ip to %s\n", str);
			task_set_gateway_ip(tbase, gateway_ip);
		}
	}
	return 0;
}

static int parse_cmd_local_ip(const char *str, struct input *input)
{
	unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, ip[4], nb_cores, i;

	if (parse_cores_task(str, lcores, &task_id, &nb_cores))
		return -1;
	if (!(str = strchr_skip_twice(str, ' ')))
		return -1;
	if (!strcmp(str, ""))
		return -1;
	if (sscanf(str, "%u.%u.%u.%u", ip, ip + 1, ip + 2, ip + 3) != 4) {
		return -1;
	}
	for (i = 0; i < nb_cores; i++) {
		lcore_id = lcores[i];
		struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
		uint32_t local_ip = ((ip[3] & 0xFF) << 24) | ((ip[2] & 0xFF) << 16) | ((ip[1] & 0xFF) << 8) | ((ip[0] & 0xFF) << 0);
		if (!task_is_mode_and_submode(lcore_id, task_id, "arp", "local")) {
			if (!task_is_sub_mode(lcore_id, task_id, "l3")) {
				plog_err("Core %u task %u is not in l3 mode\n", lcore_id, task_id);
			} else {
				plog_info("Setting local ip to %s\n", str);
				task_set_local_ip(tbase, local_ip);
			}
		} else {
			plog_info("Setting local ip to %s\n", str);
			task_arp_set_local_ip(tbase, local_ip);
		}
	}
	return 0;
}

static int parse_cmd_route_add(const char *str, struct input *input)
{
	unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, prefix, next_hop_idx, ip[4], nb_cores;

	if (parse_cores_task(str, lcores, &task_id, &nb_cores))
		return -1;
	if (!(str = strchr_skip_twice(str, ' ')))
		return -1;
	if (strcmp(str, ""))
		return -1;
	if (sscanf(str, "%u.%u.%u.%u/%u %u", ip, ip + 1, ip + 2, ip + 3,
		   &prefix, &next_hop_idx) != 8) {
		return -1;
	}
	struct rte_ring *ring;

	if (cores_task_are_valid(lcores, task_id, nb_cores)) {
		for (unsigned int i = 0; i < nb_cores; i++) {
			lcore_id = lcores[i];
			ring = ctrl_rings[lcore_id*MAX_TASKS_PER_CORE + task_id];
			if (!ring) {
				plog_err("No ring for control messages to core %u task %u\n", lcore_id, task_id);
			}
			else {
				struct route_msg rmsg;
				struct route_msg *pmsg = &rmsg;

				rmsg.ip_bytes[0] = ip[0];
				rmsg.ip_bytes[1] = ip[1];
				rmsg.ip_bytes[2] = ip[2];
				rmsg.ip_bytes[3] = ip[3];
				rmsg.prefix = prefix;
				rmsg.nh = next_hop_idx;
#if RTE_VERSION < RTE_VERSION_NUM(17,5,0,1)
				while (rte_ring_sp_enqueue_bulk(ring, (void *const *)&pmsg, 1));
#else
				while (rte_ring_sp_enqueue_bulk(ring, (void *const *)&pmsg, 1, NULL) == 0);
#endif
				while (!rte_ring_empty(ring));
			}
		}
	}
	return 0;
}

static int parse_cmd_start(const char *str, struct input *input)
{
	int task_id = -1;

	if (strncmp(str, "all", 3) == 0) {
		str += 3;
		sscanf(str, "%d", &task_id);

		start_core_all(task_id);
		req_refresh();
		return 0;
	}

	uint32_t cores[64] = {0};
	int ret;
	ret = parse_list_set(cores, str, 64);
	if (ret < 0) {
		return -1;
	}
	str = strchr(str, ' ');

	if (str) {
		sscanf(str, "%d", &task_id);
	}
	start_cores(cores, ret, task_id);
	req_refresh();
	return 0;
}

static int parse_cmd_stop(const char *str, struct input *input)
{
	int task_id = -1;

	if (strncmp(str, "all", 3) == 0) {
		str += 3;
		sscanf(str, "%d", &task_id);
		stop_core_all(task_id);
		req_refresh();
		return 0;
	}

	uint32_t cores[64] = {0};
	int ret;
	ret = parse_list_set(cores, str, 64);
	if (ret < 0) {
		return -1;
	}
	str = strchr(str, ' ');

	if (str) {
		sscanf(str, "%d", &task_id);
	}
	stop_cores(cores, ret, task_id);
	req_refresh();

	return 0;
}

static int parse_cmd_rx_distr_start(const char *str, struct input *input)
{
	unsigned lcore_id[RTE_MAX_LCORE];

	int nb_cores;

	nb_cores = parse_list_set(lcore_id, str, sizeof(lcore_id)/sizeof(lcore_id[0]));

	if (nb_cores <= 0) {
		return -1;
	}

	for (int i = 0; i < nb_cores; ++i)
		cmd_rx_distr_start(lcore_id[i]);
	return 0;
}

static int parse_cmd_tx_distr_start(const char *str, struct input *input)
{
	unsigned lcore_id[RTE_MAX_LCORE];

	int nb_cores;

	nb_cores = parse_list_set(lcore_id, str, sizeof(lcore_id)/sizeof(lcore_id[0]));

	if (nb_cores <= 0) {
		return -1;
	}

	for (int i = 0; i < nb_cores; ++i)
		cmd_tx_distr_start(lcore_id[i]);
	return 0;
}

static int parse_cmd_rx_distr_stop(const char *str, struct input *input)
{
	unsigned lcore_id[RTE_MAX_LCORE];

	int nb_cores;

	nb_cores = parse_list_set(lcore_id, str, sizeof(lcore_id)/sizeof(lcore_id[0]));

	if (nb_cores <= 0) {
		return -1;
	}

	for (int i = 0; i < nb_cores; ++i)
		cmd_rx_distr_stop(lcore_id[i]);
	return 0;
}

static int parse_cmd_tx_distr_stop(const char *str, struct input *input)
{
	unsigned lcore_id[RTE_MAX_LCORE];

	int nb_cores;

	nb_cores = parse_list_set(lcore_id, str, sizeof(lcore_id)/sizeof(lcore_id[0]));

	if (nb_cores <= 0) {
		return -1;
	}

	for (int i = 0; i < nb_cores; ++i)
		cmd_tx_distr_stop(lcore_id[i]);
	return 0;
}

static int parse_cmd_rx_distr_reset(const char *str, struct input *input)
{
	unsigned lcore_id[RTE_MAX_LCORE];

	int nb_cores;

	nb_cores = parse_list_set(lcore_id, str, sizeof(lcore_id)/sizeof(lcore_id[0]));

	if (nb_cores <= 0) {
		return -1;
	}

	for (int i = 0; i < nb_cores; ++i)
		cmd_rx_distr_rst(lcore_id[i]);
	return 0;
}

static int parse_cmd_tx_distr_reset(const char *str, struct input *input)
{
	unsigned lcore_id[RTE_MAX_LCORE];

	int nb_cores;

	nb_cores = parse_list_set(lcore_id, str, sizeof(lcore_id)/sizeof(lcore_id[0]));

	if (nb_cores <= 0) {
		return -1;
	}

	for (int i = 0; i < nb_cores; ++i)
		cmd_tx_distr_rst(lcore_id[i]);
	return 0;
}

static int parse_cmd_rx_distr_show(const char *str, struct input *input)
{
	unsigned lcore_id[RTE_MAX_LCORE];

	int nb_cores;

	nb_cores = parse_list_set(lcore_id, str, sizeof(lcore_id)/sizeof(lcore_id[0]));

	if (nb_cores <= 0) {
		return -1;
	}

	for (int i = 0; i < nb_cores; ++i)
		cmd_rx_distr_show(lcore_id[i]);
	return 0;
}

static int parse_cmd_tx_distr_show(const char *str, struct input *input)
{
	unsigned lcore_id[RTE_MAX_LCORE];

	int nb_cores;

	nb_cores = parse_list_set(lcore_id, str, sizeof(lcore_id)/sizeof(lcore_id[0]));

	if (nb_cores <= 0) {
		return -1;
	}

	for (int i = 0; i < nb_cores; ++i)
		cmd_tx_distr_show(lcore_id[i]);
	return 0;
}

static int parse_cmd_tot_stats(const char *str, struct input *input)
{
	if (strcmp("", str) != 0) {
		return -1;
	}

	struct global_stats_sample *gsl = stats_get_global_stats(1);
	uint64_t tot_rx = gsl->host_rx_packets;
	uint64_t tot_tx = gsl->host_tx_packets;
	uint64_t last_tsc = gsl->tsc;

	if (input->reply) {
		char buf[128];
		snprintf(buf, sizeof(buf), "%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64"\n",
			 tot_rx, tot_tx, last_tsc, rte_get_tsc_hz());
		input->reply(input, buf, strlen(buf));
	}
	else {
		plog_info("RX: %"PRIu64", TX: %"PRIu64"\n", tot_rx, tot_tx);
	}
	return 0;
}

static int parse_cmd_update_interval(const char *str, struct input *input)
{
	unsigned val;

	if (sscanf(str, "%u", &val) != 1) {
		return -1;
	}

	if (val == 0) {
		plog_err("Minimum update interval is 1 ms\n");
	}
	else {
		plog_info("Setting update interval to %d ms\n", val);
		set_update_interval(val);
	}
	return 0;
}

static int parse_cmd_mem_info(const char *str, struct input *input)
{
	if (strcmp("", str) != 0) {
		return -1;
	}

	cmd_mem_stats();
	cmd_mem_layout();
	return 0;
}

static int parse_cmd_tot_ierrors_tot(const char *str, struct input *input)
{
	if (strcmp(str, "") != 0) {
		return -1;
	}

	struct global_stats_sample *gsl = stats_get_global_stats(1);
	uint64_t tot = gsl->nics_ierrors;
	uint64_t last_tsc = gsl->tsc;

	if (input->reply) {
		char buf[128];
		snprintf(buf, sizeof(buf),
			 "%"PRIu64",%"PRIu64",%"PRIu64"\n",
			 tot, last_tsc, rte_get_tsc_hz());
		input->reply(input, buf, strlen(buf));
	}
	else {
		plog_info("ierrors: %"PRIu64"\n", tot);
	}
	return 0;
}

static int parse_cmd_tot_imissed_tot(const char *str, struct input *input)
{
	if (strcmp(str, "") != 0) {
		return -1;
	}

	struct global_stats_sample *gsl = stats_get_global_stats(1);
	uint64_t tot = gsl->nics_imissed;
	uint64_t last_tsc = gsl->tsc;

	if (input->reply) {
		char buf[128];
		snprintf(buf, sizeof(buf),
			 "%"PRIu64",%"PRIu64",%"PRIu64"\n",
			 tot, last_tsc, rte_get_tsc_hz());
		input->reply(input, buf, strlen(buf));
	}
	else {
		plog_info("imissed: %"PRIu64"\n", tot);
	}
	return 0;
}

static int parse_cmd_enable_multicast(const char *str, struct input *input)
{
	uint8_t port_id;
	prox_rte_ether_addr mac;

	if (sscanf(str, "%hhu %hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &port_id, mac.addr_bytes, mac.addr_bytes + 1, mac.addr_bytes + 2, mac.addr_bytes + 3, mac.addr_bytes + 4, mac.addr_bytes + 5 ) != 7) {
                return -1;
        }
	cmd_multicast(port_id, 1, &mac);
	return 0;
}

static int parse_cmd_disable_multicast(const char *str, struct input *input)
{
	uint8_t port_id;
	prox_rte_ether_addr mac;

	if (sscanf(str, "%hhu %hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &port_id, mac.addr_bytes, mac.addr_bytes + 1, mac.addr_bytes + 2, mac.addr_bytes + 3, mac.addr_bytes + 4, mac.addr_bytes + 5 ) != 7) {
                return -1;
        }

	cmd_multicast(port_id, 0, &mac);
	return 0;
}

static int parse_cmd_reset_port(const char *str, struct input *input)
{
	uint32_t port_id;

	if (sscanf(str, "%u", &port_id ) != 1) {
                return -1;
        }

	cmd_reset_port(port_id);
	return 0;
}

static int parse_cmd_write_reg(const char *str, struct input *input)
{
	uint32_t port_id;
	uint32_t id, val;

	if (sscanf(str, "%u %x %u", &port_id, &id, &val) != 3) {
                return -1;
        }

	cmd_write_reg(port_id, id, val);
	return 0;
}

static int parse_cmd_read_reg(const char *str, struct input *input)
{
	uint32_t port_id;
	uint32_t id;

	if (sscanf(str, "%u %x", &port_id, &id) != 2) {
                return -1;
        }

	cmd_read_reg(port_id, id);
	return 0;
}

static int parse_cmd_cache_reset(const char *str, struct input *input)
{
	cmd_cache_reset();
	return 0;
}

static int parse_cmd_set_cache_class_mask(const char *str, struct input *input)
{
	uint32_t lcore_id;
	uint32_t set;
	uint32_t val;

	if (sscanf(str, "%u %u %u", &lcore_id, &set, &val) != 3) {
                return -1;
        }

	cmd_set_cache_class_mask(lcore_id, set, val);
	return 0;
}

static int parse_cmd_set_cache_class(const char *str, struct input *input)
{
	uint32_t lcore_id;
	uint32_t set;

	if (sscanf(str, "%u %u", &lcore_id, &set) != 2) {
                return -1;
        }

	cmd_set_cache_class(lcore_id, set);
	return 0;
}

static int parse_cmd_get_cache_class_mask(const char *str, struct input *input)
{
	uint32_t lcore_id;
	uint32_t set;
	uint32_t val = 0;

	if (sscanf(str, "%u %u", &lcore_id, &set) != 2) {
                return -1;
        }

	cmd_get_cache_class_mask(lcore_id, set, &val);
	if (input->reply) {
		char buf[128];
		snprintf(buf, sizeof(buf), "%d, %d, %x\n", lcore_id, set, val);
		input->reply(input, buf, strlen(buf));
	} else {
		plog_info("core=%d, set=%d, mask=%x\n", lcore_id, set, val);
	}
	return 0;
}

static int parse_cmd_get_cache_class(const char *str, struct input *input)
{
	uint32_t lcore_id;
	uint32_t set;
	uint32_t val;

	if (sscanf(str, "%u", &lcore_id) != 1) {
                return -1;
        }

	cmd_get_cache_class(lcore_id, &set);
	if (input->reply) {
		char buf[128];
		snprintf(buf, sizeof(buf), "%d, %d\n", lcore_id, set);
		input->reply(input, buf, strlen(buf));
	} else {
		plog_info("core=%d, cos=%d\n", lcore_id, set);
	}
	return 0;
}

static int parse_cmd_get_cache_mask(const char *str, struct input *input)
{
	uint32_t lcore_id;
	uint32_t set;
	uint32_t mask;

	if (sscanf(str, "%u", &lcore_id) != 1) {
                return -1;
        }

	cmd_get_cache_class(lcore_id, &set);
	cmd_get_cache_class_mask(lcore_id, set, &mask);
	if (input->reply) {
		char buf[128];
		snprintf(buf, sizeof(buf), "%d, %x\n", lcore_id, mask);
		input->reply(input, buf, strlen(buf));
	} else {
		plog_info("core=%d, mask=%x\n", lcore_id, mask);
	}
	return 0;
}

static int parse_cmd_set_vlan_offload(const char *str, struct input *input)
{
	uint32_t port_id;
	uint32_t val;

	if (sscanf(str, "%u %u", &port_id, &val) != 2) {
                return -1;
        }

	cmd_set_vlan_offload(port_id, val);
	return 0;
}

static int parse_cmd_set_vlan_filter(const char *str, struct input *input)
{
	uint32_t port_id;
	uint32_t id, val;

	if (sscanf(str, "%u %d %u", &port_id, &id, &val) != 3) {
                return -1;
        }

	cmd_set_vlan_filter(port_id, id, val);
	return 0;
}

static int parse_cmd_ring_info_all(const char *str, struct input *input)
{
	if (strcmp(str, "") != 0) {
		return -1;
	}
	cmd_ringinfo_all();
	return 0;
}

static int parse_cmd_port_up(const char *str, struct input *input)
{
	unsigned val;

	if (sscanf(str, "%u", &val) != 1) {
		return -1;
	}

	cmd_port_up(val);
	return 0;
}

static int parse_cmd_port_down(const char *str, struct input *input)
{
	unsigned val;

	if (sscanf(str, "%u", &val) != 1) {
		return -1;
	}

	cmd_port_down(val);
	return 0;
}

static int parse_cmd_port_link_state(const char *str, struct input *input)
{
	unsigned val;

	if (sscanf(str, "%u", &val) != 1) {
		return -1;
	}

	if (!port_is_active(val))
		return -1;

	int active = prox_port_cfg[val].link_up;
	const char *state = active? "up\n" : "down\n";

	if (input->reply)
		input->reply(input, state, strlen(state));
	else
		plog_info("%s", state);

	return 0;
}

static int parse_cmd_xstats(const char *str, struct input *input)
{
	unsigned val;

	if (sscanf(str, "%u", &val) != 1) {
		return -1;
	}

	cmd_xstats(val);
	return 0;
}

static int parse_cmd_stats(const char *str, struct input *input)
{
	if (strcmp(str, "") == 0)
		return -1;

	char buf[32768];
	char ret2[32768];
	char *ret = ret2;
	int list = 0;

	prox_strncpy(buf, str, sizeof(buf) - 1);
	char *tok;
	uint64_t stat_val;

	while ((tok = strchr(str, ','))) {
		*tok = 0;
		stat_val = stats_parser_get(str);

		ret += sprintf(ret, "%s%"PRIu64"", list? "," :"", stat_val);
		list = 1;
		str = tok + 1;
	}

	stat_val = stats_parser_get(str);
	ret += sprintf(ret, "%s%"PRIu64"", list? "," :"", stat_val);

	sprintf(ret, "\n");

	if (input->reply)
		input->reply(input, ret2, strlen(ret2));
	else
		plog_info("%s", ret2);
	return 0;
}

static void replace_char(char *str, char to_replace, char by)
{
	for (size_t i = 0; str[i] != '\0'; ++i) {
		if (str[i] == to_replace)
			str[i] = by;
	}
}

static int parse_cmd_port_info(const char *str, struct input *input)
{
	int val;

	if (strcmp(str, "all") == 0) {
		val = -1;
	}
	else if (sscanf(str, "%d", &val) != 1) {
		return -1;
	}

	char port_info[2048];

	cmd_portinfo(val, port_info, sizeof(port_info));

	if (input->reply) {
		replace_char(port_info, '\n', ',');
		port_info[strlen(port_info) - 1] = '\n';
		input->reply(input, port_info, strlen(port_info));
	} else
		plog_info("%s", port_info);

	return 0;
}

static int parse_cmd_ring_info(const char *str, struct input *input)
{
	unsigned lcores[RTE_MAX_LCORE], task_id, nb_cores;

	if (parse_cores_task(str, lcores, &task_id, &nb_cores))
		return -1;

	if (cores_task_are_valid(lcores, task_id, nb_cores)) {
		for (unsigned int i = 0; i < nb_cores; i++) {
			cmd_ringinfo(lcores[i], task_id);
		}
	}
	return 0;
}

static int parse_cmd_port_stats(const char *str, struct input *input)
{
	unsigned val;

	if (sscanf(str, "%u", &val) != 1) {
		return -1;
	}

	struct get_port_stats s;
	if (stats_port(val, &s)) {
		plog_err("Invalid port %u\n", val);
		return 0;
	}
	char buf[256];
	snprintf(buf, sizeof(buf),
		 "%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64","
		 "%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64","
		 "%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64"\n",
		 s.no_mbufs_diff, s.ierrors_diff + s.imissed_diff,
		 s.rx_bytes_diff, s.tx_bytes_diff,
		 s.rx_pkts_diff, s.tx_pkts_diff,
		 s.rx_tot, s.tx_tot,
		 s.no_mbufs_tot, s.ierrors_tot + s.imissed_tot,
		 s.last_tsc, s.prev_tsc);
	plog_info("%s", buf);
	if (input->reply)
		input->reply(input, buf, strlen(buf));
	return 0;
}

static int parse_cmd_multi_port_stats(const char *str, struct input *input)
{
	uint32_t ports[PROX_MAX_PORTS];
	int nb_ports = parse_list_set(ports, str, PROX_MAX_PORTS);
	if (nb_ports <= 0) {
		return -1;
	}

	char buf[PROX_MAX_PORTS * (11+5*21) + 1], *pbuf = buf;
	int left = sizeof(buf);
	for (int i = 0; i < nb_ports; ++i) {
		struct get_port_stats s;
		if (stats_port(ports[i], &s)) {
			plog_err("Invalid port %u\n", ports[i]);
			return 0;
		}

		int len = snprintf(pbuf, left,
				"%u,"
				"%"PRIu64",%"PRIu64","
				"%"PRIu64",%"PRIu64","
				"%"PRIu64";",
				//TODO: adjust buf size above when adding fields
				ports[i],
				s.rx_tot, s.tx_tot,
				s.no_mbufs_tot, s.ierrors_tot + s.imissed_tot,
				s.last_tsc);
		if ((len < 0) || (len >= left)) {
			plog_err("Cannot print stats for port %u\n", ports[i]);
			return 0;
		}
		pbuf += len;
		left -= len;
	}
	pbuf--;
	*pbuf = '\n';

	plog_info("%s", buf);
	if (input->reply)
		input->reply(input, buf, sizeof(buf) - left);
	return 0;
}

static int parse_cmd_core_stats(const char *str, struct input *input)
{
	unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;

	if (parse_cores_task(str, lcores, &task_id, &nb_cores))
		return -1;

	if (cores_task_are_valid(lcores, task_id, nb_cores)) {
		for (unsigned int i = 0; i < nb_cores; i++) {
			lcore_id = lcores[i];
			uint64_t tot_rx = stats_core_task_tot_rx(lcore_id, task_id);
			uint64_t tot_tx = stats_core_task_tot_tx(lcore_id, task_id);
			uint64_t tot_drop = stats_core_task_tot_drop(lcore_id, task_id);
			uint64_t last_tsc = stats_core_task_last_tsc(lcore_id, task_id);

			if (input->reply) {
				char buf[128];
				snprintf(buf, sizeof(buf),
				 	"%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64"\n",
				 	tot_rx, tot_tx, tot_drop, last_tsc, rte_get_tsc_hz());
				input->reply(input, buf, strlen(buf));
			}
			else {
				plog_info("RX: %"PRIu64", TX: %"PRIu64", DROP: %"PRIu64"\n",
				  	tot_rx, tot_tx, tot_drop);
			}
		}
	}
	return 0;
}

static int parse_cmd_dp_core_stats(const char *str, struct input *input)
{
	unsigned lcores[RTE_MAX_LCORE], tasks[MAX_TASKS_PER_CORE], lcore_id, task_id, nb_cores, nb_tasks;

	// This function either outputs a single line, in case of syntax error on the lists of cores and/or tasks
	if (parse_cores_tasks(str, lcores, tasks, &nb_cores, &nb_tasks)) {
		if (input->reply) {
			char buf[128];
			snprintf(buf, sizeof(buf), "error: invalid syntax\n");
			input->reply(input, buf, strlen(buf));
		}
		return -1;
	}

	// or outputs (nb_cores * nb_tasks) lines, one line for each core/task pair:
	// - if the core/task pair is invalid, the output line reports an error
	// - otherwise, the output line provides the dataplane statistics for the core/task pair
	for (unsigned int i = 0; i < nb_cores; i++) {
		for (unsigned int j = 0; j < nb_tasks; j++) {
			lcore_id = lcores[i];
			task_id = tasks[j];
			if (core_task_is_valid(lcore_id, task_id) == 0) {
				if (input->reply) {
					char buf[128];
					snprintf(buf, sizeof(buf), "error: invalid core %u, task %u\n", lcore_id, task_id);
					input->reply(input, buf, strlen(buf));
				} else {
					plog_info("error: invalid core %u, task %u\n", lcore_id, task_id);
				}
				continue;
			}
			uint64_t tot_rx = stats_core_task_tot_rx(lcore_id, task_id);
			uint64_t tot_tx = stats_core_task_tot_tx(lcore_id, task_id);
			uint64_t tot_tx_fail = stats_core_task_tot_tx_fail(lcore_id, task_id);
			uint64_t tot_rx_non_dp = stats_core_task_tot_rx_non_dp(lcore_id, task_id);
			uint64_t tot_tx_non_dp = stats_core_task_tot_tx_non_dp(lcore_id, task_id);
			uint64_t tot_drop = stats_core_task_tot_drop(lcore_id, task_id);
			uint64_t last_tsc = stats_core_task_last_tsc(lcore_id, task_id);

			if (input->reply) {
				char buf[128];
				snprintf(buf, sizeof(buf),
					"%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%u,%u\n",
					tot_rx, tot_tx, tot_rx_non_dp, tot_tx_non_dp, tot_drop, tot_tx_fail, last_tsc, rte_get_tsc_hz(), lcore_id, task_id);
				input->reply(input, buf, strlen(buf));
			}
			else {
				plog_info("core: %u, task: %u, RX: %"PRIu64", TX: %"PRIu64", RX_NON_DP: %"PRIu64", TX_NON_DP: %"PRIu64", DROP: %"PRIu64", TX_FAIL: %"PRIu64"\n",
					lcore_id, task_id, tot_rx, tot_tx, tot_rx_non_dp, tot_tx_non_dp, tot_drop, tot_tx_fail);
			}
		}
	}
	return 0;
}

static int parse_cmd_lat_stats(const char *str, struct input *input)
{
	unsigned lcores[RTE_MAX_LCORE], tasks[MAX_TASKS_PER_CORE], lcore_id, task_id, nb_cores, nb_tasks;

	// This function either outputs a single line, in case of syntax error on the lists of cores and/or tasks
	if (parse_cores_tasks(str, lcores, tasks, &nb_cores, &nb_tasks)) {
		if (input->reply) {
			char buf[128];
			snprintf(buf, sizeof(buf), "error: invalid syntax\n");
			input->reply(input, buf, strlen(buf));
		}
		return -1;
	}

	// or outputs (nb_cores * nb_tasks) lines, one line for each core/task pair:
	// - if the core/task pair is invalid, the output line reports an error
	// - otherwise, the output line provides the latency statistics for the core/task pair
	for (unsigned int i = 0; i < nb_cores; i++) {
		for (unsigned int j = 0; j < nb_tasks; j++) {
			lcore_id = lcores[i];
			task_id = tasks[j];
			if (core_task_is_valid(lcore_id, task_id) == 0) {
				if (input->reply) {
					char buf[128];
					snprintf(buf, sizeof(buf), "error: invalid core %u, task %u\n", lcore_id, task_id);
					input->reply(input, buf, strlen(buf));
				} else {
					plog_info("error: invalid core %u, task %u\n", lcore_id, task_id);
				}
				continue;
			}
			if (!task_is_mode(lcore_id, task_id, "lat")) {
				if (input->reply) {
					char buf[128];
					snprintf(buf, sizeof(buf), "error: core %u task %u is not measuring latency\n", lcore_id, task_id);
					input->reply(input, buf, strlen(buf));
				} else {
					plog_info("error: core %u task %u is not measuring latency\n", lcore_id, task_id);
				}
				continue;
			}

			struct stats_latency *stats = stats_latency_find(lcore_id, task_id);
			struct stats_latency *tot = stats_latency_tot_find(lcore_id, task_id);
			if (!stats || !tot) {
				if (input->reply) {
					char buf[128];
					snprintf(buf, sizeof(buf),
						 "error: core %u task %u stats = %p tot = %p\n",
						 lcore_id, task_id, stats, tot);
					input->reply(input, buf, strlen(buf));
				} else {
					plog_info("error: core %u task %u stats = %p tot = %p\n",
						  lcore_id, task_id, stats, tot);
				}
				continue;
			}

			uint64_t last_tsc = stats_core_task_last_tsc(lcore_id, task_id);
			uint64_t lat_min_usec = time_unit_to_usec(&stats->min.time);
			uint64_t lat_max_usec = time_unit_to_usec(&stats->max.time);
			uint64_t tot_lat_min_usec = time_unit_to_usec(&tot->min.time);
			uint64_t tot_lat_max_usec = time_unit_to_usec(&tot->max.time);
			uint64_t lat_avg_usec = time_unit_to_usec(&stats->avg.time);

			if (input->reply) {
				char buf[128];
				snprintf(buf, sizeof(buf),
					 "%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%u,%u\n",
					 lat_min_usec,
					 lat_max_usec,
					 lat_avg_usec,
					 tot_lat_min_usec,
					 tot_lat_max_usec,
					 last_tsc,
					 rte_get_tsc_hz(),
					 lcore_id,
					 task_id);
				input->reply(input, buf, strlen(buf));
			}
			else {
				plog_info("core: %u, task: %u, min: %"PRIu64", max: %"PRIu64", avg: %"PRIu64", min since reset: %"PRIu64", max since reset: %"PRIu64"\n",
					  lcore_id,
					  task_id,
					  lat_min_usec,
					  lat_max_usec,
					  lat_avg_usec,
					  tot_lat_min_usec,
					  tot_lat_max_usec);
			}
		}
	}
	return 0;
}

static int parse_cmd_show_irq_buckets(const char *str, struct input *input)
{
	char buf[4096] = {0};
	unsigned int i, c;
	unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;

	if (parse_cores_task(str, lcores, &task_id, &nb_cores))
		return -1;

	if (cores_task_are_valid(lcores, task_id, nb_cores)) {
		for (c = 0; c < nb_cores; c++) {
			lcore_id = lcores[c];
			get_irq_buckets_by_core_task(buf, lcore_id, task_id);
			plog_info("%s", buf);
			if (input->reply)
				input->reply(input, buf, strlen(buf));
			buf[0] = 0;
		}
	}
	return 0;
}

static int parse_cmd_irq(const char *str, struct input *input)
{
	unsigned int i, c;
	unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;

	if (parse_cores_task(str, lcores, &task_id, &nb_cores))
		return -1;

	if (cores_task_are_valid(lcores, task_id, nb_cores)) {
		for (c = 0; c < nb_cores; c++) {
			lcore_id = lcores[c];
			if (!task_is_mode(lcore_id, task_id, "irq")) {
				plog_err("Core %u task %u is not in irq mode\n", lcore_id, task_id);
			} else {
				struct task_irq *task_irq = (struct task_irq *)(lcore_cfg[lcore_id].tasks_all[task_id]);

				task_irq_show_stats(task_irq, input);
			}
		}
	}
	return 0;
}

static void task_lat_show_latency_histogram(uint8_t lcore_id, uint8_t task_id, struct input *input)
{
#ifdef LATENCY_HISTOGRAM
	uint64_t *buckets;

	stats_core_lat_histogram(lcore_id, task_id, &buckets);

	if (buckets == NULL)
		return;

	if (input->reply) {
		char buf[4096] = {0};
		for (size_t i = 0; i < 128; i++)
			sprintf(buf+strlen(buf), "Bucket [%zu]: %"PRIu64"\n", i, buckets[i]);
		input->reply(input, buf, strlen(buf));
	}
	else {
		for (size_t i = 0; i < 128; i++)
			if (buckets[i])
				plog_info("Bucket [%zu]: %"PRIu64"\n", i, buckets[i]);
	}
#else
	plog_info("LATENCY_DETAILS disabled\n");
#endif
}

static int parse_cmd_lat_packets(const char *str, struct input *input)
{
	unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;

	if (parse_cores_task(str, lcores, &task_id, &nb_cores))
		return -1;

	if (cores_task_are_valid(lcores, task_id, nb_cores)) {
		for (unsigned int i = 0; i < nb_cores; i++) {
			lcore_id = lcores[i];
			if (!task_is_mode(lcore_id, task_id, "lat")) {
				plog_err("Core %u task %u is not measuring latency\n", lcore_id, task_id);
			}
			else {
				task_lat_show_latency_histogram(lcore_id, task_id, input);
			}
		}
	}
	return 0;
}

static int parse_cmd_cgnat_public_hash(const char *str, struct input *input)
{
	unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;

	if (parse_cores_task(str, lcores, &task_id, &nb_cores))
		return -1;

	if (cores_task_are_valid(lcores, task_id, nb_cores)) {
		for (unsigned int i = 0; i < nb_cores; i++) {
			lcore_id = lcores[i];

			if (!task_is_mode(lcore_id, task_id, "cgnat")) {
				plog_err("Core %u task %u is not cgnat\n", lcore_id, task_id);
			}
			else {
				struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
				task_cgnat_dump_public_hash((struct task_nat *)tbase);
			}
		}
	}
	return 0;
}

static int parse_cmd_cgnat_private_hash(const char *str, struct input *input)
{
	unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;
	uint32_t val;

	if (parse_cores_task(str, lcores, &task_id, &nb_cores))
		return -1;

	if (cores_task_are_valid(lcores, task_id, nb_cores)) {
		for (unsigned int i = 0; i < nb_cores; i++) {
			lcore_id = lcores[i];

			if (!task_is_mode(lcore_id, task_id, "cgnat")) {
				plog_err("Core %u task %u is not cgnat\n", lcore_id, task_id);
			}
			else {
				struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
				task_cgnat_dump_private_hash((struct task_nat *)tbase);
			}
		}
	}
	return 0;
}

static int parse_cmd_accuracy(const char *str, struct input *input)
{
	unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;
	uint32_t val;

	if (parse_cores_task(str, lcores, &task_id, &nb_cores))
		return -1;
	if (!(str = strchr_skip_twice(str, ' ')))
		return -1;
	if (sscanf(str, "%"PRIu32"", &val) != 1)
		return -1;

	if (cores_task_are_valid(lcores, task_id, nb_cores)) {
		for (unsigned int i = 0; i < nb_cores; i++) {
			lcore_id = lcores[i];

			if (!task_is_mode(lcore_id, task_id, "lat")) {
				plog_err("Core %u task %u is not measuring latency\n", lcore_id, task_id);
			}
			else {
				struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];

				task_lat_set_accuracy_limit((struct task_lat *)tbase, val);
			}
		}
	}
	return 0;
}

static int parse_cmd_leave_igmp(const char *str, struct input *input)
{
	unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;

	if (parse_cores_task(str, lcores, &task_id, &nb_cores))
		return -1;

	if (cores_task_are_valid(lcores, task_id, nb_cores)) {
		for (unsigned int i = 0; i < nb_cores; i++) {
			lcore_id = lcores[i];

			if (!task_is_mode(lcore_id, task_id, "swap")) {
				plog_err("Core %u task %u is not running swap\n", lcore_id, task_id);
			}
			else {
				struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
				igmp_leave_group(tbase);
			}
		}
	}
	return 0;
}

static int parse_cmd_join_igmp(const char *str, struct input *input)
{
	unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;
	uint32_t igmp_ip;
	uint8_t *igmp_bytes = (uint8_t *)&igmp_ip;

	if (parse_cores_task(str, lcores, &task_id, &nb_cores))
		return -1;
	if (!(str = strchr_skip_twice(str, ' ')))
		return -1;
	if (sscanf(str, "%hhu.%hhu.%hhu.%hhu", igmp_bytes, igmp_bytes + 1, igmp_bytes + 2, igmp_bytes + 3) != 4) {
		return -1;
	}
	if (cores_task_are_valid(lcores, task_id, nb_cores)) {
		for (unsigned int i = 0; i < nb_cores; i++) {
			lcore_id = lcores[i];

			if (!task_is_mode(lcore_id, task_id, "swap")) {
				plog_err("Core %u task %u is not running swap\n", lcore_id, task_id);
			}
			else {
				struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
				igmp_join_group(tbase, igmp_ip);
			}
		}
	}
	return 0;
}

static int parse_cmd_rx_tx_info(const char *str, struct input *input)
{
	if (strcmp(str, "") != 0) {
		return -1;
	}

	cmd_rx_tx_info();
	return 0;
}

static int parse_cmd_version(const char *str, struct input *input)
{
	if (strcmp(str, "") != 0) {
		return -1;
	}

	if (input->reply) {
		uint64_t version =
			((uint64_t)VERSION_MAJOR) << 24 |
			((uint64_t)VERSION_MINOR) << 16 |
			((uint64_t)VERSION_REV) << 8;

		char buf[128];
		snprintf(buf, sizeof(buf), "%"PRIu64",%"PRIu64"\n", version, (uint64_t)RTE_VERSION);
		input->reply(input, buf, strlen(buf));
	}
	else {
		plog_info("prox version: %d.%d, DPDK version: %s\n",
			  VERSION_MAJOR, VERSION_MINOR,
			  rte_version() + sizeof(RTE_VER_PREFIX));
	}
	return 0;
}

struct cmd_str {
	const char *cmd;
	const char *args;
	const char *help;
	int (*parse)(const char *args, struct input *input);
};

static int parse_cmd_help(const char *str, struct input *input);

static struct cmd_str cmd_strings[] = {
	{"history", "", "Print command history", parse_cmd_history},
	{"echo", "", "echo parameter, useful to resolving variables", parse_cmd_echo},
	{"quit", "", "Stop all cores and quit", parse_cmd_quit},
	{"quit_force", "", "Quit without waiting on cores to stop", parse_cmd_quit_force},
	{"help", "<substr>", "Show list of commands that have <substr> as a substring. If no substring is provided, all commands are shown.", parse_cmd_help},
	{"verbose", "<level>", "Set verbosity level", parse_cmd_verbose},
	{"thread info", "<core_id> <task_id>", "", parse_cmd_thread_info},
	{"mem info", "", "Show information about system memory (number of huge pages and addresses of these huge pages)", parse_cmd_mem_info},
	{"update interval", "<value>", "Update statistics refresh rate, in msec (must be >=10). Default is 1 second", parse_cmd_update_interval},
	{"rx tx info", "", "Print connections between tasks on all cores", parse_cmd_rx_tx_info},
	{"start", "<core list>|all <task_id>", "Start core <core_id> or all cores", parse_cmd_start},
	{"stop", "<core list>|all <task_id>", "Stop core <core id> or all cores", parse_cmd_stop},

	{"dump", "<core id> <task id> <nb packets>", "Create a hex dump of <nb_packets> from <task_id> on <core_id> showing how packets have changed between RX and TX.", parse_cmd_trace},
	{"dump_rx", "<core id> <task id> <nb packets>", "Create a hex dump of <nb_packets> from <task_id> on <core_id> at RX", parse_cmd_dump_rx},
	{"dump_tx", "<core id> <task id> <nb packets>", "Create a hex dump of <nb_packets> from <task_id> on <core_id> at TX", parse_cmd_dump_tx},
	{"rx distr start", "", "Start gathering statistical distribution of received packets", parse_cmd_rx_distr_start},
	{"rx distr stop", "", "Stop gathering statistical distribution of received packets", parse_cmd_rx_distr_stop},
	{"rx distr reset", "", "Reset gathered statistical distribution of received packets", parse_cmd_rx_distr_reset},
	{"rx distr show", "", "Display gathered statistical distribution of received packets", parse_cmd_rx_distr_show},
	{"tx distr start", "", "Start gathering statistical distribution of xmitted packets", parse_cmd_tx_distr_start},
	{"tx distr stop", "", "Stop gathering statistical distribution of xmitted packets", parse_cmd_tx_distr_stop},
	{"tx distr reset", "", "Reset gathered statistical distribution of xmitted packets", parse_cmd_tx_distr_reset},
	{"tx distr show", "", "Display gathered statistical distribution of xmitted packets", parse_cmd_tx_distr_show},

	{"rate", "<port id> <queue id> <rate>", "rate does not include preamble, SFD and IFG", parse_cmd_rate},
	{"count","<core id> <task id> <count>", "Generate <count> packets", parse_cmd_count},
	{"bypass", "<core_id> <task_id>", "Bypass task", parse_cmd_bypass},
	{"reconnect", "<core_id> <task_id>", "Reconnect task", parse_cmd_reconnect},
	{"pkt_size", "<core_id> <task_id> <pkt_size>", "Set the packet size to <pkt_size>", parse_cmd_pkt_size},
	{"speed", "<core_id> <task_id> <speed percentage>", "Change the speed to <speed percentage> at which packets are being generated on core <core_id> in task <task_id>.", parse_cmd_speed},
	{"speed_byte", "<core_id> <task_id> <speed>", "Change speed to <speed>. The speed is specified in units of bytes per second.", parse_cmd_speed_byte},
	{"set value", "<core_id> <task_id> <offset> <value> <value_len>", "Set <value_len> bytes to <value> at offset <offset> in packets generated on <core_id> <task_id>", parse_cmd_set_value},
	{"set random", "<core_id> <task_id> <offset> <random_str> <value_len>", "Set <value_len> bytes to <rand_str> at offset <offset> in packets generated on <core_id> <task_id>", parse_cmd_set_random},
	{"reset values all", "", "Undo all \"set value\" commands on all cores/tasks", parse_cmd_reset_values_all},
	{"reset randoms all", "", "Undo all \"set random\" commands on all cores/tasks", parse_cmd_reset_randoms_all},
	{"reset values", "<core id> <task id>", "Undo all \"set value\" commands on specified core/task", parse_cmd_reset_values},

	{"arp add", "<core id> <task id> <port id> <gre id> <svlan> <cvlan> <ip addr> <mac addr> <user>", "Add a single ARP entry into a CPE table on <core id>/<task id>.", parse_cmd_arp_add},
	{"rule add", "<core id> <task id> svlan_id&mask cvlan_id&mask ip_proto&mask source_ip/prefix destination_ip/prefix range dport_range action", "Add a rule to the ACL table on <core id>/<task id>", parse_cmd_rule_add},
	{"route add", "<core id> <task id> <ip/prefix> <next hop id>", "Add a route to the routing table on core <core id> <task id>. Example: route add 10.0.16.0/24 9", parse_cmd_route_add},
	{"gateway ip", "<core id> <task id> <ip>", "Define/Change IP address of destination gateway on core <core id> <task id>.", parse_cmd_gateway_ip},
	{"local ip", "<core id> <task id> <ip>", "Define/Change IP address of destination gateway on core <core id> <task id>.", parse_cmd_local_ip},

	{"pps unit", "", "Change core stats pps unit", parse_cmd_pps_unit},
	{"reset stats", "", "Reset all statistics", parse_cmd_reset_stats},
	{"reset lat stats", "", "Reset all latency statistics", parse_cmd_reset_lat_stats},
	{"tot stats", "", "Print total RX and TX packets", parse_cmd_tot_stats},
	{"tot ierrors tot", "", "Print total number of ierrors since reset", parse_cmd_tot_ierrors_tot},
	{"tot imissed tot", "", "Print total number of imissed since reset", parse_cmd_tot_imissed_tot},
	{"lat stats", "<core id> <task id>", "Print min,max,avg latency as measured during last sampling interval", parse_cmd_lat_stats},
	{"irq stats", "<core id> <task id>", "Print irq related infos", parse_cmd_irq},
	{"show irq buckets", "<core id> <task id>", "Print irq buckets", parse_cmd_show_irq_buckets},
	{"lat packets", "<core id> <task id>", "Print the latency for each of the last set of packets", parse_cmd_lat_packets},
	{"accuracy limit", "<core id> <task id> <nsec>", "Only consider latency of packets that were measured with an error no more than <nsec>", parse_cmd_accuracy},
	{"core stats", "<core id> <task id>", "Print rx/tx/drop for task <task id> running on core <core id>", parse_cmd_core_stats},
	{"dp core stats", "<core id> <task id>", "Print rx/tx/non_dp_rx/non_dp_tx/drop for task <task id> running on core <core id>", parse_cmd_dp_core_stats},
	{"port_stats", "<port id>", "Print rate for no_mbufs, ierrors + imissed, rx_bytes, tx_bytes, rx_pkts, tx_pkts; totals for RX, TX, no_mbufs, ierrors + imissed for port <port id>", parse_cmd_port_stats},
	{"multi port stats", "<port list>", "Get stats for multiple ports, semi-colon separated: port id, total for rx_pkts, tx_pkts, no_mbufs, ierrors + imissed, last_tsc", parse_cmd_multi_port_stats},
	{"read reg", "", "Read register", parse_cmd_read_reg},
	{"write reg", "", "Read register", parse_cmd_write_reg},
	{"set vlan offload", "", "Set Vlan offload", parse_cmd_set_vlan_offload},
	{"set vlan filter", "", "Set Vlan filter", parse_cmd_set_vlan_filter},
	{"reset cache", "", "Reset cache", parse_cmd_cache_reset},
	{"set cache class mask", "<core id> <class> <mask>", "Set cache class mask for <core id>", parse_cmd_set_cache_class_mask},
	{"get cache class mask", "<core id> <class>", "Get cache class mask", parse_cmd_get_cache_class_mask},
	{"set cache class", "<core id> <class>", "Set cache class", parse_cmd_set_cache_class},
	{"get cache class", "<core id>", "Get cache class", parse_cmd_get_cache_class},
	{"get cache mask", "<core id>", "Get cache mask", parse_cmd_get_cache_mask},
	{"reset port", "<port id>", "Reset port", parse_cmd_reset_port},
	{"enable multicast", "<port id> <MAC>", "Enable multicast", parse_cmd_enable_multicast},
	{"disable multicast", "<port id> <MAC>", "Disable multicast", parse_cmd_disable_multicast},
	{"ring info all", "", "Get information about ring, such as ring size and number of elements in the ring", parse_cmd_ring_info_all},
	{"ring info", "<core id> <task id>", "Get information about ring on core <core id> in task <task id>, such as ring size and number of elements in the ring", parse_cmd_ring_info},
	{"port info", "<port id> [brief?]", "Get port related information, such as MAC address, socket, number of descriptors..., . Adding \"brief\" after command prints short version of output.", parse_cmd_port_info},
	{"port up", "<port id>", "Set the port up", parse_cmd_port_up},
	{"port down", "<port id>", "Set the port down", parse_cmd_port_down},
	{"port link state", "<port id>", "Get link state (up or down) for port", parse_cmd_port_link_state},
	{"port xstats", "<port id>", "Get extra statistics for the port", parse_cmd_xstats},
	{"stats", "<stats_path>", "Get stats as specified by <stats_path>. A comma-separated list of <stats_path> can be supplied", parse_cmd_stats},
	{"cgnat dump public hash", "<core id> <task id>", "Dump cgnat public hash table", parse_cmd_cgnat_public_hash},
	{"cgnat dump private hash", "<core id> <task id>", "Dump cgnat private hash table", parse_cmd_cgnat_private_hash},
	{"delay_us", "<core_id> <task_id> <delay_us>", "Set the delay in usec for the impair mode to <delay_us>", parse_cmd_delay_us},
	{"random delay_us", "<core_id> <task_id> <random delay_us>", "Set the delay in usec for the impair mode to <random delay_us>", parse_cmd_random_delay_us},
	{"probability", "<core_id> <task_id> <probability>", "Set the percent of forwarded packets for the impair mode", parse_cmd_set_probability},
	{"version", "", "Show version", parse_cmd_version},
	{"join igmp", "<core_id> <task_id> <ip>", "Send igmp membership report for group <ip>", parse_cmd_join_igmp},
	{"leave igmp", "<core_id> <task_id>", "Send igmp leave group", parse_cmd_leave_igmp},
	{0,0,0,0},
};

static int parse_cmd_help(const char *str, struct input *input)
{
	/* str contains the arguments, all commands that have str as a
	   substring will be shown. */
	size_t len, len2, longest_cmd = 0;
	for (size_t i = 0; i < cmd_parser_n_cmd(); ++i) {
		if (longest_cmd <strlen(cmd_strings[i].cmd))
			longest_cmd = strlen(cmd_strings[i].cmd);
	}
	/* A single call to log will be executed after the help string
	   has been built. The reason for this is to make use of the
	   built-in pager. */
	char buf[32768] = {0};

	for (size_t i = 0; i < cmd_parser_n_cmd(); ++i) {
		int is_substr = 0;
		const size_t cmd_len = strlen(cmd_strings[i].cmd);
		for (size_t j = 0; j < cmd_len; ++j) {
			is_substr = 1;
			for (size_t k = 0; k < strlen(str); ++k) {
				if (str[k] != (cmd_strings[i].cmd + j)[k]) {
					is_substr = 0;
					break;
				}
			}
			if (is_substr)
				break;
		}
		if (!is_substr)
			continue;

		snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%s", cmd_strings[i].cmd);
		len = strlen(cmd_strings[i].cmd);
		while (len < longest_cmd) {
			len++;
			snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " ");
		}

		if (strlen(cmd_strings[i].args)) {
			char tmp[256] = {0};
			prox_strncpy(tmp, cmd_strings[i].args, 128);
			snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "Arguments: %s\n", tmp);
			len2 = len;
			if (strlen(cmd_strings[i].help)) {
				while (len2) {
					len2--;
			snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " ");
				}
			}
		}

		if (strlen(cmd_strings[i].help)) {
			int add = 0;
			const char *h = cmd_strings[i].help;
			do {
				if (add) {
					len2 = len;
					while (len2) {
						len2--;
						snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " ");
					}
				}
				char tmp[128] = {0};
				const size_t max_len = strlen(h) > 80? 80 : strlen(h);
				size_t len3 = max_len;
				if (len3 == 80) {
					while (len3 && h[len3] != ' ')
						len3--;
					if (len3 == 0)
						len3 = max_len;
				}

				// Use strncpy here and not prox_strncpy. The dest (tmp) has been initialized with 0.
				// The fact that we are copying 80 characters potentially not null terminated is hence not an issue.
				// Using prox_strncpy here might cause a PROX_PANIC
				strncpy(tmp, h, len3);
				h += len3;
				while (h[0] == ' ' && strlen(h))
					h++;

				snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%s\n", tmp);
				add = 1;
			} while(strlen(h));
		}
		if (strlen(cmd_strings[i].help) == 0&& strlen(cmd_strings[i].args) == 0) {
			snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "\n");
		}
	}
	plog_info("%s", buf);

	return 0;
}

const char *cmd_parser_cmd(size_t i)
{
	i = i < cmd_parser_n_cmd()? i: cmd_parser_n_cmd();
	return cmd_strings[i].cmd;
}

size_t cmd_parser_n_cmd(void)
{
	return sizeof(cmd_strings)/sizeof(cmd_strings[0]) - 1;
}

void cmd_parser_parse(const char *str, struct input *input)
{
	size_t skip;

	for (size_t i = 0; i < cmd_parser_n_cmd(); ++i) {
		skip = strlen(cmd_strings[i].cmd);
		if (strncmp(cmd_strings[i].cmd, str, skip) == 0 &&
		    (str[skip] == ' ' || str[skip] == 0)) {
			while (str[skip] == ' ')
				skip++;

			if (cmd_strings[i].parse(str + skip, input) != 0) {
				plog_warn("Invalid syntax for command '%s': %s %s\n",
					  cmd_strings[i].cmd, cmd_strings[i].args, cmd_strings[i].help);
			}
			return ;
		}
	}

	plog_err("Unknown command: '%s'\n", str);
}