DPDK logo

Elixir Cross Referencer

   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
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2015-2016 Intel Corporation
 * Copyright(c) 2019 Nippon Telegraph and Telephone Corporation
 */

#include <signal.h>
#include <arpa/inet.h>
#include <inttypes.h>
#include <poll.h>
#include <fcntl.h>

#include <rte_atomic.h>
#include <rte_eth_ring.h>

#include "shared/common.h"
#include "args.h"
#include "init.h"
#include "primary.h"
#include "primary/flow/flow.h"

#include "shared/port_manager.h"
#include "shared/secondary/add_port.h"
#include "shared/secondary/utils.h"

/*
 * Buffer sizes of status message of primary. Total number of size
 * must be equal to MSG_SIZE 32768 defined in `shared/common.h`.
 */
#define PRI_BUF_SIZE_LCORE 128
#define PRI_BUF_SIZE_PHY 30720
#define PRI_BUF_SIZE_PIPE 512
#define PRI_BUF_SIZE_RING \
	(MSG_SIZE - PRI_BUF_SIZE_LCORE - PRI_BUF_SIZE_PHY - PRI_BUF_SIZE_PIPE)

#define SPP_PATH_LEN 1024  /* seems enough for path of spp procs */
#define NOF_TOKENS 48  /* seems enough to contain tokens */
/* should be contain extra two tokens for `python` and path of launcher */
#define NOF_CMD_LIST (NOF_TOKENS + 2)

#define LAUNCH_CMD "python"
#define LCMD_LEN 8

#define LAUNCHER_NAME "sec_launcher.py"
#define LAUNCHER_LEN 16

#define POLL_TIMEOUT_MS 100

/**
 * Set of port id and type of resource UID, such as `vhost:1`. It is intended
 * to be used for mapping to ethdev ID. as port_id_list.
 */
struct port_id_map {
	int port_id;
	enum port_type type;
	int rx_ring_id, tx_ring_id;  /* for pipe */
};

struct port_id_map port_id_list[RTE_MAX_ETHPORTS];

static sig_atomic_t on = 1;
static enum cmd_type cmd = STOP;
static struct pollfd pfd;

/* global var - extern in header */
uint8_t lcore_id_used[RTE_MAX_LCORE];

static void
turn_off(int sig)
{
	on = 0;
	RTE_LOG(INFO, PRIMARY, "terminated %d\n", sig);
}

static const char *
get_printable_mac_addr(uint16_t port)
{
	static const char err_address[] = "00:00:00:00:00:00";
	static char addresses[RTE_MAX_ETHPORTS][sizeof(err_address)];

	if (unlikely(port >= RTE_MAX_ETHPORTS))
		return err_address;

	if (unlikely(addresses[port][0] == '\0')) {
		struct rte_ether_addr mac;

		rte_eth_macaddr_get(port, &mac);
		snprintf(addresses[port], sizeof(addresses[port]),
			"%02x:%02x:%02x:%02x:%02x:%02x\n",
			mac.addr_bytes[0], mac.addr_bytes[1],
			mac.addr_bytes[2], mac.addr_bytes[3],
			mac.addr_bytes[4], mac.addr_bytes[5]);
	}

	return addresses[port];
}

/*
 * This function displays the recorded statistics for each port
 * and for each client. It uses ANSI terminal codes to clear
 * screen when called. It is called from a single non-master
 * thread in the server process, when the process is run with more
 * than one lcore enabled.
 */
static void
do_stats_display(void)
{
	const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' };
	const char clr[] = { 27, '[', '2', 'J', '\0' };
	unsigned int i;

	/* Clear screen and move to top left */
	printf("%s%s", clr, topLeft);

	printf("PORTS\n");
	printf("-----\n");
	for (i = 0; i < ports->num_ports; i++)
		printf("Port %u: '%s'\t", ports->id[i],
			get_printable_mac_addr(ports->id[i]));
	printf("\n\n");
	for (i = 0; i < ports->num_ports; i++) {
		printf("Port %u - rx: %9"PRIu64"\t tx: %9"PRIu64"\t"
			" tx_drop: %9"PRIu64"\n",
			ports->id[i], ports->port_stats[i].rx,
			ports->port_stats[i].tx,
			ports->port_stats[i].tx_drop);
	}

	printf("\nCLIENTS\n");
	printf("-------\n");
	for (i = 0; i < num_rings; i++) {
		printf("Client %2u - rx: %9"PRIu64", rx_drop: %9"PRIu64"\n"
			"            tx: %9"PRIu64", tx_drop: %9"PRIu64"\n",
			i, ports->client_stats[i].rx,
			ports->client_stats[i].rx_drop,
			ports->client_stats[i].tx,
			ports->client_stats[i].tx_drop);
	}

	printf("\n");
}

/*
 * The function called from each non-master lcore used by the process.
 * The test_and_set function is used to randomly pick a single lcore on which
 * the code to display the statistics will run. Otherwise, the code just
 * repeatedly sleeps.
 */
static int
sleep_lcore(void *dummy __rte_unused)
{
	/* Used to pick a display thread - static, so zero-initialised */
	static rte_atomic32_t display_stats;

	/* Only one core should display stats */
	if (rte_atomic32_test_and_set(&display_stats)) {
		const unsigned int sleeptime = 1;

		RTE_LOG(INFO, PRIMARY, "Core %u displaying statistics\n",
				rte_lcore_id());

		/* Longer initial pause so above log is seen */
		sleep(sleeptime * 3);

		/* Loop forever: sleep always returns 0 or <= param */
		while (sleep(sleeptime) <= sleeptime && on)
			do_stats_display();
	}

	return 0;
}

/* main processing loop for forwarding. */
static void
forward_loop(void)
{
	unsigned int lcore_id = rte_lcore_id();

	RTE_LOG(INFO, PRIMARY, "entering main loop on lcore %u\n", lcore_id);

	while (1) {
		if (unlikely(cmd == STOP)) {
			sleep(1);
			continue;
		} else if (cmd == FORWARD) {
			forward();
		}
	}
}

/* leading to forward loop. */
static int
main_loop(void *dummy __rte_unused)
{
	forward_loop();
	return 0;
}

/*
 * Function to set all the client statistic values to zero.
 * Called at program startup.
 */
static void
clear_stats(void)
{
	memset(ports->port_stats, 0, sizeof(struct stats) * RTE_MAX_ETHPORTS);
	memset(ports->client_stats, 0, sizeof(struct stats) * MAX_CLIENT);
}

static int
do_send(int *connected, int *sock, char *str)
{
	int ret;

	ret = send(*sock, str, MSG_SIZE, 0);
	if (ret == -1) {
		RTE_LOG(ERR, PRIMARY, "Failed to send\n");
		*connected = 0;
		return -1;
	}

	RTE_LOG(INFO, PRIMARY, "To Server: %s\n", str);

	return 0;
}

/* Get directory name of given proc_name */
static int get_sec_dir(char *proc_name, char *dir_name)
{
	if (!strcmp(proc_name, "spp_nfv")) {
		sprintf(dir_name, "%s", "nfv");
		RTE_LOG(DEBUG, PRIMARY, "Found dir 'nfv' for '%s'.\n",
				proc_name);
	} else if (!strcmp(proc_name, "spp_vf")) {
		sprintf(dir_name, "%s", "vf");
		RTE_LOG(DEBUG, PRIMARY, "Found dir 'vf' for '%s'.\n",
				proc_name);
	} else if (!strcmp(proc_name, "spp_mirror")) {
		sprintf(dir_name, "%s", "mirror");
		RTE_LOG(DEBUG, PRIMARY, "Found dir 'mirror' for '%s'.\n",
				proc_name);
	} else if (!strcmp(proc_name, "spp_pcap")) {
		sprintf(dir_name, "%s", "pcap");
		RTE_LOG(DEBUG, PRIMARY, "Found dir 'pcap' for '%s'.\n",
				proc_name);
	} else {
		RTE_LOG(DEBUG, PRIMARY, "No dir found for '%s'.\n",
				proc_name);
	}
	return 0;
}

/**
 * Launch secondary process of given name and ID.
 *
 * This function finds the path of secondary by using the path of spp_primary
 * itself and given proc name.
 *
 * Output of launched proc is sent to logfile located in `log` directory in
 * the project root, and the name of logfile is a combination of proc name
 * and ID, such as `spp_nfv-1.log`.
 */
static int
launch_sec_proc(char *sec_name, int sec_id, char **sec_args)
{
	char path_spp_pri[SPP_PATH_LEN];
	char path_spp_sec[SPP_PATH_LEN];
	char path_spp_log[SPP_PATH_LEN];

	char cmd[LCMD_LEN];  /* Command for launcher */
	char launcher[LAUNCHER_LEN];  /* Name of launcher script */
	char path_launcher[SPP_PATH_LEN];

	/* Contains elems of path_spp_pri */
	char *token_list[NOF_TOKENS] = {NULL};

	/* Contains cmd and args for execvp() */
	char *cmd_list[NOF_CMD_LIST] = {NULL};

	int num_token = 0;
	int i = 0;
	char sec_dirname[16];
	int fd;

	/* Get path of spp_primary to be used to find secondary */
	if (readlink("/proc/self/exe",
				path_spp_pri, sizeof(path_spp_pri)) == -1)
		RTE_LOG(INFO, PRIMARY,
				"Failed to find exec file of spp_primary.\n");
	else {
		/**
		 * TODO(yasufum) confirm that log dir and helper tool are
		 * existing.
		 */
		/* Tokenize path of spp_primary */
		token_list[i] = strtok(path_spp_pri, "/");
		while (token_list[i] != NULL) {
			i++;
			num_token++;
			token_list[i] = strtok(NULL, "/");
		}

		/* Get path of `src` dir used as a basename */
		for (i = 0; i < num_token - 3; i++) {
			if (i == 0)
				sprintf(path_spp_sec, "/%s/", token_list[i]);
			else
				sprintf(path_spp_sec + strlen(path_spp_sec),
						"%s/", token_list[i]);
		}

		/* Dir of logfile is located in the parent dir of src */
		sprintf(path_spp_log, "%s../log/%s-%d.log",
				path_spp_sec, sec_name, sec_id);

		/* Setup cmd list */
		memset(cmd, '\0', sizeof(cmd));
		sprintf(cmd, "%s", LAUNCH_CMD);
		cmd_list[0] = cmd;

		/* Path of launcher is `spp/tools/helpers/sec_launcher.py` */
		memset(launcher, '\0', sizeof(launcher));
		sprintf(launcher, "%s", LAUNCHER_NAME);
		memset(path_launcher, '\0', sizeof(path_launcher));
		sprintf(path_launcher, "%s../tools/helpers/%s",
				path_spp_sec, launcher);
		cmd_list[1] = path_launcher;

		/* path of sec proc */
		get_sec_dir(sec_name, sec_dirname);
		sprintf(path_spp_sec + strlen(path_spp_sec), "%s/%s/%s",
				sec_dirname, token_list[num_token-2],
				sec_name);
		cmd_list[2] = path_spp_sec;

		i = 0;
		while (sec_args[i] != NULL) {
			cmd_list[i+3] = sec_args[i];
			i++;
		}

		/* Output debug log for checking tokens of cmd_list */
		i = 0;
		while (cmd_list[i] != NULL) {
			RTE_LOG(DEBUG, PRIMARY, "launch, token[%2d] = %s\n",
					i, cmd_list[i]);
			i++;
		}

		RTE_LOG(DEBUG, PRIMARY, "sec_path: '%s'.\n", path_spp_sec);
		RTE_LOG(DEBUG, PRIMARY, "sec_log: '%s'.\n", path_spp_log);

		pid_t pid;
		pid = fork();
		if (pid < 0)
			RTE_LOG(ERR, PRIMARY,
					"Failed to open secondary proc.\n");
		else if (pid == 0) {
			/* Open log file with mode equals to 'a+' */
			fd = open(path_spp_log,
					O_RDWR | O_CREAT | O_APPEND,
					0666);

			/* change to output of stdout and stderr to logfile */
			dup2(fd, 1);
			dup2(fd, 2);
			close(fd);

			printf("%s\n", sec_args[0]);
			if (execvp(cmd_list[0], cmd_list) != 0)
				RTE_LOG(ERR, PRIMARY,
					"Failed to open child proc!\n");
		} else
			RTE_LOG(INFO, PRIMARY, "Launched '%s' with ID %d.\n",
					path_spp_sec, sec_id);
	}

	return 0;
}

/* TODO(yasufum): change to use shared */
static int
append_lcore_info_json(char *str,
		uint8_t lcore_id_used[RTE_MAX_LCORE])
{
	int i;
	sprintf(str + strlen(str), "\"master-lcore\":%d,",
			rte_get_master_lcore());
	sprintf(str + strlen(str), "\"lcores\":[");
	for (i = 0; i < RTE_MAX_LCORE; i++) {
		if (lcore_id_used[i] == 1)
			sprintf(str + strlen(str), "%d,", i);
	}

	/* Remove last ','. */
	sprintf(str + strlen(str) - 1, "%s", "]");
	return 0;
}

/* TODO(yasufum): change to use shared */
static int
append_port_info_json(char *str)
{
	unsigned int i;
	unsigned int has_port = 0;  // for checking having port at last

	sprintf(str + strlen(str), "\"ports\":[");
	for (i = 0; i < RTE_MAX_ETHPORTS; i++) {

		if (ports_fwd_array[i][0].in_port_id == PORT_RESET)
			continue;

		has_port = 1;
		switch (port_map[i].port_type) {
		case PHY:
			sprintf(str + strlen(str), "\"phy:%u\",",
					port_map[i].id);
			break;
		case RING:
			sprintf(str + strlen(str), "\"ring:%u\",",
				port_map[i].id);
			break;
		case VHOST:
			sprintf(str + strlen(str), "\"vhost:%u\",",
				port_map[i].id);
			break;
		case PCAP:
			sprintf(str + strlen(str), "\"pcap:%u\",",
					port_map[i].id);
			break;
		case NULLPMD:
			sprintf(str + strlen(str), "\"nullpmd:%u\",",
					port_map[i].id);
			break;
		case TAP:
			sprintf(str + strlen(str), "\"tap:%u\",",
					port_map[i].id);
			break;
		case MEMIF:
			sprintf(str + strlen(str), "\"memif:%u\",",
					port_map[i].id);
			break;
		case PIPE:
			sprintf(str + strlen(str), "\"pipe:%u\",",
					port_map[i].id);
			break;
		case UNDEF:
			/* TODO(yasufum) Need to remove print for undefined ? */
			sprintf(str + strlen(str), "\"udf\",");
			break;
		}
	}

	/* Check if it has at least one port to remove ",". */
	if (has_port == 0) {
		sprintf(str + strlen(str), "]");
	} else {  /* Remove last ',' .*/
		sprintf(str + strlen(str) - 1, "]");
	}

	return 0;
}

/* TODO(yasufum): change to use shared */
static int
append_patch_info_json(char *str)
{
	unsigned int i;
	unsigned int has_patch = 0;  // for checking having patch at last

	char patch_str[128];
	sprintf(str + strlen(str), "\"patches\":[");
	for (i = 0; i < RTE_MAX_ETHPORTS; i++) {

		if (ports_fwd_array[i][0].in_port_id == PORT_RESET)
			continue;

		RTE_LOG(INFO, PRIMARY, "Port ID %d\n", i);
		RTE_LOG(INFO, PRIMARY, "Status %d\n",
			ports_fwd_array[i][0].in_port_id);

		memset(patch_str, '\0', sizeof(patch_str));

		sprintf(patch_str, "{\"src\":");

		switch (port_map[i].port_type) {
		case PHY:
			RTE_LOG(INFO, PRIMARY, "Type: PHY\n");
			sprintf(patch_str + strlen(patch_str),
					"\"phy:%u\",",
					port_map[i].id);
			break;
		case RING:
			RTE_LOG(INFO, PRIMARY, "Type: RING\n");
			sprintf(patch_str + strlen(patch_str),
					"\"ring:%u\",",
					port_map[i].id);
			break;
		case VHOST:
			RTE_LOG(INFO, PRIMARY, "Type: VHOST\n");
			sprintf(patch_str + strlen(patch_str),
					"\"vhost:%u\",",
					port_map[i].id);
			break;
		case PCAP:
			RTE_LOG(INFO, PRIMARY, "Type: PCAP\n");
			sprintf(patch_str + strlen(patch_str),
					"\"pcap:%u\",",
					port_map[i].id);
			break;
		case NULLPMD:
			RTE_LOG(INFO, PRIMARY, "Type: NULLPMD\n");
			sprintf(patch_str + strlen(patch_str),
					"\"nullpmd:%u\",",
					port_map[i].id);
			break;
		case TAP:
			RTE_LOG(INFO, PRIMARY, "Type: TAP\n");
			sprintf(patch_str + strlen(patch_str),
					"\"tap:%u\",",
					port_map[i].id);
			break;
		case MEMIF:
			RTE_LOG(INFO, PRIMARY, "Type: MEMIF\n");
			sprintf(patch_str + strlen(patch_str),
					"\"memif:%u\",",
					port_map[i].id);
			break;
		case PIPE:
			RTE_LOG(INFO, SHARED, "Type: PIPE\n");
			sprintf(patch_str + strlen(patch_str),
					"\"pipe:%u\",",
					port_map[i].id);
			break;
		case UNDEF:
			RTE_LOG(INFO, PRIMARY, "Type: UDF\n");
			/* TODO(yasufum) Need to remove print for undefined ? */
			sprintf(patch_str + strlen(patch_str),
					"\"udf\",");
			break;
		}

		sprintf(patch_str + strlen(patch_str), "\"dst\":");

		RTE_LOG(INFO, PRIMARY, "Out Port ID %d\n",
				ports_fwd_array[i][0].out_port_id);

		if (ports_fwd_array[i][0].out_port_id == PORT_RESET) {
			//sprintf(patch_str + strlen(patch_str), "%s", "\"\"");
			continue;
		} else {
			has_patch = 1;
			unsigned int j = ports_fwd_array[i][0].out_port_id;
			switch (port_map[j].port_type) {
			case PHY:
				RTE_LOG(INFO, PRIMARY, "Type: PHY\n");
				sprintf(patch_str + strlen(patch_str),
						"\"phy:%u\"",
						port_map[j].id);
				break;
			case RING:
				RTE_LOG(INFO, PRIMARY, "Type: RING\n");
				sprintf(patch_str + strlen(patch_str),
						"\"ring:%u\"",
						port_map[j].id);
				break;
			case VHOST:
				RTE_LOG(INFO, PRIMARY, "Type: VHOST\n");
				sprintf(patch_str + strlen(patch_str),
						"\"vhost:%u\"",
						port_map[j].id);
				break;
			case PCAP:
				RTE_LOG(INFO, PRIMARY, "Type: PCAP\n");
				sprintf(patch_str + strlen(patch_str),
						"\"pcap:%u\"",
						port_map[j].id);
				break;
			case NULLPMD:
				RTE_LOG(INFO, PRIMARY, "Type: NULLPMD\n");
				sprintf(patch_str + strlen(patch_str),
						"\"nullpmd:%u\"",
						port_map[j].id);
				break;
			case TAP:
				RTE_LOG(INFO, PRIMARY, "Type: TAP\n");
				sprintf(patch_str + strlen(patch_str),
						"\"tap:%u\"",
						port_map[j].id);
				break;
			case MEMIF:
				RTE_LOG(INFO, PRIMARY, "Type: MEMIF\n");
				sprintf(patch_str + strlen(patch_str),
						"\"memif:%u\"",
						port_map[j].id);
				break;
			case PIPE:
				RTE_LOG(INFO, SHARED, "Type: PIPE\n");
				sprintf(patch_str + strlen(patch_str),
						"\"pipe:%u\"",
						port_map[j].id);
				break;
			case UNDEF:
				RTE_LOG(INFO, PRIMARY, "Type: UDF\n");
				/*
				 * TODO(yasufum) Need to remove print for
				 * undefined ?
				 */
				sprintf(patch_str + strlen(patch_str),
						"\"udf\"");
				break;
			}
		}

		sprintf(patch_str + strlen(patch_str), "},");

		if (has_patch != 0)
			sprintf(str + strlen(str), "%s", patch_str);
	}


	/* Check if it has at least one patch to remove ",". */
	if (has_patch == 0) {
		sprintf(str + strlen(str), "]");
	} else {  /* Remove last ','. */
		sprintf(str + strlen(str) - 1, "]");
	}

	return 0;
}

static int
forwarder_status_json(char *str)
{
	char buf_running[64];
	char buf_ports[256];
	char buf_patches[256];
	memset(buf_running, '\0', sizeof(buf_running));
	memset(buf_ports, '\0', sizeof(buf_ports));
	memset(buf_patches, '\0', sizeof(buf_patches));

	sprintf(buf_running + strlen(buf_running), "\"status\":");
	if (cmd == FORWARD)
		sprintf(buf_running + strlen(buf_running), "\"%s\"", "running");
	else
		sprintf(buf_running + strlen(buf_running), "\"%s\"", "idling");

	append_port_info_json(buf_ports);
	append_patch_info_json(buf_patches);

	sprintf(str, "\"forwarder\":{%s,%s,%s}", buf_running, buf_ports,
			buf_patches);
	return 0;
}

static int
phy_port_stats_json(char *str)
{
	int i, ret;
	int buf_size = PRI_BUF_SIZE_PHY - 512;  /* size of temp buffer */
	char phy_port[PRI_BUF_SIZE_PHY];
	char flow[buf_size];
	char buf_phy_ports[PRI_BUF_SIZE_PHY];
	memset(phy_port, '\0', sizeof(phy_port));
	memset(buf_phy_ports, '\0', sizeof(buf_phy_ports));

	for (i = 0; i < ports->num_ports; i++) {

		RTE_LOG(DEBUG, PRIMARY, "Size of buf_phy_ports str: %d\n",
				(int)strlen(buf_phy_ports));

		memset(phy_port, '\0', PRI_BUF_SIZE_PHY);
		memset(flow, '\0', buf_size);

		ret = append_flow_json(i, buf_size, flow);
		if (ret != 0) {
			sprintf(buf_phy_ports + strlen(buf_phy_ports) - 1,
					"%s", "");
			break;
		}

		sprintf(phy_port, "{\"id\":%u,\"eth\":\"%s\","
				"\"rx\":%"PRIu64",\"tx\":%"PRIu64","
				"\"tx_drop\":%"PRIu64","
				"\"nof_queues\":{\"rx\":%d,\"tx\":%d},"
				"\"flow\":%s}",
				ports->id[i],
				get_printable_mac_addr(ports->id[i]),
				ports->port_stats[i].rx,
				ports->port_stats[i].tx,
				ports->port_stats[i].tx_drop,
				ports->queue_info[i].rxq,
				ports->queue_info[i].txq,
				flow);

		int cur_buf_size = (int)strlen(buf_phy_ports) +
			(int)strlen(phy_port);
		if (cur_buf_size > PRI_BUF_SIZE_PHY - 1) {
			RTE_LOG(ERR, PRIMARY,
				"Cannot send all of phy_port stats (%d/%d)\n",
				i, ports->num_ports);
			sprintf(buf_phy_ports + strlen(buf_phy_ports) - 1,
					"%s", "");
			break;
		}

		sprintf(buf_phy_ports + strlen(buf_phy_ports), "%s", phy_port);

		if (i < ports->num_ports - 1)
			sprintf(buf_phy_ports, "%s,", buf_phy_ports);
	}
	sprintf(str, "\"phy_ports\":[%s]", buf_phy_ports);
	return 0;
}

static int
ring_port_stats_json(char *str)
{
	int i;
	int buf_size = 256;  /* size of temp buffer */
	char buf_ring_ports[PRI_BUF_SIZE_RING];
	char ring_port[buf_size];
	memset(ring_port, '\0', sizeof(ring_port));
	memset(buf_ring_ports, '\0', sizeof(buf_ring_ports));

	for (i = 0; i < num_rings; i++) {

		RTE_LOG(DEBUG, PRIMARY, "Size of buf_ring_ports str: %d\n",
				(int)strlen(buf_ring_ports));

		memset(ring_port, '\0', buf_size);

		sprintf(ring_port, "{\"id\":%u,\"rx\":%"PRIu64","
			"\"rx_drop\":%"PRIu64","
			"\"tx\":%"PRIu64",\"tx_drop\":%"PRIu64"}",
			i,
			ports->client_stats[i].rx,
			ports->client_stats[i].rx_drop,
			ports->client_stats[i].tx,
			ports->client_stats[i].tx_drop);

		int cur_buf_size = (int)strlen(buf_ring_ports) +
			(int)strlen(ring_port);
		if (cur_buf_size >  PRI_BUF_SIZE_RING - 1) {
			RTE_LOG(ERR, PRIMARY,
				"Cannot send all of ring_port stats (%d/%d)\n",
				i, num_rings);
			sprintf(buf_ring_ports + strlen(buf_ring_ports) - 1,
					"%s", "");
			break;
		}

		sprintf(buf_ring_ports + strlen(buf_ring_ports),
				"%s", ring_port);

		if (i < num_rings - 1)
			sprintf(buf_ring_ports, "%s,", buf_ring_ports);
	}
	sprintf(str, "\"ring_ports\":[%s]", buf_ring_ports);
	return 0;
}

static int
pipes_json(char *str)
{
	uint16_t dev_id;
	char pipe_buf[30];  /* it is enough if port_id < 1000 */
	int find = 0;

	strcpy(str, "\"pipes\":[");
	for (dev_id = 0; dev_id < RTE_MAX_ETHPORTS; dev_id++) {
		if (port_id_list[dev_id].type != PIPE)
			continue;
		sprintf(pipe_buf, "{\"id\":%d,\"rx\":%d,\"tx\":%d}",
				port_id_list[dev_id].port_id,
				port_id_list[dev_id].rx_ring_id,
				port_id_list[dev_id].tx_ring_id);
		if (strlen(str) + strlen(pipe_buf) > PRI_BUF_SIZE_PIPE - 3) {
			RTE_LOG(ERR, PRIMARY, "Cannot send all of pipes\n");
			break;
		}
		if (find)
			strcat(str, ",");
		find = 1;
		strcat(str, pipe_buf);
	}
	strcat(str, "]");
	return 0;
}

/**
 * Retrieve all of statu of ports as JSON format managed by primary.
 *
 * Here is an exmaple.
 *
 * {
 *     "master-lcore": 0,
 *     "lcores": [0,1],
 *     "forwarder": {
 *         "status": "idling",
 *         "ports": ["phy:0", "phy:1"],
 *         "patches": ["src": "phy:0", "dst": "phy:1"]
 *     },
 *     "ring_ports": [
 *     {
 *         "id": 0,
 *             "rx": 0,
 *             "rx_drop": 0,
 *             "tx": 0,
 *             "tx_drop": 0
 *     },
 *     ...
 *     ],
 *     "phy_ports": [
 *     {
 *         "eth": "56:48:4f:53:54:00",
 *         "id": 0,
 *         "rx": 0,
 *         "tx": 0,
 *         "tx_drop": 0
 *     },
 *     ...
 *     ]
 * }
 */
static int
get_status_json(char *str)
{
	char buf_lcores[PRI_BUF_SIZE_LCORE];
	char buf_phy_ports[PRI_BUF_SIZE_PHY];
	char buf_ring_ports[PRI_BUF_SIZE_RING];
	char buf_pipes[PRI_BUF_SIZE_PIPE];
	memset(buf_phy_ports, '\0', PRI_BUF_SIZE_PHY);
	memset(buf_ring_ports, '\0', PRI_BUF_SIZE_RING);
	memset(buf_lcores, '\0', PRI_BUF_SIZE_LCORE);
	memset(buf_pipes, '\0', PRI_BUF_SIZE_PIPE);

	append_lcore_info_json(buf_lcores, lcore_id_used);
	phy_port_stats_json(buf_phy_ports);
	ring_port_stats_json(buf_ring_ports);
	pipes_json(buf_pipes);

	RTE_LOG(INFO, PRIMARY, "%s, %s, %s\n", buf_phy_ports, buf_ring_ports,
			buf_pipes);

	if (get_forwarding_flg() == 1) {
		char tmp_buf[512];
		memset(tmp_buf, '\0', sizeof(tmp_buf));
		forwarder_status_json(tmp_buf);

		sprintf(str, "{%s,%s,%s,%s,%s}",
				buf_lcores, tmp_buf, buf_phy_ports,
				buf_ring_ports, buf_pipes);

	} else {
		sprintf(str, "{%s,%s,%s,%s}",
				buf_lcores, buf_phy_ports, buf_ring_ports,
				buf_pipes);
	}

	return 0;
}

/**
 * Add a port to spp_primary. Port is given as a resource UID which is a
 * combination of port type and ID like as 'ring:0'.
 */
/* TODO(yasufum) consider to merge do_add in nfv/commands.h */
static int
add_port(char *p_type, int p_id, char **token_list)
{
	uint16_t dev_id;
	uint16_t port_id;
	int res = 0;
	uint16_t cnt = 0;

	for (dev_id = 0; dev_id < RTE_MAX_ETHPORTS; dev_id++) {
		if (port_id_list[dev_id].port_id == PORT_RESET)
			break;
		cnt += 1;
	}

	if (!strcmp(p_type, "vhost")) {
		res = add_vhost_pmd(p_id);
		port_id_list[cnt].port_id = p_id;
		port_id_list[cnt].type = VHOST;

	} else if (!strcmp(p_type, "ring")) {
		res = add_ring_pmd(p_id);
		port_id_list[cnt].port_id = p_id;
		port_id_list[cnt].type = RING;

	} else if (!strcmp(p_type, "pcap")) {
		res = add_pcap_pmd(p_id);
		port_id_list[cnt].port_id = p_id;
		port_id_list[cnt].type = PCAP;

	} else if (!strcmp(p_type, "memif")) {
		res = add_memif_pmd(p_id);
		port_id_list[cnt].port_id = p_id;
		port_id_list[cnt].type = MEMIF;

	} else if (!strcmp(p_type, "nullpmd")) {
		res = add_null_pmd(p_id);
		port_id_list[cnt].port_id = p_id;
		port_id_list[cnt].type = NULLPMD;
	} else if (!strcmp(p_type, "pipe")) {
		char *dummy_type;
		uint16_t dummy_queue;
		res = add_pipe_pmd(p_id, token_list[0], token_list[1]);
		if (res < 0)
			return -1;
		port_id_list[cnt].port_id = p_id;
		port_id_list[cnt].type = PIPE;
		parse_resource_uid(token_list[0], &dummy_type,
				&port_id_list[cnt].rx_ring_id, &dummy_queue);
		parse_resource_uid(token_list[1], &dummy_type,
				&port_id_list[cnt].tx_ring_id, &dummy_queue);
	}

	if (res < 0)
		return -1;

	port_id = (uint16_t) res;
	port_map[port_id].id = p_id;
	port_map[port_id].port_type = port_id_list[cnt].type;
	if (port_map[port_id].port_type == RING)
		port_map[port_id].stats = &ports->client_stats[p_id];
	/* NOTE: port_map[].stats points to &port_map[].default_stats
	 * other than RING. There is no support to show/clear this stats
	 * at the moment.
	 */

	/* Update ports_fwd_array with port id */
	ports_fwd_array[port_id][0].in_port_id = port_id;
	return 0;
}

/* Find ethdev ID from resource UID to delete. */
static uint16_t
find_ethdev_id(int p_id, enum port_type ptype)
{
	int cnt;
	struct port_id_map *p_map;

	RTE_LOG(DEBUG, PRIMARY, "Finding ethdev_id, p_id: %d, ptype: %d\n",
			p_id, ptype);

	for (cnt = 0; cnt < RTE_MAX_ETHPORTS; cnt++) {
		p_map = &port_id_list[cnt];
		if (p_map == NULL)
			RTE_LOG(ERR, PRIMARY, "Failed to find port_id.\n");
		else {
			if (p_map->port_id == p_id &&
					p_map->type == ptype)
				return cnt;
		}
	}

	return PORT_RESET;
}

/* Delete port. */
/* TODO(yasufum) consider to merge do_del in nfv/commands.h */
static int
del_port(char *p_type, int p_id)
{
	uint16_t dev_id = 0;

	if (!strcmp(p_type, "vhost")) {
		dev_id = find_ethdev_id(p_id, VHOST);
		if (dev_id == PORT_RESET)
			return -1;
		dev_detach_by_port_id(dev_id);

	} else if (!strcmp(p_type, "ring")) {
		dev_id = find_ethdev_id(p_id, RING);
		if (dev_id == PORT_RESET)
			return -1;
		rte_eth_dev_stop(dev_id);
		rte_eth_dev_close(dev_id);

	} else if (!strcmp(p_type, "pcap")) {
		dev_id = find_ethdev_id(p_id, PCAP);
		if (dev_id == PORT_RESET)
			return -1;
		dev_detach_by_port_id(dev_id);

	} else if (!strcmp(p_type, "memif")) {
		dev_id = find_ethdev_id(p_id, MEMIF);
		if (dev_id == PORT_RESET)
			return -1;
		dev_detach_by_port_id(dev_id);

	} else if (!strcmp(p_type, "nullpmd")) {
		dev_id = find_ethdev_id(p_id, NULLPMD);
		if (dev_id == PORT_RESET)
			return -1;
		dev_detach_by_port_id(dev_id);

	} else if (!strcmp(p_type, "pipe")) {
		dev_id = find_ethdev_id(p_id, PIPE);
		if (dev_id == PORT_RESET)
			return -1;
		dev_detach_by_port_id(dev_id);
	}

	port_id_list[dev_id].port_id = PORT_RESET;
	port_id_list[dev_id].type = UNDEF;

	forward_array_remove(dev_id, 0);
	port_map_init_one(dev_id);

	return 0;
}

static int
parse_command(char *str)
{
	char *token_list[MAX_PARAMETER] = {NULL};
	char sec_name[16];
	char *sec_args[NOF_TOKENS] = {NULL};
	int ret = 0;
	int max_token = 0;
	uint16_t dev_id;
	char dev_name[RTE_DEV_NAME_MAX_LEN] = { 0 };
	char result[16] = { 0 };  /* "succeeded" or "failed". */
	char port_uid[20] = { 0 };  /* "\"%s:%d\"" */
	char patch_set[64] = { 0 };  /* "{\"src\":\"%s:%d\",\"dst\":...}" */
	char *p_type;
	int p_id;
	char tmp_response[MSG_SIZE];
	uint16_t queue_id;

	memset(sec_name, '\0', 16);
	memset(tmp_response, '\0', MSG_SIZE);

	/* tokenize the user commands from controller */
	token_list[max_token] = strtok(str, " ");
	while (token_list[max_token] != NULL) {
		RTE_LOG(DEBUG, PRIMARY,
				"parse command, token[%2d] = %s\n",
				max_token, token_list[max_token]);
		if (max_token == 2)
			sprintf(sec_name, "%s", token_list[max_token]);
		else if (max_token > 2)
			sec_args[max_token-3] = token_list[max_token];
		max_token++;
		token_list[max_token] = strtok(NULL, " ");
	}

	if (!strcmp(token_list[0], "status")) {
		RTE_LOG(DEBUG, PRIMARY, "'status' command received.\n");

		/* Clear str and token_list nouse already */
		memset(str, '\0', MSG_SIZE);
		ret = get_status_json(str);

		/* Output all of ports under management for debugging. */
		RTE_ETH_FOREACH_DEV(dev_id) {
			rte_eth_dev_get_name_by_port(dev_id, dev_name);
			if (strlen(dev_name) > 0)
				RTE_LOG(DEBUG, PRIMARY, "Port list: %d\t%s\n",
						dev_id, dev_name);
		}

	} else if (!strcmp(token_list[0], "launch")) {
		RTE_LOG(DEBUG, PRIMARY, "'%s' command received.\n",
				token_list[0]);

		ret = launch_sec_proc(sec_name,
				strtod(token_list[1], NULL), sec_args);

		if (ret < 0) {
			RTE_LOG(ERR, PRIMARY, "Failed to launch secondary.\n");
			sprintf(result, "%s", "\"failed\"");
		} else
			sprintf(result, "%s", "\"succeeded\"");

		memset(str, '\0', MSG_SIZE);
		sprintf(str, "{%s:%s,%s:%s}",
				"\"result\"", result,
				"\"command\"", "\"launch\"");

	} else if (!strcmp(token_list[0], "stop")) {
		RTE_LOG(DEBUG, PRIMARY, "stop\n");
		cmd = STOP;
		sprintf(str, "{%s:%s,%s:%s}",
				"\"result\"", "\"succeeded\"",
				"\"command\"", "\"stop\"");

	} else if (!strcmp(token_list[0], "forward")) {
		RTE_LOG(DEBUG, PRIMARY, "forward\n");
		cmd = FORWARD;
		sprintf(str, "{%s:%s,%s:%s}",
				"\"result\"", "\"succeeded\"",
				"\"command\"", "\"forward\"");

	} else if (!strcmp(token_list[0], "add")) {
		RTE_LOG(DEBUG, PRIMARY, "'%s' command received.\n",
				token_list[0]);

		ret = parse_resource_uid(token_list[1], &p_type, &p_id,
			&queue_id);
		if (ret < 0) {
			RTE_LOG(ERR, PRIMARY, "Failed to parse RES UID.\n");
			return ret;
		}

		if (add_port(p_type, p_id, &token_list[2]) < 0) {
			RTE_LOG(ERR, PRIMARY, "Failed to add_port()\n");
			sprintf(result, "%s", "\"failed\"");
		} else
			sprintf(result, "%s", "\"succeeded\"");

		sprintf(port_uid, "\"%s:%d\"", p_type, p_id);
		memset(str, '\0', MSG_SIZE);
		sprintf(str, "{%s:%s,%s:%s,%s:%s}",
				"\"result\"", result,
				"\"command\"", "\"add\"",
				"\"port\"", port_uid);

	} else if (!strcmp(token_list[0], "del")) {
		RTE_LOG(DEBUG, PRIMARY, "Received del command\n");

		ret = parse_resource_uid(token_list[1], &p_type, &p_id,
			&queue_id);
		if (ret < 0) {
			RTE_LOG(ERR, PRIMARY, "Failed to parse RES UID.\n");
			return ret;
		}

		if (del_port(p_type, p_id) < 0) {
			RTE_LOG(ERR, PRIMARY, "Failed to del_port()\n");
			sprintf(result, "%s", "\"failed\"");
		} else
			sprintf(result, "%s", "\"succeeded\"");

		sprintf(port_uid, "\"%s:%d\"", p_type, p_id);
		memset(str, '\0', MSG_SIZE);
		sprintf(str, "{%s:%s,%s:%s,%s:%s}",
				"\"result\"", result,
				"\"command\"", "\"del\"",
				"\"port\"", port_uid);

	} else if (!strcmp(token_list[0], "patch")) {
		RTE_LOG(DEBUG, PRIMARY, "patch\n");

		if (max_token <= 1)
			return 0;

		if (strncmp(token_list[1], "reset", 5) == 0) {
			/* reset forward array*/
			forward_array_reset();
		} else {
			uint16_t in_port;
			uint16_t out_port;

			if (max_token <= 2)
				return 0;

			char *in_p_type;
			char *out_p_type;
			int in_p_id;
			int out_p_id;
			uint16_t in_queue_id, out_queue_id;

			parse_resource_uid(token_list[1], &in_p_type, &in_p_id,
				&in_queue_id);
			in_port = find_port_id(in_p_id,
					get_port_type(in_p_type));

			parse_resource_uid(token_list[2],
					&out_p_type, &out_p_id, &out_queue_id);
			out_port = find_port_id(out_p_id,
					get_port_type(out_p_type));

			if (in_port == PORT_RESET && out_port == PORT_RESET) {
				char err_msg[128];
				memset(err_msg, '\0', sizeof(err_msg));
				sprintf(err_msg, "%s '%s:%d' and '%s:%d'",
					"Patch not found, both of",
					in_p_type, in_p_id,
					out_p_type, out_p_id);
				RTE_LOG(ERR, PRIMARY, "%s\n", err_msg);
			} else if (in_port == PORT_RESET) {
				char err_msg[128];
				memset(err_msg, '\0', sizeof(err_msg));
				sprintf(err_msg, "%s '%s:%d'",
					"Patch not found, in_port",
					in_p_type, in_p_id);
				RTE_LOG(ERR, PRIMARY, "%s\n", err_msg);
			} else if (out_port == PORT_RESET) {
				char err_msg[128];
				memset(err_msg, '\0', sizeof(err_msg));
				sprintf(err_msg, "%s '%s:%d'",
					"Patch not found, out_port",
					out_p_type, out_p_id);
				RTE_LOG(ERR, PRIMARY, "%s\n", err_msg);
			}

			if (add_patch(in_port, in_queue_id, out_port,
				out_queue_id) == 0) {
				RTE_LOG(INFO, PRIMARY,
					"Patched '%s:%d' and '%s:%d'\n",
					in_p_type, in_p_id,
					out_p_type, out_p_id);
				sprintf(result, "%s", "\"succeeded\"");
			} else {
				RTE_LOG(ERR, PRIMARY, "Failed to patch\n");
				sprintf(result, "%s", "\"failed\"");
			}

			sprintf(patch_set,
				"{\"src\":\"%s:%d\",\"dst\":\"%s:%d\"}",
				in_p_type, in_p_id, out_p_type, out_p_id);

			memset(str, '\0', MSG_SIZE);
			sprintf(str, "{%s:%s,%s:%s,%s:%s}",
					"\"result\"", result,
					"\"command\"", "\"patch\"",
					"\"ports\"", patch_set);

			ret = 0;
		}

	} else if (!strcmp(token_list[0], "exit")) {
		RTE_LOG(DEBUG, PRIMARY, "'exit' command received.\n");
		cmd = STOP;
		ret = -1;
		memset(str, '\0', MSG_SIZE);
		sprintf(str, "{%s:%s,%s:%s}",
				"\"result\"", "\"succeeded\"",
				"\"command\"", "\"exit\"");

	} else if (!strcmp(token_list[0], "clear")) {
		clear_stats();
		memset(str, '\0', MSG_SIZE);
		sprintf(str, "{%s:%s,%s:%s}",
				"\"result\"", "\"succeeded\"",
				"\"command\"", "\"clear\"");

	} else if (!strcmp(token_list[0], "flow")) {
		RTE_LOG(DEBUG, PRIMARY, "'%s' command received.\n",
				token_list[0]);
		ret = parse_flow(token_list, tmp_response);
		memset(str, '\0', MSG_SIZE);
		strncpy(str, tmp_response, MSG_SIZE-1);
	}

	return ret;
}

static int
do_receive(int *connected, int *sock, char *str)
{
	int ret;

	memset(str, '\0', MSG_SIZE);

	ret = poll(&pfd, 1, POLL_TIMEOUT_MS);
	if (ret <= 0) {
		if (ret < 0) {
			close(*sock);
			*sock = SOCK_RESET;
			*connected = 0;
		}
		return -1;
	}

	ret = recv(*sock, str, MSG_SIZE, 0);
	if (ret <= 0) {
		RTE_LOG(DEBUG, PRIMARY, "Receive count: %d\n", ret);

		if (ret < 0)
			RTE_LOG(ERR, PRIMARY, "Receive Fail");
		else
			RTE_LOG(INFO, PRIMARY, "Receive 0\n");

		RTE_LOG(INFO, PRIMARY, "Assume Server closed connection\n");
		close(*sock);
		*sock = SOCK_RESET;
		*connected = 0;
		return -1;
	}

	return 0;
}

static int
do_connection(int *connected, int *sock)
{
	static struct sockaddr_in servaddr;
	int ret = 0;

	if (*connected == 0) {
		if (*sock < 0) {
			RTE_LOG(INFO, PRIMARY, "Creating socket...\n");
			*sock = socket(AF_INET, SOCK_STREAM, 0);
			if (*sock < 0)
				rte_exit(EXIT_FAILURE, "socket error\n");

			/* Creation of the socket */
			memset(&servaddr, 0, sizeof(servaddr));
			servaddr.sin_family = AF_INET;
			servaddr.sin_addr.s_addr = inet_addr(server_ip);
			servaddr.sin_port = htons(server_port);

			pfd.fd = *sock;
			pfd.events = POLLIN;
		}

		RTE_LOG(INFO,
			PRIMARY, "Trying to connect ... socket %d\n", *sock);
		ret = connect(*sock, (struct sockaddr *) &servaddr,
			sizeof(servaddr));
		if (ret < 0) {
			RTE_LOG(ERR, PRIMARY, "Connection Error");
			return ret;
		}

		RTE_LOG(INFO, PRIMARY, "Connected\n");
		*connected = 1;
	}

	return ret;
}

int
main(int argc, char *argv[])
{
	int sock = SOCK_RESET;
	uint16_t dev_id;
	char dev_name[RTE_DEV_NAME_MAX_LEN] = { 0 };
	unsigned int nb_ports;
	int connected = 0;
	char str[MSG_SIZE];
	int flg_exit;  // used as res of parse_command() to exit if -1
	int ret;
	int port_type;
	unsigned int i;
	int nof_phy_port = 0;

	set_user_log_debug(1);

	/* Register signals */
	signal(SIGINT, turn_off);

	/* initialise the system */
	if (init(argc, argv) < 0)
		return -1;

	RTE_LOG(INFO, PRIMARY, "Finished Process Init.\n");

	/* clear statistics */
	clear_stats();

	memset(port_id_list, 0x00,
			sizeof(struct port_id_map) * RTE_MAX_ETHPORTS);
	for (dev_id = 0; dev_id < RTE_MAX_ETHPORTS; dev_id++) {
		ret = rte_eth_dev_get_name_by_port(dev_id, dev_name);
		if (ret > -1) {
			port_id_list[dev_id].port_id = dev_id;
			port_id_list[dev_id].type = PHY;
		} else {
			port_id_list[dev_id].port_id = PORT_RESET;
			port_id_list[dev_id].type = UNDEF;
		}
	}

	if (get_forwarding_flg() == 1) {
		/* initialize port forward array*/
		forward_array_init();
		port_map_init();

		/* Check an even number of ports to send/receive on. */
		nb_ports = rte_eth_dev_count_avail();
		if (nb_ports > RTE_MAX_ETHPORTS)
			nb_ports = RTE_MAX_ETHPORTS;

		cmd = STOP;
		/* update port_forward_array with active port. */
		for (i = 0; i < nb_ports; i++) {
			if (!rte_eth_dev_is_valid_port(i))
				continue;

			int port_id;
			rte_eth_dev_get_name_by_port(i, dev_name);
			ret = parse_dev_name(dev_name, &port_type, &port_id);
			if (ret < 0)
				RTE_LOG(ERR, PRIMARY, "Failed to parse dev_name.\n");
			if (port_type == PHY) {
				port_id = nof_phy_port;
				nof_phy_port++;
			}

			/* Update ports_fwd_array with phy port. */
			ports_fwd_array[i][0].in_port_id = i;
			ports_fwd_array[i][0].in_queue_id = 0;
			port_map[i].port_type = port_type;
			port_map[i].id = port_id;
			port_map[i].stats = &ports->port_stats[i];
			port_map[i].queue_info = NULL;

			/* TODO(yasufum) convert type of port_type to char */
			RTE_LOG(DEBUG, PRIMARY, "Add port, type: %d, id: %d\n",
					port_type, port_id);

		}

		/* do forwarding */
		rte_eal_mp_remote_launch(main_loop, NULL, SKIP_MASTER);
	} else
		/* put all other cores to sleep bar master */
		rte_eal_mp_remote_launch(sleep_lcore, NULL, SKIP_MASTER);

	while (on) {
		ret = do_connection(&connected, &sock);
		if (ret < 0) {
			usleep(CONN_RETRY_USEC);
			continue;
		}

		ret = do_receive(&connected, &sock, str);
		if (ret < 0)
			continue;

		RTE_LOG(DEBUG, PRIMARY, "Received string: %s\n", str);

		flg_exit = parse_command(str);

		/* Send the message back to client */
		ret = do_send(&connected, &sock, str);

		if (flg_exit < 0)  /* terminate process if exit is called */
			break;
		else if (ret < 0)
			continue;
	}

	/* exit */
	close(sock);
	sock = SOCK_RESET;
	RTE_LOG(INFO, PRIMARY, "spp_primary exit.\n");
	return 0;
}