summaryrefslogtreecommitdiff
path: root/examples/vm_power_manager/channel_monitor.c
blob: 1194f62f8ef7b02ec28caa0f0d87bdd70ad6d62c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2010-2014 Intel Corporation
 */

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/epoll.h>
#include <sys/queue.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/select.h>
#ifdef USE_JANSSON
#include <jansson.h>
#else
#pragma message "Jansson dev libs unavailable, not including JSON parsing"
#endif
#include <rte_string_fns.h>
#include <rte_log.h>
#include <rte_memory.h>
#include <rte_malloc.h>
#include <rte_atomic.h>
#include <rte_cycles.h>
#include <rte_ethdev.h>
#include <rte_pmd_i40e.h>

#include <libvirt/libvirt.h>
#include "channel_monitor.h"
#include "channel_commands.h"
#include "channel_manager.h"
#include "power_manager.h"
#include "oob_monitor.h"

#define RTE_LOGTYPE_CHANNEL_MONITOR RTE_LOGTYPE_USER1

#define MAX_EVENTS 256

uint64_t vsi_pkt_count_prev[384];
uint64_t rdtsc_prev[384];
#define MAX_JSON_STRING_LEN 1024
char json_data[MAX_JSON_STRING_LEN];

double time_period_ms = 1;
static volatile unsigned run_loop = 1;
static int global_event_fd;
static unsigned int policy_is_set;
static struct epoll_event *global_events_list;
static struct policy policies[MAX_CLIENTS];

#ifdef USE_JANSSON

union PFID {
	struct rte_ether_addr addr;
	uint64_t pfid;
};

static int
str_to_ether_addr(const char *a, struct rte_ether_addr *ether_addr)
{
	int i;
	char *end;
	unsigned long o[ETHER_ADDR_LEN];

	i = 0;
	do {
		errno = 0;
		o[i] = strtoul(a, &end, 16);
		if (errno != 0 || end == a || (end[0] != ':' && end[0] != 0))
			return -1;
		a = end + 1;
	} while (++i != RTE_DIM(o) / sizeof(o[0]) && end[0] != 0);

	/* Junk at the end of line */
	if (end[0] != 0)
		return -1;

	/* Support the format XX:XX:XX:XX:XX:XX */
	if (i == ETHER_ADDR_LEN) {
		while (i-- != 0) {
			if (o[i] > UINT8_MAX)
				return -1;
			ether_addr->addr_bytes[i] = (uint8_t)o[i];
		}
	/* Support the format XXXX:XXXX:XXXX */
	} else if (i == ETHER_ADDR_LEN / 2) {
		while (i-- != 0) {
			if (o[i] > UINT16_MAX)
				return -1;
			ether_addr->addr_bytes[i * 2] =
					(uint8_t)(o[i] >> 8);
			ether_addr->addr_bytes[i * 2 + 1] =
					(uint8_t)(o[i] & 0xff);
		}
	/* unknown format */
	} else
		return -1;

	return 0;
}

static int
set_policy_mac(struct channel_packet *pkt, int idx, char *mac)
{
	union PFID pfid;
	int ret;

	/* Use port MAC address as the vfid */
	ret = str_to_ether_addr(mac, &pfid.addr);

	if (ret != 0) {
		RTE_LOG(ERR, CHANNEL_MONITOR,
			"Invalid mac address received in JSON\n");
		pkt->vfid[idx] = 0;
		return -1;
	}

	printf("Received MAC Address: %02" PRIx8 ":%02" PRIx8 ":%02" PRIx8 ":"
			"%02" PRIx8 ":%02" PRIx8 ":%02" PRIx8 "\n",
			pfid.addr.addr_bytes[0], pfid.addr.addr_bytes[1],
			pfid.addr.addr_bytes[2], pfid.addr.addr_bytes[3],
			pfid.addr.addr_bytes[4], pfid.addr.addr_bytes[5]);

	pkt->vfid[idx] = pfid.pfid;
	return 0;
}


static int
parse_json_to_pkt(json_t *element, struct channel_packet *pkt)
{
	const char *key;
	json_t *value;
	int ret;

	memset(pkt, 0, sizeof(struct channel_packet));

	pkt->nb_mac_to_monitor = 0;
	pkt->t_boost_status.tbEnabled = false;
	pkt->workload = LOW;
	pkt->policy_to_use = TIME;
	pkt->command = PKT_POLICY;
	pkt->core_type = CORE_TYPE_PHYSICAL;

	json_object_foreach(element, key, value) {
		if (!strcmp(key, "policy")) {
			/* Recurse in to get the contents of profile */
			ret = parse_json_to_pkt(value, pkt);
			if (ret)
				return ret;
		} else if (!strcmp(key, "instruction")) {
			/* Recurse in to get the contents of instruction */
			ret = parse_json_to_pkt(value, pkt);
			if (ret)
				return ret;
		} else if (!strcmp(key, "name")) {
			strlcpy(pkt->vm_name, json_string_value(value),
					sizeof(pkt->vm_name));
		} else if (!strcmp(key, "command")) {
			char command[32];
			strlcpy(command, json_string_value(value), 32);
			if (!strcmp(command, "power")) {
				pkt->command = CPU_POWER;
			} else if (!strcmp(command, "create")) {
				pkt->command = PKT_POLICY;
			} else if (!strcmp(command, "destroy")) {
				pkt->command = PKT_POLICY_REMOVE;
			} else {
				RTE_LOG(ERR, CHANNEL_MONITOR,
					"Invalid command received in JSON\n");
				return -1;
			}
		} else if (!strcmp(key, "policy_type")) {
			char command[32];
			strlcpy(command, json_string_value(value), 32);
			if (!strcmp(command, "TIME")) {
				pkt->policy_to_use = TIME;
			} else if (!strcmp(command, "TRAFFIC")) {
				pkt->policy_to_use = TRAFFIC;
			} else if (!strcmp(command, "WORKLOAD")) {
				pkt->policy_to_use = WORKLOAD;
			} else if (!strcmp(command, "BRANCH_RATIO")) {
				pkt->policy_to_use = BRANCH_RATIO;
			} else {
				RTE_LOG(ERR, CHANNEL_MONITOR,
					"Wrong policy_type received in JSON\n");
				return -1;
			}
		} else if (!strcmp(key, "workload")) {
			char command[32];
			strlcpy(command, json_string_value(value), 32);
			if (!strcmp(command, "HIGH")) {
				pkt->workload = HIGH;
			} else if (!strcmp(command, "MEDIUM")) {
				pkt->workload = MEDIUM;
			} else if (!strcmp(command, "LOW")) {
				pkt->workload = LOW;
			} else {
				RTE_LOG(ERR, CHANNEL_MONITOR,
					"Wrong workload received in JSON\n");
				return -1;
			}
		} else if (!strcmp(key, "busy_hours")) {
			unsigned int i;
			size_t size = json_array_size(value);

			for (i = 0; i < size; i++) {
				int hour = (int)json_integer_value(
						json_array_get(value, i));
				pkt->timer_policy.busy_hours[i] = hour;
			}
		} else if (!strcmp(key, "quiet_hours")) {
			unsigned int i;
			size_t size = json_array_size(value);

			for (i = 0; i < size; i++) {
				int hour = (int)json_integer_value(
						json_array_get(value, i));
				pkt->timer_policy.quiet_hours[i] = hour;
			}
		} else if (!strcmp(key, "core_list")) {
			unsigned int i;
			size_t size = json_array_size(value);

			for (i = 0; i < size; i++) {
				int core = (int)json_integer_value(
						json_array_get(value, i));
				pkt->vcpu_to_control[i] = core;
			}
			pkt->num_vcpu = size;
		} else if (!strcmp(key, "mac_list")) {
			unsigned int i;
			size_t size = json_array_size(value);

			for (i = 0; i < size; i++) {
				char mac[32];
				strlcpy(mac,
					json_string_value(json_array_get(value, i)),
					32);
				set_policy_mac(pkt, i, mac);
			}
			pkt->nb_mac_to_monitor = size;
		} else if (!strcmp(key, "avg_packet_thresh")) {
			pkt->traffic_policy.avg_max_packet_thresh =
					(uint32_t)json_integer_value(value);
		} else if (!strcmp(key, "max_packet_thresh")) {
			pkt->traffic_policy.max_max_packet_thresh =
					(uint32_t)json_integer_value(value);
		} else if (!strcmp(key, "unit")) {
			char unit[32];
			strlcpy(unit, json_string_value(value), 32);
			if (!strcmp(unit, "SCALE_UP")) {
				pkt->unit = CPU_POWER_SCALE_UP;
			} else if (!strcmp(unit, "SCALE_DOWN")) {
				pkt->unit = CPU_POWER_SCALE_DOWN;
			} else if (!strcmp(unit, "SCALE_MAX")) {
				pkt->unit = CPU_POWER_SCALE_MAX;
			} else if (!strcmp(unit, "SCALE_MIN")) {
				pkt->unit = CPU_POWER_SCALE_MIN;
			} else if (!strcmp(unit, "ENABLE_TURBO")) {
				pkt->unit = CPU_POWER_ENABLE_TURBO;
			} else if (!strcmp(unit, "DISABLE_TURBO")) {
				pkt->unit = CPU_POWER_DISABLE_TURBO;
			} else {
				RTE_LOG(ERR, CHANNEL_MONITOR,
					"Invalid command received in JSON\n");
				return -1;
			}
		} else if (!strcmp(key, "resource_id")) {
			pkt->resource_id = (uint32_t)json_integer_value(value);
		} else {
			RTE_LOG(ERR, CHANNEL_MONITOR,
				"Unknown key received in JSON string: %s\n",
				key);
		}
	}
	return 0;
}
#endif

void channel_monitor_exit(void)
{
	run_loop = 0;
	rte_free(global_events_list);
}

static void
core_share(int pNo, int z, int x, int t)
{
	if (policies[pNo].core_share[z].pcpu == lvm_info[x].pcpus[t]) {
		if (strcmp(policies[pNo].pkt.vm_name,
				lvm_info[x].vm_name) != 0) {
			policies[pNo].core_share[z].status = 1;
			power_manager_scale_core_max(
					policies[pNo].core_share[z].pcpu);
		}
	}
}

static void
core_share_status(int pNo)
{

	int noVms = 0, noVcpus = 0, z, x, t;

	get_all_vm(&noVms, &noVcpus);

	/* Reset Core Share Status. */
	for (z = 0; z < noVcpus; z++)
		policies[pNo].core_share[z].status = 0;

	/* Foreach vcpu in a policy. */
	for (z = 0; z < policies[pNo].pkt.num_vcpu; z++) {
		/* Foreach VM on the platform. */
		for (x = 0; x < noVms; x++) {
			/* Foreach vcpu of VMs on platform. */
			for (t = 0; t < lvm_info[x].num_cpus; t++)
				core_share(pNo, z, x, t);
		}
	}
}


static int
pcpu_monitor(struct policy *pol, struct core_info *ci, int pcpu, int count)
{
	int ret = 0;

	if (pol->pkt.policy_to_use == BRANCH_RATIO) {
		ci->cd[pcpu].oob_enabled = 1;
		ret = add_core_to_monitor(pcpu);
		if (ret == 0)
			RTE_LOG(INFO, CHANNEL_MONITOR,
					"Monitoring pcpu %d OOB for %s\n",
					pcpu, pol->pkt.vm_name);
		else
			RTE_LOG(ERR, CHANNEL_MONITOR,
					"Error monitoring pcpu %d OOB for %s\n",
					pcpu, pol->pkt.vm_name);

	} else {
		pol->core_share[count].pcpu = pcpu;
		RTE_LOG(INFO, CHANNEL_MONITOR,
				"Monitoring pcpu %d for %s\n",
				pcpu, pol->pkt.vm_name);
	}
	return ret;
}

static void
get_pcpu_to_control(struct policy *pol)
{

	/* Convert vcpu to pcpu. */
	struct vm_info info;
	int pcpu, count;
	struct core_info *ci;

	ci = get_core_info();

	RTE_LOG(DEBUG, CHANNEL_MONITOR,
			"Looking for pcpu for %s\n", pol->pkt.vm_name);

	/*
	 * So now that we're handling virtual and physical cores, we need to
	 * differenciate between them when adding them to the branch monitor.
	 * Virtual cores need to be converted to physical cores.
	 */
	if (pol->pkt.core_type == CORE_TYPE_VIRTUAL) {
		/*
		 * If the cores in the policy are virtual, we need to map them
		 * to physical core. We look up the vm info and use that for
		 * the mapping.
		 */
		get_info_vm(pol->pkt.vm_name, &info);
		for (count = 0; count < pol->pkt.num_vcpu; count++) {
			pcpu = info.pcpu_map[pol->pkt.vcpu_to_control[count]];
			pcpu_monitor(pol, ci, pcpu, count);
		}
	} else {
		/*
		 * If the cores in the policy are physical, we just use
		 * those core id's directly.
		 */
		for (count = 0; count < pol->pkt.num_vcpu; count++) {
			pcpu = pol->pkt.vcpu_to_control[count];
			pcpu_monitor(pol, ci, pcpu, count);
		}
	}
}

static int
get_pfid(struct policy *pol)
{

	int i, x, ret = 0;

	for (i = 0; i < pol->pkt.nb_mac_to_monitor; i++) {

		RTE_ETH_FOREACH_DEV(x) {
			ret = rte_pmd_i40e_query_vfid_by_mac(x,
				(struct rte_ether_addr *)&(pol->pkt.vfid[i]));
			if (ret != -EINVAL) {
				pol->port[i] = x;
				break;
			}
		}
		if (ret == -EINVAL || ret == -ENOTSUP || ret == ENODEV) {
			RTE_LOG(INFO, CHANNEL_MONITOR,
				"Error with Policy. MAC not found on "
				"attached ports ");
			pol->enabled = 0;
			return ret;
		}
		pol->pfid[i] = ret;
	}
	return 1;
}

static int
update_policy(struct channel_packet *pkt)
{

	unsigned int updated = 0;
	int i;


	RTE_LOG(INFO, CHANNEL_MONITOR,
			"Applying policy for %s\n", pkt->vm_name);

	for (i = 0; i < MAX_CLIENTS; i++) {
		if (strcmp(policies[i].pkt.vm_name, pkt->vm_name) == 0) {
			/* Copy the contents of *pkt into the policy.pkt */
			policies[i].pkt = *pkt;
			get_pcpu_to_control(&policies[i]);
			/* Check Eth dev only for Traffic policy */
			if (policies[i].pkt.policy_to_use == TRAFFIC) {
				if (get_pfid(&policies[i]) < 0) {
					updated = 1;
					break;
				}
			}
			core_share_status(i);
			policies[i].enabled = 1;
			updated = 1;
		}
	}
	if (!updated) {
		for (i = 0; i < MAX_CLIENTS; i++) {
			if (policies[i].enabled == 0) {
				policies[i].pkt = *pkt;
				get_pcpu_to_control(&policies[i]);
				/* Check Eth dev only for Traffic policy */
				if (policies[i].pkt.policy_to_use == TRAFFIC) {
					if (get_pfid(&policies[i]) < 0) {
						updated = 1;
						break;
					}
				}
				core_share_status(i);
				policies[i].enabled = 1;
				break;
			}
		}
	}
	return 0;
}

static int
remove_policy(struct channel_packet *pkt __rte_unused)
{
	int i;

	/*
	 * Disabling the policy is simply a case of setting
	 * enabled to 0
	 */
	for (i = 0; i < MAX_CLIENTS; i++) {
		if (strcmp(policies[i].pkt.vm_name, pkt->vm_name) == 0) {
			policies[i].enabled = 0;
			return 0;
		}
	}
	return -1;
}

static uint64_t
get_pkt_diff(struct policy *pol)
{

	uint64_t vsi_pkt_count,
		vsi_pkt_total = 0,
		vsi_pkt_count_prev_total = 0;
	double rdtsc_curr, rdtsc_diff, diff;
	int x;
	struct rte_eth_stats vf_stats;

	for (x = 0; x < pol->pkt.nb_mac_to_monitor; x++) {

		/*Read vsi stats*/
		if (rte_pmd_i40e_get_vf_stats(x, pol->pfid[x], &vf_stats) == 0)
			vsi_pkt_count = vf_stats.ipackets;
		else
			vsi_pkt_count = -1;

		vsi_pkt_total += vsi_pkt_count;

		vsi_pkt_count_prev_total += vsi_pkt_count_prev[pol->pfid[x]];
		vsi_pkt_count_prev[pol->pfid[x]] = vsi_pkt_count;
	}

	rdtsc_curr = rte_rdtsc_precise();
	rdtsc_diff = rdtsc_curr - rdtsc_prev[pol->pfid[x-1]];
	rdtsc_prev[pol->pfid[x-1]] = rdtsc_curr;

	diff = (vsi_pkt_total - vsi_pkt_count_prev_total) *
			((double)rte_get_tsc_hz() / rdtsc_diff);

	return diff;
}

static void
apply_traffic_profile(struct policy *pol)
{

	int count;
	uint64_t diff = 0;

	diff = get_pkt_diff(pol);

	if (diff >= (pol->pkt.traffic_policy.max_max_packet_thresh)) {
		for (count = 0; count < pol->pkt.num_vcpu; count++) {
			if (pol->core_share[count].status != 1)
				power_manager_scale_core_max(
						pol->core_share[count].pcpu);
		}
	} else if (diff >= (pol->pkt.traffic_policy.avg_max_packet_thresh)) {
		for (count = 0; count < pol->pkt.num_vcpu; count++) {
			if (pol->core_share[count].status != 1)
				power_manager_scale_core_med(
						pol->core_share[count].pcpu);
		}
	} else if (diff < (pol->pkt.traffic_policy.avg_max_packet_thresh)) {
		for (count = 0; count < pol->pkt.num_vcpu; count++) {
			if (pol->core_share[count].status != 1)
				power_manager_scale_core_min(
						pol->core_share[count].pcpu);
		}
	}
}

static void
apply_time_profile(struct policy *pol)
{

	int count, x;
	struct timeval tv;
	struct tm *ptm;
	char time_string[40];

	/* Obtain the time of day, and convert it to a tm struct. */
	gettimeofday(&tv, NULL);
	ptm = localtime(&tv.tv_sec);
	/* Format the date and time, down to a single second. */
	strftime(time_string, sizeof(time_string), "%Y-%m-%d %H:%M:%S", ptm);

	for (x = 0; x < HOURS; x++) {

		if (ptm->tm_hour == pol->pkt.timer_policy.busy_hours[x]) {
			for (count = 0; count < pol->pkt.num_vcpu; count++) {
				if (pol->core_share[count].status != 1) {
					power_manager_scale_core_max(
						pol->core_share[count].pcpu);
				}
			}
			break;
		} else if (ptm->tm_hour ==
				pol->pkt.timer_policy.quiet_hours[x]) {
			for (count = 0; count < pol->pkt.num_vcpu; count++) {
				if (pol->core_share[count].status != 1) {
					power_manager_scale_core_min(
						pol->core_share[count].pcpu);
			}
		}
			break;
		} else if (ptm->tm_hour ==
			pol->pkt.timer_policy.hours_to_use_traffic_profile[x]) {
			apply_traffic_profile(pol);
			break;
		}
	}
}

static void
apply_workload_profile(struct policy *pol)
{

	int count;

	if (pol->pkt.workload == HIGH) {
		for (count = 0; count < pol->pkt.num_vcpu; count++) {
			if (pol->core_share[count].status != 1)
				power_manager_scale_core_max(
						pol->core_share[count].pcpu);
		}
	} else if (pol->pkt.workload == MEDIUM) {
		for (count = 0; count < pol->pkt.num_vcpu; count++) {
			if (pol->core_share[count].status != 1)
				power_manager_scale_core_med(
						pol->core_share[count].pcpu);
		}
	} else if (pol->pkt.workload == LOW) {
		for (count = 0; count < pol->pkt.num_vcpu; count++) {
			if (pol->core_share[count].status != 1)
				power_manager_scale_core_min(
						pol->core_share[count].pcpu);
		}
	}
}

static void
apply_policy(struct policy *pol)
{

	struct channel_packet *pkt = &pol->pkt;

	/*Check policy to use*/
	if (pkt->policy_to_use == TRAFFIC)
		apply_traffic_profile(pol);
	else if (pkt->policy_to_use == TIME)
		apply_time_profile(pol);
	else if (pkt->policy_to_use == WORKLOAD)
		apply_workload_profile(pol);
}

static int
process_request(struct channel_packet *pkt, struct channel_info *chan_info)
{
	int ret;

	if (chan_info == NULL)
		return -1;

	if (rte_atomic32_cmpset(&(chan_info->status), CHANNEL_MGR_CHANNEL_CONNECTED,
			CHANNEL_MGR_CHANNEL_PROCESSING) == 0)
		return -1;

	if (pkt->command == CPU_POWER) {
		unsigned int core_num;

		if (pkt->core_type == CORE_TYPE_VIRTUAL)
			core_num = get_pcpu(chan_info, pkt->resource_id);
		else
			core_num = pkt->resource_id;

		RTE_LOG(DEBUG, CHANNEL_MONITOR, "Processing requested cmd for cpu:%d\n",
			core_num);

		switch (pkt->unit) {
		case(CPU_POWER_SCALE_MIN):
			power_manager_scale_core_min(core_num);
			break;
		case(CPU_POWER_SCALE_MAX):
			power_manager_scale_core_max(core_num);
			break;
		case(CPU_POWER_SCALE_DOWN):
			power_manager_scale_core_down(core_num);
			break;
		case(CPU_POWER_SCALE_UP):
			power_manager_scale_core_up(core_num);
			break;
		case(CPU_POWER_ENABLE_TURBO):
			power_manager_enable_turbo_core(core_num);
			break;
		case(CPU_POWER_DISABLE_TURBO):
			power_manager_disable_turbo_core(core_num);
			break;
		default:
			break;
		}
	}

	if (pkt->command == PKT_POLICY) {
		RTE_LOG(INFO, CHANNEL_MONITOR, "Processing policy request %s\n",
				pkt->vm_name);
		update_policy(pkt);
		policy_is_set = 1;
	}

	if (pkt->command == PKT_POLICY_REMOVE) {
		ret = remove_policy(pkt);
		if (ret == 0)
			RTE_LOG(INFO, CHANNEL_MONITOR,
				 "Removed policy %s\n", pkt->vm_name);
		else
			RTE_LOG(INFO, CHANNEL_MONITOR,
				 "Policy %s does not exist\n", pkt->vm_name);
	}

	/*
	 * Return is not checked as channel status may have been set to DISABLED
	 * from management thread
	 */
	rte_atomic32_cmpset(&(chan_info->status), CHANNEL_MGR_CHANNEL_PROCESSING,
			CHANNEL_MGR_CHANNEL_CONNECTED);
	return 0;

}

int
add_channel_to_monitor(struct channel_info **chan_info)
{
	struct channel_info *info = *chan_info;
	struct epoll_event event;

	event.events = EPOLLIN;
	event.data.ptr = info;
	if (epoll_ctl(global_event_fd, EPOLL_CTL_ADD, info->fd, &event) < 0) {
		RTE_LOG(ERR, CHANNEL_MONITOR, "Unable to add channel '%s' "
				"to epoll\n", info->channel_path);
		return -1;
	}
	RTE_LOG(ERR, CHANNEL_MONITOR, "Added channel '%s' "
			"to monitor\n", info->channel_path);
	return 0;
}

int
remove_channel_from_monitor(struct channel_info *chan_info)
{
	if (epoll_ctl(global_event_fd, EPOLL_CTL_DEL,
			chan_info->fd, NULL) < 0) {
		RTE_LOG(ERR, CHANNEL_MONITOR, "Unable to remove channel '%s' "
				"from epoll\n", chan_info->channel_path);
		return -1;
	}
	return 0;
}

int
channel_monitor_init(void)
{
	global_event_fd = epoll_create1(0);
	if (global_event_fd == 0) {
		RTE_LOG(ERR, CHANNEL_MONITOR,
				"Error creating epoll context with error %s\n",
				strerror(errno));
		return -1;
	}
	global_events_list = rte_malloc("epoll_events",
			sizeof(*global_events_list)
			* MAX_EVENTS, RTE_CACHE_LINE_SIZE);
	if (global_events_list == NULL) {
		RTE_LOG(ERR, CHANNEL_MONITOR, "Unable to rte_malloc for "
				"epoll events\n");
		return -1;
	}
	return 0;
}

static void
read_binary_packet(struct channel_info *chan_info)
{
	struct channel_packet pkt;
	void *buffer = &pkt;
	int buffer_len = sizeof(pkt);
	int n_bytes, err = 0;

	while (buffer_len > 0) {
		n_bytes = read(chan_info->fd,
				buffer, buffer_len);
		if (n_bytes == buffer_len)
			break;
		if (n_bytes < 0) {
			err = errno;
			RTE_LOG(DEBUG, CHANNEL_MONITOR,
				"Received error on "
				"channel '%s' read: %s\n",
				chan_info->channel_path,
				strerror(err));
			remove_channel(&chan_info);
			break;
		}
		buffer = (char *)buffer + n_bytes;
		buffer_len -= n_bytes;
	}
	if (!err)
		process_request(&pkt, chan_info);
}

#ifdef USE_JANSSON
static void
read_json_packet(struct channel_info *chan_info)
{
	struct channel_packet pkt;
	int n_bytes, ret;
	json_t *root;
	json_error_t error;

	/* read opening brace to closing brace */
	do {
		int idx = 0;
		int indent = 0;
		do {
			n_bytes = read(chan_info->fd, &json_data[idx], 1);
			if (n_bytes == 0)
				break;
			if (json_data[idx] == '{')
				indent++;
			if (json_data[idx] == '}')
				indent--;
			if ((indent > 0) || (idx > 0))
				idx++;
			if (indent <= 0)
				json_data[idx] = 0;
			if (idx >= MAX_JSON_STRING_LEN-1)
				break;
		} while (indent > 0);

		json_data[idx] = '\0';

		if (strlen(json_data) == 0)
			continue;

		printf("got [%s]\n", json_data);

		root = json_loads(json_data, 0, &error);

		if (root) {
			/*
			 * Because our data is now in the json
			 * object, we can overwrite the pkt
			 * with a channel_packet struct, using
			 * parse_json_to_pkt()
			 */
			ret = parse_json_to_pkt(root, &pkt);
			json_decref(root);
			if (ret) {
				RTE_LOG(ERR, CHANNEL_MONITOR,
					"Error validating JSON profile data\n");
				break;
			}
			process_request(&pkt, chan_info);
		} else {
			RTE_LOG(ERR, CHANNEL_MONITOR,
					"JSON error on line %d: %s\n",
					error.line, error.text);
		}
	} while (n_bytes > 0);
}
#endif

void
run_channel_monitor(void)
{
	while (run_loop) {
		int n_events, i;

		n_events = epoll_wait(global_event_fd, global_events_list,
				MAX_EVENTS, 1);
		if (!run_loop)
			break;
		for (i = 0; i < n_events; i++) {
			struct channel_info *chan_info = (struct channel_info *)
					global_events_list[i].data.ptr;
			if ((global_events_list[i].events & EPOLLERR) ||
				(global_events_list[i].events & EPOLLHUP)) {
				RTE_LOG(INFO, CHANNEL_MONITOR,
						"Remote closed connection for "
						"channel '%s'\n",
						chan_info->channel_path);
				remove_channel(&chan_info);
				continue;
			}
			if (global_events_list[i].events & EPOLLIN) {

				switch (chan_info->type) {
				case CHANNEL_TYPE_BINARY:
					read_binary_packet(chan_info);
					break;
#ifdef USE_JANSSON
				case CHANNEL_TYPE_JSON:
					read_json_packet(chan_info);
					break;
#endif
				default:
					break;
				}
			}
		}
		rte_delay_us(time_period_ms*1000);
		if (policy_is_set) {
			int j;

			for (j = 0; j < MAX_CLIENTS; j++) {
				if (policies[j].enabled == 1)
					apply_policy(&policies[j]);
			}
		}
	}
}