xref: /dpdk/examples/vm_power_manager/channel_monitor.c (revision 04a8cb8ee95f82b22f18bb9de83b740b5b943a59)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4 
5 #include <unistd.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <stdint.h>
9 #include <signal.h>
10 #include <errno.h>
11 #include <string.h>
12 #include <fcntl.h>
13 #include <sys/types.h>
14 #include <sys/epoll.h>
15 #include <sys/queue.h>
16 #include <sys/time.h>
17 #include <sys/socket.h>
18 #include <sys/select.h>
19 #ifdef USE_JANSSON
20 #include <jansson.h>
21 #else
22 #pragma message "Jansson dev libs unavailable, not including JSON parsing"
23 #endif
24 #include <rte_string_fns.h>
25 #include <rte_log.h>
26 #include <rte_memory.h>
27 #include <rte_malloc.h>
28 #include <rte_atomic.h>
29 #include <rte_cycles.h>
30 #include <rte_ethdev.h>
31 #ifdef RTE_LIBRTE_I40E_PMD
32 #include <rte_pmd_i40e.h>
33 #endif
34 
35 #include <libvirt/libvirt.h>
36 #include "channel_monitor.h"
37 #include "channel_commands.h"
38 #include "channel_manager.h"
39 #include "power_manager.h"
40 #include "oob_monitor.h"
41 
42 #define RTE_LOGTYPE_CHANNEL_MONITOR RTE_LOGTYPE_USER1
43 
44 #define MAX_EVENTS 256
45 
46 uint64_t vsi_pkt_count_prev[384];
47 uint64_t rdtsc_prev[384];
48 #define MAX_JSON_STRING_LEN 1024
49 char json_data[MAX_JSON_STRING_LEN];
50 
51 double time_period_ms = 1;
52 static volatile unsigned run_loop = 1;
53 static int global_event_fd;
54 static unsigned int policy_is_set;
55 static struct epoll_event *global_events_list;
56 static struct policy policies[RTE_MAX_LCORE];
57 
58 #ifdef USE_JANSSON
59 
60 union PFID {
61 	struct rte_ether_addr addr;
62 	uint64_t pfid;
63 };
64 
65 static int
66 str_to_ether_addr(const char *a, struct rte_ether_addr *ether_addr)
67 {
68 	int i;
69 	char *end;
70 	unsigned long o[RTE_ETHER_ADDR_LEN];
71 
72 	i = 0;
73 	do {
74 		errno = 0;
75 		o[i] = strtoul(a, &end, 16);
76 		if (errno != 0 || end == a || (end[0] != ':' && end[0] != 0))
77 			return -1;
78 		a = end + 1;
79 	} while (++i != RTE_DIM(o) / sizeof(o[0]) && end[0] != 0);
80 
81 	/* Junk at the end of line */
82 	if (end[0] != 0)
83 		return -1;
84 
85 	/* Support the format XX:XX:XX:XX:XX:XX */
86 	if (i == RTE_ETHER_ADDR_LEN) {
87 		while (i-- != 0) {
88 			if (o[i] > UINT8_MAX)
89 				return -1;
90 			ether_addr->addr_bytes[i] = (uint8_t)o[i];
91 		}
92 	/* Support the format XXXX:XXXX:XXXX */
93 	} else if (i == RTE_ETHER_ADDR_LEN / 2) {
94 		while (i-- != 0) {
95 			if (o[i] > UINT16_MAX)
96 				return -1;
97 			ether_addr->addr_bytes[i * 2] =
98 					(uint8_t)(o[i] >> 8);
99 			ether_addr->addr_bytes[i * 2 + 1] =
100 					(uint8_t)(o[i] & 0xff);
101 		}
102 	/* unknown format */
103 	} else
104 		return -1;
105 
106 	return 0;
107 }
108 
109 static int
110 set_policy_mac(struct channel_packet *pkt, int idx, char *mac)
111 {
112 	union PFID pfid;
113 	int ret;
114 
115 	/* Use port MAC address as the vfid */
116 	ret = str_to_ether_addr(mac, &pfid.addr);
117 
118 	if (ret != 0) {
119 		RTE_LOG(ERR, CHANNEL_MONITOR,
120 			"Invalid mac address received in JSON\n");
121 		pkt->vfid[idx] = 0;
122 		return -1;
123 	}
124 
125 	printf("Received MAC Address: %02" PRIx8 ":%02" PRIx8 ":%02" PRIx8 ":"
126 			"%02" PRIx8 ":%02" PRIx8 ":%02" PRIx8 "\n",
127 			pfid.addr.addr_bytes[0], pfid.addr.addr_bytes[1],
128 			pfid.addr.addr_bytes[2], pfid.addr.addr_bytes[3],
129 			pfid.addr.addr_bytes[4], pfid.addr.addr_bytes[5]);
130 
131 	pkt->vfid[idx] = pfid.pfid;
132 	return 0;
133 }
134 
135 static char*
136 get_resource_name_from_chn_path(const char *channel_path)
137 {
138 	char *substr = NULL;
139 
140 	substr = strstr(channel_path, CHANNEL_MGR_FIFO_PATTERN_NAME);
141 
142 	return substr;
143 }
144 
145 static int
146 get_resource_id_from_vmname(const char *vm_name)
147 {
148 	int result = -1;
149 	int off = 0;
150 
151 	if (vm_name == NULL)
152 		return -1;
153 
154 	while (vm_name[off] != '\0') {
155 		if (isdigit(vm_name[off]))
156 			break;
157 		off++;
158 	}
159 	result = atoi(&vm_name[off]);
160 	if ((result == 0) && (vm_name[off] != '0'))
161 		return -1;
162 
163 	return result;
164 }
165 
166 static int
167 parse_json_to_pkt(json_t *element, struct channel_packet *pkt,
168 					const char *vm_name)
169 {
170 	const char *key;
171 	json_t *value;
172 	int ret;
173 	int resource_id;
174 
175 	memset(pkt, 0, sizeof(struct channel_packet));
176 
177 	pkt->nb_mac_to_monitor = 0;
178 	pkt->t_boost_status.tbEnabled = false;
179 	pkt->workload = LOW;
180 	pkt->policy_to_use = TIME;
181 	pkt->command = PKT_POLICY;
182 	pkt->core_type = CORE_TYPE_PHYSICAL;
183 
184 	if (vm_name == NULL) {
185 		RTE_LOG(ERR, CHANNEL_MONITOR,
186 			"vm_name is NULL, request rejected !\n");
187 		return -1;
188 	}
189 
190 	json_object_foreach(element, key, value) {
191 		if (!strcmp(key, "policy")) {
192 			/* Recurse in to get the contents of profile */
193 			ret = parse_json_to_pkt(value, pkt, vm_name);
194 			if (ret)
195 				return ret;
196 		} else if (!strcmp(key, "instruction")) {
197 			/* Recurse in to get the contents of instruction */
198 			ret = parse_json_to_pkt(value, pkt, vm_name);
199 			if (ret)
200 				return ret;
201 		} else if (!strcmp(key, "command")) {
202 			char command[32];
203 			strlcpy(command, json_string_value(value), 32);
204 			if (!strcmp(command, "power")) {
205 				pkt->command = CPU_POWER;
206 			} else if (!strcmp(command, "create")) {
207 				pkt->command = PKT_POLICY;
208 			} else if (!strcmp(command, "destroy")) {
209 				pkt->command = PKT_POLICY_REMOVE;
210 			} else {
211 				RTE_LOG(ERR, CHANNEL_MONITOR,
212 					"Invalid command received in JSON\n");
213 				return -1;
214 			}
215 		} else if (!strcmp(key, "policy_type")) {
216 			char command[32];
217 			strlcpy(command, json_string_value(value), 32);
218 			if (!strcmp(command, "TIME")) {
219 				pkt->policy_to_use = TIME;
220 			} else if (!strcmp(command, "TRAFFIC")) {
221 				pkt->policy_to_use = TRAFFIC;
222 			} else if (!strcmp(command, "WORKLOAD")) {
223 				pkt->policy_to_use = WORKLOAD;
224 			} else if (!strcmp(command, "BRANCH_RATIO")) {
225 				pkt->policy_to_use = BRANCH_RATIO;
226 			} else {
227 				RTE_LOG(ERR, CHANNEL_MONITOR,
228 					"Wrong policy_type received in JSON\n");
229 				return -1;
230 			}
231 		} else if (!strcmp(key, "workload")) {
232 			char command[32];
233 			strlcpy(command, json_string_value(value), 32);
234 			if (!strcmp(command, "HIGH")) {
235 				pkt->workload = HIGH;
236 			} else if (!strcmp(command, "MEDIUM")) {
237 				pkt->workload = MEDIUM;
238 			} else if (!strcmp(command, "LOW")) {
239 				pkt->workload = LOW;
240 			} else {
241 				RTE_LOG(ERR, CHANNEL_MONITOR,
242 					"Wrong workload received in JSON\n");
243 				return -1;
244 			}
245 		} else if (!strcmp(key, "busy_hours")) {
246 			unsigned int i;
247 			size_t size = json_array_size(value);
248 
249 			for (i = 0; i < size; i++) {
250 				int hour = (int)json_integer_value(
251 						json_array_get(value, i));
252 				pkt->timer_policy.busy_hours[i] = hour;
253 			}
254 		} else if (!strcmp(key, "quiet_hours")) {
255 			unsigned int i;
256 			size_t size = json_array_size(value);
257 
258 			for (i = 0; i < size; i++) {
259 				int hour = (int)json_integer_value(
260 						json_array_get(value, i));
261 				pkt->timer_policy.quiet_hours[i] = hour;
262 			}
263 		} else if (!strcmp(key, "mac_list")) {
264 			unsigned int i;
265 			size_t size = json_array_size(value);
266 
267 			for (i = 0; i < size; i++) {
268 				char mac[32];
269 				strlcpy(mac,
270 					json_string_value(json_array_get(value, i)),
271 					32);
272 				set_policy_mac(pkt, i, mac);
273 			}
274 			pkt->nb_mac_to_monitor = size;
275 		} else if (!strcmp(key, "avg_packet_thresh")) {
276 			pkt->traffic_policy.avg_max_packet_thresh =
277 					(uint32_t)json_integer_value(value);
278 		} else if (!strcmp(key, "max_packet_thresh")) {
279 			pkt->traffic_policy.max_max_packet_thresh =
280 					(uint32_t)json_integer_value(value);
281 		} else if (!strcmp(key, "unit")) {
282 			char unit[32];
283 			strlcpy(unit, json_string_value(value), 32);
284 			if (!strcmp(unit, "SCALE_UP")) {
285 				pkt->unit = CPU_POWER_SCALE_UP;
286 			} else if (!strcmp(unit, "SCALE_DOWN")) {
287 				pkt->unit = CPU_POWER_SCALE_DOWN;
288 			} else if (!strcmp(unit, "SCALE_MAX")) {
289 				pkt->unit = CPU_POWER_SCALE_MAX;
290 			} else if (!strcmp(unit, "SCALE_MIN")) {
291 				pkt->unit = CPU_POWER_SCALE_MIN;
292 			} else if (!strcmp(unit, "ENABLE_TURBO")) {
293 				pkt->unit = CPU_POWER_ENABLE_TURBO;
294 			} else if (!strcmp(unit, "DISABLE_TURBO")) {
295 				pkt->unit = CPU_POWER_DISABLE_TURBO;
296 			} else {
297 				RTE_LOG(ERR, CHANNEL_MONITOR,
298 					"Invalid command received in JSON\n");
299 				return -1;
300 			}
301 		} else {
302 			RTE_LOG(ERR, CHANNEL_MONITOR,
303 				"Unknown key received in JSON string: %s\n",
304 				key);
305 		}
306 
307 		resource_id = get_resource_id_from_vmname(vm_name);
308 		if (resource_id < 0) {
309 			RTE_LOG(ERR, CHANNEL_MONITOR,
310 				"Could not get resource_id from vm_name:%s\n",
311 				vm_name);
312 			return -1;
313 		}
314 		strlcpy(pkt->vm_name, vm_name, VM_MAX_NAME_SZ);
315 		pkt->resource_id = resource_id;
316 	}
317 	return 0;
318 }
319 #endif
320 
321 void channel_monitor_exit(void)
322 {
323 	run_loop = 0;
324 	rte_free(global_events_list);
325 }
326 
327 static void
328 core_share(int pNo, int z, int x, int t)
329 {
330 	if (policies[pNo].core_share[z].pcpu == lvm_info[x].pcpus[t]) {
331 		if (strcmp(policies[pNo].pkt.vm_name,
332 				lvm_info[x].vm_name) != 0) {
333 			policies[pNo].core_share[z].status = 1;
334 			power_manager_scale_core_max(
335 					policies[pNo].core_share[z].pcpu);
336 		}
337 	}
338 }
339 
340 static void
341 core_share_status(int pNo)
342 {
343 
344 	int noVms = 0, noVcpus = 0, z, x, t;
345 
346 	get_all_vm(&noVms, &noVcpus);
347 
348 	/* Reset Core Share Status. */
349 	for (z = 0; z < noVcpus; z++)
350 		policies[pNo].core_share[z].status = 0;
351 
352 	/* Foreach vcpu in a policy. */
353 	for (z = 0; z < policies[pNo].pkt.num_vcpu; z++) {
354 		/* Foreach VM on the platform. */
355 		for (x = 0; x < noVms; x++) {
356 			/* Foreach vcpu of VMs on platform. */
357 			for (t = 0; t < lvm_info[x].num_cpus; t++)
358 				core_share(pNo, z, x, t);
359 		}
360 	}
361 }
362 
363 
364 static int
365 pcpu_monitor(struct policy *pol, struct core_info *ci, int pcpu, int count)
366 {
367 	int ret = 0;
368 
369 	if (pol->pkt.policy_to_use == BRANCH_RATIO) {
370 		ci->cd[pcpu].oob_enabled = 1;
371 		ret = add_core_to_monitor(pcpu);
372 		if (ret == 0)
373 			RTE_LOG(INFO, CHANNEL_MONITOR,
374 					"Monitoring pcpu %d OOB for %s\n",
375 					pcpu, pol->pkt.vm_name);
376 		else
377 			RTE_LOG(ERR, CHANNEL_MONITOR,
378 					"Error monitoring pcpu %d OOB for %s\n",
379 					pcpu, pol->pkt.vm_name);
380 
381 	} else {
382 		pol->core_share[count].pcpu = pcpu;
383 		RTE_LOG(INFO, CHANNEL_MONITOR,
384 				"Monitoring pcpu %d for %s\n",
385 				pcpu, pol->pkt.vm_name);
386 	}
387 	return ret;
388 }
389 
390 static void
391 get_pcpu_to_control(struct policy *pol)
392 {
393 
394 	/* Convert vcpu to pcpu. */
395 	struct vm_info info;
396 	int pcpu, count;
397 	struct core_info *ci;
398 
399 	ci = get_core_info();
400 
401 	RTE_LOG(DEBUG, CHANNEL_MONITOR,
402 			"Looking for pcpu for %s\n", pol->pkt.vm_name);
403 
404 	/*
405 	 * So now that we're handling virtual and physical cores, we need to
406 	 * differenciate between them when adding them to the branch monitor.
407 	 * Virtual cores need to be converted to physical cores.
408 	 */
409 	if (pol->pkt.core_type == CORE_TYPE_VIRTUAL) {
410 		/*
411 		 * If the cores in the policy are virtual, we need to map them
412 		 * to physical core. We look up the vm info and use that for
413 		 * the mapping.
414 		 */
415 		get_info_vm(pol->pkt.vm_name, &info);
416 		for (count = 0; count < pol->pkt.num_vcpu; count++) {
417 			pcpu = info.pcpu_map[pol->pkt.vcpu_to_control[count]];
418 			pcpu_monitor(pol, ci, pcpu, count);
419 		}
420 	} else {
421 		/*
422 		 * If the cores in the policy are physical, we just use
423 		 * those core id's directly.
424 		 */
425 		for (count = 0; count < pol->pkt.num_vcpu; count++) {
426 			pcpu = pol->pkt.vcpu_to_control[count];
427 			pcpu_monitor(pol, ci, pcpu, count);
428 		}
429 	}
430 }
431 
432 static int
433 get_pfid(struct policy *pol)
434 {
435 
436 	int i, x, ret = 0;
437 
438 	for (i = 0; i < pol->pkt.nb_mac_to_monitor; i++) {
439 
440 		RTE_ETH_FOREACH_DEV(x) {
441 #ifdef RTE_LIBRTE_I40E_PMD
442 			ret = rte_pmd_i40e_query_vfid_by_mac(x,
443 				(struct rte_ether_addr *)&(pol->pkt.vfid[i]));
444 #else
445 			ret = -ENOTSUP;
446 #endif
447 			if (ret != -EINVAL) {
448 				pol->port[i] = x;
449 				break;
450 			}
451 		}
452 		if (ret == -EINVAL || ret == -ENOTSUP || ret == ENODEV) {
453 			RTE_LOG(INFO, CHANNEL_MONITOR,
454 				"Error with Policy. MAC not found on "
455 				"attached ports ");
456 			pol->enabled = 0;
457 			return ret;
458 		}
459 		pol->pfid[i] = ret;
460 	}
461 	return 1;
462 }
463 
464 static int
465 update_policy(struct channel_packet *pkt)
466 {
467 
468 	unsigned int updated = 0;
469 	unsigned int i;
470 
471 
472 	RTE_LOG(INFO, CHANNEL_MONITOR,
473 			"Applying policy for %s\n", pkt->vm_name);
474 
475 	for (i = 0; i < RTE_DIM(policies); i++) {
476 		if (strcmp(policies[i].pkt.vm_name, pkt->vm_name) == 0) {
477 			/* Copy the contents of *pkt into the policy.pkt */
478 			policies[i].pkt = *pkt;
479 			get_pcpu_to_control(&policies[i]);
480 			/* Check Eth dev only for Traffic policy */
481 			if (policies[i].pkt.policy_to_use == TRAFFIC) {
482 				if (get_pfid(&policies[i]) < 0) {
483 					updated = 1;
484 					break;
485 				}
486 			}
487 			core_share_status(i);
488 			policies[i].enabled = 1;
489 			updated = 1;
490 		}
491 	}
492 	if (!updated) {
493 		for (i = 0; i < RTE_DIM(policies); i++) {
494 			if (policies[i].enabled == 0) {
495 				policies[i].pkt = *pkt;
496 				get_pcpu_to_control(&policies[i]);
497 				/* Check Eth dev only for Traffic policy */
498 				if (policies[i].pkt.policy_to_use == TRAFFIC) {
499 					if (get_pfid(&policies[i]) < 0) {
500 						updated = 1;
501 						break;
502 					}
503 				}
504 				core_share_status(i);
505 				policies[i].enabled = 1;
506 				break;
507 			}
508 		}
509 	}
510 	return 0;
511 }
512 
513 static int
514 remove_policy(struct channel_packet *pkt __rte_unused)
515 {
516 	unsigned int i;
517 
518 	/*
519 	 * Disabling the policy is simply a case of setting
520 	 * enabled to 0
521 	 */
522 	for (i = 0; i < RTE_DIM(policies); i++) {
523 		if (strcmp(policies[i].pkt.vm_name, pkt->vm_name) == 0) {
524 			policies[i].enabled = 0;
525 			return 0;
526 		}
527 	}
528 	return -1;
529 }
530 
531 static uint64_t
532 get_pkt_diff(struct policy *pol)
533 {
534 
535 	uint64_t vsi_pkt_count,
536 		vsi_pkt_total = 0,
537 		vsi_pkt_count_prev_total = 0;
538 	double rdtsc_curr, rdtsc_diff, diff;
539 	int x;
540 #ifdef RTE_LIBRTE_I40E_PMD
541 	struct rte_eth_stats vf_stats;
542 #endif
543 
544 	for (x = 0; x < pol->pkt.nb_mac_to_monitor; x++) {
545 
546 #ifdef RTE_LIBRTE_I40E_PMD
547 		/*Read vsi stats*/
548 		if (rte_pmd_i40e_get_vf_stats(x, pol->pfid[x], &vf_stats) == 0)
549 			vsi_pkt_count = vf_stats.ipackets;
550 		else
551 			vsi_pkt_count = -1;
552 #else
553 		vsi_pkt_count = -1;
554 #endif
555 
556 		vsi_pkt_total += vsi_pkt_count;
557 
558 		vsi_pkt_count_prev_total += vsi_pkt_count_prev[pol->pfid[x]];
559 		vsi_pkt_count_prev[pol->pfid[x]] = vsi_pkt_count;
560 	}
561 
562 	rdtsc_curr = rte_rdtsc_precise();
563 	rdtsc_diff = rdtsc_curr - rdtsc_prev[pol->pfid[x-1]];
564 	rdtsc_prev[pol->pfid[x-1]] = rdtsc_curr;
565 
566 	diff = (vsi_pkt_total - vsi_pkt_count_prev_total) *
567 			((double)rte_get_tsc_hz() / rdtsc_diff);
568 
569 	return diff;
570 }
571 
572 static void
573 apply_traffic_profile(struct policy *pol)
574 {
575 
576 	int count;
577 	uint64_t diff = 0;
578 
579 	diff = get_pkt_diff(pol);
580 
581 	if (diff >= (pol->pkt.traffic_policy.max_max_packet_thresh)) {
582 		for (count = 0; count < pol->pkt.num_vcpu; count++) {
583 			if (pol->core_share[count].status != 1)
584 				power_manager_scale_core_max(
585 						pol->core_share[count].pcpu);
586 		}
587 	} else if (diff >= (pol->pkt.traffic_policy.avg_max_packet_thresh)) {
588 		for (count = 0; count < pol->pkt.num_vcpu; count++) {
589 			if (pol->core_share[count].status != 1)
590 				power_manager_scale_core_med(
591 						pol->core_share[count].pcpu);
592 		}
593 	} else if (diff < (pol->pkt.traffic_policy.avg_max_packet_thresh)) {
594 		for (count = 0; count < pol->pkt.num_vcpu; count++) {
595 			if (pol->core_share[count].status != 1)
596 				power_manager_scale_core_min(
597 						pol->core_share[count].pcpu);
598 		}
599 	}
600 }
601 
602 static void
603 apply_time_profile(struct policy *pol)
604 {
605 
606 	int count, x;
607 	struct timeval tv;
608 	struct tm *ptm;
609 	char time_string[40];
610 
611 	/* Obtain the time of day, and convert it to a tm struct. */
612 	gettimeofday(&tv, NULL);
613 	ptm = localtime(&tv.tv_sec);
614 	/* Format the date and time, down to a single second. */
615 	strftime(time_string, sizeof(time_string), "%Y-%m-%d %H:%M:%S", ptm);
616 
617 	for (x = 0; x < HOURS; x++) {
618 
619 		if (ptm->tm_hour == pol->pkt.timer_policy.busy_hours[x]) {
620 			for (count = 0; count < pol->pkt.num_vcpu; count++) {
621 				if (pol->core_share[count].status != 1) {
622 					power_manager_scale_core_max(
623 						pol->core_share[count].pcpu);
624 				}
625 			}
626 			break;
627 		} else if (ptm->tm_hour ==
628 				pol->pkt.timer_policy.quiet_hours[x]) {
629 			for (count = 0; count < pol->pkt.num_vcpu; count++) {
630 				if (pol->core_share[count].status != 1) {
631 					power_manager_scale_core_min(
632 						pol->core_share[count].pcpu);
633 			}
634 		}
635 			break;
636 		} else if (ptm->tm_hour ==
637 			pol->pkt.timer_policy.hours_to_use_traffic_profile[x]) {
638 			apply_traffic_profile(pol);
639 			break;
640 		}
641 	}
642 }
643 
644 static void
645 apply_workload_profile(struct policy *pol)
646 {
647 
648 	int count;
649 
650 	if (pol->pkt.workload == HIGH) {
651 		for (count = 0; count < pol->pkt.num_vcpu; count++) {
652 			if (pol->core_share[count].status != 1)
653 				power_manager_scale_core_max(
654 						pol->core_share[count].pcpu);
655 		}
656 	} else if (pol->pkt.workload == MEDIUM) {
657 		for (count = 0; count < pol->pkt.num_vcpu; count++) {
658 			if (pol->core_share[count].status != 1)
659 				power_manager_scale_core_med(
660 						pol->core_share[count].pcpu);
661 		}
662 	} else if (pol->pkt.workload == LOW) {
663 		for (count = 0; count < pol->pkt.num_vcpu; count++) {
664 			if (pol->core_share[count].status != 1)
665 				power_manager_scale_core_min(
666 						pol->core_share[count].pcpu);
667 		}
668 	}
669 }
670 
671 static void
672 apply_policy(struct policy *pol)
673 {
674 
675 	struct channel_packet *pkt = &pol->pkt;
676 
677 	/*Check policy to use*/
678 	if (pkt->policy_to_use == TRAFFIC)
679 		apply_traffic_profile(pol);
680 	else if (pkt->policy_to_use == TIME)
681 		apply_time_profile(pol);
682 	else if (pkt->policy_to_use == WORKLOAD)
683 		apply_workload_profile(pol);
684 }
685 
686 static int
687 write_binary_packet(struct channel_packet *pkt, struct channel_info *chan_info)
688 {
689 	int ret, buffer_len = sizeof(*pkt);
690 	void *buffer = pkt;
691 
692 	if (chan_info->fd < 0) {
693 		RTE_LOG(ERR, CHANNEL_MONITOR, "Channel is not connected\n");
694 		return -1;
695 	}
696 
697 	while (buffer_len > 0) {
698 		ret = write(chan_info->fd, buffer, buffer_len);
699 		if (ret == -1) {
700 			if (errno == EINTR)
701 				continue;
702 			RTE_LOG(ERR, CHANNEL_MONITOR, "Write function failed due to %s.\n",
703 					strerror(errno));
704 			return -1;
705 		}
706 		buffer = (char *)buffer + ret;
707 		buffer_len -= ret;
708 	}
709 	return 0;
710 }
711 
712 static int
713 send_ack_for_received_cmd(struct channel_packet *pkt,
714 		struct channel_info *chan_info,
715 		uint32_t command)
716 {
717 	pkt->command = command;
718 	return write_binary_packet(pkt, chan_info);
719 }
720 
721 static int
722 process_request(struct channel_packet *pkt, struct channel_info *chan_info)
723 {
724 	int ret;
725 
726 	if (chan_info == NULL)
727 		return -1;
728 
729 	if (rte_atomic32_cmpset(&(chan_info->status), CHANNEL_MGR_CHANNEL_CONNECTED,
730 			CHANNEL_MGR_CHANNEL_PROCESSING) == 0)
731 		return -1;
732 
733 	if (pkt->command == CPU_POWER) {
734 		unsigned int core_num;
735 
736 		if (pkt->core_type == CORE_TYPE_VIRTUAL)
737 			core_num = get_pcpu(chan_info, pkt->resource_id);
738 		else
739 			core_num = pkt->resource_id;
740 
741 		RTE_LOG(DEBUG, CHANNEL_MONITOR, "Processing requested cmd for cpu:%d\n",
742 			core_num);
743 
744 		bool valid_unit = true;
745 		int scale_res;
746 
747 		switch (pkt->unit) {
748 		case(CPU_POWER_SCALE_MIN):
749 			scale_res = power_manager_scale_core_min(core_num);
750 			break;
751 		case(CPU_POWER_SCALE_MAX):
752 			scale_res = power_manager_scale_core_max(core_num);
753 			break;
754 		case(CPU_POWER_SCALE_DOWN):
755 			scale_res = power_manager_scale_core_down(core_num);
756 			break;
757 		case(CPU_POWER_SCALE_UP):
758 			scale_res = power_manager_scale_core_up(core_num);
759 			break;
760 		case(CPU_POWER_ENABLE_TURBO):
761 			scale_res = power_manager_enable_turbo_core(core_num);
762 			break;
763 		case(CPU_POWER_DISABLE_TURBO):
764 			scale_res = power_manager_disable_turbo_core(core_num);
765 			break;
766 		default:
767 			valid_unit = false;
768 			break;
769 		}
770 
771 		if (valid_unit) {
772 			ret = send_ack_for_received_cmd(pkt,
773 					chan_info,
774 					scale_res > 0 ?
775 						CPU_POWER_CMD_ACK :
776 						CPU_POWER_CMD_NACK);
777 			if (ret < 0)
778 				RTE_LOG(DEBUG, CHANNEL_MONITOR, "Error during sending ack command.\n");
779 		} else
780 			RTE_LOG(DEBUG, CHANNEL_MONITOR, "Unexpected unit type.\n");
781 
782 	}
783 
784 	if (pkt->command == PKT_POLICY) {
785 		RTE_LOG(INFO, CHANNEL_MONITOR, "Processing policy request %s\n",
786 				pkt->vm_name);
787 		int ret = send_ack_for_received_cmd(pkt,
788 				chan_info,
789 				CPU_POWER_CMD_ACK);
790 		if (ret < 0)
791 			RTE_LOG(DEBUG, CHANNEL_MONITOR, "Error during sending ack command.\n");
792 		update_policy(pkt);
793 		policy_is_set = 1;
794 	}
795 
796 	if (pkt->command == PKT_POLICY_REMOVE) {
797 		ret = remove_policy(pkt);
798 		if (ret == 0)
799 			RTE_LOG(INFO, CHANNEL_MONITOR,
800 				 "Removed policy %s\n", pkt->vm_name);
801 		else
802 			RTE_LOG(INFO, CHANNEL_MONITOR,
803 				 "Policy %s does not exist\n", pkt->vm_name);
804 	}
805 
806 	/*
807 	 * Return is not checked as channel status may have been set to DISABLED
808 	 * from management thread
809 	 */
810 	rte_atomic32_cmpset(&(chan_info->status), CHANNEL_MGR_CHANNEL_PROCESSING,
811 			CHANNEL_MGR_CHANNEL_CONNECTED);
812 	return 0;
813 
814 }
815 
816 int
817 add_channel_to_monitor(struct channel_info **chan_info)
818 {
819 	struct channel_info *info = *chan_info;
820 	struct epoll_event event;
821 
822 	event.events = EPOLLIN;
823 	event.data.ptr = info;
824 	if (epoll_ctl(global_event_fd, EPOLL_CTL_ADD, info->fd, &event) < 0) {
825 		RTE_LOG(ERR, CHANNEL_MONITOR, "Unable to add channel '%s' "
826 				"to epoll\n", info->channel_path);
827 		return -1;
828 	}
829 	RTE_LOG(ERR, CHANNEL_MONITOR, "Added channel '%s' "
830 			"to monitor\n", info->channel_path);
831 	return 0;
832 }
833 
834 int
835 remove_channel_from_monitor(struct channel_info *chan_info)
836 {
837 	if (epoll_ctl(global_event_fd, EPOLL_CTL_DEL,
838 			chan_info->fd, NULL) < 0) {
839 		RTE_LOG(ERR, CHANNEL_MONITOR, "Unable to remove channel '%s' "
840 				"from epoll\n", chan_info->channel_path);
841 		return -1;
842 	}
843 	return 0;
844 }
845 
846 int
847 channel_monitor_init(void)
848 {
849 	global_event_fd = epoll_create1(0);
850 	if (global_event_fd == 0) {
851 		RTE_LOG(ERR, CHANNEL_MONITOR,
852 				"Error creating epoll context with error %s\n",
853 				strerror(errno));
854 		return -1;
855 	}
856 	global_events_list = rte_malloc("epoll_events",
857 			sizeof(*global_events_list)
858 			* MAX_EVENTS, RTE_CACHE_LINE_SIZE);
859 	if (global_events_list == NULL) {
860 		RTE_LOG(ERR, CHANNEL_MONITOR, "Unable to rte_malloc for "
861 				"epoll events\n");
862 		return -1;
863 	}
864 	return 0;
865 }
866 
867 static void
868 read_binary_packet(struct channel_info *chan_info)
869 {
870 	struct channel_packet pkt;
871 	void *buffer = &pkt;
872 	int buffer_len = sizeof(pkt);
873 	int n_bytes, err = 0;
874 
875 	while (buffer_len > 0) {
876 		n_bytes = read(chan_info->fd,
877 				buffer, buffer_len);
878 		if (n_bytes == buffer_len)
879 			break;
880 		if (n_bytes < 0) {
881 			err = errno;
882 			RTE_LOG(DEBUG, CHANNEL_MONITOR,
883 				"Received error on "
884 				"channel '%s' read: %s\n",
885 				chan_info->channel_path,
886 				strerror(err));
887 			remove_channel(&chan_info);
888 			break;
889 		}
890 		buffer = (char *)buffer + n_bytes;
891 		buffer_len -= n_bytes;
892 	}
893 	if (!err)
894 		process_request(&pkt, chan_info);
895 }
896 
897 #ifdef USE_JANSSON
898 static void
899 read_json_packet(struct channel_info *chan_info)
900 {
901 	struct channel_packet pkt;
902 	int n_bytes, ret;
903 	json_t *root;
904 	json_error_t error;
905 	const char *resource_name;
906 	char *start, *end;
907 	uint32_t n;
908 
909 
910 	/* read opening brace to closing brace */
911 	do {
912 		int idx = 0;
913 		int indent = 0;
914 		do {
915 			n_bytes = read(chan_info->fd, &json_data[idx], 1);
916 			if (n_bytes == 0)
917 				break;
918 			if (json_data[idx] == '{')
919 				indent++;
920 			if (json_data[idx] == '}')
921 				indent--;
922 			if ((indent > 0) || (idx > 0))
923 				idx++;
924 			if (indent <= 0)
925 				json_data[idx] = 0;
926 			if (idx >= MAX_JSON_STRING_LEN-1)
927 				break;
928 		} while (indent > 0);
929 
930 		json_data[idx] = '\0';
931 
932 		if (strlen(json_data) == 0)
933 			continue;
934 
935 		printf("got [%s]\n", json_data);
936 
937 		root = json_loads(json_data, 0, &error);
938 
939 		if (root) {
940 			resource_name = get_resource_name_from_chn_path(
941 				chan_info->channel_path);
942 			/*
943 			 * Because our data is now in the json
944 			 * object, we can overwrite the pkt
945 			 * with a channel_packet struct, using
946 			 * parse_json_to_pkt()
947 			 */
948 			ret = parse_json_to_pkt(root, &pkt, resource_name);
949 			json_decref(root);
950 			if (ret) {
951 				RTE_LOG(ERR, CHANNEL_MONITOR,
952 					"Error validating JSON profile data\n");
953 				break;
954 			}
955 			start = strstr(pkt.vm_name,
956 					CHANNEL_MGR_FIFO_PATTERN_NAME);
957 			if (start != NULL) {
958 				/* move past pattern to start of fifo id */
959 				start += strlen(CHANNEL_MGR_FIFO_PATTERN_NAME);
960 
961 				end = start;
962 				n = (uint32_t)strtoul(start, &end, 10);
963 
964 				if (end[0] == '\0') {
965 					/* Add core id to core list */
966 					pkt.num_vcpu = 1;
967 					pkt.vcpu_to_control[0] = n;
968 					process_request(&pkt, chan_info);
969 				} else {
970 					RTE_LOG(ERR, CHANNEL_MONITOR,
971 						"Cannot extract core id from fifo name\n");
972 				}
973 			} else {
974 				process_request(&pkt, chan_info);
975 			}
976 		} else {
977 			RTE_LOG(ERR, CHANNEL_MONITOR,
978 					"JSON error on line %d: %s\n",
979 					error.line, error.text);
980 		}
981 	} while (n_bytes > 0);
982 }
983 #endif
984 
985 void
986 run_channel_monitor(void)
987 {
988 	while (run_loop) {
989 		int n_events, i;
990 
991 		n_events = epoll_wait(global_event_fd, global_events_list,
992 				MAX_EVENTS, 1);
993 		if (!run_loop)
994 			break;
995 		for (i = 0; i < n_events; i++) {
996 			struct channel_info *chan_info = (struct channel_info *)
997 					global_events_list[i].data.ptr;
998 			if ((global_events_list[i].events & EPOLLERR) ||
999 				(global_events_list[i].events & EPOLLHUP)) {
1000 				RTE_LOG(INFO, CHANNEL_MONITOR,
1001 						"Remote closed connection for "
1002 						"channel '%s'\n",
1003 						chan_info->channel_path);
1004 				remove_channel(&chan_info);
1005 				continue;
1006 			}
1007 			if (global_events_list[i].events & EPOLLIN) {
1008 
1009 				switch (chan_info->type) {
1010 				case CHANNEL_TYPE_BINARY:
1011 					read_binary_packet(chan_info);
1012 					break;
1013 #ifdef USE_JANSSON
1014 				case CHANNEL_TYPE_JSON:
1015 					read_json_packet(chan_info);
1016 					break;
1017 #endif
1018 				default:
1019 					break;
1020 				}
1021 			}
1022 		}
1023 		rte_delay_us(time_period_ms*1000);
1024 		if (policy_is_set) {
1025 			unsigned int j;
1026 
1027 			for (j = 0; j < RTE_DIM(policies); j++) {
1028 				if (policies[j].enabled == 1)
1029 					apply_policy(&policies[j]);
1030 			}
1031 		}
1032 	}
1033 }
1034