summaryrefslogtreecommitdiffstats
path: root/kernel/drivers/tty/moxa.h
blob: 87d16ce57be721e1d7636e4e97129dcc97ee7806 (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
#ifndef MOXA_H_FILE
#define MOXA_H_FILE

#define	MOXA		0x400
#define MOXA_GET_IQUEUE 	(MOXA + 1)	/* get input buffered count */
#define MOXA_GET_OQUEUE 	(MOXA + 2)	/* get output buffered count */
#define MOXA_GETDATACOUNT       (MOXA + 23)
#define MOXA_GET_IOQUEUE	(MOXA + 27)
#define MOXA_FLUSH_QUEUE	(MOXA + 28)
#define MOXA_GETMSTATUS         (MOXA + 65)

/*
 *    System Configuration
 */

#define Magic_code	0x404

/*
 *    for C218 BIOS initialization
 */
#define C218_ConfBase	0x800
#define C218_status	(C218_ConfBase + 0)	/* BIOS running status    */
#define C218_diag	(C218_ConfBase + 2)	/* diagnostic status      */
#define C218_key	(C218_ConfBase + 4)	/* WORD (0x218 for C218) */
#define C218DLoad_len	(C218_ConfBase + 6)	/* WORD           */
#define C218check_sum	(C218_ConfBase + 8)	/* BYTE           */
#define C218chksum_ok	(C218_ConfBase + 0x0a)	/* BYTE (1:ok)            */
#define C218_TestRx	(C218_ConfBase + 0x10)	/* 8 bytes for 8 ports    */
#define C218_TestTx	(C218_ConfBase + 0x18)	/* 8 bytes for 8 ports    */
#define C218_RXerr	(C218_ConfBase + 0x20)	/* 8 bytes for 8 ports    */
#define C218_ErrFlag	(C218_ConfBase + 0x28)	/* 8 bytes for 8 ports    */

#define C218_LoadBuf	0x0F00
#define C218_KeyCode	0x218
#define CP204J_KeyCode	0x204

/*
 *    for C320 BIOS initialization
 */
#define C320_ConfBase	0x800
#define C320_LoadBuf	0x0f00
#define STS_init	0x05	/* for C320_status        */

#define C320_status	C320_ConfBase + 0	/* BIOS running status    */
#define C320_diag	C320_ConfBase + 2	/* diagnostic status      */
#define C320_key	C320_ConfBase + 4	/* WORD (0320H for C320) */
#define C320DLoad_len	C320_ConfBase + 6	/* WORD           */
#define C320check_sum	C320_ConfBase + 8	/* WORD           */
#define C320chksum_ok	C320_ConfBase + 0x0a	/* WORD (1:ok)            */
#define C320bapi_len	C320_ConfBase + 0x0c	/* WORD           */
#define C320UART_no	C320_ConfBase + 0x0e	/* WORD           */

#define C320_KeyCode	0x320

#define FixPage_addr	0x0000	/* starting addr of static page  */
#define DynPage_addr	0x2000	/* starting addr of dynamic page */
#define C218_start	0x3000	/* starting addr of C218 BIOS prg */
#define Control_reg	0x1ff0	/* select page and reset control */
#define HW_reset	0x80

/*
 *    Function Codes
 */
#define FC_CardReset	0x80
#define FC_ChannelReset 1	/* C320 firmware not supported */
#define FC_EnableCH	2
#define FC_DisableCH	3
#define FC_SetParam	4
#define FC_SetMode	5
#define FC_SetRate	6
#define FC_LineControl	7
#define FC_LineStatus	8
#define FC_XmitControl	9
#define FC_FlushQueue	10
#define FC_SendBreak	11
#define FC_StopBreak	12
#define FC_LoopbackON	13
#define FC_LoopbackOFF	14
#define FC_ClrIrqTable	15
#define FC_SendXon	16
#define FC_SetTermIrq	17	/* C320 firmware not supported */
#define FC_SetCntIrq	18	/* C320 firmware not supported */
#define FC_SetBreakIrq	19
#define FC_SetLineIrq	20
#define FC_SetFlowCtl	21
#define FC_GenIrq	22
#define FC_InCD180	23
#define FC_OutCD180	24
#define FC_InUARTreg	23
#define FC_OutUARTreg	24
#define FC_SetXonXoff	25
#define FC_OutCD180CCR	26
#define FC_ExtIQueue	27
#define FC_ExtOQueue	28
#define FC_ClrLineIrq	29
#define FC_HWFlowCtl	30
#define FC_GetClockRate 35
#define FC_SetBaud	36
#define FC_SetDataMode  41
#define FC_GetCCSR      43
#define FC_GetDataError 45
#define FC_RxControl	50
#define FC_ImmSend	51
#define FC_SetXonState	52
#define FC_SetXoffState	53
#define FC_SetRxFIFOTrig 54
#define FC_SetTxFIFOCnt 55
#define FC_UnixRate	56
#define FC_UnixResetTimer 57

#define	RxFIFOTrig1	0
#define	RxFIFOTrig4	1
#define	RxFIFOTrig8	2
#define	RxFIFOTrig14	3

/*
 *    Dual-Ported RAM
 */
#define DRAM_global	0
#define INT_data	(DRAM_global + 0)
#define Config_base	(DRAM_global + 0x108)

#define IRQindex	(INT_data + 0)
#define IRQpending	(INT_data + 4)
#define IRQtable	(INT_data + 8)

/*
 *    Interrupt Status
 */
#define IntrRx		0x01	/* receiver data O.K.             */
#define IntrTx		0x02	/* transmit buffer empty  */
#define IntrFunc	0x04	/* function complete              */
#define IntrBreak	0x08	/* received break         */
#define IntrLine	0x10	/* line status change
				   for transmitter                */
#define IntrIntr	0x20	/* received INTR code             */
#define IntrQuit	0x40	/* received QUIT code             */
#define IntrEOF 	0x80	/* received EOF code              */

#define IntrRxTrigger 	0x100	/* rx data count reach tigger value */
#define IntrTxTrigger 	0x200	/* tx data count below trigger value */

#define Magic_no	(Config_base + 0)
#define Card_model_no	(Config_base + 2)
#define Total_ports	(Config_base + 4)
#define Module_cnt	(Config_base + 8)
#define Module_no	(Config_base + 10)
#define Timer_10ms	(Config_base + 14)
#define Disable_IRQ	(Config_base + 20)
#define TMS320_PORT1	(Config_base + 22)
#define TMS320_PORT2	(Config_base + 24)
#define TMS320_CLOCK	(Config_base + 26)

/*
 *    DATA BUFFER in DRAM
 */
#define Extern_table	0x400	/* Base address of the external table
				   (24 words *    64) total 3K bytes
				   (24 words * 128) total 6K bytes */
#define Extern_size	0x60	/* 96 bytes                       */
#define RXrptr		0x00	/* read pointer for RX buffer     */
#define RXwptr		0x02	/* write pointer for RX buffer    */
#define TXrptr		0x04	/* read pointer for TX buffer     */
#define TXwptr		0x06	/* write pointer for TX buffer    */
#define HostStat	0x08	/* IRQ flag and general flag      */
#define FlagStat	0x0A
#define FlowControl	0x0C	/* B7 B6 B5 B4 B3 B2 B1 B0              */
				/*  x  x  x  x  |  |  |  |            */
				/*              |  |  |  + CTS flow   */
				/*              |  |  +--- RTS flow   */
				/*              |  +------ TX Xon/Xoff */
				/*              +--------- RX Xon/Xoff */
#define Break_cnt	0x0E	/* received break count   */
#define CD180TXirq	0x10	/* if non-0: enable TX irq        */
#define RX_mask 	0x12
#define TX_mask 	0x14
#define Ofs_rxb 	0x16
#define Ofs_txb 	0x18
#define Page_rxb	0x1A
#define Page_txb	0x1C
#define EndPage_rxb	0x1E
#define EndPage_txb	0x20
#define Data_error	0x22
#define RxTrigger	0x28
#define TxTrigger	0x2a

#define rRXwptr 	0x34
#define Low_water	0x36

#define FuncCode	0x40
#define FuncArg 	0x42
#define FuncArg1	0x44

#define C218rx_size	0x2000	/* 8K bytes */
#define C218tx_size	0x8000	/* 32K bytes */

#define C218rx_mask	(C218rx_size - 1)
#define C218tx_mask	(C218tx_size - 1)

#define C320p8rx_size	0x2000
#define C320p8tx_size	0x8000
#define C320p8rx_mask	(C320p8rx_size - 1)
#define C320p8tx_mask	(C320p8tx_size - 1)

#define C320p16rx_size	0x2000
#define C320p16tx_size	0x4000
#define C320p16rx_mask	(C320p16rx_size - 1)
#define C320p16tx_mask	(C320p16tx_size - 1)

#define C320p24rx_size	0x2000
#define C320p24tx_size	0x2000
#define C320p24rx_mask	(C320p24rx_size - 1)
#define C320p24tx_mask	(C320p24tx_size - 1)

#define C320p32rx_size	0x1000
#define C320p32tx_size	0x1000
#define C320p32rx_mask	(C320p32rx_size - 1)
#define C320p32tx_mask	(C320p32tx_size - 1)

#define Page_size	0x2000U
#define Page_mask	(Page_size - 1)
#define C218rx_spage	3
#define C218tx_spage	4
#define C218rx_pageno	1
#define C218tx_pageno	4
#define C218buf_pageno	5

#define C320p8rx_spage	3
#define C320p8tx_spage	4
#define C320p8rx_pgno	1
#define C320p8tx_pgno	4
#define C320p8buf_pgno	5

#define C320p16rx_spage 3
#define C320p16tx_spage 4
#define C320p16rx_pgno	1
#define C320p16tx_pgno	2
#define C320p16buf_pgno 3

#define C320p24rx_spage 3
#define C320p24tx_spage 4
#define C320p24rx_pgno	1
#define C320p24tx_pgno	1
#define C320p24buf_pgno 2

#define C320p32rx_spage 3
#define C320p32tx_ofs	C320p32rx_size
#define C320p32tx_spage 3
#define C320p32buf_pgno 1

/*
 *    Host Status
 */
#define WakeupRx	0x01
#define WakeupTx	0x02
#define WakeupBreak	0x08
#define WakeupLine	0x10
#define WakeupIntr	0x20
#define WakeupQuit	0x40
#define WakeupEOF	0x80	/* used in VTIME control */
#define WakeupRxTrigger	0x100
#define WakeupTxTrigger	0x200
/*
 *    Flag status
 */
#define Rx_over		0x01
#define Xoff_state	0x02
#define Tx_flowOff	0x04
#define Tx_enable	0x08
#define CTS_state	0x10
#define DSR_state	0x20
#define DCD_state	0x80
/*
 *    FlowControl
 */
#define CTS_FlowCtl	1
#define RTS_FlowCtl	2
#define Tx_FlowCtl	4
#define Rx_FlowCtl	8
#define IXM_IXANY	0x10

#define LowWater	128

#define DTR_ON		1
#define RTS_ON		2
#define CTS_ON		1
#define DSR_ON		2
#define DCD_ON		8

/* mode definition */
#define	MX_CS8		0x03
#define	MX_CS7		0x02
#define	MX_CS6		0x01
#define	MX_CS5		0x00

#define	MX_STOP1	0x00
#define	MX_STOP15	0x04
#define	MX_STOP2	0x08

#define	MX_PARNONE	0x00
#define	MX_PAREVEN	0x40
#define	MX_PARODD	0xC0

#endif
/a> 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 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 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374
/*
// Copyright (c) 2010-2020 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(lcore_id, task_id, "gen")) {
				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(lcore_id, task_id, "impair")) {
				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(lcore_id, task_id, "impair")) {
				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(lcore_id, task_id, "impair")) {
				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(lcore_id, task_id, "gen")) {
				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_imix(const char *str, struct input *input)
{
	unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;
	uint32_t pkt_sizes[MAX_IMIX_PKTS], tmp;
	uint32_t pkt_index = 0;

	if (parse_cores_task(str, lcores, &task_id, &nb_cores))
		return -1;
	if (!(str = strchr_skip_twice(str, ' ')))
		return -1;
	while (pkt_index < MAX_IMIX_PKTS) {
		if (sscanf(str, "%d", &pkt_sizes[pkt_index]) != 1)
			break;
		pkt_index++;
		if ((str = strchr(str, ',')) == NULL)
			break;
        	str = str + 1;
	}
	if (pkt_index == 0) {
		plog_err("No pkt size found\n");
		return -1;
	}
	if ((pkt_index == MAX_IMIX_PKTS) && (str) && (sscanf(str, "%d", &tmp) == 1)) {
		plog_err("Too many inputs - unexpected inputs starting at %s\n", str);
		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_imix(tbase, pkt_index, pkt_sizes); /* 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(lcore_id, task_id, "gen")) {
			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(lcore_id, task_id, "gen")) {
				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(lcore_id, task_id, "gen")) {
				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(lcore_id, task_id, "gen")) {
				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(lcore_id, task_id, "gen")) {
				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(lcore_id, task_id, "gen")) {
				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(lcore_id, task_id, "gen")) {
				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;
}

typedef void (*parser_handler)(unsigned, unsigned, struct input *);
static int handle_cores_tasks(const char *str, struct input *input, const char *mode_str, const char *mode_name, parser_handler f)
{
	// This function either outputs a single line, in case of syntax error on the lists of cores and/or tasks
	// 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

	unsigned lcores[RTE_MAX_LCORE], tasks[MAX_TASKS_PER_CORE], lcore_id, task_id, nb_cores, nb_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;
	}

	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 ((mode_str) && (!task_is_mode(lcore_id, task_id, mode_str))) {
				if (input->reply) {
					char buf[128];
					snprintf(buf, sizeof(buf), "error: core %u task %u is not measuring %s\n", lcore_id, task_id, mode_name);
					input->reply(input, buf, strlen(buf));
				} else {
					plog_info("error: core %u task %u is not measuring %s\n", lcore_id, task_id, mode_name);
				}
				continue;
			}
			f(lcore_id, task_id, input);
		}
	}
	return 0;
}

static void handle_dp_core_stats(unsigned lcore_id, unsigned task_id, struct input *input)
{
	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);
	}
}

static void handle_lat_stats(unsigned lcore_id, unsigned task_id, struct input *input)
{
	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);
		}
		return;
	}

	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);
	}
}

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

	stats_core_lat_histogram(lcore_id, task_id, &buckets);

	if (buckets == NULL) {
		if (input->reply) {
			char buf[128];
			snprintf(buf, sizeof(buf), "error: unexpected NULL bucket\n");
			input->reply(input, buf, strlen(buf));
		}
		return;
	}

	if (input->reply) {
		char buf[4096] = {0};
		for (size_t i = 0; i < LAT_BUCKET_COUNT; 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 < LAT_BUCKET_COUNT; i++)
			if (buckets[i])
				plog_info("Bucket [%zu]: %"PRIu64"\n", i, buckets[i]);
	}
}

static void handle_stats_and_packets(unsigned lcore_id, unsigned task_id, struct input *input)
{
	handle_lat_stats(lcore_id, task_id, input);
	handle_latency_histogram(lcore_id, task_id, input);
}
#endif

static int parse_cmd_dp_core_stats(const char *str, struct input *input)
{
	handle_cores_tasks(str, input, NULL, NULL, handle_dp_core_stats);
	return 0;
}

static int parse_cmd_lat_stats(const char *str, struct input *input)
{
	handle_cores_tasks(str, input, "lat", "latency", handle_lat_stats);
	return 0;
}

static int parse_cmd_lat_packets(const char *str, struct input *input)
{
#ifdef LATENCY_HISTOGRAM
	handle_cores_tasks(str, input, "lat", "latency", handle_latency_histogram);
#else
	if (input->reply) {
		char buf[128];
		snprintf(buf, sizeof(buf), "error: invalid syntax (LATENCY_HISTOGRAM disabled)\n");
		input->reply(input, buf, strlen(buf));
	} else {
		plog_info("LATENCY_HISTOGRAMS disabled\n");
	}
#endif
	return 0;
}

static int parse_cmd_lat_stats_and_packets(const char *str, struct input *input)
{
#ifdef LATENCY_HISTOGRAM
	handle_cores_tasks(str, input, "lat", "latency", handle_stats_and_packets);
#else
	if (input->reply) {
		char buf[128];
		snprintf(buf, sizeof(buf), "error: invalid syntax (LATENCY_HISTOGRAMS disabled)\n");
		input->reply(input, buf, strlen(buf));
	} else {
		plog_info("LATENCY_HISTOGRAMS disabled\n");
	}
#endif
	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 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},
	{"imix", "<core_id> <task_id> <pkt_size,pkt_size ... >", "Set the packet sizes to <pkt_size>", parse_cmd_imix},
	{"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},
	{"lat all stats", "<core id> <task id>", "Print the latency for each of the last set of packets as well as latency distribution", parse_cmd_lat_stats_and_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);
}