summaryrefslogtreecommitdiff
path: root/src/vf/classifier_mac.c
blob: 7bb7c7019ef13a2606214d874b34faee3761552a (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
/*
 *   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 <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <math.h>

#include <rte_common.h>
#include <rte_mbuf.h>
#include <rte_log.h>
#include <rte_cycles.h>
#include <rte_malloc.h>
#include <rte_memcpy.h>
#include <rte_random.h>
#include <rte_byteorder.h>
#include <rte_per_lcore.h>
#include <rte_eal.h>
#include <rte_launch.h>
#include <rte_hash.h>

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

#define RTE_LOGTYPE_SPP_CLASSIFIER_MAC RTE_LOGTYPE_USER1

#ifdef RTE_MACHINE_CPUFLAG_SSE4_2
#include <rte_hash_crc.h>
#define DEFAULT_HASH_FUNC rte_hash_crc
#else
#include <rte_jhash.h>
#define DEFAULT_HASH_FUNC rte_jhash
#endif

/* number of classifier mac table entry */
#define NUM_CLASSIFIER_MAC_TABLE_ENTRY 128

/* number of classifier information (reference/update) */
#define NUM_CLASSIFIER_MAC_INFO 2

/* interval that wait until change update index (micro second) */
#define CHANGE_UPDATE_INDEX_WAIT_INTERVAL SPP_CHANGE_UPDATE_INTERVAL

/* interval that transmit burst packet, if buffer is not filled (nano second) */
#define DRAIN_TX_PACKET_INTERVAL 100

/*
 * hash table name buffer size
 *[reson for value]
 *	in dpdk's lib/librte_hash/rte_cuckoo_hash.c
 *		snprintf(ring_name, sizeof(ring_name), "HT_%s", params->name);
 *		snprintf(hash_name, sizeof(hash_name), "HT_%s", params->name);
 *	ring_name buffer size is RTE_RING_NAMESIZE
 *	hash_name buffer size is RTE_HASH_NAMESIZE
 */
static const size_t HASH_TABLE_NAME_BUF_SZ =
		((RTE_HASH_NAMESIZE < RTE_RING_NAMESIZE) ?  RTE_HASH_NAMESIZE :
		RTE_RING_NAMESIZE) - 3;

/* mac address string(xx:xx:xx:xx:xx:xx) buffer size */
static const size_t ETHER_ADDR_STR_BUF_SZ =
		ETHER_ADDR_LEN * 2 + (ETHER_ADDR_LEN - 1) + 1;

/* classified data (destination port, target packets, etc) */
struct classified_data {
	/* interface type (see "enum port_type") */
	enum port_type  iface_type;

	/* index of ports handled by classifier */
	int             iface_no;

	/* id for interface generated by spp_vf */
	int             iface_no_global;

	/* port id generated by DPDK */
	uint16_t        port;

	/* the number of packets in pkts[] */
	uint16_t        num_pkt;

	/* packet array to be classified */
	struct rte_mbuf *pkts[MAX_PKT_BURST];
};

/* classifier information */
struct classifier_mac_info {
	/* component name */
	char name[SPP_NAME_STR_LEN];

	/* hash table keeps classifier_table */
	struct rte_hash *classifier_table;

	/* number of valid classification */
	int num_active_classified;

	/* index of valid classification */
	int active_classifieds[RTE_MAX_ETHPORTS];

	/* index of default classification */
	int default_classified;

	/* number of transmission ports */
	int n_classified_data_tx;

	/* receive port handled by classifier */
	struct classified_data classified_data_rx;

	/* transmission ports handled by classifier */
	struct classified_data classified_data_tx[RTE_MAX_ETHPORTS];
};

/* classifier management information */
struct classifier_mac_mng_info {
	/* classifier information */
	struct classifier_mac_info info[NUM_CLASSIFIER_MAC_INFO];

	/* Reference index number for classifier information */
	volatile int ref_index;

	/* Update index number for classifier information */
	volatile int upd_index;
};

/* classifier information per lcore */
static struct classifier_mac_mng_info g_classifier_mng_info[RTE_MAX_LCORE];

/**
 * Hash table count used for making a name of hash table
 *
 * This function is required because it is incremented at the time of use,
 * but since we want to start at 0.
 */
static rte_atomic16_t g_hash_table_count = RTE_ATOMIC16_INIT(0xff);

static inline int
is_used_mng_info(const struct classifier_mac_mng_info *mng_info)
{
	return (mng_info != NULL && mng_info->info[0].classifier_table != NULL);
}

/* initialize classifier information. */
static int
init_classifier_info(struct classifier_mac_info *classifier_info,
		const struct spp_component_info *component_info)
{
	int ret = -1;
	int i;
	struct rte_hash **classifier_table = &classifier_info->classifier_table;
	struct ether_addr eth_addr;
	char mac_addr_str[ETHER_ADDR_STR_BUF_SZ];
	struct classified_data *classified_data_rx =
			&classifier_info->classified_data_rx;
	struct classified_data *classified_data_tx =
			classifier_info->classified_data_tx;
	struct spp_port_info *tx_port = NULL;

	rte_hash_reset(*classifier_table);
	classifier_info->num_active_classified = 0;
	classifier_info->default_classified = -1;
	classifier_info->n_classified_data_tx = component_info->num_tx_port;
	if (component_info->num_rx_port == 0) {
		classified_data_rx->iface_type      = UNDEF;
		classified_data_rx->iface_no        = 0;
		classified_data_rx->iface_no_global = 0;
		classified_data_rx->port         = 0;
		classified_data_rx->num_pkt      = 0;
	} else {
		classified_data_rx->iface_type      =
				component_info->rx_ports[0]->iface_type;
		classified_data_rx->iface_no        = 0;
		classified_data_rx->iface_no_global =
				component_info->rx_ports[0]->iface_no;
		classified_data_rx->port         =
				component_info->rx_ports[0]->dpdk_port;
		classified_data_rx->num_pkt      = 0;
	}

	for (i = 0; i < component_info->num_tx_port; i++) {
		tx_port = component_info->tx_ports[i];

		/* store ports information */
		classified_data_tx[i].iface_type      = tx_port->iface_type;
		classified_data_tx[i].iface_no        = i;
		classified_data_tx[i].iface_no_global = tx_port->iface_no;
		classified_data_tx[i].port         = tx_port->dpdk_port;
		classified_data_tx[i].num_pkt      = 0;

		if (component_info->tx_ports[i]->mac_addr == 0)
			continue;

		/* store active tx_port that associate with mac address */
		classifier_info->active_classifieds[classifier_info->
				num_active_classified++] = i;

		/* store default classified */
		if (unlikely(tx_port->mac_addr ==
				SPP_DEFAULT_CLASSIFIED_DMY_ADDR)) {
			classifier_info->default_classified = i;
			RTE_LOG(INFO, SPP_CLASSIFIER_MAC, "default classified. "
					"iface_type=%d, iface_no=%d, dpdk_port=%d\n",
					tx_port->iface_type,
					tx_port->iface_no,
					tx_port->dpdk_port);
			continue;
		}

		/* add entry to classifier mac table */
		rte_memcpy(&eth_addr, &tx_port->mac_addr, ETHER_ADDR_LEN);
		ether_format_addr(mac_addr_str, sizeof(mac_addr_str),
				&eth_addr);

		ret = rte_hash_add_key_data(*classifier_table,
				(void *)&eth_addr, (void *)(long)i);
		if (unlikely(ret < 0)) {
			RTE_LOG(ERR, SPP_CLASSIFIER_MAC,
					"Cannot add entry to classifier mac table. "
					"ret=%d, mac_addr=%s\n",
					ret, mac_addr_str);
			rte_hash_free(*classifier_table);
			*classifier_table = NULL;
			return -1;
		}

		RTE_LOG(INFO, SPP_CLASSIFIER_MAC, "Add entry to classifier mac table. "
				"mac_addr=%s, iface_type=%d, iface_no=%d, dpdk_port=%d\n",
				mac_addr_str,
				tx_port->iface_type,
				tx_port->iface_no,
				tx_port->dpdk_port);
	}

	return 0;
}

/* initialize classifier. */
static int
init_classifier(struct classifier_mac_mng_info *classifier_mng_info)
{
	int ret = -1;
	int i;
	char hash_table_name[HASH_TABLE_NAME_BUF_SZ];

	struct rte_hash **classifier_mac_table = NULL;
	struct spp_component_info component_info;

	memset(classifier_mng_info, 0, sizeof(struct classifier_mac_mng_info));
	/*
	 * Set the same value for "ref_index" and "upd_index"
	 * so that it will not be changed from others during initialization,
	 * and update "upd_index" after initialization is completed.
	 * Therefore, this setting is consciously described.
	 */
	classifier_mng_info->ref_index = 0;
	classifier_mng_info->upd_index = 0;
	memset(&component_info, 0x00, sizeof(component_info));

#ifdef RTE_MACHINE_CPUFLAG_SSE4_2
	RTE_LOG(DEBUG, SPP_CLASSIFIER_MAC, "Enabled SSE4.2. use CRC hash.\n");
#else
	RTE_LOG(DEBUG, SPP_CLASSIFIER_MAC, "Disabled SSE4.2. use Jenkins hash.\n");
#endif

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

		classifier_mac_table =
				&classifier_mng_info->info[i].classifier_table;

		/* make hash table name(require uniqueness between processes) */
		sprintf(hash_table_name, "cmtab_%07x%02hx%x",
				getpid(),
				rte_atomic16_add_return(&g_hash_table_count, 1),
				i);

		RTE_LOG(INFO, SPP_CLASSIFIER_MAC, "Create table. name=%s, bufsz=%lu\n",
				hash_table_name, HASH_TABLE_NAME_BUF_SZ);

		/* set hash creating parameters */
		struct rte_hash_parameters hash_params = {
				.name      = hash_table_name,
				.entries   = NUM_CLASSIFIER_MAC_TABLE_ENTRY,
				.key_len   = sizeof(struct ether_addr),
				.hash_func = DEFAULT_HASH_FUNC,
				.hash_func_init_val = 0,
				.socket_id = rte_socket_id(),
		};

		/* create classifier mac table (hash table) */
		*classifier_mac_table = rte_hash_create(&hash_params);
		if (unlikely(*classifier_mac_table == NULL)) {
			RTE_LOG(ERR, SPP_CLASSIFIER_MAC, "Cannot create classifier mac table. "
					"name=%s\n", hash_table_name);
			return -1;
		}
	}

	/* populate the classifier information at reference */
	ret = init_classifier_info(&classifier_mng_info->
			info[classifier_mng_info->ref_index], &component_info);
	if (unlikely(ret != 0)) {
		RTE_LOG(ERR, SPP_CLASSIFIER_MAC,
				"Cannot initialize classifier mac table. ret=%d\n",
				ret);
		return -1;
	}

	/* updating side can be set by completion of initialization. */
	classifier_mng_info->upd_index = classifier_mng_info->ref_index + 1;

	return 0;
}

/* uninitialize classifier. */
static void
uninit_classifier(struct classifier_mac_mng_info *classifier_mng_info)
{
	int i;

	for (i = 0; i < NUM_CLASSIFIER_MAC_INFO; ++i) {
		if (classifier_mng_info->info[i].classifier_table != NULL) {
			rte_hash_free(classifier_mng_info->info[i].
					classifier_table);
			classifier_mng_info->info[i].classifier_table = NULL;
			classifier_mng_info->ref_index = 0;
			classifier_mng_info->upd_index = 0;
		}
	}
}

/* transmit packet to one destination. */
static inline void
transmit_packet(struct classified_data *classified_data)
{
	int i;
	uint16_t n_tx;

#ifdef SPP_RINGLATENCYSTATS_ENABLE
	if (classified_data->iface_type == RING)
		/* if tx-if is ring, set ringlatencystats */
		spp_ringlatencystats_add_time_stamp(classified_data->iface_no,
				classified_data->pkts,
				classified_data->num_pkt);
#endif

	/* transmit packets */
	n_tx = rte_eth_tx_burst(classified_data->port, 0,
			classified_data->pkts, classified_data->num_pkt);

	/* free cannot transmit packets */
	if (unlikely(n_tx != classified_data->num_pkt)) {
		for (i = n_tx; i < classified_data->num_pkt; i++)
			rte_pktmbuf_free(classified_data->pkts[i]);
		RTE_LOG(DEBUG, SPP_CLASSIFIER_MAC,
				"drop packets(tx). num=%hu, dpdk_port=%hu\n",
				(uint16_t)(classified_data->num_pkt - n_tx),
				classified_data->port);
	}

	classified_data->num_pkt = 0;
}

/* transmit packet to one destination. */
static inline void
transmit_all_packet(struct classifier_mac_info *classifier_info)
{
	int i;
	struct classified_data *classified_data_tx =
				classifier_info->classified_data_tx;

	for (i = 0; i < classifier_info->n_classified_data_tx; i++) {
		if (unlikely(classified_data_tx[i].num_pkt != 0)) {
			RTE_LOG(INFO, SPP_CLASSIFIER_MAC,
					"transmit all packets (drain). "
					"index=%d, "
					"num_pkt=%hu\n",
					i,
					classified_data_tx[i].num_pkt);
			transmit_packet(&classified_data_tx[i]);
		}
	}
}

/* set mbuf pointer to tx buffer and transmit packet, if buffer is filled */
static inline void
push_packet(struct rte_mbuf *pkt, struct classified_data *classified_data)
{
	classified_data->pkts[classified_data->num_pkt++] = pkt;

	/* transmit packet, if buffer is filled */
	if (unlikely(classified_data->num_pkt == MAX_PKT_BURST)) {
		RTE_LOG(DEBUG, SPP_CLASSIFIER_MAC,
				"transmit packets (buffer is filled). "
				"iface_type=%d, iface_no={%d,%d}, tx_port=%hu, num_pkt=%hu\n",
				classified_data->iface_type,
				classified_data->iface_no_global,
				classified_data->iface_no,
				classified_data->port,
				classified_data->num_pkt);
		transmit_packet(classified_data);
	}
}

/* handle L2 multicast(include broadcast) packet */
static inline void
handle_l2multicast_packet(struct rte_mbuf *pkt,
		struct classifier_mac_info *classifier_info,
		struct classified_data *classified_data)
{
	int i;

	if (unlikely(classifier_info->num_active_classified == 0)) {
		RTE_LOG(ERR, SPP_CLASSIFIER_MAC, "No mac address.(l2 multicast packet)\n");
		rte_pktmbuf_free(pkt);
		return;
	}

	rte_mbuf_refcnt_update(pkt,
			(classifier_info->num_active_classified - 1));

	for (i = 0; i < classifier_info->num_active_classified; i++) {
		push_packet(pkt, classified_data +
				(long)classifier_info->active_classifieds[i]);
	}
}

/*
 * classify packet by destination mac address,
 * and transmit packet (conditional).
 */
static inline void
classify_packet(struct rte_mbuf **rx_pkts, uint16_t n_rx,
		struct classifier_mac_info *classifier_info,
		struct classified_data *classified_data)
{
	int ret;
	int i;
	struct ether_hdr *eth;
	void *lookup_data;
	char mac_addr_str[ETHER_ADDR_STR_BUF_SZ];

	for (i = 0; i < n_rx; i++) {
		eth = rte_pktmbuf_mtod(rx_pkts[i], struct ether_hdr *);

		/* find in table (by destination mac address)*/
		ret = rte_hash_lookup_data(classifier_info->classifier_table,
				(const void *)&eth->d_addr, &lookup_data);
		if (ret < 0) {
			/* L2 multicast(include broadcast) ? */
			if (unlikely(is_multicast_ether_addr(&eth->d_addr))) {
				RTE_LOG(DEBUG, SPP_CLASSIFIER_MAC,
						"multicast mac address.\n");
				handle_l2multicast_packet(rx_pkts[i],
						classifier_info,
						classified_data);
				continue;
			}

			/* if no default, drop packet */
			if (unlikely(classifier_info->default_classified ==
					-1)) {
				ether_format_addr(mac_addr_str,
						sizeof(mac_addr_str),
						&eth->d_addr);
				RTE_LOG(ERR, SPP_CLASSIFIER_MAC,
						"unknown mac address. "
						"ret=%d, mac_addr=%s\n",
						ret, mac_addr_str);
				rte_pktmbuf_free(rx_pkts[i]);
				continue;
			}

			/* to default classified */
			RTE_LOG(DEBUG, SPP_CLASSIFIER_MAC,
					"to default classified.\n");
			lookup_data = (void *)(long)classifier_info->
					default_classified;
		}

		/*
		 * set mbuf pointer to tx buffer
		 * and transmit packet, if buffer is filled
		 */
		push_packet(rx_pkts[i], classified_data + (long)lookup_data);
	}
}

/* change update index at classifier management information */
static inline void
change_update_index(struct classifier_mac_mng_info *classifier_mng_info, int id)
{
	if (unlikely(classifier_mng_info->ref_index ==
			classifier_mng_info->upd_index)) {

		/* Transmit all packets for switching the using data. */
		transmit_all_packet(classifier_mng_info->info +
				classifier_mng_info->ref_index);

		RTE_LOG(DEBUG, SPP_CLASSIFIER_MAC,
				"Core[%u] Change update index.\n", id);
		classifier_mng_info->ref_index =
				(classifier_mng_info->upd_index + 1) %
				NUM_CLASSIFIER_MAC_INFO;
	}
}

/* classifier(mac address) initialize globals. */
int
spp_classifier_mac_init(void)
{
	memset(g_classifier_mng_info, 0, sizeof(g_classifier_mng_info));

	return 0;
}

/* classifier(mac address) update component info. */
int
spp_classifier_mac_update(struct spp_component_info *component_info)
{
	int ret = -1;
	int id = component_info->component_id;
	struct classifier_mac_mng_info *classifier_mng_info =
			g_classifier_mng_info + id;

	struct classifier_mac_info *classifier_info = NULL;

	RTE_LOG(INFO, SPP_CLASSIFIER_MAC,
			"Component[%u] Start update component.\n", id);

	/* wait until no longer access the new update side */
	while (likely(classifier_mng_info->ref_index ==
			classifier_mng_info->upd_index))
		rte_delay_us_block(CHANGE_UPDATE_INDEX_WAIT_INTERVAL);

	classifier_info = classifier_mng_info->info +
				classifier_mng_info->upd_index;

	/* initialize update side classifier information */
	ret = init_classifier_info(classifier_info, component_info);
	if (unlikely(ret != 0)) {
		RTE_LOG(ERR, SPP_CLASSIFIER_MAC,
				"Cannot update classifier mac. ret=%d\n", ret);
		return ret;
	}
	memcpy(classifier_info->name, component_info->name, SPP_NAME_STR_LEN);

	/* change index of reference side */
	classifier_mng_info->upd_index = classifier_mng_info->ref_index;

	/* wait until no longer access the new update side */
	while (likely(classifier_mng_info->ref_index ==
			classifier_mng_info->upd_index))
		rte_delay_us_block(CHANGE_UPDATE_INDEX_WAIT_INTERVAL);

	RTE_LOG(INFO, SPP_CLASSIFIER_MAC,
			"Component[%u] Complete update component.\n", id);

	return 0;
}

/* classifier(mac address) thread function. */
int
spp_classifier_mac_do(int id)
{
	int ret = -1;
	int i;
	int n_rx;
	unsigned int lcore_id = rte_lcore_id();
	struct classifier_mac_mng_info *classifier_mng_info =
			g_classifier_mng_info + id;

	struct classifier_mac_info *classifier_info = NULL;
	struct rte_mbuf *rx_pkts[MAX_PKT_BURST];

	struct classified_data *classified_data_rx = NULL;
	struct classified_data *classified_data_tx = NULL;

	uint64_t cur_tsc, prev_tsc = 0;
	const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
			US_PER_S * DRAIN_TX_PACKET_INTERVAL;

	/* initialize */
	ret = init_classifier(classifier_mng_info);
	if (unlikely(ret != 0))
		return ret;

	while (likely(spp_get_core_status(lcore_id) == SPP_CORE_FORWARD) &&
			likely(spp_check_core_index(lcore_id) == 0)) {
		/* change index of update side */
		change_update_index(classifier_mng_info, id);

		/* decide classifier information of the current cycle */
		classifier_info = classifier_mng_info->info +
				classifier_mng_info->ref_index;
		classified_data_rx = &classifier_info->classified_data_rx;
		classified_data_tx = classifier_info->classified_data_tx;

		/* drain tx packets, if buffer is not filled for interval */
		cur_tsc = rte_rdtsc();
		if (unlikely(cur_tsc - prev_tsc > drain_tsc)) {
			for (i = 0; i < classifier_info->n_classified_data_tx;
					i++) {
				if (likely(classified_data_tx[i].num_pkt == 0))
					continue;

				RTE_LOG(DEBUG, SPP_CLASSIFIER_MAC,
						"transmit packets (drain). "
						"index=%d, "
						"num_pkt=%hu, "
						"interval=%lu\n",
						i,
						classified_data_tx[i].num_pkt,
						cur_tsc - prev_tsc);
				transmit_packet(&classified_data_tx[i]);
			}
			prev_tsc = cur_tsc;
		}

		if (classified_data_rx->iface_type == UNDEF)
			continue;

		/* retrieve packets */
		n_rx = rte_eth_rx_burst(classified_data_rx->port, 0,
				rx_pkts, MAX_PKT_BURST);
		if (unlikely(n_rx == 0))
			continue;

#ifdef SPP_RINGLATENCYSTATS_ENABLE
		if (classified_data_rx->iface_type == RING)
			spp_ringlatencystats_calculate_latency(
					classified_data_rx->iface_no,
					rx_pkts, n_rx);
#endif

		/* classify and transmit (filled) */
		classify_packet(rx_pkts, n_rx, classifier_info,
				classified_data_tx);
	}

	/* just in case */
	change_update_index(classifier_mng_info, id);

	/* uninitialize */
	uninit_classifier(classifier_mng_info);

	return 0;
}

/* classifier iterate component information */
int
spp_classifier_get_component_status(
		unsigned int lcore_id, int id,
		struct spp_iterate_core_params *params)
{
	int ret = -1;
	int i, num_tx, num_rx = 0;
	struct classifier_mac_mng_info *classifier_mng_info;
	struct classifier_mac_info *classifier_info;
	struct classified_data *classified_data;
	struct spp_port_index rx_ports[RTE_MAX_ETHPORTS];
	struct spp_port_index tx_ports[RTE_MAX_ETHPORTS];

	classifier_mng_info = g_classifier_mng_info + id;
	if (!is_used_mng_info(classifier_mng_info)) {
		RTE_LOG(ERR, SPP_CLASSIFIER_MAC,
				"Component[%d] Not used. (status)(core = %d, type = %d)\n",
				id, lcore_id, SPP_COMPONENT_CLASSIFIER_MAC);
		return -1;
	}

	classifier_info = classifier_mng_info->info +
			classifier_mng_info->ref_index;

	classified_data = classifier_info->classified_data_tx;

	memset(rx_ports, 0x00, sizeof(rx_ports));
	if (classifier_info->classified_data_rx.iface_type != UNDEF) {
		num_rx = 1;
		rx_ports[0].iface_type = classifier_info->
				classified_data_rx.iface_type;
		rx_ports[0].iface_no   = classifier_info->
				classified_data_rx.iface_no_global;
	}

	memset(tx_ports, 0x00, sizeof(tx_ports));
	num_tx = classifier_info->n_classified_data_tx;
	for (i = 0; i < num_tx; i++) {
		tx_ports[i].iface_type = classified_data[i].iface_type;
		tx_ports[i].iface_no   = classified_data[i].iface_no_global;
	}

	/* Set the information with the function specified by the command. */
	ret = (*params->element_proc)(
		params, lcore_id,
		classifier_info->name, SPP_TYPE_CLASSIFIER_MAC_STR,
		num_rx, rx_ports, num_tx, tx_ports);
	if (unlikely(ret != 0))
		return -1;

	return 0;
}

/* classifier(mac address) iterate classifier table. */
int
spp_classifier_mac_iterate_table(
		struct spp_iterate_classifier_table_params *params)
{
	int ret, i;
	const void *key;
	void *data;
	uint32_t next = 0;
	struct classifier_mac_mng_info *classifier_mng_info;
	struct classifier_mac_info *classifier_info;
	struct classified_data *classified_data;
	struct spp_port_index port;
	char mac_addr_str[ETHER_ADDR_STR_BUF_SZ];

	for (i = 0; i < RTE_MAX_LCORE; i++) {
		classifier_mng_info = g_classifier_mng_info + i;
		if (!is_used_mng_info(classifier_mng_info))
			continue;

		classifier_info = classifier_mng_info->info +
				classifier_mng_info->ref_index;

		classified_data = classifier_info->classified_data_tx;

		RTE_LOG(DEBUG, SPP_CLASSIFIER_MAC,
			"Core[%u] Start iterate classifier table.\n", i);

		if (classifier_info->default_classified >= 0) {
			port.iface_type = (classified_data +
					classifier_info->default_classified)->
					iface_type;
			port.iface_no   = (classified_data +
					classifier_info->default_classified)->
					iface_no_global;

			(*params->element_proc)(
					params,
					SPP_CLASSIFIER_TYPE_MAC,
					SPP_DEFAULT_CLASSIFIED_SPEC_STR,
					&port);
		}

		next = 0;
		while (1) {
			ret = rte_hash_iterate(
					classifier_info->classifier_table,
					&key, &data, &next);

			if (unlikely(ret < 0))
				break;

			ether_format_addr(mac_addr_str, sizeof(mac_addr_str),
					(const struct ether_addr *)key);

			port.iface_type = (classified_data + (long)data)->
					iface_type;
			port.iface_no   = (classified_data + (long)data)->
					iface_no_global;

			(*params->element_proc)(
					params,
					SPP_CLASSIFIER_TYPE_MAC,
					mac_addr_str,
					&port);
		}
	}

	return 0;
}