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