summaryrefslogtreecommitdiff
path: root/src/shared/secondary/spp_worker_th/cmd_utils.c
blob: 58db631a8f7b9df27eda3493f034711b74d3871b (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
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2018-2019 Nippon Telegraph and Telephone Corporation
 */

#include <string.h>
#include <unistd.h>

#include <rte_eth_ring.h>
#include <rte_eth_vhost.h>
#include <rte_cycles.h>
#include <rte_log.h>
#include <rte_branch_prediction.h>

#include "cmd_utils.h"
#include "shared/secondary/add_port.h"
#include "shared/secondary/return_codes.h"

#ifdef SPP_VF_MODULE
#include "vf_deps.h"
#endif

#ifdef SPP_MIRROR_MODULE
#include "mirror_deps.h"
#endif

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


/* TODO(yasufum) change log label after filename is revised. */
#define RTE_LOGTYPE_WK_CMD_UTILS RTE_LOGTYPE_USER1

/* A set of pointers of management data */
/* TODO(yasufum) change names start with `p_change` because it wrong meanig. */
struct mng_data_info {
	struct iface_info *p_iface_info;
	struct sppwk_comp_info *p_component_info;
	struct core_mng_info *p_core_info;
	int *p_change_core;
	int *p_change_component;  /* Set of flags for udpated components */
	struct cancel_backup_info *p_backup_info;
};

/* Logical core ID for main process */
static struct mng_data_info g_mng_data;

/* Hexdump `addr` for logging, used for core_info or component info. */
void
log_hexdumped(const char *obj_name, const void *obj_addr, const size_t size)
{
	size_t cnt;
	size_t max_cnt = (size / sizeof(unsigned int)) +
			((size % sizeof(unsigned int)) != 0);
	const uint32_t *buf = obj_addr;

	if ((obj_name != NULL) && (obj_name[0] != '\0'))
		RTE_LOG(DEBUG, WK_CMD_UTILS, "Name of dumped buf: %s.\n",
				obj_name);

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

/* Get status of lcore of given ID. */
enum sppwk_lcore_status
sppwk_get_lcore_status(unsigned int lcore_id)
{
	return (g_mng_data.p_core_info + lcore_id)->status;
}

/**
 * Check status of all of cores is same as given
 *
 * It returns SPPWK_RET_NG as status mismatch if status is not same.
 * If core is in use, status will be checked.
 */
static int
check_core_status(enum sppwk_lcore_status status)
{
	unsigned int lcore_id = 0;
	RTE_LCORE_FOREACH_SLAVE(lcore_id) {
		if ((g_mng_data.p_core_info + lcore_id)->status !=
								status) {
			/* Status is mismatched */
			return SPPWK_RET_NG;
		}
	}
	return SPPWK_RET_OK;
}

int
check_core_status_wait(enum sppwk_lcore_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 SPPWK_RET_OK;
	}

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

/* Set core status */
void
set_core_status(unsigned int lcore_id,
		enum sppwk_lcore_status status)
{
	(g_mng_data.p_core_info + lcore_id)->status = status;
}

/* Set all core to given status */
void
set_all_core_status(enum sppwk_lcore_status status)
{
	unsigned int lcore_id = 0;
	RTE_LCORE_FOREACH_SLAVE(lcore_id) {
		(g_mng_data.p_core_info + lcore_id)->status = status;
	}
}

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

	master_lcore = rte_get_master_lcore();
	(g_mng_data.p_core_info + master_lcore)->status =
		SPPWK_LCORE_REQ_STOP;
	set_all_core_status(SPPWK_LCORE_REQ_STOP);
}

/**
 * Return sppwk_port_info of given type and num of interface. It returns NULL
 * if given type is invalid.
 */
struct sppwk_port_info *
get_sppwk_port(enum port_type iface_type, int iface_no)
{
	struct iface_info *iface_info = g_mng_data.p_iface_info;

	switch (iface_type) {
	case PHY:
		return &iface_info->phy[iface_no];
	case VHOST:
		return &iface_info->vhost[iface_no];
	case RING:
		return &iface_info->ring[iface_no];
	default:
		return NULL;
	}
}

/* Dump of core information */
void
log_core_info(const struct core_mng_info *core_info)
{
	char str[STR_LEN_NAME];
	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, WK_CMD_UTILS,
				"core[%d] status=%d, ref=%d, upd=%d\n",
				lcore_id, info->status,
				info->ref_index, info->upd_index);

		memset(str, 0x00, STR_LEN_NAME);
		log_hexdumped(str, info->core[0].id,
				sizeof(int)*info->core[0].num);

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

/* Dump of component information */
void
log_component_info(const struct sppwk_comp_info *comp_info)
{
	char str[STR_LEN_NAME];
	const struct sppwk_comp_info *tmp_ci = NULL;
	int cnt = 0;
	for (cnt = 0; cnt < RTE_MAX_LCORE; cnt++) {
		tmp_ci = &comp_info[cnt];
		if (tmp_ci->wk_type == SPPWK_TYPE_NONE)
			continue;

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

		sprintf(str, "component[%d] rx=%d", cnt,
				tmp_ci->nof_rx);
		log_hexdumped(str, tmp_ci->rx_ports,
			sizeof(struct sppwk_port_info *)*tmp_ci->nof_rx);

		sprintf(str, "component[%d] tx=%d", cnt,
				tmp_ci->nof_tx);
		log_hexdumped(str, tmp_ci->tx_ports,
			sizeof(struct sppwk_port_info *)*tmp_ci->nof_tx);
	}
}

/* Dump of interface information */
void
log_interface_info(const struct iface_info *iface_info)
{
	const struct sppwk_port_info *port = NULL;
	int cnt = 0;
	for (cnt = 0; cnt < RTE_MAX_ETHPORTS; cnt++) {
		port = &iface_info->phy[cnt];
		if (port->iface_type == UNDEF)
			continue;

		RTE_LOG(DEBUG, WK_CMD_UTILS,
				"phy  [%d] type=%d, no=%d, port=%d, "
				"vid = %u, mac=%08lx(%s)\n",
				cnt, port->iface_type, port->iface_no,
				port->ethdev_port_id,
				port->cls_attrs.vlantag.vid,
				port->cls_attrs.mac_addr,
				port->cls_attrs.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, WK_CMD_UTILS,
				"vhost[%d] type=%d, no=%d, port=%d, "
				"vid = %u, mac=%08lx(%s)\n",
				cnt, port->iface_type, port->iface_no,
				port->ethdev_port_id,
				port->cls_attrs.vlantag.vid,
				port->cls_attrs.mac_addr,
				port->cls_attrs.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, WK_CMD_UTILS,
				"ring [%d] type=%d, no=%d, port=%d, "
				"vid = %u, mac=%08lx(%s)\n",
				cnt, port->iface_type, port->iface_no,
				port->ethdev_port_id,
				port->cls_attrs.vlantag.vid,
				port->cls_attrs.mac_addr,
				port->cls_attrs.mac_addr_str);
	}
}

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

	log_core_info(core);
	log_component_info(component);
	log_interface_info(interface);
}

/* Copy management information */
void
copy_mng_info(
		struct core_mng_info *dst_core,
		struct sppwk_comp_info *dst_component,
		struct iface_info *dst_interface,
		const struct core_mng_info *src_core,
		const struct sppwk_comp_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 sppwk_comp_info)*RTE_MAX_LCORE);
	memcpy(dst_interface, src_interface,
			sizeof(struct iface_info));
}

/* Backup the management information */
void
backup_mng_info(struct cancel_backup_info *backup)
{
	log_all_mng_info(g_mng_data.p_core_info,
			g_mng_data.p_component_info,
			g_mng_data.p_iface_info);
	copy_mng_info(backup->core, backup->component, &backup->interface,
			g_mng_data.p_core_info,
			g_mng_data.p_component_info,
			g_mng_data.p_iface_info,
			COPY_MNG_FLG_ALLCOPY);
	log_all_mng_info(backup->core, backup->component, &backup->interface);
	memset(g_mng_data.p_change_core, 0x00,
				sizeof(int)*RTE_MAX_LCORE);
	memset(g_mng_data.p_change_component, 0x00,
				sizeof(int)*RTE_MAX_LCORE);
}

/**
 * Initialize g_iface_info
 *
 * Clear g_iface_info and set initial value.
 */
static void
init_iface_info(void)
{
	int port_cnt;  /* increment ether ports */
	struct iface_info *p_iface_info = g_mng_data.p_iface_info;
	memset(p_iface_info, 0x00, sizeof(struct iface_info));
	for (port_cnt = 0; port_cnt < RTE_MAX_ETHPORTS; port_cnt++) {
		p_iface_info->phy[port_cnt].iface_type = UNDEF;
		p_iface_info->phy[port_cnt].iface_no = port_cnt;
		p_iface_info->phy[port_cnt].ethdev_port_id = -1;
		p_iface_info->phy[port_cnt].cls_attrs.vlantag.vid =
			ETH_VLAN_ID_MAX;
		p_iface_info->vhost[port_cnt].iface_type = UNDEF;
		p_iface_info->vhost[port_cnt].iface_no = port_cnt;
		p_iface_info->vhost[port_cnt].ethdev_port_id = -1;
		p_iface_info->vhost[port_cnt].cls_attrs.vlantag.vid =
			ETH_VLAN_ID_MAX;
		p_iface_info->ring[port_cnt].iface_type = UNDEF;
		p_iface_info->ring[port_cnt].iface_no = port_cnt;
		p_iface_info->ring[port_cnt].ethdev_port_id = -1;
		p_iface_info->ring[port_cnt].cls_attrs.vlantag.vid =
			ETH_VLAN_ID_MAX;
	}
}

/* Initialize g_component_info */
static void
init_component_info(void)
{
	int cnt;
	memset(g_mng_data.p_component_info, 0x00,
			sizeof(struct sppwk_comp_info)*RTE_MAX_LCORE);
	for (cnt = 0; cnt < RTE_MAX_LCORE; cnt++)
		(g_mng_data.p_component_info + cnt)->comp_id = cnt;
	memset(g_mng_data.p_change_component, 0x00,
			sizeof(int)*RTE_MAX_LCORE);
}

/* Initialize g_core_info */
static void
init_core_info(void)
{
	int cnt = 0;
	struct core_mng_info *p_core_info = g_mng_data.p_core_info;
	memset(p_core_info, 0x00,
			sizeof(struct core_mng_info)*RTE_MAX_LCORE);
	set_all_core_status(SPPWK_LCORE_STOPPED);
	for (cnt = 0; cnt < RTE_MAX_LCORE; cnt++) {
		(p_core_info + cnt)->ref_index = 0;
		(p_core_info + cnt)->upd_index = 1;
	}
	memset(g_mng_data.p_change_core, 0x00, sizeof(int)*RTE_MAX_LCORE);
}

/* Initialize mng data of ports on host */
static int
init_host_port_info(void)
{
	int port_type, port_id;
	int i, ret;
	int nof_phys = 0;
	char dev_name[RTE_DEV_NAME_MAX_LEN] = { 0 };
	struct iface_info *p_iface_info = g_mng_data.p_iface_info;

	for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
		if (!rte_eth_dev_is_valid_port(i))
			continue;

		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, WK_CMD_UTILS,
					"Failed to parse dev_name.\n");

		if (port_type == PHY) {
			port_id = nof_phys;
			nof_phys++;
		}

		switch (port_type) {
		case PHY:
			p_iface_info->phy[port_id].iface_type = port_type;
			p_iface_info->phy[port_id].ethdev_port_id = port_id;
			break;
		case VHOST:
			p_iface_info->vhost[port_id].iface_type = port_type;
			p_iface_info->vhost[port_id].ethdev_port_id = port_id;
			break;
		case RING:
			p_iface_info->ring[port_id].iface_type = port_type;
			p_iface_info->ring[port_id].ethdev_port_id = port_id;
			break;
		default:
			RTE_LOG(ERR, WK_CMD_UTILS,
					"Unsupported port on host, "
					"type:%d, id:%d.\n",
					port_type, port_id);
		}
	}

	return SPPWK_RET_OK;
}

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

	int ret = init_host_port_info();
	if (unlikely(ret != SPPWK_RET_OK))
		return SPPWK_RET_NG;
	return SPPWK_RET_OK;
}

/* Remove sock file if spp is not running */
void
del_vhost_sockfile(struct sppwk_port_info *vhost)
{
	int cnt;

	/* Do not remove for if it is running in vhost-client mode. */
	if (get_vhost_cli_mode() != 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 component_info */
enum sppwk_worker_type
sppwk_get_comp_type(int id)
{
	struct sppwk_comp_info *ci = (g_mng_data.p_component_info + id);
	return ci->wk_type;
}

/* Get core information which is in use */
struct core_info *
get_core_info(unsigned int lcore_id)
{
	struct core_mng_info *info = (g_mng_data.p_core_info + lcore_id);
	return &(info->core[info->ref_index]);
}

/* Check core index change */
int
spp_check_core_update(unsigned int lcore_id)
{
	struct core_mng_info *info = (g_mng_data.p_core_info + lcore_id);
	if (info->ref_index == info->upd_index)
		return SPPWK_RET_OK;
	else
		return SPPWK_RET_NG;
}

/* Check if component is using port. */
int
spp_check_used_port(
		enum port_type iface_type,
		int iface_no,
		enum sppwk_port_dir dir)
{
	int cnt, port_cnt, max = 0;
	struct sppwk_comp_info *component = NULL;
	struct sppwk_port_info **port_array = NULL;
	struct sppwk_port_info *port = get_sppwk_port(iface_type, iface_no);
	struct sppwk_comp_info *component_info =
					g_mng_data.p_component_info;

	if (port == NULL)
		return SPPWK_RET_NG;

	for (cnt = 0; cnt < RTE_MAX_LCORE; cnt++) {
		component = (component_info + cnt);
		if (component->wk_type == SPPWK_TYPE_NONE)
			continue;

		if (dir == SPPWK_PORT_DIR_RX) {
			max = component->nof_rx;
			port_array = component->rx_ports;
		} else if (dir == SPPWK_PORT_DIR_TX) {
			max = component->nof_tx;
			port_array = component->tx_ports;
		}
		for (port_cnt = 0; port_cnt < max; port_cnt++) {
			if (unlikely(port_array[port_cnt] == port))
				return cnt;
		}
	}

	return SPPWK_RET_NG;
}

/* Set component update flag for given port */
void
set_component_change_port(struct sppwk_port_info *port,
		enum sppwk_port_dir dir)
{
	int ret = 0;
	if ((dir == SPPWK_PORT_DIR_RX) || (dir == SPPWK_PORT_DIR_BOTH)) {
		ret = spp_check_used_port(port->iface_type, port->iface_no,
				SPPWK_PORT_DIR_RX);
		if (ret >= 0)
			*(g_mng_data.p_change_component + ret) = 1;
	}

	if ((dir == SPPWK_PORT_DIR_TX) || (dir == SPPWK_PORT_DIR_BOTH)) {
		ret = spp_check_used_port(port->iface_type, port->iface_no,
				SPPWK_PORT_DIR_TX);
		if (ret >= 0)
			*(g_mng_data.p_change_component + ret) = 1;
	}
}

/* Get ID of unused lcore. */
int
get_free_lcore_id(void)
{
	struct sppwk_comp_info *comp_info = g_mng_data.p_component_info;

	int cnt = 0;
	for (cnt = 0; cnt < RTE_MAX_LCORE; cnt++) {
		if ((comp_info + cnt)->wk_type == SPPWK_TYPE_NONE)
			return cnt;
	}
	return SPPWK_RET_NG;
}

/* Get lcore ID as user-defined component name. */
int
sppwk_get_lcore_id(const char *comp_name)
{
	struct sppwk_comp_info *comp_info = g_mng_data.p_component_info;

	int cnt = 0;
	if (comp_name[0] == '\0')
		return SPPWK_RET_NG;

	for (cnt = 0; cnt < RTE_MAX_LCORE; cnt++) {
		if (strcmp(comp_name, (comp_info + cnt)->name) == 0)
			return cnt;
	}
	return SPPWK_RET_NG;
}

/**
 * Get index of given entry in given port info array. It returns the index,
 * or NG code if the entry is not found.
 */
int
get_idx_port_info(struct sppwk_port_info *p_info, int nof_ports,
		struct sppwk_port_info *p_info_ary[])
{
	int cnt = 0;
	int ret = SPPWK_RET_NG;
	for (cnt = 0; cnt < nof_ports; cnt++) {
		if (p_info == p_info_ary[cnt])
			ret = cnt;
	}
	return ret;
}

/* Delete given port info from the port info array. */
int
delete_port_info(struct sppwk_port_info *p_info, int nof_ports,
		struct sppwk_port_info *p_info_ary[])
{
	int target_idx;  /* The index of deleted port */
	int cnt;

	/* Find index of target port to be deleted. */
	target_idx = get_idx_port_info(p_info, nof_ports, p_info_ary);
	if (target_idx < 0)
		return SPPWK_RET_NG;

	/**
	 * Overwrite the deleted port by the next one, and shift all of
	 * remained ports.
	 */
	nof_ports--;
	for (cnt = target_idx; cnt < nof_ports; cnt++)
		p_info_ary[cnt] = p_info_ary[cnt+1];
	p_info_ary[cnt] = NULL;  /* Remove old last port. */
	return SPPWK_RET_OK;
}

/* Activate temporarily stored port info while flushing. */
int
update_port_info(void)
{
	int ret = 0;
	int cnt = 0;
	struct sppwk_port_info *port = NULL;
	struct iface_info *p_iface_info = g_mng_data.p_iface_info;

	/* Initialize added vhost. */
	for (cnt = 0; cnt < RTE_MAX_ETHPORTS; cnt++) {
		port = &p_iface_info->vhost[cnt];
		if ((port->iface_type != UNDEF) && (port->ethdev_port_id < 0)) {
			ret = add_vhost_pmd(port->iface_no);
			if (ret < 0)
				return SPPWK_RET_NG;
			port->ethdev_port_id = ret;
		}
	}

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

/* Activate temporarily stored lcore info while flushing. */
void
update_lcore_info(void)
{
	int cnt = 0;
	struct core_mng_info *info = NULL;
	struct core_mng_info *p_core_info = g_mng_data.p_core_info;
	int *p_change_core = g_mng_data.p_change_core;

	/* Changed core has changed index. */
	for (cnt = 0; cnt < RTE_MAX_LCORE; cnt++) {
		if (*(p_change_core + cnt) != 0) {
			info = (p_core_info + cnt);
			info->upd_index = info->ref_index;
		}
	}

	/* Waiting for changed core change. */
	for (cnt = 0; cnt < RTE_MAX_LCORE; cnt++) {
		if (*(p_change_core + cnt) != 0) {
			info = (p_core_info + cnt);
			while (likely(info->ref_index == info->upd_index))
				rte_delay_us_block(SPPWK_UPDATE_INTERVAL);

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

/* Return port uid such as `phy:0`, `ring:1` or so. */
int sppwk_port_uid(char *port_uid, enum port_type p_type, int iface_no)
{
	const char *p_type_str;

	switch (p_type) {
	case PHY:
		p_type_str = SPPWK_PHY_STR;
		break;
	case RING:
		p_type_str = SPPWK_RING_STR;
		break;
	case VHOST:
		p_type_str = SPPWK_VHOST_STR;
		break;
	default:
		return SPPWK_RET_NG;
	}

	sprintf(port_uid, "%s:%d", p_type_str, iface_no);

	return SPPWK_RET_OK;
}

/* Convert MAC address of 'aa:bb:cc:dd:ee:ff' to value of int64_t. */
int64_t
sppwk_convert_mac_str_to_int64(const char *macaddr)
{
	int64_t ret_mac = 0;
	int64_t token_val = 0;
	int token_cnt = 0;
	char tmp_mac[STR_LEN_SHORT];
	char *str = tmp_mac;
	char *saveptr = NULL;
	char *endptr = NULL;

	RTE_LOG(DEBUG, WK_CMD_UTILS, "Try to convert MAC addr `%s`.\n",
			macaddr);

	strcpy(tmp_mac, macaddr);
	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, WK_CMD_UTILS,
					"Invalid MAC address `%s`.\n",
					macaddr);
			return SPPWK_RET_NG;
		}

		/* 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, WK_CMD_UTILS,
			"Succeeded to convert MAC addr `%s` to `0x%08lx`.\n",
			macaddr, ret_mac);
	return ret_mac;
}

/* Set management data of global var for given non-NULL args. */
int sppwk_set_mng_data(
		struct iface_info *iface_p,
		struct sppwk_comp_info *component_p,
		struct core_mng_info *core_mng_p,
		int *change_core_p,
		int *change_component_p,
		struct cancel_backup_info *backup_info_p)
{
	/**
	 * TODO(yasufum) confirm why the last `0xffffffff` is same as NULL,
	 * although it is reserved for meaning as invalid.
	 */
	if (iface_p == NULL || component_p == NULL || core_mng_p == NULL ||
			change_core_p == NULL || change_component_p == NULL ||
			backup_info_p == NULL)
		return SPPWK_RET_NG;

	g_mng_data.p_iface_info = iface_p;
	g_mng_data.p_component_info = component_p;
	g_mng_data.p_core_info = core_mng_p;
	g_mng_data.p_change_core = change_core_p;
	g_mng_data.p_change_component = change_component_p;
	g_mng_data.p_backup_info = backup_info_p;

	return SPPWK_RET_OK;
}

/* Get management data from global var for given non-NULL args. */
void sppwk_get_mng_data(
		struct iface_info **iface_p,
		struct sppwk_comp_info **component_p,
		struct core_mng_info **core_mng_p,
		int **change_core_p,
		int **change_component_p,
		struct cancel_backup_info **backup_info_p)
{

	if (iface_p != NULL)
		*iface_p = g_mng_data.p_iface_info;
	if (component_p != NULL)
		*component_p = g_mng_data.p_component_info;
	if (core_mng_p != NULL)
		*core_mng_p = g_mng_data.p_core_info;
	if (change_core_p != NULL)
		*change_core_p = g_mng_data.p_change_core;
	if (change_component_p != NULL)
		*change_component_p = g_mng_data.p_change_component;
	if (backup_info_p != NULL)
		*backup_info_p = g_mng_data.p_backup_info;

}