summaryrefslogtreecommitdiff
path: root/src/vf/spp_vf.c
blob: 6d69e3de6c8dbc94b2f08509e8f07cbc71bd8c15 (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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
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
/*
 *   BSD LICENSE
 *
 *   Copyright(c) 2017 Nippon Telegraph and Telephone Corporation
 *   All rights reserved.
 *
 *   Redistribution and use in source and binary forms, with or without
 *   modification, are permitted provided that the following conditions
 *   are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in
 *       the documentation and/or other materials provided with the
 *       distribution.
 *     * Neither the name of Nippon Telegraph and Telephone Corporation
 *       nor the names of its contributors may be used to endorse or
 *       promote products derived from this software without specific
 *       prior written permission.
 *
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <netinet/in.h>
#include <arpa/inet.h>
#include <getopt.h>

#include <rte_eth_ring.h>
#include <rte_eth_vhost.h>
#include <rte_memzone.h>
#include <rte_cycles.h>

#include "spp_vf.h"
#include "ringlatencystats.h"
#include "classifier_mac.h"
#include "spp_forward.h"
#include "command_proc.h"

/* Max number of core status check */
#define SPP_CORE_STATUS_CHECK_MAX 5

/* Sampling interval timer for latency evaluation */
#define SPP_RING_LATENCY_STATS_SAMPLING_INTERVAL 1000000

/* Name string for each component */
#define CORE_TYPE_CLASSIFIER_MAC_STR "classifier_mac"
#define CORE_TYPE_MERGE_STR          "merge"
#define CORE_TYPE_FORWARD_STR        "forward"

/* getopt_long return value for long option */
enum SPP_LONGOPT_RETVAL {
	SPP_LONGOPT_RETVAL__ = 127,

	/*
	 * Return value definition for getopt_long()
	 * Only for long option
	 */
	SPP_LONGOPT_RETVAL_CLIENT_ID,   /* --client-id    */
	SPP_LONGOPT_RETVAL_VHOST_CLIENT /* --vhost-client */
};

/* Flag of processing type to copy management information */
enum copy_mng_flg {
	COPY_MNG_FLG_NONE,
	COPY_MNG_FLG_UPDCOPY,
	COPY_MNG_FLG_ALLCOPY,
};

/* Manage given options as global variable */
struct startup_param {
	int client_id;          /* Client ID of spp_vf */
	char server_ip[INET_ADDRSTRLEN];
				/* IP address sting of spp controller */
	int server_port;        /* Port Number of spp controller */
	int vhost_client;       /* Flag for --vhost-client option */
};

/* Manage number of interfaces  and port information as global variable */
struct iface_info {
	int num_nic;            /* The number of phy */
	int num_vhost;          /* The number of vhost */
	int num_ring;           /* The number of ring */
	struct spp_port_info nic[RTE_MAX_ETHPORTS];
				/* Port information of phy */
	struct spp_port_info vhost[RTE_MAX_ETHPORTS];
				/* Port information of vhost */
	struct spp_port_info ring[RTE_MAX_ETHPORTS];
				/* Port information of ring */
};

/* Manage component running in core as global variable */
struct core_info {
	volatile enum spp_component_type type;
				/* Component type */
	int num;                /* The number of IDs below */
	int id[RTE_MAX_LCORE];  /* ID list of components executed on cpu core */
};

/* Manage core status and component information as global variable */
struct core_mng_info {
	/* Status of cpu core */
	volatile enum spp_core_status status;

	/* Index number of core information for reference */
	volatile int ref_index;

	/* Index number of core information for updating */
	volatile int upd_index;

	/* Core information of each cpu core */
	struct core_info core[SPP_INFO_AREA_MAX];
};

/* Manage data to be backup */
struct cancel_backup_info {
	/* Backup data of core information */
	struct core_mng_info core[RTE_MAX_LCORE];

	/* Backup data of component information */
	struct spp_component_info component[RTE_MAX_LCORE];

	/* Backup data of interface information */
	struct iface_info interface;
};

/* Declare global variables */
/* Logical core ID for main process */
static unsigned int g_main_lcore_id = 0xffffffff;

/* Execution parameter of spp_vf */
static struct startup_param g_startup_param;

/* Interface management information */
static struct iface_info g_iface_info;

/* Component management information */
static struct spp_component_info g_component_info[RTE_MAX_LCORE];

/* Core management information */
static struct core_mng_info g_core_info[RTE_MAX_LCORE];

/* Array of update indicator for core management information */
static int g_change_core[RTE_MAX_LCORE];

/* Array of update indicator for component management information */
static int g_change_component[RTE_MAX_LCORE];

/* Backup information for cancel command */
static struct cancel_backup_info g_backup_info;

/* Print help message */
static void
usage(const char *progname)
{
	RTE_LOG(INFO, APP, "Usage: %s [EAL args] --"
			" --client-id CLIENT_ID"
			" -s SERVER_IP:SERVER_PORT"
			" [--vhost-client]\n"
			" --client-id CLIENT_ID   : My client ID\n"
			" -s SERVER_IP:SERVER_PORT  : Access information to the server\n"
			" --vhost-client            : Run vhost on client\n"
			, progname);
}

/* Make a hexdump of an array data in every 4 byte */
static void
dump_buff(const char *name, const void *addr, const size_t size)
{
	size_t cnt = 0;
	size_t max = (size / sizeof(unsigned int)) +
			((size % sizeof(unsigned int)) != 0);
	const uint32_t *buff = addr;

	if ((name != NULL) && (name[0] != '\0'))
		RTE_LOG(DEBUG, APP, "dump buff. (%s)\n", name);

	for (cnt = 0; cnt < max; cnt += 16) {
		RTE_LOG(DEBUG, APP, "[%p]"
				" %08x %08x %08x %08x %08x %08x %08x %08x"
				" %08x %08x %08x %08x %08x %08x %08x %08x",
				&buff[cnt],
				buff[cnt+0], buff[cnt+1],
				buff[cnt+2], buff[cnt+3],
				buff[cnt+4], buff[cnt+5],
				buff[cnt+6], buff[cnt+7],
				buff[cnt+8], buff[cnt+9],
				buff[cnt+10], buff[cnt+11],
				buff[cnt+12], buff[cnt+13],
				buff[cnt+14], buff[cnt+15]);
	}
}

static int
add_ring_pmd(int ring_id)
{
	struct rte_ring *ring;
	int ring_port_id;

	/* Lookup ring of given id */
	ring = rte_ring_lookup(get_rx_queue_name(ring_id));
	if (unlikely(ring == NULL)) {
		RTE_LOG(ERR, APP,
			"Cannot get RX ring - is server process running?\n");
		return -1;
	}

	/* Create ring pmd */
	ring_port_id = rte_eth_from_ring(ring);
	RTE_LOG(INFO, APP, "ring port add. (no = %d / port = %d)\n",
			ring_id, ring_port_id);
	return ring_port_id;
}

static int
add_vhost_pmd(int index, int client)
{
	struct rte_eth_conf port_conf = {
		.rxmode = { .max_rx_pkt_len = ETHER_MAX_LEN }
	};
	struct rte_mempool *mp;
	uint16_t vhost_port_id;
	int nr_queues = 1;
	const char *name;
	char devargs[64];
	char *iface;
	uint16_t q;
	int ret;
#define NR_DESCS 128

	mp = rte_mempool_lookup(PKTMBUF_POOL_NAME);
	if (unlikely(mp == NULL)) {
		RTE_LOG(ERR, APP, "Cannot get mempool for mbufs. (name = %s)\n",
				PKTMBUF_POOL_NAME);
		return -1;
	}

	/* eth_vhost0 index 0 iface /tmp/sock0 on numa 0 */
	name = get_vhost_backend_name(index);
	iface = get_vhost_iface_name(index);

	sprintf(devargs, "%s,iface=%s,queues=%d,client=%d",
			name, iface, nr_queues, client);
	ret = rte_eth_dev_attach(devargs, &vhost_port_id);
	if (unlikely(ret < 0)) {
		RTE_LOG(ERR, APP, "rte_eth_dev_attach error. (ret = %d)\n",
				ret);
		return ret;
	}

	ret = rte_eth_dev_configure(vhost_port_id, nr_queues, nr_queues,
		&port_conf);
	if (unlikely(ret < 0)) {
		RTE_LOG(ERR, APP, "rte_eth_dev_configure error. (ret = %d)\n",
				ret);
		return ret;
	}

	/* Allocate and set up 1 RX queue per Ethernet port. */
	for (q = 0; q < nr_queues; q++) {
		ret = rte_eth_rx_queue_setup(vhost_port_id, q, NR_DESCS,
			rte_eth_dev_socket_id(vhost_port_id), NULL, mp);
		if (unlikely(ret < 0)) {
			RTE_LOG(ERR, APP,
				"rte_eth_rx_queue_setup error. (ret = %d)\n",
				ret);
			return ret;
		}
	}

	/* Allocate and set up 1 TX queue per Ethernet port. */
	for (q = 0; q < nr_queues; q++) {
		ret = rte_eth_tx_queue_setup(vhost_port_id, q, NR_DESCS,
			rte_eth_dev_socket_id(vhost_port_id), NULL);
		if (unlikely(ret < 0)) {
			RTE_LOG(ERR, APP,
				"rte_eth_tx_queue_setup error. (ret = %d)\n",
				ret);
			return ret;
		}
	}

	/* Start the Ethernet port. */
	ret = rte_eth_dev_start(vhost_port_id);
	if (unlikely(ret < 0)) {
		RTE_LOG(ERR, APP, "rte_eth_dev_start error. (ret = %d)\n", ret);
		return ret;
	}

	RTE_LOG(INFO, APP, "vhost port add. (no = %d / port = %d)\n",
			index, vhost_port_id);
	return vhost_port_id;
}

/* Get core status */
enum spp_core_status
spp_get_core_status(unsigned int lcore_id)
{
	return g_core_info[lcore_id].status;
}

/**
 * Check status of all of cores is same as given
 *
 * It returns -1 as status mismatch if status is not same.
 * If core is in use, status will be checked.
 */
static int
check_core_status(enum spp_core_status status)
{
	unsigned int lcore_id = 0;
	RTE_LCORE_FOREACH_SLAVE(lcore_id) {
		if (g_core_info[lcore_id].status != status) {
			/* Status is mismatched */
			return -1;
		}
	}
	return 0;
}

/**
 * Run check_core_status() for SPP_CORE_STATUS_CHECK_MAX times with
 * interval time (1sec)
 */
static int
check_core_status_wait(enum spp_core_status status)
{
	int cnt = 0;
	for (cnt = 0; cnt < SPP_CORE_STATUS_CHECK_MAX; cnt++) {
		sleep(1);
		int ret = check_core_status(status);
		if (ret == 0)
			return 0;
	}

	RTE_LOG(ERR, APP, "Status check time out. (status = %d)\n", status);
	return -1;
}

/* Set core status */
static void
set_core_status(unsigned int lcore_id,
		enum spp_core_status status)
{
	g_core_info[lcore_id].status = status;
}

/* Set all core to given status */
static void
set_all_core_status(enum spp_core_status status)
{
	unsigned int lcore_id = 0;
	RTE_LCORE_FOREACH_SLAVE(lcore_id) {
		g_core_info[lcore_id].status = status;
	}
}

/**
 * Set all of component status to SPP_CORE_STOP_REQUEST if received signal
 * is SIGTERM or SIGINT
 */
static void
stop_process(int signal) {
	if (unlikely(signal != SIGTERM) &&
			unlikely(signal != SIGINT)) {
		return;
	}

	g_core_info[g_main_lcore_id].status = SPP_CORE_STOP_REQUEST;
	set_all_core_status(SPP_CORE_STOP_REQUEST);
}

/**
 * Convert string of given client id to integer
 *
 * If succeeded, client id of integer is assigned to client_id and
 * return 0. Or return -1 if failed.
 */
static int
parse_app_client_id(const char *client_id_str, int *client_id)
{
	int id = 0;
	char *endptr = NULL;

	id = strtol(client_id_str, &endptr, 0);
	if (unlikely(client_id_str == endptr) || unlikely(*endptr != '\0'))
		return -1;

	if (id >= RTE_MAX_LCORE)
		return -1;

	*client_id = id;
	RTE_LOG(DEBUG, APP, "Set client id = %d\n", *client_id);
	return 0;
}

/* Parse options for server IP and port */
static int
parse_app_server(const char *server_str, char *server_ip, int *server_port)
{
	const char delim[2] = ":";
	unsigned int pos = 0;
	int port = 0;
	char *endptr = NULL;

	pos = strcspn(server_str, delim);
	if (pos >= strlen(server_str))
		return -1;

	port = strtol(&server_str[pos+1], &endptr, 0);
	if (unlikely(&server_str[pos+1] == endptr) || unlikely(*endptr != '\0'))
		return -1;

	memcpy(server_ip, server_str, pos);
	server_ip[pos] = '\0';
	*server_port = port;
	RTE_LOG(DEBUG, APP, "Set server IP   = %s\n", server_ip);
	RTE_LOG(DEBUG, APP, "Set server port = %d\n", *server_port);
	return 0;
}

/* Parse options for client app */
static int
parse_app_args(int argc, char *argv[])
{
	int cnt;
	int proc_flg = 0;
	int server_flg = 0;
	int option_index, opt;
	const int argcopt = argc;
	char *argvopt[argcopt];
	const char *progname = argv[0];
	static struct option lgopts[] = {
			{ "client-id", required_argument, NULL,
					SPP_LONGOPT_RETVAL_CLIENT_ID },
			{ "vhost-client", no_argument, NULL,
					SPP_LONGOPT_RETVAL_VHOST_CLIENT },
			{ 0 },
	};

	/**
	 * Save argv to argvopt to avoid losing the order of options
	 * by getopt_long()
	 */
	for (cnt = 0; cnt < argcopt; cnt++)
		argvopt[cnt] = argv[cnt];

	/* Clear startup parameters */
	memset(&g_startup_param, 0x00, sizeof(g_startup_param));

	/* Check options of application */
	optind = 0;
	opterr = 0;
	while ((opt = getopt_long(argc, argvopt, "s:", lgopts,
			&option_index)) != EOF) {
		switch (opt) {
		case SPP_LONGOPT_RETVAL_CLIENT_ID:
			if (parse_app_client_id(optarg,
					&g_startup_param.client_id) != 0) {
				usage(progname);
				return -1;
			}
			proc_flg = 1;
			break;
		case SPP_LONGOPT_RETVAL_VHOST_CLIENT:
			g_startup_param.vhost_client = 1;
			break;
		case 's':
			if (parse_app_server(optarg, g_startup_param.server_ip,
					&g_startup_param.server_port) != 0) {
				usage(progname);
				return -1;
			}
			server_flg = 1;
			break;
		default:
			usage(progname);
			return -1;
		}
	}

	/* Check mandatory parameters */
	if ((proc_flg == 0) || (server_flg == 0)) {
		usage(progname);
		return -1;
	}
	RTE_LOG(INFO, APP,
			"app opts (client_id=%d,server=%s:%d,vhost_client=%d)\n",
			g_startup_param.client_id,
			g_startup_param.server_ip,
			g_startup_param.server_port,
			g_startup_param.vhost_client);
	return 0;
}

/**
 * Return port info of given type and num of interface
 *
 * It returns NULL value if given type is invalid.
 */
static struct spp_port_info *
get_iface_info(enum port_type iface_type, int iface_no)
{
	switch (iface_type) {
	case PHY:
		return &g_iface_info.nic[iface_no];
	case VHOST:
		return &g_iface_info.vhost[iface_no];
	case RING:
		return &g_iface_info.ring[iface_no];
	default:
		return NULL;
	}
}

/* Dump of core information */
static void
dump_core_info(const struct core_mng_info *core_info)
{
	char str[SPP_NAME_STR_LEN];
	const struct core_mng_info *info = NULL;
	unsigned int lcore_id = 0;
	RTE_LCORE_FOREACH_SLAVE(lcore_id) {
		info = &core_info[lcore_id];
		RTE_LOG(DEBUG, APP, "core[%d] status=%d, ref=%d, upd=%d\n",
				lcore_id, info->status,
				info->ref_index, info->upd_index);

		sprintf(str, "core[%d]-0 type=%d, num=%d", lcore_id,
				info->core[0].type, info->core[0].num);
		dump_buff(str, info->core[0].id, sizeof(int)*info->core[0].num);

		sprintf(str, "core[%d]-1 type=%d, num=%d", lcore_id,
				info->core[1].type, info->core[1].num);
		dump_buff(str, info->core[1].id, sizeof(int)*info->core[1].num);
	}
}

/* Dump of component information */
static void
dump_component_info(const struct spp_component_info *component_info)
{
	char str[SPP_NAME_STR_LEN];
	const struct spp_component_info *component = NULL;
	int cnt = 0;
	for (cnt = 0; cnt < RTE_MAX_LCORE; cnt++) {
		component = &component_info[cnt];
		if (component->type == SPP_COMPONENT_UNUSE)
			continue;

		RTE_LOG(DEBUG, APP, "component[%d] name=%s, type=%d, core=%u, index=%d\n",
				cnt, component->name, component->type,
				component->lcore_id, component->component_id);

		sprintf(str, "component[%d] rx=%d", cnt,
				component->num_rx_port);
		dump_buff(str, component->rx_ports,
			sizeof(struct spp_port_info *)*component->num_rx_port);

		sprintf(str, "component[%d] tx=%d", cnt,
				component->num_tx_port);
		dump_buff(str, component->tx_ports,
			sizeof(struct spp_port_info *)*component->num_tx_port);
	}
}

/* Dump of interface information */
static void
dump_interface_info(const struct iface_info *iface_info)
{
	const struct spp_port_info *port = NULL;
	int cnt = 0;
	RTE_LOG(DEBUG, APP, "interface phy=%d, vhost=%d, ring=%d\n",
			iface_info->num_nic,
			iface_info->num_vhost,
			iface_info->num_ring);
	for (cnt = 0; cnt < RTE_MAX_ETHPORTS; cnt++) {
		port = &iface_info->nic[cnt];
		if (port->iface_type == UNDEF)
			continue;

		RTE_LOG(DEBUG, APP, "phy  [%d] type=%d, no=%d, port=%d, "
				"mac=%08lx(%s)\n",
				cnt, port->iface_type, port->iface_no,
				port->dpdk_port,
				port->mac_addr, port->mac_addr_str);
	}
	for (cnt = 0; cnt < RTE_MAX_ETHPORTS; cnt++) {
		port = &iface_info->vhost[cnt];
		if (port->iface_type == UNDEF)
			continue;

		RTE_LOG(DEBUG, APP, "vhost[%d] type=%d, no=%d, port=%d, "
				"mac=%08lx(%s)\n",
				cnt, port->iface_type, port->iface_no,
				port->dpdk_port,
				port->mac_addr, port->mac_addr_str);
	}
	for (cnt = 0; cnt < RTE_MAX_ETHPORTS; cnt++) {
		port = &iface_info->ring[cnt];
		if (port->iface_type == UNDEF)
			continue;

		RTE_LOG(DEBUG, APP, "ring [%d] type=%d, no=%d, port=%d, "
				"mac=%08lx(%s)\n",
				cnt, port->iface_type, port->iface_no,
				port->dpdk_port,
				port->mac_addr, port->mac_addr_str);
	}
}

/* Dump of all management information */
static void
dump_all_mng_info(
		const struct core_mng_info *core,
		const struct spp_component_info *component,
		const struct iface_info *interface)
{
	if (rte_log_get_global_level() < RTE_LOG_DEBUG)
		return;

	dump_core_info(core);
	dump_component_info(component);
	dump_interface_info(interface);
}

/* Copy management information */
static void
copy_mng_info(
		struct core_mng_info *dst_core,
		struct spp_component_info *dst_component,
		struct iface_info *dst_interface,
		const struct core_mng_info *src_core,
		const struct spp_component_info *src_component,
		const struct iface_info *src_interface,
		enum copy_mng_flg flg)
{
	int upd_index = 0;
	unsigned int lcore_id = 0;

	switch (flg) {
	case COPY_MNG_FLG_UPDCOPY:
		RTE_LCORE_FOREACH_SLAVE(lcore_id) {
			upd_index = src_core[lcore_id].upd_index;
			memcpy(&dst_core[lcore_id].core[upd_index],
					&src_core[lcore_id].core[upd_index],
					sizeof(struct core_info));
		}
		break;
	default:
		/*
		 * Even if the flag is set to None,
		 * copying of core is necessary,
		 * so we will treat all copies as default.
		 */
		memcpy(dst_core, src_core,
				sizeof(struct core_mng_info)*RTE_MAX_LCORE);
		break;
	}

	memcpy(dst_component, src_component,
			sizeof(struct spp_component_info)*RTE_MAX_LCORE);
	memcpy(dst_interface, src_interface,
			sizeof(struct iface_info));
}

/* Backup the management information */
static void
backup_mng_info(struct cancel_backup_info *backup)
{
	dump_all_mng_info(g_core_info, g_component_info, &g_iface_info);
	copy_mng_info(backup->core, backup->component, &backup->interface,
			g_core_info, g_component_info, &g_iface_info,
			COPY_MNG_FLG_ALLCOPY);
	dump_all_mng_info(backup->core, backup->component, &backup->interface);
	memset(g_change_core, 0x00, sizeof(g_change_core));
	memset(g_change_component, 0x00, sizeof(g_change_component));
}

/* Cancel update of management information */
static void
cancel_mng_info(struct cancel_backup_info *backup)
{
	dump_all_mng_info(backup->core, backup->component, &backup->interface);
	copy_mng_info(g_core_info, g_component_info, &g_iface_info,
			backup->core, backup->component, &backup->interface,
			COPY_MNG_FLG_ALLCOPY);
	dump_all_mng_info(g_core_info, g_component_info, &g_iface_info);
	memset(g_change_core, 0x00, sizeof(g_change_core));
	memset(g_change_component, 0x00, sizeof(g_change_component));
}

/**
 * Initialize g_iface_info
 *
 * Clear g_iface_info and set initial value.
 */
static void
init_iface_info(void)
{
	int port_cnt;  /* increment ether ports */
	memset(&g_iface_info, 0x00, sizeof(g_iface_info));
	for (port_cnt = 0; port_cnt < RTE_MAX_ETHPORTS; port_cnt++) {
		g_iface_info.nic[port_cnt].iface_type   = UNDEF;
		g_iface_info.nic[port_cnt].iface_no     = port_cnt;
		g_iface_info.nic[port_cnt].dpdk_port = -1;
		g_iface_info.vhost[port_cnt].iface_type   = UNDEF;
		g_iface_info.vhost[port_cnt].iface_no     = port_cnt;
		g_iface_info.vhost[port_cnt].dpdk_port = -1;
		g_iface_info.ring[port_cnt].iface_type   = UNDEF;
		g_iface_info.ring[port_cnt].iface_no     = port_cnt;
		g_iface_info.ring[port_cnt].dpdk_port = -1;
	}
}

/**
 * Initialize g_component_info
 */
static void
init_component_info(void)
{
	int cnt;
	memset(&g_component_info, 0x00, sizeof(g_component_info));
	for (cnt = 0; cnt < RTE_MAX_LCORE; cnt++)
		g_component_info[cnt].component_id = cnt;
	memset(g_change_component, 0x00, sizeof(g_change_component));
}

/**
 * Initialize g_core_info
 */
static void
init_core_info(void)
{
	int cnt = 0;
	memset(&g_core_info, 0x00, sizeof(g_core_info));
	set_all_core_status(SPP_CORE_STOP);
	for (cnt = 0; cnt < RTE_MAX_LCORE; cnt++) {
		g_core_info[cnt].ref_index = 0;
		g_core_info[cnt].upd_index = 1;
	}
	memset(g_change_core, 0x00, sizeof(g_change_core));
}

/**
 * Setup port info of port on host
 */
static int
set_nic_interface(void)
{
	int nic_cnt = 0;

	/* NIC Setting */
	g_iface_info.num_nic = rte_eth_dev_count();
	if (g_iface_info.num_nic > RTE_MAX_ETHPORTS)
		g_iface_info.num_nic = RTE_MAX_ETHPORTS;

	for (nic_cnt = 0; nic_cnt < g_iface_info.num_nic; nic_cnt++) {
		g_iface_info.nic[nic_cnt].iface_type   = PHY;
		g_iface_info.nic[nic_cnt].dpdk_port = nic_cnt;
	}

	return 0;
}

/**
 * Setup management info for spp_vf
 */
static int
init_mng_data(void)
{
	/* Initialize interface and core information */
	init_iface_info();
	init_core_info();
	init_component_info();

	int ret_nic = set_nic_interface();
	if (unlikely(ret_nic != 0))
		return -1;

	return 0;
}

#ifdef SPP_RINGLATENCYSTATS_ENABLE
/**
 * Print statistics of time for packet processing in ring interface
 */
static void
print_ring_latency_stats(void)
{
	/* Clear screen and move cursor to top left */
	const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' };
	const char clr[] = { 27, '[', '2', 'J', '\0' };
	printf("%s%s", clr, topLeft);

	int ring_cnt, stats_cnt;
	struct spp_ringlatencystats_ring_latency_stats stats[RTE_MAX_ETHPORTS];
	memset(&stats, 0x00, sizeof(stats));

	printf("RING Latency\n");
	printf(" RING");
	for (ring_cnt = 0; ring_cnt < RTE_MAX_ETHPORTS; ring_cnt++) {
		if (g_iface_info.ring[ring_cnt].iface_type == UNDEF)
			continue;

		spp_ringlatencystats_get_stats(ring_cnt, &stats[ring_cnt]);
		printf(", %-18d", ring_cnt);
	}
	printf("\n");

	for (stats_cnt = 0; stats_cnt < SPP_RINGLATENCYSTATS_STATS_SLOT_COUNT;
			stats_cnt++) {
		printf("%3dns", stats_cnt);
		for (ring_cnt = 0; ring_cnt < RTE_MAX_ETHPORTS; ring_cnt++) {
			if (g_iface_info.ring[ring_cnt].iface_type == UNDEF)
				continue;

			printf(", 0x%-16lx", stats[ring_cnt].slot[stats_cnt]);
		}
		printf("\n");
	}
}
#endif /* SPP_RINGLATENCYSTATS_ENABLE */

/**
 * Remove sock file
 */
static void
del_vhost_sockfile(struct spp_port_info *vhost)
{
	int cnt;

	/* Do not remove for if it is running in vhost-client mode. */
	if (g_startup_param.vhost_client != 0)
		return;

	for (cnt = 0; cnt < RTE_MAX_ETHPORTS; cnt++) {
		if (likely(vhost[cnt].iface_type == UNDEF)) {
			/* Skip removing if it is not using vhost */
			continue;
		}

		remove(get_vhost_iface_name(cnt));
	}
}

/* Get component type of target core */
enum spp_component_type
spp_get_component_type(unsigned int lcore_id)
{
	struct core_mng_info *info = &g_core_info[lcore_id];
	return info->core[info->ref_index].type;
}

/* Get component type being updated on target core */
enum spp_component_type
spp_get_component_type_update(unsigned int lcore_id)
{
	struct core_mng_info *info = &g_core_info[lcore_id];
	return info->core[info->upd_index].type;
}

/* Get core ID of target component */
unsigned int
spp_get_component_core(int component_id)
{
	struct spp_component_info *info = &g_component_info[component_id];
	return info->lcore_id;
}

/* Get usage area of target core */
static struct core_info *
get_core_info(unsigned int lcore_id)
{
	struct core_mng_info *info = &g_core_info[lcore_id];
	return &(info->core[info->ref_index]);
}

/* Check core index change */
int
spp_check_core_index(unsigned int lcore_id)
{
	struct core_mng_info *info = &g_core_info[lcore_id];
	return info->ref_index == info->upd_index;
}

/* Main process of slave core */
static int
slave_main(void *arg __attribute__ ((unused)))
{
	int ret = 0;
	int cnt = 0;
	unsigned int lcore_id = rte_lcore_id();
	enum spp_core_status status = SPP_CORE_STOP;
	struct core_mng_info *info = &g_core_info[lcore_id];
	struct core_info *core = get_core_info(lcore_id);

	RTE_LOG(INFO, APP, "Core[%d] Start.\n", lcore_id);
	set_core_status(lcore_id, SPP_CORE_IDLE);

	while ((status = spp_get_core_status(lcore_id)) !=
			SPP_CORE_STOP_REQUEST) {
		if (status != SPP_CORE_FORWARD)
			continue;

		if (spp_check_core_index(lcore_id)) {
			/* Setting with the flush command trigger. */
			info->ref_index = (info->upd_index+1) %
					SPP_INFO_AREA_MAX;
			core = get_core_info(lcore_id);
		}

		for (cnt = 0; cnt < core->num; cnt++) {
			if (spp_get_component_type(lcore_id) ==
					SPP_COMPONENT_CLASSIFIER_MAC) {
				/* Classifier loops inside the function. */
				ret = spp_classifier_mac_do(core->id[cnt]);
				break;
			}

			/*
			 * Forward / Merge returns at once.
			 * It is for processing multiple components.
			 */
			ret = spp_forward(core->id[cnt]);
			if (unlikely(ret != 0))
				break;
		}
		if (unlikely(ret != 0)) {
			RTE_LOG(ERR, APP, "Core[%d] Component Error. (id = %d)\n",
					lcore_id, core->id[cnt]);
			break;
		}
	}

	set_core_status(lcore_id, SPP_CORE_STOP);
	RTE_LOG(INFO, APP, "Core[%d] End.\n", lcore_id);
	return ret;
}

/**
 * Main function
 *
 * Return -1 explicitly if error is occurred.
 */
int
main(int argc, char *argv[])
{
	int ret = -1;
#ifdef SPP_DEMONIZE
	/* Daemonize process */
	int ret_daemon = daemon(0, 0);
	if (unlikely(ret_daemon != 0)) {
		RTE_LOG(ERR, APP, "daemonize is failed. (ret = %d)\n",
				ret_daemon);
		return ret_daemon;
	}
#endif

	/* Signal handler registration (SIGTERM/SIGINT) */
	signal(SIGTERM, stop_process);
	signal(SIGINT,  stop_process);

	while (1) {
		int ret_dpdk = rte_eal_init(argc, argv);
		if (unlikely(ret_dpdk < 0))
			break;

		argc -= ret_dpdk;
		argv += ret_dpdk;

		/* Set log level  */
		rte_log_set_global_level(RTE_LOG_LEVEL);

		/* Parse spp_vf specific parameters */
		int ret_parse = parse_app_args(argc, argv);
		if (unlikely(ret_parse != 0))
			break;

		/* Get lcore id of main thread to set its status after */
		g_main_lcore_id = rte_lcore_id();

		int ret_mng = init_mng_data();
		if (unlikely(ret_mng != 0))
			break;

		int ret_classifier_mac_init = spp_classifier_mac_init();
		if (unlikely(ret_classifier_mac_init != 0))
			break;

		spp_forward_init();

		/* Setup connection for accepting commands from controller */
		int ret_command_init = spp_command_proc_init(
				g_startup_param.server_ip,
				g_startup_param.server_port);
		if (unlikely(ret_command_init != 0))
			break;

#ifdef SPP_RINGLATENCYSTATS_ENABLE
		int ret_ringlatency = spp_ringlatencystats_init(
				SPP_RING_LATENCY_STATS_SAMPLING_INTERVAL,
				g_iface_info.num_ring);
		if (unlikely(ret_ringlatency != 0))
			break;
#endif /* SPP_RINGLATENCYSTATS_ENABLE */

		/* Start worker threads of classifier and forwarder */
		unsigned int lcore_id = 0;
		RTE_LCORE_FOREACH_SLAVE(lcore_id) {
			rte_eal_remote_launch(slave_main, NULL, lcore_id);
		}

		/* Set the status of main thread to idle */
		g_core_info[g_main_lcore_id].status = SPP_CORE_IDLE;
		int ret_wait = check_core_status_wait(SPP_CORE_IDLE);
		if (unlikely(ret_wait != 0))
			break;

		/* Start forwarding */
		set_all_core_status(SPP_CORE_FORWARD);
		RTE_LOG(INFO, APP, "My ID %d start handling message\n", 0);
		RTE_LOG(INFO, APP, "[Press Ctrl-C to quit ...]\n");

		/* Backup the management information after initialization */
		backup_mng_info(&g_backup_info);

		/* Enter loop for accepting commands */
		int ret_do = 0;
#ifndef USE_UT_SPP_VF
		while (likely(g_core_info[g_main_lcore_id].status !=
				SPP_CORE_STOP_REQUEST)) {
#else
		{
#endif
			/* Receive command */
			ret_do = spp_command_proc_do();
			if (unlikely(ret_do != 0))
				break;

			sleep(1);

#ifdef SPP_RINGLATENCYSTATS_ENABLE
			print_ring_latency_stats();
#endif /* SPP_RINGLATENCYSTATS_ENABLE */
		}

		if (unlikely(ret_do != 0)) {
			set_all_core_status(SPP_CORE_STOP_REQUEST);
			break;
		}

		ret = 0;
		break;
	}

	/* Finalize to exit */
	if (g_main_lcore_id == rte_lcore_id()) {
		g_core_info[g_main_lcore_id].status = SPP_CORE_STOP;
		int ret_core_end = check_core_status_wait(SPP_CORE_STOP);
		if (unlikely(ret_core_end != 0))
			RTE_LOG(ERR, APP, "Core did not stop.\n");

		/*
		 * Remove vhost sock file if it is not running
		 *  in vhost-client mode
		 */
		del_vhost_sockfile(g_iface_info.vhost);
	}

#ifdef SPP_RINGLATENCYSTATS_ENABLE
	spp_ringlatencystats_uninit();
#endif /* SPP_RINGLATENCYSTATS_ENABLE */

	RTE_LOG(INFO, APP, "spp_vf exit.\n");
	return ret;
}

int
spp_get_client_id(void)
{
	return g_startup_param.client_id;
}

/**
 * Check mac address used on the port for registering or removing
 */
int
spp_check_mac_used_port(
		uint64_t mac_addr,
		enum port_type iface_type,
		int iface_no)
{
	struct spp_port_info *port_info = get_iface_info(iface_type, iface_no);
	return (mac_addr == port_info->mac_addr);
}

/*
 * Check if port has been added.
 */
int
spp_check_added_port(enum port_type iface_type, int iface_no)
{
	struct spp_port_info *port = get_iface_info(iface_type, iface_no);
	return port->iface_type != UNDEF;
}

/*
 * Check if port has been flushed.
 */
int
spp_check_flush_port(enum port_type iface_type, int iface_no)
{
	struct spp_port_info *port = get_iface_info(iface_type, iface_no);
	return port->dpdk_port >= 0;
}

/*
 * Check if component is using port.
 */
int
spp_check_used_port(
		enum port_type iface_type,
		int iface_no,
		enum spp_port_rxtx rxtx)
{
	int cnt, port_cnt, max = 0;
	struct spp_component_info *component = NULL;
	struct spp_port_info **port_array = NULL;
	struct spp_port_info *port = get_iface_info(iface_type, iface_no);

	if (port == NULL)
		return SPP_RET_NG;

	for (cnt = 0; cnt < RTE_MAX_LCORE; cnt++) {
		component = &g_component_info[cnt];
		if (component->type == SPP_COMPONENT_UNUSE)
			continue;

		if (rxtx == SPP_PORT_RXTX_RX) {
			max = component->num_rx_port;
			port_array = component->rx_ports;
		} else if (rxtx == SPP_PORT_RXTX_TX) {
			max = component->num_tx_port;
			port_array = component->tx_ports;
		}
		for (port_cnt = 0; port_cnt < max; port_cnt++) {
			if (unlikely(port_array[port_cnt] == port))
				return cnt;
		}
	}

	return SPP_RET_NG;
}

/*
 * Set port change to component.
 */
static void
set_component_change_port(struct spp_port_info *port, enum spp_port_rxtx rxtx)
{
	int ret = 0;
	if ((rxtx == SPP_PORT_RXTX_RX) || (rxtx == SPP_PORT_RXTX_ALL)) {
		ret = spp_check_used_port(port->iface_type, port->iface_no,
				SPP_PORT_RXTX_RX);
		if (ret >= 0)
			g_change_component[ret] = 1;
	}

	if ((rxtx == SPP_PORT_RXTX_TX) || (rxtx == SPP_PORT_RXTX_ALL)) {
		ret = spp_check_used_port(port->iface_type, port->iface_no,
				SPP_PORT_RXTX_TX);
		if (ret >= 0)
			g_change_component[ret] = 1;
	}
}

int
spp_update_classifier_table(
		enum spp_command_action action,
		enum spp_classifier_type type,
		const char *data,
		const struct spp_port_index *port)
{
	struct spp_port_info *port_info = NULL;
	int64_t ret_mac = 0;
	uint64_t mac_addr = 0;

	if (type == SPP_CLASSIFIER_TYPE_MAC) {
		RTE_LOG(DEBUG, APP, "update_classifier_table ( type = mac, data = %s, port = %d:%d )\n",
				data, port->iface_type, port->iface_no);

		ret_mac = spp_change_mac_str_to_int64(data);
		if (unlikely(ret_mac == -1)) {
			RTE_LOG(ERR, APP, "MAC address format error. ( mac = %s )\n",
					data);
			return SPP_RET_NG;
		}
		mac_addr = (uint64_t)ret_mac;

		port_info = get_iface_info(port->iface_type, port->iface_no);
		if (unlikely(port_info == NULL)) {
			RTE_LOG(ERR, APP, "No port. ( port = %d:%d )\n",
					port->iface_type, port->iface_no);
			return SPP_RET_NG;
		}
		if (unlikely(port_info->iface_type == UNDEF)) {
			RTE_LOG(ERR, APP, "Port not added. ( port = %d:%d )\n",
					port->iface_type, port->iface_no);
			return SPP_RET_NG;
		}

		if (action == SPP_CMD_ACTION_DEL) {
			/* Delete */
			if ((port_info->mac_addr != 0) &&
					unlikely(port_info->mac_addr !=
							mac_addr)) {
				RTE_LOG(ERR, APP, "MAC address is different. ( mac = %s )\n",
						data);
				return SPP_RET_NG;
			}

			port_info->mac_addr = 0;
			memset(port_info->mac_addr_str, 0x00, SPP_MIN_STR_LEN);
		} else if (action == SPP_CMD_ACTION_ADD) {
			/* Setting */
			if (unlikely(port_info->mac_addr != 0)) {
				RTE_LOG(ERR, APP, "Port in used. ( port = %d:%d )\n",
						port->iface_type,
						port->iface_no);
				return SPP_RET_NG;
			}

			port_info->mac_addr = mac_addr;
			strcpy(port_info->mac_addr_str, data);
		}
	}

	set_component_change_port(port_info, SPP_PORT_RXTX_TX);
	return SPP_RET_OK;
}

/* Get free component */
static int
get_free_component(void)
{
	int cnt = 0;
	for (cnt = 0; cnt < RTE_MAX_LCORE; cnt++) {
		if (g_component_info[cnt].type == SPP_COMPONENT_UNUSE)
			return cnt;
	}
	return -1;
}

/* Get name matching component */
int
spp_get_component_id(const char *name)
{
	int cnt = 0;
	if (name[0] == '\0')
		return SPP_RET_NG;

	for (cnt = 0; cnt < RTE_MAX_LCORE; cnt++) {
		if (strcmp(name, g_component_info[cnt].name) == 0)
			return cnt;
	}
	return SPP_RET_NG;
}

static int
get_del_core_element(int info, int num, int *array)
{
	int cnt;
	int match = -1;
	int max = num;

	for (cnt = 0; cnt < max; cnt++) {
		if (info == array[cnt])
			match = cnt;
	}

	if (match < 0)
		return -1;

	/* Last element is excluded from movement. */
	max--;

	for (cnt = match; cnt < max; cnt++)
		array[cnt] = array[cnt+1];

	/* Last element is cleared. */
	array[cnt] = 0;
	return 0;
}

/* Component command to execute it */
int
spp_update_component(
		enum spp_command_action action,
		const char *name,
		unsigned int lcore_id,
		enum spp_component_type type)
{
	int ret = SPP_RET_NG;
	int ret_del = -1;
	int component_id = 0;
	unsigned int tmp_lcore_id = 0;
	struct spp_component_info *component = NULL;
	struct core_info *core = NULL;
	struct core_mng_info *info = NULL;

	switch (action) {
	case SPP_CMD_ACTION_START:
		info = &g_core_info[lcore_id];
		if (info->status == SPP_CORE_UNUSE) {
			RTE_LOG(ERR, APP, "Core unavailable.\n");
			return SPP_RET_NG;
		}

		component_id = spp_get_component_id(name);
		if (component_id >= 0) {
			RTE_LOG(ERR, APP, "Component name in used.\n");
			return SPP_RET_NG;
		}

		component_id = get_free_component();
		if (component_id < 0) {
			RTE_LOG(ERR, APP, "Component upper limit is over.\n");
			return SPP_RET_NG;
		}

		core = &info->core[info->upd_index];
		if ((core->type != SPP_COMPONENT_UNUSE) &&
				(core->type != type)) {
			RTE_LOG(ERR, APP, "Component type is error.\n");
			return SPP_RET_NG;
		}

		component = &g_component_info[component_id];
		memset(component, 0x00, sizeof(struct spp_component_info));
		strcpy(component->name, name);
		component->type         = type;
		component->lcore_id     = lcore_id;
		component->component_id = component_id;

		core->type = type;
		core->id[core->num] = component_id;
		core->num++;
		ret = SPP_RET_OK;
		tmp_lcore_id = lcore_id;
		g_change_component[component_id] = 1;
		break;

	case SPP_CMD_ACTION_STOP:
		component_id = spp_get_component_id(name);
		if (component_id < 0)
			return SPP_RET_OK;

		component = &g_component_info[component_id];
		tmp_lcore_id = component->lcore_id;
		memset(component, 0x00, sizeof(struct spp_component_info));

		info = &g_core_info[tmp_lcore_id];
		core = &info->core[info->upd_index];
		ret_del = get_del_core_element(component_id,
				core->num, core->id);
		if (ret_del >= 0)
			/* If deleted, decrement number. */
			core->num--;

		if (core->num == 0)
			core->type = SPP_COMPONENT_UNUSE;

		ret = SPP_RET_OK;
		g_change_component[component_id] = 0;
		break;

	default:
		break;
	}

	g_change_core[tmp_lcore_id] = 1;
	return ret;
}

static int
check_port_element(
		struct spp_port_info *info,
		int num,
		struct spp_port_info *array[])
{
	int cnt = 0;
	int match = -1;
	for (cnt = 0; cnt < num; cnt++) {
		if (info == array[cnt])
			match = cnt;
	}
	return match;
}

static int
get_del_port_element(
		struct spp_port_info *info,
		int num,
		struct spp_port_info *array[])
{
	int cnt = 0;
	int match = -1;
	int max = num;

	match = check_port_element(info, num, array);
	if (match < 0)
		return -1;

	/* Last element is excluded from movement. */
	max--;

	for (cnt = match; cnt < max; cnt++)
		array[cnt] = array[cnt+1];

	/* Last element is cleared. */
	array[cnt] = NULL;
	return 0;
}

/* Port add or del to execute it */
int
spp_update_port(enum spp_command_action action,
		const struct spp_port_index *port,
		enum spp_port_rxtx rxtx,
		const char *name)
{
	int ret = SPP_RET_NG;
	int ret_check = -1;
	int ret_del = -1;
	int component_id = 0;
	struct spp_component_info *component = NULL;
	struct spp_port_info *port_info = NULL;
	int *num = NULL;
	struct spp_port_info **ports = NULL;

	component_id = spp_get_component_id(name);
	if (component_id < 0) {
		RTE_LOG(ERR, APP, "Unknown component by port command. (component = %s)\n",
				name);
		return SPP_RET_NG;
	}

	component = &g_component_info[component_id];
	port_info = get_iface_info(port->iface_type, port->iface_no);
	if (rxtx == SPP_PORT_RXTX_RX) {
		num = &component->num_rx_port;
		ports = component->rx_ports;
	} else {
		num = &component->num_tx_port;
		ports = component->tx_ports;
	}

	switch (action) {
	case SPP_CMD_ACTION_ADD:
		ret_check = check_port_element(port_info, *num, ports);
		if (ret_check >= 0)
			return SPP_RET_OK;

		if (*num >= RTE_MAX_ETHPORTS) {
			RTE_LOG(ERR, APP, "Port upper limit is over.\n");
			break;
		}

		port_info->iface_type = port->iface_type;
		ports[*num] = port_info;
		(*num)++;

		ret = SPP_RET_OK;
		break;

	case SPP_CMD_ACTION_DEL:
		ret_del = get_del_port_element(port_info, *num, ports);
		if (ret_del == 0)
			(*num)--; /* If deleted, decrement number. */
		ret = SPP_RET_OK;
		break;
	default:
		break;
	}

	g_change_component[component_id] = 1;
	return ret;
}

/* Flush initial setting of each interface. */
static int
flush_port(void)
{
	int ret = 0;
	int cnt = 0;
	struct spp_port_info *port = NULL;

	/* Initialize added vhost. */
	for (cnt = 0; cnt < RTE_MAX_ETHPORTS; cnt++) {
		port = &g_iface_info.vhost[cnt];
		if ((port->iface_type != UNDEF) && (port->dpdk_port < 0)) {
			ret = add_vhost_pmd(port->iface_no,
					g_startup_param.vhost_client);
			if (ret < 0)
				return SPP_RET_NG;
			port->dpdk_port = ret;
		}
	}

	/* Initialize added ring. */
	for (cnt = 0; cnt < RTE_MAX_ETHPORTS; cnt++) {
		port = &g_iface_info.ring[cnt];
		if ((port->iface_type != UNDEF) && (port->dpdk_port < 0)) {
			ret = add_ring_pmd(port->iface_no);
			if (ret < 0)
				return SPP_RET_NG;
			port->dpdk_port = ret;
		}
	}
	return SPP_RET_OK;
}

/* Flush changed core. */
static void
flush_core(void)
{
	int cnt = 0;
	struct core_mng_info *info = NULL;

	/* Changed core has changed index. */
	for (cnt = 0; cnt < RTE_MAX_LCORE; cnt++) {
		if (g_change_core[cnt] != 0) {
			info = &g_core_info[cnt];
			info->upd_index = info->ref_index;
		}
	}

	/* Waiting for changed core change. */
	for (cnt = 0; cnt < RTE_MAX_LCORE; cnt++) {
		if (g_change_core[cnt] != 0) {
			info = &g_core_info[cnt];
			while (likely(info->ref_index == info->upd_index))
				rte_delay_us_block(SPP_CHANGE_UPDATE_INTERVAL);

			memcpy(&info->core[info->upd_index],
					&info->core[info->ref_index],
					sizeof(struct core_info));
		}
	}
}

/* Flush changed component */
static int
flush_component(void)
{
	int ret = 0;
	int cnt = 0;
	struct spp_component_info *component_info = NULL;

	for (cnt = 0; cnt < RTE_MAX_LCORE; cnt++) {
		if (g_change_component[cnt] == 0)
			continue;

		component_info = &g_component_info[cnt];
		if (component_info->type == SPP_COMPONENT_CLASSIFIER_MAC)
			ret = spp_classifier_mac_update(component_info);
		else
			ret = spp_forward_update(component_info);

		if (unlikely(ret < 0)) {
			RTE_LOG(ERR, APP, "Flush error. ( component = %s, type = %d)\n",
					component_info->name,
					component_info->type);
			return SPP_RET_NG;
		}
	}
	return SPP_RET_OK;
}

/* Flush command to execute it */
int
spp_flush(void)
{
	int ret = -1;

	/* Initial setting of each interface. */
	ret = flush_port();
	if (ret < 0)
		return ret;

	/* Flush of core index. */
	flush_core();

	/* Flush of component */
	ret = flush_component();

	backup_mng_info(&g_backup_info);
	return ret;
}

/* Cancel data that is not flushing */
void
spp_cancel(void)
{
	cancel_mng_info(&g_backup_info);
}

/* Iterate core information */
int
spp_iterate_core_info(struct spp_iterate_core_params *params)
{
	int ret;
	int lcore_id, cnt;
	struct core_info *core = NULL;

	RTE_LCORE_FOREACH_SLAVE(lcore_id) {
		if (spp_get_core_status(lcore_id) == SPP_CORE_UNUSE)
			continue;

		core = get_core_info(lcore_id);
		if (core->num == 0) {
			ret = (*params->element_proc)(
				params, lcore_id,
				"", SPP_TYPE_UNUSE_STR,
				0, NULL, 0, NULL);
			if (unlikely(ret != 0)) {
				RTE_LOG(ERR, APP, "Cannot iterate core information. "
						"(core = %d, type = %d)\n",
						lcore_id, SPP_COMPONENT_UNUSE);
				return SPP_RET_NG;
			}
			continue;
		}

		for (cnt = 0; cnt < core->num; cnt++) {
			if (core->type == SPP_COMPONENT_CLASSIFIER_MAC) {
				ret = spp_classifier_get_component_status(
						lcore_id,
						core->id[cnt],
						params);
			} else {
				ret = spp_forward_get_component_status(
						lcore_id,
						core->id[cnt],
						params);
			}
			if (unlikely(ret != 0)) {
				RTE_LOG(ERR, APP, "Cannot iterate core information. "
						"(core = %d, type = %d)\n",
						lcore_id, core->type);
				return SPP_RET_NG;
			}
		}
	}

	return SPP_RET_OK;
}

/* Iterate Classifier_table */
int
spp_iterate_classifier_table(
		struct spp_iterate_classifier_table_params *params)
{
	int ret;

	ret = spp_classifier_mac_iterate_table(params);
	if (unlikely(ret != 0)) {
		RTE_LOG(ERR, APP, "Cannot iterate classifier_mac_table.\n");
		return SPP_RET_NG;
	}

	return SPP_RET_OK;
}

/**
 * Separate port id of combination of iface type and number and
 * assign to given argument, iface_type and iface_no.
 *
 * For instance, 'ring:0' is separated to 'ring' and '0'.
 */
int
spp_get_iface_index(const char *port, enum port_type *iface_type, int *iface_no)
{
	enum port_type type = UNDEF;
	const char *no_str = NULL;
	char *endptr = NULL;

	/* Find out which type of interface from port */
	if (strncmp(port, SPP_IFTYPE_NIC_STR ":",
			strlen(SPP_IFTYPE_NIC_STR)+1) == 0) {
		/* NIC */
		type = PHY;
		no_str = &port[strlen(SPP_IFTYPE_NIC_STR)+1];
	} else if (strncmp(port, SPP_IFTYPE_VHOST_STR ":",
			strlen(SPP_IFTYPE_VHOST_STR)+1) == 0) {
		/* VHOST */
		type = VHOST;
		no_str = &port[strlen(SPP_IFTYPE_VHOST_STR)+1];
	} else if (strncmp(port, SPP_IFTYPE_RING_STR ":",
			strlen(SPP_IFTYPE_RING_STR)+1) == 0) {
		/* RING */
		type = RING;
		no_str = &port[strlen(SPP_IFTYPE_RING_STR)+1];
	} else {
		/* OTHER */
		RTE_LOG(ERR, APP, "Unknown interface type. (port = %s)\n",
				port);
		return -1;
	}

	/* Change type of number of interface */
	int ret_no = strtol(no_str, &endptr, 0);
	if (unlikely(no_str == endptr) || unlikely(*endptr != '\0')) {
		/* No IF number */
		RTE_LOG(ERR, APP, "No interface number. (port = %s)\n", port);
		return -1;
	}

	*iface_type = type;
	*iface_no = ret_no;

	RTE_LOG(DEBUG, APP, "Port = %s => Type = %d No = %d\n",
			port, *iface_type, *iface_no);
	return 0;
}

/**
 * Generate a formatted string of combination from interface type and
 * number and assign to given 'port'
 */
int spp_format_port_string(char *port, enum port_type iface_type, int iface_no)
{
	const char *iface_type_str;

	switch (iface_type) {
	case PHY:
		iface_type_str = SPP_IFTYPE_NIC_STR;
		break;
	case RING:
		iface_type_str = SPP_IFTYPE_RING_STR;
		break;
	case VHOST:
		iface_type_str = SPP_IFTYPE_VHOST_STR;
		break;
	default:
		return -1;
	}

	sprintf(port, "%s:%d", iface_type_str, iface_no);

	return 0;
}

/**
 * Change mac address of 'aa:bb:cc:dd:ee:ff' to int64 and return it
 */
int64_t
spp_change_mac_str_to_int64(const char *mac)
{
	int64_t ret_mac = 0;
	int64_t token_val = 0;
	int token_cnt = 0;
	char tmp_mac[SPP_MIN_STR_LEN];
	char *str = tmp_mac;
	char *saveptr = NULL;
	char *endptr = NULL;

	RTE_LOG(DEBUG, APP, "MAC address change. (mac = %s)\n", mac);

	strcpy(tmp_mac, mac);
	while (1) {
		/* Split by colon(':') */
		char *ret_tok = strtok_r(str, ":", &saveptr);
		if (unlikely(ret_tok == NULL))
			break;

		/* Check for mal-formatted address */
		if (unlikely(token_cnt >= ETHER_ADDR_LEN)) {
			RTE_LOG(ERR, APP, "MAC address format error. (mac = %s)\n",
					mac);
			return -1;
		}

		/* Convert string to hex value */
		int ret_tol = strtol(ret_tok, &endptr, 16);
		if (unlikely(ret_tok == endptr) || unlikely(*endptr != '\0'))
			break;

		/* Append separated value to the result */
		token_val = (int64_t)ret_tol;
		ret_mac |= token_val << (token_cnt * 8);
		token_cnt++;
		str = NULL;
	}

	RTE_LOG(DEBUG, APP, "MAC address change. (mac = %s => 0x%08lx)\n",
			 mac, ret_mac);
	return ret_mac;
}

/**
 * Return the type of forwarder as a member of enum of spp_component_type
 */
enum spp_component_type
spp_change_component_type(const char *type_str)
{
	if (strncmp(type_str, CORE_TYPE_CLASSIFIER_MAC_STR,
			 strlen(CORE_TYPE_CLASSIFIER_MAC_STR)+1) == 0) {
		/* Classifier */
		return SPP_COMPONENT_CLASSIFIER_MAC;
	} else if (strncmp(type_str, CORE_TYPE_MERGE_STR,
			 strlen(CORE_TYPE_MERGE_STR)+1) == 0) {
		/* Merger */
		return SPP_COMPONENT_MERGE;
	} else if (strncmp(type_str, CORE_TYPE_FORWARD_STR,
			 strlen(CORE_TYPE_FORWARD_STR)+1) == 0) {
		/* Forwarder */
		return SPP_COMPONENT_FORWARD;
	}
	return SPP_COMPONENT_UNUSE;
}